infra(validator): phase 1 — structural refactor to single-pass pipeline
Brain Structural Validation / Validate Markdown Structure & Links (push) Successful in 7s
Brain Structural Validation / Validate Markdown Structure & Links (push) Successful in 7s
- Introduce Document NamedTuple (path, rel, content, validates_frontmatter)
as the unit of discovery; all inclusion/exclusion policy lives in
discover_documents() — no check ever calls is_exempt() again.
- Introduce Issue NamedTuple (severity, path, line, message) with
Severity = Literal['error', 'warning']; line field reserved for Phase 2.
- Replace four independent rglob traversals with a single discovery pass
that reads each file once.
- Encapsulate all checks as pure functions returning list[Issue] with no
global state mutation.
- Eliminate the global errors/warnings lists and module-level execution.
- Remove the any('Deprecated' in e ...) reporting smell; per-check
pass/fail now derived from returned Issue lists, not string inspection.
- Add explicit check registry tuple in main() — the manifest of what
validation means is now visible at a glance.
- Separate report() function prepares for --format json without redesign.
- Move MERMAID_TYPES to module-level configuration alongside other constants.
- Behavioral specification preserved: exit 0, same error messages.
- Pyright: 0 errors, 0 warnings.
Also fixes two Mermaid edge label parse errors caught by the improved
validator from the previous session:
projects/magnetflix/README.md: quote |gRPC Event Stream (SSE)|
projects/homelab/guides/mail-deliverability-rdns-relay-guide.md:
quote |Encrypted TLS (587)| and |Port 25 (In & Out)|
This commit is contained in:
1 parent
b3ca28b8ef
commit
ad4d0435ce
3 files changed
+248
-167
No files matched your search
@@ -64,7 +64,7 @@ flowchart LR
|
|||||||
end
|
end
|
||||||
|
|
||||||
Client -->|Submit| Stalwart
|
Client -->|Submit| Stalwart
|
||||||
Stalwart -->|Encrypted TLS (587)| Relay
|
Stalwart -->|"Encrypted TLS (587)"| Relay
|
||||||
Relay -->|Delivers| Gmail
|
Relay -->|Delivers| Gmail
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ flowchart LR
|
|||||||
end
|
end
|
||||||
|
|
||||||
Stalwart <==>|Encrypted WireGuard Tunnel| VPS
|
Stalwart <==>|Encrypted WireGuard Tunnel| VPS
|
||||||
VPS <==>|Port 25 (In & Out)| World
|
VPS <==>|"Port 25 (In & Out)"| World
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Rent a cheap cloud VPS (Hetzner Cloud ~$3.50/mo or RackNerd ~$15/yr) that provides a static IPv4 and allows setting custom rDNS (PTR).
|
1. Rent a cheap cloud VPS (Hetzner Cloud ~$3.50/mo or RackNerd ~$15/yr) that provides a static IPv4 and allows setting custom rDNS (PTR).
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ flowchart TB
|
|||||||
WebUI -->|HTTP POST| Traefik
|
WebUI -->|HTTP POST| Traefik
|
||||||
Traefik -->|HTTP + Remote-User| BFF
|
Traefik -->|HTTP + Remote-User| BFF
|
||||||
BFF -->|gRPC SubmitDownload| Worker
|
BFF -->|gRPC SubmitDownload| Worker
|
||||||
BFF <-->|gRPC Event Stream (SSE)| Worker
|
BFF <-->|"gRPC Event Stream (SSE)"| Worker
|
||||||
Worker --> DB
|
Worker --> DB
|
||||||
Worker -->|RPC| Torrent
|
Worker -->|RPC| Torrent
|
||||||
Torrent --> Staging
|
Torrent --> Staging
|
||||||
|
|||||||
+245
-164
@@ -9,233 +9,314 @@ Checks:
|
|||||||
3. Every .md document (outside exempt paths) has YAML frontmatter.
|
3. Every .md document (outside exempt paths) has YAML frontmatter.
|
||||||
4. Every frontmatter block has a valid `type` field.
|
4. Every frontmatter block has a valid `type` field.
|
||||||
5. Every relative markdown link resolves to an existing file.
|
5. Every relative markdown link resolves to an existing file.
|
||||||
|
6. All markdown code fences are balanced.
|
||||||
|
7. Mermaid diagrams are structurally valid.
|
||||||
|
8. Python code passes Ruff linting.
|
||||||
|
9. Python code passes Pyright type checking.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import pathlib
|
import pathlib
|
||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
from typing import Literal, NamedTuple
|
||||||
|
|
||||||
ROOT = pathlib.Path(__file__).parent.parent.resolve()
|
ROOT = pathlib.Path(__file__).parent.parent.resolve()
|
||||||
|
|
||||||
VALID_TYPES = {
|
# ── Configuration ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
VALID_TYPES: frozenset[str] = frozenset({
|
||||||
"knowledge", "project", "plan", "guide", "decision",
|
"knowledge", "project", "plan", "guide", "decision",
|
||||||
"journal", "discussion", "experiment", "practice", "inbox", "reference",
|
"journal", "discussion", "experiment", "practice", "inbox", "reference",
|
||||||
"agent", "debug",
|
"agent", "debug",
|
||||||
}
|
})
|
||||||
|
|
||||||
DEPRECATED_DIRS = [
|
DEPRECATED_DIRS: tuple[str, ...] = (
|
||||||
"homelab", "journal", "learning", "docs",
|
"homelab", "journal", "learning", "docs",
|
||||||
"knowledge/dsa", "knowledge/python", "knowledge/tools",
|
"knowledge/dsa", "knowledge/python", "knowledge/tools",
|
||||||
"knowledge/devenv", "knowledge/skills",
|
"knowledge/devenv", "knowledge/skills",
|
||||||
]
|
)
|
||||||
|
|
||||||
REQUIRED_ROOT_FILES = ["README.md", "AGENTS.md", "LICENSE"]
|
REQUIRED_ROOT_FILES: tuple[str, ...] = ("README.md", "AGENTS.md", "LICENSE")
|
||||||
|
|
||||||
EXEMPT_PATTERNS = [
|
# Paths (or path substrings) excluded entirely from validation.
|
||||||
|
EXEMPT_PATTERNS: tuple[str, ...] = (
|
||||||
".obsidian",
|
".obsidian",
|
||||||
"inbox",
|
"inbox",
|
||||||
"practice/LeetCode/Solutions",
|
"practice/LeetCode/Solutions",
|
||||||
"scripts",
|
"scripts",
|
||||||
".git",
|
".git",
|
||||||
]
|
)
|
||||||
|
|
||||||
# Root-level files that are navigational/meta and intentionally have no frontmatter
|
# Navigational/meta files exempt from frontmatter and link validation.
|
||||||
FRONTMATTER_EXEMPT_FILES = {
|
# They are still validated for code fences and Mermaid diagrams.
|
||||||
|
FRONTMATTER_EXEMPT_FILES: frozenset[pathlib.Path] = frozenset({
|
||||||
ROOT / "README.md",
|
ROOT / "README.md",
|
||||||
ROOT / "AGENTS.md",
|
ROOT / "AGENTS.md",
|
||||||
ROOT / "inbox" / "README.md",
|
ROOT / "inbox" / "README.md",
|
||||||
ROOT / "agents" / "README.md",
|
ROOT / "agents" / "README.md",
|
||||||
}
|
})
|
||||||
|
|
||||||
errors: list[str] = []
|
MERMAID_TYPES: frozenset[str] = frozenset({
|
||||||
warnings: list[str] = []
|
|
||||||
|
|
||||||
|
|
||||||
def is_exempt(path: pathlib.Path) -> bool:
|
|
||||||
rel = str(path.relative_to(ROOT))
|
|
||||||
return any(ex in rel for ex in EXEMPT_PATTERNS)
|
|
||||||
|
|
||||||
|
|
||||||
# ── Check 1: No deprecated directories ────────────────────────────────────────
|
|
||||||
print("Checking deprecated directories...")
|
|
||||||
for d in DEPRECATED_DIRS:
|
|
||||||
target = ROOT / d
|
|
||||||
if target.exists():
|
|
||||||
errors.append(f"Deprecated directory still exists: {d}/")
|
|
||||||
print(f" {'✓' if not any('Deprecated' in e for e in errors) else '✗'} Done.\n")
|
|
||||||
|
|
||||||
|
|
||||||
# ── Check 2: Required root files ──────────────────────────────────────────────
|
|
||||||
print("Checking required root files...")
|
|
||||||
for f in REQUIRED_ROOT_FILES:
|
|
||||||
if not (ROOT / f).exists():
|
|
||||||
errors.append(f"Missing required root file: {f}")
|
|
||||||
print(f" {'✓' if not any('Missing required' in e for e in errors) else '✗'} Done.\n")
|
|
||||||
|
|
||||||
|
|
||||||
# ── Check 3 & 4: Frontmatter presence and valid type ─────────────────────────
|
|
||||||
print("Checking frontmatter...")
|
|
||||||
fm_errors = 0
|
|
||||||
for md in ROOT.rglob("*.md"):
|
|
||||||
if is_exempt(md):
|
|
||||||
continue
|
|
||||||
if md in FRONTMATTER_EXEMPT_FILES:
|
|
||||||
continue
|
|
||||||
content = md.read_text(errors="replace")
|
|
||||||
rel = md.relative_to(ROOT)
|
|
||||||
if not content.startswith("---"):
|
|
||||||
errors.append(f"Missing frontmatter: {rel}")
|
|
||||||
fm_errors += 1
|
|
||||||
continue
|
|
||||||
# Extract frontmatter block
|
|
||||||
end = content.find("\n---", 3)
|
|
||||||
if end == -1:
|
|
||||||
errors.append(f"Malformed frontmatter (no closing ---): {rel}")
|
|
||||||
fm_errors += 1
|
|
||||||
continue
|
|
||||||
fm_block = content[3:end]
|
|
||||||
type_match = re.search(r'^type:\s*(\S+)', fm_block, re.MULTILINE)
|
|
||||||
if not type_match:
|
|
||||||
errors.append(f"Frontmatter missing 'type' field: {rel}")
|
|
||||||
fm_errors += 1
|
|
||||||
elif type_match.group(1) not in VALID_TYPES:
|
|
||||||
errors.append(f"Invalid type '{type_match.group(1)}': {rel}")
|
|
||||||
fm_errors += 1
|
|
||||||
print(f" {'✓' if fm_errors == 0 else '✗'} Done ({fm_errors} issues).\n")
|
|
||||||
|
|
||||||
|
|
||||||
# ── Check 5: Relative link resolution ─────────────────────────────────────────
|
|
||||||
print("Checking relative links...")
|
|
||||||
link_errors = 0
|
|
||||||
for md in ROOT.rglob("*.md"):
|
|
||||||
if is_exempt(md):
|
|
||||||
continue
|
|
||||||
if md in FRONTMATTER_EXEMPT_FILES:
|
|
||||||
continue
|
|
||||||
content = md.read_text(errors="replace")
|
|
||||||
for m in re.finditer(r'\[([^\]]*)\]\(([^)]+)\)', content):
|
|
||||||
target = m.group(2).split('#')[0].strip()
|
|
||||||
if not target or target.startswith(('http', 'file:', 'mailto:')):
|
|
||||||
continue
|
|
||||||
resolved = (md.parent / target).resolve()
|
|
||||||
if not resolved.exists():
|
|
||||||
rel = md.relative_to(ROOT)
|
|
||||||
errors.append(f"Broken link in {rel}: {target}")
|
|
||||||
link_errors += 1
|
|
||||||
print(f" {'✓' if link_errors == 0 else '✗'} Done ({link_errors} broken links).\n")
|
|
||||||
|
|
||||||
|
|
||||||
# ── Check 6: Code fence balance ───────────────────────────────────────────────
|
|
||||||
print("Checking markdown code fences...")
|
|
||||||
fence_errors = 0
|
|
||||||
for md in ROOT.rglob("*.md"):
|
|
||||||
if is_exempt(md):
|
|
||||||
continue
|
|
||||||
content = md.read_text(errors="replace")
|
|
||||||
rel = md.relative_to(ROOT)
|
|
||||||
fences = [line for line in content.splitlines() if line.strip().startswith("```")]
|
|
||||||
if len(fences) % 2 != 0:
|
|
||||||
errors.append(f"Unclosed code fence in {rel} (found {len(fences)} fence delimiters)")
|
|
||||||
fence_errors += 1
|
|
||||||
print(f" {'✓' if fence_errors == 0 else '✗'} Done ({fence_errors} issues).\n")
|
|
||||||
|
|
||||||
|
|
||||||
# ── Check 7: Mermaid diagram validation ───────────────────────────────────────
|
|
||||||
print("Checking Mermaid diagrams...")
|
|
||||||
mermaid_errors = 0
|
|
||||||
MERMAID_TYPES = {
|
|
||||||
"flowchart", "graph", "sequencediagram", "erdiagram", "classdiagram",
|
"flowchart", "graph", "sequencediagram", "erdiagram", "classdiagram",
|
||||||
"statediagram", "gantt", "pie", "gitgraph", "mindmap", "timeline",
|
"statediagram", "gantt", "pie", "gitgraph", "mindmap", "timeline",
|
||||||
"quadrantchart", "c4context", "zenuml", "sankey-beta", "kanban",
|
"quadrantchart", "c4context", "zenuml", "sankey-beta", "kanban",
|
||||||
"block-beta", "packet-beta", "architecture-beta"
|
"block-beta", "packet-beta", "architecture-beta",
|
||||||
}
|
})
|
||||||
|
|
||||||
for md in ROOT.rglob("*.md"):
|
# ── Types ──────────────────────────────────────────────────────────────────────
|
||||||
if is_exempt(md):
|
|
||||||
|
Severity = Literal["error", "warning"]
|
||||||
|
|
||||||
|
|
||||||
|
class Document(NamedTuple):
|
||||||
|
path: pathlib.Path
|
||||||
|
rel: pathlib.Path
|
||||||
|
content: str
|
||||||
|
validates_frontmatter: bool # False for navigational/meta files
|
||||||
|
|
||||||
|
|
||||||
|
class Issue(NamedTuple):
|
||||||
|
severity: Severity
|
||||||
|
path: pathlib.Path | None
|
||||||
|
line: int | None # reserved for Phase 2 — always None in Phase 1
|
||||||
|
message: str
|
||||||
|
|
||||||
|
|
||||||
|
# ── Discovery ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
def discover_documents(root: pathlib.Path) -> list[Document]:
|
||||||
|
"""Return every Markdown file that requires validation.
|
||||||
|
|
||||||
|
All inclusion/exclusion policy lives here. A Document that reaches a
|
||||||
|
document validator is already known to be one that should be validated.
|
||||||
|
"""
|
||||||
|
documents: list[Document] = []
|
||||||
|
for path in root.rglob("*.md"):
|
||||||
|
rel_str = str(path.relative_to(root))
|
||||||
|
if any(pattern in rel_str for pattern in EXEMPT_PATTERNS):
|
||||||
continue
|
continue
|
||||||
content = md.read_text(errors="replace")
|
documents.append(Document(
|
||||||
rel = md.relative_to(ROOT)
|
path=path,
|
||||||
|
rel=path.relative_to(root),
|
||||||
|
content=path.read_text(errors="replace"),
|
||||||
|
validates_frontmatter=path not in FRONTMATTER_EXEMPT_FILES,
|
||||||
|
))
|
||||||
|
return documents
|
||||||
|
|
||||||
# Find all ```mermaid ... ``` blocks
|
|
||||||
for m in re.finditer(r'```mermaid\s*\n(.*?)\n```', content, re.DOTALL):
|
# ── Repository checks ──────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
def check_deprecated_dirs(root: pathlib.Path) -> list[Issue]:
|
||||||
|
return [
|
||||||
|
Issue("error", root / d, None, f"Deprecated directory still exists: {d}/")
|
||||||
|
for d in DEPRECATED_DIRS
|
||||||
|
if (root / d).exists()
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def check_required_files(root: pathlib.Path) -> list[Issue]:
|
||||||
|
return [
|
||||||
|
Issue("error", root / f, None, f"Missing required root file: {f}")
|
||||||
|
for f in REQUIRED_ROOT_FILES
|
||||||
|
if not (root / f).exists()
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
# ── Document checks ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
def check_frontmatter(doc: Document) -> list[Issue]:
|
||||||
|
if not doc.validates_frontmatter:
|
||||||
|
return []
|
||||||
|
|
||||||
|
content = doc.content
|
||||||
|
if not content.startswith("---"):
|
||||||
|
return [Issue("error", doc.rel, None, f"Missing frontmatter: {doc.rel}")]
|
||||||
|
|
||||||
|
end = content.find("\n---", 3)
|
||||||
|
if end == -1:
|
||||||
|
return [Issue("error", doc.rel, None, f"Malformed frontmatter (no closing ---): {doc.rel}")]
|
||||||
|
|
||||||
|
fm_block = content[3:end]
|
||||||
|
type_match = re.search(r'^type:\s*(\S+)', fm_block, re.MULTILINE)
|
||||||
|
if not type_match:
|
||||||
|
return [Issue("error", doc.rel, None, f"Frontmatter missing 'type' field: {doc.rel}")]
|
||||||
|
|
||||||
|
doc_type = type_match.group(1)
|
||||||
|
if doc_type not in VALID_TYPES:
|
||||||
|
return [Issue("error", doc.rel, None, f"Invalid type '{doc_type}': {doc.rel}")]
|
||||||
|
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
def check_links(doc: Document) -> list[Issue]:
|
||||||
|
if not doc.validates_frontmatter:
|
||||||
|
return []
|
||||||
|
|
||||||
|
issues: list[Issue] = []
|
||||||
|
for m in re.finditer(r'\[([^\]]*)\]\(([^)]+)\)', doc.content):
|
||||||
|
target = m.group(2).split('#')[0].strip()
|
||||||
|
if not target or target.startswith(('http', 'file:', 'mailto:')):
|
||||||
|
continue
|
||||||
|
resolved = (doc.path.parent / target).resolve()
|
||||||
|
if not resolved.exists():
|
||||||
|
issues.append(Issue("error", doc.rel, None, f"Broken link in {doc.rel}: {target}"))
|
||||||
|
return issues
|
||||||
|
|
||||||
|
|
||||||
|
def check_code_fences(doc: Document) -> list[Issue]:
|
||||||
|
fences = [line for line in doc.content.splitlines() if line.strip().startswith("```")]
|
||||||
|
if len(fences) % 2 != 0:
|
||||||
|
return [Issue(
|
||||||
|
"error", doc.rel, None,
|
||||||
|
f"Unclosed code fence in {doc.rel} (found {len(fences)} fence delimiters)",
|
||||||
|
)]
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
def check_mermaid(doc: Document) -> list[Issue]:
|
||||||
|
issues: list[Issue] = []
|
||||||
|
for m in re.finditer(r'```mermaid\s*\n(.*?)\n```', doc.content, re.DOTALL):
|
||||||
block = m.group(1).strip()
|
block = m.group(1).strip()
|
||||||
if not block:
|
if not block:
|
||||||
errors.append(f"Empty Mermaid diagram block in {rel}")
|
issues.append(Issue("error", doc.rel, None, f"Empty Mermaid diagram block in {doc.rel}"))
|
||||||
mermaid_errors += 1
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
lines = [line.strip() for line in block.splitlines() if line.strip() and not line.strip().startswith("%%")]
|
lines = [
|
||||||
|
line.strip() for line in block.splitlines()
|
||||||
|
if line.strip() and not line.strip().startswith("%%")
|
||||||
|
]
|
||||||
if not lines:
|
if not lines:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Check valid diagram header
|
|
||||||
header = lines[0].split()[0].lower()
|
header = lines[0].split()[0].lower()
|
||||||
if header not in MERMAID_TYPES:
|
if header not in MERMAID_TYPES:
|
||||||
errors.append(f"Invalid Mermaid diagram type '{header}' in {rel}")
|
issues.append(Issue(
|
||||||
mermaid_errors += 1
|
"error", doc.rel, None,
|
||||||
|
f"Invalid Mermaid diagram type '{header}' in {doc.rel}",
|
||||||
|
))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Check subgraph / block balance depending on diagram type
|
if header in ("flowchart", "graph"):
|
||||||
if header in ["flowchart", "graph"]:
|
|
||||||
subgraphs = sum(1 for line in lines if line.startswith("subgraph"))
|
subgraphs = sum(1 for line in lines if line.startswith("subgraph"))
|
||||||
ends = sum(1 for line in lines if line == "end" or line.startswith("end "))
|
ends = sum(1 for line in lines if line == "end" or line.startswith("end "))
|
||||||
if subgraphs != ends:
|
if subgraphs != ends:
|
||||||
errors.append(f"Unbalanced subgraph in Mermaid diagram in {rel} ({subgraphs} subgraph vs {ends} end)")
|
issues.append(Issue(
|
||||||
mermaid_errors += 1
|
"error", doc.rel, None,
|
||||||
|
f"Unbalanced subgraph in Mermaid diagram in {doc.rel} "
|
||||||
|
f"({subgraphs} subgraph vs {ends} end)",
|
||||||
|
))
|
||||||
elif header == "sequencediagram":
|
elif header == "sequencediagram":
|
||||||
block_openers = sum(1 for line in lines if re.match(r'^(alt|opt|loop|par|critical|break|rect)\b', line))
|
block_openers = sum(
|
||||||
|
1 for line in lines
|
||||||
|
if re.match(r'^(alt|opt|loop|par|critical|break|rect)\b', line)
|
||||||
|
)
|
||||||
ends = sum(1 for line in lines if re.match(r'^end\b', line))
|
ends = sum(1 for line in lines if re.match(r'^end\b', line))
|
||||||
if block_openers != ends:
|
if block_openers != ends:
|
||||||
errors.append(f"Unbalanced control blocks in Mermaid sequenceDiagram in {rel} ({block_openers} block openers vs {ends} end)")
|
issues.append(Issue(
|
||||||
mermaid_errors += 1
|
"error", doc.rel, None,
|
||||||
|
f"Unbalanced control blocks in Mermaid sequenceDiagram in {doc.rel} "
|
||||||
|
f"({block_openers} block openers vs {ends} end)",
|
||||||
|
))
|
||||||
|
|
||||||
# Check for unquoted special characters in transition labels: e.g. -->|label|
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
for label_match in re.finditer(r'-->\|([^|]+)\|', line):
|
for label_match in re.finditer(r'[-.=~<>]*\|([^|]+)\|', line):
|
||||||
label = label_match.group(1).strip()
|
label = label_match.group(1).strip()
|
||||||
# If label contains special characters like *, /, or unescaped parens but isn't quoted
|
if (
|
||||||
if any(ch in label for ch in ['*', '/']) and not (label.startswith('"') and label.endswith('"')):
|
any(ch in label for ch in ('*', '/', '(', ')', '[', ']', '{', '}'))
|
||||||
errors.append(f"Unquoted special character in Mermaid edge label '|{label}|' in {rel} (wrap in \"...\")")
|
and not (label.startswith('"') and label.endswith('"'))
|
||||||
mermaid_errors += 1
|
):
|
||||||
|
issues.append(Issue(
|
||||||
print(f" {'✓' if mermaid_errors == 0 else '✗'} Done ({mermaid_errors} issues).\n")
|
"error", doc.rel, None,
|
||||||
|
f"Unquoted special character in Mermaid edge label "
|
||||||
|
f"'|{label}|' in {doc.rel} (wrap in \"...\")",
|
||||||
|
))
|
||||||
|
return issues
|
||||||
|
|
||||||
|
|
||||||
# ── Check 8: Python Linting (Ruff) ────────────────────────────────────────────
|
# ── External checks ────────────────────────────────────────────────────────────
|
||||||
print("Checking Python code quality (Ruff)...")
|
|
||||||
if shutil.which("ruff"):
|
def run_ruff(root: pathlib.Path) -> list[Issue]:
|
||||||
res = subprocess.run(["ruff", "check", str(ROOT)], capture_output=True, text=True, check=False)
|
if not shutil.which("ruff"):
|
||||||
|
print(" ℹ Ruff not in PATH — Python lint check skipped.")
|
||||||
|
return []
|
||||||
|
res = subprocess.run(["ruff", "check", str(root)], capture_output=True, text=True, check=False)
|
||||||
if res.returncode != 0:
|
if res.returncode != 0:
|
||||||
errors.append(f"Ruff linting failed:\n{res.stdout.strip()}")
|
return [Issue("error", None, None, f"Ruff linting failed:\n{res.stdout.strip()}")]
|
||||||
print(" ✗ Done (Ruff errors found).\n")
|
return []
|
||||||
else:
|
|
||||||
print(" ✓ Done (0 lint issues).\n")
|
|
||||||
else:
|
|
||||||
print(" ℹ Skipped (ruff not in PATH).\n")
|
|
||||||
|
|
||||||
|
|
||||||
# ── Check 9: Python Type Checking (Pyright) ───────────────────────────────────
|
def run_pyright(root: pathlib.Path) -> list[Issue]:
|
||||||
print("Checking Python type consistency (Pyright)...")
|
if not shutil.which("pyright"):
|
||||||
if shutil.which("pyright"):
|
print(" ℹ Pyright not in PATH — type check skipped.")
|
||||||
res = subprocess.run(["pyright", str(ROOT / "scripts"), str(ROOT / "practice")], capture_output=True, text=True, check=False)
|
return []
|
||||||
|
res = subprocess.run(
|
||||||
|
["pyright", str(root / "scripts"), str(root / "practice")],
|
||||||
|
capture_output=True, text=True, check=False,
|
||||||
|
)
|
||||||
if res.returncode != 0:
|
if res.returncode != 0:
|
||||||
errors.append(f"Pyright type checks failed:\n{res.stdout.strip()}")
|
return [Issue("error", None, None, f"Pyright type checks failed:\n{res.stdout.strip()}")]
|
||||||
print(" ✗ Done (Pyright errors found).\n")
|
return []
|
||||||
|
|
||||||
|
|
||||||
|
# ── Reporting ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
def report(issues: list[Issue]) -> None:
|
||||||
|
"""Render collected issues to stdout.
|
||||||
|
|
||||||
|
The renderer is deliberately separated from the checks so that the output
|
||||||
|
format can evolve (e.g. --format json) without touching validation logic.
|
||||||
|
"""
|
||||||
|
errors = [i for i in issues if i.severity == "error"]
|
||||||
|
warnings = [i for i in issues if i.severity == "warning"]
|
||||||
|
|
||||||
|
if warnings:
|
||||||
|
print(f"\n⚠️ {len(warnings)} warning(s):\n")
|
||||||
|
for issue in warnings:
|
||||||
|
print(f" ⚠ {issue.message}")
|
||||||
|
|
||||||
|
if errors:
|
||||||
|
print(f"\n❌ Validation failed — {len(errors)} error(s):\n")
|
||||||
|
for issue in errors:
|
||||||
|
print(f" ✗ {issue.message}")
|
||||||
else:
|
else:
|
||||||
print(" ✓ Done (0 type errors).\n")
|
print("\n✅ All checks passed. Brain structure is valid.")
|
||||||
else:
|
|
||||||
print(" ℹ Skipped (pyright not in PATH).\n")
|
|
||||||
|
|
||||||
|
|
||||||
# ── Summary ───────────────────────────────────────────────────────────────────
|
# ── Runner ─────────────────────────────────────────────────────────────────────
|
||||||
if errors:
|
|
||||||
print(f"❌ Validation failed — {len(errors)} error(s):\n")
|
def main() -> int:
|
||||||
for e in errors:
|
issues: list[Issue] = []
|
||||||
print(f" ✗ {e}")
|
|
||||||
sys.exit(1)
|
# ── Repository-level checks ────────────────────────────────────────────────
|
||||||
else:
|
print("Checking repository structure...")
|
||||||
print("✅ All checks passed. Brain structure is valid.")
|
issues.extend(check_deprecated_dirs(ROOT))
|
||||||
|
issues.extend(check_required_files(ROOT))
|
||||||
|
|
||||||
|
# ── Document checks (single-pass discovery) ────────────────────────────────
|
||||||
|
print("Processing markdown files...")
|
||||||
|
documents = discover_documents(ROOT)
|
||||||
|
|
||||||
|
document_checks = (
|
||||||
|
check_frontmatter,
|
||||||
|
check_links,
|
||||||
|
check_code_fences,
|
||||||
|
check_mermaid,
|
||||||
|
)
|
||||||
|
for doc in documents:
|
||||||
|
for check in document_checks:
|
||||||
|
issues.extend(check(doc))
|
||||||
|
|
||||||
|
# ── External tooling checks ────────────────────────────────────────────────
|
||||||
|
print("Running Python quality checks...")
|
||||||
|
issues.extend(run_ruff(ROOT))
|
||||||
|
issues.extend(run_pyright(ROOT))
|
||||||
|
|
||||||
|
report(issues)
|
||||||
|
return 1 if any(i.severity == "error" for i in issues) else 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
||||||
Reference in new issue
Block a user