Skip to main content

Module code_health

Module code_health 

Source
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 ProjectHealth report (text + JSON).
scan
Project-wide code-health scan — the shared report behind lean-ctx health, the ctx_quality tool, and the dashboard.
score
Project navigability score (0–100) and its estimated token-cost (“quality tax”).

Structs§

FileHealth
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_quality tool.
FunctionCognitive
Cognitive complexity of a single function-like definition.
Hotspot
A single complexity hotspot surfaced to the agent.
NamingFinding
A function whose name is judged cryptic, with a human-readable reason.
NavigabilityInputs
Inputs for navigability. Behavioral inputs (wasted_tokens) and pricing are supplied by the caller so this stays a pure function. Copy (all fields are Copy, including the borrowed hotspot slice) so it passes by value cheaply.
NavigabilityScore
Project-level navigability summary.
ProjectHealth
Aggregated project health.

Enums§

GateMode
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 source for the given file extension.
cognitive_delta
Compare cognitive complexity of every function between old and new source for file ext. 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 source of the given file extension. Returns None when 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 source of the given file extension. Returns None when tree-sitter is disabled or the extension is unsupported.
navigability
Compute the navigability score from aggregated inputs. Deterministic.
scan_project
Scan root for code-health, pricing the quality tax with model (or the blended fallback when None). top_n bounds 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).