Skip to main content
Glama
README.md
# tether

A browser userscript bridge to a backend, doing three jobs:

1. **Control** (`puppetd/`, `userscript/core.js`, `bin/puppet`) — automate a real,
   human-logged-in browser session from a CLI/UI. Documented below and in
   [PUPPET.md](./PUPPET.md).
2. **Feed** (`puppetd/feed.py`) — passively ship the raw HTML of pages you browse
   to disk, for offline fact/data extraction. See [FEED.md](./FEED.md).
3. **Pipeline** (`puppetd/pipeline.py`) — a replayable work ledger over the feed:
   declared stages, leases, schemas, dispositions, replay. Workers (an LLM over MCP,
   `puppet work --claude`, any script) pull items; the daemon runs no models. See
   [PIPELINE.md](./PIPELINE.md).

All three run over **one userscript, one daemon, one secret**:

```
  puppet CLI  ──HTTP──▶  daemon (host)  ◀──HTTP──  userscript (browser tab)
  (the AI/UI)            history + queue           control: evals JS, reports routes
                         + feed store              feed:    ships settled pages
  offline parser ◀── feed files
```

- The daemon decides per URL which channel is live (`GET /config`, re-asked on every SPA route
  change). Control (remote JS eval) runs only on the `match` globs in `~/.puppet/config.json`;
  feed rules can add sites that are captured but **never driven**.
- `@match` in the generated script is the union of both, so a new feed domain needs no hand edits.
- Every request carries `X-Puppet-Secret`. The install URL carries it as `?k=` — the script
  embeds the secret, so it is never served without it. Get the URL from `puppet install`.

## Driving it from an LLM (MCP)

The daemon is also an MCP server at `/mcp` (streamable HTTP, same secret). `puppet daemon`
prints the exact line; it is:

```bash
claude mcp add --transport http tether http://<host>:8787/mcp --header "X-Puppet-Secret: <secret>"
```

Tools: `browser_status`, `browser_navigate` (waits for the new page and reports where it
landed), `browser_snapshot` (text + links + form controls with selectors), `browser_eval`,
`browser_click`, `browser_fill`, `browser_wait_for`, `browser_pin`, `browser_history`,
`feed_recent`, `feed_read`, `feed_rules_get`/`set`, `pipeline_status`/`claim`/`submit`/
`replay`/`outputs`/`stages_get`/`stages_set`, `events`. The server's `instructions`
teach the model the rules (one active tab, navigation, the human handles logins/CAPTCHAs);
the same text is at `GET /info` for anything that doesn't speak MCP.

**Navigation keeps the session.** A full page load replaces the script instance (new tab
id). The old instance leaves its id behind on `pagehide`; the new one reports it as
`X-Puppet-Prev`, and the daemon hands the driving session and any pin to it
(`session.handoff` in the log). Clicks that navigate are covered the same way.

## Control: puppet

Drive a **real, human-logged-in browser tab** synchronously from a CLI —
Selenium-for-the-hands, but with no webdriver / Marionette / CDP fingerprint.
Because the tab is a genuine human session, anti-bot defenses (DataDome et al.)
see a human, and the human and AI share the tab: the human handles gated steps
(logins, CAPTCHAs), the AI does the grinding.

The AI **is** the loop — it runs `puppet` commands turn by turn and reads the
output, exactly like any other shell tool. See [PUPPET.md](./PUPPET.md) for the
full design.

- **Userscript** (`userscript/core.js`) — reports `outerHTML`+URL on every load
  and SPA route change; long-polls the daemon for JS to `eval`.
- **Daemon** (`puppetd/`, stdlib-only) — holds the capture timeline and command
  queue; bridges blocking CLI calls to the polling userscript.
- **CLI** (`puppet`) — each call is one blocking request to the daemon.

## Quick start

```bash
# 1. start the daemon (generates ~/.puppet/config.json with a shared secret)
./bin/puppet daemon                       # or: pip install -e . && puppet daemon

# 2. open the status page the daemon prints (http://<host>:8787/?k=<secret>) in the Firefox
#    you browse from: it has setup steps, an Install / update button, live tabs + script
#    versions, feed rules and recent captures. (`puppet install` prints the script URL.)

# 3. open the target site in that Firefox, then drive it:
./bin/puppet status
./bin/puppet js 'return location.href'
./bin/puppet js 'return await fetch("/api/...").then(r=>r.json())'   # the real prize
./bin/puppet history last
```

## Command surface

| command | meaning |
|---|---|
| `puppet js '<code>'` | eval in the active tab, `await` promises, return JSON (the only real verb) |
| `puppet history last \| list \| get <id>` | the capture timeline (big HTML → file) |
| `puppet goto <url>` | navigate and wait for the new page; prints where it landed |
| `puppet click/fill/wait` | sugar over `js` |
| `puppet log [-f]` | the event log |
| `puppet status` | is a tab connected, what URL |
| `puppet feed [N]` | latest feed captures (see [FEED.md](./FEED.md)) |
| `puppet pipeline …` / `puppet work …` | pipeline status, replay, outputs; run a worker (see [PIPELINE.md](./PIPELINE.md)) |
| `puppet daemon \| init \| install \| userscript` | lifecycle |

**Locked semantics:** reads return through `js`; `goto` blocks until the new page
has attached and reports where it landed. A click that navigates destroys the
userscript mid-call, but the session follows the page, so act, then `wait` for what
you expect (or `history last`). `js` supports `await` and times out with a clear
error — it never hangs. Big payloads → file path.

## Security

The daemon runs arbitrary JS in a logged-in browser, so: it binds to one
interface (default `127.0.0.1`), requires the secret on every request (header, or
`?k=` on the install routes), compares it in constant time, and only enables eval on
the `match` globs — feed-only sites are never driven. Captured HTML carries
session PII — it stays under `~/.puppet/` (or wherever `feed.dir` points). For a LAN browser, set `host` in
`~/.puppet/config.json` to the daemon's LAN IP (never the public interface).

## Development

Fully autonomous, headless dev loop — no real browser, no real target needed:

```bash
pip install playwright && playwright install firefox
python dev/harness.py     # headless Firefox + local fixtures, asserts full round-trips
python tests/test_smoke.py  # browserless daemon/store/client tests
```

`dev/harness.py` injects `core.js` with Playwright's `add_init_script` (the same
hook GM provides) and points it at `dev/fixtures/` — which fake a `pushState` SPA
route change and delayed async content. The only dev/prod difference is a ~5-line
transport shim (`fetch` vs `GM_xmlhttpRequest`), selected by `typeof GM_xmlhttpRequest`.

The one human-in-the-loop moment is once, at the end: install the userscript in
real Firefox and confirm an end-to-end hit on the real target passes its bot wall.