fix(browser): evict dead Profile runtime after a closed-target run failure - #323
Open
rohan911438 wants to merge 2 commits into
Open
fix(browser): evict dead Profile runtime after a closed-target run failure#323rohan911438 wants to merge 2 commits into
rohan911438 wants to merge 2 commits into
Conversation
…ilure raw `browser run` leased pages directly and ran the program with no recovery path: `CloakSessionManager.getPage()` treats a lease as healthy whenever `page.isClosed()` is false, but a disconnected context/CDP session can still report that. `dispatchCloakAction`'s 'run' case called `runBrowserProgram` with no try/catch, so a closed-target failure never invalidated the Profile runtime the way `navigatePage`/`newPage` already do — every subsequent raw browser command on that Session kept leasing the same dead page (agentrhq#314). Wrap the run in try/catch: on the closed-target signature, evict the Profile runtime (new `CloakSessionManager.evictDeadRuntime`, guarded to no-op if the runtime was already replaced) so the next command gets a fresh context/page. We don't retry the program itself here — it may have already caused side effects, so blind replay isn't safe; the caller sees the original error and the next command recovers. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Contributor
🟠 Maintainer review suggested — low confidenceThe automated review could not reach a fully supported conclusion. This review is advisory and does not block merging. |
There was a problem hiding this comment.
Pull request overview
This PR fixes a recovery gap in the local Cloak browser runtime where browser run could keep leasing a “dead” Playwright page/context (still reporting page.isClosed() === false) after a closed-target failure, causing repeated failures for subsequent commands in the same Session/Profile.
Changes:
- Export
isClosedContextErrorso other modules can reliably detect the closed-target signature. - Add a targeted runtime eviction method to invalidate a Profile runtime only when it matches the failing caller’s context.
- Wrap
runBrowserProgramindispatchCloakAction’srunpath to evict the dead runtime on the closed-target signature and then rethrow.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/browser/runtime/local-cloak/session-manager.ts | Exports isClosedContextError and adds evictDeadRuntime for safe Profile runtime eviction. |
| src/browser/runtime/local-cloak/actions.ts | Catches closed-target failures in run and evicts the runtime so the next command gets a fresh context/page. |
| src/browser/runtime/local-cloak/run-recovery.test.ts | Adds regression coverage asserting eviction occurs only for closed-target failures and not for unrelated errors. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+431
to
+434
| evictDeadRuntime(profileId: string, context: BrowserContext): void { | ||
| const runtime = this.profiles.get(profileId); | ||
| if (runtime && runtime.context === context) this.invalidateProfileRuntime(profileId, runtime); | ||
| } |
Comment on lines
+47
to
+52
| if (command === 'Target.createTarget') { | ||
| const created = await context.newPage(); | ||
| allPages.push(created); | ||
| queueMicrotask(() => emit('page', created)); | ||
| return { targetId: targetIds.get(created) }; | ||
| } |
…double-push Address PR review feedback on agentrhq#323: - evictDeadRuntime looked up this.profiles by the raw profileId param, but the map is keyed by normalizeProfileId(...) everywhere else in this class. As a public method it could silently no-op on an un-normalized id (whitespace/case). Normalize before the lookup. - run-recovery.test.ts's Target.createTarget mock pushed the page created by context.newPage() into allPages a second time — newPage()'s own mock already does that, so this duplicated pages in context.pages(). Adds a regression test proving evictDeadRuntime still finds the runtime when called with an un-normalized profileId. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.
Description:
Summary
Fixes #314.
Raw
browser runleased pages directly and ran the program with norecovery path:
CloakSessionManager.getPage()treats a lease ashealthy whenever
page.isClosed()is false, but a disconnectedcontext/CDP session can still report that.
dispatchCloakAction'sruncase calledrunBrowserProgramwith no try/catch, so aclosed-target failure never invalidated the Profile runtime the way
navigatePage/newPagealready do — every subsequent raw browsercommand on that Session kept leasing the same dead page.
Changes
isClosedContextErrorfromsession-manager.ts.CloakSessionManager.evictDeadRuntime(profileId, context)—invalidates the Profile runtime only if it still matches the caller's
context (no-ops if it was already replaced, so it can't evict a
healthy runtime that raced ahead of the failed lease).
runBrowserPrograminactions.ts'sruncase in try/catch:on the closed-target signature, evict the runtime, then rethrow.
We don't retry the program itself — it may have already caused side
effects, so blind replay isn't safe. The caller sees the original
error; the next command gets a fresh context/page instead of
repeating the same failure forever.
Test plan
run-recovery.test.ts: closed-target run failure evicts theruntime and the next
runcommand gets a fresh page; anunrelated failure does not evict.
npx tsc --noEmitclean.session-manager.test.ts/browser-run.test.tsstill pass.