openwriter 0.13.0 → 0.15.0

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.
@@ -0,0 +1,616 @@
1
+ /**
2
+ * Matcher — given an original node graph (with IDs) and a new doc body,
3
+ * produce an updated node graph where surviving blocks keep their IDs.
4
+ *
5
+ * Core principle: the slot is innocent until proven changed. Every mutation
6
+ * rule is a deterministic detector for a specific kind of slot change. If
7
+ * no rule fires for an orphan + unmatched pair, the slot-continuity fallback
8
+ * pairs them by structural position (same type, between same pinned anchors).
9
+ *
10
+ * Rule order:
11
+ * Phase 1: exact fingerprint match, two-pass (mutual-unique + slot-aware)
12
+ * Phase 2 rules (each math-only, deterministic):
13
+ * - N-way split detection (sentence-array concatenation)
14
+ * - N-way merge detection (sentence-array concatenation, reversed)
15
+ * - Type-change detection (TipTap convention: content survives, type changes)
16
+ * - Edit detection (shared sentence tuples, slot-region constrained)
17
+ * - Slot-continuity fallback (same type, same pinned-anchor neighborhood)
18
+ * - Graveyard restore (paste-back / undo of recently deleted blocks)
19
+ * - Insert (any block still unmatched → fresh ID)
20
+ * Phase 3: orphans = previousNodes entries no rule claimed (= deletes)
21
+ *
22
+ * Fingerprints use math signals (per-sentence char count, 3-char prefix/suffix,
23
+ * terminator, word-length sequence) plus full word arrays for math-collision
24
+ * disambiguation. Documented in node-fingerprint.ts.
25
+ *
26
+ * adr: adr/node-identity-matcher.md
27
+ */
28
+ import { generateNodeId } from './helpers.js';
29
+ import { fingerprintAll, isExactMatch, isSameContent, sentenceArraysEqual, sentenceTuplesEqual, } from './node-fingerprint.js';
30
+ /**
31
+ * Run the matcher.
32
+ *
33
+ * @param previousNodes - frontmatter `nodes` map from the prior save
34
+ * @param newBlocks - block list of the new doc body
35
+ * @param options.graveyard - optional array of recently-deleted entries.
36
+ * Lets paste-back/undo restore the original ID.
37
+ */
38
+ export function matchNodes(previousNodes, newBlocks, options = {}) {
39
+ const graveyard = options.graveyard || [];
40
+ const newFingerprints = fingerprintAll(newBlocks);
41
+ const pinned = [];
42
+ const claimedPrevIds = new Set();
43
+ const claimedGraveIds = new Set();
44
+ const unmatched = newBlocks.map((block, i) => ({
45
+ position: newFingerprints[i].position,
46
+ fingerprint: newFingerprints[i],
47
+ block,
48
+ }));
49
+ pinExactMatches(unmatched, previousNodes, claimedPrevIds, pinned);
50
+ applySplitRule(unmatched, previousNodes, claimedPrevIds, pinned);
51
+ applyMergeRule(unmatched, previousNodes, claimedPrevIds, pinned);
52
+ applyTypeChangeRule(unmatched, previousNodes, claimedPrevIds, pinned);
53
+ applyEditRule(unmatched, previousNodes, claimedPrevIds, pinned);
54
+ applySlotContinuityRule(unmatched, previousNodes, claimedPrevIds, pinned, graveyard);
55
+ applyGraveyardRestoreRule(unmatched, graveyard, claimedGraveIds, pinned);
56
+ applyInsertRule(unmatched, pinned, previousNodes, claimedPrevIds, graveyard, claimedGraveIds);
57
+ const orphaned = previousNodes
58
+ .filter((prev) => !claimedPrevIds.has(prev.id))
59
+ .map((prev) => ({ id: prev.id, fingerprint: prev.fingerprint }));
60
+ const remainingGraveyard = graveyard.filter((g) => !claimedGraveIds.has(g.id));
61
+ return {
62
+ pinned,
63
+ unmatched,
64
+ orphaned,
65
+ graveyardRestored: pinned.filter((p) => p.mutation === 'graveyard-restore'),
66
+ nextGraveyard: [...orphaned, ...remainingGraveyard],
67
+ summary: {
68
+ totalBlocks: newBlocks.length,
69
+ pinnedCount: pinned.length,
70
+ unmatchedCount: unmatched.length,
71
+ orphanedCount: orphaned.length,
72
+ coverage: newBlocks.length > 0 ? pinned.length / newBlocks.length : 1,
73
+ },
74
+ };
75
+ }
76
+ /** Thread state between sequential matcher runs. */
77
+ export function rebuildPreviousFromResult(result) {
78
+ return result.pinned.map((p) => ({ id: p.id, fingerprint: p.fingerprint }));
79
+ }
80
+ /** Build a previousNodes map from a block list (bootstrap on first save). */
81
+ export function bootstrapPreviousNodes(originalBlocks) {
82
+ const fps = fingerprintAll(originalBlocks);
83
+ return originalBlocks.map((_block, i) => ({
84
+ id: generateNodeId(),
85
+ fingerprint: fps[i],
86
+ }));
87
+ }
88
+ // ----------------------------------------------------------------------
89
+ // Phase 1 — exact-match pinning, two-pass
90
+ // ----------------------------------------------------------------------
91
+ function pinExactMatches(unmatched, previousNodes, claimedPrevIds, pinned) {
92
+ // -------- PASS A: mutual-unique pairs --------
93
+ let changedA = true;
94
+ while (changedA) {
95
+ changedA = false;
96
+ const prevToCands = new Map();
97
+ const candToPrevs = new Map();
98
+ for (const prev of previousNodes) {
99
+ if (claimedPrevIds.has(prev.id))
100
+ continue;
101
+ const cands = unmatched.filter((u) => isExactMatch(prev.fingerprint, u.fingerprint));
102
+ prevToCands.set(prev.id, cands);
103
+ for (const c of cands) {
104
+ if (!candToPrevs.has(c.position))
105
+ candToPrevs.set(c.position, []);
106
+ candToPrevs.get(c.position).push(prev);
107
+ }
108
+ }
109
+ for (const [prevId, cands] of prevToCands) {
110
+ if (claimedPrevIds.has(prevId))
111
+ continue;
112
+ if (cands.length !== 1)
113
+ continue;
114
+ const cand = cands[0];
115
+ const prevs = candToPrevs.get(cand.position);
116
+ if (!prevs || prevs.length !== 1)
117
+ continue;
118
+ if (!unmatched.includes(cand))
119
+ continue;
120
+ const prev = previousNodes.find((p) => p.id === prevId);
121
+ const origPos = prev.fingerprint.position;
122
+ claimedPrevIds.add(prevId);
123
+ pinned.push({
124
+ id: prevId,
125
+ position: cand.position,
126
+ fingerprint: cand.fingerprint,
127
+ block: cand.block,
128
+ mutation: origPos !== cand.position ? 'moved' : 'unchanged',
129
+ });
130
+ const idx = unmatched.indexOf(cand);
131
+ unmatched.splice(idx, 1);
132
+ changedA = true;
133
+ }
134
+ }
135
+ // -------- PASS B: slot-aware position-distance --------
136
+ let changedB = true;
137
+ while (changedB) {
138
+ changedB = false;
139
+ for (const prev of previousNodes) {
140
+ if (claimedPrevIds.has(prev.id))
141
+ continue;
142
+ const candidates = unmatched.filter((u) => isExactMatch(prev.fingerprint, u.fingerprint));
143
+ if (candidates.length === 0)
144
+ continue;
145
+ const prevIdx = previousNodes.findIndex((p) => p.id === prev.id);
146
+ const lo = slotLowBound(previousNodes, claimedPrevIds, pinned, prevIdx);
147
+ const hi = slotHighBound(previousNodes, claimedPrevIds, pinned, prevIdx);
148
+ let inRange;
149
+ if (lo + 1 < hi) {
150
+ inRange = candidates.filter((c) => c.position > lo && c.position < hi);
151
+ }
152
+ else if (lo > hi) {
153
+ inRange = candidates; // inverted: anchors swapped, full pos-distance
154
+ }
155
+ else {
156
+ inRange = []; // empty slot
157
+ }
158
+ if (inRange.length === 0)
159
+ continue;
160
+ const origPos = prev.fingerprint.position;
161
+ let best = inRange[0];
162
+ let bestDist = Math.abs(best.position - origPos);
163
+ for (const c of inRange) {
164
+ const d = Math.abs(c.position - origPos);
165
+ if (d < bestDist) {
166
+ best = c;
167
+ bestDist = d;
168
+ }
169
+ }
170
+ claimedPrevIds.add(prev.id);
171
+ pinned.push({
172
+ id: prev.id,
173
+ position: best.position,
174
+ fingerprint: best.fingerprint,
175
+ block: best.block,
176
+ mutation: origPos !== best.position ? 'moved' : 'unchanged',
177
+ });
178
+ const idx = unmatched.indexOf(best);
179
+ unmatched.splice(idx, 1);
180
+ changedB = true;
181
+ }
182
+ }
183
+ }
184
+ // ----------------------------------------------------------------------
185
+ // Split rule — N-way
186
+ // ----------------------------------------------------------------------
187
+ function applySplitRule(unmatched, previousNodes, claimedPrevIds, pinned) {
188
+ let progress = true;
189
+ while (progress) {
190
+ progress = false;
191
+ for (const orphan of previousNodes) {
192
+ if (claimedPrevIds.has(orphan.id))
193
+ continue;
194
+ const orphanSents = orphan.fingerprint.sentences;
195
+ if (!orphanSents || orphanSents.length < 2)
196
+ continue;
197
+ let claimed = false;
198
+ for (let startIdx = 0; startIdx < unmatched.length && !claimed; startIdx++) {
199
+ const start = unmatched[startIdx];
200
+ if (start.fingerprint.type !== orphan.fingerprint.type)
201
+ continue;
202
+ const group = [startIdx];
203
+ let concatLen = start.fingerprint.sentences.length;
204
+ for (let next = startIdx + 1; next < unmatched.length; next++) {
205
+ const prev = unmatched[next - 1];
206
+ const cur = unmatched[next];
207
+ if (cur.position !== prev.position + 1)
208
+ break;
209
+ if (cur.fingerprint.type !== orphan.fingerprint.type)
210
+ break;
211
+ group.push(next);
212
+ concatLen += cur.fingerprint.sentences.length;
213
+ if (concatLen > orphanSents.length)
214
+ break;
215
+ if (concatLen < orphanSents.length)
216
+ continue;
217
+ const concat = [];
218
+ for (const gi of group)
219
+ concat.push(...unmatched[gi].fingerprint.sentences);
220
+ if (!sentenceArraysEqual(concat, orphanSents))
221
+ break;
222
+ claimedPrevIds.add(orphan.id);
223
+ for (let i = 0; i < group.length; i++) {
224
+ const c = unmatched[group[i]];
225
+ pinned.push({
226
+ id: i === 0 ? orphan.id : generateNodeId(),
227
+ position: c.position,
228
+ fingerprint: c.fingerprint,
229
+ block: c.block,
230
+ mutation: i === 0 ? 'split-first' : `split-${i + 1}`,
231
+ });
232
+ }
233
+ for (let i = group.length - 1; i >= 0; i--)
234
+ unmatched.splice(group[i], 1);
235
+ claimed = true;
236
+ progress = true;
237
+ break;
238
+ }
239
+ }
240
+ }
241
+ }
242
+ }
243
+ // ----------------------------------------------------------------------
244
+ // Merge rule — N-way
245
+ // ----------------------------------------------------------------------
246
+ function applyMergeRule(unmatched, previousNodes, claimedPrevIds, pinned) {
247
+ let progress = true;
248
+ while (progress) {
249
+ progress = false;
250
+ for (let ui = 0; ui < unmatched.length; ui++) {
251
+ const candidate = unmatched[ui];
252
+ const candidateSents = candidate.fingerprint.sentences;
253
+ if (!candidateSents || candidateSents.length < 2)
254
+ continue;
255
+ let merged = false;
256
+ for (let startOrphIdx = 0; startOrphIdx < previousNodes.length && !merged; startOrphIdx++) {
257
+ const start = previousNodes[startOrphIdx];
258
+ if (claimedPrevIds.has(start.id))
259
+ continue;
260
+ if (start.fingerprint.type !== candidate.fingerprint.type)
261
+ continue;
262
+ const group = [startOrphIdx];
263
+ let concatLen = start.fingerprint.sentences.length;
264
+ for (let next = startOrphIdx + 1; next < previousNodes.length; next++) {
265
+ const prev = previousNodes[next - 1];
266
+ const cur = previousNodes[next];
267
+ if (claimedPrevIds.has(cur.id))
268
+ break;
269
+ if (cur.fingerprint.type !== candidate.fingerprint.type)
270
+ break;
271
+ if (cur.fingerprint.position !== prev.fingerprint.position + 1)
272
+ break;
273
+ group.push(next);
274
+ concatLen += cur.fingerprint.sentences.length;
275
+ if (concatLen > candidateSents.length)
276
+ break;
277
+ if (concatLen < candidateSents.length)
278
+ continue;
279
+ const concat = [];
280
+ for (const gi of group)
281
+ concat.push(...previousNodes[gi].fingerprint.sentences);
282
+ if (!sentenceArraysEqual(concat, candidateSents))
283
+ break;
284
+ for (const gi of group)
285
+ claimedPrevIds.add(previousNodes[gi].id);
286
+ pinned.push({
287
+ id: previousNodes[group[0]].id,
288
+ position: candidate.position,
289
+ fingerprint: candidate.fingerprint,
290
+ block: candidate.block,
291
+ mutation: `merge-${group.length}-way`,
292
+ });
293
+ unmatched.splice(ui, 1);
294
+ merged = true;
295
+ progress = true;
296
+ ui--;
297
+ break;
298
+ }
299
+ }
300
+ if (merged)
301
+ break;
302
+ }
303
+ }
304
+ }
305
+ // ----------------------------------------------------------------------
306
+ // Type-change rule (TipTap convention)
307
+ // ----------------------------------------------------------------------
308
+ function applyTypeChangeRule(unmatched, previousNodes, claimedPrevIds, pinned) {
309
+ let progress = true;
310
+ while (progress) {
311
+ progress = false;
312
+ for (let ui = 0; ui < unmatched.length; ui++) {
313
+ const candidate = unmatched[ui];
314
+ const candidateOrphans = previousNodes.filter((p) => {
315
+ if (claimedPrevIds.has(p.id))
316
+ return false;
317
+ if (p.fingerprint.type === candidate.fingerprint.type)
318
+ return false;
319
+ if (!isSameContent(p.fingerprint, candidate.fingerprint))
320
+ return false;
321
+ const orphanIdx = previousNodes.findIndex((x) => x.id === p.id);
322
+ const lo = slotLowBound(previousNodes, claimedPrevIds, pinned, orphanIdx);
323
+ const hi = slotHighBound(previousNodes, claimedPrevIds, pinned, orphanIdx);
324
+ return candidate.position > lo && candidate.position < hi;
325
+ });
326
+ if (candidateOrphans.length === 0)
327
+ continue;
328
+ const origPos = candidate.position;
329
+ let best = candidateOrphans[0];
330
+ let bestDist = Math.abs(best.fingerprint.position - origPos);
331
+ for (const o of candidateOrphans) {
332
+ const d = Math.abs(o.fingerprint.position - origPos);
333
+ if (d < bestDist) {
334
+ best = o;
335
+ bestDist = d;
336
+ }
337
+ }
338
+ claimedPrevIds.add(best.id);
339
+ pinned.push({
340
+ id: best.id,
341
+ position: candidate.position,
342
+ fingerprint: candidate.fingerprint,
343
+ block: candidate.block,
344
+ mutation: `type-change-${best.fingerprint.type}-to-${candidate.fingerprint.type}`,
345
+ });
346
+ unmatched.splice(ui, 1);
347
+ progress = true;
348
+ ui--;
349
+ }
350
+ }
351
+ }
352
+ // ----------------------------------------------------------------------
353
+ // Edit rule — content drifted, but at least one sentence tuple still matches
354
+ // ----------------------------------------------------------------------
355
+ function applyEditRule(unmatched, previousNodes, claimedPrevIds, pinned) {
356
+ const unmatchedByPos = [...unmatched].sort((a, b) => a.position - b.position);
357
+ for (const candidate of unmatchedByPos) {
358
+ if (!unmatched.includes(candidate))
359
+ continue;
360
+ const candidateOrphans = previousNodes.filter((p) => {
361
+ if (claimedPrevIds.has(p.id))
362
+ return false;
363
+ if (p.fingerprint.type !== candidate.fingerprint.type)
364
+ return false;
365
+ if (!shareAnySentenceTuple(p.fingerprint.sentences, candidate.fingerprint.sentences))
366
+ return false;
367
+ const orphanIdx = previousNodes.findIndex((x) => x.id === p.id);
368
+ const lo = slotLowBound(previousNodes, claimedPrevIds, pinned, orphanIdx);
369
+ const hi = slotHighBound(previousNodes, claimedPrevIds, pinned, orphanIdx);
370
+ return candidate.position > lo && candidate.position < hi;
371
+ });
372
+ if (candidateOrphans.length !== 1)
373
+ continue;
374
+ const orphan = candidateOrphans[0];
375
+ claimedPrevIds.add(orphan.id);
376
+ pinned.push({
377
+ id: orphan.id,
378
+ position: candidate.position,
379
+ fingerprint: candidate.fingerprint,
380
+ block: candidate.block,
381
+ mutation: 'edited',
382
+ });
383
+ const idx = unmatched.indexOf(candidate);
384
+ unmatched.splice(idx, 1);
385
+ }
386
+ }
387
+ // ----------------------------------------------------------------------
388
+ // Slot-continuity fallback
389
+ // ----------------------------------------------------------------------
390
+ function applySlotContinuityRule(unmatched, previousNodes, claimedPrevIds, pinned, graveyard) {
391
+ let progress = true;
392
+ while (progress) {
393
+ progress = false;
394
+ for (let ui = 0; ui < unmatched.length; ui++) {
395
+ const candidate = unmatched[ui];
396
+ // Skip candidates that carry an explicit ID already known to the
397
+ // identity graph (in previousNodes or graveyard). Such IDs are real
398
+ // signals — the load-time matcher's previous pin, a restored snapshot,
399
+ // or a paste-back from graveyard. Don't override them with positional
400
+ // guessing; let applyInsertRule preserve them (for previousNodes hits)
401
+ // or applyGraveyardRestoreRule restore them (for graveyard hits).
402
+ //
403
+ // Transient IDs (not in either set — e.g. a fresh ID typed in the
404
+ // editor by a user replacing a block in place) still go through
405
+ // slot-continuity per the "slot is innocent" principle.
406
+ //
407
+ // adr: adr/node-identity-matcher.md
408
+ if (candidate.block.id) {
409
+ const id = candidate.block.id;
410
+ const inPrev = previousNodes.some((p) => p.id === id);
411
+ const inGrave = graveyard.some((g) => g.id === id);
412
+ if (inPrev || inGrave)
413
+ continue;
414
+ }
415
+ const matchingOrphans = previousNodes.filter((orphan) => {
416
+ if (claimedPrevIds.has(orphan.id))
417
+ return false;
418
+ if (orphan.fingerprint.type !== candidate.fingerprint.type)
419
+ return false;
420
+ const orphanIdx = previousNodes.findIndex((x) => x.id === orphan.id);
421
+ const prevAnchor = findPinnedNeighbor(previousNodes, claimedPrevIds, orphanIdx, -1);
422
+ const nextAnchor = findPinnedNeighbor(previousNodes, claimedPrevIds, orphanIdx, +1);
423
+ const prevNewPos = prevAnchor ? findPinnedPosition(pinned, prevAnchor.id) : -1;
424
+ const nextNewPos = nextAnchor ? findPinnedPosition(pinned, nextAnchor.id) : Infinity;
425
+ return candidate.position > prevNewPos && candidate.position < nextNewPos;
426
+ });
427
+ if (matchingOrphans.length === 0)
428
+ continue;
429
+ const scored = matchingOrphans.map((orphan) => ({
430
+ orphan,
431
+ score: sentenceSignalOverlapScore(orphan.fingerprint, candidate.fingerprint),
432
+ }));
433
+ scored.sort((a, b) => b.score - a.score);
434
+ const topScore = scored[0].score;
435
+ const tied = scored.filter((s) => s.score === topScore);
436
+ if (tied.length > 1 && topScore === 0)
437
+ continue;
438
+ let best = tied[0].orphan;
439
+ let bestDist = Math.abs(best.fingerprint.position - candidate.position);
440
+ for (const t of tied) {
441
+ const d = Math.abs(t.orphan.fingerprint.position - candidate.position);
442
+ if (d < bestDist) {
443
+ best = t.orphan;
444
+ bestDist = d;
445
+ }
446
+ }
447
+ claimedPrevIds.add(best.id);
448
+ pinned.push({
449
+ id: best.id,
450
+ position: candidate.position,
451
+ fingerprint: candidate.fingerprint,
452
+ block: candidate.block,
453
+ mutation: 'slot-preserved',
454
+ });
455
+ unmatched.splice(ui, 1);
456
+ ui--;
457
+ progress = true;
458
+ }
459
+ }
460
+ }
461
+ /**
462
+ * Lightweight content overlap signal used by slot-continuity scoring.
463
+ * Per sentence-pair: +1 f, +1 l, +1 t, +2 wls-equal, +3×shared-words,
464
+ * +10 full word-array equality. Word-level overlap is the disambiguator
465
+ * when math signals collide.
466
+ */
467
+ function sentenceSignalOverlapScore(a, b) {
468
+ if (!a.sentences || !b.sentences)
469
+ return 0;
470
+ let score = 0;
471
+ for (const sa of a.sentences) {
472
+ for (const sb of b.sentences) {
473
+ if (sa.f === sb.f)
474
+ score++;
475
+ if (sa.l === sb.l)
476
+ score++;
477
+ if (sa.t === sb.t)
478
+ score++;
479
+ if (arraysEqual(sa.wls, sb.wls))
480
+ score += 2;
481
+ if (Array.isArray(sa.w) && Array.isArray(sb.w)) {
482
+ const aSet = new Set(sa.w);
483
+ let shared = 0;
484
+ for (const w of sb.w)
485
+ if (aSet.has(w))
486
+ shared++;
487
+ score += shared * 3;
488
+ if (arraysEqual(sa.w, sb.w))
489
+ score += 10;
490
+ }
491
+ }
492
+ }
493
+ return score;
494
+ }
495
+ // ----------------------------------------------------------------------
496
+ // Graveyard restore
497
+ // ----------------------------------------------------------------------
498
+ function applyGraveyardRestoreRule(unmatched, graveyard, claimedGraveIds, pinned) {
499
+ for (let ui = unmatched.length - 1; ui >= 0; ui--) {
500
+ const candidate = unmatched[ui];
501
+ const ghostMatches = graveyard.filter((g) => !claimedGraveIds.has(g.id) && isExactMatch(g.fingerprint, candidate.fingerprint));
502
+ if (ghostMatches.length === 0)
503
+ continue;
504
+ const candPos = candidate.position;
505
+ let best = ghostMatches[0];
506
+ let bestDist = Math.abs(best.fingerprint.position - candPos);
507
+ for (const g of ghostMatches) {
508
+ const d = Math.abs(g.fingerprint.position - candPos);
509
+ if (d < bestDist) {
510
+ best = g;
511
+ bestDist = d;
512
+ }
513
+ }
514
+ claimedGraveIds.add(best.id);
515
+ pinned.push({
516
+ id: best.id,
517
+ position: candidate.position,
518
+ fingerprint: candidate.fingerprint,
519
+ block: candidate.block,
520
+ mutation: 'graveyard-restore',
521
+ });
522
+ unmatched.splice(ui, 1);
523
+ }
524
+ }
525
+ // ----------------------------------------------------------------------
526
+ // Insert rule (last resort)
527
+ // ----------------------------------------------------------------------
528
+ function applyInsertRule(unmatched, pinned, previousNodes, claimedPrevIds, graveyard, claimedGraveIds) {
529
+ for (let i = unmatched.length - 1; i >= 0; i--) {
530
+ const candidate = unmatched[i];
531
+ // Preserve an existing block ID if the TipTap node already carried one
532
+ // (e.g. agent-assigned via applyChangesToDocument, or freshly minted by
533
+ // the doc-update path). Minting a new ID here would diverge the server
534
+ // copy from the browser copy — the browser still has the original ID,
535
+ // so subsequent updates targeting the new ID can't be resolved and the
536
+ // browser's autosave later overwrites server state with stale content.
537
+ //
538
+ // If the preserved ID also lives in previousNodes or the graveyard, we
539
+ // must claim it from those sets so the same ID doesn't simultaneously
540
+ // appear in pinned AND in orphaned/remaining-graveyard. Without claiming,
541
+ // bb000001 would end up listed in both `nodes:` and `graveyard:` in the
542
+ // output frontmatter — the matcher's identity invariant says an ID lives
543
+ // in exactly one place.
544
+ //
545
+ // If the preserved ID is already claimed (another block earlier in this
546
+ // pass took it, e.g. an exact-match), fall back to a fresh ID to keep IDs
547
+ // globally unique.
548
+ //
549
+ // adr: adr/node-identity-matcher.md
550
+ let id;
551
+ const preservedId = candidate.block.id;
552
+ if (preservedId &&
553
+ !claimedPrevIds.has(preservedId) &&
554
+ !claimedGraveIds.has(preservedId)) {
555
+ id = preservedId;
556
+ if (previousNodes.some((p) => p.id === preservedId))
557
+ claimedPrevIds.add(preservedId);
558
+ if (graveyard.some((g) => g.id === preservedId))
559
+ claimedGraveIds.add(preservedId);
560
+ }
561
+ else {
562
+ id = generateNodeId();
563
+ }
564
+ pinned.push({
565
+ id,
566
+ position: candidate.position,
567
+ fingerprint: candidate.fingerprint,
568
+ block: candidate.block,
569
+ mutation: 'inserted',
570
+ });
571
+ unmatched.splice(i, 1);
572
+ }
573
+ }
574
+ // ----------------------------------------------------------------------
575
+ // Helpers
576
+ // ----------------------------------------------------------------------
577
+ function findPinnedNeighbor(previousNodes, claimedPrevIds, startIdx, direction) {
578
+ for (let i = startIdx + direction; i >= 0 && i < previousNodes.length; i += direction) {
579
+ if (claimedPrevIds.has(previousNodes[i].id))
580
+ return previousNodes[i];
581
+ }
582
+ return null;
583
+ }
584
+ function findPinnedPosition(pinned, id) {
585
+ const entry = pinned.find((p) => p.id === id);
586
+ return entry ? entry.position : -1;
587
+ }
588
+ function slotLowBound(previousNodes, claimedPrevIds, pinned, orphanIdx) {
589
+ const prev = findPinnedNeighbor(previousNodes, claimedPrevIds, orphanIdx, -1);
590
+ return prev ? findPinnedPosition(pinned, prev.id) : -1;
591
+ }
592
+ function slotHighBound(previousNodes, claimedPrevIds, pinned, orphanIdx) {
593
+ const next = findPinnedNeighbor(previousNodes, claimedPrevIds, orphanIdx, +1);
594
+ return next ? findPinnedPosition(pinned, next.id) : Infinity;
595
+ }
596
+ function shareAnySentenceTuple(a, b) {
597
+ if (!Array.isArray(a) || !Array.isArray(b))
598
+ return false;
599
+ for (const sa of a) {
600
+ for (const sb of b) {
601
+ if (sentenceTuplesEqual(sa, sb))
602
+ return true;
603
+ }
604
+ }
605
+ return false;
606
+ }
607
+ function arraysEqual(a, b) {
608
+ if (!Array.isArray(a) || !Array.isArray(b))
609
+ return false;
610
+ if (a.length !== b.length)
611
+ return false;
612
+ for (let i = 0; i < a.length; i++)
613
+ if (a[i] !== b[i])
614
+ return false;
615
+ return true;
616
+ }