Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

feat(decisions): ask the miner which files a commit's decision is about (#2441)

Raghav Chamadiya09b9737263b7
diff --git a/packages/core/src/repowise/core/analysis/decisions/extractor.py b/packages/core/src/repowise/core/analysis/decisions/extractor.py
index 2483e7769..c150a62fc 100644
--- a/packages/core/src/repowise/core/analysis/decisions/extractor.py
+++ b/packages/core/src/repowise/core/analysis/decisions/extractor.py
@@ -54,9 +54,11 @@ from repowise.core.analysis.decisions.policy import (
resolve_policy,
)
from repowise.core.analysis.decisions.scope import (
+ SCOPE_BASIS_SELECTED,
commit_scope_basis,
commit_scope_files,
resolve_module_nodes,
+ selected_scope_files,
)
from repowise.core.fs_walk import PRUNED_DIRS, walk_repo
from repowise.core.ingestion.traverser import load_gitignore_spec
@@ -138,6 +140,31 @@ def _coerce_dt(value: datetime | str) -> datetime:
# ---------------------------------------------------------------------------
+#: How many of a commit's files either commit prompt will show. The model has
+#: to read the list to pick from it, and a commit that touched ninety files is
+#: not one whose decisions can be assigned by reading the list anyway.
+_MAX_PROMPT_FILES = 20
+
+
+def _coerce_paths(value: object) -> list[str] | None:
+ """A list of path-ish strings out of whatever the model returned.
+
+ ``None`` for an absent key, so that a model which answered ``[]`` is told
+ apart from one that never answered: the first is a decision about none of
+ the commit's files, the second is a response from a provider that has not
+ seen the new prompt. Models return a bare string for a one-element list
+ often enough to be worth handling; anything else is dropped rather than
+ coerced.
+ """
+ if value is None:
+ return None
+ if isinstance(value, str):
+ value = [value]
+ if not isinstance(value, list):
+ return None
+ return [v.strip() for v in value if isinstance(v, str) and v.strip()]
+
+
@dataclass
class ExtractedDecision:
title: str
@@ -147,6 +174,16 @@ class ExtractedDecision:
alternatives: list[str] = field(default_factory=list)
consequences: list[str] = field(default_factory=list)
affected_files: list[str] = field(default_factory=list)
+ #: What the mining model named, before the commit's own file list has
+ #: validated it. Mining-time only: the two commit miners intersect it into
+ #: :attr:`affected_files` and clear it, so nothing downstream ever reads a
+ #: path the model produced and the commit does not list.
+ #:
+ #: ``None`` means the model was never asked or did not answer, which is
+ #: not the same as an empty list. An empty list is the model saying this
+ #: decision is about none of the commit's files, and that answer binds the
+ #: record to nothing; ``None`` falls back to the old breadth rule.
+ proposed_files: list[str] | None = None
affected_modules: list[str] = field(default_factory=list)
tags: list[str] = field(default_factory=list)
source: str = "inline_marker"
@@ -421,6 +458,34 @@ _MARKERS_PER_CALL = 5
# ---------------------------------------------------------------------------
+def _scope_from_selection(
+ decision: ExtractedDecision,
+ commit_files: Collection[str] | None,
+) -> tuple[list[str], str]:
+ """The files and basis for one decision mined out of one commit.
+
+ Returns the model's own selection, validated against the commit's file
+ list, under :data:`SCOPE_BASIS_SELECTED`. Falling back to the commit's
+ whole footprint when the model selected nothing would reinstate exactly
+ what this replaces, so an empty selection stays empty: the record keeps
+ its commit, its evidence and its place in repository-wide answers, and
+ stops answering "what governs this file". Roughly one record in six lands
+ here, and every one of them measured as a record whose subject was not in
+ the commit's list to begin with.
+
+ The old breadth rule is the fallback for a *missing* answer rather than an
+ empty one -- a provider that ignored the new key, or a cached response
+ written before it existed. There the record is scoped as it always was and
+ the legacy basis says so.
+ """
+ if decision.proposed_files is None:
+ return (commit_scope_files(commit_files), commit_scope_basis(commit_files))
+ chosen = selected_scope_files(decision.proposed_files, commit_files)
+ decision.proposed_files = None
+ return (chosen, SCOPE_BASIS_SELECTED)
+
+
+
class DecisionExtractor:
"""Extracts architectural decisions from multiple sources."""
@@ -736,10 +801,9 @@ class DecisionExtractor:
break
if sha:
d.evidence_commits = [sha]
- # Basis is judged on the commit's whole list, before the
- # cap: 42 files stored as 20 is still a 42-file footprint.
- d.scope_basis = commit_scope_basis(commit_files.get(sha))
- d.affected_files = commit_scope_files(commit_files.get(sha))
+ d.affected_files, d.scope_basis = _scope_from_selection(
+ d, commit_files.get(sha)
+ )
d.source_text = source_by_sha.get(sha, "")
d.source = "git_archaeology"
d.status = "proposed"
@@ -976,10 +1040,16 @@ class DecisionExtractor:
source_by_sha: dict[str, str] = {}
for c in batch:
pr_label = f" (PR #{c['pr']})" if c.get("pr") else ""
+ # The file list is what makes "affected_files" answerable.
+ # This miner asked for a decision without ever showing which
+ # files the commit touched, and scored 20% on topic against
+ # git archaeology's 45% on the same store.
+ files = files_by_sha.get(c["sha"], [])
bodies_block += (
f"\n--- Commit {c['sha'][:8]}{pr_label} ---\n"
f"Subject: {c['subject']}\n"
f"Body:\n{c['body'][:2000]}\n"
+ f"Files changed: {', '.join(sorted(files)[:_MAX_PROMPT_FILES])}\n"
)
source_by_sha[c["sha"]] = f"{c['subject']}\n{c['body']}"
prompt = PR_BODY_MINING_PROMPT.format(bodies_block=bodies_block)
@@ -1001,8 +1071,9 @@ class DecisionExtractor:
break
if sha:
d.evidence_commits = [sha]
- d.scope_basis = commit_scope_basis(files_by_sha.get(sha))
- d.affected_files = commit_scope_files(files_by_sha.get(sha))
+ d.affected_files, d.scope_basis = _scope_from_selection(
+ d, files_by_sha.get(sha)
+ )
d.source_text = source_by_sha.get(sha, "")
d.source = "pr"
d.status = "proposed"
@@ -1652,6 +1723,10 @@ class DecisionExtractor:
alternatives=item.get("alternatives", []),
consequences=item.get("consequences", []),
tags=item.get("tags", []),
+ # Only the two commit prompts ask for this. Every other
+ # prompt omits the key, so this stays None and the miner
+ # that owns those decisions keeps scoping them its own way.
+ proposed_files=_coerce_paths(item.get("affected_files")),
evidence_commits=[item["commit_sha"]] if "commit_sha" in item else [],
# Which marker this came from, for the inline-marker miner's
# per-marker attribution. Absent (and left None) for every
diff --git a/packages/core/src/repowise/core/analysis/decisions/prompts.py b/packages/core/src/repowise/core/analysis/decisions/prompts.py
index ee954a7cd..f46084c8a 100644
--- a/packages/core/src/repowise/core/analysis/decisions/prompts.py
+++ b/packages/core/src/repowise/core/analysis/decisions/prompts.py
@@ -36,7 +36,30 @@ Return a JSON array of decision objects. If a marker is not an architectural \
decision, skip it. Return [] if none qualify.
"""
-GIT_ARCHAEOLOGY_PROMPT = """\
+#: How the two commit miners ask for ``affected_files``. Shared so the wording
+#: cannot drift between them, and concatenated into each prompt rather than
+#: interpolated, because both templates are ``.format()``-ed later with their
+#: own block and an extra field would be a second placeholder to thread.
+#:
+#: The instruction is written so that returning nothing feels correct, because
+#: it is: measured out of sample, the empty answer is what holds noise at zero
+#: on the roughly one record in six whose subject never reached the commit's
+#: file list at all.
+_FILE_SELECTION_RULE = """\
+For "affected_files", copy paths verbatim from that commit's "Files changed" \
+line and keep only the files the decision is *about* -- the ones whose own \
+content it is a rule or a choice about. A file the change merely passed \
+through, a caller updated to match, a re-export or barrel, a lockfile, a \
+changelog, or a test that happens to exercise the subject does not belong. \
+Where one commit yields several decisions, divide its files between them \
+rather than giving each of them all. Return [] when the file the decision is \
+about is not in that commit's list; that is a correct answer and a common \
+one. Never write a path the commit does not list.\
+"""
+
+
+GIT_ARCHAEOLOGY_PROMPT = (
+ """\
Analyze these git commits to determine if they represent architectural decisions.
{commits_block}
@@ -49,14 +72,20 @@ system structure, patterns, migrations, or technology), return a JSON object:
"context": "what situation forced this",
"decision": "what was chosen or changed",
"rationale": "why this approach (infer from message and files only)",
+ "affected_files": ["which of that commit's files this decision is about"],
"alternatives": [],
"consequences": [],
"tags": ["relevant tags"]
}}
+"""
+ + _FILE_SELECTION_RULE
+ + """
+
Return a JSON array. Skip commits that are just bug fixes or minor changes. \
Return [] if none qualify. Do not hallucinate rationale.
"""
+)
README_MINING_PROMPT = """\
Analyze this documentation file and extract any architectural decisions.
@@ -86,7 +115,8 @@ Return a JSON array of decisions:
Only extract explicit decisions. Return [] if none found.
"""
-PR_BODY_MINING_PROMPT = """\
+PR_BODY_MINING_PROMPT = (
+ """\
These are squash-merge / PR commit bodies. Extract any architectural decision \
described in them (technology choices, migrations, structural changes, things \
deliberately rejected).
@@ -100,15 +130,21 @@ For each decision return a JSON object:
"context": "what situation forced this (only if stated)",
"decision": "what was chosen or changed",
"rationale": "why (quote/paraphrase the body — never invent)",
+ "affected_files": ["which of that commit's files this decision is about"],
"alternatives": ["rejected alternatives if mentioned"],
"consequences": [],
"tags": [],
"source_quote": "the exact sentence from the body this came from"
}}
+"""
+ + _FILE_SELECTION_RULE
+ + """
+
Return a JSON array. Skip bodies that are just checklists or release noise. \
Return [] if none qualify.
"""
+)
COMMENT_ARCHAEOLOGY_PROMPT = """\
These are block comments / docstrings from high-centrality code (the files \
diff --git a/packages/core/src/repowise/core/analysis/decisions/scope.py b/packages/core/src/repowise/core/analysis/decisions/scope.py
index f0d683621..bf1dff8a4 100644
--- a/packages/core/src/repowise/core/analysis/decisions/scope.py
+++ b/packages/core/src/repowise/core/analysis/decisions/scope.py
@@ -20,6 +20,7 @@ __all__ = [
"SCOPE_BASIS_FOOTPRINT",
"SCOPE_BASIS_PROXIMITY",
"SCOPE_BASIS_REPOSITORY",
+ "SCOPE_BASIS_SELECTED",
"SCOPE_BASIS_STATED",
"bind_scope_files",
"binds_to_paths",
@@ -27,6 +28,7 @@ __all__ = [
"commit_scope_files",
"derive_decision_scope",
"resolve_module_nodes",
+ "selected_scope_files",
"session_scope_basis",
]
@@ -69,6 +71,19 @@ SCOPE_BASIS_PROXIMITY = "session_proximity"
#: on the acceptance row.
SCOPE_BASIS_REPOSITORY = "repository"
+#: The basis value marking a commit-derived scope the mining model chose file
+#: by file, rather than the commit's whole footprint. It binds, and the
+#: legacy backfill never touches it, so a selected scope survives the repair
+#: that stamps every pre-selector commit record as a footprint.
+#:
+#: Measured out of sample, on 43 selections over 29 records drawn from the dev
+#: store: 93% of selected files govern the file they are shown on and none are
+#: noise, against 27% governs and 18% noise for the same records' whole commit
+#: lists. Every one of the 20 records that had a genuinely governing file kept
+#: one; the 5 the model emptied had none to keep. Evidence in
+#: ``local-stash/decision-layer-research/capture-2026-09-19/RESULTS.md``.
+SCOPE_BASIS_SELECTED = "commit_selected"
+
#: The basis value marking a scope a person stated: typed at the CLI, written
#: into the manifest, or confirmed on review. It binds, like the empty default
#: does; the difference is that the backfill repairs an empty basis and never
@@ -124,6 +139,27 @@ def commit_scope_files(files: Sequence[str] | None) -> list[str]:
return sorted(_normalized(files))[:_MAX_FILES]
+def selected_scope_files(
+ chosen: Sequence[str] | None,
+ commit_files: Sequence[str] | None,
+) -> list[str]:
+ """The files a mining model picked, kept only where the commit touched them.
+
+ The model is shown the commit's file list and asked which of those files
+ the decision is about, so a path outside that list is a path it invented.
+ Intersecting rather than trusting is the whole of the validation: the
+ miner has no other way to tell a real path from a plausible one, and a
+ hallucinated path binds a decision to a file nobody ever changed.
+
+ An empty result is a correct and common answer -- roughly one record in
+ six, where the file the decision is about never reached the commit list at
+ all -- and it means the record binds to nothing rather than to the
+ commit's bystanders.
+ """
+ allowed = set(_normalized(commit_files))
+ return sorted(f for f in _normalized(chosen) if f in allowed)[:_MAX_FILES]
+
+
def commit_scope_basis(files: Sequence[str] | None) -> str:
"""The scope basis for a record mined from one commit's file list.
diff --git a/packages/core/src/repowise/core/persistence/decision_dedupe.py b/packages/core/src/repowise/core/persistence/decision_dedupe.py
index 469c16a94..544705753 100644
--- a/packages/core/src/repowise/core/persistence/decision_dedupe.py
+++ b/packages/core/src/repowise/core/persistence/decision_dedupe.py
@@ -61,6 +61,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from repowise.core.analysis.decisions.lifecycle import ARCHITECTURAL_KIND
from repowise.core.analysis.decisions.provenance import rank_for_source
from repowise.core.analysis.decisions.scope import (
+ SCOPE_BASIS_SELECTED,
SCOPE_BASIS_STATED,
binds_to_paths,
commit_scope_basis,
@@ -330,10 +331,14 @@ async def apply_dedupe(
name: set(_json_list(getattr(canonical, column))) for column, name in _UNION_FIELDS
}
done = FoldPlan(canonical_id=canonical.id, canonical_title=canonical.title)
+ # Whether every list going into the union was chosen file by file.
+ # One footprint among them makes the union a footprint again.
+ all_selected = canonical.scope_basis == SCOPE_BASIS_SELECTED
for folded_id, title, score in cluster.folded:
folded = await session.get(DecisionRecord, folded_id)
if folded is None or folded_id not in eligible:
continue
+ all_selected = all_selected and folded.scope_basis == SCOPE_BASIS_SELECTED
for column, name in _UNION_FIELDS:
union[name] |= set(_json_list(getattr(folded, column)))
# The checkable noun wins a fold. An agreement that absorbs a
@@ -357,11 +362,19 @@ async def apply_dedupe(
# it rather than inherited: a footprint folding into a narrow
# canonical would otherwise hand it the wide list under a binding
# basis, on a record whose source the backfill does not repair.
- canonical.scope_basis = (
- SCOPE_BASIS_STATED
- if canonical.scope_basis == SCOPE_BASIS_STATED
- else commit_scope_basis(sorted(union["files"]))
- )
+ #
+ # A fold of records that each chose their own files is the exception.
+ # Duplicates are restatements of one decision, so the union of their
+ # selections is still a selection, and recomputing it by breadth would
+ # demote a scope no breadth rule ever produced. That holds only while
+ # every member chose: mixing one footprint in makes the union a
+ # footprint again, which is what ``all_selected`` tracks.
+ if canonical.scope_basis == SCOPE_BASIS_STATED:
+ pass
+ elif all_selected:
+ canonical.scope_basis = SCOPE_BASIS_SELECTED
+ else:
+ canonical.scope_basis = commit_scope_basis(sorted(union["files"]))
# Sync replaces rather than accretes, so it must see the union.
binds = binds_to_paths(canonical.scope_basis)
await sync_decision_node_links(
diff --git a/packages/core/src/repowise/core/persistence/decision_migration.py b/packages/core/src/repowise/core/persistence/decision_migration.py
index f6a44c941..f429b4ed3 100644
--- a/packages/core/src/repowise/core/persistence/decision_migration.py
+++ b/packages/core/src/repowise/core/persistence/decision_migration.py
@@ -29,7 +29,6 @@ from repowise.core.analysis.decisions.lifecycle import (
currency_for_legacy_status,
)
from repowise.core.analysis.decisions.scope import (
- MAX_GOVERNING_FILES,
SCOPE_BASIS_FOOTPRINT,
resolve_module_nodes,
session_scope_basis,
@@ -427,8 +426,21 @@ async def backfill_scope_basis(session: AsyncSession, repository_id: str) -> int
reason as the rest of this module: the rows predate the column, and only
code that runs on an existing store can fix them.
+ **Every** legacy row is repaired now, not only the wide ones. The miners
+ ask the model which files each decision is about and store that under
+ :data:`SCOPE_BASIS_SELECTED`, so an empty basis on a commit-derived row
+ means the row predates the selector and its file list is a commit
+ footprint whatever its length. Measured out of sample, those lists are 27%
+ on topic with 18% outright noise, and narrowness does not rescue them: at
+ a five-file cutoff, 82% of the rows that still bound came from commits
+ that had touched more than five files, because the miner's list is an
+ inversion of per-file git metadata rather than the commit's diff. The
+ breadth rule was the best available signal until the model was asked
+ directly, and it is not needed now.
+
Only rows with an **empty** basis are touched, which makes this idempotent
- and leaves a scope somebody set by hand alone.
+ and leaves both a scope somebody set by hand and a scope the model chose
+ alone.
The record keeps its files and loses its decision-graph links. Those are
dropped here rather than left to the next ``bulk_upsert_decisions``: a
@@ -454,7 +466,7 @@ async def backfill_scope_basis(session: AsyncSession, repository_id: str) -> int
files = json.loads(rec.affected_files_json or "[]")
except ValueError:
continue
- if len(files) <= MAX_GOVERNING_FILES:
+ if not files:
continue
rec.scope_basis = SCOPE_BASIS_FOOTPRINT
await session.execute(
diff --git a/packages/core/src/repowise/core/persistence/models.py b/packages/core/src/repowise/core/persistence/models.py
index 5679db646..16f80319c 100644
--- a/packages/core/src/repowise/core/persistence/models.py
+++ b/packages/core/src/repowise/core/persistence/models.py
@@ -971,6 +971,9 @@ class DecisionRecord(Base):
#: may answer "what governs this path". ``commit_footprint`` means the
#: files are the file list of the commit the record was mined from: kept
#: for provenance and staleness, skipped by every path-scoped surface.
+ #: ``commit_selected`` means the same miner asked the model which of that
+ #: commit's files the decision was about and stored the answer, so the
+ #: list is a claim and binds.
#: See :func:`~repowise.core.analysis.decisions.scope.binds_to_paths`.
scope_basis: Mapped[str] = mapped_column(
String(32), nullable=False, default="", server_default=""
diff --git a/tests/unit/analysis/test_decision_commit_selection.py b/tests/unit/analysis/test_decision_commit_selection.py
new file mode 100644
index 000000000..54a7b28b7
--- /dev/null
+++ b/tests/unit/analysis/test_decision_commit_selection.py
@@ -0,0 +1,219 @@
+"""A commit-mined decision binds the files the model chose, not the commit's.
+
+The defect these pin: ``pr`` and ``git_archaeology`` read one decision out of
+one commit body and then took that commit's *whole* file list, because the
+model was never asked which files the decision was about. A commit that
+bundles three unrelated changes therefore produced three decisions each
+claiming all of its files, and ``get_why`` on any of them answered with the
+other two.
+
+Measured out of sample on 29 records drawn from the dev store: the stored
+commit lists are 27% on topic with 18% outright noise, and the same records
+with the files chosen by a model reading the commit body are 93% on topic with
+none noise, losing no record that had a real answer. Evidence in
+``local-stash/decision-layer-research/capture-2026-09-19/RESULTS.md``.
+"""
+
+from __future__ import annotations
+
+import json
+from pathlib import Path
+from types import SimpleNamespace
+
+from repowise.core.analysis.decisions.extractor import DecisionExtractor
+from repowise.core.analysis.decisions.scope import (
+ SCOPE_BASIS_FOOTPRINT,
+ SCOPE_BASIS_SELECTED,
+ selected_scope_files,
+)
+
+#: One commit, two parts. ``_SUBJECT`` is what the decision under test is
+#: about; the rest is what the same commit happened to touch.
+_SUBJECT = "packages/core/src/repowise/core/hook.py"
+_BYSTANDERS = [f"packages/core/src/repowise/core/mod{i:02d}.py" for i in range(8)]
+_COMMIT_FILES = sorted([_SUBJECT, *_BYSTANDERS])
+
+_SHA = "9a0b27a7"
+
+
+def _meta_map(files: list[str] = _COMMIT_FILES) -> dict[str, dict]:
+ """A ``_git_meta_map`` whose inversion yields one commit over *files*."""
+ commit = {
+ "sha": _SHA,
+ "message": "perf: stop importing the workspace stack from the hook path",
+ "subject": "perf: stop importing the workspace stack from the hook path",
+ # Shaped so both miners accept it: the PR miner wants a PR-ish body
+ # marker plus a decision signal, git archaeology wants the signal.
+ "body": (
+ "## Why\n\n"
+ "The hook imported core.workspace.config only to reach "
+ "find_workspace_root, which initialized the extractor stack. We "
+ "chose instead to resolve CLI command modules lazily, because the "
+ "import cost was charged to every invocation."
+ ),
+ "pr_number": 2439,
+ "author": "someone",
+ "date": "2026-09-01",
+ }
+ return {f: {"significant_commits_json": json.dumps([commit])} for f in files}
+
+
+class _Provider:
+ """Returns one canned decision payload, and records what it was asked."""
+
+ def __init__(self, payload: list[dict]) -> None:
+ self._payload = payload
+ self.prompts: list[str] = []
+
+ async def generate(self, system, prompt, **kwargs):
+ self.prompts.append(prompt)
+ return SimpleNamespace(content=json.dumps(self._payload))
+
+
+def _extractor(tmp_path: Path, payload: list[dict]) -> DecisionExtractor:
+ provider = _Provider(payload)
+ ex = DecisionExtractor(
+ repo_path=tmp_path, provider=provider, git_meta_map=_meta_map()
+ )
+ ex.test_provider = provider # type: ignore[attr-defined]
+ return ex
+
+
+def _payload(**extra) -> list[dict]:
+ return [
+ {
+ "commit_sha": _SHA,
+ "title": "Avoid the workspace dependency graph in agent hooks",
+ "decision": "Drop the module-level import from the hook path.",
+ "rationale": "It initialized the whole extractor stack.",
+ **extra,
+ }
+ ]
+
+
+# --- the validator ---------------------------------------------------------
+
+
+def test_selection_keeps_only_files_the_commit_touched():
+ """A path outside the commit's list is a path the model invented.
+
+ Intersecting is the whole of the validation: the miner has no other way to
+ tell a real path from a plausible one, and the model is being shown the
+ list it is meant to be choosing from.
+ """
+ kept = selected_scope_files([_SUBJECT, "packages/core/src/invented.py"], _COMMIT_FILES)
+ assert kept == [_SUBJECT]
+
+
+def test_selection_normalises_separators_before_comparing():
+ """A Windows-shaped answer still matches a POSIX-shaped commit list."""
+ assert selected_scope_files(
+ ["packages\\core\\src\\repowise\\core\\hook.py"], _COMMIT_FILES
+ ) == [_SUBJECT]
+
+
+def test_selection_of_nothing_is_empty_rather_than_everything():
+ assert selected_scope_files([], _COMMIT_FILES) == []
+
+
+# --- the pr miner ----------------------------------------------------------
+
+
+async def test_pr_binds_the_selected_file_and_not_the_commit(tmp_path):
+ ex = _extractor(tmp_path, _payload(affected_files=[_SUBJECT]))
+
+ (decision,) = await ex.mine_pr_bodies()
+
+ assert decision.affected_files == [_SUBJECT]
+ assert decision.scope_basis == SCOPE_BASIS_SELECTED
+ for bystander in _BYSTANDERS:
+ assert bystander not in decision.affected_files
+
+
+async def test_pr_drops_a_path_the_commit_never_touched(tmp_path):
+ ex = _extractor(
+ tmp_path, _payload(affected_files=[_SUBJECT, "packages/core/src/ghost.py"])
+ )
+
+ (decision,) = await ex.mine_pr_bodies()
+
+ assert decision.affected_files == [_SUBJECT]
+
+
+async def test_pr_binds_nothing_when_the_model_selects_nothing(tmp_path):
+ """An empty answer must not fall back to the commit's whole list.
+
+ Falling back would reinstate exactly what this replaces, on the records it
+ helps most: roughly one in six, every one of them measured as a record
+ whose subject never reached the commit's file list at all.
+ """
+ ex = _extractor(tmp_path, _payload(affected_files=[]))
+
+ (decision,) = await ex.mine_pr_bodies()
+
+ assert decision.affected_files == []
+ assert decision.scope_basis == SCOPE_BASIS_SELECTED
+
+
+async def test_pr_falls_back_to_the_breadth_rule_when_the_key_is_absent(tmp_path):
+ """A missing answer is not an empty one.
+
+ A provider that has not seen the new prompt, or a cached response written
+ before it existed, leaves the record scoped as it always was, under the
+ basis that says so.
+ """
+ ex = _extractor(tmp_path, _payload())
+
+ (decision,) = await ex.mine_pr_bodies()
+
+ assert decision.affected_files == _COMMIT_FILES
+ assert decision.scope_basis == SCOPE_BASIS_FOOTPRINT
+
+
+async def test_pr_shows_the_model_the_commit_files(tmp_path):
+ """The miner that scored worst is the one that never showed the list.
+
+ ``mine_pr_bodies`` asked for a decision from a subject and a body alone,
+ so "which files is this about" had nothing to be answered from. It scored
+ 20% on topic against git archaeology's 45% on the same store.
+ """
+ ex = _extractor(tmp_path, _payload(affected_files=[_SUBJECT]))
+
+ await ex.mine_pr_bodies()
+
+ (prompt,) = ex.test_provider.prompts # type: ignore[attr-defined]
+ assert "Files changed:" in prompt
+ assert _SUBJECT in prompt
+
+
+async def test_pr_carries_no_unvalidated_path_off_the_miner(tmp_path):
+ """``proposed_files`` is cleared, so persistence cannot read it."""
+ ex = _extractor(
+ tmp_path, _payload(affected_files=[_SUBJECT, "packages/core/src/ghost.py"])
+ )
+
+ (decision,) = await ex.mine_pr_bodies()
+
+ assert decision.proposed_files is None
+
+
+# --- the git archaeology miner ---------------------------------------------
+
+
+async def test_git_archaeology_binds_the_selected_file(tmp_path):
+ ex = _extractor(tmp_path, _payload(affected_files=[_SUBJECT]))
+
+ decisions = await ex.mine_git_archaeology()
+
+ assert decisions, "the commit should have carried a decision signal"
+ assert decisions[0].affected_files == [_SUBJECT]
+ assert decisions[0].scope_basis == SCOPE_BASIS_SELECTED
+
+
+async def test_git_archaeology_falls_back_when_the_key_is_absent(tmp_path):
+ ex = _extractor(tmp_path, _payload())
+
+ decisions = await ex.mine_git_archaeology()
+
+ assert decisions[0].affected_files == _COMMIT_FILES
+ assert decisions[0].scope_basis == SCOPE_BASIS_FOOTPRINT
diff --git a/tests/unit/persistence/test_decision_dedupe.py b/tests/unit/persistence/test_decision_dedupe.py
index 7f1d45c7e..6e16dc46e 100644
--- a/tests/unit/persistence/test_decision_dedupe.py
+++ b/tests/unit/persistence/test_decision_dedupe.py
@@ -17,6 +17,7 @@ from sqlalchemy import select
from repowise.core.analysis.decisions.scope import (
MAX_GOVERNING_FILES,
SCOPE_BASIS_FOOTPRINT,
+ SCOPE_BASIS_SELECTED,
SCOPE_BASIS_STATED,
)
from repowise.core.analysis.decisions.semantic_match import DECISION_VECTOR_PREFIX
@@ -587,6 +588,80 @@ async def test_a_fold_that_stays_narrow_keeps_binding(async_session, repo_id):
assert await _links(async_session, canonical.id) == {"a.py", "b.py"}
+async def test_a_fold_of_two_selected_scopes_stays_selected(async_session, repo_id):
+ """Duplicates are restatements of one decision, so their union is a claim.
+
+ Both members chose their files file by file, so recomputing the basis by
+ breadth would demote a scope no breadth rule ever produced -- and the
+ union of two selections is wider than either, which is exactly when the
+ old recompute fires.
+ """
+ store = _store()
+ canonical, _duplicate = await _seed(
+ async_session,
+ store,
+ repo_id,
+ [
+ {
+ "title": "Keep @a",
+ "source": "pr",
+ "decision": "Body",
+ "files": _WIDE[:4],
+ "scope_basis": SCOPE_BASIS_SELECTED,
+ },
+ {
+ "title": "Drop @a",
+ "source": "comment",
+ "files": _WIDE[4:],
+ "scope_basis": SCOPE_BASIS_SELECTED,
+ },
+ ],
+ )
+ await apply_dedupe(async_session, repo_id, vector_store=store, tau=TAU)
+
+ survivor = await async_session.get(DecisionRecord, canonical.id)
+ assert len(json.loads(survivor.affected_files_json)) > MAX_GOVERNING_FILES
+ assert survivor.scope_basis == SCOPE_BASIS_SELECTED
+ assert await _links(async_session, canonical.id) == set(_WIDE)
+
+
+async def test_a_footprint_folding_into_a_selected_scope_demotes_it(
+ async_session, repo_id
+):
+ """One unchosen list in the union makes the union unchosen again.
+
+ The fold takes the union of the file lists, so a footprint folding in
+ hands the canonical files nobody ever chose. Keeping ``commit_selected``
+ there would launder them into a binding scope.
+ """
+ store = _store()
+ canonical, _duplicate = await _seed(
+ async_session,
+ store,
+ repo_id,
+ [
+ {
+ "title": "Keep @a",
+ "source": "pr",
+ "decision": "Body",
+ "files": ["kept.py"],
+ "scope_basis": SCOPE_BASIS_SELECTED,
+ },
+ {
+ "title": "Drop @a",
+ "source": "comment",
+ "files": _WIDE,
+ "scope_basis": SCOPE_BASIS_FOOTPRINT,
+ },
+ ],
+ )
+ await apply_dedupe(async_session, repo_id, vector_store=store, tau=TAU)
+
+ survivor = await async_session.get(DecisionRecord, canonical.id)
+ assert survivor.scope_basis == SCOPE_BASIS_FOOTPRINT
+ assert await _links(async_session, canonical.id) == set()
+
+
async def test_a_fold_never_overwrites_a_stated_basis(async_session, repo_id):
"""A scope a person accepted survives a later dedupe sweep."""
store = _store()
diff --git a/tests/unit/persistence/test_decision_scope_basis.py b/tests/unit/persistence/test_decision_scope_basis.py
index ee5260918..87eaaffea 100644
--- a/tests/unit/persistence/test_decision_scope_basis.py
+++ b/tests/unit/persistence/test_decision_scope_basis.py
@@ -21,6 +21,7 @@ from repowise.core.analysis.decisions.scope import (
SCOPE_BASIS_FOOTPRINT,
SCOPE_BASIS_PROXIMITY,
SCOPE_BASIS_REPOSITORY,
+ SCOPE_BASIS_SELECTED,
SCOPE_BASIS_STATED,
)
from repowise.core.persistence.crud import bulk_upsert_decisions, update_decision_metadata
@@ -149,15 +150,62 @@ async def test_backfill_marks_a_legacy_wide_pr_record_and_drops_its_file_links(
assert await get_governing_decisions(async_session, repo.id, _BYSTANDER) == []
-async def test_backfill_leaves_a_narrow_commit_record_alone(async_session):
+async def test_backfill_repairs_a_narrow_legacy_commit_record_too(async_session):
+ """Narrowness does not make a legacy commit list a claim about files.
+
+ The miner never chose any of them. A four-file legacy scope is four files
+ of one commit, and out of sample it is no more on topic than a wide one:
+ at a five-file cutoff, 82% of the records that still bound came from
+ commits that had touched more than five files, because the stored list is
+ an inversion of per-file git metadata rather than the commit's diff.
+ """
repo = await insert_repo(async_session)
files = [f"pkg/m{i}.py" for i in range(4)]
await bulk_upsert_decisions(
async_session, repo.id, [_decision("Narrow", files=files)]
)
+ assert await backfill_scope_basis(async_session, repo.id) == 1
+ assert await get_governing_decisions(async_session, repo.id, files[0]) == []
+
+
+async def test_backfill_leaves_a_selected_scope_alone(async_session):
+ """A scope the model chose file by file is not repaired into a footprint.
+
+ This is the whole point of the separate basis: the repair has to be able
+ to tell a pre-selector row from one the selector wrote, and both are
+ commit-derived with a short file list.
+ """
+ repo = await insert_repo(async_session)
+ await bulk_upsert_decisions(
+ async_session,
+ repo.id,
+ [
+ _decision(
+ "Chosen", files=[_MECHANISM], scope_basis=SCOPE_BASIS_SELECTED
+ )
+ ],
+ )
+
assert await backfill_scope_basis(async_session, repo.id) == 0
- assert await get_governing_decisions(async_session, repo.id, files[0]) != []
+ assert await get_governing_decisions(async_session, repo.id, _MECHANISM) != []
+
+
+async def test_backfill_leaves_a_scopeless_commit_record_alone(async_session):
+ """A record with no files has nothing to demote.
+
+ Stamping it a footprint would claim its empty list is a commit's, which is
+ the one thing it is not: the selector writes an empty list on purpose when
+ the decision is about none of the commit's files.
+ """
+ repo = await insert_repo(async_session)
+ ids = await bulk_upsert_decisions(
+ async_session, repo.id, [_decision("No files", files=[])]
+ )
+
+ assert await backfill_scope_basis(async_session, repo.id) == 0
+ rec = await async_session.get(DecisionRecord, ids[0])
+ assert rec.scope_basis == ""
async def test_backfill_leaves_a_wide_record_from_another_source_alone(async_session):