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/xlsx.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import { colLetters, parseA1 } from "./ops/common.js";
2
2
  import { untrusted } from "./sanitize.js";
3
- import { exactHash, normText } from "./text.js";
4
- import { walkXml } from "./xml.js";
3
+ import crypto from "node:crypto";
4
+ import { nfc } from "./nfc.js";
5
+ import { exactHash, normText, spend, weigh } from "./text.js";
6
+ import { CapError, readBudget, tooMany, walkXml } from "./xml.js";
5
7
  import { openZip } from "./zip.js";
6
8
 
7
9
  // XLSX durable anchors. Everything is read from the bytes with the small XML walker (no engine). Names are matched by local name,
@@ -20,8 +22,53 @@ const EMPTY = exactHash("");
20
22
  /** hash of a parsed cell's text, computed once per cell (resolution and the diff would otherwise hash every cell per anchor) */
21
23
  export const ch = (cell) => (cell.h ??= exactHash(cell.text));
22
24
  const short = (s, n = 80) => normText(s).slice(0, n);
23
- const MAX_CELLS = 1_500_000;
25
+ // The request's budget (src/text.js scanBudget) pays for every character hashed or cleaned, at the cell, the first time (ch and sh keep
26
+ // the result on the cell), and an anchor being made pays like one being resolved: a text the budget cannot pay for is not read (no hash,
27
+ // no header). An anchor made without its hash still goes out with its address and the context that was paid for (xlsxDurable); a hit
28
+ // whose text cannot be paid for while resolving is not one ("moved or removed" when nothing else places it). The cost of a cell's text
29
+ // is weigh() (src/text.js), kept on the cell: non-ASCII text costs the CPU about four times what ASCII does. The reviewer's case: 50
30
+ // anchors on A1:B1 through A1:AY1 of a cell of 31 million "é" each joined and hashed the cell, 6.6 s of CPU, while an anchor being made
31
+ // was charged whatever was left and went on.
32
+ const costOf = (cell) => (cell.w ??= weigh(cell.text));
33
+ const spent = (cell) => spend(costOf(cell));
34
+ /** short() of a parsed cell's text (or "" for no cell, or past the budget), computed once per cell like ch(): resolving one anchor compares
35
+ * the same header or neighbor with every hit, and normalizing a 20,000-character header per hit held the server 4 s on a column of 248,000 equal cells */
36
+ const sh = (cell) => (!cell ? "" : cell.sh ?? (spent(cell) ? (cell.sh = short(cell.text)) : ""));
37
+ /** ch(cell), paid from the budget when the cell's text is not hashed yet; null past it */
38
+ const chPaid = (cell) => (cell.h !== undefined || spent(cell) ? ch(cell) : null);
39
+ // What one reading of a workbook may hold, over every sheet it reads. The server reads a workbook on its own thread at Send, at each delivery
40
+ // to an agent, for a review and at the end of a run, so these bound how long that keeps it from answering (outside review: a sheet of
41
+ // 1.5 million cells held it 1.6 s; 100,000 merged ranges made a review take 18 s). Cells are counted as written (<c> elements), empty or
42
+ // not. A file past a cap is refused with the reason; it is never read in part. Set from measurement (docs/GUIDE.md "Size limits"), at a
43
+ // load average of 13 to 21 here: a plain workbook of 999,000 cells (10 to a row, half numbers, half short shared strings) kept /health
44
+ // waiting at most 0.74 to 0.83 s (the Send) and the server at 1.5 GB; with 30 more characters in each text, 0.86 s and 1.6 GB; 999,000
45
+ // cells on one shared string, with 49,000 merged ranges, or 100 number cells to a row, 0.57 to 0.65 s. 0.0.3 took 3.1 s at 1,000,000.
46
+ // A higher cap needs the Send's whole reading moved into a worker (the Send is the longest step, and the memory grows with it).
47
+ export const MAX_CELLS = 1_000_000;
48
+ export const MAX_MERGES = 50_000;
49
+ export const MAX_SHARED_STRINGS = 1_000_000;
50
+ export const MAX_NAMES = 20_000;
51
+ export const MAX_SHEETS = 1000;
52
+ // Characters of a sheet's name. Excel allows 31; every cell of the sheet carries the name in a review's and an agent's outline (a name of
53
+ // 100,000 characters made an outline of 2,000 cells take 1.8 s on the server thread).
54
+ export const MAX_SHEET_NAME = 255;
55
+ export const MAX_BOOK_ELEMENTS = 3_200_000;
56
+ export const MAX_BOOK_ATTRIBUTES = 6_400_000;
57
+ export const MAX_BOOK_TEXT = 32_000_000;
58
+ // Characters of XML one reading of a workbook may walk (other files: MAX_READ_CHARS, 32 million). A plain workbook of 999,000 cells with
59
+ // half of them short texts is about 48 to 62 million; two sheets of one 31-million-character attribute each (the worst shape) held /health 0.43 s.
60
+ export const MAX_BOOK_CHARS = 64_000_000;
61
+ // Characters of text the cells take from the shared strings, counted per cell as the cell is read. A shared string's text is counted once
62
+ // in MAX_BOOK_TEXT, but every cell that points at it is a cell of that text for the readers after this one (a review's outline hashes and
63
+ // cleans each cell's text): 249,000 cells pointing at one string of 20,000 characters (a 517 KB file within every other cap) held the
64
+ // server 4 to 6 s per review, delivery and done. At this cap a reading's cells hold at most 32 million characters from shared strings
65
+ // (plus at most MAX_BOOK_TEXT of their own). Measured per review or outline on the server thread, at a load average of 16: 249,000 cells
66
+ // of 128 characters about 170 ms; 4 cells of 7.9 million characters (the worst shape: each unit's text is cleaned whole) about 175 ms;
67
+ // 8 such cells (64 million) 350 ms.
68
+ export const MAX_SHARED_CELL_TEXT = 32_000_000;
69
+ const bookBudget = () => Object.assign(readBudget({ elements: MAX_BOOK_ELEMENTS, attrs: MAX_BOOK_ATTRIBUTES, text: MAX_BOOK_TEXT, chars: MAX_BOOK_CHARS, what: "workbook", split: "smaller workbooks" }), { cells: 0, merges: 0, sharedText: MAX_SHARED_CELL_TEXT });
24
70
  const KEEP_ROW_CELLS = 64;
