Skip to content

[BUG/METRICS] documentation_risk lacks LOC-smoothing, causing up to 8.4x tier1-vs-tier3 divergence at small files #1098

Closed

Description

@squid-protocol

Problem

_calc_documentation() in gitgalaxy/metrics/signal_processor.py (lines ~1437-1498) is the
only tier-aware equation among the 6 audited so far in #1056
whose density calculation has no small-file smoothing term:

net_exposure = max(0.0, risk_hits - (defense_hits / 2.0))
density = (net_exposure / max(loc, 1)) * 100.0

Compare its siblings: _calc_safety cushions the same shape of calc with
smoothed_loc = safe_loc + laplace_smoothing (20.0); _calc_concurrency and _calc_state_flux
both add a loc_padding constant to their denominators. _calc_documentation divides by raw
loc with no equivalent floor.

Because irc (Implicit Risk Correction) contributes directly and additively to risk_hits
(risk_hits = opaque_execution + api_exposure + irc), and tier3's irc = 5 is a flat constant,
dividing it by a small raw loc makes it dominate density before any documentation credit gets
a chance to offset it — a smaller file amplifies irc's relative weight far more than the
"typical file" size the constant was presumably calibrated against. This is the same mechanical
shape that caused #1055 (a flat tier constant miscalibrated relative to a small-file denominator)
— the direction here is correct per the documented philosophy (implicit languages should retain
baseline opacity risk), but the magnitude is disproportionate at the small-file edge.

Empirical Proof

Direct invocation of SignalProcessor._calc_documentation() with identical documentation
defense evidence
(doc hits swept, api/ownership/irc-driven risk otherwise zero) across
tier1/tier2/tier3, via tests/tools/audit_risk_equations.py (--equation documentation,
"defense" scenario):

loc doc hits tier1 (rust) tier2 (python) tier3 (bash) ratio
10 1 11.92 75.95 99.94 8.38x
10 3 11.92 36.59 99.80 8.37x
10 8 11.92 11.92 96.08 8.06x
20 1 11.92 39.53 93.70 7.86x
20 3 11.92 21.84 89.09 7.47x
20 8 11.92 11.92 64.57 5.42x
50 1 11.92 20.26 47.00 3.94x
150 1 11.92 14.31 20.21 1.70x
500 1 11.92 12.60 14.04 1.18x

The ratio decays smoothly toward ~1x as loc grows, confirming the small-file denominator is the
driver, not a step-function or breach-floor artifact. Note tier1 is pinned flat at 11.92
across every row: since this scenario has zero risk signal other than tier1's own irc = 0,
risk_hits = 0 for tier1 regardless of documentation credit, so net_exposure floors at 0.0 —
11.92 is simply the sigmoid's asymptotic value at density = 0 (_calc_documentation has no
if risk_hits == 0: return 0.0 early exit the way _calc_safety/_calc_concurrency/
_calc_graveyard/_calc_secrets_risk all do, so it never returns a true zero). That's a
secondary, minor inconsistency worth fixing alongside the main issue, not a separate bug.

Root Cause

_calc_documentation's density calc divides by raw loc with no smoothing/padding constant,
so irc's flat additive contribution to risk_hits dominates disproportionately at small files
(10-20 LOC) relative to how it behaves at typical file sizes (150-500 LOC), where the ratio is
much more modest (1.2-1.7x). A real (non-synthetic) tiny documented file in an implicit-tier
language would see its documentation risk score swing wildly based on file size alone, not on the
actual amount of undocumented risk present.

Proposed Fix

Add an equivalent small-file smoothing term to _calc_documentation's density calc, mirroring
_calc_safety's laplace_smoothing idiom:

t = self.risk_tuning.get("documentation", {})
smoothed_loc = max(loc, 1) + t.get("loc_smoothing", 20.0)
...
density = (net_exposure / smoothed_loc) * 100.0

The exact smoothing constant would need calibration against the golden-master corpus (per the
Differential Scan protocol), but the shape of the fix mirrors the established pattern already used
by 3 of this equation's siblings.

Secondary, lower-priority fix: add an early if risk_hits == 0 and defense_hits == 0: return 0.0 (or similar) guard so a genuinely risk-free file doesn't carry the ~11.92 sigmoid floor,
matching the zero-return convention every other audited equation in this epic already follows.

Verification & Integration

Per CLAUDE.md's Differential Scan protocol: any fix to signal_processor.py's risk equations
requires python tests/tools/crucible_check.py before pushing and golden-master re-blessing via
python tests/tools/update_golden_master.py with an explanation in the PR description (this will
shift documentation_risk scores for small tier2/tier3 files in the corpus, which is the point).

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    metrics

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions