Skip to content

fix(runtime): require sustained quiescence for tui-idle waits - #6555

Closed
nwparker wants to merge 2 commits into
mainfrom
nwparker/fix-6011
Closed

nwparker wants to merge 2 commits into
mainfrom
nwparker/fix-6011

Conversation

@nwparker

@nwparker nwparker commented Jun 28, 2026 •

Copy link
Copy Markdown
Contributor

🧑‍🤝‍🧑 Impact & ELI5

1) What's broken today (ELI5). Orca lets you script your AI coding agents — for example, "wait until the agent finishes working, then do the next thing." The command for that is orca terminal wait --for tui-idle. The bug: this "are you done yet?" check answers "yes, done!" instantly — even while the agent is still actively typing, thinking, and streaming output. So any automation built on top of it fires too early and steps on the agent mid-task. This hits anyone using Orca to orchestrate or chain agent steps (especially agents like Codex, Devin, or aider whose terminal title is just their name). It's not a crash, but for orchestration it's a real blocker: the one signal you'd rely on to know a task is finished is unreliable.

2) What this PR does (ELI5). The old check trusted the terminal's title bar to decide if the agent was done. But some agents keep a plain title like Codex while their spinner and esc to interrupt live in the terminal body, so a busy terminal could look idle. The fix now separates strong idle evidence from weak idle evidence: known ready prompts and explicit done/idle OSC titles still resolve immediately, while ambiguous name-only titles must also prove the terminal has gone quiet. So Orca still moves fast when the agent gives a real completion signal, but it no longer cuts in just because a weak title sample looked idle.

3) Tradeoffs / regressions — honest answer. Current status: zero added latency for explicit completion signals. Body-level ready prompts and explicit idle/done OSC titles (for example Codex done, Claude ✳ ..., Gemini/Pi idle markers) satisfy tui-idle immediately. The remaining quiet wait applies only to ambiguous, non-explicit idle evidence — titles like plain Codex, Devin, or aider where Orca's shared title classifier defaults to idle even though the agent may still be working. For that weak signal, the TUI_IDLE_QUIESCENCE_MS quiet window is the necessary correctness guard: resolving immediately would reintroduce #6011. The follow-up also schedules that quiet check directly, so the ambiguous-title path resolves at the quiet deadline instead of waiting for the next 2s fallback poll. No setting, new user step, UI change, platform-specific behavior, SSH caveat, or Git-provider behavior changes.


Summary

orca terminal wait --terminal <h> --for tui-idle --timeout-ms 30000 --json returned in ~0s with {satisfied: true, status: running} even while the target agent TUI was actively working (spinner animating, output streaming, esc to interrupt visible). That made tui-idle useless as a "task done" signal — it fired mid-work.

Fixes #6011.

This PR supersedes community PR #6012 (I have taken over the change; the code is now maintained here). #6012 is left open for the author. Original author credited via Co-authored-by:.

Root cause

Two compounding defects:

  1. detectAgentStatusFromTitle defaults agent titles to idle (src/shared/agent-detection.ts, final fallthrough return 'idle'). A title with a recognized agent name but no explicit working marker (no braille spinner, no working|thinking|running, no Claude . prefix, no action required|permission|waiting) falls through to idle. Agents render their spinner / esc to interrupt in the terminal body, not the OSC title, so a busy agent with a name-only title (Devin, Codex, aider, …) is classified idle.
  2. The tui-idle wait trusted a single instantaneous title sample (src/main/runtime/orca-runtime.ts, waitForTerminal). It returned satisfied the moment lastAgentStatus === 'idle', with no sustained-idle requirement. The existing TUI_IDLE_QUIESCENCE_MS = 3000 debounce was only consulted in the foreground-process fallback (which runs only when lastAgentStatus === null), so a title-derived idle bypassed it entirely.

Fix

