Skip to content

Stop PermissionRequest/blocked/waiting hook states from firing agent-task-complete - #6540

Merged
brennanb2025 merged 2 commits into
mainfrom
brennanb2025/issue-5698-permhook-task-complete
Jun 28, 2026
Merged

brennanb2025 merged 2 commits into
mainfrom
brennanb2025/issue-5698-permhook-task-complete

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

Fixes #5698

Root cause

isCompletionHookState in src/renderer/src/components/terminal-pane/agent-completion-coordinator.ts returned true for 'done' || 'waiting' || 'blocked'. But 'waiting' (e.g. a Claude PermissionRequest) and 'blocked' (e.g. a Copilot elicitation dialog) are mid-turn states — the agent is still alive and has not completed. Treating them as completion made the coordinator dispatch a false agent-task-complete notification on every permission prompt / tool pause.

The "needs you" attention notification for waiting/blocked is raised on a separate path (smart-attention.ts / notifications.ts), so it was already covered — the coordinator was double-counting these states as completions.

Change

  • isCompletionHookState now matches only a genuine 'done'.
  • New isAttentionHookState ('waiting' | 'blocked') handled by an early return in observeHookStatus that cancels any pending done-quiet timer (clearPendingHookDone()), so a pause arriving before the 1.5s quiet window can never resolve into a false completion. Recognized-agent evidence is still established at the top of observeHookStatus, so attention states keep updating agent identity.
  • Regression tests: waiting mid-turn -> 0 completions; blocked mid-turn -> 0 completions; done-then-waiting-before-quiet-window -> pending done cancelled, 0 completions. (Replaces the old test that asserted the buggy 2-dispatch behavior.)

Confined to agent-completion-coordinator.ts + its test. No change to the separate attention-notification path.

Evidence

Regression tests FAIL before the fix (against unmodified source)

 × does not dispatch completion when waiting states arrive mid-turn
 × does not dispatch completion when a blocked state arrives mid-turn
 × cancels a pending done timer when a waiting state arrives before the quiet window
⎯⎯ Failed Tests 3 ⎯⎯
AssertionError: expected "vi.fn()" to not be called at all, but actually been called 1 times
 Test Files  1 failed (1)
      Tests  3 failed | 43 passed (46)

After the fix — full coordinator suite + downstream notification suites PASS

$ pnpm vitest run --config config/vitest.config.ts \
    agent-completion-coordinator.test.ts \
    agent-completion-coordinator-dispose-leak.test.ts \
    use-notification-dispatch.test.ts \
    agent-hook-completion-notifications.test.ts

 Test Files  4 passed (4)
      Tests  79 passed (79)

Lint (changed files clean)

$ pnpm lint
oxlint && lint:switch-exhaustiveness && check-styled-scrollbars && verify:localization-catalog && verify:localization-coverage
# no errors/warnings on agent-completion-coordinator.ts or its test
# (the single useGitStatusPolling.ts warning is pre-existing and untouched)
Localization coverage check passed with 0 allowlisted candidates.

Typecheck (clean)

$ pnpm typecheck
tsgo --noEmit -p config/tsconfig.node.json && tsgo --noEmit -p config/tsconfig.tc.cli.json && tsgo --noEmit -p config/tsconfig.tc.web.json
# exit 0, no diagnostics

Re-implements the approach of community PR #6316 (rodboev), with a broader test run (full coordinator + downstream notification suites) and added "Why:" comments.

…task-complete

'waiting' (e.g. a Claude PermissionRequest) and 'blocked' (e.g. a Copilot
elicitation dialog) occur mid-turn while the agent is still alive, yet
isCompletionHookState treated them as completion and dispatched a false
agent-task-complete. Restrict completion to a genuine 'done' and add an
isAttentionHookState early return that also cancels a pending done-quiet
timer when an attention pause arrives first. The 'needs you' notification
for these states is raised separately (smart-attention), so it is unaffected.

Fixes #5698
@coderabbitai

coderabbitai Bot commented Jun 28, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

A new AgentAttentionDispatchMeta type is added to the coordinator types, and AgentCompletionCoordinatorOptions gains an optional dispatchAttention callback. The coordinator's hook-state handling is split: waiting and blocked states no longer trigger completion dispatch; instead they invoke a new dispatchAttention helper that deduplicates via lastAttentionToken. The working branch and resetCompletionState clear lastAttentionToken. This attention path is wired in pty-connection.ts and agent-hook-completion-notifications.ts, and covered by new tests for mid-turn waiting, blocked, and timer-cancellation scenarios.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the bug and fix, but it omits most required template sections, including Screenshots, AI Review Report, Security Audit, and Notes. Add the missing template sections and, where relevant, include screenshots or state 'No visual change' plus testing, review, security, and notes.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main fix: suppressing agent-task-complete for waiting/blocked permission states.
Linked Issues check ✅ Passed The changes match #5698 by treating waiting/blocked as mid-turn states, preserving completion on done, and adding regression tests for the required sequences.
Out of Scope Changes check ✅ Passed The extra type and notification wiring appear necessary to implement the attention-state fix and regression coverage, with no unrelated feature work evident.
✨ 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.

@brennanb2025

Copy link
Copy Markdown
Contributor Author

🧒 ELI5 — for your merge decision

