openwriter 0.14.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.
@@ -51,9 +51,9 @@ export function matchNodes(previousNodes, newBlocks, options = {}) {
51
51
  applyMergeRule(unmatched, previousNodes, claimedPrevIds, pinned);
52
52
  applyTypeChangeRule(unmatched, previousNodes, claimedPrevIds, pinned);
53
53
  applyEditRule(unmatched, previousNodes, claimedPrevIds, pinned);
54
- applySlotContinuityRule(unmatched, previousNodes, claimedPrevIds, pinned);
54
+ applySlotContinuityRule(unmatched, previousNodes, claimedPrevIds, pinned, graveyard);
55
55
  applyGraveyardRestoreRule(unmatched, graveyard, claimedGraveIds, pinned);
56
- applyInsertRule(unmatched, pinned);
56
+ applyInsertRule(unmatched, pinned, previousNodes, claimedPrevIds, graveyard, claimedGraveIds);
57
57
  const orphaned = previousNodes
58
58
  .filter((prev) => !claimedPrevIds.has(prev.id))
59
59
  .map((prev) => ({ id: prev.id, fingerprint: prev.fingerprint }));
@@ -387,12 +387,31 @@ function applyEditRule(unmatched, previousNodes, claimedPrevIds, pinned) {
387
387
  // ----------------------------------------------------------------------
388
388
  // Slot-continuity fallback
389
389
  // ----------------------------------------------------------------------
390
- function applySlotContinuityRule(unmatched, previousNodes, claimedPrevIds, pinned) {
390
+ function applySlotContinuityRule(unmatched, previousNodes, claimedPrevIds, pinned, graveyard) {
391
391
  let progress = true;
392
392
  while (progress) {
393
393
  progress = false;
394
394
  for (let ui = 0; ui < unmatched.length; ui++) {
395
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
+ }
396
415
  const matchingOrphans = previousNodes.filter((orphan) => {
397
416
  if (claimedPrevIds.has(orphan.id))
398
417
  return false;
@@ -506,11 +525,44 @@ function applyGraveyardRestoreRule(unmatched, graveyard, claimedGraveIds, pinned
506
525
  // ----------------------------------------------------------------------
507
526
  // Insert rule (last resort)
508
527
  // ----------------------------------------------------------------------
509
- function applyInsertRule(unmatched, pinned) {
528
+ function applyInsertRule(unmatched, pinned, previousNodes, claimedPrevIds, graveyard, claimedGraveIds) {
510
529
  for (let i = unmatched.length - 1; i >= 0; i--) {
511
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
+ }
512
564
  pinned.push({
513
- id: generateNodeId(),
565
+ id,
514
566
  position: candidate.position,
515
567
  fingerprint: candidate.fingerprint,
516
568
  block: candidate.block,