71
+ const SAME_TEXT_WORK = 4_000_000;
25
72
 
26
73
  const relTarget = (base, target) => {
27
74
  if (target.startsWith("/")) return target.slice(1);
@@ -33,7 +80,7 @@ const relTarget = (base, target) => {
33
80
  return parts.join("/");
34
81
  };
35
82
 
36
- export function readRels(zip, part) {
83
+ export function readRels(zip, part, budget = null) {
37
84
  const dir = part.split("/").slice(0, -1).join("/");
38
85
  const rels = new Map();
39
86
  const xml = zip.readText(`${dir ? `${dir}/` : ""}_rels/${part.split("/").pop()}.rels`);
@@ -42,7 +89,7 @@ export function readRels(zip, part) {
42
89
  onOpen(name, a) {
43
90
  if (local(name) === "Relationship" && a.Id && a.Target) rels.set(a.Id, relTarget(part, a.Target));
44
91
  },
45
- });
92
+ }, budget);
46
93
  return rels;
47
94
  }
48
95
  export { attrOf, local };
@@ -52,14 +99,18 @@ export function parseWorkbook(bytes) {
52
99
  const zip = openZip(bytes);
53
100
  const xml = zip.readText("xl/workbook.xml");
54
101
  if (xml == null) throw new Error("xl/workbook.xml missing");
55
- const rels = readRels(zip, "xl/workbook.xml");
102
+ const budget = bookBudget(); // the workbook part, its relationships, shared strings, styles and every sheet read share it
103
+ const rels = readRels(zip, "xl/workbook.xml", budget);
56
104
  const sheets = [];
57
105
  const names = [];
58
106
  let curName = null;
59
107
  walkXml(xml, {
60
108
  onOpen(n, a) {
61
109
  const l = local(n);
62
- if (l === "sheet") sheets.push({ index: sheets.length, name: a.name ?? "", sheetId: a.sheetId ?? null, hidden: a.state === "hidden" || a.state === "veryHidden", part: rels.get(attrOf(a, "id")) || null });
110
+ if (l === "sheet" && sheets.length >= MAX_SHEETS) throw tooMany(budget, MAX_SHEETS, "sheets");
111
+ if (l === "definedName" && names.length >= MAX_NAMES) throw tooMany(budget, MAX_NAMES, "defined names");
112
+ if (l === "sheet" && String(a.name ?? "").length > MAX_SHEET_NAME) throw new CapError(`a sheet in this workbook has a name longer than ${MAX_SHEET_NAME} characters, more than Onionskin can read. Rename it in Excel, which allows 31, and open it again`, "sheet name");
113
+ if (l === "sheet") sheets.push({ index: sheets.length, name: a.name ?? "", sheetId: a.sheetId ?? null, hidden: a.state === "hidden" || a.state === "veryHidden", state: a.state ?? "visible", part: rels.get(attrOf(a, "id")) || null });
63
114
  else if (l === "definedName") curName = { name: a.name, ref: "" };
64
115
  },
65
116
  onText(t) {
@@ -71,11 +122,25 @@ export function parseWorkbook(bytes) {
71
122
  curName = null;
72
123
  }
73
124
  },
74
- });
75
- let sst = null;
76
- const sharedStrings = () => {
77
- if (sst) return sst;
78
- sst = [];
125
+ }, budget);
126
+ // the shared strings and the number formats are read on first use, once; a reading that failed (past a cap) fails the same way
127
+ // every time (a half-read list kept after the first failure was accepted on the next call)
128
+ const once = (read) => {
129
+ let value = null;
130
+ let failed = null;
131
+ return (...args) => {
132
+ if (failed) throw failed;
133
+ if (value) return value;
134
+ try {
135
+ return (value = read(...args));
136
+ } catch (err) {
137
+ failed = err;
138
+ throw err;
139
+ }
140
+ };
141
+ };
142
+ const sharedStrings = once(() => {
143
+ const sst = [];
79
144
  const sx = zip.readText("xl/sharedStrings.xml");
80
145
  if (sx == null) return sst;
81
146
  let cur = null;
@@ -96,38 +161,40 @@ export function parseWorkbook(bytes) {
96
161
  if (l === "t") inT = false;
97
162
  else if (l === "rPh") phon = Math.max(0, phon - 1);
98
163
  else if (l === "si") {
164
+ if (sst.length >= MAX_SHARED_STRINGS) throw tooMany(budget, MAX_SHARED_STRINGS, "shared strings");
99
165
  sst.push(cur ?? "");
100
166
  cur = null;
101
167
  }
102
168
  },
103
- });
169
+ }, budget);
104
170
  return sst;
105
- };
171
+ });
106
172
  const cache = new Map();
107
173
  const sheetCells = (s) => {
108
- if (!cache.has(s.index)) cache.set(s.index, readSheet(zip, s, sharedStrings));
174
+ if (!cache.has(s.index)) cache.set(s.index, readSheet(zip, s, sharedStrings, budget));
109
175
  return cache.get(s.index);
110
176
  };
111
177
  // number formats: the style index of a cell -> its format code (xl/styles.xml cellXfs + numFmts, with the built-in ids)
112
- let fmts = null;
178
+ const formats = once(() => {
179
+ const fmts = { custom: new Map(), xf: [] };
180
+ const sx = zip.readText("xl/styles.xml");
181
+ let inXfs = false;
182
+ if (sx != null)
183
+ walkXml(sx, {
184
+ onOpen(n, a) {
185
+ const l = local(n);
186
+ if (l === "numFmt" && a.numFmtId != null) fmts.custom.set(Number(a.numFmtId), a.formatCode ?? "");
187
+ else if (l === "cellXfs") inXfs = true;
188
+ else if (l === "xf" && inXfs) fmts.xf.push(Number(a.numFmtId) || 0);
189
+ },
190
+ onClose(n) {
191
+ if (local(n) === "cellXfs") inXfs = false;
192
+ },
193
+ }, budget);
194
+ return fmts;
195
+ });
113
196
  const numberFormat = (styleIdx) => {
114
- if (!fmts) {
115
- fmts = { custom: new Map(), xf: [] };
116
- const sx = zip.readText("xl/styles.xml");
117
- let inXfs = false;
118
- if (sx != null)
119
- walkXml(sx, {
120
- onOpen(n, a) {
121
- const l = local(n);
122
- if (l === "numFmt" && a.numFmtId != null) fmts.custom.set(Number(a.numFmtId), a.formatCode ?? "");
123
- else if (l === "cellXfs") inXfs = true;
124
- else if (l === "xf" && inXfs) fmts.xf.push(Number(a.numFmtId) || 0);
125
- },
126
- onClose(n) {
127
- if (local(n) === "cellXfs") inXfs = false;
128
- },
129
- });
130
- }
197
+ const fmts = formats();
131
198
  const id = fmts.xf[styleIdx] ?? 0;
132
199
  return fmts.custom.get(id) ?? BUILTIN_FORMATS[id] ?? (id ? `format ${id}` : "General");
133
200
  };
