graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
jobs: a queued follow-up ends a blocking output wait
justrachf05983502fa1
diff --git a/docs/adr/0262-a-follow-up-ends-a-blocking-output-wait.md b/docs/adr/0262-a-follow-up-ends-a-blocking-output-wait.mdnew file mode 100644index 00000000..229d1899--- /dev/null+++ b/docs/adr/0262-a-follow-up-ends-a-blocking-output-wait.md@@ -0,0 +1,28 @@+# 0262. A queued follow-up ends a blocking output wait++Status: accepted 2026-10-07++## Context++ADR 0010 makes `action=output` with `wait_ms > 0` on a finite job block until+the job exits, with Esc as the only way out. A follow-up the user queued while+that wait ran was not delivered until the job finished, which could be hours.+The foreground wait already treated a queued follow-up as the signal to move+the job to the background (`job_wait.followup_pending`); the explicit output+wait ignored it, and ACP never raised it for a prompt that arrived mid-turn+(#1522).++## Decision++- A blocking output wait returns a snapshot as soon as a follow-up is queued,+ exactly as it does on Esc. The job keeps running and still reports on exit.+- An ACP `session/prompt` that arrives while a turn is active raises the same+ follow-up signal. It does not cancel the turn; only `session/cancel` does.+- The signal clears when the next root turn is prepared.++## Consequences++ADR 0010's single-hop wait stands while nobody is waiting on the agent: no+polling, one wait covers exit. A user who speaks while a wait runs is heard at+the next step instead of after the job ends. Revisit if a client needs to queue+prompts behind a wait on purpose; it would need a distinct "queue only" signal.diff --git a/docs/adr/README.md b/docs/adr/README.mdindex 3e8e67e1..f26c17da 100644--- a/docs/adr/README.md+++ b/docs/adr/README.md@@ -285,6 +285,7 @@ record only when you need the evidence or the edge cases.| [0257](0257-a-slimmed-comment-list-names-its-issue.md) | A slimmed MCP comment list keeps the issue id its rows share (`{issue, n, latest_author}`), so parallel results name what they describe. || [0258](0258-gemini-chat-sends-repo-context-once.md) | On the Codegraff chat wire, Gemini root turns send the project instructions and layout as the conversation's first user message (ADR 0243's split), so the system prompt every request repeats no longer carries them. || [0259](0259-openai-routes-compact-only-server-side.md) | First-party Responses routes (direct API, Codegraff GPT, ChatGPT plan) compact only server-side: no client-summary fallback while the server arm is on. A compaction item's `encrypted_content` is metered at 16 bytes/token, not as text, so a blob-anchored history does not re-compact every step. |+| [0262](0262-a-follow-up-ends-a-blocking-output-wait.md) | A queued follow-up ends a blocking `action=output` wait like Esc does (the job keeps running and still reports on exit); an ACP prompt arriving mid-turn raises that follow-up without cancelling the turn. Refines ADR 0010. |## When to write onediff --git a/src/acp_inbox.zig b/src/acp_inbox.zigindex 2f837851..50dd1168 100644--- a/src/acp_inbox.zig+++ b/src/acp_inbox.zig@@ -149,8 +149,12 @@ pub const Inbox = struct {_ = acp_ask.reply(answer_text, cancelled_answer);return;}- if (std.mem.eql(u8, r.method, "session/prompt") and self.session_id == null)- self.session_id = try self.gpa.dupe(u8, sid orelse "");+ if (std.mem.eql(u8, r.method, "session/prompt")) {+ // #1522: a prompt queued behind a live turn is a follow-up, so+ // a blocking job wait yields to it (the job keeps running).+ if (self.active) @import("job_wait.zig").noteFollowup();+ if (self.session_id == null) self.session_id = try self.gpa.dupe(u8, sid orelse "");+ }}const copy = try self.gpa.dupe(u8, line);errdefer self.gpa.free(copy);@@ -298,3 +302,21 @@ test "session config selection during a turn queues an ordinary request withouttry std.testing.expect(std.mem.indexOf(u8, second.line, "session/set_config_option") != null);try std.testing.expect(std.mem.indexOf(u8, second.line, "session/prompt") == null);}++test "#1522 a prompt queued behind a live turn is a follow-up; the turn's own prompt is not" {+ const job_wait = @import("job_wait.zig");+ job_wait.followup_pending.store(false, .release);+ defer job_wait.followup_pending.store(false, .release);+ Agent.esc_cancel.store(false, .release);+ defer Agent.esc_cancel.store(false, .release);+ var reader: Io.Reader = .fixed("");+ var inbox: Inbox = .{ .gpa = std.testing.allocator, .io = std.testing.io, .reader = &reader };+ defer inbox.deinit();+ try inbox.accept("{\"method\":\"session/prompt\",\"params\":{\"sessionId\":\"s\"}}");+ try std.testing.expect(!job_wait.followup_pending.load(.acquire));+ inbox.begin();+ try inbox.accept("{\"id\":2,\"method\":\"session/prompt\",\"params\":{\"sessionId\":\"s\"}}");+ try std.testing.expect(job_wait.followup_pending.load(.acquire));+ // A follow-up yields waits; it never cancels the running turn.+ try std.testing.expect(!Agent.esc_cancel.load(.acquire));+}diff --git a/src/jobs.zig b/src/jobs.zigindex 7915b135..0f27bd7d 100644--- a/src/jobs.zig+++ b/src/jobs.zig@@ -427,7 +427,9 @@ pub fn jobOutput(gpa: Allocator, io: Io, id: u64, wait_ms: u64) !ToolOutput {return result;}g_jobs.mutex.unlock(io);- if (Agent.esc_cancel.load(.acquire)) {+ // #1522 (ADR 0262): a queued follow-up ends the wait the way it+ // promotes a foreground one — the job keeps running, the user is heard.+ if (Agent.esc_cancel.load(.acquire) or job_wait.followup_pending.load(.acquire)) {interrupted = true; // render current state on the next passcontinue;}diff --git a/src/jobs_tests.zig b/src/jobs_tests.zigindex 70eb8d80..fae496f1 100644--- a/src/jobs_tests.zig+++ b/src/jobs_tests.zig@@ -333,3 +333,29 @@ test "#199: a pinned job is retained at session end — record kept, tree alive,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+}