graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
claude-agent: keep a response that streams during a tool re-list
justrach1b6cb8bb1a86
diff --git a/scripts/test-claude-agent.py b/scripts/test-claude-agent.pyindex fe0ef671..f561c40e 100755--- a/scripts/test-claude-agent.py+++ b/scripts/test-claude-agent.py@@ -16,6 +16,11 @@ loop has run it, then answers. Asserts:A second run has the model call `rlm` with `llm_query`: the sub-query runs onits own tools-off `claude` process and its answer reaches the held call,while the chat's process carries on (no restart, one process each).++A third run loads a tool, so graff tells Claude Code its list changed; the+fake is already streaming the next response then. Those first events must+reach the turn, not be dropped while graff waits for the re-list (that left a+response with no message_start: "unparseable response")."""import json, os, shutil, subprocess, sys, tempfilefrom pathlib import Path@@ -57,8 +62,11 @@ mcp("m1", {"method": "initialize", "params": {"protocolVersion": "2025-11-25"},mcp("m2", {"method": "tools/list", "jsonrpc": "2.0", "id": 1})ready()user = read()-if os.environ.get("FAKE_SCENARIO") == "llm_query":+scenario = os.environ.get("FAKE_SCENARIO")+if scenario == "llm_query":name, args = "rlm", {"code": 'a = llm_query("what is six times seven")\nprint(a)'}+elif scenario == "relist_streaming":+ name, args = "load_tool_schemas", {"tools": ["subagent"]}else:name, args = "write_file", {"path": "out.txt", "content": "from claude agent\n"}ev({"type": "message_start", "message": {"id": "msg_1", "type": "message", "role": "assistant", "model": "claude-test", "content": [], "usage": usage}})@@ -68,8 +76,18 @@ ev({"type": "content_block_stop", "index": 0})ev({"type": "message_delta", "delta": {"stop_reason": "tool_use", "stop_sequence": None}, "usage": {"output_tokens": 20}})ev({"type": "message_stop"})reply = mcp("m3", {"method": "tools/call", "params": {"name": name, "arguments": args, "_meta": {"claudecode/toolUseId": "toolu_1"}}, "jsonrpc": "2.0", "id": 2})-ev({"type": "message_start", "message": {"id": "msg_2", "type": "message", "role": "assistant", "model": "claude-test", "content": [], "usage": usage}})-ev({"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}})+if scenario == "relist_streaming":+ changed = reply+ assert changed["request"]["message"]["method"] == "notifications/tools/list_changed", changed+ # The next response is already under way when graff asks for the re-list.+ ev({"type": "message_start", "message": {"id": "msg_2", "type": "message", "role": "assistant", "model": "claude-test", "content": [], "usage": usage}})+ ev({"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}})+ out({"type": "control_response", "response": {"subtype": "success", "request_id": changed["request_id"], "response": {}}})+ mcp("m4", {"method": "tools/list", "jsonrpc": "2.0", "id": 3})+ reply = read()+else:+ ev({"type": "message_start", "message": {"id": "msg_2", "type": "message", "role": "assistant", "model": "claude-test", "content": [], "usage": usage}})+ ev({"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}})ev({"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "FAKE-DONE"}})ev({"type": "content_block_stop", "index": 0})ev({"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence": None}, "usage": {"output_tokens": 3}})@@ -130,6 +148,7 @@ def main():assert 'out.txt' in call['result']['content'][0]['text'], callprint('PASS claude-agent: graff tools as the SDK server, a held call answered with graff\'s result, one process', flush=True)llm_query_case(binary, temp, env)+ relist_streaming_case(binary, temp, env)finally:shutil.rmtree(temp, ignore_errors=True)@@ -162,5 +181,25 @@ def llm_query_case(binary, temp, base_env):print('PASS claude-agent: llm_query runs on its own tools-off process; the chat\'s process carries on', flush=True)+def relist_streaming_case(binary, temp, base_env):+ """A tool load re-lists graff's tools while the model's next response is+ already streaming: the response arrives whole, on the same process."""+ log = temp / 'relist.log'+ work = temp / 'proj-relist'+ work.mkdir()+ env = dict(base_env, FAKE_CLAUDE_LOG=str(log), FAKE_SCENARIO='relist_streaming')+ run = subprocess.run([binary, '--model', 'claude-agent/sonnet', '--yolo', '-p', 'load a tool'], cwd=work, env=env,+ capture_output=True, text=True, encoding='utf-8', errors='replace', timeout=120)+ assert run.returncode == 0, run.stderr[-2000:]+ assert 'FAKE-DONE' in run.stdout and 'unparseable' not in run.stdout + run.stderr, (run.stdout[-500:], run.stderr[-1500:])+ rows = [json.loads(l) for l in log.read_text().splitlines() if l.strip()]+ assert len([r for r in rows if r['kind'] == 'argv']) == 1, 'expected one claude process'+ inputs = [r['obj'] for r in rows if r['kind'] == 'in']+ replies = {i['response']['request_id']: i['response'] for i in inputs if i.get('type') == 'control_response'}+ relisted = [t['name'] for t in replies['m4']['response']['mcp_response']['result']['tools']]+ assert 'subagent' in relisted, relisted+ print('PASS claude-agent: a response streaming during a tool re-list reaches the turn whole', flush=True)++if __name__ == '__main__':main()diff --git a/src/additional_tests.zig b/src/additional_tests.zigindex 609e6533..9071b4ce 100644--- a/src/additional_tests.zig+++ b/src/additional_tests.zig@@ -33,6 +33,7 @@ test {_ = @import("claude_agent_tests.zig");_ = @import("claude_agent_login.zig");_ = @import("claude_agent.zig");+ _ = @import("claude_agent_session.zig");}test "oneshot -p still uses the live stall-watched transport" {diff --git a/src/claude_agent.zig b/src/claude_agent.zigindex a60998ec..7efa8cb5 100644--- a/src/claude_agent.zig+++ b/src/claude_agent.zig@@ -203,14 +203,27 @@ pub fn open(owner: Owner, a: std.mem.Allocator, spawn: wire.Spawn, mode: wire.Prfn control(s: *Session, a: std.mem.Allocator, id: []const u8, request_json: []const u8, tools: ?Value, poll: bool) !void {try s.send(try std.fmt.allocPrint(a, "{{\"type\":\"control_request\",\"request_id\":\"{s}\",\"request\":{s}}}", .{ id, request_json }));while (true) {- switch (try wire.classify(a, try s.readLine(poll, init_budget_ms))) {+ const line = try s.readLine(poll, init_budget_ms);+ switch (try wire.classify(a, line)) {.response => |r| if (std.mem.eql(u8, r.request_id, id)) return if (r.ok) {} else error.ClaudeAgentControlFailed,- .control => |c| try service(s, a, c, tools),- else => {},+ else => |l| try aside(s, a, l, line, tools),}}}+/// A line read while graff waited on something else. Claude Code can still be+/// streaming the model's next response (it answers a call it refused by+/// itself); dropping those events left the turn a response with no+/// message_start, which failed as "unparseable response".+fn aside(s: *Session, a: std.mem.Allocator, l: wire.Line, line: []const u8, tools: ?Value) !void {+ switch (l) {+ .control => |c| try service(s, a, c, tools),+ .cancel => |id| s.cancel(id),+ .event, .result => try s.kept.keep(s.gpa, line),+ .response, .ignore => {},+ }+}+/// How long Claude Code gets to re-read the tool list after being told.const relist_budget_ms: u64 = 15_000;@@ -228,10 +241,8 @@ fn refreshTools(s: *Session, a: std.mem.Allocator, tools: ?Value, poll: bool) !vwhile (s.tools_listed == listed) {const left = deadline - nowMs(s.io);if (left <= 0) return error.ClaudeAgentToolsNotRelisted;- switch (try wire.classify(a, try s.readLine(poll, @intCast(left)))) {- .control => |c| try service(s, a, c, tools),- else => {},- }+ const line = try s.readLine(poll, @intCast(left));+ try aside(s, a, try wire.classify(a, line), line, tools);}}@@ -337,7 +348,7 @@ fn turn(self: *Agent, a: std.mem.Allocator, s: *Session, tools: ?Value, poll: bovar responses: usize = 0;var first_token = false;while (true) {- const line = try s.readLine(poll, turn_budget_ms);+ const line = try s.nextLine(a, poll, turn_budget_ms);switch (try wire.classify(a, line)) {.event => |event| {var ev = event;@@ -368,12 +379,7 @@ fn turn(self: *Agent, a: std.mem.Allocator, s: *Session, tools: ?Value, poll: boif (std.mem.eql(u8, kind, "message_stop") and std.mem.eql(u8, stop_reason, "tool_use")) break;},.control => |c| try service(s, a, c, tools),- .cancel => |id| {- for (s.pending.items, 0..) |p, i| if (std.mem.eql(u8, p.request_id, id)) {- s.dropPending(i);- break;- };- },+ .cancel => |id| s.cancel(id),.result => |r| {if (r.is_error) {const sse = try failSse(self, a, try problem(a, r.text));diff --git a/src/claude_agent_session.zig b/src/claude_agent_session.zigindex 5adc902f..90f7588a 100644--- a/src/claude_agent_session.zig+++ b/src/claude_agent_session.zig@@ -49,6 +49,10 @@ pub const Session = struct {/// (it runs a response's calls one after another): tool_use id → MCP/// result JSON. gpa-owned keys and values.results: std.StringHashMapUnmanaged([]u8) = .empty,+ /// Response lines that arrived while graff waited on a control answer+ /// (a tool-list re-read while the model was already answering), kept in+ /// order for the turn that reads them.+ kept: Kept = .{},used_ms: i64 = 0,pub fn start(gpa: Allocator, io: Io, argv: []const []const u8, parent_env: ?*const std.process.Environ.Map, cwd: ?[]const u8, props_fp: u64) !*Session {@@ -101,6 +105,20 @@ pub const Session = struct {w.flush() catch return error.ClaudeAgentExited;}+ /// A kept line first (copied onto `a`), then the next one read.+ pub fn nextLine(self: *Session, a: Allocator, poll_stdin: bool, budget_ms: u64) ![]u8 {+ if (try self.kept.take(self.gpa, a)) |line| return line;+ return self.readLine(poll_stdin, budget_ms);+ }++ /// Claude Code abandoned a call it was waiting on.+ pub fn cancel(self: *Session, request_id: []const u8) void {+ for (self.pending.items, 0..) |p, i| if (std.mem.eql(u8, p.request_id, request_id)) {+ self.dropPending(i);+ return;+ };+ }+/// The next line, or a stall/Esc error once `budget_ms` passes silent.pub fn readLine(self: *Session, poll_stdin: bool, budget_ms: u64) ![]u8 {const Done = union(enum) { line: anyerror![]u8, stall: http.WatchdogFired };@@ -171,6 +189,7 @@ pub const Session = struct {self.gpa.free(kv.value_ptr.*);}self.results.deinit(self.gpa);+ self.kept.deinit(self.gpa);if (self.env) |*m| m.deinit();self.gpa.free(self.in_buf);self.gpa.free(self.out_buf);@@ -178,6 +197,53 @@ pub const Session = struct {}};+/// Lines read ahead of the turn that owns them, oldest first. gpa-owned.+pub const Kept = struct {+ lines: std.ArrayList([]u8) = .empty,+ head: usize = 0,++ pub fn keep(self: *Kept, gpa: Allocator, line: []const u8) !void {+ const copy = try gpa.dupe(u8, line);+ errdefer gpa.free(copy);+ try self.lines.append(gpa, copy);+ }++ /// The oldest kept line, copied onto `a`, or null when none is left.+ pub fn take(self: *Kept, gpa: Allocator, a: Allocator) !?[]u8 {+ if (self.head == self.lines.items.len) return null;+ const line = try a.dupe(u8, self.lines.items[self.head]);+ gpa.free(self.lines.items[self.head]);+ self.head += 1;+ if (self.head == self.lines.items.len) {+ self.lines.clearRetainingCapacity();+ self.head = 0;+ }+ return line;+ }++ pub fn deinit(self: *Kept, gpa: Allocator) void {+ for (self.lines.items[self.head..]) |line| gpa.free(line);+ self.lines.deinit(gpa);+ self.* = .{};+ }+};++test "kept lines come back in order, and what is left is freed" {+ const gpa = std.testing.allocator;+ var arena = std.heap.ArenaAllocator.init(gpa);+ defer arena.deinit();+ var kept: Kept = .{};+ defer kept.deinit(gpa);+ try kept.keep(gpa, "message_start");+ try kept.keep(gpa, "content_block_start");+ try std.testing.expectEqualStrings("message_start", (try kept.take(gpa, arena.allocator())).?);+ try kept.keep(gpa, "content_block_delta");+ try std.testing.expectEqualStrings("content_block_start", (try kept.take(gpa, arena.allocator())).?);+ try std.testing.expectEqualStrings("content_block_delta", (try kept.take(gpa, arena.allocator())).?);+ try std.testing.expectEqual(@as(?[]u8, null), try kept.take(gpa, arena.allocator()));+ try kept.keep(gpa, "left for stop");+}+fn readLineTask(r: *Io.Reader) anyerror![]u8 {while (true) {const line = r.takeDelimiter('\n') catch |err| switch (err) {