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
import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import http from 'node:http';
import os from 'node:os';
import path from 'node:path';
import { chromium } from 'playwright';
import { conformFixtureCompletionCall } from './completion-provider-contract.mjs';
import { fixturePageTextReply } from '../semantic-decision-stub.mjs';
// The confirmation markup is Mastodon's native v4.4.25 structure. The
// extension, provider pipeline, browser dispatch and completion gates are real;
// provider and site requests stay in this disposable, owned fixture.
const judgeOutcome = process.env.WEBBRAIN_QUALITY_DELETE_VERDICT || 'succeeded';
assert.ok(['succeeded', 'uncertain', 'pending', 'failed'].includes(judgeOutcome));
const interruption = process.env.WEBBRAIN_QUALITY_DELETE_NAV_INTERRUPT || '';
assert.ok(['', 'judge-document', 'before-capture'].includes(interruption));
const navigationCase = process.env.WEBBRAIN_QUALITY_DELETE_NAV_CASE || 'ordinary';
assert.ok(['ordinary', 'delayed', 'same-url'].includes(navigationCase));
const out = path.resolve(process.env.WEBBRAIN_QUALITY_ARTIFACTS || `ci/artifacts/quality-audit-2026-10-07/mastodon-deletion-navigation-mv3/${interruption || judgeOutcome}`);
await fs.mkdir(out, { recursive: true });
const account = 'webbrain_qa_owned', resource = `https://mastoturk.org/@${account}/200`;
const pageUrl = `https://mastoturk.org/@${account}`;
const navigationUrl = navigationCase === 'same-url' ? resource : pageUrl;
const task = `Delete only our owned QA post at "${resource}" with exact body "Owned deletion QA post". Deletion of this one disposable QA post is explicitly authorized. Do not publish, delete other content or send messages. Verify it is removed.`;
const intent = { request_kind: 'execute', scope_relation: 'new', requires_state_change: true,
requires_submission: true, messaging: null, deliverables: ['The exact owned QA post is deleted and verified.'],
allows_planner_shaped_result: false, allows_app_state_tool_evidence: false,
completion_requirements: { download: false }, read_scope: 'visible_page',
summary: 'Delete and verify only the authorized owned QA post.',
steps: [{ id: '1', action: 'Delete the exact owned post and verify its absence.' }],
memory: { use_progress_ledger: false, progress_action: null }, scheduling: null, risks: [] };
let deleted = false, actionCalls = 0, judgeCalls = 0, blockedRemote = 0, page;
const completionRecovery = { reads: 0 };
const deletes = [], providerRequests = [], judgeEvidence = [];
const textOf = content => typeof content === 'string' ? content : Array.isArray(content)
? content.filter(part => part?.type === 'text').map(part => part.text || '').join('\n') : '';
const refFor = (input, name) => {
for (const message of [...input.messages].reverse()) {
if (message.role !== 'tool') continue;
let data; try { data = JSON.parse(textOf(message.content)); } catch {
const content = textOf(message.content), start = content.indexOf('{');
try { data = JSON.parse(content.slice(start, content.lastIndexOf('}') + 1)); } catch { continue; }
}
const tree = data.pageContent || '';
const line = tree.split('\n').find(line => line.includes(`button "${name}"`));
const ref = line?.match(/\[(ref_\d+)\]/)?.[1];
if (ref) return ref;
}
throw new Error(`Owned fixture did not observe ${name}`);
};
const server = http.createServer(async (request, response) => {
if (request.url === '/resource') {
response.writeHead(deleted ? 404 : 200, { 'Content-Type': 'text/html' });
response.end(deleted ? 'The owned QA post is absent.' : 'Owned deletion QA post'); return;
}
if (request.url !== '/v1/chat/completions') { response.writeHead(404).end(); return; }
let raw = ''; for await (const part of request) raw += part;
const input = JSON.parse(raw); providerRequests.push(input);
await new Promise(resolve => setTimeout(resolve, 80));
const system = textOf(input.messages?.[0]?.content);
let completion;
if (system.startsWith('Evaluate only the observable requirements of the genuine user task')) {
judgeCalls++; judgeEvidence.push(textOf(input.messages[1]?.content));
if (interruption === 'judge-document' && judgeCalls === 1) await page.reload();
// A thinking provider counts hidden reasoning in its completion budget.
// Exercise that real provider wire contract without mocking Agent gates.
completion = Number(input.max_tokens) < 700
? { role: 'assistant', content: '', reasoning: 'The bounded hidden reasoning used the output limit.' }
: { role: 'assistant', content: JSON.stringify({ outcome: judgeOutcome,
reason: judgeOutcome === 'succeeded'
? 'The exact resource returns HTTP404 after the owned confirmation dispatch and the fresh native profile contains no status.'
: 'The owned independent judge deliberately withholds a positive completion verdict.' }) };
} else if (system.startsWith('Identify only the exact plain public Mastodon post authorized by the genuine user task.')) {
completion = { role: 'assistant', content: JSON.stringify({ kind: 'unsupported', body: null, account: null }) };
} else if (fixturePageTextReply(input.messages)) {
// A status the page showed, read the way a model would read it.
completion = { role: 'assistant', content: fixturePageTextReply(input.messages) };
} else if (system.startsWith("Answer one fixed-choice question for a browser agent's safety check.")) {
// The model's reading of the request: it names exactly this post by its link.
const request = JSON.parse(textOf(input.messages[1].content)).request;
const untrusted = textOf(input.messages.at(-1).content);
const { page } = JSON.parse(untrusted.slice(untrusted.indexOf('{'), untrusted.lastIndexOf('}') + 1));
const named = system.includes('"delete_this"') && request === task && page?.link === resource;
completion = { role: 'assistant', content: JSON.stringify(named
? { choice: 'delete_this', evidence: `Delete only our owned QA post at "${resource}"` } : { choice: 'unclear', evidence: '' }) };
} else if (system.startsWith('Check one separately proposed consequential action')) {
throw new Error('A deletion is decided by the deletion question, not the publication check.');
} else if (!input.tools?.length) completion = { role: 'assistant', content: JSON.stringify(intent) };
else {
const step = actionCalls++;
const call = step === 1 ? { name: 'click_ax', args: { ref_id: refFor(input, 'More owned post') } }
: step === 3 ? { name: 'click_ax', args: { ref_id: refFor(input, 'Delete owned post') } }
: step === 5 ? { name: 'click_ax', args: { ref_id: refFor(input, 'Sil') } }
: step === 6 ? { name: 'navigate', args: { url: navigationUrl } }
: step === 7 ? { name: 'fetch_url', args: { url: resource } }
: step === 8 ? { name: 'get_accessibility_tree', args: { filter: 'all' } }
: step > 9 && (judgeOutcome !== 'succeeded' || interruption)
? { name: 'done', args: { outcome: 'partial', summary: 'The owned native deletion ran once, but the independent judge has not verified completion. Do not repeat deletion.' } }
: step > 8 ? { name: 'done', args: { outcome: 'success', summary: 'The exact owned QA post was deleted once and verified absent; the persistent empty composer is unrelated.' } }
: { name: 'get_accessibility_tree', args: { filter: 'all' } };
const emitted = conformFixtureCompletionCall(input, call, completionRecovery);
completion = { role: 'assistant', content: '', tool_calls: [{ id: `owned-delete-${step}`, type: 'function',
function: { name: emitted.name, arguments: JSON.stringify(emitted.args) } }] };
}
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ id: `response-${providerRequests.length}`, choices: [{ message: completion,
finish_reason: completion.tool_calls ? 'tool_calls' : completion.content ? 'stop' : 'length' }], usage: { prompt_tokens: 100, completion_tokens: 30 } }));
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
const pageBody = `<!doctype html><meta charset="utf-8"><meta name="description" content="1 post (stale static metadata)"><title>Owned native deletion fixture</title>
<script id="initial-state" type="application/json">{"meta":{"delete_modal":true}}</script>
<style>body{font:18px sans-serif;margin:25px}form{padding:12px}textarea{display:block;width:650px;height:110px}.status{margin:20px}.safety-action-modal{position:fixed;inset:120px 250px;background:white;border:2px solid black;padding:30px}button{padding:12px}</style>
<form><input placeholder="Search Mastodon"></form><form class="compose-form"><div class="navigation-bar"><a class="account__display-name" href="/@${account}">@${account}</a></div><textarea class="autosuggest-textarea__textarea" aria-label="Post text"></textarea><div class="compose-form__submit"><button type="submit">Publish</button></div></form>
<main id="statuses"><div class="status"><a class="status__relative-time" href="/@${account}/200">Now</a><a class="status__display-name" href="/@${account}">@${account}</a><div class="status__content__text--visible"><p>Owned deletion QA post</p></div><div class="status__action-bar"><button id="more" type="button" class="icon-button" aria-label="More owned post"><svg class="icon icon-ellipsis-h" aria-hidden="true"></svg></button></div></div></main><div id="modal"></div>
<script>document.querySelector('#more').onclick=()=>{const menu=document.createElement('div');menu.className='dropdown-menu';menu.innerHTML='<div class="dropdown-menu__container"><ul class="dropdown-menu__container__list"><li class="dropdown-menu__item dropdown-menu__item--dangerous"><button data-index="0" aria-label="Delete owned post">Delete</button></li></ul></div>';const b=menu.querySelector('button');b.onclick=()=>{document.querySelector('#modal').innerHTML='<div role="dialog" aria-modal="true"><div class="modal-root__modal safety-action-modal"><div class="safety-action-modal__top"><div class="safety-action-modal__confirmation"><h1>Gönderiyi sil?</h1><p>Bu tootu silmek istediğinden emin misin?</p></div></div><div class="safety-action-modal__bottom"><div class="safety-action-modal__actions"><button class="link-button" type="button">İptal</button><button class="button" type="button">Sil</button></div></div></div></div>';document.querySelector('.link-button').onclick=()=>document.querySelector('#modal').replaceChildren();document.querySelector('.button').onclick=async()=>{const r=await fetch('/api/v1/statuses/200',{method:'DELETE'});if(r.ok){document.querySelector('#statuses').textContent='0 posts. No posts here.';document.querySelector('#modal').replaceChildren();}};};document.querySelector('#modal').append(menu);};document.querySelector('form.compose-form').onsubmit=e=>{e.preventDefault();throw new Error('Unrelated composer must not be submitted');};</script>`;
const profile = await fs.mkdtemp(path.join(os.tmpdir(), 'webbrain-mastodon-delete-mv3-'));
let context;
try {
const extension = path.join(profile, 'extension'); await fs.cp(path.resolve('src/chrome'), extension, { recursive: true });
await fs.appendFile(path.join(extension, 'src/background.js'), '\nglobalThis.deletionFixtureAgent = agent;\n');
context = await chromium.launchPersistentContext(profile, { headless: true, channel: 'chromium',
args: [`--disable-extensions-except=${extension}`, `--load-extension=${extension}`,
'--host-resolver-rules=MAP * ~NOTFOUND, EXCLUDE 127.0.0.1'] });
await context.route('**/*', async route => {
const request = route.request(), requested = new URL(request.url());
if (requested.hostname === '127.0.0.1' && requested.port === String(server.address().port)) return route.continue();
if (requested.origin === 'https://mastoturk.org') {
if (requested.pathname === '/api/v1/statuses/200' && request.method() === 'DELETE') {
deletes.push({ method: request.method(), path: requested.pathname }); deleted = true;
return route.fulfill({ status: 200, contentType: 'application/json', body: '{}' });
}
if (requested.pathname === '/qa-blocking.js' && request.method() === 'GET') {
await new Promise(resolve => setTimeout(resolve, 1200));
return route.fulfill({ contentType: 'application/javascript', body: '/* Owned document_idle delay. */' });
}
if (request.isNavigationRequest() && [pageUrl, resource].includes(requested.href)) {
const body = deleted && navigationCase === 'delayed'
? pageBody.replace('<style>', '<script src="/qa-blocking.js"></script><style>') : pageBody;
return route.fulfill({ contentType: 'text/html', body: body
+ (deleted ? '<script>document.querySelector("#statuses").textContent="0 posts. No posts here.";</script>' : '') });
}
return route.fulfill({ status: 404, body: '' });
}
if (/^https?:$/.test(requested.protocol)) { blockedRemote++; return route.abort('blockedbyclient'); }
return route.continue();
});
const worker = context.serviceWorkers()[0] || await context.waitForEvent('serviceworker', { timeout: 30_000 });
await worker.evaluate(({ resource, bridge }) => {
const send = fetch.bind(globalThis);
globalThis.fetch = (input, options) => {
const requested = new URL(typeof input === 'string' ? input : input.url);
if (requested.href === resource && (!options?.method || options.method === 'GET')) {
// This is a local network mapping of the owned origin, not a redirect.
return send(bridge, options).then(response => Object.defineProperty(response, 'url', { value: requested.href }));
}
if (requested.hostname === '127.0.0.1' || ['chrome-extension:', 'data:', 'blob:'].includes(requested.protocol)) return send(input, options);
return Promise.reject(new Error('Owned deletion fixture blocks external network requests.'));
};
}, { resource, bridge: `http://127.0.0.1:${server.address().port}/resource` });
if (interruption === 'before-capture') await worker.evaluate(() => {
const original = deletionFixtureAgent._captureCompletionSemanticNavigation;
let replaced = false;
deletionFixtureAgent._captureCompletionSemanticNavigation = async function (tabId, name, args, result, executionContext) {
if (!replaced && name === 'navigate' && result?.success === true) {
replaced = true;
const committed = new Promise(resolve => {
const listener = event => {
if (event.tabId !== tabId || event.frameId !== 0) return;
chrome.webNavigation.onCommitted.removeListener(listener); resolve();
};
chrome.webNavigation.onCommitted.addListener(listener);
});
await chrome.tabs.reload(tabId); await committed;
}
return original.call(this, tabId, name, args, result, executionContext);
};
});
const harness = await context.newPage(); await harness.goto(`chrome-extension://${new URL(worker.url()).host}/src/ui/settings.html`);
const message = (action, data = {}) => harness.evaluate(({ action, data }) => chrome.runtime.sendMessage({ target: 'background', action, ...data }), { action, data });
await harness.evaluate(() => chrome.storage.local.set({ helpImproveWebBrain: false, tracingEnabled: false,
autoScreenshot: false, screenshotFallback: false, planBeforeActMode: 'off', planBeforeAct: false,
planReviewMode: 'never', visionMode: 'off', maxAgentSteps: 14, systemOneEnabled: false, agentAllowLocalNetwork: true,
askBeforeConsequentialActions: false, openaiAskStreamingEnabled: false }));
assert.equal((await message('update_provider', { providerId: 'openrouter', config: { apiKey: 'owned-fixture-fake-key',
baseUrl: `http://127.0.0.1:${server.address().port}/v1`, model: 'minimax/minimax-m3', enabled: true,
contextWindow: 1000000, supportsAskStreaming: false } })).ok, true);
assert.equal((await message('set_active_provider', { providerId: 'openrouter' })).ok, true);
page = await context.newPage(); await page.goto(resource);
const tabId = await harness.evaluate(async url => (await chrome.tabs.query({})).find(tab => tab.url === url)?.id, resource);
const result = await message('chat', { tabId, text: task, mode: 'act', requestId: 'owned-native-deletion', locale: 'en' });
const diagnostic = await worker.evaluate(tabId => ({ submit: deletionFixtureAgent._completionSubmitStates.get(tabId),
verdict: deletionFixtureAgent._completionVerdicts?.get(tabId), guard: deletionFixtureAgent._planExecutionGuards.get(tabId) }), tabId);
await fs.writeFile(path.join(out, 'evidence.json'), JSON.stringify({ result, diagnostic, deletes, judgeCalls, judgeEvidence,
providerRequests, blockedRemote }, (_key, value) => typeof value === 'string' && value.startsWith('data:image/') ? '[owned fixture screenshot omitted]' : value, 2));
assert.deepEqual(deletes, [{ method: 'DELETE', path: '/api/v1/statuses/200' }], 'Only one actual native deletion occurs.');
assert.equal(await page.locator('textarea').inputValue(), '', 'The persistent composer remains empty.');
const done = result.updates.findLast(event => event.type === 'tool_result' && event.data?.name === 'done')?.data?.result;
assert.equal(done?.done, true, JSON.stringify(done));
if (judgeOutcome === 'succeeded' && !interruption) assert.equal(done?.outcome, 'success', JSON.stringify(done));
else {
assert.equal(done?.outcome, 'partial', JSON.stringify(done));
assert.ok(result.updates.some(event => event.type === 'tool_result' && event.data?.name === 'done'
&& event.data.result.blockedDone === true), 'A nonpositive or stale verdict must block the initial success attempt.');
}
if (interruption === 'before-capture') {
assert.equal(judgeCalls, 0, 'A replacement before receipt recording must not reach a positive independent judge.');
assert.equal(diagnostic.submit.semanticCommit.invalidated, true);
assert.equal(diagnostic.submit.semanticCommit.verificationNavigation, undefined);
} else {
assert.ok(judgeCalls > 0); assert.ok(judgeEvidence.some(text => /404/.test(text)), 'Actual resource absence reaches the independent judge.');
}
for (const request of providerRequests.filter(input => textOf(input.messages?.[0]?.content).startsWith('Evaluate only the observable requirements'))) {
assert.ok(request.max_tokens >= 700 && request.max_tokens <= 4096,
'The installed independent evaluator must leave bounded space for hidden reasoning and its JSON verdict');
assert.equal(request.tools, undefined, 'The independent evaluator cannot dispatch fallback browser tools');
}
assert.doesNotMatch(JSON.stringify(providerRequests), /_completionNavigationReceipt|scopePromise|nativeDocumentId/,
'Private browser commit receipts never enter a model request.');
const names = result.updates.filter(e => e.type === 'tool_call').map(e => e.data.name);
assert.ok(names.indexOf('navigate') > names.indexOf('click_ax'));
assert.equal(result.updates.filter(e => e.type === 'tool_result' && e.data.name === 'navigate' && e.data.result.success === true).length, 1);
assert.equal(done.verification.completionWarning, null);
if (interruption !== 'before-capture') {
assert.ok(judgeEvidence.some(text => { const value = JSON.parse(text); return (Array.isArray(value) ? value[0] : value).document === navigationUrl; }));
assert.equal(diagnostic.submit.semanticCommit.verificationNavigation.url, navigationUrl);
assert.notEqual(diagnostic.submit.semanticCommit.documentToken, diagnostic.submit.semanticCommit.verificationNavigation.documentToken);
}
if (interruption === 'judge-document') {
const identity = await page.evaluate(() => [location.href, performance.timeOrigin].join('|'));
assert.notEqual(diagnostic.submit.semanticCommit.verificationNavigation.documentIdentity, identity,
'The replacement document must differ from the exact app-owned verification navigation.');
assert.notEqual(diagnostic.verdict?.outcome, 'succeeded');
}
console.log(JSON.stringify({ passed: true, installedMV3: true, verificationNavigation: interruption !== 'before-capture', navigationCase, deletes: deletes.length, judgeCalls, judgeOutcome, interruption, persistentForms: 2, blockedRemote }));
} finally {
await context?.close(); await new Promise(resolve => server.close(resolve));
const resolved = path.resolve(profile);
if (!resolved.startsWith(path.resolve(os.tmpdir()) + path.sep + 'webbrain-mastodon-delete-mv3-')) throw new Error('Unexpected temporary profile path.');
await fs.rm(resolved, { recursive: true, force: true });
}