CI & Build / TypeScript typecheck (push) Failing after 2s
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 8s
CI & Build / integration (push) Successful in 28s
CI & Build / Python tests (push) Failing after 37s
CI & Build / Build & push image (push) Skipped
Every judgment now goes through one helper that remembers the fingerprint judged (classified_sha) and writes a code_shape_events row; the sync writes vanished / reappeared / drifted events and flags recheck_at when a body moves under an instance/variant. The refresh flags diverges_from on shapes new since the previous computation that sit where one canon dominates the judged siblings of their directory+kind and were not proposed as that canon (a first seed flags nothing); the write-path hint asks the same question in-band for the shapes the hook names. list_shapes(flag=divergence|recheck), shape_history(project_id, path, symbol) (read-only), coverage line/payload/ card carry divergent + recheck. Backup v8 carries the history. Plugin 0.1.36. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
345 lines
16 KiB
Python
345 lines
16 KiB
Python
"""The shape ledger's schema contract (#2787, milestone 294, note 2786).
|
|
|
|
Step 1 pins the model: identity, the classification vocabulary, and the
|
|
token-free serialisation. The sync pass (step 2) and the classification
|
|
surface (step 3) grow their tests here; DB-backed behavior lands in the
|
|
integration lane once there is behavior to exercise.
|
|
"""
|
|
import pytest
|
|
|
|
from scribe.models import Base
|
|
from scribe.models.code_shape import SHAPE_CLASSIFIERS, SHAPE_STATUSES, CodeShape
|
|
|
|
|
|
def test_identity_is_project_repo_path_symbol_kind():
|
|
"""Kind is part of identity on purpose: one file can define `.foo` (css)
|
|
and `foo` (sym) as distinct shapes — the extractor emits both."""
|
|
table = Base.metadata.tables["code_shapes"]
|
|
unique = next(
|
|
c for c in table.constraints
|
|
if getattr(c, "name", "") == "uq_code_shapes_identity"
|
|
)
|
|
assert [c.name for c in unique.columns] == [
|
|
"project_id", "repo_key", "path", "symbol", "kind",
|
|
]
|
|
|
|
|
|
def test_the_todo_state_is_the_default():
|
|
"""A shape nobody has judged yet must read `unclassified` — the ledger's
|
|
todo list — never silently look classified."""
|
|
assert CodeShape.__table__.c.status.default.arg == "unclassified"
|
|
assert "unclassified" in SHAPE_STATUSES
|
|
assert set(SHAPE_STATUSES) == {
|
|
"canonical", "instance", "variant", "exempt", "unclassified",
|
|
}
|
|
assert set(SHAPE_CLASSIFIERS) == {
|
|
"agent", "audit", "hook", "mechanical", "import",
|
|
}
|
|
|
|
|
|
def test_snippet_reference_survives_snippet_deletion_as_null():
|
|
"""SET NULL, not CASCADE: a deleted snippet must not silently erase the
|
|
accounting rows that pointed at it — the sync pass re-files them as
|
|
unclassified so they rejoin the todo."""
|
|
fk = next(iter(CodeShape.__table__.c.snippet_id.foreign_keys))
|
|
assert fk.ondelete == "SET NULL"
|
|
assert fk.column.table.name == "notes"
|
|
|
|
|
|
def test_status_queries_have_an_index():
|
|
"""list_shapes(status=unclassified) is THE todo query (step 3) — it must
|
|
not degrade into a project-wide scan as ledgers reach thousands of rows."""
|
|
names = {ix.name for ix in CodeShape.__table__.indexes}
|
|
assert "ix_code_shapes_project_status" in names
|
|
assert "ix_code_shapes_snippet" in names
|
|
|
|
|
|
# --- step 3: the classification batch validator (pure, checked before ACL) ---
|
|
|
|
|
|
def test_batch_validation_names_the_failing_item():
|
|
from scribe.services.shape_ledger import validate_classifications as v
|
|
|
|
ok = {"path": "src/a.py", "symbol": "f", "status": "exempt", "reason": "one-off"}
|
|
assert v([ok]) is None
|
|
assert "empty" in v([])
|
|
assert "classifications[1]" in v([ok, {"symbol": "f", "status": "exempt"}])
|
|
assert "unknown status" in v([{**ok, "status": "covered"}])
|
|
# A judgment that references canon must name the canon...
|
|
assert "needs snippet_id" in v(
|
|
[{"path": "a", "symbol": "f", "status": "instance"}]
|
|
)
|
|
# ...and a departure/exemption must carry its why — the why IS the record.
|
|
assert "needs a reason" in v(
|
|
[{"path": "a", "symbol": "f", "status": "variant", "snippet_id": 3}]
|
|
)
|
|
assert "needs a reason" in v([{"path": "a", "symbol": "f", "status": "exempt"}])
|
|
# Withdrawing a judgment needs neither target nor reason.
|
|
assert v([{"path": "a", "symbol": "f", "status": "unclassified"}]) is None
|
|
|
|
|
|
def test_classify_and_list_are_mounted_as_mcp_tools():
|
|
from scribe.mcp.server import build_mcp_server
|
|
|
|
mcp = build_mcp_server()
|
|
for name in ("classify_shapes", "list_shapes", "refresh_pattern_coverage"):
|
|
assert mcp._tool_manager.get_tool(name) is not None
|
|
|
|
|
|
# --- step 5: the write-path feed's evidence tests (pure) ---------------------
|
|
|
|
|
|
def test_symbol_reference_is_word_bounded_and_kind_aware():
|
|
from scribe.services.shape_ledger import references_symbol as ref
|
|
|
|
code = "const ok = await confirmed({ title: 'x' });\nif (!ok) return;"
|
|
assert ref(code, "confirmed", "sym")
|
|
assert not ref(code, "confirm", "sym") # prefix never claims the call
|
|
assert not ref("", "confirmed", "sym")
|
|
assert not ref(code, "", "sym")
|
|
# CSS: the class as a selector or inside a class attribute; dashes are part
|
|
# of the name, so `btn` must not claim `btn-primary`.
|
|
html = '<button class="btn btn-primary">Go</button>'
|
|
assert ref(html, ".btn-primary", "css")
|
|
assert ref(html, "btn-primary", "css")
|
|
assert ref(".btn-primary { color: red }", ".btn-primary", "css")
|
|
assert not ref('<button class="btn-primary">', ".btn", "css")
|
|
assert ref('<button class="btn-primary btn">', ".btn", "css")
|
|
|
|
|
|
def test_snippet_kind_reads_the_symbol_then_the_language():
|
|
from scribe.services.shape_ledger import snippet_kind
|
|
|
|
assert snippet_kind(".btn-primary", "css") == "css"
|
|
assert snippet_kind(".btn-primary", "") == "css"
|
|
assert snippet_kind("confirmed", "typescript") == "sym"
|
|
assert snippet_kind("", "scss") == "css" # whole-stylesheet record
|
|
assert snippet_kind("", "python") == "sym"
|
|
|
|
|
|
def test_route_shapes_param_parses_capped_and_deduped():
|
|
from scribe.routes.plugin import _SHAPES_CAP, _parse_shapes
|
|
|
|
assert _parse_shapes("css:btn-primary,sym:onTrash") == [
|
|
("css", "btn-primary"), ("sym", "onTrash"),
|
|
]
|
|
assert _parse_shapes(" sym:a , sym:a ,bogus:x,sym:,:,") == [("sym", "a")]
|
|
assert _parse_shapes("") == []
|
|
many = ",".join(f"sym:f{i}" for i in range(40))
|
|
assert len(_parse_shapes(many)) == _SHAPES_CAP
|
|
|
|
|
|
def test_hook_is_a_server_internal_classifier():
|
|
"""`hook` is in the status vocabulary but NOT a via a caller may claim —
|
|
a classify_shapes call saying via="hook" would launder judgment as
|
|
evidence (the reverse of the stamping rule's point)."""
|
|
from scribe.services.shape_ledger import _CALLER_VIAS
|
|
|
|
assert "hook" in SHAPE_CLASSIFIERS
|
|
assert "hook" not in _CALLER_VIAS
|
|
|
|
|
|
# --- step 6: the mechanical proposer (pure) ---------------------------------
|
|
|
|
|
|
def test_proposal_columns_and_vocabulary_are_pinned():
|
|
"""Fingerprints + the proposer's standing suggestion live on the row; the
|
|
basis vocabulary is fixed, with `derive` the odd one out (a group, not a
|
|
snippet)."""
|
|
from scribe.models.code_shape import PROPOSAL_BASES
|
|
|
|
cols = CodeShape.__table__.c
|
|
for name in ("signature", "body_sha", "proposed_snippet_id", "proposal_basis",
|
|
"proposal_score", "proposal_group", "proposed_at", "proposed_sha"):
|
|
assert name in cols, name
|
|
fk = next(iter(cols.proposed_snippet_id.foreign_keys))
|
|
assert fk.ondelete == "SET NULL" and fk.column.table.name == "notes"
|
|
assert "ix_code_shapes_proposed" in {ix.name for ix in CodeShape.__table__.indexes}
|
|
assert PROPOSAL_BASES == ("symbol", "text", "reference", "signature", "semantic", "derive")
|
|
|
|
|
|
def test_row_proposal_property_is_one_object_or_none():
|
|
row = CodeShape(project_id=1, repo_key="r", path="a.py", symbol="f", kind="sym")
|
|
assert row.proposal is None
|
|
row.proposed_snippet_id, row.proposal_basis, row.proposal_score = 9, "symbol", 1.0
|
|
assert row.proposal == {"basis": "symbol", "score": 1.0, "snippet_id": 9}
|
|
row.proposed_snippet_id = None
|
|
row.proposal_basis, row.proposal_group, row.proposal_score = "derive", "dup:abc", 3.0
|
|
assert row.proposal == {"basis": "derive", "score": 3.0, "group": "dup:abc"}
|
|
|
|
|
|
def test_signature_similarity_blanks_the_names():
|
|
from scribe.services.shape_ledger import signature_similarity as sim
|
|
|
|
a = "def move_event(project_id: int, event_id: int, after_id: int | None):"
|
|
b = "def move_beat(project_id: int, beat_id: int, after_id: int | None):"
|
|
assert sim(a, "move_event", b, "move_beat") > 0.85
|
|
assert sim(a, "move_event", "def export_pdf(manuscript, design, fonts):", "export_pdf") < 0.6
|
|
assert sim("", "x", b, "move_beat") == 0.0
|
|
# Trivial signatures resemble everything and mean nothing — floored out.
|
|
assert sim("def helper(x):", "helper", "def make_app():", "make_app") == 0.0
|
|
|
|
|
|
def test_text_containment_is_whitespace_insensitive_with_a_floor():
|
|
from scribe.services.shape_ledger import text_contains
|
|
|
|
code = "const ok = await confirmed({ title: 'Delete?', confirmLabel: 'Delete' });\nif (!ok) return;"
|
|
body = "async function onDelete() {\n const ok = await confirmed({\n title: 'Delete?',\n confirmLabel: 'Delete'\n });\n if (!ok) return;\n}"
|
|
assert text_contains(body, code)
|
|
assert text_contains(code, body)
|
|
assert not text_contains("x = 1", "x = 1") # below the substance floor
|
|
|
|
|
|
def _canon(sid, kind="sym", symbol="", locations=(), signature="", code="", project_id=0):
|
|
from scribe.services.shape_ledger import Canon, _norm_text
|
|
return Canon(sid, kind, symbol, tuple(locations), signature, _norm_text(code), project_id)
|
|
|
|
|
|
def test_match_canon_prefers_the_shapes_own_project_on_a_tie():
|
|
"""The same helper recorded in two projects: the shape's own project's
|
|
record is its canon; family canon elsewhere is the fallback."""
|
|
from scribe.services.shape_ledger import match_canon
|
|
family = _canon(3, "sym", "slugify", [("lib/text.py", "slugify")], project_id=1)
|
|
own = _canon(4, "sym", "slugify", [("src/util/text.py", "slugify")], project_id=2)
|
|
assert match_canon("sym", "src/other.py", "slugify", "def slugify(t):", "",
|
|
[family, own], project_id=2) == (4, "symbol", 1.0)
|
|
assert match_canon("sym", "src/other.py", "slugify", "def slugify(t):", "",
|
|
[family, own], project_id=1) == (3, "symbol", 1.0)
|
|
# No project given → first-best stands; nothing breaks.
|
|
assert match_canon("sym", "src/other.py", "slugify", "def slugify(t):", "",
|
|
[family, own])[1] == "symbol"
|
|
|
|
|
|
def test_match_canon_orders_bases_strongest_first_and_respects_kind():
|
|
from scribe.services.shape_ledger import match_canon
|
|
|
|
confirmed = _canon(
|
|
7, "sym", "confirmed", [("frontend/src/composables/useConfirm.ts", "confirmed")],
|
|
"export async function confirmed(opts: ConfirmOptions): Promise<boolean> {",
|
|
"export async function confirmed(opts: ConfirmOptions): Promise<boolean> { /* singleton */ }",
|
|
)
|
|
mover = _canon(
|
|
8, "sym", "move_beat", [("src/forge/plot.py", "move_beat")],
|
|
"def move_beat(project_id: int, beat_id: int, after_id: int | None) -> None:",
|
|
)
|
|
btn = _canon(9, "css", ".btn-primary", [("web/buttons.css", ".btn-primary")],
|
|
".btn-primary {", ".btn-primary { color: var(--action-primary); padding: 4px 8px; border-radius: 4px; }")
|
|
canons = [confirmed, mover, btn]
|
|
|
|
# symbol: a second `confirmed` defined elsewhere answers to #7 — but the
|
|
# canon's own location never does (that row is canonical, not a proposal).
|
|
assert match_canon("sym", "src/other.ts", "confirmed", "function confirmed() {", "", canons) == (7, "symbol", 1.0)
|
|
assert match_canon("sym", "frontend/src/composables/useConfirm.ts", "confirmed",
|
|
"export async function confirmed(", "", canons) is None
|
|
# reference: a call site of the canon.
|
|
body = "async function onTrash() {\n const ok = await confirmed({ title: 'x' });\n if (!ok) return;\n}"
|
|
assert match_canon("sym", "c.vue", "onTrash", "async function onTrash() {", body, canons) == (7, "reference", 0.9)
|
|
# signature: the family shape, names blanked.
|
|
hit = match_canon("sym", "src/forge/timeline.py", "move_event",
|
|
"def move_event(project_id: int, event_id: int, after_id: int | None) -> None:",
|
|
" pass", canons)
|
|
assert hit and hit[0] == 8 and hit[1] == "signature" and hit[2] >= 0.8
|
|
# text: the canon's code contains the shape's body (a css copy), kind-matched —
|
|
# the same text as a `sym` shape matches no css canon.
|
|
css_body = ".btn-primary { color: var(--action-primary); padding: 4px 8px; border-radius: 4px; }"
|
|
assert match_canon("css", "web/other.css", "btn-big", ".btn-big {", css_body, canons) == (9, "text", 0.95)
|
|
assert match_canon("sym", "web/other.css", "btn-big", ".btn-big {", css_body, canons) is None
|
|
# nothing in play
|
|
assert match_canon("sym", "x.py", "unrelated", "def unrelated(a, b, c, d, e):", "return 1", canons) is None
|
|
|
|
|
|
def test_match_canon_symbol_beats_everything_including_css_copies():
|
|
"""The previous test's css `btn-primary`-elsewhere case, stated plainly:
|
|
a second definition of the canon's own name is the symbol basis."""
|
|
from scribe.services.shape_ledger import match_canon
|
|
btn = _canon(9, "css", ".btn-primary", [("web/buttons.css", ".btn-primary")], ".btn-primary {", ".btn-primary { color: red; padding: 4px 8px; border-radius: 4px; }")
|
|
assert match_canon("css", "web/other.css", "btn-primary", ".btn-primary {", ".btn-primary { color: blue }", [btn]) == (9, "symbol", 1.0)
|
|
|
|
|
|
def test_derive_groups_copy_before_name_with_floors():
|
|
from scribe.services.shape_ledger import derive_groups
|
|
|
|
rows = [
|
|
("a.py", "sym", "helper", "sha1"), ("b.py", "sym", "helper", "sha1"), # identical copies
|
|
("c.py", "sym", "helper", "sha9"), # same name, 3rd file
|
|
("d.css", "css", "btn", "s1"), ("e.css", "css", "btn", "s2"), ("f.css", "css", "btn", "s3"),
|
|
("g.py", "sym", "main", "s4"), ("h.py", "sym", "main", "s5"), # only 2 files → no name group
|
|
("i.py", "sym", "one", "s6"),
|
|
]
|
|
g = derive_groups(rows)
|
|
assert g[("a.py", "sym", "helper")] == "dup:sha1" == g[("b.py", "sym", "helper")]
|
|
assert g[("c.py", "sym", "helper")] == "name:sym:helper"
|
|
assert g[("d.css", "css", "btn")] == "name:css:btn"
|
|
assert ("g.py", "sym", "main") not in g
|
|
assert ("i.py", "sym", "one") not in g
|
|
|
|
|
|
def test_confirm_requires_a_named_scope():
|
|
import asyncio
|
|
|
|
from scribe.services.shape_ledger import confirm_proposals
|
|
|
|
with pytest.raises(ValueError) as err:
|
|
asyncio.run(confirm_proposals(1, 2))
|
|
assert "name what you reviewed" in str(err.value)
|
|
|
|
|
|
def test_proposer_tools_are_mounted():
|
|
from scribe.mcp.server import build_mcp_server
|
|
|
|
mcp = build_mcp_server()
|
|
assert mcp._tool_manager.get_tool("confirm_shape_proposals") is not None
|
|
tool = mcp._tool_manager.get_tool("list_shapes")
|
|
assert "proposal" in tool.parameters.get("properties", {})
|
|
|
|
|
|
# --- step 7: the divergence readout (pure) ----------------------------------
|
|
|
|
|
|
def _row(path, kind="sym", status="unclassified", snippet_id=None):
|
|
r = CodeShape(project_id=1, repo_key="r", path=path, symbol=path.rsplit("/", 1)[-1], kind=kind)
|
|
r.status, r.snippet_id = status, snippet_id
|
|
return r
|
|
|
|
|
|
def test_dominant_canon_needs_enough_judged_siblings_and_a_clear_majority():
|
|
from scribe.services.shape_ledger import dominant_canon
|
|
|
|
dense = [_row(f"c/{i}", status="instance", snippet_id=7) for i in range(4)] + [
|
|
_row("c/x", status="instance", snippet_id=8), _row("c/y")]
|
|
assert dominant_canon(dense) == (7, 4, 5)
|
|
sparse = [_row("c/a", status="instance", snippet_id=7), _row("c/b", status="instance", snippet_id=7)]
|
|
assert dominant_canon(sparse) is None # 2 judged < floor
|
|
split = [_row(f"c/{i}", status="instance", snippet_id=7) for i in range(2)] + [
|
|
_row(f"c/{i+5}", status="instance", snippet_id=8) for i in range(2)]
|
|
assert dominant_canon(split) is None # 50% < 60% share
|
|
# Variants are departures, not votes; canonical counts like an instance.
|
|
mixed = [_row("c/a", status="canonical", snippet_id=7)] + [
|
|
_row(f"c/{i}", status="instance", snippet_id=7) for i in range(2)] + [
|
|
_row("c/v", status="variant", snippet_id=9)]
|
|
assert dominant_canon(mixed) == (7, 3, 3)
|
|
|
|
|
|
def test_history_and_readout_tools_are_mounted_and_shape_history_is_read_only():
|
|
from scribe.mcp.server import _READ_ONLY_TOOLS, build_mcp_server
|
|
|
|
mcp = build_mcp_server()
|
|
assert mcp._tool_manager.get_tool("shape_history") is not None
|
|
assert "shape_history" in _READ_ONLY_TOOLS
|
|
assert "flag" in mcp._tool_manager.get_tool("list_shapes").parameters.get("properties", {})
|
|
|
|
|
|
def test_history_and_divergence_columns_are_pinned():
|
|
from scribe.models.code_shape import SHAPE_EVENTS, CodeShapeEvent
|
|
|
|
cols = CodeShape.__table__.c
|
|
for name in ("classified_sha", "recheck_at", "diverges_from"):
|
|
assert name in cols, name
|
|
assert "ix_code_shapes_diverges" in {ix.name for ix in CodeShape.__table__.indexes}
|
|
ev = CodeShapeEvent.__table__
|
|
fk = next(iter(ev.c.shape_id.foreign_keys))
|
|
assert fk.ondelete == "CASCADE" and fk.column.table.name == "code_shapes"
|
|
assert not ev.c.snippet_id.foreign_keys # history outlives the snippet
|
|
assert SHAPE_EVENTS == ("classified", "vanished", "reappeared", "drifted")
|
|
assert "code_shape_events" in Base.metadata.tables
|