"""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