kaizen-agent 1.61.6 → 1.62.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/README.md +2 -2
  2. package/agents/kaizen-builder.md +3 -0
  3. package/agents/kaizen-planner.md +3 -0
  4. package/agents/kaizen-reviewer.md +2 -0
  5. package/assets/kaizen-logo.png +0 -0
  6. package/assets/kaizen.ico +0 -0
  7. package/cli/__pycache__/computer-helper.cpython-314.pyc +0 -0
  8. package/cli/accounts.test.ts +126 -0
  9. package/cli/accounts.ts +116 -0
  10. package/cli/add-project.ts +4 -2
  11. package/cli/agent-prefs.test.ts +127 -0
  12. package/cli/agent-prefs.ts +106 -0
  13. package/cli/board.test.ts +4 -5
  14. package/cli/computer-helper.py +299 -0
  15. package/cli/computer.test.ts +155 -0
  16. package/cli/computer.ts +66 -0
  17. package/cli/dashboard.ts +14 -15
  18. package/cli/install.ts +67 -4
  19. package/cli/keep-awake.ts +30 -0
  20. package/cli/project-tree.test.ts +1 -0
  21. package/cli/pty.test.ts +2 -1
  22. package/cli/pty.ts +6 -5
  23. package/cli/report.test.ts +59 -0
  24. package/cli/report.ts +62 -0
  25. package/cli/restart.test.ts +1 -0
  26. package/cli/settings.test.ts +21 -1
  27. package/cli/settings.ts +19 -11
  28. package/cli/setup.test.ts +2 -2
  29. package/cli/skills.test.ts +89 -0
  30. package/cli/skills.ts +112 -0
  31. package/cli/state.test.ts +46 -4
  32. package/cli/state.ts +127 -41
  33. package/cli/term-image.test.ts +23 -0
  34. package/cli/term-image.ts +26 -0
  35. package/cli/usage.test.ts +8 -2
  36. package/cli/usage.ts +8 -6
  37. package/cli/video.test.ts +5 -5
  38. package/cli/web-accept.test.ts +1 -0
  39. package/cli/web-decide.test.ts +1 -0
  40. package/cli/web-pr.test.ts +1 -0
  41. package/cli/web-skills.test.ts +32 -0
  42. package/cli/web-symlink.test.ts +1 -0
  43. package/cli/web.ts +179 -20
  44. package/cli/workers.test.ts +319 -0
  45. package/cli/workers.ts +380 -0
  46. package/cli/worktree.test.ts +21 -2
  47. package/cli/worktree.ts +21 -2
  48. package/package.json +1 -1
  49. package/skills/kaizen/config.default.yml +33 -1
  50. package/skills/kaizen/settings.md +1 -1
  51. package/skills/kaizen/skill.json +1 -0
  52. package/skills/kaizen/spec-build.md +22 -2
  53. package/skills/kaizen/spec-main.md +9 -0
  54. package/skills/kaizen/spec-plan.md +18 -1
  55. package/skills/kaizen/spec-review.md +4 -0
  56. package/skills/kaizen/spec.md +15 -0
  57. package/skills/kaizen-help/SKILL.md +1 -0
  58. package/skills/kaizen-help/skill.json +1 -0
  59. package/skills/media/kaizen-book/skill.json +1 -0
  60. package/skills/media/kaizen-deck/skill.json +1 -0
  61. package/skills/media/kaizen-video/scripts/__pycache__/intake.cpython-314.pyc +0 -0
  62. package/skills/media/kaizen-video/scripts/__pycache__/make_film.cpython-314.pyc +0 -0
  63. package/skills/media/kaizen-video/scripts/__pycache__/make_tiktok.cpython-314.pyc +0 -0
  64. package/skills/media/kaizen-video/scripts/__pycache__/render_cut.cpython-314.pyc +0 -0
  65. package/skills/media/kaizen-video/scripts/__pycache__/sfx.cpython-314.pyc +0 -0
  66. package/skills/media/kaizen-video/scripts/__pycache__/srt.cpython-314.pyc +0 -0
  67. package/skills/media/kaizen-video/scripts/__pycache__/transcribe.cpython-314.pyc +0 -0
  68. package/skills/media/kaizen-video/scripts/__pycache__/vo.cpython-314.pyc +0 -0
  69. package/skills/media/kaizen-video/skill.json +1 -0
  70. package/skills/optional/kaizen-computer/SKILL.md +38 -0
  71. package/skills/optional/kaizen-computer/skill.json +1 -0
  72. package/web/board/add-project.ts +3 -3
  73. package/web/board/agent-favicons.ts +9 -0
  74. package/web/board/agent-term.ts +1 -1
  75. package/web/board/api.ts +1 -1
  76. package/web/board/approvals.ts +1 -1
  77. package/web/board/cards.ts +50 -7
  78. package/web/board/dictation.ts +46 -3
  79. package/web/board/fixes.ts +1 -1
  80. package/web/board/home.ts +1 -1
  81. package/web/board/icebox.ts +1 -1
  82. package/web/board/keys.ts +5 -15
  83. package/web/board/onboarding.ts +1 -1
  84. package/web/board/panel.ts +24 -20
  85. package/web/board/phone.ts +4 -1
  86. package/web/board/run-view.ts +11 -6
  87. package/web/board/settings-accounts.ts +60 -0
  88. package/web/board/settings-agents.ts +102 -0
  89. package/web/board/settings-orchestration.ts +32 -0
  90. package/web/board/settings-pages.test.ts +5 -0
  91. package/web/board/settings-pages.ts +10 -4
  92. package/web/board/settings-skills.ts +47 -0
  93. package/web/board/settings-voice.test.ts +18 -0
  94. package/web/board/settings-voice.ts +28 -0
  95. package/web/board/settings.ts +40 -4
  96. package/web/board/terms.ts +2 -2
  97. package/web/board/update.ts +1 -1
  98. package/web/board/ws-board.test.ts +6 -0
  99. package/web/board/ws-board.ts +2 -0
  100. package/web/board.html +15 -3
  101. package/web/board.js +134 -84
  102. package/web/notebook.js +2 -2
  103. package/web/notes.html +3 -3
  104. package/web/term.js +27 -27
  105. package/web/term.ts +25 -3
  106. package/web/video.html +2 -2
  107. package/web/video.js +2 -2
