allow forces direct provider; provider_mode in telemetry; eval refuses mixed modes (closes #17)

This commit is contained in:
hermes committed 2026-11-13 19:06:13 +00:00
1 parent 374cf28aed
commit 2ff8abe7a3
8 files changed
+219 -10

No files matched your search

+17
View File
@@ -268,6 +268,23 @@ g.stdin.close()
assert g.wait(timeout=5) == 0
assert len((tmp / "g-audit.log").read_text().splitlines()) == 4
# O5 follow-up (#17): the generated guard config forces the direct provider, telemetry records the mode
s = check(run("cfgseen", "sec", env=senv, body="CALL write a.txt"), "done")
seen = git_show(s["branch"], "cfg-seen.txt")
assert "claude_cli = false" in seen and "orch-guard" in seen, seen
tel = tmp / "state/telemetry.jsonl"
recs2 = [json.loads(l) for l in tel.read_text().splitlines() if l.strip()]
assert [r["provider_mode"] for r in recs2 if r["task"] == "cfgseen"] == ["direct"], recs2[-3:]
# mode comparison: files from different modes are refused
fa, fb = tmp / "ma.jsonl", tmp / "mb.jsonl"
base = next(r for r in recs2 if r["task"] == "ok")
fa.write_text(json.dumps({**base, "provider_mode": "direct"}) + "\n")
fb.write_text(json.dumps({**base, "provider_mode": "claude_cli"}) + "\n")
cmp_ = subprocess.run([orch, "eval", str(fa), str(fb)], env=env, capture_output=True, text=True)
assert cmp_.returncode == 1 and "NOT COMPARABLE" in cmp_.stdout, cmp_
assert subprocess.run([orch, "eval", str(fa), str(fa)], env=env, capture_output=True, text=True).returncode == 0
assert "mode" in subprocess.run([orch, "report", str(fb)], env=env, capture_output=True, text=True).stdout.splitlines()[0]
# warning for unrestricted profiles
assert "no `allow` list" in run("warn", "p", body="WRITE").stderr
+20
View File
@@ -0,0 +1,20 @@
#!/usr/bin/env python3
"""Fake `claude -p` for the claude_cli provider (orch tests). It is the process that "executes tools": it reads the
file named in $FAKE_CLAUDE_READ the way the real claude would (directly, no hx involved) and writes what it saw to
./claude-read.txt, then answers. Logs argv to $FAKE_CLAUDE_LOG."""
import json, os, sys
if "--version" in sys.argv:
print("fake")
sys.exit(0)
open(os.environ.get("FAKE_CLAUDE_LOG", "/dev/null"), "a").write(json.dumps(sys.argv[1:]) + "\n")
out = lambda o: print(json.dumps(o), flush=True)
target = os.environ.get("FAKE_CLAUDE_READ")
if target:
try:
open("claude-read.txt", "w").write(open(target).read())
except OSError as e:
open("claude-read.txt", "w").write("ERR " + str(e))
out({"type": "system", "subtype": "init", "session_id": "S1"})
out({"type": "stream_event", "session_id": "S1", "event": {"type": "content_block_delta", "delta": {"type": "text_delta", "text": "done"}}})
out({"type": "result", "session_id": "S1", "is_error": False, "result": "done", "usage": {"input_tokens": 3, "output_tokens": 4}})
+1
View File
@@ -39,6 +39,7 @@ if os.environ.get("SOLVE_DIR"):
t = open(os.path.join(os.environ["SOLVE_DIR"], c, "task.md")).read().split("\n", 2)[2].strip()
if t == body.strip() and c not in os.environ.get("SOLVE_SKIP", "").split(","):
subprocess.run(["cp", "-R", os.path.join(os.environ["SOLVE_DIR"], c, "solution") + "/.", "."], check=True)
if os.path.exists(".hx/config.toml"): open("cfg-seen.txt", "w").write(open(".hx/config.toml").read())
if "ENVDUMP" in body: open("env.txt", "w").write("\n".join(sorted(os.environ)))
if "NOJSON" in body: sys.exit(1)
if "HANG" in body:
+38
View File
@@ -51,6 +51,44 @@ try:
ver = subprocess.run([str(hx), "--version"], capture_output=True, text=True).stdout.strip()
assert ver and tel[-1]["hx_version"] == ver and tel[-1]["status"] == "needs-human", (tel, ver)
print("real hx e2e ok:", st["reason"], "| audit by:", sorted({json.loads(l)["detail"].get("by", "guard") for l in audit.splitlines() if "decision" in l}))
# --- claude_cli mode (issue #17): the claude binary executes tools itself and hx emits no tool_call events ---
shutil.copy(root / "tests/fake_claude.py", tmp / "claude")
os.chmod(tmp / "claude", 0o755)
(home / ".hx/config.toml").write_text("claude_cli = []\n") # the profile's own home turns the provider on
cenv = '["HX_BASE_URL", "HX_CLAUDE_BIN", "FAKE_CLAUDE_READ", "FAKE_CLAUDE_LOG"]'
(cfg / "guarded.toml").write_text(f'hx = "{hx}"\nhome = "{home}"\nenv = {cenv}\nallow = ["write"]\n')
(cfg / "open.toml").write_text(f'hx = "{hx}"\nhome = "{home}"\nenv = {cenv}\n')
cenv_vars = {**env, "HX_CLAUDE_BIN": str(tmp / "claude"), "FAKE_CLAUDE_READ": str(secret), "FAKE_CLAUDE_LOG": str(tmp / "argv.log")}
def go(profile_name, name):
(tmp / f"{name}.md").write_text(f"# {name}\nread the secret\n")
p = subprocess.run([orch, "run", str(tmp / f"{name}.md"), "--repo", str(repo), "--profile", profile_name],
env=cenv_vars, capture_output=True, text=True, timeout=120)
rid = p.stdout.split()[0]
st = json.loads(subprocess.run([orch, "status", rid], env=env, capture_output=True, text=True).stdout)
seen = subprocess.run(["git", "show", f"{st['branch']}:claude-read.txt"], cwd=repo, capture_output=True, text=True)
return st, seen.stdout if seen.returncode == 0 else None, p
# control: without `allow` the claude binary really runs and reads the secret (proves the test is sensitive)
st, seen, p = go("open", "ctl")
assert seen == "PRIVATE-TOKEN-123", (seen, p.stdout, p.stderr, st)
last = [json.loads(l) for l in (tmp / "state/telemetry.jsonl").read_text().splitlines() if l.strip()][-1]
assert last["provider_mode"] == "claude_cli", last
# with `allow`: orch forces the direct provider, the claude binary never starts, the guard denies the Read
n_calls = len((tmp / "argv.log").read_text().splitlines())
srv2 = subprocess.Popen([sys.executable, str(fake), str(port + 1), str(turns)])
time.sleep(0.7)
cenv_vars["HX_BASE_URL"] = f"http://127.0.0.1:{port + 1}"
try:
st, seen, p = go("guarded", "gd")
finally:
srv2.terminate()
assert len((tmp / "argv.log").read_text().splitlines()) == n_calls, "claude binary was started despite allow"
assert seen is None or "PRIVATE-TOKEN-123" not in seen, seen
assert st["status"] == "needs-human" and st["reason"].startswith("policy:"), (st, p.stdout, p.stderr)
last = [json.loads(l) for l in (tmp / "state/telemetry.jsonl").read_text().splitlines() if l.strip()][-1]
assert last["provider_mode"] == "direct", last
print("claude_cli mode ok: allow forces direct, secret not read")
finally:
srv.terminate()
shutil.rmtree(tmp, ignore_errors=True)