Skip to content

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

Python

feat(peer): keep Accord as a standing duplex per peer

justrachbd02ce41ac24
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 14580c1e..2d8a79ea 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,7 +12,7 @@ current is part of cutting a release.
## Unreleased
-- Accord Unix 0600 live path is on by default (`GRAFF_ACCORD=0` opts out); JSONL stays the durable room (ADR 0134).
+- Accord Unix 0600 is a standing duplex per live session (`GRAFF_ACCORD=0` opts out); JSONL stays the durable room (ADR 0134).
- `#1001`: idle TUI and line REPL start a turn when the durable peer room has new unread mail (one auto-turn per batch; inbox consume resets).
- `#1007`: idle `graff acp` sessions start that same turn; `peer_message` no longer claims delivery waits for a step boundary.
- `#1006`: rendered views sit in the transcript without product chrome; desktop appearance tokens ride the turn (ADR 0135).
diff --git a/docs/adr/0134-accord-live-jsonl-durable.md b/docs/adr/0134-accord-live-jsonl-durable.md
index 3e17cf7e..44398bd6 100644
--- a/docs/adr/0134-accord-live-jsonl-durable.md
+++ b/docs/adr/0134-accord-live-jsonl-durable.md
@@ -11,10 +11,11 @@ Mailbox is a shape demo and cannot be the room.
## Decision
JSONL stays the durable log. `postMessage` / `readNewMessages` remain the seam.
-On Unix, the same JSONL line is also sent live over Accord 0600 sockets in the
-presence dir (`{pid}-{start}.accord.sock`). `GRAFF_ACCORD=0` opts out. Windows
-stays JSONL-only. A live miss never fails the durable write.
-Do not replace the room with Mailbox.
+On Unix, each announced session keeps a standing Accord duplex on
+`{pid}-{start}.accord.sock` (0600). `postMessage` still appends JSONL, then
+sends the same line as `msg` on that link (progress/stop reuse the session).
+`GRAFF_ACCORD=0` opts out. Windows stays JSONL-only. A live miss never fails
+the durable write. Do not replace the room with Mailbox.
## Consequences
diff --git a/src/presence_accord.zig b/src/presence_accord.zig
index 482c21aa..7fcbf73e 100644
--- a/src/presence_accord.zig
+++ b/src/presence_accord.zig
@@ -31,6 +31,15 @@ var g_dir_path: ?[]u8 = null;
var g_own_name: ?[]u8 = null;
var g_listener: ?net.Server = null;
var g_future: ?Io.Future(void) = null;
+var g_mu: Io.Mutex = .init;
+var g_outs: std.ArrayList(OutLink) = .empty;
+var g_ins: std.ArrayList(*accord.Session) = .empty;
+var g_connects: std.atomic.Value(u32) = .init(0);
+
+const OutLink = struct {
+ path: []u8,
+ sess: *accord.Session,
+};
pub fn takePing() bool {
return g_ping.swap(false, .acq_rel);
@@ -113,6 +122,22 @@ pub fn stop(io: Io) void {
l.deinit(io);
g_listener = null;
}
+ if (g_gpa) |gpa| {
+ g_mu.lockUncancelable(io);
+ for (g_outs.items) |link| {
+ link.sess.shutdown();
+ gpa.destroy(link.sess);
+ gpa.free(link.path);
+ }
+ g_outs.clearRetainingCapacity();
+ for (g_ins.items) |sess| sess.shutdown();
+ g_ins.clearRetainingCapacity();
+ g_mu.unlock(io);
+ g_outs.deinit(gpa);
+ g_ins.deinit(gpa);
+ g_outs = .empty;
+ g_ins = .empty;
+ }
if (g_dir_path) |dir| if (g_own_name) |name| {
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
if (std.fmt.bufPrint(&path_buf, "{s}/{s}", .{ dir, name })) |path| {
@@ -126,6 +151,7 @@ pub fn stop(io: Io) void {
g_gpa = null;
g_io = null;
g_ping.store(false, .release);
+ g_connects.store(0, .release);
}
fn acceptLoop() void {
@@ -134,37 +160,127 @@ fn acceptLoop() void {
const listener = &(g_listener orelse return);
while (true) {
const stream = listener.accept(io) catch break;
- serveOne(io, gpa, stream);
+ const sess = gpa.create(accord.Session) catch {
+ stream.close(io);
+ continue;
+ };
+ sess.* = .{ .io = io, .gpa = gpa, .role = .server, .stream = stream };
+ _ = io.concurrent(holdInbound, .{sess}) catch {
+ sess.shutdown();
+ gpa.destroy(sess);
+ continue;
+ };
}
}
-fn serveOne(io: Io, gpa: std.mem.Allocator, stream: net.Stream) void {
- const sess = gpa.create(accord.Session) catch {
- stream.close(io);
+fn holdInbound(sess: *accord.Session) void {
+ const io = sess.io;
+ const gpa = sess.gpa;
+ sess.start() catch {
+ sess.shutdown();
+ gpa.destroy(sess);
return;
};
- sess.* = .{ .io = io, .gpa = gpa, .role = .server, .stream = stream };
- defer {
+ g_mu.lockUncancelable(io);
+ g_ins.append(gpa, sess) catch {
+ g_mu.unlock(io);
sess.shutdown();
gpa.destroy(sess);
+ return;
+ };
+ g_mu.unlock(io);
+ while (true) {
+ const got = sess.recv(1) catch break;
+ defer got.deinit(gpa);
+ switch (got.kind) {
+ .msg, .progress, .stop => g_ping.store(true, .release),
+ else => {},
+ }
}
- sess.start() catch return;
- const got = sess.recv(1) catch return;
- defer got.deinit(gpa);
- g_ping.store(true, .release);
+ dropInbound(sess);
+}
+
+fn dropInbound(sess: *accord.Session) void {
+ const io = sess.io;
+ const gpa = sess.gpa;
+ g_mu.lockUncancelable(io);
+ for (g_ins.items, 0..) |item, i| {
+ if (item == sess) {
+ _ = g_ins.orderedRemove(i);
+ break;
+ }
+ }
+ g_mu.unlock(io);
+ sess.shutdown();
+ gpa.destroy(sess);
}
fn sendLine(io: Io, gpa: std.mem.Allocator, path: []const u8, json_line: []const u8) !void {
- const addr = try net.UnixAddress.init(path);
- const stream = addr.connect(io) catch return;
- const sess = try gpa.create(accord.Session);
+ const sess = ensureOut(io, gpa, path) orelse return;
+ sess.send(1, .msg, .none, json_line) catch {
+ dropOut(io, gpa, path);
+ const retry = ensureOut(io, gpa, path) orelse return;
+ retry.send(1, .msg, .none, json_line) catch {};
+ };
+}
+
+fn ensureOut(io: Io, gpa: std.mem.Allocator, path: []const u8) ?*accord.Session {
+ g_mu.lockUncancelable(io);
+ for (g_outs.items) |link| {
+ if (std.mem.eql(u8, link.path, path)) {
+ g_mu.unlock(io);
+ return link.sess;
+ }
+ }
+ g_mu.unlock(io);
+ const addr = net.UnixAddress.init(path) catch return null;
+ const stream = addr.connect(io) catch return null;
+ const sess = gpa.create(accord.Session) catch {
+ stream.close(io);
+ return null;
+ };
sess.* = .{ .io = io, .gpa = gpa, .role = .client, .stream = stream };
- defer {
+ sess.start() catch {
+ sess.shutdown();
+ gpa.destroy(sess);
+ return null;
+ };
+ const owned = gpa.dupe(u8, path) catch {
sess.shutdown();
gpa.destroy(sess);
+ return null;
+ };
+ g_mu.lockUncancelable(io);
+ defer g_mu.unlock(io);
+ for (g_outs.items) |link| {
+ if (std.mem.eql(u8, link.path, path)) {
+ sess.shutdown();
+ gpa.destroy(sess);
+ gpa.free(owned);
+ return link.sess;
+ }
+ }
+ g_outs.append(gpa, .{ .path = owned, .sess = sess }) catch {
+ gpa.free(owned);
+ sess.shutdown();
+ gpa.destroy(sess);
+ return null;
+ };
+ _ = g_connects.fetchAdd(1, .monotonic);
+ return sess;
+}
+
+fn dropOut(io: Io, gpa: std.mem.Allocator, path: []const u8) void {
+ g_mu.lockUncancelable(io);
+ defer g_mu.unlock(io);
+ for (g_outs.items, 0..) |link, i| {
+ if (!std.mem.eql(u8, link.path, path)) continue;
+ link.sess.shutdown();
+ gpa.destroy(link.sess);
+ gpa.free(link.path);
+ _ = g_outs.orderedRemove(i);
+ return;
}
- try sess.start();
- try sess.send(1, .msg, .none, json_line);
}
fn connectPath(io: Io, path: []const u8) !net.Stream {
@@ -238,11 +354,8 @@ test "listen binds a 0600 sock and stop unlinks it" {
test_enabled = false;
}
const dir = "graff-accord-listen";
- Io.Dir.cwd().makeDir(io, dir) catch |err| switch (err) {
- error.PathAlreadyExists => {},
- else => return err,
- };
- defer Io.Dir.cwd().deleteDir(io, dir) catch {};
+ try Io.Dir.cwd().createDirPath(io, dir);
+ defer Io.Dir.cwd().deleteTree(io, dir) catch {};
listen(io, gpa, dir);
const name = g_own_name orelse return error.NoSock;
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
@@ -290,3 +403,29 @@ test "Unix 0600 round-trips the JSONL line" {
defer got.deinit(gpa);
try std.testing.expectEqualStrings(line, got.payload);
}
+
+test "standing link carries msg, progress, and a reply on one session" {
+ if (builtin.os.tag == .windows) return;
+ const io = std.testing.io;
+ const gpa = std.testing.allocator;
+ const path = "graff-accord-standing.sock";
+ var pair: Pair = undefined;
+ try pair.init(io, gpa, path);
+ defer pair.deinit();
+ try pair.client.send(1, .msg, .none, "hold gui/src");
+ const first = try pair.server.recv(1);
+ defer first.deinit(gpa);
+ try std.testing.expectEqual(accord.Kind.msg, first.kind);
+ try pair.client.send(1, .progress, .none, "working");
+ const mid = try pair.server.recv(1);
+ defer mid.deinit(gpa);
+ try std.testing.expectEqual(accord.Kind.progress, mid.kind);
+ try pair.server.send(1, .msg, .none, "acked");
+ const back = try pair.client.recv(1);
+ defer back.deinit(gpa);
+ try std.testing.expectEqualStrings("acked", back.payload);
+ try pair.client.send(1, .stop, .none, &.{});
+ const halt = try pair.server.recv(1);
+ defer halt.deinit(gpa);
+ try std.testing.expectEqual(accord.Kind.stop, halt.kind);
+}