PDF TO MD EXECUTED TEST

Level-4 Chains and Three Cycle Shapes vs a 4-Level Merge: Only the True Chain Break Lost Rows, Every Cycle Came Back Whole

Run 2026-11-28 · PyMuPDF source build · pypdf 6.20.0 merge · 3 sources × 4 pages × 4 outline levels · 5 loops · 0 guessed numbers · every number below read back from the saved files, not asserted.

This is the fifth article in our outline-series. Level-2 rows survived a merge, a cyclic outline was untied, a broken level-2 chain lost its row, and a level-3 break erased the deepest row silently. This one runs two deeper break/cycle shapes side by side: what happens to a level-4 subtree when the chain above it is cut — and whether three different cycle shapes actually survive a 4-level merge.

Construction

Three sources, each 4 pages, each with a 4-level outline:

Clean merge benchmark: 12 rows, four levels, every row on its own page. Merged A + B + C via pypdf append, offsets A+0 / B+4 / C+8. Five loops, each injected into source A (or B for the L4 dangling test) and proven on disk before merging.

All five breaks/cycles were proven on disk first

Same honest rule as the earlier tests: a loop only counts as evidence if the injected state is readable back from the saved file via a raw (un-dereferenced) read. One method note we caught ourselves: our first probe used a dereferencing getter and reported the cycles as not persisted when they were on disk the whole time — the raw /Next 17 0 R entry was right there in the saved objects. We fixed the probe, not the data. Raw read-back after the fix:

LoopInjectedPersisted proof (raw read-back of saved file)
chain-break-l4A Section /First+/Last → NULLsection_first=NullObject, section_last=NullObject — A Sub and A Sub-Sub both unreachable
dangling-last-l4A Sub /Last → IndirectObject(9999)sub_last=9999, read warning “Object 9999 0 not defined”
self-cycle-l2A Section /Next → A Sectionsection_next_raw → IndirectObject(18) = A Section itself
pair-cycle-l2A Section ↔ A Sub-Sub mutual /Nextsection_next_raw → A Sub-Sub, sub_next_raw → A Section
cycle-chainbackA Section /Next → A Chaptersection_next_raw → A Chapter (the parent one level up)

Result: one true chain break kills two rows; every cycle survives whole

The chain-break loop (10 of 12 rows, both missing rows named):

TitleDepthPageExpectedpage_match
A Chapter111 (A+0)true
A Section222 (A+0)true
A SubMISSING — expected_depth 3, expected_page 3
A Sub-SubMISSING — expected_depth 4, expected_page 4
B Chapter155 (B+4)true
B Section266 (B+4)true
B Sub377 (B+4)true
B Sub-Sub488 (B+4)true
C Chapter199 (C+8)true
C Section21010 (C+8)true
C Sub31111 (C+8)true
C Sub-Sub41212 (C+8)true

bookmarks_out = 10 of 12, all_pages_match = false, missing_rows named both A-side rows with their expected depth and page — never repaired. Every surviving row’s page_match is true against its offset-adjusted expectation, and its landing page contains the source-page marker text.

The other four loops (dangling-last-l4, self-cycle-l2, pair-cycle-l2, cycle-chainback) all came back 12/12 rows, every page_match true, all_pages_match true. Notably, the dangling /Last pointer at level-4 did not kill the Sub-Sub row — pypdf rebuilt the chain from the surviving siblings. And all three cycle shapes, including a pair-cycle between a level-2 and a level-4 node, were untangled cleanly by the merge.

What this adds to the series conclusion

The pattern across the series is now explicit: a broken parent chain is the only shape that silently erases rows, and the erase zone grows with depth — a level-2 cut removed the level-3 row in the earlier test, and a level-2 cut here removed both the level-3 and level-4 rows beneath it. Dangling pointers and cycles, no matter how tangled, get resolved. The checker that matters is still the same one: walk the benchmark and the result side by side, and report what the merge dropped by name, depth, and expected page.

Files (all read back from disk, not asserted)

Honest limits — what we did NOT test