Skip to content

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

63.3 KB
Raw Blame
// Lean mode (Settings β†’ Advanced β†’ "Lean mode"; src/*/src/agent/lean-mode.js).
// A lean Act run gets a thin tool list and a short prompt, and every guard of
// class `quality` is skipped: no refusal, note, caveat, judge or forced turn.
// The hard classes, permission gates, host checks, budgets and user control
// keep their behavior. Auto turns lean mode on only for a strong model on a
// cloud provider at the Full tier. Real Agent run loops with a scripted
// provider, both builds; the recipient case runs the real content script in
// Chromium.
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFile } from 'node:fs/promises';
import { test } from 'node:test';
import vm from 'node:vm';
import { chromium } from 'playwright';
import { defaultDecide, installModelStub, stubPageTextReadings, withoutFixedChoiceAnswers } from './semantic-decision-stub.mjs';

const ROOT = fileURLToPath(new URL('../', import.meta.url));
const read = file => fs.readFileSync(path.join(ROOT, file), 'utf8');
const PAGE = 'https://owned.example.test/profile';
const TREE = 'main [ref_main]\n heading "Profile" [ref_h]\n button "Save" [ref_save]';
const area = { get: async () => ({}), set: async () => {}, remove: async () => {} };
const api = {
  storage: { local: area, session: area, onChanged: { addListener() {} } },
  runtime: { getURL: value => `extension://test/${value}`, sendMessage: async () => ({}), getManifest: () => ({ version: '0.0.0' }) },
  tabs: {
    get: async id => ({ id, url: PAGE, title: 'Profile', status: 'complete' }),
    sendMessage: async () => ({ ready: true }),
    executeScript: async () => [null],
  },
  webNavigation: { getAllFrames: async () => [{ frameId: 0, parentFrameId: -1, documentId: 'document-1', url: PAGE }] },
  scripting: { executeScript: async () => [{ result: null }] },
};
globalThis.chrome = api;
globalThis.browser = api;

const CAVEAT = 'WebBrain could not verify everything: ';
const STRONG = { name: 'lean test', model: 'claude-sonnet-4-5', config: { category: 'cloud', type: 'anthropic', model: 'claude-sonnet-4-5' } };
let nextTab = 9700;
let nextCall = 1;
const call = (id, name, args) => ({ toolCalls: [{ id: `${id}_${nextCall++}`, function: { name, arguments: JSON.stringify(args) } }] });
const names = tools => tools.map(tool => tool.function.name);

test('lean-mode.js is shared verbatim by both builds', () => {
  assert.equal(read('src/chrome/src/agent/lean-mode.js'), read('src/firefox/src/agent/lean-mode.js'));
});

