Skip to content

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

Fix stale page feedback dispatch races

Barack Sokullu891277eeeb64
diff --git a/firefox-companion/session.mjs b/firefox-companion/session.mjs
index e93028c5d..4efbeae76 100644
--- a/firefox-companion/session.mjs
+++ b/firefox-companion/session.mjs
@@ -266,6 +266,13 @@ export class BidiSession {
assertLive();
if (action === 'navigate') {
if (!/^https?:\/\//.test(payload.url)) throw new Error('Invalid navigation URL');
+ const guard = payload.pageFeedbackGuard;
+ if (guard && (typeof validatePageDispatch !== 'function'
+ || !await validatePageDispatch(id, guard, 'navigate', false))) {
+ const error = new Error('Page changed during native preparation; navigation was not sent');
+ error.code = 'page_feedback_pending'; throw error;
+ }
+ assertLive();
run.navigation = true;
try { dispatch.started = true; await this.send('browsingContext.navigate', { context: run.context, url: payload.url, wait: 'interactive' }); }
finally { run.navigation = false; }
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index 1bd194dbb..29fdaf8b8 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -33565,9 +33565,11 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
// Match Firefox's function-body contract while adding async/await:
// callers use an explicit `return` for readback instead of having to
// squeeze a multi-statement edit into one JavaScript expression.
- let expression = `(async () => {\n${code}\n})()\n//# sourceURL=webbrain-dev-execute.js`;
+ let expression = `(async () => {\n${code}\n})()`;
const pageGuard = await beforePageAgentDispatch(globalThis.chrome, tabId, { kind: 'dom' });
- if (pageGuard) expression = `document.dispatchEvent(new CustomEvent('webbrain-agent-dom-dispatch', { detail: ${JSON.stringify(JSON.stringify(pageGuard))} }));\n${expression}`;
+ const abortedDispatchToken = pageGuard ? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}` : null;
+ if (pageGuard) expression = `(() => { const gate = new CustomEvent('webbrain-agent-dom-dispatch', { detail: ${JSON.stringify(JSON.stringify(pageGuard))}, cancelable: true }); if (!window.dispatchEvent(gate)) return { __webbrainPageFeedbackAborted: ${JSON.stringify(abortedDispatchToken)} }; return ${expression}; })()`;
+ expression += '\n//# sourceURL=webbrain-dev-execute.js';
dispatched = true;
const response = await cdpClient.evaluate(tabId, expression, true, { timeoutMs: 15000 });
if (response?.exceptionDetails) {
@@ -33593,6 +33595,11 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
};
}
const remote = response?.result || {};
+ if (pageGuard && remote.value?.__webbrainPageFeedbackAborted === abortedDispatchToken) {
+ dispatched = false;
+ return { success: false, dispatched: false, noDispatch: true, retryable: true,
+ error: 'The page changed during JavaScript preparation. Re-read the page and retry.' };
+ }
let result = null;
let truncated = false;
let resultFormat = 'undefined';
@@ -43961,7 +43968,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
break;
}
- if ((steps < this.maxSteps && await this._applyPendingPageFeedback(tabId, messages, onUpdate))
+ if ((await this._applyPendingPageFeedback(tabId, messages, onUpdate))
|| this._applyPendingSteering(tabId, messages, onUpdate)) {
onUpdate('text', { content: '', replace: true });
continue;
@@ -44067,7 +44074,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
tabId, result.toolCalls, messages, onUpdate, provider, assistantToolContent, allowedToolNames, steps, runOptions, toolSchemas
);
if (['continue', 'return'].includes(batchResult.action) && !batchResult.status
- && ((steps < this.maxSteps && await this._applyPendingPageFeedback(tabId, messages, onUpdate))
+ && ((await this._applyPendingPageFeedback(tabId, messages, onUpdate))
|| this._applyPendingSteering(tabId, messages, onUpdate))) {
onUpdate('text', { content: '', replace: true });
continue;
@@ -45116,7 +45123,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
toolCalls: streamedToolCalls,
}));
- if ((steps < this.maxSteps && await this._applyPendingPageFeedback(tabId, messages, onUpdate))
+ if ((await this._applyPendingPageFeedback(tabId, messages, onUpdate))
|| this._applyPendingSteering(tabId, messages, onUpdate)) {
onUpdate('text', { content: '', replace: true });
continue;
@@ -45208,7 +45215,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
tabId, toolCalls, messages, onUpdate, provider, fullText, allowedToolNames, steps, runOptions, toolSchemas
);
if (['continue', 'return'].includes(batchResult.action) && !batchResult.status
- && ((steps < this.maxSteps && await this._applyPendingPageFeedback(tabId, messages, onUpdate))
+ && ((await this._applyPendingPageFeedback(tabId, messages, onUpdate))
|| this._applyPendingSteering(tabId, messages, onUpdate))) {
onUpdate('text', { content: '', replace: true });
continue;
diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.js
index ee6c59865..bdf9a177a 100644
--- a/src/chrome/src/content/content.js
+++ b/src/chrome/src/content/content.js
@@ -1789,6 +1789,7 @@
if (actionDeadlineExpired()) return deadlineFailure();
inp.scrollIntoView({ block: 'center', inline: 'center' });
if (actionDeadlineExpired()) return deadlineFailure();
+ window.__wbPageMonitor?.beforeLocalDispatch({ kind: 'focus', target: inp });
inp.focus();
el = inp;
textResolvedExact = (needle === ltxt);
@@ -1829,6 +1830,7 @@
if (actionDeadlineExpired()) return deadlineFailure();
inp.scrollIntoView({ block: 'center', inline: 'center' });
if (actionDeadlineExpired()) return deadlineFailure();
+ window.__wbPageMonitor?.beforeLocalDispatch({ kind: 'focus', target: inp });
inp.focus();
el = inp;
textResolvedExact = (needle === ltxt);
@@ -1972,6 +1974,7 @@
}
if (target) {
if (actionDeadlineExpired()) return deadlineFailure();
+ window.__wbPageMonitor?.beforeLocalDispatch({ kind: 'focus', target });
target.focus();
resolved = target;
}
@@ -2048,6 +2051,7 @@
return { success: true, method: 'select-already-set', selectedText: match.text.trim(), selectedValue: match.value };
}
if (actionDeadlineExpired()) return deadlineFailure();
+ window.__wbPageMonitor?.beforeLocalDispatch({ kind: 'focus', target: sel });
sel.focus();
if (actionDeadlineExpired()) return deadlineFailure();
const nativeSetter = Object.getOwnPropertyDescriptor(HTMLSelectElement.prototype, 'value')?.set;
@@ -2072,6 +2076,7 @@
// Do NOT scrollIntoView (hidden selects inside modals scroll to wrong position).
if (el instanceof HTMLSelectElement) {
if (actionDeadlineExpired()) return deadlineFailure();
+ window.__wbPageMonitor?.beforeLocalDispatch({ kind: 'focus', target: el });
el.focus();
if (actionDeadlineExpired()) return deadlineFailure();
const options = Array.from(el.options).map(o => o.text.trim());
@@ -2095,6 +2100,7 @@
}
if (nearbySel) {
if (actionDeadlineExpired()) return deadlineFailure();
+ window.__wbPageMonitor?.beforeLocalDispatch({ kind: 'focus', target: nearbySel });
nearbySel.focus();
if (actionDeadlineExpired()) return deadlineFailure();
const options = Array.from(nearbySel.options).map(o => o.text.trim());
diff --git a/src/chrome/src/content/page-monitor.js b/src/chrome/src/content/page-monitor.js
index 91b5912c5..138a401fa 100644
--- a/src/chrome/src/content/page-monitor.js
+++ b/src/chrome/src/content/page-monitor.js
@@ -355,6 +355,7 @@
const controlValue = controlValueFingerprint(el);
return JSON.stringify([shown, content, el.children.length + (el.shadowRoot?.children.length || 0), el.getAttribute('role'), el.getAttribute('aria-label'),
el.getAttribute('aria-expanded'), el.getAttribute('aria-selected'), el.getAttribute('aria-checked'),
+ el.getAttribute('aria-valuenow'), el.getAttribute('aria-valuetext'),
el.getAttribute('aria-pressed'),
el.getAttribute('href'), el.getAttribute('target'), el.getAttribute('download'), el.getAttribute('action'),
el.getAttribute('formaction'), el.getAttribute('formtarget'),
@@ -375,18 +376,32 @@
if (el.shadowRoot && !ignored(el)) observeRoot(el.shadowRoot, budget);
}
}
- function sampleFormControls() {
- if (!active || !liveControls.size) return;
+ function sampleFormControls(...targets) {
+ const extraTargets = targets.flat().filter(el => el instanceof Element);
+ if (!active || (!liveControls.size && !extraTargets.length)) return;
const controls = [...liveControls];
- if (controlCursor >= controls.length) controlCursor = 0;
+ if (!controls.length || controlCursor >= controls.length) controlCursor = 0;
const batchSize = Math.min(200, controls.length);
const batch = Array.from({ length: batchSize }, (_, index) => controls[(controlCursor + index) % controls.length]);
- controlCursor = (controlCursor + batchSize) % controls.length;
+ if (controls.length) controlCursor = (controlCursor + batchSize) % controls.length;
+ const sampled = new Set(batch);
const records = [];
for (const el of batch) {
if (!el.isConnected) { liveControls.delete(el); continue; }
if (!ignored(el)) records.push({ type: 'control', target: el });
}
+ for (const el of extraTargets) {
+ if (sampled.has(el) || !el.isConnected || !/^(INPUT|TEXTAREA|SELECT|OPTION)$/.test(el.tagName) || ignored(el)) continue;
+ sampled.add(el);
+ const previous = controlSignatures.get(el)
+ ?? [...operations.values()].find(op => op.target === el && op.controlStateAtPrepare !== undefined)?.controlStateAtPrepare;
+ if (previous !== undefined && controlState(el) !== previous) records.push({ type: 'control', target: el });
+ }
+ if (records.length) onMutations(records);
+ }
+ function flushPendingMutations() {
+ if (!active || !observer) return;
+ const records = observer.takeRecords();
if (records.length) onMutations(records);
}
function sampleDescendants(root, limit = 100) {
@@ -417,7 +432,7 @@
observer.observe(root, { childList: true, subtree: true, characterData: true, attributes: true, attributeOldValue: true,
attributeFilter: ['role', 'aria-label', 'aria-expanded', 'aria-selected', 'aria-checked', 'aria-pressed', 'aria-disabled',
'href', 'target', 'download', 'action', 'formaction', 'formtarget',
- 'aria-hidden', 'hidden', 'disabled', 'readonly', 'checked', 'selected', 'open', 'popover', 'inert', 'class', 'style'] });
+ 'aria-valuenow', 'aria-valuetext', 'aria-hidden', 'hidden', 'disabled', 'readonly', 'checked', 'selected', 'open', 'popover', 'inert', 'class', 'style'] });
listen(root, 'beforetoggle', event => {
const el = elementFor(event);
if (ignored(el) || !el.hasAttribute('popover') || !('popover' in el)) return;
@@ -604,14 +619,22 @@
observeRoot(root);
onMutations([{ type: 'shadow', target: host }]);
});
- listen(document, 'webbrain-agent-dom-dispatch', event => {
+ listen(window, 'webbrain-agent-dom-dispatch', event => {
try {
const guard = JSON.parse(String(event.detail));
- if (guard.documentToken !== documentToken || guard.revision !== revision || !operations.has(guard.operationId)) return;
+ const op = operations.get(guard.operationId);
+ event.stopImmediatePropagation();
+ flushPendingMutations();
+ sampleFormControls(op?.target);
+ if (!active || guard.runToken !== runToken || guard.documentToken !== documentToken || guard.revision !== revision
+ || !op || op.domDispatchConsumed || op.preparedRevision !== revision || domTimer || unreported || lastUserAt > op.userAt) {
+ event.preventDefault(); return;
+ }
+ op.domDispatchConsumed = true;
const marker = { userAt: lastUserAt }; agentTurn = marker;
setTimeout(() => { if (agentTurn === marker) agentTurn = null; }, 0);
- } catch { /* Only a current prepared operation can mark a DOM write. */ }
- });
+ } catch { event.stopImmediatePropagation(); event.preventDefault(); /* Only a current prepared operation can mark a DOM write. */ }
+ }, false);
listen(document, 'webbrain-agent-scroll-dispatch', event => {
try {
const guard = JSON.parse(String(event.detail));
@@ -780,7 +803,10 @@
const coordinateHit = coordinateSensitive ? document.elementFromPoint(params.x, params.y) : null;
const coordinateTarget = coordinateSensitive ? target || coordinateHit : null;
const coordinateTargetAtPoint = !target || !coordinateHit || target === coordinateHit || target.contains?.(coordinateHit);
- operations.set(params.operationId, { ...params, target: target || focusTarget, focusTarget, focusEligible, kinds: new Set(),
+ const operationTarget = target || focusTarget;
+ const controlStateAtPrepare = operationTarget && /^(INPUT|TEXTAREA|SELECT|OPTION)$/.test(operationTarget.tagName)
+ ? controlState(operationTarget) : undefined;
+ operations.set(params.operationId, { ...params, target: operationTarget, focusTarget, focusEligible, controlStateAtPrepare, kinds: new Set(),
coordinateSensitive, coordinateTarget, coordinateHit, coordinateTargetAtPoint,
coordinatePoint: coordinateSensitive ? { x: params.x, y: params.y } : null,
coordinateRect: rectFor(coordinateTarget),
@@ -793,6 +819,7 @@
op.blurTarget ??= document.activeElement;
op.target = resolveTarget(params) || (params.kind === 'input' ? op.focusTarget || deepActiveElement() : op.target);
if (params.kind === 'input' && !op.focusTarget) op.focusTarget = op.target;
+ if (params.kind === 'focus') { op.focusTarget = op.target; op.focusEligible = !!op.target; }
op.x = params.x; op.y = params.y;
if (!params.release || !op.seenEvents) op.seenEvents = new Set();
if (!params.release) op.eventTypes = Array.isArray(params.eventTypes) ? new Set(params.eventTypes) : null;
@@ -815,8 +842,9 @@
operations.set(op.operationId, op);
}
function activatePreparedDispatch(params = {}) {
- sampleFormControls();
const op = operations.get(params.operationId);
+ flushPendingMutations();
+ sampleFormControls(op?.target);
const layoutChanged = coordinatePreparationShifted(op);
if (!active || !op || layoutChanged || domTimer || unreported
|| lastUserAt > op.userAt
@@ -831,8 +859,9 @@
return { ready: true, operationId: op.operationId, revision };
}
function validateNativeDispatch(params = {}) {
- sampleFormControls();
const op = operations.get(params.operationId);
+ flushPendingMutations();
+ sampleFormControls(op?.target);
const shifted = coordinatePreparationShifted(op);
if (!active || params.runToken !== runToken || params.documentToken !== documentToken || !op || shifted
|| Number(params.revision) !== op.preparedRevision || op.preparedRevision !== revision
@@ -875,7 +904,8 @@
}
function beforeLocalDispatch({ preparation = false, kind, target } = {}) {
if (!active || !localOperation) return;
- sampleFormControls();
+ flushPendingMutations();
+ sampleFormControls(operations.get(localOperation.operationId)?.target);
const layoutChanged = coordinatePreparationShifted(operations.get(localOperation.operationId));
if (layoutChanged || domTimer || unreported || lastUserAt > localOperation.userAt || revision !== localOperation.revision) {
const error = new Error('Browser changed during action preparation. Re-observe before acting.');
@@ -913,8 +943,8 @@
else if (msg.action === 'page_monitor_validate') { respond({ ready: validateNativeDispatch(msg.params || {}) }); }
else if (msg.action === 'page_monitor_dispatch') {
const params = msg.params || {};
- if (!params.release) sampleFormControls();
const prepared = operations.get(params.operationId);
+ if (!params.release) { flushPendingMutations(); sampleFormControls(prepared?.target); }
const layoutChanged = !params.release && active && coordinatePreparationShifted(prepared);
if (!params.release && active && (layoutChanged || domTimer || unreported || (prepared && prepared.preparedRevision !== revision)
|| (params.documentToken && (params.documentToken !== documentToken || params.documentRevision !== revision)))) {
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 949b8bb1d..034c81b36 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -36886,7 +36886,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
break;
}
- if ((steps < this.maxSteps && await this._applyPendingPageFeedback(tabId, messages, onUpdate))
+ if ((await this._applyPendingPageFeedback(tabId, messages, onUpdate))
|| this._applyPendingSteering(tabId, messages, onUpdate)) {
onUpdate('text', { content: '', replace: true });
continue;
@@ -36944,7 +36944,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
tabId, result.toolCalls, messages, onUpdate, provider, assistantToolContent, allowedToolNames, steps, runOptions, toolSchemas
);
if (['continue', 'return'].includes(batchResult.action) && !batchResult.status
- && ((steps < this.maxSteps && await this._applyPendingPageFeedback(tabId, messages, onUpdate))
+ && ((await this._applyPendingPageFeedback(tabId, messages, onUpdate))
|| this._applyPendingSteering(tabId, messages, onUpdate))) {
onUpdate('text', { content: '', replace: true });
continue;
@@ -37892,7 +37892,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
toolCalls: streamedToolCalls,
}));
- if ((steps < this.maxSteps && await this._applyPendingPageFeedback(tabId, messages, onUpdate))
+ if ((await this._applyPendingPageFeedback(tabId, messages, onUpdate))
|| this._applyPendingSteering(tabId, messages, onUpdate)) {
onUpdate('text', { content: '', replace: true });
continue;
@@ -37937,7 +37937,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
tabId, toolCalls, messages, onUpdate, provider, fullText, allowedToolNames, steps, runOptions, toolSchemas
);
if (['continue', 'return'].includes(batchResult.action) && !batchResult.status
- && ((steps < this.maxSteps && await this._applyPendingPageFeedback(tabId, messages, onUpdate))
+ && ((await this._applyPendingPageFeedback(tabId, messages, onUpdate))
|| this._applyPendingSteering(tabId, messages, onUpdate))) {
onUpdate('text', { content: '', replace: true });
continue;
diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.js
index d0d16de0e..81b27cf71 100644
--- a/src/firefox/src/content/content.js
+++ b/src/firefox/src/content/content.js
@@ -2056,6 +2056,7 @@
if (actionDeadlineExpired()) return deadlineFailure();
inp.scrollIntoView({ block: 'center', inline: 'center' });
if (actionDeadlineExpired()) return deadlineFailure();
+ window.__wbPageMonitor?.beforeLocalDispatch({ kind: 'focus', target: inp });
inp.focus();
el = inp;
textResolvedExact = (needle === ltxt);
@@ -2099,6 +2100,7 @@
if (actionDeadlineExpired()) return deadlineFailure();
inp.scrollIntoView({ block: 'center', inline: 'center' });
if (actionDeadlineExpired()) return deadlineFailure();
+ window.__wbPageMonitor?.beforeLocalDispatch({ kind: 'focus', target: inp });
inp.focus();
el = inp;
textResolvedExact = (needle === ltxt);
@@ -2275,6 +2277,7 @@
}
if (target) {
if (actionDeadlineExpired()) return deadlineFailure();
+ window.__wbPageMonitor?.beforeLocalDispatch({ kind: 'focus', target });
target.focus();
resolved = target;
}
@@ -2349,6 +2352,7 @@
return { success: true, method: 'select-already-set', selectedText: match.text.trim(), selectedValue: match.value };
}
if (actionDeadlineExpired()) return deadlineFailure();
+ window.__wbPageMonitor?.beforeLocalDispatch({ kind: 'focus', target: sel });
sel.focus();
if (actionDeadlineExpired()) return deadlineFailure();
const nativeSetter = Object.getOwnPropertyDescriptor(HTMLSelectElement.prototype, 'value')?.set;
@@ -2373,6 +2377,7 @@
// Do NOT scrollIntoView (hidden selects inside modals scroll to wrong position).
if (el instanceof HTMLSelectElement) {
if (actionDeadlineExpired()) return deadlineFailure();
+ window.__wbPageMonitor?.beforeLocalDispatch({ kind: 'focus', target: el });
el.focus();
if (actionDeadlineExpired()) return deadlineFailure();
const options = Array.from(el.options).map(o => o.text.trim());
@@ -2396,6 +2401,7 @@
}
if (nearbySel) {
if (actionDeadlineExpired()) return deadlineFailure();
+ window.__wbPageMonitor?.beforeLocalDispatch({ kind: 'focus', target: nearbySel });
nearbySel.focus();
if (actionDeadlineExpired()) return deadlineFailure();
const options = Array.from(nearbySel.options).map(o => o.text.trim());
diff --git a/src/firefox/src/content/page-monitor.js b/src/firefox/src/content/page-monitor.js
index 91b5912c5..138a401fa 100644
--- a/src/firefox/src/content/page-monitor.js
+++ b/src/firefox/src/content/page-monitor.js
@@ -355,6 +355,7 @@
const controlValue = controlValueFingerprint(el);
return JSON.stringify([shown, content, el.children.length + (el.shadowRoot?.children.length || 0), el.getAttribute('role'), el.getAttribute('aria-label'),
el.getAttribute('aria-expanded'), el.getAttribute('aria-selected'), el.getAttribute('aria-checked'),
+ el.getAttribute('aria-valuenow'), el.getAttribute('aria-valuetext'),
el.getAttribute('aria-pressed'),
el.getAttribute('href'), el.getAttribute('target'), el.getAttribute('download'), el.getAttribute('action'),
el.getAttribute('formaction'), el.getAttribute('formtarget'),
@@ -375,18 +376,32 @@
if (el.shadowRoot && !ignored(el)) observeRoot(el.shadowRoot, budget);
}
}
- function sampleFormControls() {
- if (!active || !liveControls.size) return;
+ function sampleFormControls(...targets) {
+ const extraTargets = targets.flat().filter(el => el instanceof Element);
+ if (!active || (!liveControls.size && !extraTargets.length)) return;
const controls = [...liveControls];
- if (controlCursor >= controls.length) controlCursor = 0;
+ if (!controls.length || controlCursor >= controls.length) controlCursor = 0;
const batchSize = Math.min(200, controls.length);
const batch = Array.from({ length: batchSize }, (_, index) => controls[(controlCursor + index) % controls.length]);
- controlCursor = (controlCursor + batchSize) % controls.length;
+ if (controls.length) controlCursor = (controlCursor + batchSize) % controls.length;
+ const sampled = new Set(batch);
const records = [];
for (const el of batch) {
if (!el.isConnected) { liveControls.delete(el); continue; }
if (!ignored(el)) records.push({ type: 'control', target: el });
}
+ for (const el of extraTargets) {
+ if (sampled.has(el) || !el.isConnected || !/^(INPUT|TEXTAREA|SELECT|OPTION)$/.test(el.tagName) || ignored(el)) continue;
+ sampled.add(el);
+ const previous = controlSignatures.get(el)
+ ?? [...operations.values()].find(op => op.target === el && op.controlStateAtPrepare !== undefined)?.controlStateAtPrepare;
+ if (previous !== undefined && controlState(el) !== previous) records.push({ type: 'control', target: el });
+ }
+ if (records.length) onMutations(records);
+ }
+ function flushPendingMutations() {
+ if (!active || !observer) return;
+ const records = observer.takeRecords();
if (records.length) onMutations(records);
}
function sampleDescendants(root, limit = 100) {
@@ -417,7 +432,7 @@
observer.observe(root, { childList: true, subtree: true, characterData: true, attributes: true, attributeOldValue: true,
attributeFilter: ['role', 'aria-label', 'aria-expanded', 'aria-selected', 'aria-checked', 'aria-pressed', 'aria-disabled',
'href', 'target', 'download', 'action', 'formaction', 'formtarget',
- 'aria-hidden', 'hidden', 'disabled', 'readonly', 'checked', 'selected', 'open', 'popover', 'inert', 'class', 'style'] });
+ 'aria-valuenow', 'aria-valuetext', 'aria-hidden', 'hidden', 'disabled', 'readonly', 'checked', 'selected', 'open', 'popover', 'inert', 'class', 'style'] });
listen(root, 'beforetoggle', event => {
const el = elementFor(event);
if (ignored(el) || !el.hasAttribute('popover') || !('popover' in el)) return;
@@ -604,14 +619,22 @@
observeRoot(root);
onMutations([{ type: 'shadow', target: host }]);
});
- listen(document, 'webbrain-agent-dom-dispatch', event => {
+ listen(window, 'webbrain-agent-dom-dispatch', event => {
try {
const guard = JSON.parse(String(event.detail));
- if (guard.documentToken !== documentToken || guard.revision !== revision || !operations.has(guard.operationId)) return;
+ const op = operations.get(guard.operationId);
+ event.stopImmediatePropagation();
+ flushPendingMutations();
+ sampleFormControls(op?.target);
+ if (!active || guard.runToken !== runToken || guard.documentToken !== documentToken || guard.revision !== revision
+ || !op || op.domDispatchConsumed || op.preparedRevision !== revision || domTimer || unreported || lastUserAt > op.userAt) {
+ event.preventDefault(); return;
+ }
+ op.domDispatchConsumed = true;
const marker = { userAt: lastUserAt }; agentTurn = marker;
setTimeout(() => { if (agentTurn === marker) agentTurn = null; }, 0);
- } catch { /* Only a current prepared operation can mark a DOM write. */ }
- });
+ } catch { event.stopImmediatePropagation(); event.preventDefault(); /* Only a current prepared operation can mark a DOM write. */ }
+ }, false);
listen(document, 'webbrain-agent-scroll-dispatch', event => {
try {
const guard = JSON.parse(String(event.detail));
@@ -780,7 +803,10 @@
const coordinateHit = coordinateSensitive ? document.elementFromPoint(params.x, params.y) : null;
const coordinateTarget = coordinateSensitive ? target || coordinateHit : null;
const coordinateTargetAtPoint = !target || !coordinateHit || target === coordinateHit || target.contains?.(coordinateHit);
- operations.set(params.operationId, { ...params, target: target || focusTarget, focusTarget, focusEligible, kinds: new Set(),
+ const operationTarget = target || focusTarget;
+ const controlStateAtPrepare = operationTarget && /^(INPUT|TEXTAREA|SELECT|OPTION)$/.test(operationTarget.tagName)
+ ? controlState(operationTarget) : undefined;
+ operations.set(params.operationId, { ...params, target: operationTarget, focusTarget, focusEligible, controlStateAtPrepare, kinds: new Set(),
coordinateSensitive, coordinateTarget, coordinateHit, coordinateTargetAtPoint,
coordinatePoint: coordinateSensitive ? { x: params.x, y: params.y } : null,
coordinateRect: rectFor(coordinateTarget),
@@ -793,6 +819,7 @@
op.blurTarget ??= document.activeElement;
op.target = resolveTarget(params) || (params.kind === 'input' ? op.focusTarget || deepActiveElement() : op.target);
if (params.kind === 'input' && !op.focusTarget) op.focusTarget = op.target;
+ if (params.kind === 'focus') { op.focusTarget = op.target; op.focusEligible = !!op.target; }
op.x = params.x; op.y = params.y;
if (!params.release || !op.seenEvents) op.seenEvents = new Set();
if (!params.release) op.eventTypes = Array.isArray(params.eventTypes) ? new Set(params.eventTypes) : null;
@@ -815,8 +842,9 @@
operations.set(op.operationId, op);
}
function activatePreparedDispatch(params = {}) {
- sampleFormControls();
const op = operations.get(params.operationId);
+ flushPendingMutations();
+ sampleFormControls(op?.target);
const layoutChanged = coordinatePreparationShifted(op);
if (!active || !op || layoutChanged || domTimer || unreported
|| lastUserAt > op.userAt
@@ -831,8 +859,9 @@
return { ready: true, operationId: op.operationId, revision };
}
function validateNativeDispatch(params = {}) {
- sampleFormControls();
const op = operations.get(params.operationId);
+ flushPendingMutations();
+ sampleFormControls(op?.target);
const shifted = coordinatePreparationShifted(op);
if (!active || params.runToken !== runToken || params.documentToken !== documentToken || !op || shifted
|| Number(params.revision) !== op.preparedRevision || op.preparedRevision !== revision
@@ -875,7 +904,8 @@
}
function beforeLocalDispatch({ preparation = false, kind, target } = {}) {
if (!active || !localOperation) return;
- sampleFormControls();
+ flushPendingMutations();
+ sampleFormControls(operations.get(localOperation.operationId)?.target);
const layoutChanged = coordinatePreparationShifted(operations.get(localOperation.operationId));
if (layoutChanged || domTimer || unreported || lastUserAt > localOperation.userAt || revision !== localOperation.revision) {
const error = new Error('Browser changed during action preparation. Re-observe before acting.');
@@ -913,8 +943,8 @@
else if (msg.action === 'page_monitor_validate') { respond({ ready: validateNativeDispatch(msg.params || {}) }); }
else if (msg.action === 'page_monitor_dispatch') {
const params = msg.params || {};
- if (!params.release) sampleFormControls();
const prepared = operations.get(params.operationId);
+ if (!params.release) { flushPendingMutations(); sampleFormControls(prepared?.target); }
const layoutChanged = !params.release && active && coordinatePreparationShifted(prepared);
if (!params.release && active && (layoutChanged || domTimer || unreported || (prepared && prepared.preparedRevision !== revision)
|| (params.documentToken && (params.documentToken !== documentToken || params.documentRevision !== revision)))) {
diff --git a/test/firefox-bidi.mjs b/test/firefox-bidi.mjs
index 093421b5b..f64bb5240 100644
--- a/test/firefox-bidi.mjs
+++ b/test/firefox-bidi.mjs
@@ -182,6 +182,30 @@ test('stop during target preparation prevents input dispatch', async () => {
await assert.rejects(session.perform(runId, 'click', {}), /stopped/);
assert.equal(sent.includes('input.performActions'), false);
});
+test('BiDi navigation revalidates the page feedback guard before native dispatch', async () => {
+ const session = new BidiSession(), runId = id(), sent = [];
+ session.runs.set(runId, { context: 'a' });
+ session.send = async (method, params) => { sent.push({ method, params }); return {}; };
+ const guard = { documentToken: 'doc', revision: 4, operationId: 'op' };
+ await assert.rejects(session.perform(runId, 'navigate', { url: 'https://example.com/next', pageFeedbackGuard: guard },
+ async (_id, received, kind, rebindFocus) => {
+ assert.equal(received, guard);
+ assert.equal(kind, 'navigate');
+ assert.equal(rebindFocus, false);
+ return false;
+ }), error => {
+ assert.equal(error.code, 'page_feedback_pending');
+ assert.equal(error.dispatchState.noDispatch, true);
+ assert.equal(error.dispatchState.outcomeUnknown, false);
+ return true;
+ });
+ assert.equal(sent.some(call => call.method === 'browsingContext.navigate'), false,
+ 'A stale guard must be rejected before the native navigation command is sent');
+
+ await session.perform(runId, 'navigate', { url: 'https://example.com/next', pageFeedbackGuard: guard },
+ async (_id, _guard, kind) => kind === 'navigate');
+ assert.equal(sent.filter(call => call.method === 'browsingContext.navigate').length, 1);
+});
for (const failAt of [1, 2]) {
test(`page feedback change before native input ${failAt} preserves dispatch evidence`, async () => {
const session = new BidiSession(), runId = id();
diff --git a/test/page-feedback.mjs b/test/page-feedback.mjs
index fa0c2eb26..6bedd6acf 100644
--- a/test/page-feedback.mjs
+++ b/test/page-feedback.mjs
@@ -3,6 +3,7 @@ import fs from 'node:fs';
import vm from 'node:vm';
import { test } from 'node:test';
import { makeSchedulerHarness } from './lib/scheduler-harness.mjs';
+import { cdpClient } from '../src/chrome/src/cdp/cdp-client.js';
const area = { get: async () => ({}), set: async () => {}, remove: async () => {} };
const api = { storage: { local: area, session: area },
@@ -66,6 +67,45 @@ for (const build of ['chrome', 'firefox']) {
const { Agent } = await import(`../src/${build}/src/agent/agent.js`);
const { beforePageAgentDispatch } = await import(`../src/${build}/src/agent/page-feedback.js`);
+ if (build === 'chrome') {
+ test('Chrome execute_js returns without dispatch when the page-side revision gate rejects it', async () => {
+ const tab = nextTab++, agent = setup(Agent);
+ const previousSendMessage = api.tabs.sendMessage;
+ const previousEnable = cdpClient.enableDevDiagnostics, previousEvaluate = cdpClient.evaluate;
+ let expression = '';
+ try {
+ await agent._claimRunEntry(tab, 'interactive');
+ await agent._beginPageFeedbackRun(tab, 'interactive');
+ const run = agent._pageFeedbackRuns.get(tab);
+ run.frames.set(0, { token: 'execute-js-document', id: 'document-1' });
+ api.tabs.sendMessage = async (_tab, message) => message.action === 'page_monitor_dispatch'
+ ? { ready: true, guard: { runToken: run.token, documentToken: 'execute-js-document',
+ revision: 7, operationId: message.params.operationId } }
+ : { ready: true };
+ cdpClient.enableDevDiagnostics = async () => {};
+ cdpClient.evaluate = async (_tab, value) => {
+ expression = value;
+ const token = value.match(/__webbrainPageFeedbackAborted: "([^"]+)"/)?.[1];
+ assert.ok(token, 'The wrapped page expression must have a unique aborted-dispatch result');
+ return { result: { value: { __webbrainPageFeedbackAborted: token } } };
+ };
+ const result = await agent._executeDevJavaScript(tab, { code: 'window.__shouldNotRun = true;' });
+ assert.equal(result.dispatched, false);
+ assert.equal(result.noDispatch, true);
+ assert.match(result.error, /page changed during JavaScript preparation/i);
+ assert.match(expression, /cancelable: true/);
+ assert.match(expression, /if \(!window\.dispatchEvent\(gate\)\) return/);
+ assert.ok(expression.indexOf('return (async () =>') > expression.indexOf('dispatchEvent(gate)'));
+ } finally {
+ cdpClient.enableDevDiagnostics = previousEnable;
+ cdpClient.evaluate = previousEvaluate;
+ api.tabs.sendMessage = previousSendMessage;
+ if (agent._pageFeedbackRuns.has(tab)) agent._finishPageFeedbackRun(tab);
+ agent._releaseRunEntry(tab);
+ }
+ });
+ }
+
test(`${build}: observations are isolated by run, frame, document and sequence`, async () => {
const agent = setup(Agent), tab = nextTab++;
assert.equal(agent.pageMonitorState(sender(tab), 'a').active, false);
@@ -146,6 +186,63 @@ for (const build of ['chrome', 'firefox']) {
} finally { agent._releaseRunEntry(tab); }
});
+ for (const streaming of [false, true]) {
+ test(`${build}/${streaming ? 'stream' : 'chat'}: final-step page feedback suppresses a superseded completion`, async () => {
+ const tab = nextTab++, entered = deferred(), release = deferred(), staleCompletion = 'The task is complete on the old page.';
+ let providerCalls = 0, recoveryPrompt = '';
+ const provider = { name: 'feedback final-step test', model: 'test', promptTier: 'full', contextWindow: 128000,
+ supportsTools: true, supportsVision: false };
+ provider.chat = async function (messages) {
+ providerCalls++;
+ if (providerCalls === 1 && !streaming) {
+ entered.resolve(); await release.promise;
+ return { content: staleCompletion, toolCalls: null };
+ }
+ recoveryPrompt = JSON.stringify(messages);
+ return { content: null, toolCalls: [{ id: 'feedback_recovery_done',
+ function: { name: 'done', arguments: JSON.stringify({ summary: 'The page changed before the task was verified.', outcome: 'partial' }) } }] };
+ };
+ if (streaming) {
+ provider.chatStream = async function* () {
+ providerCalls++;
+ entered.resolve(); await release.promise;
+ yield { type: 'text', content: staleCompletion };
+ yield { type: 'done' };
+ };
+ }
+ const agent = setup(Agent, provider), updates = [];
+ agent.planBeforeAct = false;
+ agent.maxSteps = 1;
+ agent.autoScreenshot = 'off';
+ agent._skipPermissionGate = true;
+ agent._maybeRunPlannerGate = async () => ({ proceed: true, requestKind: 'execute', requiresStateChange: true });
+ agent._maybeReinjectAdapter = async () => {};
+ agent._ensureProgressSessionForCurrentTask = async () => ({ mode: 'inactive' });
+ agent._currentTaskLedgerRows = () => [];
+ agent._persist = () => {};
+ const run = streaming ? agent.processMessageStream.bind(agent) : agent.processMessage.bind(agent);
+ const running = run(tab, 'finish the existing task', (type, data) => updates.push({ type, data }), 'act');
+ try {
+ await entered.promise;
+ const binding = bind(agent, tab);
+ assert.equal(binding.send({ kind: 'input', source: 'user', target: 'input#filter' }).accepted, true);
+ release.resolve();
+ const final = await running;
+ assert.equal(providerCalls, 2, 'Only the bounded step-limit handoff may follow the one allowed agent step');
+ assert.equal(String(final).includes(staleCompletion), false, 'A completion formed against the previous page must be discarded');
+ assert.ok(updates.some(update => update.type === 'page_feedback'), 'The current page observation should be delivered');
+ assert.match(final, /page changed before the task was verified/i, 'The handoff must be based on the refreshed page context');
+ assert.ok(recoveryPrompt.includes('BROWSER STATE UPDATE'), 'The bounded recovery receives the page feedback in its model context');
+ assert.ok((agent.conversations.get(tab) || []).some(message => String(message.content || '').includes('BROWSER STATE UPDATE')),
+ 'The changed page state should be appended to the active model context');
+ } finally {
+ release.resolve();
+ if (agent._pageFeedbackRuns.has(tab)) agent._finishPageFeedbackRun(tab);
+ agent._releaseRunEntry(tab);
+ }
+ });
+ }
+
test(`${build}: viewport resize invalidates coordinates and waits for geometry to settle`, async () => {
const agent = setup(Agent), tab = nextTab++;
await agent._claimRunEntry(tab, 'interactive');
diff --git a/test/page-monitor-ui.mjs b/test/page-monitor-ui.mjs
index d926c110e..7c394c33b 100644
--- a/test/page-monitor-ui.mjs
+++ b/test/page-monitor-ui.mjs
@@ -433,6 +433,24 @@ test('Firefox native preparation keeps same-target user edits and sends no stale
});
for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {
+ test(`${build}: click-driven programmatic focus checks the monitor fence before focusin`, () => {
+ const source = read(build, 'content/content.js');
+ const clickStart = source.indexOf('function clickElement(params, actionDeadlineExpired = () => false)');
+ const typeStart = source.indexOf('function typeText(params, actionDeadlineExpired = () => false)', clickStart);
+ assert.ok(clickStart >= 0 && typeStart > clickStart, `${build}: click/type content action boundaries should be present`);
+ const clickBody = source.slice(clickStart, typeStart);
+ for (const [target, expected] of [['inp', 2], ['target', 1], ['sel', 1], ['el', 1], ['nearbySel', 1]]) {
+ const focusCount = [...clickBody.matchAll(new RegExp(`\\b${target}\\.focus\\(\\)`, 'g'))].length;
+ const marker = target === 'target'
+ ? /beforeLocalDispatch\(\{ kind: 'focus', target \}\);\s*target\.focus\(\)/g
+ : new RegExp(`beforeLocalDispatch\\(\\{ kind: 'focus', target: ${target} \\}\\);\\s*${target}\\.focus\\(\\)`, 'g');
+ const guardedCount = [...clickBody.matchAll(marker)].length;
+ assert.equal(focusCount, expected, `${build}: clickElement should account for ${expected} ${target}.focus() calls`);
+ assert.equal(guardedCount, focusCount,
+ `${build}: each ${target}.focus() must pass through the prepared dispatch boundary first`);
+ }
+ });
+
test(`${build}: content-script recovery preserves a live page monitor`, async () => {
const { browser, page } = await fixture(engine, build);
try {
@@ -996,6 +1014,46 @@ for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {
} finally { await browser.close(); }
});
+ test(`${build}: dispatch preflight samples its form target beyond the round-robin batch`, async () => {
+ const { browser, page } = await fixture(engine, build);
+ try {
+ const result = await page.evaluate(async () => {
+ const controls = document.createElement('div');
+ controls.innerHTML = `${'<input>'.repeat(450)}<input id="prepared-tail-control">`;
+ document.body.append(controls);
+ await Promise.resolve();
+ feedback = [];
+ deliver('page_monitor_prepare', { operationId: 'late-form-target', tool: 'click', selector: '#prepared-tail-control' });
+ document.getElementById('prepared-tail-control').value = 'private-late-value';
+ try { __wbPageMonitor.activatePreparedDispatch({ operationId: 'late-form-target', kind: 'click' }); return null; }
+ catch (error) { return error.code; }
+ });
+ assert.equal(result, 'page_feedback_pending', 'The target control must be checked even when it falls outside the current batch');
+ assert.equal(JSON.stringify(await page.evaluate(() => feedback)).includes('private-late-value'), false);
+ } finally { await browser.close(); }
+ });
+
+ test(`${build}: ARIA value changes invalidate prepared controls`, async () => {
+ const { browser, page } = await fixture(engine, build);
+ try {
+ await page.evaluate(() => {
+ const slider = document.createElement('div');
+ slider.id = 'custom-slider'; slider.setAttribute('role', 'slider');
+ slider.setAttribute('aria-valuenow', '10'); slider.setAttribute('aria-valuetext', 'Ten');
+ document.body.append(slider);
+ });
+ await page.waitForTimeout(100);
+ await page.evaluate(() => {
+ feedback = [];
+ const slider = document.getElementById('custom-slider');
+ slider.setAttribute('aria-valuenow', '20'); slider.setAttribute('aria-valuetext', 'Twenty');
+ });
+ await page.waitForFunction(() => feedback.some(event => event.kind === 'dom' && event.source === 'page'), null, { timeout: 1000 });
+ assert.equal(JSON.stringify(await page.evaluate(() => feedback)).includes('Twenty'), false,
+ 'ARIA value text is observed internally but is not copied into feedback');
+ } finally { await browser.close(); }
+ });
+
test(`${build}: native markers reject replay, tampering and copied targets`, async () => {
const { browser, page } = await fixture(engine, build);
try {
@@ -1752,13 +1810,20 @@ for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {
test(`${build}: marked synchronous DOM writes suppress their own effects and retain later edits`, async () => {
const { browser, page } = await fixture(engine, build);
try {
- await page.evaluate(() => {
+ const gates = await page.evaluate(() => {
feedback = [];
+ window.pageSawAgentGate = false;
+ window.addEventListener('webbrain-agent-dom-dispatch', () => { window.pageSawAgentGate = true; }, true);
deliver('page_monitor_prepare', { operationId: 'dom-write', tool: 'execute_js' });
deliver('page_monitor_dispatch', { operationId: 'dom-write', kind: 'dom' });
- document.dispatchEvent(new CustomEvent('webbrain-agent-dom-dispatch', { detail: JSON.stringify(lastMonitorResponse.guard) }));
+ const detail = JSON.stringify(lastMonitorResponse.guard);
+ const accepted = window.dispatchEvent(new CustomEvent('webbrain-agent-dom-dispatch', { detail, cancelable: true }));
+ const replayed = window.dispatchEvent(new CustomEvent('webbrain-agent-dom-dispatch', { detail, cancelable: true }));
document.getElementById('status').textContent = 'Own DOM write';
+ return { accepted, replayed, pageSawGate: window.pageSawAgentGate };
});
+ assert.deepEqual(gates, { accepted: true, replayed: false, pageSawGate: false },
+ 'The one-use gate authorizes the agent write without exposing its revision token to page listeners');
await page.waitForTimeout(200);
assert.equal((await page.evaluate(() => feedback)).length, 0);
await page.evaluate(() => { document.getElementById('status').textContent = 'External DOM edit'; });
@@ -1766,6 +1831,41 @@ for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {
} finally { await browser.close(); }
});
+ test(`${build}: stale execute_js dispatch gates are cancelled before page code runs`, async () => {
+ const { browser, page } = await fixture(engine, build);
+ try {
+ const guard = await page.evaluate(() => {
+ deliver('page_monitor_prepare', { operationId: 'stale-js', tool: 'execute_js' });
+ deliver('page_monitor_dispatch', { operationId: 'stale-js', kind: 'dom' });
+ return lastMonitorResponse.guard;
+ });
+ await page.locator('#human').click();
+ const result = await page.evaluate(guard => {
+ const gate = new CustomEvent('webbrain-agent-dom-dispatch', { detail: JSON.stringify(guard), cancelable: true });
+ return { accepted: window.dispatchEvent(gate), prevented: gate.defaultPrevented };
+ }, guard);
+ assert.deepEqual(result, { accepted: false, prevented: true },
+ 'The page-side bridge must cancel a guarded DOM write after user input invalidates its revision');
+ } finally { await browser.close(); }
+ });
+
+ test(`${build}: explicit agent focus is attributed before focusin`, async () => {
+ const { browser, page } = await fixture(engine, build);
+ try {
+ const events = await page.evaluate(() => {
+ feedback = [];
+ const target = document.getElementById('field');
+ const finish = __wbPageMonitor.beginContentAction('click', { selector: '#agent', _bidiPrepare: true });
+ try {
+ __wbPageMonitor.beforeLocalDispatch({ kind: 'focus', target });
+ target.focus();
+ return feedback;
+ } finally { finish(); }
+ });
+ assert.deepEqual(events, [], 'A focus event emitted immediately after its exact dispatch marker is agent activity');
+ } finally { await browser.close(); }
+ });
+
test(`${build}: native clear chords renew per phase and still detect later human keys`, async () => {
const { browser, page } = await fixture(engine, build);
try {