Cotton code
135 lines · 5.8 KB · RustRaw
//! A tool's own description points at tools that exist.//!//! The descriptions and the JSON schemas are the ASSISTANT-FACING API: they are//! the sentences a model reads to choose its next call, and they are full of//! cross-references — "a `message` hit carries threadId for//! read_message_thread", "get_photo and get_video expand one". A reference//! that names a tool the surface no longer carries is a hallucinated tool call//! in somebody's session, and nothing else catches it: a description is a//! `&'static str`, so renaming or retiring a tool leaves every sentence//! pointing at it compiling, and `tool_surface_tests.rs` — which holds the verb//! GRID against the catalogue — never reads the prose.//!//! Two live examples of this drift were found by hand on 2026-09-26 and fixed//! in the same change: `search_records` still advertised "the user's reusable//! prompts", a record kind retired at store schema 29, and `create_project`'s//! `template` prose still promised prompts that no template seeds//! (`cotton_core::templates::TemplateSeeds` is todos, one money account and a//! description, and nothing else). Both were sentences, not references, so this//! guard would not have caught either — what it catches is the other half of the//! same class, the half that is mechanically checkable.//!//! It lives in `cotton-tools` because both halves are here: the catalogue and//! the lookup that resolves a name.use std::collections::BTreeSet;use cotton_tools::mcp::catalogue::TOOLS;use cotton_tools::mcp::protocol::tool_by_name;/// Words that LOOK like a tool reference and are not, each with its reason.////// A written list, never a pattern: "anything that does not resolve is/// probably a field name" would let a real stale reference through, which is/// the whole thing this file exists to refuse.const NOT_TOOL_REFERENCES: &[(&str, &str)] = &[    (        "money_in",        "a value of an account's `bucket` reading, not a verb — list_money_accounts answers it",    ),    ("money_out", "the other half of the same `bucket` reading"),    (        "person_note",        "a record KIND in the link verbs' enums; a person note has no tool of its own",    ),    (        "record_id",        "a field of the change the generic record verbs answer — the id of the record a change \         touched — not the `record_` verb family",    ),];/// The first segment of every tool name that has one — `get`, `list`, `create`,/// `read`, and the rest.////// DERIVED from the catalogue rather than written down, so a tool family with a/// new verb is swept the day it lands instead of the day somebody remembers to/// extend a list here.fn tool_verbs() -> BTreeSet<&'static str> {    TOOLS        .iter()        .filter_map(|tool| tool.name.split_once('_'))        .map(|(verb, _)| verb)        .collect()}/// Every word in `text` shaped like a tool name — a run of letters, digits and/// underscores that carries at least one underscore, whose first segment is one/// of the catalogue's own verbs.fn tool_references(text: &str, verbs: &BTreeSet<&'static str>) -> Vec<String> {    text.split(|character: char| !(character.is_ascii_alphanumeric() || character == '_'))        .filter(|word| word.contains('_'))        .filter(|word| {            word.split_once('_')                .is_some_and(|(verb, _)| verbs.contains(verb))        })        .map(str::to_string)        .collect()}/// RULE: Every tool an MCP tool's own description or argument schema names is a tool the database still has.////// WHY: an assistant reads a description and calls what it names. A tool that/// was renamed or retired leaves every sentence pointing at it compiling and/// green, so the first thing that finds out is a session calling a tool that/// does not exist — and the person watching sees the assistant fail at/// something it was told to do.#[test]fn every_tool_a_description_names_is_a_tool_this_database_has() {    let verbs = tool_verbs();    let excused: BTreeSet<&str> = NOT_TOOL_REFERENCES.iter().map(|(word, _)| *word).collect();    for tool in TOOLS {        for field in [tool.description, tool.input_schema] {            for name in tool_references(field, &verbs) {                if excused.contains(name.as_str()) {                    continue;                }                assert!(                    tool_by_name(&name).is_some(),                    "{}'s description or schema names `{name}`, which is not a tool this \                     database has. Either the tool was renamed or retired and the sentence did \                     not follow — an assistant reading it will call a tool that does not exist — \                     or the word is not a tool reference at all and belongs in \                     NOT_TOOL_REFERENCES with the reason.",                    tool.name                );            }        }    }}/// RULE: The check that a tool description names only real tools can still fail.////// WHY: the sweep is a text scan, and a text scan that stops matching goes/// green rather than red. This feeds it a sentence naming a tool that does not/// exist and insists it is seen.#[test]fn a_sentence_naming_a_tool_that_does_not_exist_is_seen() {    let verbs = tool_verbs();    let invented = "Follow up with get_nothing_at_all (see list_todos).";    let found = tool_references(invented, &verbs);    assert!(        found.contains(&"get_nothing_at_all".to_string()),        "the sweep did not see a made-up reference in {invented:?} — it found {found:?}"    );    assert!(        tool_by_name("get_nothing_at_all").is_none(),        "the catalogue grew a tool this test invented"    );    assert!(        found.contains(&"list_todos".to_string()) && tool_by_name("list_todos").is_some(),        "the sweep did not see a real reference beside the made-up one — it found {found:?}"    );}