Skip to content

fix(terminal): keep the wake record when the shell shows through a hibernation kill - #27295

Closed
deepbluejay wants to merge 3 commits into
stablyai:mainfrom
deepbluejay:fix/hibernation-wake-record-cleared
Closed

deepbluejay wants to merge 3 commits into
stablyai:mainfrom
deepbluejay:fix/hibernation-wake-record-cleared

Conversation

@deepbluejay

@deepbluejay deepbluejay commented Oct 10, 2026 •

Copy link
Copy Markdown

ELI5

Agent sleep shuts down a finished agent tab to save memory and is supposed to bring it back when you open the tab again. Some slept tabs never came back: you opened the tab and it was a dead terminal. This makes those tabs wake up.

What Changed

  • Before: with Agent sleep on, a completed agent tab could be put to sleep and then stay dead when opened. No shell, no agent, input ignored. The user had to close the tab and run claude --resume <id> by hand. It happens when the pane already knows an agent is running in it (the tracker identified the process, or the pane has a launch identity or a live hook icon), for example a pane restored after a relaunch, or a pane that was woken once and sleeps again.
  • After: the tab resumes its session when opened.
  • Mechanism: hibernation writes a sleeping record, marks the PTY exit as suppressed, then kills the PTY. The agent dies before the PTY does, so the shell's command-finished mark reaches the pane. For a pane with a known agent identity the foreground tracker runs a confirming read, sees the shell, and onConfirmedShellForeground clears the sleeping record as stale. When the exit lands there is no record, so the wake is never armed. The fix skips that clear when the record is this pane's hibernation wake hint: noteArmsHibernatedPaneWake(record) and either the pane's current PTY has a suppressed exit pending, or the exit already armed this record as session.hibernatedWakeTarget. Everything else in the handler is unchanged.
  • One source file, about 10 lines, plus a new test file.

Why

The clear came in with #21714 to drop a stale sleeping identity after /exit, and it is right for that. During a hibernation kill the record being cleared is the one the in-flight hibernation just wrote, so "confirmed shell means the record is stale" is the wrong conclusion in exactly this window. The guard reuses the signals session-reconcile-dispose.ts and pty-exit-hibernate.ts already use to recognise a hibernation kill, so a stale record beside an ordinary live PTY is still cleared as before.

Alternatives I did not take: cancelling the tracker's pending read when hibernation starts (the pane would then never retire the dead agent's identity for the remaining second, and the handler would stay one missed caller away from the same failure), and re-writing the record after the exit (two writers for one record). Guarding the one decision that is wrong was the smallest change.

Linked Issue

Fixes #27294

Visual Proof

N/A. No UI change; the difference is whether a process is started when the tab is opened. State and process observations from the dev build are in Testing.

Testing

macOS (Apple silicon), dev build via pnpm dev with ORCA_BACKGROUND_LAUNCH=1, driven over CDP, real Claude Code, Agent sleep on with a 1 minute idle time. Not tested on Linux, Windows or SSH.

Route 1, no relaunch: tabs A and B, run claude in A to a finished turn, switch to B, let A sleep, open A (wakes), send one more prompt, switch to B, let A sleep again, open A.

  • Without the fix (1 run): after the second sleep the pane has no sleeping record; opening A leaves it with no PTY and no process for the 20 seconds I watched.
  • With the fix (1 run): the record survives the second sleep (done / worktree-sleep); opening A gives it a new PTY within 6 seconds and claude --resume <same session id> is running.

Route 2, relaunch: run claude in A, switch to B, quit and relaunch, do not open A, let A sleep, open A.

  • Without the fix (2 of 2 runs): dead, as above.
  • With the first commit of this PR (1 run): wakes.

Control: the first sleep of a hand-typed agent the tracker has not identified wakes with and without the fix, because the tracker takes its no-read fast path there.

Automated: new pty-connection-hibernation-kill-shell-foreground.test.ts, three cases.

  • "keeps the wake record while the kill is in flight": fails on main, passes here.
  • "keeps the wake record when the confirmation settles after the exit": the ordering CodeRabbit raised. Fails on main and on this PR's first commit, passes here.
  • "still clears a record beside a PTY that is not being hibernated": pins fix(terminal): clear stale agent identity after shell exit #21714's behaviour.

I ran 9 related suites (86 tests), pnpm run tc:web, oxlint, oxfmt and check:code-quality:changed; all pass. I did not run the full pnpm test for this change.

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

AI Disclosure

