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
Lean mode: a plain final after a chat send keeps the receipt check
Barack Sokullue4b14060d607
diff --git a/docs/lean-mode.md b/docs/lean-mode.mdindex 84b9253d3..85e8d0a62 100644--- a/docs/lean-mode.md+++ b/docs/lean-mode.md@@ -81,7 +81,7 @@ Some steps that look like quality work feed the hard checks, so they stay too:- the social publication contract;- the rich-text toolbar probe, which pins the typing target (only its classification is skipped);- automatic screenshots, as set in Settings;-- a chat message sent in the run: its delivery must be confirmed in the conversation and its receipt saved before `done` reports success, or a resumed run could send it again. A receipt that cannot be saved refuses success every time; a send the conversation cannot confirm refuses it once per send, and `partial` or `failed` always ends the run.+- a chat message sent in the run: its delivery must be confirmed in the conversation and its receipt saved before `done` reports success, or a resumed run could send it again. A receipt that cannot be saved refuses success every time; a send the conversation cannot confirm refuses it once per send, and `partial` or `failed` always ends the run. A plain final answer gets the same check; after two refusals it ends the run as partial instead of looping.These quality guards keep running, each because a hard check depends on it (`LEAN_KEPT_QUALITY_GUARDS` in `lean-mode.js`):diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex d0790b408..7efe602b6 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -1490,34 +1490,45 @@ export class Agent extends LoopDetector {return next;}+ // The one hard check a lean run keeps at its end+ // (native-chat-receipt-not-durable), for a done and for a plain final+ // alike: both are the model's own success claim. A chat message sent in+ // this run must be confirmed in the conversation and its receipt saved+ // before the run ends as a success, or a resumed run could send it again.+ // Returns null when the run may end as a success, otherwise the refusal. A+ // receipt that cannot be saved refuses every time, as in a full run. A send+ // the conversation cannot confirm (its read failed, the conversation+ // changed, a new message arrived) refuses once per send, as a full run's+ // submission-evidence check does.+ async _leanChatReceiptBlock(tabId) {+ if (!this.completionInvariants.get(tabId)?.lastAction?.chatDeliveryBinding+ && !this.chatSessions.get(tabId)?.completionReceipt) return null;+ let pageUrl = '';+ try { pageUrl = await this._currentUrl(tabId); } catch {}+ const nativeChatEvidence = await this._freshNativeChatTerminalEvidence(tabId, pageUrl || '');+ if (nativeChatEvidence) {+ const guard = this._planExecutionGuards.get(tabId);+ if (guard) guard.nativeChatDeliveryEvidence = nativeChatEvidence;+ const savedNativeReceipt = await this._persistNow(tabId);+ if (savedNativeReceipt === true || savedNativeReceipt?.ok === true) return null;+ return { receiptNotDurable: true,+ error: 'The verified chat delivery receipt could not be saved. Retry completion after storage is available; do not send the message again.' };+ }+ if (!this._leanChatDeliveryUnconfirmedRefuses(tabId)) return null;+ return { chatDeliveryUnconfirmed: true,+ error: 'WebBrain could not confirm in the conversation that your message was delivered: its latest read failed, the conversation changed, or a new message arrived. Do not send the message again. Read the conversation now, then call done again; if the delivery still cannot be confirmed, finish with outcome "partial" and say so.' };+ }+// done in a lean run. The completion is the model's own call: WebBrain// takes no verification screenshot, runs no completion or incomplete-task- // judge and checks no page warnings or workflow evidence. One hard check- // stays (native-chat-receipt-not-durable): a chat message sent in this run- // must be confirmed in the conversation and its receipt saved before- // success is reported, or a resumed run could send it again. A receipt- // that cannot be saved refuses success every time, as in a full run. A send- // the conversation cannot confirm (its read failed, the conversation- // changed, a new message arrived) refuses success once per send, as a full- // run's submission-evidence check does; partial and failed always end.+ // judge and checks no page warnings or workflow evidence. Only the chat+ // receipt check stays (_leanChatReceiptBlock); partial and failed always+ // end.async _leanDoneResult(tabId, args = {}) {const outcome = normalizeDoneOutcome(args?.outcome);- if (outcome === 'success' && (this.completionInvariants.get(tabId)?.lastAction?.chatDeliveryBinding- || this.chatSessions.get(tabId)?.completionReceipt)) {- let pageUrl = '';- try { pageUrl = await this._currentUrl(tabId); } catch {}- const nativeChatEvidence = await this._freshNativeChatTerminalEvidence(tabId, pageUrl || '');- if (nativeChatEvidence) {- const guard = this._planExecutionGuards.get(tabId);- if (guard) guard.nativeChatDeliveryEvidence = nativeChatEvidence;- const savedNativeReceipt = await this._persistNow(tabId);- if (savedNativeReceipt !== true && savedNativeReceipt?.ok !== true) {- return { success: false, blockedDone: true, error: 'The verified chat delivery receipt could not be saved. Retry completion after storage is available; do not send the message again.' };- }- } else if (this._leanChatDeliveryUnconfirmedRefuses(tabId)) {- return { success: false, blockedDone: true, chatDeliveryUnconfirmed: true,- error: 'WebBrain could not confirm in the conversation that your message was delivered: its latest read failed, the conversation changed, or a new message arrived. Do not send the message again. Read the conversation now, then call done again; if the delivery still cannot be confirmed, finish with outcome "partial" and say so.' };- }+ if (outcome === 'success') {+ const receiptBlock = await this._leanChatReceiptBlock(tabId);+ if (receiptBlock) return { success: false, blockedDone: true, ...receiptBlock };}return { done: true, summary: args.summary, outcome };}@@ -5598,9 +5609,12 @@ export class Agent extends LoopDetector {// A plain final the receipt check refused twice (or at the step limit)// ends the run as partial: the model's text and why it is not a success.- _chatReceiptPartialFinal(content) {+ // A lean run's refusal can also be a send the conversation never confirmed.+ _chatReceiptPartialFinal(content, receiptBlock = null) {const base = repairAssistantDisplayText(String(content || '')).trimEnd();- const why = 'WebBrain could not save the record that the message was delivered, so this run did not end as a success. Check the conversation before sending it again.';+ const why = receiptBlock?.chatDeliveryUnconfirmed+ ? 'WebBrain could not confirm in the conversation that the message was delivered, so this run did not end as a success. Check the conversation before sending it again.'+ : 'WebBrain could not save the record that the message was delivered, so this run did not end as a success. Check the conversation before sending it again.';return base ? `${base}\n\n${why}` : why;}@@ -54277,12 +54291,16 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}// A plain final is the model's own success claim: a chat message sent// in this run is confirmed and its receipt saved first, as done does- // (native-chat-receipt-not-durable). Two refusals, or the step budget,- // end the run as partial, never as a success.- const plainFinalReceiptBlock = await this._plainFinalChatReceiptBlock(tabId);+ // (native-chat-receipt-not-durable; in a lean run _leanChatReceiptBlock,+ // which also refuses a send the conversation cannot confirm, once). Two+ // refusals, or the step budget, end the run as partial, never as a+ // success.+ const plainFinalReceiptBlock = this._leanRunActive(tabId)+ ? await this._leanChatReceiptBlock(tabId)+ : await this._plainFinalChatReceiptBlock(tabId);if (plainFinalReceiptBlock) {if (plainFinalReceiptRefusals >= 2 || steps >= this.maxSteps) {- finalResponse = this._chatReceiptPartialFinal(result.content);+ finalResponse = this._chatReceiptPartialFinal(result.content, plainFinalReceiptBlock);_traceStatus = 'partial';messages.push({ role: 'assistant', content: finalResponse });onUpdate('text', { content: finalResponse, replace: true });@@ -55651,12 +55669,15 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}// A plain final is the model's own success claim: a chat message sent// in this run is confirmed and its receipt saved first, as done does- // (native-chat-receipt-not-durable). Two refusals, or the step budget,- // end the run as partial, never as a success.- const plainFinalReceiptBlock = await this._plainFinalChatReceiptBlock(tabId);+ // (see the chat loop; a lean run uses _leanChatReceiptBlock). Two+ // refusals, or the step budget, end the run as partial, never as a+ // success.+ const plainFinalReceiptBlock = this._leanRunActive(tabId)+ ? await this._leanChatReceiptBlock(tabId)+ : await this._plainFinalChatReceiptBlock(tabId);if (plainFinalReceiptBlock) {if (plainFinalReceiptRefusals >= 2 || steps >= this.maxSteps) {- const partialText = this._chatReceiptPartialFinal(fullText);+ const partialText = this._chatReceiptPartialFinal(fullText, plainFinalReceiptBlock);messages.push({ role: 'assistant', content: partialText });onUpdate('text', { content: partialText, replace: true });onUpdate('warning', { message: 'Run ended without a saved chat delivery receipt.' });diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex e26f99b36..69c91d31d 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -1122,34 +1122,45 @@ export class Agent extends LoopDetector {return next;}+ // The one hard check a lean run keeps at its end+ // (native-chat-receipt-not-durable), for a done and for a plain final+ // alike: both are the model's own success claim. A chat message sent in+ // this run must be confirmed in the conversation and its receipt saved+ // before the run ends as a success, or a resumed run could send it again.+ // Returns null when the run may end as a success, otherwise the refusal. A+ // receipt that cannot be saved refuses every time, as in a full run. A send+ // the conversation cannot confirm (its read failed, the conversation+ // changed, a new message arrived) refuses once per send, as a full run's+ // submission-evidence check does.+ async _leanChatReceiptBlock(tabId) {+ if (!this.completionInvariants.get(tabId)?.lastAction?.chatDeliveryBinding+ && !this.chatSessions.get(tabId)?.completionReceipt) return null;+ let pageUrl = '';+ try { pageUrl = await this._currentUrl(tabId); } catch {}+ const nativeChatEvidence = await this._freshNativeChatTerminalEvidence(tabId, pageUrl || '');+ if (nativeChatEvidence) {+ const guard = this._planExecutionGuards.get(tabId);+ if (guard) guard.nativeChatDeliveryEvidence = nativeChatEvidence;+ const savedNativeReceipt = await this._persistNow(tabId);+ if (savedNativeReceipt === true || savedNativeReceipt?.ok === true) return null;+ return { receiptNotDurable: true,+ error: 'The verified chat delivery receipt could not be saved. Retry completion after storage is available; do not send the message again.' };+ }+ if (!this._leanChatDeliveryUnconfirmedRefuses(tabId)) return null;+ return { chatDeliveryUnconfirmed: true,+ error: 'WebBrain could not confirm in the conversation that your message was delivered: its latest read failed, the conversation changed, or a new message arrived. Do not send the message again. Read the conversation now, then call done again; if the delivery still cannot be confirmed, finish with outcome "partial" and say so.' };+ }+// done in a lean run. The completion is the model's own call: WebBrain// takes no verification screenshot, runs no completion or incomplete-task- // judge and checks no page warnings or workflow evidence. One hard check- // stays (native-chat-receipt-not-durable): a chat message sent in this run- // must be confirmed in the conversation and its receipt saved before- // success is reported, or a resumed run could send it again. A receipt- // that cannot be saved refuses success every time, as in a full run. A send- // the conversation cannot confirm (its read failed, the conversation- // changed, a new message arrived) refuses success once per send, as a full- // run's submission-evidence check does; partial and failed always end.+ // judge and checks no page warnings or workflow evidence. Only the chat+ // receipt check stays (_leanChatReceiptBlock); partial and failed always+ // end.async _leanDoneResult(tabId, args = {}) {const outcome = normalizeDoneOutcome(args?.outcome);- if (outcome === 'success' && (this.completionInvariants.get(tabId)?.lastAction?.chatDeliveryBinding- || this.chatSessions.get(tabId)?.completionReceipt)) {- let pageUrl = '';- try { pageUrl = await this._currentUrl(tabId); } catch {}- const nativeChatEvidence = await this._freshNativeChatTerminalEvidence(tabId, pageUrl || '');- if (nativeChatEvidence) {- const guard = this._planExecutionGuards.get(tabId);- if (guard) guard.nativeChatDeliveryEvidence = nativeChatEvidence;- const savedNativeReceipt = await this._persistNow(tabId);- if (savedNativeReceipt !== true && savedNativeReceipt?.ok !== true) {- return { success: false, blockedDone: true, error: 'The verified chat delivery receipt could not be saved. Retry completion after storage is available; do not send the message again.' };- }- } else if (this._leanChatDeliveryUnconfirmedRefuses(tabId)) {- return { success: false, blockedDone: true, chatDeliveryUnconfirmed: true,- error: 'WebBrain could not confirm in the conversation that your message was delivered: its latest read failed, the conversation changed, or a new message arrived. Do not send the message again. Read the conversation now, then call done again; if the delivery still cannot be confirmed, finish with outcome "partial" and say so.' };- }+ if (outcome === 'success') {+ const receiptBlock = await this._leanChatReceiptBlock(tabId);+ if (receiptBlock) return { success: false, blockedDone: true, ...receiptBlock };}return { done: true, summary: args.summary, outcome };}@@ -5150,9 +5161,12 @@ export class Agent extends LoopDetector {// A plain final the receipt check refused twice (or at the step limit)// ends the run as partial: the model's text and why it is not a success.- _chatReceiptPartialFinal(content) {+ // A lean run's refusal can also be a send the conversation never confirmed.+ _chatReceiptPartialFinal(content, receiptBlock = null) {const base = repairAssistantDisplayText(String(content || '')).trimEnd();- const why = 'WebBrain could not save the record that the message was delivered, so this run did not end as a success. Check the conversation before sending it again.';+ const why = receiptBlock?.chatDeliveryUnconfirmed+ ? 'WebBrain could not confirm in the conversation that the message was delivered, so this run did not end as a success. Check the conversation before sending it again.'+ : 'WebBrain could not save the record that the message was delivered, so this run did not end as a success. Check the conversation before sending it again.';return base ? `${base}\n\n${why}` : why;}@@ -45779,12 +45793,16 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}// A plain final is the model's own success claim: a chat message sent// in this run is confirmed and its receipt saved first, as done does- // (native-chat-receipt-not-durable). Two refusals, or the step budget,- // end the run as partial, never as a success.- const plainFinalReceiptBlock = await this._plainFinalChatReceiptBlock(tabId);+ // (native-chat-receipt-not-durable; in a lean run _leanChatReceiptBlock,+ // which also refuses a send the conversation cannot confirm, once). Two+ // refusals, or the step budget, end the run as partial, never as a+ // success.+ const plainFinalReceiptBlock = this._leanRunActive(tabId)+ ? await this._leanChatReceiptBlock(tabId)+ : await this._plainFinalChatReceiptBlock(tabId);if (plainFinalReceiptBlock) {if (plainFinalReceiptRefusals >= 2 || steps >= this.maxSteps) {- finalResponse = this._chatReceiptPartialFinal(result.content);+ finalResponse = this._chatReceiptPartialFinal(result.content, plainFinalReceiptBlock);_traceStatus = 'partial';messages.push({ role: 'assistant', content: finalResponse });onUpdate('text', { content: finalResponse, replace: true });@@ -46991,12 +47009,15 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d}// A plain final is the model's own success claim: a chat message sent// in this run is confirmed and its receipt saved first, as done does- // (native-chat-receipt-not-durable). Two refusals, or the step budget,- // end the run as partial, never as a success.- const plainFinalReceiptBlock = await this._plainFinalChatReceiptBlock(tabId);+ // (see the chat loop; a lean run uses _leanChatReceiptBlock). Two+ // refusals, or the step budget, end the run as partial, never as a+ // success.+ const plainFinalReceiptBlock = this._leanRunActive(tabId)+ ? await this._leanChatReceiptBlock(tabId)+ : await this._plainFinalChatReceiptBlock(tabId);if (plainFinalReceiptBlock) {if (plainFinalReceiptRefusals >= 2 || steps >= this.maxSteps) {- const partialText = this._chatReceiptPartialFinal(fullText);+ const partialText = this._chatReceiptPartialFinal(fullText, plainFinalReceiptBlock);messages.push({ role: 'assistant', content: partialText });onUpdate('text', { content: partialText, replace: true });onUpdate('warning', { message: 'Run ended without a saved chat delivery receipt.' });diff --git a/test/guard-policy.mjs b/test/guard-policy.mjsindex a0f0a956b..2c012f686 100644--- a/test/guard-policy.mjs+++ b/test/guard-policy.mjs@@ -75,9 +75,9 @@ const GRANDFATHERED_CEILING = 158;// Completion refusals (`blockedDone: true`) per build. A new one must be a// registered guard routed through _guardRefusesOnce (or a hard-class guard);// raise the ceiling only in the change that registers it. Lean mode's done-// keeps one hard check (native-chat-receipt-not-durable) in its own path:-// a receipt that cannot be saved, and a send the conversation cannot confirm.-const BLOCKED_DONE_CEILING = { chrome: 20, firefox: 19 };+// keeps one hard check (native-chat-receipt-not-durable) in its own path+// (_leanDoneResult over _leanChatReceiptBlock).+const BLOCKED_DONE_CEILING = { chrome: 19, firefox: 18 };function walk(dir) {return fs.readdirSync(new URL(`../${dir}`, import.meta.url), { withFileTypes: true }).flatMap(entry => {diff --git a/test/lean-mode.mjs b/test/lean-mode.mjsindex 36ca2df81..94eb35abe 100644--- a/test/lean-mode.mjs+++ b/test/lean-mode.mjs@@ -357,9 +357,90 @@ for (const build of ['chrome', 'firefox']) {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`);});}