graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
feat(hooks): per-hook `suggest` field surfaced in pre_tool denials (#369)
justrachf158134297dc
diff --git a/src/hooks.zig b/src/hooks.zigindex 27519782..22ef9b9d 100644--- a/src/hooks.zig+++ b/src/hooks.zig@@ -39,6 +39,10 @@ pub const Hook = struct {match: []const u8,command: []const u8,timeout_ms: u64,+ /// Optional sanctioned replacement named in the denial the model sees+ /// (#369): converts a blocked call into a 1-call recovery instead of a+ /// guess-and-probe spiral.+ suggest: []const u8 = "",pub fn matches(self: Hook, tool: []const u8) bool {if (std.mem.eql(u8, self.match, "*")) return true;@@ -75,7 +79,11 @@ fn parseHookList(arena: Allocator, v: ?Value) []const Hook {(if (t == .integer and t.integer > 0) @intCast(t.integer) else 10_000)else10_000;- list.append(arena, .{ .match = match, .command = cmd.string, .timeout_ms = timeout }) catch continue;+ const suggest: []const u8 = if (item.object.get("suggest")) |sg|+ (if (sg == .string) sg.string else "")+ else+ "";+ list.append(arena, .{ .match = match, .command = cmd.string, .timeout_ms = timeout, .suggest = suggest }) catch continue;}return list.items;}@@ -97,6 +105,15 @@ pub fn loadHooks(io: Io, arena: Allocator) Hooks {const HookRun = struct { code: ?u8, stderr: []u8 };+/// Denial text for a blocking pre_tool hook: the hook's stderr (or a stock+/// phrase), plus its configured `suggest` replacement when present (#369).+/// Null only on OOM; the caller substitutes an empty error result.+pub fn denialText(gpa: Allocator, h: Hook, stderr: []const u8) ?[]u8 {+ const msg: []const u8 = if (stderr.len > 0) stderr else "denied by hook";+ const sep: []const u8 = if (h.suggest.len > 0) " — use instead: " else "";+ return std.fmt.allocPrint(gpa, "blocked by pre_tool hook: {s}{s}{s}", .{ msg, sep, h.suggest }) catch null;+}+/// 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).@@ -174,7 +191,7 @@ test "parseHookList: defaults, malformed entries skipped" {const a = arena_state.allocator();const v = try std.json.parseFromSliceLeaky(Value, a,\\[{"command": "./guard.sh"},- \\ {"match": "bash", "command": "lint", "timeout_ms": 500},+ \\ {"match": "bash", "command": "lint", "timeout_ms": 500, "suggest": "use mcp edit"},\\ {"match": "no-command-key"},\\ "not-an-object",\\ {"command": ""}]@@ -185,6 +202,23 @@ test "parseHookList: defaults, malformed entries skipped" {try std.testing.expectEqual(@as(u64, 10_000), hooks[0].timeout_ms); // default timeouttry std.testing.expectEqualStrings("bash", hooks[1].match);try std.testing.expectEqual(@as(u64, 500), hooks[1].timeout_ms);+ try std.testing.expectEqualStrings("", hooks[0].suggest); // default: no suggestion+ try std.testing.expectEqualStrings("use mcp edit", hooks[1].suggest);+}++test "denialText: stderr, stock phrase, suggest suffix" {+ const gpa = std.testing.allocator;+ const plain = Hook{ .match = "*", .command = "", .timeout_ms = 0 };+ const t1 = denialText(gpa, plain, "nope").?;+ defer gpa.free(t1);+ try std.testing.expectEqualStrings("blocked by pre_tool hook: nope", t1);+ const t2 = denialText(gpa, plain, "").?;+ defer gpa.free(t2);+ try std.testing.expectEqualStrings("blocked by pre_tool hook: denied by hook", t2);+ const sug = Hook{ .match = "*", .command = "", .timeout_ms = 0, .suggest = "mcp__codedbpro__replace" };+ const t3 = denialText(gpa, sug, "native edit off").?;+ defer gpa.free(t3);+ try std.testing.expectEqualStrings("blocked by pre_tool hook: native edit off — use instead: mcp__codedbpro__replace", t3);}const jobs = @import("jobs.zig");diff --git a/src/tools.zig b/src/tools.zigindex 9dbe1b31..4c842bcc 100644--- a/src/tools.zig+++ b/src/tools.zig@@ -226,8 +226,7 @@ pub fn hookGate(ctx: ToolCtx, call: ToolCall) ?ToolOutput {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";- const text = std.fmt.allocPrint(ctx.gpa, "blocked by pre_tool hook: {s}", .{msg}) catch+ const text = hooks.denialText(ctx.gpa, h, res.stderr) orelsereturn .{ .text = &.{}, .is_error = true };return .{ .text = text, .is_error = true };};