remote-session: detect stale resume sessions reported via JSON error events#3406
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a regression in the CLI remote-session “resume” flow by detecting stale --resume-session IDs when the underlying runtime reports the failure as a JSON error event on stdout (instead of stderr), enabling the existing recovery path to clear the stale ID and retry.
Changes:
- Capture the message from JSON
errorevents duringrunTurn()and include it in stale-session detection. - Simplify
detectStaleSession()to scan a combined haystack ofstderrTail, last reply text, and the last JSON error message. - Add a new mock scenario plus a unit test covering stale resume failures emitted via JSON
errorevents (with empty stderr).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| apps/cli/remote-session/turn-runner.ts | Track JSON error messages and use them to detect stale resume sessions. |
| apps/cli/remote-session/tests/turn-runner.test.ts | Add a unit test asserting staleSession is detected via JSON error events. |
| apps/cli/remote-session/tests/fixtures/mock-studio-code.mjs | Add a mock scenario that emits the stale-resume failure as a JSON error event. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Collaborator
📊 Performance Test ResultsComparing a72f5e4 vs trunk app-size
site-editor
site-startup
Results are median values from multiple test runs. Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change (<50ms diff) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Related issues
How AI was used in this PR
Claude Code investigated the daemon log together with me, traced the regression to PR #3360, and drafted the fix and tests. I drove the diagnosis and reviewed the diff before committing.
Proposed Changes
After #3360, the session ID stored by the remote-session bridge from a previous build can no longer be resolved by the new SessionManager.
studio code --resume-session <id>now fails immediately witherror No AI session found for resume ID: .... The poll loop already has a recovery path, gated onoutcome.staleSession. That detection was bypassed because of where the failure surfaces:errorevent on stdout, not on stderr.detectStaleSession()inapps/cli/remote-session/turn-runner.tsonly inspectedstderrTailandreplyText, so the stale-session pattern (/no ai session found for resume id/i) never had a chance to match.Local agent error (no output).to Telegram. The stale ID stayed in the remote-session state, so every subsequent turn hit the same error.This PR:
errorevents into a locallastErrorEventMessageinrunTurn().detectStaleSession()alongsidestderrTailandreplyText. Cleaned up dead code in that function while there (the previousisErrorshort-circuit had no effect on the result).stale-resume-error-eventscenario to the studio-code mock that emits the failure as a JSONerrorevent (matching the real production behavior we observed in the log).outcome.staleSession === truewhen the failure arrives viaerrorevent with empty stderr.Once
staleSessionis correctly detected, the existing recovery path inpoll-loop.ts:143-160clears the saved session, retries without--resume-session, and the new sessionId from the retry'sturn.completedis persisted atpoll-loop.ts:170-172. The stuck-on-stale-ID loop self-heals on the next turn.Testing Instructions
npm test -- apps/cli/remote-sessionshould report 112 passing tests (was 111).npm run typecheckshould be clean.npm run cli:build && node apps/cli/dist/cli/main.mjs code remote-session start --detach(with a configured bot/chat).node apps/cli/dist/cli/main.mjs code remote-session attach.~/.studio/with a non-existentsession_id(or just have a session_id from before AI sessions: adopt pi-coding-agent SessionManager end-to-end #3360).Local agent error (no output). Expected after: aninfo Resume failed; retrying without session_idline in the log, aSession expired; started a new one.message in Telegram, and the agent's actual reply right after.Pre-merge Checklist