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.

fix(decisions): stop the scope repair demoting what a person accepted (#2445)

Raghav Chamadiya05c34f9cea12
diff --git a/packages/core/src/repowise/core/analysis/decisions/extractor.py b/packages/core/src/repowise/core/analysis/decisions/extractor.py
index c150a62fc..d2a6f981f 100644
--- a/packages/core/src/repowise/core/analysis/decisions/extractor.py
+++ b/packages/core/src/repowise/core/analysis/decisions/extractor.py
@@ -38,7 +38,7 @@ import asyncio
import contextlib
import json
import re
-from collections.abc import Collection, Iterator
+from collections.abc import Collection, Iterator, Sequence
from dataclasses import dataclass, field
from datetime import UTC, datetime
from pathlib import Path
@@ -143,18 +143,26 @@ 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.
+#:
+#: Sorted before truncating at both call sites, so which files the model is
+#: allowed to choose from is a property of the commit rather than of the order
+#: ``_git_meta_map`` happened to be built in.
_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.
+ ``None`` for an absent *or malformed* key, so that a model which answered
+ ``[]`` is told apart from one that never usefully answered: the first is a
+ decision about none of the commit's files, which binds the record to
+ nothing, and the second is a provider that has not seen the new prompt,
+ which falls back to the commit list. A list of dicts or numbers is the
+ second case, not the first -- reading it as "chose nothing" would silently
+ make the record govern nothing forever on a shape error.
+
+ Models return a bare string for a one-element list often enough to be
+ worth handling.
"""
if value is None:
return None
@@ -162,7 +170,10 @@ def _coerce_paths(value: object) -> list[str] | None:
value = [value]
if not isinstance(value, list):
return None
- return [v.strip() for v in value if isinstance(v, str) and v.strip()]
+ kept = [v.strip() for v in value if isinstance(v, str) and v.strip()]
+ if value and not kept:
+ return None
+ return kept
@dataclass
@@ -460,7 +471,7 @@ _MARKERS_PER_CALL = 5
def _scope_from_selection(
decision: ExtractedDecision,
- commit_files: Collection[str] | None,
+ commit_files: Sequence[str] | None,
) -> tuple[list[str], str]:
"""The files and basis for one decision mined out of one commit.
@@ -780,7 +791,8 @@ class DecisionExtractor:
f"{body_block}"
f"Author: {c['author']}\n"
f"Date: {c['date']}\n"
- f"Files changed: {', '.join(files[:20])}\n"
+ f"Files changed: "
+ f"{', '.join(sorted(files)[:_MAX_PROMPT_FILES])}\n"
)
source_by_sha[c["sha"]] = f"{c['message']}\n{body}".strip()
@@ -805,6 +817,10 @@ class DecisionExtractor:
d, commit_files.get(sha)
)
d.source_text = source_by_sha.get(sha, "")
+ # Cleared whether or not the sha resolved: an unattributed
+ # decision has no commit to validate paths against, so the
+ # model's list is unusable rather than merely unused.
+ d.proposed_files = None
d.source = "git_archaeology"
d.status = "proposed"
signal = max(
@@ -1075,6 +1091,7 @@ class DecisionExtractor:
d, files_by_sha.get(sha)
)
d.source_text = source_by_sha.get(sha, "")
+ d.proposed_files = None
d.source = "pr"
d.status = "proposed"
d.confidence = 0.80
diff --git a/packages/core/src/repowise/core/analysis/decisions/scope.py b/packages/core/src/repowise/core/analysis/decisions/scope.py
index bf1dff8a4..c41e5323f 100644
--- a/packages/core/src/repowise/core/analysis/decisions/scope.py
+++ b/packages/core/src/repowise/core/analysis/decisions/scope.py
@@ -92,10 +92,19 @@ SCOPE_BASIS_SELECTED = "commit_selected"
SCOPE_BASIS_STATED = "stated"
#: Above this many files, a commit-derived list stops being a claim about
-#: files and becomes the footprint of the change it was mined from. The miner
-#: reads one decision out of one commit body and has no per-file evidence, so
-#: it takes the commit's whole list: true about the commit, false about most
-#: of the files in it.
+#: files and becomes the footprint of the change it was mined from.
+#:
+#: **Only the fallback path still consults this.** Both commit miners now ask
+#: the model which files each decision is about and store the answer under
+#: :data:`SCOPE_BASIS_SELECTED`, so breadth decides the scope of one case: a
+#: response that did not answer at all, from a provider that has not seen the
+#: prompt. ``backfill_scope_basis`` no longer consults it, because a legacy
+#: list is a commit's footprint at any width. The reasoning below is kept for
+#: the history of the number, not as a description of the live rule.
+#:
+#: The miner read one decision out of one commit body and had no per-file
+#: evidence, so it took the commit's whole list: true about the commit, false
+#: about most of the files in it.
#:
#: A breadth rule rather than a relevance one because relevance was tried and
#: does not work. Overlap between a decision's text and a file's own diff hunk
diff --git a/packages/core/src/repowise/core/persistence/decision_dedupe.py b/packages/core/src/repowise/core/persistence/decision_dedupe.py
index 544705753..27183dfa5 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_FOOTPRINT,
SCOPE_BASIS_SELECTED,
SCOPE_BASIS_STATED,
binds_to_paths,
@@ -331,14 +332,16 @@ 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.
+ # Whether every list going into the union was chosen file by file,
+ # and whether any of them was a list no surface may bind to.
all_selected = canonical.scope_basis == SCOPE_BASIS_SELECTED
+ any_unbound = not binds_to_paths(canonical.scope_basis)
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
+ any_unbound = any_unbound or not binds_to_paths(folded.scope_basis)
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
@@ -367,12 +370,21 @@ async def apply_dedupe(
# 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.
+ # every member chose.
+ #
+ # Otherwise, a member that could not bind on its own must not bind
+ # through the union. The breadth rule cannot express that any more:
+ # ``backfill_scope_basis`` now marks a legacy commit list a footprint
+ # at any width, so ``commit_scope_basis`` over a small union returns
+ # the binding empty basis and would hand the canonical files nobody
+ # ever chose. Breadth is left to decide only the case it still owns --
+ # two lists that were both binding to begin with.
if canonical.scope_basis == SCOPE_BASIS_STATED:
pass
elif all_selected:
canonical.scope_basis = SCOPE_BASIS_SELECTED
+ elif any_unbound:
+ canonical.scope_basis = SCOPE_BASIS_FOOTPRINT
else:
canonical.scope_basis = commit_scope_basis(sorted(union["files"]))
# Sync replaces rather than accretes, so it must see the union.
diff --git a/packages/core/src/repowise/core/persistence/decision_migration.py b/packages/core/src/repowise/core/persistence/decision_migration.py
index f429b4ed3..7ab7ce793 100644
--- a/packages/core/src/repowise/core/persistence/decision_migration.py
+++ b/packages/core/src/repowise/core/persistence/decision_migration.py
@@ -42,7 +42,12 @@ from .crud.authority import (
upsert_candidate_meta,
)
from .decision_graph import DecisionNodeLink
-from .models import DecisionCandidateMeta, DecisionRecord, GraphNode
+from .models import (
+ DecisionAcceptance,
+ DecisionCandidateMeta,
+ DecisionRecord,
+ GraphNode,
+)
__all__ = [
"MigrationPlan",
@@ -439,8 +444,17 @@ async def backfill_scope_basis(session: AsyncSession, repository_id: str) -> int
directly, and it is not needed now.
Only rows with an **empty** basis are touched, which makes this idempotent
- and leaves both a scope somebody set by hand and a scope the model chose
- alone.
+ and leaves a scope the model chose alone.
+
+ A record somebody accepted is left alone too, and by its acceptance rather
+ than by its basis: ``accept_decision`` writes
+ :data:`SCOPE_BASIS_STATED` only when the accepter passed an explicit
+ scope, and neither ``repowise decision confirm`` nor the web accept route
+ does. Filtering on the basis alone would therefore silently drop the file
+ links of every decision a reviewer had confirmed as-is -- and take their
+ hotspots with it, since ``governed_files`` is built from accepted
+ path-binding records and an ``ungoverned_hotspot`` finding is emitted for
+ every hotspot outside it.
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,6 +468,9 @@ async def backfill_scope_basis(session: AsyncSession, repository_id: str) -> int
DecisionRecord.repository_id == repository_id,
DecisionRecord.source.in_(tuple(_COMMIT_FOOTPRINT_SOURCES)),
DecisionRecord.scope_basis == "",
+ ~select(DecisionAcceptance.id)
+ .where(DecisionAcceptance.decision_id == DecisionRecord.id)
+ .exists(),
)
)
)
diff --git a/tests/unit/analysis/test_decision_commit_selection.py b/tests/unit/analysis/test_decision_commit_selection.py
index 54a7b28b7..fd262400a 100644
--- a/tests/unit/analysis/test_decision_commit_selection.py
+++ b/tests/unit/analysis/test_decision_commit_selection.py
@@ -20,7 +20,10 @@ import json
from pathlib import Path
from types import SimpleNamespace
-from repowise.core.analysis.decisions.extractor import DecisionExtractor
+from repowise.core.analysis.decisions.extractor import (
+ DecisionExtractor,
+ _coerce_paths,
+)
from repowise.core.analysis.decisions.scope import (
SCOPE_BASIS_FOOTPRINT,
SCOPE_BASIS_SELECTED,
@@ -217,3 +220,96 @@ async def test_git_archaeology_falls_back_when_the_key_is_absent(tmp_path):
assert decisions[0].affected_files == _COMMIT_FILES
assert decisions[0].scope_basis == SCOPE_BASIS_FOOTPRINT
+
+
+async def test_git_archaeology_binds_nothing_when_the_model_selects_nothing(tmp_path):
+ ex = _extractor(tmp_path, _payload(affected_files=[]))
+
+ decisions = await ex.mine_git_archaeology()
+
+ assert decisions[0].affected_files == []
+ assert decisions[0].scope_basis == SCOPE_BASIS_SELECTED
+
+
+async def test_git_archaeology_drops_a_path_the_commit_never_touched(tmp_path):
+ ex = _extractor(
+ tmp_path, _payload(affected_files=[_SUBJECT, "packages/core/src/ghost.py"])
+ )
+
+ decisions = await ex.mine_git_archaeology()
+
+ assert decisions[0].affected_files == [_SUBJECT]
+
+
+async def test_git_archaeology_shows_the_files_in_a_reproducible_order(tmp_path):
+ """Which files the model may choose from is a property of the commit.
+
+ The list is an inversion of a per-file map, so its natural order is
+ whichever file the walk reached first. Truncating that unsorted made the
+ choosable set depend on the index run rather than the commit.
+ """
+ ex = _extractor(tmp_path, _payload(affected_files=[_SUBJECT]))
+
+ await ex.mine_git_archaeology()
+
+ (prompt,) = ex.test_provider.prompts
+ shown = prompt.split("Files changed: ")[1].split("\n")[0].split(", ")
+ assert shown == sorted(shown)
+
+
+# --- what the model returned, when it is not a list of paths ---------------
+
+
+def test_a_malformed_answer_is_not_read_as_an_empty_selection():
+ """A shape error must not permanently bind a record to nothing.
+
+ ``[]`` means "about none of these files", which is a real answer and a
+ common one. A list of dicts or numbers is a provider answering in the
+ wrong shape, and reading it as ``[]`` would silently make the decision
+ govern nothing for good. It falls back instead.
+ """
+ assert _coerce_paths([{"path": "a.py"}]) is None
+ assert _coerce_paths([None]) is None
+ assert _coerce_paths({"files": ["a.py"]}) is None
+ # An answer that really is empty still reads as empty.
+ assert _coerce_paths([]) == []
+
+
+def test_a_bare_string_answer_is_read_as_one_path():
+ assert _coerce_paths("a.py") == ["a.py"]
+
+
+# --- a decision the miner cannot attribute to a commit ---------------------
+
+
+async def test_an_unattributed_decision_keeps_no_model_paths(tmp_path):
+ """No commit means no list to validate against, so the answer is unusable.
+
+ Only the attributed branch called the helper that clears the field, so an
+ unattributed decision carried the model's raw paths into
+ ``dataclasses.asdict`` and on to persistence. Nothing there reads the key,
+ so it was inert -- and the field promises that it cannot be read at all.
+ """
+ payload = _payload(affected_files=["packages/core/src/ghost.py"])
+ del payload[0]["commit_sha"]
+ payload[0]["title"] = "A title matching no commit subject"
+ ex = _extractor(tmp_path, payload)
+
+ (decision,) = await ex.mine_pr_bodies()
+
+ assert decision.proposed_files is None
+ assert decision.evidence_commits == []
+ assert decision.affected_files == []
+
+
+async def test_an_unattributed_archaeology_decision_keeps_no_model_paths(tmp_path):
+ """The same guarantee on the other miner, which has the same branch."""
+ payload = _payload(affected_files=["packages/core/src/ghost.py"])
+ del payload[0]["commit_sha"]
+ payload[0]["title"] = "A title matching no commit message"
+ ex = _extractor(tmp_path, payload)
+
+ decisions = await ex.mine_git_archaeology()
+
+ assert decisions[0].proposed_files is None
+ assert decisions[0].affected_files == []
diff --git a/tests/unit/persistence/test_decision_dedupe.py b/tests/unit/persistence/test_decision_dedupe.py
index 6e16dc46e..624070b85 100644
--- a/tests/unit/persistence/test_decision_dedupe.py
+++ b/tests/unit/persistence/test_decision_dedupe.py
@@ -17,8 +17,10 @@ from sqlalchemy import select
from repowise.core.analysis.decisions.scope import (
MAX_GOVERNING_FILES,
SCOPE_BASIS_FOOTPRINT,
+ SCOPE_BASIS_REPOSITORY,
SCOPE_BASIS_SELECTED,
SCOPE_BASIS_STATED,
+ binds_to_paths,
)
from repowise.core.analysis.decisions.semantic_match import DECISION_VECTOR_PREFIX
from repowise.core.persistence.crud.authority import (
@@ -625,6 +627,78 @@ async def test_a_fold_of_two_selected_scopes_stays_selected(async_session, repo_
assert await _links(async_session, canonical.id) == set(_WIDE)
+async def test_a_narrow_footprint_folding_in_does_not_become_binding(
+ async_session, repo_id
+):
+ """A small union of unchosen files must not come back as a claim.
+
+ The recompute asks ``commit_scope_basis``, which answers with the binding
+ empty basis for any union at or under the cutoff. That was safe while a
+ footprint could only exist above the cutoff. It no longer is:
+ ``backfill_scope_basis`` marks a legacy commit list a footprint at any
+ width, so a two-file footprint folding into a one-file canonical produced
+ a three-file union that bound -- publishing the two files the repair had
+ just taken away.
+ """
+ store = _store()
+ narrow_footprint = ["b.py", "c.py"]
+ assert len(narrow_footprint) + 1 <= MAX_GOVERNING_FILES
+ canonical, _duplicate = await _seed(
+ async_session,
+ store,
+ repo_id,
+ [
+ {
+ "title": "Keep @a",
+ "source": "pr",
+ "decision": "Body",
+ "files": ["kept.py"],
+ },
+ {
+ "title": "Drop @a",
+ "source": "comment",
+ "files": narrow_footprint,
+ "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_does_not_promote_a_repository_scope(async_session, repo_id):
+ """An agreement is true everywhere and specific nowhere, fold or no fold.
+
+ Only ``stated`` used to be preserved, so a working agreement that absorbed
+ a duplicate had its basis recomputed by breadth and came back binding on
+ whatever files the union happened to hold.
+ """
+ store = _store()
+ canonical, _duplicate = await _seed(
+ async_session,
+ store,
+ repo_id,
+ [
+ {
+ "title": "Keep @a",
+ "source": "pr",
+ "decision": "Body",
+ "files": ["a.py"],
+ "scope_basis": SCOPE_BASIS_REPOSITORY,
+ },
+ {"title": "Drop @a", "source": "comment", "files": ["b.py"]},
+ ],
+ )
+ await apply_dedupe(async_session, repo_id, vector_store=store, tau=TAU)
+
+ survivor = await async_session.get(DecisionRecord, canonical.id)
+ assert not binds_to_paths(survivor.scope_basis)
+ assert await _links(async_session, canonical.id) == set()
+
+
async def test_a_footprint_folding_into_a_selected_scope_demotes_it(
async_session, repo_id
):
diff --git a/tests/unit/persistence/test_decision_scope_basis.py b/tests/unit/persistence/test_decision_scope_basis.py
index 87eaaffea..086439c3e 100644
--- a/tests/unit/persistence/test_decision_scope_basis.py
+++ b/tests/unit/persistence/test_decision_scope_basis.py
@@ -25,6 +25,7 @@ from repowise.core.analysis.decisions.scope import (
SCOPE_BASIS_STATED,
)
from repowise.core.persistence.crud import bulk_upsert_decisions, update_decision_metadata
+from repowise.core.persistence.crud.authority import accept_decision
from repowise.core.persistence.decision_graph import (
get_governed_nodes,
get_governing_decisions,
@@ -169,6 +170,30 @@ async def test_backfill_repairs_a_narrow_legacy_commit_record_too(async_session)
assert await get_governing_decisions(async_session, repo.id, files[0]) == []
+async def test_backfill_leaves_an_accepted_record_alone(async_session):
+ """A decision somebody confirmed keeps its files.
+
+ The repair cannot tell an accepted record by its basis: ``accept_decision``
+ writes ``stated`` only when the accepter passed an explicit scope, and
+ neither ``repowise decision confirm`` nor the web accept route does. So a
+ reviewer who confirmed a commit-derived decision as-is would have had its
+ file links deleted on the next index, and its hotspots would have started
+ reporting themselves ungoverned.
+ """
+ repo = await insert_repo(async_session)
+ payload = _decision("Confirmed as is", files=_COMMIT_FILES)
+ payload["evidence_file"] = _MECHANISM
+ ids = await bulk_upsert_decisions(async_session, repo.id, [payload])
+ rec = await async_session.get(DecisionRecord, ids[0])
+ # No ``scope=``, which is what both real accept paths do, so the basis is
+ # left empty and only the acceptance row marks the record as reviewed.
+ await accept_decision(async_session, rec, accepter="tester")
+ assert rec.scope_basis == ""
+
+ assert await backfill_scope_basis(async_session, repo.id) == 0
+ assert await get_governing_decisions(async_session, repo.id, _MECHANISM) != []
+
+
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.