graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
acp: close tool rows whose only end is tool_call_finished (#1600)
justrach4918328ff333
diff --git a/build.zig b/build.zigindex 4ad20d9a..daeedc06 100644--- a/build.zig+++ b/build.zig@@ -140,6 +140,9 @@ pub fn build(b: *std.Build) void {const acp_goal_test = b.addSystemCommand(&.{ "python3", "scripts/test-acp-goal.py" });acp_goal_test.addArtifactArg(exe);test_step.dependOn(&acp_goal_test.step);+ 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_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-meta-tool-status.py b/scripts/test-acp-meta-tool-status.pynew file mode 100755index 00000000..7c6e0311--- /dev/null+++ b/scripts/test-acp-meta-tool-status.py@@ -0,0 +1,128 @@+#!/usr/bin/env python3+"""#1600: every tool row announced over ACP v1 ends exactly once.++Meta tools (todo_write here) keep their result off the durable wire, so their+tool_call_finished is the only end they get; it must still close the row.+Ordinary tools close on their result and must not get a second update.++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": "list files", "status": "in_progress"}]}},+ {"tool": "bash", "arguments": {"command": "printf hi > marker.txt"}},+ {"tool": "todo_write", "arguments": {"todos": [{"content": "list files", "status": "completed"}]}},+ {"text": "META_STATUS_DONE"},+ ])+ port = model.start(0)+ try:+ with tempfile.TemporaryDirectory(prefix="graff-acp-meta-") 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": "track a todo and write a marker"}]})+ assert events[-1]["result"]["stopReason"] == "end_turn", events[-1]+ assert (workspace / "marker.txt").read_text() == "hi"++ updates = [e["params"]["update"] for e in events+ if e.get("method") == "session/update" and "toolCallId" in e["params"].get("update", {})]+ announced = {}+ for u in updates:+ if u.get("sessionUpdate") == "tool_call":+ announced[u["toolCallId"]] = (u.get("_meta") or {}).get("graff/toolName", "?")+ names = sorted(announced.values())+ assert names.count("todo_write") == 2 and "bash" in names, names+ for call_id, name in announced.items():+ ends = [u for u in updates if u["toolCallId"] == call_id and u.get("status") in TERMINAL]+ assert len(ends) == 1, "%s (%s) ended %d times: %s" % (name, call_id, len(ends), ends)+ assert ends[0]["status"] == "completed", (name, ends[0])+ 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 "META_STATUS_DONE" in answer, answer+ 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 tool rows: every announced id ends exactly once (meta + ordinary)")+++if __name__ == "__main__":+ main()diff --git a/src/acp.zig b/src/acp.zigindex 25230583..0a4af038 100644--- a/src/acp.zig+++ b/src/acp.zig@@ -540,6 +540,7 @@ test {_ = @import("transport_gate.zig");_ = @import("acp_mcp_app.zig");_ = @import("acp_view_meta.zig");+ _ = @import("acp_tool_terminal.zig");_ = @import("model_maker.zig");}diff --git a/src/acp_stream.zig b/src/acp_stream.zigindex 555e2dc2..0cf64b9b 100644--- a/src/acp_stream.zig+++ b/src/acp_stream.zig@@ -127,15 +127,24 @@ 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 };+const terminal = @import("acp_tool_terminal.zig");+/// Translate one --json event object into zero or one session/update./// `id_buf` holds the last minted toolCallId when the event has no `id`.-pub fn translateEvent(+pub fn translateEvent(w: *Io.Writer, session_id: []const u8, ev: Value, id_buf: *[64]u8, next_tool: *u32) !Kind {+ return translateTracked(w, session_id, ev, id_buf, next_tool, null);+}++/// `translateEvent`, closing meta tools whose only end is a finish (#1600).+pub fn translateTracked(w: *Io.Writer,session_id: []const u8,ev: Value,id_buf: *[64]u8,next_tool: *u32,-) !enum { none, thought, text, tool, notice } {+ resolved: ?*terminal.Resolved,+) !Kind {if (ev != .object) return .none;const typ = util.strFieldObj(ev.object, "type") orelse return .none;if (std.mem.eql(u8, typ, "review_checkpoint")) {@@ -187,14 +196,17 @@ pub fn translateEvent(try writeToolCall(w, session_id, id, name, input);return .tool;}- // The engine closes every call with tool_result and then- // tool_call_finished (timing only, no text): the result already sent the- // terminal update, so a second one would only duplicate it (#1288).- if (std.mem.eql(u8, typ, "tool_call_finished")) return .tool;+ // An ordinary call's result already sent its terminal update (#1288); a+ // 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;+ }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 "";if (std.mem.eql(u8, result_name, "attempt_completion")) return .none;const use_id = if (util.strFieldObj(ev.object, "id")) |given| given else lastId(id_buf);+ if (resolved) |r| r.add(use_id);const text = util.strFieldObj(ev.object, "text") orelse util.strFieldObj(ev.object, "message") orelse "";const is_error = switch (ev.object.get("is_error") orelse .null) {.bool => |b| b,@@ -246,6 +258,7 @@ pub const EventSink = struct {/// context meter (acp_context_meter.zig).step: ?@import("acp_context_meter.zig").Step = null,last_id: [64]u8 = @splat(0),+ resolved: terminal.Resolved = .{},buf: [4096]u8 = undefined,writer: Io.Writer = undefined,@@ -287,7 +300,7 @@ pub const EventSink = struct {// transport writer. Both acquire this per-turn lock before output.if (self.output_lock) |lock| lock.lockUncancelable(self.output_io.?);defer if (self.output_lock) |lock| lock.unlock(self.output_io.?);- const kind = translateEvent(self.out, self.session_id.*, parsed.value, &self.last_id, &self.next_tool) catch return;+ const kind = translateTracked(self.out, self.session_id.*, parsed.value, &self.last_id, &self.next_tool, &self.resolved) catch return;// saw_text means "the last thing streamed was answer text". A tool// event resets it: a completion-tool turn streams its preamble, THEN// runs tools, and the real answer only exists in the turn's finaldiff --git a/src/acp_tool_terminal.zig b/src/acp_tool_terminal.zignew file mode 100644index 00000000..f407c988--- /dev/null+++ b/src/acp_tool_terminal.zig@@ -0,0 +1,166 @@+//! Every announced ACP tool row gets exactly one terminal status (#1600).+//!+//! The engine closes an ordinary call with `tool_result` (the terminal update)+//! and then `tool_call_finished` (timing only). Meta tools (`peer_message`,+//! `load_tool_schemas`, `todo_write`, …) keep their result off the durable+//! wire, so their `tool_call_finished` is the only end they get. The stream+//! remembers which ids a result already closed: a finish for any other id+//! sends the completed/failed status itself, and a finish after a result+//! sends nothing (no duplicate, #1288).++const std = @import("std");+const Io = std.Io;+const Value = std.json.Value;+const util = @import("util.zig");++/// The ids a `tool_result` closed recently. A ring: a finish follows its+/// call's result within the same batch, so a few dozen ids cover any batch.+pub const Resolved = struct {+ ids: [cap][64]u8 = undefined,+ lens: [cap]u8 = @splat(0),+ next: usize = 0,++ const cap = 64;++ pub fn add(self: *Resolved, id: []const u8) void {+ const n = @min(id.len, 64);+ @memcpy(self.ids[self.next][0..n], id[0..n]);+ self.lens[self.next] = @intCast(n);+ self.next = (self.next + 1) % cap;+ }++ pub fn has(self: *const Resolved, id: []const u8) bool {+ const n = @min(id.len, 64);+ for (self.ids, self.lens) |slot, len| {+ if (len != 0 and len == n and std.mem.eql(u8, slot[0..len], id[0..n])) return true;+ }+ return false;+ }+};++/// Handle `tool_call_finished`: write the terminal status unless a result+/// already closed this id. Returns whether it wrote one. Without tracking+/// (`resolved == null`) or an id it writes nothing, as before.+pub fn finish(w: *Io.Writer, session_id: []const u8, ev: std.json.ObjectMap, resolved: ?*Resolved) !bool {+ const r = resolved orelse return false;+ const id = util.strFieldObj(ev, "id") orelse return false;+ if (id.len == 0) return false;+ // attempt_completion never had a row (its payload is the final text).+ if (std.mem.eql(u8, util.strFieldObj(ev, "name") orelse "", "attempt_completion")) return false;+ if (r.has(id)) return false;+ const is_error = switch (ev.get("is_error") orelse .null) {+ .bool => |b| b,+ else => false,+ };+ try @import("acp_stream.zig").writeToolStatus(w, session_id, id, if (is_error) "failed" else "completed");+ r.add(id);+ return true;+}++const testing = std.testing;++fn object(a: std.mem.Allocator, json: []const u8) !std.json.ObjectMap {+ const parsed = try std.json.parseFromSliceLeaky(Value, a, json, .{});+ return parsed.object;+}++test "finish closes a meta tool that never got a result, once" {+ var arena = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var buf: [2048]u8 = undefined;+ var w: Io.Writer = .fixed(&buf);+ var resolved: Resolved = .{};+ const ev = try object(a, "{\"type\":\"tool_call_finished\",\"id\":\"pm1\",\"name\":\"peer_message\",\"is_error\":false,\"ms\":3}");+ try testing.expect(try finish(&w, "s1", ev, &resolved));+ try testing.expect(std.mem.indexOf(u8, w.buffered(), "\"toolCallId\":\"pm1\"") != null);+ try testing.expect(std.mem.indexOf(u8, w.buffered(), "\"status\":\"completed\"") != null);+ // A repeated finish writes nothing more.+ const before = w.buffered().len;+ try testing.expect(!try finish(&w, "s1", ev, &resolved));+ try testing.expectEqual(before, w.buffered().len);+}++test "finish reports a failed meta tool as failed" {+ var arena = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena.deinit();+ var buf: [2048]u8 = undefined;+ var w: Io.Writer = .fixed(&buf);+ var resolved: Resolved = .{};+ const ev = try object(arena.allocator(), "{\"type\":\"tool_call_finished\",\"id\":\"ld1\",\"name\":\"load_tool_schemas\",\"is_error\":true,\"ms\":1}");+ try testing.expect(try finish(&w, "s1", ev, &resolved));+ try testing.expect(std.mem.indexOf(u8, w.buffered(), "\"status\":\"failed\"") != null);+}++test "finish after a result, without an id, untracked, or for attempt_completion writes nothing" {+ var arena = std.heap.ArenaAllocator.init(testing.allocator);+ defer arena.deinit();+ const a = arena.allocator();+ var buf: [2048]u8 = undefined;+ var w: Io.Writer = .fixed(&buf);+ var resolved: Resolved = .{};+ resolved.add("b1");+ const after_result = try object(a, "{\"type\":\"tool_call_finished\",\"id\":\"b1\",\"name\":\"bash\",\"is_error\":false}");+ try testing.expect(!try finish(&w, "s1", after_result, &resolved));+ const no_id = try object(a, "{\"type\":\"tool_call_finished\",\"name\":\"bash\",\"is_error\":false}");+ try testing.expect(!try finish(&w, "s1", no_id, &resolved));+ const fresh = try object(a, "{\"type\":\"tool_call_finished\",\"id\":\"t1\",\"name\":\"todo_write\"}");+ try testing.expect(!try finish(&w, "s1", fresh, null));+ const answer = try object(a, "{\"type\":\"tool_call_finished\",\"id\":\"ac\",\"name\":\"attempt_completion\"}");+ try testing.expect(!try finish(&w, "s1", answer, &resolved));+ try testing.expectEqual(@as(usize, 0), w.buffered().len);+}++test "the ring forgets the oldest ids" {+ var resolved: Resolved = .{};+ var tmp: [16]u8 = undefined;+ for (0..Resolved.cap + 1) |i| resolved.add(try std.fmt.bufPrint(&tmp, "id-{d}", .{i}));+ try testing.expect(!resolved.has("id-0"));+ try testing.expect(resolved.has("id-1"));+ try testing.expect(resolved.has(try std.fmt.bufPrint(&tmp, "id-{d}", .{Resolved.cap})));+}++test "EventSink: every announced id ends once, meta tools included (#1600)" {+ const stream = @import("acp_stream.zig");+ var buf: [16384]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();+ // An ordinary tool: result, then finish. A meta tool: finish only (its+ // result stays off the durable wire). A failed meta tool. The answer.+ for ([_][]const u8{+ "{\"type\":\"tool_call\",\"id\":\"b1\",\"name\":\"bash\",\"input\":{\"command\":\"ls\"}}",+ "{\"type\":\"tool_result\",\"id\":\"b1\",\"name\":\"bash\",\"is_error\":false,\"text\":\"a\"}",+ "{\"type\":\"tool_call_finished\",\"id\":\"b1\",\"name\":\"bash\",\"is_error\":false,\"ms\":2}",+ "{\"type\":\"tool_call\",\"id\":\"pm1\",\"name\":\"peer_message\",\"input\":{}}",+ "{\"type\":\"tool_call_finished\",\"id\":\"pm1\",\"name\":\"peer_message\",\"is_error\":false,\"ms\":1}",+ "{\"type\":\"tool_call\",\"id\":\"td1\",\"name\":\"todo_write\",\"input\":{}}",+ "{\"type\":\"tool_call_finished\",\"id\":\"td1\",\"name\":\"todo_write\",\"is_error\":true,\"ms\":1}",+ "{\"type\":\"tool_call\",\"id\":\"ac\",\"name\":\"attempt_completion\",\"input\":{}}",+ "{\"type\":\"tool_call_finished\",\"id\":\"ac\",\"name\":\"attempt_completion\",\"is_error\":false,\"ms\":1}",+ }) |line| {+ try sink.writer.writeAll(line);+ try sink.writer.writeAll("\n");+ }+ try sink.writer.flush();+ const out = w.buffered();+ const terminal_for = struct {+ fn count(text: []const u8, id: []const u8, status: []const u8) usize {+ var n: usize = 0;+ var it = std.mem.splitScalar(u8, text, '\n');+ while (it.next()) |line| {+ var key: [80]u8 = undefined;+ const want = std.fmt.bufPrint(&key, "\"toolCallId\":\"{s}\"", .{id}) catch unreachable;+ if (std.mem.indexOf(u8, line, want) != null and std.mem.indexOf(u8, line, status) != null) n += 1;+ }+ return n;+ }+ };+ try testing.expectEqual(@as(usize, 1), terminal_for.count(out, "b1", "\"status\":\"completed\""));+ try testing.expectEqual(@as(usize, 1), terminal_for.count(out, "pm1", "\"status\":\"completed\""));+ try testing.expectEqual(@as(usize, 1), terminal_for.count(out, "td1", "\"status\":\"failed\""));+ try testing.expect(std.mem.indexOf(u8, out, "\"toolCallId\":\"ac\"") == null);+}