We Injected a Bookmark Cycle Into a PDF and Merged It: pypdf Untied the Loop and Every Row Landed Where the Offset Said

The numbers that define today's run: 4 rows in, 4 out, all_pages_match = true in both cycle shapes — plus the one earlier bench that silently dropped the entire cyclic source and still read a pass. Today we tested what a dangerous outline — one whose /Next pointer loops back on a node already visited — does when a cyclic source is merged into a clean one. Two cycle shapes went on the bench, both injected into source A's depth-2 row (A Doc Section): the sub-row's /Next pointed back at its own parent, and at the outline root itself. Source B is a clean 3-page/2-row file, so the offset formula puts B at +3. A walker that followed these /Next chains naively would crash or loop forever — during bench setup the day-36-style read-back produced a None page number and died on it, which is why today's read-back sits on the depth-stack method with a None-page guard. The honest edge of the run comes first: on the first bench attempt the injected cycle never reached the saved file at all, the merge read 4 of 4 rows matching on a cycle-free "cyclic" source, and we rebuilt the bench so the cycle is provably on disk before any merge. All numbers below come only from the hardened build.

The two cycle shapes (written first, run second)

Source A carries 3 pages and 2 rows: a depth-1 A Doc Chapter (page 1) and a depth-2 A Doc Section (page 2). The injection rewrites A Doc Section's /Next, so a walker that follows /Next past the tree's end re-enters visited territory:

loop-1: sub.Next = parent (A Doc Chapter) · loop-2: sub.Next = outline-root (/Outlines)

Before any merge, the saved cyclic source is read back at the raw dictionary level, bypassing the high-level walker: the targeted row is A Doc Section and its /Next points at the parent's dictionary (loop-1) or the /Outlines root (loop-2). Only a run whose raw check shows the loop actually on disk counts as evidence; a raw check showing no /Outlines in the root — exactly what the first bench produced — is recorded as a false pass and rebuilt.

The bench rebuild — from a false-pass cycle to a hardened one

The first merged file read back 4 of 4 rows matching. That result was discarded before it reached this page. Reading the saved "cyclic" source at the raw level showed the root carried no /Outlines at all — the cycle never reached disk, because that bench's source-saving step used a page-copy writer that drops outline structures by design. The hardened bench writes sources with clone_from (structures preserved), proves the cycle exists on disk via the raw check tables below, and only then merges.

A+cyclic / B+clean, read back step by step — loop-1 (sub.Next = parent)

Merged outline row (verbatim)depthsourcepageexpected (offset)text matchpage_match
A Doc Chapter1A (cyclic source)11yestrue
A Doc Section2A (cyclic source)22yestrue
B Doc Chapter1B (clean source)44yestrue
B Doc Section2B (clean source)55yestrue

Cycle persistence proof, read back from the saved cyclic source before merging:

Targeted rowInjectionRaw /Next read-back (target head)pypdf merge warning
A Doc SectionA-sub.Next -> A-top (parent)/Title': 'A Doc Chapter'}pypdf logged 'Detected cycle in outlines.' ×1 during merge

A+cyclic / B+clean, read back step by step — loop-2 (sub.Next = outline root)

Merged outline row (verbatim)depthsourcepageexpected (offset)text matchpage_match
A Doc Chapter1A (cyclic source)11yestrue
A Doc Section2A (cyclic source)22yestrue
B Doc Chapter1B (clean source)44yestrue
B Doc Section2B (clean source)55yestrue

Cycle persistence proof, read back from the saved cyclic source before merging:

Targeted rowInjectionRaw /Next read-back (target head)pypdf merge warning
A Doc SectionA-sub.Next -> outline-root{'/Type': '/Outlines', '/Count': 1, '/First': IndirectObject(14, 0, 140466122746688), '/Last': IndirectObject(14, 0, 140pypdf logged 'Detected cycle in outlines.' ×2 during merge
Both cycle shapes survive to disk, pypdf unties the loop during the merge, and all four rows land where the +3 offset put them.
A · 3p · 2 rows + cycle B · 3p · 2 rows (clean) offset +3 loop: sub.Next back to parent / root Merged read-back (both loops): A Doc Chapter d1 p1 ✓ · A Doc Section d2 p2 ✓ (cycle untied by pypdf merge) B Doc Chapter d1 p4 ✓ · B Doc Section d2 p5 ✓ — expected 3+0+1 / 2+3 pypdf logs 'Detected cycle in outlines.' — then migrates a clean tree

Quick FAQ

Do bookmark cycles survive a pypdf merge?

In our run, no — and that is the outcome you want. pypdf detects the loop ("Detected cycle in outlines."), unties it, and migrates a clean tree. Every row landed at its offset-predicted page, in both loop shapes.

What should never pass on this kind of run?

A "cyclic" source whose cycle never reached disk. Our first bench attempt produced a 4-of-4 pass on exactly that file; the raw-dictionary check caught it, and this page reports that false pass as part of the result instead of hiding it.

What about level-3 cycles, or a cycle in the clean source instead?

Not run. The two injected shapes above are exactly what was executed; everything else stays on the not-run list until it is run on this bench.

What we could NOT test — the list stays honest

- Cycles injected into a level-3 row, or into nested children beyond depth 2 — not run. Reason: today's bench shapes sub-rows at depth 2 only.

- Cross-document cycles created by the merge step itself (two sources whose outlines reference each other's objects) — not run. Reason: today's cycle is injected into one source before the merge, not created during it.

- Cycles injected into the clean source B so the cyclic side sits at the +3 offset — not run. Reason: both runs put the cycle in A, offset 0, on purpose.

- Other tools: the merge under test is pypdf append; no other library ran today.

Decision rule this run supports

- When a cyclic source is merged, treat the untying as the expected outcome and verify every surviving row against its offset-predicted page — 4 of 4 held in both shapes.

- Before trusting any "cycle survived to disk" claim, read the raw /Next of the injected row back from the saved file — the raw dict is what caught our empty-outlines false pass.

- A bookmark /Next cycle is not the same as a broken parent chain: today's loops were untied harmlessly; the failure mode to watch next is the one that produces depth mismatches.

This page pays the cycle-promise left on the nested-merge run, on the same toolchain as the nested split and split-by-bookmarks runs, and the same depth-stack discipline as the day-36 nesting work. Bookmark pdf2mdtool.com — the level-3-cycle and cross-document-cycle runs stay queued until executed.