onionskin-cli 0.0.3 → 0.0.5

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 (92) hide show
  1. package/CHANGELOG.md +100 -0
  2. package/INSTALL.md +5 -1
  3. package/NOTICE +34 -0
  4. package/README.md +101 -33
  5. package/docs/contracts/batch.schema.json +27 -4
  6. package/docs/contracts/ops.schema.json +117 -5
  7. package/package.json +4 -2
  8. package/skills/onionskin/SKILL.md +9 -6
  9. package/skills/onionskin/reference/batch.md +5 -3
  10. package/skills/onionskin/reference/formats.md +5 -1
  11. package/skills/onionskin/reference/ops.md +47 -3
  12. package/skills/onionskin/reference/troubleshooting.md +31 -0
  13. package/src/agent-detect.js +28 -2
  14. package/src/agent-prompt.js +12 -1
  15. package/src/agent-runner.js +16 -5
  16. package/src/anchor-work.js +154 -0
  17. package/src/anchor-worker.js +38 -0
  18. package/src/anchors.js +61 -11
  19. package/src/apply.js +119 -47
  20. package/src/batch.js +32 -8
  21. package/src/check-worker.js +8 -5
  22. package/src/cli.js +91 -25
  23. package/src/constants.js +34 -1
  24. package/src/diff.js +3 -1
  25. package/src/docx.js +368 -58
  26. package/src/edit-hash.js +19 -0
  27. package/src/fsutil.js +1 -1
  28. package/src/history-worker.js +8 -8
  29. package/src/history.js +135 -31
  30. package/src/html-active.js +384 -0
  31. package/src/html-envelope.js +51 -0
  32. package/src/html-parser.js +8 -0
  33. package/src/html-render-worker.js +25 -0
  34. package/src/html-render.js +880 -0
  35. package/src/html-utf8.js +65 -0
  36. package/src/html-write-worker.js +14 -0
  37. package/src/html-write.js +380 -0
  38. package/src/html.js +1257 -0
  39. package/src/local.js +68 -4
  40. package/src/merge-package.js +1110 -0
  41. package/src/merge-pptx.js +98 -38
  42. package/src/merge-proof.js +1019 -0
  43. package/src/merge-worker.js +60 -0
  44. package/src/merge-xlsx.js +84 -29
  45. package/src/merge.js +286 -83
  46. package/src/nfc.js +46 -0
  47. package/src/notes.js +122 -0
  48. package/src/offload.js +400 -1
  49. package/src/open-worker.js +27 -0
  50. package/src/ops/common.js +2 -1
  51. package/src/ops/docx.js +58 -29
  52. package/src/ops/html.js +394 -0
  53. package/src/ops/pptx.js +74 -21
  54. package/src/ops/run.js +23 -3
  55. package/src/ops/runs.js +215 -13
  56. package/src/ops/schema.js +8 -1
  57. package/src/ops/xlsx.js +12 -7
  58. package/src/ops-worker.js +17 -0
  59. package/src/package.js +26 -2
  60. package/src/person.js +2 -1
  61. package/src/pptx.js +85 -21
  62. package/src/quick-plan.js +13 -6
  63. package/src/redline-worker.js +10 -14
  64. package/src/reply.js +46 -19
  65. package/src/retain.js +223 -0
  66. package/src/review.js +32 -18
  67. package/src/rewrite-check.js +2 -2
  68. package/src/sanitize.js +16 -3
  69. package/src/server.js +80 -36
  70. package/src/session.js +1138 -208
  71. package/src/stop-gate.js +34 -0
  72. package/src/suggest-reply.js +42 -7
  73. package/src/suggest.js +14 -29
  74. package/src/text.js +161 -11
  75. package/src/update.js +288 -0
  76. package/src/voice-guide.js +4 -3
  77. package/src/voice-learn.js +4 -3
  78. package/src/voice-sample-worker.js +7 -6
  79. package/src/voice-worker.js +4 -6
  80. package/src/worker-host.js +23 -0
  81. package/src/xlsx.js +355 -94
  82. package/src/xml-tree.js +6 -3
  83. package/src/xml.js +82 -34
  84. package/src/zip-write.js +2 -1
  85. package/src/zip.js +9 -5
  86. package/vendor/parse5/LICENSE.txt +32 -0
  87. package/vendor/parse5/parse5.mjs +7822 -0
  88. package/vendor/parse5.lock.json +23 -0
  89. package/web/dist/assets/index-DkNx0LSH.css +1 -0
  90. package/web/dist/assets/{index-DS-WST1E.js → index-DytKkMdi.js} +112 -108
  91. package/web/dist/index.html +2 -2
  92. package/web/dist/assets/index-BCAEKiOG.css +0 -1
