onionskin-cli 0.0.3 → 0.0.6

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 (109) hide show
  1. package/CHANGELOG.md +198 -0
  2. package/INSTALL.md +5 -1
  3. package/NOTICE +34 -0
  4. package/README.md +102 -34
  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/scripts/checker/check.py +24 -5
  9. package/scripts/checker/requirements.txt +482 -5
  10. package/scripts/requirements.txt +482 -5
  11. package/scripts/rewriter/apple/main.swift +1 -1
  12. package/scripts/rewriter/apple/onionskin-fm +0 -0
  13. package/scripts/rewriter/apple/onionskin-fm.json +1 -1
  14. package/skills/onionskin/SKILL.md +12 -9
  15. package/skills/onionskin/reference/batch.md +5 -3
  16. package/skills/onionskin/reference/formats.md +5 -1
  17. package/skills/onionskin/reference/ops.md +48 -4
  18. package/skills/onionskin/reference/troubleshooting.md +31 -0
  19. package/src/agent-detect.js +28 -2
  20. package/src/agent-prompt.js +12 -1
  21. package/src/agent-runner.js +110 -20
  22. package/src/anchor-work.js +154 -0
  23. package/src/anchor-worker.js +38 -0
  24. package/src/anchors.js +61 -11
  25. package/src/apply.js +119 -47
  26. package/src/batch.js +32 -8
  27. package/src/check-worker.js +8 -5
  28. package/src/checker.js +238 -33
  29. package/src/cli.js +248 -42
  30. package/src/constants.js +34 -1
  31. package/src/diff.js +3 -1
  32. package/src/doctor.js +12 -3
  33. package/src/docx.js +464 -87
  34. package/src/edit-hash.js +19 -0
  35. package/src/fsutil.js +14 -6
  36. package/src/history-worker.js +8 -8
  37. package/src/history.js +144 -31
  38. package/src/html-active.js +384 -0
  39. package/src/html-envelope.js +51 -0
  40. package/src/html-parser.js +8 -0
  41. package/src/html-render-worker.js +25 -0
  42. package/src/html-render.js +939 -0
  43. package/src/html-utf8.js +65 -0
  44. package/src/html-write-worker.js +14 -0
  45. package/src/html-write.js +380 -0
  46. package/src/html.js +1259 -0
  47. package/src/local.js +68 -4
  48. package/src/merge-package.js +1166 -0
  49. package/src/merge-pptx.js +98 -38
  50. package/src/merge-proof.js +1019 -0
  51. package/src/merge-worker.js +60 -0
  52. package/src/merge-xlsx.js +84 -29
  53. package/src/merge.js +286 -83
  54. package/src/nfc.js +46 -0
  55. package/src/notes.js +122 -0
  56. package/src/offload.js +419 -1
  57. package/src/open-worker.js +27 -0
  58. package/src/ops/common.js +2 -1
  59. package/src/ops/docx-table.js +5 -3
  60. package/src/ops/docx.js +58 -29
  61. package/src/ops/html.js +394 -0
  62. package/src/ops/pptx.js +82 -23
  63. package/src/ops/run.js +23 -3
  64. package/src/ops/runs.js +215 -13
  65. package/src/ops/schema.js +8 -1
  66. package/src/ops/xlsx.js +121 -10
  67. package/src/ops-worker.js +17 -0
  68. package/src/package.js +43 -4
  69. package/src/person.js +2 -1
  70. package/src/pptx.js +323 -23
  71. package/src/question.js +10 -3
  72. package/src/quick-plan.js +13 -6
  73. package/src/quick.js +1 -1
  74. package/src/redline-check.js +12 -2
  75. package/src/redline-worker.js +10 -14
  76. package/src/reply.js +46 -19
  77. package/src/retain.js +223 -0
  78. package/src/review.js +32 -18
  79. package/src/rewrite-check.js +2 -2
  80. package/src/rewriter.js +38 -8
  81. package/src/sanitize.js +16 -3
  82. package/src/server-entry.js +4 -3
  83. package/src/server.js +175 -62
  84. package/src/session.js +1151 -210
  85. package/src/setup.js +20 -2
  86. package/src/stop-gate.js +34 -0
  87. package/src/suggest-reply.js +42 -7
  88. package/src/suggest.js +14 -29
  89. package/src/text.js +161 -11
  90. package/src/update.js +517 -0
  91. package/src/voice-guide.js +15 -9
  92. package/src/voice-learn.js +15 -4
  93. package/src/voice-sample-worker.js +7 -6
  94. package/src/voice-store.js +11 -4
  95. package/src/voice-worker.js +4 -6
  96. package/src/voice.js +2 -1
  97. package/src/worker-host.js +23 -0
  98. package/src/xlsx.js +378 -97
  99. package/src/xml-tree.js +6 -3
  100. package/src/xml.js +82 -34
  101. package/src/zip-write.js +10 -5
  102. package/src/zip.js +73 -18
  103. package/vendor/parse5/LICENSE.txt +32 -0
  104. package/vendor/parse5/parse5.mjs +7822 -0
  105. package/vendor/parse5.lock.json +23 -0
  106. package/web/dist/assets/{index-DS-WST1E.js → index-CjsTf5gj.js} +58 -52
  107. package/web/dist/assets/index-DnbLtHhG.css +1 -0
  108. package/web/dist/index.html +2 -2
  109. package/web/dist/assets/index-BCAEKiOG.css +0 -1
