GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(claude): fork history in the provider environment · pingdotgg/t3code@c1310b2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/server/src/orchestration-v2/Adapters
    • ClaudeAdapterV2.test.ts
    • ClaudeAdapterV2.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+66-1Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as NodeOS from "node:os";
2+
import * as NodeCrypto from "node:crypto";
23

34
import type {
45
Query as ClaudeQuery,
@@ -58,6 +59,7 @@ import { WorktreeToolkit } from "../../mcp/toolkits/worktree/tools.ts";
5859
import { ThreadToolkit } from "../../mcp/toolkits/thread/tools.ts";
5960
import { OrchestratorToolkit } from "../../mcp/toolkits/orchestrator/tools.ts";
6061
import { ClaudeExecutableFileCheck } from "../../provider/Drivers/ClaudeExecutable.ts";
62+
import * as ProviderEventLoggers from "../../provider/Layers/ProviderEventLoggers.ts";
6163
import type { EventNdjsonLogger } from "../../provider/Layers/EventNdjsonLogger.ts";
6264
import {
6365
ProviderAdapterV2RuntimePolicy,
@@ -1605,6 +1607,67 @@ describe("ClaudeAdapterV2 attachments", () => {
16051607
});
16061608

16071609
describe("ClaudeAdapterV2 native fork", () => {
1610+
it.effect(
1611+
"forks history from the provider environment without changing the server environment",
1612+
() =>
1613+
Effect.gen(function* () {
1614+
const fileSystem = yield* FileSystem.FileSystem;
1615+
const path = yield* Path.Path;
1616+
const runner = yield* ClaudeAdapterV2.ClaudeAgentSdkQueryRunner;
1617+
const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-claude-fork-" });
1618+
const configDir = path.join(root, "provider");
1619+
const cwd = path.join(root, "project");
1620+
const projectDir = path.join(configDir, "projects", cwd.replace(/[^a-zA-Z0-9]/g, "-"));
1621+
yield* fileSystem.makeDirectory(projectDir, { recursive: true });
1622+
yield* fileSystem.makeDirectory(cwd);
1623+
const sessionId = NodeCrypto.randomUUID();
1624+
const cursor = NodeCrypto.randomUUID();
1625+
const source =
1626+
[cursor, NodeCrypto.randomUUID()]
1627+
.map((uuid, index) =>
1628+
JSON.stringify({
1629+
uuid,
1630+
parentUuid: index === 0 ? null : cursor,
1631+
sessionId,
1632+
cwd,
1633+
type: "user",
1634+
isSidechain: false,
1635+
timestamp: "2026-01-01T00:00:00.000Z",
1636+
message: { role: "user", content: index === 0 ? "checkpoint" : "later" },
1637+
}),
1638+
)
1639+
.join("\n") + "\n";
1640+
const sourcePath = path.join(projectDir, sessionId + ".jsonl");
1641+
yield* fileSystem.writeFileString(sourcePath, source);
1642+
const environment = { ...process.env };
1643+
const fork = yield* runner.forkSession({
1644+
environment: { ...environment, CLAUDE_CONFIG_DIR: configDir },
1645+
sessionId,
1646+
options: { dir: cwd, upToMessageId: cursor },
1647+
threadId: ThreadId.make("fork-target"),
1648+
providerSessionId: ProviderSessionId.make("fork-session"),
1649+
});
1650+
assert.notEqual(fork.sessionId, sessionId);
1651+
const forkText = yield* fileSystem.readFileString(
1652+
path.join(projectDir, fork.sessionId + ".jsonl"),
1653+
);
1654+
assert.include(forkText, "checkpoint");
1655+
assert.notInclude(forkText, "later");
1656+
assert.equal(yield* fileSystem.readFileString(sourcePath), source);
1657+
assert.deepEqual(process.env, environment);
1658+
}).pipe(
1659+
Effect.provide(ClaudeAdapterV2.claudeAgentSdkQueryRunnerLiveLayer),
1660+
Effect.provide(
1661+
Layer.succeed(ProviderEventLoggers.ProviderEventLoggers, {
1662+
native: undefined,
1663+
canonical: undefined,
1664+
}),
1665+
),
1666+
Effect.provide(NodeServices.layer),
1667+
Effect.scoped,
1668+
),
1669+
);
1670+
16081671
it.effect("forks at the source assistant cursor and resumes the forked session", () =>
16091672
Effect.scoped(
16101673
Effect.gen(function* () {
@@ -1615,6 +1678,7 @@ describe("ClaudeAdapterV2 native fork", () => {
16151678
});
16161679
const openedQueries: Array<ClaudeAdapterV2.ClaudeAgentSdkQueryOpenInput> = [];
16171680
const forkCalls: Array<{
1681+
readonly environment: NodeJS.ProcessEnv;
16181682
readonly sessionId: string;
16191683
readonly options: unknown;
16201684
readonly threadId: ThreadId;
@@ -1623,7 +1687,7 @@ describe("ClaudeAdapterV2 native fork", () => {
16231687
const adapter = ClaudeAdapterV2.makeClaudeAdapterV2({
16241688
instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
16251689
settings: DEFAULT_CLAUDE_SETTINGS,
1626-
environment: {},
1690+
environment: { CLAUDE_CONFIG_DIR: "/provider-home" },
16271691
attachmentsDir,
16281692
fileSystem,
16291693
path: yield* Path.Path,
@@ -1706,6 +1770,7 @@ describe("ClaudeAdapterV2 native fork", () => {
17061770

17071771
assert.deepEqual(forkCalls, [
17081772
{
1773+
environment: { CLAUDE_CONFIG_DIR: "/provider-home" },
17091774
sessionId: "source-native-session",
17101775
options: {
17111776
dir: "/workspace",
Collapse file

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+52-6Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as NodeCrypto from "node:crypto";
2+
import * as NodeSea from "node:sea";
3+
import * as NodeURL from "node:url";
24

35
import { makeProviderTextDeltaCoalescer } from "./ProviderTextDeltaCoalescer.ts";
46
import {
@@ -10,7 +12,6 @@ import { isWorkspaceImagePreviewPath } from "@t3tools/shared/filePreview";
1012
import { normalizeClaudeTurnTokenUsage } from "../../provider/ClaudeTurnTokenUsage.ts";
1113
import {
1214
type CanUseTool,
13-
forkSession as forkClaudeSession,
1415
type ForkSessionOptions,
1516
type ForkSessionResult,
1617
getSubagentMessages,
@@ -85,6 +86,10 @@ import * as Queue from "effect/Queue";
8586
import * as Ref from "effect/Ref";
8687
import * as Schema from "effect/Schema";
8788
import * as Stream from "effect/Stream";
89+
import * as ChildProcess from "effect/unstable/process/ChildProcess";
90+
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
91+
92+
import { collectUint8StreamText } from "../../stream/collectUint8StreamText.ts";
8893

8994
import { resolveAttachmentPath } from "../../attachmentStore.ts";
9095
import { resolveClaudeSdkExecutablePath } from "../../provider/Drivers/ClaudeExecutable.ts";
@@ -378,6 +383,7 @@ export class ClaudeAgentSdkQueryRunner extends Context.Service<
378383
>()("t3/orchestration-v2/Adapters/ClaudeAdapterV2/ClaudeAgentSdkQueryRunner") {}
379384

380385
export interface ClaudeAgentSdkSessionForkInput {
386+
readonly environment: NodeJS.ProcessEnv;
381387
readonly sessionId: string;
382388
readonly options: ForkSessionOptions;
383389
readonly threadId: ThreadId;
@@ -591,14 +597,21 @@ export function makeClaudeAgentSdkProtocolLogger(input: {
591597
};
592598
}
593599

600+
const decodeForkSessionResult = Schema.decodeEffect(
601+
Schema.fromJsonString(Schema.Struct({ sessionId: Schema.String })),
602+
);
603+
594604
export const claudeAgentSdkQueryRunnerLiveLayer: Layer.Layer<
595605
ClaudeAgentSdkQueryRunner,
596606
never,
597-
Crypto.Crypto | ProviderEventLoggers.ProviderEventLoggers
607+
| Crypto.Crypto
608+
| ProviderEventLoggers.ProviderEventLoggers
609+
| ChildProcessSpawner.ChildProcessSpawner
598610
> = Layer.effect(
599611
ClaudeAgentSdkQueryRunner,
600612
Effect.gen(function* () {
601613
const crypto = yield* Crypto.Crypto;
614+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
602615
const { native: nativeEventLogger } = yield* ProviderEventLoggers.ProviderEventLoggers;
603616

604617
return ClaudeAgentSdkQueryRunner.of({
@@ -744,10 +757,42 @@ export const claudeAgentSdkQueryRunnerLiveLayer: Layer.Layer<
744757
options: input.options,
745758
},
746759
});
747-
const result = yield* Effect.tryPromise({
748-
try: () => forkClaudeSession(input.sessionId, input.options),
749-
catch: (cause) => queryRunnerError(cause, "forkSession"),
750-
});
760+
// SDK history helpers read process.env rather than query options.env.
761+
const result = yield* Effect.gen(function* () {
762+
const args = ["forkSession", input.sessionId, JSON.stringify(input.options)];
763+
const workerArgs = NodeSea.isSea()
764+
? ["__claude-history", ...args]
765+
: [
766+
NodeURL.fileURLToPath(
767+
new URL(
768+
import.meta.url.endsWith(".ts")
769+
? "../../claude-history-worker.ts"
770+
: "./claude-history-worker.mjs",
771+
import.meta.url,
772+
),
773+
),
774+
...args,
775+
];
776+
const child = yield* spawner.spawn(
777+
ChildProcess.make(process.execPath, workerArgs, {
778+
env: input.environment,
779+
extendEnv: false,
780+
}),
781+
);
782+
const [stdout, stderr, exitCode] = yield* Effect.all(
783+
[
784+
collectUint8StreamText({ stream: child.stdout, maxBytes: 1024 * 1024 }),
785+
collectUint8StreamText({ stream: child.stderr, maxBytes: 1024 * 1024 }),
786+
child.exitCode,
787+
],
788+
{ concurrency: "unbounded" },
789+
);
790+
if (exitCode !== 0) return yield* Effect.fail(new Error(stderr.text));
791+
return yield* decodeForkSessionResult(stdout.text);
792+
}).pipe(
793+
Effect.scoped,
794+
Effect.mapError((cause) => queryRunnerError(cause, "forkSession")),
795+
);
751796
yield* logProtocolEvent({
752797
direction: "incoming",
753798
stage: "decoded",
@@ -7663,6 +7708,7 @@ export function makeClaudeAdapterV2(
76637708
...(upToMessageId === undefined ? {} : { upToMessageId }),
76647709
};
76657710
const forked = yield* queryRunner.forkSession({
7711+
environment: adapterOptions.environment,
76667712
sessionId: sourceNativeThreadId,
76677713
options: forkOptions,
76687714
threadId: forkInput.targetThreadId,

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.