Skip to content

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

Python

fix: require deferred tools before emitting hosted search (#746)

Rach Pradhan61b82ea7fc97
diff --git a/src/agent_request_body_responses.zig b/src/agent_request_body_responses.zig
index 4e31c879..e926cce6 100644
--- a/src/agent_request_body_responses.zig
+++ b/src/agent_request_body_responses.zig
@@ -332,6 +332,35 @@ fn testAgentFor(arena: std.mem.Allocator, id: []const u8, kind: @import("provide
// every provider. The serialization guard in buildBody turns an empty
// catalog string into "omit the field" on all three wires; this pins that
// so no future invalidate-without-rebuild can reach the wire malformed.
+test "#746 emitted Responses body has hosted search only with a deferred tool" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+ for ([_]struct { catalog: []const u8, deferred: bool }{
+ .{ .catalog = "[]", .deferred = false },
+ .{ .catalog = "[{\"type\":\"function\",\"name\":\"bash\"}]", .deferred = false },
+ .{ .catalog = "[{\"type\":\"function\",\"name\":\"webfetch\",\"defer_loading\":false}]", .deferred = false },
+ .{ .catalog = "[{\"type\":\"function\",\"name\":\"webfetch\"}]", .deferred = true },
+ }) |case| {
+ var agent = try testAgentFor(a, "codex", .responses, "gpt-5.6-sol");
+ const body = try agent.buildBody(case.catalog, false, true, true);
+ defer std.testing.allocator.free(body);
+ const parsed = try std.json.parseFromSliceLeaky(Value, a, body, .{});
+ var searches: usize = 0;
+ var deferred = false;
+ for (parsed.object.get("tools").?.array.items) |tool| {
+ if (tool.object.get("type")) |typ| {
+ if (typ == .string and std.mem.eql(u8, typ.string, "tool_search")) searches += 1;
+ }
+ if (tool.object.get("defer_loading")) |flag| {
+ if (flag == .bool and flag.bool) deferred = true;
+ }
+ }
+ try std.testing.expectEqual(case.deferred, deferred);
+ try std.testing.expectEqual(@as(usize, if (case.deferred) 1 else 0), searches);
+ }
+}
+
test "#695: an empty catalog string is omitted from the body, never serialized as tools:, (#695)" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
diff --git a/src/codex_tool_search.zig b/src/codex_tool_search.zig
index 2ed4546f..44ea769e 100644
--- a/src/codex_tool_search.zig
+++ b/src/codex_tool_search.zig
@@ -66,14 +66,28 @@ pub fn splice(arena: Allocator, tools_json: []const u8) ![]const u8 {
var value = std.json.parseFromSliceLeaky(Value, arena, tools_json, .{ .allocate = .alloc_always }) catch return tools_json;
if (value != .array) return tools_json;
var changed = false;
+ var deferred = false;
for (value.array.items) |*tool| {
if (tool.* != .object) continue;
- if (!shouldDefer(toolName(tool.*))) continue;
- if (tool.object.get("defer_loading")) |_| continue;
- try tool.object.put(arena, "defer_loading", .{ .bool = true });
- changed = true;
+ if (shouldDefer(toolName(tool.*)) and !tool.object.contains("defer_loading")) {
+ try tool.object.put(arena, "defer_loading", .{ .bool = true });
+ changed = true;
+ }
+ if (tool.object.get("defer_loading")) |flag| {
+ if (flag == .bool and flag.bool) deferred = true;
+ }
}
- if (!hasType(value.array.items, "tool_search")) {
+ // Hosted search is invalid without a genuinely deferred tool (#746).
+ // Also remove an orphan carried by a previously processed catalog.
+ if (!deferred) {
+ var i: usize = 0;
+ while (i < value.array.items.len) {
+ if (hasType(value.array.items[i .. i + 1], "tool_search")) {
+ _ = value.array.orderedRemove(i);
+ changed = true;
+ } else i += 1;
+ }
+ } else if (!hasType(value.array.items, "tool_search")) {
var obj: std.json.ObjectMap = .empty;
try obj.put(arena, "type", .{ .string = "tool_search" });
try value.array.append(.{ .object = obj });
@@ -101,6 +115,31 @@ pub fn spliceWebSearch(arena: Allocator, tools_json: []const u8) ![]const u8 {
return aw.toOwnedSlice();
}
+test "#746 eager and empty catalogs never acquire hosted tool_search" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ for ([_][]const u8{
+ "[]",
+ "[{\"type\":\"function\",\"name\":\"bash\"}]",
+ "[{\"type\":\"function\",\"name\":\"webfetch\",\"defer_loading\":false}]",
+ "[{\"type\":\"function\",\"name\":\"webfetch\",\"defer_loading\":\"true\"}]",
+ }) |catalog| try std.testing.expectEqualStrings(catalog, try splice(arena.allocator(), catalog));
+}
+
+test "#746 existing deferred flags enable hosted search and orphan search is removed" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const catalog = "[{\"type\":\"function\",\"name\":\"custom\",\"defer_loading\":true}]";
+ const out = try splice(a, catalog);
+ try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, out, "\"type\":\"tool_search\""));
+ try std.testing.expectEqualStrings(out, try splice(a, out));
+ try std.testing.expectEqualStrings("[]", try splice(a, "[{\"type\":\"tool_search\"}]"));
+ const eager = try splice(a, "[{\"type\":\"function\",\"name\":\"bash\"},{\"type\":\"tool_search\"}]");
+ try std.testing.expect(std.mem.indexOf(u8, eager, "tool_search") == null);
+ try std.testing.expect(std.mem.indexOf(u8, eager, "bash") != null);
+}
+
test "modelSupports is gpt-5.4+ / gpt-6, not gpt-5.3" {
try std.testing.expect(modelSupports("gpt-5.4"));
try std.testing.expect(modelSupports("gpt-5.6-sol"));