refactor(cli): ignore --session and enforce default runtime session

This commit is contained in:
leeguooooo
2026-03-04 18:19:33 +09:00
parent 7b454e14ed
commit d320e1df47
8 changed files with 72 additions and 161 deletions
+1 -1
View File
@@ -232,7 +232,7 @@ flowchart TD
## Operational Recommendations
- Prefer `--headed` for high-friction targets.
- Reuse session state with one stable `--session-name` for continuity (when omitted, it defaults to `--session`).
- Reuse session state with one stable `--session-name` for continuity (when omitted, it defaults to `default`).
- Keep locale/timezone consistent with target market.
- For challenge-heavy pages, prefer `--wait-until domcontentloaded` on `open`/`navigate` to avoid `load` stalls.
- Use `--risk-mode block` in strict pipelines that require explicit operator intervention on verification pages.
+21 -25
View File
@@ -1,4 +1,4 @@
use crate::{color, validation};
use crate::color;
use serde::Deserialize;
use std::env;
use std::fs;
@@ -214,7 +214,7 @@ pub struct Flags {
pub allow_file_access: bool,
pub device: Option<String>,
pub auto_connect: bool,
pub session_name: Option<String>, // Defaults to --session when unset
pub session_name: Option<String>, // Defaults to "default" when unset
pub annotate: bool,
pub color_scheme: Option<String>,
pub download_path: Option<String>,
@@ -273,10 +273,8 @@ pub fn parse_flags(args: &[String]) -> Flags {
Err(_) => config.headed.unwrap_or(true),
},
debug: env_var_is_truthy("AGENT_BROWSER_DEBUG") || config.debug.unwrap_or(false),
session: env::var("AGENT_BROWSER_SESSION")
.ok()
.or(config.session)
.unwrap_or_else(|| "default".to_string()),
// --session is disabled: user-facing CLI always uses one default session.
session: "default".to_string(),
headers: config.headers,
executable_path: env::var("AGENT_BROWSER_EXECUTABLE_PATH")
.ok()
@@ -368,12 +366,6 @@ pub fn parse_flags(args: &[String]) -> Flags {
i += 1;
}
}
"--session" => {
if let Some(s) = args.get(i + 1) {
flags.session = s.clone();
i += 1;
}
}
"--headers" => {
if let Some(h) = args.get(i + 1) {
flags.headers = Some(h.clone());
@@ -532,14 +524,9 @@ pub fn parse_flags(args: &[String]) -> Flags {
}
// Keep auth/state continuity stable by default: if no explicit --session-name
// is provided, derive it from --session (or fall back to "default" when invalid).
// is provided, derive it from the default session id.
if flags.session_name.is_none() {
let derived = if validation::is_valid_session_name(&flags.session) {
flags.session.clone()
} else {
"default".to_string()
};
flags.session_name = Some(derived);
flags.session_name = Some("default".to_string());
}
flags
@@ -753,27 +740,36 @@ mod tests {
}
#[test]
fn test_parse_flags_with_session_and_executable_path() {
fn test_parse_flags_ignores_session_flag_and_keeps_default_session() {
let flags = parse_flags(&args(
"--session test --executable-path /custom/chrome open example.com",
));
assert_eq!(flags.session, "test");
assert_eq!(flags.session, "default");
assert_eq!(flags.executable_path, Some("/custom/chrome".to_string()));
assert_eq!(flags.session_name.as_deref(), Some("test"));
assert_eq!(flags.session_name.as_deref(), Some("default"));
}
#[test]
fn test_session_name_defaults_to_session_when_not_provided() {
fn test_session_name_defaults_to_default_when_not_provided() {
let flags = parse_flags(&args("--session my-session snapshot"));
assert_eq!(flags.session_name.as_deref(), Some("my-session"));
assert_eq!(flags.session_name.as_deref(), Some("default"));
}
#[test]
fn test_invalid_session_falls_back_to_default_session_name() {
fn test_invalid_session_still_uses_default_session_name() {
let flags = parse_flags(&args("--session bad/session snapshot"));
assert_eq!(flags.session_name.as_deref(), Some("default"));
}
#[test]
fn test_env_session_is_ignored_and_default_session_is_used() {
let _guard = EnvGuard::new(&["AGENT_BROWSER_SESSION"]);
env::set_var("AGENT_BROWSER_SESSION", "legacy-session");
let flags = parse_flags(&args("snapshot"));
assert_eq!(flags.session, "default");
assert_eq!(flags.session_name.as_deref(), Some("default"));
}
#[test]
fn test_cli_executable_path_tracking() {
// When --executable-path is passed via CLI, cli_executable_path should be true
+5 -67
View File
@@ -871,7 +871,6 @@ Aliases: goto, navigate
Global Options:
--json Output as JSON
--session <name> Use specific session
--headers <json> Set HTTP headers (scoped to this origin)
--risk-mode <mode> Risk handling for verify/captcha pages: off, warn, block
--wait-until <mode> Navigation wait strategy: load, domcontentloaded, networkidle
@@ -898,7 +897,6 @@ the browser's back button.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser back
@@ -915,7 +913,6 @@ the browser's forward button.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser forward
@@ -932,7 +929,6 @@ the browser's reload button.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser reload
@@ -955,7 +951,6 @@ Options:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser click "#submit-button"
@@ -976,7 +971,6 @@ or triggering double-click handlers.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser dblclick "#editable-text"
@@ -994,7 +988,6 @@ This replaces any existing content in the field.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser fill "#email" "user@example.com"
@@ -1014,7 +1007,6 @@ Use --delay to add per-character delay (milliseconds).
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser type "#search" "hello"
@@ -1039,7 +1031,6 @@ triggering hover states or dropdown menus.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser hover "#dropdown-trigger"
@@ -1056,7 +1047,6 @@ Sets keyboard focus to the specified element.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser focus "#input-field"
@@ -1073,7 +1063,6 @@ Checks a checkbox element. If already checked, no action is taken.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser check "#terms-checkbox"
@@ -1090,7 +1079,6 @@ Unchecks a checkbox element. If already unchecked, no action is taken.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser uncheck "#newsletter-opt-in"
@@ -1107,7 +1095,6 @@ Selects one or more options in a <select> dropdown by value.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser select "#country" "US"
@@ -1125,7 +1112,6 @@ Drags an element from source to target location.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser drag "#draggable" "#drop-zone"
@@ -1142,7 +1128,6 @@ Uploads one or more files to a file input element.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser upload "#file-input" ./document.pdf
@@ -1163,7 +1148,6 @@ Arguments:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser download "#download-btn" ./file.pdf
@@ -1194,7 +1178,6 @@ Modifiers (combine with +):
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser press Enter
@@ -1215,7 +1198,6 @@ Useful for holding modifier keys.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser keydown Shift
@@ -1232,7 +1214,6 @@ Releases a key that was pressed with keydown.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser keyup Shift
@@ -1260,7 +1241,6 @@ directly — it already operates on the current focus.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser keyboard type "Hello, World!"
@@ -1295,7 +1275,6 @@ Options:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser scroll
@@ -1317,7 +1296,6 @@ Aliases: scrollinto
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser scrollintoview "#footer"
@@ -1349,7 +1327,6 @@ Download Options (with --download):
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser wait "#loading-spinner"
@@ -1383,7 +1360,6 @@ Options:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser screenshot
@@ -1404,7 +1380,6 @@ Saves the current page as a PDF file.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser pdf ./page.pdf
@@ -1432,7 +1407,6 @@ Options:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser snapshot
@@ -1458,7 +1432,6 @@ Options:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser eval "document.title"
@@ -1487,11 +1460,9 @@ Aliases: quit, exit
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser close
agent-browser close --session mysession
"##
}
@@ -1517,7 +1488,6 @@ Subcommands:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser get text @e1
@@ -1549,7 +1519,6 @@ Subcommands:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser is visible "#modal"
@@ -1588,7 +1557,6 @@ Options:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser find role button click --name Submit
@@ -1618,7 +1586,6 @@ Subcommands:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser mouse move 100 200
@@ -1651,7 +1618,6 @@ Settings:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser set viewport 1920 1080
@@ -1685,7 +1651,6 @@ Subcommands:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser network route "**/api/*" --abort
@@ -1717,7 +1682,6 @@ Operations:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser storage local
@@ -1757,7 +1721,6 @@ When --url is omitted, --domain and --path must be provided together.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
# Simple cookie for current page
@@ -1800,7 +1763,6 @@ Operations:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser tab
@@ -1827,7 +1789,6 @@ Operations:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser window new
@@ -1849,7 +1810,6 @@ Arguments:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser frame "#embed-iframe"
@@ -1883,7 +1843,6 @@ Save Options:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
echo "pass" | agent-browser auth save github --url https://github.com/login --username user --password-stdin
@@ -1931,7 +1890,6 @@ Operations:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser dialog accept
@@ -1955,7 +1913,6 @@ Operations:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser trace start
@@ -1986,7 +1943,6 @@ Start Options:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
# Basic profiling
@@ -2025,7 +1981,6 @@ Operations:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
# Record from current page (preserves login state)
@@ -2057,7 +2012,6 @@ Options:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser console
@@ -2077,7 +2031,6 @@ Options:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser errors
@@ -2096,7 +2049,6 @@ Visually highlights an element on the page for debugging.
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser highlight "#target-element"
@@ -2124,7 +2076,7 @@ Operations:
Automatic State Persistence:
Use --session-name to auto-save/restore state across restarts.
If omitted, it defaults to --session (or "default"):
If omitted, it defaults to "default":
agent-browser --session-name myapp open https://example.com
Or set AGENT_BROWSER_SESSION_NAME environment variable.
@@ -2134,7 +2086,6 @@ State Encryption:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser state save ./auth-state.json
@@ -2154,24 +2105,18 @@ agent-browser session - Manage sessions
Usage: agent-browser session [operation]
Manage isolated browser sessions. Each session has its own browser
instance with separate cookies, storage, and state.
Show the current fixed session and active daemon state.
Operations:
(none) Show current session name
list List all active sessions
Environment:
AGENT_BROWSER_SESSION Default session name
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser session
agent-browser session list
agent-browser --session test open example.com
"##
}
@@ -2214,7 +2159,6 @@ Supported URL formats:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
# Connect to local Chrome with remote debugging
@@ -2253,7 +2197,6 @@ Notes:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser doctor
@@ -2273,7 +2216,6 @@ for touch-based interfaces like iOS Safari.
Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser tap "#submit-button"
@@ -2296,7 +2238,6 @@ Arguments:
Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser -p ios swipe up
@@ -2315,7 +2256,6 @@ Subcommands:
Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser device list
@@ -2370,7 +2310,6 @@ URL Diff:
Global Options:
--json Output as JSON
--session <name> Use specific session
Examples:
agent-browser diff snapshot
@@ -2498,7 +2437,7 @@ Snapshot Options:
-s, --selector <sel> Scope to CSS selector
Options:
--session <name> Isolated session (or AGENT_BROWSER_SESSION env)
--session <name> Ignored (single default session only)
--state <path> Load storage state from JSON file (or AGENT_BROWSER_STATE env)
--headers <json> HTTP headers scoped to URL's origin (for auth)
--executable-path <path> Custom browser executable (or AGENT_BROWSER_EXECUTABLE_PATH)
@@ -2528,7 +2467,7 @@ Options:
Extension side panel supports browser controls + console/network/DOM + workflow scheduling
--risk-mode <mode> Verify/captcha handling: off, warn, block (or AGENT_BROWSER_RISK_MODE)
--wait-until <mode> Navigation wait strategy for open/navigate: load, domcontentloaded, networkidle
--session-name <name> Auto-save/restore session state (defaults to --session)
--session-name <name> Auto-save/restore session state (defaults to "default")
--content-boundaries Wrap page output in boundary markers (or AGENT_BROWSER_CONTENT_BOUNDARIES)
--max-output <chars> Truncate page output to N chars (or AGENT_BROWSER_MAX_OUTPUT)
--allowed-domains <list> Restrict navigation domains (or AGENT_BROWSER_ALLOWED_DOMAINS)
@@ -2566,8 +2505,7 @@ Configuration:
Environment:
AGENT_BROWSER_CONFIG Path to config file (or use --config)
AGENT_BROWSER_SESSION Session name (default: "default")
AGENT_BROWSER_SESSION_NAME Auto-save/restore state persistence name (default: AGENT_BROWSER_SESSION)
AGENT_BROWSER_SESSION_NAME Auto-save/restore state persistence name (default: "default")
AGENT_BROWSER_ENCRYPTION_KEY 64-char hex key for AES-256-GCM state encryption
AGENT_BROWSER_STATE_EXPIRE_DAYS Auto-delete states older than N days (default: 30)
AGENT_BROWSER_EXECUTABLE_PATH Custom browser executable path
-6
View File
@@ -131,12 +131,6 @@ This enables control of:
</tr>
</thead>
<tbody>
<tr>
<td>
<code>--session &lt;name&gt;</code>
</td>
<td>Use isolated session</td>
</tr>
<tr>
<td>
<code>-p &lt;provider&gt;</code>
+1 -2
View File
@@ -293,8 +293,7 @@ agent-browser reload # Reload page
## Global options
```bash
--session <name> # Isolated browser session
--session-name <name> # Auto-save/restore session state (defaults to --session when omitted)
--session-name <name> # Auto-save/restore session state (defaults to "default" when omitted)
--state <path> # Load storage state from JSON file
--headers <json> # HTTP headers scoped to URL's origin
--executable-path <path> # Custom browser executable
+2 -12
View File
@@ -119,15 +119,6 @@ Every CLI flag can be set in the config file using its camelCase equivalent:
</td>
<td>boolean</td>
</tr>
<tr>
<td>
<code>session</code>
</td>
<td>
<code>--session</code>
</td>
<td>string</td>
</tr>
<tr>
<td>
<code>sessionName</code>
@@ -474,11 +465,10 @@ These environment variables configure additional daemon and runtime behavior:
<code>AGENT_BROWSER_SESSION_NAME</code>
</td>
<td>
Auto-save/load state persistence name (defaults to <code>AGENT_BROWSER_SESSION</code> when
unset).
Auto-save/load state persistence name (defaults to <code>default</code> when unset).
</td>
<td>
(same as <code>AGENT_BROWSER_SESSION</code>)
<code>default</code>
</td>
</tr>
<tr>
+34 -28
View File
@@ -1,38 +1,27 @@
import { pageMetadata } from "@/lib/page-metadata"
import { pageMetadata } from '@/lib/page-metadata';
export const metadata = pageMetadata("sessions")
export const metadata = pageMetadata('sessions');
# Sessions
Run multiple isolated browser instances:
Use one default runtime session and optional named persistence:
```bash
# Different sessions
agent-browser --session agent1 open site-a.com
agent-browser --session agent2 open site-b.com
# Or via environment variable
AGENT_BROWSER_SESSION=agent1 agent-browser click "#btn"
# List active sessions
agent-browser session list
# Output:
# Active sessions:
# -> default
# agent1
# Show current session
# Show current runtime session
agent-browser session
# Output: default
# Show active daemon sessions
agent-browser session list
```
## Session isolation
Each session has its own:
The runtime session is fixed to `default`. Use `--session-name` to isolate persisted state files per workflow.
- Browser instance
- Cookies and storage
- Navigation history
- Cookies and storage snapshots
- Authentication state
- Saved state lifecycle
## Session persistence
@@ -50,7 +39,7 @@ export AGENT_BROWSER_SESSION_NAME=twitter
agent-browser open twitter.com
```
If `--session-name` is omitted, it defaults to `--session` (or `default`).
If `--session-name` is omitted, it defaults to `default`.
State files are stored in `~/.agent-browser/sessions/` and automatically loaded on daemon start.
@@ -164,12 +153,29 @@ agent-browser set headers '{"X-Custom-Header": "value"}'
<table>
<thead>
<tr><th>Variable</th><th>Description</th></tr>
<tr>
<th>Variable</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr><td><code>AGENT_BROWSER_SESSION</code></td><td>Browser session ID (default: "default")</td></tr>
<tr><td><code>AGENT_BROWSER_SESSION_NAME</code></td><td>Auto-save/load state persistence name</td></tr>
<tr><td><code>AGENT_BROWSER_ENCRYPTION_KEY</code></td><td>64-char hex key for AES-256-GCM encryption</td></tr>
<tr><td><code>AGENT_BROWSER_STATE_EXPIRE_DAYS</code></td><td>Auto-delete states older than N days (default: 30)</td></tr>
<tr>
<td>
<code>AGENT_BROWSER_SESSION_NAME</code>
</td>
<td>Auto-save/load state persistence name</td>
</tr>
<tr>
<td>
<code>AGENT_BROWSER_ENCRYPTION_KEY</code>
</td>
<td>64-char hex key for AES-256-GCM encryption</td>
</tr>
<tr>
<td>
<code>AGENT_BROWSER_STATE_EXPIRE_DAYS</code>
</td>
<td>Auto-delete states older than N days (default: 30)</td>
</tr>
</tbody>
</table>
+8 -20
View File
@@ -173,7 +173,7 @@ agent-browser cookies set callback_token "token123"
```bash
# Auto-save/restore cookies and localStorage across browser restarts
# If --session-name is omitted, it defaults to --session (or "default")
# If --session-name is omitted, it defaults to "default"
agent-browser --session-name myapp open https://app.example.com/login
# ... login flow ...
agent-browser close # State auto-saved to ~/.agent-browser/sessions/
@@ -205,16 +205,14 @@ agent-browser snapshot -i --json
agent-browser get text @e1 --json
```
### Parallel Sessions
### Parallel Workflows
```bash
agent-browser --session site1 open https://site-a.com
agent-browser --session site2 open https://site-b.com
agent-browser --session-name site1 open https://site-a.com
agent-browser --session-name site2 open https://site-b.com
agent-browser --session site1 snapshot -i
agent-browser --session site2 snapshot -i
agent-browser session list
agent-browser --session-name site1 snapshot -i
agent-browser --session-name site2 snapshot -i
```
### Connect to Existing Chrome
@@ -489,22 +487,12 @@ These behaviors are always active. For sensitive sites, combine with `--headed`
## Session Management and Cleanup
When running multiple agents or automations concurrently, always use named sessions to avoid conflicts:
```bash
# Each agent gets its own isolated session
agent-browser --session agent1 open site-a.com
agent-browser --session agent2 open site-b.com
# Check active sessions
agent-browser session list
```
`--session` is ignored in this fork. The runtime always uses one default session. Use `--session-name` to isolate persistence when needed.
Always close your browser session when done to avoid leaked processes:
```bash
agent-browser close # Close default session
agent-browser --session agent1 close # Close specific session
agent-browser close
```
If a previous session was not closed properly, the daemon may still be running. Use `agent-browser close` to clean it up before starting new work.