graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Preserve distinct streamed tool calls when indexes restart
Rach Pradhan92d2ac1e75b2
diff --git a/evals/harness_behavior.jsonl b/evals/harness_behavior.jsonlindex 0869b803..d5b1b47a 100644--- a/evals/harness_behavior.jsonl+++ b/evals/harness_behavior.jsonl@@ -139,3 +139,4 @@{"id":"atomic-edit-batch-rejects-later-span","why":"A later ambiguous edit span must reject the entire batch without leaving earlier spans on disk; a corrected dependent batch then commits once and verifies.","args":["--old"],"env":{"GRAFF_NO_CODEDB_GUARD":"1"},"workspace_seed":{"f.txt":"alpha beta\nreturn v\nreturn v\n"},"prompt":"Apply the three edits to f.txt, check that a rejected batch left the file unchanged, then correct the ambiguous span and verify the final contents.","script":[{"tool":"edit_file","arguments":{"path":"f.txt","edits":[{"old_string":"alpha","new_string":"ALPHA"},{"old_string":"beta","new_string":"BETA"},{"old_string":"return v","new_string":"return value"}]}},{"tool":"bash","arguments":{"command":"python3 -c \"from pathlib import Path; assert Path('f.txt').read_text() == 'alpha beta\\nreturn v\\nreturn v\\n'; print('UNCHANGED_AFTER_REJECTION')\""}},{"tool":"edit_file","arguments":{"path":"f.txt","edits":[{"old_string":"alpha","new_string":"ALPHA"},{"old_string":"beta","new_string":"BETA"},{"old_string":"return v\nreturn v","new_string":"return value\nreturn value"}]}},{"tool":"bash","arguments":{"command":"python3 -c \"from pathlib import Path; assert Path('f.txt').read_text() == 'ALPHA BETA\\nreturn value\\nreturn value\\n'; print('CORRECTED_BATCH_VERIFIED')\""}},{"text":"The corrected batch passed both file checks."}],"assert":[{"event":{"type":"tool_call_finished","name":"edit_file","is_error":true}},{"event":{"type":"tool_call_finished","name":"edit_file","is_error":false}},{"events_at_most":{"match":{"type":"tool_call_finished","name":"edit_file","is_error":true},"count":1}},{"request_contains":{"index":1,"text":"edit span 3/3 failed"}},{"request_contains":{"index":1,"text":"no batch changes written"}},{"event":{"type":"tool_result","name":"bash","text":"UNCHANGED_AFTER_REJECTION\n"}},{"event":{"type":"tool_result","name":"bash","text":"CORRECTED_BATCH_VERIFIED\n"}},{"file_equals":{"path":"f.txt","content":"ALPHA BETA\nreturn value\nreturn value\n"}},{"final_text_contains":"The corrected batch passed both file checks."}]}{"id":"large-log-preview-early-omitted-error","why":"A spilled tool result must surface useful omitted error lines in its first model request without an extra pager call.","args":["--old"],"env":{"GRAFF_NO_CODEDB_GUARD":"1"},"prompt":"Run the diagnostic report and summarize its errors.","script":[{"tool":"shell","arguments":{"command":"python3 -c '[print('\"'\"'ok %04d '\"'\"'%i+'\"'\"'x'\"'\"'*32) for i in range(24)]; print('\"'\"'ERROR '\"'\"'+ '\"'\"'EARLY_'\"'\"'+'\"'\"'GAP_VISIBLE'\"'\"'); [print('\"'\"'ok %04d '\"'\"'%i+'\"'\"'x'\"'\"'*32) for i in range(24,625)]'"}},{"text":"Report reviewed."}],"assert":[{"request_count":2},{"request_contains":{"index":1,"text":"ERROR EARLY_GAP_VISIBLE"}},{"request_contains":{"index":1,"text":"tool result handle"}},{"final_text_contains":"Report reviewed."}]}{"id":"large-log-preview-short-error-after-long-line","why":"A spilled tool result must surface useful omitted error lines in its first model request without an extra pager call.","args":["--old"],"env":{"GRAFF_NO_CODEDB_GUARD":"1"},"prompt":"Run the diagnostic report and summarize its errors.","script":[{"tool":"shell","arguments":{"command":"python3 -c '[print('\"'\"'ok %04d '\"'\"'%i+'\"'\"'x'\"'\"'*32) for i in range(100)]; print('\"'\"'ERROR '\"'\"'+ '\"'\"'z'\"'\"'*2500); print('\"'\"'ERROR '\"'\"'+ '\"'\"'SHORT_'\"'\"'+'\"'\"'DETAIL_VISIBLE'\"'\"'); [print('\"'\"'ok %04d '\"'\"'%i+'\"'\"'x'\"'\"'*32) for i in range(100,700)]'"}},{"text":"Report reviewed."}],"assert":[{"request_count":2},{"request_contains":{"index":1,"text":"ERROR SHORT_DETAIL_VISIBLE"}},{"request_contains":{"index":1,"text":"tool result handle"}},{"final_text_contains":"Report reviewed."}]}+{"id":"stream-reused-index-distinct-tool-ids","why":"A streamed tool index can restart with a fresh call ID. Guard the incomplete first call, execute the separate complete call once, and preserve both outcomes in model history.","args":["--old"],"env":{"GRAFF_NO_CODEDB_GUARD":"1"},"prompt":"Run the scripted file operation and report the observed result.","script":[{"stream_deltas":[{"tool_calls":[{"index":0,"id":"old","type":"function","function":{"name":"write_file","arguments":"{\"path\":\"guard.txt\""}}]},{"tool_calls":[{"index":0,"id":"new","type":"function","function":{"name":"write_file","arguments":"{\"path\":\"result.txt\",\"content\":\"done\\n\"}"}}]}]},{"text":"The valid file operation completed."}],"verify_command":["python3","-c","from pathlib import Path; assert not Path('guard.txt').exists(); assert Path('result.txt').read_text() == 'done\\n'"],"assert":[{"request_count":2},{"event":{"type":"tool_call_finished","name":"write_file","is_error":true}},{"event":{"type":"tool_call_finished","name":"write_file","is_error":false}},{"events_at_most":{"match":{"type":"tool_call_finished","name":"write_file","is_error":false},"count":1}},{"request_contains":{"index":1,"text":"tool call arguments were truncated or not a JSON object"}},{"file_equals":{"path":"result.txt","content":"done\n"}},{"final_text_contains":"The valid file operation completed."}]}diff --git a/scripts/eval/mock_model.py b/scripts/eval/mock_model.pyindex b84d28cc..cd9a8913 100644--- a/scripts/eval/mock_model.py+++ b/scripts/eval/mock_model.py@@ -149,7 +149,9 @@ class ScriptedModel:deltas.append({"tool_calls": [{"index": position, "id": call["id"], "type": "function", "function": fn,}]})- finish = "tool_calls" if "tool_calls" in message else "stop"+ if "stream_deltas" in reply:+ deltas = reply["stream_deltas"]+ finish = "tool_calls" if "tool_calls" in message or "stream_deltas" in reply else "stop"for delta in deltas or [{"role": "assistant", "content": ""}]:self._chunk({"choices": [{"index": 0, "delta": delta, "finish_reason": None}]})if hook := getattr(model, "after_stream_delta", None):diff --git a/src/agent_steps.zig b/src/agent_steps.zigindex c27b5081..f07b4c7a 100644--- a/src/agent_steps.zig+++ b/src/agent_steps.zig@@ -435,25 +435,14 @@ pub fn assembleAnthropic(self: *Agent, body: []const u8) !?std.json.ObjectMap {return (try util.dupeJsonValue(result_arena, .{ .object = r })).object;}-/// One in-flight tool call while reassembling an OpenAI chat stream;-/// id and function.name arrive on the first fragment, arguments-/// accumulate across fragments keyed by index.-const CallAcc = struct {- id: []const u8 = "",- name: []const u8 = "",- args: std.ArrayList(u8) = .empty,- thought_signature: []const u8 = "",- /// Google hangs the signature off the CALL (extra_content.google.thought_signature);- /// echoing it on the message alone drops the binding, so keep it where it came from.- extra_content: ?Value = null,-};+const chat_tool_calls = @import("chat_tool_calls.zig");pub fn assembleOpenAI(self: *Agent, body: []const u8) !?std.json.ObjectMap {const result_arena = self.messageMutationAlloc();var content: std.ArrayList(u8) = .empty;var reasoning_content: std.ArrayList(u8) = .empty;var reasoning: std.ArrayList(u8) = .empty;- var calls: std.ArrayList(CallAcc) = .empty;+ var calls: chat_tool_calls.Calls = .{};var role: []const u8 = "assistant";var finish: ?Value = null;var usage: ?Value = null;@@ -522,17 +511,7 @@ pub fn assembleOpenAI(self: *Agent, body: []const u8) !?std.json.ObjectMap {if (d.object.get("tool_calls")) |tcs| if (tcs == .array) {for (tcs.array.items) |tc| {if (tc != .object) continue;- const idx: usize = blk: {- if (tc.object.get("index")) |ix| if (ix == .integer and ix.integer >= 0) break :blk @intCast(ix.integer);- // No index: a fresh id opens a new call, otherwise- // the fragment continues the latest one.- break :blk if (tc.object.get("id") != null or calls.items.len == 0) calls.items.len else calls.items.len - 1;- };- while (calls.items.len <= idx) try calls.append(result_arena, .{});- const acc = &calls.items[idx];- if (tc.object.get("id")) |x| if (x == .string and x.string.len > 0) {- acc.id = x.string;- };+ const acc = try calls.forFragment(result_arena, tc);if (tc.object.get("thought_signature")) |x| if (x == .string and x.string.len > 0) {acc.thought_signature = x.string;};@@ -542,7 +521,7 @@ pub fn assembleOpenAI(self: *Agent, body: []const u8) !?std.json.ObjectMap {}if (tc.object.get("function")) |f| if (f == .object) {if (f.object.get("name")) |x| if (x == .string and x.string.len > 0) {- acc.name = x.string;+ acc.acceptName(x.string);};if (f.object.get("arguments")) |x| if (x == .string) try acc.args.appendSlice(result_arena, x.string);};@@ -563,13 +542,13 @@ pub fn assembleOpenAI(self: *Agent, body: []const u8) !?std.json.ObjectMap {try message.put(result_arena, "content", if (content.items.len > 0) Value{ .string = content.items } else .null);if (reasoning_content.items.len > 0) try message.put(result_arena, "reasoning_content", .{ .string = reasoning_content.items });if (reasoning.items.len > 0) try message.put(result_arena, "reasoning", .{ .string = reasoning.items });- if (calls.items.len > 0) {+ if (calls.items.items.len > 0) {var tcs = std.json.Array.init(result_arena);- for (calls.items) |c| {+ for (calls.items.items) |c| {if (c.id.len == 0 and c.name.len == 0) continue;var function: std.json.ObjectMap = .empty;try function.put(result_arena, "name", .{ .string = try result_arena.dupe(u8, c.name) }); // #124: dupe off the scratch parse tree- try function.put(result_arena, "arguments", .{ .string = c.args.items });+ try function.put(result_arena, "arguments", .{ .string = c.arguments() });var tc: std.json.ObjectMap = .empty;try tc.put(result_arena, "id", .{ .string = try result_arena.dupe(u8, c.id) }); // #124: dupe off the scratch parse treetry tc.put(result_arena, "type", .{ .string = "function" });diff --git a/src/agent_steps_tests.zig b/src/agent_steps_tests.zigindex a6a4bb6f..2176d1ea 100644--- a/src/agent_steps_tests.zig+++ b/src/agent_steps_tests.zig@@ -4,6 +4,94 @@const std = @import("std");const Agent = @import("agent.zig").Agent;+fn streamedCalls(arena: std.mem.Allocator, body: []const u8) !std.json.Array {+ var agent: Agent = .{+ .gpa = std.testing.allocator,+ .arena = arena,+ .io = undefined,+ .client = undefined,+ .provider = undefined,+ .messages = undefined,+ .sub = false,+ .label = "test",+ .out = null,+ };+ const root = (try agent.assembleOpenAI(body)).?;+ return root.get("choices").?.array.items[0].object.get("message").?.object.get("tool_calls").?.array;+}++test "same-index fresh IDs retain complete replacement after incomplete read_file" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const calls = try streamedCalls(+ arena,+ "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"old\",\"function\":{\"name\":\"read_file\",\"arguments\":\"{\\\"path\\\": \"}}]}}]}\n" +++ "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"new\",\"function\":{\"name\":\"rlm\",\"arguments\":\"{\\\"code\\\":\\\"print(1)\\\"}\"}}]},\"finish_reason\":\"tool_calls\"}]}\n",+ );+ try std.testing.expectEqual(@as(usize, 2), calls.items.len);+ const first = calls.items[0].object;+ const second = calls.items[1].object;+ try std.testing.expectEqualStrings("old", first.get("id").?.string);+ try std.testing.expectEqualStrings("new", second.get("id").?.string);+ const args = @import("tool_call_args.zig");+ try std.testing.expect(!args.parse(arena, first.get("function").?.object.get("arguments").?.string).valid);+ const clean = args.parse(arena, second.get("function").?.object.get("arguments").?.string);+ try std.testing.expect(clean.valid);+ try std.testing.expectEqualStrings("print(1)", clean.input.object.get("code").?.string);+}++test "same-index complete calls preserve both; repeated ID fragments and indexes interleave" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const calls = try streamedCalls(+ arena,+ "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"a\",\"function\":{\"name\":\"read_file\",\"arguments\":\"{\\\"path\\\":\\\"a\"}}]}}]}\n" +++ "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":1,\"id\":\"b\",\"function\":{\"name\":\"read_file\",\"arguments\":\"{\\\"path\\\":\\\"b\\\"}\"}}]}}]}\n" +++ "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"a\",\"function\":{\"arguments\":\"\\\"}\"}}]}}]}\n" +++ "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"c\",\"function\":{\"name\":\"read_file\",\"arguments\":\"{\\\"path\\\":\\\"c\\\"}\"}}]},\"finish_reason\":\"tool_calls\"}]}\n",+ );+ try std.testing.expectEqual(@as(usize, 3), calls.items.len);+ const expected = [_][]const u8{ "a", "b", "c" };+ for (calls.items, expected) |call, id| {+ try std.testing.expectEqualStrings(id, call.object.get("id").?.string);+ const parsed = @import("tool_call_args.zig").parse(arena, call.object.get("function").?.object.get("arguments").?.string);+ try std.testing.expect(parsed.valid);+ try std.testing.expectEqualStrings(id, parsed.input.object.get("path").?.string);+ }+}++test "empty first call is guarded when a fresh ID restarts the index" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const calls = try streamedCalls(+ arena,+ "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"old\",\"function\":{\"name\":\"read_file\"}}]}}]}\n" +++ "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"new\",\"function\":{\"name\":\"read_file\",\"arguments\":\"{\\\"path\\\":\\\"next.txt\\\"}\"}}]},\"finish_reason\":\"tool_calls\"}]}\n",+ );+ try std.testing.expectEqual(@as(usize, 2), calls.items.len);+ const parse = @import("tool_call_args.zig").parse;+ try std.testing.expect(!parse(arena, calls.items[0].object.get("function").?.object.get("arguments").?.string).valid);+ const next = parse(arena, calls.items[1].object.get("function").?.object.get("arguments").?.string);+ try std.testing.expect(next.valid);+ try std.testing.expectEqualStrings("next.txt", next.input.object.get("path").?.string);+}++test "same ID with contradictory function names remains non-executable" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ const calls = try streamedCalls(+ arena,+ "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"a\",\"function\":{\"name\":\"read_file\",\"arguments\":\"{\\\"path\\\":\\\"a\\\"}\"}}]}}]}\n" +++ "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"a\",\"function\":{\"name\":\"shell\"}}]},\"finish_reason\":\"tool_calls\"}]}\n",+ );+ try std.testing.expectEqual(@as(usize, 1), calls.items.len);+ try std.testing.expect(!@import("tool_call_args.zig").parse(arena, calls.items[0].object.get("function").?.object.get("arguments").?.string).valid);+}+test "assembleOpenAI preserves streamed reasoning and Gemini echo fields" {var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);defer arena_state.deinit();diff --git a/src/chat_tool_calls.zig b/src/chat_tool_calls.zignew file mode 100644index 00000000..506c426d--- /dev/null+++ b/src/chat_tool_calls.zig@@ -0,0 +1,80 @@+//! Assemble streamed chat tool calls by explicit ID, even when a provider+//! reuses a wire index. ID-less fragments follow the latest call at that index.+const std = @import("std");+const Value = std.json.Value;+const Allocator = std.mem.Allocator;++pub const Call = struct {+ id: []const u8 = "",+ name: []const u8 = "",+ args: std.ArrayList(u8) = .empty,+ invalid: bool = false,+ thought_signature: []const u8 = "",+ extra_content: ?Value = null,++ pub fn acceptName(self: *Call, name: []const u8) void {+ if (name.len == 0) return;+ if (self.name.len > 0 and !std.mem.eql(u8, self.name, name)) {+ self.invalid = true; // A contradictory name cannot be executed safely.+ return;+ }+ self.name = name;+ }++ pub fn arguments(self: Call) []const u8 {+ return if (self.invalid) "[" else self.args.items;+ }+};++pub const Calls = struct {+ items: std.ArrayList(Call) = .empty,+ index_slots: std.ArrayList(?usize) = .empty,++ fn matchingId(self: *Calls, id: []const u8) ?usize {+ if (id.len == 0) return null;+ for (self.items.items, 0..) |call, slot| {+ if (std.mem.eql(u8, call.id, id)) return slot;+ }+ return null;+ }++ pub fn forFragment(self: *Calls, alloc: Allocator, tc: Value) !*Call {+ const index: ?usize = if (tc.object.get("index")) |ix|+ (if (ix == .integer and ix.integer >= 0) @as(usize, @intCast(ix.integer)) else null)+ else+ null;+ const id: []const u8 = if (tc.object.get("id")) |v|+ (if (v == .string) v.string else "")+ else+ "";+ var slot = self.matchingId(id);+ if (slot == null) {+ if (index) |ix| {+ while (self.index_slots.items.len <= ix) try self.index_slots.append(alloc, null);+ if (self.index_slots.items[ix]) |current| {+ // Only a fresh nonempty ID starts another call at this index.+ if (id.len == 0 or self.items.items[current].id.len == 0 or+ std.mem.eql(u8, self.items.items[current].id, id))+ {+ slot = current;+ } else if (!@import("tool_call_args.zig").isObjectString(std.heap.page_allocator, self.items.items[current].args.items)) {+ self.items.items[current].invalid = true;+ }+ }+ }+ }+ if (slot == null and index == null and id.len == 0 and self.items.items.len > 0)+ slot = self.items.items.len - 1;+ if (slot == null) {+ slot = self.items.items.len;+ try self.items.append(alloc, .{});+ }+ if (index) |ix| {+ while (self.index_slots.items.len <= ix) try self.index_slots.append(alloc, null);+ self.index_slots.items[ix] = slot;+ }+ const call = &self.items.items[slot.?];+ if (id.len > 0) call.id = id;+ return call;+ }+};