graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix: join parallel shell batches on the caller thread
Cursor Agenteade0c3dac67
diff --git a/evals/harness_behavior.jsonl b/evals/harness_behavior.jsonlindex 26e9f6d7..70a5c608 100644--- a/evals/harness_behavior.jsonl+++ b/evals/harness_behavior.jsonl@@ -110,3 +110,4 @@{"id":"review-checkpoint-preserves-unfinished-scope","why":"#306: a long read-only review requests an evidence checkpoint without forcing completion or granting edits.","args":["--old"],"prompt_options":{"type":"review"},"prompt":"Review target.txt without modifying it.","workspace_seed":{"target.txt":"original\n"},"script":[{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"tool":"read_file","arguments":{"path":"target.txt"}},{"text":"No findings. Review finished."}],"assert":[{"events_at_least":{"match":{"type":"review_checkpoint","complete":false},"count":1}},{"events_at_most":{"match":{"type":"review_checkpoint"},"count":1}},{"request_contains":{"text":"Review checkpoint:"}},{"request_contains":{"text":"next inspection tool call"}},{"file_equals":{"path":"target.txt","content":"original\n"}},{"final_text_contains":"No findings. Review finished."}]}{"id": "same-turn-two-writes-land", "why": "Two write_file calls in one assistant message must both land. Serial mutating batches must not drop the second write; parallel must not race-lose it either.", "args": [], "env": {"GRAFF_NO_CODEDB_GUARD": "1"}, "prompt": "create a.txt and b.txt", "script": [{"tools": [{"tool": "write_file", "arguments": {"path": "a.txt", "content": "alpha\n"}}, {"tool": "write_file", "arguments": {"path": "b.txt", "content": "beta\n"}}]}, {"tool": "bash", "arguments": {"command": "grep -qx alpha a.txt && grep -qx beta b.txt && echo BOTH_LANDED"}}, {"text": "both files are in"}], "assert": [{"events_at_least": {"match": {"type": "tool_call", "name": "write_file"}, "count": 2}}, {"events_at_most": {"match": {"type": "tool_call_finished", "is_error": true}, "count": 0}}, {"event": {"type": "tool_result", "name": "bash", "text": "BOTH_LANDED\n"}}, {"final_text_contains": "both files are in"}]}{"id": "same-turn-write-then-edit-same-file", "why": "write_file then edit_file on the same path in one assistant message used to race (#337). Serial batches make the edit see the write; a lost race fails old_string or missing file.", "args": [], "env": {"GRAFF_NO_CODEDB_GUARD": "1"}, "prompt": "create n.txt then change v1 to v2", "script": [{"tools": [{"tool": "write_file", "arguments": {"path": "n.txt", "content": "v1\n"}}, {"tool": "edit_file", "arguments": {"path": "n.txt", "old_string": "v1", "new_string": "v2"}}]}, {"tool": "bash", "arguments": {"command": "grep -qx v2 n.txt && echo EDIT_SAW_WRITE"}}, {"text": "the file now says v2"}], "assert": [{"event": {"type": "tool_call", "name": "write_file"}}, {"event": {"type": "tool_call", "name": "edit_file"}}, {"events_at_most": {"match": {"type": "tool_call_finished", "is_error": true}, "count": 0}}, {"event": {"type": "tool_result", "name": "bash", "text": "EDIT_SAW_WRITE\n"}}, {"final_text_contains": "v2"}]}+{"id": "parallel-fast-shell-batch-resumes", "why": "#1166: several fast shell calls in one batch must each return once, then the next model step must run. A heap trap after reaping used to kill the process before the second request.", "args": [], "prompt": "run the three checks", "script": [{"tools": [{"tool": "bash", "arguments": {"command": "printf AAA"}}, {"tool": "bash", "arguments": {"command": "printf BBBBB"}}, {"tool": "bash", "arguments": {"command": "printf CCCCCCC"}}]}, {"text": "all three checks returned"}], "assert": [{"events_at_least": {"match": {"type": "tool_result", "name": "bash"}, "count": 3}}, {"events_at_most": {"match": {"type": "tool_call_finished", "name": "bash", "is_error": true}, "count": 0}}, {"request_contains": {"index": -1, "text": "AAA"}}, {"request_contains": {"index": -1, "text": "BBBBB"}}, {"request_contains": {"index": -1, "text": "CCCCCCC"}}, {"final_text_contains": "all three checks returned"}]}diff --git a/src/agent_tool_batch.zig b/src/agent_tool_batch.zigindex 9c77ff84..605a09d7 100644--- a/src/agent_tool_batch.zig+++ b/src/agent_tool_batch.zig@@ -26,8 +26,9 @@ const eval_control = @import("agent_eval_control.zig");pub const skipped_text = "tool execution skipped because the operation was aborted";/// Write / edit / imagegen serialize the whole batch so a later edit cannot-/// race a write on the same path. Shell-only batches stay parallel (#266).-/// Mixing bash with write/edit still serializes, because write/edit do.+/// race a write on the same path. Mixing bash with write/edit still+/// serializes, because write/edit do. Shell-only batches stay parallel+/// (#266) but join on the caller thread (#1166).pub fn isSequential(name: []const u8) bool {if (std.mem.eql(u8, name, "write_file")) return true;if (std.mem.eql(u8, name, "edit_file")) return true;@@ -35,6 +36,19 @@ pub fn isSequential(name: []const u8) bool {return false;}+fn isShellName(name: []const u8) bool {+ return std.mem.eql(u8, name, "shell") or std.mem.eql(u8, name, "bash");+}++/// Shell job reaping is not safe on pool threads: fast parallel completions+/// corrupted a free block after every result was already persisted (#1166).+pub fn batchNeedsCooperative(calls: []const ToolCall, idx: []const usize) bool {+ for (idx) |i| {+ if (isShellName(calls[i].name)) return true;+ }+ return false;+}+pub fn batchNeedsSerial(calls: []const ToolCall, ext_idx: []const usize) bool {for (ext_idx) |i| {if (isSequential(calls[i].name)) return true;@@ -169,8 +183,15 @@ fn runParallel(self: *Agent, ctx: ToolCtx, calls: []const ToolCall, ext_idx: []cdefer self.gpa.free(futures);const outputs = try self.gpa.alloc(ToolOutput, spawn_at.items.len);defer self.gpa.free(outputs);- for (spawn_at.items, futures) |i, *fut|- fut.* = self.io.concurrent(execTool, .{ ctx, calls[i] }) catch self.io.async(execTool, .{ ctx, calls[i] });+ const cooperative = batchNeedsCooperative(calls, spawn_at.items);+ for (spawn_at.items, futures) |i, *fut| {+ // Shell batches start together cooperatively so #266 still holds, but+ // they do not share io.concurrent worker threads (#1166).+ fut.* = if (cooperative)+ self.io.async(execTool, .{ ctx, calls[i] })+ else+ self.io.concurrent(execTool, .{ ctx, calls[i] }) catch self.io.async(execTool, .{ ctx, calls[i] });+ }for (futures, outputs) |*fut, *output| output.* = fut.await(self.io);defer for (outputs) |output| self.gpa.free(output.text);@@ -206,8 +227,17 @@ test "batchNeedsSerial: file mutations serialize; bash-only stays parallel" {try std.testing.expect(batchNeedsSerial(&only_write, &.{0}));const two_bash = [_]ToolCall{ bash, bash };try std.testing.expect(!batchNeedsSerial(&two_bash, &.{ 0, 1 }));+ try std.testing.expect(batchNeedsCooperative(&two_bash, &.{ 0, 1 }));const bash_write = [_]ToolCall{ bash, write };try std.testing.expect(batchNeedsSerial(&bash_write, &.{ 0, 1 }));+ try std.testing.expect(!batchNeedsCooperative(&reads, &.{ 0, 1 }));+}++test "isShellName: bash and shell, not reads" {+ try std.testing.expect(isShellName("bash"));+ try std.testing.expect(isShellName("shell"));+ try std.testing.expect(!isShellName("read_file"));+ try std.testing.expect(!isShellName("webfetch"));}test "skipped_text is a stable model-facing sentence" {