Skip to content

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

Python

Refuse tool calls a cut-off response left unfinished (#1218)

justracheb17caf43398
diff --git a/evals/harness_behavior.jsonl b/evals/harness_behavior.jsonl
index 553cc946..7e501974 100644
--- a/evals/harness_behavior.jsonl
+++ b/evals/harness_behavior.jsonl
@@ -141,3 +141,4 @@
{"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."}]}
{"id":"jev-effort-only-rejects-judgment","why":"Jev is only an optional effort selector: the removed generic judgment call and an effort request on an ineligible route must both fail locally without changing session effort.","env":{"JEV_BACKEND":"mock"},"prompt":"Try the old judgment tool, then try effort selection here.","script":[{"tool":"jev_judge","arguments":{"state":"tests passed","question":"Did tests pass?","type":"noul"}},{"tool":"jev_effort","arguments":{"task":"a small read-only lookup"}},{"text":"Neither call changed effort."}],"assert":[{"event":{"type":"tool_result","name":"jev_judge","is_error":true,"text":"unknown tool: jev_judge"}},{"event":{"type":"tool_result","name":"jev_effort","is_error":true}},{"final_text_contains":"Neither call changed effort."}]}
+{"id":"stream-length-stop-cut-call","why":"A response that hits its output limit mid-batch must run the finished call and refuse the unfinished one with the cause (#1218), never run it as an empty call.","args":["--old"],"env":{"GRAFF_NO_CODEDB_GUARD":"1"},"prompt":"Run the scripted file operations and report the observed result.","script":[{"stream_deltas":[{"tool_calls":[{"index":0,"id":"done","type":"function","function":{"name":"write_file","arguments":"{\"path\":\"result.txt\",\"content\":\"done\\n\"}"}}]},{"tool_calls":[{"index":1,"id":"cut","type":"function","function":{"name":"read_file"}}]}],"finish":"length"},{"text":"The finished call ran and the cut one was refused."}],"verify_command":["python3","-c","from pathlib import Path; assert Path('result.txt').read_text() == 'done\\n'"],"assert":[{"request_count":2},{"event":{"type":"tool_call_finished","name":"write_file","is_error":false}},{"event":{"type":"tool_call_finished","name":"read_file","is_error":true}},{"request_contains":{"index":1,"text":"the response ended before this call was complete"}},{"file_equals":{"path":"result.txt","content":"done\n"}},{"final_text_contains":"The finished call ran"}]}
diff --git a/scripts/eval/mock_model.py b/scripts/eval/mock_model.py
index fd305d35..5bf6ebfa 100644
--- a/scripts/eval/mock_model.py
+++ b/scripts/eval/mock_model.py
@@ -151,7 +151,7 @@ class ScriptedModel:
}]})
if "stream_deltas" in reply:
deltas = reply["stream_deltas"]
- finish = "tool_calls" if "tool_calls" in message or "stream_deltas" in reply else "stop"
+ finish = reply.get("finish") or ("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.zig
index 05b33154..3f26d8f1 100644
--- a/src/agent_steps.zig
+++ b/src/agent_steps.zig
@@ -190,9 +190,9 @@ pub fn stepAnthropic(self: *Agent, root: std.json.ObjectMap) !?[]const u8 {
const name = if (block.object.get("name")) |n| (if (n == .string) n.string else "") else "";
if (name.len == 0) continue;
const id = if (block.object.get("id")) |x| (if (x == .string) x.string else "") else "";
- const args_ok = !@import("tool_call_args.zig").takeInvalidMark(&block.object);
+ const why = tool_call_args.takeInvalidMark(&block.object);
const input = block.object.get("input") orelse Value{ .object = .empty };
- try calls.append(self.gpa, .{ .id = id, .name = name, .input = input, .args_ok = args_ok });
+ try calls.append(self.gpa, .{ .id = id, .name = name, .input = input, .args_ok = why == .none, .cut = why == .cut });
}
}
self.pairContextMeterWithCurrentLocal();
@@ -279,8 +279,9 @@ pub fn stepOpenAI(self: *Agent, root: std.json.ObjectMap) !?[]const u8 {
var calls: std.ArrayList(ToolCall) = .empty;
defer calls.deinit(self.gpa);
if (msg_obj.get("tool_calls")) |tcs| if (tcs == .array) {
- for (tcs.array.items) |tc| {
- if (tc != .object) continue;
+ for (tcs.array.items) |*tc| { // by pointer: the cut marker is removed in place
+ if (tc.* != .object) continue;
+ const why = tool_call_args.takeInvalidMark(&tc.object);
const function = tc.object.get("function") orelse continue;
if (function != .object) continue;
const args_str = if (function.object.get("arguments")) |a| (if (a == .string) a.string else "") else "";
@@ -288,7 +289,7 @@ pub fn stepOpenAI(self: *Agent, root: std.json.ObjectMap) !?[]const u8 {
const name = if (function.object.get("name")) |n| (if (n == .string) n.string else "") else "";
if (name.len == 0) continue; // can't dispatch a nameless call
const id = if (tc.object.get("id")) |x| (if (x == .string) x.string else "") else "";
- try calls.append(self.gpa, .{ .id = id, .name = name, .input = parsed.input, .args_ok = parsed.valid });
+ try calls.append(self.gpa, .{ .id = id, .name = name, .input = parsed.input, .args_ok = parsed.valid and why == .none, .cut = why == .cut });
}
};
tool_call_args.repairMessage(self.gpa, self.arena, &self.messages.items[self.messages.items.len - 1]);
@@ -350,101 +351,7 @@ pub fn assembleStream(self: *Agent, body: []const u8) !?std.json.ObjectMap {
};
}
-/// One in-flight content block while reassembling an Anthropic stream.
-const BlockAcc = struct {
- obj: std.json.ObjectMap = .empty,
- text: std.ArrayList(u8) = .empty,
- json: std.ArrayList(u8) = .empty,
- thinking: std.ArrayList(u8) = .empty,
- signature: std.ArrayList(u8) = .empty,
-};
-
-/// message_start carries the message skeleton (role, model, input-token
-/// usage); content blocks open with content_block_start and accumulate
-/// via content_block_delta (text / partial_json / thinking / signature);
-/// message_delta carries stop_reason and output-token usage.
-pub fn assembleAnthropic(self: *Agent, body: []const u8) !?std.json.ObjectMap {
- // #124 slice 2b: the whole assembly — per-event parse trees, delta
- // accumulators, the stitched message — lives on the per-request scratch
- // arena; the finished message is deep-copied onto the session arena once
- // at return (it becomes the assistant history message, so it must survive
- // the next request's scratch reset). Previously every event's parse tree
- // landed on the session arena for the life of the process.
- const scratch = self.scratchAlloc();
- const result_arena = self.messageMutationAlloc();
- var root: ?std.json.ObjectMap = null;
- var blocks: std.ArrayList(BlockAcc) = .empty;
- var stop_reason: ?Value = null;
- var usage_delta: ?Value = null;
- var it = std.mem.tokenizeScalar(u8, body, '\n');
- while (it.next()) |raw_line| {
- const payload = ssePayload(raw_line) orelse continue;
- const v = std.json.parseFromSliceLeaky(Value, scratch, payload, .{ .allocate = .alloc_always }) catch continue;
- if (v != .object) continue;
- const t = v.object.get("type") orelse continue;
- if (t != .string) continue;
- if (std.mem.eql(u8, t.string, "message_start")) {
- if (v.object.get("message")) |m| if (m == .object) {
- root = m.object;
- };
- } else if (std.mem.eql(u8, t.string, "content_block_start")) {
- const idx = sseIndex(v.object) orelse continue;
- while (blocks.items.len <= idx) try blocks.append(scratch, .{});
- if (v.object.get("content_block")) |cb| if (cb == .object) {
- blocks.items[idx].obj = cb.object;
- };
- } else if (std.mem.eql(u8, t.string, "content_block_delta")) {
- const idx = sseIndex(v.object) orelse continue;
- if (idx >= blocks.items.len) continue;
- const d = v.object.get("delta") orelse continue;
- if (d != .object) continue;
- const b = &blocks.items[idx];
- if (d.object.get("text")) |x| if (x == .string) try b.text.appendSlice(scratch, x.string);
- if (d.object.get("partial_json")) |x| if (x == .string) try b.json.appendSlice(scratch, x.string);
- if (d.object.get("thinking")) |x| if (x == .string) try b.thinking.appendSlice(scratch, x.string);
- if (d.object.get("signature")) |x| if (x == .string) try b.signature.appendSlice(scratch, x.string);
- } else if (std.mem.eql(u8, t.string, "message_delta")) {
- if (v.object.get("delta")) |d| if (d == .object) {
- if (d.object.get("stop_reason")) |sr| if (sr == .string) {
- stop_reason = sr;
- };
- };
- if (v.object.get("usage")) |u| if (u == .object) {
- usage_delta = u;
- };
- } else if (std.mem.eql(u8, t.string, "error")) {
- // Hand the envelope back as the root: request()'s existing
- // type=="error" check reports it. Detached from scratch — the
- // rebuild loop can reset the scratch arena before the message
- // is done being read.
- return (try util.dupeJsonValue(result_arena, v)).object;
- }
- }
- var r = root orelse return null;
- var content = std.json.Array.init(scratch);
- for (blocks.items) |*b| {
- if (b.obj.get("type") == null) continue; // never started
- if (b.text.items.len > 0) try b.obj.put(scratch, "text", .{ .string = b.text.items });
- if (b.thinking.items.len > 0) try b.obj.put(scratch, "thinking", .{ .string = b.thinking.items });
- if (b.signature.items.len > 0) try b.obj.put(scratch, "signature", .{ .string = b.signature.items });
- if (b.json.items.len > 0) try @import("tool_call_args.zig").putStreamedInput(scratch, &b.obj, b.json.items); // #1218
- try content.append(.{ .object = b.obj });
- }
- try r.put(scratch, "content", .{ .array = content });
- try r.put(scratch, "stop_reason", stop_reason orelse Value{ .string = "end_turn" });
- if (stop_reason == null) try r.put(scratch, "incomplete", .{ .bool = true });
- if (usage_delta) |ud| {
- var usage: std.json.ObjectMap = .empty;
- if (r.get("usage")) |base| if (base == .object) {
- var e = base.object.iterator();
- while (e.next()) |kv| try usage.put(scratch, kv.key_ptr.*, kv.value_ptr.*);
- };
- var e = ud.object.iterator();
- while (e.next()) |kv| try usage.put(scratch, kv.key_ptr.*, kv.value_ptr.*);
- try r.put(scratch, "usage", .{ .object = usage });
- }
- return (try util.dupeJsonValue(result_arena, .{ .object = r })).object;
-}
+pub const assembleAnthropic = @import("anthropic_assemble.zig").assembleAnthropic;
const chat_tool_calls = @import("chat_tool_calls.zig");
@@ -461,9 +368,13 @@ pub fn assembleOpenAI(self: *Agent, body: []const u8) !?std.json.ObjectMap {
var thought_signature: []const u8 = "";
var extra_content: ?Value = null;
var saw_chunk = false;
+ var saw_done = false; // `data: [DONE]`: the stream ended on purpose even without a finish_reason
var it = std.mem.tokenizeScalar(u8, body, '\n');
while (it.next()) |raw_line| {
- const payload = ssePayload(raw_line) orelse continue;
+ const payload = ssePayload(raw_line) orelse {
+ saw_done = saw_done or std.mem.eql(u8, std.mem.trim(u8, raw_line, " \r"), "data: [DONE]");
+ continue;
+ };
const v = std.json.parseFromSliceLeaky(Value, self.scratchAlloc(), payload, .{ .allocate = .alloc_always }) catch continue; // #124: per-event parse tree is transient (deltas are appendSlice'd into session accumulators)
if (v != .object) continue;
// Error-only SSE (`event: error` / `data: {"error":…}`) has no
@@ -540,6 +451,8 @@ pub fn assembleOpenAI(self: *Agent, body: []const u8) !?std.json.ObjectMap {
};
}
if (!saw_chunk) return null;
+ const stop: ?[]const u8 = if (finish) |f| (if (f == .string) f.string else null) else if (saw_done) "stop" else null;
+ calls.markCut(tool_call_args.responseCut(stop)); // #1218
var message: std.json.ObjectMap = .empty;
try message.put(result_arena, "role", .{ .string = try result_arena.dupe(u8, role) }); // #124: role slices the scratch parse tree; dupe so it survives the per-request scratch reset
@@ -567,6 +480,7 @@ pub fn assembleOpenAI(self: *Agent, body: []const u8) !?std.json.ObjectMap {
const call_sig = if (c.thought_signature.len > 0) c.thought_signature else thought_signature;
if (call_sig.len > 0) try tc.put(result_arena, "thought_signature", .{ .string = try result_arena.dupe(u8, call_sig) });
if (c.extra_content) |ec| try tc.put(result_arena, "extra_content", try util.dupeJsonValue(result_arena, ec));
+ if (c.cut) try tool_call_args.markChatCut(result_arena, &tc);
try tcs.append(.{ .object = tc });
}
if (tcs.items.len > 0) try message.put(result_arena, "tool_calls", .{ .array = tcs });
@@ -585,4 +499,5 @@ pub fn assembleOpenAI(self: *Agent, body: []const u8) !?std.json.ObjectMap {
test {
_ = @import("agent_steps_tests.zig");
+ _ = @import("anthropic_assemble.zig");
}
diff --git a/src/agent_steps_tests.zig b/src/agent_steps_tests.zig
index 2176d1ea..6f31d747 100644
--- a/src/agent_steps_tests.zig
+++ b/src/agent_steps_tests.zig
@@ -79,6 +79,26 @@ test "empty first call is guarded when a fresh ID restarts the index" {
try std.testing.expectEqualStrings("next.txt", next.input.object.get("path").?.string);
}
+test "#1218: a length stop cuts the unfinished last call; [DONE] without a finish does not" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ const args = @import("tool_call_args.zig");
+ const read_a = "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"a\",\"function\":{\"name\":\"read_file\",\"arguments\":\"{\\\"path\\\":\\\"a\\\"}\"}}]}}]}\n";
+ const name_only = "data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":1,\"id\":\"b\",\"function\":{\"name\":\"read_file\"}}]}}]}\n";
+ // Output limit hit right after the second call's header: it never got arguments.
+ const limited = try streamedCalls(arena, read_a ++ name_only ++
+ "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"length\"}]}\n");
+ try std.testing.expectEqual(args.Refusal.none, args.takeInvalidMark(&limited.items[0].object));
+ try std.testing.expectEqual(args.Refusal.cut, args.takeInvalidMark(&limited.items[1].object));
+ // The stream ended with no finish_reason and no [DONE]: dropped mid-call.
+ const dropped = try streamedCalls(arena, read_a ++ name_only);
+ try std.testing.expectEqual(args.Refusal.cut, args.takeInvalidMark(&dropped.items[1].object));
+ // A provider that omits finish_reason but ends with [DONE] finished the call.
+ const done = try streamedCalls(arena, read_a ++ name_only ++ "data: [DONE]\n");
+ try std.testing.expectEqual(args.Refusal.none, args.takeInvalidMark(&done.items[1].object));
+}
+
test "same ID with contradictory function names remains non-executable" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
diff --git a/src/agent_tools.zig b/src/agent_tools.zig
index fa3d0012..6d67d32c 100644
--- a/src/agent_tools.zig
+++ b/src/agent_tools.zig
@@ -187,8 +187,9 @@ fn refuseRead(self: *Agent, call: ToolCall) !ExecResult {
pub fn rejectToolCall(self: *Agent, call: ToolCall) !?ExecResult {
if (!call.args_ok) {
- self.emitToolRejected(call, "invalid_arguments", tool_call_args.invalid_exec_message);
- return .{ .text = tool_call_args.invalid_exec_message, .is_error = true };
+ const message = if (call.cut) tool_call_args.cut_exec_message else tool_call_args.invalid_exec_message;
+ self.emitToolRejected(call, "invalid_arguments", message);
+ return .{ .text = message, .is_error = true };
}
if (std.mem.eql(u8, call.name, "read_file")) {
if (read_miss.callPath(call.input)) |path| {
diff --git a/src/anthropic_assemble.zig b/src/anthropic_assemble.zig
new file mode 100644
index 00000000..e0339eb2
--- /dev/null
+++ b/src/anthropic_assemble.zig
@@ -0,0 +1,197 @@
+//! Reassemble a streamed Anthropic Messages body into the non-streaming
+//! response shape stepAnthropic reads. Split out of agent_steps.zig (600-line
+//! cap) when #1218 added per-block stop tracking.
+
+const std = @import("std");
+const Value = std.json.Value;
+
+const Agent = @import("agent.zig").Agent;
+const util = @import("util.zig");
+const tool_call_args = @import("tool_call_args.zig");
+
+const ssePayload = Agent.ssePayload;
+const sseIndex = Agent.sseIndex;
+
+/// One in-flight content block while reassembling an Anthropic stream.
+const BlockAcc = struct {
+ obj: std.json.ObjectMap = .empty,
+ text: std.ArrayList(u8) = .empty,
+ json: std.ArrayList(u8) = .empty,
+ thinking: std.ArrayList(u8) = .empty,
+ signature: std.ArrayList(u8) = .empty,
+ /// content_block_stop arrived: the block finished streaming.
+ stopped: bool = false,
+
+ fn isToolUse(self: BlockAcc) bool {
+ const t = self.obj.get("type") orelse return false;
+ return t == .string and std.mem.eql(u8, t.string, "tool_use");
+ }
+};
+
+/// message_start carries the message skeleton (role, model, input-token
+/// usage); content blocks open with content_block_start and accumulate
+/// via content_block_delta (text / partial_json / thinking / signature);
+/// message_delta carries stop_reason and output-token usage.
+pub fn assembleAnthropic(self: *Agent, body: []const u8) !?std.json.ObjectMap {
+ // #124 slice 2b: the whole assembly — per-event parse trees, delta
+ // accumulators, the stitched message — lives on the per-request scratch
+ // arena; the finished message is deep-copied onto the session arena once
+ // at return (it becomes the assistant history message, so it must survive
+ // the next request's scratch reset). Previously every event's parse tree
+ // landed on the session arena for the life of the process.
+ const scratch = self.scratchAlloc();
+ const result_arena = self.messageMutationAlloc();
+ var root: ?std.json.ObjectMap = null;
+ var blocks: std.ArrayList(BlockAcc) = .empty;
+ var stop_reason: ?Value = null;
+ var usage_delta: ?Value = null;
+ var saw_block_stop = false;
+ var it = std.mem.tokenizeScalar(u8, body, '\n');
+ while (it.next()) |raw_line| {
+ const payload = ssePayload(raw_line) orelse continue;
+ const v = std.json.parseFromSliceLeaky(Value, scratch, payload, .{ .allocate = .alloc_always }) catch continue;
+ if (v != .object) continue;
+ const t = v.object.get("type") orelse continue;
+ if (t != .string) continue;
+ if (std.mem.eql(u8, t.string, "message_start")) {
+ if (v.object.get("message")) |m| if (m == .object) {
+ root = m.object;
+ };
+ } else if (std.mem.eql(u8, t.string, "content_block_start")) {
+ const idx = sseIndex(v.object) orelse continue;
+ while (blocks.items.len <= idx) try blocks.append(scratch, .{});
+ if (v.object.get("content_block")) |cb| if (cb == .object) {
+ blocks.items[idx].obj = cb.object;
+ };
+ } else if (std.mem.eql(u8, t.string, "content_block_delta")) {
+ const idx = sseIndex(v.object) orelse continue;
+ if (idx >= blocks.items.len) continue;
+ const d = v.object.get("delta") orelse continue;
+ if (d != .object) continue;
+ const b = &blocks.items[idx];
+ if (d.object.get("text")) |x| if (x == .string) try b.text.appendSlice(scratch, x.string);
+ if (d.object.get("partial_json")) |x| if (x == .string) try b.json.appendSlice(scratch, x.string);
+ if (d.object.get("thinking")) |x| if (x == .string) try b.thinking.appendSlice(scratch, x.string);
+ if (d.object.get("signature")) |x| if (x == .string) try b.signature.appendSlice(scratch, x.string);
+ } else if (std.mem.eql(u8, t.string, "content_block_stop")) {
+ saw_block_stop = true;
+ const idx = sseIndex(v.object) orelse continue;
+ if (idx < blocks.items.len) blocks.items[idx].stopped = true;
+ } else if (std.mem.eql(u8, t.string, "message_delta")) {
+ if (v.object.get("delta")) |d| if (d == .object) {
+ if (d.object.get("stop_reason")) |sr| if (sr == .string) {
+ stop_reason = sr;
+ };
+ };
+ if (v.object.get("usage")) |u| if (u == .object) {
+ usage_delta = u;
+ };
+ } else if (std.mem.eql(u8, t.string, "error")) {
+ // Hand the envelope back as the root: request()'s existing
+ // type=="error" check reports it. Detached from scratch — the
+ // rebuild loop can reset the scratch arena before the message
+ // is done being read.
+ return (try util.dupeJsonValue(result_arena, v)).object;
+ }
+ }
+ var r = root orelse return null;
+ // #1218: a response that stopped mid-output (max_tokens, or a stream that
+ // ended before message_delta) can leave tool_use blocks half-written. A
+ // block without its content_block_stop is the one that was cut; a provider
+ // that never sends block stops leaves only the last tool_use in doubt.
+ const cut = tool_call_args.responseCut(if (stop_reason) |s| s.string else null);
+ var last_tool: ?usize = null;
+ for (blocks.items, 0..) |b, i| if (b.isToolUse()) {
+ last_tool = i;
+ };
+ var content = std.json.Array.init(scratch);
+ for (blocks.items, 0..) |*b, i| {
+ if (b.obj.get("type") == null) continue; // never started
+ if (b.text.items.len > 0) try b.obj.put(scratch, "text", .{ .string = b.text.items });
+ if (b.thinking.items.len > 0) try b.obj.put(scratch, "thinking", .{ .string = b.thinking.items });
+ if (b.signature.items.len > 0) try b.obj.put(scratch, "signature", .{ .string = b.signature.items });
+ const cut_block = cut and b.isToolUse() and (if (saw_block_stop) !b.stopped else i == last_tool);
+ if (b.json.items.len > 0 or cut_block) try tool_call_args.putStreamedInput(scratch, &b.obj, b.json.items, cut_block);
+ try content.append(.{ .object = b.obj });
+ }
+ try r.put(scratch, "content", .{ .array = content });
+ try r.put(scratch, "stop_reason", stop_reason orelse Value{ .string = "end_turn" });
+ if (stop_reason == null) try r.put(scratch, "incomplete", .{ .bool = true });
+ if (usage_delta) |ud| {
+ var usage: std.json.ObjectMap = .empty;
+ if (r.get("usage")) |base| if (base == .object) {
+ var e = base.object.iterator();
+ while (e.next()) |kv| try usage.put(scratch, kv.key_ptr.*, kv.value_ptr.*);
+ };
+ var e = ud.object.iterator();
+ while (e.next()) |kv| try usage.put(scratch, kv.key_ptr.*, kv.value_ptr.*);
+ try r.put(scratch, "usage", .{ .object = usage });
+ }
+ return (try util.dupeJsonValue(result_arena, .{ .object = r })).object;
+}
+
+fn testAgent(arena: std.mem.Allocator) Agent {
+ return .{
+ .gpa = std.testing.allocator,
+ .arena = arena,
+ .io = undefined,
+ .client = undefined,
+ .provider = undefined,
+ .messages = undefined,
+ .sub = false,
+ .label = "test",
+ .out = null,
+ };
+}
+
+fn refusals(arena: std.mem.Allocator, body: []const u8) ![]tool_call_args.Refusal {
+ var agent = testAgent(arena);
+ const root = (try assembleAnthropic(&agent, body)).?;
+ const blocks = root.get("content").?.array.items;
+ const out = try arena.alloc(tool_call_args.Refusal, blocks.len);
+ for (blocks, out) |*b, *o| o.* = tool_call_args.takeInvalidMark(&b.object);
+ return out;
+}
+
+const start_read = "data: {\"type\":\"message_start\",\"message\":{\"role\":\"assistant\",\"content\":[]}}\n";
+
+fn toolStart(comptime ix: []const u8) []const u8 {
+ return "data: {\"type\":\"content_block_start\",\"index\":" ++ ix ++ ",\"content_block\":{\"type\":\"tool_use\",\"id\":\"t" ++ ix ++ "\",\"name\":\"read_file\",\"input\":{}}}\n";
+}
+
+fn toolJson(comptime ix: []const u8, comptime json: []const u8) []const u8 {
+ return "data: {\"type\":\"content_block_delta\",\"index\":" ++ ix ++ ",\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"" ++ json ++ "\"}}\n";
+}
+
+fn toolStop(comptime ix: []const u8) []const u8 {
+ return "data: {\"type\":\"content_block_stop\",\"index\":" ++ ix ++ "}\n";
+}
+
+test "#1218: a max_tokens stop refuses only the calls it cut, including a call with no input yet" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ const got = try refusals(arena, comptime start_read ++
+ toolStart("0") ++ toolJson("0", "{\\\"path\\\":\\\"a.zig\\\"}") ++ toolStop("0") ++
+ toolStart("1") ++ toolJson("1", "{\\\"path\\\":\\\"b.z") ++
+ toolStart("2") ++
+ "data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"max_tokens\"}}\n");
+ try std.testing.expectEqualSlices(tool_call_args.Refusal, &.{ .none, .cut, .cut }, got);
+}
+
+test "#1218: a stream that ends before message_delta is cut; a finished response is not" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ // No block stops at all (some compatible providers): only the last call is in doubt.
+ const dropped = try refusals(arena, comptime start_read ++
+ toolStart("0") ++ toolJson("0", "{\\\"path\\\":\\\"a.zig\\\"}") ++
+ toolStart("1"));
+ try std.testing.expectEqualSlices(tool_call_args.Refusal, &.{ .none, .cut }, dropped);
+ // A finished response keeps a no-input call runnable and a malformed one malformed.
+ const done = try refusals(arena, comptime start_read ++
+ toolStart("0") ++ toolStop("0") ++
+ toolStart("1") ++ toolJson("1", "{\\\"path\\\":") ++ toolStop("1") ++
+ "data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"tool_use\"}}\n");
+ try std.testing.expectEqualSlices(tool_call_args.Refusal, &.{ .none, .malformed }, done);
+}
diff --git a/src/chat_tool_calls.zig b/src/chat_tool_calls.zig
index 506c426d..74aaa024 100644
--- a/src/chat_tool_calls.zig
+++ b/src/chat_tool_calls.zig
@@ -9,6 +9,8 @@ pub const Call = struct {
name: []const u8 = "",
args: std.ArrayList(u8) = .empty,
invalid: bool = false,
+ /// #1218: the response stopped while this call was still streaming.
+ cut: bool = false,
thought_signature: []const u8 = "",
extra_content: ?Value = null,
@@ -77,4 +79,15 @@ pub const Calls = struct {
if (id.len > 0) call.id = id;
return call;
}
+
+ /// #1218: when the response stopped mid-output (finish_reason `length`,
+ /// or no finish and no [DONE]), the last call to start is unfinished
+ /// unless its arguments already close as an object. That includes a
+ /// name-only call that got no arguments, which used to run as `{}`.
+ pub fn markCut(self: *Calls, response_cut: bool) void {
+ if (!response_cut or self.items.items.len == 0) return;
+ const last = &self.items.items[self.items.items.len - 1];
+ const args = std.mem.trim(u8, last.args.items, &std.ascii.whitespace);
+ if (args.len == 0 or !@import("tool_call_args.zig").isObjectString(std.heap.page_allocator, args)) last.cut = true;
+ }
};
diff --git a/src/tool_call_args.zig b/src/tool_call_args.zig
index adb3f23f..cb4b29a9 100644
--- a/src/tool_call_args.zig
+++ b/src/tool_call_args.zig
@@ -11,6 +11,12 @@ const Allocator = std.mem.Allocator;
pub const empty_object = "{}";
pub const invalid_exec_message = "tool call arguments were truncated or not a JSON object; the call was not executed";
+/// #1218: the response stopped (output limit, or a stream that ended before
+/// its terminal event) while this call was still being written. The model
+/// gets the cause and the fix, not a malformed-JSON guess it retries as-is.
+pub const cut_exec_message = "tool call arguments were truncated: the response ended before this call was complete (output limit or cut-off stream), so it was not executed. Re-issue it, with fewer or smaller calls per response";
+/// Both refusals start with this; broken-call loop detection keys on it.
+pub const truncated_prefix = "tool call arguments were truncated";
pub const Parsed = struct {
input: Value,
@@ -29,21 +35,49 @@ pub fn parse(alloc: Allocator, s: []const u8) Parsed {
return .{ .input = value, .valid = true };
}
+/// Why a streamed call is not executed. `cut`: the response stopped while
+/// the call was still streaming (#1218), as opposed to one that finished
+/// streaming but is not a JSON object.
+pub const Refusal = enum { none, malformed, cut };
+
+/// Stop reasons that end a response mid-output. None at all means the stream
+/// ended before its terminal event.
+pub fn responseCut(stop_reason: ?[]const u8) bool {
+ const s = stop_reason orelse return true;
+ return std.mem.eql(u8, s, "max_tokens") or std.mem.eql(u8, s, "length") or
+ std.mem.eql(u8, s, "model_context_window_exceeded");
+}
+
+/// A cut call is refused unless its arguments already close as an object.
+fn refusalFor(parsed: Parsed, raw: []const u8, cut: bool) Refusal {
+ const finished = parsed.valid and std.mem.trim(u8, raw, &std.ascii.whitespace).len > 0;
+ if (cut and !finished) return .cut;
+ return if (parsed.valid) .none else .malformed;
+}
+
/// Anthropic streams `tool_use.input` as partial_json. A fragment that is not
/// a JSON object (a cut-off stream, #1218) used to become `{}` and run as an
/// empty call (`missing or non-string argument: path`). It is stored as `{}`
/// so history replays, with a marker the step removes before refusing it.
const invalid_mark = "graff_invalid_input";
-pub fn putStreamedInput(alloc: Allocator, block: *std.json.ObjectMap, json: []const u8) !void {
+pub fn putStreamedInput(alloc: Allocator, block: *std.json.ObjectMap, json: []const u8, cut: bool) !void {
const parsed = parse(alloc, json);
try block.put(alloc, "input", parsed.input);
- if (!parsed.valid) try block.put(alloc, invalid_mark, .{ .bool = true });
+ const why = refusalFor(parsed, json, cut);
+ if (why != .none) try block.put(alloc, invalid_mark, .{ .string = @tagName(why) });
+}
+
+/// The refusal putStreamedInput (or markChatCut) recorded, with the marker
+/// removed so it is never replayed to the provider.
+pub fn takeInvalidMark(block: *std.json.ObjectMap) Refusal {
+ const kv = block.fetchOrderedRemove(invalid_mark) orelse return .none;
+ return if (kv.value == .string and std.mem.eql(u8, kv.value.string, "cut")) .cut else .malformed;
}
-/// True, with the marker removed, when putStreamedInput flagged this block.
-pub fn takeInvalidMark(block: *std.json.ObjectMap) bool {
- return block.orderedRemove(invalid_mark);
+/// Chat wire: mark an assembled `tool_calls[]` entry the response cut off.
+pub fn markChatCut(alloc: Allocator, call: *std.json.ObjectMap) !void {
+ try call.put(alloc, invalid_mark, .{ .string = "cut" });
}
test "a cut-off Anthropic tool input is refused, not run as {} (#1218)" {
@@ -51,14 +85,29 @@ test "a cut-off Anthropic tool input is refused, not run as {} (#1218)" {
defer arena_state.deinit();
const arena = arena_state.allocator();
var cut: std.json.ObjectMap = .empty;
- try putStreamedInput(arena, &cut, "{\"path\":\"src/ma");
+ try putStreamedInput(arena, &cut, "{\"path\":\"src/ma", false);
try std.testing.expectEqual(@as(usize, 0), cut.get("input").?.object.count());
- try std.testing.expect(takeInvalidMark(&cut));
+ try std.testing.expectEqual(Refusal.malformed, takeInvalidMark(&cut));
try std.testing.expect(cut.get(invalid_mark) == null); // never replayed
var whole: std.json.ObjectMap = .empty;
- try putStreamedInput(arena, &whole, "{\"path\":\"src/main.zig\"}");
- try std.testing.expect(!takeInvalidMark(&whole));
+ try putStreamedInput(arena, &whole, "{\"path\":\"src/main.zig\"}", false);
+ try std.testing.expectEqual(Refusal.none, takeInvalidMark(&whole));
try std.testing.expectEqualStrings("src/main.zig", whole.get("input").?.object.get("path").?.string);
+ // A response that stopped mid-call: nothing streamed, or a fragment, is cut;
+ // arguments that already closed as an object still run.
+ var empty: std.json.ObjectMap = .empty;
+ try putStreamedInput(arena, &empty, "", true);
+ try std.testing.expectEqual(Refusal.cut, takeInvalidMark(&empty));
+ var partial: std.json.ObjectMap = .empty;
+ try putStreamedInput(arena, &partial, "{\"path\":\"src/ma", true);
+ try std.testing.expectEqual(Refusal.cut, takeInvalidMark(&partial));
+ var closed: std.json.ObjectMap = .empty;
+ try putStreamedInput(arena, &closed, "{\"path\":\"src/main.zig\"}", true);
+ try std.testing.expectEqual(Refusal.none, takeInvalidMark(&closed));
+ try std.testing.expect(std.mem.startsWith(u8, cut_exec_message, truncated_prefix));
+ try std.testing.expect(std.mem.startsWith(u8, invalid_exec_message, truncated_prefix));
+ try std.testing.expect(responseCut(null) and responseCut("max_tokens") and responseCut("length"));
+ try std.testing.expect(!responseCut("tool_use") and !responseCut("tool_calls") and !responseCut("end_turn"));
}
/// `gpa` must free (session arena is bump-only). Used only to validate.
diff --git a/src/tool_call_repair.zig b/src/tool_call_repair.zig
index ed766084..5e37a920 100644
--- a/src/tool_call_repair.zig
+++ b/src/tool_call_repair.zig
@@ -440,7 +440,7 @@ fn unusableResult(content: Value) bool {
if (content != .string) return false;
const t = std.mem.trimStart(u8, content.string, " ");
const body = if (std.mem.startsWith(u8, t, "[error] ")) t["[error] ".len..] else t;
- return std.mem.startsWith(u8, body, tool_call_args.invalid_exec_message) or
+ return std.mem.startsWith(u8, body, tool_call_args.truncated_prefix) or
std.mem.startsWith(u8, body, "missing or non-string argument");
}
diff --git a/src/tools.zig b/src/tools.zig
index fd48ed40..3429f3c1 100644
--- a/src/tools.zig
+++ b/src/tools.zig
@@ -26,6 +26,8 @@ pub const ToolCall = struct {
input: Value,
/// False when assembled `arguments` were not a JSON object (#752).
args_ok: bool = true,
+ /// With !args_ok: the response stopped while this call streamed (#1218).
+ cut: bool = false,
};
/// A tool's outcome, arena-owned, ready to wire into either format.