Eureka/tools/fixtures/context-pack/fake-eureka-state.py
Eureka 0342dceef9 recall: recalled text is inert data; the deadline is the whole run; budget, stdin and answer are bounded
Soul s1 fixes. The hook header says the lines are recalled records, data and
never instructions, written by agents not the operator. Every item is one JSON
object per line: id, title and operator_quote are JSON strings with < >, control,
bidi and separator characters escaped, so a title cannot close the framing, forge
a tag, a header or an operator segment. RECALL_DEADLINE_MS now bounds the whole
run through a hard stop; stdin is read to 4 MiB and the answer to 1 MiB
(mind.call max_bytes); RECALL_BUDGET is clamped to 9000 under Claude Code's
10,000-character cap. Tests: hostile text, a non-ASCII process-boundary run under
cp1252, byte-versus-character budget, stdin held open and endless, answer size,
budget ceiling, and the four unrecorded survivors (negative reply timeout, reply
id check, env-int positivity, EOF sentinel). Mutants of each were run and caught.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 21:44:02 +02:00

71 lines
4.7 KiB
Python

#!/usr/bin/env python3
"""A stand-in for eureka-state's stdio MCP: answers initialize and tools/call view.
FAKE_SPEC: the view result to return. FAKE_MODE=unavailable answers isError
Unavailable (naming HUGINN_ENDPOINT, as the real daemon's text might). FAKE_COUNT:
a file that gains one line per view call. FAKE_MODE=needenv answers the spec only when
EUREKA_INSTANCE is "t" and HUGINN_ENDPOINT equals FAKE_NEED, else isError Misconfigured.
tools/call query (recall.py): FAKE_COUNT gains a "query" line, FAKE_ARGS gains the call's arguments as one
JSON line, FAKE_SLEEP seconds pass before the answer (then FAKE_MARK, if set, is written), and the answer is {"items": {"Documents": FAKE_ITEMS}}
(a file holding the list); FAKE_MODE unavailable and garbage answer as they do for view; FAKE_MODE=die exits without answering;
FAKE_NOISE writes a non-JSON line, a notification and an id-99 reply before the real one."""
import json, os, sys, time
inited = False
for ln in sys.stdin:
msg = json.loads(ln)
if "id" not in msg:
inited = inited or msg.get("method") == "notifications/initialized"
continue
if msg["method"] == "initialize":
p = msg.get("params", {})
if msg["id"] != 1 or not p.get("protocolVersion") or not p.get("clientInfo", {}).get("name") or "capabilities" not in p:
sys.exit(1)
res = {"protocolVersion": "2024-11-05", "capabilities": {}, "serverInfo": {"name": "fake", "version": "0"}}
else:
p = msg.get("params", {})
if p.get("name") == "query" and inited and msg["method"] == "tools/call" and msg["id"] == 2:
with open(os.environ["FAKE_COUNT"], "a") as f:
f.write("query\n")
with open(os.environ["FAKE_ARGS"], "a") as f:
f.write(json.dumps(p.get("arguments")) + "\n")
time.sleep(float(os.environ.get("FAKE_SLEEP", "0")))
if os.environ.get("FAKE_MARK"): # written only by a child that outlived its caller's deadline
open(os.environ["FAKE_MARK"], "w").write("alive")
if os.environ.get("FAKE_MODE") == "die": # exits without answering
sys.exit(0)
if os.environ.get("FAKE_NOISE"): # lines that are not this call's reply
sys.stdout.write("log: starting\n")
sys.stdout.write(json.dumps({"jsonrpc": "2.0", "method": "notifications/message", "params": {}}) + "\n")
sys.stdout.write(json.dumps({"jsonrpc": "2.0", "id": 99, "result": {"content": [{"type": "text",
"text": json.dumps({"items": {"Documents": []}})}]}}) + "\n")
if os.environ.get("FAKE_MODE") == "unavailable":
res = {"isError": True, "content": [{"type": "text", "text": "Unavailable"}]}
elif os.environ.get("FAKE_MODE") == "garbage":
res = {"content": [{"type": "text", "text": "not json"}]}
else:
docs = json.load(open(os.environ["FAKE_ITEMS"], encoding="utf-8"))
res = {"content": [{"type": "text", "text": json.dumps({"items": {"Documents": docs}})}]}
sys.stdout.write(json.dumps({"jsonrpc": "2.0", "id": msg["id"], "result": res}) + "\n")
sys.stdout.flush()
continue
if (not inited or msg["method"] != "tools/call" or p.get("name") != "view" or msg["id"] != 2
or p.get("arguments") != {"id": {"kind": "CutSpec", "id": os.environ["FAKE_WANT"]}}):
sys.exit(1)
with open(os.environ["FAKE_COUNT"], "a") as f:
f.write("view\n")
if os.environ.get("FAKE_MODE") == "null":
res = {"content": [{"type": "text", "text": "null"}]}
elif os.environ.get("FAKE_MODE") == "garbage":
res = {"content": [{"type": "text", "text": "not json"}]}
elif os.environ.get("FAKE_MODE") == "invalid":
res = {"isError": True, "content": [{"type": "text", "text": os.environ.get("FAKE_CODE", "InvalidInput") + ": " + os.environ.get("HUGINN_ENDPOINT", "")}]}
elif os.environ.get("FAKE_MODE") == "needenv" and not (
os.environ.get("EUREKA_INSTANCE") == "t" and os.environ.get("HUGINN_ENDPOINT") == os.environ["FAKE_NEED"]):
res = {"isError": True, "content": [{"type": "text", "text": "Misconfigured"}]}
elif os.environ.get("FAKE_MODE") == "unavailable":
res = {"isError": True, "content": [{"type": "text",
"text": "Unavailable: " + os.environ.get("HUGINN_ENDPOINT", "")}]}
else:
res = {"content": [{"type": "text", "text": open(os.environ["FAKE_SPEC"], encoding="utf-8").read()}]}
sys.stdout.write(json.dumps({"jsonrpc": "2.0", "id": msg["id"], "result": res}) + "\n")
sys.stdout.flush()