Skip to content

feat(pair): phone pairing page and per-daemon service worker proxy - #15

Draft
agentiknet wants to merge 3 commits into
mainfrom
wt/rdv-pair-page
Draft

agentiknet wants to merge 3 commits into
mainfrom
wt/rdv-pair-page

Conversation

@agentiknet

@agentiknet agentiknet commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Draft: blocked on agentproto/ts#1450 being released. This depends on @agentproto/pair-client@^0.1.0, which isn't on npm yet, and on browser entries of @agentproto/acp / @agentproto/secrets that the published versions don't have. CI installs from npm ranges, so pnpm install fails until those ship. Everything below was built and run against the local #1450 build (wt/rdv-pr2-browser-tunnel-client @ 63b4ff73, pair/v2).

Required companion: agentik-studio/agentik-studio#313. It fixes session-chat's API base path. Without it, the Control Center's calls escape the service worker scope; see "session-chat base path" below.

Part 3 of the phone ↔ daemon over rendezvous plan (RDV-BROWSER-PLAN PR 3).

What it does

  • /pair reads the offer from the fragment (https://cli.agentproto.sh/pair#v=2&…, what agentproto pair offer --qr prints) and wipes it from the URL with history.replaceState before anything else runs. It then calls pairFromOffer and shows the daemon's name and fingerprint. Confirm stores the credential with createIndexedDbCredentialStore (IndexedDB only, nothing in localStorage). Cancel drops it and tells the user how to remove the daemon's record. Errors get their own page: expired or spent offer, unreachable daemon, and a v1 offer, which is refused as outdated with no network dial. With no offer, /pair lists this phone's paired daemons. The manifest's start_url is /pair?launch=1, which goes straight to the daemon when only one is paired.

  • Service worker, one registration per pairing, scoped to /d/<fingerprint>/ (src/sw/pair-sw.ts, bundled by scripts/build-sw.mjs into public/pair-sw.js, which is gitignored). It keeps one TunnelClient at module scope, shared by every fetch event and rebuilt lazily if the browser killed the worker. It answers every fetch under its scope with client.fetch(event.request) and mapPath stripping /d/<id>. The pages never connect themselves; they ask the worker over postMessage, which keeps the daemon's 2 standing slots per pairing free. The script is served from /, so its default max scope covers /d/<id>/ without a Service-Worker-Allowed header.

  • The Control Center loads from the daemon, unchanged, at /d/<id>/apps/@agentik/session-chat/ui/. No session-chat code lives here.

  • /d/<id> (outside the scope) is the state page:

    • paired · connecting
    • daemon offline · retrying: TunnelClient state and error, "Retry now", reconnects on online
    • revoked: re-pair instructions, "Forget this daemon"
    • not paired · outdated pairing: the credential predates pair/v2 (outdated / protocol_outdated); update agentproto and re-pair
    • not paired

    The worker sends an open Control Center here on revoke or outdated (both terminal), or after 8 s offline. A page load that can't reach the daemon is redirected here too. Once the tunnel is up again, the page returns to where the user was.

  • Add-to-home-screen manifest and icons.

One origin per daemon (<fingerprint>.agentproto.cloud)

On one shared origin, every stored pairing sits in the same IndexedDB and behind the same service-worker origin, so any daemon's Control Center could use every other daemon's pairing. The app is now host-agnostic (src/lib/pair-host.ts):

  • Per-daemon origin, https://<daemon-fingerprint>.<pair domain> (pair domain from NEXT_PUBLIC_AGENTPROTO_PAIR_DOMAIN at build time, default agentproto.cloud, inlined into the pages by Next and into the worker by build-sw.mjs). The origin belongs to exactly one daemon.
    • /pair refuses an offer whose fingerprint isn't the host label. It checks with inspectOffer, before any handshake, so the offer stays unspent. The refusal page links to the same offer on that daemon's own origin, and nothing is stored. A second check after the handshake cancels on a mismatch.
    • The credential store is scoped to the origin's daemon, in the pages and in the worker: other ids read as absent, and put refuses them.
    • /d/<other-id> on a per-daemon origin points to that daemon's origin. The bare pair domain, or a label that isn't a fingerprint, refuses to pair.
    • The /d/<id>/ worker layout is unchanged (id = the same fingerprint).
  • Shared origin: any other host (cli.agentproto.sh, localhost) keeps today's behaviour, with a short notice on /pair that pairings on this address share it (to link AIP-59 §5.8 once it exists).
  • The pair-client helper (expectedPairHost / {fp} pair-page templates) wasn't on #1450 yet (head 3228a8a7), so the host check is local: a few lines in pair-host.ts. It can switch to the helper once it lands. The CLI side, where pair offer --qr prints https://<fp>.agentproto.cloud/pair#…, is #1450's {fp} template work.

Deployment notes (not done here; no DNS or deploy config touched)

  • The site must answer for *.agentproto.cloud: a wildcard domain on the service (Coolify / reverse proxy), or a Cloudflare Worker route that forwards *.agentproto.cloud/* to it.
  • Cloudflare: a proxied * DNS record on agentproto.cloud, SSL/TLS Full (strict), and Universal SSL covering *.agentproto.cloud. The origin certificate must also cover the wildcard if the origin terminates TLS.
  • The apex agentproto.cloud needn't serve the app; if it does, it refuses to pair.
  • The default pair domain is compiled in. To change it, pass NEXT_PUBLIC_AGENTPROTO_PAIR_DOMAIN at build time (the Dockerfile doesn't forward it yet; add an ARG/ENV if needed).
  • cli.agentproto.sh keeps working as the shared-origin fallback.

session-chat base path (studio: agentik-studio#313)

session-chat resolved its API base as the injected window.__AGENTPROTO_BASEURL__, else location.origin. Behind the tunnel, the daemon builds that injected value from the Host it sees, http://127.0.0.1:<port>, so every REST, /mcp and SSE call escaped the SW scope. #313 derives the base from the served document instead: origin plus whatever prefix comes before /apps/. The daemon-served page then calls …/d/<id>/mcp, …/d/<id>/sessions/…. Screenshot 3 shows daemon: http://localhost:3010/d/ae5be03faa146dd7. react-agentproto builds every URL from that base. Phones need a daemon that ships #313's session-chat build.

End-to-end run (local, nothing live touched)

Setup: a local rendezvous serve at its default --max-message-bytes (1 MiB), a scratch daemon from the #1450 build (own HOME, port 18897) serving #313's session-chat index.html (byte-identical), and headless Chrome at 390×844 mobile.

  1. Outdated: a stored pre-v2 credential for the daemon showed not paired · outdated pairing. A v1 offer link on /pair showed "from an older agentproto", and the broker logged no dial.
  2. Fresh v2 pair: pair offer --qr printed /pair#v=2&…&s=…. The page ran the handshake and showed name + fingerprint, and the fragment was gone. Confirm replaced the old credential and reset the worker.
  3. Control Center (1.45 MB page): loaded from the daemon through the worker over the 1 MiB-capped broker, on one stable channel with no reconnect churn (the large-frame fix, 87c96694 → 105c7f20 on #1450). The session list (session_list) and the send path (agent_start) went through /d/<id>/mcp.
  4. Offline: stopping the broker gave daemon offline · retrying. Restarting it brought the Control Center back automatically.
  5. Revoke: agentproto pair revoke moved the open Control Center to revoked on its own. "Forget" removed the credential and unregistered the worker, leaving not paired.

Not verified: a streamed agent reply. The scratch daemon has no LLM credentials (agent_start → missing_auth_credential), and I didn't copy real ones into a scratch home.

Per-daemon origins (local, NEXT_PUBLIC_AGENTPROTO_PAIR_DOMAIN=localhost)

*.localhost resolves to loopback and is a secure context in Chrome. *.localtest.me over http isn't, so service workers and WebCrypto are off there.

  • Mismatch: the ae5be03faa146dd7 offer opened on 0123456789abcdef.localhost:3010/pair was refused ("for a different daemon"). That origin ended with no IndexedDB database and no worker.
  • Match: the refusal page's link opened the same, still-unspent offer on ae5be03faa146dd7.localhost:3010. It paired, showed no shared notice, and loaded the Control Center from the daemon.
  • Wrong /d/ id: /d/0123456789abcdef on that origin showed "this address serves another daemon" with a link to the right origin.
  • Single origin: a fresh offer on 127.0.0.1:3010 paired and loaded the Control Center as before, with the shared-origin notice.
  • Bare pair domain: localhost:3010/pair (standing in for agentproto.cloud) refused to pair.

SSE through the service worker

  • Chunks are delivered as they arrive. /sessions/:id/events/stream delivered its : keep-alive comments at 0, 25, 50 … 375 s, each as its own read.
  • Long-lived streams survive. One SW fetch event carried that stream for 390 s+ (past Chrome's nominal 5-minute event budget), with no cut.
  • The /events bus used to die at exactly 120 s: the daemon tunnel's httpStreamIdleTimeoutMs with no keep-alive to reset it. With #1450's keep-alive fix it now ticks every 25 s and stayed open past 159 s.
  • /mcp GET SSE: session-chat doesn't open one. react-agentproto sends each MCP call as its own short POST /mcp (client.ts), and the network log shows only POST …/mcp. So the 120 s idle cut can't hit it.

Gates

No lockfile committed (as before). esbuild is a new devDependency.

Screenshots (390×844 mobile, pair/v2, default-cap broker)

Confirm Control Center under /d/<id>/ Sessions panel (base = scope)
Wrong origin (per-daemon) Shared-origin notice
Daemon offline Revoked Outdated pairing Not paired

🤖 @agentproto-bot — PR · session sess_515e1367 (rdv-browser PR3: cli-site /pair + service worker) · claude-code / subscription · auth-profile Claude Subs Agentik · model claude-opus-5-5 · supervisor sess_d3e835f4 · $43.0072 (daemon) · host mac.home · cwd agentik-studio/rdv-pair-page

/pair reads the offer from the URL fragment (what `agentproto pair offer
--qr` links to), clears it from history, runs the E2E handshake through the
rendezvous with @agentproto/pair-client, and asks the user to confirm the
daemon's name and fingerprint before storing the credential in IndexedDB.

A service worker registered per pairing at /d/<fingerprint>/ holds the one
TunnelClient and answers every request under that scope from the daemon, so
the Control Center loads unchanged from the daemon at
/d/<id>/apps/@agentik/session-chat/ui/ with its REST, /mcp and SSE calls
tunnelled too. /d/<id> is the state page (connecting, daemon offline and
retrying, revoked, not paired); the worker sends an open Control Center
there when the tunnel is revoked or stays offline.

Adds an add-to-home-screen manifest. The worker is bundled by
scripts/build-sw.mjs (esbuild) into public/pair-sw.js before dev/build.
A stored credential or a scanned offer from before pair/v2 can never
connect again (pair-client's `outdated` state / `protocol_outdated`,
refused before any dial). The worker treats it as terminal like a
revocation and sends an open Control Center to /d/<id>, which now shows a
"not paired · outdated pairing" page telling the user to update agentproto
and pair again from a new QR. /pair shows a matching message for a v1 offer.

Screenshots re-taken against pair/v2 through a rendezvous at its default
1 MiB message cap.
On a shared origin every stored pairing sits in the same IndexedDB and
behind the same service-worker origin, so any daemon's Control Center could
use every other daemon's pairing. The pairing pages now also run on a
dedicated per-daemon origin, https://<daemon fingerprint>.agentproto.cloud
(pair domain set at build time by NEXT_PUBLIC_AGENTPROTO_PAIR_DOMAIN):

- /pair refuses an offer whose daemon fingerprint isn't the host label,
  before any handshake (the offer stays unspent), and links to the offer on
  its daemon's own origin. Nothing is stored.
- The credential store, in the pages and in the worker, is scoped to the
  origin's daemon: other ids read as absent and are never written.
- /d/<id> for another daemon points to that daemon's origin; the bare pair
  domain or a non-fingerprint label refuses to pair.

Any other host (cli.agentproto.sh, localhost) keeps working as a shared
origin and says so in a short notice.
agentiknet added a commit to agentproto/ts that referenced this pull request Sep 26, 2026
… Worker) (#1457)

* feat(pair-page): per-daemon pair page on agentproto.cloud (Cloudflare Worker)

A private package with the phone pair page as one static bundle, served
identically on every <daemon-fingerprint>.agentproto.cloud by a Cloudflare
Worker (AIP-59 §5.8). Isolation comes from the browser origin; there is no
per-daemon server logic.

- /pair pairs from the offer in the fragment with @agentproto/pair-client,
  refuses an offer whose fingerprint isn't the origin's first label before
  any network I/O (the offer stays unspent), and asks to confirm the daemon.
- /d/<fp> is the status page (connecting, offline, revoked, outdated, not
  paired, wrong address). The service worker (dist/pair-sw.js) proxies
  /d/<fp>/ to the daemon over one tunnel client, so the Control Center loads
  from the daemon.
- The IndexedDB store, in the page and the worker, holds only the origin's
  daemon.
- Plain DOM, bundled with esbuild into dist/: no framework, no inline script
  or style, no third-party code. The build checks index.html for that.
- src/worker.ts (logic in src/edge.ts): 404 for the apex and any host whose
  first label isn't a fingerprint, extra hosts only via PREVIEW_HOSTS (a
  labelled preview mode), a strict CSP and security headers on every
  response, no-cache for the worker script and immutable hashed assets.
  wrangler.toml: workers_dev, [assets] with run_worker_first, no routes.
- scripts/preview.mjs serves dist/ through the same edge handler locally.

Ported from agentproto/cli-site#15.

* feat(secrets,cli,runtime): default pair page is one origin per daemon

`pair offer --qr` now links to https://{fp}.agentproto.cloud/pair by
default (DEFAULT_PAIR_PAGE = PAIR_WEB_URL_TEMPLATE_CLOUD), the page from
packages/pair-page. It stays configurable with pairing.pairPage and
--pair-page, as AIP-59 requires. encodeOfferWebUrl and the pairing.pairPage
config default follow. PAIR_WEB_URL (https://cli.agentproto.sh/pair) stays
exported as the explicit shared-origin option.

* feat(secrets)!: 128-bit identity fingerprint

identityFingerprint is now the first 32 lowercase hex chars (128 bits) of
sha256(x25519 pub), was 16 (64 bits). The fingerprint names the daemon's
browser origin (<fingerprint>.agentproto.cloud, AIP-59 §5.8): at 64 bits a
well-resourced attacker could grind a daemon key whose fingerprint collides
with a victim's and get their daemon's UI onto the victim's origin. 32 hex is
still one DNS label.

One fingerprint concept: the handshake's peerFingerprint (the daemon's key
seen by the client, the client's key seen by the daemon, the pairing handle)
uses the same function and widens with it.

- The offer codec requires `id` to be exactly 32 lowercase hex.
- The pair page and its Worker match ^[0-9a-f]{32}$.
- sealKeyId keeps 16 hex (it is persisted in keyring files); it is the
  fingerprint's prefix.
- Golden vectors: the fingerprint fields and the v2 offer URL id are
  re-derived; a KAT checks that the captured 64-bit values are their 16-char
  prefix, so only the length changed. The captured v1 offer keeps its id.
- pair ls widens its column; docs, examples and READMEs use 32-hex values.

BREAKING CHANGE: fingerprints, offer URLs and stored pairings from before this
change don't validate. pair/v2 is unreleased, so there is no migration.

* feat(pair-page): show the 128-bit fingerprint in groups of 4

32 hex chars don't fit one line on a phone; groups of 4 wrap between groups
and are easier to compare with the terminal. The gaps are CSS margins, so a
copy still yields the raw hex. Screenshots re-taken with 32-hex fingerprints.

* chore(pair-page): route *.agentproto.cloud/* to the Worker

Deployed after the workers.dev preview was verified; the zone carries the
proxied wildcard record (AAAA 100::) the route needs.

* docs(pair-page): production proof screenshots (agentproto.cloud, hosted rdv)

Captured on 2026-09-26 against the deployed Worker (cae88bea) and
wss://rdv.agentproto.sh/v1 with two scratch daemons: confirm and session
list on daemon A's origin, B's offer refused on A's origin, B paired on its
own origin, A's paired list, A revoked, B unaffected.

* fix(pair-page): don't rebuild the status page on an unchanged worker state

The status page redrew its whole view on every worker reply (every 2.5 s),
replacing its buttons each time, so a tap could land on a node that was just
swapped out (the production proof's Forget click failed that way). Draw only
when the shown state, error, name or fingerprint changes.

* chore: agentic reviewer — changeset

---------

Co-authored-by: agentproto-bot <bot@agentproto.dev>
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.

1 participant