graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Name the sender when graff is stopped by SIGTERM
justracheeddaadd898c
diff --git a/src/main.zig b/src/main.zigindex 02c6523e..31486ea2 100644--- a/src/main.zig+++ b/src/main.zig@@ -50,7 +50,7 @@ const learn_eval = @import("learn_eval.zig");const learn_cli = @import("learn_cli.zig");test {_ = .{ @import("http2_buffered.zig"), @import("clipboard_native.zig"), @import("clipboard_edge_tests.zig"), @import("clipboard_failure_tests.zig"), @import("process_runner_clipboard_tests.zig") };- _ = @import("server_orphan.zig");+ _ = .{ @import("server_orphan.zig"), @import("term_sender.zig") };_ = @import("repo_transaction.zig");_ = .{ @import("pr_command.zig"), @import("pr_evidence.zig"), @import("artifact_repository.zig"), @import("artifact_claim_ledger.zig"), @import("artifact_claim_store.zig"), @import("artifact_claim_stale.zig"), @import("artifact_claim_unresolved.zig") };_ = @import("argstream_citation_tests.zig"); // unit_tests' root is main.zig only, so reference every split-out module or its tests silently never run@@ -252,9 +252,9 @@ pub fn main(init: std.process.Init) !void {const gpa = init.gpa;const io = init.io;const arena = init.arena.allocator();- // #122: raise the open-file soft limit to the OS hard cap (Darwin defaults- // to 256). Raising it avoids ProcessFdQuotaExceeded during parallel tools.+ // #122: raise the open-file soft limit to the OS hard cap (Darwin defaults to 256) to avoid ProcessFdQuotaExceeded during parallel tools.std.process.raiseFileDescriptorLimit();+ @import("term_sender.zig").install(); // a SIGTERM names its sender on stderr// #124: a per-turn scratch arena for the root agent's transient parse garbage,// reset each request() so a long REPL/--json/serve session's RSS stays flat.var scratch_state = std.heap.ArenaAllocator.init(gpa);diff --git a/src/term_sender.zig b/src/term_sender.zignew file mode 100644index 00000000..d01190aa--- /dev/null+++ b/src/term_sender.zig@@ -0,0 +1,61 @@+//! Name whoever sends graff SIGTERM. A client that sees only "exited+//! unexpectedly (killed by signal 15)" plus graff's last stderr lines cannot+//! tell an outside `kill`/`pkill` from its own shutdown. The handler writes one+//! stderr line with the sender's pid and process name, restores the default+//! action, and re-raises, so the exit status is still signal 15.+const std = @import("std");+const builtin = @import("builtin");+const posix = std.posix;++const supported = builtin.os.tag == .macos or builtin.os.tag == .linux;++extern "c" fn proc_name(pid: c_int, buffer: [*]u8, buffersize: u32) c_int;++pub fn install() void {+ if (comptime !supported) return;+ const act: posix.Sigaction = .{+ .handler = .{ .sigaction = handle },+ .mask = posix.sigemptyset(),+ .flags = posix.SA.SIGINFO | posix.SA.RESETHAND,+ };+ posix.sigaction(.TERM, &act, null);+}++fn handle(sig: posix.SIG, info: *const posix.siginfo_t, _: ?*anyopaque) callconv(.c) void {+ const pid: i32 = if (comptime builtin.os.tag == .linux) info.fields.common.first.piduid.pid else info.pid;+ var name_buf: [64]u8 = undefined;+ var line_buf: [160]u8 = undefined;+ const line = note(&line_buf, pid, senderName(pid, &name_buf));+ _ = posix.system.write(2, line.ptr, line.len);+ posix.raise(sig) catch {};+}++/// Async-signal-safe: one syscall-backed lookup, no allocation.+fn senderName(pid: i32, buf: []u8) []const u8 {+ if (pid <= 0) return "";+ if (comptime builtin.os.tag == .macos) {+ const n = proc_name(pid, buf.ptr, @intCast(buf.len));+ return if (n > 0) buf[0..@min(@as(usize, @intCast(n)), buf.len)] else "";+ }+ var path_buf: [32]u8 = undefined;+ const path = std.fmt.bufPrintZ(&path_buf, "/proc/{d}/comm", .{pid}) catch return "";+ const fd = std.os.linux.open(path, .{ .ACCMODE = .RDONLY }, 0);+ if (std.os.linux.errno(fd) != .SUCCESS) return "";+ defer _ = std.os.linux.close(@intCast(fd));+ const n = std.os.linux.read(@intCast(fd), buf.ptr, buf.len);+ if (std.os.linux.errno(n) != .SUCCESS) return "";+ return std.mem.trimEnd(u8, buf[0..n], "\n");+}++pub fn note(buf: []u8, pid: i32, name: []const u8) []const u8 {+ if (pid <= 0) return "graff: stopped by SIGTERM (sender unknown)\n";+ if (name.len == 0) return std.fmt.bufPrint(buf, "graff: stopped by SIGTERM from pid {d}\n", .{pid}) catch "graff: stopped by SIGTERM\n";+ return std.fmt.bufPrint(buf, "graff: stopped by SIGTERM from pid {d} ({s})\n", .{ pid, name }) catch "graff: stopped by SIGTERM\n";+}++test "the SIGTERM note names the sender when it can" {+ var buf: [160]u8 = undefined;+ try std.testing.expectEqualStrings("graff: stopped by SIGTERM from pid 4242 (pkill)\n", note(&buf, 4242, "pkill"));+ try std.testing.expectEqualStrings("graff: stopped by SIGTERM from pid 7\n", note(&buf, 7, ""));+ try std.testing.expectEqualStrings("graff: stopped by SIGTERM (sender unknown)\n", note(&buf, 0, "x"));+}