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
Fix photo downloads rejected by passive page updates
Emre Sokullu44df6ba871e4
diff --git a/docs/page-feedback-monitor.md b/docs/page-feedback-monitor.mdindex 44c8356bf..c35ed62b1 100644--- a/docs/page-feedback-monitor.md+++ b/docs/page-feedback-monitor.md@@ -94,6 +94,16 @@ Steer or grant authorization. Enter and Alt+Enter retain their existing behaviorexact media asset before inference. The downloader validates that identitybefore fetching and again before saving. A replaced asset fails without adownload; a bound attempt cannot fall back to another MSE or vision resource.+ A transparent backing image can bind to the same visible image painted by its+ immediate parent/sibling; the carrier, exact asset and rendered box must match.+ Binding comparison ignores object-key ordering introduced by browser API+ serialization, while preserving every key, value and array position.+ Missing capture yields a read-only `media_binding_unavailable` tool result;+ the model can inspect sources and choose an explicit URL. It does not attempt+ an unbound fetch/save or count optional capture failure as a page-change retry.+ Capture rechecks URL, document and steering identity after injection. When a+ browser omits document IDs, an active content-monitor acknowledgement must+ prove the same run/document token; a failed registration cannot verify media.Bulk and scrolling downloads keep their existing conservative behavior.- An unchanged AX target can opt into passive revalidation only after a freshtarget/context comparison. The content monitor then checks immutable node,@@ -109,7 +119,9 @@ Steer or grant authorization. Enter and Alt+Enter retain their existing behaviorresponses. App-owned feedback trees and captures are replaced by the latestobservation, preserving user instructions and tool results instead of growingthe prompt with repeated copies. Trace notes record disposition, reason,- refresh time, stage, tool names and retry streak without action arguments.+ refresh time, stage, tool names, retention policies and retry streak without+ action arguments. `media_binding_capture` reports sanitized capture status,+ target-specific reason codes and bounded counts without URLs or page labels.## Workflow and notificationsdiff --git a/src/chrome/src/agent/media-download-binding.js b/src/chrome/src/agent/media-download-binding.jsindex 9f502db0f..56cecbe93 100644--- a/src/chrome/src/agent/media-download-binding.js+++ b/src/chrome/src/agent/media-download-binding.js@@ -9,3 +9,27 @@ export function captureMediaDownloadBindingInPage(options = null) {}return bindings;}++/** Read-only capture; failures are observations, never evidence of user activity. */+export function captureMediaDownloadStateInPage() {+ const downloader = globalThis.window?.SocialMediaDownloader;+ const bindings = {}, diagnostics = {};+ if (typeof downloader?.getMediaBinding !== 'function') {+ return { bindings, diagnostics, status: 'library_unavailable' };+ }+ for (const target of ['auto', 'image', 'video']) {+ const options = { mode: 'main', target, limit: 1 };+ try {+ bindings[target] = downloader.getMediaBinding(options);+ diagnostics[target] = bindings[target] ? { status: 'bound', reason: 'verified_resource',+ candidateCount: bindings[target].candidates.length, sourceCount: bindings[target].sources.length,+ paintCarrierCount: bindings[target].sources.filter(source => source.paintCarrier).length }+ : typeof downloader.getMediaBindingDiagnostics === 'function' ? downloader.getMediaBindingDiagnostics(options)+ : { status: 'unavailable', reason: 'no_verified_media' };+ } catch {+ bindings[target] = null;+ diagnostics[target] = { status: 'unavailable', reason: 'capture_failed' };+ }+ }+ return { bindings, diagnostics, status: Object.values(bindings).some(Boolean) ? 'ready' : 'unavailable' };+}diff --git a/src/chrome/src/agent/page-feedback-policy.js b/src/chrome/src/agent/page-feedback-policy.jsindex 65837bdb0..074dbdf16 100644--- a/src/chrome/src/agent/page-feedback-policy.js+++ b/src/chrome/src/agent/page-feedback-policy.js@@ -78,12 +78,18 @@ export function pageFeedbackCallPolicy(name, args = {}, state = {}, currentPageif (name === 'download_public_media' && (httpUrl(args.url) || httpUrl(state.url))) return { kind: 'independent' };if (name === 'navigate' && httpUrl(args.url)) return { kind: 'navigate' };if (name === 'done' && args.outcome === 'success' && currentPage?.success !== false) return { kind: 'observe', terminal: true };- if (name === 'download_social_media' && args.strategy !== 'vision' && !args.scroll && args.mode !== 'all'- && (!args.limit || args.limit === 1)) {+ if (name === 'download_social_media' && !args.scroll && !args.all+ && [undefined, 'main', 'auto'].includes(args.mode)+ && [undefined, 'dom', 'auto'].includes(args.strategy)+ && (args.limit == null || args.limit === 1)) {const target = !args.target || args.target === 'media' ? 'auto' : args.target;- const binding = state.mediaBindings?.[target];- if (binding && (args.mode === 'main' || ((!args.mode || args.mode === 'auto') && binding.focused))) {- return { kind: 'media', binding, target };+ if (['auto', 'image', 'video'].includes(target)) {+ const binding = state.mediaBindings?.[target];+ if (binding && (args.mode === 'main' || binding.focused)) return { kind: 'media', binding, target };+ // An unavailable optional precapture is not a changed page. Dispatch a+ // read-only resolver result so the model can inspect actual media sources;+ // this policy never permits an unbound fetch/save or a fallback mutation.+ return { kind: 'media_resolve', target, reason: binding ? 'focus_unverified' : 'media_binding_missing' };}}if (!AX_ACTIONS.has(name) || !currentPage || events.some(event => /^(?:form|input|textarea|select|option)(?:$|[\s#.])/.test(event.target || ''))) return { kind: 'unsafe' };diff --git a/src/chrome/src/agent/page-feedback.js b/src/chrome/src/agent/page-feedback.jsindex 04b170c5d..82fac5c3a 100644--- a/src/chrome/src/agent/page-feedback.js+++ b/src/chrome/src/agent/page-feedback.js@@ -1,6 +1,6 @@import { canRetainPageFeedbackCalls, isPassivePageFeedback, pageFeedbackCallPolicy } from './page-feedback-policy.js';import { accountFeedbackSupersession, resetFeedbackSupersession } from './page-feedback-recovery.js';-import { captureMediaDownloadBindingInPage } from './media-download-binding.js';+import { captureMediaDownloadStateInPage } from './media-download-binding.js';import * as trace from '../trace/recorder.js';/** Run-owned browser observations. Keep the Firefox copy byte-identical. */@@ -12,6 +12,42 @@ const clean = (value, limit = 240) => String(value ?? '').replace(/[\u0000-\u001const token = () => globalThis.crypto.randomUUID();const apiFor = () => globalThis.browser || globalThis.chrome;+async function captureMediaState(api, tabId) {+ let stage = 'injection_failed';+ try {+ let result;+ if (typeof api.tabs.executeScript === 'function') {+ await api.tabs.executeScript(tabId, { file: 'src/agent/social-media-downloader.js' });+ stage = 'capture_failed';+ result = (await api.tabs.executeScript(tabId, { code: `(${captureMediaDownloadStateInPage.toString()})()` }))?.[0];+ } else {+ await api.scripting.executeScript({ target: { tabId }, world: 'MAIN', files: ['src/agent/social-media-downloader.js'] });+ stage = 'capture_failed';+ result = (await api.scripting.executeScript({ target: { tabId }, world: 'MAIN', func: captureMediaDownloadStateInPage }))?.[0]?.result;+ }+ return result || { bindings: {}, diagnostics: {}, status: 'capture_failed' };+ } catch {+ return { bindings: {}, diagnostics: {}, status: stage };+ }+}++function mediaCaptureMetadata(capture) {+ const statuses = new Set(['ready', 'unavailable', 'library_unavailable', 'injection_failed', 'capture_failed', 'context_changed', 'context_unverified']);+ const reasons = new Set(['verified_resource', 'no_verified_media', 'capture_failed', 'unsupported_request',+ 'ambiguous_media', 'no_visible_media', 'no_live_source', 'no_matching_media', 'no_single_media',+ 'unsupported_target', 'unsupported_scope', 'source_unbound', 'capture_error', 'bound', 'media_unavailable', 'focus_unverified']);+ const targets = {};+ for (const target of ['auto', 'image', 'video']) {+ const diagnostic = capture?.diagnostics?.[target];+ targets[target] = { status: capture?.bindings?.[target] ? 'bound' : 'unavailable',+ reason: reasons.has(diagnostic?.reason) ? diagnostic.reason : 'no_verified_media' };+ for (const key of ['candidateCount', 'sourceCount', 'paintCarrierCount']) {+ if (Number.isSafeInteger(diagnostic?.[key])) targets[target][key] = Math.max(0, Math.min(128, diagnostic[key]));+ }+ }+ return { status: statuses.has(capture?.status) ? capture.status : 'unavailable', targets };+}+function clearGestureLease(run, frameId) {const lease = run.gestureLeases?.get(frameId);if (lease) clearTimeout(lease.timer);@@ -438,24 +474,56 @@ export const pageFeedbackMethods = {// The downloader is lazy-loaded. Capture its private document/node/asset// binding before inference, never from model arguments or page instructions.if (/^https?:\/\/(?:[^/]+\.)?(?:x\.com|twitter\.com|instagram\.com|youtube\.com|youtu\.be|tiktok\.com|facebook\.com|reddit\.com)\//i.test(url)) {+ let capture = await captureMediaState(apiFor(), tabId);+ // Injection is asynchronous. Never attach a new document's media to the+ // URL/document/frame/steering snapshot sampled before that injection.+ let contextVerified = false, contextStatus = 'context_unverified';try {const api = apiFor();- if (typeof api.tabs.executeScript === 'function') {- // Firefox MV2 downloads in the isolated extension world. Capture in- // that same world so its private node/document identities match.- await api.tabs.executeScript(tabId, { file: 'src/agent/social-media-downloader.js' });- const results = await api.tabs.executeScript(tabId, { code: `(${captureMediaDownloadBindingInPage.toString()})()` });- state.mediaBindings = results?.[0] || {};- } else {- await api.scripting.executeScript({ target: { tabId }, world: 'MAIN', files: ['src/agent/social-media-downloader.js'] });- const results = await api.scripting.executeScript({ target: { tabId }, world: 'MAIN', func: captureMediaDownloadBindingInPage });- state.mediaBindings = results?.[0]?.result || {};+ let monitorVerified = false;+ if (!documentId && state.frames.get(0)?.token) {+ // Firefox versions without browser document IDs must round-trip the+ // current content monitor registration, rather than trust a cached+ // token that could belong to a just-reloaded same-URL document.+ const response = await api.tabs.sendMessage(tabId, { target: 'content', action: 'page_monitor_state', active: true }, { frameId: 0 });+ monitorVerified = response?.ready === true && response.active === true+ && response.documentToken === state.frames.get(0).token && response.runToken === run.token;}- } catch { /* No binding means a stale media response cannot be retained. */ }+ const [tab, frames] = await Promise.all([api.tabs.get(tabId), api.webNavigation?.getAllFrames?.({ tabId })]);+ const currentDocument = frames?.find(frame => frame.frameId === 0)?.documentId;+ const mainFrame = state.frames.get(0);+ const identified = documentId ? currentDocument === documentId : !!mainFrame?.token && monitorVerified;+ contextVerified = identified && tab.url === url+ && this._pageFeedbackStateCurrent(tabId, run, state, url)+ && Object.values(capture.bindings || {}).every(binding => !binding || binding.pageUrl === url);+ contextStatus = contextVerified ? 'ready' : !documentId && !identified ? 'context_unverified' : 'context_changed';+ } catch { /* Failed identity verification permits observation only. */ }+ if (!contextVerified) capture = { bindings: {}, diagnostics: {}, status: contextStatus };+ state.mediaBindings = capture.bindings || {};+ state.mediaCapture = mediaCaptureMetadata(capture);+ const runId = this.currentRunId?.get(tabId);+ if (runId) trace.recordNote(runId, 0, 'media_binding_capture', state.mediaCapture);}if (this._pageFeedbackRuns?.get(tabId) === run) { run.modelState = state; run.validatedTargets = new Map(); }},+ async _resolvePageFeedbackMedia(tabId, target) {+ const capture = await captureMediaState(apiFor(), tabId);+ const diagnostic = mediaCaptureMetadata(capture);+ const binding = capture.bindings?.[target];+ // This is an observation only. A later model turn must choose a download+ // with a binding captured before that inference or an explicit observed URL.+ return { success: false, dispatched: false, noDispatch: true,+ errorCode: 'media_binding_unavailable', mediaResolution: binding ? 'resolved' : 'unavailable',+ ...(binding ? { currentCandidates: binding.candidates.map(({ url, type }) => ({ url, type })) } : {}),+ bindingDiagnostic: diagnostic.targets[target], captureStatus: diagnostic.status,+ error: 'The current media could not be verified before inference. No download was attempted. '+ + (target === 'image' ? 'Inspect current image sources with extract_data({type:"images"})'+ : 'Inspect current media sources with read_page_source or inspect_network_requests')+ + ' and download the intended explicit URL with download_files, or retry this tool after a unique focused resource is verified. '+ + 'Do not repeat an unresolved download.' };+ },+_pageFeedbackStateCurrent(tabId, run, state, url = run.url) {return !!state && state.token === run.token && state.url === url&& state.steeringRevision === (this._steeringRuns?.get(tabId)?.acceptedIds.size || 0)@@ -634,6 +702,14 @@ export const pageFeedbackMethods = {if (runId) trace.recordNote(runId, 0, 'page_feedback_decision', { stage, disposition: responseToolCalls === null ? 'observed' : accepted ? 'retained' : 'superseded',reason: sameDocument && isPassivePageFeedback(events) ? 'passive_dom' : 'intervention_or_document',refreshMs: Date.now() - started, streak: run.passiveSupersessionStreak || 0,+ toolPolicies: (responseToolCalls || []).flatMap(call => {+ try {+ const name = call.function?.name;+ if (!/^[a-z][a-z0-9_]{0,63}$/.test(name || '')) return [];+ const policy = pageFeedbackCallPolicy(name, JSON.parse(call.function.arguments), state, page, events);+ return [{ name, kind: policy.kind, ...(policy.reason ? { reason: policy.reason } : {}) }];+ } catch { return []; }+ }).slice(0, 16),toolNames: (responseToolCalls || []).map(call => call.function?.name).filter(name => /^[a-z][a-z0-9_]{0,63}$/.test(name || '')).slice(0, 16) });this._persist(tabId);return !accepted;@@ -651,6 +727,7 @@ export function installPageFeedback(Agent) {// A model cannot supply the private expected binding. Even without queued// feedback, use the identity captured before inference for a focused download.const policy = pageFeedbackCallPolicy(name, args || {}, run?.modelState, run?.latestPage);+ if (run && policy.kind === 'media_resolve') return this._resolvePageFeedbackMedia(tabId, policy.target);const targetValidated = !!run && run.validatedTargets?.get(`${name}:${JSON.stringify(args)}`) === run.revision;const allowPassiveRebase = targetValidated || (policy.kind === 'navigate'&& !!run && this._pageFeedbackStateCurrent(tabId, run, run.modelState));diff --git a/src/chrome/src/agent/social-media-downloader.js b/src/chrome/src/agent/social-media-downloader.jsindex 0f8d3bda3..fe9e17f37 100644--- a/src/chrome/src/agent/social-media-downloader.js+++ b/src/chrome/src/agent/social-media-downloader.js@@ -612,6 +612,61 @@ window.SocialMediaDownloader = (() => {return [...urls];};+ const paintCarrierVisible = (carrier, backingImage) => {+ if (!isVisibleElement(carrier)) return false;+ for (let node = carrier; node; node = node.parentElement) {+ const style = window.getComputedStyle(node);+ if (node.inert || node.hasAttribute('inert') || style.display === 'none'+ || ['hidden', 'collapse'].includes(style.visibility) || Number(style.opacity) === 0+ || style.contentVisibility === 'hidden') return false;+ }+ if (typeof document.elementFromPoint !== 'function') return false;+ const rect = carrier.getBoundingClientRect(), vp = viewportSize();+ const left = Math.max(0, rect.left), top = Math.max(0, rect.top);+ const right = Math.min(vp.w, rect.right), bottom = Math.min(vp.h, rect.bottom);+ if (right <= left || bottom <= top) return false;+ const dx = Math.min(2, (right - left) / 4), dy = Math.min(2, (bottom - top) / 4);+ const points = [[(left + right) / 2, (top + bottom) / 2], [left + dx, top + dy],+ [right - dx, top + dy], [left + dx, bottom - dy], [right - dx, bottom - dy]];+ return points.some(([x, y]) => {+ const hit = document.elementFromPoint(x, y);+ if (hit === carrier || hit === backingImage) return true;+ // React Native's paint layer can disable pointer events. Accept only its+ // immediate shared wrapper with no unrelated rendered child surface.+ const wrapper = carrier.parentElement;+ return hit === wrapper && window.getComputedStyle(carrier).pointerEvents === 'none'+ && wrapper.contains(backingImage)+ && [...wrapper.children].every(child => child === carrier || child === backingImage);+ });+ };++ const matchingPaintCarrier = el => {+ // React Native Web paints an Image as a background and leaves its backing+ // IMG transparent. Accept only the same asset, in the same rendered box,+ // on the immediate parent or a direct sibling. An arbitrary hidden IMG+ // cannot nominate an unrelated ancestor's background as visible media.+ if (el?.tagName !== 'IMG' || !el.parentElement || !window.getComputedStyle) return null;+ const style = window.getComputedStyle(el);+ if (Number(style.opacity) !== 0 || style.display === 'none' || style.visibility === 'hidden') return null;+ const imageRect = el.getBoundingClientRect();+ if (imageRect.width < 24 || imageRect.height < 24) return null;+ const assets = directMediaUrls(el);+ const carriers = [el.parentElement, ...el.parentElement.children].filter(node => node !== el);+ const matches = [];+ for (const carrier of carriers) {+ if (!paintCarrierVisible(carrier, el)) continue;+ const background = window.getComputedStyle(carrier).backgroundImage.trim();+ const match = /^url\((?:"([^"\\]*)"|'([^'\\]*)'|([^"'()\\]*))\)$/.exec(background);+ if (!match) continue;+ const url = absoluteUrl(match[1] ?? match[2] ?? match[3]);+ if (!isMediaUrl(url) || !assets.includes(url)) continue;+ const rect = carrier.getBoundingClientRect();+ if (['left', 'top', 'width', 'height'].some(key => Math.abs(rect[key] - imageRect[key]) > 1)) continue;+ matches.push({ element: carrier, url });+ }+ return matches.length === 1 ? matches[0] : null;+ };+const mediaElementsIn = root => {if (!root) return [];const out = [];@@ -622,8 +677,7 @@ window.SocialMediaDownloader = (() => {return out;};- const mediaElementScore = el => {- const box = mediaBoxElement(el);+ const mediaElementScore = (el, box = mediaBoxElement(el)) => {if (!box || !box.getBoundingClientRect) return -Infinity;const rect = box.getBoundingClientRect();const area = visibleArea(rect);@@ -640,26 +694,30 @@ window.SocialMediaDownloader = (() => {return score;};- const bestFocusedMedia = (roots, excluded) => {+ const bestFocusedMedia = (roots, excluded, { target = 'auto', allowPaintCarrier = false } = {}) => {const candidates = [];for (const root of roots || []) {if (!root || isExcluded(root, excluded) || !isVisibleElement(root)) continue;for (const el of mediaElementsIn(root)) {- const box = mediaBoxElement(el);+ if (target === 'image' && el.tagName !== 'IMG' && !(el.tagName === 'SOURCE' && el.closest('picture'))) continue;+ if (target === 'video' && el.tagName !== 'VIDEO' && !(el.tagName === 'SOURCE' && el.closest('video'))) continue;+ const paint = allowPaintCarrier ? matchingPaintCarrier(el) : null;+ const box = paint?.element || mediaBoxElement(el);if (!box || isExcluded(box, excluded) || !isVisibleElement(box)) continue;const rect = box.getBoundingClientRect();const intrinsicW = el.naturalWidth || el.videoWidth || el.width || rect.width || 0;const intrinsicH = el.naturalHeight || el.videoHeight || el.height || rect.height || 0;if (intrinsicW < 120 || intrinsicH < 120) continue;- const urls = directMediaUrls(el);+ const rawUrls = directMediaUrls(el);+ const urls = filterUrlsForTarget(paint ? [paint.url] : rawUrls, target);if (!urls.length) continue;- candidates.push({ el, root, urls, score: mediaElementScore(el) });+ candidates.push({ el, root, box, paint, urls, rawUrls, score: mediaElementScore(el, box) });}}candidates.sort((a, b) => b.score - a.score);const best = candidates[0];if (!best) return null;- const rival = candidates.find(candidate => mediaBoxElement(candidate.el) !== mediaBoxElement(best.el));+ const rival = candidates.find(candidate => candidate.box !== best.box);return { ...best, ambiguous: !!rival && Math.abs(best.score - rival.score) < 1 };};@@ -674,18 +732,18 @@ window.SocialMediaDownloader = (() => {return roots;};- const collectFocusedMedia = (profile, excluded) => {+ const collectFocusedMedia = (profile, excluded, options) => {const dialogRoots = queryVisibleRoots('dialog[open], [aria-modal="true"], [role="dialog"], ' +'[data-pagelet*="MediaViewer" i], [data-testid*="lightbox" i]');- let best = bestFocusedMedia(dialogRoots, excluded);+ let best = bestFocusedMedia(dialogRoots, excluded, options);if (best) return { ...best, scope: 'dialog' };- best = bestFocusedMedia(queryVisibleRoots(profile.focusSel), excluded);+ best = bestFocusedMedia(queryVisibleRoots(profile.focusSel), excluded, options);if (best) return { ...best, scope: 'main' };- best = bestFocusedMedia([document.body || document.documentElement], excluded);+ best = bestFocusedMedia([document.body || document.documentElement], excluded, options);return best ? { ...best, scope: 'document' } : null;};@@ -1510,13 +1568,13 @@ window.SocialMediaDownloader = (() => {// ---------- Main extraction entry point ----------// mode: 'auto' | 'main' | 'all'- const collect = (mode = 'auto') => {+ const collect = (mode = 'auto', focusOptions = undefined) => {const profile = activeProfile();const excluded = buildExclusionSet(profile);const bindingSources = [];const focusedMedia = mode === 'auto'- ? collectFocusedMedia(profile, excluded)+ ? collectFocusedMedia(profile, excluded, focusOptions): null;const focusedUrls = focusedMedia?.urls || [];let useMain;@@ -1539,7 +1597,8 @@ window.SocialMediaDownloader = (() => {if (focusedUrls.length) {urls = focusedUrls;sourceMode = 'focused';- bindingSources.push({ root: focusedMedia.root, node: focusedMedia.el, urls: focusedUrls });+ bindingSources.push({ root: focusedMedia.root, node: focusedMedia.el, urls: focusedUrls,+ rawUrls: focusedMedia.rawUrls, paint: focusedMedia.paint, box: focusedMedia.box });} else if (useGallery) {const galleryUrls = [];const galleryEls = document.querySelectorAll(profile.gallerySel);@@ -1645,8 +1704,8 @@ window.SocialMediaDownloader = (() => {return result;};- const collectBoundMedia = mode => {- const focused = collect('auto');+ const collectBoundMedia = (mode, target) => {+ const focused = collect('auto', { target, allowPaintCarrier: true });// Main-mode selectors may include a timeline behind an open photo modal.// A bound single-media call uses the same focused selection as its capture;// unbound main/bulk calls keep their existing collection behavior.@@ -1654,24 +1713,30 @@ window.SocialMediaDownloader = (() => {return collect(mode);};- const getMediaBinding = ({ mode = 'main', target = 'auto', all = false, limit = 1 } = {}) => {- if (all || !['main', 'auto'].includes(mode) || Number(limit) !== 1) return null;+ const mediaBindingDetails = ({ mode = 'main', target = 'auto', all = false, limit = 1 } = {}) => {+ const unavailable = (reason, candidateCount = 0, sourceCount = 0, paintCarrierCount = 0) => ({+ binding: null, diagnostics: { status: 'unavailable', reason, candidateCount, sourceCount, paintCarrierCount },+ });+ if (all || !['main', 'auto'].includes(mode) || Number(limit) !== 1) return unavailable('unsupported_scope');if (target === 'media') target = 'auto';- if (!['auto', 'image', 'video'].includes(target)) return null;- const collection = collectBoundMedia(mode);- if (collectionFocusAmbiguity.get(collection)) return null;- const selected = filterUrlsForTarget(collection.urls, target).slice(0, 1);- if (selected.length !== 1 || !['main', 'focused'].includes(collection.mode)) return null;+ if (!['auto', 'image', 'video'].includes(target)) return unavailable('unsupported_target');+ const collection = collectBoundMedia(mode, target);+ const candidates = filterUrlsForTarget(collection.urls, target);+ if (collectionFocusAmbiguity.get(collection)) return unavailable('ambiguous_media', candidates.length);+ const selected = candidates.slice(0, 1);+ if (selected.length !== 1) return unavailable('no_matching_media', candidates.length);+ if (!['main', 'focused'].includes(collection.mode)) return unavailable('unsupported_scope', candidates.length);const sources = (collectionBindingSources.get(collection) || []).filter(source =>preferHighQuality(source.urls).some(url => selected.includes(url)));// Do not retain downloads inferred from a URL without a live, identifiable// media source, or a page's arbitrary main-container fallback.- if (!sources.length || sources.some(source => !source.node || !source.root)- || !sources.some(source => ['IMG', 'VIDEO', 'SOURCE'].includes(source.node.tagName)- && isVisibleElement(mediaBoxElement(source.node)))) return null;+ if (!sources.length || sources.some(source => !source.node || !source.root)) return unavailable('source_unbound', candidates.length, sources.length);+ const sourceVisible = source => isVisibleElement(source.box || mediaBoxElement(source.node));+ if (!sources.some(source => ['IMG', 'VIDEO', 'SOURCE'].includes(source.node.tagName)+ && sourceVisible(source))) return unavailable('no_visible_media', candidates.length, sources.length);const state = mediaBindingState();const focusScope = collectionFocusScopes.get(collection);- return {+ const binding = {schema: 1, documentToken: state.token, pageUrl: location.href,site: collection.profile.name, sourceMode: collection.mode,focused: collection.mode === 'focused' && ['dialog', 'main'].includes(focusScope),@@ -1682,15 +1747,37 @@ window.SocialMediaDownloader = (() => {// while the browser is loading a newly assigned src/srcset.sources: sources.map(source => ({ root: mediaNodeIdentity(source.root), node: mediaNodeIdentity(source.node),container: mediaNodeIdentity(mediaBoxElement(source.node)?.parentElement),- assets: source.urls, visible: ['IMG', 'VIDEO', 'SOURCE'].includes(source.node.tagName)- ? isVisibleElement(mediaBoxElement(source.node)) : null })),+ assets: source.rawUrls || source.urls,+ paintCarrier: source.paint ? { node: mediaNodeIdentity(source.paint.element), url: source.paint.url } : null,+ visible: ['IMG', 'VIDEO', 'SOURCE'].includes(source.node.tagName) ? sourceVisible(source) : null })),};+ return { binding, diagnostics: { status: 'bound', reason: 'bound', candidateCount: candidates.length,+ sourceCount: sources.length, paintCarrierCount: sources.filter(source => source.paint).length } };+ };+ const getMediaBinding = options => mediaBindingDetails(options).binding;+ const getMediaBindingDiagnostics = options => {+ try { return mediaBindingDetails(options).diagnostics; }+ catch { return { status: 'unavailable', reason: 'capture_error', candidateCount: 0, sourceCount: 0, paintCarrierCount: 0 }; }+ };++ const sameBindingValue = (left, right) => {+ if (Object.is(left, right)) return true;+ if (!left || !right || typeof left !== 'object' || typeof right !== 'object') return false;+ const leftArray = Array.isArray(left), rightArray = Array.isArray(right);+ if (leftArray !== rightArray || (leftArray && left.length !== right.length)) return false;+ if (!leftArray && (Object.prototype.toString.call(left) !== '[object Object]'+ || Object.prototype.toString.call(right) !== '[object Object]')) return false;+ const leftKeys = Object.keys(left), rightKeys = Object.keys(right);+ return leftKeys.length === rightKeys.length && leftKeys.every(key => Object.prototype.hasOwnProperty.call(right, key)+ && sameBindingValue(left[key], right[key]));};const mediaBindingMatches = (expected, options) => {if (!expected || expected.schema !== 1) return false;const current = getMediaBinding(options);- return current !== null && JSON.stringify(current) === JSON.stringify(expected);+ // Extension transports can reorder object keys recursively. Arrays, key+ // sets, primitive types and every value still bind the exact same resource.+ return current !== null && sameBindingValue(current, expected);};const changedMediaBinding = () => ({success: false, noDispatch: true, dispatched: false, pageFeedbackPending: true,@@ -1843,7 +1930,7 @@ window.SocialMediaDownloader = (() => {const urls = all? await scrollAndCollect({ maxScrolls, scrollDelay, settleDelay,mode: mode === 'auto' ? 'all' : mode })- : (bindingGuard ? collectBoundMedia(mode).urls : list(mode));+ : (bindingGuard ? collectBoundMedia(mode, target).urls : list(mode));const eligibleUrls = filterUrlsForTarget(urls, target);const selected = eligibleUrls.slice(0, limit);console.log(`[SMD] downloading ${selected.length} of ${eligibleUrls.length} target-matching URLs`);@@ -1897,7 +1984,7 @@ window.SocialMediaDownloader = (() => {};return {- run, single, list, scrollAndCollect, getMediaBinding,+ run, single, list, scrollAndCollect, getMediaBinding, getMediaBindingDiagnostics,// MSE recorder (v4). Arm BEFORE the player loads:// SocialMediaDownloader.armMseRecorder()// <reload the page>diff --git a/src/chrome/src/content/page-monitor.js b/src/chrome/src/content/page-monitor.jsindex 0c3c1bd3c..498f848b1 100644--- a/src/chrome/src/content/page-monitor.js+++ b/src/chrome/src/content/page-monitor.js@@ -1222,7 +1222,10 @@try {const state = await api.runtime.sendMessage({ target: 'background', action: 'get_page_monitor_state', documentToken,frameName: compact(window.name.slice(0, 256), 256) });- if (generation === requestGeneration && !disposed) start(state);+ if (generation === requestGeneration && !disposed) {+ if (state?.active) start(state);+ else stop();+ }} catch { stop(); }}function prepare(params) {@@ -1397,7 +1400,10 @@const onMessage = (msg, _sender, respond) => {if (disposed || msg?.target !== 'content') return;if (msg.action === 'page_monitor_state') {- if (msg.active) { void requestState().then(() => respond({ ready: true })); return true; }+ if (msg.active) {+ void requestState().then(() => respond({ ready: true, active, documentToken, runToken: active ? runToken : '' }));+ return true;+ }else if (!msg.runToken || msg.runToken === runToken) { requestGeneration++; stop(); respond({ ready: true }); }} else if (msg.action === 'page_monitor_prepare') { prepare(msg.params || {}); respond({ ready: true }); }else if (msg.action === 'page_monitor_resize_begin') { respond({ ready: beginAgentResize(msg.params || {}) }); }diff --git a/src/firefox/src/agent/media-download-binding.js b/src/firefox/src/agent/media-download-binding.jsindex 9f502db0f..56cecbe93 100644--- a/src/firefox/src/agent/media-download-binding.js+++ b/src/firefox/src/agent/media-download-binding.js@@ -9,3 +9,27 @@ export function captureMediaDownloadBindingInPage(options = null) {}return bindings;}++/** Read-only capture; failures are observations, never evidence of user activity. */+export function captureMediaDownloadStateInPage() {+ const downloader = globalThis.window?.SocialMediaDownloader;+ const bindings = {}, diagnostics = {};+ if (typeof downloader?.getMediaBinding !== 'function') {+ return { bindings, diagnostics, status: 'library_unavailable' };+ }+ for (const target of ['auto', 'image', 'video']) {+ const options = { mode: 'main', target, limit: 1 };+ try {+ bindings[target] = downloader.getMediaBinding(options);+ diagnostics[target] = bindings[target] ? { status: 'bound', reason: 'verified_resource',+ candidateCount: bindings[target].candidates.length, sourceCount: bindings[target].sources.length,+ paintCarrierCount: bindings[target].sources.filter(source => source.paintCarrier).length }+ : typeof downloader.getMediaBindingDiagnostics === 'function' ? downloader.getMediaBindingDiagnostics(options)+ : { status: 'unavailable', reason: 'no_verified_media' };+ } catch {+ bindings[target] = null;+ diagnostics[target] = { status: 'unavailable', reason: 'capture_failed' };+ }+ }+ return { bindings, diagnostics, status: Object.values(bindings).some(Boolean) ? 'ready' : 'unavailable' };+}diff --git a/src/firefox/src/agent/page-feedback-policy.js b/src/firefox/src/agent/page-feedback-policy.jsindex 65837bdb0..074dbdf16 100644--- a/src/firefox/src/agent/page-feedback-policy.js+++ b/src/firefox/src/agent/page-feedback-policy.js@@ -78,12 +78,18 @@ export function pageFeedbackCallPolicy(name, args = {}, state = {}, currentPageif (name === 'download_public_media' && (httpUrl(args.url) || httpUrl(state.url))) return { kind: 'independent' };if (name === 'navigate' && httpUrl(args.url)) return { kind: 'navigate' };if (name === 'done' && args.outcome === 'success' && currentPage?.success !== false) return { kind: 'observe', terminal: true };- if (name === 'download_social_media' && args.strategy !== 'vision' && !args.scroll && args.mode !== 'all'- && (!args.limit || args.limit === 1)) {+ if (name === 'download_social_media' && !args.scroll && !args.all+ && [undefined, 'main', 'auto'].includes(args.mode)+ && [undefined, 'dom', 'auto'].includes(args.strategy)+ && (args.limit == null || args.limit === 1)) {const target = !args.target || args.target === 'media' ? 'auto' : args.target;- const binding = state.mediaBindings?.[target];- if (binding && (args.mode === 'main' || ((!args.mode || args.mode === 'auto') && binding.focused))) {- return { kind: 'media', binding, target };+ if (['auto', 'image', 'video'].includes(target)) {+ const binding = state.mediaBindings?.[target];+ if (binding && (args.mode === 'main' || binding.focused)) return { kind: 'media', binding, target };+ // An unavailable optional precapture is not a changed page. Dispatch a+ // read-only resolver result so the model can inspect actual media sources;+ // this policy never permits an unbound fetch/save or a fallback mutation.+ return { kind: 'media_resolve', target, reason: binding ? 'focus_unverified' : 'media_binding_missing' };}}if (!AX_ACTIONS.has(name) || !currentPage || events.some(event => /^(?:form|input|textarea|select|option)(?:$|[\s#.])/.test(event.target || ''))) return { kind: 'unsafe' };diff --git a/src/firefox/src/agent/page-feedback.js b/src/firefox/src/agent/page-feedback.jsindex 04b170c5d..82fac5c3a 100644--- a/src/firefox/src/agent/page-feedback.js+++ b/src/firefox/src/agent/page-feedback.js@@ -1,6 +1,6 @@import { canRetainPageFeedbackCalls, isPassivePageFeedback, pageFeedbackCallPolicy } from './page-feedback-policy.js';import { accountFeedbackSupersession, resetFeedbackSupersession } from './page-feedback-recovery.js';-import { captureMediaDownloadBindingInPage } from './media-download-binding.js';+import { captureMediaDownloadStateInPage } from './media-download-binding.js';import * as trace from '../trace/recorder.js';/** Run-owned browser observations. Keep the Firefox copy byte-identical. */@@ -12,6 +12,42 @@ const clean = (value, limit = 240) => String(value ?? '').replace(/[\u0000-\u001const token = () => globalThis.crypto.randomUUID();const apiFor = () => globalThis.browser || globalThis.chrome;+async function captureMediaState(api, tabId) {+ let stage = 'injection_failed';+ try {+ let result;+ if (typeof api.tabs.executeScript === 'function') {+ await api.tabs.executeScript(tabId, { file: 'src/agent/social-media-downloader.js' });+ stage = 'capture_failed';+ result = (await api.tabs.executeScript(tabId, { code: `(${captureMediaDownloadStateInPage.toString()})()` }))?.[0];+ } else {+ await api.scripting.executeScript({ target: { tabId }, world: 'MAIN', files: ['src/agent/social-media-downloader.js'] });+ stage = 'capture_failed';+ result = (await api.scripting.executeScript({ target: { tabId }, world: 'MAIN', func: captureMediaDownloadStateInPage }))?.[0]?.result;+ }+ return result || { bindings: {}, diagnostics: {}, status: 'capture_failed' };+ } catch {+ return { bindings: {}, diagnostics: {}, status: stage };+ }+}++function mediaCaptureMetadata(capture) {+ const statuses = new Set(['ready', 'unavailable', 'library_unavailable', 'injection_failed', 'capture_failed', 'context_changed', 'context_unverified']);+ const reasons = new Set(['verified_resource', 'no_verified_media', 'capture_failed', 'unsupported_request',+ 'ambiguous_media', 'no_visible_media', 'no_live_source', 'no_matching_media', 'no_single_media',+ 'unsupported_target', 'unsupported_scope', 'source_unbound', 'capture_error', 'bound', 'media_unavailable', 'focus_unverified']);+ const targets = {};+ for (const target of ['auto', 'image', 'video']) {+ const diagnostic = capture?.diagnostics?.[target];+ targets[target] = { status: capture?.bindings?.[target] ? 'bound' : 'unavailable',+ reason: reasons.has(diagnostic?.reason) ? diagnostic.reason : 'no_verified_media' };+ for (const key of ['candidateCount', 'sourceCount', 'paintCarrierCount']) {+ if (Number.isSafeInteger(diagnostic?.[key])) targets[target][key] = Math.max(0, Math.min(128, diagnostic[key]));+ }+ }+ return { status: statuses.has(capture?.status) ? capture.status : 'unavailable', targets };+}+function clearGestureLease(run, frameId) {const lease = run.gestureLeases?.get(frameId);if (lease) clearTimeout(lease.timer);@@ -438,24 +474,56 @@ export const pageFeedbackMethods = {// The downloader is lazy-loaded. Capture its private document/node/asset// binding before inference, never from model arguments or page instructions.if (/^https?:\/\/(?:[^/]+\.)?(?:x\.com|twitter\.com|instagram\.com|youtube\.com|youtu\.be|tiktok\.com|facebook\.com|reddit\.com)\//i.test(url)) {+ let capture = await captureMediaState(apiFor(), tabId);+ // Injection is asynchronous. Never attach a new document's media to the+ // URL/document/frame/steering snapshot sampled before that injection.+ let contextVerified = false, contextStatus = 'context_unverified';try {const api = apiFor();- if (typeof api.tabs.executeScript === 'function') {- // Firefox MV2 downloads in the isolated extension world. Capture in- // that same world so its private node/document identities match.- await api.tabs.executeScript(tabId, { file: 'src/agent/social-media-downloader.js' });- const results = await api.tabs.executeScript(tabId, { code: `(${captureMediaDownloadBindingInPage.toString()})()` });- state.mediaBindings = results?.[0] || {};- } else {- await api.scripting.executeScript({ target: { tabId }, world: 'MAIN', files: ['src/agent/social-media-downloader.js'] });- const results = await api.scripting.executeScript({ target: { tabId }, world: 'MAIN', func: captureMediaDownloadBindingInPage });- state.mediaBindings = results?.[0]?.result || {};+ let monitorVerified = false;+ if (!documentId && state.frames.get(0)?.token) {+ // Firefox versions without browser document IDs must round-trip the+ // current content monitor registration, rather than trust a cached+ // token that could belong to a just-reloaded same-URL document.+ const response = await api.tabs.sendMessage(tabId, { target: 'content', action: 'page_monitor_state', active: true }, { frameId: 0 });+ monitorVerified = response?.ready === true && response.active === true+ && response.documentToken === state.frames.get(0).token && response.runToken === run.token;}- } catch { /* No binding means a stale media response cannot be retained. */ }+ const [tab, frames] = await Promise.all([api.tabs.get(tabId), api.webNavigation?.getAllFrames?.({ tabId })]);+ const currentDocument = frames?.find(frame => frame.frameId === 0)?.documentId;+ const mainFrame = state.frames.get(0);+ const identified = documentId ? currentDocument === documentId : !!mainFrame?.token && monitorVerified;+ contextVerified = identified && tab.url === url+ && this._pageFeedbackStateCurrent(tabId, run, state, url)+ && Object.values(capture.bindings || {}).every(binding => !binding || binding.pageUrl === url);+ contextStatus = contextVerified ? 'ready' : !documentId && !identified ? 'context_unverified' : 'context_changed';+ } catch { /* Failed identity verification permits observation only. */ }+ if (!contextVerified) capture = { bindings: {}, diagnostics: {}, status: contextStatus };+ state.mediaBindings = capture.bindings || {};+ state.mediaCapture = mediaCaptureMetadata(capture);+ const runId = this.currentRunId?.get(tabId);+ if (runId) trace.recordNote(runId, 0, 'media_binding_capture', state.mediaCapture);}if (this._pageFeedbackRuns?.get(tabId) === run) { run.modelState = state; run.validatedTargets = new Map(); }},+ async _resolvePageFeedbackMedia(tabId, target) {+ const capture = await captureMediaState(apiFor(), tabId);+ const diagnostic = mediaCaptureMetadata(capture);+ const binding = capture.bindings?.[target];+ // This is an observation only. A later model turn must choose a download+ // with a binding captured before that inference or an explicit observed URL.+ return { success: false, dispatched: false, noDispatch: true,+ errorCode: 'media_binding_unavailable', mediaResolution: binding ? 'resolved' : 'unavailable',+ ...(binding ? { currentCandidates: binding.candidates.map(({ url, type }) => ({ url, type })) } : {}),+ bindingDiagnostic: diagnostic.targets[target], captureStatus: diagnostic.status,+ error: 'The current media could not be verified before inference. No download was attempted. '+ + (target === 'image' ? 'Inspect current image sources with extract_data({type:"images"})'+ : 'Inspect current media sources with read_page_source or inspect_network_requests')+ + ' and download the intended explicit URL with download_files, or retry this tool after a unique focused resource is verified. '+ + 'Do not repeat an unresolved download.' };+ },+_pageFeedbackStateCurrent(tabId, run, state, url = run.url) {return !!state && state.token === run.token && state.url === url&& state.steeringRevision === (this._steeringRuns?.get(tabId)?.acceptedIds.size || 0)@@ -634,6 +702,14 @@ export const pageFeedbackMethods = {if (runId) trace.recordNote(runId, 0, 'page_feedback_decision', { stage, disposition: responseToolCalls === null ? 'observed' : accepted ? 'retained' : 'superseded',reason: sameDocument && isPassivePageFeedback(events) ? 'passive_dom' : 'intervention_or_document',refreshMs: Date.now() - started, streak: run.passiveSupersessionStreak || 0,+ toolPolicies: (responseToolCalls || []).flatMap(call => {+ try {+ const name = call.function?.name;+ if (!/^[a-z][a-z0-9_]{0,63}$/.test(name || '')) return [];+ const policy = pageFeedbackCallPolicy(name, JSON.parse(call.function.arguments), state, page, events);+ return [{ name, kind: policy.kind, ...(policy.reason ? { reason: policy.reason } : {}) }];+ } catch { return []; }+ }).slice(0, 16),toolNames: (responseToolCalls || []).map(call => call.function?.name).filter(name => /^[a-z][a-z0-9_]{0,63}$/.test(name || '')).slice(0, 16) });this._persist(tabId);return !accepted;@@ -651,6 +727,7 @@ export function installPageFeedback(Agent) {// A model cannot supply the private expected binding. Even without queued// feedback, use the identity captured before inference for a focused download.const policy = pageFeedbackCallPolicy(name, args || {}, run?.modelState, run?.latestPage);+ if (run && policy.kind === 'media_resolve') return this._resolvePageFeedbackMedia(tabId, policy.target);const targetValidated = !!run && run.validatedTargets?.get(`${name}:${JSON.stringify(args)}`) === run.revision;const allowPassiveRebase = targetValidated || (policy.kind === 'navigate'&& !!run && this._pageFeedbackStateCurrent(tabId, run, run.modelState));diff --git a/src/firefox/src/agent/social-media-downloader.js b/src/firefox/src/agent/social-media-downloader.jsindex 0f8d3bda3..fe9e17f37 100644--- a/src/firefox/src/agent/social-media-downloader.js+++ b/src/firefox/src/agent/social-media-downloader.js@@ -612,6 +612,61 @@ window.SocialMediaDownloader = (() => {return [...urls];};+ const paintCarrierVisible = (carrier, backingImage) => {+ if (!isVisibleElement(carrier)) return false;+ for (let node = carrier; node; node = node.parentElement) {+ const style = window.getComputedStyle(node);+ if (node.inert || node.hasAttribute('inert') || style.display === 'none'+ || ['hidden', 'collapse'].includes(style.visibility) || Number(style.opacity) === 0+ || style.contentVisibility === 'hidden') return false;+ }+ if (typeof document.elementFromPoint !== 'function') return false;+ const rect = carrier.getBoundingClientRect(), vp = viewportSize();+ const left = Math.max(0, rect.left), top = Math.max(0, rect.top);+ const right = Math.min(vp.w, rect.right), bottom = Math.min(vp.h, rect.bottom);+ if (right <= left || bottom <= top) return false;+ const dx = Math.min(2, (right - left) / 4), dy = Math.min(2, (bottom - top) / 4);+ const points = [[(left + right) / 2, (top + bottom) / 2], [left + dx, top + dy],+ [right - dx, top + dy], [left + dx, bottom - dy], [right - dx, bottom - dy]];+ return points.some(([x, y]) => {+ const hit = document.elementFromPoint(x, y);+ if (hit === carrier || hit === backingImage) return true;+ // React Native's paint layer can disable pointer events. Accept only its+ // immediate shared wrapper with no unrelated rendered child surface.+ const wrapper = carrier.parentElement;+ return hit === wrapper && window.getComputedStyle(carrier).pointerEvents === 'none'+ && wrapper.contains(backingImage)+ && [...wrapper.children].every(child => child === carrier || child === backingImage);+ });+ };++ const matchingPaintCarrier = el => {+ // React Native Web paints an Image as a background and leaves its backing+ // IMG transparent. Accept only the same asset, in the same rendered box,+ // on the immediate parent or a direct sibling. An arbitrary hidden IMG+ // cannot nominate an unrelated ancestor's background as visible media.+ if (el?.tagName !== 'IMG' || !el.parentElement || !window.getComputedStyle) return null;+ const style = window.getComputedStyle(el);+ if (Number(style.opacity) !== 0 || style.display === 'none' || style.visibility === 'hidden') return null;+ const imageRect = el.getBoundingClientRect();+ if (imageRect.width < 24 || imageRect.height < 24) return null;+ const assets = directMediaUrls(el);+ const carriers = [el.parentElement, ...el.parentElement.children].filter(node => node !== el);+ const matches = [];+ for (const carrier of carriers) {+ if (!paintCarrierVisible(carrier, el)) continue;+ const background = window.getComputedStyle(carrier).backgroundImage.trim();+ const match = /^url\((?:"([^"\\]*)"|'([^'\\]*)'|([^"'()\\]*))\)$/.exec(background);+ if (!match) continue;+ const url = absoluteUrl(match[1] ?? match[2] ?? match[3]);+ if (!isMediaUrl(url) || !assets.includes(url)) continue;+ const rect = carrier.getBoundingClientRect();+ if (['left', 'top', 'width', 'height'].some(key => Math.abs(rect[key] - imageRect[key]) > 1)) continue;+ matches.push({ element: carrier, url });+ }+ return matches.length === 1 ? matches[0] : null;+ };+const mediaElementsIn = root => {if (!root) return [];const out = [];@@ -622,8 +677,7 @@ window.SocialMediaDownloader = (() => {return out;};- const mediaElementScore = el => {- const box = mediaBoxElement(el);+ const mediaElementScore = (el, box = mediaBoxElement(el)) => {if (!box || !box.getBoundingClientRect) return -Infinity;const rect = box.getBoundingClientRect();const area = visibleArea(rect);@@ -640,26 +694,30 @@ window.SocialMediaDownloader = (() => {return score;};- const bestFocusedMedia = (roots, excluded) => {+ const bestFocusedMedia = (roots, excluded, { target = 'auto', allowPaintCarrier = false } = {}) => {const candidates = [];for (const root of roots || []) {if (!root || isExcluded(root, excluded) || !isVisibleElement(root)) continue;for (const el of mediaElementsIn(root)) {- const box = mediaBoxElement(el);+ if (target === 'image' && el.tagName !== 'IMG' && !(el.tagName === 'SOURCE' && el.closest('picture'))) continue;+ if (target === 'video' && el.tagName !== 'VIDEO' && !(el.tagName === 'SOURCE' && el.closest('video'))) continue;+ const paint = allowPaintCarrier ? matchingPaintCarrier(el) : null;+ const box = paint?.element || mediaBoxElement(el);if (!box || isExcluded(box, excluded) || !isVisibleElement(box)) continue;const rect = box.getBoundingClientRect();const intrinsicW = el.naturalWidth || el.videoWidth || el.width || rect.width || 0;const intrinsicH = el.naturalHeight || el.videoHeight || el.height || rect.height || 0;if (intrinsicW < 120 || intrinsicH < 120) continue;- const urls = directMediaUrls(el);+ const rawUrls = directMediaUrls(el);+ const urls = filterUrlsForTarget(paint ? [paint.url] : rawUrls, target);if (!urls.length) continue;- candidates.push({ el, root, urls, score: mediaElementScore(el) });+ candidates.push({ el, root, box, paint, urls, rawUrls, score: mediaElementScore(el, box) });}}candidates.sort((a, b) => b.score - a.score);const best = candidates[0];if (!best) return null;- const rival = candidates.find(candidate => mediaBoxElement(candidate.el) !== mediaBoxElement(best.el));+ const rival = candidates.find(candidate => candidate.box !== best.box);return { ...best, ambiguous: !!rival && Math.abs(best.score - rival.score) < 1 };};@@ -674,18 +732,18 @@ window.SocialMediaDownloader = (() => {return roots;};- const collectFocusedMedia = (profile, excluded) => {+ const collectFocusedMedia = (profile, excluded, options) => {const dialogRoots = queryVisibleRoots('dialog[open], [aria-modal="true"], [role="dialog"], ' +'[data-pagelet*="MediaViewer" i], [data-testid*="lightbox" i]');- let best = bestFocusedMedia(dialogRoots, excluded);+ let best = bestFocusedMedia(dialogRoots, excluded, options);if (best) return { ...best, scope: 'dialog' };- best = bestFocusedMedia(queryVisibleRoots(profile.focusSel), excluded);+ best = bestFocusedMedia(queryVisibleRoots(profile.focusSel), excluded, options);if (best) return { ...best, scope: 'main' };- best = bestFocusedMedia([document.body || document.documentElement], excluded);+ best = bestFocusedMedia([document.body || document.documentElement], excluded, options);return best ? { ...best, scope: 'document' } : null;};@@ -1510,13 +1568,13 @@ window.SocialMediaDownloader = (() => {// ---------- Main extraction entry point ----------// mode: 'auto' | 'main' | 'all'- const collect = (mode = 'auto') => {+ const collect = (mode = 'auto', focusOptions = undefined) => {const profile = activeProfile();const excluded = buildExclusionSet(profile);const bindingSources = [];const focusedMedia = mode === 'auto'- ? collectFocusedMedia(profile, excluded)+ ? collectFocusedMedia(profile, excluded, focusOptions): null;const focusedUrls = focusedMedia?.urls || [];let useMain;@@ -1539,7 +1597,8 @@ window.SocialMediaDownloader = (() => {if (focusedUrls.length) {urls = focusedUrls;sourceMode = 'focused';- bindingSources.push({ root: focusedMedia.root, node: focusedMedia.el, urls: focusedUrls });+ bindingSources.push({ root: focusedMedia.root, node: focusedMedia.el, urls: focusedUrls,+ rawUrls: focusedMedia.rawUrls, paint: focusedMedia.paint, box: focusedMedia.box });} else if (useGallery) {const galleryUrls = [];const galleryEls = document.querySelectorAll(profile.gallerySel);@@ -1645,8 +1704,8 @@ window.SocialMediaDownloader = (() => {return result;};- const collectBoundMedia = mode => {- const focused = collect('auto');+ const collectBoundMedia = (mode, target) => {+ const focused = collect('auto', { target, allowPaintCarrier: true });// Main-mode selectors may include a timeline behind an open photo modal.// A bound single-media call uses the same focused selection as its capture;// unbound main/bulk calls keep their existing collection behavior.@@ -1654,24 +1713,30 @@ window.SocialMediaDownloader = (() => {return collect(mode);};- const getMediaBinding = ({ mode = 'main', target = 'auto', all = false, limit = 1 } = {}) => {- if (all || !['main', 'auto'].includes(mode) || Number(limit) !== 1) return null;+ const mediaBindingDetails = ({ mode = 'main', target = 'auto', all = false, limit = 1 } = {}) => {+ const unavailable = (reason, candidateCount = 0, sourceCount = 0, paintCarrierCount = 0) => ({+ binding: null, diagnostics: { status: 'unavailable', reason, candidateCount, sourceCount, paintCarrierCount },+ });+ if (all || !['main', 'auto'].includes(mode) || Number(limit) !== 1) return unavailable('unsupported_scope');if (target === 'media') target = 'auto';- if (!['auto', 'image', 'video'].includes(target)) return null;- const collection = collectBoundMedia(mode);- if (collectionFocusAmbiguity.get(collection)) return null;- const selected = filterUrlsForTarget(collection.urls, target).slice(0, 1);- if (selected.length !== 1 || !['main', 'focused'].includes(collection.mode)) return null;+ if (!['auto', 'image', 'video'].includes(target)) return unavailable('unsupported_target');+ const collection = collectBoundMedia(mode, target);+ const candidates = filterUrlsForTarget(collection.urls, target);+ if (collectionFocusAmbiguity.get(collection)) return unavailable('ambiguous_media', candidates.length);+ const selected = candidates.slice(0, 1);+ if (selected.length !== 1) return unavailable('no_matching_media', candidates.length);+ if (!['main', 'focused'].includes(collection.mode)) return unavailable('unsupported_scope', candidates.length);const sources = (collectionBindingSources.get(collection) || []).filter(source =>preferHighQuality(source.urls).some(url => selected.includes(url)));// Do not retain downloads inferred from a URL without a live, identifiable// media source, or a page's arbitrary main-container fallback.- if (!sources.length || sources.some(source => !source.node || !source.root)- || !sources.some(source => ['IMG', 'VIDEO', 'SOURCE'].includes(source.node.tagName)- && isVisibleElement(mediaBoxElement(source.node)))) return null;+ if (!sources.length || sources.some(source => !source.node || !source.root)) return unavailable('source_unbound', candidates.length, sources.length);+ const sourceVisible = source => isVisibleElement(source.box || mediaBoxElement(source.node));+ if (!sources.some(source => ['IMG', 'VIDEO', 'SOURCE'].includes(source.node.tagName)+ && sourceVisible(source))) return unavailable('no_visible_media', candidates.length, sources.length);const state = mediaBindingState();const focusScope = collectionFocusScopes.get(collection);- return {+ const binding = {schema: 1, documentToken: state.token, pageUrl: location.href,site: collection.profile.name, sourceMode: collection.mode,focused: collection.mode === 'focused' && ['dialog', 'main'].includes(focusScope),@@ -1682,15 +1747,37 @@ window.SocialMediaDownloader = (() => {// while the browser is loading a newly assigned src/srcset.sources: sources.map(source => ({ root: mediaNodeIdentity(source.root), node: mediaNodeIdentity(source.node),container: mediaNodeIdentity(mediaBoxElement(source.node)?.parentElement),- assets: source.urls, visible: ['IMG', 'VIDEO', 'SOURCE'].includes(source.node.tagName)- ? isVisibleElement(mediaBoxElement(source.node)) : null })),+ assets: source.rawUrls || source.urls,+ paintCarrier: source.paint ? { node: mediaNodeIdentity(source.paint.element), url: source.paint.url } : null,+ visible: ['IMG', 'VIDEO', 'SOURCE'].includes(source.node.tagName) ? sourceVisible(source) : null })),};+ return { binding, diagnostics: { status: 'bound', reason: 'bound', candidateCount: candidates.length,+ sourceCount: sources.length, paintCarrierCount: sources.filter(source => source.paint).length } };+ };+ const getMediaBinding = options => mediaBindingDetails(options).binding;+ const getMediaBindingDiagnostics = options => {+ try { return mediaBindingDetails(options).diagnostics; }+ catch { return { status: 'unavailable', reason: 'capture_error', candidateCount: 0, sourceCount: 0, paintCarrierCount: 0 }; }+ };++ const sameBindingValue = (left, right) => {+ if (Object.is(left, right)) return true;+ if (!left || !right || typeof left !== 'object' || typeof right !== 'object') return false;+ const leftArray = Array.isArray(left), rightArray = Array.isArray(right);+ if (leftArray !== rightArray || (leftArray && left.length !== right.length)) return false;+ if (!leftArray && (Object.prototype.toString.call(left) !== '[object Object]'+ || Object.prototype.toString.call(right) !== '[object Object]')) return false;+ const leftKeys = Object.keys(left), rightKeys = Object.keys(right);+ return leftKeys.length === rightKeys.length && leftKeys.every(key => Object.prototype.hasOwnProperty.call(right, key)+ && sameBindingValue(left[key], right[key]));};const mediaBindingMatches = (expected, options) => {if (!expected || expected.schema !== 1) return false;const current = getMediaBinding(options);- return current !== null && JSON.stringify(current) === JSON.stringify(expected);+ // Extension transports can reorder object keys recursively. Arrays, key+ // sets, primitive types and every value still bind the exact same resource.+ return current !== null && sameBindingValue(current, expected);};const changedMediaBinding = () => ({success: false, noDispatch: true, dispatched: false, pageFeedbackPending: true,@@ -1843,7 +1930,7 @@ window.SocialMediaDownloader = (() => {const urls = all? await scrollAndCollect({ maxScrolls, scrollDelay, settleDelay,mode: mode === 'auto' ? 'all' : mode })- : (bindingGuard ? collectBoundMedia(mode).urls : list(mode));+ : (bindingGuard ? collectBoundMedia(mode, target).urls : list(mode));const eligibleUrls = filterUrlsForTarget(urls, target);const selected = eligibleUrls.slice(0, limit);console.log(`[SMD] downloading ${selected.length} of ${eligibleUrls.length} target-matching URLs`);@@ -1897,7 +1984,7 @@ window.SocialMediaDownloader = (() => {};return {- run, single, list, scrollAndCollect, getMediaBinding,+ run, single, list, scrollAndCollect, getMediaBinding, getMediaBindingDiagnostics,// MSE recorder (v4). Arm BEFORE the player loads:// SocialMediaDownloader.armMseRecorder()// <reload the page>diff --git a/src/firefox/src/content/page-monitor.js b/src/firefox/src/content/page-monitor.jsindex 0c3c1bd3c..498f848b1 100644--- a/src/firefox/src/content/page-monitor.js+++ b/src/firefox/src/content/page-monitor.js@@ -1222,7 +1222,10 @@try {const state = await api.runtime.sendMessage({ target: 'background', action: 'get_page_monitor_state', documentToken,frameName: compact(window.name.slice(0, 256), 256) });- if (generation === requestGeneration && !disposed) start(state);+ if (generation === requestGeneration && !disposed) {+ if (state?.active) start(state);+ else stop();+ }} catch { stop(); }}function prepare(params) {@@ -1397,7 +1400,10 @@const onMessage = (msg, _sender, respond) => {if (disposed || msg?.target !== 'content') return;if (msg.action === 'page_monitor_state') {- if (msg.active) { void requestState().then(() => respond({ ready: true })); return true; }+ if (msg.active) {+ void requestState().then(() => respond({ ready: true, active, documentToken, runToken: active ? runToken : '' }));+ return true;+ }else if (!msg.runToken || msg.runToken === runToken) { requestGeneration++; stop(); respond({ ready: true }); }} else if (msg.action === 'page_monitor_prepare') { prepare(msg.params || {}); respond({ ready: true }); }else if (msg.action === 'page_monitor_resize_begin') { respond({ ready: beginAgentResize(msg.params || {}) }); }diff --git a/test/media-download-binding.mjs b/test/media-download-binding.mjsindex 6c3ba560f..7cd16018f 100644--- a/test/media-download-binding.mjs+++ b/test/media-download-binding.mjs@@ -33,6 +33,38 @@ async function fixture(engine, build) {}for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {+ for (const mutation of ['none', 'nested_value', 'type', 'extra_key', 'array_order']) {+ test(`${build}: binding serialization key order is harmless while ${mutation} stays exact`, async () => {+ const { browser, page } = await fixture(engine, build);+ try {+ const result = await page.evaluate(async change => {+ if (change === 'array_order') document.getElementById('photo').setAttribute('data-src', 'https://pbs.twimg.com/media/other.jpg');+ const original = SocialMediaDownloader.getMediaBinding({ mode: 'main', target: 'image', limit: 1 });+ const reorder = value => Array.isArray(value) ? value.map(reorder)+ : value && typeof value === 'object' ? Object.fromEntries(Object.keys(value).sort().reverse()+ .map(key => [key, reorder(value[key])])) : value;+ const expectedMediaBinding = reorder(original);+ const keyOrderChanged = JSON.stringify(original) !== JSON.stringify(expectedMediaBinding);+ if (change === 'nested_value') expectedMediaBinding.sources[0].node = 'different-node';+ if (change === 'type') expectedMediaBinding.request.limit = '1';+ if (change === 'extra_key') expectedMediaBinding.sources[0].extra = true;+ if (change === 'array_order') expectedMediaBinding.sources[0].assets.reverse();+ return { keyOrderChanged, result: await SocialMediaDownloader.run({ mode: 'main', target: 'image', limit: 1,+ delayBetweenDownloads: 0, expectedMediaBinding }) };+ }, mutation);+ assert.equal(result.keyOrderChanged, true);+ if (mutation === 'none') {+ assert.equal(result.result.stats.completed, 1);+ assert.deepEqual(await page.evaluate(() => effects), { fetches: 1, clicks: 1, scrolls: 0 });+ } else {+ assert.equal(result.result.noDispatch, true);+ assert.equal(result.result.errorCode, 'media_binding_changed');+ assert.deepEqual(await page.evaluate(() => effects), { fetches: 0, clicks: 0, scrolls: 0 });+ }+ } finally { await browser.close(); }+ });+ }+test(`${build}: media binding survives counters and downloader reinjection, then downloads once`, async () => {const { browser, page, inject } = await fixture(engine, build);try {@@ -161,3 +193,178 @@ test('media download binding source stays identical across browsers', () => {fs.readFileSync(new URL(`../src/firefox/src/agent/${file}`, import.meta.url), 'utf8'));}});++// React Native Web paints the visible asset separately from its backing IMG.+{+ const asset = 'https://pbs.twimg.com/media/intended.jpg';+ const html = `<!doctype html><style>body{margin:0}[aria-modal=true]{position:fixed;inset:0;background:white}+ #paint{position:relative;width:500px;height:400px;background-size:cover;background-image:url("${asset}")}+ #photo{position:absolute;inset:0;width:100%;height:100%;opacity:0;z-index:-1}</style>+ <div role="dialog" aria-modal="true"><button id="likes">1575 Likes</button>+ <div data-testid="tweetPhoto"><div id="paint"><img id="photo" src="${asset}"></div></div></div>`;++ async function fixture(engine, build, shape = 'parent') {+ const browser = await engine.launch({ headless: true });+ const context = await browser.newContext();+ await context.route('https://x.com/**', route => route.fulfill({ contentType: 'text/html', body: html }));+ await context.route('https://pbs.twimg.com/**', route => route.fulfill({ contentType: 'image/svg+xml',+ body: '<svg xmlns="http://www.w3.org/2000/svg" width="800" height="600"><rect width="800" height="600" fill="red"/></svg>' }));+ const page = await context.newPage();+ await page.goto('https://x.com/account/status/123/photo/1');+ await page.waitForFunction(() => document.getElementById('photo').naturalWidth > 0);+ if (shape === 'sibling') await page.evaluate(() => {+ const image = document.getElementById('photo'), parent = image.parentElement;+ const paint = document.createElement('div'); paint.id = 'paint-sibling';+ paint.style.cssText = `position:absolute;inset:0;background-image:${getComputedStyle(parent).backgroundImage};background-size:cover`;+ parent.style.backgroundImage = 'none'; parent.appendChild(paint);+ });+ await page.addScriptTag({ content: fs.readFileSync(new URL(`../src/${build}/src/agent/social-media-downloader.js`, import.meta.url), 'utf8') });+ await page.evaluate(() => {+ window.effects = { fetches: [], clicks: 0, scrolls: 0 };+ window.fetch = async url => { effects.fetches.push(url); return { ok: true, blob: async () => new Blob(['photo']) }; };+ HTMLAnchorElement.prototype.click = () => effects.clicks++;+ window.scrollTo = () => effects.scrolls++;+ });+ return { browser, page };+ }++ for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {+ for (const phase of ['capture', 'dispatch']) for (const obstruction of ['ancestor_opacity', 'ancestor_display', 'ancestor_visibility', 'ancestor_inert', 'overlay']) {+ test(`${build}: painted media rejects ${obstruction} during ${phase}`, async () => {+ const { browser, page } = await fixture(engine, build);+ try {+ let binding;+ if (phase === 'dispatch') {+ binding = (await page.evaluate(captureMediaDownloadBindingInPage)).image;+ assert.ok(binding);+ }+ await page.evaluate(kind => {+ const media = document.querySelector('[data-testid=tweetPhoto]');+ if (kind === 'ancestor_opacity') media.style.opacity = '0';+ if (kind === 'ancestor_display') media.style.display = 'none';+ if (kind === 'ancestor_visibility') media.style.visibility = 'hidden';+ if (kind === 'ancestor_inert') media.inert = true;+ if (kind === 'overlay') {+ const overlay = document.createElement('div');+ overlay.style.cssText = 'position:fixed;inset:0;background:black;z-index:99999';+ document.body.appendChild(overlay);+ }+ }, obstruction);+ if (phase === 'capture') assert.equal((await page.evaluate(captureMediaDownloadBindingInPage)).image, null);+ else {+ const result = await page.evaluate(expectedMediaBinding => SocialMediaDownloader.run({+ mode: 'main', target: 'image', limit: 1, delayBetweenDownloads: 0, expectedMediaBinding,+ }), binding);+ assert.equal(result.noDispatch, true);+ assert.equal(result.errorCode, 'media_binding_changed');+ }+ assert.deepEqual(await page.evaluate(() => effects), { fetches: [], clicks: 0, scrolls: 0 });+ } finally { await browser.close(); }+ });+ }++ for (const shape of ['parent', 'sibling']) test(`${build}: transparent backing IMG binds and downloads its matching visible ${shape} paint`, async () => {+ const { browser, page } = await fixture(engine, build, shape);+ try {+ const binding = (await page.evaluate(captureMediaDownloadBindingInPage)).image;+ assert.ok(binding?.focused);+ assert.match(binding.candidates[0].url, /intended\.jpg/);+ assert.ok(binding.sources[0].paintCarrier?.node);+ assert.equal(binding.sources[0].paintCarrier.url, asset);+ const diagnostics = await page.evaluate(() => SocialMediaDownloader.getMediaBindingDiagnostics({ mode: 'main', target: 'image', limit: 1 }));+ assert.equal(diagnostics.status, 'bound');+ assert.equal(diagnostics.paintCarrierCount, 1);+ assert.doesNotMatch(JSON.stringify(diagnostics), /intended|pbs\.twimg|media_\d/);+ await page.evaluate(() => { document.getElementById('likes').textContent = '1591 Likes'; });+ const result = await page.evaluate(expectedMediaBinding => SocialMediaDownloader.run({+ mode: 'main', target: 'image', limit: 1, delayBetweenDownloads: 0, expectedMediaBinding,+ }), binding);+ assert.equal(result.stats.completed, 1);+ assert.deepEqual(await page.evaluate(() => effects), { fetches: [binding.candidates[0].url], clicks: 1, scrolls: 0 });+ } finally { await browser.close(); }+ });++ test(`${build}: noninteractive React Native paint accepts only its immediate image wrapper`, async () => {+ const { browser, page } = await fixture(engine, build, 'sibling');+ try {+ await page.evaluate(() => { document.getElementById('paint-sibling').style.pointerEvents = 'none'; });+ const binding = (await page.evaluate(captureMediaDownloadBindingInPage)).image;+ assert.ok(binding?.sources[0].paintCarrier);+ const result = await page.evaluate(expectedMediaBinding => SocialMediaDownloader.run({+ mode: 'main', target: 'image', limit: 1, delayBetweenDownloads: 0, expectedMediaBinding,+ }), binding);+ assert.equal(result.stats.completed, 1);+ } finally { await browser.close(); }+ });++ for (const change of ['background', 'carrier', 'image', 'hidden', 'geometry']) test(`${build}: changed ${change} paint binding cannot dispatch`, async () => {+ const { browser, page } = await fixture(engine, build);+ try {+ const binding = (await page.evaluate(captureMediaDownloadBindingInPage)).image;+ assert.ok(binding);+ await page.evaluate(kind => {+ const paint = document.getElementById('paint'), image = document.getElementById('photo');+ if (kind === 'background') paint.style.backgroundImage = 'url("https://pbs.twimg.com/media/other.jpg")';+ if (kind === 'carrier') {+ const replacement = paint.cloneNode(false); replacement.appendChild(image); paint.replaceWith(replacement);+ }+ if (kind === 'image') image.src = 'https://pbs.twimg.com/media/other.jpg';+ if (kind === 'hidden') paint.style.visibility = 'hidden';+ if (kind === 'geometry') image.style.width = '100px';+ }, change);+ const result = await page.evaluate(expectedMediaBinding => SocialMediaDownloader.run({+ mode: 'main', target: 'image', limit: 1, delayBetweenDownloads: 0, expectedMediaBinding,+ }), binding);+ assert.equal(result.noDispatch, true);+ assert.equal(result.errorCode, 'media_binding_changed');+ assert.deepEqual(await page.evaluate(() => effects), { fetches: [], clicks: 0, scrolls: 0 });+ } finally { await browser.close(); }+ });++ test(`${build}: unrelated visible background cannot make a transparent backing IMG bindable`, async () => {+ const { browser, page } = await fixture(engine, build);+ try {+ await page.evaluate(() => { document.getElementById('paint').style.backgroundImage = 'url("https://pbs.twimg.com/media/other.jpg")'; });+ assert.equal((await page.evaluate(captureMediaDownloadBindingInPage)).image, null);+ const diagnostics = await page.evaluate(() => SocialMediaDownloader.getMediaBindingDiagnostics({ target: 'image' }));+ assert.equal(diagnostics.status, 'unavailable');+ assert.equal(diagnostics.reason, 'no_visible_media');+ assert.doesNotMatch(JSON.stringify(diagnostics), /intended|other\.jpg|pbs\.twimg/);+ } finally { await browser.close(); }+ });++ test(`${build}: changing paint during fetch cannot save the obsolete resource`, async () => {+ const { browser, page } = await fixture(engine, build);+ try {+ const binding = (await page.evaluate(captureMediaDownloadBindingInPage)).image;+ const result = await page.evaluate(async expectedMediaBinding => {+ window.fetch = async url => {+ effects.fetches.push(url);+ document.getElementById('paint').style.backgroundImage = 'url("https://pbs.twimg.com/media/other.jpg")';+ return { ok: true, blob: async () => new Blob(['old image']) };+ };+ return SocialMediaDownloader.run({ mode: 'main', target: 'image', limit: 1, delayBetweenDownloads: 0, expectedMediaBinding });+ }, binding);+ assert.equal(result.noDispatch, true);+ assert.deepEqual(await page.evaluate(() => effects), { fetches: [binding.candidates[0].url], clicks: 0, scrolls: 0 });+ } finally { await browser.close(); }+ });++ test(`${build}: image binding excludes a larger focused video before scoring`, async () => {+ const { browser, page } = await fixture(engine, build);+ try {+ await page.evaluate(() => {+ const video = document.createElement('video');+ video.src = 'https://video.twimg.com/intended-video.mp4';+ video.style.cssText = 'position:fixed;left:550px;top:0;width:700px;height:700px';+ document.querySelector('[aria-modal=true]').appendChild(video);+ });+ const bindings = await page.evaluate(captureMediaDownloadBindingInPage);+ assert.match(bindings.image.candidates[0].url, /intended\.jpg/);+ assert.match(bindings.video.candidates[0].url, /intended-video\.mp4/);+ assert.equal(bindings.image.candidates[0].type, 'image');+ assert.equal(bindings.video.candidates[0].type, 'video');+ } finally { await browser.close(); }+ });+ }+}diff --git a/test/media-download-capture-integration.mjs b/test/media-download-capture-integration.mjsindex 71aee060f..f2554cb97 100644--- a/test/media-download-capture-integration.mjs+++ b/test/media-download-capture-integration.mjs@@ -1,11 +1,14 @@import assert from 'node:assert/strict';import fs from 'node:fs';import { test } from 'node:test';+import os from 'node:os';+import path from 'node:path';import { chromium, firefox } from 'playwright';const area = { get: async () => ({}), set: async () => {}, remove: async () => {} };const initialApi = { storage: { local: area, session: area },runtime: { getURL: value => `extension://test/${value}`, sendMessage: async () => ({}) },+ webNavigation: { getAllFrames: async ({ tabId }) => [{ frameId: 0, documentId: `synthetic-${tabId}`, url: 'https://x.com/account/status/123/photo/1' }] },tabs: { get: async id => ({ id, url: 'https://x.com/account/status/123/photo/1' }), sendMessage: async () => ({}) } };globalThis.chrome = initialApi;globalThis.browser = initialApi;@@ -33,7 +36,7 @@ async function fixture(build, engine, { conflictingScripting = false } = {}) {});const calls = [];const source = fs.readFileSync(new URL(`../src/${build}/src/agent/social-media-downloader.js`, import.meta.url), 'utf8');- const api = { storage: { local: area, session: area }, runtime: initialApi.runtime,+ const api = { storage: { local: area, session: area }, runtime: initialApi.runtime, webNavigation: initialApi.webNavigation,tabs: { get: async id => ({ id, url: page.url() }), sendMessage: async () => ({}) } };if (build === 'chrome') api.scripting = { async executeScript(request) {calls.push({ transport: 'scripting', world: request.world, file: request.files?.[0], func: !!request.func });@@ -121,3 +124,189 @@ for (const [build, Agent, engine] of variants) {} finally { await browser.close(); }});}++// Installed MV3 runtime: real chrome.scripting MAIN-world capture and dispatch,+// independent of the API mocks above. All page/media traffic is synthetic.+for (const change of ['counter', 'asset']) {+ test(`installed Chrome MV3: painted X photo ${change === 'counter' ? 'downloads across counter churn' : 'replacement fails before fetch'}`, async () => {+ const profile = fs.mkdtempSync(path.join(os.tmpdir(), 'webbrain-photo-mv3-'));+ const extension = path.resolve('src/chrome');+ let context;+ try {+ context = await chromium.launchPersistentContext(profile, { headless: true, channel: 'chromium',+ args: [`--disable-extensions-except=${extension}`, `--load-extension=${extension}`] });+ const worker = context.serviceWorkers()[0] || await context.waitForEvent('serviceworker');+ const extensionId = new URL(worker.url()).host;+ await context.route('https://x.com/**', route => route.fulfill({ contentType: 'text/html', body:+ `<!doctype html><style>body{margin:0}.paint{width:500px;height:400px;+ background-image:url("https://pbs.twimg.com/media/intended.jpg");background-size:cover}+ img{opacity:0;width:100%;height:100%}</style><div role="dialog" aria-modal="true">+ <button id="likes">1575 Likes</button><div data-testid="tweetPhoto" class="paint">+ <img id="photo" src="https://pbs.twimg.com/media/intended.jpg"></div></div>` }));+ await context.route('https://pbs.twimg.com/**', route => route.fulfill({ contentType: 'image/svg+xml',+ body: '<svg xmlns="http://www.w3.org/2000/svg" width="800" height="600"><rect width="800" height="600" fill="red"/></svg>' }));+ const photo = await context.newPage();+ await photo.goto('https://x.com/account/status/123/photo/1');+ await photo.waitForFunction(() => document.getElementById('photo').naturalWidth > 0);+ await photo.evaluate(() => {+ window.savedUrls = []; window.anchorClicks = 0;+ window.fetch = async url => { savedUrls.push(url); return { ok: true, blob: async () => new Blob(['photo']) }; };+ HTMLAnchorElement.prototype.click = () => anchorClicks++;+ });+ const ui = await context.newPage();+ await ui.goto(`chrome-extension://${extensionId}/src/ui/settings.html`);+ const captured = await ui.evaluate(async () => {+ const { Agent } = await import(chrome.runtime.getURL('src/agent/agent.js'));+ const provider = { name: 'synthetic capture test', model: 'test', supportsVision: false };+ const agent = new Agent({ getActive: () => provider, getProvider: () => provider, getVisionProvider: async () => null });+ agent._uncertainTextMutationBlock = async () => null;+ agent._richTextToolbarToolBlock = async () => null;+ agent._finalizeToolResultOnce = async (_tab, _name, _args, result) => result;+ agent._resolveVisionRoute = async () => { throw new Error('Bound media cannot use vision fallback'); };+ const tab = (await chrome.tabs.query({ url: 'https://x.com/account/status/123/photo/1' }))[0];+ const frames = await chrome.webNavigation.getAllFrames({ tabId: tab.id });+ const run = { token: 'synthetic-run', url: tab.url, documents: new Map(frames.map(frame => [frame.frameId, frame.documentId])),+ frames: new Map(), events: new Map(), revision: 0, gestures: new Set(), gestureLeases: new Map() };+ agent._pageFeedbackRuns = new Map([[tab.id, run]]);+ window.captureAgent = agent; window.captureTab = tab.id;+ await agent._capturePageFeedbackModelState(tab.id);+ return { binding: run.modelState.mediaBindings.image, diagnostic: run.modelState.mediaCapture,+ legacyAvailable: typeof chrome.tabs.executeScript, manifest: chrome.runtime.getManifest().manifest_version };+ });+ assert.equal(captured.manifest, 3);+ assert.equal(captured.legacyAvailable, 'undefined');+ assert.equal(captured.diagnostic.targets.image.status, 'bound');+ assert.equal(captured.diagnostic.targets.image.paintCarrierCount, 1);+ assert.equal(captured.binding.focused, true);+ assert.match(captured.binding.candidates[0].url, /intended/);+ assert.ok(captured.binding.sources[0].paintCarrier?.node, 'Bind the actually painted carrier');+ await photo.evaluate(kind => {+ document.getElementById('likes').textContent = '1591 Likes';+ if (kind === 'asset') document.querySelector('.paint').style.backgroundImage = 'url("https://pbs.twimg.com/media/replacement.jpg")';+ }, change);+ const result = await ui.evaluate(async () => {+ captureAgent._queuePageFeedback(captureTab, { kind: 'dom', source: 'page', frameId: 0, target: 'button#likes' });+ return captureAgent.executeTool(captureTab, 'download_social_media', { target: 'image' });+ });+ const effects = await photo.evaluate(() => ({ urls: savedUrls, clicks: anchorClicks }));+ if (change === 'counter') {+ assert.equal(result.completedCount, 1, JSON.stringify(result));+ assert.deepEqual(effects, { urls: [captured.binding.candidates[0].url], clicks: 1 });+ } else {+ assert.equal(result.noDispatch, true);+ assert.equal(result.errorCode, 'media_binding_changed');+ assert.deepEqual(effects, { urls: [], clicks: 0 });+ }+ } finally {+ await context?.close();+ fs.rmSync(profile, { recursive: true, force: true });+ }+ });+}++for (const [build, Agent] of variants) {+ for (const failure of ['injection_failed', 'capture_failed', 'library_unavailable', 'source_unbound', 'untrusted_diagnostic']) {+ test(`${build}: missing capture reports sanitized ${failure} without a download or page-change retry`, async () => {+ const originalChrome = globalThis.chrome, originalBrowser = globalThis.browser;+ let injected = false;+ const capture = { bindings: {}, diagnostics: { image: { status: 'unavailable',+ reason: failure === 'untrusted_diagnostic' ? 'https://private.example/secret?token=hidden' : 'source_unbound',+ candidateCount: 200, sourceCount: -1, paintCarrierCount: 'private label', url: 'https://private.example/secret' } },+ status: failure === 'library_unavailable' ? failure : 'unavailable' };+ const inject = () => {+ if (failure === 'injection_failed') throw new Error('private provider error');+ injected = true; return [];+ };+ const read = () => {+ if (failure === 'capture_failed') throw new Error('private page content');+ return capture;+ };+ const testApi = { ...initialApi, tabs: { ...initialApi.tabs } };+ if (build === 'chrome') testApi.scripting = { executeScript: async request =>+ request.files ? inject() : [{ result: read() }] };+ else testApi.tabs.executeScript = async (_tab, request) => request.file ? inject() : [read()];+ globalThis.chrome = testApi; globalThis.browser = testApi;+ const tab = nextTab++, { agent, run } = agentWithRun(Agent, tab, 'https://x.com/account/status/123/photo/1');+ try {+ await agent._capturePageFeedbackModelState(tab);+ assert.deepEqual(run.modelState.mediaBindings, {});+ const metadata = run.modelState.mediaCapture;+ assert.equal(metadata.status, ['injection_failed', 'capture_failed', 'library_unavailable'].includes(failure) ? failure : 'unavailable');+ if (failure === 'source_unbound' || failure === 'untrusted_diagnostic') {+ assert.equal(metadata.targets.image.reason, failure === 'source_unbound' ? 'source_unbound' : 'no_verified_media');+ assert.equal(metadata.targets.image.candidateCount, 128);+ assert.equal(metadata.targets.image.sourceCount, 0);+ assert.ok(!('paintCarrierCount' in metadata.targets.image));+ }+ assert.ok(!JSON.stringify(metadata).includes('private'));+ const result = await agent.executeTool(tab, 'download_social_media', { target: 'image' });+ assert.equal(result.errorCode, 'media_binding_unavailable');+ assert.equal(result.noDispatch, true);+ assert.ok(!result.pageFeedbackPending, 'Unavailable capture is not browser intervention');+ assert.equal(run.passiveSupersessionStreak || 0, 0);+ assert.equal(injected, failure !== 'injection_failed');+ assert.ok(!JSON.stringify(result).includes('private'));+ } finally { globalThis.chrome = originalChrome; globalThis.browser = originalBrowser; }+ });+ }+}++for (const [build, Agent] of variants) {+ for (const change of ['url', 'document', 'steering', 'binding_url', 'identity_failure', 'document_unavailable']) {+ test(`${build}: ${change} drift during capture cannot attach fresh media to an older model snapshot`, async () => {+ const originalChrome = globalThis.chrome, originalBrowser = globalThis.browser;+ const tab = nextTab++, url = 'https://x.com/account/status/123/photo/1';+ let pageUrl = url, documentId = 'original-document', tabReads = 0;+ const { agent, run } = agentWithRun(Agent, tab, url);+ const api = { ...initialApi, tabs: { ...initialApi.tabs, get: async () => {+ if (++tabReads > 1 && change === 'identity_failure') throw new Error('identity unavailable');+ return { id: tab, url: pageUrl };+ } },+ webNavigation: { getAllFrames: async () => [{ frameId: 0, documentId, url: pageUrl }] } };+ const capture = () => {+ if (change === 'url') pageUrl = 'https://x.com/account/status/456/photo/1';+ if (change === 'document') documentId = 'replacement-document';+ if (change === 'document_unavailable') documentId = undefined;+ if (change === 'steering') agent._steeringRuns = new Map([[tab, { acceptedIds: new Set(['steered']) }]]);+ return { bindings: { image: { schema: 1, pageUrl: change === 'binding_url' ? 'https://x.com/account/status/456/photo/1' : pageUrl,+ focused: true, candidates: [{ url: 'https://pbs.twimg.com/media/intended.jpg', type: 'image' }] } },+ diagnostics: {}, status: 'ready' };+ };+ if (build === 'chrome') api.scripting = { executeScript: async request => request.files ? [] : [{ result: capture() }] };+ else api.tabs.executeScript = async (_tab, request) => request.file ? [] : [capture()];+ globalThis.chrome = api; globalThis.browser = api;+ try {+ await agent._capturePageFeedbackModelState(tab);+ assert.deepEqual(run.modelState.mediaBindings, {});+ assert.equal(run.modelState.mediaCapture.status, change === 'identity_failure' ? 'context_unverified' : 'context_changed');+ } finally { globalThis.chrome = originalChrome; globalThis.browser = originalBrowser; }+ });+ }+}++for (const [build, Agent] of variants) {+ for (const registered of [false, true, 'replacement', 'unavailable', 'inactive', 'token_mismatch', 'run_mismatch']) {+ test(`${build}: absent browser document identity (${registered}) requires a current main frame registration`, async () => {+ const originalChrome = globalThis.chrome, originalBrowser = globalThis.browser;+ const tab = nextTab++, url = 'https://x.com/account/status/123/photo/1';+ const { agent, run } = agentWithRun(Agent, tab, url);+ if (registered) run.frames.set(0, { token: 'content-token', id: '' });+ const api = { ...initialApi, webNavigation: { getAllFrames: async () => [{ frameId: 0, url }] },+ tabs: { ...initialApi.tabs, get: async () => ({ id: tab, url }), sendMessage: async () => {+ if (registered === 'replacement') run.frames.set(0, { token: 'replacement-token', id: '' });+ return { ready: registered !== 'unavailable', active: registered !== 'inactive',+ documentToken: registered === 'token_mismatch' ? 'new-document' : run.frames.get(0)?.token,+ runToken: registered === 'run_mismatch' ? 'another-run' : run.token };+ } } };+ const read = () => ({ bindings: { image: { pageUrl: url, focused: true, candidates: [], sources: [] } }, diagnostics: {}, status: 'ready' });+ if (build === 'chrome') api.scripting = { executeScript: async request => request.files ? [] : [{ result: read() }] };+ else api.tabs.executeScript = async (_tab, request) => request.file ? [] : [read()];+ globalThis.chrome = api; globalThis.browser = api;+ try {+ await agent._capturePageFeedbackModelState(tab);+ assert.equal(!!run.modelState.mediaBindings.image, registered === true);+ assert.equal(run.modelState.mediaCapture.status, registered === true ? 'ready' : 'context_unverified');+ } finally { globalThis.chrome = originalChrome; globalThis.browser = originalBrowser; }+ });+ }+}diff --git a/test/page-feedback-policy.mjs b/test/page-feedback-policy.mjsindex 57f90f570..a4e19ac02 100644--- a/test/page-feedback-policy.mjs+++ b/test/page-feedback-policy.mjs@@ -62,3 +62,22 @@ for (const build of ['chrome', 'firefox']) {assert.equal(keep('click_ax', { ref_id: 'ref_7' }), false);});}++for (const build of ['chrome', 'firefox']) {+ const { canRetainPageFeedbackCalls, pageFeedbackCallPolicy } = await import(`../src/${build}/src/agent/page-feedback-policy.js`);+ for (const args of [{ target: 'image' }, { mode: 'main', target: 'image' }, { target: 'video' }, { target: 'auto' }]) {+ test(`${build}: missing ${args.target} prebinding retains only a readonly resolution under counter churn`, () => {+ const state = { url: 'https://x.com/account/status/123/photo/1', mediaBindings: {} };+ assert.equal(pageFeedbackCallPolicy('download_social_media', args, state, page).kind, 'media_resolve');+ assert.equal(canRetainPageFeedbackCalls([call('download_social_media', args)], state, page, passive), true);+ assert.equal(canRetainPageFeedbackCalls([call('download_social_media', args)], state, page,+ [{ kind: 'pointer', source: 'user', frameId: 0 }]), false);+ });+ }+ for (const extra of [{ scroll: true }, { all: true }, { mode: 'all' }, { strategy: 'vision' }, { limit: 2 }]) {+ test(`${build}: media resolution does not authorize ${JSON.stringify(extra)}`, () => {+ assert.equal(pageFeedbackCallPolicy('download_social_media', { target: 'image', ...extra }, {}, page).kind, 'unsafe');+ assert.equal(canRetainPageFeedbackCalls([call('download_social_media', { target: 'image', ...extra })], {}, page, passive), false);+ });+ }+}diff --git a/test/page-feedback-progress.mjs b/test/page-feedback-progress.mjsindex 9e4393ab0..17023ae19 100644--- a/test/page-feedback-progress.mjs+++ b/test/page-feedback-progress.mjs@@ -283,3 +283,155 @@ for (const build of ['chrome', 'firefox']) {} finally { agent._finishSteeringRun(tab); agent._releaseRunEntry(tab); }});}++{+ // Download stage from webbrain-traces-1791372051482.json: X photo overlay,+ // unrelated live counters, and an optional pre-inference image binding missing.+ const PAGE_URL = 'https://x.com/mahallejargonu/status/2107603050810712208/photo/1';+ const IMAGE_URL = 'https://pbs.twimg.com/media/example-photo.jpg';+ const area = { get: async () => ({}), set: async () => {}, remove: async () => {} };+ let blindDownloadAttempts = 0;+ const api = {+ storage: { local: area, session: area },+ runtime: { getURL: value => `extension://test/${value}`, sendMessage: async () => ({}) },+ tabs: { get: async id => ({ id, url: PAGE_URL, title: 'Home / X' }), sendMessage: async () => ({ ready: true }) },+ webNavigation: { getAllFrames: async () => [{ frameId: 0, parentFrameId: -1, documentId: 'photo-document', url: PAGE_URL }] },+ scripting: { executeScript: async options => {+ if (options.files) return [];+ if (/SocialMediaDownloader\.run\(/.test(String(options.func))) blindDownloadAttempts++;+ // Both optional prebinding and readonly candidate resolution fail here.+ // Extraction remains available, as in CSS/media carriers not represented+ // by the downloader's initial candidate selector.+ return [{ result: { bindings: { auto: null, image: null, video: null },+ diagnostics: { image: { status: 'unavailable', reason: 'no_matching_media' } }, status: 'unavailable' } }];+ } },+ };+ let nextTab = 4100;+ const call = (id, name, args) => ({ toolCalls: [{ id, function: { name, arguments: JSON.stringify(args) } }] });++ function setup(Agent, next) {+ const provider = {+ name: 'missing media regression', model: 'test', promptTier: 'full', contextWindow: 128000,+ supportsTools: true, supportsVision: false, chat: next,+ async *chatStream(messages) {+ const response = await next(messages);+ if (response.content) yield { type: 'text', content: response.content };+ if (response.toolCalls) yield { type: 'tool_call', content: response.toolCalls.map((tool, index) => ({ ...tool, index })) };+ yield { type: 'done' };+ },+ };+ const agent = new Agent({ getActive: () => provider, getProvider: () => provider, getVisionProvider: async () => null });+ for (const method of ['_hydrate', '_persistSubmittedTurn', '_endTraceRun', '_manageContext', '_maybeReinjectAdapter',+ '_maybeEmitAskModeHandoff', '_ensureProgressSessionForCurrentTask']) agent[method] = async () => {};+ agent._persist = () => {};+ agent._persistNow = async () => ({ ok: true });+ agent._startTraceRun = async () => null;+ agent._beginReadCompleteness = async () => null;+ agent._checkCostAllowance = async () => null;+ agent._recordCostUsage = async () => null;+ agent._currentUrl = async () => PAGE_URL;+ agent._getTabUrlTitle = async () => ({ tabUrl: PAGE_URL, tabTitle: 'Home / X' });+ agent._plannerMode = () => 'off';+ agent._runPlannerIntentGate = async () => ({+ proceed: true, requestKind: 'execute', requiresStateChange: false, requiresSubmission: false,+ completionRequirements: { download: true },+ });+ agent._skipPermissionGate = true;+ agent._ensureGateSetting = async () => true;+ for (const name of ['_chromeProtectedPageFailure', '_captchaMutationPreflight', '_workflowPreSubmitDispatchBlock',+ '_messageRecipientGuardBlock', '_detectLikelySubmitAction', '_socialPublicationPreSubmitBlock']) agent[name] = async () => null;+ agent._downloadSocialMediaFallbackDecision = async () => null;+ agent._adoptLiveSocialPublishWorkflow = async () => false;+ agent._isFormValidationCandidate = () => false;+ agent._preflightRichTextToolbarTarget = async () => ({ block: null });+ agent._auditRichTextToolbarTarget = async () => {};+ agent._shouldAutoScreenshot = () => false;+ agent._pageFeedbackIdleMs = 0;+ agent._completionDoneBlock = () => null;+ agent.maxSteps = Infinity;+ return agent;+ }++ for (const build of ['chrome', 'firefox']) {+ const { Agent } = await import(`../src/${build}/src/agent/agent.js`);+ for (const streaming of [false, true]) for (const mode of [undefined, 'main']) {+ test(`${build}: ${streaming ? 'stream' : 'chat'} missing ${mode || 'auto'} photo prebinding gives one readonly diagnostic and permits fresh extraction`, async () => {+ const previousChrome = globalThis.chrome, previousBrowser = globalThis.browser;+ globalThis.chrome = api; globalThis.browser = api;+ try {+ const tab = nextTab++, updates = [], dispatched = [];+ let agent, requests = 0, sequence = 0, photoReads = 0;+ const final = 'Downloaded the photo as photo.jpg (download ID: saved-photo).';+ const counterUpdate = () => {+ const from = { tab: { id: tab }, frameId: 0, documentId: 'photo-document', url: PAGE_URL };+ const state = agent.pageMonitorState(from, 'photo-content');+ assert.equal(state.active, true);+ assert.equal(agent.observePageFeedback(from, { ...state, seq: ++sequence, revision: sequence,+ kind: 'dom', source: 'page', target: 'button [button]' }).accepted, true);+ };+ const next = async messages => {+ assert.ok(++requests <= 4, 'The missing optional binding must produce actionable tool feedback, not repeated model retries');+ counterUpdate();+ switch (requests) {+ case 1: return call('resolve-photo', 'download_social_media', { target: 'image', ...(mode ? { mode } : {}) });+ case 2: {+ const results = messages.filter(message => message.role === 'tool');+ assert.equal(results.length, 1);+ assert.match(results[0].content, /media_binding_unavailable/);+ assert.match(results[0].content, /"noDispatch":true/);+ assert.doesNotMatch(results[0].content, /"pageFeedbackPending":true/);+ return call('extract-photo', 'extract_data', { type: 'images' });+ }+ case 3:+ assert.ok(messages.some(message => message.role === 'tool' && message.content.includes(IMAGE_URL)));+ return call('download-exact-photo', 'download_files', { urls: [IMAGE_URL] });+ case 4: return call('finished', 'done', { summary: final, outcome: 'success' });+ }+ };+ agent = setup(Agent, next);+ agent._enrichUserMessageWithCurrentPage = async (_tab, _messages, content) => {+ // Bind the content document before the production inference snapshot.+ agent.pageMonitorState({ tab: { id: tab }, frameId: 0, documentId: 'photo-document', url: PAGE_URL }, 'photo-content');+ return { role: 'user', content };+ };+ const executeProductionTool = Agent.prototype.executeTool.bind(agent);+ agent.executeTool = async (_tab, name, args, onUpdate, context) => {+ if (name === 'get_accessibility_tree') {+ photoReads++;+ return { success: true, pageContent: `dialog [ref_1]\n button "${2556 + photoReads} Likes. Like" [ref_2] type="button"\n image "Photo" [ref_3]` };+ }+ if (name === 'download_social_media') {+ dispatched.push({ name, args });+ return executeProductionTool(_tab, name, args, onUpdate, context);+ }+ if (name === 'extract_data') {+ dispatched.push({ name, args });+ return { success: true, images: [{ src: IMAGE_URL }] };+ }+ if (name === 'download_files') {+ dispatched.push({ name, args });+ return { success: true, count: 1, completedCount: 1, downloads: [{ id: 'saved-photo', filename: 'photo.jpg', state: 'complete' }] };+ }+ assert.equal(name, 'done');+ return { done: true, success: true, summary: args.summary, outcome: args.outcome };+ };+ const blindBefore = blindDownloadAttempts;+ const options = { detachedRequestId: 'missing-media-run', askStreamingEnabled: false };+ const update = (type, data) => updates.push({ type, data });+ const result = streaming+ ? await agent.processMessageStream(tab, 'Download the currently open X photo', update, 'act', options)+ : await agent.processMessage(tab, 'Download the currently open X photo', update, 'act', [], options);+ assert.equal(result, final, JSON.stringify(updates.filter(entry => entry.type === 'tool_result')));+ assert.equal(requests, 4);+ assert.deepEqual(dispatched.map(entry => entry.name), ['download_social_media', 'extract_data', 'download_files']);+ assert.equal(blindDownloadAttempts, blindBefore, 'Missing binding resolution must never invoke the downloader');+ assert.ok(photoReads > 0);+ assert.equal(agent._pageFeedbackRuns.size, 0);+ assert.equal(updates.some(entry => entry.type === 'run_status' && entry.data.status === 'page_unstable'), false);+ } finally {+ globalThis.chrome = previousChrome; globalThis.browser = previousBrowser;+ }+ });+ }+ }+}diff --git a/test/page-monitor-passive-progress.mjs b/test/page-monitor-passive-progress.mjsindex c9a7e1f85..fc264a25e 100644--- a/test/page-monitor-passive-progress.mjs+++ b/test/page-monitor-passive-progress.mjs@@ -44,6 +44,35 @@ async function fixture(browser, build) {return { context, page };}+async function registrationFixture(browser, build, mode = 'active') {+ const context = await browser.newContext();+ await context.route('https://registration.test/**', route => route.fulfill({ contentType: 'text/html', body: '<!doctype html><p>Registration fixture</p>' }));+ await context.addInitScript(mode => {+ window.registrationRequests = [];+ window.registrationMode = mode;+ window.registrationListeners = [];+ const runtime = { onMessage: { addListener: listener => registrationListeners.push(listener), removeListener: () => {} },+ async sendMessage(message) {+ if (message.action === 'get_page_monitor_state') {+ registrationRequests.push(message);+ if (registrationMode === 'throw') throw new Error('Background registration failed');+ if (registrationMode === 'inactive') return { active: false };+ return { active: true, runToken: 'registration-run', documentToken: message.documentToken };+ }+ return { accepted: true };+ } };+ window.chrome = { runtime }; window.browser = window.chrome;+ window.activateMonitor = () => new Promise(resolve => {+ for (const listener of registrationListeners) listener({ target: 'content', action: 'page_monitor_state', active: true }, {}, resolve);+ });+ }, mode);+ 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://registration.test/same-url');+ await page.waitForFunction(() => window.__wbPageMonitor && registrationRequests.length > 0);+ return { context, page };+}+async function prepare(page, boundary, extra = {}) {return page.evaluate(async ({ boundary, extra }) => {const params = { operationId: 'prepared', tool: 'click_ax', selector: '#send', allowPassiveRebase: true, ...extra };@@ -242,6 +271,57 @@ for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {});}+for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {+ test(`${build}: monitor registration acknowledgment proves current document ownership`, { timeout: 30000 }, async t => {+ const browser = await engine.launch({ headless: true });+ try {+ await t.test('successful registration returns the active document and run tokens', async () => {+ const { context, page } = await registrationFixture(browser, build);+ try {+ const acknowledgment = await page.evaluate(() => activateMonitor());+ const requestedToken = await page.evaluate(() => registrationRequests.at(-1).documentToken);+ assert.equal(acknowledgment.ready, true);+ assert.equal(acknowledgment.active, true);+ assert.equal(acknowledgment.documentToken, requestedToken);+ assert.equal(acknowledgment.runToken, 'registration-run');+ assert.ok(acknowledgment.documentToken);+ assert.equal(await page.evaluate(() => __wbPageMonitor.active), true);+ } finally { await context.close(); }+ });++ for (const mode of ['inactive', 'throw']) for (const previouslyActive of [false, true]) await t.test(`${mode} background registration (${previouslyActive ? 'previously active' : 'new document'}) cannot acknowledge active ownership`, async () => {+ const { context, page } = await registrationFixture(browser, build, previouslyActive ? 'active' : mode);+ try {+ if (previouslyActive) {+ assert.equal((await page.evaluate(() => activateMonitor())).active, true);+ await page.evaluate(value => { registrationMode = value; }, mode);+ }+ const acknowledgment = await page.evaluate(() => activateMonitor());+ assert.equal(acknowledgment.ready, true);+ assert.equal(acknowledgment.active, false);+ assert.notEqual(acknowledgment.runToken, 'registration-run');+ assert.equal(await page.evaluate(() => __wbPageMonitor.active), false);+ } finally { await context.close(); }+ });++ await t.test('reloading the same URL returns a fresh document token', async () => {+ const { context, page } = await registrationFixture(browser, build);+ try {+ const previous = await page.evaluate(() => activateMonitor());+ await page.reload();+ await page.waitForFunction(() => window.__wbPageMonitor && registrationRequests.length > 0);+ const current = await page.evaluate(() => activateMonitor());+ assert.equal(current.active, true);+ assert.equal(current.runToken, previous.runToken);+ assert.notEqual(current.documentToken, previous.documentToken);+ assert.equal(current.documentToken, await page.evaluate(() => registrationRequests.at(-1).documentToken));+ assert.equal(page.url(), 'https://registration.test/same-url');+ } finally { await context.close(); }+ });+ } finally { await browser.close(); }+ });+}+test('Chrome and Firefox passive preparation validation stay byte-identical', () => {assert.equal(fs.readFileSync(new URL('../src/chrome/src/content/page-monitor.js', import.meta.url), 'utf8'),diff --git a/test/page-monitor-ui.mjs b/test/page-monitor-ui.mjsindex d924d4224..917ec3afa 100644--- a/test/page-monitor-ui.mjs+++ b/test/page-monitor-ui.mjs@@ -3059,8 +3059,9 @@ for (const [build, engine] of [['chrome', chromium], ['firefox', firefox]]) {});const mark = sequence => page.evaluate(marker => document.getElementById('field').setAttribute('data-webbrain-native-action', marker), createNativeActionMarker(guard, 'input', sequence));- await mark(1); await page.keyboard.down('Control');- await mark(2); await page.keyboard.press('a'); await page.keyboard.up('Control');+ const selectModifier = await page.evaluate(() => /Mac|iPhone|iPad|iPod/i.test(navigator.platform) ? 'Meta' : 'Control');+ await mark(1); await page.keyboard.down(selectModifier);+ await mark(2); await page.keyboard.press('a'); await page.keyboard.up(selectModifier);await mark(3); await page.keyboard.press('Delete');await mark(4); await page.keyboard.insertText('new');assert.equal(await page.locator('#field').inputValue(), 'new');