agentleFS
TencentCloudTencentCloud/octop-browser/AGENTS.md

octop-browser

AGENTS.md22 starsChanged 2 months agoMITView on GitHub

This file provides guidance to CodeBuddy Code when working with code in this repository. All targets run through uv and the Makefile: Git hooks (required before committing): run make install-hooks once per clone. It sets core.hooksPath=.githooks, so every git commit first runs make all (format rewrites files, then lint / typecheck / test). Staged files rewritten by format are re-added automatically, so the commit contains the formatted content. This replaces the pre-commit install route — core.hooksPath overrides .git/hooks/, and make…

  • Reads credentials
  • Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about checking a change actually works.

    Of the AGENTS.md files we read, the 11,638 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.

  • In this repository3 other files it gives its agents.

The file

We split this AGENTS.md into the rules it gives an agent

2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    All targets run through uv and the Makefile:

    line 7
  2. Very specific:

    Follow Octop-style flow documented in CONTRIBUTING.md:

    line 132

≈ “Common” is an estimate: at least 1 in 5 of the 16,597 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
AGENTS.md

CODEBUDDY.md

This file provides guidance to CodeBuddy Code when working with code in this repository.

Commands

All targets run through uv and the Makefile:

make all       # ship bar: format + lint + typecheck + test — what pre-commit runs
make format    # ruff check --fix + ruff format on src/ tests/ (rewrites files)
make lint      # ruff check + ruff format --check on src/ tests/
make typecheck # mypy strict on src/
make test      # full suite with coverage (uv run pytest --cov=octop_browser)
make build     # uv build → wheel + sdist
make mcp       # launch the MCP stdio server (python -m octop_browser.mcp_server)
make publish   # local uv publish (prefer /publish skill + Actions; do not use for public releases)
make clean     # remove dist/, .coverage, .mypy_cache/, .ruff_cache/

Git hooks (required before committing): run make install-hooks once per clone. It sets core.hooksPath=.githooks, so every git commit first runs make all (format rewrites files, then lint / typecheck / test). Staged files rewritten by format are re-added automatically, so the commit contains the formatted content. This replaces the pre-commit install route — core.hooksPath overrides .git/hooks/, and make all is a superset of .pre-commit-config.yaml (ruff + ruff-format + mypy, plus tests). Bypass only in emergencies: SKIP_PRECOMMIT=1 git commit … or git commit --no-verify; never skip hooks to land a red suite.

Useful narrower invocations:

# Unit tests only — no Chrome required
uv run pytest tests/unit/ -q

# A single test file or test
uv run pytest tests/unit/test_mode.py -q
uv run pytest tests/unit/test_settings.py::test_env_override_browser_mode -q

# Integration tests — auto-skipped when Chrome/Chromium is not on the host
uv run pytest tests/integration/ -v

# Type-check or lint just one path
uv run mypy src/octop_browser/session.py
uv run ruff check src/octop_browser/cdp/

# Install dev extras + enable the commit gate
uv sync --extra dev
make install-hooks

pytest-asyncio runs in asyncio_mode = "auto" (set in pyproject.toml) — async test functions need no decorator. tests/integration/conftest.py skips the whole integration suite when find_chrome() returns None, so unit work on machines without Chrome stays green.

Architecture

This package wraps Chrome over pure CDP (WebSocket, no Playwright) and exposes it three ways: an async Python API, a stateless browser_tool() for AI frameworks, and an MCP server. Understanding the call chain matters when changing anything in the launch / connect / DOM path.

Layered call chain

browser_tool()  ──► BrowserSession ──► _InternalCDPSession ──► CDPClient (websockets) ──► Chrome
mcp_server.py   ──┘                          │
                                             └─► launcher.launch_or_attach + get_page_ws_url
  • tool_interface.browser_tool(action, profile=..., mode=...) — stateless dispatch keyed by profile. Maintains a _registry: dict[str, BrowserSession]; first call per profile creates a session, subsequent calls reuse it. Action names map directly to BrowserSession methods via getattr.
  • session.BrowserSession — public async API. Mixes in HooksMixin (events: before_action, after_action, action_error; page_navigated is reserved and never fired) and accumulates running totals in metrics_summary().
  • session._InternalCDPSession — owns one CDPClient + one RefCache per page. Honors cfg.cdp_ws_url: if set, bypasses the launcher entirely and connects directly to the given WebSocket (used for remote/Docker Chrome).
  • cdp.client.CDPClient — minimal CDP framing over websockets.asyncio.client. send(method, params) returns the result; enable_domain() enables Page / DOM / Runtime / Input after connect.
  • cdp.launcher — find_chrome(), launch_or_attach() (re-attach if port is already serving CDP), _get_ws_url() polls http://{cdp_host}:{port}/json/version, get_page_ws_url() picks the first type==page target. All HTTP probes go through aiohttp and always read cdp_host from settings — never hardcode localhost.

Configuration: OctopSettings (single source of truth)

