Skip to content

[CLI] Fix --session-name auto-restore, --state precedence, and --init-script ordering on launch#1387

Open
gaojude wants to merge 2 commits into
mainfrom
fix-session-name-auto-restore-explicit-launch
Open

[CLI] Fix --session-name auto-restore, --state precedence, and --init-script ordering on launch#1387
gaojude wants to merge 2 commits into
mainfrom
fix-session-name-auto-restore-explicit-launch

Conversation

@gaojude
Copy link
Copy Markdown

@gaojude gaojude commented May 25, 2026

--session-name <slug> silently failed to auto-restore cookies and localStorage whenever the CLI dispatched an explicit launch command — any of --headed, --executable-path, --proxy, --user-agent, --args, --profile, --state, --allow-file-access, --color-scheme, --download-path, --engine, or --extensions. try_auto_restore_state was wired into the four branches of auto_launch but none of the five parallel branches of handle_launch. Now mirrored in each (cdp_url, cdp_port, auto_connect, provider, local).

Two adjacent ordering bugs surfaced while testing. --session-name and explicit --state both loaded, merging stale auto-state into the explicit file — try_auto_restore_state is now gated on storage_state.is_none() so explicit --state is the sole source. --init-script registered via Page.addScriptToEvaluateOnNewDocument ran after restore, missing the per-origin navigations state::load_state performs to replay localStorage — apply_launch_init_scripts now runs before restore in handle_launch, matching auto_launch's existing order. Adds e2e_session_name_auto_restores_cookies_via_explicit_launch, e2e_explicit_state_skips_session_name_auto_restore, and e2e_init_script_applies_to_restore_navigation; the two new tests fail on the prior code.

When --headed, --executable-path, --proxy, --user-agent, --args, or other
flags trigger main.rs:1057's dispatch logic, the CLI sends an explicit
`launch` command to the daemon before the first action. That routes
through handle_launch instead of auto_launch — and only auto_launch was
calling try_auto_restore_state. Result: --session-name silently failed to
restore cookies and localStorage for any flag combination that hit the
explicit-launch path.

Mirror the four auto_launch call sites in handle_launch's five
successful-launch paths (cdp_url, cdp_port, auto_connect, provider,
local). Placed before load_storage_state_or_rollback so explicit --state
still wins on conflicts, matching auto_launch's precedence.

Add e2e_session_name_auto_restores_cookies_via_explicit_launch to cover
the path that was missing — the existing test only exercised auto_launch
via a bare navigate, which is why the gap slipped in.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 25, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
agent-browser Ready Ready Preview, Comment May 28, 2026 1:07am
agent-browser-env-demo Ready Ready Preview, Comment May 28, 2026 1:07am

…estore

- Gate try_auto_restore_state on storage_state being None in every
  handle_launch and auto_launch branch so an explicit --state is the sole
  source of state (no silent merge of stale session-name data).
- Move apply_launch_init_scripts ahead of restore in handle_launch so
  --init-script applies to restore-driven navigations (load_state navigates
  to each origin to replay localStorage).
- Add regression tests:
  * e2e_explicit_state_skips_session_name_auto_restore
  * e2e_init_script_applies_to_restore_navigation
@gaojude gaojude changed the title Fix --session-name auto-restore for explicit launch paths [CLI] Fix --session-name auto-restore, --state precedence, and --init-script ordering on launch May 28, 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.

1 participant