Skip to content

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

Python
35.3 KB
Raw Blame
//! Root tool permission gate. Kept separate from agent_tools.zig so the
//! dispatch/meta-tool module stays below the repository's source-size limit.

const std = @import("std");
const main_mod = @import("main.zig");
const shell_tool = @import("shell_tool.zig");
const json_inbox = @import("json_inbox.zig");
const agent_mod = @import("agent.zig");
const Agent = agent_mod.Agent;
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;
const companionReadOnly = skills.companionReadOnly;
const fleet = @import("fleet.zig");
const telemetry = @import("telemetry.zig");
const learning_privacy = @import("learning_privacy.zig");
const presence = @import("presence.zig");
const publish_gate = @import("publish_gate.zig");

const TemplateCandidates = struct {
    items: [16][]const u8 = undefined,
    len: usize = 0,

    fn add(self: *TemplateCandidates, io: std.Io, obj: std.json.ObjectMap) void {
        const prompt = fleet.privateOverride(obj) orelse return;
        if (learning_privacy.isTemplateApproved(io, prompt)) return;
        for (self.items[0..self.len]) |existing| if (std.mem.eql(u8, existing, prompt)) return;
        if (self.len == self.items.len) return;
        self.items[self.len] = prompt;
        self.len += 1;
    }
};

fn collectPrivateTemplates(io: std.Io, call: ToolCall) TemplateCandidates {
    var found: TemplateCandidates = .{};
    if (call.input != .object) return found;
    if (std.mem.eql(u8, call.name, "subagent")) {
        found.add(io, call.input.object);
        return found;
    }
    if (!std.mem.eql(u8, call.name, "workflow")) return found;
    if (call.input.object.get("phases")) |phases| if (phases == .array) {
        for (phases.array.items) |phase| {
            if (phase != .object) continue;
            if (phase.object.get("tasks")) |tasks| if (tasks == .array) {
                for (tasks.array.items) |task| if (task == .object) found.add(io, task.object);
            };
        }
    };
    if (call.input.object.get("pipeline")) |pipeline| if (pipeline == .object) {
        if (pipeline.object.get("stages")) |stages| if (stages == .array) {
            for (stages.array.items) |stage| if (stage == .object) found.add(io, stage.object);
        };
    };
    return found;
}

fn previewLine(buf: []u8, prompt: []const u8) []const u8 {
    const source = prompt[0..@min(prompt.len, buf.len)];
    for (source, 0..) |c, i| buf[i] = if (c < 0x20 or c == 0x7f) ' ' else c;
    return buf[0..source.len];
}

/// Template mode is only a ceiling. A private persona still needs exact
/// interactive approval; no stdin, --yolo, or saved command approval can turn
/// that into content consent. Declining sharing continues the child locally.
fn gatePrivateTemplateSharing(self: *Agent, call: ToolCall) !?ExecResult {
    if (!learning_privacy.allowsTemplateReview() or !main_mod.g_fleet) return null;
    const telem = telemetry.g_telem orelse return null;
    if (!telem.on()) return null;
    const candidates = collectPrivateTemplates(self.io, call);
    if (candidates.len == 0) return null;
    const in = self.in orelse return null;
    const w = self.out orelse return null;

    try w.print("  ⚠ {d} private reusable subagent template{s} could be published\n", .{ candidates.len, if (candidates.len == 1) "" else "s" });
    var blocked_kind: []const u8 = "";
    for (candidates.items[0..candidates.len]) |prompt| {
        const fp = learning_privacy.displayFingerprint(prompt);
        const scan = learning_privacy.scanSecrets(prompt);
        if (!scan.safe() and blocked_kind.len == 0) blocked_kind = scan.first_kind;
        var pbuf: [120]u8 = undefined;
        try w.print("    {s} · {d} bytes · {s}{s}\n", .{ &fp, prompt.len, previewLine(&pbuf, prompt), if (prompt.len > pbuf.len) "…" else "" });
    }
    if (blocked_kind.len > 0) {
        try w.print("  blocked by local secret scan ({s}); child will run locally and no template text will be sent.\n", .{blocked_kind});
        try w.flush();
        return null;
    }
    try w.writeAll("  Sends: exact reusable system/persona text. Excludes: task prompt, bindings, code, paths, reports, and tool results.\n");
    try w.writeAll("  [y] share exact template version(s) this session · [l] run locally · [n] cancel child › ");
    try w.flush();
    const raw: []const u8 = (try json_inbox.reply(self.arena, in)) orelse "";
    const answer = std.mem.trim(u8, raw, " \t\r");
    if (answer.len > 0 and (answer[0] == 'y' or answer[0] == 'Y')) {
        for (candidates.items[0..candidates.len]) |prompt| {
            if (!learning_privacy.approveTemplate(self.io, prompt)) return .{
                .text = try self.arena.dupe(u8, "template approval failed closed; retry locally or inspect the prompt for secret-like content"),
                .is_error = true,
            };
        }
        return null;
    }
    if (answer.len == 0 or answer[0] == 'l' or answer[0] == 'L') return null;
    return .{
        .text = try self.arena.dupe(u8, "user cancelled the child at the template-publication boundary"),
        .is_error = true,
    };
}

