GeezerVison is showing github.com — fix(claude): fork history in the provider environment · pingdotgg/t3code@c1310b2
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/orchestration-v2/Adapters
Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
| 1 | 1 | import * as NodeOS from "node:os"; | | 2 | +import * as NodeCrypto from "node:crypto"; | 2 | 3 |
| 3 | 4 | import type { | 4 | 5 | Query as ClaudeQuery, | @@ -58,6 +59,7 @@ import { WorktreeToolkit } from "../../mcp/toolkits/worktree/tools.ts"; | 58 | 59 | import { ThreadToolkit } from "../../mcp/toolkits/thread/tools.ts"; | 59 | 60 | import { OrchestratorToolkit } from "../../mcp/toolkits/orchestrator/tools.ts"; | 60 | 61 | import { ClaudeExecutableFileCheck } from "../../provider/Drivers/ClaudeExecutable.ts"; | | 62 | +import * as ProviderEventLoggers from "../../provider/Layers/ProviderEventLoggers.ts"; | 61 | 63 | import type { EventNdjsonLogger } from "../../provider/Layers/EventNdjsonLogger.ts"; | 62 | 64 | import { | 63 | 65 | ProviderAdapterV2RuntimePolicy, | @@ -1605,6 +1607,67 @@ describe("ClaudeAdapterV2 attachments", () => { | 1605 | 1607 | }); | 1606 | 1608 |
| 1607 | 1609 | 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 | + | 1608 | 1671 | it.effect("forks at the source assistant cursor and resumes the forked session", () => | 1609 | 1672 | Effect.scoped( | 1610 | 1673 | Effect.gen(function* () { | @@ -1615,6 +1678,7 @@ describe("ClaudeAdapterV2 native fork", () => { | 1615 | 1678 | }); | 1616 | 1679 | const openedQueries: Array<ClaudeAdapterV2.ClaudeAgentSdkQueryOpenInput> = []; | 1617 | 1680 | const forkCalls: Array<{ | | 1681 | + readonly environment: NodeJS.ProcessEnv; | 1618 | 1682 | readonly sessionId: string; | 1619 | 1683 | readonly options: unknown; | 1620 | 1684 | readonly threadId: ThreadId; | @@ -1623,7 +1687,7 @@ describe("ClaudeAdapterV2 native fork", () => { | 1623 | 1687 | const adapter = ClaudeAdapterV2.makeClaudeAdapterV2({ | 1624 | 1688 | instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID, | 1625 | 1689 | settings: DEFAULT_CLAUDE_SETTINGS, | 1626 | | - environment: {}, | | 1690 | + environment: { CLAUDE_CONFIG_DIR: "/provider-home" }, | 1627 | 1691 | attachmentsDir, | 1628 | 1692 | fileSystem, | 1629 | 1693 | path: yield* Path.Path, | @@ -1706,6 +1770,7 @@ describe("ClaudeAdapterV2 native fork", () => { | 1706 | 1770 |
| 1707 | 1771 | assert.deepEqual(forkCalls, [ | 1708 | 1772 | { | | 1773 | + environment: { CLAUDE_CONFIG_DIR: "/provider-home" }, | 1709 | 1774 | sessionId: "source-native-session", | 1710 | 1775 | options: { | 1711 | 1776 | dir: "/workspace", | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 1 | 1 | import * as NodeCrypto from "node:crypto"; | | 2 | +import * as NodeSea from "node:sea"; | | 3 | +import * as NodeURL from "node:url"; | 2 | 4 |
| 3 | 5 | import { makeProviderTextDeltaCoalescer } from "./ProviderTextDeltaCoalescer.ts"; | 4 | 6 | import { | @@ -10,7 +12,6 @@ import { isWorkspaceImagePreviewPath } from "@t3tools/shared/filePreview"; | 10 | 12 | import { normalizeClaudeTurnTokenUsage } from "../../provider/ClaudeTurnTokenUsage.ts"; | 11 | 13 | import { | 12 | 14 | type CanUseTool, | 13 | | - forkSession as forkClaudeSession, | 14 | 15 | type ForkSessionOptions, | 15 | 16 | type ForkSessionResult, | 16 | 17 | getSubagentMessages, | @@ -85,6 +86,10 @@ import * as Queue from "effect/Queue"; | 85 | 86 | import * as Ref from "effect/Ref"; | 86 | 87 | import * as Schema from "effect/Schema"; | 87 | 88 | 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"; | 88 | 93 |
| 89 | 94 | import { resolveAttachmentPath } from "../../attachmentStore.ts"; | 90 | 95 | import { resolveClaudeSdkExecutablePath } from "../../provider/Drivers/ClaudeExecutable.ts"; | @@ -378,6 +383,7 @@ export class ClaudeAgentSdkQueryRunner extends Context.Service< | 378 | 383 | >()("t3/orchestration-v2/Adapters/ClaudeAdapterV2/ClaudeAgentSdkQueryRunner") {} | 379 | 384 |
| 380 | 385 | export interface ClaudeAgentSdkSessionForkInput { | | 386 | + readonly environment: NodeJS.ProcessEnv; | 381 | 387 | readonly sessionId: string; | 382 | 388 | readonly options: ForkSessionOptions; | 383 | 389 | readonly threadId: ThreadId; | @@ -591,14 +597,21 @@ export function makeClaudeAgentSdkProtocolLogger(input: { | 591 | 597 | }; | 592 | 598 | } | 593 | 599 |
| | 600 | +const decodeForkSessionResult = Schema.decodeEffect( | | 601 | + Schema.fromJsonString(Schema.Struct({ sessionId: Schema.String })), | | 602 | +); | | 603 | + | 594 | 604 | export const claudeAgentSdkQueryRunnerLiveLayer: Layer.Layer< | 595 | 605 | ClaudeAgentSdkQueryRunner, | 596 | 606 | never, | 597 | | - Crypto.Crypto | ProviderEventLoggers.ProviderEventLoggers | | 607 | + | Crypto.Crypto | | 608 | + | ProviderEventLoggers.ProviderEventLoggers | | 609 | + | ChildProcessSpawner.ChildProcessSpawner | 598 | 610 | > = Layer.effect( | 599 | 611 | ClaudeAgentSdkQueryRunner, | 600 | 612 | Effect.gen(function* () { | 601 | 613 | const crypto = yield* Crypto.Crypto; | | 614 | + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; | 602 | 615 | const { native: nativeEventLogger } = yield* ProviderEventLoggers.ProviderEventLoggers; | 603 | 616 |
| 604 | 617 | return ClaudeAgentSdkQueryRunner.of({ | @@ -744,10 +757,42 @@ export const claudeAgentSdkQueryRunnerLiveLayer: Layer.Layer< | 744 | 757 | options: input.options, | 745 | 758 | }, | 746 | 759 | }); | 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 | + ); | 751 | 796 | yield* logProtocolEvent({ | 752 | 797 | direction: "incoming", | 753 | 798 | stage: "decoded", | @@ -7663,6 +7708,7 @@ export function makeClaudeAdapterV2( | 7663 | 7708 | ...(upToMessageId === undefined ? {} : { upToMessageId }), | 7664 | 7709 | }; | 7665 | 7710 | const forked = yield* queryRunner.forkSession({ | | 7711 | + environment: adapterOptions.environment, | 7666 | 7712 | sessionId: sourceNativeThreadId, | 7667 | 7713 | options: forkOptions, | 7668 | 7714 | threadId: forkInput.targetThreadId, | |
|