graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Let write-only cat heredocs past the codedb guard
Rach Pradhanf3520545aea9
diff --git a/docs/adr/0234-the-model-knows-the-os-and-the-codedb-guard-only-redirects-searches.md b/docs/adr/0234-the-model-knows-the-os-and-the-codedb-guard-only-redirects-searches.mdindex 5ead749a..bd382a1a 100644--- a/docs/adr/0234-the-model-knows-the-os-and-the-codedb-guard-only-redirects-searches.md+++ b/docs/adr/0234-the-model-knows-the-os-and-the-codedb-guard-only-redirects-searches.md@@ -29,12 +29,16 @@ calls did work the task asked for.(BSD tools: `sed -i ''`, no GNU-only flags)" in a macOS build, "on Linux" ina Linux build, and nothing elsewhere. The text is fixed per build, so thecached prefix does not change between turns.-- The codedb guard lets two shapes through (`codedb_guard_scope.zig`):+- The codedb guard lets three shapes through (`codedb_guard_scope.zig`):- an in-place `sed` edit (`-i`, `-i ''`, `-i.bak`, `-Ei`, `--in-place`) inthe command's first segment;- a plain whole-file read (`cat`, `head`, `tail`, `nl`, `wc`, `bat`) with nopipe, when every file it names exists and is at most 16 KiB, the size a- tool result keeps inline before it becomes a handle.+ tool result keeps inline before it becomes a handle;+ - a write-only `cat` (#1709): its first segment names no input file and+ sends stdout to a file, as in `cat > f <<'EOF'` or `cat >> f <<EOF`. An+ output redirection target is a destination, so its size never counts+ toward the read limit; a file operand or `<` input still makes it a read.Searches (`grep`, `rg`, `awk`, `sed` without `-i`, or a read piped intoone) and reads of bigger files still go to codedb. Both checks run beforediff --git a/src/codedb_guard_scope.zig b/src/codedb_guard_scope.zigindex cafde654..bc27699d 100644--- a/src/codedb_guard_scope.zig+++ b/src/codedb_guard_scope.zig@@ -4,8 +4,9 @@//! The guard moves source SEARCHES, and reads of files too big to take whole,//! onto codedb. Two shapes are neither, and blocking them only cost the model//! a round trip: an in-place edit (`sed -i`) writes the file and reads nothing-//! back, and a whole-file read of a small file returns no more than a-//! `read_file` would.+//! back, a write-only `cat > file <<'EOF'` (or `>>`) reads no file at all, and+//! a whole-file read of a small file returns no more than a `read_file` would.+//! Output redirection targets are destinations, never counted as reads (#1709).const std = @import("std");const Io = std.Io;@@ -32,6 +33,98 @@ pub fn inPlaceEdit(tool: []const u8, cmd: []const u8) bool {return false;}+/// Every exemption, for the guard in `tools.zig::codedbGuard`. Paths resolve+/// in the agent's own worktree when it has one, else the process cwd.+pub fn exempt(io: Io, agent_cwd: ?[]const u8, tool: []const u8, cmd: []const u8) bool {+ return inPlaceEdit(tool, cmd) or writeOnly(tool, cmd) or smallReadIn(io, agent_cwd, tool, cmd);+}++/// `exempt` against an open directory (tests).+pub fn exemptAt(io: Io, cwd: Io.Dir, tool: []const u8, cmd: []const u8) bool {+ return inPlaceEdit(tool, cmd) or writeOnly(tool, cmd) or smallRead(io, cwd, tool, cmd);+}++/// `cat` whose first command segment names no input file and sends stdout to+/// a file: `cat > f <<'EOF'`, `cat >> f <<EOF`, `cat > f`. Its input is a+/// heredoc, a here-string or stdin, so no source is read however large the+/// destination is. Any file operand (`cat big.zig > out`), `<` input+/// redirection or command substitution makes it a read the guard still+/// checks.+pub fn writeOnly(tool: []const u8, cmd: []const u8) bool {+ if (!std.mem.eql(u8, tool, "cat")) return false;+ var i: usize = std.mem.indexOfAny(u8, cmd, " \t") orelse return false; // past `cat`+ var target: enum { none, file, word } = .none; // what the next word is+ var stdout_to_file = false;+ while (true) {+ while (i < cmd.len and (cmd[i] == ' ' or cmd[i] == '\t')) i += 1;+ if (i >= cmd.len) break;+ const c = cmd[i];+ if (c == ';' or c == '|' or c == '\n') break; // end of the first segment+ if (c == '&' and !(i + 1 < cmd.len and cmd[i + 1] == '>')) break; // `&&`, `&`+ // An optional fd (`2>`, `1>>`) or `&` (stdout and stderr) before the operator.+ var fd_end = i;+ while (fd_end < cmd.len and std.ascii.isDigit(cmd[fd_end])) fd_end += 1;+ if (fd_end == i and c == '&') fd_end += 1;+ if (fd_end < cmd.len and (cmd[fd_end] == '>' or cmd[fd_end] == '<')) {+ if (target != .none) return false; // an operator where a target belongs+ const fd = cmd[i..fd_end];+ i = fd_end;+ if (cmd[i] == '>') {+ i += 1;+ if (i < cmd.len and (cmd[i] == '>' or cmd[i] == '|')) i += 1;+ if (i < cmd.len and cmd[i] == '&') { // `>&2`: a dup, not a file+ i += 1;+ while (i < cmd.len and (std.ascii.isDigit(cmd[i]) or cmd[i] == '-')) i += 1;+ continue;+ }+ if (fd.len == 0 or std.mem.eql(u8, fd, "1") or std.mem.eql(u8, fd, "&")) stdout_to_file = true;+ target = .file;+ } else {+ i += 1;+ if (i < cmd.len and cmd[i] == '<') { // heredoc `<<`/`<<-` or here-string `<<<`+ i += 1;+ if (i < cmd.len and (cmd[i] == '<' or cmd[i] == '-')) i += 1;+ target = .word;+ } else if (i < cmd.len and cmd[i] == '&') { // `<&3`: a dup+ i += 1;+ while (i < cmd.len and (std.ascii.isDigit(cmd[i]) or cmd[i] == '-')) i += 1;+ } else return false; // `< file`, `<> file`: reads a file+ }+ continue;+ }+ // A word, with quotes kept whole.+ const start = i;+ while (i < cmd.len and std.mem.indexOfScalar(u8, " \t\n;|&<>", cmd[i]) == null) {+ if (cmd[i] == '\'' or cmd[i] == '"') {+ const close = std.mem.indexOfScalarPos(u8, cmd, i + 1, cmd[i]) orelse return false;+ i = close;+ }+ i += 1;+ }+ const word = cmd[start..i];+ if (std.mem.indexOfScalar(u8, word, '`') != null or std.mem.indexOf(u8, word, "$(") != null) return false;+ if (target != .none) {+ target = .none;+ continue;+ }+ if (word.len > 0 and word[0] == '-') continue; // a flag, or `-` for stdin+ return false; // a file operand: this cat reads+ }+ return target == .none and stdout_to_file;+}++/// An output redirection token: `>`, `>>`, `2>`, `&>>`, `>|` alone (target is+/// the next token) or with its target attached (`>out`, `2>/dev/null`).+fn outputRedirect(tok: []const u8) ?enum { alone, attached } {+ var i: usize = 0;+ while (i < tok.len and std.ascii.isDigit(tok[i])) i += 1;+ if (i == 0 and i < tok.len and tok[i] == '&') i += 1;+ if (i >= tok.len or tok[i] != '>') return null;+ i += 1;+ if (i < tok.len and (tok[i] == '>' or tok[i] == '|')) i += 1;+ return if (i == tok.len) .alone else .attached;+}+fn plainReader(tool: []const u8) bool {for ([_][]const u8{ "cat", "head", "tail", "nl", "wc", "bat", "batcat" }) |reader|if (std.mem.eql(u8, reader, tool)) return true;@@ -48,14 +141,19 @@ pub fn smallReadIn(io: Io, agent_cwd: ?[]const u8, tool: []const u8, cmd: []cons}/// A plain reader (cat/head/tail/nl/wc/bat) with no pipe (`cat x | grep` is a-/// search) whose every named file is at most `small_read_bytes`. Paths-/// resolve against `cwd`, where the command runs.+/// search) whose every named input file is at most `small_read_bytes`; an+/// output redirection target is written, not read, so its size is not+/// counted. Paths resolve against `cwd`, where the command runs.pub fn smallRead(io: Io, cwd: Io.Dir, tool: []const u8, cmd: []const u8) bool {if (!plainReader(tool)) return false;if (std.mem.indexOfScalar(u8, cmd, '|') != null) return false;var it = std.mem.tokenizeAny(u8, cmd, " \t\n;&");var files: usize = 0;while (it.next()) |raw| {+ if (outputRedirect(raw)) |form| {+ if (form == .alone) _ = it.next(); // skip the destination+ continue;+ }const tok = std.mem.trim(u8, raw, "'\"`()");if (tok.len == 0 or tok[0] == '-') continue;const st = cwd.statFile(io, tok, .{}) catch continue; // a command word, a flag value, a missing file@@ -97,3 +195,49 @@ test "smallRead: whole small files pass; big files, pipes and searches do not" {try std.testing.expect(!smallRead(io, tmp.dir, "grep", "grep print small.py"));try std.testing.expect(!smallRead(io, tmp.dir, "cat", "cat missing.py"));}++test "writeOnly: heredoc and redirect-only cat writes are not reads (#1709)" {+ try std.testing.expect(writeOnly("cat", "cat > src/synthetic.rs <<'EOF'\n// synthetic append\nEOF"));+ try std.testing.expect(writeOnly("cat", "cat >> src/synthetic.rs <<'EOF'\n// synthetic append\nEOF"));+ try std.testing.expect(writeOnly("cat", "cat >>src/x.rs <<EOF\nfn main() {}\nEOF"));+ try std.testing.expect(writeOnly("cat", "cat <<-\"EOF\" > out.zig\nconst x = 1;\nEOF"));+ try std.testing.expect(writeOnly("cat", "cat > out.py <<< 'print(1)'"));+ try std.testing.expect(writeOnly("cat", "cat > out.rs"));+ try std.testing.expect(writeOnly("cat", "cat 2>/dev/null >> out.rs <<'EOF'\nx\nEOF\nzig build"));+ // The heredoc body never counts as operands.+ try std.testing.expect(writeOnly("cat", "cat > a.zig <<'EOF'\ncat big.zig b.zig\nEOF"));++ // Mixed read/write and plain reads are still checked.+ try std.testing.expect(!writeOnly("cat", "cat big.zig > out"));+ try std.testing.expect(!writeOnly("cat", "cat a.rs b.rs >> c.rs"));+ try std.testing.expect(!writeOnly("cat", "cat > out.rs < big.rs"));+ try std.testing.expect(!writeOnly("cat", "cat >out.rs<big.rs"));+ try std.testing.expect(!writeOnly("cat", "cat > \"$(grep -l x big.rs)\" <<'EOF'\nx\nEOF"));+ try std.testing.expect(!writeOnly("cat", "cat big.rs"));+ try std.testing.expect(!writeOnly("cat", "cat <<'EOF' | grep x\nx\nEOF")); // no file destination+ try std.testing.expect(!writeOnly("cat", "cat 2> err.log big.rs"));+ try std.testing.expect(!writeOnly("cat", "cat >"));+ try std.testing.expect(!writeOnly("tee", "tee out.rs <<'EOF'\nx\nEOF"));+}++test "exempt: large write destinations pass, large source reads do not (#1709)" {+ var tmp = std.testing.tmpDir(.{});+ defer tmp.cleanup();+ const io = std.testing.io;+ const big = try std.testing.allocator.alloc(u8, small_read_bytes + 1);+ defer std.testing.allocator.free(big);+ @memset(big, 'a');+ try tmp.dir.writeFile(io, .{ .sub_path = "big.rs", .data = big });+ try tmp.dir.writeFile(io, .{ .sub_path = "dest.rs", .data = big });+ try tmp.dir.writeFile(io, .{ .sub_path = "small.rs", .data = "fn a() {}\n" });++ try std.testing.expect(exemptAt(io, tmp.dir, "cat", "cat >> dest.rs <<'EOF'\n// synthetic append\nEOF"));+ try std.testing.expect(exemptAt(io, tmp.dir, "cat", "cat > dest.rs <<'EOF'\n// synthetic\nEOF"));+ // Reading a small file into a large destination reads only the small file.+ try std.testing.expect(exemptAt(io, tmp.dir, "cat", "cat small.rs >> dest.rs"));+ try std.testing.expect(exemptAt(io, tmp.dir, "cat", "cat small.rs >dest.rs"));+ try std.testing.expect(!exemptAt(io, tmp.dir, "cat", "cat big.rs > out.rs"));+ try std.testing.expect(!exemptAt(io, tmp.dir, "cat", "cat small.rs big.rs >> dest.rs"));+ try std.testing.expect(!exemptAt(io, tmp.dir, "cat", "cat > out.rs < big.rs"));+ try std.testing.expect(!exemptAt(io, tmp.dir, "grep", "grep fn big.rs > out.txt"));+}diff --git a/src/tools.zig b/src/tools.zigindex 1d17b486..4247c71a 100644--- a/src/tools.zig+++ b/src/tools.zig@@ -252,9 +252,9 @@ pub fn codedbGuard(ctx: ToolCtx, call: ToolCall) ?ToolOutput {is_scanner = true;};if (!is_scanner) return null;- // ADR 0234: an in-place edit and a whole read of small files cost a round trip to refuse and save nothing.+ // ADR 0234: an in-place edit, a write-only cat (#1709) and a whole read of small files cost a round trip to refuse and save nothing.const scope = @import("codedb_guard_scope.zig");- if (scope.inPlaceEdit(tool, trimmed) or scope.smallReadIn(ctx.io, ctx.agent_cwd, tool, trimmed)) return null;+ if (scope.exempt(ctx.io, ctx.agent_cwd, tool, trimmed)) return null;// …aimed at a concrete source file (not a glob — codedb glob/tree cover that).const src_path = extractSourceFilePath(cmd) orelse return null;