Skip to content

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

fix(chat): correct message ordering, thread binding, and send guards

Codex release-state test0cfb1df470d1
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index b0bfdb6d2..ee8458947 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -17711,8 +17711,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
chatWorkflow: workflow(),
};
}
- const composerRef = String(args.composer_ref || args.composerRef || before.composer?.ref || '');
- if (!composerRef || (before.composer?.ref && composerRef !== before.composer.ref)) {
+ // The observed ref is the only trusted binding. Without it a caller-supplied
+ // ref would be typed into an unverified element, so require both and match.
+ const observedComposerRef = String(before.composer?.ref || '');
+ const requestedComposerRef = String(args.composer_ref || args.composerRef || '');
+ const composerRef = requestedComposerRef || observedComposerRef;
+ if (!observedComposerRef || composerRef !== observedComposerRef) {
return {
success: false,
noDispatch: true,
diff --git a/src/chrome/src/agent/chat-workflow.js b/src/chrome/src/agent/chat-workflow.js
index 8d1081632..51d6b135e 100644
--- a/src/chrome/src/agent/chat-workflow.js
+++ b/src/chrome/src/agent/chat-workflow.js
@@ -54,6 +54,9 @@ const USER_INPUT_REASON_SET = new Set(CHAT_USER_INPUT_REASONS);
const MAX_MESSAGES = 200;
const MAX_MESSAGE_TEXT = 4_000;
const MAX_ID = 240;
+// A dispatch that never produced a visible bubble must not block the same text
+// forever. `attemptedAt` ages the pending record out so the workflow can retry.
+const PENDING_OUTBOUND_TTL_MS = 10 * 60 * 1000;
function bounded(value, max = MAX_ID) {
return String(value ?? '')
@@ -221,6 +224,13 @@ export function createChatSession({ threadKey = '', now = Date.now() } = {}) {
return session;
}
+function pendingOutboundExpired(pending, now) {
+ if (!pending) return false;
+ const attempted = Date.parse(pending.attemptedAt || '');
+ if (!Number.isFinite(attempted)) return true;
+ return now - attempted > PENDING_OUTBOUND_TTL_MS;
+}
+
export function normalizeChatSession(value, now = Date.now()) {
const source = value && typeof value === 'object' && !Array.isArray(value) ? value : {};
const state = STATE_SET.has(source.state) ? source.state : 'waiting_for_transfer';
@@ -324,7 +334,9 @@ export function advanceChatSession(value, rawSnapshot, now = Date.now()) {
state: nextState,
seenMessageIds: [...new Set([...session.seenMessageIds, ...snapshot.messages.map(message => message.id)])].slice(-MAX_MESSAGES),
sentMessageKeys: [...outgoingKeys].slice(-MAX_MESSAGES),
- pendingOutbound: matchedPending ? null : session.pendingOutbound,
+ pendingOutbound: matchedPending || pendingOutboundExpired(session.pendingOutbound, now)
+ ? null
+ : session.pendingOutbound,
resolutionEvidence: snapshot.resolutionEvidence,
lastObservedAt: snapshot.observedAt,
userInput: snapshot.userInput,
@@ -347,7 +359,9 @@ export function advanceChatSession(value, rawSnapshot, now = Date.now()) {
export function decideChatSend(value, rawSnapshot, text, now = Date.now()) {
const session = normalizeChatSession(value, now);
const snapshot = normalizeChatSnapshot(rawSnapshot, now);
- const body = normalizeChatText(text);
+ // Normalize one character past the cap so an over-long body is rejected
+ // rather than silently truncated into a different message than requested.
+ const body = normalizeChatText(text, MAX_MESSAGE_TEXT + 1);
if (!body) return { ok: false, reason: 'empty_message', error: 'A non-empty chat message is required.' };
if (body.length > MAX_MESSAGE_TEXT) return { ok: false, reason: 'message_too_long', error: 'Chat messages must be 4,000 characters or fewer.' };
if (session.userInput?.required || snapshot.userInput?.required) {
@@ -367,7 +381,7 @@ export function decideChatSend(value, rawSnapshot, text, now = Date.now()) {
|| snapshot.messages.some(message => message.direction === 'outgoing' && messageKey(snapshot.threadKey, message.text) === key)) {
return { ok: false, duplicate: true, reason: 'already_sent', messageKey: key, error: 'This exact message is already visible as an outgoing message in this conversation.' };
}
- if (session.pendingOutbound?.key === key) {
+ if (session.pendingOutbound?.key === key && !pendingOutboundExpired(session.pendingOutbound, now)) {
return { ok: false, duplicate: true, pending: true, reason: 'send_pending', messageKey: key, error: 'This exact message is already pending verification; observe the conversation before retrying.' };
}
return { ok: true, messageKey: key, threadKey: snapshot.threadKey, text: body, attemptedAt: new Date(now).toISOString() };
diff --git a/src/chrome/src/content/chat-observation.js b/src/chrome/src/content/chat-observation.js
index a0bb4c5a5..7b3ca8de6 100644
--- a/src/chrome/src/content/chat-observation.js
+++ b/src/chrome/src/content/chat-observation.js
@@ -163,7 +163,8 @@
if (fromComposer) return fromComposer;
const activeMarker = queryMany(root, THREAD_ID_ATTRIBUTES.map(name => `[${name}]`))
.filter(node => visible(node))
- .find(node => attribute(node, 'aria-current').toLowerCase() !== 'false'
+ .find(node => (node.hasAttribute?.('aria-current')
+ && attribute(node, 'aria-current').toLowerCase() !== 'false')
|| attribute(node, 'aria-selected').toLowerCase() === 'true');
return firstAttribute(activeMarker ? [activeMarker] : [], THREAD_ID_ATTRIBUTES)
|| firstAttribute([root], THREAD_ID_ATTRIBUTES);
@@ -214,7 +215,12 @@
const tag = String(node.tagName || '').toLowerCase();
if (['button', 'input', 'textarea', 'select', 'option', 'nav', 'header', 'aside'].includes(tag)) return true;
try {
- return !!node.closest?.('button,[role="button"],[role="navigation"],nav,header,aside,[aria-live],[role="alert"]');
+ // Chat transcripts are routinely wrapped in a single aria-live/log
+ // region, so an announcing ancestor cannot disqualify its messages.
+ // Only the node itself counts as a status region; interactive and
+ // navigational ancestors still do.
+ if (node.matches?.('[aria-live],[role="alert"],[role="status"]')) return true;
+ return !!node.closest?.('button,[role="button"],[role="navigation"],nav,header,aside');
} catch {
return false;
}
@@ -270,7 +276,9 @@
}
}
- return selected.slice(0, MAX_ITEMS).map(item => ({
+ // The kernel keeps the last MAX_ITEMS entries, so the newest bubbles β€”
+ // not the oldest β€” must survive the cap in a long conversation.
+ return selected.slice(-MAX_ITEMS).map(item => ({
...(item.id ? { id: item.id } : {}),
direction: item.direction,
text: item.text,
@@ -306,7 +314,7 @@
if (/otp|one[-_ ]?time|verification[-_ ]?code|security[-_ ]?code/.test(explicit)
|| autocomplete.includes('one-time-code')
|| /otp|one[-_ ]?time|verification[-_ ]?code|security[-_ ]?code/.test(semantic)) reason = 'otp';
- else if (/pin/.test(explicit) || /pin/.test(semantic)) reason = 'pin';
+ else if (/pin/.test(explicit) || /(?:^|[^a-z0-9])pin(?:$|[^a-z0-9])/.test(semantic)) reason = 'pin';
else if (/payment|cc-|credit[-_ ]?card|cvc|cvv/.test(explicit)
|| /cc-|credit[-_ ]?card|cvc|cvv/.test(autocomplete)
|| /payment|credit[-_ ]?card|cvc|cvv/.test(semantic)) reason = 'payment';
@@ -314,7 +322,7 @@
|| node.hasAttribute?.('data-new-user-decision-required')) reason = 'new_user_decision';
else if (type === 'password'
|| /authentication|auth|password|login|sign[-_ ]?in/.test(explicit)
- || /authentication|auth|password|login|sign[-_ ]?in/.test(semantic)) reason = 'authentication';
+ || /(?:^|[^a-z0-9])(?:authentication|auth|password|login|sign[-_ ]?in)(?:$|[^a-z0-9])/.test(semantic)) reason = 'authentication';
if (!reason) continue;
const messages = {
otp: 'A one-time code or verification code requires the user.',
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 672616105..8c5a05bd7 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -15460,8 +15460,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
chatWorkflow: workflow(),
};
}
- const composerRef = String(args.composer_ref || args.composerRef || before.composer?.ref || '');
- if (!composerRef || (before.composer?.ref && composerRef !== before.composer.ref)) {
+ // The observed ref is the only trusted binding. Without it a caller-supplied
+ // ref would be typed into an unverified element, so require both and match.
+ const observedComposerRef = String(before.composer?.ref || '');
+ const requestedComposerRef = String(args.composer_ref || args.composerRef || '');
+ const composerRef = requestedComposerRef || observedComposerRef;
+ if (!observedComposerRef || composerRef !== observedComposerRef) {
return {
success: false,
noDispatch: true,
diff --git a/src/firefox/src/agent/chat-workflow.js b/src/firefox/src/agent/chat-workflow.js
index 8d1081632..51d6b135e 100644
--- a/src/firefox/src/agent/chat-workflow.js
+++ b/src/firefox/src/agent/chat-workflow.js
@@ -54,6 +54,9 @@ const USER_INPUT_REASON_SET = new Set(CHAT_USER_INPUT_REASONS);
const MAX_MESSAGES = 200;
const MAX_MESSAGE_TEXT = 4_000;
const MAX_ID = 240;
+// A dispatch that never produced a visible bubble must not block the same text
+// forever. `attemptedAt` ages the pending record out so the workflow can retry.
+const PENDING_OUTBOUND_TTL_MS = 10 * 60 * 1000;
function bounded(value, max = MAX_ID) {
return String(value ?? '')
@@ -221,6 +224,13 @@ export function createChatSession({ threadKey = '', now = Date.now() } = {}) {
return session;
}
+function pendingOutboundExpired(pending, now) {
+ if (!pending) return false;
+ const attempted = Date.parse(pending.attemptedAt || '');
+ if (!Number.isFinite(attempted)) return true;
+ return now - attempted > PENDING_OUTBOUND_TTL_MS;
+}
+
export function normalizeChatSession(value, now = Date.now()) {
const source = value && typeof value === 'object' && !Array.isArray(value) ? value : {};
const state = STATE_SET.has(source.state) ? source.state : 'waiting_for_transfer';
@@ -324,7 +334,9 @@ export function advanceChatSession(value, rawSnapshot, now = Date.now()) {
state: nextState,
seenMessageIds: [...new Set([...session.seenMessageIds, ...snapshot.messages.map(message => message.id)])].slice(-MAX_MESSAGES),
sentMessageKeys: [...outgoingKeys].slice(-MAX_MESSAGES),
- pendingOutbound: matchedPending ? null : session.pendingOutbound,
+ pendingOutbound: matchedPending || pendingOutboundExpired(session.pendingOutbound, now)
+ ? null
+ : session.pendingOutbound,
resolutionEvidence: snapshot.resolutionEvidence,
lastObservedAt: snapshot.observedAt,
userInput: snapshot.userInput,
@@ -347,7 +359,9 @@ export function advanceChatSession(value, rawSnapshot, now = Date.now()) {
export function decideChatSend(value, rawSnapshot, text, now = Date.now()) {
const session = normalizeChatSession(value, now);
const snapshot = normalizeChatSnapshot(rawSnapshot, now);
- const body = normalizeChatText(text);
+ // Normalize one character past the cap so an over-long body is rejected
+ // rather than silently truncated into a different message than requested.
+ const body = normalizeChatText(text, MAX_MESSAGE_TEXT + 1);
if (!body) return { ok: false, reason: 'empty_message', error: 'A non-empty chat message is required.' };
if (body.length > MAX_MESSAGE_TEXT) return { ok: false, reason: 'message_too_long', error: 'Chat messages must be 4,000 characters or fewer.' };
if (session.userInput?.required || snapshot.userInput?.required) {
@@ -367,7 +381,7 @@ export function decideChatSend(value, rawSnapshot, text, now = Date.now()) {
|| snapshot.messages.some(message => message.direction === 'outgoing' && messageKey(snapshot.threadKey, message.text) === key)) {
return { ok: false, duplicate: true, reason: 'already_sent', messageKey: key, error: 'This exact message is already visible as an outgoing message in this conversation.' };
}
- if (session.pendingOutbound?.key === key) {
+ if (session.pendingOutbound?.key === key && !pendingOutboundExpired(session.pendingOutbound, now)) {
return { ok: false, duplicate: true, pending: true, reason: 'send_pending', messageKey: key, error: 'This exact message is already pending verification; observe the conversation before retrying.' };
}
return { ok: true, messageKey: key, threadKey: snapshot.threadKey, text: body, attemptedAt: new Date(now).toISOString() };
diff --git a/src/firefox/src/content/chat-observation.js b/src/firefox/src/content/chat-observation.js
index a0bb4c5a5..7b3ca8de6 100644
--- a/src/firefox/src/content/chat-observation.js
+++ b/src/firefox/src/content/chat-observation.js
@@ -163,7 +163,8 @@
if (fromComposer) return fromComposer;
const activeMarker = queryMany(root, THREAD_ID_ATTRIBUTES.map(name => `[${name}]`))
.filter(node => visible(node))
- .find(node => attribute(node, 'aria-current').toLowerCase() !== 'false'
+ .find(node => (node.hasAttribute?.('aria-current')
+ && attribute(node, 'aria-current').toLowerCase() !== 'false')
|| attribute(node, 'aria-selected').toLowerCase() === 'true');
return firstAttribute(activeMarker ? [activeMarker] : [], THREAD_ID_ATTRIBUTES)
|| firstAttribute([root], THREAD_ID_ATTRIBUTES);
@@ -214,7 +215,12 @@
const tag = String(node.tagName || '').toLowerCase();
if (['button', 'input', 'textarea', 'select', 'option', 'nav', 'header', 'aside'].includes(tag)) return true;
try {
- return !!node.closest?.('button,[role="button"],[role="navigation"],nav,header,aside,[aria-live],[role="alert"]');
+ // Chat transcripts are routinely wrapped in a single aria-live/log
+ // region, so an announcing ancestor cannot disqualify its messages.
+ // Only the node itself counts as a status region; interactive and
+ // navigational ancestors still do.
+ if (node.matches?.('[aria-live],[role="alert"],[role="status"]')) return true;
+ return !!node.closest?.('button,[role="button"],[role="navigation"],nav,header,aside');
} catch {
return false;
}
@@ -270,7 +276,9 @@
}
}
- return selected.slice(0, MAX_ITEMS).map(item => ({
+ // The kernel keeps the last MAX_ITEMS entries, so the newest bubbles β€”
+ // not the oldest β€” must survive the cap in a long conversation.
+ return selected.slice(-MAX_ITEMS).map(item => ({
...(item.id ? { id: item.id } : {}),
direction: item.direction,
text: item.text,
@@ -306,7 +314,7 @@
if (/otp|one[-_ ]?time|verification[-_ ]?code|security[-_ ]?code/.test(explicit)
|| autocomplete.includes('one-time-code')
|| /otp|one[-_ ]?time|verification[-_ ]?code|security[-_ ]?code/.test(semantic)) reason = 'otp';
- else if (/pin/.test(explicit) || /pin/.test(semantic)) reason = 'pin';
+ else if (/pin/.test(explicit) || /(?:^|[^a-z0-9])pin(?:$|[^a-z0-9])/.test(semantic)) reason = 'pin';
else if (/payment|cc-|credit[-_ ]?card|cvc|cvv/.test(explicit)
|| /cc-|credit[-_ ]?card|cvc|cvv/.test(autocomplete)
|| /payment|credit[-_ ]?card|cvc|cvv/.test(semantic)) reason = 'payment';
@@ -314,7 +322,7 @@
|| node.hasAttribute?.('data-new-user-decision-required')) reason = 'new_user_decision';
else if (type === 'password'
|| /authentication|auth|password|login|sign[-_ ]?in/.test(explicit)
- || /authentication|auth|password|login|sign[-_ ]?in/.test(semantic)) reason = 'authentication';
+ || /(?:^|[^a-z0-9])(?:authentication|auth|password|login|sign[-_ ]?in)(?:$|[^a-z0-9])/.test(semantic)) reason = 'authentication';
if (!reason) continue;
const messages = {
otp: 'A one-time code or verification code requires the user.',