bindery-cli 0.58.0__tar.gz → 0.60.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/AGENTS.md +27 -7
  2. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/PKG-INFO +1 -1
  3. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/patchnotes.md +91 -0
  4. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/pyproject.toml +1 -1
  5. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/roadmap.md +2 -0
  6. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/spec.md +29 -4
  7. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/src/bindery/__init__.py +1 -1
  8. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/src/bindery/cli.py +17 -0
  9. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/src/bindery/epub.py +211 -0
  10. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/src/bindery/validate.py +33 -7
  11. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_epub.py +254 -0
  12. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_flags_real_core.py +29 -0
  13. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_flags_wiring.py +1 -0
  14. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_validate.py +64 -6
  15. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/uv.lock +1 -1
  16. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/.github/FUNDING.yml +0 -0
  17. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/.github/workflows/ci.yml +0 -0
  18. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/.github/workflows/publish.yml +0 -0
  19. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/.gitignore +0 -0
  20. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/LICENSE +0 -0
  21. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/OBSERVE-RESEARCH.md +0 -0
  22. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/README.md +0 -0
  23. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/docs/screenshots/library-sweep.png +0 -0
  24. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/logo.svg +0 -0
  25. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/plugin/__init__.py +0 -0
  26. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/project.done +0 -0
  27. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/run_tests.sh +0 -0
  28. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/scripts/FastSweep.java +0 -0
  29. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/scripts/build_plugin.py +0 -0
  30. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/scripts/fast_sweep.py +0 -0
  31. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/src/bindery/__main__.py +0 -0
  32. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/src/bindery/audit.py +0 -0
  33. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/src/bindery/library.py +0 -0
  34. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/src/bindery/observe.py +0 -0
  35. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/src/bindery/pagination.py +0 -0
  36. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/src/bindery/reserialize.py +0 -0
  37. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/src/bindery/transforms.py +0 -0
  38. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/src/bindery/watermark.py +0 -0
  39. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_audit.py +0 -0
  40. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_cli.py +0 -0
  41. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_comment_hyphens.py +0 -0
  42. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_contamination.py +0 -0
  43. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_cover_tocdrift.py +0 -0
  44. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_help.py +0 -0
  45. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_library.py +0 -0
  46. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_matcher_hardening.py +0 -0
  47. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_observe.py +0 -0
  48. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_pagination.py +0 -0
  49. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_plugin.py +0 -0
  50. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_reserialize.py +0 -0
  51. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_space_renames.py +0 -0
  52. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_spine_guard.py +0 -0
  53. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_transforms.py +0 -0
  54. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_version.py +0 -0
  55. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_watermark.py +0 -0
  56. {bindery_cli-0.58.0 → bindery_cli-0.60.0}/tests/test_xml_hardening.py +0 -0
