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
Check for corrections after the action run's browser setup, not before
Claude5a062d73807e
diff --git a/docs/architecture.md b/docs/architecture.mdindex 3413c454a..a33e692b7 100644--- a/docs/architecture.md+++ b/docs/architecture.md@@ -75,7 +75,7 @@ Model tiering is separate from mode: `compact | mid | full` controls how many no1. Run state decides alone when it can: a continuation keeps its run's mode; a turn for an in-browser (WebGPU) model, which runs as a tiny Ask, a selected-text scope, the standalone window, a Cloud run and a message that carries an `<untrusted_page_content>` block (a selection shortcut with page context) stay in Ask; and a video attachment, which can only be uploaded, needs Act. Context-menu and selection prompts are sent as Ask, as in Manual.2. Otherwise the decision model answers the fixed-choice `run_mode` question (`semantic-decisions.js`) about the user's own words, the previous turn's mode and the earlier task. It never sees page text, title or address. The question starts as soon as the turn's cost state exists and is awaited after `_enrichUserMessageWithCurrentPage`, so it overlaps page capture; after capture the turn waits at most `AUTO_MODE_ROUTE_GRACE_MS` (250 ms). Only a sure `act` or `dev` answer (the choices are `authorizing`) selects an action mode; Compact maps Dev to Act. The routed mode replaces the provisional Ask setup: run mode, system prompt, the turn's trusted runtime line and dialog handling (Chrome) or the BiDi run (Firefox).-3. Without a sure answer, or with no decision model, the turn starts in Ask at once. So does a turn whose user sent a correction while the route ran (`route_corrected`): the route did not read it, so an action answer is not trusted. Its tools add `request_action_mode`, and its system prompt adds `AUTO_MODE_ASK_NOTE`. The tool is never dispatched. The loop accepts the call only when its `evidence` quotes the user's own words exactly (the request, or a correction sent during the turn) and, because the calling model has read the page, when the same `run_mode` question, about the request with its corrections and asked again without any page content (decision model, then the active model with a quoted reason; a sure routing answer is reused from the semantic-decision cache) also answers act or dev. A correction sent while that check runs supersedes the call like any pending correction: the next step applies it and the model asks again, so a correction that needs Dev gets a Dev hand-off. Corrections sent during the read-only pass, applied or still pending, go with the hand-off: they stay journaled under the same request, and the action run queues them before its first model or tool boundary, so it never acts on the uncorrected request. A correction that arrives after the check (while the hand-off is saved) marks the escalation `reroute`: the background then restarts the request in Automatic again, and that route reads the request with its carried corrections; a third such restart runs read-only (`reroute_limit`, without the hand-off tool): it applies the corrections and answers, and never runs in a mode chosen before the latest correction. An accepted hand-off restores the conversation and per-tab turn state captured before the turn gathered its context (approved plans, site-adapter and API notes, the follow-up allowance, the durable-turn marker) and returns; the background's `chat` handler then restarts the same request with `modeEscalation` in the requested mode, under the same request ID, so Stop still cancels it. The action run's planner gate reads the request again before any action. A refused call gets a tool result with the reason, and a second refusal withdraws the tool. Chrome drains Dev diagnostics only when a routed turn after Dev ends without handing itself to Dev.+3. Without a sure answer, or with no decision model, the turn starts in Ask at once. So does a turn whose user sent a correction while the route ran or while the action run's browser setup started (`route_corrected`): the route did not read it, so an action answer is not trusted. That check follows the setup's last await, so nothing arrives between it and the committed mode. Its tools add `request_action_mode`, and its system prompt adds `AUTO_MODE_ASK_NOTE`. The tool is never dispatched. The loop accepts the call only when its `evidence` quotes the user's own words exactly (the request, or a correction sent during the turn) and, because the calling model has read the page, when the same `run_mode` question, about the request with its corrections and asked again without any page content (decision model, then the active model with a quoted reason; a sure routing answer is reused from the semantic-decision cache) also answers act or dev. A correction sent while that check runs supersedes the call like any pending correction: the next step applies it and the model asks again, so a correction that needs Dev gets a Dev hand-off. Corrections sent during the read-only pass, applied or still pending, go with the hand-off: they stay journaled under the same request, and the action run queues them before its first model or tool boundary, so it never acts on the uncorrected request. A correction that arrives after the check (while the hand-off is saved) marks the escalation `reroute`: the background then restarts the request in Automatic again, and that route reads the request with its carried corrections; a third such restart runs read-only (`reroute_limit`, without the hand-off tool): it applies the corrections and answers, and never runs in a mode chosen before the latest correction. An accepted hand-off restores the conversation and per-tab turn state captured before the turn gathered its context (approved plans, site-adapter and API notes, the follow-up allowance, the durable-turn marker) and returns; the background's `chat` handler then restarts the same request with `modeEscalation` in the requested mode, under the same request ID, so Stop still cancels it. The action run's planner gate reads the request again before any action. A refused call gets a tool result with the reason, and a second refusal withdraws the tool. Chrome drains Dev diagnostics only when a routed turn after Dev ends without handing itself to Dev.The agent reports the routed mode with a `run_mode` update (`{ mode, source }`), which the run journal stores as the snapshot's mode and the panel stores on the assistant message for Retry, Continue and recovery. Traces record a `mode_route` note with the source, the decision latency and the time the turn waited. In Automatic, slash commands and the Ctrl+Shift+A/X/D shortcuts choose a mode for one message only; the shortcuts write the slash command in front of the draft.diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 69d0399b7..e4308ff96 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -27086,12 +27086,23 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst waitStartedAt = Date.now();let routed = await settleAutoModeRoute(route);// The route read the request and the corrections it was given. One the- // user sent while it ran is not covered, so an action route is not- // trusted: the turn starts read-only, applies the correction and can- // still hand itself off through request_action_mode.+ // user sent while it ran, or while the action run's browser setup below+ // starts, is not covered, so an action route is not trusted: the turn+ // starts read-only, applies the correction and can still hand itself off+ // through request_action_mode. The check comes after the last await, so+ // nothing can arrive between it and the mode being committed.const covered = new Set(runOptions?.autoModeRouting?.correctionIds || []);- if (routed.mode !== 'ask'- && (this._steeringRuns.get(tabId)?.messages || []).some(item => !covered.has(String(item.id)))) {+ const corrected = () => (this._steeringRuns.get(tabId)?.messages || []).some(item => !covered.has(String(item.id)));+ if (routed.mode !== 'ask' && !corrected() && !this._isStandaloneChatRun(runOptions)) {+ await cdpClient.startDialogHandling(tabId, {+ signal: this._runAbortSignal(tabId),+ }).catch(error => {+ if (error?.name === 'AbortError' || error?.code === 'dialog_startup_timeout') throw error;+ // Restricted tabs can still use non-CDP tools.+ });+ }+ if (routed.mode !== 'ask' && corrected()) {+ cdpClient.stopDialogHandling(tabId);routed = { mode: 'ask', choice: null, source: 'route_corrected', latencyMs: routed.latencyMs };}const previousMode = runOptions?.autoModeRouting?.previousMode || null;@@ -27102,14 +27113,6 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dthis._runModeOverrides.set(tabId, mode);this.getConversation(tabId, mode);withRoutedRuntimeMode(enriched, mode);- if (!this._isStandaloneChatRun(runOptions)) {- await cdpClient.startDialogHandling(tabId, {- signal: this._runAbortSignal(tabId),- }).catch(error => {- if (error?.name === 'AbortError' || error?.code === 'dialog_startup_timeout') throw error;- // Restricted tabs can still use non-CDP tools.- });- }}// Leaving Dev drains its diagnostics. A read-only turn may still hand// itself to Dev, so processMessage decides when the turn ends.diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex d7802498f..68616aa60 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -24131,12 +24131,18 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst waitStartedAt = Date.now();let routed = await settleAutoModeRoute(route);// The route read the request and the corrections it was given. One the- // user sent while it ran is not covered, so an action route is not- // trusted: the turn starts read-only, applies the correction and can- // still hand itself off through request_action_mode.+ // user sent while it ran, or while the action run's browser setup below+ // starts, is not covered, so an action route is not trusted: the turn+ // starts read-only, applies the correction and can still hand itself off+ // through request_action_mode. The check comes after the last await, so+ // nothing can arrive between it and the mode being committed.const covered = new Set(runOptions?.autoModeRouting?.correctionIds || []);- if (routed.mode !== 'ask'- && (this._steeringRuns.get(tabId)?.messages || []).some(item => !covered.has(String(item.id)))) {+ const corrected = () => (this._steeringRuns.get(tabId)?.messages || []).some(item => !covered.has(String(item.id)));+ if (routed.mode !== 'ask' && !corrected() && !this._isStandaloneChatRun(runOptions)) {+ await firefoxBidi.startRun(tabId, this._runAbortSignal(tabId));+ }+ if (routed.mode !== 'ask' && corrected()) {+ firefoxBidi.stopRun(tabId);routed = { mode: 'ask', choice: null, source: 'route_corrected', latencyMs: routed.latencyMs };}const mode = routed.mode;@@ -24146,7 +24152,6 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dthis._runModeOverrides.set(tabId, mode);this.getConversation(tabId, mode);withRoutedRuntimeMode(enriched, mode);- if (!this._isStandaloneChatRun(runOptions)) await firefoxBidi.startRun(tabId, this._runAbortSignal(tabId));}onUpdate('run_mode', { mode, source: routed.source });return {diff --git a/test/auto-mode.mjs b/test/auto-mode.mjsindex 8aaf5e6ed..fb372995a 100644--- a/test/auto-mode.mjs+++ b/test/auto-mode.mjs@@ -517,6 +517,33 @@ for (const build of BUILDS) {assert.equal(f.agent.takeModeEscalation(f.tab), null);});+ test(`${build}: a correction sent while the action run's browser setup starts keeps the turn read-only`, async () => {+ // The last await before the mode is committed: Chrome's dialog handling,+ // Firefox's BiDi run.+ const setup = build === 'chrome'+ ? { module: await import('../src/chrome/src/cdp/cdp-client.js'), owner: 'cdpClient', start: 'startDialogHandling', stop: 'stopDialogHandling' }+ : { module: await import('../src/firefox/src/bidi/client.js'), owner: 'firefoxBidi', start: 'startRun', stop: 'stopRun' };+ const client = setup.module[setup.owner];+ const original = { start: client[setup.start], stop: client[setup.stop] };+ let stopped = 0;+ const f = fixture(Agent, {+ decide: (_q, options) => (options.mainModel === false ? { choice: 'act', source: 'decision_model' } : { choice: 'act', source: 'main_model' }),+ replies: [done('Read the console.')],+ });+ client[setup.start] = async () => { f.agent._steeringRuns.get(f.tab)?.messages.push({ id: 's14', text: 'instead inspect the console errors' }); };+ client[setup.stop] = () => { stopped += 1; };+ try {+ assert.equal(await f.run('Fix the login button.'), 'Read the console.');+ } finally {+ client[setup.start] = original.start;+ client[setup.stop] = original.stop;+ }+ assert.deepEqual(runModes(f.updates), [{ mode: 'ask', source: 'route_corrected' }]);+ assert.ok(stopped >= 1, 'the setup is undone');+ assert.ok(f.calls[0].tools.includes('request_action_mode'));+ assert.ok(!f.calls[0].tools.includes('click_ax') && !f.calls[0].tools.includes('click'));+ });+test(`${build}: corrections checked with the hand-off do not route the restart again`, async () => {const f = fixture(Agent, {decide: (_q, options) => (options.mainModel === false ? { choice: null, source: 'unavailable' } : { choice: 'act', source: 'main_model' }),