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
package/src/ops/docx.js CHANGED
@@ -4,7 +4,7 @@ import { replaceEntry } from "../zip-write.js";
4
4
  import { checkExpect, engineFailure, excerpt, fail, textHash, normText, refused } from "./common.js";
5
5
  import { TableError, tableEditor } from "./docx-table.js";
6
6
  import { loadEngine, openEngine } from "./engine.js";
7
- import { applySet, findAll, hasObjects, layout, partialWords, pickMatches, replaceRange, setAllText, splitForRange } from "./runs.js";
7
+ import { applySet, applySets, countMatches, findRanges, hasObjects, layout, pickMatches, rangesOf, replaceRanges, setAllText, splitForRange, splitForRanges } from "./runs.js";
8
8
 
9
9
  /**
10
10
  * DOCX driver. Two phases per run (see run.js): bind() resolves anchors and checks `expect` against the file as it was before any
@@ -45,15 +45,17 @@ export async function open({ bytes, name, anchors }) {
45
45
  const ours = readDocxParagraphs(bytes);
46
46
  const group = (list) => {
47
47
  const m = new Map();
48
- for (const x of list) m.set(x.hash, [...(m.get(x.hash) || []), x]);
48
+ for (const x of list) (m.get(x.hash) || m.set(x.hash, []).get(x.hash)).push(x); // a copy per paragraph was quadratic among equal texts
49
49
  return m;
50
50
  };
51
51
  const ourBy = group(ours);
52
+ const ourAt = new Map(); // a paragraph of ours -> its occurrence number among equal texts (indexOf per lookup was quadratic)
53
+ for (const list of ourBy.values()) list.forEach((x, i) => ourAt.set(x, i));
52
54
  const engBy = group(eng);
53
55
  const cur = new Map(); // pid -> current single-part Paragraph model
54
56
  for (const e of eng) if (e.pid !== undefined && e.parts.length === 1) cur.set(e.pid, e.parts[0]);
55
57
  // tableOps: the row and column edits, run on the engine's saved bytes (save); bound: the body paths other ops of this file edit
56
- return { h, paraSpec, meta, styles: meta.styles || [], ours, eng, ourBy, engBy, cur, anchors: anchors || new Map(), newPid: NEW_PID_BASE, name, bytes, tableOps: [], bound: [], shadow: null };
58
+ return { h, paraSpec, meta, styles: meta.styles || [], ours, eng, ourBy, ourAt, engBy, cur, anchors: anchors || new Map(), newPid: NEW_PID_BASE, name, bytes, tableOps: [], bound: [], shadow: null };
57
59
  }
58
60
 
59
61
  /** the engine paragraph that is `o` (same text, same occurrence number among equal texts); null when the two readings of the file disagree */
@@ -61,7 +63,7 @@ function engineFor(st, o) {
61
63
  const a = st.ourBy.get(o.hash);
62
64
  const b = st.engBy.get(o.hash);
63
65
  if (!b || a.length !== b.length) return null;
64
- return b[a.indexOf(o)];
66
+ return b[st.ourAt.get(o)];
65
67
  }
66
68
 
