kaizen-agent 1.50.0 → 1.52.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/web.ts CHANGED
@@ -20,6 +20,7 @@ import { listNotes, saveNote, renameNote, deleteNote, boardIndex, linksTo } from
20
20
  import * as video from "./video.ts";
21
21
  import * as localsend from "./localsend.ts";
22
22
  import * as hermes from "./hermes.ts";
23
+ import { listChats, readChat, sendChat, stopChat, deleteChat, okId as okChatId, busy as chatBusy, type Event as ChatEvent, type Upload as ChatUpload } from "./chat.ts";
23
24
 
24
25
  const PAGE = join(dirname(import.meta.dir), "web", "board.html");
25
26
  // The board's script: web/board/*.ts, bundled into one file by `bun run build:web`.
@@ -158,6 +159,30 @@ export async function web(repoDir: string, opts: Opts = {}) {
158
159
  .filter((d) => existsSync(join(d, ".kaizen")) ? (remember(d), false) : true).sort();
159
160
  };
160
161
 
162
+ // New project…: a folder in the Projects folder setting (else ~/Projects) or at `to`,
163
+ // empty or new, with .kaizen, registered. check: only say where it would go.
164
+ async function makeProject(name: string, t = "", check = false): Promise<{ dir: string } | { to: string } | { error: string; status?: number }> {
165
+ // Windows' rules on every platform, so a project made here also opens there:
166
+ // no reserved device names (CON, NUL, COM1…), no trailing dot or space.
167
+ const winBad = (n: string) => !n || /[\\/:*?"<>|\x00-\x1f]/.test(n) || /[. ]$/.test(n)
168
+ || /^(con|prn|aux|nul|com[1-9]|lpt[1-9])(\..*)?$/i.test(n);
169
+ const to = t ? resolve(home, nativePath(t.startsWith("~") ? home + t.slice(1) : t))
170
+ : join((await projectsRoot()) || join(home, "Projects"), name);
171
+ // The page asks while the name is typed: "con" on the way to "config" is no error yet.
172
+ if (check) return { to };
173
+ if (winBad(name)) return { error: "name the project: a folder name, without / \\ : * ? \" < > |, not ending in a dot or space, and not CON, NUL, COM1 or the like" };
174
+ if (winBad(basename(to))) return { error: `${basename(to)} cannot be a folder name on Windows` };
175
+ let used = false;
176
+ try { used = readdirSync(to).length > 0; } catch (e) { used = existsSync(to); }
177
+ if (used) return { error: `${to} already exists and is not empty`, status: 409 };
178
+ // A drive that is not there, a read-only folder, a path too long: said in a toast, not a crash.
179
+ try { mkdirSync(join(to, ".kaizen"), { recursive: true }); }
180
+ catch (e) { return { error: `could not create ${to}: ${(e as Error).message}` }; }
181
+ remember(to);
182
+ changed();
183
+ return { dir: join(to, ".kaizen") };
184
+ }
185
+
161
186
  const states = (all: boolean, cur = boardState) => (all || !cur
162
187
  ? [...knownProjects().map((d) => join(d, ".kaizen")), join(home, ".kaizen")]
163
188
  : [cur]
@@ -220,9 +245,6 @@ export async function web(repoDir: string, opts: Opts = {}) {
220
245
  return { key, alive: st.alive, asking: st.asking };
221
246
  };
222
247
  };
223
- // Dev: the page reloads itself when board.html or its bundle is saved. Server code needs
224
- // `bun --watch cli/install.ts web`; the page reconnects and reloads on hello.
225
- for (const f of [PAGE, BOARD_JS]) try { watch(f, () => { for (const c of clients) { try { c.enqueue("data: reload\n\n"); } catch { clients.delete(c); } } }); } catch { /* no dev reload */ }
226
248
  const watchers: FSWatcher[] = [];
227
249
  const watchAll = () => {
228
250
  for (const w of watchers) { try { w.close(); } catch { /* gone */ } }
@@ -784,6 +806,64 @@ export async function web(repoDir: string, opts: Opts = {}) {
784
806
  });
785
807
  }
786
808
 
