Skip to content

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

Python

Keep the ACP input reader concurrent when the worker pool is busy

justrach94f0c65b5a6c
diff --git a/docs/adr/0035-first-turn-skips-deferred-mcp-join.md b/docs/adr/0035-first-turn-skips-deferred-mcp-join.md
index 334db0cb..53807022 100644
--- a/docs/adr/0035-first-turn-skips-deferred-mcp-join.md
+++ b/docs/adr/0035-first-turn-skips-deferred-mcp-join.md
@@ -30,6 +30,10 @@ Interactive boots print a dim receipt after `plugins:` (`mcp: N server(s)
in background`, `companion: codedb-pro (background)`) and name any boot
phase ≥80ms, so a hang is visible without `GRAFF_BOOT_DEBUG`.
+ACP’s stdin pump must use `io.concurrent`, never `io.async`: a busy async
+quota can otherwise run the endless reader inline and prevent dispatch.
+The subprocess startup regression includes enough silent servers to cover this.
+
## Consequences
A one-shot first turn that *only* has MCP tools will not see them until
diff --git a/scripts/test-acp-startup.py b/scripts/test-acp-startup.py
index 3dd38e6e..71d2a195 100644
--- a/scripts/test-acp-startup.py
+++ b/scripts/test-acp-startup.py
@@ -24,9 +24,14 @@ def main():
security.write_text("#!/bin/sh\nexit 44\n")
security.chmod(0o700)
slow = root / "slow.py"
- slow.write_text("import os,pathlib,sys,time\npathlib.Path('started').write_text(str(os.getpid()))\nfor line in sys.stdin: time.sleep(30)\n")
+ slow.write_text("import os,pathlib,sys,time\npathlib.Path('started-'+str(os.getpid())).touch()\nfor line in sys.stdin: time.sleep(30)\n")
config = root / "mcp.json"
- config.write_text(json.dumps({"mcpServers": {"slow-fixture": {"command": sys.executable, "args": [str(slow)]}}}))
+ # Exceed the opportunistic async quota as well as testing deferred MCP.
+ # The ACP stdin pump must stay concurrent when that quota is busy.
+ config.write_text(json.dumps({"mcpServers": {
+ f"slow-{i}": {"command": sys.executable, "args": [str(slow)]}
+ for i in range((os.cpu_count() or 2) + 2)
+ }}))
env = {"HOME": str(root), "PATH": str(shim) + ":/usr/bin:/bin", "TERM": "dumb",
"LMSTUDIO_API_KEY": "fixture", "GRAFF_MCP_CONFIG": str(config),
"GRAFF_BOOT_DEBUG": "1", "GRAFF_NO_ADOPT": "1", "GRAFF_NO_TELEMETRY": "1", "GRAFF_FLEET": "off"}
@@ -38,11 +43,11 @@ def main():
poll.register(child.stdout, selectors.EVENT_READ)
try:
deadline = time.monotonic() + 5
- while not (root / "started").exists() and time.monotonic() < deadline:
+ while not list(root.glob("started-*")) and time.monotonic() < deadline:
if child.poll() is not None:
raise AssertionError("ACP exited before the optional server started")
time.sleep(.01)
- assert (root / "started").exists(), "optional server did not start"
+ assert list(root.glob("started-*")), "optional server did not start"
child.stdin.write(b'{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":1}}\n')
child.stdin.flush()
deadline = time.monotonic() + 5
@@ -59,7 +64,7 @@ def main():
message = json.loads(line) # stdout must stay protocol-clean.
ready |= message.get("id") == 1 and "result" in message
assert ready, "ACP initialize blocked on the optional MCP handshake\n" + (root / "stderr").read_text()[-4096:]
- os.kill(int((root / "started").read_text()), 0)
+ os.kill(int(next(root.glob("started-*")).name.removeprefix("started-")), 0)
finally:
poll.close()
try:
diff --git a/src/acp.zig b/src/acp.zig
index 316bc4b3..d5bce15b 100644
--- a/src/acp.zig
+++ b/src/acp.zig
@@ -242,7 +242,7 @@ pub fn runAcpCommand(gpa: Allocator, io: Io, environ_map: anytype, root: *agent_
engine.cancel_flag.store(false, .release);
engine.on_cancel = syncEscCancel;
var inbox: @import("acp_inbox.zig").Inbox = .{ .gpa = gpa, .io = io, .reader = in };
- inbox.start();
+ try inbox.start();
defer inbox.deinit();
var live: LiveTurn = .{ .root = root, .keys = keys, .out = out, .inbox = &inbox };
var d: Dispatch = .{
diff --git a/src/acp_inbox.zig b/src/acp_inbox.zig
index cec6e689..982d71d7 100644
--- a/src/acp_inbox.zig
+++ b/src/acp_inbox.zig
@@ -18,8 +18,10 @@ pub const Inbox = struct {
active: bool = false,
future: ?Io.Future(void) = null,
- pub fn start(self: *Inbox) void {
- self.future = self.io.async(pump, .{self});
+ pub fn start(self: *Inbox) !void {
+ // async may run the endless reader inline when its worker quota is
+ // busy, preventing the dispatcher from ever answering initialize.
+ self.future = try self.io.concurrent(pump, .{self});
}
pub fn deinit(self: *Inbox) void {