graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
//! Root/worker tool-catalog materialization. Split out of agent.zig (600-line ceiling).
const std = @import("std");
const Allocator = std.mem.Allocator;
const main_mod = @import("main.zig");
const schema = @import("schema.zig");
const no_local_tools = @import("no_local_tools.zig");
const surface = @import("tool_surface.zig");
const mcp = @import("mcp.zig");
const Provider = @import("provider.zig").Provider;
const Agent = @import("agent.zig").Agent;
const local_tools = @import("local_tools.zig");
const schedule = @import("schedule.zig");
const jev_tool = @import("jev_tool.zig");
fn withExtras(arena: Allocator, base: []const schema.ToolSpec, provider: Provider) ![]const schema.ToolSpec {
const a = local_tools.catalogExtras(arena);
const b = schedule.catalogExtras(arena);
const c = jev_tool.catalogExtras(provider);
if (a.len == 0 and b.len == 0 and c.len == 0) return base;
const out = try arena.alloc(schema.ToolSpec, base.len + a.len + b.len + c.len);
@memcpy(out[0..base.len], base);
@memcpy(out[base.len..][0..a.len], a);
@memcpy(out[base.len + a.len ..][0..b.len], b);
@memcpy(out[base.len + a.len + b.len ..], c);
return out;
}
/// ADR 0231: with no human to answer (`-p`, a piped session outside
/// --json/ACP) the root does not offer ask_user.
fn withoutAskUser(self: *const Agent, specs: []const schema.ToolSpec) ![]const schema.ToolSpec {
if (!@import("ask_user.zig").g_no_human) return specs;
var kept: std.ArrayList(schema.ToolSpec) = .empty;
for (specs) |spec| if (!std.mem.eql(u8, spec.name, "ask_user")) try kept.append(self.arena, spec);
return kept.items;
}
pub fn toolsJson(self: *const Agent) []const u8 {
if (self.sub) {
if (self.worker_tools.len != 0) return self.worker_tools; // ADR 0227
if (main_mod.g_codedbpro_licensed) return slot(self);
return schema.subToolsJson(self.provider.kind, no_local_tools.enabled);
}
return slot(self);
}
fn slot(self: *const Agent) []const u8 {
return switch (self.provider.kind) {
.anthropic => self.tools_anthropic,
.openai => self.tools_openai,
.responses => self.tools_responses,
.interactions => self.tools_interactions,
};
}
/// A registry or restored selection change invalidates every provider format.
/// The active format is rebuilt immediately; inactive formats remain lazy.
pub fn invalidateRootTools(self: *Agent) void {
if (self.sub) return;
@import("prompt_cache_hud.zig").noteBust(.tools);
self.tools_anthropic = "";
self.tools_openai = "";
self.tools_responses = "";
self.tools_interactions = "";
}
pub fn refreshMcp(self: *Agent) bool {
if (self.sub) return false;
const registry = self.registry orelse return false;
if (!registry.catalog_dirty.swap(false, .acq_rel)) return false;
invalidateRootTools(self);
return true;
}
pub fn ensureRootTools(self: *Agent, kind: Provider.Kind) !void {
if (self.sub and !main_mod.g_codedbpro_licensed) return;
_ = refreshMcp(self);
const dest = switch (kind) {
.anthropic => &self.tools_anthropic,
.openai => &self.tools_openai,
.responses => &self.tools_responses,
.interactions => &self.tools_interactions,
};
if (dest.*.len != 0) return;
const specs = if (self.sub)
try surface.filterSpecs(@TypeOf(schema.base_specs[0]), self.arena, schema.base_specs[0..])
else
try withExtras(self.arena, try withoutAskUser(self, try schema.effectiveRootSpecs(self.arena)), self.provider);
const connected: []const mcp.Tool = if (self.registry) |registry| blk: {
const snapshot = try registry.snapshotTools(self.arena);
break :blk if (self.sub) try surface.filterWorkerMcp(self.arena, snapshot) else snapshot;
} else &.{};
const rendered = try @import("shell_tool.zig").withPython(self.arena, try schema.renderRootTools(self.arena, kind, specs, connected)); // ADR 0242
dest.* = try @import("gemini_turns.zig").withCompletionBatch(self.arena, rendered, self.provider.model); // ADR 0256
}
test "invalidate without ensure leaves toolsJson empty; rebuild is a JSON array" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
var agent: Agent = undefined;
agent.arena = arena;
agent.sub = false;
agent.registry = null;
agent.tools_anthropic = "";
agent.tools_openai = "";
agent.tools_responses = "";
agent.provider = .{
.id = "xai",
.kind = .responses,
.auth = .bearer,
.url = "",
.api_key = "k",
.model = "grok-4.6",
.context = 100_000,
};
try agent.ensureRootTools(.responses);
try std.testing.expect(agent.tools_responses.len > 2);
agent.invalidateRootTools();
try std.testing.expectEqual(@as(usize, 0), agent.toolsJson().len);
try agent.ensureRootTools(.responses);
try std.testing.expect(agent.tools_responses.len > 2);
const parsed = try std.json.parseFromSlice(std.json.Value, std.testing.allocator, agent.tools_responses, .{});
defer parsed.deinit();
try std.testing.expect(parsed.value == .array);
try std.testing.expect(parsed.value.array.items.len > 0);
}
test "ask_user is offered only when a human can answer (ADR 0231)" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
const ask = @import("ask_user.zig");
const saved = ask.g_no_human;
defer ask.g_no_human = saved;
var agent: Agent = undefined;
agent.arena = arena;
agent.sub = false;
agent.registry = null;
agent.tools_responses = "";
agent.provider = .{ .id = "xai", .kind = .responses, .auth = .bearer, .url = "", .api_key = "k", .model = "grok-4.6", .context = 100_000 };
ask.g_no_human = true;
try agent.ensureRootTools(.responses);
try std.testing.expect(std.mem.indexOf(u8, agent.tools_responses, "\"ask_user\"") == null);
ask.g_no_human = false;
agent.tools_responses = "";
try agent.ensureRootTools(.responses);
try std.testing.expect(std.mem.indexOf(u8, agent.tools_responses, "\"ask_user\"") != null);
}