@@ -139,7 +206,7 @@ const BUILTIN_FORMATS = { 0: "General", 1: "0", 2: "0.00", 3: "#,##0", 4: "#,##0
139
206
  const key = (r, c) => r * 16384 + c;
140
207
 
141
208
  /** {cells: Map(r*16384+c -> {r,c,text}), rows: Map(r -> cells sorted by column), maxR, maxC, truncated, merges: [{r0,c0,r1,c1}]}. Empty cells are not kept. */
142
- function readSheet(zip, sheet, sharedStrings) {
209
+ function readSheet(zip, sheet, sharedStrings, budget = bookBudget()) {
143
210
  const cells = new Map();
144
211
  const rows = new Map();
145
212
  const out = { cells, rows, maxR: -1, maxC: -1, truncated: false, merges: [] };
@@ -149,17 +216,39 @@ function readSheet(zip, sheet, sharedStrings) {
149
216
  let field = null; // v | f | is
150
217
  let inIs = false;
151
218
  let phon = 0;
219
+ let rowIdx = -1;
220
+ let colIdx = 0;
152
221
  walkXml(xml, {
153
222
  onOpen(n, a) {
154
223
  const l = local(n);
155
224
  if (l === "mergeCell") {
225
+ if (++budget.merges > MAX_MERGES) throw tooMany(budget, MAX_MERGES, "merged ranges");
156
226
  const q = parseA1(a.ref ?? "");
157
227
  if (q && (q.r1 > q.r0 || q.c1 > q.c0)) out.merges.push(q);
158
- } else if (l === "c" && a.r) {
159
- const m = /^([A-Z]{1,3})(\d+)$/.exec(a.r);
160
- cur = m ? { c: [...m[1]].reduce((x, ch) => x * 26 + ch.charCodeAt(0) - 64, 0) - 1, r: Number(m[2]) - 1, t: a.t || "n", s: Number(a.s) || 0, v: "", f: null, is: "" } : null;
228
+ } else if (l === "row") {
229
+ rowIdx = a.r && /^\d+$/.test(a.r) ? Number(a.r) - 1 : rowIdx + 1;
230
+ colIdx = 0;
231
+ } else if (l === "c" && ++budget.cells > MAX_CELLS) throw tooMany(budget, MAX_CELLS, "cells");
232
+ else if (l === "c") {
233
+ let r = -1;
234
+ let c = -1;
235
+ if (a.r) {
236
+ const m = /^([A-Z]{1,3})(\d+)$/.exec(a.r);
237
+ if (m) {
238
+ c = [...m[1]].reduce((x, ch) => x * 26 + ch.charCodeAt(0) - 64, 0) - 1;
239
+ r = Number(m[2]) - 1;
240
+ colIdx = c + 1;
241
+ }
242
+ } else {
243
+ r = rowIdx;
244
+ c = colIdx++;
245
+ }
246
+ cur = r >= 0 && r < 1_048_576 ? { c, r, t: a.t || "n", s: Number(a.s) || 0, v: "", hasV: false, f: null, is: "" } : null;
161
247
  } else if (!cur) return;
162
- else if (l === "v") field = "v";
248
+ else if (l === "v") {
249
+ field = "v";
250
+ cur.hasV = true;
251
+ }
163
252
  else if (l === "f") {
164
253
  field = "f";
165
254
  cur.f = "";
@@ -176,28 +265,36 @@ function readSheet(zip, sheet, sharedStrings) {
176
265
  else if (l === "is") inIs = false;
177
266
  else if (l === "rPh") phon = Math.max(0, phon - 1);
178
267
  else if (l === "c" && cur) {
179
- if (cells.size >= MAX_CELLS) out.truncated = true;
180
- else {
181
- let text;
182
- if (cur.f) text = `=${cur.f}`;
183
- else if (cur.t === "s") text = sharedStrings()[Number(cur.v)] ?? "";
184
- else if (cur.t === "inlineStr") text = cur.is;
185
- else if (cur.t === "b") text = cur.v === "1" ? "TRUE" : "FALSE";
186
- else if (cur.t === "n" && NUM.test(cur.v)) text = String(Number(cur.v));
187
- else text = cur.v;
188
- if (text !== "" && cur.c < 16384) {
189
- const cell = { r: cur.r, c: cur.c, text, k: cur.f ? "formula" : cur.t === "s" || cur.t === "inlineStr" || cur.t === "str" ? "text" : cur.t === "b" ? "boolean" : cur.t === "e" ? "error" : "number", s: cur.s, ...(cur.f ? { cached: cur.v } : cur.f === "" ? { shared: true } : {}) }; // `cached`: the value a formula last computed (History shows it next to the formula)
268
+ let text;
269
+ if (cur.f) text = `=${cur.f}`;
270
+ else if (cur.t === "s") {
271
+ text = sharedStrings()[Number(cur.v)] ?? "";
272
+ if ((budget.sharedText -= text.length) < 0) throw tooMany(budget, MAX_SHARED_CELL_TEXT, "characters of text in its cells");
273
+ }
274
+ else if (cur.t === "inlineStr") text = cur.is;
275
+ else if (cur.t === "b") text = !cur.hasV ? "" : cur.v === "1" || cur.v === "true" ? "TRUE" : "FALSE";
276
+ else if (cur.t === "n" && NUM.test(cur.v)) text = String(Number(cur.v));
277
+ else text = cur.v;
278
+ if (text !== "" && cur.c < 16384) {
279
+ const cell = { r: cur.r, c: cur.c, text, k: cur.f ? "formula" : cur.t === "s" || cur.t === "inlineStr" || cur.t === "str" ? "text" : cur.t === "b" ? "boolean" : cur.t === "e" ? "error" : "number", s: cur.s, ...(cur.f ? { cached: cur.v } : cur.f === "" ? { shared: true } : {}) }; // `cached`: the value a formula last computed (History shows it next to the formula)
280
+ const prev = cells.get(key(cur.r, cur.c));
281
+ if (prev) {
282
+ // a cell written twice: the later one counts, and its row lists it once
283
+ for (const k of Object.keys(prev)) delete prev[k];
284
+ Object.assign(prev, cell);
285
+ }
286
+ else {
190
287
  cells.set(key(cur.r, cur.c), cell);
191
288
  if (!rows.has(cur.r)) rows.set(cur.r, []);
192
289
  rows.get(cur.r).push(cell);
193
- if (cur.r > out.maxR) out.maxR = cur.r;
194
- if (cur.c > out.maxC) out.maxC = cur.c;
195
290
  }
291
+ if (cur.r > out.maxR) out.maxR = cur.r;
292
+ if (cur.c > out.maxC) out.maxC = cur.c;
196
293
  }
197
294
  cur = null;
198
295
  }
199
296
  },
200
- });
297
+ }, budget);
201
298
  for (const list of rows.values()) list.sort((a, b) => a.c - b.c);
202
299
  return out;
203
300
  }
@@ -207,6 +304,21 @@ function readSheet(zip, sheet, sharedStrings) {
207
304
  export const a1 = (r, c) => `${colLetters(c)}${r + 1}`;
208
305
  export const a1Range = (q) => (q.r0 === q.r1 && q.c0 === q.c1 ? a1(q.r0, q.c0) : `${a1(q.r0, q.c0)}:${a1(q.r1, q.c1)}`);
209
306
  const at = (sd, r, c) => (r < 0 || c < 0 ? "" : sd.cells.get(key(r, c))?.text ?? "");
307
+ /** the hash of the text at (r, c) (EMPTY for no cell), paid as chPaid pays it; null past the budget */
308
+ const hashAt = (sd, r, c) => {
309
+ const cell = r < 0 || c < 0 ? undefined : sd.cells.get(key(r, c));
310
+ return cell ? chPaid(cell) : EMPTY;
311
+ };
312
+ /** The parsed cell at row r, column c (0-based) of a sheet's cells (sheetCells), or undefined. */
313
+ export const cellAt = (sd, r, c) => sd.cells.get(key(r, c));
314
+ /** Merged ranges of a sheet by their top-left cell, built once per sheet: a lookup per cell, never a scan of every range. */
315
+ export function mergeAt(sd, r, c) {
316
+ if (!sd.mergeIndex) {
317
+ sd.mergeIndex = new Map();
318
+ for (const q of sd.merges) if (!sd.mergeIndex.has(key(q.r0, q.c0))) sd.mergeIndex.set(key(q.r0, q.c0), q); // the first, as a scan finds it
319
+ }
320
+ return sd.mergeIndex.get(key(r, c));
321
+ }
210
322
 
211
323
  /** the text of a rectangle as `apply` expects it: cells joined by TAB, rows by LF (bounded) */
212
324
  export function rangeText(sd, q) {
@@ -219,6 +331,96 @@ export function rangeText(sd, q) {
219
331
  return rows.join("\n");
220
332
  }
221
333
 
334
+ // What an anchor keeps of a range's text: the batch quotes at most 500 characters of it and shows at most 20 x 10 of its cells, 200
335
+ // characters each (src/batch.js). An anchor kept the whole text: 50 anchors on ranges of a cell of 31 million characters held 50 copies
336
+ // of it (the server ran out of memory), and each was saved with the session and cleaned again for the batch.
337
+ const CELL_KEEP = 1000;
338
+ const TEXT_KEEP = 4000;
339
+ const VALUES = { rows: 20, cols: 10 };
340
+ const cut = (s) => (s.length > CELL_KEEP ? s.slice(0, CELL_KEEP) : s);
341
+ const lastRow = (q) => Math.min(q.r1, q.r0 + 2000);
342
+ const lastCol = (q) => Math.min(q.c1, q.c0 + 200);
343
+
344
+ /**
345
+ * A range as an anchor keeps it, per range on the sheet's reading: `text` (rangeText with each cell cut at CELL_KEEP characters, at most
346
+ * TEXT_KEEP in all), `values` (its top-left VALUES.rows x VALUES.cols cells, cut the same way) and `hash`, the hash of the whole rangeText
347
+ * (what `apply` checks an `expect.text_hash` against). Paid before the work: WALK_CELL units a cell to walk it, then HASH_CELL a cell and
348
+ * weigh() of every cell's text for the hash (measured on 2,000 x 201 cells of "a": the walk 50 to 65 ns a cell, the hash 125; charged
349
+ * at 3 and 1 units a cell, 500 comments on such ranges held a Send 3.1 s). Priced as a search is (src/text.js HIT, about 6 ns a unit):
350
+ * a budget of range work is about 0.25 s. Past it an anchor goes out without its range hash (the agent is offered full ops for it).
351
+ * Null when the walk cannot be paid; `hash` null when the hash cannot (asked again by the next request, on a fresh budget).
352
+ * The hash is fed in chunks of whole cells, not a call per cell: 1,000 x 20 cells of ordinary values (about 10 characters) took 125 to
353
+ * 140 ns a cell fed one by one and 25 to 35 ns in chunks (on "a": 100 and 25), so HASH_CELL is 5 and the text is paid by its weight.
354
+ * Ordinary ranges cost about 25 units a cell: 50 of 1,000 x 20 cells take 25 million units (40.25 million at 20 a cell, and the 50th
355
+ * went out without its hash). Ceiling: cells averaging past about 25 ASCII characters put 50 such ranges over the budget again.
356
+ * NFC of the whole is the NFC of each cell, since a tab or a line break never composes or reorders with what is next to it (src/nfc.js
357
+ * runs of marks end at either too), so a chunk ends after any cell. 50 distinct ranges on one large cell cost 50 hashes of
358
+ * it, which the budget pays for or refuses; the cell's own hash (one-cell ranges, the top-left identity) is kept on the cell.
359
+ */
360
+ const WALK_CELL = 10;
361
+ const HASH_CELL = 5;
362
+ const CHUNK = 1 << 16;
363
+ function rangeOf(sd, q) {
364
+ const k = `${q.r0},${q.c0},${q.r1},${q.c1}`;
365
+ const memo = (sd.ranges ||= new Map()); // Ceiling: one entry per range an anchor names, so at most the anchors of the reading
366
+ let v = memo.get(k);
367
+ const one = q.r0 === q.r1 && q.c0 === q.c1;
368
+ if (!v) {
369
+ const r1 = lastRow(q);
370
+ const c1 = lastCol(q);
371
+ if (!spend(WALK_CELL * (r1 - q.r0 + 1) * (c1 - q.c0 + 1))) return null;
372
+ let text = "";
373
+ let cost = 0;
374
+ const values = [];
375
+ for (let r = q.r0; r <= r1; r++) {
376
+ const row = r - q.r0 < VALUES.rows ? [] : null;
377
+ for (let c = q.c0; c <= c1; c++) {
378
+ const cell = sd.cells.get(key(r, c));
379
+ const t = cell ? cell.text : "";
380
+ if (cell) cost += costOf(cell);
381
+ cost += HASH_CELL;
382
+ if (row && c - q.c0 < VALUES.cols) row.push(cut(t));
383
+ if (text.length < TEXT_KEEP) text += (c > q.c0 ? "\t" : r > q.r0 ? "\n" : "") + cut(t);
384
+ }
385
+ if (row) values.push(row);
386
+ }
387
+ memo.set(k, (v = { text: text.length > TEXT_KEEP ? text.slice(0, TEXT_KEEP) : text, values, hash: null, cost }));
388
+ }
389
+ if (v.hash === null) v.hash = one ? hashAt(sd, q.r0, q.c0) : spend(v.cost) ? streamHash(sd, q) : null;
390
+ return v;
391
+ }
392
+ /** exactHash(rangeText(sd, q)), fed in chunks that end after a whole cell */
393
+ function streamHash(sd, q) {
394
+ const h = crypto.createHash("sha256");
395
+ let buf = "";
396
+ for (let r = q.r0; r <= lastRow(q); r++) {
397
+ if (r > q.r0) buf += "\n";
398
+ for (let c = q.c0; c <= lastCol(q); c++) {
399
+ if (c > q.c0) buf += "\t";
400
+ buf += at(sd, r, c);
401
+ if (buf.length >= CHUNK) (h.update(nfc(buf)), (buf = ""));
402
+ }
403
+ }
404
+ h.update(nfc(buf));
405
+ return h.digest("hex").slice(0, 16);
406
+ }
407
+
408
+ // A number format as an anchor keeps it: its first FORMAT_KEEP characters (the batch shows 100), kept per style of a reading. An outside
409
+ // review: a custom format of 1,000 decomposed letters on every cell of A1:J20 was kept whole per cell of each of 500 anchors (323 MB).
410
+ // numberFormat() itself stays whole: the merge compares formats.
411
+ const FORMAT_KEEP = 100;
412
+ const FORMATS = new WeakMap();
413
+ function formatOf(wb, s) {
414
+ let m = FORMATS.get(wb);
415
+ if (!m) FORMATS.set(wb, (m = new Map()));
416
+ let v = m.get(s);
417
+ if (v === undefined) {
418
+ v = wb.numberFormat(s);
419
+ if (v.length > FORMAT_KEEP) v = v.slice(0, FORMAT_KEEP);
420
+ m.set(s, v);
421
+ }
422
+ return v;
423
+ }
222
424
  /** Per cell of a range (at most 20 x 10, like the evidence `values`): its kind (number, text, formula, boolean, error, empty) and number format. */
223
425
  function cellMeta(wb, sd, q) {
224
426
  const types = [];
@@ -229,7 +431,7 @@ function cellMeta(wb, sd, q) {
229
431
  for (let c = q.c0; c <= Math.min(q.c1, q.c0 + 9); c++) {
230
432
  const cell = sd.cells.get(key(r, c));
231
433
  t.push(cell ? cell.k : "empty");
232
- f.push(cell ? wb.numberFormat(cell.s) : "General");
434
+ f.push(cell ? formatOf(wb, cell.s) : "General");
233
435
  }
234
436
  types.push(t);
235
437
  formats.push(f);
@@ -239,29 +441,37 @@ function cellMeta(wb, sd, q) {
239
441
 
240
442
  /** The nearest non-empty cell above `r` in column `c`: the column header (the topmost one, which is the header row of a table). */
241
443
  function colHeaderAt(sd, r, c) {
242
- for (let i = 0; i < r; i++) {
243
- const t = at(sd, i, c);
244
- if (t !== "") return short(t);
444
+ // the first cell of each column (only cells with text are kept), found once per sheet: a scan up the column per call was quadratic
445
+ if (!sd.colFirst) {
446
+ sd.colFirst = new Map();
447
+ for (const cell of sd.cells.values()) {
448
+ const f = sd.colFirst.get(cell.c);
449
+ if (!f || cell.r < f.r) sd.colFirst.set(cell.c, cell);
450
+ }
245
451
  }
246
- return "";
452
+ const f = sd.colFirst.get(c);
453
+ return f && f.r < r ? sh(f) : "";
247
454
  }
248
455
 
249
456
  /** What surrounds a range: neighbors, the row's other cells (hashes), the row's leftmost cell, the column's header. */
250
457
  function contextOf(sd, q) {
251
458
  const rowCells = [];
252
- let rowHeader = "";
253
- for (const cell of sd.rows.get(q.r0) || []) {
459
+ let rowHeader = null;
460
+ const row = sd.rows.get(q.r0) || [];
461
+ for (const cell of spend(row.length) ? row : []) { // a row read from the request's budget (src/text.js scanBudget); past it the row's context is not kept
254
462
  if (cell.c >= q.c0 && cell.c <= q.c1) continue;
255
- if (!rowHeader && cell.c < q.c0) rowHeader = cell.text;
256
- if (rowCells.length < KEEP_ROW_CELLS) rowCells.push(ch(cell));
463
+ if (!rowHeader && cell.c < q.c0 && cell.text) rowHeader = cell;
464
+ if (rowCells.length >= KEEP_ROW_CELLS) continue;
465
+ const h = chPaid(cell); // past the budget the cell is left out of what the row is remembered by
466
+ if (h !== null) rowCells.push(h);
257
467
  }
258
468
  return {
259
- rowHeader: short(rowHeader),
469
+ rowHeader: sh(rowHeader),
260
470
  colHeader: colHeaderAt(sd, q.r0, q.c0),
261
- left: short(at(sd, q.r0, q.c0 - 1)),
262
- right: short(at(sd, q.r0, q.c1 + 1)),
263
- up: short(at(sd, q.r0 - 1, q.c0)),
264
- down: short(at(sd, q.r1 + 1, q.c0)),
471
+ left: sh(cellAt(sd, q.r0, q.c0 - 1)),
472
+ right: sh(cellAt(sd, q.r0, q.c1 + 1)),
473
+ up: sh(cellAt(sd, q.r0 - 1, q.c0)),
474
+ down: sh(cellAt(sd, q.r1 + 1, q.c0)),
265
475
  rowCells: rowCells.sort(),
266
476
  };
267
477
  }
@@ -272,7 +482,8 @@ function rowOverlap(sd, ctx, r, c0, c1) {
272
482
  const have = new Map();
273
483
  for (const cell of sd.rows.get(r) || []) {
274
484
  if (cell.c >= c0 && cell.c <= c1) continue;
275
- const h = ch(cell);
485
+ const h = chPaid(cell);
486
+ if (h === null) continue;
276
487
  have.set(h, (have.get(h) || 0) + 1);
277
488
  }
278
489
  let hit = 0;
@@ -286,12 +497,20 @@ function rowOverlap(sd, ctx, r, c0, c1) {
286
497
  }
287
498
  const hasRowInfo = (ctx) => ctx.rowCells.length > 0 || !!ctx.rowHeader;
288
499
  const firstOutside = (sd, r, c0, c1) => (sd.rows.get(r) || []).find((x) => x.c < c0 || x.c > c1);
289
- /** row `r` looks like the remembered row: most of its other cells are there, or its leftmost cell is the same */
290
- const sameRow = (sd, ctx, r, c0, c1) => rowOverlap(sd, ctx, r, c0, c1) >= 0.6 || (!!ctx.rowHeader && short(firstOutside(sd, r, c0, c1)?.text) === ctx.rowHeader);
500
+ /**
501
+ * row `r` looks like the remembered row: most of its other cells are there, or its leftmost cell is the same. Reading the row is spent
502
+ * from the request's budget (src/text.js scanBudget), at a cell a unit and again for the leftmost one; past it the row does not look
503
+ * like it (so the anchor resolves as "moved or removed"). `charge` false when the caller spent for its rows already.
504
+ */
505
+ const sameRow = (sd, ctx, r, c0, c1, charge = true) => {
506
+ if (charge && !spend(2 * (sd.rows.get(r)?.length || 0) + 1)) return false;
507
+ return rowOverlap(sd, ctx, r, c0, c1) >= 0.6 || (!!ctx.rowHeader && sh(firstOutside(sd, r, c0, c1)) === ctx.rowHeader);
508
+ };
291
509
 
292
510
  function neighborScore(sd, ctx, r, c, d) {
293
511
  const eq = (a, b) => (a !== "" && a === b ? 0.5 : 0);
294
- return eq(ctx.left, short(at(sd, r, c - 1))) + eq(ctx.right, short(at(sd, r, c + (d.c1 - d.c0) + 1))) + eq(ctx.up, short(at(sd, r - 1, c))) + eq(ctx.down, short(at(sd, r + (d.r1 - d.r0) + 1, c)));
512
+ const near = (rr, cc) => (rr < 0 || cc < 0 ? "" : sh(sd.cells.get(key(rr, cc))));
513
+ return eq(ctx.left, near(r, c - 1)) + eq(ctx.right, near(r, c + (d.c1 - d.c0) + 1)) + eq(ctx.up, near(r - 1, c)) + eq(ctx.down, near(r + (d.r1 - d.r0) + 1, c));
295
514
  }
296
515
 
297
516
  // ---- anchors --------------------------------------------------------------------------------------------------
@@ -301,6 +520,33 @@ const bookRef = (ref) => {
301
520
  if (!m) return null;
302
521
  return { sheet: (m[1] ?? m[2]).replace(/''/g, "'"), a1: m[5] ? `${m[3]}${m[4]}:${m[5]}${m[6]}` : `${m[3]}${m[4]}` };
303
522
  };
523
+ // the defined names by the cell they point at and by name, read once per workbook (an anchor looked up every name, with a regex each)
524
+ function nameIndex(wb) {
525
+ if (wb.nameIndex) return wb.nameIndex;
526
+ const at = new Map();
527
+ const byName = new Map();
528
+ for (const n of wb.names) {
529
+ if (!byName.has(n.name)) byName.set(n.name, n);
530
+ const b = bookRef(n.ref);
531
+ if (!b) continue;
532
+ const k = `${b.sheet}\u0000${b.a1}`;
533
+ (at.get(k) || at.set(k, []).get(k)).push(n.name);
534
+ }
535
+ return (wb.nameIndex = { at, byName });
536
+ }
537
+ // What an anchor keeps of the names that point at its range: resolving reads them in order and takes the first that still checks out, so
538
+ // a few are enough. At most NAMES_KEEP of at most 255 characters (Excel's own limit), each paid from the budget by its length (20,000
539
+ // names of 250 characters on A1: every comment on A1 kept all of them, 5 MB each, 400 MB of session file for 80 comments).
540
+ const NAMES_KEEP = 8;
541
+ const namesAt = (wb, sheetName, full) => {
542
+ const out = [];
543
+ for (const nm of nameIndex(wb).at.get(`${sheetName}\u0000${full}`) || []) {
544
+ if (out.length >= NAMES_KEEP) break;
545
+ if (nm.length <= 255 && spend(nm.length)) out.push(nm);
546
+ }
547
+ return out;
548
+ };
549
+ const nameDef = (wb, nm) => nameIndex(wb).byName.get(nm);
304
550
  const pickSheet = (wb, { sheetName, sheetIndex }) => wb.sheets.find((s) => s.name === sheetName) || wb.sheets[sheetIndex] || null;
305
551
 
306
552
  /**
@@ -314,18 +560,23 @@ export function xlsxDurable(wb, loc) {
314
560
  const q = parseA1(loc.range || "");
315
561
  if (!q) return { ...base, cell: false, text: "", textHash: null, ctx: null, names: [] };
316
562
  const sd = wb.sheetCells(sheet);
317
- const text = rangeText(sd, q);
563
+ // Within the request's budget, like resolving (src/text.js scanBudget): the identity first, so a range too large to hash whole still
564
+ // knows its top-left cell. Past the budget the anchor still goes out, weaker: the address and what context was paid for, with a null
565
+ // hash (the batch offers the agent full `ops` for it, never a `set` checked against a hash it does not have).
566
+ const textHash = hashAt(sd, q.r0, q.c0); // of the top-left cell: the identity
567
+ const got = rangeOf(sd, q);
318
568
  const full = a1Range(q);
319
569
  return {
320
570
  ...base,
321
571
  cell: true,
322
572
  r0: q.r0, c0: q.c0, r1: q.r1, c1: q.c1,
323
- text, // typed text of the whole range
324
- textHash: exactHash(at(sd, q.r0, q.c0)), // of the top-left cell: the identity
325
- rangeHash: exactHash(text),
573
+ text: got ? got.text : "", // typed text of the range, as much as the batch can show (TEXT_KEEP)
574
+ values: got ? got.values : [],
575
+ textHash,
576
+ rangeHash: got ? got.hash : null,
326
577
  ctx: contextOf(sd, q),
327
578
  ...cellMeta(wb, sd, q),
328
- names: wb.names.filter((n) => { const b = bookRef(n.ref); return b && b.sheet === sheet.name && b.a1 === full; }).map((n) => n.name),
579
+ names: namesAt(wb, sheet.name, full),
329
580
  };
330
581
  }
331
582
 
@@ -368,43 +619,73 @@ function resolveIn(wb, s, d) {
368
619
  const hh = d.r1 - d.r0;
369
620
  const shape = (r, c) => ({ r0: r, c0: c, r1: r + hh, c1: c + w });
370
621
  const res = (method, q) => {
371
- const text = rangeText(sd, q);
372
- return { method, sheetId: s.sheetId, sheetName: s.name, sheetIndex: s.index, range: a1Range(q), changed: exactHash(text) !== d.rangeHash, moved: q.r0 !== d.r0 || q.c0 !== d.c0, text: text.slice(0, 200), ...q };
622
+ // rangeOf reads up to 2001 x 201 cells: paid before it is read, nothing of it once the budget is out. An anchor made without its
623
+ // range hash (past the budget at Send), or a range whose hash this request did not pay for, compares the text both kept (its first
624
+ // TEXT_KEEP characters): a difference there is a change. Ceiling: an edit past that text reads as unchanged; ordinary ranges keep
625
+ // their hash (rangeOf). An anchor stored by an earlier version (no `values`) kept its whole text, and one whose walk was not paid
626
+ // kept none (empty `values`): neither is compared.
627
+ const got = rangeOf(sd, q);
628
+ const text = got ? got.text : "";
629
+ const changed = d.rangeHash != null && got?.hash != null ? got.hash !== d.rangeHash : !!got && d.values?.length > 0 && got.text !== d.text;
630
+ return { method, sheetId: s.sheetId, sheetName: s.name, sheetIndex: s.index, range: a1Range(q), changed, moved: q.r0 !== d.r0 || q.c0 !== d.c0, text: text.slice(0, 200), ...q };
373
631
  };
374
632
  const noCtx = !hasRowInfo(ctx) && !ctx.left && !ctx.right && !ctx.up && !ctx.down;
375
- const rowOk = (r, c) => sameRow(sd, ctx, r, c, c + w);
633
+ const rowOk = (r, c, charged = false) => sameRow(sd, ctx, r, c, c + w, !charged); // charged: the caller spent for these rows
376
634
  const colOk = (r, c) => !ctx.colHeader || colHeaderAt(sd, r, c) === ctx.colHeader;
377
635
  const gate = (r, c) => (hasRowInfo(ctx) ? rowOk(r, c) : noCtx || neighborScore(sd, ctx, r, c, d) >= 1);
636
+ // an anchor made without the top-left hash (past the budget at Send) is found by its context only (steps 3 to 5); with no context
637
+ // either, by its address, as a whole-row or whole-sheet anchor is. hashAt is null past the budget: never equal to a missing hash.
638
+ const known = d.textHash != null;
378
639
  // 1. same address, same text, same row
379
- if (exactHash(at(sd, d.r0, d.c0)) === d.textHash && gate(d.r0, d.c0)) return res("position", shape(d.r0, d.c0));
640
+ if (known && hashAt(sd, d.r0, d.c0) === d.textHash && gate(d.r0, d.c0)) return res("position", shape(d.r0, d.c0));
380
641
  // 2. the same text somewhere else; the context picks between equal values
381
- if (d.textHash !== EMPTY) {
642
+ if (known && d.textHash !== EMPTY && spend(sd.cells.size * 8)) {
643
+ // the scan of every cell runs within the budget of the anchors resolved together (src/text.js scanBudget)
382
644
  const hits = [];
383
- for (const cell of sd.cells.values()) if (ch(cell) === d.textHash) hits.push(cell);
645
+ for (const cell of sd.cells.values()) if (chPaid(cell) === d.textHash) hits.push(cell);
646
+ // Ceiling: scoring a hit scans its row, so when the hits' rows hold more than SAME_TEXT_WORK cells in all (a sheet of one value
647
+ // repeated in long rows), they are not scored one by one; the steps below place the anchor by its row and column instead.
648
+ // Upgrade path: score each row once per anchor.
649
+ // The rows the hits sit in are spent from the request's budget too (the ceiling above is per anchor; 500 anchors of it were not bounded).
650
+ let work = 0;
651
+ for (const x of hits) work += sd.rows.get(x.r)?.length || 0;
652
+ if (work > SAME_TEXT_WORK || !spend(2 * work + hits.length)) hits.length = 0;
384
653
  // one other cell with the same text only counts when the row or column header agree with it (or the anchor never had any): an edited
385
654
  // cell must not be stolen by an unrelated cell that happens to hold the old text; the in-place step below gets its chance first
386
- const fits = (x) => (hasRowInfo(ctx) ? rowOk(x.r, x.c) || (!!ctx.colHeader && colHeaderAt(sd, x.r, x.c) === ctx.colHeader) : !ctx.colHeader || colHeaderAt(sd, x.r, x.c) === ctx.colHeader);
655
+ const fits = (x) => (hasRowInfo(ctx) ? rowOk(x.r, x.c, true) || (!!ctx.colHeader && colHeaderAt(sd, x.r, x.c) === ctx.colHeader) : !ctx.colHeader || colHeaderAt(sd, x.r, x.c) === ctx.colHeader);
387
656
  if (hits.length === 1 && fits(hits[0])) return res("value", shape(hits[0].r, hits[0].c));
388
657
  if (hits.length > 1) {
389
- const scored = hits
390
- .map((x) => ({ x, score: (rowOk(x.r, x.c) ? 2 : 0) + (ctx.colHeader && colHeaderAt(sd, x.r, x.c) === ctx.colHeader ? 1.5 : 0) + neighborScore(sd, ctx, x.r, x.c, d) }))
391
- .sort((a, b) => b.score - a.score);
392
- if (scored[0].score > 0 && scored[0].score > scored[1].score) return res("value", shape(scored[0].x.r, scored[0].x.c));
658
+ // the best and the runner-up in one pass (a sort of every hit is not needed for two)
659
+ let best = null;
660
+ let second = -Infinity;
661
+ for (const x of hits) {
662
+ const score = (rowOk(x.r, x.c, true) ? 2 : 0) + (ctx.colHeader && colHeaderAt(sd, x.r, x.c) === ctx.colHeader ? 1.5 : 0) + neighborScore(sd, ctx, x.r, x.c, d);
663
+ if (!best || score > best.score) (second = best ? best.score : -Infinity), (best = { x, score });
664
+ else if (score > second) second = score;
665
+ }
666
+ if (best.score > 0 && best.score > second) return res("value", shape(best.x.r, best.x.c));
393
667
  }
394
668
  }
395
669
  // 3. edited in place: the text differs but the row and the column header still say it is the same cell
396
- if (hasRowInfo(ctx) ? rowOk(d.r0, d.c0) && colOk(d.r0, d.c0) : !noCtx && neighborScore(sd, ctx, d.r0, d.c0, d) >= 1) return res("position", shape(d.r0, d.c0));
670
+ // (an anchor without its hash: one remembered neighbor still there is enough, as it has nothing else to go by)
671
+ if (hasRowInfo(ctx) ? rowOk(d.r0, d.c0) && colOk(d.r0, d.c0) : (!known && noCtx) || (!noCtx && neighborScore(sd, ctx, d.r0, d.c0, d) >= (known ? 1 : 0.5))) return res("position", shape(d.r0, d.c0));
397
672
  // 4. the text changed and the cell moved: the row by its other cells, the column by its header
398
- if (hasRowInfo(ctx)) {
399
- const rowsHit = [];
673
+ if (hasRowInfo(ctx) && spend(sd.cells.size * 8)) {
674
+ let best = null;
675
+ let second = null;
676
+ let count = 0;
400
677
  for (const r of sd.rows.keys()) {
401
678
  const ov = rowOverlap(sd, ctx, r, -1, -1);
402
- const hdr = !!ctx.rowHeader && short(sd.rows.get(r)[0]?.text) === ctx.rowHeader;
403
- if (ov >= 0.6 || hdr) rowsHit.push({ r, score: ov + (hdr ? 0.5 : 0) });
679
+ const hdr = !!ctx.rowHeader && sh(sd.rows.get(r)[0]) === ctx.rowHeader;
680
+ if (ov >= 0.6 || hdr) {
681
+ const hit = { r, score: ov + (hdr ? 0.5 : 0) };
682
+ count++;
683
+ if (!best || hit.score > best.score) (second = best), (best = hit);
684
+ else if (!second || hit.score > second.score) second = hit;
685
+ }
404
686
  }
405
- rowsHit.sort((a, b) => b.score - a.score);
406
- if (rowsHit.length && (rowsHit.length === 1 || rowsHit[0].score > rowsHit[1].score)) {
407
- const row = rowsHit[0].r;
687
+ if (best && (count === 1 || best.score > second.score) && spend(sd.maxC + 1)) {
688
+ const row = best.r;
408
689
  const cols = [];
409
690
  if (ctx.colHeader) {
410
691
  for (let c = 0; c <= sd.maxC; c++) if (colHeaderAt(sd, row, c) === ctx.colHeader) cols.push(c);
@@ -414,11 +695,11 @@ function resolveIn(wb, s, d) {
414
695
  }
415
696
  // 5. a defined name that pointed at this range; last, because a library that does not shift names leaves them stale
416
697
  for (const nm of d.names || []) {
417
- const def = wb.names.find((n) => n.name === nm);
698
+ const def = nameDef(wb, nm);
418
699
  const b = def ? bookRef(def.ref) : null;
419
700
  const q = b && b.sheet === s.name ? parseA1(b.a1) : null;
420
701
  // a name that moved was updated by the writer (trusted); one that still points at the old address is stale unless the cell checks out
421
- const verified = q && (exactHash(at(sd, q.r0, q.c0)) === d.textHash || (hasRowInfo(ctx) ? sameRow(sd, ctx, q.r0, q.c0, q.c0 + w) : neighborScore(sd, ctx, q.r0, q.c0, d) >= 1));
702
+ const verified = q && ((known && hashAt(sd, q.r0, q.c0) === d.textHash) || (hasRowInfo(ctx) ? sameRow(sd, ctx, q.r0, q.c0, q.c0 + w) : neighborScore(sd, ctx, q.r0, q.c0, d) >= 1));
422
703
  if (q && (q.r0 !== d.r0 || q.c0 !== d.c0 || verified)) return res("name", q);
423
704
  }
424
705
  return { method: "none" };
@@ -426,7 +707,7 @@ function resolveIn(wb, s, d) {
426
707
 
427
708
  /** The sheet is gone (renamed and renumbered): the cell by its text and context in every sheet; only one clear winner counts. */
428
709
  function contentSearch(wb, d) {
429
- if (!d.cell || d.textHash === EMPTY) return { method: "none" };
710
+ if (!d.cell || d.textHash === EMPTY || d.textHash == null) return { method: "none" };
430
711
  const found = [];
431
712
  for (const s of wb.sheets) {
432
713
  const r = resolveIn(wb, s, d);