Skip to content

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

Python

feat(peer): idle TUI auto-turn on new durable-room mail (#1001)

Rach Pradhan4ad6090c68cb
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d18ce933..386c5810 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,7 @@ current is part of cutting a release.
## Unreleased
- Opt-in Accord Unix 0600 live path behind `presence_chan.postMessage` (`GRAFF_ACCORD=1`); JSONL stays the durable room (ADR 0134).
+- `#1001`: idle TUI starts a turn when the durable peer room has new unread mail (one auto-turn per batch; inbox consume resets).
- Live 12-PR board on grok-4.6 SuperGrok (n=3, no SPEC.md): graff / Pi /
OpenCode 12/12, grok 11/12 (`#727`), exo 9/12 (gemini + two turbos —
diff --git a/docs/adr/0134-accord-live-jsonl-durable.md b/docs/adr/0134-accord-live-jsonl-durable.md
index b03dcf6e..ade417f0 100644
--- a/docs/adr/0134-accord-live-jsonl-durable.md
+++ b/docs/adr/0134-accord-live-jsonl-durable.md
@@ -17,5 +17,6 @@ Do not replace the room with Mailbox.
## Consequences
-Idle sessions still only drain on the next `runTurn` (#1001). Flipping the
-flag on does not change history pull (ADR 0004).
+Idle TUI sessions auto-start a turn on new parked peer mail (#1001), one
+batch at a time; `action=inbox` resets the latch. Flipping GRAFF_ACCORD
+does not change history pull (ADR 0004).
diff --git a/src/main.zig b/src/main.zig
index 61522e51..fddfcf22 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -590,7 +590,7 @@ test { // ── Unit tests (`zig build test`): pull in tests from imported modu
_ = @import("goal_flow.zig");
_ = @import("goal_todo.zig");
_ = @import("goal_pacing.zig");
- _ = .{ @import("presence_accord.zig"), @import("presence_chan.zig"), @import("presence_record.zig"), @import("peer_inbox_content_test.zig"), @import("peer_inbox_failure_test.zig"), @import("peer_inbox_storage_test.zig"), @import("peer_inbox_sequence_test.zig"), @import("peer_inbox_snapshot_test.zig") };
+ _ = .{ @import("presence_accord.zig"), @import("peer_idle.zig"), @import("presence_chan.zig"), @import("presence_record.zig"), @import("peer_inbox_content_test.zig"), @import("peer_inbox_failure_test.zig"), @import("peer_inbox_storage_test.zig"), @import("peer_inbox_sequence_test.zig"), @import("peer_inbox_snapshot_test.zig") };
_ = @import("acp_agents.zig");
_ = @import("subagent_activity.zig");
_ = @import("subagent_recovery.zig");
diff --git a/src/peer_channel.zig b/src/peer_channel.zig
index bcbfc2cc..b0fa4d79 100644
--- a/src/peer_channel.zig
+++ b/src/peer_channel.zig
@@ -27,6 +27,7 @@ const repl = @import("repl.zig");
const peer_context = @import("peer_context.zig");
const peer_inbox = @import("peer_inbox.zig");
const peer_target = @import("peer_target.zig");
+const peer_idle = @import("peer_idle.zig");
const Agent = agent_mod.Agent;
const ToolCall = tools_mod.ToolCall;
@@ -87,6 +88,7 @@ pub fn handleMessage(self: *Agent, call: ToolCall) !ExecResult {
.text = "peer_message: inbox read failed; messages and dropped count retained — retry action=inbox",
.is_error = true,
};
+ peer_idle.noteInboxConsumed();
return .{ .text = text, .is_error = false };
}
if (!std.mem.eql(u8, action, "send")) return .{
diff --git a/src/peer_idle.zig b/src/peer_idle.zig
new file mode 100644
index 00000000..0d6f9ece
--- /dev/null
+++ b/src/peer_idle.zig
@@ -0,0 +1,107 @@
+//! #1001: idle roots start a turn when the durable room has unread peer mail.
+//! JSONL drain is the source of truth; Accord Unix (ADR 0134) is opt-in live
+//! on post. One auto-turn per new unread batch — inbox consume resets.
+
+const std = @import("std");
+const Io = std.Io;
+const Allocator = std.mem.Allocator;
+
+const peer_inbox = @import("peer_inbox.zig");
+const peer_target = @import("peer_target.zig");
+const presence = @import("presence.zig");
+const presence_chan = @import("presence_chan.zig");
+
+var last_woken_unread: usize = 0;
+
+pub fn resetForTest() void {
+ last_woken_unread = 0;
+}
+
+fn ingest(io: Io, arena: Allocator) void {
+ const local = presence.drainChannel(io, arena);
+ const device = presence.drainDevice(io, arena);
+ const own = presence.ownSession();
+ var tree: std.ArrayList(presence.Message) = .empty;
+ for (local) |m| {
+ if (peer_target.treeHears(m, own)) tree.append(arena, m) catch break;
+ }
+ var heard: std.ArrayList(presence.Message) = .empty;
+ for (device) |m| {
+ if (peer_target.deviceHears(m, own)) heard.append(arena, m) catch break;
+ }
+ _ = peer_inbox.parkHeard(tree.items, heard.items);
+}
+
+/// Idle TUI auto-turn. Null when nothing new is parked. Does not inject
+/// history — `runTurn`'s deliverInbound handles that if JSONL still has bytes.
+pub fn takeIdleWake(io: Io, arena: Allocator, buf: []u8) ?[]const u8 {
+ ingest(io, arena);
+ const n = peer_inbox.unread();
+ if (n == 0 or n <= last_woken_unread) return null;
+ last_woken_unread = n;
+ const wake = peer_inbox.formatWake(arena);
+ if (wake.len == 0) return null;
+ const copy = @min(wake.len, buf.len);
+ @memcpy(buf[0..copy], wake[0..copy]);
+ return buf[0..copy];
+}
+
+/// `action=inbox` consumed the batch — a later peer can wake again.
+pub fn noteInboxConsumed() void {
+ last_woken_unread = 0;
+}
+
+test "#1001 idle wake fires once per new unread batch" {
+ peer_inbox.resetForTest();
+ resetForTest();
+ defer {
+ peer_inbox.resetForTest();
+ resetForTest();
+ }
+ const io = std.testing.io;
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ var buf: [256]u8 = undefined;
+ try std.testing.expect(takeIdleWake(io, arena, &buf) == null);
+
+ const msg: presence_chan.Message = .{
+ .from_pid = 2,
+ .from_start = 1,
+ .from_session = "s-peer",
+ .text = "hold gui/src",
+ .to = "s-me",
+ };
+ try std.testing.expectEqual(@as(usize, 1), peer_inbox.parkHeard(&.{msg}, &.{}));
+ const first = takeIdleWake(io, arena, &buf) orelse return error.ExpectedWake;
+ try std.testing.expect(std.mem.indexOf(u8, first, "[peer]") != null);
+ try std.testing.expect(std.mem.indexOf(u8, first, "inbox") != null);
+ try std.testing.expectEqual(@as(usize, 1), peer_inbox.unread());
+ try std.testing.expect(takeIdleWake(io, arena, &buf) == null);
+
+ _ = try peer_inbox.takeAll(arena);
+ noteInboxConsumed();
+ try std.testing.expect(takeIdleWake(io, arena, &buf) == null);
+}
+
+test "#1001 more mail after a wake can fire again" {
+ peer_inbox.resetForTest();
+ resetForTest();
+ defer {
+ peer_inbox.resetForTest();
+ resetForTest();
+ }
+ const io = std.testing.io;
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const arena = arena_state.allocator();
+ var buf: [256]u8 = undefined;
+ const a: presence_chan.Message = .{ .from_pid = 2, .from_start = 1, .from_session = "s-a", .text = "one" };
+ const b: presence_chan.Message = .{ .from_pid = 3, .from_start = 1, .from_session = "s-b", .text = "two" };
+ _ = peer_inbox.parkHeard(&.{a}, &.{});
+ try std.testing.expect(takeIdleWake(io, arena, &buf) != null);
+ try std.testing.expect(takeIdleWake(io, arena, &buf) == null);
+ _ = peer_inbox.parkHeard(&.{b}, &.{});
+ const again = takeIdleWake(io, arena, &buf) orelse return error.ExpectedSecond;
+ try std.testing.expect(std.mem.indexOf(u8, again, "[peer]") != null);
+}
diff --git a/src/tui_launch.zig b/src/tui_launch.zig
index c8ea680e..a9f40244 100644
--- a/src/tui_launch.zig
+++ b/src/tui_launch.zig
@@ -26,6 +26,7 @@ const engine_sink = @import("engine_sink.zig");
const tui_sink = @import("tui_sink.zig");
const tui_acp = @import("tui_acp.zig");
const job_notify = @import("job_notify.zig");
+const peer_idle = @import("peer_idle.zig");
const schedule = @import("schedule.zig");
const channel_worker = @import("channel_worker.zig");
const util = @import("util.zig");
@@ -212,7 +213,9 @@ fn idleWakeCb(ctx: ?*anyopaque, buf: []u8) ?[]const u8 {
const c: *repl_glue.ReplCtx = @ptrCast(@alignCast(ctx orelse return null));
if (job_notify.takeIdleWake(c.io, buf)) |t| return t; // an idle stop waits for a real step boundary (#199)
if (schedule.takeWake(c.io, buf)) |t| return t;
- return channel_worker.takeWake(c.io, buf);
+ if (channel_worker.takeWake(c.io, buf)) |t| return t;
+ const root = c.root orelse return null;
+ return peer_idle.takeIdleWake(c.io, root.arena, buf);
}
fn versionCb(ctx: ?*anyopaque, gpa: Allocator) ?[]const u8 {