Skip to content

Commit cddc847

Browse files
GreyCJeff Chenjackwener
authored
docs(browser): clarify named session lifecycle (#1542)
* docs(browser): clarify named session lifecycle * docs(browser): clarify owned versus bound sessions --------- Co-authored-by: Jeff Chen <jeff@adtiming.com> Co-authored-by: jackwener <jakevingoo@gmail.com>
1 parent 4f5fcd9 commit cddc847

3 files changed

Lines changed: 46 additions & 8 deletions

File tree

docs/guide/browser-bridge.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ Key rules:
4848
- `tab select <targetId>` makes that tab the default target for later untargeted `opencli browser ...` commands.
4949
- `tab close <targetId>` removes the tab; if it was the current default target, the stored default is cleared.
5050

51+
## Session Lifecycle
52+
53+
Use a stable session name when you want multiple `opencli browser` commands to keep operating on the same page:
54+
55+
```bash
56+
opencli browser my-session open https://example.com
57+
opencli browser my-session state
58+
opencli browser my-session extract "main"
59+
```
60+
61+
Owned browser sessions use an interactive tab lease with a 10-minute idle timeout. Release it explicitly when done:
62+
63+
```bash
64+
opencli browser my-session close
65+
```
66+
67+
Use `opencli browser <session> bind` when you want to attach OpenCLI to a Chrome tab you already opened manually. Bound sessions do not have the owned-session idle close timer; they stay attached until `unbind`, tab close, window close, or daemon restart. For owned sessions, use `--window foreground` to watch OpenCLI work in a visible automation window, or `--window background` to keep that automation window out of the way.
68+
5169
## How It Works
5270

5371
```

docs/zh/guide/browser-bridge.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ opencli browser baidu tab close <targetId>
4646
- `tab select <targetId>` 会把该 tab 设为后续未显式指定 target 的 `opencli browser ...` 命令默认目标。
4747
- `tab close <targetId>` 会关闭该 tab;如果它正好是当前默认目标,会一并清掉这条默认绑定。
4848

49+
## Session 生命周期
50+
51+
如果你希望多条 `opencli browser` 命令持续操作同一个页面,请使用稳定的 session 名称:
52+
53+
```bash
54+
opencli browser my-session open https://example.com
55+
opencli browser my-session state
56+
opencli browser my-session extract "main"
57+
```
58+
59+
OpenCLI 拥有的 browser session 使用交互式 tab lease,默认空闲超时为 10 分钟。完成后可以显式释放:
60+
61+
```bash
62+
opencli browser my-session close
63+
```
64+
65+
如果要把 OpenCLI 绑定到你已经手动打开的 Chrome tab,请使用 `opencli browser <session> bind`。绑定 session 没有 owned session 的 idle close 计时器,会一直保持到 `unbind`、tab 关闭、窗口关闭或 daemon 重启。对于 OpenCLI 自己创建的 owned session,使用 `--window foreground` 可以在可见自动化窗口里观察 OpenCLI 操作;使用 `--window background` 可以让这个自动化窗口留在后台。
66+
4967
## Daemon 生命周期
5068

5169
Daemon 在首次运行浏览器命令时自动启动,之后保持常驻运行。

skills/opencli-browser/SKILL.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Until `doctor` is green, nothing else will work. Typical failures: Chrome not ru
2525
## Session lifecycle
2626

2727
- `opencli browser *` commands require a `<session>` positional immediately after `browser`. Use the same session name for a multi-step flow; use a different name to isolate parallel browser work.
28+
- Use a stable session name for any multi-command or human-paced browser workflow. Example: `opencli browser fb-yaya-warmup open https://example.com`, then reuse `opencli browser fb-yaya-warmup state`, `extract`, `click`, etc.
2829
- Owned browser sessions keep a tab lease alive between calls. Release it with `opencli browser <session> close` or let the idle timeout expire.
2930
- `opencli browser <session> bind` binds the Chrome tab you already have open to that session. Use this for logged-in pages, SSO flows, or pages you manually positioned before handing control to the agent.
3031
- `--window foreground|background` (or `OPENCLI_WINDOW=foreground|background`) chooses whether OpenCLI creates/focuses a foreground browser window or uses a background browser window for owned sessions.
@@ -59,14 +60,15 @@ Bound sessions have no OpenCLI idle-close timer; the binding lasts until `unbind
5960
## Critical rules
6061

6162
1. **Always inspect before you act.** Run `state` or `find` first. Never hard-code a ref or selector from memory across sessions — indices are per-snapshot.
62-
2. **Prefer numeric ref over CSS once you have it.** Numeric refs survive mild DOM shifts because the CLI fingerprints each tagged element. A CSS selector written by hand will break the first time the site re-renders.
63-
3. **Read `match_level` after every write.** `exact` = all good. `stable` = the element is the same but some soft attrs drifted — your action still applied. `reidentified` = the original ref was gone and the CLI found a unique replacement; double-check you hit the right element.
64-
4. **Use the `compound` field for form controls.** Do not regex-guess a date format, do not `state` twice to get the full `<select>` options list. The compound envelope has the format string, full option list up to 50, `options_total` for overflow, and `accept`/`multiple` for `<input type=file>`.
65-
5. **Verify writes that matter.** After `type <target> <text>`, run `get value <target>`. After `select`, run `get value`. Autocomplete widgets, React controlled inputs, and masked fields all silently eat characters. The CLI cannot detect this for you.
66-
6. **`state` → action → `state` after a page change.** Navigations, form submits, and SPA route changes invalidate refs. Take a fresh snapshot. Do not reuse refs from before the transition.
67-
7. **Chain with `&&`.** A chained sequence runs in one shell so refs acquired by the first command stay live for the second. Separate shell invocations lose the session context you just set up.
68-
8. **`eval` is read-only.** Wrap the JS in an IIFE and return JSON. If you need to *change* the page, use the structured `click` / `type` / `select` / `keys` commands instead — they produce structured output and fingerprints, `eval` does not.
69-
9. **Prefer `network` to screen-scraping.** If a page you care about fetches its data from a JSON API, the API is almost always more reliable than scraping the rendered DOM. Capture once, inspect the shape, then `--detail <key>` the body you need.
63+
2. **Prefer site adapters before raw browser driving.** If `opencli <site> <command>` already covers the task, use that adapter command first (`opencli facebook notifications`, `opencli reddit read`, etc.). Use `opencli browser ...` only for gaps, debugging, or one-off UI flows the adapter does not expose.
64+
3. **Prefer numeric ref over CSS once you have it.** Numeric refs survive mild DOM shifts because the CLI fingerprints each tagged element. A CSS selector written by hand will break the first time the site re-renders.
65+
4. **Read `match_level` after every write.** `exact` = all good. `stable` = the element is the same but some soft attrs drifted — your action still applied. `reidentified` = the original ref was gone and the CLI found a unique replacement; double-check you hit the right element.
66+
5. **Use the `compound` field for form controls.** Do not regex-guess a date format, do not `state` twice to get the full `<select>` options list. The compound envelope has the format string, full option list up to 50, `options_total` for overflow, and `accept`/`multiple` for `<input type=file>`.
67+
6. **Verify writes that matter.** After `type <target> <text>`, run `get value <target>`. After `select`, run `get value`. Autocomplete widgets, React controlled inputs, and masked fields all silently eat characters. The CLI cannot detect this for you.
68+
7. **`state` → action → `state` after a page change.** Navigations, form submits, and SPA route changes invalidate refs. Take a fresh snapshot. Do not reuse refs from before the transition.
69+
8. **Chain with `&&` when reusing freshly parsed refs.** A chained sequence runs in one shell so the ref you just read from output can be passed directly to the next command. Separate shell invocations keep the named browser session, but any shell-local variables or copied refs from the previous command can go stale after page changes.
70+
9. **`eval` is read-only.** Wrap the JS in an IIFE and return JSON. If you need to *change* the page, use the structured `click` / `type` / `select` / `keys` commands instead — they produce structured output and fingerprints, `eval` does not.
71+
10. **Prefer `network` to screen-scraping.** If a page you care about fetches its data from a JSON API, the API is almost always more reliable than scraping the rendered DOM. Capture once, inspect the shape, then `--detail <key>` the body you need.
7072

7173
---
7274

0 commit comments

Comments
 (0)