Artifact e5edb324bb1c907f1a4f2691605af657a152fe43223eece890ce01321d06ff64:
- File
eval/test_eval.py
— part of check-in
[b70296c566]
at
2026-11-10 18:10:01
on branch rules
— Keep standing rules: ctx remember, ctx rules, and CLAUDE.md and memory files imported
A rule is an instruction for every future session. Each is a technote in the private repository tagged rule, so every version is kept and rules sync. Its header records key, project, source file, origin session, until date, and status with the rule that replaced it and why.
ctx remember TEXT [--project P] [--until DATE] adds one. ctx rules lists those in force for this project and every project; --all shows retired, replaced and expired ones; ctx rules retire KEY --reason WHY [--by KEY] retires or replaces. Every scan imports ~/.claude/CLAUDE.md, each project's CLAUDE.md and AGENTS.md, and Claude Code's memory files, redacted as transcripts are: a changed file makes a new version, a removed one retires its rule. A memory file's project is the one its neighbouring transcripts were archived under, then the session it names, then the folder its name stands for, decoded against the disk.
Rules are indexed from the technotes as their own kind, newest technote per key, and search returns those in force for this project and every project. Technotes tagged rule are never indexed as sessions. The derived schema version rises to 3, so the index rebuilds once.
On the real archive: 507 rules from memory files and CLAUDE.md files, imported in about a second.
eval: --via FILE routes every model call through the env of a Claude Code settings file, such as a proxy to cheaper models; --yolo runs answers without permission checks. Each run records its route and the commands it was denied; compare refuses runs with different models, routes or permissions. The test suite now never reads the real Claude config. (user: dmestas, size: 6544) [annotate] [blame] [check-ins using] [more...]
#!/usr/bin/env python3 """Tests skill_eval end to end with a stand-in for the model. usage: test_eval.py CTX""" import json import os import re import subprocess import sys import tempfile HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) os.environ["SKILL_EVAL_HOME"] = tempfile.mkdtemp() import skill_eval as se # noqa: E402 assert se.sign_p(3, 0) == 0.25 and se.sign_p(0, 0) == 1.0 assert se.set_of("a") == se.set_of("a") assert 20 < sum(se.set_of(str(i)) == "holdout" for i in range(100)) < 50, "about a third held out" assert "ctx" not in se.mask("ran ctx search on x.jsonl, see #1234") assert se.breaches([("Bash", "ls ~/.claude/projects/-Users-me-p", False)], "-work") assert not se.breaches([("Bash", "ls ~/.claude/projects/-Users-me-p", True)], "-work"), "a refusal is no breach" assert not se.breaches([("Glob", '{"path": "/h/.claude/projects/-work-A"}', False)], "-work"), "its own folder" sys.dont_write_bytecode = True body = 'we ran ctx and it worked\nBash {"command":"cd x; ~/.local/bin/ctx search a b | head -3"}\nBash {"command":"grep ctx file"}\nBash {"command":"A=1 ctx show 5"}' assert se.ctx_calls(body) == [("search", ["a", "b"]), ("show", ["5"])], se.ctx_calls(body) code = json.dumps({"command": "cat > x.go <<'EOF'\nctx context.Context\nfunc f(ctx interface{}) {}\nEOF\nctx turn 5"}) assert se.ctx_calls("Bash " + code) == [("turn", ["5"])], se.ctx_calls("Bash " + code) assert se.ctx_calls('Bash {"command":"go test -run X(ctx context.Context)"}') == [] assert not se.breaches([("Bash", "cd /h/.ctx/eval/work/x/repos/C/p", False)], "-work"), "its own workspace" assert se.breaches([("Read", "/h/.ctx/ctx.sqlite", False)], "-work"), "the live archive" T = tempfile.mkdtemp() LOG = f"{T}/log" FAKE = f"{T}/fake" with open(FAKE, "w") as f: f.write(f"""#!{sys.executable} import json, os, re, sys a = sys.argv prompt = a[a.index("-p") + 1] fmt = a[a.index("--output-format") + 1] if "BLIND-GRADE" in prompt: answers = json.loads(prompt[prompt.index("ANSWERS:") + 8:]) print(json.dumps({{"result": json.dumps([{{"answer_id": x["answer_id"], "grade": "correct" if "good" in x["answer"] else "wrong"}} for x in answers])}})) sys.exit(0) with open({LOG!r}, "a") as f: f.write(json.dumps(dict(skill=os.path.exists(".claude/skills/ctx/SKILL.md"), db=os.environ.get("CTX_DB"), persist="--no-session-persistence" in a, hooks="disableAllHooks" in a[a.index("--settings") + 1])) + "\\n") print(json.dumps({{"type": "assistant", "message": {{"content": [{{"type": "tool_use", "id": "1", "name": "Bash", "input": {{"command": "ctx search thing"}}}}]}}}})) print(json.dumps({{"type": "result", "subtype": "success", "result": os.environ.get("ANSWER", "good answer"), "num_turns": 2, "duration_ms": 1000, "total_cost_usd": 0.01, "permission_denials": [], "usage": {{"input_tokens": 10, "output_tokens": 5, "cache_creation_input_tokens": int(os.environ.get("TOKENS", "100")), "cache_read_input_tokens": 7}}}})) """) os.chmod(FAKE, 0o755) os.makedirs(f"{T}/repo/proj") open(f"{T}/repo/proj/code.txt", "w").write("x\n") skill = f"{T}/SKILL.md" open(skill, "w").write("skill text\n") se.write_json(se.BANK, {"projects": {"proj": {"repo": f"{T}/repo/proj", "sessions": f"{T}/repo/proj"}}, "questions": [{"id": i, "project": "proj", "kind": "fix", "question": f"what about {i}?", "answer": "it", "must_include": ["it"]} for i in ("a", "b", "c")]}) env = dict(os.environ, SKILL_EVAL_CLAUDE=FAKE, CTX=os.path.abspath(sys.argv[1])) def cli(*args, ok=True, **extra): p = subprocess.run([sys.executable, f"{HERE}/skill_eval.py", *args], env=dict(env, **extra), capture_output=True, text=True) assert (p.returncode == 0) == ok, f"{args}: exit {p.returncode}\n{p.stderr}{p.stdout}" return p.stdout good = cli("run", "--set", "all", "--cond", "A,C", "--skill", skill, "--name", "good", "--jobs", "2").strip() seen = [json.loads(x) for x in open(LOG)] assert len(seen) == 6 and all(x["persist"] and x["hooks"] for x in seen), "isolated" assert sum(x["skill"] for x in seen) == 3 and sum(bool(x["db"]) for x in seen) == 3, "only C gets the skill and archive" assert not any(x["db"] and "/snap.sqlite" not in x["db"] for x in seen), "C reads the frozen copy" assert not os.listdir(f"{se.HOME}/work"), "the workspace is removed" cli("grade", good) report = cli("report", good) assert re.search(r"C: score 3\.0/3", report), report bad = cli("run", "--set", "all", "--skill", skill, "--name", "bad", ANSWER="poor answer").strip() cost = cli("run", "--set", "all", "--skill", skill, "--name", "cost", TOKENS="500").strip() assert "FAIL: score dropped" in cli("compare", good, bad, ok=False) assert "PASS" in cli("compare", bad, good) assert "tokens rose" in cli("compare", good, cost, ok=False) assert "ctx commands by agents" in cli("usage") via = f"{T}/via.json" json.dump({"env": {"ANTHROPIC_BASE_URL": "http://127.0.0.1:1", "ANTHROPIC_DEFAULT_SONNET_MODEL": "cheap", "ANTHROPIC_AUTH_TOKEN": "s3cret"}}, open(via, "w")) routed = subprocess.run([sys.executable, f"{HERE}/skill_eval.py", "--via", via, "--yolo", "run", "--set", "all", "--skill", skill, "--name", "routed"], env=env, capture_output=True, text=True).stdout.strip() meta = json.load(open(f"{routed}/meta.json")) assert meta["yolo"] and meta["via"]["ANTHROPIC_DEFAULT_SONNET_MODEL"] == "cheap", meta assert "s3cret" not in open(f"{routed}/meta.json").read(), "a token is never recorded" mixed = subprocess.run([sys.executable, f"{HERE}/skill_eval.py", "compare", good, routed], env=env, capture_output=True, text=True) assert mixed.returncode and "different models" in mixed.stderr, mixed.stderr known = se.known_commands(os.path.abspath(sys.argv[1])) assert {"wiki", "source", "project", "index", "sync", "remote", "rebuild", "search", "turn"} <= known, known assert "nonsense" not in known # Documents: S and W get the same tools; only W can find the wiki. sw = cli("run", "--set", "all", "--cond", "S,W", "--skill", skill, "--name", "docs").strip() rows = json.load(open(f"{sw}/runs.json")) assert sorted(r["cond"] for r in rows) == ["S", "S", "S", "W", "W", "W"], rows ri = cli("run", "--set", "all", "--cond", "I", "--skill", skill, "--name", "inj").strip() assert all(r["cond"] == "I" and "injected" in r for r in json.load(open(f"{ri}/runs.json"))) assert "--bank" in subprocess.run([sys.executable, f"{HERE}/skill_eval.py", "-h"], capture_output=True, text=True).stdout print("eval tests passed")