809
+ // Home's chat: chats live in ~/.kaizen/chats, each in a project of the board or in none.
810
+ // A send answers with the turn as NDJSON.
811
+ if (path === "/chats" || path === "/chat" || path === "/chat/stop" || path === "/chat/delete") {
812
+ const base = join(home, ".kaizen");
813
+ if (req.method === "GET" && path === "/chats") return json({ chats: listChats(base) });
814
+ if (req.method === "GET" && path === "/chat") {
815
+ const c = readChat(base, url.searchParams.get("id") ?? "");
816
+ return c ? json({ ...c, busy: chatBusy(base, c.id) }) : bad("no such chat", 404);
817
+ }
818
+ if (req.method !== "POST") return bad("method not allowed", 405);
819
+ if (!local(req)) return bad("cross-origin write refused", 403);
820
+ let body: any;
821
+ let uploads: ChatUpload[] = [];
822
+ if (path === "/chat" && req.headers.get("content-type")?.startsWith("multipart/form-data")) {
823
+ if (Number(req.headers.get("content-length")) > 28 * 1024 * 1024) return bad("attachments too large", 413);
824
+ let form: FormData;
825
+ try { form = await req.formData(); } catch { return bad("form data expected"); }
826
+ const files = form.getAll("files");
827
+ if (files.length > 5 || files.some((f) => !(f instanceof File) || f.size > 10 * 1024 * 1024) || files.reduce((n, f) => n + (f instanceof File ? f.size : 0), 0) > 20 * 1024 * 1024) return bad("up to 5 files, 10 MB each and 20 MB total", 413);
828
+ body = { id: form.get("id"), text: form.get("text") ?? "", project: form.get("project") };
829
+ uploads = await Promise.all(files.map(async (f) => ({ name: (f as File).name, bytes: new Uint8Array(await (f as File).arrayBuffer()) })));
830
+ } else try { body = await req.json(); } catch { return bad("json body expected"); }
831
+ const id = body.id == null || body.id === "" ? null : body.id;
832
+ if (id !== null && !okChatId(id)) return bad("bad chat id");
833
+ if (path === "/chat/stop") return stopChat(base, id ?? "") ? json({ ok: true }) : bad("not working", 409);
834
+ if (path === "/chat/delete") { const why = deleteChat(base, id ?? ""); return why ? bad(why, 409) : json({ ok: true }); }
835
+ const text = String(body.text ?? "").trim();
836
+ if (!text && !uploads.length) return bad("empty message");
837
+ if (text.length > 20000) return bad("message too long");
838
+ const board = {
839
+ projects: states(true).filter((d) => d !== base).map((d) => ({ dir: d, label: basename(dirname(d)) })),
840
+ create: async (name: string) => {
841
+ const r = await makeProject(name);
842
+ return "error" in r ? { error: r.error } : { dir: (r as { dir: string }).dir };
843
+ },
844
+ };
845
+ let ctl!: ReadableStreamDefaultController;
846
+ const stream = new ReadableStream({ start: (c) => { ctl = c; } });
847
+ const enc = new TextEncoder();
848
+ let chatId = id, moved = false, continued = false;
849
+ const emitChat = (e: ChatEvent) => {
850
+ if (e.t === "chat") chatId = e.id;
851
+ if (e.t === "project") moved = true;
852
+ if (e.t === "done" && moved && !continued && chatId) {
853
+ continued = true;
854
+ const why = sendChat(base, chatId, "Continue the user's pending request now that this chat is in the new project. Use the conversation context; finish the task without asking the user to repeat it. If they only asked to switch projects, stop after acknowledging the switch.", null, board, emitChat, true);
855
+ if (!why) return;
856
+ emitChat({ t: "error", error: why });
857
+ }
858
+ try { ctl.enqueue(enc.encode(JSON.stringify(e) + "\n")); if (e.t === "done") ctl.close(); } catch {}
859
+ // A turn often starts a run or edits the board's files.
860
+ if (e.t === "done") changed();
861
+ };
862
+ const why = sendChat(base, id, text, typeof body.project === "string" ? body.project : null, board, emitChat, false, uploads);
863
+ if (why) return bad(why, why === "no such chat" ? 404 : 409);
864
+ return new Response(stream, { headers: { "content-type": "application/x-ndjson", "cache-control": "no-store" } });
865
+ }
866
+
787
867
  if (req.method === "POST") {
788
868
  if (!local(req)) return bad("cross-origin write refused", 403);
789
869
  let body: any;
@@ -879,27 +959,8 @@ export async function web(repoDir: string, opts: Opts = {}) {
879
959
  // where the page names, set up at once. /find only sees folders that already
880
960
  // have a .kaizen, so a folder made by hand never shows there.
881
961
  if (path === "/new-project") {
882
- const name = typeof body.name === "string" ? body.name.trim() : "";
883
- // Windows' rules on every platform, so a project made here also opens there:
884
- // no reserved device names (CON, NUL, COM1…), no trailing dot or space.
885
- const winBad = (n: string) => !n || /[\\/:*?"<>|\x00-\x1f]/.test(n) || /[. ]$/.test(n)
886
- || /^(con|prn|aux|nul|com[1-9]|lpt[1-9])(\..*)?$/i.test(n);
887
- const t = typeof body.to === "string" ? body.to.trim() : "";
888
- const to = t ? resolve(home, nativePath(t.startsWith("~") ? home + t.slice(1) : t))
889
- : join((await projectsRoot()) || join(home, "Projects"), name);
890
- // The page asks while the name is typed: "con" on the way to "config" is no error yet.
891
- if (body.check) return json({ ok: true, to });
892
- if (winBad(name)) return bad("name the project: a folder name, without / \\ : * ? \" < > |, not ending in a dot or space, and not CON, NUL, COM1 or the like");
893
- if (winBad(basename(to))) return bad(`${basename(to)} cannot be a folder name on Windows`);
894
- let used = false;
895
- try { used = readdirSync(to).length > 0; } catch (e) { used = existsSync(to); }
896
- if (used) return bad(`${to} already exists and is not empty`, 409);
897
- // A drive that is not there, a read-only folder, a path too long: said in a toast, not a crash.
898
- try { mkdirSync(join(to, ".kaizen"), { recursive: true }); }
899
- catch (e) { return bad(`could not create ${to}: ${(e as Error).message}`); }
900
- remember(to);
901
- changed();
902
- return json({ ok: true, dir: join(to, ".kaizen") });
962
+ const r = await makeProject(typeof body.name === "string" ? body.name.trim() : "", typeof body.to === "string" ? body.to.trim() : "", !!body.check);
963
+ return "error" in r ? bad(r.error, r.status) : json({ ok: true, ...r });
903
964
  }
904
965
 
905
966
  // Clone a GitHub repository into the projects folder (else ~/Projects) and
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kaizen-agent",
3
- "version": "1.50.0",
3
+ "version": "1.52.0",
4
4
  "description": "Staged workflow for AI agents — code, writing, research, operations: plan, human approval, build, independent review, bounded fix loop.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -178,9 +178,9 @@ Every iteration is written to `runs/<id>/05-iterations/` so the loop is auditabl
178
178
 
179
179
  Each run keeps its own backlog at `runs/<id>/06-backlog.md`. You write it, in the main
180
180
  thread, at two writes that already happen: `02-approval.md` at the plan approval
181
- (only work the user explicitly deferred) and `stage: "done"` at the final approval
182
- (findings still open after the fix loop, unresolved verification, and explicit
183
- deferrals). Suggestions the user did not request are not backlog items. No subagent writes it.
181
+ and `stage: "done"` at the final approval, and only with what the user asked to
182
+ backlog. Open findings, unverified checks and suggestions are offered, never filed by
183
+ themselves. No subagent writes it.
184
184
  The full rules are in [`spec-main.md`](spec-main.md).
185
185
 
186
186
  `/kaizen status` and `/kaizen backlog`: read [`status.md`](status.md) first and follow
@@ -6,16 +6,14 @@ the final approval. No subagent reads this file.
6
6
  ---
7
7
  ## Backlog
8
8
 
9
- Some runs leave work deliberately unresolved. Capture only:
10
-
11
- 1. Work the user explicitly deferred at an approval.
12
- 2. Findings still open after the bounded fix loop, or below a configured higher
13
- `auto_fix.min_severity` that the user chose not to fix.
14
- 3. Criteria the reviewer answered `CANNOT VERIFY` that the user did not settle.
15
-
16
- Planner suggestions and builder notes outside the approved scope are reported, but
17
- are not backlog items unless the user explicitly defers them. Without capture,
18
- unresolved work would survive only in the conversation that produced it.
9
+ The backlog holds only what the user asked to put there. Nothing is filed because it
10
+ was left over: findings still open after the fix loop, `CANNOT VERIFY` criteria,
11
+ planner suggestions and builder notes are reported at the approval, and each goes to
12
+ the backlog only when the user says so. What the user does not name stays in
13
+ `04-review.md` or the plan as record, not as open work. In `auto` mode nobody is asked,
14
+ so nothing is filed. Asking counts only in the user's own words naming the item ("do
15
+ X next run", "backlog 5"); a bare approve files nothing. When unsure, ask once; do not
16
+ file to be safe.
19
17
 
20
18
  `backlog.enabled: false` skips both capture points — the `02-approval.md` append at
21
19
  plan approval and the `06-backlog.md` append at final approval — for that run. The run
@@ -69,8 +67,8 @@ unconditionally:
69
67
 
70
68
  | Capture point | Stage | What gets appended |
71
69
  |---|---|---|
72
- | Writing `02-approval.md`, when the decision is an approval | Plan approval | Only work the user explicitly deferred |
73
- | Setting `stage: "done"` | Final approval | Unresolved findings and `CANNOT VERIFY` criteria, plus explicit deferrals not already recorded |
70
+ | Writing `02-approval.md`, when the decision is an approval | Plan approval | Only items the user asked to backlog |
71
+ | Setting `stage: "done"` | Final approval | Only items the user asked to backlog, not already recorded |
74
72
 
75
73
  Neither is optional and neither is a separate step to remember — a run cannot finish
76
74
  without touching both files.
@@ -153,18 +151,17 @@ everything in place: report its message as printed and let the user decide. The
153
151
  `done` once approved; whether the work landed is reported, not hidden.
154
152
 
155
153
  **Open findings are offered by number.** List them with the reviewer's numbering
156
- intact and let the user name which to fix — "fix 2 and 5". A number not named is not
157
- touched, and goes to the backlog as `open` like any other deferred item. This is the
154
+ intact, with `CANNOT VERIFY` criteria after them, and let the user name what to fix and
155
+ what to backlog — "fix 2, backlog 5". A number not named is not touched and not filed;
156
+ it stays in `04-review.md`. This is the
158
157
  same granularity the fix loop already works at; the only change is that the user
159
158
  picks the subset instead of accepting or declining the whole review.
160
159
 
161
- Before setting `stage: "done"`, append findings still open after the fix loop,
162
- findings below a configured higher fix threshold that the user chose not to fix,
163
- `CANNOT VERIFY` criteria the user did not settle, and work explicitly deferred at
164
- the final approval to `runs/<id>/06-backlog.md`, each as `open`, in the format given
160
+ Before setting `stage: "done"`, append the findings, criteria and work the user asked
161
+ to backlog at the final approval to `runs/<id>/06-backlog.md`, each as `open`, in the format given
165
162
  in *Backlog* above. Check existing entries and do not append a duplicate. Builder
166
163
  notes outside the approved scope are reported, but are not filed without an explicit
167
- deferral.
164
+ request.
168
165
 
169
166
  Set `stage: "done"` in `state.json` once the user accepts.
170
167
 
@@ -295,7 +295,8 @@ modification the user asked for. When the decision is an approval — full or pa
295
295
  the same write appends to `runs/<id>/06-backlog.md` only work the user explicitly
296
296
  deferred, each as `open` — see *Backlog* below for the format. An unaccepted
297
297
  out-of-scope suggestion is not a backlog item merely because it appeared in the plan;
298
- silence is not a request to do it later. A rejection appends nothing: there is no
298
+ silence is not a request to do it later, and neither is a bare "approve" — a deferral
299
+ is the user naming the item and asking for it later. A rejection appends nothing: there is no
299
300
  approved scope yet to measure against, and the approval of the revised plan is what
300
301
  captures any explicit deferral, once.
301
302
 
@@ -113,8 +113,8 @@ this skill's `config.default.yml`.
113
113
 
114
114
  ## Backlog and status
115
115
 
116
- Backlog records explicit deferrals and findings still unresolved after the fix loop,
117
- not every out-of-scope suggestion. Items: `- <status>: <item text>`, `<status>` one of `open`, `done`, `rejected`
116
+ Backlog records only what the user asked to backlog at an approval; open findings and
117
+ suggestions are offered, not filed by themselves. Items: `- <status>: <item text>`, `<status>` one of `open`, `done`, `rejected`
118
118
  (`rejected` carries a reason after `|`). `/kaizen backlog`/`status` are **read-only** —
119
119
  never edit `06-backlog.md` or `state.json`, even to fix a spotted inconsistency; report
120
120
  it and ask.
package/web/board/api.ts CHANGED
@@ -30,6 +30,8 @@ export async function load(quiet) {
30
30
  }
31
31
  const was = store.openKey && store.S.cards.find((x) => key(x) === store.openKey);
32
32
  store.S = await r.json();
33
+ // Home follows a project switch without the board importing it.
34
+ dispatchEvent(new Event("kz-state"));
33
35
  {
34
36
  const seen = new Map();
35
37
  for (const c of store.S.cards) {