Files
loki/scripts/test/tab-memory.ts
CatoandClaude 1f1f40429c fix(builder): sessions survive cloud builder updates (#1326)
Every deploy restarted the cloud builder, and a restart ends every session
it holds. The drain waited for nobody to be mid-task, then logged "idle
sessions resume on their next dispatch" — nothing resumed them. The owner
found "No live sessions" after each of five deploys in one afternoon.

- The builder writes down each session it opens (~/.loki/open-tabs.json)
  and crosses it off only when it really ends: closed from Loki, or the
  agent exited while the builder stayed up. On start it reopens the rest;
  Claude sessions come back as the same conversation (claude --resume).
- Deploys compare runner files by content (rsync --checksum). A fresh
  checkout gives every file a new mtime, so every deploy, docs-only
  included, read as a runner change and restarted it.
- The manifest sync no longer lists a package-lock.json this pnpm repo
  does not have; its rsync error blanked the output and hid real changes.
- Runner 0.8.47; the drain log says what happens now.


Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-19 15:47:07 +02:00

115 lines
3.9 KiB
TypeScript

/**
* The cloud builder reopens the sessions a restart ended (desktop/src/main/
* tab-memory.ts). Owner, 2026-10-19: five deploys in an afternoon each
* restarted the builder, and each restart left "No live sessions" behind.
*
* Pinned: a session opened is written down; one closed from Loki, or whose
* agent exits while the builder stays up, is crossed off; a late exit from an
* agent that was replaced does not cross off its replacement; a restart
* reopens what is written down, resuming Claude's conversation; one that
* cannot come back is crossed off instead of retried forever.
*
* Run: pnpm exec tsx scripts/test/tab-memory.ts
*/
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import {
enableTabMemory,
forgetTab,
rememberTab,
rememberedTabs,
restoreTabs,
} from "../../desktop/src/main/tab-memory";
let passed = 0;
function check(label: string, condition: boolean): void {
if (!condition) throw new Error(`✗ ${label}`);
passed++;
console.log(` ✓ ${label}`);
}
async function main(): Promise<void> {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "tab-memory-"));
const dirA = path.join(root, "bitbaum");
const dirB = path.join(root, "center-som");
fs.mkdirSync(dirA);
fs.mkdirSync(dirB);
// Off by default: a laptop runner remembers nothing.
rememberTab({ tab: "bitbaum", dir: dirA, agent: "claude" });
check("off unless the box turns it on", rememberedTabs().length === 0);
enableTabMemory(path.join(root, ".loki", "open-tabs.json"));
const genA = rememberTab({ tab: "bitbaum", dir: dirA, agent: "claude" });
rememberTab({ tab: "Center SOM", dir: dirB, agent: "codex" });
check(
"each opened session is written down",
rememberedTabs()
.map((t) => t.tab)
.join() === "bitbaum,Center SOM",
);
// An agent switch: the old agent's exit lands after the new one opened.
const genA2 = rememberTab({ tab: "bitbaum", dir: dirA, agent: "claude" });
forgetTab("bitbaum", genA);
check("a replaced agent's late exit keeps its replacement", rememberedTabs().length === 2);
check("one row per session", rememberedTabs().filter((t) => t.tab === "bitbaum").length === 1);
// A restart: nothing is crossed off, and everything comes back.
const launched: string[] = [];
const restored = await restoreTabs({
isLive: () => false,
launch: async (tab, dir, agent, sessionId) => {
launched.push(`${tab}|${agent}|${sessionId ?? "-"}`);
},
sessionFor: async (dir) => (dir === dirA ? "c0ffee" : null),
});
check("both sessions reopen", restored.length === 2);
check(
"Claude resumes its conversation; another agent starts fresh",
[...launched].sort().join() === "Center SOM|codex|-,bitbaum|claude|c0ffee",
);
// Already live (the poller beat us to it): left alone.
launched.length = 0;
await restoreTabs({
isLive: () => true,
launch: async (tab) => {
launched.push(tab);
},
sessionFor: async () => null,
});
check("a live session is not launched twice", launched.length === 0);
// A folder that is gone: crossed off, not retried on every restart.
fs.rmSync(dirB, { recursive: true });
const lines: string[] = [];
await restoreTabs({
isLive: () => false,
launch: async () => {},
sessionFor: async () => null,
log: (l) => lines.push(l),
});
check(
"a session that cannot come back is crossed off",
!rememberedTabs().some((t) => t.tab === "Center SOM"),
);
check(
"and the log says why",
lines.some((l) => l.includes("is gone")),
);
// Closed on purpose (or the agent exited while the builder stayed up).
forgetTab("bitbaum", genA2);
check("a session that really ended is crossed off", rememberedTabs().length === 0);
fs.rmSync(root, { recursive: true, force: true });
console.log(`\n${passed}/${passed} tab-memory cases passed`);
}
main().catch((e) => {
console.error(e instanceof Error ? e.message : e);
process.exit(1);
});