Found, fixed and tested with Claude Code (Claude Opus 5.5). I hit the bug myself on 1.4.224 and reviewed the diff.

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

  • Renderer-only. No IPC, wire or persisted-format change, so older peers and remote hosts are unaffected. The guard reads suppressedPtyExitIds, which hibernation sets for local, remote-runtime and SSH panes alike; I only exercised local.
  • Not tested: an agent started from Orca's launcher. It has a launch identity from the start, so I expect it to hit the bug on its first sleep and to be fixed by the same guard, but I did not run that route.
  • The branch is based on bd85767e. The lines touched are unchanged on main at 314993c2.
  • Correction to my first description: I originally wrote that a relaunch was required. Tracing the no-relaunch case showed the real condition is a known agent identity on the pane.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • 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)

🤖 Generated with Claude Code

…bernation kill

Agent sleep writes a sleeping record, suppresses the PTY exit, then kills the
PTY. The agent dies before the PTY does, so the pane can confirm its own shell
in the foreground in between. That confirmation treated the record as stale and
cleared it, so the exit found nothing to arm and the pane never resumed.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c3111be3-b981-4955-ac37-64628d7f9a36

📥 Commits

Reviewing files that changed from the base of the PR and between 40c4ecd and 0ce0221.


📒 Files selected for processing (2)
  • src/renderer/src/components/terminal-pane/pty-connection-hibernation-kill-shell-foreground.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection/pane-agent-identity.ts

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



📝 Walkthrough

Walkthrough

When shell foreground is confirmed, the handler preserves the sleeping-pane record if it arms a hibernation wake and the current PTY is exit-suppressed, or if the wake target matches the record and the current PTY or no PTY is bound. The handler clears other sleeping records. Tests cover retention during a kill, delayed confirmation, replacement PTY binding, and an unsuppressed exit.


Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 0ce02

