Skip to content

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

Python

fix: separate completed Thinking summaries

justrach939aa89387f6
diff --git a/src/agent_stream.zig b/src/agent_stream.zig
index f0ebf40e..5775be57 100644
--- a/src/agent_stream.zig
+++ b/src/agent_stream.zig
@@ -503,6 +503,15 @@ pub fn printDelta(self: *Agent, raw_line: []const u8) void {
const reasoning = reasoningDelta(self.provider.kind, obj);
if (reasoning.len != 0 or text.len != 0) self.traceFirstToken();
if (reasoning.len != 0) sink.emit(self.io, .{ .reasoning_delta = .{ .text = reasoning } });
+ // Summary chunks are contiguous; completed summaries are paragraphs, even
+ // across reasoning items / responses. Never replay the done event's text.
+ if (self.provider.kind == .responses) {
+ const t = obj.get("type");
+ const done_text = obj.get("text");
+ if (t != null and t.? == .string and std.mem.eql(u8, t.?.string, "response.reasoning_summary_text.done") and
+ done_text != null and done_text.? == .string and done_text.?.string.len != 0)
+ sink.emit(self.io, .{ .reasoning_delta = .{ .text = "\n\n" } });
+ }
if (text.len == 0) return;
self.streamed_text = true;
sink.emit(self.io, .{ .text_delta = .{ .text = text } });
@@ -565,6 +574,10 @@ pub fn oneshotShouldPaint(unattended: bool, json_mode: bool, text_len: usize) bo
return unattended and !json_mode and text_len != 0;
}
+test "Responses summary boundaries stream through the REPL and headless TUI" {
+ try stream_tests.reasoningBoundaries();
+}
+
test "oneshot paints answer tokens, never --json or empty deltas" {
try std.testing.expect(oneshotShouldPaint(true, false, 4));
try std.testing.expect(!oneshotShouldPaint(false, false, 4)); // line REPL uses out
diff --git a/src/agent_stream_tests.zig b/src/agent_stream_tests.zig
index efea935e..3c44591b 100644
--- a/src/agent_stream_tests.zig
+++ b/src/agent_stream_tests.zig
@@ -3,6 +3,119 @@
const std = @import("std");
const Kind = @import("provider.zig").Provider.Kind;
+pub fn reasoningBoundaries() !void {
+ const a = std.testing.allocator;
+ const tui = @import("tui");
+ const tui_sink = @import("tui_sink.zig");
+ var arena_state = std.heap.ArenaAllocator.init(a);
+ defer arena_state.deinit();
+ var client: std.http.Client = .{ .allocator = a, .io = std.testing.io };
+ defer client.deinit();
+ var queue: tui.EventQueue = .{};
+ queue.attach(a);
+ defer queue.deinit();
+ var buf: [2048]u8 = undefined;
+ var stream: @import("repl.zig").StreamBuf = .{ .buf = &buf };
+ var bridge: tui_sink.Bridge = .{ .queue = &queue, .stream = &stream, .show_thinking = true };
+ var agent: @import("agent.zig").Agent = .{
+ .gpa = a,
+ .arena = arena_state.allocator(),
+ .io = std.testing.io,
+ .client = &client,
+ .provider = .{ .id = "test", .kind = .responses, .auth = .bearer, .url = "", .api_key = "", .model = "test", .context = 0 },
+ .messages = std.json.Array.init(arena_state.allocator()),
+ .sink = tui_sink.forBridge(&bridge),
+ .sub = false,
+ .label = "test",
+ .out = null,
+ };
+ // Also exercise the classic REPL's actual "▼ Thinking" writer, not just
+ // the fullscreen frontend's live buffer.
+ const engine_sink = @import("engine_sink.zig");
+ const main = @import("main.zig");
+ const tick_gate = @import("tick_gate.zig");
+ const saved_color = main.use_color;
+ const saved_hosted = engine_sink.hosted_frontend;
+ const saved_open = main.g_thinking_open;
+ const saved_gate = tick_gate.g_gate;
+ defer {
+ main.use_color = saved_color;
+ engine_sink.hosted_frontend = saved_hosted;
+ main.g_thinking_open = saved_open;
+ tick_gate.g_gate = saved_gate;
+ }
+ main.use_color = true;
+ engine_sink.hosted_frontend = false;
+ var rendered: std.Io.Writer.Allocating = .init(a);
+ defer rendered.deinit();
+ var plain_agent = agent;
+ plain_agent.out = &rendered.writer;
+ plain_agent.sink = engine_sink.tuiSink(&plain_agent);
+ plain_agent.show_thinking = true;
+ defer plain_agent.thinking_text.deinit(a);
+ const agents = [_]*@import("agent.zig").Agent{ &agent, &plain_agent };
+ const dispatch = struct {
+ fn send(receivers: []const *@import("agent.zig").Agent, line: []const u8) void {
+ for (receivers) |receiver| @import("agent_stream.zig").printDelta(receiver, line);
+ }
+ }.send;
+ // Two parts in one item, then a new item, then a new response. Split
+ // inside emphasis as well as words: transport chunks are not paragraphs.
+ const headings = [_][]const u8{ "First", "Second", "Third", "Fourth" };
+ for (headings, 0..) |heading, i| {
+ if (i == 3) dispatch(&agents, "data: {\"type\":\"response.created\"}");
+ if (i != 1) dispatch(&agents, "data: {\"type\":\"response.output_item.added\",\"item\":{\"type\":\"reasoning\"}}");
+ dispatch(&agents, "data: {\"type\":\"response.reasoning_summary_part.added\",\"part\":{\"type\":\"summary_text\",\"text\":\"\"}}");
+ for ([_][]const u8{ "*", "*", heading, " heading", "**" }) |chunk| {
+ const line = try std.fmt.allocPrint(a, "data: {{\"type\":\"response.reasoning_summary_text.delta\",\"delta\":\"{s}\"}}", .{chunk});
+ defer a.free(line);
+ dispatch(&agents, line);
+ }
+ dispatch(&agents, "data: {\"type\":\"response.reasoning_summary_text.done\",\"text\":\"synthetic completed summary\"}");
+ dispatch(&agents, "data: {\"type\":\"response.reasoning_summary_part.done\",\"part\":{\"type\":\"summary_text\",\"text\":\"synthetic completed summary\"}}");
+ if (i != 0) dispatch(&agents, "data: {\"type\":\"response.output_item.done\",\"item\":{\"type\":\"reasoning\",\"encrypted_content\":\"synthetic opaque value\"}}");
+ if (i >= 2) dispatch(&agents, "data: {\"type\":\"response.completed\"}");
+ }
+ dispatch(&agents, "data: {\"type\":\"response.reasoning_text.delta\",\"delta\":\"synthetic private text\"}");
+ dispatch(&agents, "data: {\"type\":\"response.reasoning_summary_text.done\",\"text\":\"\"}");
+ const snap = stream.snapshot(a) orelse return error.NoStream;
+ defer a.free(snap);
+ try std.testing.expectEqualStrings("**First heading**\n\n**Second heading**\n\n**Third heading**\n\n**Fourth heading**\n\n", snap);
+ try std.testing.expectEqualStrings(snap, plain_agent.thinking_text.items);
+ try std.testing.expect(std.mem.indexOf(u8, rendered.written(), "▼ Thinking") != null);
+ try std.testing.expect(std.mem.endsWith(u8, rendered.written(), snap));
+
+ var term: tui.sim.Term = undefined;
+ term.init(a, 80, 24);
+ defer term.deinit();
+ const Job = std.meta.Child(std.meta.Child(@TypeOf(term.model.pending)));
+ var job: Job = .{
+ .gpa = a,
+ .history = &.{},
+ .params = .{},
+ .stream = .{ .buf = &buf },
+ .threaded = false,
+ };
+ job.stream.appendBytes(snap);
+ try term.model.push(.pending, "");
+ term.model.pending = &job;
+ defer term.model.pending = null;
+ const visible = try term.screen();
+ defer a.free(visible);
+ var lines = std.mem.splitScalar(u8, visible, '\n');
+ var seen: usize = 0;
+ while (lines.next()) |line| {
+ if (std.mem.indexOf(u8, line, " heading") != null) {
+ try std.testing.expect(seen < headings.len);
+ try std.testing.expect(std.mem.indexOf(u8, line, headings[seen]) != null);
+ try std.testing.expect(std.mem.count(u8, line, " heading") == 1);
+ try std.testing.expect(std.mem.indexOf(u8, line, "**") == null);
+ seen += 1;
+ }
+ }
+ try std.testing.expectEqual(headings.len, seen);
+}
+
pub fn streamEnd(is_stream_end: anytype) !void {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();