onionskin-cli 0.0.2 → 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 (93) hide show
  1. package/CHANGELOG.md +111 -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 +77 -19
  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 +8 -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 +88 -37
  70. package/src/session.js +1143 -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/watch.js +188 -0
  81. package/src/worker-host.js +23 -0
  82. package/src/xlsx.js +355 -94
  83. package/src/xml-tree.js +6 -3
  84. package/src/xml.js +82 -34
  85. package/src/zip-write.js +2 -1
  86. package/src/zip.js +9 -5
  87. package/vendor/parse5/LICENSE.txt +32 -0
  88. package/vendor/parse5/parse5.mjs +7822 -0
  89. package/vendor/parse5.lock.json +23 -0
  90. package/web/dist/assets/index-DkNx0LSH.css +1 -0
  91. package/web/dist/assets/{index-BUtofdtr.js → index-DytKkMdi.js} +117 -107
  92. package/web/dist/index.html +2 -2
  93. package/web/dist/assets/index-Dfizlg0i.css +0 -1
package/src/batch.js CHANGED
@@ -1,11 +1,13 @@
1
1
  import crypto from "node:crypto";
2
2
  import path from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
- import { PRODUCT } from "./constants.js";
4
+ import { PRODUCT, unknownFormat } from "./constants.js";
5
5
  import { untrusted, userText } from "./sanitize.js";
6
6
  import { pptxLocator } from "./pptx.js";
7
+ import { htmlLocator } from "./html.js";
7
8
  import { GUIDE_MAX, SECTIONS, SHORT_MAX } from "./voice-guide.js";
8
9
  import { xlsxLocator } from "./xlsx.js";
10
+ import { NOTE_MAX } from "./retain.js";
9
11
 
10
12
  const REFERENCE = "The skill's SKILL.md and reference/ files (batch.md, ops.md, troubleshooting.md) hold the full rules and every op.";
11
13
 
@@ -32,6 +34,13 @@ const EXAMPLES = [
32
34
  { format: "xlsx", description: "Set the cell or range an anchor names; expect.text_hash is the anchor's locator.text_hash.", op: { op: "set_cells", target: { anchor_id: "a1" }, expect: { text_hash: "<locator.text_hash of a1>" }, values: "<new value or =formula>" } },
33
35
  { format: "pptx", description: "Replace text inside the shape an anchor names.", op: { op: "replace_text_in_shape", target: { anchor_id: "a1" }, expect: { text_hash: "<locator.text_hash of a1>" }, find: "<text inside the shape>", replace: "<new text>" } },
34
36
  { format: "docx", description: "Replace text inside the paragraph an anchor names.", op: { op: "replace_text", target: { anchor_id: "a1" }, expect: { text_hash: "<locator.text_hash of a1>" }, find: "<text inside the paragraph>", replace: "<new text>" } },
37
+ { format: "html", description: "Replace text inside the element an anchor names (inside one text: not across a link or bold text).", op: { op: "replace_text", target: { anchor_id: "a1" }, expect: { text_hash: "<locator.text_hash of a1>" }, find: "<text inside the element>", replace: "<new text>" } },
38
+ { format: "html", description: "Add markup after the element an anchor names (text, formatting, lists, tables, local images and links only).", op: { op: "insert_html", target: { anchor_id: "a1" }, expect: { text_hash: "<locator.text_hash of a1>" }, where: "after", html: "<p>New paragraph with a <a href=\"https://example.com\">link</a>.</p>" } },
39
+ ];
40
+ // HTML (PLAN-html R1): what a write may add, and what the refusal looks like
41
+ const HTML_RULES = [
42
+ "HTML: code that runs in a browser cannot be added or changed (scripts, event handlers such as onclick, frames, forms, stylesheet links, remote images, data- attributes, URLs other than http, https, mailto, tel or relative); script and style elements cannot be edited. Such a write is refused whole with code active_content: \"The agent tried to add code that runs in a browser, so nothing was written.\"",
43
+ "HTML: replace_text works inside one text; a match across an element's edge answers spans_markup: use replace_element with the element's new markup.",
35
44
  ];
36
45
 
37
46
  /** Static, machine-readable instructions for editing the file (no document text in it). The long rules live in the skill's reference files, not in every batch. */
@@ -52,6 +61,7 @@ function howToApply(file, batchId = "batch", format = null) {
52
61
  `Or with apply (same engine as the browser): target ops by anchor id and give each an expect copied from what you read. ${REFERENCE}`,
53
62
  `If you edit with other tools, never regenerate the file from scratch and never write the document's own path: edit a COPY, then run \`${PRODUCT.bin} commit ${shellQuote(file)} --from <copy> --lease <ticket>\` (the same checked write as apply; \`done\` flags a file that changed any other way).`,
54
63
  "Anchors with protected: true cannot be edited losslessly; apply skips them (see protected_reason).",
64
+ ...(format === "html" ? HTML_RULES : []),
55
65
  ],