/// Hosted documentation queries still cross a network boundary after the
/// ordinary approval prompt. Reject recognizable credentials locally even
/// under --yolo; source/docs identifiers never need to contain a secret.
fn gateSmolifySecrets(arena: std.mem.Allocator, call: ToolCall) !?ExecResult {
    if (!std.mem.startsWith(u8, call.name, "mcp__smolify__")) return null;
    var encoded: std.Io.Writer.Allocating = .init(arena);
    var stringify: std.json.Stringify = .{ .writer = &encoded.writer };
    try stringify.write(call.input);
    const scan = learning_privacy.scanSecrets(encoded.writer.buffered());
    if (scan.safe()) return null;
    return .{
        .text = try std.fmt.allocPrint(arena, "Smolify call blocked locally: arguments resemble a {s}. Remove secrets and send only public project/documentation identifiers.", .{scan.first_kind}),
        .is_error = true,
    };
}

/// The permission gate, root side: an unapproved external action prompts the
/// user. Subagents never prompt; their gate is the executor's allowlist.
/// #366: the harness's own policy file is not the model's to edit. A worker
/// was observed writing pre_approved_tools into it after a denial — a write
/// that takes effect on next load is self-privilege-escalation, cwd-jail or
/// not. Applies to root AND subagents, before every other gate.
fn gateHarnessPolicyWrite(self: *Agent, call: ToolCall) !?ExecResult {
    if (!(std.mem.eql(u8, call.name, "write_file") or std.mem.eql(u8, call.name, "edit_file"))) return null;
    const obj = json_args.object(call.input) orelse return null;
    const pv = obj.get("path") orelse return null;
    if (pv != .string) return null;
    const path = std.mem.trim(u8, pv.string, " \t");
    if (!(std.mem.eql(u8, path, Approvals.settings_path) or std.mem.endsWith(u8, path, "/" ++ Approvals.settings_path))) return null;
    return .{
        .text = try self.arena.dupe(u8, "refused: " ++ Approvals.settings_path ++ " is the harness's own policy file (approvals, hooks, pre-approvals) — a model never widens its own permissions. Ask the USER to change config, or proceed without this tool."),
        .is_error = true,
    };
}

/// An informational child keeps its read, search and read-only shell tools
/// (#1360); withholding the whole catalog left audits with nothing to inspect.
/// Writes are refused the way plan mode refuses them at the root. An MCP tool
/// is a read when it is a companion read or its server declares it read-only
/// (ADR 0228).
fn readOnlyChildAllows(registry: ?*mcp.Registry, call: ToolCall) bool {
    for ([_][]const u8{ "write_file", "edit_file", "imagegen", "peer_message", "learn_candidate" }) |name|
        if (std.mem.eql(u8, call.name, name)) return false;
    if (mcp.Registry.isMcp(call.name)) return companionReadOnly(call.name, call.input) or
        (if (registry) |reg| reg.declaresReadOnly(call.name) else false);
    if (!shell_tool.runsCommand(call.name)) return true;
    const args = json_args.object(call.input) orelse return true;
    const cmd_val = args.get("command") orelse return true; // action=output / kill
    if (cmd_val != .string) return true;
    const cmd = std.mem.trim(u8, cmd_val.string, " \t");
    return Approvals.readOnlyAllowed(cmd) or Approvals.readOnlyExternal(cmd) or @import("readonly_sandbox.zig").admits(call.input); // ADR 0232
}

