graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
ws: stream codex WS deltas through printDelta like SSE
Rach Pradhan912c4c82bbae
diff --git a/scripts/codex_ws_test.py b/scripts/codex_ws_test.pyindex f7061c21..45b994c3 100644--- a/scripts/codex_ws_test.py+++ b/scripts/codex_ws_test.py@@ -349,6 +349,12 @@ def run_scenario(session.wait_for_literal(REPLY_TEXT, start=cursor)session.wait_for(CTX_RE, start=cursor)standing = terminal_text(bytes(session.raw[cursor:]))+ # WS streams deltas through the same printDelta hook as SSE; the+ # completed response must not surface the answer a second time.+ if standing.count(REPLY_TEXT) != 1:+ raise AssertionError(+ f"{label}: reply shown {standing.count(REPLY_TEXT)}x, expected once"+ )pct = int(CTX_RE.search(standing).group(1))if not 0 <= pct <= 100:raise AssertionError(f"{label}: invalid standing ctx percent {pct}")diff --git a/src/agent_ws.zig b/src/agent_ws.zigindex e755681e..2ec23803 100644--- a/src/agent_ws.zig+++ b/src/agent_ws.zig@@ -549,7 +549,11 @@ pub fn postResponsesWs(self: *Agent, body: []const u8) ![]u8 {if (frames_seen == 1) if (self.tracer) |tr| tr.note("ws", "first frame");if (fbuf.items.len == 0) continue :stream;try loop_guard.event(self, fbuf.items, false);- @import("agent_async_tools.zig").onLine(self, fbuf.items);+ // Same per-line hook as the SSE reader: streams text/thinking/tool-arg+ // deltas to the TUI and ACP sinks (and runs async-tool admission).+ // Without it a WS turn surfaced nothing until the response completed.+ const sse_line = try std.mem.concat(self.scratchAlloc(), u8, &.{ "data: ", fbuf.items });+ self.printDelta(sse_line);// …and THIS is the budget signal: visible prose, from EITHER event that// grows partial_text on SSE — an output-text delta, or the streamed// arguments of a whitelisted meta call (attempt_completion / ask_user),@@ -582,5 +586,6 @@ pub fn postResponsesWs(self: *Agent, body: []const u8) ![]u8 {}self.traceFirstToken(); // tool-only responses fall back to completion timeself.codex_ws_used_ms = nowAwakeMs(self.io); // completed turn — restart the idle window (#codex-ws)+ sink.emit(self.io, .{ .stream_complete = .{ .streamed_text = self.streamed_text } }); // as SSE: flush the held markdown tailreturn full.toOwnedSlice();}diff --git a/src/agent_ws_stream_test.zig b/src/agent_ws_stream_test.zignew file mode 100644index 00000000..e1598051--- /dev/null+++ b/src/agent_ws_stream_test.zig@@ -0,0 +1,76 @@+//! Codex WS turns surface deltas as they arrive, through the same per-line+//! hook (printDelta) as the SSE reader. Before, a WS turn buffered every frame+//! and only rendered the answer after response.completed: ACP clients got the+//! whole reply as one chunk at the end, and a turn that stalled mid-answer had+//! shown nothing at all.++const std = @import("std");+const builtin = @import("builtin");+const Io = std.Io;++const http = @import("http.zig");+const http_stall = @import("http_stall.zig");+const ws = @import("ws.zig");+const agent_ws = @import("agent_ws.zig");++const mock = @import("agent_ws_mock.zig");+const Mock = mock.Mock;+const nowMs = mock.nowMs;+const mockAgent = mock.mockAgent;++test "a WS text delta reaches the UI before the response completes" {+ if (builtin.os.tag == .windows) return error.SkipZigTest;+ const gpa = std.testing.allocator;+ const io = std.testing.io;++ var addr = try std.Io.net.IpAddress.parseLiteral("127.0.0.1:0");+ var server = try std.Io.net.IpAddress.listen(&addr, io, .{});+ defer server.deinit(io);+ var done: std.atomic.Value(bool) = .init(false);+ // One output_text delta ("hi"), then silence: the response never completes,+ // so anything the UI shows must have been streamed.+ var fut = io.async(Mock.run, .{ io, &server, Mock.Mode.frame_then_silence, &done });+ defer fut.await(io);+ defer done.store(true, .release);++ const saved_stream = http.stream_stall_ms;+ const saved_floor = http_stall.idle_floor_ms;+ http.stream_stall_ms = 2000;+ http_stall.idle_floor_ms = 100;+ defer http.stream_stall_ms = saved_stream;+ defer http_stall.idle_floor_ms = saved_floor;++ var arena_state = std.heap.ArenaAllocator.init(gpa);+ defer arena_state.deinit();+ const arena = arena_state.allocator();++ var out: Io.Writer.Allocating = .init(gpa);+ defer out.deinit();+ var url_buf: [64]u8 = undefined;+ const url = try std.fmt.bufPrint(&url_buf, "http://127.0.0.1:{d}/x", .{server.socket.address.getPort()});+ var agent = mockAgent(gpa, arena, io, url);+ agent.out = &out.writer;+ defer agent.partial_text.deinit(arena);+ defer agent.md_buf.deinit(gpa);+ defer agent.md_word.deinit(gpa);++ var ws_url_buf: [64]u8 = undefined;+ const ws_url = try std.fmt.bufPrint(&ws_url_buf, "ws://127.0.0.1:{d}/x", .{server.socket.address.getPort()});+ agent.codex_ws = try ws.WsClient.connect(gpa, io, ws_url, false, &.{});+ agent.codex_ws_used_ms = nowMs(io);+ defer if (agent.codex_ws) |c| {+ c.dead = true;+ c.deinit(gpa);+ agent.codex_ws = null;+ };++ const r = agent_ws.postResponsesWs(&agent, "{\"model\":\"gpt-5\",\"previous_response_id\":\"resp_1\",\"input\":[]}");+ if (r) |ok| gpa.free(ok) else |_| {}+ try std.testing.expectError(error.StreamStalled, r);++ try std.testing.expect(agent.streamed_text);+ try std.testing.expectEqualStrings("hi", agent.partial_text.items);+ // The markdown renderer holds an unterminated line until its tail flush.+ agent.flushStreamTail();+ try std.testing.expect(std.mem.indexOf(u8, out.written(), "hi") != null);+}diff --git a/src/agent_ws_test.zig b/src/agent_ws_test.zigindex 18a38520..e739d17c 100644--- a/src/agent_ws_test.zig+++ b/src/agent_ws_test.zig@@ -596,4 +596,5 @@ comptime {_ = @import("agent_ws_stall_test.zig");_ = @import("agent_ws_reuse_test.zig");_ = @import("agent_ws_fallback_test.zig"); // (#427) the WS→SSE ladder's routing decisions+ _ = @import("agent_ws_stream_test.zig"); // WS deltas stream through printDelta like SSE}