56
66
  examples: EXAMPLES.filter((e) => !format || e.format === format),
57
67
  };
@@ -66,7 +76,9 @@ export function replyWithOf(anchor, format) {
66
76
  if (!anchor.locator.text_hash) return "ops";
67
77
  if (format === "xlsx") return "set";
68
78
  if (format === "pptx") return anchor.unit === "shape" ? "find_replace_or_text" : "ops";
69
- return "find_replace_or_text";
79
+ if (format === "docx") return "find_replace_or_text";
80
+ if (format === "html") return anchor.unit === "text" ? "find_replace_or_text" : "ops"; // an element that holds text (replace_text, set_text); an image or a container: ops
81
+ return unknownFormat(format, "replyWithOf");
70
82
  }
71
83
 
72
84
  /** A comment the person answered: a stored question and an index that is in range of its options. */
@@ -96,21 +108,25 @@ const MAX_VALUES = { rows: 20, cols: 10 };
96
108
  const unitOfXlsx = (d) => (d.cell ? (d.r0 === d.r1 && d.c0 === d.c1 ? "cell" : "range") : /^\d+:\d+$/.test(d.range) ? "rows" : /^[A-Za-z]+:[A-Za-z]+$/.test(d.range) ? "columns" : "sheet");
97
109
 
98
110
  /** Format-specific evidence of a durable anchor: locator, quote, unit and context. All strings are document-derived and sanitized by the caller. */
