kaizen-agent 1.63.0 → 1.64.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.
- package/cli/handoff.test.ts +133 -0
- package/cli/handoff.ts +122 -0
- package/cli/install.ts +25 -2
- package/cli/pty.ts +16 -2
- package/cli/resources.test.ts +50 -0
- package/cli/resources.ts +113 -0
- package/cli/restart.test.ts +28 -1
- package/cli/settings.ts +3 -0
- package/cli/state.ts +8 -1
- package/cli/usage.test.ts +11 -0
- package/cli/web.ts +86 -5
- package/cli/workers.ts +11 -4
- package/cli/worktree.test.ts +174 -1
- package/cli/worktree.ts +190 -35
- package/package.json +1 -1
- package/skills/kaizen/config.default.yml +10 -0
- package/skills/kaizen/settings.md +3 -0
- package/skills/kaizen/spec-build.md +13 -0
- package/skills/kaizen/spec-main.md +13 -0
- package/web/board/cards.ts +1 -0
- package/web/board/git.ts +4 -4
- package/web/board/phone.ts +3 -5
- package/web/board/resources.ts +152 -0
- package/web/board/workspace.ts +2 -0
- package/web/board/ws-tabs.test.ts +8 -0
- package/web/board/ws-tabs.ts +8 -7
- package/web/board.html +13 -1
- package/web/board.js +99 -89
- package/web/notebook.js +23 -23
- package/web/notes.html +1 -0
- package/web/term.js +1 -1
- package/web/video.html +1 -0
- package/web/video.js +50 -50
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { test, expect, beforeEach } from "bun:test";
|
|
2
|
+
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { move, awaitBoard, childDied, childQuiet, QUIET_MS, finish, markWoken, requestOpen, readHandoff, request, REQUEST_MS, started, wakeAction, wakeLine, type Handoff } from "./handoff.ts";
|
|
6
|
+
import { agentLine } from "./workers.ts";
|
|
7
|
+
|
|
8
|
+
const ID = "2026-11-01-x";
|
|
9
|
+
let state: string;
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
state = mkdtempSync(join(tmpdir(), "hand-"));
|
|
12
|
+
mkdirSync(join(state, "runs", ID), { recursive: true });
|
|
13
|
+
writeFileSync(join(state, "runs", ID, "state.json"), "{}");
|
|
14
|
+
});
|
|
15
|
+
const idle = { alive: true, busy: false, asking: false, typedAt: 0 };
|
|
16
|
+
const ended = (o: Partial<Handoff> = {}): Handoff => ({ status: "done", parent_terminal: "sh-1", at: 0, ...o });
|
|
17
|
+
|
|
18
|
+
test("G-04 outside a board terminal nothing is requested", () => {
|
|
19
|
+
expect(request(state, ID, undefined)).toBeNull();
|
|
20
|
+
expect(readHandoff(state, ID)).toBeNull();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("G-04 no answer declines, so a late board opens nothing", async () => {
|
|
24
|
+
request(state, ID, "sh-1");
|
|
25
|
+
expect(await awaitBoard(state, ID, 30, 5)).toBe(false);
|
|
26
|
+
const h = readHandoff(state, ID)!;
|
|
27
|
+
expect(h.status).toBe("declined");
|
|
28
|
+
expect(requestOpen(h, h.at)).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("G-04 an old request is not acted on", () => {
|
|
32
|
+
expect(requestOpen(ended({ status: "requested", at: 0 }), REQUEST_MS + 1)).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("the board's answer ends the wait", async () => {
|
|
36
|
+
request(state, ID, "sh-1"); started(state, ID, "sh-2");
|
|
37
|
+
expect(await awaitBoard(state, ID, 30, 5)).toBe(true);
|
|
38
|
+
expect(readHandoff(state, ID)!.child_terminal).toBe("sh-2");
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("only a running handoff takes a result", () => {
|
|
42
|
+
expect(finish(state, ID, "done")).toContain("no handoff");
|
|
43
|
+
request(state, ID, "sh-1");
|
|
44
|
+
expect(finish(state, ID, "done")).toContain("requested");
|
|
45
|
+
started(state, ID, "sh-2");
|
|
46
|
+
expect(finish(state, ID, "blocked", "why")).toBeNull();
|
|
47
|
+
expect(finish(state, ID, "done")).not.toBeNull();
|
|
48
|
+
expect(readHandoff(state, ID)).toMatchObject({ status: "blocked", reason: "why" });
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("G-05 a wake is held while busy, asking or typing, then sent once", () => {
|
|
52
|
+
expect(wakeAction(ended(), { ...idle, busy: true }, 1e6)).toBe("wait");
|
|
53
|
+
expect(wakeAction(ended(), { ...idle, asking: true }, 1e6)).toBe("wait");
|
|
54
|
+
expect(wakeAction(ended(), { ...idle, typedAt: 1e6 - 1000 }, 1e6)).toBe("wait");
|
|
55
|
+
expect(wakeAction(ended(), idle, 1e6)).toBe("send");
|
|
56
|
+
expect(wakeAction(ended({ woken: true }), idle, 1e6)).toBe("none");
|
|
57
|
+
expect(wakeAction(ended({ status: "running" }), idle, 1e6)).toBe("none");
|
|
58
|
+
request(state, ID, "sh-1"); started(state, ID, "sh-2"); finish(state, ID, "done"); markWoken(state, ID);
|
|
59
|
+
expect(wakeAction(readHandoff(state, ID), idle, 1e6)).toBe("none");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("G-06 a dead child fails, and a dead parent is reported, not typed at", () => {
|
|
63
|
+
expect(childDied(ended({ status: "running" }), false)).toBe(true);
|
|
64
|
+
expect(childDied(ended({ status: "running" }), true)).toBe(false);
|
|
65
|
+
expect(childDied(ended(), false)).toBe(false);
|
|
66
|
+
expect(wakeAction(ended({ status: "failed" }), idle, 1e6)).toBe("send");
|
|
67
|
+
expect(wakeAction(ended(), null, 1e6)).toBe("gone");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("G-04 a second agent that already finished is not mistaken for no board", async () => {
|
|
71
|
+
request(state, ID, "sh-1"); started(state, ID, "sh-2"); finish(state, ID, "done");
|
|
72
|
+
expect(await awaitBoard(state, ID, 30, 5)).toBe(true);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("G-05 the wake line reaches a real terminal, submitted", async () => {
|
|
76
|
+
const { startPty, submitPty, ptyTail } = await import("./pty.ts");
|
|
77
|
+
if (process.platform === "win32") return;
|
|
78
|
+
startPty("hand-wake", ["sh", "-c", "timeout 1 cat"], state);
|
|
79
|
+
submitPty("hand-wake", wakeLine(ID, ended()), 20);
|
|
80
|
+
await Bun.sleep(200);
|
|
81
|
+
expect(ptyTail("hand-wake")).toContain(`kaizen run ${ID}: the worktree agent ended (done)`);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("G-07 the second agent never takes bypass", () => {
|
|
85
|
+
const project = join(state, "p");
|
|
86
|
+
mkdirSync(join(project, ".kaizen", "runs", ID), { recursive: true });
|
|
87
|
+
writeFileSync(join(project, ".kaizen", "runs", ID, "state.json"), JSON.stringify({ agent: "claude" }));
|
|
88
|
+
const l = agentLine(project, ID, "hi", "own");
|
|
89
|
+
if (typeof l !== "string") { expect(l.bypass).toBe(false); expect(l.argv.join(" ")).not.toContain("dangerously"); }
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("a silent child goes quiet, wakes the parent, and can still report", () => {
|
|
93
|
+
const pty = { alive: true, busy: false, asking: false, lastOut: 0 };
|
|
94
|
+
expect(childQuiet(ended({ status: "running" }), pty, QUIET_MS + 1)).toBe(true);
|
|
95
|
+
expect(childQuiet(ended({ status: "running" }), { ...pty, asking: true }, QUIET_MS + 1)).toBe(false);
|
|
96
|
+
expect(childQuiet(ended({ status: "running" }), pty, QUIET_MS - 1)).toBe(false);
|
|
97
|
+
expect(wakeAction(ended({ status: "quiet" }), idle, 1e6)).toBe("send");
|
|
98
|
+
request(state, ID, "sh-1"); started(state, ID, "sh-2");
|
|
99
|
+
move(state, ID, ["running"], { status: "quiet", woken: true });
|
|
100
|
+
expect(finish(state, ID, "done")).toBeNull();
|
|
101
|
+
expect(wakeAction(readHandoff(state, ID), idle, 1e6)).toBe("send");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("cold 1: a second start never replaces a live agent", () => {
|
|
105
|
+
request(state, ID, "sh-1"); started(state, ID, "sh-2");
|
|
106
|
+
expect(request(state, ID, "sh-9")).toMatchObject({ status: "running", child_terminal: "sh-2" });
|
|
107
|
+
finish(state, ID, "blocked", "x");
|
|
108
|
+
expect(request(state, ID, "sh-9")).toMatchObject({ status: "requested", parent_terminal: "sh-9" });
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("cold 2: claim and decline cannot both win", async () => {
|
|
112
|
+
request(state, ID, "sh-1");
|
|
113
|
+
const claim = setTimeout(() => started(state, ID, "sh-2"), 5);
|
|
114
|
+
await Bun.sleep(10); clearTimeout(claim);
|
|
115
|
+
expect(await awaitBoard(state, ID, 0)).toBe(true);
|
|
116
|
+
writeFileSync(join(state, "runs", ID, "handoff.json"), JSON.stringify({ status: "declined", parent_terminal: "sh-1", at: 0 }));
|
|
117
|
+
expect(started(state, ID, "sh-3")).toBe(false);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("cold 3: a stale board read cannot overwrite done", () => {
|
|
121
|
+
request(state, ID, "sh-1"); started(state, ID, "sh-2"); finish(state, ID, "done");
|
|
122
|
+
expect(move(state, ID, ["running", "quiet"], { status: "failed" })).toBe(false);
|
|
123
|
+
expect(readHandoff(state, ID)!.status).toBe("done");
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("cold 5: a quiet child that dies counts as dead", () => {
|
|
127
|
+
expect(childDied(ended({ status: "quiet" }), false)).toBe(true);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("cold 7: a draft holds the wake", () => {
|
|
131
|
+
expect(wakeAction(ended(), { ...idle, draft: true, typedAt: 1e6 - 6000 }, 1e6)).toBe("held");
|
|
132
|
+
expect(wakeAction(ended(), { ...idle, draft: true, typedAt: 1e9 - 11 * 60_000 }, 1e9)).toBe("send");
|
|
133
|
+
});
|
package/cli/handoff.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// A worktree run handed to a second agent: the first agent stays with the user, the board opens
|
|
2
|
+
// the second in the worktree, and wakes the first when it ends. Its own file, not state.json,
|
|
3
|
+
// because agents hand-write state.json. The wake-up carries nothing: this file and the run's
|
|
4
|
+
// reports are the message, so a lost one costs only the nudge.
|
|
5
|
+
import { closeSync, existsSync, openSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
export type Status = "requested" | "running" | "done" | "blocked" | "failed" | "declined" | "quiet";
|
|
9
|
+
export type Handoff = { status: Status; parent_terminal: string; child_terminal?: string; reason?: string; at: number; woken?: boolean };
|
|
10
|
+
export const REQUEST_MS = 10_000;
|
|
11
|
+
const QUIET_TYPING_MS = 5_000;
|
|
12
|
+
const DRAFT_MS = 10 * 60_000; // a draft untouched this long no longer holds the wake
|
|
13
|
+
|
|
14
|
+
const file = (state: string, id: string) => join(state, "runs", id, "handoff.json");
|
|
15
|
+
|
|
16
|
+
export function readHandoff(state: string, id: string): Handoff | null {
|
|
17
|
+
try { return JSON.parse(readFileSync(file(state, id), "utf8")); } catch { return null; }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function writeHandoff(state: string, id: string, h: Handoff) {
|
|
21
|
+
const f = file(state, id), tmp = `${f}.${process.pid}.tmp`;
|
|
22
|
+
writeFileSync(tmp, JSON.stringify(h, null, 2));
|
|
23
|
+
renameSync(tmp, f);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// The CLI and the board both move this file, so every move holds a lock across its read and
|
|
27
|
+
// write and names the statuses it may leave: a stale reader changes nothing.
|
|
28
|
+
function locked<T>(state: string, id: string, fn: () => T): T {
|
|
29
|
+
const lock = `${file(state, id)}.lock`;
|
|
30
|
+
for (let i = 0; ; i++) {
|
|
31
|
+
try { closeSync(openSync(lock, "wx")); break; } catch (err) {
|
|
32
|
+
try { if (Date.now() - statSync(lock).mtimeMs > 5000) { rmSync(lock, { force: true }); continue; } } catch { /* just released */ }
|
|
33
|
+
if (i > 500) throw err;
|
|
34
|
+
Bun.sleepSync(2);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
try { return fn(); } finally { rmSync(lock, { force: true }); }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Moves the handoff only if it is in one of `from`. True when it moved. */
|
|
41
|
+
export function move(state: string, id: string, from: Status[], set: Partial<Handoff>): boolean {
|
|
42
|
+
return locked(state, id, () => {
|
|
43
|
+
const h = readHandoff(state, id);
|
|
44
|
+
if (!h || !from.includes(h.status)) return false;
|
|
45
|
+
writeHandoff(state, id, { ...h, ...set, at: Date.now() });
|
|
46
|
+
return true;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Asks the board for the second agent. Null when this is no board terminal: build here. An
|
|
52
|
+
* agent already open, or a request still live, is returned as it is: never a second one.
|
|
53
|
+
*/
|
|
54
|
+
export function request(state: string, id: string, term: string | undefined): Handoff | null {
|
|
55
|
+
if (!term || !existsSync(join(state, "runs", id, "state.json"))) return null;
|
|
56
|
+
return locked(state, id, () => {
|
|
57
|
+
const was = readHandoff(state, id);
|
|
58
|
+
if (was && (was.status === "running" || was.status === "quiet" || requestOpen(was, Date.now()))) return was;
|
|
59
|
+
const h: Handoff = { status: "requested", parent_terminal: term, at: Date.now() };
|
|
60
|
+
writeHandoff(state, id, h);
|
|
61
|
+
return h;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Waits for the board to answer. No answer declines the request, so a board that comes up
|
|
67
|
+
* later cannot open a second agent beside the one now building here. The decline can lose to
|
|
68
|
+
* the board's claim: then the agent is open, and this says so.
|
|
69
|
+
*/
|
|
70
|
+
export async function awaitBoard(state: string, id: string, ms = REQUEST_MS, step = 200): Promise<boolean> {
|
|
71
|
+
const took = (s?: Status) => s === "running" || s === "quiet" || s === "done" || s === "blocked" || s === "failed";
|
|
72
|
+
const end = Date.now() + ms;
|
|
73
|
+
while (Date.now() < end) {
|
|
74
|
+
const s = readHandoff(state, id)?.status;
|
|
75
|
+
// A quick second agent may be finished by the time this looks: it still took the run.
|
|
76
|
+
if (took(s)) return true;
|
|
77
|
+
if (s !== "requested") return false;
|
|
78
|
+
await Bun.sleep(step);
|
|
79
|
+
}
|
|
80
|
+
if (move(state, id, ["requested"], { status: "declined", reason: "no board answered" })) return false;
|
|
81
|
+
return took(readHandoff(state, id)?.status);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The board's claim, made before anything launches: it loses to a decline, so no stray agent. */
|
|
85
|
+
export const started = (state: string, id: string, key: string) => move(state, id, ["requested"], { status: "running", child_terminal: key });
|
|
86
|
+
export const refused = (state: string, id: string, why: string) => move(state, id, ["requested"], { status: "declined", reason: why });
|
|
87
|
+
|
|
88
|
+
/** The second agent's last word. A repeat cannot rewrite the result; a report after `quiet` wakes the first agent again, since it is news. */
|
|
89
|
+
export function finish(state: string, id: string, status: "done" | "blocked", reason?: string): string | null {
|
|
90
|
+
const h = readHandoff(state, id);
|
|
91
|
+
if (!h) return "this run has no handoff";
|
|
92
|
+
return move(state, id, ["running", "quiet"], { status, reason, woken: false }) ? null : `the handoff is ${readHandoff(state, id)!.status}, not running`;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** A request the board may still act on. An old one was declined or abandoned by its asker. */
|
|
96
|
+
export const requestOpen = (h: Handoff | null, now: number) => h?.status === "requested" && now - h.at < REQUEST_MS;
|
|
97
|
+
|
|
98
|
+
/** A live second agent silent this long, not working or asking: it may have ended without saying so. */
|
|
99
|
+
export const QUIET_MS = 20 * 60_000;
|
|
100
|
+
export const childQuiet = (h: Handoff | null, pty: { alive: boolean; busy: boolean; asking: boolean; lastOut?: number } | null, now: number) =>
|
|
101
|
+
h?.status === "running" && !!pty?.alive && !pty.busy && !pty.asking && now - (pty.lastOut ?? now) > QUIET_MS;
|
|
102
|
+
|
|
103
|
+
/** The child terminal ended without a report. */
|
|
104
|
+
export const childDied = (h: Handoff | null, alive: boolean) => (h?.status === "running" || h?.status === "quiet") && !alive;
|
|
105
|
+
|
|
106
|
+
type Term = { alive: boolean; busy: boolean; asking: boolean; typedAt: number; draft?: boolean } | null;
|
|
107
|
+
export type Wake = "wait" | "send" | "gone" | "held" | "none";
|
|
108
|
+
|
|
109
|
+
/** What to do about the first agent: send only into a live, idle terminal nobody is typing in. */
|
|
110
|
+
export function wakeAction(h: Handoff | null, parent: Term, now: number): Wake {
|
|
111
|
+
if (!h || h.woken || !["done", "blocked", "failed", "quiet"].includes(h.status)) return "none";
|
|
112
|
+
if (!parent?.alive) return "gone";
|
|
113
|
+
// Unsent text in its input box: a wake would submit that draft. The card speaks instead.
|
|
114
|
+
if (parent.draft && now - parent.typedAt < DRAFT_MS) return "held";
|
|
115
|
+
return parent.busy || parent.asking || now - parent.typedAt < QUIET_TYPING_MS ? "wait" : "send";
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const wakeLine = (id: string, h: Handoff) =>
|
|
119
|
+
`kaizen run ${id}: the worktree agent ended (${h.status}). Read .kaizen/runs/${id}/handoff.json, 03-impl.md and 04-review.md, then report to the user.`;
|
|
120
|
+
|
|
121
|
+
/** Marked before sending: a board restart between the two loses a nudge, never repeats one. */
|
|
122
|
+
export const markWoken = (state: string, id: string) => move(state, id, ["done", "blocked", "failed", "quiet"], { woken: true });
|
package/cli/install.ts
CHANGED
|
@@ -562,17 +562,40 @@ if (wantSettings) {
|
|
|
562
562
|
if (args.has("worktree")) {
|
|
563
563
|
const [, verb = "", id = ""] = Bun.argv.slice(2).filter((a) => !a.startsWith("-"));
|
|
564
564
|
const { locate } = await import("./state.ts");
|
|
565
|
-
const { startWorktree,
|
|
565
|
+
const { startWorktree, landRun, adoptWorkspace, linkTerm } = await import("./worktree.ts");
|
|
566
566
|
const state = locate();
|
|
567
567
|
if (!state || (verb !== "start" && verb !== "land")) { console.log("\n usage: kaizen worktree start|land <run-id>, in a project with .kaizen\n"); process.exit(1); }
|
|
568
568
|
const project = dirname(state);
|
|
569
569
|
// Started inside another workspace of the project (a board worktree): the run is placed there.
|
|
570
|
-
const r = verb === "start" ? adoptWorkspace(project, id, process.cwd()) ?? startWorktree(project, id) :
|
|
570
|
+
const r = verb === "start" ? adoptWorkspace(project, id, process.cwd()) ?? startWorktree(project, id) : landRun(project, id, process.env.KAIZEN_TERM);
|
|
571
571
|
if (r.ok && verb === "start") linkTerm(state, process.env.KAIZEN_TERM, id);
|
|
572
|
+
if ("warn" in r && r.warn) console.log(`\n warning: ${r.warn}`);
|
|
572
573
|
console.log(r.ok ? `\n ${verb === "start" ? `worktree ready: ${(r as { path: string }).path}` : `landed by ${(r as { how: string }).how}`}\n` : `\n ${r.why}\n`);
|
|
573
574
|
process.exit(r.ok ? 0 : 1);
|
|
574
575
|
}
|
|
575
576
|
|
|
577
|
+
// A worktree run's second agent. `start` exits 3 for "build here": no board, or none answered.
|
|
578
|
+
if (args.has("handoff")) {
|
|
579
|
+
const [, verb = "", first = ""] = Bun.argv.slice(2).filter((a) => !a.startsWith("-"));
|
|
580
|
+
const { projectFromWorktree } = await import("./worktree.ts");
|
|
581
|
+
const h = await import("./handoff.ts");
|
|
582
|
+
// A worktree holds committed .kaizen spec copies, never the run: always the project's own.
|
|
583
|
+
const state = join(projectFromWorktree(process.cwd()), ".kaizen");
|
|
584
|
+
const usage = () => { console.log('\n usage: kaizen handoff start <run-id> | done | blocked "<reason>"\n'); process.exit(1); };
|
|
585
|
+
if (verb === "start") {
|
|
586
|
+
if (!first) usage();
|
|
587
|
+
const asked = h.request(state, first, process.env.KAIZEN_TERM);
|
|
588
|
+
if (!asked || !(await h.awaitBoard(state, first))) { console.log("\n no board terminal to hand to: build here, in the worktree\n"); process.exit(3); }
|
|
589
|
+
console.log("\n handed off: end your turn, the board wakes you when it ends\n");
|
|
590
|
+
process.exit(0);
|
|
591
|
+
}
|
|
592
|
+
const id = process.env.KAIZEN_RUN_ID;
|
|
593
|
+
if ((verb !== "done" && verb !== "blocked") || !id) usage();
|
|
594
|
+
const why = h.finish(state, id!, verb as "done" | "blocked", verb === "blocked" ? first : undefined);
|
|
595
|
+
console.log(why ? `\n ${why}\n` : `\n handoff ${verb}\n`);
|
|
596
|
+
process.exit(why ? 1 : 0);
|
|
597
|
+
}
|
|
598
|
+
|
|
576
599
|
// Deleting runs is a job for code with a test, not for an agent with `rm`. Lists by
|
|
577
600
|
// default; --yes is what deletes.
|
|
578
601
|
if (args.has("prune")) {
|
package/cli/pty.ts
CHANGED
|
@@ -11,7 +11,7 @@ const WIN = process.platform === "win32";
|
|
|
11
11
|
// Windows PowerShell, which every Windows has, both hosts the console and runs the agent in it.
|
|
12
12
|
const winPs = () => join(process.env.SystemRoot ?? "C:\\Windows", "System32", "WindowsPowerShell", "v1.0", "powershell.exe");
|
|
13
13
|
|
|
14
|
-
type Pty = { lastOut?: number; proc: Bun.Subprocess; out: string[]; size: number; listeners: Set<(d: string | null) => void>; exit: number | null; asking: boolean; busy: boolean; quiet?: Timer; total: number; since: number; onChange?: () => void; tmux?: string; last: string; lastAt: number; typedAt: number; born: number };
|
|
14
|
+
type Pty = { lastOut?: number; proc: Bun.Subprocess; out: string[]; size: number; listeners: Set<(d: string | null) => void>; exit: number | null; asking: boolean; busy: boolean; quiet?: Timer; total: number; since: number; onChange?: () => void; tmux?: string; last: string; lastAt: number; typedAt: number; born: number; draft?: boolean };
|
|
15
15
|
|
|
16
16
|
// What a page attaching late gets replayed. A full-screen tool redraws often, so the
|
|
17
17
|
// tail of its output is enough to rebuild the screen.
|
|
@@ -198,6 +198,9 @@ export function writePty(key: string, d: string) {
|
|
|
198
198
|
if (d.replace(REPORT, "")) {
|
|
199
199
|
p.since = p.total;
|
|
200
200
|
p.typedAt = Date.now();
|
|
201
|
+
// Text typed and not yet submitted. Keys and escape sequences leave no text; Enter, Esc and Ctrl-C end it.
|
|
202
|
+
if (/[\r\n\x03]/.test(d) || d === "\x1b") p.draft = false;
|
|
203
|
+
else if (!d.startsWith("\x1b") && /[^\x00-\x1f\x7f]/.test(d)) p.draft = true;
|
|
201
204
|
if (p.asking) { p.asking = false; p.onChange?.(); }
|
|
202
205
|
}
|
|
203
206
|
send(p, d);
|
|
@@ -230,6 +233,17 @@ export function exitPty(key: string, grace = 5000): boolean {
|
|
|
230
233
|
return true;
|
|
231
234
|
}
|
|
232
235
|
|
|
236
|
+
// The resource monitor's Kill: at once, with no /exit first, which a plain shell would only
|
|
237
|
+
// print an error for. The whole group goes (see the note above); a tmux agent with its session.
|
|
238
|
+
export function killPty(key: string): boolean {
|
|
239
|
+
const p = ptys.get(key);
|
|
240
|
+
if (!p || p.exit !== null) return false;
|
|
241
|
+
if (p.tmux) killTmux(p.tmux);
|
|
242
|
+
if (WIN) p.proc.kill();
|
|
243
|
+
else { try { process.kill(-p.proc.pid, "SIGKILL"); } catch { p.proc.kill(); } }
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
|
|
233
247
|
// The board stopping with a tmux-kept agent: only its client ends, the agent keeps running
|
|
234
248
|
// in kaizen's tmux server, and the next board attaches to it (attachCmd).
|
|
235
249
|
export function detachPty(key: string): boolean {
|
|
@@ -285,7 +299,7 @@ export const ptyKeys = () => [...ptys.keys()];
|
|
|
285
299
|
|
|
286
300
|
export const ptyState = (key: string) => {
|
|
287
301
|
const p = ptys.get(key);
|
|
288
|
-
return p ? { pid: p.proc.pid, alive: p.exit === null, exit: p.exit, asking: p.asking, busy: p.busy, total: p.total, last: p.last, at: p.lastAt, born: p.born, lastOut: p.lastOut } : null;
|
|
302
|
+
return p ? { pid: p.proc.pid, alive: p.exit === null, exit: p.exit, asking: p.asking, busy: p.busy, total: p.total, last: p.last, at: p.lastAt, born: p.born, lastOut: p.lastOut, typedAt: p.typedAt, draft: !!p.draft } : null;
|
|
289
303
|
};
|
|
290
304
|
|
|
291
305
|
// The environment the user's login shell builds: PATH and anything exported in their
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { expect, it } from "bun:test";
|
|
2
|
+
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { folderSize, measure, parsePs, systemMemory, treeRss } from "./resources.ts";
|
|
6
|
+
|
|
7
|
+
it("reads available memory, so page cache is not shown as used", () => {
|
|
8
|
+
const m = systemMemory("MemTotal: 1 kB\nMemAvailable: 1024 kB\n");
|
|
9
|
+
expect(m.total - m.used).toBe(1024 * 1024);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("adds a process tree once, whichever roots reach it", () => {
|
|
13
|
+
const t = parsePs(" 1 0 10\n 2 1 20\n 3 2 30\n 4 1 40\njunk\n 9 0 90\n");
|
|
14
|
+
expect(t).toHaveLength(5);
|
|
15
|
+
expect(treeRss(t, [2])).toBe(50 * 1024);
|
|
16
|
+
expect(treeRss(t, [1, 2, 3])).toBe(100 * 1024);
|
|
17
|
+
expect(treeRss(t, [777])).toBe(0);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const scratch = () => mkdtempSync(join(tmpdir(), "kz-res-"));
|
|
21
|
+
|
|
22
|
+
it("does not follow symlinks", async () => {
|
|
23
|
+
const d = scratch();
|
|
24
|
+
try {
|
|
25
|
+
mkdirSync(join(d, "a")); mkdirSync(join(d, "big"));
|
|
26
|
+
writeFileSync(join(d, "big", "f"), Buffer.alloc(1 << 20));
|
|
27
|
+
const before = await folderSize(join(d, "a"));
|
|
28
|
+
symlinkSync(join(d, "big"), join(d, "a", "link"));
|
|
29
|
+
expect(await folderSize(join(d, "a"))).toBeLessThan(before + 65536);
|
|
30
|
+
expect(await folderSize(join(d, "big"))).toBeGreaterThanOrEqual(1 << 20);
|
|
31
|
+
} finally { rmSync(d, { recursive: true, force: true }); }
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("counts a real folder once and a nested root only in its own row", async () => {
|
|
35
|
+
const d = scratch();
|
|
36
|
+
try {
|
|
37
|
+
mkdirSync(join(d, "inst", ".kaizen"), { recursive: true });
|
|
38
|
+
writeFileSync(join(d, "inst", ".kaizen", "f"), Buffer.alloc(1 << 20));
|
|
39
|
+
symlinkSync(join(d, "inst", ".kaizen"), join(d, "alias"));
|
|
40
|
+
const r = await measure([
|
|
41
|
+
{ name: "inst", dir: "", path: join(d, "inst"), kind: "install" },
|
|
42
|
+
{ name: "p", dir: "x", path: join(d, "inst", ".kaizen"), kind: "project" },
|
|
43
|
+
{ name: "again", dir: "y", path: join(d, "alias"), kind: "project" },
|
|
44
|
+
{ name: "gone", dir: "z", path: join(d, "missing"), kind: "project" },
|
|
45
|
+
]);
|
|
46
|
+
expect(r.rows.map((x) => x.name)).toEqual(["p", "inst"]);
|
|
47
|
+
expect(r.rows[1].bytes).toBeLessThan(1 << 20);
|
|
48
|
+
expect(r.total).toBe(r.rows[0].bytes + r.rows[1].bytes);
|
|
49
|
+
} finally { rmSync(d, { recursive: true, force: true }); }
|
|
50
|
+
});
|
package/cli/resources.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// What the footer's resource monitor reports: memory for the machine, for kaizen and per
|
|
2
|
+
// terminal, and the disk kaizen takes. Apart from cli/web.ts so the parsing and the walk
|
|
3
|
+
// can be tested without a board. Read only: nothing here kills or deletes.
|
|
4
|
+
import { freemem, totalmem } from "node:os";
|
|
5
|
+
import { readFileSync, realpathSync } from "node:fs";
|
|
6
|
+
import { lstat, readdir } from "node:fs/promises";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
|
|
9
|
+
export function systemMemory(meminfo?: string) {
|
|
10
|
+
const total = totalmem();
|
|
11
|
+
let free = freemem();
|
|
12
|
+
// freemem() on Linux leaves out the page cache, which would read as a nearly full machine.
|
|
13
|
+
try {
|
|
14
|
+
const text = meminfo ?? readFileSync("/proc/meminfo", "utf8");
|
|
15
|
+
const kb = /^MemAvailable:\s+(\d+) kB/m.exec(text);
|
|
16
|
+
if (kb) free = Number(kb[1]) * 1024;
|
|
17
|
+
} catch { /* not Linux */ }
|
|
18
|
+
return { total, used: Math.max(0, total - free) };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type Proc = { pid: number; ppid: number; rss: number };
|
|
22
|
+
|
|
23
|
+
export function parsePs(out: string): Proc[] {
|
|
24
|
+
return out.split("\n").flatMap((l) => {
|
|
25
|
+
const m = /^\s*(\d+)\s+(\d+)\s+(\d+)\s*$/.exec(l);
|
|
26
|
+
return m ? [{ pid: Number(m[1]), ppid: Number(m[2]), rss: Number(m[3]) * 1024 }] : [];
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Every process with its parent and memory; null where there is no ps (Windows), so a
|
|
31
|
+
// caller shows "not known" rather than zero.
|
|
32
|
+
export async function processTable(): Promise<Proc[] | null> {
|
|
33
|
+
if (process.platform === "win32") return null;
|
|
34
|
+
try {
|
|
35
|
+
const p = Bun.spawn(["ps", "-axo", "pid=,ppid=,rss="], { stdout: "pipe", stderr: "ignore", windowsHide: true });
|
|
36
|
+
const out = await new Response(p.stdout).text();
|
|
37
|
+
return (await p.exited) === 0 ? parsePs(out) : null;
|
|
38
|
+
} catch { return null; }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Memory of these processes and all their descendants, each counted once however many
|
|
42
|
+
// roots reach it. Resident sizes add shared pages more than once, so it runs a little high.
|
|
43
|
+
export function treeRss(table: Proc[], roots: number[]): number {
|
|
44
|
+
const kids = new Map<number, Proc[]>(), byPid = new Map(table.map((p) => [p.pid, p]));
|
|
45
|
+
for (const p of table) (kids.get(p.ppid) ?? kids.set(p.ppid, []).get(p.ppid)!).push(p);
|
|
46
|
+
const seen = new Set<number>(), todo = [...roots];
|
|
47
|
+
let sum = 0;
|
|
48
|
+
while (todo.length) {
|
|
49
|
+
const pid = todo.pop()!;
|
|
50
|
+
if (seen.has(pid)) continue;
|
|
51
|
+
seen.add(pid);
|
|
52
|
+
sum += byPid.get(pid)?.rss ?? 0;
|
|
53
|
+
for (const k of kids.get(pid) ?? []) todo.push(k.pid);
|
|
54
|
+
}
|
|
55
|
+
return sum;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const real = (p: string) => { try { return realpathSync(p); } catch { return null; } };
|
|
59
|
+
|
|
60
|
+
// Bytes on disk under a folder. lstat, so a symlink counts as itself and is never followed;
|
|
61
|
+
// a folder in `skip` (real paths) belongs to another row. Every await hands the one thread
|
|
62
|
+
// back, so a large tree slows the scan and not the board.
|
|
63
|
+
export async function folderSize(path: string, skip: Set<string> = new Set()): Promise<number> {
|
|
64
|
+
let sum = 0;
|
|
65
|
+
const todo = [path];
|
|
66
|
+
while (todo.length) {
|
|
67
|
+
const dir = todo.pop()!;
|
|
68
|
+
if (skip.has(dir)) continue;
|
|
69
|
+
let names: string[];
|
|
70
|
+
try { names = await readdir(dir); } catch { continue; }
|
|
71
|
+
for (let i = 0; i < names.length; i += 64) {
|
|
72
|
+
const stats = await Promise.all(names.slice(i, i + 64).map(async (n) => {
|
|
73
|
+
try { const s = await lstat(join(dir, n)); return { p: join(dir, n), s }; } catch { return null; }
|
|
74
|
+
}));
|
|
75
|
+
for (const e of stats) {
|
|
76
|
+
if (!e) continue;
|
|
77
|
+
sum += e.s.blocks ? e.s.blocks * 512 : e.s.size;
|
|
78
|
+
if (e.s.isDirectory()) todo.push(e.p);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return sum;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export type Root = { name: string; dir: string; path: string; kind: "home" | "install" | "project" };
|
|
86
|
+
export type StorageRow = Omit<Root, "path"> & { bytes: number };
|
|
87
|
+
|
|
88
|
+
// One row per real folder, and no folder counted inside another row: a checkout that is
|
|
89
|
+
// both the install and a registered project gives its .kaizen to the project row.
|
|
90
|
+
export async function measure(roots: Root[]): Promise<{ total: number; rows: StorageRow[] }> {
|
|
91
|
+
const seen = new Map<string, Root>();
|
|
92
|
+
for (const r of roots) { const p = real(r.path); if (p && !seen.has(p)) seen.set(p, r); }
|
|
93
|
+
const rows: StorageRow[] = [];
|
|
94
|
+
for (const [p, r] of seen) {
|
|
95
|
+
const skip = new Set([...seen.keys()].filter((o) => o !== p));
|
|
96
|
+
rows.push({ name: r.name, dir: r.dir, kind: r.kind, bytes: await folderSize(p, skip) });
|
|
97
|
+
}
|
|
98
|
+
rows.sort((a, b) => b.bytes - a.bytes);
|
|
99
|
+
return { total: rows.reduce((n, r) => n + r.bytes, 0), rows };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const FRESH = 5 * 60_000;
|
|
103
|
+
let result: { total: number; rows: StorageRow[]; at: number } | null = null;
|
|
104
|
+
let scanning = false;
|
|
105
|
+
|
|
106
|
+
// Never waits: starts a scan when the last is old (or on refresh) and answers with what it has.
|
|
107
|
+
export function storage(roots: () => Root[], refresh = false) {
|
|
108
|
+
if (!scanning && (refresh || !result || Date.now() - result.at > FRESH)) {
|
|
109
|
+
scanning = true;
|
|
110
|
+
void measure(roots()).then((r) => { result = { ...r, at: Date.now() }; }).catch(() => {}).finally(() => { scanning = false; });
|
|
111
|
+
}
|
|
112
|
+
return { ...(result ?? { total: 0, rows: [], at: 0 }), scanning };
|
|
113
|
+
}
|
package/cli/restart.test.ts
CHANGED
|
@@ -5,8 +5,9 @@ import { test, expect } from "bun:test";
|
|
|
5
5
|
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, realpathSync } from "node:fs";
|
|
6
6
|
import { tmpdir } from "node:os";
|
|
7
7
|
import { join } from "node:path";
|
|
8
|
-
import { startPty, exitPty, ptyState, tmuxOk, tmuxName, tmuxAlive, killTmux, detachPty, attachPty, panePid } from "./pty.ts";
|
|
8
|
+
import { startPty, exitPty, killPty, ptyState, tmuxOk, tmuxName, tmuxAlive, killTmux, detachPty, attachPty, panePid } from "./pty.ts";
|
|
9
9
|
import { keepAlive } from "./state.ts";
|
|
10
|
+
import { processTable, treeRss } from "./resources.ts";
|
|
10
11
|
|
|
11
12
|
const root = realpathSync(mkdtempSync(join(tmpdir(), "kaizen-restart-")));
|
|
12
13
|
const posix = process.platform !== "win32";
|
|
@@ -193,3 +194,29 @@ console.log("up");`);
|
|
|
193
194
|
expect(version).toBe(JSON.parse(readFileSync(join(import.meta.dir, "..", "package.json"), "utf8")).version);
|
|
194
195
|
} finally { fresh.kill("SIGKILL"); old.kill("SIGKILL"); }
|
|
195
196
|
}, 20000);
|
|
197
|
+
|
|
198
|
+
test("killPty ends the terminal's whole group at once, and only that terminal", async () => {
|
|
199
|
+
if (!posix) return;
|
|
200
|
+
const file = (n: string) => join(root, n + ".pid");
|
|
201
|
+
for (const k of ["kill-a", "kill-b"]) startPty(k, ["sh", "-c", `sleep 300 & echo $! > ${file(k)}; wait`], root);
|
|
202
|
+
for (let i = 0; i < 100 && !(existsSync(file("kill-a")) && existsSync(file("kill-b"))); i++) await Bun.sleep(20);
|
|
203
|
+
const a = Number(readFileSync(file("kill-a"), "utf8")), b = Number(readFileSync(file("kill-b"), "utf8"));
|
|
204
|
+
expect(killPty("kill-a")).toBe(true);
|
|
205
|
+
expect(await gone(a)).toBe(true);
|
|
206
|
+
expect(() => process.kill(b, 0)).not.toThrow();
|
|
207
|
+
expect(killPty("no-such")).toBe(false);
|
|
208
|
+
killPty("kill-b");
|
|
209
|
+
expect(await gone(b)).toBe(true);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test.skipIf(!posix || !tmuxOk())("a tmux-kept terminal is measured from its pane and killed with its session", async () => {
|
|
213
|
+
const name = tmuxName("res-" + process.pid);
|
|
214
|
+
startPty("res-tmux", ["sleep", "60"], root, undefined, undefined, { tmux: name });
|
|
215
|
+
let pane: number | null = null;
|
|
216
|
+
for (let i = 0; i < 100 && !(pane = panePid(name)); i++) await Bun.sleep(30);
|
|
217
|
+
expect(pane).toBeGreaterThan(0);
|
|
218
|
+
expect(treeRss((await processTable())!, [pane!])).toBeGreaterThan(0);
|
|
219
|
+
expect(killPty("res-tmux")).toBe(true);
|
|
220
|
+
for (let i = 0; i < 100 && tmuxAlive(name); i++) await Bun.sleep(30);
|
|
221
|
+
expect(tmuxAlive(name)).toBe(false);
|
|
222
|
+
});
|
package/cli/settings.ts
CHANGED
|
@@ -122,6 +122,9 @@ const SETTINGS: Setting[] = [
|
|
|
122
122
|
{ key: "auto_fix.max_iterations", label: "Max fix rounds", group: "Auto-fix", values: ["1", "2", "3", "4", "5"], help: "", when: ["auto_fix.enabled", "true"] },
|
|
123
123
|
{ key: "git.auto_commit", label: "Commit when done", group: "Git", values: ["false", "true"], help: "Never pushes" },
|
|
124
124
|
{ key: "git.worktree_per_run", label: "Branch and folder per run", group: "Git", values: ["ask", "true", "false"], help: "" },
|
|
125
|
+
{ key: "git.generated", label: "Generated files", group: "Git", values: [], text: true, help: "Path patterns, space-separated. A land takes the base's side on conflict. Empty: none" },
|
|
126
|
+
{ key: "git.rebuild_on_land", label: "Rebuild after resolving", group: "Git", values: [], text: true, help: "Command that remakes the generated files. Empty: none" },
|
|
127
|
+
{ key: "git.land_check", label: "Check before landing", group: "Git", values: [], text: true, help: "After a rebase that pulled commits. auto: the test script in package.json · off · or a command" },
|
|
125
128
|
{ key: "orchestration.enabled", label: "Build with workers", group: "Orchestration", values: ["false", "true"], help: "Off by default. A plan may split into workers, each in its own terminal and folder. Linux and macOS only" },
|
|
126
129
|
{ key: "orchestration.max_workers", label: "Max workers per run", group: "Orchestration", values: ["1", "2", "3", "4", "5", "6"], help: "Total per run", when: ["orchestration.enabled", "true"] },
|
|
127
130
|
{ key: "orchestration.permissions", label: "Worker permissions", group: "Orchestration", values: ["own", "bypass"], help: "own: each agent's own prompts. Bypass skips every prompt in every worker", when: ["orchestration.enabled", "true"] },
|
package/cli/state.ts
CHANGED
|
@@ -776,9 +776,15 @@ export function markBacklogDone(state: string) {
|
|
|
776
776
|
|
|
777
777
|
// ---------------------------------------------------------------- board data
|
|
778
778
|
|
|
779
|
+
export type Land = { kind: "conflict" | "check" | "merge"; why: string; paths?: string[]; term?: string; at?: number };
|
|
780
|
+
const readLand = (st: string, id: string): Land | undefined => {
|
|
781
|
+
try { const l = JSON.parse(readFileSync(join(st, "runs", id, "land.json"), "utf8")); return typeof l?.kind === "string" ? l : undefined; } catch { return undefined; }
|
|
782
|
+
};
|
|
779
783
|
export type Card = {
|
|
780
784
|
kind: "idea" | "run";
|
|
781
785
|
id?: string; // runs only: the directory name
|
|
786
|
+
handoff?: string; // runs only: how a handoff ended, when its first agent is gone
|
|
787
|
+
land?: Land; // runs only: what stopped the last land (land.json)
|
|
782
788
|
stage?: string; // runs only: the stage word
|
|
783
789
|
iteration?: number; // runs only: the fix round
|
|
784
790
|
status: string; // what the legend calls this card
|
|
@@ -1025,7 +1031,7 @@ const NEED: Record<string, string> = {
|
|
|
1025
1031
|
};
|
|
1026
1032
|
export function notice(c: Card): string | null {
|
|
1027
1033
|
if (c.kind !== "run") return null;
|
|
1028
|
-
const what = c.awaiting ? NEED[c.awaiting] ?? `waiting on ${c.awaiting}` : c.status === "done" ? (c.accept === "needs" ? "done, not approved" : "done") : null;
|
|
1034
|
+
const what = c.land ? (c.land.kind === "conflict" ? `conflict: ${c.land.paths?.join(", ") || c.land.why}` : `land failed: ${c.land.why}`) : c.handoff ? `the worktree agent ended (${c.handoff}): tell the first agent, or review it here` : c.awaiting ? NEED[c.awaiting] ?? `waiting on ${c.awaiting}` : c.status === "done" ? (c.accept === "needs" ? "done, not approved" : "done") : null;
|
|
1029
1035
|
return what && `${c.text} — ${what}\n${c.where}`;
|
|
1030
1036
|
}
|
|
1031
1037
|
|
|
@@ -1295,6 +1301,7 @@ export function boardCards(states: string[], now: number): Card[] {
|
|
|
1295
1301
|
notes: readNotes(st, r.id) || undefined,
|
|
1296
1302
|
parent: parentOf(body(st, r.id)),
|
|
1297
1303
|
...(r.healed ? { healed: true } : {}),
|
|
1304
|
+
...((land) => land ? { land } : {})(readLand(st, r.id)),
|
|
1298
1305
|
...(r.stage === "done" ? accepted(st, r.id) : {}),
|
|
1299
1306
|
});
|
|
1300
1307
|
}
|
package/cli/usage.test.ts
CHANGED
|
@@ -82,6 +82,17 @@ test.skipIf(process.platform === "win32")("/shell: foreign origin, unknown proje
|
|
|
82
82
|
expect((await post({ dir: st }, "http://localhost:3000")).status).toBe(403);
|
|
83
83
|
// A simple (no-preflight) POST body type.
|
|
84
84
|
expect((await post({ dir: st }, at, "text/plain")).status).toBe(415);
|
|
85
|
+
const kill = (body: object, origin?: string, type = "application/json") => fetch(at + "/pty/kill", { method: "POST", headers: { "content-type": type, ...(origin ? { origin } : {}) }, body: JSON.stringify(body) });
|
|
86
|
+
expect((await kill({ key: "x" }, "https://evil.example")).status).toBe(403);
|
|
87
|
+
expect((await kill({ key: "x" }, at, "text/plain")).status).toBe(415);
|
|
88
|
+
expect((await kill({ key: "no-such" }, at)).status).toBe(404);
|
|
89
|
+
expect((await kill({ key: "../x" }, at)).status).toBe(404);
|
|
90
|
+
const res = await (await fetch(at + "/resources")).json();
|
|
91
|
+
expect(res.system.total).toBeGreaterThan(0);
|
|
92
|
+
expect(res.kaizen).toBeGreaterThan(0);
|
|
93
|
+
const disk = await fetch(at + "/resources/storage");
|
|
94
|
+
expect(disk.status).toBe(200);
|
|
95
|
+
expect(typeof (await disk.json()).scanning).toBe("boolean");
|
|
85
96
|
expect((await post({ dir: join(root, "elsewhere", ".kaizen") })).status).toBe(404);
|
|
86
97
|
expect((await post({ dir: join(homeDir, ".kaizen") })).status).toBe(404);
|
|
87
98
|
expect((await post({ dir: st, agent: "rm -rf /" })).status).toBe(400);
|