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
perf(actions): store Fix first and the Do next view at index time (#3311)
Raghav Chamadiya3489bef1997f
diff --git a/docs/architecture/code-health.md b/docs/architecture/code-health.mdindex b13c7095a..5cdc1e775 100644--- a/docs/architecture/code-health.md+++ b/docs/architecture/code-health.md@@ -661,6 +661,8 @@ Full method and data:### 6.3 Fix first ranking (`fix_first/`, `worth.py`)+Index and update save the queue and the Do next list, so these surfaces open without rebuilding them.+Fix first is built once in `analysis/health/fix_first/build.py` from stored rowsand rendered unchanged by `get_health`, `repowise health`, the dashboard and thegenerated CLAUDE.md. Every excluded unit is counted in `totals.excluded` under onediff --git a/docs/layers/CODE_HEALTH.md b/docs/layers/CODE_HEALTH.mdindex 453c0bbf6..8d1f36cc9 100644--- a/docs/layers/CODE_HEALTH.md+++ b/docs/layers/CODE_HEALTH.md@@ -224,7 +224,8 @@ repowise next --json # the whole stored viewRows are grouped as **Now**, **Worth planning** and **Improve what Repowise cansee** (steps such as adding a coverage report that make other answers sharper).-Run `repowise update` to refresh the list after you change code.+Run `repowise update` to refresh the list after you change code. Dismissing or+snoozing a row takes effect at once.A fragile file asks for tests only when it has no measured coverage and no testreaches it in the code graph. When tests do reach it, the row asks you todiff --git a/packages/cli/src/repowise/cli/commands/init_cmd/command.py b/packages/cli/src/repowise/cli/commands/init_cmd/command.pyindex 83a9d9e74..85d06785f 100644--- a/packages/cli/src/repowise/cli/commands/init_cmd/command.py+++ b/packages/cli/src/repowise/cli/commands/init_cmd/command.py@@ -1976,6 +1976,11 @@ def init_command(save_key=save_key,)+ # Every store is final: store the views ``repowise next`` and the+ # dashboard read, before the editor files quote Fix first from them.+ from repowise.cli.commands.update_cmd.persistence import refresh_read_snapshots++ refresh_read_snapshots(repo_path)files_written = write_editor_project_files(console,repo_path,diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/command.py b/packages/cli/src/repowise/cli/commands/update_cmd/command.pyindex 4cac417dd..73da77e0d 100644--- a/packages/cli/src/repowise/cli/commands/update_cmd/command.py+++ b/packages/cli/src/repowise/cli/commands/update_cmd/command.py@@ -62,6 +62,7 @@ from .persistence import (heal_commit_offsets,health_analyzer_changed,parser_changed,+ refresh_read_snapshots,stamp_head_commit,)from .reporting import (@@ -181,6 +182,23 @@ def _record_update_outcome(return+def _finish_outcome(+ repo_path: Any,+ agents_md: bool | None,+ degraded: list[str] | None = None,+ *,+ write: bool = True,+ timings: PhaseTimings | None = None,+) -> None:+ """Every update outcome ends here: the stored read views, then the editor+ files that quote them. ``write=False`` (a dry run) skips the views."""+ if write:+ with timed(timings, "read_snapshots"):+ refresh_read_snapshots(repo_path)+ with timed(timings, "editor_files"):+ _refresh_editor_stamp(repo_path, agents_md, degraded)++def _refresh_editor_stamp(repo_path: Any, agents_md: bool | None, degraded: list[str] | None = None) -> None:@@ -1081,7 +1099,7 @@ def run_update(# A capture added after this repo was indexed would otherwise wait# for the next commit to land, which on a quiet repo is never.heal_commit_offsets(repo_path)- _refresh_editor_stamp(repo_path, agents_md)+ _finish_outcome(repo_path, agents_md)# The index is current, so any pending marker a bailed update left# is by definition caught-up (or older); drop it here too rather# than waiting for the next content-changing update. This is the@@ -1325,7 +1343,7 @@ def run_update(# This path skips the git phase, so its offset backfill never runs.if not dry_run:heal_commit_offsets(repo_path)- _refresh_editor_stamp(repo_path, agents_md)+ _finish_outcome(repo_path, agents_md, write=not dry_run)# We hold the lock and have advanced to head; drop any stale pending# marker a bailed update left behind.consume_update_pending(repo_path, head)@@ -1407,7 +1425,7 @@ def run_update(# disk and advances the row itself. It re-reads rather than being told,# so it is a no-op in a linked worktree or a non-git checkout — both# pre-existing, and neither is what this change is about.- _refresh_editor_stamp(repo_path, agents_md)+ _finish_outcome(repo_path, agents_md)consume_update_pending(repo_path, head)if emitter is not None:emitter.done(@@ -1803,7 +1821,7 @@ def run_update(if emitter is not None:emitter.error(str(exc))raise- _refresh_editor_stamp(repo_path, agents_md, degraded)+ _finish_outcome(repo_path, agents_md, degraded)# Index-only is the post-commit hook's hot path; clear any stale pending# marker here too, not just on the full-docs path.consume_update_pending(repo_path, head)@@ -2496,9 +2514,8 @@ def run_update(if run_full_security_rescan(repo_path, exclude_patterns):state["security_scanner_version"] = SECURITY_SCANNER_VERSION- # ---- Editor project files (best-effort) ----- with timed(timings, "editor_files"):- _refresh_editor_stamp(repo_path, agents_md, degraded)+ # ---- Stored read views, then the editor files that quote them ----+ _finish_outcome(repo_path, agents_md, degraded, timings=timings)# Update statefrom repowise.cli.helpers import config_fingerprintdiff --git a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.pyindex 9e8b24227..9f5d6a82d 100644--- a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py+++ b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py@@ -280,6 +280,28 @@ def heal_commit_offsets(repo_path: Any) -> None:run_async(_run())+def refresh_read_snapshots(repo_path: Any) -> None:+ """Store the Fix first queue and the next-actions view for the next reader.++ A named step at every outcome of ``repowise update`` and at the end of+ ``repowise init``, once every store is final, so ``repowise next``, the+ overview and the dashboard read one row instead of rebuilding in each new+ process. A view no write has invalidated since is left as it is, so the+ "already up to date" path costs one primary-key read per view.+ Best-effort: without a snapshot a reader builds live, as before.+ """+ from repowise.cli.helpers import repo_index_session+ from repowise.core.persistence.crud.analysis.actions import write_read_snapshots++ async def _run() -> None:+ async with repo_index_session(Path(repo_path)) as opened:+ if opened is not None:+ await write_read_snapshots(*opened)++ with contextlib.suppress(Exception):+ run_async(_run())++# A repair window that has grown past this many commits stopped being a repair# and became a re-index: the diff it forces every update to walk is no longer# change-sized. Bounding it is what stops a step that fails on every run fromdiff --git a/packages/cli/src/repowise/cli/commands/update_cmd/workspace.py b/packages/cli/src/repowise/cli/commands/update_cmd/workspace.pyindex 39892cfc9..bf6e7aebd 100644--- a/packages/cli/src/repowise/cli/commands/update_cmd/workspace.py+++ b/packages/cli/src/repowise/cli/commands/update_cmd/workspace.py@@ -584,6 +584,8 @@ def _refresh_workspace_editor_project_files(from repowise.cli.editor_integrations.defaults import get_default_project_file_overridesfrom repowise.cli.editor_setup import EditorSetupOptions, refresh_editor_project_files+ from .persistence import refresh_read_snapshots+options: EditorSetupOptions | None = Noneif agents_md is not None:options = EditorSetupOptions(@@ -595,6 +597,7 @@ def _refresh_workspace_editor_project_files(repo_path = (ws_root / entry.path).resolve()if not (repo_path / ".repowise").is_dir():continue+ refresh_read_snapshots(repo_path)try:refresh_editor_project_files(console, repo_path, options=options)except Exception as exc:diff --git a/packages/core/alembic/versions/0100_read_snapshots.py b/packages/core/alembic/versions/0100_read_snapshots.pynew file mode 100644index 000000000..6d1a736fc--- /dev/null+++ b/packages/core/alembic/versions/0100_read_snapshots.py@@ -0,0 +1,53 @@+"""read_snapshots: the Fix first queue and the next-actions view, built at index time.++Both were built on the first read of every process: on a 21k-file repository+the next-actions view cost 4.6 s of file facts and 3.7 s of Fix first before+the first row printed. Index and update now store each as one row keyed by+the code that built it; a write to any store a view reads deletes the rows in+the same transaction, and a reader with no row builds live. Derived state: an+existing store upgrades empty and fills on its next index or update.++Local SQLite stores get the table from ``init_db``'s ``create_all``; this+migration covers managed Postgres.++Revision ID: 0100+Revises: 0099+Create Date: 2026-10-10+"""++from __future__ import annotations++from collections.abc import Sequence++import sqlalchemy as sa+from alembic import op++revision: str = "0100"+down_revision: str | None = "0099"+branch_labels: str | Sequence[str] | None = None+depends_on: str | Sequence[str] | None = None+++def upgrade() -> None:+ op.create_table(+ "read_snapshots",+ sa.Column(+ "repository_id",+ sa.String(32),+ sa.ForeignKey("repositories.id", ondelete="CASCADE"),+ primary_key=True,+ ),+ sa.Column("kind", sa.String(32), primary_key=True),+ sa.Column("key", sa.Text(), nullable=False),+ sa.Column("payload_json", sa.Text(), nullable=False),+ sa.Column(+ "updated_at",+ sa.DateTime(timezone=True),+ nullable=False,+ server_default=sa.func.now(),+ ),+ )+++def downgrade() -> None:+ op.drop_table("read_snapshots")diff --git a/packages/core/src/repowise/core/analysis/actions/__init__.py b/packages/core/src/repowise/core/analysis/actions/__init__.pyindex 970b37033..20152ebc0 100644--- a/packages/core/src/repowise/core/analysis/actions/__init__.py+++ b/packages/core/src/repowise/core/analysis/actions/__init__.py@@ -8,13 +8,21 @@ loader that reads the stores lives infrom __future__ import annotations-from .engine import RULES, ActionStateRecord, compose_actions, find_action+from .engine import (+ RULES,+ ActionStateRecord,+ compose_actions,+ find_action,+ rank_actions,+ rule_actions,+)from .facts import RepoFactsfrom .model import (ACTION_RULES,ACTION_STATES,ACTION_SURFACES,ACTION_TIERS,+ ACTIONS_MODEL_VERSION,FACT_BASES,HORIZONS,RULE_STATUSES,@@ -25,6 +33,7 @@ from .model import ()__all__ = [+ "ACTIONS_MODEL_VERSION","ACTION_RULES","ACTION_STATES","ACTION_SURFACES",@@ -41,4 +50,6 @@ __all__ = ["RuleOutcome","compose_actions","find_action",+ "rank_actions",+ "rule_actions",]diff --git a/packages/core/src/repowise/core/analysis/actions/engine.py b/packages/core/src/repowise/core/analysis/actions/engine.pyindex a1b0231ca..f2d6b99d2 100644--- a/packages/core/src/repowise/core/analysis/actions/engine.py+++ b/packages/core/src/repowise/core/analysis/actions/engine.py@@ -8,7 +8,7 @@ from __future__ import annotationsfrom collections.abc import Callable, Mappingfrom dataclasses import dataclassfrom datetime import UTC, datetime-from typing import Any+from typing import Any, NamedTuplefrom .context import RepoContext, build_contextfrom .facts import RepoFacts@@ -49,37 +49,50 @@ class ActionStateRecord:until: datetime | None = None-def _hidden(action: Action, record: ActionStateRecord | None, now: datetime) -> bool:+class Ranked(NamedTuple):+ """One ranked unit: the rule's ordering weight and the action as served."""++ weight: float+ action: dict[str, Any]+++def _hidden(action: dict[str, Any], record: ActionStateRecord | None, now: datetime) -> bool:if record is None:return Falseif record.state == "snoozed":return record.until is not None and _naive(record.until) > _naive(now)# Dismissed and done both hold only while the facts are the ones the person# saw. A dismissed "raise coverage" returns when ten more fixes land.- return record.fingerprint == action.fingerprint+ return record.fingerprint == action["fingerprint"]def _naive(value: datetime) -> datetime:return value.astimezone(UTC).replace(tzinfo=None) if value.tzinfo else value-def _order(actions: list[Action]) -> list[Action]:+def _order(actions: list[Ranked]) -> list[Ranked]:ranked = sorted(actions,- key=lambda a: (TIER_RANK[a.tier], RULE_RANK[a.rule], -a.weight, a.action_id),+ key=lambda r: (+ TIER_RANK[r.action["tier"]],+ RULE_RANK[r.action["rule"]],+ -r.weight,+ r.action["id"],+ ),)- out: list[Action] = []- for tier in sorted({a.tier for a in ranked}, key=TIER_RANK.__getitem__):- in_tier = [a for a in ranked if a.tier == tier]- head: list[Action] = []- rest: list[Action] = []+ out: list[Ranked] = []+ for tier in sorted({r.action["tier"] for r in ranked}, key=TIER_RANK.__getitem__):+ in_tier = [r for r in ranked if r.action["tier"] == tier]+ head: list[Ranked] = []+ rest: list[Ranked] = []taken: dict[str, int] = {}- for a in in_tier:- if taken.get(a.rule, 0) < PER_RULE_HEAD:- head.append(a)- taken[a.rule] = taken.get(a.rule, 0) + 1+ for r in in_tier:+ rule = r.action["rule"]+ if taken.get(rule, 0) < PER_RULE_HEAD:+ head.append(r)+ taken[rule] = taken.get(rule, 0) + 1else:- rest.append(a)+ rest.append(r)out.extend(head + rest)return out@@ -93,33 +106,59 @@ def find_action(facts: RepoFacts, action_id: str) -> Action | None:)-def compose_actions(- facts: RepoFacts,+def rule_actions(facts: RepoFacts) -> dict[str, Any]:+ """Every rule's outcome and actions, JSON-ready: the part of the view the+ stores decide. :func:`rank_actions` applies the person's answers and ranks+ it; index and update store it so a read skips the facts and the rules."""+ ctx = build_context(facts)+ outcomes = [rule(facts, ctx) for rule in RULES]+ return {+ "anchor": facts.anchor.isoformat() if facts.anchor else None,+ "week_start": ctx.week_start.isoformat() if ctx.week_start else None,+ "context": {+ "production_files": ctx.production_files,+ "active_authors_90d": ctx.active_authors_90d,+ "fix_commits_90d": ctx.fix_commits_90d,+ "busy_threshold": ctx.busy_threshold,+ "coverage": facts.coverage.status,+ "history_too_short": ctx.history_too_short,+ },+ "rules": [o.as_dict() for o in outcomes],+ "actions": [(a.weight, a.as_dict()) for o in outcomes for a in o.actions],+ }+++def rank_actions(+ ruled: Mapping[str, Any],states: Mapping[str, ActionStateRecord] | None = None,*,now: datetime,) -> dict[str, Any]:- ctx = build_context(facts)- outcomes = [rule(facts, ctx) for rule in RULES]- actions = [a for o in outcomes for a in o.actions]-+ """The view: :func:`rule_actions`' output per horizon, answered and ranked."""+ actions = [Ranked(weight, action) for weight, action in ruled["actions"]]states = states or {}horizons: dict[str, Any] = {}for horizon in HORIZONS:- pool = [a for a in actions if horizon in a.horizons]+ pool = [r for r in actions if horizon in r.action["horizons"]]# A folder action speaks for the fragile files inside it, in the time# frame it appears in; listing both there tells one story twice. The# week's regression roll-up names files too, but only as a starting# point, so it absorbs nothing.- absorbed = {p for a in pool if a.rule == "fix_concentration" for p in a.includes}- pool = [a for a in pool if not (a.rule == "fragile_file" and a.target_path in absorbed)]- visible = [a for a in pool if not _hidden(a, states.get(a.action_id), now)]+ absorbed = {+ p for r in pool if r.action["rule"] == "fix_concentration" for p in r.action["includes"]+ }+ pool = [+ r+ for r in pool+ if not (r.action["rule"] == "fragile_file" and r.action["target"]["path"] in absorbed)+ ]+ visible = [r for r in pool if not _hidden(r.action, states.get(r.action["id"]), now)]ordered = _order(visible)by_tier: dict[str, int] = {}- for a in ordered:- by_tier[a.tier] = by_tier.get(a.tier, 0) + 1+ for r in ordered:+ by_tier[r.action["tier"]] = by_tier.get(r.action["tier"], 0) + 1horizons[horizon] = {- "actions": [a.as_dict() for a in ordered[:KEEP_PER_HORIZON]],+ "actions": [r.action for r in ordered[:KEEP_PER_HORIZON]],"total": len(ordered),"hidden": len(pool) - len(visible),"by_tier": by_tier,@@ -127,17 +166,24 @@ def compose_actions(return {"status": "available",- "anchor": facts.anchor.isoformat() if facts.anchor else None,- "week_start": ctx.week_start.isoformat() if ctx.week_start else None,- "context": {- "production_files": ctx.production_files,- "active_authors_90d": ctx.active_authors_90d,- "fix_commits_90d": ctx.fix_commits_90d,- "busy_threshold": ctx.busy_threshold,- "coverage": facts.coverage.status,- "history_too_short": ctx.history_too_short,- },+ "anchor": ruled["anchor"],+ "week_start": ruled["week_start"],+ "context": ruled["context"],"horizons": horizons,- "summary": summarize(horizons, facts.anchor),- "rules": [o.as_dict() for o in outcomes],+ # Ranked after the person's answers, so composed here, not stored.+ "summary": summarize(horizons, _anchor(ruled["anchor"])),+ "rules": ruled["rules"],}+++def _anchor(iso: str | None) -> datetime | None:+ return datetime.fromisoformat(iso) if iso else None+++def compose_actions(+ facts: RepoFacts,+ states: Mapping[str, ActionStateRecord] | None = None,+ *,+ now: datetime,+) -> dict[str, Any]:+ return rank_actions(rule_actions(facts), states, now=now)diff --git a/packages/core/src/repowise/core/analysis/actions/model.py b/packages/core/src/repowise/core/analysis/actions/model.pyindex 47e444e5e..92c12f1fc 100644--- a/packages/core/src/repowise/core/analysis/actions/model.py+++ b/packages/core/src/repowise/core/analysis/actions/model.py@@ -12,6 +12,10 @@ from typing import Any, Literal, get_argsfrom repowise.core.analysis.next_call import ActionCommand+#: Bumped when a rule's output changes meaning or shape; stored views built+#: under another version are rebuilt.+ACTIONS_MODEL_VERSION = 1+ActionRule = Literal["live_secret","fresh_regressions",diff --git a/packages/core/src/repowise/core/persistence/crud/analysis/actions.py b/packages/core/src/repowise/core/persistence/crud/analysis/actions.pyindex 85f504251..e65ccdb6d 100644--- a/packages/core/src/repowise/core/persistence/crud/analysis/actions.py+++ b/packages/core/src/repowise/core/persistence/crud/analysis/actions.py@@ -6,6 +6,11 @@ built before a detector shipped, or an older schema, costs that store's rulesThe reads here only narrow; the row-to-fact rule is the pure builder in``repowise.core.analysis.actions.build``.++Index and update store the rules' output as a read snapshot+(:func:`write_actions_snapshot`), kept only until a store the facts come from+is written (``persistence.read_snapshots``); a read that finds it ranks the+stored actions and skips the facts and the rules."""from __future__ import annotations@@ -19,11 +24,13 @@ from sqlalchemy import func, selectfrom sqlalchemy.ext.asyncio import AsyncSessionfrom repowise.core.analysis.actions import (+ ACTIONS_MODEL_VERSION,Action,ActionStateRecord,RepoFacts,- compose_actions,find_action,+ rank_actions,+ rule_actions,)from repowise.core.analysis.actions.build import (ABSENT,@@ -66,7 +73,8 @@ from ...models import (HealthFinding,SecurityFinding,)-from .fix_first import load_fix_first+from ...read_snapshots import mark_current, read_snapshot, refresh_snapshot, snapshot_key+from .fix_first import load_fix_first, write_fix_first_snapshotlogger = logging.getLogger(__name__)@@ -449,7 +457,11 @@ async def set_action_state(fingerprint: str = "",until: datetime | None = None,) -> None:- """Record, replace, or (``state=None``) clear a person's answer to one action."""+ """Record, replace, or (``state=None``) clear a person's answer to one action.++ Not a stored view's input (``read_snapshots.UNRELATED_TABLES``): answers+ apply on every read, so writing one keeps the stored views.+ """row = (await session.execute(select(ActionState).where(@@ -475,18 +487,89 @@ async def set_action_state(row.state, row.fingerprint, row.until = state, fingerprint, until+#: The stored rules' output's ``read_snapshots.kind``.+SNAPSHOT_KIND = "actions"+_KEY = snapshot_key(SNAPSHOT_KIND, ACTIONS_MODEL_VERSION)+++async def _ruled(session: AsyncSession, repo_id: str) -> dict[str, Any]:+ """The rules' output from the facts as stored now, and what could not be read."""+ facts = await load_repo_facts(session, repo_id)+ return {**rule_actions(facts), "unavailable": dict(facts.unavailable)}+++async def write_actions_snapshot(session: AsyncSession, repo_id: str) -> bool:+ """Store the rules' output for the next reader; ``False`` when the stored+ one is still current."""+ return await refresh_snapshot(+ session, repo_id, SNAPSHOT_KIND, _KEY, lambda: _ruled(session, repo_id)+ )+++async def write_read_snapshots(session: AsyncSession, repo_id: str) -> None:+ """Store the Fix first queue, then the actions view that quotes its head.++ For a writer whose stores are final for this run, on the session that+ wrote them or a later one. Best-effort, each in its own savepoint: a view+ that fails to build is built live on read instead.+ """+ written = True+ for kind, write in (+ ("fix_first", write_fix_first_snapshot),+ (SNAPSHOT_KIND, write_actions_snapshot),+ ):+ try:+ async with session.begin_nested():+ await write(session, repo_id)+ except Exception as exc: # a reader builds live instead+ logger.warning("read snapshot %s not written: %s", kind, exc)+ written = False+ # Built after this session's own writes, so its commit may keep them; a+ # kind that failed may have left an older row, which the commit drops.+ if written:+ await mark_current(session)+++def _view(ruled: dict[str, Any], states: dict[str, ActionStateRecord], now: datetime) -> dict:+ view = rank_actions(ruled, states, now=now)+ view["unavailable"] = dict(ruled["unavailable"])+ return view+++def _stored_view(+ ruled: dict[str, Any] | None, states: dict[str, ActionStateRecord], now: datetime+) -> dict[str, Any] | None:+ """The view from the stored rules' output; ``None`` when there is none or+ the ranking cannot read it. Fields the ranking does not read pass through+ as stored: a change to an action's shape bumps ``ACTIONS_MODEL_VERSION``,+ which is in the key."""+ if ruled is None:+ return None+ try:+ return _view(ruled, states, now)+ except (KeyError, TypeError, ValueError, AttributeError) as exc:+ logger.info("stored actions no longer fit the model: %s", exc)+ return None++async def load_actions_view(session: AsyncSession, repo_id: str, *, now: datetime | None = None) -> dict[str, Any]:- """The one entry point: facts, the person's answers, and the ranked view."""- facts = await load_repo_facts(session, repo_id)+ """The one entry point: the rules' output, the person's answers, and the ranked view.++ The rules' output is the stored snapshot while it is current, else built+ from the facts here. Never writes.+ """try:async with session.begin_nested():states = await get_action_states(session, repo_id)except Exception:states = {}- view = compose_actions(facts, states, now=now or datetime.now(UTC))- view["unavailable"] = dict(facts.unavailable)+ when = now or datetime.now(UTC)+ stored = await read_snapshot(session, repo_id, SNAPSHOT_KIND, _KEY)+ view = _stored_view(stored, states, when)+ if view is None:+ view = _view(await _ruled(session, repo_id), states, when)return viewdiff --git a/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.py b/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.pyindex cf525de2e..9eabf5350 100644--- a/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.py+++ b/packages/core/src/repowise/core/persistence/crud/analysis/fix_first.py@@ -16,21 +16,29 @@ time, as refactoring and performance already are, and rank them in SQL.The built queue is cached in process, keyed by the repository and thenewest write to each store it reads, so repeated calls between updates cost-one aggregate read.+one aggregate read. Index and update also store the production queue+(:func:`write_fix_first_snapshot`), kept only until a store it reads is+written (``persistence.read_snapshots``), so the first call of a new process+reads one row instead of building."""from __future__ import annotationsfrom collections import OrderedDict, namedtuplefrom collections.abc import Callable-from dataclasses import replace+from dataclasses import asdict, replacefrom typing import Anyfrom sqlalchemy import and_, case, func, or_, selectfrom sqlalchemy.ext.asyncio import AsyncSessionfrom repowise.core.analysis.finding_registry import excluded_types-from repowise.core.analysis.health.fix_first import DEFAULT_LIMIT, FixFirstQueue, build_fix_first+from repowise.core.analysis.health.fix_first import (+ DEFAULT_LIMIT,+ FIX_FIRST_MODEL_VERSION,+ FixFirstQueue,+ build_fix_first,+)from repowise.core.analysis.health.fix_first.build import (DEAD_CONFIDENCE,MIN_WORTH,@@ -61,6 +69,7 @@ from ...models import (RefactoringOpportunity,RefactoringSuggestion,)+from ...read_snapshots import decode_or_none, read_snapshot, refresh_snapshot, snapshot_keyfrom ...sql import json_text#: Files read for plan-less finding items, by open code-shape deduction.@@ -517,6 +526,35 @@ async def _stamp(session: AsyncSession, repo_id: str) -> tuple[Any, ...]:return tuple(stamps)+#: The stored production queue's ``read_snapshots.kind``.+SNAPSHOT_KIND = "fix_first"+_KEY = snapshot_key(SNAPSHOT_KIND, FIX_FIRST_MODEL_VERSION)+++async def write_fix_first_snapshot(session: AsyncSession, repository_id: str) -> bool:+ """Store the whole production queue, tests resolved, for the next reader.++ The whole queue rather than its head: an id lookup, a page past the+ first, the counts and the refactoring list's exclusion reasons are all+ read from it. The writer of the stores calls this once they are final.+ Returns whether it wrote (a row still current writes nothing).+ """++ async def build() -> dict[str, Any]:+ full = await _build(session, repository_id, limit=None, scope="production", item_id=None)+ return asdict(full)++ return await refresh_snapshot(session, repository_id, SNAPSHOT_KIND, _KEY, build)+++async def _stored(session: AsyncSession, repo_id: str, scope: str) -> FixFirstQueue | None:+ """The stored production queue, when one is current and fits the model."""+ if scope != "production":+ return None+ payload = await read_snapshot(session, repo_id, SNAPSHOT_KIND, _KEY)+ return decode_or_none(FixFirstQueue, payload) if payload is not None else None++def clear_fix_first_cache() -> None:_cache.clear()@@ -538,7 +576,8 @@ async def load_fix_first(The full queue is built once per store write and every ``limit`` and idis a slice of it: the reads are the same whatever is kept, and writing- every item costs little next to them.+ every item costs little next to them. The production queue comes from the+ stored snapshot while it is current."""base = (str(session.bind.url) if session.bind is not None else None,@@ -553,10 +592,15 @@ async def load_fix_first(# A verified queue answers an unverified ask too; never the reverse.full = _cached((*base, True, None, None)) or _cached((*base, verify, None, None))if full is None:- full = await _build(- session, repository_id, limit=None, scope=scope, item_id=None, verify=verify- )- _remember((*base, verify, None, None), full)+ # The stored queue is verified, so it answers either ask.+ full = await _stored(session, repository_id, scope)+ if full is not None:+ _remember((*base, True, None, None), full)+ else:+ full = await _build(+ session, repository_id, limit=None, scope=scope, item_id=None, verify=verify+ )+ _remember((*base, verify, None, None), full)queue = full if key[-2:] == (None, None) else queue_view(full, limit=limit, item_id=item_id)_remember(key, queue)return queue@@ -628,4 +672,12 @@ async def _build()-__all__ = ["CACHE_SIZE", "FINDING_FILES", "clear_fix_first_cache", "load_fix_first", "queue_view"]+__all__ = [+ "CACHE_SIZE",+ "FINDING_FILES",+ "SNAPSHOT_KIND",+ "clear_fix_first_cache",+ "load_fix_first",+ "queue_view",+ "write_fix_first_snapshot",+]diff --git a/packages/core/src/repowise/core/persistence/database.py b/packages/core/src/repowise/core/persistence/database.pyindex c85947b5f..fcdd55d6b 100644--- a/packages/core/src/repowise/core/persistence/database.py+++ b/packages/core/src/repowise/core/persistence/database.py@@ -29,6 +29,7 @@ from sqlalchemy.pool import NullPool, StaticPoolfrom sqlalchemy.schema import CreateIndex, CreateTablefrom sqlalchemy.sql import text+from . import read_snapshots # noqa: F401 (a write to a view's input drops the stored view)from .models import Baselog = structlog.get_logger(__name__)diff --git a/packages/core/src/repowise/core/persistence/models.py b/packages/core/src/repowise/core/persistence/models.pyindex 9922733b1..a9d58075e 100644--- a/packages/core/src/repowise/core/persistence/models.py+++ b/packages/core/src/repowise/core/persistence/models.py@@ -28,6 +28,7 @@ from sqlalchemy import (Text,UniqueConstraint,false,+ func,)from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column@@ -2336,8 +2337,9 @@ class CoverageIngest(Base):class ActionState(Base):"""A person's answer to one next action: dismissed, snoozed, or done.- Actions themselves are computed on read (``analysis.actions``), so this is- the only stored half. ``action_id`` is stable across re-index because it is+ Actions themselves are derived from the other stores (``analysis.actions``,+ cached in :class:`ReadSnapshot`), so this is the only half a person writes.+ ``action_id`` is stable across re-index because it isderived from the rule and its target. ``fingerprint`` is the one the actioncarried when the person acted; a dismissal holds only while it stillmatches, so an action whose facts changed materially comes back.@@ -2362,6 +2364,35 @@ class ActionState(Base):)+class ReadSnapshot(Base):+ """A read view built at index or update time, served while its key holds.++ ``kind`` names the view (the Fix first queue, the next-actions view) and+ ``key`` the code that built it (repowise and model versions). Any write+ to a store a view reads deletes the rows in the same transaction+ (``persistence.read_snapshots``), so a row that exists is current and a+ missing one is built live. Derived data: losing a row costs one build.+ """++ __tablename__ = "read_snapshots"++ repository_id: Mapped[str] = mapped_column(+ String(32),+ ForeignKey("repositories.id", ondelete="CASCADE"),+ primary_key=True,+ )+ kind: Mapped[str] = mapped_column(String(32), primary_key=True)+ key: Mapped[str] = mapped_column(Text, nullable=False)+ payload_json: Mapped[str] = mapped_column(Text, nullable=False)+ updated_at: Mapped[datetime] = mapped_column(+ DateTime(timezone=True),+ nullable=False,+ default=_now_utc,+ onupdate=_now_utc,+ server_default=func.now(),+ )++class TestCoverageEntry(Base):"""One ``(test, source file)`` coverage fact - the test-to-code map.diff --git a/packages/core/src/repowise/core/persistence/read_snapshots.py b/packages/core/src/repowise/core/persistence/read_snapshots.pynew file mode 100644index 000000000..49797e725--- /dev/null+++ b/packages/core/src/repowise/core/persistence/read_snapshots.py@@ -0,0 +1,250 @@+"""Read views built at index or update time, kept only while their inputs hold.++A view the read path would otherwise rebuild in every new process (the Fix+first queue, the next-actions view) is written once, by the writer that last+touched its stores, under a key naming the code that built it (repowise+version and the view's model version).++Freshness is kept on the write side: any session that writes a store a view+could read deletes every stored view in the same transaction+(:func:`_invalidate`), so a stored row is current by construction and a+missing one is built live. The rule fails closed: a table it does not know+is an input (:data:`UNRELATED_TABLES` lists the ones that are not), a write+in a savepoint that rolled back still invalidates, and a write to one+repository drops every repository's rows in a shared store. Readers never+write. The builders read only the stores; settings that shape them+(``.repowise/health-rules.json``, the exclude spec, scope) reach a view+through the store writes they cause.++Ceilings: SQLite serialises writers, so it is safe; on Postgres under READ+COMMITTED a writer building while another session commits an input write+can store one stale build, which the next input write drops. Writes that+bypass a ``Session`` (``engine.connect()`` / ``engine.begin()`` callers, today+only FTS and schema setup) and processes that never import+``persistence.database`` (an older repowise against the same store) do not+invalidate; the version in the key covers an upgrade, not a downgrade+followed by a return.+"""++from __future__ import annotations++import json+import logging+import re+from collections.abc import Awaitable, Callable+from functools import cache+from typing import Any++from sqlalchemy import delete, event, select+from sqlalchemy.ext.asyncio import AsyncSession+from sqlalchemy.orm import Session++from repowise.core import __version__++from .models import ReadSnapshot++logger = logging.getLogger(__name__)++#: Tables no stored view reads: writing them leaves the views current. Any+#: other table, including one added later, counts as an input. Person-level+#: answers (``action_states``, ``set_action_state``) are applied on every+#: read, so they are not inputs.+UNRELATED_TABLES = frozenset(+ {+ "action_states",+ "answer_cache",+ "chat_messages",+ "conversations",+ "doc_drift_references",+ "external_systems",+ "generation_jobs",+ "git_commit_health_deltas",+ "git_function_blame",+ "graph_node_membership",+ "health_snapshots",+ "kg_node_meta",+ "kg_project_meta",+ "knowledge_graph_layers",+ "knowledge_graph_tour_steps",+ "llm_costs",+ "performance_summaries",+ "pipeline_jobs",+ "read_snapshots",+ "refactoring_summaries",+ "repositories",+ "webhook_events",+ "wiki_page_versions",+ "wiki_pages",+ "wiki_symbols",+ }+)++#: ``session.info`` flag: this transaction wrote a store a view reads.+_STALE = "read_snapshots_stale"++# Fail-safe: any text statement that is not a plain read counts as a write,+# and one whose target cannot be named counts as a write to an input.+_COMMENTS = re.compile(r"^(?:\s+|--[^\n]*\n?|/\*.*?\*/)+", re.DOTALL)+_TEXT_READ = re.compile(r"(?:select|pragma|explain)\b", re.IGNORECASE)+_TEXT_TARGET = re.compile(+ r"(?:insert(?:\s+or\s+\w+)?\s+into|replace\s+into|update(?:\s+or\s+\w+)?|delete\s+from)"+ r"\s+(?:[\"`\[]?\w+[\"`\]]?\.)?[\"`\[]?(\w+)",+ re.IGNORECASE,+)+++def _written_table(statement: Any) -> str | None:+ """The table a write statement targets; ``""`` for a write whose table+ cannot be read (counted as an input); ``None`` for a read."""+ if getattr(statement, "is_dml", False):+ return getattr(getattr(statement, "table", None), "name", "")+ sql = getattr(statement, "text", None)+ if not isinstance(sql, str):+ return None+ sql = _COMMENTS.sub("", sql, count=1)+ if _TEXT_READ.match(sql):+ return None+ found = _TEXT_TARGET.match(sql)+ return found.group(1).lower() if found else ""++[email protected]_for(Session, "do_orm_execute")+def _note_statement(state: Any) -> None:+ table = _written_table(state.statement)+ if table is not None and table not in UNRELATED_TABLES:+ state.session.info[_STALE] = True+++def _pending_input(session: Session) -> bool:+ return any(+ getattr(obj, "__tablename__", "") not in UNRELATED_TABLES+ for obj in (*session.new, *session.dirty, *session.deleted)+ )++[email protected]_for(Session, "before_flush")+def _note_flush(session: Session, _context: Any, _instances: Any) -> None:+ if _pending_input(session):+ session.info[_STALE] = True+++# ``commit`` runs this before its own final flush, so objects still pending+# then are checked here rather than by the flush hook.[email protected]_for(Session, "before_commit")+def _invalidate(session: Session) -> None:+ """Drop the stored views in the transaction that changed their inputs."""+ if session.in_nested_transaction():+ return # a savepoint release is not the commit; the outer one decides+ if not stale(session):+ return+ try:+ with session.begin_nested():+ session.execute(delete(ReadSnapshot))+ except Exception as exc: # a store from before the table holds no views+ logger.debug("read snapshots not cleared: %s", exc)+ # Popped after the delete: the savepoint's autoflush can set it again.+ session.info.pop(_STALE, None)+++def stale(session: Session) -> bool:+ """Whether this transaction wrote, or is about to write, a view's input."""+ return bool(session.info.get(_STALE)) or _pending_input(session)+++async def mark_current(session: AsyncSession) -> None:+ """The views just written reflect every write this transaction made."""+ await session.flush()+ session.info.pop(_STALE, None)+++def snapshot_key(*parts: Any) -> str:+ """The stored form of a key: the repowise version first, then ``parts``.++ Ceiling: a development checkout keeps one version across commits, so a+ builder change there is served stale until a store write or an update+ drops the row; a release changes the version.+ """+ return json.dumps([__version__, *parts], default=str)+++async def read_snapshot(session: AsyncSession, repo_id: str, kind: str, key: str) -> Any | None:+ """The stored payload for ``kind`` when it was built under ``key``.++ Any failure is a miss and the caller builds live: a store from before+ this table, a row another build wrote, a payload that does not parse.+ The savepoint keeps that failure off the caller's session.+ """+ try:+ async with session.begin_nested():+ row = (+ await session.execute(+ select(ReadSnapshot.key, ReadSnapshot.payload_json).where(+ ReadSnapshot.repository_id == repo_id, ReadSnapshot.kind == kind+ )+ )+ ).first()+ if row is None or row.key != key:+ return None+ return json.loads(row.payload_json)+ except Exception as exc: # build live instead+ logger.debug("read_snapshot %s unavailable: %s", kind, exc)+ return None+++async def refresh_snapshot(+ session: AsyncSession,+ repo_id: str,+ kind: str,+ key: str,+ build: Callable[[], Awaitable[Any]],+) -> bool:+ """Store ``await build()`` (JSON-ready) as the ``kind`` row under ``key``,+ unless the row already holds that key and this transaction has written+ no input (such a row would be dropped at commit). Returns whether it+ wrote.++ Two writers racing on one row (two updates of one store) can fail on the+ primary key; the caller treats any failure as "no snapshot", which a+ reader answers by building live.+ """+ row = await session.get(ReadSnapshot, (repo_id, kind))+ if row is not None and row.key == key and not stale(session.sync_session):+ return False+ text = json.dumps(await build(), ensure_ascii=False)+ if row is None:+ session.add(ReadSnapshot(repository_id=repo_id, kind=kind, key=key, payload_json=text))+ else:+ row.key, row.payload_json = key, text+ await session.flush()+ return True+++@cache+def _adapter(tp: type) -> Any:+ # pydantic ships with repowise; imported here so opening a store stays cheap.+ from pydantic import TypeAdapter++ return TypeAdapter(tp)+++def decode_or_none(tp: type, value: Any) -> Any | None:+ """``value`` (``dataclasses.asdict`` through JSON) back as a ``tp``, or+ ``None`` for a payload that does not fit (an older or newer build's)."""+ from pydantic import ValidationError++ try:+ return _adapter(tp).validate_python(value)+ except (ValidationError, TypeError, ValueError) as exc:+ logger.info("stored %s no longer fits the model: %s", getattr(tp, "__name__", tp), exc)+ return None+++__all__ = [+ "UNRELATED_TABLES",+ "decode_or_none",+ "mark_current",+ "read_snapshot",+ "refresh_snapshot",+ "snapshot_key",+ "stale",+]diff --git a/packages/core/src/repowise/core/pipeline/full_index.py b/packages/core/src/repowise/core/pipeline/full_index.pyindex eec1aa26c..ac59223e1 100644--- a/packages/core/src/repowise/core/pipeline/full_index.py+++ b/packages/core/src/repowise/core/pipeline/full_index.py@@ -83,6 +83,10 @@ async def index_repo_full()or [])+ # Every store is final: the views the overview and dashboard read.+ from repowise.core.persistence.crud.analysis.actions import write_read_snapshots++ await write_read_snapshots(session, repo.id)from repowise.core.pipeline.cleanup_debt import (clear_cleanup_debt,diff --git a/packages/server/src/repowise/server/job_executor.py b/packages/server/src/repowise/server/job_executor.pyindex c17ad937a..cb1fdb448 100644--- a/packages/server/src/repowise/server/job_executor.py+++ b/packages/server/src/repowise/server/job_executor.py@@ -653,6 +653,11 @@ async def _persist_index_result(run: _JobRun, result: Any, incremental_pages: liawait rebuild_page_tree(session, repo_id)+ # Every store is final: the views the overview and dashboard read.+ from repowise.core.persistence.crud.analysis.actions import write_read_snapshots++ await write_read_snapshots(session, repo_id)+# Vector deletes before the SQL commit: a separate store, so no lock# conflict, and the idempotent delete keeps the durable commit last.if swept_page_ids and run.vector_store is not None:diff --git a/tests/unit/cli/test_editor_setup.py b/tests/unit/cli/test_editor_setup.pyindex 22c194f9b..b0ea1ec85 100644--- a/tests/unit/cli/test_editor_setup.py+++ b/tests/unit/cli/test_editor_setup.py@@ -1223,17 +1223,19 @@ def test_claude_refresh_project_files_skips_when_options_disable_file(def test_update_command_uses_editor_refresh_abstraction() -> None:- from repowise.cli.commands.update_cmd.command import _refresh_editor_stamp+ from repowise.cli.commands.update_cmd.command import _finish_outcome, _refresh_editor_stampcommand_source = inspect.getsource(run_update)+ finish_source = inspect.getsource(_finish_outcome)stamp_source = inspect.getsource(_refresh_editor_stamp)- # The command routes every editor-file write through the shared stamp- # helper, which in turn uses the refresh abstraction — never the raw- # generator/fetcher internals.- assert "_refresh_editor_stamp" in command_source+ # The command routes every editor-file write through the shared exit+ # helper and its stamp helper, which in turn uses the refresh abstraction —+ # never the raw generator/fetcher internals.+ assert "_finish_outcome" in command_source+ assert "_refresh_editor_stamp" in finish_sourceassert "refresh_editor_project_files" in stamp_source- for source in (command_source, stamp_source):+ for source in (command_source, finish_source, stamp_source):assert "ClaudeMdGenerator" not in sourceassert "EditorFileDataFetcher" not in sourceassert "claude_md" not in sourcediff --git a/tests/unit/cli/test_read_snapshot_refresh.py b/tests/unit/cli/test_read_snapshot_refresh.pynew file mode 100644index 000000000..2682ff1a8--- /dev/null+++ b/tests/unit/cli/test_read_snapshot_refresh.py@@ -0,0 +1,72 @@+"""Index and update leave the read snapshots behind for the next reader."""++from __future__ import annotations++import asyncio+import sqlite3+import subprocess+from pathlib import Path++from repowise.cli.commands.update_cmd.persistence import refresh_read_snapshots+++def _git(repo: Path, *args: str) -> None:+ subprocess.run(["git", *args], cwd=str(repo), capture_output=True, check=True)+++def _kinds(repo: Path) -> list[str]:+ with sqlite3.connect(repo / ".repowise" / "wiki.db") as db:+ return sorted(r[0] for r in db.execute("SELECT kind FROM read_snapshots"))+++def test_index_writes_and_update_restores_the_snapshots(tmp_path: Path) -> None:+ from repowise.core.pipeline.full_index import index_repo_full++ repo = tmp_path / "repo"+ (repo / "src").mkdir(parents=True)+ (repo / "src" / "app.py").write_text("def run(x):\n return x + 1\n")+ _git(repo, "init")+ _git(repo, "config", "user.email", "[email protected]")+ _git(repo, "config", "user.name", "T")+ _git(repo, "add", ".")+ _git(repo, "commit", "-m", "c0")+ (repo / ".repowise").mkdir()++ asyncio.run(index_repo_full(repo))+ assert _kinds(repo) == ["actions", "fix_first"]++ with sqlite3.connect(repo / ".repowise" / "wiki.db") as db:+ db.execute("DELETE FROM read_snapshots")+ refresh_read_snapshots(repo)+ assert _kinds(repo) == ["actions", "fix_first"]+++def test_every_update_outcome_ends_in_one_helper() -> None:+ """Each outcome of ``repowise update`` refreshes the views through one exit."""+ import inspect++ from repowise.cli.commands.update_cmd import command++ source = inspect.getsource(command)+ assert source.count("_refresh_editor_stamp(repo_path") == 1 # only inside the helper+ assert source.count("refresh_read_snapshots(repo_path)") == 1+ assert source.count("_finish_outcome(repo_path") == 5+++def test_the_up_to_date_outcome_refreshes_the_views(tmp_path: Path, monkeypatch) -> None:+ from repowise.cli.commands.update_cmd import command as upd_cmd+ from tests.unit.cli.test_update_up_to_date_lock import (+ _indexed_repo,+ _install_recorders,+ _invoke_update,+ )++ repo, _head = _indexed_repo(tmp_path)+ calls: dict[str, int] = {}+ _install_recorders(monkeypatch, calls)+ monkeypatch.setattr(upd_cmd, "try_acquire_update_lock", lambda *_: None)+ monkeypatch.setattr(+ upd_cmd, "refresh_read_snapshots", lambda _p: calls.update(views=calls.get("views", 0) + 1)+ )+ _invoke_update(repo)+ assert calls.get("views") == 1diff --git a/tests/unit/persistence/test_models.py b/tests/unit/persistence/test_models.pyindex feb9f63ed..aa7078504 100644--- a/tests/unit/persistence/test_models.py+++ b/tests/unit/persistence/test_models.py@@ -334,6 +334,7 @@ def test_base_includes_all_models():"coverage_files","coverage_ingests","action_states",+ "read_snapshots","test_coverage","pipeline_jobs","graph_metrics",diff --git a/tests/unit/persistence/test_read_snapshots.py b/tests/unit/persistence/test_read_snapshots.pynew file mode 100644index 000000000..fdab1bb40--- /dev/null+++ b/tests/unit/persistence/test_read_snapshots.py@@ -0,0 +1,363 @@+"""Read snapshots: the Fix first queue and the actions view stored at index time.++Written once the stores are final, served while current, dropped by any write+to a store they read, equal to a live build, and never written on read.+"""++from __future__ import annotations++import json+from dataclasses import asdict, dataclass+from types import SimpleNamespace++import pytest+from sqlalchemy import func, select, text, update++from repowise.core.analysis.health.fix_first import FIX_KINDS, FixFirstQueue, build_fix_first+from repowise.core.persistence.crud.analysis import actions as actions_loader+from repowise.core.persistence.crud.analysis import fix_first as fix_first_loader+from repowise.core.persistence.crud.analysis.actions import (+ load_actions_view,+ set_action_state,+ write_read_snapshots,+)+from repowise.core.persistence.crud.analysis.coverage_map import save_test_coverage+from repowise.core.persistence.crud.analysis.fix_first import (+ clear_fix_first_cache,+ load_fix_first,+)+from repowise.core.persistence.models import (+ GraphEdge,+ HealthFinding,+ ReadSnapshot,+ SecurityFinding,+)+from repowise.core.persistence.read_snapshots import _written_table, decode_or_none+from tests.unit.health.fix_first_rows import FINDINGS, METRICS, PERFORMANCE, PLANS, REFACTORING+from tests.unit.persistence.test_actions_loader import NOW, _seed+++def _wire(value) -> str:+ return json.dumps(value, sort_keys=False, default=str)+++async def _live(session, rid: str) -> tuple[str, str, dict]:+ clear_fix_first_cache()+ view = await load_actions_view(session, rid, now=NOW)+ queue = await load_fix_first(session, rid, limit=None)+ return _wire(view), _wire(queue.as_dict()), dict(queue.refactoring_reasons)+++async def _snapshot_rows(session) -> int:+ return (await session.execute(select(func.count()).select_from(ReadSnapshot))).scalar_one()+++def no_live_build(monkeypatch):+ """Fail any live build, so a read that succeeds came from the snapshot."""++ async def refuse(*_args, **_kwargs):+ raise AssertionError("built live")++ def arm() -> None:+ clear_fix_first_cache()+ monkeypatch.setattr(fix_first_loader, "_build", refuse)+ monkeypatch.setattr(actions_loader, "_ruled", refuse)++ return arm+++async def test_persist_writes_both_snapshots_once(async_session) -> None:+ rid = await _seed(async_session)+ await write_read_snapshots(async_session, rid)+ kinds = (await async_session.execute(select(ReadSnapshot.kind))).scalars().all()+ assert sorted(kinds) == ["actions", "fix_first"]+ # Nothing moved since, so a second pass writes nothing.+ assert not await fix_first_loader.write_fix_first_snapshot(async_session, rid)+ assert not await actions_loader.write_actions_snapshot(async_session, rid)+++async def test_a_fresh_snapshot_is_served_and_equals_a_live_build(+ async_session, no_live_build+) -> None:+ rid = await _seed(async_session)+ view, queue, reasons = await _live(async_session, rid)+ await write_read_snapshots(async_session, rid)+ await async_session.commit()++ no_live_build()+ served = await load_fix_first(async_session, rid, limit=None)+ assert _wire(served.as_dict()) == queue+ assert served.refactoring_reasons == reasons+ assert _wire(await load_actions_view(async_session, rid, now=NOW)) == view+ # Every limit and id is a slice of the stored queue.+ lead = served.items[0]+ assert (await load_fix_first(async_session, rid, limit=1)).items == (lead,)+ assert (await load_fix_first(async_session, rid, item_id=lead.id)).find(lead.id) == lead+++async def test_a_persons_answer_applies_to_the_stored_view(async_session, no_live_build) -> None:+ rid = await _seed(async_session)+ await write_read_snapshots(async_session, rid)+ first = (await load_actions_view(async_session, rid, now=NOW))["horizons"]["quarter"]+ action = first["actions"][0]+ await set_action_state(+ async_session, rid, action["id"], state="dismissed", fingerprint=action["fingerprint"]+ )+ await async_session.commit()++ no_live_build()+ after = (await load_actions_view(async_session, rid, now=NOW))["horizons"]["quarter"]+ assert action["id"] not in {a["id"] for a in after["actions"]}+ assert after["hidden"] == first["hidden"] + 1+++async def _triage(session, rid) -> None:+ finding = (+ await session.execute(select(HealthFinding).where(HealthFinding.function_name == "run"))+ ).scalar_one()+ finding.status = "dismissed"+++async def _new_secret(session, rid) -> None:+ session.add(+ SecurityFinding(repository_id=rid, file_path="src/cfg.py", kind="hardcoded_secret",+ line_number=3, snippet='KEY = "sk_live_9"', severity="high", commit_sha="")+ )+++async def _coverage_ingest(session, rid) -> None:+ record = SimpleNamespace(test_id="tests/test_core.py::test_run", test_file="tests/test_core.py",+ file_path="src/core.py", covered_lines=[12, 13])+ await save_test_coverage(session, rid, [record], source_format="coverage.py")+++async def _graph_text_write(session, rid) -> None:+ await session.execute(+ text("UPDATE graph_nodes SET start_line = 7 WHERE repository_id = :r"), {"r": rid}+ )+++async def _graph_bulk_write(session, rid) -> None:+ await session.execute(+ update(GraphEdge).where(GraphEdge.repository_id == rid).values(edge_type="calls")+ )+++ "change", [_triage, _new_secret, _coverage_ingest, _graph_text_write, _graph_bulk_write]+)+async def test_a_write_to_an_input_drops_the_snapshots(async_session, change) -> None:+ rid = await _seed(async_session)+ await write_read_snapshots(async_session, rid)+ await async_session.commit()+ assert await _snapshot_rows(async_session) == 2++ await change(async_session, rid)+ await async_session.commit()+ assert await _snapshot_rows(async_session) == 0+ view, queue, _ = await _live(async_session, rid)+ assert _wire((await load_fix_first(async_session, rid, limit=None)).as_dict()) == queue+ assert _wire(await load_actions_view(async_session, rid, now=NOW)) == view+++async def test_a_dropped_snapshot_would_have_been_wrong(async_session) -> None:+ rid = await _seed(async_session)+ before = await _live(async_session, rid)+ await _triage(async_session, rid)+ await _new_secret(async_session, rid)+ await async_session.commit()+ view, queue, _ = await _live(async_session, rid)+ assert view != before[0] and queue != before[1]+++async def test_writes_elsewhere_keep_the_snapshots(async_session) -> None:+ rid = await _seed(async_session)+ await write_read_snapshots(async_session, rid)+ await async_session.commit()+ await async_session.execute(+ text("UPDATE repositories SET head_commit = 'abc' WHERE id = :r"), {"r": rid}+ )+ async with async_session.begin_nested():+ await async_session.execute(select(HealthFinding.id)) # a read in a savepoint+ await async_session.commit()+ assert await _snapshot_rows(async_session) == 2+++async def test_writes_before_the_snapshot_in_one_session_keep_it(async_session) -> None:+ rid = await _seed(async_session) # pending input rows in this session+ await _new_secret(async_session, rid)+ await write_read_snapshots(async_session, rid)+ await async_session.commit()+ assert await _snapshot_rows(async_session) == 2+++async def _stored_payload(session, kind: str) -> dict:+ row = await session.get(ReadSnapshot, (await _repo_id(session), kind))+ return json.loads(row.payload_json)+++async def _repo_id(session) -> str:+ return (await session.execute(select(ReadSnapshot.repository_id))).scalars().first()+++async def _replace_payload(session, kind: str, payload: dict) -> None:+ row = await session.get(ReadSnapshot, (await _repo_id(session), kind))+ row.payload_json = json.dumps(payload)+ await session.flush()++[email protected]("kind", ["fix_first", "actions"])+async def test_a_payload_an_older_build_wrote_is_a_miss(async_session, kind) -> None:+ rid = await _seed(async_session)+ view, queue, _ = await _live(async_session, rid)+ await write_read_snapshots(async_session, rid)+ payload = await _stored_payload(async_session, kind)+ if kind == "fix_first":+ del payload["items"][0]["verify"]+ else:+ del payload["actions"][0][1]["tier"]+ await _replace_payload(async_session, kind, payload)++ clear_fix_first_cache()+ assert _wire((await load_fix_first(async_session, rid, limit=None)).as_dict()) == queue+ assert _wire(await load_actions_view(async_session, rid, now=NOW)) == view+++async def test_a_read_never_writes_a_snapshot(async_session) -> None:+ rid = await _seed(async_session)+ await async_session.commit()+ await _live(async_session, rid)+ assert await _snapshot_rows(async_session) == 0+ assert not async_session.new and not async_session.dirty+++async def test_a_store_without_the_table_builds_live(async_session) -> None:+ rid = await _seed(async_session)+ await async_session.execute(text("DROP TABLE read_snapshots"))+ await async_session.commit()+ view = await load_actions_view(async_session, rid, now=NOW)+ assert view["unavailable"] == {}+ assert (await load_fix_first(async_session, rid, limit=None)).items+++def test_decode_inverts_asdict_over_every_item_kind() -> None:+ profile = {"via": "call", "tests": ["tests/test_core.py"], "total": 3, "basis": "inferred",+ "commands": ["pytest tests/test_core.py"]}+ queue = build_fix_first(metrics=METRICS, findings=FINDINGS, refactoring=REFACTORING,+ performance=PERFORMANCE, plans=PLANS, limit=None,+ validate=lambda *_: profile)+ assert {i.kind for i in queue.items} == set(FIX_KINDS)+ assert any(i.verify.tests for i in queue.items) and queue.refactoring_reasons+ stored = json.loads(json.dumps(asdict(queue)))+ assert decode_or_none(FixFirstQueue, stored) == queue+++@dataclass(frozen=True)+class Leaf:+ n: int+++@dataclass(frozen=True)+class Node:+ pair: tuple[str, Leaf]+ either: Leaf | str | None+ many: tuple[Leaf, ...] = ()+++def test_decode_fixed_tuples_and_unions() -> None:+ raw = {"pair": ["a", {"n": 1}], "either": {"n": 2}, "many": [{"n": 3}]}+ assert decode_or_none(Node, raw) == Node(("a", Leaf(1)), Leaf(2), (Leaf(3),))+ assert decode_or_none(Node, {**raw, "either": "text"}).either == "text"+ assert decode_or_none(Node, {**raw, "pair": ["a"]}) is None+ assert decode_or_none(Node, {"either": None}) is None+++async def test_a_rewrite_in_the_writing_session_replaces_a_current_row(async_session) -> None:+ """Re-index: the stores and the views written in one session, over a row+ whose key still matches. The views must be rebuilt, not kept."""+ rid = await _seed(async_session)+ await write_read_snapshots(async_session, rid)+ await async_session.commit()++ await _triage(async_session, rid) # an input write, not yet committed+ await write_read_snapshots(async_session, rid)+ await async_session.commit()+ assert await _snapshot_rows(async_session) == 2+ view, queue, _ = await _live(async_session, rid)+ clear_fix_first_cache()+ stored = await load_fix_first(async_session, rid, limit=None)+ assert _wire(stored.as_dict()) == queue+ assert "src/core.py" not in {i.target.file_path for i in stored.items}+ assert _wire(await load_actions_view(async_session, rid, now=NOW)) == view+++async def test_a_failed_view_write_leaves_nothing_stale(async_session, monkeypatch) -> None:+ rid = await _seed(async_session)+ await write_read_snapshots(async_session, rid)+ await async_session.commit()++ async def broken(*_args):+ raise RuntimeError("build failed")++ monkeypatch.setattr(actions_loader, "write_actions_snapshot", broken)+ await _triage(async_session, rid)+ await write_read_snapshots(async_session, rid)+ await async_session.commit()+ assert await _snapshot_rows(async_session) == 0+++async def test_a_rolled_back_savepoint_still_invalidates(async_session) -> None:+ rid = await _seed(async_session)+ await write_read_snapshots(async_session, rid)+ await async_session.commit()+ savepoint = await async_session.begin_nested()+ await _graph_text_write(async_session, rid)+ await savepoint.rollback()+ await async_session.commit()+ assert await _snapshot_rows(async_session) == 0+++async def test_one_sessions_write_does_not_mark_another(async_session, session_factory) -> None:+ rid = await _seed(async_session)+ await write_read_snapshots(async_session, rid)+ await async_session.commit()+ async with session_factory() as other:+ await _graph_text_write(other, rid) # flagged, never committed+ async with session_factory() as reader:+ await reader.execute(select(HealthFinding.id))+ await reader.commit()+ await other.rollback()+ assert await _snapshot_rows(async_session) == 2+++async def test_a_corrupt_payload_is_a_miss(async_session) -> None:+ rid = await _seed(async_session)+ view, queue, _ = await _live(async_session, rid)+ await write_read_snapshots(async_session, rid)+ for kind in ("fix_first", "actions"):+ row = await async_session.get(ReadSnapshot, (rid, kind))+ row.payload_json = "{not json"+ await async_session.flush()+ clear_fix_first_cache()+ assert _wire((await load_fix_first(async_session, rid, limit=None)).as_dict()) == queue+ assert _wire(await load_actions_view(async_session, rid, now=NOW)) == view+++ ("sql", "table"),+ [+ ("SELECT 1", None),+ (" -- note\n select 2", None),+ ("/* x */ PRAGMA table_info(t)", None),+ ("EXPLAIN SELECT 1", None),+ ("WITH r AS (SELECT 1) UPDATE git_metadata SET a = 1", ""),+ ("UPDATE main.graph_nodes SET x = 1", "graph_nodes"),+ ('DELETE FROM "wiki_pages"', "wiki_pages"),+ ("-- note\nUPDATE wiki_pages SET x = 1", "wiki_pages"),+ ("DROP TABLE x", ""),+ ("VACUUM", ""),+ ],+)+def test_text_sql_counts_as_a_write_unless_it_is_a_plain_read(sql, table) -> None:+ assert _written_table(text(sql)) == table