Skip to content

fix(web): refuse commands on closed browser sessions - #4348

Draft
lucasacoutinho wants to merge 4 commits into
callstack:mainfrom
lucasacoutinho:fix/closed-web-session-reuse
Draft

lucasacoutinho wants to merge 4 commits into
callstack:mainfrom
lucasacoutinho:fix/closed-web-session-reuse

Conversation

@lucasacoutinho

@lucasacoutinho lucasacoutinho commented Nov 10, 2026 •

Copy link
Copy Markdown

Summary

Closing a web session records that browser on the daemon session store until the daemon restarts or an explicit open succeeds. Later non-open commands answer SESSION_NOT_FOUND with named_session_not_found and dispatched: no, including when --platform web is set. A second close succeeds and does not close the browser again. A session this daemon never opened still follows the device-selector path. A failed close stays retryable. A failed reopen stays closed.

The process-record fleet no longer tracks closed sessions, and the provider does not refuse commands. closed(session) is unchanged.

Validation

Head 4d891037fe9f19e3bd2eb2b9f3f8a2b6715c45ed. Base 093341a6040cc825763e05e0997bbeec7cdc085e.

pnpm check:affected --run --head 4d891037fe9f19e3bd2eb2b9f3f8a2b6715c45ed passed with exactCommit: true (receipt run-W3AMxE): format, lint, typecheck, layering, fallow, build, vitest-related, unit, docs-links, docs-prose. Web smoke and replay lanes are skipped here because GitHub owns them.

Live Node 24 and headless Chrome, private state dir: open, close, then snapshot, screenshot, and click refuse before dispatch. session list is empty. A second close succeeds. Open plus snapshot works again. A forced backend close failure left the session listed and a later snapshot worked. A forced open failure left the closed refusal in place. The private daemon was stopped.

@thymikee

Copy link
Copy Markdown
Member

Thanks for the fix. I read 61fcab1 and found one gap to close before merge, plus a design question. No checks report on this draft head yet.

This changes web browser lifecycle through the daemon, and the PR has no live run for it. Please paste the transcript for: open --platform web --session S, close, then snapshot, screenshot and click @ref with --platform web --session S. Each should answer SESSION_NOT_FOUND with details.reason web_session_closed and dispatched no. Then session list empty, and open again with a working snapshot. The failed-close and failed-reopen routes would help too. Your own check:affected receipt failed at unit, so a green one for this head is needed as well.

A close of an already-closed browser must be a no-op success. The guard at packages/platform-web/src/agent-browser-provider.ts:507 refuses every non-open command, including close. The daemon keeps a session for a retried close when a later step fails after the platform close succeeded (session-close.ts around L448), so the retry would get SESSION_NOT_FOUND, which becomes a platform close error; teardownSessionResources hits the same refusal. I traced this but did not reproduce it. A regression test (close, close again, no throw, no extra backend close) would pin it.

Could the refusal live at the daemon seam that already owns "session not held"? A non-open web request that names a session, resolves to a web device and finds no held session would answer namedSessionNotFoundError. That needs no new fleet state, no changed closed(session, succeeded) signature and no provider guard, and failed close and failed reopen fall out naturally. The trade-off is that a never-opened --platform web --session X would no longer auto-launch. Is that auto-launch intended? If so, the seam needs a closed marker next to the session store rather than in the process-record fleet.

