Skip to content

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

Python

feat(engine): agent_ws emits typed events — spinner aliases and inline ⚠ notices gone (#422)

Rach Pradhan77634aebf606
diff --git a/src/agent_stream_render.zig b/src/agent_stream_render.zig
index 46655412..fc5356f9 100644
--- a/src/agent_stream_render.zig
+++ b/src/agent_stream_render.zig
@@ -3,9 +3,9 @@
//! and the live dimmed "Thinking" reasoning block (streamThinking/
//! closeThinkingBlock/toggleThinkingFold), moved verbatim out of
//! agent_stream.zig so the transport loop owns no terminal drawing. Driven by
-//! TuiSink (engine_sink.zig); agent_ws.zig still reaches the spinner through
-//! its Agent member aliases. Frontend territory: term.zig/ansi.zig/anim.zig
-//! imports live here, never in engine files.
+//! TuiSink (engine_sink.zig) only, as of slice 1b — agent_ws.zig emits events
+//! now instead of reaching the spinner aliases. Frontend territory:
+//! term.zig/ansi.zig/anim.zig imports live here, never in engine files.
//!
//! Agent.g_spin_stop/Agent.g_spin_future are struct-level `pub var`s that stay
//! declared directly inside the Agent struct (never alias a `var`) — reached
diff --git a/src/agent_ws.zig b/src/agent_ws.zig
index 4bcd76c9..62e5dcd0 100644
--- a/src/agent_ws.zig
+++ b/src/agent_ws.zig
@@ -27,6 +27,11 @@ const Agent = agent_mod.Agent;
const ws = @import("ws.zig");
const http_headers = @import("http_headers.zig");
+// #422 slice 1b: this file draws nothing. Wait/cut moments are typed events
+// through the sink; the spinner and the ⚠ notices are TuiSink's rendering.
+const engine_sink = @import("engine_sink.zig");
+const engine_events = @import("engine_events.zig");
+
const term = @import("term.zig");
const tty = term.tty;
@@ -93,33 +98,21 @@ pub fn postLive(self: *Agent, body: []const u8) ![]u8 {
// reconnect budget in agent_request exhausts and the turn ends — exercises the
// give-up path offline, no network/key needed.
if (main_mod.g_force_stall_always) {
- if (!main_mod.json_mode) if (self.out) |o| {
- o.writeAll("\n⚠ stream stalled\n") catch {};
- o.flush() catch {};
- };
+ emitAbort(self, .stalled, false);
return error.StreamStalled;
}
if (main_mod.g_force_drop_always) {
- if (!main_mod.json_mode) if (self.out) |o| {
- o.writeAll("\n⚠ connection dropped\n") catch {};
- o.flush() catch {};
- };
+ emitAbort(self, .dropped, false);
return error.StreamDropped;
}
if (main_mod.g_force_stall_once) {
main_mod.g_force_stall_once = false;
- if (!main_mod.json_mode) if (self.out) |o| {
- o.writeAll("\n⚠ stream stalled — ending turn\n") catch {};
- o.flush() catch {};
- };
+ emitAbort(self, .stalled, true);
return error.StreamStalled;
}
if (main_mod.g_force_drop_once) {
main_mod.g_force_drop_once = false;
- if (!main_mod.json_mode) if (self.out) |o| {
- o.writeAll("\n⚠ connection dropped — response ended early\n") catch {};
- o.flush() catch {};
- };
+ emitAbort(self, .dropped, true);
return error.StreamDropped;
}
if (!wsEligible(self)) return self.postStream(body);
@@ -161,6 +154,14 @@ pub fn postLive(self: *Agent, body: []const u8) ![]u8 {
return response;
}
+/// (#422 slice 1b) A transport cut becomes a typed event; the sink decides
+/// what shows. TuiSink: the old inline ⚠ lines, byte for byte. JsonSink:
+/// nothing — the wire never carried these moments and must not start to.
+fn emitAbort(self: *Agent, reason: engine_events.StreamAbort, turn_ending: bool) void {
+ const cut: engine_events.TransportAbort = .{ .reason = reason, .turn_ending = turn_ending };
+ engine_sink.forAgent(self).emit(self.io, .{ .transport_aborted = cut });
+}
+
fn wssUrl(arena: std.mem.Allocator, https_url: []const u8) ![]u8 {
if (std.mem.startsWith(u8, https_url, "https://"))
return std.fmt.allocPrint(arena, "wss://{s}", .{https_url["https://".len..]});
@@ -381,8 +382,11 @@ pub fn postResponsesWs(self: *Agent, body: []const u8) ![]u8 {
tty.restore(o);
};
- self.spinnerStart();
- defer self.spinnerStop();
+ // One wait spans the whole ws turn (frames reassemble, never stream live):
+ // the same bracket events postStream uses; the wire has no shape for them.
+ const sink = engine_sink.forAgent(self);
+ sink.emit(self.io, .stream_begin);
+ defer sink.emit(self.io, .stream_finished);
if (self.tracer) |tr| tr.note("ws", "connecting");
// (#codex-ws) Preemptive idle re-anchor: don't reuse a WS the server has
@@ -518,10 +522,7 @@ pub fn postResponsesWs(self: *Agent, body: []const u8) ![]u8 {
// blocking readMessage can hang forever on a half-open ws with no
// watchdog; end the turn as StreamStalled (never a hang, never a user
// Esc), exactly as the .deadline arm below does.
- if (!main_mod.json_mode) if (self.out) |o| {
- o.writeAll("\n⚠ stream stalled — ending turn\n") catch {};
- o.flush() catch {};
- };
+ emitAbort(self, .stalled, true);
if (self.tracer) |tr| tr.note("ws", "stall");
return error.StreamStalled;
};
@@ -549,10 +550,8 @@ pub fn postResponsesWs(self: *Agent, body: []const u8) ![]u8 {
if (self.tracer) |tr| tr.note("ws", if (w == .esc) "esc" else "reuse dead — no first frame");
return watchdogError(w, error.HungRequest);
} else {
- if (w == .deadline and !main_mod.json_mode) if (self.out) |o| {
- o.writeAll("\n⚠ stream stalled — ending turn\n") catch {};
- o.flush() catch {};
- };
+ // Only the deadline gets a notice; a user Esc stays silent.
+ if (w == .deadline) emitAbort(self, .stalled, true);
if (self.tracer) |tr| tr.note("ws", if (w == .esc) "esc" else "stall");
return watchdogError(w, error.StreamStalled);
},
diff --git a/src/agent_ws_test.zig b/src/agent_ws_test.zig
index 764d208f..40acff92 100644
--- a/src/agent_ws_test.zig
+++ b/src/agent_ws_test.zig
@@ -1,12 +1,15 @@
//! Regression tests for Codex WebSocket delta-session re-anchoring.
const std = @import("std");
+const main_mod = @import("main.zig");
const Agent = @import("agent.zig").Agent;
const textMessage = @import("messages.zig").textMessage;
const ws = @import("ws.zig");
const agent_ws = @import("agent_ws.zig");
const codex_chain = @import("codex_chain.zig");
const agent_compact = @import("agent_compact.zig");
+const engine_sink = @import("engine_sink.zig");
+const engine_events = @import("engine_events.zig");
test "closeCodexWs resets the delta session state + frees the response id (codex-ws)" {
var agent: Agent = undefined;
@@ -96,6 +99,76 @@ test "WS fallback latches only after a retry" {
try std.testing.expect(agent_ws.wsShouldFallback(255));
}
+fn sinkRecord(ctx: *anyopaque, ev: engine_sink.Stamped) void {
+ const rec: *std.ArrayList(engine_sink.Stamped) = @ptrCast(@alignCast(ctx));
+ rec.append(std.testing.allocator, ev) catch @panic("OOM");
+}
+
+// (#422 slice 1b) postLive draws nothing itself anymore: each forced stall/
+// drop seam emits exactly ONE transport_aborted event through the sink
+// (TuiSink renders the old inline ⚠ lines, JsonSink drops it — pinned in
+// engine_sink.zig) and returns the matching transport error. An injected
+// recording sink observes the contract directly — no TTY, no globals read.
+test "postLive force seams emit one transport_aborted through the sink (#422)" {
+ const saved_sa = main_mod.g_force_stall_always;
+ const saved_da = main_mod.g_force_drop_always;
+ const saved_so = main_mod.g_force_stall_once;
+ const saved_do = main_mod.g_force_drop_once;
+ defer {
+ main_mod.g_force_stall_always = saved_sa;
+ main_mod.g_force_drop_always = saved_da;
+ main_mod.g_force_stall_once = saved_so;
+ main_mod.g_force_drop_once = saved_do;
+ }
+ main_mod.g_force_stall_always = false;
+ main_mod.g_force_drop_always = false;
+ main_mod.g_force_stall_once = false;
+ main_mod.g_force_drop_once = false;
+
+ var rec: std.ArrayList(engine_sink.Stamped) = .empty;
+ defer rec.deinit(std.testing.allocator);
+ const vt: engine_sink.VTable = .{ .emit = sinkRecord, .durable = false };
+ var a: Agent = .{
+ .gpa = std.testing.allocator,
+ .arena = std.testing.allocator,
+ .io = undefined,
+ .client = undefined,
+ .provider = undefined,
+ .messages = undefined,
+ .sub = false,
+ .label = "test",
+ .out = null,
+ .sink = .{ .ctx = &rec, .vt = &vt },
+ };
+
+ const Case = struct {
+ flag: *bool,
+ consumed: bool, // the once-seams reset themselves on use
+ err: anyerror,
+ reason: engine_events.StreamAbort,
+ turn_ending: bool,
+ };
+ const cases = [_]Case{
+ .{ .flag = &main_mod.g_force_stall_always, .consumed = false, .err = error.StreamStalled, .reason = .stalled, .turn_ending = false },
+ .{ .flag = &main_mod.g_force_drop_always, .consumed = false, .err = error.StreamDropped, .reason = .dropped, .turn_ending = false },
+ .{ .flag = &main_mod.g_force_stall_once, .consumed = true, .err = error.StreamStalled, .reason = .stalled, .turn_ending = true },
+ .{ .flag = &main_mod.g_force_drop_once, .consumed = true, .err = error.StreamDropped, .reason = .dropped, .turn_ending = true },
+ };
+ for (cases, 1..) |c, want_events| {
+ c.flag.* = true;
+ try std.testing.expectError(c.err, agent_ws.postLive(&a, "{}"));
+ if (c.consumed) try std.testing.expect(!c.flag.*) else c.flag.* = false;
+ try std.testing.expectEqual(want_events, rec.items.len); // exactly one event per cut
+ switch (rec.items[want_events - 1].event) {
+ .transport_aborted => |t| {
+ try std.testing.expectEqual(c.reason, t.reason);
+ try std.testing.expectEqual(c.turn_ending, t.turn_ending);
+ },
+ else => return error.TestUnexpectedEvent,
+ }
+ }
+}
+
// (#codex-ws) End-to-end regression for the reanchor fix: buildBody's
// .responses branch must emit previous_response_id + a message-slice delta
// while a WS session + prev id are held, and after closeCodexWs (called by