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
@@ -0,0 +1,60 @@
1
+ import { byFormat, env } from "./constants.js";
2
+ import { readAll, readUnits } from "./anchors.js";
3
+ import { runOps } from "./ops/run.js";
4
+ import { CapError } from "./xml.js";
5
+ import { mergeDocx } from "./merge.js";
6
+ import { mergePptx } from "./merge-pptx.js";
7
+ import { mergeXlsx } from "./merge-xlsx.js";
8
+ import { handBack, prepareMerge, proveMerge } from "./merge-proof.js";
9
+
10
+ // The merge of what the person typed while the agent worked (src/session.js mergeUserEdits), off the server thread, run by
11
+ // src/worker-host.js within a time and memory limit (src/offload.js mergeOffThread): the three versions read whole within the readers'
12
+ // caps, then the plan and the engine's run (src/merge.js, src/merge-xlsx.js, src/merge-pptx.js). One message back:
13
+ // {ok: true, bytes, merged, unplaced, copy, stable} | {cap: why} (a version past a reader's cap) | {invalid: why} | {error: why} |
14
+ // {refused: why}: the proof of the merge (src/merge-proof.js) could not account for every edit of the person's in the result, so no
15
+ // result is returned and the caller keeps the person's edits (why: counts and kinds, never document text).
16
+ // ONIONSKIN_TEST_MERGE_DELAY_MS (tests only) blocks this thread for that long first, the way a slow merge would.
17
+ // imported here, not on demand: stopping a worker while a module is still being evaluated aborts the server (src/worker-host.js)
18
+ const MERGERS = { docx: mergeDocx, xlsx: mergeXlsx, pptx: mergePptx };
19
+
20
+ export async function work({ format, base, mine, theirs, name, edits, agent }) {
21
+ const delay = Number(env("TEST_MERGE_DELAY_MS", 0));
22
+ if (delay > 0) Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, delay);
23
+ const [b, m, t] = [base, mine, theirs].map((x) => Buffer.from(x.buffer, x.byteOffset, x.byteLength));
24
+ try {
25
+ for (const bytes of [t, b, m]) readAll(readUnits(format, bytes));
26
+ } catch (err) {
27
+ return err instanceof CapError ? { cap: err.message } : { invalid: String(err?.message || err).slice(0, 200) };
28
+ }
29
+ try {
30
+ const steps = Array.isArray(agent) ? agent.map((s) => (s.route === "apply" ? { ...s, from: Buffer.from(s.from), to: Buffer.from(s.to) } : s)) : null;
31
+ const r = await mergeProven({ format, base: b, mine: m, theirs: t, name, edits, agent: steps });
32
+ if (r.refused) return r;
33
+ return { ...r, unplaced: r.unplaced.map(({ of, as, ...u }) => u) }; // the proof's refs and kinds stay here
34
+ } catch (err) {
35
+ return { error: String(err?.message || err).slice(0, 200) };
36
+ }
37
+ }
38
+
39
+ /**
40
+ * The merge and its proof, on this thread (`work` runs it in the merge worker): the planner and the engine's run, then what the person
41
+ * changed of a kind no planner carries (a header, a picture, a shape's place, a cell's style: src/merge-proof.js handBack) handed back
42
+ * with a copy of the person's file, then the proof of everything, from the bytes of the four versions alone. Returns {ok: true, bytes,
43
+ * merged, unplaced (with the proof's refs `of` and kinds `as`), copy, stable} | {refused: why}. The tests of the merge call it as well.
44
+ */
45
+ export async function mergeProven({ format, base, mine, theirs, name, edits = [], agent = null }) {
46
+ const r = await byFormat(MERGERS, format, "mergeProven")({ base, mine, theirs, name, runOps, edits, agent });
47
+ if (r.refuse) return { refused: r.refuse }; // the planner cannot tell what to carry (src/merge-xlsx.js: the page's list of edits was cut)
48
+ // ONIONSKIN_TEST_MERGE_FORGET (tests only) drops the merge's list of what it handed back, the way a planner that loses an edit would
49
+ if (env("TEST_MERGE_FORGET", "") === "1") r.unplaced = [];
50
+ // a text handed back cut at the planners' 2,000 characters is whole only in a copy of the person's file (src/merge-proof.js holds)
51
+ if (r.unplaced.some((u) => typeof u.text === "string" && u.text.length >= 2000)) r.copy = true;
52
+ // every return path, a merge that wrote nothing as well: the result is the agent's file then
53
+ const args = { base, mine, theirs, merged: r.bytes ?? theirs, read: r.read };
54
+ const ctx = prepareMerge(format, args);
55
+ const more = handBack(format, { ...args, ctx, unplaced: r.unplaced, copy: !!r.copy });
56
+ if (more.length || more.copy) (r.unplaced = [...r.unplaced, ...more]), (r.copy = true);
57
+ const why = proveMerge(format, { ...args, ctx, unplaced: r.unplaced, copy: !!r.copy });
58
+ if (why) return { refused: why };
59
+ return { ok: true, bytes: r.bytes ?? null, merged: r.merged, unplaced: r.unplaced, copy: !!r.copy, stable: r.stable ?? null };
60
+ }
package/src/merge-xlsx.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { EDITS_OVERFLOW } from "./constants.js";
1
2
  import { untrusted } from "./sanitize.js";
