Skip to content

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

Use service-worker-safe imports and guarded local decision diagnostics

Barack Sokullu71cfc3d81aa0
diff --git a/src/chrome/src/agent/systemone-judge.js b/src/chrome/src/agent/systemone-judge.js
index 23d3fab06..1b3b06ffb 100644
--- a/src/chrome/src/agent/systemone-judge.js
+++ b/src/chrome/src/agent/systemone-judge.js
@@ -4,6 +4,8 @@
// answer-shape checks so scheduler callers only need one small evaluate()
// interface. It never performs an action or turns a non-success into success.
+import { evaluateD1 } from '../providers/d1.js';
+
export const SYSTEM_ONE_API_URL = 'https://api.typesafe.ai/v1/systemone';
export const SYSTEM_ONE_MODEL = 'jev-1.13.0';
export const SYSTEM_ONE_API_KEY = 'typesafeApiKey';
@@ -282,7 +284,6 @@ export function createSystemOneJudge({
try {
if (config?.provider === 'webgpu_d1') {
if (beforeRequest) await abortable(beforeRequest(), request.signal);
- const { evaluateD1 } = await import('../providers/d1.js');
const result = await abortable(evaluateD1({ state, images, questions, config, signal: request.signal }), request.signal);
validateSystemOneAnswers(result.answers, questions);
if (result.model !== model || !Number.isInteger(result.usage?.input_tokens) || result.usage.input_tokens < 0 || result.usage?.output_tokens !== 0) throw systemOneError('JEV_INVALID_USAGE', 'Invalid local D1 usage/model.');
diff --git a/src/chrome/src/background.js b/src/chrome/src/background.js
index d5ca2190e..cd62044d1 100644
--- a/src/chrome/src/background.js
+++ b/src/chrome/src/background.js
@@ -1,5 +1,6 @@
import { resolveDecisionConfig, listDecisionModels } from './agent/decision-config.js';
import { d1Request } from './providers/d1.js';
+import { evaluateD1LocalFixture } from './providers/d1-diagnostic.js';
import { D1_CONSENT_KEY, D1_CONSENT_VERSION } from './providers/d1-config.js';
chrome.storage.onChanged.addListener((changes, area) => {
@@ -4181,6 +4182,10 @@ async function handleMessage(msg, sender) {
await strictSecretModeReady;
try {
if (agent.strictSecretMode) throw new Error('Decision requests are disabled in Strict Secret Mode.');
+ if (Object.hasOwn(msg, 'localFixture')) {
+ const result = await evaluateD1LocalFixture({ fixture: msg.localFixture, sender, api: chrome, strictSecretMode: agent.strictSecretMode, agent, client: createSystemOneJudge({ maxRetries: 0 }) });
+ return { success: true, result };
+ }
let config = msg.settings ? resolveDecisionConfig(msg.settings) : undefined;
if (config?.provider === 'openrouter' && (!msg.settings.decisionVisionMode || msg.settings.decisionVisionMode === 'auto')) {
try { const card = (await listDecisionModels(config)).find(m => m.id === config.model); if (card) config = { ...config, supportsVision: card.supportsVision }; } catch {}
diff --git a/src/chrome/src/providers/d1-diagnostic.js b/src/chrome/src/providers/d1-diagnostic.js
new file mode 100644
index 000000000..8cd2752fc
--- /dev/null
+++ b/src/chrome/src/providers/d1-diagnostic.js
@@ -0,0 +1,19 @@
+import { DECISION_SETTINGS_KEYS, resolveDecisionConfig } from '../agent/decision-config.js';
+import { validateSystemOneAnswers } from '../agent/systemone-judge.js';
+import { D1_CONSENT_KEY, D1_CONSENT_VERSION } from './d1-config.js';
+
+export async function evaluateD1LocalFixture({ fixture, sender, api, strictSecretMode, agent, client }) {
+ const settingsUrl = api.runtime.getURL('src/ui/settings.html');
+ if (sender?.id !== api.runtime.id || String(sender.url || '').split('#')[0] !== settingsUrl) throw new Error('Local D1 fixture tests are available only from extension Settings.');
+ if (strictSecretMode) throw new Error('Decision requests are disabled in Strict Secret Mode.');
+ const stored = await api.storage.local.get(DECISION_SETTINGS_KEYS);
+ if (stored.decisionProvider !== 'webgpu_d1' || stored.systemOneEnabled !== true || stored[D1_CONSENT_KEY] !== D1_CONSENT_VERSION) throw new Error('Select, enable and explicitly consent to the local D1 provider before testing a fixture.');
+ const config = resolveDecisionConfig(stored);
+ if (config.provider !== 'webgpu_d1' || !config.enabled) throw new Error('Select, enable and explicitly consent to the local D1 provider before testing a fixture.');
+ if (!fixture || typeof fixture !== 'object' || Array.isArray(fixture) || !Object.hasOwn(fixture, 'state') || !fixture.questions || (fixture.images != null && !Array.isArray(fixture.images))) throw new Error('Invalid local D1 fixture.');
+ if (fixture.images?.length && !config.supportsVision) throw new Error('Enable local D1 vision before testing an image fixture.');
+ const result = await agent.evaluateSystemOne(null, client, { state: fixture.state, images: fixture.images, questions: fixture.questions, config });
+ validateSystemOneAnswers(result.answers, fixture.questions);
+ if (result.model !== config.model || result.provider !== 'webgpu_d1' || !Number.isInteger(result.usage?.input_tokens) || result.usage.input_tokens < 0 || result.usage?.output_tokens !== 0) throw new Error('Invalid local D1 fixture response.');
+ return result;
+}
diff --git a/src/chrome/src/providers/d1.js b/src/chrome/src/providers/d1.js
index 345fdd98b..2faac2ecf 100644
--- a/src/chrome/src/providers/d1.js
+++ b/src/chrome/src/providers/d1.js
@@ -1,4 +1,6 @@
import { D1_MODEL_ID, D1_CONSENT_KEY, D1_CONSENT_VERSION } from './d1-config.js';
+import { ensureOffscreen } from '../offscreen/ensure.js';
+import { dispatchD1Worker, resetD1Worker } from './d1-host.js';
export async function d1Request(command, payload = {}, { timeoutMs = 90_000, signal } = {}) {
const api = globalThis.browser || globalThis.chrome;
@@ -11,11 +13,10 @@ export async function d1Request(command, payload = {}, { timeoutMs = 90_000, sig
let result;
if (api.offscreen?.createDocument) {
if (['reset', 'dispose', 'stop'].includes(command) && !await api.offscreen.hasDocument()) return { reset: true };
- const { ensureOffscreen } = await import('../offscreen/ensure.js'); await ensureOffscreen();
+ await ensureOffscreen();
const response = await api.runtime.sendMessage({ type: 'd1-offscreen', command, payload, timeoutMs });
if (!response?.ok) throw new Error(response?.error || 'D1 offscreen host unavailable.'); result = response.result;
} else {
- const { dispatchD1Worker, resetD1Worker } = await import('./d1-host.js');
if (command === 'reset') { resetD1Worker(); return { reset: true }; }
result = await dispatchD1Worker(command, payload, timeoutMs);
}
diff --git a/src/firefox/src/agent/systemone-judge.js b/src/firefox/src/agent/systemone-judge.js
index 23d3fab06..1b3b06ffb 100644
--- a/src/firefox/src/agent/systemone-judge.js
+++ b/src/firefox/src/agent/systemone-judge.js
@@ -4,6 +4,8 @@
// answer-shape checks so scheduler callers only need one small evaluate()
// interface. It never performs an action or turns a non-success into success.
+import { evaluateD1 } from '../providers/d1.js';
+
export const SYSTEM_ONE_API_URL = 'https://api.typesafe.ai/v1/systemone';
export const SYSTEM_ONE_MODEL = 'jev-1.13.0';
export const SYSTEM_ONE_API_KEY = 'typesafeApiKey';
@@ -282,7 +284,6 @@ export function createSystemOneJudge({
try {
if (config?.provider === 'webgpu_d1') {
if (beforeRequest) await abortable(beforeRequest(), request.signal);
- const { evaluateD1 } = await import('../providers/d1.js');
const result = await abortable(evaluateD1({ state, images, questions, config, signal: request.signal }), request.signal);
validateSystemOneAnswers(result.answers, questions);
if (result.model !== model || !Number.isInteger(result.usage?.input_tokens) || result.usage.input_tokens < 0 || result.usage?.output_tokens !== 0) throw systemOneError('JEV_INVALID_USAGE', 'Invalid local D1 usage/model.');
diff --git a/src/firefox/src/background.js b/src/firefox/src/background.js
index 13025ff98..3a8d72cca 100644
--- a/src/firefox/src/background.js
+++ b/src/firefox/src/background.js
@@ -1,5 +1,6 @@
import { resolveDecisionConfig, listDecisionModels } from './agent/decision-config.js';
import { d1Request } from './providers/d1.js';
+import { evaluateD1LocalFixture } from './providers/d1-diagnostic.js';
import { D1_CONSENT_KEY, D1_CONSENT_VERSION } from './providers/d1-config.js';
browser.storage.onChanged.addListener((changes, area) => {
@@ -3630,6 +3631,10 @@ async function handleMessage(msg, sender) {
await strictSecretModeReady;
try {
if (agent.strictSecretMode) throw new Error('Decision requests are disabled in Strict Secret Mode.');
+ if (Object.hasOwn(msg, 'localFixture')) {
+ const result = await evaluateD1LocalFixture({ fixture: msg.localFixture, sender, api: browser, strictSecretMode: agent.strictSecretMode, agent, client: createSystemOneJudge({ maxRetries: 0 }) });
+ return { success: true, result };
+ }
let config = msg.settings ? resolveDecisionConfig(msg.settings) : undefined;
if (config?.provider === 'openrouter' && (!msg.settings.decisionVisionMode || msg.settings.decisionVisionMode === 'auto')) {
try { const card = (await listDecisionModels(config)).find(m => m.id === config.model); if (card) config = { ...config, supportsVision: card.supportsVision }; } catch {}
diff --git a/src/firefox/src/providers/d1-diagnostic.js b/src/firefox/src/providers/d1-diagnostic.js
new file mode 100644
index 000000000..8cd2752fc
--- /dev/null
+++ b/src/firefox/src/providers/d1-diagnostic.js
@@ -0,0 +1,19 @@
+import { DECISION_SETTINGS_KEYS, resolveDecisionConfig } from '../agent/decision-config.js';
+import { validateSystemOneAnswers } from '../agent/systemone-judge.js';
+import { D1_CONSENT_KEY, D1_CONSENT_VERSION } from './d1-config.js';
+
+export async function evaluateD1LocalFixture({ fixture, sender, api, strictSecretMode, agent, client }) {
+ const settingsUrl = api.runtime.getURL('src/ui/settings.html');
+ if (sender?.id !== api.runtime.id || String(sender.url || '').split('#')[0] !== settingsUrl) throw new Error('Local D1 fixture tests are available only from extension Settings.');
+ if (strictSecretMode) throw new Error('Decision requests are disabled in Strict Secret Mode.');
+ const stored = await api.storage.local.get(DECISION_SETTINGS_KEYS);
+ if (stored.decisionProvider !== 'webgpu_d1' || stored.systemOneEnabled !== true || stored[D1_CONSENT_KEY] !== D1_CONSENT_VERSION) throw new Error('Select, enable and explicitly consent to the local D1 provider before testing a fixture.');
+ const config = resolveDecisionConfig(stored);
+ if (config.provider !== 'webgpu_d1' || !config.enabled) throw new Error('Select, enable and explicitly consent to the local D1 provider before testing a fixture.');
+ if (!fixture || typeof fixture !== 'object' || Array.isArray(fixture) || !Object.hasOwn(fixture, 'state') || !fixture.questions || (fixture.images != null && !Array.isArray(fixture.images))) throw new Error('Invalid local D1 fixture.');
+ if (fixture.images?.length && !config.supportsVision) throw new Error('Enable local D1 vision before testing an image fixture.');
+ const result = await agent.evaluateSystemOne(null, client, { state: fixture.state, images: fixture.images, questions: fixture.questions, config });
+ validateSystemOneAnswers(result.answers, fixture.questions);
+ if (result.model !== config.model || result.provider !== 'webgpu_d1' || !Number.isInteger(result.usage?.input_tokens) || result.usage.input_tokens < 0 || result.usage?.output_tokens !== 0) throw new Error('Invalid local D1 fixture response.');
+ return result;
+}
diff --git a/src/firefox/src/providers/d1.js b/src/firefox/src/providers/d1.js
index 345fdd98b..fda231d71 100644
--- a/src/firefox/src/providers/d1.js
+++ b/src/firefox/src/providers/d1.js
@@ -1,4 +1,5 @@
import { D1_MODEL_ID, D1_CONSENT_KEY, D1_CONSENT_VERSION } from './d1-config.js';
+import { dispatchD1Worker, resetD1Worker } from './d1-host.js';
export async function d1Request(command, payload = {}, { timeoutMs = 90_000, signal } = {}) {
const api = globalThis.browser || globalThis.chrome;
@@ -8,17 +9,8 @@ export async function d1Request(command, payload = {}, { timeoutMs = 90_000, sig
const abort = () => { void d1Request('reset').catch(() => {}); };
signal?.addEventListener('abort', abort, { once: true });
try {
- let result;
- if (api.offscreen?.createDocument) {
- if (['reset', 'dispose', 'stop'].includes(command) && !await api.offscreen.hasDocument()) return { reset: true };
- const { ensureOffscreen } = await import('../offscreen/ensure.js'); await ensureOffscreen();
- const response = await api.runtime.sendMessage({ type: 'd1-offscreen', command, payload, timeoutMs });
- if (!response?.ok) throw new Error(response?.error || 'D1 offscreen host unavailable.'); result = response.result;
- } else {
- const { dispatchD1Worker, resetD1Worker } = await import('./d1-host.js');
- if (command === 'reset') { resetD1Worker(); return { reset: true }; }
- result = await dispatchD1Worker(command, payload, timeoutMs);
- }
+ if (command === 'reset') { resetD1Worker(); return { reset: true }; }
+ const result = await dispatchD1Worker(command, payload, timeoutMs);
if (signal?.aborted) throw signal.reason; return result;
} finally { signal?.removeEventListener('abort', abort); }
}
diff --git a/test/d1-decision.mjs b/test/d1-decision.mjs
index f7d66e941..1df28c902 100644
--- a/test/d1-decision.mjs
+++ b/test/d1-decision.mjs
@@ -10,6 +10,61 @@ for (const build of ['chrome', 'firefox']) {
const cache = await import(`../src/${build}/src/providers/d1-cache.js`);
const completion = await import(`../src/${build}/src/agent/completion-verifier.js`);
const transfer = await import(`../src/${build}/src/config-transfer.js`);
+ const diagnostic = await import(`../src/${build}/src/providers/d1-diagnostic.js`);
+ const judge = await import(`../src/${build}/src/agent/systemone-judge.js`);
+ test(`${build}: fixture diagnostics require Settings, stored selected consent and normal typed evaluation`, async () => {
+ const settings = { decisionProvider: 'webgpu_d1', systemOneEnabled: true, [pin.D1_CONSENT_KEY]: pin.D1_CONSENT_VERSION };
+ const api = { runtime: { id: 'extension-id', getURL: path => 'chrome-extension://extension-id/' + path }, storage: { local: { get: async () => settings } } };
+ const sender = { id: api.runtime.id, url: api.runtime.getURL('src/ui/settings.html') + '#decision' };
+ const fixture = { state: 'Exact original state', images: ['data:image/png;base64,YQ=='], questions: { q: { type: 'noul', instructions: 'Is blue visible?' } } };
+ const client = {};
+ const response = { model: pin.D1_MODEL_ID, provider: 'webgpu_d1', answers: { q: { type: 'noul', noul: .75 } }, usage: { input_tokens: 12, output_tokens: 0 } };
+ let calls = 0;
+ const agent = { evaluateSystemOne: async (tabId, actualClient, args) => { calls++; assert.equal(tabId, null); assert.equal(actualClient, client); assert.equal(args.state, fixture.state); assert.equal(args.images, fixture.images); assert.equal(args.questions, fixture.questions); assert.equal(args.config.provider, 'webgpu_d1'); return response; } };
+ const args = { fixture, sender, api, strictSecretMode: false, agent, client };
+ assert.equal(await diagnostic.evaluateD1LocalFixture(args), response);
+ assert.equal(calls, 1);
+ for (const badSender of [{ ...sender, id: 'other-id' }, { ...sender, url: 'https://example.com/' }, { ...sender, url: api.runtime.getURL('src/ui/popup.html') }]) await assert.rejects(diagnostic.evaluateD1LocalFixture({ ...args, sender: badSender }), /only from extension Settings/);
+ await assert.rejects(diagnostic.evaluateD1LocalFixture({ ...args, strictSecretMode: true }), /Strict Secret/);
+ for (const stored of [{ ...settings, decisionProvider: 'compass' }, { ...settings, systemOneEnabled: false }, { ...settings, [pin.D1_CONSENT_KEY]: 0 }]) await assert.rejects(diagnostic.evaluateD1LocalFixture({ ...args, api: { ...api, storage: { local: { get: async () => stored } } } }), /Select, enable/);
+ assert.equal(calls, 1);
+ await assert.rejects(diagnostic.evaluateD1LocalFixture({ ...args, api: { ...api, storage: { local: { get: async () => ({ ...settings, decisionVisionMode: 'off' }) } } } }), /Enable local D1 vision/);
+ await assert.rejects(diagnostic.evaluateD1LocalFixture({ ...args, agent: { evaluateSystemOne: async () => ({ ...response, usage: { input_tokens: 12, output_tokens: 1 } }) } }), /Invalid local D1 fixture response/);
+ await assert.rejects(diagnostic.evaluateD1LocalFixture({ ...args, agent: { evaluateSystemOne: async () => ({ ...response, answers: { q: { type: 'noul', noul: 1.5 } } }) } }), /probability/);
+ });
+ test(`${build}: SW-reachable D1 modules use static imports and diagnostic branch leaves normal connection test intact`, async () => {
+ for (const path of ['providers/d1.js', 'providers/d1-diagnostic.js', 'providers/d1-host.js', 'agent/systemone-judge.js']) {
+ const source = await readFile(new URL(`../src/${build}/src/${path}`, import.meta.url), 'utf8');
+ assert.doesNotMatch(source, /\bimport\s*\(/, path);
+ }
+ const background = await readFile(new URL(`../src/${build}/src/background.js`, import.meta.url), 'utf8');
+ const branch = background.slice(background.indexOf("case 'test_system_one':"), background.indexOf("case 'test_captcha_provider_balance':"));
+ assert.match(branch, /await strictSecretModeReady/);
+ assert.match(branch, /Object\.hasOwn\(msg, 'localFixture'\)/);
+ assert.match(branch, /return \{ success: true, result \}/);
+ assert.ok(branch.indexOf('evaluateD1LocalFixture') < branch.indexOf('let config = msg.settings'));
+ assert.match(branch, /Decision connection test was inconclusive/);
+ });
+ test(`${build}: actual typed Judge dispatches a local text/image fixture without HTTP or dynamic imports`, async () => {
+ const saved = { chrome: globalThis.chrome, browser: globalThis.browser, Worker: globalThis.Worker };
+ const questions = { q: { type: 'noul', instructions: 'Is blue visible?' } };
+ const state = 'Original source state', images = ['data:image/png;base64,YQ=='];
+ const result = { model: pin.D1_MODEL_ID, provider: 'webgpu_d1', answers: { q: { type: 'noul', noul: .75 } }, usage: { input_tokens: 12, output_tokens: 0 } };
+ let sends = 0, usages = 0;
+ const receive = message => { sends++; assert.equal(message.command || message.type, 'evaluate'); assert.equal(message.payload.state, state); assert.deepEqual(message.payload.images, images); assert.deepEqual(message.payload.questions, questions); return result; };
+ const api = { storage: { local: { get: async () => ({ [pin.D1_CONSENT_KEY]: pin.D1_CONSENT_VERSION }) } }, runtime: { getURL: path => 'chrome-extension://test/' + path, sendMessage: async message => ({ ok: true, result: receive(message) }) } };
+ if (build === 'chrome') api.offscreen = { createDocument: async () => { throw new Error('Existing host must be reused'); }, hasDocument: async () => true };
+ globalThis.chrome = api; globalThis.browser = build === 'firefox' ? api : undefined;
+ globalThis.Worker = class { postMessage(message) { const response = receive(message); queueMicrotask(() => this.onmessage({ data: { id: message.id, ok: true, result: response } })); } terminate() {} };
+ try {
+ const selected = config.resolveDecisionConfig({ decisionProvider: 'webgpu_d1', systemOneEnabled: true, [pin.D1_CONSENT_KEY]: pin.D1_CONSENT_VERSION });
+ const response = await judge.createSystemOneJudge({ fetchImpl: () => { throw new Error('No cloud request'); }, maxRetries: 0 }).evaluate({ state, images, questions, config: selected, onUsage: metadata => { usages++; assert.equal(Object.hasOwn(metadata, 'answers'), false); } });
+ assert.deepEqual(response.answers, result.answers); assert.equal(response.usage.output_tokens, 0); assert.equal(sends, 1); assert.equal(usages, 1);
+ } finally {
+ const host = await import(`../src/${build}/src/providers/d1-host.js`); host.resetD1Worker();
+ globalThis.chrome = saved.chrome; globalThis.browser = saved.browser; globalThis.Worker = saved.Worker;
+ }
+ });
test(`${build}: D1 is opt-in, endpoint/key-free, zero-cost and overrides Compass only with explicit local consent`, async () => {
const stored = { decisionProvider: 'webgpu_d1', systemOneEnabled: true };
assert.equal(config.resolveDecisionConfig(stored).enabled, false);
@@ -86,6 +141,6 @@ for (const build of ['chrome', 'firefox']) {
});
}
-test('new decision runtime modules are byte-identical across Chrome and Firefox', async () => {
- for (const name of ['d1-config.js', 'd1-runtime.js', 'd1-preprocess.js', 'd1-cache.js', 'd1-worker.js', 'd1-host.js', 'd1.js']) assert.deepEqual(await readFile(new URL(`../src/chrome/src/providers/${name}`, import.meta.url)), await readFile(new URL(`../src/firefox/src/providers/${name}`, import.meta.url)), name);
+test('new shared decision runtime modules are byte-identical across Chrome and Firefox', async () => {
+ for (const name of ['d1-config.js', 'd1-runtime.js', 'd1-preprocess.js', 'd1-cache.js', 'd1-worker.js', 'd1-host.js', 'd1-diagnostic.js']) assert.deepEqual(await readFile(new URL(`../src/chrome/src/providers/${name}`, import.meta.url)), await readFile(new URL(`../src/firefox/src/providers/${name}`, import.meta.url)), name);
});