Skip to content

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

fix(providers): address codex round 3 - exact share request, data-uri scrub, trailing-only strip

Emre Sokullu3d84ac31a2a7
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 9d91ccd73..3d8dd5797 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -21078,6 +21078,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
tools = null,
toolChoice = null,
returnResult = false,
+ shareCapture = null,
} = {}) {
let modelMessages = this._messagesForSourceGroundedRun(
messages,
@@ -21106,6 +21107,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
...modelMessages.slice(modelMessages[0]?.role === 'system' ? 1 : 0),
];
const prunedMessages = this._pruneOldImages(contextMessages, provider);
+ // Voluntary research sharing needs the exact model-facing request (with
+ // the context-only system prompt). Callers pass a holder when the result
+ // may be shared; recovery phases omit it and are never shared.
+ if (shareCapture) shareCapture.request = prunedMessages;
const chatOpts = {
temperature: ['delivery_recovery', 'step_limit_recovery'].includes(phase) ? 0.2 : 0.3,
maxTokens: this._providerMaxOutputTokens(provider),
@@ -21187,6 +21192,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
runOptions = {},
currentUserMessage = null,
priorMessageSet = null,
+ shareCapture = null,
) {
const alreadyStopped = this._consumeContextOnlyAbort(tabId, messages, onUpdate);
if (alreadyStopped) return alreadyStopped;
@@ -21196,7 +21202,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
try {
finalResponse = await this._generateContextOnlyResponse(
tabId, messages, provider, costState, runId,
- { phase: 'response_only', step: 1, runOptions, currentUserMessage, priorMessageSet },
+ { phase: 'response_only', step: 1, runOptions, currentUserMessage, priorMessageSet, shareCapture },
);
} catch (error) {
status = this._isCostAllowanceError(error) ? 'cost_limit' : 'error';
@@ -40507,6 +40513,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// 'done' but never hit the provider. Flipped to true once past those
// guards (or on a successful response-only turn).
let shareHadProviderCompletion = false;
+ // Exact pruned request of the last provider call in this run (set at each
+ // chatMainTurn invocation and for response-only turns). The persisted
+ // `messages` array is mutated after each call (appended tool calls and
+ // tool results), so reconstructing at finalization would leak
+ // response-side data into the shared request.
+ let currentNonStreamRequestMessages = null;
let traceFailureCode = null;
let traceTurnEndExtra = {}; // step-limit handoff outcome; trace status keeps max_steps
let lastTraceStep = 0; // step counter for turn_end, readable outside the loop
@@ -40631,15 +40643,22 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
trustedContinuation: runOptions?.trustedContinuation === true,
});
if (gateOutcome.responseOnly === true) {
+ const responseOnlyShareCapture = {};
const responseOnly = await this._completeResponseOnlyTurn(
tabId, messages, onUpdate, provider, costState, runId,
- runOptions, enriched, sourceBoundPriorMessages,
+ runOptions, enriched, sourceBoundPriorMessages, responseOnlyShareCapture,
);
finalResponse = responseOnly.content;
_traceStatus = responseOnly.status;
// Response-only turns do call the model (source-grounded + pruned
- // inside _generateContextOnlyResponse). Only mark shareable on success.
- if (responseOnly.status === 'done') shareHadProviderCompletion = true;
+ // inside _generateContextOnlyResponse). Only mark shareable on success,
+ // keeping the exact request (with the context-only system prompt).
+ if (responseOnly.status === 'done') {
+ shareHadProviderCompletion = true;
+ if (Array.isArray(responseOnlyShareCapture.request) && responseOnlyShareCapture.request.length) {
+ currentNonStreamRequestMessages = responseOnlyShareCapture.request;
+ }
+ }
return finalResponse;
}
if (this._consumeSelectionGroundingRestoration(tabId, enriched)) this._persist(tabId);
@@ -40811,6 +40830,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const startedAt = Date.now();
let result;
try {
+ // Retain the exact request for voluntary research sharing (see
+ // currentNonStreamRequestMessages). chatMessages is already the
+ // source-grounded, pruned model input for this call.
+ currentNonStreamRequestMessages = chatMessages;
result = await chatMainTurnRaw(chatMessages, chatOptions, requestContext);
} catch (error) {
if (error?.webbrainOutputEmitted === true) throw error;
@@ -40820,6 +40843,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
code: 'vision_local_fallback_retry',
message: 'The active provider rejected the image; retrying once from the retained capture using a local LiquidAI description.',
});
+ currentNonStreamRequestMessages = fallbackMessages;
result = await chatMainTurnRaw(fallbackMessages, chatOptions, requestContext);
}
messageCompletion = aggregateMessageCompletion(
@@ -41603,13 +41627,22 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
lastTraceStep,
this._traceTurnEndPayload(_traceStatus, traceFailureCode, traceTurnEndExtra),
);
- // Share the actual model-facing request (source-grounded filtered +
- // pruned), not the full persisted history which can contain unrelated
- // pages/attachments the provider never received.
+ // Share the exact model-facing request captured at the last provider
+ // call. Unlike a reconstruction from the now-mutated `messages` array,
+ // it contains neither the terminal tool call/result appended after the
+ // call nor unrelated history, and response-only turns keep their
+ // context-only system prompt. Falls back to a filtered reconstruction
+ // only when no call was captured (should not happen for shared runs).
let shareRequest = null;
try {
- const rawShare = typeof modelMessagesForRun === 'function' ? modelMessagesForRun() : messages;
- shareRequest = this._pruneOldImages(rawShare, provider);
+ if (Array.isArray(currentNonStreamRequestMessages) && currentNonStreamRequestMessages.length) {
+ shareRequest = currentNonStreamRequestMessages;
+ } else if (typeof modelMessagesForRun === 'function') {
+ const rawShare = modelMessagesForRun();
+ shareRequest = this._pruneOldImages(rawShare, provider);
+ } else {
+ shareRequest = messages;
+ }
} catch {}
await this._endTraceRun(tabId, runId, _traceStatus, finalResponse, { provider, messages, mode, shareRequest, hadProviderCompletion: shareHadProviderCompletion });
}
@@ -41825,6 +41858,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
let _traceStatus = 'done';
// See processMessage: only provider-backed completions may be shared.
let shareHadProviderCompletion = false;
+ // Exact pruned request of the last streaming provider call (set before
+ // each chatStream invocation; also used for response-only turns, which
+ // return before the loop). Declared here so pre-loop exits can capture.
+ let currentStreamRequestMessages = null;
let traceFailureCode = null;
let traceTurnEndExtra = {}; // step-limit handoff outcome; trace status keeps max_steps
const finish = (response, status = _traceStatus) => {
@@ -41888,11 +41925,17 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
trustedContinuation: runOptions?.trustedContinuation === true,
});
if (gateOutcome.responseOnly === true) {
+ const responseOnlyShareCapture = {};
const responseOnly = await this._completeResponseOnlyTurn(
tabId, messages, onUpdate, provider, costState, runId,
- runOptions, enriched, sourceBoundPriorMessages,
+ runOptions, enriched, sourceBoundPriorMessages, responseOnlyShareCapture,
);
- if (responseOnly.status === 'done') shareHadProviderCompletion = true;
+ if (responseOnly.status === 'done') {
+ shareHadProviderCompletion = true;
+ if (Array.isArray(responseOnlyShareCapture.request) && responseOnlyShareCapture.request.length) {
+ currentStreamRequestMessages = responseOnlyShareCapture.request;
+ }
+ }
return finish(responseOnly.content, responseOnly.status);
}
if (this._consumeSelectionGroundingRestoration(tabId, enriched)) this._persist(tabId);
@@ -41957,7 +42000,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
let pendingVisionFallbackMessages = null;
let visionFallbackAttempted = false;
let streamEmittedOutput = false;
- let currentStreamRequestMessages = null;
+ // currentStreamRequestMessages is declared at the top of this function so
+ // pre-loop exits (e.g. response-only turns) can capture their request.
const recommendedFirstTool = await this._maybeExecuteRecommendedActionFirstTool(
tabId, runOptions, messages, onUpdate, provider, allowedToolNames, toolSchemas,
diff --git a/src/chrome/src/trace/webbrain-share-outbox.js b/src/chrome/src/trace/webbrain-share-outbox.js
index eb6b23f2c..5317ef408 100644
--- a/src/chrome/src/trace/webbrain-share-outbox.js
+++ b/src/chrome/src/trace/webbrain-share-outbox.js
@@ -27,11 +27,31 @@ function clampText(value, limit = MAX_MESSAGE_CHARS) {
return bounded(value, limit);
}
+// Remove embedded binary payloads from free text before clamping. Tool
+// results are plain strings, so a canvas toDataURL(), screenshot bytes, or
+// other base64 blob echoed into `message.content` would otherwise leave the
+// browser unchanged (containsBinaryBlock never sees string content).
+function scrubText(value, limit = MAX_MESSAGE_CHARS) {
+ if (typeof value !== 'string') return value;
+ let text = value;
+ if (text.length > 500 && text.includes('data:')) {
+ // Newlines (but not spaces/words) are allowed inside the payload so
+ // wrapped base64 is still removed while surrounding prose survives.
+ text = text.replace(
+ /data:(image|audio|video|application|font|model)\/[a-zA-Z0-9+.-]+;base64,[A-Za-z0-9+/=\r\n]{200,}/g,
+ '[embedded base64 data omitted]',
+ );
+ }
+ return bounded(text, limit);
+}
+
function containsBinaryBlock(value) {
- // Bare data-URI strings can appear as array items (e.g. screenshot bytes
- // echoed into a tool result). Catch them before the object checks below.
+ // Bare data-URI strings can appear as array items or nested values (e.g.
+ // screenshot bytes echoed into a tool result). Check anywhere in the
+ // string, not just at the start, since payloads may be embedded in text.
if (typeof value === 'string') {
- if (value.length > 1000 && value.slice(0, 200).match(/^data:(image|audio|video|application)\/[^;]+;base64,/i)) return true;
+ if (value.length > 1000 && value.includes('data:')
+ && /data:(image|audio|video|application)\/[^;]+;base64,/i.test(value)) return true;
return false;
}
if (!value || typeof value !== 'object') return false;
@@ -54,25 +74,22 @@ function containsBinaryBlock(value) {
}
function requestMessages(messages, responseContent) {
- if (!Array.isArray(messages)) return messages;
+ if (!Array.isArray(messages) || !messages.length) return messages;
const want = String(responseContent ?? '').trim();
if (!want) return [...messages];
+ // Only ever strip a trailing terminal answer. Pre-response snapshots (e.g.
+ // the streaming path's captured request) contain no terminal message, and a
+ // backward scan could otherwise delete earlier history that happens to
+ // equal the response (asking the model to repeat a prior answer).
const request = [...messages];
- for (let index = request.length - 1; index >= 0; index--) {
- const message = request[index];
- if (message?.role !== 'assistant') continue;
- // Normal path: the agent appends the final string answer before _endTraceRun.
- if (typeof message.content === 'string' && message.content.trim() === want) {
- request.splice(index, 1);
- break;
- }
- // Multimodal path: single text block echoing the final answer.
- if (Array.isArray(message.content) && message.content.length === 1
- && message.content[0]?.type === 'text'
- && String(message.content[0]?.text ?? '').trim() === want) {
- request.splice(index, 1);
- break;
- }
+ const last = request[request.length - 1];
+ if (last?.role !== 'assistant') return request;
+ if (typeof last.content === 'string' && last.content.trim() === want) {
+ request.pop();
+ } else if (Array.isArray(last.content) && last.content.length === 1
+ && last.content[0]?.type === 'text'
+ && String(last.content[0]?.text ?? '').trim() === want) {
+ request.pop();
}
return request;
}
@@ -84,15 +101,21 @@ function scrubMessage(message) {
if (Array.isArray(message.content)) {
const items = [];
for (const item of message.content) {
+ if (typeof item === 'string') {
+ // Primitive string parts never reach containsBinaryBlock: scrub any
+ // embedded data URIs here before clamping.
+ items.push(scrubText(item));
+ continue;
+ }
if (!item || typeof item !== 'object') {
items.push(item);
continue;
}
// Drop images entirely so raw screenshot/data-URI bytes never leave the
- // browser; text blocks are clamped individually.
+ // browser; text blocks are scrubbed of embedded payloads, then clamped.
if (containsBinaryBlock(item)) continue;
if (item.type === 'text' && typeof item.text === 'string') {
- items.push({ ...item, text: clampText(item.text) });
+ items.push({ ...item, text: scrubText(item.text) });
continue;
}
items.push(item);
@@ -101,7 +124,7 @@ function scrubMessage(message) {
copy.content = items;
} else if (typeof message.content === 'string') {
if (!message.content.length) return null;
- copy.content = clampText(message.content);
+ copy.content = scrubText(message.content);
}
return copy;
}
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index e65148c14..2e19a9d55 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -18822,6 +18822,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
tools = null,
toolChoice = null,
returnResult = false,
+ shareCapture = null,
} = {}) {
let modelMessages = this._messagesForSourceGroundedRun(
messages,
@@ -18850,6 +18851,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
...modelMessages.slice(modelMessages[0]?.role === 'system' ? 1 : 0),
];
const prunedMessages = this._pruneOldImages(contextMessages, provider);
+ // Voluntary research sharing needs the exact model-facing request (with
+ // the context-only system prompt). Callers pass a holder when the result
+ // may be shared; recovery phases omit it and are never shared.
+ if (shareCapture) shareCapture.request = prunedMessages;
const chatOpts = {
temperature: ['delivery_recovery', 'step_limit_recovery'].includes(phase) ? 0.2 : 0.3,
maxTokens: this._providerMaxOutputTokens(provider),
@@ -18931,6 +18936,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
runOptions = {},
currentUserMessage = null,
priorMessageSet = null,
+ shareCapture = null,
) {
const alreadyStopped = this._consumeContextOnlyAbort(tabId, messages, onUpdate);
if (alreadyStopped) return alreadyStopped;
@@ -18940,7 +18946,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
try {
finalResponse = await this._generateContextOnlyResponse(
tabId, messages, provider, costState, runId,
- { phase: 'response_only', step: 1, runOptions, currentUserMessage, priorMessageSet },
+ { phase: 'response_only', step: 1, runOptions, currentUserMessage, priorMessageSet, shareCapture },
);
} catch (error) {
status = this._isCostAllowanceError(error) ? 'cost_limit' : 'error';
@@ -33653,6 +33659,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// 'done' but never hit the provider. Flipped to true once past those
// guards (or on a successful response-only turn).
let shareHadProviderCompletion = false;
+ // Exact pruned request of the last provider call in this run (set at each
+ // chatMainTurn invocation and for response-only turns). The persisted
+ // `messages` array is mutated after each call (appended tool calls and
+ // tool results), so reconstructing at finalization would leak
+ // response-side data into the shared request.
+ let currentNonStreamRequestMessages = null;
let traceFailureCode = null;
let traceTurnEndExtra = {}; // step-limit handoff outcome; trace status keeps max_steps
let lastTraceStep = 0; // step counter for turn_end, readable outside the loop
@@ -33769,15 +33781,22 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
trustedContinuation: runOptions?.trustedContinuation === true,
});
if (gateOutcome.responseOnly === true) {
+ const responseOnlyShareCapture = {};
const responseOnly = await this._completeResponseOnlyTurn(
tabId, messages, onUpdate, provider, costState, runId,
- runOptions, enriched, sourceBoundPriorMessages,
+ runOptions, enriched, sourceBoundPriorMessages, responseOnlyShareCapture,
);
finalResponse = responseOnly.content;
_traceStatus = responseOnly.status;
// Response-only turns do call the model (source-grounded + pruned
- // inside _generateContextOnlyResponse). Only mark shareable on success.
- if (responseOnly.status === 'done') shareHadProviderCompletion = true;
+ // inside _generateContextOnlyResponse). Only mark shareable on success,
+ // keeping the exact request (with the context-only system prompt).
+ if (responseOnly.status === 'done') {
+ shareHadProviderCompletion = true;
+ if (Array.isArray(responseOnlyShareCapture.request) && responseOnlyShareCapture.request.length) {
+ currentNonStreamRequestMessages = responseOnlyShareCapture.request;
+ }
+ }
return finalResponse;
}
if (this._consumeSelectionGroundingRestoration(tabId, enriched)) this._persist(tabId);
@@ -33945,6 +33964,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const startedAt = Date.now();
let result;
try {
+ // Retain the exact request for voluntary research sharing (see
+ // currentNonStreamRequestMessages). chatMessages is already the
+ // source-grounded, pruned model input for this call.
+ currentNonStreamRequestMessages = chatMessages;
result = await chatMainTurnRaw(chatMessages, chatOptions, requestContext);
} catch (error) {
if (error?.webbrainOutputEmitted === true) throw error;
@@ -33954,6 +33977,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
code: 'vision_local_fallback_retry',
message: 'The active provider rejected the image; retrying once from the retained capture using a local LiquidAI description.',
});
+ currentNonStreamRequestMessages = fallbackMessages;
result = await chatMainTurnRaw(fallbackMessages, chatOptions, requestContext);
}
messageCompletion = aggregateMessageCompletion(
@@ -34623,13 +34647,22 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
lastTraceStep,
this._traceTurnEndPayload(_traceStatus, traceFailureCode, traceTurnEndExtra),
);
- // Share the actual model-facing request (source-grounded filtered +
- // pruned), not the full persisted history which can contain unrelated
- // pages/attachments the provider never received.
+ // Share the exact model-facing request captured at the last provider
+ // call. Unlike a reconstruction from the now-mutated `messages` array,
+ // it contains neither the terminal tool call/result appended after the
+ // call nor unrelated history, and response-only turns keep their
+ // context-only system prompt. Falls back to a filtered reconstruction
+ // only when no call was captured (should not happen for shared runs).
let shareRequest = null;
try {
- const rawShare = typeof modelMessagesForRun === 'function' ? modelMessagesForRun() : messages;
- shareRequest = this._pruneOldImages(rawShare, provider);
+ if (Array.isArray(currentNonStreamRequestMessages) && currentNonStreamRequestMessages.length) {
+ shareRequest = currentNonStreamRequestMessages;
+ } else if (typeof modelMessagesForRun === 'function') {
+ const rawShare = modelMessagesForRun();
+ shareRequest = this._pruneOldImages(rawShare, provider);
+ } else {
+ shareRequest = messages;
+ }
} catch {}
await this._endTraceRun(tabId, runId, _traceStatus, finalResponse, { provider, messages, mode, shareRequest, hadProviderCompletion: shareHadProviderCompletion });
}
@@ -34808,6 +34841,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
let _traceStatus = 'done';
// See processMessage: only provider-backed completions may be shared.
let shareHadProviderCompletion = false;
+ // Exact pruned request of the last streaming provider call (set before
+ // each chatStream invocation; also used for response-only turns, which
+ // return before the loop). Declared here so pre-loop exits can capture.
+ let currentStreamRequestMessages = null;
let traceFailureCode = null;
let traceTurnEndExtra = {}; // step-limit handoff outcome; trace status keeps max_steps
const finish = (response, status = _traceStatus) => {
@@ -34865,11 +34902,17 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
trustedContinuation: runOptions?.trustedContinuation === true,
});
if (gateOutcome.responseOnly === true) {
+ const responseOnlyShareCapture = {};
const responseOnly = await this._completeResponseOnlyTurn(
tabId, messages, onUpdate, provider, costState, runId,
- runOptions, enriched, sourceBoundPriorMessages,
+ runOptions, enriched, sourceBoundPriorMessages, responseOnlyShareCapture,
);
- if (responseOnly.status === 'done') shareHadProviderCompletion = true;
+ if (responseOnly.status === 'done') {
+ shareHadProviderCompletion = true;
+ if (Array.isArray(responseOnlyShareCapture.request) && responseOnlyShareCapture.request.length) {
+ currentStreamRequestMessages = responseOnlyShareCapture.request;
+ }
+ }
return finish(responseOnly.content, responseOnly.status);
}
if (this._consumeSelectionGroundingRestoration(tabId, enriched)) this._persist(tabId);
@@ -34921,7 +34964,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
let pendingVisionFallbackMessages = null;
let visionFallbackAttempted = false;
let streamEmittedOutput = false;
- let currentStreamRequestMessages = null;
+ // currentStreamRequestMessages is declared at the top of this function so
+ // pre-loop exits (e.g. response-only turns) can capture their request.
const recommendedFirstTool = await this._maybeExecuteRecommendedActionFirstTool(
tabId, runOptions, messages, onUpdate, provider, allowedToolNames, toolSchemas,
diff --git a/src/firefox/src/trace/webbrain-share-outbox.js b/src/firefox/src/trace/webbrain-share-outbox.js
index eb6b23f2c..5317ef408 100644
--- a/src/firefox/src/trace/webbrain-share-outbox.js
+++ b/src/firefox/src/trace/webbrain-share-outbox.js
@@ -27,11 +27,31 @@ function clampText(value, limit = MAX_MESSAGE_CHARS) {
return bounded(value, limit);
}
+// Remove embedded binary payloads from free text before clamping. Tool
+// results are plain strings, so a canvas toDataURL(), screenshot bytes, or
+// other base64 blob echoed into `message.content` would otherwise leave the
+// browser unchanged (containsBinaryBlock never sees string content).
+function scrubText(value, limit = MAX_MESSAGE_CHARS) {
+ if (typeof value !== 'string') return value;
+ let text = value;
+ if (text.length > 500 && text.includes('data:')) {
+ // Newlines (but not spaces/words) are allowed inside the payload so
+ // wrapped base64 is still removed while surrounding prose survives.
+ text = text.replace(
+ /data:(image|audio|video|application|font|model)\/[a-zA-Z0-9+.-]+;base64,[A-Za-z0-9+/=\r\n]{200,}/g,
+ '[embedded base64 data omitted]',
+ );
+ }
+ return bounded(text, limit);
+}
+
function containsBinaryBlock(value) {
- // Bare data-URI strings can appear as array items (e.g. screenshot bytes
- // echoed into a tool result). Catch them before the object checks below.
+ // Bare data-URI strings can appear as array items or nested values (e.g.
+ // screenshot bytes echoed into a tool result). Check anywhere in the
+ // string, not just at the start, since payloads may be embedded in text.
if (typeof value === 'string') {
- if (value.length > 1000 && value.slice(0, 200).match(/^data:(image|audio|video|application)\/[^;]+;base64,/i)) return true;
+ if (value.length > 1000 && value.includes('data:')
+ && /data:(image|audio|video|application)\/[^;]+;base64,/i.test(value)) return true;
return false;
}
if (!value || typeof value !== 'object') return false;
@@ -54,25 +74,22 @@ function containsBinaryBlock(value) {
}
function requestMessages(messages, responseContent) {
- if (!Array.isArray(messages)) return messages;
+ if (!Array.isArray(messages) || !messages.length) return messages;
const want = String(responseContent ?? '').trim();
if (!want) return [...messages];
+ // Only ever strip a trailing terminal answer. Pre-response snapshots (e.g.
+ // the streaming path's captured request) contain no terminal message, and a
+ // backward scan could otherwise delete earlier history that happens to
+ // equal the response (asking the model to repeat a prior answer).
const request = [...messages];
- for (let index = request.length - 1; index >= 0; index--) {
- const message = request[index];
- if (message?.role !== 'assistant') continue;
- // Normal path: the agent appends the final string answer before _endTraceRun.
- if (typeof message.content === 'string' && message.content.trim() === want) {
- request.splice(index, 1);
- break;
- }
- // Multimodal path: single text block echoing the final answer.
- if (Array.isArray(message.content) && message.content.length === 1
- && message.content[0]?.type === 'text'
- && String(message.content[0]?.text ?? '').trim() === want) {
- request.splice(index, 1);
- break;
- }
+ const last = request[request.length - 1];
+ if (last?.role !== 'assistant') return request;
+ if (typeof last.content === 'string' && last.content.trim() === want) {
+ request.pop();
+ } else if (Array.isArray(last.content) && last.content.length === 1
+ && last.content[0]?.type === 'text'
+ && String(last.content[0]?.text ?? '').trim() === want) {
+ request.pop();
}
return request;
}
@@ -84,15 +101,21 @@ function scrubMessage(message) {
if (Array.isArray(message.content)) {
const items = [];
for (const item of message.content) {
+ if (typeof item === 'string') {
+ // Primitive string parts never reach containsBinaryBlock: scrub any
+ // embedded data URIs here before clamping.
+ items.push(scrubText(item));
+ continue;
+ }
if (!item || typeof item !== 'object') {
items.push(item);
continue;
}
// Drop images entirely so raw screenshot/data-URI bytes never leave the
- // browser; text blocks are clamped individually.
+ // browser; text blocks are scrubbed of embedded payloads, then clamped.
if (containsBinaryBlock(item)) continue;
if (item.type === 'text' && typeof item.text === 'string') {
- items.push({ ...item, text: clampText(item.text) });
+ items.push({ ...item, text: scrubText(item.text) });
continue;
}
items.push(item);
@@ -101,7 +124,7 @@ function scrubMessage(message) {
copy.content = items;
} else if (typeof message.content === 'string') {
if (!message.content.length) return null;
- copy.content = clampText(message.content);
+ copy.content = scrubText(message.content);
}
return copy;
}
diff --git a/test/run.js b/test/run.js
index 51aa735a2..585ba3d29 100644
--- a/test/run.js
+++ b/test/run.js
@@ -12553,6 +12553,44 @@ test('Share-for-research item excludes terminal answers and binary document bloc
}
});
+test('Share-for-research scrub removes embedded data URIs and keeps repeated-answer history', () => {
+ for (const [label, outbox] of [['chrome', SHARE_OUTBOX_CH], ['firefox', SHARE_OUTBOX_FX]]) {
+ const bigImage = `canvas snapshot data:image/png;base64,${'iVBORw0KGgoAAAANSUhEUg'.repeat(40)} trailing note`;
+ const item = outbox.buildShareGenerationItem({
+ runId: `run-share-datauri-${label}`,
+ finalContent: 'done here',
+ messages: [
+ { role: 'tool', content: bigImage },
+ {
+ role: 'user',
+ content: [
+ 'look at this',
+ `embedded shot data:image/jpeg;base64,${'ABCD1234abcd'.repeat(40)} end`,
+ ],
+ },
+ ],
+ model: 'some-model', mode: 'act', provider: 'anthropic', provider_name: 'Anthropic Claude',
+ });
+ const serialized = JSON.stringify(item);
+ assert.equal(serialized.includes('iVBORw0KGgoAAAANSUhEUg'), false, `${label}: tool-result data URI escaped the scrub`);
+ assert.equal(serialized.includes('ABCD1234abcd'), false, `${label}: array string data URI escaped the scrub`);
+ assert.match(serialized, /embedded base64 data omitted/, `${label}: data-URI placeholder missing`);
+ // Pre-response snapshots must not lose earlier history that repeats the answer.
+ const repeat = outbox.buildShareGenerationItem({
+ runId: `run-share-repeat-${label}`,
+ finalContent: 'hello',
+ messages: [
+ { role: 'user', content: 'Say hello' },
+ { role: 'assistant', content: 'hello' },
+ { role: 'user', content: 'Repeat what you said' },
+ ],
+ model: 'some-model', mode: 'act', provider: 'anthropic', provider_name: 'Anthropic Claude',
+ });
+ 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`);
+ }
+});
+
test('Share-for-research outbox persists retryable failures and removes acknowledged or rejected entries', async () => {
const originalChrome = globalThis.chrome;
const storage = {};
@@ -12671,12 +12709,16 @@ test('Share-for-research delivery stays opt-in and mirrored across both builds',
const provider = fs.readFileSync(path.join(ROOT, `src/${browser}/src/providers/openai.js`), 'utf8');
assert.match(agent, /status === 'done'[\s\S]*hadProviderCompletion === true[\s\S]*shareQueriesForResearch === true[\s\S]*enqueueShareGeneration/, `${browser}: capture must require a provider completion and the per-provider toggle`);
assert.match(agent, /shareRequest/, `${browser}: capture must prefer the model-facing source-grounded request`);
+ assert.match(agent, /currentNonStreamRequestMessages/, `${browser}: non-streaming turns must retain the exact pruned request`);
+ assert.match(agent, /currentStreamRequestMessages/, `${browser}: streaming turns must retain the exact pruned request`);
+ assert.match(agent, /shareCapture/, `${browser}: response-only turns must keep their context-only request`);
assert.match(agent, /shareHadProviderCompletion/, `${browser}: local-only fast paths must not be shareable`);
assert.match(agent, /'webbrain-cloud'/, `${browser}: Compass provider must not route through the share path`);
assert.match(agent, /void flushShareOutbox\(shareTransport\)/, `${browser}: run-end share flush missing`);
assert.match(agent, /_shareSessionId\(/, `${browser}: share session id sanitizer missing`);
assert.match(chromeOutbox, /client_share_id/, `${browser}: outbox flush must send an idempotency key`);
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(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`);