Brain Structural Validation / Validate Markdown Structure & Links (push) Failing after 8s
69 lines
4.8 KiB
Markdown
69 lines
4.8 KiB
Markdown
---
|
||
type: journal
|
||
date: 2026-09-26
|
||
tags:
|
||
- brain
|
||
- tooling
|
||
- validation
|
||
- python
|
||
- architecture
|
||
- mermaid
|
||
---
|
||
|
||
## Wins
|
||
|
||
### validate-brain.py: Mermaid Parse Error Caught and Fixed
|
||
- Discovered that the validator was silently passing a broken Mermaid diagram in `projects/magnetflix/README.md`. Obsidian reported a parse error (`got 'PS'`) on the `|gRPC Event Stream (SSE)|` edge label — parentheses in an unquoted Mermaid label tokenize as Paren Start in Mermaid's Jison parser.
|
||
- Root cause: two bugs in Check 7 — the arrow regex only matched `-->` (missing `<-->`, `<==>`) and the special character check omitted `()[]{}` despite the code comment mentioning parens.
|
||
- Fixed the validator and quoted three edge labels across two files:
|
||
- `projects/magnetflix/README.md`: `|"gRPC Event Stream (SSE)"|`
|
||
- `projects/homelab/guides/mail-deliverability-rdns-relay-guide.md`: `|"Encrypted TLS (587)"| ` and `|"Port 25 (In & Out)"|`
|
||
|
||
### validate-brain.py: Three-Phase Architectural Refactor
|
||
Following feedback from two architectural review sources, executed a three-phase refactor of the validator without breaking the repository or the pre-commit hook.
|
||
|
||
**Phase 1 — Structural refactor (same validator, different architecture):**
|
||
- Introduced `Document` NamedTuple `(path, rel, content, validates_frontmatter)` as the unit of discovery
|
||
- Introduced `Issue` NamedTuple `(severity, path, line, message)` with `Severity = Literal['error', 'warning']`
|
||
- Replaced 4× `rglob` traversals with a single `discover_documents()` pass that reads each file once and encodes all exemption policy
|
||
- Eliminated global mutable `errors: list[str]` — all checks return `list[Issue]`
|
||
- Eliminated module-scope execution — wrapped in `main()` with `raise SystemExit(main())`
|
||
- Removed `any('Deprecated' in e for e in errors)` smell — per-check pass/fail derived from returned lists
|
||
- Added explicit check registry tuple in `main()` — the manifest of what validation means is visible at a glance
|
||
- Separated `report()` function, preserving the door to `--format json` later
|
||
- **Zero behavioral change**: same exit code, same error messages
|
||
|
||
**Phase 2 — Precise diagnostics:**
|
||
- Added `_line_of(content, pos)` helper converting character offsets to 1-indexed line numbers
|
||
- Every check now populates `Issue.line`:
|
||
- Frontmatter: exact line of the `type:` field
|
||
- Links: exact line of each markdown link target span
|
||
- Code fences: line of the last unpaired backtick delimiter
|
||
- Mermaid: structural issues anchored to diagram header, edge-label issues anchored to exact line
|
||
- `report()` renders `path:line message` — path embedding removed from message strings
|
||
|
||
**Phase 3 — Shared Markdown scanner:**
|
||
- Added `CodeBlock` NamedTuple `(language, content, start_line, end_line)`
|
||
- Added `scan_code_blocks(content)` — linear walker respecting CommonMark variable-width backtick fences. A 4-backtick outer fence is not closed by a 3-backtick inner line.
|
||
- `check_code_fences()` now consumes `unclosed` from the scanner — each unclosed fence is a separate Issue
|
||
- `check_mermaid()` now consumes `blocks` from the scanner — filters `language == 'mermaid'`, eliminating the DOTALL regex false-positive (example mermaid syntax inside a ````markdown documentation fence)
|
||
- Verified with 6 integration tests including the false-positive case
|
||
|
||
All three commits passed the pre-commit hook (the validator validated itself on each commit).
|
||
|
||
## Architecture & Design Decisions
|
||
|
||
### validate-brain.py: Where to Stop
|
||
Two feedback sources disagreed on scope. The conservative refactor (Phase 1 + functions) was immediately actionable. The architectural redesign had accurate diagnoses but over-prescribed: `ValidationConfig` dataclass, multi-module package split, PyYAML, Mermaid CLI. Rejected all of these. The guiding principle: architecture should be proportional to the problem. At 138 documents and 1 author, a single 300-line file with clear separation of discovery, validation, and reporting is the right stopping point. Add a dependency when the repo has a requirement the current implementation cannot reliably satisfy — not because a more sophisticated architecture could use it.
|
||
|
||
### Phase Sequencing as a Regression Boundary
|
||
Separating the refactor into three phases with explicit behavioral contracts made each phase auditable:
|
||
- Phase 1: architecture changes, behavior preserved (same exit code, same error messages)
|
||
- Phase 2: diagnostics explicitly improved (line numbers added)
|
||
- Phase 3: Markdown parsing behavior explicitly changed (where old behavior was demonstrably wrong)
|
||
This meant any regression could be attributed to exactly one phase.
|
||
|
||
## Records
|
||
- [validate-brain.py Refactor — Architecture Discussion](../discussions/2026-09-26-validate-brain-refactor-architecture.md)
|
||
- [validate-brain.py Refactor — Debug/Postmortem](../debug/2026-09-26-validate-brain-refactor.md)
|