@devries-cameron20 / t3code-1 / commits / 0a8aaa3ccb
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): retained forks keep inherited shell item counts after source deletion (#36657)

Julius Marminge committed Nov 14, 2026 at 02:46 UTC 0a8aaa3ccb944dadb70054ae4ef2e29897b0db03
2 files changed +31 -4
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+20
@@ -10562,6 +10562,26 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
10562 ["local", targetThreadId, "command_execution"],
10563 ],
10564 );
10565 +
10566 + const sourceThread = (yield* projectionStore.getThreadProjection(sourceThreadId)).thread;
10567 + yield* projectionStore.apply({
10568 + id: EventId.make("event:projection-fork-source-rollback:source-deleted"),
10569 + type: "thread.deleted",
10570 + threadId: sourceThreadId,
10571 + occurredAt: now,
10572 + payload: { ...sourceThread, deletedAt: now },
10573 + });
10574 + const afterSourceDeleted = yield* projectionStore.getShellSnapshot();
10575 + assert.isUndefined(afterSourceDeleted.threads.find((thread) => thread.id === sourceThreadId));
10576 + for (const forkId of [targetThreadId, nestedThreadId]) {
10577 + const expected = (yield* projectionStore.getThreadProjection(forkId)).visibleTurnItems
10578 + .length;
10579 + assert.equal((yield* projectionStore.getThreadShell(forkId))?.visibleItemCount, expected);
10580 + assert.equal(
10581 + afterSourceDeleted.threads.find((thread) => thread.id === forkId)?.visibleItemCount,
10582 + expected,
10583 + );
10584 + }
10585 }),
10586 );
10587
apps/server/src/orchestration-v2/ProjectionStore.ts
+11 -4
@@ -7660,7 +7660,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7660 worktreePath,
7661 threadIds,
7662 listedOnly = false,
7663 - }: ShellSnapshotOptions & { readonly listedOnly?: boolean } = {},
7663 + includeDeleted = false,
7664 + }: ShellSnapshotOptions & {
7665 + readonly listedOnly?: boolean;
7666 + // A retained fork still shows the history of a deleted source run.
7667 + readonly includeDeleted?: boolean;
7668 + } = {},
7669 ) =>
7670 sql<ShellThreadRow>`
7671 SELECT
@@ -7839,7 +7844,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7844 candidate.ordinal DESC, candidate.run_id DESC
7845 LIMIT 1
7846 ) AND blocked.status = 'failed'
7842 - WHERE t.deleted_at IS NULL${
7847 + WHERE ${includeDeleted ? sql`1 = 1` : sql`t.deleted_at IS NULL`}${
7848 listedOnly
7849 ? sql` AND json_extract(t.payload_json, '$.lineage.relationshipToParent') IS NOT 'subagent'
7850 AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1`
@@ -8496,7 +8501,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
8501 while (pendingSourceIds.length > 0) {
8502 const sourceId = pendingSourceIds.pop();
8503 if (sourceId === undefined || rowsByThreadId.has(sourceId)) continue;
8499 - const source = (yield* selectShellThreadRows(sourceId))[0];
8504 + const source = (yield* selectShellThreadRows(sourceId, { includeDeleted: true }))[0];
8505 if (source === undefined) continue;
8506 rowsByThreadId.set(sourceId, source);
8507 if (source.forked_from_run_source_thread_id !== null) {
@@ -8976,7 +8981,9 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
8981 if (nextId === undefined || rowsByThreadId.has(nextId)) {
8982 continue;
8983 }
8979 - const rows = yield* selectShellThreadRows(nextId);
8984 + const rows = yield* selectShellThreadRows(nextId, {
8985 + includeDeleted: nextId !== threadId,
8986 + });
8987 const row = rows[0];
8988 if (row === undefined) {
8989 continue;