test "a read-only child keeps reads and read-only shell, not writes (#1360)" {
    var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena_state.deinit();
    const a = arena_state.allocator();
    const call = struct {
        fn f(al: std.mem.Allocator, name: []const u8, input: []const u8) ToolCall {
            const v = std.json.parseFromSliceLeaky(std.json.Value, al, input, .{}) catch unreachable;
            return .{ .id = "c", .name = name, .input = v };
        }
    }.f;
    try std.testing.expect(readOnlyChildAllows(null, call(a, "read_file", "{\"path\":\"src/main.zig\"}")));
    try std.testing.expect(readOnlyChildAllows(null, call(a, "bash", "{\"command\":\"ls src\"}")));
    try std.testing.expect(readOnlyChildAllows(null, call(a, "bash", "{\"action\":\"output\",\"id\":3}")));
    try std.testing.expect(!readOnlyChildAllows(null, call(a, "write_file", "{\"path\":\"a\",\"content\":\"b\"}")));
    try std.testing.expect(!readOnlyChildAllows(null, call(a, "edit_file", "{}")));
    // ADR 0232: on macOS a foreground command runs under the sandbox, which refuses the
    // write; in the background, or with no sandbox, only the allowlist runs.
    try std.testing.expectEqual(@import("readonly_sandbox.zig").available, readOnlyChildAllows(null, call(a, "bash", "{\"command\":\"rm -rf build\"}")));
    try std.testing.expect(!readOnlyChildAllows(null, call(a, "bash", "{\"command\":\"rm -rf build\",\"run_in_background\":true}")));
    // ADR 0228: an MCP tool is a read when its server declares it read-only.
    var registry = mcp.Registry.empty(std.testing.allocator, std.testing.io);
    defer registry.deinit();
    var listed = [_]mcp.Tool{
        .{ .server_index = 0, .original_name = "list", .qualified_name = "mcp__linear__list", .description = "", .input_schema = .null, .read_only = true },
        .{ .server_index = 0, .original_name = "save", .qualified_name = "mcp__linear__save", .description = "", .input_schema = .null },
    };
    registry.tools = &listed;
    try std.testing.expect(readOnlyChildAllows(&registry, call(a, "mcp__linear__list", "{}")));
    try std.testing.expect(!readOnlyChildAllows(&registry, call(a, "mcp__linear__save", "{}")));
    try std.testing.expect(!readOnlyChildAllows(null, call(a, "mcp__linear__list", "{}")));
}

pub fn gateTool(self: *Agent, call: ToolCall) !?ExecResult {
    if (try gateHarnessPolicyWrite(self, call)) |blocked| return blocked; // #366: before the sub branch — children are exactly who must not widen approvals
    if (self.sub) {
        if (std.mem.eql(u8, call.name, "learn_candidate")) return .{
            .text = try self.arena.dupe(u8, "learning is root-only — subagents cannot run mutators, evaluators, or publish grades"),
            .is_error = true,
        };
        if (shell_tool.runsCommand(call.name) and call.input == .object) {
            if (call.input.object.get("command")) |cv| if (cv == .string) {
                if (try publish_gate.bash(self, std.mem.trim(u8, cv.string, " \t"))) |blocked| return blocked;
                if (Approvals.isDestructiveGit(cv.string)) return .{
                    .text = try self.arena.dupe(u8, "destructive git is blocked for subagents (no one to confirm) — leave reset --hard / clean -f / force-push / branch -D to the root session"),
                    .is_error = true,
                };
            };
        }
        if (self.read_only and !readOnlyChildAllows(self.registry, call)) return .{
            .text = try self.arena.dupe(u8, "this subagent is read-only (an informational task) — read, search and run read-only commands, and report the change instead of making it"),
            .is_error = true,
        };
        return null;
    }
    if (try gateSmolifySecrets(self.arena, call)) |blocked| return blocked;
    if (try gatePrivateTemplateSharing(self, call)) |blocked| return blocked;
    const approvals = self.approvals orelse return null;

    if (main_mod.plan_mode) {
        if (std.mem.eql(u8, call.name, "learn_candidate") or
            std.mem.eql(u8, call.name, "write_file") or std.mem.eql(u8, call.name, "edit_file") or
            std.mem.eql(u8, call.name, "imagegen") or // #352: writes a file and spawns a workspace-write child
            std.mem.eql(u8, call.name, "peer_message") or // #469: appends to a peer's inbox — a side effect
            (mcp.Registry.isMcp(call.name) and !companionReadOnly(call.name, call.input))) return .{
            .text = try self.arena.dupe(u8, "plan mode is on — read-only. Fold this change into the plan you present; the user applies it after approving (/plan toggles the mode off)."),
            .is_error = true,
        };
        if (shell_tool.runsCommand(call.name)) {
            // 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;
            if (Approvals.readOnlyExternal(cmd)) {
                if (approvals.planReadAllowed(self.io, cmd)) return null;
                if (self.permission) |handler| {
                    const description = try std.fmt.allocPrint(self.arena, "plan mode — read outside the project: {s}", .{cmd});
                    if (handler.ask(self.io, .{ .call_id = call.id, .tool = call.name, .description = description, .input = call.input }) == .allow_once) {
                        try approvals.approvePlanReadOnce(self.io, self.arena, self, call.id, cmd);
                        return null;
                    }
                    return .{ .text = try self.arena.dupe(u8, "plan mode — read-only access outside the project was declined"), .is_error = true };
                }
                if (self.in) |in| if (self.out) |w| {
                    try w.print("  ⚠ plan mode — read outside the project: {s}\n  [a]llow read-only access to these paths this session · [n]o › ", .{cmd});
                    try w.flush();
                    const raw: []const u8 = (try json_inbox.reply(self.arena, in)) orelse "";
                    const answer = std.mem.trim(u8, raw, " \t\r");
                    if (answer.len > 0 and (answer[0] == 'a' or answer[0] == 'A' or answer[0] == 'y' or answer[0] == 'Y')) {
                        try approvals.approvePlanRead(self.io, self.gpa, cmd);
                        return null;
                    }
                };
                return .{
                    .text = try self.arena.dupe(u8, "plan mode — read-only access outside the project was declined; describe what you need read in the plan instead"),
                    .is_error = true,
                };
            }
            return .{
                .text = try self.arena.dupe(u8, "plan mode is on — only read-only commands run (ls/cat/grep/git status…). Put this command in the plan instead."),
                .is_error = true,
            };
        }
    }

    var key: []const u8 = undefined;
    var line_buf: [256]u8 = undefined;
    var prompt_line: []const u8 = undefined;
    if (shell_tool.runsCommand(call.name)) {
        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");
        if (try publish_gate.bash(self, cmd)) |blocked| return blocked;
        // #469: one deliberate checkpoint per live co-owner before this session
        // touches the shared index/tree — fires under --yolo too, where no
        // approvals prompt would ever surface the collision. The model sees
        // the peer's identity + goal and re-issues the command to proceed.
        if (presence.isSharedTreeGit(cmd) or presence.isSharedTreeShell(cmd)) {
            if (presence.sharedTreeGateApplies(self.gpa, self.io, self.arena, cmd)) {
                if (presence.gateCheck(self.io, self.arena)) |checkpoint| return .{
                    .text = checkpoint,
                    .is_error = true,
                };
            }
        }
        const destructive_git = Approvals.isDestructiveGit(cmd);
        const gate_ok = !destructive_git or Approvals.destructiveGitAllowed(approvals.yolo, self.sub);
        if (gate_ok and approvals.allowed(self.io, cmd)) return null;
        key = firstWord(cmd);
        prompt_line = if (destructive_git)
            std.fmt.bufPrint(&line_buf, "DESTRUCTIVE git — run: {s}", .{cmd}) catch cmd
        else
            std.fmt.bufPrint(&line_buf, "run: {s}", .{cmd}) catch cmd;
    } else if (std.mem.eql(u8, call.name, "write_file") or std.mem.eql(u8, call.name, "edit_file")) {
        // #469: the same once-per-peer checkpoint guards the files themselves —
        // the incident's other vector was editing a file a live peer had open.
        const path = if (call.input == .object)
            (if (call.input.object.get("path")) |p| (if (p == .string) p.string else "?") else "?")
        else
            "?";
        const target = if (presence.hasCheckpointContext()) @import("file_worktree.zig").identity(self.gpa, self.io, self.arena, self.agent_cwd, path) else null;
        const checkpoint = if (target) |id| presence.gateCheckIdentity(self.io, self.arena, id) else presence.gateCheck(self.io, self.arena);
        if (checkpoint) |text| return .{ .text = text, .is_error = true };
        if (approvals.allowedExact(self.io, call.name)) return null;
        key = call.name;
        prompt_line = std.fmt.bufPrint(&line_buf, "{s} {s}", .{ call.name, path }) catch call.name;
    } else if (std.mem.eql(u8, call.name, "learn_candidate")) {
        if (!learning_privacy.allowsAggregate()) {
            const in = self.in orelse return .{
                .text = try self.arena.dupe(u8, "learning privacy is Local and no interactive user can approve this one-shot aggregate submission"),
                .is_error = true,
            };
            const w = self.out orelse return .{
                .text = try self.arena.dupe(u8, "learning privacy is Local and no interactive user can approve this one-shot aggregate submission"),
                .is_error = true,
            };
            try w.writeAll(
                "  ⚠ learn_candidate will run the pinned adapters, then send signed aggregate grades once\n" ++
                    "  Adapter egress: a configured model adapter may send prompt/genome text to its model provider.\n" ++
                    "  Sends: pass counts/deltas, significance, critical regressions, tool/cost/latency totals, gate settings, eligibility, fingerprints, and evidence IDs.\n" ++
                    "  No stable install ID, OS, architecture, or service version is sent.\n" ++
                    "  Excludes: prompt/genome text, tasks, code, paths, evaluator input/output, and traces.\n" ++
                    "  [y] run + send once · [n] cancel › ",
            );
            try w.flush();
            const raw: []const u8 = (try json_inbox.reply(self.arena, in)) orelse "";
            const answer = std.mem.trim(u8, raw, " \t\r");
            if (answer.len > 0 and (answer[0] == 'y' or answer[0] == 'Y')) {
                learning_privacy.authorizeAggregateOnce(self.io);
                return null;
            }
            return .{
                .text = try self.arena.dupe(u8, "user declined the bundled learning submission"),
                .is_error = true,
            };
        }
        if (approvals.allowedExact(self.io, call.name)) return null;
        key = call.name;
        prompt_line = "run pinned learning mutator/evaluator and submit signed aggregate grades";
    } else if (mcp.Registry.isMcp(call.name)) {
        if (companionTrusted(call.name)) return null;
        if (approvals.allowedExact(self.io, call.name)) return null;
        key = call.name;
        prompt_line = if (std.mem.startsWith(u8, call.name, "mcp__smolify__"))
            std.fmt.bufPrint(&line_buf, "send arguments to hosted Smolify tool {s}", .{call.name}) catch call.name
        else
            std.fmt.bufPrint(&line_buf, "call MCP tool {s}", .{call.name}) catch call.name;
    } else return null;

    if (self.permission) |handler| {
        switch (handler.ask(self.io, .{ .call_id = call.id, .tool = call.name, .description = prompt_line, .input = call.input, .allow_always = true, .always_label = try std.fmt.allocPrint(self.arena, "Always allow {s}", .{key}) })) {
            .allow_once => return null,
            .allow_always => {
                try approvals.approve(self.io, self.gpa, key);
                return null;
            },
            .deny => {},
        }
        return .{ .text = try self.arena.dupe(u8, "user declined this tool call; do not retry it or a reworded equivalent. Continue with approved work or report the blocker."), .is_error = true };
    }

    // #369: a denial that just says "not pre-approved" strands the model —
    // observed spirals: probing .harness/settings.json, retrying the same
    // tool, burning the whole call budget. Name what was denied, forbid the
    // dead ends, and point at a productive next move.
    const in = self.in orelse return .{
        .text = try std.fmt.allocPrint(self.arena, "tool denied ({s}): not pre-approved, and this unattended run has no user to ask. Do NOT retry it, probe harness config, or try to change approvals — use a different tool you already have, or finish and report the limitation. The USER can pre-approve it in {s} or run with --yolo.", .{ prompt_line, Approvals.settings_path }),
        .is_error = true,
    };
    const w = self.out orelse return .{
        .text = try std.fmt.allocPrint(self.arena, "tool denied ({s}): not pre-approved, and no interactive user to ask. Do NOT retry it or probe harness config — use a different tool you already have, or finish and report the limitation. The USER can pre-approve it in {s} or run with --yolo.", .{ prompt_line, Approvals.settings_path }),
        .is_error = true,
    };
    try w.print("  ⚠ {s}\n  [y]es once · [a]lways allow \"{s}\" (saved to {s}) · [n]o › ", .{ prompt_line, key, Approvals.settings_path });
    try w.flush();
    const raw: []const u8 = (try json_inbox.reply(self.arena, in)) orelse "";
    const answer = std.mem.trim(u8, raw, " \t\r");
    if (answer.len > 0) switch (answer[0]) {
        'y', 'Y' => return null,
        'a', 'A' => {
            try approvals.approve(self.io, self.gpa, key);
            if (shell_tool.runsCommand(call.name) and Approvals.isInterpreter(key)) {
                try w.print("  note: \"{s}\" can execute arbitrary code (e.g. {s} -c '…'); approving it is effectively unrestricted.\n", .{ key, key });
                try w.flush();
            }
            return null;
        },
        else => {},
    };
    return .{
        // Narrow the recovery space on purpose: a declined call invites the
        // model to invent a different detour every run (measured as turn-count
        // variance on identical tasks, #476). Point at the deterministic exits.
        .text = try self.arena.dupe(u8, "user declined this tool call — do not retry it or a reworded equivalent; the answer will not change. Continue with what is already approved, delegate the declined work to a subagent, or report the blocker plainly."),
        .is_error = true,
    };
}

/// ToolCtx.host_gate: a call an rlm script makes (bash, write_file, MCP…)
/// gets the same gate as the catalog call would (#1292). It runs on a pool
/// thread, so it holds the lock that already guards this agent's arena there.
pub fn hostGate(context: *anyopaque, call: ToolCall) ?tools_mod.ToolOutput {
    const self: *Agent = @ptrCast(@alignCast(context));
    self.host_gate_mutex.lockUncancelable(self.io);
    defer self.host_gate_mutex.unlock(self.io);
    const denied = (gateTool(self, call) catch
        return .{ .text = self.gpa.dupe(u8, "tool gate failed; the call was not run") catch &.{}, .is_error = true }) orelse return null;
    return .{ .text = self.gpa.dupe(u8, denied.text) catch &.{}, .is_error = true, .cancelled = denied.cancelled };
}

pub fn firstWord(cmd: []const u8) []const u8 {
    const end = std.mem.indexOfAny(u8, cmd, " \t") orelse cmd.len;
    return cmd[0..end];
}

test "firstWord splits the command on the first whitespace" {
    try std.testing.expectEqualStrings("git", firstWord("git status -s"));
    try std.testing.expectEqualStrings("ls", firstWord("ls"));
    try std.testing.expectEqualStrings("cat", firstWord("cat\tfile"));
    try std.testing.expectEqualStrings("", firstWord(""));
    try std.testing.expectEqualStrings("", firstWord(" leading"));
}