Not blocking: namedSessionNotFoundDetails(session) from @agent-device/kernel/errors would replace the hand-built details at agent-browser-provider.ts:512; the docs line at website/docs/docs/sessions.md:60 could say "until the daemon restarts", since the closed state is in memory; and one daemon-level test would cover more than the three helper tests with a fake executor. The linked issues (#597, #105) look unrelated to this change.

cursoragent and others added 2 commits November 10, 2026 20:19
A successful web close records the session on the daemon session store.
Later non-open commands answer named_session_not_found before dispatch.
Closing again succeeds and does not close the browser a second time.
A session that was never opened still follows the device-selector path.

Co-authored-by: Lucas Coutinho <mail@lucasacoutinho.com>
Session teardown asks the cleanup capability whether a browser closed
and stores that answer. The capability stays the only place that selects web.

Co-authored-by: Lucas Coutinho <mail@lucasacoutinho.com>

Copy link
Copy Markdown
Author

Head 4d891037fe9f19e3bd2eb2b9f3f8a2b6715c45ed (on 093341a60, not rebased).

Auto-launch of a never-opened --platform web --session X stays. #3674 keeps a named request that also names a device selector, and --platform is that selector. The same change leaves an unnamed request sessionless, which is the implicit --platform web path. Docs and the web smoke open first; nothing relies on a never-opened named session launching Chrome. Refusing every unheld named web command would change that contract and would still recreate the browser after close --platform web with no --session. The closed address is stored beside the daemon session store, in memory until the daemon restarts. The fleet closed-set and the provider guard are gone, and closed(session) is unchanged.

The refusal is SESSION_NOT_FOUND, details.reason named_session_not_found, dispatched no. A second close is success and does not close the browser again. Daemon test request-router-closed-web-session.test.ts: close, close again, the backend close count stays put, and teardown does not call closeManagedBrowser. A failed close does not record the address. A failed open leaves it set.

Live Node 24, headless Chrome (agent-browser 0.39.0), private state dir. Snapshot after reopen is example.com’s “Learn more” link.

agent-device open https://example.com --platform web --session S --json
{"success":true,"data":{"session":"S"}}

agent-device close --platform web --session S --json
{"success":true,"data":{"session":"S","message":"Closed: S"}}

agent-device snapshot -i --platform web --session S --json
{"success":false,"error":{"code":"SESSION_NOT_FOUND","details":{"reason":"named_session_not_found","dispatched":"no","session":"S"}}}

agent-device screenshot /tmp/shot.png --platform web --session S --json
{"success":false,"error":{"code":"SESSION_NOT_FOUND","details":{"reason":"named_session_not_found","dispatched":"no","session":"S"}}}

agent-device click @e1 --platform web --session S --json
{"success":false,"error":{"code":"SESSION_NOT_FOUND","details":{"reason":"named_session_not_found","dispatched":"no","session":"S"}}}

agent-device session list --json
{"success":true,"data":{"sessions":[]}}

agent-device close --platform web --session S --json
{"success":true,"data":{"session":"S","message":"Closed: S"}}

agent-device open https://example.com --platform web --session S --json
{"success":true,"data":{"session":"S","message":"Opened: https://example.com"}}

agent-device snapshot -i --platform web --session S --json
{"success":true,"data":{"nodeCount":1,"labels":["Learn more"]}}

Failed close: the backend close was forced to exit 1. close returned COMMAND_FAILED (dispatched: unknown, stderr induced close failure). session list still showed S on web. snapshot -i --platform web --session S then succeeded (Learn more). A retry close succeeded and the list was empty.

Failed reopen: after a successful close, open was forced to exit 1 (COMMAND_FAILED). The following snapshot was still named_session_not_found / dispatched: no. The next open succeeded and snapshot returned Learn more.

pnpm check:affected --run --head 4d891037fe9f19e3bd2eb2b9f3f8a2b6715c45ed passed, exactCommit: true, receipt run-W3AMxE: format, lint, typecheck, layering, fallow, build, vitest-related, unit, docs-links, docs-prose. Web smoke and the replay lanes are skipped locally because GitHub owns them. Private daemon stopped; no managed Chrome left.

@thymikee

Copy link
Copy Markdown
Member

Thanks for the update. I reviewed 4d89103 against the earlier findings (#4348 (comment)). The earlier issues are fixed, but one new problem remains in how the closed marker is cleared.

The closed marker is keyed by the session address only, and it is cleared only for web opens (session-open-execution.ts#L303). The chain guard (request-handler-chain.ts#L146-L152) refuses every non-allowed command on a marked address, even when the daemon holds a session there. Sequence: open --platform web --session S, close --session S, then open Settings --platform android --session S. The open succeeds and does not clear the marker, so snapshot --session S fails with SESSION_NOT_FOUND on a session the daemon holds. The same happens on default, which HTTP RPC clients use without a cwd. The session stays refused until a web open or a daemon restart, and the error gives no hint why. I traced this from the code and did not run it. The rule the code must satisfy is this: a marked address is refused only while the daemon holds no session there, and any successful open on the address supersedes the marker. The smallest change is to drop the device.platform === 'web' condition so every successful open clears it. Please add a regression test in request-router-closed-web-session.test.ts that opens and closes web on S, opens a non-web device on S, and asserts the next snapshot is not refused with named_session_not_found. The test and live transcript only cover web reuse, so they would not catch this. A live check would run that same sequence and show snapshot --session S returning the held session's tree.

Not blocking, and you can take or leave these: no test drives the true branch of closeManagedBrowser in teardown (session-teardown.ts#L58) or the production factory's new return true, so one lease-expiry or teardown test asserting hasClosedBrowserSession would help; closedWebSessionAllows hand-lists 'open', 'close', 'batch' and 'replay' (request-handler-chain.ts#L129), which could be a trait on the command descriptor instead; and the doc comment on closeManagedBrowser (platform-resource-cleanup.ts#L6) says it returns false when there is no browser to close, but the factory returns true for every web device with a backend key.

No checks are reported on this head yet, and I could not verify the local check:affected run. There are no conflicts. Before merge, the marker must clear on any successful open and the regression test must be added.

A non-web open of a marked address left the marker in place, so the next
snapshot was refused as named_session_not_found. Every successful open now
leaves the set, and the chain refuses a marked address only while no session
is held there.

Co-authored-by: Lucas Coutinho <mail@lucasacoutinho.com>
@thymikee

Copy link
Copy Markdown
Member

Thanks for the update. At d97c0db the earlier review items on the closed-session refusal are addressed in code, but three things remain, and the live validation is missing.

The delta changes the pre-dispatch refusal predicate in request-handler-chain.ts: it adds the held-session condition and clears the marker on every platform. The live transcript and the check:affected receipt in the PR body are for 4d89103, so nothing shows the head still refuses on a real browser. Please rerun at d97c0db and update the Validation section. With --platform web --session S, run open about:blank, close, then snapshot. Snapshot must refuse with SESSION_NOT_FOUND, reason named_session_not_found, dispatched no. Then run open about:blank and snapshot, which must return the page tree. If an Android or iOS target is available, also run web open, close, then a non-web open on S and snapshot, and show it returns that device tree. Add the check:affected receipt for this head.

The marker is cleared in session-open-execution.ts right after openApplication resolves, before the isRequestCanceled check, requireOpenSessionAdmission and the final publish. Does a canceled open, or one that loses admission, leave the marker cleared with no session published? If so, a later unheld named web command takes the never-opened auto-launch path. The marker should be cleared where the open publishes its session, after those checks.

Likely also an issue: the new sessionStore.get(...) === undefined condition at request-handler-chain.ts:150 never changes the outcome on success routes, because every successful open clears the marker and an ordinary close retires the session. It matters when a session stays held after its browser was marked closed, since closePlatformObservingWebSession (session-close.ts:203-205) marks before retire. That can happen after a repair-armed close whose repair commit fails (prepareRepairClose), a failed provider lease release (runCloseTeardownAndRelease returns kind response), or closeAppWithoutEndingSession. At 4d89103 later commands on such a session were refused. At the head they dispatch to a closed browser, which is the original symptom in a failure branch. I did not confirm that a web session can be repair-armed or hold a provider lease in practice. The rule should be that a session cannot be both held and marked closed, so it needs a test for a web session kept after a failed close.

Would it be simpler to set the marker inside SessionStore when a web session is retired after a successful platform close (for example, a retire option the close path passes), and clear it on every successful open publish? Then held-and-marked cannot exist, the chain guard reduces to hasClosedBrowserSession(address) && !allowed(command), and the held-session condition and its doc caveats go away.

Not blocking, and fine to take or leave: the snapshot assertion in request-router-closed-web-session.test.ts is conditional and only checks the reason is not namedSessionNotFound, so it passes even if the unconditional clear is reverted. Asserting that the snapshot is dispatched (ok, or a device-level error) and dropping the conditional would test observable behavior.

I traced the regression tests but did not run them. No checks report on this head (fork draft PR), so there is no failing job to attribute, and no conflicts are known. Before merge, the live rerun at d97c0db and the check:affected receipt for this head are needed.

@thymikee

Copy link
Copy Markdown
Member

This branch now conflicts with main at d97c0db. Please rebase or merge main and resolve it. The open items from my last review (the live rerun at the new head, clearing the marker only where the open publishes its session, and a held session after a failed close) still apply after the rebase.

This branch has not been deployed

No deployments
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.

3 participants