Skip to content

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

Python

feat(engine): tool-arg prose + transport cuts join the event vocabulary; sinks map them (#422)

Rach Pradhan317ec15c6127
diff --git a/src/engine_events.zig b/src/engine_events.zig
index 145d71a0..d54bc0ca 100644
--- a/src/engine_events.zig
+++ b/src/engine_events.zig
@@ -45,6 +45,20 @@ pub const Delta = struct {
text: []const u8,
};
+/// A live transport attempt was cut before the provider's terminal event —
+/// the transport-layer counterpart of StreamAbort, born in the connection/
+/// read guards (agent_ws.zig slice 1b) where nothing of the answer has
+/// rendered yet: sinks may surface a notice but have no held output to
+/// flush. `reason == .interrupted` is never emitted today (a deliberate Esc
+/// propagates as an error, silently); a sink renders nothing for it.
+pub const TransportAbort = struct {
+ reason: StreamAbort,
+ /// The guard is giving the turn up (its notice says so); false = an
+ /// attempt-level cut the reconnect ladder may still retry, surfaced
+ /// more tersely.
+ turn_ending: bool,
+};
+
/// Everything the streaming path tells a frontend. Each doc comment states
/// the emission site's contract, not how any one sink draws it.
pub const EngineEvent = union(enum) {
@@ -60,6 +74,18 @@ pub const EngineEvent = union(enum) {
/// TUI: streamed markdown (or raw bytes off-color). The first one also
/// ends the reasoning presentation (block close, spinner stop).
text_delta: Delta,
+ /// A chunk of user-facing prose streamed out of a whitelisted meta tool
+ /// call's still-in-flight arguments (attempt_completion's result,
+ /// ask_user's question — agent_argstream.zig, slice 1b). Never on the
+ /// wire: --json clients get the assembled tool_call instead, so a wire
+ /// sink stays silent. TUI: renders like answer text (spinner handoff
+ /// included) but does NOT end the reasoning presentation — argument
+ /// prose can stream while the model is still mid-call.
+ tool_arg_delta: Delta,
+ /// A live transport attempt was cut; the payload says how and whether
+ /// the turn ends with it (slice 1b). Distinct from stream_aborted: no
+ /// partial answer is in flight, so sinks notice without flushing.
+ transport_aborted: TransportAbort,
/// The user asked to fold/unfold the live reasoning view (Ctrl-T, #92).
/// Presentation-only; a headless frontend ignores it. TRANSITIONAL
/// (Phase 1b): this is frontend INPUT round-tripping engine-ward through
@@ -149,6 +175,18 @@ test "stamp: reserving draws fresh monotonic ids; observing never advances" {
try std.testing.expectEqual(generation(), o.generation);
}
+test "slice 1b: tool-arg prose and transport aborts are presentation pulses" {
+ // Neither ever appeared on the --json wire (argLiveDelta gates --json
+ // off; the transport notices were TTY-only), so neither may reserve a
+ // sequence id — promoting one is a schema_version event, not a default.
+ const pulses: [3]EngineEvent = .{
+ .{ .tool_arg_delta = .{ .text = "prose" } },
+ .{ .transport_aborted = .{ .reason = .stalled, .turn_ending = true } },
+ .{ .transport_aborted = .{ .reason = .dropped, .turn_ending = false } },
+ };
+ for (pulses) |ev| try std.testing.expect(!durable(ev));
+}
+
test "generation only moves forward, one restart at a time" {
const before = generation();
try std.testing.expect(before >= 1);
diff --git a/src/engine_sink.zig b/src/engine_sink.zig
index b22f04e7..fa3290e1 100644
--- a/src/engine_sink.zig
+++ b/src/engine_sink.zig
@@ -122,7 +122,35 @@ fn tuiEmit(ctx: *anyopaque, ev: Stamped) void {
if (!a.sub) _ = tick_gate.setLineStart(d.text[d.text.len - 1] == '\n'); // #tui-tick
}
},
+ // Meta-tool argument prose renders like answer text — spinner handoff
+ // included — with two deliberate differences from .text_delta, both
+ // the old inline emitArgText behavior verbatim: the reasoning block
+ // stays as-is (the model is still mid-call), and the no-color branch
+ // never marks a tick-gate line start.
+ .tool_arg_delta => |d| {
+ render.spinnerStop(a); // first visible byte: clear the thinking line
+ if (main_mod.use_color) {
+ a.streamMarkdown(d.text);
+ } else if (a.out) |w| {
+ w.writeAll(d.text) catch return;
+ w.flush() catch return;
+ }
+ },
.thinking_fold_toggle => render.toggleThinkingFold(a),
+ // Transport cuts carry no partial answer (nothing streamed on the ws
+ // path), so unlike .stream_aborted there is no tail to flush — only
+ // the notice, worded exactly as the old inline agent_ws lines were.
+ .transport_aborted => |t| notice(a, switch (t.reason) {
+ .interrupted => return, // deliberate stop: silent, as ever
+ .stalled => if (t.turn_ending)
+ "\n⚠ stream stalled — ending turn\n"
+ else
+ "\n⚠ stream stalled\n",
+ .dropped => if (t.turn_ending)
+ "\n⚠ connection dropped — response ended early\n"
+ else
+ "\n⚠ connection dropped\n",
+ }),
.stream_aborted => |reason| {
a.flushStreamTail(); // render any held partial markdown line
switch (reason) {
@@ -162,12 +190,13 @@ fn jsonEmit(ctx: *anyopaque, ev: Stamped) void {
.reasoning_delta => |d| jsonLine(w, ev.cursor, .{ .type = "reasoning", .text = d.text }),
.text_delta => |d| jsonLine(w, ev.cursor, .{ .type = "text", .text = d.text }),
// Stream end/abort still flushes the held render tail, as the old
- // inline path did in EVERY mode: emitArgText streams tool-arg prose
- // through streamMarkdown whenever use_color is on — --json on a TTY
- // included — so md_buf/md_table can hold bytes even here. Yes, that
- // interleaves non-JSONL text into the wire exactly as before;
- // making --json drop the tail (or never dirty md state) is a
- // deliberate future wire change, not slice-1 fallout.
+ // inline path did in EVERY mode. (Slice 1b correction to this note:
+ // tool-arg prose CANNOT dirty md state here — argLiveDelta has always
+ // gated --json off, and this sink drops .tool_arg_delta — so with
+ // .text_delta going to the wire the tail is clean and the flush adds
+ // no bytes today. It stays because removing a wire-visible behavior,
+ // however latent, is a deliberate schema-gated change, not refactor
+ // fallout.)
.stream_aborted, .stream_complete => a.flushStreamTail(),
else => {},
}
@@ -260,6 +289,90 @@ test "JsonSink writes today's wire lines byte-for-byte" {
);
}
+test "TuiSink streams tool-arg prose raw and never ends the answer line (slice 1b)" {
+ const saved_color = main_mod.use_color; // pin the no-color branch
+ main_mod.use_color = false;
+ defer main_mod.use_color = saved_color;
+ var aw: Io.Writer.Allocating = .init(std.testing.allocator);
+ defer aw.deinit();
+ var a: Agent = .{
+ .gpa = std.testing.allocator,
+ .arena = std.testing.allocator,
+ .io = undefined,
+ .client = undefined,
+ .provider = undefined,
+ .messages = undefined,
+ .sub = false,
+ .label = "test",
+ .out = &aw.writer,
+ };
+ const s = tuiSink(&a);
+ s.emit(undefined, .{ .tool_arg_delta = .{ .text = "answer " } });
+ s.emit(undefined, .{ .tool_arg_delta = .{ .text = "prose" } });
+ // Exactly the bytes, flushed per delta, no separators added — the old
+ // inline emitArgText no-color branch.
+ try std.testing.expectEqualStrings("answer prose", aw.writer.buffered());
+}
+
+test "TuiSink transport-abort notices reproduce the old inline lines (slice 1b)" {
+ var aw: Io.Writer.Allocating = .init(std.testing.allocator);
+ defer aw.deinit();
+ var a: Agent = .{
+ .gpa = std.testing.allocator,
+ .arena = std.testing.allocator,
+ .io = undefined,
+ .client = undefined,
+ .provider = undefined,
+ .messages = undefined,
+ .sub = false,
+ .label = "test",
+ .out = &aw.writer,
+ };
+ const s = tuiSink(&a);
+ const cases = [_]struct { ev: engine_events.TransportAbort, want: []const u8 }{
+ .{ .ev = .{ .reason = .stalled, .turn_ending = false }, .want = "\n⚠ stream stalled\n" },
+ .{ .ev = .{ .reason = .stalled, .turn_ending = true }, .want = "\n⚠ stream stalled — ending turn\n" },
+ .{ .ev = .{ .reason = .dropped, .turn_ending = false }, .want = "\n⚠ connection dropped\n" },
+ .{ .ev = .{ .reason = .dropped, .turn_ending = true }, .want = "\n⚠ connection dropped — response ended early\n" },
+ // A deliberate interrupt was never announced at the transport layer.
+ .{ .ev = .{ .reason = .interrupted, .turn_ending = true }, .want = "" },
+ };
+ for (cases) |c| {
+ aw.clearRetainingCapacity();
+ s.emit(undefined, .{ .transport_aborted = c.ev });
+ try std.testing.expectEqualStrings(c.want, aw.writer.buffered());
+ }
+}
+
+test "JsonSink stays silent for moments the wire never carried (slice 1b)" {
+ const saved_json = main_mod.json_mode; // pin: see the dispatch-order test
+ main_mod.json_mode = false;
+ defer main_mod.json_mode = saved_json;
+ protocol_seq.resetForTest();
+ defer protocol_seq.resetForTest();
+ var aw: Io.Writer.Allocating = .init(std.testing.allocator);
+ defer aw.deinit();
+ var a: Agent = .{
+ .gpa = std.testing.allocator,
+ .arena = std.testing.allocator,
+ .io = undefined,
+ .client = undefined,
+ .provider = undefined,
+ .messages = undefined,
+ .sub = false,
+ .label = "test",
+ .out = &aw.writer,
+ };
+ const s = jsonSink(&a);
+ s.emit(undefined, .{ .tool_arg_delta = .{ .text = "prose" } });
+ s.emit(undefined, .{ .transport_aborted = .{ .reason = .stalled, .turn_ending = true } });
+ s.emit(undefined, .{ .transport_aborted = .{ .reason = .dropped, .turn_ending = false } });
+ // No wire line AND no sequence id burned: both stay pulses, so the
+ // wire's gap-free numbering is untouched by them (#330).
+ try std.testing.expectEqualStrings("", aw.writer.buffered());
+ try std.testing.expectEqual(@as(u64, 0), protocol_seq.current());
+}
+
test "TuiSink renders a plain text delta exactly as the no-color TTY did" {
var aw: Io.Writer.Allocating = .init(std.testing.allocator);
defer aw.deinit();