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
@@ -0,0 +1,19 @@
1
+ // The text hash of a pending HTML text edit (PLAN-html R9): one function for the page (web/src/html-view.js) and the server
2
+ // (src/html-write.js), so both read the same 14 hex digits. Pure, no imports. Fixed vectors: test/html-view.test.js.
3
+
4
+ /**
5
+ * cyrb53 over the exact text (UTF-16 code units, nothing normalized), 14 hex digits. Not a security hash: it tells "the text the edit
6
+ * was made on" from another text, and the version's sha256 travels with it.
7
+ */
8
+ export function editHash(s) {
9
+ let h1 = 0xdeadbeef;
10
+ let h2 = 0x41c6ce57;
11
+ for (let i = 0; i < s.length; i++) {
12
+ const c = s.charCodeAt(i);
13
+ h1 = Math.imul(h1 ^ c, 2654435761);
14
+ h2 = Math.imul(h2 ^ c, 1597334677);
15
+ }
16
+ h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
17
+ h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
18
+ return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(16).padStart(14, "0");
19
+ }
package/src/fsutil.js CHANGED
@@ -19,7 +19,7 @@ export function checkRegularOwned(file) {
19
19
  /** Reads a document, refusing by size first (an oversized file is never pulled into memory). */
20
20
  export function readDoc(file, max = MAX_DOC_BYTES) {
21
21
  const st = fs.statSync(file);
22
- if (st.size > max) throw Object.assign(new Error(`${path.basename(file)} is larger than ${Math.round(max / 1024 / 1024)} MB`), { code: "TOO_LARGE" });
22
+ if (st.size > max) throw Object.assign(new Error(`${path.basename(file)} is larger than ${Math.round(max / 1024 / 1024)} MB, more than Onionskin can read. Make it smaller (pictures and videos are often most of a file) or split it, and open that`), { code: "TOO_LARGE" });
23
23
  return fs.readFileSync(file);
24
24
  }
25
25
 
@@ -92,16 +92,13 @@ export function acquireLock(file, { waitMs = 5000 } = {}) {
92
92
  stale = e.code === "ESRCH";
93
93
  }
94
94
  } else stale = Date.now() - fs.statSync(lock).mtimeMs > 2000; // being created, or garbage
95
- } catch {
96
- continue; // released between our attempts
97
- }
95
+ } catch {} // it may have been released, or its pid may be unreadable
98
96
  if (stale) {
99
97
  try {
100
98
  fs.unlinkSync(lock);
101
99
  } catch {}
102
- continue;
103
100
  }
104
- if (Date.now() > deadline) throw new LockBusy(file);
101
+ if (Date.now() >= deadline) throw new LockBusy(file);
105
102
  sleepSync(20);
106
103
  }
107
104
  }