Use the strongest idle evidence first:

  • Body-level ready prompts (isKnownReadyPromptPreview) satisfy immediately because they directly detect the cursor at the input prompt.
  • Explicit OSC idle titles satisfy immediately when the live OSC title itself contains a strong idle marker (for example ready, idle, done, Claude ✳, Gemini/Pi idle markers).
  • Ambiguous title-derived idle — especially name-only titles that reach detectAgentStatusFromTitle's broad default-to-idle fallback — satisfies only after PTY output has been quiet for TUI_IDLE_QUIESCENCE_MS.

hasSatisfiedTuiIdle now centralizes that distinction across the synchronous fast paths, in-promise registration checks, fallback polls, and transition resolvers. Ambiguous idle waits use a one-shot quiet timer, so they resolve at the quiet-window deadline rather than waiting for the next fallback poll. Push-on-idle orchestration message delivery uses the same confirmed-idle boundary, so queued text is not injected into a terminal that is still streaming output behind a name-only idle-looking title.

Defect (1)'s broad default-to-idle is intentionally left in place (it's a renderer-shared classifier driving badges/indicators); tui-idle now neutralizes that weak signal locally without changing UI semantics. Tightening the shared classifier could be a follow-up.

Screenshots

No visual change. This is a main-process timing fix; no renderer/UI surface is touched.

Testing

  • ./node_modules/.bin/vitest run --config config/vitest.config.ts src/main/runtime/orca-runtime.test.ts -t "tui-idle|push-on-idle|already-idle agent|Cursor Agent without auto-submitting" — 20 passed
  • ./node_modules/.bin/vitest run --config config/vitest.config.ts src/main/runtime/orca-runtime.test.ts — 490 passed
  • ./node_modules/.bin/oxfmt --check src/main/runtime/orca-runtime.ts src/main/runtime/orca-runtime.test.ts — clean
  • ./node_modules/.bin/oxlint src/main/runtime/orca-runtime.ts src/main/runtime/orca-runtime.test.ts — clean
  • ./node_modules/.bin/tsgo --noEmit -p config/tsconfig.node.json — clean
  • pnpm build — not run (no build-affecting change; main-process logic only)
  • Regression coverage now verifies: name-only agent titles do not resolve while output keeps streaming; explicit idle OSC titles resolve immediately; ambiguous name-only idle resolves exactly at the quiet deadline; push-on-idle delivery waits for the same confirmed-idle guard.

AI Review Report

Reviewed adversarially (review-until-clean) treating the original community author as potentially malicious, since this is an adopted PR:

  • Correctness: explicit idle only bypasses the quiet window when the latest OSC title itself passes detectExplicitIdleStatusFromTitle; the broad default-to-idle name-only path cannot use that fast path. Ambiguous idle still depends on lastOutputAt and TUI_IDLE_QUIESCENCE_MS, and the one-shot quiet timer re-checks live state before resolving.
  • Waiter coverage: all title-derived satisfaction sites now flow through hasSatisfiedTuiIdle or the existing explicit renderer-title/ready-prompt checks. Waiter cleanup clears the new quiet timer alongside timeout, poll interval, and abort cleanup.
  • Message delivery: push-on-idle orchestration delivery now uses the same confirmed-idle boundary, preventing a queued message from being injected while a name-only idle-looking title is still streaming output.
  • Edge cases: body-level prompt detection remains immediate; explicit renderer/adopted title readiness remains immediate when there is no PTY stream to debounce; unsupported CLIs still time out predictably.
  • Cross-platform (macOS / Linux / Windows): pure timing/title logic in the Node main process — no keyboard shortcuts, path handling, shell invocation, or platform branches touched.
  • SSH / remote: lastOutputAt is set from PTY data regardless of local vs remote runtime, so the ambiguous-title guard behaves identically over SSH.
  • Git provider: terminal-level change; provider-agnostic, no GitHub/GitLab-specific code.

Security Audit

