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): honor native consent and preserve raw completions
Emre Sokullu1de4385e89f1
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 0db80df8a..e6d486e45 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -12738,7 +12738,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// rawSummary is the model-authored done text before the progress// ledger is appended for display; research sharing stores it as the// response instead of mislabeling local presentation as provider output.- return { action: 'return', value: finalResponse, rawSummary: repairedDoneSummary };+ return { action: 'return', value: finalResponse, rawSummary: rawDoneSummary };}// Loop detection — exact calls, semantic AX reads, and coordinates run@@ -21217,6 +21217,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}if (returnResult) return result;if (result?.toolCalls?.length) return '';+ if (shareCapture) shareCapture.response = String(result?.content ?? '');return repairAssistantDisplayText(String(result?.content || '').trim());}@@ -40709,6 +40710,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// keeping the exact request (with the context-only system prompt).if (responseOnly.status === 'done') {shareHadProviderCompletion = true;+ shareRawResponse = responseOnlyShareCapture.response;if (Array.isArray(responseOnlyShareCapture.request) && responseOnlyShareCapture.request.length) {currentNonStreamRequestMessages = responseOnlyShareCapture.request;}@@ -41602,11 +41604,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout donUpdate('warning', { message: finalResponse });break;}+ // Preserve provider text before display repairs and local notices.+ shareRawResponse = String(result.content ?? '');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;@@ -42001,6 +42001,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d);if (responseOnly.status === 'done') {shareHadProviderCompletion = true;+ shareRawResponse = responseOnlyShareCapture.response;if (Array.isArray(responseOnlyShareCapture.request) && responseOnlyShareCapture.request.length) {currentStreamRequestMessages = responseOnlyShareCapture.request;}@@ -42642,6 +42643,8 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dcloseTraceStep({ ok: false, code: 'EMPTY_RESPONSE' });return finish(incompleteFailure, 'incomplete_output');}+ // Streaming text is still the provider's raw completion at this point.+ shareRawResponse = fullText;const repairedFullText = repairAssistantDisplayText(fullText);if (repairedFullText !== fullText) {fullText = repairedFullText;@@ -42649,9 +42652,6 @@ 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}`;diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex e63317a7f..db1523b09 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -11251,7 +11251,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// rawSummary is the model-authored done text before the progress// ledger is appended for display; research sharing stores it as the// response instead of mislabeling local presentation as provider output.- return { action: 'return', value: finalResponse, rawSummary: repairedDoneSummary };+ return { action: 'return', value: finalResponse, rawSummary: rawDoneSummary };}// Loop detection — exact calls, semantic AX reads, and coordinates run@@ -18961,6 +18961,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}if (returnResult) return result;if (result?.toolCalls?.length) return '';+ if (shareCapture) shareCapture.response = String(result?.content ?? '');return repairAssistantDisplayText(String(result?.content || '').trim());}@@ -33847,6 +33848,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d// keeping the exact request (with the context-only system prompt).if (responseOnly.status === 'done') {shareHadProviderCompletion = true;+ shareRawResponse = responseOnlyShareCapture.response;if (Array.isArray(responseOnlyShareCapture.request) && responseOnlyShareCapture.request.length) {currentNonStreamRequestMessages = responseOnlyShareCapture.request;}@@ -34629,11 +34631,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout donUpdate('warning', { message: finalResponse });break;}+ // Preserve provider text before display repairs and local notices.+ shareRawResponse = String(result.content ?? '');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;@@ -34979,6 +34979,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d);if (responseOnly.status === 'done') {shareHadProviderCompletion = true;+ shareRawResponse = responseOnlyShareCapture.response;if (Array.isArray(responseOnlyShareCapture.request) && responseOnlyShareCapture.request.length) {currentStreamRequestMessages = responseOnlyShareCapture.request;}@@ -35534,10 +35535,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dthis._persist(tabId);return finish(planOnlyDecision.failure, planOnlyDecision.status || 'plan_only_output');}+ // Streaming text is still the provider's raw completion at this point.+ shareRawResponse = fullText;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.diff --git a/src/firefox/src/providers/openai.js b/src/firefox/src/providers/openai.jsindex c6cce7ff7..80601cc92 100644--- a/src/firefox/src/providers/openai.js+++ b/src/firefox/src/providers/openai.js@@ -10,6 +10,7 @@ import {applyOpenRouterRoutingVariant,} from './provider-compatibility.js';import { normalizeRuntimeTraceConfig } from '../trace/runtime-config.js';+import { RESEARCH_DATA_COLLECTION } from '../trace/research-consent.js';import { canonicalizeOllamaBaseUrl } from './context-windows.js';import { AUTO_VISION_PROVIDER_IDS, configuredVisionSupport } from './vision-capabilities.js';@@ -239,6 +240,15 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {if (String(this.config.providerName || '').toLowerCase() !== 'webbrain-cloud') {return { ok: false, retryable: false, status: 0 };}+ // Persisted opt-ins and queued entries must also honor native permission+ // revocation from Firefox's extension settings before any upload.+ try {+ if (!await browser.permissions.contains({ data_collection: RESEARCH_DATA_COLLECTION })) {+ return { ok: false, retryable: false, status: 0 };+ }+ } catch {+ return { ok: false, retryable: false, status: 0 };+ }const controller = typeof AbortController === 'function' ? new AbortController() : null;const timer = controller ? setTimeout(() => controller.abort(), Math.max(250, timeoutMs)) : null;try {diff --git a/src/firefox/src/trace/research-consent.js b/src/firefox/src/trace/research-consent.jsnew file mode 100644index 000000000..316633cca--- /dev/null+++ b/src/firefox/src/trace/research-consent.js@@ -0,0 +1,9 @@+// Firefox's native data-collection consent covers the text and attribution+// included in an opted-in research generation.+export const RESEARCH_DATA_COLLECTION = [+ 'personallyIdentifyingInfo',+ 'authenticationInfo',+ 'personalCommunications',+ 'websiteContent',+ 'technicalAndInteraction',+];diff --git a/src/firefox/src/ui/settings.js b/src/firefox/src/ui/settings.jsindex a73e22ad4..270946031 100644--- a/src/firefox/src/ui/settings.js+++ b/src/firefox/src/ui/settings.js@@ -4,6 +4,7 @@import { t, getLocale, setLocale, LANGUAGES } from './i18n.js';import { escapeHtml } from './utils.js';+import { RESEARCH_DATA_COLLECTION } from '../trace/research-consent.js';import { THEME_MODES, applyMode, loadMode, watch } from './theme.js';import {UI_SCALE_LEVELS,@@ -1914,6 +1915,33 @@ const SHARE_RESEARCH_FIELD = {type: 'checkbox',};+async function confirmResearchSharing(event) {+ const input = event.currentTarget;+ if (!input.checked) return;+ if (!window.confirm(t('st.providers.share_research.confirm'))) {+ event.preventDefault();+ return;+ }+ // Keep the setting off while Firefox asks for native collection consent.+ // Call request directly in the click handler, before the first await, to+ // preserve the user gesture required by the permissions API.+ input.checked = false;+ input.disabled = true;+ try {+ const granted = await browser.permissions.request({+ data_collection: RESEARCH_DATA_COLLECTION,+ });+ if (granted && input.isConnected) {+ input.checked = true;+ markProviderDirty(input.dataset.provider);+ }+ } catch {+ // Denied or unavailable native consent leaves research sharing off.+ } finally {+ input.disabled = false;+ }+}+function providerDefinitionId(id, config = providersData[id]) {return String(config?.sourceProviderId || config?.duplicateOf || id || '');}@@ -2907,15 +2935,7 @@ function renderProviders() {input.addEventListener(eventName, () => markProviderDirty(input.dataset.provider));});document.querySelectorAll('input[data-key="shareQueriesForResearch"]').forEach(input => {- input.addEventListener('click', (event) => {- // In click handlers, `input.checked` has already updated to the target- // state. Only prompt for confirmation when turning the toggle ON.- // preventDefault() cancels the click and rolls `checked` back to false.- if (!input.checked) return;- if (!window.confirm(t('st.providers.share_research.confirm'))) {- event.preventDefault();- }- });+ input.addEventListener('click', confirmResearchSharing);});document.querySelectorAll('.btn-remove-duplicate').forEach(btn => {btn.addEventListener('click', () => removeDuplicateProvider(btn.dataset.provider));diff --git a/test/run.js b/test/run.jsindex b9c2c7488..c229c6aa5 100644--- a/test/run.js+++ b/test/run.js@@ -13040,6 +13040,70 @@ test('Provider settings permanently purge queued shares on opt-out, including ac}});+test('Firefox research opt-in requires native consent and stays off while consent is pending', async () => {+ const source = fs.readFileSync(path.join(ROOT, 'src/firefox/src/ui/settings.js'), 'utf8');+ const start = source.indexOf('async function confirmResearchSharing(event) {');+ const end = source.indexOf('\nfunction providerDefinitionId', start);+ assert.ok(start >= 0 && end > start);+ const { RESEARCH_DATA_COLLECTION } = await import(pathToFileURL(path.join(ROOT, 'src/firefox/src/trace/research-consent.js')).href);+ const manifest = JSON.parse(fs.readFileSync(path.join(ROOT, 'src/firefox/manifest.json'), 'utf8'));+ assert.deepEqual(RESEARCH_DATA_COLLECTION, manifest.browser_specific_settings.gecko.data_collection_permissions.optional);+ for (const outcome of ['grant', 'deny', 'error', 'cancel', 'disable', 'detached']) {+ let settle;+ let reject;+ const pendingPermission = new Promise((resolve, rejectPermission) => { settle = resolve; reject = rejectPermission; });+ const requests = [];+ const dirty = [];+ let cancelled = false;+ const input = { checked: outcome !== 'disable', disabled: false, isConnected: true, dataset: { provider: 'openai' } };+ const handler = Function('browser', 'window', 't', 'markProviderDirty', 'RESEARCH_DATA_COLLECTION', `${source.slice(start, end)}\nreturn confirmResearchSharing;`)(+ { permissions: { request: permission => { requests.push(permission); return pendingPermission; } } },+ { confirm: () => outcome !== 'cancel' }, key => key, id => dirty.push(id), RESEARCH_DATA_COLLECTION,+ );+ const completion = handler({ currentTarget: input, preventDefault() { cancelled = true; input.checked = false; } });+ if (outcome === 'cancel' || outcome === 'disable') {+ await completion;+ assert.equal(requests.length, 0, `${outcome}: native consent should not be requested`);+ assert.equal(cancelled, outcome === 'cancel');+ } else {+ assert.deepEqual(requests, [{ data_collection: RESEARCH_DATA_COLLECTION }], 'request must start synchronously in the click handler');+ assert.equal(input.checked, false, 'an ungranted opt-in became saveable');+ assert.equal(input.disabled, true);+ if (outcome === 'detached') input.isConnected = false;+ if (outcome === 'error') reject(new Error('Permission unavailable'));+ else settle(outcome !== 'deny');+ await completion;+ }+ assert.equal(input.checked, outcome === 'grant', `${outcome}: incorrect final checkbox state`);+ assert.equal(input.disabled, false, `${outcome}: checkbox stayed disabled`);+ assert.deepEqual(dirty, outcome === 'grant' ? ['openai'] : []);+ }+});++test('Firefox research transport blocks native permission revocation before uploading', async () => {+ const originalBrowser = globalThis.browser;+ const originalFetch = globalThis.fetch;+ try {+ const provider = new OpenAIProviderFx({ providerName: 'webbrain-cloud', baseUrl: 'https://share.example.test/v1' });+ for (const consent of [false, 'error', true]) {+ const requests = [];+ globalThis.browser = { permissions: { async contains(permission) {+ assert.ok(permission.data_collection.includes('websiteContent'));+ if (consent === 'error') throw new Error('Permission unavailable');+ return consent;+ } } };+ globalThis.fetch = async (url, options) => { requests.push({ url, options }); return new Response('', { status: 202 }); };+ const result = await provider.sendShareGeneration('share_test', { request: [], response: { content: 'ok' } });+ assert.equal(requests.length, consent === true ? 1 : 0, `${consent}: native consent was bypassed`);+ assert.equal(result.ok, consent === true);+ assert.equal(result.retryable, false);+ }+ } finally {+ globalThis.browser = originalBrowser;+ globalThis.fetch = originalFetch;+ }+});+test('Share-for-research delivery stays opt-in and mirrored across both builds', () => {const chromeOutbox = fs.readFileSync(path.join(ROOT, 'src/chrome/src/trace/webbrain-share-outbox.js'), 'utf8');const firefoxOutbox = fs.readFileSync(path.join(ROOT, 'src/firefox/src/trace/webbrain-share-outbox.js'), 'utf8');@@ -68956,6 +69020,40 @@ test('provider response path preserves raw assistant content and metadata', asyn}});+test('shared completions retain provider text before terminal display repairs', async () => {+ const samples = [+ ['Verification:\n- Page title: "Example Domain"', 'Verification:\n- Page title: Example Domain'],+ [JSON.stringify('**Result**\n\n- First\n- Second').slice(1, -1), '**Result**\n\n- First\n- Second'],+ ];+ for (const [label, AgentClass] of [['chrome', AgentCh], ['firefox', AgentFx]]) {+ for (const streaming of [false, true]) {+ for (const responseOnly of [false, true]) {+ for (const [raw, displayed] of samples) {+ const provider = {+ supportsTools: true, supportsVision: false, promptTier: 'full', contextWindow: 128000,+ model: 'test-model', name: 'test-provider',+ async chat() { return { content: raw, toolCalls: [] }; },+ async *chatStream() { yield { type: 'text', content: raw }; yield { type: 'done' }; },+ };+ const agent = new AgentClass({ getActive: () => provider, getVisionProvider: async () => null });+ const tabId = 4095;+ configurePlanOnlyGuardAgent(agent, tabId);+ if (responseOnly) agent._maybeRunPlannerGate = async () => ({ proceed: true, responseOnly: true, requestKind: 'respond', requiresStateChange: false });+ agent._startTraceRun = async () => null;+ let capture;+ agent._endTraceRun = async (_tabId, _runId, status, content, options) => { capture = { status, content, ...options }; };+ const final = await agent[streaming ? 'processMessageStream' : 'processMessage'](tabId, 'Show the result.', () => {}, 'ask');+ const context = `${label}: streaming=${streaming}, responseOnly=${responseOnly}`;+ assert.equal(final, displayed, `${context}: display repairs changed`);+ assert.equal(capture.status, 'done', context);+ assert.equal(capture.hadProviderCompletion, true, context);+ assert.equal(capture.shareResponse, raw, `${context}: shared completion was display-repaired`);+ }+ }+ }+ }+});+test('terminal display repair normalizes JSON-quoted page title lines', async () => {const title = 'Example Domain';const malformed = `Verification:\n- Page title: ${JSON.stringify(title)}\n- Timestamp: 3:39 PM`;@@ -85062,6 +85160,7 @@ test('accepted done repairs only the terminal display summary', async () => {assert.equal(result.action, 'return', `${AgentClass.name}: accepted done should finish`);assert.equal(result.value, expected, `${AgentClass.name}: done summary display was not repaired`);+ assert.equal(result.rawSummary, malformed, `${AgentClass.name}: shared done summary was display-repaired`);const rawUpdate = updates.find(update => update.type === 'tool_result' && update.data?.name === 'done');assert.equal(rawUpdate?.data?.result?.summary,