Skip to content

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

Python

fix(rlm): print(read_file(path)) returns the file, not the literal

Cursor Agent7a564b72029c
diff --git a/lean-proofs/Graff/ToolCatalog.lean b/lean-proofs/Graff/ToolCatalog.lean
index 3d75b3d5..1b7d1a9e 100644
--- a/lean-proofs/Graff/ToolCatalog.lean
+++ b/lean-proofs/Graff/ToolCatalog.lean
@@ -26,7 +26,7 @@ def localTools : List String :=
"write_file", "codedb", "read_tool_result", "imagegen"]
def leanTools : List String :=
- ["bash", "edit_file", "write_file", "codedb",
+ ["bash", "read_file", "edit_file", "write_file", "codedb",
"subagent", "attempt_completion", "load_tool_schemas"]
def optionalTools : List String :=
@@ -128,8 +128,6 @@ example : mem "webfetch" localTools = false := by native_decide
example : mem "imagegen" localTools = true := by native_decide
example : mem "imagegen" leanTools = false := by native_decide
example : mem "subagent" leanTools = true := by native_decide
-example : mem "read_file" leanTools = false := by native_decide
-example : advertised { lean := true } "read_file" = false := by native_decide
example : unique (baseTools ++ metaTools ++ rootExtras) = true := by native_decide
example : catalog {} =
diff --git a/spec/kernels/tool_catalog.json b/spec/kernels/tool_catalog.json
index abb91362..7d7726ba 100644
--- a/spec/kernels/tool_catalog.json
+++ b/spec/kernels/tool_catalog.json
@@ -50,6 +50,7 @@
],
"lean": [
"bash",
+ "read_file",
"edit_file",
"write_file",
"codedb",
@@ -599,6 +600,7 @@
},
"advertised": [
"bash",
+ "read_file",
"edit_file",
"write_file",
"codedb",
@@ -648,6 +650,7 @@
},
"advertised": [
"bash",
+ "read_file",
"edit_file",
"write_file",
"codedb",
@@ -697,6 +700,7 @@
},
"advertised": [
"bash",
+ "read_file",
"edit_file",
"write_file",
"codedb",
@@ -746,6 +750,7 @@
},
"advertised": [
"bash",
+ "read_file",
"edit_file",
"write_file",
"codedb",
@@ -795,6 +800,7 @@
},
"advertised": [
"bash",
+ "read_file",
"edit_file",
"write_file",
"codedb",
@@ -841,6 +847,7 @@
},
"advertised": [
"bash",
+ "read_file",
"edit_file",
"write_file",
"codedb",
@@ -887,6 +894,7 @@
},
"advertised": [
"bash",
+ "read_file",
"edit_file",
"write_file",
"codedb",
@@ -933,6 +941,7 @@
},
"advertised": [
"bash",
+ "read_file",
"edit_file",
"write_file",
"codedb",
diff --git a/spec/kernels/tool_catalog.md b/spec/kernels/tool_catalog.md
index 40e71dc1..c734fd9a 100644
--- a/spec/kernels/tool_catalog.md
+++ b/spec/kernels/tool_catalog.md
@@ -11,9 +11,8 @@ always in this order:
two compile-time optional rows).
2. **Add** every `#352` optional whose flag is on (`imagegen` today).
3. **Subtract** `#330` `--no-local-tools` (host-touching built-ins).
-4. **Keep** `--lean` (seven one-shot tools; catalog `read_file` is
- dropped — reads go through `rlm` host functions or `codedb`).
- Description compaction does not change names.
+4. **Keep** `--lean` (eight one-shot tools). Description compaction does
+ not change names.
A subagent starts from `base_specs` only (no meta, no `subagent`), then
applies (2) and (3). `--lean` is a root-prefix concern; children do not
@@ -28,7 +27,7 @@ run the keep-list.
| root extras | `subagent` `workflow` `agent_output` `learn_candidate` `peer_message` `workspace` |
| optional | `imagegen` |
| local (`#330`) | `bash` `bash_output` `bash_kill` `read_file` `edit_file` `write_file` `codedb` `read_tool_result` `imagegen` |
-| lean keep | `bash` `edit_file` `write_file` `codedb` `subagent` `attempt_completion` `load_tool_schemas` |
+| lean keep | `bash` `read_file` `edit_file` `write_file` `codedb` `subagent` `attempt_completion` `load_tool_schemas` |
`webfetch`, every meta tool except the lean keepers, and every `mcp__*`
name are **not** local. MCP is outside this kernel.
diff --git a/spec/ref/tool_catalog.py b/spec/ref/tool_catalog.py
index a258e3f2..4f09fdde 100644
--- a/spec/ref/tool_catalog.py
+++ b/spec/ref/tool_catalog.py
@@ -23,6 +23,7 @@ LOCAL_TOOLS = (
)
LEAN_TOOLS = (
"bash",
+ "read_file",
"edit_file",
"write_file",
"codedb",
diff --git a/src/no_local_tools.zig b/src/no_local_tools.zig
index 18c0381c..c112088e 100644
--- a/src/no_local_tools.zig
+++ b/src/no_local_tools.zig
@@ -74,13 +74,13 @@ pub var lean: bool = false;
/// The lean keep-list. File/shell tools stay on the wire: an rlm-only
/// catalog (ADR 0024 follow-up) made grok-4.6 retry scripts for 122
-/// calls / 4/6. Catalog `read_file` is the leftover re-read tax — the
-/// model already printed those files via rlm, then called read_file
-/// again (ADR 0024). Reads stay as rlm host functions + codedb.
-/// `load_tool_schemas` stays so MCP can unfold; it is hidden on lean.
-/// Overflow paging (`read_tool_result`) is interactive.
+/// calls / 4/6. Dropping catalog `read_file` (085320) made it worse
+/// (63 calls) — the model retried rlm/bash cat when print(read_file)
+/// was a no-op. `load_tool_schemas` stays so MCP can unfold; it is
+/// hidden on lean. Overflow paging (`read_tool_result`) is interactive.
pub const lean_tools = [_][]const u8{
"bash",
+ "read_file",
"edit_file",
"write_file",
"codedb",
@@ -222,13 +222,13 @@ test "lean: the filter keeps exactly the seven one-shot tools, and allocates not
lean = true;
const kept = try filterLeanSpecs(Spec, arena, &specs);
- try std.testing.expectEqual(@as(usize, 3), kept.len);
+ try std.testing.expectEqual(@as(usize, 4), kept.len);
try std.testing.expectEqualStrings("bash", kept[0].name);
- try std.testing.expectEqualStrings("subagent", kept[1].name);
- try std.testing.expectEqualStrings("attempt_completion", kept[2].name);
- try std.testing.expectEqual(@as(usize, 7), lean_tools.len);
+ 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);
+ try std.testing.expectEqual(@as(usize, 8), lean_tools.len);
try std.testing.expect(!leanKeeps("read_tool_result"));
- try std.testing.expect(!leanKeeps("read_file")); // host fn on rlm, not a catalog tool on -p
for (lean_tools) |tool| try std.testing.expect(leanKeeps(tool));
try std.testing.expect(!leanKeeps("workflow"));
try std.testing.expect(!leanKeeps("todo_write"));
@@ -398,5 +398,5 @@ test "lean catalog drops overflow pager and empty load_tool_schemas" {
try std.testing.expect(std.mem.indexOf(u8, json, "read_tool_result") == null);
try std.testing.expect(std.mem.indexOf(u8, json, "load_tool_schemas") == null);
try std.testing.expect(std.mem.indexOf(u8, json, "\"rlm\"") != null);
- try std.testing.expect(std.mem.indexOf(u8, json, "\"name\":\"read_file\"") == null);
+ try std.testing.expect(std.mem.indexOf(u8, json, "\"name\":\"read_file\"") != null);
}
diff --git a/src/prompt_text.zig b/src/prompt_text.zig
index 6d1b766f..fe5c6f93 100644
--- a/src/prompt_text.zig
+++ b/src/prompt_text.zig
@@ -260,9 +260,11 @@ pub const parallel_tools_note = parallel_core_note ++ parallel_examples_note ++
/// covers the !yolo map). Same edit discipline, ~1k fewer prefix bytes.
pub const lean_local_tools_note =
\\
- \\Read files through one rlm script (host read_file/codedb) or codedb.
- \\Prefer edit_file for existing files and write_file only for new files.
- \\Independent reads belong in ONE rlm script, not a chain of turns.
+ \\read_file before editing; prefer edit_file for existing files and
+ \\write_file only for new files. Navigate with codedb (context, around,
+ \\callpath, list_dir, status) or one rlm script of those functions.
+ \\print(read_file("path")) returns the file — do not read it again.
+ \\Independent reads belong in ONE response or one rlm script, not a chain of turns.
\\A passing verify command and attempt_completion belong in ONE response.
;
@@ -271,15 +273,15 @@ pub const lean_local_tools_note =
/// model spends one API call per read (ADR 0024 leftover vs grok).
pub const lean_parallel_note =
\\
- \\Independent reads belong in one rlm script; they run as it streams.
+ \\Independent reads and checks belong in ONE response; they run concurrently.
\\A call that needs an earlier result, or two writes to the same file, stays
\\in its own turn. A passing verify and attempt_completion share a response.
;
pub const lean_intro_note =
\\You are a coding agent. Use the cataloged tools; never invent one.
- \\Read files through rlm (or codedb), then edit. A passing test and
- \\attempt_completion belong in ONE response.
+ \\Independent reads belong in ONE response (or one rlm script), not one per turn.
+ \\A passing test and attempt_completion belong in ONE response.
;
pub const lean_work_note =
diff --git a/src/rlm.zig b/src/rlm.zig
index a812dbd0..21b85b80 100644
--- a/src/rlm.zig
+++ b/src/rlm.zig
@@ -24,7 +24,7 @@ pub const tool_name = "rlm";
pub const tool_desc = "Programmatic tool calling (RLM + sPTC). Script whose functions ARE this session's tools. Independent literal read_file/codedb/bash/webfetch/sleep_ms/llm_query/subagent calls start as the script streams. Binds persist across rlm calls. subagent(\"task\") is sidecar-only (keep the critical-path next step local; sync in v1; run_in_background=true returns an id). print() is the answer. Example: a = read_file(\"src/main.zig\"); b = codedb(\"status\"); print(a, b). No imports, no control flow. Prefer one rlm over N tool calls.";
/// --lean catalog desc: same contract, no REPL essay. maybeAppend is after
/// compactLeanSpecs, so this is the one-shot wire text.
-pub const lean_tool_desc = "Read files here (host read_file/codedb/bash). Those reads are not a catalog tool on -p — print() is the answer, then edit_file/write_file/bash as catalog tools. Literal calls start as it streams. Binds persist.";
+pub const lean_tool_desc = "Batch independent read_file/codedb/bash here. print(read_file(\"p\")) returns the file — do not catalog-read it again. Then edit_file/write_file/bash as catalog tools. Literal calls start as it streams. Binds persist.";
pub const tool_schema =
\\{"type": "object", "properties": {"code": {"type": "string", "description": "Python-like script: name = read_file(\"path\") / codedb(\"command\") / bash(\"cmd\") / sleep_ms(ms) / llm_query(\"prompt\") / subagent(\"task\"); print(...) is the result. Assignments persist across rlm calls. Semicolons or newlines separate statements."}}, "required": ["code"]}
;
@@ -180,7 +180,7 @@ fn evalStmt(
return null;
}
if (printArgs(stmt)) |inner| {
- const text = try renderPrint(arena, inner, binds);
+ const text = try renderPrint(ctx, arena, inner, binds, claimed);
if (printed.items.len > 0) try printed.append(ctx.gpa, '\n');
try printed.appendSlice(ctx.gpa, text);
return null;
@@ -201,18 +201,18 @@ fn printArgs(stmt: []const u8) ?[]const u8 {
return t["print(".len .. t.len - 1];
}
-fn renderPrint(arena: Allocator, inner: []const u8, binds: []const Binding) ![]const u8 {
+fn renderPrint(ctx: ToolCtx, arena: Allocator, inner: []const u8, binds: []const Binding, claimed: *std.StringHashMap(ToolOutput)) ![]const u8 {
const parts = try spec_ptc.splitTopLevel(arena, inner, ',');
if (parts.len == 0) return "";
var out: std.ArrayList(u8) = .empty;
for (parts, 0..) |part, i| {
if (i > 0) try out.append(arena, '\n');
- try out.appendSlice(arena, try renderPrintPart(arena, part, binds));
+ try out.appendSlice(arena, try renderPrintPart(ctx, arena, part, binds, claimed));
}
return out.toOwnedSlice(arena);
}
-fn renderPrintPart(arena: Allocator, inner: []const u8, binds: []const Binding) ![]const u8 {
+fn renderPrintPart(ctx: ToolCtx, arena: Allocator, inner: []const u8, binds: []const Binding, claimed: *std.StringHashMap(ToolOutput)) ![]const u8 {
const t = std.mem.trim(u8, inner, " \t");
if (t.len >= 2 and (t[0] == '"' or t[0] == '\'') and t[t.len - 1] == t[0]) return t[1 .. t.len - 1];
var i = binds.len;
@@ -220,6 +220,13 @@ fn renderPrintPart(arena: Allocator, inner: []const u8, binds: []const Binding)
i -= 1;
if (std.mem.eql(u8, binds[i].name, t)) return binds[i].text;
}
+ if (try spec_ptc.extractCall(arena, t)) |c| {
+ const key = try c.key(arena);
+ if (claimed.get(key)) |hit| return try arena.dupe(u8, hit.text);
+ const out = runHost(ctx, c);
+ defer ctx.gpa.free(out.text);
+ return try arena.dupe(u8, out.text);
+ }
return try arena.dupe(u8, t);
}
@@ -308,6 +315,10 @@ test "runScript reads a fixture via speculated read_file and prints it" {
defer gpa.free(out.text);
try std.testing.expect(!out.is_error);
try std.testing.expect(std.mem.indexOf(u8, out.text, "hello-rlm") != null);
+ const nested = try runScript(ctx, "print(read_file(\"note.txt\"))");
+ defer gpa.free(nested.text);
+ try std.testing.expect(!nested.is_error);
+ try std.testing.expect(std.mem.indexOf(u8, nested.text, "hello-rlm") != null);
}
test "runScript splits a semicolon one-liner and prints both binds" {
diff --git a/src/spec_ptc.zig b/src/spec_ptc.zig
index 67a6f2be..e2d81656 100644
--- a/src/spec_ptc.zig
+++ b/src/spec_ptc.zig
@@ -94,11 +94,29 @@ pub fn extractCall(arena: Allocator, stmt: []const u8) !?Call {
pub fn extractCalls(arena: Allocator, statements: []const []const u8) ![]Call {
var out: std.ArrayList(Call) = .empty;
for (statements) |stmt| {
- if (try extractCall(arena, stmt)) |c| try out.append(arena, c);
+ if (try extractCall(arena, stmt)) |c| {
+ try out.append(arena, c);
+ continue;
+ }
+ // print(read_file("p")) is a print, not an assign — still speculate
+ // the nested host call so print() can claim the file, not the literal.
+ if (printInner(stmt)) |inner| {
+ const parts = try splitTopLevel(arena, inner, ',');
+ for (parts) |part| {
+ if (try extractCall(arena, std.mem.trim(u8, part, " \t"))) |c|
+ try out.append(arena, c);
+ }
+ }
}
return out.toOwnedSlice(arena);
}
+fn printInner(stmt: []const u8) ?[]const u8 {
+ const t = std.mem.trim(u8, stmt, " \t");
+ if (!std.mem.startsWith(u8, t, "print(") or t[t.len - 1] != ')') return null;
+ return t["print(".len .. t.len - 1];
+}
+
fn stripComment(stmt: []const u8) []const u8 {
var i: usize = 0;
var in_str: ?u8 = null;
@@ -450,6 +468,12 @@ test "extractCalls finds independent sleeps in a three-call script" {
const calls = try extractCalls(a, &src);
try std.testing.expectEqual(@as(usize, 3), calls.len);
try std.testing.expectEqualStrings("sleep_ms", calls[0].name);
+ const nested = [_][]const u8{ "print(read_file(\"note.txt\"))", "print(codedb(\"list_dir .\"), bash(\"ls\"))" };
+ const inside = try extractCalls(a, &nested);
+ try std.testing.expectEqual(@as(usize, 3), inside.len);
+ try std.testing.expectEqualStrings("read_file", inside[0].name);
+ try std.testing.expectEqualStrings("codedb", inside[1].name);
+ try std.testing.expectEqualStrings("bash", inside[2].name);
}
test "splitStatements breaks semicolon one-liners; strings keep their commas" {