graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
//! OpenAI hosted `tool_search` on Codex Responses (gpt-5.4+).
//!
//! Folded native tools and MCP names ship with `defer_loading: true` plus a
//! `{"type":"tool_search"}` entry so the model loads schemas at the end of
//! the window (cache-preserving) instead of paying every parameter block up
//! 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;
const Value = std.json.Value;
const Io = std.Io;
const Provider = @import("provider.zig").Provider;
const native_fold = @import("native_fold.zig");
pub fn modelSupports(model: []const u8) bool {
return std.mem.startsWith(u8, model, "gpt-5.4") or
std.mem.startsWith(u8, model, "gpt-5.5") or
std.mem.startsWith(u8, model, "gpt-5.6") or
std.mem.startsWith(u8, model, "gpt-6");
}
pub fn active(provider_id: []const u8, kind: Provider.Kind, model: []const u8) bool {
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") or
std.mem.eql(u8, itype, "web_search_call");
}
fn toolName(tool: Value) []const u8 {
if (tool != .object) return "";
if (tool.object.get("function")) |fn_v| if (fn_v == .object) {
if (fn_v.object.get("name")) |n| if (n == .string) return n.string;
};
if (tool.object.get("name")) |n| if (n == .string) return n.string;
return "";
}
fn shouldDefer(name: []const u8) bool {
if (name.len == 0) return false;
if (native_fold.isFolded(name)) return true;
return std.mem.startsWith(u8, name, "mcp__") or std.mem.startsWith(u8, name, "mcp_");
}
fn hasType(tools: []const Value, typ: []const u8) bool {
for (tools) |tool| {
if (tool != .object) continue;
const t = if (tool.object.get("type")) |v| (if (v == .string) v.string else "") else "";
if (std.mem.eql(u8, t, typ)) return true;
}
return false;
}
/// Mark deferred functions and append hosted `tool_search` once.
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.*)) 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;
}
}
// 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 });
changed = true;
}
if (!changed) return tools_json;
var aw: Io.Writer.Allocating = .init(arena);
var s: std.json.Stringify = .{ .writer = &aw.writer };
try s.write(value);
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 "#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"));
try std.testing.expect(modelSupports("gpt-6-astra"));
try std.testing.expect(!modelSupports("gpt-5.3-codex"));
try std.testing.expect(!active("codex", .openai, "gpt-5.6-sol"));
try std.testing.expect(active("codex", .responses, "gpt-5.6-sol"));
try std.testing.expect(!active("openai", .responses, "gpt-5.6-sol"));
}
test "splice defers folded tools and appends tool_search once" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const a = arena_state.allocator();
const tools =
\\[{"type":"function","name":"bash","description":"run"},{"type":"function","name":"webfetch","description":"web"}]
;
const out = try splice(a, tools);
try std.testing.expect(std.mem.indexOf(u8, out, "\"type\":\"tool_search\"") != null);
try std.testing.expect(std.mem.indexOf(u8, out, "\"name\":\"webfetch\"") != null);
try std.testing.expect(std.mem.indexOf(u8, out, "\"defer_loading\":true") != null);
try std.testing.expect(std.mem.indexOf(u8, out, "\"name\":\"bash\"") != null);
// bash is eager — the only defer_loading is on webfetch.
try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, out, "\"defer_loading\":true"));
const again = try splice(a, out);
try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, again, "\"type\":\"tool_search\""));
}
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\"}]"));
}