@@ -30,13 +30,13 @@ Born from the 2026 library audit (see the user memory `calibre-library-epubcheck
30
30
  `xmlns`, bare `&`, named entities, void self-closing), the NCX pipeline, and the
31
31
  mimetype fix (added/normalized/first-stored, epub.py's archive rewrite); every core
32
32
  fix must render identically to the author's intent: never add, remove, or reorder
33
- visible content. The canonical opt-in inventory lives in spec.md: twenty-five structural
33
+ visible content. The canonical opt-in inventory lives in spec.md: twenty-six structural
34
34
  repairs, four lossy strips, four safe opt-ins, plus `--reserialize`.
35
35
  * **Structural repairs** (`--fix-empty-body`, `--fix-missing-title`, `--fix-id-colons`,
36
36
  `--fix-page-map`, `--strip-epub3-attrs`, `--downgrade-epub3-tags`,
37
37
  `--unwrap-block-in-inline`, `--strip-invalid-value`, `--unwrap-illegal-tags`,
38
38
  `--prune-missing-resources`, `--declare-missing-items`, `--strip-broken-anchors`,
39
- `--encode-url-spaces`,
39
+ `--prune-dangling-ncx-targets`, `--encode-url-spaces`,
40
40
  `--fix-container`, `--fix-media-types`, `--fix-cover`,
41
41
  `--fix-comment-double-hyphen`, `--fix-svg-dup-ids`, `--fix-duplicate-ids`,
42
42
  `--fix-cdata-terminator`,
@@ -129,6 +129,25 @@ Born from the 2026 library audit (see the user memory `calibre-library-epubcheck
129
129
  `--fix-media-types` (font/* blessed on both package versions, magic
130
130
  confirmed, `.eot` never touched) with the flag accepted under `no_worse`
131
131
  (its gain sits on the INFO axis).
132
+ The v0.59.0 one (issue #23, the dangling-NCX class): `--prune-dangling-ncx-targets`
133
+ removes navPoint/pageTarget/navTarget elements whose `<content src>` names a
134
+ document absent from the archive (RSC-007 at toc.ncx; the OPF never declared the
135
+ dropped file, so `--prune-missing-resources` has no item to prune and the navPoint
136
+ survived `--all` as "no applicable fixes"). Node-exact rule over a balanced
137
+ stack scanner (`strip_ncx_dangling_targets`, quote-aware possessive token regex,
138
+ navTarget legacy spelling included): a dead node whose whole subtree dies with it
139
+ drops as one span; a dead node above surviving descendants is SPLICED (tags plus
140
+ its own navLabel/dead `<content>` header go, children re-homed one level up --
141
+ dropping live navigation to repair a dead label is no repair); scheme URLs,
142
+ bare fragments, contentless nodes, and comment-hidden elements are never this
143
+ class; the token walk tracks the navMap/pageList containers too, so an
144
+ unbalanced walk (stray close, wrapper crossing a node's span) aborts AND a
145
+ prune that would empty a container refuses (the NCX wants navPoint+/pageTarget+;
146
+ an empty navMap is itself RSC-005 per epubcheck 5.3, so the gate would otherwise
147
+ apply a TOC-less NCX). When it fires, the
148
+ contract-gated playOrder pass re-runs in the same branch (the survivors' values
149
+ start gapped; the fixture went 0f/1e/0w -> 0f/0e/0w in one gate, one navPoint
150
+ pruned, 389 playOrder values re-sequenced); normal `gate`.
132
151
  They alter markup structure or fabricate minimal content. v0.14–v0.16 ran these unconditionally, which broke this rule;
133
152
  v0.17.0 restored it. `--unwrap-illegal-tags` additionally protects any illegal-tag
134
153
  name that an EPUB stylesheet styles as an element selector (`css_protected_tags`,
@@ -172,8 +191,9 @@ Born from the 2026 library audit (see the user memory `calibre-library-epubcheck
172
191
  (their gain is invisible to epubcheck, so it only forbids a regression, never demands
173
192
  a measured improvement). The structural opt-ins go through the normal `gate` (their
174
193
  gain IS visible, so a run with no measurable improvement is a noop and nothing is
175
- applied) except the two whose gains sit on axes epubcheck does not measure:
176
- `--fix-cover` and `--encode-url-spaces`, accepted under the same `no_worse` bar with
194
+ applied) except the three whose gains sit on axes epubcheck does not measure:
195
+ `--fix-cover`, `--encode-url-spaces`, and `--fix-media-types` (the font half,
196
+ v0.56.0), accepted under the same `no_worse` bar with
177
197
  the partial rule intact. One masked-axis rule (v0.51.0, issue #9, for the
178
198
  NCX sync; generalized v0.57.0, issue #22, to every fired fix): when the gate
179
199
  answers noop on a book that started AND stayed fatal, `process_book`
@@ -207,17 +227,17 @@ Born from the 2026 library audit (see the user memory `calibre-library-epubcheck
207
227
  - `src/bindery/watermark.py`: the opt-in lossy watermark strip (anchored and anchorless signature removal).
208
228
  - `src/bindery/reserialize.py`: structural repair via `html5lib`.
209
229
  - `src/bindery/audit.py`: read-only body-text audits (`content`, `pagenumbers`, `emptytext`, `ocr`, `monolithic` since v0.21.0 with `--max-doc-chars N`, and `completeness` since v0.36.0) behind the `audit` subcommand (v0.15.0). Since v0.18.0 library mode resolves EPUB paths through `cquarry.get_format_path()` and can apply a tag to flagged books via `cquarry.write.WritableCalibreDB` (only with explicit `--tag`; the only sanctioned write path). Since v0.19.0 `audit --id BOOK_ID` audits a single library book through cquarry's single-entity `get_book()` fetch (no library-wide cache; supports `--tag`; incompatible with the directory argument; comma lists since v0.23.0). Since v0.22.0 every archive entry is fully read (CRC + decompression) before analysis (a damaged entry reports its own CORRUPT verdict instead of feeding emptytext) and manifest/NCX references to absent files classify as `convention` (bloated ToC, consecutive span) or `fragment` (broken span). Hazard: the scan loop reuses `tag` as its per-book display column, so the `--tag` argument is captured as `audit_tag` at function entry; do not collapse them again. Since v0.29.0 `audit --json FILE` writes per-file analyzer verdicts in the `library --json` shape (directory, library, and single-book modes; with `--id`, exactly one id; emptytext omitted from a record when the archive verdict owns the body-text story). Since v0.44.0 two more advisory analyzers ride the single pass: `cover` (the EPUB3 `properties~="cover-image"` slice of the ruled hybrid: dangling EPUB2 meta, EPUB3 declaration, cover-file presence) and `tocdrift` (the NCX navMap vs EPUB3 nav toc structural diff, fragments included; audit-only, ToC synthesis out of charter permanently); the loader now exposes the parsed OPF, the NCX text, and the nav HTML on `Book`. Since v0.50.0 a ninth analyzer, `contamination` (issue #8, the judge-and-import class, advisory by contract like completeness/cover/tocdrift, never moves the exit code): foreign-title template stamps (head `<title>` repeats or a volume/book marker with named words in front of it, judged against the OPF's own title/creator/publisher/series with a marker-stripped comparison; a part label whose marker leads never flags), fan-bundle ad signatures (jnovels, kokoro connect, zerobooks, a light-novel plug), and review-paste fingerprints (strong flags alone, weak needs corroboration) in the first five content docs; its corpus is spine PLUS `Book.extra_docs` (the archive's non-spine html, retained by the loader in archive order under a 64 MiB cap, because a broken manifest can leave the junk entirely off the spine: Hothouse). Since v0.50.0 `run_directory`/`run_single` dispatch analyzers through `_dir_dispatch` (one entry per ALL key; the old if/elif chain's `else` ran ocr for cover/tocdrift in those modes since v0.44.0) and `tests/test_contamination.py` pins the map's key set to ALL. Since v0.36.0 the completeness analyzer (Phase 15) reports the phase-1 spot-check per book: spine/prose-doc counts (prose = >= 400 visible chars), first/middle/last prose-doc opening/closing excerpts, trailing-ToC classification (`_trailing_toc`: short link lines dominate, no paragraph-length blocks), and the unreadable fraction. It is ADVISORY by contract (problem always False; ADVISORY on a trailing ToC or unreadable fraction >= 10%), never moves the exit code, and is skipped like emptytext when the archive verdict owns the book. Since v0.38.0 the archive verdict distinguishes font obfuscation from DRM (`OBFUSCATION_ALGOS`: IDPF 2008/embedding + both Adobe URIs; readable obfuscated entries are the benign OBFUSCATED advisory, an unreadable one is CORRUPT, and only non-obfuscation algorithms give ENCRYPTED). Since v0.43.0 untrusted metadata XML is parsed through `_safe_xml_parse` (DOCTYPE/ENTITY declarations refused before xml.etree sees them; the stdlib shape of defusedxml's entity protection) and single entries above `_MAX_ENTRY_BYTES` (32 MB) are refused before decompression; a refused metadata file returns the shell book. Since v0.40.0 the vir-tui console renderer loads through the module-level `_LazyUI` proxy (a plain `import vir_tui as ui` at module level would make 3.12/3.13 installs unimportable now that the stack is marker-gated; LOAD_GLOBAL never consults module `__getattr__`, so a proxy object is the lazy shape that works).
210
- - `src/bindery/epub.py`: archive rewrite, NCX uid sync, RepairReport, mismatch detection, and the opt-in structural-repair plumbing (including the CSS precondition scan). Since v0.58.0 (issue #18) the copy loop hands every `zout.writestr` a PRIVATE ZipInfo (`copy.copy(item)`, a fresh one on a rename) and the media-type `peek` catches `zipfile.BadZipFile` too. HAZARD: `writestr` STAMPS the ZipInfo it is handed with the DESTINATION offsets (header_offset, sizes land on it for the central directory); sharing the source's info repointed zin's own name lookups into the output file mid-loop, and one drifted probe killed whole books as "unreadable" -- 56 books "Bad magic number for file header" plus the "possible zip bomb" Development as Freedom (the drifted offset landed on a valid header whose data overlaps its neighbor), all one root cause. Since v0.58.0 (issue #21) `strip_broken_guide_fragments` gives the OPF guide the NCX fragment-strip's document-precision fallback, and the NCX branch re-runs `fix_ncx_playorder` after the anchor pass fires (the pre-strip sequence goes stale when fragments collapse onto their document). Since v0.58.0 (issue #20) `_resolve_href` decodes the five predefined XML entities plus numeric
230
+ - `src/bindery/epub.py`: archive rewrite, NCX uid sync, RepairReport, mismatch detection, and the opt-in structural-repair plumbing (including the CSS precondition scan). Since v0.59.0 (issue #23) `strip_ncx_dangling_targets` is the NCX dangling-target prune (`--prune-dangling-ncx-targets`): a balanced stack scanner over navPoint/navTarget/pageTarget tokens (quote-aware possessive regex, `_NcxTargetNode` tree, iterative post-order survival pass; header_end delimits the node's own navLabel+`<content>` so a splice removes the tags AND the dead header while the children re-home; an unbalanced walk aborts the part unchanged) and the NCX branch re-runs `fix_ncx_playorder` when it fires, alongside the anchor pass's own re-run. Since v0.58.0 (issue #18) the copy loop hands every `zout.writestr` a PRIVATE ZipInfo (`copy.copy(item)`, a fresh one on a rename) and the media-type `peek` catches `zipfile.BadZipFile` too. HAZARD: `writestr` STAMPS the ZipInfo it is handed with the DESTINATION offsets (header_offset, sizes land on it for the central directory); sharing the source's info repointed zin's own name lookups into the output file mid-loop, and one drifted probe killed whole books as "unreadable" -- 56 books "Bad magic number for file header" plus the "possible zip bomb" Development as Freedom (the drifted offset landed on a valid header whose data overlaps its neighbor), all one root cause. Since v0.58.0 (issue #21) `strip_broken_guide_fragments` gives the OPF guide the NCX fragment-strip's document-precision fallback, and the NCX branch re-runs `fix_ncx_playorder` after the anchor pass fires (the pre-strip sequence goes stale when fragments collapse onto their document). Since v0.58.0 (issue #20) `_resolve_href` decodes the five predefined XML entities plus numeric
211
231
  character references before the fragment split (attribute text arrives
212
232
  escaped; entries are raw; decoding first keeps the `#` inside `&#38;`
213
233
  from cutting the target), `_ITEM_TAG_RE`/`_SPINE_ITEM_RE`/`_GUIDE_REF_RE`/prune_dangling_edges' matcher accept an optional ns prefix (`<opf:item>`), and the renamed-reference spellings percent-encode `[`/`]` (`_encode_ref_path`). Since v0.52.0 (issue #10): `spine_dangles(src)` walks itemref -> manifest item -> archive entry through `_resolve_href`+`_norm_path` (None on an unreadable package: the veto needs evidence, never fires on a probe failure), and `prune_missing_manifest_items` derives its spine protection from the text it prunes (no snapshot parameter: fix_manifest_ids renames ids ahead of it, and the existence set it checks against must be the layout its hrefs are written in — `present_src` when space renames are active, matching the content-doc prune). Since v0.46.0 the opt-in selection is the `RepairFlags` dataclass (one field per flag, all off by default; the all-off instance IS the default pass): `repair_epub(src, dst, flags)` and `process_book(..., flags)` take one object instead of a ~25-kwarg signature, the EPUB2-targeted fixes' version gate swaps in a `dataclasses.replace` copy (never mutating the caller's instance), and the plugin's bare `repair_epub(src, dst)` call constructs exactly the all-off default pass. Since v0.55.0 (issue #16) the RSC-008 half lives beside the prune: `collect_undeclared_resources` (one whole-book census: content img/link, inline `<style>` url(), stylesheet url(), OPF guide refs) + `declare_missing_manifest_items` (sorted `bindery-dN` items before `</manifest>`, `_DECLARE_MEDIA_TYPES` blessed extensions only), gated and threaded through `RepairFlags.declare_missing`. Since v0.38.0/v0.39.0 (Phase 16): `generate_container` + the `--fix-container` gateway (insert after mimetype when missing, replace in-loop when stale; constant-epoch timestamp keeps bytes deterministic), `prune_dangling_edges` (prune_missing_manifest_items returns the pruned ids and the edges that pointed at them (spine@toc, media-overlay, fallback, EPUB2 cover meta) are rewritten so pruning cannot manufacture the regression the gate rejects), and `fix_manifest_media_types` (extension map + magic-byte confirmation via a `peek` callable over the open source zip; jpg/png/gif only), and `fix_cover_meta` (the ruling's deterministic half: dangling EPUB2 cover meta re-pointed from the guide's cover reference when exactly one manifest item carries that file, removed otherwise; accepted under no_worse like the lossy strips because cover wiring is invisible to epubcheck).
214
- - `src/bindery/validate.py`: epubcheck wrapper, the `gate` (improvement) and `no_worse` (no-regression, for the lossy strips) acceptance bars. Since v0.55.0 (issue #15) the default oracle budget is size-scaled (`oracle_timeout_for`: 300s at or under 10 MB, +60s per started 10 MB above, 1800s cap), one subprocess retry follows a timed-out attempt (the daemon path's subprocess fallback is that attempt), and only a double timeout raises `OracleTimeout`, which the gate surfaces as `(epubcheck timeout)` instead of `(epubcheck failed)` (the sweep selectors catch it and keep the book a candidate).
234
+ - `src/bindery/validate.py`: epubcheck wrapper, the `gate` (improvement) and `no_worse` (no-regression, for the lossy strips) acceptance bars. Since v0.55.0 (issue #15) the default oracle budget is size-scaled (`oracle_timeout_for`: 300s at or under 10 MB, +60s per started 10 MB above, 1800s cap), one subprocess retry follows a timed-out attempt (the daemon path's subprocess fallback is that attempt), and only a double timeout raises `OracleTimeout`, which the gate surfaces as `(epubcheck timeout)` instead of `(epubcheck failed)` (the sweep selectors catch it and keep the book a candidate). Since v0.60.0 (issue #24) the size term counts decimal MB (10,440,245 bytes is 9.96 binary MiB, so the book the docs call 10.4 MB computed the flat 300s base with no size chunk), and the budget also scales on archive entry count (+60s per started 500 entries above 1,000; a path that does not open as a zip falls back to size-only): the Summa Theologica is entry-count-bound, not bytes-bound (3,831 entries / 3,808 spine docs; measured 384s source and 445s candidate), and now computes 720s.
215
235
  - `src/bindery/library.py`: Calibre walk, atomic replace, backups, and native format installation. Since v0.19.0 `CalibreIdResolver` resolves the book id from `metadata.db` through cquarry (one lazy path→id map per run; since v0.20.0 the map comes from `CalibreDB.format_path_index()`, re-normalized with `resolve().lower()` for the resolver's symlinked-directory and case-insensitive matching); the `(id)` directory-name regex is only the no-catalog fallback. Since v0.24.0 `install_format()` places the repaired file atomically and updates the `data` row through `cquarry.write.WritableCalibreDB` (since v0.31.0 via `set_format`, cquarry 1.17's sanctioned remove+add in one transaction); the external `calibredb` CLI is gone. Since v0.32.0 a guessed id drives a row update only when metadata.db corroborates it (`_verify_guess`: books row exists, file in the book's own directory, catalogued `data.name` when a row exists); anything else saves in place and leaves the catalog untouched: never reintroduce directory-name guessing as the primary source: renamed/mismatched directories would replace the wrong book. Since 2026-09-15 `make_backup` takes the opt-in `--backup-keep N` ring: at most N backup files per book, the author original `.bak` never deleted, newest content at the highest name; default remains an unbounded rotation.
216
236
  - `src/bindery/cli.py`: `repair` and `library` subcommands, including `--all` and `--install-to-calibre`; since v0.52.0 `process_book` carries the spine-resolution veto (issue #10) above every verdict path, promotions included: a candidate whose spine dangles more than the source's is `reject` with `(spine collapse)` appended to the summary, whatever the counts say. `repair --json` emits the library per-book record vocabulary (2026-09-15; since v0.45.0 every per-book record, the phase1 payload included, also carries the structured fix breakdown: the `fixes` dict plus `ncx_uid_synced`/`watermark_refusals` off the `Outcome`, and `_phase1_decisions` reads that data instead of substring-matching the rendered `summary`, which stays for human eyes; CalibreQuarry's lossy-consent mirror is the waiting consumer, floor `bindery>=0.45.0`); since v0.29.0 also the `run` verbs: `phase1` (pre-import vetting over loose files: the audit battery composed with one gated repair sweep; read-only until `--apply-lossy`, which IS the lossy-strip consent) and `phase3` (the scoped post-import apply step: `library --id --sweep --only all --apply --all --install-to-calibre` plus a pre/post summary; mechanically refuses unscoped sweeps with exit 2; since 2026-09-15 the summary sums applied/equal states only and carries rejected projections as `rejected_projection`). Plus `bindery doctor` (2026-09-15): the environment self-check that imports none of the VirInvictus stack, never raises, and always exits 0. The `observe` verb (v0.53.0) writes schema v2 artifacts since v0.55.0 (issue #13): `codes.messages` (renamed from the mislabeled `occurrences`) counts aggregated epubcheck JSON message entries, `codes.occurrences` now carries the true per-site scale (summed `locations`), a `counting` block states both scales in the artifact, and `--against` accepts v1 or v2 (the trend keys exist in both). Both run verbs take `--non-interactive` and surface open questions as `decisions_needed` in their JSON, never prompts. They drive the shipped `run_library` through the real parser, so no flag can drift between the verb and the subcommand it wraps. Since v0.46.0 `cli._flags_from_args(args)` is the one place the argparse names map onto `RepairFlags` fields (each `or --all`), shared by `run_library` and `run_repair`; the old per-verb ~25-kwarg blocks are gone. Since v0.47.1 the help is two-level (the --help-repairs reference is
217
237
  custom-rendered and colors itself with the CPython 3.14 argparse theme,
218
238
  gated by argparse's own _colorize detection so the levels always agree) and the repair-flag inventory is one table: `_REPAIR_FLAG_TABLE` (dest + one-line short help + long reference text) registers the compact grouped `--help` (argparse argument groups, hand-short usage line) and `--help-repairs` renders the long descriptions (a custom argparse Action, deliberately NOT store_true so the wiring test's selection count ignores it); adding a flag means adding a table row. Since v0.48.0 the metadata.db doors are enforced: `_require_closed_calibre` (pgrep fail-closed, exit 1) guards `audit --tag` and `library --apply --install-to-calibre` (run phase3 transitively), and `--help-json` (a custom argparse Action like --help-repairs, never colored) dumps the per-verb exit contract and the door list; since v0.48.1 the per-verb exit contracts tell the code's truth (repair: 0 probed/written incl. partial / 1 trouble, never 2; audit: 0/1/2 plus 3 for findings+write-failure; library and the run slices: the unified 0/2/1); since v0.49.0 the single-file verb's no-output form is a probe (the gate runs, the record's `output` is null and `applied` false, nothing is written anywhere; the old implicit `(repaired).epub` sibling was issue #7's import litter in a vetted tree, and writing now needs an explicit DST). `library --workers N` (v0.30.0) parallelizes only the sweep's candidate pass, in windows of N consumed in order so `--limit` stays lazy; the repair phase is never parallel (shared workdir, atomic-replacement contract). Since v0.40.0 `main()` guards the VirInvictus stack: a `ModuleNotFoundError` naming `vir_tui`/`cquarry` (marker-gated to 3.14+ on the 3.12 floor) becomes one stderr line and trouble exit 2 instead of a traceback; anything else missing still raises.
219
239
  - `plugin/__init__.py` + `scripts/build_plugin.py`: the Bindery Repair Calibre plugin (v0.37.0; identity `Bindery Repair` / `bindery_repair`, approved and built per the scoped spec in roadmap.md Phase 3). The builder vendors `transforms.py`, `epub.py`, `pagination.py`, `watermark.py`, `reserialize.py` byte-identically into the zip (the zip root is a package, so their relative imports resolve unchanged; the suite's drift test pins the equality) with the version tuple substituted from the single-source VERSION; `publish.yml` attaches `BinderyRepair-v<VERSION>.zip` to each release. The plugin runs ONLY the default pass (epubcheck cannot gate inside Calibre), never raises, is byte-idempotent (zero fixes returns the original path), testzip()-verifies its output, logs one line per book, and takes JSON config via `site_customization` (`log`, `log_path`, `max_size_mb` default 150, `max_log_mb` rotation cap default 2, `epubcheck_path` experimental validation default OFF). Load it in tests through the `tests/test_plugin.py` stubs: the calibre namespace is installed in sys.modules and the package loads with a two-dir `__path__` (plugin dir first, then src/bindery) exactly like calibre's zipplugin loader shape. **Plugin/interpreter compatibility (v0.40.0):** v0.39.0 shipped PEP 758 unparenthesized except tuples, and the plugin died with a bare SyntaxError on every released Calibre (7.x and the whole 8.x series embed Python 3.11, 9.0+ embeds 3.14; from calibre's bypy sources.json, checked against the upstream clone). The core is now grammar-pinned: parenthesized except tuples everywhere, `ruff`'s `target-version = "py312"` pin (inference from requires-python would rewrite the parens back off), `tests/test_version.py`'s parse guard, and CI's `plugin-compat` job byte-compiling the built zip under 3.11/3.12/3.13/3.14. `minimum_calibre_version` is (7, 0, 0): the oldest series whose interpreter CI covers, replacing the hollow (2, 0, 0); `supported_platforms` stays `["linux"]` (developed and tested on Linux only). Any new syntax in the vendored modules must clear the guard before it ships.
220
- - `tests/`: transforms (including the shared duplicate-id rename), end-to-end repair, atomic replace, pagination, watermarks, the audit analyzers (content/pagenumbers/emptytext/ocr/monolithic/completeness, archive integrity, spine classification, contamination since v0.50.0), the library sweep and `CalibreIdResolver`, validate, reserialize, CLI wiring, the version pin and the syntax-floor grammar guard, and the Calibre plugin (build drift + run behavior under calibre stubs). Since the v0.46.0 follow-ups: `tests/test_flags_wiring.py` reflectively pins the RepairFlags inventory to the argparse dests and `_flags_from_args` mapping (a flag added without a field, a field without a flag, or a remapped line fails there), the all-off defaults, and the version gate's caller-immutability contract (test_epub's `TestVersionGateKeepsFlagsIntact`); `tests/test_comment_hyphens.py` carries the RSC-016 fix; `tests/test_flags_real_core.py` drives every repair flag from argv through the unmocked core (--no-validate; the gate itself stays pinned by the mocked-oracle tests and runs for real wherever epubcheck exists). `tests/test_contamination.py` (v0.50.0) carries the three contamination sweeps, the advisory contract, the `_dir_dispatch` key pin, and the cover-routing regression. The v0.46.0 trio (test_flags_wiring, test_comment_hyphens, test_flags_real_core) rides CI's core-compat 3.12/3.13 leg; the hand-listed module list there must gain an entry for every new stack-free test module (audit-side test modules like test_contamination stay on the 3.14 leg by convention). The v0.55.0 batch (issues #11-#16): `tests/test_validate.py` pins the size-scaled timeout and the retry-then-OracleTimeout contract, `tests/test_observe.py` the v2 line format (`CODE:messages:occurrences`), the renamed codes block, the counting envelope, and v1 `--against` acceptance, `tests/test_epub.py::TestDeclareMissingItems` the RSC-008 declarer (sorted deterministic items, blessed media types only, id collisions prefixed, schemes/fragments/spaces/declared skipped), `tests/test_transforms.py::TestFixDuplicateIds` the content-doc rename, and `tests/test_flags_real_core.py` drives `--fix-duplicate-ids`, `--declare-missing-items`, and the extended `--strip-epub3-attrs` set through the real argv-to-fix path.
240
+ - `tests/`: transforms (including the shared duplicate-id rename), end-to-end repair, atomic replace, pagination, watermarks, the audit analyzers (content/pagenumbers/emptytext/ocr/monolithic/completeness, archive integrity, spine classification, contamination since v0.50.0), the library sweep and `CalibreIdResolver`, validate, reserialize, CLI wiring, the version pin and the syntax-floor grammar guard, and the Calibre plugin (build drift + run behavior under calibre stubs). Since the v0.46.0 follow-ups: `tests/test_flags_wiring.py` reflectively pins the RepairFlags inventory to the argparse dests and `_flags_from_args` mapping (a flag added without a field, a field without a flag, or a remapped line fails there), the all-off defaults, and the version gate's caller-immutability contract (test_epub's `TestVersionGateKeepsFlagsIntact`); `tests/test_comment_hyphens.py` carries the RSC-016 fix; `tests/test_flags_real_core.py` drives every repair flag from argv through the unmocked core (--no-validate; the gate itself stays pinned by the mocked-oracle tests and runs for real wherever epubcheck exists). `tests/test_contamination.py` (v0.50.0) carries the three contamination sweeps, the advisory contract, the `_dir_dispatch` key pin, and the cover-routing regression. The v0.46.0 trio (test_flags_wiring, test_comment_hyphens, test_flags_real_core) rides CI's core-compat 3.12/3.13 leg; the hand-listed module list there must gain an entry for every new stack-free test module (audit-side test modules like test_contamination stay on the 3.14 leg by convention). The v0.55.0 batch (issues #11-#16): `tests/test_validate.py` pins the size-scaled timeout and the retry-then-OracleTimeout contract, `tests/test_observe.py` the v2 line format (`CODE:messages:occurrences`), the renamed codes block, the counting envelope, and v1 `--against` acceptance, `tests/test_epub.py::TestDeclareMissingItems` the RSC-008 declarer (sorted deterministic items, blessed media types only, id collisions prefixed, schemes/fragments/spaces/declared skipped), `tests/test_transforms.py::TestFixDuplicateIds` the content-doc rename, and `tests/test_flags_real_core.py` drives `--fix-duplicate-ids`, `--declare-missing-items`, and the extended `--strip-epub3-attrs` set through the real argv-to-fix path. The v0.59.0 addition (issue #23): `tests/test_epub.py::TestPruneDanglingNcxTargets` pins the node-exact prune rule (dead leaf dropped, dead parent spliced above live children, dead subtree one span, scheme/bare-fragment/contentless/comment-hidden untouched, escaped-ampersand src resolves, pageTarget and legacy navTarget covered, wrapper-crossing and unclosed walks abort, a wholesale-dead container refuses) plus the end-to-end prune+resequence, and `tests/test_flags_real_core.py` drives `--prune-dangling-ncx-targets` through argv. The v0.60.0 addition (issue #24): `tests/test_validate.py` pins the decimal-MB size term (exactly 10 MiB earns its 360s chunk) and the entry term (the 3,831-entry shape at <= 10 MB computes 660s; the full Summa shape computes 720s; exactly 1,000 entries stays at the size budget; 1,001 adds the first chunk; 2,500 computes 540s).
221
241
 
222
242
  ## Conventions
223
243
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: bindery-cli
3
- Version: 0.58.0
3
+ Version: 0.60.0
4
4
  Summary: Audit EPUB body text and repair broken EPUBs with deterministic, epubcheck-gated fixes; atomic in-place replacement in a Calibre library.
5
5
  Project-URL: Homepage, https://github.com/VirInvictus/bindery-cli
6
6
  Project-URL: Repository, https://github.com/VirInvictus/bindery-cli
@@ -1,5 +1,96 @@
1
1
  # bindery-cli Patch Notes
2
2
 
3
+ ## v0.60.0 (2026-11-01)
4
+
5
+ ### Issue #24: the oracle budget also scales on archive entry count
6
+
7
+ The Summa Theologica (Calibre book #7301: 10,440,245 bytes, 3,831
8
+ archive entries, 3,808 spine documents, a 16,456-finding playOrder NCX)
9
+ kept dying at the gate even after v0.55.0's size-scaled budget, and the
10
+ first miss was the units: the size term divides by binary MiB, and
11
+ 10,440,245 bytes is 9.96 MiB, so the book every doc reports as 10.4 MB
12
+ computed the flat 300s base with no size chunk at all. Even with a size
13
+ chunk it would have missed: epubcheck measured 384s on the source and
14
+ 445s on the `--all` candidate (both runs under sweep contention), because
15
+ the book's cost is entry-count-bound (3,831 entries, 3,808 spine
16
+ documents), not bytes-bound. Either way the retry burned the same way
17
+ and every sweep misfiled the book as `(epubcheck timeout)` -- it stayed
18
+ a candidate forever.
19
+
20
+ Two budget changes, same 1800s cap. The size term counts decimal MB
21
+ (how book sizes are actually reported), so exactly-10-MiB files earn
22
+ their chunk instead of straddling the threshold. And the budget gains an
23
+ entry term shaped like the size term at finer granularity: +60s per
24
+ started 500 archive entries above the first 1,000. The Summa computes
25
+ 720s (300 base + 60 size + 360 entries) against its measured 445s worst
26
+ case; books at or under 1,000 entries and 10 MB keep their historical
27
+ budget exactly, and a path that does not open as a zip (the budget is
28
+ computed before the book is judged) falls back to the size term alone.
29
+ No gate or CLI surface changes; the sweep selectors and the OracleTimeout
30
+ contract are untouched, and no library dry-run re-validation is
31
+ triggered (the change is the oracle's patience, not an acceptance bar).
32
+
33
+ The diagnosis also settled what the repair buys: epubcheck DOES police
34
+ the playOrder contract (2 of the source's 6 RSC-005 error entries are
35
+ exactly that: identical playOrder values for different targets, and
36
+ playOrder sequence gaps), so the default-pass candidate measures
37
+ 0f/6e -> 0f/4e, a strict error decrease the gate accepts; the scoped
38
+ `--all` candidate measures 0f/0e/0w. Measured pins live in
39
+ tests/test_validate.py: exactly 10 MiB earns its decimal chunk (360s),
40
+ the 3,831-entry shape at <= 10 MB computes 660s, the full Summa shape
41
+ (10.44 decimal MB + 3,831 entries) computes 720s, exactly 1,000 entries
42
+ stays at the size budget, 1,001 entries adds the first chunk, and 2,500
43
+ entries compute 540s.
44
+
45
+ ## v0.59.0 (2026-10-28)
46
+
47
+ ### Issue #23: the dangling NCX navPoint, pruned and re-sequenced in one gate
48
+
49
+ `--prune-dangling-ncx-targets` is the twenty-sixth structural repair. The
50
+ class: an NCX navPoint (or pageTarget) whose `<content src>` names a document
51
+ the archive never contained. epubcheck answers RSC-007 from toc.ncx, and
52
+ nothing reached it: `--prune-missing-resources` prunes manifest items whose
53
+ FILE is absent, and a producer that drops the document without declaring it
54
+ leaves no item to prune, so the navPoint survived `--all` with "no applicable
55
+ fixes" (the Splintered Harmony fixture, 0f/1e/0w). Hand removal works but
56
+ cascades: every surviving playOrder then reads as starting at 2 (391 RSC-005s
57
+ on the live book), naive renumbering splits the navPoint/pageTarget
58
+ same-target contract (79 more), and the working rescue was a second `--all`
59
+ run to let the contract-gated playOrder pass heal the survivors. The new flag
60
+ folds that whole recipe into one gate.
61
+
62
+ The rule is node-exact, over a balanced stack scanner that tracks navPoint,
63
+ the legacy navTarget spelling, and pageTarget as one family: a navPoint dies
64
+ when its OWN target document is absent from the archive. A dead node whose
65
+ whole subtree dies with it is removed as one element; a dead node above
66
+ surviving descendants is spliced (its tags, its navLabel, and its dead
67
+ `<content>` go; the children are re-homed one level up), because dropping
68
+ live navigation to repair a dead label is no repair. Scheme URLs, bare
69
+ fragments, contentless nodes, comment-hidden elements, and escaped-ampersand
70
+ srcs that resolve (the issue #20 decoder) are never this class. The token
71
+ walk tracks the navMap/pageList containers too: an unbalanced walk (a stray
72
+ close, a wrapper crossing a node's span) aborts the pass unchanged, and so
73
+ does a prune that would EMPTY a container, because the NCX requires
74
+ navPoint+/pageTarget+ and an empty navMap is itself one RSC-005 (verified
75
+ against epubcheck 5.3): without the rule, the gate would happily apply a
76
+ TOC-less NCX, and a wholly dead TOC is the manual list's finding, not a
77
+ repair. When the prune fires, the
78
+ contract-gated playOrder pass re-runs in the same branch, the fragment
79
+ strip's v0.58.0 pattern. On the fixture the one run went 0f/1e/0w to
80
+ 0f/0e/0w: one navPoint pruned, 389 playOrder values re-sequenced across
81
+ navMap and pageList, epubcheck clean, and a second probe reports no
82
+ applicable fixes.
83
+
84
+ Wiring follows the house pattern: the `RepairFlags.prune_ncx_targets` field,
85
+ the `_REPAIR_FLAG_TABLE` row (compact `--help` and `--help-repairs`), the
86
+ `_flags_from_args` mapping, `--all` enabled like every structural flag, and
87
+ the wiring test's dest-to-field pin. Tests: `TestPruneDanglingNcxTargets`
88
+ pins the node-exact rule end to end (dead leaf, splice, dead subtree,
89
+ refusals, the unbalanced abort, the container-emptying refusal, the
90
+ escaped-ampersand resolve, pageTarget and navTarget, and the resequence
91
+ through `repair_epub`), and the real-core
92
+ suite drives the flag through argv. Suite: 820 tests.
93
+
3
94
  ## v0.58.0 (2026-10-26)
4
95
 
5
96
  ### Issues #18-#21: the structural dry run's four rejects, read to the root
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "bindery-cli"
7
- version = "0.58.0"
7
+ version = "0.60.0"
8
8
  description = "Audit EPUB body text and repair broken EPUBs with deterministic, epubcheck-gated fixes; atomic in-place replacement in a Calibre library."
9
9
  readme = "README.md"
10
10
  classifiers = [
@@ -52,6 +52,8 @@ The plugin vendors the core and never gates (the epubcheck latency ruling).
52
52
  | 0.56 | The boxed recipes, built same-day on the follow-up call: `--fix-unique-identifier` (the OPF-030 recipe: wire the package's named id onto the first dc:identifier, refuses every ambiguous shape, and predicts the post-wiring uid so the NCX sync the broken wiring was masking fires in the same run -- eagle 2e -> 1e accept) and the CSS-007 font half inside `--fix-media-types` (font/* blessed on both package versions, magic confirmed, .eot never touched; accepted under no_worse because the gain sits on the INFO axis -- Brave New World 8 fonts normalized, CSS-007 cleared) | v0.56.0 |
53
53
  | 0.57 | The embed scar's element half and the end of the silent equal (issue #22): `--fix-unbound-prefix` now binds an unbound ELEMENT prefix inline (the 9.7.0 writer self-declares xmlns:dc on every dc: element but one; rule 2 for a literal dc: element resolves to the DC namespace itself; Hackers 1f/1e -> 0f/1e with both binds in one run, and the half-repaired intermediate that read equal now repairs), and the masked-axis reclassification generalizes from the v0.51.0 NCX sync to every fired fix: a gate noop on a book that started and stayed fatal reads partial (the manual list) instead of a silent equal; the specimen's residual ncx:-prefixed dtb:uid gap shipped as the v0.57.1 patch (both matchers read a prefixed `<ncx:meta>` carrier; Hackers fully clean 0f/0e/0w) | v0.57.0-v0.57.1 |
54
54
  | 0.58 | The structural dry run's four rejects (issues #18-#21), each bisected to the root on its specimens: the private-ZipInfo write contract (issue #18: writestr stamps the info it is handed with the DESTINATION offsets, one shared info repointed zin's own lookups into the output file -- all 57 "unreadable" books, the "possible zip bomb" included, were this one bug; the peek also catches BadZipFile per the issue), bracket-encoded renamed references plus the start-tag/PI anchor on both reference rewriters (issue #19: raw brackets are RSC-020 path-illegal -- IJ 108e -> 22e -- and the unanchored regex corrupted display text that teaches HTML -- Hacking's two RSC-016 fatals), the entity-decoding href resolver and prefix-tolerant OPF matchers (issue #20: `&amp;` hrefs vs raw-`&` entries made prune and declare swap the same items on the D&D four, and `<opf:item>` blindness duplicated whole manifests on the Sony three), and the three #21 mechanisms (guide-fragment strip, the post-anchor playOrder re-run, the comment-blind duplicate-id rename) | v0.58.0 |
55
+ | 0.59 | The dangling-NCX prune (issue #23): `--prune-dangling-ncx-targets` removes navPoint/pageTarget elements whose `<content src>` names a document the archive never contained (RSC-007 the manifest prune cannot reach: no item exists for a file the OPF never declared), node-exact over a balanced stack scanner (dead subtree drops as one span, dead parent spliced above live children, scheme/bare-fragment/comment-hidden/contentless refused, unbalanced walk aborts), with the contract-gated playOrder pass re-run in the same branch -- the hand-surgery-plus-second-run rescue is one gate (fixture 0f/1e/0w -> 0f/0e/0w: one navPoint pruned, 389 playOrder values re-sequenced) | v0.59.0 |
56
+ | 0.60 | The oracle budget scales on decimal MB and archive entry count (issue #24): the Summa Theologica (10,440,245 bytes = 9.96 binary MiB, 3,831 entries, 3,808 spine docs, 16,456 playOrder findings) is entry-count-bound, not bytes-bound -- measured 384s source and 445s candidate against the flat 300s its bytes computed (the binary 10 MiB divisor ate the size chunk; the book the docs call 10.4 MB never earned one) -- so every sweep double-timed-out and misfiled it as `(epubcheck timeout)`; the size term counts decimal MB now, the entry term adds +60s per started 500 entries above 1,000 (same 1800s cap), the book computes 720s, and a non-zip path falls back to size-only. epubcheck does police the playOrder contract (2 of the source's 6 RSC-005 error entries): default-pass candidate measures 0f/6e -> 0f/4e (accept), scoped `--all` candidate 0f/0e/0w | v0.60.0 |
55
57
 
56
58
  ## Open work
57
59
 
@@ -13,12 +13,12 @@ restyle, re-compress, or restructure content, and it does not attempt to fix arb
13
13
  schema (RSC-005) violations, which are usually harmless to readers and not safely
14
14
  mechanizable.
15
15
 
16
- The deliberate exceptions to "semantics-preserving" come in opt-in groups. The twenty-five
16
+ The deliberate exceptions to "semantics-preserving" come in opt-in groups. The twenty-six
17
17
  **structural repairs** (`--fix-empty-body`, `--fix-missing-title`, `--fix-id-colons`,
18
18
  `--fix-page-map`, `--strip-epub3-attrs`, `--downgrade-epub3-tags`,
19
19
  `--unwrap-block-in-inline`, `--strip-invalid-value`, `--unwrap-illegal-tags`,
20
20
  `--prune-missing-resources`, `--declare-missing-items`, `--strip-broken-anchors`,
21
- `--encode-url-spaces`,
21
+ `--prune-dangling-ncx-targets`, `--encode-url-spaces`,
22
22
  `--fix-container`, `--fix-media-types`, `--fix-cover`, `--fix-comment-double-hyphen`,
23
23
  `--fix-svg-dup-ids`, `--fix-duplicate-ids`, `--fix-cdata-terminator`,
24
24
  `--fix-misnested-inline`,
@@ -147,7 +147,7 @@ byte-for-byte. The normal gate applies.
147
147
 
148
148
  ### Opt-in: structural repairs
149
149
 
150
- Twenty-five repairs go past well-formedness and therefore require their own flag; none is ever
150
+ Twenty-six repairs go past well-formedness and therefore require their own flag; none is ever
151
151
  part of the default pipeline:
152
152
 
153
153
  - **`--fix-empty-body`**: `&nbsp;` inside a strictly empty `<body></body>` ("body
@@ -257,6 +257,31 @@ every EPUB3 book in an `--all` sweep taking exactly that damage).
257
257
  spine-integrity report. href values carrying a scheme no reader resolves
258
258
  (`kindle:embed:`, `file:`, ...) are stripped under the same flag; the resolvable set
259
259
  is fixed (`http`, `https`, `mailto`) and extends only with a named finding.
260
+ - **`--prune-dangling-ncx-targets`** (v0.59.0, issue #23): remove NCX
261
+ `navPoint`/`pageTarget` elements whose `<content src>` names a document the archive
262
+ does not contain (RSC-007 raised from toc.ncx). This is the class the prune flag
263
+ cannot reach (`--prune-missing-resources` prunes manifest items whose FILE is
264
+ absent; a producer that drops the document without declaring it leaves no item and
265
+ the navPoint survived `--all` as "no applicable fixes") and the class hand removal
266
+ breaks (every surviving playOrder reads as starting at 2, and naive renumbering
267
+ splits the navPoint/pageTarget same-target contract -- the live case ended at
268
+ 391 RSC-005s before the rescue). The rule is node-exact: a navPoint dies when its
269
+ OWN target document is absent; a dead node whose whole subtree dies with it is
270
+ removed as one element, and a dead node above surviving descendants is spliced (its
271
+ tags, label, and dead `<content>` go; the children are re-homed one level up),
272
+ because dropping live navigation to repair a dead label is no repair. Scheme URLs,
273
+ bare fragments, comment-hidden elements, and contentless nodes are never touched,
274
+ and the token walk tracks the navMap/pageList containers: an unbalanced walk (a
275
+ stray close, a wrapper crossing a node's span) aborts the pass unchanged, and so
276
+ does a prune that would EMPTY a container -- the NCX requires navPoint+/pageTarget+
277
+ and trading a wholly dead TOC for no TOC is the manual list's call (an empty navMap
278
+ is itself one RSC-005, verified against epubcheck 5.3, so the gate would happily
279
+ apply the amputation). When the prune fires, the
280
+ always-on playOrder pass re-runs on the final navMap (the same contract-gated
281
+ re-sequencing the fragment strip uses), so the rescue that took hand surgery plus a
282
+ second `--all` run is one gate here. The live specimen went 0f/1e/0w to 0f/0e/0w in
283
+ one run (one navPoint pruned, 389 playOrder values re-sequenced across navMap and
284
+ pageList, epubcheck clean).
260
285
  - **`--encode-url-spaces`**: repairs raw-space URLs two ways. The reference half
261
286
  percent-encodes raw spaces in `src`/`href` attribute values across the package
262
287
  (OPF manifest, NCX `content src`, content documents): a URL with a literal space is
@@ -406,7 +431,7 @@ leftovers whose failing OPF is a stale non-rootfile are unreachable by construct
406
431
  byte-identical. Runs on the OPF only: content documents have the
407
432
  invalid-attribute drop (`--strip-bad-attrs`) for the same defect class.
408
433
 
409
- Twenty-three of the twenty-five are evaluated by the normal `gate`: unlike the lossy strips,
434
+ Twenty-three of the twenty-six are evaluated by the normal `gate`: unlike the lossy strips,
410
435
  their benefit is visible to epubcheck (they clear errors), so a run with no measurable
411
436
  improvement is a noop and nothing is applied. The three exceptions are `--fix-cover`, `--encode-url-spaces`, and
412
437
  `--fix-media-types` (whose gains sit on axes epubcheck's counts do not measure:
@@ -11,7 +11,7 @@ Calibre plugin built from this core.
11
11
  Single source of truth for the version. pyproject.toml must match VERSION below.
12
12
  """
13
13
 
14
- VERSION = "0.58.0"
14
+ VERSION = "0.60.0"
15
15
  __version__ = VERSION
16
16
 
17
17
  from .epub import RepairReport, repair_epub # noqa: E402
@@ -95,6 +95,7 @@ def _flags_from_args(args: argparse.Namespace) -> RepairFlags:
95
95
  prune_missing=args.prune_missing_resources or all_,
96
96
  declare_missing=args.declare_missing_items or all_,
97
97
  strip_anchors=args.strip_broken_anchors or all_,
98
+ prune_ncx_targets=args.prune_dangling_ncx_targets or all_,
98
99
  url_spaces=args.encode_url_spaces or all_,
99
100
  fix_container=args.fix_container or all_,
100
101
  fix_media_types=args.fix_media_types or all_,
@@ -1800,6 +1801,22 @@ _REPAIR_FLAG_TABLE: dict[str, list[tuple[str, str, str]]] = {
1800
1801
  "two fragments onto one document cannot leave the same-target-"
1801
1802
  "shares-one-value contract violated.",
1802
1803
  ),
1804
+ (
1805
+ "prune_dangling_ncx_targets",
1806
+ "remove NCX navPoints/pageTargets whose content src is absent (RSC-007)",
1807
+ "remove NCX navPoint/pageTarget elements whose <content src> names a "
1808
+ "document the archive does not contain (RSC-007; issue #23) -- the "
1809
+ "class --prune-missing-resources cannot reach, because the OPF never "
1810
+ "declared the dropped file, and whose hand removal breaks the "
1811
+ "playOrder contract (every survivor's value reads as starting at 2). "
1812
+ "A dead navPoint whose whole subtree dies with it is removed as one "
1813
+ "element; a dead navPoint above surviving descendants is spliced: "
1814
+ "its tags go and the children are re-homed one level up, because "
1815
+ "dropping live navigation to repair a dead label is no repair. "
1816
+ "When the prune fires, the always-on playOrder pass re-runs on the "
1817
+ "final navMap, so the rescue that took hand surgery plus a second "
1818
+ "--all run is one gate here.",
1819
+ ),
1803
1820
  (
1804
1821
  "encode_url_spaces",
1805
1822
  "encode raw spaces in URLs; rename space-bearing entries (PKG-010)",
@@ -290,6 +290,7 @@ class RepairFlags:
290
290
  prune_missing: bool = False
291
291
  declare_missing: bool = False
292
292
  strip_anchors: bool = False
293
+ prune_ncx_targets: bool = False
293
294
  url_spaces: bool = False
294
295
  fix_container: bool = False
295
296
  fix_media_types: bool = False
@@ -1610,6 +1611,192 @@ def strip_ncx_broken_fragments(
1610
1611
  return outside_protected_map(ncx_text, fix)
1611
1612
 
1612
1613
 
1614
+ # --prune-dangling-ncx-targets (issue #23) token set: navPoint, its legacy
1615
+ # navTarget spelling, and pageTarget share one element family, so the
1616
+ # scanner tracks them together. Quote-aware possessive start tags (the
1617
+ # house matcher: the overlapping [^>] branch makes a plain greedy loop
1618
+ # re-partition exponentially on failing candidates) and the optional
1619
+ # namespace prefix, matching the OPF matchers' issue #20 tolerance.
1620
+ _NCXP_TOKEN_RE = re.compile(
1621
+ r"""<(?:[\w.-]+:)?(navPoint|navTarget|pageTarget)\b(?:"[^"]*"|'[^']*'|[^>])*+>"""
1622
+ r"""|</(?:[\w.-]+:)?(navPoint|navTarget|pageTarget)\s*>"""
1623
+ r"""|<(?:[\w.-]+:)?(navMap|pageList)\b(?:"[^"]*"|'[^']*'|[^>])*+>"""
1624
+ r"""|</(?:[\w.-]+:)?(navMap|pageList)\s*>""",
1625
+ re.IGNORECASE,
1626
+ )
1627
+
1628
+
1629
+ @dataclass
1630
+ class _NcxTargetNode:
1631
+ """One navPoint/navTarget/pageTarget element located in the NCX text.
1632
+
1633
+ Spans index the tags for the edit pass; `header_end` is where the
1634
+ node's own header (navLabel, then the `<content>` that `own_src` came
1635
+ from) ends and the first navPoint-family child or the node's own end
1636
+ tag begins -- the splice edit removes [open_start, header_end) so the
1637
+ dead label and dead target go with the tags while the children stay.
1638
+ The survival pass fills `action`: "drop" removes the whole span,
1639
+ "splice" removes the tags plus that header, None keeps the node."""
1640
+
1641
+ name: str
1642
+ open_start: int
1643
+ open_end: int
1644
+ header_end: int = -1
1645
+ close_start: int = -1
1646
+ close_end: int = -1
1647
+ own_src: str | None = None
1648
+ children: list[_NcxTargetNode] = field(default_factory=list)
1649
+ keeps: bool = True
1650
+ action: str | None = None
1651
+
1652
+
1653
+ def strip_ncx_dangling_targets(
1654
+ ncx_text: str, ncx_dir: str, present: frozenset[str]
1655
+ ) -> tuple[str, int]:
1656
+ """Remove navPoint/pageTarget elements whose `<content src>` names a
1657
+ document the archive does not contain (RSC-007 at toc.ncx, issue #23).
1658
+
1659
+ `--prune-missing-resources` cannot reach this class: the OPF never
1660
+ declared the dropped file, so there is no manifest item to prune and
1661
+ the navPoint survived `--all` ("no applicable fixes"), while hand
1662
+ removal breaks the playOrder contract (every survivor's value reads as
1663
+ starting at 2). The wiring therefore re-runs the contract-gated
1664
+ playOrder pass the moment this fires.
1665
+
1666
+ The rule is node-exact: a navPoint dies when ITS OWN target document is
1667
+ absent. A dead node whose whole subtree dies with it is removed as one
1668
+ span; a dead node above surviving descendants is spliced (its tags,
1669
+ label, and dead `<content>` go, the children are re-homed one level up),
1670
+ because dropping live navigation to repair a dead label is no repair.
1671
+ Scheme URLs (kindle:, https:) and bare-fragment targets are never this
1672
+ class, a node with no `<content>` of its own is left alone. The walk
1673
+ tracks the navMap/pageList containers as tokens too: an unbalanced walk
1674
+ (a stray close, a wrapper crossing a node's span) aborts the pass
1675
+ unchanged, and so does a prune that would EMPTY a container -- the NCX
1676
+ requires navPoint+/pageTarget+ and trading a wholly dead TOC for no TOC
1677
+ is the manual list's call (verified against epubcheck 5.3: an empty
1678
+ navMap is itself one RSC-005, so the gate would happily apply the
1679
+ amputation). This is text surgery over a possibly-broken book; the gate
1680
+ is the safety net behind it."""
1681
+
1682
+ def prune(text: str) -> tuple[str, int]:
1683
+ tokens = list(_NCXP_TOKEN_RE.finditer(text))
1684
+ # top-level family nodes bucket per container; `bare` holds nodes
1685
+ # outside any navMap/pageList (DTD-invalid but seen in the wild)
1686
+ containers: list[tuple[str, list[_NcxTargetNode]]] = []
1687
+ bare: list[_NcxTargetNode] = []
1688
+ stack: list[_NcxTargetNode] = []
1689
+ cur_name: str | None = None
1690
+ cur_bucket: list[_NcxTargetNode] = []
1691
+
1692
+ def bucket() -> list[_NcxTargetNode]:
1693
+ if stack:
1694
+ return stack[-1].children
1695
+ return cur_bucket if cur_name else bare
1696
+
1697
+ for i, m in enumerate(tokens):
1698
+ if m.group(1) is not None: # navPoint/navTarget/pageTarget open
1699
+ if m.group(0).endswith("/>"):
1700
+ # self-closing: complete leaf, no <content> child
1701
+ bucket().append(
1702
+ _NcxTargetNode(
1703
+ name=m.group(1).lower(),
1704
+ open_start=m.start(),
1705
+ open_end=m.end(),
1706
+ )
1707
+ )
1708
+ continue
1709
+ node = _NcxTargetNode(
1710
+ name=m.group(1).lower(), open_start=m.start(), open_end=m.end()
1711
+ )
1712
+ node.header_end = (
1713
+ tokens[i + 1].start() if i + 1 < len(tokens) else len(text)
1714
+ )
1715
+ sm = _NCX_CONTENT_SRC_RE.search(text, node.open_end, node.header_end)
1716
+ node.own_src = sm.group(3) if sm else None
1717
+ stack.append(node)
1718
+ elif m.group(2) is not None: # family close
1719
+ if not stack or stack[-1].name != m.group(2).lower():
1720
+ return text, 0 # unbalanced tree: never guess
1721
+ node = stack.pop()
1722
+ # the parent AFTER the pop: the closed node joins its
1723
+ # parent's children, never its own
1724
+ bucket().append(node)
1725
+ node.close_start, node.close_end = m.start(), m.end()
1726
+ elif m.group(3) is not None: # navMap/pageList open
1727
+ if stack or cur_name is not None:
1728
+ return text, 0 # a container inside a node or container
1729
+ if not m.group(0).endswith("/>"):
1730
+ cur_name, cur_bucket = m.group(3).lower(), []
1731
+ else: # container close
1732
+ if stack or cur_name != m.group(4).lower():
1733
+ return text, 0 # a node still open under the container
1734
+ containers.append((cur_name, cur_bucket))
1735
+ cur_name, cur_bucket = None, []
1736
+ if stack or cur_name is not None:
1737
+ return text, 0 # unclosed element at end of part: same refusal
1738
+
1739
+ # Survival, children before parents (every node lands after its
1740
+ # parent in the pre-order flat list, so the reversed walk is
1741
+ # post-order): a dead node keeps only what survives beneath it.
1742
+ flat: list[_NcxTargetNode] = []
1743
+ work: list[_NcxTargetNode] = list(bare)
1744
+ for _, b in containers:
1745
+ work.extend(b)
1746
+ while work:
1747
+ n = work.pop()
1748
+ flat.append(n)
1749
+ work.extend(n.children)
1750
+ for n in reversed(flat):
1751
+ resolved = _resolve_href(ncx_dir, n.own_src) if n.own_src else None
1752
+ # None covers a missing <content>, an external scheme, and a
1753
+ # bare fragment (it would resolve inside the NCX itself): none
1754
+ # of those is the absent-document class
1755
+ if resolved is None or resolved in present:
1756
+ continue
1757
+ child_keeps = any(c.keeps for c in n.children)
1758
+ n.action = "splice" if child_keeps else "drop"
1759
+ n.keeps = child_keeps
1760
+ for _, b in containers:
1761
+ if b and not any(n.keeps for n in b):
1762
+ return text, 0 # the prune would empty a required container
1763
+
1764
+ tops: list[_NcxTargetNode] = list(bare)
1765
+ for _, b in containers:
1766
+ tops.extend(b)
1767
+ edits: list[tuple[int, int]] = []
1768
+ count = 0
1769
+ work = [(n, False) for n in tops] # (node, inside a dropped span)
1770
+ while work:
1771
+ n, buried = work.pop()
1772
+ if buried or n.action is None:
1773
+ work.extend((c, buried or n.action == "drop") for c in n.children)
1774
+ continue
1775
+ if n.action == "drop":
1776
+ edits.append((n.open_start, n.close_end))
1777
+ count += 1
1778
+ work.extend((c, True) for c in n.children)
1779
+ else:
1780
+ # splice: the tags AND the dead node's own header (its
1781
+ # navLabel and its dead <content>) go; only the children
1782
+ # survive, re-homed one level up
1783
+ edits.append((n.open_start, n.header_end))
1784
+ edits.append((n.close_start, n.close_end))
1785
+ count += 1
1786
+ work.extend((c, False) for c in n.children)
1787
+ if not edits:
1788
+ return text, 0
1789
+ out: list[str] = []
1790
+ pos = 0
1791
+ for start, end in sorted(edits):
1792
+ out.append(text[pos:start])
1793
+ pos = end
1794
+ out.append(text[pos:])
1795
+ return "".join(out), count
1796
+
1797
+ return outside_protected_map(ncx_text, prune)
1798
+
1799
+
1613
1800
  def strip_broken_guide_fragments(
1614
1801
  opf_text: str, opf_dir: str, ids_by_doc: dict[str, frozenset[str]]
1615
1802
  ) -> tuple[str, int]:
@@ -1841,6 +2028,11 @@ def repair_epub(
1841
2028
  target document does not define (RSC-020/RSC-012; NCX navTargets keep the document
1842
2029
  target) and non-resolvable URI schemes (kindle:, file:). Anchor text is always
1843
2030
  preserved byte-for-byte.
2031
+ With `prune_ncx_targets`, remove NCX navPoint/pageTarget elements whose
2032
+ <content src> names a document the archive does not contain (RSC-007; issue #23),
2033
+ re-homing the surviving children of a dead navPoint one level up, and re-run the
2034
+ contract-gated playOrder pass so the survivors' values stay contiguous in the
2035
+ same run.
1844
2036
  With `url_spaces`, repair raw-space URLs two ways: percent-encode raw spaces
1845
2037
  in src/href attribute values across the package (OPF href, NCX src, content
1846
2038
  src/href; a literal space is not a valid URL, RSC-020), and RENAME the
@@ -2037,6 +2229,7 @@ def repair_epub(
2037
2229
  or flags.strip_anchors
2038
2230
  or flags.fix_media_types
2039
2231
  or flags.declare_missing
2232
+ or flags.prune_ncx_targets
2040
2233
  ):
2041
2234
  for n in zin.namelist():
2042
2235
  norm_to_raw.setdefault(_norm_path(space_renames.get(n, n)), n)
@@ -2291,6 +2484,24 @@ def repair_epub(
2291
2484
  counts["fix_ncx_playorder"] = (
2292
2485
  counts.get("fix_ncx_playorder", 0) + n2
2293
2486
  )
2487
+ if flags.prune_ncx_targets:
2488
+ text, n = strip_ncx_dangling_targets(
2489
+ text, posixpath.dirname(out_item.filename), present
2490
+ )
2491
+ if n:
2492
+ counts["ncx_dangling_targets_pruned"] = n
2493
+ # The prune leaves the survivors' playOrder values
2494
+ # gapped (and pageTargets' shared values without
2495
+ # their navPoint anchors): the same contract
2496
+ # violation the fragment strip manufactures. The
2497
+ # pass is contract-gated, so a still-valid NCX
2498
+ # re-runs as a no-op (issue #23's hand-rescue
2499
+ # recipe, one gate).
2500
+ text, n2 = fix_ncx_playorder(text)
2501
+ if n2:
2502
+ counts["fix_ncx_playorder"] = (
2503
+ counts.get("fix_ncx_playorder", 0) + n2
2504
+ )
2294
2505
  synced = False
2295
2506
  sync_uid = uid if uid else wired_uid
2296
2507
  if sync_uid: