Skip to content

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

Python

fix(prompt): one-shots learn the REAL approval map up front, not by trial

Rach Pradhan3b2fa3af0edf
diff --git a/src/main.zig b/src/main.zig
index 787ed1ec..adc9c0e2 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -167,7 +167,7 @@ const skills = @import("skills.zig");
const skills_registry = skills.skills_registry;
const companion_servers = skills.companion_servers;
const mcpServerConnected = skills.mcpServerConnected;
-const probeCodedbproLicensed = skills.probeCodedbproLicensed;
+const probeLicensedPinEager = session_start.probeLicensedPinEager; // probe + #416 eager pin in one startup step
/// PATH captured at startup for skill detection (PATH won't change mid-run).
pub var g_path_env: []const u8 = "";
/// Human-facing current workspace folder shown in the REPL prompt.
@@ -195,7 +195,7 @@ pub var g_skill_disabled: [skills_registry.len]bool = @splat(false);
/// Same opt-out, for the metered companion MCP servers (codedb-pro) — they live in companion_servers, NOT skills_registry, so need their own flags.
pub var g_companion_disabled: [companion_servers.len]bool = @splat(false);
/// True when `codedb-pro probe` exits 0 (paid + usable); set once at startup after the companion connects, selecting the licensed vs conservative note.
-var g_codedbpro_licensed: bool = false;
+pub var g_codedbpro_licensed: bool = false; // pub: codedbpro_report's native-tool gate consults it per call
// Thinking animations: spinner animations + color themes (+ settings persistence) live in anim.zig; spinner consumers (Agent.spinnerTask, /animation, /theme) stay here.
const anim = @import("anim.zig");
// Steering (Codex-style): bytes typed while a turn streams are captured (not discarded), echoed live in dim emerald, and queued to run next on Enter —
@@ -392,7 +392,7 @@ pub fn main(init: std.process.Init) !void {
try session_start.connectCompanion(io, arena, &registry_storage, flags, out, json_mode, init.environ_map);
const mcp_tools: []const mcp.Tool = registry_storage.tools;
// If the metered companion connected, probe its license once so the note below can lean into paid tools (vs the conservative free-codedb note).
- if (mcpServerConnected(mcp_tools, "codedbpro")) g_codedbpro_licensed = probeCodedbproLicensed(gpa, io);
+ if (mcpServerConnected(mcp_tools, "codedbpro")) g_codedbpro_licensed = probeLicensedPinEager(gpa, arena, io);
boot.mark(io, "companion");
var approvals: Approvals = undefined;
@@ -408,7 +408,7 @@ pub fn main(init: std.process.Init) !void {
// Root system-prompt layering (base + AGENTS.md/HARNESS.md/CLAUDE.md + --append-system-prompt + active-skill lines + connected-MCP notes) lives
// in startup.zig as buildSystemPrompt() — pure over io/arena, returns the composed base by value. buildRootAgent derives every prompt variant
// from it via prompts.setSystemPrompts() (#326).
- const sys_normal = try startup.buildSystemPrompt(io, arena, out, flags.system_prompt_flag, flags.append_system_flag, json_mode or flags.oneshot_prompt != null, mcp_tools, g_codedbpro_licensed, init.environ_map.get("GRAFF_LEARNED_PROMPT"), init.environ_map);
+ const sys_normal = try startup.buildSystemPrompt(io, arena, out, flags.system_prompt_flag, flags.append_system_flag, json_mode or flags.oneshot_prompt != null, flags.oneshot_prompt != null and !flags.yolo_flag, mcp_tools, g_codedbpro_licensed, init.environ_map.get("GRAFF_LEARNED_PROMPT"), init.environ_map);
boot.mark(io, "system prompt");
session_run.learningNotice(io, arena, init.environ_map, out, json_mode or flags.oneshot_prompt != null);
diff --git a/src/prompt_snapshot_tests.zig b/src/prompt_snapshot_tests.zig
index 84651543..7924c736 100644
--- a/src/prompt_snapshot_tests.zig
+++ b/src/prompt_snapshot_tests.zig
@@ -552,3 +552,26 @@ test "#391/#445: the prompt funnel never arms the note store in a test build" {
// the pure string function the rest of this suite depends on.
try std.testing.expect(!prompts.compactNotesArmed());
}
+
+test "unattended one-shots are told the REAL approval map up front; attended sessions hear nothing" {
+ var a_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer a_state.deinit();
+ const a = a_state.allocator();
+ const startup = @import("startup.zig");
+ const saved_img = imagegen.available;
+ defer imagegen.available = saved_img;
+ var env = std.process.Environ.Map.init(std.testing.allocator);
+ defer env.deinit();
+ const Io = std.Io;
+ var aw: Io.Writer.Allocating = .init(a);
+ const attended = try startup.buildSystemPrompt(std.testing.io, a, &aw.writer, null, null, true, false, &.{}, false, null, env);
+ try std.testing.expect(std.mem.indexOf(u8, attended, "AUTO-DENIED") == null);
+ var aw2: Io.Writer.Allocating = .init(a);
+ const unattended = try startup.buildSystemPrompt(std.testing.io, a, &aw2.writer, null, null, true, true, &.{}, false, null, env);
+ try std.testing.expect(std.mem.indexOf(u8, unattended, "AUTO-DENIED") != null);
+ // The map the denial text alone could not teach: the root is gated,
+ // subagents are the ungated path, and --yolo/settings are the user's.
+ try std.testing.expect(std.mem.indexOf(u8, unattended, "Subagents are NOT approval-gated") != null);
+ try std.testing.expect(std.mem.indexOf(u8, unattended, "--yolo") != null);
+ try std.testing.expect(std.mem.indexOf(u8, unattended, ".harness/settings.json") != null);
+}
diff --git a/src/prompts.zig b/src/prompts.zig
index aea0e13b..4a936024 100644
--- a/src/prompts.zig
+++ b/src/prompts.zig
@@ -80,6 +80,18 @@ pub const main_system_prompt = blk: {
break :blk out;
};
+/// One-shot (-p) sessions have no user to approve tool calls. The gate's REAL
+/// map (agent_tool_gate.zig): at the ROOT, bash/edit_file/write_file and
+/// unapproved MCP are auto-denied; SUBAGENTS skip the approval gate entirely
+/// (only destructive git and learn_candidate are blocked for them). Without
+/// this note the model learns the map by trial: measured benchmark flips
+/// between "subagent detour, task done" and "root edit denied, task
+/// abandoned" on identical input — plus a retry tax of failed calls first.
+/// Telling it UP FRONT makes the delegation deterministic. Appended by
+/// startup.buildSystemPrompt only when the session is unattended AND not
+/// --yolo (yolo pre-approves everything, so the note would be a lie there).
+pub const unattended_note = "\n\nThis session is non-interactive: there is no user to approve tool calls, so YOUR approval-gated tools — bash, edit_file, write_file, unapproved MCP calls — are AUTO-DENIED at the root. Never retry a denial; it will not change. Subagents are NOT approval-gated: delegate implementation and command execution to a subagent (it can edit files and run tests itself), then report its result plainly. The user can also re-run with --yolo, or pre-approve tools in .harness/settings.json, to let the root act directly.";
+
/// #421: what this session can actually do, exactly as the tool catalog
/// reports it. Defaults are "everything", so a caller that only cares about
/// one gate names one field.
diff --git a/src/startup.zig b/src/startup.zig
index b93a1867..ddaea416 100644
--- a/src/startup.zig
+++ b/src/startup.zig
@@ -325,6 +325,7 @@ pub fn buildSystemPrompt(
system_prompt_flag: ?[]const u8,
append_system_flag: ?[]const u8,
quiet: bool,
+ unattended: bool,
mcp_tools: []const mcp.Tool,
codedbpro_licensed: bool,
learned_policy_env: ?[]const u8,
@@ -353,6 +354,7 @@ pub fn buildSystemPrompt(
if (append_system_flag) |extra| {
sys_normal = try std.fmt.allocPrint(arena, "{s}\n\n{s}", .{ sys_normal, extra });
}
+ if (unattended) sys_normal = try std.fmt.allocPrint(arena, "{s}{s}", .{ sys_normal, prompts.unattended_note });
// Codex-style skills: one capability line per installed optional
// companion (skills_registry) — metadata in context, --help on demand.
for (skills.skills_registry) |sk| {