No actionable merge-blocking issue is established. Merge after normal checks.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check Passed The title is concise, specific, and accurately describes the main change: preserving the wake record during a hibernation kill.
Description check Passed The description follows the repository template, explains the user impact and mechanism, links issue #27294, documents testing and limitations, and identifies that the full test suite was not run.
Linked Issues check Passed Issue [#27294] requires a completed agent pane to retain its hibernation wake record and resume its session when opened. The change preserves the record when it identifies the pane's hibernation wake …
Out of Scope Changes check Passed The pull request changes the shell-foreground hibernation decision and adds focused regression tests. The tests support issue [#27294]. The available change summary shows no unrelated product behavior…

  • Fix all pre-merge checks with AI
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: eb26f405-39d8-452d-b230-f7f19bad431b
📥 Commits

Reviewing files that changed from the base of the PR and between 314993c and c7ebe6f.

📒 Files selected for processing (2)
  • src/renderer/src/components/terminal-pane/pty-connection-hibernation-kill-shell-foreground.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection/pane-agent-identity.ts

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

…er the exit

A command-finished read can still be awaiting the foreground process when the
suppressed exit lands and arms the wake. The suppression is spent by then, so
also recognise the record the pane has armed as its wake target.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

🟡 Minor · Match the wake target to the current PTY. · pane-agent-identity.ts:205-214

src/renderer/src/components/terminal-pane/pty-connection/pane-agent-identity.ts:205-214
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Match the wake target to the current PTY.

bindActivePanePty can bind live PTY B without clearing the armed wake target or sleeping record. The foreground tracker can then confirm B as a shell before wake consumption. The record-only match skips stale-record cleanup for B.

Suggested fix
       const sleepingRecord = session.getSleepingRecordForPane(state)
       const ptyId = session.transport.getPtyId()
+      const wakeTarget = session.hibernatedWakeTarget
       // Why: a hibernation kill ends the agent before the PTY, so the shell shows through for a
       // moment. That record was just written as the wake hint; clearing it strands the pane.
       // A read can also settle after the exit armed the wake, when the suppression is already spent.
       const isHibernationWakeRecord =
         sleepingRecord !== null &&
         noteArmsHibernatedPaneWake(sleepingRecord.record) &&
         ((ptyId !== null && state.suppressedPtyExitIds[ptyId] === true) ||
-          session.hibernatedWakeTarget?.record === sleepingRecord.record)
+          (wakeTarget?.record === sleepingRecord.record &&
+            (ptyId === null || wakeTarget.ptyId === ptyId)))

ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6a36243f-aba4-4931-bb9d-902bde47d02f
📥 Commits

Reviewing files that changed from the base of the PR and between c7ebe6f and 40c4ecd.

📒 Files selected for processing (2)
  • src/renderer/src/components/terminal-pane/pty-connection-hibernation-kill-shell-foreground.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection/pane-agent-identity.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/renderer/src/components/terminal-pane/pty-connection/pane-agent-identity.ts
  • src/renderer/src/components/terminal-pane/pty-connection-hibernation-kill-shell-foreground.test.ts

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

The wake-target guard matched the record alone, so a shell confirmed on a
PTY bound after the hibernated one (a restart) kept the stale record.
Match the target's PTY too, as consumeHibernatedAgentWake already does.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@deepbluejay

Copy link
Copy Markdown
Author

@coderabbitai Re: the outside-diff comment "Match the wake target to the current PTY" (pane-agent-identity.ts:205-214).

Valid, and fixed in 0ce0221.

The guard I added in 40c4ecd matched the armed wake target by record only. A restart can bind a new PTY to the pane without consuming the armed wake or clearing the sleeping record, so a shell confirmed on that new PTY kept the stale record. Before this PR that confirmation cleared it, so this was a regression from my own fix.

The guard now also requires the target's PTY to be the one still bound (or no PTY bound), which is the same check consumeHibernatedAgentWake already makes before it wakes the pane.

New test: "clears the record once another PTY is bound in place of the hibernated one". It fails on 40c4ecd and passes now. The three existing tests in that file still pass, including the one where the confirmation settles after the exit with the hibernated PTY still bound.

@coderabbitai

coderabbitai Bot commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail
ast-grep outline src/renderer/src/components/terminal-pane/pty-connection/pane-agent-identity.ts
sed -n '170,240p' src/renderer/src/components/terminal-pane/pty-connection/pane-agent-identity.ts
ast-grep outline src/renderer/src/components/terminal-pane/pty-connection-hibernation-kill-shell-foreground.test.ts
sed -n '1,300p' src/renderer/src/components/terminal-pane/pty-connection-hibernation-kill-shell-foreground.test.ts
rg -n -C 12 'consumeHibernatedAgentWake\s*[:=(]' src/renderer/src/components/terminal-pane

Length of output: 27610


@napaholic Thanks for the fix and explanation. The updated guard requires the armed wake target to match both the sleeping record and the bound PTY, unless no PTY is bound.

The new regression test checks that a shell confirmation clears the record after a replacement PTY is bound. The existing tests retain coverage for confirmations during the kill and after the suppressed exit.

This addresses “Match the wake target to the current PTY.” I verified the code and test assertions; I did not rerun the tests.


✏️ Learnings added
Learnt from: napaholic
URL: https://github.com/stablyai/orca/pull/27295

Timestamp: 2026-10-11T16:14:27.858Z
Learning: In stablyai/orca's TypeScript terminal pane lifecycle, a restart can bind a replacement PTY without consuming session.hibernatedWakeTarget or clearing the sleeping record. In src/renderer/src/components/terminal-pane/pty-connection/pane-agent-identity.ts, preserving a sleeping record based on an armed wake target requires both record identity and PTY identity: wakeTarget.record must match the sleeping record, and wakeTarget.ptyId must match the bound PTY unless no PTY is bound. A confirmed shell on a replacement PTY must clear the stale sleeping record when that PTY is not marked for a suppressed hibernation exit.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


🧠 Learnings used
Learnt from: napaholic
Repo: stablyai/orca

Timestamp: 2026-10-11T05:32:04.827Z
Learning: In stablyai/orca's TypeScript terminal foreground tracking, a real `pty:exit` clears the transport's PTY id before `onExit`. The foreground tracker then discards a pending confirmation because `trackablePtyId() !== ptyId`. When assessing confirmations that settle after a suppressed hibernation exit, distinguish this normal exit path from paths where the transport's PTY id stays bound.

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

Thank you for using CodeRabbit!

You are interacting with an AI system.

@nwparker

Copy link
Copy Markdown
Contributor

Thanks @deepbluejay. Main landed the in-flight half of this in #28838, so I rebased the remaining late-confirmation half and your regression tests onto main in #29391 (you're credited as co-author). Closing this one in favour of that.

@nwparker nwparker closed this Oct 16, 2026
@nwparker

Copy link
Copy Markdown
Contributor

Correction: #29391 was closed as a duplicate. The remaining half of this fix landed on main in #29288 (which builds on your tests and approach), alongside #28838 for the in-flight case.

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]: Agent sleep leaves a completed agent tab dead (wake record is cleared while the PTY is being killed)

2 participants