graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
Park interactive shells and yield the parent REPL
justrach8a2bc2988740
diff --git a/docs/adr/0154-parked-shells-yield-the-parent.md b/docs/adr/0154-parked-shells-yield-the-parent.mdnew file mode 100644index 00000000..d6bbef91--- /dev/null+++ b/docs/adr/0154-parked-shells-yield-the-parent.md@@ -0,0 +1,33 @@+# 0154. Interactive parked shells yield the parent like subagents++Status: accepted 2026-09-21++## Context++Interactive `subagent` already backgrounds and yields the prompt (ADR 0118).+Root `shell` did not: `gh run watch` sat in the 120s foreground wait+(`· bash still running · 1m15s`), then the model polled `bash_output` with+`wait_ms` and the 15s pulse held the turn again. The user could not use the+REPL while CI ran.++ADR 0152 already makes parked jobs snapshot-only. The missing piece was+ending the parent turn so the model cannot immediately poll, and shortening+the interactive wait so a watch parks before a minute is gone.++## Decision++- Interactive root (REPL, TUI, GUI — `interactive_children`) uses the 15s+ foreground wait already used by lean `-p` (ADR 0055). Unattended non-lean+ stays 120s (ADR 0026).+- Parking a job — `run_in_background`, auto-promote, or follow-up promote —+ latches the same yield as ADR 0118. The next model request is replaced with+ a short "keep using the prompt" notice. Exit is an idle wake+ (`job_notify` / `takeIdleWake`). `shell action=output` remains a snapshot.+- Subagent bash still kills at 120s (#93).++## Consequences++`ls` and short tests still return inline. `gh run watch` parks by 15s, the+prompt comes back, and the harness revives when the run finishes. A 40s+`zig build` also parks; the wake carries the result. Models that still pass+`wait_ms: 30000` on a parked job get an immediate snapshot (ADR 0152).diff --git a/docs/adr/README.md b/docs/adr/README.mdindex 15d49bfd..088222c4 100644--- a/docs/adr/README.md+++ b/docs/adr/README.md@@ -175,6 +175,7 @@ record only when you need the evidence or the edge cases.| [0151](0151-zai-coding-plan-login.md) | Z.AI Coding Plan is a `sub_login` like Kimi/xAI: `graff login zai` uses ZCode's public CLI OAuth broker, then provisions a Graff-named API key onto `/api/coding/paas/v4`. || [0152](0152-persistent-shells-snapshot-once.md) | Persistent shells snapshot immediately (`wait_ms` ignored) and stay on `/jobs`; do not poll. Finite jobs still wait until exit (ADR 0010). || [0153](0153-task-workspaces-copy-and-scripts.md) | Task workspaces copy gitignored include files from the main checkout and run setup/run/archive scripts from `.graff/workspace.toml`. Finish is still not archive. |+| [0154](0154-parked-shells-yield-the-parent.md) | Interactive parked shells yield the parent like subagents: 15s foreground wait, then the prompt is yours until the job-exit wake. |## When to write onediff --git a/src/exec_bash.zig b/src/exec_bash.zigindex 6afff507..b4567e18 100644--- a/src/exec_bash.zig+++ b/src/exec_bash.zig@@ -1,10 +1,12 @@//! Root and subagent `bash` dispatch. Split out of exec.zig so the grok-build//! auto-background path (#620) has room without growing exec.zig.//!-//! Root foreground: spawn a job, wait up to 120s (a shorter `timeout` may-//! promote earlier; a larger one cannot extend the wait), then promote-//! rather than kill — the process keeps running and the model gets a job-//! id. Subagents stay on the #93 kill-at-120s path (no TTY, no /jobs UI).+//! Root foreground: spawn a job, wait up to 120s unattended (interactive+//! REPL/TUI/GUI: 15s, ADR 0154). A shorter `timeout` may promote earlier; a+//! larger one cannot extend the wait. Then promote rather than kill — the+//! process keeps running and the model gets a job id. Interactive parked+//! jobs yield the parent like subagents. Subagents stay on the #93+//! kill-at-120s path (no TTY, no /jobs UI).const std = @import("std");const builtin = @import("builtin");@@ -30,7 +32,8 @@ const exec_bash_stream = @import("exec_bash_stream.zig");pub const root_wait_ms: u64 = 120 * 1000;/// Lean `-p` has no human watching a hung test (ADR 0055). DeepSeek flash-/// SWE sat 120s on `python3 test_json_stream.py`. Interactive stays 120s.+/// SWE sat 120s on `python3 test_json_stream.py`. Interactive REPL/TUI/GUI+/// uses the same 15s bound so a `gh run watch` parks and yields (ADR 0154).pub const lean_oneshot_wait_ms: u64 = 15 * 1000;/// Wall-clock ceiling for one *subagent* bash command. Subagents run on pool@@ -90,14 +93,19 @@ fn startedText(gpa: Allocator, id: u32, cmd: []const u8, ssh: bool, auto_bg: booreturn aw.toOwnedSlice();}-fn defaultRootWaitMs() u64 {+fn defaultRootWaitMs(interactive: bool) u64 {const main_mod = @import("main.zig");+ if (interactive) return lean_oneshot_wait_ms;if (main_mod.unattended and @import("no_local_tools.zig").lean) return lean_oneshot_wait_ms;return root_wait_ms;}fn rootWaitMs(input: Value) u64 {- const bound = defaultRootWaitMs();+ return rootWaitMsFor(input, false);+}++fn rootWaitMsFor(input: Value, interactive: bool) u64 {+ const bound = defaultRootWaitMs(interactive);const t = intField(input, "timeout") orelse return bound;if (t <= 0) return bound;// #850: timeout may shorten the foreground wait; it must not recreate the@@ -178,6 +186,16 @@ test "rootWaitMs: lean unattended oneshot defaults to 15s; shorter timeout stilltry std.testing.expectEqual(root_wait_ms, rootWaitMs(empty.value));}+test "rootWaitMs: interactive REPL parks at 15s (ADR 0154)" {+ const empty = try std.json.parseFromSlice(Value, std.testing.allocator, "{}", .{});+ defer empty.deinit();+ try std.testing.expectEqual(lean_oneshot_wait_ms, rootWaitMsFor(empty.value, true));+ try std.testing.expectEqual(root_wait_ms, rootWaitMsFor(empty.value, false));+ const huge = try std.json.parseFromSlice(Value, std.testing.allocator, "{\"timeout\":120000}", .{});+ defer huge.deinit();+ try std.testing.expectEqual(lean_oneshot_wait_ms, rootWaitMsFor(huge.value, true));+}+fn formatJobDone(gpa: Allocator, cmd: []const u8, wait: jobs.FgDone) !ToolOutput {defer gpa.free(wait.output);var aw: Io.Writer.Allocating = .init(gpa);@@ -321,9 +339,10 @@ fn execUnchecked(ctx: ToolCtx, call: tools.ToolCall) !ToolOutput {.persistent = bg,}) catch |err| return .{ .text = try spawnFailText(gpa, err), .is_error = true };if (bg) {+ @import("subagent_interactive.zig").request(ctx);return .{ .text = try startedText(gpa, job.id, job.cmd, ssh, false, 0, "") };}- const wait_ms = rootWaitMs(input);+ const wait_ms = rootWaitMsFor(input, ctx.interactive_children);const waited = jobs.waitForeground(gpa, io, job.id, wait_ms) catch |err| return .{.text = try std.fmt.allocPrint(gpa, "could not wait on job {d} ({t})", .{ job.id, err }),.is_error = true,@@ -336,6 +355,7 @@ fn execUnchecked(ctx: ToolCtx, call: tools.ToolCall) !ToolOutput {.running => |r| blk: {defer gpa.free(r.output);jobs.markPersistent(io, r.id);+ @import("subagent_interactive.zig").request(ctx);break :blk .{ .text = try startedText(gpa, r.id, cmd, ssh, true, wait_ms / 1000, r.output) };},.cancelled => |c| blk: {diff --git a/src/jobs_tests.zig b/src/jobs_tests.zigindex 809ca7de..eb2940e5 100644--- a/src/jobs_tests.zig+++ b/src/jobs_tests.zig@@ -191,6 +191,20 @@ test "#850: a bounded promote leaves the child on bash_output and bash_kill" {try std.testing.expect(std.mem.indexOf(u8, after.text, "running") == null);}+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 = .{};diff --git a/src/main.zig b/src/main.zigindex 202f74f1..3ef96b9e 100644--- a/src/main.zig+++ b/src/main.zig@@ -593,6 +593,7 @@ test { // ── Unit tests (`zig build test`): pull in tests from imported modu_ = .{ @import("presence_accord.zig"), @import("peer_idle.zig"), @import("acp_idle.zig"), @import("presence_chan.zig"), @import("presence_record.zig"), @import("daddy.zig"), @import("peer_inbox_content_test.zig"), @import("peer_inbox_failure_test.zig"), @import("peer_inbox_storage_test.zig"), @import("peer_inbox_sequence_test.zig"), @import("peer_inbox_snapshot_test.zig") };_ = @import("acp_agents.zig");_ = @import("subagent_activity.zig");+ _ = @import("subagent_interactive.zig");_ = @import("subagent_recovery.zig");_ = @import("acp_agent_activity.zig");_ = @import("read_image.zig");diff --git a/src/subagent_interactive.zig b/src/subagent_interactive.zigindex c724b52a..c0484452 100644--- a/src/subagent_interactive.zig+++ b/src/subagent_interactive.zig@@ -33,7 +33,7 @@ pub fn beforeRequest(root: anytype) !?[]const u8 {if (root.sub or !enabled.load(.acquire)) return null;yielded = requested.swap(false, .acq_rel);if (!yielded) return null;- const text = "Subagents launched; their work continues separately. You can keep using the prompt; completed results will be surfaced automatically.";+ const text = "Background work continues separately. You can keep using the prompt; completed shell jobs and subagents will be surfaced automatically.";return try root.arena.dupe(u8, text);}@@ -83,3 +83,15 @@ pub fn output(ctx: tools.ToolCtx, id: u32, wait_ms: u64) !tools.ToolOutput {if (registry.find(id)) |job| if (!job.done) request(ctx);return result;}++test "beforeRequest yields so parked shell jobs free the prompt" {+ configure(true);+ defer configure(false);+ requested.store(true, .release);+ var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena_state.deinit();+ const Fake = struct { sub: bool = false, arena: std.mem.Allocator };+ const text = (try beforeRequest(Fake{ .arena = arena_state.allocator() })) orelse return error.TestUnexpectedResult;+ try std.testing.expect(std.mem.indexOf(u8, text, "keep using the prompt") != null);+ try std.testing.expect(std.mem.indexOf(u8, text, "shell jobs") != null);+}