Skip to content

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

Python

acp: a yield_turn handoff is delivered once (#1615)

justrach382e9be52f74
diff --git a/build.zig b/build.zig
index daeedc06..48105ed5 100644
--- a/build.zig
+++ b/build.zig
@@ -143,6 +143,9 @@ pub fn build(b: *std.Build) void {
const acp_meta_status_test = b.addSystemCommand(&.{ "python3", "scripts/test-acp-meta-tool-status.py" });
acp_meta_status_test.addArtifactArg(exe);
test_step.dependOn(&acp_meta_status_test.step);
+ const acp_yield_once_test = b.addSystemCommand(&.{ "python3", "scripts/test-acp-yield-once.py" });
+ acp_yield_once_test.addArtifactArg(exe);
+ test_step.dependOn(&acp_yield_once_test.step);
const acp_startup_test = b.addSystemCommand(&.{ "python3", "scripts/test-acp-startup.py" });
acp_startup_test.addArtifactArg(exe);
test_step.dependOn(&acp_startup_test.step);
diff --git a/scripts/test-acp-yield-once.py b/scripts/test-acp-yield-once.py
new file mode 100755
index 00000000..41075463
--- /dev/null
+++ b/scripts/test-acp-yield-once.py
@@ -0,0 +1,118 @@
+#!/usr/bin/env python3
+"""#1615: a yield_turn handoff reaches an ACP client exactly once.
+
+The handoff streams as text while yield_turn runs and is also the turn's
+returned result; its tool row must still complete.
+
+Offline: uses the tier-2 scripted model, an isolated workspace and no credentials.
+"""
+from __future__ import annotations
+
+import json
+import os
+from pathlib import Path
+import queue
+import subprocess
+import sys
+import tempfile
+import threading
+
+REPO = Path(__file__).resolve().parents[1]
+sys.path.insert(0, str(REPO / "scripts" / "eval"))
+from mock_model import ScriptedModel
+
+TERMINAL = ("completed", "failed")
+
+
+def main() -> None:
+ binary = str(Path(sys.argv[1] if len(sys.argv) > 1 else REPO / "zig-out/bin/graff").resolve())
+ model = ScriptedModel([
+ {"tool": "todo_write", "arguments": {"todos": [{"content": "wait for the screenshot", "status": "in_progress"}]}},
+ {"tool": "yield_turn", "arguments": {"message": "HANDOFF_ONCE: attach the screenshot and I will continue."}},
+ ])
+ port = model.start(0)
+ try:
+ with tempfile.TemporaryDirectory(prefix="graff-acp-yield-") as temp:
+ workspace = Path(temp)
+ env = {key: os.environ[key] for key in ("PATH", "TMPDIR", "SystemRoot") if key in os.environ}
+ config = workspace / "mcp.json"
+ config.write_text('{"mcpServers":{}}')
+ env.update({
+ "HOME": temp,
+ "AI_GATEWAY_API_KEY": "local",
+ "GRAFF_VERCEL_URL": "http://127.0.0.1:%d/v1/chat/completions" % port,
+ "GRAFF_MCP_CONFIG": str(config),
+ "GRAFF_NO_TELEMETRY": "1",
+ "GRAFF_FLEET": "off",
+ "GRAFF_NO_SMOLIFY": "1",
+ "GRAFF_LEARN_AUTO": "0",
+ "GRAFF_BEHAVIOR_UPLOAD": "0",
+ "GRAFF_NO_BROWSER": "1",
+ "NO_COLOR": "1",
+ })
+ settings = workspace / ".harness/settings.json"
+ settings.parent.mkdir()
+ settings.write_text('{"ai_title":false}')
+ proc = subprocess.Popen(
+ [binary, "--yolo", "--no-telemetry", "--model", "vercel", "--old", "--no-lean", "acp"],
+ cwd=temp, env=env, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE, text=True, encoding="utf-8",
+ )
+ responses = queue.Queue()
+
+ def read_stdout() -> None:
+ for line in proc.stdout:
+ try:
+ responses.put(json.loads(line))
+ except ValueError:
+ responses.put({"invalid_stdout": line})
+ responses.put({"eof": True})
+
+ reader = threading.Thread(target=read_stdout, daemon=True)
+ reader.start()
+
+ def request(rpc_id: int, method: str, params: dict) -> list:
+ proc.stdin.write(json.dumps({"jsonrpc": "2.0", "id": rpc_id, "method": method, "params": params}) + "\n")
+ proc.stdin.flush()
+ events = []
+ while True:
+ event = responses.get(timeout=45)
+ assert "invalid_stdout" not in event and "eof" not in event, event
+ events.append(event)
+ if event.get("id") == rpc_id:
+ assert "error" not in event, event
+ return events
+
+ try:
+ request(1, "initialize", {"protocolVersion": 1})
+ sid = request(2, "session/new", {"cwd": temp, "mcpServers": []})[-1]["result"]["sessionId"]
+ events = request(3, "session/prompt", {"sessionId": sid, "prompt": [{"type": "text", "text": "fix the layout bug"}]})
+ assert events[-1]["result"]["stopReason"] == "end_turn", events[-1]
+ answer = "".join(e["params"]["update"]["content"]["text"] for e in events
+ if e.get("method") == "session/update"
+ and e["params"]["update"].get("sessionUpdate") == "agent_message_chunk")
+ assert answer.count("HANDOFF_ONCE") == 1, "handoff delivered %d times: %r" % (answer.count("HANDOFF_ONCE"), answer)
+ updates = [e["params"]["update"] for e in events
+ if e.get("method") == "session/update" and "toolCallId" in e["params"].get("update", {})]
+ rows = {u["toolCallId"]: (u.get("_meta") or {}).get("graff/toolName") for u in updates
+ if u.get("sessionUpdate") == "tool_call"}
+ for call_id, name in rows.items():
+ ends = [u for u in updates if u["toolCallId"] == call_id and u.get("status") in TERMINAL]
+ assert len(ends) == 1, "%s ended %d times" % (name, len(ends))
+ proc.stdin.close()
+ assert proc.wait(timeout=15) == 0, proc.stderr.read()
+ finally:
+ if proc.poll() is None:
+ proc.kill()
+ proc.wait()
+ reader.join(timeout=5)
+ for pipe in (proc.stdin, proc.stdout, proc.stderr):
+ if pipe and not pipe.closed:
+ pipe.close()
+ finally:
+ model.stop()
+ print("ACP yield_turn: the handoff arrives once and its row completes")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/src/acp_stream.zig b/src/acp_stream.zig
index 0cf64b9b..71fb64ff 100644
--- a/src/acp_stream.zig
+++ b/src/acp_stream.zig
@@ -127,7 +127,9 @@ fn lastId(id_buf: *const [64]u8) []const u8 {
return if (n == 0) "call-1" else id_buf[0..n];
}
-pub const Kind = enum { none, thought, text, tool, notice };
+/// `tool` starts work (the answer is still to come); `tool_end` closes a
+/// call and leaves already-streamed answer text standing (#1615).
+pub const Kind = enum { none, thought, text, tool, tool_end, notice };
const terminal = @import("acp_tool_terminal.zig");
/// Translate one --json event object into zero or one session/update.
@@ -200,7 +202,7 @@ pub fn translateTracked(
// meta tool's finish is its only end (#1600, acp_tool_terminal.zig).
if (std.mem.eql(u8, typ, "tool_call_finished")) {
_ = try terminal.finish(w, session_id, ev.object, resolved);
- return .tool;
+ return .tool_end;
}
if (std.mem.eql(u8, typ, "tool_result") or std.mem.eql(u8, typ, "tool_rejected")) {
const result_name = util.strFieldObj(ev.object, "name") orelse "";
@@ -216,10 +218,10 @@ pub fn translateTracked(
// native edit keeps its announced diff instead of "replaced 1 …".
if (!is_error and (std.mem.eql(u8, result_name, "edit_file") or std.mem.eql(u8, result_name, "write_file"))) {
try writeToolStatus(w, session_id, use_id, "completed");
- return .tool;
+ return .tool_end;
}
try writeToolDone(w, session_id, use_id, is_error, text);
- return .tool;
+ return .tool_end;
}
if (std.mem.eql(u8, typ, "ask_user")) {
if (acp_elicit.mode == .elicitation) {
@@ -318,6 +320,9 @@ pub const EventSink = struct {
self.saw_text.* = false;
if (self.step) |step| step.run(step.ctx, self.out);
},
+ // A call ending is no new work: text it streamed (a yield_turn
+ // handoff) is still the answer, or it would be delivered twice.
+ .tool_end => if (self.step) |step| step.run(step.ctx, self.out),
else => {},
}
self.out.flush() catch {};
@@ -396,7 +401,7 @@ test "translateEvent: an id-less tool_call_started updates the announced call" {
w = .fixed(&buf);
const finished = try std.json.parseFromSlice(Value, a, "{\"type\":\"tool_call_finished\",\"name\":\"bash\",\"is_error\":false,\"ms\":12}", .{});
defer finished.deinit();
- try testing.expectEqual(.tool, try translateEvent(&w, "s1", finished.value, &id_buf, &next));
+ try testing.expectEqual(.tool_end, try translateEvent(&w, "s1", finished.value, &id_buf, &next));
try testing.expectEqual(@as(usize, 0), w.buffered().len);
}
@@ -497,7 +502,7 @@ test "translateEvent: tool_call then tool_result share a minted id" {
w = .fixed(&buf);
const result = try std.json.parseFromSlice(Value, a, "{\"type\":\"tool_result\",\"name\":\"read_file\",\"is_error\":false,\"text\":\"fn main\"}", .{});
defer result.deinit();
- try testing.expectEqual(.tool, try translateEvent(&w, "s1", result.value, &id_buf, &next));
+ try testing.expectEqual(.tool_end, try translateEvent(&w, "s1", result.value, &id_buf, &next));
try testing.expect(std.mem.indexOf(u8, w.buffered(), "tool_call_update") != null);
try testing.expect(std.mem.indexOf(u8, w.buffered(), "call-1") != null);
try testing.expect(std.mem.indexOf(u8, w.buffered(), "completed") != null);
diff --git a/src/acp_tool_terminal.zig b/src/acp_tool_terminal.zig
index f407c988..4a043f20 100644
--- a/src/acp_tool_terminal.zig
+++ b/src/acp_tool_terminal.zig
@@ -164,3 +164,41 @@ test "EventSink: every announced id ends once, meta tools included (#1600)" {
try testing.expectEqual(@as(usize, 1), terminal_for.count(out, "td1", "\"status\":\"failed\""));
try testing.expect(std.mem.indexOf(u8, out, "\"toolCallId\":\"ac\"") == null);
}
+
+test "EventSink: a yield_turn handoff stays delivered past its tool's end (#1615)" {
+ const stream = @import("acp_stream.zig");
+ var buf: [8192]u8 = undefined;
+ var w: Io.Writer = .fixed(&buf);
+ var sid: []const u8 = "s1";
+ var saw = false;
+ var sink: stream.EventSink = undefined;
+ sink.init(testing.allocator, &w, &sid, &saw);
+ defer sink.deinit();
+ // tool start -> handoff text -> terminal status: the handoff is the answer
+ // already on the wire, so the turn's returned handoff must not repeat it.
+ for ([_][]const u8{
+ "{\"type\":\"tool_call\",\"id\":\"y1\",\"name\":\"yield_turn\",\"input\":{\"message\":\"HANDOFF\"}}",
+ "{\"type\":\"text\",\"text\":\"HANDOFF\"}",
+ "{\"type\":\"tool_call_finished\",\"id\":\"y1\",\"name\":\"yield_turn\",\"is_error\":false,\"ms\":1}",
+ }) |line| {
+ try sink.writer.writeAll(line);
+ try sink.writer.writeAll("\n");
+ }
+ try sink.writer.flush();
+ try testing.expect(saw);
+ try testing.expect(std.mem.indexOf(u8, w.buffered(), "\"status\":\"completed\"") != null);
+ try testing.expectEqual(@as(usize, 1), std.mem.count(u8, w.buffered(), "\"text\":\"HANDOFF\""));
+
+ // A preamble followed by a tool still leaves the real answer to come.
+ for ([_][]const u8{
+ "{\"type\":\"text\",\"text\":\"Let me check.\"}",
+ "{\"type\":\"tool_call\",\"id\":\"r1\",\"name\":\"read_file\",\"input\":{\"path\":\"a\"}}",
+ "{\"type\":\"tool_result\",\"id\":\"r1\",\"name\":\"read_file\",\"is_error\":false,\"text\":\"x\"}",
+ "{\"type\":\"tool_call_finished\",\"id\":\"r1\",\"name\":\"read_file\",\"is_error\":false,\"ms\":1}",
+ }) |line| {
+ try sink.writer.writeAll(line);
+ try sink.writer.writeAll("\n");
+ }
+ try sink.writer.flush();
+ try testing.expect(!saw);
+}