Skip to content

An agentic browser harness: an AI side panel & orb in your own logged-in Chrome, Edge, Arc, Brave or even Firefox run by the agent/subscription you already use (Claude Code, Codex, Antigravity). Free, open source, no API key. Also an MCP server.

fix(daemon): order browsers by a counter rather than a clock, because two focus frames within one millisecond left the earlier browser in front

Shahriard839e17c2aaf
diff --git a/src/daemon/daemon.ts b/src/daemon/daemon.ts
index 6d801d4..e393529 100644
--- a/src/daemon/daemon.ts
+++ b/src/daemon/daemon.ts
@@ -463,7 +463,7 @@ export async function startDaemon({ version, idleExit = true }: DaemonOptions):
/** The browser the user was last in, for a caller that has no run to say which one it means. */
function activeLink(): ExtensionLink | null {
return openLinks().reduce<ExtensionLink | null>(
- (latest, link) => (latest && latest.lastActiveAt >= link.lastActiveAt ? latest : link),
+ (latest, link) => (latest && latest.lastActive >= link.lastActive ? latest : link),
null,
);
}
diff --git a/src/daemon/extension-link.ts b/src/daemon/extension-link.ts
index cc766b3..77490d3 100644
--- a/src/daemon/extension-link.ts
+++ b/src/daemon/extension-link.ts
@@ -25,6 +25,10 @@ interface Pending {
timer: ReturnType<typeof setTimeout>;
}
+let activity = 0;
+/** A counter rather than a clock: two browsers can be touched within one millisecond, and the later one has to win. */
+const touched = () => ++activity;
+
export class ExtensionLink {
readonly extensionVersion: string;
readonly manifestHash: string;
@@ -32,7 +36,7 @@ export class ExtensionLink {
readonly id: string;
readonly browser?: string;
/** Which connected browser the user was last in, so a caller that names none reaches that one. */
- lastActiveAt = Date.now();
+ lastActive = touched();
/** What this browser offers: a Firefox build lists fewer tools than a Chromium one, and a drifted build its own. */
tools: ToolDescriptor[] = [];
private readonly pending = new Map<string, Pending>();
@@ -112,9 +116,9 @@ export class ExtensionLink {
if (!frame) return log('dropped unparseable frame from extension');
if (frame.t === 'ping') return this.send({ t: 'pong', id: frame.id });
if (frame.t === 'pong') return;
- if (frame.t === 'focus') return void (this.lastActiveAt = Date.now());
+ if (frame.t === 'focus') return void (this.lastActive = touched());
if (isExtensionRequest(frame)) {
- this.lastActiveAt = Date.now();
+ this.lastActive = touched();
return this.onRequest?.(frame, this);
}
diff --git a/src/daemon/test/browsers.test.ts b/src/daemon/test/browsers.test.ts
index 39e7c8c..f550e4a 100644
--- a/src/daemon/test/browsers.test.ts
+++ b/src/daemon/test/browsers.test.ts
@@ -1,6 +1,6 @@
import { mkdirSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
-import { afterAll, afterEach, beforeAll, describe, expect, test } from 'vitest';
+import { afterAll, afterEach, beforeAll, describe, expect, test, vi } from 'vitest';
import { clearAuth } from '../auth-store';
import { startDaemon, type Daemon } from '../daemon';
import { readLockfile, stateDir } from '../lockfile';
@@ -147,6 +147,22 @@ describe('a caller outside any browser', () => {
]);
});
+ test('follows the browser focused last, even when both were focused within the same millisecond', async () => {
+ const first = await pair(chrome);
+ const second = await pair(brave);
+ await settled();
+ vi.useFakeTimers({ toFake: ['Date'] });
+ try {
+ await first.focus();
+ await second.focus();
+ const viaSecond = await answeredBy(await control());
+ await first.focus();
+ expect([viaSecond, await answeredBy(await control())]).toEqual([brave.installId, chrome.installId]);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
test('moves to the browser still connected when its own goes away', async () => {
const first = await pair(chrome);
await pair(brave);