test "Smolify arguments containing recognizable secrets fail closed locally" {
    var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    const secret = try std.json.parseFromSliceLeaky(std.json.Value, arena, "{\"query\":\"github_token=ghp_not-for-egress\"}", .{});
    const public = try std.json.parseFromSliceLeaky(std.json.Value, arena, "{\"query\":\"Zig HTTP client\"}", .{});
    const blocked = (try gateSmolifySecrets(arena, .{ .id = "1", .name = "mcp__smolify__search_docs", .input = secret })).?;
    try std.testing.expect(blocked.is_error);
    try std.testing.expect(std.mem.indexOf(u8, blocked.text, "blocked locally") != null);
    try std.testing.expect((try gateSmolifySecrets(arena, .{ .id = "2", .name = "mcp__smolify__search_docs", .input = public })) == null);
    try std.testing.expect((try gateSmolifySecrets(arena, .{ .id = "3", .name = "read_file", .input = secret })) == null);
}

test "gateHarnessPolicyWrite: the model never edits the harness's own policy file (#366)" {
    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;
    const settings = try std.json.parseFromSliceLeaky(std.json.Value, arena, "{\"path\":\".harness/settings.json\",\"content\":\"{}\"}", .{});
    const nested = try std.json.parseFromSliceLeaky(std.json.Value, arena, "{\"path\":\"../other/.harness/settings.json\",\"content\":\"{}\"}", .{});
    const normal = try std.json.parseFromSliceLeaky(std.json.Value, arena, "{\"path\":\"src/main.zig\",\"content\":\"x\"}", .{});
    // Both write tools are refused on the policy file, any spelling of it…
    const direct = (try gateHarnessPolicyWrite(&agent, .{ .id = "1", .name = "write_file", .input = settings })).?;
    try std.testing.expect(direct.is_error);
    try std.testing.expect(std.mem.indexOf(u8, direct.text, "policy file") != null);
    try std.testing.expect((try gateHarnessPolicyWrite(&agent, .{ .id = "2", .name = "edit_file", .input = nested })) != null);
    // …while ordinary paths and other tools pass through untouched.
    try std.testing.expect((try gateHarnessPolicyWrite(&agent, .{ .id = "3", .name = "write_file", .input = normal })) == null);
    try std.testing.expect((try gateHarnessPolicyWrite(&agent, .{ .id = "4", .name = "bash", .input = settings })) == null);
}