package/src/server.js CHANGED
@@ -7,12 +7,15 @@ import { PRODUCT, SERVER_PROTOCOL, env } from "./constants.js";
7
7
  import { sha256 } from "./fsutil.js";
8
8
  import { ensureStateDir, serverRecordPath, socketPath, stateDir, writeStateFile } from "./paths.js";
9
9
  import { setupCheckerStatus, startLateSoffice, startSetupChecker } from "./checker.js";
10
- import { analyzeOffThread } from "./offload.js";
10
+ import { analyzeOffThread, warmAnchors } from "./offload.js";
11
+ import { REQUEST_MAX, requestSize } from "./retain.js";
11
12
  import { warm, warmNewest } from "./history.js";
12
13
  import { stopRewriter } from "./rewriter.js";
13
- import { HttpError, Sessions } from "./session.js";
14
+ import { HttpError, Sessions, tooLarge } from "./session.js";
15
+ import { encodeHtmlView } from "./html-envelope.js";
16
+ import { CapError } from "./xml.js";
14
17
  import { quickPreview } from "./quick-plan.js";
15
- import { cliStatus, leaseOf, localRunner } from "./local.js"; // leaseOf: the delivery an agent acts on (batch, holder, token), all three required
18
+ import { batchFile, checkBinding, claimOfQuery, cliBatch, cliStatus, leaseOf, localRunner, openWork, sessionFor } from "./local.js"; // leaseOf: the delivery an agent acts on (batch, holder, token), all three required
16
19
  import { readStamps, writeStamps } from "./stamps.js";
17
20
  import { setupRewriterStatus, startSetupRewriter, suggest, suggestStatus, useSuggestion, warmSuggest } from "./suggest.js";
18
21
  import { readStyle, writeStyle } from "./style.js";
@@ -32,6 +35,7 @@ function secure(res) {
32
35
  res.setHeader("Referrer-Policy", "no-referrer");
33
36
  res.setHeader("X-Content-Type-Options", "nosniff");
34
37
  res.setHeader("Cache-Control", "no-store");
38
+ res.setHeader("X-DNS-Prefetch-Control", "off"); // PLAN-html R3, R21: no DNS prefetch for anything the page or an HTML view names
35
39
  }
