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
3636 → release waiting Claude/Codex hooks
3737
3838Every 30 seconds:
39 unfinished setup / unsettled T3 chat / running work / VS Code / live shell cwd
39 unfinished setup / unarchived T3 chat / running work / VS Code / live shell cwd
4040 → keep workspace, reset grace
4141 no references for ≥60 seconds, with a fresh check immediately before removal
4242 → JJ snapshot → JJ workspace forget → remove directory
......@@ -44,6 +44,10 @@ Every 30 seconds:
4444 → keep workspaces, reset grace
4545```
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
4751Only registered workspaces under `~/workspaces` are collected. JJ retains
4852snapshotted changes; ignored files are deleted with the directory. Closing T3
4953pauses 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() {
152152 }
153153
154154 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 ]);
156159 if (
157160 !Array.isArray(data.projects) || !Array.isArray(data.threads) ||
158 !Array.isArray(data.archivedThreads)
161 !Array.isArray(archived.threads)
159162 ) {
160163 throw new Error(
161164 "T3 Code returned an unknown thread list. Cleanup is paused.",
162165 );
163166 }
167 data.archivedThreads = archived.threads;
164168 for (const thread of [...data.threads, ...data.archivedThreads]) {
165169 if (
166170 ![
......@@ -181,7 +185,7 @@ export async function t3() {
181185 return data;
182186 }
183187
184 async function dispatch(command) {
188 async function rpc(tag, payload) {
185189 const { ticket } = await request("/api/auth/websocket-ticket", {});
186190 const url = new URL("/ws", origin);
187191 url.protocol = "ws:";
......@@ -193,7 +197,7 @@ export async function t3() {
193197 () =>
194198 finish(
195199 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.",
197201 ),
198202 ),
199203 15_000,
......@@ -209,10 +213,8 @@ export async function t3() {
209213 JSON.stringify({
210214 _tag: "Request",
211215 id: "1",
212 tag: command.type.startsWith("project.")
213 ? "projects.mutate"
214 : "orchestration.dispatchCommand",
215 payload: { commandId: randomUUID(), ...command },
216 tag,
217 payload,
216218 headers: [],
217219 }) + "\n",
218220 );
......@@ -226,7 +228,7 @@ export async function t3() {
226228 else {
227229 finish(
228230 new Error(
229 `T3 Code rejected the workspace command: ${
231 `T3 Code rejected the workspace request: ${
230232 JSON.stringify(message.exit)
231233 }`,
232234 ),
......@@ -244,6 +246,16 @@ export async function t3() {
244246 };
245247 });
246248 }
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
247259 return { snapshot, dispatch, environment, app };
248260}
249261
......@@ -473,8 +485,7 @@ async function references(client) {
473485 );
474486 const chats = [];
475487 for (const thread of [...snapshot.threads, ...snapshot.archivedThreads]) {
476 const released = thread.deletedAt !== null || thread.archivedAt !== null ||
477 thread.settledOverride === "settled";
488 const released = thread.deletedAt !== null || thread.archivedAt !== null;
478489 const busy = ![
479490 "idle",
480491 "completed",
users/clover/packages/workspaces/workspaces.test.mjs+49-1
......@@ -457,7 +457,15 @@ exec "$WORKSPACE_TEST_PNPM" "$@"
457457 );
458458 await client.dispatch({ type: "thread.settle", threadId: originalThread });
459459 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");
461469 assert.ok(readRecord().idleSince > Date.now() - 5000);
462470
463471 editorPaths = [path.join(first, "shared/local")];
......@@ -523,7 +531,45 @@ exec "$WORKSPACE_TEST_PNPM" "$@"
523531 fs.existsSync(first),
524532 "another unsettled chat in the workspace project keeps the folder",
525533 );
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);
526539 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();
527573 age();
528574 await main(["gc", "--dry-run"]);
529575 assert.ok(fs.existsSync(first), "dry run never removes a workspace");
......@@ -567,6 +613,8 @@ exec "$WORKSPACE_TEST_PNPM" "$@"
567613 });
568614 age();
569615 await main(["gc"]);
616 assert.ok(fs.existsSync(first), "a settled replacement chat keeps its workspace");
617 assert.equal(readRecord().idleSince, null);
570618 const secondRecord = JSON.parse(
571619 fs.readFileSync(
572620 path.join(directory, ".local/state/workspaces/workspaces/second.json"),