orca icon indicating copy to clipboard operation
orca copied to clipboard

fix(agent-status): don't fire "Task complete" while a Codex tool is still running

Open choihjin opened this issue 4 months ago • 1 comments

Summary

Codex emits a Stop hook between intermediate responses within a single turn, not only at the end of a request. The completion coordinator treated every done as end-of-turn, so a multi-step Codex task fired "Task complete" repeatedly while it was still working — the user got a notification, went to look, and found the agent mid-tool-call.

The fix defers a hook done while the pane is still producing output: if more output arrives within HOOK_DONE_OUTPUT_QUIET_MS (1.5s), the done is treated as a mid-task turn boundary and the timer re-arms. Critically, the deferral is bounded by HOOK_DONE_MAX_DEFER_MS (15s), so the notification can be delayed but never swallowed.

Closes #9333

ELI5

Codex says "I'm done" several times while working through one request — it means "done with this step", but Orca heard "done with everything" and pinged you each time. Now Orca waits a moment: if Codex keeps typing, it knows that wasn't really the end and stays quiet. And there's a hard 15-second limit on that waiting, so if something goes strange you still get told your task finished — the worst outcome would be silence, not an early ping.

Proof of fix

On origin/main (test kept, coordinator reverted) — fails:

$ git checkout origin/main -- src/renderer/src/components/terminal-pane/agent-completion-coordinator.ts
$ npx vitest run --config config/vitest.config.ts \
    src/renderer/src/components/terminal-pane/agent-completion-coordinator.test.ts

 × defers a Codex hook done while the pane is still producing output (#9333)

      Tests  1 failed | 74 passed (75)

With this PR:

$ git checkout HEAD -- src/renderer/src/components/terminal-pane/agent-completion-coordinator.ts
$ npx vitest run --config config/vitest.config.ts \
    src/renderer/src/components/terminal-pane/agent-completion-coordinator.test.ts

 Test Files  1 passed (1)
      Tests  75 passed (75)

Three tests were added, and the second is the one that matters most for review:

  • defers a Codex hook done while the pane is still producing output (#9333) — the bug itself.
  • still fires a Codex done when the agent TUI never stops redrawing (#9333) — the guard against the mirror-image bug. If a pane never goes quiet, the 15s ceiling fires the notification anyway. Silently swallowing a real completion would be worse than the spurious one being fixed, because the user waits forever with no signal; this test pins that it cannot happen.
  • fires a Codex done exactly once when Stop repeats across a turn (#9333) — covers the "repeated" half of the reported symptom, not just the timing.

Proof of no regressions

$ npx vitest run --config config/vitest.config.ts src/renderer/src/components/terminal-pane/
 Test Files  186 passed (186)
      Tests  2434 passed (2434)

$ npx tsc --noEmit -p config/tsconfig.tc.web.json      # exit 0
$ npx oxlint src/renderer/src/components/terminal-pane/agent-completion-coordinator.ts \
             src/renderer/src/components/terminal-pane/agent-completion-coordinator.test.ts
(no findings)

User-visible behavior that changes:

  • A Codex "Task complete" notification may now arrive up to 1.5s later than before (longer only while output keeps arriving, hard-capped at 15s). This is the intended trade: a slightly later notification that is correct, instead of an immediate one that is wrong.
  • Repeated mid-turn Stop signals no longer produce repeated notifications.
  • No completion is ever dropped. The deferral re-arms a timer; it does not cancel the dispatch.

Agents other than Codex are unaffected in practice: the deferral only engages when a hook done is followed by continued output within the quiet window, which is the Codex intermediate-Stop pattern. A turn that genuinely ends goes quiet and dispatches on the existing path.

Compatibility

  • Platforms: macOS / Linux / Windows. Pure renderer-side timer/state logic with no platform branch, no paths, no shell behavior, no Electron API, and no key handling.
  • SSH / remote: hook events arrive over the relay and can be delayed or dropped. This is precisely why the change is a bounded deferral rather than a gate: a missing follow-up event cannot strand the notification, because HOOK_DONE_MAX_DEFER_MS dispatches regardless. A dropped intermediate Stop simply means the notification fires on the normal path.
  • Performance: No hot-path change. observeOutputActivity() gains a single Date.now() assignment; there is no added allocation, listener, subscription, or re-render trigger. The deferral reuses the existing pendingHookDoneTimer rather than adding a new one, so no extra timer is created per pane.

Screenshots

No visual change beyond when an OS notification appears. The behavior is asserted in agent-completion-coordinator.test.ts with fake timers — including the never-swallow ceiling — rather than captured as an image, so the guarantee is enforced in CI. I did not record a screen capture of a live Codex turn; noting that plainly rather than implying a manual check that was not performed.

Testing

  • [ ] pnpm lint — not run in full. Ran npx oxlint on both changed files: no findings.
  • [x] pnpm typecheck — npx tsc --noEmit -p config/tsconfig.tc.web.json, exit 0.
  • [ ] pnpm test — not run in full (30+ min). Ran the whole touched directory: src/renderer/src/components/terminal-pane/ → 186 files, 2434 tests passed.
  • [ ] pnpm build — not run.
  • [x] Added or updated high-quality tests that would catch regressions — three tests covering the deferral, the never-swallow ceiling, and single-fire dedupe across repeated Stops.

AI Review Report

Reviewed with Claude Code (Opus 5) and independently re-verified. Risks checked:

  • Swallowing a real completion — the dominant risk. A guard that waits for "the tool to finish" can wait forever if the tool crashes, is killed, produces no terminating marker, or its event is dropped over SSH. The user then gets no notification at all, which is worse than the spurious one. Verified the design is a bounded deferral with a hard HOOK_DONE_MAX_DEFER_MS ceiling, and pinned it with a dedicated test (never stops redrawing). The completion is delayed, never cancelled.
  • Timer accounting: the deferral re-arms the existing pendingHookDoneTimer and resets pendingHookDoneDeadline in the same clear path as the rest of the pending state, so there is no orphaned timer or leaked deadline when a pane closes mid-defer.
  • Deadline is set once per pending done (if (pendingHookDoneDeadline === 0)), so continued output extends the quiet check but cannot push the ceiling forward indefinitely — otherwise a chatty pane would defer forever, reintroducing the swallow.
  • Repeated-Stop dedupe: the reported symptom is repeated notifications, so single-fire across a turn is tested explicitly rather than assumed from the timing fix.
  • Agent scoping: the file is a generic coordinator. Verified the deferral engages only on the hook-done-then-more-output pattern and that the full 2434-test terminal-pane suite — which covers the other agents' completion paths — is unchanged.
  • Cross-platform (macOS / Linux / Windows): no platform-dependent code, shortcut labels, paths, or shell behavior in this diff.

Security Audit

  • Input handling: none added. The change reads existing hook payloads and an output-activity timestamp; no new parsing of untrusted terminal bytes.
  • Command execution: none. This only affects when a local notification is dispatched.
  • Path handling: none.
  • Auth / secrets: none touched. Note the notification payload is unchanged — this PR alters timing only, not content, so nothing new is surfaced to the OS notification layer.
  • Dependencies: none added or changed.
  • IPC: no new channel or payload.

Notes

  • The constants are deliberately conservative: 1.5s matches the existing HOOK_DONE_QUIET_MS cadence already used in this file, and 15s is a ceiling rather than a tuning knob — it exists to make "never swallow" structurally true, not to be hit in normal operation.
  • Adjacent in-flight work touching neighbouring agent-status paths, worth sequencing against: #10116 (SSH interrupt inference) and #9716 (dead-PTY writes). No file overlap with either, but all three shape when a pane's agent status changes.

Original patch by @choihjin.

choihjin avatar Jul 20 '26 03:07 choihjin

Review Change Stack

📝 Walkthrough

Walkthrough

The coordinator now tracks the freshness and presence of child processes from process inspections. Scheduled Codex hook-done completions are rescheduled while recent inspection evidence shows an active child process, then dispatched after the child exits and the quiet window passes. A regression test verifies that completion remains pending during active tool execution and emits once afterward.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is concise and accurately summarizes the main fix: preventing premature Task complete notifications during active Codex work.
Description check ✅ Passed The description covers all required template sections and includes testing, review, security, screenshots, and notes.

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.

coderabbitai[bot] avatar Jul 20 '26 03:07 coderabbitai[bot]