Skip to content

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

Python

Snapshot persistent shell jobs instead of honoring wait_ms

Cursor Agentc9093ffd7874
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 42f57536..b83db71b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,7 @@ current is part of cutting a release.
## Unreleased
+- Persistent `shell` / `bash_output` jobs snapshot immediately (`wait_ms` ignored) and stay on `/jobs`; the 15s still-running poll no longer holds the turn (ADR 0152). Finite jobs still wait until exit.
- `graff login zai` (aliases `glm`, `z.ai`) signs in to a Z.AI Coding Plan and routes chat to `/api/coding/paas/v4` without `ZAI_CODING=1` (ADR 0151).
- Native: clickable browser/chat links, folder-picker repeat submissions, composer history recall layout, prompt-history session/attachment scoping, saved-snapshot reopen, worktree path wrapping. Shutdown-fixture stalls dump renderer/backend/worker state (#1080, #1103, #1105, #1108, #1109, #1112, #1113).
diff --git a/docs/adr/0091-persistent-jobs-honor-wait-ms.md b/docs/adr/0091-persistent-jobs-honor-wait-ms.md
index 83306344..d7a7c75e 100644
--- a/docs/adr/0091-persistent-jobs-honor-wait-ms.md
+++ b/docs/adr/0091-persistent-jobs-honor-wait-ms.md
@@ -1,6 +1,9 @@
# 0091. Persistent bash jobs honor wait_ms as a timeout
-Status: accepted 2026-09-08
+Status: superseded 2026-09-21 by [0152](0152-persistent-shells-snapshot-once.md)
+
+Honoring `wait_ms` as a real timeout on servers produced the 15s
+`still running` poll flood. Persistent jobs now snapshot immediately.
## Context
diff --git a/docs/adr/0152-persistent-shells-snapshot-once.md b/docs/adr/0152-persistent-shells-snapshot-once.md
new file mode 100644
index 00000000..e458e310
--- /dev/null
+++ b/docs/adr/0152-persistent-shells-snapshot-once.md
@@ -0,0 +1,32 @@
+# 0152. Persistent shells snapshot once and stay in the job list
+
+Status: accepted 2026-09-21
+
+## Context
+
+ADR 0091 made `wait_ms` a real millisecond timeout on `run_in_background`
+servers so `wait_ms: 1000` would not block for 10 hours (#810). Models still
+pass `wait_ms: 15000` / `180000` on a server. The wait pulses every 15s
+(`bash_output · job N still running · … unread byte(s)`) and floods the
+transcript. The tool result already called `wait_ms` a snapshot timeout.
+
+Background subagents already return one id and continue. A persistent
+server should match that: one "running" result, then the model works. Exit
+is a later wake (ADR 0010 notify).
+
+## Decision
+
+- Persistent jobs (`run_in_background` or auto-parked after the foreground
+ wait) return an immediate running snapshot from `action=output` /
+ `bash_output`. `wait_ms` is ignored.
+- The job stays in `/jobs`. Unread bytes are read by a later snapshot.
+ Do not poll.
+- Finite (non-persistent) jobs keep ADR 0010: `wait_ms>0` waits until exit
+ (10h cap). REPL, TUI, and GUI share this engine path.
+
+## Consequences
+
+A `wait_ms: 30000` call on a server returns now, not in 30s, so the 15s
+pulse cannot fire. A model that wanted "sleep 1s then snapshot startup
+logs" no longer can through `wait_ms`; it snapshots now and reads again
+later if needed. Finite CI watches are unchanged.
diff --git a/docs/adr/README.md b/docs/adr/README.md
index b431d371..0505443e 100644
--- a/docs/adr/README.md
+++ b/docs/adr/README.md
@@ -101,7 +101,7 @@ record only when you need the evidence or the edge cases.
| [0088](0088-failed-children-return-partial-evidence.md) | Failed children return bounded partial evidence with error status intact, including an excerpt for workflow synthesis. |
| [0089](0089-canonical-compaction-windows-survive-recovery.md) | Preserve standalone compaction windows across pruning and resume; opaque recovery uses the server or reports failure without discarding history. |
| [0090](0090-project-constraints-require-explicit-scope.md) | Local steering stays local; durable project constraints require explicit scope and exact current-user text, while legacy unscoped records are reviewable. |
-| [0091](0091-persistent-jobs-honor-wait-ms.md) | Persistent `run_in_background` jobs honor `wait_ms` as a timeout; finite jobs still wait until exit (ADR 0010). |
+| [0091](0091-persistent-jobs-honor-wait-ms.md) | Superseded by [0152](0152-persistent-shells-snapshot-once.md) for persistent `wait_ms`; finite jobs still wait until exit (ADR 0010). |
| [0092](0092-codedb-context-is-local-first.md) | Bare `codedb context` is `--local`; `--hybrid` / `--semantic` are opt-in remote rerank (and still refused under ADR 0084 policy). |
| [0093](0093-project-layout-breadth-and-directory-hints.md) | Project layout favors breadth under its caps; missing directories offer bounded sibling hints without changing confinement or ADR 0090 constraint policy. |
| [0094](0094-subagent-interrupt-is-a-cancel-file.md) | GUI Stop writes `{id}.cancel` in the activity dir; the child finishes failed even if the model returns. Not `session/cancel`. |
@@ -173,6 +173,7 @@ record only when you need the evidence or the edge cases.
| [0149](0149-daddy-directives-are-explicit-control.md) | Ambient `[peer]` wakes are not a new task; `[daddy]` directives are explicit supervisor control. |
| [0150](0150-subagent-briefs-are-structured.md) | Child briefs are headed sections plus a harness environment header; the judge stays a bare prompt. |
| [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). |
## When to write one
diff --git a/src/exec_bash.zig b/src/exec_bash.zig
index ef55005f..6afff507 100644
--- a/src/exec_bash.zig
+++ b/src/exec_bash.zig
@@ -76,11 +76,11 @@ fn startedText(gpa: Allocator, id: u32, cmd: []const u8, ssh: bool, auto_bg: boo
const w = &aw.writer;
try w.print("[job {d} started: {s}]\n", .{ id, cmd });
if (@import("job_wait.zig").followup_pending.load(.acquire)) {
- try w.print("Follow-up received while this was running; it was moved to the background so you can talk. Process is still running as job {d}. shell action=output wait_ms>0 waits until it exits; omit wait_ms for a snapshot. action=kill stops it.", .{id});
+ try w.print("Follow-up received while this was running; it was moved to the background so you can talk. Process is still running as job {d}. You are notified on exit — do not poll. action=output is a snapshot; action=kill stops it.", .{id});
} else if (auto_bg) {
- try w.print("Command exceeded the {d}s foreground wait and was automatically moved to the background. Process is still running. You are notified on exit — do not poll. shell action=output id {d} is a snapshot, not wait-until-exit; omit wait_ms for an immediate snapshot. Leave it on /jobs or action=kill it. Explicit run_in_background keeps wait-until-exit for intentional long work.", .{ wait_s, id });
+ try w.print("Command exceeded the {d}s foreground wait and was automatically moved to the background. Process is still running as job {d}. You are notified on exit — do not poll. action=output is a snapshot; leave it on /jobs or action=kill it.", .{ wait_s, id });
} else {
- try w.print("This is a persistent job. shell action=output wait_ms is a snapshot timeout, not wait-until-exit — omit wait_ms for an immediate snapshot. You are notified on exit — do not poll. action=output id {d} reads unread output. action=kill stops it.", .{id});
+ try w.print("This is a persistent server. It runs in the background across turns. You are notified on exit — do not poll. action=output id {d} reads unread output. action=kill stops it.", .{id});
}
if (ssh) try w.writeAll(" Killing the local SSH job cannot prove a detached remote process stopped; verify the remote host.");
if (partial.len > 0) {
@@ -223,9 +223,20 @@ test "auto-promoted jobs snapshot instead of inheriting wait-until-exit" {
const gpa = std.testing.allocator;
const text = try startedText(gpa, 7, "find /", false, true, 15, "partial");
defer gpa.free(text);
- try std.testing.expect(std.mem.indexOf(u8, text, "wait-until-exit") != null);
+ try std.testing.expect(std.mem.indexOf(u8, text, "do not poll") != null);
+ try std.testing.expect(std.mem.indexOf(u8, text, "snapshot") != null);
try std.testing.expect(std.mem.indexOf(u8, text, "waits until it exits") == null);
- try std.testing.expect(std.mem.indexOf(u8, text, "run_in_background") != null);
+ try std.testing.expect(std.mem.indexOf(u8, text, "job 7") != null);
+}
+
+test "persistent start text parks the server and forbids polling" {
+ const gpa = std.testing.allocator;
+ const text = try startedText(gpa, 446, "npm run dev", false, false, 0, "");
+ defer gpa.free(text);
+ try std.testing.expect(std.mem.indexOf(u8, text, "[job 446 started: npm run dev]") != null);
+ try std.testing.expect(std.mem.indexOf(u8, text, "persistent server") != null);
+ try std.testing.expect(std.mem.indexOf(u8, text, "do not poll") != null);
+ try std.testing.expect(std.mem.indexOf(u8, text, "omit wait_ms") == null);
}
fn formatCapped(gpa: Allocator, cmd: []const u8, run: jobs.CappedRun) !ToolOutput {
diff --git a/src/job_notify.zig b/src/job_notify.zig
index 0519cbe7..db6c4abc 100644
--- a/src/job_notify.zig
+++ b/src/job_notify.zig
@@ -138,9 +138,9 @@ pub fn printRunning(w: *Io.Writer, id: u32, waited_ms: u64, interrupted: bool, p
const el = tool_pulse.formatElapsed(&ebuf, waited_ms);
if (persistent) {
if (interrupted) {
- try w.print("[job {d}: running · {s} waited, then interrupted — persistent server; wait_ms is a snapshot timeout, not wait-until-exit]", .{ id, el });
+ try w.print("[job {d}: running · {s} waited, then interrupted — persistent server; parked in the background. You are notified on exit; do not poll]", .{ id, el });
} else {
- try w.print("[job {d}: running · {s} elapsed — persistent server; wait_ms is a snapshot timeout. omit wait_ms for an immediate snapshot]", .{ id, el });
+ try w.print("[job {d}: running — persistent server; parked in the background. You are notified on exit; do not poll. action=output reads unread bytes]", .{id});
}
return;
}
@@ -267,6 +267,17 @@ test "printRunning reports the wait that happened, not the deadline (ADR 0061)"
try std.testing.expectEqualStrings("[job 5: running · 2m05s elapsed — you are notified on exit; do not call bash_output again]", aw.written());
}
+test "printRunning parks a persistent server instead of inviting a wait_ms poll (ADR 0152)" {
+ var aw: Io.Writer.Allocating = .init(std.testing.allocator);
+ defer aw.deinit();
+ try printRunning(&aw.writer, 446, 180_000, false, true);
+ try std.testing.expectEqualStrings("[job 446: running — persistent server; parked in the background. You are notified on exit; do not poll. action=output reads unread bytes]", aw.written());
+ try std.testing.expect(std.mem.indexOf(u8, aw.written(), "omit wait_ms") == null);
+ aw.clearRetainingCapacity();
+ try printRunning(&aw.writer, 446, 15_000, true, true);
+ try std.testing.expectEqualStrings("[job 446: running · 15s waited, then interrupted — persistent server; parked in the background. You are notified on exit; do not poll]", aw.written());
+}
+
test "takeWake drains and formats the grok-build do-not-poll reminder" {
const io = std.testing.io;
count = 0;
diff --git a/src/job_wait.zig b/src/job_wait.zig
index 6d7aac23..9ac4991b 100644
--- a/src/job_wait.zig
+++ b/src/job_wait.zig
@@ -35,15 +35,15 @@ pub const legacy_poll_ms: u64 = 30_000;
/// * `> 0` on a finite job — wait until exit (or Esc), always `wait_cap_ms`.
/// Mid-range "safety timeouts" used to bounce the model every few minutes
/// (#640 / ADR 0010).
-/// * `> 0` on a persistent server (`run_in_background`) — honor `wait_ms` as
-/// a millisecond cap, then return a running snapshot (ADR 0091 / #810).
+/// * `> 0` on a persistent server (`run_in_background` or auto-parked) —
+/// snapshot now. `wait_ms` is ignored so a 15s/30s poll cannot hold the
+/// turn (ADR 0152). Unread bytes stay in the job until a later snapshot.
pub fn resolveDeadline(wait_ms: u64) u64 {
return resolveDeadlineFor(wait_ms, false);
}
pub fn resolveDeadlineFor(wait_ms: u64, persistent: bool) u64 {
- if (wait_ms == 0) return 0;
- if (persistent) return @min(wait_ms, wait_cap_ms);
+ if (wait_ms == 0 or persistent) return 0;
return wait_cap_ms;
}
@@ -73,9 +73,10 @@ test "resolveDeadline: 10h is 36_000_000 ms" {
try std.testing.expectEqual(@as(u64, 36_000_000), wait_cap_ms);
}
-test "resolveDeadlineFor: persistent jobs honor wait_ms as a timeout (#810)" {
+test "resolveDeadlineFor: persistent jobs snapshot immediately (ADR 0152)" {
try std.testing.expectEqual(@as(u64, 0), resolveDeadlineFor(0, true));
- try std.testing.expectEqual(@as(u64, 1000), resolveDeadlineFor(1000, true));
- try std.testing.expectEqual(wait_cap_ms, resolveDeadlineFor(wait_cap_ms + 1, true));
+ try std.testing.expectEqual(@as(u64, 0), resolveDeadlineFor(1000, true));
+ try std.testing.expectEqual(@as(u64, 0), resolveDeadlineFor(legacy_poll_ms, true));
+ try std.testing.expectEqual(@as(u64, 0), resolveDeadlineFor(wait_cap_ms, true));
try std.testing.expectEqual(wait_cap_ms, resolveDeadlineFor(1000, false));
}
diff --git a/src/jobs.zig b/src/jobs.zig
index 1b7fbe50..40a82f80 100644
--- a/src/jobs.zig
+++ b/src/jobs.zig
@@ -372,7 +372,7 @@ pub fn spawnJobOpts(gpa: Allocator, io: Io, cmd: []const u8, opts: SpawnOpts) !*
/// bash_output: unread output + status. wait_ms=0 is a snapshot. wait_ms>0
/// blocks until the job exits (or Esc) for finite jobs (ADR 0010). Persistent
-/// servers honor wait_ms as a millisecond cap (ADR 0091 / #810).
+/// servers snapshot immediately; wait_ms is ignored (ADR 0152).
pub fn jobOutput(gpa: Allocator, io: Io, id: u32, wait_ms: u64) !ToolOutput {
var waited: u64 = 0;
var interrupted = false; // Esc: report what was waited, not the 10h cap (ADR 0061)
diff --git a/src/jobs_tests.zig b/src/jobs_tests.zig
index 6c0ad37c..76cbd504 100644
--- a/src/jobs_tests.zig
+++ b/src/jobs_tests.zig
@@ -109,17 +109,25 @@ test "isolated capped runs clean descendants after timeout and normal exit" {
try std.testing.expectError(error.FileNotFound, tmp.dir.openFile(io, "completed-marker", .{}));
}
-test "bash_output wait_ms on a persistent job is a snapshot timeout (#810)" {
+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 snap = try jobs.jobOutput(gpa, io, id, 400);
+ const t0 = std.time.milliTimestamp();
+ const snap = try jobs.jobOutput(gpa, io, id, 5_000);
+ const elapsed = std.time.milliTimestamp() - t0;
defer gpa.free(snap.text);
+ try std.testing.expect(elapsed < 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)" {
diff --git a/src/no_local_tools.zig b/src/no_local_tools.zig
index 613f842b..fdfe8279 100644
--- a/src/no_local_tools.zig
+++ b/src/no_local_tools.zig
@@ -139,7 +139,7 @@ pub const lean_subagent_desc = "Spawn a sidecar for a self-contained task. Child
/// One-shot tool prose (ADR 0046). Same names and JSON schemas; shorter
/// descriptions so flash models start and finish instead of rereading
/// essays. Do not drop to four tools (ADR 0024).
-pub const lean_shell_desc = "Run /bin/sh -c in cwd. action=run returns stdout/stderr/exit. Foreground still running after 15s (or a shorter timeout) backgrounds — action=output wait_ms>0 waits; do not poll.";
+pub const lean_shell_desc = "Run /bin/sh -c in cwd. action=run returns stdout/stderr/exit. Foreground still running after 15s (or a shorter timeout) backgrounds. Persistent servers snapshot immediately — do not poll. Finite action=output wait_ms>0 waits until exit.";
pub const lean_bash_desc = lean_shell_desc;
pub const lean_read_file_desc = "Read a UTF-8 file. Call before editing. contains=exact-key numbered lines; start_line/end_line for a window.";
pub const lean_edit_file_desc = "Replace exact text. Prefer one batched edits[] over many calls. Prefer over write_file for an existing file.";
diff --git a/src/shell_tool.zig b/src/shell_tool.zig
index 361ee44a..95af4a07 100644
--- a/src/shell_tool.zig
+++ b/src/shell_tool.zig
@@ -21,9 +21,9 @@ const jobs = @import("jobs.zig");
const exec_bash = @import("exec_bash.zig");
pub const tool_name = "shell";
-pub const tool_desc = "Run a shell command via /bin/sh -c in the current working directory, read a background job, or stop one. action=run (default when command is set) returns stdout/stderr/exit. A user-cancelled command reports cancelled (its whole local process group is killed; a remote process started over ssh may survive on the remote host). Foreground commands still running after 120s (lean -p: 15s, or a shorter timeout ms) move to the background and return a job id. For long-running commands set run_in_background true to skip the wait. You are notified on completion — do not poll. action=output wait_ms>0 blocks until exit (up to 10h) for a finite job; omit wait_ms for a snapshot. action=kill stops it. A background job that writes nothing and is read by nobody for 2 hours is stopped for inactivity; the user pins a long-lived server with /jobs keep. bash/bash_output/bash_kill still dispatch. Not a PTY: you cannot type into the process. Type in the composer to steer the turn.";
+pub const tool_desc = "Run a shell command via /bin/sh -c in the current working directory, read a background job, or stop one. action=run (default when command is set) returns stdout/stderr/exit. A user-cancelled command reports cancelled (its whole local process group is killed; a remote process started over ssh may survive on the remote host). Foreground commands still running after 120s (lean -p: 15s, or a shorter timeout ms) move to the background and return a job id. For long-running commands set run_in_background true to skip the wait. You are notified on completion — do not poll. action=output wait_ms>0 blocks until exit (up to 10h) for a finite job; a persistent server snapshots immediately (wait_ms is ignored) and stays on /jobs. action=kill stops it. A background job that writes nothing and is read by nobody for 2 hours is stopped for inactivity; the user pins a long-lived server with /jobs keep. bash/bash_output/bash_kill still dispatch. Not a PTY: you cannot type into the process. Type in the composer to steer the turn.";
pub const tool_schema =
- \\{"type": "object", "properties": {"action": {"type": "string", "enum": ["run", "output", "kill"], "description": "run a command, read a job, or stop a job. Default run when command is set."}, "command": {"type": "string", "description": "Shell command to execute (action=run)"}, "timeout": {"type": "integer", "description": "Optional foreground wait in milliseconds before auto-background. A shorter value promotes earlier; a larger value cannot extend past the 120s (lean -p: 15s) bound — to wait for a genuinely long command, set run_in_background true and use action=output wait_ms>0, which blocks until exit (10h cap). 0 uses the default. Ignored when run_in_background is true."}, "run_in_background": {"type": "boolean", "description": "Start as a background job and return its id immediately instead of waiting (default false)"}, "id": {"type": "integer", "description": "Job id for action=output or kill"}, "wait_ms": {"type": "integer", "description": "0 = snapshot now. Finite jobs: >0 waits until exit (10h cap). Persistent servers: >0 is a millisecond timeout, then a running snapshot."}}}
+ \\{"type": "object", "properties": {"action": {"type": "string", "enum": ["run", "output", "kill"], "description": "run a command, read a job, or stop a job. Default run when command is set."}, "command": {"type": "string", "description": "Shell command to execute (action=run)"}, "timeout": {"type": "integer", "description": "Optional foreground wait in milliseconds before auto-background. A shorter value promotes earlier; a larger value cannot extend past the 120s (lean -p: 15s) bound. 0 uses the default. Ignored when run_in_background is true. A persistent server's action=output is a snapshot, not wait-until-exit."}, "run_in_background": {"type": "boolean", "description": "Start as a background job and return its id immediately instead of waiting (default false). Do not poll it."}, "id": {"type": "integer", "description": "Job id for action=output or kill"}, "wait_ms": {"type": "integer", "description": "0 = snapshot now. Finite jobs: >0 waits until exit (10h cap). Persistent servers: snapshot now; wait_ms is ignored. Do not poll."}}}
;
pub const Action = enum { run, output, kill };
@@ -197,6 +197,8 @@ test "catalog advertises shell, not the three bash names" {
saw_shell = true;
try std.testing.expectEqualStrings(tool_desc, t.desc);
try std.testing.expectEqualStrings(tool_schema, t.schema);
+ try std.testing.expect(std.mem.indexOf(u8, t.schema, "wait_ms is ignored") != null);
+ try std.testing.expect(std.mem.indexOf(u8, t.desc, "snapshots immediately") != null);
}
}
try std.testing.expect(saw_shell);