Skip to content

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

Python

channel: tail-cap the #469 backlog a late joiner hears

Rach Pradhan0581c48b03ba
diff --git a/src/peer_channel.zig b/src/peer_channel.zig
index f289c52c..be49af4a 100644
--- a/src/peer_channel.zig
+++ b/src/peer_channel.zig
@@ -22,6 +22,7 @@ const agent_mod = @import("agent.zig");
const tools_mod = @import("tools.zig");
const engine_sink = @import("engine_sink.zig");
const presence = @import("presence.zig");
+const presence_chan = @import("presence_chan.zig");
const worktree_lease = @import("worktree_lease.zig");
const Agent = agent_mod.Agent;
@@ -165,12 +166,28 @@ fn peerNoteIfChanged(root: *Agent) ?[]const u8 {
/// user-role note in history plus user-visible events. Never fails a turn —
/// delivery trouble is silence, not an error, because a peer must never break
/// our session.
+/// The room tail a late joiner hears in full; anything older collapses into
+/// one "N omitted" marker instead of flooding the first history note.
+pub const backlog_tail_max = 10;
+var g_backlog_tail_cut = false;
+
pub fn deliverInbound(root: *Agent) void {
if (root.sub) return;
const note = peerNoteIfChanged(root);
- const local_msgs = presence.drainChannel(root.io, root.arena);
- const device_msgs = presence.drainDevice(root.io, root.arena);
- if (note == null and local_msgs.len == 0 and device_msgs.len == 0) return;
+ var local_msgs = presence.drainChannel(root.io, root.arena);
+ var device_msgs = presence.drainDevice(root.io, root.arena);
+ // Only the process's FIRST drain carries the rooms' whole backlog; later
+ // drains are incremental and must never be capped — those are live.
+ var omitted: usize = 0;
+ if (!g_backlog_tail_cut) {
+ g_backlog_tail_cut = true;
+ const dl = presence_chan.backlogDrop(local_msgs.len, backlog_tail_max);
+ const dd = presence_chan.backlogDrop(device_msgs.len, backlog_tail_max);
+ omitted = dl + dd;
+ local_msgs = local_msgs[dl..];
+ device_msgs = device_msgs[dd..];
+ }
+ if (note == null and local_msgs.len == 0 and device_msgs.len == 0 and omitted == 0) return;
const sink = engine_sink.forAgent(root);
const own = presence.ownSession();
var buf: std.ArrayList(u8) = .empty;
@@ -179,6 +196,14 @@ pub fn deliverInbound(root: *Agent) void {
buf.append(root.arena, '\n') catch {};
sink.emit(root.io, .{ .session_notice = .{ .text = n, .tone = .plain } });
}
+ if (omitted > 0) {
+ const marker = std.fmt.allocPrint(root.arena, "[#469 channel: {d} older message(s) predating this session omitted]", .{omitted}) catch "";
+ if (marker.len > 0) {
+ buf.appendSlice(root.arena, marker) catch {};
+ buf.append(root.arena, '\n') catch {};
+ sink.emit(root.io, .{ .session_notice = .{ .text = marker, .tone = .plain } });
+ }
+ }
for ([2][]const presence.Message{ local_msgs, device_msgs }, [2][]const u8{ "", " · device" }) |msgs, scope| {
for (msgs) |m| {
const goal = if (m.from_goal.len > 0) std.fmt.allocPrint(root.arena, " (goal: {s})", .{m.from_goal}) catch "" else "";
diff --git a/src/presence.zig b/src/presence.zig
index 698726b8..0839aad8 100644
--- a/src/presence.zig
+++ b/src/presence.zig
@@ -398,8 +398,9 @@ pub fn postTo(io: Io, arena: Allocator, text: []const u8, to: []const u8) bool {
/// Drain the shared channel: every complete message since our last drain,
// minus our own echo. Empty when unannounced or caught up. A session that
-/// joins late hears the channel's whole backlog once — context, not spam:
-/// the room predates it. EXCEPTION: a -p one-shot joins, works, and exits
+/// joins late hears the whole backlog once — but peer_channel.deliverInbound
+/// tail-caps that first drain (backlog_tail_max) with an omitted-count marker.
+/// EXCEPTION: a -p one-shot joins, works, and exits
/// inside a minute — the backlog is context it cannot use (measured ~4k
/// tokens of stale chatter injected on the first step of every benchmark
/// one-shot — and it made ephemeral workers ANSWER old messages, burning
diff --git a/src/presence_chan.zig b/src/presence_chan.zig
index 693b2ecb..5c0d5915 100644
--- a/src/presence_chan.zig
+++ b/src/presence_chan.zig
@@ -84,6 +84,20 @@ pub fn readNewMessages(io: Io, arena: Allocator, dir: Io.Dir, name: []const u8,
return msgs.items;
}
+/// A late joiner hears only the room's recent tail: more than `max` backlogged
+/// messages would bury live coordination under hours of roll-call from dead
+/// sessions (and the dump rides every later request as durable history).
+/// Returns how many leading messages to drop; the caller renders the marker.
+pub fn backlogDrop(count: usize, max: usize) usize {
+ return if (count > max) count - max else 0;
+}
+
+test "backlogDrop: only the overflow beyond the tail cap drops" {
+ try std.testing.expectEqual(@as(usize, 0), backlogDrop(0, 10));
+ try std.testing.expectEqual(@as(usize, 0), backlogDrop(10, 10));
+ try std.testing.expectEqual(@as(usize, 21), backlogDrop(31, 10));
+}
+
test "channel: every reader hears every message once; only the sender's own echo drops" {
const io = std.testing.io;
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);