---
estimated_steps: 18
estimated_files: 3
skills_used: []
---

# T01: Add BodySection schema models and citation validation utility

Add BodySubSection and BodySection Pydantic models to schemas.py. Change SynthesizedPage.body_sections from dict to list[BodySection]. Add body_sections_format field. Create a small citation validation utility (validate_citations) that checks [N] markers in section content against a moment count. This utility is reused by the test harness in T03.

## Steps

1. Read `backend/pipeline/schemas.py` fully.
2. Add `BodySubSection` model with `heading: str` and `content: str` fields.
3. Add `BodySection` model with `heading: str`, `content: str`, and `subsections: list[BodySubSection]` (default_factory=list).
4. Change `SynthesizedPage.body_sections` type from `dict` to `list[BodySection]` with updated description.
5. Add `body_sections_format: str = Field(default='v2')` to `SynthesizedPage`.
6. Create `backend/pipeline/citation_utils.py` with:
   - `extract_citations(text: str) -> list[int]` — regex extracts all `[N]` and `[N,M]` markers, returns sorted unique indices.
   - `validate_citations(sections: list[BodySection], moment_count: int) -> dict` — returns `{valid: bool, total_citations: int, invalid_indices: list[int], uncited_moments: list[int], coverage_pct: float}`.
7. Add unit tests in `backend/pipeline/test_citation_utils.py` covering: valid citations, out-of-range indices, multi-citation markers `[0,2]`, no citations, empty sections.

## Must-Haves

- [ ] BodySubSection and BodySection models exist with correct field types
- [ ] SynthesizedPage.body_sections is list[BodySection]
- [ ] body_sections_format field defaults to 'v2'
- [ ] extract_citations parses [N] and [N,M] markers correctly
- [ ] validate_citations returns coverage stats and flags invalid indices
- [ ] Unit tests pass

## Inputs

- ``backend/pipeline/schemas.py` — existing schema file with SynthesizedPage model`

## Expected Output

- ``backend/pipeline/schemas.py` — updated with BodySection, BodySubSection models and modified SynthesizedPage`
- ``backend/pipeline/citation_utils.py` — citation extraction and validation utilities`
- ``backend/pipeline/test_citation_utils.py` — unit tests for citation utilities`

## Verification

cd /home/aux/projects/content-to-kb-automator && python -m pytest backend/pipeline/test_citation_utils.py -v
