Skip to content

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

Python

fix: xAI server compact (ADR 0002), SuperGrok web_search, Codex tool_search

Rach Pradhan581acd80f0e6
diff --git a/src/agent_request_body_responses.zig b/src/agent_request_body_responses.zig
index cae659cc..a82b2c43 100644
--- a/src/agent_request_body_responses.zig
+++ b/src/agent_request_body_responses.zig
@@ -11,6 +11,7 @@ const http_headers = @import("http_headers.zig");
const codex_chain = @import("codex_chain.zig");
const server_compact = @import("agent_server_compact.zig");
const xai_hosted = @import("xai_hosted.zig");
+const codex_tool_search = @import("codex_tool_search.zig");
pub fn write(self: *Agent, s: *std.json.Stringify, tools: ?[]const u8, force_tool: bool) !void {
// Responses API (codex / ChatGPT, xAI, and native Codegraff aliases).
@@ -58,10 +59,17 @@ pub fn write(self: *Agent, s: *std.json.Stringify, tools: ?[]const u8, force_too
for (self.messages.items[from..]) |m| try s.write(m);
try s.endArray();
if (tools) |t| {
- const payload = if (xai_hosted.active(self.provider.id, self.provider.kind))
- xai_hosted.splice(self.scratchAlloc(), t) catch t
- else
- t;
+ const payload = blk: {
+ var next = t;
+ if (xai_hosted.active(self.provider.id, self.provider.kind)) {
+ const login = self.provider.source == .login;
+ next = xai_hosted.splice(self.scratchAlloc(), next, login) catch next;
+ }
+ if (codex_tool_search.active(self.provider.id, self.provider.kind, self.provider.model)) {
+ next = codex_tool_search.splice(self.scratchAlloc(), next) catch next;
+ }
+ break :blk next;
+ };
try s.objectField("tools");
try serde.writeOpenAITools(s, self.scratchAlloc(), payload); // #261 follow-up
try s.objectField("tool_choice");
@@ -419,11 +427,19 @@ test "xAI Responses splices hosted x_search onto a tools turn; Codex and chat do
defer std.testing.allocator.free(body);
try std.testing.expect(std.mem.indexOf(u8, body, "\"type\":\"x_search\"") != null);
try std.testing.expect(std.mem.indexOf(u8, body, "\"name\":\"bash\"") != null);
+ try std.testing.expect(std.mem.indexOf(u8, body, "web_search") == null);
+
+ var login = try testAgentFor(a, "xai", .responses, "grok-4.6");
+ login.provider.source = .login;
+ const login_body = try login.buildBody(tools, false, true, true);
+ defer std.testing.allocator.free(login_body);
+ try std.testing.expect(std.mem.indexOf(u8, login_body, "\"type\":\"web_search\"") != null);
var codex = try testAgentFor(a, "codex", .responses, "gpt-5.6-sol");
const cb = try codex.buildBody(tools, false, true, true);
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);
var chat = try testAgentFor(a, "xai", .openai, "grok-4.6");
const chat_body = try chat.buildBody(tools, false, true, true);
diff --git a/src/agent_server_compact.zig b/src/agent_server_compact.zig
index 15ed0e73..b551a9b4 100644
--- a/src/agent_server_compact.zig
+++ b/src/agent_server_compact.zig
@@ -39,7 +39,8 @@ pub var g_server_compact_override: ?bool = null;
/// Is the server-compaction path active for this provider?
pub fn enabled(p: Provider) bool {
- if (manualRoute(p) == .local) return false;
+ if (p.kind != .responses) return false;
+ if (manualRoute(p) == .local and p.serverCompactUrl() == null) return false;
return g_server_compact_override orelse true;
}
diff --git a/src/agent_server_compact_tests.zig b/src/agent_server_compact_tests.zig
index a962f673..bb9c99c7 100644
--- a/src/agent_server_compact_tests.zig
+++ b/src/agent_server_compact_tests.zig
@@ -134,6 +134,11 @@ test "enabled: responses-only; server is the default; override wins" {
try std.testing.expect(enabled(p));
p.id = "kilo"; // third-party Responses hosts keep the local summary
try std.testing.expect(!enabled(p));
+ p.id = "xai";
+ try std.testing.expect(enabled(p)); // ADR 0002: explicit /responses/compact
+ p.kind = .openai;
+ try std.testing.expect(!enabled(p));
+ p.kind = .responses;
p.id = "codex";
asc.g_server_compact_override = false;
try std.testing.expect(!enabled(p));
diff --git a/src/agent_steps.zig b/src/agent_steps.zig
index 0c10afd0..0b3ac57d 100644
--- a/src/agent_steps.zig
+++ b/src/agent_steps.zig
@@ -84,7 +84,9 @@ pub fn stepResponses(self: *Agent, response: std.json.ObjectMap) !?[]const u8 {
if (item != .object) continue;
try self.messages.append(item); // valid as next-turn input
const itype = if (item.object.get("type")) |t| (if (t == .string) t.string else "") else "";
- if (std.mem.eql(u8, itype, "message")) {
+ if (@import("codex_tool_search.zig").isServerSideItem(itype)) {
+ continue;
+ } else if (std.mem.eql(u8, itype, "message")) {
if (item.object.get("content")) |c| if (c == .array) {
for (c.array.items) |block| {
if (block != .object) continue;
diff --git a/src/codex_tool_search.zig b/src/codex_tool_search.zig
new file mode 100644
index 00000000..0329a4d7
--- /dev/null
+++ b/src/codex_tool_search.zig
@@ -0,0 +1,114 @@
+//! 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.
+
+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 fn isServerSideItem(itype: []const u8) bool {
+ return std.mem.eql(u8, itype, "tool_search_call") or std.mem.eql(u8, itype, "tool_search_output");
+}
+
+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;
+ 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 (!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();
+}
+
+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("function_call"));
+}
diff --git a/src/main.zig b/src/main.zig
index fcc0321d..426c49c5 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -586,6 +586,7 @@ test { // pull in tests from imported modules (mcp.zig)
_ = @import("test_hooks.zig"); // unreached modules; their tests were silently skipped
_ = @import("agent_overflow_tests.zig"); // #414: and, through it, agent_overflow.zig's table tests
_ = @import("agent_server_compact.zig"); // server-side autocompact (codex Responses)
+ _ = @import("codex_tool_search.zig"); // hosted tool_search on gpt-5.4+ Codex
_ = @import("acp_preauth.zig"); // credential-free ACP loop must stay in the test root
_ = @import("task_outcome.zig"); // goal-outcome telemetry events
_ = @import("learn_delete.zig"); // #303: its tests were dead until listed here
diff --git a/src/session_settings.zig b/src/session_settings.zig
index 67b922e9..c005dddd 100644
--- a/src/session_settings.zig
+++ b/src/session_settings.zig
@@ -215,6 +215,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.enabled = !off;
}
+ if (environ_map.get("GRAFF_XAI_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");
+ xai_hosted.web_search = !off;
+ }
if (environ_map.get("GRAFF_XAI_URL")) |v| {
if (v.len > 0) provider_mod.g_xai_url_override = v;
}
diff --git a/src/xai_hosted.zig b/src/xai_hosted.zig
index 6cd3d046..d7897b91 100644
--- a/src/xai_hosted.zig
+++ b/src/xai_hosted.zig
@@ -1,10 +1,12 @@
-//! Hosted xAI tools on the Responses wire (`{"type":"x_search"}`).
+//! Hosted xAI tools on the Responses wire (`{"type":"x_search"}`, and
+//! `web_search` on a SuperGrok login).
//!
//! Not a graff catalog function and not on the always-on prefix (ADR 0011 /
//! 0013). xAI runs the search server-side; graff must never local-exec the
-//! resulting `custom_tool_call` / `x_search_call`. Chat-completions and
-//! non-xAI Responses providers never see this splice.
-//! `GRAFF_XAI_X_SEARCH=0|off|false|no` turns the default off.
+//! resulting `custom_tool_call` / `x_search_call` / `web_search_call`.
+//! Chat-completions and non-xAI Responses providers never see this splice.
+//! `GRAFF_XAI_X_SEARCH=0|off|false|no` turns x_search off.
+//! `GRAFF_XAI_WEB_SEARCH=0|off|false|no` turns web_search off.
const std = @import("std");
const Allocator = std.mem.Allocator;
@@ -15,34 +17,49 @@ const Provider = @import("provider.zig").Provider;
/// Test / env seam. `applyEnvKnobs` sets this from GRAFF_XAI_X_SEARCH.
pub var enabled: bool = true;
+/// Allowed on SuperGrok login only. API-key turns keep graff `webfetch`.
+pub var web_search: bool = true;
pub fn active(provider_id: []const u8, kind: Provider.Kind) bool {
return enabled and kind == .responses and std.mem.eql(u8, provider_id, "xai");
}
-/// Append `{"type":"x_search"}` when the tools array does not already carry it.
+fn hasHosted(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;
+}
+
+fn appendHosted(arena: Allocator, tools: *std.json.Array, typ: []const u8) !void {
+ var obj: std.json.ObjectMap = .empty;
+ try obj.put(arena, "type", .{ .string = typ });
+ try tools.append(.{ .object = obj });
+}
+
+/// Append hosted xAI tools the array does not already carry.
/// Invalid JSON is returned unchanged so the existing writer path still fires.
-pub fn splice(arena: Allocator, tools_json: []const u8) ![]const u8 {
+pub fn splice(arena: Allocator, tools_json: []const u8, grok_login: bool) ![]const u8 {
var value = std.json.parseFromSliceLeaky(Value, arena, tools_json, .{ .allocate = .alloc_always }) catch return tools_json;
if (value != .array) return tools_json;
- for (value.array.items) |tool| {
- if (hostedType(tool)) return tools_json;
+ var changed = false;
+ if (enabled and !hasHosted(value.array.items, "x_search")) {
+ try appendHosted(arena, &value.array, "x_search");
+ changed = true;
}
- var obj: std.json.ObjectMap = .empty;
- try obj.put(arena, "type", .{ .string = "x_search" });
- try value.array.append(.{ .object = obj });
+ if (web_search and grok_login and !hasHosted(value.array.items, "web_search")) {
+ try appendHosted(arena, &value.array, "web_search");
+ 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();
}
-fn hostedType(tool: Value) bool {
- if (tool != .object) return false;
- const t = if (tool.object.get("type")) |v| (if (v == .string) v.string else "") else "";
- return std.mem.eql(u8, t, "x_search");
-}
-
/// Server-executed search items: keep them in history, never dispatch locally.
pub fn isServerSideCall(itype: []const u8, name: []const u8) bool {
if (std.mem.eql(u8, itype, "x_search_call") or std.mem.eql(u8, itype, "web_search_call"))
@@ -50,6 +67,7 @@ pub fn isServerSideCall(itype: []const u8, name: []const u8) bool {
if (!std.mem.eql(u8, itype, "custom_tool_call") and !std.mem.eql(u8, itype, "function_call"))
return false;
return std.mem.eql(u8, name, "x_search") or
+ std.mem.eql(u8, name, "web_search") or
std.mem.eql(u8, name, "x_keyword_search") or
std.mem.eql(u8, name, "x_semantic_search") or
std.mem.eql(u8, name, "x_user_search") or
@@ -74,30 +92,41 @@ test "splice appends hosted x_search once" {
defer arena_state.deinit();
const arena = arena_state.allocator();
const tools = "[{\"type\":\"function\",\"name\":\"bash\",\"parameters\":{\"type\":\"object\"}}]";
- const out = try splice(arena, tools);
+ const out = try splice(arena, tools, false);
try std.testing.expect(std.mem.indexOf(u8, out, "\"type\":\"x_search\"") != null);
try std.testing.expect(std.mem.indexOf(u8, out, "\"name\":\"bash\"") != null);
- const again = try splice(arena, out);
+ try std.testing.expect(std.mem.indexOf(u8, out, "web_search") == null);
+ const again = try splice(arena, out, false);
try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, again, "\"type\":\"x_search\""));
}
test "splice of an empty tools array is just x_search" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
- const out = try splice(arena_state.allocator(), "[]");
+ const out = try splice(arena_state.allocator(), "[]", false);
try std.testing.expectEqualStrings("[{\"type\":\"x_search\"}]", out);
}
+test "SuperGrok login also splices hosted web_search" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const out = try splice(arena_state.allocator(), "[]", true);
+ try std.testing.expect(std.mem.indexOf(u8, out, "\"type\":\"x_search\"") != null);
+ try std.testing.expect(std.mem.indexOf(u8, out, "\"type\":\"web_search\"") != null);
+}
+
test "splice leaves invalid JSON alone" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
- try std.testing.expectEqualStrings("not-json", try splice(arena_state.allocator(), "not-json"));
+ try std.testing.expectEqualStrings("not-json", try splice(arena_state.allocator(), "not-json", false));
}
test "isServerSideCall matches the live xAI shapes and ignores bash" {
try std.testing.expect(isServerSideCall("custom_tool_call", "x_keyword_search"));
try std.testing.expect(isServerSideCall("x_search_call", ""));
+ try std.testing.expect(isServerSideCall("web_search_call", ""));
try std.testing.expect(isServerSideCall("function_call", "x_search"));
+ try std.testing.expect(isServerSideCall("function_call", "web_search"));
try std.testing.expect(!isServerSideCall("function_call", "bash"));
try std.testing.expect(!isServerSideCall("function_call", "webfetch"));
try std.testing.expect(!isServerSideCall("message", "x_search"));