CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 17s
CI & Build / TypeScript typecheck (push) Successful in 54s
CI & Build / integration (push) Successful in 1m9s
CI & Build / Python tests (push) Successful in 2m12s
CI & Build / Build & push image (push) Successful in 31s
Snippet locations are keyed by path too, so a snippet recorded under a moved directory reads `missing` at its next check. The tool is now move_path(project_id, from_path, to_path). It moves the ledger rows as before, and re-paths the project's snippet locations under from_path through update_snippet, so each body and its indexed mirror stay one record. A snippet the caller can only read is listed in snippets_read_only. The tool refuses only when nothing at all is recorded under from_path. The path test is the one snippets already uses (_path_touches), and the plugin version is minted for the skill edit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
450 lines
23 KiB
Python
450 lines
23 KiB
Python
"""Shape-ledger MCP tools — the classification write/read surface.
|
|
|
|
The accounting model: the snippet library records CANON (small);
|
|
the ledger accounts for EVERY extracted shape (total). These tools are how
|
|
agents move shapes out of `unclassified` — the todo state — and how they read
|
|
what still needs judgment. The ledger rows themselves are fed by the coverage
|
|
refresh; a judgment given with `repo` may also land on a shape written this
|
|
turn that the sync has not seen yet.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from grimoire.mcp._context import current_user_id
|
|
from grimoire.services import coverage as coverage_svc
|
|
from grimoire.services import shape_ledger as shape_ledger_svc
|
|
from grimoire.services import snippets as snippets_svc
|
|
|
|
|
|
async def classify_shapes(
|
|
project_id: int, classifications: list[dict], via: str = "agent", repo: str = ""
|
|
) -> dict:
|
|
"""Record judgments for a project's code shapes — in batch, as rows.
|
|
|
|
EVERY shape in a bound repo should end up classified:
|
|
- `instance` of snippet N — it conforms to recorded canon (family-level
|
|
canon in another project counts; that fully accounts for the shape).
|
|
- `variant` of snippet N — a deliberate, named departure. `reason`
|
|
(the why) is REQUIRED; it is the record.
|
|
- `exempt` — judged genuinely one-off. `reason` REQUIRED.
|
|
- `canonical` of snippet N — this row IS the snippet's reference
|
|
(rarely set by hand; the coverage sync stamps these mechanically).
|
|
- `unclassified` — withdraw a judgment; the shape rejoins the todo.
|
|
|
|
Consumer maps belong HERE, not in prose: when an audit enumerates call
|
|
sites of a canonical helper, each call site's defining shape is an
|
|
`instance` row — a sentence in a verification detail cannot be sorted,
|
|
queried, or diffed.
|
|
|
|
Args:
|
|
project_id: The project whose ledger is being judged.
|
|
classifications: Objects of {path, symbol, status, kind?, snippet_id?,
|
|
idea_id?, reason?, reason_code?}. path+symbol name the shape exactly as
|
|
list_shapes shows it; kind ("sym"/"css") narrows when one file
|
|
defines both. snippet_id is required for canonical/instance/
|
|
variant — or idea_id, a family idea: the shape is then judged
|
|
against that idea's reference implementation in the shape's
|
|
language, or its first reference when none is in that language
|
|
(an idea is shared across languages: a Python shape can be an
|
|
instance of an idea whose reference is Go). reason is required
|
|
for variant/exempt. reason_code is
|
|
an OPTIONAL index beside the prose (one of: scoped-css,
|
|
one-off-handler, test-helper, convention-plumbing, pure-helper,
|
|
generated, script, typed-record) so the ledger can be filtered
|
|
and aggregated by kind of one-off — the prose stays the record.
|
|
uses is an OPTIONAL list of snippet ids this shape CALLS:
|
|
conformance (status + snippet_id) says what shape it is, uses
|
|
says which canonical helpers it consumes — a service function
|
|
can be an instance of the service-function convention AND use
|
|
hash_token. Consumer maps are uses edges; list_shapes(uses=N)
|
|
and get_snippet's `uses` read them.
|
|
via: Who is judging — "agent" (default), "audit" (a sweep), or
|
|
"import" (carrying maps recorded elsewhere).
|
|
repo: The repo the shapes live in (its remote URL or bound key), for
|
|
judging what you wrote THIS turn — shapes the ledger has not
|
|
synced yet. With it, a shape no row matches is recorded as a
|
|
provisional row under that repo and judged; the next sync
|
|
confirms it. Must be bound to the project. Omit it to judge only
|
|
synced rows.
|
|
|
|
FAMILY CANON FOLLOWS. A shape judged against a canon family idea's
|
|
reference IS the project's answer to that idea: an instance answers it
|
|
`adopted`, a variant `variant` (with the shape's reason), and
|
|
withdrawing the shapes that gave an answer returns it to `unassessed`.
|
|
The adoption ledger is moved for you — `family` in the result lists the
|
|
answers that moved — and assess_family_adoption refuses an answer the
|
|
shapes contradict. The family-canon skill has when to answer an idea.
|
|
|
|
All-or-nothing: a structural error, a missing snippet target, an unbound
|
|
repo, or no write access applies NOTHING. Returns {"classified": N,
|
|
"unmatched": [...], "provisional": N?, "family": [...]?} — unmatched names
|
|
shapes no live ledger row matches (the tree may have moved since you
|
|
listed; pass `repo` for a shape you just wrote, or re-run the project's
|
|
coverage refresh).
|
|
"""
|
|
uid = current_user_id()
|
|
return await shape_ledger_svc.classify_shapes(
|
|
uid, project_id, classifications, via=via, repo=repo
|
|
)
|
|
|
|
|
|
async def list_shapes(
|
|
project_id: int,
|
|
status: str = "",
|
|
path: str = "",
|
|
snippet_id: int = 0,
|
|
include_vanished: bool = False,
|
|
limit: int = 100,
|
|
offset: int = 0,
|
|
proposal: str = "",
|
|
flag: str = "",
|
|
compact: bool = False,
|
|
uses: int = 0,
|
|
) -> dict:
|
|
"""Read a project's shape ledger — `status="unclassified"` IS the todo.
|
|
|
|
Every extracted definition in the project's bound repos has a row here
|
|
(fed by the coverage refresh). Filters compose:
|
|
|
|
Args:
|
|
status: canonical | instance | variant | exempt | scoped | unclassified.
|
|
`scoped` is the sync's mechanical stamp on one-offs by
|
|
construction (a Vue component's scoped <style> rules and its
|
|
<script setup> functions): accounted for, not judged, still
|
|
proposed against / grouped / flagged, and overridable by any
|
|
classify_shapes judgment. The human todo is `unclassified`.
|
|
path: exact file, or a directory — matches everything beneath it
|
|
(the coverage line's "largest" dirs go straight in here).
|
|
snippet_id: rows classified against this snippet (instance/variant
|
|
of it — conformance).
|
|
uses: rows that CALL this snippet — the consumer map proper,
|
|
whatever shape each row is itself; edges come from judgments
|
|
(classify_shapes uses=), the write-path hook, and the proposer's
|
|
by-name reference hits.
|
|
include_vanished: include shapes no longer in the tree (history).
|
|
limit/offset: page through big ledgers (limit caps at 500).
|
|
compact: rows as `path · symbol · kind · status · signature` plus
|
|
snippet_id / by / proposal / diverges_from / recheck only when
|
|
set — no commits, shas or timestamps. THE form for an audit:
|
|
a full 500-row page fits the tool budget. The default rows carry
|
|
everything (shape_history-grade bookkeeping).
|
|
proposal: the proposer's queue — "any", "canon" (rows the
|
|
machine thinks are an instance of a snippet: `proposal` carries
|
|
snippet_id, basis, score), "derive" (rows that repeat with NO
|
|
canon: `proposal.group` names the family), or one basis
|
|
(symbol/text/reference/signature/semantic/family). "family" is
|
|
a canon family idea's reference — another project's, often
|
|
another language's — that the body means: the top match over
|
|
every snippet you can read, on a platform the project shares.
|
|
flag: the divergence readout — "divergence": shapes new
|
|
since the previous refresh in a directory where one canon
|
|
dominates the judged siblings and NOT proposed as that canon
|
|
(`diverges_from` names it: button B where button A is canon —
|
|
classify it: instance if it should use the canon, variant with
|
|
the why if deliberate); "recheck": judged instances/variants
|
|
whose body changed since judged (the judgment stands; confirm
|
|
it again with classify_shapes, or re-judge); "unused-css":
|
|
live css rules no file's markup names — a
|
|
deletion candidate to look at, never auto-deleted. Transition
|
|
classes and concatenated names are read, so the list is
|
|
worth acting on; a name assembled in a script still is not.
|
|
|
|
Returns {"shapes": [...], "total": N} — total counts every match, not
|
|
just this page. Every css row carries `used_by` {count, paths} — the
|
|
files whose markup names its class (the CSS consumer
|
|
map: a scoped rule is used by its own template; a shared recipe by
|
|
many; a count of 0 is "no template names it"). Each row's
|
|
`classified_by` says who judged: agent /
|
|
audit / import are judgments; `mechanical` is the canonical stamp the
|
|
sync applies; `hook` is write-path EVIDENCE — the session pulled
|
|
a snippet and then wrote code referencing/resembling it, so the shape
|
|
was stamped an instance with the evidence in `reason`. A hook row is
|
|
overridable by any classify_shapes call; it never overrides yours.
|
|
|
|
THE FAST PATH through a big todo is the proposer's queue: every coverage
|
|
refresh matches unclassified shapes against canon (strongest basis
|
|
first: same symbol elsewhere → textual containment → body references
|
|
the canon → signature resemblance → semantic, and a family idea's
|
|
reference in any language) and attaches a
|
|
`proposal` to each row it can speak for. Review `proposal="canon"` by
|
|
snippet or directory, then confirm_shape_proposals the ones that hold —
|
|
hundreds at a time — and classify_shapes the rest (variant/exempt, or
|
|
instance of a different snippet). `proposal="derive"` lists the
|
|
derive-first candidates: a repeating shape with NO recorded canon is
|
|
never N loose classifications — consolidate onto a reference,
|
|
create_snippet it, then classify the group against it.
|
|
"""
|
|
uid = current_user_id()
|
|
rows, total = await shape_ledger_svc.list_project_shapes(
|
|
uid, project_id,
|
|
status=status, path=path, snippet_id=snippet_id,
|
|
include_vanished=include_vanished, limit=limit, offset=offset,
|
|
proposal=proposal, flag=flag, uses=uses,
|
|
)
|
|
shapes = [r.to_compact() if compact else r.to_dict() for r in rows]
|
|
used_by = await shape_ledger_svc.used_by_map(rows)
|
|
for row, out in zip(rows, shapes):
|
|
if row.id in used_by:
|
|
out["used_by"] = used_by[row.id]
|
|
return {"shapes": shapes, "total": total}
|
|
|
|
|
|
async def classify_shapes_by_rule(
|
|
project_id: int,
|
|
path: str,
|
|
status: str,
|
|
pattern: str = "",
|
|
kind: str = "",
|
|
snippet_id: int = 0,
|
|
reason: str = "",
|
|
via: str = "agent",
|
|
include_judged: bool = False,
|
|
reason_code: str = "",
|
|
uses: list[int] | None = None,
|
|
) -> dict:
|
|
"""The sweep form of classify_shapes: ONE judgment applied to every
|
|
unclassified shape under a directory whose symbol matches a glob.
|
|
|
|
For the long tail an audit judges by family, not by row — "every scoped
|
|
rule under frontend/src/views is exempt: styles one element of its view",
|
|
"every `*_scheduler.py` symbol is an instance of ScheduledJob" — where
|
|
listing 900 rows and sending them back is the whole cost. The row form
|
|
stays the precise tool; reach for it when each row gets its own reason.
|
|
|
|
Args:
|
|
project_id: The project whose ledger is being judged.
|
|
path: A file, or a directory and everything beneath it. Required —
|
|
a sweep names what it judges.
|
|
status: instance | variant | exempt | unclassified (canonical is the
|
|
sync's stamp, not a sweep's).
|
|
pattern: Shell glob on the symbol (`*_rows`, `_*`, `modal-*`, `*`);
|
|
"" = every symbol under path.
|
|
kind: "sym" or "css" to narrow; "" = both.
|
|
snippet_id: Required for instance/variant — the canon judged against.
|
|
reason: Required for variant/exempt — the why, recorded on every row.
|
|
via: "agent" (default) | "audit" | "import".
|
|
reason_code: Optional catalogue code beside the reason (see
|
|
classify_shapes) — a sweep is exactly where one applies.
|
|
uses: Optional snippet ids every matched shape CALLS — e.g.
|
|
"every *_scheduler.py symbol uses ScheduledJob".
|
|
include_judged: By default only unjudged rows are touched —
|
|
`unclassified` and the sync's mechanical `scoped` stamp — a
|
|
sweep never silently overwrites a judgment. True re-judges every
|
|
matching live row (use to re-confirm after a recheck, or to
|
|
revise a family you judged earlier).
|
|
|
|
One transaction: applies whole or not at all. Returns
|
|
{"classified": N, "sample": ["path::symbol", ...], "family": [...]?}
|
|
(sample: first 12, sorted) so you can see what the rule reached; N = 0
|
|
means the rule matched nothing live and unclassified — widen the pattern
|
|
or refresh coverage. As with classify_shapes, a family idea's reference
|
|
judged here moves the project's answer to that idea (`family`).
|
|
"""
|
|
uid = current_user_id()
|
|
try:
|
|
return await shape_ledger_svc.classify_shapes_where(
|
|
uid, project_id, path=path, status=status, pattern=pattern,
|
|
kind=kind, snippet_id=snippet_id or None, reason=reason or None,
|
|
via=via, include_judged=include_judged,
|
|
reason_code=reason_code or None, uses=uses or None,
|
|
)
|
|
except ValueError as exc:
|
|
return {"error": str(exc)}
|
|
|
|
|
|
async def shape_history(
|
|
project_id: int, path: str, symbol: str = "", limit: int = 200
|
|
) -> dict:
|
|
"""What was used here, when, and why — a shape's (or a directory's)
|
|
history from the ledger.
|
|
|
|
`shapes` are the current rows at `path` (a file, or a directory and
|
|
everything beneath it; `symbol` narrows to one definition) with
|
|
first/last-seen commits, vanished_at, and the standing judgment;
|
|
`events` are the state changes, oldest first: `classified` (status,
|
|
snippet_id, who, why — one per judgment, so a shape that was an instance
|
|
of #N and later a variant of #M shows both), `vanished`, `reappeared`,
|
|
`drifted` (the body moved under a judgment; see list_shapes flag=
|
|
"recheck"), `moved` (move_path carried the row; the event's `path`
|
|
is the one it left). Each event carries the commit the tree was read at.
|
|
|
|
Read it as a timeline: "instance of #N from <first classified at>,
|
|
re-judged variant of #M at <at> because <reason>, vanished at <commit>".
|
|
Read-only; requires read access to the project.
|
|
"""
|
|
uid = current_user_id()
|
|
return await shape_ledger_svc.shape_history(
|
|
uid, project_id, path, symbol=symbol, limit=limit
|
|
)
|
|
|
|
|
|
async def stamps_to_review(project_id: int, top: int = 10) -> dict:
|
|
"""Judged rows whose evidence no longer meets the ledger's bar, and canons
|
|
whose own rows no longer agree what they are. **Read-only — you decide.**
|
|
|
|
This tool deliberately cannot fix anything. Every row it lists was written
|
|
unattended by the write-path hook on a similarity score, and what made
|
|
that harmful was not one bad score but a machine recording a permanent
|
|
classification with nobody reading it. Un-asserting them automatically
|
|
would be the same mistake with a wider blast radius. So: read the
|
|
evidence, judge, and record the judgment yourself with `classify_shapes`
|
|
— under your own name, with a reason.
|
|
|
|
`weak` — rows the hook stamped on a resemblance below the current floor
|
|
(`floor` in the response). The floor arrived after they did, and a guard
|
|
at the point of classification never undoes what is already stored. Rows
|
|
an agent or an audit judged are NOT listed at any age: a judgment is not
|
|
weak evidence, it is a different kind of evidence. Each row carries its
|
|
score, signature and derived form so you can judge rather than trust the
|
|
threshold.
|
|
|
|
`incoherent` — canons whose membership no longer agrees what the canon
|
|
is. A canon asserts that some shapes are the same sort of thing; when its
|
|
members are a class, three getters and a dozen tests, that assertion has
|
|
stopped being true and every base-rate reading built on it — the
|
|
divergence prompt included — is reading noise.
|
|
|
|
Disagreement is judged by FAMILY (a sync helper and an async one are one
|
|
family), and a method defined beside a member class counts as part of the
|
|
shape rather than against it — so a model convention that covers both the
|
|
class and its `to_dict` reads as coherent, which it is. Each
|
|
entry carries `families` and `forms`, the majority `family`, `attached`
|
|
(minority rows excused as methods of a member), `strangers` — THE ROWS
|
|
THAT DO NOT FIT, which is what you judge — and `unattended` / `weak_rows`,
|
|
the count written by the hook with nobody reading. An incoherence whose
|
|
rows are all judged is likelier this check being strict than a bad
|
|
ledger; one made of unattended rows is the real thing.
|
|
|
|
A row listed here is a question, not a verdict. Some will be correct.
|
|
|
|
Read-only; requires read access to the project.
|
|
"""
|
|
uid = current_user_id()
|
|
rows = await shape_ledger_svc.live_rows_for(uid, project_id)
|
|
return shape_ledger_svc.stamps_to_review(rows, top=top)
|
|
|
|
|
|
async def confirm_shape_proposals(
|
|
project_id: int,
|
|
snippet_id: int = 0,
|
|
path: str = "",
|
|
basis: str = "",
|
|
min_score: float = 0.0,
|
|
) -> dict:
|
|
"""Confirm the proposer's canon proposals you have reviewed, in batch.
|
|
|
|
The machine proposes, judgment classifies: each matching row —
|
|
live, unclassified, carrying a `proposal` with a snippet_id — becomes
|
|
`instance` of that snippet, classified_by="agent", reason naming the
|
|
basis and score. Narrow to what you actually looked at: at least one of
|
|
snippet_id (confirm one canon's whole queue after reading its
|
|
`list_shapes(proposal="canon", ...)` page), path (a directory you
|
|
audited), or basis (e.g. "symbol" and "reference" are near-certain;
|
|
"semantic" and "family" deserve a look first) is required — a bare
|
|
confirm-all is not a judgment. min_score trims a basis's tail.
|
|
|
|
Proposals you do NOT confirm are judged with classify_shapes (variant,
|
|
exempt, or instance of a different snippet) — any judgment retires the
|
|
proposal. A confirmed family reference answers that idea `adopted` in
|
|
the project (`family` lists the answers that moved). Requires write
|
|
access. Returns {"confirmed": N, "family": [...]?}.
|
|
"""
|
|
uid = current_user_id()
|
|
try:
|
|
return await shape_ledger_svc.confirm_proposals(
|
|
uid, project_id, snippet_id=snippet_id, path=path, basis=basis,
|
|
min_score=min_score,
|
|
)
|
|
except ValueError as exc:
|
|
return {"error": str(exc)}
|
|
|
|
|
|
async def refresh_pattern_coverage(project_id: int) -> dict:
|
|
"""Seed or refresh the project's shape ledger NOW, and return the readout.
|
|
|
|
The synchronous form of the arrival-moment background seed:
|
|
downloads the bound repos through the OWNER's forge connections, upserts
|
|
every extracted shape into the ledger (new shapes arrive `unclassified`),
|
|
re-stamps snippet reference locations as canonical, and recomputes the
|
|
accounting. Reach for it when the ledger must be current before you act —
|
|
a classification batch about to run, a coverage question asked directly —
|
|
rather than waiting on the background seed an enter_project triggers.
|
|
|
|
Takes seconds, not milliseconds (it moves repo archives). Requires write
|
|
access to the project. Errors name the fix: no forge connection → the
|
|
owner adds one (Settings → Integrations → Git Forges); no served repo →
|
|
bind_repo on a host a connection serves.
|
|
|
|
The refresh is also when the mechanical proposer runs: with the
|
|
repo bodies in hand it matches every changed unclassified shape against
|
|
canon and records proposals (see list_shapes proposal=), then regroups
|
|
the derive-first candidates. Semantic matching is capped per refresh, so
|
|
a large ledger's queue grows across refreshes rather than in one.
|
|
|
|
Returns the accounting payload — total, accounted, counts by status,
|
|
unclassified, repos, largest_gaps, `proposed` (canon proposals awaiting
|
|
confirmation), `derive_groups` (the biggest repeats-with-no-canon
|
|
families, each css one with `consumers` — the files whose markup
|
|
render it), `unused_css` (css rules no template names —
|
|
counting a `<Transition name=>`'s generated classes and concatenated
|
|
names as named; None where the map has no evidence of templates), `derive_new` (copies
|
|
that joined a family since the previous
|
|
refresh — the drift to act on now: derive the canon, don't queue an
|
|
audit), `proposer` (what this refresh examined) — plus
|
|
`pattern_coverage`, the same one-line summary enter_project carries.
|
|
"""
|
|
uid = current_user_id()
|
|
coverage = await coverage_svc.refresh_for_caller(uid, project_id)
|
|
return {
|
|
"pattern_coverage": coverage_svc.coverage_line(coverage),
|
|
**coverage,
|
|
}
|
|
|
|
|
|
async def move_path(project_id: int, from_path: str, to_path: str) -> dict:
|
|
"""Carry what the record knows about a file or directory that moved in
|
|
the tree: its shape judgments and its snippets' recorded locations.
|
|
|
|
Both are keyed by path. After `git mv src/old src/new`, the next coverage
|
|
refresh would mark every row under src/old vanished and list the same
|
|
shapes again under src/new, unclassified, with every judgment left
|
|
behind. Each snippet recorded under src/old would also read `missing`.
|
|
Run this once the move is committed, before that refresh.
|
|
- Every ledger row under `from_path` is re-keyed to the same place under
|
|
`to_path`, in every repo bound to the project. It keeps its judgment,
|
|
uses edges and history, and gains a `moved` event (shape_history).
|
|
- Every snippet of the project recorded under `from_path` has its
|
|
location re-pathed.
|
|
|
|
A ledger row already at the new path is replaced when nobody judged it
|
|
(the refresh ran first, or the sync stamped it `scoped`). A judged one
|
|
stands; the old row stays where it is and is listed in `kept`. The
|
|
refresh that follows compares bodies as always, so a judged shape whose
|
|
body changed in the move comes back flagged `recheck`.
|
|
|
|
Args:
|
|
project_id: The project whose record moves. Requires write access.
|
|
from_path: The file or directory as it was recorded.
|
|
to_path: Where it lives now. Neither path may contain the other.
|
|
|
|
Returns {"moved": N, "replaced": N, "kept": [{path, symbol, kind}],
|
|
"snippets_moved": [ids], "snippets_read_only": [ids]}. A snippet shared
|
|
with you read-only stays where it is, and its owner moves it.
|
|
"""
|
|
uid = current_user_id()
|
|
out = await shape_ledger_svc.move_shapes(uid, project_id, from_path, to_path)
|
|
snippets = await snippets_svc.move_locations(uid, project_id, from_path, to_path)
|
|
out["snippets_moved"] = snippets["moved"]
|
|
out["snippets_read_only"] = snippets["read_only"]
|
|
if not (out["moved"] or out["kept"] or snippets["moved"] or snippets["read_only"]):
|
|
raise ValueError(f"nothing in project {project_id} is recorded under `{from_path}`")
|
|
return out
|
|
|
|
|
|
def register(mcp) -> None:
|
|
for fn in (
|
|
classify_shapes, classify_shapes_by_rule, list_shapes,
|
|
refresh_pattern_coverage, confirm_shape_proposals, shape_history,
|
|
stamps_to_review, move_path,
|
|
):
|
|
mcp.tool(name=fn.__name__)(fn)
|