Skip to content

Note comparison after a Word save: custody markers that Word keeps, moves or drops (#430) - #452

Open
hadim wants to merge 11 commits into
tensorbee:mainfrom
hadim:fix/430-word-saved-note-custody
Open

hadim wants to merge 11 commits into
tensorbee:mainfrom
hadim:fix/430-word-saved-note-custody

Conversation

@hadim

@hadim hadim commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Part of #430. Stacked on #450.

What F-X287 asks

docs/hld/14-development-backlog.md (F-X287, "Repair note comparison after Word-save normalization"):

Restore successful current-source resolution and unrelated comparison for the authenticated native middle save. Restore mixed-author destination and open-marker lifetime behavior through both author orders, accept/reject choices and reopens, including the successful first-author action that retires markers while another author remains pending. Each failed action must remain atomic relative to its immediate prior state.

Include a different invariant discriminator during design. No guard relaxation, adjacency-based lost-owner inference, skipped reader, API narrowing or success through deleting native markers is approved. Preserve the exact independent Q-A430-28 unproved exception.

This PR does that on reconstructed Word saves, in five code commits. The last one, part C, needs your decision D1, and it can be reverted alone.

Decisions for you

D1, the tracked-note unit (commit 5). A normal footnote that no custody pair binds is resolved as one unit when its own bytes prove that its whole definition is tracked as one insertion or one deletion (paragraph marks included), and its only reference is wrapped the same way.

  • The proof binds the reference to the owner only through w:footnoteReference/@w:id to w:footnote/@w:id. It never reads a marker or an adjacent element, so under Q-A430-29 an unmatched body pair stays unassociated.
  • Why. It is the only way to repair files whose note pair Word has already dropped, which includes the RC3 packets.
  • New successes on ordinary documents. Two cases that refused on v0.17.0 now succeed, outside any comparison:
    • C2: rejecting a hand-built wholly tracked footnote (its paragraph mark tracked) that was never compared, which removes it.
    • C3: rejecting the elements of a wholly tracked footnote whose reference was already removed, which removes the unreferenced note.
  • Cost. A completed body pair that an earlier Word resolution left in a part still votes when a later tracked note settles that part. A stale 01 bit can then make a reject remove a part that held only separators. This extends the limit you accepted under Q-A430-29 to markerless units.
  • If you refuse D1. Revert commit 5 alone and re-record the archive row. Commits 1 to 4 stay intact and green, and new compare results still survive a Word save (commit 1). What comes back, for files whose note pair Word already dropped and for hand-built tracked notes:
    • the refusals when rejecting a tracked note in a mixed Word save of this branch, C2 and C3
    • the first-author marker loss on the mixed shape where Word kept one note pair and dropped the other
    • body custody pair is inside a revision on Accept All and Reject All for the issue's "a footnote added" save whose body pair Word merged and whose note pair it dropped, the RC3 middle symptom, because commit 2 admits a merged pair only with its note pair (run on commit 4, inputs/word/added_para.docx). Commit 5's message omits this one.

D2, partial selection of a tracked note. A partial selection keeps the ordinary element rules: what succeeds today still succeeds, and what refuses at the final paragraph mark still refuses. The alternative is the marked-unit rule, where a partial selection refuses atomically.

C3, removing an unreferenced tracked note. A whole selection of the elements of a tracked note with no reference left removes it. The alternative keeps today's refusal.

Not covered, a choice for you. A footnote that a person inserts in Word 16.115 with Track Changes on is not a tracked note: Word tracks its reference and content but not its paragraph mark.

  • On it, the library's reject_all leaves an empty unreferenced footnote where Word's own Reject All removes the note (tc_base_word_reject.docx). This is the same on v0.17.0, so it is not a regression.
  • The proof could instead be anchored on a tracked w:footnoteRef run rather than on tracked paragraph marks, which would cover that case. This PR does not do it.

Item 5, the moved reference (commit 4). The fix is a refusal, see below, and it narrows comparison. Of eight note drafts checked, two now refuse (a new note inserted before an existing one, and two notes that swap places), and so does a footnoted sentence moved to another paragraph with its id unchanged. All three compared on #450 to correct destinations inside the library, with a pending file holding one note from a deleted and an inserted reference. Word 16.115 could not open the pending result of the first. The other two were not opened in Word, by rule. A pending result with unique ids for these edits would need note ids that differ from one destination, which the exact-identity contract does not allow. Your options: keep the refusal, relax exact identities, or drop commit 4.

The parts

Where the Word observations come from.

  • Microsoft Word for Mac 16.115 (26110837), driven by AppleScript open, save as, close.
  • RC3 used Word 16.114.1, per F-X272a, which was not tested here.
  • The rules below are observations on the issue 430 shapes, not Microsoft documentation.

1. Note pair after the note reference mark (commit 1, D4)

Cause. Word 16.115 drops a zero-width bookmark pair that sits before the w:footnoteRef run of a footnote that is not the first of its part.

  • It did so on every such save: a footnote added to an existing paragraph, one added with a body text change, the issue's "a footnote added" with a new paragraph, both units of a two-author result, the second footnote of a part created with two notes, and a second footnote removed with a text change (dropped again on a second save).
  • Word kept the pair for the only or first note of a part, and in 8 of 8 shapes once the pair was moved after the reference-mark run.

Fix.

  • insert_note_marker places the note pair after the first reference-mark run of the content wrapper: inside the w:ins for an addition, inside the w:del for a deletion.
  • A note without a reference-mark run keeps the old position.
  • Recognition is unchanged.

Re-check. With a build of commits 1 to 3 before the rebase onto #450, Word 16.115 kept every note pair on 9 of 9 shapes, including those that lost it before. An independent check confirmed it on 6 more shapes, against 6 of 6 dropped with the v0.17.0 placement. Word was not opened again after the rebase.

2. Body pair inside its reference's own insertion (commit 2)

Cause. Word 16.115 merges adjacent insertions of one author and date.

  • In the issue's "a footnote added" result, the new paragraph's text insertion and the reference insertion merge around the body pair: <w:ins><w:r>text</w:r><B/><w:r>reference</w:r></w:ins>.
  • note_markers refused every body pair inside a revision, so Accept All, Reject All, per author and an unrelated comparison all refused with body custody pair is inside a revision, the RC3 middle symptom.

Fix, admission. A body pair is also admitted as a direct child of the one w:ins of a main-story paragraph whose next child is the reference run, and only when its note pair is present.

  • This is the proof of the direct position: the next sibling names the reference, and the note pair binds the owner, which must agree.
  • Everything else inside a revision still refuses: an unmatched pair, a pair in a w:del (Word was never observed to put one there), a pair folded into a preceding insertion or placed before another run, a pair in a move destination, and a pair nested deeper.
  • Commit 5 adds one case: an unmatched pair before the reference of a note that part C proves by itself.

Fix, lift-out. An insertion whose content a resolution removes keeps such a pair in its place (RenderState::custody_pairs, emitted in render_selected_revision). When another unit is pending, the pair stays a tombstone and keeps its part-presence vote. Word's own Reject All also keeps it.

3. Leftover pairs retire when a resolution settles the note part (commit 3, D3)

Cause. With a note pair gone, no unit is recognised and settle_footnote_custody returned early. Accepting a Word-saved result left the hidden body bookmark behind, while a library-only result settles with no marker.

Fix. A resolution that edits the note part or resolves a note reference, and leaves no revision in a normal note owner or note reference, now settles the part even when no unit is recognised, and retires the pairs in it.

  • Resolving the reference alone counts, so the pair also retires when the note side settled first (review item L2).
  • A pair in an untouched part stays. While any note revision is pending, every pair stays.

4. A note reference that moves to another site refuses (commit 4)

Cause. Footnote comparison pairs owners by note id. A new note inserted before an existing one (the same on v0.17.0) is an edited input whose new first note takes id 2 while the existing note moves to id 3.

  • The result paired the original note 2 ("Contract, clause 7.") with the edited note 2 ("Delivery note, p.1.") as one text change.
  • It held two references to note 2, one in a w:del (the original site) and one in a w:ins (the new site): the shared-reference shape of Q-A430-28.
  • Accept and reject still gave the edited and original notes, but Word 16.115 could not open the pending file.

Fix. A footnote that both inputs reference refuses atomically, before any note custody is written, when the pending result would give it more physical references than either input does. This is the note contract's "refuse a moved reference". The same edit with the new note after the existing one still compares.

5. Tracked-note unit (commit 5, needs D1)

Cause. With the note pair lost, rejecting an added note or accepting a removed one ran as ordinary revisions. That had to remove the note's only paragraph mark, so it refused with cannot remove the final paragraph mark, the RC3 mixed symptom. With a second author pending, resolving the first author could also retire every custody pair, because only marked units counted as pending.

Fix, the proof (tracked_note_units, tracked_note_owner). A normal owner with no bound pair is a unit when:

  • every paragraph carries a paragraph-mark revision of one kind K
  • every other paragraph child is a K wrapper or a bookmarkStart, bookmarkEnd or proofErr
  • every revision inside it is K
  • it has at most one reference, and that reference sits in a K wrapper.

Fix, resolution.

  • Selecting all its elements resolves it as one, and the owner goes before rendering when the resolution removes its content.
  • A partial selection keeps the ordinary element rules (D2).
  • A whole selection of an unreferenced tracked note removes it (C3).

Fix, lifetime.

  • A tracked note has no token or bits and does not vote, but it is pending for the open lifetime.
  • When a tracked note settles while a note owner outside every unit still holds a revision, the pairs stay too (found in review: a mixed note with a hyperlink). The rule is kept as small as it is: it applies only when a tracked note is selected and a marked unit exists, it reads note owners only, and it leaves the residual below on purpose.
  • FootnoteCustody::token becomes Option<i32>, and reconciliation, projection and retirement use marked units only.

Residual limits, unchanged from v0.17.0.

  • A note outside the tracked-note grammar (for example one holding a hyperlink) keeps the old behaviour once Word drops its pair: rejecting it refuses atomically.
  • When Word kept the other author's pair (a marked unit), resolving that author first still retires the outside note's body pair while its revisions are pending.
  • Closing that would change the tombstone contract, so it is left as it is.

Why no guard is weaker

Guard Before After
Body pair must be a direct child of w:p refuses every pair in a wrapper refuses every pair in a wrapper, except a matched pair (its note pair present) that is, by the same next-sibling proof, in its own reference's w:ins in the main story, and, with commit 5, an unmatched pair before a tracked note's reference. The resolver never drops such a pair
Marked-unit consistency (bits, token, owner, duplicates, note pair without body pair) unchanged unchanged. Tracked notes are built only from owners that no pair binds
Partial custody selection refuses unchanged for marked units. Tracked notes keep the ordinary element rules (D2)
Final paragraph mark refuses removing the last paragraph of a retained owner unchanged for every retained owner. A whole owner goes only under a full tracked-note selection, which also removes its reference when it has one (C3 has none)
Dangling reference refuses unchanged, also checked for tracked notes at settlement
Shared references (Q-A430-28) marked-unit rules, and a moved reference could create one marked-unit rules unchanged. A tracked note has at most one reference, and a comparison that would create a shared pending reference now refuses
Marker retirement at the settlement of the last recognised unit never earlier: a pending tracked note, or a note revision outside every unit while a tracked note settles, keeps the pairs. Commit 3 adds retirement only when a resolution edits the note part or a reference and leaves no note revision pending
Prior-revision policy in comparison unchanged unchanged

Behaviour changes

  • D4, compare output bytes (commit 1). The note custody pair now follows the note's reference-mark run inside its insertion or deletion. For a deletion it was the paragraph boundary outside the w:del. Results written by 0.17.0 keep being recognised.
  • Commit 2. A body pair inside its reference's insertion is admitted when its note pair is present. Rejecting that insertion keeps the pair in place.
  • D3 (commit 3). Accepting a Word-saved result that lost its note pair no longer leaves the hidden _rdocxCmpNoteF...B bookmark. A pair left in a part retires when a resolution edits that part or resolves a note reference and leaves no note revision pending.
  • Commit 4. A comparison in which a referenced footnote's reference moves to another site, including a new note inserted before an existing one, now refuses atomically instead of writing a result Word 16.115 cannot open.
  • Commit 5.
    • A wholly tracked footnote with at most one tracked reference can be rejected (or, when deleted, accepted) as a whole, where the ordinary path refused at the final paragraph mark.
    • This covers compare results whose note pair Word dropped, and hand-built tracked notes (C2). It does not cover a footnote inserted in Word 16.115 with Track Changes on (see above).
    • Without any custody pair, rejecting the last such note of a part keeps a separator-only part, as Word 16.115's own Reject All does, because nothing records that the part was created.
  • Commit 5, C3. A whole selection of an unreferenced tracked note removes it, where it refused.
  • Commit 5, compare entry. A pending tracked note keeps the part open, so a comparison on top of a Word-saved result keeps the earlier author's unmatched body pair instead of dropping it.
  • No public API, signature, listing, count or selector semantics change.

F-X287 items, met and open

  • Resolution and unrelated comparison of the "middle" save. Met on the reconstruction: issue430_word_saved_body_pair_in_its_reference_insertion_resolves_and_compares, issue430_word_saved_lost_note_pair_resolves_as_a_tracked_note.
  • Mixed-author destination and open-marker lifetime, both orders, both choices, reopens, and the first-author action. Met on the reconstruction: issue430_word_saved_mixed_authors_resolve_in_both_orders_and_choices, issue430_word_saved_mixed_note_outside_the_grammar_keeps_both_pairs, issue430_comparison_over_a_word_saved_tracked_note_keeps_both_lifetimes, issue430_body_pair_lifted_out_of_a_rejected_insertion_keeps_its_vote. An independent review ran 1,004 Word-saved sequences, each reaching the library's own destination, on the branch before its last fix pass.
  • Failed actions stay atomic. Every negative test asserts unchanged bytes after the refusal.
  • A different invariant discriminator. issue430_word_saved_created_part_with_a_lost_note_pair_settles_to_its_destination (part presence of a created part) and the lift-out vote test.
  • No guard relaxation, adjacency-based lost-owner inference, skipped reader, API narrowing or success through deleting native markers. See the table. Commit 4 narrows one comparison that used to write an unopenable result.
  • Q-A430-28 preserved. A tracked note has one reference at most, the two-reference negative keeps today's refusal, and a comparison no longer creates a shared pending reference.
  • Open, needs your RC3 packets. Exact source and output hashes, Word 16.114.1, selector outcomes, held handles, inventories and original errors.
  • Open. The 45 A / 14 G matrix was not re-run row by row. The residual limits of part 5 remain.

What needs your RC3 packets

These were built on this machine, not taken from the RC3 packets (.claude/scratch/S96-final-evidence/.../native-middle-rc3-runtime-probe-1, native-mixed-rc3-runtime-probe-1):

  • a Word 16.115 reconstruction of the "middle" save (the issue's "a footnote added" result with the merged body pair)
  • the two-author saves (both note pairs lost, and one kept)
  • the part-creating two-note save.

They reproduce the three recorded symptoms. Qualification as F-X287 defines it still needs your packets.

Left to you: docs/hld/12-testing-strategy.md line 5 and the sprint documents (docs/sprints/BACKLOG.md, CURRENT_SPRINT.md, SPRINT_PLAN.md, SPRINT_TRACKER.md, AS_BUILT.md), which still describe the carried gap. This PR changes only 03-architecture.md and 10-bindings-spec.md.

Custody reads (measured)

Counted with a debug-only parse counter on the CI-style debug build. Each row counts XML parses (XmlTree::parse) per phase, base #450 (d3c5dc4e7) against this branch.

Case Phase #450 This PR
issue shape "a footnote added" (compare result) compare / accept_all / reject_all 156 / 18 / 18 141 / 14 / 15
issue shape "a footnote removed" compare / accept_all / reject_all 121 / 19 / 18 108 / 17 / 14
1000 paragraphs, no note compare / accept_all / reject_all 225 / 3 / 3 225 / 3 / 3
1000 paragraphs, 50 notes, text edits compare / accept_all / reject_all 307 / 10 / 10 308 / 10 / 10
1000 paragraphs, one pending wholly tracked note compare / accept_all / reject_all 315 / 10 / 10 (reject refuses) 316 / 10 / 12

For scale, an earlier sample profile of a note-free run-granularity comparison put all custody validation at 6.8% to 13.9% of compare.

Gates

Run on the final tree (3513f67c2, on top of #450 at d3c5dc4e7), macOS arm64, Rust 1.97.1. Other branches ran full suites on the same machine before and between, so durations are not meaningful.

  • cargo fmt --all --check: pass.
  • cargo clippy --workspace --all-targets --all-features -- -D warnings: pass, exit 0.
  • python3 scripts/prose_check.py: 0 violations.
  • python3 scripts/sync_agent_skills.py --check: 26 skills in sync.
  • python3 scripts/hash_harness.py --check: 49 entries match (the samples make no comparison).
  • python3 scripts/readme_doctests.py: pass, with the re-recorded rdocx archive row (4,531,634 compressed bytes, 56,539,892 member bytes, 37 members, measured 2026-11-16).
  • cargo build -p rdocx-cli: pass.
  • cargo test -p rdocx --no-fail-fast (Poppler from pixi):
    • lib: 1053 passed, 3 failed, 13 ignored. Environmental: large_word_and_presentation_pdfs_preserve_logical_reading_order, word_and_powerpoint_chart_pixels_are_identical (at the rasterizer version assert), fx195_variable_svg_paint_matches_independent_static_instances.
    • integration_test: 572 passed, 5 failed, 8 ignored. Environmental, the LibreOffice-pin and offline tests: odt_reader_matches_pinned_libreoffice_structure, public_authored_theme_and_fonts_match_pinned_word_resolution, sanitized_public_authoring_fixture_passes_every_conformance_stage, section_page_semantics_match_pinned_libreoffice_render, every_conditional_table_region_matches_word.
    • regression_test: 2537 passed, 0 failed, 7 ignored.
    • doc-tests: 2 passed.
  • Python suite on the CI-style debug build (opt-level 2, debug assertions, overflow checks): 1402 passed, 61 failed, 2 skipped. Environmental: test_issue_253_pdf_text_view, test_colour_inputs_and_nested_font_formats_reach_actual_rendering, test_fx200_python_style_saved_output_paints_exact_expected_colours_with_deterministic_native_cli, and the 58 tests of test_rendering_threads.py (the F_X195_* inputs that CI prepares in a step not run here, and the Poppler 26.01 pin with 26.10.0 installed).
  • Release wheel (LTO off, 16 codegen units): mypy --strict on typing_smoke.py and the package, no issues in 9 source files. mypy.stubtest rdocx, no issues in 8 modules.
  • Per commit: the issue43* integration tests and scoped clippy pass at each code commit, the issue43* lib tests at commits 2, 3 and 5, the issue4* regression tests at commit 5, and the issue430 Python tests at commits 3 and 5.
  • The mixed-author lifetime scenarios found in review, on the final debug build: every pair is kept while another author's note is pending.
  • Not run: the wasm check and the other crates' suites.

Tests

Inline Word 16.115 bytes from the saves above, with rsid and w14 paragraph identities removed. Each fixture comment names the Word version and what was done. The fixture of issue430_body_pair_lifted_out_of_a_rejected_insertion_keeps_its_vote is hand-built from two observed Word shapes, and says so.

  • Commit 1: issue430_custody_note_pair_follows_the_note_reference_mark, issue430_word_saved_note_pair_after_the_reference_mark_resolves_and_compares.
  • Commit 2:
    • issue430_word_saved_body_pair_in_its_reference_insertion_resolves_and_compares
    • issue430_body_pair_in_any_other_revision_position_still_refuses: six positions (the deletion, the unmatched pair and the nested pair among them) x three operations, atomic
    • issue430_body_pair_lifted_out_of_a_rejected_insertion_keeps_its_vote: both orders, both choices, reopens. It fails without the lift-out.
  • Commit 3: issue430_word_saved_unmatched_body_pair_retires_when_its_part_settles, issue430_settling_only_the_reference_retires_the_leftover_body_pair, issue430_word_saved_mixed_authors_keep_pairs_until_the_last_note_settles, and the Python test test_issue430_word_saved_merged_body_pair_and_lost_note_pair_settle.
  • Commit 4: issue430_comparison_refuses_a_note_reference_that_moves_to_another_site (the refusal, atomic, and the control that still compares).
  • Commit 5:
    • issue430_word_saved_lost_note_pair_resolves_as_a_tracked_note: 4 shapes, every choice, unrelated comparison
    • issue430_word_saved_mixed_authors_resolve_in_both_orders_and_choices: both pairs lost, and one kept
    • issue430_word_saved_mixed_note_outside_the_grammar_keeps_both_pairs: a mixed note outside the grammar, which fails without the fix
    • issue430_comparison_over_a_word_saved_tracked_note_keeps_both_lifetimes
    • issue430_word_saved_created_part_with_a_lost_note_pair_settles_to_its_destination: the discriminator
    • issue430_tracked_note_needs_its_whole_owner_and_its_only_reference_tracked_one_way: 4 shapes that keep the old refusal atomically, plus both partial selections
    • the Python test test_issue430_word_saved_lost_note_pairs_resolve_in_both_author_orders.

hadim added 11 commits November 16, 2026 18:14
…ularity

Since v0.17.0 a word comparison aligns logical words over characters, so
that a word split across runs compares equal (F-X279b, issue 434 case 3).
The character units then reached the output as they were: every unchanged
character of a changed paragraph became its own w:r, and each letter of a
revision its own w:delText or w:t inside the revision's run. For the 200
paragraphs of issue 446 case 1 that is 17,180 runs and 517 KiB of
document.xml where v0.16.4 wrote 6,200 runs and 245 KiB. Character
granularity has always written one run per character, and v0.16.4 wrote
one run per word, space and punctuation mark at word granularity.

The granularity now sets what is compared, never how the result is cut.
Two rules, in the two helpers that the paragraph path and the inline
control path share, apply to every granularity, story and revision kind:

- coalesced_granular_alignment keeps unchanged pairs together while they
  continue the same original and the same edited text element, as it
  already kept the pairs of one change together. Unchanged text is one run
  between two revisions.
- merge_unit_runs joins consecutive fragments of one text element back
  into one text with one source selection, whole again when every fragment
  is there. A revision holds its text in one w:delText or w:t, and a fully
  unchanged element is published from its source as at run granularity.

The same 200 paragraphs now give 600 runs and 76 KiB, one run per revision
and one per stretch of text around them. Two cases still cut a stretch:
under ignore_whitespace an ignored whitespace unit ends it, so unchanged
text stays one run per word and per space, and a tab or a break ends it as
it ends a text element. The alignment, the text
revisions, the logical-word rule, the shared revision of a word split
across runs, the custody markers and the refusals are unchanged. One
visible difference besides the runs: a formatting change of a run is one
w:rPrChange per unchanged stretch, as at run granularity, instead of one
per word (v0.16.4) or per character (v0.17.0).

The space-flag control of compare_producer_noise pinned the one-run space
of the old shape. It now checks the same fact on the stretch that ends
with the space.

Part of tensorbee#446.

Tests, issue446_* in regression_test.rs state the shape as invariants in
the body, a table cell, an inline control, a hyperlink, a header and a
footnote (one text element a run, no added run that repeats the properties
of the sibling it follows, run count bounded by the revisions, run count
and added size independent of the unchanged text), two unit tests in
comparison.rs, test_issue446_* in test_core.py
Harness, unchanged, the samples make no comparison
…candidate once

A comparison became slower in v0.17.0 at every granularity, the default
run granularity included (about 4 times on the documents of issue 446,
with the same output), and quadratic in the children of one paragraph or
table. Four causes, none of them in the alignment:

- The source guard of F-X272 (af99e9d) read a parent from its start
  once per child. RunSource::capture did it for each run of a paragraph,
  publication_parent_at and source_child for each row of a table, cell of
  a row, child of a cell or a control and inline control of a paragraph,
  in the five source snapshots of a comparison and again in the compare
  and mark paths (compare_table, compare_row, compare_cell_from_xml,
  compare_granular_controls, compare_complex_paragraph,
  paragraph_children_xml, marked rows and cells, and the parent of each
  written piece in compare_granular_paragraph). capture_all,
  publication_parents_at and source_children read a parent once for all
  its children. Each answer, an error included, is the one a read for
  that child alone gives, and each is taken where that read was made, so
  the errors keep their order. ParentScopes reads the scopes a paragraph
  path may ask on its first question.
- Each postcondition direction resolved the candidate twice, once for the
  package guard and once more inside verify_footnote_destination
  (F-X272a, 1c69b7e). resolved_candidate resolves it once and both
  checks read it.
- The postcondition signature of an ordinary part is taken at character
  granularity so that run splits do not change it (F-X279b, 2d38731).
  It built one unit, one copy of the run properties and one token per
  character of all four documents. A run with no raw child and no deleted
  text is now read one stretch at a time, and consecutive plain characters
  are one token, in paragraphs and in the direct runs of a control. Two
  signatures read equal exactly when their character tokens did.
- A paragraph snapshot computed its run spans twice. It computes them
  once.

The package guard still reads each cleaned story a second time, as
v0.17.0 does. That read refuses a drawing that shares its run with the
begin mark of a complex field, whatever else changes ("comparison could
not correlate split field run 2"), and a test pins that refusal: a speed
change must not change which inputs a guard refuses.

compare() CPU time, median of 5 runs alternated between builds, release
wheels (0.16.4 and v0.17.0 from PyPI with fat LTO, this branch with LTO
off and 16 codegen units, which favours the older wheels), measured on
the final tree of this series:

    case                       granularity  0.16.4  v0.17.0  branch
    issue 446, 200 changed     run          0.11 s   0.44 s  0.31 s
    issue 446, 200 changed     word         0.64 s   6.65 s  0.45 s
    issue 446, 200 changed     character    1.74 s   6.95 s  0.76 s
    issue 446, 400 changed     run          0.21 s   0.89 s  0.62 s
    issue 446, 400 changed     word         1.38 s  14.40 s  0.94 s
    1000 paragraphs, 100 chg.  run          0.33 s   1.14 s  0.81 s
    1000 paragraphs, 100 chg.  word         0.73 s   4.33 s  0.94 s
    1000 paragraphs, 100 chg.  character    1.57 s   4.62 s  1.01 s
    one paragraph of 400 runs  run          0.06 s   0.53 s  0.12 s
    one paragraph of 400 runs  word         0.09 s   9.29 s  0.15 s
    table of 100 rows          run          0.21 s   1.74 s  0.88 s
    table of 100 rows          word         0.33 s   2.34 s  0.95 s

Growth for 4 times the rows of a table is 4.2 (0.16.4 4.2, v0.17.0 6.9)
and for 4 times the runs of one paragraph 4.4 at run and 4.6 at word
granularity (v0.17.0 13.2 at run).

Run granularity stays 2.5 to 3 times 0.16.4 on paragraphs and 4.2 on a
table. Restoring the guard's second read accounts for part of it. A
release profile of the issue 446 script at run granularity (400
paragraphs) attributes 24 percent of a comparison to the footnote custody
validation in a document with no note, 18.5 percent to
canonicalize_drawing_ids in a document with no drawing, 19 percent to the
package guard and 15 percent to compare_body.

Checked on the final tree of this series against the v0.17.0 release
wheel, with a debug build and every debug oracle on. The 1,080 cases of a
refusal map around revisions, notes, comments, moves, controls, fields and
bookmarks give the same refusals and messages and leave refused inputs
intact. 792 guard cases give the same refusals, messages and
run-granularity bytes. 500 random pairs give the same accepted and
rejected text and formatting, and the same insertions, deletions and run
bytes except 3 pure resplits inside an inline control, which a later
commit of this series compares as equal.

Part of tensorbee#446.

Tests, comparison.rs units compare the one-read answers with lone reads,
errors included, check the stretch tokens, and count the source bytes a
comparison reads for few and many runs, rows, hyperlink runs and
inserted paragraphs and tables at the three granularities,
issue446_comparison_keeps_refusing_a_drawing_in_a_field_begin_run in
regression_test.rs, no timing threshold
Harness, unchanged, the samples make no comparison
A word or character comparison of a changed paragraph whose w:t holds a
reference refused with "split text has opaque mixed events", in the body
and in every related story. That covers &amp;, &lt;, &gt; and &quot;,
which rdocx writes itself on save, and numeric references such as
&tensorbee#233;: saving and reopening "Smith & Sons pay now." and comparing it
with "... later." refused. 0.16.4 compared it.

source_text_fragment (F-X272, af99e9d) checks that the source of a
split text is one element holding text, and took exactly one text event.
The XML reader reports a reference as an event of its own between two
pieces of text. Any number of text and reference events now pass at the
text's depth. A nested element, CDATA, a comment or a processing
instruction still refuses. The value written comes from the parsed model
and is escaped again on output, as before.

Part of tensorbee#446.

Tests, issue446_comparison_splits_text_that_holds_references in
regression_test.rs, with the reference in the changed token, at word and
character granularity, in the body and a header, accepted and rejected
back to each input
Harness, unchanged, the samples make no comparison
A paragraph whose runs only split the same text differently compares
equal since v0.17.0 (F-X279b). An inline content control did not: its
runs were aligned one by one, so one run against three with the same text
and formatting gave 4 revisions at run granularity, as in 0.16.4. A word
redline of one sentence inside a control, accepted or rejected and
compared again with its input, gave 25 revisions at word granularity and
70 at character granularity on 0.16.4 and v0.17.0, and 4 and 8 before
this commit. It now gives none.

When every child of both controls is a run and the runs differ,
compare_control_from_xml now reads them with the equality the paragraph
path trusts, coalesced_paragraphs_equal, and keeps the original source
when they are equal. It does not apply under leaf capture, to a control
holding a raw child or a revision, or to runs holding a field, whose
source facts it cannot read from copies, which keep the path below.

Part of tensorbee#446.

Tests, issue446_comparison_reads_a_resplit_inline_control_as_equal in
regression_test.rs at the three granularities, with a real change kept as
a revision
Harness, unchanged, the samples make no comparison
The rdocx archive grows with the issue 446 comparison changes and their
tests, on main eb980e9. It is the only crate whose member bytes change.
It was measured on 2026-11-16, which its row and the remeasurement date
now record.

Tests, scripts/readme_doctests.py build_package_archive and
archive_measurement for rdocx, member bytes stable on a second pass
Harness, unchanged, measurements only
… mark

Microsoft Word for Mac 16.115 drops a zero-width bookmark pair that
precedes the reference mark of a footnote that is not the first of its
part, and keeps one that follows it (observed on every such save of the
issue 430 shapes, again on a second save of the same file, and kept in 8
of 8 shapes once moved after the mark). A compare result whose note pair
Word dropped can no longer prove its note owner, so later resolutions of
that note refuse or lose its custody.

The note pair now follows the reference-mark run inside the note's content
insertion or deletion. A note without a reference-mark run keeps the
earlier position, the start of its insertion or the paragraph boundary
outside its deletion. Recognition is unchanged: it already admits a note
pair as a direct child of the one wrapper that holds the note content.

Part of tensorbee#430 (F-X287).

Tests, issue430_custody_note_pair_follows_the_note_reference_mark checks
the emitted position in both directions with and without a reference
mark, issue430_word_saved_note_pair_after_the_reference_mark_resolves_and_compares
runs a Word-saved result through every choice and an unrelated comparison
Harness, unchanged, the samples make no comparison
…eference's insertion

Word for Mac 16.115 merges adjacent insertions of one author and date.
When a comparison adds a paragraph with a new footnote, the paragraph's
text insertion and the reference insertion meet around the body custody
pair, and the saved file has the pair inside the merged insertion, right
before the reference run (the issue's own "a footnote added" case).
note_markers refused every body pair inside a revision, so Accept All,
Reject All, selective resolution and an unrelated comparison all refused
with "body custody pair is inside a revision".

A body pair is now also admitted as a direct child of the one insertion
of a main-story paragraph whose next child is the reference run, and only
when its note pair is present. That is the same proof as at the direct
position: the next sibling names the reference, and the note pair binds
the owner, which must agree. The insertion is the reference's own
revision element, so it resolves with the unit.

Everything else inside a revision still refuses, atomically: an unmatched
pair (no note pair), a pair inside a deletion (Word was not observed to
put one there), a pair folded into a preceding insertion or placed before
another run, a pair in a move destination, and a pair nested one level
deeper.

An insertion whose content a resolution removes now keeps such a pair in
its place instead of dropping it with the content. When another unit is
still pending, the pair stays a tombstone and its part-presence bits
still vote, so the lifetime does not depend on Word's merging. Word's own
Reject All also keeps the pair.

Part of tensorbee#430 (F-X287).

Tests, issue430_word_saved_body_pair_in_its_reference_insertion_resolves_and_compares
on the Word-saved bytes through every choice, an unrelated comparison and a
partial id, issue430_body_pair_in_any_other_revision_position_still_refuses
for six positions (the deletion, the unmatched pair and the nested pair
among them) and three operations, atomic,
issue430_body_pair_lifted_out_of_a_rejected_insertion_keeps_its_vote for
both author orders and choices with reopens, which fails without the
lift-out (the created part would survive)
Harness, unchanged, the samples make no comparison
…les the note part

When Word drops a note custody pair, its body pair is left without a note
counterpart. Q-A430-29 keeps such a pair unassociated, so no unit is
recognized and settle_footnote_custody returned early: accepting the whole
Word-saved comparison left the hidden body bookmark behind, while a
library-only result settles with no marker at all.

A resolution that edits the note part or resolves a note reference, and
leaves no revision in a normal note owner or a note reference, now settles
the part even when no unit is recognized, and retires the pairs left in
it, at the same point as any settlement. Resolving the reference alone
counts, so the pair also retires when the note side settled first, in an
earlier call or in Word. Whether a reference was resolved is read from the
render that resolved it, only when the document holds custody pairs. A
pair in a part that the resolution leaves untouched stays, as before, and
while any note revision is pending every pair stays. This matches the
contract for markers Word leaves in a completed part: kept while
untouched, retired once the library edits that part.

Part of tensorbee#430 (F-X287).

Tests, issue430_word_saved_unmatched_body_pair_retires_when_its_part_settles
on Word-saved bytes, with Word for Mac 16.115's own Accept All output as
the untouched and the edited-part controls,
issue430_settling_only_the_reference_retires_the_leftover_body_pair (the
note side settled first by id, or already settled as Word leaves it, both
choices), issue430_word_saved_mixed_authors_keep_pairs_until_the_last_note_settles
for both author orders with reopens,
test_issue430_word_saved_merged_body_pair_and_lost_note_pair_settle in
test_core.py for the merged body pair (part B) and the leftover pair
through the binding
Harness, unchanged, the samples make no comparison
Footnote comparison pairs the owners of both inputs by note id. When the
edited input inserts a note before an existing one, as Word and the
add_footnote producer number notes, the new note takes the existing
note's id and the existing note moves to the next id. The comparison then
paired two different notes as one text change, deleted the original
reference and inserted the edited one, both naming the same owner. The
pending result held two physical references to one note, the
shared-reference shape of Q-A430-28. Accept and reject still gave the
edited and original notes, but Word for Mac 16.115 could not open the
pending file (review A, the "a new note before an existing one" result,
the same on v0.17.0).

A footnote that both inputs reference now refuses atomically, before any
note custody is written, when the pending result would give it more
physical references than either input does. That is the "refuse a moved
reference" rule of the note contract. Producing a pending result for this
edit instead would need note ids that differ from one destination, which
the exact-identity contract does not allow.

Part of tensorbee#430.

Tests, issue430_comparison_refuses_a_note_reference_that_moves_to_another_site
(the refusal, atomic, and the same edit with the new note after the
existing one still compares to both exact destinations)
Harness, unchanged, the samples make no comparison
…d dropped

A comparison that Word for Mac 16.115 saved before the note pair moved
after the reference mark has lost its note pair for good: the RC3 packets
of F-X287 are such files. Q-A430-29 forbids recovering the owner from the
remaining body pair, so the note resolved as ordinary revisions. Rejecting
an added note, or accepting a removed one, then had to remove the note's
only paragraph mark and refused with "cannot remove the final paragraph
mark". With another author pending, resolving the first author could also
retire every custody pair while the second author's revisions stayed
pending, because only marked units counted as pending.

A tracked-note unit is a normal owner that no pair binds and whose own
bytes prove that the whole definition is tracked as one insertion or one
deletion: every paragraph carries a paragraph-mark revision of that kind,
every other paragraph child is a wrapper of that kind or a zero-width
range marker, and its one reference, bound by footnote id, is wrapped the
same way. The proof reads no marker and no adjacent element, so an
unmatched body pair stays unassociated. An unmatched body pair inside its
reference's insertion is admitted only when that reference's note is a
tracked note, and only keeps its place there.

A selection of all its elements resolves it as one: the owner goes before
rendering when the resolution removes its content. A partial selection
keeps the ordinary element rules. A whole selection of an unreferenced
tracked note removes it. An owner with two or more references keeps the
shared-reference rules (Q-A430-28). A tracked note has no token or bits
and does not vote on part presence, but it is pending for the open
lifetime, so pairs stay while it is pending and retire at the last
settlement. When a tracked note settles while a note owner outside every
unit still holds a revision (a note with a hyperlink, say), the pairs
stay too, as on v0.17.0. A comparison on top keeps an earlier author's
unmatched body pair instead of retiring it at entry.

The proof uses the note tree and the reference inventory that
validate_footnote_custody already reads. Each reference site now carries
its wrapper's revision, which also drops the per-site reparse of the
reference story in the marked-unit check. Settlement reads the reference
inventory only when it removes an owner, and does not read the note part
again when it removes nothing and either a unit is still pending or no
marked unit settles. Retirement returns at once when no unit carries a
token, and projection returns before reading markers when no marked unit
is retained or projected.

Reverting this commit brings back, for files whose note pair Word already
dropped and for hand-built wholly tracked notes: the refusals when
rejecting a tracked note in a mixed save (review scenarios L1.2 and L1.4)
or without any comparison (C2), and when removing an unreferenced tracked
note (C3), and the first-author marker loss on the mixed shape with one
pair kept and one lost. Its archive row must then be re-recorded.

Part of tensorbee#430 (F-X287). This needs the maintainer's decision D1.

Tests, issue430_word_saved_lost_note_pair_resolves_as_a_tracked_note on
four Word-saved shapes (added note, added note with text and a new
paragraph, merged body pair, removed note) through every choice and an
unrelated comparison,
issue430_word_saved_mixed_authors_resolve_in_both_orders_and_choices for
both author orders, both choices and reopens, with both note pairs lost
and with one kept,
issue430_word_saved_mixed_note_outside_the_grammar_keeps_both_pairs (from
review B: a mixed note with a hyperlink keeps both pairs, either author
first, either choice),
issue430_comparison_over_a_word_saved_tracked_note_keeps_both_lifetimes
(a comparison that adds custody over a pending tracked note),
issue430_word_saved_created_part_with_a_lost_note_pair_settles_to_its_destination
(the discriminator: a part created with two notes rejected in either
order ends absent, and without any pair a separator-only part stays),
issue430_tracked_note_needs_its_whole_owner_and_its_only_reference_tracked_one_way
(four shapes that keep the old refusal atomically, and both partial
selections), test_issue430_word_saved_lost_note_pairs_resolve_in_both_author_orders
in test_core.py
Harness, unchanged, the samples make no comparison
The rdocx archive grows with the issue 430 custody changes and their tests,
on tensorbee#450 (d3c5dc4). It is the only crate whose member bytes change. Its
measurement date moves to the day of this measurement.

Tests, scripts/readme_doctests.py build_package_archive and
archive_measurement for rdocx, member bytes stable on a second pass
Harness, unchanged, measurements only

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant