Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
Judge automatic page activity by the action footprint, not as intervention
Claudee15267e19de9
diff --git a/src/chrome/src/content/page-monitor.js b/src/chrome/src/content/page-monitor.jsindex e02b06da5..bf9f498f4 100644--- a/src/chrome/src/content/page-monitor.js+++ b/src/chrome/src/content/page-monitor.js@@ -36,6 +36,9 @@let pointerHeld = false, composing = false, localOperation = null, controlIterator = null;let preparedOperationCursor = 0;let requestGeneration = 0, unreported = 0, interventionUnreported = 0, pendingDOM = null;+ // Every non-passive observation (user, unknown, page focus/input/scroll,+ // frame resize). Legacy bindings without a private footprint keep this fence.+ let uncertainRevision = 0, uncertainUnreported = 0;let interventionDiagnostic = '';let lastViewport = '';let agentResize = null;@@ -141,11 +144,18 @@publishRevision();const feedback = { ...event, runToken, documentToken, seq: ++seq, revision };const pending = event.source !== 'agent';- const intervention = pending && (event.kind !== 'dom' || event.source !== 'page');+ // Only genuine human input and a top-level viewport change (window size,+ // zoom, browser chrome) invalidate model-bound decisions by themselves.+ // Automatic or uncertain observations still advance `revision`, so strict+ // fences remain; a certificate's private action footprint judges them.+ const intervention = pending && (event.source === 'user'+ || (event.kind === 'resize' && event.source === 'unknown' && window === window.top));+ const uncertain = pending && (event.kind !== 'dom' || event.source !== 'page');const owner = runToken;if (pending) unreported++;- if (intervention) {- interventionRevision++; interventionUnreported++;+ if (intervention) { interventionRevision++; interventionUnreported++; }+ if (uncertain) {+ uncertainRevision++; uncertainUnreported++;if (!(event.kind === 'dom' && event.source === 'unknown' && interventionDiagnostic.startsWith('dom.'))) {const kind = ['dom', 'activity', 'input', 'click', 'selection', 'scroll', 'navigation', 'resize'].includes(event.kind) ? event.kind : 'other';const source = ['user', 'page', 'unknown'].includes(event.source) ? event.source : 'other';@@ -154,9 +164,14 @@}try {const delivery = Promise.resolve(api.runtime.sendMessage({ target: 'background', action: 'page_feedback', feedback }))- .then(() => { if (owner === runToken) {+ .then(reply => { if (owner === runToken) {if (pending) unreported = Math.max(0, unreported - 1);if (intervention) interventionUnreported = Math.max(0, interventionUnreported - 1);+ if (uncertain) uncertainUnreported = Math.max(0, uncertainUnreported - 1);+ // The background no longer owns this run (for example after an MV3+ // worker restart). Stop observing instead of waking it for every+ // page update; a new run reactivates this document by handshake.+ if (reply?.accepted === false && reply.reason === 'stale-observation') stop();} }).catch(() => stop());if (pending) lastFeedbackDelivery = delivery;@@ -174,7 +189,7 @@if (!proof?.viewportNotificationPending || proof.scrollPreparationUntil < Date.now()|| proof.runToken !== runToken || proof.documentToken !== documentToken|| proof.userAt !== lastUserAt || proof.interventionRevision !== interventionRevision- || deepActiveElement() !== op.preparedBinding.target) continue;+ || (proof.focusRequired !== false && deepActiveElement() !== op.preparedBinding.target)) continue;const before = proof.binding, after = before.relaxed? relaxedTargetBinding(before.target, before.grantToken) : actionFootprint(before.target, before.readonlyPreparation);if (!after) continue;@@ -386,6 +401,10 @@send({ kind: 'dom', source, target: targetName(target || currentHit) });return true;}+ function physicalInputEvent(event) {+ return event?.isTrusted === true && (['keydown', 'keyup', 'keypress', 'pointerdown', 'pointerup', 'mousedown', 'mouseup'].includes(event.type)+ || (event.type === 'click' && event.detail > 0));+ }function expected(kind, el, event, nativeMarkerOnly = false) {let activatedNativeMarker = false;const markerNode = event?.composedPath?.().find(node => node instanceof Element && node.hasAttribute(nativeMarkerAttribute))@@ -427,6 +446,10 @@prune();const match = op => {if (event && !event.isTrusted && !op.synchronous) return null;+ // A content-script operation dispatches synthetic events only. A trusted+ // key or pointer press/release (or a click with a pointer detail) during+ // it is physical input; its execCommand/focus/scroll effects still match.+ if (op.localSynthetic === true && physicalInputEvent(event)) return null;if (event) {const keyboardClick = kind === 'click' && event.type === 'click' && event.isTrusted === true && op.keyboardClickTarget&& related(op.keyboardClickTarget, el);@@ -448,6 +471,12 @@if (kind === 'focus' && op.focusEligible && op.focusTarget === el && lastUserAt <= op.userAt) {const found = match(op); if (found) return found;}+ // Its own synchronous preparation focus commits the field losing focus.+ // The user's earlier edits there were reported by their own input events.+ if (kind === 'input' && event?.type === 'change' && op.preparationBlur?.target === el+ && lastUserAt <= op.preparationBlur.userAt) {+ const found = match(op); if (found) return found;+ }if (!op.dispatched) continue;if (lastUserAt > op.userAt) continue;// Keyboard-activated clicks can report detail=1 in Firefox. Do not let@@ -661,6 +690,49 @@for (const slot of [3, 4, 5]) current[slot] = original[slot];return samePreparedBinding(before, { ...after, hit: before.hit, context: JSON.stringify(current) });}+ // Share a derived geometry proof for this original certificate. Callers+ // have checked samePreparationSemantics; private semantic values stay fixed.+ function rebaseOwnedGeometry(binding, after, ownedWindowScroll = false) {+ const previous = ownedPreparationBindings.get(binding);+ const current = previous && preparedBindingFor(binding) === previous.binding ? previous : null;+ ownedPreparationBindings.set(binding, { binding: after, runToken, documentToken, userAt: lastUserAt, interventionRevision,+ // An owned window scroll may be followed by one late subpixel visual+ // viewport notification (as after typing preparation); a click target+ // need not hold focus for that bounded reconciliation.+ ...(ownedWindowScroll ? { scrollPreparationUntil: Date.now() + 200, viewportNotificationPending: true, focusRequired: false }+ : { scrollPreparationUntil: current?.scrollPreparationUntil || 0, viewportNotificationPending: current?.viewportNotificationPending === true,+ ...(current?.focusRequired === false ? { focusRequired: false } : {}) }) });+ }+ // Decision-level validation (model response, preparation). The model chose+ // this exact node by ref/selector, not by coordinates: when only scroll+ // offsets or the target rectangle moved and the hit is still the target,+ // the decision stands and the proof is rebased to the current geometry.+ // Exact geometry/hit checks then apply from preparation until dispatch.+ function modelDecisionPredicate(before, after) {+ const current = preparedBindingFor(before);+ const predicate = bindingPredicate(current, after);+ if (predicate !== 'geometry_changed') return predicate;+ // Another node receiving the hit (for example an interactive or named+ // descendant replacing a decorative icon) is not a pure move.+ if (current.hit !== after.hit) return 'target_hit_changed';+ rebaseOwnedGeometry(before, after);+ return '';+ }+ // An owned synchronous phase (selector scroll, pointer hover) may move the+ // certified node. Refresh its derived geometry proof once, at the end of+ // that phase, only for the same node with identical semantics and hit.+ // Any later geometry change is still checked exactly before dispatch.+ function refreshOwnedGeometry(op, phase) {+ if (!active || !phase || op?.ownedGeometryPhase !== phase || operations.get(op.operationId) !== op) return;+ op.ownedGeometryPhase = null;+ // Relaxed retry bindings carry no geometry; their grant rules apply instead.+ if (op.preparedBinding?.model !== true || op.preparedBinding.relaxed || op.allowPassiveRebase !== true+ || op.modelBindingInvalid || op.layoutInvalidated || authorityChanged(op)) return;+ const before = preparedBindingFor(op.preparedBinding);+ const after = actionFootprint(op.preparedBinding.target, op.preparedBinding.readonlyPreparation);+ if (samePreparationSemantics(before, after) && before.hit === after.hit) rebaseOwnedGeometry(op.preparedBinding, after,+ phase.kind === 'selector-scroll' && (phase.scrollX !== window.scrollX || phase.scrollY !== window.scrollY));+ }function bindingPredicate(before, after) {if (!before || !after) return 'semantic_binding_changed';if (before.target !== after.target || before.scope !== after.scope) return 'target_changed';@@ -1721,7 +1793,7 @@if (snapshot.relaxedGrantToken && !relaxed) return { reason: 'relaxed_grant_expired' };if (snapshot.userAt !== lastUserAt || (!relaxed && (snapshot.interventionRevision !== interventionRevision || interventionUnreported)))return { reason: 'intervention_changed' };- if (!relaxed && pendingDOM && pendingDOM.source !== 'page') return { reason: 'pending_feedback' };+ if (!relaxed && pendingDOM?.source === 'user') return { reason: 'pending_feedback' };const target = modelTarget(params);if (!target) {if (relaxed) { revokeRelaxedGrant(); return { reason: 'target_unresolved' }; }@@ -1749,7 +1821,7 @@}if (before?.readonlyPreparation && params.tool !== 'click_ax') return {};if (before) {- const reason = bindingPredicate(preparedBindingFor(before), actionFootprint(target, before.readonlyPreparation));+ const reason = modelDecisionPredicate(before, actionFootprint(target, before.readonlyPreparation));return reason ? { reason } : { binding: before };}const current = modelCandidateIdentity(target);@@ -2067,11 +2139,17 @@...(params.observationRef ? { observationScopeCaptured } : {}),...(page ? { page } : {}) };}+ // A certified operation whose only dispatch so far was its own preparation+ // (selector scroll, pointer hover, typing scroll/focus). Its footprint is+ // checked, and can be rebased over passive churn, at the next boundary.+ function ownedPreparationPhase(op) {+ return op?.pointerPreparationOnly === true || op?.typingPreparationOnly === true || op?.selectorPreparationOnly === true;+ }function preparedBindingChanged(op, { allowFocusRebind = false } = {}) {if (op?.allowPassiveRebase === true && op.preparedBinding?.model === true && op.focusBound&& !allowFocusRebind && deepActiveElement() !== op.focusTarget) return true;return op?.allowPassiveRebase === true && !!op.preparedBinding- && (!op.dispatched || (op.preparedBinding.model === true && (op.kind === 'input' || op.pointerPreparationOnly === true || op.typingPreparationOnly === true)))+ && (!op.dispatched || (op.preparedBinding.model === true && (op.kind === 'input' || ownedPreparationPhase(op))))&& !operationBindingMatches(op);}function nativeReceiverPredicate(op, params) {@@ -2100,14 +2178,21 @@? op.focusTarget : op.preparedBinding.target;return nativeReceiverBelongsTo(target, receiver) ? '' : 'target_changed';}+ // Model-bound operations are fenced by genuine intervention only; their+ // private footprint judges automatic page activity. Legacy bindings keep+ // the previous fence on every uncertain (non-passive) observation.+ function authorityChanged(op) {+ if (!op || lastUserAt > op.userAt) return true;+ return op.preparedBinding?.model === true+ ? op.preparedInterventionRevision !== interventionRevision || interventionUnreported > 0 || pendingDOM?.source === 'user'+ : op.preparedUncertainRevision !== uncertainRevision || uncertainUnreported > 0 || (!!pendingDOM && pendingDOM.source !== 'page');+ }function rebasePassivePreparation(op, params = {}) {const relaxed = op?.preparedBinding?.relaxed === true && currentRelaxedGrant(op.preparedBinding.grantToken);const continuingInput = op?.dispatched && op.kind === 'input' && op.preparedBinding?.model === true;- const continuingPreparation = op?.dispatched && (op.pointerPreparationOnly === true || op.typingPreparationOnly === true) && op.preparedBinding?.model === true;+ const continuingPreparation = op?.dispatched && ownedPreparationPhase(op) && op.preparedBinding?.model === true;if (!active || !op || op.allowPassiveRebase !== true || (op.dispatched && !continuingInput && !continuingPreparation) || op.coordinateSensitive || op.layoutInvalidated- || op.until < Date.now() || lastUserAt > op.userAt || (!relaxed && interventionUnreported)- || (!relaxed && op.preparedInterventionRevision !== interventionRevision)- || (!relaxed && pendingDOM && pendingDOM.source !== 'page')+ || op.until < Date.now() || lastUserAt > op.userAt || (!relaxed && authorityChanged(op))|| (params.documentToken && params.documentToken !== documentToken)) return false;const independentNavigation = op.tool === 'navigate';if (independentNavigation) {@@ -2125,11 +2210,7 @@}// Do not suppress feedback. Flush it for the agent while rebasing only this// exact, freshly revalidated operation over unrelated passive mutations.- if (domTimer) {- clearTimeout(domTimer); domTimer = null;- const observation = pendingDOM; pendingDOM = null;- if (observation) send(observation);- }+ if (domTimer) flushPendingDOM();op.preparedRevision = revision;return true;}@@ -2218,6 +2299,11 @@}return { nodes, exceeded: false };}+ const rootHandlers = [['animationend', checkSettledAnimation], ['animationcancel', checkSettledAnimation],+ ['transitionend', checkSettledAnimation], ['transitioncancel', checkSettledAnimation],+ ['beforetoggle', onBeforeToggle], ['toggle', onToggle]];+ const shadowListenerRoots = new Set();+ let shadowListenerSweepAt = 256;function observeRoot(root, budget) {if (!root || roots.has(root) || !observer) return;roots.add(root);@@ -2227,33 +2313,50 @@'aria-expanded', 'aria-selected', 'aria-checked', 'aria-pressed', 'aria-disabled','type', 'href', 'target', 'download', 'action', 'method', 'formaction', 'formmethod', 'formtarget', 'novalidate', 'formnovalidate','aria-valuenow', 'aria-valuetext', 'aria-hidden', 'hidden', 'disabled', 'readonly', 'checked', 'selected', 'open', 'popover', 'inert', 'class', 'style'] });- for (const name of ['animationend', 'animationcancel', 'transitionend', 'transitioncancel'])- listen(root, name, checkSettledAnimation);- listen(root, 'beforetoggle', event => {- const el = elementFor(event);- if (ignored(el) || !el.hasAttribute('popover') || !('popover' in el)) return;- const mark = { observer, oldState: event.oldState, newState: event.newState, notified: false,- agentUserAt: agentTurn && lastUserAt <= agentTurn.userAt ? agentTurn.userAt : undefined };- popoverTurns.set(el, mark);- // Check after page handlers can cancel opening, but before an action can- // dispatch in the next task. Retain exact attribution for the later toggle.- queueMicrotask(() => {- if (!active || observer !== mark.observer || popoverTurns.get(el) !== mark || event.defaultPrevented- || popoverOpen(el) !== (mark.newState === 'open')) return;- mark.notified = true;- onMutations([{ type: 'popover', target: el, stateChanged: mark.oldState !== mark.newState, agentUserAt: mark.agentUserAt }]);- });- });- listen(root, 'toggle', event => {- const el = elementFor(event);- if (ignored(el) || !el.hasAttribute('popover') || !('popover' in el)- || popoverOpen(el) !== (event.newState === 'open')) return;- const mark = popoverTurns.get(el);- if (mark?.observer === observer && mark.newState === event.newState && mark.notified) return;- onMutations([{ type: 'popover', target: el, stateChanged: event.oldState !== event.newState }]);- });+ // Animation and popover events are not composed, so each open shadow root+ // needs its own listeners. Track those roots weakly: a removed component+ // must stay collectable while the run continues.+ if (root === document) for (const [name, handler] of rootHandlers) listen(root, name, handler);+ else {+ for (const [name, handler] of rootHandlers) root.addEventListener(name, handler, { capture: true, passive: true });+ shadowListenerRoots.add(new WeakRef(root));+ if (shadowListenerRoots.size >= shadowListenerSweepAt) {+ for (const reference of shadowListenerRoots) if (!reference.deref()) shadowListenerRoots.delete(reference);+ shadowListenerSweepAt = Math.max(256, shadowListenerRoots.size * 2);+ }+ }seed(root, budget);}+ function removeShadowRootListeners() {+ for (const reference of shadowListenerRoots) {+ const root = reference.deref();+ if (root) for (const [name, handler] of rootHandlers) root.removeEventListener(name, handler, true);+ }+ shadowListenerRoots.clear(); shadowListenerSweepAt = 256;+ }+ function onBeforeToggle(event) {+ const el = elementFor(event);+ if (!active || ignored(el) || !el.hasAttribute('popover') || !('popover' in el)) return;+ const mark = { observer, oldState: event.oldState, newState: event.newState, notified: false,+ agentUserAt: agentTurn && lastUserAt <= agentTurn.userAt ? agentTurn.userAt : undefined };+ popoverTurns.set(el, mark);+ // Check after page handlers can cancel opening, but before an action can+ // dispatch in the next task. Retain exact attribution for the later toggle.+ queueMicrotask(() => {+ if (!active || observer !== mark.observer || popoverTurns.get(el) !== mark || event.defaultPrevented+ || popoverOpen(el) !== (mark.newState === 'open')) return;+ mark.notified = true;+ onMutations([{ type: 'popover', target: el, stateChanged: mark.oldState !== mark.newState, agentUserAt: mark.agentUserAt }]);+ });+ }+ function onToggle(event) {+ const el = elementFor(event);+ if (!active || ignored(el) || !el.hasAttribute('popover') || !('popover' in el)+ || popoverOpen(el) !== (event.newState === 'open')) return;+ const mark = popoverTurns.get(el);+ if (mark?.observer === observer && mark.newState === event.newState && mark.notified) return;+ onMutations([{ type: 'popover', target: el, stateChanged: event.oldState !== event.newState }]);+ }function popoverOpen(el) {return el.hasAttribute('popover') && 'popover' in el && el.matches(':popover-open');}@@ -2558,11 +2661,24 @@for (const op of operations.values())if (!op.dispatched && op.target && op.until >= Date.now()) op.preparedTargetSignature = signature(op.target);if (!changed) return;- if (source !== 'page' && source !== 'agent') interventionRevision++;+ // DOM mutations alone never establish human origin. A user-turn change is+ // an intervention; unknown attribution remains a strict legacy fence only.+ if (source === 'user') interventionRevision++;+ if (source !== 'page' && source !== 'agent') uncertainRevision++;revision++; publishRevision();clearTimeout(domTimer);- pendingDOM = { kind: 'dom', source, target };- domTimer = setTimeout(() => { domTimer = null; const observation = pendingDOM; pendingDOM = null; send(observation); }, 150);+ // Coalescing keeps the strongest attribution in this window. A later+ // passive batch must not relabel an earlier user or unknown change.+ if (!pendingDOM || domSeverity(source) >= domSeverity(pendingDOM.source)) pendingDOM = { kind: 'dom', source, target };+ domTimer = setTimeout(flushPendingDOM, 150);+ }+ function domSeverity(source) {+ return source === 'user' ? 3 : source === 'unknown' ? 2 : source === 'page' ? 1 : 0;+ }+ function flushPendingDOM() {+ clearTimeout(domTimer); domTimer = null;+ const observation = pendingDOM; pendingDOM = null;+ if (observation) send(observation);}function observeLayoutShifts() {if (typeof PerformanceObserver !== 'function'@@ -2607,7 +2723,7 @@return;}stop();- active = true; runToken = state.runToken; seq = 0; revision = 0; interventionRevision = 0; interventionDiagnostic = ''; publishRevision();+ active = true; runToken = state.runToken; seq = 0; revision = 0; interventionRevision = 0; uncertainRevision = 0; interventionDiagnostic = ''; publishRevision();relaxedMonitorGrant = authorizedGrant;observer = new MutationObserver(onMutations);observeRoot(document);@@ -2678,6 +2794,12 @@const op = operations.get(guard.operationId);op.target = el; op.scrollAncestors.clear();for (let node = el?.parentElement || el?.getRootNode?.().host; node; node = node.parentElement || node.getRootNode?.().host) op.scrollAncestors.add(node);+ // The caller scrolls synchronously after this event returns. Refresh+ // the owned geometry proof at the end of that same script task.+ if (selectorPreparationOnly) {+ const phase = op.ownedGeometryPhase = { kind: 'selector-scroll', scrollX: window.scrollX, scrollY: window.scrollY };+ queueMicrotask(() => refreshOwnedGeometry(op, phase));+ }} catch { event.preventDefault(); }}, false);const noteAgentTurn = event => {@@ -2686,6 +2808,12 @@if (!op) return;if (event.type === 'keydown') op.tabFocusRebindAllowed = event.key === 'Tab';if (['input', 'beforeinput', 'change'].includes(event.type)) rememberAgentLayout(op);+ // The agent's own certified pointer movement can apply :hover styles or+ // page hover handlers. After that input task, refresh only its geometry.+ if (op.pointerPreparationOnly === true && event.isTrusted === true && !op.ownedGeometryPhase) {+ const phase = op.ownedGeometryPhase = { kind: 'hover' };+ setTimeout(() => refreshOwnedGeometry(op, phase), 0);+ }const marker = { userAt: op.userAt };agentTurn = marker;const enterFormSubmit = event.type === 'keydown' && event.key === 'Enter' && op.kind === 'input'@@ -2782,10 +2910,12 @@const expectedInput = expected('input', el, event);const expectedClick = !expectedInput && el.matches?.('input[type="checkbox"],input[type="radio"],select,option')? expected('click', el, event) : null;- const localInput = !event.isTrusted && agentTurn && lastUserAt <= agentTurn.userAt- && localOperation?.started && operations.get(localOperation.operationId)?.target === el;+ // Untrusted events dispatched synchronously inside the agent's own turn+ // (its synthetic input, or a page handler re-dispatching for a hidden+ // native control) are part of that action rather than page input.+ const agentTurnInput = !event.isTrusted && agentTurn && lastUserAt <= agentTurn.userAt;if (ignored(el)) return;- if (expectedInput || expectedClick || localInput) { refreshControl(el); return; }+ if (expectedInput || expectedClick || agentTurnInput) { refreshControl(el); return; }if (event.isTrusted) interact('input', el);else { revision++; send({ kind: 'input', source: 'page', target: targetName(el) }); }refreshControl(el);@@ -2879,8 +3009,9 @@ownedPreparationBindings = new WeakMap();observer?.disconnect(); observer = null;layoutObserver?.disconnect(); layoutObserver = null;- clearTimeout(domTimer); clearTimeout(scrollTimer); clearInterval(controlTimer); domTimer = null; scrollTimer = null; controlTimer = null; unreported = 0; interventionUnreported = 0; interventionRevision = 0; pendingDOM = null; lastFeedbackDelivery = Promise.resolve();+ clearTimeout(domTimer); clearTimeout(scrollTimer); clearInterval(controlTimer); domTimer = null; scrollTimer = null; controlTimer = null; unreported = 0; interventionUnreported = 0; interventionRevision = 0; uncertainUnreported = 0; uncertainRevision = 0; pendingDOM = null; lastFeedbackDelivery = Promise.resolve();listeners.splice(0).forEach(remove => remove());+ removeShadowRootListeners();operations.clear(); modelSnapshots.clear(); localOperation = null; agentTurn = null;animationAttribution = new WeakMap();agentLayoutHistory.length = 0;@@ -2919,11 +3050,11 @@const certified = expected ? bindingResult.binding : inherited&& previous.runToken === runToken && previous.until >= Date.now()&& lastUserAt === previous.userAt- && (previous.preparedBinding.relaxed ? currentRelaxedGrant(previous.preparedBinding.grantToken)- : previous.preparedInterventionRevision === interventionRevision && !interventionUnreported && (!pendingDOM || pendingDOM.source === 'page'))+ && (previous.preparedBinding.relaxed ? currentRelaxedGrant(previous.preparedBinding.grantToken) : !authorityChanged(previous))&& !previous.modelBindingInvalid && !previous.layoutInvalidated&& operationTarget === previous.preparedBinding.target- && operationBindingMatches(previous, operationTarget) ? previous.preparedBinding : null;+ && (previous.preparedBinding.relaxed ? operationBindingMatches(previous, operationTarget)+ : !modelDecisionPredicate(previous.preparedBinding, operationBinding(previous, operationTarget))) ? previous.preparedBinding : null;const proofRequired = expected || inherited;const allowPassiveRebase = inherited ? previous.allowPassiveRebase === true : params.allowPassiveRebase === true;const controlStateAtPrepare = operationTarget && /^(INPUT|TEXTAREA|SELECT|OPTION)$/.test(operationTarget.tagName)@@ -2938,7 +3069,7 @@modelBindingReason: proofRequired && !certified ? bindingResult?.reason || 'semantic_binding_changed' : '',preparedTargetSignature: operationTarget ? signature(operationTarget) : undefined, kinds: new Set(),preparedBinding: proofRequired ? certified : allowPassiveRebase && operationTarget ? preparedBinding(operationTarget) : null,- preparedInterventionRevision: interventionRevision,+ preparedInterventionRevision: interventionRevision, preparedUncertainRevision: uncertainRevision, localSynthetic: false,preparedPageContext: JSON.stringify([location.href, document.baseURI, currentViewport(), window.scrollX, window.scrollY]),coordinateSensitive, coordinateTarget, coordinateHit, coordinateTargetAtPoint,coordinatePoint: coordinateSensitive ? { x: params.x, y: params.y } : null,@@ -2963,6 +3094,7 @@if (!params.release) op.eventTypes = Array.isArray(params.eventTypes) ? new Set(params.eventTypes) : null;op.focused = params.kind === 'input'; op.kind = params.kind;op.nativeWheel = params.nativeWheel === true;+ op.ownedGeometryPhase = null; // A later dispatch ends any owned geometry phase.// Only certified, unheld pointer movement is preparation. Once this same// operation has consequential input, later movement cannot downgrade it.const consequentialInputPending = op.consequentialInputPending === true@@ -3087,9 +3219,9 @@const approved = [...operations.values()].reverse().find(op => op.allowPassiveRebase === true && !op.dispatched&& op.preparedBinding?.target === target && op.until >= Date.now()&& lastUserAt <= op.userAt- && (op.preparedBinding.relaxed ? currentRelaxedGrant(op.preparedBinding.grantToken)- : op.preparedInterventionRevision === interventionRevision && !interventionUnreported && (!pendingDOM || pendingDOM.source === 'page'))- && operationBindingMatches(op, target));+ && (op.preparedBinding.relaxed ? currentRelaxedGrant(op.preparedBinding.grantToken) && operationBindingMatches(op, target)+ : !authorityChanged(op) && (op.preparedBinding.model === true+ ? !modelDecisionPredicate(op.preparedBinding, operationBinding(op, target)) : operationBindingMatches(op, target))));prepare({ selector: params.selector, ref_id: params.ref_id, x: params.x, y: params.y,textMatch: action === 'click' ? params.text : undefined, tool: bindingTool, operationId, runToken,...(params.expectedModelSnapshot !== undefined || approved?.expectedModelSnapshot !== undefined@@ -3097,26 +3229,33 @@...(approved?.preparedBinding?.relaxed ? { relaxedMonitorGrantToken: approved.preparedBinding.grantToken } : {}),...(bindingTool !== action ? { relaxedMonitorGrantToken: params.relaxedMonitorGrantToken } : {}),allowPassiveRebase: params.allowPassiveRebase === true || !!approved });- if (approved?.preparedBinding?.model === true) {- const local = operations.get(operationId);- if (local) {- local.preparedBinding = approved.preparedBinding;- local.modelBindingTool = approved.modelBindingTool || approved.tool;- local.expectedModelSnapshot = approved.expectedModelSnapshot;- local.focusBound = approved.focusBound;- local.modelBindingInvalid = false;- local.allowPassiveRebase = true;- }- }- localOperation = { operationId, kind: kindFor(action), userAt: lastUserAt, revision,+ const local = operations.get(operationId);+ // Content-script dispatch is synthetic. It can produce trusted focus,+ // scroll and execCommand input, never trusted keys or pointer presses.+ if (local) local.localSynthetic = true;+ if (local && approved?.preparedBinding?.model === true) {+ local.preparedBinding = approved.preparedBinding;+ local.modelBindingTool = approved.modelBindingTool || approved.tool;+ local.expectedModelSnapshot = approved.expectedModelSnapshot;+ local.focusBound = approved.focusBound;+ local.modelBindingInvalid = false;+ local.allowPassiveRebase = true;+ }+ const installed = localOperation = { operationId, kind: kindFor(action), userAt: lastUserAt, revision,navigationCandidate: ['click', 'click_ax', 'set_checked'].includes(action),- scrollIntoView: /^ax_resolve|ax_prepare_field/.test(action) };+ scrollIntoView: /^ax_resolve|ax_prepare_field/.test(action), previous };// Do not mark a BiDi preparation probe as actual input.if (!params._bidiPrepare) beforeLocalDispatch({ preparation: true });return () => {const op = operations.get(operationId);if (op) { finishRelaxedOwnedAction(op); op.until = Date.now() + 200; op.finished = true; }- localOperation = previous;+ installed.finished = true;+ // Restore only this action's own owner. A stop/start or a later action in+ // between keeps its current owner; a finished outer owner is not revived.+ if (localOperation !== installed) return;+ let next = installed.previous;+ while (next?.finished) next = next.previous;+ localOperation = next || null;};}function beforeLocalDispatch({ preparation = false, kind, target } = {}) {@@ -3135,6 +3274,13 @@error.pagePreparationVeto = preparationVetoFacts(operation, { layoutChanged, bindingChanged, target });throw error;}+ // The caller moves focus synchronously next. That move commits the field+ // that loses focus (a trusted 'change'); attribute only that commit here.+ if (preparation && kind === 'focus' && operation) {+ const blur = { target: deepActiveElement(), userAt: lastUserAt };+ operation.preparationBlur = blur;+ setTimeout(() => { if (operation.preparationBlur === blur) operation.preparationBlur = null; }, 0);+ }// A validation-only phase must retain the preceding actual preparation's// scroll attribution until its delayed browser events have arrived.if (preparation && operation?.dispatched && (operation.typingPreparationOnly || ['scroll', 'focus'].includes(operation.kind))) return;@@ -3209,7 +3355,7 @@function preparationVetoFacts(op, { layoutChanged = false, bindingChanged = false, target, documentChanged = false } = {}) {const predicate = !active ? 'run_ended' : !op ? 'operation_missing' : documentChanged ? 'document_changed': op.until < Date.now() ? 'operation_expired'- : lastUserAt > op.userAt || op.preparedInterventionRevision !== interventionRevision || interventionUnreported ? 'intervention_changed'+ : authorityChanged(op) ? 'intervention_changed': op.modelBindingInvalid ? op.modelBindingReason || 'model_snapshot_invalid': target && target !== op.target ? 'target_changed' : layoutChanged ? 'geometry_changed': bindingChanged ? op.focusBound && deepActiveElement() !== op.focusTarget ? 'focus_changed'@@ -3302,10 +3448,7 @@documentChanged: !!params.documentToken && params.documentToken !== documentToken,documentRevisionChanged: !!params.documentToken && !rebased && params.documentRevision !== revision,};- if (domTimer) {- clearTimeout(domTimer); domTimer = null; const observation = pendingDOM; pendingDOM = null;- if (observation) send(observation);- }+ if (domTimer) flushPendingDOM();void lastFeedbackDelivery.then(() => respond({ ready: false, pageFeedbackPending: true, preparationVeto }));return true;}@@ -3323,16 +3466,28 @@&& (!active || params.runToken !== runToken || params.documentToken !== documentToken || !op)) {respond({ ready: false }); return;}+ // Deliver this operation's own observed reactions before its tool returns:+ // pending observer records are processed while it is still unfinished,+ // the debounced DOM batch is sent now, and the reply awaits delivery.+ if (active && params.runToken === runToken) {+ flushPendingMutations(op); sampleFormControls(op?.target);+ if (domTimer) flushPendingDOM();+ }if (op) { finishRelaxedOwnedAction(op); op.until = Date.now() + 200; op.finished = true; }- respond({ ready: true });+ void lastFeedbackDelivery.then(() => respond({ ready: true }));+ return true;}};api.runtime.onMessage.addListener(onMessage);window.__wbPageMonitor = { beginContentAction, beforeLocalDispatch, withLocalPreparation, withLocalDispatch, captureLocalInputObservation, dispatch: params => dispatch(params), activatePreparedDispatch, withPreparedDispatch, registerDecoration, isReadonlyPreparationDecoration, currentIdentityPreparationCapture,get active() { return active; },get disposed() { return disposed; },- dispose() { disposed = true; requestGeneration++; stop(); api.runtime.onMessage.removeListener?.(onMessage); } };+ dispose() {+ disposed = true; requestGeneration++; stop(); api.runtime.onMessage.removeListener?.(onMessage);+ window.removeEventListener('pageshow', onPageShow);+ } };void requestState();// A document restored from BFCache needs a fresh run/document handshake.- window.addEventListener('pageshow', () => { if (!disposed && !active) void requestState(); });+ function onPageShow() { if (!disposed && !active) void requestState(); }+ window.addEventListener('pageshow', onPageShow);})();diff --git a/src/firefox/src/content/page-monitor.js b/src/firefox/src/content/page-monitor.jsindex e02b06da5..bf9f498f4 100644--- a/src/firefox/src/content/page-monitor.js+++ b/src/firefox/src/content/page-monitor.js@@ -36,6 +36,9 @@let pointerHeld = false, composing = false, localOperation = null, controlIterator = null;let preparedOperationCursor = 0;let requestGeneration = 0, unreported = 0, interventionUnreported = 0, pendingDOM = null;+ // Every non-passive observation (user, unknown, page focus/input/scroll,+ // frame resize). Legacy bindings without a private footprint keep this fence.+ let uncertainRevision = 0, uncertainUnreported = 0;let interventionDiagnostic = '';let lastViewport = '';let agentResize = null;@@ -141,11 +144,18 @@publishRevision();const feedback = { ...event, runToken, documentToken, seq: ++seq, revision };const pending = event.source !== 'agent';- const intervention = pending && (event.kind !== 'dom' || event.source !== 'page');+ // Only genuine human input and a top-level viewport change (window size,+ // zoom, browser chrome) invalidate model-bound decisions by themselves.+ // Automatic or uncertain observations still advance `revision`, so strict+ // fences remain; a certificate's private action footprint judges them.+ const intervention = pending && (event.source === 'user'+ || (event.kind === 'resize' && event.source === 'unknown' && window === window.top));+ const uncertain = pending && (event.kind !== 'dom' || event.source !== 'page');const owner = runToken;if (pending) unreported++;- if (intervention) {- interventionRevision++; interventionUnreported++;+ if (intervention) { interventionRevision++; interventionUnreported++; }+ if (uncertain) {+ uncertainRevision++; uncertainUnreported++;if (!(event.kind === 'dom' && event.source === 'unknown' && interventionDiagnostic.startsWith('dom.'))) {const kind = ['dom', 'activity', 'input', 'click', 'selection', 'scroll', 'navigation', 'resize'].includes(event.kind) ? event.kind : 'other';const source = ['user', 'page', 'unknown'].includes(event.source) ? event.source : 'other';@@ -154,9 +164,14 @@}try {const delivery = Promise.resolve(api.runtime.sendMessage({ target: 'background', action: 'page_feedback', feedback }))- .then(() => { if (owner === runToken) {+ .then(reply => { if (owner === runToken) {if (pending) unreported = Math.max(0, unreported - 1);if (intervention) interventionUnreported = Math.max(0, interventionUnreported - 1);+ if (uncertain) uncertainUnreported = Math.max(0, uncertainUnreported - 1);+ // The background no longer owns this run (for example after an MV3+ // worker restart). Stop observing instead of waking it for every+ // page update; a new run reactivates this document by handshake.+ if (reply?.accepted === false && reply.reason === 'stale-observation') stop();} }).catch(() => stop());if (pending) lastFeedbackDelivery = delivery;@@ -174,7 +189,7 @@if (!proof?.viewportNotificationPending || proof.scrollPreparationUntil < Date.now()|| proof.runToken !== runToken || proof.documentToken !== documentToken|| proof.userAt !== lastUserAt || proof.interventionRevision !== interventionRevision- || deepActiveElement() !== op.preparedBinding.target) continue;+ || (proof.focusRequired !== false && deepActiveElement() !== op.preparedBinding.target)) continue;const before = proof.binding, after = before.relaxed? relaxedTargetBinding(before.target, before.grantToken) : actionFootprint(before.target, before.readonlyPreparation);if (!after) continue;@@ -386,6 +401,10 @@send({ kind: 'dom', source, target: targetName(target || currentHit) });return true;}+ function physicalInputEvent(event) {+ return event?.isTrusted === true && (['keydown', 'keyup', 'keypress', 'pointerdown', 'pointerup', 'mousedown', 'mouseup'].includes(event.type)+ || (event.type === 'click' && event.detail > 0));+ }function expected(kind, el, event, nativeMarkerOnly = false) {let activatedNativeMarker = false;const markerNode = event?.composedPath?.().find(node => node instanceof Element && node.hasAttribute(nativeMarkerAttribute))@@ -427,6 +446,10 @@prune();const match = op => {if (event && !event.isTrusted && !op.synchronous) return null;+ // A content-script operation dispatches synthetic events only. A trusted+ // key or pointer press/release (or a click with a pointer detail) during+ // it is physical input; its execCommand/focus/scroll effects still match.+ if (op.localSynthetic === true && physicalInputEvent(event)) return null;if (event) {const keyboardClick = kind === 'click' && event.type === 'click' && event.isTrusted === true && op.keyboardClickTarget&& related(op.keyboardClickTarget, el);@@ -448,6 +471,12 @@if (kind === 'focus' && op.focusEligible && op.focusTarget === el && lastUserAt <= op.userAt) {const found = match(op); if (found) return found;}+ // Its own synchronous preparation focus commits the field losing focus.+ // The user's earlier edits there were reported by their own input events.+ if (kind === 'input' && event?.type === 'change' && op.preparationBlur?.target === el+ && lastUserAt <= op.preparationBlur.userAt) {+ const found = match(op); if (found) return found;+ }if (!op.dispatched) continue;if (lastUserAt > op.userAt) continue;// Keyboard-activated clicks can report detail=1 in Firefox. Do not let@@ -661,6 +690,49 @@for (const slot of [3, 4, 5]) current[slot] = original[slot];return samePreparedBinding(before, { ...after, hit: before.hit, context: JSON.stringify(current) });}+ // Share a derived geometry proof for this original certificate. Callers+ // have checked samePreparationSemantics; private semantic values stay fixed.+ function rebaseOwnedGeometry(binding, after, ownedWindowScroll = false) {+ const previous = ownedPreparationBindings.get(binding);+ const current = previous && preparedBindingFor(binding) === previous.binding ? previous : null;+ ownedPreparationBindings.set(binding, { binding: after, runToken, documentToken, userAt: lastUserAt, interventionRevision,+ // An owned window scroll may be followed by one late subpixel visual+ // viewport notification (as after typing preparation); a click target+ // need not hold focus for that bounded reconciliation.+ ...(ownedWindowScroll ? { scrollPreparationUntil: Date.now() + 200, viewportNotificationPending: true, focusRequired: false }+ : { scrollPreparationUntil: current?.scrollPreparationUntil || 0, viewportNotificationPending: current?.viewportNotificationPending === true,+ ...(current?.focusRequired === false ? { focusRequired: false } : {}) }) });+ }+ // Decision-level validation (model response, preparation). The model chose+ // this exact node by ref/selector, not by coordinates: when only scroll+ // offsets or the target rectangle moved and the hit is still the target,+ // the decision stands and the proof is rebased to the current geometry.+ // Exact geometry/hit checks then apply from preparation until dispatch.+ function modelDecisionPredicate(before, after) {+ const current = preparedBindingFor(before);+ const predicate = bindingPredicate(current, after);+ if (predicate !== 'geometry_changed') return predicate;+ // Another node receiving the hit (for example an interactive or named+ // descendant replacing a decorative icon) is not a pure move.+ if (current.hit !== after.hit) return 'target_hit_changed';+ rebaseOwnedGeometry(before, after);+ return '';+ }+ // An owned synchronous phase (selector scroll, pointer hover) may move the+ // certified node. Refresh its derived geometry proof once, at the end of+ // that phase, only for the same node with identical semantics and hit.+ // Any later geometry change is still checked exactly before dispatch.+ function refreshOwnedGeometry(op, phase) {+ if (!active || !phase || op?.ownedGeometryPhase !== phase || operations.get(op.operationId) !== op) return;+ op.ownedGeometryPhase = null;+ // Relaxed retry bindings carry no geometry; their grant rules apply instead.+ if (op.preparedBinding?.model !== true || op.preparedBinding.relaxed || op.allowPassiveRebase !== true+ || op.modelBindingInvalid || op.layoutInvalidated || authorityChanged(op)) return;+ const before = preparedBindingFor(op.preparedBinding);+ const after = actionFootprint(op.preparedBinding.target, op.preparedBinding.readonlyPreparation);+ if (samePreparationSemantics(before, after) && before.hit === after.hit) rebaseOwnedGeometry(op.preparedBinding, after,+ phase.kind === 'selector-scroll' && (phase.scrollX !== window.scrollX || phase.scrollY !== window.scrollY));+ }function bindingPredicate(before, after) {if (!before || !after) return 'semantic_binding_changed';if (before.target !== after.target || before.scope !== after.scope) return 'target_changed';@@ -1721,7 +1793,7 @@if (snapshot.relaxedGrantToken && !relaxed) return { reason: 'relaxed_grant_expired' };if (snapshot.userAt !== lastUserAt || (!relaxed && (snapshot.interventionRevision !== interventionRevision || interventionUnreported)))return { reason: 'intervention_changed' };- if (!relaxed && pendingDOM && pendingDOM.source !== 'page') return { reason: 'pending_feedback' };+ if (!relaxed && pendingDOM?.source === 'user') return { reason: 'pending_feedback' };const target = modelTarget(params);if (!target) {if (relaxed) { revokeRelaxedGrant(); return { reason: 'target_unresolved' }; }@@ -1749,7 +1821,7 @@}if (before?.readonlyPreparation && params.tool !== 'click_ax') return {};if (before) {- const reason = bindingPredicate(preparedBindingFor(before), actionFootprint(target, before.readonlyPreparation));+ const reason = modelDecisionPredicate(before, actionFootprint(target, before.readonlyPreparation));return reason ? { reason } : { binding: before };}const current = modelCandidateIdentity(target);@@ -2067,11 +2139,17 @@...(params.observationRef ? { observationScopeCaptured } : {}),...(page ? { page } : {}) };}+ // A certified operation whose only dispatch so far was its own preparation+ // (selector scroll, pointer hover, typing scroll/focus). Its footprint is+ // checked, and can be rebased over passive churn, at the next boundary.+ function ownedPreparationPhase(op) {+ return op?.pointerPreparationOnly === true || op?.typingPreparationOnly === true || op?.selectorPreparationOnly === true;+ }function preparedBindingChanged(op, { allowFocusRebind = false } = {}) {if (op?.allowPassiveRebase === true && op.preparedBinding?.model === true && op.focusBound&& !allowFocusRebind && deepActiveElement() !== op.focusTarget) return true;return op?.allowPassiveRebase === true && !!op.preparedBinding- && (!op.dispatched || (op.preparedBinding.model === true && (op.kind === 'input' || op.pointerPreparationOnly === true || op.typingPreparationOnly === true)))+ && (!op.dispatched || (op.preparedBinding.model === true && (op.kind === 'input' || ownedPreparationPhase(op))))&& !operationBindingMatches(op);}function nativeReceiverPredicate(op, params) {@@ -2100,14 +2178,21 @@? op.focusTarget : op.preparedBinding.target;return nativeReceiverBelongsTo(target, receiver) ? '' : 'target_changed';}+ // Model-bound operations are fenced by genuine intervention only; their+ // private footprint judges automatic page activity. Legacy bindings keep+ // the previous fence on every uncertain (non-passive) observation.+ function authorityChanged(op) {+ if (!op || lastUserAt > op.userAt) return true;+ return op.preparedBinding?.model === true+ ? op.preparedInterventionRevision !== interventionRevision || interventionUnreported > 0 || pendingDOM?.source === 'user'+ : op.preparedUncertainRevision !== uncertainRevision || uncertainUnreported > 0 || (!!pendingDOM && pendingDOM.source !== 'page');+ }function rebasePassivePreparation(op, params = {}) {const relaxed = op?.preparedBinding?.relaxed === true && currentRelaxedGrant(op.preparedBinding.grantToken);const continuingInput = op?.dispatched && op.kind === 'input' && op.preparedBinding?.model === true;- const continuingPreparation = op?.dispatched && (op.pointerPreparationOnly === true || op.typingPreparationOnly === true) && op.preparedBinding?.model === true;+ const continuingPreparation = op?.dispatched && ownedPreparationPhase(op) && op.preparedBinding?.model === true;if (!active || !op || op.allowPassiveRebase !== true || (op.dispatched && !continuingInput && !continuingPreparation) || op.coordinateSensitive || op.layoutInvalidated- || op.until < Date.now() || lastUserAt > op.userAt || (!relaxed && interventionUnreported)- || (!relaxed && op.preparedInterventionRevision !== interventionRevision)- || (!relaxed && pendingDOM && pendingDOM.source !== 'page')+ || op.until < Date.now() || lastUserAt > op.userAt || (!relaxed && authorityChanged(op))|| (params.documentToken && params.documentToken !== documentToken)) return false;const independentNavigation = op.tool === 'navigate';if (independentNavigation) {@@ -2125,11 +2210,7 @@}// Do not suppress feedback. Flush it for the agent while rebasing only this// exact, freshly revalidated operation over unrelated passive mutations.- if (domTimer) {- clearTimeout(domTimer); domTimer = null;- const observation = pendingDOM; pendingDOM = null;- if (observation) send(observation);- }+ if (domTimer) flushPendingDOM();op.preparedRevision = revision;return true;}@@ -2218,6 +2299,11 @@}return { nodes, exceeded: false };}+ const rootHandlers = [['animationend', checkSettledAnimation], ['animationcancel', checkSettledAnimation],+ ['transitionend', checkSettledAnimation], ['transitioncancel', checkSettledAnimation],+ ['beforetoggle', onBeforeToggle], ['toggle', onToggle]];+ const shadowListenerRoots = new Set();+ let shadowListenerSweepAt = 256;function observeRoot(root, budget) {if (!root || roots.has(root) || !observer) return;roots.add(root);@@ -2227,33 +2313,50 @@'aria-expanded', 'aria-selected', 'aria-checked', 'aria-pressed', 'aria-disabled','type', 'href', 'target', 'download', 'action', 'method', 'formaction', 'formmethod', 'formtarget', 'novalidate', 'formnovalidate','aria-valuenow', 'aria-valuetext', 'aria-hidden', 'hidden', 'disabled', 'readonly', 'checked', 'selected', 'open', 'popover', 'inert', 'class', 'style'] });- for (const name of ['animationend', 'animationcancel', 'transitionend', 'transitioncancel'])- listen(root, name, checkSettledAnimation);- listen(root, 'beforetoggle', event => {- const el = elementFor(event);- if (ignored(el) || !el.hasAttribute('popover') || !('popover' in el)) return;- const mark = { observer, oldState: event.oldState, newState: event.newState, notified: false,- agentUserAt: agentTurn && lastUserAt <= agentTurn.userAt ? agentTurn.userAt : undefined };- popoverTurns.set(el, mark);- // Check after page handlers can cancel opening, but before an action can- // dispatch in the next task. Retain exact attribution for the later toggle.- queueMicrotask(() => {- if (!active || observer !== mark.observer || popoverTurns.get(el) !== mark || event.defaultPrevented- || popoverOpen(el) !== (mark.newState === 'open')) return;- mark.notified = true;- onMutations([{ type: 'popover', target: el, stateChanged: mark.oldState !== mark.newState, agentUserAt: mark.agentUserAt }]);- });- });- listen(root, 'toggle', event => {- const el = elementFor(event);- if (ignored(el) || !el.hasAttribute('popover') || !('popover' in el)- || popoverOpen(el) !== (event.newState === 'open')) return;- const mark = popoverTurns.get(el);- if (mark?.observer === observer && mark.newState === event.newState && mark.notified) return;- onMutations([{ type: 'popover', target: el, stateChanged: event.oldState !== event.newState }]);- });+ // Animation and popover events are not composed, so each open shadow root+ // needs its own listeners. Track those roots weakly: a removed component+ // must stay collectable while the run continues.+ if (root === document) for (const [name, handler] of rootHandlers) listen(root, name, handler);+ else {+ for (const [name, handler] of rootHandlers) root.addEventListener(name, handler, { capture: true, passive: true });+ shadowListenerRoots.add(new WeakRef(root));+ if (shadowListenerRoots.size >= shadowListenerSweepAt) {+ for (const reference of shadowListenerRoots) if (!reference.deref()) shadowListenerRoots.delete(reference);+ shadowListenerSweepAt = Math.max(256, shadowListenerRoots.size * 2);+ }+ }seed(root, budget);}+ function removeShadowRootListeners() {+ for (const reference of shadowListenerRoots) {+ const root = reference.deref();+ if (root) for (const [name, handler] of rootHandlers) root.removeEventListener(name, handler, true);+ }+ shadowListenerRoots.clear(); shadowListenerSweepAt = 256;+ }+ function onBeforeToggle(event) {+ const el = elementFor(event);+ if (!active || ignored(el) || !el.hasAttribute('popover') || !('popover' in el)) return;+ const mark = { observer, oldState: event.oldState, newState: event.newState, notified: false,+ agentUserAt: agentTurn && lastUserAt <= agentTurn.userAt ? agentTurn.userAt : undefined };+ popoverTurns.set(el, mark);+ // Check after page handlers can cancel opening, but before an action can+ // dispatch in the next task. Retain exact attribution for the later toggle.+ queueMicrotask(() => {+ if (!active || observer !== mark.observer || popoverTurns.get(el) !== mark || event.defaultPrevented+ || popoverOpen(el) !== (mark.newState === 'open')) return;+ mark.notified = true;+ onMutations([{ type: 'popover', target: el, stateChanged: mark.oldState !== mark.newState, agentUserAt: mark.agentUserAt }]);+ });+ }+ function onToggle(event) {+ const el = elementFor(event);+ if (!active || ignored(el) || !el.hasAttribute('popover') || !('popover' in el)+ || popoverOpen(el) !== (event.newState === 'open')) return;+ const mark = popoverTurns.get(el);+ if (mark?.observer === observer && mark.newState === event.newState && mark.notified) return;+ onMutations([{ type: 'popover', target: el, stateChanged: event.oldState !== event.newState }]);+ }function popoverOpen(el) {return el.hasAttribute('popover') && 'popover' in el && el.matches(':popover-open');}@@ -2558,11 +2661,24 @@for (const op of operations.values())if (!op.dispatched && op.target && op.until >= Date.now()) op.preparedTargetSignature = signature(op.target);if (!changed) return;- if (source !== 'page' && source !== 'agent') interventionRevision++;+ // DOM mutations alone never establish human origin. A user-turn change is+ // an intervention; unknown attribution remains a strict legacy fence only.+ if (source === 'user') interventionRevision++;+ if (source !== 'page' && source !== 'agent') uncertainRevision++;revision++; publishRevision();clearTimeout(domTimer);- pendingDOM = { kind: 'dom', source, target };- domTimer = setTimeout(() => { domTimer = null; const observation = pendingDOM; pendingDOM = null; send(observation); }, 150);+ // Coalescing keeps the strongest attribution in this window. A later+ // passive batch must not relabel an earlier user or unknown change.+ if (!pendingDOM || domSeverity(source) >= domSeverity(pendingDOM.source)) pendingDOM = { kind: 'dom', source, target };+ domTimer = setTimeout(flushPendingDOM, 150);+ }+ function domSeverity(source) {+ return source === 'user' ? 3 : source === 'unknown' ? 2 : source === 'page' ? 1 : 0;+ }+ function flushPendingDOM() {+ clearTimeout(domTimer); domTimer = null;+ const observation = pendingDOM; pendingDOM = null;+ if (observation) send(observation);}function observeLayoutShifts() {if (typeof PerformanceObserver !== 'function'@@ -2607,7 +2723,7 @@return;}stop();- active = true; runToken = state.runToken; seq = 0; revision = 0; interventionRevision = 0; interventionDiagnostic = ''; publishRevision();+ active = true; runToken = state.runToken; seq = 0; revision = 0; interventionRevision = 0; uncertainRevision = 0; interventionDiagnostic = ''; publishRevision();relaxedMonitorGrant = authorizedGrant;observer = new MutationObserver(onMutations);observeRoot(document);@@ -2678,6 +2794,12 @@const op = operations.get(guard.operationId);op.target = el; op.scrollAncestors.clear();for (let node = el?.parentElement || el?.getRootNode?.().host; node; node = node.parentElement || node.getRootNode?.().host) op.scrollAncestors.add(node);+ // The caller scrolls synchronously after this event returns. Refresh+ // the owned geometry proof at the end of that same script task.+ if (selectorPreparationOnly) {+ const phase = op.ownedGeometryPhase = { kind: 'selector-scroll', scrollX: window.scrollX, scrollY: window.scrollY };+ queueMicrotask(() => refreshOwnedGeometry(op, phase));+ }} catch { event.preventDefault(); }}, false);const noteAgentTurn = event => {@@ -2686,6 +2808,12 @@if (!op) return;if (event.type === 'keydown') op.tabFocusRebindAllowed = event.key === 'Tab';if (['input', 'beforeinput', 'change'].includes(event.type)) rememberAgentLayout(op);+ // The agent's own certified pointer movement can apply :hover styles or+ // page hover handlers. After that input task, refresh only its geometry.+ if (op.pointerPreparationOnly === true && event.isTrusted === true && !op.ownedGeometryPhase) {+ const phase = op.ownedGeometryPhase = { kind: 'hover' };+ setTimeout(() => refreshOwnedGeometry(op, phase), 0);+ }const marker = { userAt: op.userAt };agentTurn = marker;const enterFormSubmit = event.type === 'keydown' && event.key === 'Enter' && op.kind === 'input'@@ -2782,10 +2910,12 @@const expectedInput = expected('input', el, event);const expectedClick = !expectedInput && el.matches?.('input[type="checkbox"],input[type="radio"],select,option')? expected('click', el, event) : null;- const localInput = !event.isTrusted && agentTurn && lastUserAt <= agentTurn.userAt- && localOperation?.started && operations.get(localOperation.operationId)?.target === el;+ // Untrusted events dispatched synchronously inside the agent's own turn+ // (its synthetic input, or a page handler re-dispatching for a hidden+ // native control) are part of that action rather than page input.+ const agentTurnInput = !event.isTrusted && agentTurn && lastUserAt <= agentTurn.userAt;if (ignored(el)) return;- if (expectedInput || expectedClick || localInput) { refreshControl(el); return; }+ if (expectedInput || expectedClick || agentTurnInput) { refreshControl(el); return; }if (event.isTrusted) interact('input', el);else { revision++; send({ kind: 'input', source: 'page', target: targetName(el) }); }refreshControl(el);@@ -2879,8 +3009,9 @@ownedPreparationBindings = new WeakMap();observer?.disconnect(); observer = null;layoutObserver?.disconnect(); layoutObserver = null;- clearTimeout(domTimer); clearTimeout(scrollTimer); clearInterval(controlTimer); domTimer = null; scrollTimer = null; controlTimer = null; unreported = 0; interventionUnreported = 0; interventionRevision = 0; pendingDOM = null; lastFeedbackDelivery = Promise.resolve();+ clearTimeout(domTimer); clearTimeout(scrollTimer); clearInterval(controlTimer); domTimer = null; scrollTimer = null; controlTimer = null; unreported = 0; interventionUnreported = 0; interventionRevision = 0; uncertainUnreported = 0; uncertainRevision = 0; pendingDOM = null; lastFeedbackDelivery = Promise.resolve();listeners.splice(0).forEach(remove => remove());+ removeShadowRootListeners();operations.clear(); modelSnapshots.clear(); localOperation = null; agentTurn = null;animationAttribution = new WeakMap();agentLayoutHistory.length = 0;@@ -2919,11 +3050,11 @@const certified = expected ? bindingResult.binding : inherited&& previous.runToken === runToken && previous.until >= Date.now()&& lastUserAt === previous.userAt- && (previous.preparedBinding.relaxed ? currentRelaxedGrant(previous.preparedBinding.grantToken)- : previous.preparedInterventionRevision === interventionRevision && !interventionUnreported && (!pendingDOM || pendingDOM.source === 'page'))+ && (previous.preparedBinding.relaxed ? currentRelaxedGrant(previous.preparedBinding.grantToken) : !authorityChanged(previous))&& !previous.modelBindingInvalid && !previous.layoutInvalidated&& operationTarget === previous.preparedBinding.target- && operationBindingMatches(previous, operationTarget) ? previous.preparedBinding : null;+ && (previous.preparedBinding.relaxed ? operationBindingMatches(previous, operationTarget)+ : !modelDecisionPredicate(previous.preparedBinding, operationBinding(previous, operationTarget))) ? previous.preparedBinding : null;const proofRequired = expected || inherited;const allowPassiveRebase = inherited ? previous.allowPassiveRebase === true : params.allowPassiveRebase === true;const controlStateAtPrepare = operationTarget && /^(INPUT|TEXTAREA|SELECT|OPTION)$/.test(operationTarget.tagName)@@ -2938,7 +3069,7 @@modelBindingReason: proofRequired && !certified ? bindingResult?.reason || 'semantic_binding_changed' : '',preparedTargetSignature: operationTarget ? signature(operationTarget) : undefined, kinds: new Set(),preparedBinding: proofRequired ? certified : allowPassiveRebase && operationTarget ? preparedBinding(operationTarget) : null,- preparedInterventionRevision: interventionRevision,+ preparedInterventionRevision: interventionRevision, preparedUncertainRevision: uncertainRevision, localSynthetic: false,preparedPageContext: JSON.stringify([location.href, document.baseURI, currentViewport(), window.scrollX, window.scrollY]),coordinateSensitive, coordinateTarget, coordinateHit, coordinateTargetAtPoint,coordinatePoint: coordinateSensitive ? { x: params.x, y: params.y } : null,@@ -2963,6 +3094,7 @@if (!params.release) op.eventTypes = Array.isArray(params.eventTypes) ? new Set(params.eventTypes) : null;op.focused = params.kind === 'input'; op.kind = params.kind;op.nativeWheel = params.nativeWheel === true;+ op.ownedGeometryPhase = null; // A later dispatch ends any owned geometry phase.// Only certified, unheld pointer movement is preparation. Once this same// operation has consequential input, later movement cannot downgrade it.const consequentialInputPending = op.consequentialInputPending === true@@ -3087,9 +3219,9 @@const approved = [...operations.values()].reverse().find(op => op.allowPassiveRebase === true && !op.dispatched&& op.preparedBinding?.target === target && op.until >= Date.now()&& lastUserAt <= op.userAt- && (op.preparedBinding.relaxed ? currentRelaxedGrant(op.preparedBinding.grantToken)- : op.preparedInterventionRevision === interventionRevision && !interventionUnreported && (!pendingDOM || pendingDOM.source === 'page'))- && operationBindingMatches(op, target));+ && (op.preparedBinding.relaxed ? currentRelaxedGrant(op.preparedBinding.grantToken) && operationBindingMatches(op, target)+ : !authorityChanged(op) && (op.preparedBinding.model === true+ ? !modelDecisionPredicate(op.preparedBinding, operationBinding(op, target)) : operationBindingMatches(op, target))));prepare({ selector: params.selector, ref_id: params.ref_id, x: params.x, y: params.y,textMatch: action === 'click' ? params.text : undefined, tool: bindingTool, operationId, runToken,...(params.expectedModelSnapshot !== undefined || approved?.expectedModelSnapshot !== undefined@@ -3097,26 +3229,33 @@...(approved?.preparedBinding?.relaxed ? { relaxedMonitorGrantToken: approved.preparedBinding.grantToken } : {}),...(bindingTool !== action ? { relaxedMonitorGrantToken: params.relaxedMonitorGrantToken } : {}),allowPassiveRebase: params.allowPassiveRebase === true || !!approved });- if (approved?.preparedBinding?.model === true) {- const local = operations.get(operationId);- if (local) {- local.preparedBinding = approved.preparedBinding;- local.modelBindingTool = approved.modelBindingTool || approved.tool;- local.expectedModelSnapshot = approved.expectedModelSnapshot;- local.focusBound = approved.focusBound;- local.modelBindingInvalid = false;- local.allowPassiveRebase = true;- }- }- localOperation = { operationId, kind: kindFor(action), userAt: lastUserAt, revision,+ const local = operations.get(operationId);+ // Content-script dispatch is synthetic. It can produce trusted focus,+ // scroll and execCommand input, never trusted keys or pointer presses.+ if (local) local.localSynthetic = true;+ if (local && approved?.preparedBinding?.model === true) {+ local.preparedBinding = approved.preparedBinding;+ local.modelBindingTool = approved.modelBindingTool || approved.tool;+ local.expectedModelSnapshot = approved.expectedModelSnapshot;+ local.focusBound = approved.focusBound;+ local.modelBindingInvalid = false;+ local.allowPassiveRebase = true;+ }+ const installed = localOperation = { operationId, kind: kindFor(action), userAt: lastUserAt, revision,navigationCandidate: ['click', 'click_ax', 'set_checked'].includes(action),- scrollIntoView: /^ax_resolve|ax_prepare_field/.test(action) };+ scrollIntoView: /^ax_resolve|ax_prepare_field/.test(action), previous };// Do not mark a BiDi preparation probe as actual input.if (!params._bidiPrepare) beforeLocalDispatch({ preparation: true });return () => {const op = operations.get(operationId);if (op) { finishRelaxedOwnedAction(op); op.until = Date.now() + 200; op.finished = true; }- localOperation = previous;+ installed.finished = true;+ // Restore only this action's own owner. A stop/start or a later action in+ // between keeps its current owner; a finished outer owner is not revived.+ if (localOperation !== installed) return;+ let next = installed.previous;+ while (next?.finished) next = next.previous;+ localOperation = next || null;};}function beforeLocalDispatch({ preparation = false, kind, target } = {}) {@@ -3135,6 +3274,13 @@error.pagePreparationVeto = preparationVetoFacts(operation, { layoutChanged, bindingChanged, target });throw error;}+ // The caller moves focus synchronously next. That move commits the field+ // that loses focus (a trusted 'change'); attribute only that commit here.+ if (preparation && kind === 'focus' && operation) {+ const blur = { target: deepActiveElement(), userAt: lastUserAt };+ operation.preparationBlur = blur;+ setTimeout(() => { if (operation.preparationBlur === blur) operation.preparationBlur = null; }, 0);+ }// A validation-only phase must retain the preceding actual preparation's// scroll attribution until its delayed browser events have arrived.if (preparation && operation?.dispatched && (operation.typingPreparationOnly || ['scroll', 'focus'].includes(operation.kind))) return;@@ -3209,7 +3355,7 @@function preparationVetoFacts(op, { layoutChanged = false, bindingChanged = false, target, documentChanged = false } = {}) {const predicate = !active ? 'run_ended' : !op ? 'operation_missing' : documentChanged ? 'document_changed': op.until < Date.now() ? 'operation_expired'- : lastUserAt > op.userAt || op.preparedInterventionRevision !== interventionRevision || interventionUnreported ? 'intervention_changed'+ : authorityChanged(op) ? 'intervention_changed': op.modelBindingInvalid ? op.modelBindingReason || 'model_snapshot_invalid': target && target !== op.target ? 'target_changed' : layoutChanged ? 'geometry_changed': bindingChanged ? op.focusBound && deepActiveElement() !== op.focusTarget ? 'focus_changed'@@ -3302,10 +3448,7 @@documentChanged: !!params.documentToken && params.documentToken !== documentToken,documentRevisionChanged: !!params.documentToken && !rebased && params.documentRevision !== revision,};- if (domTimer) {- clearTimeout(domTimer); domTimer = null; const observation = pendingDOM; pendingDOM = null;- if (observation) send(observation);- }+ if (domTimer) flushPendingDOM();void lastFeedbackDelivery.then(() => respond({ ready: false, pageFeedbackPending: true, preparationVeto }));return true;}@@ -3323,16 +3466,28 @@&& (!active || params.runToken !== runToken || params.documentToken !== documentToken || !op)) {respond({ ready: false }); return;}+ // Deliver this operation's own observed reactions before its tool returns:+ // pending observer records are processed while it is still unfinished,+ // the debounced DOM batch is sent now, and the reply awaits delivery.+ if (active && params.runToken === runToken) {+ flushPendingMutations(op); sampleFormControls(op?.target);+ if (domTimer) flushPendingDOM();+ }if (op) { finishRelaxedOwnedAction(op); op.until = Date.now() + 200; op.finished = true; }- respond({ ready: true });+ void lastFeedbackDelivery.then(() => respond({ ready: true }));+ return true;}};api.runtime.onMessage.addListener(onMessage);window.__wbPageMonitor = { beginContentAction, beforeLocalDispatch, withLocalPreparation, withLocalDispatch, captureLocalInputObservation, dispatch: params => dispatch(params), activatePreparedDispatch, withPreparedDispatch, registerDecoration, isReadonlyPreparationDecoration, currentIdentityPreparationCapture,get active() { return active; },get disposed() { return disposed; },- dispose() { disposed = true; requestGeneration++; stop(); api.runtime.onMessage.removeListener?.(onMessage); } };+ dispose() {+ disposed = true; requestGeneration++; stop(); api.runtime.onMessage.removeListener?.(onMessage);+ window.removeEventListener('pageshow', onPageShow);+ } };void requestState();// A document restored from BFCache needs a fresh run/document handshake.- window.addEventListener('pageshow', () => { if (!disposed && !active) void requestState(); });+ function onPageShow() { if (!disposed && !active) void requestState(); }+ window.addEventListener('pageshow', onPageShow);})();diff --git a/test/monitor-intervention-contract.mjs b/test/monitor-intervention-contract.mjsnew file mode 100644index 000000000..ba16f2c3b--- /dev/null+++ b/test/monitor-intervention-contract.mjs@@ -0,0 +1,349 @@+// Content half of the page-feedback event contract: only genuine user input+// and top-level viewport changes are interventions. Automatic observations+// advance the revision (strict fences) while a model certificate's private+// footprint decides whether its action changed. Real page-monitor.js in+// Chromium and Firefox; extension messaging is mocked.+import assert from 'node:assert/strict';+import fs from 'node:fs';+import { test } from 'node:test';+import { chromium, firefox } from 'playwright';+import { createNativeActionMarker } from '../firefox-companion/session.mjs';++const RUN = 'contract-run';+const html = `<!doctype html><style>+body { margin: 20px; font: 16px sans-serif; } button, input { margin: 6px; }+.row span { display: inline-block; width: 6px; }+</style><main><h1>Messages</h1>+<section id="composer" role="region"><h2 id="recipient">Alice</h2>+<form id="form" action="/send" method="post"><label for="field">Message</label>+<input id="field" autocomplete="off"><input id="to" type="hidden" name="to" value="alice">+<button id="send" type="submit">Send</button><ul id="suggest"></ul></form></section>+<button id="toggle" type="button" role="switch" aria-checked="false">Notifications</button>+<input id="hidden-switch" type="checkbox" hidden>+<p id="status">Ready</p></main>+<aside id="side"><span id="counter">0</span><input id="search" aria-label="Search"><div id="rows"></div></aside>+<button id="human" type="button">Human control</button><div style="height:1200px"></div>+<script>+window.submissions = 0;+document.getElementById('form').addEventListener('submit', event => { event.preventDefault(); submissions++; });+document.getElementById('toggle').addEventListener('click', () => {+ // A switch backed by a hidden native control re-dispatches a synthetic change.+ const box = document.getElementById('hidden-switch');+ box.checked = !box.checked;+ document.getElementById('toggle').setAttribute('aria-checked', String(box.checked));+ box.dispatchEvent(new Event('change', { bubbles: true }));+});+document.getElementById('rows').innerHTML = Array.from({ length: 45 }, (_, i) =>+ '<div class="row">' + '<span>x</span>'.repeat(8) + '</div>').join('');+</script>`;++async function fixture(browser, build, { feedbackDelay = 0 } = {}) {+ const context = await browser.newContext({ viewport: { width: 1000, height: 700 } });+ await context.route('https://contract.test/**', route => route.fulfill({ contentType: 'text/html', body: html }));+ await context.addInitScript(({ RUN, feedbackDelay }) => {+ window.feedback = []; window.delivered = []; window.messageListeners = [];+ const runtime = {+ onMessage: { addListener: fn => messageListeners.push(fn), removeListener: fn => { messageListeners = messageListeners.filter(item => item !== fn); } },+ async sendMessage(msg) {+ if (msg.action === 'get_page_monitor_state') return { active: true, runToken: RUN, documentToken: msg.documentToken };+ if (msg.action === 'page_feedback') {+ feedback.push(msg.feedback);+ if (feedbackDelay) await new Promise(resolve => setTimeout(resolve, feedbackDelay));+ delivered.push(msg.feedback);+ return { accepted: true };+ }+ return {};+ },+ };+ window.chrome = { runtime }; window.browser = window.chrome;+ window.__wb_ax_lookup = ref => document.getElementById(ref.replace(/^ref_/, ''));+ window.deliver = (action, params = {}) => new Promise(resolve => {+ let answered = false;+ const respond = value => { answered = true; resolve(value); };+ for (const listener of messageListeners) {+ const asynchronous = listener({ target: 'content', action, params: { runToken: RUN, ...params } }, {}, respond);+ if (answered || asynchronous === true) return;+ }+ resolve(undefined);+ });+ }, { RUN, feedbackDelay });+ await context.addInitScript({ content: fs.readFileSync(new URL(`../src/${build}/src/content/page-monitor.js`, import.meta.url), 'utf8') });+ const page = await context.newPage();+ await page.goto('https://contract.test/start');+ await page.waitForFunction(() => window.__wbPageMonitor?.active);+ await page.evaluate(() => document.getElementById('field').focus());+ await page.waitForTimeout(250);+ await page.evaluate(() => { feedback = []; delivered = []; });+ return { context, page };+}++const message = (page, action, params = {}) => page.evaluate(({ action, params }) => Promise.race([deliver(action, params),+ new Promise(resolve => setTimeout(() => resolve({ ready: false, code: 'monitor_response_missing' }), 3000))]), { action, params });+const settle = page => page.waitForTimeout(260); // Debounced DOM feedback (150 ms) has been delivered.+const sources = async page => (await page.evaluate(() => feedback)).map(event => `${event.kind}/${event.source}`);++async function capture(page) {+ const snapshot = await message(page, 'page_monitor_capture_model');+ assert.equal(snapshot.ready, true, JSON.stringify(snapshot));+ return snapshot;+}++for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {+ test(`${build}: automatic page activity during certified multi-key input is judged by the footprint`, { timeout: 120000 }, async t => {+ const browser = await engine.launch({ headless: true });+ try {+ // r13: a typeahead renders after key 1 while the operation is dispatched.+ const typeKeys = async (page, between) => {+ const snapshot = await capture(page);+ const prepared = await message(page, 'page_monitor_prepare', { operationId: 'keys', tool: 'press_keys',+ allowPassiveRebase: true, expectedModelSnapshot: snapshot.snapshotToken });+ assert.equal(prepared.modelBindingValid, true);+ const typed = [];+ for (const key of ['I', 's', 't']) {+ const down = await message(page, 'page_monitor_dispatch', { operationId: 'keys', kind: 'input',+ eventTypes: ['keydown', 'keypress', 'beforeinput', 'input'] });+ if (!down.ready) return { typed: typed.join(''), veto: down.preparationVeto?.predicate || 'vetoed' };+ await page.keyboard.down(key);+ await message(page, 'page_monitor_dispatch', { operationId: 'keys', kind: 'input', release: true, eventTypes: ['keyup'] });+ await page.keyboard.up(key);+ typed.push(key);+ if (typed.length === 1) await between(page);+ }+ await message(page, 'page_monitor_finish', { operationId: 'keys' });+ return { typed: typed.join(''), veto: '' };+ };++ await t.test('an unrelated typeahead render does not veto the next key and stays observable', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ const result = await typeKeys(page, page => page.evaluate(() => {+ document.getElementById('suggest').innerHTML = '<li>Istanbul</li><li>Izmir</li>';+ }));+ assert.deepEqual(result, { typed: 'Ist', veto: '' });+ assert.equal(await page.inputValue('#field'), 'Ist');+ await settle(page);+ assert.ok((await sources(page)).includes('dom/unknown'), 'The uncertain render is still reported to the agent');+ } finally { await context.close(); }+ });++ for (const [name, between] of [+ ['a changed recipient', page => page.evaluate(() => { document.getElementById('recipient').textContent = 'Bob'; })],+ ['a changed hidden destination value', page => page.evaluate(() => { document.getElementById('to').value = 'bob'; })],+ ['a genuine user keypress in the same field', page => page.keyboard.press('X')],+ ['a genuine user click on another control', page => page.click('#human')],+ ]) await t.test(`${name} still vetoes the next key`, async () => {+ const { context, page } = await fixture(browser, build);+ try {+ const result = await typeKeys(page, between);+ assert.equal(result.typed, 'I');+ assert.ok(result.veto);+ } finally { await context.close(); }+ });++ await t.test('per-key native validation survives a counter render after every key (r14)', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ const snapshot = await capture(page);+ const prepared = await message(page, 'page_monitor_prepare', { operationId: 'native', tool: 'type_ax', ref_id: 'ref_field',+ allowPassiveRebase: true, expectedModelSnapshot: snapshot.snapshotToken });+ assert.equal(prepared.modelBindingValid, true);+ const fence = await message(page, 'page_monitor_dispatch', { operationId: 'native', tool: 'type_ax', ref_id: 'ref_field', kind: 'input', fenceOnly: true });+ let guard = fence.guard, sequence = 0, typed = '';+ for (const key of 'Hey') {+ const validation = await message(page, 'page_monitor_validate', { ...guard, kind: 'input', rebindFocus: false });+ if (!validation.ready) break;+ guard = { ...guard, revision: validation.revision };+ const marker = createNativeActionMarker(guard, 'input', ++sequence);+ await page.evaluate(marker => document.getElementById('field').setAttribute('data-webbrain-native-action', marker), marker);+ await page.keyboard.press(key);+ typed += key;+ // The page renders its character counter in a later task.+ await page.evaluate(() => { document.getElementById('counter').textContent = String(document.getElementById('field').value.length); });+ }+ await message(page, 'page_monitor_finish', { operationId: 'native' });+ assert.equal(typed, 'Hey');+ assert.equal(await page.inputValue('#field'), 'Hey');+ assert.equal((await page.evaluate(() => feedback)).some(event => event.source === 'user'), false,+ 'Signed native keys remain attributed to the agent');+ } finally { await context.close(); }+ });++ await t.test('coordinate operations keep the strict revision fence', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ const box = await page.locator('#human').boundingBox();+ await message(page, 'page_monitor_prepare', { operationId: 'coordinates', tool: 'click', allowPassiveRebase: true,+ x: box.x + box.width / 2, y: box.y + box.height / 2 });+ await page.evaluate(() => { document.getElementById('counter').textContent = '7'; });+ const result = await message(page, 'page_monitor_dispatch', { operationId: 'coordinates', kind: 'click' });+ assert.equal(result.ready, false);+ assert.equal(result.pageFeedbackPending, true);+ } finally { await context.close(); }+ });+ } finally { await browser.close(); }+ });++ test(`${build}: automatic observations do not invalidate a model snapshot by themselves`, { timeout: 120000 }, async t => {+ const browser = await engine.launch({ headless: true });+ try {+ const automatic = [+ ['page focus elsewhere', page => page.evaluate(() => document.getElementById('search').focus()), 'activity/page'],+ ['an untrusted page input event', page => page.evaluate(() => {+ document.getElementById('search').dispatchEvent(new Event('input', { bubbles: true }));+ }), 'input/page'],+ ['a page-script scroll', page => page.evaluate(() => window.scrollTo(0, 30)), 'scroll/unknown'],+ ['a measurement-capped page render', page => page.evaluate(() => {+ for (const row of document.querySelectorAll('.row')) row.classList.add('selected-theme');+ }), 'dom/unknown'],+ ['unknown DOM attribution during another operation', page => page.evaluate(() => {+ __wbPageMonitor.dispatch({ operationId: 'other-operation', kind: 'dom', runToken: 'contract-run' });+ document.getElementById('counter').textContent = '9';+ }), 'dom/unknown'],+ ];+ for (const [name, mutate, observed] of automatic) await t.test(`${name} leaves the certified decision to its footprint`, async () => {+ const { context, page } = await fixture(browser, build);+ try {+ const snapshot = await capture(page);+ await mutate(page);+ await settle(page);+ assert.ok((await sources(page)).includes(observed), `${observed} remains observable: ${await sources(page)}`);+ const validated = await message(page, 'page_monitor_validate_model', { snapshotToken: snapshot.snapshotToken, tool: 'click_ax', ref_id: 'ref_send' });+ assert.equal(validated.ready, true, JSON.stringify(validated));+ const prepared = await message(page, 'page_monitor_prepare', { operationId: 'model', tool: 'click_ax', ref_id: 'ref_send',+ allowPassiveRebase: true, expectedModelSnapshot: snapshot.snapshotToken });+ assert.equal(prepared.modelBindingValid, true);+ const dispatched = await message(page, 'page_monitor_dispatch', { operationId: 'model', kind: 'click' });+ assert.equal(dispatched.ready, true);+ await page.evaluate(() => __wbPageMonitor.withPreparedDispatch('model', () => document.getElementById('send').click()));+ assert.equal(await page.evaluate(() => submissions), 1);+ } finally { await context.close(); }+ });++ for (const [name, mutate] of [+ ['a genuine user click elsewhere', page => page.click('#human')],+ ['a top-level viewport resize', page => page.setViewportSize({ width: 990, height: 690 })],+ ['a changed recipient', page => page.evaluate(() => { document.getElementById('recipient').textContent = 'Bob'; })],+ ]) await t.test(`${name} still invalidates the snapshot`, async () => {+ const { context, page } = await fixture(browser, build);+ try {+ const snapshot = await capture(page);+ await mutate(page);+ await settle(page);+ const validated = await message(page, 'page_monitor_validate_model', { snapshotToken: snapshot.snapshotToken, tool: 'click_ax', ref_id: 'ref_send' });+ assert.equal(validated.ready, false);+ const prepared = await message(page, 'page_monitor_prepare', { operationId: 'model', tool: 'click_ax', ref_id: 'ref_send',+ allowPassiveRebase: true, expectedModelSnapshot: snapshot.snapshotToken });+ assert.equal(prepared.modelBindingValid, false);+ assert.equal(await page.evaluate(() => submissions), 0);+ } finally { await context.close(); }+ });++ await t.test('page focus still invalidates a focus-bound decision', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ const snapshot = await capture(page);+ await page.evaluate(() => document.getElementById('search').focus());+ await settle(page);+ const validated = await message(page, 'page_monitor_validate_model', { snapshotToken: snapshot.snapshotToken, tool: 'press_keys' });+ assert.equal(validated.ready, false);+ assert.equal(validated.reason, 'focus_changed');+ } finally { await context.close(); }+ });++ await t.test('a page focus after the agent click is reported without becoming an intervention (X3)', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ const snapshot = await capture(page);+ const box = await page.locator('#toggle').boundingBox();+ const x = box.x + box.width / 2, y = box.y + box.height / 2;+ await message(page, 'page_monitor_prepare', { operationId: 'click', tool: 'click', selector: '#toggle' });+ await message(page, 'page_monitor_dispatch', { operationId: 'click', kind: 'click', selector: '#toggle', x, y,+ eventTypes: ['pointerover', 'pointerenter', 'mouseover', 'mouseenter', 'pointerdown', 'mousedown', 'pointerup', 'mouseup', 'click', 'input', 'change'] });+ await page.evaluate(() => document.getElementById('toggle').addEventListener('click',+ () => setTimeout(() => document.getElementById('search').focus(), 30), { once: true }));+ await page.mouse.click(x, y);+ await message(page, 'page_monitor_finish', { operationId: 'click' });+ await page.waitForFunction(() => feedback.some(event => event.kind === 'activity' && event.source === 'page'));+ await settle(page);+ const validated = await message(page, 'page_monitor_validate_model', { snapshotToken: snapshot.snapshotToken, tool: 'click_ax', ref_id: 'ref_send' });+ assert.equal(validated.ready, true, JSON.stringify(validated));+ assert.equal((await page.evaluate(() => feedback)).some(event => event.source === 'user'), false);+ } finally { await context.close(); }+ });+ } finally { await browser.close(); }+ });++ test(`${build}: untrusted events inside the agent's own turn belong to that turn (X7)`, { timeout: 60000 }, async t => {+ const browser = await engine.launch({ headless: true });+ try {+ await t.test('a page handler re-dispatching change for a hidden control is not page input', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ const box = await page.locator('#toggle').boundingBox();+ const x = box.x + box.width / 2, y = box.y + box.height / 2;+ await message(page, 'page_monitor_prepare', { operationId: 'toggle', tool: 'click', selector: '#toggle' });+ await message(page, 'page_monitor_dispatch', { operationId: 'toggle', kind: 'click', selector: '#toggle', x, y,+ eventTypes: ['pointerover', 'pointerenter', 'mouseover', 'mouseenter', 'pointerdown', 'mousedown', 'pointerup', 'mouseup', 'click', 'input', 'change'] });+ await page.mouse.click(x, y);+ await message(page, 'page_monitor_finish', { operationId: 'toggle' });+ await settle(page);+ assert.equal(await page.evaluate(() => document.getElementById('hidden-switch').checked), true);+ const events = await sources(page);+ assert.equal(events.includes('input/page'), false, `The synthetic change is part of the agent click: ${events}`);+ assert.equal(events.some(event => event.endsWith('/user')), false);+ } finally { await context.close(); }+ });++ await t.test('the same synthetic event outside the agent turn remains observable page input', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ await page.evaluate(() => document.getElementById('hidden-switch').dispatchEvent(new Event('change', { bubbles: true })));+ await settle(page);+ assert.ok((await sources(page)).includes('input/page'));+ } finally { await context.close(); }+ });+ } finally { await browser.close(); }+ });++ test(`${build}: finishing an operation delivers its own pending reactions first`, { timeout: 60000 }, async t => {+ const browser = await engine.launch({ headless: true });+ try {+ await t.test('a debounced reaction is flushed and delivered before finish replies (D2)', async () => {+ const { context, page } = await fixture(browser, build, { feedbackDelay: 40 });+ try {+ await message(page, 'page_monitor_prepare', { operationId: 'typing', tool: 'type_text', selector: '#field' });+ await message(page, 'page_monitor_dispatch', { operationId: 'typing', kind: 'input', selector: '#field', eventTypes: ['beforeinput', 'input', 'change'] });+ await page.keyboard.insertText('hello');+ const atFinish = await page.evaluate(async () => {+ // The page renders its reaction in a later task, inside the operation+ // window; the tool finishes before the 150 ms debounce elapses.+ document.getElementById('status').textContent = 'chars 5';+ await new Promise(resolve => setTimeout(resolve, 0));+ const response = await deliver('page_monitor_finish', { operationId: 'typing' });+ return { response, delivered: delivered.map(event => `${event.kind}/${event.source}`) };+ });+ assert.equal(atFinish.response?.ready, true);+ assert.ok(atFinish.delivered.includes('dom/unknown'), `The reaction reached the background before finish replied: ${atFinish.delivered}`);+ } finally { await context.close(); }+ });++ await t.test('coalesced feedback keeps its strongest attribution', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ await page.evaluate(async () => {+ const finish = __wbPageMonitor.beginContentAction('click', { selector: '#human' });+ __wbPageMonitor.beforeLocalDispatch();+ await new Promise(resolve => setTimeout(resolve, 0));+ document.getElementById('counter').textContent = 'during the operation';+ await new Promise(resolve => setTimeout(resolve, 0));+ finish();+ document.getElementById('status').textContent = 'after the operation';+ });+ await settle(page);+ const dom = (await page.evaluate(() => feedback)).filter(event => event.kind === 'dom').map(event => event.source);+ assert.ok(dom.includes('unknown'), `A later passive batch must not relabel the uncertain change: ${dom}`);+ } finally { await context.close(); }+ });+ } finally { await browser.close(); }+ });+}diff --git a/test/monitor-lifecycle-leaks.mjs b/test/monitor-lifecycle-leaks.mjsnew file mode 100644index 000000000..8a83ae34e--- /dev/null+++ b/test/monitor-lifecycle-leaks.mjs@@ -0,0 +1,179 @@+// Run-scoped monitor lifecycle: an orphaned monitor stops when the background+// no longer owns its run, removed shadow roots stay collectable during a run,+// disposal removes the BFCache listener, and a content action cannot restore+// another run's local operation.+import assert from 'node:assert/strict';+import fs from 'node:fs';+import { test } from 'node:test';+import { chromium, firefox } from 'playwright';++const html = `<!doctype html><main><h1>Feed</h1><p id="clock">0</p><input id="field"><button id="send" type="button">Send</button></main>`;++async function fixture(browser, build) {+ const context = await browser.newContext();+ await context.route('https://lifecycle.test/**', route => route.fulfill({ contentType: 'text/html', body: html }));+ await context.addInitScript(() => {+ window.feedback = []; window.messageListeners = []; window.replyMode = 'accept'; window.runToken = 'run-1';+ window.pageshowListeners = new Set();+ // Observe the monitor's own BFCache registration without changing it.+ const add = EventTarget.prototype.addEventListener, remove = EventTarget.prototype.removeEventListener;+ EventTarget.prototype.addEventListener = function (type, handler, options) {+ if (this === window && type === 'pageshow') pageshowListeners.add(handler);+ return add.call(this, type, handler, options);+ };+ EventTarget.prototype.removeEventListener = function (type, handler, options) {+ if (this === window && type === 'pageshow') pageshowListeners.delete(handler);+ return remove.call(this, type, handler, options);+ };+ const replies = { accept: { accepted: true }, stale: { accepted: false, reason: 'stale-observation' },+ duplicate: { accepted: false, reason: 'duplicate-observation' }, invalid: { accepted: false, reason: 'invalid-observation' } };+ const runtime = {+ onMessage: { addListener: fn => messageListeners.push(fn), removeListener: fn => { messageListeners = messageListeners.filter(item => item !== fn); } },+ async sendMessage(msg) {+ if (msg.action === 'get_page_monitor_state') return replyMode === 'stale' ? { active: false } : { active: true, runToken, documentToken: msg.documentToken };+ if (msg.action === 'page_feedback') { feedback.push(msg.feedback); return replies[replyMode]; }+ return {};+ },+ };+ window.chrome = { runtime }; window.browser = window.chrome;+ window.deliver = (action, params = {}, extra = {}) => new Promise(resolve => {+ let answered = false;+ const respond = value => { answered = true; resolve(value); };+ for (const listener of messageListeners) {+ const asynchronous = listener({ target: 'content', action, params: { runToken, ...params }, ...extra }, {}, respond);+ if (answered || asynchronous === true) return;+ }+ resolve(undefined);+ });+ });+ await context.addInitScript({ content: fs.readFileSync(new URL(`../src/${build}/src/content/page-monitor-shadow.js`, import.meta.url), 'utf8') });+ await context.addInitScript({ content: fs.readFileSync(new URL(`../src/${build}/src/content/page-monitor.js`, import.meta.url), 'utf8') });+ const page = await context.newPage();+ await page.goto('https://lifecycle.test/start');+ await page.waitForFunction(() => window.__wbPageMonitor?.active);+ await page.waitForTimeout(200);+ await page.evaluate(() => { feedback = []; });+ return { context, page };+}++const tick = page => page.evaluate(() => { const clock = document.getElementById('clock'); clock.textContent = String(Number(clock.textContent) + 1); });++for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {+ test(`${build}: an orphaned monitor stops on a stale-observation reply`, { timeout: 60000 }, async t => {+ const browser = await engine.launch({ headless: true });+ try {+ await t.test('the run is gone: stop observing and stop waking the background', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ await page.evaluate(() => { replyMode = 'stale'; });+ await tick(page);+ await page.waitForFunction(() => feedback.length > 0);+ await page.waitForTimeout(100);+ assert.equal(await page.evaluate(() => __wbPageMonitor.active), false);+ assert.equal(await page.evaluate(() => document.documentElement.hasAttribute('data-webbrain-page-revision')), false);+ const sent = await page.evaluate(() => feedback.length);+ for (let i = 0; i < 3; i++) { await tick(page); await page.waitForTimeout(200); }+ assert.equal(await page.evaluate(() => feedback.length), sent, 'No further page_feedback messages after the run is gone');+ // A later run reactivates the same document through the normal handshake.+ await page.evaluate(() => { replyMode = 'accept'; runToken = 'run-2'; });+ await page.evaluate(() => deliver('page_monitor_state', {}, { active: true }));+ await page.waitForFunction(() => __wbPageMonitor.active);+ await tick(page);+ await page.waitForFunction(count => feedback.length > count, sent);+ } finally { await context.close(); }+ });++ for (const mode of ['duplicate', 'invalid']) await t.test(`a ${mode}-observation reply keeps monitoring`, async () => {+ const { context, page } = await fixture(browser, build);+ try {+ await page.evaluate(mode => { replyMode = mode; }, mode);+ await tick(page);+ await page.waitForFunction(() => feedback.length > 0);+ await page.waitForTimeout(100);+ assert.equal(await page.evaluate(() => __wbPageMonitor.active), true);+ } finally { await context.close(); }+ });+ } finally { await browser.close(); }+ });++ test(`${build}: run-scoped listeners and local operations do not outlive their owner`, { timeout: 90000 }, async t => {+ const browser = await engine.launch({ headless: true });+ try {+ await t.test('disposal removes the BFCache pageshow listener', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ assert.equal(await page.evaluate(() => pageshowListeners.size), 1);+ await page.evaluate(() => __wbPageMonitor.dispose());+ assert.equal(await page.evaluate(() => pageshowListeners.size), 0, 'A disposed monitor must not stay reachable from window');+ } finally { await context.close(); }+ });++ await t.test('a content action finishing after stop/start keeps the new run\'s operation', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ const observed = await page.evaluate(async () => {+ const finishOld = __wbPageMonitor.beginContentAction('type_ax', { selector: '#field' });+ await deliver('page_monitor_state', { runToken: 'run-1' }, { active: false, runToken: 'run-1' });+ runToken = 'run-2';+ await deliver('page_monitor_state', {}, { active: true });+ const finishNew = __wbPageMonitor.beginContentAction('type_ax', { selector: '#field' });+ finishOld(); // The old run's action completes late.+ feedback = [];+ const field = document.getElementById('field');+ __wbPageMonitor.withLocalDispatch(() => { field.value = 'agent'; field.dispatchEvent(new Event('input', { bubbles: true })); });+ finishNew();+ await new Promise(resolve => setTimeout(resolve, 50));+ return feedback.map(event => `${event.kind}/${event.source}`);+ });+ assert.equal(observed.includes('input/page'), false, `The new run's synthetic input stays owned: ${observed}`);+ } finally { await context.close(); }+ });++ if (build === 'chrome') await t.test('removed shadow roots stay collectable while the run is active', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ await page.evaluate(async () => {+ for (let i = 0; i < 300; i++) {+ const host = document.createElement('div'); document.body.append(host);+ host.attachShadow({ mode: 'open' }).innerHTML = '<span>card</span>';+ if (i % 50 === 49) await new Promise(resolve => setTimeout(resolve, 0));+ host.remove();+ }+ await new Promise(resolve => setTimeout(resolve, 300));+ });+ assert.equal(await page.evaluate(() => __wbPageMonitor.active), true);+ const session = await context.newCDPSession(page);+ await session.send('HeapProfiler.enable');+ await session.send('HeapProfiler.collectGarbage');+ const prototype = await session.send('Runtime.evaluate', { expression: 'ShadowRoot.prototype' });+ const objects = await session.send('Runtime.queryObjects', { prototypeObjectId: prototype.result.objectId });+ const count = await session.send('Runtime.callFunctionOn', { objectId: objects.objects.objectId,+ functionDeclaration: 'function () { return this.length; }', returnByValue: true });+ assert.ok(count.result.value < 30, `Removed shadow roots retained: ${count.result.value}`);+ } finally { await context.close(); }+ });++ await t.test('shadow-root listeners still observe and are removed on stop', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ const result = await page.evaluate(async () => {+ const host = document.createElement('div'); document.body.append(host);+ const root = host.attachShadow({ mode: 'open' });+ root.innerHTML = '<div popover id="tip">Tip</div>';+ await new Promise(resolve => setTimeout(resolve, 50));+ feedback = [];+ root.getElementById('tip').showPopover();+ await new Promise(resolve => setTimeout(resolve, 250));+ const observed = feedback.length > 0;+ await deliver('page_monitor_state', { runToken: 'run-1' }, { active: false, runToken: 'run-1' });+ feedback = [];+ root.getElementById('tip').hidePopover();+ await new Promise(resolve => setTimeout(resolve, 250));+ return { observed, afterStop: feedback.length };+ });+ assert.deepEqual(result, { observed: true, afterStop: 0 });+ } finally { await context.close(); }+ });+ } finally { await browser.close(); }+ });+}diff --git a/test/monitor-local-attribution.mjs b/test/monitor-local-attribution.mjsnew file mode 100644index 000000000..6b28f75fa--- /dev/null+++ b/test/monitor-local-attribution.mjs@@ -0,0 +1,163 @@+// Attribution during content-script (synthetic) operations: trusted key and+// pointer presses cannot come from synthetic dispatch and belong to the user,+// while the operation's own execCommand/focus/blur effects stay the agent's.+import assert from 'node:assert/strict';+import fs from 'node:fs';+import { test } from 'node:test';+import { chromium, firefox } from 'playwright';++const RUN = 'local-run';+const html = `<!doctype html><style>body { margin: 20px; font: 16px sans-serif; } button, input { margin: 8px; }</style>+<main><h1>Profile</h1><form id="form" action="/results" method="get">+<label>Query <input id="q" name="q"></label><label>Name <input id="a" name="a"></label>+<button id="go" type="submit">Search</button><button id="b" type="button">Next</button></form>+<div id="editor" contenteditable="true" role="textbox" aria-label="Notes"></div></main>+<script>+window.submits = 0; window.nextClicks = 0;+document.getElementById('form').addEventListener('submit', event => { event.preventDefault(); submits++; });+document.getElementById('b').addEventListener('click', () => nextClicks++);+</script>`;++async function fixture(browser, build) {+ const context = await browser.newContext({ viewport: { width: 900, height: 700 } });+ await context.route('https://local.test/**', route => route.fulfill({ contentType: 'text/html', body: html }));+ await context.addInitScript(RUN => {+ window.feedback = []; window.messageListeners = [];+ const runtime = {+ onMessage: { addListener: fn => messageListeners.push(fn), removeListener() {} },+ async sendMessage(msg) {+ if (msg.action === 'get_page_monitor_state') return { active: true, runToken: RUN, documentToken: msg.documentToken };+ if (msg.action === 'page_feedback') { feedback.push(msg.feedback); return { accepted: true }; }+ return {};+ },+ };+ window.chrome = { runtime }; window.browser = window.chrome;+ window.__wb_ax_lookup = ref => document.getElementById(ref.replace(/^ref_/, ''));+ window.deliver = (action, params = {}) => new Promise(resolve => {+ let answered = false;+ const respond = value => { answered = true; resolve(value); };+ for (const listener of messageListeners) {+ const asynchronous = listener({ target: 'content', action, params: { runToken: RUN, ...params } }, {}, respond);+ if (answered || asynchronous === true) return;+ }+ resolve(undefined);+ });+ }, RUN);+ await context.addInitScript({ content: fs.readFileSync(new URL(`../src/${build}/src/content/page-monitor.js`, import.meta.url), 'utf8') });+ const page = await context.newPage();+ await page.goto('https://local.test/start');+ await page.waitForFunction(() => window.__wbPageMonitor?.active);+ await page.waitForTimeout(250);+ await page.evaluate(() => { feedback = []; });+ return { context, page };+}++const events = page => page.evaluate(() => feedback.map(event => `${event.kind}/${event.source}${event.operation ? ':' + event.operation : ''}`));++for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {+ test(`${build}: physical input during a synthetic content operation belongs to the user`, { timeout: 90000 }, async t => {+ const browser = await engine.launch({ headless: true });+ try {+ await t.test('a user Enter during a type settle wait is not correlated to the agent (r7)', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ await page.evaluate(() => {+ const q = document.getElementById('q');+ window.finishLocal = __wbPageMonitor.beginContentAction('type_ax', { ref_id: 'ref_q' });+ __wbPageMonitor.beforeLocalDispatch({ preparation: true, kind: 'focus', target: q });+ q.focus();+ __wbPageMonitor.withLocalDispatch(() => { q.value = 'webbrain'; q.dispatchEvent(new Event('input', { bubbles: true })); });+ });+ await page.keyboard.press('Enter'); // The operation is still settling.+ await page.evaluate(() => finishLocal());+ await page.waitForTimeout(260);+ const observed = await events(page);+ assert.equal(await page.evaluate(() => submits), 1);+ assert.ok(observed.includes('activity/user'), `The physical key is user activity: ${observed}`);+ assert.equal(observed.some(event => event.startsWith('activity/agent')), false, `No agent submit correlation: ${observed}`);+ } finally { await context.close(); }+ });++ await t.test('a user mouse click during a synthetic click operation is reported', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ await page.evaluate(() => {+ window.finishLocal = __wbPageMonitor.beginContentAction('click', { selector: '#b' });+ __wbPageMonitor.withLocalDispatch(() => document.getElementById('b').click());+ });+ await page.click('#b');+ await page.evaluate(() => finishLocal());+ await page.waitForTimeout(100);+ assert.equal(await page.evaluate(() => nextClicks), 2);+ // The trusted pointer press itself (not only a later duplicate click) is the user's.+ assert.ok((await events(page)).includes('activity/user'), `The physical press is user input: ${await events(page)}`);+ } finally { await context.close(); }+ });++ await t.test('the operation\'s own execCommand input and preparation focus stay attributed to it', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ await page.evaluate(() => {+ const editor = document.getElementById('editor');+ const finish = __wbPageMonitor.beginContentAction('type_ax', { ref_id: 'ref_editor' });+ __wbPageMonitor.beforeLocalDispatch({ preparation: true, kind: 'focus', target: editor });+ editor.focus();+ __wbPageMonitor.withLocalDispatch(() => document.execCommand('insertText', false, 'Agent note'));+ finish();+ });+ await page.waitForTimeout(260);+ assert.equal(await page.evaluate(() => document.getElementById('editor').textContent), 'Agent note');+ const observed = await events(page);+ assert.equal(observed.some(event => event.endsWith('/user') || event === 'input/page' || event === 'activity/page'), false, observed.join());+ } finally { await context.close(); }+ });+ } finally { await browser.close(); }+ });++ test(`${build}: the blur commit caused by the agent's own preparation focus is not an intervention (r8)`, { timeout: 90000 }, async t => {+ const browser = await engine.launch({ headless: true });+ try {+ const agentTypes = async page => {+ await page.evaluate(() => document.getElementById('a').focus());+ await page.evaluate(() => deliver('page_monitor_prepare', { operationId: 'typing', tool: 'type_ax', selector: '#a' }));+ await page.evaluate(() => deliver('page_monitor_dispatch', { operationId: 'typing', kind: 'input', selector: '#a',+ eventTypes: ['beforeinput', 'input', 'change'] }));+ await page.keyboard.insertText('Ada Lovelace');+ await page.evaluate(() => deliver('page_monitor_finish', { operationId: 'typing' }));+ await page.waitForTimeout(400); // The next decision: the typing operation has expired.+ };+ const localClickOnB = page => page.evaluate(() => {+ const b = document.getElementById('b');+ const finish = __wbPageMonitor.beginContentAction('click_ax', { ref_id: 'ref_b' });+ try {+ __wbPageMonitor.beforeLocalDispatch({ preparation: true, kind: 'focus', target: b });+ b.focus(); // Commits field A: the browser fires a trusted change there.+ __wbPageMonitor.withLocalDispatch(() => b.click());+ return 'clicked';+ } catch (error) { return error.pagePreparationVeto?.predicate || error.code || 'error'; }+ finally { finish(); }+ });++ await t.test('the agent click proceeds and the commit is not reported as user input', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ await agentTypes(page);+ await page.evaluate(() => { feedback = []; });+ assert.equal(await localClickOnB(page), 'clicked');+ assert.equal(await page.evaluate(() => nextClicks), 1);+ assert.equal((await events(page)).some(event => event.endsWith('/user')), false, (await events(page)).join());+ } finally { await context.close(); }+ });++ await t.test('a user edit in the same field is still reported by its own input events', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ await page.click('#a');+ await page.keyboard.type('Grace');+ await page.waitForTimeout(100);+ assert.ok((await events(page)).includes('input/user'));+ } finally { await context.close(); }+ });+ } finally { await browser.close(); }+ });+}diff --git a/test/monitor-owned-geometry.mjs b/test/monitor-owned-geometry.mjsnew file mode 100644index 000000000..f77b592ce--- /dev/null+++ b/test/monitor-owned-geometry.mjs@@ -0,0 +1,235 @@+// Agent-owned preparation geometry and decision-level geometry for model-bound+// targets: the agent's own selector scroll or hover may move the exact node,+// and a pure layout/scroll move before preparation keeps the model decision.+// Semantics, hit, user intervention and later moves stay exactly checked.+import assert from 'node:assert/strict';+import fs from 'node:fs';+import { test } from 'node:test';+import { chromium, firefox } from 'playwright';++const RUN = 'geometry-run';+const MOVE = ['pointerover', 'pointerenter', 'mouseover', 'mouseenter', 'pointermove', 'mousemove'];+const PRESS = ['pointerover', 'pointerenter', 'mouseover', 'mouseenter', 'pointerdown', 'mousedown', 'click', 'input', 'change'];+const page = ({ spacer = 0, css = '' } = {}) => `<!doctype html><style>+body { margin: 20px; font: 16px sans-serif; } #commit { width: 200px; height: 45px; } .more { display: none; } ${css}+</style><div id="banner-slot"></div>${spacer ? `<div style="height:${spacer}px"></div>` : ''}+<main><h1>Current record settings</h1><button id="commit" type="button">Apply settings</button>+<button class="more" type="button">More actions</button>+<label>Record title <input id="title" value="Owned neutral draft"></label></main>+<aside><span id="counter">0</span></aside><div style="height:3000px"></div>+<script>window.clicks = 0; document.getElementById('commit').addEventListener('click', () => clicks++);</script>`;++async function fixture(browser, build, body) {+ const context = await browser.newContext({ viewport: { width: 900, height: 700 } });+ await context.route('https://geometry.test/**', route => route.fulfill({ contentType: 'text/html', body }));+ await context.addInitScript(RUN => {+ window.feedback = []; window.messageListeners = [];+ const runtime = {+ onMessage: { addListener: fn => messageListeners.push(fn), removeListener() {} },+ async sendMessage(msg) {+ if (msg.action === 'get_page_monitor_state') return { active: true, runToken: RUN, documentToken: msg.documentToken };+ if (msg.action === 'page_feedback') { feedback.push(msg.feedback); return { accepted: true }; }+ return {};+ },+ };+ window.chrome = { runtime }; window.browser = window.chrome;+ window.__wb_ax_lookup = ref => document.getElementById(ref.replace(/^ref_/, ''));+ window.deliver = (action, params = {}) => new Promise(resolve => {+ let answered = false;+ const respond = value => { answered = true; resolve(value); };+ for (const listener of messageListeners) {+ const asynchronous = listener({ target: 'content', action, params: { runToken: RUN, ...params } }, {}, respond);+ if (answered || asynchronous === true) return;+ }+ resolve(undefined);+ });+ }, RUN);+ await context.addInitScript({ content: fs.readFileSync(new URL(`../src/${build}/src/content/page-monitor.js`, import.meta.url), 'utf8') });+ const tab = await context.newPage();+ await tab.goto('https://geometry.test/start');+ await tab.waitForFunction(() => window.__wbPageMonitor?.active);+ await tab.mouse.move(5, 690);+ await tab.waitForTimeout(250);+ await tab.evaluate(() => { feedback = []; });+ return { context, page: tab };+}++const message = (tab, action, params = {}) => tab.evaluate(({ action, params }) => Promise.race([deliver(action, params),+ new Promise(resolve => setTimeout(() => resolve({ ready: false, code: 'monitor_response_missing' }), 3000))]), { action, params });++async function certify(tab, extra = {}) {+ const snapshot = await message(tab, 'page_monitor_capture_model');+ assert.equal(snapshot.ready, true);+ const base = { runToken: RUN, operationId: 'owned', tool: 'click', selector: '#commit',+ expectedModelSnapshot: snapshot.snapshotToken, allowPassiveRebase: true, ...extra };+ assert.equal((await message(tab, 'page_monitor_prepare', base)).modelBindingValid, true);+ return { snapshot, base };+}++async function center(tab) {+ const box = await tab.locator('#commit').boundingBox();+ return { x: box.x + box.width / 2, y: box.y + box.height / 2 };+}++for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {+ test(`${build}: the agent's own selector scroll refreshes only its target geometry`, { timeout: 120000 }, async t => {+ const browser = await engine.launch({ headless: true });+ try {+ const selectorClick = async (tab, between = async () => {}) => {+ const { base } = await certify(tab);+ const fence = await message(tab, 'page_monitor_dispatch', { ...base, kind: 'scroll', scrollIntoView: true, fenceOnly: true });+ assert.equal(fence.ready, true);+ const before = await tab.evaluate(() => scrollY);+ await tab.evaluate(guard => {+ const commit = document.getElementById('commit');+ commit.dispatchEvent(new CustomEvent('webbrain-agent-scroll-dispatch', { bubbles: true, composed: true, cancelable: true, detail: JSON.stringify(guard) }));+ commit.scrollIntoView({ block: 'center', inline: 'center', behavior: 'instant' });+ }, fence.guard);+ assert.ok(await tab.evaluate(() => scrollY) > before, 'The owned preparation scroll moved the target');+ await between(tab);+ const point = await center(tab);+ const movement = await message(tab, 'page_monitor_dispatch', { ...base, kind: 'click', ...point, navigationCandidate: false,+ pointerPreparationOnly: true, eventTypes: MOVE });+ if (!movement.ready) return { stage: 'movement', predicate: movement.preparationVeto?.predicate };+ const press = await message(tab, 'page_monitor_dispatch', { ...base, kind: 'click', ...point, navigationCandidate: true, eventTypes: PRESS });+ return press.ready ? { stage: 'ready' } : { stage: 'press', predicate: press.preparationVeto?.predicate };+ };++ await t.test('a certified selector target that was not centred is pressed after the owned scroll', async () => {+ const { context, page: tab } = await fixture(browser, build, page({ spacer: 440 }));+ try { assert.deepEqual(await selectorClick(tab), { stage: 'ready' }); }+ finally { await context.close(); }+ });++ await t.test('unrelated churn between the owned scroll and the pointer stays observable without a veto', async () => {+ const { context, page: tab } = await fixture(browser, build, page({ spacer: 440 }));+ try {+ assert.deepEqual(await selectorClick(tab, tab => tab.evaluate(() => { document.getElementById('counter').textContent = '1'; })),+ { stage: 'ready' });+ await tab.waitForTimeout(260);+ assert.ok((await tab.evaluate(() => feedback)).some(event => event.kind === 'dom'));+ } finally { await context.close(); }+ });++ for (const [name, between] of [+ ['a changed target label', tab => tab.evaluate(() => { document.getElementById('commit').textContent = 'Delete records'; })],+ ['a covering overlay', tab => tab.evaluate(() => {+ const overlay = document.createElement('div');+ overlay.style.cssText = 'position:fixed;inset:0;background:white;z-index:10';+ document.body.append(overlay);+ })],+ ['a later page move of the target', tab => tab.evaluate(() => { document.getElementById('commit').style.marginLeft = '40px'; })],+ ['a genuine user wheel scroll', tab => tab.mouse.wheel(0, 120).then(() => tab.waitForTimeout(100))],+ ]) await t.test(`${name} after the owned scroll still refuses the press`, async () => {+ const { context, page: tab } = await fixture(browser, build, page({ spacer: 440 }));+ try {+ const result = await selectorClick(tab, between);+ assert.notEqual(result.stage, 'ready', name);+ assert.equal(await tab.evaluate(() => clicks), 0);+ } finally { await context.close(); }+ });+ } finally { await browser.close(); }+ });++ test(`${build}: the agent's own hover may move but not change its certified target`, { timeout: 120000 }, async t => {+ const browser = await engine.launch({ headless: true });+ try {+ const hoverThenPress = async (tab, after = async () => {}) => {+ const { base } = await certify(tab);+ const point = await center(tab);+ const movement = await message(tab, 'page_monitor_dispatch', { ...base, kind: 'click', ...point, navigationCandidate: false,+ pointerPreparationOnly: true, eventTypes: MOVE });+ assert.equal(movement.ready, true);+ await tab.mouse.move(point.x, point.y);+ await tab.waitForTimeout(60);+ await after(tab);+ const press = await message(tab, 'page_monitor_dispatch', { ...base, kind: 'click', ...point, navigationCandidate: true, eventTypes: PRESS });+ return press.ready ? 'ready' : press.preparationVeto?.predicate || 'vetoed';+ };++ 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} does not refuse the press`, async () => {+ const { context, page: tab } = await fixture(browser, build, page({ css }));+ try { assert.equal(await hoverThenPress(tab), 'ready'); }+ finally { await context.close(); }+ });++ await t.test('a hover-revealed control in the owner is not accepted silently', async () => {+ const { context, page: tab } = await fixture(browser, build, page({ css: 'main:hover .more { display: inline-block; }' }));+ try { assert.notEqual(await hoverThenPress(tab), 'ready'); }+ finally { await context.close(); }+ });++ await t.test('a page move after the hover phase still refuses the press', async () => {+ const { context, page: tab } = await fixture(browser, build, page({ css: '#commit:hover { transform: translateY(-3px); }' }));+ try {+ assert.notEqual(await hoverThenPress(tab, tab => tab.evaluate(() => { document.getElementById('commit').style.marginLeft = '40px'; })), 'ready');+ } finally { await context.close(); }+ });+ } finally { await browser.close(); }+ });++ test(`${build}: a pure layout move before preparation keeps the model decision`, { timeout: 120000 }, async t => {+ const browser = await engine.launch({ headless: true });+ try {+ const decide = async (tab, mutate) => {+ const snapshot = await message(tab, 'page_monitor_capture_model');+ await mutate(tab);+ await tab.waitForTimeout(260);+ const validated = await message(tab, 'page_monitor_validate_model', { snapshotToken: snapshot.snapshotToken, tool: 'click_ax', ref_id: 'ref_commit' });+ const prepared = await message(tab, 'page_monitor_prepare', { operationId: 'decision', tool: 'click_ax', ref_id: 'ref_commit',+ allowPassiveRebase: true, expectedModelSnapshot: snapshot.snapshotToken });+ return { validated, prepared };+ };+ const banner = tab => tab.evaluate(() => { document.getElementById('banner-slot').innerHTML = '<p style="height:90px">Cookie notice</p>'; });++ await t.test('a banner inserted above the target keeps and dispatches the same node', async () => {+ const { context, page: tab } = await fixture(browser, build, page());+ try {+ const before = await center(tab);+ const { validated, prepared } = await decide(tab, banner);+ assert.ok((await center(tab)).y > before.y + 50, 'The target moved down');+ assert.equal(validated.ready, true, JSON.stringify(validated));+ assert.equal(prepared.modelBindingValid, true);+ assert.equal((await message(tab, 'page_monitor_dispatch', { operationId: 'decision', kind: 'click' })).ready, true);+ await tab.evaluate(() => __wbPageMonitor.withPreparedDispatch('decision', () => document.getElementById('commit').click()));+ assert.equal(await tab.evaluate(() => clicks), 1);+ } finally { await context.close(); }+ });++ for (const [name, mutate] of [+ ['a same-text clone replacing the target', tab => tab.evaluate(() => {+ const commit = document.getElementById('commit'); commit.replaceWith(commit.cloneNode(true));+ })],+ ['an overlay covering the target', tab => tab.evaluate(() => {+ const overlay = document.createElement('div');+ overlay.style.cssText = 'position:fixed;inset:0;background:white;z-index:10';+ document.body.append(overlay);+ })],+ ['a genuine user scroll', tab => tab.mouse.wheel(0, 40)],+ ]) await t.test(`${name} is refused`, async () => {+ const { context, page: tab } = await fixture(browser, build, page());+ try {+ const { validated, prepared } = await decide(tab, mutate);+ assert.equal(validated.ready, false);+ assert.equal(prepared.modelBindingValid, false);+ } finally { await context.close(); }+ });++ await t.test('a move between preparation and dispatch is still checked exactly', async () => {+ const { context, page: tab } = await fixture(browser, build, page());+ try {+ const { prepared } = await decide(tab, async () => {});+ assert.equal(prepared.modelBindingValid, true);+ await banner(tab);+ const dispatched = await message(tab, 'page_monitor_dispatch', { operationId: 'decision', kind: 'click' });+ assert.equal(dispatched.ready, false);+ assert.equal(dispatched.preparationVeto?.predicate, 'geometry_changed');+ assert.equal(await tab.evaluate(() => clicks), 0);+ } finally { await context.close(); }+ });+ } finally { await browser.close(); }+ });+}diff --git a/test/page-monitor-decorative-hit.mjs b/test/page-monitor-decorative-hit.mjsindex 2abbd94a4..9b44ac9ec 100644--- a/test/page-monitor-decorative-hit.mjs+++ b/test/page-monitor-decorative-hit.mjs@@ -178,7 +178,6 @@ const changes = [}],['account owner identity', () => { document.getElementById('owner').setAttribute('data-account-id', 'other-owned-account'); }],['action label', () => { document.getElementById('action').setAttribute('aria-label', 'Different account action'); }],- ['action geometry', () => { document.getElementById('action').style.left = '40px'; }],['action node', () => { const action = document.getElementById('action'); action.replaceWith(action.cloneNode(true)); }],['scope node', () => { const owner = document.getElementById('owner'); owner.replaceWith(owner.cloneNode(true)); }],];@@ -315,6 +314,22 @@ for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {} finally { await context.close(); }});+ // A ref decision binds the node, not its coordinates: a pure move whose+ // hit is the same normalized node keeps the decision (rebased before+ // preparation). Replaced or non-decorative hits below still block.+ await t.test('action geometry alone keeps the old model action for the same node', async () => {+ const { context, page } = await fixture(browser, build);+ try {+ const captured = await capture(page);+ await page.evaluate(() => { document.getElementById('action').style.left = '40px'; });+ assert.equal((await validate(page, captured)).ready, true);+ const executed = await dispatch(page, captured);+ assert.equal(executed.fence.ready, true, JSON.stringify(executed));+ assert.equal(executed.result?.success, true, JSON.stringify(executed));+ assert.equal(executed.clicks, 1);+ } finally { await context.close(); }+ });+for (const [name, mutate] of changes) await t.test(`${name} blocks the old model action`, async () => {const { context, page } = await fixture(browser, build);try {diff --git a/test/page-monitor-passive-progress.mjs b/test/page-monitor-passive-progress.mjsindex de4beacfe..2edb1e3e3 100644--- a/test/page-monitor-passive-progress.mjs+++ b/test/page-monitor-passive-progress.mjs@@ -1409,7 +1409,6 @@ for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {['form action', () => { document.getElementById('form').action = '/other'; }],['external accessible label', () => { document.getElementById('send-label').textContent = 'Delete'; }],['target node identity', () => { const node = document.getElementById('send'); node.replaceWith(node.cloneNode(true)); }],- ['target geometry', () => { document.getElementById('send').style.transform = 'translateX(45px)'; }],['opaque occluding overlay', () => {const rect = document.getElementById('send').getBoundingClientRect(); const overlay = document.createElement('div');overlay.style.cssText = `position:fixed;z-index:99999;background:black;left:${rect.left}px;top:${rect.top}px;width:${rect.width}px;height:${rect.height}px`;@@ -1429,6 +1428,23 @@ for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {} finally { await context.close(); }});+ // A ref/selector decision binds the node, not its coordinates: a pure+ // move with identical semantics and hit is rebased before preparation.+ // Geometry is still exact from preparation to dispatch (see+ // test/monitor-owned-geometry.mjs).+ await t.test('target geometry changed before prepare keeps the decision for the same node', async () => {+ const { context, page } = await modelFixture(browser, build);+ try {+ const snapshot = await captureModel(page);+ await page.evaluate(() => { document.getElementById('send').style.transform = 'translateX(45px)'; });+ await page.waitForTimeout(180);+ assert.equal((await validateModel(page, snapshot)).ready, true);+ assert.equal((await prepareModel(page, snapshot)).modelBindingValid, true);+ assert.equal((await dispatchModel(page)).ready, true);+ assert.equal(await page.evaluate(() => submissions), 1);+ } finally { await context.close(); }+ });+await t.test('target changed after successful model validation is checked again during prepare', async () => {const { context, page } = await modelFixture(browser, build);try {