Skip to content

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

Python

feat: splice hosted web_search onto Codex Responses

Rach Pradhanbf88d08cf610
diff --git a/src/agent_request_body.zig b/src/agent_request_body.zig
index 091d837c..d231470f 100644
--- a/src/agent_request_body.zig
+++ b/src/agent_request_body.zig
@@ -448,7 +448,7 @@ test "retained reasoning: codex full resend keeps encrypted reasoning items and
try std.testing.expect(std.mem.indexOf(u8, body, "\"parameters\":{\"type\":\"object\"}") != null); // #261 follow-up: repaired on the responses wire too
// Ask the backend to hand reasoning back encrypted...
- try std.testing.expect(std.mem.indexOf(u8, body, "\"include\":[\"reasoning.encrypted_content\"]") != null);
+ try std.testing.expect(std.mem.indexOf(u8, body, "\"include\":[\"reasoning.encrypted_content\"") != null);
// ...and send the prior turn's reasoning item straight back. store:false
// means the server keeps nothing, so this resend IS the retention.
try std.testing.expect(std.mem.indexOf(u8, body, "\"encrypted_content\":\"ENCBLOB\"") != null);
diff --git a/src/agent_request_body_responses.zig b/src/agent_request_body_responses.zig
index a82b2c43..542cc4bb 100644
--- a/src/agent_request_body_responses.zig
+++ b/src/agent_request_body_responses.zig
@@ -67,6 +67,7 @@ pub fn write(self: *Agent, s: *std.json.Stringify, tools: ?[]const u8, force_too
}
if (codex_tool_search.active(self.provider.id, self.provider.kind, self.provider.model)) {
next = codex_tool_search.splice(self.scratchAlloc(), next) catch next;
+ next = codex_tool_search.spliceWebSearch(self.scratchAlloc(), next) catch next;
}
break :blk next;
};
@@ -94,6 +95,7 @@ pub fn write(self: *Agent, s: *std.json.Stringify, tools: ?[]const u8, force_too
try s.objectField("include");
try s.beginArray();
try s.write("reasoning.encrypted_content");
+ if (is_codex and codex_tool_search.web_search) try s.write("web_search_call.action.sources");
try s.endArray();
try s.objectField("store");
try s.write(false);
@@ -440,6 +442,8 @@ test "xAI Responses splices hosted x_search onto a tools turn; Codex and chat do
defer std.testing.allocator.free(cb);
try std.testing.expect(std.mem.indexOf(u8, cb, "x_search") == null);
try std.testing.expect(std.mem.indexOf(u8, cb, "\"type\":\"tool_search\"") != null);
+ try std.testing.expect(std.mem.indexOf(u8, cb, "\"type\":\"web_search\"") != null);
+ try std.testing.expect(std.mem.indexOf(u8, cb, "web_search_call.action.sources") != null);
var chat = try testAgentFor(a, "xai", .openai, "grok-4.6");
const chat_body = try chat.buildBody(tools, false, true, true);
diff --git a/src/codex_tool_search.zig b/src/codex_tool_search.zig
index 0329a4d7..2ed4546f 100644
--- a/src/codex_tool_search.zig
+++ b/src/codex_tool_search.zig
@@ -6,6 +6,9 @@
//! front. Always-on loop tools (bash, files, codedb, …) stay eager.
//! `tool_search_call` / `tool_search_output` are server-executed — never
//! dispatched locally.
+//!
+//! Hosted `web_search` is spliced onto the same Codex tools array (probed live
+//! on chatgpt.com/backend-api/codex/responses). `GRAFF_CODEX_WEB_SEARCH=0` opts out.
const std = @import("std");
const Allocator = std.mem.Allocator;
@@ -26,8 +29,12 @@ pub fn active(provider_id: []const u8, kind: Provider.Kind, model: []const u8) b
return kind == .responses and std.mem.eql(u8, provider_id, "codex") and modelSupports(model);
}
+pub var web_search: bool = true;
+
pub fn isServerSideItem(itype: []const u8) bool {
- return std.mem.eql(u8, itype, "tool_search_call") or std.mem.eql(u8, itype, "tool_search_output");
+ return std.mem.eql(u8, itype, "tool_search_call") or
+ std.mem.eql(u8, itype, "tool_search_output") or
+ std.mem.eql(u8, itype, "web_search_call");
}
fn toolName(tool: Value) []const u8 {
@@ -79,6 +86,21 @@ pub fn splice(arena: Allocator, tools_json: []const u8) ![]const u8 {
return aw.toOwnedSlice();
}
+/// Append hosted `web_search` once. Codex accepted this live on gpt-5.6-sol.
+pub fn spliceWebSearch(arena: Allocator, tools_json: []const u8) ![]const u8 {
+ if (!web_search) return tools_json;
+ var value = std.json.parseFromSliceLeaky(Value, arena, tools_json, .{ .allocate = .alloc_always }) catch return tools_json;
+ if (value != .array) return tools_json;
+ if (hasType(value.array.items, "web_search")) return tools_json;
+ var obj: std.json.ObjectMap = .empty;
+ try obj.put(arena, "type", .{ .string = "web_search" });
+ try value.array.append(.{ .object = obj });
+ var aw: Io.Writer.Allocating = .init(arena);
+ var s: std.json.Stringify = .{ .writer = &aw.writer };
+ try s.write(value);
+ return aw.toOwnedSlice();
+}
+
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"));
@@ -110,5 +132,19 @@ test "splice defers folded tools and appends tool_search once" {
test "isServerSideItem covers hosted search items" {
try std.testing.expect(isServerSideItem("tool_search_call"));
try std.testing.expect(isServerSideItem("tool_search_output"));
+ try std.testing.expect(isServerSideItem("web_search_call"));
try std.testing.expect(!isServerSideItem("function_call"));
}
+
+test "spliceWebSearch appends hosted web_search once" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ const out = try spliceWebSearch(a, "[{\"type\":\"function\",\"name\":\"bash\"}]");
+ try std.testing.expect(std.mem.indexOf(u8, out, "\"type\":\"web_search\"") != null);
+ try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, try spliceWebSearch(a, out), "\"type\":\"web_search\""));
+ const saved = web_search;
+ defer web_search = saved;
+ web_search = false;
+ try std.testing.expectEqualStrings("[{\"type\":\"function\",\"name\":\"bash\"}]", try spliceWebSearch(a, "[{\"type\":\"function\",\"name\":\"bash\"}]"));
+}
diff --git a/src/session_settings.zig b/src/session_settings.zig
index c005dddd..183d23d0 100644
--- a/src/session_settings.zig
+++ b/src/session_settings.zig
@@ -219,6 +219,10 @@ pub fn applyEnvKnobs(arena: Allocator, environ_map: anytype) !void {
const off = std.mem.eql(u8, v, "0") or std.ascii.eqlIgnoreCase(v, "false") or std.ascii.eqlIgnoreCase(v, "off") or std.ascii.eqlIgnoreCase(v, "no");
xai_hosted.web_search = !off;
}
+ if (environ_map.get("GRAFF_CODEX_WEB_SEARCH")) |v| {
+ const off = std.mem.eql(u8, v, "0") or std.ascii.eqlIgnoreCase(v, "false") or std.ascii.eqlIgnoreCase(v, "off") or std.ascii.eqlIgnoreCase(v, "no");
+ @import("codex_tool_search.zig").web_search = !off;
+ }
if (environ_map.get("GRAFF_XAI_URL")) |v| {
if (v.len > 0) provider_mod.g_xai_url_override = v;
}