67
69
  function locatorOf(st, target) {
@@ -72,31 +74,52 @@ function locatorOf(st, target) {
72
74
  return { loc: a.locator, quote: a.quote };
73
75
  }
74
76
 
77
+ const pathKey = (path) => (Array.isArray(path) && path.every(Number.isInteger) ? path.join(",") : null); // a body path is integers (anything else matches nothing)
78
+ const NONE = [];
79
+ /**
80
+ * The paragraphs of the file by w14:paraId, by body path and by text hash, built on first use: every op of a run looks its paragraph up
81
+ * instead of scanning them all (a scan per op made a merge of 8,000 edited paragraphs hold the server's thread for 1.4 s, 49,999 for 120 s).
82
+ */
83
+ function lookup(st) {
84
+ if (st.lookup) return st.lookup;
85
+ const ix = { byId: new Map(), byPath: new Map(), byHash: new Map() };
86
+ const add = (m, k, p) => (m.get(k) || m.set(k, []).get(k)).push(p);
87
+ for (const p of st.ours) {
88
+ if (p.paraId) add(ix.byId, p.paraId, p);
89
+ if (p.path) add(ix.byPath, pathKey(p.path), p);
90
+ add(ix.byHash, p.hash, p);
91
+ }
92
+ return (st.lookup = ix);
93
+ }
94
+
75
95
  /** durable locator -> one paragraph of the file. Exact matches only (paraId, path+hash, hash, quote): no fuzzy matching. */
76
96
  function resolveParagraph(st, target) {
77
97
  const { loc, quote, error } = locatorOf(st, target);
78
98
  if (error) return fail("anchor_not_found", error);
79
99
  const pid = loc.para_id || loc.paraId;
80
- const path = loc.body_path || null;
100
+ const path = Array.isArray(loc.body_path) ? loc.body_path : null;
81
101
  const hash = loc.text_hash || null;
82
- const same = (a, b) => a && b && a.length === b.length && a.every((v, i) => v === b[i]);
102
+ const ix = lookup(st);
83
103
  const unique = (hits, how) => (hits.length === 1 ? { o: hits[0] } : hits.length > 1 ? fail("anchor_not_found", `ambiguous locator (${how} matches ${hits.length} paragraphs)`) : null);
84
104
  if (pid) {
85
- const r = unique(st.ours.filter((p) => p.paraId === String(pid).toUpperCase()), "para_id");
105
+ const r = unique(ix.byId.get(String(pid).toUpperCase()) || NONE, "para_id");
86
106
  if (r) return r;
87
107
  }
108
+ const atPath = path ? ix.byPath.get(pathKey(path)) || NONE : NONE;
88
109
  if (path && hash) {
89
- const r = unique(st.ours.filter((p) => same(p.path, path) && p.hash === hash), "body_path with text_hash");
110
+ const r = unique(atPath.filter((p) => p.hash === hash), "body_path with text_hash");
90
111
  if (r) return r;
91
112
  }
92
113
  if (hash) {
93
- const r = unique(st.ours.filter((p) => p.hash === hash), "text_hash");
114
+ const r = unique(ix.byHash.get(hash) || NONE, "text_hash");
94
115
  if (r) return r;
95
116
  }
96
117
  if (path && !hash) {
97
- const r = unique(st.ours.filter((p) => same(p.path, path)), "body_path");
118
+ const r = unique(atPath, "body_path");
98
119
  if (r) return r;
99
120
  }
121
+ // Ceiling: the quote is the last resort (no id, path or hash matched) and scans every paragraph; ops that reach it are rare (an agent's
122
+ // locator whose paragraph changed), and a batch carries at most its comments' anchors. Upgrade path: a substring index.
100
123
  const q = normText(quote || "");
101
124
  if (q.length >= 8) {
102
125
  const r = unique(st.ours.filter((p) => normText(p.text).includes(q)), "the anchor quote");
@@ -166,16 +189,18 @@ export function bind(st, op) {
166
189
  }
167
190
  case "set_table_cell_text": {
168
191
  const { table, row, col, paragraph = 0 } = op.target;
169
- const o = st.ours.find((p) => p.path && p.path.length === 4 && p.path[0] === table && p.path[1] === row && p.path[2] === col && p.path[3] === paragraph);
192
+ const o = (lookup(st).byPath.get(pathKey([table, row, col, paragraph])) || NONE)[0];
170
193
  if (!o) return fail("anchor_not_found", `no paragraph ${paragraph} in cell (${row},${col}) of table ${table}`);
171
194
  const r = bindParagraph(st, { text_hash: o.hash, body_path: o.path }, op.expect);
172
195
  return r.ok ? { ...r, op } : r;
173
196
  }
174
197
  case "replace_all_text": {
175
- const hits = st.ours.map((o) => ({ o, n: findAll(o.text, op.find, op.match_case !== false).length })).filter((x) => x.n);
198
+ // counted, not listed: one pass per paragraph for the count and the matches inside a longer word (a list of every match of 8
199
+ // million characters held the thread for seconds in an outside review)
200
+ const hits = st.ours.map((o) => ({ o, ...countMatches(o.text, op.find, op.match_case !== false) })).filter((x) => x.n);
176
201
  const total = hits.reduce((n, x) => n + x.n, 0);
177
202
  // what previewAll reports: matches inside a longer word, and matches in protected paragraphs (which the run skips)
178
- const extra = { partial_words: hits.reduce((n, x) => n + partialWords(x.o.text, op.find, op.match_case !== false), 0), protected: hits.reduce((n, x) => n + (x.o.protected ? x.n : 0), 0) };
203
+ const extra = { partial_words: hits.reduce((n, x) => n + x.partial, 0), protected: hits.reduce((n, x) => n + (x.o.protected ? x.n : 0), 0) };
179
204
  if (total !== op.expect.count) return fail("expect_mismatch", `found ${total} occurrence(s), expected ${op.expect.count}`, { found: total, ...extra });
180
205
  return { ok: true, op, hits, total, ...extra };
181
206
  }
@@ -202,7 +227,7 @@ const docXml = (st) => (st.docXml ??= openZip(st.bytes).readText("word/document.
202
227
  /** cell paragraphs -> text: grouped by `key` (path index of the cell) in order, paragraphs joined by LF, groups by `sep` */
203
228
  function joined(paras, key, sep) {
204
229
  const groups = new Map();
205
- for (const p of paras) groups.set(p.path[key], [...(groups.get(p.path[key]) || []), p.text]);
230
+ for (const p of paras) (groups.get(p.path[key]) || groups.set(p.path[key], []).get(p.path[key])).push(p.text);
206
231
  return [...groups.keys()].sort((a, b) => a - b).map((k) => groups.get(k).join("\n")).join(sep);
207
232
  }
208
233
  const tableSpec = (op, tableNo) => {
@@ -290,7 +315,11 @@ const after = (p, block, edited) => ({ ...block, edited, ...(p.pd ? { pd: p.pd }
290
315
 
291
316
  function matchesIn(p, find, matchCase, pick) {
292
317
  const { text } = layout(p.runs);
293
- return pickMatches(findAll(text, find, matchCase !== false), pick);
318
+ const found = findRanges(text, find, matchCase !== false);
319
+ const m = pickMatches(found.map((_, i) => i), pick);
320
+ if (m.error) return m;
321
+ const chosen = m.starts.map((i) => found[i]);
322
+ return { starts: chosen.map((r) => r.start), lens: chosen.map((r) => r.end - r.start) }; // lens: with match_case off a match is not always find.length long
294
323
  }
295
324
 
296
325
  export async function exec(st, b) {
@@ -309,9 +338,7 @@ export async function exec(st, b) {
309
338
  }
310
339
  const m = matchesIn(p, op.find, op.match_case, op);
311
340
  if (m.error) return fail("anchor_not_found", m.error);
312
- let runs = p.runs;
313
- for (const s of [...m.starts].sort((a, c) => c - a)) runs = replaceRange(runs, s, s + op.find.length, op.replace); // right to left keeps offsets valid
314
- await editRuns(st, b.pid, runs);
341
+ await editRuns(st, b.pid, replaceRanges(p.runs, m.starts, m.lens, op.replace));
315
342
  return { status: "applied", detail: { replaced: m.starts.length } };
316
343
  }
317
344
  case "format_text": {
@@ -320,12 +347,8 @@ export async function exec(st, b) {
320
347
  const m = matchesIn(p, op.find, op.match_case, op);
321
348
  if (m.error) return fail("anchor_not_found", m.error);
322
349
  const patch = runProps(op.set);
323
- let runs = p.runs;
324
- for (const s of [...m.starts].sort((a, c) => c - a)) {
325
- const sp = splitForRange(runs, s, s + op.find.length);
326
- runs = applySet(sp.runs, sp.covered, patch);
327
- }
328
- await editRuns(st, b.pid, runs);
350
+ const sp = splitForRanges(p.runs, rangesOf(m.starts, m.lens));
351
+ await editRuns(st, b.pid, applySet(sp.runs, sp.covered.flat(), patch));
329
352
  return { status: "applied", detail: { ranges: m.starts.length } };
330
353
  }
331
354
  case "insert_paragraph_after":
@@ -386,9 +409,15 @@ export async function exec(st, b) {
386
409
  if (op.runs?.length) {
387
410
  // offsets are those of the paragraph as the file has it: any difference in how the engine reads the text and nothing is formatted
388
411
  if (layout(runs).text !== b.o.text) return refused("text_read_differently", "the engine reads this paragraph's text differently; not formatted");
389
- for (const r of [...op.runs].sort((x, y) => y.start - x.start)) {
390
- const sp = splitForRange(runs, r.start, r.end);
391
- runs = applySet(sp.runs, sp.covered, r.set);
412
+ const ranges = [...op.runs].sort((x, y) => x.start - y.start);
413
+ const sp = splitForRanges(runs, ranges);
414
+ if (sp) runs = applySets(sp.runs, sp.covered, ranges.map((r) => r.set));
415
+ else {
416
+ // ranges that overlap or are empty: one at a time, right to left (each a pass over the runs)
417
+ for (const r of ranges.reverse()) {
418
+ const one = splitForRange(runs, r.start, r.end);
419
+ runs = applySet(one.runs, one.covered, r.set);
420
+ }
392
421
  }
393
422
  }
394
423
  const spec = { ...st.paraSpec([{ ...p, edited: true, runs }]), ...(Object.keys(pd).length ? { pd } : {}) };
@@ -435,9 +464,9 @@ export async function exec(st, b) {
435
464
  skip(m.error ? "the text is no longer in the paragraph (changed by an earlier op of this file?)" : `the engine finds ${m.starts.length} of the ${expected} match(es)`);
436
465
  continue;
437
466
  }
438
- let runs = p.runs;
467
+ let runs;
439
468
  try {
440
- for (const s of [...m.starts].sort((a, c) => c - a)) runs = replaceRange(runs, s, s + op.find.length, op.replace);
469
+ runs = replaceRanges(p.runs, m.starts, m.lens, op.replace);
441
470
  } catch (err) {
442
471
  if (!(err instanceof RangeError)) throw err;
443
472
  skip(err.message);
@@ -0,0 +1,394 @@
1
+ import { ACTIVE_REFUSED, HTML_NS, inventory } from "../html-active.js";
2
+ import { byPath, elementAddressable, elementPath, escapeAttr, escapeText, hasDuplicateAttributes, HtmlEncodingError, kids, ownHash, parseFragmentIn, pathString, proveEdits, RAWTEXT, RCDATA, readHtml, visibleText } from "../html.js";
3
+ import { assemble, checkedHtmlWrite, conflicts, editDelta, emptyEdit, textEdit } from "../html-write.js";
4
+ import { CapError } from "../xml.js";
5
+ import { excerpt, fail, normText, refused } from "./common.js";
6
+ import { countMatches, findRanges, pickMatches } from "./runs.js";
7
+
8
+ /**
9
+ * HTML driver (PLAN-html 3.1, 3.5 as changed by R1, R7, R8, R10, R11, R12, R22). No engine: every op is a splice of the file's source
10
+ * through src/html.js, never a serializer, so the bytes outside the edited places stay as they are. Elements are addressed by an anchor id
11
+ * of the batch, or by the `path` or unique `element_id` the batch and the outline give; `expect.text_hash` is the hash of the element's
12
+ * own text (its direct text, the batch's `locator.text_hash`), `expect.quote` must occur in its visible text.
13
+ *
14
+ * All ops bind against the file as delivered (one reading); each op's edit is built then, and checked at bind for active content it
15
+ * would add (R1, which names the op). Execute accepts the edits in order and refuses one that changes a place an earlier op of the file
16
+ * changes (`overlapping_edit`). finish() applies every accepted splice at once and proves the result in one parse (src/html.js
17
+ * proveEdits: one parse with its proof took about 1.5 s of worker CPU at the limits, so one per op would not fit a batch of 50 in the
18
+ * ops worker's time limit), then runs the write check (src/html-write.js checkedHtmlWrite: charset, the whole-file inventory). This
19
+ * module never loads the parser: src/ops/run.js runs it in the ops worker (src/ops-worker.js) unless the parser is on this thread.
20
+ */
21
+
22
+ const SPANS = "the text runs across the edge of an element (a link, bold text or similar), so it is in no single text of the page; use replace_element with the element's new markup";
23
+ const FROZEN = "script and style elements cannot be changed by an agent here (code that runs in a browser, and styles, stay as they are)";
24
+ const VOID = new Set(["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "source", "track", "wbr", "param", "keygen", "image", "basefont", "bgsound", "frame"]);
25
+ const isText = (x) => x.nodeName === "#text";
26
+ const isEl = (x) => x.tagName !== undefined;
27
+ const frozen = (el) => (el.namespaceURI === HTML_NS ? RAWTEXT.has(el.tagName) : el.tagName === "script" || el.tagName === "style");
28
+
29
+ export async function open({ bytes, name, anchors = new Map() }) {
30
+ const b = Buffer.isBuffer(bytes) ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
31
+ const st = { bytes: b, name, anchors, read: null, error: null, inv: null, accepted: [] };
32
+ try {
33
+ st.read = readHtml(b);
34
+ st.inv = inventory(st.read.doc);
35
+ } catch (err) {
36
+ if (err instanceof CapError) st.error = refused("too_large", String(err.message));
37
+ else if (err instanceof HtmlEncodingError) st.error = refused("not_utf8", err.message);
38
+ else throw err;
39
+ }
40
+ return st;
41
+ }
42
+
43
+ // ---- targets and expect --------------------------------------------------------------------------------------------------------
44
+ const SEG = /^([a-z0-9-]{1,32}|t-[0-9a-f]{12})\[([1-9][0-9]{0,6})\]$/;
45
+ /** "/html[1]/body[1]/p[3]" (pathString) -> ["html:0", "body:0", "p:2"], or null. */
46
+ function parsePath(p) {
47
+ if (typeof p !== "string" || !p.startsWith("/") || p.length > 20000) return null;
48
+ const out = [];
49
+ for (const s of p.slice(1).split("/")) {
50
+ const m = SEG.exec(s);
51
+ if (!m) return null;
52
+ out.push(`${m[1]}:${Number(m[2]) - 1}`);
53
+ }
54
+ return out.length ? out : null;
55
+ }
56
+
57
+ /** The element an op's target names, located in the source and not frozen; or a failure. */
58
+ function element(st, t) {
59
+ let loc = t || {};
60
+ let what = "";
61
+ if ("anchor_id" in loc) {
62
+ const a = st.anchors.get(loc.anchor_id);
63
+ if (!a) return fail("anchor_not_found", `anchor ${loc.anchor_id} is not in the batch (pass --batch batch.json, or run inside the session that delivered it)`);
64
+ if (!a.resolved || !a.locator || (!a.locator.path && !a.locator.element_id)) return fail("anchor_not_found", `anchor ${loc.anchor_id} was not resolved to an element of the page when the batch was built`);
65
+ loc = { element_id: a.locator.element_id || undefined, path: a.locator.path || undefined };
66
+ what = `anchor ${t.anchor_id}`;
67
+ }
68
+ let el = null;
69
+ if (loc.element_id) el = st.read.byId.get(loc.element_id) || null;
70
+ if (!el && loc.path) {
71
+ const segs = parsePath(loc.path);
72
+ if (!segs) return fail("anchor_not_found", "path is not an element path as the batch gives it (/html[1]/body[1]/p[3])");
73
+ el = byPath(st.read, segs);
74
+ }
75
+ if (!el && !loc.element_id && !loc.path) return fail("anchor_not_found", "give anchor_id, path or element_id (from the batch)");
76
+ if (!el) return fail("anchor_not_found", what ? `${what} no longer names an element of the page` : loc.element_id && !loc.path ? `no single element has the id ${JSON.stringify(String(loc.element_id).slice(0, 64))}` : "no element at that path");
77
+ if (frozen(el)) return refused("active_content", FROZEN);
78
+ if (!el.sourceCodeLocation?.startTag || el.__hidden) return refused("not_addressable", "this element is not written as such in the page's source (the parser implied it, or it is template content); target its nearest written ancestor");
79
+ return { el };
80
+ }
81
+
82
+ /** `expect` against the element: text_hash equals its own text's hash, quote occurs in its visible text (never a script or style body: R7). */
83
+ function expectOn(st, el, expect) {
84
+ const text = visibleText(st.read, el);
85
+ if ("quote" in expect && !normText(text).includes(normText(expect.quote))) return fail("expect_mismatch", "the target no longer says what `expect.quote` says", { actual_excerpt: excerpt(text) });
86
+ const h = ownHash(el);
87
+ if ("text_hash" in expect && expect.text_hash !== h) return fail("expect_mismatch", "the target's text hash differs from `expect.text_hash`", { actual_excerpt: excerpt(text), actual_text_hash: h });
88
+ return null;
89
+ }
90
+
91
+ // ---- finding text ------------------------------------------------------------------------------------------------------------------
92
+ /** The text nodes under `root` an op may search (not raw text, not template content), in document order. */
93
+ function textsUnder(root) {
94
+ const out = [];
95
+ const stack = [root];
96
+ while (stack.length) {
97
+ const x = stack.pop();
98
+ if (isText(x)) {
99
+ if (!x.__hidden && x.__ctx) out.push(x);
100
+ continue;
101
+ }
102
+ if (isEl(x) && (frozen(x) || x.__hidden)) continue;
103
+ const ks = kids(x);
104
+ for (let i = ks.length - 1; i >= 0; i--) stack.push(ks[i]);
105
+ }
106
+ return out;
107
+ }
108
+ const foldFind = (f) => f.replace(/\s+/g, " ");
109
+ /**
110
+ * A text with every whitespace run as one space (what the agent read: the outline folds whitespace as src/text.js normText does), and
111
+ * where each of its offsets is in the text: `from` (start of that unit) and `to` (end). Null maps when nothing folds.
112
+ */
113
+ function folded(v) {
114
+ if (!/\s\s|[^\S ]/.test(v)) return { n: v, from: null, to: null };
115
+ const from = new Int32Array(v.length + 1);
116
+ const to = new Int32Array(v.length + 1);
117
+ let n = "";
118
+ let k = 0;
119
+ for (const m of v.matchAll(/\s+|\S+/g)) {
120
+ if (/^\s/.test(m[0])) {
121
+ from[k] = m.index;
122
+ to[k] = m.index + m[0].length;
123
+ k++;
124
+ n += " ";
125
+ } else {
126
+ for (let i = 0; i < m[0].length; i++) (from[k] = m.index + i), (to[k] = m.index + i + 1), k++;
127
+ n += m[0];
128
+ }
129
+ }
130
+ from[k] = v.length;
131
+ return { n, from, to };
132
+ }
133
+ /** The matches of `find` in a text node's value (whitespace runs match any whitespace), as value ranges; and how many touch a letter. */
134
+ function matchesIn(v, find, matchCase) {
135
+ const f = folded(v);
136
+ const q = foldFind(find);
137
+ const r = findRanges(f.n, q, matchCase);
138
+ const ranges = f.from ? r.map(({ start, end }) => ({ start: f.from[start], end: end > start ? f.to[end - 1] : f.from[start] })) : r;
139
+ return { ranges, partial: r.length ? countMatches(f.n, q, matchCase).partial : 0 };
140
+ }
141
+ /** How many times `find` occurs in the element's visible text (crossing element edges): more than in its text nodes means spans_markup. */
142
+ const visibleCount = (st, el, find, matchCase) => findRanges(visibleText(st.read, el), foldFind(find), matchCase).length;
143
+
144
+ // ---- binding ---------------------------------------------------------------------------------------------------------------------
145
+ /** One op's result of bind: the edit, checked for active content it adds (R1, by the fixed reason codes). */
146
+ function bound(st, op, edit, detail) {
147
+ const d = editDelta(edit, st.inv.scripted);
148
+ if (d.refused) return { ...refused("active_content", ACTIVE_REFUSED), reasons: d.reasons };
149
+ return { ok: true, op, edit, ...(detail ? { detail } : {}) };
150
+ }
151
+
152
+ /** The edit of setting an element's whole text (set_text, replace_text with `text`). */
153
+ function wholeText(st, el, text) {
154
+ const ks = kids(el);
155
+ if (ks.length === 1 && isText(ks[0])) {
156
+ const node = ks[0];
157
+ if (node.__ok !== true) return refused("not_addressable", "this text cannot be edited by a splice at its exact place (whitespace only, or read differently from its source); use replace_element");
158
+ const t = textEdit(st.read, [{ node, start: 0, end: node.value.length, repl: text }]);
159
+ return t.ok ? { edit: t.edit } : refused(t.code === "not_addressable" ? "not_addressable" : "not_well_formed", t.why);
160
+ }
161
+ if (ks.length) return refused("spans_markup", "the element holds other elements or more than one text; use replace_text for a part of its text, or replace_element with its new markup");
162
+ if (el.namespaceURI !== HTML_NS || VOID.has(el.tagName)) return refused("not_addressable", "this element holds no text");
163
+ const edit = emptyEdit();
164
+ if (text === "") return { edit };
165
+ const at = el.sourceCodeLocation.startTag.endOffset;
166
+ const esc = escapeText(text, RCDATA.has(el.tagName) ? "rcdata" : "normal", { atStartOfLeadingLf: ["pre", "listing", "textarea"].includes(el.tagName) });
167
+ if (!esc.ok) return refused("not_well_formed", "the new text holds a NUL character or a lone surrogate");
168
+ edit.splices.push({ s: at, e: at, repl: esc.out });
169
+ edit.children.push({ parent: el, i: 0, j: 0, make: () => [{ nodeName: "#text", value: text }], removed: [] });
170
+ edit.touched.push(el);
171
+ return { edit };
172
+ }
173
+
174
+ /** A fragment for a place: parsed in its context, which must read back the same (R22). */
175
+ function fragment(ctx, markup) {
176
+ if (/[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/.test(markup) || markup.includes("\u0000")) return refused("not_well_formed", "the markup holds a NUL character or a lone surrogate");
177
+ const f = parseFragmentIn(ctx, markup);
178
+ if (!f.roundTrips) return refused("not_well_formed", "the markup does not read back the same in that place (an unclosed, misplaced or misnested tag?)");
179
+ return { nodes: f.nodes };
180
+ }
181
+
182
+ const quoteOf = (src) => {
183
+ const eq = src.indexOf("=");
184
+ if (eq < 0) return null;
185
+ const c = src.slice(eq + 1).trimStart()[0];
186
+ return c === '"' || c === "'" ? c : "";
187
+ };
188
+
189
+ export async function bind(st, op) {
190
+ if (st.error) return { ...st.error };
191
+ const tgt = (expectToo = true) => {
192
+ const r = element(st, op.target);
193
+ if (!r.el) return r;
194
+ const bad = expectToo ? expectOn(st, r.el, op.expect || {}) : null;
195
+ return bad || r;
196
+ };
197
+ switch (op.op) {
198
+ case "replace_text": {
199
+ const r = tgt();
200
+ if (!r.el) return r;
201
+ if ("text" in op) {
202
+ const w = wholeText(st, r.el, String(op.text));
203
+ return w.edit ? bound(st, op, w.edit) : w;
204
+ }
205
+ const mc = op.match_case !== false;
206
+ const hits = [];
207
+ for (const node of textsUnder(r.el)) for (const m of matchesIn(node.value, op.find, mc).ranges) hits.push({ node, ...m });
208
+ const pick = pickMatches(hits.map((_, i) => i), op);
209
+ if (pick.error) {
210
+ const seen = visibleCount(st, r.el, op.find, mc);
211
+ if (seen > hits.length && (!hits.length || (op.occurrence && op.occurrence > hits.length))) return refused("spans_markup", SPANS);
212
+ return fail("anchor_not_found", pick.error);
213
+ }
214
+ const chosen = pick.starts.map((i) => hits[i]);
215
+ if (chosen.some((h) => h.node.__ok !== true)) return refused("not_addressable", "the match is in text that cannot be edited by a splice at its exact place (text the parser moved, or whitespace only); use replace_element");
216
+ const t = textEdit(st.read, chosen.map((h) => ({ node: h.node, start: h.start, end: h.end, repl: op.replace })));
217
+ if (!t.ok) return refused(t.code === "not_addressable" ? "not_addressable" : "not_well_formed", t.why);
218
+ return bound(st, op, t.edit, { replaced: chosen.length });
219
+ }
220
+ case "set_text": {
221
+ const r = tgt();
222
+ if (!r.el) return r;
223
+ const w = wholeText(st, r.el, op.text);
224
+ return w.edit ? bound(st, op, w.edit) : w;
225
+ }
226
+ case "set_attribute":
227
+ case "remove_attribute": {
228
+ const r = tgt();
229
+ if (!r.el) return r;
230
+ const el = r.el;
231
+ if (el.namespaceURI !== HTML_NS) return refused("not_addressable", "attributes of SVG and MathML elements cannot be changed here");
232
+ const name = op.name.toLowerCase();
233
+ const st0 = el.sourceCodeLocation.startTag;
234
+ const at = st0.attrs?.[name];
235
+ const cur = el.attrs.find((a) => a.name === name && !a.namespace);
236
+ const text = st.read.text;
237
+ const edit = emptyEdit();
238
+ edit.touched.push(el);
239
+ if (op.op === "remove_attribute") {
240
+ if (!cur) return fail("anchor_not_found", `the element has no ${JSON.stringify(name)} attribute`);
241
+ // R22: removing the first of duplicate attributes would make the next one count
242
+ if (!at || hasDuplicateAttributes(st.read, el.__id)) return refused("not_addressable", "this start tag holds an attribute more than once; removing one would make another count instead");
243
+ let s = at.startOffset;
244
+ while (s > 0 && /[\t\n\f\r ]/.test(text[s - 1])) s--;
245
+ edit.splices.push({ s, e: at.endOffset, repl: "" });
246
+ edit.attrs.push({ el, name, value: null });
247
+ return bound(st, op, edit);
248
+ }
249
+ const value = String(op.value);
250
+ if (cur) {
251
+ if (!at) return refused("not_addressable", "this attribute has no place in the source");
252
+ const src = text.slice(at.startOffset, at.endOffset);
253
+ const q = quoteOf(src);
254
+ const esc = escapeAttr(value, q === null ? '"' : q);
255
+ if (!esc.ok) return refused("not_well_formed", "the value holds a NUL character or a lone surrogate");
256
+ edit.splices.push({ s: at.startOffset, e: at.endOffset, repl: `${src.slice(0, name.length)}=${esc.out}` });
257
+ } else {
258
+ const esc = escapeAttr(value, '"');
259
+ if (!esc.ok) return refused("not_well_formed", "the value holds a NUL character or a lone surrogate");
260
+ let p = st0.startOffset + 1 + el.tagName.length;
261
+ for (const a of Object.values(st0.attrs || {})) if (a.endOffset > p) p = a.endOffset;
262
+ edit.splices.push({ s: p, e: p, repl: ` ${name}=${esc.out}` });
263
+ }
264
+ edit.attrs.push({ el, name, value });
265
+ return bound(st, op, edit);
266
+ }
267
+ case "insert_html":
268
+ case "replace_element":
269
+ case "delete_element": {
270
+ const r = tgt();
271
+ if (!r.el) return r;
272
+ const el = r.el;
273
+ const loc = el.sourceCodeLocation;
274
+ const outside = op.op !== "insert_html" || op.where === "before" || op.where === "after";
275
+ const parent = outside ? el.__par : el;
276
+ if (outside) {
277
+ // R8, on demand: the element's source, parsed alone in its parent's context, is exactly it
278
+ if (!el.__ctx || !isEl(parent) || !elementAddressable(st.read, el.__id)) return refused("not_addressable", "this element cannot be cut out of the source exactly (the parser builds it from more than its own tags); target an ancestor, or change its contents");
279
+ } else if (el.namespaceURI !== HTML_NS || VOID.has(el.tagName) || RCDATA.has(el.tagName)) return refused("not_addressable", "this element cannot hold markup");
280
+ const edit = emptyEdit();
281
+ edit.touched.push(parent);
282
+ if (op.op === "delete_element") {
283
+ edit.splices.push({ s: loc.startOffset, e: loc.endOffset, repl: "" });
284
+ edit.children.push({ parent, i: el.__idx, j: el.__idx + 1, make: () => [], removed: [el] });
285
+ edit.removed.push(el);
286
+ return bound(st, op, edit);
287
+ }
288
+ const f = fragment(outside ? el.__ctx : el, op.html);
289
+ if (!f.nodes) return f;
290
+ const make = () => f.nodes;
291
+ if (op.op === "replace_element") {
292
+ edit.splices.push({ s: loc.startOffset, e: loc.endOffset, repl: op.html });
293
+ edit.children.push({ parent, i: el.__idx, j: el.__idx + 1, make, removed: [el] });
294
+ edit.removed.push(el);
295
+ return bound(st, op, edit);
296
+ }
297
+ let at;
298
+ let i;
299
+ if (op.where === "before") [at, i] = [loc.startOffset, el.__idx];
300
+ else if (op.where === "after") [at, i] = [loc.endOffset, el.__idx + 1];
301
+ else if (op.where === "first_child") [at, i] = [loc.startTag.endOffset, 0];
302
+ else {
303
+ if (!loc.endTag) return refused("not_addressable", "this element has no end tag in the source, so there is no exact place after its last child; use replace_element");
304
+ [at, i] = [loc.endTag.startOffset, kids(el).length];
305
+ }
306
+ edit.splices.push({ s: at, e: at, repl: op.html });
307
+ edit.children.push({ parent, i, j: i, make, removed: [] });
308
+ return bound(st, op, edit);
309
+ }
310
+ case "replace_all_text": {
311
+ const mc = op.match_case !== false;
312
+ const hits = [];
313
+ for (const node of textsUnder(st.read.doc)) {
314
+ const m = matchesIn(node.value, op.find, mc);
315
+ if (m.ranges.length) hits.push({ node, ranges: m.ranges, partial: m.partial, ro: node.__ok !== true });
316
+ }
317
+ const total = hits.reduce((n, h) => n + h.ranges.length, 0);
318
+ const extra = { partial_words: hits.reduce((n, h) => n + h.partial, 0), protected: hits.reduce((n, h) => n + (h.ro ? h.ranges.length : 0), 0) };
319
+ if (total !== op.expect.count) {
320
+ const across = findRanges(st.read.flat, foldFind(op.find), mc).length - total;
321
+ return fail("expect_mismatch", `found ${total} occurrence(s), expected ${op.expect.count}`, { found: total, ...extra, ...(across > 0 ? { spans_markup: across } : {}) });
322
+ }
323
+ // each text node's matches become splices; a node that cannot take them (read differently, or cut inside a character reference) is skipped and listed
324
+ const skipped = [];
325
+ const parts = [];
326
+ for (const h of hits) {
327
+ const t = h.ro ? null : textEdit(st.read, h.ranges.map((g) => ({ node: h.node, ...g, repl: op.replace })));
328
+ if (t?.ok) parts.push(t.edit);
329
+ else if (t && t.code !== "not_addressable") return refused("not_well_formed", t.why);
330
+ else skipped.push({ path: pathString(elementPath(h.node.__par)), excerpt: excerpt(h.node.value), matches: h.ranges.length });
331
+ }
332
+ const edit = emptyEdit();
333
+ for (const p of parts) for (const k of Object.keys(edit)) edit[k].push(...p[k]);
334
+ const replaced = total - skipped.reduce((n, s) => n + s.matches, 0);
335
+ return bound(st, op, edit, { replaced, ...(skipped.length ? { skipped_protected: skipped.slice(0, 50) } : {}) });
336
+ }
337
+ default:
338
+ return refused("unsupported_op", `${op.op} does not apply to an .html file`);
339
+ }
340
+ }
341
+
342
+ /** Accept the op's edit unless it changes a place an earlier op of this file changes. */
343
+ export async function exec(st, b) {
344
+ if (st.accepted.some((a) => a.edit.splices.length) && conflicts(st.accepted.map((a) => a.edit), b.edit)) return refused("overlapping_edit", "an earlier op of this file changes the same place (the same text, attribute or element, or one inside it); give one op per place, or one replace_element");
345
+ if (b.op.op === "replace_all_text" && b.detail.replaced === 0 && b.detail.skipped_protected) return { status: "skipped_protected", reason: "every text with a match cannot be edited at its exact place", detail: b.detail };
346
+ st.accepted.push({ edit: b.edit });
347
+ return { status: "applied", ...(b.detail ? { detail: b.detail } : {}) };
348
+ }
349
+
350
+ const ATOMIC = "atomic mode: another op did not apply, so nothing was written";
351
+ const notApplied = (r, reason) => ({ index: r.index, op: r.op, ...(r.id ? { id: r.id } : {}), status: "not_applied", reason, ...(r.detail ? { would_have: r.detail } : {}) });
352
+ const refusedAs = (r, code, reason, extra = {}) => ({ index: r.index, op: r.op, ...(r.id ? { id: r.id } : {}), status: "refused", code, reason, ...extra });
353
+
354
+ /**
355
+ * The end of a run (src/ops/run.js calls it instead of save and the package checks): every accepted splice at once, one proof, then the
356
+ * write check. A proof that fails is traced to the ops that fail it alone (a parse each, only then): those are refused `proof_failed`;
357
+ * in --partial mode the rest is tried again without them, in atomic mode nothing is written. A refused write check (active content,
358
+ * charset) refuses every op that would have been written, with its code. Returns runOps' answer {results, bytes, warnings, blocked}.
359
+ */
360
+ export async function finish(st, results, { partial = false } = {}) {
361
+ let live = [];
362
+ let k = 0;
363
+ results.forEach((r, i) => r.status === "applied" && live.push({ ri: i, edit: st.accepted[k++].edit }));
364
+ const done = (bytes, extra = {}) => ({ results, bytes, warnings: [], blocked: null, ...extra });
365
+ for (;;) {
366
+ if (!live.some((x) => x.edit.splices.length)) return done(null);
367
+ const { bytes, plan } = assemble(st.read, live.map((x) => x.edit));
368
+ const p = proveEdits(st.read, bytes, plan);
369
+ if (!p.ok) {
370
+ const alone = (x) => {
371
+ const a = assemble(st.read, [x.edit]);
372
+ return proveEdits(st.read, a.bytes, a.plan).ok;
373
+ };
374
+ let bad = live.length > 1 ? live.filter((x) => x.edit.splices.length && !alone(x)) : live;
375
+ if (!bad.length) bad = live; // each alone proves, together they do not
376
+ const why = p.code === "limits" ? `the result would be past a limit: ${p.why}` : "the change would also change the page somewhere else (the parser would read the new source differently), so nothing was written";
377
+ for (const x of bad) results[x.ri] = refusedAs(results[x.ri], p.code === "limits" ? "too_large" : "proof_failed", why);
378
+ live = live.filter((x) => !bad.includes(x));
379
+ if (!partial) {
380
+ for (const x of live) results[x.ri] = notApplied(results[x.ri], ATOMIC);
381
+ return done(null);
382
+ }
383
+ continue;
384
+ }
385
+ const c = checkedHtmlWrite(st.bytes, p.parsed.bytes, { read: st.read, parsed: p.parsed, beforeInv: st.inv });
386
+ if (!c.ok) {
387
+ for (const x of live) results[x.ri] = refusedAs(results[x.ri], c.code, c.reason, c.reasons ? { reasons: c.reasons } : {});
388
+ return done(null);
389
+ }
390
+ return done(c.bytes, { html: { charset_added: c.charset_added, links: c.links } });
391
+ }
392
+ }
393
+
394
+ export const close = () => {};