Skip to content

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

Never resend or fall back after trusted input was delivered

Claude19b555a74caa
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.js
index bda705428..3b192e231 100644
--- a/src/chrome/src/agent/agent.js
+++ b/src/chrome/src/agent/agent.js
@@ -56,7 +56,7 @@ import { buildGithubStargazerProgressItems } from './observers/github-stargazers
import { analyzeMastodonPage, mastodonHandoffInstruction, mastodonProgressGuard } from './observers/mastodon.js';
import { isProgressActionAllowed, isProgressIntentActive, normalizeProgressAction, normalizeProgressIntent } from './progress-intent.js';
import { classifyCompletionForm, completionDoneBlock, completionPlainFinalBlock, completionPlainFinalPartial, consumeCompletionObservation, consumeCompletionObservationResult, createCompletionInvariantState, hasUnconsumedCompletionObservation, hasUnconsumedCompletionObservationResult, publicationDetailResource, publicationReplyParent, publicationResourceRecordRoot, recordCompletionToolResult } from './completion-invariant.js';
-import { cdpClient } from '../cdp/cdp-client.js';
+import { cdpClient, isCdpCommandNotSentError } from '../cdp/cdp-client.js';
import { findLastGmailResultPage, getActiveAdapter, getAdapterWorkflowRouting, getCarouselNavigationPolicy, getCarouselNavigationTarget, getFullPageCapturePolicy, getGmailResultCountPolicy, getGmailResultPageUrl, getMessageRecipientGuardPolicy, isPublicComposerRoute, parseCarouselSlideCount, parseGmailPaginationRange, resolveAdapterWorkflowJob, UNIVERSAL_PREAMBLE } from './adapters.js';
import { formatAdapterWorkflowExecutionPolicy } from './adapter-workflow.js';
import {
@@ -506,6 +506,30 @@ const CONTENT_ACTION_SIGNAL_DEADLINES = new WeakMap();
// Only this module can attest that its exact ref stayed unchanged before any
// trusted text command was attempted; page/model data cannot forge this proof.
const TRUSTED_FIELD_PREPARATION_UNCHANGED = Symbol('trustedFieldPreparationUnchanged');
+// Sends one trusted CDP input command through `send(options)`. `onSent` runs
+// once the page-feedback fence has passed, immediately before the command can
+// reach the page, and also for any rejection the transport cannot place before
+// that point (a lost acknowledgement can follow delivery). After that, callers
+// never resend, re-dispatch or fall back.
+async function sendTrustedCdpInput(send, { beforeSend = null, onSent = null } = {}) {
+ let sent = false, checked = false;
+ const markSent = () => {
+ if (sent) return;
+ onSent?.();
+ sent = true;
+ };
+ try {
+ const result = await send({ beforeSend: () => { checked = true; beforeSend?.(); markSent(); } });
+ markSent();
+ return result;
+ } catch (error) {
+ // A rejection from our own last check, or one the transport places
+ // before the protocol call, sent nothing; any other may follow delivery.
+ if (!checked && !isCdpCommandNotSentError(error)) markSent();
+ throw error;
+ }
+}
+
const EARLY_CDP_ACTION_TOOLS = new Set(['click', 'type_text', 'press_keys', 'hover', 'drag_drop', 'upload_file']);
const DONE_OUTCOMES = new Set(['success', 'partial', 'failed']);
const LOCAL_CANCELLATION_ASSISTANT_RE = /^\[?Stopped by user(?: before (?:the run started|executing requested tool calls))?\.?\]?$/;
@@ -1123,6 +1147,7 @@ export class Agent extends LoopDetector {
this._lastCdpClickIdent = new Map(); // tabId -> string
this._lastClickProgress = new Map(); // tabId -> { ident, snapshot }
this._clickAxCdpFallbacks = new Map(); // tabId -> Set(documentToken|ref_id), one trusted attempt per certified action; unresolved input locks its target
+ this._taskClickLocks = new Map(); // tabId -> { taskToken, keys:Set }, possibly delivered trusted clicks still unresolved for this task
this._lastAxScopes = new Map(); // tabId -> { documentToken, pageUrl }, captured by the latest AX read
this._uncertainTextMutations = new Map(); // tabId -> Map(target -> unresolved, potentially applied text write; no raw text)
this._verifiedTextReplacements = new Map(); // tabId -> exact replacement digest bound to the live document
@@ -16424,10 +16449,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
*/
async _autoSelectOption(tabId, cdpClient, optionText, abortSignal = null, beforeDispatch = null) {
const throwIfAborted = () => this._throwIfAborted(abortSignal);
- const markDispatch = () => {
- throwIfAborted();
- if (typeof beforeDispatch === 'function') beforeDispatch();
- };
+ // Key input is marked dispatched only once its fence passed (see
+ // sendTrustedCdpInput); focusing the exact select is preparation.
+ const sendKeyDown = params => sendTrustedCdpInput(
+ options => cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', params, '', options),
+ { beforeSend: throwIfAborted, onSent: typeof beforeDispatch === 'function' ? beforeDispatch : null },
+ );
throwIfAborted();
const needle = (optionText || '').trim();
if (!needle) return null;
@@ -16582,11 +16609,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// Escape must belong to the exact select found by the modal-scoped,
// ambiguity-checked scan. Sending it to the previously focused control
// can close a dialog or cancel unrelated UI before this select is used.
- markDispatch();
+ throwIfAborted();
if (!await focusExactTarget()) return null;
- await cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
- type: 'keyDown', key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27,
- });
+ await sendKeyDown({ type: 'keyDown', key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27 });
await cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
type: 'keyUp', key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27,
});
@@ -16600,10 +16625,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const arrowKey = delta > 0 ? 'ArrowDown' : 'ArrowUp';
const arrowVK = delta > 0 ? 40 : 38;
for (let i = 0; i < Math.abs(delta); i++) {
- markDispatch();
- await cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
- type: 'keyDown', key: arrowKey, code: arrowKey, windowsVirtualKeyCode: arrowVK,
- });
+ await sendKeyDown({ type: 'keyDown', key: arrowKey, code: arrowKey, windowsVirtualKeyCode: arrowVK });
await cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
type: 'keyUp', key: arrowKey, code: arrowKey, windowsVirtualKeyCode: arrowVK,
});
@@ -18347,7 +18369,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
dispatched: uncertain, ...(uncertain ? { outcomeUnknown: true, retryable: false }
: { noDispatch: true, outcomeUnknown: false, retryable: false }),
...(timing?.preparationVeto ? { preparationVeto: { ...timing.preparationVeto } } : {}), error });
- if (attempted.has(actionKey) || attempted.has(pendingKey)) return failure('This observed button action was already attempted or its input outcome remains unresolved. Do not repeat it; a verified effect and a new current observation are required for another action.');
+ if (attempted.has(actionKey) || attempted.has(pendingKey) || this._taskClickLockKeys(tabId)?.has(pendingKey)) return failure('This observed button action was already attempted or its input outcome remains unresolved. Do not repeat it; a verified effect and a new current observation are required for another action.');
// Store only short certificate/target keys; never retain model snapshots.
if (attempted.size > 4094) return failure('The trusted input bookkeeping limit for this run was reached. No input was sent.');
let pressed = false, pressAttempted = false, released = false, clickUserAt;
@@ -18385,9 +18407,21 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
}
if (current.documentToken !== prepared.documentToken
|| current.refScopeUrl !== prepared.refScopeUrl || current.ref_id !== args.ref_id
- || current.x !== prepared.x || current.y !== prepared.y) {
+ || current.tag !== prepared.tag || current.name !== prepared.name
+ || current.buttonStrongState !== prepared.buttonStrongState || current.buttonDisabled !== prepared.buttonDisabled
+ || !Number.isFinite(current.x) || !Number.isFinite(current.y)
+ // Only small hover geometry (lift, border, scale) is the same
+ // physical decision; a larger move needs a new observation.
+ || Math.abs(current.x - prepared.x) > 8 || Math.abs(current.y - prepared.y) > 8) {
return failure('The native button changed during trusted input preparation. Re-observe before acting.');
}
+ // The agent's own hover may lift or resize this exact certified button
+ // (:hover transform, border or size). The re-probe hit-tested its current
+ // centre on the same node; the monitor fence below rechecks the certified
+ // binding, and the press transport hit-tests the pressed point against
+ // that node immediately before the press. Never press the stale
+ // pre-hover point, which could now hit another node.
+ const pressPoint = { x: current.x, y: current.y };
preparationStage = 'after_pointer_before_press';
const inputGuard = await beforePageAgentDispatch(chrome, tabId, { kind: 'click', tool: 'click_ax', ref_id: args.ref_id,
documentToken: dispatchCall.modelState.actionBinding.documentToken, fenceOnly: true, prepareMonitor: true });
@@ -18396,16 +18430,22 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
if (!finishInput) return failure('The current owned native input operation could not be captured. Re-observe before acting.');
// Reserve before the consequential protocol call: a lost acknowledgement
// is not permission to replay either a trusted or a synthetic click.
- if (attempted.has(actionKey) || attempted.has(pendingKey)) return failure('A trusted input attempt for this action is already pending. Do not repeat it.');
+ if (attempted.has(actionKey) || attempted.has(pendingKey) || this._taskClickLockKeys(tabId)?.has(pendingKey)) return failure('A trusted input attempt for this action is already pending. Do not repeat it.');
attempted.add(actionKey); attempted.add(pendingKey);
clickUserAt = run.lastUserAt;
- pressAttempted = true; dispatchState.started = true; baseline.startedAt = Date.now();
preparationStage = 'press';
- await cdpClient.dispatchMouseEvent(tabId, 'mousePressed', prepared.x, prepared.y);
+ // Dispatch starts when the press passes its fence and final owner check.
+ await sendTrustedCdpInput(
+ options => cdpClient.dispatchMouseEvent(tabId, 'mousePressed', pressPoint.x, pressPoint.y, 'left', options),
+ { beforeSend: assertCurrent, onSent: () => {
+ pressAttempted = true; dispatchState.started = true; baseline.startedAt = Date.now();
+ this._rememberTaskClickLock(tabId, pendingKey);
+ } },
+ );
pressed = true; if (timing) timing.trustedInputEvents++;
assertCurrent();
preparationStage = 'release';
- await cdpClient.dispatchMouseEvent(tabId, 'mouseReleased', prepared.x, prepared.y);
+ await cdpClient.dispatchMouseEvent(tabId, 'mouseReleased', pressPoint.x, pressPoint.y);
released = true; pressed = false; if (timing) timing.trustedInputEvents++;
assertNotAborted();
const blocked = await cdpClient.consumeFileInputClickGuard(tabId);
@@ -18466,8 +18506,9 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
// may follow, even if its page effect is unverified. This proves delivery,
// not a business save. Lost or aborted input never reaches this release.
attempted.delete(pendingKey);
+ this._releaseTaskClickLock(tabId, pendingKey);
return { success: true, method: 'cdp-native-button', initialTrustedNativeButton: true,
- ref_id: args.ref_id, tag: prepared.tag, name: String(current.name || '').slice(0, 160), rect: prepared.rect, dispatched: true,
+ ref_id: args.ref_id, tag: prepared.tag, name: String(current.name || '').slice(0, 160), rect: current.rect || prepared.rect, dispatched: true,
trusted: true, verified: verifiedLocalEffect,
nativeInputAcknowledged: true, observationInterrupted: false,
effectVerificationPending: !verifiedLocalEffect,
@@ -18478,7 +18519,7 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
} catch (error) {
if (pressed || (pressAttempted && !released)) await this._cancelTrustedClickPress(tabId);
// The dispatch fence rejects before the protocol command is sent.
- const uncertain = pressed || released || (pressAttempted && error?.code !== 'page_feedback_pending');
+ const uncertain = pressed || released || (pressAttempted && !isCdpCommandNotSentError(error));
if (!uncertain) { dispatchState.started = false; attempted.delete(pendingKey); }
if (timing) {
const veto = error?.pagePreparationVeto;
@@ -18654,6 +18695,18 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
const messageOptions = dispatchBinding?.token && Number.isInteger(dispatchBinding.frameId)
? { frameId: dispatchBinding.frameId }
: undefined;
+ // An earlier trusted click on this exact document|ref may have landed
+ // without a verified effect (lost acknowledgement or interrupted
+ // observation). It stays locked across Continue/Retry of the same task,
+ // for the synthetic path as well; a re-rendered target or another
+ // document has a different key.
+ const taskLocks = this._taskClickLockKeys(tabId);
+ if (taskLocks?.size && axScope?.documentToken && typeof args?.ref_id === 'string'
+ && (taskLocks.has(`native-button-pending:${axScope.documentToken}|${args.ref_id}`)
+ || taskLocks.has(`${axScope.documentToken}|${args.ref_id}`))) {
+ return { success: false, dispatched: false, noDispatch: true, retryable: false, repeatBlocked: true,
+ error: 'An earlier trusted click on this exact target in this task may already have taken effect and was never verified. Inspect the page to reconcile its outcome instead of clicking it again.' };
+ }
const actionDeadlineAt = [upstreamAbortSignal, deadlineAbortSignal]
.map(signal => Number(CONTENT_ACTION_SIGNAL_DEADLINES.get(signal)?.deadlineAt))
.filter(deadlineAt => Number.isFinite(deadlineAt) && deadlineAt > 0)
@@ -18719,10 +18772,19 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
throwIfAborted();
} catch (error) {
if (error?.code === 'content_action_timeout') {
- return this._contentActionTimeoutResult('click_ax', error);
+ return dispatchState.started ? this._contentActionTimeoutResult('click_ax', error)
+ : this._contentActionPreparationTimeoutResult('click_ax', error, 'click preparation');
+ }
+ // Stop keeps the real dispatch state for the lifecycle wrapper.
+ if (error?.name === 'AbortError') throw error;
+ throwIfAborted();
+ // Only the browser's explicit missing-receiver error proves that the
+ // click message reached no listener. A closed response channel can
+ // follow a delivered el.click() that navigated or replaced its
+ // document; resending would click twice or misreport the delivery.
+ if (!this._isMissingContentReceiverError(error)) {
+ return this._contentActionCommunicationFailure('click_ax', error, dispatchState.started);
}
- // A rejected first message is the no-receiver path that triggers
- // injection; no page click was delivered unless the retry starts.
dispatchState.started = false;
try {
throwIfAborted();
@@ -18736,9 +18798,12 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
throwIfAborted();
} catch (retryError) {
if (retryError?.code === 'content_action_timeout') {
- return this._contentActionTimeoutResult('click_ax', retryError);
+ return dispatchState.started ? this._contentActionTimeoutResult('click_ax', retryError)
+ : this._contentActionPreparationTimeoutResult('click_ax', retryError, 'content-script injection');
}
- return { error: `Failed to communicate with page: ${retryError.message}` };
+ if (retryError?.name === 'AbortError') throw retryError;
+ if (this._isMissingContentReceiverError(retryError)) dispatchState.started = false;
+ return this._contentActionCommunicationFailure('click_ax', retryError, dispatchState.started);
}
}
const syntheticClickStartedAt = Number(response?._syntheticClickStartedAt);
@@ -38702,6 +38767,40 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
};
}
+ // A possibly delivered trusted click whose effect was not verified locks
+ // its exact document|ref for the rest of the task. The per-run lock set is
+ // cleared at every run boundary, including trusted Continue and recovery
+ // Retry, which reuse the task token; a new task (new token) drops these.
+ _taskClickLockKeys(tabId) {
+ const token = this._taskTokens?.get(tabId);
+ const entry = this._taskClickLocks?.get(tabId);
+ if (!entry) return null;
+ if (typeof token !== 'string' || !token || entry.taskToken !== token) {
+ // A different task must re-observe; it does not inherit old locks.
+ if (typeof token === 'string' && token) this._taskClickLocks?.delete(tabId);
+ return null;
+ }
+ return entry.keys;
+ }
+
+ _rememberTaskClickLock(tabId, key) {
+ const token = this._taskTokens?.get(tabId);
+ if (typeof token !== 'string' || !token || !key) return;
+ this._taskClickLocks ??= new Map();
+ let entry = this._taskClickLocks.get(tabId);
+ if (!entry || entry.taskToken !== token) {
+ entry = { taskToken: token, keys: new Set() };
+ this._taskClickLocks.set(tabId, entry);
+ }
+ // Bounded: the oldest lock yields only after many distinct unresolved clicks.
+ if (entry.keys.size >= 256) entry.keys.delete(entry.keys.values().next().value);
+ entry.keys.add(key);
+ }
+
+ _releaseTaskClickLock(tabId, key) {
+ this._taskClickLocks?.get(tabId)?.keys.delete(key);
+ }
+
async _readTextMutationEnrichment(operation, signal) {
if (!signal) return operation();
this._throwIfAborted(signal);
@@ -38915,15 +39014,15 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
} catch {}
};
throwIfEarlyCdpAborted();
- markEarlyCdpDispatched();
try {
- await cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
+ // Dispatch starts only once the fence passed; a vetoed key is unsent.
+ await sendTrustedCdpInput(options => cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
type: 'keyDown',
...keyParams,
...(commands.length ? { commands } : {}),
- });
+ }, '', options), { beforeSend: throwIfEarlyCdpAborted, onSent: markEarlyCdpDispatched });
} catch (error) {
- if (earlyCdpAbortSignal?.aborted) {
+ if (earlyCdpAbortSignal?.aborted && !isCdpCommandNotSentError(error)) {
await releaseKey();
throwIfEarlyCdpAborted();
}
@@ -44267,19 +44366,20 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
if (recipientValidation?.success !== true) return recipientValidation;
}
throwIfEarlyCdpAborted();
- markEarlyCdpDispatched();
- let textPointerPressed = false;
+ let textPressSent = false;
+ let textPointerReleased = false;
try {
- await cdpClient.dispatchMouseEvent(tabId, 'mousePressed', info.x, info.y);
- textPointerPressed = true;
+ // Dispatch starts only once the press passed its fence; a vetoed
+ // press leaves this click undispatched.
+ await sendTrustedCdpInput(options => cdpClient.dispatchMouseEvent(tabId, 'mousePressed', info.x, info.y, 'left', options),
+ { beforeSend: throwIfEarlyCdpAborted, onSent: () => { textPressSent = true; markEarlyCdpDispatched(); } });
throwIfEarlyCdpAborted();
await cdpClient.dispatchMouseEvent(tabId, 'mouseReleased', info.x, info.y);
- textPointerPressed = false;
+ textPointerReleased = true;
throwIfEarlyCdpAborted();
} catch (error) {
- if (earlyCdpAbortSignal?.aborted && textPointerPressed) {
- await cancelEarlyCdpPressedPointer();
- }
+ // A press whose release was not acknowledged may still be held.
+ if (textPressSent && !textPointerReleased) await cancelEarlyCdpPressedPointer();
throw error;
}
const blockedFileInput = await cdpClient.consumeFileInputClickGuard(tabId);
@@ -44393,9 +44493,10 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
if (validation?.success !== true) return validation;
}
throwIfEarlyCdpAborted();
- markEarlyCdpDispatched();
return { success: true };
},
+ // Marked at the transport boundary, after the press fence passed.
+ onDispatch: markEarlyCdpDispatched,
});
if ((selResult?.noDispatch === true || selResult?.dispatched === false) && selResult?.outcomeUnknown !== true) {
earlyCdpDispatchState.started = false;
@@ -44578,19 +44679,18 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
if (recipientValidation?.success !== true) return recipientValidation;
}
throwIfEarlyCdpAborted();
- markEarlyCdpDispatched();
- let coordinatePointerPressed = false;
+ let coordinatePressSent = false;
+ let coordinatePointerReleased = false;
try {
- await cdpClient.dispatchMouseEvent(tabId, 'mousePressed', clickX, clickY);
- coordinatePointerPressed = true;
+ await sendTrustedCdpInput(options => cdpClient.dispatchMouseEvent(tabId, 'mousePressed', clickX, clickY, 'left', options),
+ { beforeSend: throwIfEarlyCdpAborted, onSent: () => { coordinatePressSent = true; markEarlyCdpDispatched(); } });
throwIfEarlyCdpAborted();
await cdpClient.dispatchMouseEvent(tabId, 'mouseReleased', clickX, clickY);
- coordinatePointerPressed = false;
+ coordinatePointerReleased = true;
throwIfEarlyCdpAborted();
} catch (error) {
- if (earlyCdpAbortSignal?.aborted && coordinatePointerPressed) {
- await cancelEarlyCdpPressedPointer();
- }
+ // A press whose release was not acknowledged may still be held.
+ if (coordinatePressSent && !coordinatePointerReleased) await cancelEarlyCdpPressedPointer();
throw error;
}
const blockedFileInput = await cdpClient.consumeFileInputClickGuard(tabId);
@@ -44650,6 +44750,25 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
// index-based: fall through to content-script path which knows the
// interactive-elements ordering.
} catch (e) {
+ if (e?.code === 'content_action_timeout' || e?.name === 'AbortError') throw e;
+ // A fence veto before any press reached the page is an undispatched
+ // action; the page-feedback wrapper reports it with its diagnostics.
+ if (e?.code === 'page_feedback_pending' && earlyCdpDispatchState.started !== true) throw e;
+ if (earlyCdpDispatchState.started === true) {
+ // Input was sent (or its acknowledgement was lost): a later failure,
+ // such as a post-click read on a navigated document, cannot make it
+ // undispatched. Observe before any further click.
+ return this._withCoordinateReconciliation({
+ success: false,
+ dispatched: true,
+ outcomeUnknown: true,
+ mutationMayHaveOccurred: true,
+ verified: false,
+ retryable: false,
+ ...(e?.code === 'page_feedback_pending' ? { pageFeedbackPending: true } : {}),
+ error: `The click input was sent but its outcome is unknown (${e.message}). Inspect the page before deciding whether to click again.`,
+ }, coordinateDiagnostic);
+ }
return this._withCoordinateReconciliation(
{ success: false, error: `Click failed: ${e.message}` },
coordinateDiagnostic,
@@ -44658,7 +44777,6 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
}
if (name === 'type_text') {
- let dispatched = false;
try {
await cdpClient.attach(tabId);
throwIfEarlyCdpAborted();
@@ -44709,7 +44827,6 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
error: 'Focused select target could not be prepared.',
};
}
- dispatched = true;
await dispatchEarlyCdpKeyPress({
key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27,
});
@@ -44760,7 +44877,6 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
if (args.clear) {
const selectAllModifiers = await cdpClient.selectAllModifier(tabId);
throwIfEarlyCdpAborted();
- dispatched = true;
await dispatchEarlyCdpKeyPress({
key: 'a', code: 'KeyA', modifiers: selectAllModifiers, windowsVirtualKeyCode: 65, commands: ['selectAll'],
});
@@ -44784,10 +44900,9 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
}
throwIfEarlyCdpAborted();
}
- dispatched = true;
- markEarlyCdpDispatched();
const nativeText = String(args.text ?? '').replace(/\r\n?/g, '\n');
- await cdpClient.sendCommand(tabId, 'Input.insertText', { text: nativeText });
+ await sendTrustedCdpInput(options => cdpClient.sendCommand(tabId, 'Input.insertText',
+ { text: nativeText }, '', options), { beforeSend: throwIfEarlyCdpAborted, onSent: markEarlyCdpDispatched });
throwIfEarlyCdpAborted();
const verification = await sendFocusedTarget('verify_focused_type_dispatch', {
text: nativeText,
@@ -44824,12 +44939,16 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
};
} finally { await clearReceipt?.dispose(); }
} catch (error) {
+ // Dispatch state comes from the transport boundary: a fence veto
+ // before any key or text reached the page is not an uncertain write.
+ const inputSent = earlyCdpDispatchState.started === true;
+ if (error?.code === 'page_feedback_pending' && !inputSent) throw error;
return {
success: false,
- dispatched,
- ...(dispatched ? {} : { noDispatch: true }),
- retryable: !dispatched,
- error: dispatched
+ dispatched: inputSent,
+ ...(inputSent ? {} : { noDispatch: true }),
+ retryable: !inputSent,
+ error: inputSent
? `Focused typing outcome became uncertain: ${error?.message || String(error)}`
: `Focused typing was not dispatched: ${error?.message || String(error)}`,
};
@@ -44993,7 +45112,6 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
}
// Close any open native dropdown first
- dispatched = true;
await dispatchEarlyCdpKeyPress({
key: 'Escape', code: 'Escape', windowsVirtualKeyCode: 27,
});
@@ -45068,7 +45186,6 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
if (args.clear) {
const selectAllModifiers = await cdpClient.selectAllModifier(tabId);
throwIfEarlyCdpAborted();
- dispatched = true;
await dispatchEarlyCdpKeyPress({
key: 'a', code: 'KeyA', modifiers: selectAllModifiers, windowsVirtualKeyCode: 65, commands: ['selectAll'],
});
@@ -45091,10 +45208,9 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
}
throwIfEarlyCdpAborted();
}
- dispatched = true;
- markEarlyCdpDispatched();
const nativeText = String(args.text ?? '').replace(/\r\n?/g, '\n');
- await cdpClient.sendCommand(tabId, 'Input.insertText', { text: nativeText });
+ await sendTrustedCdpInput(options => cdpClient.sendCommand(tabId, 'Input.insertText',
+ { text: nativeText }, '', options), { beforeSend: throwIfEarlyCdpAborted, onSent: markEarlyCdpDispatched });
throwIfEarlyCdpAborted();
const verified = await cdpClient.verifyTextEntry(tabId, {
focused: true,
@@ -45134,9 +45250,13 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
error: 'type_text: internal — no branch matched. Provide {text} (with focused field) or {selector, text}.',
};
} catch (e) {
+ // Keys and text mark dispatch at the transport boundary. A fence veto
+ // before any input reached the page stays undispatched (no text debt).
+ const typeDispatched = earlyCdpDispatchState.started === true;
+ if (e?.code === 'page_feedback_pending' && !typeDispatched) throw e;
return {
success: false,
- ...(dispatched
+ ...(typeDispatched
? { dispatched: true }
: { dispatched: false, noDispatch: true }),
error: `Type failed: ${e.message}`,
@@ -45173,6 +45293,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
let guardedTargetConsumed = false;
let messageRecipientConsumed = false;
let keyDispatchAttempted = false;
+ let deliveredKeys = 0;
try {
await cdpClient.attach(tabId);
throwIfEarlyCdpAborted();
@@ -45220,21 +45341,19 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
';': { code: 'Semicolon', windowsVirtualKeyCode: 186 },
}[key];
+ const keyEvent = (type, options) => cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
+ type,
+ key,
+ code: keyMeta.code,
+ windowsVirtualKeyCode: keyMeta.windowsVirtualKeyCode,
+ }, '', options);
for (let i = 0; i < repeat; i++) {
- keyDispatchAttempted = true;
- markEarlyCdpDispatched();
- await cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
- type: 'keyDown',
- key,
- code: keyMeta.code,
- windowsVirtualKeyCode: keyMeta.windowsVirtualKeyCode,
- });
- await cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
- type: 'keyUp',
- key,
- code: keyMeta.code,
- windowsVirtualKeyCode: keyMeta.windowsVirtualKeyCode,
+ await sendTrustedCdpInput(options => keyEvent('keyDown', options), {
+ beforeSend: throwIfEarlyCdpAborted,
+ onSent: () => { keyDispatchAttempted = true; markEarlyCdpDispatched(); },
});
+ await keyEvent('keyUp');
+ deliveredKeys++;
throwIfEarlyCdpAborted();
}
@@ -45248,16 +45367,41 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
earlyCdpAbortSignal,
);
} catch (e) {
- if (guardedTargetConsumed || messageRecipientConsumed) {
+ const guarded = guardedTargetConsumed || messageRecipientConsumed;
+ if (keyDispatchAttempted) {
+ // A trusted key may already have reached the page. Replaying the
+ // requested keys synthetically would repeat them, so report how many
+ // complete key presses were acknowledged and leave the outcome open.
+ if (e?.code === 'content_action_timeout' || e?.name === 'AbortError') throw e;
return {
success: false,
- dispatched: keyDispatchAttempted,
- ...(keyDispatchAttempted ? {} : { noDispatch: true, retryable: true }),
- error: keyDispatchAttempted
- ? `Keyboard dispatch became uncertain after the guarded target was revalidated: ${e.message || e}`
- : `Keyboard dispatch failed after the guarded target was revalidated: ${e.message || e}`,
+ dispatched: true,
+ outcomeUnknown: true,
+ mutationMayHaveOccurred: true,
+ verified: false,
+ retryable: false,
+ method: 'cdp-key',
+ key,
+ repeat,
+ deliveredKeys,
+ ...(e?.code === 'page_feedback_pending' ? { pageFeedbackPending: true } : {}),
+ error: guarded
+ ? `Keyboard dispatch became uncertain after the guarded target was revalidated (${deliveredKeys} of ${repeat} key presses acknowledged): ${e.message || e}`
+ : `Keyboard input became uncertain after ${deliveredKeys} of ${repeat} ${key} key presses were acknowledged (${e.message || e}). Inspect the page before sending more keys.`,
};
}
+ if (guarded) {
+ return {
+ success: false,
+ dispatched: false,
+ noDispatch: true,
+ retryable: true,
+ error: `Keyboard dispatch failed after the guarded target was revalidated: ${e.message || e}`,
+ };
+ }
+ // A fence veto before any key stays undispatched; the page-feedback
+ // wrapper reports it. It is never converted into synthetic keys.
+ if (e?.code === 'page_feedback_pending') throw e;
// Fall through to content-script path if CDP is unavailable.
}
}
@@ -46408,6 +46552,14 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
return { expectedModelSnapshot: call.expectedModelSnapshot,
relaxedMonitorGrantToken: call.relaxedMonitorGrantToken, allowPassiveRebase: true };
};
+ // Trusted input is marked only once its fence passed (or its rejection
+ // cannot be placed before the protocol call); a vetoed command is unsent.
+ const markTrustedDispatched = () => { trustedDispatched = true; };
+ const markTextAttempted = () => { trustedDispatched = true; trustedTextAttempted = true; };
+ const sendTrusted = (method, params, onSent) => sendTrustedCdpInput(
+ options => cdpClient.sendCommand(tabId, method, params, '', options),
+ { beforeSend: () => this._throwIfAborted(abortSignal), onSent },
+ );
const finishPreparationFailure = async failure => {
if (!trustedDispatched || trustedTextAttempted || !beforePreparation?.documentToken || abortSignal?.aborted) return failure;
const afterPreparation = await this._textMutationValueDigest(tabId, digestTarget);
@@ -46459,10 +46611,9 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
type: 'mouseMoved', x, y, button: 'none', buttons: 0,
});
this._throwIfAborted(abortSignal);
- trustedDispatched = true;
- await cdpClient.sendCommand(tabId, 'Input.dispatchMouseEvent', {
+ await sendTrusted('Input.dispatchMouseEvent', {
type: 'mousePressed', x, y, button: 'left', buttons: 1, clickCount: 1,
- });
+ }, markTrustedDispatched);
await cdpClient.sendCommand(tabId, 'Input.dispatchMouseEvent', {
type: 'mouseReleased', x, y, button: 'left', buttons: 0, clickCount: 1,
});
@@ -46489,7 +46640,6 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
}
prepared = reselected;
}
- trustedDispatched = true;
const appendingContentEditable = prepared.contentEditable && !clearsExisting;
// Canonicalize before native insertion as well as readback: raw CRLF
// becomes CR spaces plus LF paragraphs in browser-managed editors.
@@ -46497,12 +46647,13 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
? (typeof args?.text === 'string' ? args.text : '')
: expected).replace(/\r\n?/g, '\n');
this._throwIfAborted(abortSignal);
- // A lost response can precede a delayed applied write. After this
- // boundary an unchanged sample cannot make the text command retry-safe.
- trustedTextAttempted = true;
+ // A lost response can precede a delayed applied write. Once the first
+ // text-changing command passes its fence, an unchanged sample cannot
+ // make it retry-safe; a vetoed command leaves the editor untouched.
const pageHandledPaste = prepared.editorKind === 'draftjs' && /[\r\n]/.test(trustedText);
let verification;
if (pageHandledPaste) {
+ markTextAttempted();
verification = await sendContent({
target: 'content', action: 'ax_paste_draft',
params: { ref_id: args.ref_id, text: trustedText, clear: clearsExisting },
@@ -46514,7 +46665,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
}
} else {
if (trustedText || appendingContentEditable) {
- await cdpClient.sendCommand(tabId, 'Input.insertText', { text: trustedText });
+ await sendTrusted('Input.insertText', { text: trustedText }, markTextAttempted);
} else {
// Native chat knows that an explicitly deleted caret is an empty
// message. Keep that proof private and scoped; generic BRs stay LF.
@@ -46529,9 +46680,9 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
if (nativeClearReceipt) {
const modifiers = await cdpClient.selectAllModifier(tabId);
this._throwIfAborted(abortSignal);
- await cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
+ await sendTrusted('Input.dispatchKeyEvent', {
type: 'keyDown', key: 'a', code: 'KeyA', modifiers, windowsVirtualKeyCode: 65, commands: ['selectAll'],
- });
+ }, markTextAttempted);
await cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
type: 'keyUp', key: 'a', code: 'KeyA', modifiers, windowsVirtualKeyCode: 65,
});
@@ -46539,9 +46690,9 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
}
}
this._throwIfAborted(abortSignal);
- await cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
+ await sendTrusted('Input.dispatchKeyEvent', {
type: 'keyDown', key: 'Delete', code: 'Delete', windowsVirtualKeyCode: 46,
- });
+ }, markTextAttempted);
await cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
type: 'keyUp', key: 'Delete', code: 'Delete', windowsVirtualKeyCode: 46,
});
@@ -46640,10 +46791,9 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
this._throwIfAborted(abortSignal);
let enterDispatched = false;
try {
- await cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
+ await sendTrusted('Input.dispatchKeyEvent', {
type: 'keyDown', key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13,
- });
- enterDispatched = true;
+ }, () => { enterDispatched = true; });
await cdpClient.sendCommand(tabId, 'Input.dispatchKeyEvent', {
type: 'keyUp', key: 'Enter', code: 'Enter', windowsVirtualKeyCode: 13,
});
@@ -46942,7 +47092,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
const fallbackKey = `${target.documentToken || 'document'}|${args?.ref_id || ''}`;
const attempted = this._clickAxCdpFallbacks.get(tabId) || new Set();
- if (attempted.has(fallbackKey)) {
+ if (attempted.has(fallbackKey) || this._taskClickLockKeys(tabId)?.has(fallbackKey)) {
if (timing) timing.duplicateFallbackBlocked = true;
return withSnapshot({
...response,
@@ -46982,6 +47132,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
let dispatchStage = 'attach';
let dispatchedEvents = 0;
let pressedDelivered = false;
+ let pointerReleased = false;
try {
this._throwIfAborted(abortSignal);
await this._measureClickAxStage(timing, 'fallbackPreparationMs', () => cdpClient.attach(tabId));
@@ -47006,27 +47157,29 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
await this._measureClickAxStage(
timing,
'fallbackDispatchMs',
- () => cdpClient.dispatchMouseEvent(tabId, 'mousePressed', target.x, target.y),
+ () => sendTrustedCdpInput(
+ options => cdpClient.dispatchMouseEvent(tabId, 'mousePressed', target.x, target.y, 'left', options),
+ { beforeSend: () => this._throwIfAborted(abortSignal), onSent: () => {
+ // Reserve the one-shot slot before the press can reach the page:
+ // a press is partial user input, and a lost acknowledgement is not
+ // permission to replay it or the synthetic click before it.
+ pressedDelivered = true;
+ attempted.add(fallbackKey);
+ this._clickAxCdpFallbacks.set(tabId, attempted);
+ this._rememberTaskClickLock(tabId, fallbackKey);
+ } },
+ ),
);
dispatchedEvents++;
if (timing) timing.trustedInputEvents = dispatchedEvents;
- pressedDelivered = true;
- // A confirmed press is partial user input; from this point onward a
- // retry could double-activate or leave mismatched pointer state.
- attempted.add(fallbackKey);
- this._clickAxCdpFallbacks.set(tabId, attempted);
- try {
- this._throwIfAborted(abortSignal);
- } catch (error) {
- await this._cancelTrustedClickPress(tabId);
- throw error;
- }
+ this._throwIfAborted(abortSignal);
dispatchStage = 'mouseReleased';
await this._measureClickAxStage(
timing,
'fallbackDispatchMs',
() => cdpClient.dispatchMouseEvent(tabId, 'mouseReleased', target.x, target.y),
);
+ pointerReleased = true;
dispatchedEvents++;
if (timing) timing.trustedInputEvents = dispatchedEvents;
this._throwIfAborted(abortSignal);
@@ -47052,12 +47205,15 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
}
dispatchStage = 'complete';
} catch (error) {
+ // A press whose release was not acknowledged may still be held.
+ if (pressedDelivered && !pointerReleased) await this._cancelTrustedClickPress(tabId);
if (error?.code === 'content_action_timeout') throw error;
- const fallbackAttempted = dispatchedEvents > 0;
+ const fallbackAttempted = dispatchedEvents > 0 || pressedDelivered;
return withSnapshot({
...response,
success: false,
dispatched: true,
+ ...(pressedDelivered ? { outcomeUnknown: true } : {}),
noProgress: pressedDelivered || undefined,
fallback: 'cdp_after_synthetic_no_progress',
fallbackAttempted,
@@ -47114,6 +47270,7 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
);
}
if (trustedTargetStateChanged || trustedTargetWeakStateChanged) {
+ this._releaseTaskClickLock(tabId, fallbackKey);
return withSnapshot({
...response,
success: true,
@@ -47166,6 +47323,9 @@ If the user has already named or confirmed this exact recipient, do NOT ask agai
}, trustedObservation);
}
+ // A verified effect reconciles the trusted click for later runs of this
+ // task; the per-run one-shot slot still stays used for this run.
+ this._releaseTaskClickLock(tabId, fallbackKey);
return withSnapshot({
...response,
success: true,
diff --git a/src/chrome/src/cdp/cdp-client.js b/src/chrome/src/cdp/cdp-client.js
index 507c2fe6f..86d565184 100644
--- a/src/chrome/src/cdp/cdp-client.js
+++ b/src/chrome/src/cdp/cdp-client.js
@@ -37,6 +37,17 @@ function throwIfPageFeedbackPending(result) {
error.code = 'page_feedback_pending'; throw error;
}
+// Rejections raised before a command could reach chrome.debugger: the session
+// check, the receiving-target and page-feedback fences, and the caller's last
+// beforeSend check. Any other rejection can follow an applied Input command
+// (a lost acknowledgement), so callers must treat it as possibly delivered.
+const COMMAND_NOT_SENT_ERRORS = new WeakSet();
+export function isCdpCommandNotSentError(error) {
+ if (!error || typeof error !== 'object') return false;
+ // The dispatch fence rejects before any protocol command is issued.
+ return COMMAND_NOT_SENT_ERRORS.has(error) || error.code === 'page_feedback_pending';
+}
+
function readProseMirrorText(el) {
if (!el?.isContentEditable || !el.classList?.contains('ProseMirror')) return null;
// Paragraphs are document line breaks, not innerText's visual spacing.
@@ -822,7 +833,39 @@ export class CDPClient {
/**
* Send a CDP command and get the result.
*/
- async sendCommand(tabId, method, params = {}, sessionId = '') {
+ async sendCommand(tabId, method, params = {}, sessionId = '', { beforeSend = null } = {}) {
+ try {
+ await this._beforeCommand(tabId, method, params, sessionId);
+ // Last synchronous point before the command can reach the page. Callers
+ // record dispatch (and recheck cancellation) only after the fence passed.
+ if (typeof beforeSend === 'function') beforeSend();
+ } catch (error) {
+ if (error && typeof error === 'object') COMMAND_NOT_SENT_ERRORS.add(error);
+ throw error;
+ }
+
+ return new Promise((resolve, reject) => {
+ chrome.debugger.sendCommand(
+ { tabId, ...(sessionId ? { sessionId } : {}) },
+ method,
+ params,
+ (result) => {
+ // Chrome extension APIs expose callback failures through
+ // chrome.runtime.lastError, not a second callback argument. Read it
+ // synchronously while the callback is active; Chrome clears it after
+ // the callback returns.
+ const error = chrome.runtime.lastError;
+ if (error) {
+ reject(new Error(error.message || String(error)));
+ return;
+ }
+ resolve(result);
+ },
+ );
+ });
+ }
+
+ async _beforeCommand(tabId, method, params, sessionId) {
if (!this.sessions.has(tabId)) {
throw new Error(`Not attached to tab ${tabId}`);
}
@@ -850,26 +893,6 @@ export class CDPClient {
&& params.button === 'none' && params.buttons === 0,
});
}
-
- return new Promise((resolve, reject) => {
- chrome.debugger.sendCommand(
- { tabId, ...(sessionId ? { sessionId } : {}) },
- method,
- params,
- (result) => {
- // Chrome extension APIs expose callback failures through
- // chrome.runtime.lastError, not a second callback argument. Read it
- // synchronously while the callback is active; Chrome clears it after
- // the callback returns.
- const error = chrome.runtime.lastError;
- if (error) {
- reject(new Error(error.message || String(error)));
- return;
- }
- resolve(result);
- },
- );
- });
}
async _pageAgentObjectTarget(tabId, objectId, sessionId = '') {
@@ -3596,7 +3619,7 @@ export class CDPClient {
/**
* Dispatch mouse event.
*/
- async dispatchMouseEvent(tabId, type, x, y, button = 'left') {
+ async dispatchMouseEvent(tabId, type, x, y, button = 'left', options = {}) {
// Use string button names as required by CDP Input.dispatchMouseEvent.
// 'buttons' is a bitmask: 1 = left held. clickCount must be 1 on BOTH
// mousePressed AND mouseReleased for the browser to synthesize a 'click'
@@ -3610,7 +3633,7 @@ export class CDPClient {
button: isMove ? 'none' : button,
buttons: isDown ? 1 : 0,
clickCount: isMove ? 0 : 1,
- });
+ }, '', options);
}
/**
@@ -4984,7 +5007,16 @@ export class CDPClient {
}
// Step 1: real mouse events at center coordinates.
+ // dispatchAttempted means a click-producing command may have reached the
+ // page. It is set at the transport's last pre-protocol point (after the
+ // page-feedback fence), and also for any rejection that cannot be placed
+ // before that point, because a lost acknowledgement can follow delivery.
let dispatchAttempted = false;
+ const markDispatchAttempted = () => {
+ if (dispatchAttempted) return;
+ if (typeof options?.onDispatch === 'function') options.onDispatch();
+ dispatchAttempted = true;
+ };
const pageDeadlineResult = (priorDispatchAttempted, error) => priorDispatchAttempted
? {
success: false,
@@ -5015,15 +5047,16 @@ export class CDPClient {
};
};
if (info.inViewport && info.hitOk) {
+ const rect = {
+ x: Math.round(info.x - (info.width || 1) / 2),
+ y: Math.round(info.y - (info.height || 1) / 2),
+ w: Math.round(info.width || 1),
+ h: Math.round(info.height || 1),
+ };
+ let released = false;
try {
await this.armFileInputClickGuard(tabId);
throwIfAborted();
- const rect = {
- x: Math.round(info.x - (info.width || 1) / 2),
- y: Math.round(info.y - (info.height || 1) / 2),
- w: Math.round(info.width || 1),
- h: Math.round(info.height || 1),
- };
await this.sendCommand(tabId, 'Input.dispatchMouseEvent', {
type: 'mouseMoved', x: info.x, y: info.y, button: 'none', buttons: 0,
});
@@ -5035,20 +5068,21 @@ export class CDPClient {
rect,
});
if (validation.success !== true) return validation;
- dispatchAttempted = true;
- await this.sendCommand(tabId, 'Input.dispatchMouseEvent', {
- type: 'mousePressed', x: info.x, y: info.y, button: 'left', buttons: 1, clickCount: 1,
- });
+ let pressChecked = false;
try {
- throwIfAborted();
+ await this.sendCommand(tabId, 'Input.dispatchMouseEvent', {
+ type: 'mousePressed', x: info.x, y: info.y, button: 'left', buttons: 1, clickCount: 1,
+ }, '', { beforeSend: () => { pressChecked = true; throwIfAborted(); markDispatchAttempted(); } });
} catch (error) {
- if (error?.code === 'page_feedback_pending') throw error;
- await cancelPressedPointer();
+ if (!pressChecked && !isCdpCommandNotSentError(error)) markDispatchAttempted();
throw error;
}
+ markDispatchAttempted();
+ throwIfAborted();
await this.sendCommand(tabId, 'Input.dispatchMouseEvent', {
type: 'mouseReleased', x: info.x, y: info.y, button: 'left', buttons: 0, clickCount: 1,
});
+ released = true;
throwIfAborted();
const blockedFileInput = await this.consumeFileInputClickGuard(tabId);
if (blockedFileInput?.blocked) {
@@ -5069,9 +5103,31 @@ export class CDPClient {
rect,
};
} catch (e) {
+ if (dispatchAttempted) {
+ // The trusted press may already have activated the target. A DOM or
+ // JS fallback would be a second activation reusing the consumed
+ // dispatch authorization, so report the possibly delivered click.
+ if (!released) await cancelPressedPointer();
+ if (actionExpired()) throwIfAborted();
+ if (e?.code === 'page_feedback_pending') throw e;
+ return {
+ success: false,
+ dispatched: true,
+ outcomeUnknown: true,
+ retryable: false,
+ trusted: true,
+ method: info.viaCDP ? 'cdp-mouse-closed-shadow' : 'cdp-mouse',
+ tag: info.tag,
+ text: info.text,
+ x: info.x,
+ y: info.y,
+ rect,
+ error: `The trusted click was sent but its acknowledgement was lost (${e?.message || String(e)}). Inspect the page before deciding whether to click again; no fallback click was sent.`,
+ };
+ }
if (e?.code === 'page_feedback_pending') throw e;
if (actionExpired()) throwIfAborted();
- // fall through to fallback
+ // No press reached the protocol: fall through to the fallback.
}
}
@@ -5105,7 +5161,7 @@ export class CDPClient {
const validation = await authorizeDispatch({ x: info.x, y: info.y, tag: info.tag });
if (validation.success !== true) return validation;
const pageGuard = await beforePageAgentDispatch(globalThis.chrome, tabId, { kind: 'click', selector, x: info.x, y: info.y });
- dispatchAttempted = true;
+ let clickChecked = false;
const clicked = await this.sendCommand(tabId, 'Runtime.callFunctionOn', {
objectId,
functionDeclaration: `function(actionDeadlineAt, pageGuard) {
@@ -5131,6 +5187,9 @@ export class CDPClient {
arguments: [{ value: deadlineAt }, { value: pageGuard || null }],
returnByValue: true,
awaitPromise: false,
+ }, '', { beforeSend: () => { clickChecked = true; throwIfAborted(); markDispatchAttempted(); } }).catch(error => {
+ if (!clickChecked && !isCdpCommandNotSentError(error)) markDispatchAttempted();
+ throw error;
});
if (clicked?.result?.value?.pageFeedbackPending === true) {
await this.consumeFileInputClickGuard(tabId);
@@ -5166,6 +5225,14 @@ export class CDPClient {
} catch (e) {
if (e?.code === 'page_feedback_pending') throw e;
if (actionExpired()) throwIfAborted();
+ // A node click whose response was lost may already have run.
+ if (dispatchAttempted) {
+ return {
+ success: false, dispatched: true, outcomeUnknown: true, retryable: false,
+ method: 'cdp-node-click',
+ error: `The node click was sent but its response was lost (${e?.message || String(e)}). Inspect the page before deciding whether to click again; no fallback click was sent.`,
+ };
+ }
// fall through
} finally {
if (objectId) {
@@ -5182,7 +5249,8 @@ export class CDPClient {
if (fallbackValidation.success !== true) return fallbackValidation;
const priorDispatchAttempted = dispatchAttempted;
const pageGuard = await beforePageAgentDispatch(globalThis.chrome, tabId, { kind: 'click', selector, x: info.x, y: info.y });
- dispatchAttempted = true;
+ throwIfAborted();
+ markDispatchAttempted();
const fb = await this.evaluateFunction(tabId, `function (sel, actionDeadlineAt, pageGuard) {
${PAGE_AGENT_DOM_ACTION_SOURCE}
const deadlineExpired = () => actionDeadlineAt > 0 && Date.now() >= actionDeadlineAt;
@@ -5763,9 +5831,29 @@ export class CDPClient {
error.name = 'AbortError';
throw error;
};
- const markDispatch = () => {
- throwIfAborted();
+ // Dispatch is recorded once the page-feedback fence has passed, immediately
+ // before an input command can reach the page, and for any rejection the
+ // transport cannot place before that point. A vetoed command stays unsent.
+ let inputSent = false;
+ const markSent = () => {
+ // The caller's hook may refuse (owner ended); then nothing was sent.
if (beforeDispatch) beforeDispatch();
+ inputSent = true;
+ };
+ const sendInput = async (method, params) => {
+ let checked = false;
+ try {
+ const result = await this.sendCommand(tabId, method, params, '', {
+ beforeSend: () => { checked = true; throwIfAborted(); markSent(); },
+ });
+ if (!inputSent) markSent();
+ return result;
+ } catch (error) {
+ // A rejection from our own last check, or one the transport places
+ // before the protocol call, sent nothing; any other may follow delivery.
+ if (!checked && !isCdpCommandNotSentError(error)) markSent();
+ throw error;
+ }
};
const cancelPressedPointer = async () => {
try {
@@ -5789,16 +5877,15 @@ export class CDPClient {
});
} catch {}
};
- markDispatch();
try {
- await this.sendCommand(tabId, 'Input.dispatchKeyEvent', {
+ await sendInput('Input.dispatchKeyEvent', {
type: 'keyDown',
...keyParams,
...(commands.length ? { commands } : {}),
});
} catch (error) {
if (error?.code === 'page_feedback_pending') throw error;
- if (actionExpired()) {
+ if (actionExpired() && !isCdpCommandNotSentError(error)) {
// A delayed debugger response can mean keyDown reached the page even
// though the action deadline already expired. Release defensively so
// later user or agent input cannot inherit a stuck key state.
@@ -6151,7 +6238,8 @@ export class CDPClient {
});
throwIfAborted();
dispatched = true;
- markDispatch();
+ // Focusing the field is preparation: only text-changing commands
+ // below record text dispatch for the caller.
await this.sendCommand(tabId, 'Input.dispatchMouseEvent', {
type: 'mousePressed', x: info.x, y: info.y, button: 'left', buttons: 1, clickCount: 1,
});
@@ -6259,12 +6347,14 @@ export class CDPClient {
} catch (e) {
if (e?.code === 'page_feedback_pending') throw e;
if (actionExpired()) throwIfAborted();
+ // Only a select-all/Delete that may have reached the page can have
+ // changed the text; a refused key leaves the value untouched.
return {
success: false,
- dispatched,
- ...(dispatched ? {} : { noDispatch: true }),
+ dispatched: inputSent,
+ ...(inputSent ? {} : { noDispatch: true }),
verified: false,
- mutationMayHaveOccurred: dispatched,
+ mutationMayHaveOccurred: inputSent,
error: `Could not safely clear the text field: ${e?.message || String(e)}`,
};
}
@@ -6311,12 +6401,23 @@ export class CDPClient {
// Type via Input.insertText — atomic, fires beforeinput/input correctly.
try {
dispatched = true;
- markDispatch();
- await this.sendCommand(tabId, 'Input.insertText', { text: insertionText });
+ await sendInput('Input.insertText', { text: insertionText });
throwIfAborted();
} catch (e) {
if (e?.code === 'page_feedback_pending') throw e;
if (actionExpired()) throwIfAborted();
+ if (!inputSent) {
+ // Refused before the protocol call and no earlier input reached the
+ // page: nothing changed, so this is not an uncertain write.
+ return {
+ success: false,
+ dispatched: false,
+ noDispatch: true,
+ verified: false,
+ retryable: true,
+ error: `Text insertion was not sent: ${e?.message || String(e)}`,
+ };
+ }
return {
success: false,
dispatched: true,
diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.js
index b36ac550b..40491b123 100644
--- a/src/chrome/src/content/content.js
+++ b/src/chrome/src/content/content.js
@@ -8333,6 +8333,20 @@
}
// --- Message handler ---
+ // Stop finishes the owning page-feedback run (page_monitor_state
+ // active:false for that run) while a compound content action may still be
+ // in an asynchronous settle wait. Count those run endings so a later phase,
+ // such as the submit after a field write, never runs for a stopped task.
+ // A stale stop for another run leaves the current monitor active.
+ // The counter lives on this isolated world's window, shared by reinjected
+ // copies of this script and invisible to the page's own scripts.
+ chrome.runtime.onMessage.addListener((msg) => {
+ if (msg?.target === 'content' && msg.action === 'page_monitor_state' && msg.active === false
+ && window.__wbPageMonitor?.active !== true) {
+ window.__wbContentRunStopEpoch = (Number(window.__wbContentRunStopEpoch) || 0) + 1;
+ }
+ });
+
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.target !== 'content' || msg.action?.startsWith('page_monitor_')) return;
// Only the newest installed copy answers. An orphaned copy from a
@@ -9324,6 +9338,11 @@
}
},
'set_field': async () => {
+ // Captured synchronously when the action begins (see the run-stop listener).
+ const runOwner = { monitored: window.__wbPageMonitor?.active === true,
+ stopEpoch: Number(window.__wbContentRunStopEpoch) || 0 };
+ const runStopped = () => (Number(window.__wbContentRunStopEpoch) || 0) !== runOwner.stopEpoch
+ || (runOwner.monitored && window.__wbPageMonitor?.active !== true);
let dispatched = false;
let submissionDispatched = false;
let submissionBlockedBeforeDispatch = false;
@@ -9483,6 +9502,15 @@
const fallbackAttempted = false;
let nativeSubmitAttempted = false;
let submissionOutcomeUnknown = false;
+ // The field write already happened; report it honestly and send no
+ // submit for a run that was stopped during the settle wait.
+ const stoppedBeforeSubmit = () => failure(
+ 'Stopped before submitting. The field value was written, but no submit was sent.',
+ { cancelled: true, ref_id, rect, verified, fieldMeta, submitted: false, submissionDispatched: false,
+ submissionBlockedBeforeDispatch: true, submissionBlockReason: 'run_stopped',
+ outcomeUnknown: !verified, retryable: false },
+ );
+ if (submit && verified && runStopped()) return stoppedBeforeSubmit();
if (submit && verified) {
if (actionDeadlineExpired()) return deadlineFailure();
submissionOutcomeUnknown = true;
@@ -9608,11 +9636,14 @@
if (isCombobox) {
await new Promise(r => setTimeout(r, 80));
if (actionDeadlineExpired()) return deadlineFailure();
+ if (runStopped()) return stoppedBeforeSubmit();
const arrowResult = dispatchKeySequence('ArrowDown', 40);
if (!arrowResult.completedWithinDeadline) return deadlineFailure();
await new Promise(r => setTimeout(r, 30));
if (actionDeadlineExpired()) return deadlineFailure();
}
+ // The ArrowDown above was a pre-submit key; Enter is the submit.
+ if (runStopped()) return stoppedBeforeSubmit();
if (actionDeadlineExpired()) return deadlineFailure();
const enterResult = dispatchKeySequence('Enter', 13, true);
submissionDispatched = enterResult.dispatched;
diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.js
index 111b1e415..9b8649381 100644
--- a/src/firefox/src/agent/agent.js
+++ b/src/firefox/src/agent/agent.js
@@ -15330,10 +15330,19 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return await finish(await dispatch());
} catch (error) {
if (error?.code === 'content_action_timeout') {
- return this._contentActionTimeoutResult('click_ax', error);
+ return dispatchState.started ? this._contentActionTimeoutResult('click_ax', error)
+ : this._contentActionPreparationTimeoutResult('click_ax', error, 'click preparation');
+ }
+ // Stop keeps the real dispatch state for the lifecycle wrapper.
+ if (error?.name === 'AbortError') throw error;
+ throwIfAborted();
+ // Only the browser's explicit missing-receiver error proves that the
+ // click message reached no listener. A torn-down response conduit can
+ // follow a delivered el.click() that navigated or replaced its
+ // document; resending would click twice or misreport the delivery.
+ if (!this._isMissingContentReceiverError(error)) {
+ return this._contentActionCommunicationFailure('click_ax', error, dispatchState.started);
}
- // A rejected first message is the no-receiver path that triggers
- // injection; no page click was delivered unless the retry starts.
dispatchState.started = false;
try {
throwIfAborted();
@@ -15342,13 +15351,18 @@ Rules: no prose intro, no conclusion, no "this screenshot shows...", no layout d
return await finish(await dispatch());
} catch (retryError) {
if (retryError?.code === 'content_action_timeout') {
- return this._contentActionTimeoutResult('click_ax', retryError);
- }
- let pageUrl = '';
- try { pageUrl = (await browser.tabs.get(tabId))?.url || ''; } catch {}
- const accessFailure = firefoxHostPermissionFailure(pageUrl, retryError.message);
- if (accessFailure) return accessFailure;
- return { error: `Failed to communicate with page: ${retryError.message}` };
+ return dispatchState.started ? this._contentActionTimeoutResult('click_ax', retryError)
+ : this._contentActionPreparationTimeoutResult('click_ax', retryError, 'content-script injection');
+ }
+ if (retryError?.name === 'AbortError') throw retryError;
+ if (this._isMissingContentReceiverError(retryError)) dispatchState.started = false;
+ if (!dispatchState.started) {
+ let pageUrl = '';
+ try { pageUrl = (await browser.tabs.get(tabId))?.url || ''; } catch {}
+ const accessFailure = firefoxHostPermissionFailure(pageUrl, retryError.message);
+ if (accessFailure) return accessFailure;
+ }
+ return this._contentActionCommunicationFailure('click_ax', retryError, dispatchState.started);
}
}
}
diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.js
index 1a53e3559..af8b7e83c 100644
--- a/src/firefox/src/content/content.js
+++ b/src/firefox/src/content/content.js
@@ -7416,6 +7416,20 @@
}
// --- Message handler ---
+ // Stop finishes the owning page-feedback run (page_monitor_state
+ // active:false for that run) while a compound content action may still be
+ // in an asynchronous settle wait. Count those run endings so a later phase,
+ // such as the submit after a field write, never runs for a stopped task.
+ // A stale stop for another run leaves the current monitor active.
+ // The counter lives on this isolated world's window, shared by reinjected
+ // copies of this script and invisible to the page's own scripts.
+ browser.runtime.onMessage.addListener((msg) => {
+ if (msg?.target === 'content' && msg.action === 'page_monitor_state' && msg.active === false
+ && window.__wbPageMonitor?.active !== true) {
+ window.__wbContentRunStopEpoch = (Number(window.__wbContentRunStopEpoch) || 0) + 1;
+ }
+ });
+
browser.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.target !== 'content' || msg.action?.startsWith('page_monitor_')) return;
if (WEBBRAIN_GENERATION !== (Number(window.__webbrain_generation) || 0)) return;
@@ -8336,6 +8350,11 @@
}
},
'set_field': async () => {
+ // Captured synchronously when the action begins (see the run-stop listener).
+ const runOwner = { monitored: window.__wbPageMonitor?.active === true,
+ stopEpoch: Number(window.__wbContentRunStopEpoch) || 0 };
+ const runStopped = () => (Number(window.__wbContentRunStopEpoch) || 0) !== runOwner.stopEpoch
+ || (runOwner.monitored && window.__wbPageMonitor?.active !== true);
let dispatched = false;
let submissionDispatched = false;
let submissionBlockedBeforeDispatch = false;
@@ -8479,6 +8498,15 @@
let nativeSubmitAttempted = false;
let submissionOutcomeUnknown = false;
+ // The field write already happened; report it honestly and send no
+ // submit for a run that was stopped during the settle wait.
+ const stoppedBeforeSubmit = () => failure(
+ 'Stopped before submitting. The field value was written, but no submit was sent.',
+ { cancelled: true, ref_id, rect, verified, fieldMeta, submitted: false, submissionDispatched: false,
+ submissionBlockedBeforeDispatch: true, submissionBlockReason: 'run_stopped',
+ outcomeUnknown: !verified, retryable: false },
+ );
+ if (submit && verified && runStopped()) return stoppedBeforeSubmit();
if (submit && verified) {
if (actionDeadlineExpired()) return deadlineFailure();
submissionOutcomeUnknown = true;
@@ -8597,11 +8625,14 @@
if (isCombobox) {
await new Promise(r => setTimeout(r, 80));
if (actionDeadlineExpired()) return deadlineFailure();
+ if (runStopped()) return stoppedBeforeSubmit();
const arrowResult = dispatchKeySequence('ArrowDown', 40);
if (!arrowResult.completedWithinDeadline) return deadlineFailure();
await new Promise(r => setTimeout(r, 30));
if (actionDeadlineExpired()) return deadlineFailure();
}
+ // The ArrowDown above was a pre-submit key; Enter is the submit.
+ if (runStopped()) return stoppedBeforeSubmit();
if (actionDeadlineExpired()) return deadlineFailure();
const enterResult = dispatchKeySequence('Enter', 13, true);
submissionDispatched = enterResult.dispatched;
diff --git a/test/cdp-clear-receipt.mjs b/test/cdp-clear-receipt.mjs
index 7c52cfc94..560922f78 100644
--- a/test/cdp-clear-receipt.mjs
+++ b/test/cdp-clear-receipt.mjs
@@ -59,7 +59,10 @@ test('native generic editor clear receipts preserve literal breaks and reject in
const session = await context.newCDPSession(page);
let intervention = null, insertions = 0, nativeMarkers = false;
cdpClient.attach = async () => ({ attached: true });
- cdpClient.sendCommand = async (_tab, method, params) => {
+ cdpClient.sendCommand = async (_tab, method, params, _sessionId, options) => {
+ // Like the real transport, run the caller's last pre-protocol check
+ // (dispatch marking and owner validation) before the command is sent.
+ options?.beforeSend?.();
if (method === 'Input.insertText') insertions++;
if (nativeMarkers && method.startsWith('Input.')) {
const active = await session.send('Runtime.evaluate', { expression: 'document.activeElement' });
diff --git a/test/delivery-cdp-input.mjs b/test/delivery-cdp-input.mjs
new file mode 100644
index 000000000..744e66b8e
--- /dev/null
+++ b/test/delivery-cdp-input.mjs
@@ -0,0 +1,537 @@
+// Delivery regressions for Chrome trusted CDP input (C2-C5):
+// - press_keys never falls back to synthetic keys after a trusted key may have
+// reached the page, and reports how many key presses were acknowledged;
+// - CDP click reports delivered input as dispatched/outcome-unknown and
+// lets a page-feedback fence veto before any press stay undispatched;
+// - text input marks dispatch only after the fence passes, so a vetoed
+// insertion cannot create uncertain-text debt;
+// - cdpClient.clickElement never falls back to a DOM/JS click after a press.
+//
+// Two kinds of fixtures, labelled per test:
+// [real DOM] the real Agent and cdpClient drive real Chromium through a
+// Playwright CDP session bridged into chrome.debugger.sendCommand; the page
+// counts the events it actually received. Failures are injected only into
+// protocol acknowledgements (after the command reached the page) or before
+// the protocol call.
+// [mocked transport] the real Agent with cdpClient.sendCommand replaced; a
+// page_feedback_pending rejection models the dispatch fence, which rejects
+// before any protocol command is sent.
+import assert from 'node:assert/strict';
+import fs from 'node:fs';
+import { test } from 'node:test';
+import { chromium } from 'playwright';
+
+const area = { get: async () => ({}), set: async () => {}, remove: async () => {} };
+const api = {
+ storage: { local: area, session: area },
+ runtime: { getURL: value => `extension://test/${value}`, getManifest: () => ({ version: 'test' }), lastError: null },
+ tabs: { get: async () => ({ id: 17, url: 'https://cdp-delivery.test/start' }), query: async () => [{ id: 17 }] },
+ scripting: { executeScript: async () => [{}] },
+ debugger: { sendCommand: () => { throw new Error('debugger bridge not installed'); },
+ onEvent: { addListener() {}, removeListener() {} }, onDetach: { addListener() {}, removeListener() {} } },
+};
+globalThis.chrome = api;
+globalThis.browser = api;
+const { Agent } = await import('../src/chrome/src/agent/agent.js');
+const { cdpClient } = await import('../src/chrome/src/cdp/cdp-client.js');
+const original = {
+ sendCommand: cdpClient.sendCommand,
+ attach: cdpClient.attach,
+ evaluate: cdpClient.evaluate,
+ arm: cdpClient.armFileInputClickGuard,
+ consume: cdpClient.consumeFileInputClickGuard,
+ resolveSelector: cdpClient.resolveSelector,
+ evaluateFunction: cdpClient.evaluateFunction,
+ textEntrySignature: cdpClient.textEntrySignature,
+ verifyTextEntry: cdpClient.verifyTextEntry,
+ selectAllModifier: cdpClient.selectAllModifier,
+ captureTextClearReceipt: cdpClient.captureTextClearReceipt,
+};
+function restoreCdp() {
+ Object.assign(cdpClient, {
+ sendCommand: original.sendCommand, attach: original.attach, evaluate: original.evaluate,
+ armFileInputClickGuard: original.arm, consumeFileInputClickGuard: original.consume,
+ resolveSelector: original.resolveSelector, evaluateFunction: original.evaluateFunction,
+ textEntrySignature: original.textEntrySignature, verifyTextEntry: original.verifyTextEntry,
+ selectAllModifier: original.selectAllModifier, captureTextClearReceipt: original.captureTextClearReceipt,
+ });
+ cdpClient.sessions.delete(17);
+}
+
+const pageFeedbackVeto = () => Object.assign(
+ new Error('The browser changed during action preparation. Re-observe the page before acting.'),
+ { code: 'page_feedback_pending' },
+);
+
+function newAgent() {
+ const agent = new Agent({ getActive: () => ({ model: 'delivery-test' }) });
+ agent._currentUrl = async () => 'https://cdp-delivery.test/start';
+ agent._clickProgressSnapshot = async () => '';
+ agent._redirectTargetBlankClick = async () => null;
+ agent._showAgentTarget = () => {};
+ agent._keyProgressSnapshot = async () => '';
+ return agent;
+}
+
+const keysHtml = `<!doctype html><main><h1>Notes</h1><label>Note <textarea id="note"></textarea></label>
+<button id="post" type="button">Post</button><output id="count">0</output></main>
+<script>window.qaKeys = []; window.qaClicks = 0;
+note.addEventListener('keydown', event => qaKeys.push({ key: event.key, trusted: event.isTrusted }));
+post.addEventListener('click', event => { qaClicks++; count.textContent = String(qaClicks); });
+note.focus();</script>`;
+
+// [real DOM] Bridge chrome.debugger.sendCommand to a real Chromium CDP session.
+async function realChromium(body, { contentScripts = true } = {}) {
+ const browserInstance = await chromium.launch({ headless: true });
+ const context = await browserInstance.newContext({ viewport: { width: 900, height: 600 } });
+ await context.route('https://cdp-delivery.test/**', route => route.fulfill({ contentType: 'text/html', body }));
+ await context.addInitScript(() => {
+ window.qaListeners = [];
+ const runtime = {
+ onMessage: { addListener: fn => qaListeners.push(fn), removeListener() {} },
+ getURL: value => value,
+ async sendMessage(message) { return message.action === 'get_page_monitor_state' ? { active: false } : {}; },
+ };
+ window.chrome = { runtime }; window.browser = window.chrome;
+ window.qaMessage = message => new Promise(resolve => {
+ let answered = false, pending = false;
+ const respond = value => { if (!answered) { answered = true; resolve(value); } };
+ for (const listener of [...qaListeners]) if (listener(message, {}, respond) === true) pending = true;
+ if (!answered && !pending) resolve(undefined);
+ });
+ });
+ if (contentScripts) {
+ for (const file of ['file-upload-policy.js', 'rich-text-toolbar-heuristic.js', 'accessibility-tree.js', 'page-monitor.js', 'content.js']) {
+ await context.addInitScript({ content: fs.readFileSync(new URL(`../src/chrome/src/content/${file}`, import.meta.url), 'utf8') });
+ }
+ }
+ const page = await context.newPage();
+ await page.goto('https://cdp-delivery.test/start');
+ const session = await context.newCDPSession(page);
+ const protocol = [];
+ const hooks = { before: null, after: null };
+ api.debugger.sendCommand = (_target, method, params, callback) => {
+ protocol.push({ method, type: params?.type, key: params?.key });
+ Promise.resolve()
+ .then(() => hooks.before?.(method, params))
+ .then(() => session.send(method, params))
+ .then(async result => { await hooks.after?.(method, params); return result; })
+ .then(callback, error => {
+ api.runtime.lastError = { message: error.message };
+ try { callback(); } finally { api.runtime.lastError = null; }
+ });
+ };
+ cdpClient.attach = async tabId => { cdpClient.sessions.set(tabId, { tabId, attached: true }); return cdpClient.sessions.get(tabId); };
+ await cdpClient.attach(17);
+ // The file-chooser guard is unrelated CDP interception; keep it inert.
+ cdpClient.armFileInputClickGuard = async () => {};
+ cdpClient.consumeFileInputClickGuard = async () => ({ blocked: false });
+ api.tabs.sendMessage = async (_tabId, message) => page.evaluate(m => window.qaMessage(m), message);
+ return {
+ page, protocol, hooks,
+ async close() { restoreCdp(); await browserInstance.close(); },
+ };
+}
+
+test('[real DOM] press_keys: a lost keyDown acknowledgement after the first key is never replayed synthetically', async () => {
+ const fixture = await realChromium(keysHtml);
+ try {
+ let keyDowns = 0;
+ fixture.hooks.after = method => {
+ // The second keyDown reaches the page; only its acknowledgement is lost.
+ if (method === 'Input.dispatchKeyEvent' && fixture.protocol.at(-1)?.type === 'keyDown' && ++keyDowns === 2) {
+ throw new Error('Detached while handling command.');
+ }
+ };
+ const agent = newAgent();
+ let contentKeys = 0;
+ const send = api.tabs.sendMessage;
+ api.tabs.sendMessage = async (tabId, message) => {
+ if (message.action === 'press_keys') contentKeys++;
+ return send(tabId, message);
+ };
+ const result = await agent.executeTool(17, 'press_keys', { key: 'ArrowDown', repeat: 3 });
+ const keys = await fixture.page.evaluate(() => window.qaKeys);
+ assert.equal(contentKeys, 0, 'no synthetic content-script fallback after trusted delivery');
+ assert.equal(keys.length, 2, 'the page received exactly the two trusted keyDowns that were sent');
+ assert.ok(keys.every(key => key.trusted === true));
+ assert.equal(result.success, false);
+ assert.equal(result.dispatched, true);
+ assert.equal(result.outcomeUnknown, true);
+ assert.equal(result.retryable, false);
+ assert.equal(result.deliveredKeys, 1, 'one complete key press was acknowledged');
+ } finally { await fixture.close(); }
+});
+
+test('[real DOM] press_keys: normal trusted repeats are delivered once each', async () => {
+ const fixture = await realChromium(keysHtml);
+ try {
+ const agent = newAgent();
+ const result = await agent.executeTool(17, 'press_keys', { key: 'ArrowDown', repeat: 2 });
+ const keys = await fixture.page.evaluate(() => window.qaKeys);
+ assert.equal(keys.length, 2);
+ assert.ok(keys.every(key => key.trusted === true && key.key === 'ArrowDown'));
+ assert.equal(result.method, 'cdp-key');
+ assert.equal(result.dispatched, true);
+ } finally { await fixture.close(); }
+});
+
+test('[mocked transport] press_keys: target preparation failing before the second key never replays the first one', async () => {
+ const agent = newAgent();
+ const sent = [];
+ cdpClient.attach = async () => {};
+ cdpClient.sendCommand = async (_tab, method, params) => {
+ if (method === 'Input.dispatchKeyEvent' && params.type === 'keyDown' && sent.filter(type => type === 'keyDown').length === 1) {
+ throw new Error('Cannot find context with specified id');
+ }
+ if (method === 'Input.dispatchKeyEvent') sent.push(params.type);
+ return {};
+ };
+ let contentKeys = 0;
+ api.tabs.sendMessage = async (_tab, message) => {
+ if (message.action === 'press_keys') { contentKeys++; return { success: true, dispatched: true, method: 'keyboardevent' }; }
+ return { success: true };
+ };
+ try {
+ const state = { started: false };
+ const result = await agent._executeToolImpl(17, 'press_keys', { key: 'Enter', repeat: 2 }, null,
+ { _contentActionAbortSignal: new AbortController().signal, _contentActionDispatchState: state });
+ assert.deepEqual(sent, ['keyDown', 'keyUp']);
+ assert.equal(contentKeys, 0);
+ assert.equal(state.started, true);
+ assert.equal(result.dispatched, true);
+ assert.equal(result.outcomeUnknown, true);
+ assert.equal(result.deliveredKeys, 1);
+ assert.notEqual(result.noDispatch, true);
+ } finally { restoreCdp(); }
+});
+
+test('[mocked transport] press_keys: a fence veto before any key stays undispatched and is not replayed synthetically', async () => {
+ const agent = newAgent();
+ cdpClient.attach = async () => {};
+ cdpClient.sendCommand = async (_tab, method) => {
+ if (method === 'Input.dispatchKeyEvent') throw pageFeedbackVeto();
+ return {};
+ };
+ let contentKeys = 0;
+ api.tabs.sendMessage = async (_tab, message) => {
+ if (message.action === 'press_keys') { contentKeys++; return { success: true, dispatched: true, method: 'keyboardevent' }; }
+ return { success: true };
+ };
+ try {
+ const result = await agent.executeTool(17, 'press_keys', { key: 'Enter' });
+ assert.equal(contentKeys, 0, 'a vetoed trusted key must not become a synthetic key');
+ assert.equal(result.success, false);
+ assert.equal(result.noDispatch, true);
+ assert.equal(result.dispatched, false);
+ assert.equal(result.pageFeedbackPending, true);
+ } finally { restoreCdp(); }
+});
+
+function mockTextClick({ failPressWith = null, failAfterRelease = null } = {}) {
+ const events = [];
+ cdpClient.attach = async () => {};
+ cdpClient.armFileInputClickGuard = async () => {};
+ cdpClient.consumeFileInputClickGuard = async () => ({ blocked: false });
+ cdpClient.evaluate = async (_tab, expression) => {
+ if (/const needle =/.test(expression)) {
+ return { result: { value: { found: true, mode: 'exact', x: 5, y: 5, width: 10, height: 10, tag: 'DIV', text: 'Post',
+ isSubmitControl: false, isEditableControl: false } } };
+ }
+ if (failAfterRelease && events.includes('mouseReleased')) throw failAfterRelease();
+ return { result: { value: null } };
+ };
+ cdpClient.sendCommand = async (_tab, method, params) => {
+ if (method === 'Input.dispatchMouseEvent') {
+ if (params.type === 'mousePressed' && failPressWith) throw failPressWith();
+ events.push(params.type);
+ }
+ return {};
+ };
+ return events;
+}
+
+test('[mocked transport] CDP click: a navigation-destroyed post-click read keeps the delivered click dispatched', async () => {
+ const agent = newAgent();
+ agent._annotateClickProgress = async (_t, _n, _a, response) => response;
+ const events = mockTextClick({ failAfterRelease: () => new Error('Execution context was destroyed.') });
+ try {
+ const state = { started: false };
+ const result = await agent._executeToolImpl(17, 'click', { text: 'Post' }, null,
+ { _contentActionAbortSignal: new AbortController().signal, _contentActionDispatchState: state });
+ assert.deepEqual(events, ['mouseMoved', 'mousePressed', 'mouseReleased']);
+ assert.equal(state.started, true);
+ assert.equal(result.success, false);
+ assert.equal(result.dispatched, true);
+ assert.equal(result.outcomeUnknown, true);
+ assert.equal(result.retryable, false);
+ assert.notEqual(result.noDispatch, true);
+ } finally { restoreCdp(); }
+});
+
+test('[mocked transport] CDP click: a fence veto before the press is reported undispatched', async () => {
+ const agent = newAgent();
+ agent._annotateClickProgress = async (_t, _n, _a, response) => response;
+ const events = mockTextClick({ failPressWith: pageFeedbackVeto });
+ try {
+ const result = await agent.executeTool(17, 'click', { text: 'Post' });
+ assert.deepEqual(events, ['mouseMoved']);
+ assert.equal(result.success, false);
+ assert.equal(result.dispatched, false);
+ assert.equal(result.noDispatch, true);
+ assert.equal(result.pageFeedbackPending, true);
+ } finally { restoreCdp(); }
+});
+
+test('[mocked transport] CDP click by text: a normal click delivers one press/release and succeeds', async () => {
+ const agent = newAgent();
+ agent._annotateClickProgress = async (_t, _n, _a, response) => response;
+ const events = mockTextClick();
+ try {
+ const state = { started: false };
+ const result = await agent._executeToolImpl(17, 'click', { text: 'Post' }, null,
+ { _contentActionAbortSignal: new AbortController().signal, _contentActionDispatchState: state });
+ assert.deepEqual(events, ['mouseMoved', 'mousePressed', 'mouseReleased']);
+ assert.equal(result.success, true);
+ assert.equal(result.method, 'cdp-by-text');
+ assert.equal(state.started, true);
+ } finally { restoreCdp(); }
+});
+
+const buttonHtml = `<!doctype html><main><h1>Compose</h1>
+<button id="post" type="button" style="position:absolute;left:100px;top:100px;width:120px;height:40px">Post</button></main>
+<script>window.qaClicks = []; post.addEventListener('click', event => qaClicks.push(event.isTrusted));</script>`;
+
+test('[real DOM] clickElement: a lost release acknowledgement never adds a DOM/JS fallback click', async () => {
+ const fixture = await realChromium(buttonHtml, { contentScripts: false });
+ try {
+ fixture.hooks.after = (method, params) => {
+ if (method === 'Input.dispatchMouseEvent' && params.type === 'mouseReleased' && params.clickCount === 1) {
+ throw new Error('Internal error');
+ }
+ };
+ let authorizations = 0, dispatchMarks = 0;
+ const result = await cdpClient.clickElement(17, '#post', {
+ beforeDispatch: async () => { authorizations++; return { success: true }; },
+ onDispatch: () => { dispatchMarks++; },
+ });
+ const clicks = await fixture.page.evaluate(() => window.qaClicks);
+ assert.deepEqual(clicks, [true], 'exactly one trusted activation; no DOM/JS fallback click');
+ assert.equal(authorizations, 1);
+ assert.equal(dispatchMarks, 1);
+ assert.equal(result.success, false);
+ assert.equal(result.dispatched, true);
+ assert.equal(result.outcomeUnknown, true);
+ assert.equal(result.retryable, false);
+ assert.deepEqual(fixture.protocol.filter(entry => entry.method === 'Input.dispatchMouseEvent')
+ .map(entry => entry.type).slice(0, 3), ['mouseMoved', 'mousePressed', 'mouseReleased']);
+ } finally { await fixture.close(); }
+});
+
+test('[real DOM] clickElement: a normal selector click is one trusted activation', async () => {
+ const fixture = await realChromium(buttonHtml, { contentScripts: false });
+ try {
+ let dispatchMarks = 0;
+ const result = await cdpClient.clickElement(17, '#post', { onDispatch: () => { dispatchMarks++; } });
+ assert.equal(result.success, true);
+ assert.deepEqual(await fixture.page.evaluate(() => window.qaClicks), [true]);
+ assert.equal(dispatchMarks, 1);
+ } finally { await fixture.close(); }
+});
+
+test('[real DOM] clickElement: a press that never reached the protocol may use the single JS fallback', async () => {
+ const fixture = await realChromium(buttonHtml, { contentScripts: false });
+ try {
+ // The real transport refuses the press at its last pre-protocol check, so
+ // the command never reaches chrome.debugger.sendCommand.
+ const sendCommand = cdpClient.sendCommand;
+ cdpClient.sendCommand = function (tabId, method, params, sessionId, options) {
+ if (method === 'Input.dispatchMouseEvent' && params.type === 'mousePressed') {
+ return sendCommand.call(this, tabId, method, params, sessionId,
+ { ...options, beforeSend: () => { throw new Error(`Not attached to tab ${tabId}`); } });
+ }
+ return sendCommand.call(this, tabId, method, params, sessionId, options);
+ };
+ let dispatchMarks = 0;
+ const result = await cdpClient.clickElement(17, '#post', { onDispatch: () => { dispatchMarks++; } });
+ assert.equal(result.success, true);
+ assert.equal(result.method, 'js-click');
+ assert.deepEqual(await fixture.page.evaluate(() => window.qaClicks), [false], 'one synthetic activation only');
+ assert.equal(dispatchMarks, 1);
+ } finally { await fixture.close(); }
+});
+
+test('[mocked transport] CDP click by selector: a fence veto before the press is undispatched and keeps no started state', async () => {
+ const agent = newAgent();
+ agent._annotateClickProgress = async (_t, _n, _a, response) => response;
+ cdpClient.attach = async () => {};
+ cdpClient.evaluate = async () => ({ result: { value: null } });
+ cdpClient.armFileInputClickGuard = async () => {};
+ cdpClient.consumeFileInputClickGuard = async () => ({ blocked: false });
+ cdpClient.resolveSelector = async () => ({ inViewport: true, hitOk: true, x: 10, y: 10, width: 20, height: 20, tag: 'BUTTON', text: 'Send' });
+ const events = [];
+ cdpClient.sendCommand = async (_tab, method, params) => {
+ if (method === 'Input.dispatchMouseEvent') {
+ if (params.type === 'mousePressed') throw pageFeedbackVeto();
+ events.push(params.type);
+ }
+ return {};
+ };
+ let fallbackClicks = 0;
+ cdpClient.evaluateFunction = async () => { fallbackClicks++; return { result: { value: { success: true, dispatched: true } } }; };
+ try {
+ const result = await agent.executeTool(17, 'click', { selector: '#send' });
+ assert.deepEqual(events, ['mouseMoved']);
+ assert.equal(fallbackClicks, 0);
+ assert.equal(result.noDispatch, true);
+ assert.equal(result.dispatched, false);
+ assert.equal(result.pageFeedbackPending, true);
+ } finally { restoreCdp(); }
+});
+
+function focusedTypingMocks({ insertText }) {
+ const commands = [];
+ cdpClient.attach = async () => {};
+ cdpClient.evaluate = async () => ({ result: { value: { focused: true, editable: true, tag: 'TEXTAREA', name: 'msg',
+ rect: { x: 0, y: 0, w: 1, h: 1 } } } });
+ cdpClient.textEntrySignature = async () => 'signature';
+ cdpClient.verifyTextEntry = async () => true;
+ cdpClient.sendCommand = async (_tab, method, params) => {
+ commands.push(method);
+ if (method === 'Input.insertText') return insertText(params);
+ return {};
+ };
+ return commands;
+}
+
+test('[mocked transport] type_text: a fence veto before Input.insertText is undispatched and records no text debt', async () => {
+ const agent = newAgent();
+ focusedTypingMocks({ insertText: () => { throw pageFeedbackVeto(); } });
+ try {
+ const result = await agent.executeTool(17, 'type_text', { text: 'hello' });
+ assert.equal(result.success, false);
+ assert.equal(result.dispatched, false);
+ assert.equal(result.noDispatch, true);
+ assert.equal(result.pageFeedbackPending, true);
+ assert.equal(agent._uncertainTextMutations.get(17)?.size || 0, 0, 'no uncertain-text debt for text that never reached the page');
+ // Positive progress: the same write is allowed after a fresh decision.
+ focusedTypingMocks({ insertText: () => ({}) });
+ const retried = await agent.executeTool(17, 'type_text', { text: 'hello' });
+ assert.equal(retried.success, true);
+ assert.equal(retried.verified, true);
+ } finally { restoreCdp(); }
+});
+
+test('[mocked transport] type_text: a lost Input.insertText acknowledgement stays uncertain and blocks a blind repeat', async () => {
+ const agent = newAgent();
+ focusedTypingMocks({ insertText: () => { throw new Error('Owned text response lost'); } });
+ try {
+ const result = await agent.executeTool(17, 'type_text', { text: 'hello' });
+ assert.equal(result.success, false);
+ assert.equal(result.dispatched, true);
+ assert.notEqual(result.noDispatch, true);
+ assert.equal(agent._uncertainTextMutations.get(17)?.size, 1, 'possibly delivered text is recorded as uncertain');
+ } finally { restoreCdp(); }
+});
+
+test('[mocked transport] type_text clear: a fence veto before the select-all key leaves dispatch unstarted', async () => {
+ const agent = newAgent();
+ const commands = focusedTypingMocks({ insertText: () => ({}) });
+ cdpClient.selectAllModifier = async () => 2;
+ cdpClient.captureTextClearReceipt = async () => null;
+ const sendCommand = cdpClient.sendCommand;
+ cdpClient.sendCommand = async (tab, method, params, ...rest) => {
+ if (method === 'Input.dispatchKeyEvent' && params.type === 'keyDown') throw pageFeedbackVeto();
+ return sendCommand(tab, method, params, ...rest);
+ };
+ try {
+ const result = await agent.executeTool(17, 'type_text', { text: 'hello', clear: true });
+ assert.equal(commands.includes('Input.insertText'), false);
+ assert.equal(result.noDispatch, true);
+ assert.equal(result.dispatched, false);
+ assert.equal(agent._uncertainTextMutations.get(17)?.size || 0, 0);
+ } finally { restoreCdp(); }
+});
+
+test('[real DOM] cdpClient.typeText: an insertion refused before the protocol call is not marked dispatched', async () => {
+ const fixture = await realChromium(`<!doctype html><textarea id="note"></textarea>`, { contentScripts: false });
+ try {
+ const sendCommand = cdpClient.sendCommand;
+ cdpClient.sendCommand = function (tabId, method, params, ...rest) {
+ // The transport refuses the insertion before chrome.debugger is called.
+ if (method === 'Input.insertText') this.sessions.delete(tabId);
+ return sendCommand.call(this, tabId, method, params, ...rest);
+ };
+ let dispatchMarks = 0;
+ const result = await cdpClient.typeText(17, '#note', 'hello', false, null, { beforeDispatch: () => { dispatchMarks++; } });
+ assert.equal(dispatchMarks, 0, 'no text reached the page, so the caller records no dispatch');
+ assert.equal(result.success, false);
+ assert.equal(result.noDispatch, true);
+ assert.notEqual(result.mutationMayHaveOccurred, true);
+ assert.equal(await fixture.page.locator('#note').inputValue(), '');
+ } finally { await fixture.close(); }
+});
+
+test('[real DOM] cdpClient.typeText: normal typing marks dispatch once text is sent', async () => {
+ const fixture = await realChromium(`<!doctype html><textarea id="note"></textarea>`, { contentScripts: false });
+ try {
+ let dispatchMarks = 0;
+ const result = await cdpClient.typeText(17, '#note', 'hello', false, null, { beforeDispatch: () => { dispatchMarks++; } });
+ assert.equal(result.success, true, JSON.stringify(result));
+ assert.ok(dispatchMarks >= 1);
+ assert.equal(await fixture.page.locator('#note').inputValue(), 'hello');
+ } finally { await fixture.close(); }
+});
+
+test('[mocked transport] trusted field fallback: a vetoed insertion leaves no uncertain text debt', async () => {
+ const agent = newAgent();
+ const args = { ref_id: 'ref_7', text: 'New value', clear: true };
+ agent._lastAxScopes.set(17, { documentToken: 'doc-field', pageUrl: 'https://cdp-delivery.test/start' });
+ const commands = [];
+ api.tabs.sendMessage = async (_tab, message) => {
+ if (message.action === 'field_value_digest') return { success: true, documentToken: 'doc-field', refScopeUrl: 'https://cdp-delivery.test/start', valueLength: 3, valueSha256: 'old' };
+ if (message.action === 'ax_prepare_field_for_trusted_type') return { success: true, contentEditable: false };
+ if (message.action === 'ax_verify_field_value') return { success: true, verified: true };
+ return { success: true };
+ };
+ cdpClient.attach = async () => {};
+ cdpClient.sendCommand = async (_tab, method) => {
+ commands.push(method);
+ if (method === 'Input.insertText') throw pageFeedbackVeto();
+ return {};
+ };
+ try {
+ const result = await agent._maybeFallbackFieldWithCdpImpl(17, 'set_field', args,
+ { success: false, verified: false, noDispatch: true, dispatched: false, _expectedValue: args.text, error: 'Trusted editor typing required' },
+ {}, new AbortController().signal);
+ assert.equal(result.success, false);
+ assert.notEqual(result.dispatched, true, JSON.stringify(result));
+ assert.equal(result.noDispatch, true);
+ const finalized = await agent._finalizeTextMutationResult(17, 'set_field', args, result);
+ assert.equal(finalized.noDispatch, true);
+ assert.equal(agent._uncertainTextMutations.get(17)?.size || 0, 0, 'a vetoed insertion is not uncertain text');
+ } finally { restoreCdp(); api.tabs.sendMessage = async () => ({}); }
+});
+
+test('[mocked transport] trusted field fallback: a lost insertion acknowledgement stays uncertain', async () => {
+ const agent = newAgent();
+ const args = { ref_id: 'ref_7', text: 'New value', clear: true };
+ agent._lastAxScopes.set(17, { documentToken: 'doc-field', pageUrl: 'https://cdp-delivery.test/start' });
+ api.tabs.sendMessage = async (_tab, message) => {
+ if (message.action === 'field_value_digest') return { success: true, documentToken: 'doc-field', refScopeUrl: 'https://cdp-delivery.test/start', valueLength: 3, valueSha256: 'old' };
+ if (message.action === 'ax_prepare_field_for_trusted_type') return { success: true, contentEditable: false };
+ return { success: true };
+ };
+ cdpClient.attach = async () => {};
+ cdpClient.sendCommand = async (_tab, method) => {
+ if (method === 'Input.insertText') throw new Error('Owned text response lost');
+ return {};
+ };
+ try {
+ const result = await agent._maybeFallbackFieldWithCdpImpl(17, 'set_field', args,
+ { success: false, verified: false, noDispatch: true, dispatched: false, _expectedValue: args.text, error: 'Trusted editor typing required' },
+ {}, new AbortController().signal);
+ assert.equal(result.dispatched, true);
+ assert.notEqual(result.textUnchanged, true);
+ } finally { restoreCdp(); api.tabs.sendMessage = async () => ({}); }
+});
diff --git a/test/delivery-click-ax-response-loss.mjs b/test/delivery-click-ax-response-loss.mjs
new file mode 100644
index 000000000..738c959b7
--- /dev/null
+++ b/test/delivery-click-ax-response-loss.mjs
@@ -0,0 +1,207 @@
+// Delivery regression (C1): a click_ax content message whose response is lost
+// after the content script already ran el.click() must never be re-sent.
+//
+// Real-DOM fixture: the real Chrome/Firefox Agent dispatches click_ax through
+// the real accessibility-tree.js + page-monitor.js + content.js of the same
+// build, loaded into a Playwright page of the matching engine (Chromium for
+// the Chrome build, Firefox for the Firefox build). Only the extension
+// messaging transport is bridged: it can deliver the message to the page and
+// then report the browser's lost-response or missing-receiver error.
+import assert from 'node:assert/strict';
+import fs from 'node:fs';
+import { test } from 'node:test';
+import { chromium, firefox } from 'playwright';
+
+const area = { get: async () => ({}), set: async () => {}, remove: async () => {} };
+const api = {
+ storage: { local: area, session: area },
+ runtime: { getURL: value => `extension://test/${value}`, getManifest: () => ({ version: 'test' }) },
+ tabs: { get: async () => ({ id: 17, url: 'https://delivery.test/start' }), query: async () => [] },
+ scripting: { executeScript: async () => [{}] },
+};
+globalThis.chrome = api;
+globalThis.browser = api;
+const builds = {
+ chrome: { Agent: (await import('../src/chrome/src/agent/agent.js')).Agent, engine: chromium },
+ firefox: { Agent: (await import('../src/firefox/src/agent/agent.js')).Agent, engine: firefox },
+};
+
+// Exact browser error texts. Chrome reports a closed response channel when
+// the listener answered asynchronously and its document went away; Firefox
+// reports the conduit teardown. Neither proves that no click was delivered.
+const LOST_RESPONSE = {
+ chrome: 'A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received',
+ firefox: "Actor 'Conduits' destroyed before query 'RuntimeMessage' was resolved",
+};
+const MISSING_RECEIVER = 'Could not establish connection. Receiving end does not exist.';
+
+const html = `<!doctype html><main><h1>Cart</h1>
+<button id="add" type="button">Add to cart</button><output id="count">0</output></main>
+<script>window.qaClicks = 0; add.addEventListener('click', () => { count.textContent = String(++window.qaClicks); });</script>`;
+
+async function fixture(browserInstance, build) {
+ const context = await browserInstance.newContext({ viewport: { width: 900, height: 600 } });
+ await context.route('https://delivery.test/**', route => route.fulfill({ contentType: 'text/html', body: html }));
+ await context.addInitScript(() => {
+ window.qaListeners = [];
+ const runtime = {
+ onMessage: { addListener: fn => qaListeners.push(fn), removeListener: fn => {
+ const index = qaListeners.indexOf(fn); if (index >= 0) qaListeners.splice(index, 1);
+ } },
+ getURL: value => value,
+ async sendMessage(message) {
+ if (message.action === 'get_page_monitor_state') return { active: false };
+ return {};
+ },
+ };
+ window.chrome = { runtime }; window.browser = window.chrome;
+ // Like the browser runtime, every listener in the content world receives
+ // the message; the first sendResponse answers it.
+ window.qaMessage = message => new Promise(resolve => {
+ let answered = false, pending = false;
+ const respond = value => { if (!answered) { answered = true; resolve(value); } };
+ for (const listener of [...qaListeners]) {
+ if (listener(message, {}, respond) === true) pending = true;
+ }
+ if (!answered && !pending) resolve(undefined);
+ });
+ });
+ for (const file of ['file-upload-policy.js', 'rich-text-toolbar-heuristic.js', 'accessibility-tree.js', 'page-monitor.js', 'content.js']) {
+ const source = fs.readFileSync(new URL(`../src/${build}/src/content/${file}`, import.meta.url), 'utf8');
+ await context.addInitScript({ content: source });
+ }
+ const page = await context.newPage();
+ await page.goto('https://delivery.test/start');
+ await page.waitForFunction(() => typeof window.__wb_ax_ref === 'function' && window.qaListeners.length > 0);
+ const ref = await page.evaluate(() => {
+ window.__generateAccessibilityTree?.('all', 15, 6000);
+ return window.__wb_ax_ref(document.getElementById('add'));
+ });
+ assert.match(ref, /^ref_\d+$/);
+ return { context, page, ref };
+}
+
+function agentFor(build, Agent) {
+ const agent = new Agent({ getActive: () => ({ model: 'delivery-test' }) });
+ // Observation helpers are not under test here; keep them read-only and local.
+ agent._clickProgressSnapshot = async () => '';
+ agent._maybeFallbackClickAxWithCdp = async (_tab, _args, response) => response;
+ if (build === 'chrome') agent._captureClickAxObservation = async () => ({ startedAt: Date.now(), snapshot: '' });
+ agent.injections = 0;
+ agent._injectCoreContentScripts = async () => { agent.injections++; };
+ return agent;
+}
+
+function bridge(page, messages, { failClickAx = [] } = {}) {
+ let clickAxSends = 0;
+ api.tabs.sendMessage = async (_tabId, message) => {
+ messages.push(message.action);
+ if (message.action !== 'click_ax') return page.evaluate(m => window.qaMessage(m), message);
+ const failure = failClickAx[clickAxSends++];
+ if (failure === 'missing-receiver') throw new Error(MISSING_RECEIVER);
+ const response = await page.evaluate(m => window.qaMessage(m), message);
+ if (failure === 'lost-response') throw new Error(LOST_RESPONSE[page.qaBuild]);
+ return response;
+ };
+}
+
+for (const [build, { Agent, engine }] of Object.entries(builds)) {
+ test(`${build}: click_ax response loss after delivery is reported uncertain and never re-sent`, async t => {
+ const browserInstance = await engine.launch({ headless: true });
+ try {
+ await t.test('lost response after the content script clicked: one page click, outcome unknown', async () => {
+ const { context, page, ref } = await fixture(browserInstance, build);
+ try {
+ page.qaBuild = build;
+ const messages = [];
+ bridge(page, messages, { failClickAx: ['lost-response'] });
+ const agent = agentFor(build, Agent);
+ const state = { started: false };
+ const result = await agent._dispatchClickAx(17, { ref_id: ref }, null, null, {}, null, state, null);
+ assert.equal(await page.evaluate(() => window.qaClicks), 1, 'the delivered click must not be repeated');
+ assert.equal(messages.filter(action => action === 'click_ax').length, 1);
+ assert.equal(agent.injections, 0, 'a lost response is not a missing receiver; nothing is re-injected');
+ assert.equal(state.started, true, 'the delivered click keeps its dispatch state');
+ assert.equal(result.success, false);
+ assert.equal(result.dispatched, true);
+ assert.notEqual(result.noDispatch, true);
+ assert.equal(result.outcomeUnknown, true);
+ assert.equal(result.retryable, false);
+ } finally { await context.close(); }
+ });
+
+ await t.test('explicit missing receiver: inject once and deliver exactly one click', async () => {
+ const { context, page, ref } = await fixture(browserInstance, build);
+ try {
+ page.qaBuild = build;
+ const messages = [];
+ bridge(page, messages, { failClickAx: ['missing-receiver'] });
+ const agent = agentFor(build, Agent);
+ const state = { started: false };
+ const result = await agent._dispatchClickAx(17, { ref_id: ref }, null, null, {}, null, state, null);
+ assert.equal(agent.injections, 1);
+ assert.equal(messages.filter(action => action === 'click_ax').length, 2);
+ assert.equal(await page.evaluate(() => window.qaClicks), 1);
+ assert.equal(result.success, true);
+ assert.equal(state.started, true);
+ } finally { await context.close(); }
+ });
+
+ await t.test('missing receiver on both attempts reports an undispatched click', async () => {
+ const { context, page, ref } = await fixture(browserInstance, build);
+ try {
+ page.qaBuild = build;
+ const messages = [];
+ bridge(page, messages, { failClickAx: ['missing-receiver', 'missing-receiver'] });
+ const agent = agentFor(build, Agent);
+ const state = { started: false };
+ const result = await agent._dispatchClickAx(17, { ref_id: ref }, null, null, {}, null, state, null);
+ assert.equal(await page.evaluate(() => window.qaClicks), 0);
+ assert.equal(result.success, false);
+ assert.equal(result.dispatched, false);
+ assert.equal(result.noDispatch, true);
+ assert.equal(state.started, false);
+ } finally { await context.close(); }
+ });
+
+ await t.test('normal click: one message, one page click, success', async () => {
+ const { context, page, ref } = await fixture(browserInstance, build);
+ try {
+ page.qaBuild = build;
+ const messages = [];
+ bridge(page, messages);
+ const agent = agentFor(build, Agent);
+ const result = await agent._dispatchClickAx(17, { ref_id: ref }, null, null, {}, null, { started: false }, null);
+ assert.equal(result.success, true);
+ assert.equal(messages.filter(action => action === 'click_ax').length, 1);
+ assert.equal(await page.evaluate(() => window.qaClicks), 1);
+ assert.equal(agent.injections, 0);
+ } finally { await context.close(); }
+ });
+ } finally {
+ await browserInstance.close();
+ }
+ });
+
+ test(`${build}: Stop while a click_ax message is in flight keeps its dispatch state`, async () => {
+ const agent = agentFor(build, Agent);
+ const controller = new AbortController();
+ let sent = 0;
+ api.tabs.sendMessage = async (_tabId, message) => {
+ if (message.action !== 'click_ax') return {};
+ sent++;
+ // The content script received the click; Stop arrives before its reply
+ // and the document then drops the response channel.
+ controller.abort(Object.assign(new Error('Stopped by user'), { name: 'AbortError' }));
+ throw new Error(LOST_RESPONSE[build]);
+ };
+ const state = { started: false };
+ await assert.rejects(
+ agent._dispatchClickAx(17, { ref_id: 'ref_1' }, null, null, {}, controller.signal, state, null),
+ error => error?.name === 'AbortError',
+ );
+ assert.equal(sent, 1);
+ assert.equal(agent.injections, 0, 'cancellation is not a missing receiver');
+ assert.equal(state.started, true, 'the lifecycle wrapper reports a cancelled delivered click as uncertain');
+ });
+}
diff --git a/test/delivery-click-locks.mjs b/test/delivery-click-locks.mjs
new file mode 100644
index 000000000..9bef930c0
--- /dev/null
+++ b/test/delivery-click-locks.mjs
@@ -0,0 +1,151 @@
+// Delivery regressions (C6, C7) for the Chrome click_ax trusted fallback lock.
+// [mocked transport] The real Agent fallback pipeline runs with the page
+// observation, target resolution and CDP mouse transport stubbed; the stubs
+// model a synthetic click that produced no observable change on an eligible
+// generic target, so the single trusted fallback is considered.
+// C6: a lost mousePressed acknowledgement may follow delivery, so it reserves
+// the one-shot lock and is never reported retryable.
+// C7: an unresolved trusted click stays locked across the run boundaries of
+// trusted Continue / recovery Retry (same task token) and is released by
+// a new task. processMessage clears the per-run lock set at every run
+// boundary; the test performs exactly that clear.
+import assert from 'node:assert/strict';
+import { test } from 'node:test';
+
+const area = { get: async () => ({}), set: async () => {}, remove: async () => {} };
+const api = {
+ storage: { local: area, session: area },
+ runtime: { getURL: value => value, getManifest: () => ({ version: 'test' }) },
+ tabs: { get: async () => ({ id: 42, url: 'https://locks.test/' }), query: async () => [{ id: 42 }], sendMessage: async () => ({}) },
+};
+globalThis.chrome = api;
+globalThis.browser = api;
+const { Agent } = await import('../src/chrome/src/agent/agent.js');
+const { cdpClient } = await import('../src/chrome/src/cdp/cdp-client.js');
+
+const syntheticResponse = () => ({
+ success: true, method: 'click_ax', ref_id: 'ref_row', tag: 'div',
+ _fallbackStateBefore: '{"full":"same"}', _fallbackStateAfterImmediate: '{"full":"same"}',
+ _fallbackStrongStateBefore: '{"aria-current":"<absent>"}', _fallbackStrongStateAfterImmediate: '{"aria-current":"<absent>"}',
+ _fallbackWeakStateBefore: '{"className":""}', _fallbackWeakStateAfterImmediate: '{"className":""}',
+});
+
+function lockFixture({ press = () => ({}), proved = false } = {}) {
+ const agent = new Agent({});
+ const events = [];
+ let callStart = 0;
+ agent._clickAxFinalSettleMs = () => 0;
+ let observations = 0;
+ agent._observeClickAxSideEffect = async () => {
+ observations++;
+ const prove = proved && events.slice(callStart).includes('mouseReleased');
+ return { changed: prove, proved: prove, weakEvidence: false, safetyVeto: false, observable: true,
+ reasons: prove ? ['url'] : [], proofReasons: prove ? ['url'] : [], weakReasons: [], safetyReasons: [],
+ snapshot: '{"text":"same","active":""}' };
+ };
+ agent._captureClickAxObservation = async (_tab, snapshot, sideEffectWatch, startedAt) => ({ startedAt, snapshot, tabIds: '42', sideEffectWatch });
+ agent._resolveClickAxFallbackTarget = async () => ({ success: true, fallbackEligible: true, documentToken: 'doc-1',
+ ref_id: 'ref_row', tag: 'div', x: 100, y: 200, rect: { x: 50, y: 180, w: 100, h: 40 },
+ fallbackState: '{"aria-current":"<absent>"}', fallbackStrongState: '{"aria-current":"<absent>"}', fallbackWeakState: '{"className":""}' });
+ const saved = { attach: cdpClient.attach, arm: cdpClient.armFileInputClickGuard, consume: cdpClient.consumeFileInputClickGuard,
+ dispatch: cdpClient.dispatchMouseEvent, sendCommand: cdpClient.sendCommand };
+ cdpClient.attach = async () => ({});
+ cdpClient.armFileInputClickGuard = async () => {};
+ cdpClient.consumeFileInputClickGuard = async () => ({ blocked: false });
+ cdpClient.sendCommand = async () => ({});
+ cdpClient.dispatchMouseEvent = async (_tab, type) => {
+ events.push(type);
+ if (type === 'mousePressed') return press(events);
+ return {};
+ };
+ const fallback = () => {
+ callStart = events.length;
+ return agent._maybeFallbackClickAxWithCdp(42, { ref_id: 'ref_row' }, syntheticResponse(),
+ { snapshot: '{"text":"same"}', sideEffectWatch: { created: [], requests: [] } });
+ };
+ const restore = () => Object.assign(cdpClient, { attach: saved.attach, armFileInputClickGuard: saved.arm,
+ consumeFileInputClickGuard: saved.consume, dispatchMouseEvent: saved.dispatch, sendCommand: saved.sendCommand });
+ // processMessage clears the per-run lock set at each run boundary.
+ const runBoundary = () => agent._clickAxCdpFallbacks.delete(42);
+ return { agent, events, fallback, restore, runBoundary, observations: () => observations };
+}
+
+test('C6: a lost trusted press acknowledgement reserves the one-shot fallback and is not retryable', async () => {
+ const f = lockFixture({ press: () => { throw new Error('Detached while handling command.'); } });
+ try {
+ const first = await f.fallback();
+ assert.equal(first.success, false);
+ assert.equal(first.dispatched, true);
+ assert.equal(first.outcomeUnknown, true, 'the press may have reached the page');
+ assert.equal(first.retryable, false);
+ assert.equal(f.events.filter(type => type === 'mousePressed').length, 1);
+ // The held button is cleared without completing a click on the target.
+ assert.ok(f.events.includes('mouseReleased') === false, 'no target release was sent');
+ const repeated = await f.fallback();
+ assert.match(repeated.fallbackSkipped, /already attempted/);
+ assert.equal(f.events.filter(type => type === 'mousePressed').length, 1, 'no second trusted press');
+ } finally { f.restore(); }
+});
+
+test('C6: a press vetoed by the dispatch fence before sending stays retryable and unreserved', async () => {
+ const veto = () => { throw Object.assign(new Error('The browser changed during action preparation.'), { code: 'page_feedback_pending' }); };
+ const f = lockFixture({ press: veto });
+ try {
+ const first = await f.fallback();
+ assert.equal(first.retryable, true);
+ assert.notEqual(first.outcomeUnknown, true);
+ assert.equal(f.agent._clickAxCdpFallbacks.get(42)?.size || 0, 0);
+ } finally { f.restore(); }
+});
+
+test('C7: an unresolved trusted click stays locked across Continue/Retry of the same task; a new task may act again', async () => {
+ const f = lockFixture();
+ try {
+ f.agent._taskTokens.set(42, 'task_original');
+ const first = await f.fallback();
+ assert.equal(first.trusted, true);
+ assert.equal(first.verified, false, 'acknowledged input with no observed effect stays unresolved');
+ assert.equal(f.events.filter(type => type === 'mousePressed').length, 1);
+
+ // Trusted Continue / failure-recovery Retry: new run, same task token.
+ f.runBoundary();
+ const continued = await f.fallback();
+ assert.match(continued.fallbackSkipped || '', /already attempted/, JSON.stringify(continued));
+ assert.equal(f.events.filter(type => type === 'mousePressed').length, 1, 'no repeated trusted press after Continue/Retry');
+
+ // The synthetic click_ax path is blocked for the same exact document|ref.
+ const realFallback = f.agent._maybeFallbackClickAxWithCdp;
+ f.agent._maybeFallbackClickAxWithCdp = async (_tab, _args, response) => response;
+ let contentClicks = 0;
+ api.tabs.sendMessage = async (_tab, message) => { if (message.action === 'click_ax') contentClicks++; return { success: true }; };
+ const synthetic = await f.agent._dispatchClickAx(42, { ref_id: 'ref_row' }, { documentToken: 'doc-1', pageUrl: 'https://locks.test/' },
+ null, {}, null, { started: false }, null);
+ assert.equal(synthetic.noDispatch, true);
+ assert.equal(synthetic.repeatBlocked, true);
+ assert.equal(contentClicks, 0);
+ // A re-rendered target or another document is not this lock.
+ const otherRef = await f.agent._dispatchClickAx(42, { ref_id: 'ref_other' }, { documentToken: 'doc-1', pageUrl: 'https://locks.test/' },
+ null, {}, null, { started: false }, null);
+ assert.notEqual(otherRef.repeatBlocked, true);
+ assert.equal(contentClicks, 1);
+ f.agent._maybeFallbackClickAxWithCdp = realFallback;
+
+ // A new task does not inherit the old task's locks.
+ f.runBoundary();
+ f.agent._taskTokens.set(42, 'task_new');
+ await f.fallback();
+ assert.equal(f.events.filter(type => type === 'mousePressed').length, 2);
+ } finally { f.restore(); api.tabs.sendMessage = async () => ({}); }
+});
+
+test('C7: a verified trusted effect releases the task lock for later runs', async () => {
+ const f = lockFixture({ proved: true });
+ try {
+ f.agent._taskTokens.set(42, 'task_verified');
+ const first = await f.fallback();
+ assert.equal(first.verified, true);
+ f.runBoundary();
+ await f.fallback();
+ assert.equal(f.events.filter(type => type === 'mousePressed').length, 2, 'a reconciled click does not block a later decision');
+ } finally { f.restore(); }
+});
diff --git a/test/delivery-native-button-geometry.mjs b/test/delivery-native-button-geometry.mjs
new file mode 100644
index 000000000..74376bfa1
--- /dev/null
+++ b/test/delivery-native-button-geometry.mjs
@@ -0,0 +1,180 @@
+// Delivery regression (C9): the agent's own hover may lift or resize the exact
+// certified native BUTTON between the pointer move and the press. The press
+// must use the button's verified current centre (never stale coordinates) and
+// any other change must still refuse before dispatch.
+//
+// [real DOM, Chromium only: the trusted native-button path is Chrome CDP]
+// The real Agent, page-feedback run, page monitor, accessibility tree and
+// content script run unchanged; only the extension transport and debugger
+// session are bridged to Playwright.
+import { chromium } from 'playwright';
+import { readFile } from 'node:fs/promises';
+import { fileURLToPath, pathToFileURL } from 'node:url';
+import path from 'node:path';
+import assert from 'node:assert/strict';
+import { test } from 'node:test';
+import { cdpClient } from '../src/chrome/src/cdp/cdp-client.js';
+
+const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
+const area = { get: async () => ({}), set: async () => {}, remove: async () => {} };
+let page, session, commandHook = null;
+const commands = [];
+const requests = new Set();
+const api = {
+ storage: { local: area, session: area },
+ runtime: { getURL: p => pathToFileURL(path.join(root, 'src/chrome', p)).href },
+ tabs: {
+ get: async () => ({ id: 17, url: page.url() }),
+ query: async () => [{ id: 17, url: page.url() }],
+ sendMessage: (_id, message) => page.evaluate(message => window.qaMessage(message), message),
+ },
+ webNavigation: { getAllFrames: async () => [{ frameId: 0, documentId: 'delivery-native-document', url: page.url() }] },
+ webRequest: { onBeforeRequest: { addListener: l => requests.add(l), removeListener: l => requests.delete(l) } },
+ debugger: { sendCommand: (_target, method, params, callback) => {
+ commands.push({ method, type: params.type, x: params.x, y: params.y });
+ Promise.resolve().then(() => commandHook?.(method, params))
+ .then(() => session.send(method, params)).then(callback, error => {
+ api.runtime.lastError = { message: error.message };
+ callback(); delete api.runtime.lastError;
+ });
+ } },
+};
+globalThis.chrome = api;
+globalThis.browser = api;
+const { Agent } = await import('../src/chrome/src/agent/agent.js');
+
+const html = ({ css = '', script = '' } = {}) => `<!doctype html><style>
+body{margin:20px} #commit{width:200px;height:45px} ${css}</style>
+<main><h1>Current record settings</h1><button id="commit" type="button">Apply settings</button>
+<label>Record title<input value="Owned neutral draft"></label><output id="result">0</output></main>
+<script>window.qaClicks=[];
+commit.addEventListener('click',event=>{qaClicks.push({trusted:event.isTrusted,x:event.clientX,y:event.clientY});
+if(event.isTrusted){result.textContent=String(qaClicks.length);commit.setAttribute('aria-pressed',String(qaClicks.length%2===1));}});
+${script}</script>`;
+
+async function fixture(browserInstance, options) {
+ const context = await browserInstance.newContext({ viewport: { width: 900, height: 700 } });
+ await context.route('**/*', route => route.request().url().startsWith('https://delivery-native.invalid/')
+ ? route.fulfill({ contentType: 'text/html', body: html(options) }) : route.abort());
+ page = await context.newPage();
+ const agent = new Agent({ getActive: () => ({ model: 'delivery-model' }) });
+ agent._runningTabs.add(17);
+ await page.exposeFunction('qaBackground', message => {
+ const sender = { tab: { id: 17 }, frameId: 0, documentId: 'delivery-native-document', url: page.url() };
+ if (message.action === 'get_page_monitor_state') return agent.pageMonitorState(sender, message.documentToken);
+ if (message.action === 'page_feedback') return agent.observePageFeedback(sender, message.feedback);
+ return {};
+ });
+ await page.addInitScript(() => {
+ window.qaListeners = [];
+ window.chrome = window.browser = { runtime: {
+ getURL: p => p,
+ onMessage: { addListener: f => qaListeners.push(f), removeListener() {} },
+ sendMessage: message => window.qaBackground(message),
+ } };
+ window.qaMessage = message => new Promise(resolve => {
+ let answered = false;
+ const respond = value => { answered = true; resolve(value); };
+ for (const f of qaListeners) { const value = f(message, {}, respond); if (answered || value === true) return; }
+ resolve(undefined);
+ });
+ });
+ for (const name of ['rich-text-toolbar-heuristic.js', 'file-upload-policy.js', 'accessibility-tree.js', 'page-monitor.js', 'content.js']) {
+ await page.addInitScript({ content: await readFile(path.join(root, 'src/chrome/src/content', name), 'utf8') });
+ }
+ await page.goto('https://delivery-native.invalid/current');
+ session = await context.newCDPSession(page);
+ await agent._beginPageFeedbackRun(17, 'neutral');
+ await page.waitForFunction(() => window.__wbPageMonitor?.active);
+ await page.mouse.move(5, 690);
+ await page.waitForTimeout(180);
+ const run = agent._pageFeedbackRuns.get(17);
+ run.events.clear();
+ const axScope = await api.tabs.sendMessage(17, { target: 'content', action: 'get_accessibility_tree', params: { filter: 'visible', maxChars: 12000 } });
+ const capture = await api.tabs.sendMessage(17, { target: 'content', action: 'page_monitor_capture_model',
+ params: { runToken: run.token, includeTree: true } });
+ const ref = capture.page.pageContent.match(/button "Apply settings" \[(ref_[A-Za-z0-9_-]+)\]/)?.[1];
+ assert.ok(ref, capture.page.pageContent);
+ const state = { token: run.token, url: run.url, steeringRevision: 0, frames: new Map(run.frames),
+ actionBinding: { snapshotToken: capture.snapshotToken, runToken: run.token, documentToken: capture.documentToken } };
+ run.modelState = state;
+ run.dispatchCall = { name: 'click_ax', args: { ref_id: ref }, expectedModelSnapshot: capture.snapshotToken,
+ modelState: state, dispatchState: { started: false }, allowPassiveRebase: true };
+ const prepared = await api.tabs.sendMessage(17, { target: 'content', action: 'page_monitor_prepare',
+ params: { runToken: run.token, tool: 'click_ax', ref_id: ref, operationId: 'delivery-owned-operation',
+ expectedModelSnapshot: capture.snapshotToken, allowPassiveRebase: true } });
+ assert.equal(prepared.modelBindingValid, true);
+ commands.length = 0; commandHook = null;
+ agent._settleContentFilePickerGuard = async (_id, result) => result;
+ agent._annotateClickProgress = async (_id, _name, _args, result) => result;
+ agent._recordInteractionRect = () => {};
+ agent._clearUploadSelectorRecoveryAfterInspection = () => {};
+ agent._lastAxScopes.set(17, { documentToken: axScope.documentToken, pageUrl: axScope.refScopeUrl });
+ return { context, agent, ref };
+}
+
+const presses = () => commands.filter(call => call.method === 'Input.dispatchMouseEvent' && call.type === 'mousePressed');
+const moves = () => commands.filter(call => call.method === 'Input.dispatchMouseEvent' && call.type === 'mouseMoved');
+
+test('Chrome: the agent\'s own hover may move the certified native button, never onto another node', { timeout: 240000 }, async t => {
+ const cdpAttach = cdpClient.attach;
+ cdpClient.attach = async () => cdpClient.sessions.set(17, {});
+ const browserInstance = await chromium.launch({ headless: true });
+ try {
+ for (const [name, css] of [
+ ['an instant hover lift', '#commit:hover { transform: translateY(-3px); }'],
+ ['a hover border width', '#commit { width: auto; height: auto; border: 1px solid #333; } #commit:hover { border-width: 4px; }'],
+ ]) await t.test(`${name}: one trusted click at the verified current centre`, async () => {
+ const f = await fixture(browserInstance, { css });
+ try {
+ const result = await f.agent.executeTool(17, 'click_ax', { ref_id: f.ref });
+ const clicks = await page.evaluate(() => window.qaClicks);
+ assert.equal(result.success, true, JSON.stringify(result));
+ assert.equal(result.initialTrustedNativeButton, true);
+ assert.deepEqual(clicks.map(click => click.trusted), [true], 'exactly one trusted activation');
+ assert.equal(presses().length, 1);
+ const box = await page.locator('#commit').boundingBox();
+ const centre = { x: Math.round(box.x + box.width / 2), y: Math.round(box.y + box.height / 2) };
+ assert.deepEqual({ x: presses()[0].x, y: presses()[0].y }, centre, 'the press used the hovered button\'s current centre');
+ assert.notDeepEqual({ x: moves()[0].x, y: moves()[0].y }, centre, 'the hover itself moved the button');
+ } finally { await f.context.close(); }
+ });
+
+ await t.test('an unmoved button keeps its single trusted click', async () => {
+ const f = await fixture(browserInstance, {});
+ try {
+ const result = await f.agent.executeTool(17, 'click_ax', { ref_id: f.ref });
+ assert.equal(result.success, true);
+ assert.deepEqual((await page.evaluate(() => window.qaClicks)).map(click => click.trusted), [true]);
+ assert.equal(presses().length, 1);
+ } finally { await f.context.close(); }
+ });
+
+ for (const [name, options] of [
+ ['a hover-revealed overlay covering the button', {
+ css: '#cover{position:absolute;display:none;left:10px;top:60px;width:400px;height:120px;background:#fff;z-index:5} main:hover ~ #cover{display:block}',
+ script: "const cover=document.createElement('div');cover.id='cover';document.body.append(cover);",
+ }],
+ ['a hover move that puts another node over the new centre', {
+ css: '#commit:hover{transform:translateY(-3px)} #shield{position:absolute;display:none;z-index:5;background:#eee} main:hover ~ #shield{display:block}',
+ script: "const shield=document.createElement('div');shield.id='shield';const r=commit.getBoundingClientRect();"
+ + "Object.assign(shield.style,{left:r.left+'px',top:(r.top-3)+'px',width:r.width+'px',height:(r.height/2+2)+'px'});document.body.append(shield);",
+ }],
+ ['a hover that replaces the button node', {
+ script: "commit.addEventListener('mouseover',()=>{const next=commit.cloneNode(true);next.id='commit';commit.replaceWith(next);},{once:true});",
+ }],
+ ]) await t.test(`${name} refuses before any press`, async () => {
+ const f = await fixture(browserInstance, options);
+ try {
+ const result = await f.agent.executeTool(17, 'click_ax', { ref_id: f.ref });
+ assert.equal(result.success, false);
+ assert.equal(result.noDispatch, true, JSON.stringify(result));
+ assert.equal(presses().length, 0, 'no press was sent');
+ assert.deepEqual(await page.evaluate(() => window.qaClicks), []);
+ } finally { await f.context.close(); }
+ });
+ } finally {
+ cdpClient.attach = cdpAttach;
+ await browserInstance.close();
+ }
+});
diff --git a/test/delivery-stop-compound.mjs b/test/delivery-stop-compound.mjs
new file mode 100644
index 000000000..c2cfea208
--- /dev/null
+++ b/test/delivery-stop-compound.mjs
@@ -0,0 +1,146 @@
+// Delivery regression (C8): Stop between the write and the submit of a
+// compound content action. The real set_field handler of each build runs in a
+// Playwright page of the matching engine with the real page monitor; only the
+// extension messaging transport is mocked. Stop is the agent's actual
+// run-finish notification (page_monitor_state active:false for the owning run).
+import assert from 'node:assert/strict';
+import fs from 'node:fs';
+import { test } from 'node:test';
+import { chromium, firefox } from 'playwright';
+
+const form = `<!doctype html><main><h1>Search</h1>
+<form id="f" action="/results" method="get"><label>Query <input id="q" name="q"></label>
+<button type="submit">Go</button></form></main>
+<script>window.qaSubmits = 0; window.qaEnters = 0;
+q.addEventListener('keydown', event => { if (event.key === 'Enter') qaEnters++; });
+f.addEventListener('submit', event => { event.preventDefault(); qaSubmits++; });</script>`;
+
+// A form-less combobox commits through page-owned keyboard handlers instead
+// of requestSubmit; Stop must also prevent its ArrowDown/Enter phase.
+const combobox = `<!doctype html><main><h1>Pick a city</h1>
+<div><input id="q" role="combobox" aria-autocomplete="list" aria-controls="cities" aria-label="City"></div>
+<ul id="cities" role="listbox"><li role="option">Lisbon</li></ul></main>
+<script>window.qaSubmits = 0; window.qaEnters = 0; window.qaArrows = 0;
+q.addEventListener('keydown', event => { if (event.key === 'Enter') { qaEnters++; qaSubmits++; } if (event.key === 'ArrowDown') qaArrows++; });</script>`;
+
+async function fixture(browserInstance, build, body) {
+ const context = await browserInstance.newContext({ viewport: { width: 900, height: 600 } });
+ await context.route('https://stop.test/**', route => route.fulfill({ contentType: 'text/html', body }));
+ await context.addInitScript(() => {
+ window.qaListeners = [];
+ window.qaRunToken = 'run-owner';
+ const runtime = {
+ onMessage: { addListener: fn => qaListeners.push(fn), removeListener: fn => {
+ const index = qaListeners.indexOf(fn); if (index >= 0) qaListeners.splice(index, 1);
+ } },
+ getURL: value => value,
+ async sendMessage(message) {
+ if (message.action === 'get_page_monitor_state') {
+ return window.qaRunToken
+ ? { active: true, runToken: window.qaRunToken, documentToken: message.documentToken }
+ : { active: false };
+ }
+ return { accepted: true };
+ },
+ };
+ window.chrome = { runtime }; window.browser = window.chrome;
+ // Like the browser runtime, every listener in the content world receives
+ // the message; the first sendResponse answers it.
+ window.qaDeliver = message => new Promise(resolve => {
+ let answered = false, pending = false;
+ const respond = value => { if (!answered) { answered = true; resolve(value); } };
+ for (const listener of [...qaListeners]) {
+ if (listener(message, {}, respond) === true) pending = true;
+ }
+ if (!answered && !pending) resolve(undefined);
+ });
+ });
+ for (const file of ['file-upload-policy.js', 'rich-text-toolbar-heuristic.js', 'accessibility-tree.js', 'page-monitor.js', 'content.js']) {
+ await context.addInitScript({ content: fs.readFileSync(new URL(`../src/${build}/src/content/${file}`, import.meta.url), 'utf8') });
+ }
+ const page = await context.newPage();
+ await page.goto('https://stop.test/start');
+ await page.waitForFunction(() => window.__wbPageMonitor?.active === true);
+ await page.waitForTimeout(200);
+ const ref = await page.evaluate(() => {
+ window.__generateAccessibilityTree?.('all', 15, 6000);
+ return window.__wb_ax_ref(document.getElementById('q'));
+ });
+ return { context, page, ref };
+}
+
+async function setFieldWithStop(page, ref, { stopAfterMs = null, text = 'webbrain' } = {}) {
+ return page.evaluate(async ({ ref, stopAfterMs, text }) => {
+ const operation = window.qaDeliver({ target: 'content', action: 'set_field',
+ params: { ref_id: ref, text, submit: true } });
+ if (stopAfterMs !== null) {
+ await new Promise(resolve => setTimeout(resolve, stopAfterMs));
+ // The agent's run finish (Stop) notifies every frame for the owning run.
+ await window.qaDeliver({ target: 'content', action: 'page_monitor_state', active: false, runToken: window.qaRunToken });
+ window.qaRunToken = '';
+ }
+ const result = await operation;
+ await new Promise(resolve => setTimeout(resolve, 250));
+ return { result, submits: window.qaSubmits, enters: window.qaEnters, value: document.getElementById('q').value,
+ arrows: window.qaArrows || 0 };
+ }, { ref, stopAfterMs, text });
+}
+
+for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {
+ test(`${build}: Stop between a set_field write and its submit prevents the submission`, async t => {
+ const browserInstance = await engine.launch({ headless: true });
+ try {
+ for (const [name, body] of [['native form submit', form], ['form-less combobox Enter', combobox]]) {
+ await t.test(`${name}: Stop during the settle wait sends no submission`, async () => {
+ const { context, page, ref } = await fixture(browserInstance, build, body);
+ try {
+ const observed = await setFieldWithStop(page, ref, { stopAfterMs: 20 });
+ assert.equal(observed.submits, 0, 'no form submission may follow Stop');
+ assert.equal(observed.enters, 0, 'no Enter may follow Stop');
+ assert.equal(observed.arrows, 0, 'no combobox commit key may follow Stop');
+ assert.equal(observed.value, 'webbrain', 'the write itself already happened');
+ const { result } = observed;
+ assert.equal(result.success, false);
+ assert.equal(result.dispatched, true, 'the field write was delivered');
+ assert.equal(result.submitted, false);
+ assert.equal(result.submissionDispatched, false);
+ assert.equal(result.submissionBlockedBeforeDispatch, true);
+ assert.equal(result.cancelled, true);
+ assert.equal(result.verified, true, 'the write outcome is reported honestly');
+ } finally { await context.close(); }
+ });
+
+ await t.test(`${name}: without Stop the value is set and submitted exactly once`, async () => {
+ const { context, page, ref } = await fixture(browserInstance, build, body);
+ try {
+ const observed = await setFieldWithStop(page, ref);
+ assert.equal(observed.value, 'webbrain');
+ assert.equal(observed.submits, 1);
+ assert.equal(observed.result.success, true);
+ assert.equal(observed.result.verified, true);
+ assert.equal(observed.result.submissionDispatched, true);
+ } finally { await context.close(); }
+ });
+ }
+
+ await t.test('a stale Stop for another run does not cancel the current submission', async () => {
+ const { context, page, ref } = await fixture(browserInstance, build, form);
+ try {
+ const observed = await page.evaluate(async ref => {
+ const operation = window.qaDeliver({ target: 'content', action: 'set_field',
+ params: { ref_id: ref, text: 'webbrain', submit: true } });
+ await new Promise(resolve => setTimeout(resolve, 20));
+ await window.qaDeliver({ target: 'content', action: 'page_monitor_state', active: false, runToken: 'older-run' });
+ const result = await operation;
+ await new Promise(resolve => setTimeout(resolve, 250));
+ return { result, submits: window.qaSubmits };
+ }, ref);
+ assert.equal(observed.submits, 1);
+ assert.equal(observed.result.success, true);
+ } finally { await context.close(); }
+ });
+ } finally {
+ await browserInstance.close();
+ }
+ });
+}
diff --git a/test/run.js b/test/run.js
index 6c44345c8..ff8ba2fe0 100644
--- a/test/run.js
+++ b/test/run.js
@@ -81703,7 +81703,7 @@ test('set_field submit chooses exactly one native or page-owned commit path', as
assert.ok(branchStart >= 0 && branchEnd > branchStart && blockStart > branchStart && blockEnd > blockStart && blockEnd < branchEnd, `${label}: submit block must remain inside the set_field handler`);
const block = source.slice(blockStart, blockEnd);
const runner = vm.runInNewContext(`async (stubs) => {
- const { el, msg, failure, isCombobox, _setFieldUsesNativeSubmit, _consumeMessageRecipientDispatchBinding, actionDeadlineExpired, deadlineFailure, ref_id, rect } = stubs;
+ const { el, msg, failure, isCombobox, _setFieldUsesNativeSubmit, _consumeMessageRecipientDispatchBinding, actionDeadlineExpired, deadlineFailure, runStopped, stoppedBeforeSubmit, ref_id, rect } = stubs;
let nativeSubmitAttempted = false;
let submissionOutcomeUnknown = true;
let submissionDispatched = false;
@@ -81775,6 +81775,9 @@ test('set_field submit chooses exactly one native or page-owned commit path', as
_consumeMessageRecipientDispatchBinding: () => ({ success: true }),
actionDeadlineExpired: () => false,
deadlineFailure: () => { throw new Error('unexpected deadline failure'); },
+ // The submit block now rechecks Stop before each commit key.
+ runStopped: () => false,
+ stoppedBeforeSubmit: () => { throw new Error('unexpected run stop'); },
ref_id: 'ref_1',
rect: { x: 0, y: 0, w: 1, h: 1 },
});
@@ -114501,7 +114504,9 @@ test('Chrome primary selector clicks classify deadline expiry at mouse dispatch'
const delayedClick = new Promise(resolve => { releaseClick = resolve; });
cdpClientCh.clickElement = async (_tabId, _selector, options) => {
assert.ok(options.abortSignal instanceof AbortSignal);
- if (dispatchStarted) await options.beforeDispatch({ x: 10, y: 20 });
+ // Authorization precedes the press; dispatch is recorded when the
+ // press reaches the transport boundary (onDispatch).
+ if (dispatchStarted) { await options.beforeDispatch({ x: 10, y: 20 }); options.onDispatch?.(); }
markClickStarted();
await delayedClick;
return { success: false, dispatched: dispatchStarted, noDispatch: !dispatchStarted };