36
40
  function sendJson(res, status, body) {
37
41
  secure(res);
@@ -44,23 +48,32 @@ function sendText(res, status, text) {
44
48
  res.writeHead(status, { "Content-Type": "text/plain; charset=utf-8" });
45
49
  res.end(text);
46
50
  }
47
- function readBody(req, max = MAX_BODY) {
51
+ // A body past `max` is refused with a JSON answer the page can show (413, code too_large_request), not a cut connection (the page read
52
+ // that as a failure to retry forever): the rest of it is read and dropped, up to MAX_BODY more, so the browser gets the answer; past
53
+ // that the connection is closed.
54
+ function readBody(req, max = MAX_BODY, refusal = null) {
48
55
  return new Promise((resolve, reject) => {
49
56
  const chunks = [];
50
57
  let n = 0;
58
+ let over = false;
51
59
  req.on("data", (c) => {
52
60
  n += c.length;
61
+ if (over) {
62
+ if (n > max + MAX_BODY) req.destroy();
63
+ return;
64
+ }
53
65
  if (n > max) {
54
- reject(new HttpError(413, "body too large"));
55
- req.destroy();
66
+ over = true;
67
+ chunks.length = 0;
68
+ reject(refusal || new HttpError(413, "body too large", { code: "too_large_request" }));
56
69
  } else chunks.push(c);
57
70
  });
58
71
  req.on("end", () => resolve(Buffer.concat(chunks)));
59
72
  req.on("error", reject);
60
73
  });
61
74
  }
62
- async function readJson(req, max = MAX_BODY) {
63
- const raw = await readBody(req, max);
75
+ async function readJson(req, max = MAX_BODY, refusal = null) {
76
+ const raw = await readBody(req, max, refusal);
64
77
  try {
65
78
  return raw.length ? JSON.parse(raw.toString("utf8")) : {};
66
79
  } catch {
@@ -70,6 +83,8 @@ async function readJson(req, max = MAX_BODY) {
70
83
  function fail(res, err) {
71
84
  if (err instanceof HttpError) return sendJson(res, err.status, { error: err.message, ...err.extra });
72
85
  if (err instanceof SettingsError) return sendJson(res, err.status, { error: err.message });
86
+ // a document past a reader's cap (src/xml.js): plain words a person can act on, whatever route read it
87
+ if (err instanceof CapError) return sendJson(res, 422, { error: `Onionskin cannot read this file: ${err.message}.`, code: "too_large" });
73
88
  console.error("internal error:", err.stack || err);
74
89
  return sendJson(res, 500, { error: "internal error" });
75
90
  }
@@ -158,6 +173,10 @@ export async function startServer({ port = 0 } = {}) {
158
173
  ensureStateDir();
159
174
  const sessions = new Sessions();
160
175
  const sse = new Map(); // key -> Set<res>
176
+ // key -> when a page of that session last spoke (its event stream opened or ended, any other request): `onionskin update` holds
177
+ // off while a tab is open or was open lately (src/local.js openWork), since edits typed in a tab live only in that tab until Send
178
+ const tabSeen = new Map();
179
+ const tabsOf = (key) => sse.get(key)?.size || 0;
161
180
  // LibreOffice checks running after `done` returned (session key -> Set<{cancel}>). Cancelled when the session closes or the server stops.
162
181
  const late = new Map();
163
182
  function startLate(key, inputs, analysis) {
@@ -188,7 +207,7 @@ export async function startServer({ port = 0 } = {}) {
188
207
  let analysis, done;
189
208
  try {
190
209
  analysis = inputs ? await analyzeOffThread(inputs) : null; // the server keeps answering while the checks run
191
- done = sessions.done(s.key, results, claim, analysis);
210
+ done = await sessions.doneAsync(s.key, results, claim, analysis, { inline: false }); // the checks and the anchors never run on this thread
192
211
  } catch (err) {
193
212
  if (delivery && s.delivered === delivery && stageBefore) sessions.setStage(s.key, stageBefore); // done did not finish and this delivery is still the current one
194
213
  throw err;
@@ -203,7 +222,7 @@ export async function startServer({ port = 0 } = {}) {
203
222
  sessions.local = local;
204
223
  sessions.on("local", (s, ticket) => setImmediate(() => local.run(s.key, ticket)));
205
224
  // the background agent (src/agent-runner.js): started for a file opened from a person's terminal, or for a Send nobody took
206
- const agent = agentRunner(sessions, { tabs: (key) => sse.get(key)?.size || 0 });
225
+ const agent = agentRunner(sessions, { tabs: tabsOf });
207
226
  sessions.agent = agent;
208
227
  // a faster outside-edit notice (src/watch.js); the page's /api/stale poll stays the backstop
209
228
  const watcher = docWatcher(sessions);
@@ -248,7 +267,8 @@ export async function startServer({ port = 0 } = {}) {
248
267
  const bad = auth.check(req, { mutating, queryToken: isEvents ? url.searchParams.get("t") || "" : "" });
249
268
  if (bad) return sendJson(res, isBootstrap && bad.code === "token_required" ? 401 : bad.status, { error: bad.error, ...(bad.code ? { code: bad.code, reopen: `${PRODUCT.bin} open <file>` } : {}) });
250
269
  const key = url.searchParams.get("s") || "";
251
- if (isBootstrap) return sendJson(res, 200, { session: sessions.view(sessions.get(key)), product: PRODUCT.name, settings: readSettings(), suggest: suggestStatus() });
270
+ // a page that loads (or reloads) while a Send of this file is in flight starts from what it committed (Sessions.settled)
271
+ if (isBootstrap) return sendJson(res, 200, { session: (await sessions.settled(key), sessions.view(sessions.get(key))), product: PRODUCT.name, settings: readSettings(), suggest: suggestStatus() });
252
272
  // per-machine settings and the background image: not tied to a session, so any tab (a read-only one too) may use them
253
273
  if (p === "/api/settings" || p.startsWith("/api/background")) return await settingsRoute(req, res, p);
254
274
  // own stamps are per machine too: a list of {label, words} the person typed, any tab may use it
@@ -273,6 +293,7 @@ export async function startServer({ port = 0 } = {}) {
273
293
  const tab = String(req.headers["x-onionskin-tab"] || url.searchParams.get("tab") || "").slice(0, 64);
274
294
  if (isEvents) return events(req, res, key, tab);
275
295
  const s = sessions.get(key);
296
+ tabSeen.set(key, Date.now());
276
297
  if (req.method === "GET" && p === "/api/file") {
277
298
  const { bytes, diskSha } = sessions.loadForPage(key); // records an outside change the user is taking (history)
278
299
  warm(s);
@@ -280,6 +301,16 @@ export async function startServer({ port = 0 } = {}) {
280
301
  res.writeHead(200, { "Content-Type": "application/octet-stream", "Content-Length": bytes.length, "X-Sha256": diskSha });
281
302
  return res.end(bytes);
282
303
  }
304
+ if (req.method === "GET" && p === "/api/html-view") {
305
+ // an HTML file's render copy and what the page needs with it, in one binary body (src/html-envelope.js); never text/html (R12)
306
+ const v = await sessions.htmlView(key, { plain: url.searchParams.get("plain") === "1" });
307
+ warm(s);
308
+ const [prefix, copy] = encodeHtmlView({ version: v.sha, plain: v.plain, reason: v.reason, stats: v.stats, texts: v.texts, editable: v.editable, ident: v.ident, copy: v.bytes });
309
+ secure(res);
310
+ res.writeHead(200, { "Content-Type": "application/octet-stream", "Content-Length": prefix.length + copy.length, "X-Sha256": v.sha });
311
+ res.write(prefix);
312
+ return res.end(copy);
313
+ }
283
314
  if (req.method === "GET" && p === "/api/stale") return sendJson(res, 200, sessions.staleCheck(key, url.searchParams.get("sha") || ""));
284
315
  if (req.method === "GET" && p === "/api/history") return sendJson(res, 200, await sessions.history(key));
285
316
  if (req.method === "GET" && p.startsWith("/api/history/")) return sendJson(res, 200, await sessions.historyUpdate(key, decodeURIComponent(p.slice("/api/history/".length)).slice(0, 64)));
@@ -293,9 +324,14 @@ export async function startServer({ port = 0 } = {}) {
293
324
  }
294
325
  // starts the writing helper of quick fixes if it is cold (src/suggest.js); carries no text, changes nothing
295
326
  if (p === "/api/suggest/warm") return sendJson(res, 200, (await readJson(req, 1024), warmSuggest()));
296
- const body = await readJson(req, p === "/api/export-tracked" || p === "/api/agent" ? 1024 : p === "/api/suggest" ? 8 * 1024 : MAX_BODY); // the export and the agent's buttons take ids only
327
+ // the export and the agent's buttons take ids only; a draft carries no document, so it is held to the cap of a request's own data
328
+ const body = await readJson(req, p === "/api/export-tracked" || p === "/api/agent" ? 1024 : p === "/api/suggest" ? 8 * 1024 : p === "/api/draft" || p === "/api/handover" ? REQUEST_MAX : MAX_BODY, p === "/api/draft" || p === "/api/handover" ? tooLarge("This draft") : null);
329
+ // a Send or a quick fix's Use carries the document too: their comments, note, typed edits and view state are held to the same cap
330
+ if ((p === "/api/send" || p === "/api/suggest/use") && requestSize([body.comments, body.note, body.edits, body.viewState, body.htmlEdits]) > REQUEST_MAX) throw tooLarge(p === "/api/send" ? "This Send" : "This request");
297
331
  if (p === "/api/claim" && !/^[\w-]{6,64}$/.test(tab)) return sendJson(res, 400, { error: "a tab id is required" });
298
332
  if (p === "/api/claim") return sendJson(res, 200, { ok: sessions.claimEditor(key, tab, { takeover: body.takeover === true }), session: sessions.view(s) });
333
+ // the tab that just lost editing hands over its unsaved words (Sessions.handOver checks that it is that tab), in the file's turn
334
+ if (p === "/api/handover") return sendJson(res, 200, { ok: true, session: (await sessions.inTurn(key, () => sessions.handOver(key, tab, { comments: body.comments, note: body.note, base: body.base, baseSeq: body.baseSeq, noteRev: body.noteRev })), sessions.view(s)) });
299
335
  if (p !== "/api/resolve") sessions.requireEditor(key, tab);
300
336
  // the background agent's buttons: a fixed action, and for `use` a CLI name, for `show` a switch. No path or text is accepted.
301
337
  if (p === "/api/agent") {
@@ -308,18 +344,20 @@ export async function startServer({ port = 0 } = {}) {
308
344
  await agent.action(key, { action: body.action, cli: body.cli, on: body.on, takeBack: body.takeBack === true });
309
345
  return sendJson(res, 200, { ok: true, session: sessions.view(s) });
310
346
  }
311
- if (p === "/api/draft") return sendJson(res, 200, { ok: true, session: (sessions.setDraft(key, body), sessions.view(s)) });
347
+ // a draft save, an answer and Take back run in this file's turn: one that arrives while a Send is in flight waits for it (Sessions.inTurn)
348
+ if (p === "/api/draft") return sendJson(res, 200, { ok: true, session: (await sessions.setDraftAsync(key, { comments: body.comments, note: body.note, rev: body.rev, noteRev: body.noteRev, seen: body.seen, noteSeen: body.noteSeen, tab }), sessions.view(s)) });
312
349
  // a tap on a question card: an index (or null for "Something else"), never option text; checked against what the server stored
313
- if (p === "/api/answer") return sendJson(res, 200, { ok: true, session: (sessions.answer(key, { id: String(body.id || ""), choice: body.choice }), sessions.view(s)) });
350
+ if (p === "/api/answer") return sendJson(res, 200, { ok: true, session: (await sessions.inTurn(key, () => sessions.answer(key, { id: String(body.id || ""), choice: body.choice })), sessions.view(s)) });
314
351
  // review first (src/review.js): a button's kind, then taps on its cards (ids and an integer, never text)
315
352
  if (p === "/api/review") return sendJson(res, 200, { ok: true, session: (sessions.requestReview(key, body.kind), sessions.view(s)) });
316
353
  if (p === "/api/suggestion") return sendJson(res, 200, { ok: true, session: (sessions.answerSuggestions(key, { ids: body.ids, choice: body.choice }), sessions.view(s)) });
317
354
  // quick fixes (src/suggest.js): asking writes nothing; Use names the stored suggestion by id and writes it on this Mac
318
355
  if (p === "/api/suggest") return sendJson(res, 200, await suggest(sessions, key, body));
319
- if (p === "/api/suggest/use") return sendJson(res, 200, { ok: true, session: (useSuggestion(sessions, key, body), sessions.view(s)) });
356
+ if (p === "/api/suggest/use") return sendJson(res, 200, { ok: true, session: (await useSuggestion(sessions, key, { ...body, tab }), sessions.view(s)) });
320
357
  if (p === "/api/send") {
321
358
  const bytes = typeof body.bytesB64 === "string" ? Buffer.from(body.bytesB64, "base64") : null;
322
- sessions.send(key, { baseSha: String(body.baseSha || ""), bytes, comments: body.comments, note: body.note, edits: body.edits, viewState: body.viewState, quick: body.quick });
359
+ // an HTML file's text edits (PLAN-html R9) come as {nodeId, versionSha, baseTextHash, newText}, never as bytes
360
+ await sessions.sendAsync(key, { baseSha: String(body.baseSha || ""), bytes, comments: body.comments, note: body.note, edits: body.edits, htmlEdits: body.htmlEdits, viewState: body.viewState, quick: body.quick, rev: body.rev, noteRev: body.noteRev, seen: body.seen, noteSeen: body.noteSeen, sends: body.sends, tab });
323
361
  warm(s);
324
362
  return sendJson(res, 200, { ok: true, session: sessions.view(s) });
325
363
  }
@@ -329,9 +367,9 @@ export async function startServer({ port = 0 } = {}) {
329
367
  warm(s);
330
368
  return sendJson(res, 200, { ok: true, ...r, session: sessions.view(s) });
331
369
  }
332
- if (p === "/api/takeback") return sendJson(res, 200, { ok: true, session: (sessions.takeBack(key), sessions.view(s)) });
333
- if (p === "/api/revert") return sendJson(res, 200, { ok: true, session: (sessions.revert(key, String(body.id || "")), warm(s), sessions.view(s)) });
334
- if (p === "/api/resolve") return sendJson(res, 200, sessions.resolveAll(key));
370
+ if (p === "/api/takeback") return sendJson(res, 200, { ok: true, session: (await sessions.inTurn(key, () => sessions.takeBack(key)), sessions.view(s)) });
371
+ if (p === "/api/revert") return sendJson(res, 200, { ok: true, session: (await sessions.revertAsync(key, String(body.id || "")), warm(s), sessions.view(s)) });
372
+ if (p === "/api/resolve") return sendJson(res, 200, await sessions.resolveAllAsync(key));
335
373
  if (p === "/api/export-tracked") return sendJson(res, 200, await sessions.exportTracked(key, String(body.id || "").slice(0, 64)));
336
374
  if (p === "/api/show-copy") return sendJson(res, 200, sessions.showCopy(String(body.path || "")));
337
375
  if (p === "/api/dismiss-conflict") return sendJson(res, 200, { ok: true, session: (sessions.dismissConflict(key), sessions.view(s)) });
@@ -396,7 +434,8 @@ export async function startServer({ port = 0 } = {}) {
396
434
  res.write(`event: state\ndata: ${JSON.stringify(sessions.view(s))}\n\n`);
397
435
  if (!sse.has(key)) sse.set(key, new Set());
398
436
  sse.get(key).add(res);
399
- req.on("close", () => sse.get(key)?.delete(res));
437
+ tabSeen.set(key, Date.now());
438
+ req.on("close", () => (sse.get(key)?.delete(res), tabSeen.set(key, Date.now())));
400
439
  }
401
440
  sessions.on("change", (s, event) => {
402
441
  const set = sse.get(s.key);
@@ -417,44 +456,46 @@ export async function startServer({ port = 0 } = {}) {
417
456
  if (p === "/health") return sendJson(res, 200, { ok: true, pid: process.pid, protocol: SERVER_PROTOCOL, sessions: sessions.map.size });
418
457
  if (req.method === "POST" && p === "/cli/open") {
419
458
  const { file, agent: wants } = await readJson(req);
420
- const s = sessions.open(String(file || ""), { agent: wants === "background" ? "background" : undefined });
459
+ const s = await sessions.openAsync(String(file || ""), { agent: wants === "background" ? "background" : undefined });
421
460
  if (wants === "background") agent.ensure(s.key); // a person opened it from a terminal: the background agent works on it
422
461
  const nonce = auth.newNonce();
423
462
  return sendJson(res, 200, { key: s.key, url: `${auth.origin}/?s=${s.key}&n=${nonce}`, state: sessions.state(s), name: path.basename(s.file) });
424
463
  }
425
464
  if (req.method === "GET" && p === "/cli/status") {
426
- return sendJson(res, 200, cliStatus(sessions, sessions.byFile(url.searchParams.get("file") || "")));
465
+ return sendJson(res, 200, cliStatus(sessions, sessionFor(sessions, url.searchParams.get("file") || "", url.searchParams.get("holder"))));
466
+ }
467
+ if (req.method === "GET" && p === "/cli/open-work") {
468
+ // what `onionskin update` would cut off by restarting the server: counts only, no file names
469
+ return sendJson(res, 200, openWork(sessions, { tabs: tabsOf, seen: (key) => tabSeen.get(key) ?? null }));
427
470
  }
428
471
  if (req.method === "GET" && p === "/cli/batch-file") {
429
- // which file a delivered batch belongs to (for `reply --lease <ticket>` without a file); the lease token is still checked at the write
430
- const id = url.searchParams.get("batch_id") || "";
431
- const s = id ? [...sessions.map.values()].find((x) => x.delivered?.batch.batch_id === id) : null;
432
- return sendJson(res, 200, { file: s ? s.file : null });
472
+ // which file a delivered batch belongs to (for `reply --lease <ticket>` without a file): only for the whole ticket (batch id and token)
473
+ return sendJson(res, 200, { file: batchFile(sessions, claimOfQuery(url.searchParams)) });
433
474
  }
434
475
  if (req.method === "GET" && p === "/cli/batch") {
435
- // the batch delivered to the agent (anchors for `apply`); null when none is in progress
436
- const s = sessions.byFile(url.searchParams.get("file") || "");
437
- return sendJson(res, 200, { state: sessions.state(s), batch: s.delivered?.batch || null });
476
+ // the batch delivered to the agent (anchors for `apply`); with a claim, only for the lease holder (src/local.js cliBatch)
477
+ const claim = claimOfQuery(url.searchParams);
478
+ return sendJson(res, 200, cliBatch(sessions, sessionFor(sessions, url.searchParams.get("file") || "", claim.holder), claim));
438
479
  }
439
480
  if (req.method === "POST" && p === "/cli/agent-write") {
440
481
  const body = await readJson(req);
441
- const s = sessions.byFile(String(body.file || ""));
482
+ const s = sessionFor(sessions, String(body.file || ""), body.holder);
442
483
  const bytes = typeof body.bytes_b64 === "string" ? Buffer.from(body.bytes_b64, "base64") : null;
443
484
  // `ops` is what apply ran to make these bytes: kept for a later merge, which checks it against the bytes before relying on it
444
- return sendJson(res, 200, sessions.agentWrite(s.key, { baseSha: String(body.base_sha256 || ""), bytes, route: "apply", ops: Array.isArray(body.ops) ? body.ops : null, partial: body.partial === true, ...leaseOf(body) }));
485
+ return sendJson(res, 200, await sessions.agentWriteChecked(s.key, { baseSha: String(body.base_sha256 || ""), bytes, route: "apply", ops: Array.isArray(body.ops) ? body.ops : null, partial: body.partial === true, ...leaseOf(body) }));
445
486
  }
446
487
  if (req.method === "POST" && p === "/cli/commit") {
447
488
  // candidate bytes made elsewhere (a copy edited with python-docx and so on): same lease check, compare-and-swap, snapshot and atomic replace as apply.
448
489
  // Without base_sha256 the base is the hash this server last wrote or delivered for the batch.
449
490
  const body = await readJson(req);
450
- const s = sessions.byFile(String(body.file || ""));
491
+ const s = sessionFor(sessions, String(body.file || ""), body.holder);
451
492
  const bytes = typeof body.bytes_b64 === "string" ? Buffer.from(body.bytes_b64, "base64") : null;
452
493
  const baseSha = typeof body.base_sha256 === "string" && body.base_sha256 ? body.base_sha256 : null;
453
- return sendJson(res, 200, sessions.agentWrite(s.key, { baseSha, bytes, label: "before agent commit", ...leaseOf(body) }));
494
+ return sendJson(res, 200, await sessions.agentWriteChecked(s.key, { baseSha, bytes, label: "before agent commit", ...leaseOf(body) }));
454
495
  }
455
496
  if (req.method === "POST" && p === "/cli/apply-report") {
456
497
  const body = await readJson(req);
457
- const s = sessions.byFile(String(body.file || ""));
498
+ const s = sessionFor(sessions, String(body.file || ""), body.holder);
458
499
  return sendJson(res, 200, sessions.noteApply(s.key, {
459
500
  ...leaseOf(body), run: typeof body.run === "string" ? body.run : "",
460
501
  problems: Array.isArray(body.problems) ? body.problems : [], applied: Array.isArray(body.applied) ? body.applied.filter((k) => typeof k === "string") : [],
@@ -462,12 +503,14 @@ export async function startServer({ port = 0 } = {}) {
462
503
  }
463
504
  if (req.method === "GET" && p === "/cli/poll") {
464
505
  const f = url.searchParams.get("file") || "";
506
+ checkBinding(f, url.searchParams.get("holder")); // before the file is looked up or opened
465
507
  let s;
466
508
  try {
467
509
  s = sessions.byFile(f);
468
510
  } catch {
469
- s = sessions.open(f); // a poller may start before (or after a server restart without) `open`
511
+ s = await sessions.openAsync(f); // a poller may start before (or after a server restart without) `open`
470
512
  }
513
+ await sessions.outlineAhead(s.key, url.searchParams.get("holder") || "agent"); // the outline a delivery carries, built off this thread
471
514
  const handle = sessions.poll(s.key, { ack: url.searchParams.get("ack") === "1", holder: url.searchParams.get("holder") || "agent", full: url.searchParams.get("full") === "1", compact: url.searchParams.get("compact") === "1" });
472
515
  res.on("close", () => handle.cancel());
473
516
  const out = await handle.promise;
@@ -476,7 +519,7 @@ export async function startServer({ port = 0 } = {}) {
476
519
  }
477
520
  if (req.method === "POST" && p === "/cli/done") {
478
521
  const body = await readJson(req);
479
- const s = sessions.byFile(String(body.file || ""));
522
+ const s = sessionFor(sessions, String(body.file || ""), body.holder);
480
523
  return sendJson(res, 200, await finishRun(s.key, body.results, leaseOf(body)));
481
524
  }
482
525
  if (req.method === "POST" && p === "/cli/stop") {
@@ -539,6 +582,7 @@ export async function startServer({ port = 0 } = {}) {
539
582
  await new Promise((resolve, reject) => (unix.once("error", reject), unix.listen(sock, resolve)));
540
583
  fs.chmodSync(sock, 0o600);
541
584
  writeStateFile(serverRecordPath(), JSON.stringify({ pid: process.pid, port: tcpPort, protocol: SERVER_PROTOCOL }));
585
+ warmAnchors(); // the anchor worker starts now, so the first Send does not wait for it
542
586
  });
543
587
 
544
588
  const idleMs = Number(env("IDLE_MS", 2 * 60 * 60 * 1000));