TypeScript
fix(server): forked Claude threads keep their compaction chain (#27577)
Julius Marmingeff73c4c2e663
diff --git a/apps/server/package.json b/apps/server/package.jsonindex 04541dabfb0..efc25e865e7 100644--- a/apps/server/package.json+++ b/apps/server/package.json@@ -34,7 +34,7 @@"generate:expo-schema": "node scripts/fetch-expo-schema.ts"},"dependencies": {- "@anthropic-ai/claude-agent-sdk": "^0.3.276",+ "@anthropic-ai/claude-agent-sdk": "^0.3.284","@cursor/sdk": "1.2.0","@effect/platform-node": "catalog:","@effect/platform-node-shared": "catalog:",diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.tsindex 68eaca85ea1..340de961471 100644--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts@@ -37,32 +37,34 @@ vi.mock("@anthropic-ai/claude-agent-sdk", { spy: true });const FORK_SESSION_ID = "11111111-1111-4111-8111-111111111111";-const writeClaudeSession = Effect.fnUntraced(function* (configDir: string) {+const sessionEntry = (uuid: string, parentUuid: string | null, message: unknown) => ({+ type: parentUuid === null ? "user" : "assistant",+ uuid,+ parentUuid,+ sessionId: FORK_SESSION_ID,+ cwd: "/repo",+ isSidechain: false,+ timestamp: "2026-10-01T00:00:00.000Z",+ message,+});++const writeClaudeSession = Effect.fnUntraced(function* (+ configDir: string,+ entries: ReadonlyArray<unknown> = [+ sessionEntry("aaaaaaaa-0000-4000-8000-000000000001", null, { role: "user", content: "hello" }),+ sessionEntry("aaaaaaaa-0000-4000-8000-000000000002", "aaaaaaaa-0000-4000-8000-000000000001", {+ role: "assistant",+ content: [{ type: "text", text: "hi" }],+ }),+ ],+) {const fileSystem = yield* FileSystem.FileSystem;const path = yield* Path.Path;const projectDir = path.join(configDir, "projects", "-repo");- const entry = (uuid: string, parentUuid: string | null, message: unknown) => ({- type: parentUuid === null ? "user" : "assistant",- uuid,- parentUuid,- sessionId: FORK_SESSION_ID,- cwd: "/repo",- isSidechain: false,- timestamp: "2026-10-01T00:00:00.000Z",- message,- });yield* fileSystem.makeDirectory(projectDir, { recursive: true });yield* fileSystem.writeFileString(path.join(projectDir, `${FORK_SESSION_ID}.jsonl`),- [- entry("aaaaaaaa-0000-4000-8000-000000000001", null, { role: "user", content: "hello" }),- entry("aaaaaaaa-0000-4000-8000-000000000002", "aaaaaaaa-0000-4000-8000-000000000001", {- role: "assistant",- content: [{ type: "text", text: "hi" }],- }),- ]- .map((line) => JSON.stringify(line))- .join("\n"),+ entries.map((line) => JSON.stringify(line)).join("\n"),);return projectDir;});@@ -112,6 +114,93 @@ it.effect("forks a session stored under the instance's own CLAUDE_CONFIG_DIR", (}).pipe(Effect.provide(forkRunnerLayer)),).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),);+it.effect("forked Claude sessions keep their compaction chain", () =>+ Effect.scoped(+ Effect.gen(function* () {+ const fileSystem = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const configDir = yield* fileSystem.makeTempDirectoryScoped({+ prefix: "t3-claude-fork-compact-",+ });+ const uuid = (n: number) => `bbbbbbbb-0000-4000-8000-00000000000${n}`;+ const turn = (id: number, parent: string | null, role: "user" | "assistant") => ({+ ...sessionEntry(uuid(id), parent, {+ role,+ content: role === "user" ? `question ${id}` : [{ type: "text", text: `answer ${id}` }],+ }),+ type: role,+ });+ const projectDir = yield* writeClaudeSession(configDir, [+ turn(1, null, "user"),+ turn(2, uuid(1), "assistant"),+ turn(3, uuid(2), "user"),+ turn(4, uuid(3), "assistant"),+ {+ ...sessionEntry(uuid(5), null, undefined),+ type: "system",+ subtype: "compact_boundary",+ content: "Conversation compacted",+ logicalParentUuid: uuid(4),+ compactMetadata: {+ trigger: "manual",+ preTokens: 19979,+ preservedSegment: { headUuid: uuid(3), anchorUuid: uuid(6), tailUuid: uuid(4) },+ preservedMessages: {+ anchorUuid: uuid(6),+ uuids: [uuid(3), uuid(4)],+ allUuids: [uuid(3), uuid(4)],+ },+ postTokens: 3589,+ },+ },+ {+ ...turn(6, uuid(5), "user"),+ isCompactSummary: true,+ isVisibleInTranscriptOnly: true,+ },+ turn(7, uuid(4), "user"),+ turn(8, uuid(7), "assistant"),+ ]);++ const runner = yield* ClaudeAgentSdkQueryRunner;+ const forked = yield* runner.forkSession({+ sessionId: FORK_SESSION_ID,+ options: { dir: "/repo" },+ environment: { ...process.env, CLAUDE_CONFIG_DIR: configDir },+ threadId: ThreadId.make("thread-fork-compaction"),+ providerSessionId: ProviderSessionId.make("session-fork-compaction"),+ });++ const entries: ReadonlyArray<{+ readonly uuid?: string;+ readonly subtype?: string;+ readonly compactMetadata?: {+ readonly preservedSegment: Record<"headUuid" | "anchorUuid" | "tailUuid", string>;+ readonly preservedMessages: {+ readonly anchorUuid: string;+ readonly uuids: string[];+ readonly allUuids: string[];+ };+ };+ }> = (yield* fileSystem.readFileString(path.join(projectDir, `${forked.sessionId}.jsonl`)))+ .trim()+ .split("\n")+ .map((line) => JSON.parse(line));+ const forkedUuids = new Set(entries.map((entry) => entry.uuid));+ const boundary = entries.find((entry) => entry.subtype === "compact_boundary");+ assert.isDefined(boundary?.compactMetadata);+ const { preservedSegment, preservedMessages } = boundary!.compactMetadata!;+ const danglingRefs = [+ ...Object.values(preservedSegment),+ preservedMessages.anchorUuid,+ ...preservedMessages.uuids,+ ...preservedMessages.allUuids,+ ].filter((ref) => !forkedUuids.has(ref));+ assert.deepEqual(danglingRefs, [], "compaction refs missing from the forked transcript");+ }).pipe(Effect.provide(forkRunnerLayer)),+ ).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),+);+const realQueryRunner = layerQueryRunner.pipe(Layer.provide(PortScanner.layerTest),Layer.provide(diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 9be1036ff4a..84a19021c78 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -834,8 +834,8 @@ importers:apps/server:dependencies:'@anthropic-ai/claude-agent-sdk':- specifier: ^0.3.276- version: 0.3.276(@anthropic-ai/[email protected]([email protected]))(@modelcontextprotocol/[email protected]([email protected])([email protected]))([email protected])+ specifier: ^0.3.284+ version: 0.3.284(@anthropic-ai/[email protected]([email protected]))(@modelcontextprotocol/[email protected]([email protected])([email protected]))([email protected])'@cursor/sdk':specifier: 1.2.0version: 1.2.0([email protected])@@ -2678,8 +2678,8 @@ packages:vitest:optional: true- '@anthropic-ai/[email protected]':- resolution: {integrity: sha512-Dic43v4uuGLhibPAArWy3RSfq3zHdn+4Oh2AI2/18a5tlbNKkvChEpazyp8c+grUJvzxkzt0jETCa1wrADbkyw==}+ '@anthropic-ai/[email protected]':+ resolution: {integrity: sha512-NSoJwEq6nFSf8dtaacYx37QdGgqApI3eHFUlxclMLYi8irb6ZJwEaUnPnLUCyAyg9W/tMkgZC0GXWLRCc01I0w==}engines: {node: '>=18.0.0'}peerDependencies:'@anthropic-ai/sdk': '>=0.93.0'@@ -12663,7 +12663,7 @@ snapshots:- bufferutil- utf-8-validate- '@anthropic-ai/[email protected](@anthropic-ai/[email protected]([email protected]))(@modelcontextprotocol/[email protected]([email protected])([email protected]))([email protected])':+ '@anthropic-ai/[email protected](@anthropic-ai/[email protected]([email protected]))(@modelcontextprotocol/[email protected]([email protected])([email protected]))([email protected])':dependencies:'@anthropic-ai/sdk': 0.93.0([email protected])'@modelcontextprotocol/sdk': 1.29.0([email protected])([email protected])diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yamlindex fc5d6b110c5..df77b847c34 100644--- a/pnpm-workspace.yaml+++ b/pnpm-workspace.yaml@@ -175,7 +175,6 @@ minimumReleaseAgeExclude:- - "@anthropic-ai/[email protected]"- "@shadcn/[email protected]"