served_text_audit.py

scripts/served_text_audit.py

Audit the SERVED surface for names a client cannot call.

_allowed

_allowed()

scripts/served_text_audit.py:195

Show on the map →

Called by cmd_scan, collect

Calls none

Source · 21 lines
def _allowed() -> tuple[set[str], set[str]]:
    """Derive what is callable from the code, never from a restated list.

    THREE VOCABULARIES, all imported: the registered TOOLS, the `index` ACTIONS, and the
    `search` CORPORA. A served string saying `action='cull'` or `corpus='prose'` is correct
    even though neither is a tool, and the first version of this audit flagged three such
    lines because it only knew about tools.

    Deriving rather than listing is also what makes rule A survive the next rename — which is
    the reasoning the shipped guard states and then undercuts by requiring a backtick.

    @brief Allowed callable names.
    @return (tools, tools|actions|corpora).
    @version 2
    """
    sys.path.insert(0, str(REPO))
    from clew.mcp_server.server import INDEX_ACTIONS, TIER0_TOOLS
    from clew.mcp_server.tools_query import CORPORA, TIER1_TOOLS

    tools = set(TIER0_TOOLS) | set(TIER1_TOOLS)
    return tools, tools | set(INDEX_ACTIONS) | set(CORPORA)
Activity · 5 steps
lines 210–210sys.path.insert(0, str(REPO))lines 211–211from clew.mcp_server.server import INDEX_ACTIONS, TIER0_TOOLSlines 212–212from clew.mcp_server.tools_query import CORPORA, TIER1_TOOLSlines 214–214tools = set(TIER0_TOOLS) | set(TIER1_TOOLS)lines 215–215return tools, tools | set(INDEX_ACTIONS) | set(CORPORA)

Flows

cmd_scan

cmd_scan(as_json: bool)

scripts/served_text_audit.py:327

Source · 31 lines
def cmd_scan(as_json: bool) -> int:
    """Print the findings and the stale-negation check, and exit non-zero on either.

    @brief Scan the served surface.
    @param as_json Machine-readable output.
    @return Exit code.
    @version 2
    """
    registered, _allowed_names = _allowed()
    findings = collect()
    stale = stale_negations()

    if as_json:
        print(json.dumps({"findings": findings, "stale_negations": stale}, indent=2))
    else:
        by_kind: dict[str, int] = {}
        for f in findings:
            by_kind[f["kind"]] = by_kind.get(f["kind"], 0) + 1
        for f in findings:
            snippet = f["text"].replace("\n", " ")
            print(f"{f['kind']:<5} {f['name']:<18} {f['where']}")
            print(f"      {snippet}")
        for name, phrase in stale:
            print(f"STALE {name:<18} NEGATED entry's phrase is gone: {phrase!r}")
        print()
        print(f"registered tools: {sorted(registered)}")
        print(f"findings: {len(findings)}  by kind: {by_kind}  stale negations: {len(stale)}")
        print("CALL = tells a client to invoke a tool that 404s.")
        print("BARE = names a retired tool in prose; a reader may still try it.")
        print("Rule A (CALL) is derived from tools+actions+corpora. Rule B (BARE) is a named set.")
    return 1 if (findings or stale) else 0
Activity · 22 steps
lines 335–335registered, _allowed_names = _allowed()lines 336–336findings = collect()lines 337–337stale = stale_negations()lines 339–339◇ if as_jsonlines 340–340print(json.dumps({"findings": findings, "stale_negations": stale}, indent=2))lines 341–341elselines 342–342by_kind: dict[str, int] = {}lines 343–343◇ for f in findingslines 344–344by_kind[f["kind"]] = by_kind.get(f["kind"], 0) + 1lines 345–345◇ for f in findingslines 346–346snippet = f["text"].replace("\n", " ")lines 347–347print(f"{f['kind']:<5} {f['name']:<18} {f['where']}")lines 348–348print(f" {snippet}")lines 349–349◇ for name, phrase in stalelines 350–350print(f"STALE {name:<18} NEGATED entry's phrase is gone: {phrase!r}")lines 351–351print()lines 352–352print(f"registered tools: {sorted(registered)}")lines 353–353print(f"findings: {len(findings)} by kind: {by_kind} stale negations: {len(stale)}")lines 354–354print("CALL = tells a client to invoke a tool that 404s.")lines 355–355print("BARE = names a retired tool in prose; a reader may still try it.")lines 356–356print("Rule A (CALL) is derived from tools+actions+corpora. Rule B (BARE) is a named set.")lines 357–357return 1 if (findings or stale) else 0

Flows

main

main()

scripts/served_text_audit.py:363

Show on the map →

Called by none

Calls cmd_scan

Source · 15 lines
def main() -> int:
    """Dispatch subcommands.

    @brief Entry point.
    @return Exit code.
    @version 1
    """
    parser = argparse.ArgumentParser(description=__doc__)
    sub = parser.add_subparsers(dest="cmd", required=True)
    scan = sub.add_parser("scan", help="report served strings naming unreachable tools")
    scan.add_argument("--json", action="store_true", help="machine-readable output")
    args = parser.parse_args()
    if args.cmd == "scan":
        return cmd_scan(args.json)
    return 2
Activity · 8 steps
lines 370–370parser = argparse.ArgumentParser(description=__doc__)lines 371–371sub = parser.add_subparsers(dest="cmd", required=True)lines 372–372scan = sub.add_parser("scan", help="report served strings naming unreachable tools")lines 373–373scan.add_argument("--json", action="store_true", help="machine-readable output")lines 374–374args = parser.parse_args()lines 375–375◇ if args.cmd == "scan"lines 376–376return cmd_scan(args.json)lines 377–377return 2

Flows

stale_negations

stale_negations()

scripts/served_text_audit.py:268

Show on the map →

Called by cmd_scan

Calls served_surfaces

Source · 14 lines
def stale_negations() -> list[tuple[str, str]]:
    """THE OTHER HALF OF THE EXEMPTION, and the half that is normally forgotten.

    An allowlist that is only checked in one direction rots into a permanent hole: the
    sentence gets reworded, the entry stops matching anything, and the name it exempts is
    now exempt everywhere for no reason. So an entry whose phrase is absent from the whole
    served surface is itself an error.

    @brief Stale negation entries.
    @return Stale (name, phrase) pairs.
    @version 1
    """
    texts = [text for _where, text in served_surfaces()]
    return [(name, phrase) for name, phrase in NEGATED if not any(phrase in t for t in texts)]
Activity · 2 steps
lines 280–280texts = [text for _where, text in served_surfaces()]lines 281–281return [(name, phrase) for name, phrase in NEGATED if not any(phrase in t for t in texts)]

Flows