Expand description
Native code-health engine — clean code as a token-cost lever.
Computes the structural signals the Sonar study links to agent token cost — cognitive complexity (S3776-style), naming quality, and module coupling — once during indexing, then fans them out across the data fabric and every agent surface (see the Code Health Engine plan).
Naming note: this is distinct from lean-ctx-engine-ext’s core::quality (compression
fidelity) and from crate::core::gain’s usage-quality component; this
module scores the source code’s navigability.
Modules§
- annotate
- Read-time code-health annotations.
- cognitive
- Cognitive complexity (SonarQube S3776-style) via tree-sitter.
- delta
- Edit-time cognitive-complexity delta.
- fabric
- Code-health interconnection fabric (Phase 3 / #1083).
- gate
- Edit-time code-health gate — shared by
ctx_edit,ctx_patch, and the native-edit hook. - naming
- Naming-quality heuristic.
- persist
- Persisted project health (
health.json) + stale-guarded background refresh. - report
- Shared rendering of a
ProjectHealthreport (text + JSON). - scan
- Project-wide code-health scan — the shared report behind
lean-ctx health, thectx_qualitytool, and the dashboard. - score
- Project navigability score (0–100) and its estimated token-cost (“quality tax”).
Structs§
- File
Health - Combined per-file health: cognitive scores plus naming findings. This is the
single entry point used by the edit-gate, read annotations, and the
ctx_qualitytool. - Function
Cognitive - Cognitive complexity of a single function-like definition.
- Hotspot
- A single complexity hotspot surfaced to the agent.
- Naming
Finding - A function whose name is judged cryptic, with a human-readable reason.
- Navigability
Inputs - Inputs for
navigability. Behavioral inputs (wasted_tokens) and pricing are supplied by the caller so this stays a pure function.Copy(all fields areCopy, including the borrowed hotspot slice) so it passes by value cheaply. - Navigability
Score - Project-level navigability summary.
- Project
Health - Aggregated project health.
Enums§
- Gate
Mode - Edit-gate behavior when an edit increases cognitive complexity.
Constants§
- DEFAULT_
COGNITIVE_ THRESHOLD - Default cognitive-complexity threshold (SonarQube S3776 “HIGH” default).
A function at or below this is considered navigable. Mirrored by
CodeHealthConfig::default().cognitive_threshold.
Functions§
- analyze_
file - Analyze one file’s
sourcefor the given fileextension. - cognitive_
delta - Compare cognitive complexity of every function between
oldandnewsource for fileext. Returns only functions whose complexity changed, sorted by name. Empty when tree-sitter is disabled or nothing changed. - cognitive_
per_ function - Compute cognitive complexity per function for
sourceof the given fileextension. ReturnsNonewhen tree-sitter is disabled, the extension is unsupported, or the file has no functions. - format_
gate_ notice - Deterministic one-line edit-gate notice. No timestamps/counters → #498-safe.
- grade
- Letter grade for a navigability score, for compact display.
- naming_
findings - Report cryptic function names in
sourceof the given fileextension. ReturnsNonewhen tree-sitter is disabled or the extension is unsupported. - navigability
- Compute the navigability score from aggregated inputs. Deterministic.
- scan_
project - Scan
rootfor code-health, pricing the quality tax withmodel(or the blended fallback whenNone).top_nbounds the hotspot list in the score. - worst_
regression - The most significant increase that ends above
threshold, if any. This is what the edit-gate surfaces (one line, the worst offender).