Skip to content

Comparison: track paragraph count changes in cells and boundary changes at story ends (#453) - #454

Open
hadim wants to merge 7 commits into
tensorbee:mainfrom
hadim:fix/compare-paragraph-boundaries
Open

hadim wants to merge 7 commits into
tensorbee:mainfrom
hadim:fix/compare-paragraph-boundaries

Conversation

@hadim

@hadim hadim commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #453 (cases 1, 2 and 3). Stacked on #450: its five commits come first, this pull request adds the last two (b8ad09896, b2d894224). Once #450 merges, the diff reduces to those two.

What changes

Three refusals of compare on real document pairs, each reduced to a synthetic pair in #453, now compare and resolve both ways.

  • A table cell whose paragraphs merge, split, or are inserted or deleted. compare_cell_from_xml paired cell children by position and refused any change in their number. Such a cell now aligns its children as a block content control already does: align, pair_gaps_by_kind and replace_changed_paragraph_runs, then the trailing-paragraph rule (mark_previous_paragraph, final_paragraph) for a final inserted or deleted paragraph, so the last paragraph mark stays the cell terminator. A cell with as many children on both sides keeps its pairing by position, unless one pair there would refuse (different kinds, or a paragraph pair of the next point).
  • A final paragraph that gains or loses a hyperlink, in a cell, the body or a block content control. compare_paragraph replaced such a pair in place as a deleted paragraph followed by an inserted one, whose mark is the story terminator when the pair is last: rejection had to remove it, and the self-check refused (cannot remove the final paragraph mark, paragraph mark removal requires an adjacent paragraph). In replace_changed_paragraph_runs, a last pair whose inline boundaries differ now becomes a deleted and an inserted paragraph, which the trailing-paragraph rule already writes correctly: the mark before them carries both markers.
  • A paragraph pair whose bookmarks differ, in a changed run that holds a hyperlink (a paragraph that becomes a bookmarked heading). The run could not be replaced whole (FieldBalance::Uncarried), and the in-place replacement requires equal bookmarks, so the attributed path refused the pair. Such a pair now also becomes a deleted and an inserted paragraph.

Guarantees kept

  • Only inputs that compare refused before take the new paths. A cell with the same number of children and no refusing pair, and every pair revised in place, keep their output byte for byte.
  • A pair stays paired, and keeps its refusal, when either side holds a complex field boundary (FieldEvent::Begin or End), a simple field outside a complex field (paragraph_wraps_simple_field), or a bookmark or comment range that the other input also holds anywhere in the same container. Unpairing would hold that marker twice in the redline. This keeps the refusal for a producer that renumbers generated bookmarks (_Toc1, _Toc2, ...) when a heading is inserted, the last example of compare refuses a paragraph count change in a table cell, a hyperlink change on a final paragraph, and a bookmarked heading beside a hyperlink #453.
  • The acceptance self-check is unchanged and proves each new output: accepting gives the edited projection, rejecting the original.
  • A cell with unmodeled children (extra_xml) keeps its refusal.

Not changed

Tests and gates

  • New regression_test comparison_tracks_paragraph_count_changes_in_cells_and_boundary_changes_at_story_ends. It runs 13 pairs through compare_and_resolve with the default options and with granularity=word, ignore_fields, ignore_formatting. Accepting must give the edited document and rejecting the original, text and an empty postcondition compare against each input. The pairs cover:

    • in a cell: merge, split, deleted first, middle or final paragraph, inserted final paragraph, three paragraphs reduced to one, a nested table between changed paragraphs, a one-paragraph cell that gains or loses a hyperlink.
    • a last body paragraph and a last control paragraph that gain a hyperlink.
    • a paragraph that becomes, or stops being, a bookmarked heading beside a hyperlink.

    It also checks that a pair sharing a bookmark across the two inputs keeps its refusal.

  • Run locally: cargo fmt --all --check, cargo clippy -p rdocx --all-targets -- -D warnings, cargo test -p rdocx --test regression_test compar (102 passed, 1 ignored), cargo test -p rdocx --lib comparison (77 passed), scripts/prose_check.py, scripts/readme_doctests.py with the re-recorded rdocx archive row. The full suite is left to CI.

  • The three real pairs that found compare refuses a paragraph count change in a table cell, a hyperlink change on a final paragraph, and a bookmarked heading beside a hyperlink #453, which cannot be shared, compare and resolve on this branch, apart from the documented renumbered-bookmark refusal on one of them. With its generated bookmarks removed from both copies, that pair resolves too. Under ignore_fields=True the only difference left after acceptance is a cached NUMPAGES result, which keeps the original's value by design.

Docs

docs/hld/03-architecture.md, comparison section: the cell alignment and the rule that turns a refusing pair into a deleted and an inserted paragraph, with its exceptions.

…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 &, <, > and ",
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
…changes at story ends

A cell whose paragraphs merged, split, or were inserted or deleted was
refused as a cell boundary change, because compare_cell_from_xml paired
its children by position. Such a cell now aligns its children as a block
content control does: align, pair_gaps_by_kind and
replace_changed_paragraph_runs, then the trailing-paragraph rule for a
final inserted or deleted paragraph. A cell with as many children on both
sides keeps its pairing by position unless one pair there would refuse.

A paragraph pair whose inline boundaries differ was replaced in place by
compare_paragraph only when its bookmarks were equal, and that in-place
replacement leaves an inserted paragraph mark that no rejection can remove
when the pair is the last owner of a story, cell or content control. Such a
pair now becomes a deleted and an inserted paragraph, which the
trailing-paragraph rule closes on the mark before them. It keeps its
refusal when either side holds a complex field boundary, a simple field, or
a bookmark or comment range that the other input also holds, as when a
producer renumbers generated _Toc bookmarks.

Only inputs that compare refused before take the new paths. Equal-count
cells and pairs revised in place keep their output.

Tests, regression_test comparison_tracks_paragraph_count_changes_in_cells_and_boundary_changes_at_story_ends,
regression_test and lib tests filtered on compar
The rdocx archive grows with the comparison change and its test, on top of
the issue 446 comparison branch. It is the only crate whose member bytes
change. It was measured on 2026-11-16, which its row already records.

Tests, scripts/readme_doctests.py build_package_archive and
archive_measurement, then scripts/readme_doctests.py

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.

compare refuses a paragraph count change in a table cell, a hyperlink change on a final paragraph, and a bookmarked heading beside a hyperlink

1 participant