How I built one-time browser pairing for my desktop AI companion: 73 tests, 0 repeated prompts, and a click that can never replay
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
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
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.