How I built one-time browser pairing for my desktop AI companion: 73 tests, 0 repeated prompts, and a click that can never replay
Ankita AI is the desktop AI assistant I build — a little companion, a lot more possible — by Krish Verma. It can already drive a browser, but until recently every session started the same way: connect, answer the prompt, work, close everything, repeat tomorrow. The browser tools worked. The pairing did not survive anything. The fix: pair once, let Ankita use my signed-in browser without asking again — and make sure a click can never fire twice because a connection dropped.
The problem: the prompt was the feature
The old path built a Chrome DevTools command with --autoConnect and told the user Chrome asks each time. That is honest, but it is a tax on every session — and a remote-debugging port needs a non-default data directory from Chrome 136 onwards, so it cannot deliver "use my existing signed-in profile" at all. The extension route can: the debugger API gives an extension a DevTools transport without a debugging port, and the scripting API covers ordinary DOM work. So the design question was not "which transport?" but "what survives a restart?" The answer: the pairing identity and credential persist on both sides, while transport sessions and request IDs stay separate from that identity. A restarted process authenticates with a fresh session using the existing grant. Desktop close, browser close, worker suspension, transport loss — disconnections, not revocations. They must never delete the pairing or open another prompt.
The grant, the session, and the alarm
The whole wire policy lives in one file, desktop/browser-helper/control-protocol.mjs. The pairing lives under CONTROL_SESSION_KEY = 'pairedDesktop' in extension-private storage, so it survives worker and browser restarts. The reconnect loop, BrowserConnection in desktop/browser-helper/connection.mjs, says the quiet part out loud in its own comment: "Reconnects transport sessions, never browser commands."
The numbers that shape the loop:
- 1,500 ms initial retry, doubling to a 30,000 ms cap while the desktop is closed
- 0.5 minutes (Chrome's minimum alarm interval) to wake a suspended worker
- 20,000 ms long poll, under the extension fetch/worker idle deadline
- 30,000 ms bound on any page command, and a command is never retried once delivered
- 32 pending commands maximum — a bounded mailbox, not a replay queue
- 3 MB maximum result, including a base64 screenshot
Explicit Disconnect pauses automatic reconnect until the user chooses Connect again — no new pairing needed. Explicit Unpair/Revoke removes the saved grant and requires a new pairing. The two actions are labelled distinctly. And if access is removed, the transport outage never deletes the pairing: the status becomes browser_access_removed with the shared recovery line "Browser access removed. Pair with Ankita again."
DOM first, debugger only when the job needs it
Ordinary operations — open, navigate, snapshot, read, act, fill a form, screenshot — go through packaged extension code. The debugger is reserved for trusted input events, the page accessibility tree, background screenshots, and JavaScript dialogs. A synthetic DOM click is not a trusted input event, and I do not pretend otherwise.
That split exists because of one rule: do not retry a mutating action through a second path after an uncertain result. If a click was dispatched and the outcome is unknown, the code observes first — it never fires the click again through the debugger "just in case". A lost receipt must not cause the action to be dispatched twice, so the result POST is never retried. When the page changes, stale element references are invalidated and the model gets a plain instruction: "The page changed. Take a fresh snapshot before interacting." And when the connection drops mid-action: "Browser connection interrupted. A dispatched action may have completed; its outcome is uncertain. Inspect the page before continuing."
Chrome's own notices stay visible. Cancelling the debugger warning detaches the session, and I treat that as a revocation — not a reason to immediately reattach.
What the verification actually proved
The focused serial integration run passed 73 tests, 73 passed, 0 failed, covering pairing persistence, cancelled command admission, site/ref/job guards, and both app entry points under the shipped CSP. A real extension trace against a dedicated Chromium profile reported EXTENSION_LIVE_OK: one pairing, a DOM post and an Enter post exactly once each, a screenshot, restarts, pause/resume, and revoke recovery. The form received exactly two intended submissions in total, with no replay after restarts. The last shared-copy refactor rechecked 16/16 broker/reconnect/actual-extension cases.
Honest limits: the extension is loaded unpacked — no store distribution yet. Packaged desktop IPC, macOS/Linux/Edge acceptance, and the full test suite were not run for this change. The real-browser tests used a dedicated fixture profile, not my signed-in profile. Commands already sent before Stop cannot be undone, and uncertain receipts are not replayed.
Tell me I'm wrong
DOM-first with debugger escalation means two execution paths to maintain, and the debugger path carries Chrome's warning banner. Would a single debugger-only path be simpler and more honest — one code path, one capability model, banner and all? Or is the split worth it, since most of what a companion does in a browser is ordinary reading, clicking, and typing? And on the strict side: treating Chrome's Cancel as a revocation is the safe call, but is it too strict when the user just fat-fingered the banner?
The code is in the Ankita repo — desktop/browser-helper/control-protocol.mjs and connection.mjs are the two files to read first — and the project site is ankita-sable.vercel.app. If you have shipped an extension that survives restarts without nagging its user, where does your line sit?
— Krish Verma, creator of Ankita AI
Top comments (2)
The DOM-first split is worth the two paths, because the alternative trains users to click through a security warning every session — and a warning you see daily stops being a warning. Keeping Chrome's debugger banner visible and treating cancel as revocation is the right call for the same reason.
One edge I'd want to see tested: with a bounded mailbox of 32 pending commands, what happens when it fills during a long offline stretch — does the newest command drop, the oldest, or does the sender block? And does cancel-as-revocation distinguish a fat-fingered cancel from a deliberate one, or is re-pairing the only way back either way?
Good edge to poke at — I checked the code rather than guessing, and the answer is the least dramatic of the three: the sender gets an error, nothing is dropped.
execute() in desktop/electron/browser-control.mjs rejects with 'Browser command queue is full' as soon as pending.size reaches CONTROL_MAX_PENDING (32, in desktop/browser-helper/control-protocol.mjs) — so the newest command is refused outright, the oldest 32 stay put, and the caller sees the failure instead of it vanishing. Two things bound how bad that gets in practice: the mailbox is process-local (this.pending = new Map() in BrowserControl), nothing is replayed after a restart, and while the browser is offline execute() rejects even earlier with 'The paired browser is disconnected', so a long offline stretch never fills the queue in the first place. Each pending command also self-cancels after 30s (CONTROL_COMMAND_MS).
On the cancel question: no, it can't tell a fat-finger from a deliberate cancel — Chrome's detach reason is just canceled_by_user, and ExtensionBrowserOperations in desktop/browser-helper/browser-operations.mjs records the tab in a persisted revokedDebug set either way. But re-pairing isn't the way back: allowDebugger() clears that set, and it runs on Resume from the extension popup (background.mjs), so reconnecting restores deeper control with the pairing itself intact. Full re-pairing is only for an actual unpair.
Caveat: I haven't tested the full-queue path live — the rejection is in the code, but I couldn't find a test pinning 'queue is full', so the 30s self-cancel draining the mailbox under a stalled browser is the part I'd want a test for first.