2
3
  import { exactHash } from "./text.js";
3
4
  import { a1, ch, parseWorkbook } from "./xlsx.js";
@@ -26,6 +27,7 @@ import { a1, ch, parseWorkbook } from "./xlsx.js";
26
27
  // all repeat across the shifted columns) is caught only by `placing`.
27
28
 
28
29
  const CAP = 2000;
30
+ const turn = () => new Promise((resolve) => setImmediate(resolve));
29
31
  const MAX_OPS = 2000;
30
32
  const cut = (t) => (t.length > CAP ? `${t.slice(0, CAP)}...` : t);
31
33
  const key = (r, c) => r * 16384 + c;
@@ -37,10 +39,21 @@ const same = (a, b) => (!a || !b ? !a && !b : (a.shared && b.shared) || (a.text
37
39
  // Cell text cannot prove that a row was inserted: it can only show that the differences are also what a shift would leave behind.
38
40
  // A line (row or column) is compared by its cells; a formula counts without its cell references, which a shift rewrites.
39
41
  const REF = /\$?[A-Za-z]{1,3}\$?\d{1,7}/g;
40
- const cellSig = (cell) => (cell.shared ? "shared" : cell.k === "formula" ? `f${exactHash(cell.text.replace(REF, "@"))}` : ch(cell));
42
+ // A short text stands for itself ("=" never starts a hash): hashing every cell of a large sheet was a sixth of its plan. Not one with
43
+ // "|", which joins a line's cells in its signature: such a text could spell out other cells (test/merge-xlsx.test.js).
44
+ const cellSig = (cell) => (cell.shared ? "shared" : cell.k === "formula" ? `f${exactHash(cell.text.replace(REF, "@"))}` : cell.text.length <= 16 && !cell.text.includes("|") ? `=${cell.text.normalize("NFC")}` : ch(cell));
41
45
 
42
- /** The rows (or columns) of a sheet: index -> {sig, cells: Map(position in the line -> cell signature)} */
46
+ /**
47
+ * The rows (or columns) of a sheet: index -> {sig, cells: Map(position in the line -> cell signature)}. Read once per sheet reading and
48
+ * axis: the plan asks for the base sheet's lines up to three times (the person's side, the agent's side, `placing`), and a sheet of
49
+ * 1,000,000 cells took 11 s to plan in an outside review, most of it building the same lines again. Callers never change them.
50
+ */
51
+ const LINES = new WeakMap();
43
52
  function lines(sd, rows) {
53
+ const kept = LINES.get(sd) || LINES.set(sd, [null, null]).get(sd);
54
+ return (kept[rows ? 0 : 1] ??= readLines(sd, rows));
55
+ }
56
+ function readLines(sd, rows) {
44
57
  const out = new Map();
45
58
  for (const cell of sd.cells.values()) {
46
59
  const [i, j] = rows ? [cell.r, cell.c] : [cell.c, cell.r];
@@ -77,12 +90,15 @@ function linesMoved(A, B) {
77
90
  const inB = where(B);
78
91
  const held = new Map(); // "position:cell signature" -> the changed lines of A that hold it
79
92
  for (const q of changed) for (const [j, h] of A.get(q)?.cells || []) (held.get(`${j}:${h}`) || held.set(`${j}:${h}`, []).get(`${j}:${h}`)).push(q);
93
+ // a changed line i never reads in A as it reads in B, so it is never among the lines of A that read like B's line i (nor the other way
94
+ // round): those lines are counted per reading once, instead of searched for each changed line (quadratic over many lines that read alike)
95
+ const changedIn = new Map(); // signature -> changed lines of A that read so
96
+ for (const q of changed) if (A.has(q)) changedIn.set(A.get(q).sig, (changedIn.get(A.get(q).sig) || 0) + 1);
80
97
  for (const i of changed) {
81
98
  const a = A.get(i);
82
99
  const b = B.get(i);
83
- const from = b ? (inA.get(b.sig) || []).filter((q) => q !== i) : [];
84
- if (a ? from.some((q) => changed.has(q)) : from.length) return true;
85
- if (a && !b && (inB.get(a.sig) || []).some((q) => q !== i)) return true;
100
+ if (b && (a ? changedIn.get(b.sig) > 0 : inA.has(b.sig))) return true;
101
+ if (a && !b && inB.has(a.sig)) return true;
86
102
  if (!b) continue;
87
103
  const votes = new Map();
88
104
  for (const [j, h] of b.cells) {
@@ -124,9 +140,21 @@ function axis(A, B) {
124
140
  };
125
141
  const endA = end(A);
126
142
  const endB = end(B);
143
+ // a cell value at a position in its line, as one number (a string key per cell made the index most of a large sheet's plan)
144
+ const values = new Map();
145
+ const key = (j, h) => {
146
+ let v = values.get(h);
147
+ if (v === undefined) values.set(h, (v = values.size));
148
+ return v * 2 ** 21 + j; // a line has fewer than 2^21 positions (1,048,576 rows)
149
+ };
127
150
  const index = (M) => {
128
151
  const m = new Map();
129
- for (const [i, line] of M) for (const [j, h] of line.cells) (m.get(`${j}:${h}`) || m.set(`${j}:${h}`, []).get(`${j}:${h}`)).push(i);
152
+ for (const [i, line] of M) for (const [j, h] of line.cells) {
153
+ const k = key(j, h);
154
+ const list = m.get(k);
155
+ if (list) list.push(i);
156
+ else m.set(k, [i]);
157
+ }
130
158
  return m;
131
159
  };
132
160
  const count = (M) => {
@@ -137,18 +165,22 @@ function axis(A, B) {
137
165
  let ix, sigs, steady;
138
166
  let work = 0;
139
167
  const rare = (key) => (ix.A.get(key)?.length || 0) <= RARE && (ix.B.get(key)?.length || 0) <= RARE;
168
+ const weights = new Map();
140
169
  const weight = (line) => {
141
- if (line.w == null) {
142
- line.w = 0;
143
- for (const [j, h] of line.cells) if (rare(`${j}:${h}`)) line.w++;
170
+ let w = weights.get(line); // kept here, not on the line: lines are shared by every reader of the sheet (lines())
171
+ if (w == null) {
172
+ w = 0;
173
+ for (const [j, h] of line.cells) if (rare(key(j, h))) w++;
174
+ weights.set(line, w);
144
175
  }
145
- return line.w;
176
+ return w;
146
177
  };
147
178
  /** how much of `line` reads as line i of M (`own`) and as the line of M it is most like apart from i (`other`): the share of the rare cells of the larger of the two */
148
179
  const shares = (i, line, M, inM) => {
149
180
  const votes = new Map();
150
181
  for (const [j, h] of line.cells) {
151
- const list = rare(`${j}:${h}`) && inM.get(`${j}:${h}`);
182
+ const k = key(j, h);
183
+ const list = rare(k) && inM.get(k);
152
184
  if (!list) continue;
153
185
  work += list.length;
154
186
  for (const q of list) votes.set(q, (votes.get(q) || 0) + 1);
@@ -281,7 +313,9 @@ export function planXlsxMerge(base, mine, theirs, edits = [], agent = UNKNOWN) {
281
313
  const items = [];
282
314
  const unplaced = [];
283
315
  let copy = false;
284
- const whole = (label, why, text = "") => (unplaced.push({ label, text: cut(text), why }), (copy = true));
316
+ // `of` names what an entry stands for, for the proof of the merge (src/merge-proof.js): "S" and a sheet key, "c" and a sheet key, "!"
317
+ // and a cell key, or "sheets" (the list of sheets)
318
+ const whole = (label, why, text = "", of = []) => (unplaced.push({ label, text: cut(text), why, of }), (copy = true));
285
319
  const inMine = new Map(mine.sheets.map((s) => [sheetKey(s), s]));
286
320
  const inTheirs = new Map(theirs.sheets.map((s) => [sheetKey(s), s]));
287
321
  const inBase = new Set(base.sheets.map(sheetKey));
@@ -328,21 +362,30 @@ export function planXlsxMerge(base, mine, theirs, edits = [], agent = UNKNOWN) {
328
362
  for (const s of mine.sheets) {
329
363
  if (inBase.has(sheetKey(s))) continue;
330
364
  sheetsChanged = true;
331
- whole(`Sheet ${named(s)}`, `You added or renamed the sheet ${named(s)}. Sheet changes are not carried over.`);
365
+ whole(`Sheet ${named(s)}`, `You added or renamed the sheet ${named(s)}. Sheet changes are not carried over.`, "", [`S${sheetKey(s)}`]);
332
366
  }
333
367
  for (const b of base.sheets) {
334
368
  const m = inMine.get(sheetKey(b));
335
369
  if (!m) {
336
370
  sheetsChanged = true;
337
- unplaced.push({ label: `Sheet ${named(b)}`, text: "", why: `You removed or renamed the sheet ${named(b)}. Sheet changes are not carried over.` });
371
+ unplaced.push({ label: `Sheet ${named(b)}`, text: "", why: `You removed or renamed the sheet ${named(b)}. Sheet changes are not carried over.`, of: [`S${sheetKey(b)}`] });
338
372
  continue;
339
373
  }
340
374
  const label = (r, c) => `${untrusted(b.name, 60)}!${a1(r, c)}`;
375
+ const box = [`S${sheetKey(b)}`];
376
+ const cellRef = (k) => [`c${sheetKey(b)}!${k}`];
341
377
  const sdB = base.sheetCells(b);
342
378
  const sdM = mine.sheetCells(m);
343
379
  const changed = [];
344
380
  for (const k of new Set([...sdB.cells.keys(), ...sdM.cells.keys()])) if (!same(sdB.cells.get(k), sdM.cells.get(k))) changed.push(k);
345
381
  changed.sort((x, y) => x - y);
382
+ // a number format changed on a cell whose value did not, which the page did not name: said too, never carried
383
+ if (!said.format.has(b.name) && !said.format.has(null)) {
384
+ for (const [k, cell] of sdB.cells) {
385
+ const now = sdM.cells.get(k);
386
+ if (now && same(cell, now) && base.numberFormat(cell.s) !== mine.numberFormat(now.s)) unplaced.push({ label: label(Math.floor(k / 16384), k % 16384), text: "", why: `You changed the number format of ${label(Math.floor(k / 16384), k % 16384)}. Formatting is not carried over.`, of: cellRef(k) });
387
+ }
388
+ }
346
389
  const mineMoved = movedMine(b);
347
390
  if (!changed.length && !mineMoved) continue;
348
391
  const values = () => {
@@ -354,9 +397,9 @@ export function planXlsxMerge(base, mine, theirs, edits = [], agent = UNKNOWN) {
354
397
  return out;
355
398
  };
356
399
  const t = inTheirs.get(sheetKey(b));
357
- if (mineMoved) whole(`Sheet ${named(b)}`, `You added, removed or moved rows or columns on the sheet ${named(b)}. That is not carried over. These cells differ in your version.`, values());
358
- else if (!byAddress(b)) whole(`Sheet ${named(b)}`, theirWhy[theirSide(b)](b), values());
359
- else if (items.length + changed.length > MAX_OPS) whole(`Sheet ${named(b)}`, `You changed more than ${MAX_OPS} cells while the agent worked. The cells on the sheet ${named(b)} were not carried over. These are yours.`, values());
400
+ if (mineMoved) whole(`Sheet ${named(b)}`, `You added, removed or moved rows or columns on the sheet ${named(b)}. That is not carried over. These cells differ in your version.`, values(), box);
401
+ else if (!byAddress(b)) whole(`Sheet ${named(b)}`, theirWhy[theirSide(b)](b), values(), box);
402
+ else if (items.length + changed.length > MAX_OPS) whole(`Sheet ${named(b)}`, `You changed more than ${MAX_OPS} cells while the agent worked. The cells on the sheet ${named(b)} were not carried over. These are yours.`, values(), box);
360
403
  else {
361
404
  const sdT = theirs.sheetCells(t);
362
405
  for (const k of changed) {
@@ -367,30 +410,33 @@ export function planXlsxMerge(base, mine, theirs, edits = [], agent = UNKNOWN) {
367
410
  const now = sdT.cells.get(k);
368
411
  if (same(now, want)) continue; // the agent wrote the same value
369
412
  if (!same(now, was)) {
370
- unplaced.push({ label: label(r, c), text: cut(want?.text ?? ""), why: want ? `The agent changed ${label(r, c)} too. Its value is in the sheet; this is yours.` : `You emptied ${label(r, c)}, but the agent changed it, so it was kept.` });
413
+ unplaced.push({ label: label(r, c), text: cut(want?.text ?? ""), why: want ? `The agent changed ${label(r, c)} too. Its value is in the sheet; this is yours.` : `You emptied ${label(r, c)}, but the agent changed it, so it was kept.`, of: cellRef(k) });
371
414
  continue;
372
415
  }
373
416
  if (theirSide(b) === "stable" && agentRows(b).has(r)) {
374
- unplaced.push({ label: label(r, c), text: cut(want?.text ?? ""), why: want ? `The agent changed other cells in the same row as ${label(r, c)}, so that row may hold other content now. Your value was not carried over. This is yours.` : `You emptied ${label(r, c)}, but the agent changed other cells in the same row, so it was kept.` });
417
+ unplaced.push({ label: label(r, c), text: cut(want?.text ?? ""), why: want ? `The agent changed other cells in the same row as ${label(r, c)}, so that row may hold other content now. Your value was not carried over. This is yours.` : `You emptied ${label(r, c)}, but the agent changed other cells in the same row, so it was kept.`, of: cellRef(k) });
375
418
  continue;
376
419
  }
377
420
  if (theirSide(b) === "stable" && !place(b).cell(r, c)) {
378
- unplaced.push({ label: label(r, c), text: cut(want?.text ?? ""), why: want ? `The agent changed so much around ${label(r, c)} that it may not be the same cell any more, so your value was not carried over. This is yours.` : `You emptied ${label(r, c)}, but the agent changed so much around it that it may not be the same cell any more, so it was kept.` });
421
+ unplaced.push({ label: label(r, c), text: cut(want?.text ?? ""), why: want ? `The agent changed so much around ${label(r, c)} that it may not be the same cell any more, so your value was not carried over. This is yours.` : `You emptied ${label(r, c)}, but the agent changed so much around it that it may not be the same cell any more, so it was kept.`, of: cellRef(k) });
379
422
  continue;
380
423
  }
381
424
  if (want?.k === "formula" && !formulasFit()) {
382
- unplaced.push({ label: label(r, c), text: cut(want.text), why: `Rows or columns may have moved while the agent worked, so your formula for ${label(r, c)} may point at other cells now. It was not carried over. This is yours.` });
425
+ unplaced.push({ label: label(r, c), text: cut(want.text), why: `Rows or columns may have moved while the agent worked, so your formula for ${label(r, c)} may point at other cells now. It was not carried over. This is yours.`, of: cellRef(k) });
383
426
  continue;
384
427
  }
385
- items.push({ label: label(r, c), text: cut(want?.text ?? ""), sheet: sheetKey(b), k, want, op: { op: "set_cells", target: { sheet: b.name, range: a1(r, c) }, expect: { text_hash: exactHash(was?.text ?? "") }, values: want ? want.text : null } });
428
+ items.push({ label: label(r, c), of: cellRef(k), text: cut(want?.text ?? ""), sheet: sheetKey(b), k, want, op: { op: "set_cells", target: { sheet: b.name, range: a1(r, c) }, expect: { text_hash: exactHash(was?.text ?? "") }, values: want ? want.text : null } });
386
429
  }
387
430
  }
388
431
  }
389
432
  const order = (wb) => wb.sheets.map(sheetKey).filter((x) => inBase.has(x) && inMine.has(x)).join("\n");
390
- if (!sheetsChanged && (said.book || order(base) !== order(mine))) unplaced.push({ label: "Sheets", text: "", why: "You moved, hid or changed sheets. Sheet changes are not carried over." });
433
+ // sheets moved are said even when sheets were also added or removed (those lines name only the sheets added or removed: a move next to
434
+ // them was dropped without a word, found by test/merge-xlsx-property.test.js)
435
+ if (order(base) !== order(mine) || (said.book && !sheetsChanged)) unplaced.push({ label: "Sheets", text: "", why: "You moved, hid or changed sheets. Sheet changes are not carried over.", of: ["sheets"] });
391
436
  const where = (sheet) => (sheet == null ? "" : ` on the sheet "${untrusted(sheet, 60)}"`);
392
- for (const sheet of said.format) unplaced.push({ label: "Formatting", text: "", why: `You changed formatting${where(sheet)}. Formatting is not carried over.` });
393
- for (const sheet of said.layout) unplaced.push({ label: "Layout", text: "", why: `You changed row or column sizes, hidden rows or columns, or the frozen panes${where(sheet)}. That is not carried over.` });
437
+ const sheetRef = (sheet) => mine.sheets.filter((s) => sheet == null || s.name === sheet).map((s) => `S${sheetKey(s)}`);
438
+ for (const sheet of said.format) unplaced.push({ label: "Formatting", text: "", why: `You changed formatting${where(sheet)}. Formatting is not carried over.`, of: sheetRef(sheet) });
439
+ for (const sheet of said.layout) unplaced.push({ label: "Layout", text: "", why: `You changed row or column sizes, hidden rows or columns, or the frozen panes${where(sheet)}. That is not carried over.`, of: sheetRef(sheet) });
394
440
  const stable = base.sheets.filter((b) => inMine.has(sheetKey(b)) && !movedMine(b) && byAddress(b)).map((b) => b.name);
395
441
  return { items, unplaced, copy, stable };
396
442
  }
@@ -426,11 +472,18 @@ function verify(theirs, out, applied) {
426
472
  * that did not land as the user had it is taken out and the rest is run again from the agent's bytes (once), so it is never written.
427
473
  */
428
474
  export async function mergeXlsx({ base, mine, theirs, name, runOps, edits = [], agent = null }) {
475
+ // which sheets had rows or columns moved comes from the page's list of edits as well as from the bytes: a list cut short cannot say
476
+ // that none were, so nothing is carried by address on its word (the merge returns no file; src/merge-worker.js)
477
+ if ((edits || []).some((e) => e?.kind === EDITS_OVERFLOW)) return { refuse: "more edits were made than Onionskin can follow; it cannot tell whether rows or columns moved" };
429
478
  const T = parseWorkbook(theirs);
430
- const plan = planXlsxMerge(parseWorkbook(base), parseWorkbook(mine), T, edits, await agentChanges(agent, { runOps, name }));
479
+ const facts = await agentChanges(agent, { runOps, name });
480
+ await turn(); // between the readings and the plan (the sheets are read as the plan needs them)
481
+ const read = { base: parseWorkbook(base), mine: parseWorkbook(mine), theirs: T }; // also for the proof of the merge (src/merge-worker.js)
482
+ const plan = planXlsxMerge(read.base, read.mine, T, edits, facts);
483
+ await turn();
431
484
  const unplaced = [...plan.unplaced];
432
- const fail = (it, why) => unplaced.push({ label: it.label, text: it.text, why });
433
- const end = (bytes, merged) => ({ bytes, merged, unplaced, copy: plan.copy, stable: plan.stable });
485
+ const fail = (it, why) => unplaced.push({ label: it.label, text: it.text, why, of: it.of });
486
+ const end = (bytes, merged) => ({ bytes, merged, unplaced, copy: plan.copy, stable: plan.stable, read });
434
487
  let items = plan.items;
435
488
  for (let pass = 0; pass < 2 && items.length; pass++) {
436
489
  const { results, bytes, blocked } = await runOps({ bytes: theirs, format: "xlsx", name, ops: items.map((x) => x.op), partial: true });
@@ -439,10 +492,12 @@ export async function mergeXlsx({ base, mine, theirs, name, runOps, edits = [],
439
492
  return end(null, 0);
440
493
  }
441
494
  const applied = items.filter((_, n) => results[n]?.status === "applied");
495
+ await turn();
442
496
  const { foreign, bad } = verify(T, parseWorkbook(bytes), applied);
443
497
  if (foreign) break;
498
+ const isApplied = new Set(applied); // includes() per item was quadratic
444
499
  for (const it of items) {
445
- if (!applied.includes(it)) fail(it, `Your value for ${it.label} could not be placed on the agent's version.`);
500
+ if (!isApplied.has(it)) fail(it, `Your value for ${it.label} could not be placed on the agent's version.`);
446
501
  else if (bad.has(it)) fail(it, `Your value for ${it.label} did not land as typed, so the agent's version was kept there.`);
447
502
  }
448
503
  if (!bad.size) return end(bytes, applied.length);