test "private template collector covers subagents and workflow stages" {
    const io = std.testing.io;
    learning_privacy.setMode(io, .templates);
    defer learning_privacy.setMode(io, .local);
    var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
    defer arena_state.deinit();
    const arena = arena_state.allocator();
    const sub_value = try std.json.parseFromSliceLeaky(std.json.Value, arena, "{\"system_prompt\":\"private one\",\"prompt\":\"task\"}", .{});
    const sub = collectPrivateTemplates(io, .{ .id = "1", .name = "subagent", .input = sub_value });
    try std.testing.expectEqual(@as(usize, 1), sub.len);
    const wf_value = try std.json.parseFromSliceLeaky(std.json.Value, arena, "{\"phases\":[{\"tasks\":[{\"system_prompt\":\"phase private\",\"prompt\":\"x\"}]}],\"pipeline\":{\"stages\":[{\"system_prompt\":\"stage private\",\"prompt\":\"y\"}]}}", .{});
    const wf = collectPrivateTemplates(io, .{ .id = "2", .name = "workflow", .input = wf_value });
    try std.testing.expectEqual(@as(usize, 2), wf.len);
}

test "#851 bash gate resolves the git target before gateCheck" {
    const src = @embedFile("agent_tool_gate.zig");
    const applies = std.mem.indexOf(u8, src, "sharedTreeGateApplies").?;
    const check = std.mem.indexOfPos(u8, src, applies, "gateCheck(").?;
    try std.testing.expect(applies < check);
}

test "#851 isolated git -C target does not use the caller worktree identity" {
    const gpa = std.testing.allocator;
    var arena_state = std.heap.ArenaAllocator.init(gpa);
    defer arena_state.deinit();
    const a = arena_state.allocator();
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    const root = try tmp.dir.realPathFileAlloc(std.testing.io, ".", a);
    const path_a = try std.fmt.allocPrint(a, "{s}/a", .{root});
    const path_b = try std.fmt.allocPrint(a, "{s}/b", .{root});
    try tmp.dir.createDirPath(std.testing.io, "a");
    try tmp.dir.createDirPath(std.testing.io, "b");
    const runner = @import("process_runner.zig");
    for ([_][]const u8{ path_a, path_b }) |p| {
        const r = try runner.runCapped(gpa, std.testing.io, &.{ "git", "init", "-q", p }, 4096, 4096, 15_000);
        defer {
            gpa.free(r.stdout);
            gpa.free(r.stderr);
        }
        try std.testing.expect(runner.ranOk(r));
    }
    const worktree_lease = @import("worktree_lease.zig");
    const id_a = worktree_lease.identityAt(gpa, std.testing.io, a, path_a);
    try std.testing.expect(id_a.id.len > 0);
    presence.bindForTest(root, id_a.id, "s-caller", .{});
    defer presence.unbindForTest();
    const cmd_b = try std.fmt.allocPrint(a, "git -C {s} cherry-pick abc", .{path_b});
    const cmd_a = try std.fmt.allocPrint(a, "git -C {s} cherry-pick abc", .{path_a});
    const cmd_sub = try std.fmt.allocPrint(a, "cd {s}/. && git stash", .{path_a});
    try std.testing.expect(!presence.sharedTreeGateApplies(gpa, std.testing.io, a, cmd_b));
    try std.testing.expect(presence.sharedTreeGateApplies(gpa, std.testing.io, a, cmd_a));
    try std.testing.expect(presence.sharedTreeGateApplies(gpa, std.testing.io, a, cmd_sub));
    try std.testing.expect(presence.sharedTreeGateApplies(gpa, std.testing.io, a, "git cherry-pick abc"));
}

