authoravatarclover caruso<git@paperclover.net>2026-10-13 16:02:09-07:00
committeravatarclover caruso<git@paperclover.net>2026-11-13 16:28:31-08:00
logd42c02bf0f8c0e77c11a68135eee6926251c0c15
treecbf8f8efee5de97a8d6ee690eef36c953bbf5958
parent4bf7386b8ac43d8c776938af98f4e158f7978df4
signature Signed by SSH key SHA256:cOKiuRFOeSRxne6EWgHtdQQSlBxjOXm2hOCFnCdLQbQ

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

3 files changed, 76 insertions(+), 13 deletions(-)

users/clover/packages/workspaces/README.md+5-1
...@@ -36,7 +36,7 @@ reserve empty folder → create and open T3 thread...@@ -36,7 +36,7 @@ reserve empty folder → create and open T3 thread
36 → release waiting Claude/Codex hooks36 → release waiting Claude/Codex hooks
3737
38Every 30 seconds:38Every 30 seconds:
39 unfinished setup / unsettled T3 chat / running work / VS Code / live shell cwd39 unfinished setup / unarchived T3 chat / running work / VS Code / live shell cwd
40 → keep workspace, reset grace40 → keep workspace, reset grace
41 no references for ≥60 seconds, with a fresh check immediately before removal41 no references for ≥60 seconds, with a fresh check immediately before removal
42 → JJ snapshot → JJ workspace forget → remove directory42 → JJ snapshot → JJ workspace forget → remove directory
...@@ -44,6 +44,10 @@ Every 30 seconds:...@@ -44,6 +44,10 @@ Every 30 seconds:
44 → keep workspaces, reset grace44 → keep workspaces, reset grace
45```45```
4646
47Settling a chat keeps its workspace directory. Cleanup begins only after every
48chat using the directory is archived or deleted and no work is running. Archival
49releases the directory for cleanup; settling does not.
50
47Only registered workspaces under `~/workspaces` are collected. JJ retains51Only registered workspaces under `~/workspaces` are collected. JJ retains
48snapshotted changes; ignored files are deleted with the directory. Closing T352snapshotted changes; ignored files are deleted with the directory. Closing T3
49pauses cleanup. VS Code's companion extension reports live folders and documents53pauses cleanup. VS Code's companion extension reports live folders and documents
users/clover/packages/workspaces/workspaces.mjs+22-11
...@@ -152,15 +152,19 @@ export async function t3() {...@@ -152,15 +152,19 @@ export async function t3() {
152 }152 }
153153
154 async function snapshot() {154 async function snapshot() {
155 const data = await request("/api/orchestration/shell");155 const [data, archived] = await Promise.all([
156 request("/api/orchestration/shell"),
157 rpc("orchestration.getArchivedShellSnapshot", {}),
158 ]);
156 if (159 if (
157 !Array.isArray(data.projects) || !Array.isArray(data.threads) ||160 !Array.isArray(data.projects) || !Array.isArray(data.threads) ||
158 !Array.isArray(data.archivedThreads)161 !Array.isArray(archived.threads)
159 ) {162 ) {
160 throw new Error(163 throw new Error(
161 "T3 Code returned an unknown thread list. Cleanup is paused.",164 "T3 Code returned an unknown thread list. Cleanup is paused.",
162 );165 );
163 }166 }
167 data.archivedThreads = archived.threads;
164 for (const thread of [...data.threads, ...data.archivedThreads]) {168 for (const thread of [...data.threads, ...data.archivedThreads]) {
165 if (169 if (
166 ![170 ![
...@@ -181,7 +185,7 @@ export async function t3() {...@@ -181,7 +185,7 @@ export async function t3() {
181 return data;185 return data;
182 }186 }
183187
184 async function dispatch(command) {188 async function rpc(tag, payload) {
185 const { ticket } = await request("/api/auth/websocket-ticket", {});189 const { ticket } = await request("/api/auth/websocket-ticket", {});
186 const url = new URL("/ws", origin);190 const url = new URL("/ws", origin);
187 url.protocol = "ws:";191 url.protocol = "ws:";
...@@ -193,7 +197,7 @@ export async function t3() {...@@ -193,7 +197,7 @@ export async function t3() {
193 () =>197 () =>
194 finish(198 finish(
195 new Error(199 new Error(
196 "T3 Code did not confirm the workspace chat. Run ws open to retry.",200 "T3 Code did not respond. Start the nightly app and retry.",
197 ),201 ),
198 ),202 ),
199 15_000,203 15_000,
...@@ -209,10 +213,8 @@ export async function t3() {...@@ -209,10 +213,8 @@ export async function t3() {
209 JSON.stringify({213 JSON.stringify({
210 _tag: "Request",214 _tag: "Request",
211 id: "1",215 id: "1",
212 tag: command.type.startsWith("project.")216 tag,
213 ? "projects.mutate"217 payload,
214 : "orchestration.dispatchCommand",
215 payload: { commandId: randomUUID(), ...command },
216 headers: [],218 headers: [],
217 }) + "\n",219 }) + "\n",
218 );220 );
...@@ -226,7 +228,7 @@ export async function t3() {...@@ -226,7 +228,7 @@ export async function t3() {
226 else {228 else {
227 finish(229 finish(
228 new Error(230 new Error(
229 `T3 Code rejected the workspace command: ${231 `T3 Code rejected the workspace request: ${
230 JSON.stringify(message.exit)232 JSON.stringify(message.exit)
231 }`,233 }`,
232 ),234 ),
...@@ -244,6 +246,16 @@ export async function t3() {...@@ -244,6 +246,16 @@ export async function t3() {
244 };246 };
245 });247 });
246 }248 }
249
250 function dispatch(command) {
251 return rpc(
252 command.type.startsWith("project.")
253 ? "projects.mutate"
254 : "orchestration.dispatchCommand",
255 { commandId: randomUUID(), ...command },
256 );
257 }
258
247 return { snapshot, dispatch, environment, app };259 return { snapshot, dispatch, environment, app };
248}260}
249261
...@@ -473,8 +485,7 @@ async function references(client) {...@@ -473,8 +485,7 @@ async function references(client) {
473 );485 );
474 const chats = [];486 const chats = [];
475 for (const thread of [...snapshot.threads, ...snapshot.archivedThreads]) {487 for (const thread of [...snapshot.threads, ...snapshot.archivedThreads]) {
476 const released = thread.deletedAt !== null || thread.archivedAt !== null ||488 const released = thread.deletedAt !== null || thread.archivedAt !== null;
477 thread.settledOverride === "settled";
478 const busy = ![489 const busy = ![
479 "idle",490 "idle",
480 "completed",491 "completed",
users/clover/packages/workspaces/workspaces.test.mjs+49-1
...@@ -457,7 +457,15 @@ exec "$WORKSPACE_TEST_PNPM" "$@"...@@ -457,7 +457,15 @@ exec "$WORKSPACE_TEST_PNPM" "$@"
457 );457 );
458 await client.dispatch({ type: "thread.settle", threadId: originalThread });458 await client.dispatch({ type: "thread.settle", threadId: originalThread });
459 await main(["gc"]);459 await main(["gc"]);
460 assert.ok(fs.existsSync(first), "settlement begins the grace period");460 assert.ok(fs.existsSync(first), "settlement keeps the prepared workspace");
461 assert.equal(readRecord().idleSince, null);
462 age();
463 await main(["gc"]);
464 assert.ok(fs.existsSync(first), "settlement never expires after the grace period");
465 assert.equal(readRecord().idleSince, null);
466 await client.dispatch({ type: "thread.archive", threadId: originalThread });
467 await main(["gc"]);
468 assert.ok(fs.existsSync(first), "archival begins the grace period");
461 assert.ok(readRecord().idleSince > Date.now() - 5000);469 assert.ok(readRecord().idleSince > Date.now() - 5000);
462470
463 editorPaths = [path.join(first, "shared/local")];471 editorPaths = [path.join(first, "shared/local")];
...@@ -523,7 +531,45 @@ exec "$WORKSPACE_TEST_PNPM" "$@"...@@ -523,7 +531,45 @@ exec "$WORKSPACE_TEST_PNPM" "$@"
523 fs.existsSync(first),531 fs.existsSync(first),
524 "another unsettled chat in the workspace project keeps the folder",532 "another unsettled chat in the workspace project keeps the folder",
525 );533 );
534 await client.dispatch({ type: "thread.settle", threadId: anotherThread });
535 age();
536 await main(["gc"]);
537 assert.ok(fs.existsSync(first), "another settled chat still keeps the folder");
538 assert.equal(readRecord().idleSince, null);
526 await client.dispatch({ type: "thread.archive", threadId: anotherThread });539 await client.dispatch({ type: "thread.archive", threadId: anotherThread });
540 const archivedThread = (await client.snapshot()).archivedThreads.find(
541 (thread) => thread.id === originalThread,
542 );
543 assert.ok(archivedThread, "reference reads include archived chats");
544 const fetch = globalThis.fetch;
545 let snapshotReads = 0;
546 let busy = true;
547 const referenceMock = mock.method(globalThis, "fetch", async (...args) => {
548 const response = await fetch(...args);
549 if (!String(args[0]).endsWith("/api/orchestration/shell")) return response;
550 snapshotReads++;
551 const snapshot = await response.json();
552 const thread = { ...archivedThread };
553 if (busy) thread.activeRunId = randomUUID();
554 else if (snapshotReads === 2) thread.archivedAt = null;
555 snapshot.threads.push(thread);
556 return new Response(JSON.stringify(snapshot), { status: response.status });
557 });
558 age();
559 await main(["gc"]);
560 assert.ok(fs.existsSync(first), "running work keeps an archived workspace");
561 assert.equal(readRecord().idleSince, null);
562 busy = false;
563 snapshotReads = 0;
564 age();
565 await main(["gc"]);
566 assert.equal(snapshotReads, 2, "cleanup rechecks references before removal");
567 assert.ok(
568 fs.existsSync(first),
569 "a settled chat returning at the fresh check keeps the folder",
570 );
571 assert.equal(readRecord().idleSince, null);
572 referenceMock.mock.restore();
527 age();573 age();
528 await main(["gc", "--dry-run"]);574 await main(["gc", "--dry-run"]);
529 assert.ok(fs.existsSync(first), "dry run never removes a workspace");575 assert.ok(fs.existsSync(first), "dry run never removes a workspace");
...@@ -567,6 +613,8 @@ exec "$WORKSPACE_TEST_PNPM" "$@"...@@ -567,6 +613,8 @@ exec "$WORKSPACE_TEST_PNPM" "$@"
567 });613 });
568 age();614 age();
569 await main(["gc"]);615 await main(["gc"]);
616 assert.ok(fs.existsSync(first), "a settled replacement chat keeps its workspace");
617 assert.equal(readRecord().idleSince, null);
570 const secondRecord = JSON.parse(618 const secondRecord = JSON.parse(
571 fs.readFileSync(619 fs.readFileSync(
572 path.join(directory, ".local/state/workspaces/workspaces/second.json"),620 path.join(directory, ".local/state/workspaces/workspaces/second.json"),