Skip to content

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

Python

feat(lean): unfold rlm on -p; drop one-shot prompt tax

Cursor Agent6295060f8431
diff --git a/docs/adr/0024-three-harness-compare-prompt-subagent-rss.md b/docs/adr/0024-three-harness-compare-prompt-subagent-rss.md
index 7cf209f7..6f9252a6 100644
--- a/docs/adr/0024-three-harness-compare-prompt-subagent-rss.md
+++ b/docs/adr/0024-three-harness-compare-prompt-subagent-rss.md
@@ -225,3 +225,22 @@ default rlm: **+1 pass**, **−41% wall** (743s → 441s), **−65% input
tokens** (1.26M → 447k), **−48% calls** (96 → 50). Still ~3× grok's
tokens/calls (fatter prefix + more turns) and **~17× less RSS**. Do
not copy grok's 159M heap.
+
+## Why still ~3× tokens (and the next cut)
+
+447k vs grok's 157–185k is **not** their heap. Per-call ~9k vs ~5k,
+and 50 calls vs 31. Two leftover taxes on `-p --yolo --lean`:
+
+1. **`rlm` was folded out of the catalog.** Stable-catalog + native
+ fold hid the default loop behind `load_tool_schemas`. The model
+ did N structured bash/read/edit calls instead of one script.
+ Lean now unfolds `rlm` onto the catalog (ADR 0022: it is the
+ one-shot loop, not a late power tool).
+2. **Interactive essays on every one-shot turn.** Fan-out /
+ `.graff/traces` / `gh issue create` / heads-up / the outside-cwd
+ local-tools essay / the repo map / the folded-native listing.
+ None of that helps a 5-file SWE sandbox. Dropped or swapped for
+ a short `lean_local_tools_note`.
+
+Live A/B after this diet: `--suite swe --harness graff-dev`. Goal:
+same 5/6, fewer calls and input tokens, RSS stays ~9M.
diff --git a/src/mcp_schema_gate.zig b/src/mcp_schema_gate.zig
index ac080557..72edeb73 100644
--- a/src/mcp_schema_gate.zig
+++ b/src/mcp_schema_gate.zig
@@ -497,7 +497,7 @@ pub fn descWithListing(arena: Allocator, all: []const mcp.Tool) ![]const u8 {
// as the MCP half — stable mode lists by policy, never by load state.
const native_fold = @import("native_fold.zig");
var natives: std.ArrayList([]const u8) = .empty;
- if (native_fold.anyFolded()) {
+ if (native_fold.anyFolded() and !@import("no_local_tools.zig").lean) {
for (native_fold.folded) |name| {
if (!native_fold.isFolded(name)) continue; // rlm stays off the default listing
if (!g_stable_catalog and native_fold.isLoaded(name)) continue;
diff --git a/src/native_fold.zig b/src/native_fold.zig
index 9cb1e46f..5bf15b0d 100644
--- a/src/native_fold.zig
+++ b/src/native_fold.zig
@@ -64,7 +64,12 @@ pub const folded = [_][]const u8{
/// `rlm` joins the stack while available (default); `--old` drops it so the
/// structured-only catalog and the meta-tool listing stay unchanged.
pub fn isFolded(name: []const u8) bool {
- if (std.mem.eql(u8, name, "rlm")) return @import("rlm_spec.zig").available;
+ // --lean / -p: rlm is the default loop (ADR 0022), not a late power
+ // tool. Folding it behind load_tool_schemas on a 5–10 turn one-shot
+ // made the model do N structured calls instead of one script (ADR 0024
+ // leftover token tax vs grok-build).
+ if (std.mem.eql(u8, name, "rlm"))
+ return @import("rlm_spec.zig").available and !@import("no_local_tools.zig").lean;
for (folded) |tool| {
if (std.mem.eql(u8, name, tool)) return true;
}
@@ -442,3 +447,42 @@ test "folded rlm is a listing name, not a catalog schema; off stays off the list
try std.testing.expect(std.mem.indexOf(u8, on, "llm_query") == null); // full desc/schema stay folded
try std.testing.expect(std.mem.indexOf(u8, on, rlm.tool_schema) == null);
}
+
+test "lean unfolds rlm into the catalog (one-shot loop, not a listing stub)" {
+ const schema_mod = @import("schema.zig");
+ const rlm = @import("rlm.zig");
+ const rlm_spec = @import("rlm_spec.zig");
+ const no_local = @import("no_local_tools.zig");
+ const saved_avail = rlm.available;
+ const saved_spec = rlm_spec.available;
+ const saved_enabled = enabled;
+ const saved_lean = no_local.lean;
+ const saved_loaded = g_loaded;
+ const saved_loaded_len = g_loaded_len;
+ defer {
+ rlm.available = saved_avail;
+ rlm_spec.available = saved_spec;
+ rlm.sync();
+ enabled = saved_enabled;
+ no_local.lean = saved_lean;
+ g_loaded = saved_loaded;
+ g_loaded_len = saved_loaded_len;
+ }
+ enabled = true;
+ g_loaded_len = 0;
+ no_local.lean = true;
+ rlm.available = true;
+ rlm.sync();
+ try std.testing.expect(!isFolded("rlm"));
+ try std.testing.expect(!catalogSkips("rlm"));
+
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ const specs = [_]schema_mod.ToolSpec{
+ .{ .name = rlm.tool_name, .desc = rlm.tool_desc, .schema = rlm.tool_schema },
+ };
+ const json = try schema_mod.renderRootTools(arena, .openai, &specs, &.{});
+ try std.testing.expect(std.mem.indexOf(u8, json, "llm_query") != null);
+ try std.testing.expect(std.mem.indexOf(u8, json, rlm.tool_schema) != null);
+}
diff --git a/src/no_local_tools.zig b/src/no_local_tools.zig
index 63e661fd..f7445e89 100644
--- a/src/no_local_tools.zig
+++ b/src/no_local_tools.zig
@@ -373,3 +373,19 @@ test "#330: dispatch refuses a hallucinated bash call for the root AND for a sub
try std.testing.expect(std.mem.indexOf(u8, out.text, "gate-escaped") == null); // never ran
}
}
+
+test "lean prompt diet: no fan-out / trace / issue-filing; short local-tools note" {
+ const prompts = @import("prompts.zig");
+ const saved = lean;
+ defer lean = saved;
+ lean = true;
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const out = try prompts.composeBase(arena_state.allocator(), prompts.detectCaps());
+ try std.testing.expect(std.mem.indexOf(u8, out, ".graff/traces") == null);
+ try std.testing.expect(std.mem.indexOf(u8, out, "gh issue create") == null);
+ try std.testing.expect(std.mem.indexOf(u8, out, "fan out") == null);
+ try std.testing.expect(std.mem.indexOf(u8, out, "not covered by /rewind") == null);
+ try std.testing.expect(std.mem.indexOf(u8, out, "list_dir") != null);
+ try std.testing.expect(std.mem.indexOf(u8, out, "SPEC.md") != null);
+}
diff --git a/src/prompt_snapshot_tests.zig b/src/prompt_snapshot_tests.zig
index f0cfce86..8cabcd94 100644
--- a/src/prompt_snapshot_tests.zig
+++ b/src/prompt_snapshot_tests.zig
@@ -565,7 +565,7 @@ test "lean drops the todo/constraint capabilities from the prompt, never the loc
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
+ try std.testing.expect(lean.local_tools and !lean.subagents); // one-shot: no fan-out essay
// …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();
diff --git a/src/prompt_text.zig b/src/prompt_text.zig
index 0bbc063b..ec81e07c 100644
--- a/src/prompt_text.zig
+++ b/src/prompt_text.zig
@@ -8,6 +8,8 @@
//! Read this file as the prompt itself; read prompts.zig for when each part of
//! it is sent.
+const std = @import("std");
+
// ── ROOT PROMPT BEGIN ── examples/prepare_graff_tournament.py extracts every
// multiline-literal line between these two markers as the seed root policy, so
// this comment deliberately carries no literal marker of its own.
@@ -252,3 +254,24 @@ pub const parallel_tail_note =
pub const parallel_tools_note = parallel_core_note ++ parallel_examples_note ++ parallel_tail_note;
// ── ROOT PROMPT END ─────────────────────────────────────────────────────────
+
+/// --lean / -p swap-in for `local_tools_note`. Drops the outside-cwd
+/// exception and the approval essay (evals are --yolo; unattended_note
+/// covers the !yolo map). Same edit discipline, ~1k fewer prefix bytes.
+pub const lean_local_tools_note =
+ \\
+ \\read_file before editing; prefer edit_file for existing files and
+ \\write_file only for new files or full rewrites. Exact-key lookup:
+ \\read_file once with contains. Navigate with codedb (context, around,
+ \\callpath, list_dir, status), not bash grep/find/ls. Read one current
+ \\span, smallest edit, do not verify after success.
+;
+
+/// --lean composition: drop harness-debug / narration; swap the long
+/// local-tools essay. `null` means skip the segment entirely.
+pub fn leanSegment(name: []const u8, original: []const u8, is_lean: bool) ?[]const u8 {
+ if (!is_lean) return original;
+ if (std.mem.eql(u8, name, "trace") or std.mem.eql(u8, name, "harness_issue") or std.mem.eql(u8, name, "headsup")) return null;
+ if (std.mem.eql(u8, name, "local_tools")) return lean_local_tools_note;
+ return original;
+}
diff --git a/src/prompts.zig b/src/prompts.zig
index 9c3aa679..2bd5ed48 100644
--- a/src/prompts.zig
+++ b/src/prompts.zig
@@ -140,7 +140,7 @@ pub fn detectCaps() Caps {
// is also not a capability the prompt may instruct about.
return .{
.local_tools = toolAdvertised("read_file") and toolAdvertised("bash"),
- .subagents = toolAdvertised("subagent"),
+ .subagents = toolAdvertised("subagent") and !no_local_tools.lean, // lean: no fan-out essay
.todos = toolAdvertised("todo_write") and !no_local_tools.lean,
.constraints = toolAdvertised("note_constraint") and !no_local_tools.lean,
.git_repo = g_git_repo,
@@ -173,7 +173,9 @@ pub fn probeGitRepo(io: Io) void {
pub fn composeSegments(arena: Allocator, caps: Caps) ![]const u8 {
var aw: Io.Writer.Allocating = .init(arena);
for (segments) |seg| {
- if (caps.has(seg.gate)) try aw.writer.writeAll(seg.text);
+ if (!caps.has(seg.gate)) continue;
+ const bytes = text.leanSegment(seg.name, seg.text, no_local_tools.lean) orelse continue;
+ try aw.writer.writeAll(bytes);
}
return aw.writer.buffered();
}
diff --git a/src/startup.zig b/src/startup.zig
index bc355672..da98cabe 100644
--- a/src/startup.zig
+++ b/src/startup.zig
@@ -120,7 +120,9 @@ pub fn buildSystemPrompt(
}
// Repo map (repo_map.zig): the top of the working tree, so the model reads
// the files it needs instead of spending opening turns on ls/find.
- if (environ.get("GRAFF_NO_REPO_MAP") == null) {
+ // Lean one-shots name their files; the map is an interactive opener
+ // and was prefix bytes on every -p turn (ADR 0024).
+ if (environ.get("GRAFF_NO_REPO_MAP") == null and !@import("no_local_tools.zig").lean) {
if (@import("repo_map.zig").segment(io, arena)) |map| sys_normal = try std.fmt.allocPrint(arena, "{s}{s}", .{ sys_normal, map });
}
if (unattended) sys_normal = try std.fmt.allocPrint(arena, "{s}{s}", .{ sys_normal, prompts.unattended_note });