@@ -1,13 +1,14 @@
1
1
  import { startServer } from "./server.js";
2
2
 
3
+ let srv;
3
4
  try {
4
- await startServer();
5
+ srv = await startServer();
5
6
  process.stdout.write("ready\n"); // the CLI waits for this line instead of polling
6
7
  } catch (err) {
7
8
  if (err?.code === "EALREADY") process.exit(0);
8
9
  console.error(err?.message || err);
9
10
  process.exit(1);
10
11
  }
11
- process.on("SIGTERM", () => process.exit(0));
12
- process.on("SIGINT", () => process.exit(0));
12
+ process.on("SIGTERM", () => srv.shutdown());
13
+ process.on("SIGINT", () => srv.shutdown());
13
14
  process.stdout.on("error", () => {});
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";
@@ -25,6 +28,7 @@ import { BG_PRESETS, MAX_IMAGE_BYTES, SettingsError, readCustomImage, readSettin
25
28
  const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
26
29
  const WEB_DIST = env("WEB_DIST", path.join(root, "web", "dist"));
27
30
  const MAX_BODY = 300 * 1024 * 1024; // base64 of a 200 MB document; ceiling: whole body buffered in memory. Upgrade path: binary upload route.
31
+ const META_ROUTES = new Set(["/api/claim", "/api/answer", "/api/review", "/api/suggestion", "/api/takeback", "/api/revert", "/api/show-copy", "/api/dismiss-conflict", "/api/close"]);
28
32
  const MIME = { ".html": "text/html; charset=utf-8", ".js": "text/javascript; charset=utf-8", ".css": "text/css; charset=utf-8", ".json": "application/json", ".svg": "image/svg+xml", ".png": "image/png", ".woff2": "font/woff2", ".ico": "image/x-icon", ".map": "application/json" };
29
33
 
30
34
  function secure(res) {
@@ -32,6 +36,7 @@ function secure(res) {
32
36
  res.setHeader("Referrer-Policy", "no-referrer");
33
37
  res.setHeader("X-Content-Type-Options", "nosniff");
34
38
  res.setHeader("Cache-Control", "no-store");
39
+ res.setHeader("X-DNS-Prefetch-Control", "off"); // PLAN-html R3, R21: no DNS prefetch for anything the page or an HTML view names
35
40
  }
36
41
  function sendJson(res, status, body) {
37
42
  secure(res);
@@ -44,23 +49,32 @@ function sendText(res, status, text) {
44
49
  res.writeHead(status, { "Content-Type": "text/plain; charset=utf-8" });
45
50
  res.end(text);
46
51
  }
47
- function readBody(req, max = MAX_BODY) {
52
+ // 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
53
+ // 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
54
+ // that the connection is closed.
55
+ function readBody(req, max = MAX_BODY, refusal = null) {
48
56
  return new Promise((resolve, reject) => {
49
57
  const chunks = [];
50
58
  let n = 0;
59
+ let over = false;
51
60
  req.on("data", (c) => {
52
61
  n += c.length;
62
+ if (over) {
63
+ if (n > max + MAX_BODY) req.destroy();
64
+ return;
65
+ }
53
66
  if (n > max) {
54
- reject(new HttpError(413, "body too large"));
55
- req.destroy();
67
+ over = true;
68
+ chunks.length = 0;
69
+ reject(refusal || new HttpError(413, "body too large", { code: "too_large_request" }));
56
70
  } else chunks.push(c);
57
71
  });
58
72
  req.on("end", () => resolve(Buffer.concat(chunks)));
59
73
  req.on("error", reject);
60
74
  });
61
75
  }
62
- async function readJson(req, max = MAX_BODY) {
63
- const raw = await readBody(req, max);
76
+ async function readJson(req, max = MAX_BODY, refusal = null) {
77
+ const raw = await readBody(req, max, refusal);
64
78
  try {
65
79
  return raw.length ? JSON.parse(raw.toString("utf8")) : {};
66
80
  } catch {
@@ -70,6 +84,8 @@ async function readJson(req, max = MAX_BODY) {
70
84
  function fail(res, err) {
71
85
  if (err instanceof HttpError) return sendJson(res, err.status, { error: err.message, ...err.extra });
72
86
  if (err instanceof SettingsError) return sendJson(res, err.status, { error: err.message });
87
+ // a document past a reader's cap (src/xml.js): plain words a person can act on, whatever route read it
88
+ if (err instanceof CapError) return sendJson(res, 422, { error: `Onionskin cannot read this file: ${err.message}.`, code: "too_large" });
73
89
  console.error("internal error:", err.stack || err);
74
90
  return sendJson(res, 500, { error: "internal error" });
75
91
  }
@@ -95,20 +111,44 @@ function serveIndex(res, launchToken) {
95
111
  res.end(raw);
96
112
  }
97
113
 
114
+ function decodeUriPart(s) {
115
+ try {
116
+ return decodeURIComponent(s);
117
+ } catch {
118
+ throw new HttpError(400, "invalid URL encoding");
119
+ }
120
+ }
121
+
98
122
  function serveStatic(res, urlPath) {
99
- const rel = decodeURIComponent(urlPath).replace(/^\/+/, "");
100
- const file = path.resolve(WEB_DIST, rel);
101
- if (file !== WEB_DIST && !file.startsWith(WEB_DIST + path.sep)) return sendText(res, 403, "forbidden");
123
+ const rel = decodeUriPart(urlPath).replace(/^\/+/, "");
124
+ const dist = path.resolve(WEB_DIST);
125
+ const file = path.resolve(dist, rel);
126
+ if (file !== dist && !file.startsWith(dist + path.sep)) return sendText(res, 403, "forbidden");
127
+ let realDist;
128
+ let realFile;
102
129
  let st;
103
130
  try {
104
- st = fs.statSync(file);
131
+ realDist = fs.realpathSync(dist);
132
+ realFile = fs.realpathSync(file);
133
+ } catch {
134
+ return sendText(res, 404, "not found");
135
+ }
136
+ if (realFile !== realDist && !realFile.startsWith(realDist + path.sep)) return sendText(res, 403, "forbidden");
137
+ try {
138
+ st = fs.statSync(realFile);
105
139
  } catch {
106
140
  return sendText(res, 404, "not found");
107
141
  }
108
142
  if (!st.isFile()) return sendText(res, 404, "not found");
109
143
  secure(res);
110
144
  res.writeHead(200, { "Content-Type": MIME[path.extname(file)] || "application/octet-stream", "Content-Length": st.size });
111
- fs.createReadStream(file).pipe(res);
145
+ const stream = fs.createReadStream(realFile);
146
+ stream.on("error", () => {
147
+ if (!res.headersSent) sendText(res, 404, "not found");
148
+ else res.destroy();
149
+ });
150
+ res.on("close", () => stream.destroy());
151
+ stream.pipe(res);
112
152
  }
113
153
 
114
154
  const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
@@ -128,14 +168,22 @@ async function withStartLock(fn) {
128
168
  break;
129
169
  } catch (err) {
130
170
  if (err.code !== "EEXIST") throw err;
131
- let alive = false;
171
+ let stale = false;
132
172
  try {
133
- process.kill(Number(fs.readFileSync(lock, "utf8")), 0);
134
- alive = true;
135
- } catch (e) {
136
- alive = e.code === "EPERM";
173
+ const pid = Number(fs.readFileSync(lock, "utf8"));
174
+ if (Number.isInteger(pid) && pid > 0) {
175
+ try {
176
+ process.kill(pid, 0);
177
+ } catch (e) {
178
+ stale = e.code !== "EPERM";
179
+ }
180
+ } else {
181
+ stale = Date.now() - fs.statSync(lock).mtimeMs > 2000;
182
+ }
183
+ } catch {
184
+ continue;
137
185
  }
138
- if (!alive) {
186
+ if (stale) {
139
187
  try {
140
188
  fs.unlinkSync(lock);
141
189
  } catch {}
@@ -158,6 +206,10 @@ export async function startServer({ port = 0 } = {}) {
158
206
  ensureStateDir();
159
207
  const sessions = new Sessions();
160
208
  const sse = new Map(); // key -> Set<res>
209
+ // key -> when a page of that session last spoke (its event stream opened or ended, any other request): `onionskin update` holds
210
+ // 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
211
+ const tabSeen = new Map();
212
+ const tabsOf = (key) => sse.get(key)?.size || 0;
161
213
  // LibreOffice checks running after `done` returned (session key -> Set<{cancel}>). Cancelled when the session closes or the server stops.
162
214
  const late = new Map();
163
215
  function startLate(key, inputs, analysis) {
@@ -188,7 +240,7 @@ export async function startServer({ port = 0 } = {}) {
188
240
  let analysis, done;
189
241
  try {
190
242
  analysis = inputs ? await analyzeOffThread(inputs) : null; // the server keeps answering while the checks run
191
- done = sessions.done(s.key, results, claim, analysis);
243
+ done = await sessions.doneAsync(s.key, results, claim, analysis, { inline: false }); // the checks and the anchors never run on this thread
192
244
  } catch (err) {
193
245
  if (delivery && s.delivered === delivery && stageBefore) sessions.setStage(s.key, stageBefore); // done did not finish and this delivery is still the current one
194
246
  throw err;
@@ -203,7 +255,7 @@ export async function startServer({ port = 0 } = {}) {
203
255
  sessions.local = local;
204
256
  sessions.on("local", (s, ticket) => setImmediate(() => local.run(s.key, ticket)));
205
257
  // 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 });
258
+ const agent = agentRunner(sessions, { tabs: tabsOf });
207
259
  sessions.agent = agent;
208
260
  // a faster outside-edit notice (src/watch.js); the page's /api/stale poll stays the backstop
209
261
  const watcher = docWatcher(sessions);
@@ -248,7 +300,8 @@ export async function startServer({ port = 0 } = {}) {
248
300
  const bad = auth.check(req, { mutating, queryToken: isEvents ? url.searchParams.get("t") || "" : "" });
249
301
  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
302
  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() });
303
+ // a page that loads (or reloads) while a Send of this file is in flight starts from what it committed (Sessions.settled)
304
+ if (isBootstrap) return sendJson(res, 200, { session: (await sessions.settled(key), sessions.view(sessions.get(key))), product: PRODUCT.name, settings: readSettings(), suggest: suggestStatus() });
252
305
  // per-machine settings and the background image: not tied to a session, so any tab (a read-only one too) may use them
253
306
  if (p === "/api/settings" || p.startsWith("/api/background")) return await settingsRoute(req, res, p);
254
307
  // own stamps are per machine too: a list of {label, words} the person typed, any tab may use it
@@ -273,6 +326,7 @@ export async function startServer({ port = 0 } = {}) {
273
326
  const tab = String(req.headers["x-onionskin-tab"] || url.searchParams.get("tab") || "").slice(0, 64);
274
327
  if (isEvents) return events(req, res, key, tab);
275
328
  const s = sessions.get(key);
329
+ tabSeen.set(key, Date.now());
276
330
  if (req.method === "GET" && p === "/api/file") {
277
331
  const { bytes, diskSha } = sessions.loadForPage(key); // records an outside change the user is taking (history)
278
332
  warm(s);
@@ -280,22 +334,45 @@ export async function startServer({ port = 0 } = {}) {
280
334
  res.writeHead(200, { "Content-Type": "application/octet-stream", "Content-Length": bytes.length, "X-Sha256": diskSha });
281
335
  return res.end(bytes);
282
336
  }
337
+ if (req.method === "GET" && p === "/api/html-view") {
338
+ // 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)
339
+ const v = await sessions.htmlView(key, { plain: url.searchParams.get("plain") === "1" });
340
+ warm(s);
341
+ 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 });
342
+ secure(res);
343
+ res.writeHead(200, { "Content-Type": "application/octet-stream", "Content-Length": prefix.length + copy.length, "X-Sha256": v.sha });
344
+ res.write(prefix);
345
+ return res.end(copy);
346
+ }
283
347
  if (req.method === "GET" && p === "/api/stale") return sendJson(res, 200, sessions.staleCheck(key, url.searchParams.get("sha") || ""));
284
348
  if (req.method === "GET" && p === "/api/history") return sendJson(res, 200, await sessions.history(key));
285
- 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)));
349
+ if (req.method === "GET" && p.startsWith("/api/history/")) return sendJson(res, 200, await sessions.historyUpdate(key, decodeUriPart(p.slice("/api/history/".length)).slice(0, 64)));
286
350
  if (req.method !== "POST") return sendJson(res, 405, { error: "method not allowed" });
287
351
  // the preview of a quick change (src/quick-plan.js): read only, so no editor claim; it answers for the file as it is on disk now
288
352
  if (p === "/api/quick") return sendJson(res, 200, await quickPreview(s, String((await readJson(req, 8 * 1024))?.note ?? "")));
289
353
  // "Build my voice guide" (src/voice-learn.js): the editing tab only, with its own body cap (five 20 MB files in base64 plus 1 MB)
290
354
  if (p === "/api/voice/build") {
291
355
  sessions.requireEditor(key, tab);
292
- return sendJson(res, 200, { ok: true, session: (await buildVoiceRequest(sessions, key, await readJson(req, VOICE_BUILD_MAX_BODY)), sessions.view(s)) });
356
+ const body = await readJson(req, VOICE_BUILD_MAX_BODY);
357
+ await buildVoiceRequest(sessions, key, body);
358
+ return sendJson(res, 200, { ok: true, session: sessions.view(s) });
293
359
  }
294
360
  // starts the writing helper of quick fixes if it is cold (src/suggest.js); carries no text, changes nothing
295
361
  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
362
+ // 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
363
+ const bodyMax =
364
+ p === "/api/export-tracked" || p === "/api/agent" ? 1024 :
365
+ p === "/api/suggest" ? 8 * 1024 :
366
+ META_ROUTES.has(p) ? 16 * 1024 :
367
+ p === "/api/draft" || p === "/api/handover" ? REQUEST_MAX :
368
+ MAX_BODY;
369
+ const body = await readJson(req, bodyMax, p === "/api/draft" || p === "/api/handover" ? tooLarge("This draft") : null);
370
+ // 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
371
+ 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
372
  if (p === "/api/claim" && !/^[\w-]{6,64}$/.test(tab)) return sendJson(res, 400, { error: "a tab id is required" });
298
373
  if (p === "/api/claim") return sendJson(res, 200, { ok: sessions.claimEditor(key, tab, { takeover: body.takeover === true }), session: sessions.view(s) });
374
+ // the tab that just lost editing hands over its unsaved words (Sessions.handOver checks that it is that tab), in the file's turn
375
+ 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
376
  if (p !== "/api/resolve") sessions.requireEditor(key, tab);
300
377
  // 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
378
  if (p === "/api/agent") {
@@ -308,18 +385,20 @@ export async function startServer({ port = 0 } = {}) {
308
385
  await agent.action(key, { action: body.action, cli: body.cli, on: body.on, takeBack: body.takeBack === true });
309
386
  return sendJson(res, 200, { ok: true, session: sessions.view(s) });
310
387
  }
311
- if (p === "/api/draft") return sendJson(res, 200, { ok: true, session: (sessions.setDraft(key, body), sessions.view(s)) });
388
+ // 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)
389
+ 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
390
  // 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)) });
391
+ 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
392
  // review first (src/review.js): a button's kind, then taps on its cards (ids and an integer, never text)
315
- if (p === "/api/review") return sendJson(res, 200, { ok: true, session: (sessions.requestReview(key, body.kind), sessions.view(s)) });
316
- if (p === "/api/suggestion") return sendJson(res, 200, { ok: true, session: (sessions.answerSuggestions(key, { ids: body.ids, choice: body.choice }), sessions.view(s)) });
393
+ if (p === "/api/review") return sendJson(res, 200, { ok: true, session: (await sessions.inTurn(s.key, () => sessions.requestReview(key, body.kind)), sessions.view(s)) });
394
+ if (p === "/api/suggestion") return sendJson(res, 200, { ok: true, session: (await sessions.inTurn(s.key, () => sessions.answerSuggestions(key, { ids: body.ids, choice: body.choice })), sessions.view(s)) });
317
395
  // quick fixes (src/suggest.js): asking writes nothing; Use names the stored suggestion by id and writes it on this Mac
318
396
  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)) });
397
+ if (p === "/api/suggest/use") return sendJson(res, 200, { ok: true, session: (await useSuggestion(sessions, key, { ...body, tab }), sessions.view(s)) });
320
398
  if (p === "/api/send") {
321
399
  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 });
400
+ // an HTML file's text edits (PLAN-html R9) come as {nodeId, versionSha, baseTextHash, newText}, never as bytes
401
+ 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
402
  warm(s);
324
403
  return sendJson(res, 200, { ok: true, session: sessions.view(s) });
325
404
  }
@@ -329,9 +408,9 @@ export async function startServer({ port = 0 } = {}) {
329
408
  warm(s);
330
409
  return sendJson(res, 200, { ok: true, ...r, session: sessions.view(s) });
331
410
  }
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));
411
+ if (p === "/api/takeback") return sendJson(res, 200, { ok: true, session: (await sessions.inTurn(key, () => sessions.takeBack(key)), sessions.view(s)) });
412
+ if (p === "/api/revert") return sendJson(res, 200, { ok: true, session: (await sessions.revertAsync(key, String(body.id || "")), warm(s), sessions.view(s)) });
413
+ if (p === "/api/resolve") return sendJson(res, 200, await sessions.resolveAllAsync(key));
335
414
  if (p === "/api/export-tracked") return sendJson(res, 200, await sessions.exportTracked(key, String(body.id || "").slice(0, 64)));
336
415
  if (p === "/api/show-copy") return sendJson(res, 200, sessions.showCopy(String(body.path || "")));
337
416
  if (p === "/api/dismiss-conflict") return sendJson(res, 200, { ok: true, session: (sessions.dismissConflict(key), sessions.view(s)) });
@@ -385,26 +464,50 @@ export async function startServer({ port = 0 } = {}) {
385
464
  return sendJson(res, 200, await status());
386
465
  }
387
466
 
467
+ function dropSse(key, res) {
468
+ const set = sse.get(key);
469
+ if (!set) return;
470
+ if (set.delete(res)) tabSeen.set(key, Date.now());
471
+ if (!set.size) sse.delete(key);
472
+ }
473
+ function writeSse(key, res, chunk) {
474
+ try {
475
+ res.write(chunk);
476
+ return true;
477
+ } catch {
478
+ dropSse(key, res);
479
+ try { res.destroy(); } catch {}
480
+ return false;
481
+ }
482
+ }
388
483
  function events(req, res, key, tab) {
389
484
  const s = sessions.get(key);
390
- if (/^[\w-]{6,64}$/.test(tab)) {
391
- sessions.claimEditor(key, tab);
392
- req.on("close", () => sessions.releaseEditor(key, tab));
393
- }
485
+ const hasTab = /^[\w-]{6,64}$/.test(tab);
486
+ if (hasTab) sessions.claimEditor(key, tab);
487
+ let cleaned = false;
488
+ const cleanup = () => {
489
+ if (cleaned) return;
490
+ cleaned = true;
491
+ if (hasTab) sessions.releaseEditor(key, tab);
492
+ dropSse(key, res);
493
+ };
494
+ req.on("close", cleanup);
495
+ res.on("error", cleanup);
496
+ res.on("close", cleanup);
394
497
  secure(res);
395
498
  res.writeHead(200, { "Content-Type": "text/event-stream", "Cache-Control": "no-store", Connection: "keep-alive" });
396
- res.write(`event: state\ndata: ${JSON.stringify(sessions.view(s))}\n\n`);
397
499
  if (!sse.has(key)) sse.set(key, new Set());
398
500
  sse.get(key).add(res);
399
- req.on("close", () => sse.get(key)?.delete(res));
501
+ tabSeen.set(key, Date.now());
502
+ writeSse(key, res, `event: state\ndata: ${JSON.stringify(sessions.view(s))}\n\n`);
400
503
  }
401
504
  sessions.on("change", (s, event) => {
402
505
  const set = sse.get(s.key);
403
506
  if (!set?.size) return;
404
507
  const view = JSON.stringify(sessions.view(s));
405
- for (const res of set) {
406
- if (event === "reload") res.write(`event: reload\ndata: ${view}\n\n`);
407
- res.write(`event: state\ndata: ${view}\n\n`);
508
+ for (const res of [...set]) {
509
+ if (event === "reload" && !writeSse(s.key, res, `event: reload\ndata: ${view}\n\n`)) continue;
510
+ writeSse(s.key, res, `event: state\ndata: ${view}\n\n`);
408
511
  }
409
512
  });
410
513
 
@@ -417,44 +520,46 @@ export async function startServer({ port = 0 } = {}) {
417
520
  if (p === "/health") return sendJson(res, 200, { ok: true, pid: process.pid, protocol: SERVER_PROTOCOL, sessions: sessions.map.size });
418
521
  if (req.method === "POST" && p === "/cli/open") {
419
522
  const { file, agent: wants } = await readJson(req);
420
- const s = sessions.open(String(file || ""), { agent: wants === "background" ? "background" : undefined });
523
+ const s = await sessions.openAsync(String(file || ""), { agent: wants === "background" ? "background" : undefined });
421
524
  if (wants === "background") agent.ensure(s.key); // a person opened it from a terminal: the background agent works on it
422
525
  const nonce = auth.newNonce();
423
526
  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
527
  }
425
528
  if (req.method === "GET" && p === "/cli/status") {
426
- return sendJson(res, 200, cliStatus(sessions, sessions.byFile(url.searchParams.get("file") || "")));
529
+ return sendJson(res, 200, cliStatus(sessions, sessionFor(sessions, url.searchParams.get("file") || "", url.searchParams.get("holder"))));
530
+ }
531
+ if (req.method === "GET" && p === "/cli/open-work") {
532
+ // what `onionskin update` would cut off by restarting the server: counts only, no file names
533
+ return sendJson(res, 200, openWork(sessions, { tabs: tabsOf, seen: (key) => tabSeen.get(key) ?? null }));
427
534
  }
428
535
  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 });
536
+ // which file a delivered batch belongs to (for `reply --lease <ticket>` without a file): only for the whole ticket (batch id and token)
537
+ return sendJson(res, 200, { file: batchFile(sessions, claimOfQuery(url.searchParams)) });
433
538
  }
434
539
  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 });
540
+ // the batch delivered to the agent (anchors for `apply`); with a claim, only for the lease holder (src/local.js cliBatch)
541
+ const claim = claimOfQuery(url.searchParams);
542
+ return sendJson(res, 200, cliBatch(sessions, sessionFor(sessions, url.searchParams.get("file") || "", claim.holder), claim));
438
543
  }
439
544
  if (req.method === "POST" && p === "/cli/agent-write") {
440
545
  const body = await readJson(req);
441
- const s = sessions.byFile(String(body.file || ""));
546
+ const s = sessionFor(sessions, String(body.file || ""), body.holder);
442
547
  const bytes = typeof body.bytes_b64 === "string" ? Buffer.from(body.bytes_b64, "base64") : null;
443
548
  // `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) }));
549
+ 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
550
  }
446
551
  if (req.method === "POST" && p === "/cli/commit") {
447
552
  // 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
553
  // Without base_sha256 the base is the hash this server last wrote or delivered for the batch.
449
554
  const body = await readJson(req);
450
- const s = sessions.byFile(String(body.file || ""));
555
+ const s = sessionFor(sessions, String(body.file || ""), body.holder);
451
556
  const bytes = typeof body.bytes_b64 === "string" ? Buffer.from(body.bytes_b64, "base64") : null;
452
557
  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) }));
