Milestone 8: the loop detector alerts when test failures repeat, budget caps limit context/cost/wall-time per session with compaction support, and live control lets you interrupt, message, or kill a running session from the TUI. Decisions can now be vetoed, which sends hx/message and rewinds the session to code. All 82 tests pass. - src/budget.rs: pure functions for active time, budgets and loop detect - src/runner.rs: Ctl control object polls for commands ~100ms, handles interrupt/message/abort/stop at every blocking point, re-runs on hints, and compacts when context is high - src/db.rs: commands table for TUI→runner control - src/detail.rs, src/goal.rs, src/tui/: loop detector box, budget caps, session keys (i/m/s/k/r), goal keys (v for veto), state forcing - tests/fake_acp_hx.py: slow mode and hx/message, hx/compact log - examples/machines/task.toml: [budget] with wall_secs, cost, compact_at Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
117 lines
5.4 KiB
Python
117 lines
5.4 KiB
Python
"""Fake `hx serve --acp --cwd DIR --session ID`: newline-delimited JSON-RPC 2.0 on stdio.
|
|
|
|
Behaviour per `hx/step`, chosen with FAKE_MODE (orch passes FAKE_* through the profile's `env`):
|
|
ok intent, tool call, edit, usage, decision, plan, then step_result done (default)
|
|
blocked step_result blocked, catch FAKE_CATCH (default PlanInvalid)
|
|
block_once blocked the first time (marker file .fake_blocked in cwd), done afterwards
|
|
needs_input step_result needs_input with a question; finishes when the retried step has a context.hint
|
|
permission asks session/request_permission first and records the answer in permission.txt
|
|
slow works until hx/interrupt (records hx/message and hx/compact in messages.txt / compact.txt), then
|
|
answers done; a retried step with context.hint finishes at once
|
|
crash exits without a step_result
|
|
FAKE_USAGE="ctx_used,ctx_limit,cost" makes every step report that usage first.
|
|
Every received message is appended to FAKE_LOG when set. Each step also appends its prompt kind to steps.txt.
|
|
"""
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
args = sys.argv[1:]
|
|
cwd = args[args.index("--cwd") + 1] if "--cwd" in args else os.getcwd()
|
|
session = args[args.index("--session") + 1] if "--session" in args else "sess-1"
|
|
mode = os.environ.get("FAKE_MODE", "ok")
|
|
log = os.environ.get("FAKE_LOG")
|
|
|
|
|
|
def send(o):
|
|
sys.stdout.write(json.dumps({"jsonrpc": "2.0", **o}) + "\n")
|
|
sys.stdout.flush()
|
|
|
|
|
|
def note(method, params):
|
|
send({"method": method, "params": params})
|
|
|
|
|
|
def recv():
|
|
line = sys.stdin.readline()
|
|
if not line:
|
|
sys.exit(0)
|
|
m = json.loads(line)
|
|
if log:
|
|
with open(log, "a") as f:
|
|
f.write(json.dumps(m) + "\n")
|
|
return m
|
|
|
|
|
|
def step(params):
|
|
kind = params["prompt"]
|
|
with open(os.path.join(cwd, "steps.txt"), "a") as f:
|
|
f.write(kind + "\n")
|
|
if mode == "crash":
|
|
sys.exit(3)
|
|
note("hx/intent", {"text": f"working on {kind}"})
|
|
usage = os.environ.get("FAKE_USAGE") # "ctx_used,ctx_limit,cost"
|
|
if usage:
|
|
used, limit, cost = usage.split(",")
|
|
note("hx/usage", {"ctx_used": int(used), "ctx_limit": int(limit), "cost": float(cost)})
|
|
note("session/update", {"sessionId": session, "update": {"sessionUpdate": "tool_call", "toolCallId": "t1", "title": "edit src/lib.rs", "kind": "edit", "status": "in_progress"}})
|
|
if mode == "permission":
|
|
send({"id": 900, "method": "session/request_permission", "params": {
|
|
"sessionId": session,
|
|
"toolCall": {"toolCallId": "t2", "title": "docker compose up -d redis"},
|
|
"options": [{"optionId": "allow", "name": "Allow once", "kind": "allow_once"}, {"optionId": "deny", "name": "Deny", "kind": "reject_once"}]}})
|
|
while True:
|
|
m = recv()
|
|
if m.get("id") == 900:
|
|
with open(os.path.join(cwd, "permission.txt"), "w") as f:
|
|
f.write(json.dumps(m["result"]))
|
|
break
|
|
# slow keeps working until the orchestrator interrupts it; it records what it is told meanwhile.
|
|
# A retried step (context.hint set, e.g. the user's reply after an interrupt) finishes at once.
|
|
if mode == "slow" and not params.get("context", {}).get("hint"):
|
|
while True:
|
|
m = recv()
|
|
md = m.get("method")
|
|
if md == "hx/message":
|
|
with open(os.path.join(cwd, "messages.txt"), "a") as f:
|
|
f.write(m["params"]["text"] + "\n")
|
|
elif md == "hx/compact":
|
|
with open(os.path.join(cwd, "compact.txt"), "a") as f:
|
|
f.write("compact\n")
|
|
elif md == "hx/interrupt":
|
|
note("hx/step_result", {"status": "done", "summary": "interrupted"})
|
|
return
|
|
# needs_input asks once; with the user's answer in context.hint the same step finishes
|
|
if mode == "needs_input" and not params.get("context", {}).get("hint"):
|
|
note("hx/step_result", {"status": "needs_input", "question": "Which provider?"})
|
|
return
|
|
blocked = mode == "blocked"
|
|
if mode == "block_once" and not os.path.exists(os.path.join(cwd, ".fake_blocked")):
|
|
open(os.path.join(cwd, ".fake_blocked"), "w").close()
|
|
blocked = True
|
|
if blocked:
|
|
note("hx/step_result", {"status": "blocked", "catch": os.environ.get("FAKE_CATCH", "PlanInvalid"), "summary": "cannot continue"})
|
|
return
|
|
with open(os.path.join(cwd, f"agent_{kind}.txt"), "a") as f:
|
|
f.write("work\n")
|
|
note("hx/edit", {"path": f"agent_{kind}.txt", "added": 4, "removed": 1})
|
|
note("hx/usage", {"ctx_used": 40000, "ctx_limit": 200000, "cost": 0.25})
|
|
note("hx/decision", {"text": "token bucket over sliding window", "state": kind, "at": 1})
|
|
if kind == "plan":
|
|
note("hx/plan", {"steps": ["write the limiter", "add tests"], "files": ["src/limiter.rs"]})
|
|
note("hx/step_result", {"status": "done", "summary": f"{kind} finished"})
|
|
|
|
|
|
while True:
|
|
m = recv()
|
|
method, mid = m.get("method"), m.get("id")
|
|
if method == "initialize":
|
|
send({"id": mid, "result": {"protocolVersion": 1, "agentCapabilities": {}}})
|
|
elif method == "session/new":
|
|
send({"id": mid, "result": {"sessionId": session}})
|
|
elif method == "hx/step":
|
|
send({"id": mid, "result": {}}) # ack; the outcome comes as hx/step_result
|
|
step(m["params"])
|
|
elif mid is not None:
|
|
send({"id": mid, "error": {"code": -32601, "message": "unknown method"}})
|