Skip to content

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

Python

fix(tools): raise the open-file limit at startup + actionable fd-quota errors (#122)

Rach Pradhanabe4f1e8729a
diff --git a/src/exec.zig b/src/exec.zig
index 324a68b3..4805138e 100644
--- a/src/exec.zig
+++ b/src/exec.zig
@@ -135,7 +135,13 @@ fn execToolInner(ctx: ToolCtx, call: ToolCall) !ToolOutput {
const bg = if (input == .object) (if (input.object.get("run_in_background")) |v| v == .bool and v.bool else false) else false;
if (bg) {
const job = spawnJob(gpa, io, cmd) catch |err| return .{
- .text = try std.fmt.allocPrint(gpa, "could not start background job ({t}) — run it in the foreground instead", .{err}),
+ // #122: backgrounding costs MORE fds (pipes + pump task), so the
+ // generic "run it in the foreground" advice is right for every
+ // error except the fd-quota one — special-case that.
+ .text = if (err == error.ProcessFdQuotaExceeded)
+ try gpa.dupe(u8, "could not start background job (ProcessFdQuotaExceeded) — graff hit its open-file limit. Wait for running jobs/tools to finish, then retry with less parallel fan-out; if it recurs, raise the limit (`ulimit -n 4096`) before starting graff.")
+ else
+ try std.fmt.allocPrint(gpa, "could not start background job ({t}) — run it in the foreground instead", .{err}),
.is_error = true,
};
return .{ .text = try std.fmt.allocPrint(gpa, "[job {d} started: {s}]\nIt keeps running across turns. Poll new output with bash_output (id {d}, optional wait_ms), stop it with bash_kill.", .{ job.id, job.cmd, job.id }) };
diff --git a/src/main.zig b/src/main.zig
index 5bc4f76a..9d163a1c 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -241,6 +241,11 @@ pub fn main(init: std.process.Init) !void {
const gpa = init.gpa;
const io = init.io;
const arena = init.arena.allocator();
+ // #122: raise the open-file soft limit to the OS hard cap (Darwin defaults
+ // to 256) so parallel tool/subagent/MCP fan-out in one turn doesn't blow up
+ // shell tools with ProcessFdQuotaExceeded. Best-effort, no-op where rlimits
+ // don't exist.
+ std.process.raiseFileDescriptorLimit();
// #124: a per-turn scratch arena for the root agent's transient parse garbage,
// reset each request() so a long REPL/--json/serve session's RSS stays flat.
var scratch_state = std.heap.ArenaAllocator.init(gpa);
diff --git a/src/tools.zig b/src/tools.zig
index c1c955a4..49ad872d 100644
--- a/src/tools.zig
+++ b/src/tools.zig
@@ -350,6 +350,12 @@ fn companionNativeFallback(bare: []const u8) []const u8 {
}
pub fn failure(gpa: Allocator, err: anyerror) ToolOutput {
+ // #122: the bare error name is useless advice for an open-file-limit hit —
+ // say what it means and what actually helps.
+ if (err == error.ProcessFdQuotaExceeded) {
+ const text = gpa.dupe(u8, "error: ProcessFdQuotaExceeded — graff hit its open-file limit, usually from many parallel tools/subagents/MCP servers in one turn. Retry the failed calls sequentially (less parallel fan-out); if it recurs, raise the limit (`ulimit -n 4096`) before starting graff.") catch return .{ .is_error = true };
+ return .{ .text = text, .is_error = true };
+ }
const text = std.fmt.allocPrint(gpa, "error: {t}", .{err}) catch return .{ .is_error = true };
return .{ .text = text, .is_error = true };
}