Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
fix(providers): address codex round 5 - bare-base64 scrub, response scrub, wrapper-aware caps
Emre Sokullud402a81abdad
diff --git a/src/chrome/src/trace/webbrain-share-outbox.js b/src/chrome/src/trace/webbrain-share-outbox.jsindex 4cbf238d4..6322424fc 100644--- a/src/chrome/src/trace/webbrain-share-outbox.js+++ b/src/chrome/src/trace/webbrain-share-outbox.js@@ -34,9 +34,9 @@ function clampText(value, limit = MAX_MESSAGE_CHARS) {function scrubText(value, limit = MAX_MESSAGE_CHARS) {if (typeof value !== 'string') return value;let text = value;- // Detect data URIs regardless of surrounding string or payload length: even- // a 1x1 canvas/QR thumbnail is image bytes the UI promises to strip.if (text.includes('data:')) {+ // Detect data URIs regardless of surrounding string or payload length:+ // even a 1x1 canvas/QR thumbnail is image bytes the UI promises to strip.// Newlines (but not spaces/words) are allowed inside the payload so// wrapped base64 is still removed while surrounding prose survives.text = text.replace(@@ -44,6 +44,15 @@ function scrubText(value, limit = MAX_MESSAGE_CHARS) {'[embedded base64 data omitted]',);}+ // Bare base64 blobs without a data: wrapper (e.g. read_downloaded_file+ // serializes small binaries as {"base64":"..."} into string tool content).+ // A 200+ run of base64 alphabet characters never occurs in prose, hashes,+ // or URLs (delimiters break the run), so redact it from research copies.+ if (/[A-Za-z0-9+/=\r\n]{200,}/.test(text)) {+ text = text+ .replace(/"base64"\s*:\s*"[A-Za-z0-9+/=\r\n]+"/g, '"base64":"[omitted]"')+ .replace(/[A-Za-z0-9+/]{200}[A-Za-z0-9+/=\r\n]*/g, '[embedded base64 data omitted]');+ }return bounded(text, limit);}@@ -173,10 +182,33 @@ function scrubMessages(messages) {}kept.reverse();const headOmitted = startIndex > (hasSystemPrompt ? 1 : 0);+ const omissionMarker = { role: 'system', content: '[earlier shared messages omitted]' };const out = [];if (hasSystemPrompt) out.push(scrubbedAll[0].copy);- if (headOmitted) out.push({ role: 'system', content: '[earlier shared messages omitted]' });+ if (headOmitted) out.push(omissionMarker);for (const entry of kept) out.push(entry.copy);+ // Charge the wrapper entries and JSON array delimiters, which the+ // per-message budget above does not count: trim oldest shared turns until+ // both the message cap and the byte budget hold for the final payload.+ const wrapperFloor = out.length - kept.length; // system + marker entries+ let outJson = '';+ try { outJson = JSON.stringify(out); } catch { outJson = ''; }+ let trimmed = false;+ while ((out.length > MAX_SCRUBBED_MESSAGES || (outJson && outJson.length > MAX_REQUEST_BUDGET))+ && out.length > wrapperFloor) {+ out.splice(wrapperFloor, 1);+ trimmed = true;+ try { outJson = JSON.stringify(out); } catch { break; }+ }+ if (trimmed && !headOmitted) {+ out.splice(hasSystemPrompt ? 1 : 0, 0, omissionMarker);+ if (out.length > MAX_SCRUBBED_MESSAGES) out.splice(hasSystemPrompt ? 2 : 1, 1);+ try { outJson = JSON.stringify(out); } catch { /* keep best effort */ }+ while (outJson && outJson.length > MAX_REQUEST_BUDGET && out.length > (hasSystemPrompt ? 2 : 1)) {+ out.splice(hasSystemPrompt ? 2 : 1, 1);+ try { outJson = JSON.stringify(out); } catch { break; }+ }+ }return out;}@@ -202,7 +234,7 @@ export function buildShareGenerationItem({model: String(model || '').slice(0, 255),mode: String(mode || '').slice(0, 32),request,- response: { role: 'assistant', content: clampText(responseContent, MAX_RESPONSE_CHARS) },+ response: { role: 'assistant', content: scrubText(responseContent, MAX_RESPONSE_CHARS) },};}diff --git a/src/firefox/src/trace/webbrain-share-outbox.js b/src/firefox/src/trace/webbrain-share-outbox.jsindex 4cbf238d4..6322424fc 100644--- a/src/firefox/src/trace/webbrain-share-outbox.js+++ b/src/firefox/src/trace/webbrain-share-outbox.js@@ -34,9 +34,9 @@ function clampText(value, limit = MAX_MESSAGE_CHARS) {function scrubText(value, limit = MAX_MESSAGE_CHARS) {if (typeof value !== 'string') return value;let text = value;- // Detect data URIs regardless of surrounding string or payload length: even- // a 1x1 canvas/QR thumbnail is image bytes the UI promises to strip.if (text.includes('data:')) {+ // Detect data URIs regardless of surrounding string or payload length:+ // even a 1x1 canvas/QR thumbnail is image bytes the UI promises to strip.// Newlines (but not spaces/words) are allowed inside the payload so// wrapped base64 is still removed while surrounding prose survives.text = text.replace(@@ -44,6 +44,15 @@ function scrubText(value, limit = MAX_MESSAGE_CHARS) {'[embedded base64 data omitted]',);}+ // Bare base64 blobs without a data: wrapper (e.g. read_downloaded_file+ // serializes small binaries as {"base64":"..."} into string tool content).+ // A 200+ run of base64 alphabet characters never occurs in prose, hashes,+ // or URLs (delimiters break the run), so redact it from research copies.+ if (/[A-Za-z0-9+/=\r\n]{200,}/.test(text)) {+ text = text+ .replace(/"base64"\s*:\s*"[A-Za-z0-9+/=\r\n]+"/g, '"base64":"[omitted]"')+ .replace(/[A-Za-z0-9+/]{200}[A-Za-z0-9+/=\r\n]*/g, '[embedded base64 data omitted]');+ }return bounded(text, limit);}@@ -173,10 +182,33 @@ function scrubMessages(messages) {}kept.reverse();const headOmitted = startIndex > (hasSystemPrompt ? 1 : 0);+ const omissionMarker = { role: 'system', content: '[earlier shared messages omitted]' };const out = [];if (hasSystemPrompt) out.push(scrubbedAll[0].copy);- if (headOmitted) out.push({ role: 'system', content: '[earlier shared messages omitted]' });+ if (headOmitted) out.push(omissionMarker);for (const entry of kept) out.push(entry.copy);+ // Charge the wrapper entries and JSON array delimiters, which the+ // per-message budget above does not count: trim oldest shared turns until+ // both the message cap and the byte budget hold for the final payload.+ const wrapperFloor = out.length - kept.length; // system + marker entries+ let outJson = '';+ try { outJson = JSON.stringify(out); } catch { outJson = ''; }+ let trimmed = false;+ while ((out.length > MAX_SCRUBBED_MESSAGES || (outJson && outJson.length > MAX_REQUEST_BUDGET))+ && out.length > wrapperFloor) {+ out.splice(wrapperFloor, 1);+ trimmed = true;+ try { outJson = JSON.stringify(out); } catch { break; }+ }+ if (trimmed && !headOmitted) {+ out.splice(hasSystemPrompt ? 1 : 0, 0, omissionMarker);+ if (out.length > MAX_SCRUBBED_MESSAGES) out.splice(hasSystemPrompt ? 2 : 1, 1);+ try { outJson = JSON.stringify(out); } catch { /* keep best effort */ }+ while (outJson && outJson.length > MAX_REQUEST_BUDGET && out.length > (hasSystemPrompt ? 2 : 1)) {+ out.splice(hasSystemPrompt ? 2 : 1, 1);+ try { outJson = JSON.stringify(out); } catch { break; }+ }+ }return out;}@@ -202,7 +234,7 @@ export function buildShareGenerationItem({model: String(model || '').slice(0, 255),mode: String(mode || '').slice(0, 32),request,- response: { role: 'assistant', content: clampText(responseContent, MAX_RESPONSE_CHARS) },+ response: { role: 'assistant', content: scrubText(responseContent, MAX_RESPONSE_CHARS) },};}diff --git a/test/run.js b/test/run.jsindex 8fe037639..58f7c61a4 100644--- a/test/run.js+++ b/test/run.js@@ -12478,7 +12478,7 @@ test('Share-for-research item scrubs images and clamps oversized content', () =>{role: 'user',content: [- { type: 'text', text: 'A'.repeat(12_000) },+ { type: 'text', text: 'Long prose argument ' + 'lorem ipsum dolor sit amet '.repeat(500) },{ type: 'image_url', image_url: { url: 'data:image/png;base64,RAWBYTES' } },],},@@ -12497,7 +12497,7 @@ test('Share-for-research item scrubs images and clamps oversized content', () =>assert.equal(JSON.stringify(item).includes('RAWBYTES'), false, `${label}: image bytes escaped the scrub`);assert.equal(item.request[0].image_url, undefined, `${label}: top-level image_url key survived`);assert.equal(item.request[1].content[0].type, 'text', `${label}: text block dropped with the image`);- assert.match(item.request[1].content[0].text, /\[… 2000 characters omitted\]/, `${label}: long text block not clamped`);+ assert.match(item.request[1].content[0].text, /\[… \d+ characters omitted\]/, `${label}: long text block not clamped`);assert.equal(item.request[2].content, 'Short reply');assert.deepEqual(item.response, { role: 'assistant', content: 'Summary.' });} finally {@@ -12515,7 +12515,7 @@ test('Share-for-research item drops empty runs and caps the whole request', () =}), null, 'blank response must not be shared');const item = SHARE_OUTBOX_CH.buildShareGenerationItem({runId: 'r', finalContent: 'x',- messages: Array.from({ length: 60 }, (_, i) => ({ role: 'user', content: `tail${i}-` + 'y'.repeat(9_000) })),+ messages: Array.from({ length: 60 }, (_, i) => ({ role: 'user', content: `tail${i}-` + 'lorem ipsum dolor sit amet '.repeat(360) })),model: 'm', mode: 'act', provider: 'p', provider_name: 'p',});const total = JSON.stringify(item.request).length;@@ -12600,9 +12600,39 @@ test('Share-for-research scrub removes embedded data URIs and keeps repeated-ans});assert.equal(repeat.request.length, 3, `${label}: pre-response history was corrupted by terminal-answer stripping`);assert.equal(repeat.request[1].content, 'hello', `${label}: repeated-answer history lost`);+ // Bare base64 without a data: wrapper (e.g. serialized {"base64":"..."}+ // tool results) must be scrubbed from request strings too.+ const bareBlob = 'ABCD1234abcd'.repeat(40);+ const bare = outbox.buildShareGenerationItem({+ runId: `run-share-bare-${label}`,+ finalContent: 'ok',+ messages: [{ role: 'tool', content: `file bytes {"base64":"${bareBlob}"} end` }],+ model: 'some-model', mode: 'act', provider: 'anthropic', provider_name: 'Anthropic Claude',+ });+ assert.equal(JSON.stringify(bare).includes(bareBlob.slice(0, 60)), false, `${label}: bare base64 tool bytes escaped the scrub`);+ // Responses get the same binary scrub as requests, not just clamping.+ const answered = outbox.buildShareGenerationItem({+ runId: `run-share-resp-${label}`,+ finalContent: `here it is: data:image/png;base64,${'iVBORw0K'.repeat(60)}`,+ messages: [{ role: 'user', content: 'reproduce the canvas' }],+ model: 'some-model', mode: 'act', provider: 'anthropic', provider_name: 'Anthropic Claude',+ });+ assert.equal(JSON.stringify(answered).includes('iVBORw0K'), false, `${label}: response image bytes escaped the scrub`);}});+test('Share-for-research caps count wrapper messages and serialized overhead', () => {+ const messages = [{ role: 'system', content: 'SYS' }];+ for (let i = 0; i < 250; i++) messages.push({ role: 'user', content: `cap${i}-` + 'word '.repeat(150) });+ const item = SHARE_OUTBOX_CH.buildShareGenerationItem({+ runId: 'cap-run', finalContent: 'x', messages, model: 'm', mode: 'act', provider: 'p', provider_name: 'p',+ });+ assert.ok(item.request.length <= 200, `message cap broken with wrappers (${item.request.length})`);+ assert.ok(JSON.stringify(item.request).length <= 150_000, `byte budget broken with wrappers (${JSON.stringify(item.request).length})`);+ assert.equal(item.request[0].content, 'SYS', 'system prompt lost to cap accounting');+ assert.match(item.request.at(-1).content, /^cap249-/, 'tail lost to cap accounting');+});+test('Share-for-research outbox persists retryable failures and removes acknowledged or rejected entries', async () => {const originalChrome = globalThis.chrome;const storage = {};@@ -12732,6 +12762,7 @@ test('Share-for-research delivery stays opt-in and mirrored across both builds',assert.match(chromeOutbox, /input_file/, `${browser}: binary scrub must cover file/input_file blocks`);assert.match(chromeOutbox, /embedded base64 data omitted/, `${browser}: string content must be scrubbed of data URIs`);assert.match(chromeOutbox, /earlier shared messages omitted/, `${browser}: truncation must preserve the tail`);+ assert.match(chromeOutbox, /scrubText\(responseContent/, `${browser}: shared responses must get the binary scrub`);assert.match(settings, /shareQueriesForResearch/, `${browser}: share toggle field missing from settings`);assert.match(settings, /!input\.checked[\s\S]*?confirm\(/, `${browser}: consent confirmation must guard turning the share toggle on`);assert.match(provider, /\/improvement\/generations/, `${browser}: share endpoint missing from the Compass provider transport`);