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
End a hand-off restart the user kept correcting read-only
Claude35c8c64bfe94
diff --git a/docs/architecture.md b/docs/architecture.mdindex 2adf0b816..3413c454a 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; the last of at most three restarts runs in the mode it asked for. 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 (`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.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 d87ceb168..31fa16c63 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -27012,6 +27012,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst previousMode = this.conversationModes.get(tabId) || null;let tier = '';try { tier = this._resolvePromptTier(this._activeProvider(tabId)); } catch {}+ // A hand-off restart the user kept correcting ends read-only, without the+ // hand-off tool: it applies the corrections and answers.+ if (runOptions?.modeEscalation?.readOnly === true) {+ onUpdate('run_mode', { mode: 'ask', source: 'reroute_limit' });+ return { mode: 'ask', runOptions: { ...runOptions, modeRoute: { requested: AUTO_MODE, mode: 'ask', source: 'reroute_limit' } } };+ }const hard = autoModeHardRoute({trustedContinuation: runOptions?.trustedContinuation === true,previousMode,diff --git a/src/chrome/src/background.js b/src/chrome/src/background.jsindex 4cd754300..56b55f179 100644--- a/src/chrome/src/background.js+++ b/src/chrome/src/background.js@@ -4052,15 +4052,18 @@ async function handleMessage(msg, sender) {// An automatically routed Ask turn may hand itself to Act or Dev; it// restarts here under the same request, so Stop still cancels it. A// correction that arrived after the hand-off was checked routes the- // restart again; the last restart runs in the mode it asked for.+ // restart again. A third such restart runs read-only instead: it+ // applies the corrections and answers, never in a mode chosen+ // before the latest correction.let escalation = mode === 'auto' ? agent.takeModeEscalation(tabId) : null;for (let restarts = 0; escalation; restarts += 1) {- const reroute = escalation.reroute === true && restarts < 2;+ const reroute = escalation.reroute === true;+ const readOnly = reroute && restarts >= 2;result = await agent.processMessage(tabId, msg.text, publishUpdate, reroute ? 'auto' : escalation.mode, msg.attachments, {...runOptions,- modeEscalation: { mode: escalation.mode, ...(reroute ? { reroute: true } : {}) },+ modeEscalation: { mode: escalation.mode, ...(reroute ? { reroute: true } : {}), ...(readOnly ? { readOnly: true } : {}) },});- escalation = reroute ? agent.takeModeEscalation(tabId) : null;+ escalation = reroute && !readOnly ? agent.takeModeEscalation(tabId) : null;}}diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex e68b8058d..b122d07dc 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -24058,6 +24058,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dconst previousMode = this.conversationModes.get(tabId) || null;let tier = '';try { tier = this._resolvePromptTier(this._activeProvider(tabId)); } catch {}+ // A hand-off restart the user kept correcting ends read-only, without the+ // hand-off tool: it applies the corrections and answers.+ if (runOptions?.modeEscalation?.readOnly === true) {+ onUpdate('run_mode', { mode: 'ask', source: 'reroute_limit' });+ return { mode: 'ask', runOptions: { ...runOptions, modeRoute: { requested: AUTO_MODE, mode: 'ask', source: 'reroute_limit' } } };+ }const hard = autoModeHardRoute({trustedContinuation: runOptions?.trustedContinuation === true,previousMode,diff --git a/src/firefox/src/background.js b/src/firefox/src/background.jsindex a7d86a7ef..c2c4f5331 100644--- a/src/firefox/src/background.js+++ b/src/firefox/src/background.js@@ -3501,15 +3501,18 @@ async function handleMessage(msg, sender) {// An automatically routed Ask turn may hand itself to Act or Dev; it// restarts here under the same request, so Stop still cancels it. A// correction that arrived after the hand-off was checked routes the- // restart again; the last restart runs in the mode it asked for.+ // restart again. A third such restart runs read-only instead: it+ // applies the corrections and answers, never in a mode chosen+ // before the latest correction.let escalation = mode === 'auto' ? agent.takeModeEscalation(tabId) : null;for (let restarts = 0; escalation; restarts += 1) {- const reroute = escalation.reroute === true && restarts < 2;+ const reroute = escalation.reroute === true;+ const readOnly = reroute && restarts >= 2;result = await agent.processMessage(tabId, msg.text, publishUpdate, reroute ? 'auto' : escalation.mode, msg.attachments, {...runOptions,- modeEscalation: { mode: escalation.mode, ...(reroute ? { reroute: true } : {}) },+ modeEscalation: { mode: escalation.mode, ...(reroute ? { reroute: true } : {}), ...(readOnly ? { readOnly: true } : {}) },});- escalation = reroute ? agent.takeModeEscalation(tabId) : null;+ escalation = reroute && !readOnly ? agent.takeModeEscalation(tabId) : null;}}diff --git a/test/auto-mode.mjs b/test/auto-mode.mjsindex 6fd500f7f..abb91438d 100644--- a/test/auto-mode.mjs+++ b/test/auto-mode.mjs@@ -479,6 +479,20 @@ for (const build of BUILDS) {assert.deepEqual(f.agent.takeModeEscalation(f.tab), { mode: 'dev', evidence: 'first inspect the console errors' });});+ test(`${build}: a restart rerouted too often ends read-only and still applies the corrections`, async () => {+ const f = fixture(Agent, { decide: () => { throw new Error('a read-only restart asks no route'); } });+ f.agent._modeEscalationSteering.set(f.tab, [{ id: 's11', text: 'instead inspect the console errors' }]);+ fixtureReplies(f, [messages => {+ assert.ok(messages.some(message => message.role === 'user' && String(message.content).startsWith('instead inspect the console errors')));+ return done('The console shows two errors.');+ }]);+ assert.equal(await f.run('Fix the login button.', 'auto', [], { modeEscalation: { mode: 'act', reroute: true, readOnly: true } }), 'The console shows two errors.');+ assert.deepEqual(runModes(f.updates), [{ mode: 'ask', source: 'reroute_limit' }]);+ assert.ok(!f.calls[0].tools.includes('request_action_mode'), 'no further hand-off');+ assert.ok(!f.calls[0].tools.includes('click_ax') && !f.calls[0].tools.includes('click'), 'and no browser actions');+ assert.equal(f.agent.takeModeEscalation(f.tab), null);+ });+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' }),@@ -789,8 +803,8 @@ for (const build of BUILDS) {test(`${build}: the background restarts a hand-off under the same request and reports the routed mode`, () => {const background = source(build, 'background.js');const chat = background.slice(background.indexOf(" case 'chat': {"), background.indexOf(" case 'chat_stream': {"));- assert.match(chat, /let escalation = mode === 'auto' \? agent\.takeModeEscalation\(tabId\) : null;\s*for \(let restarts = 0; escalation; restarts \+= 1\) \{\s*const reroute = escalation\.reroute === true && restarts < 2;\s*result = await agent\.processMessage\(tabId, msg\.text, publishUpdate, reroute \? 'auto' : escalation\.mode, msg\.attachments, \{\s*\.\.\.runOptions,\s*modeEscalation: \{ mode: escalation\.mode, \.\.\.\(reroute \? \{ reroute: true \} : \{\}\) \},\s*\}\);\s*escalation = reroute \? agent\.takeModeEscalation\(tabId\) : null;\s*\}/,- 'a late correction routes the restart again, and the last restart always runs in the mode it asked for');+ assert.match(chat, /let escalation = mode === 'auto' \? agent\.takeModeEscalation\(tabId\) : null;\s*for \(let restarts = 0; escalation; restarts \+= 1\) \{\s*const reroute = escalation\.reroute === true;\s*const readOnly = reroute && restarts >= 2;\s*result = await agent\.processMessage\(tabId, msg\.text, publishUpdate, reroute \? 'auto' : escalation\.mode, msg\.attachments, \{\s*\.\.\.runOptions,\s*modeEscalation: \{ mode: escalation\.mode, \.\.\.\(reroute \? \{ reroute: true \} : \{\}\), \.\.\.\(readOnly \? \{ readOnly: true \} : \{\}\) \},\s*\}\);\s*escalation = reroute && !readOnly \? agent\.takeModeEscalation\(tabId\) : null;\s*\}/,+ 'a late correction routes the restart again; a third one ends read-only, never in a mode chosen before it');assert.match(chat, /if \(type === 'run_mode' && \['ask', 'act', 'dev'\]\.includes\(data\?\.mode\)\) effectiveMode = data\.mode;/);assert.match(chat, /mode: effectiveMode,/);assert.match(chat, /effectiveMode === 'ask' \? askCompletionSucceededForBadge/);