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): keep an invoke the daemon is still holding alive on the control socket, so an agent waiting on an approval hears the answer rather than DAEMON_UNREACHABLE a minute in

Shahriarf0eee65f2aa2
diff --git a/docs/internals/request-path.md b/docs/internals/request-path.md
index 5d52417..99780b3 100644
--- a/docs/internals/request-path.md
+++ b/docs/internals/request-path.md
@@ -54,6 +54,13 @@ well as agent runs.
link allows 120 s for a screenshot, the computed typing duration plus 30 s for `page.typeText`, any
declared `timeoutMs` plus 5 s, and 30 s otherwise.
+**A held call is kept alive.** An approval can hold an invoke for as long as the user takes, and a
+scheduled run's for ten minutes. So `RemoteBridge` sends `keepAlive` with every invoke. The daemon
+answers with a `working` frame every 2 s until the result, and the bridge restarts its timeout on
+each one. Only a daemon that goes quiet is reported as `DAEMON_UNREACHABLE`. Without this, the agent
+heard "unreachable" a minute into every approval and asked again. A client that doesn't send
+`keepAlive` is never sent `working`.
+
**Screenshots are persisted by the daemon**, not the browser, and only on request.
`persistScreenshot()` writes nothing unless the call passed `save: true` or a mapping run supplied a
`saveTo` — so the captures an agent takes to look at a page leave no files behind. It reads `save`
diff --git a/src/daemon/control.ts b/src/daemon/control.ts
index d29c032..38f4824 100644
--- a/src/daemon/control.ts
+++ b/src/daemon/control.ts
@@ -26,10 +26,17 @@ export interface SessionSummary {
connected: boolean;
}
+/**
+ * An invoke can be held far longer than the caller's own timeout: an approval waits on the user.
+ * A caller that sends `keepAlive` hears `working` this often until the result, and restarts its
+ * timeout on each; one that doesn't — an older client — is never sent a frame it cannot read.
+ */
+export const INVOKE_KEEPALIVE_MS = 2_000;
+
export type ControlRequest =
| { id: string; op: 'describe'; runId?: string }
| { id: string; op: 'status' }
- | { id: string; op: 'invoke'; action: string; input?: unknown; runId?: string }
+ | { id: string; op: 'invoke'; action: string; input?: unknown; runId?: string; keepAlive?: boolean }
| { id: string; op: 'pair' }
| { id: string; op: 'sessions' }
| { id: string; op: 'revoke'; session?: string; origin?: string }
@@ -39,6 +46,7 @@ export type ControlMessage =
| { id: string; op: 'describe'; tools: ToolDescriptor[]; reserved?: string[] }
| { id: string; op: 'status'; status: BridgeStatus }
| { id: string; op: 'invoke'; result: ActionResult }
+ | { id: string; op: 'working' }
| { id: string; op: 'pair'; code: string; expiresAt: number }
| { id: string; op: 'sessions'; sessions: SessionSummary[] }
| { id: string; op: 'revoke'; revoked: number }
diff --git a/src/daemon/daemon.ts b/src/daemon/daemon.ts
index 4e4bd4f..1e0c020 100644
--- a/src/daemon/daemon.ts
+++ b/src/daemon/daemon.ts
@@ -59,7 +59,15 @@ import { agentState, grantRunner, refreshModelLists } from './agent/runners';
import { deleteSiteMap, deleteSkill, saveSkill } from './agent/skill-store';
import { loadSkills } from './agent/skills';
import { commitStaging, discardStaging } from './agent/site-map-store';
-import type { Bridge, BridgeStatus, ControlMessage, ControlRequest, Described, SessionSummary } from './control';
+import {
+ INVOKE_KEEPALIVE_MS,
+ type Bridge,
+ type BridgeStatus,
+ type ControlMessage,
+ type ControlRequest,
+ type Described,
+ type SessionSummary,
+} from './control';
import { ExtensionLink } from './extension-link';
import { startScheduler } from './schedules/scheduler';
import { refreshNativeHost } from './native-host';
@@ -702,16 +710,23 @@ export async function startDaemon({ version, idleExit = true }: DaemonOptions):
return send(ws, { id: request.id, op: 'status', status: statusNow(routeFor(binding)) });
}
if (request.op === 'invoke') {
+ const working = request.keepAlive
+ ? setInterval(() => send(ws, { id: request.id, op: 'working' }), INVOKE_KEEPALIVE_MS)
+ : undefined;
let result: ActionResult;
- if (request.runId) {
- result =
- (await sessionRunning(request.runId)?.invokeForRun(request.runId, request.action, request.input)) ??
- failure('RUN_INACTIVE', 'This agent run is no longer active');
- if (!result.ok && result.error.code === 'RUN_INACTIVE') {
- log(`control ${client} invoked ${request.action} for inactive run ${request.runId}`);
+ try {
+ if (request.runId) {
+ result =
+ (await sessionRunning(request.runId)?.invokeForRun(request.runId, request.action, request.input)) ??
+ failure('RUN_INACTIVE', 'This agent run is no longer active');
+ if (!result.ok && result.error.code === 'RUN_INACTIVE') {
+ log(`control ${client} invoked ${request.action} for inactive run ${request.runId}`);
+ }
+ } else {
+ result = await invokeExternal(routeFor(binding), request.action, request.input, client);
}
- } else {
- result = await invokeExternal(routeFor(binding), request.action, request.input, client);
+ } finally {
+ clearInterval(working);
}
return send(ws, { id: request.id, op: 'invoke', result });
}
diff --git a/src/daemon/remote-bridge.test.ts b/src/daemon/remote-bridge.test.ts
index f2dfc8a..fea2b32 100644
--- a/src/daemon/remote-bridge.test.ts
+++ b/src/daemon/remote-bridge.test.ts
@@ -72,7 +72,7 @@ describe('asking the daemon', () => {
const result = await bridge.invoke('page.clickElement', { target: { text: 'Buy' } });
expect([result, received[0]]).toEqual([
{ ok: true, data: { clicked: true } },
- { id: expect.any(String), op: 'invoke', action: 'page.clickElement', input: { target: { text: 'Buy' } }, runId: 'run-9' },
+ { id: expect.any(String), op: 'invoke', action: 'page.clickElement', input: { target: { text: 'Buy' } }, runId: 'run-9', keepAlive: true },
]);
});
@@ -170,6 +170,46 @@ describe('when the daemon does not answer', () => {
]);
});
+ describe('while the daemon says it is still working on an action', () => {
+ const unreachable = { ok: false, error: { code: 'DAEMON_UNREACHABLE', message: 'The Browsentic daemon did not respond' } };
+
+ /** Frames on one socket arrive in order, so once the manifest event is heard, so was the frame before it. */
+ const deliver = async (message: ControlMessage) => {
+ const read = new Promise<void>((resolve) => bridge.onManifestChanged(resolve));
+ daemonSide.send(JSON.stringify(message));
+ daemonSide.send(JSON.stringify({ event: 'manifest-changed' }));
+ await read;
+ };
+
+ const held = async () => {
+ const asked = new Promise<string>((resolve) => (answer = ({ id }) => (resolve(id), null)));
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'Date'] });
+ const started = Date.now();
+ const call = bridge.invoke('page.submitForm');
+ return { id: await asked, call, took: () => Date.now() - started };
+ };
+
+ test('the action outlives the minute, for as long as it keeps hearing so', async () => {
+ const { id, call, took } = await held();
+ for (let minute = 0; minute < 3; minute++) {
+ await vi.advanceTimersByTimeAsync(50_000);
+ await deliver({ id, op: 'working' });
+ }
+ await deliver({ id, op: 'invoke', result: { ok: true, data: { submitted: true } } });
+ expect([await call, took()]).toEqual([{ ok: true, data: { submitted: true } }, 150_000]);
+ });
+
+ test('once the daemon falls silent, the action gives up a minute after the last word', async () => {
+ const { id, call, took } = await held();
+ await vi.advanceTimersByTimeAsync(50_000);
+ await deliver({ id, op: 'working' });
+ let outcome: unknown;
+ void call.then((result) => (outcome = result));
+ while (!outcome) await vi.advanceTimersToNextTimerAsync();
+ expect([outcome, took()]).toEqual([unreachable, 110_000]);
+ });
+ });
+
test('once closed, calls fail at once without being sent', async () => {
await bridge.close();
await vi.waitFor(() => expect(daemonSide.readyState).toBe(daemonSide.CLOSED));
diff --git a/src/daemon/remote-bridge.ts b/src/daemon/remote-bridge.ts
index 5ce0ff2..f55403f 100644
--- a/src/daemon/remote-bridge.ts
+++ b/src/daemon/remote-bridge.ts
@@ -39,7 +39,7 @@ export class RemoteBridge implements Bridge {
async invoke(action: string, input?: unknown): Promise<ActionResult> {
const reply = await this.request(
- { id: randomUUID(), op: 'invoke', action, input, runId: this.runId },
+ { id: randomUUID(), op: 'invoke', action, input, runId: this.runId, keepAlive: true },
invokeTimeoutFor(action, input),
);
if (reply && 'result' in reply) return reply.result;
@@ -87,14 +87,18 @@ export class RemoteBridge implements Bridge {
private request(request: ControlRequest, timeoutMs = REQUEST_TIMEOUT_MS): Promise<ControlMessage | null> {
if (this.socket.readyState !== WebSocket.OPEN) return Promise.resolve(null);
return new Promise((resolve) => {
- const timer = setTimeout(() => {
- this.pending.delete(request.id);
- resolve(null);
- }, timeoutMs);
- this.pending.set(request.id, (message) => {
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const finish = (message: ControlMessage | null) => {
clearTimeout(timer);
+ this.pending.delete(request.id);
resolve(message);
- });
+ };
+ const wait = () => {
+ clearTimeout(timer);
+ timer = setTimeout(() => finish(null), timeoutMs);
+ };
+ wait();
+ this.pending.set(request.id, (message) => ('op' in message && message.op === 'working' ? wait() : finish(message)));
this.socket.send(JSON.stringify(request));
});
}
@@ -110,10 +114,7 @@ export class RemoteBridge implements Bridge {
if (message.event === 'manifest-changed') for (const listener of this.manifestListeners) listener();
return;
}
- const settle = this.pending.get(message.id);
- if (!settle) return;
- this.pending.delete(message.id);
- settle(message);
+ this.pending.get(message.id)?.(message);
}
}
diff --git a/src/daemon/test/browsers.test.ts b/src/daemon/test/browsers.test.ts
index f550e4a..4883873 100644
--- a/src/daemon/test/browsers.test.ts
+++ b/src/daemon/test/browsers.test.ts
@@ -1,6 +1,7 @@
import { mkdirSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { afterAll, afterEach, beforeAll, describe, expect, test, vi } from 'vitest';
+import { WebSocket } from 'ws';
import { clearAuth } from '../auth-store';
import { startDaemon, type Daemon } from '../daemon';
import { readLockfile, stateDir } from '../lockfile';
@@ -190,6 +191,37 @@ describe('a caller outside any browser', () => {
});
});
+describe('an action the browser takes a while over', () => {
+ test('a caller that asks hears the daemon is still working; one that does not hears only the result', async () => {
+ const browser = await pair(chrome);
+ await settled();
+ let release!: () => void;
+ browser.holdInvokes = new Promise((resolve) => (release = resolve));
+
+ const raw = new WebSocket(`ws://127.0.0.1:${daemon.port}/control`, { headers: { authorization: `Bearer ${readLockfile()!.token}` } });
+ opened.push(raw);
+ await new Promise((resolve) => raw.once('open', resolve));
+ const heard: string[] = [];
+ const working = new Promise<void>((resolve) =>
+ raw.on('message', (frame) => {
+ const { id, op } = JSON.parse(String(frame)) as { id: string; op: string };
+ heard.push(`${id} ${op}`);
+ if (op === 'working') resolve();
+ }),
+ );
+ raw.send(JSON.stringify({ id: 'patient', op: 'invoke', action: 'page.getPageInfo', keepAlive: true }));
+ raw.send(JSON.stringify({ id: 'plain', op: 'invoke', action: 'page.getPageInfo' }));
+
+ await working;
+ release();
+ await vi.waitFor(() => expect(heard.filter((line) => line.endsWith(' invoke'))).toHaveLength(2));
+ expect([heard.filter((line) => line.startsWith('patient')), heard.filter((line) => line.startsWith('plain'))]).toEqual([
+ ['patient working', 'patient invoke'],
+ ['plain invoke'],
+ ]);
+ });
+});
+
describe('the tool list a caller is offered', () => {
const offeredTo = async (bridge: RemoteBridge) => (await bridge.describe()).tools.map(({ name }) => name);
diff --git a/src/daemon/test/fake-browser.ts b/src/daemon/test/fake-browser.ts
index c5ccbd5..287ff8b 100644
--- a/src/daemon/test/fake-browser.ts
+++ b/src/daemon/test/fake-browser.ts
@@ -35,6 +35,8 @@ export class FakeBrowser {
taskReply: ActionResult = { ok: true, data: { sessionId: 'fake-session' } };
/** The task list the daemon last pushed or answered with. */
tasks: TaskList | null = null;
+ /** While set, every invoke is answered only once it resolves. */
+ holdInvokes: Promise<void> | null = null;
private readonly replies = new Map<string, (frame: SocketFrame) => void>();
readonly closed: Promise<string>;
/** Resolves once this browser has answered the daemon's request for its manifest — a drifted build is asked right behind the welcome. */
@@ -139,7 +141,10 @@ export class FakeBrowser {
}
if (frame?.t !== 'invoke') return;
this.invoked.push(frame.action);
- send(this.socket, { t: 'result', id: frame.id, result: { ok: true, data: { answeredBy: this.profile.installId } } });
+ const answer = () =>
+ send(this.socket, { t: 'result', id: frame.id, result: { ok: true, data: { answeredBy: this.profile.installId } } });
+ if (this.holdInvokes) void this.holdInvokes.then(answer);
+ else answer();
}
}