Skip to content

fix(terminal): retire resume state after confirmed agent exit - #14534

Closed
erishforG wants to merge 1 commit into
stablyai:mainfrom
erishforG:fix/14228-retire-clean-agent-exit
Closed

erishforG wants to merge 1 commit into
stablyai:mainfrom
erishforG:fix/14228-retire-clean-agent-exit

Conversation

@erishforG

@erishforG erishforG commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

When a user exits Claude normally and later closes its shell, Orca should not bring that finished agent back after an app restart.

What Changed

  • Clear the resolved pane resume record when foreground-process evidence confirms that the agent returned to the shell.
  • Clear legacy aliases that claim the same provider session.
  • Keep sibling pane recovery records untouched.
  • Preserve existing recovery behavior for unexpected PTY loss.
  • Add regression coverage for confirmed agent exit followed immediately by PTY teardown, legacy aliases, exact-pane isolation, and cleanup ordering.

Why

A bare PTY exit is ambiguous and may represent a recoverable crash, so Orca intentionally preserves resume authority in that path. The existing agent-exited fact is emitted only after foreground-process verification confirms that the agent has returned to the shell. That confirmed transition is the safe point to retire the pane resume authority.

Without this cleanup, the persisted sleeping-session record can survive shell exit and cause a deliberately finished Claude session to be resumed after restarting Orca.

Prior work: #14376 explored Claude SessionEnd hooks but was closed after verification showed that /exit does not emit SessionEnd. Its conclusion was that process-exit or liveness evidence must be the real backstop; this PR uses the existing process-confirmed agent-exited fact.

Linked Issue

Fixes #14228

Visual Proof

After fix on macOS: exit Claude, exit the shell, restart Orca, and verify that the completed Claude session is not resumed.

fixed_record_public.mp4

The recording uses an isolated local project. Account email and provider session identifiers were redacted before upload.

Testing

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

Manual verification on macOS:

  1. Started Claude Code in a local project.
  2. Exited Claude and then exited the shell.
  3. Restarted Orca with the same isolated profile.
  4. Verified that no Claude resume process started and only a normal shell was restored.

Automated verification:

  • affected renderer and terminal lifecycle suites: 667 tests passed
  • focused final impact suite after review fixes: 588 tests passed
  • pnpm lint
  • pnpm typecheck
  • pnpm build
  • formatting, oxlint, and diff checks

The full local suite reaches pre-existing machine-specific Codex HOME/auth and updater failures; the affected suites are green and CI will run in a clean repository environment.

AI Disclosure

OpenAI Codex was used to assist with investigation, implementation, test drafting, and review. I reviewed the focused diff and manually verified the end-to-end behavior.

Review

  • Security: no new external input or privilege boundary.
  • Cross-platform: renderer lifecycle state cleanup only; no OS-specific implementation.
  • Remote SSH: uses the existing process-confirmed agent-exited fact and resolved pane key.
  • Mobile: no mobile-specific path changed.
  • Backwards compatibility: unexpected PTY loss remains recoverable.
  • Performance: exact-key store deletion plus bounded duplicate-alias cleanup on confirmed agent exit.

Checklist

  • This PR is small and focused
  • I explained what changed and why (including ELI5)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

The affected test suites, lint, typecheck, and build pass locally. Repository CI will cover the clean full-suite environment because the local full suite encounters unrelated machine-specific Codex authentication and updater fixtures.

Author

@coderabbitai

coderabbitai Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7bda2d59-c66a-4a1a-90ac-fd41da19b6fc

📥 Commits

Reviewing files that changed from the base of the PR and between a7a4f5d and c936b3c.

📒 Files selected for processing (3)
  • src/renderer/src/components/terminal-pane/pty-connection-main-side-effect-authority.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection/agent-idle-working-handlers.ts
  • src/renderer/src/store/slices/terminal-tab-retirement-store.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/renderer/src/store/slices/terminal-tab-retirement-store.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection-main-side-effect-authority.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

onAgentExited now clears the affected pane’s sleeping-record provider duplicates before the exit callback runs. Tests verify cleanup of the confirmed pane, preservation of sibling-pane records, and callback ordering after immediate PTY teardown. A terminal retirement test description now identifies preservation of resume authority after unexpected PTY loss.

Merge Risk: ⚪ Minimal · up to c936b

