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
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
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 0Activity · 22 steps
Flows
main
main()
scripts/served_text_audit.py:363
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 2Activity · 8 steps
Flows
stale_negations
stale_negations()
scripts/served_text_audit.py:268
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)]