Repository navigation
fix(orchestration): count approval requests per CLI signal and report them once the prompt is drawn #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix(orchestration): count approval requests per CLI signal and report them once the prompt is drawn #205
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,7 +90,10 @@ export interface AgentWaitResult { | |
| export interface AgentWaitTiming { | ||
| /** How often a waiting call reads the session again. */ | ||
| checkMs: number; | ||
| /** How long the screen must stay the same before an idle status counts (a CLI may still be drawing its answer). */ | ||
| /** | ||
| * How long the screen must stay the same before an idle status counts (a CLI may still be drawing its answer), and | ||
| * the longest a needs_approval waits for its prompt to reach the screen. | ||
| */ | ||
| settleMs: number; | ||
| /** How long a session without status must show the same screen before it counts as "quiet". */ | ||
| quietMs: number; | ||
|
|
@@ -393,12 +396,15 @@ export class AgentControlService { | |
| if (!session) return answer(null, "closed"); | ||
| const now = Date.now(); | ||
| const current = this.outputOffset(sessionId); | ||
| if (current !== offset) { offset = current; changedAt = now; } | ||
| const stillWriting = current !== offset; | ||
| if (stillWriting) { offset = current; changedAt = now; } | ||
| const quietFor = now - changedAt; | ||
| if (session.exitCode !== null) return answer(session, session.exitCode === 0 ? "done" : "failed"); | ||
| // A request a wait already returned (the card shows no newer one) is the person's to answer: wait for it. | ||
| const reported = this.reportedApprovals.has(sessionId) && this.reportedApprovals.get(sessionId) === this.approvalRequest(sessionId); | ||
| if (session.status === "needs_approval" && !reported) return answer(session, "needs_approval"); | ||
| if (session.status === "needs_approval" && !reported && this.approvalOnScreen(sessionId, current, stillWriting, now, timing)) { | ||
| return answer(session, "needs_approval"); | ||
| } | ||
| if (session.status !== "needs_approval") this.reportedApprovals.delete(sessionId); | ||
| // After a prompt, an idle that no turn followed (the CLI's startup idle, or one reported before the turn began) | ||
| // is not the answer: only an idle after a turn that started since that prompt is. A CLI that never reports its | ||
|
|
@@ -483,6 +489,18 @@ export class AgentControlService { | |
| return tail(typeof this.terminals.redactSecrets === "function" ? this.terminals.redactSecrets(text) : text, maxChars); | ||
| } | ||
|
|
||
| /** | ||
| * Whether the current approval request's prompt can be read: the screen has changed since the request was counted | ||
| * and did not change in the last check. A request signal can come before its prompt is drawn (OpenCode's | ||
| * permission.asked); one whose prompt was drawn first, or never is, counts as shown once settleMs passed. | ||
| */ | ||
| private approvalOnScreen(sessionId: string, current: number, stillWriting: boolean, now: number, timing: AgentWaitTiming): boolean { | ||
| let prompt: { outputOffset: number; at: number } | null = null; | ||
| try { prompt = typeof this.terminals.approvalPrompt === "function" ? this.terminals.approvalPrompt(sessionId) : null; } catch { prompt = null; } | ||
| if (!prompt) return true; | ||
| return (current > prompt.outputOffset && !stillWriting) || now - prompt.at >= timing.settleMs; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If Qwen emits its approval title in a separate PTY chunk and Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| /** The card's current approval request (TerminalManager.approvalRequest); undefined when the manager has none. */ | ||
| private approvalRequest(sessionId: string): number | undefined { | ||
| try { return typeof this.terminals.approvalRequest === "function" ? this.terminals.approvalRequest(sessionId) ?? undefined : undefined; } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { mkdtemp, realpath, rm } from "node:fs/promises"; | ||
| import { tmpdir } from "node:os"; | ||
| import { join } from "node:path"; | ||
| import { setTimeout as delay } from "node:timers/promises"; | ||
| import test from "node:test"; | ||
| import { AgentControlService } from "../src/main/services/AgentControlService.ts"; | ||
| import { TerminalManager } from "../src/main/services/TerminalManager.ts"; | ||
| import { availableRegistry, fakeSpawner } from "./helpers/terminal.mjs"; | ||
|
|
||
| // wait_for_agent reports each approval request once, with its prompt. CLIs tell a request in different ways: OpenCode's | ||
| // permission.asked can come before the prompt is drawn, Qwen's title says needs_approval before its PermissionRequest | ||
| // hook for the same prompt, and Grok's permission_prompt Notification is its only request hook. | ||
|
|
||
| const at = { x: 0, y: 0 }; | ||
| const PROMPT = "\x1b[2J\x1b[HPermission required\r\n $ rm -rf build\r\n> Allow once Allow always Reject"; | ||
|
|
||
| async function fixture(t, provider, timing = { checkMs: 5, settleMs: 20, quietMs: 60_000 }) { | ||
| const root = await realpath(await mkdtemp(join(tmpdir(), "ctty-approval-signals-"))); | ||
| t.after(() => rm(root, { recursive: true, force: true })); | ||
| const calls = []; | ||
| const terminals = new TerminalManager(() => undefined, availableRegistry(), undefined, undefined, true, fakeSpawner(calls)); | ||
| t.after(() => terminals.shutdown()); | ||
| const control = new AgentControlService(terminals, { waitTiming: timing }); | ||
| const orchestrator = terminals.create({ provider: "claude", profile: "normal", cwd: root, position: at, role: "orchestrator" }); | ||
| const child = terminals.create({ provider, profile: "normal", cwd: root, position: at, role: "subagent", parentSessionId: orchestrator.id }); | ||
| const pty = calls.at(-1).process; | ||
| const signal = (event, source = "hook") => terminals.applyProviderSignal(child.id, { kind: "lifecycle", state: "needs_approval", event }, source); | ||
| return { terminals, control, child, pty, signal }; | ||
| } | ||
|
|
||
| async function pending(promise, ms = 150) { | ||
| let settled = false; | ||
| void promise.then(() => { settled = true; }, () => { settled = true; }); | ||
| await delay(ms); | ||
| return !settled; | ||
| } | ||
|
|
||
| test("an approval signalled before its prompt is drawn is reported with the prompt, and still only once", async (t) => { | ||
| const { control, child, pty, signal } = await fixture(t, "opencode", { checkMs: 5, settleMs: 2_000, quietMs: 60_000 }); | ||
| signal("permission.asked"); | ||
| const wait = control.waitFor(child.id, { timeoutMs: 5_000 }); | ||
| await delay(60); | ||
| pty.emitData(PROMPT); | ||
| const reported = await wait; | ||
| assert.equal(reported.reason, "needs_approval"); | ||
| assert.match(reported.output, /rm -rf build/, "the prompt is in the output"); | ||
| assert.ok(reported.waitedMs < 1_500, `returned once the prompt was drawn, not at the settle limit (${reported.waitedMs} ms)`); | ||
| assert.equal(await pending(control.waitFor(child.id, { timeoutMs: 5_000 })), true, "the next wait waits for the person"); | ||
| }); | ||
|
|
||
| test("an approval whose screen never changes is still reported after the settle time", async (t) => { | ||
| const { control, child, signal } = await fixture(t, "opencode", { checkMs: 5, settleMs: 80, quietMs: 60_000 }); | ||
| signal("permission.asked"); | ||
| const reported = await control.waitFor(child.id, { timeoutMs: 5_000 }); | ||
| assert.equal(reported.reason, "needs_approval"); | ||
| assert.ok(reported.waitedMs < 1_000, `no wait until the timeout (${reported.waitedMs} ms)`); | ||
| }); | ||
|
|
||
| test("Qwen's title and its PermissionRequest hook for one prompt are one request; a later hook is a new one", async (t) => { | ||
| const { terminals, control, child, signal } = await fixture(t, "qwen"); | ||
| signal(undefined, "title"); | ||
| assert.equal((await control.waitFor(child.id, { timeoutMs: 5_000 })).reason, "needs_approval"); | ||
| signal("PermissionRequest"); | ||
| const same = control.waitFor(child.id, { timeoutMs: 5_000 }); | ||
| assert.equal(await pending(same), true, "the same prompt is not reported again"); | ||
|
|
||
| terminals.input(child.id, "1"); | ||
| signal("PermissionRequest"); | ||
| assert.equal((await same).reason, "needs_approval", "the next request is reported"); | ||
| }); | ||
|
|
||
| test("an echo Notification before the PermissionRequest hook is the same request", async (t) => { | ||
| const { control, child, signal } = await fixture(t, "claude"); | ||
| signal("Notification"); | ||
| assert.equal((await control.waitFor(child.id, { timeoutMs: 5_000 })).reason, "needs_approval"); | ||
| signal("PermissionRequest"); | ||
| signal("Notification"); | ||
| assert.equal(await pending(control.waitFor(child.id, { timeoutMs: 5_000 })), true); | ||
| }); | ||
|
|
||
| test("each of Grok's permission_prompt Notifications is a request, one right after another too", async (t) => { | ||
| const { terminals, control, child, signal } = await fixture(t, "grok"); | ||
| signal("Notification"); | ||
| assert.equal((await control.waitFor(child.id, { timeoutMs: 5_000 })).reason, "needs_approval"); | ||
| const same = control.waitFor(child.id, { timeoutMs: 5_000 }); | ||
| assert.equal(await pending(same), true, "the reported prompt waits for the person"); | ||
|
|
||
| terminals.input(child.id, "1"); | ||
| signal("Notification"); | ||
| const next = await same; | ||
| assert.equal(next.reason, "needs_approval", "Grok's second prompt is reported"); | ||
| assert.ok(next.waitedMs < 2_000, `at once, not at the timeout (${next.waitedMs} ms)`); | ||
| assert.equal(await pending(control.waitFor(child.id, { timeoutMs: 5_000 })), true, "and only once"); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not treat the initial offset snapshot as settled output.
If
permission.askedarrives, the CLI writes only the prompt header, andwaitForstarts before the command and choices arrive,offsetandcurrentare equal on the first iteration. Line 399 setsstillWritingtofalsewithout observing a quiet interval. Line 501 then accepts the partial output immediately.The result marks this request as reported. Later waits suppress the same request even after its complete prompt arrives. Require an observed settling interval before accepting output-based visibility. Keep the
settleMsfallback for unchanged output.