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(events): measure contract events after extraction and drop the attribution_jump kind (#3861)

Raghav Chamadiyaf66b06d0d74a
diff --git a/docs/reference/CLI_REFERENCE.md b/docs/reference/CLI_REFERENCE.md
index 59a896ba7..17299d856 100644
--- a/docs/reference/CLI_REFERENCE.md
+++ b/docs/reference/CLI_REFERENCE.md
@@ -1648,13 +1648,13 @@ Teams that already run OPA can use the `evidence` object of `repowise check --fo
### `repowise events`
-Typed events between consecutive index runs. Each `init` and `update` stores a snapshot of every component (ownership counts, bus factor, hotspots, dead code, governing decisions, provided contracts, test reach, attribution with coverage) under the analyzer and parser stamp it ran with. When an index run and the run right before it share a stamp, what moved is stored as an event. A stamp change starts a new baseline, and so does the first run after one that took no snapshot (events switched off, or the snapshot failed); a run on the commit already snapshotted, including `--working-tree` on it, is skipped. Events are written as CloudEvents JSON lines to `.repowise/events.jsonl` and read back here, oldest first.
+Typed events between consecutive index runs. Each `init` and `update` stores a snapshot of every component (ownership counts, bus factor, hotspots, dead code, governing decisions, provided contracts, test reach) under the analyzer and parser stamp it ran with. When an index run and the run right before it share a stamp, what moved is stored as an event. A stamp change starts a new baseline, and so does the first run after one that took no snapshot (events switched off, or the snapshot failed); a run on the commit already snapshotted, including `--working-tree` on it, is skipped. Events are written as CloudEvents JSON lines to `.repowise/events.jsonl` and read back here, oldest first.
```bash
repowise events [--path PATH] [--since 7d|12h|ISO-DATE] [--kind KIND]... [--limit N] [--format table|json|jsonl]
```
-Kinds: `ownership_shift`, `owner_inactive`, `bus_factor_drop` (critical components), `new_hotspot` (off by default), `contract_added`, `contract_broken`, `decision_stale`, `dead_code_growth`, `test_reach_drop`, `attribution_jump` (regulated components). The `events:` block in `.repowise/config.yaml` sets `enabled`, `retention_days` (365), `compact_after_days` (30; older runs keep one per week), per-kind switches under `kinds`, and the `sinks` to deliver to (`jsonl` by default, `stdout` for CI).
+Kinds: `ownership_shift`, `owner_inactive`, `bus_factor_drop` (critical components), `new_hotspot` (off by default), `contract_added`, `contract_broken`, `decision_stale`, `dead_code_growth`, `test_reach_drop`. Contract kinds need a workspace: after the cross-repo pass extracts contracts at the run's commit, the run's contract facts are measured and its contract events stored; outside a workspace contracts stay unavailable and those kinds never fire. An index-only `update` (the post-commit hook) skips the cross-repo pass to stay fast, so its run keeps contracts unavailable; contract kinds compare each measured run with the last earlier run that measured them, passing over those runs. Known limit: consumer counts are read at the provider's own runs, so a change in a consumer repo alone moves no contract event. Known limit: past `compact_after_days` compaction keeps one run per week, so the measured run a later run would compare with can be dropped, and the comparison falls back to an older measured run or none. The `events:` block in `.repowise/config.yaml` sets `enabled`, `retention_days` (365), `compact_after_days` (30; older runs keep one per week), per-kind switches under `kinds`, and the `sinks` to deliver to (`jsonl` by default, `stdout` for CI).
The web UI lists the same events on the repository's Events page, newest first. An empty list says why: `GET /api/repos/{id}/events` returns `snapshot_runs` and `empty_reason` (`baseline_only` until two snapshots exist, `nothing_moved`, or `no_match` under filters), and the same sentence this command prints as `empty_note`.
diff --git a/packages/core/src/repowise/core/events/diff.py b/packages/core/src/repowise/core/events/diff.py
index c11b09a90..49e5d7aaf 100644
--- a/packages/core/src/repowise/core/events/diff.py
+++ b/packages/core/src/repowise/core/events/diff.py
@@ -14,8 +14,6 @@ from typing import Any
#: Criticality levels a "critical component" rule applies to.
CRITICAL_LEVELS = frozenset({"high", "critical"})
-#: Component tags that make attribution moves worth an event.
-REGULATED_TAGS = frozenset({"regulated", "pci", "cra-product"})
#: Below this many owned files ownership figures are anecdotes.
MIN_OWNED_FILES = 5
@@ -29,9 +27,6 @@ TEST_REACH_MIN_DROP = 0.10
TEST_REACH_MIN_FILES = 5
#: Measured line coverage must fall by this many points.
LINE_COVERAGE_MIN_DROP = 5.0
-#: Agent share must move by this much, over at least this many commits per run.
-ATTRIBUTION_MIN_DELTA = 0.20
-ATTRIBUTION_MIN_COMMITS = 10
Occurrence = tuple[str, str, str, dict[str, Any]]
@@ -50,6 +45,13 @@ def diff_snapshots(
return out
+def contract_occurrences(
+ component: str, before: Mapping[str, Any], after: Mapping[str, Any]
+) -> list[Occurrence]:
+ """The contract rule alone, for one component's facts at two runs."""
+ return [(kind, component, subject, ev) for kind, subject, ev in _contracts(before, after)]
+
+
def _get(facts: Mapping[str, Any], key: str) -> dict[str, Any]:
value = facts.get(key)
return value if isinstance(value, dict) else {}
@@ -190,27 +192,6 @@ def _test_reach(before: Mapping[str, Any], after: Mapping[str, Any]) -> Iterator
yield "test_reach_drop", "", ev
-def _attribution(before: Mapping[str, Any], after: Mapping[str, Any]) -> Iterator[tuple]:
- if not REGULATED_TAGS & set(after.get("tags") or ()):
- return
- a, b = _get(before, "attribution"), _get(after, "attribution")
- if a.get("agent_share") is None or b.get("agent_share") is None:
- return
- if a.get("recent_capped") or b.get("recent_capped"):
- return
- if min(a.get("commits", 0), b.get("commits", 0)) < ATTRIBUTION_MIN_COMMITS:
- return
- if round(abs(b["agent_share"] - a["agent_share"]), 3) >= ATTRIBUTION_MIN_DELTA:
- yield "attribution_jump", "", {
- "basis": "measured",
- "previous_agent_share": a["agent_share"],
- "agent_share": b["agent_share"],
- "coverage": b.get("coverage"),
- "commits": b.get("commits"),
- "window_days": b.get("window_days"),
- }
-
-
_RULES = (
_ownership,
_owner_inactive,
@@ -220,5 +201,4 @@ _RULES = (
_decisions,
_dead_code,
_test_reach,
- _attribution,
)
diff --git a/packages/core/src/repowise/core/events/model.py b/packages/core/src/repowise/core/events/model.py
index a4bbd68e1..2c123504d 100644
--- a/packages/core/src/repowise/core/events/model.py
+++ b/packages/core/src/repowise/core/events/model.py
@@ -47,7 +47,6 @@ KINDS: dict[str, EventKind] = {
_kind("decision_stale", "decision", "stale", True, "a governing decision went stale"),
_kind("dead_code_growth", "deadcode", "grew", True, "dead-code findings grew"),
_kind("test_reach_drop", "testreach", "dropped", True, "test reach fell"),
- _kind("attribution_jump", "attribution", "jumped", True, "agent share moved sharply"),
)
}
diff --git a/packages/core/src/repowise/core/events/snapshot.py b/packages/core/src/repowise/core/events/snapshot.py
index bbdf47b71..87e8c115a 100644
--- a/packages/core/src/repowise/core/events/snapshot.py
+++ b/packages/core/src/repowise/core/events/snapshot.py
@@ -2,8 +2,8 @@
The figures reuse the component rollups (:func:`component_rollups`) and add
what a diff needs on top: the full hotspot set, observed ownership as opaque
-digests, governing decisions with their staleness, the provided contract set,
-and the agent share of recent commits with its coverage. Nothing here names a
+digests, governing decisions with their staleness, and the provided contract
+set. Nothing here names a
person: owners are counted, and the main-owner set is kept only as HMAC digests
under a per-repository random key (``.repowise/events.key``), which is all a
set comparison between two runs needs. A digest cannot be checked against a
@@ -29,7 +29,7 @@ logger = logging.getLogger(__name__)
#: Bumped when a fact's definition changes; part of the stamp.
SNAPSHOT_FACTS_VERSION = 1
-#: Window for "recent" activity and attribution, back from the newest indexed commit.
+#: Window for "recent" activity, back from the newest indexed commit.
RECENT_DAYS = 90
#: A governing decision at or above this staleness counts as stale
#: (the threshold ``get_stale_decisions`` uses).
@@ -91,19 +91,11 @@ async def collect_facts(
repo_root: Path,
*,
head_commit: str | None,
- attribution: bool,
) -> dict[str, dict[str, Any]]:
"""Facts per component name for the repository's current index."""
- from repowise.core.components.store import (
- component_rollups,
- load_repo_components,
- workspace_services,
- )
+ from repowise.core.components.store import component_rollups
- found = await load_repo_components(
- session, repository_id, repo_root, services=workspace_services(repo_root)
- )
- files = {name: paths for name, paths in found.resolved.files.items() if paths}
+ found, files = await _components(session, repository_id, repo_root)
if not files:
return {}
rollups = await component_rollups(session, repository_id, files, found.test_files)
@@ -131,7 +123,7 @@ async def collect_facts(
rolled = rollups.get(comp.name, {})
commits = {r["sha"]: r for p in paths for r in recent_by_file.get(p, ())}
facts: dict[str, Any] = {
- "definition": _digest(json.dumps([sorted(comp.paths), comp.root, comp.source])),
+ "definition": _definition(comp),
"criticality": comp.criticality,
"tags": sorted(comp.tags),
# Named only as a group; any other owner ref is compared by digest.
@@ -148,12 +140,39 @@ async def collect_facts(
"decisions": _component_decisions(paths, governing, stale),
"contracts": _component_contracts(paths, contracts),
}
- if attribution:
- facts["attribution"] = _attribution(commits.values(), capped)
out[comp.name] = facts
return out
+async def contract_facts(
+ session: Any, repository_id: str, repo_root: Path, *, head_commit: str | None
+) -> dict[str, tuple[str, dict[str, Any]]] | None:
+ """``{component: (definition, contracts fact)}`` measured now; ``None``
+ while the contracts at *head_commit* are unavailable."""
+ contracts = provided_contracts(repo_root, head_commit)
+ if contracts is None:
+ return None
+ found, files = await _components(session, repository_id, repo_root)
+ return {
+ comp.name: (_definition(comp), _component_contracts(files[comp.name], contracts))
+ for comp in found.resolved.components
+ if files.get(comp.name)
+ }
+
+
+async def _components(session: Any, repository_id: str, repo_root: Path) -> tuple[Any, dict]:
+ from repowise.core.components.store import load_repo_components, workspace_services
+
+ found = await load_repo_components(
+ session, repository_id, repo_root, services=workspace_services(repo_root)
+ )
+ return found, {name: paths for name, paths in found.resolved.files.items() if paths}
+
+
+def _definition(comp: Any) -> str:
+ return _digest(json.dumps([sorted(comp.paths), comp.root, comp.source]))
+
+
async def _git_rows(session: Any, repository_id: str) -> dict[str, dict[str, Any]]:
from repowise.core.persistence.crud import code_file_rows
from repowise.core.persistence.models import GitMetadata
@@ -189,8 +208,6 @@ async def _recent_commit_files(session: Any, repository_id: str) -> list[dict[st
GitCommit.sha,
GitCommit.author_name,
GitCommit.author_email,
- GitCommit.attribution,
- GitCommit.agent_name,
)
.join(
GitCommit,
@@ -323,20 +340,3 @@ def _component_contracts(
for cid, n in contracts.get(p, {}).items():
provided[cid] = provided.get(cid, 0) + n
return {"basis": "measured", "provided": dict(sorted(provided.items()))}
-
-
-def _attribution(commits: Any, capped: bool) -> dict[str, Any]:
- from repowise.core.attribution import AGENT, HUMAN_ATTESTED, attribution_share, commit_label
-
- labels = Counter(commit_label(c["attribution"], c["agent_name"]) for c in commits)
- share = attribution_share(labels[AGENT], labels[HUMAN_ATTESTED], sum(labels.values()))
- return {
- "window_days": RECENT_DAYS,
- "commits": share.total,
- "agent": share.agent,
- "human_attested": share.human_attested,
- "coverage": None if share.coverage is None else round(share.coverage, 3),
- "agent_share": None if share.agent_share is None else round(share.agent_share, 3),
- "suppressed_reason": share.suppressed_reason,
- "recent_capped": capped,
- }
diff --git a/packages/core/src/repowise/core/events/store.py b/packages/core/src/repowise/core/events/store.py
index ce22369b9..c53840a03 100644
--- a/packages/core/src/repowise/core/events/store.py
+++ b/packages/core/src/repowise/core/events/store.py
@@ -6,7 +6,9 @@ just before this one and carries the same stamp, it stores the typed
occurrences between the two and hands them to the configured sinks. A stamp
change (a new analyzer, parser, git-history or contract-extraction version)
starts a fresh baseline instead: figures computed under different rules are
-not comparable, so their difference is not an event.
+not comparable, so their difference is not an event. The contract kinds
+compare against the last run that measured contracts instead, since a run
+between them may have had none available (:func:`_contract_pairs`).
An index run that takes no snapshot (events switched off, or the snapshot
failed) leaves a gap marker, so the next snapshot is a baseline rather than a
@@ -34,13 +36,24 @@ from typing import Any
from sqlalchemy import delete, select
-from .diff import diff_snapshots
+from .diff import contract_occurrences, diff_snapshots
from .model import EVENT_SCHEMA_VERSION, KINDS, EngineEvent, event_id, severity_for
logger = logging.getLogger(__name__)
#: Stamp prefix of a gap marker: an index run with no snapshot.
GAP_STAMP = "gap:"
+#: Kinds read from the contracts the workspace's cross-repo pass extracts.
+CONTRACT_KINDS = frozenset({"contract_added", "contract_broken"})
+#: Earlier runs read when looking for a component's last measured contracts.
+#: Ceiling: past it a contract change waits for the next measured pair.
+#: Upgrade path: a column on the run saying its contracts were measured.
+CONTRACT_LOOKBACK_RUNS = 100
+#: ``session.info`` keys of the events waiting for the caller's commit.
+_PENDING = "repowise.events.pending"
+_LISTENING = "repowise.events.listening"
+#: Config errors already logged by this process; each is named once.
+_WARNED: set[str] = set()
@dataclass(frozen=True)
@@ -139,12 +152,10 @@ async def record_snapshot(
found nothing to do, or ``--working-tree`` on the same commit: uncommitted
work is not history).
- Ceiling: sinks are called before the caller commits, because this runs
- inside the index run's session. An event id is keyed on the previous run,
- so a retry after a failed commit re-delivers the same id and a consumer
- dedupes on it. Upgrade path: a per-sink cursor read after commit.
+ The events reach the sinks once the caller's session commits
+ (:func:`_deliver_after_commit`); a transaction that rolls back delivers
+ nothing.
"""
- from repowise.core.attribution import attribution_enabled
from repowise.core.persistence.models import ComponentSnapshot, Repository, SnapshotRun
from .snapshot import collect_facts, current_stamp
@@ -155,7 +166,9 @@ async def record_snapshot(
root = Path(repo.local_path)
settings = load_event_settings(root)
for error in settings.errors:
- logger.warning("%s (kept the default)", error)
+ if error not in _WARNED:
+ _WARNED.add(error)
+ logger.warning("%s (kept the default)", error)
if not settings.enabled:
await record_gap(session, repository_id, "disabled", now=now)
return []
@@ -165,9 +178,7 @@ async def record_snapshot(
if previous is not None and head and previous.commit_sha == head and previous.stamp == stamp:
return []
- facts = await collect_facts(
- session, repository_id, root, head_commit=head, attribution=attribution_enabled(root)
- )
+ facts = await collect_facts(session, repository_id, root, head_commit=head)
taken_at = now or datetime.now(UTC)
run = SnapshotRun(repository_id=repository_id, taken_at=taken_at, commit_sha=head, stamp=stamp)
session.add(run)
@@ -177,20 +188,119 @@ async def record_snapshot(
for name, f in facts.items()
)
- events: list[EngineEvent] = []
+ pairs: list[tuple[Any, Any]] = []
# A gap marker's stamp never equals a real one, so a run after it is a baseline.
if previous is not None and previous.stamp == stamp:
before = await run_facts(session, previous.id)
- events = await _store_events(
- session, repo, run, previous, before, facts, settings, taken_at
- )
+ kinds = settings.kinds - CONTRACT_KINDS
+ pairs = [(previous, o) for o in diff_snapshots(before, facts) if o[0] in kinds]
+ pairs += await _contract_pairs(session, run, facts, settings.kinds & CONTRACT_KINDS)
+ events = await _store_events(session, repo, run, pairs, facts, taken_at)
await compact(session, repository_id, settings, now=taken_at)
await session.flush()
- if events:
- await _deliver(events, settings, root)
+ _deliver_after_commit(session, events, settings, root)
return events
+async def refresh_contract_facts(session: Any, repository_id: str) -> list[EngineEvent]:
+ """Measure the newest run's contracts once the workspace has extracted them.
+
+ :func:`record_snapshot` runs before the cross-repo pass extracts contracts
+ at the new commit, so that run stored them as unavailable. Called after
+ extraction, this rewrites the same run's contract facts and stores its
+ contract events (:func:`_contract_pairs`). Idempotent: event ids are keyed
+ on the two runs compared. A run whose commit or
+ stamp is no longer current, or contracts still unavailable, is left alone.
+ """
+ from repowise.core.persistence.models import ComponentSnapshot, Repository
+
+ from .snapshot import contract_facts, current_stamp
+
+ repo = await session.get(Repository, repository_id)
+ if repo is None or not repo.local_path:
+ return []
+ root = Path(repo.local_path)
+ settings = load_event_settings(root)
+ if not settings.enabled:
+ return []
+ run = await _newest_run(session, repository_id)
+ if run is None or not run.commit_sha or run.commit_sha != repo.head_commit:
+ return []
+ if run.stamp != current_stamp():
+ return []
+ measured = await contract_facts(session, repository_id, root, head_commit=run.commit_sha)
+ if measured is None:
+ return []
+ # Another update may have moved the repo or stored a newer run meanwhile.
+ head = await session.scalar(
+ select(Repository.head_commit).where(Repository.id == repository_id)
+ )
+ newest = await _newest_run(session, repository_id)
+ if head != run.commit_sha or newest is None or newest.id != run.id:
+ return []
+ after: dict[str, dict[str, Any]] = {}
+ for row in await session.scalars(
+ select(ComponentSnapshot).where(ComponentSnapshot.run_id == run.id)
+ ):
+ facts = json.loads(row.facts_json)
+ definition, contracts = measured.get(row.component, (None, None))
+ # A component re-drawn since the snapshot keeps what it stored.
+ if definition == facts.get("definition") and facts.get("contracts") != contracts:
+ facts["contracts"] = contracts
+ row.facts_json = json.dumps(facts, sort_keys=True)
+ after[row.component] = facts
+ pairs = await _contract_pairs(session, run, after, settings.kinds & CONTRACT_KINDS)
+ events = await _store_events(session, repo, run, pairs, after, _aware(run.taken_at))
+ await session.flush()
+ _deliver_after_commit(session, events, settings, root)
+ return events
+
+
+def _measured(facts: dict[str, Any]) -> bool:
+ contracts = facts.get("contracts")
+ return isinstance(contracts, dict) and contracts.get("basis") == "measured"
+
+
+async def _contract_pairs(
+ session: Any, run: Any, after: dict[str, dict[str, Any]], kinds: frozenset[str]
+) -> list[tuple[Any, Any]]:
+ """``(compared run, occurrence)`` for the contract kinds of *run*.
+
+ Each component with measured contracts is compared with the most recent
+ earlier run of the same stamp that measured them under the same
+ definition. Runs whose contracts were unavailable (an index-only update
+ skips the cross-repo pass) are passed over, never read as every contract
+ removed; a stamp change, a gap marker, or the component missing or
+ re-drawn ends the search.
+ """
+ from repowise.core.persistence.models import SnapshotRun
+
+ pending = {name: f for name, f in after.items() if _measured(f)}
+ if not kinds or not pending:
+ return []
+ earlier = await session.scalars(
+ select(SnapshotRun)
+ .where(SnapshotRun.repository_id == run.repository_id, SnapshotRun.id != run.id)
+ .order_by(SnapshotRun.taken_at.desc(), SnapshotRun.id.desc())
+ .limit(CONTRACT_LOOKBACK_RUNS)
+ )
+ out: list[tuple[Any, Any]] = []
+ for prev in earlier:
+ if prev.stamp != run.stamp:
+ break
+ before = await run_facts(session, prev.id, components=pending)
+ for name in list(pending):
+ facts = before.get(name)
+ if facts is None or facts.get("definition") != pending[name].get("definition"):
+ del pending[name]
+ elif _measured(facts):
+ found = contract_occurrences(name, facts, pending.pop(name))
+ out.extend((prev, o) for o in found if o[0] in kinds)
+ if not pending:
+ break
+ return out
+
+
async def _newest_run(session: Any, repository_id: str) -> Any:
from repowise.core.persistence.models import SnapshotRun
@@ -222,15 +332,19 @@ async def record_gap(
await session.flush()
-async def run_facts(session: Any, run_id: str) -> dict[str, dict[str, Any]]:
- """The component facts one snapshot run stored, by component name."""
+async def run_facts(
+ session: Any, run_id: str, *, components: Iterable[str] | None = None
+) -> dict[str, dict[str, Any]]:
+ """The component facts one snapshot run stored, by component name; only
+ *components* when given."""
from repowise.core.persistence.models import ComponentSnapshot
- rows = await session.execute(
- select(ComponentSnapshot.component, ComponentSnapshot.facts_json).where(
- ComponentSnapshot.run_id == run_id
- )
+ query = select(ComponentSnapshot.component, ComponentSnapshot.facts_json).where(
+ ComponentSnapshot.run_id == run_id
)
+ if components is not None:
+ query = query.where(ComponentSnapshot.component.in_(sorted(components)))
+ rows = await session.execute(query)
return {name: json.loads(raw) for name, raw in rows}
@@ -238,16 +352,14 @@ async def _store_events(
session: Any,
repo: Any,
run: Any,
- previous: Any,
- before: dict[str, dict[str, Any]],
+ pairs: list[tuple[Any, Any]],
after: dict[str, dict[str, Any]],
- settings: EventSettings,
taken_at: datetime,
) -> list[EngineEvent]:
+ """Store ``(compared run, occurrence)`` pairs of *run* as events."""
from repowise.core.persistence.models import Change, EngineEventRecord
- occurrences = [o for o in diff_snapshots(before, after) if o[0] in settings.kinds]
- if not occurrences:
+ if not pairs:
return []
url = _public_url(repo.url)
vcs: dict[str, Any] = {}
@@ -263,7 +375,7 @@ async def _store_events(
vcs["vcs.change.id"] = change_id
events = [
EngineEvent(
- # Keyed on the previous run, not only the commits: a pair seen
+ # Keyed on the compared run, not only the commits: a pair seen
# again (A to B, back to A, to B) or a run with no commit is a new
# occurrence, while a retry of the same run keeps its id.
id=event_id(
@@ -281,34 +393,68 @@ async def _store_events(
source=url,
vcs=vcs,
)
- for kind, component, subject, evidence in occurrences
+ for previous, (kind, component, subject, evidence) in pairs
]
- seen = set(
- await session.scalars(
- select(EngineEventRecord.id).where(EngineEventRecord.id.in_([e.id for e in events]))
- )
- )
- events = [e for e in events if e.id not in seen]
- session.add_all(
- EngineEventRecord(
- id=e.id,
- repository_id=e.repo_id,
- kind=e.kind,
- component=e.component,
- severity=e.severity,
- occurred_at=e.occurred_at,
- commit_sha=e.commit_sha,
- base_commit_sha=e.base_commit_sha,
- stamp=e.stamp,
- payload_json=json.dumps(
+ table = EngineEventRecord.__table__
+ if session.get_bind().dialect.name == "postgresql":
+ from sqlalchemy.dialects.postgresql import insert
+ else:
+ from sqlalchemy.dialects.sqlite import insert
+ # Insert-or-ignore on the id: an event another run or process already
+ # stored is neither stored nor delivered twice.
+ stmt = insert(table).on_conflict_do_nothing(index_elements=[table.c.id]).returning(table.c.id)
+ stored: list[EngineEvent] = []
+ for e in events:
+ row = {
+ "id": e.id,
+ "repository_id": e.repo_id,
+ "kind": e.kind,
+ "component": e.component,
+ "severity": e.severity,
+ "occurred_at": e.occurred_at,
+ "commit_sha": e.commit_sha,
+ "base_commit_sha": e.base_commit_sha,
+ "stamp": e.stamp,
+ "payload_json": json.dumps(
{"evidence": dict(e.evidence), "vcs": dict(e.vcs), "source": e.source},
sort_keys=True,
),
- schema_version=e.schema,
- )
- for e in events
- )
- return events
+ "schema_version": e.schema,
+ }
+ if (await session.execute(stmt.values(**row))).scalar() is not None:
+ stored.append(e)
+ return stored
+
+
+def _deliver_after_commit(
+ session: Any, events: Sequence[EngineEvent], settings: EventSettings, root: Path
+) -> None:
+ """Hand *events* to the sinks once the session's transaction commits.
+
+ A rollback drops them; the retry stores and delivers the same ids. The
+ commit hook runs in the async session's greenlet, so the sinks are
+ awaited there.
+ """
+ from sqlalchemy import event
+
+ if not events:
+ return
+ sync = session.sync_session
+ sync.info.setdefault(_PENDING, []).append((list(events), settings, root))
+ if not sync.info.get(_LISTENING):
+ sync.info[_LISTENING] = True
+ event.listen(sync, "after_commit", _on_commit)
+ event.listen(sync, "after_rollback", lambda s: s.info.pop(_PENDING, None))
+
+
+def _on_commit(sync_session: Any) -> None:
+ from sqlalchemy.util import await_only
+
+ for events, settings, root in sync_session.info.pop(_PENDING, []):
+ try:
+ await_only(_deliver(events, settings, root))
+ except Exception as exc: # the store keeps them; `repowise events` replays
+ logger.warning("events not delivered: %s", exc)
async def _deliver(events: Sequence[EngineEvent], settings: EventSettings, root: Path) -> None:
diff --git a/packages/core/src/repowise/core/report/sections/trend.py b/packages/core/src/repowise/core/report/sections/trend.py
index bef0ea42f..ef9571f42 100644
--- a/packages/core/src/repowise/core/report/sections/trend.py
+++ b/packages/core/src/repowise/core/report/sections/trend.py
@@ -3,7 +3,7 @@ snapshot and the newest one at least 90 days older under the same stamp.
Reuses the event diff rules, so a move counts here exactly when it would
count as an event. Snapshots under different analyzer or parser stamps are
-never compared. Agent-share moves are left out: the report shows no share.
+never compared.
"""
from __future__ import annotations
@@ -17,7 +17,6 @@ from sqlalchemy import select
from repowise.core.report.model import ReportContext, Section, capped, check, report_sections
QUARTER_DAYS = 90
-_LEFT_OUT = frozenset({"attribution_jump"})
_METHOD = (
"the event diff rules, for the event kinds the repository has on, applied to two "
f"stored component snapshots with the same analyzer and parser stamp, at least "
@@ -64,7 +63,7 @@ async def collect(ctx: ReportContext) -> list[dict[str, Any]]:
after = await run_facts(ctx.session, newest.id)
# The kinds this repository's events config turns on, so a move counts here
# exactly when it would fire as an event (new_hotspot is off by default).
- kinds = load_event_settings(ctx.repo_root).kinds - _LEFT_OUT
+ kinds = load_event_settings(ctx.repo_root).kinds
moved: dict[str, list[str]] = defaultdict(list)
for kind, component, subject, _evidence in diff_snapshots(before, after):
if kind in kinds:
diff --git a/packages/core/src/repowise/core/workspace/update.py b/packages/core/src/repowise/core/workspace/update.py
index 994939158..705c13751 100644
--- a/packages/core/src/repowise/core/workspace/update.py
+++ b/packages/core/src/repowise/core/workspace/update.py
@@ -1316,6 +1316,34 @@ async def run_cross_repo_hooks(
timings.on_phase_done("workspace_store")
_log_phase(timings, "workspace_store")
+ # Each updated repo snapshotted its components before contracts were
+ # extracted at its new commit; measure them now.
+ for alias in changed_repos:
+ entry = ws_config.get_repo(alias)
+ if entry is not None:
+ await _refresh_contract_facts((workspace_root / entry.path).resolve())
+
+
+async def _refresh_contract_facts(repo_path: Path) -> None:
+ """Best-effort: a failure leaves the run's contracts unavailable."""
+ from ..events.store import refresh_contract_facts
+ from ..persistence import create_engine, create_session_factory, get_session
+ from ..persistence.crud import get_repository_by_path
+ from ..persistence.database import has_db_store, resolve_db_url
+
+ if not has_db_store(repo_path):
+ return
+ engine = create_engine(resolve_db_url(repo_path))
+ try:
+ async with get_session(create_session_factory(engine)) as session:
+ repo = await get_repository_by_path(session, str(repo_path))
+ if repo is not None:
+ await refresh_contract_facts(session, repo.id)
+ except Exception:
+ _log.warning("Contract facts not refreshed for %s", repo_path, exc_info=True)
+ finally:
+ await engine.dispose()
+
async def _run_phases(
ws_config: WorkspaceConfig,
diff --git a/packages/web/messages/en.json b/packages/web/messages/en.json
index 88fb3c02a..18b7caad2 100644
--- a/packages/web/messages/en.json
+++ b/packages/web/messages/en.json
@@ -578,7 +578,7 @@
},
"events": {
"title": "Events",
- "description": "What moved between two index runs in a row: owners, bus factor, hotspots, contracts, decisions, dead code, test reach and attribution, per component.",
+ "description": "What moved between two index runs in a row: owners, bus factor, hotspots, contracts, decisions, dead code and test reach, per component.",
"repository": "Whole repository",
"count": "{count, plural, one {# event} other {# events}}, newest first.",
"emptyTitle": "No events yet",
@@ -596,8 +596,7 @@
"contract_broken": "Contract with consumers disappeared",
"decision_stale": "Governing decision went stale",
"dead_code_growth": "Dead-code findings grew",
- "test_reach_drop": "Test reach fell",
- "attribution_jump": "Agent share moved sharply"
+ "test_reach_drop": "Test reach fell"
},
"severities": {
"critical": "critical",
diff --git a/packages/web/messages/zh-CN.json b/packages/web/messages/zh-CN.json
index 3ab32e37e..f26814f85 100644
--- a/packages/web/messages/zh-CN.json
+++ b/packages/web/messages/zh-CN.json
@@ -578,7 +578,7 @@
},
"events": {
"title": "事件",
- "description": "相邻两次索引之间发生的变化:按组件列出负责人、巴士系数、热点、契约、决策、死代码、测试覆盖与来源归属。",
+ "description": "相邻两次索引之间发生的变化:按组件列出负责人、巴士系数、热点、契约、决策、死代码与测试覆盖。",
"repository": "整个仓库",
"count": "共 {count} 个事件,按时间从新到旧。",
"emptyTitle": "暂无事件",
@@ -596,8 +596,7 @@
"contract_broken": "有使用方的契约消失",
"decision_stale": "治理决策已过时",
"dead_code_growth": "死代码发现增多",
- "test_reach_drop": "测试覆盖下降",
- "attribution_jump": "智能体占比大幅变化"
+ "test_reach_drop": "测试覆盖下降"
},
"severities": {
"critical": "严重",
diff --git a/tests/unit/core/test_events.py b/tests/unit/core/test_events.py
index 7a531fd20..69d08d18a 100644
--- a/tests/unit/core/test_events.py
+++ b/tests/unit/core/test_events.py
@@ -38,7 +38,6 @@ def _facts(**over):
},
"decisions": {"governing": 1, "stale": []},
"contracts": {"basis": "measured", "provided": {"http::GET::/a": 2}},
- "attribution": {"commits": 20, "agent_share": 0.1, "coverage": 0.5, "window_days": 90},
}
for key, value in over.items():
if isinstance(value, dict) and isinstance(base.get(key), dict):
@@ -91,10 +90,7 @@ def test_owner_digests_under_another_key_are_not_compared():
assert _kinds(_facts(declared_owner_key="x1"), renamed) == []
-def test_a_capped_recent_window_does_not_drive_activity_or_attribution():
- reg = {"tags": ["pci"]}
- capped = {"agent_share": 0.5, "recent_capped": True}
- assert _kinds(_facts(**reg), _facts(**reg, attribution=capped)) == []
+def test_a_capped_recent_window_does_not_drive_activity():
gone = {"active_main_owners": 0, "recent_capped": True}
assert _kinds(_facts(), _facts(ownership=gone)) == []
@@ -164,17 +160,6 @@ def test_test_reach_drop_by_share_or_measured_coverage():
assert found[0][3] == {"line_coverage": {"basis": "measured", "previous_pct": 80.0, "pct": 70.0}}
-def test_attribution_jump_only_on_regulated_components_with_coverage():
- up = {"agent_share": 0.5}
- assert _kinds(_facts(), _facts(attribution=up)) == []
- reg = {"tags": ["pci"]}
- assert _kinds(_facts(**reg), _facts(**reg, attribution=up)) == [("attribution_jump", "")]
- withheld = {"agent_share": None}
- assert _kinds(_facts(**reg), _facts(**reg, attribution=withheld)) == []
- few = {"commits": 3, "agent_share": 0.9}
- assert _kinds(_facts(**reg), _facts(**reg, attribution=few)) == []
-
-
# ---------------------------------------------------------------------------
# Wire form and settings
# ---------------------------------------------------------------------------
@@ -279,6 +264,17 @@ def test_settings_name_every_wrong_value(tmp_path):
]
+def test_a_config_naming_a_kind_that_no_longer_exists_warns_and_keeps_defaults(tmp_path):
+ assert "attribution_jump" not in KINDS
+ (tmp_path / ".repowise").mkdir()
+ (tmp_path / ".repowise" / "config.yaml").write_text(
+ "events:\n kinds: {attribution_jump: true, new_hotspot: true}\n", encoding="utf-8"
+ )
+ s = load_event_settings(tmp_path)
+ assert [e.split(":")[0] for e in s.errors] == ["events.kinds.attribution_jump"]
+ assert s.kinds == EventSettings().kinds | {"new_hotspot"}
+
+
def test_doctor_reports_a_wrong_events_block(tmp_path):
from repowise.cli.commands.doctor_cmd.repo_checks import _events_config_check
@@ -557,3 +553,225 @@ async def test_a_failing_sink_leaves_the_event_stored(repo_db, monkeypatch):
events = await _run(factory, repo_id, head="b" * 40, facts=after, monkeypatch=monkeypatch)
async with get_session(factory) as session:
assert [e.id for e in await list_events(session, repo_id)] == [events[0].id]
+
+
+async def test_a_stored_event_of_a_kind_no_longer_known_still_reads(repo_db):
+ from repowise.core.events.store import list_events
+ from repowise.core.persistence.database import get_session
+ from repowise.core.persistence.models import EngineEventRecord
+
+ factory, repo_id, _root = repo_db
+ async with get_session(factory) as session:
+ session.add(
+ EngineEventRecord(
+ id="x" * 32,
+ repository_id=repo_id,
+ kind="retired_kind",
+ component="c",
+ severity="info",
+ occurred_at=datetime(2026, 1, 1, tzinfo=UTC),
+ stamp="s1",
+ payload_json=json.dumps({"evidence": {"basis": "measured"}}),
+ schema_version=1,
+ )
+ )
+ async with get_session(factory) as session:
+ (event,) = await list_events(session, repo_id)
+ assert to_cloudevent(event)["type"] == "dev.repowise.retired_kind.0.1.0"
+
+
+# ---------------------------------------------------------------------------
+# Contract facts measured after the workspace extracts contracts
+# ---------------------------------------------------------------------------
+
+
+async def _refresh(factory, repo_id, monkeypatch, measured, stamp="s1"):
+ from repowise.core.events.store import refresh_contract_facts
+ from repowise.core.persistence.database import get_session
+
+ async def _contracts(*_a, **_k):
+ return measured
+
+ monkeypatch.setattr(snapshot_mod, "contract_facts", _contracts)
+ monkeypatch.setattr(snapshot_mod, "current_stamp", lambda: stamp)
+ async with get_session(factory) as session:
+ return await refresh_contract_facts(session, repo_id)
+
+
+async def _stored_contracts(factory, repo_id):
+ from sqlalchemy import select
+
+ from repowise.core.events.store import run_facts
+ from repowise.core.persistence.database import get_session
+ from repowise.core.persistence.models import SnapshotRun
+
+ async with get_session(factory) as session:
+ runs = await session.scalars(
+ select(SnapshotRun.id).where(SnapshotRun.repository_id == repo_id)
+ .order_by(SnapshotRun.taken_at, SnapshotRun.id)
+ )
+ return [(await run_facts(session, r))["c"]["contracts"] for r in runs]
+
+
+async def test_contracts_extracted_after_the_snapshot_fill_the_same_run(repo_db, monkeypatch):
+ from repowise.core.events.store import list_events
+ from repowise.core.persistence.database import get_session
+
+ factory, repo_id, _root = repo_db
+ unavailable = {"basis": "unavailable"}
+ t0 = datetime(2026, 5, 1, tzinfo=UTC)
+ await _run(factory, repo_id, head="a" * 40, facts={"c": _facts()}, monkeypatch=monkeypatch, now=t0)
+ # The update's snapshot runs before extraction reaches the new commit.
+ later = {"c": {**_facts(), "contracts": unavailable}}
+ t1 = t0 + timedelta(hours=1)
+ assert await _run(factory, repo_id, head="b" * 40, facts=later, monkeypatch=monkeypatch, now=t1) == []
+
+ # Still unavailable after the pass (no workspace): nothing is written.
+ assert await _refresh(factory, repo_id, monkeypatch, None) == []
+ assert (await _stored_contracts(factory, repo_id))[1] == unavailable
+
+ now = {"basis": "measured", "provided": {"http::GET::/b": 0}}
+ events = await _refresh(factory, repo_id, monkeypatch, {"c": ("d1", now)})
+ assert sorted((e.kind, e.evidence["contract_id"]) for e in events) == [
+ ("contract_added", "http::GET::/b"),
+ ("contract_broken", "http::GET::/a"),
+ ]
+ assert {e.commit_sha for e in events} == {"b" * 40}
+ assert (await _stored_contracts(factory, repo_id))[1] == now
+ # Idempotent: a second pass over the same run stores nothing new.
+ assert await _refresh(factory, repo_id, monkeypatch, {"c": ("d1", now)}) == []
+ async with get_session(factory) as session:
+ assert len(await list_events(session, repo_id)) == 2
+
+
+async def test_contract_refresh_leaves_a_redrawn_component_or_an_old_run_alone(
+ repo_db, monkeypatch
+):
+ factory, repo_id, _root = repo_db
+ unavailable = {"basis": "unavailable"}
+ await _run(factory, repo_id, head="a" * 40, facts={"c": {**_facts(), "contracts": unavailable}}, monkeypatch=monkeypatch)
+ measured = {"basis": "measured", "provided": {}}
+ assert await _refresh(factory, repo_id, monkeypatch, {"c": ("d2", measured)}) == []
+ assert await _stored_contracts(factory, repo_id) == [unavailable]
+ # Under another stamp the run is not this analyzer's to fill.
+ assert await _refresh(factory, repo_id, monkeypatch, {"c": ("d1", measured)}, stamp="s2") == []
+ assert await _stored_contracts(factory, repo_id) == [unavailable]
+
+
+async def test_contract_facts_are_unavailable_outside_a_workspace(tmp_path):
+ assert await snapshot_mod.contract_facts(None, "r", tmp_path, head_commit="a" * 40) is None
+
+
+async def test_contract_kinds_skip_runs_whose_contracts_were_unavailable(repo_db, monkeypatch):
+ factory, repo_id, _root = repo_db
+ t0 = datetime(2026, 5, 1, tzinfo=UTC)
+ gone = {"basis": "measured", "provided": {}}
+ unavailable = {"basis": "unavailable"}
+
+ async def run(head, contracts, hours, **over):
+ facts = {"c": {**_facts(**over), "contracts": contracts}}
+ now = t0 + timedelta(hours=hours)
+ return await _run(factory, repo_id, head=head * 40, facts=facts, monkeypatch=monkeypatch, now=now)
+
+ await run("a", _facts()["contracts"], 0)
+ # An index-only run in between: never read as every contract removed.
+ assert await run("b", unavailable, 1) == []
+ events = await run("c", gone, 2)
+ assert [(e.kind, e.base_commit_sha) for e in events] == [("contract_broken", "a" * 40)]
+ # A component re-drawn in between ends the search.
+ await run("d", unavailable, 3, definition="d2")
+ assert await run("e", _facts()["contracts"], 4) == []
+
+
+async def test_contract_refresh_compares_with_the_last_measured_run(repo_db, monkeypatch):
+ from repowise.core.events.store import list_events
+ from repowise.core.persistence.database import get_session
+
+ factory, repo_id, _root = repo_db
+ t0 = datetime(2026, 5, 1, tzinfo=UTC)
+ unavailable = {"c": {**_facts(), "contracts": {"basis": "unavailable"}}}
+ await _run(factory, repo_id, head="a" * 40, facts={"c": _facts()}, monkeypatch=monkeypatch, now=t0)
+ for i, head in enumerate("bc", start=1):
+ await _run(factory, repo_id, head=head * 40, facts=unavailable, monkeypatch=monkeypatch,
+ now=t0 + timedelta(hours=i))
+ now = {"basis": "measured", "provided": {"http::GET::/a": 2, "http::GET::/b": 0}}
+ events = await _refresh(factory, repo_id, monkeypatch, {"c": ("d1", now)})
+ assert [(e.kind, e.base_commit_sha, e.commit_sha) for e in events] == [
+ ("contract_added", "a" * 40, "c" * 40)
+ ]
+ assert await _refresh(factory, repo_id, monkeypatch, {"c": ("d1", now)}) == []
+ async with get_session(factory) as session:
+ assert len(await list_events(session, repo_id)) == 1
+
+
+async def test_events_reach_the_sinks_only_after_the_commit(repo_db, monkeypatch):
+ from repowise.core.events.store import list_events, record_snapshot
+ from repowise.core.persistence.database import get_session
+ from repowise.core.persistence.models import Repository
+
+ factory, repo_id, root = repo_db
+ sink = root / ".repowise" / "events.jsonl"
+ await _run(factory, repo_id, head="a" * 40, facts={"c": _facts()}, monkeypatch=monkeypatch)
+ hot = {"c": _facts(hotspots=["a.py", "b.py"])}
+
+ async def _collect(*_a, **_k):
+ return hot
+
+ monkeypatch.setattr(snapshot_mod, "collect_facts", _collect)
+ with pytest.raises(RuntimeError):
+ async with get_session(factory) as session:
+ (await session.get(Repository, repo_id)).head_commit = "b" * 40
+ assert [e.kind for e in await record_snapshot(session, repo_id)] == ["new_hotspot"]
+ assert not sink.exists()
+ raise RuntimeError("the caller's commit fails")
+ assert not sink.exists()
+ async with get_session(factory) as session:
+ assert await list_events(session, repo_id) == []
+ # The retry stores and delivers the same event once.
+ (event,) = await _run(factory, repo_id, head="b" * 40, facts=hot, monkeypatch=monkeypatch)
+ assert [json.loads(line)["id"] for line in sink.read_text(encoding="utf-8").splitlines()] == [
+ event.id
+ ]
+
+
+async def test_contract_refresh_skips_a_run_superseded_during_extraction(repo_db, monkeypatch):
+ from sqlalchemy import update
+
+ from repowise.core.events.store import refresh_contract_facts
+ from repowise.core.persistence.database import get_session
+ from repowise.core.persistence.models import Repository
+
+ factory, repo_id, _root = repo_db
+ unavailable = {"basis": "unavailable"}
+ await _run(factory, repo_id, head="a" * 40, facts={"c": _facts()}, monkeypatch=monkeypatch)
+ await _run(factory, repo_id, head="b" * 40, facts={"c": {**_facts(), "contracts": unavailable}},
+ monkeypatch=monkeypatch)
+
+ async def _moved(session, *_a, **_k):
+ await session.execute(update(Repository).values(head_commit="c" * 40))
+ return {"c": ("d1", {"basis": "measured", "provided": {}})}
+
+ monkeypatch.setattr(snapshot_mod, "contract_facts", _moved)
+ async with get_session(factory) as session:
+ assert await refresh_contract_facts(session, repo_id) == []
+ assert (await _stored_contracts(factory, repo_id))[1] == unavailable
+
+
+async def test_a_config_error_is_logged_once_per_process(repo_db, monkeypatch, caplog):
+ factory, repo_id, root = repo_db
+ (root / ".repowise" / "config.yaml").write_text(
+ "events:\n kinds: {once_only_retired_kind: true}\n", encoding="utf-8"
+ )
+ for head in "ab":
+ await _run(factory, repo_id, head=head * 40, facts={"c": _facts()}, monkeypatch=monkeypatch)
+ named = [r for r in caplog.records if "once_only_retired_kind" in r.getMessage()]
+ assert len(named) == 1
diff --git a/tests/unit/workspace/test_update.py b/tests/unit/workspace/test_update.py
index 0b5a63529..e46bbd725 100644
--- a/tests/unit/workspace/test_update.py
+++ b/tests/unit/workspace/test_update.py
@@ -714,6 +714,30 @@ class TestCrossRepoHooks:
import asyncio
asyncio.run(run_cross_repo_hooks(ws_config, tmp_path, ["backend"]))
+ def test_measures_contract_facts_of_the_changed_repos_last(self, tmp_path: Path) -> None:
+ """The snapshot each update took ran before extraction, so the hooks
+ end by filling its contract facts, for the repos that changed only."""
+ ws_config = WorkspaceConfig(
+ repos=[RepoEntry(path="api", alias="api"), RepoEntry(path="web", alias="web")]
+ )
+ order: list[str] = []
+
+ async def _phases(*_a, **_k):
+ order.append("phases")
+
+ async def _refresh(path):
+ order.append(path.name)
+
+ import asyncio
+
+ with (
+ patch("repowise.core.workspace.update._run_phases", _phases),
+ patch("repowise.core.workspace.store.refresh_workspace_store", AsyncMock()),
+ patch("repowise.core.workspace.update._refresh_contract_facts", _refresh),
+ ):
+ asyncio.run(run_cross_repo_hooks(ws_config, tmp_path, ["api"]))
+ assert order == ["phases", "api"]
+
# ---------------------------------------------------------------------------
# RepoUpdateResult