graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
//! Job-pool tests. Split out of jobs.zig so waitForeground (#620) fits under
//! the 600-line ceiling. Imported from jobs.zig's `test {}` so they stay in
//! the suite.
const std = @import("std");
const builtin = @import("builtin");
const jobs = @import("jobs.zig");
const job_idle = @import("job_idle.zig");
const job_registry = @import("job_registry.zig");
const proc_identity = @import("proc_identity.zig");
test {
_ = @import("jobs_completion_tests.zig");
}
test "#728 consumed successful blocking output cannot inject a late completion" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
const notify = @import("job_notify.zig");
const Agent = @import("agent.zig").Agent;
const io = std.testing.io;
const gpa = std.testing.allocator;
jobs.g_jobs = .{};
Agent.esc_cancel.store(false, .release);
var wake: [512]u8 = undefined;
while (notify.takeWake(io, &wake) != null) {}
defer jobs.jobsReap(gpa, io);
// More than the notice capacity: correctness must not depend on a
// bounded cache remembering consumed ids while pump publication lags.
for (0..24) |_| {
const id = (try jobs.spawnJob(gpa, io, "printf success; exit 0")).id;
const output = try jobs.jobOutput(gpa, io, id, 1);
defer gpa.free(output.text);
try std.testing.expect(!output.is_error);
try std.testing.expect(std.mem.indexOf(u8, output.text, "exited with code 0") != null);
try std.testing.expect(std.mem.indexOf(u8, output.text, "success") != null);
jobs.reapFinished(gpa, io, id); // await pump, including late publication
try std.testing.expect(notify.takeWake(io, &wake) == null);
try std.testing.expect(!Agent.esc_cancel.load(.acquire));
}
}
test "foreground tool subprocesses own their process group (#266, #198)" {
const inherited = jobs.toolRunOptions(null);
try std.testing.expect(inherited.kill_process_tree);
try std.testing.expect(std.meta.activeTag(inherited.cwd) == .inherit);
const pinned = jobs.toolRunOptions("/tmp/graff-worktree");
try std.testing.expect(pinned.kill_process_tree);
try std.testing.expectEqualStrings("/tmp/graff-worktree", pinned.cwd.path);
}
test "killing a background job takes its grandchildren with it (#198)" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
jobs.g_jobs = .{};
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
// The job inherits this process's cwd, the one tmpDir resolved .zig-cache
// against, so the marker path is reachable from the shell. The grandchild
// detaches from the job's pipes or the pump never sees EOF; `sh` execs the
// trailing sleep, so a bare child kill orphans the marker subshell.
const cmd = try std.fmt.allocPrint(gpa, "(sleep 0.8; printf survived > .zig-cache/tmp/{s}/marker) >/dev/null 2>&1 & sleep 30", .{&tmp.sub_path});
defer gpa.free(cmd);
const id = (try jobs.spawnJob(gpa, io, cmd)).id;
defer jobs.jobsReap(gpa, io);
const killed = try jobs.jobKill(gpa, io, id);
gpa.free(killed.text);
io.sleep(.fromMilliseconds(1_200), .awake) catch {};
try std.testing.expectError(error.FileNotFound, tmp.dir.openFile(io, "marker", .{}));
}
test "isolated capped runs clean descendants after timeout and normal exit" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
const timed = try jobs.runCappedWithOptions(
std.testing.allocator,
io,
&.{ "/bin/sh", "-c", "(sleep 0.8; printf survived > timeout-marker) >/dev/null 2>&1 & sleep 30" },
1024,
1024,
50,
.{ .cwd = .{ .dir = tmp.dir }, .kill_process_tree = true },
);
defer {
std.testing.allocator.free(timed.stdout);
std.testing.allocator.free(timed.stderr);
}
try std.testing.expect(timed.timed_out);
const completed = try jobs.runCappedWithOptions(
std.testing.allocator,
io,
&.{ "/bin/sh", "-c", "(sleep 0.8; printf survived > completed-marker) >/dev/null 2>&1 & exit 0" },
1024,
1024,
2_000,
.{ .cwd = .{ .dir = tmp.dir }, .kill_process_tree = true },
);
defer {
std.testing.allocator.free(completed.stdout);
std.testing.allocator.free(completed.stderr);
}
try std.testing.expect(jobs.ranOk(completed));
io.sleep(.fromMilliseconds(1_000), .awake) catch {};
try std.testing.expectError(error.FileNotFound, tmp.dir.openFile(io, "timeout-marker", .{}));
try std.testing.expectError(error.FileNotFound, tmp.dir.openFile(io, "completed-marker", .{}));
}
test "bash_output wait_ms on a persistent job snapshots immediately (ADR 0152)" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
jobs.g_jobs = .{};
const gpa = std.testing.allocator;
const io = std.testing.io;
const id = (try jobs.spawnJobOpts(gpa, io, "sleep 8", .{ .persistent = true })).id;
defer jobs.jobsReap(gpa, io);
const t0 = std.Io.Timestamp.now(io, .awake).nanoseconds;
const snap = try jobs.jobOutput(gpa, io, id, 5_000);
const elapsed_ms = @divTrunc(std.Io.Timestamp.now(io, .awake).nanoseconds - t0, std.time.ns_per_ms);
defer gpa.free(snap.text);
try std.testing.expect(elapsed_ms < 800);
try std.testing.expect(std.mem.indexOf(u8, snap.text, "running") != null);
try std.testing.expect(std.mem.indexOf(u8, snap.text, "persistent server") != null);
try std.testing.expect(std.mem.indexOf(u8, snap.text, "do not poll") != null);
try std.testing.expect(std.mem.indexOf(u8, snap.text, "omit wait_ms") == null);
const again = try jobs.jobOutput(gpa, io, id, 30_000);
defer gpa.free(again.text);
try std.testing.expect(std.mem.indexOf(u8, again.text, "running") != null);
}
test "bash_output wait_ms>0 waits for exit, not the next byte (ADR 0010)" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
jobs.g_jobs = .{};
const gpa = std.testing.allocator;
const io = std.testing.io;
const id = (try jobs.spawnJob(gpa, io, "printf start; sleep 0.15; printf done; exit 7")).id;
defer jobs.jobsReap(gpa, io);
const snap = try jobs.jobOutput(gpa, io, id, 0);
defer gpa.free(snap.text);
const done = try jobs.jobOutput(gpa, io, id, 30_000);
defer gpa.free(done.text);
const text = if (std.mem.indexOf(u8, done.text, "exited with code 7") != null) done.text else snap.text;
try std.testing.expect(std.mem.indexOf(u8, text, "exited with code 7") != null);
}
test "#620: a still-running foreground wait promotes instead of killing" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
jobs.g_jobs = .{};
const gpa = std.testing.allocator;
const io = std.testing.io;
const id = (try jobs.spawnJob(gpa, io, "sleep 2; printf never")).id;
defer jobs.jobsReap(gpa, io);
const waited = try jobs.waitForeground(gpa, io, id, 200);
defer switch (waited) {
.running => |r| gpa.free(r.output),
.done => |d| gpa.free(d.output),
.cancelled => |c| gpa.free(c.output),
};
try std.testing.expect(waited == .running);
try std.testing.expectEqual(id, waited.running.id);
const snap = try jobs.jobOutput(gpa, io, id, 0);
defer gpa.free(snap.text);
try std.testing.expect(std.mem.indexOf(u8, snap.text, "running") != null);
}
test "#850: a bounded promote leaves the child on bash_output and bash_kill" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
jobs.g_jobs = .{};
const gpa = std.testing.allocator;
const io = std.testing.io;
const id = (try jobs.spawnJob(gpa, io, "sleep 8; printf never")).id;
defer jobs.jobsReap(gpa, io);
const waited = try jobs.waitForeground(gpa, io, id, 200);
defer switch (waited) {
.running => |r| gpa.free(r.output),
.done => |d| gpa.free(d.output),
.cancelled => |c| gpa.free(c.output),
};
try std.testing.expect(waited == .running);
const snap = try jobs.jobOutput(gpa, io, id, 0);
defer gpa.free(snap.text);
try std.testing.expect(std.mem.indexOf(u8, snap.text, "running") != null);
try std.testing.expect(snap.pending and !snap.is_error);
const killed = try jobs.jobKill(gpa, io, id);
defer gpa.free(killed.text);
try std.testing.expect(!killed.is_error);
try std.testing.expect(std.mem.indexOf(u8, killed.text, "killed") != null);
const after = try jobs.jobOutput(gpa, io, id, 2_000);
defer gpa.free(after.text);
try std.testing.expect(std.mem.indexOf(u8, after.text, "running") == null);
try std.testing.expect(after.cancelled and after.is_error and !after.pending);
}
test "ADR 0152: auto-parked job output ignores wait_ms" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
jobs.g_jobs = .{};
const gpa = std.testing.allocator;
const io = std.testing.io;
const id = (try jobs.spawnJob(gpa, io, "sleep 8; printf never")).id;
defer jobs.jobsReap(gpa, io);
jobs.markPersistent(io, id);
const snap = try jobs.jobOutput(gpa, io, id, 30_000);
defer gpa.free(snap.text);
try std.testing.expect(std.mem.indexOf(u8, snap.text, "running") != null);
try std.testing.expect(std.mem.indexOf(u8, snap.text, "do not poll") != null);
}
test "#620: a short foreground command finishes as done, not a job" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
jobs.g_jobs = .{};
const gpa = std.testing.allocator;
const io = std.testing.io;
const id = (try jobs.spawnJob(gpa, io, "printf hi; exit 0")).id;
defer jobs.jobsReap(gpa, io);
const waited = try jobs.waitForeground(gpa, io, id, 2_000);
defer switch (waited) {
.running => |r| gpa.free(r.output),
.done => |d| gpa.free(d.output),
.cancelled => |c| gpa.free(c.output),
};
try std.testing.expect(waited == .done);
try std.testing.expectEqual(@as(?u8, 0), waited.done.exit_code);
try std.testing.expect(std.mem.indexOf(u8, waited.done.output, "hi") != null);
}
test "#199: a silent, unread job is stopped after the idle budget and can be restarted" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
jobs.g_jobs = .{};
const gpa = std.testing.allocator;
const io = std.testing.io;
const saved = job_idle.policy;
defer job_idle.policy = saved;
job_idle.policy = .{ .warn_ms = 100, .stop_ms = 400 };
const id = (try jobs.spawnJob(gpa, io, "sleep 30")).id;
defer jobs.jobsReap(gpa, io);
io.sleep(.fromMilliseconds(1_500), .awake) catch {};
const snap = try jobs.jobOutput(gpa, io, id, 0);
defer gpa.free(snap.text);
try std.testing.expect(std.mem.indexOf(u8, snap.text, "stopped after") != null);
try std.testing.expect(std.mem.indexOf(u8, snap.text, "/jobs keep") != null);
// The command survives: a restart is a new job, running, in the same place.
const again = try jobs.restartJob(gpa, io, id);
try std.testing.expect(again.id != id);
const snap2 = try jobs.jobOutput(gpa, io, again.id, 0);
defer gpa.free(snap2.text);
try std.testing.expect(std.mem.indexOf(u8, snap2.text, "running") != null);
try std.testing.expectError(error.StillRunning, jobs.restartJob(gpa, io, again.id));
try std.testing.expectError(error.NoSuchJob, jobs.restartJob(gpa, io, 9999));
}
test "#199: output keeps a job alive past the budget; a pinned job is never idle-stopped" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
jobs.g_jobs = .{};
const gpa = std.testing.allocator;
const io = std.testing.io;
const saved = job_idle.policy;
defer job_idle.policy = saved;
job_idle.policy = .{ .warn_ms = 100, .stop_ms = 300 };
const chatty = (try jobs.spawnJob(gpa, io, "i=0; while [ $i -lt 15 ]; do echo tick; sleep 0.1; i=$((i+1)); done")).id;
const pinned = (try jobs.spawnJob(gpa, io, "sleep 4")).id;
defer jobs.jobsReap(gpa, io);
try std.testing.expectEqual(@as(?bool, true), jobs.setPinned(io, pinned, true));
try std.testing.expectEqual(@as(?bool, null), jobs.setPinned(io, 9999, true));
io.sleep(.fromMilliseconds(1_000), .awake) catch {};
for ([_]u64{ chatty, pinned }) |id| {
const snap = try jobs.jobOutput(gpa, io, id, 0);
defer gpa.free(snap.text);
try std.testing.expect(std.mem.indexOf(u8, snap.text, "running") != null);
}
_ = jobs.setPinned(io, pinned, false); // so the reap below kills it instead of retaining it
}
test "#199: a spawn writes an ownership record with the leader's identity; reaping removes it" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
jobs.g_jobs = .{};
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var arena_state = std.heap.ArenaAllocator.init(gpa);
defer arena_state.deinit();
const arena = arena_state.allocator();
var buf: [std.fs.max_path_bytes]u8 = undefined;
const n = try tmp.dir.realPath(io, &buf);
const saved_home = job_registry.home;
defer job_registry.home = saved_home;
job_registry.home = buf[0..n];
const job = try jobs.spawnJob(gpa, io, "sleep 5");
const pid = job.child.id.?;
const recs = job_registry.list(io, arena, job_registry.home);
try std.testing.expectEqual(@as(usize, 1), recs.len);
try std.testing.expectEqual(pid, recs[0].pid);
try std.testing.expectEqualStrings("sleep 5", recs[0].cmd);
try std.testing.expectEqual(proc_identity.selfPid(), recs[0].owner_pid);
try std.testing.expect(job_registry.ownerAlive(io, recs[0]));
try std.testing.expectEqual(job_registry.State.running, job_registry.state(io, recs[0]));
jobs.jobsReap(gpa, io);
try std.testing.expectEqual(@as(usize, 0), job_registry.list(io, arena, job_registry.home).len);
}
test "#199: a pinned job is retained at session end — record kept, tree alive, then stoppable by pid" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
jobs.g_jobs = .{};
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var arena_state = std.heap.ArenaAllocator.init(gpa);
defer arena_state.deinit();
const arena = arena_state.allocator();
var buf: [std.fs.max_path_bytes]u8 = undefined;
const n = try tmp.dir.realPath(io, &buf);
const saved_home = job_registry.home;
defer job_registry.home = saved_home;
job_registry.home = buf[0..n];
const job = try jobs.spawnJob(gpa, io, "sleep 30");
const pid = job.child.id.?;
try std.testing.expectEqual(@as(?bool, true), jobs.setPinned(io, job.id, true));
jobs.jobsReap(gpa, io); // session end: pinned → retained, not killed
const recs = job_registry.list(io, arena, job_registry.home);
try std.testing.expectEqual(@as(usize, 1), recs.len);
try std.testing.expect(recs[0].retained);
try std.testing.expect(recs[0].pinned);
try std.testing.expectEqualStrings("user_pin", @tagName(recs[0].retention_reason.?));
try std.testing.expectEqual(pid, recs[0].pid);
try std.testing.expectEqual(job_registry.State.running, job_registry.state(io, recs[0]));
// A later `graff servers stop <pid>`: verified, then the whole group goes.
try std.testing.expectEqual(job_registry.StopResult.stopped, job_registry.stopTree(io, recs[0]));
job_registry.forget(io, job_registry.home, pid);
try std.testing.expectEqual(@as(usize, 0), job_registry.list(io, arena, job_registry.home).len);
}
test "#1522 a queued follow-up ends a blocking output wait and leaves the job running" {
if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
const Agent = @import("agent.zig").Agent;
const job_wait = @import("job_wait.zig");
const io = std.testing.io;
const gpa = std.testing.allocator;
jobs.g_jobs = .{};
Agent.esc_cancel.store(false, .release);
defer job_wait.followup_pending.store(false, .release);
defer jobs.jobsReap(gpa, io);
const id = (try jobs.spawnJob(gpa, io, "sleep 20")).id;
job_wait.noteFollowup();
const started = jobs.nowMs(io);
const output = try jobs.jobOutput(gpa, io, id, 3_600_000);
defer gpa.free(output.text);
// Without the yield this blocks until the job exits.
try std.testing.expect(jobs.nowMs(io) - started < 10_000);
try std.testing.expect(output.pending);
try std.testing.expect(!output.is_error);
try std.testing.expect(std.mem.indexOf(u8, output.text, "interrupted") != null);
jobs.g_jobs.mutex.lockUncancelable(io);
defer jobs.g_jobs.mutex.unlock(io);
const job = jobs.g_jobs.find(id) orelse return error.JobLost;
try std.testing.expect(!job.done); // the wait yielded; the job was not stopped
}