settings.OctopSettings is a dataclass whose every field uses field(default_factory=lambda: _env_*(...)). This means each instantiation re-reads env vars, which is what monkeypatch.setenv in tests relies on. The module-level settings singleton (read at import time) is the default everywhere; tests should construct a fresh OctopSettings() rather than mutating it.

Env vars (all optional):

VariableDefaultUsed in
BROWSER_USE_PROFILES_DIR~/.octop-browser/profilesProfileManager.__init__
BROWSER_USE_CDP_HOSTlocalhostlauncher._get_ws_url, _port_in_use, get_page_ws_url, session.list_tabs; also drives --remote-debugging-address when non-loopback
BROWSER_USE_CDP_PORT_START9222ProfileManager port assignment
BROWSER_USE_MODEautoresolved via mode.resolve_headless()
BROWSER_USE_CHROME_BINauto-detectlauncher.find_chrome (checked before path search)
BROWSER_USE_CDP_TIMEOUT30.0CDPClient send timeout
BROWSER_USE_LAUNCH_RETRIES / BROWSER_USE_LAUNCH_DELAY20 / 0.25launcher polling
BROWSER_USE_CDP_WS_URL—_InternalCDPSession.connect direct-connect path
BROWSER_USE_IDLE_TIMEOUT_MINUTES0local profile idle reaper; real CDP commands refresh the deadline

The MCP server has no per-tool inputSchema field for these — they are passed via the standard mcpServers.<name>.env block in client config. Do not add them to the tool schema.

Browser mode resolution (octop_browser/mode.py)

Three modes — auto / headed / headless. Resolution priority in BrowserSession.create():

  1. Explicit headless=True/False argument (legacy escape hatch, wins outright)
  2. Explicit mode= argument
  3. cfg.browser_mode (from OctopSettings, which reads BROWSER_USE_MODE)

auto calls has_desktop_environment(): True on macOS/Windows always; on Linux requires $DISPLAY or $WAYLAND_DISPLAY. The result is fed to resolve_headless(). _build_flags only appends --headless=new when the resolved bool is True.

DOM output: 4 levels and the ref system

  • dom.builder.DOMBuilder produces minimal (~50 tok) / interactive (~200–500 tok) / full (~1000–3000 tok) / structured (JSON) views from a CDP DOM.getDocument snapshot.
  • dom.refs.RefCache issues stable refs like btn_2, inp_search mapping ref → CDP nodeId. Refs are invalidated on navigation — nav_actions.navigate / go_back / go_forward / reload clear the cache. Any new navigation-class action MUST do the same or you'll hand the agent stale node IDs.
  • Token estimate is len(content) // 4 — keep this when adding new actions that build text content.

Profiles and login persistence

profile.ProfileManager allocates one Chrome --user-data-dir per profile name under cfg.profiles_dir, with cdp_port = cfg.cdp_port_start + index. This is the entire login-persistence story: same profile name → same data dir → cookies and storage reused. Don't add ad-hoc paths for cookies/auth — go through Profile.

Hooks and metrics

HooksMixin is what BrowserSession mixes in. Every action goes:

_fire("before_action", {...}) → action body → _record(result)
                                                  └─► _fire("after_action", metrics)  (or "action_error")

ActionMetrics (models.py) carries duration_ms, dom_nodes_scanned, estimated_tokens, screenshot_size_kb. BrowserSession.metrics_summary() aggregates totals across the session.

Skill files (Claude Code / agent-facing)

Live at .codebuddy/skills/octop-browser/SKILL.md (English) and .codebuddy/skills/octop-browser-zh/SKILL.md (Chinese). They use the standard YAML-frontmatter format (name, description, allowed-tools) and are designed to be cp -r'd into other agent projects. Keep these in sync when the public surface (env vars, modes, action list) changes.

Project conventions

  • Commit gate: make install-hooks once per clone; make all must be green before every commit (the hook enforces it). See Commands.
  • Docs language: README is bilingual (README.md canonical English + README.zh.md); CHANGELOG.md and CONTRIBUTING.md are Chinese-only by deliberate choice.
  • ruff: line-length = 88, lint selects E F I N W. Do not raise the line length to dodge wraps.
  • mypy: strict = true, python_version = "3.11". New code must type-check under strict mode; reach for from __future__ import annotations (already used everywhere) before adding # type: ignore.
  • No Playwright, no Selenium: the dependency story is just websockets, aiohttp, pydantic, mcp. Don't add browser drivers.
  • localhost is never hardcoded — always go through cfg.cdp_host. A dedicated regression hazard if you forget.
  • Repository URL in docs is https://github.com/TencentCloud/octop-browser.git.

Branching & release

Follow Octop-style flow documented in CONTRIBUTING.md:

feature/* ──PR──► develop ──► release/x.y.z ──PR──► main ──tag v*──► publish

Agent publish: .cursor/skills/publish (/publish <version>). Never push directly to main/develop.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.