xonecas.io
79113
Ma

mini-coding-agent / commit 43445d9

feat: report status to herdr

xonecas2026-10-24 43445d9f44c49b56f937f7714aaeeba825737fae parent 3ac9136

3 files changed

src/config.zig+4−0
@@ -50,6 +50,7 @@ pub const Config = struct {
5050 thinking_effort: ?[]const u8,
5151 theme: []const u8,
5252 program_status: bool,
53+ herdr: bool,
5354 show_images: bool,
5455 custom_providers: []const CustomProvider,
5556 mcp_servers: std.json.ArrayHashMap(McpServer),
@@ -75,6 +76,7 @@ const File = struct {
7576 mcpServers: ?std.json.ArrayHashMap(McpServer) = null,
7677 theme: ?[]const u8 = null,
7778 programStatus: ?bool = null,
79+ herdr: ?bool = null,
7880 showImages: ?bool = null,
7981 snapshotIgnoreDirs: ?[]const []const u8 = null,
8082 snapshotUsesGitignore: ?bool = null,
@@ -155,6 +157,7 @@ fn readInto(a: std.mem.Allocator, path: []const u8, required: bool, cfg: *Config
155157 if (file.thinkingEffort) |v| cfg.thinking_effort = v;
156158 if (file.theme) |v| cfg.theme = v;
157159 if (file.programStatus) |v| cfg.program_status = v;
160+ if (file.herdr) |v| cfg.herdr = v;
158161 if (file.showImages) |v| cfg.show_images = v;
159162 if (file.snapshotIgnoreDirs) |v| cfg.snapshot_ignore_dirs = v;
160163 if (file.snapshotUsesGitignore) |v| cfg.snapshot_uses_gitignore = v;
@@ -208,6 +211,7 @@ fn defaults(a: std.mem.Allocator) !Config {
208211 .thinking_effort = null,
209212 .theme = "tokyonight",
210213 .program_status = false,
214+ .herdr = false,
211215 .show_images = false,
212216 .custom_providers = &.{},
213217 .mcp_servers = .{},
src/status.zig+155−0
@@ -1,5 +1,8 @@
11 const std = @import("std");
2+const builtin = @import("builtin");
23 const platform = @import("platform.zig");
4+const time = @import("time.zig");
5+const config = @import("config.zig");
36
47 /// OSC 7501, the Program Status Protocol. The program reports what it is doing
58 /// so any consumer of the pty can react; well-behaved terminals ignore the
@@ -7,7 +10,159 @@ const platform = @import("platform.zig");
710 const app = "mini-coding-agent";
811 const max_msg = 256;
912
13+/// Herdr's own integration prefix is reserved for Herdr; a source of our own
14+/// keeps its state and ours apart. See https://herdr.dev/docs/add-herdr-support/
15+const herdr_source = "mini";
16+const herdr_agent = "mini";
17+const herdr_report_timeout_ms = 2000;
18+const herdr_release_timeout_ms = 1000;
19+
20+var program_status = false;
21+var herdr_on = false;
22+var herdr_bin: []const u8 = "";
23+var herdr_pane: []const u8 = "";
24+
25+/// Reports leave through a single worker that always sends the newest state and
26+/// drops the ones it queued over, so a slow Herdr never slows the agent down.
27+var lock: std.Io.Mutex = .init;
28+var pending: ?Job = null;
29+var pending_msg: [max_msg]u8 = undefined;
30+var seq: u64 = 0;
31+
32+const Job = struct {
33+ state: []const u8,
34+ msg_len: usize,
35+ seq: u64,
36+};
37+
38+/// Enables the status consumers this run reports to. Herdr is only a consumer
39+/// inside a Herdr pane; anywhere else it stays off.
40+pub fn init(cfg: *const config.Config) void {
41+ program_status = cfg.program_status;
42+ if (!cfg.herdr) return;
43+ if (!std.mem.eql(u8, platform.getEnv("HERDR_ENV") orelse "", "1")) return;
44+ herdr_bin = platform.getEnv("HERDR_BIN_PATH") orelse return;
45+ herdr_pane = platform.getEnv("HERDR_PANE_ID") orelse return;
46+ if (herdr_bin.len == 0 or herdr_pane.len == 0) return;
47+
48+ // Herdr drops reports whose --seq is not higher than the last one it took,
49+ // even across our own restarts, so the sequence starts at the wall clock.
50+ seq = @intCast(@max(0, time.nowMs()));
51+ herdr_on = true;
52+
53+ const worker = std.Thread.spawn(.{}, loop, .{}) catch {
54+ herdr_on = false;
55+ return;
56+ };
57+ worker.detach();
58+}
59+
1060 pub fn report(state: []const u8, kind: ?[]const u8, msg: ?[]const u8) void {
61+ if (program_status) osc7501(state, kind, msg);
62+ if (!herdr_on) return;
63+
64+ const mapped = herdrState(state);
65+ const text: []const u8 = if (std.mem.eql(u8, mapped, "blocked") and msg != null)
66+ msg.?[0..@min(msg.?.len, max_msg)]
67+ else
68+ "";
69+
70+ lock.lockUncancelable(platform.io);
71+ defer lock.unlock(platform.io);
72+ seq += 1;
73+ if (text.len > 0) @memcpy(pending_msg[0..text.len], text);
74+ pending = .{ .state = mapped, .msg_len = text.len, .seq = seq };
75+}
76+
77+/// Clears our name, state, and resume command from the pane. Only for an exit,
78+/// and the report must land before the process goes away.
79+pub fn release() void {
80+ if (!herdr_on) return;
81+ herdr_on = false;
82+
83+ var seq_buf: [24]u8 = undefined;
84+ const n = std.fmt.bufPrint(&seq_buf, "{d}", .{nextSeq()}) catch return;
85+ runQuiet(&.{ herdr_bin, "pane", "release-agent", herdr_pane, "--source", herdr_source, "--agent", herdr_agent, "--seq", n }, herdr_release_timeout_ms);
86+}
87+
88+/// Herdr knows three states; everything else it sees is "ready for input".
89+fn herdrState(state: []const u8) []const u8 {
90+ if (std.mem.eql(u8, state, "working")) return "working";
91+ if (std.mem.eql(u8, state, "blocked")) return "blocked";
92+ return "idle";
93+}
94+
95+fn nextSeq() u64 {
96+ lock.lockUncancelable(platform.io);
97+ defer lock.unlock(platform.io);
98+ seq += 1;
99+ return seq;
100+}
101+
102+fn loop() void {
103+ while (true) {
104+ var msg: [max_msg]u8 = undefined;
105+ var job: ?Job = null;
106+ lock.lockUncancelable(platform.io);
107+ if (pending) |p| {
108+ @memcpy(msg[0..p.msg_len], pending_msg[0..p.msg_len]);
109+ job = p;
110+ pending = null;
111+ }
112+ lock.unlock(platform.io);
113+
114+ if (job) |j| send(j, msg[0..j.msg_len]);
115+ std.Io.sleep(platform.io, .{ .nanoseconds = 20 * std.time.ns_per_ms }, .boot) catch {};
116+ }
117+}
118+
119+fn send(job: Job, msg: []const u8) void {
120+ var seq_buf: [24]u8 = undefined;
121+ const n = std.fmt.bufPrint(&seq_buf, "{d}", .{job.seq}) catch return;
122+
123+ const base = [_][]const u8{
124+ herdr_bin, "pane", "report-agent", herdr_pane, "--source", herdr_source,
125+ "--agent", herdr_agent, "--state", job.state, "--seq", n,
126+ };
127+ var argv: [base.len + 2][]const u8 = undefined;
128+ @memcpy(argv[0..base.len], &base);
129+ var len = base.len;
130+ if (msg.len > 0) {
131+ argv[len] = "--message";
132+ argv[len + 1] = msg;
133+ len += 2;
134+ }
135+ runQuiet(argv[0..len], herdr_report_timeout_ms);
136+}
137+
138+/// Fire and forget: our stdio stays ours, failures are invisible, and a Herdr
139+/// that wedges is killed instead of holding the worker forever.
140+fn runQuiet(argv: []const []const u8, timeout_ms: i64) void {
141+ var child = std.process.spawn(platform.io, .{
142+ .argv = argv,
143+ .stdin = .ignore,
144+ .stdout = .ignore,
145+ .stderr = .ignore,
146+ }) catch return;
147+
148+ const pid = child.id orelse return;
149+ const deadline = time.nowMs() + timeout_ms;
150+ while (!reaped(pid)) {
151+ if (time.nowMs() >= deadline) {
152+ child.kill(platform.io);
153+ return;
154+ }
155+ std.Io.sleep(platform.io, .{ .nanoseconds = 5 * std.time.ns_per_ms }, .boot) catch {};
156+ }
157+}
158+
159+fn reaped(pid: std.posix.pid_t) bool {
160+ var status: if (builtin.link_libc) c_int else u32 = undefined;
161+ const rc = std.posix.system.waitpid(pid, &status, std.posix.W.NOHANG);
162+ return std.posix.errno(rc) == .SUCCESS and rc != 0;
163+}
164+
165+fn osc7501(state: []const u8, kind: ?[]const u8, msg: ?[]const u8) void {
11166 var buf: [1024]u8 = undefined;
12167 const head = "\x1b]7501;";
13168 @memcpy(buf[0..head.len], head);
src/tui/tui.zig+9−12
@@ -400,7 +400,7 @@ const Tui = struct {
400400 self.text_bytes = 0;
401401 self.reasoning_bytes = 0;
402402 }
403- self.reportStatus(switch (p.phase) {
403+ status.report(switch (p.phase) {
404404 .preparing, .waiting_model, .streaming, .snapshotting, .running_tool => "working",
405405 .pausing => "blocked",
406406 .idle => "idle",
@@ -440,31 +440,26 @@ const Tui = struct {
440440 .message => |am| self.commitMessage(am),
441441 .tool_result => |tr| self.commitToolResult(tr.name, tr.text, tr.is_error, tr.diffs, tr.body, tr.images),
442442 .err => |m| {
443- self.reportStatus("error", null, m);
443+ status.report("error", null, m);
444444 self.retry = true;
445445 self.endTurn(styles.err(self.s, std.fmt.allocPrint(self.s, "! {s}", .{m}) catch "! error"));
446446 },
447447 .no_model => {
448- self.reportStatus("error", null, "no model configured");
448+ status.report("error", null, "no model configured");
449449 self.endTurn(styles.err(self.s, "! no model configured"));
450450 },
451451 .cancelled => {
452- self.reportStatus("idle", null, null);
452+ status.report("idle", null, null);
453453 self.endTurn(styles.err(self.s, "! cancelled"));
454454 },
455455 .complete => {
456- self.reportStatus("done", null, null);
456+ status.report("done", null, null);
457457 self.endTurn(styles.comment(self.s, std.fmt.allocPrint(self.s, "[complete · {d}s]", .{@max(0, @divFloor(time.nowMs() - self.turn_start, 1000))}) catch "[complete]"));
458458 },
459459 }
460460 self.dirty = true;
461461 }
462462
463- fn reportStatus(self: *Tui, state: []const u8, kind: ?[]const u8, msg: ?[]const u8) void {
464- if (!self.cfg.program_status) return;
465- status.report(state, kind, msg);
466- }
467-
468463 fn endTurn(self: *Tui, line: []const u8) void {
469464 self.phase = .idle;
470465 self.keepName(&self.detail, null);
@@ -669,7 +664,7 @@ const Tui = struct {
669664 self.steering_text = "";
670665 if (!self.aborting) {
671666 self.aborting = true;
672- self.reportStatus("idle", null, null);
667+ status.report("idle", null, null);
673668 self.endTurn(styles.err(self.s, "! cancelled"));
674669 }
675670 self.dirty = true;
@@ -1017,6 +1012,7 @@ const Tui = struct {
10171012 self.term.write(self.scrollback.buf.items);
10181013 self.term.stop();
10191014 self.opts.session.close();
1015+ status.release();
10201016 }
10211017 };
10221018
@@ -1129,6 +1125,7 @@ pub fn run(opts: agent.Options, cfg: *const config.Config, tool_names: []const c
11291125 self.* = Tui.init(gpa, opts_ptr, cfg, tool_names);
11301126 self.bindAllocators();
11311127 self.history = history.Store.init(gpa, config.historyPath(gpa), cfg.history_size);
1128+ status.init(cfg);
11321129
11331130 self.term.start();
11341131 defer self.term.stop();
@@ -1148,7 +1145,7 @@ pub fn run(opts: agent.Options, cfg: *const config.Config, tool_names: []const c
11481145 std.posix.sigaction(std.posix.SIG.HUP, &term_act, null);
11491146 std.posix.sigaction(std.posix.SIG.QUIT, &term_act, null);
11501147
1151- self.reportStatus("idle", null, null);
1148+ status.report("idle", null, null);
11521149 self.pushBanner();
11531150 self.draw();