evidence-agent / controller_exchange.py
Spencer Walker
Publish evaluated controller agent and reproducible source evidence
90cf215 verified
Raw History Blame Contribute Delete
1.35 kB
"""One-shot IPC helper: send a model/tool response and read the next request."""
import json
import sys
import time
from pathlib import Path
sys.stdout.reconfigure(encoding="utf-8")
sys.stdin.reconfigure(encoding="utf-8")
roots = {"model": Path("controller_mailbox"), "web": Path("controller_mailbox/web")}
def read(path):
try:
return json.loads(path.read_text(encoding="utf-8"))
except (OSError, ValueError):
return {}
old = {key: read(root / "request.json").get("request_id") for key, root in roots.items()}
payload = json.load(sys.stdin)
channel = payload.pop("channel")
target = roots[channel] / "response.json"
temporary = target.with_suffix(".tmp")
temporary.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
temporary.replace(target)
deadline = time.monotonic() + 15
while time.monotonic() < deadline:
for key, root in roots.items():
request = read(root / "request.json")
if request.get("request_id") and request["request_id"] != old[key]:
request.pop("tools", None)
if "messages" in request:
request["messages"] = request["messages"][1:]
print(json.dumps({"channel": key, **request}, ensure_ascii=False))
raise SystemExit(0)
time.sleep(0.1)
print("Response delivered; next request is not ready yet.")