Skip to content

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

Python

test(integration): guard the two silent-failure classes this batch exposed

Rach Pradhanbef096e3eb4d
diff --git a/scripts/eval/tier1-manifest.json b/scripts/eval/tier1-manifest.json
index 0d11b785..b91b7aea 100644
--- a/scripts/eval/tier1-manifest.json
+++ b/scripts/eval/tier1-manifest.json
@@ -14,7 +14,7 @@
"src/main.zig",
"src/repl.zig"
],
- "test_count_baseline": 1113,
+ "test_count_baseline": 1116,
"test_count_slack": 25,
"required_invariants": [
{
diff --git a/src/prompt_snapshot_tests.zig b/src/prompt_snapshot_tests.zig
index d96a3520..84651543 100644
--- a/src/prompt_snapshot_tests.zig
+++ b/src/prompt_snapshot_tests.zig
@@ -336,7 +336,6 @@ test "#445: the transcript line is absent until the first compaction, then rides
var agent: agent_mod.Agent = undefined;
agent.sub = false;
- agent.io = std.testing.io; // the note block reads the fs when armed; a stub's undefined io segfaults
agent.session_name = "session-445";
prompts.g_session_compacted = false;
@@ -396,7 +395,6 @@ test "#445: /new and /clear disarm the transcript line again, and a later compac
var agent: agent_mod.Agent = undefined;
agent.sub = false;
- agent.io = std.testing.io; // the note block reads the fs when armed; a stub's undefined io segfaults
agent.session_name = "session-long";
prompts.g_session_compacted = false;
prompts.armSessionTranscript(a, agent.session_name, .{}, false);
@@ -463,7 +461,6 @@ test "#445: /resume disarms the transcript line and leaves no stale session path
var agent: agent_mod.Agent = undefined;
agent.sub = false;
- agent.io = std.testing.io; // the note block reads the fs when armed; a stub's undefined io segfaults
agent.session_name = "session-before";
prompts.g_session_compacted = false;
prompts.armSessionTranscript(a, agent.session_name, .{}, false);
@@ -521,3 +518,37 @@ test "#421: goal steering is empty outside a goal run" {
try std.testing.expectEqualStrings("", try repl_glue.goalSteeringNote(a, .{ .objective = "ship it", .status = .complete }));
try std.testing.expect((try repl_glue.goalSteeringNote(a, .{ .objective = "ship it", .status = .active })).len > 0);
}
+
+// #391 + #445 regression. The two features collided at integration in a way
+// neither suite could see: #391's note store is a PROCESS global whose
+// composition reads the filesystem through `agent.io`, and #445's tests drive
+// the funnel with stub Agents whose `io` is `undefined`. Once anything armed
+// the store, every later composition in the binary segfaulted inside
+// compact_note.pathFor — three tests away from the cause.
+//
+// The fix is that the funnel's IMPLICIT arming is gated on `!builtin.is_test`,
+// so a test can only arm the store by asking for it. This pins that: remove the
+// gate and this goes red instead of the segfault coming back.
+test "#391/#445: the prompt funnel never arms the note store in a test build" {
+ var a_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer a_state.deinit();
+ const a = a_state.allocator();
+
+ const saved = prompts.g_session_compacted;
+ defer prompts.g_session_compacted = saved;
+ defer prompts.armCompactNotes("");
+ defer prompts.armSessionTranscript(a, "", .{}, false);
+
+ prompts.armCompactNotes(""); // start disarmed, as a fresh process would
+ try std.testing.expect(!prompts.compactNotesArmed());
+
+ var agent: agent_mod.Agent = undefined;
+ agent.sub = false;
+ agent.session_name = "session-purity";
+ agent.io = std.testing.io; // a real io: this test is about arming, not about io
+ try prompts.setRootSystemPrompts(&agent, "BASE", a);
+
+ // Production arms here. A test build must not, or the funnel stops being
+ // the pure string function the rest of this suite depends on.
+ try std.testing.expect(!prompts.compactNotesArmed());
+}
diff --git a/src/prompts.zig b/src/prompts.zig
index 2eed0c51..aea0e13b 100644
--- a/src/prompts.zig
+++ b/src/prompts.zig
@@ -27,6 +27,7 @@
//! compaction and rides that boundary — see transcriptLineWanted below.
const std = @import("std");
+const builtin = @import("builtin");
const Io = std.Io;
const Allocator = std.mem.Allocator;
const shapes = @import("shapes.zig");
@@ -183,6 +184,13 @@ var g_note_session_buf: [256]u8 = undefined;
/// Armed by setRootSystemPrompts, and re-armed by the note writer itself so a
/// `/save <name>` mid-session cannot leave the injection reading a store the
/// writer has stopped writing to.
+/// Whether the note store currently points at a session. Exists so a test can
+/// pin the funnel's purity (see the #391/#445 regression test) without making
+/// the store itself public.
+pub fn compactNotesArmed() bool {
+ return g_note_session.len > 0;
+}
+
pub fn armCompactNotes(session_name: []const u8) void {
if (session_name.len == 0 or session_name.len > g_note_session_buf.len) {
g_note_session = "";
@@ -292,7 +300,15 @@ pub fn setRootSystemPrompts(agent: *Agent, base: []const u8, arena: Allocator) !
// dead process, and what that process left in the file is exactly the
// context this one loaded, so the line would again describe what is here.
armSessionTranscript(arena, agent.session_name, detectCaps(), g_session_compacted);
- armCompactNotes(agent.session_name); // #391: same session, same one-time arming
+ // #391: same session, same one-time arming — but only outside test builds.
+ // This is the IMPLICIT path, and a unit test reaching it with a stub Agent
+ // drags prompt composition into a filesystem read through an `undefined`
+ // io, segfaulting three tests away from the cause. That is exactly what
+ // happened when #445's tests met #391 at integration. A test that WANTS the
+ // note block calls armCompactNotes directly, as compact_note_glue_tests
+ // does; the funnel stays the pure string function the rest of the suite
+ // relies on. Same shape as #444's `emit_to_stderr = !builtin.is_test`.
+ if (!builtin.is_test) armCompactNotes(agent.session_name);
return setSystemPrompts(agent, base, arena);
}
@@ -369,8 +385,11 @@ pub fn resetSessionCompacted(agent: *Agent, arena: Allocator) void {
// name and injects the old conversation's notes into the new one, which is
// the same stale-identity bug this function exists to prevent for the
// transcript line, one store over.
- const note_moved = !std.mem.eql(u8, g_note_session, agent.session_name);
- armCompactNotes(agent.session_name);
+ // An UNARMED store has not "moved" — it was never pointed anywhere. Without
+ // this the predicate reads a fresh session as a move (`"" != name`) and
+ // defeats #445's own no-op-when-already-false contract.
+ const note_moved = g_note_session.len > 0 and !std.mem.eql(u8, g_note_session, agent.session_name);
+ if (!builtin.is_test) armCompactNotes(agent.session_name); // implicit path, see setRootSystemPrompts
if (!g_session_compacted and !note_moved) return;
g_session_compacted = false;
armSessionTranscript(arena, agent.session_name, detectCaps(), false);
@@ -450,13 +469,6 @@ test "setSystemPrompts (#326): derives all four variants from base, composing ul
defer arena_state.deinit();
const a = arena_state.allocator();
var agent: Agent = undefined;
- // #391's note block reads the filesystem whenever a session is armed, and
- // g_note_session is a PROCESS global: another test in this binary can leave
- // it armed, at which point this stub's undefined `io` segfaults inside
- // compact_note.load. A real io makes the read miss harmlessly instead, and
- // disarming keeps this test from depending on suite order either way.
- agent.io = std.testing.io;
- armCompactNotes("");
try setSystemPrompts(&agent, "BASE-A", a);
try std.testing.expectEqualStrings("BASE-A", agent.sys_normal);
diff --git a/src/session_settings.zig b/src/session_settings.zig
index bd16d82a..ebd1f0e5 100644
--- a/src/session_settings.zig
+++ b/src/session_settings.zig
@@ -42,7 +42,15 @@ pub const ThemeSetup = struct {
/// helper returns), so the `defer`s stay in main(), gated on the booleans
/// this returns; main() registers them in the same order as the original
/// inline code so LIFO defer-firing order is unchanged.
-pub fn setupSkillsAndTheme(io: Io, arena: Allocator, environ_map: anytype, out: *Io.Writer, flags: args.Flags, use_color: bool, json_mode: bool, cwd_display: []const u8) !ThemeSetup {
+/// Every `GRAFF_*` environment knob this binary honors, in one place.
+///
+/// Extracted from setupSkillsAndTheme so it can be driven directly by a test.
+/// The reason is concrete: #440 added GRAFF_TOOL_HANDLE_BYTES here while #429
+/// batch 2 moved this whole function to another file. Git merged both without
+/// a conflict and the knob simply stopped being parsed — no error, no failing
+/// test, a feature that silently ceased to exist. `envKnobsAreParsed` below
+/// now fails if that happens to any knob in this list.
+pub fn applyEnvKnobs(arena: Allocator, environ_map: anytype) !void {
// Companion auto-activation: if the metered code-intelligence companion
// (codedb-pro, formerly muonry) is installed but nothing connected it (no
// workspace .mcp.json entry, or consent declined), spawn it directly — a
@@ -115,6 +123,10 @@ pub fn setupSkillsAndTheme(io: Io, arena: Allocator, environ_map: anytype, out:
if (environ_map.get("GRAFF_WS_FORCE_FAIL_COUNT")) |v| {
ws.g_force_connect_failure_count = std.fmt.parseInt(u8, std.mem.trim(u8, v, " \t"), 10) catch 0;
}
+}
+
+pub fn setupSkillsAndTheme(io: Io, arena: Allocator, environ_map: anytype, out: *Io.Writer, flags: args.Flags, use_color: bool, json_mode: bool, cwd_display: []const u8) !ThemeSetup {
+ try applyEnvKnobs(arena, environ_map);
skills.loadSkillSettings(io, arena); // per-skill opt-outs, also gates the auto-connect
anim.loadAnimationSetting(io, arena); // {"animation": "..."} → thinking spinner choice
anim.loadThemeSetting(io, arena); // {"theme": "<name>"} → opt-in terminal color theme
diff --git a/src/session_settings_tests.zig b/src/session_settings_tests.zig
new file mode 100644
index 00000000..6eff1cbb
--- /dev/null
+++ b/src/session_settings_tests.zig
@@ -0,0 +1,184 @@
+//! Guard against a `GRAFF_*` knob silently ceasing to exist.
+//!
+//! The failure this exists for actually happened, on 2026-08-06: #440 added
+//! `GRAFF_TOOL_HANDLE_BYTES` to `setupSkillsAndTheme` while #429 batch 2 moved
+//! that entire function into this module. Git merged both branches without a
+//! conflict, both suites stayed green, and the knob simply stopped being
+//! parsed. No error, no failing test — a shipped feature that quietly became
+//! unreachable, and it was caught by eye during integration rather than by
+//! anything automatic.
+//!
+//! The important test here is `every knob is actually read`. It does not check
+//! what a knob DOES; it checks that `applyEnvKnobs` asks for the name at all.
+//! A refactor that drops a parse makes the name go unqueried, and this goes
+//! red. Checking the effect instead would not catch the case where a whole
+//! block is dropped, because there would be nothing left to assert against.
+
+const std = @import("std");
+const session_settings = @import("session_settings.zig");
+const main_mod = @import("main.zig");
+const provider_mod = @import("provider.zig");
+const tool_handle = @import("tool_handle.zig");
+const no_local_tools = @import("no_local_tools.zig");
+const http = @import("http.zig");
+const ws = @import("ws.zig");
+const agent_ws = @import("agent_ws.zig");
+
+const Knob = struct { name: []const u8, value: []const u8 };
+
+/// Every knob `applyEnvKnobs` is expected to honor. Adding a knob there without
+/// adding it here is fine (this list is a floor, not a ceiling); REMOVING the
+/// parse while the knob is still documented is what this catches.
+const knobs = [_]Knob{
+ .{ .name = "GRAFF_NO_CODEDB_GUARD", .value = "1" },
+ .{ .name = "GRAFF_FORCE_STALL_ONCE", .value = "1" },
+ .{ .name = "GRAFF_FORCE_DROP_ONCE", .value = "1" },
+ .{ .name = "GRAFF_FORCE_STALL_ALWAYS", .value = "1" },
+ .{ .name = "GRAFF_FORCE_DROP_ALWAYS", .value = "1" },
+ .{ .name = "GRAFF_STREAM_STALL_SECS", .value = "7" },
+ .{ .name = "GRAFF_CODEX_WS", .value = "off" },
+ .{ .name = "GRAFF_CLOCK_SLEEP", .value = "1" },
+ .{ .name = "GRAFF_NO_LOCAL_TOOLS", .value = "1" },
+ .{ .name = "GRAFF_CODEX_WS_IDLE_SECS", .value = "11" },
+ .{ .name = "GRAFF_CONTEXT", .value = "123456" },
+ .{ .name = "GRAFF_TOOL_HANDLE_BYTES", .value = "8192" },
+ .{ .name = "GRAFF_COMPACT_PCT", .value = "55" },
+ .{ .name = "GRAFF_WS_DEBUG", .value = "1" },
+ .{ .name = "GRAFF_WS_FORCE_FAIL_ONCE", .value = "1" },
+ .{ .name = "GRAFF_WS_FORCE_FAIL_COUNT", .value = "3" },
+};
+
+/// A stand-in for the process environment that records which names were asked
+/// for. Deliberately array-backed rather than a hash map: no allocator, and the
+/// recording is what the test is actually about.
+const RecordingEnv = struct {
+ asked: *[knobs.len]bool,
+
+ pub fn get(self: RecordingEnv, name: []const u8) ?[]const u8 {
+ for (knobs, 0..) |k, i| {
+ if (std.mem.eql(u8, k.name, name)) {
+ self.asked[i] = true;
+ return k.value;
+ }
+ }
+ return null; // PATH and anything else: absent, as an empty env would be
+ }
+};
+
+/// Save/restore for every global `applyEnvKnobs` writes. These are process
+/// globals in a single test binary, so leaking one would corrupt whatever test
+/// runs next — the exact failure mode that made #391's note store segfault
+/// three unrelated tests during the same integration.
+const Saved = struct {
+ codedb_guard: bool,
+ stall_once: bool,
+ drop_once: bool,
+ stall_always: bool,
+ drop_always: bool,
+ codex_ws: bool,
+ clock_sleep: bool,
+ stream_stall_ms: u64,
+ codex_ws_idle_ms: i64,
+ context_override: ?u64,
+ compact_pct_override: ?u8,
+ threshold_bytes: usize,
+ no_local: bool,
+ ws_debug: bool,
+ ws_fail_once: bool,
+ ws_fail_count: u8,
+
+ fn capture() Saved {
+ return .{
+ .codedb_guard = main_mod.g_codedb_guard,
+ .stall_once = main_mod.g_force_stall_once,
+ .drop_once = main_mod.g_force_drop_once,
+ .stall_always = main_mod.g_force_stall_always,
+ .drop_always = main_mod.g_force_drop_always,
+ .codex_ws = main_mod.g_codex_ws,
+ .clock_sleep = main_mod.g_clock_sleep,
+ .stream_stall_ms = http.stream_stall_ms,
+ .codex_ws_idle_ms = agent_ws.codex_ws_idle_ms,
+ .context_override = provider_mod.g_context_override,
+ .compact_pct_override = provider_mod.g_compact_pct_override,
+ .threshold_bytes = tool_handle.threshold_bytes,
+ .no_local = no_local_tools.enabled,
+ .ws_debug = ws.g_debug,
+ .ws_fail_once = ws.g_force_connect_failure_once,
+ .ws_fail_count = ws.g_force_connect_failure_count,
+ };
+ }
+
+ fn restore(s: Saved) void {
+ main_mod.g_codedb_guard = s.codedb_guard;
+ main_mod.g_force_stall_once = s.stall_once;
+ main_mod.g_force_drop_once = s.drop_once;
+ main_mod.g_force_stall_always = s.stall_always;
+ main_mod.g_force_drop_always = s.drop_always;
+ main_mod.g_codex_ws = s.codex_ws;
+ main_mod.g_clock_sleep = s.clock_sleep;
+ http.stream_stall_ms = s.stream_stall_ms;
+ agent_ws.codex_ws_idle_ms = s.codex_ws_idle_ms;
+ provider_mod.g_context_override = s.context_override;
+ provider_mod.g_compact_pct_override = s.compact_pct_override;
+ tool_handle.threshold_bytes = s.threshold_bytes;
+ no_local_tools.enabled = s.no_local;
+ ws.g_debug = s.ws_debug;
+ ws.g_force_connect_failure_once = s.ws_fail_once;
+ ws.g_force_connect_failure_count = s.ws_fail_count;
+ }
+};
+
+test "every GRAFF_ knob is actually read by applyEnvKnobs (a dropped parse is invisible otherwise)" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+
+ const saved = Saved.capture();
+ defer saved.restore();
+
+ var asked: [knobs.len]bool = @splat(false);
+ try session_settings.applyEnvKnobs(arena_state.allocator(), RecordingEnv{ .asked = &asked });
+
+ for (knobs, 0..) |k, i| {
+ if (!asked[i]) {
+ std.debug.print("\nknob '{s}' is no longer read by applyEnvKnobs\n", .{k.name});
+ return error.KnobNoLongerParsed;
+ }
+ }
+}
+
+test "applyEnvKnobs actually applies the values it reads" {
+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena_state.deinit();
+
+ const saved = Saved.capture();
+ defer saved.restore();
+
+ // Start from the opposite of every expected outcome, so a no-op parse
+ // cannot pass by coincidence.
+ main_mod.g_codedb_guard = true;
+ main_mod.g_force_stall_once = false;
+ main_mod.g_codex_ws = true;
+ main_mod.g_clock_sleep = false;
+ no_local_tools.enabled = false;
+ ws.g_debug = false;
+ provider_mod.g_context_override = null;
+ provider_mod.g_compact_pct_override = null;
+ tool_handle.threshold_bytes = tool_handle.default_threshold_bytes;
+
+ var asked: [knobs.len]bool = @splat(false);
+ try session_settings.applyEnvKnobs(arena_state.allocator(), RecordingEnv{ .asked = &asked });
+
+ try std.testing.expect(!main_mod.g_codedb_guard); // present ⇒ guard OFF
+ try std.testing.expect(main_mod.g_force_stall_once);
+ try std.testing.expect(!main_mod.g_codex_ws); // "off" disables the WS transport
+ try std.testing.expect(main_mod.g_clock_sleep);
+ try std.testing.expect(no_local_tools.enabled);
+ try std.testing.expect(ws.g_debug);
+ try std.testing.expectEqual(@as(u64, 7_000), http.stream_stall_ms); // seconds → ms
+ try std.testing.expectEqual(@as(i64, 11_000), agent_ws.codex_ws_idle_ms);
+ try std.testing.expectEqual(@as(?u64, 123_456), provider_mod.g_context_override);
+ try std.testing.expectEqual(@as(?u8, 55), provider_mod.g_compact_pct_override);
+ try std.testing.expectEqual(@as(usize, 8192), tool_handle.threshold_bytes);
+ try std.testing.expect(ws.g_force_connect_failure_once);
+ try std.testing.expectEqual(@as(u8, 3), ws.g_force_connect_failure_count);
+}
diff --git a/src/test_hooks.zig b/src/test_hooks.zig
index 49cc1b45..906f62ab 100644
--- a/src/test_hooks.zig
+++ b/src/test_hooks.zig
@@ -73,6 +73,10 @@ const session_tests = @import("session_tests.zig");
// #441: and session_transcript.zig's, moved off it for the same reason again.
// The module itself is reached from session.queueSave, but this FILE is not, so
// without the hook its whole suite compiles to nothing and reports green.
+// #429 batch 2 + #440: the env-knob guard. Its own module, so a knob that
+// stops being parsed fails a test instead of vanishing silently.
+const session_settings_tests = @import("session_settings_tests.zig");
+
const session_transcript_tests = @import("session_transcript_tests.zig");
// #445: moved off commands_session.zig when the transcript-line reset needed
@@ -197,6 +201,7 @@ test {
_ = scoring_slot_test;
_ = session_tests;
_ = session_transcript_tests;
+ _ = session_settings_tests;
_ = commands_session_test;
_ = mcp_config;
_ = mcp_schema_gate;