Files
alzeo/tests/test_needle_refusal.py
Hasan BasbunarandClaude Opus 5.5 d163cc4c1f tests: thirteen that could not fail now see the defect they are named for
Each was reproduced by a mutant that left the suite green, and now fails on it:
- the uninstall inventory read the last assignment of each pin (the help text's moving
  tag, a later "$CUR_DRAFT"), matched images by substring and units and containers by
  presence anywhere in the file: it reads the pins, matches whole entries, and checks the
  disable, rm and docker rm lines themselves, the flash launcher's container included;
- the fake systemctl printed an epoch whatever it was asked, so a dropped
  --timestamp=unix went unseen;
- the native-restore guard's fixture carried a --context-length no native unit has, and
  its exit code was thrown away, so a dropped `|| true` stayed green;
- the 27B opencode block's own numbers were the table's, so which one was used could not
  be seen;
- needle.sh's 1/1 pass tolerated rc 1;
- the System One cancel was set before the call, so a check above the slot wait passed;
- the image lane's root refusal was a substring, the cockpit step's wiring too: both run
  now, the step against stub installers under install.sh's own shell options;
- job pruning was called by a name that did not exist, behind a hasattr;
- the audit test never read the argv it is named for;
- "importing binds nothing" looked for a name nothing defines: it measures threads and
  sockets around a fresh import;
- the static directory's containment was a string prefix, so a sibling named static.bak/
  was served before the session check: it compares path components, with a test.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 18:22:38 +02:00

96 lines
4.3 KiB
Python

#!/usr/bin/env python3
"""needle.sh must not report a lane's own prompt limit as a retrieval failure.
needle.sh talks to the keepalive proxy by default, so the oversize guard and the
lane's PROMPT_CEILING_TOKENS apply to it. Its default depths run to 140000 while
the flash lane's ceiling is 128000, so the tool used to print ERROR for a prompt
the box had correctly refused and exit 1, reading as long-context corruption.
A fake engine here answers small prompts and refuses big ones exactly as the
proxy does."""
import http.server
import json
import os
import shutil
import subprocess
import sys
import tempfile
import threading
REPO_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
NEEDLE = os.path.join(REPO_DIR, "needle.sh")
BIG = 400000 # bytes of request body above which the fake refuses (calibration is ~210 KB)
class Fake(http.server.BaseHTTPRequestHandler):
def log_message(self, *a):
pass
def _send(self, code, obj):
raw = json.dumps(obj).encode()
self.send_response(code)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(raw)))
self.end_headers()
self.wfile.write(raw)
def do_POST(self):
n = int(self.headers.get("Content-Length") or 0)
body = self.rfile.read(n)
if self.path.startswith("/flush_cache"):
return self._send(200, {"ok": True})
if len(body) > BIG:
return self._send(400, {"error": {
"type": "context_too_long",
"message": "keepalive-proxy: 190000 prompt tokens; this lane serves at most 128000"}})
text = json.loads(body)["messages"][0]["content"]
pw = ""
if "passphrase is " in text:
pw = text.split("passphrase is ", 1)[1].split(".", 1)[0].strip()
return self._send(200, {"choices": [{"message": {"content": pw or "OK"}}],
"usage": {"prompt_tokens": max(40, len(text) // 4 + 40)}})
def main() -> None:
srv = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Fake)
threading.Thread(target=srv.serve_forever, daemon=True).start()
port = srv.server_address[1]
# needle.sh reads the API key from $HOME/.config/qwen38/api-key. Relying on
# the developer's own key made this pass here and fail on a runner that has
# none, so the test brings its own HOME.
home = tempfile.mkdtemp()
os.makedirs(os.path.join(home, ".config", "qwen38"))
with open(os.path.join(home, ".config", "qwen38", "api-key"), "w") as f:
f.write("test-key\n")
env = {**os.environ, "PORT": str(port), "HOME": home}
try:
# A depth the fake refuses, mixed with one it serves.
r = subprocess.run(["bash", NEEDLE, "--model", "m", "--depths", "1000 300000",
"--trials", "1", "--no-flush"],
capture_output=True, text=True, env=env, timeout=180)
out = r.stdout + r.stderr
assert "REFUSED by the lane" in out, f"a policy refusal was not labelled:\n{out}"
assert "ERROR" not in out, f"a policy refusal was reported as an error:\n{out}"
# the refused trial must not be counted as an attempted retrieval
assert "1/1 exact retrievals" in out, f"the ratio counted the refusal:\n{out}"
assert "refused by the lane's own prompt limit" in out, out
assert "PORT=30000" in out, "the way past the ceiling is not explained"
# the one retrieval attempted succeeded, so this is a pass: tolerating 1 here let a
# count of the refused trials as failures through (found in review, 2026-09-24)
assert r.returncode == 0, f"1/1 retrieved must exit 0, got rc={r.returncode}\n{out}"
# Every depth refused: nothing was measured, and that is not a pass.
r = subprocess.run(["bash", NEEDLE, "--model", "m", "--depths", "300000",
"--trials", "1", "--no-flush"],
capture_output=True, text=True, env=env, timeout=180)
out = r.stdout + r.stderr
assert r.returncode == 2, f"all-refused must not exit 0/1, got {r.returncode}\n{out}"
assert "nothing was measured" in out, out
print("test_needle_refusal: OK")
finally:
srv.shutdown()
shutil.rmtree(home, ignore_errors=True)
if __name__ == "__main__":
sys.exit(main())