[bug] metadata.json drops per-source chapter heading samples

Author: hydraxmanCreated Sep 24, 2026Updated Sep 24, 2026

What happened

extract_single_file() returns chapter_headings_sample, but the CLI omits that field from every entry in metadata.json.sources. Only the consolidated top-level sample survives. With two sources containing 12 numbered chapters each, that top-level sample contains the first source's first 10 headings; the second source's headings cannot be audited from the metadata.

@wgm66 identified this separate omission in the discussion of #236. This issue isolates that serialization defect; it does not propose a chapter-selection algorithm change.

What you expected

Each sources[i].chapter_headings_sample should preserve the list already returned by extraction, including an empty list when that is the existing result. This makes source-level chapter reports inspectable without changing detection or generation behavior.

Source document

  • Format: synthetic Markdown, no copyrighted material.
  • Pages / size: no pages; 12 short numbered chapters per file.
  • Language: English, with a separate CJK-heading regression.
  • Extractor: built-in plain-text parser; no optional package needed.

Repro

Create alpha.md and beta.md, each with numbered headings from 1 to 12, using different titles:

markdown
# Chapter 1: Alpha 1
Body for this chapter.

# Chapter 2: Alpha 2
Body for this chapter.

Continue through Chapter 12, and use Beta for the second file. Run:

bash
BOOK_SKILL_WORKDIR="$PWD/repro-output" python3 scripts/extract.py alpha.md beta.md --mode text --install-missing no

Current: both sources objects have chapters_detected and chapters_method, but no chapter_headings_sample key. Direct extraction produces distinct 10-heading lists for both sources.

Dev Design

The main() metadata projection in book_to_skill/utils.py copies the count, method and ToC flag but drops the sample. Copy the existing sample into the same per-source object. Recomputing it would be redundant; substituting the consolidated sample would attach the wrong source's headings.

This is an additive JSON field. Existing fields and values remain unchanged, and no dependency or migration is needed. Removing that field reverts the change.

Non-goals: structural selection, Part shortcuts, chapter counts, global sample aggregation, sample limits, SKILL.md, or the other defects in #236/#237. Structural samples that are currently empty remain empty.

Test plan and evidence

A real CLI subprocess regression on 80ae087784ddbc21dbbfde355fe5509631e0e322 exercises single-source, multi-source, reversed source order, empty samples, and Unicode. All five cases fail with KeyError: 'chapter_headings_sample' after confirming the source extractor already returned the expected lists. No extractor or serialization mocks are used.

Run the same regression after the projection fix, followed by pytest -q, ruff check ., and skill validation.

Acceptance criteria

  • Every serialized source retains its own existing sample, including empty lists and Unicode.
  • The existing 10-heading bound is preserved independently for each source.
  • Counts, methods, top-level detection and extracted text retain their existing behavior.
  • Focused CLI regressions and repository checks pass.

Environment

  • OS: macOS
  • Python: 3.11.15
  • book-to-skill: 1.4.0, commit 80ae087784ddbc21dbbfde355fe5509631e0e322

Verification update

Implemented in #245 at b27360ccfc2f92e0a6571dac03e32389a6e4cfe4. The same five CLI regressions pass; the full Python 3.11.15 suite reports 695 passed and 5 optional-dependency skips. Ruff, skill validation and diff checks pass. The hosted Python 3.9–3.13 matrix, smoke, lint, security, dependency review, template/title checks and CodeQL have all passed. No chapter-selection or counting algorithm changed.

Source: virgiliojr94/book-to-skill