test('docs/lean-mode.md says a lean run keeps its tools and names every quality guard lean mode keeps', async () => {
  const doc = read('docs/lean-mode.md');
  const { LEAN_KEPT_QUALITY_GUARDS } = await import('../src/chrome/src/agent/lean-mode.js');
  const kept = Object.keys(LEAN_KEPT_QUALITY_GUARDS);
  for (const id of kept) assert.ok(doc.includes(`| \`${id}\` |`), `the kept-guard table lists ${id}`);
  assert.match(doc, new RegExp(`except the ${kept.length} listed under`));
  assert.match(doc, /\*\*Tools\.\*\* The same tools as any Act run at the provider's tier, with the same schemas\./);
  assert.match(doc, /\*\*Prompt\.\*\* The tier's Act prompt with its usual additions, plus one note, `SYSTEM_PROMPT_LEAN_NOTE`/);
  // The comparison the doc points to is generated by its script.
  assert.match(read('docs/lean-mode-comparison.md'), /^# Lean mode comparison\n\nGenerated by `node scripts\/lean-mode-comparison\.mjs`/);
});

for (const build of ['chrome', 'firefox']) {
  const lean = await import(`../src/${build}/src/agent/lean-mode.js`);
  const policy = await import(`../src/${build}/src/agent/guard-policy.js`);
  const tools = await import(`../src/${build}/src/agent/tools.js`);
  const { Agent } = await import(`../src/${build}/src/agent/agent.js`);
  const { AnthropicProvider } = await import(`../src/${build}/src/providers/anthropic.js`);
  const { OpenAICompatibleProvider } = await import(`../src/${build}/src/providers/openai.js`);
  const { LlamaCppProvider } = await import(`../src/${build}/src/providers/llamacpp.js`);

  // A real run loop with a scripted provider; page tools and done are stubbed
  // at the lifecycle boundary, so batches, plain finals and forced turns run
  // for real. `provider` decides whether Auto resolves to lean mode.
  function runAgent(next, { setting = 'auto', provider = STRONG, strict = false, maxSteps = 8 } = {}) {
    const requests = [];
    // Model calls WebBrain makes on its own, without tools (the progress
    // ledger classifier, judges, readings), are counted apart from the
    // scripted turns and get an empty answer.
    const sideCalls = [];
    const scripted = {
      contextWindow: 128000, supportsTools: true, supportsVision: false, promptTier: 'full', ...provider,
      chat: withoutFixedChoiceAnswers(async (messages, options) => {
        if (!options?.tools?.length) {
          sideCalls.push(String(messages[0]?.content || '').slice(0, 80));
          return { content: '{}' };
        }
        requests.push({ system: String(messages[0]?.content || ''), tools: names(options?.tools || []), choice: options?.toolChoice ?? null });
        return next(requests.length, messages, options);
      }),
      async *chatStream(messages, options) {
        const response = await this.chat(messages, options);
        if (response.content) yield { type: 'text', content: response.content };
        if (response.toolCalls) yield { type: 'tool_call', content: response.toolCalls.map((item, index) => ({ ...item, index })) };
        yield { type: 'done' };
      },
    };
    const agent = new Agent({ getActive: () => scripted, getProvider: () => scripted, getVisionProvider: async () => null });
    agent.leanMode = setting;
    agent.strictGuards = strict;
    Object.assign(agent, {
      _hydrate: async () => {}, _persist: () => {}, _persistNow: async () => ({ ok: true }), _persistSubmittedTurn: async () => {},
      _startTraceRun: async () => null, _endTraceRun: async () => {}, _beginReadCompleteness: async () => null,
      _manageContext: async () => {}, _checkCostAllowance: async () => null, _recordCostUsage: async () => null,
      _currentUrl: async () => PAGE, _getTabUrlTitle: async () => ({ tabUrl: PAGE, tabTitle: 'Profile' }),
      _maybeEmitAskModeHandoff: async () => {}, _maybeReinjectAdapter: async () => {},
      _enrichUserMessageWithCurrentPage: async (_tab, _messages, content) => ({ role: 'user', content }),
      _plannerMode: () => 'off',
      _runPlannerIntentGate: async () => ({ proceed: true, requestKind: 'execute', requiresStateChange: false, requiresSubmission: false }),
      _skipPermissionGate: true, _ensureGateSetting: async () => true, _adoptLiveSocialPublishWorkflow: async () => false,
      _isFormValidationCandidate: () => false, _shouldAutoScreenshot: () => false, _pageFeedbackIdleMs: 0,
    });
    for (const name of ['_chromeProtectedPageFailure', '_captchaMutationPreflight', '_workflowPreSubmitDispatchBlock',
      '_messageRecipientGuardBlock', '_detectLikelySubmitAction', '_socialPublicationPreSubmitBlock']) agent[name] = async () => null;
    agent.maxSteps = maxSteps;
    const dispatched = [];
    agent._executeToolWithLifecycle = async (_tab, name, args) => {
      dispatched.push(name);
      if (name === 'done') return { done: true, success: true, summary: args.summary, outcome: args.outcome };
      if (name === 'get_accessibility_tree') return { success: true, pageContent: TREE };
      return { success: true, dispatched: true };
    };
    return { agent, requests, dispatched, sideCalls };
  }
  const runLoop = (agent, tab, streaming, text, options = {}) => streaming
    ? agent.processMessageStream(tab, text, () => {}, 'act', { detachedRequestId: `lean-${tab}`, ...options })
    : agent.processMessage(tab, text, () => {}, 'act', [], { detachedRequestId: `lean-${tab}`, ...options });
  // A lean run state without a run loop, for handler-level checks.
  const leanState = (agent, tab) => agent._leanRuns.set(tab, { active: true, setting: 'on', reason: 'setting_on' });

  // ── Auto resolution ─────────────────────────────────────────────────────
  test(`${build}: Auto turns lean mode on only for strong models on cloud providers at the Full tier`, () => {
    const cases = [
      // [provider, expected active, expected reason]
      [{ category: 'cloud', model: 'claude-sonnet-4-5', promptTier: 'full' }, true, 'strong_cloud_model'],
      [{ category: 'cloud', model: 'claude-opus-4-1-20250805', promptTier: 'full' }, true, 'strong_cloud_model'],
      [{ category: 'cloud', model: 'claude-opus-5-5', promptTier: 'full' }, true, 'strong_cloud_model'],
      [{ category: 'cloud', model: 'claude-fable-5-1', promptTier: 'full' }, true, 'strong_cloud_model'],
      [{ category: 'cloud', model: 'claude-haiku-5-5', promptTier: 'full' }, false, 'model_not_listed'],
      [{ category: 'cloud', model: 'gpt-5', promptTier: 'full' }, true, 'strong_cloud_model'],
      [{ category: 'cloud', model: 'gpt-6.1-sol', promptTier: 'full' }, true, 'strong_cloud_model'],
      [{ category: 'cloud', model: 'o3', promptTier: 'full' }, true, 'strong_cloud_model'],
      [{ category: 'cloud', model: 'o4-mini', promptTier: 'full' }, true, 'strong_cloud_model'],
      [{ category: 'cloud', model: 'gemini-2.5-pro', promptTier: 'full' }, true, 'strong_cloud_model'],
      [{ category: 'cloud', model: 'gemini-3.1-pro', promptTier: 'full' }, true, 'strong_cloud_model'],
      [{ category: 'cloud', model: 'grok-4', promptTier: 'full' }, true, 'strong_cloud_model'],
      [{ category: 'router', model: 'anthropic/claude-sonnet-4.5', promptTier: 'full' }, true, 'strong_cloud_model'],
      // The provider's tier vetoes Auto.
      [{ category: 'router', model: 'anthropic/claude-sonnet-4.5', promptTier: 'mid' }, false, 'prompt_tier'],
      [{ category: 'router', model: 'openai/gpt-5', promptTier: 'compact' }, false, 'prompt_tier'],
      // Not on the strong list.
      [{ category: 'cloud', model: 'claude-3-7-sonnet-20250219', promptTier: 'full' }, false, 'model_not_listed'],
      [{ category: 'cloud', model: 'claude-haiku-4-5-20251001', promptTier: 'full' }, false, 'model_not_listed'],
      [{ category: 'cloud', model: 'gpt-4o', promptTier: 'full' }, false, 'model_not_listed'],
      [{ category: 'cloud', model: 'gpt-5-nano', promptTier: 'full' }, false, 'model_not_listed'],
      [{ category: 'cloud', model: 'gemini-2.5-flash', promptTier: 'full' }, false, 'model_not_listed'],
      [{ category: 'cloud', model: 'webbrain-cloud 1.0', promptTier: 'full' }, false, 'model_not_listed'],
      // Local providers never, whatever they serve.
      [{ category: 'local', type: 'openai', providerId: 'ollama', model: 'gpt-oss:120b', promptTier: 'mid' }, false, 'local_provider'],
      [{ category: 'local', type: 'llamacpp', model: 'claude-sonnet-4-5', promptTier: 'full' }, false, 'local_provider'],
      [{ type: 'webgpu', model: 'gpt-5' }, false, 'local_provider'],
      [{ providerId: 'lmstudio', model: 'gpt-5' }, false, 'local_provider'],
      [{ providerId: 'ollama', model: 'gpt-5', promptTier: 'full' }, false, 'local_provider'],
      // An explicit category decides before the type and id lists: Ollama
      // Cloud is a cloud provider although its id starts with "ollama-".
      [{ category: 'cloud', type: 'openai', providerId: 'ollama-cloud', model: 'gemini-3-pro-preview', promptTier: 'full' }, true, 'strong_cloud_model'],
      [{ category: 'local', providerId: 'custom-cloud', model: 'gpt-5', promptTier: 'full' }, false, 'local_provider'],
      // A blank configured model is unset: the provider's own model decides.
      [{ config: { category: 'cloud', model: '' }, model: 'claude-sonnet-5' }, true, 'strong_cloud_model'],
      [{ config: { category: 'cloud', model: '   ' }, model: 'gpt-5' }, true, 'strong_cloud_model'],
      [{ category: 'cloud', model: '', promptTier: 'full' }, false, 'model_not_listed'],
      // A provider of unknown category is not assumed to be a cloud one.
      [{ model: 'gpt-5', promptTier: 'full' }, false, 'unknown_provider'],
    ];
    for (const [provider, active, reason] of cases) {
      const resolved = lean.resolveLeanMode('auto', { mode: 'act', provider });
      assert.equal(resolved.active, active, `${JSON.stringify(provider)}: ${JSON.stringify(resolved)}`);
      assert.equal(resolved.reason, reason, JSON.stringify(provider));
    }
    // On and Off are the user's choice; Ask and Dev are never lean.
    assert.equal(lean.resolveLeanMode('on', { provider: { category: 'local', model: 'qwen3:4b', promptTier: 'compact' } }).active, true);
    assert.equal(lean.resolveLeanMode('off', { provider: { category: 'cloud', model: 'gpt-5', promptTier: 'full' } }).active, false);
    for (const mode of ['ask', 'dev']) {
      assert.equal(lean.resolveLeanMode('on', { mode, provider: { category: 'cloud', model: 'gpt-5' } }).active, false, mode);
    }
    assert.equal(lean.normalizeLeanModeSetting('ON'), 'on');
    assert.equal(lean.normalizeLeanModeSetting('maybe'), 'auto');
    assert.equal(lean.normalizeLeanModeSetting(undefined), 'auto');
  });

  test(`${build}: Auto reads real provider instances: category, tier and model`, () => {
    const anthropic = new AnthropicProvider({ type: 'anthropic', category: 'cloud', model: 'claude-opus-4-1', apiKey: 'test' });
    const router = new OpenAICompatibleProvider({ type: 'openai', category: 'router', model: 'anthropic/claude-sonnet-4.5', apiKey: 'test', baseUrl: 'https://openrouter.ai/api/v1' });
    const routerMid = new OpenAICompatibleProvider({ type: 'openai', category: 'router', model: 'anthropic/claude-sonnet-4.5', promptTier: 'mid', apiKey: 'test', baseUrl: 'https://openrouter.ai/api/v1' });
    const llama = new LlamaCppProvider({ type: 'llamacpp', category: 'local', model: 'gpt-5', baseUrl: 'http://localhost:8080' });
    assert.equal(lean.resolveLeanMode('auto', { provider: anthropic }).active, true);
    assert.equal(lean.resolveLeanMode('auto', { provider: router }).active, true);
    assert.equal(lean.resolveLeanMode('auto', { provider: routerMid }).reason, 'prompt_tier');
    assert.equal(lean.resolveLeanMode('auto', { provider: llama }).reason, 'local_provider');
    // The agent resolves for the tab's run mode and provider.
    const agent = new Agent({ getActive: () => anthropic });
    const tab = nextTab++;
    agent._runModeOverrides.set(tab, 'act');
    assert.equal(agent._leanModeActive(tab), true);
    agent._runModeOverrides.set(tab, 'ask');
    assert.equal(agent._leanModeActive(tab), false, 'Ask keeps its behavior');
    agent._runModeOverrides.set(tab, 'dev');
    assert.equal(agent._leanModeActive(tab), false, 'Dev keeps its behavior');
    agent._runModeOverrides.set(tab, 'act');
    agent.leanMode = 'off';
    assert.equal(agent._leanModeActive(tab, anthropic), false);
    agent.leanMode = 'on';
    assert.equal(agent._leanModeActive(tab, llama), true);
  });

  test(`${build}: Auto follows the built-in provider's category and the model it actually calls`, async () => {
    const { ProviderManager } = await import(`../src/${build}/src/providers/manager.js`);
    const manager = new ProviderManager();
    const defaults = manager._defaultConfigs();
    // Built the way the extension builds them, from the shipped defaults.
    const built = (id, overrides) => manager._createProvider(id, { ...defaults[id], ...overrides });
    // Ollama Cloud is category 'cloud'; a local Ollama stays local whatever
    // model it serves.
    assert.equal(defaults['ollama-cloud'].category, 'cloud');
    assert.deepEqual(lean.resolveLeanMode('auto', { provider: built('ollama-cloud', { model: 'gemini-3-pro-preview', apiKey: 'test' }) }),
      { active: true, setting: 'auto', reason: 'strong_cloud_model', family: 'gemini' });
    assert.equal(lean.resolveLeanMode('auto', { provider: built('ollama', { model: 'gpt-5' }) }).reason, 'local_provider');
    // A cleared model setting means the provider's default, which for
    // Anthropic is claude-sonnet-5.
    const anthropic = built('anthropic', { model: '', apiKey: 'test' });
    assert.equal(anthropic.model, 'claude-sonnet-5');
    assert.deepEqual(lean.resolveLeanMode('auto', { provider: anthropic }),
      { active: true, setting: 'auto', reason: 'strong_cloud_model', family: 'claude' });
    // Auto agrees with ProviderManager.categoryFor on every built-in, and a
    // built-in whose default URL is on this machine is local.
    for (const [id, config] of Object.entries(defaults)) {
      const category = ProviderManager.categoryFor(id, config);
      const info = lean.leanProviderInfo({ config: { ...config, category, _providerId: id } });
      assert.equal(info.local, category === 'local', `${id} is ${category}`);
      assert.equal(info.cloud, category !== 'local', `${id} is ${category}`);
      if (/^https?:\/\/(?:localhost|127\.0\.0\.1|\[::1\])[:/]/.test(String(config.baseUrl || ''))) assert.ok(info.local, `${id} runs locally`);
    }
  });

  // ── Tool list and prompt ────────────────────────────────────────────────
  test(`${build}: a lean run gets its tier's tools: the tool list does not depend on lean mode`, () => {
    // getToolsForMode has no lean option: a lean run is offered what any run
    // of its tier is, the download and upload tools included (from a trace:
    // a lean run found release assets but had no way to download them).
    assert.equal(tools.LEAN_TOOL_NAMES, undefined, 'no separate lean tool list');
    assert.equal(tools.LEAN_NATIVE_CHAT_TOOL_NAMES, undefined);
    const full = names(tools.getToolsForMode('act', { tier: 'full', webMcpAvailable: true, researchEscalationEnabled: true }));
    for (const name of ['download_files', 'list_downloads', 'upload_file', 'fetch_url', 'browser_batch', 'scratchpad_write',
      'progress_update', 'chat_observe', 'chat_send', 'delegate_research']) {
      assert.ok(full.includes(name), `the Full tier offers ${name}`);
    }
  });

  test(`${build}: the lean note asks the model to check its own work, and there is no separate lean prompt`, () => {
    assert.equal(tools.SYSTEM_PROMPT_ACT_LEAN, undefined, 'a lean run uses its tier\'s Act prompt');
    const note = tools.SYSTEM_PROMPT_LEAN_NOTE;
    assert.match(note, /^LEAN MODE: WebBrain does not re-check your typing or your result for you\./);
    assert.match(note, /read the page again and compare what you see with the request before calling done/);
    assert.ok(note.length < 400, 'one short note');
  });

  test(`${build}: the system prompt follows the run: a lean run gets the Off prompt plus the note, with every tool's guidance`, () => {
    const agent = new Agent({ getActive: () => ({ ...STRONG, promptTier: 'full' }) });
    agent.captchaSolverEnabled = true;
    agent.captchaProviderIds = ['capsolver'];
    agent.webMcpEnabled = true;
    agent.researchEscalationEnabled = true;
    const tab = nextTab++;
    agent._runModeOverrides.set(tab, 'act');
    agent._beginLeanRun(tab, 'act');
    assert.equal(agent._leanRunActive(tab), true, 'Auto resolves to lean for the strong cloud model');
    const leanPrompt = agent._buildSystemPrompt('act', tab);
    assert.equal(agent._captchaToolsAvailable(tab), true, 'a lean run keeps the CAPTCHA tools of its tier');
    agent.leanMode = 'off';
    agent._beginLeanRun(tab, 'act');
    const fullPrompt = agent._buildSystemPrompt('act', tab);
    assert.ok(fullPrompt.startsWith(tools.SYSTEM_PROMPT_ACT), 'Off keeps the Full prompt');
    assert.ok(leanPrompt.startsWith(`${tools.SYSTEM_PROMPT_ACT}`), 'lean keeps it too');
    assert.equal(leanPrompt.split(tools.SYSTEM_PROMPT_LEAN_NOTE).length, 2, 'the note is added once');
    assert.equal(leanPrompt.replace(`\n\n${tools.SYSTEM_PROMPT_LEAN_NOTE}`, ''), fullPrompt, 'the lean prompt is the Off prompt plus the note');
    assert.doesNotMatch(fullPrompt, /LEAN MODE:/);
    for (const section of [/BATCHING:/, /ChatGPT/]) {
      assert.match(fullPrompt, section);
      assert.match(leanPrompt, section, `a lean run keeps ${section}`);
    }
    if (build === 'chrome') assert.match(leanPrompt, /WEBMCP/, 'a lean run keeps the WebMCP guidance');
    assert.equal(agent._buildSystemPrompt('ask', tab).startsWith(tools.SYSTEM_PROMPT_ASK), true);
  });

  // ── Guard helpers ───────────────────────────────────────────────────────
  test(`${build}: in a lean run every quality guard is skipped and every other class keeps its outcome`, () => {
    for (const strict of [false, true]) {
      const agent = new Agent({ getActive: () => STRONG });
      agent.strictGuards = strict;
      const tab = nextTab++, other = nextTab++;
      leanState(agent, tab);
      for (const [id, entry] of Object.entries(policy.GUARDS)) {
        for (const decided of [true, false]) {
          const plain = policy.guardOutcome(id, { decided, strict });
          const skipped = entry.class === 'quality' && !Object.hasOwn(lean.LEAN_KEPT_QUALITY_GUARDS, id);
          assert.equal(agent._guardOutcome(id, { decided, tabId: tab }), skipped ? 'skip' : plain, `${id} decided=${decided} strict=${strict}`);
          // Another tab's run and a guard that names no run are unaffected.
          assert.equal(agent._guardOutcome(id, { decided, tabId: other }), plain, `${id}: other tab`);
          assert.equal(agent._guardOutcome(id, { decided }), plain, `${id}: no tab`);
        }
      }
    }
    // Every kept id is a registered quality guard, with a reason.
    for (const [id, reason] of Object.entries(lean.LEAN_KEPT_QUALITY_GUARDS)) {
      assert.equal(policy.guardEntry(id)?.class, 'quality', id);
      assert.ok(reason.length > 20, id);
    }
  });

  test(`${build}: a skipped guard lets the action through with no note and the completion with no caveat`, () => {
    const agent = new Agent({ getActive: () => STRONG });
    agent.strictGuards = true; // Lean mode ignores Strict checks.
    const tab = nextTab++;
    leanState(agent, tab);
    const advisories = [];
    assert.equal(agent._guardAdvisesFor(advisories, 'text-write-readback-unverified', 'note', { decided: false, tabId: tab }), true);
    // A list the run tagged names its tab by itself.
    const tagged = agent._trackGuardAdvisories([], tab);
    assert.equal(agent._guardAdvisesFor(tagged, 'jev-target-changed', 'note'), true);
    assert.deepEqual([...advisories, ...tagged], [], 'no note');
    for (let attempt = 0; attempt < 3; attempt++) {
      assert.equal(agent._guardRefusesOnce(tab, 'done-verification-debt', { decided: false, caveat: 'Not checked.' }), false);
    }
    assert.equal(agent._guardCaveatLine(tab), '', 'no caveat');
    assert.equal(agent._completionRecoveryPolicy(tab, tools.getToolsForMode('act', { tier: 'full' }), { verification: true }), null,
      'no forced verification turn');
    // A hard guard is untouched.
    assert.equal(agent._guardAdvisesFor([], 'recipient-mismatch-confirm-card', 'note', { tabId: tab }), false);
    assert.equal(agent._guardRefusesOnce(tab, 'native-chat-receipt-not-durable', {}), true);
  });

  // ── Run loops ───────────────────────────────────────────────────────────
  for (const streaming of [false, true]) {
    const label = `${build} ${streaming ? 'stream' : 'chat'}`;
    test(`${label}: a lean run takes an unobserved done at once: no note, caveat or forced turn`, async () => {
      for (const setting of ['auto', 'off']) {
        const tab = nextTab++;
        const h = runAgent(n => (n === 1 ? call('save', 'click_ax', { ref_id: 'ref_save' })
          : call('done', 'done', { summary: 'Saved the profile.', outcome: 'success' })), { setting });
        let ended = null;
        const result = await runLoop(h.agent, tab, streaming, 'Save the owned profile.', { onRunFinished: (_status, details) => { ended = details; } });
        if (setting === 'auto') {
          assert.equal(h.requests.length, 2, `${label}: the first done is accepted`);
          assert.equal(result, 'Saved the profile.', `${label}: no caveat`);
          assert.ok(h.requests.every(request => request.tools.length > 40), `${label}: the Full tool list on every turn`);
          assert.ok(h.requests.every(request => request.tools.includes('download_files') && request.tools.includes('upload_file')), `${label}: downloads and uploads`);
          assert.ok(h.requests.every(request => request.system.startsWith(tools.SYSTEM_PROMPT_ACT)
            && request.system.includes(tools.SYSTEM_PROMPT_LEAN_NOTE)), `${label}: the Full prompt with the lean note`);
          assert.ok(h.requests.every(request => request.choice === null), `${label}: no forced turn`);
          assert.equal(ended?.leanMode, true, 'the run state records lean mode');
          assert.deepEqual(h.sideCalls, [], `${label}: no side calls (no progress-ledger classifier, judge or reading)`);
        } else {
          assert.equal(h.requests.length, 3, `${label}: Off keeps the one refusal of an unobserved done`);
          assert.equal(result, `Saved the profile.\n\n${CAVEAT}The page was not checked again after the last action.`);
          assert.ok(h.requests[0].tools.length > 40, `${label}: Off keeps the Full tool list`);
          assert.ok(h.sideCalls.some(text => /progress ledger/.test(text)), `${label}: Off still classifies the task for the progress ledger`);
          assert.equal(ended?.leanMode, false);
        }
        assert.equal(h.agent._leanRuns.has(tab), false, 'the run state ends with the run');
      }
    });

    test(`${label}: a lean run accepts a plain final answer after an action`, async () => {
      const tab = nextTab++;
      const h = runAgent(n => (n === 1 ? call('save', 'click_ax', { ref_id: 'ref_save' })
        : n === 2 ? call('look', 'get_accessibility_tree', { filter: 'visible' }) : { content: 'Saved the profile.' }));
      let chatReads = 0;
      h.agent._freshNativeChatTerminalEvidence = async () => { chatReads++; return null; };
      const result = await runLoop(h.agent, tab, streaming, 'Save the owned profile.');
      assert.equal(h.requests.length, 3, `${label}: no done-protocol nudge`);
      assert.equal(result, 'Saved the profile.');
      assert.equal(chatReads, 0, `${label}: no chat send, so no conversation read at the end`);
    });

    // A plain final is the model's own success claim too: after a chat send
    // it gets the lean done's receipt check (native-chat-receipt-not-durable).
    const chatSendThenFinal = (getAgent, tab, { contract = false } = {}) => n => {
      if (n === 1) return call('send', 'click_ax', { ref_id: 'ref_save' });
      if (n === 2) {
        const agent = getAgent();
        // What the chat send's delivery binding records for the run.
        agent.completionInvariants.set(tab, { ...(agent.completionInvariants.get(tab) || {}),
          lastAction: { name: 'click_ax', sequence: 7, chatDeliveryBinding: { token: 'sent', threadKey: 'dom:chat:1' } } });
        if (contract) {
          Object.assign(agent._planExecutionGuards.get(tab) || {}, { enabled: true, requiresSubmission: true, requiresStateChange: true });
          if (!agent._planExecutionGuards.get(tab)) {
            agent._planExecutionGuards.set(tab, { enabled: true, requiresSubmission: true, requiresStateChange: true, taskKey: 'tk_reply' });
          }
        }
      }
      return { content: 'Sent the reply.' };
    };
    const runCollecting = async (agent, tab, text) => {
      const events = [];
      const finished = [];
      const onUpdate = (type, data) => events.push({ type, data });
      const options = { detachedRequestId: `lean-${tab}`, onRunFinished: status => finished.push(status) };
      const result = streaming
        ? await agent.processMessageStream(tab, text, onUpdate, 'act', options)
        : await agent.processMessage(tab, text, onUpdate, 'act', [], options);
      return { result, events, finished };
    };

    test(`${label}: a lean plain final after a chat send confirms the delivery and saves its receipt first`, async () => {
      const tab = nextTab++;
      let h = null;
      h = runAgent(chatSendThenFinal(() => h.agent, tab));
      let reads = 0, savedAfterRead = 0;
      h.agent._freshNativeChatTerminalEvidence = async () => { reads++; return { receipt: 'delivered' }; };
      h.agent._persistNow = async () => { if (reads) savedAfterRead++; return { ok: true }; };
      const { result, events } = await runCollecting(h.agent, tab, 'Reply in the owned chat.');
      assert.equal(result, 'Sent the reply.', `${label}: a confirmed, saved send ends as the model said`);
      assert.equal(h.requests.length, 2, `${label}: no extra turn`);
      assert.equal(reads, 1, `${label}: the conversation was read once at the end`);
      assert.ok(savedAfterRead >= 1, `${label}: the receipt was saved before the run ended`);
      assert.equal(events.some(event => event.type === 'run_status' && event.data?.status === 'partial'), false);
    });

    test(`${label}: a lean plain final whose chat receipt cannot be saved never ends as a success`, async () => {
      const tab = nextTab++;
      let h = null;
      h = runAgent(chatSendThenFinal(() => h.agent, tab));
      let reads = 0;
      h.agent._freshNativeChatTerminalEvidence = async () => { reads++; return { receipt: 'delivered' }; };
      h.agent._persistNow = async () => (reads ? false : { ok: true });
      const { result, events, finished } = await runCollecting(h.agent, tab, 'Reply in the owned chat.');
      const refusals = h.agent.conversations.get(tab).filter(message => message.webbrainAppOwnedKind === 'chat_receipt');
      assert.equal(refusals.length, 2, `${label}: refused twice with the receipt instruction`);
      assert.match(String(refusals[0].content), /delivery receipt could not be saved/);
      assert.match(String(refusals[0].content), /do not send the message again/);
      assert.equal(h.requests.length, 4, `${label}: the third plain final ends the run instead of looping`);
      assert.notEqual(result, 'Sent the reply.', `${label}: not accepted as a plain success`);
      assert.match(String(result), /^Sent the reply\.\n\nWebBrain could not save the record that the message was delivered/);
      assert.ok(events.some(event => event.type === 'run_status' && event.data?.status === 'partial'), `${label}: run_status partial`);
      assert.equal(finished.at(-1), 'partial', `${label}: the run finishes as partial`);
    });

    test(`${label}: a lean plain final over a send the conversation cannot confirm is refused once, then ends`, async () => {
      const tab = nextTab++;
      let h = null;
      h = runAgent(chatSendThenFinal(() => h.agent, tab, { contract: true }));
      let reads = 0;
      h.agent._freshNativeChatTerminalEvidence = async () => { reads++; return null; };
      const { result } = await runCollecting(h.agent, tab, 'Reply in the owned chat.');
      const refusals = h.agent.conversations.get(tab).filter(message => message.webbrainAppOwnedKind === 'chat_receipt');
      assert.equal(refusals.length, 1, `${label}: one refusal for the send`);
      assert.match(String(refusals[0].content), /could not confirm in the conversation/);
      assert.equal(h.requests.length, 3);
      assert.equal(result, 'Sent the reply.', `${label}: once per send, the next plain final ends the run`);
      assert.equal(reads, 2, `${label}: read again at the second final`);
    });
  }

  // From a trace: a lean run on WhatsApp Web had no chat_observe, which the
  // recipient guard's contact check and latest-message repairs point to, and
  // the plan's chat_observe/chat_send steps could not run. A lean run now gets
  // its tier's tools on every page, the chat tools included.
  test(`${build}: a lean run on a native chat client is offered the chat tools its recipient guard uses`, async () => {
    for (const streaming of [false, true]) {
      for (const url of ['https://web.whatsapp.com/', 'https://web.telegram.org/k/', PAGE]) {
        const tab = nextTab++;
        const h = runAgent(() => call('finish', 'done', { summary: 'Read.', outcome: 'success' }), { setting: 'on' });
        h.agent._currentUrl = async () => url;
        await runLoop(h.agent, tab, streaming, 'Read the open chat.');
        assert.ok(h.requests.length > 0, 'the run made a model request');
        for (const name of ['chat_observe', 'chat_send', 'click_ax']) {
          assert.ok(h.requests[0].tools.includes(name), `${streaming ? 'streaming' : 'plain'} ${url}: ${name}`);
        }
      }
    }
  });

  test(`${build}: a lean run still refuses a tool it does not offer, and still stops at the step limit`, async () => {
    const tab = nextTab++;
    // execute_js is a Dev tool: an Act run, lean or not, does not offer it.
    const h = runAgent(n => (n === 1 ? call('script', 'execute_js', { code: 'document.title' }) : call('save', 'click_ax', { ref_id: 'ref_save' })),
      { maxSteps: 3 });
    const events = [];
    const result = await h.agent.processMessage(tab, 'Save the owned profile.', type => events.push(type), 'act', [], { detachedRequestId: `lean-${tab}` });
    assert.equal(h.requests[0].tools.includes('execute_js'), false, 'not offered in Act');
    assert.equal(h.dispatched.includes('execute_js'), false, 'execute_js was not run');
    const refused = h.agent.conversations.get(tab).find(message => message.role === 'tool' && /execute_js is not available/.test(String(message.content)));
    assert.ok(refused, 'tool-not-advertised (a permission gate) refused it');
    assert.ok(events.includes('max_steps_reached'), 'the step budget still ends the run');
    // Three steps, then the budget's own done-only delivery turn.
    assert.equal(h.requests.length, 4);
    assert.deepEqual(h.requests[3].tools, ['done'], 'the step-limit handoff stays');
    assert.match(String(result), /./);
  });

  test(`${build}: a lean run still asks the site permission gate before acting`, async () => {
    const tab = nextTab++;
    const h = runAgent(n => (n === 1 ? call('save', 'click_ax', { ref_id: 'ref_save' }) : call('done', 'done', { summary: 'Could not save.', outcome: 'failed' })));
    h.agent._skipPermissionGate = false;
    h.agent._ensureGateSetting = async () => false;
    h.agent.permissions.check = () => ({ allowed: false, needsPrompt: false }); // a standing deny for this site
    await h.agent.processMessage(tab, 'Save the owned profile.', () => {}, 'act', [], { detachedRequestId: `lean-${tab}` });
    assert.equal(h.dispatched.includes('click_ax'), false, 'the denied click was not dispatched');
    assert.ok(h.agent.conversations.get(tab).some(message => message.role === 'tool' && /denied permission/.test(String(message.content))));
  });

  // ── Handlers ────────────────────────────────────────────────────────────
  test(`${build}: a lean done takes no screenshot and asks no judge; a chat receipt that cannot be saved still refuses`, async () => {
    const agent = new Agent({ getActive: () => STRONG });
    const tab = nextTab++;
    agent.conversationModes.set(tab, 'act');
    agent._runModeOverrides.set(tab, 'act');
    leanState(agent, tab);
    let probes = 0, judged = 0;
    agent._captureViewportProbe = async () => { probes++; return { url: PAGE }; };
    agent.evaluateSystemOne = async () => { judged++; return { answers: {} }; };
    agent._semanticDecision = async () => { judged++; return { choice: null }; };
    const done = await agent.executeTool(tab, 'done', { summary: 'Saved.', outcome: 'success' });
    assert.deepEqual(done, { done: true, summary: 'Saved.', outcome: 'success' });
    assert.equal(probes + judged, 0, 'no probe, screenshot or judge');
    // recipient class: a delivered chat message must have its receipt saved,
    // every time, as in a full run.
    agent.completionInvariants.set(tab, { lastAction: { name: 'click_ax', sequence: 3, chatDeliveryBinding: { token: 'sent', threadKey: 'dom:chat:1' } } });
    agent._freshNativeChatTerminalEvidence = async () => ({ receipt: 'delivered' });
    agent._persistNow = async () => false;
    for (let attempt = 0; attempt < 3; attempt++) {
      const refused = await agent.executeTool(tab, 'done', { summary: 'Sent.', outcome: 'success' });
      assert.equal(refused.blockedDone, true, JSON.stringify(refused));
      assert.match(refused.error, /delivery receipt could not be saved/);
    }
  });

  test(`${build}: a lean done over a send the conversation cannot confirm is refused once, then ends`, async () => {
    const agent = new Agent({ getActive: () => STRONG });
    const tab = nextTab++;
    agent.conversationModes.set(tab, 'act');
    agent._runModeOverrides.set(tab, 'act');
    leanState(agent, tab);
    let reads = 0;
    agent._freshNativeChatTerminalEvidence = async () => { reads++; return null; };
    agent._persistNow = async () => assert.fail('nothing to save without confirmed delivery');
    agent.completionInvariants.set(tab, { lastAction: { name: 'press_keys', sequence: 4, chatDeliveryBinding: { token: 'sent', threadKey: 'dom:chat:1' } } });
    // Outside a plan contract that requires the send, there is no receipt
    // to confirm.
    assert.deepEqual(await agent.executeTool(tab, 'done', { summary: 'Replied.', outcome: 'success' }),
      { done: true, summary: 'Replied.', outcome: 'success' });
    agent._planExecutionGuards.set(tab, { enabled: true, requiresSubmission: true, requiresStateChange: true, taskKey: 'tk_reply' });
    const refused = await agent.executeTool(tab, 'done', { summary: 'Replied.', outcome: 'success' });
    assert.equal(refused.blockedDone, true, JSON.stringify(refused));
    assert.equal(refused.chatDeliveryUnconfirmed, true);
    assert.match(refused.error, /could not confirm in the conversation/);
    assert.match(refused.error, /Do not send the message again/);
    assert.match(refused.error, /outcome "partial"/);
    // An honest ending is never refused.
    for (const outcome of ['partial', 'failed']) {
      assert.deepEqual(await agent.executeTool(tab, 'done', { summary: 'Sent; not confirmed.', outcome }),
        { done: true, summary: 'Sent; not confirmed.', outcome });
    }
    // Once per send: the next success ends the run.
    assert.deepEqual(await agent.executeTool(tab, 'done', { summary: 'Replied.', outcome: 'success' }),
      { done: true, summary: 'Replied.', outcome: 'success' });
    // A later send in the same run is checked again.
    agent.completionInvariants.set(tab, { lastAction: { name: 'press_keys', sequence: 9, chatDeliveryBinding: { token: 'sent-2', threadKey: 'dom:chat:1' } } });
    assert.equal((await agent.executeTool(tab, 'done', { summary: 'Replied again.', outcome: 'success' })).chatDeliveryUnconfirmed, true);
    assert.ok(reads >= 3, 'the conversation is read again at every success');
  });

  test(`${build}: a lean done after a site's own message send records its delivery as the job's evidence`, async () => {
    // An X direct message sent through the adapter's send-message job has no
    // native conversation receipt; its receipt is the job's terminal
    // evidence, which a continued run checks before it would send again.
    const xUrl = 'https://x.com/i/chat/123-456';
    const fixture = ({ delivered = true, saves = true } = {}) => {
      const agent = new Agent({ getActive: () => STRONG });
      const tab = nextTab++;
      agent.conversationModes.set(tab, 'act');
      agent._runModeOverrides.set(tab, 'act');
      leanState(agent, tab);
      agent.conversations.set(tab, [{ role: 'system', content: 'sys' }, { role: 'user', content: 'Send Alex: Hello there' }]);
      const siteWorkflow = agent._resolvePlannerSiteWorkflow(xUrl, { request_kind: 'execute', requires_submission: true,
        messaging: { target_kind: 'named', recipients: ['@altryne'] } });
      assert.equal(siteWorkflow?.job?.id, 'send-message');
      const guard = agent._startPlanExecutionGuard(tab, 'act', { requestKind: 'execute', requiresStateChange: true,
        requiresSubmission: true, messaging: { target_kind: 'named', recipients: ['@altryne'] }, siteWorkflow });
      guard.workflowMetadataRequirements = [{ field: 'body', value: 'Hello there' }];
      guard.workflowMetadataRequirementsResolved = true;
      const workflowBinding = agent._workflowSubmitBindingForAttempt(tab, xUrl, {
        messageRecipientGuardRequired: true, messageRecipientDispatchBinding: { token: 'x-send' },
        messageRecipientBody: 'Hello there', messageRecipientBodyBaselineCount: 0,
        messageRecipientExistingMessageIds: ['message-prior'] });
      assert.ok(workflowBinding, 'the send is bound to the job');
      agent._completionSubmitStates.set(tab, { dispatched: true, observedAfterSubmit: true, originatingUrl: xUrl,
        currentUrl: xUrl, taskKey: agent._progressTaskKeyHash(tab), runToken: 'run-1', actionSequence: 2, workflowBinding });
      agent._currentUrl = async () => xUrl;
      agent._liveRegionMessages = async () => [];
      const reads = [];
      agent._messageRecipientGuardProbe = async (_tab, params) => {
        reads.push(params);
        return { success: true, conclusive: true, composerEmpty: true,
          strongRecipientCandidates: [{ identity: '@altryne', role: 'to' }],
          existingMessageIds: delivered ? ['message-prior', 'message-new'] : ['message-prior'],
          matchingOutgoingMessageIds: delivered ? ['message-new'] : [],
          matchingOutgoingMessageCount: delivered ? 1 : 0 };
      };
      let persisted = 0;
      agent._persistNow = async () => { persisted++; return saves ? { ok: true } : false; };
      return { agent, tab, guard, reads, persisted: () => persisted };
    };

    // Delivered: the job's evidence is recorded and saved, and the run ends.
    const sent = fixture();
    assert.deepEqual(await sent.agent.executeTool(sent.tab, 'done', { summary: 'Sent.', outcome: 'success' }),
      { done: true, summary: 'Sent.', outcome: 'success' });
    assert.equal(sent.reads.length, 1, 'the conversation was read');
    assert.equal(sent.reads[0].expectedMessageBody, 'Hello there');
    assert.equal(sent.guard.workflowTerminalEvidence?.verificationKind, 'message_sent');
    assert.equal(sent.guard.workflowTerminalEvidence?.job, 'send-message');
    assert.equal(sent.persisted(), 1, 'the receipt was saved before the run ended');
    assert.equal(sent.agent._workflowTerminalEvidenceMatchesState(sent.guard, sent.guard.workflowTerminalEvidence), true,
      'a continued run sees the send as done');

    // Delivered but not saved: refused every time.
    const unsaved = fixture({ saves: false });
    for (let attempt = 0; attempt < 3; attempt++) {
      const refused = await unsaved.agent.executeTool(unsaved.tab, 'done', { summary: 'Sent.', outcome: 'success' });
      assert.equal(refused.blockedDone, true, JSON.stringify(refused));
      assert.match(refused.error, /delivery receipt could not be saved/);
    }

    // Not confirmed: refused once per send, and an honest ending is never refused.
    const unconfirmed = fixture({ delivered: false });
    const refused = await unconfirmed.agent.executeTool(unconfirmed.tab, 'done', { summary: 'Sent.', outcome: 'success' });
    assert.equal(refused.blockedDone, true, JSON.stringify(refused));
    assert.equal(refused.chatDeliveryUnconfirmed, true);
    assert.match(refused.error, /Do not send the message again/);
    assert.equal(unconfirmed.guard.workflowTerminalEvidence, null);
    assert.deepEqual(await unconfirmed.agent.executeTool(unconfirmed.tab, 'done', { summary: 'Not confirmed.', outcome: 'partial' }),
      { done: true, summary: 'Not confirmed.', outcome: 'partial' });
    assert.deepEqual(await unconfirmed.agent.executeTool(unconfirmed.tab, 'done', { summary: 'Sent.', outcome: 'success' }),
      { done: true, summary: 'Sent.', outcome: 'success' });
    // The plain-final path shares the check.
    const plain = fixture({ delivered: false });
    assert.equal((await plain.agent._leanChatReceiptBlock(plain.tab))?.chatDeliveryUnconfirmed, true);
    assert.equal(await plain.agent._leanChatReceiptBlock(plain.tab), null);
  });

  test(`${build}: the live-message probe returns the page's visible status texts`, async () => {
    const agent = new Agent({ getActive: () => STRONG });
    const run = code => {
      const element = (text, width = 10) => ({ innerText: text, getBoundingClientRect: () => ({ width, height: 10 }) });
      const nodes = [element(' Message sent '), element('Hidden', 0), element('')];
      // The page's value arrives serialized, as CDP and executeScript return it.
      return JSON.parse(JSON.stringify(vm.runInNewContext(code, { document: { querySelectorAll: () => nodes },
        window: { getComputedStyle: () => ({ visibility: 'visible', display: 'block', opacity: '1' }) } })));
    };
    if (build === 'chrome') {
      const { cdpClient } = await import('../src/chrome/src/cdp/cdp-client.js');
      const original = cdpClient.evaluate;
      cdpClient.evaluate = async (_tab, expression) => ({ result: { value: run(expression) } });
      try { assert.deepEqual(await agent._liveRegionMessages(1), ['Message sent']); } finally { cdpClient.evaluate = original; }
    } else {
      const original = globalThis.browser.tabs.executeScript;
      globalThis.browser.tabs.executeScript = async (_tab, { code }) => [run(code)];
      try { assert.deepEqual(await agent._liveRegionMessages(1), ['Message sent']); } finally { globalThis.browser.tabs.executeScript = original; }
    }
  });

  test(`${build}: in a lean run an unverified CodeMirror write is a plain success with no note or lock`, async () => {
    const fileText = 'line one\n\nline three\n';
    const editorMeta = { tag: 'div', contentEditable: true, codeMirror: true, ariaLabel: 'Editing README.md file contents' };
    const agent = new Agent({ getActive: () => STRONG, getProvider: () => STRONG, getVisionProvider: async () => null });
    agent.strictGuards = true; // ignored in lean mode
    const tab = nextTab++;
    leanState(agent, tab);
    Object.assign(agent, { _hydrate: async () => {}, _persist: () => {}, _persistNow: async () => ({ ok: true }),
      _currentUrl: async () => 'https://github.com/example/repo/edit/main/README.md', _isPdfTab: async () => false,
      _chromeProtectedPageFailure: async () => null, _richTextToolbarToolBlock: async () => null, _injectCoreContentScripts: async () => {} });
    agent._lastAxScopes.set(tab, { documentToken: 'document-1', pageUrl: 'https://github.com/example/repo/edit/main/README.md' });
    const writes = [];
    const previous = api.tabs.sendMessage;
    api.tabs.sendMessage = async (_id, message) => {
      if (message.action === 'set_field') {
        writes.push(message.params);
        return { success: false, verified: false, dispatched: true, mutationMayHaveOccurred: true, outcomeUnknown: true, retryable: false,
          error: 'The complete settled rich-text value could not be verified exactly.', unverified: { reason: 'readback_mismatch', reader: 'rendered' },
          readbackExact: false, ref_id: 'ref_editor', fieldMeta: editorMeta };
      }
      return { success: false, documentToken: 'document-1', refScopeUrl: 'https://github.com/example/repo/edit/main/README.md' };
    };
    try {
      const first = await agent.executeTool(tab, 'set_field', { ref_id: 'ref_editor', text: fileText });
      assert.equal(first.success, true, JSON.stringify(first));
      assert.equal(first.unverifiedWrite, true);
      assert.equal(first.guardAdvisories, undefined, 'no note');
      assert.equal(Object.hasOwn(first, 'repeatBlocked'), false, 'no lock');
      const second = await agent.executeTool(tab, 'set_field', { ref_id: 'ref_editor', text: 'tail\n', clear: false });
      assert.equal(writes.length, 2, 'the next write goes out');
      assert.equal(second.guardAdvisories, undefined, 'no duplicate-append note either');
      assert.equal(agent._uncertainTextMutations?.has(tab) === true, false, 'no debt');
    } finally { api.tabs.sendMessage = previous; }
  });

  test(`${build}: the GitHub commit gates do not hold a lean commit; Off still holds it`, async () => {
    for (const setting of ['on', 'off']) {
      const agent = new Agent({ getActive: () => STRONG });
      agent.leanMode = setting;
      const tab = nextTab++;
      agent._runModeOverrides.set(tab, 'act');
      agent._beginLeanRun(tab, 'act');
      agent._socialPublicationPreSubmitBlock = async () => null;
      agent._captureMastodonPublicationAttempt = async () => null;
      agent._currentUrl = async () => 'https://github.com/example/repo/edit/main/README.md';
      agent._readControlEffect = async () => null;
      agent._refreshGithubTextReplacementProofs = async () => {};
      agent._planExecutionGuards.set(tab, { enabled: true, siteWorkflow: { adapterName: 'github', job: { id: 'edit-file-and-commit' } } });
      const commit = { isSubmit: true, validationSubmitEvidence: 'form', fields: [{ id: 'commit-message-input', name: 'message' }] };
      const block = await agent._workflowPreSubmitDispatchBlock(tab, 'click_ax', { ref_id: 'ref_commit' }, commit);
      if (setting === 'on') assert.equal(block, null, 'the commit click goes ahead (the submit card still applies to it)');
      else assert.match(block?.error || '', /Commit submission is blocked/, JSON.stringify(block));
    }
  });

  test(`${build}: lean mode keeps no progress ledger and reads no arithmetic task`, async () => {
    const agent = new Agent({ getActive: () => STRONG });
    const tab = nextTab++;
    leanState(agent, tab);
    let calculationReads = 0;
    agent._prepareCalculationReading = async () => { calculationReads++; };
    agent._readCompletenessStates = null;
    assert.equal(agent._readCompletenessNeedsScopeClassification(tab), false, 'no read-scope reading');
    // The run loop calls these through the lean checks; see agent.js.
    const source = read(`src/${build}/src/agent/agent.js`);
    assert.equal((source.match(/if \(!this\._leanRunActive\(tabId\)\) await this\._prepareCalculationReading\(tabId\);/g) || []).length, 2);
    // Every gate that starts or revises a task (both run loops and steering)
    // takes its ledger options from the one helper that honours lean mode.
    const ledgerCalls = source.match(/await this\._ensureProgressSessionForCurrentTask\(tabId, \{[\s\S]*?\n\s*\}\);/g) || [];
    assert.equal(ledgerCalls.length, 3);
    for (const site of ledgerCalls) assert.match(site, /\.\.\.this\._progressLedgerGateOptions\(tabId, (?:gateOutcome|gate)\)/);
    const gate = { progressLedgerPolicy: 'enabled', progressAction: 'process_item', expectedItems: [{ id: 'a' }] };
    assert.deepEqual(agent._progressLedgerGateOptions(tab, gate), { progressLedgerPolicy: 'disabled', progressAction: null, expectedItems: null });
    assert.deepEqual(agent._progressLedgerGateOptions(nextTab++, gate), gate, 'a run that is not lean keeps the gate\'s ledger');
    assert.equal(calculationReads, 0);
  });

  // A correction mid-run reruns the planner gate; a lean run keeps no ledger
  // after it either: the ledger's classification and checks are quality work
  // lean mode skips.
  for (const setting of ['on', 'off']) {
    test(`${build}: a steered ${setting === 'on' ? 'lean' : 'full'} run ${setting === 'on' ? 'keeps the progress ledger off' : 'takes the revised ledger'}`, async () => {
      const agent = new Agent({ getActive: () => STRONG });
      const tab = nextTab++;
      agent.conversationModes.set(tab, 'act');
      if (setting === 'on') leanState(agent, tab);
      Object.assign(agent, { _persist: () => {}, _persistNow: async () => ({ ok: true }), _currentUrl: async () => PAGE,
        _applyPendingSteering: () => {} });
      agent._steeringRuns.set(tab, { requestId: `lean-${tab}`, messages: [], acceptedIds: new Set(), revision: 2,
        authorizedRevision: 1, onUpdate: () => {} });
      const revisedGate = { proceed: true, requestKind: 'execute', requiresStateChange: true, requiresSubmission: false,
        progressLedgerPolicy: 'enabled', progressAction: 'process_item', expectedItems: [{ id: 'row-1' }, { id: 'row-2' }] };
      agent._maybeRunPlannerGate = async () => revisedGate;
      const ledgerCalls = [];
      const ensure = agent._ensureProgressSessionForCurrentTask.bind(agent);
      agent._ensureWorkflowMetadataRequirements = async () => {};
      agent._ensureProgressSessionForCurrentTask = async (tabId, opts) => {
        ledgerCalls.push({ policy: opts.progressLedgerPolicy, action: opts.progressAction, items: opts.expectedItems });
        return setting === 'on' ? ensure(tabId, opts) : null;
      };
      const messages = [{ role: 'system', content: 'sys' }, { role: 'user', content: 'Process the two rows.' }];
      agent.conversations.set(tab, messages);
      const revised = await agent._revalidatePendingSteering(tab, messages, () => {}, 'act', {}, null, {});
      assert.equal(revised?.changed, true);
      if (setting === 'on') {
        assert.deepEqual(ledgerCalls, [{ policy: 'disabled', action: null, items: null }]);
        assert.equal(agent.progressSessions.get(tab)?.mode, 'inactive', 'the revised task has no active ledger');
        assert.equal(agent.progressExpectedItems.has(tab), false);
      } else {
        assert.deepEqual(ledgerCalls, [{ policy: 'enabled', action: 'process_item', items: revisedGate.expectedItems }]);
      }
    });
  }

  // ── Hard checks keep their cards ────────────────────────────────────────
  test(`${build}: a lean run still sends an unclear deletion to WebBrain's deletion card`, async () => {
    const pageUrl = 'https://shop.example/cart';
    const documentIdentity = `${pageUrl}|1700000000000.5`;
    const provider = { ...STRONG, chat: async () => ({ content: '{}' }) };
    const agent = new Agent({ getActive: () => provider });
    const tab = nextTab++;
    agent._persist = () => {};
    agent.conversations.set(tab, [{ role: 'system', content: 'system' }, { role: 'user', content: 'Remove the first item from my cart.' }]);
    agent._startPlanExecutionGuard(tab, 'act', { requestKind: 'execute', requiresStateChange: true, requiresSubmission: true });
    agent._beginCompletionInvariant(tab);
    agent._runModeOverrides.set(tab, 'act');
    agent._beginLeanRun(tab, 'act');
    assert.equal(agent._leanRunActive(tab), true);
    const text = 'Wool socks Size L $12';
    const detected = { isSubmit: true, mutationKind: 'delete', publicationControl: false, url: pageUrl, frameDocumentIdentity: documentIdentity,
      actionRecord: { kind: 'record', text, owner: 'li', stage: 'record', complete: true, scopeToken: 'owned-record-token-0001',
        document: documentIdentity, siblings: ['Hat Size L $20'], siblingCount: 1,
        footprint: agent._sha256TextSync(JSON.stringify({ document: documentIdentity, owner: 'li', text })),
        identity: agent._sha256TextSync(JSON.stringify({ origin: new URL(pageUrl).origin, owner: 'li', text })) },
      reconciliationResources: [], reconciliationResourceScanComplete: false };
    agent._detectLikelySubmitAction = async () => structuredClone(detected);
    const calls = installModelStub(agent, { decide: defaultDecide, provider });
    const block = await agent._resourceMutationPreSubmitBlock(tab, 'click_ax', { ref_id: 'ref_42' }, structuredClone(detected), provider);
    assert.equal(block?.actionGuard?.reason, 'record_not_named_in_request', JSON.stringify(block));
    assert.equal(block.mutationConfirmation?.reason, 'not_named', 'the deletion card is offered');
    assert.equal(block.noDispatch, true);
    assert.equal(calls.filter(item => item.kind === 'deletion_request').length, 1, 'the deletion decision still runs');
  });

  test(`${build}: settings, background, export and locales carry the setting`, async () => {
    const html = read(`src/${build}/src/ui/settings.html`);
    const settings = read(`src/${build}/src/ui/settings.js`);
    const background = read(`src/${build}/src/background.js`);
    const runtime = build === 'chrome' ? 'chrome' : 'browser';
    assert.match(html, /<select id="select-lean-mode" data-i18n-aria-label="st\.display\.lean_mode\.label">\s*<option value="auto" data-i18n="st\.display\.lean_mode\.auto"><\/option>\s*<option value="on" data-i18n="st\.display\.lean_mode\.on"><\/option>\s*<option value="off" data-i18n="st\.display\.lean_mode\.off"><\/option>/);
    assert.ok(html.indexOf('select-lean-mode') > html.indexOf('toggle-strict-guards'), 'next to Strict checks, which it overrides');
    assert.match(settings, /'strictGuards', 'leanMode'/);
    assert.match(settings, /leanModeSelect\.value = normalizeLeanModeSetting\(stored\.leanMode\)/);
    assert.match(settings, new RegExp(`await ${runtime}\\.storage\\.local\\.set\\(\\{ leanMode: normalizeLeanModeSetting\\(leanModeSelect\\.value\\) \\}\\)`));
    assert.match(background, /get\(\['strictSecretMode', 'strictGuards', 'leanMode'\]\)/);
    assert.match(background, /agent\.leanMode = normalizeLeanModeSetting\(stored\?\.leanMode\);/);
    assert.match(background, /if \(changes\.leanMode\) \{\s*agent\.leanMode = normalizeLeanModeSetting\(changes\.leanMode\.newValue\);/);
    const config = await import(`../src/${build}/src/config-transfer.js`);
    assert.equal(config.DEFAULT_CONFIG_SETTINGS.leanMode, 'auto');
    assert.ok(config.CONFIG_STORAGE_KEYS.includes('leanMode'));
    assert.equal(config.createConfigExport({ leanMode: 'on' }).settings.leanMode, 'on');
    const exported = config.createConfigExport({});
    assert.equal(config.parseConfigImport(JSON.stringify({ ...exported, settings: { ...exported.settings, leanMode: 'off' } })).settings.leanMode, 'off');
    assert.throws(() => config.parseConfigImport(JSON.stringify({ ...exported, settings: { ...exported.settings, leanMode: 'maybe' } })), /leanMode/);
    // Every locale has real copy for the five strings.
    const dir = path.join(ROOT, `src/${build}/src/ui/locales`);
    const english = (await import(`../src/${build}/src/ui/locales/en.js`)).default;
    assert.match(english['st.display.lean_mode.desc'], /^For strong models: no automatic checks of typing[\s\S]*who receives a message, deleting data, spending money, credentials, publishing something you didn’t ask for, and site permissions/);
    // Lean mode keeps the tools and prompt, so no language says it cuts them.
    assert.doesNotMatch(english['st.display.lean_mode.desc'], /fewer tools|shorter prompt/);
    const locales = fs.readdirSync(dir).filter(name => /^[a-z]{2}\.js$/.test(name));
    assert.equal(locales.length, 23);
    for (const file of locales) {
      const dict = (await import(`../src/${build}/src/ui/locales/${file}`)).default;
      for (const key of ['label', 'desc', 'auto', 'on', 'off']) {
        assert.ok(String(dict[`st.display.lean_mode.${key}`] || '').trim(), `${file}: ${key}`);
      }
      if (file !== 'en.js') {
        assert.notEqual(dict['st.display.lean_mode.desc'], english['st.display.lean_mode.desc'], `${file}: translated description`);
        assert.notEqual(dict['st.display.lean_mode.label'], english['st.display.lean_mode.label'], `${file}: translated label`);
      }
    }
  });

  test(`${build}: traces and the scheduler know which mode ran`, async () => {
    const runtimeConfig = await import(`../src/${build}/src/trace/runtime-config.js`);
    assert.deepEqual(runtimeConfig.normalizeRuntimeTraceConfig({ lean_mode: true, lean_mode_setting: 'auto', api_key: 'x' }),
      { schema_version: 1, lean_mode: true, lean_mode_setting: 'auto' });
    const agent = new Agent({ getActive: () => STRONG });
    const tab = nextTab++;
    agent._runModeOverrides.set(tab, 'act');
    agent._beginLeanRun(tab, 'act');
    const config = agent._runtimeTraceConfig(STRONG, { tabId: tab, mode: 'act' });
    assert.equal(config.lean_mode, true);
    assert.equal(config.lean_mode_setting, 'auto');
    assert.match(read(`src/${build}/src/agent/agent.js`), /trace\.recordNote\(runId, null, 'lean_mode', \{/);
    // The scheduler's opt-in System One judge is a quality check.
    const { ScheduledJobManager } = await import(`../src/${build}/src/agent/scheduler.js`);
    const skipped = await ScheduledJobManager.prototype._evaluateSystemOne.call({ systemOneJudge: {} }, { source: 'task', prompt: 'x' }, 'ok', 'success',
      { leanMode: true, settings: { systemOneEnabled: true, systemOneCompletionEnabled: true } });
    assert.deepEqual(skipped, { decision: 'skip', reason: 'lean_mode' });
  });
}

// ── Recipient mismatch, real content script (Chromium) ────────────────────
const CHAT_URL = 'https://example.test/chat/rooms/one';
const chatPage = `<!doctype html><style>
  body{margin:0;font:16px sans-serif}aside{position:absolute;left:0;top:0;width:300px;height:790px;overflow:auto}
  #main{position:absolute;left:320px;top:0;width:940px;height:790px}
  #main>header{height:70px;display:flex;align-items:center;gap:30px;padding-left:70px}
  #messages{height:580px;overflow:auto}footer{display:flex;gap:15px;align-items:center}
  #body{width:550px;min-height:32px}button{min-height:28px}
</style><aside><button>Bob</button><button>Carol</button></aside>
<main id="main" role="main"><header><h2>Bob</h2></header><section id="messages">
  <div class="msg">Bob: please also write to Alice about the invoice</div>
  <div class="msg">Me: sure, will do</div></section>
  <footer><div id="body" contenteditable="true" role="textbox" aria-label="Type a message">Hi Bob, sending the invoice now</div>
    <button id="send" aria-label="Send">Send</button></footer></main>`;
for (const build of ['chrome', 'firefox']) {
  test(`${build}: a lean run still sends a recipient mismatch to WebBrain's recipient card`, async () => {
    const { Agent } = await import(`../src/${build}/src/agent/agent.js`);
    const browserInstance = await chromium.launch({ headless: true });
    try {
      const page = await browserInstance.newPage({ viewport: { width: 1280, height: 800 } });
      await page.route('**/*', route => route.fulfill({ contentType: 'text/html', body: '<!doctype html>' }));
      await page.goto(CHAT_URL);
      await page.setContent(chatPage);
      await page.evaluate(() => {
        window.fixtureHandlers = [];
        const runtime = { onMessage: { addListener(fn) { window.fixtureHandlers.push(fn); } }, getURL: p => p };
        window.chrome = { runtime }; window.browser = { runtime };
        window.sent = 0;
        document.querySelector('#send').addEventListener('click', () => window.sent++);
      });
      for (const file of ['file-upload-policy.js', 'rich-text-toolbar-heuristic.js', 'accessibility-tree.js', 'content.js']) {
        await page.addScriptTag({ content: await readFile(path.join(ROOT, 'src', build, 'src/content', file), 'utf8') });
      }
      const contentCall = (action, params = {}) => page.evaluate(message => new Promise(resolve => {
        for (const handler of window.fixtureHandlers) handler(message, {}, resolve);
      }), { target: 'content', action, params });
      const agent = new Agent({ getActive: () => ({ ...STRONG, supportsVision: false }) });
      const tab = 71;
      agent._currentUrl = async () => page.url();
      stubPageTextReadings(agent);
      agent._messageRecipientContentProbe = (_, params) => contentCall('probe_message_recipient_guard', params);
      agent._planExecutionGuards.set(tab, { messaging: { target_kind: 'named', recipients: ['[email protected]'] },
        requiresStateChange: true, requiresSubmission: true });
      agent._runModeOverrides.set(tab, 'act');
      agent._beginLeanRun(tab, 'act');
      assert.equal(agent._leanRunActive(tab), true);
      const blocked = await agent._messageRecipientGuardBlock(tab, 'click', { selector: '#send' }, page.url(), {});
      assert.equal(blocked?.reasonCode, 'active_recipient_unverified', JSON.stringify(blocked));
      assert.deepEqual(blocked.recipientConfirmation?.recipients, [{ identity: 'Bob', role: 'to' }], 'the recipient card is offered');
      assert.equal(await page.evaluate(() => window.sent), 0, 'nothing was sent');
    } finally { await browserInstance.close(); }
  });
}