@@ -209,6 +206,8 @@ export function replaceGuarded(file, bytes, { expectSha = null, snapshotBefore =
209
206
  const dfd = fs.openSync(path.dirname(file), "r");
210
207
  try {
211
208
  fs.fsyncSync(dfd);
209
+ } catch (err) {
210
+ if (err?.code !== "EINVAL" && err?.code !== "ENOTSUP" && err?.code !== "EPERM" && err?.code !== "EROFS") throw err;
212
211
  } finally {
213
212
  fs.closeSync(dfd);
214
213
  }
@@ -258,6 +257,15 @@ export function writeSiblingCopy(file, bytes, { now = new Date(), label = "Onion
258
257
  fs.linkSync(tmp, dest);
259
258
  return { name, path: dest };
260
259
  } catch (err) {
260
+ if (err.code === "ENOTSUP" || err.code === "EXDEV" || err.code === "EPERM") {
261
+ try {
262
+ fs.copyFileSync(tmp, dest, fs.constants.COPYFILE_EXCL);
263
+ return { name, path: dest };
264
+ } catch (copyErr) {
265
+ if (copyErr.code !== "EEXIST") throw copyErr;
266
+ continue;
267
+ }
268
+ }
261
269
  if (err.code !== "EEXIST") throw err;
262
270
  }
263
271
  }
@@ -1,16 +1,16 @@
1
1
  import fs from "node:fs";
2
- import { parentPort, workerData } from "node:worker_threads";
2
+ import "./html-parser.js"; // an HTML file's versions are compared here too: the parser lives in worker threads only
3
3
  import { compareBytes } from "./history.js";
4
4
 
5
- // One message back: a result per job, in order. A version that cannot be read or parsed fails its own job only.
6
- const bytes = new Map(); // blob path -> Buffer, so a version shared by two pairs is read (and parsed) once
7
- const read = (f) => bytes.get(f) || (bytes.set(f, fs.readFileSync(f)), bytes.get(f));
8
- parentPort.postMessage(
9
- workerData.jobs.map((j) => {
5
+ // Run by src/worker-host.js. One message back: a result per job, in order. A version that cannot be read or parsed fails its own job only.
6
+ export function work(data) {
7
+ const bytes = new Map(); // blob path -> Buffer, so a version shared by two pairs is read (and parsed) once
8
+ const read = (f) => bytes.get(f) || (bytes.set(f, fs.readFileSync(f)), bytes.get(f));
9
+ return data.jobs.map((j) => {
10
10
  try {
11
11
  return { ok: true, result: compareBytes(j.format, read(j.before), read(j.after), { places: j.places }) };
12
12
  } catch (err) {
13
13
  return { ok: false, error: String(err?.message || err) };
14
14
  }
15
- })
16
- );
15
+ });
16
+ }
package/src/history.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import crypto from "node:crypto";
2
2
  import fs from "node:fs";
3
3
  import path from "node:path";
4
- import { Worker } from "node:worker_threads";
5
- import { env } from "./constants.js";
4
+ import { env, unknownFormat } from "./constants.js";
6
5
  import { readUnits } from "./anchors.js";
7
6
  import { diffText } from "./diff.js";
8
7
  import { exactHash, normText, similarity } from "./text.js";
@@ -10,6 +9,9 @@ import { fmtNote } from "./fmt.js";
10
9
  import { sub, writeStateFile } from "./paths.js";
11
10
  import { untrusted } from "./sanitize.js";
12
11
  import { a1, a1Range, ch, sheetRows } from "./xlsx.js";
12
+ import { startWorker } from "./offload.js";
13
+ import { elementPath, ownHash, ownText, pathString, safeTag } from "./html.js";
14
+ import { inventory, inventoryDelta } from "./html-active.js";
13
15
 
14
16
  // Change history: the last updates to the document, built from the version snapshots the session already keeps. Read-only: nothing here
15
17
  // writes the document. One update = one round (the user's own edits saved by one Send, one agent run, one revert, one outside change
@@ -147,38 +149,81 @@ const docxLocator = (p) => (p ? { format: "docx", body_path: p.path ?? null, par
147
149
  export const docxFmt = (p) => ({ props: p.fmt, style: p.style, styleName: p.styleName, align: p.align });
148
150
 
149
151
  /**
150
- * Match paragraphs the way the durable anchors resolve: same text in order (LCS on the text hash), the same text moved elsewhere,
151
- * the same w14:paraId with edited text, then the best similar unmatched paragraph (>= 0.3 alone in its gap, >= 0.5 within a gap,
152
- * >= 0.6 anywhere), all under a work budget. Returns {mapA, mapB, moved}.
152
+ * Paragraph identity between two versions of a document, the one rule every pairing of DOCX paragraphs follows (History, both sides
153
+ * of the merge, redline, voice). A paragraph id (w14:paraId) that is unique in its version is the paragraph's identity: a text match
154
+ * may never pair it with a paragraph other than the one carrying that id on the other side, and no paragraph is paired with one that
155
+ * carries the id of another paragraph. Text and order decide where ids are absent or duplicated, and where an id is found in one
156
+ * version only (it says nothing about the other: a re-save that gave every paragraph a new id still reads as unchanged). `strict` (the
157
+ * agent's side of the merge): a paragraph with an id of its own that the other version, which has ids, does not have at all was
158
+ * removed or rebuilt, and is paired with nothing.
159
+ * Returns {may, keyA, keyB}: `may(i, j)` says whether A[i] and B[j] may be the same paragraph; `keyA` and `keyB` are LCS keys (the
160
+ * text hash, then "#" and the id when that id is unique in both versions) so that order matching links no two paragraphs that
161
+ * different ids tell apart.
153
162
  */
154
- export function matchParagraphs(A, B) {
155
- const { mapA, mapB } = lcsPairs(A.map((p) => p.hash), B.map((p) => p.hash));
163
+ export function paraIdentity(A, B, { strict = false } = {}) {
164
+ const tally = (L) => {
165
+ const m = new Map();
166
+ for (const p of L) if (p.paraId) m.set(p.paraId, (m.get(p.paraId) || 0) + 1);
167
+ return m;
168
+ };
169
+ const inA = tally(A);
170
+ const inB = tally(B);
171
+ const may = (i, j) => {
172
+ const a = A[i].paraId || null;
173
+ const b = B[j].paraId || null;
174
+ if (a === b) return true; // the same id, or neither has one
175
+ const ownA = a && inA.get(a) === 1;
176
+ const ownB = b && inB.get(b) === 1;
177
+ if (ownA && (inB.has(a) || (strict && inB.size))) return false; // A[i] is the paragraph with its id there; strict: or it is gone
178
+ if (ownB && inA.has(b)) return false; // the same from B's side
179
+ if ((b && inA.get(b) === 1) || (a && inB.get(a) === 1)) return false; // it carries the id of another paragraph
180
+ return true; // ids absent, duplicated, or found on one side only (a re-save that renumbered every id): text and order decide
181
+ };
182
+ const sure = (id, own, other) => (id && own.get(id) === 1 && other.get(id) === 1 ? id : "");
183
+ return {
184
+ may,
185
+ keyA: A.map((p) => `${p.hash}#${sure(p.paraId, inA, inB)}`),
186
+ keyB: B.map((p) => `${p.hash}#${sure(p.paraId, inB, inA)}`),
187
+ };
188
+ }
189
+
190
+ /**
191
+ * Match paragraphs the way the durable anchors resolve, under the identity rule of paraIdentity: same text in order (LCS on the text
192
+ * hash, ids told apart), the same unique id (moved when the text is the same), the same text moved elsewhere, then the best similar
193
+ * unmatched paragraph (>= 0.3 alone in its gap, >= 0.5 within a gap, >= 0.6 anywhere), all under a work budget. No step pairs two
194
+ * paragraphs the ids tell apart. Returns {mapA, mapB, moved}.
195
+ */
196
+ export function matchParagraphs(A, B, { strict = false } = {}) {
197
+ const { may, keyA, keyB } = paraIdentity(A, B, { strict });
198
+ const { mapA, mapB } = lcsPairs(keyA, keyB);
199
+ // keys equal by text with no shared id can still be two different identities (ids unique on both sides, present on one only)
200
+ for (let i = 0; i < A.length; i++) if (mapA[i] >= 0 && !may(i, mapA[i])) ((mapB[mapA[i]] = -1), (mapA[i] = -1));
156
201
  const lcsMapB = Int32Array.from(mapB);
157
202
  const moved = new Set();
158
203
  const link = (i, j) => ((mapA[i] = j), (mapB[j] = i));
204
+ // the same unique id on both sides: the same paragraph, wherever it is; out of the order matched above with the same text, moved
205
+ const idOf = (k) => k.slice(k.indexOf("#") + 1); // "" when the id does not decide
206
+ const idAt = new Map();
207
+ keyA.forEach((k, i) => mapA[i] < 0 && idOf(k) && idAt.set(idOf(k), i));
208
+ keyB.forEach((k, j) => {
209
+ const i = mapB[j] < 0 && idOf(k) ? idAt.get(idOf(k)) : undefined;
210
+ if (i === undefined) return;
211
+ link(i, j);
212
+ if (A[i].hash === B[j].hash) moved.add(j);
213
+ });
159
214
  const long = (p) => normText(p.text).length >= 20; // short or empty texts repeat: a "move" of one would be a coincidence
160
215
  const queue = new Map();
161
216
  A.forEach((p, i) => mapA[i] < 0 && long(p) && (queue.get(p.hash) || queue.set(p.hash, []).get(p.hash)).push(i));
217
+ // Ceiling: candidates the ids rule out are skipped within a budget for the whole pass (many copies of one long text, each with an id
218
+ // of its own the other version does not have, would make the search quadratic); past it, the remaining candidates are not tried.
219
+ let tries = 100_000;
162
220
  B.forEach((p, j) => {
163
221
  const q = mapB[j] < 0 && queue.get(p.hash);
164
- if (q?.length) (link(q.shift(), j), moved.add(j));
165
- });
166
- const idCount = (L) => {
167
- const m = new Map();
168
- for (const p of L) if (p.paraId) m.set(p.paraId, (m.get(p.paraId) || 0) + 1);
169
- return m;
170
- };
171
- const idsA = idCount(A);
172
- const idsB = idCount(B);
173
- const byId = new Map();
174
- A.forEach((p, i) => mapA[i] < 0 && p.paraId && idsA.get(p.paraId) === 1 && byId.set(p.paraId, i));
175
- B.forEach((p, j) => {
176
- if (mapB[j] >= 0 || !p.paraId || idsB.get(p.paraId) !== 1) return;
177
- const i = byId.get(p.paraId);
178
- if (i != null && mapA[i] < 0) link(i, j);
222
+ const k = q ? q.findIndex((i) => tries-- > 0 && may(i, j)) : -1;
223
+ if (k >= 0) (link(q.splice(k, 1)[0], j), moved.add(j));
179
224
  });
180
225
  let budget = 20_000; // similarity calls in all
181
- const sim = (i, j) => (budget-- > 0 ? similarity(A[i].text.slice(0, 600), B[j].text.slice(0, 600)) : 0);
226
+ const sim = (i, j) => (may(i, j) && budget-- > 0 ? similarity(A[i].text.slice(0, 600), B[j].text.slice(0, 600)) : 0);
182
227
  // gap pass: unmatched paragraphs between the same two matched neighbors
183
228
  const prevA = new Int32Array(B.length);
184
229
  const nextA = new Int32Array(B.length);
@@ -191,6 +236,7 @@ export function matchParagraphs(A, B) {
191
236
  let gapPrev = -2;
192
237
  let floor = -1;
193
238
  for (let j = 0; j < B.length; j++) {
239
+ if (budget <= 0) break; // no similarity left to link with: listing the candidates of every paragraph of a long gap was quadratic
194
240
  if (mapB[j] >= 0) continue;
195
241
  if (prevA[j] !== gapPrev) (gapPrev = prevA[j], (floor = prevA[j]));
196
242
  const cand = [];
@@ -332,9 +378,11 @@ function compareSheet(sheet, rowsA, rowsB, out, wbA, wbB) {
332
378
 
333
379
  function compareXlsx(wbA, wbB, out) {
334
380
  const claimed = new Set();
381
+ const namesB = new Map(); // sheet name -> how many sheets of B have it, counted once (MAX_SHEETS bounds the scans by name)
382
+ for (const y of wbB.sheets) namesB.set(y.name, (namesB.get(y.name) || 0) + 1);
335
383
  const sheetLabel = (s) => `Sheet "${clean(s.name, 60)}"`;
336
384
  for (const sb of wbB.sheets) {
337
- const sa = wbA.sheets.find((x) => x.name === sb.name && !claimed.has(x)) || wbA.sheets.find((x) => x.sheetId === sb.sheetId && !claimed.has(x) && !wbB.sheets.some((y) => y !== sb && y.name === x.name));
385
+ const sa = wbA.sheets.find((x) => x.name === sb.name && !claimed.has(x)) || wbA.sheets.find((x) => x.sheetId === sb.sheetId && !claimed.has(x) && !(namesB.get(x.name) > (x.name === sb.name ? 1 : 0)));
338
386
  if (!sa) {
339
387
  out.add("added", () => place({ format: "xlsx", label: sheetLabel(sb), locator: xlsxLocator(sb, "", null), change: "added", note: "sheet added" }));
340
388
  continue;
@@ -384,13 +432,22 @@ function compareShapes(sa, sb, out) {
384
432
  if (old) (used.add(old), pair(old, sh));
385
433
  else unmatchedB.push(sh);
386
434
  }
387
- const leftA = sa.shapes.filter((x) => !used.has(x));
388
- const byName = (L, x) => L.filter((y) => y.name === x.name && y.kind === x.kind);
435
+ // by name and kind among the shapes left unpaired, grouped once (a scan of every shape per shape was quadratic on a slide of many shapes)
436
+ const nk = (x) => `${x.kind}\u0000${x.name}`;
437
+ const groupsA = new Map();
438
+ for (const x of sa.shapes) if (!used.has(x)) (groupsA.get(nk(x)) || groupsA.set(nk(x), new Set()).get(nk(x))).add(x);
439
+ const countB = new Map();
440
+ for (const x of unmatchedB) countB.set(nk(x), (countB.get(nk(x)) || 0) + 1);
389
441
  for (const sh of unmatchedB) {
390
- const cand = sh.name ? byName(leftA, sh) : [];
391
- if (cand.length === 1 && byName(unmatchedB, sh).length === 1) (leftA.splice(leftA.indexOf(cand[0]), 1), pair(cand[0], sh));
392
- else out.add("added", () => place({ format: "pptx", label: shapeLabel(sb, sh), locator: shapeLocator(sb, sh), change: "added", text: sh.text ? clean(sh.text, TEXT_CAP) : null, note: sh.text ? null : `${KIND_NOTE[sh.kind] || "an object"} was added` }));
442
+ const cand = sh.name ? groupsA.get(nk(sh)) : null;
443
+ if (cand?.size === 1 && countB.get(nk(sh)) === 1) {
444
+ const [old] = cand;
445
+ cand.delete(old);
446
+ used.add(old);
447
+ pair(old, sh);
448
+ } else out.add("added", () => place({ format: "pptx", label: shapeLabel(sb, sh), locator: shapeLocator(sb, sh), change: "added", text: sh.text ? clean(sh.text, TEXT_CAP) : null, note: sh.text ? null : `${KIND_NOTE[sh.kind] || "an object"} was added` }));
393
449
  }
450
+ const leftA = sa.shapes.filter((x) => !used.has(x));
394
451
  for (const sh of leftA) out.add("removed", () => place({ format: "pptx", label: shapeLabel(sa, sh), locator: null, change: "removed", text: sh.text ? clean(sh.text, TEXT_CAP) : null, note: sh.text ? null : `${KIND_NOTE[sh.kind] || "an object"} was removed` }));
395
452
  function pair(old, sh) {
396
453
  if (old.hash === sh.hash) {
@@ -402,6 +459,60 @@ function compareShapes(sa, sb, out) {
402
459
  }
403
460
  }
404
461
 
462
+ // ---- HTML -------------------------------------------------------------------------------------------------------------
463
+ // A text-level diff by element (PLAN-html 2a): the units of each version (elements that hold text directly, and images and other leaves,
464
+ // src/html.js) matched by tag and own text in order; a pair left between two matches with the same tag is a change of its text, the rest
465
+ // added or removed. Link and image targets that appeared or went away are listed too, as plain text (R1): an agent may change a link.
466
+
467
+ const htmlUnitText = (u) => (u.kind === "leaf" ? `${u.tag} ${u.node.attrs.find((a) => a.name === "alt")?.value ?? ""}`.trim() : ownText(u.node));
468
+ const htmlLabel = (u) => untrusted(`${safeTag(u.tag)}${u.htmlId ? ` #${u.htmlId}` : ""}`, 100);
469
+ const htmlLocator = (read, u) => ({ format: "html", node_id: u.nodeId, path: pathString(elementPath(u.node)), element_id: u.htmlId ?? null, text_hash: ownHash(u.node) });
470
+
471
+ function compareHtml(RA, RB, out) {
472
+ const key = (u) => `${u.tag}\u0000${exactHash(htmlUnitText(u))}`;
473
+ const A = RA.units;
474
+ const B = RB.units;
475
+ const { mapA, mapB } = lcsPairs(A.map(key), B.map(key));
476
+ // walk the gaps between matched pairs: same-tag units across a gap pair up in order (a changed text), the rest are added or removed
477
+ let i = 0;
478
+ let j = 0;
479
+ const flushGap = (iEnd, jEnd) => {
480
+ const gapA = [];
481
+ const byTag = new Map();
482
+ for (let x = i; x < iEnd; x++) {
483
+ if (mapA[x] >= 0) continue;
484
+ const ua = A[x];
485
+ gapA.push(ua);
486
+ let entry = byTag.get(ua.tag);
487
+ if (!entry) byTag.set(ua.tag, (entry = { list: [], idx: 0 }));
488
+ entry.list.push(ua);
489
+ }
490
+ const used = new Set();
491
+ for (let y = j; y < jEnd; y++) {
492
+ if (mapB[y] >= 0) continue;
493
+ const ub = B[y];
494
+ const entry = byTag.get(ub.tag);
495
+ const ua = entry && entry.idx < entry.list.length ? entry.list[entry.idx++] : null;
496
+ if (ua) {
497
+ used.add(ua);
498
+ out.add("changed", () => place({ format: "html", label: htmlLabel(ub), locator: htmlLocator(RB, ub), ...textChange(htmlUnitText(ua), htmlUnitText(ub)) }));
499
+ } else out.add("added", () => place({ format: "html", label: htmlLabel(ub), locator: htmlLocator(RB, ub), change: "added", text: clean(normText(htmlUnitText(ub)), TEXT_CAP) || null, note: ub.kind === "leaf" ? `${safeTag(ub.tag)} added` : null }));
500
+ }
501
+ for (const ua of gapA) if (!used.has(ua)) out.add("removed", () => place({ format: "html", label: htmlLabel(ua), locator: null, change: "removed", text: clean(normText(htmlUnitText(ua)), TEXT_CAP) || null, note: ua.kind === "leaf" ? `${safeTag(ua.tag)} removed` : null }));
502
+ };
503
+ for (let y = 0; y <= B.length; y++) {
504
+ if (y < B.length && mapB[y] < 0) continue;
505
+ const x = y < B.length ? mapB[y] : A.length;
506
+ flushGap(x, y);
507
+ i = x + 1;
508
+ j = y + 1;
509
+ }
510
+ const d = inventoryDelta(inventory(RA.doc), inventory(RB.doc)).links;
511
+ const what = (e) => (e.kind === "link" ? "Link" : "Image or style");
512
+ for (const e of d.added) out.add("changed", () => place({ format: "html", label: untrusted(`${what(e)} target`, 60), locator: null, change: "link", note: clean(`now points to ${e.url}`, 300) }));
513
+ for (const e of d.removed) out.add("changed", () => place({ format: "html", label: untrusted(`${what(e)} target`, 60), locator: null, change: "link", note: clean(`no longer points to ${e.url}`, 300) }));
514
+ }
515
+
405
516
  /** Compare two versions of one format: {counts: {changed, added, removed}, places: [...] | null, more}. Pure; runs in the worker. */
406
517
  export function compareBytes(format, before, after, { places = false } = {}) {
407
518
  const out = new Out(places);
@@ -409,7 +520,9 @@ export function compareBytes(format, before, after, { places = false } = {}) {
409
520
  const B = parse(format, after);
410
521
  if (format === "docx") compareDocx(A.paragraphs, B.paragraphs, out);
411
522
  else if (format === "xlsx") compareXlsx(A.wb, B.wb, out);
412
- else compareDeck(A.deck, B.deck, out);
523
+ else if (format === "pptx") compareDeck(A.deck, B.deck, out);
524
+ else if (format === "html") compareHtml(A.read, B.read, out);
525
+ else unknownFormat(format, "compareBytes");
413
526
  return { counts: out.counts, places: out.places, more: out.places ? out.total - out.places.length : 0 };
414
527
  }
415
528
 
@@ -450,7 +563,7 @@ function runInWorker(jobs) {
450
563
  const failAll = (why) => finish(jobs.map(() => ({ ok: false, error: why })));
451
564
  const timer = setTimeout(() => failAll("timed out"), WORKER_TIMEOUT_MS());
452
565
  try {
453
- worker = new Worker(new URL("./history-worker.js", import.meta.url), { workerData: { jobs } });
566
+ worker = startWorker("history-worker.js", { workerData: { jobs } });
454
567
  } catch (err) {
455
568
  return failAll(String(err?.message || err));
456
569
  }