Skip to content

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

Python

Keep a hosted-search answer instead of bouncing it as a described change (#1786)

justrach17544d95bf7a
diff --git a/src/agent_empty_completion.zig b/src/agent_empty_completion.zig
index 511f9a18..a108237a 100644
--- a/src/agent_empty_completion.zig
+++ b/src/agent_empty_completion.zig
@@ -166,6 +166,8 @@ fn handleWithWait(self: *Agent, final_text: []const u8, hist_len: usize, comptim
if (@import("task_intent.zig").current(self) == .informational) return false;
if (@import("exact_reply.zig").requested(messages.latestUserText(self.messages.items))) return false;
if (no_local_tools.enabled) return false;
+ // #1786: an answer built on the provider's own searches did its work this completion.
+ if (@import("xai_hosted.zig").ranServerSideSearch(self.messages.items[@min(hist_len, self.messages.items.len)..])) return false;
// #1518: a question answered in text is done, and a retry the model-call
// budget cannot pay for turns a valid answer into a budget failure.
if (!@import("task_intent.zig").requestsAction(messages.latestUserText(self.messages.items))) return false;
@@ -278,9 +280,10 @@ pub const PendingWork = struct {
test {
_ = @import("run_idle.zig");
+ _ = @import("agent_empty_completion_tests.zig");
}
-fn pendingFixture(arena: std.mem.Allocator) Agent {
+pub fn pendingFixture(arena: std.mem.Allocator) Agent {
var self: Agent = undefined;
self.arena = arena;
self.io = std.testing.io;
@@ -493,40 +496,6 @@ test "exact reply bypasses only fake done while mixed requests and empty replies
}
}
-test "#1518 lean bounce skips questions and a spent model-call budget" {
- var state = std.heap.ArenaAllocator.init(std.testing.allocator);
- defer state.deinit();
- const old_unattended = main_mod.unattended;
- const old_lean = no_local_tools.lean;
- const old_enabled = no_local_tools.enabled;
- main_mod.unattended = true;
- no_local_tools.lean = true;
- no_local_tools.enabled = false;
- defer {
- main_mod.unattended = old_unattended;
- no_local_tools.lean = old_lean;
- no_local_tools.enabled = old_enabled;
- }
- var budget: @import("run_budget.zig").RunBudget = .{ .max_model_calls = 1 };
- budget.model_calls.store(1, .release);
- const cases = .{
- .{ "What does parse() return for an empty string?", false, false },
- .{ "Fix the off-by-one in fib.py", true, false },
- .{ "Fix the off-by-one in fib.py", false, true },
- };
- inline for (cases) |case| {
- var self = pendingFixture(state.allocator());
- self.empty_completion_retries = 0;
- self.call_kind = .title; // suppress fixture notices without initializing a writer
- self.tool_calls_this_turn = 0;
- self.model_calls_this_turn = 1;
- if (case[1]) self.run_budget = &budget;
- try self.messages.append(try messages.textMessage(self.arena, "user", case[0]));
- const before = self.messages.items.len;
- try std.testing.expectEqual(case[2], try handle(&self, "It returns an empty list.", before));
- }
-}
-
test "#1582 an informational question does not resume an earlier task's untouched checklist" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
diff --git a/src/agent_empty_completion_tests.zig b/src/agent_empty_completion_tests.zig
new file mode 100644
index 00000000..d97839b9
--- /dev/null
+++ b/src/agent_empty_completion_tests.zig
@@ -0,0 +1,77 @@
+//! Lean `-p` bounce cases, split from agent_empty_completion.zig (600-line goal).
+
+const std = @import("std");
+const main_mod = @import("main.zig");
+const no_local_tools = @import("no_local_tools.zig");
+const messages = @import("messages.zig");
+const ec = @import("agent_empty_completion.zig");
+
+const LeanRun = struct {
+ unattended: bool,
+ lean: bool,
+ enabled: bool,
+
+ fn begin() LeanRun {
+ const saved: LeanRun = .{ .unattended = main_mod.unattended, .lean = no_local_tools.lean, .enabled = no_local_tools.enabled };
+ main_mod.unattended = true;
+ no_local_tools.lean = true;
+ no_local_tools.enabled = false;
+ return saved;
+ }
+
+ fn end(saved: LeanRun) void {
+ main_mod.unattended = saved.unattended;
+ no_local_tools.lean = saved.lean;
+ no_local_tools.enabled = saved.enabled;
+ }
+};
+
+test "#1518 lean bounce skips questions and a spent model-call budget" {
+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer state.deinit();
+ const saved = LeanRun.begin();
+ defer saved.end();
+ var budget: @import("run_budget.zig").RunBudget = .{ .max_model_calls = 1 };
+ budget.model_calls.store(1, .release);
+ const cases = .{
+ .{ "What does parse() return for an empty string?", false, false },
+ .{ "Fix the off-by-one in fib.py", true, false },
+ .{ "Fix the off-by-one in fib.py", false, true },
+ };
+ inline for (cases) |case| {
+ var self = ec.pendingFixture(state.allocator());
+ self.empty_completion_retries = 0;
+ self.call_kind = .title; // suppress fixture notices without initializing a writer
+ self.tool_calls_this_turn = 0;
+ self.model_calls_this_turn = 1;
+ if (case[1]) self.run_budget = &budget;
+ try self.messages.append(try messages.textMessage(self.arena, "user", case[0]));
+ const before = self.messages.items.len;
+ try std.testing.expectEqual(case[2], try ec.handle(&self, "It returns an empty list.", before));
+ }
+}
+
+test "#1786 an answer built on the provider's own searches is not bounced as a described change" {
+ var state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer state.deinit();
+ const saved = LeanRun.begin();
+ defer saved.end();
+ // "build" makes this read as an action request; the searches did the work.
+ const prompt = "Search posts from the people who build the app and report what they said, with links.";
+ const answer = "Three posts: ...";
+ for ([_]?[]const u8{ "{\"type\":\"x_search_call\",\"status\":\"completed\"}", null }) |search| {
+ var self = ec.pendingFixture(state.allocator());
+ self.empty_completion_retries = 0;
+ self.call_kind = .title;
+ self.tool_calls_this_turn = 0;
+ self.model_calls_this_turn = 1;
+ try self.messages.append(try messages.textMessage(self.arena, "user", prompt));
+ const before = self.messages.items.len;
+ if (search) |raw| try self.messages.append(try std.json.parseFromSliceLeaky(std.json.Value, self.arena, raw, .{}));
+ try self.messages.append(try messages.textMessage(self.arena, "assistant", answer));
+ const after = self.messages.items.len;
+ // With a hosted search the answer stands; with none, the bounce still fires.
+ try std.testing.expectEqual(search == null, try ec.handle(&self, answer, before));
+ try std.testing.expectEqual(after + @intFromBool(search == null), self.messages.items.len);
+ }
+}
diff --git a/src/xai_hosted.zig b/src/xai_hosted.zig
index d7897b91..d403f35f 100644
--- a/src/xai_hosted.zig
+++ b/src/xai_hosted.zig
@@ -74,6 +74,35 @@ pub fn isServerSideCall(itype: []const u8, name: []const u8) bool {
std.mem.eql(u8, name, "x_thread_fetch");
}
+/// True when these output items carry a search the provider ran itself. Those
+/// calls never count as local tool calls, so a reply built on them would
+/// otherwise look like a bare text answer.
+pub fn ranServerSideSearch(items: []const Value) bool {
+ for (items) |item| {
+ if (item != .object) continue;
+ const itype = if (item.object.get("type")) |t| (if (t == .string) t.string else "") else "";
+ const name = if (item.object.get("name")) |n| (if (n == .string) n.string else "") else "";
+ if (isServerSideCall(itype, name)) return true;
+ }
+ return false;
+}
+
+test "ranServerSideSearch finds hosted search items, not local calls or text" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ for ([_][]const u8{
+ "[{\"type\":\"x_search_call\"},{\"type\":\"message\"}]",
+ "[{\"type\":\"web_search_call\"}]",
+ "[{\"type\":\"custom_tool_call\",\"name\":\"x_keyword_search\"}]",
+ }) |raw| {
+ const v = try std.json.parseFromSliceLeaky(Value, a, raw, .{});
+ try std.testing.expect(ranServerSideSearch(v.array.items));
+ }
+ const local = try std.json.parseFromSliceLeaky(Value, a, "[{\"type\":\"function_call\",\"name\":\"shell\"},{\"type\":\"message\"}]", .{});
+ try std.testing.expect(!ranServerSideSearch(local.array.items));
+}
+
test "active only for xAI Responses while enabled" {
const saved = enabled;
defer enabled = saved;