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:
- A: A Chapter (depth 1, p1) → A Section (2, p2) → A Sub (3, p3) → A Sub-Sub (4, p4)
- B: same four levels on pages 5–8
- C: same four levels on pages 9–12
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:
| Loop | Injected | Persisted proof (raw read-back of saved file) |
|---|---|---|
| chain-break-l4 | A Section /First+/Last → NULL | section_first=NullObject, section_last=NullObject — A Sub and A Sub-Sub both unreachable |
| dangling-last-l4 | A Sub /Last → IndirectObject(9999) | sub_last=9999, read warning “Object 9999 0 not defined” |
| self-cycle-l2 | A Section /Next → A Section | section_next_raw → IndirectObject(18) = A Section itself |
| pair-cycle-l2 | A Section ↔ A Sub-Sub mutual /Next | section_next_raw → A Sub-Sub, sub_next_raw → A Section |
| cycle-chainback | A Section /Next → A Chapter | section_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):
| Title | Depth | Page | Expected | page_match |
|---|---|---|---|---|
| A Chapter | 1 | 1 | 1 (A+0) | true |
| A Section | 2 | 2 | 2 (A+0) | true |
| A Sub | MISSING — expected_depth 3, expected_page 3 | |||
| A Sub-Sub | MISSING — expected_depth 4, expected_page 4 | |||
| B Chapter | 1 | 5 | 5 (B+4) | true |
| B Section | 2 | 6 | 6 (B+4) | true |
| B Sub | 3 | 7 | 7 (B+4) | true |
| B Sub-Sub | 4 | 8 | 8 (B+4) | true |
| C Chapter | 1 | 9 | 9 (C+8) | true |
| C Section | 2 | 10 | 10 (C+8) | true |
| C Sub | 3 | 11 | 11 (C+8) | true |
| C Sub-Sub | 4 | 12 | 12 (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)
- day42_results.json — five loops: construction, persisted-probes, source/merged read-backs, checks, missing_rows, honest_limits
- day42_merged_chain-break-l4.pdf — merged output with the two-row erasure (10 rows)
- day42_merged_dangling-last-l4.pdf · self-cycle · pair-cycle · chainback — the four 12/12 loops
- day42_A_chain-break-l4.pdf — source A with the level-4 break on disk
Honest limits — what we did NOT test
- Level-5+ depth: untested.
- Simultaneous cycle + chain-break in the same source: untested.
- Cycle at the outline root itself (root /Next pointing into the tree): untested.
- Malformed XREF / cross-reference-stream corruption: untested.
- L4 break in sources B or C: untested — only source A (and one L4 pointer in A) was broken.