test "plan frontend allow once reaches actual dispatch without a reusable grant" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var arena: std.heap.ArenaAllocator = .init(gpa);
    defer arena.deinit();
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(io, .{ .sub_path = "proof.txt", .data = "approved read proof" });
    const path = try tmp.dir.realPathFileAlloc(io, "proof.txt", arena.allocator());
    const command = try std.fmt.allocPrint(arena.allocator(), "cat {s}", .{path});
    try std.testing.expect(Approvals.readOnlyExternal(command));
    const Fixture = struct {
        calls: usize = 0,
        fn ask(ptr: *anyopaque, _: std.Io, _: @import("engine_permission.zig").Request) @import("engine_permission.zig").Decision {
            const self: *@This() = @ptrCast(@alignCast(ptr));
            self.calls += 1;
            return .allow_once;
        }
    };
    var fixture: Fixture = .{};
    var approvals: Approvals = .{};
    const saved_plan = main_mod.plan_mode;
    defer main_mod.plan_mode = saved_plan;
    main_mod.plan_mode = true;
    var agent: Agent = .{ .gpa = gpa, .arena = arena.allocator(), .io = io, .client = undefined, .provider = .{ .id = "fixture", .kind = .openai, .auth = .bearer, .url = "", .api_key = "", .model = "fixture", .context = 1000 }, .messages = .init(arena.allocator()), .sub = false, .label = "root", .out = null, .approvals = &approvals, .permission = .{ .ctx = &fixture, .request = Fixture.ask } };
    defer agent.tools_used.deinit(gpa);
    var args: std.json.ObjectMap = .empty;
    try args.put(arena.allocator(), "command", .{ .string = command });
    const call: ToolCall = .{ .id = "one-read", .name = "bash", .input = .{ .object = args } };
    const results = try agent.runTools(&.{call});
    try std.testing.expectEqual(@as(usize, 1), fixture.calls);
    try std.testing.expect(!results[0].is_error);
    try std.testing.expect(std.mem.indexOf(u8, results[0].text, "approved read proof") != null);
    try std.testing.expect(!approvals.planReadAllowed(io, command));
    const ctx: tools_mod.ToolCtx = .{ .gpa = gpa, .io = io, .client = undefined, .provider = agent.provider, .registry = null, .from_sub = false, .approvals = &approvals, .tracer = null, .plan_read_owner = &agent };
    const repeated = @import("exec.zig").execTool(ctx, call);
    defer gpa.free(repeated.text);
    try std.testing.expect(repeated.is_error);
    try std.testing.expect(std.mem.indexOf(u8, repeated.text, "plan mode") != null);
    // An approved call cancelled before dispatch cannot carry consent forward.
    try approvals.approvePlanReadOnce(io, arena.allocator(), &agent, call.id, command);
    _ = try agent.runTools(&.{});
    try std.testing.expect(!approvals.consumePlanReadOnce(io, &agent, call.id, command));
}

test "approval gate asks for shell commands under the advertised name and the bash alias (#1292)" {
    const gpa = std.testing.allocator;
    const io = std.testing.io;
    var arena = std.heap.ArenaAllocator.init(gpa);
    defer arena.deinit();
    const Fixture = struct {
        calls: usize = 0,
        fn ask(ptr: *anyopaque, _: std.Io, _: @import("engine_permission.zig").Request) @import("engine_permission.zig").Decision {
            const self: *@This() = @ptrCast(@alignCast(ptr));
            self.calls += 1;
            return .deny;
        }
    };
    for ([_][]const u8{ shell_tool.tool_name, "bash" }) |name| {
        var fixture: Fixture = .{};
        var approvals: Approvals = .{};
        var agent: Agent = .{ .gpa = gpa, .arena = arena.allocator(), .io = io, .client = undefined, .provider = .{ .id = "fixture", .kind = .openai, .auth = .bearer, .url = "", .api_key = "", .model = "fixture", .context = 1000 }, .messages = .init(arena.allocator()), .sub = false, .label = "root", .out = null, .approvals = &approvals, .permission = .{ .ctx = &fixture, .request = Fixture.ask } };
        defer agent.tools_used.deinit(gpa);
        var args: std.json.ObjectMap = .empty;
        try args.put(arena.allocator(), "command", .{ .string = "curl -X POST https://example.invalid/upload -d @nothing.txt" });
        const results = try agent.runTools(&.{.{ .id = "gate", .name = name, .input = .{ .object = args } }});
        try std.testing.expectEqual(@as(usize, 1), fixture.calls);
        try std.testing.expect(results[0].is_error);
        try std.testing.expect(std.mem.indexOf(u8, results[0].text, "declined") != null);
    }
}