Skip to content

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

Python

refactor: extract lifecycle hooks into hooks.zig (#123)

justrach7fda7e2e56a6
diff --git a/src/hooks.zig b/src/hooks.zig
new file mode 100644
index 00000000..04268ec8
--- /dev/null
+++ b/src/hooks.zig
@@ -0,0 +1,182 @@
+//! Lifecycle hooks (codex/Claude-style): the Hook/Hooks config types, the
+//! settings.json "hooks" loader, and the per-hook subprocess runner. Split out
+//! of main.zig (#123). Back-imports main only for Approvals (the settings-file
+//! path) and the win shim (Windows stderr-pipe peek). The pre/post/turn-end
+//! dispatch and the codedb-guard file-index cache stay in main.
+
+const std = @import("std");
+const builtin = @import("builtin");
+const Io = std.Io;
+const Value = std.json.Value;
+const Allocator = std.mem.Allocator;
+
+const root = @import("main.zig");
+const Approvals = root.Approvals;
+const win = root.win;
+
+/// Lifecycle hooks (codex/Claude-style), loaded once at startup from
+/// .harness/settings.json's "hooks" object. Three events:
+///
+/// pre_tool — runs BEFORE a tool executes (root and subagents, MCP
+/// included). Exit 2 blocks the call: the hook's stderr is
+/// returned to the model as the tool result. Any other exit
+/// allows. The event JSON arrives on the hook's stdin.
+/// post_tool — runs after a tool executed (formatters, notifications);
+/// best-effort, exit code ignored.
+/// turn_end — runs after each root turn completes; best-effort.
+///
+/// Shape: {"hooks": {"pre_tool": [{"match": "bash|edit_file",
+/// "command": "./guard.sh", "timeout_ms": 10000}], ...}}. `match` is "*"
+/// (default) or pipe-separated tool names. A hung hook is killed at its
+/// timeout and treated as allow — hooks must never brick the loop.
+pub const Hook = struct {
+ match: []const u8,
+ command: []const u8,
+ timeout_ms: u64,
+
+ pub fn matches(self: Hook, tool: []const u8) bool {
+ if (std.mem.eql(u8, self.match, "*")) return true;
+ var it = std.mem.splitScalar(u8, self.match, '|');
+ while (it.next()) |m| {
+ if (std.mem.eql(u8, std.mem.trim(u8, m, " "), tool)) return true;
+ }
+ return false;
+ }
+};
+
+pub const Hooks = struct {
+ pre_tool: []const Hook = &.{},
+ post_tool: []const Hook = &.{},
+ turn_end: []const Hook = &.{},
+
+ pub fn total(self: Hooks) usize {
+ return self.pre_tool.len + self.post_tool.len + self.turn_end.len;
+ }
+};
+fn parseHookList(arena: Allocator, v: ?Value) []const Hook {
+ const arr = v orelse return &.{};
+ if (arr != .array) return &.{};
+ var list: std.ArrayList(Hook) = .empty;
+ for (arr.array.items) |item| {
+ if (item != .object) continue;
+ const cmd = item.object.get("command") orelse continue;
+ if (cmd != .string or cmd.string.len == 0) continue;
+ const match: []const u8 = if (item.object.get("match")) |m|
+ (if (m == .string and m.string.len > 0) m.string else "*")
+ else
+ "*";
+ const timeout: u64 = if (item.object.get("timeout_ms")) |t|
+ (if (t == .integer and t.integer > 0) @intCast(t.integer) else 10_000)
+ else
+ 10_000;
+ list.append(arena, .{ .match = match, .command = cmd.string, .timeout_ms = timeout }) catch continue;
+ }
+ return list.items;
+}
+
+/// Parse the "hooks" section of .harness/settings.json (arena-owned slices;
+/// call once at startup with the session arena).
+pub fn loadHooks(io: Io, arena: Allocator) Hooks {
+ const data = Io.Dir.cwd().readFileAlloc(io, Approvals.settings_path, arena, .limited(1 << 20)) catch return .{};
+ const v = std.json.parseFromSliceLeaky(Value, arena, data, .{ .allocate = .alloc_always }) catch return .{};
+ if (v != .object) return .{};
+ const hooks = v.object.get("hooks") orelse return .{};
+ if (hooks != .object) return .{};
+ return .{
+ .pre_tool = parseHookList(arena, hooks.object.get("pre_tool")),
+ .post_tool = parseHookList(arena, hooks.object.get("post_tool")),
+ .turn_end = parseHookList(arena, hooks.object.get("turn_end")),
+ };
+}
+
+const HookRun = struct { code: ?u8, stderr: []u8 };
+
+/// Run one hook command: /bin/sh -c, event JSON on stdin, stderr captured
+/// (capped), killed at its timeout. Returns the exit code (null on timeout
+/// or spawn failure) and the trimmed stderr (gpa-owned).
+pub fn runHookCmd(gpa: Allocator, io: Io, command: []const u8, payload: []const u8, timeout_ms: u64) HookRun {
+ const none: HookRun = .{ .code = null, .stderr = &.{} };
+ var child = std.process.spawn(io, .{
+ .argv = &.{ "/bin/sh", "-c", command },
+ .stdin = .pipe,
+ .stdout = .ignore,
+ .stderr = .pipe,
+ }) catch return none;
+ {
+ var wbuf: [4096]u8 = undefined;
+ var w = child.stdin.?.writerStreaming(io, &wbuf);
+ w.interface.writeAll(payload) catch {};
+ w.interface.flush() catch {};
+ child.stdin.?.close(io);
+ child.stdin = null;
+ }
+ var rbuf: [16 * 1024]u8 = undefined;
+ var r = child.stderr.?.readerStreaming(io, &rbuf);
+ var err_text: std.ArrayList(u8) = .empty;
+ defer err_text.deinit(gpa);
+ const t0: Io.Timestamp = .now(io, .awake);
+ var timed_out = false;
+ while (true) {
+ // Bounded read pump: stderr is tiny in practice; the deadline is the
+ // real guard. takeDelimiter would block, so poll the fd instead.
+ if (t0.untilNow(io, .awake).toMilliseconds() >= timeout_ms) {
+ timed_out = true;
+ child.kill(io);
+ break;
+ }
+ if (r.interface.buffered().len == 0) {
+ if (builtin.os.tag == .windows) {
+ // No pollfd on Windows; peek the pipe for available bytes.
+ var avail: u32 = 0;
+ if (win.PeekNamedPipe(child.stderr.?.handle, null, 0, null, &avail, null) == 0) break; // broken pipe = EOF
+ if (avail == 0) {
+ io.sleep(.fromMilliseconds(50), .awake) catch {};
+ continue;
+ }
+ } else {
+ var fds = [_]std.posix.pollfd{.{ .fd = child.stderr.?.handle, .events = std.posix.POLL.IN, .revents = 0 }};
+ const ready = std.posix.poll(&fds, 100) catch break;
+ if (ready == 0) continue;
+ if (fds[0].revents & (std.posix.POLL.HUP | std.posix.POLL.ERR) != 0 and fds[0].revents & std.posix.POLL.IN == 0) break;
+ }
+ }
+ const b = r.interface.takeByte() catch break; // EOF: hook exited
+ if (err_text.items.len < 4096) err_text.append(gpa, b) catch break;
+ }
+ const code: ?u8 = if (timed_out) null else switch (child.wait(io) catch return none) {
+ .exited => |c| c,
+ else => null,
+ };
+ const trimmed = std.mem.trim(u8, err_text.items, " \t\r\n");
+ return .{ .code = code, .stderr = gpa.dupe(u8, trimmed) catch &.{} };
+}
+test "Hook.matches: wildcard, pipe lists, exact" {
+ const star = Hook{ .match = "*", .command = "", .timeout_ms = 0 };
+ try std.testing.expect(star.matches("bash"));
+ try std.testing.expect(star.matches("anything"));
+ const multi = Hook{ .match = "bash|edit_file | write_file", .command = "", .timeout_ms = 0 };
+ try std.testing.expect(multi.matches("bash"));
+ try std.testing.expect(multi.matches("edit_file"));
+ try std.testing.expect(multi.matches("write_file")); // spaces trimmed
+ try std.testing.expect(!multi.matches("read_file"));
+ try std.testing.expect(!multi.matches("bas")); // no prefix matching
+}
+
+test "parseHookList: defaults, malformed entries skipped" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+ const a = arena_state.allocator();
+ const v = try std.json.parseFromSliceLeaky(Value, a,
+ \\[{"command": "./guard.sh"},
+ \\ {"match": "bash", "command": "lint", "timeout_ms": 500},
+ \\ {"match": "no-command-key"},
+ \\ "not-an-object",
+ \\ {"command": ""}]
+ , .{});
+ const hooks = parseHookList(a, v);
+ try std.testing.expectEqual(@as(usize, 2), hooks.len);
+ try std.testing.expectEqualStrings("*", hooks[0].match); // default match
+ try std.testing.expectEqual(@as(u64, 10_000), hooks[0].timeout_ms); // default timeout
+ try std.testing.expectEqualStrings("bash", hooks[1].match);
+ try std.testing.expectEqual(@as(u64, 500), hooks[1].timeout_ms);
+}
diff --git a/src/main.zig b/src/main.zig
index 83b54338..d7cb6c1e 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -167,6 +167,7 @@ test {
_ = oauth;
_ = anim;
_ = approvals_mod;
+ _ = hooks;
}
/// Wire format + auth style + endpoint per provider. Base URLs and env-var
@@ -2202,47 +2203,12 @@ fn isSlashCommandLine(line: []const u8) bool {
return true;
}
-/// Lifecycle hooks (codex/Claude-style), loaded once at startup from
-/// .harness/settings.json's "hooks" object. Three events:
-///
-/// pre_tool — runs BEFORE a tool executes (root and subagents, MCP
-/// included). Exit 2 blocks the call: the hook's stderr is
-/// returned to the model as the tool result. Any other exit
-/// allows. The event JSON arrives on the hook's stdin.
-/// post_tool — runs after a tool executed (formatters, notifications);
-/// best-effort, exit code ignored.
-/// turn_end — runs after each root turn completes; best-effort.
-///
-/// Shape: {"hooks": {"pre_tool": [{"match": "bash|edit_file",
-/// "command": "./guard.sh", "timeout_ms": 10000}], ...}}. `match` is "*"
-/// (default) or pipe-separated tool names. A hung hook is killed at its
-/// timeout and treated as allow — hooks must never brick the loop.
-const Hook = struct {
- match: []const u8,
- command: []const u8,
- timeout_ms: u64,
-
- fn matches(self: Hook, tool: []const u8) bool {
- if (std.mem.eql(u8, self.match, "*")) return true;
- var it = std.mem.splitScalar(u8, self.match, '|');
- while (it.next()) |m| {
- if (std.mem.eql(u8, std.mem.trim(u8, m, " "), tool)) return true;
- }
- return false;
- }
-};
+// Lifecycle hooks (Hook/Hooks config types + settings loader + per-hook
+// subprocess runner) live in hooks.zig (#123). g_hooks below, the
+// pre/post/turn-end dispatch, and the codedb-guard file-index cache stay here.
+const hooks = @import("hooks.zig");
-const Hooks = struct {
- pre_tool: []const Hook = &.{},
- post_tool: []const Hook = &.{},
- turn_end: []const Hook = &.{},
-
- fn total(self: Hooks) usize {
- return self.pre_tool.len + self.post_tool.len + self.turn_end.len;
- }
-};
-
-var g_hooks: Hooks = .{};
+var g_hooks: hooks.Hooks = .{};
/// Built-in codedb guard (issue #626): when a repo is codedb-indexed, agents
/// reflexively grep/sed/cat source files and never touch the structural tools,
@@ -2290,104 +2256,6 @@ fn codedbFileIndexed(io: Io, gpa: Allocator, path: []const u8) bool {
return result;
}
-fn parseHookList(arena: Allocator, v: ?Value) []const Hook {
- const arr = v orelse return &.{};
- if (arr != .array) return &.{};
- var list: std.ArrayList(Hook) = .empty;
- for (arr.array.items) |item| {
- if (item != .object) continue;
- const cmd = item.object.get("command") orelse continue;
- if (cmd != .string or cmd.string.len == 0) continue;
- const match: []const u8 = if (item.object.get("match")) |m|
- (if (m == .string and m.string.len > 0) m.string else "*")
- else
- "*";
- const timeout: u64 = if (item.object.get("timeout_ms")) |t|
- (if (t == .integer and t.integer > 0) @intCast(t.integer) else 10_000)
- else
- 10_000;
- list.append(arena, .{ .match = match, .command = cmd.string, .timeout_ms = timeout }) catch continue;
- }
- return list.items;
-}
-
-/// Parse the "hooks" section of .harness/settings.json (arena-owned slices;
-/// call once at startup with the session arena).
-fn loadHooks(io: Io, arena: Allocator) Hooks {
- const data = Io.Dir.cwd().readFileAlloc(io, Approvals.settings_path, arena, .limited(1 << 20)) catch return .{};
- const v = std.json.parseFromSliceLeaky(Value, arena, data, .{ .allocate = .alloc_always }) catch return .{};
- if (v != .object) return .{};
- const hooks = v.object.get("hooks") orelse return .{};
- if (hooks != .object) return .{};
- return .{
- .pre_tool = parseHookList(arena, hooks.object.get("pre_tool")),
- .post_tool = parseHookList(arena, hooks.object.get("post_tool")),
- .turn_end = parseHookList(arena, hooks.object.get("turn_end")),
- };
-}
-
-const HookRun = struct { code: ?u8, stderr: []u8 };
-
-/// Run one hook command: /bin/sh -c, event JSON on stdin, stderr captured
-/// (capped), killed at its timeout. Returns the exit code (null on timeout
-/// or spawn failure) and the trimmed stderr (gpa-owned).
-fn runHookCmd(gpa: Allocator, io: Io, command: []const u8, payload: []const u8, timeout_ms: u64) HookRun {
- const none: HookRun = .{ .code = null, .stderr = &.{} };
- var child = std.process.spawn(io, .{
- .argv = &.{ "/bin/sh", "-c", command },
- .stdin = .pipe,
- .stdout = .ignore,
- .stderr = .pipe,
- }) catch return none;
- {
- var wbuf: [4096]u8 = undefined;
- var w = child.stdin.?.writerStreaming(io, &wbuf);
- w.interface.writeAll(payload) catch {};
- w.interface.flush() catch {};
- child.stdin.?.close(io);
- child.stdin = null;
- }
- var rbuf: [16 * 1024]u8 = undefined;
- var r = child.stderr.?.readerStreaming(io, &rbuf);
- var err_text: std.ArrayList(u8) = .empty;
- defer err_text.deinit(gpa);
- const t0: Io.Timestamp = .now(io, .awake);
- var timed_out = false;
- while (true) {
- // Bounded read pump: stderr is tiny in practice; the deadline is the
- // real guard. takeDelimiter would block, so poll the fd instead.
- if (t0.untilNow(io, .awake).toMilliseconds() >= timeout_ms) {
- timed_out = true;
- child.kill(io);
- break;
- }
- if (r.interface.buffered().len == 0) {
- if (builtin.os.tag == .windows) {
- // No pollfd on Windows; peek the pipe for available bytes.
- var avail: u32 = 0;
- if (win.PeekNamedPipe(child.stderr.?.handle, null, 0, null, &avail, null) == 0) break; // broken pipe = EOF
- if (avail == 0) {
- io.sleep(.fromMilliseconds(50), .awake) catch {};
- continue;
- }
- } else {
- var fds = [_]std.posix.pollfd{.{ .fd = child.stderr.?.handle, .events = std.posix.POLL.IN, .revents = 0 }};
- const ready = std.posix.poll(&fds, 100) catch break;
- if (ready == 0) continue;
- if (fds[0].revents & (std.posix.POLL.HUP | std.posix.POLL.ERR) != 0 and fds[0].revents & std.posix.POLL.IN == 0) break;
- }
- }
- const b = r.interface.takeByte() catch break; // EOF: hook exited
- if (err_text.items.len < 4096) err_text.append(gpa, b) catch break;
- }
- const code: ?u8 = if (timed_out) null else switch (child.wait(io) catch return none) {
- .exited => |c| c,
- else => null,
- };
- const trimmed = std.mem.trim(u8, err_text.items, " \t\r\n");
- return .{ .code = code, .stderr = gpa.dupe(u8, trimmed) catch &.{} };
-}
-
/// Codex-style optional skills: known companion tools the harness quietly
/// upgrades itself with when they're installed. Progressive disclosure, same
/// shape as codex SKILL.md metadata: the `note` is the only thing that ever
@@ -3265,7 +3133,7 @@ fn parseDsrCol(seq: []const u8) ?usize {
return if (col == 0) null else col;
}
-const win = struct {
+pub const win = struct {
const HANDLE = *anyopaque;
const BOOL = i32;
const DWORD = u32;
@@ -5419,7 +5287,7 @@ pub fn main(init: std.process.Init) !void {
}
// Lifecycle hooks (pre_tool/post_tool/turn_end) from the same file.
// (Per-skill opt-outs were loaded earlier, before the muonry auto-connect.)
- g_hooks = loadHooks(io, arena);
+ g_hooks = hooks.loadHooks(io, arena);
if (g_hooks.total() > 0 and !json_mode and oneshot_prompt == null) {
try out.print("{s}loaded {d} lifecycle hook(s) from {s} — /hooks lists them{s}\n", .{ style.dim, g_hooks.total(), Approvals.settings_path, style.reset });
try out.flush();
@@ -6120,7 +5988,7 @@ pub fn main(init: std.process.Init) !void {
// `continue` above and never reach here, so ok is always true).
if (g_hooks.turn_end.len > 0) {
for (g_hooks.turn_end) |h| {
- const res = runHookCmd(gpa, io, h.command, "{\"event\":\"turn_end\",\"ok\":true}", h.timeout_ms);
+ const res = hooks.runHookCmd(gpa, io, h.command, "{\"event\":\"turn_end\",\"ok\":true}", h.timeout_ms);
if (res.stderr.len > 0) gpa.free(res.stderr);
}
}
@@ -12836,7 +12704,7 @@ fn hookGate(ctx: ToolCtx, call: ToolCall) ?ToolOutput {
defer ctx.gpa.free(payload);
for (g_hooks.pre_tool) |h| {
if (!h.matches(call.name)) continue;
- const res = runHookCmd(ctx.gpa, ctx.io, h.command, payload, h.timeout_ms);
+ const res = hooks.runHookCmd(ctx.gpa, ctx.io, h.command, payload, h.timeout_ms);
defer if (res.stderr.len > 0) ctx.gpa.free(res.stderr);
if (res.code) |c| if (c == 2) {
const msg: []const u8 = if (res.stderr.len > 0) res.stderr else "denied by hook";
@@ -12856,7 +12724,7 @@ fn runPostToolHooks(ctx: ToolCtx, call: ToolCall, out: ToolOutput) void {
defer ctx.gpa.free(payload);
for (g_hooks.post_tool) |h| {
if (!h.matches(call.name)) continue;
- const res = runHookCmd(ctx.gpa, ctx.io, h.command, payload, h.timeout_ms);
+ const res = hooks.runHookCmd(ctx.gpa, ctx.io, h.command, payload, h.timeout_ms);
if (res.stderr.len > 0) ctx.gpa.free(res.stderr);
}
}
@@ -15198,37 +15066,6 @@ test { // pull in tests from imported modules (mcp.zig)
_ = mcp;
}
-test "Hook.matches: wildcard, pipe lists, exact" {
- const star = Hook{ .match = "*", .command = "", .timeout_ms = 0 };
- try std.testing.expect(star.matches("bash"));
- try std.testing.expect(star.matches("anything"));
- const multi = Hook{ .match = "bash|edit_file | write_file", .command = "", .timeout_ms = 0 };
- try std.testing.expect(multi.matches("bash"));
- try std.testing.expect(multi.matches("edit_file"));
- try std.testing.expect(multi.matches("write_file")); // spaces trimmed
- try std.testing.expect(!multi.matches("read_file"));
- try std.testing.expect(!multi.matches("bas")); // no prefix matching
-}
-
-test "parseHookList: defaults, malformed entries skipped" {
- var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
- defer arena_state.deinit();
- const a = arena_state.allocator();
- const v = try std.json.parseFromSliceLeaky(Value, a,
- \\[{"command": "./guard.sh"},
- \\ {"match": "bash", "command": "lint", "timeout_ms": 500},
- \\ {"match": "no-command-key"},
- \\ "not-an-object",
- \\ {"command": ""}]
- , .{});
- const hooks = parseHookList(a, v);
- try std.testing.expectEqual(@as(usize, 2), hooks.len);
- try std.testing.expectEqualStrings("*", hooks[0].match); // default match
- try std.testing.expectEqual(@as(u64, 10_000), hooks[0].timeout_ms); // default timeout
- try std.testing.expectEqualStrings("bash", hooks[1].match);
- try std.testing.expectEqual(@as(u64, 500), hooks[1].timeout_ms);
-}
-
test "toolResultMessage: result text serializes as a JSON string in every wire format" {
var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena_state.deinit();