Module calibration
Expand description
Corpus-calibration artifact: per-language quantile breakpoints for raw per-function metrics, plus the interpolation lookup that turns a metric value into a corpus-relative percentile (“your cyclomatic complexity sits at P74 versus the reference corpus”).
§The artifact
A CalibrationArtifact is a versioned, compact JSON container. For each
Tier-1 language it holds a LanguageTable whose (v1: single) Stratum
carries one MetricQuantiles per raw metric. Each metric stores a
QUANTILE_POINTS-long, non-decreasing vector: element i is the value at
quantile i / (QUANTILE_POINTS - 1), so index 750 is the corpus q0.750
breakpoint. Storing breakpoints (not raw observations) keeps the embedded
world artifact small while still supporting interpolated lookups.
§Lookup
percentile binary-searches a metric’s breakpoint vector and linearly
interpolates between the two neighbours. Values below the corpus minimum
floor to p = 0.0; values above the maximum saturate to p = 1.0 and set
CorpusPercentile::beyond_corpus. A language whose pooled sample is below
MIN_LANG_SAMPLE — too thin to trust — is treated as absent, as is an
unknown language or metric; those all return None.
conditional_tail_percentile is the companion lookup for pools dominated
by trivial (zero-complexity) functions: it conditions the percentile on the
non-trivial tail so a real file’s complexity does not saturate the top of a
mostly-zero distribution.
§Building
build_from_observations pools raw per-function metric values per language
and reduces each pool to a breakpoint vector. The generated_at timestamp is
injected by the caller (the CLI passes the wall clock; determinism tests pass
a constant) so the same observations always produce byte-identical output.
merge blends two artifacts by sample-count-weighted interpolation of
their quantile vectors. This is an approximation — see its own docs.
Structs§
- Calibration
Artifact - A versioned corpus-calibration artifact: per-language quantile breakpoints for raw per-function metrics.
- Corpus
Manifest - A corpus build manifest: the set of pinned repos the
calibratecommand ingests and pools per-function metrics from. - Corpus
Percentile - The result of a corpus-percentile lookup.
- Corpus
Repo - One pinned repo in a
CorpusManifest. - Lang
Observations - Raw per-function metric observations, pooled per
(language, metric)before reduction to breakpoint vectors bybuild_from_observations. - Language
Table - Per-language breakpoints.
languageis a [Tier1Language] name (crate::complexity::language::Tier1Language::as_str). - Metric
Quantiles - Quantile breakpoints for one raw metric (
"cyclomatic","cognitive","sloc","nargs","max_nesting").quantileshas lengthQUANTILE_POINTSand is non-decreasing. - Repo
Metrics - Repo-level metric pools: one observation per corpus repo. Absent on artifacts built before this section existed — absent = no lens.
- Stratum
- A SLOC-bounded stratum of metric breakpoints. v1: bounds are
0..=u64::MAX.
Constants§
- CALIBRATION_
FORMAT_ VERSION - Artifact schema version. A
loaded artifact whoseformat_versiondiffers is rejected with a hard error — an explicitly passed--calibrationfile that cannot be used is a configuration mistake, not a degradable state. - MIN_
LANG_ SAMPLE - Minimum pooled function count for a language to be trusted. Below this the language is treated as absent (its breakpoints are too noisy to compare against).
- QUANTILE_
POINTS - Length of every quantile-breakpoint vector: q0.000 … q1.000 inclusive at a 0.001 step.
Functions§
- active_
vintage - Vintage string of the calibration artifact active for
opts. A thin wrapper overload_active_artifact— the one place the resolution precedence lives — so the provenance stamp and the corpus lens never drift apart. - attach_
repo_ metrics - Attach repo-level metric pools to a calibration artifact.
- build_
from_ observations - Build a calibration artifact from pooled raw observations.
- conditional_
tail_ percentile - Percentile of
valuewithin the corpus’s non-trivial tail for(language, metric)— the conditional lookup the anchored hotspot score uses in place ofpercentile. - embedded_
world - The embedded world artifact, lazily parsed once.
- language_
sample_ functions - Pooled per-function sample size behind
language’s breakpoints — the honestnfor a Wilson confidence interval on a per-function corpus percentile. - load
- Load and validate a calibration artifact from a JSON file.
- load_
active_ artifact - The single home for the active-artifact resolution precedence, shared by the corpus-percentile lens and the provenance vintage stamp:
- load_
manifest - Parse and return a
CorpusManifestfrom a TOML file. - merge
- Blend two artifacts by sample-count-weighted interpolation of their quantile vectors, summing sample counts.
- percentile
- Corpus-relative percentile of
valuefor(language, metric). - raw_
percentile - Midpoint-rank percentile of
valueamongsorted(ascending).