@@ -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);