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
Guard policy: a plain final after a chat send saves the receipt first
Barack Sokullu5d4a0ea6454b
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 7c1f6e93c..e0b30a3ae 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -5300,6 +5300,37 @@ export class Agent extends LoopDetector {return (this._guardRunState(tabId).outcomeDowngrades?.size || 0) > 0;}+ // native-chat-receipt-not-durable for a plain final. A plain final in an+ // Act or Dev run is the model's own success claim, and without Strict+ // checks it can end a run where done would have run this check: a chat+ // message sent in this run is read back in the conversation and its+ // receipt saved first, or a resumed run could send it again. Returns the+ // refusal when the receipt cannot be saved, otherwise null. A send the+ // conversation cannot confirm is left to the evidence checks, as for done.+ async _plainFinalChatReceiptBlock(tabId) {+ if (!this._isActionMode(this._effectiveRunMode(tabId))) return null;+ if (!this.completionInvariants.get(tabId)?.lastAction?.chatDeliveryBinding+ && !this.chatSessions.get(tabId)?.completionReceipt) return null;+ let pageUrl = '';+ try { pageUrl = await this._currentUrl(tabId); } catch {}+ const evidence = await this._freshNativeChatTerminalEvidence(tabId, pageUrl || '');+ if (!evidence) return null;+ const guard = this._planExecutionGuards.get(tabId);+ if (guard) guard.nativeChatDeliveryEvidence = evidence;+ const saved = await this._persistNow(tabId);+ if (saved === true || saved?.ok === true) return null;+ return { receiptNotDurable: true,+ error: 'The verified chat delivery receipt could not be saved. Do not send the message again. Retry once storage is available by calling done; if it still cannot be saved, finish with outcome "partial" and say so.' };+ }++ // 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) {+ 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.';+ return base ? `${base}\n\n${why}` : why;+ }+// Adds the "WebBrain could not verify everything" line, if any, to the// answer of an accepted completion. _guardRunState first drops a previous// run's caveats.@@ -52128,6 +52159,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dlet structuredOutputRecoveryAttempted = false;let autoModeEscalationRefusals = 0;let completionPlainFinalRecoveryAttempted = 0;+ let plainFinalReceiptRefusals = 0;let forceCompletionVerificationTurn = false;let allowCompletionFailureTurn = false;let forceCompletionDoneAfterVerification = false;@@ -52363,6 +52395,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dforceCompletionDoneAfterVerification = false;forceCompletionDoneTurn = false;completionPlainFinalRecoveryAttempted = 0;+ plainFinalReceiptRefusals = 0;}if (steeringOutcome?.terminal) {if (steeringOutcome.capture) {@@ -53209,6 +53242,29 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout donUpdate('warning', { message: finalResponse });break;}+ // 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);+ if (plainFinalReceiptBlock) {+ if (plainFinalReceiptRefusals >= 2 || steps >= this.maxSteps) {+ finalResponse = this._chatReceiptPartialFinal(result.content);+ _traceStatus = 'partial';+ messages.push({ role: 'assistant', content: finalResponse });+ onUpdate('text', { content: finalResponse, replace: true });+ onUpdate('warning', { message: 'Run ended without a saved chat delivery receipt.' });+ onUpdate('run_status', { status: 'partial', message: finalResponse });+ break;+ }+ plainFinalReceiptRefusals++;+ messages.push(this._withResponseItems({ role: 'assistant', content: result.content }, result.responseItems, result.reasoningContent, provider));+ messages.push(this._appOwnedUserMessage(plainFinalReceiptBlock.error, 'chat_receipt'));+ onUpdate('text', { content: '', replace: true });+ onUpdate('warning', { message: 'The chat delivery receipt is not saved yet; continuing.' });+ this._persist(tabId);+ continue;+ }shareRawResponse = rawProviderContent;const repairedFinalContent = repairAssistantDisplayText(result.content);finalResponse = result.costAllowanceMessage@@ -53749,6 +53805,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dlet emptyOutputRecoveryAttempted = false;let compressionPlaceholderRecoveryAttempted = false;let completionPlainFinalRecoveryAttempted = 0;+ let plainFinalReceiptRefusals = 0;let forceCompletionVerificationTurn = false;let allowCompletionFailureTurn = false;let forceCompletionDoneAfterVerification = false;@@ -53820,6 +53877,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dforceCompletionDoneAfterVerification = false;forceCompletionDoneTurn = false;completionPlainFinalRecoveryAttempted = 0;+ plainFinalReceiptRefusals = 0;}if (steeringOutcome?.terminal) {if (steeringOutcome.capture) {@@ -54550,6 +54608,30 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dcloseTraceStep({ ok: false, code: 'EMPTY_RESPONSE' });return finish(incompleteFailure, 'incomplete_output');}+ // 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);+ if (plainFinalReceiptBlock) {+ if (plainFinalReceiptRefusals >= 2 || steps >= this.maxSteps) {+ const partialText = this._chatReceiptPartialFinal(fullText);+ messages.push({ role: 'assistant', content: partialText });+ onUpdate('text', { content: partialText, replace: true });+ onUpdate('warning', { message: 'Run ended without a saved chat delivery receipt.' });+ onUpdate('run_status', { status: 'partial', message: partialText });+ this._persist(tabId);+ closeTraceStep({ ok: true });+ return finish(partialText, 'partial');+ }+ plainFinalReceiptRefusals++;+ messages.push(this._withResponseItems({ role: 'assistant', content: fullText }, responseItems, reasoningContent, provider));+ messages.push(this._appOwnedUserMessage(plainFinalReceiptBlock.error, 'chat_receipt'));+ onUpdate('text', { content: '', replace: true });+ onUpdate('warning', { message: 'The chat delivery receipt is not saved yet; continuing.' });+ this._persist(tabId);+ continue;+ }shareRawResponse = rawProviderContent;const repairedFullText = repairAssistantDisplayText(fullText);if (repairedFullText !== fullText) {diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex aef4785c0..2f45819f6 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -4852,6 +4852,37 @@ export class Agent extends LoopDetector {return (this._guardRunState(tabId).outcomeDowngrades?.size || 0) > 0;}+ // native-chat-receipt-not-durable for a plain final. A plain final in an+ // Act or Dev run is the model's own success claim, and without Strict+ // checks it can end a run where done would have run this check: a chat+ // message sent in this run is read back in the conversation and its+ // receipt saved first, or a resumed run could send it again. Returns the+ // refusal when the receipt cannot be saved, otherwise null. A send the+ // conversation cannot confirm is left to the evidence checks, as for done.+ async _plainFinalChatReceiptBlock(tabId) {+ if (!this._isActionMode(this._effectiveRunMode(tabId))) return null;+ if (!this.completionInvariants.get(tabId)?.lastAction?.chatDeliveryBinding+ && !this.chatSessions.get(tabId)?.completionReceipt) return null;+ let pageUrl = '';+ try { pageUrl = await this._currentUrl(tabId); } catch {}+ const evidence = await this._freshNativeChatTerminalEvidence(tabId, pageUrl || '');+ if (!evidence) return null;+ const guard = this._planExecutionGuards.get(tabId);+ if (guard) guard.nativeChatDeliveryEvidence = evidence;+ const saved = await this._persistNow(tabId);+ if (saved === true || saved?.ok === true) return null;+ return { receiptNotDurable: true,+ error: 'The verified chat delivery receipt could not be saved. Do not send the message again. Retry once storage is available by calling done; if it still cannot be saved, finish with outcome "partial" and say so.' };+ }++ // 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) {+ 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.';+ return base ? `${base}\n\n${why}` : why;+ }+// Adds the "WebBrain could not verify everything" line, if any, to the// answer of an accepted completion. _guardRunState first drops a previous// run's caveats.@@ -43879,6 +43910,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dlet structuredOutputRecoveryAttempted = false;let autoModeEscalationRefusals = 0;let completionPlainFinalRecoveryAttempted = 0;+ let plainFinalReceiptRefusals = 0;let forceCompletionVerificationTurn = false;let allowCompletionFailureTurn = false;let forceCompletionDoneAfterVerification = false;@@ -44103,6 +44135,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dforceCompletionDoneAfterVerification = false;forceCompletionDoneTurn = false;completionPlainFinalRecoveryAttempted = 0;+ plainFinalReceiptRefusals = 0;}if (steeringOutcome?.terminal) {if (steeringOutcome.capture) {@@ -44844,6 +44877,29 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout donUpdate('warning', { message: finalResponse });break;}+ // 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);+ if (plainFinalReceiptBlock) {+ if (plainFinalReceiptRefusals >= 2 || steps >= this.maxSteps) {+ finalResponse = this._chatReceiptPartialFinal(result.content);+ _traceStatus = 'partial';+ messages.push({ role: 'assistant', content: finalResponse });+ onUpdate('text', { content: finalResponse, replace: true });+ onUpdate('warning', { message: 'Run ended without a saved chat delivery receipt.' });+ onUpdate('run_status', { status: 'partial', message: finalResponse });+ break;+ }+ plainFinalReceiptRefusals++;+ messages.push(this._withResponseItems({ role: 'assistant', content: result.content }, result.responseItems, result.reasoningContent, provider));+ messages.push(this._appOwnedUserMessage(plainFinalReceiptBlock.error, 'chat_receipt'));+ onUpdate('text', { content: '', replace: true });+ onUpdate('warning', { message: 'The chat delivery receipt is not saved yet; continuing.' });+ this._persist(tabId);+ continue;+ }// Preserve provider text before display repairs and local notices.shareRawResponse = String(result.content ?? '');const repairedFinalContent = repairAssistantDisplayText(result.content);@@ -45305,6 +45361,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dlet emptyOutputRecoveryAttempted = false;let compressionPlaceholderRecoveryAttempted = false;let completionPlainFinalRecoveryAttempted = 0;+ let plainFinalReceiptRefusals = 0;let forceCompletionVerificationTurn = false;let allowCompletionFailureTurn = false;let forceCompletionDoneAfterVerification = false;@@ -45373,6 +45430,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dforceCompletionDoneAfterVerification = false;forceCompletionDoneTurn = false;completionPlainFinalRecoveryAttempted = 0;+ plainFinalReceiptRefusals = 0;}if (steeringOutcome?.terminal) {if (steeringOutcome.capture) {@@ -46023,6 +46081,30 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout dthis._persist(tabId);return finish(visibleFailure, planOnlyDecision.status || 'plan_only_output');}+ // 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);+ if (plainFinalReceiptBlock) {+ if (plainFinalReceiptRefusals >= 2 || steps >= this.maxSteps) {+ const partialText = this._chatReceiptPartialFinal(fullText);+ messages.push({ role: 'assistant', content: partialText });+ onUpdate('text', { content: partialText, replace: true });+ onUpdate('warning', { message: 'Run ended without a saved chat delivery receipt.' });+ onUpdate('run_status', { status: 'partial', message: partialText });+ this._persist(tabId);+ closeTraceStep({ ok: true });+ return finish(partialText, 'partial');+ }+ plainFinalReceiptRefusals++;+ messages.push(this._withResponseItems({ role: 'assistant', content: fullText }, responseItems, reasoningContent, provider));+ messages.push(this._appOwnedUserMessage(plainFinalReceiptBlock.error, 'chat_receipt'));+ onUpdate('text', { content: '', replace: true });+ onUpdate('warning', { message: 'The chat delivery receipt is not saved yet; continuing.' });+ this._persist(tabId);+ continue;+ }// Streaming text is still the provider's raw completion at this point.shareRawResponse = fullText;const repairedFullText = repairAssistantDisplayText(fullText);diff --git a/test/guard-advisory-completion.mjs b/test/guard-advisory-completion.mjsindex e63fe4b3d..1fd3eaacf 100644--- a/test/guard-advisory-completion.mjs+++ b/test/guard-advisory-completion.mjs@@ -273,6 +273,57 @@ for (const build of ['chrome', 'firefox']) {}}});++ test(`${label}: a plain final after a chat send saves the delivery receipt first`, async () => {+ // A plain final ends a run where done would have saved the receipt+ // (native-chat-receipt-not-durable), so it runs the same check.+ for (const save of ['saved', 'fails', 'no send']) {+ const tab = nextTab++;+ let n = 0;+ let receiptSave = false;+ const evidenceReads = [];+ const agent = loopAgent(Agent, async () => {+ n++;+ if (n === 1) return call('send', 'click_ax', { ref_id: 'ref_save' });+ // The click was a native chat send bound to its delivery.+ const state = agent.completionInvariants.get(tab);+ if (save !== 'no send' && state && !state.lastAction?.chatDeliveryBinding) {+ agent.completionInvariants.set(tab, { ...state, lastAction: { ...(state.lastAction || {}), chatDeliveryBinding: { token: 'chat-1' } } });+ }+ return { content: 'Sent the reply.' };+ });+ agent._freshNativeChatTerminalEvidence = async () => {+ evidenceReads.push(n);+ receiptSave = true;+ return { threadKey: 'dom:telegram:k:123456:0', messageId: 'telegram:k:123456:0:5' };+ };+ agent._persistNow = async () => {+ const failing = receiptSave && save === 'fails';+ receiptSave = false;+ return failing ? { ok: false } : { ok: true };+ };+ const runStatuses = [];+ let finished = null;+ const onUpdate = (type, data) => { if (type === 'run_status') runStatuses.push(data?.status); };+ const options = { detachedRequestId: `guard-${tab}`, onRunFinished: status => { finished = status; } };+ const result = streaming+ ? await agent.processMessageStream(tab, 'Reply to the owned chat.', onUpdate, 'act', options)+ : await agent.processMessage(tab, 'Reply to the owned chat.', onUpdate, 'act', [], options);+ if (save === 'saved') {+ assert.ok(evidenceReads.length >= 1, `${label}: the delivery was not read back before the run ended`);+ assert.equal(finished, 'done', `${label}: a saved receipt keeps the plain final`);+ assert.ok(String(result).startsWith('Sent the reply.'));+ } else if (save === 'fails') {+ assert.equal(evidenceReads.length, 3, `${label}: two refusals, then the run ends`);+ assert.equal(finished, 'partial', `${label}: a run without a saved receipt ended as a success`);+ assert.deepEqual(runStatuses, ['partial']);+ assert.match(String(result), /could not save the record that the message was delivered/);+ } else {+ assert.deepEqual(evidenceReads, [], `${label}: a run without a chat send read the conversation`);+ assert.equal(finished, 'done');+ }+ }+ });}test(`${build}: each untraced run gets its own one refusal per guard`, async () => {