graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix(repl): keep mid-turn reconnect and call counters off the transcript
Cursor Agent9fe6ca3b276a
diff --git a/docs/adr/0021-transcript-is-the-task-not-the-bus.md b/docs/adr/0021-transcript-is-the-task-not-the-bus.mdindex ba2c7fbc..28bf067b 100644--- a/docs/adr/0021-transcript-is-the-task-not-the-bus.md+++ b/docs/adr/0021-transcript-is-the-task-not-the-bus.md@@ -21,8 +21,9 @@ Every harness event is one of four classes:| Useful operation | a semantic one-liner || New information | model narration |-`note_constraint`, `skill`, `read_tool_result`, and successful compaction are-bookkeeping (`/debug` still prints compaction). `todo_write` reprints a+`note_constraint`, `skill`, `read_tool_result`, successful compaction, mid-turn+stall/reconnect, and inner-loop "model call N" pulses are bookkeeping (`/debug`+still prints compaction). `todo_write` reprints a`WORKING` block only when the checklist changes, and the same block sitsabove `›`. Subagents are `↯ scout` / `✓ scout` one-liners; the `sa-` id isnot foreground UI. `batch` is `inspect`. Observe, then conclude, then act.diff --git a/src/agent_request.zig b/src/agent_request.zigindex 3cbf5ae0..26753850 100644--- a/src/agent_request.zig+++ b/src/agent_request.zig@@ -282,8 +282,8 @@ pub fn request(self: *Agent, tools_in: ?[]const u8) !std.json.ObjectMap {// the session — codex's WS→SSE fallback, and consistent// with how a WS transport error is already handled (ws_off).if (stall_retries > 1) self.ws_off = true;- const via: []const u8 = if (self.ws_off) " (SSE)" else "";- try self.say("[{s} — reconnecting{s} ({d}/{d})]\n", .{ what, via, stall_retries, max_stall_retries });+ // Reconnect is bookkeeping (ADR 0021). Tracer and+ // telemetry keep the attempt; the transcript does not.if (self.tracer) |tr| tr.note("stream_retry", what);if (telemetry.g_telem) |t| t.errorEvent("stream_retry", what);self.partial_text.clearRetainingCapacity(); // fresh stream re-streams cleanly, no concatdiff --git a/src/agent_ws_test.zig b/src/agent_ws_test.zigindex 52dfb8c6..92e926bd 100644--- a/src/agent_ws_test.zig+++ b/src/agent_ws_test.zig@@ -117,7 +117,7 @@ fn sinkRecord(ctx: *anyopaque, ev: engine_sink.Stamped) void {// (#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+// (ending-turn ⚠ only; mid-turn is silent — ADR 0021, 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)" {diff --git a/src/engine_events.zig b/src/engine_events.zigindex 6c86c47b..5bcd2e38 100644--- a/src/engine_events.zig+++ b/src/engine_events.zig@@ -54,8 +54,8 @@ pub const Delta = struct {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.+ /// attempt-level cut the reconnect ladder may still retry — silent on+ /// the transcript (ADR 0021).turn_ending: bool,};diff --git a/src/engine_sink.zig b/src/engine_sink.zigindex b2c46634..36b9299d 100644--- a/src/engine_sink.zig+++ b/src/engine_sink.zig@@ -263,17 +263,10 @@ fn tuiEmit(ctx: *anyopaque, ev: Stamped) void {// 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",- }),+ .transport_aborted => |t| {+ const line = transportEndingNotice(t) orelse return;+ notice(a, line);+ },.stream_aborted => |reason| {a.flushStreamTail(); // render any held partial markdown lineswitch (reason) {@@ -482,6 +475,17 @@ pub fn compactArg(input: std.json.Value) []const u8 {return "";}+/// Mid-turn transport cuts are the reconnect ladder (ADR 0021). Only a+/// turn that is actually ending earns a ⚠ on the transcript.+fn transportEndingNotice(t: engine_events.TransportAbort) ?[]const u8 {+ if (!t.turn_ending or t.reason == .interrupted) return null;+ return switch (t.reason) {+ .interrupted => null,+ .stalled => "\n⚠ stream stalled — ending turn\n",+ .dropped => "\n⚠ connection dropped — response ended early\n",+ };+}+pub fn firstLineCap(text: []const u8, cap: usize) []const u8 {var s = std.mem.trim(u8, text, " \t\r\n");if (std.mem.indexOfScalar(u8, s, '\n')) |i| s = std.mem.trim(u8, s[0..i], " \t\r");diff --git a/src/engine_sink_tests.zig b/src/engine_sink_tests.zigindex 5cc6a788..2637f302 100644--- a/src/engine_sink_tests.zig+++ b/src/engine_sink_tests.zig@@ -125,15 +125,15 @@ test "TuiSink streams tool-arg prose raw and never ends the answer line (slice 1try std.testing.expectEqualStrings("answer prose", aw.writer.buffered());}-test "TuiSink transport-abort notices reproduce the old inline lines (slice 1b)" {+test "TuiSink transport-abort notices only when the turn is ending (ADR 0021)" {var aw: Io.Writer.Allocating = .init(std.testing.allocator);defer aw.deinit();var a = testAgent(&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 = false }, .want = "" },.{ .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 = false }, .want = "" },.{ .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 = "" },@@ -145,6 +145,13 @@ test "TuiSink transport-abort notices reproduce the old inline lines (slice 1b)"}}+test "stall reconnect is not announced on the transcript (ADR 0021)" {+ const src = @embedFile("agent_request.zig");+ try std.testing.expect(std.mem.indexOf(u8, src, "[{s} — reconnecting") == null);+ try std.testing.expect(std.mem.indexOf(u8, src, "reconnecting{s}") == null);+ try std.testing.expect(std.mem.indexOf(u8, src, "tr.note(\"stream_retry\"") != null);+}+test "JsonSink stays silent for moments the wire never carried (slice 1b)" {const saved_json = main_mod.json_mode; // pin: see the dispatch-order testmain_mod.json_mode = false;diff --git a/src/tui_sink.zig b/src/tui_sink.zigindex 688a56b4..31563497 100644--- a/src/tui_sink.zig+++ b/src/tui_sink.zig@@ -167,12 +167,16 @@ fn emit(ctx: *anyopaque, ev: engine_sink.Stamped) void {std.fmt.bufPrint(&buf, "{s}{s}", .{ n.lead, n.text }) catch n.lead;b.queue.push(.{ .notice = line });},- .transport_aborted => |t| switch (t.reason) {- // A deliberate Esc is silent here as everywhere: finishJob already- // writes the one "■ interrupted" row.- .interrupted => {},- .stalled => b.queue.push(.{ .notice = "⚠ stream stalled" }),- .dropped => b.queue.push(.{ .notice = "⚠ connection dropped" }),+ .transport_aborted => |t| {+ // Mid-turn cuts are the reconnect ladder (ADR 0021). The pager+ // already shows Thinking / tool rows; a ⚠ here is what made+ // people hit Esc on a turn that was still recovering.+ if (!t.turn_ending) return;+ switch (t.reason) {+ .interrupted => {},+ .stalled => b.queue.push(.{ .notice = "⚠ stream stalled — ending turn" }),+ .dropped => b.queue.push(.{ .notice = "⚠ connection dropped — response ended early" }),+ }},.stream_aborted => |reason| switch (reason) {.interrupted => {},@@ -431,6 +435,31 @@ test "the turn-sink binding is thread-local and released after the turn" {try std.testing.expect(engine_sink.turnSink() == null);}+test "mid-turn transport cuts stay off the pager (ADR 0021)" {+ var qbuf: tui.EventQueue = .{};+ qbuf.attach(std.testing.allocator);+ defer qbuf.deinit();+ var sbuf: [64]u8 = undefined;+ var stream: repl.StreamBuf = .{ .buf = &sbuf };+ var bridge: Bridge = .{ .queue = &qbuf, .stream = &stream };+ const sink = forBridge(&bridge);+ sink.emit(undefined, .{ .transport_aborted = .{ .reason = .stalled, .turn_ending = false } });+ sink.emit(undefined, .{ .transport_aborted = .{ .reason = .dropped, .turn_ending = false } });+ sink.emit(undefined, .{ .transport_aborted = .{ .reason = .interrupted, .turn_ending = true } });+ {+ const silent = qbuf.drain();+ defer qbuf.free(silent);+ try std.testing.expectEqual(@as(usize, 0), silent.len);+ }+ sink.emit(undefined, .{ .transport_aborted = .{ .reason = .stalled, .turn_ending = true } });+ sink.emit(undefined, .{ .transport_aborted = .{ .reason = .dropped, .turn_ending = true } });+ const evs = qbuf.drain();+ defer qbuf.free(evs);+ try std.testing.expectEqual(@as(usize, 2), evs.len);+ try std.testing.expectEqualStrings("⚠ stream stalled — ending turn", evs[0].notice);+ try std.testing.expectEqualStrings("⚠ connection dropped — response ended early", evs[1].notice);+}+test "acp_owns_transcript leaves thought/tool/text to session/update" {var qbuf: tui.EventQueue = .{};qbuf.attach(std.testing.allocator);diff --git a/src/turn_chrome.zig b/src/turn_chrome.zigindex db90078d..ea3805e5 100644--- a/src/turn_chrome.zig+++ b/src/turn_chrome.zig@@ -1,13 +1,13 @@//! Dim chrome for long inner-loop turns and API transport retries (retry n/N).//!//! Production default: unlimited inner-loop model calls (`max_turn_model_calls = 0`).-//! `GRAFF_MAX_TURN_MODEL_CALLS` is the opt-in cap. JSON mode is dropped inside-//! `tool_pulse.emitNotice` (ADR 0020: chrome, not output).+//! `GRAFF_MAX_TURN_MODEL_CALLS` is the opt-in cap. Inner-loop counters stay+//! off the transcript (ADR 0021); retry notices still go through+//! `tool_pulse.emitNotice` (ADR 0020: chrome, not output; --json drops them).const std = @import("std");const Io = std.Io;-const main_mod = @import("main.zig");const Agent = @import("agent.zig").Agent;const tool_pulse = @import("tool_pulse.zig");@@ -25,9 +25,14 @@ pub fn formatRetryNotice(buf: []u8, class: []const u8, attempt: usize, max_attem}pub fn formatTurnPulse(buf: []u8, call_n: u64, cap: u64, tools: u64) []const u8 {- if (cap == 0)- return std.fmt.bufPrint(buf, "· turn still going · model call {d} · {d} tools", .{ call_n, tools }) catch buf[0..0];- return std.fmt.bufPrint(buf, "· turn still going · model call {d}/{d} · {d} tools", .{ call_n, cap, tools }) catch buf[0..0];+ // Inner-loop counters are bookkeeping (ADR 0021). Tool rows already+ // show the work; "model call N · N tools" sat at the same weight as+ // "Wrote 1 file" and made a turn unreadable.+ _ = buf;+ _ = call_n;+ _ = cap;+ _ = tools;+ return "";}pub fn emitRetryNotice(io: Io, class: []const u8, attempt: usize, max_attempts: usize) void {@@ -37,17 +42,13 @@ pub fn emitRetryNotice(io: Io, class: []const u8, attempt: usize, max_attempts:tool_pulse.emitNotice(io, "{s}", .{text});}-/// Count this inner-loop request, pulse from call 2, optionally pause if a-/// non-zero cap is set. Returns pause text or null to proceed. Never pauses-/// when the cap is 0.+/// Count this inner-loop request and optionally pause if a non-zero cap is+/// set. Returns pause text or null to proceed. Never pauses when the cap is 0.pub fn beforeRequest(self: *Agent) !?[]const u8 {self.model_calls_this_turn += 1;const cap = max_turn_model_calls;- if (self.model_calls_this_turn >= 2 and !self.sub and !main_mod.json_mode) {- var buf: [120]u8 = undefined;- const text = formatTurnPulse(&buf, self.model_calls_this_turn, cap, self.tool_calls_this_turn);- if (text.len > 0) tool_pulse.emitNotice(self.io, "{s}", .{text});- }+ // Call 2+ used to print an inner-loop counter line. That line is gone;+ // formatTurnPulse is the pin that keeps it gone.if (cap != 0 and self.model_calls_this_turn > cap) {return try std.fmt.allocPrint(self.arena,@@ -73,8 +74,18 @@ test "formatRetryNotice names HungRequest and UnknownHostName with attempt n/N"try std.testing.expectEqualStrings("· retry 1/6 · UnknownHostName", formatRetryNotice(&buf, "UnknownHostName", 1, 6));}-test "formatTurnPulse omits a denominator when the cap is unlimited" {+test "formatTurnPulse stays off the transcript (ADR 0021)" {var buf: [80]u8 = undefined;- try std.testing.expectEqualStrings("· turn still going · model call 3 · 11 tools", formatTurnPulse(&buf, 3, 0, 11));- try std.testing.expectEqualStrings("· turn still going · model call 3/64 · 11 tools", formatTurnPulse(&buf, 3, 64, 11));+ try std.testing.expectEqualStrings("", formatTurnPulse(&buf, 3, 0, 11));+ try std.testing.expectEqualStrings("", formatTurnPulse(&buf, 3, 64, 11));+}++test "beforeRequest does not print an inner-loop pulse (ADR 0021)" {+ const src = @embedFile("turn_chrome.zig");+ const start = std.mem.indexOf(u8, src, "pub fn beforeRequest").?;+ const rest = src[start + 1 ..];+ const end = std.mem.indexOf(u8, rest, "\ntest ").?;+ const body = rest[0..end];+ try std.testing.expect(std.mem.indexOf(u8, body, "emitNotice") == null);+ try std.testing.expect(std.mem.indexOf(u8, body, "turn still going") == null);}