99
- function evidenceOf(d, extra) {
111
+ function evidenceOf(d, extra, clean = (f) => untrusted(f, 100)) {
100
112
  if (d?.format === "xlsx") {
101
- const values = d.cell ? d.text.split("\n").slice(0, MAX_VALUES.rows).map((r) => r.split("\t").slice(0, MAX_VALUES.cols).map((v) => untrusted(v, 200))) : undefined;
113
+ // the anchor keeps its top-left cells (src/xlsx.js rangeOf); one stored by an earlier version has its whole text instead
114
+ const grid = Array.isArray(d.values) ? d.values : d.cell ? d.text.split("\n").map((r) => r.split("\t")) : null;
115
+ const values = d.cell ? grid.slice(0, MAX_VALUES.rows).map((r) => r.slice(0, MAX_VALUES.cols).map((v) => untrusted(v, 200))) : undefined;
102
116
  const c = d.ctx;
103
117
  return {
104
118
  unit: unitOfXlsx(d),
105
119
  locator: { ...xlsxLocator(d), text_hash: d.cell ? d.rangeHash : null },
106
120
  quote: d.text,
107
121
  ...(values ? { values } : {}),
108
- ...(d.cell && d.types ? { cell_types: d.types, number_formats: d.formats.map((r) => r.map((f) => untrusted(f, 100))) } : {}),
122
+ ...(d.cell && d.types ? { cell_types: d.types, number_formats: d.formats.map((r) => r.map(clean)) } : {}),
109
123
  ...(c ? { context: { row_header: untrusted(c.rowHeader, 120), column_header: untrusted(c.colHeader, 120), left: untrusted(c.left, 120), right: untrusted(c.right, 120), above: untrusted(c.up, 120), below: untrusted(c.down, 120) } } : {}),
110
124
  };
111
125
  }
112
126
  if (d?.format === "pptx") return { unit: d.kind, locator: pptxLocator(d), quote: d.quote || d.text, prefix: d.prefix, suffix: d.suffix };
113
- return null;
127
+ if (d?.format === "html") return { unit: d.kind, locator: htmlLocator(d), quote: d.quote || d.text, prefix: d.prefix, suffix: d.suffix };
128
+ if (!d || d.format === undefined || d.format === "docx") return null; // Word's evidence is built by the caller (no anchor: unresolved)
129
+ return unknownFormat(d.format, "evidenceOf");
114
130
  }
115
131
 
116
132
  /**
@@ -124,9 +140,16 @@ export function buildBatch({ file, format, baseSha, note, comments, edits, style
124
140
  const batchId = `b_${crypto.randomBytes(6).toString("hex")}`;
125
141
  const anchors = [];
126
142
  let n = 0;
143
+ // a number format is sanitized once per batch: 500 anchors on one range repeat the same 200 formats
144
+ const formats = new Map();
145
+ const cleanFormat = (f) => {
146
+ let v = formats.get(f);
147
+ if (v === undefined) formats.set(f, (v = untrusted(f, 100)));
148
+ return v;
149
+ };
127
150
  const addAnchor = (d, extra = {}) => {
128
151
  const id = `a${++n}`;
129
- const ev = evidenceOf(d, extra);
152
+ const ev = evidenceOf(d, extra, cleanFormat);
130
153
  anchors.push({
131
154
  id,
132
155
  kind: extra.kind || "paragraph",
@@ -172,7 +195,8 @@ export function buildBatch({ file, format, baseSha, note, comments, edits, style
172
195
  format,
173
196
  base_sha256: baseSha,
174
197
  note: "user_edits are changes the user already made; they are in the file; do not redo or revert them",
175
- instructions: { overall_note: userText(note), ...(style ? { style_note: style } : {}), ...(guide ? { voice_guide: guide } : {}), ...(review ? { review: { kind: review.kind, ask: review.ask } } : {}), comments: instructionComments },
198
+ // the note at NOTE_MAX: a waiting batch's note and the next one together are never cut here (longer is refused at Send, src/session.js)
199
+ instructions: { overall_note: userText(note, NOTE_MAX), ...(style ? { style_note: style } : {}), ...(guide ? { voice_guide: guide } : {}), ...(review ? { review: { kind: review.kind, ask: review.ask } } : {}), comments: instructionComments },
176
200
  untrusted_document_evidence: { file_name: untrusted(path.basename(file)), anchors, user_edits: userEdits, ...(questions.length ? { questions } : {}), ...(units ? { units, ...(unitsMore ? { units_more: unitsMore } : {}) } : {}) },
177
201
  how_to_apply: howToApply(file, batchId, format),
178
202
  agent_rules: AGENT_RULES,
@@ -1,8 +1,11 @@
1
- import { parentPort, workerData } from "node:worker_threads";
1
+ import "./html-parser.js"; // an HTML file is read again at `done` (src/package.js htmlChecks): the parser lives in worker threads only
2
2
  import { analyze } from "./analyze.js";
3
3
 
4
- try {
5
- parentPort.postMessage({ ok: true, ...analyze(Buffer.from(workerData.pre), Buffer.from(workerData.post), workerData.format, { deferSoffice: !!workerData.deferSoffice }) });
6
- } catch (err) {
7
- parentPort.postMessage({ ok: false, error: String(err?.message || err) });
4
+ /** The `done` analysis (src/offload.js analyzeOffThread), run by src/worker-host.js. One message back: {ok, flags, checks, changes} or {ok: false, error}. */
5
+ export function work(data) {
6
+ try {
7
+ return { ok: true, ...analyze(Buffer.from(data.pre), Buffer.from(data.post), data.format, { deferSoffice: !!data.deferSoffice }) };
8
+ } catch (err) {
9
+ return { ok: false, error: String(err?.message || err) };
10
+ }
8
11
  }
package/src/cli.js CHANGED
@@ -4,7 +4,8 @@ import http from "node:http";
4
4
  import net from "node:net";
5
5
  import path from "node:path";
6
6
  import tty from "node:tty";
7
- import { spawn } from "node:child_process";
7
+ import os from "node:os";
8
+ import { spawn, spawnSync } from "node:child_process";
8
9
  import { fileURLToPath } from "node:url";
9
10
  import { PRODUCT, SERVER_PROTOCOL } from "./constants.js";
10
11
  import { holderFor, loadHolder, loadTicketRecord, parseTicket, saveHolder } from "./lease.js";
@@ -14,14 +15,14 @@ import { ReplyError, replyCommand } from "./reply.js";
14
15
  import { setupChecker } from "./checker.js";
15
16
  import { runSetup } from "./setup.js";
16
17
  import { expandShortcut, personOpenText, personPollText, pickAudience, shellQuote } from "./person.js";
17
- import { findClaude, findCodex, isAgentHolder } from "./agent-detect.js";
18
+ import { NOT_YOUR_FILE, agentMayUse, findClaude, findCodex, isAgentHolder } from "./agent-detect.js";
18
19
  import { BG_PRESETS, DEFAULT_BACKGROUND, MAX_IMAGE_BYTES, SettingsError, describeBackground, readSettings, saveCustomImage, writeBackground } from "./settings.js";
19
20
 
20
21
  const here = path.dirname(fileURLToPath(import.meta.url));
21
22
  const BIN = PRODUCT.bin;
22
23
  const HELP = `${PRODUCT.name}
23
24
 
24
- ${BIN} open <file.docx|xlsx|pptx> [--no-open] [--no-agent|--agent] Open the file in the browser editor (also: ${BIN} <file>). At a terminal it also starts Claude Code on it in the background; --no-agent only opens the page, --agent prints the agent's output
25
+ ${BIN} open <file.docx|xlsx|pptx|html|htm> [--no-open] [--no-agent|--agent] Open the file in the browser editor (also: ${BIN} <file>). At a terminal it also starts Claude Code on it in the background; --no-agent only opens the page, --agent prints the agent's output
25
26
  ${BIN} poll <file> [--ack] [--full] [--timeout <secs>] [--agent] Block until the user hits Send, print the batch as JSON (--full: the complete batch even when you already have the rules)
26
27
  ${BIN} status <file> [--ticket] Print session state without blocking (--ticket: re-read your delivery's ticket)
27
28
  ${BIN} apply <file> --ops <ops.json> --lease <ticket> [--dry-run] [--partial] [--summary] [--batch <batch.json>]
@@ -35,6 +36,10 @@ const HELP = `${PRODUCT.name}
35
36
  ${BIN} setup-checker Create the Python venv for the deeper checks at \`done\` (uses the network once)
36
37
  ${BIN} setup-rewriter [--remove] Turn on quick fixes on this Mac: quick spelling and grammar fixes on one sentence, with Apple's built-in model (macOS 26 or later, Apple Intelligence on; nothing is downloaded); --remove turns them off
37
38
  ${BIN} doctor [--json] [--verbose] Check the install (OK / MISSING / NOTE per line, fix command on each failure); exit 1 if a required check fails
39
+ ${BIN} update [--check] [--yes] [--force] Bring this copy to the latest release, the way it was installed (checks for a new version only when you run it)
40
+ --check: print this version and the latest one, change nothing. An npm copy gets the two npm commands (--yes runs them).
41
+ Refuses while a session has unsent edits, draft comments or a batch in flight (--force updates anyway)
42
+ ${BIN} utf8-copy <file.html|htm> Write a UTF-8 copy of a page saved as windows-1252 or ISO-8859-1 next to it (<name>.utf8.html, only when that name is free); the file itself is not changed
38
43
  ${BIN} shapes <file.pptx> [--slide <slide id>] List the shapes of a slide (id, name, placeholder, text preview) as JSON; read-only
39
44
  ${BIN} config background <${BG_PRESETS.join("|")}|default> Choose the background behind the editing UI (this machine)
40
45
  ${BIN} config background --image <file> [--strength 0-100] [--no-blur] Use your own PNG, JPEG or WebP (max 15 MB, scaled by the page)
@@ -56,7 +61,9 @@ export const FLAGS = {
56
61
  "setup-checker": [],
57
62
  "setup-rewriter": ["--model", "--remove", "--clean-old"],
58
63
  doctor: ["--json", "--verbose"],
64
+ update: ["--check", "--yes", "--force"],
59
65
  shapes: ["--slide"],
66
+ "utf8-copy": [],
60
67
  config: ["--image", "--strength", "--no-blur"],
61
68
  stop: [],
62
69
  };
@@ -90,10 +97,21 @@ function req(options, body) {
90
97
  });
91
98
  r.on("error", reject);
92
99
  if (options.signal) options.signal.addEventListener("abort", () => r.destroy(new Error("aborted")), { once: true });
100
+ // `deadline` (ms): the whole request, answer included, or it ends with `deadlineError`
101
+ if (options.deadline) {
102
+ const t = setTimeout(() => r.destroy(new Error(options.deadlineError || "the server did not answer in time")), options.deadline);
103
+ r.on("close", () => clearTimeout(t));
104
+ }
93
105
  if (body) r.write(JSON.stringify(body));
94
106
  r.end();
95
107
  });
96
108
  }
109
+ // Opening reads the file in a worker first: up to 60 s waiting for a free reader and 60 s reading (src/offload.js), so the CLI waits longer.
110
+ // ONIONSKIN_CLI_OPEN_DEADLINE_MS shortens it in tests.
111
+ const openRequest = (body) => {
112
+ const ms = Number(process.env[`${PRODUCT.envPrefix}_CLI_OPEN_DEADLINE_MS`]) || 150_000;
113
+ return req({ method: "POST", path: "/cli/open", deadline: ms, deadlineError: `Onionskin did not finish opening ${path.basename(String(body.file))} within ${Math.round(ms / 1000)} seconds. It may be very large or damaged, or this Mac is very busy. Try again, or open a smaller file` }, body);
114
+ };
97
115
  const json = (t) => {
98
116
  try {
99
117
  return JSON.parse(t);
@@ -160,6 +178,25 @@ async function removeStaleSocket() {
160
178
  }
161
179
  }
162
180
 
181
+ /** `stop` (and `update`): asks a running server to stop, waits for its process to end, then removes a leftover socket. */
182
+ async function stopServer() {
183
+ if (await healthy()) {
184
+ const pid = json((await req({ method: "GET", path: "/health", timeout: 1500 }).catch(() => ({ text: "" }))).text).pid;
185
+ await req({ method: "POST", path: "/cli/stop" }, {});
186
+ // the server answers first and exits a moment later: "stopped" is printed only once its process is gone (waits at most 5 s)
187
+ const gone = () => {
188
+ try {
189
+ process.kill(pid, 0);
190
+ return false;
191
+ } catch {
192
+ return true;
193
+ }
194
+ };
195
+ for (let i = 0; i < 100 && Number.isInteger(pid) && !gone(); i++) await new Promise((r) => setTimeout(r, 50));
196
+ }
197
+ await removeStaleSocket(); // a socket file nothing answers on is no use to anyone, and `doctor` would keep reporting it
198
+ }
199
+
163
200
  /** Starts the detached server and waits (event-based) for its "ready" line. */
164
201
  async function ensureServer() {
165
202
  ensureStateDir();
@@ -257,6 +294,8 @@ const realOrSame = (file) => {
257
294
  * a = {ack, full, compact, timeout}; `quiet` skips the human line on STDERR.
258
295
  */
259
296
  async function pollFor(file, a, { quiet = false } = {}) {
297
+ // an agent Onionskin started is bound to its one file: refused before anything about another file is asked
298
+ if (!agentMayUse(process.env[`${PRODUCT.envPrefix}_HOLDER`], file)) throw new Error(NOT_YOUR_FILE);
260
299
  // an agent Onionskin started (in the background or in Terminal) never starts a server, and never reconnects to one: a server that stopped means its review ended
261
300
  const bg = isAgentHolder(process.env[`${PRODUCT.envPrefix}_HOLDER`]); // the background agent, or the same agent opened in Terminal
262
301
  const serverGone = { status: "closed", reason: "the server stopped" };
@@ -286,14 +325,14 @@ async function pollFor(file, a, { quiet = false } = {}) {
286
325
  } catch (err) {
287
326
  clearTimeout(timer);
288
327
  if (ac.signal.aborted) continue; // deadline reached: loop returns the timeout
289
- if (/not found|no such session/i.test(err.message)) throw err;
328
+ if (/not found|no such session/i.test(err.message) || err.message === NOT_YOUR_FILE) throw err;
290
329
  if (bg) return serverGone;
291
330
  if (++failures > 5) throw err;
292
331
  process.stderr.write(`Lost the connection (${err.message}); reconnecting.\n`);
293
332
  await new Promise((r) => setTimeout(r, 1000 * failures));
294
333
  await ensureServer();
295
334
  // the session lives in the server's state dir; re-open it so the poll can register
296
- await req({ method: "POST", path: "/cli/open" }, { file });
335
+ await openRequest({ file });
297
336
  ack = false;
298
337
  }
299
338
  }
@@ -301,13 +340,15 @@ async function pollFor(file, a, { quiet = false } = {}) {
301
340
 
302
341
  /** `done`: reports `results` for the delivery named by `lease` (a ticket string) and returns the server's answer. Shared by `done` and `reply`. */
303
342
  async function finishBatch(file, results, lease) {
343
+ const envHolder = process.env[`${PRODUCT.envPrefix}_HOLDER`];
344
+ if (!agentMayUse(envHolder, file)) throw new Error(NOT_YOUR_FILE);
304
345
  if (!(await healthy())) throw new Error(`the ${PRODUCT.name} server is not running`);
305
346
  const realFile = realOrSame(file);
306
347
  // the only credential is the ticket printed in the batch's `done` command; a delivery without it is refused, with no per-file fallback
307
348
  const ticket = parseTicket(lease);
308
- const st = json((await req({ method: "GET", path: `/cli/status?file=${encodeURIComponent(realFile)}` })).text);
349
+ const st = json((await req({ method: "GET", path: `/cli/status?file=${encodeURIComponent(realFile)}${envHolder ? `&holder=${encodeURIComponent(envHolder)}` : ""}` })).text);
309
350
  if (!ticket && st.delivered_batch) throw new Error(`a batch is delivered for this file: pass --lease <ticket> (it is printed in the batch's done command; run \`${BIN} status <file> --ticket\` if you lost it, only the agent that polled gets it back)`);
310
- const claim = ticket ? { holder: process.env[`${PRODUCT.envPrefix}_HOLDER`] || loadTicketRecord(ticket.batch_id)?.holder, lease_token: ticket.token, batch_id: ticket.batch_id } : {};
351
+ const claim = ticket ? { holder: envHolder || loadTicketRecord(ticket.batch_id)?.holder, lease_token: ticket.token, batch_id: ticket.batch_id } : envHolder ? { holder: envHolder } : {};
311
352
  const r = await req({ method: "POST", path: "/cli/done" }, { file, results, ...claim });
312
353
  const j = json(r.text);
313
354
  if (r.status !== 200) throw new Error(j.error || "done failed");
@@ -339,6 +380,7 @@ export function parseArgs(argv) {
339
380
  }
340
381
  else if (x === "--json" || x === "--verbose") a[x.slice(2)] = true; // doctor
341
382
  else if (x === "--remove") a.remove = true; // setup-rewriter
383
+ else if (x === "--check" || x === "--yes" || x === "--force") a[x.slice(2)] = true; // update
342
384
  else if (x === "--clean-old") a.cleanOld = true; // setup-rewriter, from install.sh: only removes what the older Granite download left
343
385
  else if (x === "--model" || x.startsWith("--model=")) a.model = x.includes("=") ? x.split("=")[1] : rest[++i]; // setup-rewriter
344
386
  else if (x === "--timeout" || x.startsWith("--timeout=")) {
@@ -355,7 +397,7 @@ export function parseArgs(argv) {
355
397
  else if (!a.file) a.file = x;
356
398
  else throw new Error(`unexpected argument ${x}`);
357
399
  }
358
- if (["open", "poll", "status", "done", "shapes"].includes(cmd) && !a.file) throw new Error(`Usage: ${BIN} ${cmd} <file>`);
400
+ if (["open", "poll", "status", "done", "shapes", "utf8-copy"].includes(cmd) && !a.file) throw new Error(`Usage: ${BIN} ${cmd} <file>`);
359
401
  if (cmd === "done" && !a.results) throw new Error(`Usage: ${BIN} done <file> --results <results.json>`);
360
402
  return a;
361
403
  }
@@ -399,6 +441,31 @@ async function main() {
399
441
  await out(a.json ? JSON.stringify(r, null, 2) + "\n" : formatDoctor(r));
400
442
  process.exit(r.ok ? 0 : 1);
401
443
  }
444
+ if (argv[0] === "update") {
445
+ const a = parseArgs(argv);
446
+ if (a.file) throw new Error(`unexpected argument ${a.file}`);
447
+ // everything update needs is loaded before the installer runs: install.sh deletes the app folder this process runs from
448
+ const { updateCommand, runGetShDefault } = await import("./update.js");
449
+ const appRoot = path.resolve(here, "..");
450
+ const home = os.homedir();
451
+ const versionOf = (cmd, args) => {
452
+ const r = spawnSync(cmd, args, { encoding: "utf8", timeout: 20000, stdio: ["ignore", "pipe", "ignore"] });
453
+ return r.status === 0 ? r.stdout.trim().split("\n")[0] || null : null;
454
+ };
455
+ const code = await updateCommand(argv.slice(1), {
456
+ version: JSON.parse(fs.readFileSync(path.join(appRoot, "package.json"), "utf8")).version,
457
+ appRoot,
458
+ home,
459
+ env: process.env,
460
+ print: (line) => process.stdout.write(line + "\n"),
461
+ server: { healthy, openWork: async () => { const r = await req({ method: "GET", path: "/cli/open-work", timeout: 3000 }); return { status: r.status, body: json(r.text) }; } },
462
+ stop: stopServer,
463
+ runGetSh: (base) => runGetShDefault(base),
464
+ launcherVersion: (route, root) => (route === "npm" ? versionOf(process.execPath, [path.join(root, "src", "cli.js"), "--version"]) : versionOf(path.join(home, ".local", "bin", BIN), ["--version"])),
465
+ run: (cmd, args) => spawnSync(cmd, args, { stdio: ["ignore", "inherit", "inherit"] }).status ?? 1,
466
+ });
467
+ process.exit(code);
468
+ }
402
469
  if (argv[0] === "reply") {
403
470
  // JSON on STDOUT only (also for failures), so an agent parses one stream; the exit code says whether the batch was finished
404
471
  let r;
@@ -434,6 +501,12 @@ async function main() {
434
501
  if (path.extname(real).toLowerCase() !== ".pptx") throw new Error("shapes reads a .pptx file");
435
502
  return out(JSON.stringify({ file: path.basename(real), slides: listShapes(parseDeck(readDoc(real)), a.slide ?? null) }, null, 2) + "\n");
436
503
  }
504
+ if (a.cmd === "utf8-copy") {
505
+ const { writeUtf8Copy } = await import("./html-utf8.js");
506
+ const r = writeUtf8Copy(file);
507
+ if (!r.ok) throw new Error(r.reason);
508
+ return out(`Wrote a UTF-8 copy of ${a.file}:\n ${r.path}\nThe file itself is not changed. Open the copy with:\n ${BIN} open ${shellQuote(r.path)}\n`);
509
+ }
437
510
  if (a.cmd === "poll" && aud.person) {
438
511
  await out(personPollText(file));
439
512
  process.exit(2);
@@ -441,8 +514,13 @@ async function main() {
441
514
  if (a.cmd === "open") {
442
515
  await ensureServer();
443
516
  // a person at a terminal (not --no-agent): the server starts its background agent on the file
444
- const r = await req({ method: "POST", path: "/cli/open" }, aud.offer ? { file, agent: "background" } : { file });
517
+ const r = await openRequest(aud.offer ? { file, agent: "background" } : { file });
445
518
  const j = json(r.text);
519
+ if (r.status !== 200 && j.code === "not_utf8" && /\.html?$/i.test(file)) {
520
+ // a page saved as windows-1252 or ISO-8859-1: the fixed sentence (R18) and the command that makes the copy it offers
521
+ const { copyable, openRefusal } = await import("./html-utf8.js");
522
+ if (copyable(fs.readFileSync(file))) throw new Error(openRefusal(BIN, file, shellQuote));
523
+ }
446
524
  if (r.status !== 200) throw new Error(j.error || "could not open that file");
447
525
  const realPath = fs.realpathSync(file);
448
526
  if (!a.noOpen) openBrowser(j.url);
@@ -452,8 +530,10 @@ async function main() {
452
530
  } else if (a.cmd === "poll") {
453
531
  return out(JSON.stringify(await pollFor(file, a), null, 2) + "\n");
454
532
  } else if (a.cmd === "status") {
533
+ const envHolder = process.env[`${PRODUCT.envPrefix}_HOLDER`];
534
+ if (!agentMayUse(envHolder, file)) throw new Error(NOT_YOUR_FILE); // the same words whether that file is open or not
455
535
  if (!(await healthy())) return out(JSON.stringify({ state: "no-server" }, null, 2) + "\n");
456
- const r = await req({ method: "GET", path: `/cli/status?file=${encodeURIComponent(file)}` });
536
+ const r = await req({ method: "GET", path: `/cli/status?file=${encodeURIComponent(file)}${envHolder ? `&holder=${encodeURIComponent(envHolder)}` : ""}` });
457
537
  if (r.status !== 200) throw new Error(json(r.text).error || "no such session");
458
538
  const st = json(r.text);
459
539
  if (a.ticket) {
@@ -477,21 +557,7 @@ async function main() {
477
557
  if (a.summary) return out(JSON.stringify({ ok: !!j.ok, flags: (j.flags || []).map((f) => ({ code: f.code, severity: f.severity })), ...(j.checks?.notice ? { checks_notice: j.checks.notice } : {}) }) + "\n");
478
558
  return out(JSON.stringify(j, null, 2) + "\n");
479
559
  } else if (a.cmd === "stop") {
480
- if (await healthy()) {
481
- const pid = json((await req({ method: "GET", path: "/health", timeout: 1500 }).catch(() => ({ text: "" }))).text).pid;
482
- await req({ method: "POST", path: "/cli/stop" }, {});
483
- // the server answers first and exits a moment later: "stopped" is printed only once its process is gone (waits at most 5 s)
484
- const gone = () => {
485
- try {
486
- process.kill(pid, 0);
487
- return false;
488
- } catch {
489
- return true;
490
- }
491
- };
492
- for (let i = 0; i < 100 && Number.isInteger(pid) && !gone(); i++) await new Promise((r) => setTimeout(r, 50));
493
- }
494
- await removeStaleSocket(); // a socket file nothing answers on is no use to anyone, and `doctor` would keep reporting it
560
+ await stopServer();
495
561
  return out("stopped\n");
496
562
  } else throw new Error(`unknown command ${a.cmd}\n${HELP}`);
497
563
  }
package/src/constants.js CHANGED
@@ -7,13 +7,46 @@ export const PRODUCT = {
7
7
  };
8
8
 
9
9
 
10
- export const SERVER_PROTOCOL = 5; // 2: session.stage, reply, compact polls, /cli/batch-file, stop ends waiting polls; 3: question cards (/api/answer, results "asked"); 4: quick, review and suggestion routes, local history kind, merge in every format (/api/merge edits, /cli/agent-write ops), /api/export-tracked; 5: "Build my voice guide" (/api/voice/build, /api/voice/draft/discard, done takes voice_draft)
10
+ export const SERVER_PROTOCOL = 7; // 2: session.stage, reply, compact polls, /cli/batch-file, stop ends waiting polls; 3: question cards (/api/answer, results "asked"); 4: quick, review and suggestion routes, local history kind, merge in every format (/api/merge edits, /cli/agent-write ops), /api/export-tracked; 5: "Build my voice guide" (/api/voice/build, /api/voice/draft/discard, done takes voice_draft); 6: an agent Onionskin started is bound to its file on every /cli/* route, /cli/batch and /cli/batch-file check the lease first (a CLI never trusts an older server to do that); 7: HTML files (.html, .htm open; /api/html-view; Send takes htmlEdits)
11
11
  export const FORMATS = {
12
12
  docx: { main: "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml", part: "/word/document.xml" },
13
13
  xlsx: { main: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml", part: "/xl/workbook.xml" },
14
14
  pptx: { main: "application/vnd.openxmlformats-officedocument.presentationml.presentation.main+xml", part: "/ppt/presentation.xml" },
15
+ // not a zip package: src/package.js validatePackage checks its encoding (src/html.js decodeHtml); the open worker reads it whole
16
+ html: { main: null, part: null, zip: false },
15
17
  };
16
18
  export const REFUSED_EXTENSIONS = new Set([".docm", ".xlsm", ".pptm"]);
19
+ /**
20
+ * Kinds of web file Onionskin refuses with a fixed sentence (PLAN-html D8, R15), by extension. Checked before the format: these are
21
+ * close to .html but not pages Onionskin can edit by splices.
22
+ */
23
+ export const REFUSED_PAGES = {
24
+ ".mhtml": "Onionskin cannot open this kind of saved page; in Chrome choose Save As, Webpage, Complete.",
25
+ ".mht": "Onionskin cannot open this kind of saved page; in Chrome choose Save As, Webpage, Complete.",
26
+ ".webarchive": "Onionskin cannot open this kind of saved page; in Chrome choose Save As, Webpage, Complete.",
27
+ ".xhtml": "Onionskin cannot open .xhtml pages; save the page as .html and open that.",
28
+ ".svg": "Onionskin cannot open .svg pictures; it opens .html and .htm pages.",
29
+ };
30
+ /** The format of a file by its extension (".htm" is "html"), or null when Onionskin does not open that kind of file. */
31
+ export function formatOfExt(ext) {
32
+ const e = String(ext).toLowerCase();
33
+ if (e === ".htm" || e === ".html") return "html";
34
+ const f = e.slice(1);
35
+ return Object.hasOwn(FORMATS, f) ? f : null;
36
+ }
37
+ /** The sentence for a file Onionskin does not open: a refused page kind, or the list of what it opens. */
38
+ export const ONLY_FORMATS = "Only .docx, .xlsx, .pptx, .html and .htm files are supported";
39
+ /**
40
+ * The end of every per-format dispatch chain: a format the chain does not know throws here, so it never runs another format's code (a
41
+ * web page read as a deck). `where` names the chain. The message holds at most 20 characters of the value.
42
+ */
43
+ export function unknownFormat(format, where) {
44
+ throw new Error(`unknown format ${JSON.stringify(String(format).slice(0, 20))} in ${where}`);
45
+ }
46
+ /** `table[format]` of a per-format table, or unknownFormat (own keys only: "constructor" is not a format). */
47
+ export const byFormat = (table, format, where) => (Object.hasOwn(table, format) ? table[format] : unknownFormat(format, where));
48
+ /** The kind of the entry that ends a list of the page's edits cut short (src/session.js editKinds): more were made than it holds. */
49
+ export const EDITS_OVERFLOW = "edits.overflow";
17
50
  export const env = (suffix, fallback) => process.env[`${PRODUCT.envPrefix}_${suffix}`] ?? fallback;
18
51
 
19
52
  /** The largest document that is opened, sent, written or checked. Enforced by size BEFORE the bytes are read. */
package/src/diff.js CHANGED
@@ -17,7 +17,9 @@ const WORD_LIMIT = 2000;
17
17
  export const HARD_CAP_TOKENS = 60_000;
18
18
  const MAX_D = 2000;
19
19
  const CJK = "\\p{Script=Han}|\\p{Script=Hiragana}|\\p{Script=Katakana}";
20
- const TOKEN = new RegExp(`(?:${CJK})|(?:(?!${CJK})[\\p{L}\\p{N}\\p{M}])+(?:['’](?:(?!${CJK})[\\p{L}\\p{N}\\p{M}])+)*|\\s+|[^\\s]`, "gu");
20
+ // a word is at most 4,096 characters a token (a longer one is several): an unbounded repeat over a run of 7.9 million combining marks
21
+ // overflowed the regex stack ("Maximum call stack size exceeded")
22
+ const TOKEN = new RegExp(`(?:${CJK})|(?:(?!${CJK})[\\p{L}\\p{N}\\p{M}]){1,4096}(?:['’](?:(?!${CJK})[\\p{L}\\p{N}\\p{M}]){1,4096})*|\\s+|[^\\s]`, "gu");
21
23
  const IS_WORD = new RegExp(`^(?:${CJK}|[\\p{L}\\p{N}\\p{M}])`, "u");
22
24
  const IS_CJK = new RegExp(`^(?:${CJK})$`, "u");
23
25
  const SENTENCE = /[^.!?。!?\n]*(?:[.!?。!?]+|\n|$)\s*/gu;