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): recording a decision leaves the index's commit alone (#3541)

Raghav Chamadiyaca5f82c0a568
diff --git a/packages/cli/src/repowise/cli/commands/generate_cmd/engine.py b/packages/cli/src/repowise/cli/commands/generate_cmd/engine.py
index fb60ac4e3..7f914f0b6 100644
--- a/packages/cli/src/repowise/cli/commands/generate_cmd/engine.py
+++ b/packages/cli/src/repowise/cli/commands/generate_cmd/engine.py
@@ -110,7 +110,9 @@ async def run_scoped_generation(
try:
async with _get_session(sf) as session:
- repo = await upsert_repository(session, name=repo_path.name, local_path=str(repo_path))
+ repo = await upsert_repository(
+ session, name=repo_path.name, local_path=str(repo_path), stamp_head=True
+ )
repo_id = repo.id
# Honor the persisted submodule/nested-repo semantics of the original
diff --git a/packages/cli/src/repowise/cli/commands/init_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/init_cmd/persistence.py
index 4566391aa..cf5d04d4d 100644
--- a/packages/cli/src/repowise/cli/commands/init_cmd/persistence.py
+++ b/packages/cli/src/repowise/cli/commands/init_cmd/persistence.py
@@ -171,6 +171,7 @@ async def persist_result(
session,
name=result.repo_name,
local_path=str(repo_path),
+ stamp_head=True,
)
# Persist the detected tech stack into the repository's settings
# blob. Merge into any pre-existing settings so we don't clobber
diff --git a/packages/cli/src/repowise/cli/commands/restyle_cmd.py b/packages/cli/src/repowise/cli/commands/restyle_cmd.py
index 5c2b41ad6..a874c7669 100644
--- a/packages/cli/src/repowise/cli/commands/restyle_cmd.py
+++ b/packages/cli/src/repowise/cli/commands/restyle_cmd.py
@@ -90,7 +90,9 @@ async def _run_restyle(
sf = create_session_factory(engine)
async with get_session(sf) as session:
- repo = await upsert_repository(session, name=repo_path.name, local_path=str(repo_path))
+ repo = await upsert_repository(
+ session, name=repo_path.name, local_path=str(repo_path), stamp_head=True
+ )
repo_id = repo.id
# Rehydrate the graph + git signals from SQL — no parse, no resolve, no blame.
diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/command.py b/packages/cli/src/repowise/cli/commands/update_cmd/command.py
index 8aafd824f..5d24ee432 100644
--- a/packages/cli/src/repowise/cli/commands/update_cmd/command.py
+++ b/packages/cli/src/repowise/cli/commands/update_cmd/command.py
@@ -1514,12 +1514,8 @@ def run_update(
timings.stop("run")
updated_state["phase_timings"] = timings.totals
save_state(repo_path, updated_state)
- # No stamp_head_commit here, which is the behaviour this branch has
- # always had: the re-score's `upsert_repository` re-reads HEAD from
- # disk and advances the row itself. The re-read follows a worktree
- # `.git` pointer (the same helper the MCP reader uses). A path that
- # is not a checkout still leaves a stored commit untouched, because a
- # missed read never blanks one.
+ # Both branches advanced state.json to head; keep the DB stamp with it.
+ stamp_head_commit(repo_path, head)
_finish_outcome(repo_path, agents_md)
consume_update_pending(repo_path, head)
if emitter is not None:
diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
index 311d38e79..c126716c9 100644
--- a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
+++ b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
@@ -835,7 +835,9 @@ async def _persist_full_update_async(
async with get_session(sf) as session:
with timed(timings, "persist.open"):
- repo = await upsert_repository(session, name=repo_name, local_path=str(repo_path))
+ repo = await upsert_repository(
+ session, name=repo_name, local_path=str(repo_path), stamp_head=True
+ )
repo_id = repo.id
if reconcile_full_scope:
diff --git a/packages/cli/src/repowise/cli/commands/upgrade_flow.py b/packages/cli/src/repowise/cli/commands/upgrade_flow.py
index 68bcc1b1f..f7ce2d38f 100644
--- a/packages/cli/src/repowise/cli/commands/upgrade_flow.py
+++ b/packages/cli/src/repowise/cli/commands/upgrade_flow.py
@@ -268,7 +268,9 @@ async def _run_upgrade(
# wiring: pipeline_jobs.repository_id is an FK to repositories.id, so the
# row must exist before the backfill creates its checkpoint job.
async with get_session(sf) as session:
- repo = await upsert_repository(session, name=repo_path.name, local_path=str(repo_path))
+ repo = await upsert_repository(
+ session, name=repo_path.name, local_path=str(repo_path), stamp_head=True
+ )
repo_id = repo.id
# 2. Backfill the git tier ESSENTIAL -> FULL (resumable via JobStore).
diff --git a/packages/core/src/repowise/core/persistence/crud/repository.py b/packages/core/src/repowise/core/persistence/crud/repository.py
index 603570499..ea31d22ac 100644
--- a/packages/core/src/repowise/core/persistence/crud/repository.py
+++ b/packages/core/src/repowise/core/persistence/crud/repository.py
@@ -38,6 +38,7 @@ async def upsert_repository(
default_branch: str = "main",
settings: dict | None = None,
head_commit: str | None = None,
+ stamp_head: bool = False,
repo_id: str | None = None,
) -> Repository:
"""Create or update a repository record.
@@ -51,17 +52,20 @@ async def upsert_repository(
when a row for ``local_path`` already exists (the existing id wins).
``head_commit`` records the git commit the index was built against — the
- value the MCP ``_meta`` freshness check compares to the live HEAD. Callers
- that already know the synced commit may pass it; otherwise it is read from
- ``local_path``'s git HEAD via plain file I/O (no ``git`` subprocess). This
- runs at the start of every index/update, so the stored commit tracks each
- sync and the MCP staleness signal stays calibrated instead of being NULL
- forever (which silently disabled the preferred HEAD-vs-index comparison).
+ value the MCP ``_meta`` freshness check compares to the live HEAD. A new
+ row always gets one: the passed value, else ``local_path``'s git HEAD read
+ via plain file I/O (no ``git`` subprocess). An existing row's commit moves
+ only when the caller records a sync, by passing ``head_commit`` or
+ ``stamp_head=True`` (read HEAD). Callers that only need the row, such as
+ decisions, scans and server registration, leave it alone: moving it there
+ made the database claim an index at HEAD while ``state.json`` still named
+ the last real sync.
"""
result = await session.execute(select(Repository).where(Repository.local_path == local_path))
repo = result.scalar_one_or_none()
- resolved_head = head_commit or _read_head_commit(local_path)
+ stamping = repo is None or stamp_head or head_commit is not None
+ resolved_head = (head_commit or _read_head_commit(local_path)) if stamping else None
if repo is None:
repo = Repository(
diff --git a/packages/core/src/repowise/core/pipeline/full_index.py b/packages/core/src/repowise/core/pipeline/full_index.py
index ac59223e1..f9bf08d3b 100644
--- a/packages/core/src/repowise/core/pipeline/full_index.py
+++ b/packages/core/src/repowise/core/pipeline/full_index.py
@@ -73,6 +73,7 @@ async def index_repo_full(
session,
name=result.repo_name,
local_path=str(repo_path),
+ stamp_head=True,
)
stale_page_ids = (
await persist_pipeline_result(
diff --git a/packages/core/src/repowise/core/pipeline/incremental.py b/packages/core/src/repowise/core/pipeline/incremental.py
index 42b9171a5..242c89f6e 100644
--- a/packages/core/src/repowise/core/pipeline/incremental.py
+++ b/packages/core/src/repowise/core/pipeline/incremental.py
@@ -2000,7 +2000,7 @@ async def persist_incremental_index(
async with get_session(sf) as session:
with timed(timings, "persist.open"):
repo = await upsert_repository(
- session, name=repo_path.name, local_path=str(repo_path)
+ session, name=repo_path.name, local_path=str(repo_path), stamp_head=True
)
repo_id = repo.id
diff --git a/tests/unit/cli/test_decision_cmd.py b/tests/unit/cli/test_decision_cmd.py
index 3c92add9c..f9721f7ae 100644
--- a/tests/unit/cli/test_decision_cmd.py
+++ b/tests/unit/cli/test_decision_cmd.py
@@ -151,6 +151,45 @@ def test_decision_add_records_without_prompting(indexed_repo: Path) -> None:
assert record["tags"] == ["database"]
+def test_decision_commands_leave_the_indexed_commit_alone(tmp_path: Path) -> None:
+ """Recording or confirming a decision is not a sync, so the row's commit stays.
+
+ Moving it to HEAD left the database and ``state.json`` naming two different
+ commits, which made test selection distrust the index and run every test.
+ """
+ import sqlite3
+
+ repo = tmp_path / "repo"
+ git = repo / ".git"
+ (git / "refs" / "heads").mkdir(parents=True)
+ (git / "HEAD").write_text("ref: refs/heads/main\n", encoding="utf-8")
+ (git / "refs" / "heads" / "main").write_text("b" * 40 + "\n", encoding="utf-8")
+ indexed = "a" * 40
+ _seed_wiki_db(repo, [{"id": "cccccccc", "title": "Old", "status": "proposed"}])
+ conn = sqlite3.connect(repo / ".repowise" / "wiki.db")
+ with conn:
+ conn.execute("UPDATE repositories SET head_commit = ?", (indexed,))
+ conn.close()
+
+ added = CliRunner().invoke(
+ cli,
+ ["decision", "add", "--title", "T", "--decision", "D", str(repo)],
+ input="",
+ )
+ assert added.exit_code == 0, added.output
+ confirmed = CliRunner().invoke(cli, ["decision", "confirm", "cccccccc", str(repo)])
+ assert confirmed.exit_code == 0, confirmed.output
+ listed = CliRunner().invoke(cli, ["decision", "list", str(repo)])
+ assert listed.exit_code == 0, listed.output
+
+ conn = sqlite3.connect(repo / ".repowise" / "wiki.db")
+ try:
+ rows = conn.execute("SELECT head_commit FROM repositories").fetchall()
+ finally:
+ conn.close()
+ assert rows == [(indexed,)]
+
+
def test_a_flag_driven_decision_lands_proposed(indexed_repo: Path) -> None:
"""A caller that inferred a decision has not reviewed it, and the store says so.
diff --git a/tests/unit/cli/test_update_e2e.py b/tests/unit/cli/test_update_e2e.py
index 47d70c59a..3225e6a4a 100644
--- a/tests/unit/cli/test_update_e2e.py
+++ b/tests/unit/cli/test_update_e2e.py
@@ -175,6 +175,32 @@ def test_update_refreshes_every_index_layer(tmp_path: Path) -> None:
assert state.get("knowledge_graph", {}).get("fingerprint")
+def test_only_indexing_moves_the_stored_commit(tmp_path: Path) -> None:
+ """A full index and an update stamp HEAD; a decision recorded between them does not."""
+ repo = _make_git_repo(tmp_path)
+ _index_full(repo)
+ base = _git(repo, "rev-parse", "HEAD")
+ assert asyncio.run(_db_snapshot(repo))["head_commit"] == base
+
+ from repowise.cli.helpers import save_state
+
+ save_state(repo, {"last_sync_commit": base, "docs_enabled": False})
+ (repo / "c.py").write_text("def gamma():\n return 3\n")
+ _git(repo, "add", ".")
+ _git(repo, "commit", "-m", "add c.py")
+ new_head = _git(repo, "rev-parse", "HEAD")
+
+ added = CliRunner().invoke(
+ cli, ["decision", "add", "--title", "T", "--decision", "D", str(repo)], input=""
+ )
+ assert added.exit_code == 0, added.output
+ assert asyncio.run(_db_snapshot(repo))["head_commit"] == base
+
+ result = CliRunner().invoke(cli, ["update", str(repo), "--no-workspace"])
+ assert result.exit_code == 0, result.output
+ assert asyncio.run(_db_snapshot(repo))["head_commit"] == new_head
+
+
def test_update_recovers_idle_file_decayed_health(tmp_path: Path, monkeypatch) -> None:
"""#728: an idle file's time-decayed history must recover on a plain git
update. a.py churns hard, then only b.py changes six months later — a.py is
diff --git a/tests/unit/persistence/test_repository_head_commit.py b/tests/unit/persistence/test_repository_head_commit.py
index 95eebc138..ce65bc4ae 100644
--- a/tests/unit/persistence/test_repository_head_commit.py
+++ b/tests/unit/persistence/test_repository_head_commit.py
@@ -183,11 +183,19 @@ async def test_upsert_repository_stamps_and_advances_head(tmp_path: Path) -> Non
repo = await upsert_repository(session, name=tmp_path.name, local_path=str(tmp_path))
assert repo.head_commit == sha1
- # HEAD moves; a re-index advances the stored commit.
+ # HEAD moves. A caller that only needs the row (a decision, a scan)
+ # leaves the stored commit where the last sync put it.
sha2 = "2" * 40
(tmp_path / ".git" / "refs" / "heads" / "main").write_text(sha2 + "\n", encoding="utf-8")
async with get_session(sf) as session:
repo = await upsert_repository(session, name=tmp_path.name, local_path=str(tmp_path))
+ assert repo.head_commit == sha1
+
+ # A re-index advances it.
+ async with get_session(sf) as session:
+ repo = await upsert_repository(
+ session, name=tmp_path.name, local_path=str(tmp_path), stamp_head=True
+ )
assert repo.head_commit == sha2
# An explicit override wins over the on-disk read.
@@ -220,7 +228,7 @@ async def test_upsert_repository_keeps_head_when_path_not_git(tmp_path: Path) ->
# Same DB row keyed by local_path, but pretend the .git vanished.
(tmp_path / ".git" / "HEAD").unlink()
async with get_session(sf) as session:
- repo = await upsert_repository(session, name="r", local_path=str(tmp_path))
+ repo = await upsert_repository(session, name="r", local_path=str(tmp_path), stamp_head=True)
assert repo.head_commit == sha # preserved, not nulled
finally:
await engine.dispose()
@@ -245,12 +253,12 @@ async def test_upsert_repository_advances_worktree_head(tmp_path: Path) -> None:
sha2 + "\n", encoding="utf-8"
)
async with get_session(sf) as session:
- repo = await upsert_repository(session, name="wt", local_path=str(checkout))
+ repo = await upsert_repository(session, name="wt", local_path=str(checkout), stamp_head=True)
assert repo.head_commit == sha2
(checkout / ".git").write_text("not a gitdir\n", encoding="utf-8")
async with get_session(sf) as session:
- repo = await upsert_repository(session, name="wt", local_path=str(checkout))
+ repo = await upsert_repository(session, name="wt", local_path=str(checkout), stamp_head=True)
assert repo.head_commit == sha2
finally:
await engine.dispose()