No security-relevant surface changed. No new dependencies, no eval, no command execution, no path handling, no auth/secret access, and no network or IPC changes. The diff only adjusts in-memory title/readiness checks, timers, and the existing push-on-idle delivery gate. Re-audited the adopted community change for exfiltration / injection / traversal / suspicious deps: none found. No follow-up needed.

Notes

  • Supersedes fix(runtime): require sustained quiescence for tui-idle waits #6012 (kept open for the original author; credited via Co-authored-by:).
  • Follow-up suggestion (out of scope here): tighten detectAgentStatusFromTitle's broad default-to-idle so name-only agent titles don't classify as idle in the first place. Left alone here because it's a renderer-shared classifier driving status badges and could shift UI semantics; the confirmed-idle guard neutralizes its tui-idle impact without that risk.

Made with Orca 🐋

`terminal wait --for tui-idle` returned satisfied:true in ~0s while an
agent TUI was actively working. detectAgentStatusFromTitle defaults a
name-only agent title (e.g. "Codex", "Devin", "aider") to 'idle' because
the spinner / "esc to interrupt" live in the terminal body, not the OSC
title. The tui-idle wait trusted that single instantaneous title sample,
bypassing the existing TUI_IDLE_QUIESCENCE_MS debounce (only consulted in
the foreground-process fallback, which runs when lastAgentStatus is null).

Require *sustained* idle: a title-derived 'idle' satisfies tui-idle only
after PTY output has been quiet for TUI_IDLE_QUIESCENCE_MS. Two helpers
(isTuiOutputActive, hasSustainedTuiIdle) replace the bare
`lastAgentStatus === 'idle'` checks at every title-derived satisfaction
site (sync fast paths, in-promise checks, fallback polls, and the
working->idle transition resolvers). Body-level ready-prompt detection
and adopted/daemon handles with no PTY output stream keep their immediate
title signal.

Fixes #6011.

Co-authored-by: Revofusion <[email protected]>
@nwparker

Copy link
Copy Markdown
Contributor Author

Verification evidence

1. New regression test FAILS without the source fix (proves it catches the bug)

Stashed the orca-runtime.ts change only (keeping the new test), then ran the new test against unpatched source:

 FAIL  src/main/runtime/orca-runtime.test.ts > OrcaRuntimeService > does not report tui-idle while a name-only agent title keeps streaming output
AssertionError: expected true to be false // Object.is equality
- Expected
+ Received
- false
+ true
 ❯ src/main/runtime/orca-runtime.test.ts:7513:23
    7511|         await vi.advanceTimersByTimeAsync(2_000)
    7512|       }
    7513|       expect(settled).toBe(false)
 Test Files  1 failed (1)
      Tests  1 failed | 486 skipped (487)

settled became true mid-work — the wait resolved in ~0s while the Codex (name-only → defaults to idle) agent was still streaming output. Exactly the reported bug.

2. Focused tui-idle suite PASSES with the fix (15 = 14 prior + 1 new)

./node_modules/.bin/vitest run --config config/vitest.config.ts src/main/runtime/orca-runtime.test.ts -t "tui-idle"

 Test Files  1 passed (1)
      Tests  15 passed | 472 skipped (487)
   Duration  2.24s

3. Full test file PASSES — no regressions

./node_modules/.bin/vitest run --config config/vitest.config.ts src/main/runtime/orca-runtime.test.ts

 Test Files  1 passed (1)
      Tests  487 passed (487)
   Duration  6.46s

Includes the previously-cited adopted-title, Codex/Antigravity ready-prompt, and quiet-background-agent tests — all green.

4. Lint + typecheck clean

$ ./node_modules/.bin/oxlint src/main/runtime/orca-runtime.ts src/main/runtime/orca-runtime.test.ts
OXLINT_EXIT: 0   (no output)

$ ./node_modules/.bin/tsgo --noEmit -p config/tsconfig.node.json
TSGO_EXIT: 0

