Skip to content

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

Python

feat(lean): --lean also slims the tool surface to the one-shot seven

Rach Pradhan3a35c0d3131d
diff --git a/src/no_local_tools.zig b/src/no_local_tools.zig
index 4afe8632..f5d0f8da 100644
--- a/src/no_local_tools.zig
+++ b/src/no_local_tools.zig
@@ -64,6 +64,55 @@ pub fn blocks(name: []const u8) bool {
return enabled and isLocalTool(name);
}
+/// --lean / GRAFF_LEAN=1 tool surface — the token-cost counterpart of the MCP
+/// skip (session_start.leanSkipsMcp reads the same flag/env). A one-shot
+/// coding task needs seven tools; every other schema is prefix re-sent on
+/// EVERY model turn. Measured on the full root catalog: 25.9k schema bytes
+/// (~6.5k tokens) against 8.6k (~2.1k) for this subset — ~4.3k tokens saved
+/// per turn, the difference between a ~10.7k and a ~6.4k one-shot prefix.
+/// Unlike #330 this is NOT a security boundary, so there is no dispatch
+/// layer: an unadvertised tool is simply unknown to the model, and a
+/// hallucinated call fails like any bad tool name.
+pub var lean: bool = false;
+
+/// The lean keep-list. `subagent` stays despite being the priciest schema
+/// (5.1k bytes): in an unattended non-yolo one-shot the root is
+/// approval-gated out of bash/edit_file and delegation is the sanctioned
+/// path (prompts.unattended_note) — dropping it would strand exactly the
+/// runs lean is built for.
+pub const lean_tools = [_][]const u8{
+ "bash",
+ "read_file",
+ "edit_file",
+ "write_file",
+ "codedb",
+ "subagent",
+ "attempt_completion",
+};
+
+/// A name test only, so the catalog filter and tests share one list.
+pub fn leanKeeps(name: []const u8) bool {
+ for (lean_tools) |tool| {
+ if (std.mem.eql(u8, name, tool)) return true;
+ }
+ return false;
+}
+
+/// schema.zig chains this after filterRootSpecs (a tool may survive one and
+/// not the other). Returns the input untouched when lean is off — no
+/// allocation on the normal path, mirroring filterRootSpecs.
+pub fn filterLeanSpecs(comptime Spec: type, arena: Allocator, specs: []const Spec) ![]const Spec {
+ if (!lean) return specs;
+ const buf = try arena.alloc(Spec, specs.len);
+ var kept: usize = 0;
+ for (specs) |spec| {
+ if (!leanKeeps(spec.name)) continue;
+ buf[kept] = spec;
+ kept += 1;
+ }
+ return buf[0..kept];
+}
+
/// Refusal text. Names the flag so the model stops retrying the same call, and
/// points it at what does work in this deployment.
pub const refusal_text = "--no-local-tools is set for this process: the built-in bash, bash_output, bash_kill, read_file, edit_file, write_file and codedb tools are hard-disabled and cannot be re-enabled by asking. Run commands and touch files through the sandbox tools the connected MCP server provides instead; webfetch still works.";
@@ -127,8 +176,45 @@ test "#330: the gated set is exactly the host-touching built-ins; webfetch, meta
}
test "#330: GRAFF_NO_LOCAL_TOOLS is affirmative-only" {
- for ([_][]const u8{ "1", "true", "TRUE", "on", "Yes" }) |value| try std.testing.expect(envEnables(value));
- for ([_][]const u8{ "0", "off", "false", "no", "", "maybe" }) |value| try std.testing.expect(!envEnables(value));
+ for ([_][]const u8{ "1", "true", "TRUE", "on", "yes" }) |v| try std.testing.expect(envEnables(v));
+ for ([_][]const u8{ "", "0", "false", "off", "no", "2", "enabled" }) |v| try std.testing.expect(!envEnables(v));
+}
+
+test "lean: the filter keeps exactly the seven one-shot tools, and allocates nothing when off" {
+ const Spec = struct { name: []const u8 };
+ const specs = [_]Spec{
+ .{ .name = "bash" },
+ .{ .name = "workflow" },
+ .{ .name = "read_file" },
+ .{ .name = "todo_write" },
+ .{ .name = "subagent" },
+ .{ .name = "attempt_completion" },
+ };
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+
+ const saved = lean;
+ defer lean = saved;
+
+ lean = false;
+ try std.testing.expectEqual(specs.len, (try filterLeanSpecs(Spec, arena, &specs)).len);
+ try std.testing.expectEqual(@as(usize, 0), arena_state.queryCapacity());
+
+ lean = true;
+ const kept = try filterLeanSpecs(Spec, arena, &specs);
+ try std.testing.expectEqual(@as(usize, 4), kept.len);
+ try std.testing.expectEqualStrings("bash", kept[0].name);
+ try std.testing.expectEqualStrings("read_file", kept[1].name);
+ try std.testing.expectEqualStrings("subagent", kept[2].name);
+ try std.testing.expectEqualStrings("attempt_completion", kept[3].name);
+ // The keep-list is exactly the seven documented tools — subagent stays
+ // (the unattended delegation path), everything orchestration/meta goes.
+ try std.testing.expectEqual(@as(usize, 7), lean_tools.len);
+ for (lean_tools) |tool| try std.testing.expect(leanKeeps(tool));
+ try std.testing.expect(!leanKeeps("workflow"));
+ try std.testing.expect(!leanKeeps("todo_write"));
+ try std.testing.expect(!leanKeeps("webfetch"));
}
test "#330: filterRootSpecs drops only the gated names, and does not allocate when the gate is off" {
diff --git a/src/prompt_snapshot_tests.zig b/src/prompt_snapshot_tests.zig
index 7924c736..9f6553ec 100644
--- a/src/prompt_snapshot_tests.zig
+++ b/src/prompt_snapshot_tests.zig
@@ -553,6 +553,24 @@ test "#391/#445: the prompt funnel never arms the note store in a test build" {
try std.testing.expect(!prompts.compactNotesArmed());
}
+test "lean drops the todo/constraint capabilities from the prompt, never the local tools" {
+ const saved = no_local_tools.lean;
+ defer no_local_tools.lean = saved;
+ no_local_tools.lean = false;
+ const full = prompts.detectCaps();
+ try std.testing.expect(full.todos and full.constraints and full.local_tools);
+ no_local_tools.lean = true;
+ const lean = prompts.detectCaps();
+ try std.testing.expect(!lean.todos and !lean.constraints);
+ try std.testing.expect(lean.local_tools and lean.subagents); // the lean seven keep both
+ // …and the composition carries it: the dropped segments' bytes are gone.
+ var a_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer a_state.deinit();
+ const a = a_state.allocator();
+ const lean_prompt = try prompts.composeBase(a, lean);
+ try std.testing.expect(lean_prompt.len < prompts.main_system_prompt.len);
+}
+
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();
diff --git a/src/prompts.zig b/src/prompts.zig
index 4a936024..5584a137 100644
--- a/src/prompts.zig
+++ b/src/prompts.zig
@@ -131,11 +131,13 @@ pub fn toolAdvertised(name: []const u8) bool {
/// The live gates. Every flag and env knob feeding them is settled before
/// startup.buildSystemPrompt runs (args.parse, then setupSkillsAndTheme).
pub fn detectCaps() Caps {
+ // The lean halves agree: a tool the catalog filtered out (no_local_tools)
+ // is also not a capability the prompt may instruct about.
return .{
.local_tools = toolAdvertised("read_file") and toolAdvertised("bash"),
.subagents = toolAdvertised("subagent"),
- .todos = toolAdvertised("todo_write"),
- .constraints = toolAdvertised("note_constraint"),
+ .todos = toolAdvertised("todo_write") and !no_local_tools.lean,
+ .constraints = toolAdvertised("note_constraint") and !no_local_tools.lean,
};
}
diff --git a/src/schema.zig b/src/schema.zig
index 90c321de..d7c3d1fb 100644
--- a/src/schema.zig
+++ b/src/schema.zig
@@ -325,9 +325,8 @@ pub fn effectiveRootSpecs(arena: Allocator) ![]const ToolSpec {
(if (learn_store.active_agent_loaded) &root_specs else &root_specs_without_learning)
else
(if (learn_store.active_agent_loaded) &root_specs_without_clock else &root_specs_without_optional);
- // Optionals are appended BEFORE the #330 filter, never after: being
- // optional does not exempt a tool from the hard gate.
- return no_local_tools.filterRootSpecs(ToolSpec, arena, try tool_gates.withAvailable(ToolSpec, arena, chosen, &optional_specs));
+ // Optionals are appended BEFORE the #330 filter, never after (optional ≠ exempt); the --lean token filter chains last.
+ return no_local_tools.filterLeanSpecs(ToolSpec, arena, try no_local_tools.filterRootSpecs(ToolSpec, arena, try tool_gates.withAvailable(ToolSpec, arena, chosen, &optional_specs)));
}
const Schema = union(enum) { raw: []const u8, value: Value };
diff --git a/src/session_settings.zig b/src/session_settings.zig
index ebd1f0e5..b06b1d00 100644
--- a/src/session_settings.zig
+++ b/src/session_settings.zig
@@ -87,6 +87,9 @@ pub fn applyEnvKnobs(arena: Allocator, environ_map: anytype) !void {
main_mod.g_clock_sleep = main_mod.g_clock_sleep or std.mem.eql(u8, v, "1") or std.ascii.eqlIgnoreCase(v, "true") or std.ascii.eqlIgnoreCase(v, "on") or std.ascii.eqlIgnoreCase(v, "yes");
}
if (environ_map.get("GRAFF_NO_LOCAL_TOOLS")) |v| no_local_tools.enabled = no_local_tools.enabled or no_local_tools.envEnables(v);
+ // GRAFF_LEAN: presence-based, exactly matching session_start.leanSkipsMcp
+ // (the MCP half of the same switch) — a "0" still means lean, by design.
+ if (environ_map.get("GRAFF_LEAN") != null) no_local_tools.lean = true;
// (#codex-ws) GRAFF_CODEX_WS_IDLE_SECS raises/lowers the held-WS idle limit
// (default 4 min — the backend killed ours within 8.5 min idle; opencode
// pools at 5). Mirrors GRAFF_STREAM_STALL_SECS above: seconds, ignored if