558
+ return sendJson(res, 200, await sessions.agentWriteChecked(s.key, { baseSha, bytes, label: "before agent commit", ...leaseOf(body) }));
454
559
  }
455
560
  if (req.method === "POST" && p === "/cli/apply-report") {
456
561
  const body = await readJson(req);
457
- const s = sessions.byFile(String(body.file || ""));
562
+ const s = sessionFor(sessions, String(body.file || ""), body.holder);
458
563
  return sendJson(res, 200, sessions.noteApply(s.key, {
459
564
  ...leaseOf(body), run: typeof body.run === "string" ? body.run : "",
460
565
  problems: Array.isArray(body.problems) ? body.problems : [], applied: Array.isArray(body.applied) ? body.applied.filter((k) => typeof k === "string") : [],
@@ -462,21 +567,25 @@ export async function startServer({ port = 0 } = {}) {
462
567
  }
463
568
  if (req.method === "GET" && p === "/cli/poll") {
464
569
  const f = url.searchParams.get("file") || "";
570
+ checkBinding(f, url.searchParams.get("holder")); // before the file is looked up or opened
571
+ let handle;
572
+ res.once("close", () => handle?.cancel());
465
573
  let s;
466
574
  try {
467
575
  s = sessions.byFile(f);
468
576
  } catch {
469
- s = sessions.open(f); // a poller may start before (or after a server restart without) `open`
577
+ s = await sessions.openAsync(f); // a poller may start before (or after a server restart without) `open`
470
578
  }
471
- 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
- res.on("close", () => handle.cancel());
579
+ await sessions.outlineAhead(s.key, url.searchParams.get("holder") || "agent"); // the outline a delivery carries, built off this thread
580
+ if (res.destroyed) return;
581
+ 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" });
473
582
  const out = await handle.promise;
474
583
  if (res.destroyed) return;
475
584
  return sendJson(res, 200, out);
476
585
  }
477
586
  if (req.method === "POST" && p === "/cli/done") {
478
587
  const body = await readJson(req);
479
- const s = sessions.byFile(String(body.file || ""));
588
+ const s = sessionFor(sessions, String(body.file || ""), body.holder);
480
589
  return sendJson(res, 200, await finishRun(s.key, body.results, leaseOf(body)));
481
590
  }
482
591
  if (req.method === "POST" && p === "/cli/stop") {
@@ -539,19 +648,23 @@ export async function startServer({ port = 0 } = {}) {
539
648
  await new Promise((resolve, reject) => (unix.once("error", reject), unix.listen(sock, resolve)));
540
649
  fs.chmodSync(sock, 0o600);
541
650
  writeStateFile(serverRecordPath(), JSON.stringify({ pid: process.pid, port: tcpPort, protocol: SERVER_PROTOCOL }));
651
+ warmAnchors(); // the anchor worker starts now, so the first Send does not wait for it
542
652
  });
543
653
 
544
654
  const idleMs = Number(env("IDLE_MS", 2 * 60 * 60 * 1000));
545
655
  const timer = setInterval(() => {
546
656
  sessions.tick();
547
657
  agent.tick();
548
- for (const set of sse.values()) for (const r of set) r.write(": ping\n\n");
658
+ for (const [k, set] of [...sse]) for (const r of [...set]) writeSse(k, r, ": ping\n\n");
549
659
  // the background agent's own polls do not keep the server up (it stops after 20 minutes without a tab; then the server may stop too)
550
660
  const busy = [...sessions.map.values()].some((s) => s.pollers.some((p) => !isBgHolder(p.holder)) || s.delivered);
551
661
  if (!busy && ![...sse.values()].some((x) => x.size) && Date.now() - lastActivity > idleMs) shutdown();
552
662
  }, Number(env("TICK_MS", 1000)));
553
663
 
664
+ let shuttingDown = false;
554
665
  function shutdown() {
666
+ if (shuttingDown) return;
667
+ shuttingDown = true;
555
668
  clearInterval(timer);
556
669
  watcher.close();
557
670
  for (const k of [...late.keys()]) cancelLate(k, "the server stopped"); // no soffice outlives the server