When Claude asked for permission mid-task (a "PermissionRequest" hook), Orca wrongly fired an "agent task complete" notification, so you'd get a false "done" ping while the agent was actually still waiting on you. This PR makes completion fire only on a genuine finish, and cancels a pending "done" if a waiting/blocked state arrives first.

What you'll notice: No more false "agent finished" notifications when an agent pauses for a permission prompt mid-task. Real completions still notify once.

Behavior change Behavior change is the fix itself: 'waiting'/'blocked' states no longer count as completion. Real completions still fire exactly once after the quiet window. Scoped to the completion-coordinator logic; no setting, no user action.

Merge risk: low

Auto-generated summary to aid review. Reproduction + lint/typecheck/test evidence is in the PR body / checks. This PR was produced and independently reviewed by an automated bug-bash; please verify before merging.

@brennanb2025

Copy link
Copy Markdown
Contributor Author

Thanks for the fix. I added a maintainer follow-up so PermissionRequest/blocked/waiting states no longer fire false completion notifications, but still raise the needs-input attention notification for Claude-style permission waits.

Validated with targeted tests, typecheck/lint, and Electron validation of a Claude waiting state.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 88f5536f-30ac-4690-9db7-7265b22d9090

📥 Commits

Reviewing files that changed from the base of the PR and between bed5060 and 5e2b222.

📒 Files selected for processing (6)
  • src/renderer/src/components/terminal-pane/agent-completion-coordinator-types.ts
  • src/renderer/src/components/terminal-pane/agent-completion-coordinator.test.ts
  • src/renderer/src/components/terminal-pane/agent-completion-coordinator.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts
  • src/renderer/src/hooks/agent-hook-completion-notifications.test.ts
  • src/renderer/src/hooks/agent-hook-completion-notifications.ts

Comment on lines +51 to +56
function isAttentionHookState(state: ParsedAgentStatusPayload['state']): boolean {
// Why: 'waiting' (e.g. a Claude PermissionRequest) and 'blocked' (e.g. a
// Copilot elicitation dialog) pause mid-turn — the agent is still alive and
// has not completed, so they must not fire agent-task-complete. The "needs
// you" notification for these states is raised separately (smart-attention).
return state === 'waiting' || state === 'blocked'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift

Don't make blocked globally non-terminal.

The linked objective explicitly says blocked should only skip completion when it means “waiting for user.” This helper now routes every blocked payload through the attention path, so any agent that uses blocked as a terminal/failure state will never emit agent-task-complete. Gate blocked on agent-specific semantics or an explicit “needs user input” signal instead.

let lastCompletedTurn: number | null = null
let lastCompletionSource: CompletionSource | null = null
let lastCompletionIdentity: LastCompletionIdentity | null = null
let lastAttentionToken: string | null = null

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Attention dedup resets on live coordinator recreation.

lastAttentionToken is instance-local, but this file already preserves completion dedup across live remounts because hook snapshots can be replayed on activation/switching. Recreating the coordinator while a pane is still waiting/blocked can therefore dispatch the same attention notification again. Mirror the pane-key-scoped completion dedup if this spam guard is meant to survive remounts too.

Also applies to: 277-290

@brennanb2025
brennanb2025 merged commit 211138e into main Jun 28, 2026
1 check passed
@brennanb2025
brennanb2025 deleted the brennanb2025/issue-5698-permhook-task-complete branch June 28, 2026 08:50
chris-reusch pushed a commit to chris-reusch/undercoverorca that referenced this pull request Jun 28, 2026
…task-complete (stablyai#6540)

* Stop PermissionRequest/blocked/waiting hook states from firing agent-task-complete

'waiting' (e.g. a Claude PermissionRequest) and 'blocked' (e.g. a Copilot
elicitation dialog) occur mid-turn while the agent is still alive, yet
isCompletionHookState treated them as completion and dispatched a false
agent-task-complete. Restrict completion to a genuine 'done' and add an
isAttentionHookState early return that also cancels a pending done-quiet
timer when an attention pause arrives first. The 'needs you' notification
for these states is raised separately (smart-attention), so it is unaffected.

Fixes stablyai#5698

* Preserve attention notifications for waiting hooks

---------

Co-authored-by: brennanb2025 <[email protected]>
Co-authored-by: Neil <[email protected]>
(cherry picked from commit 211138e)
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…task-complete (stablyai#6540)

* Stop PermissionRequest/blocked/waiting hook states from firing agent-task-complete

'waiting' (e.g. a Claude PermissionRequest) and 'blocked' (e.g. a Copilot
elicitation dialog) occur mid-turn while the agent is still alive, yet
isCompletionHookState treated them as completion and dispatched a false
agent-task-complete. Restrict completion to a genuine 'done' and add an
isAttentionHookState early return that also cancels a pending done-quiet
timer when an attention pause arrives first. The 'needs you' notification
for these states is raised separately (smart-attention), so it is unaffected.

Fixes stablyai#5698

* Preserve attention notifications for waiting hooks

---------

Co-authored-by: brennanb2025 <[email protected]>
Co-authored-by: Neil <[email protected]>
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.

[Bug]: Claude PermissionRequest hook dispatches "agent-task-complete" notification while agent is still waiting on user

2 participants