Skip to content

fix: load storage state at launch when --state / AGENT_BROWSER_STATE is set#1241

Merged
ctate merged 8 commits intomainfrom
fix/state-flag-load-at-launch
Apr 16, 2026
Merged

fix: load storage state at launch when --state / AGENT_BROWSER_STATE is set#1241
ctate merged 8 commits intomainfrom
fix/state-flag-load-at-launch

Conversation

@tomdale
Copy link
Copy Markdown
Contributor

@tomdale tomdale commented Apr 15, 2026

Fixes #1164

The --state <path> flag and AGENT_BROWSER_STATE env var are documented as loading saved browser state (cookies + localStorage) at launch, but load_state() was never called after the browser started. The flag was fully plumbed through parsing, env propagation, and validation but the actual loading step was never wired up. This has been the case since the flag was introduced in the native Rust rewrite.

Reproduction

# Save a cookie to a state file
agent-browser open https://example.com
agent-browser cookies set test_cookie hello_world
agent-browser state save /tmp/repro-state.json
agent-browser close

# Relaunch with --state and check cookies
agent-browser --state /tmp/repro-state.json open https://example.com
agent-browser --json cookies get
agent-browser close

Before (v0.25.4):

{"success": true, "data": {"cookies": []}, "error": null}

After (this PR):

{"success": true, "data": {"cookies": [{"name": "test_cookie", "value": "hello_world", ...}]}, "error": null}

Changes

  • handle_launch(): Call state::load_state() with the storageState path after browser launch
  • auto_launch(): Extract storage_state from LaunchOptions before the struct is moved, then call try_load_storage_state() after try_auto_restore_state() in all four branches (CDP, auto-connect, provider, local Chrome)
  • New helper try_load_storage_state(): Encapsulates the load pattern to avoid duplication across branches
  • 4 new e2e tests covering every state persistence path

Test plan

  • e2e_state_flag_restores_cookiesstorageState in explicit launch command (was broken, now passes)
  • e2e_state_env_restores_cookies_on_auto_launchAGENT_BROWSER_STATE env via auto-launch (was broken, now passes)
  • e2e_session_name_auto_restores_cookies--session-name auto-save/restore (regression guard)
  • e2e_explicit_state_load_restores_cookies — explicit state load command (baseline)
  • Full e2e suite: 57/57 passed, 0 regressions
  • Unit tests: 635/635 passed

…is set

The `--state` flag and `AGENT_BROWSER_STATE` env var were documented as
restoring saved browser state (cookies + localStorage) at launch, but
`load_state()` was never called after the browser started. The feature
has been broken since it was introduced.

Adds `try_load_storage_state()` and calls it from every early-return
path in `auto_launch()` (lazy launch triggered by commands like
`navigate`) and from `handle_launch()` (explicit `launch` command).

Also adds 4 e2e tests covering all state-persistence paths:
- Explicit launch with `storageState` field
- Auto-launch via `AGENT_BROWSER_STATE` env var
- Session-name auto-restore via `try_auto_restore_state`
- Explicit `state_load` command (baseline sanity check)

Fixes #1164.
@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Apr 15, 2026

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

Project Deployment Actions Updated (UTC)
agent-browser Ready Ready Preview, Comment Apr 15, 2026 7:58pm
agent-browser-env-demo Ready Ready Preview, Comment Apr 15, 2026 7:58pm

Copy link
Copy Markdown
Contributor

@vercel vercel Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional Suggestion:

In handle_launch, the storageState parameter is silently ignored when connecting via CDP URL, CDP port, auto-connect, or provider paths because storage state loading only exists in the normal BrowserManager::launch code path.

Fix on Vercel

Reformats a single long format\! call to satisfy CI's rustfmt check.
No behavior change.
The CDP URL, CDP port, auto-connect, and provider early-return branches
were skipping storage state loading because try_load_storage_state was
only called in the normal BrowserManager::launch() path at the bottom
of handle_launch().

Also compute storage_state_owned once and reuse it across all branches
rather than borrowing storage_state (a &str tied to cmd) in a helper
that needs an owned Option<String>.
@ctate
Copy link
Copy Markdown
Collaborator

ctate commented Apr 16, 2026

Thanks @tomdale!

@ctate ctate merged commit 19808d0 into main Apr 16, 2026
13 checks passed
@ctate ctate mentioned this pull request Apr 16, 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.

Linux v0.24.1: state save/--state and --session-name write state but do not restore cookies or localStorage

2 participants