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 id migration copying a record onto an occupied id (#2613)

Raghav Chamadiyad03362bd70e9
diff --git a/packages/core/src/repowise/core/persistence/decision_id_migration.py b/packages/core/src/repowise/core/persistence/decision_id_migration.py
index f01d5b442..6cde39459 100644
--- a/packages/core/src/repowise/core/persistence/decision_id_migration.py
+++ b/packages/core/src/repowise/core/persistence/decision_id_migration.py
@@ -181,12 +181,16 @@ async def plan_id_migration(session: AsyncSession, repository_id: str) -> IdMigr
)
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.
+ # The record that keeps each derived id: the first in creation order, so
+ # the folded group ends up under the oldest reading of the decision. A
+ # record already sitting on the id keeps it instead, because nothing can
+ # be copied onto an id that is still occupied.
keeper: dict[str, str] = {}
for rec in records:
keeper.setdefault(derived[rec.id], rec.id)
+ for new_id in keeper:
+ if derived.get(new_id) == new_id:
+ keeper[new_id] = new_id
rows: list[IdRowPlan] = []
for rec in records:
@@ -281,13 +285,19 @@ async def _existing_tables(session: AsyncSession) -> set[str]:
return await connection.run_sync(_table_names)
-async def _rewrite_one(
+def _parking_id(old_id: str) -> str:
+ """A temporary id of the same length that no hex id can equal."""
+ return f"~{old_id[1:]}"
+
+
+async def _move(
session: AsyncSession,
- repository_id: str,
- row: IdRowPlan,
+ from_id: str,
+ to_id: str,
+ title: str,
tables: set[str],
) -> None:
- """Move one record to its derived id without ever orphaning a dependent.
+ """Move one record to *to_id* without ever orphaning a dependent.
The order is forced by the foreign keys, which cascade on delete and do
nothing on update: a dependent cannot point at an id that does not exist
@@ -300,7 +310,7 @@ async def _rewrite_one(
would make them the same decision twice.
"""
columns = [column.name for column in DecisionRecord.__table__.columns]
- placeholder = f"repowise:id-migration:{row.old_id}"
+ placeholder = f"repowise:id-migration:{from_id}"
projected = ", ".join(
":new_id" if name == "id" else (":placeholder" if name == "title" else name)
for name in columns
@@ -311,7 +321,7 @@ async def _rewrite_one(
f"INSERT INTO decision_records ({column_list}) "
f"SELECT {projected} FROM decision_records WHERE id = :old_id"
),
- {"new_id": row.new_id, "placeholder": placeholder, "old_id": row.old_id},
+ {"new_id": to_id, "placeholder": placeholder, "old_id": from_id},
)
for table, column in _DEPENDENT_COLUMNS:
@@ -319,18 +329,27 @@ async def _rewrite_one(
continue
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},
+ {"new_id": to_id, "old_id": from_id},
)
await session.execute(
_sql_text("DELETE FROM decision_records WHERE id = :old_id"),
- {"old_id": row.old_id},
+ {"old_id": from_id},
)
await session.execute(
_sql_text("UPDATE decision_records SET title = :title WHERE id = :new_id"),
- {"title": row.title, "new_id": row.new_id},
+ {"title": title, "new_id": to_id},
)
+
+async def _write_alias(
+ session: AsyncSession,
+ repository_id: str,
+ row: IdRowPlan,
+ reason: str,
+ tables: set[str],
+) -> None:
+ """Record that *row*'s original id now resolves to its final id."""
if "decision_aliases" not in tables:
return
if await session.get(DecisionAlias, row.old_id) is not None:
@@ -344,7 +363,7 @@ async def _rewrite_one(
alias_id=row.old_id,
repository_id=repository_id,
decision_id=row.new_id,
- reason=ALIAS_REASON,
+ reason=reason,
created_at=_now_utc(),
)
)
@@ -352,7 +371,7 @@ async def _rewrite_one(
async def _fold_one(
session: AsyncSession,
- repository_id: str,
+ from_id: str,
row: IdRowPlan,
tables: set[str],
) -> None:
@@ -369,6 +388,7 @@ async def _fold_one(
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.
"""
+ row = IdRowPlan(from_id, row.new_id, row.title, row.outcome, row.reason)
for table, column in _DEPENDENT_COLUMNS:
if table not in tables:
continue
@@ -391,20 +411,6 @@ async def _fold_one(
{"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,
@@ -540,6 +546,10 @@ async def apply_id_migration(
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.
+ A record sitting on another's target is parked on a temporary id first,
+ so chains and cycles of occupied ids settle in one run. Aliases name the
+ original id and the final one, never the parking id.
+
Idempotent: a second run classifies every id as ``stable``, finds no
rewrites and no folds, and touches nothing.
"""
@@ -563,18 +573,23 @@ async def apply_id_migration(
await session.flush()
tables = await _existing_tables(session)
- for row in rewrites:
- # A savepoint per record, because a rewrite that fails halfway has
- # already inserted the copy under a placeholder title. Both callers
- # swallow the exception and commit later, so without this the store
- # keeps a phantom decision that every count and search would pick up,
- # 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()
+ targets = {row.new_id for row in rewrites}
+ current = {row.old_id: row.old_id for row in (*rewrites, *folds)}
+ # One savepoint for every move. Both callers swallow the exception and
+ # commit later, so a partial run would keep a placeholder copy or a
+ # parked record with no alias naming the id it came from.
+ async with session.begin_nested():
+ for row in (*rewrites, *folds):
+ if row.old_id in targets:
+ current[row.old_id] = _parking_id(row.old_id)
+ await _move(session, row.old_id, current[row.old_id], row.title, tables)
+ for row in rewrites:
+ await _move(session, current[row.old_id], row.new_id, row.title, tables)
+ await _write_alias(session, repository_id, row, ALIAS_REASON, tables)
+ for row in folds:
+ await _fold_one(session, current[row.old_id], row, tables)
+ await _write_alias(session, repository_id, row, FOLD_REASON, tables)
+ await session.flush()
_detach_moved(session)
rekeyed = 0
diff --git a/tests/unit/persistence/test_decision_stable_ids.py b/tests/unit/persistence/test_decision_stable_ids.py
index be2d449a1..4b79d1a8f 100644
--- a/tests/unit/persistence/test_decision_stable_ids.py
+++ b/tests/unit/persistence/test_decision_stable_ids.py
@@ -12,6 +12,7 @@ that fold.
from __future__ import annotations
import json
+from datetime import UTC, datetime
import pytest
from sqlalchemy import func, select
@@ -712,13 +713,13 @@ async def test_a_failed_rewrite_leaves_no_half_moved_record(async_session, monke
import repowise.core.persistence.decision_id_migration as mod
- real = mod._rewrite_one
+ real = mod._move
- async def _explode(session, repository_id, row, tables):
- await real(session, repository_id, row, tables)
+ async def _explode(session, from_id, to_id, title, tables):
+ await real(session, from_id, to_id, title, tables)
raise RuntimeError("boom")
- monkeypatch.setattr(mod, "_rewrite_one", _explode)
+ monkeypatch.setattr(mod, "_move", _explode)
with pytest.raises(RuntimeError):
await apply_id_migration(async_session, repo.id)
@@ -727,3 +728,107 @@ async def test_a_failed_rewrite_leaves_no_half_moved_record(async_session, monke
(await async_session.execute(select(DecisionRecord.title))).scalars().all()
)
assert titles == ["Legacy one"]
+
+
+def _occupant_id(repo_id: str, quote: str) -> str:
+ return derive_decision_id(
+ repo_id, "any title", source="session", evidence_file="src/app.py", identity_quote=quote
+ )
+
+
+async def test_a_record_already_on_the_derived_id_keeps_it(async_session):
+ """An older duplicate folds into the record sitting on the id.
+
+ Keeping the older one would copy it onto an id that is still occupied,
+ which raises on the primary key on every run.
+ """
+ repo = await insert_repo(async_session)
+ occupied = _occupant_id(repo.id, "q")
+ await _legacy_record(
+ async_session,
+ repo.id,
+ "Older wording",
+ id="a" * 32,
+ identity_quote="q",
+ created_at=datetime(2026, 1, 1, tzinfo=UTC),
+ )
+ await _legacy_record(
+ async_session,
+ repo.id,
+ "Newer wording",
+ id=occupied,
+ identity_quote="q",
+ created_at=datetime(2026, 2, 1, tzinfo=UTC),
+ )
+
+ plan = await apply_id_migration(async_session, repo.id)
+
+ assert plan.counts() == {"fold": 1, "stable": 1}
+ assert (await async_session.execute(select(DecisionRecord.id))).scalars().all() == [occupied]
+ assert await resolve_decision_id(async_session, "a" * 32) == occupied
+ assert (await apply_id_migration(async_session, repo.id)).counts() == {"stable": 1}
+
+
+async def _seat(session, repo_id: str, quote: str, sits_on: str) -> None:
+ """A record whose identity is *quote*, sitting on the id *sits_on*."""
+ await _legacy_record(session, repo_id, f"Decided {quote}", id=sits_on, identity_quote=quote)
+ session.add(
+ DecisionEvidence(
+ decision_id=sits_on, source="session", evidence_file="src/app.py", source_quote=quote
+ )
+ )
+ await session.flush()
+
+
+async def _assert_settled_in_one_run(session, repo_id: str, seats: dict[str, str]) -> None:
+ """*seats* maps each quote to the id its record sat on before the run."""
+ plan = await apply_id_migration(session, repo_id)
+ assert plan.counts() == {"rewrite": len(seats)}
+
+ for quote, old_id in seats.items():
+ final = _occupant_id(repo_id, quote)
+ rec = await session.get(DecisionRecord, final)
+ assert rec is not None and rec.title == f"Decided {quote}"
+ evidence = (
+ await session.execute(
+ select(DecisionEvidence.decision_id).where(DecisionEvidence.source_quote == quote)
+ )
+ ).scalar_one()
+ assert evidence == final
+ # Straight from the original id to the final one, never via a parking id.
+ alias = await session.get(DecisionAlias, old_id)
+ assert (alias.decision_id, alias.reason) == (final, ALIAS_REASON)
+ ids = (await session.execute(select(DecisionRecord.id))).scalars().all()
+ assert sorted(ids) == sorted(_occupant_id(repo_id, q) for q in seats)
+ aliases = (await session.execute(select(DecisionAlias.decision_id))).scalars().all()
+ assert not [a for a in aliases if a.startswith("~")]
+ assert (await apply_id_migration(session, repo_id)).counts() == {"stable": len(seats)}
+
+
+async def test_a_chain_of_occupied_ids_settles_in_one_run(async_session):
+ """Each target is held by the next record, and the last target is free."""
+ repo = await insert_repo(async_session)
+ seats = {
+ "q1": "a" * 32,
+ "q2": _occupant_id(repo.id, "q1"),
+ "q3": _occupant_id(repo.id, "q2"),
+ }
+ for quote, sits_on in seats.items():
+ await _seat(async_session, repo.id, quote, sits_on)
+
+ await _assert_settled_in_one_run(async_session, repo.id, seats)
+
+
[email protected]("size", [2, 3])
+async def test_records_on_each_others_ids_settle_in_one_run(async_session, size):
+ """A cycle has no free target to start from, so one member is parked first."""
+ repo = await insert_repo(async_session)
+ quotes = [f"q{n}" for n in range(size)]
+ seats = {q: _occupant_id(repo.id, quotes[(n + 1) % size]) for n, q in enumerate(quotes)}
+ for quote, sits_on in seats.items():
+ await _seat(async_session, repo.id, quote, sits_on)
+
+ await _assert_settled_in_one_run(async_session, repo.id, seats)