Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Saved downloads: fit the reply limit, refuse a swapped path; reset refusal count

Emre Sokulluc253825b8665
diff --git a/firefox-companion/README.md b/firefox-companion/README.md
index 3e26ca6f1..554bf6557 100644
--- a/firefox-companion/README.md
+++ b/firefox-companion/README.md
@@ -28,7 +28,7 @@ The connection test checks the real BiDi session. It does not submit forms or se
- Typing dispatches one character at a time, checking run ownership, deadline and focus before each step. Stop prevents the next step; a single already-dispatched character can still arrive. Newlines use a literal editing command, never Enter; single-line fields reject multiline payloads before clearing.
- Exact value readback after typing. `set_field` submits with Enter only after verification. An uncertain dispatch is not safe to repeat automatically.
- Active-run dialogs: acknowledge alerts; dismiss confirmations and prompts; stay on beforeunload unless an explicit navigate action is in progress. A Leave allowance is single use. This prevents prompts from hanging input without blanket approval of destructive confirmations. Idle tabs are not auto-handled. Pending dialog events are retained and handled when a task takes ownership; manually closed dialogs are discarded. Browser permission dialogs and arbitrary operating-system dialogs are outside this scope.
-- Saved downloads (capability `saved_download`): `readSavedDownload` reads a completed download's bytes from the path the browser recorded for it, for an open run only. It must be a regular file (not a link) of at most 25 MB that still has the recorded size and does not change while it is read. `upload_file({downloadId})` attaches those exact bytes. With an older companion, or without trusted automation, the extension fetches the file again from its address and refuses bytes whose size differs from the saved file.
+- Saved downloads (capability `saved_download`): `readSavedDownload` reads a completed download's bytes from the path the browser recorded for it, for an open run only. It must be a regular file, opened without following a link and bound to the file the check saw, that still has the recorded size and does not change while it is read, and at most about 24 MB, which is what a reply can carry within the 32 MiB native transfer limit. `upload_file({downloadId})` attaches those exact bytes. With an older companion, without trusted automation, or for a larger file (up to the 25 MB upload limit), the extension fetches the file again from its address and refuses bytes whose size differs from the saved file.
- Vision mode's point input (capability `point_input`, which `connect` reports): `click_at` (one left click) and `hover_at` at a whole-pixel CSS point of the run's top document, rechecked against the element the extension marked there. A drag, a wheel, a right, middle or repeated click and a point over an embedded frame or inside an open shadow root stay synthetic; a BiDi wheel scrolls only the window's selected tab. With an older companion, which reports no capability, all vision input stays synthetic.
- Uploads keep the existing WebBrain attachment/download/picker authorization. Bytes are written to a private temporary file, passed to `input.setFiles`, and the file input's name and size are checked. The input must be unique, enabled, and in the current document's light DOM. Attachment is not proof the site has uploaded or submitted the file. Arbitrary model-supplied filesystem paths are not accepted.
diff --git a/firefox-companion/session.mjs b/firefox-companion/session.mjs
index 5cf3e29af..f1e8d3360 100644
--- a/firefox-companion/session.mjs
+++ b/firefox-companion/session.mjs
@@ -1,4 +1,5 @@
import { mkdtemp, writeFile, rm, open, lstat } from 'node:fs/promises';
+import { constants as FS } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, basename, isAbsolute } from 'node:path';
@@ -185,7 +186,12 @@ function unpackRemoteObjectValue(value) {
// `connect`. point_input: vision mode's click_at and hover_at, trusted input
// at a viewport point of the run's top document.
export const COMPANION_CAPABILITIES = Object.freeze(['point_input', 'saved_download']);
-const SAVED_DOWNLOAD_MAX_BYTES = 25 * 1024 * 1024;
+// What a reply can carry as base64 within the 32 MiB native transfer limit
+// (native-messages.mjs), with room for its envelope. A larger download is
+// refused with SAVED_DOWNLOAD_TOO_LARGE, and the extension falls back to its
+// own size-checked route.
+const SAVED_DOWNLOAD_MAX_BYTES = 24 * 1024 * 1024 - 64 * 1024;
+export const SAVED_DOWNLOAD_TOO_LARGE = 'saved_download_too_large';
// Vision mode's point actions (point_input).
const POINT_ACTIONS = new Set(['click_at', 'hover_at']);
@@ -290,13 +296,20 @@ export class BidiSession {
}
const link = await lstat(download.filename);
if (!link.isFile()) throw new Error('The saved download is not a regular file');
- const file = await open(download.filename, 'r');
+ // Opened without following a link, and bound to the file lstat saw: a
+ // path swapped in between is refused, not read.
+ const file = await open(download.filename, FS.O_RDONLY | (FS.O_NOFOLLOW || 0) | (FS.O_NONBLOCK || 0));
try {
const before = await file.stat();
+ if (!before.isFile() || before.dev !== link.dev || before.ino !== link.ino) {
+ throw new Error('The saved download changed before it was read');
+ }
if (Number.isSafeInteger(download.fileSize) && download.fileSize >= 0 && download.fileSize !== before.size) {
throw new Error('The saved download no longer matches its browser record');
}
- if (before.size > SAVED_DOWNLOAD_MAX_BYTES) throw new Error('File size exceeds 25MB limit.');
+ if (before.size > SAVED_DOWNLOAD_MAX_BYTES) {
+ throw new Error(`${SAVED_DOWNLOAD_TOO_LARGE}: the saved download is larger than the companion can return.`);
+ }
const bytes = Buffer.alloc(before.size);
let offset = 0;
while (offset < bytes.length) {
@@ -305,7 +318,7 @@ export class BidiSession {
offset += part.bytesRead;
}
const after = await file.stat();
- if (!this.runs.get(runId) || after.size !== before.size || after.mtimeMs !== before.mtimeMs || after.ino !== before.ino) {
+ if (!this.runs.get(runId) || after.size !== before.size || after.mtimeMs !== before.mtimeMs) {
throw new Error('The saved download or the run changed while it was read');
}
return { base64: bytes.toString('base64'), size: bytes.length, filename: basename(download.filename), mimeType: String(download.mime || '') };
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 527addc28..b9332659b 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -22934,6 +22934,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
refusals.bySignature.set(signature, repeats);
refusals.total += 1;
if (repeats < 3 && refusals.total < 6) return null;
+ // The run stops here; a recovery that carries on starts counting again.
+ state.refusals = null;
const detail = String(result?.error || 'the action was refused').replace(/\s+/g, ' ').trim().slice(0, 240);
return `Stopped: WebBrain refused ${repeats >= 3 ? 'the same action three times' : 'six actions'} in a row without any action going through (${detail}). The task is not complete; nothing more was sent, and any draft is left as it is.`;
}
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 04b90d39c..b10c1191b 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -17255,6 +17255,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
try { saved = await firefoxBidi.readSavedDownload(tabId, item); }
catch (error) {
if (error?.name === 'AbortError') throw error;
+ // Larger than a companion reply can carry: the size-checked fetch.
+ if (String(error?.message || '').startsWith('saved_download_too_large')) return null;
return { error: `Could not read the saved download (${String(error?.message || error).slice(0, 160)}).` };
}
if (!saved || typeof saved.base64 !== 'string') return null;
@@ -19759,6 +19761,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
refusals.bySignature.set(signature, repeats);
refusals.total += 1;
if (repeats < 3 && refusals.total < 6) return null;
+ // The run stops here; a recovery that carries on starts counting again.
+ state.refusals = null;
const detail = String(result?.error || 'the action was refused').replace(/\s+/g, ' ').trim().slice(0, 240);
return `Stopped: WebBrain refused ${repeats >= 3 ? 'the same action three times' : 'six actions'} in a row without any action going through (${detail}). The task is not complete; nothing more was sent, and any draft is left as it is.`;
}
diff --git a/test/firefox-bidi.mjs b/test/firefox-bidi.mjs
index 9de51ace2..d0df838c1 100644
--- a/test/firefox-bidi.mjs
+++ b/test/firefox-bidi.mjs
@@ -73,6 +73,17 @@ test('the companion reads a saved download only as the browser recorded it', asy
const link = join(dir, 'link.pdf');
await symlink(file, link);
await assert.rejects(session.readSavedDownload(runId, { ...record, filename: link }), /not a regular file/);
+ // The largest file the companion returns still fits a native reply; a larger one is refused with the fallback marker.
+ const { nativeReplyMessages } = await import('../firefox-companion/native-messages.mjs');
+ const { SAVED_DOWNLOAD_TOO_LARGE } = await import('../firefox-companion/session.mjs');
+ const limit = 24 * 1024 * 1024 - 64 * 1024;
+ const largest = join(dir, 'largest.bin');
+ await writeFile(largest, Buffer.alloc(limit, 1));
+ const fits = await session.readSavedDownload(runId, { ...record, filename: largest, fileSize: limit });
+ assert.ok([...nativeReplyMessages({ id: 'request-123456', result: fits })].length > 1);
+ const larger = join(dir, 'larger.bin');
+ await writeFile(larger, Buffer.alloc(limit + 1, 1));
+ await assert.rejects(session.readSavedDownload(runId, { ...record, filename: larger, fileSize: limit + 1 }), error => error.message.startsWith(SAVED_DOWNLOAD_TOO_LARGE));
} finally {
await session.close();
await rm(dir, { recursive: true, force: true });
diff --git a/test/vision-mode-agent.mjs b/test/vision-mode-agent.mjs
index babedb58e..8bfb83aa6 100644
--- a/test/vision-mode-agent.mjs
+++ b/test/vision-mode-agent.mjs
@@ -721,6 +721,8 @@ for (const build of (process.env.VISION_BUILDS || 'chrome,firefox').split(','))
const stops = [1, 2, 3, 4, 5, 6].map(i => other._noteRefusalWithoutDispatch(tab, 'click', { x: i, y: i }, refusal));
assert.deepEqual(stops.map(Boolean), [false, false, false, false, false, true]);
assert.match(stops[5], /six actions/);
+ // A stop clears the count: a recovery that carries on starts again.
+ assert.equal(other._noteRefusalWithoutDispatch(tab, 'click', { x: 9, y: 9 }, refusal), null);
});
test(`${build}: a page that changed while the model decided refuses its points; a scroll gets them re-checked`, async () => {
diff --git a/test/vision-mode-guards.mjs b/test/vision-mode-guards.mjs
index 8124a6cf2..f7474134f 100644
--- a/test/vision-mode-guards.mjs
+++ b/test/vision-mode-guards.mjs
@@ -638,6 +638,8 @@ for (const build of ['chrome', 'firefox']) {
assert.equal(await agent._firefoxSavedDownloadBytes(9, item), null, 'no companion: the caller fetches again');
firefoxBidi.readSavedDownload = async () => { throw new Error('The saved download no longer matches its browser record'); };
assert.match((await agent._firefoxSavedDownloadBytes(9, item)).error, /no longer matches/);
+ firefoxBidi.readSavedDownload = async () => { throw new Error('saved_download_too_large: the saved download is larger than the companion can return.'); };
+ assert.equal(await agent._firefoxSavedDownloadBytes(9, item), null, 'too large for a reply: the size-checked fetch');
assert.match((await agent._firefoxSavedDownloadBytes(9, { ...item, exists: false })).error, /no longer on disk/);
} finally {
firefoxBidi.readSavedDownload = saved;