Skip to main content

Module change_context

Module change_context 

Expand description

Temporal pre-write briefing: what an agent should know about a set of files before it edits them.

build_change_context assembles a compact, deterministic text briefing for 1–MAX_BRIEFING_PATHS repo-relative paths. Like crate::enrichment::fact_sheet, it never computes anything new: it runs the existing analyses once for the whole batch (run_code_health, run_hotspots, run_coupling, owner_activity_for_paths, plus a windowed churn query), then picks each path’s values and renders them. It only reads the fact store — it never writes to it and never perturbs any analysis output.

§Determinism

Two builds over the same fact store produce byte-identical text: every float renders through a fixed rounding, blocks appear in request order, and nothing iterates a HashMap into the output (co-change partners are sorted into a Vec during assembly; per-path owner / churn data is looked up by key, never enumerated).

§Rendered format (the output contract)

When the repository is partway through a merge / rebase / cherry-pick / revert (Repo::merge_or_rebase_in_progress), one leading note precedes everything, separated from the first block by a blank line:

note: merge/rebase in progress — briefing reflects committed HEAD history

Each requested path renders one block; blocks are separated by a single blank line. A path with recorded history renders five indented lines, plus an optional sixth next-action line:

crates/codelore-lib/src/cache.rs
  health 67.3 (yellow) · risk 0.42 · calibrated defects-2026-07-15
  hotspot #12 (score 0.67, 23 revs)
  co-change: options.rs (68%, p=0.003) · facts/mod.rs (54%, p=0.011)
  owner: Emre Camdere 82% (sole owner, active 12d ago)
  recent: 4 commits, 310 lines churned in last 90d
  → historically co-changes with options.rs — consider the same edit there

Each line falls back to an honest-absence form when its data is missing:

  • health — score to one decimal + band, risk (structural_risk) to two decimals, and a calibration suffix (calibrated <vintage> from active_vintage, else uncalibrated). A path with no code-health row but some other history renders health: no code-health row.
  • hotspot #<rank> (score <score>, <revs> revs)rank is the 1-based position in the un-row-limited hotspot ranking; a path outside the ranking renders not in the hotspot set.
  • co-change: — up to three partners with shared >= DEFAULT_MIN_SHARED_REVS and fisher_p < DEFAULT_FISHER_SIGNIFICANCE, sorted by degree descending then partner path ascending; when more partners clear the filter than are rendered, the line ends with (+n more) disclosing the overflow count; none renders co-change: none significant.
  • owner: — main author, ownership share, sole owner / shared concentration, and departed <n>d when days_since_main_active exceeds departed_threshold_days (else active <n>d ago). A path with no attributable ownership renders owner: inconclusive.
  • recent: — commit count + churned lines over the last window_days; a path untouched in the window renders recent: quiet in last <window_days>d.
  • new-code: (optional, before the action line) — rendered only when the repo declares a [new_code] section AND the path is inside its window: born in the last <n>d (first seen in the window) or touched in the last <n>d (touched, first seen earlier), each with the obligation that band implies. Absent the section, or for a path outside the window, no line renders — a repo without [new_code] is byte-identical to before.
  • (optional final line) — a single next action derived from the picked values: co-change partners surface the top partner to edit alongside; with no partners, a main author past departed_threshold_days surfaces a knowledge-continuity flag. A block with neither carries no action line.

A path absent from every feed — no code-health row, not in the hotspot ranking, no significant co-change partners, no attributable ownership, and untouched in the churn window (a brand-new, untracked, or mistyped path) — renders a two-line block instead:

brand/new.rs
  no history at HEAD (new or untracked file)

Constants§

MAX_BRIEFING_PATHS
Maximum number of paths a single briefing accepts. Keeps the assembled text within the tool’s token budget and bounds the per-batch query cost.

Functions§

build_change_context
Assemble and render the pre-write briefing for paths.