graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
fix: keep thinking live while queuing follow-ups (#845)
Rachfcdc3298abca
diff --git a/TUI/issue_845_tests.zig b/TUI/issue_845_tests.zignew file mode 100644index 00000000..0fc90fa2--- /dev/null+++ b/TUI/issue_845_tests.zig@@ -0,0 +1,86 @@+//! #845: composing or queuing a follow-up must not freeze the thinking+//! indicator or interrupt the live turn. Driven through Term (AGENTS.md).++const std = @import("std");++const engine = @import("engine.zig");+const glyphs = @import("glyphs.zig");+const Term = @import("sim.zig").Term;++fn attachThinking(term: *Term, alloc: std.mem.Allocator) !*engine.Job {+ try term.model.push(.user, "think about this");+ try term.model.push(.pending, "");+ const job = try alloc.create(engine.Job);+ job.* = .{+ .gpa = alloc,+ .history = &.{},+ .params = .{},+ .stream = .{},+ .raw = .{},+ .threaded = false,+ };+ job.events.attach(alloc);+ term.model.pending = job;+ return job;+}++test "queuing a follow-up keeps the thinking indicator live (#845)" {+ const alloc = std.testing.allocator;+ var term: Term = undefined;+ term.init(alloc, 80, 24);+ defer term.deinit();+ const job = try attachThinking(&term, alloc);+ _ = job;++ term.now_ms = 0;+ const idle = try term.screen();+ defer alloc.free(idle);+ try std.testing.expect(std.mem.indexOf(u8, idle, "Thinking") != null);+ try std.testing.expect(std.mem.indexOf(u8, idle, glyphs.thinking[0]) != null);++ _ = term.typeText("queued follow-up");+ try std.testing.expectEqualStrings("queued follow-up", term.model.input.getValue());+ try std.testing.expect(term.model.pending != null);+ try std.testing.expect(!term.model.cancel_requested);++ term.now_ms = 500;+ const composing = try term.screen();+ defer alloc.free(composing);+ try std.testing.expect(std.mem.indexOf(u8, composing, "Thinking") != null);+ try std.testing.expect(std.mem.indexOf(u8, composing, "queued follow-up") != null);+ try std.testing.expect(std.mem.indexOf(u8, composing, glyphs.thinking[1]) != null);+ try std.testing.expect(std.mem.indexOf(u8, composing, glyphs.thinking[0]) == null);++ _ = term.enter();+ try std.testing.expectEqual(@as(usize, 1), term.model.steer_queue.items.len);+ try std.testing.expectEqualStrings("queued follow-up", term.model.steer_queue.items[0]);+ try std.testing.expectEqualStrings("", term.model.input.getValue());+ try std.testing.expect(term.model.pending != null);+ try std.testing.expect(!term.model.cancel_requested);+ try std.testing.expect(term.model.pending.? == job);++ term.now_ms = 1000;+ const queued = try term.screen();+ defer alloc.free(queued);+ try std.testing.expect(std.mem.indexOf(u8, queued, "Thinking") != null);+ try std.testing.expect(std.mem.indexOf(u8, queued, "queued") != null);+ try std.testing.expect(std.mem.indexOf(u8, queued, glyphs.thinking[0]) != null);+ try std.testing.expect(std.mem.indexOf(u8, queued, glyphs.thinking[1]) == null);+}++test "empty Enter with a queued follow-up is the interrupt; typing is not (#845)" {+ const alloc = std.testing.allocator;+ var term: Term = undefined;+ term.init(alloc, 80, 24);+ defer term.deinit();+ _ = try attachThinking(&term, alloc);++ _ = term.typeText("next");+ _ = term.enter();+ try std.testing.expectEqual(@as(usize, 1), term.model.steer_queue.items.len);+ try std.testing.expect(!term.model.cancel_requested);++ _ = term.enter();+ try std.testing.expect(term.model.cancel_requested);+ try std.testing.expect(term.model.pending != null);+}diff --git a/TUI/root.zig b/TUI/root.zigindex 307b2b6e..0bb2bebb 100644--- a/TUI/root.zig+++ b/TUI/root.zig@@ -112,6 +112,7 @@ test {_ = @import("layout_cache.zig");_ = @import("selection.zig");_ = @import("selection_tests.zig");+ _ = @import("issue_845_tests.zig");_ = @import("theme_tint.zig");_ = @import("hover.zig");_ = @import("hover_tests.zig");diff --git a/src/agent_interrupt.zig b/src/agent_interrupt.zigindex 0accccf1..79502038 100644--- a/src/agent_interrupt.zig+++ b/src/agent_interrupt.zig@@ -186,14 +186,18 @@ pub fn escPressed(echo: bool) bool {if (echo) {if (!main_mod.g_steer_echoed) {main_mod.g_steer_visible.store(true, .release);- repl_glue.steerEcho("\n");- repl_glue.steerEcho(style.accent);- repl_glue.steerEcho("↳ steer ›");- repl_glue.steerEcho(style.reset);- repl_glue.steerEcho(" ");+ repl_glue.steerLock();+ repl_glue.steerEchoUnlocked("\n");+ repl_glue.steerEchoUnlocked(style.accent);+ repl_glue.steerEchoUnlocked("↳ steer ›");+ repl_glue.steerEchoUnlocked(style.reset);+ repl_glue.steerEchoUnlocked(" ");main_mod.g_steer_echoed = true;+ repl_glue.steerEchoUnlocked(buf[i .. i + 1]);+ repl_glue.steerUnlock();+ } else {+ repl_glue.steerEcho(buf[i .. i + 1]);}- repl_glue.steerEcho(buf[i .. i + 1]);}}return esc_found;diff --git a/src/agent_stream_render.zig b/src/agent_stream_render.zigindex ef1b4899..9c4c0b40 100644--- a/src/agent_stream_render.zig+++ b/src/agent_stream_render.zig@@ -32,36 +32,61 @@ test {_ = @import("compact_status.zig");}+/// CSI around one spinner frame. When the steer row is live, hop up one+/// line (DECSC/DECRC) so the frame never wipes the queued follow-up (#845).+pub fn spinnerFramePrefix(steer_visible: bool) []const u8 {+ return if (steer_visible)+ "\x1b7\x1b[1A\r\x1b[2K\x1b[?7l"+ else+ "\r\x1b[2K\x1b[?7l";+}++pub fn spinnerFrameSuffix(steer_visible: bool) []const u8 {+ return if (steer_visible) "\x1b[?7h\x1b8" else "\x1b[?7h";+}++pub fn spinnerStopSeq(steer_visible: bool) []const u8 {+ return if (steer_visible)+ "\x1b[?7h\x1b7\x1b[1A\r\x1b[2K\x1b8"+ else+ "\x1b[?7h\r\x1b[2K";+}+pub fn spinnerTask(io: Io) void {var i: usize = 0;var buf: [512]u8 = undefined;var w = Io.File.stdout().writer(io, &buf);+ const glue = @import("repl_glue.zig");while (!Agent.g_spin_stop.load(.acquire)) {- if (main_mod.g_steer_visible.load(.acquire)) {- io.sleep(.fromMilliseconds(20), .awake) catch break;- continue;- }- // Clear-then-draw each frame: animations may vary in width.- w.interface.writeAll("\r\x1b[2K\x1b[?7l") catch return; // ?7l: autowrap off so a wide spinner truncates instead of wrapping in a narrow window (the "goes on and on" bug)- if (@import("compact_status.zig").isActive()) {- w.interface.writeAll(@import("compact_status.zig").label) catch return;- } else {- anim.anims[anim.g_anim_current].frame(&w.interface, i) catch return;- }- w.interface.writeAll("\x1b[?7h") catch return; // restore autowrap- w.interface.flush() catch return;+ const steer = main_mod.g_steer_visible.load(.acquire);+ glue.steerLock();+ // Clear-then-draw each frame: animations may vary in width. ?7l:+ // autowrap off so a wide spinner truncates instead of wrapping.+ const drew = blk: {+ w.interface.writeAll(spinnerFramePrefix(steer)) catch break :blk false;+ if (@import("compact_status.zig").isActive()) {+ w.interface.writeAll(@import("compact_status.zig").label) catch break :blk false;+ } else {+ anim.anims[anim.g_anim_current].frame(&w.interface, i) catch break :blk false;+ }+ w.interface.writeAll(spinnerFrameSuffix(steer)) catch break :blk false;+ w.interface.flush() catch break :blk false;+ break :blk true;+ };+ glue.steerUnlock();+ if (!drew) return;i += 1;const frame_ticks = @max(@as(usize, 1), @as(usize, anim.anims[anim.g_anim_current].frame_ms) / 20);var t: usize = 0;while (t < frame_ticks and !Agent.g_spin_stop.load(.acquire)) : (t += 1) {- if (main_mod.g_steer_visible.load(.acquire)) break;io.sleep(.fromMilliseconds(20), .awake) catch break;}}- if (!main_mod.g_steer_visible.load(.acquire)) {- w.interface.writeAll("\x1b[?7h\r\x1b[2K") catch return; // restore autowrap + clear- w.interface.flush() catch {};- }+ const steer = main_mod.g_steer_visible.load(.acquire);+ glue.steerLock();+ w.interface.writeAll(spinnerStopSeq(steer)) catch {};+ w.interface.flush() catch {};+ glue.steerUnlock();}pub fn spinnerStart(self: *Agent) void {@@ -156,3 +181,18 @@ pub fn toggleThinkingFold(self: *Agent) void {}w.flush() catch return;}++test "a live steer row hops the spinner up one line instead of freezing (#845)" {+ const hop = spinnerFramePrefix(true);+ try std.testing.expect(std.mem.indexOf(u8, hop, "\x1b7") != null);+ try std.testing.expect(std.mem.indexOf(u8, hop, "\x1b[1A") != null);+ try std.testing.expect(std.mem.startsWith(u8, hop, "\x1b7"));+ try std.testing.expectEqualStrings("\x1b[?7h\x1b8", spinnerFrameSuffix(true));+ try std.testing.expect(std.mem.indexOf(u8, spinnerStopSeq(true), "\x1b[1A") != null);+}++test "without a steer row the spinner still redraws in place (#845)" {+ try std.testing.expectEqualStrings("\r\x1b[2K\x1b[?7l", spinnerFramePrefix(false));+ try std.testing.expectEqualStrings("\x1b[?7h", spinnerFrameSuffix(false));+ try std.testing.expectEqualStrings("\x1b[?7h\r\x1b[2K", spinnerStopSeq(false));+}diff --git a/src/main.zig b/src/main.zigindex f9cb6146..3afdc814 100644--- a/src/main.zig+++ b/src/main.zig@@ -200,12 +200,12 @@ pub var g_codedbpro_licensed: bool = false; // pub: codedbpro_report's native-toconst anim = @import("anim.zig");// Steering (Codex-style): bytes typed while a turn streams are captured (not discarded), echoed live in dim emerald, and queued to run next on Enter —// follow-ups queue without waiting for the turn to finish. TTY-only (raw-stdin esc-watch is off in --json/GUI mode); watchdog/select arms may-// drain/echo stdin while the stream reader is blocked, so g_steer_visible pauses spinner redraws to keep the live row intact.+// drain/echo stdin while the stream reader is blocked. g_steer_visible: spinner hops up one line (#845).pub var g_steer_buf: std.ArrayList(u8) = .empty; // in-progress line (page-alloc)const SteerEntry = repl_glue.SteerEntry; // struct { text: []const u8, force: bool }pub var g_steer_queue: std.ArrayList(SteerEntry) = .empty; // completed linespub var g_steer_echoed = false; // "↳ steer ›" prefix shown for the current line-pub var g_steer_visible: std.atomic.Value(bool) = .init(false); // visible live steering row; pauses spinner redraws+pub var g_steer_visible: std.atomic.Value(bool) = .init(false); // visible live steering row; spinner draws on the line abovepub var g_steer_lock: std.atomic.Value(bool) = .init(false); // spin-guards g_steer_queue/g_steer_buf mutations across concurrent drainers (main reader + pool esc-watch/watchdog arms) so one submit never flushes as N entries (#129); acquire via repl_glue.steerLockpub var g_out: ?*Io.Writer = null; // stdout writer for steer echo (set in main)pub var g_gui_mu: Io.Mutex = .init; // serializes --json stdout across pool-thread subagent emits (guiEmit + printDelta)diff --git a/src/repl_glue.zig b/src/repl_glue.zigindex 23bacb3c..403a74b1 100644--- a/src/repl_glue.zig+++ b/src/repl_glue.zig@@ -371,11 +371,12 @@ pub fn replCancelCb(ctx_ptr: ?*anyopaque) void {pub const SteerEntry = struct { text: []const u8, force: bool };-/// Spin-lock guarding g_steer_buf/g_steer_queue mutations. The concurrent steer-/// drainers (the main reader + pool esc-watch/watchdog arms, #129) hold it only-/// for the tiny queue/buffer critical sections — never across a stdin read/poll-/// — so it stays uncontended and needs no Io handle (unlike the Io.Mutex used-/// elsewhere, which the io-less escPressed cannot reach).+/// Spin-lock guarding g_steer_buf/g_steer_queue mutations and the stdout+/// bytes that share a line with the thinking spinner (#845). The concurrent+/// steer drainers (the main reader + pool esc-watch/watchdog arms, #129) hold+/// it only for the tiny queue/buffer critical sections — never across a stdin+/// read/poll — so it stays uncontended and needs no Io handle (unlike the+/// Io.Mutex used elsewhere, which the io-less escPressed cannot reach).pub fn steerLock() void {while (main_mod.g_steer_lock.cmpxchgWeak(false, true, .acquire, .monotonic) != null) std.atomic.spinLoopHint();}@@ -427,7 +428,14 @@ pub fn resetSteerPartial() void {/// Writes steering echo to the stdout writer (the same buffered writer the/// streaming text uses, already flushed before escPressed runs, so ordering/// stays correct) and flushes so the user sees queued keystrokes live.+/// Serialized with spinner frames so a hop-up redraw cannot land mid-echo (#845).pub fn steerEcho(bytes: []const u8) void {+ steerLock();+ defer steerUnlock();+ steerEchoUnlocked(bytes);+}++pub fn steerEchoUnlocked(bytes: []const u8) void {if (main_mod.g_out) |w| {w.writeAll(bytes) catch {};w.flush() catch {};