Before / after reasoning

  • Before: any title-derived idle (which a name-only busy agent produces, since detectAgentStatusFromTitle defaults to idle) immediately satisfied tui-idle. The TUI_IDLE_QUIESCENCE_MS=3000 debounce was bypassed because it only ran in the lastAgentStatus === null foreground fallback.
  • After: hasSustainedTuiIdle requires lastAgentStatus === 'idle' AND output quiet for >= TUI_IDLE_QUIESCENCE_MS. While the agent streams, lastOutputAt stays recent so the wait blocks; once the agent finishes and output goes quiet, the debounce elapses and the wait resolves within one poll interval.

@coderabbitai

coderabbitai Bot commented Jun 28, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The tui-idle waiter condition in orca-runtime.ts now requires sustained output quiescence before resolving. Two private helpers are introduced: isTuiOutputActive (checks lastOutputAt against TUI_IDLE_QUIESCENCE_MS) and hasSustainedTuiIdle (combines lastAgentStatus === 'idle' with the output-quiet check). These helpers replace bare lastAgentStatus === 'idle' checks across all waiter resolution and polling paths for PTY, leaf, PTY-live, and live-leaf targets. A new end-to-end test verifies the waiter stays unsettled while PTY output chunks keep arriving and resolves only after the quiescence window elapses.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main runtime change: requiring sustained quiescence for tui-idle waits.
Linked Issues check ✅ Passed The changes implement sustained idle gating for title-derived tui-idle and preserve the stated prompt/no-PTY exceptions.
Out of Scope Changes check ✅ Passed The diff stays within the runtime waiter fix and its regression test, with no obvious unrelated additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description check ✅ Passed The PR description includes all required sections with sufficient detail, including summary, screenshots, testing, review, security, and notes.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nwparker

Copy link
Copy Markdown
Contributor Author

Follow-up pushed in 8c7e4b441d to reduce the reported tui-idle tradeoff.

What changed:

  • Explicit OSC idle titles (for example Codex done, Claude ✳ ..., Gemini/Pi idle markers) now satisfy terminal wait --for tui-idle immediately again. They bypass the output-quiescence debounce because they are the agent's explicit completion signal.
  • Ambiguous default-idle titles (for example name-only Codex / Devin / aider) still require output quiescence to preserve the terminal wait --for tui-idle returns satisfied:true in ~0s while an agent TUI is actively working #6011 fix, but the waiter now uses an exact one-shot quiet timer instead of waiting for the next 2s fallback poll. That bounds the remaining unavoidable delay to the existing TUI_IDLE_QUIESCENCE_MS window instead of quiet window + poll slack.
  • Push-on-idle orchestration message delivery now uses the same confirmed-idle boundary, so a name-only idle-looking title cannot inject queued text while output is still streaming.

Tradeoff status: zero added latency for explicit idle signals and ready-prompt body detection. The only remaining latency is for ambiguous name-only titles, where resolving immediately would reintroduce the original false-positive bug.

Validation:

  • ./node_modules/.bin/vitest run --config config/vitest.config.ts src/main/runtime/orca-runtime.test.ts -t "tui-idle|push-on-idle|already-idle agent|Cursor Agent without auto-submitting" — 20 passed
  • ./node_modules/.bin/vitest run --config config/vitest.config.ts src/main/runtime/orca-runtime.test.ts — 490 passed
  • ./node_modules/.bin/oxfmt --check src/main/runtime/orca-runtime.ts src/main/runtime/orca-runtime.test.ts — clean
  • ./node_modules/.bin/oxlint src/main/runtime/orca-runtime.ts src/main/runtime/orca-runtime.test.ts — clean
  • ./node_modules/.bin/tsgo --noEmit -p config/tsconfig.node.json — clean

nwparker added a commit that referenced this pull request Sep 12, 2026
…ilence

