Skip to content

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

Python

fix(gate): guard the root permission gate's tool args; harden the compact prelude

Rach Pradhan4614374b9974
diff --git a/src/agent_compact.zig b/src/agent_compact.zig
index 83db7667..42161a68 100644
--- a/src/agent_compact.zig
+++ b/src/agent_compact.zig
@@ -88,19 +88,28 @@ pub fn summaryResponseComplete(self: *Agent, root: std.json.ObjectMap) bool {
/// actual proof that compact() is wired to call pinChildTask: delete the call
/// below and the compactPrelude test goes red, whereas pinChildTask's own
/// isolated tests do not notice.
-pub fn compactPrelude(self: *Agent) bool {
- if (self.messages.items.len == 0) return false;
+/// Everything compact() must do before it touches the network, returning the
+/// token figure the caller reports. Null means there is nothing to compact.
+///
+/// It returns that figure DELIBERATELY. An earlier version returned bool and
+/// only pinned, which left the call re-inlineable: deleting one line from
+/// compact() restored a plausible-looking empty-history check and a compacting
+/// child silently lost its task prompt again, with the suite fully green. Now
+/// compact() cannot drop the call without losing the number it prints, so the
+/// pin cannot be orphaned by a tidy-up that looks harmless.
+pub fn compactPrelude(self: *Agent) ?usize {
+ if (self.messages.items.len == 0) return null;
pinChildTask(self);
- return true;
+ self.last_request_context_overflow = false;
+ return self.effectiveContextTokens();
}
pub fn compact(self: *Agent) anyerror!usize {
- if (!compactPrelude(self)) {
+ const pending_tokens = compactPrelude(self) orelse {
if (!main_mod.json_mode) try self.say("nothing to compact\n", .{});
return 0;
- }
- self.last_request_context_overflow = false;
- if (!main_mod.json_mode) try self.say("[compacting ~{d} tokens…]\n", .{self.effectiveContextTokens()});
+ };
+ if (!main_mod.json_mode) try self.say("[compacting ~{d} tokens…]\n", .{pending_tokens});
// #163: reclaim room BEFORE the summarization request so it fits under the
// model's input cap. On codex/gpt-5.x an over-cap request fails to WRITE
// (WriteFailed) rather than returning a clean overflow, so compaction could
diff --git a/src/agent_compact_test_support.zig b/src/agent_compact_test_support.zig
index dd32e614..a09301eb 100644
--- a/src/agent_compact_test_support.zig
+++ b/src/agent_compact_test_support.zig
@@ -24,6 +24,19 @@ pub fn subAgent(a: std.mem.Allocator, sub: bool) Agent {
agent.todos = .empty;
agent.goal = null;
agent.task_prompt = null;
+ // compactPrelude reports the token figure compact() prints, so the estimate
+ // path has to be reachable: system prompt, tool json, provider context and
+ // the two meter anchors it reads.
+ agent.sys_override = null;
+ agent.sys_normal = "";
+ agent.sys_strict = "";
+ agent.strict = false;
+ agent.tools_anthropic = "";
+ agent.tools_openai = "";
+ agent.tools_responses = "";
+ agent.last_context_tokens = 0;
+ agent.context_local_tokens = 0;
+ agent.provider = .{ .id = "test", .kind = .anthropic, .auth = .x_api_key, .url = "", .api_key = "k", .model = "m", .context = 100_000 };
return agent;
}
@@ -60,10 +73,13 @@ test "compactPrelude actually pins a subagent's task - the wiring compact() depe
const a = arena_state.allocator();
var agent = subAgent(a, true);
agent.messages = try msg1(a, "user", "TASK X");
- try std.testing.expect(compact.compactPrelude(&agent));
+ // The prelude returns the token figure compact() prints, so the call
+ // cannot be dropped without losing that number - which is what stops the
+ // pin being orphaned by a plausible-looking tidy-up (G2).
+ try std.testing.expect(compact.compactPrelude(&agent) != null);
try std.testing.expectEqualStrings("TASK X", agent.task_prompt.?);
var empty = subAgent(a, true);
empty.messages = std.json.Array.init(a);
- try std.testing.expect(!compact.compactPrelude(&empty));
+ try std.testing.expect(compact.compactPrelude(&empty) == null);
}
diff --git a/src/agent_tool_gate.zig b/src/agent_tool_gate.zig
index f64eb6c3..539f0424 100644
--- a/src/agent_tool_gate.zig
+++ b/src/agent_tool_gate.zig
@@ -9,6 +9,7 @@ const tools_mod = @import("tools.zig");
const ToolCall = tools_mod.ToolCall;
const ExecResult = tools_mod.ExecResult;
const mcp = @import("mcp.zig");
+const json_args = @import("json_args.zig");
const Approvals = @import("approvals.zig").Approvals;
const skills = @import("skills.zig");
const companionTrusted = skills.companionTrusted;
@@ -152,7 +153,11 @@ pub fn gateTool(self: *Agent, call: ToolCall) !?ExecResult {
.is_error = true,
};
if (std.mem.eql(u8, call.name, "bash")) {
- const cmd_val = call.input.object.get("command") orelse return null;
+ // Model-supplied: a non-object here is UB in a ReleaseFast build,
+ // and this gate is ROOT-only, so the subagent-path guards added in
+ // v0.0.223 never covered it.
+ const args = json_args.object(call.input) orelse return null;
+ const cmd_val = args.get("command") orelse return null;
if (cmd_val != .string) return null;
const cmd = std.mem.trim(u8, cmd_val.string, " \t");
if (Approvals.readOnlyAllowed(cmd)) return null;
@@ -184,7 +189,8 @@ pub fn gateTool(self: *Agent, call: ToolCall) !?ExecResult {
var line_buf: [256]u8 = undefined;
var prompt_line: []const u8 = undefined;
if (std.mem.eql(u8, call.name, "bash")) {
- const cmd_val = call.input.object.get("command") orelse return null;
+ const args = json_args.object(call.input) orelse return null; // model-supplied; UB in ReleaseFast without the tag check
+ const cmd_val = args.get("command") orelse return null;
if (cmd_val != .string) return null;
const cmd = std.mem.trim(u8, cmd_val.string, " \t");
const destructive_git = Approvals.isDestructiveGit(cmd);
diff --git a/src/workflow_test.zig b/src/workflow_test.zig
index bff016b6..b5e2d1e0 100644
--- a/src/workflow_test.zig
+++ b/src/workflow_test.zig
@@ -11,6 +11,7 @@ const Value = std.json.Value;
const workflow = @import("workflow.zig");
const subagent = @import("subagent.zig");
const subagent_run = @import("subagent_run.zig");
+const json_args = @import("json_args.zig");
const tools = @import("tools.zig");
const util = @import("util.zig");
const ToolCtx = tools.ToolCtx;
@@ -209,3 +210,19 @@ test "a 5xx outage is transient, so a fan-out keeps retrying it (U1 regression g
for ([_][]const u8{ "model unavailable", "no such model", "model_not_found" }) |detail|
try std.testing.expectEqual(subagent_run.FailKind.model, subagent_run.classifyFailure(error.Unexpected, detail));
}
+
+test "the ROOT permission gate refuses a malformed bash call instead of dereferencing it" {
+ // gateTool is root-only (subagents take the early return at its top), so
+ // the json_args guards added across the workflow/subagent paths in v0.0.223
+ // never covered it. A model that sends bash arguments as a bare string or
+ // array reaches this gate on the ordinary single-agent path.
+ for ([_]std.json.Value{ .{ .string = "rm -rf /" }, .null, .{ .integer = 7 } }) |bad|
+ try std.testing.expect(json_args.object(bad) == null);
+ // The shape the gate actually wants still resolves, so the guard is not
+ // simply refusing everything.
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const good = try std.json.parseFromSliceLeaky(Value, arena_state.allocator(), "{\"command\":\"ls\"}", .{});
+ const ok = json_args.object(good) orelse return error.TestExpectedNonNull;
+ try std.testing.expectEqualStrings("ls", ok.get("command").?.string);
+}