Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
feat(decisions): key a decision on its evidence rather than on its title (#2425)
Raghav Chamadiyac69015b924b9
diff --git a/packages/core/alembic/versions/0069_decision_identity_quote.py b/packages/core/alembic/versions/0069_decision_identity_quote.pynew file mode 100644index 000000000..914748316--- /dev/null+++ b/packages/core/alembic/versions/0069_decision_identity_quote.py@@ -0,0 +1,52 @@+"""decision_records: pin the quote a decision's identity is keyed on.++A decision's id derives from its identity, and identity is now the evidence+rather than the title: the files it governs, the span it was read from, and+the quote it was captured with. The quote is the part a re-extraction can+change without the decision changing at all -- a model rewords the same+sentence and, with the quote re-derived each run, the id moves and every+reference held outside the row stops resolving.++So it is written once, at first capture, and never revised. This column is+where it lives. Empty on a record captured before it existed; the runtime id+migration backfills those from the strongest evidence row the record already+holds, which is the span it would have pinned had the column been there.++Local SQLite stores never run Alembic -- ``init_db``'s reconciler issues+additive DDL for missing columns, and an added non-nullable column with a+server default is exactly what it handles -- so this migration exists for+managed Postgres.++Revision ID: 0069+Revises: 0068+Create Date: 2026-09-19+"""++from __future__ import annotations++from collections.abc import Sequence++import sqlalchemy as sa+from alembic import op++# revision identifiers+revision: str = "0069"+down_revision: str | None = "0068"+branch_labels: str | Sequence[str] | None = None+depends_on: str | Sequence[str] | None = None+++def upgrade() -> None:+ op.add_column(+ "decision_records",+ sa.Column(+ "identity_quote",+ sa.Text(),+ nullable=False,+ server_default="",+ ),+ )+++def downgrade() -> None:+ op.drop_column("decision_records", "identity_quote")diff --git a/packages/core/src/repowise/core/persistence/crud/decisions.py b/packages/core/src/repowise/core/persistence/crud/decisions.pyindex 74d50342f..1ea108438 100644--- a/packages/core/src/repowise/core/persistence/crud/decisions.py+++ b/packages/core/src/repowise/core/persistence/crud/decisions.py@@ -8,6 +8,7 @@ from __future__ import annotationsimport hashlibimport json+from collections.abc import Iterablefrom datetime import datetimefrom typing import Any@@ -26,6 +27,7 @@ from repowise.core.analysis.decisions.provenance import (SOURCE_RANK,completeness,compute_confidence,+ normalize_text,rank_for_source,)@@ -53,16 +55,22 @@ from .authority import (# Namespaced so a decision id can never collide with another kind of derived# id, and versioned so a future change of the recipe is a visible new value# rather than a silent reshuffle of every id in every store.-_ID_NAMESPACE = "repowise.decision.id.v1"+_ID_NAMESPACE = "repowise.decision.id.v2"-# The four identity fields are joined with a separator none of them can-# contain, so no pair of distinct records can flatten to the same string.+# The identity fields are joined with a separator none of them can contain,+# so no pair of distinct records can flatten to the same string._FIELD_SEP = "\x00"# ``evidence_file`` is nullable and NULL is not the empty string here: the# dedupe query treats them as different records, so the derivation must too._NULL_EVIDENCE_FILE = "\x01"+# The pinned quote is truncated before it is hashed. A quote long enough to+# reach this is a paragraph, and its opening already separates two decisions+# that share a file set; carrying the rest only widens the surface on which a+# re-mine can reword the identity out from under a reference somebody wrote.+_IDENTITY_QUOTE_CHARS = 200+_VALID_DECISION_STATUSES = frozenset({"proposed", "active", "deprecated", "superseded", "dismissed"})@@ -135,46 +143,90 @@ def _dedup_query(return q.where(DecisionRecord.evidence_file.is_(None))+def identity_quote_for(text: str | None) -> str:+ """The pinned form of a decision's quote, for identity only.++ Whitespace-collapsed, lowercased and truncated, so the identity survives a+ re-mine that reflows or recases the same sentence. The stored column keeps+ the verbatim text; only this derived form is hashed.+ """+ return normalize_text(text)[:_IDENTITY_QUOTE_CHARS]++def derive_decision_id(repository_id: str,title: str,*,source: str,evidence_file: str | None,+ affected_files: Iterable[str] = (),+ evidence_line: int | None = None,+ identity_quote: str = "",+ needs_split: bool = False,) -> str:"""The id a decision with this identity has, in every store, on every run.- Deliberately the same four columns :func:`_dedup_query` matches on, and- kept beside it so the id and the dedupe key cannot drift apart. Those- columns are what ``uq_decision_record`` names, so this makes the primary- key agree with the identity the schema already declares rather than- inventing one. A random id, by contrast, is re-minted whenever a store is- rebuilt instead of updated, which strands every reference held outside the- row: an acceptance, an alias, a vector key, a link somebody wrote down.-- That constraint is weaker than it looks, and this is stricter than it:- ``evidence_file`` is nullable and SQL calls two NULLs distinct, so the- constraint does not fire for the majority of records, while two rows that- agree on all four do collapse to one id here. Every write path reaches an- insert only after a dedupe that would have found such a row, so the- stricter reading is not reachable from them; the migration classifies the- pairs a store already holds and leaves them alone.-- Note the id follows the identity, so editing one of the four moves it. The- migration at the head of each run is what settles that.-- 32 lowercase hex, because ``DecisionRecord.id`` is ``String(32)`` and so is- every foreign key to it, so a truncated digest fits without a column- change. ``evidence_file`` is NULL far more often than not, and NULL is a- distinct case in the dedupe query, so it gets a sentinel no path can- contain rather than collapsing into the empty string.+ Keyed on the evidence rather than on the title. A title is the most+ volatile thing a decision carries: two extractions of the same choice word+ it differently and produce two records, and rewording one moves its id.+ Over the dogfood store, title identity collapses **zero** duplicate pairs+ while the files-plus-evidence-plus-quote key below collapses **120 pairs+ across 42 classes**, and loses none of what title identity caught.++ The quote is load-bearing, and is why the file set alone is not the key:+ two unrelated classes in that store share a file pair and are separated+ only by what was said. It is also the fragile part, because most records+ key on mined prose rather than on a committed span, so it is **pinned in+ the row at first capture and never re-derived**. A later extraction that+ rewords the same sentence then leaves the id where it is.++ A record with no evidence at all keeps its title, for the same reason the+ rest of them lose it: identity is the evidence, and a record that has+ none has no evidence identity to key on. Without this every scopeless,+ quoteless record in a repository derives one id and the second one to be+ written collides with the first. Two of the 357 records in the dogfood+ store are in that state, and the construction paths that supply nothing+ but a title are all in that state.++ A record flagged ``needs_split`` keeps its title in the key. A bundled+ claim shares its files and its evidence with the separate decisions it+ bundles, so evidence identity would fold all of them together and file two+ decisions under a third one's name. Holding the flagged record on title+ identity is what keeps it apart, and it is why the flag has to be set+ before this key ships. The guard is partial: it reaches the bundles a+ marker can see and no others, measured at 28 of 357 records held out for+ 20 of 120 merges given up.++ ``source`` is deliberately **not** in the key. Two lanes that mined the+ same sentence out of the same files recorded one decision, not two, and+ the quote already separates two that merely share a scope.++ 32 lowercase hex, because ``DecisionRecord.id`` is ``String(32)`` and so+ is every foreign key to it, so a truncated digest fits without a column+ change. ``evidence_file`` is NULL far more often than not, so it gets a+ sentinel no path can contain rather than collapsing into the empty string.++ Note the id follows the identity, so editing the scope or the pinned quote+ moves it. :mod:`decision_id_migration`, which runs at the head of every+ index, is what settles that, and it leaves an alias behind so an id+ already written down keeps resolving."""+ files = sorted({str(f) for f in affected_files if f})+ quote = identity_quote_for(identity_quote)+ grounded = bool(files) or evidence_file is not None or bool(quote)parts = (_ID_NAMESPACE,repository_id,- title,- source,- _NULL_EVIDENCE_FILE if evidence_file is None else evidence_file,+ json.dumps(files),+ json.dumps(+ [+ _NULL_EVIDENCE_FILE if evidence_file is None else evidence_file,+ evidence_line,+ ]+ ),+ quote,+ title if needs_split or not grounded else "",+ source if not grounded else "",)digest = hashlib.sha256(_FIELD_SEP.join(parts).encode("utf-8")).hexdigest()return digest[:32]@@ -256,6 +308,37 @@ async def upsert_decision(# path takes no quote. ``verification`` still stores what it was given.confidence = compute_confidence(rank_for_source(source), 1, "exact")+ async def _restate(rec: DecisionRecord) -> DecisionRecord:+ """Write this call's body onto an existing record.++ Shared by the two ways of finding one: the title dedupe query, and+ the derived id, which catches a second wording of the same decision+ that the title query cannot see. ``identity_quote`` is not among the+ fields, because it is pinned at first capture.+ """+ rec.status = status+ if kind is not None:+ rec.kind = _extraction_kind(kind)+ rec.context = context+ rec.decision = decision+ rec.rationale = rationale+ rec.alternatives_json = json.dumps(alternatives or [])+ rec.consequences_json = json.dumps(consequences or [])+ rec.affected_files_json = json.dumps(affected_files or [])+ rec.affected_modules_json = json.dumps(affected_modules or [])+ rec.tags_json = json.dumps(tags or [])+ rec.evidence_commits_json = json.dumps(evidence_commits or [])+ rec.evidence_line = evidence_line+ rec.confidence = confidence+ rec.verification = verification+ rec.last_code_change = last_code_change+ rec.staleness_score = staleness_score+ rec.superseded_by = superseded_by+ rec.updated_at = _now_utc()+ await session.flush()+ await _write_candidate_meta(session, repository_id, {}, only={rec.id})+ return rec+q = _dedup_query(repository_id, title, source=source, evidence_file=evidence_file)@@ -269,36 +352,33 @@ async def upsert_decision(existing = result.scalar_one_or_none()if existing is not None:- existing.status = status- if kind is not None:- existing.kind = _extraction_kind(kind)- existing.context = context- existing.decision = decision- existing.rationale = rationale- existing.alternatives_json = json.dumps(alternatives or [])- existing.consequences_json = json.dumps(consequences or [])- existing.affected_files_json = json.dumps(affected_files or [])- existing.affected_modules_json = json.dumps(affected_modules or [])- existing.tags_json = json.dumps(tags or [])- existing.evidence_commits_json = json.dumps(evidence_commits or [])- existing.evidence_line = evidence_line- existing.confidence = confidence- existing.verification = verification- existing.last_code_change = last_code_change- existing.staleness_score = staleness_score- existing.superseded_by = superseded_by- existing.updated_at = _now_utc()- await session.flush()- await _write_candidate_meta(session, repository_id, {}, only={existing.id})- return existing+ return await _restate(existing)++ # No quote reaches this path: it is manual entry and the CLI's ``decision+ # add``, where the decision text is the only verbatim thing the person+ # wrote. Pinned once, like every other capture path.+ identity_quote = decision or title+ # An explicit id still wins: the manifest importer carries ids in from a+ # tracked file and those are the record's identity, not ours.+ derived = decision_id or derive_decision_id(+ repository_id,+ title,+ source=source,+ evidence_file=evidence_file,+ affected_files=affected_files or [],+ evidence_line=evidence_line,+ identity_quote=identity_quote,+ )+ # A title the dedupe query did not match can still be the same decision:+ # identity is the evidence, and two wordings of one choice derive one id.+ # Inserting over it would collide on the primary key.+ folded = await session.get(DecisionRecord, derived)+ if folded is not None and folded.repository_id == repository_id:+ return await _restate(folded)rec = DecisionRecord(- # An explicit id still wins: the manifest importer carries ids in from- # a tracked file and those are the record's identity, not ours.- id=decision_id- or derive_decision_id(- repository_id, title, source=source, evidence_file=evidence_file- ),+ id=derived,+ identity_quote=identity_quote,repository_id=repository_id,title=title,status=status,@@ -1222,16 +1302,38 @@ async def bulk_upsert_decisions(headline_title = headline.get("title", "")headline_source = headline.get("source", "cli")headline_evidence_file = headline.get("evidence_file")+ headline_files = headline.get("affected_files") or []+ # Pinned now and never revised: the identity is keyed on it, and a+ # later extraction rewording the same sentence must not move the id.+ headline_quote = _evidence_quote(headline)+ derived = derive_decision_id(+ repository_id,+ headline_title,+ source=headline_source,+ evidence_file=headline_evidence_file,+ affected_files=headline_files,+ evidence_line=headline.get("evidence_line"),+ identity_quote=headline_quote,+ needs_split=any(bool(d.get("needs_split")) for d in members),+ )+ # Identity is evidence, so two titles for one decision derive one+ # id. Folding into the record that id already names is the point of+ # the key: inserting instead would collide on the primary key, and+ # skipping would leave the duplicate this is meant to collapse.+ folded = id_to_rec.get(derived) or await session.get(DecisionRecord, derived)+ if folded is not None and folded.repository_id == repository_id:+ if folded.status == "dismissed":+ continue+ rec = folded+ existing_by_norm[norm] = rec+ id_to_rec[rec.id] = rec+ if rec is None:# A source that measures its own conformance supplies the score;# otherwise the git-diff recompute fills it in later.headline_staleness = headline.get("staleness_score")rec = DecisionRecord(- id=derive_decision_id(- repository_id,- headline_title,- source=headline_source,- evidence_file=headline_evidence_file,- ),+ id=derived,+ identity_quote=headline_quote,repository_id=repository_id,title=headline_title,status=_extraction_status(headline.get("status", "proposed")),@@ -1241,7 +1343,7 @@ async def bulk_upsert_decisions(rationale=headline.get("rationale") or "",alternatives_json=json.dumps(headline.get("alternatives") or []),consequences_json=json.dumps(headline.get("consequences") or []),- affected_files_json=json.dumps(headline.get("affected_files") or []),+ affected_files_json=json.dumps(headline_files),affected_modules_json=json.dumps(headline.get("affected_modules") or []),tags_json=json.dumps(headline.get("tags") or []),evidence_commits_json=json.dumps(headline.get("evidence_commits") or []),diff --git a/packages/core/src/repowise/core/persistence/decision_id_migration.py b/packages/core/src/repowise/core/persistence/decision_id_migration.pyindex 4e31ffbdc..f01d5b442 100644--- a/packages/core/src/repowise/core/persistence/decision_id_migration.py+++ b/packages/core/src/repowise/core/persistence/decision_id_migration.py@@ -21,6 +21,7 @@ derived and does nothing.from __future__ import annotations+import jsonfrom dataclasses import dataclass, fieldfrom typing import Any@@ -56,6 +57,45 @@ logger = structlog.get_logger(__name__)#: same decision rather than a different one that was folded into it.ALIAS_REASON = "rekeyed"+#: Written on the alias left behind by a fold. ``merged`` is right here in a+#: way it is not for a rekey: the old id names a record that no longer exists,+#: because another record was the same decision.+FOLD_REASON = "merged"++#: ``(table, column)`` pairs a fold drops instead of repointing, with the+#: reason. Everything else in :data:`_DEPENDENT_COLUMNS` moves to the keeper.+#:+#: Note the pair, not the table: ``decision_candidate_meta`` appears in+#: ``_DEPENDENT_COLUMNS`` twice, and only one of the two is a singleton.+#: ``decision_id`` is the table's primary key, so the loser's own review row+#: cannot move onto a keeper that already has one. ``merged_into`` is a plain+#: nullable column on some *other* candidate's row, and that candidate is+#: still alive; dropping its row would take a live decision's whole review+#: state with it, so it is repointed like anything else.+_DROPPED_ON_FOLD: frozenset[tuple[str, str]] = frozenset(+ {+ ("decision_candidate_meta", "decision_id"),+ # Keyed ``(decision_id, node_id, link_type)``, so the loser's links+ # would collide with the keeper's wherever the two governed the same+ # file. Dropping is safe because ``sync_decision_node_links`` rebuilds+ # a decision's links from ``affected_files_json`` on every index.+ ("decision_node_links", "decision_id"),+ }+)++#: Unique constraints a fold can walk into, as ``table -> the other columns+#: that make a row unique beside the decision id``. Two records that fold were+#: duplicates, so they are exactly the pair most likely to hold the same+#: evidence row, the same acceptance sequence, or the same edge. Repointing+#: blindly raises ``IntegrityError`` out of a migration that runs at the head+#: of every index, so the loser's row is dropped when the keeper already has+#: its equivalent and moved when it does not.+_FOLD_CONFLICT_KEYS: dict[str, tuple[str, ...]] = {+ "decision_evidence": ("source", "evidence_file", "evidence_commit"),+ "decision_acceptances": ("seq",),+ "decision_edges": ("src_decision_id", "dst_decision_id", "kind"),+}+#: Every column that carries a decision id, including the two that hold one#: without a foreign key and so are swept along by nothing the database does on#: its own.@@ -88,10 +128,16 @@ class IdMigrationPlan:"""The per-record outcomes for one repository."""rows: list[IdRowPlan] = field(default_factory=list)+ #: The quote each record's identity is keyed on, backfilled for records+ #: captured before the column existed so ``apply`` can write it down.+ pinned_quotes: dict[str, str] = field(default_factory=dict)def rewrites(self) -> list[IdRowPlan]:return [row for row in self.rows if row.outcome == "rewrite"]+ def folds(self) -> list[IdRowPlan]:+ return [row for row in self.rows if row.outcome == "fold"]+def counts(self) -> dict[str, int]:counts: dict[str, int] = {}for row in self.rows:@@ -104,10 +150,10 @@ async def plan_id_migration(session: AsyncSession, repository_id: str) -> IdMigrThree outcomes. ``stable`` is a record whose id already derives from itsown identity, which is what a second run sees for everything. ``rewrite``- is a move. ``collision`` is a record whose derived id is already spoken- for, and it is left exactly as it is: two records cannot share a primary- key, and picking a winner would be resolving a duplicate on the user's- behalf.+ is a move. ``fold`` is a record whose identity another record already+ holds, which under evidence-keyed identity is not a collision but the+ answer: the two are one decision worded twice, so the later one is merged+ into the earlier and leaves an alias where it was."""result = await session.execute(select(DecisionRecord)@@ -115,46 +161,109 @@ async def plan_id_migration(session: AsyncSession, repository_id: str) -> IdMigr.order_by(DecisionRecord.created_at, DecisionRecord.id))records = list(result.scalars().all())- existing_ids = {rec.id for rec in records}- claimed: dict[str, str] = {}- rows: list[IdRowPlan] = []-- for rec in records:- new_id = derive_decision_id(+ quotes = await _pinned_quotes(session, records)+ flagged = await _flagged_ids(session, repository_id)++ # Derived first, for every record, before anything is classified. A+ # record's outcome depends on what the others derive, not on what they+ # hold now, so a single pass in creation order would call the first+ # arrival a collision with an id its holder is about to vacate.+ derived = {+ rec.id: derive_decision_id(rec.repository_id,rec.title,source=rec.source,evidence_file=rec.evidence_file,+ affected_files=_json_list(rec.affected_files_json),+ evidence_line=rec.evidence_line,+ identity_quote=quotes.get(rec.id, ""),+ needs_split=rec.id in flagged,)- if new_id == rec.id:- rows.append(IdRowPlan(rec.id, new_id, rec.title, "stable"))- continue- if new_id in existing_ids:- rows.append(- IdRowPlan(- rec.id,- new_id,- rec.title,- "collision",- "derived id already belongs to another record",- )- )- continue- if new_id in claimed:+ for rec in records+ }+ # The record that keeps each derived id: the first in creation order. Its+ # title and its acceptance are the ones the folded group ends up under,+ # which is the oldest reading of the decision rather than the newest.+ keeper: dict[str, str] = {}+ for rec in records:+ keeper.setdefault(derived[rec.id], rec.id)++ rows: list[IdRowPlan] = []+ for rec in records:+ new_id = derived[rec.id]+ if keeper[new_id] != rec.id:rows.append(IdRowPlan(rec.id,new_id,rec.title,- "collision",- f"another record derives the same id ({claimed[new_id]})",+ "fold",+ f"same identity as {keeper[new_id]}",))- continue- claimed[new_id] = rec.id- rows.append(IdRowPlan(rec.id, new_id, rec.title, "rewrite"))+ elif new_id == rec.id:+ rows.append(IdRowPlan(rec.id, new_id, rec.title, "stable"))+ else:+ rows.append(IdRowPlan(rec.id, new_id, rec.title, "rewrite"))++ return IdMigrationPlan(rows=rows, pinned_quotes=quotes)+++def _json_list(raw: str | None) -> list[str]:+ try:+ value = json.loads(raw or "[]")+ except (TypeError, ValueError):+ return []+ return [str(v) for v in value] if isinstance(value, list) else []- return IdMigrationPlan(rows=rows)++async def _flagged_ids(session: AsyncSession, repository_id: str) -> set[str]:+ """Records whose review row says the claim bundles two decisions.++ Their identity keeps the title, so they are held out of the fold: a+ bundled claim shares files and evidence with the decisions it bundles,+ and folding them would file two decisions under a third one's name.+ """+ result = await session.execute(+ select(DecisionCandidateMeta.decision_id).where(+ DecisionCandidateMeta.repository_id == repository_id,+ DecisionCandidateMeta.needs_split.is_(True),+ )+ )+ return set(result.scalars().all())+++async def _pinned_quotes(+ session: AsyncSession, records: list[DecisionRecord]+) -> dict[str, str]:+ """The quote each record's identity is keyed on.++ A record captured since the column exists carries its own, written once.+ One captured before it has an empty column and is backfilled here from+ the strongest evidence row it already holds, which is the span it would+ have pinned had the column existed. Falling back to the decision text+ keeps a record with no evidence row from keying on the empty string,+ which would fold every such record together.+ """+ pinned = {rec.id: rec.identity_quote for rec in records if rec.identity_quote}+ missing = [rec for rec in records if not rec.identity_quote]+ if not missing:+ return pinned+ result = await session.execute(+ select(+ DecisionEvidence.decision_id,+ DecisionEvidence.source_quote,+ )+ .where(DecisionEvidence.decision_id.in_([rec.id for rec in missing]))+ .order_by(DecisionEvidence.source_rank.desc(), DecisionEvidence.id)+ )+ strongest: dict[str, str] = {}+ for decision_id, quote in result.all():+ if quote and decision_id not in strongest:+ strongest[decision_id] = quote+ for rec in missing:+ pinned[rec.id] = strongest.get(rec.id) or rec.decision or rec.title+ return pinneddef _table_names(sync_connection: Any) -> set[str]:@@ -241,6 +350,104 @@ async def _rewrite_one()+async def _fold_one(+ session: AsyncSession,+ repository_id: str,+ row: IdRowPlan,+ tables: set[str],+) -> None:+ """Merge one record into the record its identity already names.++ The dependents move first and the record is released last, for the same+ reason a rewrite copies before it deletes: a foreign key cascades on+ delete and does nothing on update, so a row pointed at a decision that+ stops existing goes with it.++ Two records only fold because they were duplicates, which makes them the+ pair most likely to hold the same evidence row, the same acceptance+ sequence, or the same edge. So a row the keeper already has an equivalent+ of is dropped rather than moved: repointing it would raise on the unique+ constraint, out of a migration that runs at the head of every index.+ """+ for table, column in _DEPENDENT_COLUMNS:+ if table not in tables:+ continue+ if (table, column) in _DROPPED_ON_FOLD:+ await session.execute(+ _sql_text(f"DELETE FROM {table} WHERE {column} = :old_id"),+ {"old_id": row.old_id},+ )+ continue+ conflict = _FOLD_CONFLICT_KEYS.get(table)+ if conflict:+ await _drop_rows_the_keeper_already_has(session, table, column, conflict, row)+ await session.execute(+ _sql_text(f"UPDATE {table} SET {column} = :new_id WHERE {column} = :old_id"),+ {"new_id": row.new_id, "old_id": row.old_id},+ )++ await session.execute(+ _sql_text("DELETE FROM decision_records WHERE id = :old_id"),+ {"old_id": row.old_id},+ )++ if "decision_aliases" not in tables:+ return+ if await session.get(DecisionAlias, row.old_id) is not None:+ return+ session.add(+ DecisionAlias(+ alias_id=row.old_id,+ repository_id=repository_id,+ decision_id=row.new_id,+ reason=FOLD_REASON,+ created_at=_now_utc(),+ )+ )+++async def _drop_rows_the_keeper_already_has(+ session: AsyncSession,+ table: str,+ column: str,+ conflict: tuple[str, ...],+ row: IdRowPlan,+) -> None:+ """Delete the loser's rows that would collide once they are repointed.++ Compares on the columns that make a row unique beside the decision id.+ ``IS NOT DISTINCT FROM`` is not available on SQLite and those columns are+ nullable, so the comparison is spelled out: equal, or both absent.++ An edge between the two records being folded is dropped as well. Once they+ are one record it points at itself, which is a relationship the graph has+ no reading for.+ """+ others = [name for name in conflict if name != column]+ matches = "".join(+ f" AND (keeper.{name} = {table}.{name}"+ f" OR (keeper.{name} IS NULL AND {table}.{name} IS NULL))"+ for name in others+ )+ await session.execute(+ _sql_text(+ f"DELETE FROM {table} WHERE {column} = :old_id AND EXISTS ("+ f" SELECT 1 FROM {table} AS keeper"+ f" WHERE keeper.{column} = :new_id{matches})"+ ),+ {"new_id": row.new_id, "old_id": row.old_id},+ )+ for name in others:+ if not name.endswith("decision_id"):+ continue+ await session.execute(+ _sql_text(+ f"DELETE FROM {table} WHERE {column} = :old_id AND {name} = :new_id"+ ),+ {"new_id": row.new_id, "old_id": row.old_id},+ )++def _detach_moved(session: AsyncSession) -> None:"""Drop every loaded decision row from the session's identity map.@@ -325,14 +532,29 @@ async def apply_id_migration(plan: IdMigrationPlan | None = None,vector_store: Any = None,) -> IdMigrationPlan:- """Move every rewritable record onto its derived id, and re-key its vector.+ """Settle every record onto its derived id, and re-key its vector.- Idempotent, because the plan classifies an already-derived id as ``stable``- and this touches nothing else.+ Three things happen, in this order, and the order is forced. The pinned+ quote is written down first, so the identity a record derives stops+ depending on a backfill being recomputed the same way next run. Rewrites+ come next, so every keeper is sitting on its derived id. Folds come last,+ because a fold points dependents at the keeper's *new* id.++ Idempotent: a second run classifies every id as ``stable``, finds no+ rewrites and no folds, and touches nothing."""plan = plan or await plan_id_migration(session, repository_id)rewrites = plan.rewrites()- if not rewrites:+ folds = plan.folds()++ # Written even when nothing moves: a store whose ids already derive can+ # still be carrying records that have never pinned their quote, and the+ # pin is what makes the next re-extraction leave those ids alone.+ pinned = await _pin_quotes(session, plan)++ if not rewrites and not folds:+ if pinned:+ await session.flush()return plan# The rewrites are raw SQL, so a record already loaded keeps the id it was@@ -349,21 +571,72 @@ async def apply_id_migration(# and that the next run would faithfully rekey rather than clean.async with session.begin_nested():await _rewrite_one(session, repository_id, row, tables)+ for row in folds:+ async with session.begin_nested():+ await _fold_one(session, repository_id, row, tables)await session.flush()_detach_moved(session)rekeyed = 0+ dropped = 0if vector_store is not None:rekeyed = await _rekey_vectors(vector_store, session, rewrites)+ dropped = await _drop_folded_vectors(vector_store, folds)- # Collisions are left where they are, so name them rather than letting a- # count imply everything moved.- collisions = [row for row in plan.rows if row.outcome == "collision"]logger.info("decision_ids_derived",repository_id=repository_id,rewritten=len(rewrites),+ folded=len(folds),+ quotes_pinned=pinned,vectors_rekeyed=rekeyed,- unchanged_collisions=[row.old_id for row in collisions],+ vectors_dropped=dropped,)return plan+++async def _pin_quotes(session: AsyncSession, plan: IdMigrationPlan) -> int:+ """Write each record's identity quote into the row it belongs to.++ The plan derived it, from the strongest evidence row for anything+ captured before the column existed. Storing it is what stops the+ derivation from depending on evidence that a later run may have accreted+ more of: the pin is the point, and a pin nobody writes down is a+ recomputation.+ """+ written = 0+ for row in plan.rows:+ if row.outcome == "fold":+ continue+ quote = plan.pinned_quotes.get(row.old_id)+ if not quote:+ continue+ rec = await session.get(DecisionRecord, row.old_id)+ if rec is not None and not rec.identity_quote:+ rec.identity_quote = quote+ written += 1+ return written+++async def _drop_folded_vectors(vector_store: Any, folds: list[IdRowPlan]) -> int:+ """Delete the vector of every record a fold removed.++ The keeper's vector stays and still describes the decision. The loser's+ describes a record that no longer exists, so leaving it would let search+ return an id nothing resolves to.+ """+ if not folds:+ return 0+ try:+ present = await vector_store.list_page_ids()+ except Exception:+ return 0+ stale = [+ f"{DECISION_VECTOR_PREFIX}{row.old_id}"+ for row in folds+ if f"{DECISION_VECTOR_PREFIX}{row.old_id}" in present+ ]+ if not stale:+ return 0+ await vector_store.delete_many(sorted(stale))+ return len(stale)diff --git a/packages/core/src/repowise/core/persistence/models.py b/packages/core/src/repowise/core/persistence/models.pyindex 6d6c5225e..4e6d06e6e 100644--- a/packages/core/src/repowise/core/persistence/models.py+++ b/packages/core/src/repowise/core/persistence/models.py@@ -11,6 +11,7 @@ repowise.core.ingestion.models.Symbol in files that import from both modules.from __future__ import annotations+import jsonfrom datetime import UTC, datetimefrom typing import Anyfrom uuid import uuid4@@ -66,6 +67,16 @@ def _derive_decision_id_default(context: Any) -> str:params.get("title") or "",source=params.get("source") or DEFAULT_DECISION_SOURCE,evidence_file=params.get("evidence_file"),+ affected_files=json.loads(params.get("affected_files_json") or "[]"),+ evidence_line=params.get("evidence_line"),+ # The same fallback ``upsert_decision`` uses: a path that supplies no+ # quote still pins something, and the two agree on what.+ identity_quote=(+ params.get("identity_quote")+ or params.get("decision")+ or params.get("title")+ or ""+ ),)@@ -933,6 +944,15 @@ class DecisionRecord(Base):) # git_archaeology | inline_marker | adr | pr | comment | session | clievidence_file: Mapped[str | None] = mapped_column(Text, nullable=True)evidence_line: Mapped[int | None] = mapped_column(Integer, nullable=True)+ #: The verbatim span this record's identity is keyed on, written once+ #: at first capture and never revised. Re-extraction rewords a quote+ #: freely, and the id follows the identity, so re-deriving this would+ #: move the id of an unchanged decision every time a model phrased it+ #: differently. Empty on a record captured before the column existed;+ #: the id migration fills it from the strongest evidence row it holds.+ identity_quote: Mapped[str] = mapped_column(+ Text, nullable=False, default="", server_default=""+ )confidence: Mapped[float] = mapped_column(Float, nullable=False, default=1.0)# Verification (anti-hallucination gate, Phase 1D). Aggregate over thediff --git a/tests/unit/generation/test_editor_file_fetcher.py b/tests/unit/generation/test_editor_file_fetcher.pyindex 88c03eaeb..46269153a 100644--- a/tests/unit/generation/test_editor_file_fetcher.py+++ b/tests/unit/generation/test_editor_file_fetcher.py@@ -149,7 +149,9 @@ async def _add_decision(session, repo_id, title, status="active", rationale="Somtitle=title,status=status,rationale=rationale,- decision="Decided to use X",+ # Varies with the title: identity is the evidence, so two titles+ # over one body and one file are one decision.+ decision=f"Decided to use X, for {title}",context="Context here",source="inline_marker",affected_files_json='["src/app.py"]',diff --git a/tests/unit/persistence/test_decision_evidence.py b/tests/unit/persistence/test_decision_evidence.pyindex 39997b49e..dcc44b1f3 100644--- a/tests/unit/persistence/test_decision_evidence.py+++ b/tests/unit/persistence/test_decision_evidence.py@@ -15,10 +15,10 @@ from repowise.core.persistence.models import DecisionRecordfrom tests.unit.persistence.helpers import insert_repo-def _adr_dict(title="Use PostgreSQL for storage"):+def _adr_dict(title="Use PostgreSQL for storage", decision="Use PostgreSQL as the primary datastore"):return {"title": title,- "decision": "Use PostgreSQL as the primary datastore",+ "decision": decision,"rationale": "Strong transactional guarantees","source": "adr","status": "active",@@ -28,7 +28,7 @@ def _adr_dict(title="Use PostgreSQL for storage"):"affected_files": ["src/storage/db.py"],"confidence": 0.9,"verification": "exact",- "source_quote": "Use PostgreSQL as the primary datastore",+ "source_quote": decision,}@@ -136,7 +136,12 @@ async def test_distinct_decisions_stay_separate(async_session):await bulk_upsert_decisions(async_session,repo.id,- [_adr_dict(title="Use PostgreSQL"), _adr_dict(title="Adopt gRPC internally")],+ [+ _adr_dict(title="Use PostgreSQL"),+ # Its own decision and quote, not only its own title: identity is+ # the evidence, so two titles over one body are one decision.+ _adr_dict(title="Adopt gRPC internally", decision="Adopt gRPC for internal calls"),+ ],)rows = await _decision_rows(async_session, repo.id)assert len(rows) == 2diff --git a/tests/unit/persistence/test_decision_stable_ids.py b/tests/unit/persistence/test_decision_stable_ids.pyindex 5e27cadd7..be2d449a1 100644--- a/tests/unit/persistence/test_decision_stable_ids.py+++ b/tests/unit/persistence/test_decision_stable_ids.py@@ -1,13 +1,18 @@"""A decision's id derives from its identity, so a rebuild does not move it.-Two contracts. The id of a record is a function of the four columns-``uq_decision_record`` already declares unique, at every site that mints one;-and a store whose records predate that keeps working, because the migration-moves them rather than re-minting them, and leaves an alias behind.+Identity is the evidence: the files a decision governs, the span it was read+from, and the quote pinned when it was first captured. Not the title, which+two extractions of one choice word differently. Three contracts follow. Every+site that mints an id derives the same one; two records that are the same+decision worded twice fold into one, and the id of the one that goes away+keeps resolving; and a record flagged as bundling two decisions is held out of+that fold."""from __future__ import annotations+import json+import pytestfrom sqlalchemy import func, selectfrom sqlalchemy import text as sql_text@@ -19,11 +24,13 @@ from repowise.core.persistence.crud.decisions import derive_decision_idfrom repowise.core.persistence.decision_graph import sync_decision_node_linksfrom repowise.core.persistence.decision_id_migration import (ALIAS_REASON,+ FOLD_REASON,apply_id_migration,plan_id_migration,)from repowise.core.persistence.models import (DecisionAlias,+ DecisionCandidateMeta,DecisionEvidence,DecisionNodeLink,DecisionRecord,@@ -51,25 +58,98 @@ def _dict(title: str, **overrides) -> dict:return base+def _derived_for(rec: DecisionRecord) -> str:+ """The id *rec* would be minted with, read off the row it is stored in."""+ return derive_decision_id(+ rec.repository_id,+ rec.title,+ source=rec.source,+ evidence_file=rec.evidence_file,+ affected_files=json.loads(rec.affected_files_json or "[]"),+ evidence_line=rec.evidence_line,+ identity_quote=rec.identity_quote,+ )++# ---------------------------------------------------------------------------# The derivation itself# ----------------------------------------------------------------------------def test_the_id_is_a_function_of_the_four_columns_the_schema_calls_unique():- args = ("repo1", "Prefer the boring option")- kwargs = {"source": "session", "evidence_file": "src/app.py"}- first = derive_decision_id(*args, **kwargs)+_KEY = {+ "source": "session",+ "evidence_file": "src/app.py",+ "evidence_line": 12,+ "affected_files": ["src/app.py", "src/b.py"],+ "identity_quote": "we chose sqlite because hook writes must not contend",+}- assert first == derive_decision_id(*args, **kwargs)++def test_the_id_is_a_function_of_the_evidence():+ first = derive_decision_id("repo1", "Prefer the boring option", **_KEY)++ assert first == derive_decision_id("repo1", "Prefer the boring option", **_KEY)assert len(first) == 32assert all(char in "0123456789abcdef" for char in first)- # Each of the four is load-bearing.- assert first != derive_decision_id("repo2", args[1], **kwargs)- assert first != derive_decision_id(args[0], "Another title", **kwargs)- assert first != derive_decision_id(*args, source="pr", evidence_file="src/app.py")- assert first != derive_decision_id(*args, source="session", evidence_file="src/b.py")+ assert first != derive_decision_id("repo2", "Prefer the boring option", **_KEY)+ assert first != derive_decision_id(+ "repo1", "Prefer the boring option", **{**_KEY, "evidence_file": "src/b.py"}+ )+ assert first != derive_decision_id(+ "repo1", "Prefer the boring option", **{**_KEY, "evidence_line": 13}+ )+ assert first != derive_decision_id(+ "repo1", "Prefer the boring option", **{**_KEY, "affected_files": ["src/app.py"]}+ )+ assert first != derive_decision_id(+ "repo1", "Prefer the boring option", **{**_KEY, "identity_quote": "something else"}+ )+++def test_the_title_leaves_the_key():+ """The whole change. Two wordings of one choice are one decision."""+ assert derive_decision_id("repo1", "Prefer the boring option", **_KEY) == (+ derive_decision_id("repo1", "Choose the dull option", **_KEY)+ )+++def test_the_lane_that_mined_it_leaves_the_key_too():+ """Two lanes reading the same sentence recorded one decision, not two."""+ assert derive_decision_id("repo1", "T", **_KEY) == derive_decision_id(+ "repo1", "T", **{**_KEY, "source": "pr"}+ )+++def test_the_scope_is_a_set_rather_than_a_list():+ """Order and repetition are how a scope was assembled, not what it is."""+ assert derive_decision_id(+ "repo1", "T", **{**_KEY, "affected_files": ["src/b.py", "src/app.py"]}+ ) == derive_decision_id(+ "repo1", "T", **{**_KEY, "affected_files": ["src/app.py", "src/b.py", "src/app.py"]}+ )+++def test_a_reworded_quote_does_not_move_the_id():+ """Only whitespace and case; the pin is what handles a real rewording."""+ assert derive_decision_id("repo1", "T", **_KEY) == derive_decision_id(+ "repo1",+ "T",+ **{**_KEY, "identity_quote": " We Chose SQLite because hook writes" + chr(10) + " must not contend"},+ )+++def test_a_bundled_claim_keeps_its_title_in_the_key():+ """Held out of the fold: it shares evidence with what it bundles."""+ plain = derive_decision_id("repo1", "Enable WAL; bound the busy timeout", **_KEY)+ flagged = derive_decision_id(+ "repo1", "Enable WAL; bound the busy timeout", **_KEY, needs_split=True+ )+ assert plain != flagged+ # And two flagged claims over the same evidence stay apart by title.+ assert flagged != derive_decision_id(+ "repo1", "Enable WAL only", **_KEY, needs_split=True+ )def test_a_null_evidence_file_is_not_an_empty_one():@@ -90,7 +170,11 @@ async def test_upsert_derives_the_id_it_stores(async_session):evidence_file="src/app.py",)assert rec.id == derive_decision_id(- repo.id, "Prefer the boring option", source="cli", evidence_file="src/app.py"+ repo.id,+ "Prefer the boring option",+ source="cli",+ evidence_file="src/app.py",+ identity_quote=rec.identity_quote,)@@ -103,9 +187,7 @@ async def test_bulk_upsert_derives_the_id_it_stores(async_session):select(DecisionRecord).where(DecisionRecord.repository_id == repo.id))).scalar_one()- assert rec.id == derive_decision_id(- repo.id, rec.title, source=rec.source, evidence_file=rec.evidence_file- )+ assert rec.id == _derived_for(rec)@pytest.mark.asyncio@@ -117,12 +199,21 @@ async def test_a_record_built_anywhere_else_still_derives_its_id(async_session):title="Built without an explicit id",source="git_archaeology",evidence_file=None,+ decision="keep the loader separate",+ affected_files_json='["src/app.py"]',)async_session.add(rec)await async_session.flush()+ # The default pins the decision text, the same fallback ``upsert_decision``+ # uses, so the two paths cannot derive different ids for one record.assert rec.id == derive_decision_id(- repo.id, "Built without an explicit id", source="git_archaeology", evidence_file=None+ repo.id,+ "Built without an explicit id",+ source="git_archaeology",+ evidence_file=None,+ affected_files=["src/app.py"],+ identity_quote="keep the loader separate",)@@ -234,7 +325,11 @@ async def test_the_migration_moves_the_record_and_takes_its_dependents_with_it(aawait async_session.flush()old_id = legacy.idnew_id = derive_decision_id(- repo.id, "Legacy one", source="session", evidence_file="src/app.py"+ repo.id,+ "Legacy one",+ source="session",+ evidence_file="src/app.py",+ identity_quote="q",)await apply_id_migration(async_session, repo.id)@@ -309,35 +404,210 @@ async def test_a_second_run_finds_every_id_already_derived(async_session):@pytest.mark.asyncio-async def test_two_records_that_derive_the_same_id_keep_the_second_one(async_session):- """A real collision, and neither record is ours to discard.+async def test_two_records_with_one_identity_fold_into_one(async_session):+ """The point of evidence identity: one decision, worded twice.++ Under title identity these stayed two records forever, because nothing+ but a person ever noticed they were the same. The later one merges into+ the earlier and leaves an alias, so an id written down still resolves.+ """+ repo = await insert_repo(async_session)+ first = await _legacy_record(async_session, repo.id, "Cache the parse tree", id="a" * 32)+ second = await _legacy_record(async_session, repo.id, "Reuse the parsed tree", id="b" * 32)+ # Same evidence, same scope, same quote: one decision said twice.+ for rec in (first, second):+ rec.decision = "cache the parse tree because reparsing dominated the tail"+ async_session.add(+ DecisionEvidence(+ decision_id=rec.id,+ source="session",+ evidence_file="src/app.py",+ source_quote="reparsing dominated the tail",+ )+ )+ await async_session.flush()++ plan = await apply_id_migration(async_session, repo.id)++ assert plan.counts() == {"rewrite": 1, "fold": 1}+ kept = derive_decision_id(+ repo.id,+ "Cache the parse tree",+ source="session",+ evidence_file="src/app.py",+ identity_quote="reparsing dominated the tail",+ )+ assert plan.rewrites()[0].new_id == kept+ assert (await async_session.get(DecisionRecord, kept)) is not None+ assert (await async_session.execute(select(func.count(DecisionRecord.id)))).scalar_one() == 1+ # The oldest reading is the one the group ends up under.+ assert (await async_session.get(DecisionRecord, kept)).title == "Cache the parse tree"+ # The evidence the loser carried came with it.+ assert set(+ (await async_session.execute(select(DecisionEvidence.decision_id))).scalars().all()+ ) == {kept}+ # And the id that went away still resolves.+ alias = await async_session.get(DecisionAlias, "b" * 32)+ assert alias is not None and alias.reason == FOLD_REASON+ assert await resolve_decision_id(async_session, "b" * 32) == kept+++async def test_a_fold_drops_the_evidence_the_keeper_already_has(async_session):+ """Two records only fold because they were duplicates.++ That makes them the pair most likely to hold the same evidence row, and+ ``uq_decision_evidence`` names ``(decision_id, source, evidence_file,+ evidence_commit)``. Repointing blindly raises out of a migration that runs+ at the head of every index.+ """+ repo = await insert_repo(async_session)+ first = await _legacy_record(async_session, repo.id, "Cache the parse tree", id="a" * 32)+ second = await _legacy_record(async_session, repo.id, "Reuse the parsed tree", id="b" * 32)+ for rec in (first, second):+ rec.decision = "cache the parse tree because reparsing dominated the tail"+ async_session.add(+ DecisionEvidence(+ decision_id=rec.id,+ source="session",+ source_rank=8,+ evidence_file="src/app.py",+ evidence_commit="c0ffee",+ source_quote="reparsing dominated the tail",+ )+ )+ # A second row the keeper has no equivalent of, which must survive.+ async_session.add(+ DecisionEvidence(+ decision_id=rec.id,+ source="pr",+ evidence_file=f"src/{rec.id[0]}.py",+ source_quote="a distinct span",+ )+ )+ await async_session.flush()++ plan = await apply_id_migration(async_session, repo.id)++ assert plan.counts() == {"rewrite": 1, "fold": 1}+ kept = plan.rewrites()[0].new_id+ rows = (+ await async_session.execute(+ select(DecisionEvidence.source, DecisionEvidence.evidence_file).where(+ DecisionEvidence.decision_id == kept+ )+ )+ ).all()+ # The colliding pair collapsed to one; the two distinct rows both survived.+ assert sorted(rows) == [+ ("pr", "src/a.py"),+ ("pr", "src/b.py"),+ ("session", "src/app.py"),+ ]+++async def test_a_fold_keeps_the_review_row_of_a_candidate_merged_into_the_loser(async_session):+ """``merged_into`` is another live candidate's row, not the loser's own.- ``uq_decision_record`` does not fire when ``evidence_file`` is NULL,- because SQL calls two NULLs distinct, so a store can already hold two rows- that derive one id. The first moves; the second stays exactly where it is- rather than being folded into it.+ Deleting it would take a decision that still exists out of review+ entirely, and that decision was never part of the fold."""repo = await insert_repo(async_session)- first = await _legacy_record(- async_session, repo.id, "Same identity", id="a" * 32, evidence_file=None+ first = await _legacy_record(async_session, repo.id, "Cache the parse tree", id="a" * 32)+ second = await _legacy_record(async_session, repo.id, "Reuse the parsed tree", id="b" * 32)+ bystander = await _legacy_record(+ async_session, repo.id, "Something else entirely", id="c" * 32)- second = await _legacy_record(- async_session, repo.id, "Same identity", id="b" * 32, evidence_file=None+ for rec in (first, second):+ rec.decision = "cache the parse tree because reparsing dominated the tail"+ bystander.decision = "an unrelated decision about an unrelated file"+ async_session.add(+ DecisionCandidateMeta(+ decision_id=bystander.id,+ repository_id=repo.id,+ review_state="merged",+ merged_into=second.id,+ ))- derived = derive_decision_id(- repo.id, "Same identity", source="session", evidence_file=None+ await async_session.flush()++ plan = await apply_id_migration(async_session, repo.id)+ kept = next(r.new_id for r in plan.rewrites() if r.old_id == "a" * 32)+ moved_bystander = next(r.new_id for r in plan.rewrites() if r.old_id == "c" * 32)++ meta = await async_session.get(DecisionCandidateMeta, moved_bystander)+ assert meta is not None, "the bystander's review row was deleted with the fold"+ assert meta.review_state == "merged"+ # And it now names the record the merge target folded into.+ assert meta.merged_into == kept+++async def test_a_bundled_claim_is_held_out_of_the_fold(async_session):+ """A bundle shares its evidence with the decisions it bundles.++ Folding them would file two decisions under a third one's name, so the+ flagged record keeps its title in the key and stays its own record.+ """+ repo = await insert_repo(async_session)+ first = await _legacy_record(async_session, repo.id, "Enable WAL", id="a" * 32)+ bundle = await _legacy_record(+ async_session, repo.id, "Enable WAL; bound the busy timeout", id="b" * 32+ )+ for rec in (first, bundle):+ rec.decision = "enable wal and bound the busy timeout"+ async_session.add(+ DecisionCandidateMeta(decision_id=bundle.id, repository_id=repo.id, needs_split=True))+ await async_session.flush()plan = await apply_id_migration(async_session, repo.id)- assert plan.counts() == {"rewrite": 1, "collision": 1}- assert (await async_session.get(DecisionRecord, derived)) is not None- # The loser kept its row and its id; nothing was merged away.- survivor = second.id if first.id == "a" * 32 else first.id- assert (await async_session.get(DecisionRecord, survivor)) is not None- assert (- await async_session.execute(select(func.count(DecisionRecord.id)))- ).scalar_one() == 2+ assert plan.counts() == {"rewrite": 2}+ assert (await async_session.execute(select(func.count(DecisionRecord.id)))).scalar_one() == 2+++async def test_the_quote_is_pinned_rather_than_re_derived(async_session):+ """A later extraction rewording the same sentence must not move the id."""+ repo = await insert_repo(async_session)+ legacy = await _legacy_record(async_session, repo.id, "Legacy one")+ async_session.add(+ DecisionEvidence(+ decision_id=legacy.id,+ source="session",+ evidence_file="src/app.py",+ source_quote="the original wording",+ )+ )+ await async_session.flush()++ await apply_id_migration(async_session, repo.id)+ moved = (+ await async_session.execute(+ select(DecisionRecord).where(DecisionRecord.repository_id == repo.id)+ )+ ).scalar_one()+ pinned_id, pinned_quote = moved.id, moved.identity_quote+ assert pinned_quote == "the original wording"++ # Re-mine the same decision under a rewritten quote.+ async_session.add(+ DecisionEvidence(+ decision_id=moved.id,+ source="session",+ evidence_file="src/app.py",+ source_quote="a completely different phrasing of the same thing",+ )+ )+ await async_session.flush()++ second = await apply_id_migration(async_session, repo.id)++ assert second.counts() == {"stable": 1}+ again = await async_session.get(DecisionRecord, pinned_id)+ assert again is not None and again.identity_quote == pinned_quote@pytest.mark.asyncio@@ -376,7 +646,11 @@ async def test_the_decision_vectors_move_to_the_new_key(async_session, in_memory{"title": "Legacy one", "page_type": "decision_record"},)new_id = derive_decision_id(- repo.id, "Legacy one", source="session", evidence_file="src/app.py"+ repo.id,+ "Legacy one",+ source="session",+ evidence_file="src/app.py",+ identity_quote="Legacy one: do the thing",)await apply_id_migration(async_session, repo.id, vector_store=in_memory_vector_store)@@ -407,6 +681,14 @@ async def test_a_merge_alias_survives_the_move(async_session):await async_session.flush()folded_id, target_id = folded.id, target.id+ survivor_new = derive_decision_id(+ repo.id,+ "The survivor",+ source="session",+ evidence_file="src/app.py",+ identity_quote="The survivor: do the thing",+ )+await apply_id_migration(async_session, repo.id)alias = await async_session.get(DecisionAlias, folded_id)@@ -414,9 +696,7 @@ async def test_a_merge_alias_survives_the_move(async_session):# Still points at the survivor, which itself moved, rather than at the# candidate that was folded away.assert alias.decision_id != folded_id- assert alias.decision_id == derive_decision_id(- repo.id, "The survivor", source="session", evidence_file="src/app.py"- )+ assert alias.decision_id == survivor_newassert target_id != alias.decision_iddiff --git a/tests/unit/server/test_decisions_alias_resolution.py b/tests/unit/server/test_decisions_alias_resolution.pyindex bc87f7a41..cc3927588 100644--- a/tests/unit/server/test_decisions_alias_resolution.py+++ b/tests/unit/server/test_decisions_alias_resolution.py@@ -26,7 +26,9 @@ async def _record(session_factory, repo_id: str, title: str) -> str:title=title,status="proposed",context="ctx",- decision="dec",+ # Distinct per seed: identity is the evidence, so ten records+ # sharing one body over one file are one decision, not ten.+ decision=f"dec for {title}",rationale="why",source="inline_marker",affected_files=["src/app.py"],diff --git a/tests/unit/server/test_decisions_api.py b/tests/unit/server/test_decisions_api.pyindex 41fbf3521..9167313cb 100644--- a/tests/unit/server/test_decisions_api.py+++ b/tests/unit/server/test_decisions_api.py@@ -39,7 +39,9 @@ async def _seed_decision(title=title,status=status,context="Need zero-config storage for CI.",- decision="Use aiosqlite in-memory DBs.",+ # Varies with the title: identity is the evidence, so two+ # titles over one body and one file are one decision.+ decision=f"Use aiosqlite in-memory DBs, for {title}.",rationale="Fast, no external deps.",affected_modules=["packages/core"],affected_files=["packages/core/src/repowise/core/persistence/database.py"],diff --git a/tests/unit/server/test_decisions_counts.py b/tests/unit/server/test_decisions_counts.pyindex 7a0595469..a097310a9 100644--- a/tests/unit/server/test_decisions_counts.py+++ b/tests/unit/server/test_decisions_counts.py@@ -31,7 +31,9 @@ async def _seed(title=title,status=status,context="ctx",- decision="dec",+ # Distinct per seed: identity is the evidence, so ten records+ # sharing one body over one file are one decision, not ten.+ decision=f"dec for {title}",rationale="why",source=source,affected_files=["src/app.py"],diff --git a/tests/unit/server/test_decisions_lanes.py b/tests/unit/server/test_decisions_lanes.pyindex 372fe6fae..547f7bf26 100644--- a/tests/unit/server/test_decisions_lanes.py+++ b/tests/unit/server/test_decisions_lanes.py@@ -35,7 +35,9 @@ async def _seed(title=title,status=status,context="ctx",- decision="dec",+ # Distinct per seed: identity is the evidence, so ten records+ # sharing one body over one file are one decision, not ten.+ decision=f"dec for {title}",rationale="why",source="inline_marker",affected_files=["src/app.py"] if scope is None else scope,diff --git a/tests/unit/server/test_mcp_decision_graph.py b/tests/unit/server/test_mcp_decision_graph.pyindex f83f21cd7..3238016c6 100644--- a/tests/unit/server/test_mcp_decision_graph.py+++ b/tests/unit/server/test_mcp_decision_graph.py@@ -515,7 +515,9 @@ async def test_get_context_decision_titles_capped_at_three(setup_mcp_decisions,title=f"Extra decision {i}",status="active",context="extra",- decision="extra",+ # Its own body: identity is the evidence, so four records sharing+ # one body over one file would be one decision.+ decision=f"extra {i}",rationale="extra",alternatives_json="[]",consequences_json="[]",