This change retires resume state only after confirmed agent exit while preserving recovery for unexpected PTY loss; no actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes retiring resume state after confirmed agent exit.
Description check ✅ Passed The description covers the change, rationale, linked issue, testing, risks, and checklist; visual proof lacks an explicit before recording.
Linked Issues check ✅ Passed The changes retire cleanly exited agent records, clear related aliases, preserve sibling records, and retain recovery after unexpected PTY loss for issue [#14228].
Out of Scope Changes check ✅ Passed All changes are limited to the requested terminal resume-state cleanup, regression coverage, and a related test rename.
Docstring Coverage ✅ Passed Docstring check was indeterminate for this PR — some files could not be analyzed in time. Not blocking.

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 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 Plus

Run ID: f30fdb2a-0b49-4a7a-81ee-882ada97b90b

📥 Commits

Reviewing files that changed from the base of the PR and between cb42b60 and 24e94f858a32ffb1f877d8daa24148d5b8f04865.

📒 Files selected for processing (3)
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts
  • src/renderer/src/store/slices/terminal-tab-retirement-store.test.ts

@erishforG

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

Addressed both review findings in e6b3f13: confirmed exits now clear the resolved legacy key and same-session aliases, and the regression test pins cleanup before the exit callback.

@coderabbitai

coderabbitai Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor
🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

git show --stat --oneline e6b3f13
git show --format=fuller --find-renames --find-copies -- src/renderer/src/components/terminal-pane/pty-connection.ts src/renderer/src/components/terminal-pane/pty-connection.test.ts e6b3f13

ast-grep outline src/renderer/src/components/terminal-pane/pty-connection.ts --items all --view expanded
ast-grep outline src/renderer/src/components/terminal-pane/pty-connection.test.ts --items all --view expanded

Length of output: 26881


🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n -A45 -B15 '\b(getSleepingRecordForPane|clearSleepingRecordProviderDuplicates|clearSleepingAgentSession)\b' \
  src/renderer/src/components/terminal-pane/pty-connection.ts \
  src/renderer/src/components/terminal-pane/pty-connection.test.ts

sed -n '19910,20010p' src/renderer/src/components/terminal-pane/pty-connection.test.ts
sed -n '3325,3410p' src/renderer/src/components/terminal-pane/pty-connection.ts

rg -n -A20 -B10 'clearSleepingRecordProviderDuplicates\s*\(' src/renderer/src/components/terminal-pane/pty-connection.ts

Length of output: 186930


@erishforG Review complete. I found no new issues.

The cleanup now removes the resolved legacy record and same-session aliases only. It preserves the sibling record with a different provider session. The regression test also verifies cleanup before onAgentExitedRef.current.


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

⚠️ Action not completed

Already reviewed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@AmethystLiang
AmethystLiang requested a review from OrcaWin August 14, 2026 16:50
@erishforG
erishforG force-pushed the fix/14228-retire-clean-agent-exit branch from e6b3f13 to 7a60c20 Compare August 17, 2026 05:17
@coderabbitai

coderabbitai Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@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: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ff494ee0-4150-49dc-9feb-00cd0a9a265b

📥 Commits

Reviewing files that changed from the base of the PR and between 8ca4ed9 and 7a60c20fb5802a6cbdfc680ffc2f9b7cf989e3c0.

📒 Files selected for processing (3)
  • src/renderer/src/components/terminal-pane/pty-connection-main-side-effect-authority.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts
  • src/renderer/src/store/slices/terminal-tab-retirement-store.test.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/renderer/src/store/slices/terminal-tab-retirement-store.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts

Included review availability: Your plan includes up to 10 reviews per rolling hour; 8 remain after this review.

@shay2911

Copy link
Copy Markdown

Independent validation against the current main branch:

  • Both commits from this PR cherry-picked cleanly onto current origin/main; no conflict remained locally.
  • Focused lifecycle suites passed: 32/32 tests across pty-connection-main-side-effect-authority, terminal-tab-retirement-store, and resume-sleeping-agent-session-replay.
  • typecheck:web passed.
  • Real Electron headless smoke tests passed for agent launch and clean shell recovery after agent exit (2/2).
  • The local 1.4.185 incident matched the bug exactly: 8 orphan working/live resume records for absent tabs were turned into 8 replacement agent tabs during update relaunch.

The cleanup boundary in this PR remains the right one: process-confirmed agent exit retires resume authority, while unexpected PTY loss keeps crash recovery.

Rebased onto the pty-connection.ts module split (stablyai#15166), which merged after
this fix and reintroduced the pre-fix onAgentExited body at its new home in
agent-idle-working-handlers.ts. Ports the same retire-on-confirmed-exit logic
onto the session.* API the split introduced.
@erishforG
erishforG force-pushed the fix/14228-retire-clean-agent-exit branch from bec0b6b to c936b3c Compare August 23, 2026 22:45
@erishforG

Copy link
Copy Markdown
Contributor Author

Rebased onto main — #15166 (the pty-connection.ts module split) merged after this PR opened and reintroduced the pre-fix onAgentExited body verbatim at its new home in agent-idle-working-handlers.ts, silently reverting this fix's coverage. Ported the same retire-on-confirmed-exit logic onto the session.* API the split introduced; same regression tests pass (24/24 in the affected suites, full terminal-pane/ suite still green at 3754/3755, 1 pre-existing skip).

@coderabbitai

coderabbitai Bot commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@nwparker

Copy link
Copy Markdown
Contributor

Thanks @erishforG for digging into this and for the careful write-up and recording. This path is now handled on main by #21714 (b6b974c): when the pane's foreground-process tracker confirms the agent has returned to a shell (after the command-finished mark, a visible-PTY read, or a confirmed process exit), onConfirmedShellForeground in pane-agent-identity.ts clears that pane's sleeping-session record along with any provider-session duplicates, so a cleanly exited Claude session is no longer a restore candidate. Main deliberately keys this off the process-confirmed shell rather than onAgentExited, since that fact can also fire from a title change without a foreground read. If you still see a finished session resumed on the latest build, please comment or reopen with the steps and we'll take another look.

@nwparker nwparker closed this Oct 26, 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.

[Bug]: Cleanly exited Claude session is auto-resumed with claude --resume after an app restart (local macOS, hibernation off)

5 participants