Skip to content

Move a comment thread with its root, and consume table cells by one rule (#446 cases 2 and 3) - #448

Open
hadim wants to merge 3 commits into
tensorbee:mainfrom
hadim:fix/446-thread-move-grid-span
Open

hadim wants to merge 3 commits into
tensorbee:mainfrom
hadim:fix/446-thread-move-grid-span

Conversation

@hadim

@hadim hadim commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Part of #446 (cases 2 and 3). Case 1, the word-granularity compare, is not in this pull request.

Context: why case 2 reverses a decision of F-X279d

This pull request changes what a root comment move does with the replies of its thread, which reverses the expectation of issue434_root_moves_keep_crossing_replies_and_generic_pairing_strict and the HLD 03 sentence "Root moves select that comment's qualified markers ... preserving crossing reply anchors". The reason is a data loss, so here is the history first.

  • 0.16.4. reply_to wrote no range and no reference for a reply. A reply was only a definition and a commentsExtended row, so it followed its root wherever the root moved.
  • 0.17.0, F-X279d. Replies got their own range crossing their parent's (rdocx: comments, replies, compare, revisions and insert_document depart from Word #434). The root-move consumer then refused threads with crossing replies, and the bounded repair at .claude/plans/F-X279d-design.md:11-19 made it select only the root's markers. That design says the extension "preserves root-only moves, thread metadata, unrelated reply anchors and atomic refusal". So the replies stayed on the old text by construction, not by an explicit product choice.
  • The loss. Move a root, then remove the text it left. The replies are anchored on that text, so the removal reconciliation removes them with it, silently, as it must for any comment whose whole anchor goes.

What was verified, and how:

  • With the released 0.16.4 wheel, after move_comment_to_text(root, "bearing positions") the reply has no anchor, and removing the old paragraph keeps both comments.
  • With the released 0.17.0 wheel, the reply stays on "about 12 mm", and removing that paragraph leaves only the root.
  • On this branch, the reply crosses the new text and both comments survive the removal.
  • A thread that LibreOffice 26.8 wrote in a headless round trip has nested reply ranges: a root, two replies and a reply to a reply. On this branch it moves as one unit, and removing the old text keeps all four comments.
  • Microsoft Word was not run. What Word does when commented text moves is the motivation in the issue, not an observation of this pull request.

Case 2: a root move carries its thread

Root cause. move_comment_to_text and the comment branch of move_story_range in crates/rdocx/src/comments.rs moved the root's three markers only.

Rule now. The move runs on one staged candidate with one commit:

  1. The root moves exactly as before.
  2. Each reply that has one range start, one range end and one reference leaves its old place.
  3. It then crosses the new range of its nearest anchored ancestor through the reply authoring path (anchor_reply_crossing, which reply_to also uses). Parents go first and siblings in id order, so the markers are exactly those reply_to writes. The unit test asserts that order.
  4. The reply's original reference run, with its formatting and attributes, replaces the synthesized one, as the root's already does.

Other reply shapes:

  • A reply with no marker at all, as 0.16 wrote them, follows through the thread metadata.
  • Any other reply anchor refuses the move before any edit.
  • A reply that fails after the root moved in the candidate refuses the whole move, and the document is unchanged.
  • Moving a reply id still refuses, and the error names its thread root.

move_comment onto the root's current range still publishes nothing when every anchored reply already crosses it. When a reply sits elsewhere, it now runs the move, which gathers the replies. That is the healing call for files that 0.17.0 already split (see below).

The removal contract is unchanged. A comment whose whole anchor goes is removed with its replies, and a cut that would leave a reply anchor behind a removed root refuses. A thread-level refusal was tried and rejected: rejecting a compared reply insertion legitimately removes one reply's anchor while its root stays, and seven Python comparison tests rely on it.

Entry points.

  • Rust: Document::move_comment, move_comment_to_text, move_story_range on a comment range.
  • Python: Document.move_comment, Document.move_comment_to_text.
  • CLI: rdocx comment move.

Operation table. comment_thread_operations_keep_every_reply_on_its_root_text runs 15 operations on a root with a reply, a sibling reply and a reply to the reply. Each row must keep every reply's own range and reference on its root's text, or remove the whole thread, or refuse without changing the bytes. It also checks that no marker outlives its comment, after save and reopen.

  • The 4 move rows fail on v0.17.0 (checked by running the table on the base commit): move_comment_to_text, the same in one paragraph, move_comment, and move then remove the old text.
  • The other 11 rows pass on both and are now pinned: remove_content, remove_content_range, remove_content_at (refused), Paragraph::set_text, try_replace_text, clone_content, move_content, compare then accept, compare then reject, import_fragment of a whole body (the Python insert_document path), and save and reopen.

Further tests:

  • comment_thread_moves_carry_replies_across_story_owners: out of, and back into, a table cell, header, footer, footnote and text box.
  • comment_thread_moves_heal_split_threads_and_refuse_untravelable_replies: healing, a reply with no marker, a reference-only reply, and a reply reference inside w:del that fails after the root moved.
  • The inverted unit test also asserts the exact marker order after both move calls.

Cost. Each anchored reply adds about half a root move. The reply selections run on the flushed candidate instead of fresh staging clones, which removed about 40 percent of the per-reply cost. Release builds, move_comment_to_text at 800 paragraphs on a loaded machine:

replies before this change after
0 0.270 s 0.249 s
1 0.512 s 0.383 s
4 1.860 s 0.815 s
16 4.406 s 2.548 s

Going further would mean anchoring all replies in one source pass, which is a rewrite of the reply authoring path. It is not done here.

Case 3: one rule for every edit that consumes a cell

Root cause.

  • Table::set_cell_grid_span_checked and Table::set_row_grid_omissions consumed a cell only when its paragraph equaled CT_P::new() and its properties held a width alone (cell_is_discardable_for_grid_edit).
  • rdocx: markup the library writes breaks the schema or differs from Word (equations, notes, hyperlinks, cells, numbering, TOC, replace) #432 case 4 relaxed Table::merge_cells only. So a span refused a right-aligned empty paragraph, or the w14:paraId Word writes on every paragraph, that Cell.merge consumed.
  • The span and omission edits also removed and inserted cells with raw Vec calls. So a row-level w:bookmarkEnd or w:permEnd after the last cell was lost, and a leading omission moved a row-level range onto another cell. v0.17.0 did the same on a bare <w:p/> cell.

Rule now. cell_is_consumable serves all three edits. A cell is consumed when two conditions hold.

  • Its content is one paragraph that only paragraph formatting (including the mark's run formatting) or root identity attributes tell from an empty one. A tracked paragraph mark, a tracked paragraph property change, a tracked numbering change and section properties count as content.
  • The cell has no opaque cell, cell-property or border XML and no vertical merge ownership. Its modeled formatting, such as shading or vertical alignment, goes with it, as in merge_cells and in Word's merge.

What each edit does with a cell that fails:

  • A merge refuses a cell whose metadata cannot go, and keeps any other content in the merged cell.
  • A span or an omission refuses, with the existing errors. They also refuse a legacy w:hMerge cell. merge_cells never reaches that case, since its column-edit topology check refuses any table that holds one, on v0.17.0 too.

Spans and omissions now remove cells through remove_row_cell, as merge_cells does. A narrowing span and a trailing omission restore cells through a sibling that inserts after the row-level markers at that boundary, so a span or a trailing omission round trips. A leading restore keeps markers at the row start leading.

Entry points.

  • Rust: Table::merge_cells, set_cell_grid_span_checked, set_row_grid_omissions.
  • Python: Cell.merge, Table.set_cell_grid_span.
  • The CLI and WASM have no merge commands.

Tests. issue446_every_cell_consuming_edit_shares_one_rule runs 15 cell contents over 5 entry points (merge across, merge down, grid span, leading omission, trailing omission) with one expected outcome per row: consumed, kept by a merge and refused elsewhere, or refused everywhere.

  • Consumed: an empty paragraph, paragraph properties, mark run properties, identity attributes, modeled cell formatting.
  • Kept by a merge, refused elsewhere: an empty run with properties, a bookmark, a comment anchor, text, two empty paragraphs, a tracked inserted mark, a tracked deleted mark, a tracked property change, section properties.
  • Refused everywhere: opaque cell XML.

It also checks legacy hMerge cells, and row-level markers through a span, both omissions and their restoration. test_issue446_grid_span_and_merge_consume_the_same_cells runs the issue's script through Python.

Behaviour changes

  • move_comment, move_comment_to_text and rdocx comment move move every reply that has a range with its root. Replies with no marker are unchanged.

  • A root move now refuses when a reply has one of six shapes that v0.17.0 moved the root past, leaving the reply behind:

    • a reference without a range,
    • a range without a reference,
    • a start alone,
    • two references,
    • a reversed range,
    • a reference inside w:del.

    No writer found produces them: LibreOffice 26.8 writes full nested ranges, 0.16.4 writes no marker and 0.17.0 writes full ranges.

  • move_comment onto the root's own range gathers replies left elsewhere, instead of returning early.

  • Error wording:

    • comment 1 is a reply, not a movable root: it moves with its thread root 0
    • comment 0 cannot move: reply 1 has an incomplete anchor
    • comment 0 cannot move: reply 1: <cause>
  • set_cell_grid_span and set_row_grid_omissions consume empty cells that carry paragraph formatting, identity attributes, modeled cell formatting or their own gridSpan, as Cell.merge already did.

  • Cell.merge keeps a paragraph with a tracked mark, a tracked property change or a tracked numbering change, where v0.17.0 dropped it and its revision. For section properties, the native merge keeps the paragraph, and the Python Cell.merge refuses with "generic content operations cannot mutate a section-owning paragraph" instead of dropping the section.

  • Spans and omissions keep row-level markers at their cell boundaries.

Not changed

  • Files that 0.17.0 already saved with split replies. Removing the old text still removes the replies anchored on it, as for any comment whose whole anchor goes. move_comment(root, root.anchor), or move_comment_to_text onto the root's current text, gathers them first. The heal test pins this.
  • numPr on an empty paragraph. It counts as formatting. A merge drops a numbered empty paragraph, as on v0.17.0, and a span or an omission now consumes it too, so its visible list number goes.
  • Table::set_cell_vertical_merge. It only writes the property and removes no cell, so it keeps its own validation. Layout hides a continuation cell's content (rdocx-layout/src/table.rs), and clearing the merge shows it again. merge_cells stays the content-aware vertical merge.

Findings in no issue

Native Rust Document::insert_document, append and append_with_break import the body only (HLD 03 says native insertion imports no companions), but they keep the source's comment markers and note references in that body. With a source holding one root, one reply and one footnote reference, all three calls give the same host:

  • The 6 comment markers stay, with no comments part, so validate_comment_ownership fails with "comment markers have no owned definitions part".
  • The w:footnoteReference stays, while footnotes() is empty and the note text is not in the package.

A single comment without replies behaves the same, on v0.17.0 too. The note references make this larger than dropping comment markers, so this pull request does not touch it.

Gates on the final tree

Run on 77c101e, macOS arm64, Poppler 26.09 through pixi exec -s poppler.

  • cargo fmt --all --check and cargo clippy --workspace --all-targets --all-features -- -D warnings: clean.
  • python3 scripts/prose_check.py: 0 violations. python3 scripts/sync_agent_skills.py --check: clean. python3 scripts/hash_harness.py --check: 49 entries match.
  • python3 scripts/readme_doctests.py: passes, with the rdocx and rdocx-cli archive rows re-recorded in their own commit.
  • cargo test -p rdocx --no-fail-fast (RUST_MIN_STACK=8388608): lib 1048 passed, 3 failed. integration_test 557 passed, 5 failed. regression_test 2534 passed. The 8 failures pin tool versions this machine does not have:
    • lib: large_word_and_presentation_pdfs_preserve_logical_reading_order (pdftotext 26.01.0), word_and_powerpoint_chart_pixels_are_identical (at the rasterizer version assert), fx195_variable_svg_paint_matches_independent_static_instances (HarfBuzz 14.4.0).
    • integration: odt_reader_matches_pinned_libreoffice_structure, public_authored_theme_and_fonts_match_pinned_word_resolution, section_page_semantics_match_pinned_libreoffice_render and every_conditional_table_region_matches_word (LibreOffice 26.2.5.2), sanitized_public_authoring_fixture_passes_every_conformance_stage (cargo run --offline without the crate in the local registry).
  • cargo test -p rdocx-cli --no-fail-fast: 120 passed.
  • Python, built as the CI job builds it (maturin develop --locked, debug assertions on), full crates/rdocx-py/tests: 1400 passed, 2 skipped, 61 failed. The 61 pin tools: test_issue_253_pdf_text_view (pdftotext 26.01.0), test_colour_inputs_and_nested_font_formats_reach_actual_rendering (pdftoppm 26.01.0), test_fx200_python_style_saved_output_paints_exact_expected_colours_with_deterministic_native_cli (RDOCX_CLI unset), and the 58 tests of test_rendering_threads.py (Poppler 26.01.0 and installed fonts).
  • Release wheel: mypy --strict on typing_smoke.py and the package, no issues in 9 source files. mypy.stubtest rdocx: no issues in 8 modules.
  • The cell rule test goes red when the new paragraph-property check is disabled, and when the span removal or the trailing omission goes back to a raw Vec call.

Since 0.17.0 a reply has its own range crossing its parent's (tensorbee#434). The
root moves of F-X279d moved only the root's range, so move_comment,
move_comment_to_text and rdocx comment move left every reply on the old
text, and removing that text then removed the replies with it. On 0.16.4
a reply had no range and followed its root.

A root move now carries its thread on the same staged candidate. Once
the root's range has moved, each reply with a complete range and
reference leaves its old place and crosses its nearest anchored
ancestor's new range through the reply authoring path, parents first and
siblings in id order, so the markers are those reply_to writes. Its
original reference run travels as the root's does. The selections run on
the flushed candidate instead of fresh clones. A reply without markers,
as 0.16 wrote them, follows through the thread metadata. A reply with any
other anchor refuses the move before any edit, a reply that fails after
the root moved refuses it naming the root and the reply, and a reply id
still refuses, now naming its thread root.

Moving a root onto its own range publishes nothing when every anchored
reply already crosses it, and otherwise gathers the replies, which heals
a thread that a 0.17.0 move split. Removal keeps its contract: a comment
whose whole anchor goes is removed with its replies.

Tests, regression_test comment_thread_operations_keep_every_reply_on_its_root_text,
comment_thread_moves_carry_replies_across_story_owners and
comment_thread_moves_heal_split_threads_and_refuse_untravelable_replies,
comments unit issue446_root_moves_carry_crossing_replies_and_generic_pairing_strict
which inverts the reply expectations of the F-X279d root move test,
test_core issue446 thread move, CLI comment move with a reply
Harness, unchanged, no sample moves a comment
Table::set_cell_grid_span_checked and set_row_grid_omissions consumed a
cell only when its paragraph equaled an empty one and its properties held
a width alone, while Table::merge_cells drops a lone paragraph whatever
its paragraph properties and identity attributes (tensorbee#432 case 4). So
set_cell_grid_span refused a cell whose only paragraph was right aligned,
or carried the w14:paraId Word writes, that Cell.merge consumed.

The three now share cell_is_consumable. Its content is one paragraph that
only paragraph formatting or root identity attributes tell from an empty
one. A tracked paragraph mark, a tracked paragraph property or numbering
change and section properties count as content, so a merge now keeps such
a paragraph where it dropped it, and a span or an omission refuses. The
cell has no opaque cell, cell-property or border XML and no vertical
merge ownership. Modeled formatting such as shading goes with the cell,
as merge_cells and Word's merge already do. Spans and omissions also keep
refusing a legacy hMerge cell, which merge_cells never reaches since its
column-edit topology check refuses such a table.

Spans and omissions now remove and restore cells through the row helpers
that merge_cells uses, so row-level bookmark and permission markers keep
their cell boundaries instead of being lost or moved to another cell. A
restored cell goes before the markers that followed the span or closed
the row, so a removal round trips. set_cell_vertical_merge only writes
the property and removes no cell, so it keeps its own validation.

Tests, table unit issue446_every_cell_consuming_edit_shares_one_rule over
fifteen cell contents and five entry points, legacy hMerge cells and
row-level markers through span, omissions and their restoration,
test_formatting_tables issue446 grid span and merge
Harness, unchanged, samples use the unchecked span builder
The rdocx and rdocx-cli archives grow with the issue 446 changes and their
tests, on main eb980e9. They are the only crates whose member bytes
change. Both rows take the measurement date 2026-11-16.

Tests, python3 scripts/readme_doctests.py
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