Enforce Correct Citation Spacing with Mechanical Agent
New pipeline agent that mechanically enforces correct markdown citation spacing — no LLM, pure regex. Fixes the recurring problem of clumped, duplicated, and improperly spaced citations that affect Obsidian rendering, HTML/PDF exports, and readability.
Changelog - 2026-03-23_04
Enforce Correct Citation Spacing with Mechanical Agent
New pipeline agent that mechanically enforces correct markdown citation spacing — no LLM, pure regex. Fixes the recurring problem of clumped, duplicated, and improperly spaced citations that affect Obsidian rendering, HTML/PDF exports, and readability.
Why
Citation spacing issues have been a recurring problem across multiple pipeline iterations. LLM agents produce citations with inconsistent spacing — claim.[^1] instead of claim. [^1], [^3][^3] instead of [^3], [^1][^2] instead of [^1] [^2]. These issues affect:
Obsidian rendering: Obsidian's footnote parser is sensitive to spacing
HTML/PDF exports: Pandoc interprets spacing inconsistencies differently
Readability: Clumped citations are hard to distinguish
The rules are well-documented in context-v/reminders/Extended-Markdown-Citation-System-Syntax.md and context-v/reminders/Citation-Reminders.md but were never enforced mechanically. Every time we fix spacing in one agent, another agent reintroduces it. A dedicated mechanical agent that runs after assembly is the correct solution.
What Changed
New Agent: citation_spacing.py
Purely mechanical (no LLM). Runs after toc and before validate_citations in the pipeline. Enforces five rules via regex:
| Rule | Before | After |
| Space before citation after punctuation | claim.[^1] | claim. [^1] |
| Space between consecutive citations | [^1][^2] | [^1] [^2] |
| Deduplicate consecutive citations | [^3] [^3] | [^3] |
| Definitions start at column 0 | [^1]: text | [^1]: text |
| One space after colon in definitions | [^1]:text | [^1]: text |
# Core function signature
def fix_citation_spacing(content: str) -> str:
"""Fix all citation spacing issues in markdown content."""The agent processes both the final draft (7-*.md) and individual section files (2-sections/*.md) for consistency.
Pipeline position:
... → assemble_citations → toc → fix_citation_spacing → validate_citations → ...Verification
Tested on Metabologic v0.2.4 final draft: found and fixed 12 spacing issues, primarily duplicate consecutive citations ([^3] [^3] → [^3]) and missing spaces before citations after punctuation (Ozempic.[^3] → Ozempic. [^3]).
Files Changed
| File | Change |
src/agents/citation_spacing.py | Created — Mechanical citation spacing fixer (5 regex rules) |
src/workflow.py | Added fix_citation_spacing node + edge after toc, before validate_citations |