@devries-cameron20 / t3code-1 / commits / 733a20008c
Fork 0

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

fix(server): prune checkpoint refs owned by deleted threads (#24140)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Tin Zeljar committed Nov 14, 2026 at 23:47 UTC 733a20008c7e7ae627e8e012b4e410bbc96028c9
4 files changed +484 -35
apps/server/src/orchestration-v2/EventSink.test.ts new
+109
@@ -0,0 +1,109 @@
1 +import { assert, it } from "@effect/vitest";
2 +import {
3 + CommandId,
4 + EventId,
5 + ProjectId,
6 + ProviderInstanceId,
7 + RunId,
8 + ThreadId,
9 + type OrchestrationV2AppThread,
10 +} from "@t3tools/contracts";
11 +import * as DateTime from "effect/DateTime";
12 +import * as Effect from "effect/Effect";
13 +import * as Layer from "effect/Layer";
14 +
15 +import * as Sqlite from "../persistence/Sqlite.ts";
16 +import * as EventSink from "./EventSink.ts";
17 +import * as EventStore from "./EventStore.ts";
18 +import * as ProjectionStore from "./ProjectionStore.ts";
19 +
20 +const layer = EventSink.layer.pipe(
21 + Layer.provideMerge(Layer.merge(EventStore.layer, ProjectionStore.layer)),
22 + Layer.provide(Sqlite.layerMemory),
23 +);
24 +
25 +it.effect("rejects a planned fork whose source was deleted before it committed", () =>
26 + Effect.gen(function* () {
27 + const sink = yield* EventSink.EventSinkV2;
28 + const projections = yield* ProjectionStore.ProjectionStoreV2;
29 + const now = yield* DateTime.now;
30 + const sourceId = ThreadId.make("fork-source");
31 + const targetId = ThreadId.make("fork-target");
32 + const providerInstanceId = ProviderInstanceId.make("codex");
33 + const source: OrchestrationV2AppThread = {
34 + id: sourceId,
35 + projectId: ProjectId.make("project"),
36 + title: "Source",
37 + createdBy: "user",
38 + creationSource: "web",
39 + providerInstanceId,
40 + modelSelection: { instanceId: providerInstanceId, model: "gpt-5" },
41 + runtimeMode: "full-access",
42 + interactionMode: "default",
43 + branch: null,
44 + worktreePath: null,
45 + activeProviderThreadId: null,
46 + lineage: { rootThreadId: sourceId, parentThreadId: null, relationshipToParent: null },
47 + forkedFrom: null,
48 + createdAt: now,
49 + updatedAt: now,
50 + archivedAt: null,
51 + deletedAt: null,
52 + settledOverride: null,
53 + settledAt: null,
54 + snoozedUntil: null,
55 + snoozedAt: null,
56 + lastVisitedAt: null,
57 + };
58 + yield* sink.write({
59 + events: [
60 + {
61 + id: EventId.make("source-created"),
62 + type: "thread.created",
63 + threadId: sourceId,
64 + occurredAt: now,
65 + payload: source,
66 + },
67 + ],
68 + });
69 + const plannedFork = {
70 + commandId: CommandId.make("fork"),
71 + commandType: "thread.fork",
72 + threadId: targetId,
73 + acceptedAt: now,
74 + effects: [],
75 + events: [
76 + {
77 + id: EventId.make("fork-created"),
78 + type: "thread.created" as const,
79 + threadId: targetId,
80 + occurredAt: now,
81 + payload: {
82 + ...source,
83 + id: targetId,
84 + forkedFrom: { type: "run" as const, threadId: sourceId, runId: RunId.make("run") },
85 + },
86 + },
87 + ],
88 + };
89 + yield* sink.write({
90 + events: [
91 + {
92 + id: EventId.make("source-deleted"),
93 + type: "thread.deleted",
94 + threadId: sourceId,
95 + occurredAt: now,
96 + payload: { ...source, deletedAt: now },
97 + },
98 + ],
99 + });
100 + const rejected = yield* sink.commitCommand(plannedFork);
101 + assert.equal(rejected.receipt.status, "rejected");
102 + assert.isFalse(rejected.committed);
103 + const replayed = yield* sink.commitCommand(plannedFork);
104 + assert.deepEqual(replayed.receipt, rejected.receipt);
105 + assert.isFalse(replayed.committed);
106 + const missing = yield* projections.getThread(targetId).pipe(Effect.flip);
107 + assert.instanceOf(missing, ProjectionStore.ProjectionStoreThreadNotFoundError);
108 + }).pipe(Effect.provide(layer)),
109 +);
apps/server/src/orchestration-v2/EventSink.ts
+32
@@ -779,6 +779,38 @@ const layerBase: Layer.Layer<
779 return { ...existing, committed: false as const, cancelledEffectIds: [] };
780 }
781
782 + // Planning does not lock the source. Once deletion commits, maintenance
783 + // can remove its checkpoints, so a new fork must not revive that history.
784 + if (input.commandType === "thread.fork") {
785 + for (const event of input.events) {
786 + if (event.type !== "thread.created" || event.payload.forkedFrom?.type !== "run")
787 + continue;
788 + const sourceId = event.payload.forkedFrom.threadId;
789 + const source = yield* sql`
790 + SELECT 1 FROM orchestration_v2_projection_threads
791 + WHERE thread_id = ${sourceId} AND deleted_at IS NULL
792 + `;
793 + if (source.length === 0) {
794 + const receipt: CommandReceiptStore.CommandReceiptV2 = {
795 + commandId: input.commandId,
796 + threadId: input.threadId,
797 + commandType: input.commandType,
798 + acceptedAt: input.acceptedAt,
799 + resultSequence: yield* eventStore.latestSequence({ threadId: input.threadId }),
800 + status: "rejected",
801 + error: "Cannot fork a missing or deleted thread.",
802 + };
803 + yield* commandReceipts.upsert(receipt);
804 + return {
805 + receipt,
806 + storedEvents: [],
807 + committed: false as const,
808 + cancelledEffectIds: [],
809 + };
810 + }
811 + }
812 + }
813 +
814 const runId = input.updateDelegatedCompletionOnlyRunId;
815 const normalized = yield* normalizeEvents(
816 runId === undefined
apps/server/src/storageCleanup.test.ts
+227 -35
@@ -2,6 +2,8 @@ import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 import * as Effect from "effect/Effect";
4 import * as Context from "effect/Context";
5 +import type * as Digest from "effect/crypto/Digest";
6 +import * as Base64Url from "effect/encoding/Base64Url";
7 import * as Deferred from "effect/Deferred";
8 import * as Fiber from "effect/Fiber";
9 import * as FileSystem from "effect/FileSystem";
@@ -14,6 +16,8 @@ import * as Stream from "effect/Stream";
16 import { SCAN_CACHE_FILE_NAME } from "./usage/usageScanCache.ts";
17 import { TestClock } from "effect/testing";
18 import * as SqlClient from "effect/sql/SqlClient";
19 +import * as Sqlite from "./persistence/Sqlite.ts";
20 +import { checkpointRefForScopeOrdinal } from "./orchestration-v2/CheckpointService.ts";
21 import {
22 DEFAULT_SERVER_SETTINGS,
23 type WorktreeKeepWhen,
@@ -39,6 +43,7 @@ import { ServerActivation } from "./serverActivation.ts";
43 import { withWorkspaceLease } from "./workspace/workspaceLease.ts";
44 import { describe, expect, it } from "@effect/vitest";
45 import {
46 + CheckpointScopeId,
47 EventId,
48 ProjectId,
49 ProviderDriverKind,
@@ -328,6 +333,7 @@ const cleanupFixture = (
333 worktreeAfterDays: 8,
334 worktreeOnDelete: true,
335 },
336 + sqlOverride?: SqlClient.SqlClient,
337 ) =>
338 Effect.gen(function* () {
339 const fs = yield* FileSystem.FileSystem;
@@ -465,41 +471,49 @@ const cleanupFixture = (
471 Effect.provideService(Orchestrator.OrchestratorV2, {
472 streamDomainEvents: Stream.suspend(() => events),
473 } as unknown as Orchestrator.OrchestratorV2["Service"]),
468 - Effect.provideService(SqlClient.SqlClient, ((strings: TemplateStringsArray) =>
469 - Effect.sync(() =>
470 - openSessionCwd !== null && strings.join("").includes("projection_provider_sessions")
471 - ? [
472 - {
473 - payload_json: JSON.stringify({
474 - id: "session-1",
475 - driver: "codex",
476 - providerInstanceId: "codex",
477 - status: "ready",
478 - cwd: openSessionCwd,
479 - model: null,
480 - capabilities: CodexProviderCapabilitiesV2,
481 - createdAt: "2026-06-01T12:00:00.000Z",
482 - updatedAt: "2026-06-01T12:00:00.000Z",
483 - lastError: null,
484 - }),
485 - },
486 - ]
487 - : outboxStatus !== null && strings.join("").includes("orchestration_v2_effect_outbox")
488 - ? [{ status: outboxStatus }]
489 - : strings.join("").includes("deleted_at IS NOT NULL")
490 - ? deletedThreads.map((thread) => ({
491 - payload_json: JSON.stringify(thread),
492 - workspaceRoot: repo,
493 - }))
494 - : strings.join("").includes('AS "worktreePath"')
474 + Effect.provideService(
475 + SqlClient.SqlClient,
476 + sqlOverride ??
477 + (((strings: TemplateStringsArray) =>
478 + Effect.sync(() =>
479 + strings.join("").includes("retained_checkpoint_threads")
480 + ? []
481 + : openSessionCwd !== null &&
482 + strings.join("").includes("projection_provider_sessions")
483 ? [
496 - ...threads.flatMap((thread) => thread.worktreePath ?? []),
497 - ...storedWorktreePaths,
498 - ].map((worktreePath) => ({ worktreePath }))
499 - : strings.join("").includes("scheduled_tasks")
500 - ? scheduledThreadIds.map((threadId) => ({ threadId }))
501 - : [],
502 - )) as unknown as SqlClient.SqlClient),
484 + {
485 + payload_json: JSON.stringify({
486 + id: "session-1",
487 + driver: "codex",
488 + providerInstanceId: "codex",
489 + status: "ready",
490 + cwd: openSessionCwd,
491 + model: null,
492 + capabilities: CodexProviderCapabilitiesV2,
493 + createdAt: "2026-06-01T12:00:00.000Z",
494 + updatedAt: "2026-06-01T12:00:00.000Z",
495 + lastError: null,
496 + }),
497 + },
498 + ]
499 + : outboxStatus !== null &&
500 + strings.join("").includes("orchestration_v2_effect_outbox")
501 + ? [{ status: outboxStatus }]
502 + : strings.join("").includes("deleted_at IS NOT NULL")
503 + ? deletedThreads.map((thread) => ({
504 + payload_json: JSON.stringify(thread),
505 + workspaceRoot: repo,
506 + }))
507 + : strings.join("").includes('AS "worktreePath"')
508 + ? [
509 + ...threads.flatMap((thread) => thread.worktreePath ?? []),
510 + ...storedWorktreePaths,
511 + ].map((worktreePath) => ({ worktreePath }))
512 + : strings.join("").includes("scheduled_tasks")
513 + ? scheduledThreadIds.map((threadId) => ({ threadId }))
514 + : [],
515 + )) as unknown as SqlClient.SqlClient),
516 + ),
517 Effect.provideService(GitManager.GitManager, {
518 invalidateStatus: () => Effect.void,
519 } as unknown as GitManager.GitManager["Service"]),
@@ -640,6 +654,7 @@ const runCleanupTest = <A, E>(
654 A,
655 E,
656 | FileSystem.FileSystem
657 + | Digest.Digest
658 | ServerConfig.ServerConfig
659 | GitVcsDriver.GitVcsDriver
660 | Path.Path
@@ -647,6 +662,182 @@ const runCleanupTest = <A, E>(
662 >,
663 ) => effect.pipe(Effect.provide(cleanupTestLayer));
664
665 +const legacyCheckpointRef = (threadId: string, turn: number) =>
666 + `refs/t3/checkpoints/${Base64Url.encode(threadId)}/turn/${turn}`;
667 +
668 +const checkpointCleanupFixture = Effect.fn("test.checkpointCleanupFixture")(function* () {
669 + const sql = yield* SqlClient.SqlClient;
670 + const git = yield* GitVcsDriver.GitVcsDriver;
671 + const fixture = yield* cleanupFixture(DEFAULT_SERVER_SETTINGS.storageCleanup, sql);
672 + const { repo, worktree, command } = fixture;
673 + const timestamp = DateTime.formatIso(at(0));
674 + yield* sql`INSERT INTO projection_projects ${sql.insert({
675 + project_id: "project-1",
676 + title: "Checkpoint cleanup",
677 + workspace_root: repo,
678 + scripts_json: "[]",
679 + created_at: timestamp,
680 + updated_at: timestamp,
681 + deleted_at: timestamp,
682 + })}`;
683 + const addThread = Effect.fn("test.addCheckpointThread")(function* (
684 + id: string,
685 + options: { deleted?: boolean; archived?: boolean; source?: string; legacyOnly?: boolean } = {},
686 + ) {
687 + const scopeId = CheckpointScopeId.make(`scope:${id}`);
688 + yield* sql`INSERT INTO orchestration_v2_projection_threads ${sql.insert({
689 + thread_id: id,
690 + project_id: "project-1",
691 + title: id,
692 + default_provider: "codex",
693 + runtime_mode: "full-access",
694 + interaction_mode: "default",
695 + created_at: timestamp,
696 + updated_at: timestamp,
697 + archived_at: options.archived ? timestamp : null,
698 + deleted_at: options.deleted ? timestamp : null,
699 + payload_json: JSON.stringify({
700 + worktreePath: worktree,
701 + forkedFrom: options.source ? { type: "run", threadId: options.source } : null,
702 + }),
703 + })}`;
704 + if (!options.legacyOnly)
705 + yield* sql`INSERT INTO orchestration_v2_projection_checkpoint_scopes ${sql.insert({
706 + scope_id: scopeId,
707 + thread_id: id,
708 + node_id: `node:${id}`,
709 + kind: "root_run",
710 + ordinal_within_parent: 0,
711 + advances_app_run_count: 1,
712 + created_at: timestamp,
713 + payload_json: JSON.stringify({ cwd: worktree }),
714 + })}`;
715 + const refs: string[] = options.legacyOnly
716 + ? [legacyCheckpointRef(id, 0)]
717 + : [
718 + yield* checkpointRefForScopeOrdinal({ scopeId, ordinalWithinScope: 0 }),
719 + yield* checkpointRefForScopeOrdinal({ scopeId, ordinalWithinScope: 1 }),
720 + legacyCheckpointRef(id, 0),
721 + ];
722 + for (const ref of refs) yield* command(repo, ["update-ref", ref, "HEAD"]);
723 + return refs;
724 + });
725 + const listRefs = () =>
726 + git
727 + .execute({
728 + operation: "cleanup-test.list-checkpoints",
729 + cwd: repo,
730 + args: ["for-each-ref", "--format=%(refname)", "refs/t3/"],
731 + maxOutputBytes: 16 * 1024 * 1024,
732 + })
733 + .pipe(Effect.map(({ stdout }) => stdout.trim().split("\n").filter(Boolean).sort()));
734 + return { ...fixture, sql, addThread, listRefs };
735 +});
736 +
737 +describe("checkpoint ref cleanup", () => {
738 + it.live("prunes refs owned by deleted threads after their worktree is removed", () =>
739 + runCleanupTest(
740 + Effect.gen(function* () {
741 + const { service, repo, worktree, command, addThread, listRefs } =
742 + yield* checkpointCleanupFixture();
743 + yield* addThread("deleted", { deleted: true });
744 + yield* addThread("imported-deleted", { deleted: true, legacyOnly: true });
745 + const retained = [
746 + ...(yield* addThread("active")),
747 + ...(yield* addThread("archived", { archived: true })),
748 + legacyCheckpointRef("unknown-owner", 0),
749 + ];
750 + yield* command(repo, ["update-ref", retained.at(-1)!, "HEAD"]);
751 + yield* command(repo, ["pack-refs", "--all"]);
752 + yield* command(repo, ["worktree", "remove", worktree]);
753 + yield* service.runNow;
754 + expect(yield* listRefs()).toEqual(retained.sort());
755 + }).pipe(Effect.provide(Sqlite.layerMemory)),
756 + ),
757 + );
758 +
759 + it.live("keeps deleted fork ancestry while a live or archived descendant needs it", () =>
760 + runCleanupTest(
761 + Effect.gen(function* () {
762 + const { service, sql, addThread, listRefs } = yield* checkpointCleanupFixture();
763 + const root = yield* addThread("root", { deleted: true });
764 + const intermediate = yield* addThread("intermediate", { deleted: true, source: "root" });
765 + const child = yield* addThread("child", { archived: true, source: "intermediate" });
766 + yield* service.runNow;
767 + expect(yield* listRefs()).toEqual([...root, ...intermediate, ...child].sort());
768 + yield* sql`UPDATE orchestration_v2_projection_threads SET deleted_at = 'deleted' WHERE thread_id = 'child'`;
769 + yield* service.runNow;
770 + expect(yield* listRefs()).toEqual([]);
771 + }).pipe(Effect.provide(Sqlite.layerMemory)),
772 + ),
773 + );
774 +
775 + it.live("waits for a deleted thread's pending checkpoint effects", () =>
776 + runCleanupTest(
777 + Effect.gen(function* () {
778 + const { service, sql, addThread, listRefs } = yield* checkpointCleanupFixture();
779 + const refs = yield* addThread("capturing", { deleted: true });
780 + yield* sql`INSERT INTO orchestration_v2_effect_outbox ${sql.insert({
781 + effect_id: "capture",
782 + command_id: "capture",
783 + thread_id: "capturing",
784 + effect_type: "checkpoint.capture",
785 + payload_json: "{}",
786 + status: "running",
787 + available_at: "2026-01-01T00:00:00Z",
788 + created_at: "2026-01-01T00:00:00Z",
789 + updated_at: "2026-01-01T00:00:00Z",
790 + })}`;
791 + yield* service.runNow;
792 + expect(yield* listRefs()).toEqual(refs.toSorted());
793 + yield* sql`UPDATE orchestration_v2_effect_outbox SET status = 'succeeded'`;
794 + yield* service.runNow;
795 + expect(yield* listRefs()).toEqual([]);
796 + }).pipe(Effect.provide(Sqlite.layerMemory)),
797 + ),
798 + );
799 +
800 + it.live("deletes a symbolic checkpoint ref without its target or neighbouring refs", () =>
801 + runCleanupTest(
802 + Effect.gen(function* () {
803 + const { service, repo, command, addThread, listRefs } = yield* checkpointCleanupFixture();
804 + const refs = yield* addThread("deleted", { deleted: true });
805 + yield* command(repo, ["symbolic-ref", refs[0]!, "refs/heads/main"]);
806 + const retained = [
807 + "refs/t3/unrelated/0",
808 + `${refs[1]}-other`,
809 + refs[1]!.replace(/1$/, "2/nested"),
810 + ];
811 + for (const ref of retained) yield* command(repo, ["update-ref", ref, "HEAD"]);
812 + yield* service.runNow;
813 + expect(yield* listRefs()).toEqual(retained.sort());
814 + yield* command(repo, ["show-ref", "--verify", "refs/heads/main"]);
815 + }).pipe(Effect.provide(Sqlite.layerMemory)),
816 + ),
817 + );
818 +
819 + it.live("cleans more checkpoint refs than default git output holds", () =>
820 + runCleanupTest(
821 + Effect.gen(function* () {
822 + const { service, repo, addThread, listRefs } = yield* checkpointCleanupFixture();
823 + const git = yield* GitVcsDriver.GitVcsDriver;
824 + const prefix = (yield* addThread("many-checkpoints", { deleted: true }))[0]!.slice(0, -1);
825 + yield* git.execute({
826 + operation: "cleanup-test.seed-checkpoints",
827 + cwd: repo,
828 + args: ["update-ref", "--stdin"],
829 + stdin: Array.from(
830 + { length: 12_283 },
831 + (_, ordinal) => `update ${prefix}${ordinal} HEAD\n`,
832 + ).join(""),
833 + });
834 + yield* service.runNow;
835 + expect(yield* listRefs()).toEqual([]);
836 + }).pipe(Effect.provide(Sqlite.layerMemory)),
837 + ),
838 + );
839 +});
840 +
841 it.live.each(["repository", "uppercase", "bare"] as const)(
842 "keeps a worktree holding an ignored nested %s",
843 (kind) =>
@@ -2378,7 +2569,8 @@ describe("storage cleanup reports and local file policies", () => {
2569 occurredAt: at(0),
2570 payload: { ...shell({ deletedAt: at(0) }), lastVisitedAt: null },
2571 });
2381 - expect(fixture.snapshotReads()).toBe(startupReads + 3 * Number(worktreeOnDelete));
2572 + // Deletion always sweeps so the deleted thread's checkpoint refs are pruned.
2573 + expect(fixture.snapshotReads()).toBe(startupReads + 2 * Number(worktreeOnDelete) + 1);
2574 expect(fixture.sessionLiveChecks()).toBe(worktreeOnDelete ? 6 : 0);
2575 }),
2576 ),
apps/server/src/storageCleanup.ts
+116
@@ -1,10 +1,12 @@
1 // @effect-diagnostics nodeBuiltinImport:off
2 import * as NodeFSP from "node:fs/promises";
3 import {
4 + CheckpointScopeId,
5 GitCommandError,
6 OrchestrationV2AppThreadJson,
7 OrchestrationV2ProviderSessionJson,
8 } from "@t3tools/contracts";
9 +import * as Base64Url from "effect/encoding/Base64Url";
10 import * as Schema from "effect/Schema";
11 import * as SqlClient from "effect/sql/SqlClient";
12 import type {
@@ -46,6 +48,7 @@ import { scratchFolderRoot } from "./project/ManagedProjectFolders.ts";
48 import * as Orchestrator from "./orchestration-v2/Orchestrator.ts";
49 import * as ProjectionStore from "./orchestration-v2/ProjectionStore.ts";
50 import * as ProviderSessionManager from "./orchestration-v2/ProviderSessionManager.ts";
51 +import { checkpointRefForScopeOrdinal } from "./orchestration-v2/CheckpointService.ts";
52 import { threadHasQueuedTurnStart } from "./orchestration-v2/ThreadSettlementService.ts";
53 import { MAX_OVERDUE_ARM_MS } from "./orchestration-v2/UsageLimitRecoveryWorker.ts";
54 import * as WorktreeBranchSync from "./orchestration-v2/WorktreeBranchSync.ts";
@@ -1644,6 +1647,115 @@ const make = Effect.gen(function* () {
1647 });
1648
1649 const reportRef = yield* SubscriptionRef.make<StorageCleanupReport | null>(null);
1650 + const cleanCheckpointRefs = Effect.fn("StorageCleanup.cleanCheckpointRefs")(function* () {
1651 + // Forks display their source history even after its thread is deleted. Keep
1652 + // that ancestry, including deleted intermediate forks and archived children.
1653 + const rows = yield* sql<{
1654 + threadId: string;
1655 + worktreePath: string | null;
1656 + workspaceRoot: string | null;
1657 + scopeId: string | null;
1658 + cwd: string | null;
1659 + }>`
1660 + WITH RECURSIVE retained_checkpoint_threads(thread_id) AS (
1661 + SELECT thread_id FROM orchestration_v2_projection_threads WHERE deleted_at IS NULL
1662 + UNION
1663 + SELECT json_extract(t.payload_json, '$.forkedFrom.threadId')
1664 + FROM orchestration_v2_projection_threads t
1665 + JOIN retained_checkpoint_threads retained ON retained.thread_id = t.thread_id
1666 + WHERE json_extract(t.payload_json, '$.forkedFrom.type') = 'run'
1667 + )
1668 + SELECT t.thread_id AS "threadId",
1669 + json_extract(t.payload_json, '$.worktreePath') AS "worktreePath",
1670 + p.workspace_root AS "workspaceRoot", s.scope_id AS "scopeId",
1671 + json_extract(s.payload_json, '$.cwd') AS cwd
1672 + FROM orchestration_v2_projection_threads t
1673 + LEFT JOIN projection_projects p ON p.project_id = t.project_id
1674 + LEFT JOIN orchestration_v2_projection_checkpoint_scopes s ON s.thread_id = t.thread_id
1675 + WHERE t.deleted_at IS NOT NULL
1676 + AND NOT EXISTS (SELECT 1 FROM retained_checkpoint_threads WHERE thread_id = t.thread_id)
1677 + AND NOT EXISTS (
1678 + SELECT 1 FROM orchestration_v2_effect_outbox e
1679 + WHERE e.thread_id = t.thread_id AND e.status IN ('pending', 'running')
1680 + )
1681 + `.pipe(NodeSqliteClient.readOnly);
1682 + const targets = new Map<string, Set<string>>();
1683 + for (const row of rows) {
1684 + // Derive ownership from tombstones, never from absence in this database:
1685 + // another T3 home can own refs in the same Git repository.
1686 + const prefixes = [`refs/t3/checkpoints/${Base64Url.encode(row.threadId)}/turn/`];
1687 + if (row.scopeId !== null) {
1688 + prefixes.push(
1689 + (yield* checkpointRefForScopeOrdinal({
1690 + scopeId: CheckpointScopeId.make(row.scopeId),
1691 + ordinalWithinScope: 0,
1692 + })).slice(0, -1),
1693 + );
1694 + }
1695 + // A worktree may already be gone; its refs live in the shared repository.
1696 + for (const cwd of [row.cwd, row.worktreePath, row.workspaceRoot]) {
1697 + if (cwd === null) continue;
1698 + const key = path.resolve(cwd);
1699 + const owned = targets.get(key) ?? new Set<string>();
1700 + for (const prefix of prefixes) owned.add(prefix);
1701 + targets.set(key, owned);
1702 + }
1703 + }
1704 + const repositories = new Map<string, { cwd: string; prefixes: Set<string> }>();
1705 + for (const [cwd, prefixes] of targets) {
1706 + yield* Effect.gen(function* () {
1707 + if (!(yield* fs.exists(cwd))) return;
1708 + const result = yield* git.execute({
1709 + operation: "StorageCleanup.checkpointRepository",
1710 + cwd,
1711 + args: ["rev-parse", "--path-format=absolute", "--git-common-dir"],
1712 + allowNonZeroExit: true,
1713 + });
1714 + if (result.exitCode !== 0) return;
1715 + const commonDir = yield* fs.realPath(result.stdout.trim());
1716 + const repository = repositories.get(commonDir) ?? { cwd, prefixes: new Set<string>() };
1717 + for (const prefix of prefixes) repository.prefixes.add(prefix);
1718 + repositories.set(commonDir, repository);
1719 + }).pipe(
1720 + Effect.catch((cause) =>
1721 + Effect.logWarning("checkpoint repository inspection failed", { cause }),
1722 + ),
1723 + );
1724 + }
1725 + for (const { cwd, prefixes } of repositories.values()) {
1726 + yield* Effect.gen(function* () {
1727 + const result = yield* git.execute({
1728 + operation: "StorageCleanup.listCheckpointRefs",
1729 + cwd,
1730 + args: [
1731 + "for-each-ref",
1732 + "--format=%(refname)",
1733 + "refs/t3/checkpoints/",
1734 + "refs/t3/orchestration-v2/checkpoints/",
1735 + ],
1736 + maxOutputBytes: 16 * 1024 * 1024,
1737 + });
1738 + const refs = result.stdout.split("\n").filter((ref) => {
1739 + const ordinalStart = ref.lastIndexOf("/") + 1;
1740 + return (
1741 + prefixes.has(ref.slice(0, ordinalStart)) &&
1742 + /^(0|[1-9]\d*)$/.test(ref.slice(ordinalStart))
1743 + );
1744 + });
1745 + if (refs.length === 0) return;
1746 + yield* git.execute({
1747 + operation: "StorageCleanup.deleteCheckpointRefs",
1748 + cwd,
1749 + // Remove a symbolic checkpoint itself, never its branch/tag target.
1750 + args: ["update-ref", "--no-deref", "-z", "--stdin"],
1751 + stdin: refs.map((ref) => `delete ${ref}\0\0`).join(""),
1752 + });
1753 + yield* Effect.logInfo("storage cleanup removed checkpoint refs", { refCount: refs.length });
1754 + }).pipe(
1755 + Effect.catch((cause) => Effect.logWarning("checkpoint ref cleanup failed", { cause })),
1756 + );
1757 + }
1758 + });
1759 // Only deleted threads' worktrees wait on session stops: any stop while one waits, or a stop inside one kept for another reason.
1760 // A deletion stays pending until a sweep that started after it finishes.
1761 let deletedWorktreeWaits = true;
@@ -1658,6 +1770,9 @@ const make = Effect.gen(function* () {
1770 const serverSettings = yield* settingsService.getSettings;
1771 const settings = serverSettings.storageCleanup;
1772 const entries: StorageCleanupReportEntry[] = [];
1773 + yield* cleanCheckpointRefs().pipe(
1774 + Effect.catch((cause) => Effect.logWarning("checkpoint ref cleanup failed", { cause })),
1775 + );
1776 const deletedWorktrees = yield* cleanWorktrees(serverSettings, now, entries).pipe(
1777 Effect.catch((error) =>
1778 Effect.gen(function* () {
@@ -1836,6 +1951,7 @@ const make = Effect.gen(function* () {
1951 )
1952 return Effect.void;
1953 }
1954 + if (event.type === "thread.deleted") return requestSweep;
1955 if (!anyWorktreePolicy(lastSettings, (rules) => rules.worktreeOnDelete)) return Effect.void;
1956 return event.type === "provider-session.updated" && event.payload.status === "error"
1957 ? providerSessions