`terminal wait --for tui-idle` returned satisfied in ~0s while an agent was
mid-turn. The shared title detector defaults a name-only agent title to `idle`
so the sidebar can clear a stale spinner, and the wait accepted that stored
value as completion.

Rank the evidence instead. An explicit idle marker in the agent's own title or
a known ready prompt settles the wait; a fresh first-party OSC 9999 status
saying working/blocked/waiting vetoes it; a name-only title is a last resort
that settles only once the stream has also gone quiet. The rank is derived at
read time from `lastOscTitle` rather than stamped onto the record, because
`syncWindowGraph` rebuilds leaves from an explicit field list and would drop a
bespoke provenance field on any renderer publish.

Two things the ranking alone gets wrong are handled here too. A quiet non-shell
foreground process no longer proves idle on a pane where Orca launched a known
agent — that is an agent still booting, and resolving on it is what let
`dispatch --inject` lose the prompt (#9976). And the idle poll re-reads the live
leaf each tick, because a record captured at registration stops advancing and
its frozen `lastOutputAt` makes the quiescence gate pass while the pane streams.

The demotion is scoped to agents that go on to announce rest explicitly. Grok,
Copilot, Aider, Mimo, agy and OpenCode emit their name and nothing more at rest:
a real idle Grok pane repaints its banner about four times a second forever, so
demanding quiescence from it left no settle signal at all and the wait ran to
timeout.

The design is Brennan Benson's, from #14642, which won a cross-review against
#6012, #6555 and this branch's earlier approach; it is ported here only because
that branch shares no git history with main and cannot be merged. Neil's #6555
first drew the explicit-vs-ambiguous line the ranking rests on, and Revofusion's
#6012 first identified that a single title sample cannot prove completion.

Fixes #6011

Co-authored-by: Brennan Benson <[email protected]>
Co-authored-by: Neil <[email protected]>
Co-authored-by: Revofusion <[email protected]>
nwparker added a commit that referenced this pull request Sep 13, 2026
…ilence

`terminal wait --for tui-idle` returned satisfied in ~0s while an agent was
mid-turn. The shared title detector defaults a name-only agent title to `idle`
so the sidebar can clear a stale spinner, and the wait accepted that stored
value as completion.

Rank the evidence instead. An explicit idle marker in the agent's own title or
a known ready prompt settles the wait; a fresh first-party OSC 9999 status
saying working/blocked/waiting vetoes it; a name-only title is a last resort
that settles only once the stream has also gone quiet. The rank is derived at
read time from `lastOscTitle` rather than stamped onto the record, because
`syncWindowGraph` rebuilds leaves from an explicit field list and would drop a
bespoke provenance field on any renderer publish.

Two things the ranking alone gets wrong are handled here too. A quiet non-shell
foreground process no longer proves idle on a pane where Orca launched a known
agent — that is an agent still booting, and resolving on it is what let
`dispatch --inject` lose the prompt (#9976). And the idle poll re-reads the live
leaf each tick, because a record captured at registration stops advancing and
its frozen `lastOutputAt` makes the quiescence gate pass while the pane streams.

The demotion is scoped to agents that go on to announce rest explicitly. Grok,
Copilot, Aider, Mimo, agy and OpenCode emit their name and nothing more at rest:
a real idle Grok pane repaints its banner about four times a second forever, so
demanding quiescence from it left no settle signal at all and the wait ran to
timeout.

The design is Brennan Benson's, from #14642, which won a cross-review against
#6012, #6555 and this branch's earlier approach; it is ported here only because
that branch shares no git history with main and cannot be merged. Neil's #6555
first drew the explicit-vs-ambiguous line the ranking rests on, and Revofusion's
#6012 first identified that a single title sample cannot prove completion.

Fixes #6011

Co-authored-by: Brennan Benson <[email protected]>
Co-authored-by: Neil <[email protected]>
Co-authored-by: Revofusion <[email protected]>
nwparker added a commit that referenced this pull request Sep 13, 2026
…ilence (#20155)

`terminal wait --for tui-idle` returned satisfied in ~0s while an agent was
mid-turn. The shared title detector defaults a name-only agent title to `idle`
so the sidebar can clear a stale spinner, and the wait accepted that stored
value as completion.

Rank the evidence instead. An explicit idle marker in the agent's own title or
a known ready prompt settles the wait; a fresh first-party OSC 9999 status
saying working/blocked/waiting vetoes it; a name-only title is a last resort
that settles only once the stream has also gone quiet. The rank is derived at
read time from `lastOscTitle` rather than stamped onto the record, because
`syncWindowGraph` rebuilds leaves from an explicit field list and would drop a
bespoke provenance field on any renderer publish.

Two things the ranking alone gets wrong are handled here too. A quiet non-shell
foreground process no longer proves idle on a pane where Orca launched a known
agent — that is an agent still booting, and resolving on it is what let
`dispatch --inject` lose the prompt (#9976). And the idle poll re-reads the live
leaf each tick, because a record captured at registration stops advancing and
its frozen `lastOutputAt` makes the quiescence gate pass while the pane streams.

The demotion is scoped to agents that go on to announce rest explicitly. Grok,
Copilot, Aider, Mimo, agy and OpenCode emit their name and nothing more at rest:
a real idle Grok pane repaints its banner about four times a second forever, so
demanding quiescence from it left no settle signal at all and the wait ran to
timeout.

The design is Brennan Benson's, from #14642, which won a cross-review against
#6012, #6555 and this branch's earlier approach; it is ported here only because
that branch shares no git history with main and cannot be merged. Neil's #6555
first drew the explicit-vs-ambiguous line the ranking rests on, and Revofusion's
#6012 first identified that a single title sample cannot prove completion.

Fixes #6011

Co-authored-by: Brennan Benson <[email protected]>
Co-authored-by: Neil <[email protected]>
Co-authored-by: Revofusion <[email protected]>
@nwparker

Copy link
Copy Markdown
Contributor Author

Closing this — superseded by e9065dd, which fixes #6011. You are a co-author on that commit.

This PR drew the line the landed fix rests on. hasExplicitOscTuiIdle letting an explicit OSC idle marker bypass the quiescence debounce, so that only ambiguous name-only titles have to wait, is the explicit-vs-ambiguous distinction that #14642 later generalised into a full evidence ranking and that we ported. #6012 correctly identified that a single title sample cannot prove completion; this PR was the first to say which title samples can.

Two things here turned out to be structurally important:

  1. Deriving explicitness at read time from lastOscTitle rather than storing a new field. Our own first attempt stamped provenance onto the record and it silently decayed — syncWindowGraph rebuilds every leaf from an explicit field list, so the stamp was dropped on any renderer publish and terminal wait --for tui-idle returns satisfied:true in ~0s while an agent TUI is actively working #6011 returned in full: measured, the wait correctly refused before a graph sync and returned satisfied=true in 0ms after it. Your read-time derivation cannot decay, because lastOscTitle is copied.
  2. Keeping explicit markers on the fast path, so an agent that has already said it is done is not made to wait out a debounce.

The remaining gap, which applies to every quiescence-gated approach including ours: a real idle grok pane repaints its banner about four times a second forever (max gap 891ms over 22s), so a name-only title from an agent with no explicit idle channel can never clear a quiescence gate — a measured 45s timeout against 1ms before. The landed fix scopes the demotion to agents that go on to announce rest explicitly.

Also mechanical: this branch shares no git history with main (different root commit; git merge refuses unrelated histories), as do #6012, #9929 and #14642 — which is why the fix landed as a port with co-authorship rather than as one of these branches.

@nwparker nwparker closed this Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

terminal wait --for tui-idle returns satisfied:true in ~0s while an agent TUI is actively working

1 participant