Skip to content

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

Guard policy: chat_send follows the advisory policy for an unseen message

Barack Sokullu0b6311c2f9fb
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 7485d4942..8af43eb73 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -28727,7 +28727,15 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const prior = this.chatSessions.get(tabId) || createChatSession();
const observed = advanceChatSession(prior, before);
const workflow = () => this._chatWorkflowView(observed);
- if (observed.newMessages.some(message => message.direction === 'incoming')) {
+ // chat-send-preconditions (quality), as in the generic dispatch: without
+ // Strict checks an unread incoming message sends with a note, which this
+ // call's result carries (a refusal later in the call drops it). A
+ // pending outbound or a required user input keeps refusing below
+ // (decideChatSend).
+ if (observed.newMessages.some(message => message.direction === 'incoming')
+ && !observed.session.pendingOutbound && !observed.session.userInput?.required
+ && !this._guardAdvisesFor(executionContext?.guardAdvisories, 'chat-send-preconditions',
+ 'A new incoming message appeared since your last chat observation; this reply was sent without considering it. Read the conversation (chat_observe) before the next reply.')) {
return {
success: false,
noDispatch: true,
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 51210de51..663ce506e 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -25835,7 +25835,15 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const prior = this.chatSessions.get(tabId) || createChatSession();
const observed = advanceChatSession(prior, before);
const workflow = () => this._chatWorkflowView(observed);
- if (observed.newMessages.some(message => message.direction === 'incoming')) {
+ // chat-send-preconditions (quality), as in the generic dispatch: without
+ // Strict checks an unread incoming message sends with a note, which this
+ // call's result carries (a refusal later in the call drops it). A
+ // pending outbound or a required user input keeps refusing below
+ // (decideChatSend).
+ if (observed.newMessages.some(message => message.direction === 'incoming')
+ && !observed.session.pendingOutbound && !observed.session.userInput?.required
+ && !this._guardAdvisesFor(executionContext?.guardAdvisories, 'chat-send-preconditions',
+ 'A new incoming message appeared since your last chat observation; this reply was sent without considering it. Read the conversation (chat_observe) before the next reply.')) {
return {
success: false,
noDispatch: true,
diff --git a/test/guard-advisory-actions.mjs b/test/guard-advisory-actions.mjs
index 4e60e7493..f85c5471f 100644
--- a/test/guard-advisory-actions.mjs
+++ b/test/guard-advisory-actions.mjs
@@ -350,6 +350,54 @@ for (const build of ['chrome', 'firefox']) {
}
});
+ test(`${build}: chat-send-preconditions ${mode} mode: chat_send ${strict ? 'refuses a reply after unseen incoming' : 'sends after unseen incoming with a note'}`, async () => {
+ // The dedicated chat_send tool follows the same policy as the generic
+ // dispatch above.
+ const tab = 746;
+ const { agent, observed } = chatAgent(strict);
+ const baseline = { coverage: 'mounted_native_id_floor', complete: true, threadKey,
+ messageIds: ['telegram:k:123456:0:1', 'telegram:k:123456:0:2'] };
+ observed.nativeChatMessageBaseline = structuredClone(baseline);
+ observed.composer = { available: true, empty: true, ref: 'composer' };
+ agent._messageRecipientGuardBlock = async (_tab, _name, _args, _url, value) => {
+ Object.assign(value, { messageRecipientGuardRequired: true, messageRecipientBody: chatBody,
+ messageRecipientDispatchBinding: { token: 'fixture-one-use-token' },
+ messageRecipientNativeChatBaseline: structuredClone(baseline) });
+ return null;
+ };
+ const sent = [];
+ agent.executeTool = async (_tab, name, args) => {
+ sent.push({ name, args });
+ return { success: true, dispatched: true, verified: true, submitted: true };
+ };
+ await agent._observeChatWorkflow(tab);
+ observed.messages.push({ id: 'telegram:k:123456:0:2:rnext', direction: 'incoming', text: 'New fixture question' });
+ const guardAdvisories = [];
+ const result = await agent._sendChatWorkflow(tab, { thread_key: threadKey, composer_ref: 'composer', text: chatBody },
+ null, { guardAdvisories });
+ if (strict) {
+ assert.equal(result.reason, 'chat_observe_required');
+ assert.deepEqual(sent, []);
+ assert.deepEqual(guardsOf(guardAdvisories), []);
+ } else {
+ assert.notEqual(result.reason, 'chat_observe_required', JSON.stringify(result));
+ assert.deepEqual(sent.map(item => item.name), ['set_field'], 'the reply was sent');
+ assert.deepEqual(guardsOf(guardAdvisories), ['chat-send-preconditions']);
+ }
+ // A required user input (a one-time code here) still refuses.
+ const otp = chatAgent(strict);
+ otp.agent.executeTool = async () => assert.fail('nothing is sent while the page asks for user input');
+ otp.observed.composer = { available: true, empty: true, ref: 'composer' };
+ await otp.agent._observeChatWorkflow(tab);
+ otp.observed.messages.push({ id: 'telegram:k:123456:0:2:rnext', direction: 'incoming', text: 'New fixture question' });
+ otp.observed.userInput = { required: true, reason: 'otp' };
+ const otpAdvisories = [];
+ const refused = await otp.agent._sendChatWorkflow(tab, { thread_key: threadKey, composer_ref: 'composer', text: chatBody },
+ null, { guardAdvisories: otpAdvisories });
+ assert.equal(refused.noDispatch, true, JSON.stringify(refused));
+ assert.deepEqual(guardsOf(otpAdvisories), []);
+ });
+
test(`${build}: chat-send-preconditions ${mode} mode keeps a one-time-code request blocking`, async () => {
const tab = 745;
const { agent, run, dispatched, observed } = chatAgent(strict);
diff --git a/test/run.js b/test/run.js
index 2648c52ef..4f5c1e26d 100644
--- a/test/run.js
+++ b/test/run.js
@@ -5962,6 +5962,7 @@ test('model-callable chat tools bind the thread, send once, and require outgoing
assert.equal(boundedView.newMessages.at(-1).truncated, true, `${label}: oversized chat delta was not marked truncated`);
assert.equal(boundedView.deltaTruncated, true, `${label}: dropped chat delta entries were not disclosed`);
const staleAgent = new AgentClass({});
+ staleAgent.strictGuards = true; // Strict checks: the guard's refusal itself is under test.
staleAgent._persist = () => { throw new Error('stale preflight must not commit chat state'); };
staleAgent._persistNow = async () => { throw new Error('stale preflight must not persist chat state'); };
staleAgent.chatSessions.set(tabId, agent.chatSessions.get(tabId));