feat(8246): the code index links each test to what it guards, and can say redundant or unused
CI & Build / Plugin hooks (push) Successful in 20s
CI & Build / Python lint (push) Successful in 3s
CI & Build / TypeScript typecheck (push) Successful in 41s
CI & Build / Python tests (push) Failing after 2m8s
CI & Build / integration (push) Successful in 2m24s
CI & Build / Build & push image (push) Skipped

Every coverage refresh now reads, for each test in a bound repo, which product
shapes its body exercises: through module aliases and imports, following test
helpers, with mocked names left out and shapes most test files reach dropped as
hubs. Tests in different files that guard exactly the same set form a group.
The project page lists the groups and a person can mark one test redundant,
naming the test that covers it, or keep them all with a reason. Agents get
test_guard_groups and review_test_group, and classify_shapes takes the two new
removal verdicts, redundant (with covered_by) and unused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 21:17:48 -05:00
co-authored by Claude Opus 5.5
parent 134dd68274
commit 865465ce8f
19 changed files with 1412 additions and 27 deletions
+68 -4
View File
@@ -29,7 +29,9 @@ from grimoire.models.lesson_rule_link import LessonNoRule, LessonRuleLink
from grimoire.models.lesson_situation import LessonSituation
from grimoire.models.rule_field_judgment import RuleFieldJudgment
from grimoire.models.rule_moment_judgment import RuleMomentJudgment
from grimoire.models.code_shape import CodeShape, CodeShapeEvent, CodeShapeUse
from grimoire.models.code_shape import (
CodeShape, CodeShapeEvent, CodeShapeGuardReview, CodeShapeUse,
)
from grimoire.models.project import Project
from grimoire.models.repo_binding import RepoBinding
from grimoire.models.rulebook import (
@@ -124,7 +126,11 @@ logger = logging.getLogger(__name__)
# v31 lesson_situations: the moments and triggers each lesson arrives at,
# with the proposals waiting and the rejections that stop a value being
# proposed again. Nothing in the lesson's text records them.
BACKUP_VERSION = 31
# v32 code_shape_guard_reviews, groups of same-guard tests judged to be
# distinct behaviours, and code_shapes.covered_by_id, the shape a
# `redundant` verdict names. Lose them and every reviewed group is put
# back in front of a reviewer, and every redundant verdict loses its why.
BACKUP_VERSION = 32
# Every table this backup carries, by its REAL name. Paired with _NOT_INCLUDED
# below, these two lists must together account for the entire schema — which is
@@ -141,6 +147,8 @@ _BACKED_UP = [
"note_usage_events", "repo_bindings", "note_supersessions",
# The shape ledger, its history and its consumption edges.
"code_shapes", "code_shape_events", "code_shape_uses",
# Groups of tests that guard the same thing, reviewed and kept.
"code_shape_guard_reviews",
# The global area catalog. Global, not
# user-scoped, so it rides in EVERY export — including a single-user
# one, whose Systems would otherwise restore unmapped.
@@ -223,6 +231,9 @@ _NOT_INCLUDED = [
# rebuilt from the repo archive by every coverage sync, and carries no
# judgment — the first refresh after a restore recreates it exactly.
"code_shape_consumers",
# Derived the same way: which shapes each test exercises, read from the
# repo archive by every coverage sync.
"code_shape_guards",
# Not a section: the full backup carries the one value in its header as
# `instance_id`, and a restore decides from it rather than copying a row.
"instance_identity",
@@ -321,9 +332,12 @@ _COLUMN_EXCLUSIONS: dict[str, set[str]] = {
# derived data is regenerated, never restored.
"proposed_snippet_id", "proposal_basis", "proposal_score",
"proposal_group", "proposed_at", "proposed_sha",
# A test's weak-test signals: recomputed by every sync from the body.
"test_signals",
},
"code_shape_events": set(),
"code_shape_uses": set(),
"code_shape_guard_reviews": {"id"},
# Matched on SLUG at restore, like canonical_systems: a target install
# already seeded the standard platforms from its migrations.
"platforms": {"deleted_at", "deleted_batch_id", "id", "created_at", "updated_at"},
@@ -419,9 +433,15 @@ _IMPORT_COLUMN_EXCLUSIONS: dict[str, set[str]] = {
# next refresh recomputes it against the restored snippet ids.
"proposed_snippet_id", "proposal_basis", "proposal_score",
"proposal_group", "proposed_at", "proposed_sha",
# Not exported: recomputed by the next sync.
"test_signals",
# Reason 3: it points at another shape of this restore, possibly one
# placed after this row, so it is patched once every shape has an id.
"covered_by_id",
},
"code_shape_events": {"id"},
"code_shape_uses": {"id"},
"code_shape_guard_reviews": {"id"},
"platforms": {"id", "deleted_at", "deleted_batch_id", "created_at", "updated_at"},
# `platform_id` IS set, from the exported slug.
"project_platforms": set(),
@@ -619,6 +639,10 @@ def _code_shape_use_rows(rows) -> list[dict]:
return [r.to_dict() for r in rows]
def _code_shape_guard_review_rows(rows) -> list[dict]:
return [r.to_dict() for r in rows]
def _repo_binding_rows(rows) -> list[dict]:
return [
# `ref` is the branch this binding's ledger follows. Without it a
@@ -1135,6 +1159,9 @@ async def export_full_backup() -> dict:
code_shape_events = (await session.execute(
select(CodeShapeEvent).order_by(CodeShapeEvent.at, CodeShapeEvent.id)
)).scalars().all()
code_shape_guard_reviews = (await session.execute(
select(CodeShapeGuardReview).order_by(CodeShapeGuardReview.id)
)).scalars().all()
code_shape_uses = (await session.execute(
select(CodeShapeUse).order_by(CodeShapeUse.shape_id, CodeShapeUse.snippet_id, CodeShapeUse.id)
)).scalars().all()
@@ -1213,6 +1240,7 @@ async def export_full_backup() -> dict:
"code_shapes": _code_shape_rows(code_shapes),
"code_shape_events": _code_shape_event_rows(code_shape_events),
"code_shape_uses": _code_shape_use_rows(code_shape_uses),
"code_shape_guard_reviews": _code_shape_guard_review_rows(code_shape_guard_reviews),
**_family_sections(
platforms, project_platforms, family_ideas, family_idea_platforms,
family_idea_references, family_adoptions, family_decisions,
@@ -1317,6 +1345,10 @@ async def export_user_backup(user_id: int) -> dict:
select(CodeShapeEvent).where(CodeShapeEvent.project_id.in_(project_ids))
.order_by(CodeShapeEvent.at, CodeShapeEvent.id)
)).scalars().all() if project_ids else []
code_shape_guard_reviews = (await session.execute(
select(CodeShapeGuardReview).where(CodeShapeGuardReview.project_id.in_(project_ids))
.order_by(CodeShapeGuardReview.id)
)).scalars().all() if project_ids else []
code_shape_uses = (await session.execute(
select(CodeShapeUse).join(CodeShape, CodeShape.id == CodeShapeUse.shape_id)
.where(CodeShape.project_id.in_(project_ids))
@@ -1516,6 +1548,7 @@ async def export_user_backup(user_id: int) -> dict:
"code_shapes": _code_shape_rows(code_shapes),
"code_shape_events": _code_shape_event_rows(code_shape_events),
"code_shape_uses": _code_shape_use_rows(code_shape_uses),
"code_shape_guard_reviews": _code_shape_guard_review_rows(code_shape_guard_reviews),
**_family_sections(
platforms, project_platforms, family_ideas, family_idea_platforms,
family_idea_references, family_adoptions, family_decisions,
@@ -2346,6 +2379,22 @@ def _build_code_shape_event(row: dict, maps: _Maps) -> CodeShapeEvent | None:
)
def _build_code_shape_guard_review(row: dict, maps: _Maps) -> CodeShapeGuardReview | None:
"""A reviewed group rides its project. Its key names the member tests by
path and symbol, not by id, so it needs no re-mapping."""
pid = maps.projects.get(row.get("project_id", 0))
if pid is None:
return None
return CodeShapeGuardReview(
project_id=pid,
group_key=row.get("group_key", ""),
verdict=row.get("verdict", "distinct"),
reason=row.get("reason", ""),
reviewed_by=row.get("reviewed_by", ""),
reviewed_at=_dt(row.get("reviewed_at")),
)
def _build_code_shape_use(row: dict, maps: _Maps) -> CodeShapeUse | None:
"""Consumption edges ride their shape AND their snippet — both ends
must have survived, or the edge is no longer a fact."""
@@ -2552,7 +2601,7 @@ async def _load_v2(session, data: dict, maps: "_Maps", *, keep_ids: bool) -> dic
"system_usage_events": 0,
"repo_bindings": 0,
"note_supersessions": 0, "code_shapes": 0, "code_shape_events": 0,
"code_shape_uses": 0, "canonical_systems": 0,
"code_shape_uses": 0, "code_shape_guard_reviews": 0, "canonical_systems": 0,
"rule_systems": 0, "rule_moments": 0, "rule_moment_judgments": 0,
"rule_triggers": 0, "rule_field_judgments": 0,
"rule_relations": 0, "rule_versions": 0,
@@ -2984,6 +3033,21 @@ async def _load_v2(session, data: dict, maps: "_Maps", *, keep_ids: bool) -> dic
session.add(edge)
stats["code_shape_uses"] += 1
# v32: a redundant verdict's cover, once every shape has its new id.
for cs_data in data.get("code_shapes", []):
cover = maps.shapes.get(cs_data.get("covered_by_id") or 0)
placed = maps.shapes.get(int(cs_data["id"])) if cs_data.get("id") else None
if cover and placed:
shape = await session.get(CodeShape, placed)
if shape is not None:
shape.covered_by_id = cover
for rv in data.get("code_shape_guard_reviews", []):
review = _build_code_shape_guard_review(rv, maps)
if review is None:
continue
session.add(review)
stats["code_shape_guard_reviews"] += 1
# v10: a project's design-system pointer and its inception record ride
# the project but point at design systems and rulebooks restored AFTER
# it — so they are written last, with ids re-mapped. An id that did
@@ -3044,7 +3108,7 @@ _CATALOG_TABLES = ("canonical_systems", "platforms")
# longer holds, so a rollback clears them and the backfills rebuild them.
_DERIVED_TABLES = (
"note_embeddings", "rule_embeddings", "milestone_embeddings",
"system_embeddings", "code_shape_consumers",
"system_embeddings", "code_shape_consumers", "code_shape_guards",
)