Skip to content

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

Python

feat: async background subagents with completion events (#276)

Rach Pradhan96a2ca96e62b
diff --git a/src/commands_misc.zig b/src/commands_misc.zig
index 522c0606..ba177300 100644
--- a/src/commands_misc.zig
+++ b/src/commands_misc.zig
@@ -29,6 +29,7 @@ const ansi = @import("ansi.zig");
const style = &ansi.style;
const jobs = @import("jobs.zig");
+const subagent = @import("subagent.zig"); // #276 P0-3: g_agent_jobs, for /jobs
const pricing = @import("pricing.zig");
const default_context = pricing.default_context;
@@ -138,29 +139,53 @@ pub fn tryHandle(root: *Agent, keys: *Keys, arena: Allocator, line: []const u8,
}
if (std.mem.eql(u8, line, "/jobs")) {
jobs.g_jobs.mutex.lockUncancelable(root.io);
- defer jobs.g_jobs.mutex.unlock(root.io);
if (jobs.g_jobs.list.items.len == 0) {
- try out.writeAll("no background jobs — the model starts one with bash {run_in_background: true}\n");
- try out.flush();
- return true;
+ try out.writeAll("no background bash jobs — the model starts one with bash {run_in_background: true}\n");
+ } else {
+ try out.print("{s}background jobs{s}\n", .{ style.bold, style.reset });
+ for (jobs.g_jobs.list.items) |job| {
+ var sbuf: [32]u8 = undefined;
+ const status: []const u8 = if (!job.done)
+ "running"
+ else if (job.killed)
+ "killed"
+ else if (job.exit_code) |c|
+ (std.fmt.bufPrint(&sbuf, "exit {d}", .{c}) catch "exited")
+ else
+ "abnormal";
+ try out.print(" {s}{d:>3}{s} {s}{s:<8}{s} {d:>7} unread B {s}\n", .{
+ style.accent, job.id, style.reset,
+ if (job.done) style.dim else style.green, status, style.reset,
+ job.buf.items.len - job.cursor, utf8Prefix(job.cmd, 60),
+ });
+ }
}
- try out.print("{s}background jobs{s}\n", .{ style.bold, style.reset });
- for (jobs.g_jobs.list.items) |job| {
- var sbuf: [32]u8 = undefined;
- const status: []const u8 = if (!job.done)
- "running"
- else if (job.killed)
- "killed"
- else if (job.exit_code) |c|
- (std.fmt.bufPrint(&sbuf, "exit {d}", .{c}) catch "exited")
- else
- "abnormal";
- try out.print(" {s}{d:>3}{s} {s}{s:<8}{s} {d:>7} unread B {s}\n", .{
- style.accent, job.id, style.reset,
- if (job.done) style.dim else style.green, status, style.reset,
- job.buf.items.len - job.cursor, utf8Prefix(job.cmd, 60),
- });
+ jobs.g_jobs.mutex.unlock(root.io);
+
+ // #276 P0-3: background subagents (subagent {run_in_background:true}),
+ // same listing shape as bash jobs above.
+ subagent.g_agent_jobs.mutex.lockUncancelable(root.io);
+ if (subagent.g_agent_jobs.list.items.len == 0) {
+ try out.writeAll("no background agents — the model starts one with subagent {run_in_background: true}\n");
+ } else {
+ try out.print("{s}background agents{s}\n", .{ style.bold, style.reset });
+ for (subagent.g_agent_jobs.list.items) |job| {
+ const status: []const u8 = if (!job.admitted)
+ "queued"
+ else if (!job.done)
+ "running"
+ else if (job.is_error)
+ "failed"
+ else
+ "done";
+ try out.print(" {s}{d:>3}{s} {s}{s:<8}{s} {d:>7}ms {s}\n", .{
+ style.accent, job.id, style.reset,
+ if (job.done) style.dim else style.green, status, style.reset,
+ job.usage.duration_ms, utf8Prefix(job.label, 60),
+ });
+ }
}
+ subagent.g_agent_jobs.mutex.unlock(root.io);
try out.flush();
return true;
}
diff --git a/src/exec.zig b/src/exec.zig
index 161553d2..5698cd73 100644
--- a/src/exec.zig
+++ b/src/exec.zig
@@ -37,6 +37,7 @@ const codedb_result_cap = tools.codedb_result_cap;
const subagent = @import("subagent.zig");
const execSubagent = subagent.execSubagent;
+const agentOutput = subagent.agentOutput; // #276 P0-3
const workflow = @import("workflow.zig");
const execWorkflow = workflow.execWorkflow;
@@ -449,6 +450,12 @@ fn execToolInner(ctx: ToolCtx, call: ToolCall) !ToolOutput {
}
if (std.mem.eql(u8, call.name, "subagent")) return execSubagent(ctx, input);
if (std.mem.eql(u8, call.name, "workflow")) return execWorkflow(ctx, input);
+ if (std.mem.eql(u8, call.name, "agent_output")) {
+ const id = intField(input, "id") orelse return missingArg(gpa, "id");
+ const wait_ms = intField(input, "wait_ms") orelse 0;
+ if (id < 0 or id > std.math.maxInt(u32)) return .{ .text = try gpa.dupe(u8, "invalid agent id"), .is_error = true };
+ return agentOutput(gpa, io, @intCast(id), @intCast(@max(wait_ms, 0)));
+ }
return .{ .text = try std.fmt.allocPrint(gpa, "unknown tool: {s}", .{call.name}), .is_error = true };
}
diff --git a/src/main.zig b/src/main.zig
index d1c2d0f9..790c438a 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -443,6 +443,11 @@ pub fn main(init: std.process.Init) !void {
// A later guarded defer moves normal reaping ahead of terminal upload.
var jobs_reaped = false;
defer if (!jobs_reaped) jobsReap(gpa, io);
+ // Background subagents (#276 P0-3) die with the session too, though not
+ // killed the same way — see agentJobsReap's doc comment for why. Mirrors
+ // jobsReap's early-fallback/later-guarded-defer pattern immediately above.
+ var agent_jobs_reaped = false;
+ defer if (!agent_jobs_reaped) agentJobsReap(gpa, io);
// Root Agent construction + post-construction config (session name, persisted thinking/goal/eval settings, session-start trace note) + the
// backgrounded fleet-champion pull live in session_start.zig. `root`'s pointer fields (snapshots/client/tracer/approvals/registry) all reference
// already-stable main()-owned storage passed in by address, so returning the constructed Agent by value here is safe.
@@ -473,13 +478,18 @@ pub fn main(init: std.process.Init) !void {
// first; finish() is idempotent and keeps that status terminal.
errdefer behavior_boot.behavior.finish(.failed);
- // LIFO teardown: joinElites, then background bash pumps, then emit/send
- // run_finished, then close/deinit the two behavioral sinks. The earlier
- // fallback remains for failures that occur before this defer is registered.
+ // LIFO teardown: joinElites, then background bash + subagent pumps, then
+ // emit/send run_finished, then close/deinit the two behavioral sinks. The
+ // earlier fallbacks remain for failures that occur before these defers
+ // are registered.
defer if (!jobs_reaped) {
jobsReap(gpa, io);
jobs_reaped = true;
};
+ defer if (!agent_jobs_reaped) {
+ agentJobsReap(gpa, io);
+ agent_jobs_reaped = true;
+ };
defer joinElites(io); // reap if the session quits before any turn joins it
session_run.saveOrResumeSession(&root, &keys, arena, flags);
@@ -590,6 +600,7 @@ const jobsReap = jobs.jobsReap;
const tools_mod = @import("tools.zig");
const Snapshots = tools_mod.Snapshots;
const subagent = @import("subagent.zig");
+const agentJobsReap = subagent.agentJobsReap; // #276 P0-3: background subagents die with the session, mirroring jobsReap
const workflow = @import("workflow.zig");
const exec = @import("exec.zig");
// ── Unit tests (`zig build test`) ──────────────────────────────────────────
diff --git a/src/schema.zig b/src/schema.zig
index 7039ba4e..f3834d9b 100644
--- a/src/schema.zig
+++ b/src/schema.zig
@@ -204,9 +204,17 @@ const meta_specs = [_]ToolSpec{
const subagent_spec = ToolSpec{
.name = "subagent",
- .desc = "Delegate a self-contained task to a subagent. It has bash, read_file, edit_file, write_file, and codedb (code search); it cannot spawn further subagents and does NOT share your context — the prompt must be self-contained. Returns the subagent final report. Call several times in one response to run tasks in parallel. Pick a persona with agent (builtins: reviewer, researcher, implementer, skeptic — plus any in .harness/agents/), or give the child a custom system_prompt; the trajectory log records the lineage either way.",
+ .desc = "Delegate a self-contained task to a subagent. It has bash, read_file, edit_file, write_file, and codedb (code search); it cannot spawn further subagents and does NOT share your context — the prompt must be self-contained. Returns the subagent final report. Call several times in one response to run tasks in parallel. Pick a persona with agent (builtins: reviewer, researcher, implementer, skeptic — plus any in .harness/agents/), or give the child a custom system_prompt; the trajectory log records the lineage either way. Set run_in_background true to launch it asynchronously instead: you get the agent id back immediately and keep working, then fetch the result with agent_output.",
.schema =
- \\{"type": "object", "properties": {"description": {"type": "string", "description": "Short label for logs, 3-5 words"}, "prompt": {"type": "string", "description": "Complete, self-contained task description"}, "agent": {"type": "string", "description": "Optional: a named agent type (reviewer, researcher, implementer, skeptic, or a .harness/agents/ name) whose persona the child runs with"}, "system_prompt": {"type": "string", "description": "Optional: replace the child's system prompt with a custom one (overrides agent)"}, "isolation": {"type": "string", "enum": ["shared_cwd", "worktree"], "description": "Optional: \"worktree\" gives this child its own scratch git worktree (auto-removed if it finishes with no changes, kept + reported if it has any) instead of the shared working tree — use for parallel agents that edit files. Default shared_cwd (today's behavior), unless the chosen agent persona sets its own default."}, "isolation_fallback": {"type": "boolean", "description": "Optional: if isolation:\"worktree\" fails to set up (not a git repo, git error), run in the shared working tree instead of failing the spawn. Default false (fails the spawn on setup failure)."}}, "required": ["description", "prompt"]}
+ \\{"type": "object", "properties": {"description": {"type": "string", "description": "Short label for logs, 3-5 words"}, "prompt": {"type": "string", "description": "Complete, self-contained task description"}, "agent": {"type": "string", "description": "Optional: a named agent type (reviewer, researcher, implementer, skeptic, or a .harness/agents/ name) whose persona the child runs with"}, "system_prompt": {"type": "string", "description": "Optional: replace the child's system prompt with a custom one (overrides agent)"}, "isolation": {"type": "string", "enum": ["shared_cwd", "worktree"], "description": "Optional: \"worktree\" gives this child its own scratch git worktree (auto-removed if it finishes with no changes, kept + reported if it has any) instead of the shared working tree — use for parallel agents that edit files. Default shared_cwd (today's behavior), unless the chosen agent persona sets its own default."}, "isolation_fallback": {"type": "boolean", "description": "Optional: if isolation:\"worktree\" fails to set up (not a git repo, git error), run in the shared working tree instead of failing the spawn. Default false (fails the spawn on setup failure)."}, "run_in_background": {"type": "boolean", "description": "Optional: start this subagent asynchronously and return its agent id immediately instead of waiting for it to finish (default false). Poll status/result with agent_output (id, optional wait_ms). A spawn beyond the concurrency cap is queued, never failed."}}, "required": ["description", "prompt"]}
+ ,
+};
+
+const agent_output_spec = ToolSpec{
+ .name = "agent_output",
+ .desc = "Fetch a background subagent's status/result (one started with subagent run_in_background:true). Running: a brief status line. Completed: the full report — the same text a synchronous subagent call would have returned, including any kept-worktree note — plus a usage summary (duration, tool-call count, context tokens, cache-read tokens). Failed: an is_error result explaining why; failures are always reported, never silent. Set wait_ms to block while still running. Calling again after completion re-returns the same result — nothing is consumed.",
+ .schema =
+ \\{"type": "object", "properties": {"id": {"type": "integer", "description": "Agent id returned by subagent with run_in_background:true"}, "wait_ms": {"type": "integer", "description": "Max milliseconds to wait while still running (0-30000, default 0)"}}, "required": ["id"]}
,
};
@@ -226,7 +234,7 @@ const learn_candidate_spec = ToolSpec{
,
};
-pub const root_specs = base_specs ++ meta_specs ++ [_]ToolSpec{ subagent_spec, workflow_spec, learn_candidate_spec };
+pub const root_specs = base_specs ++ meta_specs ++ [_]ToolSpec{ subagent_spec, workflow_spec, agent_output_spec, learn_candidate_spec };
// The common catalog (clock_sleep off) is static too. Previously every root
// startup allocated and filled a filtered ToolSpec array before rendering even
@@ -403,7 +411,7 @@ fn writeToolEntry(s: *std.json.Stringify, kind: Provider.Kind, name: []const u8,
/// a per-commit git describe) — bump this only when the schema or JSONL
/// protocol changes shape, so SDK regeneration stays byte-stable across
/// commits.
-pub const schema_version = "0.7"; // #276 P0-1: subagent/workflow-task schemas gained isolation/isolation_fallback
+pub const schema_version = "0.8"; // #276 P0-3: subagent gained run_in_background; new agent_output tool
/// Emit the machine-readable interface description for `harness --schema`:
/// providers, models, built-in tools (name/description/parameters), and the
@@ -598,3 +606,26 @@ test "providerTakesEffort: effort-honoring providers, but never for grok models"
// grok via the codegraff gateway must NOT get reasoning_effort (grok rejects it)
try std.testing.expect(!providerTakesEffort(.openai, "codegraff", "grok-build"));
}
+test "agent_output: root-only (subagent/workflow's run_in_background has nothing to poll from inside a child), never doubles as a meta tool" {
+ var found_root = false;
+ for (root_specs) |t| if (std.mem.eql(u8, t.name, "agent_output")) {
+ found_root = true;
+ };
+ try std.testing.expect(found_root);
+ try std.testing.expect(!isMetaName("agent_output")); // dispatched like bash_output, not handled inline
+
+ // Subagents can't spawn subagents (execSubagent's from_sub gate), so they
+ // can never mint an agent id to poll — keep it out of their own catalog
+ // (tools_anthropic_sub is built from base_specs only).
+ try std.testing.expect(std.mem.indexOf(u8, tools_anthropic_sub, "agent_output") == null);
+ try std.testing.expect(std.mem.indexOf(u8, tools_anthropic_sub, "subagent") == null);
+}
+
+test "subagent_spec: run_in_background is a boolean, optional (not in required), valid JSON alongside the rest of the schema" {
+ try std.testing.expect(std.mem.indexOf(u8, subagent_spec.schema, "\"run_in_background\": {\"type\": \"boolean\"") != null);
+ var parsed = try std.json.parseFromSlice(std.json.Value, std.testing.allocator, subagent_spec.schema, .{});
+ defer parsed.deinit();
+ const required = parsed.value.object.get("required").?.array.items;
+ for (required) |r| try std.testing.expect(!std.mem.eql(u8, r.string, "run_in_background"));
+}
+
diff --git a/src/subagent.zig b/src/subagent.zig
index 0e72832d..41fa98aa 100644
--- a/src/subagent.zig
+++ b/src/subagent.zig
@@ -36,6 +36,7 @@ const learning_privacy = @import("learning_privacy.zig");
const agentTypePrompt = fleet.agentTypePrompt;
const Isolation = fleet.Isolation;
const jobs = @import("jobs.zig"); // #276 P0-1: agentWorktreeCreate/agentWorktreeFinish/isolationFailureText
+const run_budget = @import("run_budget.zig"); // #276 P0-3: default_max_concurrency, reused as the background-agent cap's default value
const cards = @import("cards.zig");
const trace = @import("trace.zig");
const messages_mod = @import("messages.zig");
@@ -43,7 +44,9 @@ const textMessage = messages_mod.textMessage;
/// Spawn a one-level-deep subagent: fresh arena, fresh history, same shared
/// http client and provider. Runs entirely on this pool thread; its own tool
-/// calls fan out further via io.async.
+/// calls fan out further via io.async. `run_in_background:true` (#276 P0-3)
+/// instead registers the spawn and returns immediately — see
+/// spawnSubBackground below.
pub fn execSubagent(ctx: ToolCtx, input: Value) !ToolOutput {
if (ctx.from_sub) return .{
.text = try ctx.gpa.dupe(u8, "subagents cannot spawn subagents — do this work yourself"),
@@ -53,9 +56,16 @@ pub fn execSubagent(ctx: ToolCtx, input: Value) !ToolOutput {
const prompt = if (input.object.get("prompt")) |p| (if (p == .string) p.string else "") else "";
if (prompt.len == 0) return .{ .text = try ctx.gpa.dupe(u8, "subagent: missing required \"prompt\" (a self-contained task)"), .is_error = true };
const sys_override = fleet.resolveOverride(input.object);
- return runSub(ctx, "subagent", label, prompt, sys_override, fleet.resolveNiche(input.object), fleet.resolveIsolation(input.object), fleet.resolveIsolationFallback(input.object));
+ const niche = fleet.resolveNiche(input.object);
+ const isolation = fleet.resolveIsolation(input.object);
+ const isolation_fallback = fleet.resolveIsolationFallback(input.object);
+ const background = if (input.object.get("run_in_background")) |v| v == .bool and v.bool else false;
+ if (background) return spawnSubBackground(ctx, label, prompt, sys_override, niche, isolation, isolation_fallback);
+ const run = try runSub(ctx, "subagent", label, prompt, sys_override, niche, isolation, isolation_fallback);
+ return run.output;
}
+
/// Surface a workflow subagent as a synthetic `tool_call` / `tool_result` on the
/// --json GUI stream so each parallel worker shows as its own live row — the
/// orchestrator's children otherwise run detached (out=null), so the GUI only
@@ -166,17 +176,39 @@ test "child model pin crosses the current root provider only with consent" {
try std.testing.expectEqualStrings("gpt-5.6-terra", childProvider(anthropic_root, terra, true).model);
}
+/// Duration/tool-call/context usage for one subagent run — the numbers
+/// runSub already computes for the trajectory node, now also handed back to
+/// the caller so a background completion event (#276 P0-3) can carry them.
+/// Zero-valued when the child never ran at all (an early rejection before
+/// `agent.runTurn()`, e.g. depth limit or isolation setup failure).
+pub const AgentUsage = struct {
+ duration_ms: u64 = 0,
+ tool_calls: u32 = 0,
+ context_tokens: u64 = 0,
+ cache_read_tokens: u64 = 0,
+};
+
+/// runSub's result: the tool-facing output plus the usage summary above.
+/// Every direct caller (execSubagent, workflowTask, workflowRetryTask,
+/// judgeTask, workflow.zig's pipelineChain) only ever wanted `.output`
+/// before #276 P0-3; `spawnSubBackground`'s pump is the one caller that
+/// also reads `.usage`.
+pub const SubRun = struct { output: ToolOutput, usage: AgentUsage };
+
/// Run one isolated subagent to completion: fresh arena, fresh history,
/// same shared http client and the configured child provider (or the root
/// provider when no child model is pinned). `sys_override` swaps the
/// lean default system prompt for a per-child variant (swarm prompt
/// evolution); either way the run is recorded as a trajectory node under
/// the turn that spawned it, with the prompt's fingerprint.
-pub fn runSub(ctx: ToolCtx, kind: []const u8, label: []const u8, prompt: []const u8, sys_override: ?[]const u8, niche: []const u8, isolation: Isolation, isolation_fallback: bool) !ToolOutput {
+pub fn runSub(ctx: ToolCtx, kind: []const u8, label: []const u8, prompt: []const u8, sys_override: ?[]const u8, niche: []const u8, isolation: Isolation, isolation_fallback: bool) !SubRun {
const gpa = ctx.gpa;
if (ctx.run_budget) |budget| if (ctx.depth >= budget.max_depth) return .{
- .text = try gpa.dupe(u8, "agent depth limit reached (max depth 1) — do this work in the current agent"),
- .is_error = true,
+ .output = .{
+ .text = try gpa.dupe(u8, "agent depth limit reached (max depth 1) — do this work in the current agent"),
+ .is_error = true,
+ },
+ .usage = .{},
};
var arena_state = std.heap.ArenaAllocator.init(gpa);
defer arena_state.deinit();
@@ -245,7 +277,10 @@ pub fn runSub(ctx: ToolCtx, kind: []const u8, label: []const u8, prompt: []const
if (jobs.agentWorktreeCreate(gpa, ctx.io, arena, sub_id)) |created| {
wt = created;
} else |err| {
- if (!isolation_fallback) return .{ .text = try gpa.dupe(u8, jobs.isolationFailureText(err)), .is_error = true };
+ if (!isolation_fallback) return .{
+ .output = .{ .text = try gpa.dupe(u8, jobs.isolationFailureText(err)), .is_error = true },
+ .usage = .{},
+ };
isolation_note = "\n\n[note: isolation:\"worktree\" failed and fell back to the shared working tree — isolation_fallback:true allowed it]";
}
}
@@ -261,6 +296,17 @@ pub fn runSub(ctx: ToolCtx, kind: []const u8, label: []const u8, prompt: []const
const run_ok = if (report) |r| r.len > 0 else |_| false;
if (wf_task) guiEmit(ctx.io, .{ .type = "tool_result", .name = "subagent", .is_error = !run_ok });
const used_tools = agent.tools_used.render(arena);
+ // #276 P0-3: usage summary for a background completion event, captured
+ // once here (right after the child's turn tree is fully joined) and
+ // threaded through every remaining return point below, success or
+ // failure alike — the two early returns above happen before the child
+ // ever runs, so they carry the zero value instead.
+ const usage: AgentUsage = .{
+ .duration_ms = @intCast(run_ms),
+ .tool_calls = @intCast(agent.tools_used.count()),
+ .context_tokens = agent.effectiveContextTokens(),
+ .cache_read_tokens = agent.last_cache_read,
+ };
const fp = promptFingerprint(agent.systemPrompt());
if (trace.g_traj) |tj| {
tj.capturePrompt(fp, agent.systemPrompt());
@@ -294,11 +340,11 @@ pub fn runSub(ctx: ToolCtx, kind: []const u8, label: []const u8, prompt: []const
// from "changes we're about to lose" once the run failed abnormally.
var out = subagentFailure(gpa, sub_id, err, agent.last_api_error);
if (wt) |w| {
- const combined = std.fmt.allocPrint(gpa, "{s}\n\n[worktree left in place after failure — path: {s}, branch: {s}]", .{ out.text, w.path, w.branch }) catch return out;
+ const combined = std.fmt.allocPrint(gpa, "{s}\n\n[worktree left in place after failure — path: {s}, branch: {s}]", .{ out.text, w.path, w.branch }) catch return .{ .output = out, .usage = usage };
gpa.free(out.text);
out.text = combined;
}
- return out;
+ return .{ .output = out, .usage = usage };
};
const empty = text.len == 0;
const report_body = if (empty) "subagent finished without a report" else text;
@@ -322,24 +368,352 @@ pub fn runSub(ctx: ToolCtx, kind: []const u8, label: []const u8, prompt: []const
}
defer if (extra_owned) gpa.free(extra);
- if (empty) return .{ .text = try std.fmt.allocPrint(gpa, "{s}{s}", .{ report_body, extra }), .is_error = true };
+ if (empty) return .{
+ .output = .{ .text = try std.fmt.allocPrint(gpa, "{s}{s}", .{ report_body, extra }), .is_error = true },
+ .usage = usage,
+ };
// Append the inspect: path so the orchestrator can cite the detail file.
if (detail) |p|
- return .{ .text = try std.fmt.allocPrint(gpa, "{s}\n\n[subagent {s} · inspect: {s}]{s}", .{ text, sub_id, p, extra }) };
- return .{ .text = try std.fmt.allocPrint(gpa, "{s}{s}", .{ text, extra }) };
+ return .{
+ .output = .{ .text = try std.fmt.allocPrint(gpa, "{s}\n\n[subagent {s} · inspect: {s}]{s}", .{ text, sub_id, p, extra }) },
+ .usage = usage,
+ };
+ return .{ .output = .{ .text = try std.fmt.allocPrint(gpa, "{s}{s}", .{ text, extra }) }, .usage = usage };
+}
+
+// ── Async, fire-and-forget background subagents (#276 P0-3) ────────────────
+// Extends jobs.zig's background-bash-job pattern (Job/g_jobs/spawnJob/
+// jobOutput/jobKill — stable incrementing ids, a session-global mutex-guarded
+// registry, non-blocking spawn) to subagent spawns: `run_in_background:true`
+// on `subagent` returns a stable agent id immediately (spawnSubBackground);
+// the child runs on io.concurrent, never io.async — io.async may run inline
+// and block the spawning call forever on a long-lived child, same reason
+// spawnJob avoids it for bash. Completion is polled non-destructively via
+// the new `agent_output` tool (agentOutput), mirroring bash_output's
+// id/wait_ms shape and its 30s wait cap — except a subagent's report is a
+// one-shot value, not a stream, so a completed fetch always replays the
+// FULL result rather than consuming a cursor (design point 4).
+
+/// Concurrency ceiling on background subagents actually RUNNING (i.e. past
+/// admission, inside their own runSub call) at once. Deliberately a counter
+/// of its own, not a reuse of RunBudget.active/acquireConcurrency: a
+/// background job's admission slot would otherwise have to stay held for its
+/// entire — possibly minutes-long — lifetime, while its OWN internal turns
+/// separately acquire per-model-call permits from that very same pool; once
+/// enough jobs queued up, every held outer slot would starve the inner
+/// acquires each job needs to ever finish and release it — a deadlock. Same
+/// admission SHAPE as RunBudget.acquireConcurrency (spin-wait, no failure on
+/// saturation) and the same default value, but an independent resource.
+const max_concurrent_background_agents: u32 = run_budget.default_max_concurrency;
+
+/// One background subagent spawn: the runSub arguments (gpa-owned copies —
+/// the caller's ToolCtx.arena-backed strings do not outlive this tool call's
+/// return, the whole point of fire-and-forget) plus its outcome once done.
+/// `admitted` is false while queued behind max_concurrent_background_agents;
+/// jobs.zig's Job mirrors this shape closely (id/cmd there ~ id/label+prompt
+/// here).
+const AgentJob = struct {
+ id: u32,
+ label: []u8,
+ prompt: []u8,
+ sys_override: ?[]u8 = null,
+ niche: []u8,
+ isolation: Isolation,
+ isolation_fallback: bool,
+ ctx: ToolCtx,
+ admitted: bool = false,
+ done: bool = false,
+ is_error: bool = false,
+ result: []u8 = &.{}, // gpa-owned once done
+ usage: AgentUsage = .{},
+ future: Io.Future(void) = undefined, // pump; awaited only by agentJobsReap
+};
+
+const AgentJobs = struct {
+ mutex: Io.Mutex = .init,
+ list: std.ArrayList(*AgentJob) = .empty,
+ active: u32 = 0, // admitted and not yet done
+ next_id: u32 = 1,
+
+ fn find(self: *AgentJobs, id: u32) ?*AgentJob {
+ for (self.list.items) |j| if (j.id == id) return j;
+ return null;
+ }
+};
+
+pub var g_agent_jobs: AgentJobs = .{};
+
+/// Pure admission step: pop the oldest not-yet-admitted job and mark it
+/// admitted (bumping `active`), or return null when the cap is full or
+/// nothing is queued. No Io — callers hold AgentJobs.mutex around it; kept
+/// separate so the queue-not-fail contract (design point 6) is unit-testable
+/// without a real Io/thread pool.
+fn admitOneLocked(registry: *AgentJobs) ?*AgentJob {
+ if (registry.active >= max_concurrent_background_agents) return null;
+ for (registry.list.items) |j| {
+ if (!j.admitted) {
+ j.admitted = true;
+ registry.active += 1;
+ return j;
+ }
+ }
+ return null;
+}
+
+/// Drain admittable jobs, launching each via io.concurrent. Called right
+/// after a spawn (start immediately if there's room) and at the end of
+/// every job's pump (drain the next queued one) — a small self-perpetuating
+/// worker chain, no separate ticker task needed. Never called with the
+/// mutex held.
+fn admitNext(gpa: Allocator, io: Io) void {
+ while (true) {
+ g_agent_jobs.mutex.lockUncancelable(io);
+ const job = admitOneLocked(&g_agent_jobs);
+ g_agent_jobs.mutex.unlock(io);
+ const j = job orelse return;
+ j.future = io.concurrent(agentJobPump, .{ j, gpa, io }) catch {
+ // Pool has no spare concurrency right now — un-admit and stop;
+ // the next spawn or completion retries (#276 P0-3 design point
+ // 6: queue rather than fail — never surfaced to the model).
+ g_agent_jobs.mutex.lockUncancelable(io);
+ j.admitted = false;
+ g_agent_jobs.active -= 1;
+ g_agent_jobs.mutex.unlock(io);
+ return;
+ };
+ }
+}
+
+/// The background pump: runs the child to completion off the calling turn,
+/// records the structured completion (status/result/usage — never silent,
+/// even on failure), then drains the next queued spawn. Mirrors jobPump's
+/// shape in jobs.zig.
+fn agentJobPump(job: *AgentJob, gpa: Allocator, io: Io) void {
+ const t0: Io.Timestamp = .now(io, .awake);
+ const run = runSub(job.ctx, "subagent", job.label, job.prompt, job.sys_override, job.niche, job.isolation, job.isolation_fallback) catch |err| SubRun{
+ .output = failure(gpa, err),
+ .usage = .{ .duration_ms = @intCast(@max(0, t0.untilNow(io, .awake).toMilliseconds())) },
+ };
+ g_agent_jobs.mutex.lockUncancelable(io);
+ job.result = run.output.text;
+ job.is_error = run.output.is_error;
+ job.usage = run.usage;
+ job.done = true;
+ g_agent_jobs.active -= 1;
+ g_agent_jobs.mutex.unlock(io);
+ admitNext(gpa, io);
+}
+
+/// `run_in_background:true` path for `subagent` (execSubagent): register the
+/// job and return immediately with its id; the child runs on the pool.
+/// Never blocks on a free concurrency slot — a spawn beyond the cap is
+/// queued, not failed; admitNext drains it once room frees up.
+fn spawnSubBackground(ctx: ToolCtx, label: []const u8, prompt: []const u8, sys_override: ?[]const u8, niche: []const u8, isolation: Isolation, isolation_fallback: bool) !ToolOutput {
+ const gpa = ctx.gpa;
+ const label_c = try gpa.dupe(u8, label);
+ errdefer gpa.free(label_c);
+ const prompt_c = try gpa.dupe(u8, prompt);
+ errdefer gpa.free(prompt_c);
+ const sys_c: ?[]u8 = if (sys_override) |s| try gpa.dupe(u8, s) else null;
+ errdefer if (sys_c) |s| gpa.free(s);
+ const niche_c = try gpa.dupe(u8, niche);
+ errdefer gpa.free(niche_c);
+
+ const job = try gpa.create(AgentJob);
+ job.* = .{
+ .id = 0,
+ .label = label_c,
+ .prompt = prompt_c,
+ .sys_override = sys_c,
+ .niche = niche_c,
+ .isolation = isolation,
+ .isolation_fallback = isolation_fallback,
+ .ctx = ctx,
+ };
+
+ g_agent_jobs.mutex.lockUncancelable(ctx.io);
+ job.id = g_agent_jobs.next_id;
+ g_agent_jobs.next_id += 1;
+ const appended = blk: {
+ g_agent_jobs.list.append(gpa, job) catch break :blk false;
+ break :blk true;
+ };
+ g_agent_jobs.mutex.unlock(ctx.io);
+ if (!appended) {
+ gpa.free(label_c);
+ gpa.free(prompt_c);
+ if (sys_c) |s| gpa.free(s);
+ gpa.free(niche_c);
+ gpa.destroy(job);
+ return error.OutOfMemory;
+ }
+ admitNext(gpa, ctx.io);
+ return .{ .text = try std.fmt.allocPrint(
+ gpa,
+ "[agent {d} started: {s}]\nIt runs in the background across turns. Poll status/result with agent_output (id {d}, optional wait_ms); once it completes, agent_output keeps returning the same result — nothing is consumed.",
+ .{ job.id, job.label, job.id },
+ ) };
+}
+
+/// Pure formatting for agent_output's status line: the "structured
+/// completion event" shape (#276 P0-3 design point 3) — running / completed
+/// / failed, plus the usage summary, plus the full result once done.
+/// Factored out of agentOutput so it's unit-testable without a real Io.
+fn agentStatusText(gpa: Allocator, id: u32, done: bool, is_error: bool, usage: AgentUsage, result: []const u8) ![]u8 {
+ if (!done) return std.fmt.allocPrint(gpa, "[agent {d}: running]", .{id});
+ return std.fmt.allocPrint(
+ gpa,
+ "[agent {d}: {s} in {d}ms · {d} tool call(s) · ~{d} context token(s) · {d} cache-read token(s)]\n\n{s}",
+ .{ id, if (is_error) "failed" else "completed", usage.duration_ms, usage.tool_calls, usage.context_tokens, usage.cache_read_tokens, result },
+ );
+}
+
+/// Same wait cap as jobs.zig's bash_output (job_wait_cap_ms) — kept as its
+/// own constant rather than made pub there, since it's one shared value, not
+/// shared state.
+const agent_wait_cap_ms: u64 = 30_000;
+
+/// agent_output: fetch a background subagent's status/result. Non-
+/// destructive — a completed fetch always replays the full result (unlike
+/// bash_output's cursor, a subagent's report is one-shot, not a stream).
+/// wait_ms > 0 polls (capped at 30s, like bash_output) while still running.
+pub fn agentOutput(gpa: Allocator, io: Io, id: u32, wait_ms: u64) !ToolOutput {
+ const deadline = @min(wait_ms, agent_wait_cap_ms);
+ var waited: u64 = 0;
+ while (true) {
+ g_agent_jobs.mutex.lockUncancelable(io);
+ const job = g_agent_jobs.find(id) orelse {
+ g_agent_jobs.mutex.unlock(io);
+ return .{ .text = try std.fmt.allocPrint(gpa, "no background agent {d} — it may never have started; subagent with run_in_background:true returns the id when it launches", .{id}), .is_error = true };
+ };
+ if (job.done or waited >= deadline) {
+ const text = try agentStatusText(gpa, id, job.done, job.is_error, job.usage, job.result);
+ const is_err = job.done and job.is_error;
+ g_agent_jobs.mutex.unlock(io);
+ return .{ .text = text, .is_error = is_err };
+ }
+ g_agent_jobs.mutex.unlock(io);
+ if (Agent.esc_cancel.load(.acquire)) {
+ waited = deadline; // render current state on the next pass
+ continue;
+ }
+ io.sleep(.fromMilliseconds(100), .awake) catch {
+ waited = deadline;
+ continue;
+ };
+ waited += 100;
+ }
+}
+
+/// Session end: let every still-running background agent finish naturally —
+/// unlike a bash child process (jobs.zig's jobsReap, which flags
+/// kill_requested and the pump kills the child within ~200ms), there is no
+/// cheap way to abort an in-flight agent turn mid-request, so this mirrors
+/// jobsReap's SHAPE (drain the registry, await every pump, free everything
+/// before the process exits) but not its KILL semantics — see this feature's
+/// commit body for the full rationale. A job still queued (never admitted)
+/// at shutdown never started, so it's simply dropped: nothing ran, nothing
+/// to lose.
+pub fn agentJobsReap(gpa: Allocator, io: Io) void {
+ g_agent_jobs.mutex.lockUncancelable(io);
+ const list = g_agent_jobs.list.toOwnedSlice(gpa) catch {
+ g_agent_jobs.mutex.unlock(io);
+ return;
+ };
+ g_agent_jobs.mutex.unlock(io);
+ for (list) |job| {
+ if (job.admitted) job.future.await(io);
+ gpa.free(job.label);
+ gpa.free(job.prompt);
+ if (job.sys_override) |s| gpa.free(s);
+ gpa.free(job.niche);
+ gpa.free(job.result);
+ gpa.destroy(job);
+ }
+ gpa.free(list);
+}
+
+test "admitOneLocked: admits up to the cap, queues the rest, FIFO order (#276 P0-3 design point 6)" {
+ const gpa = std.testing.allocator;
+ var registry: AgentJobs = .{};
+ defer registry.list.deinit(gpa);
+
+ var stub_jobs: [max_concurrent_background_agents + 3]AgentJob = undefined;
+ for (&stub_jobs, 0..) |*j, i| j.* = .{
+ .id = @intCast(i + 1),
+ .label = @constCast(""),
+ .prompt = @constCast(""),
+ .niche = @constCast(""),
+ .isolation = .shared_cwd,
+ .isolation_fallback = false,
+ .ctx = undefined,
+ };
+ for (&stub_jobs) |*j| try registry.list.append(gpa, j);
+
+ var admitted_order: [stub_jobs.len]u32 = undefined;
+ var n: usize = 0;
+ while (admitOneLocked(&registry)) |j| : (n += 1) admitted_order[n] = j.id;
+
+ try std.testing.expectEqual(@as(usize, max_concurrent_background_agents), n); // only the cap gets admitted immediately
+ try std.testing.expectEqual(max_concurrent_background_agents, registry.active);
+ for (admitted_order[0..n], 1..) |id, expect| try std.testing.expectEqual(@as(u32, @intCast(expect)), id); // FIFO
+
+ var still_queued: usize = 0;
+ for (stub_jobs) |j| if (!j.admitted) {
+ still_queued += 1;
+ };
+ try std.testing.expectEqual(stub_jobs.len - n, still_queued); // the remainder stay queued, not failed
+
+ // A finished job frees its slot; the next queued one is then admittable.
+ registry.active -= 1;
+ const next = admitOneLocked(&registry).?;
+ try std.testing.expectEqual(@as(u32, max_concurrent_background_agents + 1), next.id);
+}
+
+test "agentStatusText: running/completed/failed shapes carry the usage summary, and a failure is never silent (#276 P0-3)" {
+ const gpa = std.testing.allocator;
+
+ const running = try agentStatusText(gpa, 7, false, false, .{}, "");
+ defer gpa.free(running);
+ try std.testing.expectEqualStrings("[agent 7: running]", running);
+
+ const ok = try agentStatusText(gpa, 7, true, false, .{ .duration_ms = 1200, .tool_calls = 3, .context_tokens = 4500, .cache_read_tokens = 100 }, "final report");
+ defer gpa.free(ok);
+ try std.testing.expect(std.mem.indexOf(u8, ok, "completed") != null);
+ try std.testing.expect(std.mem.indexOf(u8, ok, "1200ms") != null);
+ try std.testing.expect(std.mem.indexOf(u8, ok, "3 tool call") != null);
+ try std.testing.expect(std.mem.indexOf(u8, ok, "final report") != null);
+
+ const failed_text = "subagent sa-014-abcd failed before producing a report: connection reset [transport failure]. retry is likely safe";
+ const failed = try agentStatusText(gpa, 9, true, true, .{ .duration_ms = 300 }, failed_text);
+ defer gpa.free(failed);
+ try std.testing.expect(std.mem.indexOf(u8, failed, "failed") != null); // status names the failure — never silent
+ try std.testing.expect(std.mem.indexOf(u8, failed, failed_text) != null); // the child's own diagnostic rides along verbatim
}
+test "agentStatusText: composes with isolation:\"worktree\" — a kept-worktree note in the result survives verbatim (#276 P0-3 design point 5)" {
+ const gpa = std.testing.allocator;
+ const result_with_worktree = "final report text\n\n[worktree kept (has changes) — path: .graff/worktrees/agent-sa-001-aa11, branch: graff/agents/sa-001-aa11]";
+ const out = try agentStatusText(gpa, 3, true, false, .{ .duration_ms = 500 }, result_with_worktree);
+ defer gpa.free(out);
+ try std.testing.expect(std.mem.indexOf(u8, out, "[worktree kept (has changes) — path:") != null);
+ try std.testing.expect(std.mem.indexOf(u8, out, "branch: graff/agents/sa-001-aa11") != null);
+}
+
+
/// One task inside a workflow phase; never throws, suitable for io.async.
/// `niche` is the task's MAP-Elites cell, threaded through so runSub's
/// fleet:propose — and scoreVariants' submit — tag the variant's genome.
pub fn workflowTask(ctx: ToolCtx, label: []const u8, prompt: []const u8, sys_override: ?[]const u8, niche: []const u8, isolation: Isolation, isolation_fallback: bool) ToolOutput {
- return runSub(ctx, "workflow_task", label, prompt, sys_override, niche, isolation, isolation_fallback) catch |err| failure(ctx.gpa, err);
+ const run = runSub(ctx, "workflow_task", label, prompt, sys_override, niche, isolation, isolation_fallback) catch |err| return failure(ctx.gpa, err);
+ return run.output;
}
/// A second workflow attempt has its own explicit budget kind. It still shares
/// the same invocation-wide atomic ceiling and concurrency limiter.
pub fn workflowRetryTask(ctx: ToolCtx, label: []const u8, prompt: []const u8, sys_override: ?[]const u8, niche: []const u8, isolation: Isolation, isolation_fallback: bool) ToolOutput {
- return runSub(ctx, "workflow_retry", label, prompt, sys_override, niche, isolation, isolation_fallback) catch |err| failure(ctx.gpa, err);
+ const run = runSub(ctx, "workflow_retry", label, prompt, sys_override, niche, isolation, isolation_fallback) catch |err| return failure(ctx.gpa, err);
+ return run.output;
}
/// The --judge LLM-as-judge run (see runEval in main): an isolated subagent
@@ -348,9 +722,11 @@ pub fn workflowRetryTask(ctx: ToolCtx, label: []const u8, prompt: []const u8, sy
/// on a pool thread. Never worktree-isolated: a judge only reads/reasons over
/// text handed to it, never the filesystem, so there's nothing to isolate.
pub fn judgeTask(ctx: ToolCtx, prompt: []const u8) ToolOutput {
- return runSub(ctx, "judge_task", "judge", prompt, null, "", .shared_cwd, false) catch |err| failure(ctx.gpa, err);
+ const run = runSub(ctx, "judge_task", "judge", prompt, null, "", .shared_cwd, false) catch |err| return failure(ctx.gpa, err);
+ return run.output;
}
+
/// Build the judge prompt that scores one workflow variant's output against its
/// task on a 0-100 scale (see scoreVariants). Bounded: the task spec and output
/// tail are truncated so a fat phase can't blow up the judge's context.
diff --git a/src/trace.zig b/src/trace.zig
index ce1651cd..544dcab5 100644
--- a/src/trace.zig
+++ b/src/trace.zig
@@ -415,6 +415,12 @@ pub const ToolSink = struct {
return total;
}
+ /// Number of recorded calls (successful + failed) — the tool_calls count
+ /// in a background subagent's completion usage summary (#276 P0-3).
+ pub fn count(self: *const ToolSink) u64 {
+ return self.entries.items.len;
+ }
+
pub fn clear(self: *ToolSink, io: Io) void {
self.mutex.lockUncancelable(io);
defer self.mutex.unlock(io);
@@ -426,6 +432,16 @@ pub const ToolSink = struct {
}
};
+test "ToolSink.count: total calls independent of render()'s joined-string shape, including failures (#276 P0-3)" {
+ var sink: ToolSink = .{};
+ defer sink.deinit(std.testing.allocator);
+ try std.testing.expectEqual(@as(u64, 0), sink.count());
+ sink.add(std.testing.io, std.testing.allocator, "read_file", false);
+ sink.add(std.testing.io, std.testing.allocator, "edit_file", true);
+ sink.add(std.testing.io, std.testing.allocator, "bash", false);
+ try std.testing.expectEqual(@as(u64, 3), sink.count());
+ try std.testing.expectEqual(@as(u64, 1), sink.errorCount()); // count() isn't just errorCount()
+}
test "writeJsonLine: one complete newline-terminated JSON record per call" {
var aw: Io.Writer.Allocating = .init(std.testing.allocator);
defer aw.deinit();
diff --git a/src/workflow.zig b/src/workflow.zig
index ee41b701..e24056a9 100644
--- a/src/workflow.zig
+++ b/src/workflow.zig
@@ -119,10 +119,10 @@ fn pipelineChain(ctx: ToolCtx, item: []const u8, stages: []const StageSpec) Tool
var prev: []const u8 = "";
for (stages, 1..) |st, stage_no| {
const prompt = pipelinePrompt(arena, st.prompt, item, prev, stage_no) catch |e| return failure(gpa, e);
- var out = runSub(ctx, "workflow_task", st.label, prompt, st.override, st.niche, st.isolation, st.isolation_fallback) catch |e| failure(gpa, e);
+ var out = if (runSub(ctx, "workflow_task", st.label, prompt, st.override, st.niche, st.isolation, st.isolation_fallback)) |r| r.output else |e| failure(gpa, e);
if (out.is_error) {
gpa.free(out.text);
- out = runSub(ctx, "workflow_retry", st.label, prompt, st.override, st.niche, st.isolation, st.isolation_fallback) catch |e| failure(gpa, e);
+ out = if (runSub(ctx, "workflow_retry", st.label, prompt, st.override, st.niche, st.isolation, st.isolation_fallback)) |r| r.output else |e| failure(gpa, e);
if (out.is_error) {
gpa.free(out.text);
return .{