graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Give tool children non-interactive editor, pager and prompt defaults (#1268)
Rach Pradhan5e7821d2fb6a
diff --git a/src/tool_env.zig b/src/tool_env.zigindex 8dff6667..d6e1395e 100644--- a/src/tool_env.zig+++ b/src/tool_env.zig@@ -9,6 +9,13 @@//! GRAFF_TOOL_ENV_PASS (comma-separated names) keeps any of them for the rare//! command that really needs one.//!+//! #1268: a tool child has no terminal (stdin is /dev/null). `git commit`+//! without -m, `git rebase -i`, or anything that opens a pager waited on the+//! user's editor or pager until the call's deadline. Editors become `true`+//! (git then aborts on the empty message), pagers `cat`, and git's+//! credential prompt is switched off. Not on Windows, where these commands+//! and that failure mode do not apply the same way.+//!//! Children graff launches as graff (learning runs) and MCP servers do not//! come through here: they keep the full environment, because they are the//! ones that legitimately read those keys. Subagents run in-process.@@ -30,6 +37,20 @@ const extra_secrets = [_][]const u8{"HARNESS_SERVE_TOKEN",};+/// Non-interactive defaults every tool child gets (#1268), over whatever the+/// user has set: a tool call can never answer an editor or a prompt.+pub const non_interactive = [_][2][]const u8{+ .{ "GIT_EDITOR", "true" },+ .{ "GIT_SEQUENCE_EDITOR", "true" },+ .{ "EDITOR", "true" },+ .{ "VISUAL", "true" },+ .{ "PAGER", "cat" },+ .{ "GIT_PAGER", "cat" },+ .{ "GIT_TERMINAL_PROMPT", "0" },+ .{ "GIT_ASKPASS", "true" },+ .{ "DEBIAN_FRONTEND", "noninteractive" },+};+var g_map: ?Map = null;/// The map to hand `environ_map` when spawning a tool child. Null before@@ -54,6 +75,7 @@ pub fn build(gpa: Allocator, parent: *const Map) Allocator.Error!Map {for (provider.provider_specs) |spec| scrub(&m, spec.env_key, pass);if (provider.additional_router) |spec| scrub(&m, spec.env_key, pass);for (extra_secrets) |name| scrub(&m, name, pass);+ if (builtin.os.tag != .windows) for (non_interactive) |kv| try m.put(kv[0], kv[1]);return m;}@@ -115,11 +137,12 @@ test "#1267: GRAFF_TOOL_ENV_PASS keeps the named keys only" {try std.testing.expect(m.get("NOT_SET") == null);}-/// Install `build(fake parent)` as the tool environment for one test and-/// return what it replaced.-fn installFake(gpa: Allocator) !?Map {+/// Install `build(fake parent + extra)` as the tool environment for one+/// test and return what it replaced.+fn installFake(gpa: Allocator, extra: []const [2][]const u8) !?Map {var parent = try fakeParent(gpa);defer parent.deinit();+ for (extra) |kv| try parent.put(kv[0], kv[1]);const saved = g_map;g_map = try build(gpa, &parent);return saved;@@ -130,7 +153,7 @@ test "#1267: a real tool child (both shell paths) cannot see the key" {const gpa = std.testing.allocator;const io = std.testing.io;const jobs = @import("jobs.zig");- const saved = try installFake(gpa);+ const saved = try installFake(gpa, &.{});defer {g_map.?.deinit();g_map = saved;@@ -160,3 +183,49 @@ test "#1267: a real tool child (both shell paths) cannot see the key" {try std.testing.expect(std.mem.indexOf(u8, waited.done.output, "xai-fake") == null);try std.testing.expect(std.mem.indexOf(u8, waited.done.output, "OPENAI_API_KEY") == null);}++test "#1268: tool env pins editors, pagers and prompts to non-interactive values" {+ if (builtin.os.tag == .windows) return error.SkipZigTest;+ const gpa = std.testing.allocator;+ var parent = try fakeParent(gpa);+ defer parent.deinit();+ try parent.put("EDITOR", "vim");+ try parent.put("GIT_PAGER", "less -R");+ var m = try build(gpa, &parent);+ defer m.deinit();+ try std.testing.expect(m.get("OPENAI_API_KEY") == null);+ for (non_interactive) |kv| try std.testing.expectEqualStrings(kv[1], m.get(kv[0]) orelse "<unset>");+ try std.testing.expectEqualStrings("0", m.get("GIT_TERMINAL_PROMPT") orelse "<unset>");+ try std.testing.expect(m.get("GIT_TERMINAL_PROMPTS") == null);+}++test "#1268: git commit without -m in a tool shell returns instead of waiting on the editor" {+ if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;+ const gpa = std.testing.allocator;+ const io = std.testing.io;+ const jobs = @import("jobs.zig");+ // The user's editor, as a tool child used to inherit it: never returns.+ const saved = try installFake(gpa, &.{+ .{ "EDITOR", "sleep 30" },+ .{ "GIT_EDITOR", "sleep 30" },+ .{ "GIT_CONFIG_GLOBAL", "/dev/null" },+ .{ "GIT_CONFIG_NOSYSTEM", "1" },+ });+ defer {+ g_map.?.deinit();+ g_map = saved;+ }+ const script =+ \\command -v git >/dev/null || exit 127+ \\d=$(mktemp -d) || exit 126+ \\cd "$d" && git init -q && git -c user.name=t -c [email protected] commit --allow-empty+ \\rc=$?; cd /; rm -rf "$d"; exit $rc+ ;+ const sh = jobs.shellArgv(script);+ const run = try jobs.runCappedWithOptions(gpa, io, &sh, 64 * 1024, 64 * 1024, 8_000, jobs.toolRunOptions(null));+ defer gpa.free(run.stdout);+ defer gpa.free(run.stderr);+ if (run.term == .exited and run.term.exited == 127) return error.SkipZigTest; // no git here+ try std.testing.expect(!run.timed_out);+ try std.testing.expect(std.mem.indexOf(u8, run.stderr, "empty commit message") != null);+}