From d42c02bf0f8c0e77c11a68135eee6926251c0c15 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Tue, 13 Oct 2026 16:02:09 -0700 Subject: [PATCH] fix(workspaces): retain settled chats until archival Clover wants workspace directories to survive settling a chat and remain until their associated PR is merged. Settled chats keep their directory; collection starts only after all chats release it through archival or deletion and existing work, setup, editor, shell, and fresh-read protections permit removal. The HTTP shell snapshot contains active chats only. Read archived chats through the existing V2 archived-shell RPC, using the same transport as command dispatch, so running work in archived chats also blocks cleanup. Verified with the isolated built V2 CLI, real JJ/APFS/pnpm, reference sockets, and lifecycle and lock tests. The old settlement condition fails the retention regression. Automatic merge/abandon detection is separate. Assisted-by: gpt-6.1-sol --- users/clover/packages/workspaces/README.md | 6 ++- .../clover/packages/workspaces/workspaces.mjs | 33 ++++++++---- .../packages/workspaces/workspaces.test.mjs | 50 ++++++++++++++++++- 3 files changed, 76 insertions(+), 13 deletions(-) diff --git a/users/clover/packages/workspaces/README.md b/users/clover/packages/workspaces/README.md index 0b7627cf3bce82a2991883b7f4e7ce9e05254df1..3e6f4776e50c2e38085c1012956bfbdbe2bd1bc9 100644 --- a/users/clover/packages/workspaces/README.md +++ b/users/clover/packages/workspaces/README.md @@ -36,7 +36,7 @@ reserve empty folder → create and open T3 thread → release waiting Claude/Codex hooks Every 30 seconds: - unfinished setup / unsettled T3 chat / running work / VS Code / live shell cwd + unfinished setup / unarchived T3 chat / running work / VS Code / live shell cwd → keep workspace, reset grace no references for ≥60 seconds, with a fresh check immediately before removal → JJ snapshot → JJ workspace forget → remove directory @@ -44,6 +44,10 @@ Every 30 seconds: → keep workspaces, reset grace ``` +Settling a chat keeps its workspace directory. Cleanup begins only after every +chat using the directory is archived or deleted and no work is running. Archival +releases the directory for cleanup; settling does not. + Only registered workspaces under `~/workspaces` are collected. JJ retains snapshotted changes; ignored files are deleted with the directory. Closing T3 pauses cleanup. VS Code's companion extension reports live folders and documents diff --git a/users/clover/packages/workspaces/workspaces.mjs b/users/clover/packages/workspaces/workspaces.mjs index 3b9280477ea43140b93d7b272f9dfd6c8bda2431..bfa53800b753e404987236b5babb828c78f1fc5e 100644 --- a/users/clover/packages/workspaces/workspaces.mjs +++ b/users/clover/packages/workspaces/workspaces.mjs @@ -152,15 +152,19 @@ export async function t3() { } async function snapshot() { - const data = await request("/api/orchestration/shell"); + const [data, archived] = await Promise.all([ + request("/api/orchestration/shell"), + rpc("orchestration.getArchivedShellSnapshot", {}), + ]); if ( !Array.isArray(data.projects) || !Array.isArray(data.threads) || - !Array.isArray(data.archivedThreads) + !Array.isArray(archived.threads) ) { throw new Error( "T3 Code returned an unknown thread list. Cleanup is paused.", ); } + data.archivedThreads = archived.threads; for (const thread of [...data.threads, ...data.archivedThreads]) { if ( ![ @@ -181,7 +185,7 @@ export async function t3() { return data; } - async function dispatch(command) { + async function rpc(tag, payload) { const { ticket } = await request("/api/auth/websocket-ticket", {}); const url = new URL("/ws", origin); url.protocol = "ws:"; @@ -193,7 +197,7 @@ export async function t3() { () => finish( new Error( - "T3 Code did not confirm the workspace chat. Run ws open to retry.", + "T3 Code did not respond. Start the nightly app and retry.", ), ), 15_000, @@ -209,10 +213,8 @@ export async function t3() { JSON.stringify({ _tag: "Request", id: "1", - tag: command.type.startsWith("project.") - ? "projects.mutate" - : "orchestration.dispatchCommand", - payload: { commandId: randomUUID(), ...command }, + tag, + payload, headers: [], }) + "\n", ); @@ -226,7 +228,7 @@ export async function t3() { else { finish( new Error( - `T3 Code rejected the workspace command: ${ + `T3 Code rejected the workspace request: ${ JSON.stringify(message.exit) }`, ), @@ -244,6 +246,16 @@ export async function t3() { }; }); } + + function dispatch(command) { + return rpc( + command.type.startsWith("project.") + ? "projects.mutate" + : "orchestration.dispatchCommand", + { commandId: randomUUID(), ...command }, + ); + } + return { snapshot, dispatch, environment, app }; } @@ -473,8 +485,7 @@ async function references(client) { ); const chats = []; for (const thread of [...snapshot.threads, ...snapshot.archivedThreads]) { - const released = thread.deletedAt !== null || thread.archivedAt !== null || - thread.settledOverride === "settled"; + const released = thread.deletedAt !== null || thread.archivedAt !== null; const busy = ![ "idle", "completed", diff --git a/users/clover/packages/workspaces/workspaces.test.mjs b/users/clover/packages/workspaces/workspaces.test.mjs index bc9df9a0cc341710c4897c88b2bb9ceb523efc23..25af8ab78bf61fdcfbf5afeb8615ad40f9eec703 100644 --- a/users/clover/packages/workspaces/workspaces.test.mjs +++ b/users/clover/packages/workspaces/workspaces.test.mjs @@ -457,7 +457,15 @@ exec "$WORKSPACE_TEST_PNPM" "$@" ); await client.dispatch({ type: "thread.settle", threadId: originalThread }); await main(["gc"]); - assert.ok(fs.existsSync(first), "settlement begins the grace period"); + assert.ok(fs.existsSync(first), "settlement keeps the prepared workspace"); + assert.equal(readRecord().idleSince, null); + age(); + await main(["gc"]); + assert.ok(fs.existsSync(first), "settlement never expires after the grace period"); + assert.equal(readRecord().idleSince, null); + await client.dispatch({ type: "thread.archive", threadId: originalThread }); + await main(["gc"]); + assert.ok(fs.existsSync(first), "archival begins the grace period"); assert.ok(readRecord().idleSince > Date.now() - 5000); editorPaths = [path.join(first, "shared/local")]; @@ -523,7 +531,45 @@ exec "$WORKSPACE_TEST_PNPM" "$@" fs.existsSync(first), "another unsettled chat in the workspace project keeps the folder", ); + await client.dispatch({ type: "thread.settle", threadId: anotherThread }); + age(); + await main(["gc"]); + assert.ok(fs.existsSync(first), "another settled chat still keeps the folder"); + assert.equal(readRecord().idleSince, null); await client.dispatch({ type: "thread.archive", threadId: anotherThread }); + const archivedThread = (await client.snapshot()).archivedThreads.find( + (thread) => thread.id === originalThread, + ); + assert.ok(archivedThread, "reference reads include archived chats"); + const fetch = globalThis.fetch; + let snapshotReads = 0; + let busy = true; + const referenceMock = mock.method(globalThis, "fetch", async (...args) => { + const response = await fetch(...args); + if (!String(args[0]).endsWith("/api/orchestration/shell")) return response; + snapshotReads++; + const snapshot = await response.json(); + const thread = { ...archivedThread }; + if (busy) thread.activeRunId = randomUUID(); + else if (snapshotReads === 2) thread.archivedAt = null; + snapshot.threads.push(thread); + return new Response(JSON.stringify(snapshot), { status: response.status }); + }); + age(); + await main(["gc"]); + assert.ok(fs.existsSync(first), "running work keeps an archived workspace"); + assert.equal(readRecord().idleSince, null); + busy = false; + snapshotReads = 0; + age(); + await main(["gc"]); + assert.equal(snapshotReads, 2, "cleanup rechecks references before removal"); + assert.ok( + fs.existsSync(first), + "a settled chat returning at the fresh check keeps the folder", + ); + assert.equal(readRecord().idleSince, null); + referenceMock.mock.restore(); age(); await main(["gc", "--dry-run"]); assert.ok(fs.existsSync(first), "dry run never removes a workspace"); @@ -567,6 +613,8 @@ exec "$WORKSPACE_TEST_PNPM" "$@" }); age(); await main(["gc"]); + assert.ok(fs.existsSync(first), "a settled replacement chat keeps its workspace"); + assert.equal(readRecord().idleSince, null); const secondRecord = JSON.parse( fs.readFileSync( path.join(directory, ".local/state/workspaces/workspaces/second.json"), -- 2.54.1