Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

repl: a scripted repl waits for the background work its turn started

justrach048919309e18
diff --git a/build.zig b/build.zig
index 8a1b8e65..b6519aab 100644
--- a/build.zig
+++ b/build.zig
@@ -190,6 +190,9 @@ pub fn build(b: *std.Build) void {
const acp_background_child_test = b.addSystemCommand(&.{ "python3", "scripts/test-acp-background-subagent.py" });
acp_background_child_test.addArtifactArg(exe);
test_step.dependOn(&acp_background_child_test.step);
+ const repl_background_wait_test = b.addSystemCommand(&.{ "python3", "scripts/test-repl-background-wait.py" });
+ repl_background_wait_test.addArtifactArg(exe);
+ test_step.dependOn(&repl_background_wait_test.step);
const workflow_isolation_test = b.addSystemCommand(&.{ "python3", "scripts/test-workflow-isolation.py" });
workflow_isolation_test.addArtifactArg(exe);
test_step.dependOn(&workflow_isolation_test.step);
diff --git a/scripts/test-repl-background-wait.py b/scripts/test-repl-background-wait.py
new file mode 100755
index 00000000..87bd9145
--- /dev/null
+++ b/scripts/test-repl-background-wait.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python3
+"""Offline: a scripted `graff repl` waits for the background subagent its turn
+started (ADR 0215). The child answers only after the root has ended its reply,
+so its report can reach the root only through the idle wait; before the fix the
+script hit EOF and graff exited with the child still running."""
+import json
+import os
+import subprocess
+import sys
+import tempfile
+import threading
+import time
+from pathlib import Path
+
+REPO = Path(__file__).resolve().parents[1]
+sys.path.insert(0, str(REPO / "scripts/eval"))
+from mock_model import ScriptedModel
+
+PROMPT = "Start the background child."
+REPORT = "child report: the answer is 42"
+DONE = "Done: the child says 42."
+
+
+class Model(ScriptedModel):
+ def __init__(self):
+ super().__init__([], exhausted_text="unexpected model request")
+ self.root_bodies = []
+ self.root_ended = threading.Event()
+
+ def next_reply(self, body):
+ text = json.dumps(body.get("messages", []))
+ with self._lock:
+ self.requests.append(body)
+ root = PROMPT in text
+ if root:
+ self.root_bodies.append(body)
+ n = len(self.root_bodies)
+ if not body.get("tools"):
+ return {"text": "Short title"}
+ if not root:
+ assert self.root_ended.wait(30), "the root never ended its reply"
+ time.sleep(0.5)
+ return {"text": REPORT}
+ if n == 1:
+ return {"tool": "subagent", "arguments": {
+ "description": "Background child", "prompt": "Report the answer.",
+ "isolation": "shared_cwd", "run_in_background": True}}
+ if n == 2:
+ self.root_ended.set()
+ return {"text": "Waiting for the child to report."}
+ return {"text": DONE}
+
+
+def main():
+ binary = (Path(sys.argv[1]) if len(sys.argv) > 1 else REPO / "zig-out/bin/graff").resolve()
+ model = Model()
+ port = model.start(0)
+ try:
+ with tempfile.TemporaryDirectory() as tmp:
+ home = Path(tmp) / "home"
+ work = Path(tmp) / "work"
+ home.mkdir()
+ work.mkdir()
+ env = {k: v for k, v in os.environ.items() if not k.endswith("_API_KEY") and not k.startswith(("GRAFF_", "HARNESS_"))}
+ env.update(HOME=str(home), AI_GATEWAY_API_KEY="local", GRAFF_FLEET="off",
+ GRAFF_VERCEL_URL=f"http://127.0.0.1:{port}/v1/chat/completions",
+ GRAFF_NO_TELEMETRY="1", GRAFF_NO_SMOLIFY="1", GRAFF_BEHAVIOR_UPLOAD="off", NO_COLOR="1")
+ proc = subprocess.run([str(binary), "repl", "--yolo", "--old", "--model", "vercel"],
+ input=PROMPT + "\n", text=True, capture_output=True,
+ cwd=work, env=env, timeout=90)
+ finally:
+ model.stop()
+ tail = f"\nstdout:\n{proc.stdout[-1500:]}\nstderr:\n{proc.stderr[-1500:]}"
+ assert len(model.root_bodies) >= 3, f"the root ended with its child still running ({len(model.root_bodies)} root requests){tail}"
+ assert any(REPORT in json.dumps(b) for b in model.root_bodies[2:]), f"the child's report never reached the root{tail}"
+ assert DONE in proc.stdout, f"the resumed turn's answer is missing{tail}"
+ print("PASS scripted repl waits for the background subagent it started (ADR 0215)")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/src/session_run.zig b/src/session_run.zig
index db519d72..cac4db37 100644
--- a/src/session_run.zig
+++ b/src/session_run.zig
@@ -117,6 +117,10 @@ pub fn runReplCommand(gpa: Allocator, io: Io, environ_map: anytype, root: *agent
}
repl_glue.bindHostCommands();
defer repl_glue.unbindHostCommands();
+ // ADR 0215: nothing wakes a scripted repl after its last line, so each
+ // turn waits for the background work it started, as `-p` does.
+ @import("run_idle.zig").enabled = true;
+ defer @import("run_idle.zig").enabled = false;
try repl.runScripted(gpa, io, environ_map, in, out, &repl_ctx, repl_glue.replTurnCb, repl_glue.replModelCb, repl_glue.replCancelCb, root.provider.model, models_buf.items);
// Same stderr footer as `-p`, so evals can score the scripted REPL the
// same way. TTY `graff repl` is the TUI and keeps the restore tail clean.