Files
alzeo/tests/test_needle_bounds.py
T
Hasan BasbunarandClaude Opus 5.5 5e60c2d485 needle.sh: its wait and its reply's length are its caller's
A request was cut after 900 s, where a prompt of 960,000 tokens is read for 1,461 s on the Inkling 2 Mini lane, and a reply
after 40 tokens, where a model that reasons first spends them before it answers: both read as a retrieval missed. --timeout
and --reply-tokens set them, and what was cut is said so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 08:55:17 +01:00

96 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""needle.sh's two bounds are its caller's (2026-12-01): a request was cut after 900 s, where a prompt of 960,000 tokens is
read for 1,461 s on the Inkling 2 Mini lane, and a reply after 40 tokens, where a model that reasons first spends them
before it answers. Both read as a retrieval missed. Run against an engine of this test's own, which finds the passphrase
in the prompt it is given and answers it when the reply is long enough for its reasoning."""
import http.server
import json
import os
import re
import shutil
import subprocess
import tempfile
import threading
import time
import unittest
from pathlib import Path
NEEDLE = Path(__file__).resolve().parents[1] / "needle.sh"
class Engine(http.server.BaseHTTPRequestHandler):
reasons, wait, asked = 0, 0.0, []
def log_message(self, *a):
pass
def do_POST(self):
body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
text, room = body["messages"][0]["content"], body["max_tokens"]
found = re.search(r"the secret passphrase is ([\w-]+)\.", text)
if found:
Engine.asked.append(room)
time.sleep(Engine.wait)
cut = room <= Engine.reasons
content = "" if cut else (found.group(1) if found else "OK")
out = json.dumps({"choices": [{"message": {"content": content}, "finish_reason": "length" if cut else "stop"}],
"usage": {"prompt_tokens": len(text) // 4}}).encode()
try:
self.send_response(200)
self.send_header("Content-Length", str(len(out)))
self.end_headers()
self.wfile.write(out)
except OSError:
pass
class ItsBounds(unittest.TestCase):
def setUp(self):
self.home = tempfile.mkdtemp(prefix="needle-bounds-")
self.addCleanup(shutil.rmtree, self.home, ignore_errors=True)
(Path(self.home) / ".config/qwen38").mkdir(parents=True)
(Path(self.home) / ".config/qwen38/api-key").write_text("test-key\n")
self.srv = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Engine)
threading.Thread(target=self.srv.serve_forever, daemon=True).start()
self.addCleanup(self.srv.server_close)
self.addCleanup(self.srv.shutdown)
Engine.reasons, Engine.wait, Engine.asked = 0, 0.0, []
def run_needle(self, *args):
r = subprocess.run(["bash", str(NEEDLE), "--model", "m", "--trials", "1", "--depths", "1000", "--no-flush", *args],
capture_output=True, text=True, timeout=120,
env={**os.environ, "PORT": str(self.srv.server_port), "HOME": self.home})
return r.returncode, r.stdout + r.stderr
def test_as_before_by_default(self):
rc, out = self.run_needle()
self.assertEqual((rc, Engine.asked), (0, [40]), out)
self.assertIn("NEEDLE SUMMARY: 1/1 exact retrievals", out)
def test_a_reply_cut_in_its_reasoning_is_said_cut_and_not_missed(self):
Engine.reasons = 500
rc, out = self.run_needle()
self.assertEqual(rc, 1, out)
self.assertIn("-> CUT at 40 tokens, see --reply-tokens", out)
self.assertNotIn("MISS", out)
rc, out = self.run_needle("--reply-tokens", "4096")
self.assertEqual((rc, Engine.asked[-1]), (0, 4096), out)
def test_a_prompt_read_for_longer_than_the_bound_is_said_so(self):
Engine.wait = 3
rc, out = self.run_needle("--timeout", "1")
self.assertEqual(rc, 1, out)
self.assertIn("(no answer in 1 s, see --timeout)", out)
rc, out = self.run_needle("--timeout", "30")
self.assertEqual(rc, 0, out)
def test_what_is_no_number_is_refused(self):
for args in (("--timeout", "x"), ("--reply-tokens", "-1"), ("--timeout", "1.5")):
rc, out = self.run_needle(*args)
self.assertEqual(rc, 2, out)
self.assertIn("take whole numbers", out)
if __name__ == "__main__":
unittest.main()