graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Anthropic: refuse a cut-off tool input instead of running it as {} (refs #1218)
justrach8762410695a0
diff --git a/src/agent_steps.zig b/src/agent_steps.zigindex 0a91757c..05b33154 100644--- a/src/agent_steps.zig+++ b/src/agent_steps.zig@@ -178,8 +178,8 @@ pub fn stepAnthropic(self: *Agent, root: std.json.ObjectMap) !?[]const u8 {var calls: std.ArrayList(ToolCall) = .empty;defer calls.deinit(self.gpa);var final_text: []const u8 = "";- for (content.array.items) |block| {- if (block != .object) continue;+ for (content.array.items) |*block| { // by pointer: the invalid-input marker is removed in place+ if (block.* != .object) continue;const kind = if (block.object.get("type")) |t| (if (t == .string) t.string else "") else "";if (std.mem.eql(u8, kind, "text")) {if (block.object.get("text")) |tx| if (tx == .string) {@@ -190,8 +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 input = block.object.get("input") orelse Value{ .object = .empty };- try calls.append(self.gpa, .{ .id = id, .name = name, .input = input });+ try calls.append(self.gpa, .{ .id = id, .name = name, .input = input, .args_ok = args_ok });}}self.pairContextMeterWithCurrentLocal();@@ -426,10 +427,7 @@ pub fn assembleAnthropic(self: *Agent, body: []const u8) !?std.json.ObjectMap {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) {- const input = std.json.parseFromSliceLeaky(Value, scratch, b.json.items, .{ .allocate = .alloc_always }) catch Value{ .object = .empty };- try b.obj.put(scratch, "input", input);- }+ if (b.json.items.len > 0) try @import("tool_call_args.zig").putStreamedInput(scratch, &b.obj, b.json.items); // #1218try content.append(.{ .object = b.obj });}try r.put(scratch, "content", .{ .array = content });diff --git a/src/tool_call_args.zig b/src/tool_call_args.zigindex 9345227f..adb3f23f 100644--- a/src/tool_call_args.zig+++ b/src/tool_call_args.zig@@ -29,6 +29,38 @@ pub fn parse(alloc: Allocator, s: []const u8) Parsed {return .{ .input = value, .valid = true };}+/// 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 {+ const parsed = parse(alloc, json);+ try block.put(alloc, "input", parsed.input);+ if (!parsed.valid) try block.put(alloc, invalid_mark, .{ .bool = true });+}++/// True, with the marker removed, when putStreamedInput flagged this block.+pub fn takeInvalidMark(block: *std.json.ObjectMap) bool {+ return block.orderedRemove(invalid_mark);+}++test "a cut-off Anthropic tool input is refused, not run as {} (#1218)" {+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const arena = arena_state.allocator();+ var cut: std.json.ObjectMap = .empty;+ try putStreamedInput(arena, &cut, "{\"path\":\"src/ma");+ try std.testing.expectEqual(@as(usize, 0), cut.get("input").?.object.count());+ try std.testing.expect(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 std.testing.expectEqualStrings("src/main.zig", whole.get("input").?.object.get("path").?.string);+}+/// `gpa` must free (session arena is bump-only). Used only to validate.pub fn isObjectString(gpa: Allocator, s: []const u8) bool {const t = std.mem.trim(u8, s, &std.ascii.whitespace);