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 9 - share raw provider completion, not appended notices
Emre Sokullu92527ea6ed8e
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 3bcdd1cf5..3bd531a31 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -18721,7 +18721,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d* Compass delivery remains active when optional local tracing is disabled.* Shared by the streaming and non-streaming message paths. (#9)*/- async _endTraceRun(tabId, runId, status, finalContent, { provider = null, messages = null, mode = '', shareRequest = null, hadProviderCompletion = false } = {}) {+ async _endTraceRun(tabId, runId, status, finalContent, { provider = null, messages = null, mode = '', shareRequest = null, shareResponse = null, hadProviderCompletion = false } = {}) {if (String(provider?.config?.providerName || '').toLowerCase() === 'webbrain-cloud') {try {const sessionId = this.conversationIds.get(tabId) || null;@@ -18769,6 +18769,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst shareItem = buildShareGenerationItem({runId,finalContent,+ sharedResponse: shareResponse,messages: Array.isArray(shareRequest) && shareRequest.length ? shareRequest : messages,model: provider?.model,mode,@@ -40545,6 +40546,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// tool results), so reconstructing at finalization would leak// response-side data into the shared request.let currentNonStreamRequestMessages = null;+ // Raw provider completion before local notices are appended (cost,+ // attribution, caveats). Shared as the generation's response so+ // application-authored text is never mislabeled as provider output.+ let shareRawResponse = null;let traceFailureCode = null;let traceTurnEndExtra = {}; // step-limit handoff outcome; trace status keeps max_stepslet lastTraceStep = 0; // step counter for turn_end, readable outside the loop@@ -40926,6 +40931,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dshareHadProviderCompletion = true;while (steps < this.maxSteps) {+ // Each turn's raw provider text belongs to that turn only: reset so a+ // non-terminal final-text pass can never pair stale output with a later+ // tool-batch result in the shared record.+ shareRawResponse = null;// Check for abort before each stepif (this._checkAbort(tabId)) {finalResponse = finalResponse || '[Stopped by user]';@@ -41568,6 +41577,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dbreak;}const repairedFinalContent = repairAssistantDisplayText(result.content);+ // Retain the provider-authored completion before appending local+ // notices: voluntary research sharing must label the model provider's+ // response, not our cost/attribution/caveat text, as the generation.+ shareRawResponse = repairedFinalContent;finalResponse = result.costAllowanceMessage? `${repairedFinalContent}\n\n${result.costAllowanceMessage}`: repairedFinalContent;@@ -41670,14 +41683,13 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dshareRequest = messages;}} catch {}- await this._endTraceRun(tabId, runId, _traceStatus, finalResponse, { provider, messages, mode, shareRequest, hadProviderCompletion: shareHadProviderCompletion });+ await this._endTraceRun(tabId, runId, _traceStatus, finalResponse, { provider, messages, mode, shareRequest, shareResponse: shareRawResponse, hadProviderCompletion: shareHadProviderCompletion });}}/*** Process a message with streaming output.- */- async processMessageStream(tabId, userMessage, onUpdate = () => {}, mode = 'ask', runOptions = {}) {+ */ async processMessageStream(tabId, userMessage, onUpdate = () => {}, mode = 'ask', runOptions = {}) {await this._claimRunEntry(tabId, 'interactive', runOptions);try {let continuationEligible = false;@@ -41890,6 +41902,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// 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;+ // Raw provider completion before local notices are appended (see the+ // non-streaming path): shared as the generation's response.+ let shareRawResponse = null;let traceFailureCode = null;let traceTurnEndExtra = {}; // step-limit handoff outcome; trace status keeps max_stepsconst finish = (response, status = _traceStatus) => {@@ -42055,6 +42070,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dshareHadProviderCompletion = true;while (steps < this.maxSteps) {+ // See the non-streaming loop: raw provider text must not survive into+ // a later turn's shared record.+ shareRawResponse = null;if (this._checkAbort(tabId)) {const content = '[Stopped by user]';messages.push(this._localCancellationMessage(content));@@ -42602,6 +42620,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// Replace the transient bubble once with the repaired terminal text.onUpdate('text', { content: fullText, replace: true });}+ // Retain the provider-authored completion before appending local+ // cost/attribution notices (see the non-streaming path).+ shareRawResponse = repairedFullText;if (costStopMessage) {onUpdate('text_delta', { content: `\n\n${costStopMessage}` });fullText = `${fullText}\n\n${costStopMessage}`;@@ -42738,7 +42759,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dshareRequest = this._pruneOldImages(modelMessagesForRun(), provider);}} catch {}- await this._endTraceRun(tabId, runId, _traceStatus, finalResponse, { provider, messages, mode, shareRequest, hadProviderCompletion: shareHadProviderCompletion });+ await this._endTraceRun(tabId, runId, _traceStatus, finalResponse, { provider, messages, mode, shareRequest, shareResponse: shareRawResponse, hadProviderCompletion: shareHadProviderCompletion });}}}diff --git a/src/chrome/src/trace/webbrain-share-outbox.js b/src/chrome/src/trace/webbrain-share-outbox.jsindex 5f27a37a7..5ac54c332 100644--- a/src/chrome/src/trace/webbrain-share-outbox.js+++ b/src/chrome/src/trace/webbrain-share-outbox.js@@ -279,6 +279,7 @@ function scrubMessages(messages) {export function buildShareGenerationItem({runId,finalContent,+ sharedResponse = null,messages,model,mode,@@ -292,6 +293,12 @@ export function buildShareGenerationItem({if (!request?.length) return null;const generatedId = globalThis.crypto?.randomUUID?.()|| `${Date.now().toString(36)}_${(++fallbackRunCounter).toString(36)}`;+ // The stored response is the raw provider completion when the caller+ // retained it; terminal stripping above always uses finalContent (the+ // displayed composite), which is what an appended terminal message holds.+ const storedResponse = sharedResponse != null && String(sharedResponse).trim()+ ? String(sharedResponse)+ : responseContent;return {id: String(runId || `share_${generatedId}`),// Stable provider-config id for consent checks (purge). provider/provider_name@@ -303,7 +310,7 @@ export function buildShareGenerationItem({model: String(model || '').slice(0, 255),mode: String(mode || '').slice(0, 32),request,- response: { role: 'assistant', content: scrubText(responseContent, MAX_RESPONSE_CHARS) },+ response: { role: 'assistant', content: scrubText(storedResponse, MAX_RESPONSE_CHARS) },};}diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex 68c5b761f..7d5ab0be2 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -16515,7 +16515,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d* Compass delivery remains active when optional local tracing is disabled.* Shared by the streaming and non-streaming message paths. (#9)*/- async _endTraceRun(tabId, runId, status, finalContent, { provider = null, messages = null, mode = '', shareRequest = null, hadProviderCompletion = false } = {}) {+ async _endTraceRun(tabId, runId, status, finalContent, { provider = null, messages = null, mode = '', shareRequest = null, shareResponse = null, hadProviderCompletion = false } = {}) {if (String(provider?.config?.providerName || '').toLowerCase() === 'webbrain-cloud') {try {const sessionId = this.conversationIds.get(tabId) || null;@@ -16564,6 +16564,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout drunId,finalContent,messages: Array.isArray(shareRequest) && shareRequest.length ? shareRequest : messages,+ sharedResponse: shareResponse,model: provider?.model,mode,provider: String(provider?.config?.providerName || '').toLowerCase(),@@ -33691,6 +33692,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// tool results), so reconstructing at finalization would leak// response-side data into the shared request.let currentNonStreamRequestMessages = null;+ // Raw provider completion before local notices are appended (cost,+ // attribution, caveats). Shared as the generation's response so+ // application-authored text is never mislabeled as provider output.+ let shareRawResponse = null;let traceFailureCode = null;let traceTurnEndExtra = {}; // step-limit handoff outcome; trace status keeps max_stepslet lastTraceStep = 0; // step counter for turn_end, readable outside the loop@@ -34053,6 +34058,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dshareHadProviderCompletion = true;while (steps < this.maxSteps) {+ // Each turn's raw provider text belongs to that turn only: reset so a+ // non-terminal final-text pass can never pair stale output with a later+ // tool-batch result in the shared record.+ shareRawResponse = null;if (this._checkAbort(tabId)) {finalResponse = finalResponse || '[Stopped by user]';_traceStatus = 'cancelled';@@ -34595,6 +34604,10 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dbreak;}const repairedFinalContent = repairAssistantDisplayText(result.content);+ // Retain the provider-authored completion before appending local+ // notices: voluntary research sharing must label the model provider's+ // response, not our cost/attribution/caveat text, as the generation.+ shareRawResponse = repairedFinalContent;finalResponse = result.costAllowanceMessage? `${repairedFinalContent}\n\n${result.costAllowanceMessage}`: repairedFinalContent;@@ -34690,7 +34703,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dshareRequest = messages;}} catch {}- await this._endTraceRun(tabId, runId, _traceStatus, finalResponse, { provider, messages, mode, shareRequest, hadProviderCompletion: shareHadProviderCompletion });+ await this._endTraceRun(tabId, runId, _traceStatus, finalResponse, { provider, messages, mode, shareRequest, shareResponse: shareRawResponse, hadProviderCompletion: shareHadProviderCompletion });}}@@ -34873,6 +34886,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// 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;+ // Raw provider completion before local notices are appended (see the+ // non-streaming path): shared as the generation's response.+ let shareRawResponse = null;let traceFailureCode = null;let traceTurnEndExtra = {}; // step-limit handoff outcome; trace status keeps max_stepsconst finish = (response, status = _traceStatus) => {@@ -35019,6 +35035,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dshareHadProviderCompletion = true;while (steps < this.maxSteps) {+ // See the non-streaming loop: raw provider text must not survive into+ // a later turn's shared record.+ shareRawResponse = null;if (this._checkAbort(tabId)) {const content = '[Stopped by user]';messages.push(this._localCancellationMessage(content));@@ -35487,6 +35506,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dreturn finish(planOnlyDecision.failure, planOnlyDecision.status || 'plan_only_output');}const repairedFullText = repairAssistantDisplayText(fullText);+ // Retain the provider-authored completion before appending local+ // cost/attribution notices (see the non-streaming path).+ shareRawResponse = repairedFullText;if (repairedFullText !== fullText) {fullText = repairedFullText;// Streaming deltas have already displayed the malformed escapes.@@ -35606,7 +35628,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dshareRequest = this._pruneOldImages(modelMessagesForRun(), provider);}} catch {}- await this._endTraceRun(tabId, runId, _traceStatus, finalResponse, { provider, messages, mode, shareRequest, hadProviderCompletion: shareHadProviderCompletion });+ await this._endTraceRun(tabId, runId, _traceStatus, finalResponse, { provider, messages, mode, shareRequest, shareResponse: shareRawResponse, hadProviderCompletion: shareHadProviderCompletion });}}}diff --git a/src/firefox/src/trace/webbrain-share-outbox.js b/src/firefox/src/trace/webbrain-share-outbox.jsindex 5f27a37a7..5ac54c332 100644--- a/src/firefox/src/trace/webbrain-share-outbox.js+++ b/src/firefox/src/trace/webbrain-share-outbox.js@@ -279,6 +279,7 @@ function scrubMessages(messages) {export function buildShareGenerationItem({runId,finalContent,+ sharedResponse = null,messages,model,mode,@@ -292,6 +293,12 @@ export function buildShareGenerationItem({if (!request?.length) return null;const generatedId = globalThis.crypto?.randomUUID?.()|| `${Date.now().toString(36)}_${(++fallbackRunCounter).toString(36)}`;+ // The stored response is the raw provider completion when the caller+ // retained it; terminal stripping above always uses finalContent (the+ // displayed composite), which is what an appended terminal message holds.+ const storedResponse = sharedResponse != null && String(sharedResponse).trim()+ ? String(sharedResponse)+ : responseContent;return {id: String(runId || `share_${generatedId}`),// Stable provider-config id for consent checks (purge). provider/provider_name@@ -303,7 +310,7 @@ export function buildShareGenerationItem({model: String(model || '').slice(0, 255),mode: String(mode || '').slice(0, 32),request,- response: { role: 'assistant', content: scrubText(responseContent, MAX_RESPONSE_CHARS) },+ response: { role: 'assistant', content: scrubText(storedResponse, MAX_RESPONSE_CHARS) },};}diff --git a/test/run.js b/test/run.jsindex c8f491ab3..c112d584b 100644--- a/test/run.js+++ b/test/run.js@@ -12618,6 +12618,19 @@ test('Share-for-research scrub removes embedded data URIs and keeps repeated-ansmodel: '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`);+ // The stored response is the raw provider completion: locally appended+ // notices are displayed but must not be mislabeled as provider output,+ // while the composite still strips the appended terminal message.+ const composite = 'Answer.\n\nSpend notice $0.01';+ const rawShared = outbox.buildShareGenerationItem({+ runId: `run-share-raw-${label}`,+ finalContent: composite,+ sharedResponse: 'Answer.',+ messages: [{ role: 'user', content: 'go' }, { role: 'assistant', content: composite }],+ model: 'some-model', mode: 'act', provider: 'anthropic', provider_name: 'Anthropic Claude',+ });+ assert.equal(rawShared.request.length, 1, `${label}: composite terminal answer not stripped`);+ assert.deepEqual(rawShared.response, { role: 'assistant', content: 'Answer.' }, `${label}: shared response is not the raw completion`);// Tool-call arguments can embed raw binary (solve_captcha image_to_text):// multi-step trajectories keep the calls, never the bytes.const captchaBytes = `iVBORw0KGgoAAAANSUhEUg${'A'.repeat(500)}`;@@ -12816,6 +12829,7 @@ 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, /shareRawResponse/, `${browser}: shared response must be the raw provider completion`);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`);@@ -12827,7 +12841,8 @@ 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(chromeOutbox, /scrubText\(storedResponse/, `${browser}: shared responses must get the binary scrub`);+ assert.match(chromeOutbox, /sharedResponse/, `${browser}: raw provider completion must be storable separately`);assert.match(chromeOutbox, /_attachImage/, `${browser}: binary metadata attachments must be dropped`);assert.match(chromeOutbox, /scrubToolCallArguments/, `${browser}: tool-call arguments must be scrubbed`);const managerSource = fs.readFileSync(path.join(ROOT, `src/${browser}/src/providers/manager.js`), 'utf8');