@@ -0,0 +1,319 @@
1
+ // Orchestration against throwaway git repos: nothing here touches a real project or agent folder.
2
+ import { afterAll, expect, test } from "bun:test";
3
+ import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
4
+ import { tmpdir } from "node:os";
5
+ import { delimiter, dirname, join } from "node:path";
6
+ import { readReport, signalOf } from "./report.ts";
7
+ import { workerStale, init, launchOf, merge, readWorkers, report, requested, requestStart, resolve, setRun, startWorker, statusText, type Status } from "./workers.ts";
8
+
9
+ const win = process.platform === "win32";
10
+ const t = win ? test.skip : test;
11
+ const roots: string[] = [];
12
+ afterAll(() => { for (const r of roots) rmSync(r, { recursive: true, force: true }); });
13
+
14
+ const g = (cwd: string, ...a: string[]) => {
15
+ const r = Bun.spawnSync(["git", ...a], { cwd, stdin: "ignore" });
16
+ if (r.exitCode !== 0) throw new Error(`git ${a.join(" ")}: ${r.stderr.toString()}`);
17
+ return r.stdout.toString().trim();
18
+ };
19
+
20
+ // A repo with a run worktree on kaizen/<id>, a plan with two workers, and an approval that records the split.
21
+ let n = 0;
22
+ function fixture(o: { state?: object; plan?: string; approved?: boolean } = {}) {
23
+ const project = mkdtempSync(join(tmpdir(), "kaizen-workers-"));
24
+ roots.push(project);
25
+ const id = `2026-10-31-t${++n}`;
26
+ g(project, "init", "-q", "-b", "main");
27
+ g(project, "config", "user.email", "t@t"); g(project, "config", "user.name", "t");
28
+ writeFileSync(join(project, "a.txt"), "one\ntwo\nthree\n");
29
+ g(project, "add", "."); g(project, "commit", "-qm", "base");
30
+ const wt = join(project, ".kaizen", "wt", id), branch = `kaizen/${id}`;
31
+ g(project, "worktree", "add", "-q", "-b", branch, wt);
32
+ const dir = join(project, ".kaizen", "runs", id);
33
+ mkdirSync(dir, { recursive: true });
34
+ writeFileSync(join(dir, "state.json"), JSON.stringify({ id, stage: "build", runner: "full", agent: "claude", orchestration: "on", worktree: wt, branch, ...o.state }));
35
+ writeFileSync(join(dir, "01-plan.md"), o.plan ?? "# Plan\n\n## Workers\n- w1 — left half — owns: src/left, a.txt\n- w2 — right half — owns: src/right\n\n## Goal\nx\n");
36
+ if (o.approved !== false) writeFileSync(join(dir, "02-approval.md"), "# Approval\n\nworkers: approved\n");
37
+ return { project, id, wt, branch, dir };
38
+ }
39
+
40
+ // Marks a worker with a status and, when asked, gives it a real worktree and a committed change.
41
+ function setup(f: ReturnType<typeof fixture>, status: Status | Status[], edits?: Record<string, [string, string]>) {
42
+ expect(init(f.project, f.id)).toEqual({ ok: true, n: 2 });
43
+ const w = readWorkers(f.project, f.id)!;
44
+ w.workers.forEach((x, i) => {
45
+ x.status = Array.isArray(status) ? status[i]! : status;
46
+ g(f.project, "worktree", "add", "-q", "-b", x.branch, x.worktree, f.branch);
47
+ const e = edits?.[x.id];
48
+ if (e) { mkdirSync(dirname(join(x.worktree, e[0])), { recursive: true }); writeFileSync(join(x.worktree, e[0]), e[1]); g(x.worktree, "add", "."); g(x.worktree, "commit", "-qm", x.id); }
49
+ });
50
+ writeFileSync(join(f.dir, "workers.json"), JSON.stringify(w));
51
+ }
52
+ const head = (f: ReturnType<typeof fixture>) => g(f.wt, "rev-parse", "HEAD");
53
+
54
+ t("G-01 off: init writes nothing, status says off, the board finds nothing to start", () => {
55
+ const f = fixture({ state: { orchestration: undefined } });
56
+ expect(resolve(f.project, f.id).on).toBe(false);
57
+ const r = init(f.project, f.id);
58
+ expect(r.ok).toBe(false);
59
+ expect(existsSync(join(f.dir, "workers.json"))).toBe(false);
60
+ expect(statusText(f.project, f.id)).toContain("orchestration is off");
61
+ expect(requested(f.project)).toEqual([]);
62
+ });
63
+
64
+ t("G-02 lite, Windows and no worktree each resolve off with a reason", () => {
65
+ expect(resolve(...(([f]) => [f.project, f.id] as const)([fixture({ state: { runner: "lite" } })]))).toMatchObject({ on: false, why: expect.stringContaining("lite") });
66
+ const w = fixture();
67
+ expect(resolve(w.project, w.id, "win32")).toMatchObject({ on: false, why: expect.stringContaining("Windows") });
68
+ const n = fixture({ state: { worktree: undefined, branch: undefined } });
69
+ expect(resolve(n.project, n.id)).toMatchObject({ on: false, why: expect.stringContaining("no worktree") });
70
+ expect(resolve(w.project, w.id)).toMatchObject({ on: true });
71
+ });
72
+
73
+ t("G-03 a worker has no bypass flag under own, even with global bypass; with bypass it is flagged and titled", () => {
74
+ const f = fixture();
75
+ const prefs = join(f.project, "agents.json");
76
+ writeFileSync(prefs, JSON.stringify({ permissions: "bypass", agents: { claude: { command: "/bin/sh" } } }));
77
+ process.env.KAIZEN_AGENT_PREFS = prefs;
78
+ try {
79
+ init(f.project, f.id);
80
+ const x = readWorkers(f.project, f.id)!.workers[0]!;
81
+ const own = launchOf(f.project, f.id, x, "own");
82
+ if (typeof own === "string") throw new Error(own);
83
+ expect(own.argv.join(" ")).not.toContain("dangerously");
84
+ expect([own.bypass, own.title]).toEqual([false, "Worker w1"]);
85
+ const by = launchOf(f.project, f.id, x, "bypass");
86
+ if (typeof by === "string") throw new Error(by);
87
+ expect(by.argv).toContain("--dangerously-skip-permissions");
88
+ expect([by.bypass, by.title]).toEqual([true, "Worker w1 · bypass"]);
89
+ } finally { delete process.env.KAIZEN_AGENT_PREFS; }
90
+ });
91
+
92
+ t("G-04 merge refuses unless every worker succeeded, and merges nothing", () => {
93
+ for (const bad of ["failed", "blocked", "unverified", "running", "requested"] as Status[]) {
94
+ const f = fixture();
95
+ setup(f, ["succeeded", bad], { w1: ["src-left.txt", "l"] });
96
+ const before = head(f), r = merge(f.project, f.id);
97
+ expect(r.ok).toBe(false);
98
+ expect(!r.ok && r.why).toContain(`w2 is ${bad}`);
99
+ expect(head(f)).toBe(before);
100
+ }
101
+ });
102
+
103
+ t("merge: two succeeded workers land on the run's branch and their folders go", () => {
104
+ const f = fixture();
105
+ setup(f, "succeeded", { w1: ["src/left/l.txt", "left"], w2: ["src/right/r.txt", "right"] });
106
+ const r = merge(f.project, f.id);
107
+ expect(r.ok).toBe(true);
108
+ expect(readFileSync(join(f.wt, "src/left/l.txt"), "utf8")).toBe("left");
109
+ expect(readFileSync(join(f.wt, "src/right/r.txt"), "utf8")).toBe("right");
110
+ const w = readWorkers(f.project, f.id)!;
111
+ expect([w.merge?.status, ...w.workers.map((x) => x.status)]).toEqual(["merged", "merged", "merged"]);
112
+ expect(existsSync(w.workers[0]!.worktree)).toBe(false);
113
+ expect(g(f.project, "branch", "--list", w.workers[0]!.branch)).toBe("");
114
+ });
115
+
116
+ t("G-05 a conflict leaves the run's branch where it was, marks it, names the files and sets the blocker", () => {
117
+ const f = fixture();
118
+ setup(f, "succeeded", { w1: ["a.txt", "one\nLEFT\nthree\n"], w2: ["src/right/f.txt", "RIGHT"] });
119
+ // the run's own branch moved on and now clashes with w2's file
120
+ mkdirSync(join(f.wt, "src", "right"), { recursive: true });
121
+ writeFileSync(join(f.wt, "src", "right", "f.txt"), "RUN"); g(f.wt, "add", "."); g(f.wt, "commit", "-qm", "run");
122
+ const before = head(f), r = merge(f.project, f.id);
123
+ expect(r.ok).toBe(false);
124
+ expect(head(f)).toBe(before);
125
+ expect(g(f.wt, "status", "--porcelain")).toBe("");
126
+ const w = readWorkers(f.project, f.id)!;
127
+ expect(w.workers[1]!.status).toBe("conflict");
128
+ expect(w.merge).toMatchObject({ status: "conflict", sha0: before, conflicts: ["src/right/f.txt"] });
129
+ expect(readReport(join(f.project, ".kaizen"), f.id)?.blocked?.question).toContain("src/right/f.txt");
130
+ expect(readFileSync(new URL("./workers.ts", import.meta.url), "utf8")).not.toMatch(/"-X|--strategy|"-s"/);
131
+ });
132
+
133
+ t("G-06 settings changed after init do not reach the run; the cap holds", () => {
134
+ const f = fixture();
135
+ init(f.project, f.id);
136
+ mkdirSync(join(f.project, ".kaizen"), { recursive: true });
137
+ writeFileSync(join(f.project, ".kaizen", "config.yml"), "orchestration:\n enabled: false\n max_workers: 1\n permissions: bypass\n");
138
+ setRun(f.project, f.id, "off") ?? null; // refused: it has workers
139
+ expect(statusText(f.project, f.id)).toContain("max 3 · permissions own · model inherit (frozen)");
140
+ // a hand-edited file with a fourth worker: the fourth start is refused
141
+ const w = readWorkers(f.project, f.id)!;
142
+ for (const id of ["w3", "w4"]) w.workers.push({ ...w.workers[0]!, id, status: "planned", after: [] });
143
+ w.workers[0]!.status = w.workers[1]!.status = w.workers[2]!.status = "running";
144
+ writeFileSync(join(f.dir, "workers.json"), JSON.stringify(w));
145
+ const r = requestStart(f.project, f.id, "w4", 100);
146
+ expect(r.ok).toBe(false);
147
+ expect(r.why).toContain("most allowed is 3");
148
+ expect(readWorkers(f.project, f.id)!.workers[3]!.status).toBe("planned");
149
+ });
150
+
151
+ t("G-07 overlapping ownership and an unrecorded approval are refused", () => {
152
+ const o = fixture({ plan: "## Workers\n- w1 — a — owns: src\n- w2 — b — owns: src/deep/file.ts\n" });
153
+ const r = init(o.project, o.id);
154
+ expect(!r.ok && r.why).toContain("both own");
155
+ expect(existsSync(join(o.dir, "workers.json"))).toBe(false);
156
+ const u = fixture({ approved: false });
157
+ const r2 = init(u.project, u.id);
158
+ expect(!r2.ok && r2.why).toContain("approved");
159
+ expect(existsSync(join(u.dir, "workers.json"))).toBe(false);
160
+ const m = fixture({ plan: "## Workers\n- x1 — a — owns: a\n- w2 — b — owns: b\n" });
161
+ expect(init(m.project, m.id).ok).toBe(false);
162
+ const big = fixture({ plan: "## Workers\n- w1 — a — owns: a\n- w2 — b — owns: b\n- w3 — c — owns: c\n- w4 — d — owns: d\n" });
163
+ const r4 = init(big.project, big.id);
164
+ expect(!r4.ok && r4.why).toContain("most allowed is 3");
165
+ });
166
+
167
+ t("G-08 start times out cleanly with no board and puts the worker back to planned; only the board scan starts workers", () => {
168
+ const f = fixture();
169
+ init(f.project, f.id);
170
+ const r = requestStart(f.project, f.id, "w1", 400);
171
+ expect(r).toEqual({ ok: false, why: "the board did not start it: is `kaizen web` running?" });
172
+ expect(readWorkers(f.project, f.id)!.workers[0]!.status).toBe("planned");
173
+ expect(requested(f.project)).toEqual([]);
174
+ const web = readFileSync(new URL("./web.ts", import.meta.url), "utf8");
175
+ expect(web.match(/startWorker\(/g)?.length).toBe(1);
176
+ const cli = readFileSync(new URL("./workers.ts", import.meta.url), "utf8");
177
+ expect(cli).not.toContain("startPty");
178
+ });
179
+
180
+ t("G-09 done refuses a dirty worktree; cleanup uses branch -d, never -D", () => {
181
+ const f = fixture();
182
+ setup(f, "running");
183
+ const x = readWorkers(f.project, f.id)!.workers[0]!;
184
+ writeFileSync(join(x.worktree, "new.txt"), "x");
185
+ const why = report(f.project, f.id, "w1", "done", { outcome: "succeeded", summary: "done" });
186
+ expect(why).toContain("commit first");
187
+ g(x.worktree, "add", "."); g(x.worktree, "commit", "-qm", "w1");
188
+ expect(report(f.project, f.id, "w1", "done", { outcome: "bogus", summary: "x" })).toContain("succeeded or failed");
189
+ expect(report(f.project, f.id, "w1", "done", { outcome: "succeeded", summary: "done" })).toBeNull();
190
+ expect(readWorkers(f.project, f.id)!.workers[0]!.status).toBe("succeeded");
191
+ const src = readFileSync(new URL("./workers.ts", import.meta.url), "utf8");
192
+ expect(src).toContain('"branch", "-d"');
193
+ expect(src).not.toContain('"-D"');
194
+ });
195
+
196
+ t("G-10 a blocked worker lights the run's existing blocker signal", () => {
197
+ const f = fixture();
198
+ setup(f, "running");
199
+ expect(report(f.project, f.id, "w2", "blocked", { text: "which table?" })).toBeNull();
200
+ const st = join(f.project, ".kaizen"), rep = readReport(st, f.id);
201
+ expect(rep?.blocked?.question).toBe("w2: which table?");
202
+ expect(signalOf({ stage: "build" }, rep, undefined, Date.now(), 5)).toBe("blocked");
203
+ expect(report(f.project, f.id, "w2", "unblock")).toBeNull();
204
+ expect(readReport(st, f.id)?.blocked).toBeUndefined();
205
+ });
206
+
207
+ t("worker model: the run's own agent's name, frozen at init; empty keeps the line; a tool with no model flag ignores it and says so", () => {
208
+ const f = fixture();
209
+ const cfg = join(f.project, ".kaizen", "config.yml");
210
+ writeFileSync(cfg, "orchestration:\n worker_model:\n claude: haiku\n cursor: whatever\n codex: gpt-x\n");
211
+ expect(resolve(f.project, f.id).model).toBe("haiku"); // claude is this run's agent, not codex
212
+ init(f.project, f.id);
213
+ writeFileSync(cfg, "orchestration:\n worker_model:\n claude: opus\n");
214
+ expect(readWorkers(f.project, f.id)!.frozen.model).toBe("haiku");
215
+ expect(statusText(f.project, f.id)).toContain("model haiku (frozen)");
216
+ expect(resolve(f.project, f.id).model).toBe("opus");
217
+ writeFileSync(cfg, "orchestration:\n worker_model:\n claude: not a model\n");
218
+ expect(resolve(f.project, f.id).model).toBe("");
219
+ writeFileSync(cfg, "orchestration:\n worker_model:\n claude: \"\"\n");
220
+ expect(resolve(f.project, f.id).model).toBe("");
221
+
222
+ const prefs = join(f.project, "agents.json");
223
+ writeFileSync(prefs, JSON.stringify({ agents: { claude: { command: "/bin/sh" }, cursor: { command: "/bin/sh" } } }));
224
+ process.env.KAIZEN_AGENT_PREFS = prefs;
225
+ try {
226
+ const x = readWorkers(f.project, f.id)!.workers[0]!;
227
+ const go = (pick: string) => { const l = launchOf(f.project, f.id, x, "own", undefined, pick); if (typeof l === "string") throw new Error(l); return l; };
228
+ const inh = go("");
229
+ expect(inh.argv).not.toContain("--model");
230
+ expect(inh.model).toBeUndefined();
231
+ const pick = go("sonnet");
232
+ expect(pick.argv.slice(pick.argv.indexOf("--model"), pick.argv.indexOf("--model") + 2)).toEqual(["--model", "sonnet"]);
233
+ expect([pick.model, pick.modelNote]).toEqual(["sonnet", undefined]);
234
+ const st = JSON.parse(readFileSync(join(f.dir, "state.json"), "utf8"));
235
+ writeFileSync(join(f.dir, "state.json"), JSON.stringify({ ...st, agent: "cursor" }));
236
+ const none = launchOf(f.project, f.id, x, "own", undefined, "haiku");
237
+ if (typeof none === "string") throw new Error(none);
238
+ expect(none.argv).not.toContain("--model");
239
+ expect(none.model).toBeUndefined();
240
+ expect(none.modelNote).toContain("takes no model flag");
241
+ } finally { delete process.env.KAIZEN_AGENT_PREFS; }
242
+ });
243
+
244
+ t("fix 1: a worker with after: branches off the worker it follows and sees its code", () => {
245
+ const f = fixture({ plan: "## Workers\n- w1 — left — owns: src/left\n- w2 — right — owns: src/right — after: w1\n" });
246
+ expect(init(f.project, f.id)).toEqual({ ok: true, n: 2 });
247
+ const w = readWorkers(f.project, f.id)!, [a, b] = w.workers as [typeof w.workers[0], typeof w.workers[0]];
248
+ g(f.project, "worktree", "add", "-q", "-b", a.branch, a.worktree, f.branch);
249
+ mkdirSync(join(a.worktree, "src"), { recursive: true });
250
+ writeFileSync(join(a.worktree, "src", "left"), "L"); g(a.worktree, "add", "."); g(a.worktree, "commit", "-qm", "w1");
251
+ a.status = "succeeded"; b.status = "requested"; b.requestedAt = Date.now();
252
+ writeFileSync(join(f.dir, "workers.json"), JSON.stringify(w));
253
+ // CI has no agent installed: a stand-in on PATH, so the start gets as far as the branch.
254
+ const bin = join(f.project, "bin"), path = process.env.PATH;
255
+ mkdirSync(bin);
256
+ writeFileSync(join(bin, "claude"), "#!/bin/sh\n", { mode: 0o755 });
257
+ writeFileSync(join(bin, "claude.cmd"), "@echo off\n");
258
+ process.env.PATH = `${bin}${delimiter}${path}`;
259
+ let r: ReturnType<typeof startWorker>;
260
+ try { r = startWorker(f.project, f.id, "w2"); } finally { process.env.PATH = path; }
261
+ expect(r.ok).toBe(true);
262
+ expect(readFileSync(join(b.worktree, "src", "left"), "utf8")).toBe("L");
263
+ // its own commit on top merges cleanly, and w1's code is not counted as w2's stray paths
264
+ mkdirSync(join(b.worktree, "src"), { recursive: true });
265
+ writeFileSync(join(b.worktree, "src", "right"), "R"); g(b.worktree, "add", "."); g(b.worktree, "commit", "-qm", "w2");
266
+ b.status = "succeeded";
267
+ writeFileSync(join(f.dir, "workers.json"), JSON.stringify(w));
268
+ expect(merge(f.project, f.id).ok).toBe(true);
269
+ expect(readFileSync(join(f.wt, "src", "right"), "utf8")).toBe("R");
270
+ });
271
+
272
+ t("fix 2: a stale request is not started by the board", () => {
273
+ const f = fixture();
274
+ init(f.project, f.id);
275
+ const w = readWorkers(f.project, f.id)!;
276
+ w.workers[0]!.status = "requested"; w.workers[0]!.requestedAt = Date.now() - 3_600_000;
277
+ writeFileSync(join(f.dir, "workers.json"), JSON.stringify(w));
278
+ expect(requested(f.project)).toEqual([]);
279
+ const r = startWorker(f.project, f.id, "w1");
280
+ expect(!r.ok && r.why).toContain("stale");
281
+ expect(existsSync(w.workers[0]!.worktree)).toBe(false);
282
+ });
283
+
284
+ t("fix 3: merge refuses a branch that changed paths it does not own", () => {
285
+ const f = fixture();
286
+ setup(f, "succeeded", { w1: ["src/left", "l"], w2: ["elsewhere.txt", "x"] });
287
+ const before = head(f), r = merge(f.project, f.id);
288
+ expect(!r.ok && r.why).toContain("w2 changed paths it does not own (elsewhere.txt)");
289
+ expect(head(f)).toBe(before);
290
+ });
291
+
292
+ t("fix 4: setRun writes state.json whole, with no temp file left", () => {
293
+ const f = fixture();
294
+ expect(setRun(f.project, f.id, "off")).toBeNull();
295
+ expect(JSON.parse(readFileSync(join(f.dir, "state.json"), "utf8")).orchestration).toBe("off");
296
+ expect(readdirSync(f.dir).filter((n) => n.endsWith(".tmp"))).toEqual([]);
297
+ });
298
+
299
+ t("fix 5: merge says what cleanup left behind", () => {
300
+ const f = fixture();
301
+ setup(f, "succeeded", { w1: ["src/left", "l"], w2: ["src/right", "r"] });
302
+ const x = readWorkers(f.project, f.id)!.workers[0]!;
303
+ writeFileSync(join(x.worktree, "untracked.txt"), "u");
304
+ const r = merge(f.project, f.id);
305
+ expect(r.ok && r.left.length).toBe(2);
306
+ expect(r.ok && r.left.join("\n")).toContain("w1 folder");
307
+ expect(r.ok && r.left.join("\n")).toContain("w1 branch");
308
+ expect(existsSync(x.worktree)).toBe(true);
309
+ });
310
+
311
+ test("a live worker gone quiet reads stale; a busy, asking or done one does not", () => {
312
+ const run = { status: "running" }, old = { alive: true, lastOut: 0 }, t = 11 * 60_000;
313
+ expect(workerStale(run, old, t, 5)).toBe(true);
314
+ expect(workerStale(run, { ...old, busy: true }, t, 5)).toBe(false);
315
+ expect(workerStale(run, { ...old, asking: true }, t, 5)).toBe(false);
316
+ expect(workerStale(run, { alive: true, lastOut: t - 60_000 }, t, 5)).toBe(false);
317
+ expect(workerStale({ status: "succeeded" }, old, t, 5)).toBe(false);
318
+ expect(workerStale(run, null, t, 5)).toBe(false);
319
+ });