@wenathlan/extension 1.1.58 → 1.1.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../cdpbus.ts", "../../emulation.ts", "../../runtimeline.ts", "../../policy.ts", "../pageactions.ts", "../pagexpath.ts", "../pageresolve.ts", "../pagereads.ts", "../pagecontrols.ts", "../pagepointer.ts", "../pageinteract.ts", "../pagenav.ts", "../pagedialogs.ts", "../pageobserve.ts", "../pagedetect.ts", "../pagewatch.ts", "../pageemulate.ts", "../pagedebug.ts", "../pageprofile.ts", "../pageforms.ts", "../pagewizards.ts", "../pagedata.ts", "../datacommand.ts", "../pagebridge.ts"],
4
- "sourcesContent": ["import type { breakpointspec, cdpallowlist, cdpcommand, cdpeventrule, cdpsession, pausestate, scriptoverride, stackframe, stepmode, teardownplan, watchexpression } from \"./types.js\";\n\n/**\n * Devtools protocol bus for the 1.1.46 debugging family.\n * Every correlated rule for the reviewed instrumented devtools session lives in this file: the domain grammar, the attach and detach lifecycle with the honest derivation note, the raw command records with duration and error class, the per session command serialization in send order, the domain event rules with match filters and per domain event counts, the breakpoint input grammar, the pause state capture with call frames, the step mode grammar, the watch expression values per pause, the script override url patterns and the teardown plan that reverts every breakpoint and override and decides the resume policy when the user detaches the debugger.\n * The chrome devtools protocol is unavailable without the debugger permission, which the manifest gate forbids, so every command routes through the page-instrumented harness injected through the scripting api; the derivation is recorded on every session instead of hidden.\n */\n\n/** The debugging kinds of the devtools family, listed among the available capabilities of every proposal request. */\nexport const cdpkinds: string[] = [\"attachcdp\", \"detachcdp\", \"cdpcmd\", \"watchcdp\", \"setbreakpoint\", \"stepcode\", \"watchexpr\", \"overridescript\"];\n\n/** The reviewed devtools domain grammar: runtime evaluation, log capture, debugger state, dom snapshots, network facts and page lifecycle; the enabled subset stays a user choice bounded by this grammar only. */\nexport const cdpdomains: string[] = [\"Runtime\", \"Log\", \"Debugger\", \"DOM\", \"Network\", \"Page\"];\n\n/** Resolves the domain of one devtools method name of the form Domain.method; malformed methods resolve to undefined. */\nexport function methoddomain(method: string): string | undefined {\n const match = /^([A-Z][A-Za-z]*)\\.([a-zA-Z][A-Za-z0-9]*)$/.exec(method.trim());\n return match?.[1];\n}\n\n/** Normalizes one reviewed devtools domain allowlist: the enabled domains bounded by the reviewed domain grammar and the optional method gates inside the enabled domains; a gate list that names no method of the enabled domains is refused. */\nexport function cdpallowlistof(value: unknown): cdpallowlist | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const domains = Array.isArray(entry.domains) ? entry.domains.filter((domain): domain is string => typeof domain === \"string\" && cdpdomains.includes(domain)) : [];\n if (domains.length === 0) return undefined;\n if (entry.methods === undefined) return { domains };\n const methods = Array.isArray(entry.methods) ? entry.methods.filter((method): method is string => typeof method === \"string\" && methoddomain(method) !== undefined && domains.includes(methoddomain(method) as string)) : [];\n if (methods.length === 0) return undefined;\n return { domains, methods };\n}\n\n/** True when the allowlist covers one method: the method domain must be enabled and a present method gate list must name the method. */\nexport function allowlistcovers(allowlist: cdpallowlist, method: string): boolean {\n const domain = methoddomain(method);\n if (domain === undefined) return false;\n if (!allowlist.domains.includes(domain)) return false;\n if (allowlist.methods !== undefined && !allowlist.methods.includes(method)) return false;\n return true;\n}\n\n/** Attaches one instrumented devtools session to the run tab with the reviewed enabled domains and the honest debugger derivation note. */\nexport function attachcdpsession(input: { id: string; runid: string; stepid: string; tabid: number; origin: string; domains: string[]; now: number; debuggerversion: string }): cdpsession {\n return { id: input.id, runid: input.runid, stepid: input.stepid, tabid: input.tabid, origin: input.origin, attachedat: input.now, domains: [...new Set(input.domains)], debuggerversion: input.debuggerversion };\n}\n\n/** Detaches one session cleanly: the detach time stamps the record and the domains stay auditable after the detach. */\nexport function detachcdpsession(session: cdpsession, at: number, userdetached = false): cdpsession {\n return { ...session, detachedat: at, ...(userdetached ? { userdetached: true } : {}) };\n}\n\n/** Builds one raw protocol command record with its method, params, domain, dotted result path, duration and error class. */\nexport function sendcdpcommand(input: { id: string; sessionid: string; runid: string; stepid: string; method: string; params?: Record<string, unknown>; resultpath?: string; duration: number; errorclass?: string; at: number }): cdpcommand {\n const domain = methoddomain(input.method);\n if (domain === undefined) return { ...input, method: input.method.trim(), domain: \"\", duration: input.duration, ...(input.errorclass !== undefined ? { errorclass: input.errorclass } : { errorclass: \"malformedmethod\" }), at: input.at };\n return { ...input, method: input.method.trim(), domain, duration: input.duration, ...(input.errorclass !== undefined ? { errorclass: input.errorclass } : {}), at: input.at };\n}\n\n/** Appends one command to the per session send queue so concurrent commands serialize in send order. */\nexport function serializecdpcommand(queue: cdpcommand[], command: cdpcommand): cdpcommand[] {\n return [...queue, command];\n}\n\n/** Normalizes one reviewed domain event rule: the domain of the reviewed grammar, the event name and the optional payload match filter. */\nexport function cdpeventruleof(value: unknown): Pick<cdpeventrule, \"domain\" | \"event\" | \"match\"> | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const domain = typeof entry.domain === \"string\" && cdpdomains.includes(entry.domain) ? entry.domain : undefined;\n const event = typeof entry.event === \"string\" && entry.event.trim() ? entry.event.trim() : undefined;\n if (domain === undefined || event === undefined) return undefined;\n const match = typeof entry.match === \"string\" && entry.match.trim() ? entry.match.trim() : undefined;\n return { domain, event, ...(match !== undefined ? { match } : {}) };\n}\n\n/** Matches observed domain events against the reviewed rules and counts the matched events per domain; the match filter must appear in the payload before an event forwards. */\nexport function watchcdpevents(rules: cdpeventrule[], events: Array<{ domain: string; event: string; payload?: string }>): { matched: Array<{ ruleid: string; domain: string; event: string; payload?: string }>; counts: Record<string, number> } {\n const matched: Array<{ ruleid: string; domain: string; event: string; payload?: string }> = [];\n const counts: Record<string, number> = {};\n for (const domain of cdpdomains) counts[domain] = 0;\n for (const event of events) {\n for (const rule of rules) {\n if (rule.domain !== event.domain || rule.event !== event.event) continue;\n if (rule.match !== undefined && !(event.payload ?? \"\").includes(rule.match)) continue;\n matched.push({ ruleid: rule.id, domain: event.domain, event: event.event, ...(event.payload !== undefined ? { payload: event.payload } : {}) });\n counts[event.domain] = (counts[event.domain] ?? 0) + 1;\n }\n }\n return { matched, counts };\n}\n\n/** Normalizes one reviewed breakpoint input: the script url, the zero based line, the optional column and the condition of the reviewed expression grammar. */\nexport function breakpointinputof(value: unknown): Pick<breakpointspec, \"url\" | \"line\" | \"column\" | \"condition\"> | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const url = typeof entry.url === \"string\" && entry.url.trim() ? entry.url.trim() : undefined;\n const line = typeof entry.line === \"number\" && Number.isInteger(entry.line) && entry.line >= 0 ? entry.line : undefined;\n if (url === undefined || line === undefined) return undefined;\n const column = typeof entry.column === \"number\" && Number.isInteger(entry.column) && entry.column >= 0 ? entry.column : undefined;\n const condition = typeof entry.condition === \"string\" && entry.condition.trim() ? entry.condition.trim() : undefined;\n return { url, line, ...(column !== undefined ? { column } : {}), ...(condition !== undefined ? { condition } : {}) };\n}\n\n/** Captures one pause state from the instrumented pause: the reason, the call frames, the hit breakpoint and the dom snapshot reference of the page bridge capture. */\nexport function capturepause(input: { id: string; runid: string; stepid: string; reason: string; callframes: stackframe[]; hitbreakpoint?: string; domsnapshotid?: string; at: number }): pausestate {\n return { id: input.id, runid: input.runid, stepid: input.stepid, reason: input.reason, callframes: [...input.callframes], ...(input.hitbreakpoint !== undefined ? { hitbreakpoint: input.hitbreakpoint } : {}), ...(input.domsnapshotid !== undefined ? { domsnapshotid: input.domsnapshotid } : {}), at: input.at };\n}\n\n/** Normalizes one reviewed step mode of the instrumented debugger: stepover, stepinto, stepout or resume. */\nexport function stepmodeof(value: unknown): stepmode | undefined {\n const modes: stepmode[] = [\"stepover\", \"stepinto\", \"stepout\", \"resume\"];\n return typeof value === \"string\" && modes.includes(value as stepmode) ? value as stepmode : undefined;\n}\n\n/** Normalizes one reviewed watch expression input: the expression text and the pause scope it evaluates in. */\nexport function watchexpressionof(value: unknown): Pick<watchexpression, \"expression\" | \"scope\"> | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const expression = typeof entry.expression === \"string\" && entry.expression.trim() ? entry.expression.trim() : undefined;\n if (expression === undefined) return undefined;\n const scope = typeof entry.scope === \"string\" && entry.scope.trim() ? entry.scope.trim() : \"topframe\";\n return { expression, scope };\n}\n\n/** Records one watch value captured at a pause with the pause scope correlation. */\nexport function recordwatchvalue(expression: watchexpression, pauseid: string, value: string, at: number): watchexpression {\n return { ...expression, values: [...expression.values, { pauseid, value, at }] };\n}\n\n/** Normalizes one reviewed script override input: the url pattern that names its origin explicitly and the full fixture source. */\nexport function overrideinputof(value: unknown): Pick<scriptoverride, \"urlpattern\" | \"source\"> | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const urlpattern = typeof entry.urlpattern === \"string\" && entry.urlpattern.trim() ? entry.urlpattern.trim() : undefined;\n const source = typeof entry.source === \"string\" ? entry.source : undefined;\n if (urlpattern === undefined || source === undefined || source.trim().length === 0) return undefined;\n return { urlpattern, source };\n}\n\n/** Matches one script url against a reviewed override pattern of an explicit https origin with single star segments and double star subtrees. */\nexport function overridematches(urlpattern: string, url: string): boolean {\n const patternmatch = /^(https:\\/\\/[^/]+)(\\/.*)?$/.exec(urlpattern);\n const urlmatch = /^(https:\\/\\/[^/]+)(\\/.*)?$/.exec(url);\n if (!patternmatch || !urlmatch) return false;\n if (patternmatch[1] !== urlmatch[1]) return false;\n const patternpath = (patternmatch[2] ?? \"/\").split(\"/\").filter(segment => segment.length > 0);\n const urlpath = (urlmatch[2] ?? \"/\").split(\"/\").filter(segment => segment.length > 0);\n const walk = (patternindex: number, urlindex: number): boolean => {\n if (patternindex >= patternpath.length) return urlindex >= urlpath.length;\n const segment = patternpath[patternindex];\n if (segment === \"**\") return walk(patternindex + 1, urlindex) || (urlindex < urlpath.length && walk(patternindex, urlindex + 1));\n if (urlindex >= urlpath.length) return false;\n if (segment !== \"*\" && segment !== urlpath[urlindex]) return false;\n return walk(patternindex + 1, urlindex + 1);\n };\n return walk(0, 0);\n}\n\n/** Normalizes one reviewed teardown plan: the revert steps and the resume policy of resume, pause or ask. */\nexport function teardownplanof(value: unknown): teardownplan | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const revertsteps = Array.isArray(entry.revertsteps) ? entry.revertsteps.filter((step): step is string => typeof step === \"string\" && step.trim().length > 0) : [];\n const policy = entry.resumepolicy;\n if (revertsteps.length === 0) return undefined;\n if (policy !== undefined && policy !== \"resume\" && policy !== \"pause\" && policy !== \"ask\") return undefined;\n return { revertsteps, resumepolicy: policy ?? \"ask\" };\n}\n\n/** The teardown outcome of one session: every breakpoint and override reverts, the resume policy decides the continuation and a user detach keeps the session record alive for review while the run pauses. */\nexport interface teardowndecision {\n session: cdpsession;\n revertedbreakpoints: string[];\n revertedoverrides: string[];\n resumepolicy: teardownplan[\"resumepolicy\"];\n paused: boolean;\n keepsalive: boolean;\n}\n\n/** Tears one session down safely: the revert steps of the reviewed teardown plan revert every active breakpoint and override in order, the session detaches, and a user detach keeps the session record alive while the run pauses for review before continuing. */\nexport function teardowncdpsession(input: { session: cdpsession; breakpoints: breakpointspec[]; overrides: scriptoverride[]; plan: teardownplan | undefined; userdetached: boolean; at: number }): teardowndecision {\n const revertedbreakpoints = input.breakpoints.filter(spec => spec.revertedat === undefined).map(spec => spec.id);\n const revertedoverrides = input.overrides.filter(spec => spec.revertedat === undefined).map(spec => spec.id);\n const resumepolicy = input.userdetached ? \"pause\" : input.plan?.resumepolicy ?? \"ask\";\n return {\n session: detachcdpsession(input.session, input.at, input.userdetached),\n revertedbreakpoints,\n revertedoverrides,\n resumepolicy,\n paused: input.userdetached || resumepolicy === \"pause\",\n keepsalive: input.userdetached,\n };\n}\n", "import type { agentpreset, blackboxrule, devicepreset, emulationlayer, emulationstate, locationconsent, locationpreset, networkpreset, permissiongrant, permissionstate, presetlibrary, stackframe } from \"./types.js\";\n\n/**\n * Emulation layer engine for the 1.1.48 family.\n * Every correlated rule for the reviewed masks of the run lives in this file: the preset normalizers of the user curated device, network, location and agent libraries, the reviewed revert plan grammar, the layer apply and revert math with prior state capture and reverse order restore, the stacking conflict order where the last applied layer wins, the latitude, longitude and user agent grammars, the browser permission set, the blackbox pattern matching that hides third party frames from stack traces, the retention expiry of reverted layer states and the versioned preset library import and export.\n * True device metric, network condition, geolocation and user agent override needs browser debugger or platform permissions that the manifest gate forbids, so every layer applies page-injected overrides through the scripting api and shapes only the traffic the extension itself initiates; the derivation is recorded on every layer instead of hidden.\n */\n\n/** The emulation kinds of the 1.1.48 family, listed among the available capabilities of every proposal request. */\nexport const emulationkinds: string[] = [\"emulatedevice\", \"emulatenetwork\", \"emulatelocate\", \"setuseragent\", \"overridepermission\", \"blackboxscripts\"];\n\n/** The reviewed browser permission set of the override grammar; overrides outside this set are refused. */\nexport const browserpermissions: string[] = [\"geolocation\", \"notifications\", \"camera\", \"microphone\", \"clipboard-read\", \"clipboard-write\", \"midi\", \"persistent-storage\"];\n\n/** The reviewed permission states of an override: granted, denied or the browser default prompt. */\nexport const permissionstates: permissionstate[] = [\"granted\", \"denied\", \"prompt\"];\n\n/** The emulation family of one emulation action kind. */\nexport function familyofkind(kind: string): \"device\" | \"network\" | \"location\" | \"agent\" | \"permission\" | \"blackbox\" | undefined {\n if (kind === \"emulatedevice\") return \"device\";\n if (kind === \"emulatenetwork\") return \"network\";\n if (kind === \"emulatelocate\") return \"location\";\n if (kind === \"setuseragent\") return \"agent\";\n if (kind === \"overridepermission\") return \"permission\";\n if (kind === \"blackboxscripts\") return \"blackbox\";\n return undefined;\n}\n\n/** Normalizes one user curated device preset: the name, width, height, pixel ratio and the mobile flag with every bound a user choice only. */\nexport function devicepresetof(value: unknown): devicepreset | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const name = typeof entry.name === \"string\" && entry.name.trim() ? entry.name.trim() : undefined;\n const width = typeof entry.width === \"number\" && Number.isInteger(entry.width) && entry.width > 0 ? entry.width : undefined;\n const height = typeof entry.height === \"number\" && Number.isInteger(entry.height) && entry.height > 0 ? entry.height : undefined;\n const pixelratio = typeof entry.pixelratio === \"number\" && Number.isFinite(entry.pixelratio) && entry.pixelratio > 0 ? entry.pixelratio : undefined;\n if (name === undefined || width === undefined || height === undefined || pixelratio === undefined) return undefined;\n return { name, width, height, pixelratio, mobile: entry.mobile === true };\n}\n\n/** Normalizes one user curated network preset: the name, latency, download and upload bounds and the offline flag. */\nexport function networkpresetof(value: unknown): networkpreset | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const name = typeof entry.name === \"string\" && entry.name.trim() ? entry.name.trim() : undefined;\n const latency = typeof entry.latency === \"number\" && Number.isFinite(entry.latency) && entry.latency >= 0 ? entry.latency : undefined;\n const download = typeof entry.download === \"number\" && Number.isFinite(entry.download) && entry.download >= 0 ? entry.download : undefined;\n const upload = typeof entry.upload === \"number\" && Number.isFinite(entry.upload) && entry.upload >= 0 ? entry.upload : undefined;\n if (name === undefined || latency === undefined || download === undefined || upload === undefined) return undefined;\n return { name, latency, download, upload, offline: entry.offline === true };\n}\n\n/** Normalizes one user curated location preset: the name, the latitude and longitude inside the reviewed ranges and the non-negative accuracy radius. */\nexport function locationpresetof(value: unknown): locationpreset | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const name = typeof entry.name === \"string\" && entry.name.trim() ? entry.name.trim() : undefined;\n const latitude = typeof entry.latitude === \"number\" && Number.isFinite(entry.latitude) ? entry.latitude : undefined;\n const longitude = typeof entry.longitude === \"number\" && Number.isFinite(entry.longitude) ? entry.longitude : undefined;\n const accuracy = typeof entry.accuracy === \"number\" && Number.isFinite(entry.accuracy) && entry.accuracy >= 0 ? entry.accuracy : undefined;\n if (name === undefined || latitude === undefined || longitude === undefined || accuracy === undefined) return undefined;\n if (!locationrangevalid(latitude, longitude)) return undefined;\n return { name, latitude, longitude, accuracy };\n}\n\n/** Normalizes one user curated agent preset: the user agent string of the reviewed grammar, the platform and the non-empty brand list reported together. */\nexport function agentpresetof(value: unknown): agentpreset | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const name = typeof entry.name === \"string\" && entry.name.trim() ? entry.name.trim() : undefined;\n const useragent = typeof entry.useragent === \"string\" ? entry.useragent : undefined;\n const platform = typeof entry.platform === \"string\" && entry.platform.trim() ? entry.platform.trim() : undefined;\n const brands = Array.isArray(entry.brands) ? entry.brands.filter((brand): brand is string => typeof brand === \"string\" && brand.trim().length > 0) : [];\n if (name === undefined || useragent === undefined || platform === undefined || brands.length === 0) return undefined;\n if (!agentgrammarvalid(useragent)) return undefined;\n return { name, useragent, platform, brands: [...new Set(brands)] };\n}\n\n/** Normalizes one reviewed permission override: the name of the reviewed browser permission set, the state of the reviewed permission states and the run scope flag. */\nexport function permissiongrantof(value: unknown): permissiongrant | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const name = typeof entry.name === \"string\" && browserpermissions.includes(entry.name) ? entry.name : undefined;\n const state = typeof entry.state === \"string\" && permissionstates.includes(entry.state as permissionstate) ? entry.state as permissionstate : undefined;\n if (name === undefined || state === undefined) return undefined;\n return { name, state, runscope: entry.runscope !== false };\n}\n\n/** Normalizes one reviewed blackbox rule: a non-empty url pattern list where every pattern names its origin explicitly and the trace scope of profiles, traces or both. */\nexport function blackboxruleof(value: unknown): blackboxrule | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const urlpatterns = Array.isArray(entry.urlpatterns) ? entry.urlpatterns.filter((pattern): pattern is string => typeof pattern === \"string\" && /^https:\\/\\//.test(pattern)) : [];\n const tracescope = entry.tracescope;\n if (urlpatterns.length === 0) return undefined;\n if (tracescope !== \"profiles\" && tracescope !== \"traces\" && tracescope !== \"both\") return undefined;\n return { urlpatterns: [...new Set(urlpatterns)], tracescope };\n}\n\n/** Normalizes one reviewed revert plan: a non-empty ordered list of revert steps kept beside its layer; plans without steps are refused. */\nexport function revertplanof(value: unknown): string[] | undefined {\n const steps = Array.isArray(value) ? value.filter((step): step is string => typeof step === \"string\" && step.trim().length > 0) : [];\n return steps.length > 0 ? steps : undefined;\n}\n\n/** Builds one emulation layer record with its origin scope, apply time, captured prior state and reviewed revert plan. */\nexport function newlayer(input: { id: string; runid: string; stepid: string; family: emulationlayer[\"family\"]; name: string; originscope: string; revertplan: string[]; prior?: Record<string, unknown>; at: number }): emulationlayer {\n return { id: input.id, runid: input.runid, stepid: input.stepid, family: input.family, name: input.name, originscope: input.originscope, appliedat: input.at, ...(input.prior !== undefined ? { prior: input.prior } : {}), revertplan: [...input.revertplan] };\n}\n\n/** Builds the initial emulation state of one run scoped to its tab and origin. */\nexport function emulationstateof(input: { runid: string; tabid: number; origin: string; now: number }): emulationstate {\n return { runid: input.runid, tabid: input.tabid, origin: input.origin, layers: [], updatedat: input.now };\n}\n\n/** Stacks one layer onto the emulation state in apply order: a second layer of the same family replaces the first in effect while both stay in the history, so the last applied layer wins conflicts. */\nexport function applylayer(state: emulationstate, layer: emulationlayer, at: number): emulationstate {\n const layers = [...state.layers.filter(item => item.id !== layer.id), layer];\n return { ...state, layers, updatedat: at };\n}\n\n/** Reverts one layer of the state by its id: the revert time stamps the record while the layer history survives for review. */\nexport function revertlayer(state: emulationstate, layerid: string, at: number): emulationstate {\n const layers = state.layers.map(layer => layer.id === layerid && layer.revertedat === undefined ? { ...layer, revertedat: at } : layer);\n return { ...state, layers, updatedat: at };\n}\n\n/** Reverts every active layer of the state in reverse apply order: the reversed list is the exact restore sequence and every layer keeps its revert stamp. */\nexport function revertalllayers(state: emulationstate, at: number): { state: emulationstate; reverted: emulationlayer[] } {\n const reverted = [...state.layers].reverse().filter(layer => layer.revertedat === undefined);\n const layers = state.layers.map(layer => layer.revertedat === undefined ? { ...layer, revertedat: at } : layer);\n return { state: { ...state, layers, updatedat: at }, reverted };\n}\n\n/** Returns the active layers of the state in apply order. */\nexport function activelayers(state: emulationstate | undefined): emulationlayer[] {\n return state ? state.layers.filter(layer => layer.revertedat === undefined) : [];\n}\n\n/** Returns the names of the active layers for the response envelope and the review panel. */\nexport function layernames(state: emulationstate | undefined): string[] {\n return activelayers(state).map(layer => layer.name);\n}\n\n/** Counts the active layers of one family so the panel can warn when layers stack on one tab. */\nexport function stackedcount(state: emulationstate | undefined): number {\n return activelayers(state).length;\n}\n\n/** True when the reviewed latitude stays inside the -90 to 90 degree range and the longitude inside the -180 to 180 degree range. */\nexport function locationrangevalid(latitude: number, longitude: number): boolean {\n return Number.isFinite(latitude) && Number.isFinite(longitude) && latitude >= -90 && latitude <= 90 && longitude >= -180 && longitude <= 180;\n}\n\n/** Validates one user agent string against the reviewed grammar: tokens of word characters, separators, slashes, spaces, numbers and version dots; the string must carry at least one token pair and refuse line breaks. */\nexport function agentgrammarvalid(useragent: string): boolean {\n const text = useragent.trim();\n if (text.length === 0 || text.length > 512) return false;\n if (/[\\r\\n]/.test(text)) return false;\n if (!/^[A-Za-z0-9][A-Za-z0-9._+\\-()/:; ,]*$/.test(text)) return false;\n return /\\/\\d/.test(text) || /\\d+\\.\\d+/.test(text);\n}\n\n/** Grades one reviewed permission name by its power: location, camera, microphone and notification overrides carry the user's most sensitive signals and stay the highest grade. */\nexport function permissiongrade(name: string): \"powerful\" | \"standard\" {\n return name === \"geolocation\" || name === \"camera\" || name === \"microphone\" || name === \"notifications\" ? \"powerful\" : \"standard\";\n}\n\n/** Matches one script url against a reviewed blackbox pattern of an explicit https origin with single star segments and double star subtrees. */\nexport function blackboxmatches(urlpattern: string, url: string): boolean {\n const patternmatch = /^(https:\\/\\/[^/]+)(\\/.*)?$/.exec(urlpattern);\n const urlmatch = /^(https:\\/\\/[^/]+)(\\/.*)?$/.exec(url);\n if (!patternmatch || !urlmatch) return false;\n if (patternmatch[1] !== urlmatch[1]) return false;\n const patternpath = (patternmatch[2] ?? \"/\").split(\"/\").filter(segment => segment.length > 0);\n const urlpath = (urlmatch[2] ?? \"/\").split(\"/\").filter(segment => segment.length > 0);\n const walk = (patternindex: number, urlindex: number): boolean => {\n if (patternindex >= patternpath.length) return urlindex >= urlpath.length;\n const segment = patternpath[patternindex];\n if (segment === undefined) return false;\n if (segment === \"**\") return walk(patternindex + 1, urlindex) || (urlindex < urlpath.length && walk(patternindex, urlindex + 1));\n if (urlindex >= urlpath.length) return false;\n if (segment !== \"*\" && segment !== urlpath[urlindex]) return false;\n return walk(patternindex + 1, urlindex + 1);\n };\n return walk(0, 0);\n}\n\n/** Hides blackboxed frames from one stack trace: every frame whose url matches a rule pattern with a traces scope of traces or both is dropped so third party frames never enter the shaped trace. */\nexport function hideblackboxedframes(rules: blackboxrule[], frames: stackframe[]): stackframe[] {\n const patterns = rules.filter(rule => rule.tracescope === \"traces\" || rule.tracescope === \"both\").flatMap(rule => rule.urlpatterns);\n if (patterns.length === 0) return frames;\n return frames.filter(frame => !patterns.some(pattern => blackboxmatches(pattern, frame.url)));\n}\n\n/** Marks one url as blackboxed when any rule with a profiles or both trace scope matches it, so the trace shaping lists the hidden third party urls. */\nexport function blackboxedurls(rules: blackboxrule[], urls: string[]): string[] {\n const patterns = rules.flatMap(rule => rule.urlpatterns);\n return urls.filter(url => patterns.some(pattern => blackboxmatches(pattern, url)));\n}\n\n/** Expires the prior states of reverted layers after the retention window while the layer history itself always survives; an absent window keeps every prior state. */\nexport function expirelayers(state: emulationstate, retention: number | undefined, now: number): emulationstate {\n if (retention === undefined) return state;\n const layers = state.layers.map(layer => {\n if (layer.revertedat === undefined || layer.prior === undefined || layer.priorexpired === true) return layer;\n if (now - layer.revertedat <= retention) return layer;\n const { prior, ...metadata } = layer;\n void prior;\n return { ...metadata, priorexpired: true };\n });\n return { ...state, layers, updatedat: now };\n}\n\n/** Builds one shareable preset library file of the user curated presets; the version carries the library contract for imports through review. */\nexport function exportpresetlibrary(input: { devices: devicepreset[]; networks: networkpreset[]; locations: locationpreset[]; agents: agentpreset[]; now: number }): presetlibrary {\n return { version: 1, devices: [...input.devices], networks: [...input.networks], locations: [...input.locations], agents: [...input.agents], exportedat: input.now };\n}\n\n/** Parses one reviewed preset library file: every preset entry must pass its normalizer and a file without any valid preset is refused; unknown fields stay ignored. */\nexport function importpresetlibrary(value: unknown): presetlibrary | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const devices = (Array.isArray(entry.devices) ? entry.devices : []).flatMap(preset => { const parsed = devicepresetof(preset); return parsed !== undefined ? [parsed] : []; });\n const networks = (Array.isArray(entry.networks) ? entry.networks : []).flatMap(preset => { const parsed = networkpresetof(preset); return parsed !== undefined ? [parsed] : []; });\n const locations = (Array.isArray(entry.locations) ? entry.locations : []).flatMap(preset => { const parsed = locationpresetof(preset); return parsed !== undefined ? [parsed] : []; });\n const agents = (Array.isArray(entry.agents) ? entry.agents : []).flatMap(preset => { const parsed = agentpresetof(preset); return parsed !== undefined ? [parsed] : []; });\n if (devices.length + networks.length + locations.length + agents.length === 0) return undefined;\n return { version: typeof entry.version === \"number\" && Number.isInteger(entry.version) && entry.version >= 1 ? entry.version : 1, devices, networks, locations, agents, exportedat: typeof entry.exportedat === \"number\" ? entry.exportedat : Date.now() };\n}\n\n/** True when one approved location consent of that origin covers the reviewed coordinates; the prompt shows the exact latitude and longitude before the override applies. */\nexport function locationconsentcovers(origin: string, latitude: number, longitude: number, consents: locationconsent[]): boolean {\n return consents.some(consent => consent.origin === origin && consent.approved === true && consent.revokedat === undefined && consent.latitude === latitude && consent.longitude === longitude);\n}\n", "import type { consolediff, consoleentry, errorrecord, exchangerecord, loglevel, loglevelset, longtaskentry, netfailureentry, rejectionrecord, rotationrule, spamrule, stackframe, timelineentry, timelinesource } from \"./types.js\";\n\n/**\n * Run timeline logics for the 1.1.45 debugging family.\n * Every correlated rule for console capture with reviewed depth bounds and secret redaction, stack frame parsing, error, rejection and resource failure capture, long task attribution windows, the timeline binding to one run and its step ids, spam collapse with reviewed thresholds, log rotation without entry loss, level and source filters, the blocking duration per step window and the console diff between two runs lives in this file.\n * Console, error and task watching derives from page-injected listeners installed through the scripting api and the performance buffers, so no debugger permission exists anywhere in the manifest; redaction runs before any entry leaves the page bridge and captured text never carries reviewed secret patterns.\n */\n\n/** The debugging kinds, listed among the available capabilities of every proposal request. */\nexport const timelinekinds: string[] = [\"watchconsole\", \"watcherrors\", \"watchtasks\"];\n\n/** The reviewed level set, ordered from the most severe to the most verbose level. */\nexport const loglevels: loglevel[] = [\"error\", \"warn\", \"info\", \"log\", \"debug\", \"trace\"];\n\n/** The reviewed timeline source grammar: page console output, script errors, promise rejections, resource failures, long tasks, failed network requests and instrumented devtools protocol events. */\nexport const timelinesources: timelinesource[] = [\"console\", \"error\", \"rejection\", \"resource\", \"longtask\", \"network\", \"cdp\"];\n\n/** Ranks one level inside the reviewed level set; lower ranks are more severe. */\nexport function levelrank(level: loglevel): number {\n return loglevels.indexOf(level);\n}\n\n/** Redacts reviewed secret patterns from console text before any entry leaves the page bridge; matched patterns never survive into a stored entry. */\nexport function redactconsoletext(text: string, patterns: string[]): string {\n let redacted = text;\n for (const pattern of patterns) {\n if (!pattern) continue;\n while (redacted.includes(pattern)) redacted = redacted.replace(pattern, \"[redacted]\");\n }\n return redacted;\n}\n\n/** Classifies the kind tag of one console argument for the consoleentry argument kinds. */\nexport function argkind(value: unknown): string {\n if (value === null) return \"null\";\n if (Array.isArray(value)) return \"array\";\n if (value instanceof Error) return \"error\";\n switch (typeof value) {\n case \"string\": return \"string\";\n case \"number\": return \"number\";\n case \"boolean\": return \"boolean\";\n case \"bigint\": return \"bigint\";\n case \"symbol\": return \"symbol\";\n case \"function\": return \"function\";\n case \"undefined\": return \"undefined\";\n default: return \"object\";\n }\n}\n\n/** Serializes one console argument through the reviewed depth bound; deeper objects collapse to their constructor tag so the bound stays a user choice with no code ceiling. */\nexport function serializearg(value: unknown, depth: number): string {\n const render = (item: unknown, remaining: number): string => {\n if (item instanceof Error) return `${item.name}: ${item.message}`;\n if (typeof item === \"string\") return item;\n if (typeof item === \"function\") return `[function ${item.name || \"anonymous\"}]`;\n if (typeof item === \"bigint\") return `${item}n`;\n if (typeof item === \"symbol\") return item.toString();\n if (item === null || item === undefined || typeof item !== \"object\") return String(item);\n if (remaining <= 0) {\n const tag = Array.isArray(item) ? \"Array\" : (item as { constructor?: { name?: string } }).constructor?.name ?? \"Object\";\n return `[${tag}]`;\n }\n if (Array.isArray(item)) return `[${item.map(entry => render(entry, remaining - 1)).join(\", \")}]`;\n const record = item as Record<string, unknown>;\n return `{${Object.keys(record).map(key => `${key}: ${render(record[key], remaining - 1)}`).join(\", \")}}`;\n };\n return render(value, Math.max(0, depth));\n}\n\n/** Builds one captured console entry from a forwarded console call: the level, the redacted text of every serialized argument and the argument kinds, with redaction applied before the entry exists. */\nexport function consolecapture(input: { level: loglevel; args: unknown[]; depth: number; redact: string[] }): consoleentry {\n const parts = input.args.map(arg => serializearg(arg, input.depth));\n return { level: input.level, text: redactconsoletext(parts.join(\" \"), input.redact), argkinds: input.args.map(arg => argkind(arg)), repeat: 1 };\n}\n\n/** Parses one stack trace text into stack frames with function names, urls, lines and columns; unparseable lines are skipped instead of crashing capture. */\nexport function stackframes(stacktext: string): stackframe[] {\n const frames: stackframe[] = [];\n for (const row of stacktext.split(\"\\n\")) {\n const trimmed = row.trim();\n if (!trimmed.startsWith(\"at \")) continue;\n const body = trimmed.slice(3).trim();\n const location = body.match(/\\(([^()]*:\\d+:\\d+)\\)$/) ?? body.match(/^(.*:\\d+:\\d+)$/);\n const located = location?.[1];\n if (!located) continue;\n const segments = located.split(\":\");\n const column = Number.parseInt(segments.pop() ?? \"\", 10);\n const lineno = Number.parseInt(segments.pop() ?? \"\", 10);\n const url = segments.join(\":\");\n if (!Number.isFinite(lineno) || lineno < 0) continue;\n const name = body.endsWith(`(${located})`) ? body.slice(0, body.length - located.length - 2).trim() : \"\";\n frames.push({ ...(name ? { functionname: name } : {}), url, line: lineno, ...(Number.isFinite(column) ? { column } : {}) });\n }\n return frames;\n}\n\n/** Builds one captured javascript error record body from an error event: the redacted message, the parsed stack frames, the source url and the line. */\nexport function errorcapture(input: { message: string; sourceurl: string; line: number; stacktext?: string; redact: string[] }): Pick<errorrecord, \"message\" | \"frames\" | \"sourceurl\" | \"line\"> {\n return { message: redactconsoletext(input.message, input.redact), frames: input.stacktext !== undefined ? stackframes(input.stacktext) : [], sourceurl: input.sourceurl, line: input.line };\n}\n\n/** Builds one captured unhandled rejection record body from the rejection reason text and its stack frames, with the reason redacted before capture. */\nexport function rejectioncapture(input: { reason: string; stacktext?: string; redact: string[] }): Pick<rejectionrecord, \"reason\" | \"frames\"> {\n return { reason: redactconsoletext(input.reason, input.redact), frames: input.stacktext !== undefined ? stackframes(input.stacktext) : [] };\n}\n\n/** Builds the long task entry bodies of one watch window from performance longtask entries with their attribution names; the reviewed threshold filters entries below the user configured duration. */\nexport function longtaskcapture(input: { entries: Array<{ starttime: number; duration: number; attributions: string[] }>; threshold: number }): Array<Pick<longtaskentry, \"duration\" | \"starttime\" | \"attributions\">> {\n return input.entries.filter(entry => entry.duration >= input.threshold).map(entry => ({ duration: Math.round(entry.duration), starttime: Math.round(entry.starttime), attributions: [...entry.attributions] }));\n}\n\n/** One run timeline bound to its run and step ids. */\nexport interface timeline {\n runid: string;\n origin: string;\n stepids: string[];\n attachedat: number;\n entries: timelineentry[];\n}\n\n/** Binds one timeline to the run and its step ids; capture stays scoped to that run and every entry carries the run correlation id of its step. */\nexport function attachtimeline(input: { runid: string; origin: string; stepids: string[]; now: number }): timeline {\n return { runid: input.runid, origin: input.origin, stepids: [...input.stepids], attachedat: input.now, entries: [] };\n}\n\n/** Applies the reviewed level set to timeline entries: per step level floors drop entries more verbose than the floor and source filters drop entries from unreviewed sources. */\nexport function filterentries(entries: timelineentry[], levelset: loglevelset): timelineentry[] {\n return entries.filter(entry => {\n const floor = levelset.floors?.[entry.stepid] ?? levelset.floors?.[\"*\"];\n if (floor !== undefined && levelrank(entry.level) > levelrank(floor)) return false;\n if (levelset.sources !== undefined && levelset.sources.length > 0 && !levelset.sources.includes(entry.source)) return false;\n return true;\n });\n}\n\n/** Collapses repeated identical messages inside the reviewed spam window into counts and flags the patterns that exceed the reviewed collapse threshold. */\nexport function spamdetect(entries: timelineentry[], rule: spamrule): { entries: Array<timelineentry & { repeat: number }>; flagged: Array<{ message: string; count: number }> } {\n const collapsed: Array<timelineentry & { repeat: number }> = [];\n const counts = new Map<string, number>();\n for (const entry of entries) {\n if (rule.pattern !== \"\" && !entry.message.includes(rule.pattern)) { collapsed.push({ ...entry, repeat: 1 }); continue; }\n const key = `${entry.level}|${entry.source}|${entry.message}`;\n const previous = collapsed[collapsed.length - 1];\n if (previous && previous.repeat !== undefined && `${previous.level}|${previous.source}|${previous.message}` === key && entry.time - previous.time <= rule.windowsize) {\n previous.repeat += 1;\n continue;\n }\n collapsed.push({ ...entry, repeat: 1 });\n }\n for (const entry of collapsed) {\n if (entry.repeat > 1) counts.set(`${entry.level}|${entry.source}|${entry.message}`, entry.repeat);\n }\n const flagged = [...counts.entries()].filter(([, count]) => count > rule.collapse).map(([key, count]) => ({ message: key.split(\"|\").slice(2).join(\"|\"), count }));\n return { entries: collapsed, flagged };\n}\n\n/** Rotates the timeline of one run: the newest entries stay inside the reviewed max entries window while every overflow entry moves to the rotation target store without data loss. */\nexport function rotatelogs(entries: timelineentry[], rule: rotationrule): { kept: timelineentry[]; overflow: timelineentry[] } {\n if (entries.length <= rule.maxentries) return { kept: [...entries], overflow: [] };\n const kept = entries.slice(entries.length - rule.maxentries);\n const overflow = entries.slice(0, entries.length - rule.maxentries);\n return { kept, overflow };\n}\n\n/** Counts the timeline entries per level for the response envelope timeline block. */\nexport function timelinecounts(entries: timelineentry[]): Record<string, number> {\n const counts: Record<string, number> = {};\n for (const level of loglevels) counts[level] = 0;\n for (const entry of entries) counts[entry.level] = (counts[entry.level] ?? 0) + 1;\n return counts;\n}\n\n/** Sums the blocking duration of long task entries inside one step window; the window stays a reviewed value with no code ceiling. */\nexport function blockingduration(tasks: Array<{ starttime: number; duration: number }>, stepid: string, window: { startedat: number; endedat: number }): { stepid: string; blocking: number; tasks: number } {\n const inside = tasks.filter(task => task.starttime >= window.startedat && task.starttime <= window.endedat);\n return { stepid, blocking: inside.reduce((total, task) => total + task.duration, 0), tasks: inside.length };\n}\n\n/** Marks one failed request of the run in the timeline from its observed exchange: the url, status, error class and correlation id. */\nexport function netfailureentryof(input: { id: string; exchange: exchangerecord; at: number }): netfailureentry | null {\n const exchange = input.exchange;\n if (exchange.errorclass === undefined && exchange.status < 400) return null;\n return { id: input.id, runid: exchange.runid, stepid: exchange.stepid, url: exchange.url, status: exchange.status, errorclass: exchange.errorclass ?? \"httperror\", correlationid: exchange.correlationid, at: input.at };\n}\n\n/** Decides whether a watcher detached early because the run tab navigated inside its watch window: navigation timestamps inside the window detach the watcher at the navigation time. */\nexport function watcherdetached(input: { startedat: number; lifetime: number; navigations: number[] }): { detached: boolean; at?: number } {\n for (const navigation of input.navigations) {\n if (navigation >= input.startedat && navigation <= input.startedat + input.lifetime) return { detached: true, at: navigation };\n }\n return { detached: false };\n}\n\n/** Compares the console output of two runs and classifies every line as added, removed or repeated; repeated lines carry their repeat counts. */\nexport function consolediff(input: { baseid: string; targetid: string; baselines: string[]; targetlines: string[]; now: number }): consolediff {\n const base = input.baselines;\n const target = input.targetlines;\n const basemap = new Map<string, number>();\n for (const line of base) basemap.set(line, (basemap.get(line) ?? 0) + 1);\n const targetmap = new Map<string, number>();\n for (const line of target) targetmap.set(line, (targetmap.get(line) ?? 0) + 1);\n const lines = [];\n const added: string[] = [];\n const removed: string[] = [];\n const repeated: string[] = [];\n for (const [line, count] of targetmap) {\n const basecount = basemap.get(line) ?? 0;\n if (basecount === 0) {\n for (let index = 0; index < count; index += 1) { lines.push({ kind: \"added\" as const, text: line }); added.push(line); }\n continue;\n }\n const share = Math.min(basecount, count);\n for (let index = 0; index < share; index += 1) { lines.push({ kind: \"repeated\" as const, text: line, count: share }); repeated.push(line); }\n for (let index = share; index < count; index += 1) { lines.push({ kind: \"added\" as const, text: line }); added.push(line); }\n }\n for (const [line, count] of basemap) {\n const targetcount = targetmap.get(line) ?? 0;\n const missing = Math.max(0, count - targetcount);\n for (let index = 0; index < missing; index += 1) { lines.push({ kind: \"removed\" as const, text: line }); removed.push(line); }\n }\n return { base: input.baseid, target: input.targetid, lines, added: added.length, removed: removed.length, repeated: repeated.length, at: input.now };\n}\n", "import type { actionkind, agentbudget, agentmessage, agentplan, agentscope, agentsession, allowlistentry, approvaltimeout, attachtarget, blackboardentry, callratelimit, captureexport, costbudget, draftstep, modeloutput, plandraft, providerconfig, replanrecord, spawnrequest, taskqueue, captureformat, capturenaming, captureoptions, cdpallowlist, cleanuprule, clientidentity, clientrecord, consoleconsentrecord, debuggergrant, delaystep, downloadspec, editormodel, endpointconfig, fieldkind, formprofile, locationconsent, loglevel, loglevelset, mimefilter, mcpserverconfig, observationmode, permissionstate, policyevaluation, protocoleventsubscription, quarantineentry, regionrect, rotationrule, runsettings, safetyverdict, samplingrequest, siteoverride, sourcemapconsent, spamrule, steptemplate, toolcallrecord, toolcatalog, tooldef, toolmock, toolnamespace, toolstep, tooldryrun, transformrule, waitstep, watchdogconfig, workflowrecord, workflowstep } from \"./types.js\";\nimport { domainkinds, toolnamespaces } from \"./toolcatalog.js\";\nimport { channeloptionsof, channelorigin, pollcursorof, subscriptionoptionsof } from \"./socketbus.js\";\nimport { apireplayspecof, privatemime } from \"./netwatch.js\";\nimport { blockruleof, cookiedomaingranted, cookierecordof, mockspecof, patternorigin, proxyrouteof, headeruleof } from \"./netcontrol.js\";\nimport { allowlistcovers, breakpointinputof, cdpallowlistof, cdpdomains, cdpeventruleof, methoddomain, overrideinputof, stepmodeof, teardownplanof, watchexpressionof } from \"./cdpbus.js\";\nimport { annotationof, attachtargetof, flowspecof, tracecategories } from \"./profilers.js\";\nimport { agentgrammarvalid, agentpresetof, blackboxruleof, browserpermissions, devicepresetof, familyofkind, locationconsentcovers, locationpresetof, locationrangevalid, networkpresetof, permissiongrantof, permissiongrade, permissionstates, revertplanof } from \"./emulation.js\";\nimport { autointervalof, importsessionfile, restoreplanof, searchqueryof, sessionkinds, snapshotplanof } from \"./sessions.js\";\nimport { composeworkflow, expressionof, expressionoperators, regexruleof, steptemplateof, validateworkflow, workflowblockof, workflowstepof } from \"./workflow.js\";\nimport { branchof, conditionof, controlsteps, foreachof, iscontrolflowkind, loopof, parallelof, repeatuntilof, tryof, whileof } from \"./controlflow.js\";\nimport { armrule, cronparse, triggerfamilyof, triggerpayloadof, triggereventcatalog, webhooksecretok } from \"./trigger.js\";\nimport { formpayloadof, multipartpayloadof, oauthflowof } from \"./netauth.js\";\nimport { loglevels, timelinesources } from \"./runtimeline.js\";\n\nconst sensitiveactions = new Set<actionkind>([\"click\", \"type\", \"navigate\", \"select\", \"presskey\", \"drag\", \"drop\", \"upload\", \"clear\", \"check\", \"uncheck\", \"toggle\", \"submit\", \"reload\", \"back\", \"forward\", \"writestorage\", \"setattribute\", \"removeattribute\", \"evaluate\", \"tabcreate\", \"tabactivate\", \"tabclose\", \"tabreload\", \"windowcreate\", \"windowclose\", \"windowresize\", \"downloadfile\", \"clickpoint\", \"shiftclick\", \"dismissdialog\", \"enterframe\", \"typetime\", \"appendtext\", \"setvalue\", \"typeedit\", \"keyhold\", \"keyrelease\", \"submitsearch\", \"selectmulti\", \"chooseradio\", \"setslider\", \"setdate\", \"setcolor\", \"openlink\", \"openprivate\", \"reloadcache\", \"stopnav\", \"followlink\", \"spanav\", \"rewritequery\", \"setfragment\", \"navlist\", \"navprofile\", \"handleauth\", \"printpdf\", \"prefetch\", \"preconnect\", \"deeplink\", \"reopentab\", \"pausenav\", \"navrate\", \"openclipboard\", \"batchopen\", \"duplicatetab\", \"closepattern\", \"pintab\", \"mutetab\", \"movetab\", \"movetabwindow\", \"grouptabs\", \"colorgroup\", \"collapsegroup\", \"discardtab\", \"reloadtabs\", \"zoomin\", \"zoomout\", \"switchtab\", \"maximizewindow\", \"minimizewindow\", \"restorewindow\", \"focuswindow\", \"scratchwindow\", \"incognitowindow\", \"restoretab\", \"restorelayout\", \"reopenrun\", \"badgetab\", \"fillform\", \"filllabel\", \"fillplaceholder\", \"submitform\", \"retryform\", \"runwizard\", \"selectchain\", \"picktypeahead\", \"pickdate\", \"attachfile\", \"fillcard\", \"fillcode\", \"consentpassword\", \"exportcsv\", \"exportjson\", \"exportexcel\", \"copytable\", \"pushsheets\", \"streamdisk\", \"paginateextract\", \"resumeextract\", \"batchdownload\", \"pausedownload\", \"resumedownload\", \"interceptmime\", \"readclipboard\", \"writeclipboard\", \"copyscreen\", \"quarantinedownload\", \"scanvirus\", \"cleanupartifacts\", \"recordscreen\", \"captureaudio\", \"downloadimages\", \"callrest\", \"callgraphql\", \"sendmessage\", \"blockrequest\", \"mockresponse\", \"rewriteheaders\", \"setcookies\", \"clearcookies\", \"authflow\", \"saveapikey\", \"routeproxy\", \"postform\", \"postfiles\", \"attachcdp\", \"detachcdp\", \"cdpcmd\", \"overridescript\", \"heapshot\", \"profilecpu\", \"capturesourcemaps\", \"emulatedevice\", \"emulatenetwork\", \"emulatelocate\", \"setuseragent\", \"overridepermission\", \"restoresession\", \"exportsessions\", \"importsessions\", \"runworkflow\", \"visitrule\", \"urlrule\", \"menurule\", \"keyrule\", \"buttonrule\", \"cronrule\", \"intervalrule\", \"urllistrule\", \"webhookrule\", \"eventrule\"]);\nconst interactionactions = new Set<actionkind>([\"focus\", \"scroll\", \"hover\", \"clickdeep\", \"rightclick\", \"doubleclick\", \"scrollpage\", \"scrollby\", \"scrollend\", \"scrolltop\", \"fullscreen\", \"zoomset\", \"movepointer\", \"clicktext\", \"clickaria\", \"clickname\", \"expanddetails\", \"pierceshadow\", \"retryaction\", \"capturebodies\", \"setbreakpoint\", \"stepcode\", \"watchexpr\", \"loop\", \"repeatuntil\", \"whileloop\", \"foreach\", \"parallel\", \"trycatch\"]);\nconst readactions = new Set<actionkind>([\"observe\", \"inspect\", \"extract\", \"wait\", \"waitfor\", \"waittext\", \"readattribute\", \"readstyle\", \"readgeometry\", \"readvalue\", \"readtext\", \"readhtml\", \"countelements\", \"readtable\", \"readlinks\", \"readimages\", \"readmeta\", \"readforms\", \"readstorage\", \"highlight\", \"tablist\", \"windowlist\", \"tabsnapshot\", \"mapclicks\", \"verifyvisible\", \"verifyenabled\", \"resolvexpath\", \"a11ytree\", \"readvisible\", \"readertree\", \"detectlists\", \"detecttables\", \"readjson\", \"watchmutate\", \"waitquiet\", \"watchbanner\", \"detectinfinitescroll\", \"detectvirtual\", \"detectlazy\", \"readscrollpos\", \"readlang\", \"readoutline\", \"countpages\", \"listshadow\", \"listframes\", \"classifypage\", \"fingerprintsection\", \"diffsnapshots\", \"readselection\", \"watchfocus\", \"detectsticky\", \"detectscrolllock\", \"readopengraph\", \"detectlanguage\", \"deriveselector\", \"waitload\", \"waiturl\", \"spawait\", \"detecthttp\", \"readredirects\", \"readfinalurl\", \"trailaudit\", \"navintent\", \"checksafe\", \"querytabs\", \"watchtab\", \"findclones\", \"searchtabs\", \"listaudio\", \"snapshotsession\", \"savelayout\", \"attachmeta\", \"detectfields\", \"generatevalues\", \"saveprofiles\", \"asksubmit\", \"readerrors\", \"skiphoneypot\", \"detectlogin\", \"detecttemplate\", \"handoffcaptcha\", \"scrapetable\", \"importcsv\", \"looprows\", \"transformvalues\", \"deduperows\", \"mergepages\", \"stamplerows\", \"previewgrid\", \"logprovenance\", \"verifydownload\", \"exportnetlog\", \"namecaptures\", \"shotview\", \"shotfullpage\", \"shotelement\", \"shotregion\", \"contactsheet\", \"capturepdf\", \"captureframe\", \"readmedia\", \"readassets\", \"probestream\", \"timelapse\", \"shotcanvas\", \"convertimage\", \"makethumbs\", \"fetchurl\", \"parsejson\", \"parsehtml\", \"opensocket\", \"waitmessage\", \"watchrequests\", \"readheaders\", \"mapapi\", \"subscribesse\", \"longpoll\", \"extractapi\", \"readcookies\", \"watchconsole\", \"watcherrors\", \"watchtasks\", \"watchcdp\", \"measureflow\", \"trackmemory\", \"watchshifts\", \"traceload\", \"annotatetrace\", \"replaytrace\", \"blackboxscripts\", \"persiststate\", \"capturesession\", \"namedsessions\", \"diffsessions\", \"searchsessions\", \"composeworkflow\", \"savetemplate\", \"dryrun\", \"delay\", \"waitelement\", \"compute\", \"extractvars\", \"listruns\", \"condition\", \"branch\"]);\nconst allowedactions = new Set<actionkind>([...sensitiveactions, ...interactionactions, ...readactions]);\nconst watchactions = new Set<actionkind>([\"watchmutate\", \"watchbanner\", \"watchfocus\", \"watchtab\"]);\nconst targetactions = new Set<actionkind>([\"inspect\", \"focus\", \"click\", \"type\", \"scroll\", \"select\", \"hover\", \"clickdeep\", \"rightclick\", \"doubleclick\", \"drag\", \"drop\", \"upload\", \"clear\", \"check\", \"uncheck\", \"toggle\", \"submit\", \"readattribute\", \"readstyle\", \"readgeometry\", \"readvalue\", \"readtext\", \"readhtml\", \"countelements\", \"readtable\", \"highlight\", \"setattribute\", \"removeattribute\", \"waitfor\", \"shiftclick\", \"typetime\", \"appendtext\", \"setvalue\", \"typeedit\", \"submitsearch\", \"selectmulti\", \"chooseradio\", \"setslider\", \"setdate\", \"setcolor\", \"expanddetails\", \"verifyvisible\", \"verifyenabled\", \"pierceshadow\", \"deriveselector\", \"fingerprintsection\", \"submitform\", \"retryform\", \"selectchain\", \"picktypeahead\", \"pickdate\", \"attachfile\", \"fillcode\", \"consentpassword\", \"scrapetable\", \"paginateextract\", \"shotelement\", \"captureframe\", \"shotcanvas\"]);\nconst valueactions = new Set<actionkind>([\"presskey\", \"drag\", \"drop\", \"upload\", \"readattribute\", \"removeattribute\", \"waittext\", \"evaluate\", \"zoomset\", \"tabactivate\", \"tabclose\", \"tabreload\", \"windowclose\", \"windowresize\", \"tabcreate\", \"windowcreate\", \"downloadfile\", \"typetime\", \"appendtext\", \"setvalue\", \"typeedit\", \"keyhold\", \"keyrelease\", \"chooseradio\", \"setslider\", \"setdate\", \"setcolor\", \"followlink\", \"setfragment\", \"handleauth\", \"navintent\", \"openclipboard\", \"checksafe\", \"reopentab\", \"spanav\", \"duplicatetab\", \"pintab\", \"mutetab\", \"movetab\", \"movetabwindow\", \"searchtabs\", \"badgetab\", \"attachmeta\", \"focuswindow\", \"maximizewindow\", \"minimizewindow\", \"restorewindow\", \"incognitowindow\", \"asksubmit\", \"selectchain\", \"picktypeahead\", \"pickdate\", \"attachfile\", \"fillcode\", \"consentpassword\", \"pausedownload\", \"resumedownload\", \"verifydownload\", \"writeclipboard\", \"quarantinedownload\", \"scanvirus\"]);\nconst tabscommandactions = new Set<actionkind>([\"querytabs\", \"duplicatetab\", \"closepattern\", \"pintab\", \"mutetab\", \"movetab\", \"movetabwindow\", \"grouptabs\", \"colorgroup\", \"collapsegroup\", \"discardtab\", \"reloadtabs\", \"zoomin\", \"zoomout\", \"watchtab\", \"switchtab\", \"maximizewindow\", \"minimizewindow\", \"restorewindow\", \"focuswindow\", \"scratchwindow\", \"incognitowindow\", \"restoretab\", \"savelayout\", \"restorelayout\", \"findclones\", \"searchtabs\", \"badgetab\", \"attachmeta\", \"listaudio\", \"reopenrun\", \"snapshotsession\"]);\nconst formactions = new Set<actionkind>([\"fillform\", \"filllabel\", \"fillplaceholder\", \"detectfields\", \"generatevalues\", \"saveprofiles\", \"asksubmit\", \"submitform\", \"readerrors\", \"retryform\", \"runwizard\", \"selectchain\", \"picktypeahead\", \"pickdate\", \"attachfile\", \"handoffcaptcha\", \"fillcard\", \"fillcode\", \"consentpassword\", \"skiphoneypot\", \"detectlogin\", \"detecttemplate\"]);\n/** Extraction, transform, export and provenance kinds of the forms and data part two family. */\nconst datasetactions = new Set<actionkind>([\"scrapetable\", \"exportcsv\", \"exportjson\", \"exportexcel\", \"copytable\", \"pushsheets\", \"importcsv\", \"looprows\", \"transformvalues\", \"deduperows\", \"paginateextract\", \"mergepages\", \"stamplerows\", \"previewgrid\", \"streamdisk\", \"resumeextract\", \"logprovenance\"]);\n/** Export kinds that move extracted data out of local memory to disk, the clipboard or a reviewed sheet endpoint. */\nconst exportactions = new Set<actionkind>([\"exportcsv\", \"exportjson\", \"exportexcel\", \"copytable\", \"pushsheets\", \"streamdisk\"]);\n/** Files, clipboard and downloads kinds of the batch queue, interception, clipboard, quarantine, naming and cleanup family. */\nconst filesactions = new Set<actionkind>([\"batchdownload\", \"pausedownload\", \"resumedownload\", \"verifydownload\", \"interceptmime\", \"exportnetlog\", \"readclipboard\", \"writeclipboard\", \"copyscreen\", \"quarantinedownload\", \"scanvirus\", \"namecaptures\", \"cleanupartifacts\"]);\nconst captureactions = new Set<actionkind>([\"shotview\", \"shotfullpage\", \"shotelement\", \"shotregion\", \"contactsheet\"]);\n/** Media capture part two kinds of the pdf, recording, image, canvas, stream, asset, lapse, conversion and thumbnail family. */\nconst mediaactions = new Set<actionkind>([\"capturepdf\", \"recordscreen\", \"captureaudio\", \"captureframe\", \"downloadimages\", \"shotcanvas\", \"probestream\", \"readmedia\", \"readassets\", \"timelapse\", \"convertimage\", \"makethumbs\"]);\n\nconst httpactions = new Set<actionkind>([\"fetchurl\", \"parsejson\", \"parsehtml\", \"callrest\", \"callgraphql\"]);\n\nconst socketactions = new Set<actionkind>([\"opensocket\", \"sendmessage\", \"waitmessage\", \"subscribesse\", \"longpoll\"]);\n\nconst netwatchactions = new Set<actionkind>([\"watchrequests\", \"readheaders\", \"capturebodies\", \"mapapi\", \"extractapi\"]);\n\n/** Network control kinds of the 1.1.44 family: blocking, mocking, header rewriting, cookies, auth, api keys, proxy routing and uploads. */\nconst controlactions = new Set<actionkind>([\"blockrequest\", \"mockresponse\", \"rewriteheaders\", \"setcookies\", \"readcookies\", \"clearcookies\", \"authflow\", \"saveapikey\", \"routeproxy\", \"postform\", \"postfiles\"]);\n\n/** Debugging kinds of the 1.1.45 family: console, error and task watching stays read only timeline capture. */\nconst debugactions = new Set<actionkind>([\"watchconsole\", \"watcherrors\", \"watchtasks\"]);\n\n/** The devtools protocol kinds of the 1.1.46 debugging family: attach, detach, raw commands, event watches, breakpoints, stepping, watch expressions and script overrides. */\nconst cdpactions = new Set<actionkind>([\"attachcdp\", \"detachcdp\", \"cdpcmd\", \"watchcdp\", \"setbreakpoint\", \"stepcode\", \"watchexpr\", \"overridescript\"]);\n\n/** The profiling kinds of the 1.1.47 debugging part three family: flow measurement, heap snapshots, memory growth tracking, cpu profiles, layout shift watches, trace records, trace annotation, offline trace replay and source map capture. */\nconst profileractions = new Set<actionkind>([\"measureflow\", \"heapshot\", \"trackmemory\", \"profilecpu\", \"watchshifts\", \"traceload\", \"annotatetrace\", \"replaytrace\", \"capturesourcemaps\"]);\n\nconst emulationactions = new Set<actionkind>([\"emulatedevice\", \"emulatenetwork\", \"emulatelocate\", \"setuseragent\", \"overridepermission\", \"blackboxscripts\"]);\n\n/** The session memory kinds of the 1.1.49 family: task state persistence, session capture, restore, naming, diffing, search, export and import. */\nconst sessionactions = new Set<actionkind>([\"persiststate\", \"capturesession\", \"restoresession\", \"namedsessions\", \"diffsessions\", \"searchsessions\", \"exportsessions\", \"importsessions\"]);\n\n/** The workflow kinds of the 1.1.50 and 1.1.51 families: composition, templates, runs, dry runs, jittered delays, element waits, expressions, variable extraction, and the control flow family of conditionals, branching, loops, parallel branches with joins and try catch with retries and timeouts. */\nconst workflowactions = new Set<actionkind>([\"composeworkflow\", \"savetemplate\", \"runworkflow\", \"dryrun\", \"delay\", \"waitelement\", \"compute\", \"extractvars\", \"condition\", \"branch\", \"loop\", \"repeatuntil\", \"whileloop\", \"foreach\", \"parallel\", \"trycatch\"]);\n\n/** The trigger kinds of the 1.1.52 family: page visit, url pattern, context menu, keyboard shortcut, toolbar button, cron, interval, url list, webhook and page event rules that launch reviewed workflows; every rule arms behind the explicit arm review and grades sensitive because it launches runs automatically. */\nconst triggeractions = new Set<actionkind>([\"visitrule\", \"urlrule\", \"menurule\", \"keyrule\", \"buttonrule\", \"cronrule\", \"intervalrule\", \"urllistrule\", \"webhookrule\", \"eventrule\"]);\n\n/** Header names that carry credentials; sending any of them needs the explicit consent that names the header. */\nconst credentialheaders = new Set([\"authorization\", \"proxy-authorization\", \"cookie\", \"cookie2\", \"set-cookie\", \"api-key\", \"x-api-key\", \"x-auth-token\", \"x-session-token\", \"proxy-authorization\"]);\n/** Field kinds the form grammar accepts inside records, profiles and value rules. */\nconst fieldkinds: fieldkind[] = [\"text\", \"email\", \"phone\", \"date\", \"number\", \"select\", \"check\", \"radio\", \"file\", \"password\", \"card\", \"code\"];\nconst layoutmutationactions = new Set<actionkind>([\"grouptabs\", \"colorgroup\", \"collapsegroup\", \"savelayout\", \"restorelayout\"]);\n/** Chromium tab group colors accepted as reviewed group color choices. */\nconst groupcolors = [\"grey\", \"blue\", \"red\", \"yellow\", \"green\", \"pink\", \"purple\", \"cyan\", \"orange\"];\n\n/** Normalizes a user supplied HTTPS endpoint without preserving a provider lock-in. */\nexport function normalizeendpoint(value: string): endpointconfig {\n const endpoint = new URL(value.trim());\n if (endpoint.protocol !== \"https:\") throw new Error(\"Devthink accepts HTTPS endpoints only.\");\n if (endpoint.username || endpoint.password) throw new Error(\"Endpoint credentials are not allowed in the URL.\");\n return { endpoint: endpoint.toString(), origin: endpoint.origin, configuredat: Date.now() };\n}\n\n/** Creates the exact optional host pattern requested from Chromium. */\nexport function hostpattern(origin: string): string {\n const parsed = new URL(origin);\n if (parsed.protocol !== \"https:\") throw new Error(\"Only HTTPS origins can be granted.\");\n return `${parsed.origin}/*`;\n}\n\n/** True when the kind belongs to the session memory family of the 1.1.49 release. */\nexport function issessionkind(kind: actionkind): boolean {\n return sessionactions.has(kind);\n}\n\n/** True when the kind belongs to the workflow family of the 1.1.50 release. */\nexport function isworkflowkind(kind: actionkind): boolean {\n return workflowactions.has(kind);\n}\n\n/** True when the kind belongs to the trigger family of the 1.1.52 release: every trigger kind arms an automatic launcher and needs the explicit arm review. */\nexport function istriggeraction(kind: actionkind): boolean {\n return triggeractions.has(kind);\n}\n\n/** True when the action kind observes the page over a reviewed lifetime window. */\nexport function iswatchkind(kind: actionkind): boolean {\n return watchactions.has(kind);\n}\n\n/** True when the kind belongs to the debugging family of console, error and task watching. */\nexport function isdebugkind(kind: actionkind): boolean {\n return debugactions.has(kind);\n}\n\n/** True when the kind belongs to the devtools protocol family of attaches, raw commands, event watches, breakpoints, stepping, watch expressions and script overrides. */\nexport function iscdpkind(kind: actionkind): boolean {\n return cdpactions.has(kind);\n}\n\n/** True when the kind belongs to the profiling family of flow, heap, cpu, shift, trace and source map instruments. */\nexport function isprofilekind(kind: actionkind): boolean {\n return profileractions.has(kind);\n}\n\n/** True when the kind belongs to the emulation family of device, network, location, agent and permission layers plus blackbox trace shaping. */\nexport function isemulationkind(kind: actionkind): boolean {\n return emulationactions.has(kind);\n}\n\n/** Grades the observation mode of a kind: passive capture, watched lifetimes or diffing passes. */\nexport function observationmodeof(kind: actionkind): observationmode {\n if (watchactions.has(kind) || debugactions.has(kind) || profileractions.has(kind) && kind !== \"heapshot\" && kind !== \"replaytrace\" && kind !== \"annotatetrace\" && kind !== \"capturesourcemaps\" && kind !== \"profilecpu\" || cdpactions.has(kind) && kind === \"watchcdp\" || kind === \"waitquiet\") return \"watching\";\n if (kind === \"diffsnapshots\") return \"diffing\";\n return \"passive\";\n}\n\n/** Defines action risk from the fixed local allowlist. */\nexport function actionrisk(kind: actionkind): \"read\" | \"interaction\" | \"sensitive\" {\n if (!allowedactions.has(kind)) throw new Error(\"Unsupported browser action.\");\n if (sensitiveactions.has(kind)) return \"sensitive\";\n return interactionactions.has(kind) ? \"interaction\" : \"read\";\n}\n\n/** True when the action kind accepts a css selector target or a reviewed targetref. */\nexport function needstarget(kind: actionkind): boolean {\n return targetactions.has(kind);\n}\n\n/** Parses the reviewed JSON options of a step; malformed payloads are rejected early. */\nexport function parseoptions(step: toolstep): Record<string, unknown> {\n if (step.options === undefined) return {};\n let parsed: unknown;\n try { parsed = JSON.parse(step.options); } catch { throw new Error(\"Step options must be a JSON object.\"); }\n if (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) throw new Error(\"Step options must be a JSON object.\");\n return parsed as Record<string, unknown>;\n}\n\n/** Maps an action kind to the optional browser permission it requires, if any. */\nexport function requiredcapability(kind: actionkind): string | undefined {\n if (kind === \"tablist\") return \"tabs\";\n if (kind === \"downloadfile\") return \"downloads\";\n if (kind === \"openclipboard\") return \"clipboardRead\";\n if (kind === \"copytable\") return \"clipboardWrite\";\n if (kind === \"batchdownload\" || kind === \"pausedownload\" || kind === \"resumedownload\" || kind === \"verifydownload\" || kind === \"interceptmime\" || kind === \"quarantinedownload\" || kind === \"scanvirus\") return \"downloads\";\n if (kind === \"readclipboard\") return \"clipboardRead\";\n if (kind === \"writeclipboard\" || kind === \"copyscreen\") return \"clipboardWrite\";\n if (kind === \"downloadimages\") return \"downloads\";\n if (kind === \"authflow\") return \"tabs\";\n if (kind === \"capturesession\" || kind === \"restoresession\") return \"tabs\";\n if (kind === \"exportsessions\") return \"downloads\";\n if (kind === \"openlink\" || kind === \"openprivate\" || kind === \"navlist\" || kind === \"batchopen\" || kind === \"reopentab\" || kind === \"deeplink\") return \"tabs\";\n if (tabscommandactions.has(kind)) return \"tabs\";\n return undefined;\n}\n\n/** True when the kind commands tabs or windows beyond the active tab and needs the optional tabs capability. */\nexport function istabscommandkind(kind: actionkind): boolean {\n return tabscommandactions.has(kind);\n}\n\n/** True when the kind mutates tab groups or layouts and therefore stays inside the active session. */\nexport function islayoutkind(kind: actionkind): boolean {\n return layoutmutationactions.has(kind);\n}\n\n/** True when the kind belongs to the forms and data family. */\nexport function isformkind(kind: actionkind): boolean {\n return formactions.has(kind);\n}\n\n/** True when the kind belongs to the extraction, transform, export and provenance family. */\nexport function isdatasetkind(kind: actionkind): boolean {\n return datasetactions.has(kind);\n}\n\n/** True when the kind exports extracted data out of local memory to disk, the clipboard or a reviewed sheet endpoint. */\nexport function isexportkind(kind: actionkind): boolean {\n return exportactions.has(kind);\n}\n\n/** True when the kind belongs to the files, clipboard and downloads family. */\nexport function isfileskind(kind: actionkind): boolean {\n return filesactions.has(kind);\n}\n\n/** True when the kind belongs to the media capture family of viewport, full page, element, region and contact sheet shots. */\nexport function iscapturekind(kind: actionkind): boolean {\n return captureactions.has(kind);\n}\n\n/** Requires the active tab grant of the live session before any capture kind runs: the session tab and origin must match and the origin grant must cover the active origin. */\nexport function capturegate(session: agentsession | undefined, tabid: number, origin: string, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the capture.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot capture.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot capture.\" };\n if (session.tabid !== tabid) return { allowed: false, reason: `The capture needs the active tab grant of session tab ${session.tabid} and refuses tab ${tabid}.` };\n if (!origingranted(session, origin)) return { allowed: false, reason: `The capture of ${origin} needs the session origin grants first.` };\n return { allowed: true };\n}\n\n/** Validates one reviewed capture options payload: format inside the png, jpeg and webp set, quality bounded only by the format range, pixel ratio from one up with no code ceiling, and a known export target. */\nexport function validatecaptureoptions(value: unknown): policyevaluation {\n if (value === undefined) return { allowed: true };\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"The reviewed capture options must be an object in options.capture.\" };\n const options = value as Record<string, unknown>;\n if (options.format !== undefined && options.format !== \"png\" && options.format !== \"jpeg\" && options.format !== \"webp\") return { allowed: false, reason: \"The reviewed capture format must be png, jpeg or webp.\" };\n if (options.quality !== undefined && (typeof options.quality !== \"number\" || !Number.isFinite(options.quality) || options.quality < 0 || options.quality > 100)) return { allowed: false, reason: \"The reviewed capture quality must stay between zero and one hundred; any value in that range is the user choice with no code cap.\" };\n if (options.pixelratio !== undefined && (typeof options.pixelratio !== \"number\" || !Number.isFinite(options.pixelratio) || options.pixelratio < 1)) return { allowed: false, reason: \"The reviewed pixel ratio starts at one and climbs to any user configured ceiling with no code ceiling.\" };\n if (options.annotate !== undefined && typeof options.annotate !== \"boolean\") return { allowed: false, reason: \"The reviewed capture annotation flag must be a boolean.\" };\n if (options.exporttarget !== undefined && options.exporttarget !== \"memory\" && options.exporttarget !== \"download\" && options.exporttarget !== \"clipboard\") return { allowed: false, reason: \"The reviewed capture export target must be memory, download or clipboard.\" };\n return { allowed: true };\n}\n\n/** Validates one reviewed region rectangle in css pixels; negative coordinates and non positive sizes are refused. */\nexport function validateregionrect(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed regionrect with x, y, width and height in css pixels is required in options.\" };\n const rect = value as Record<string, unknown>;\n for (const field of [\"x\", \"y\", \"width\", \"height\"]) {\n if (typeof rect[field] !== \"number\" || !Number.isFinite(rect[field] as number)) return { allowed: false, reason: `The reviewed regionrect needs a numeric ${field} in css pixels.` };\n }\n if ((rect.x as number) < 0 || (rect.y as number) < 0) return { allowed: false, reason: \"The reviewed regionrect refuses negative coordinates.\" };\n if ((rect.width as number) <= 0 || (rect.height as number) <= 0) return { allowed: false, reason: \"The reviewed regionrect needs positive width and height values.\" };\n return { allowed: true };\n}\n\n/** Validates one reviewed capture naming rule against the allowed segment set: run, step, sequence and kind flags only. */\nexport function validatecapturenaming(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed capturenaming rule with run, step, sequence and kind flags is required.\" };\n const rule = value as Record<string, unknown>;\n const segments = [\"run\", \"step\", \"sequence\", \"kind\"];\n for (const key of Object.keys(rule)) {\n if (!segments.includes(key)) return { allowed: false, reason: `The reviewed capturenaming rule refuses the unknown ${key} segment; only run, step, sequence and kind participate.` };\n }\n for (const segment of segments) {\n if (rule[segment] !== undefined && typeof rule[segment] !== \"boolean\") return { allowed: false, reason: `The reviewed capturenaming ${segment} flag must be a boolean.` };\n }\n if (!segments.some(segment => rule[segment] === true)) return { allowed: false, reason: \"The reviewed capturenaming rule needs at least one enabled segment of run, step, sequence and kind.\" };\n return { allowed: true };\n}\n\n/** Routes the capture export target: memory stays local, clipboard needs the clipboardwrite grant and disk writes only run through the reviewed download flow. */\nexport function captureexportgranted(target: captureexport | undefined): policyevaluation {\n if (target === undefined || target === \"memory\") return { allowed: true };\n if (target === \"clipboard\") return { allowed: true, reason: \"The clipboard capture export runs behind the optional clipboardwrite capability, negotiated through the permissions api before the copy.\" };\n if (target === \"download\") return { allowed: true, reason: \"The download capture export runs only through the reviewed download flow behind the optional downloads capability.\" };\n return { allowed: false, reason: \"The capture export target must be memory, download or clipboard; no other disk route exists.\" };\n}\n\n/** Keeps the stitching scroll budget inside the reviewed wait window: the settle windows of every tile must fit the reviewed wait window with no code ceiling on either side. */\nexport function stitchbudgetallowed(tiles: number, settle: number, wait: number): policyevaluation {\n if (tiles <= 0) return { allowed: false, reason: \"The stitch budget needs at least one tile.\" };\n if (settle < 0 || wait < 0) return { allowed: false, reason: \"The reviewed settle and wait windows must be zero or positive milliseconds.\" };\n if (tiles * settle > wait) return { allowed: false, reason: `The stitching scroll budget of ${tiles} tiles at ${settle} milliseconds exceeds the reviewed wait window of ${wait} milliseconds; review a wider window or a smaller settle.` };\n return { allowed: true };\n}\n\n/** Allows beforeafter state capture to wrap any existing action kind except the capture kinds themselves; pixel evidence around sensitive actions grades as reviewable evidence. */\nexport function beforeafterwrapallowed(kind: actionkind): boolean {\n return allowedactions.has(kind) && !captureactions.has(kind);\n}\n\n/** Exposes the capture retention window as a user configured choice; an absent value keeps every capture byte forever with no code ceiling. */\nexport function captureretentionwindow(settings: runsettings | undefined): number | undefined {\n return settings?.captureretention;\n}\n\n/** Validates the reviewed media capture parameter grammar; pixel ratios, quality values, cell counts and retention windows stay user choices with no code ceilings. */\nfunction validatecapturegrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n const optioncheck = validatecaptureoptions(options.capture);\n if (!optioncheck.allowed) return optioncheck;\n if (options.settle !== undefined && (typeof options.settle !== \"number\" || !Number.isFinite(options.settle) || options.settle < 0)) return { allowed: false, reason: \"The reviewed capture settle window must be zero or a positive number of milliseconds.\" };\n if (options.overlap !== undefined && (typeof options.overlap !== \"number\" || !Number.isInteger(options.overlap) || options.overlap < 0)) return { allowed: false, reason: \"The reviewed stitch overlap must be zero or a positive number of rows.\" };\n if (options.wait !== undefined && (typeof options.wait !== \"number\" || !Number.isFinite(options.wait) || options.wait < 0)) return { allowed: false, reason: \"The reviewed capture wait window must be zero or a positive number of milliseconds.\" };\n if (options.naming !== undefined) {\n const namingcheck = validatecapturenaming(options.naming);\n if (!namingcheck.allowed) return namingcheck;\n }\n if (kind === \"shotregion\") {\n const rectcheck = validateregionrect(options.regionrect);\n if (!rectcheck.allowed) return rectcheck;\n if (options.reviewed !== true) return { allowed: false, reason: \"Every reviewed regionrect needs the explicit reviewed flag before shotregion runs.\" };\n if (options.container !== undefined && !isnonempty(options.container)) return { allowed: false, reason: \"The reviewed scrollable container selector must be a non-empty string.\" };\n if (options.steps !== undefined && (typeof options.steps !== \"number\" || !Number.isInteger(options.steps) || options.steps < 1)) return { allowed: false, reason: \"The reviewed container scroll steps must be a positive integer with no code ceiling.\" };\n }\n if (kind === \"contactsheet\") {\n const elements = options.elements;\n if (!Array.isArray(elements) || elements.length === 0 || !elements.every(item => isnonempty(item))) return { allowed: false, reason: \"A reviewed non-empty list of element selectors is required in options for the contact sheet; the cell count stays the user choice.\" };\n const layout = options.sheet;\n if (layout !== undefined) {\n if (!layout || typeof layout !== \"object\" || Array.isArray(layout)) return { allowed: false, reason: \"The reviewed sheetlayout must be an object with cellsize, columns and label.\" };\n const sheet = layout as Record<string, unknown>;\n if (typeof sheet.cellsize !== \"number\" || !Number.isFinite(sheet.cellsize) || sheet.cellsize <= 0) return { allowed: false, reason: \"The reviewed contact sheet cell size must be a positive number of pixels.\" };\n if (typeof sheet.columns !== \"number\" || !Number.isInteger(sheet.columns) || sheet.columns < 1) return { allowed: false, reason: \"The reviewed contact sheet column count must be a positive integer with no code ceiling.\" };\n if (sheet.label !== undefined && sheet.label !== \"none\" && sheet.label !== \"index\" && sheet.label !== \"selector\" && sheet.label !== \"both\") return { allowed: false, reason: \"The reviewed contact sheet label style must be none, index, selector or both.\" };\n }\n }\n return { allowed: true };\n}\n\n/** Refuses any export that would leave local memory while the session origin grants do not cover the active origin. */\nexport function exportgranted(session: agentsession | undefined, origin: string): policyevaluation {\n if (!origingranted(session, origin)) return { allowed: false, reason: `The export of extracted data from ${origin} needs the session origin grants before it leaves local memory.` };\n return { allowed: true };\n}\n\n/** Validates the reviewed fieldmatch grammar of one form field entry. */\nexport function validatefieldmatch(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed field match is required in options.\" };\n const match = value as Record<string, unknown>;\n if (match.mode !== \"label\" && match.mode !== \"placeholder\" && match.mode !== \"arialabel\" && match.mode !== \"name\") return { allowed: false, reason: \"The reviewed field match mode must be label, placeholder, arialabel or name.\" };\n const key = match.mode === \"label\" ? \"label\" : match.mode === \"placeholder\" ? \"placeholder\" : match.mode === \"arialabel\" ? \"arialabel\" : \"name\";\n if (!isnonempty(match[key])) return { allowed: false, reason: `The reviewed ${match.mode} field match needs a non-empty ${key}.` };\n return { allowed: true };\n}\n\n/** Validates a reviewed structured form record; password entries are refused because passwords need the explicit consentpassword consent. */\nexport function validateformrecord(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed form record with entries is required in options.\" };\n const record = value as Record<string, unknown>;\n if (record.form !== undefined && !isnonempty(record.form)) return { allowed: false, reason: \"The reviewed form record form selector must be a non-empty string.\" };\n if (!Array.isArray(record.entries) || record.entries.length === 0) return { allowed: false, reason: \"The reviewed form record needs a non-empty list of entries.\" };\n for (const item of record.entries) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) return { allowed: false, reason: \"Every reviewed form record entry must be an object.\" };\n const entry = item as Record<string, unknown>;\n const matchcheck = validatefieldmatch(entry.match);\n if (!matchcheck.allowed) return matchcheck;\n if (typeof entry.kind !== \"string\" || !fieldkinds.includes(entry.kind as fieldkind)) return { allowed: false, reason: \"Every reviewed form record entry needs a known field kind.\" };\n if (typeof entry.value !== \"string\") return { allowed: false, reason: \"Every reviewed form record entry needs a string value.\" };\n if (entry.kind === \"password\") return { allowed: false, reason: \"Password entries are refused inside form records; use consentpassword with a reviewed consent ref.\" };\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed valuegen grammar of a generatevalues step. */\nexport function validatevaluegen(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed valuegen rule with a field kind is required in options.\" };\n const rule = value as Record<string, unknown>;\n if (typeof rule.kind !== \"string\" || !fieldkinds.includes(rule.kind as fieldkind)) return { allowed: false, reason: \"The reviewed valuegen kind must be a known field kind.\" };\n if (rule.locale !== undefined && !isnonempty(rule.locale)) return { allowed: false, reason: \"The reviewed valuegen locale must be a non-empty string.\" };\n if (rule.seed !== undefined && (typeof rule.seed !== \"number\" || !Number.isFinite(rule.seed))) return { allowed: false, reason: \"The reviewed valuegen seed must be a finite number.\" };\n return { allowed: true };\n}\n\n/** Validates a reviewed list of label or placeholder value pairs for filllabel and fillplaceholder steps. */\nfunction validatefieldpairs(options: Record<string, unknown>, mode: \"label\" | \"placeholder\"): policyevaluation {\n const pairs = options.fields;\n if (!Array.isArray(pairs) || pairs.length === 0) return { allowed: false, reason: \"A reviewed non-empty list of field pairs is required in options.\" };\n for (const item of pairs) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) return { allowed: false, reason: \"Every reviewed field pair must be an object.\" };\n const pair = item as Record<string, unknown>;\n if (!isnonempty(pair[mode])) return { allowed: false, reason: `Every reviewed field pair needs a non-empty ${mode}.` };\n if (typeof pair.value !== \"string\" || !pair.value.trim()) return { allowed: false, reason: \"Every reviewed field pair needs a non-empty value.\" };\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed card segment grammar of a fillcard step. */\nfunction validatecardsegments(value: unknown): policyevaluation {\n if (!Array.isArray(value) || value.length === 0) return { allowed: false, reason: \"A reviewed non-empty list of card segments is required in options.\" };\n for (const item of value) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) return { allowed: false, reason: \"Every reviewed card segment must be an object.\" };\n const segment = item as Record<string, unknown>;\n const matchcheck = validatefieldmatch(segment.match);\n if (!matchcheck.allowed) return matchcheck;\n if (typeof segment.value !== \"string\" || !segment.value.trim()) return { allowed: false, reason: \"Every reviewed card segment needs a non-empty value.\" };\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed forms and data parameter grammar of the form family. */\nfunction validateformgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"fillform\" || (kind === \"saveprofiles\" && options.formrecord !== undefined)) {\n const recordcheck = validateformrecord(options.formrecord);\n if (!recordcheck.allowed) return recordcheck;\n }\n if (kind === \"filllabel\" || kind === \"fillplaceholder\") {\n const paircheck = validatefieldpairs(options, kind === \"filllabel\" ? \"label\" : \"placeholder\");\n if (!paircheck.allowed) return paircheck;\n }\n if (kind === \"generatevalues\" && options.valuegen !== undefined) {\n const rulecheck = validatevaluegen(options.valuegen);\n if (!rulecheck.allowed) return rulecheck;\n }\n if (kind === \"saveprofiles\" && !isnonempty(options.name)) return { allowed: false, reason: \"A reviewed profile name is required in options.\" };\n if (kind === \"submitform\" && !isnonempty(options.consentref)) return { allowed: false, reason: \"A reviewed consent ref of an approved asksubmit ticket is required in options.\" };\n if (kind === \"retryform\") {\n const backoff = options.backoff;\n if (!backoff || typeof backoff !== \"object\" || Array.isArray(backoff)) return { allowed: false, reason: \"A reviewed backoff rule with wait and factor is required in options.\" };\n const rule = backoff as Record<string, unknown>;\n if (typeof rule.wait !== \"number\" || !Number.isFinite(rule.wait) || rule.wait <= 0) return { allowed: false, reason: \"The reviewed retry backoff wait must be a positive number of milliseconds with no code ceiling.\" };\n if (typeof rule.factor !== \"number\" || !Number.isFinite(rule.factor) || rule.factor < 1) return { allowed: false, reason: \"The reviewed retry backoff factor must be one or greater with no code ceiling.\" };\n if (options.attempts !== undefined && (typeof options.attempts !== \"number\" || !Number.isInteger(options.attempts) || options.attempts < 1)) return { allowed: false, reason: \"The reviewed retry attempts must be a positive integer with no code ceiling.\" };\n }\n if (kind === \"runwizard\" && options.steps !== undefined && (typeof options.steps !== \"number\" || !Number.isInteger(options.steps) || options.steps < 1)) return { allowed: false, reason: \"The reviewed wizard step count must be a positive integer with no code ceiling.\" };\n if (kind === \"selectchain\") {\n if (!isnonempty(options.child)) return { allowed: false, reason: \"A reviewed child selector of the dependent control is required in options.\" };\n if (!nonnegativeoption(options, \"wait\")) return { allowed: false, reason: \"The reviewed dependent wait must be zero or a positive number of milliseconds.\" };\n }\n if (kind === \"picktypeahead\") {\n if (!isnonempty(options.pick)) return { allowed: false, reason: \"A reviewed suggestion entry to pick is required in options.\" };\n if (!nonnegativeoption(options, \"timeout\")) return { allowed: false, reason: \"The reviewed typeahead timeout must be zero or a positive number of milliseconds.\" };\n }\n if (kind === \"pickdate\" && !/^\\d{4}-\\d{2}-\\d{2}$/.test(step.value ?? \"\")) return { allowed: false, reason: \"The reviewed date must use the yyyy-mm-dd form.\" };\n if (kind === \"fillcard\") {\n const segmentcheck = validatecardsegments(options.segments);\n if (!segmentcheck.allowed) return segmentcheck;\n if (!nonnegativeoption(options, \"pause\")) return { allowed: false, reason: \"The reviewed card typing pause must be zero or a positive number of milliseconds.\" };\n }\n if (kind === \"fillcode\" && !isnonempty(options.source)) return { allowed: false, reason: \"A reviewed one time code source is required in options.\" };\n if (kind === \"consentpassword\" && !isnonempty(options.consentref)) return { allowed: false, reason: \"A reviewed consent ref is required in options before any password is filled.\" };\n return { allowed: true };\n}\n\n/** Validates one reviewed transform rule: a supported expression, source columns and a target column. */\nexport function validatetransformrule(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed transform rule with an expression, sources and a target is required in options.\" };\n const rule = value as Record<string, unknown>;\n const expression = rule.expression;\n if (typeof expression !== \"string\" || !/^(trim|upper|lower|number|prefix|suffix|replace)(?::.+)?$/.test(expression)) return { allowed: false, reason: \"The reviewed transform expression must be trim, upper, lower, number, prefix, suffix or replace with an optional argument.\" };\n if (expression.startsWith(\"replace\") && !expression.slice(\"replace\".length).includes(\"=>\")) return { allowed: false, reason: \"The reviewed replace expression needs the from=>to separator.\" };\n if (expression.startsWith(\"replace\") && expression.slice(\"replace:\".length).split(\"=>\")[0] === \"\") return { allowed: false, reason: \"The reviewed replace expression needs a non-empty from part.\" };\n if (!Array.isArray(rule.sources) || rule.sources.length === 0 || !rule.sources.every(source => isnonempty(source))) return { allowed: false, reason: \"Every reviewed transform rule needs a non-empty list of source columns.\" };\n if (!isnonempty(rule.target)) return { allowed: false, reason: \"Every reviewed transform rule needs a non-empty target column.\" };\n return { allowed: true };\n}\n\n/** Validates a reviewed dataset id list in options. */\nfunction validatedatasetids(options: Record<string, unknown>, key: string): policyevaluation {\n const ids = options[key];\n if (!Array.isArray(ids) || ids.length === 0 || !ids.every(id => isnonempty(id))) return { allowed: false, reason: `A reviewed non-empty list of dataset ids is required in options as ${key}.` };\n return { allowed: true };\n}\n\n/** Validates the reviewed extraction, transform, export and provenance parameter grammar of the data family. */\nfunction validatedatagrammar(step: toolstep, options: Record<string, unknown>, origin: string): policyevaluation {\n const kind = step.kind;\n if (kind === \"scrapetable\") {\n if (options.name !== undefined && !isnonempty(options.name)) return { allowed: false, reason: \"The reviewed dataset name must be a non-empty string.\" };\n if (options.rowlimit !== undefined && (typeof options.rowlimit !== \"number\" || !Number.isInteger(options.rowlimit) || options.rowlimit < 1)) return { allowed: false, reason: \"The reviewed row limit must be a positive integer with no code ceiling.\" };\n }\n if (kind === \"paginateextract\") {\n if (!isnonempty(options.next)) return { allowed: false, reason: \"A reviewed next control selector is required in options.\" };\n if (options.pages !== undefined && (typeof options.pages !== \"number\" || !Number.isInteger(options.pages) || options.pages < 1)) return { allowed: false, reason: \"The reviewed page count must be a positive integer with no code ceiling.\" };\n if (!nonnegativeoption(options, \"wait\")) return { allowed: false, reason: \"The reviewed row freshness wait must be zero or a positive number of milliseconds.\" };\n }\n if (kind === \"exportcsv\" || kind === \"exportjson\" || kind === \"exportexcel\" || kind === \"copytable\" || kind === \"streamdisk\") {\n if (!isnonempty(options.dataset)) return { allowed: false, reason: \"A reviewed dataset id is required in options.\" };\n if (options.name !== undefined && !isnonempty(options.name)) return { allowed: false, reason: \"The reviewed artifact name must be a non-empty string.\" };\n }\n if (kind === \"exportcsv\" && options.delimiter !== undefined && (typeof options.delimiter !== \"string\" || options.delimiter.length !== 1)) return { allowed: false, reason: \"The reviewed csv delimiter must be a single character.\" };\n if (kind === \"streamdisk\" && (typeof options.chunk !== \"number\" || !Number.isInteger(options.chunk) || options.chunk < 1)) return { allowed: false, reason: \"The reviewed streaming chunk size must be a positive integer with no code ceiling.\" };\n if (kind === \"pushsheets\") {\n if (!isnonempty(options.dataset)) return { allowed: false, reason: \"A reviewed dataset id is required in options.\" };\n if (!isnonempty(options.sheet)) return { allowed: false, reason: \"A reviewed sheet endpoint url is required in options.\" };\n if (!ishttpsurl(options.sheet)) return { allowed: false, reason: \"The reviewed sheet endpoint url must use HTTPS.\" };\n if (options.reviewed !== true) return { allowed: false, reason: \"The sheet push needs the explicit reviewed flag before any data leaves local memory.\" };\n }\n if (kind === \"importcsv\") {\n if (typeof options.csv !== \"string\" || !options.csv.trim()) return { allowed: false, reason: \"Reviewed csv content is required in options.\" };\n if (options.name !== undefined && !isnonempty(options.name)) return { allowed: false, reason: \"The reviewed dataset name must be a non-empty string.\" };\n if (options.mapping !== undefined) {\n const mapping = options.mapping;\n if (!mapping || typeof mapping !== \"object\" || Array.isArray(mapping) || !Object.values(mapping).every(item => typeof item === \"string\")) return { allowed: false, reason: \"The reviewed csv column mapping must be an object of string values.\" };\n }\n }\n if (kind === \"looprows\") {\n if (!isnonempty(options.dataset)) return { allowed: false, reason: \"A reviewed dataset id is required in options.\" };\n if (options.variable !== undefined && !isnonempty(options.variable)) return { allowed: false, reason: \"The reviewed row variable name must be a non-empty string.\" };\n const inner = validateinnerstep(options, origin);\n if (!inner.allowed) return inner;\n }\n if (kind === \"transformvalues\") {\n if (!isnonempty(options.dataset)) return { allowed: false, reason: \"A reviewed dataset id is required in options.\" };\n const rules = options.rules;\n if (!Array.isArray(rules) || rules.length === 0) return { allowed: false, reason: \"A reviewed non-empty list of transform rules is required in options.\" };\n for (const item of rules) {\n const rulecheck = validatetransformrule(item);\n if (!rulecheck.allowed) return rulecheck;\n }\n }\n if (kind === \"deduperows\") {\n if (!isnonempty(options.dataset)) return { allowed: false, reason: \"A reviewed dataset id is required in options.\" };\n const keys = options.keys;\n if (!Array.isArray(keys) || keys.length === 0 || !keys.every(key => isnonempty(key))) return { allowed: false, reason: \"A reviewed non-empty list of dedupe column keys is required in options.\" };\n }\n if (kind === \"mergepages\") {\n const listcheck = validatedatasetids(options, \"datasets\");\n if (!listcheck.allowed) return listcheck;\n }\n if (kind === \"stamplerows\") {\n if (!isnonempty(options.dataset)) return { allowed: false, reason: \"A reviewed dataset id is required in options.\" };\n if (options.url !== undefined && !ishttpsurl(options.url)) return { allowed: false, reason: \"The reviewed source url must use HTTPS.\" };\n }\n if (kind === \"previewgrid\") {\n if (!isnonempty(options.dataset)) return { allowed: false, reason: \"A reviewed dataset id is required in options.\" };\n if (options.sample !== undefined && (typeof options.sample !== \"number\" || !Number.isInteger(options.sample) || options.sample < 1)) return { allowed: false, reason: \"The reviewed sample row count must be a positive integer with no code ceiling.\" };\n }\n if (kind === \"resumeextract\" && !isnonempty(options.session)) return { allowed: false, reason: \"A reviewed extract session id is required in options.\" };\n if (kind === \"logprovenance\" && !isnonempty(options.artifact)) return { allowed: false, reason: \"A reviewed artifact id or name is required in options.\" };\n return { allowed: true };\n}\n\n/** Validates a reviewed batch download specification: a non-empty HTTPS url list, an optional filename rule and an optional completion criterion. */\nexport function validatedownloadspec(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed downloadspec with a url list is required in options.\" };\n const spec = value as Record<string, unknown>;\n if (!Array.isArray(spec.urls) || spec.urls.length === 0 || !spec.urls.every(url => ishttpsurl(url))) return { allowed: false, reason: \"The reviewed downloadspec needs a non-empty list of HTTPS urls.\" };\n if (spec.filename !== undefined && !isnonempty(spec.filename)) return { allowed: false, reason: \"The reviewed downloadspec filename rule must be a non-empty string.\" };\n if (spec.complete !== undefined && spec.complete !== \"size\" && spec.complete !== \"checksum\") return { allowed: false, reason: \"The reviewed downloadspec completion criterion must be size or checksum.\" };\n return { allowed: true };\n}\n\n/** Validates a reviewed mime interception filter: include and exclude patterns plus the deny default for unlisted mime types. */\nexport function validatemimefilter(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed mimefilter with include and exclude patterns is required in options.\" };\n const filter = value as Record<string, unknown>;\n if (!Array.isArray(filter.include) || filter.include.length === 0 || !filter.include.every(pattern => isnonempty(pattern))) return { allowed: false, reason: \"The reviewed mimefilter needs a non-empty list of include patterns.\" };\n if (filter.exclude !== undefined && (!Array.isArray(filter.exclude) || !filter.exclude.every(pattern => isnonempty(pattern)))) return { allowed: false, reason: \"The reviewed mimefilter exclude patterns must be a list of non-empty strings.\" };\n if (filter.default !== \"deny\" && filter.default !== \"allow\") return { allowed: false, reason: \"The reviewed mimefilter needs the deny or allow default for unlisted mime types.\" };\n return { allowed: true };\n}\n\n/** Validates one reviewed cleanup rule: a positive age window with no code ceiling, an artifact kind and a keep policy. */\nexport function validatecleanuprule(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed cleanuprule with an age, a kind and a keep policy is required.\" };\n const rule = value as Record<string, unknown>;\n if (typeof rule.age !== \"number\" || !Number.isFinite(rule.age) || rule.age <= 0) return { allowed: false, reason: \"The reviewed cleanup age window must be a positive number of milliseconds with no code ceiling.\" };\n if (!isnonempty(rule.kind)) return { allowed: false, reason: \"The reviewed cleanup rule needs a non-empty artifact kind, or any to match every kind.\" };\n if (rule.keep !== \"none\" && rule.keep !== \"latest\" && rule.keep !== \"all\") return { allowed: false, reason: \"The reviewed cleanup keep policy must be none, latest or all.\" };\n return { allowed: true };\n}\n\n/** Validates the reviewed files, clipboard and downloads parameter grammar; batch sizes, concurrent windows and cleanup ages stay user configured with no code ceilings. */\nfunction validatefilesgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"batchdownload\") {\n const speccheck = validatedownloadspec(options.downloadspec);\n if (!speccheck.allowed) return speccheck;\n if (options.concurrent !== undefined && (typeof options.concurrent !== \"number\" || !Number.isInteger(options.concurrent) || options.concurrent < 1)) return { allowed: false, reason: \"The reviewed concurrent download window must be a positive integer with no code ceiling.\" };\n }\n if (kind === \"pausedownload\" || kind === \"resumedownload\" || kind === \"verifydownload\" || kind === \"quarantinedownload\" || kind === \"scanvirus\") {\n if (!isnonempty(step.value)) return { allowed: false, reason: \"A reviewed download or quarantine reference is required.\" };\n if (kind === \"verifydownload\") {\n if (options.checksum !== undefined && !isnonempty(options.checksum)) return { allowed: false, reason: \"The reviewed expected checksum must be a non-empty string.\" };\n if (options.bytes !== undefined && (typeof options.bytes !== \"number\" || !Number.isFinite(options.bytes) || options.bytes < 0)) return { allowed: false, reason: \"The reviewed expected size must be zero or a positive number of bytes.\" };\n }\n if (kind === \"scanvirus\" && options.scanner !== undefined && !isnonempty(options.scanner)) return { allowed: false, reason: \"The reviewed scanner name must be a non-empty string.\" };\n if (kind === \"quarantinedownload\" && options.reason !== undefined && !isnonempty(options.reason)) return { allowed: false, reason: \"The reviewed quarantine reason must be a non-empty string.\" };\n }\n if (kind === \"interceptmime\") {\n const filtercheck = validatemimefilter(options.mimefilter);\n if (!filtercheck.allowed) return filtercheck;\n }\n if (kind === \"readclipboard\") {\n if (!isnonempty(options.consentref)) return { allowed: false, reason: \"A clipboard read requires a reviewed consent ref of an approved consent prompt in options.\" };\n if (options.prompt !== undefined && !isnonempty(options.prompt)) return { allowed: false, reason: \"The reviewed clipboard consent prompt must be a non-empty string.\" };\n }\n if (kind === \"exportnetlog\" && options.stepid !== undefined && !isnonempty(options.stepid)) return { allowed: false, reason: \"The reviewed netlog step filter must be a non-empty step id.\" };\n if (kind === \"namecaptures\") {\n if (!isnonempty(options.task)) return { allowed: false, reason: \"A reviewed task id is required in options for capture naming.\" };\n if (options.steps !== undefined && (!Array.isArray(options.steps) || options.steps.length === 0 || !options.steps.every(item => isnonempty(item)))) return { allowed: false, reason: \"The reviewed capture steps must be a non-empty list of step ids when present.\" };\n if (options.extension !== undefined && !isnonempty(options.extension)) return { allowed: false, reason: \"The reviewed capture extension must be a non-empty string.\" };\n }\n if (kind === \"cleanupartifacts\" && options.rules !== undefined) {\n const rules = options.rules;\n if (!Array.isArray(rules) || rules.length === 0) return { allowed: false, reason: \"The reviewed cleanup rules must be a non-empty list when present.\" };\n for (const item of rules) {\n const rulecheck = validatecleanuprule(item);\n if (!rulecheck.allowed) return rulecheck;\n }\n }\n return { allowed: true };\n}\n\n/** Requires an approved consent prompt before any clipboard read; every read consumes its own prompt. */\nexport function clipboardconsentgranted(step: toolstep): policyevaluation {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const consentref = options.consentref;\n if (typeof consentref !== \"string\" || !consentref.trim()) return { allowed: false, reason: \"A clipboard read requires a reviewed consent ref in options.\" };\n return { allowed: true };\n}\n\n/** Refuses to release any quarantined file before a clean scan verdict exists. */\nexport function quarantinereleasegranted(entry: quarantineentry): policyevaluation {\n if (entry.scan !== \"clean\") return { allowed: false, reason: `The quarantined file ${entry.path} cannot leave quarantine with the ${entry.scan} scan verdict; only a clean verdict releases it.` };\n return { allowed: true };\n}\n\n/** Refuses downloads and download interception that fall outside the session origin grants. */\nexport function downloadgranted(session: agentsession | undefined, url: string): policyevaluation {\n let origin = \"\";\n try { origin = new URL(url).origin; } catch { return { allowed: false, reason: \"The reviewed download URL is invalid.\" }; }\n if (!origingranted(session, origin)) return { allowed: false, reason: `The download from ${origin} leaves the session origin grants and needs a session grant first.` };\n return { allowed: true };\n}\n\n/** Masks a clipboard payload for every log line; the full text never persists anywhere. */\nexport function maskclipboard(payload: string): string {\n return `[clipboard payload of ${payload.length} character${payload.length === 1 ? \"\" : \"s\"}]`;\n}\n\n/** Requires an asksubmit review step before every form submission step. */\nexport function submitreviewgranted(steps: toolstep[], submitid: string): policyevaluation {\n const position = steps.findIndex(candidate => candidate.id === submitid);\n const asked = steps.some((candidate, index) => candidate.kind === \"asksubmit\" && (position === -1 || index < position));\n return asked ? { allowed: true } : { allowed: false, reason: \"Form submission requires an asksubmit review step before it.\" };\n}\n\n/** Requires a reviewed consent ref before any password field is filled. */\nexport function passwordconsentgranted(step: toolstep): policyevaluation {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const consentref = options.consentref;\n if (typeof consentref !== \"string\" || !consentref.trim()) return { allowed: false, reason: \"A password fill requires a reviewed consent ref in options.\" };\n return { allowed: true };\n}\n\n/** True when a numeric value passes the Luhn checksum used by card networks. */\nfunction luhnvalid(digits: string): boolean {\n let sum = 0;\n let double = false;\n for (let index = digits.length - 1; index >= 0; index -= 1) {\n let value = Number.parseInt(digits[index] ?? \"\", 10);\n if (!Number.isFinite(value)) return false;\n if (double) { value *= 2; if (value > 9) value -= 9; }\n sum += value;\n double = !double;\n }\n return sum % 10 === 0;\n}\n\n/** Refuses generated values that look like real card numbers or personal identifiers; test prefixed card values stay allowed. */\nexport function generatedvalueallowed(value: string): policyevaluation {\n const compact = value.replace(/[\\s-]/g, \"\");\n if (/^\\d{13,19}$/.test(compact) && luhnvalid(compact) && !compact.startsWith(\"4111\")) return { allowed: false, reason: \"The generated value looks like a real card number and is refused; generated card values use the 4111 test prefix.\" };\n if (/^\\d{3}-\\d{2}-\\d{4}$/.test(value.trim())) return { allowed: false, reason: \"The generated value looks like a personal identifier and is refused.\" };\n return { allowed: true };\n}\n\n/** Requires the origin grants of a saved profile to cover the origin before its values fill a page. */\nexport function profilegrantgranted(profile: formprofile, origin: string): policyevaluation {\n if (!profile.grants.includes(origin)) return { allowed: false, reason: `The saved profile ${profile.name} is not granted to ${origin}; add the origin to the profile grants first.` };\n return { allowed: true };\n}\n\n/** Restricts group and layout mutations to the active session: they refuse without a live session. */\nexport function layoutmutationgranted(session: agentsession | undefined, now: number): policyevaluation {\n if (!session || session.stoppedat || session.expiresat <= now) return { allowed: false, reason: \"Group and layout mutations stay inside the active session.\" };\n return { allowed: true };\n}\n\n/** Requires explicit review before closing a window that holds more than one task tab. */\nexport function windowclosegate(tasktabcount: number, reviewed: boolean): policyevaluation {\n if (tasktabcount > 1 && !reviewed) return { allowed: false, reason: `The window holds ${tasktabcount} task tabs and needs explicit review before it closes.` };\n return { allowed: true };\n}\n\n/** Reads the user configured concurrent task tab ceiling; an absent value never refuses a tab. */\nexport function tasktabceiling(settings: runsettings | undefined): number | undefined {\n const ceiling = settings?.tasktabceiling;\n return typeof ceiling === \"number\" && Number.isFinite(ceiling) && ceiling >= 0 ? ceiling : undefined;\n}\n\n/** Parses the reviewed wait duration of a wait step with no upper bound. */\nexport function waitduration(step: toolstep): number {\n const requested = step.value ? Number.parseInt(step.value, 10) : 250;\n if (!Number.isFinite(requested) || requested < 0) throw new Error(\"Wait duration must be zero or a positive number of milliseconds.\");\n return requested;\n}\n\nfunction isnumericid(value: unknown): value is string {\n return typeof value === \"string\" && /^\\d+$/.test(value);\n}\n\nfunction numericoption(options: Record<string, unknown>, key: string): boolean {\n return options[key] === undefined || (typeof options[key] === \"number\" && Number.isFinite(options[key] as number));\n}\n\n/** True when an optional numeric option is absent or a finite number of zero or more. */\nfunction nonnegativeoption(options: Record<string, unknown>, key: string): boolean {\n return numericoption(options, key) && !(typeof options[key] === \"number\" && (options[key] as number) < 0);\n}\n\nfunction isnonempty(value: unknown): value is string {\n return typeof value === \"string\" && value.trim().length > 0;\n}\n\nfunction ispoint(value: unknown): boolean {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return false;\n const point = value as Record<string, unknown>;\n return typeof point.x === \"number\" && Number.isFinite(point.x) && typeof point.y === \"number\" && Number.isFinite(point.y);\n}\n\n/** Grades a resolution match count: zero is absent, one is resolved and more than one is refused as ambiguous. */\nexport function resolutionverdict(count: number): \"absent\" | \"resolved\" | \"ambiguous\" {\n if (!Number.isFinite(count) || count <= 0) return \"absent\";\n return count === 1 ? \"resolved\" : \"ambiguous\";\n}\n\n/** Validates the reviewed targetref grammar of every resolution mode and rejects empty references. */\nexport function validatetargetref(reference: unknown): policyevaluation {\n if (!reference || typeof reference !== \"object\" || Array.isArray(reference)) return { allowed: false, reason: \"The reviewed target reference must be an object.\" };\n const ref = reference as Record<string, unknown>;\n if (ref.mode === \"selector\") return isnonempty(ref.selector) ? { allowed: true } : { allowed: false, reason: \"The selector target reference needs a non-empty selector.\" };\n if (ref.mode === \"text\") return isnonempty(ref.text) ? { allowed: true } : { allowed: false, reason: \"The text target reference needs non-empty text.\" };\n if (ref.mode === \"aria\") {\n if (!isnonempty(ref.role)) return { allowed: false, reason: \"The aria target reference needs a non-empty role.\" };\n return isnonempty(ref.name) ? { allowed: true } : { allowed: false, reason: \"The aria target reference needs a non-empty name.\" };\n }\n if (ref.mode === \"name\") return isnonempty(ref.name) ? { allowed: true } : { allowed: false, reason: \"The name target reference needs a non-empty name.\" };\n if (ref.mode === \"xpath\") return isnonempty(ref.xpath) ? { allowed: true } : { allowed: false, reason: \"The xpath target reference needs a non-empty expression.\" };\n if (ref.mode === \"index\") {\n const index = ref.index;\n return typeof index === \"number\" && Number.isInteger(index) && index >= 1 ? { allowed: true } : { allowed: false, reason: \"The index target reference needs a positive integer map number.\" };\n }\n if (ref.mode === \"point\") {\n const pointok = typeof ref.x === \"number\" && Number.isFinite(ref.x) && typeof ref.y === \"number\" && Number.isFinite(ref.y);\n return pointok ? { allowed: true } : { allowed: false, reason: \"The point target reference needs numeric x and y coordinates.\" };\n }\n return { allowed: false, reason: \"The target reference mode must be selector, text, aria, name, xpath, index or point.\" };\n}\n\n/** True when the session origin grants cover the given origin; a session without grants only allows its own origin. */\nexport function origingranted(session: agentsession | undefined, origin: string): boolean {\n if (!session) return false;\n const grants = session.grants ?? [session.origin];\n return grants.includes(origin);\n}\n\n/** Decides whether an unreviewed origin may open: the session grants cover it or a safe checksafe verdict vouches for it. */\nexport function originverified(url: string, grants: string[], verdicts: safetyverdict[]): policyevaluation {\n let origin = \"\";\n try { origin = new URL(url).origin; } catch { return { allowed: false, reason: \"The reviewed navigation URL is invalid.\" }; }\n if (grants.includes(origin)) return { allowed: true };\n const covered = verdicts.find(verdict => verdict.safe && (verdict.url === url || (safeorigin(verdict.url) === origin)));\n if (covered) return { allowed: true };\n return { allowed: false, reason: `The origin ${origin} is outside the session grants and has no safe checksafe verdict; run checksafe and review it first.` };\n}\n\nfunction safeorigin(url: string): string {\n try { return new URL(url).origin; } catch { return \"\"; }\n}\n\n/** Refuses navigation that would move a granted task tab outside the session origin grants until the user consents. */\nexport function navigationgranted(session: agentsession | undefined, url: string): policyevaluation {\n let origin = \"\";\n try { origin = new URL(url).origin; } catch { return { allowed: false, reason: \"The reviewed navigation URL is invalid.\" }; }\n if (origingranted(session, origin)) return { allowed: true };\n return { allowed: false, reason: `Navigation to ${origin} leaves the task tab origins and needs the user consent of a session grant first.` };\n}\n\n/** Validates the reviewed inner step of a retry or frame wrapper against the same rules as a top-level step. */\nfunction validateinnerstep(options: Record<string, unknown>, origin: string): policyevaluation {\n const stepid = options.stepid;\n const kind = options.kind;\n if (isnonempty(stepid)) {\n if (kind !== undefined) return { allowed: false, reason: \"The reviewed wrapper must reference a step id or an inline step, not both.\" };\n return { allowed: true };\n }\n if (typeof kind !== \"string\" || !kind.trim()) return { allowed: false, reason: \"A reviewed step id or inline step kind is required in options.\" };\n if (kind === \"retryaction\" || kind === \"enterframe\" || kind === \"looprows\") return { allowed: false, reason: \"The reviewed inner step cannot be another wrapper kind.\" };\n if (!allowedactions.has(kind as actionkind)) return { allowed: false, reason: \"The reviewed inner step kind is unsupported.\" };\n const inneroptions = options.options;\n if (inneroptions !== undefined && (!inneroptions || typeof inneroptions !== \"object\" || Array.isArray(inneroptions))) return { allowed: false, reason: \"The reviewed inner step options must be an object.\" };\n const inner: toolstep = {\n id: \"inner\",\n kind: kind as actionkind,\n summary: \"Reviewed inner step.\",\n risk: actionrisk(kind as actionkind),\n ...(isnonempty(options.target) ? { target: options.target } : {}),\n ...(isnonempty(options.value) ? { value: options.value } : {}),\n ...(inneroptions !== undefined ? { options: JSON.stringify(inneroptions) } : {}),\n };\n return validatestep(inner, origin);\n}\n\n/** True when a reviewed https url parses. */\nfunction ishttpsurl(value: unknown): value is string {\n if (typeof value !== \"string\" || !value.trim()) return false;\n try { return new URL(value).protocol === \"https:\"; } catch { return false; }\n}\n\n/** Validates the reviewed navtarget grammar of a navigation step. */\nfunction validatenavtarget(value: unknown, kind: string): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed navtarget with a url is required in options.\" };\n const target = value as Record<string, unknown>;\n if (!ishttpsurl(target.url)) return { allowed: false, reason: \"The reviewed navtarget url must use HTTPS.\" };\n const container = target.container ?? \"tab\";\n if (container !== \"current\" && container !== \"tab\" && container !== \"window\" && container !== \"private\") return { allowed: false, reason: \"The reviewed navtarget container must be current, tab, window or private.\" };\n if (target.position !== undefined && target.position !== \"adjacent\" && target.position !== \"end\") return { allowed: false, reason: \"The reviewed navtarget position must be adjacent or end.\" };\n if (kind === \"openprivate\" && container !== \"private\") return { allowed: false, reason: \"The openprivate step requires the private container.\" };\n if (kind === \"openlink\" && container === \"private\") return { allowed: false, reason: \"The openlink step cannot open the private container; use openprivate.\" };\n return { allowed: true };\n}\n\n/** Validates the reviewed waitprofile grammar with its load signals, thresholds and per origin overrides. */\nfunction validatewaitprofile(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed waitprofile with load signals is required in options.\" };\n const profile = value as Record<string, unknown>;\n if (!Array.isArray(profile.signals) || profile.signals.length === 0 || !profile.signals.every(signal => isnonempty(signal))) return { allowed: false, reason: \"The reviewed waitprofile needs a non-empty list of load signals.\" };\n if (!nonnegativeoption(profile, \"idle\")) return { allowed: false, reason: \"The reviewed waitprofile idle threshold must be zero or a positive number of milliseconds.\" };\n if (!nonnegativeoption(profile, \"timeout\")) return { allowed: false, reason: \"The reviewed waitprofile timeout must be zero or a positive number of milliseconds.\" };\n if (profile.overrides !== undefined) {\n if (!Array.isArray(profile.overrides) || profile.overrides.length === 0) return { allowed: false, reason: \"The reviewed waitprofile overrides must be a non-empty list when present.\" };\n for (const entry of profile.overrides) {\n if (!entry || typeof entry !== \"object\" || Array.isArray(entry)) return { allowed: false, reason: \"Every reviewed waitprofile override must be an object with an origin.\" };\n const override = entry as Record<string, unknown>;\n if (!ishttpsurl(override.origin)) return { allowed: false, reason: \"Every reviewed waitprofile override origin must use HTTPS.\" };\n if (override.signals !== undefined && (!Array.isArray(override.signals) || !override.signals.every(signal => isnonempty(signal)))) return { allowed: false, reason: \"The reviewed waitprofile override signals must be a list of non-empty strings.\" };\n if (!nonnegativeoption(override, \"idle\") || !nonnegativeoption(override, \"timeout\")) return { allowed: false, reason: \"The reviewed waitprofile override thresholds must be zero or positive numbers.\" };\n }\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed urlpattern grammar with its match mode plus query and fragment parts. */\nexport function validateurlpattern(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed urlpattern is required in options.\" };\n const pattern = value as Record<string, unknown>;\n if (pattern.mode !== \"exact\" && pattern.mode !== \"prefix\" && pattern.mode !== \"host\" && pattern.mode !== \"pattern\") return { allowed: false, reason: \"The reviewed urlpattern mode must be exact, prefix, host or pattern.\" };\n if (!ishttpsurl(pattern.url)) return { allowed: false, reason: \"The reviewed urlpattern url must use HTTPS.\" };\n if (pattern.query !== undefined) {\n if (!pattern.query || typeof pattern.query !== \"object\" || Array.isArray(pattern.query)) return { allowed: false, reason: \"The reviewed urlpattern query part must be an object of parameter names and values.\" };\n for (const item of Object.values(pattern.query)) if (typeof item !== \"string\") return { allowed: false, reason: \"The reviewed urlpattern query values must be strings.\" };\n }\n if (pattern.fragment !== undefined && !isnonempty(pattern.fragment)) return { allowed: false, reason: \"The reviewed urlpattern fragment must be a non-empty string.\" };\n return { allowed: true };\n}\n\n/** Validates a reviewed non-empty list of HTTPS urls in options. */\nfunction validateurllist(options: Record<string, unknown>, key: string): policyevaluation {\n const urls = options[key];\n if (!Array.isArray(urls) || urls.length === 0 || !urls.every(url => ishttpsurl(url))) return { allowed: false, reason: `A reviewed non-empty list of HTTPS urls is required in options as ${key}.` };\n return { allowed: true };\n}\n\n/** Validates the reviewed ratelimit grammar of a navrate step; the window and ceiling stay user configured with no hardcoded cap. */\nfunction validateratelimit(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed ratelimit with a window and a ceiling is required in options.\" };\n const limit = value as Record<string, unknown>;\n if (limit.domain !== undefined && !isnonempty(limit.domain)) return { allowed: false, reason: \"The reviewed ratelimit domain must be a non-empty string.\" };\n if (typeof limit.window !== \"number\" || !Number.isFinite(limit.window) || limit.window <= 0) return { allowed: false, reason: \"The reviewed ratelimit window must be a positive number of milliseconds with no code ceiling.\" };\n if (typeof limit.ceiling !== \"number\" || !Number.isInteger(limit.ceiling) || limit.ceiling < 1) return { allowed: false, reason: \"The reviewed ratelimit ceiling must be a positive integer with no code ceiling.\" };\n return { allowed: true };\n}\n\n/** Validates the reviewed tabquery grammar with url, title, id and pattern matchers; at least one matcher is required. */\nexport function validatetabquery(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed tabquery with at least one matcher is required in options.\" };\n const query = value as Record<string, unknown>;\n const hasmatcher = query.url !== undefined || query.title !== undefined || query.id !== undefined || query.pattern !== undefined;\n if (!hasmatcher) return { allowed: false, reason: \"The reviewed tabquery needs a url, title, id or pattern matcher.\" };\n if (query.url !== undefined && !isnonempty(query.url)) return { allowed: false, reason: \"The reviewed tabquery url matcher must be a non-empty string.\" };\n if (query.title !== undefined && !isnonempty(query.title)) return { allowed: false, reason: \"The reviewed tabquery title matcher must be a non-empty string.\" };\n if (query.pattern !== undefined && !isnonempty(query.pattern)) return { allowed: false, reason: \"The reviewed tabquery pattern matcher must be a non-empty string.\" };\n if (query.id !== undefined && (typeof query.id !== \"number\" || !Number.isInteger(query.id) || query.id < 0)) return { allowed: false, reason: \"The reviewed tabquery id matcher must be a non-negative integer tab id.\" };\n return { allowed: true };\n}\n\n/** Validates a reviewed group color choice against the Chromium tab group palette. */\nfunction validategroupcolor(value: unknown): boolean {\n return typeof value === \"string\" && (groupcolors as string[]).includes(value);\n}\n\n/** Validates a reviewed list of numeric browser ids in options. */\nfunction validateidlist(options: Record<string, unknown>, key: string): boolean {\n const ids = options[key];\n return Array.isArray(ids) && ids.length > 0 && ids.every(id => typeof id === \"number\" && Number.isInteger(id) && id >= 0);\n}\n\n/** Validates the reviewed tab and window parameter grammar of the tabs and windows command family. */\nfunction validatetabsgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"querytabs\" || kind === \"closepattern\") {\n const querycheck = validatetabquery(options.tabquery);\n if (!querycheck.allowed) return querycheck;\n if (kind === \"closepattern\" && options.reviewed !== true) return { allowed: false, reason: \"The close pattern needs the explicit reviewed flag before any tab closes.\" };\n }\n if (kind === \"duplicatetab\" || kind === \"pintab\" || kind === \"mutetab\" || kind === \"movetab\" || kind === \"movetabwindow\" || kind === \"badgetab\" || kind === \"attachmeta\") {\n if (!isnumericid(step.value)) return { allowed: false, reason: \"A numeric browser tab id is required.\" };\n }\n if (kind === \"focuswindow\" || kind === \"maximizewindow\" || kind === \"minimizewindow\" || kind === \"restorewindow\") {\n if (!isnumericid(step.value)) return { allowed: false, reason: \"A numeric browser window id is required.\" };\n }\n if (kind === \"pintab\" && typeof options.pinned !== \"boolean\") return { allowed: false, reason: \"A reviewed pinned flag is required in options.\" };\n if (kind === \"mutetab\" && typeof options.muted !== \"boolean\") return { allowed: false, reason: \"A reviewed muted flag is required in options.\" };\n if (kind === \"movetab\") {\n if (typeof options.index !== \"number\" || !Number.isInteger(options.index) || options.index < 0) return { allowed: false, reason: \"A reviewed non-negative target index is required in options.\" };\n }\n if (kind === \"movetabwindow\") {\n if (typeof options.windowid !== \"number\" || !Number.isInteger(options.windowid) || options.windowid < 0) return { allowed: false, reason: \"A reviewed target window id is required in options.\" };\n }\n if (kind === \"grouptabs\") {\n const group = options.group;\n if (!group || typeof group !== \"object\" || Array.isArray(group)) return { allowed: false, reason: \"A reviewed group with a name is required in options.\" };\n const spec = group as Record<string, unknown>;\n if (!isnonempty(spec.name)) return { allowed: false, reason: \"The reviewed group needs a non-empty name.\" };\n if (!validategroupcolor(spec.color)) return { allowed: false, reason: \"The reviewed group color must be a Chromium tab group color.\" };\n if (!validateidlist(spec, \"tabids\")) return { allowed: false, reason: \"The reviewed group needs a non-empty list of member tab ids.\" };\n }\n if (kind === \"colorgroup\") {\n if (!isnonempty(options.name)) return { allowed: false, reason: \"A reviewed group name is required in options.\" };\n if (!validategroupcolor(options.color)) return { allowed: false, reason: \"The reviewed group color must be a Chromium tab group color.\" };\n }\n if (kind === \"collapsegroup\") {\n if (!isnonempty(options.name)) return { allowed: false, reason: \"A reviewed group name is required in options.\" };\n if (typeof options.collapsed !== \"boolean\") return { allowed: false, reason: \"A reviewed collapsed flag is required in options.\" };\n }\n if (kind === \"discardtab\" || kind === \"reloadtabs\") {\n if (!isnumericid(step.value) && !validateidlist(options, \"tabs\")) return { allowed: false, reason: \"A numeric tab id or a reviewed list of tab ids is required.\" };\n }\n if (kind === \"zoomin\" || kind === \"zoomout\") {\n if (options.step !== undefined && (typeof options.step !== \"number\" || !Number.isFinite(options.step) || options.step <= 0)) return { allowed: false, reason: \"The reviewed zoom step must be a positive number with no code ceiling.\" };\n if (step.value !== undefined && step.value !== \"\" && !isnumericid(step.value)) return { allowed: false, reason: \"The reviewed zoom target must be a numeric tab id.\" };\n }\n if (kind === \"switchtab\") {\n if (options.direction !== \"next\" && options.direction !== \"previous\") return { allowed: false, reason: \"A reviewed switch direction of next or previous is required in options.\" };\n }\n if (kind === \"restorewindow\") {\n const bounds = options.bounds;\n if (bounds !== undefined) {\n if (!bounds || typeof bounds !== \"object\" || Array.isArray(bounds)) return { allowed: false, reason: \"The reviewed window bounds must be an object.\" };\n const shape = bounds as Record<string, unknown>;\n for (const field of [\"left\", \"top\", \"width\", \"height\"]) {\n if (typeof shape[field] !== \"number\" || !Number.isFinite(shape[field])) return { allowed: false, reason: \"The reviewed window bounds need numeric left, top, width and height.\" };\n }\n }\n }\n if (kind === \"scratchwindow\") {\n if (step.value !== undefined && step.value !== \"\" && !ishttpsurl(step.value)) return { allowed: false, reason: \"The reviewed scratch window url must use HTTPS.\" };\n }\n if (kind === \"incognitowindow\" && !ishttpsurl(step.value)) return { allowed: false, reason: \"A reviewed HTTPS url is required to open an incognito window.\" };\n if (kind === \"restoretab\" && step.value !== undefined && step.value !== \"\" && !ishttpsurl(step.value)) return { allowed: false, reason: \"The reviewed restore url must use HTTPS.\" };\n if (kind === \"savelayout\" || kind === \"restorelayout\") {\n if (!isnonempty(options.name)) return { allowed: false, reason: \"A reviewed layout name is required in options.\" };\n }\n if (kind === \"badgetab\") {\n if (!isnonempty(options.label)) return { allowed: false, reason: \"A reviewed badge label is required in options.\" };\n if (options.taskid !== undefined && !isnonempty(options.taskid)) return { allowed: false, reason: \"The reviewed badge task id must be a non-empty string.\" };\n }\n if (kind === \"attachmeta\") {\n const labels = options.labels;\n const taskrefs = options.taskrefs;\n const haslabels = Array.isArray(labels) && labels.length > 0 && labels.every(label => isnonempty(label));\n const hastaskrefs = Array.isArray(taskrefs) && taskrefs.length > 0 && taskrefs.every(ref => isnonempty(ref));\n if (!haslabels && !hastaskrefs) return { allowed: false, reason: \"Reviewed labels or task refs are required in options to attach metadata.\" };\n if (options.provenance !== undefined && !isnonempty(options.provenance)) return { allowed: false, reason: \"The reviewed provenance must be a non-empty string.\" };\n }\n if (kind === \"reopenrun\" && !isnonempty(options.run)) return { allowed: false, reason: \"A reviewed run id is required in options to reopen its tabs.\" };\n return { allowed: true };\n}\n\n/** True when the kind belongs to the media capture family of pdf documents, recordings, images, canvases, streams, assets, lapses, conversions and thumbnails. */\nexport function ismediakind(kind: actionkind): boolean {\n return mediaactions.has(kind);\n}\n\n/** True when the kind belongs to the network observation family of fetching, parsing and typed calls. */\nexport function ishttpkind(kind: actionkind): boolean {\n return httpactions.has(kind);\n}\n\n/** True when the kind belongs to the socket and stream family of channels, messages, subscriptions and poll loops. */\nexport function issocketkind(kind: actionkind): boolean {\n return socketactions.has(kind);\n}\n\n/** True when the kind belongs to the request observation family of watches, headers, bodies and page api discovery. */\nexport function isnetwatchkind(kind: actionkind): boolean {\n return netwatchactions.has(kind);\n}\n\n/** True when the kind belongs to the network control family of blocking, mocking, header rewriting, cookies, auth, api keys, proxy routing and uploads. */\nexport function iscontrolkind(kind: actionkind): boolean {\n return controlactions.has(kind);\n}\n\n/** Resolves the reviewed risk of one step: capturebodies grades sensitive when the reviewed mime list carries private payload types and extractapi grades sensitive when the replay verb mutates, while every other kind keeps its risk table grade. */\nexport function resolvedrisk(step: toolstep): \"read\" | \"interaction\" | \"sensitive\" {\n if (step.kind === \"capturebodies\") {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const body = options.body;\n const mimes = body && typeof body === \"object\" && !Array.isArray(body) ? (body as Record<string, unknown>).mimes : undefined;\n if (Array.isArray(mimes) && mimes.some(mime => typeof mime === \"string\" && privatemime(mime))) return \"sensitive\";\n return \"interaction\";\n }\n if (step.kind === \"extractapi\") {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const replay = options.replay;\n const verb = replay && typeof replay === \"object\" && !Array.isArray(replay) ? (replay as Record<string, unknown>).verb : undefined;\n if (typeof verb === \"string\" && ![\"GET\", \"HEAD\", \"OPTIONS\"].includes(verb.trim().toUpperCase())) return \"sensitive\";\n return \"read\";\n }\n return actionrisk(step.kind);\n}\n\n/** Restricts every outbound channel to a granted origin: wss websocket and https event stream urls map onto their https origin, carry no embedded credentials and stay inside the session origin grants. */\nexport function socketgate(session: agentsession | undefined, url: string): policyevaluation {\n let parsed: URL;\n try { parsed = new URL(url); } catch { return { allowed: false, reason: \"The channel needs a valid url before it can be reviewed.\" }; }\n if (parsed.protocol !== \"wss:\" && parsed.protocol !== \"https:\") return { allowed: false, reason: \"Channels use wss websocket urls or https event stream urls only.\" };\n if (parsed.username || parsed.password) return { allowed: false, reason: \"Channel credentials are not allowed in the url.\" };\n const origin = channelorigin(url);\n if (!origingranted(session, origin)) return { allowed: false, reason: `The channel to ${origin} stays outside the session origin grants.` };\n return { allowed: true };\n}\n\n/** Requires the user granted request watching before any watchrequests step runs; the observation derives from the page timing buffers and the grant adds no manifest permission. */\nexport function watchgate(session: agentsession | undefined, settings: runsettings | undefined, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the request watch.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot watch requests.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot watch requests.\" };\n if (settings?.webrequestgrant !== true) return { allowed: false, reason: \"Request watching needs the webrequest grant in the review panel first; the observation derives from the page timing buffers and adds no manifest permission.\" };\n return { allowed: true };\n}\n\n/** Requires the host grant for every observed origin before header reads, body captures and endpoint replays touch an exchange. */\nexport function observedorigingranted(session: agentsession | undefined, url: string): policyevaluation {\n let origin = \"\";\n try { origin = new URL(url).origin; } catch { return { allowed: false, reason: \"The observed exchange url does not parse for an origin check.\" }; }\n if (!origingranted(session, origin)) return { allowed: false, reason: `The observed origin ${origin} stays outside the session origin grants; grant it before reading headers, bodies or replays.` };\n return { allowed: true };\n}\n\n/** Restricts every outbound request to a granted origin: the url must be a reviewed HTTPS url inside the session origin grants. */\nexport function origincheck(session: agentsession | undefined, url: string): policyevaluation {\n let parsed: URL;\n try { parsed = new URL(url); } catch { return { allowed: false, reason: \"The outbound request needs a valid url before it can be reviewed.\" }; }\n if (parsed.protocol !== \"https:\") return { allowed: false, reason: \"Outbound requests use HTTPS urls only.\" };\n if (parsed.username || parsed.password) return { allowed: false, reason: \"Endpoint credentials are not allowed in the url.\" };\n if (!origingranted(session, parsed.origin)) return { allowed: false, reason: `The outbound request to ${parsed.origin} stays outside the session origin grants.` };\n return { allowed: true };\n}\n\n/** True when a header name carries credentials and therefore needs the explicit consent that names it. */\nexport function credentialheadername(name: string): boolean {\n return credentialheaders.has(name.trim().toLowerCase());\n}\n\n/** Requires a reviewed consent ref before any custom header leaves the extension; requests without custom headers need no prompt. */\nexport function fetchconsentrefgranted(step: toolstep): policyevaluation {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const request = options.fetch;\n const headers = request && typeof request === \"object\" && !Array.isArray(request) ? (request as Record<string, unknown>).headers : undefined;\n const names = headers && typeof headers === \"object\" && !Array.isArray(headers) ? Object.keys(headers as Record<string, unknown>) : [];\n if (names.length === 0) return { allowed: true };\n const empty = names.some(name => !name.trim());\n if (empty) return { allowed: false, reason: \"Header allowlists with empty names are refused.\" };\n const credential = names.find(name => credentialheadername(name));\n if (credential !== undefined && !isnonempty(options.consentref)) return { allowed: false, reason: `The credential bearing header ${credential} needs the explicit reviewed consent that names it before it is sent.` };\n if (!isnonempty(options.consentref)) return { allowed: false, reason: `The ${names.length} reviewed custom header${names.length === 1 ? \"\" : \"s\"} need a reviewed consent ref in options before any send.` };\n return { allowed: true };\n}\n\n/** True when one stored fetch consent still covers the origin and every header name inside its expiry window. */\nexport function fetchconsentcovers(consent: { origin: string; headers: Array<{ name: string }>; approved?: boolean; expiresat: number }, origin: string, headernames: string[], now: number): boolean {\n if (consent.approved !== true) return false;\n if (consent.expiresat <= now) return false;\n if (consent.origin !== origin) return false;\n const covered = new Set(consent.headers.map(header => header.name.trim().toLowerCase()));\n return headernames.every(name => covered.has(name.trim().toLowerCase()));\n}\n\n/** True when the reviewed call mutates: rest verbs beyond get, head and options or a graphql mutation; mutating calls grade sensitive. */\nexport function mutationcallof(step: toolstep): boolean {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n if (step.kind === \"callgraphql\") {\n const request = options.graphql;\n return Boolean(request && typeof request === \"object\" && !Array.isArray(request) && (request as Record<string, unknown>).operationkind === \"mutation\");\n }\n if (step.kind === \"callrest\") {\n const method = typeof options.method === \"string\" ? options.method.trim().toUpperCase() : undefined;\n if (method !== undefined) return ![\"GET\", \"HEAD\", \"OPTIONS\"].includes(method);\n }\n return false;\n}\n\n/** Keeps the reviewed fetch waits inside the reviewed wait budget: the worst case of every timeout plus every backoff wait must fit; every bound itself stays a user choice with no code ceiling. */\nexport function fetchbudgetallowed(timeout: number | undefined, retries: number | undefined, backoff: number | undefined, wait: number | undefined): policyevaluation {\n for (const [label, value] of [[\"timeout\", timeout], [\"retries\", retries], [\"backoff\", backoff]] as Array<[string, number | undefined]>) {\n if (value !== undefined && (typeof value !== \"number\" || !Number.isFinite(value) || value < 0)) return { allowed: false, reason: `The reviewed fetch ${label} must be zero or a positive number with no code ceiling.` };\n }\n if (wait !== undefined && (typeof wait !== \"number\" || !Number.isFinite(wait) || wait < 0)) return { allowed: false, reason: \"The reviewed fetch wait budget must be zero or a positive number of milliseconds.\" };\n if (wait === undefined || timeout === undefined) return { allowed: true };\n const attempts = Math.max(1, Math.floor((retries ?? 0)) + 1);\n const waits = (backoff ?? 0) * (attempts * (attempts - 1)) / 2;\n const worstcase = timeout * attempts + waits;\n if (worstcase > wait) return { allowed: false, reason: `The fetch worst case of ${worstcase} milliseconds exceeds the reviewed wait budget of ${wait} milliseconds; review a wider budget or fewer retries.` };\n return { allowed: true };\n}\n\n/** Resolves the outbound url of an http step at review time: the fetch request url of a fetchurl step and nothing for typed calls whose endpoints resolve at execution. */\nexport function outboundtarget(step: toolstep): string | undefined {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const request = options.fetch;\n if (request && typeof request === \"object\" && !Array.isArray(request)) {\n const url = (request as Record<string, unknown>).url;\n if (typeof url === \"string\" && url.trim()) return url.trim();\n }\n return undefined;\n}\n\n/** Validates one reviewed typed endpoint definition: name, method, HTTPS url template with variables, header allowlist with non-empty names and a payload schema with kinds, required flags and defaults. */\nexport function validateendpointrecord(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed endpoint record is required.\" };\n const record = value as Record<string, unknown>;\n if (!isnonempty(record.name)) return { allowed: false, reason: \"The endpoint record needs a reviewed non-empty name.\" };\n if (!isnonempty(record.method)) return { allowed: false, reason: \"The endpoint record needs a reviewed method.\" };\n if (!ishttpsurl(record.url)) return { allowed: false, reason: \"The endpoint record url template must be an HTTPS url.\" };\n if (record.headers !== undefined) {\n if (!record.headers || typeof record.headers !== \"object\" || Array.isArray(record.headers)) return { allowed: false, reason: \"The endpoint header allowlist must be an object of reviewed headers.\" };\n for (const name of Object.keys(record.headers as Record<string, unknown>)) {\n if (!name.trim()) return { allowed: false, reason: \"Endpoint header allowlists with empty names are refused.\" };\n const headervalue = (record.headers as Record<string, unknown>)[name];\n if (typeof headervalue !== \"string\") return { allowed: false, reason: `The endpoint header ${name} needs a reviewed string value.` };\n }\n }\n const schema = record.schema;\n if (!schema || typeof schema !== \"object\" || Array.isArray(schema)) return { allowed: false, reason: \"Every typed endpoint call needs a reviewed payload schema; endpoint records without schemas are refused.\" };\n const fields = (schema as Record<string, unknown>).fields;\n if (!Array.isArray(fields) || fields.length === 0) return { allowed: false, reason: \"The endpoint payload schema needs a non-empty field list.\" };\n for (const item of fields) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) return { allowed: false, reason: \"Every payload schema field must be an object.\" };\n const field = item as Record<string, unknown>;\n if (!isnonempty(field.name)) return { allowed: false, reason: \"Every payload schema field needs a non-empty name.\" };\n if (field.kind !== \"string\" && field.kind !== \"number\" && field.kind !== \"boolean\") return { allowed: false, reason: `The payload schema field ${field.name} must be a string, number or boolean kind.` };\n if (field.required !== undefined && typeof field.required !== \"boolean\") return { allowed: false, reason: `The payload schema field ${field.name} required flag must be a boolean.` };\n if (field.default !== undefined && typeof field.default !== \"string\" && typeof field.default !== \"number\" && typeof field.default !== \"boolean\") return { allowed: false, reason: `The payload schema field ${field.name} default must match its kind.` };\n }\n return { allowed: true };\n}\n\n/** Validates one dotted json path against the path grammar: non-empty segments of names, digits, underscores or hyphens. */\nfunction validpath(path: string): boolean {\n return path.split(\".\").every(segment => /^[A-Za-z0-9_-]+$/.test(segment));\n}\n\n/** Validates the reviewed network observation parameter grammar of the 1.1.42 family: fetch requests with header allowlists, fetch policies with timeout, retries, backoff and follow limit, stream budgets, dotted json paths, html queries, graphql operations and typed endpoint references; every bound stays a user choice with no code ceiling. */\nfunction validatehttpgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"fetchurl\") {\n const request = options.fetch;\n if (!request || typeof request !== \"object\" || Array.isArray(request)) return { allowed: false, reason: \"A reviewed fetch request with a url is required in options.fetch.\" };\n const fetchrequest = request as Record<string, unknown>;\n if (typeof fetchrequest.url !== \"string\" || !fetchrequest.url.trim()) return { allowed: false, reason: \"The reviewed fetch request needs a non-empty url.\" };\n if (fetchrequest.method !== undefined && (typeof fetchrequest.method !== \"string\" || ![\"GET\", \"HEAD\", \"POST\", \"PUT\", \"PATCH\", \"DELETE\", \"OPTIONS\"].includes(fetchrequest.method.trim().toUpperCase()))) return { allowed: false, reason: \"The reviewed fetch method must be a known HTTP verb.\" };\n if (fetchrequest.headers !== undefined) {\n if (!fetchrequest.headers || typeof fetchrequest.headers !== \"object\" || Array.isArray(fetchrequest.headers)) return { allowed: false, reason: \"The reviewed header allowlist must be an object of custom headers.\" };\n for (const name of Object.keys(fetchrequest.headers as Record<string, unknown>)) {\n if (!name.trim()) return { allowed: false, reason: \"Header allowlists with empty names are refused.\" };\n if (typeof (fetchrequest.headers as Record<string, unknown>)[name] !== \"string\") return { allowed: false, reason: `The reviewed header ${name} needs a string value.` };\n }\n }\n if (fetchrequest.body !== undefined && typeof fetchrequest.body !== \"string\") return { allowed: false, reason: \"The reviewed fetch body must be a string.\" };\n if (fetchrequest.mode !== undefined && fetchrequest.mode !== \"cors\" && fetchrequest.mode !== \"no-cors\" && fetchrequest.mode !== \"same-origin\") return { allowed: false, reason: \"The reviewed fetch mode must be cors, no-cors or same-origin.\" };\n const consentgate = fetchconsentrefgranted(step);\n if (!consentgate.allowed) return consentgate;\n const policycheck = validatefetchoptions(options.fetchoptions);\n if (!policycheck.allowed) return policycheck;\n const fetchpolicy = fetchoptionsvalues(options.fetchoptions);\n const budget = fetchbudgetallowed(fetchpolicy.timeout, fetchpolicy.retries, fetchpolicy.backoff, fetchnumeric(options, \"wait\"));\n if (!budget.allowed) return budget;\n if (options.stream !== undefined) {\n if (!options.stream || typeof options.stream !== \"object\" || Array.isArray(options.stream)) return { allowed: false, reason: \"The reviewed stream window must be an object with an optional byte budget.\" };\n const streambudget = (options.stream as Record<string, unknown>).budget;\n if (streambudget !== undefined && (typeof streambudget !== \"number\" || !Number.isFinite(streambudget) || streambudget < 0)) return { allowed: false, reason: \"The reviewed stream byte budget must be zero or a positive number of bytes with no code ceiling.\" };\n }\n }\n if (kind === \"parsejson\") {\n if (!isnonempty(options.call)) return { allowed: false, reason: \"A reviewed stored call id is required in options.call before the body parses.\" };\n const fields = options.fields;\n if (!Array.isArray(fields) || fields.length === 0) return { allowed: false, reason: \"A reviewed non-empty list of json path rules is required in options.fields.\" };\n for (const item of fields) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) return { allowed: false, reason: \"Every json path rule must be an object.\" };\n const rule = item as Record<string, unknown>;\n if (!isnonempty(rule.name)) return { allowed: false, reason: \"Every json path rule needs a non-empty field name.\" };\n if (typeof rule.path !== \"string\" || !rule.path.trim() || !validpath(rule.path.trim())) return { allowed: false, reason: `The json path of ${rule.name} must be a dotted path of non-empty segments.` };\n if (rule.kind !== undefined && rule.kind !== \"text\" && rule.kind !== \"number\" && rule.kind !== \"boolean\" && rule.kind !== \"json\") return { allowed: false, reason: `The json path kind of ${rule.name} must be text, number, boolean or json.` };\n }\n }\n if (kind === \"parsehtml\") {\n if (!isnonempty(options.call)) return { allowed: false, reason: \"A reviewed stored call id is required in options.call before the markup parses.\" };\n const queries = options.queries;\n if (!Array.isArray(queries) || queries.length === 0) return { allowed: false, reason: \"A reviewed non-empty list of html queries is required in options.queries.\" };\n for (const item of queries) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) return { allowed: false, reason: \"Every html query must be an object.\" };\n const query = item as Record<string, unknown>;\n if (!isnonempty(query.selector)) return { allowed: false, reason: \"Every html query needs a selector from the reviewed selector grammar.\" };\n if (query.attribute !== undefined && !isnonempty(query.attribute)) return { allowed: false, reason: \"The reviewed html query attribute must be a non-empty attribute name.\" };\n if (query.multi !== undefined && typeof query.multi !== \"boolean\") return { allowed: false, reason: \"The reviewed html query multi flag must be a boolean.\" };\n }\n }\n if (kind === \"callrest\" || kind === \"callgraphql\") {\n if (!isnonempty(options.endpoint)) return { allowed: false, reason: \"A reviewed typed endpoint name is required in options.endpoint.\" };\n if (kind === \"callrest\") {\n if (options.payload !== undefined && (!options.payload || typeof options.payload !== \"object\" || Array.isArray(options.payload))) return { allowed: false, reason: \"The reviewed rest payload must be an object of reviewed values.\" };\n if (options.method !== undefined && (typeof options.method !== \"string\" || ![\"GET\", \"HEAD\", \"POST\", \"PUT\", \"PATCH\", \"DELETE\", \"OPTIONS\"].includes(options.method.trim().toUpperCase()))) return { allowed: false, reason: \"The reviewed endpoint method override must be a known HTTP verb.\" };\n if (options.success !== undefined && (!Array.isArray(options.success) || !options.success.every(code => typeof code === \"number\" && Number.isInteger(code)))) return { allowed: false, reason: \"The reviewed success status list must be a list of integer status codes.\" };\n }\n if (kind === \"callgraphql\") {\n const request = options.graphql;\n if (!request || typeof request !== \"object\" || Array.isArray(request)) return { allowed: false, reason: \"A reviewed graphql request with an operation is required in options.graphql.\" };\n const graphql = request as Record<string, unknown>;\n if (typeof graphql.query !== \"string\" || !graphql.query.trim()) return { allowed: false, reason: \"The reviewed graphql operation text must be a non-empty string.\" };\n if (graphql.operationkind !== \"query\" && graphql.operationkind !== \"mutation\") return { allowed: false, reason: \"The reviewed graphql operation kind must be query or mutation; unknown operation kinds are refused.\" };\n if (graphql.variables !== undefined && (!graphql.variables || typeof graphql.variables !== \"object\" || Array.isArray(graphql.variables))) return { allowed: false, reason: \"The reviewed graphql variables must be an object of reviewed values.\" };\n if (graphql.operationname !== undefined && !isnonempty(graphql.operationname)) return { allowed: false, reason: \"The reviewed graphql operation name must be a non-empty string.\" };\n }\n if (options.apikeys !== undefined && (!Array.isArray(options.apikeys) || !options.apikeys.every(name => isnonempty(name)))) return { allowed: false, reason: \"The reviewed api key reference list must be a list of non-empty stored names.\" };\n const policycheck = validatefetchoptions(options.fetchoptions);\n if (!policycheck.allowed) return policycheck;\n const fetchpolicy = fetchoptionsvalues(options.fetchoptions);\n const budget = fetchbudgetallowed(fetchpolicy.timeout, fetchpolicy.retries, fetchpolicy.backoff, fetchnumeric(options, \"wait\"));\n if (!budget.allowed) return budget;\n }\n return { allowed: true };\n}\n\n/** Validates one reviewed fetch policy object: timeout, retries, backoff base and redirect follow limit stay user choices with no code ceiling. */\nfunction validatefetchoptions(value: unknown): policyevaluation {\n if (value === undefined) return { allowed: true };\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"The reviewed fetch options must be an object with timeout, retries, backoff and follow.\" };\n const options = value as Record<string, unknown>;\n for (const key of [\"timeout\", \"backoff\"]) {\n if (options[key] !== undefined && (typeof options[key] !== \"number\" || !Number.isFinite(options[key]) || options[key] < 0)) return { allowed: false, reason: `The reviewed fetch ${key} must be zero or a positive number with no code ceiling.` };\n }\n for (const key of [\"retries\", \"follow\"]) {\n if (options[key] !== undefined && (typeof options[key] !== \"number\" || !Number.isInteger(options[key]) || options[key] < 0)) return { allowed: false, reason: `The reviewed fetch ${key} must be zero or a positive integer with no code ceiling.` };\n }\n return { allowed: true };\n}\n\n/** Reads the numeric fetch policy fields of one reviewed fetch options object. */\nfunction fetchoptionsvalues(value: unknown): { timeout?: number | undefined; retries?: number | undefined; backoff?: number | undefined } {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return {};\n const options = value as Record<string, unknown>;\n return { timeout: fetchnumeric(options, \"timeout\"), retries: fetchnumeric(options, \"retries\"), backoff: fetchnumeric(options, \"backoff\") };\n}\n\n/** Reads one numeric fetch policy field from the step options. */\nfunction fetchnumeric(options: Record<string, unknown>, key: string): number | undefined {\n const value = options[key];\n return typeof value === \"number\" && Number.isFinite(value) ? value : undefined;\n}\n\n/** True when the kind records user activity and needs the reviewed recording consent before it starts. */\nexport function isrecordingkind(kind: actionkind): boolean {\n return kind === \"recordscreen\" || kind === \"captureaudio\";\n}\n\n/** Validates the reviewed socket and stream parameter grammar of the 1.1.43 family: channel urls with protocols, reconnect budgets and backoff ceilings, multiplexed message payloads, message filters with dotted paths and match limits, event subscriptions with cancellation paths and long poll cursors with intervals kept inside the reviewed wait budget; every bound stays a user choice with no code ceiling. */\nfunction validatesocketgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"opensocket\") {\n const channel = channeloptionsof(options.socket);\n if (!channel) return { allowed: false, reason: \"A reviewed socket with a url is required in options.socket.\" };\n if (channel.options.reconnect !== undefined && !Number.isInteger(channel.options.reconnect)) return { allowed: false, reason: \"The reviewed socket reconnect budget must be an integer attempt count with no code ceiling.\" };\n for (const label of [\"backoff\", \"backoffceiling\"] as const) {\n const value = channel.options[label];\n if (value !== undefined && (typeof value !== \"number\" || !Number.isFinite(value) || value < 0)) return { allowed: false, reason: `The reviewed socket ${label} must be zero or a positive number of milliseconds with no code ceiling.` };\n }\n if (channel.options.lifetime !== undefined && (typeof channel.options.lifetime !== \"number\" || !Number.isFinite(channel.options.lifetime) || channel.options.lifetime <= 0)) return { allowed: false, reason: \"The reviewed socket lifetime window must be a positive number of milliseconds.\" };\n }\n if (kind === \"sendmessage\") {\n const message = options.message;\n if (!message || typeof message !== \"object\" || Array.isArray(message)) return { allowed: false, reason: \"A reviewed message with a channel, stream and payload is required in options.message.\" };\n const envelope = message as Record<string, unknown>;\n if (!isnonempty(envelope.channel)) return { allowed: false, reason: \"The reviewed message needs the open channel id in options.message.channel.\" };\n if (envelope.stream !== undefined && !isnonempty(envelope.stream)) return { allowed: false, reason: \"The reviewed message stream name must be a non-empty string.\" };\n if (typeof envelope.payload !== \"string\") return { allowed: false, reason: \"The reviewed message payload must be a string.\" };\n }\n if (kind === \"waitmessage\") {\n if (options.filter !== undefined) {\n const filter = options.filter;\n if (!filter || typeof filter !== \"object\" || Array.isArray(filter)) return { allowed: false, reason: \"The reviewed message filter must be an object of stream, path and limit.\" };\n const reviewed = filter as Record<string, unknown>;\n if (reviewed.stream !== undefined && !isnonempty(reviewed.stream)) return { allowed: false, reason: \"The reviewed message filter stream name must be a non-empty string.\" };\n if (reviewed.path !== undefined && (typeof reviewed.path !== \"string\" || !validpath(reviewed.path.trim()))) return { allowed: false, reason: \"The reviewed message filter path must be a dotted path of non-empty segments.\" };\n if (reviewed.limit !== undefined && (typeof reviewed.limit !== \"number\" || !Number.isInteger(reviewed.limit) || reviewed.limit < 1)) return { allowed: false, reason: \"The reviewed message match limit must be a positive integer with no code ceiling.\" };\n }\n if (options.wait !== undefined && (typeof options.wait !== \"number\" || !Number.isFinite(options.wait) || options.wait < 0)) return { allowed: false, reason: \"The reviewed message wait budget must be zero or a positive number of milliseconds.\" };\n }\n if (kind === \"subscribesse\") {\n const subscription = subscriptionoptionsof(options.subscription);\n if (!subscription) return { allowed: false, reason: \"A reviewed subscription with an event stream url and a cancellation path is required in options.subscription.\" };\n const rawlifetime = options.subscription && typeof options.subscription === \"object\" && !Array.isArray(options.subscription) ? (options.subscription as Record<string, unknown>).lifetime : undefined;\n if (rawlifetime !== undefined && (typeof rawlifetime !== \"number\" || !Number.isFinite(rawlifetime) || rawlifetime <= 0)) return { allowed: false, reason: \"The reviewed subscription lifetime window must be a positive number of milliseconds.\" };\n }\n if (kind === \"longpoll\") {\n const cursor = pollcursorof(options.poll);\n if (!cursor) return { allowed: false, reason: \"A reviewed poll cursor with a url, cursor field, interval and stop condition is required in options.poll.\" };\n const wait = options.wait;\n if (wait !== undefined && (typeof wait !== \"number\" || !Number.isFinite(wait) || wait < 0)) return { allowed: false, reason: \"The reviewed long poll wait budget must be zero or a positive number of milliseconds.\" };\n if (wait !== undefined && cursor.interval > wait) return { allowed: false, reason: `The long poll interval of ${cursor.interval} milliseconds exceeds the reviewed wait budget of ${wait} milliseconds; review a wider budget or a shorter interval.` };\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed request observation parameter grammar of the 1.1.43 family: watch windows with user configured match limits, header filters whose redaction list is required before any header value is stored, body filters with url patterns, mime lists and byte ceilings and api replay specs with known verbs and dotted extraction paths. */\nfunction validatenetwatchgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"watchrequests\") {\n if (options.watch !== undefined) {\n const watch = options.watch;\n if (!watch || typeof watch !== \"object\" || Array.isArray(watch)) return { allowed: false, reason: \"The reviewed watch window must be an object.\" };\n const reviewed = watch as Record<string, unknown>;\n if (reviewed.window !== undefined && (typeof reviewed.window !== \"number\" || !Number.isFinite(reviewed.window) || reviewed.window < 0)) return { allowed: false, reason: \"The reviewed watch window must be zero or a positive number of milliseconds.\" };\n }\n if (options.limit !== undefined && (typeof options.limit !== \"number\" || !Number.isInteger(options.limit) || options.limit < 1)) return { allowed: false, reason: \"The reviewed watch match limit must be a positive integer with no code ceiling.\" };\n }\n if (kind === \"readheaders\") {\n const headers = options.headers;\n if (!headers || typeof headers !== \"object\" || Array.isArray(headers)) return { allowed: false, reason: \"A reviewed header filter with a name allowlist and a redaction list is required in options.headers.\" };\n const reviewed = headers as Record<string, unknown>;\n if (!Array.isArray(reviewed.allow) || reviewed.allow.length === 0 || !reviewed.allow.every((name): name is string => isnonempty(name))) return { allowed: false, reason: \"The reviewed header allowlist must be a non-empty list of header names.\" };\n if (!Array.isArray(reviewed.redact) || reviewed.redact.length === 0 || !reviewed.redact.every((name): name is string => isnonempty(name))) return { allowed: false, reason: \"Header capture requires a reviewed redaction list before any header value is stored.\" };\n }\n if (kind === \"capturebodies\") {\n const body = options.body;\n if (!body || typeof body !== \"object\" || Array.isArray(body)) return { allowed: false, reason: \"A reviewed body filter with a url pattern, mime list and byte ceiling is required in options.body.\" };\n const reviewed = body as Record<string, unknown>;\n if (reviewed.urlpattern !== undefined && !isnonempty(reviewed.urlpattern)) return { allowed: false, reason: \"The reviewed body url pattern must be a non-empty string.\" };\n if (reviewed.mimes !== undefined && (!Array.isArray(reviewed.mimes) || reviewed.mimes.length === 0 || !reviewed.mimes.every((mime): mime is string => isnonempty(mime)))) return { allowed: false, reason: \"The reviewed body mime list must be a non-empty list of mime types.\" };\n if (reviewed.ceiling !== undefined && (typeof reviewed.ceiling !== \"number\" || !Number.isFinite(reviewed.ceiling) || reviewed.ceiling < 0)) return { allowed: false, reason: \"The reviewed body byte ceiling must be zero or a positive number of bytes with no code ceiling.\" };\n }\n if (kind === \"mapapi\") {\n if (options.limit !== undefined && (typeof options.limit !== \"number\" || !Number.isInteger(options.limit) || options.limit < 1)) return { allowed: false, reason: \"The reviewed mapapi match limit must be a positive integer with no code ceiling.\" };\n }\n if (kind === \"extractapi\") {\n const replay = apireplayspecof(options.replay);\n if (!replay) return { allowed: false, reason: \"A reviewed replay spec with an endpoint is required in options.replay.\" };\n if (!ishttpsurl(replay.endpoint)) return { allowed: false, reason: \"The reviewed replay endpoint must be an HTTPS url.\" };\n if (replay.verb !== undefined && ![\"GET\", \"HEAD\", \"POST\", \"PUT\", \"PATCH\", \"DELETE\", \"OPTIONS\"].includes(replay.verb)) return { allowed: false, reason: \"The reviewed replay verb must be a known HTTP verb.\" };\n for (const path of replay.paths ?? []) {\n if (!validpath(path.trim())) return { allowed: false, reason: `The reviewed replay extraction path ${path} must be a dotted path of non-empty segments.` };\n }\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed network control parameter grammar of the 1.1.44 family: block rules with url patterns that name their origin, mock fixtures reviewed with their full body, header rewrite rules with named origin patterns and set, append and remove operations, cookie records scoped to granted domains, oauth flows with provider consent refs, api key entries behind explicit consent, proxy routes with required bypass lists, urlencoded form payloads and multipart uploads whose every file carries the explicit reviewed flag; every bound stays a user choice with no code ceiling. */\nfunction validatecontrolgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"blockrequest\") {\n const rule = blockruleof(options.block);\n if (!rule) return { allowed: false, reason: \"A reviewed block rule with a url pattern is required in options.block.\" };\n if (patternorigin(rule.urlpattern) === undefined) return { allowed: false, reason: \"Block rules need an https origin pattern; patterns without a named origin are refused.\" };\n if ((options.block as Record<string, unknown>).reviewed !== true) return { allowed: false, reason: \"The block rule carries the explicit reviewed flag before any request is blocked.\" };\n }\n if (kind === \"mockresponse\") {\n const spec = mockspecof(options.mock);\n if (!spec) return { allowed: false, reason: \"A reviewed mock fixture with a url pattern, status and its reviewed body or a captured body ref is required in options.mock.\" };\n if (patternorigin(spec.urlpattern) === undefined) return { allowed: false, reason: \"Mock fixtures need an https origin pattern; patterns without a named origin are refused.\" };\n if (spec.reviewed !== true) return { allowed: false, reason: \"Every mock fixture is reviewed with its full body or the referenced captured body through the explicit reviewed flag before it serves.\" };\n }\n if (kind === \"rewriteheaders\") {\n const rules = options.rules;\n if (!Array.isArray(rules) || rules.length === 0) return { allowed: false, reason: \"A reviewed non-empty list of header rewrite rules is required in options.rules.\" };\n for (const item of rules) {\n const rule = headeruleof(item);\n if (!rule) return { allowed: false, reason: \"Every header rewrite rule needs a url pattern, header name, a set, append or remove operation and its value.\" };\n if (patternorigin(rule.urlpattern) === undefined) return { allowed: false, reason: \"Header rewrite rules must name their origin pattern explicitly; patterns without a named origin are refused.\" };\n }\n }\n if (kind === \"setcookies\") {\n const cookies = options.cookies;\n if (!Array.isArray(cookies) || cookies.length === 0) return { allowed: false, reason: \"A reviewed non-empty list of cookie records is required in options.cookies.\" };\n for (const item of cookies) {\n if (!cookierecordof(item)) return { allowed: false, reason: \"Every cookie record needs a name, domain, path and reviewed string value with an optional expiry.\" };\n }\n }\n if (kind === \"readcookies\" && options.domain !== undefined && !isnonempty(options.domain)) return { allowed: false, reason: \"The reviewed cookie read domain must be a non-empty host.\" };\n if (kind === \"clearcookies\") {\n if (!isnonempty(options.domain)) return { allowed: false, reason: \"A reviewed cookie domain is required before cookies are cleared.\" };\n if (options.names !== undefined && (!Array.isArray(options.names) || options.names.length === 0 || !options.names.every((name): name is string => isnonempty(name)))) return { allowed: false, reason: \"The reviewed cookie clear list must be a non-empty list of cookie names when present.\" };\n }\n if (kind === \"authflow\") {\n const flow = oauthflowof(options.oauth);\n if (!flow) return { allowed: false, reason: \"A reviewed oauth flow with provider, authorize url, token url, scopes and redirect origin is required in options.oauth.\" };\n if (!ishttpsurl(flow.authorizeurl) || !ishttpsurl(flow.tokenurl)) return { allowed: false, reason: \"The oauth authorize and token urls must use HTTPS.\" };\n if (!ishttpsurl(flow.redirectorigin) && !/^https:\\/\\/[^/]+\\/?$/.test(flow.redirectorigin)) return { allowed: false, reason: \"The oauth redirect origin must be an HTTPS origin inside the grants.\" };\n const consent = authconsentgranted(step);\n if (!consent.allowed) return consent;\n }\n if (kind === \"saveapikey\") {\n const key = options.key;\n if (!key || typeof key !== \"object\" || Array.isArray(key)) return { allowed: false, reason: \"A reviewed api key entry with name, origin scopes and header is required in options.key.\" };\n const entry = key as Record<string, unknown>;\n if (!isnonempty(entry.name)) return { allowed: false, reason: \"The api key entry needs a reviewed non-empty name.\" };\n if (!Array.isArray(entry.origins) || entry.origins.length === 0 || !entry.origins.every((item): item is string => ishttpsurl(item))) return { allowed: false, reason: \"The api key needs a reviewed non-empty list of HTTPS origin scopes.\" };\n if (!isnonempty(entry.header)) return { allowed: false, reason: \"The api key entry needs a reviewed non-empty header name.\" };\n if (typeof entry.value !== \"string\" || !entry.value) return { allowed: false, reason: \"The api key needs its secret value in the reviewed options; it never enters the audit trail.\" };\n const consent = apikeyconsentgranted(step);\n if (!consent.allowed) return consent;\n }\n if (kind === \"routeproxy\") {\n if (!proxyrouteof(options.proxy)) return { allowed: false, reason: \"A reviewed proxy route with scheme, host, port and a non-empty bypass list is required in options.proxy.\" };\n if (!isnonempty(options.consentref)) return { allowed: false, reason: \"Proxy routing needs the explicit reviewed consent ref before any route applies.\" };\n }\n if (kind === \"postform\") {\n const form = formpayloadof(options.form);\n if (!form) return { allowed: false, reason: \"A reviewed form payload with a url and a non-empty field list is required in options.form.\" };\n if (!ishttpsurl(form.url)) return { allowed: false, reason: \"The form submission target must use HTTPS.\" };\n if (options.wait !== undefined && (typeof options.wait !== \"number\" || !Number.isFinite(options.wait) || options.wait < 0)) return { allowed: false, reason: \"The reviewed rate limit wait budget must be zero or a positive number of milliseconds.\" };\n }\n if (kind === \"postfiles\") {\n const upload = multipartpayloadof(options.upload);\n if (!upload) return { allowed: false, reason: \"A reviewed multipart upload with a url and reviewed files is required in options.upload; every file carries the explicit reviewed flag.\" };\n if (!ishttpsurl(upload.url)) return { allowed: false, reason: \"The multipart upload target must use HTTPS.\" };\n if (options.wait !== undefined && (typeof options.wait !== \"number\" || !Number.isFinite(options.wait) || options.wait < 0)) return { allowed: false, reason: \"The reviewed rate limit wait budget must be zero or a positive number of milliseconds.\" };\n }\n return { allowed: true };\n}\n\n/** Requires the reviewed block rule of a live session before any blockrequest runs: the rule must carry the explicit reviewed flag and the session must stay active, unpaused and unexpired; every rule applies for the run only and reverts at run end. */\nexport function blockgate(session: agentsession | undefined, step: toolstep, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the request block.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot block requests.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot block requests.\" };\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const rule = options.block;\n if (!rule || typeof rule !== \"object\" || Array.isArray(rule) || (rule as Record<string, unknown>).reviewed !== true) return { allowed: false, reason: \"Request blocking needs its reviewed block rule with the explicit reviewed flag before any rule applies.\" };\n if (!blockruleof(rule)) return { allowed: false, reason: \"The block rule needs a url pattern and an optional resource type list.\" };\n return { allowed: true };\n}\n\n/** Scopes every cookie kind to a granted domain of a live session: the domain must equal a granted origin host or sit beneath it, and every other domain is refused. */\nexport function cookiegate(session: agentsession | undefined, domain: string, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the cookie operation.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot touch cookies.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot touch cookies.\" };\n const grants = session.grants ?? [session.origin];\n if (!cookiedomaingranted(domain, grants)) return { allowed: false, reason: `The cookie domain ${domain} stays outside the session origin grants; cookie control refuses domains beyond the grants.` };\n return { allowed: true };\n}\n\n/** Requires the explicit reviewed consent before routeproxy changes routing: a live session, a reviewed consent ref and a valid route with its bypass list; the route applies for the run only and restores the previous state at run end. */\nexport function proxygate(session: agentsession | undefined, step: toolstep, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the proxy route.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot change routing.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot change routing.\" };\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n if (!isnonempty(options.consentref)) return { allowed: false, reason: \"Proxy routing needs the explicit reviewed consent ref before any route applies.\" };\n if (!proxyrouteof(options.proxy)) return { allowed: false, reason: \"The proxy route needs a scheme, host, port and a non-empty bypass list of origins that stay direct.\" };\n return { allowed: true };\n}\n\n/** Requires the reviewed provider consent prompt ref before any authflow runs. */\nexport function authconsentgranted(step: toolstep): policyevaluation {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const consentref = options.consentref;\n if (typeof consentref !== \"string\" || !consentref.trim()) return { allowed: false, reason: \"An oauth flow requires the reviewed provider consent prompt ref in options before it starts.\" };\n return { allowed: true };\n}\n\n/** Requires the explicit consent prompt ref before saveapikey stores a key. */\nexport function apikeyconsentgranted(step: toolstep): policyevaluation {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const consentref = options.consentref;\n if (typeof consentref !== \"string\" || !consentref.trim()) return { allowed: false, reason: \"Storing an api key requires the explicit reviewed consent prompt ref in options before anything is stored.\" };\n return { allowed: true };\n}\n\n/** Keeps the rate limit wait inside the reviewed wait budget as user configured behavior: the wait until the reset window passes must fit when a budget was reviewed; both bounds stay user choices with no code ceiling. */\nexport function ratelimitbudgetallowed(wait: number | undefined, budget: number | undefined): policyevaluation {\n if (wait !== undefined && (typeof wait !== \"number\" || !Number.isFinite(wait) || wait < 0)) return { allowed: false, reason: \"The rate limit wait must be zero or a positive number of milliseconds.\" };\n if (budget !== undefined && (typeof budget !== \"number\" || !Number.isFinite(budget) || budget < 0)) return { allowed: false, reason: \"The reviewed rate limit budget must be zero or a positive number of milliseconds.\" };\n if (wait !== undefined && budget !== undefined && wait > budget) return { allowed: false, reason: `The rate limit wait of ${wait} milliseconds exceeds the reviewed budget of ${budget} milliseconds; review a wider budget or submit later.` };\n return { allowed: true };\n}\n\n/** Requires the active tab grant of the live session for every debugging kind: the timeline gate scopes console, error and task capture to the run tab only and refuses every other tab. */\nexport function timelinegate(session: agentsession | undefined, tabid: number, origin: string, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the timeline capture.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot capture the timeline.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot capture the timeline.\" };\n if (session.tabid !== tabid) return { allowed: false, reason: `The timeline capture needs the run tab ${session.tabid} and refuses tab ${tabid}.` };\n if (!origingranted(session, origin)) return { allowed: false, reason: `The timeline capture of ${origin} needs the session origin grants first.` };\n return { allowed: true };\n}\n\n/** True when one approved console capture consent of that origin exists; console capture on a new origin prompts once and the approved decision persists. */\nexport function consoleconsentcovers(origin: string, consents: consoleconsentrecord[]): policyevaluation {\n if (consents.some(consent => consent.origin === origin && consent.approved === true)) return { allowed: true };\n return { allowed: false, reason: `Console capture on ${origin} needs the reviewed console consent first; approve the prompt in the review panel.` };\n}\n\n/** Requires the granted origin before stack frames are captured; stack capture outside the granted origin is refused. */\nexport function stackgate(session: agentsession | undefined, origin: string): policyevaluation {\n if (!origingranted(session, origin)) return { allowed: false, reason: `Stack capture of ${origin} stays outside the session origin grants.` };\n return { allowed: true };\n}\n\n/** Keeps the debug watch window inside the reviewed wait budget: the watch wait must fit the reviewed budget when one was reviewed; both bounds stay user choices with no code ceiling. */\nexport function debugwaitbudgetallowed(watchwindow: number | undefined, wait: number | undefined): policyevaluation {\n if (watchwindow !== undefined && (typeof watchwindow !== \"number\" || !Number.isFinite(watchwindow) || watchwindow < 0)) return { allowed: false, reason: \"The debug watch window must be zero or a positive number of milliseconds.\" };\n if (wait !== undefined && (typeof wait !== \"number\" || !Number.isFinite(wait) || wait < 0)) return { allowed: false, reason: \"The reviewed debug wait budget must be zero or a positive number of milliseconds.\" };\n if (watchwindow !== undefined && wait !== undefined && watchwindow > wait) return { allowed: false, reason: `The debug watch window of ${watchwindow} milliseconds exceeds the reviewed wait budget of ${wait} milliseconds; review a wider budget or a shorter window.` };\n return { allowed: true };\n}\n\n/** Exposes the timeline retention window as a user configured choice; an absent value keeps every timeline entry forever while the level count summaries always survive. */\nexport function timelineretentionwindow(settings: runsettings | undefined): number | undefined {\n return settings?.timelineretention;\n}\n\n/** Grades console diffing as read only comparison evidence: the diff compares two stored console outputs and touches no page or browser state. */\nexport function diffreviewgrade(): { risk: \"read\"; mode: \"diffing\"; evidence: \"comparison\" } {\n return { risk: \"read\", mode: \"diffing\", evidence: \"comparison\" };\n}\n\n/** Validates the reviewed debugging parameter grammar of the 1.1.45 family: a watch window inside the reviewed wait budget, level floors from the reviewed level set, source filters from the reviewed source grammar, spam rules with user configured thresholds, serialization depth bounds, rotation rules with no hardcoded entry ceiling and the required redaction pattern list before any console text is captured. */\nfunction validatetimelinegrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n let watchwindow: number | undefined;\n if (options.watch !== undefined) {\n const watch = options.watch;\n if (!watch || typeof watch !== \"object\" || Array.isArray(watch)) return { allowed: false, reason: \"The reviewed debug watch window must be an object.\" };\n const reviewed = watch as Record<string, unknown>;\n if (reviewed.window !== undefined) {\n if (typeof reviewed.window !== \"number\" || !Number.isFinite(reviewed.window) || reviewed.window < 0) return { allowed: false, reason: \"The reviewed debug watch window must be zero or a positive number of milliseconds.\" };\n watchwindow = reviewed.window;\n }\n }\n const budgetcheck = debugwaitbudgetallowed(watchwindow, typeof options.wait === \"number\" ? options.wait : undefined);\n if (!budgetcheck.allowed) return budgetcheck;\n if (options.level !== undefined && !loglevels.includes(options.level as loglevel)) return { allowed: false, reason: `The reviewed level floor must be one of ${loglevels.join(\", \")}.` };\n if (options.sources !== undefined) {\n if (!Array.isArray(options.sources) || options.sources.length === 0 || !options.sources.every(source => timelinesources.includes(source as never))) return { allowed: false, reason: `The reviewed source filters must be a non-empty list of the reviewed timeline sources: ${timelinesources.join(\", \")}.` };\n }\n if (kind === \"watchconsole\") {\n if (options.redact === undefined || !Array.isArray(options.redact) || options.redact.length === 0 || !options.redact.every(pattern => isnonempty(pattern))) return { allowed: false, reason: \"Console capture requires a reviewed non-empty redaction pattern list before any console text is captured.\" };\n if (options.depth !== undefined && (typeof options.depth !== \"number\" || !Number.isInteger(options.depth) || options.depth < 1)) return { allowed: false, reason: \"The reviewed serialization depth bound must be a positive integer with no code ceiling.\" };\n if (options.spam !== undefined) {\n const rule = spamruleof(options.spam);\n if (!rule) return { allowed: false, reason: \"The reviewed spam rule needs a pattern, a window size and a collapse threshold.\" };\n if (rule.collapse < 1) return { allowed: false, reason: \"The reviewed spam collapse threshold must be a positive integer of user configured value with no code ceiling.\" };\n }\n if (options.rotation !== undefined) {\n const rule = rotationruleof(options.rotation);\n if (!rule) return { allowed: false, reason: \"The reviewed rotation rule needs a max entry count and an overflow target.\" };\n }\n }\n if (kind === \"watchtasks\") {\n if (options.threshold !== undefined && (typeof options.threshold !== \"number\" || !Number.isFinite(options.threshold) || options.threshold < 0)) return { allowed: false, reason: \"The reviewed long task threshold must be zero or a positive number of milliseconds with no code ceiling.\" };\n }\n return { allowed: true };\n}\n\n/** Normalizes a reviewed spam rule: the pattern, the window size and the collapse threshold as user configured values. */\nexport function spamruleof(value: unknown): spamrule | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const pattern = typeof entry.pattern === \"string\" ? entry.pattern : \"\";\n const windowsize = typeof entry.windowsize === \"number\" && Number.isFinite(entry.windowsize) && entry.windowsize >= 0 ? entry.windowsize : undefined;\n const collapse = typeof entry.collapse === \"number\" && Number.isInteger(entry.collapse) ? entry.collapse : undefined;\n if (windowsize === undefined || collapse === undefined) return undefined;\n return { pattern, windowsize, collapse };\n}\n\n/** Normalizes a reviewed log rotation rule: the max entries per run and the overflow target store with no hardcoded entry ceiling. */\nexport function rotationruleof(value: unknown): rotationrule | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const maxentries = typeof entry.maxentries === \"number\" && Number.isInteger(entry.maxentries) && entry.maxentries >= 1 ? entry.maxentries : undefined;\n const overflowtarget = typeof entry.overflowtarget === \"string\" && entry.overflowtarget.trim() ? entry.overflowtarget.trim() : undefined;\n if (maxentries === undefined || overflowtarget === undefined) return undefined;\n return { maxentries, overflowtarget };\n}\n\n/** Requires the active run tab grant of the live session for every devtools protocol kind: the debug gate scopes attaches, commands, watches, breakpoints, steps and overrides to the run tab only and refuses every other tab. */\nexport function debuggate(session: agentsession | undefined, tabid: number, origin: string, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the devtools protocol step.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot run a devtools protocol step.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot run a devtools protocol step.\" };\n if (session.tabid !== tabid) return { allowed: false, reason: `The devtools protocol step needs the run tab ${session.tabid} and refuses tab ${tabid}.` };\n if (!origingranted(session, origin)) return { allowed: false, reason: `The devtools protocol step on ${origin} needs the session origin grants first.` };\n return { allowed: true };\n}\n\n/** True when one approved debugger consent of that origin covers every requested domain; the first attachcdp of a run needs the approved record and revocation removes the coverage. */\nexport function debuggerconsentcovers(origin: string, domains: string[], grants: debuggergrant[]): policyevaluation {\n const needed = [...new Set(domains)];\n const covering = grants.find(grant => grant.origin === origin && grant.approved === true && grant.revokedat === undefined && needed.every(domain => grant.domains.includes(domain)));\n if (covering) return { allowed: true };\n if (grants.some(grant => grant.origin === origin && grant.revokedat !== undefined)) return { allowed: false, reason: `The debugger consent on ${origin} was revoked; approve a new prompt before the devtools protocol runs again.` };\n return { allowed: false, reason: `The devtools protocol on ${origin} needs the reviewed debugger consent for ${needed.join(\", \")} first; approve the prompt with the domain allowlist shown in the review panel.` };\n}\n\n/** The profiling target gate of every 1.1.47 kind: the live session run tab and origin grants come first, every iframe, worker and service worker target stays inside the granted origins, and the reviewed debugger grant of the origin covers every profiling instrument because profiling is debugger grade instrumentation. */\nexport function targetgate(input: { session: agentsession | undefined; tabid: number; origin: string; targets: attachtarget[]; grants: debuggergrant[] | undefined; now: number }): policyevaluation {\n const base = debuggate(input.session, input.tabid, input.origin, input.now);\n if (!base.allowed) return base;\n for (const target of input.targets) {\n if (target.kind === \"page\") continue;\n const origincheckresult = origincheck(input.session, target.url);\n if (!origincheckresult.allowed) return { allowed: false, reason: `The ${target.kind} target ${target.url} stays outside the granted origins; profiling refuses to attach.` };\n }\n if (input.grants === undefined) return { allowed: true };\n const consent = debuggerconsentcovers(input.origin, [], input.grants);\n if (!consent.allowed) return { allowed: false, reason: `The profiling step on ${input.origin} needs the reviewed debugger grant of the origin first; approve the prompt with the profiling derivation shown in the review panel.` };\n return { allowed: true };\n}\n\n/** True when one approved source map capture consent of that origin covers the capture; revocation removes the coverage and the next capture needs a new reviewed prompt. */\nexport function sourcemapconsentcovers(origin: string, consents: sourcemapconsent[]): policyevaluation {\n const covering = consents.find(consent => consent.origin === origin && consent.approved === true && consent.revokedat === undefined);\n if (covering) return { allowed: true };\n if (consents.some(consent => consent.origin === origin && consent.revokedat !== undefined)) return { allowed: false, reason: `The source map capture consent on ${origin} was revoked; approve a new prompt before another map file is fetched.` };\n return { allowed: false, reason: `The source map capture on ${origin} needs the reviewed per origin consent first; approve the prompt shown in the review panel.` };\n}\n\n/** Exposes the user configured retention window for the heavy profile bytes; an absent window keeps every snapshot, sample and trace file. */\nexport function profileretentionwindow(settings: runsettings | undefined): number | undefined {\n return settings?.profileretention;\n}\n\n/** Exposes the user configured trace byte ceiling; an absent value never refuses a trace export because the cap stays a user choice only. */\nexport function traceceilingof(settings: runsettings | undefined): number | undefined {\n return settings?.traceceiling;\n}\n\n/** Validates one breakpoint condition against the reviewed expression grammar: member chains, literals of number, string, boolean and null, comparison and logic operators, negation and parentheses; assignments, calls and statements are refused. */\nexport function validatebreakpointcondition(condition: string): policyevaluation {\n const expression = condition.trim();\n if (expression.length === 0) return { allowed: false, reason: \"The breakpoint condition must not be empty.\" };\n if (/(?<![=!<>])=(?!=)/.test(expression)) return { allowed: false, reason: \"Breakpoint conditions refuse assignment because the reviewed grammar is comparison only.\" };\n if (/[A-Za-z_$][\\w$]*\\s*\\(/.test(expression)) return { allowed: false, reason: \"Breakpoint conditions refuse calls because the reviewed grammar is comparison only.\" };\n const literal = /^(?:-?\\d+(?:\\.\\d+)?|\"(?:[^\"\\\\]|\\\\.)*\"|'(?:[^'\\\\]|\\\\.)*'|true|false|null)$/;\n const tokens = expression.match(/(?:[A-Za-z_$][\\w$]*|-?\\d+(?:\\.\\d+)?|\"(?:[^\"\\\\]|\\\\.)*\"|'(?:[^'\\\\]|\\\\.)*'|===|!==|==|!=|>=|<=|&&|\\|\\||[!.<>()+\\-*\\/%])/g);\n if (tokens === null || tokens.join(\"\") !== expression.replace(/\\s+/g, \"\")) return { allowed: false, reason: \"The breakpoint condition must use the reviewed expression grammar of member chains, literals, comparisons, logic operators, negation and parentheses.\" };\n const identifierlike = /^(?:true|false|null)$/;\n for (const token of tokens) {\n if (literal.test(token) || identifierlike.test(token)) continue;\n if ([\"===\", \"!==\", \"==\", \"!=\", \">=\", \"<=\", \"&&\", \"||\", \"!\", \".\", \"(\", \")\", \"<\", \">\", \"+\", \"-\", \"*\", \"/\", \"%\"].includes(token)) continue;\n if (/^[A-Za-z_$][\\w$]*$/.test(token)) continue;\n return { allowed: false, reason: `The token ${token} of the breakpoint condition stays outside the reviewed expression grammar.` };\n }\n return { allowed: true };\n}\n\n/** Keeps the breakpoint count of one run inside the user configured ceiling: an absent ceiling never refuses a breakpoint because the cap stays a user choice only. */\nexport function breakpointbudgetallowed(active: number, ceiling: number | undefined): policyevaluation {\n if (ceiling === undefined) return { allowed: true };\n if (typeof ceiling !== \"number\" || !Number.isInteger(ceiling) || ceiling < 0) return { allowed: false, reason: \"The reviewed breakpoint ceiling must be zero or a positive integer of user configured value with no code ceiling.\" };\n if (active >= ceiling) return { allowed: false, reason: `The run already holds ${active} active breakpoint${active === 1 ? \"\" : \"s\"} and the reviewed breakpoint ceiling is ${ceiling}; revert one or review a wider ceiling.` };\n return { allowed: true };\n}\n\n/** Exposes the pause capture retention window as a user configured choice; an absent value keeps every pause capture with its call frames. */\nexport function pauseretentionwindow(settings: runsettings | undefined): number | undefined {\n return settings?.pauseretention;\n}\n\n/** Exposes the user configured breakpoint ceiling; an absent value never refuses a breakpoint because the cap stays a user choice only. */\nexport function breakpointceilingof(settings: runsettings | undefined): number | undefined {\n return settings?.breakpointceiling;\n}\n\n/** Requires the review of every emulation layer before it applies: a live session on the run tab, an approved plan, the explicit reviewed flag on the layer options and the reviewed revert plan beside it. */\nexport function emugate(input: { session: agentsession | undefined; plan: agentplan | undefined; step: toolstep; tabid: number; origin: string; now: number }): policyevaluation {\n const gate = sessiongate({ session: input.session, tabid: input.tabid, origin: input.origin, now: input.now, action: \"emulate the run tab\" });\n if (!gate.allowed) return gate;\n if (!input.plan || input.plan.state !== \"approved\") return { allowed: false, reason: \"Emulation layers need an approved plan before they apply.\" };\n let options: Record<string, unknown> = {};\n try { options = parseoptions(input.step); } catch { options = {}; }\n if (options.reviewed !== true) return { allowed: false, reason: `The ${input.step.kind} layer needs the explicit reviewed flag before any mask applies.` };\n if (revertplanof(options.revertplan) === undefined) return { allowed: false, reason: `Every ${input.step.kind} layer needs a reviewed revert plan beside it before any mask applies.` };\n return { allowed: true };\n}\n\n/** Allows layer stacking only when the reviewed plan lists the steps: a second layer of one family needs at least two reviewed steps of that family in the same plan because the last applied layer wins conflicts. */\nexport function emulationstackallowed(plan: agentplan | undefined, kind: actionkind, active: number): policyevaluation {\n if (!plan) return { allowed: false, reason: \"Layer stacking needs the reviewed plan first.\" };\n const listed = plan.steps.filter(step => step.kind === kind).length;\n if (active >= listed) return { allowed: false, reason: `The plan lists ${listed} reviewed ${kind} step${listed === 1 ? \"\" : \"s\"} and ${active} layer${active === 1 ? \"\" : \"s\"} of that family are already active; stacking beyond the reviewed plan is refused.` };\n return { allowed: true };\n}\n\n/** True when one approved location consent of that origin covers the reviewed coordinates; the prompt shows the exact latitude and longitude before emulatelocate applies. */\nexport function locationconsentgate(origin: string, latitude: number, longitude: number, consents: locationconsent[]): policyevaluation {\n if (consents.some(consent => consent.origin === origin && consent.revokedat !== undefined)) return { allowed: false, reason: `The location consent on ${origin} was revoked; approve a new prompt before the location override runs again.` };\n if (locationconsentcovers(origin, latitude, longitude, consents)) return { allowed: true };\n return { allowed: false, reason: `The location override of ${latitude}, ${longitude} on ${origin} needs the reviewed location consent first; approve the prompt with the coordinates shown in the review panel.` };\n}\n\n/** Exposes the user configured retention window for reverted emulation layer states; an absent window keeps every prior state while the layer history always survives. */\nexport function emulationretentionwindow(settings: runsettings | undefined): number | undefined {\n return settings?.emulationretention;\n}\n\n/** Validates the reviewed emulation parameter grammar of the 1.1.48 family: device presets with width, height, pixel ratio and the mobile flag plus the reviewed reload flag, network presets with latency, download and upload bounds and the offline window, location presets inside the latitude and longitude ranges behind the location consent, agent presets of the reviewed user agent grammar with platform and brand list, permission overrides of the reviewed browser permission set graded by name, blackbox rules of explicit origin patterns with their trace scope, and the reviewed revert plan beside every layer. */\nfunction validateemulationgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (revertplanof(options.revertplan) === undefined) return { allowed: false, reason: `Every ${kind} layer needs a reviewed revert plan before any mask applies.` };\n if (kind === \"emulatedevice\") {\n const preset = devicepresetof(options.device);\n if (!preset) return { allowed: false, reason: \"The device layer needs a reviewed preset with a name, positive integer width and height and a positive pixel ratio.\" };\n if (options.reload !== undefined && typeof options.reload !== \"boolean\") return { allowed: false, reason: \"The reviewed reload flag must be a boolean; the page reloads only when the reviewed plan asks.\" };\n return { allowed: true };\n }\n if (kind === \"emulatenetwork\") {\n const preset = networkpresetof(options.network);\n if (!preset) return { allowed: false, reason: \"The network layer needs a reviewed preset with a name and zero or positive latency, download and upload bounds.\" };\n if (options.window !== undefined && (typeof options.window !== \"number\" || !Number.isFinite(options.window) || options.window < 0)) return { allowed: false, reason: \"The reviewed offline window must be zero or a positive number of milliseconds with no code ceiling.\" };\n return { allowed: true };\n }\n if (kind === \"emulatelocate\") {\n const preset = locationpresetof(options.location);\n if (!preset) return { allowed: false, reason: \"The location layer needs a reviewed preset with a name, a latitude inside -90 and 90, a longitude inside -180 and 180 and a zero or positive accuracy radius.\" };\n if (!locationrangevalid(preset.latitude, preset.longitude)) return { allowed: false, reason: \"The reviewed latitude must stay inside -90 and 90 degrees and the longitude inside -180 and 180 degrees.\" };\n return { allowed: true };\n }\n if (kind === \"setuseragent\") {\n const preset = agentpresetof(options.agent);\n if (!preset) return { allowed: false, reason: \"The agent layer needs a reviewed preset with a user agent string of the reviewed grammar, a platform and a non-empty brand list.\" };\n if (!agentgrammarvalid(preset.useragent)) return { allowed: false, reason: \"The reviewed user agent string must use the reviewed grammar of tokens, separators and version marks without line breaks.\" };\n return { allowed: true };\n }\n if (kind === \"overridepermission\") {\n const grant = permissiongrantof(options.permission);\n if (!grant) return { allowed: false, reason: `The permission override needs a reviewed name of the browser permission set (${browserpermissions.join(\", \")}) and a state of ${permissionstates.join(\", \")}.` };\n void permissiongrade(grant.name);\n return { allowed: true };\n }\n if (kind === \"blackboxscripts\") {\n const rules = Array.isArray(options.rules) ? options.rules.flatMap(rule => { const parsed = blackboxruleof(rule); return parsed !== undefined ? [parsed] : []; }) : [];\n if (rules.length === 0) return { allowed: false, reason: \"The blackbox layer needs a reviewed non-empty rule list where every pattern names its origin explicitly and carries a trace scope.\" };\n return { allowed: true };\n }\n return { allowed: true };\n}\n\n/** Validates one permission override name against the reviewed browser permission set. */\nexport function permissionnamevalid(name: string): policyevaluation {\n if (!browserpermissions.includes(name)) return { allowed: false, reason: `The permission ${name} stays outside the reviewed browser permission set: ${browserpermissions.join(\", \")}.` };\n return { allowed: true };\n}\n\n/** Validates the reviewed session parameter grammar of the 1.1.49 memory family: snapshot plans with the scope, the section toggles of the reviewed grammar and the optional auto interval whose period, maximum snapshot count and expiry stay user choices with no code ceiling, restore plans with their tab, form and capture policies behind the explicit restore review, session filings with unique reviewed names and folders, diffs of two saved records, searches with the term grammar and the field set, exports behind the explicit export review and imports of the known file format behind the full record review. */\nfunction validatesessiongrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"persiststate\") {\n if (options.resume !== undefined && typeof options.resume !== \"boolean\") return { allowed: false, reason: \"The reviewed resume flag must be a boolean.\" };\n return { allowed: true };\n }\n if (kind === \"capturesession\") {\n const plan = snapshotplanof(options.snapshot);\n if (!plan) return { allowed: false, reason: \"The session capture needs a reviewed snapshot plan with its scope, a non-empty section list of the reviewed grammar (tabs, scroll, forms, storage, cookies) and the capture link flag.\" };\n if (plan.auto !== undefined) {\n const interval = autointervalof((options.snapshot as Record<string, unknown>).auto);\n if (interval === undefined) return { allowed: false, reason: \"The reviewed auto snapshot interval needs a positive period, a positive maximum snapshot count and a zero or positive expiry window with no code ceiling.\" };\n }\n return { allowed: true };\n }\n if (kind === \"restoresession\") {\n if (typeof options.sessionid !== \"string\" || !options.sessionid.trim()) return { allowed: false, reason: \"The session restore needs the reviewed session id of the saved record.\" };\n if (restoreplanof(options.restore) === undefined) return { allowed: false, reason: \"The session restore needs a reviewed restore plan with its tab, form and capture policies.\" };\n if (options.reviewed !== true) return { allowed: false, reason: \"Every session restore needs the explicit restore review with its tabs, form state and captures listed before it reopens anything.\" };\n return { allowed: true };\n }\n if (kind === \"namedsessions\") {\n if (typeof options.sessionid !== \"string\" || !options.sessionid.trim()) return { allowed: false, reason: \"The session filing needs the reviewed session id of the saved record.\" };\n if (typeof options.name !== \"string\" || !options.name.trim()) return { allowed: false, reason: \"The session filing needs a reviewed non-empty session name.\" };\n if (options.folder !== undefined && (typeof options.folder !== \"string\" || !options.folder.trim())) return { allowed: false, reason: \"The reviewed folder name must be a non-empty string.\" };\n if (options.tags !== undefined && (!Array.isArray(options.tags) || !options.tags.every(tag => typeof tag === \"string\" && tag.trim()))) return { allowed: false, reason: \"The reviewed tag list must be a list of non-empty strings.\" };\n return { allowed: true };\n }\n if (kind === \"diffsessions\") {\n if (typeof options.left !== \"string\" || !options.left.trim() || typeof options.right !== \"string\" || !options.right.trim()) return { allowed: false, reason: \"The session diff needs the reviewed ids of both saved sessions.\" };\n return { allowed: true };\n }\n if (kind === \"searchsessions\") {\n if (searchqueryof(options.query) === undefined) return { allowed: false, reason: \"The session search needs a reviewed query with a non-empty term list, fields of the reviewed grammar (urls, titles, names, text) and an optional time window.\" };\n return { allowed: true };\n }\n if (kind === \"exportsessions\") {\n if (options.reviewed !== true) return { allowed: false, reason: \"Session exports need the explicit export review before any session file leaves the device.\" };\n if (options.ids !== undefined && (!Array.isArray(options.ids) || options.ids.length === 0 || !options.ids.every(id => typeof id === \"string\" && id.trim()))) return { allowed: false, reason: \"The reviewed export id list must be a non-empty list of saved session ids.\" };\n return { allowed: true };\n }\n if (kind === \"importsessions\") {\n if (options.reviewed !== true) return { allowed: false, reason: \"Session imports need the explicit full record review before any record joins the library.\" };\n if (importsessionfile(options.file) === undefined) return { allowed: false, reason: \"The session import needs a reviewed file of the known format version with an intact checksum.\" };\n return { allowed: true };\n }\n return { allowed: true };\n}\n\n/** Requires the explicit restore review flag and the reviewed restore plan before any session restore reopens a tab; the review lists every tab, form state and capture first. */\nexport function restorereviewgranted(step: toolstep): policyevaluation {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n if (restoreplanof(options.restore) === undefined) return { allowed: false, reason: \"Every session restore needs a reviewed restore plan with its tab, form and capture policies.\" };\n if (options.reviewed !== true) return { allowed: false, reason: \"The session restore needs the explicit restore review of its tabs, form state and captures before it reopens anything.\" };\n return { allowed: true };\n}\n\n/** The session consent gate of every session memory step: a live session, an approved plan and the restore review of every restore; crash restore prompts stay inside the same consent model. */\nexport function sessionrestoregate(input: { session: agentsession | undefined; plan: agentplan | undefined; step: toolstep; tabid: number; origin: string; now: number }): policyevaluation {\n const gate = sessiongate({ session: input.session, tabid: input.tabid, origin: input.origin, now: input.now, action: \"run the session memory step\" });\n if (!gate.allowed) return gate;\n if (!input.plan || input.plan.state !== \"approved\") return { allowed: false, reason: \"Session memory steps need an approved plan before they run.\" };\n if (input.step.kind === \"restoresession\") return restorereviewgranted(input.step);\n return { allowed: true };\n}\n\n/** Returns the origins a restore reopens outside the grants so the restore skips and reports them; captures and cookies restore only with their origin grants. */\nexport function restoreoriginsgranted(urls: string[], grants: string[]): { allowed: boolean; skippedorigins: string[] } {\n const covered = new Set(grants);\n const skippedorigins: string[] = [];\n for (const url of urls) {\n let origin = \"\";\n try { origin = new URL(url).origin; } catch { origin = \"\"; }\n if (!origin || !covered.has(origin)) skippedorigins.push(origin || url);\n }\n return { allowed: skippedorigins.length === 0, skippedorigins: [...new Set(skippedorigins)] };\n}\n\n/** Requires session names to stay unique inside the library so a filing never shadows another saved session. */\nexport function sessionnameunique(name: string, records: Array<{ id: string; name: string }>, recordid?: string): policyevaluation {\n if (records.some(record => record.name === name && record.id !== recordid)) return { allowed: false, reason: `The session name ${name} already exists in the library; review a unique name.` };\n return { allowed: true };\n}\n\n/** Requires folder names to stay unique inside the folder tree so one folder never shadows another. */\nexport function sessionfolderunique(name: string, folders: Array<{ name: string }>): policyevaluation {\n if (folders.some(folder => folder.name === name)) return { allowed: false, reason: `The folder name ${name} already exists in the library; review a unique folder name.` };\n return { allowed: true };\n}\n\n/** Exposes the user configured retention window for saved session sections; an absent window keeps every section and no code ceiling applies. */\nexport function snapshotretentionwindow(settings: runsettings | undefined): number | undefined {\n return settings?.sessionretention;\n}\n\n/** Validates the reviewed workflow parameter grammar of the 1.1.50 and 1.1.51 families: composition with the expanded block list so no step stays hidden, shareable step templates, workflow runs behind the explicit run review, dry runs of the known workflow, jittered delays and element waits of user configured bounds with no code ceiling, expressions whose operators match the operand kinds and result kinds, regex rules of bounded backtracking shapes applied to reviewed text, and the control flow payloads of conditionals, branching, loops with user configured safety bounds, foreach selectors, parallel branches with join policies and try catch with retry and timeout policies whose child kinds all stay inside the reviewed vocabulary. */\nfunction validateworkflowgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"composeworkflow\") {\n const payload = options.workflow;\n if (!payload || typeof payload !== \"object\" || Array.isArray(payload)) return { allowed: false, reason: \"The workflow composition needs the reviewed workflow payload with its name, version, origins, steps and blocks.\" };\n const candidate = payload as Record<string, unknown>;\n if (typeof candidate.name !== \"string\" || !candidate.name.trim()) return { allowed: false, reason: \"The workflow composition needs a reviewed non-empty name.\" };\n if (typeof candidate.version !== \"number\" || !Number.isInteger(candidate.version) || candidate.version < 1) return { allowed: false, reason: \"The workflow version must be a positive integer.\" };\n if (!Array.isArray(candidate.origins) || candidate.origins.length === 0 || !candidate.origins.every(origin => typeof origin === \"string\" && origin.startsWith(\"https://\"))) return { allowed: false, reason: \"The workflow needs at least one granted HTTPS origin so every step stays inside the grants.\" };\n if (!Array.isArray(candidate.steps) || candidate.steps.length === 0 || !candidate.steps.every(entry => workflowstepof(entry) !== undefined || (entry && typeof entry === \"object\" && typeof (entry as Record<string, unknown>).block === \"string\"))) return { allowed: false, reason: \"The workflow needs a non-empty reviewed step list of the workflow step grammar or block invocations.\" };\n const blocks = Array.isArray(candidate.blocks) ? candidate.blocks.flatMap(block => { const parsed = workflowblockof(block); return parsed !== undefined ? [parsed] : []; }) : [];\n if (Array.isArray(candidate.blocks) && blocks.length !== (candidate.blocks as unknown[]).length) return { allowed: false, reason: \"The reviewed block list must carry unique lowercase names, labels and valid child steps.\" };\n try {\n const record = composeworkflow({ name: candidate.name, version: candidate.version, origins: candidate.origins as string[], steps: (candidate.steps as Array<Record<string, unknown>>).map(entry => \"block\" in entry ? { block: entry.block as string, label: typeof entry.label === \"string\" ? entry.label : entry.block as string } : workflowstepof(entry) as workflowstep), blocks, now: 0, kindallowed: candidatekind => { try { actionrisk(candidatekind as actionkind); return true; } catch { return false; } }, riskof: candidatekind => actionrisk(candidatekind as actionkind) });\n const inputs = Array.isArray(candidate.inputs) ? candidate.inputs.flatMap(name => typeof name === \"string\" ? [name] : []) : undefined;\n const checked = validateworkflow(record, { kindallowed: workflowkind => { try { actionrisk(workflowkind as actionkind); return true; } catch { return false; } }, ...(inputs !== undefined ? { inputs } : {}) });\n if (!checked.allowed) return checked;\n } catch (error) {\n return { allowed: false, reason: error instanceof Error ? error.message : \"The workflow payload failed its composition validation.\" };\n }\n return { allowed: true };\n }\n if (kind === \"savetemplate\") {\n const payload = options.template && typeof options.template === \"object\" && !Array.isArray(options.template) ? options.template as Record<string, unknown> : {};\n const template = steptemplateof({ id: \"templatereview\", origin: \"https://example.com\", sharedat: 0, ...payload });\n if (!template) return { allowed: false, reason: \"The step template needs a reviewed name and a valid workflow step it shares across workflows.\" };\n return { allowed: true };\n }\n if (kind === \"runworkflow\") {\n if (typeof options.workflowid !== \"string\" || !options.workflowid.trim()) return { allowed: false, reason: \"The workflow run needs the reviewed id of the composed workflow.\" };\n if (options.reviewed !== true) return { allowed: false, reason: \"Every real workflow run needs the explicit run review with its expanded step list shown before the first step executes.\" };\n if (options.variables !== undefined && (!options.variables || typeof options.variables !== \"object\" || Array.isArray(options.variables) || !Object.values(options.variables).every(value => typeof value === \"string\" || typeof value === \"number\" || typeof value === \"boolean\"))) return { allowed: false, reason: \"The reviewed run variables must be an object of string, number or boolean values.\" };\n return { allowed: true };\n }\n if (kind === \"dryrun\") {\n if (typeof options.workflowid !== \"string\" || !options.workflowid.trim()) return { allowed: false, reason: \"The dry run needs the reviewed id of the composed workflow.\" };\n return { allowed: true };\n }\n if (kind === \"delay\") {\n const delay = options.delay;\n if (!delay || typeof delay !== \"object\" || Array.isArray(delay)) return { allowed: false, reason: \"The delay needs a reviewed base and jitter window in options.\" };\n const reviewed = delay as Record<string, unknown>;\n if (typeof reviewed.base !== \"number\" || !Number.isFinite(reviewed.base) || reviewed.base < 0) return { allowed: false, reason: \"The reviewed delay base must be zero or a positive number of milliseconds.\" };\n if (typeof reviewed.jitter !== \"number\" || !Number.isFinite(reviewed.jitter) || reviewed.jitter < 0) return { allowed: false, reason: \"The reviewed delay jitter window must be zero or a positive number of milliseconds with no code ceiling.\" };\n return { allowed: true };\n }\n if (kind === \"waitelement\") {\n const wait = options.wait;\n if (!wait || typeof wait !== \"object\" || Array.isArray(wait)) return { allowed: false, reason: \"The element wait needs a reviewed selector, timeout and poll interval in options.\" };\n const reviewed = wait as Record<string, unknown>;\n if (typeof reviewed.selector !== \"string\" || !reviewed.selector.trim()) return { allowed: false, reason: \"The element wait needs a reviewed non-empty selector.\" };\n if (typeof reviewed.timeout !== \"number\" || !Number.isFinite(reviewed.timeout) || reviewed.timeout < 0) return { allowed: false, reason: \"The reviewed element wait timeout must be zero or a positive number of milliseconds with no code ceiling.\" };\n if (typeof reviewed.poll !== \"number\" || !Number.isFinite(reviewed.poll) || reviewed.poll < 0) return { allowed: false, reason: \"The reviewed element wait poll interval must be zero or a positive number of milliseconds with no code ceiling.\" };\n return { allowed: true };\n }\n if (kind === \"compute\") {\n const expression = expressionof(options.expression);\n if (!expression) return { allowed: false, reason: `The expression step needs a reviewed expression with operands, an operator of the reviewed set (${expressionoperators.join(\", \")}) and a result variable of a reviewed kind.` };\n const operatorcheck = validatexpressionoperators(expression);\n if (!operatorcheck.allowed) return operatorcheck;\n return { allowed: true };\n }\n if (kind === \"extractvars\") {\n const rule = regexruleof(options.rule);\n if (!rule) return { allowed: false, reason: \"The variable extraction needs a reviewed regex rule with its pattern, flags and named capture groups.\" };\n const shapecheck = validateregexrule(rule.pattern);\n if (!shapecheck.allowed) return shapecheck;\n if (typeof options.text !== \"string\") return { allowed: false, reason: \"The variable extraction needs the reviewed text the regex rule applies to.\" };\n return { allowed: true };\n }\n if (kind === \"condition\") {\n const condition = conditionof(options.condition);\n if (!condition) return { allowed: false, reason: \"The condition step needs a reviewed boolean expression in its options.\" };\n const operatorcheck = validatexpressionoperators(condition.expression);\n if (!operatorcheck.allowed) return operatorcheck;\n return { allowed: true };\n }\n if (kind === \"branch\") {\n const branch = branchof(options.branch);\n if (!branch) return { allowed: false, reason: \"The branch step needs reviewed unique paths with boolean match expressions and an else path in its options so every branch terminates.\" };\n for (const path of [...branch.paths, branch.else]) {\n if (path.when === undefined) continue;\n const operatorcheck = validatexpressionoperators(path.when);\n if (!operatorcheck.allowed) return operatorcheck;\n }\n return controlchildkinds(step);\n }\n if (kind === \"loop\") {\n const loop = loopof(options.loop);\n if (!loop) return { allowed: false, reason: \"The loop step needs a reviewed list variable, distinct item and index variables, an optional positive safety bound and a non-empty body in its options; an absent bound keeps the documented default.\" };\n return controlchildkinds(step);\n }\n if (kind === \"repeatuntil\") {\n const repeat = repeatuntilof(options.repeatuntil);\n if (!repeat) return { allowed: false, reason: \"The repeat until step needs a reviewed convergence expression, an optional positive safety bound and a non-empty body in its options.\" };\n const operatorcheck = validatexpressionoperators(repeat.until);\n if (!operatorcheck.allowed) return operatorcheck;\n return controlchildkinds(step);\n }\n if (kind === \"whileloop\") {\n const condition = whileof(options.while);\n if (!condition) return { allowed: false, reason: \"The while step needs a reviewed condition, a mandatory positive safety bound and a non-empty body in its options; a while loop without a safety bound is refused.\" };\n const operatorcheck = validatexpressionoperators(condition.while);\n if (!operatorcheck.allowed) return operatorcheck;\n return controlchildkinds(step);\n }\n if (kind === \"foreach\") {\n const foreach = foreachof(options.foreach);\n if (!foreach) return { allowed: false, reason: \"The foreach step needs a reviewed non-empty selector, distinct item and index variables and a non-empty body in its options.\" };\n return controlchildkinds(step);\n }\n if (kind === \"parallel\") {\n const parallel = parallelof(options.parallel);\n if (!parallel) return { allowed: false, reason: \"The parallel step needs uniquely identified branches with bodies and a join policy of the first, last or fail strategy with cancel or continue on branch failure in its options.\" };\n return controlchildkinds(step);\n }\n if (kind === \"trycatch\") {\n const fragile = tryof(options.try);\n if (!fragile) return { allowed: false, reason: \"The try step needs a fragile body, a catch handler and optional retry and timeout policies in its options: attempts stay user configured with no code ceiling, backoff is fixed or exponential and budgets are positive.\" };\n return controlchildkinds(step);\n }\n return { allowed: true };\n}\n\n/** Checks every child step of a control payload against the reviewed action vocabulary so no control construct hides an unreviewed kind behind its body. */\nfunction controlchildkinds(step: toolstep): policyevaluation {\n const children = controlsteps({ id: step.id, kind: step.kind, label: step.summary, ...(step.options !== undefined ? { options: step.options } : {}) });\n for (const child of children) {\n try { actionrisk(child.kind); } catch { return { allowed: false, reason: `The ${child.kind} step inside the control payload of the ${step.kind} step is not a reviewed action kind.` }; }\n }\n return { allowed: true };\n}\n\n/** Rejects unbounded backtracking shapes of reviewed regex patterns: a quantified group whose body itself ends with an unbounded quantifier can explode on adversarial text, so the shape is refused while bounded repetitions stay user choices. */\nexport function validateregexrule(pattern: string): policyevaluation {\n try { new RegExp(pattern); } catch { return { allowed: false, reason: \"The reviewed regex pattern does not compile.\" }; }\n const nestedquantifier = /\\((?:[^()\\\\]|\\\\.)*[+*}]\\)[+*{]/.test(pattern) || /\\(\\)[+*{]/.test(pattern);\n if (nestedquantifier) return { allowed: false, reason: \"The reviewed regex pattern nests an unbounded quantifier inside a quantified group and is refused because adversarial text could explode the backtracking.\" };\n const unboundedrepeat = /\\{\\d+,\\}/.test(pattern);\n if (unboundedrepeat && /\\([^)]*\\{\\d+,\\}[^)]*\\)[+*{]/.test(pattern)) return { allowed: false, reason: \"The reviewed regex pattern repeats an unbounded group and is refused because adversarial text could explode the backtracking.\" };\n return { allowed: true };\n}\n\n/** Validates the reviewed expression operators against the operand kinds and the result kind: arithmetic needs numbers and returns numbers, logic needs booleans and returns booleans, comparison needs numbers and returns booleans, text operators return strings or booleans and length returns a number. */\nfunction validatexpressionoperators(expression: import(\"./types.js\").expressiontype): policyevaluation {\n const numeric = new Set([\"add\", \"subtract\", \"multiply\", \"divide\", \"modulo\"]);\n const logic = new Set([\"and\", \"or\", \"not\"]);\n const comparison = new Set([\"less\", \"greater\", \"lessequal\", \"greaterequal\"]);\n const text = new Set([\"concat\", \"contains\"]);\n const operator = expression.operator;\n if (numeric.has(operator)) {\n for (const operand of [expression.left, expression.right]) {\n if (operand === undefined) continue;\n if (operand.literal !== undefined && typeof operand.literal === \"boolean\") return { allowed: false, reason: `The ${operator} operator needs numeric operands; boolean literals are refused.` };\n }\n if (expression.resultkind !== \"number\" && expression.resultkind !== \"string\") return { allowed: false, reason: `The ${operator} operator needs a number result kind.` };\n }\n if (logic.has(operator)) {\n for (const operand of [expression.left, expression.right]) {\n if (operand === undefined) continue;\n if (operand.literal !== undefined && typeof operand.literal !== \"boolean\") return { allowed: false, reason: `The ${operator} operator needs boolean operands; non boolean literals are refused.` };\n }\n if (expression.resultkind !== \"boolean\") return { allowed: false, reason: `The ${operator} operator needs a boolean result kind.` };\n if (operator === \"not\" && expression.right !== undefined) return { allowed: false, reason: \"The not operator takes one operand only.\" };\n }\n if (comparison.has(operator) && expression.resultkind !== \"boolean\") return { allowed: false, reason: `The ${operator} operator needs a boolean result kind.` };\n if (text.has(operator) && expression.resultkind !== \"boolean\" && expression.resultkind !== \"string\") return { allowed: false, reason: `The ${operator} operator needs a string or boolean result kind.` };\n if (operator === \"contains\" && expression.resultkind !== \"boolean\") return { allowed: false, reason: \"The contains operator needs a boolean result kind.\" };\n if (operator === \"length\") {\n if (expression.right !== undefined) return { allowed: false, reason: \"The length operator takes one operand only.\" };\n if (expression.resultkind !== \"number\") return { allowed: false, reason: \"The length operator needs a number result kind.\" };\n }\n if ((operator === \"equal\" || operator === \"notequal\") && !new Set([\"boolean\", \"string\", \"number\"]).has(expression.resultkind)) return { allowed: false, reason: \"The equality operator needs a primitive result kind.\" };\n return { allowed: true };\n}\n\n/** The workflow consent gate: a live session, an approved plan and the explicit run review of every real run; dry runs stay read only inside the same session and plan gates. */\nexport function workflowgate(input: { session: agentsession | undefined; plan: agentplan | undefined; step: toolstep; tabid: number; origin: string; now: number }): policyevaluation {\n const gate = sessiongate({ session: input.session, tabid: input.tabid, origin: input.origin, now: input.now, action: \"run the workflow step\" });\n if (!gate.allowed) return gate;\n if (!input.plan || input.plan.state !== \"approved\") return { allowed: false, reason: \"Workflow steps need the approved plan review before they run.\" };\n if (input.step.kind === \"runworkflow\") {\n let runoptions: Record<string, unknown> = {};\n try { runoptions = parseoptions(input.step); } catch { runoptions = {}; }\n if (runoptions.reviewed !== true) return { allowed: false, reason: \"Every real workflow run needs the explicit run review with its expanded step list shown before the first step executes.\" };\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed trigger parameter grammar of the 1.1.52 family: every kind arms exactly one rule behind the explicit arm review, the workflow reference must name a composed workflow, the match payloads follow their family grammar \u2014 visit origins and url list entries must be HTTPS urls, url patterns must parse as HTTPS globs, cron expressions must parse as five field schedules with named weekdays and months and a resolvable timezone, interval periods stay positive with zero or positive jitter, webhook secrets must clear the documented entropy floor with a non-empty payload schema, event names must come from the observed event catalog and context menu titles stay non-empty \u2014 while cooldown windows stay user configured positive values with the documented default of the webhook and event families winning only when the review configures none. */\nfunction validatetriggergrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const family = triggerfamilyof(step.kind);\n if (family === undefined) return { allowed: false, reason: \"The trigger step is not a reviewed trigger kind.\" };\n if (typeof options.workflowid !== \"string\" || !options.workflowid.trim()) return { allowed: false, reason: \"Every trigger rule needs the reviewed id of the composed workflow it launches.\" };\n if (options.reviewed !== true) return { allowed: false, reason: \"Every trigger rule needs the explicit arm review with its match fields and bound workflow shown before it arms.\" };\n if (options.label !== undefined && (typeof options.label !== \"string\" || !options.label.trim())) return { allowed: false, reason: \"The reviewed trigger label must be a non-empty string.\" };\n if (options.cooldown !== undefined && (typeof options.cooldown !== \"number\" || !Number.isFinite(options.cooldown) || options.cooldown <= 0)) return { allowed: false, reason: \"The reviewed cooldown window must be a positive number of milliseconds with no code ceiling; the webhook and event families keep the documented default when the review configures none.\" };\n const payload = options.rule;\n if (!payload || typeof payload !== \"object\" || Array.isArray(payload)) return { allowed: false, reason: `The ${step.kind} step needs its reviewed rule payload in options.` };\n if (triggerpayloadof(family, payload) === undefined) {\n if (family === \"visit\") return { allowed: false, reason: \"The visit rule needs a non-empty reviewed list of HTTPS origins it fires on.\" };\n if (family === \"url\") return { allowed: false, reason: \"The url rule needs a reviewed HTTPS glob url pattern; `*` spans one path segment and `**` spans across segments.\" };\n if (family === \"menu\") return { allowed: false, reason: \"The menu rule needs a reviewed non-empty context menu entry title.\" };\n if (family === \"key\") return { allowed: false, reason: \"The keyboard shortcut rule needs a reviewed lowercase command name and an optional suggested key binding.\" };\n if (family === \"cron\") return { allowed: false, reason: \"The cron rule needs a reviewed five field cron expression of minutes, hours, days, months and weekdays with named weekdays and months and an optional resolvable timezone; unparseable schedules are refused.\" };\n if (family === \"interval\") return { allowed: false, reason: \"The interval rule needs a reviewed positive period in milliseconds with an optional zero or positive jitter window.\" };\n if (family === \"urllist\") return { allowed: false, reason: \"The url list rule needs a reviewed non-empty list of HTTPS urls its workflow runs across.\" };\n if (family === \"webhook\") return { allowed: false, reason: `The webhook rule needs a reviewed shared secret of at least twenty four characters mixing letters and digits and a non-empty payload schema of named string, number or boolean fields.` };\n if (family === \"event\") return { allowed: false, reason: `The page event rule needs a reviewed non-empty list of event names of the observed event catalog: ${triggereventcatalog.join(\", \")}.` };\n return { allowed: false, reason: \"The trigger rule payload does not follow its family grammar.\" };\n }\n if (family === \"cron\") {\n const candidate = payload as Record<string, unknown>;\n if (typeof candidate.cron === \"string\" && cronparse(candidate.cron) === undefined) return { allowed: false, reason: \"The cron expression does not parse as a five field schedule and is refused.\" };\n }\n if (family === \"webhook\") {\n const candidate = payload as Record<string, unknown>;\n if (typeof candidate.secret === \"string\" && !webhooksecretok(candidate.secret)) return { allowed: false, reason: \"The webhook shared secret must hold at least twenty four characters mixing letters and digits; the entropy floor is a floor, never a cap.\" };\n }\n const armed = armrule({ family, workflowid: options.workflowid, ...(typeof options.label === \"string\" && options.label.trim() ? { label: options.label } : {}), payload, ...(typeof options.cooldown === \"number\" ? { cooldown: options.cooldown } : {}), now: 0 });\n if (armed === undefined) return { allowed: false, reason: \"The trigger rule payload does not arm as a reviewed rule.\" };\n return { allowed: true };\n}\n\n/** The trigger consent gate: a live session, an approved plan and the explicit arm review of every rule; automatic launchers never arm outside the consent gates. */\nexport function triggergate(input: { session: agentsession | undefined; plan: agentplan | undefined; step: toolstep; tabid: number; origin: string; now: number }): policyevaluation {\n const gate = sessiongate({ session: input.session, tabid: input.tabid, origin: input.origin, now: input.now, action: \"arm the trigger rule\" });\n if (!gate.allowed) return gate;\n if (!input.plan || input.plan.state !== \"approved\") return { allowed: false, reason: \"Trigger rules need the approved plan review before they arm.\" };\n let triggeroptions: Record<string, unknown> = {};\n try { triggeroptions = parseoptions(input.step); } catch { triggeroptions = {}; }\n if (triggeroptions.reviewed !== true) return { allowed: false, reason: \"Every trigger rule needs the explicit arm review with its match fields and bound workflow shown before it arms.\" };\n return { allowed: true };\n}\n\n/** Returns the match origins of one reviewed trigger rule so callers can keep every rule inside the workflow grant list; triggers on origins outside the grants are refused. */\nexport function triggerorigins(step: toolstep): string[] {\n let triggeroptions: Record<string, unknown> = {};\n try { triggeroptions = parseoptions(step); } catch { return []; }\n const family = triggerfamilyof(step.kind);\n if (family === undefined) return [];\n const armed = armrule({ family, workflowid: typeof triggeroptions.workflowid === \"string\" ? triggeroptions.workflowid : \"\", payload: triggeroptions.rule, ...(typeof triggeroptions.cooldown === \"number\" ? { cooldown: triggeroptions.cooldown } : {}), now: 0 });\n if (armed === undefined) return [];\n const origins: string[] = [];\n for (const origin of armed.origins ?? []) origins.push(origin);\n if (armed.pattern !== undefined) { try { origins.push(new URL(armed.pattern).origin); } catch { /* the pattern grammar already refused unparseable patterns */ } }\n for (const url of armed.urls ?? []) { try { origins.push(new URL(url).origin); } catch { /* the url list grammar already refused unparseable urls */ } }\n return [...new Set(origins)];\n}\n\n/** Returns the read only projection of one workflow step for dry runs: read class steps report their would be outcome while interaction and mutation steps carry no projection and the dry run refuses them; a control step projects only when every child step of its payload grades read. */\nexport function dryrunprojection(step: workflowstep): string | undefined {\n if (iscontrolflowkind(step.kind)) {\n for (const child of controlsteps(step)) {\n const childrisk = resolvedrisk({ id: child.id, kind: child.kind, summary: child.label, risk: \"read\", ...(child.target !== undefined ? { target: child.target } : {}), ...(child.value !== undefined ? { value: child.value } : {}), ...(child.options !== undefined ? { options: child.options } : {}) });\n if (childrisk !== \"read\") return undefined;\n }\n if (step.kind === \"condition\") return \"The condition step would evaluate its reviewed expression over the extracted values with no page side effect.\";\n if (step.kind === \"branch\") return \"The branch step would choose one reviewed path by page state and only the chosen path would run.\";\n if (step.kind === \"loop\") return \"The loop step would iterate its reviewed list binding the item and index variables per iteration inside the safety bound.\";\n if (step.kind === \"repeatuntil\") return \"The repeat until step would rerun its body until the convergence expression holds inside the safety bound.\";\n if (step.kind === \"whileloop\") return \"The while step would loop while its condition holds inside the reviewed safety bound.\";\n if (step.kind === \"foreach\") return \"The foreach step would iterate the elements of its reviewed selector binding the item and index variables per iteration.\";\n if (step.kind === \"parallel\") return \"The parallel step would run its branches concurrently and join their outcomes under the reviewed strategy.\";\n return \"The try step would run its fragile body and only the catch handler on failure.\";\n }\n const risk = resolvedrisk({ id: step.id, kind: step.kind, summary: step.label, risk: \"read\", ...(step.target !== undefined ? { target: step.target } : {}), ...(step.value !== undefined ? { value: step.value } : {}), ...(step.options !== undefined ? { options: step.options } : {}) });\n if (risk !== \"read\") return undefined;\n if (step.kind === \"delay\") return `The delay step would sleep its reviewed base inside the jitter window.`;\n if (step.kind === \"waitelement\") return `The element wait step would poll ${step.target ?? \"the reviewed selector\"} until appearance or the reviewed timeout.`;\n if (step.kind === \"compute\") return `The compute step would evaluate its reviewed expression into the result variable.`;\n if (step.kind === \"extractvars\") return `The variable extraction step would apply its reviewed regex rule and store the named captures.`;\n return `The ${step.kind} step would run read only and mutate nothing.`;\n}\n\n/** Validates one reviewed permission state of an override. */\nexport function permissionstatevalid(state: string): policyevaluation {\n if (!permissionstates.includes(state as permissionstate)) return { allowed: false, reason: `The reviewed permission state must be one of ${permissionstates.join(\", \")}.` };\n return { allowed: true };\n}\n\n/** Validates the reviewed devtools parameter grammar of the 1.1.46 family: enabled domains bounded by the reviewed domain grammar, the required teardown plan of every attach, raw commands of the Domain.method form, domain event rules with match filters inside the reviewed watch window, breakpoints with conditions of the reviewed expression grammar, step modes, reviewed watch expressions, and script overrides with the explicit reviewed flag and a url pattern that names its origin. */\nfunction validatecdpgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"attachcdp\") {\n if (!Array.isArray(options.domains) || options.domains.length === 0 || !options.domains.every((domain): domain is string => typeof domain === \"string\" && cdpdomains.includes(domain))) return { allowed: false, reason: `The attach needs a non-empty enabled domain list of the reviewed domain grammar: ${cdpdomains.join(\", \")}.` };\n if (teardownplanof(options.teardown) === undefined) return { allowed: false, reason: \"Every attach needs a reviewed teardown plan with its revert steps and resume policy before approval.\" };\n if (options.allowlist !== undefined) {\n const allowlist = cdpallowlistof(options.allowlist);\n if (!allowlist || !allowlist.domains.every(domain => (options.domains as string[]).includes(domain))) return { allowed: false, reason: \"The reviewed method allowlist must stay inside the enabled domains of the attach.\" };\n }\n const budgetcheck = debugwaitbudgetallowed(typeof options.wait === \"number\" ? options.wait : undefined, undefined);\n if (!budgetcheck.allowed) return budgetcheck;\n return { allowed: true };\n }\n if (kind === \"detachcdp\") return { allowed: true };\n if (kind === \"cdpcmd\") {\n const command = options.command && typeof options.command === \"object\" && !Array.isArray(options.command) ? options.command as Record<string, unknown> : undefined;\n if (!command || typeof command.method !== \"string\" || methoddomain(command.method) === undefined) return { allowed: false, reason: \"The raw command needs a reviewed method of the Domain.method form.\" };\n if (command.params !== undefined && (typeof command.params !== \"object\" || Array.isArray(command.params))) return { allowed: false, reason: \"The raw command params must be a JSON object.\" };\n if (command.resultpath !== undefined && typeof command.resultpath !== \"string\") return { allowed: false, reason: \"The reviewed result path must be a dotted path string.\" };\n return { allowed: true };\n }\n if (kind === \"watchcdp\") {\n if (!Array.isArray(options.events) || options.events.length === 0 || !options.events.every(rule => cdpeventruleof(rule) !== undefined)) return { allowed: false, reason: \"The event watch needs a non-empty reviewed list of domain event rules of the reviewed domain grammar.\" };\n let watchwindow: number | undefined;\n if (options.watch !== undefined) {\n const watch = options.watch;\n if (!watch || typeof watch !== \"object\" || Array.isArray(watch)) return { allowed: false, reason: \"The reviewed event watch window must be an object.\" };\n const reviewed = watch as Record<string, unknown>;\n if (reviewed.window !== undefined) {\n if (typeof reviewed.window !== \"number\" || !Number.isFinite(reviewed.window) || reviewed.window < 0) return { allowed: false, reason: \"The reviewed event watch window must be zero or a positive number of milliseconds.\" };\n watchwindow = reviewed.window;\n }\n }\n if (watchwindow === undefined) return { allowed: false, reason: \"The event watch needs a reviewed lifetime window before any domain event is observed.\" };\n const budgetcheck = debugwaitbudgetallowed(watchwindow, typeof options.wait === \"number\" ? options.wait : undefined);\n if (!budgetcheck.allowed) return budgetcheck;\n return { allowed: true };\n }\n if (kind === \"setbreakpoint\") {\n const breakpoint = breakpointinputof(options.breakpoint);\n if (!breakpoint) return { allowed: false, reason: \"The breakpoint needs a reviewed script url and a zero based line.\" };\n if (!ishttpsurl(breakpoint.url)) return { allowed: false, reason: \"The breakpoint script url must be a reviewed HTTPS url.\" };\n if (breakpoint.condition !== undefined) {\n const conditioncheck = validatebreakpointcondition(breakpoint.condition);\n if (!conditioncheck.allowed) return conditioncheck;\n }\n return { allowed: true };\n }\n if (kind === \"stepcode\") {\n if (stepmodeof(options.mode) === undefined) return { allowed: false, reason: \"The step code mode must be one of stepover, stepinto, stepout or resume.\" };\n return { allowed: true };\n }\n if (kind === \"watchexpr\") {\n if (watchexpressionof(options.expression) === undefined) return { allowed: false, reason: \"The watch expression needs the reviewed expression text.\" };\n if (options.reviewed !== true) return { allowed: false, reason: \"Watch expressions must be reviewed before evaluation; set the explicit reviewed flag on the step.\" };\n return { allowed: true };\n }\n if (kind === \"overridescript\") {\n const override = overrideinputof(options.override);\n if (!override) return { allowed: false, reason: \"The script override needs a reviewed url pattern and its full fixture source.\" };\n if (patternorigin(override.urlpattern) === undefined) return { allowed: false, reason: \"Script overrides without a named https origin pattern are refused.\" };\n if (options.reviewed !== true) return { allowed: false, reason: \"The full fixture source must be reviewed before the script override runs; set the explicit reviewed flag on the step.\" };\n return { allowed: true };\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed profiling parameter grammar of the 1.1.47 family: flow specs of the reviewed metric set inside a reviewed watch window, heap snapshots with the user chosen interval only, growth tracking with the reviewed slope, cpu profiles bounded by the reviewed wait budget, layout shift watches with the user chosen window only, trace records bounded by the reviewed category list and byte ceiling, trace annotations that carry step ids, offline replays of stored traces and source map capture scripts of explicit https urls. */\nfunction validateprofilegrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"measureflow\") {\n if (flowspecof(options.flow) === undefined) return { allowed: false, reason: `The flow measurement needs a reviewed flow spec with its mark prefix, step window and metric list of the reviewed metric set: navigation, paint, lcp, fid, interaction, blocking.` };\n const watch = options.watch && typeof options.watch === \"object\" && !Array.isArray(options.watch) ? options.watch as Record<string, unknown> : {};\n if (typeof watch.window !== \"number\" || !Number.isFinite(watch.window) || watch.window < 0) return { allowed: false, reason: \"The flow measurement needs a reviewed watch window of zero or more milliseconds.\" };\n const budgetcheck = debugwaitbudgetallowed(watch.window, typeof options.wait === \"number\" ? options.wait : undefined);\n if (!budgetcheck.allowed) return budgetcheck;\n return { allowed: true };\n }\n if (kind === \"heapshot\") {\n const heap = options.heap && typeof options.heap === \"object\" && !Array.isArray(options.heap) ? options.heap as Record<string, unknown> : {};\n if (heap.interval !== undefined && (typeof heap.interval !== \"number\" || !Number.isFinite(heap.interval) || heap.interval < 0)) return { allowed: false, reason: \"The reviewed heap snapshot interval must be zero or a positive number of milliseconds and stays a user choice with no code ceiling.\" };\n return { allowed: true };\n }\n if (kind === \"trackmemory\") {\n const growth = options.growth && typeof options.growth === \"object\" && !Array.isArray(options.growth) ? options.growth as Record<string, unknown> : undefined;\n if (!growth || typeof growth.slope !== \"number\" || !Number.isFinite(growth.slope) || growth.slope < 0) return { allowed: false, reason: \"Memory growth tracking needs the reviewed slope in bytes per millisecond before any sample is flagged.\" };\n if (growth.interval !== undefined && (typeof growth.interval !== \"number\" || !Number.isFinite(growth.interval) || growth.interval < 0)) return { allowed: false, reason: \"The reviewed sampling interval must be zero or a positive number of milliseconds and stays a user choice with no code ceiling.\" };\n return { allowed: true };\n }\n if (kind === \"profilecpu\") {\n const profile = options.profile && typeof options.profile === \"object\" && !Array.isArray(options.profile) ? options.profile as Record<string, unknown> : undefined;\n if (!profile || typeof profile.duration !== \"number\" || !Number.isFinite(profile.duration) || profile.duration < 0) return { allowed: false, reason: \"The cpu profile needs a reviewed duration of zero or more milliseconds.\" };\n const budgetcheck = debugwaitbudgetallowed(profile.duration, typeof options.wait === \"number\" ? options.wait : undefined);\n if (!budgetcheck.allowed) return budgetcheck;\n return { allowed: true };\n }\n if (kind === \"watchshifts\") {\n const watch = options.watch && typeof options.watch === \"object\" && !Array.isArray(options.watch) ? options.watch as Record<string, unknown> : {};\n if (typeof watch.window !== \"number\" || !Number.isFinite(watch.window) || watch.window < 0) return { allowed: false, reason: \"The layout shift watch needs a reviewed observation window of zero or more milliseconds; the window stays a user choice with no code ceiling.\" };\n if (options.threshold !== undefined && (typeof options.threshold !== \"number\" || !Number.isFinite(options.threshold) || options.threshold < 0)) return { allowed: false, reason: \"The reviewed shift score threshold must be zero or a positive number.\" };\n const budgetcheck = debugwaitbudgetallowed(watch.window, typeof options.wait === \"number\" ? options.wait : undefined);\n if (!budgetcheck.allowed) return budgetcheck;\n return { allowed: true };\n }\n if (kind === \"traceload\") {\n const trace = options.trace && typeof options.trace === \"object\" && !Array.isArray(options.trace) ? options.trace as Record<string, unknown> : undefined;\n if (!trace || !Array.isArray(trace.categories) || trace.categories.length === 0 || !trace.categories.every((category): category is string => typeof category === \"string\" && tracecategories.includes(category))) return { allowed: false, reason: `The trace record needs a non-empty reviewed category list of the reviewed category grammar: ${tracecategories.join(\", \")}.` };\n if (typeof trace.window !== \"number\" || !Number.isFinite(trace.window) || trace.window < 0) return { allowed: false, reason: \"The trace record needs a reviewed window of zero or more milliseconds and stops at the reviewed window end.\" };\n if (trace.exporttarget !== undefined && trace.exporttarget !== \"memory\" && trace.exporttarget !== \"download\") return { allowed: false, reason: \"The trace export target must be memory or download.\" };\n const budgetcheck = debugwaitbudgetallowed(trace.window, typeof options.wait === \"number\" ? options.wait : undefined);\n if (!budgetcheck.allowed) return budgetcheck;\n return { allowed: true };\n }\n if (kind === \"annotatetrace\" || kind === \"replaytrace\") {\n const trace = options.trace && typeof options.trace === \"object\" && !Array.isArray(options.trace) ? options.trace as Record<string, unknown> : undefined;\n if (!trace || typeof trace.traceid !== \"string\" || !trace.traceid.trim()) return { allowed: false, reason: `The ${kind === \"annotatetrace\" ? \"trace annotation\" : \"trace replay\"} needs the stored trace id of a recorded trace.` };\n if (kind === \"replaytrace\") return { allowed: true };\n if (!Array.isArray(options.annotations) || options.annotations.length === 0 || !options.annotations.every(annotation => annotationof(annotation) !== undefined)) return { allowed: false, reason: \"Exported traces carry their step annotations: every annotation needs a step id, a label and an optional offset from the trace start.\" };\n return { allowed: true };\n }\n if (kind === \"capturesourcemaps\") {\n if (options.scripts !== undefined) {\n if (!Array.isArray(options.scripts) || options.scripts.length === 0 || !options.scripts.every((url): url is string => typeof url === \"string\" && ishttpsurl(url))) return { allowed: false, reason: \"The source map capture scripts must be a non-empty list of reviewed HTTPS urls.\" };\n }\n return { allowed: true };\n }\n return { allowed: true };\n}\n\n/** Resolves the reviewed cdp allowlist of one plan: the enabled domains and method gates of its attachcdp step, the reviewable contract every later cdp kind of the plan must stay inside. */\nexport function planallowlist(steps: toolstep[]): cdpallowlist | undefined {\n const attach = steps.find(step => step.kind === \"attachcdp\");\n if (!attach) return undefined;\n let options: Record<string, unknown> = {};\n try { options = parseoptions(attach); } catch { options = {}; }\n const domains = Array.isArray(options.domains) ? options.domains.filter((domain): domain is string => typeof domain === \"string\" && cdpdomains.includes(domain)) : [];\n if (domains.length === 0) return undefined;\n const gated = cdpallowlistof(options.allowlist);\n return { domains, ...(gated?.methods !== undefined ? { methods: gated.methods } : {}) };\n}\n\n/** Resolves the reviewed outbound url of a network control step at review time: the form url of postform, the upload url of postfiles and the token url of authflow. */\nexport function controltarget(step: toolstep): string | undefined {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n for (const key of [\"form\", \"upload\"] as const) {\n const value = options[key];\n if (value && typeof value === \"object\" && !Array.isArray(value)) {\n const url = (value as Record<string, unknown>).url;\n if (typeof url === \"string\" && url.trim()) return url.trim();\n }\n }\n if (step.kind === \"authflow\") {\n const flow = oauthflowof(options.oauth);\n if (flow) return flow.tokenurl;\n }\n return undefined;\n}\n\n/** Resolves the reviewed channel url of a socket step at review time: the socket url of opensocket, the event stream url of subscribesse and the poll url of longpoll. */\nexport function sockettarget(step: toolstep): string | undefined {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n for (const key of [\"socket\", \"subscription\", \"poll\"] as const) {\n const value = options[key];\n if (value && typeof value === \"object\" && !Array.isArray(value)) {\n const url = (value as Record<string, unknown>).url;\n if (typeof url === \"string\" && url.trim()) return url.trim();\n }\n }\n return undefined;\n}\n\n/** Requires the active tab grant of the live session for every media kind: the session tab and origin must match and the origin grant must cover the active origin. */\nexport function mediagate(session: agentsession | undefined, tabid: number, origin: string, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the media capture.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot capture media.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot capture media.\" };\n if (session.tabid !== tabid) return { allowed: false, reason: `The media capture needs the active tab grant of session tab ${session.tabid} and refuses tab ${tabid}.` };\n if (!origingranted(session, origin)) return { allowed: false, reason: `The media capture of ${origin} needs the session origin grants first.` };\n return { allowed: true };\n}\n\n/** Requires an approved recording consent prompt before any recording of user activity starts; every start consumes its own prompt. */\nexport function recordingconsentgranted(step: toolstep): policyevaluation {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const consentref = options.consentref;\n if (typeof consentref !== \"string\" || !consentref.trim()) return { allowed: false, reason: \"A recording of user activity requires a reviewed consent ref in options before it starts.\" };\n return { allowed: true };\n}\n\n/** Exposes the recording duration window as a user configured choice in milliseconds; an absent window leaves the duration to the reviewed step options with no code ceiling. */\nexport function recordingwindow(settings: runsettings | undefined): number | undefined {\n const window = settings?.recordingwindow;\n return typeof window === \"number\" && Number.isFinite(window) && window > 0 ? window : undefined;\n}\n\n/** Keeps the reviewed lapse plan inside the reviewed wait budget: the whole lapse duration must fit the wait window with no code ceiling on either side. */\nexport function lapsebudgetallowed(interval: number, duration: number, wait: number | undefined): policyevaluation {\n if (!(interval > 0)) return { allowed: false, reason: \"The reviewed lapse interval must be a positive number of milliseconds.\" };\n if (!(duration > 0)) return { allowed: false, reason: \"The reviewed lapse duration must be a positive number of milliseconds.\" };\n if (wait !== undefined && !(wait >= 0)) return { allowed: false, reason: \"The reviewed wait budget must be zero or a positive number of milliseconds.\" };\n if (wait !== undefined && duration > wait) return { allowed: false, reason: `The lapse duration of ${duration} milliseconds exceeds the reviewed wait budget of ${wait} milliseconds; review a wider budget or a shorter duration.` };\n return { allowed: true };\n}\n\n/** Validates the reviewed media capture parameter grammar of the 1.1.41 family: pdf paper sizes, recording scopes and windows, image filters, lapse plans, conversion targets and thumbnail directives stay user choices with no code ceilings. */\nfunction validatemediagrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"capturepdf\") {\n const pdf = options.pdf;\n if (pdf !== undefined) {\n if (!pdf || typeof pdf !== \"object\" || Array.isArray(pdf)) return { allowed: false, reason: \"The reviewed pdf options must be an object in options.pdf.\" };\n const pdfoptions = pdf as Record<string, unknown>;\n if (pdfoptions.paperwidth !== undefined && (typeof pdfoptions.paperwidth !== \"number\" || !Number.isFinite(pdfoptions.paperwidth) || pdfoptions.paperwidth <= 0)) return { allowed: false, reason: \"The reviewed pdf paper width must be a positive number of inches with no code cap.\" };\n if (pdfoptions.paperheight !== undefined && (typeof pdfoptions.paperheight !== \"number\" || !Number.isFinite(pdfoptions.paperheight) || pdfoptions.paperheight <= 0)) return { allowed: false, reason: \"The reviewed pdf paper height must be a positive number of inches with no code cap.\" };\n if (pdfoptions.margins !== undefined) {\n const margins = pdfoptions.margins;\n if (!margins || typeof margins !== \"object\" || Array.isArray(margins)) return { allowed: false, reason: \"The reviewed pdf margins must be an object with top, right, bottom and left inches.\" };\n for (const side of [\"top\", \"right\", \"bottom\", \"left\"]) {\n const value = (margins as Record<string, unknown>)[side];\n if (value === undefined) continue;\n if (typeof value !== \"number\" || !Number.isFinite(value) || value < 0) return { allowed: false, reason: `The reviewed pdf ${side} margin must be zero or a positive number of inches; negative margins are refused.` };\n }\n }\n if (pdfoptions.scale !== undefined && (typeof pdfoptions.scale !== \"number\" || !Number.isFinite(pdfoptions.scale) || pdfoptions.scale <= 0)) return { allowed: false, reason: \"The reviewed pdf scale must be a positive number with no code cap.\" };\n if (pdfoptions.landscape !== undefined && typeof pdfoptions.landscape !== \"boolean\") return { allowed: false, reason: \"The reviewed pdf landscape flag must be a boolean.\" };\n if (pdfoptions.paginate !== undefined && typeof pdfoptions.paginate !== \"boolean\") return { allowed: false, reason: \"The reviewed pdf paginate flag must be a boolean.\" };\n }\n if (options.breakpoints !== undefined && (!Array.isArray(options.breakpoints) || options.breakpoints.length === 0 || !options.breakpoints.every(item => isnonempty(item)))) return { allowed: false, reason: \"The reviewed pdf break points must be a non-empty list of selectors when present.\" };\n if (options.exporttarget !== undefined && options.exporttarget !== \"memory\" && options.exporttarget !== \"download\") return { allowed: false, reason: \"The reviewed pdf export target must be memory or download; pdf documents do not route to the clipboard.\" };\n if (options.name !== undefined && !isnonempty(options.name)) return { allowed: false, reason: \"The reviewed pdf artifact name must be a non-empty string.\" };\n }\n if (kind === \"recordscreen\" || kind === \"captureaudio\") {\n const recording = options.recording;\n if (recording !== undefined) {\n if (!recording || typeof recording !== \"object\" || Array.isArray(recording)) return { allowed: false, reason: \"The reviewed recording options must be an object in options.recording.\" };\n const recordoptions = recording as Record<string, unknown>;\n if (recordoptions.scope !== undefined && recordoptions.scope !== \"tab\" && recordoptions.scope !== \"run\") return { allowed: false, reason: \"The reviewed recording scope must be tab or run.\" };\n if (recordoptions.fps !== undefined && (typeof recordoptions.fps !== \"number\" || !Number.isFinite(recordoptions.fps) || recordoptions.fps <= 0)) return { allowed: false, reason: \"The reviewed recording fps must be a positive number with no code ceiling.\" };\n if (recordoptions.bitrate !== undefined && (typeof recordoptions.bitrate !== \"number\" || !Number.isFinite(recordoptions.bitrate) || recordoptions.bitrate <= 0)) return { allowed: false, reason: \"The reviewed recording bitrate must be a positive number with no code ceiling.\" };\n if (recordoptions.audio !== undefined && typeof recordoptions.audio !== \"boolean\") return { allowed: false, reason: \"The reviewed recording audio flag must be a boolean.\" };\n }\n if (options.duration !== undefined && (typeof options.duration !== \"number\" || !Number.isFinite(options.duration) || options.duration <= 0)) return { allowed: false, reason: \"The reviewed recording duration must be a positive number of milliseconds with no code ceiling.\" };\n const consent = recordingconsentgranted(step);\n if (!consent.allowed) return consent;\n }\n if (kind === \"captureframe\") {\n if (options.timestamp !== undefined && (typeof options.timestamp !== \"number\" || !Number.isFinite(options.timestamp) || options.timestamp < 0)) return { allowed: false, reason: \"The reviewed frame timestamp must be zero or a positive number of seconds.\" };\n if (options.poster !== undefined && typeof options.poster !== \"boolean\") return { allowed: false, reason: \"The reviewed poster flag must be a boolean.\" };\n const capturecheck = validatecaptureoptions(options.capture);\n if (!capturecheck.allowed) return capturecheck;\n }\n if (kind === \"downloadimages\") {\n const filter = options.imagefilter;\n if (!filter || typeof filter !== \"object\" || Array.isArray(filter)) return { allowed: false, reason: \"A reviewed imagefilter is required in options before any image downloads.\" };\n const imagefilter = filter as Record<string, unknown>;\n if (imagefilter.selector !== undefined && !isnonempty(imagefilter.selector)) return { allowed: false, reason: \"The reviewed imagefilter selector must be a non-empty selector from the reviewed selector grammar.\" };\n if (imagefilter.minwidth !== undefined && (typeof imagefilter.minwidth !== \"number\" || !Number.isFinite(imagefilter.minwidth) || imagefilter.minwidth < 0)) return { allowed: false, reason: \"The reviewed imagefilter minimum width must be zero or a positive number of pixels.\" };\n if (imagefilter.minheight !== undefined && (typeof imagefilter.minheight !== \"number\" || !Number.isFinite(imagefilter.minheight) || imagefilter.minheight < 0)) return { allowed: false, reason: \"The reviewed imagefilter minimum height must be zero or a positive number of pixels.\" };\n if (imagefilter.formats !== undefined && (!Array.isArray(imagefilter.formats) || imagefilter.formats.length === 0 || !imagefilter.formats.every(item => isnonempty(item)))) return { allowed: false, reason: \"The reviewed imagefilter format list must be a non-empty list of mime or extension patterns when present.\" };\n if (options.naming !== undefined) {\n const namingcheck = validatecapturenaming(options.naming);\n if (!namingcheck.allowed) return namingcheck;\n }\n }\n if (kind === \"shotcanvas\") {\n const capturecheck = validatecaptureoptions(options.capture);\n if (!capturecheck.allowed) return capturecheck;\n }\n if (kind === \"probestream\" && options.selector !== undefined && !isnonempty(options.selector)) return { allowed: false, reason: \"The reviewed stream probe scope selector must be a non-empty string.\" };\n if (kind === \"timelapse\") {\n const lapse = options.lapse;\n if (!lapse || typeof lapse !== \"object\" || Array.isArray(lapse)) return { allowed: false, reason: \"A reviewed lapse plan with interval, duration and format is required in options.\" };\n const plan = lapse as Record<string, unknown>;\n if (typeof plan.interval !== \"number\" || !Number.isFinite(plan.interval) || plan.interval <= 0) return { allowed: false, reason: \"The reviewed lapse interval must be a positive number of milliseconds with no code ceiling.\" };\n if (typeof plan.duration !== \"number\" || !Number.isFinite(plan.duration) || plan.duration <= 0) return { allowed: false, reason: \"The reviewed lapse duration must be a positive number of milliseconds with no code ceiling.\" };\n if (plan.format !== undefined && plan.format !== \"png\" && plan.format !== \"jpeg\" && plan.format !== \"webp\") return { allowed: false, reason: \"The reviewed lapse format must be png, jpeg or webp.\" };\n const budget = lapsebudgetallowed(plan.interval as number, plan.duration as number, typeof options.wait === \"number\" ? options.wait : undefined);\n if (!budget.allowed) return budget;\n const capturecheck = validatecaptureoptions(options.capture);\n if (!capturecheck.allowed) return capturecheck;\n }\n if (kind === \"convertimage\" || kind === \"makethumbs\") {\n const single = options.capture;\n const list = options.captures;\n const hasone = isnonempty(single);\n const haslist = Array.isArray(list) && list.length > 0 && list.every(item => isnonempty(item));\n if (!hasone && !haslist) return { allowed: false, reason: \"A reviewed capture id or a reviewed non-empty capture id list is required in options.\" };\n if (hasone && haslist) return { allowed: false, reason: \"The reviewed step needs one capture id or a capture id list, not both.\" };\n }\n if (kind === \"convertimage\") {\n const convert = options.convert;\n if (!convert || typeof convert !== \"object\" || Array.isArray(convert)) return { allowed: false, reason: \"A reviewed convert directive with a target format is required in options.\" };\n const directive = convert as Record<string, unknown>;\n if (directive.target !== \"png\" && directive.target !== \"jpeg\" && directive.target !== \"webp\") return { allowed: false, reason: \"The reviewed conversion target must be png, jpeg or webp.\" };\n if (directive.source !== undefined && directive.source !== \"png\" && directive.source !== \"jpeg\" && directive.source !== \"webp\") return { allowed: false, reason: \"The reviewed conversion source must be png, jpeg or webp.\" };\n if (directive.quality !== undefined && (typeof directive.quality !== \"number\" || !Number.isFinite(directive.quality) || directive.quality < 0 || directive.quality > 100)) return { allowed: false, reason: \"The reviewed conversion quality must stay between zero and one hundred with no code cap inside that range.\" };\n }\n if (kind === \"makethumbs\") {\n const thumb = options.thumb;\n if (!thumb || typeof thumb !== \"object\" || Array.isArray(thumb)) return { allowed: false, reason: \"A reviewed thumb directive with size, fit and suffix is required in options.\" };\n const directive = thumb as Record<string, unknown>;\n if (typeof directive.size !== \"number\" || !Number.isFinite(directive.size) || directive.size <= 0) return { allowed: false, reason: \"The reviewed thumbnail size must be a positive number of pixels with no fixed set.\" };\n if (directive.fit !== \"cover\" && directive.fit !== \"contain\") return { allowed: false, reason: \"The reviewed thumbnail fit must be cover or contain.\" };\n if (!isnonempty(directive.suffix)) return { allowed: false, reason: \"The reviewed thumbnail naming suffix must be a non-empty string.\" };\n }\n return { allowed: true };\n}\n\n/** Validates a single proposal against the active tab origin and local policy. */\nexport function validatestep(step: toolstep, origin: string): policyevaluation {\n if (!allowedactions.has(step.kind)) return { allowed: false, reason: \"Unsupported action kind.\" };\n if (!step.summary.trim()) return { allowed: false, reason: \"A human-readable action summary is required.\" };\n let options: Record<string, unknown>;\n try { options = parseoptions(step); } catch { return { allowed: false, reason: \"Step options must be a JSON object.\" }; }\n const hastargetref = options.targetref !== undefined;\n if (targetactions.has(step.kind) && !step.target?.trim() && !hastargetref) return { allowed: false, reason: \"A page target is required.\" };\n if (valueactions.has(step.kind) && !step.value?.trim()) return { allowed: false, reason: \"A reviewed value is required.\" };\n if (step.kind === \"select\" && !step.value?.trim()) return { allowed: false, reason: \"A reviewed option value is required.\" };\n if (step.kind === \"navigate\" && !step.value) return { allowed: false, reason: \"A navigation URL is required.\" };\n if (hastargetref) {\n const reference = validatetargetref(options.targetref);\n if (!reference.allowed) return reference;\n }\n if (step.kind === \"wait\") {\n try { waitduration(step); } catch { return { allowed: false, reason: \"Wait duration must be zero or a positive number of milliseconds.\" }; }\n }\n if (step.kind === \"navigate\") {\n try {\n if (new URL(step.value ?? \"\").origin !== origin) return { allowed: false, reason: \"Navigation must remain within the approved origin.\" };\n } catch {\n return { allowed: false, reason: \"Navigation URL is invalid.\" };\n }\n }\n if (step.kind === \"tabcreate\" || step.kind === \"windowcreate\" || step.kind === \"downloadfile\") {\n try {\n const url = new URL(step.value ?? \"\");\n if (url.protocol !== \"https:\") return { allowed: false, reason: \"The reviewed URL must use HTTPS.\" };\n } catch {\n return { allowed: false, reason: \"The reviewed URL is invalid.\" };\n }\n }\n if (step.kind === \"tabactivate\" || step.kind === \"tabclose\" || step.kind === \"tabreload\" || step.kind === \"windowclose\" || step.kind === \"windowresize\") {\n if (!isnumericid(step.value)) return { allowed: false, reason: \"A numeric browser id is required.\" };\n }\n if (step.kind === \"zoomset\") {\n const zoom = Number(step.value);\n if (!Number.isFinite(zoom) || zoom <= 0) return { allowed: false, reason: \"The reviewed zoom must be a positive number.\" };\n }\n if (step.kind === \"setattribute\" || step.kind === \"writestorage\") {\n const keyname = step.kind === \"setattribute\" ? \"name\" : \"key\";\n if (typeof options[keyname] !== \"string\" || !(options[keyname] as string).trim()) return { allowed: false, reason: `A reviewed ${keyname} is required in options.` };\n if (typeof options.value !== \"string\") return { allowed: false, reason: \"A reviewed value is required in options.\" };\n }\n if (step.kind === \"windowresize\") {\n if (typeof options.width !== \"number\" || typeof options.height !== \"number\" || !Number.isFinite(options.width) || !Number.isFinite(options.height)) return { allowed: false, reason: \"Reviewed width and height numbers are required in options.\" };\n }\n if ((step.kind === \"scrollpage\" || step.kind === \"scrollby\") && (!numericoption(options, \"x\") || !numericoption(options, \"y\"))) return { allowed: false, reason: \"Scroll amounts must be numbers in options.\" };\n if (step.kind === \"waitfor\" && options.timeout !== undefined && (typeof options.timeout !== \"number\" || options.timeout < 0)) return { allowed: false, reason: \"The waitfor timeout must be zero or a positive number of milliseconds.\" };\n if (step.kind === \"movepointer\") {\n const path = options.pointpath;\n if (!path || typeof path !== \"object\" || Array.isArray(path)) return { allowed: false, reason: \"A reviewed pointpath with start and end points is required in options.\" };\n const points = path as Record<string, unknown>;\n if (!ispoint(points.start) || !ispoint(points.end)) return { allowed: false, reason: \"The reviewed pointpath needs numeric start and end points.\" };\n if (points.waypoints !== undefined && (!Array.isArray(points.waypoints) || !points.waypoints.every(waypoint => ispoint(waypoint)))) return { allowed: false, reason: \"The reviewed pointpath waypoints must be numeric points.\" };\n if (!nonnegativeoption(points, \"duration\")) return { allowed: false, reason: \"The reviewed pointpath duration must be zero or a positive number of milliseconds.\" };\n const speed = options.speedprofile;\n if (speed !== undefined) {\n if (!speed || typeof speed !== \"object\" || Array.isArray(speed)) return { allowed: false, reason: \"The reviewed speed profile must be an object.\" };\n const profile = speed as Record<string, unknown>;\n if (profile.easing !== undefined && profile.easing !== \"linear\" && profile.easing !== \"easeinout\") return { allowed: false, reason: \"The reviewed easing must be linear or easeinout.\" };\n if (!nonnegativeoption(profile, \"peak\")) return { allowed: false, reason: \"The reviewed peak velocity must be zero or a positive number.\" };\n if (!nonnegativeoption(profile, \"jitter\")) return { allowed: false, reason: \"The reviewed jitter window must be zero or a positive number of milliseconds.\" };\n }\n }\n if (step.kind === \"clickpoint\" && (!hastargetref || (options.targetref as Record<string, unknown>).mode !== \"point\")) return { allowed: false, reason: \"A reviewed point target reference is required in options.\" };\n if (step.kind === \"clicktext\" && (!hastargetref || (options.targetref as Record<string, unknown>).mode !== \"text\")) return { allowed: false, reason: \"A reviewed text target reference is required in options.\" };\n if (step.kind === \"clickaria\" && (!hastargetref || (options.targetref as Record<string, unknown>).mode !== \"aria\")) return { allowed: false, reason: \"A reviewed aria target reference is required in options.\" };\n if (step.kind === \"clickname\" && (!hastargetref || (options.targetref as Record<string, unknown>).mode !== \"name\")) return { allowed: false, reason: \"A reviewed name target reference is required in options.\" };\n if (step.kind === \"resolvexpath\" && (!hastargetref || (options.targetref as Record<string, unknown>).mode !== \"xpath\")) return { allowed: false, reason: \"A reviewed xpath target reference is required in options.\" };\n if (step.kind === \"typetime\" && options.delay !== undefined && (typeof options.delay !== \"number\" || !Number.isFinite(options.delay) || options.delay < 0)) return { allowed: false, reason: \"The reviewed per keystroke delay must be zero or a positive number of milliseconds.\" };\n if (step.kind === \"submitsearch\") {\n if (!isnonempty(options.results)) return { allowed: false, reason: \"A reviewed results region selector is required in options.\" };\n if (options.timeout !== undefined && (typeof options.timeout !== \"number\" || !Number.isFinite(options.timeout) || options.timeout < 0)) return { allowed: false, reason: \"The submitsearch timeout must be zero or a positive number of milliseconds.\" };\n }\n if (step.kind === \"selectmulti\") {\n const values = options.values;\n if (!Array.isArray(values) || values.length === 0 || !values.every(value => isnonempty(value))) return { allowed: false, reason: \"A reviewed list of option values is required in options.\" };\n }\n if (step.kind === \"setslider\") {\n const slider = Number(step.value);\n if (!Number.isFinite(slider)) return { allowed: false, reason: \"The reviewed slider value must be a number.\" };\n }\n if (step.kind === \"setdate\" && !/^\\d{4}-\\d{2}-\\d{2}$/.test(step.value ?? \"\")) return { allowed: false, reason: \"The reviewed date must use the yyyy-mm-dd form.\" };\n if (step.kind === \"setcolor\" && !/^#[0-9a-fA-F]{6}$/.test(step.value ?? \"\")) return { allowed: false, reason: \"The reviewed color must use the #rrggbb form.\" };\n if (step.kind === \"keyhold\" && options.holdid !== undefined && !isnonempty(options.holdid)) return { allowed: false, reason: \"The reviewed hold id must be a non-empty string.\" };\n if (step.kind === \"dismissdialog\") {\n const accept = options.accept;\n const answer = options.answer;\n if (accept === undefined && !isnonempty(answer)) return { allowed: false, reason: \"A reviewed accept flag or prompt answer is required in options.\" };\n if (accept !== undefined && typeof accept !== \"boolean\") return { allowed: false, reason: \"The reviewed dialog accept flag must be a boolean.\" };\n if (answer !== undefined && !isnonempty(answer)) return { allowed: false, reason: \"The reviewed prompt answer must be a non-empty string.\" };\n }\n if (step.kind === \"pierceshadow\" && options.shadow !== undefined) {\n if (!Array.isArray(options.shadow) || !options.shadow.every(item => isnonempty(item))) return { allowed: false, reason: \"The reviewed shadow path must be a list of non-empty selectors.\" };\n }\n if (step.kind === \"enterframe\") {\n const path = options.framepath;\n if (!Array.isArray(path) || path.length === 0 || !path.every(item => typeof item === \"number\" && Number.isInteger(item) && item >= 0)) return { allowed: false, reason: \"A reviewed frame path of frame indexes is required in options.\" };\n return validateinnerstep(options, origin);\n }\n if (step.kind === \"retryaction\") {\n const inner = validateinnerstep(options, origin);\n if (!inner.allowed) return inner;\n const rule = options.retryrule;\n if (!rule || typeof rule !== \"object\" || Array.isArray(rule)) return { allowed: false, reason: \"A reviewed retry rule with attempts is required in options.\" };\n const retry = rule as Record<string, unknown>;\n if (typeof retry.attempts !== \"number\" || !Number.isInteger(retry.attempts) || retry.attempts < 1) return { allowed: false, reason: \"The reviewed retry attempts must be a positive integer with no code ceiling.\" };\n if (!nonnegativeoption(retry, \"settle\")) return { allowed: false, reason: \"The reviewed retry settle window must be zero or a positive number of milliseconds.\" };\n if (!nonnegativeoption(retry, \"tolerance\")) return { allowed: false, reason: \"The reviewed retry movement tolerance must be zero or a positive number of pixels.\" };\n }\n if (watchactions.has(step.kind)) {\n if (typeof options.lifetime !== \"number\" || !Number.isFinite(options.lifetime) || options.lifetime <= 0) return { allowed: false, reason: \"A reviewed watch lifetime window in milliseconds is required in options.\" };\n if (options.scopes !== undefined && (!Array.isArray(options.scopes) || !options.scopes.every(scope => isnonempty(scope)))) return { allowed: false, reason: \"The reviewed watch scopes must be a list of non-empty selectors.\" };\n if (options.events !== undefined && (!Array.isArray(options.events) || !options.events.every(event => isnonempty(event)))) return { allowed: false, reason: \"The reviewed watch event kinds must be a list of non-empty strings.\" };\n if (!nonnegativeoption(options, \"poll\")) return { allowed: false, reason: \"The reviewed watch poll interval must be zero or a positive number of milliseconds.\" };\n }\n if (step.kind === \"waitquiet\") {\n const rule = options.quietrule;\n if (!rule || typeof rule !== \"object\" || Array.isArray(rule)) return { allowed: false, reason: \"A reviewed quietrule with an idle threshold is required in options.\" };\n const quiet = rule as Record<string, unknown>;\n if (typeof quiet.idle !== \"number\" || !Number.isFinite(quiet.idle) || quiet.idle <= 0) return { allowed: false, reason: \"The reviewed quiet idle threshold must be a positive number of milliseconds with no code ceiling.\" };\n if (!nonnegativeoption(quiet, \"poll\")) return { allowed: false, reason: \"The reviewed quiet poll interval must be zero or a positive number of milliseconds.\" };\n if (!nonnegativeoption(quiet, \"timeout\")) return { allowed: false, reason: \"The reviewed quiet timeout must be zero or a positive number of milliseconds.\" };\n }\n if (step.kind === \"diffsnapshots\") {\n const versions = options.versions;\n if (!Array.isArray(versions) || versions.length !== 2 || !versions.every(version => typeof version === \"number\" && Number.isInteger(version) && version >= 1)) return { allowed: false, reason: \"Two reviewed observation version numbers are required in options.\" };\n }\n if (step.kind === \"openlink\" || step.kind === \"openprivate\" || step.kind === \"deeplink\") {\n const targetcheck = validatenavtarget(options.navtarget, step.kind);\n if (!targetcheck.allowed) return targetcheck;\n if (step.kind === \"deeplink\") {\n const app = options.app;\n if (!isnonempty(app)) return { allowed: false, reason: \"A reviewed deep link app pattern is required in options.\" };\n const params = options.params;\n if (params !== undefined && (!params || typeof params !== \"object\" || Array.isArray(params) || !Object.values(params).every(item => typeof item === \"string\"))) return { allowed: false, reason: \"The reviewed deep link params must be an object of string values.\" };\n }\n }\n if (step.kind === \"waitload\" && !nonnegativeoption(options, \"timeout\")) return { allowed: false, reason: \"The waitload timeout must be zero or a positive number of milliseconds.\" };\n if (step.kind === \"waiturl\" || step.kind === \"spawait\") {\n if (step.kind === \"waiturl\") {\n const patterncheck = validateurlpattern(options.urlpattern);\n if (!patterncheck.allowed) return patterncheck;\n }\n if (!nonnegativeoption(options, \"timeout\")) return { allowed: false, reason: \"The wait timeout must be zero or a positive number of milliseconds.\" };\n if (!nonnegativeoption(options, \"poll\")) return { allowed: false, reason: \"The wait poll interval must be zero or a positive number of milliseconds.\" };\n }\n if (step.kind === \"followlink\") {\n if (options.fragment !== undefined && typeof options.fragment !== \"boolean\") return { allowed: false, reason: \"The reviewed followlink fragment flag must be a boolean.\" };\n }\n if (step.kind === \"spanav\") {\n if (options.routepattern !== undefined) {\n const routecheck = validateurlpattern(options.routepattern);\n if (!routecheck.allowed) return routecheck;\n }\n if (!nonnegativeoption(options, \"timeout\")) return { allowed: false, reason: \"The spanav route timeout must be zero or a positive number of milliseconds.\" };\n }\n if (step.kind === \"rewritequery\") {\n const set = options.set;\n const remove = options.remove;\n if (set === undefined && remove === undefined) return { allowed: false, reason: \"Reviewed query parameters to set or remove are required in options.\" };\n if (set !== undefined && (!set || typeof set !== \"object\" || Array.isArray(set) || !Object.values(set).every(item => typeof item === \"string\"))) return { allowed: false, reason: \"The reviewed query parameters to set must be an object of string values.\" };\n if (remove !== undefined && (!Array.isArray(remove) || !remove.every(item => isnonempty(item)))) return { allowed: false, reason: \"The reviewed query parameters to remove must be a list of non-empty names.\" };\n }\n if (step.kind === \"navlist\") {\n const listcheck = validateurllist(options, \"urls\");\n if (!listcheck.allowed) return listcheck;\n }\n if (step.kind === \"navprofile\") {\n const profilecheck = validatewaitprofile(options.waitprofile);\n if (!profilecheck.allowed) return profilecheck;\n }\n if (step.kind === \"handleauth\" && !ishttpsurl(step.value)) return { allowed: false, reason: \"A reviewed HTTPS origin or url is required as the auth target.\" };\n if (step.kind === \"printpdf\" && options.name !== undefined && !isnonempty(options.name)) return { allowed: false, reason: \"The reviewed artifact name must be a non-empty string.\" };\n if (step.kind === \"prefetch\") {\n const listcheck = validateurllist(options, \"urls\");\n if (!listcheck.allowed) return listcheck;\n }\n if (step.kind === \"preconnect\") {\n const origins = options.origins;\n if (!Array.isArray(origins) || origins.length === 0 || !origins.every(originurl => ishttpsurl(originurl))) return { allowed: false, reason: \"A reviewed non-empty list of HTTPS origins is required in options.\" };\n }\n if (step.kind === \"reopentab\" && step.value !== undefined && !ishttpsurl(step.value)) return { allowed: false, reason: \"The reviewed reopen url must use HTTPS.\" };\n if (step.kind === \"navrate\") {\n const limitcheck = validateratelimit(options.ratelimit);\n if (!limitcheck.allowed) return limitcheck;\n }\n if (step.kind === \"checksafe\" && !ishttpsurl(step.value)) return { allowed: false, reason: \"A reviewed HTTPS url is required for the safety check.\" };\n if (step.kind === \"batchopen\") {\n const listcheck = validateurllist(options, \"urls\");\n if (!listcheck.allowed) return listcheck;\n }\n if (istabscommandkind(step.kind)) {\n const tabscheck = validatetabsgrammar(step, options);\n if (!tabscheck.allowed) return tabscheck;\n }\n if (isformkind(step.kind)) {\n const formcheck = validateformgrammar(step, options);\n if (!formcheck.allowed) return formcheck;\n }\n if (isdatasetkind(step.kind)) {\n const datacheck = validatedatagrammar(step, options, origin);\n if (!datacheck.allowed) return datacheck;\n }\n if (isfileskind(step.kind)) {\n const filescheck = validatefilesgrammar(step, options);\n if (!filescheck.allowed) return filescheck;\n }\n if (iscapturekind(step.kind)) {\n const capturecheck = validatecapturegrammar(step, options);\n if (!capturecheck.allowed) return capturecheck;\n }\n if (ismediakind(step.kind)) {\n const mediacheck = validatemediagrammar(step, options);\n if (!mediacheck.allowed) return mediacheck;\n }\n if (ishttpkind(step.kind)) {\n const httpcheck = validatehttpgrammar(step, options);\n if (!httpcheck.allowed) return httpcheck;\n }\n if (issocketkind(step.kind)) {\n const socketcheck = validatesocketgrammar(step, options);\n if (!socketcheck.allowed) return socketcheck;\n }\n if (isnetwatchkind(step.kind)) {\n const netwatchcheck = validatenetwatchgrammar(step, options);\n if (!netwatchcheck.allowed) return netwatchcheck;\n }\n if (iscontrolkind(step.kind)) {\n const controlcheck = validatecontrolgrammar(step, options);\n if (!controlcheck.allowed) return controlcheck;\n }\n if (isdebugkind(step.kind)) {\n const timelinecheck = validatetimelinegrammar(step, options);\n if (!timelinecheck.allowed) return timelinecheck;\n }\n if (iscdpkind(step.kind)) {\n const cdpcheck = validatecdpgrammar(step, options);\n if (!cdpcheck.allowed) return cdpcheck;\n }\n if (isprofilekind(step.kind)) {\n const profilecheck = validateprofilegrammar(step, options);\n if (!profilecheck.allowed) return profilecheck;\n }\n if (isemulationkind(step.kind)) {\n const emulationcheck = validateemulationgrammar(step, options);\n if (!emulationcheck.allowed) return emulationcheck;\n }\n if (issessionkind(step.kind)) {\n const sessioncheck = validatesessiongrammar(step, options);\n if (!sessioncheck.allowed) return sessioncheck;\n }\n if (isworkflowkind(step.kind)) {\n const workflowcheck = validateworkflowgrammar(step, options);\n if (!workflowcheck.allowed) return workflowcheck;\n }\n if (istriggeraction(step.kind)) {\n const triggercheck = validatetriggergrammar(step, options);\n if (!triggercheck.allowed) return triggercheck;\n }\n if (step.kind === \"tabcreate\") {\n if (options.background !== undefined && typeof options.background !== \"boolean\") return { allowed: false, reason: \"The reviewed background flag must be a boolean.\" };\n if (options.window !== undefined && (typeof options.window !== \"number\" || !Number.isInteger(options.window) || options.window < 0)) return { allowed: false, reason: \"The reviewed target window id must be a non-negative integer.\" };\n }\n if (step.kind === \"windowcreate\") {\n for (const field of [\"left\", \"top\", \"width\", \"height\"]) {\n if (options[field] !== undefined && (typeof options[field] !== \"number\" || !Number.isFinite(options[field]))) return { allowed: false, reason: `The reviewed window ${field} must be a number.` };\n }\n if (options.state !== undefined && ![\"normal\", \"maximized\", \"minimized\", \"fullscreen\"].includes(options.state as string)) return { allowed: false, reason: \"The reviewed window state must be normal, maximized, minimized or fullscreen.\" };\n }\n return { allowed: true };\n}\n\n/** Shared session gate: a live, unpaused session that still matches the active tab. */\nfunction sessiongate(input: { session: agentsession | undefined; tabid: number; origin: string; now: number; action: string }): policyevaluation {\n if (!input.session || input.session.stoppedat) return { allowed: false, reason: \"No active browser session exists.\" };\n if (input.session.expiresat <= input.now) return { allowed: false, reason: \"The browser session has expired.\" };\n if (input.session.pausedat) return { allowed: false, reason: `The browser session is paused and cannot ${input.action}.` };\n if (input.session.tabid !== input.tabid || input.session.origin !== input.origin) return { allowed: false, reason: `The ${input.action} is outside the approved tab or origin.` };\n return { allowed: true };\n}\n\n/** Applies the consent gate immediately before an action reaches the page bridge. */\nexport function canexecute(input: { session: agentsession | undefined; plan: agentplan | undefined; step: toolstep; tabid: number; origin: string; now?: number; verdicts?: safetyverdict[]; settings?: runsettings }): policyevaluation {\n const now = input.now ?? Date.now();\n const gate = sessiongate({ session: input.session, tabid: input.tabid, origin: input.origin, now, action: \"execute an action\" });\n if (!gate.allowed) return gate;\n if (!input.plan || input.plan.state !== \"approved\") return { allowed: false, reason: \"The plan has not received explicit approval.\" };\n if (input.plan.expiresat <= now) return { allowed: false, reason: \"The approved plan has expired.\" };\n if ((input.step.kind === \"pierceshadow\" || input.step.kind === \"enterframe\") && !origingranted(input.session, input.origin)) return { allowed: false, reason: \"The shadow or frame step is outside the session origin grants.\" };\n if (input.step.kind === \"readjson\" && !origingranted(input.session, input.origin)) return { allowed: false, reason: \"The json state read is outside the session origin grants.\" };\n if (isexportkind(input.step.kind)) {\n const exportgate = exportgranted(input.session, input.origin);\n if (!exportgate.allowed) return exportgate;\n }\n if (input.step.kind === \"navlist\") {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(input.step); } catch { options = {}; }\n for (const url of Array.isArray(options.urls) ? options.urls : []) {\n if (typeof url !== \"string\") continue;\n const navigation = navigationgranted(input.session, url);\n if (!navigation.allowed) return navigation;\n }\n }\n if (islayoutkind(input.step.kind) && !layoutmutationgranted(input.session, now).allowed) return { allowed: false, reason: \"Group and layout mutations stay inside the active session.\" };\n if (input.step.kind === \"submitform\" || input.step.kind === \"retryform\") {\n if (!input.plan) return { allowed: false, reason: \"Form submission requires an asksubmit review step before it.\" };\n const reviewgate = submitreviewgranted(input.plan.steps, input.step.id);\n if (!reviewgate.allowed) return reviewgate;\n }\n if (input.step.kind === \"consentpassword\") {\n const consentgate = passwordconsentgranted(input.step);\n if (!consentgate.allowed) return consentgate;\n }\n if (input.step.kind === \"readclipboard\") {\n const clipgate = clipboardconsentgranted(input.step);\n if (!clipgate.allowed) return clipgate;\n }\n if (input.step.kind === \"interceptmime\" && !origingranted(input.session, input.origin)) return { allowed: false, reason: \"The download interception is outside the session origin grants.\" };\n if (iscapturekind(input.step.kind)) {\n const capturegatecheck = capturegate(input.session, input.tabid, input.origin, now);\n if (!capturegatecheck.allowed) return capturegatecheck;\n let captureoptions: Record<string, unknown> = {};\n try { captureoptions = parseoptions(input.step); } catch { captureoptions = {}; }\n const target = (captureoptions.capture as Record<string, unknown> | undefined)?.exporttarget;\n if (target !== undefined && target !== \"memory\" && target !== \"download\" && target !== \"clipboard\") return { allowed: false, reason: \"The capture export target must be memory, download or clipboard.\" };\n }\n if (ismediakind(input.step.kind)) {\n const mediagatecheck = mediagate(input.session, input.tabid, input.origin, now);\n if (!mediagatecheck.allowed) return mediagatecheck;\n }\n if (isrecordingkind(input.step.kind)) {\n const recordinggate = recordingconsentgranted(input.step);\n if (!recordinggate.allowed) return recordinggate;\n }\n if (ishttpkind(input.step.kind)) {\n const target = outboundtarget(input.step);\n if (target !== undefined) {\n const outboundgate = origincheck(input.session, target);\n if (!outboundgate.allowed) return outboundgate;\n }\n if (input.step.kind === \"fetchurl\" || input.step.kind === \"callrest\" || input.step.kind === \"callgraphql\") {\n const consentgate = fetchconsentrefgranted(input.step);\n if (!consentgate.allowed) return consentgate;\n }\n }\n if (issocketkind(input.step.kind)) {\n const channelurl = sockettarget(input.step);\n if (channelurl !== undefined) {\n const channelgate = socketgate(input.session, channelurl);\n if (!channelgate.allowed) return channelgate;\n }\n }\n if (input.step.kind === \"watchrequests\") {\n const watchgatecheck = watchgate(input.session, input.settings, now);\n if (!watchgatecheck.allowed) return watchgatecheck;\n }\n if (isdebugkind(input.step.kind)) {\n const timelinegatecheck = timelinegate(input.session, input.tabid, input.origin, now);\n if (!timelinegatecheck.allowed) return timelinegatecheck;\n }\n if (iscdpkind(input.step.kind)) {\n const debuggatecheck = debuggate(input.session, input.tabid, input.origin, now);\n if (!debuggatecheck.allowed) return debuggatecheck;\n if (!input.plan) return { allowed: false, reason: \"The devtools protocol steps need an approved plan.\" };\n const allowlist = planallowlist(input.plan.steps);\n if (input.step.kind !== \"attachcdp\") {\n if (allowlist === undefined) return { allowed: false, reason: \"The devtools protocol step needs the attachcdp step of the same plan with its enabled domains first.\" };\n if (input.step.kind === \"cdpcmd\") {\n let cdpoptions: Record<string, unknown> = {};\n try { cdpoptions = parseoptions(input.step); } catch { cdpoptions = {}; }\n const command = cdpoptions.command && typeof cdpoptions.command === \"object\" && !Array.isArray(cdpoptions.command) ? cdpoptions.command as Record<string, unknown> : undefined;\n const method = typeof command?.method === \"string\" ? command.method : \"\";\n if (methoddomain(method) === undefined || !allowlistcovers(allowlist, method)) return { allowed: false, reason: `The raw command ${method || \"\"} stays outside the enabled domain allowlist of the plan attach; review the attach domains or the method gates.` };\n }\n }\n let cdpoptions: Record<string, unknown> = {};\n try { cdpoptions = parseoptions(input.step); } catch { cdpoptions = {}; }\n if (input.step.kind === \"setbreakpoint\") {\n const breakpoint = breakpointinputof(cdpoptions.breakpoint);\n if (breakpoint) {\n const targetgate = origincheck(input.session, breakpoint.url);\n if (!targetgate.allowed) return targetgate;\n }\n }\n if (input.step.kind === \"overridescript\") {\n const override = overrideinputof(cdpoptions.override);\n if (override) {\n const targetgate = origincheck(input.session, override.urlpattern);\n if (!targetgate.allowed) return targetgate;\n }\n }\n }\n if (isprofilekind(input.step.kind)) {\n let profileoptions: Record<string, unknown> = {};\n try { profileoptions = parseoptions(input.step); } catch { profileoptions = {}; }\n const targets = [\n ...(attachtargetof(profileoptions.target) !== undefined ? [attachtargetof(profileoptions.target) as attachtarget] : []),\n ...(Array.isArray(profileoptions.attachtargets) ? profileoptions.attachtargets.flatMap(target => { const parsed = attachtargetof(target); return parsed !== undefined ? [parsed] : []; }) : []),\n ];\n const targetgatecheck = targetgate({ session: input.session, tabid: input.tabid, origin: input.origin, targets, grants: undefined, now });\n if (!targetgatecheck.allowed) return targetgatecheck;\n if (input.step.kind === \"capturesourcemaps\") {\n for (const url of Array.isArray(profileoptions.scripts) ? profileoptions.scripts : []) {\n if (typeof url !== \"string\") continue;\n const scriptgate = origincheck(input.session, url);\n if (!scriptgate.allowed) return scriptgate;\n }\n }\n }\n if (isemulationkind(input.step.kind)) {\n const emugatecheck = emugate({ session: input.session, plan: input.plan, step: input.step, tabid: input.tabid, origin: input.origin, now });\n if (!emugatecheck.allowed) return emugatecheck;\n }\n if (issessionkind(input.step.kind)) {\n const sessiongatecheck = sessionrestoregate({ session: input.session, plan: input.plan, step: input.step, tabid: input.tabid, origin: input.origin, now });\n if (!sessiongatecheck.allowed) return sessiongatecheck;\n if (input.step.kind === \"restoresession\") {\n let restoreoptions: Record<string, unknown> = {};\n try { restoreoptions = parseoptions(input.step); } catch { restoreoptions = {}; }\n for (const url of Array.isArray(restoreoptions.origins) ? restoreoptions.origins : []) {\n if (typeof url !== \"string\" || !url) continue;\n const origingate = origincheck(input.session, url);\n if (!origingate.allowed) return { allowed: false, reason: `The session restore reopens ${url} outside the session origin grants; review the restore record or grant the origin.` };\n }\n }\n }\n if (isworkflowkind(input.step.kind)) {\n const workflowgatecheck = workflowgate({ session: input.session, plan: input.plan, step: input.step, tabid: input.tabid, origin: input.origin, now });\n if (!workflowgatecheck.allowed) return workflowgatecheck;\n }\n if (istriggeraction(input.step.kind)) {\n const triggergatecheck = triggergate({ session: input.session, plan: input.plan, step: input.step, tabid: input.tabid, origin: input.origin, now });\n if (!triggergatecheck.allowed) return triggergatecheck;\n }\n if (iscontrolkind(input.step.kind)) {\n const controlgate = sessiongate({ session: input.session, tabid: input.tabid, origin: input.origin, now, action: \"control the network\" });\n if (!controlgate.allowed) return controlgate;\n let controloptions: Record<string, unknown> = {};\n try { controloptions = parseoptions(input.step); } catch { controloptions = {}; }\n if (input.step.kind === \"blockrequest\") {\n const blockgatecheck = blockgate(input.session, input.step, now);\n if (!blockgatecheck.allowed) return blockgatecheck;\n const rule = blockruleof(controloptions.block);\n if (rule) {\n const blockorigin = origincheck(input.session, rule.urlpattern);\n if (!blockorigin.allowed) return blockorigin;\n }\n }\n if (input.step.kind === \"mockresponse\" || input.step.kind === \"rewriteheaders\") {\n const patterns = input.step.kind === \"mockresponse\" ? [mockspecof(controloptions.mock)?.urlpattern ?? \"\"] : (Array.isArray(controloptions.rules) ? controloptions.rules.map(item => item && typeof item === \"object\" && !Array.isArray(item) ? String((item as Record<string, unknown>).urlpattern ?? \"\") : \"\") : []);\n for (const pattern of patterns) {\n const patterngate = origincheck(input.session, pattern);\n if (!patterngate.allowed) return patterngate;\n }\n }\n if (input.step.kind === \"setcookies\" || input.step.kind === \"readcookies\" || input.step.kind === \"clearcookies\") {\n const domain = typeof controloptions.domain === \"string\" && controloptions.domain.trim() ? controloptions.domain : Array.isArray(controloptions.cookies) ? String((controloptions.cookies[0] as Record<string, unknown> | undefined)?.domain ?? \"\") : \"\";\n if (!domain) return { allowed: false, reason: \"A reviewed cookie domain is required before cookie control runs.\" };\n const cookiegatecheck = cookiegate(input.session, domain, now);\n if (!cookiegatecheck.allowed) return cookiegatecheck;\n }\n if (input.step.kind === \"authflow\") {\n const authconsent = authconsentgranted(input.step);\n if (!authconsent.allowed) return authconsent;\n }\n if (input.step.kind === \"saveapikey\") {\n const keyconsent = apikeyconsentgranted(input.step);\n if (!keyconsent.allowed) return keyconsent;\n }\n if (input.step.kind === \"routeproxy\") {\n const proxygatecheck = proxygate(input.session, input.step, now);\n if (!proxygatecheck.allowed) return proxygatecheck;\n }\n const target = controltarget(input.step);\n if (target !== undefined) {\n const targetgate = origincheck(input.session, target);\n if (!targetgate.allowed) return targetgate;\n }\n }\n if (input.step.kind === \"extractapi\") {\n let replayoptions: Record<string, unknown> = {};\n try { replayoptions = parseoptions(input.step); } catch { replayoptions = {}; }\n const replay = apireplayspecof(replayoptions.replay);\n if (replay !== undefined) {\n const replaygate = origincheck(input.session, replay.endpoint);\n if (!replaygate.allowed) return replaygate;\n }\n }\n if (input.step.kind === \"openlink\" || input.step.kind === \"openprivate\" || input.step.kind === \"batchopen\" || input.step.kind === \"prefetch\" || input.step.kind === \"deeplink\" || input.step.kind === \"reopentab\") {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(input.step); } catch { options = {}; }\n const grammar = validatestep(input.step, input.origin);\n if (!grammar.allowed) return grammar;\n const grants = input.session?.grants ?? [input.session?.origin ?? input.origin];\n const targets: unknown[] = input.step.kind === \"batchopen\" || input.step.kind === \"prefetch\" ? (Array.isArray(options.urls) ? options.urls : []) : input.step.kind === \"reopentab\" ? [input.step.value] : [(options.navtarget as Record<string, unknown> | undefined)?.url];\n for (const target of targets) {\n if (typeof target !== \"string\" || !target) continue;\n const verified = originverified(target, grants, input.verdicts ?? []);\n if (!verified.allowed) return verified;\n }\n }\n return validatestep(input.step, input.origin);\n}\n\n/** Allows a non-mutating, temporary target preview during plan review. */\nexport function canpreview(input: { session: agentsession | undefined; plan: agentplan | undefined; step: toolstep; tabid: number; origin: string; now?: number }): policyevaluation {\n const now = input.now ?? Date.now();\n const gate = sessiongate({ session: input.session, tabid: input.tabid, origin: input.origin, now, action: \"preview a target\" });\n if (!gate.allowed) return gate;\n if (!input.plan || ![\"pending\", \"approved\"].includes(input.plan.state)) return { allowed: false, reason: \"Only a reviewed pending or approved plan can be previewed.\" };\n if (input.plan.expiresat <= now) return { allowed: false, reason: \"The reviewed plan has expired.\" };\n let options: Record<string, unknown> = {};\n try { options = parseoptions(input.step); } catch { options = {}; }\n if (!targetactions.has(input.step.kind) && options.targetref === undefined) return { allowed: false, reason: \"Only a target-based action can be previewed.\" };\n return validatestep(input.step, input.origin);\n}\n\n/** Lists every reviewed action kind of the policy table so the step library of the editor browses the whole vocabulary. */\nexport function reviewedkinds(): string[] {\n return [...allowedactions].sort();\n}\n\n/** The editor save gate: the canvas model of a save needs a live session and an approved plan like every other reviewed artifact, its nodes must be steps or block invocations with unique ids, its edges must reference existing steps and run forward only so no cycle forms, and the composed record still passes the full workflow grammar through the composition the save triggers. */\nexport function editorsavegate(input: { session: agentsession | undefined; plan: agentplan | undefined; model: editormodel; now: number }): policyevaluation {\n const gate = sessiongate({ session: input.session, tabid: input.session?.tabid ?? 0, origin: input.session?.origin ?? \"https://example.com\", now: input.now, action: \"save the workflow editor canvas\" });\n if (!gate.allowed) return gate;\n if (!input.plan || input.plan.state !== \"approved\") return { allowed: false, reason: \"Editor saves need the approved plan review before a new workflow version composes.\" };\n const model = input.model;\n if (typeof model.name !== \"string\" || !model.name.trim()) return { allowed: false, reason: \"The workflow name of the canvas must be a non-empty string.\" };\n if (typeof model.version !== \"number\" || !Number.isInteger(model.version) || model.version < 1) return { allowed: false, reason: \"The workflow version of the canvas must be a positive integer.\" };\n if (!Array.isArray(model.origins) || model.origins.length === 0) return { allowed: false, reason: \"The canvas needs at least one granted HTTPS origin.\" };\n const ids = new Set<string>();\n for (const node of model.nodes) {\n if ((node.step === undefined) === (node.invocation === undefined)) return { allowed: false, reason: \"Every canvas node must be exactly one workflow step or one block invocation.\" };\n const id = node.id ?? (node.step !== undefined ? node.step.id : (node.invocation as { block: string }).block);\n if (!id || ids.has(id)) return { allowed: false, reason: `The canvas node id ${id || \"(empty)\"} must be unique.` };\n ids.add(id);\n }\n const reachable = new Set<string>();\n for (const node of model.nodes) {\n if (node.step !== undefined) { reachable.add(node.step.id); continue; }\n const walk = (entries: Array<{ id?: string; kind?: string; label?: string; block?: string }>): void => {\n for (const entry of entries) {\n if (typeof entry.id === \"string\" && typeof entry.kind === \"string\") { reachable.add(entry.id); continue; }\n if (typeof entry.block === \"string\") {\n const nested = model.blocks.find(candidate => candidate.name === entry.block);\n if (nested) walk(nested.steps as Array<{ id?: string; kind?: string; label?: string; block?: string }>);\n }\n }\n };\n const block = model.blocks.find(candidate => candidate.name === (node.invocation as { block: string }).block);\n if (!block) return { allowed: false, reason: `The block ${(node.invocation as { block: string }).block} of the canvas has no definition.` };\n walk(block.steps as Array<{ id?: string; kind?: string; label?: string; block?: string }>);\n }\n let order = 0;\n const positionof = new Map<string, number>();\n for (const node of model.nodes) {\n if (node.step !== undefined) { positionof.set(node.step.id, order); order += 1; continue; }\n const walk = (entries: Array<{ id?: string; kind?: string; label?: string; block?: string }>): void => {\n for (const entry of entries) {\n if (typeof entry.id === \"string\" && typeof entry.kind === \"string\") { positionof.set(entry.id, order); order += 1; continue; }\n if (typeof entry.block === \"string\") {\n const nested = model.blocks.find(candidate => candidate.name === entry.block);\n if (nested) walk(nested.steps as Array<{ id?: string; kind?: string; label?: string; block?: string }>);\n }\n }\n };\n walk((model.blocks.find(candidate => candidate.name === (node.invocation as { block: string }).block) as { steps: Array<{ id?: string; kind?: string; label?: string; block?: string }> }).steps);\n }\n for (const edge of model.edges) {\n if (!reachable.has(edge.from)) return { allowed: false, reason: `The canvas edge of ${edge.variable} references the unknown source step ${edge.from}.` };\n if (!reachable.has(edge.to)) return { allowed: false, reason: `The canvas edge of ${edge.variable} references the unknown target step ${edge.to}.` };\n if ((positionof.get(edge.from) ?? -1) >= (positionof.get(edge.to) ?? -1)) return { allowed: false, reason: `The canvas edge of ${edge.variable} runs backwards and would form a cycle.` };\n }\n return { allowed: true };\n}\n\n/** Refuses to run a workflow whose review state stays pending: an imported workflow or a version rollback stays unreviewed until the user approves its expanded step list through the import or rollback review. */\nexport function runreviewgranted(record: workflowrecord): policyevaluation {\n if (record.reviewstate === \"pending\") return { allowed: false, reason: \"The workflow stays unreviewed: the import or rollback review must approve its expanded step list before any run.\" };\n return { allowed: true };\n}\n\n/** The reviewed policy knobs a per site override may adjust: loop safety bounds, per step and per run timeout budgets, element wait timeouts and delay bases. */\nconst overrideknobs = [\"loopbound\", \"stepms\", \"runms\", \"waitms\", \"delaybase\"];\n\n/** Validates one per site policy override so overrides only adjust reviewed knobs: the pattern must be an https origin or a `*` subdomain glob of one and every delta must name a reviewed knob with a positive user value and no code ceiling. */\nexport function validatesiteoverride(override: { pattern: string; deltas: Record<string, number> }): policyevaluation {\n if (typeof override.pattern !== \"string\" || !override.pattern.startsWith(\"https://\") || !/[a-z0-9.-]+/i.test(override.pattern.slice(8))) return { allowed: false, reason: \"The override pattern must be an https origin or a `*` subdomain glob of one.\" };\n if (!override.pattern.includes(\"*\")) {\n try {\n if (new URL(override.pattern).origin !== override.pattern) return { allowed: false, reason: \"The override pattern must be a bare https origin or a `*` subdomain glob, never a path.\" };\n } catch {\n return { allowed: false, reason: \"The override pattern must parse as an https origin or a `*` subdomain glob of one.\" };\n }\n }\n for (const [knob, delta] of Object.entries(override.deltas)) {\n if (!overrideknobs.includes(knob)) return { allowed: false, reason: `The override knob ${knob} is not one of the reviewed knobs: ${overrideknobs.join(\", \")}.` };\n if (typeof delta !== \"number\" || !Number.isFinite(delta) || delta <= 0) return { allowed: false, reason: `The override delta of ${knob} must be a positive user value with no code ceiling.` };\n }\n return { allowed: true };\n}\n\n/** Validates the export contents of a workflow file so secrets never leave the browser: every step options object of the workflow and of every packed template is parsed and any field that names a secret, token, api key, password or authorization header refuses the export. */\nexport function exportcontentreview(file: { workflow: workflowrecord; templates: steptemplate[] }): policyevaluation {\n const secretkeys = /(secret|token|apikey|api_key|password|authorization|credential)/i;\n const scan = (label: string, options: string | undefined): policyevaluation | undefined => {\n if (options === undefined) return undefined;\n let payload: unknown;\n try { payload = JSON.parse(options); } catch { return undefined; }\n const walk = (value: unknown, path: string): policyevaluation | undefined => {\n if (!value || typeof value !== \"object\") return undefined;\n for (const [key, entry] of Object.entries(value as Record<string, unknown>)) {\n if (secretkeys.test(key)) return { allowed: false, reason: `The export of ${label} carries the secret field ${path}${key} and secrets never leave the browser.` };\n const nested = walk(entry, `${path}${key}.`);\n if (nested !== undefined) return nested;\n }\n return undefined;\n };\n return walk(payload, \"\");\n };\n for (const step of file.workflow.steps) {\n const refusal = scan(`the step ${step.id}`, step.options);\n if (refusal !== undefined) return refusal;\n }\n for (const template of file.templates) {\n const refusal = scan(`the template ${template.name}`, template.step.options);\n if (refusal !== undefined) return refusal;\n }\n return { allowed: true };\n}\n\n/** Validates one watchdog configuration: the stall threshold stays a positive user value with no code ceiling, the recovery action is one of retry, pause or cancel and the zombie window, when configured, stays positive with no ceiling. */\nexport function watchdogconfigvalid(config: watchdogconfig): policyevaluation {\n if (typeof config.enabled !== \"boolean\") return { allowed: false, reason: \"The watchdog enabled flag must be a boolean.\" };\n if (typeof config.stallthreshold !== \"number\" || !Number.isFinite(config.stallthreshold) || config.stallthreshold <= 0) return { allowed: false, reason: \"The watchdog stall threshold must be a positive number of milliseconds with no code ceiling.\" };\n if (![\"retry\", \"pause\", \"cancel\"].includes(config.action)) return { allowed: false, reason: \"The watchdog recovery action must be retry, pause or cancel.\" };\n if (config.zombiewindow !== undefined && (typeof config.zombiewindow !== \"number\" || !Number.isFinite(config.zombiewindow) || config.zombiewindow <= 0)) return { allowed: false, reason: \"The watchdog zombie window, when configured, must be a positive number of milliseconds with no code ceiling.\" };\n return { allowed: true };\n}\n\n/** Validates one mcp tool catalog against the action kind grammar: every tool name stays namespaced and unique, every wrapped kind belongs to the reviewed vocabulary, every namespace keeps its tools inside its domain kinds and every input schema carries typed properties with its required list. */\nexport function validatetoolcatalog(catalog: toolcatalog): policyevaluation {\n if (!Array.isArray(catalog.domains) || catalog.domains.length === 0) return { allowed: false, reason: \"The tool catalog needs its tool domains.\" };\n const seen = new Set<string>();\n for (const domain of catalog.domains) {\n if (!toolnamespaces.includes(domain.namespace)) return { allowed: false, reason: `The tool domain ${String(domain.namespace)} is not a reviewed namespace.` };\n if (!Array.isArray(domain.tools) || domain.tools.length === 0) return { allowed: false, reason: `The ${domain.namespace} domain exposes no tools.` };\n for (const tool of domain.tools) {\n if (typeof tool.name !== \"string\" || !tool.name.startsWith(`${domain.namespace}.`)) return { allowed: false, reason: `The tool ${String(tool.name)} does not carry its ${domain.namespace} namespace prefix.` };\n if (seen.has(tool.name)) return { allowed: false, reason: `The tool name ${tool.name} is not unique across the catalog.` };\n seen.add(tool.name);\n if (!allowedactions.has(tool.kind)) return { allowed: false, reason: `The tool ${tool.name} wraps ${String(tool.kind)} which is outside the reviewed action kind grammar.` };\n if (!domainkinds[domain.namespace].includes(tool.kind)) return { allowed: false, reason: `The tool ${tool.name} wraps ${String(tool.kind)} which does not belong to the ${domain.namespace} domain.` };\n if (typeof tool.description !== \"string\" || tool.description.trim() === \"\") return { allowed: false, reason: `The tool ${tool.name} needs its plain language description.` };\n const schema = tool.inputschema;\n if (!schema || schema.type !== \"object\" || schema.properties === undefined || schema.properties === null || typeof schema.properties !== \"object\" || Array.isArray(schema.properties) || Object.keys(schema.properties).length === 0) return { allowed: false, reason: `The tool ${tool.name} needs its json schema inputs of at least one typed property.` };\n for (const [name, property] of Object.entries(schema.properties)) {\n if (![\"string\", \"number\", \"boolean\", \"object\", \"array\"].includes(property.type)) return { allowed: false, reason: `The ${tool.name} input ${name} carries an untyped property.` };\n if (typeof property.description !== \"string\" || property.description.trim() === \"\") return { allowed: false, reason: `The ${tool.name} input ${name} needs its plain language description.` };\n }\n for (const name of schema.required) {\n if (!(name in schema.properties)) return { allowed: false, reason: `The tool ${tool.name} marks ${name} required outside its properties.` };\n }\n }\n }\n return { allowed: true };\n}\n\n/** Grades one tooldef with the risk class of its action kind and refuses a tool whose declared grade disagrees with the grammar. */\nexport function toolriskgrade(tool: tooldef): policyevaluation {\n const grade = actionrisk(tool.kind);\n if (grade !== tool.risk) return { allowed: false, reason: `The tool ${tool.name} declares the ${tool.risk} grade while its kind ${String(tool.kind)} grades ${grade}.` };\n return { allowed: true };\n}\n\n/** Requires consent metadata on every tool with side effects: read only tools stay free of the extra review while interaction and sensitive tools must declare their review requirement. */\nexport function toolconsentrequired(tool: tooldef): policyevaluation {\n if (tool.risk === \"read\") return { allowed: true };\n if (tool.consentmeta === undefined || typeof tool.consentmeta.review !== \"string\" || tool.consentmeta.review.trim() === \"\") return { allowed: false, reason: `The tool ${tool.name} has side effects and needs its consent metadata with the review requirement.` };\n return { allowed: true };\n}\n\n/** Grades one server bind configuration: the localhost bind stays the reviewed default while a bind outside localhost grades sensitive and needs the explicit remote review flag. */\nexport function serverbindgate(config: mcpserverconfig): policyevaluation {\n const bind = config.bind !== undefined && config.bind.trim() !== \"\" ? config.bind.trim() : \"127.0.0.1\";\n const local = bind === \"127.0.0.1\" || bind === \"localhost\" || bind === \"::1\";\n if (!local && config.remote !== true) return { allowed: false, reason: `The bind ${bind} leaves localhost and grades sensitive: the explicit remote review must approve it first.` };\n return { allowed: true };\n}\n\n/** Refuses one tool whose version stays below the negotiated compatibility floor so a client never receives a tool older than it can parse. */\nexport function toolversionfloor(tool: tooldef, floor: number): policyevaluation {\n if (typeof floor === \"number\" && Number.isFinite(floor) && tool.version < floor) return { allowed: false, reason: `The tool ${tool.name} of version ${tool.version} stays below the negotiated compatibility floor of ${floor}.` };\n return { allowed: true };\n}\n\n/** Requires the explicit user enablement before the mcp server ever starts; a disabled or unreviewed config never listens. */\nexport function serverenablementgate(config: mcpserverconfig): policyevaluation {\n if (config.enabled !== true) return { allowed: false, reason: \"The mcp server starts only after the user enables it; the protocol surface stays closed by default.\" };\n const bind = serverbindgate(config);\n if (!bind.allowed) return bind;\n if (!Array.isArray(config.transports) || config.transports.length === 0) return { allowed: false, reason: \"The mcp server needs at least one allowed transport of stdio or http.\" };\n if (!config.transports.every(transport => transport === \"stdio\" || transport === \"http\")) return { allowed: false, reason: \"The allowed transports of the mcp server are stdio and http.\" };\n if (typeof config.port !== \"number\" || !Number.isFinite(config.port) || config.port <= 0 || config.port > 65535) return { allowed: false, reason: \"The http listener port must be a valid port number.\" };\n if (config.framesize !== undefined && (typeof config.framesize !== \"number\" || !Number.isFinite(config.framesize) || config.framesize <= 0)) return { allowed: false, reason: \"The user configured frame size must stay a positive number with no code ceiling.\" };\n if (config.queuedepth !== undefined && (typeof config.queuedepth !== \"number\" || !Number.isFinite(config.queuedepth) || config.queuedepth <= 0)) return { allowed: false, reason: \"The user configured queue depth must stay a positive number with no code ceiling.\" };\n const remote = remoteenablementgate(config);\n if (!remote.allowed) return remote;\n return { allowed: true };\n}\n\n/** Validates the namespace membership of one tool: the name prefix must name the domain the tool lives in and the wrapped kind must belong to that domain so no tool drifts out of its namespace. */\nexport function toolnamespacegate(tool: tooldef): policyevaluation {\n const namespace = tool.name.split(\".\")[0];\n if (!toolnamespaces.includes(namespace as never)) return { allowed: false, reason: `The tool ${tool.name} carries no reviewed namespace prefix.` };\n if (!domainkinds[namespace as keyof typeof domainkinds].includes(tool.kind)) return { allowed: false, reason: `The tool ${tool.name} wraps ${String(tool.kind)} which does not belong to the ${namespace} domain.` };\n return { allowed: true };\n}\n\n/** The mcp tool dispatch gate: the client must be paired, the session live, the plan approved and the origin inside the session grants; read only tools pass under the dryrun risk class without extra approval while every tool with side effects must name the approved plan step of its own kind it executes. The full canexecute gates re-run at execution time. */\nexport function tooldispatchgate(input: { client: clientrecord; tool: tooldef; session: agentsession | undefined; plan: agentplan | undefined; origin: string; stepid?: string; now: number }): policyevaluation {\n if (input.client.disconnectedat !== undefined) return { allowed: false, reason: \"The mcp client is disconnected and its tool calls are refused.\" };\n if (!input.client.paired) return { allowed: false, reason: \"The mcp client waits for the user pairing approval; unpaired clients never dispatch tools.\" };\n if (!input.session || input.session.stoppedat || input.session.pausedat) return { allowed: false, reason: \"Tool dispatch needs the live browser session behind the consent gates.\" };\n if (input.session.expiresat <= input.now) return { allowed: false, reason: \"The browser session has expired and tool dispatch is refused.\" };\n if (!input.plan || input.plan.state !== \"approved\") return { allowed: false, reason: \"Tool dispatch needs the approved plan review before any tool runs.\" };\n if (!origingranted(input.session, input.origin)) return { allowed: false, reason: `The tool call origin ${input.origin} stays outside the session grants and is refused.` };\n if (input.tool.risk === \"read\") return { allowed: true };\n if (input.stepid === undefined || input.stepid.trim() === \"\") return { allowed: false, reason: `The ${input.tool.name} tool has side effects and needs the id of the approved plan step it executes.` };\n const step = input.plan.steps.find(candidate => candidate.id === input.stepid);\n if (step === undefined) return { allowed: false, reason: `The tool call names the step ${input.stepid} which the approved plan does not carry.` };\n if (step.kind !== input.tool.kind) return { allowed: false, reason: `The tool call names the step ${input.stepid} whose kind ${String(step.kind)} does not match the ${input.tool.name} tool.` };\n return { allowed: true };\n}\n\n/** Grades the consent metadata of every sensitive tool: the risk class must match the policy grading of the wrapped kind, the approval gate requirement must be explicit and the origin scope must stay the session grants. */\nexport function consentmetagrade(tool: tooldef): policyevaluation {\n if (tool.risk === \"read\") return { allowed: true };\n if (tool.consentmeta === undefined) return { allowed: false, reason: `The tool ${tool.name} has side effects and needs its consent metadata.` };\n if (tool.consentmeta.riskclass !== actionrisk(tool.kind)) return { allowed: false, reason: `The consent metadata of ${tool.name} declares the ${String(tool.consentmeta.riskclass)} risk class while policy grades its kind ${String(tool.kind)} as ${actionrisk(tool.kind)}.` };\n if (tool.consentmeta.approvalrequired !== true) return { allowed: false, reason: `The tool ${tool.name} has side effects and its consent metadata must require the explicit approval gate.` };\n if (tool.consentmeta.originscope !== \"session\") return { allowed: false, reason: `The tool ${tool.name} must scope its calls to the session grants.` };\n return { allowed: true };\n}\n\n/** Validates one allowlist entry against the known client identities: the fingerprint must belong to a stored identity, the display name must be non empty and every granted namespace must be a reviewed namespace. */\nexport function allowlistentryvalid(entry: allowlistentry, identities: clientidentity[]): policyevaluation {\n if (typeof entry.fingerprint !== \"string\" || entry.fingerprint.trim() === \"\") return { allowed: false, reason: \"The allowlist entry needs the client fingerprint it grants.\" };\n if (!identities.some(identity => identity.fingerprint === entry.fingerprint)) return { allowed: false, reason: `The allowlist entry ${entry.fingerprint} matches no known client identity.` };\n if (typeof entry.displayname !== \"string\" || entry.displayname.trim() === \"\") return { allowed: false, reason: `The allowlist entry ${entry.fingerprint} needs its display name.` };\n if (!Array.isArray(entry.namespaces) || entry.namespaces.length === 0) return { allowed: false, reason: `The allowlist entry ${entry.displayname} grants no tool namespace.` };\n if (!entry.namespaces.every(namespace => toolnamespaces.includes(namespace))) return { allowed: false, reason: `The allowlist entry ${entry.displayname} grants an unreviewed namespace.` };\n return { allowed: true };\n}\n\n/** Validates one session token lifetime as a user configured value: an absent lifetime keeps the documented default while a configured window must stay positive with no code ceiling. */\nexport function tokenlifetimevalid(lifetime: number | undefined): policyevaluation {\n if (lifetime === undefined) return { allowed: true };\n if (typeof lifetime !== \"number\" || !Number.isFinite(lifetime) || lifetime <= 0) return { allowed: false, reason: \"The token lifetime must stay a positive user value with no code ceiling.\" };\n return { allowed: true };\n}\n\n/** Requires tls for any non localhost transport: a configured remote access policy or a bind outside localhost must carry the on or required tls mode before any remote traffic passes. */\nexport function remotetransporttls(config: mcpserverconfig): policyevaluation {\n const bind = config.bind !== undefined && config.bind.trim() !== \"\" ? config.bind.trim() : \"127.0.0.1\";\n const local = bind === \"127.0.0.1\" || bind === \"localhost\" || bind === \"::1\";\n const tls = config.remoteaccess?.tls ?? config.httpstream?.tls;\n if ((config.remoteaccess !== undefined || !local) && (tls === undefined || tls.mode === \"off\")) return { allowed: false, reason: `The ${config.remoteaccess !== undefined ? \"remote transport\" : `bind ${bind}`} leaves localhost and every non localhost transport requires tls before any remote traffic.` };\n return { allowed: true };\n}\n\n/** Refuses the pairing flow when no session is active: pairing codes issue only while the live browser session exists, so no remote client pairs against a closed surface. */\nexport function pairingreadinessgate(session: agentsession | undefined, now: number): policyevaluation {\n if (!session || session.stoppedat || session.pausedat) return { allowed: false, reason: \"The pairing flow needs the live browser session before any code issues.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and the pairing flow is refused.\" };\n return { allowed: true };\n}\n\n/** Grades the remote transport enablement as a sensitive user choice: a configured remote access policy requires the explicit remote review and tls before the remote surface opens. */\nexport function remoteenablementgate(config: mcpserverconfig): policyevaluation {\n if (config.remoteaccess === undefined) return { allowed: true };\n if (config.remote !== true) return { allowed: false, reason: \"The remote transport enablement is a sensitive user choice and needs the explicit remote review.\" };\n const tls = remotetransporttls(config);\n if (!tls.allowed) return tls;\n if (typeof config.remoteaccess.endpoint !== \"string\" || config.remoteaccess.endpoint.trim() === \"\") return { allowed: false, reason: \"The remote access policy needs its user configured endpoint.\" };\n if (config.remoteaccess.maxclients !== undefined && (typeof config.remoteaccess.maxclients !== \"number\" || !Number.isFinite(config.remoteaccess.maxclients) || config.remoteaccess.maxclients <= 0)) return { allowed: false, reason: \"The user configured client ceiling must stay a positive value with no code ceiling.\" };\n const lifetime = tokenlifetimevalid(config.remoteaccess.tokenlifetimems);\n if (!lifetime.allowed) return lifetime;\n const timeout = approvaltimeoutvalid(config.remoteaccess.approvaltimeout);\n if (!timeout.allowed) return timeout;\n return { allowed: true };\n}\n\n/** Limits the token scopes to the namespaces the user granted: every scope must be a reviewed namespace the grant list carries, so a token never widens beyond the allowlist. */\nexport function tokenscopevalid(scopes: toolnamespace[], granted: toolnamespace[]): policyevaluation {\n if (!Array.isArray(scopes) || scopes.length === 0) return { allowed: false, reason: \"A session token needs at least one granted tool namespace.\" };\n for (const scope of scopes) {\n if (!toolnamespaces.includes(scope)) return { allowed: false, reason: `The scope ${String(scope)} is not a reviewed tool namespace.` };\n if (!granted.includes(scope)) return { allowed: false, reason: `The scope ${scope} stays outside the namespaces the user granted.` };\n }\n return { allowed: true };\n}\n\n/** Validates one approval timeout as a user configured positive window with the documented refusal default; an absent timeout keeps the documented default. */\nexport function approvaltimeoutvalid(timeout: approvaltimeout | undefined): policyevaluation {\n if (timeout === undefined) return { allowed: true };\n if (typeof timeout.windowms !== \"number\" || !Number.isFinite(timeout.windowms) || timeout.windowms <= 0) return { allowed: false, reason: \"The approval timeout must stay a positive user window with no code ceiling.\" };\n if (timeout.ontimeout !== \"refuse\") return { allowed: false, reason: \"The documented disposition of an unanswered approval gate is refusal.\" };\n return { allowed: true };\n}\n\n/** Grades the token revocation as an always available user action: no gate, review or state ever blocks the user from revoking a paired client. */\nexport function revocationgate(): policyevaluation {\n return { allowed: true };\n}\n\n/** Grades one client event subscription as read only when its filters exclude the mutation mirror: a subscription that listens to the callstarted kind must narrow itself with an origin or tool filter so it never streams the side effect calls of unreviewed origins. */\nexport function subscriptiongrade(subscription: protocoleventsubscription): policyevaluation {\n if (!Array.isArray(subscription.kinds) || subscription.kinds.length === 0) return { allowed: false, reason: \"An event subscription needs at least one protocol event kind.\" };\n if (subscription.kinds.includes(\"callstarted\") && subscription.origin === undefined && subscription.tool === undefined) return { allowed: false, reason: \"An event subscription that mirrors the callstarted events of tools with side effects needs its origin or tool filter so it never widens what the session grants.\" };\n return { allowed: true };\n}\n\n/** Grades sampling callbacks as sensitive: the prompt leaves the browser, so page content rides a callback only behind the explicit user grant and the granted maximum tokens stay a positive user value. */\nexport function samplinggrade(input: { request: samplingrequest; pagegrant: boolean }): policyevaluation {\n if (!input.pagegrant && input.request.pagecontent !== undefined) return { allowed: false, reason: \"The sampling callback carries page content the user never granted and is refused.\" };\n if (input.request.maxtokens !== undefined && (!Number.isFinite(input.request.maxtokens) || input.request.maxtokens <= 0)) return { allowed: false, reason: \"The granted maximum tokens of a sampling callback must stay a positive user value with no code ceiling.\" };\n if (input.request.prompt.trim() === \"\") return { allowed: false, reason: \"A sampling callback needs its prompt.\" };\n return { allowed: true };\n}\n\n/** Validates one per client rate limit as a user configured value: the window and budget stay positive when set while an absent limit or budget documents the unbounded choice instead of a silent default. */\nexport function callratelimitvalid(limit: callratelimit | undefined): policyevaluation {\n if (limit === undefined) return { allowed: true };\n if (typeof limit.windowms !== \"number\" || !Number.isFinite(limit.windowms) || limit.windowms <= 0) return { allowed: false, reason: \"The rate limit window must stay a positive user value with no code ceiling.\" };\n if (limit.budget !== undefined && (typeof limit.budget !== \"number\" || !Number.isFinite(limit.budget) || limit.budget <= 0)) return { allowed: false, reason: \"The rate limit budget must stay a positive user value with no code ceiling.\" };\n if (limit.clientid.trim() === \"\") return { allowed: false, reason: \"A per client rate limit needs the client it counts.\" };\n return { allowed: true };\n}\n\n/** Requires one audit entry for every tool call without exception: every call record of the runtime must appear in the audit set so no call ever leaves the trail. */\nexport function callauditcomplete(input: { calls: toolcallrecord[]; audit: toolcallrecord[] }): policyevaluation {\n const audited = new Set(input.audit.map(record => record.id));\n const missing = input.calls.filter(record => !audited.has(record.id));\n if (missing.length > 0) return { allowed: false, reason: `${missing.length} tool call${missing.length === 1 ? \"\" : \"s\"} carry no audit entry and the audit trail must name every call without exception.` };\n return { allowed: true };\n}\n\n/** Grades one batch call by its most sensitive member: a batch that carries a sensitive member takes the sensitive grade and runs only behind the approval gates while a read only batch stays read. */\nexport function batchgrade(input: { calls: Array<{ risk: tooldef[\"risk\"] }>; approved: boolean }): policyevaluation {\n if (input.calls.length === 0) return { allowed: false, reason: \"A batch call needs at least one ordered tool call.\" };\n const sensitive = input.calls.some(call => call.risk === \"sensitive\");\n if (sensitive && !input.approved) return { allowed: false, reason: \"The batch grades sensitive through its most sensitive member and runs only behind the approval gates.\" };\n return { allowed: true };\n}\n\n/** Keeps one tool dry run free of page mutations: a dry run record that claims execution or lists page mutations is refused because a dry run evaluates arguments and consent and never executes anything. */\nexport function dryrunpurity(dryrun: tooldryrun): policyevaluation {\n if (dryrun.executed) return { allowed: false, reason: `The ${dryrun.tool} dry run claims execution and a dry run never executes anything.` };\n if (dryrun.mutations.length > 0) return { allowed: false, reason: `The ${dryrun.tool} dry run lists ${dryrun.mutations.length} page mutation${dryrun.mutations.length === 1 ? \"\" : \"s\"} and a dry run leaves the page untouched.` };\n return { allowed: true };\n}\n\n/** Validates tool mock usage to test contexts only: a mock outside a test context is refused while a test context mock must name a tool and carry a canned result. */\nexport function mockusagevalid(mock: toolmock): policyevaluation {\n if (mock.testcontext !== true) return { allowed: false, reason: `The ${mock.tool} mock stays outside a test context and is refused; tool mocks never answer real calls.` };\n if (mock.tool.trim() === \"\") return { allowed: false, reason: \"A tool mock needs the namespaced tool it stands in for.\" };\n if (typeof mock.result.content !== \"string\") return { allowed: false, reason: \"A tool mock needs its canned result content.\" };\n return { allowed: true };\n}\n\n/**\n * Llm integration gates of the 1.1.57 family.\n * Every model side gate lives here: the provider validation that keeps every endpoint, model and protocol shape a user configured value, the data egress grading of provider calls, the explicit consent requirement before page content leaves the browser, the local endpoint preference for sensitive extractions, the review requirement of model drafted plans, the fresh review requirement of replanned steps, the cost budget validation, the guard verdict gate that refuses invalid model output and the plan lint that checks model drafts against the action grammar before review.\n * No provider, endpoint, model, key or ceiling is ever hardcoded: the gates validate user choices and refuse everything else.\n */\n\n/** Validates one provider config: the endpoint stays a user configured http or https url, the model list stays non-empty free text, the protocol shape stays one of the four wire shapes and the auth reference stays a storage id reference that never carries key material. */\nexport function providervalid(config: providerconfig): policyevaluation {\n if (config.name.trim() === \"\") return { allowed: false, reason: \"The provider config needs its name.\" };\n if (config.endpoint.trim() === \"\") return { allowed: false, reason: \"The provider config needs the user configured endpoint url; no default endpoint ever applies.\" };\n let parsed: URL;\n try { parsed = new URL(config.endpoint); } catch { return { allowed: false, reason: \"The provider endpoint must be a well-formed url.\" }; }\n if (parsed.protocol !== \"https:\" && parsed.protocol !== \"http:\") return { allowed: false, reason: \"The provider endpoint must speak http or https.\" };\n if (config.models.length === 0) return { allowed: false, reason: \"The provider config needs at least one user configured model name.\" };\n if (config.models.some(model => model.trim() === \"\")) return { allowed: false, reason: \"Every provider model name must stay non-empty free text.\" };\n if (config.style !== \"chatcompletions\" && config.style !== \"responses\" && config.style !== \"messages\" && config.style !== \"gemini\") return { allowed: false, reason: \"The provider protocol shape must be one of the four wire shapes the user picks.\" };\n if (config.authref !== undefined && config.authref.storageid.trim() === \"\") return { allowed: false, reason: \"The provider auth reference needs the storage id of the stored key; the key material never enters the config.\" };\n return { allowed: true };\n}\n\n/** Grades one provider call as a data egress event for the audit trail: every remote completion leaves the browser with its prompt text, so the audit names the endpoint, the model and the token counts; a local endpoint grades as the local preference. */\nexport function provideregressgrade(input: { provider: providerconfig; local: boolean }): policyevaluation {\n const valid = providervalid(input.provider);\n if (!valid.allowed) return valid;\n return { allowed: true, reason: input.local ? \"The model call stays on the local machine endpoint and grades as the local data egress preference.\" : \"The model call leaves the browser for the user configured endpoint and grades as a data egress event with its endpoint, model and token counts in the audit trail.\" };\n}\n\n/** Requires explicit consent before any page content leaves the browser: page content inside a call the user has not granted refuses the call, while calls without page content pass untouched. */\nexport function egressconsentgate(input: { pagecontent?: string; granted: boolean }): policyevaluation {\n if (input.pagecontent !== undefined && input.pagecontent.trim() !== \"\" && input.granted !== true) return { allowed: false, reason: \"The model call carries page content the user has not granted, so the content stays in the browser and the call refuses.\" };\n return { allowed: true };\n}\n\n/** Grades the local endpoint preference for sensitive extractions: a sensitive extraction prefers the local model endpoint, and the grade names the preference while a local endpoint satisfies it. */\nexport function localsensitivegrade(input: { sensitive: boolean; local: boolean }): policyevaluation {\n if (input.sensitive && !input.local) return { allowed: true, reason: \"The sensitive extraction prefers the local model endpoint; the user keeps the choice of the remote provider.\" };\n return { allowed: true, reason: input.local ? \"The local model endpoint satisfies the sensitive extraction preference.\" : \"The extraction stays non-sensitive and every configured endpoint serves it.\" };\n}\n\n/** Requires the plan review before any model drafted plan executes: only an approved draft may turn into a plan, and the plan itself still passes the same human plan review every local plan passes. */\nexport function plandraftreviewgate(draft: plandraft): policyevaluation {\n if (draft.state !== \"approved\") return { allowed: false, reason: \"The model drafted plan stays unreviewed; the human review approves the draft before any step executes.\" };\n if (draft.steps.length === 0) return { allowed: false, reason: \"The model drafted plan carries no step, so nothing executes.\" };\n return { allowed: true };\n}\n\n/** Requires fresh review for replanned steps: a pending replan never executes and every revised tail step carries the fresh review marker, so the human review sees the changed tail before it runs. */\nexport function replanreviewgate(replan: replanrecord): policyevaluation {\n if (replan.state !== \"approved\") return { allowed: false, reason: \"The replanned tail stays unreviewed; the fresh review approves the changed steps before any of them executes.\" };\n if (replan.tail.some(step => step.freshreview !== true)) return { allowed: false, reason: \"Every revised step of a replan must carry the fresh review marker.\" };\n return { allowed: true };\n}\n\n/** Validates one cost budget: the token and currency ceilings stay positive user values with no code ceiling, the currency names the unit of the cost ceiling and a budget without any ceiling documents the unbounded choice instead of inventing one. */\nexport function costbudgetvalid(budget: costbudget): policyevaluation {\n if (budget.maxtokens !== undefined && (!Number.isFinite(budget.maxtokens) || budget.maxtokens <= 0)) return { allowed: false, reason: \"The token ceiling of a cost budget must stay a positive user value.\" };\n if (budget.maxcost !== undefined && (!Number.isFinite(budget.maxcost) || budget.maxcost <= 0)) return { allowed: false, reason: \"The cost ceiling of a cost budget must stay a positive user value.\" };\n if (budget.maxcost !== undefined && (budget.currency === undefined || budget.currency.trim() === \"\")) return { allowed: false, reason: \"The cost ceiling of a cost budget needs its currency unit.\" };\n if (budget.maxtokens === undefined && budget.maxcost === undefined) return { allowed: false, reason: \"The cost budget needs at least one ceiling the user configured; an absent budget stays the documented unbounded choice.\" };\n return { allowed: true };\n}\n\n/** Refuses tool calls the guardrails marked invalid: only a valid guard verdict passes, an invalid or refused model output never executes. */\nexport function guardverdictgate(output: modeloutput): policyevaluation {\n if (output.verdict === \"invalid\") return { allowed: false, reason: output.reason ?? \"The guardrails marked the model output invalid.\" };\n if (output.verdict === \"refused\") return { allowed: false, reason: output.reason ?? \"The model refused the request, so nothing executes.\" };\n return { allowed: true };\n}\n\n/** Lints one model drafted plan against the action grammar before review: every drafted step maps onto the tool step grammar with its derived risk, and every violation lands in the findings the review sees; an empty origin skips the origin bound checks the way the review pipeline runs them later. */\n/** Derives the risk of one drafted step for the grammar check; an unknown kind grades sensitive so the grammar check names the violation instead of crashing. */\nfunction draftriskof(step: draftstep): \"read\" | \"interaction\" | \"sensitive\" {\n try { return resolvedrisk({ id: step.id, kind: step.kind as actionkind, ...(step.target !== undefined ? { target: step.target } : {}), ...(step.value !== undefined ? { value: step.value } : {}), summary: step.summary, risk: \"sensitive\" }); } catch { return \"sensitive\"; }\n}\n\nexport function planlint(draft: plandraft, origin: string): string[] {\n const findings: string[] = [];\n if (draft.goal.trim() === \"\") findings.push(\"The drafted plan carries no goal.\");\n if (draft.steps.length === 0) findings.push(\"The drafted plan carries no step.\");\n for (const step of draft.steps) {\n const mapped: toolstep = { id: step.id, kind: step.kind as actionkind, ...(step.target !== undefined ? { target: step.target } : {}), ...(step.value !== undefined ? { value: step.value } : {}), summary: step.summary, risk: draftriskof(step) } as toolstep;\n const verdict = validatestep(mapped, origin);\n if (!verdict.allowed) findings.push(`The drafted step ${step.id || \"without id\"} of kind ${step.kind || \"unknown\"} violates the action grammar: ${verdict.reason ?? \"the step failed its grammar check.\"}`);\n }\n return findings;\n}\n\n/**\n * Multi agent part one gates of the 1.1.58 family.\n * Every swarm side gate lives here: the task queue validation of the user configured lanes, priorities and completion policy, the work stealing grade that permits stealing only inside one user approved swarm, the per agent budget validation of positive user ceilings, the per agent scope validation against the session grant list, the spawn grading with the risk class of the requested role, the killswitch gate that stays available with no configuration barrier, the egress grading of cross agent messages that carry page content and the blackboard consent grade that inherits the class of the source extraction.\n * No agent count, lane name, priority scale, depth ceiling or freshness window is ever hardcoded: the gates validate user choices and refuse everything else, and no swarm coordination ever bypasses the human review.\n */\n\n/** Validates one task queue: the lanes stay non-empty unique user names, the priorities stay finite user values, the completion policy stays all or any, and every item waits in a configured lane under a configured priority when the user configured the scales. */\nexport function queuelanesvalid(queue: taskqueue): policyevaluation {\n if (queue.lanes.some(lane => lane.trim() === \"\")) return { allowed: false, reason: \"Every queue lane needs its user configured name.\" };\n if (new Set(queue.lanes).size !== queue.lanes.length) return { allowed: false, reason: \"The queue lane names must stay unique.\" };\n if (queue.priorities.some(priority => !Number.isFinite(priority))) return { allowed: false, reason: \"Every queue priority must stay a finite user value.\" };\n if (queue.completionpolicy !== \"all\" && queue.completionpolicy !== \"any\") return { allowed: false, reason: \"The queue completion policy must stay all or any.\" };\n for (const item of queue.items) {\n if (item.payload.trim() === \"\") return { allowed: false, reason: `The task ${item.id} carries no payload.` };\n if (queue.lanes.length > 0 && !queue.lanes.includes(item.lane)) return { allowed: false, reason: `The task ${item.id} waits in the lane ${item.lane} which the user did not configure.` };\n if (queue.priorities.length > 0 && !queue.priorities.includes(item.priority)) return { allowed: false, reason: `The task ${item.id} carries the priority ${item.priority} which the user did not configure.` };\n }\n return { allowed: true };\n}\n\n/** Grades one work stealing attempt: stealing is permitted only inside one user approved swarm, and a steal outside an approved swarm refuses because lane ownership only exists inside the reviewed swarm. */\nexport function workstealgrade(input: { swarmapproved: boolean; agentrole: string; lane: string; ownership?: Array<{ lane: string; roles: string[] }> }): policyevaluation {\n if (!input.swarmapproved) return { allowed: false, reason: \"Work stealing runs only inside one user approved swarm; an unapproved swarm keeps every lane closed.\" };\n const rule = (input.ownership ?? []).find(entry => entry.lane === input.lane);\n if (rule && !rule.roles.includes(input.agentrole)) return { allowed: false, reason: `The lane ${input.lane} only opens its tasks to the roles ${rule.roles.join(\", \")} the user configured; the ${input.agentrole} agent may not steal.` };\n return { allowed: true, reason: rule === undefined ? `The lane ${input.lane} carries no ownership rule, so every agent of the approved swarm may steal its tasks.` : `The lane ${input.lane} opens its tasks to the ${input.agentrole} role the user configured.` };\n}\n\n/** Validates one per agent budget: the token, cost and step ceilings stay positive user values with no code ceiling, the cost ceiling names its currency and a budget without any ceiling documents the unbounded choice instead of inventing one. */\nexport function agentbudgetvalid(budget: agentbudget): policyevaluation {\n if (budget.agentid.trim() === \"\") return { allowed: false, reason: \"The agent budget needs its agent id.\" };\n if (budget.maxtokens !== undefined && (!Number.isFinite(budget.maxtokens) || budget.maxtokens <= 0)) return { allowed: false, reason: \"The token ceiling of an agent budget must stay a positive user value.\" };\n if (budget.maxcost !== undefined && (!Number.isFinite(budget.maxcost) || budget.maxcost <= 0)) return { allowed: false, reason: \"The cost ceiling of an agent budget must stay a positive user value.\" };\n if (budget.maxsteps !== undefined && (!Number.isFinite(budget.maxsteps) || budget.maxsteps <= 0)) return { allowed: false, reason: \"The step ceiling of an agent budget must stay a positive user value.\" };\n if (budget.maxcost !== undefined && (budget.currency === undefined || budget.currency.trim() === \"\")) return { allowed: false, reason: \"The cost ceiling of an agent budget needs its currency unit.\" };\n if (budget.maxtokens === undefined && budget.maxcost === undefined && budget.maxsteps === undefined) return { allowed: false, reason: \"The agent budget needs at least one ceiling the user configured; an absent budget stays the documented unbounded choice.\" };\n return { allowed: true };\n}\n\n/** Validates one per agent scope against the session grant list: every granted origin must sit inside the session grants and every granted tool namespace must be one of the catalog namespaces, so no agent scope ever widens the session. */\nexport function agentscopevalid(input: { scope: agentscope; grants: string[] }): policyevaluation {\n if (input.scope.agentid.trim() === \"\") return { allowed: false, reason: \"The agent scope needs its agent id.\" };\n if (input.scope.origins.some(origin => origin.trim() === \"\")) return { allowed: false, reason: \"Every origin of an agent scope needs its user configured name.\" };\n if (input.scope.toolnamespaces.some(namespace => !toolnamespaces.includes(namespace))) return { allowed: false, reason: \"Every tool namespace of an agent scope must be one of the catalog namespaces.\" };\n const outside = input.scope.origins.filter(origin => input.grants.length > 0 && !input.grants.includes(origin));\n if (outside.length > 0) return { allowed: false, reason: `The agent scope grants the origins ${outside.join(\", \")} which the session grant list does not carry; no agent scope widens the session.` };\n return { allowed: true };\n}\n\n/** Grades one spawn request with the risk class of the requested role: a planner or observer spawn grades read side while a worker or custom role spawn grades sensitive because it may execute reviewed steps, and the grade names the class the review sees. */\nexport function spawngrade(request: spawnrequest): policyevaluation {\n if (request.parentid.trim() === \"\") return { allowed: false, reason: \"The spawn request needs its parent agent id.\" };\n if (request.task.trim() === \"\") return { allowed: false, reason: \"The spawn request needs its task in plain language.\" };\n if (request.depth < 1) return { allowed: false, reason: \"The spawn request depth must sit at one or deeper because every sub agent lives under a parent.\" };\n const risk = request.role === \"planner\" || request.role === \"observer\" ? \"read\" : \"sensitive\";\n return { allowed: true, reason: risk === \"read\" ? `The spawn of the ${request.role} sub agent grades read side: the role composes or observes and never executes page actions.` : `The spawn of the ${request.role} sub agent grades sensitive: the role may execute reviewed steps, so every proposal it drafts still passes the same human review.` };\n}\n\n/** Keeps the killswitch available with no configuration barrier: the switch never needs a setting, an approval or a state to fire, so the user halts every agent at once at any time. */\nexport function killswitchgate(): policyevaluation {\n return { allowed: true, reason: \"The killswitch stays available with no configuration barrier: the user halts every agent of the swarm at once at any time.\" };\n}\n\n/** Grades one cross agent message that carries page content as a data egress event: the delivery stays inside the swarm while the copied page content lands its egress class in the audit trail so the reviewer reads what moved between agents. */\nexport function messageegressgrade(input: { message: agentmessage; carriespagecontent: boolean }): policyevaluation {\n if (input.message.payload.trim() === \"\") return { allowed: false, reason: \"The agent message needs its payload.\" };\n if (input.carriespagecontent) return { allowed: true, reason: `The message ${input.message.id} from ${input.message.senderid} to ${input.message.recipient} carries page content and grades as a data egress event with its sender, recipient and routing in the audit trail.` };\n return { allowed: true, reason: `The message ${input.message.id} from ${input.message.senderid} to ${input.message.recipient} carries no page content and stays a plain swarm delivery.` };\n}\n\n/** Grades one blackboard entry by the consent class of its source extraction: the entry inherits the class exactly, a sensitive extraction stays sensitive on the board and every reader sees the class beside the value. */\nexport function blackboardconsentgrade(entry: blackboardentry): policyevaluation {\n if (entry.key.trim() === \"\") return { allowed: false, reason: \"The blackboard entry needs its key.\" };\n if (entry.consentclass !== \"read\" && entry.consentclass !== \"interaction\" && entry.consentclass !== \"sensitive\") return { allowed: false, reason: \"The blackboard entry inherits one of the three consent classes of its source extraction.\" };\n if (entry.consentclass === \"sensitive\") return { allowed: true, reason: `The blackboard entry ${entry.key} inherits the sensitive class of its source extraction; every agent reads the class beside the value.` };\n return { allowed: true, reason: `The blackboard entry ${entry.key} inherits the ${entry.consentclass} class of its source extraction; every agent reads the class beside the value.` };\n}\n", "import type { toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\n\n/**\n * Mutating page interactions for reviewed steps.\n * Every correlated rule for pointer, key, drag, upload, form, attribute, storage and evaluation actions lives in this file.\n */\n\nexport type stepresult = { ok: boolean; summary: string; details?: Record<string, unknown> };\n\nfunction events(target: Element): void {\n target.dispatchEvent(new Event(\"input\", { bubbles: true }));\n target.dispatchEvent(new Event(\"change\", { bubbles: true }));\n}\n\nfunction modifiers(options: Record<string, unknown>): string[] {\n return Array.isArray(options.modifiers) ? options.modifiers.filter((item): item is string => typeof item === \"string\") : [];\n}\n\nfunction keyevent(type: \"keydown\" | \"keypress\" | \"keyup\", key: string, mods: string[]): KeyboardEvent {\n const code = key.length === 1 ? `Key${key.toUpperCase()}` : key;\n return new KeyboardEvent(type, { key, code, bubbles: true, cancelable: true, composed: true, ctrlKey: mods.includes(\"ctrl\"), shiftKey: mods.includes(\"shift\"), altKey: mods.includes(\"alt\"), metaKey: mods.includes(\"meta\") });\n}\n\nfunction fieldlike(target: Element | null): HTMLInputElement | HTMLTextAreaElement | null {\n return target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement ? target : null;\n}\n\nfunction stringify(value: unknown): unknown {\n try { return JSON.parse(JSON.stringify(value)) ?? null; } catch { return String(value); }\n}\n\n/** Runs one mutating page action after the background policy gate and a fresh target check. */\nexport function runpageaction(step: toolstep, target: Element | null): stepresult | Promise<stepresult> {\n const options = (() => { try { return parseoptions(step); } catch { return {} as Record<string, unknown>; } })();\n switch (step.kind) {\n case \"presskey\": {\n const receiver = target instanceof HTMLElement ? target : document.activeElement instanceof HTMLElement ? document.activeElement : document.body;\n const key = step.value ?? \"\";\n const mods = modifiers(options);\n receiver.dispatchEvent(keyevent(\"keydown\", key, mods));\n receiver.dispatchEvent(keyevent(\"keypress\", key, mods));\n receiver.dispatchEvent(keyevent(\"keyup\", key, mods));\n return { ok: true, summary: `Key ${key} delivered with ${mods.length} modifier${mods.length === 1 ? \"\" : \"s\"}.` };\n }\n case \"clickdeep\": {\n if (!(target instanceof HTMLElement)) return { ok: false, summary: \"Action target is no longer available.\" };\n target.dispatchEvent(new PointerEvent(\"pointerdown\", { bubbles: true, cancelable: true, composed: true }));\n target.dispatchEvent(new MouseEvent(\"mousedown\", { bubbles: true, cancelable: true }));\n target.dispatchEvent(new PointerEvent(\"pointerup\", { bubbles: true, cancelable: true, composed: true }));\n target.dispatchEvent(new MouseEvent(\"mouseup\", { bubbles: true, cancelable: true }));\n target.click();\n return { ok: true, summary: \"Full pointer click sequence delivered.\" };\n }\n case \"rightclick\": {\n if (!(target instanceof HTMLElement)) return { ok: false, summary: \"Action target is no longer available.\" };\n const init: MouseEventInit = { bubbles: true, cancelable: true, button: 2, buttons: 2 };\n target.dispatchEvent(new PointerEvent(\"pointerdown\", { ...init, composed: true }));\n target.dispatchEvent(new MouseEvent(\"mousedown\", init));\n target.dispatchEvent(new MouseEvent(\"contextmenu\", init));\n return { ok: true, summary: \"Context menu events delivered.\" };\n }\n case \"doubleclick\": {\n if (!(target instanceof HTMLElement)) return { ok: false, summary: \"Action target is no longer available.\" };\n target.click();\n target.click();\n target.dispatchEvent(new MouseEvent(\"dblclick\", { bubbles: true, cancelable: true, detail: 2 }));\n return { ok: true, summary: \"Double click sequence delivered.\" };\n }\n case \"drag\": {\n if (!(target instanceof HTMLElement)) return { ok: false, summary: \"Drag source is no longer available.\" };\n const destination = document.querySelector(step.value ?? \"\");\n if (!destination) return { ok: false, summary: \"Drag destination is no longer available.\" };\n const transfer = new DataTransfer();\n if (typeof options.data === \"string\") transfer.setData(\"text/plain\", options.data);\n target.dispatchEvent(new DragEvent(\"dragstart\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n destination.dispatchEvent(new DragEvent(\"dragenter\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n destination.dispatchEvent(new DragEvent(\"dragover\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n destination.dispatchEvent(new DragEvent(\"drop\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n target.dispatchEvent(new DragEvent(\"dragend\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n return { ok: true, summary: \"Drag and drop sequence delivered.\" };\n }\n case \"drop\": {\n if (!(target instanceof HTMLElement)) return { ok: false, summary: \"Drop zone is no longer available.\" };\n const transfer = new DataTransfer();\n transfer.setData(\"text/plain\", step.value ?? \"\");\n target.dispatchEvent(new DragEvent(\"dragenter\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n target.dispatchEvent(new DragEvent(\"dragover\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n target.dispatchEvent(new DragEvent(\"drop\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n return { ok: true, summary: \"Drop payload delivered.\" };\n }\n case \"upload\": {\n if (!(target instanceof HTMLInputElement) || target.type !== \"file\") return { ok: false, summary: \"Target is not a file input.\" };\n const transfer = new DataTransfer();\n transfer.items.add(new File([typeof options.content === \"string\" ? options.content : \"\"], step.value ?? \"upload\", { type: typeof options.type === \"string\" ? options.type : \"text/plain\" }));\n target.files = transfer.files;\n events(target);\n return { ok: true, summary: `Uploaded ${step.value ?? \"file\"} into the reviewed input.` };\n }\n case \"clear\": {\n const field = fieldlike(target);\n if (!field) return { ok: false, summary: \"Target cannot hold a value.\" };\n field.value = \"\";\n events(field);\n return { ok: true, summary: \"Field cleared.\" };\n }\n case \"check\":\n case \"uncheck\":\n case \"toggle\": {\n if (!(target instanceof HTMLInputElement) || (target.type !== \"checkbox\" && target.type !== \"radio\")) return { ok: false, summary: \"Target is not a checkbox or radio control.\" };\n if (step.kind === \"uncheck\" && target.type === \"radio\") return { ok: false, summary: \"A radio control cannot be unchecked.\" };\n target.checked = step.kind === \"toggle\" ? !target.checked : step.kind === \"check\";\n events(target);\n return { ok: true, summary: `Control is now ${target.checked ? \"checked\" : \"unchecked\"}.` };\n }\n case \"submit\": {\n const form = target instanceof HTMLFormElement ? target : target instanceof HTMLElement ? target.closest(\"form\") : null;\n if (!form) return { ok: false, summary: \"No form owns the reviewed target.\" };\n try { form.requestSubmit(target instanceof HTMLFormElement ? undefined : (target as HTMLElement)); } catch { form.submit(); }\n return { ok: true, summary: \"Form submission requested.\" };\n }\n case \"setattribute\": {\n if (!target) return { ok: false, summary: \"Action target is no longer available.\" };\n const name = typeof options.name === \"string\" ? options.name : \"\";\n target.setAttribute(name, typeof options.value === \"string\" ? options.value : \"\");\n return { ok: true, summary: `Attribute ${name} set.` };\n }\n case \"removeattribute\": {\n if (!target) return { ok: false, summary: \"Action target is no longer available.\" };\n const name = step.value ?? \"\";\n target.removeAttribute(name);\n return { ok: true, summary: `Attribute ${name} removed.` };\n }\n case \"writestorage\": {\n try {\n localStorage.setItem(typeof options.key === \"string\" ? options.key : \"\", typeof options.value === \"string\" ? options.value : \"\");\n return { ok: true, summary: `Local storage entry ${String(options.key)} written.` };\n } catch (error) { return { ok: false, summary: `Local storage refused the write: ${error instanceof Error ? error.message : String(error)}` }; }\n }\n case \"evaluate\": {\n try {\n let outcome: unknown;\n try { outcome = new Function(`\"use strict\"; return (${step.value ?? \"undefined\"});`)(); } catch { outcome = new Function(`\"use strict\"; ${step.value ?? \"\"}`)(); }\n return { ok: true, summary: `Reviewed expression returned ${outcome === undefined ? \"no value\" : \"a value\"}.`, details: { result: stringify(outcome) } };\n } catch (error) { return { ok: false, summary: `Reviewed expression failed: ${error instanceof Error ? error.message : String(error)}` }; }\n }\n case \"fullscreen\": {\n const element = target instanceof HTMLElement ? target : document.documentElement;\n return document.fullscreenElement === element\n ? document.exitFullscreen().then(() => ({ ok: true, summary: \"Fullscreen state cleared.\" }) as stepresult)\n : element.requestFullscreen().then(() => ({ ok: true, summary: \"Fullscreen state entered.\" }) as stepresult).catch(error => ({ ok: false, summary: `Fullscreen was refused: ${error instanceof Error ? error.message : String(error)}` }));\n }\n default: return { ok: false, summary: \"Unsupported page action.\" };\n }\n}\n", "/**\n * Xpath resolution subset for reviewed steps.\n * Every correlated rule for expression parsing, node trees and evaluation of the supported xpath grammar lives in this file.\n * The engine supports absolute and descendant steps, tag or wildcard names, attribute predicates, text predicates and positional predicates.\n */\n\n/** Serializable dom node used by the xpath engine; the live glue attaches the element. */\nexport interface xnode {\n tag: string;\n attributes: Record<string, string>;\n text: string;\n children: xnode[];\n element?: Element;\n}\n\ntype xpathpredicate =\n | { kind: \"attr\"; name: string; value?: string; contains?: boolean }\n | { kind: \"text\"; value: string; contains?: boolean }\n | { kind: \"position\"; index: number };\n\ninterface xpathstep {\n descendant: boolean;\n tag: string;\n predicates: xpathpredicate[];\n}\n\nfunction owntext(element: Element): string {\n let combined = \"\";\n for (const node of element.childNodes) if (node.nodeType === Node.TEXT_NODE) combined += node.textContent ?? \"\";\n return combined.replace(/\\s+/g, \" \").trim();\n}\n\nfunction attributesof(element: Element): Record<string, string> {\n const attributes: Record<string, string> = {};\n for (const attribute of [...element.attributes]) attributes[attribute.name] = attribute.value;\n return attributes;\n}\n\nfunction wrap(element: Element): xnode {\n return { tag: element.tagName.toLowerCase(), attributes: attributesof(element), text: owntext(element), children: [...element.children].map(wrap), element };\n}\n\n/** Builds the serializable node tree of one live document for the xpath engine. */\nexport function buildxtree(root: Document): xnode {\n return { tag: \"#document\", attributes: {}, text: \"\", children: root.documentElement ? [wrap(root.documentElement)] : [] };\n}\n\n/** Parses one predicate body into the supported predicate shapes. */\nfunction parsepredicate(raw: string): xpathpredicate | null {\n const body = raw.trim();\n let match = /^@([\\w-]+)$/.exec(body);\n if (match) return { kind: \"attr\", name: match[1] as string };\n match = /^@([\\w-]+)\\s*=\\s*['\"]([^'\"]*)['\"]$/.exec(body);\n if (match) return { kind: \"attr\", name: match[1] as string, value: match[2] as string };\n match = /^contains\\(\\s*@([\\w-]+)\\s*,\\s*['\"]([^'\"]*)['\"]\\s*\\)$/.exec(body);\n if (match) return { kind: \"attr\", name: match[1] as string, value: match[2] as string, contains: true };\n match = /^text\\(\\)\\s*=\\s*['\"]([^'\"]*)['\"]$/.exec(body);\n if (match) return { kind: \"text\", value: match[1] as string };\n match = /^contains\\(\\s*text\\(\\)\\s*,\\s*['\"]([^'\"]*)['\"]\\s*\\)$/.exec(body);\n if (match) return { kind: \"text\", value: match[1] as string, contains: true };\n match = /^(\\d+)$/.exec(body);\n if (match) return { kind: \"position\", index: Number.parseInt(match[1] as string, 10) };\n return null;\n}\n\n/** Parses the reviewed xpath expression into evaluation steps; unsupported syntax is refused. */\nexport function parsexpath(expression: string): xpathstep[] {\n const trimmed = expression.trim();\n if (!trimmed.startsWith(\"/\")) throw new Error(\"The reviewed xpath expression must start with a slash.\");\n const steps: xpathstep[] = [];\n let index = 0;\n while (index < trimmed.length) {\n if (trimmed[index] !== \"/\") throw new Error(\"The reviewed xpath expression contains an unsupported segment.\");\n let slashes = 0;\n while (index < trimmed.length && trimmed[index] === \"/\") { slashes += 1; index += 1; }\n const start = index;\n let quote = \"\";\n while (index < trimmed.length) {\n const character = trimmed[index];\n if (quote) { if (character === quote) quote = \"\"; }\n else if (character === \"'\" || character === '\"') quote = character;\n else if (character === \"/\") break;\n index += 1;\n }\n const body = trimmed.slice(start, index);\n if (!body) throw new Error(\"The reviewed xpath expression contains an empty step.\");\n const parsed = /^(\\*|[a-zA-Z][\\w-]*)((?:\\[[^\\]]*\\])*)$/.exec(body);\n if (!parsed) throw new Error(`The reviewed xpath step ${body} is not supported.`);\n const predicates: xpathpredicate[] = [];\n const pattern = /\\[([^\\]]*)\\]/g;\n let predicate: RegExpExecArray | null;\n while ((predicate = pattern.exec(parsed[2] ?? \"\")) !== null) {\n const parsedpredicate = parsepredicate(predicate[1] as string);\n if (!parsedpredicate) throw new Error(`The reviewed xpath predicate [${predicate[1]}] is not supported.`);\n predicates.push(parsedpredicate);\n }\n steps.push({ descendant: slashes > 1, tag: (parsed[1] as string).toLowerCase(), predicates });\n }\n return steps;\n}\n\nfunction descendants(node: xnode, includeself: boolean): xnode[] {\n const result: xnode[] = includeself ? [node] : [];\n for (const child of node.children) { result.push(child); result.push(...descendants(child, false)); }\n return result;\n}\n\nfunction applypredicates(nodes: xnode[], predicates: xpathpredicate[]): xnode[] {\n let result = nodes;\n for (const predicate of predicates) {\n if (predicate.kind === \"position\") {\n const entry = result[predicate.index - 1];\n result = entry ? [entry] : [];\n continue;\n }\n result = result.filter(node => {\n if (predicate.kind === \"attr\") {\n const value = node.attributes[predicate.name];\n if (value === undefined) return false;\n if (predicate.value === undefined) return true;\n return predicate.contains ? value.includes(predicate.value) : value === predicate.value;\n }\n return predicate.contains ? node.text.includes(predicate.value) : node.text === predicate.value;\n });\n }\n return result;\n}\n\n/** Evaluates the supported xpath subset against a serializable node tree and returns every matched node in document order. */\nexport function evaluatexpath(root: xnode, expression: string): xnode[] {\n const steps = parsexpath(expression);\n let current: xnode[] = [root];\n let first = true;\n for (const step of steps) {\n let matched: xnode[] = [];\n for (const node of current) {\n const pool = step.descendant ? descendants(node, first) : node.children;\n matched = matched.concat(pool.filter(candidate => candidate.tag === step.tag || step.tag === \"*\"));\n }\n current = applypredicates(matched, step.predicates);\n first = false;\n }\n return current;\n}\n", "import { parseoptions, resolutionverdict } from \"../policy.js\";\nimport type { clickablemap, mapentry, resolvedtarget, targetmode, toolstep } from \"../types.js\";\nimport { buildxtree, evaluatexpath } from \"./pagexpath.js\";\n\n/**\n * Target resolution engine for reviewed steps.\n * Every correlated rule for element summaries, targetref modes, ambiguity reports, shadow piercing, frame walking and the numbered clickable map lives in this file.\n */\n\n/** Descriptive fields shared by every resolution candidate; the live dom glue attaches the element. */\nexport interface candidatefields {\n tag: string;\n id: string;\n role: string;\n name: string;\n label: string;\n text: string;\n selector: string;\n}\n\n/** One live dom candidate: descriptive fields plus the element itself. */\nexport interface livecandidate extends candidatefields {\n element: Element;\n}\n\n/** Serializable description of one document with nested frames; the glue attaches the live document. */\nexport interface framedescription {\n frames: frameentry[];\n live?: Document;\n}\n\n/** One nested frame: same origin frames expose their document, cross origin frames do not. */\nexport interface frameentry {\n sameorigin: boolean;\n document?: framedescription;\n}\n\n/** Serializable scope tree: one scope's candidates plus the open shadow scopes nested inside it. */\nexport interface scopetree<T extends candidatefields = livecandidate> {\n host?: T;\n candidates: T[];\n shadows: scopetree<T>[];\n}\n\nexport function clean(value: string): string {\n return value.replace(/\\s+/g, \" \").trim();\n}\n\nfunction cssescape(value: string): string {\n return typeof CSS !== \"undefined\" && typeof CSS.escape === \"function\" ? CSS.escape(value) : value.replace(/[^a-zA-Z0-9_-]/g, \"\\\\$&\");\n}\n\n/** Computes the accessible style label of an element from aria hints, linked labels, title and content. */\nexport function elementlabel(element: Element): string {\n const aria = element.getAttribute(\"aria-label\");\n let linked = \"\";\n const labelledby = element.getAttribute(\"aria-labelledby\");\n if (labelledby) {\n try {\n const owner = element.ownerDocument?.getElementById(labelledby);\n if (owner) linked = owner.textContent ?? \"\";\n } catch { /* detached documents refuse lookups; the label falls back */ }\n }\n let forlabel = \"\";\n if (element.id) {\n try {\n const label = element.ownerDocument?.querySelector(`label[for=\"${cssescape(element.id)}\"]`);\n if (label instanceof HTMLElement) forlabel = label.textContent ?? \"\";\n } catch { /* the document may be detached; the label falls back */ }\n }\n return clean(aria || linked || forlabel || element.getAttribute(\"title\") || element.textContent || \"\");\n}\n\n/** Computes the implicit aria role of common elements when no explicit role attribute exists. */\nexport function implicitrole(element: Element): string {\n const tag = element.tagName.toLowerCase();\n if (tag === \"button\") return \"button\";\n if (tag === \"a\" && element.getAttribute(\"href\")) return \"link\";\n if (tag === \"select\") return \"combobox\";\n if (tag === \"textarea\") return \"textbox\";\n if (tag === \"details\") return \"group\";\n if (tag === \"input\") {\n const type = element.getAttribute(\"type\") ?? \"text\";\n if (type === \"checkbox\") return \"checkbox\";\n if (type === \"radio\") return \"radio\";\n if (type === \"button\" || type === \"submit\" || type === \"reset\") return \"button\";\n if (type === \"range\") return \"slider\";\n return \"textbox\";\n }\n return \"\";\n}\n\n/** Builds the css selector that re-finds one element inside its document. */\nexport function elementselector(element: Element): string {\n if (element.id) return `#${cssescape(element.id)}`;\n const role = element.getAttribute(\"role\");\n const name = element.getAttribute(\"name\");\n if (role && name) return `[role=\"${cssescape(role)}\"][name=\"${cssescape(name)}\"]`;\n if (name) return `${element.tagName.toLowerCase()}[name=\"${cssescape(name)}\"]`;\n const tag = element.tagName.toLowerCase();\n const parent = element.parentElement;\n if (!parent) return tag;\n const peers = [...parent.children].filter(node => node.tagName === element.tagName);\n return `${tag}:nth-of-type(${peers.indexOf(element) + 1})`;\n}\n\n/** Own text of an element: only its direct text nodes, so parents do not shadow their children. */\nexport function owntext(element: Element): string {\n let combined = \"\";\n for (const node of element.childNodes) if (node.nodeType === Node.TEXT_NODE) combined += node.textContent ?? \"\";\n return clean(combined);\n}\n\n/** Summarizes one live element into the candidate shape used by every resolution mode. */\nexport function summarize(element: Element): livecandidate {\n return {\n tag: element.tagName.toLowerCase(),\n id: element.id,\n role: element.getAttribute(\"role\")?.toLowerCase() || implicitrole(element),\n name: element.getAttribute(\"name\") ?? \"\",\n label: elementlabel(element),\n text: owntext(element),\n selector: elementselector(element),\n element,\n };\n}\n\nconst clickableselector = \"a[href], button, input, textarea, select, summary, [role=button], [role=link], [role=combobox], [role=option], [role=checkbox], [role=radio], [role=switch], [role=tab]\";\n\n/** Collects every clickable candidate in document order. */\nexport function collectclickable(root: ParentNode): livecandidate[] {\n return [...root.querySelectorAll(clickableselector)].map(summarize);\n}\n\n/** Collects every element as a resolution candidate in document order. */\nexport function collectcandidates(root: ParentNode): livecandidate[] {\n return [...root.querySelectorAll(\"*\")].map(summarize);\n}\n\n/** Matches candidates whose visible own text or label equals or contains the reviewed text. */\nexport function matchtext<T extends candidatefields>(candidates: T[], text: string): T[] {\n const wanted = clean(text).toLowerCase();\n if (!wanted) return [];\n const exact = candidates.filter(candidate => candidate.text.toLowerCase() === wanted || candidate.label.toLowerCase() === wanted);\n if (exact.length > 0) return exact;\n return candidates.filter(candidate => candidate.text.toLowerCase().includes(wanted) || candidate.label.toLowerCase().includes(wanted));\n}\n\n/** Matches candidates by the aria role and accessible name pair. */\nexport function matcharia<T extends candidatefields>(candidates: T[], role: string, name: string): T[] {\n const wantedrole = clean(role).toLowerCase();\n const wantedname = clean(name).toLowerCase();\n if (!wantedrole || !wantedname) return [];\n return candidates.filter(candidate => candidate.role.toLowerCase() === wantedrole && (candidate.label.toLowerCase() === wantedname || candidate.name.toLowerCase() === wantedname));\n}\n\n/** Matches candidates by accessible name, preferring clickable elements when several share one name. */\nexport function matchname<T extends candidatefields>(candidates: T[], name: string, clickable?: (candidate: T) => boolean): T[] {\n const wanted = clean(name).toLowerCase();\n if (!wanted) return [];\n const matches = candidates.filter(candidate => candidate.label.toLowerCase() === wanted || candidate.name.toLowerCase() === wanted);\n if (matches.length > 1 && clickable) {\n const interactive = matches.filter(clickable);\n if (interactive.length === 1) return interactive;\n }\n return matches;\n}\n\n/** Matches one clickable map number; map numbers are one based and stable inside one observation version. */\nexport function matchindex<T extends candidatefields>(candidates: T[], index: number): T[] {\n if (!Number.isInteger(index) || index < 1) return [];\n const entry = candidates[index - 1];\n return entry ? [entry] : [];\n}\n\n/** Builds the numbered clickable map from clickable candidates in document order. */\nexport function buildclickablemap<T extends candidatefields>(candidates: T[], version: number, builtat = 0): clickablemap {\n const entries: mapentry[] = candidates.map((candidate, position) => ({ number: position + 1, selector: candidate.selector, role: candidate.role || candidate.tag, label: candidate.label, mode: \"selector\" as const }));\n return { version, entries, builtat };\n}\n\n/** Describes the nested frame structure of one live document; cross origin frames stay opaque. */\nexport function describeframes(root: Document): framedescription {\n const frames = [...root.querySelectorAll(\"iframe\")].map(frame => {\n let content: Document | null = null;\n try { content = frame.contentDocument; } catch { content = null; }\n let sameorigin = false;\n try { sameorigin = content !== null && frame.contentWindow?.location.origin === location.origin; } catch { sameorigin = false; }\n return sameorigin && content ? { sameorigin: true, document: describeframes(content) } : { sameorigin: false };\n });\n return { frames, live: root };\n}\n\n/** Walks a reviewed frame path through same origin frames and refuses cross origin or absent hops. */\nexport function walkframepath(root: framedescription, path: number[]): { ok: true; document: framedescription } | { ok: false; reason: string } {\n let current = root;\n for (const index of path) {\n if (!Number.isInteger(index) || index < 0) return { ok: false, reason: \"The reviewed frame path contains an invalid frame index.\" };\n const entry = current.frames[index];\n if (!entry) return { ok: false, reason: `Frame ${index} of the reviewed frame path is absent.` };\n if (!entry.sameorigin || !entry.document) return { ok: false, reason: `Frame ${index} of the reviewed frame path is cross origin and was refused.` };\n current = entry.document;\n }\n return { ok: true, document: current };\n}\n\n/** Describes one document or shadow root scope with every nested open shadow scope. */\nexport function describescopes(root: ParentNode): scopetree {\n const elements = [...root.querySelectorAll(\"*\")];\n const shadows: scopetree[] = [];\n for (const element of elements) {\n const shadow = element.shadowRoot;\n if (shadow) {\n const nested = describescopes(shadow);\n nested.host = summarize(element);\n shadows.push(nested);\n }\n }\n return { candidates: elements.map(summarize), shadows };\n}\n\n/** Searches one scope tree depth first for candidates matching the predicate, piercing open shadow scopes recursively. */\nexport function piercescopes<T extends candidatefields>(scope: scopetree<T>, match: (candidate: T) => boolean): T[] {\n const found: T[] = [];\n for (const candidate of scope.candidates) if (match(candidate)) found.push(candidate);\n for (const shadow of scope.shadows) found.push(...piercescopes(shadow, match));\n return found;\n}\n\n/** Resolves a reviewed selector chain through open shadow roots: each selector resolves inside the previous scope. */\nexport function queryshadowchain(root: ParentNode, selectors: string[]): Element | null {\n let scope: ParentNode = root;\n for (let position = 0; position < selectors.length; position += 1) {\n const found = scope.querySelector(selectors[position] as string);\n if (!found) return null;\n if (position === selectors.length - 1) return found;\n const shadow = found.shadowRoot;\n if (!shadow) return null;\n scope = shadow;\n }\n return null;\n}\n\n/** Finds the first element matching a selector in the scope or any nested open shadow root. */\nexport function queryscoped(root: ParentNode, selector: string): Element | null {\n const direct = root.querySelector(selector);\n if (direct) return direct;\n for (const element of [...root.querySelectorAll(\"*\")]) {\n const shadow = element.shadowRoot;\n if (shadow) {\n const found = queryscoped(shadow, selector);\n if (found) return found;\n }\n }\n return null;\n}\n\nfunction parseoptionssafe(step: toolstep): Record<string, unknown> {\n try { return parseoptions(step); } catch { return {}; }\n}\n\n/** Builds the matched element summary attached to step results for review. */\nexport function targetsummary(mode: targetmode, element: HTMLElement): resolvedtarget {\n const rect = element.getBoundingClientRect();\n return { mode, selector: elementselector(element), tag: element.tagName.toLowerCase(), label: elementlabel(element), geometry: { x: rect.x, y: rect.y, width: rect.width, height: rect.height } };\n}\n\nexport type stepresolution =\n | { status: \"none\" }\n | { status: \"resolved\"; element: HTMLElement; target: resolvedtarget }\n | { status: \"ambiguous\"; mode: targetmode; candidates: string[] }\n | { status: \"absent\"; mode?: targetmode };\n\nfunction singleresolution(mode: targetmode, matches: livecandidate[]): stepresolution {\n const verdict = resolutionverdict(matches.length);\n if (verdict === \"resolved\") {\n const winner = matches[0];\n if (winner && winner.element instanceof HTMLElement) return { status: \"resolved\", element: winner.element, target: targetsummary(mode, winner.element) };\n return { status: \"absent\", mode };\n }\n if (verdict === \"ambiguous\") return { status: \"ambiguous\", mode, candidates: matches.slice(0, 8).map(candidate => candidate.label || candidate.selector) };\n return { status: \"absent\", mode };\n}\n\n/** Resolves one reviewed targetref mode against the live dom in a single pass. */\nfunction resolvetargetref(reference: Record<string, unknown>, root: Document): stepresolution {\n const mode = reference.mode;\n if (mode === \"selector\") {\n const selector = typeof reference.selector === \"string\" ? reference.selector : \"\";\n const element = selector ? root.querySelector(selector) : null;\n return element instanceof HTMLElement ? { status: \"resolved\", element, target: targetsummary(\"selector\", element) } : { status: \"absent\", mode: \"selector\" };\n }\n if (mode === \"point\") {\n const x = Number(reference.x);\n const y = Number(reference.y);\n if (!Number.isFinite(x) || !Number.isFinite(y)) return { status: \"absent\", mode: \"point\" };\n const element = root.elementFromPoint(x, y);\n return element instanceof HTMLElement ? { status: \"resolved\", element, target: targetsummary(\"point\", element) } : { status: \"absent\", mode: \"point\" };\n }\n if (mode === \"xpath\") {\n const expression = typeof reference.xpath === \"string\" ? reference.xpath : \"\";\n if (!expression) return { status: \"absent\", mode: \"xpath\" };\n const matches = evaluatexpath(buildxtree(root), expression);\n const first = matches[0];\n return first?.element instanceof HTMLElement ? { status: \"resolved\", element: first.element, target: targetsummary(\"xpath\", first.element) } : { status: \"absent\", mode: \"xpath\" };\n }\n if (mode === \"index\") {\n const matches = matchindex(collectclickable(root), Number(reference.index));\n return singleresolution(\"index\", matches);\n }\n const candidates = collectcandidates(root);\n if (mode === \"text\") return singleresolution(\"text\", matchtext(candidates, typeof reference.text === \"string\" ? reference.text : \"\"));\n if (mode === \"aria\") return singleresolution(\"aria\", matcharia(candidates, typeof reference.role === \"string\" ? reference.role : \"\", typeof reference.name === \"string\" ? reference.name : \"\"));\n if (mode === \"name\") return singleresolution(\"name\", matchname(candidates, typeof reference.name === \"string\" ? reference.name : \"\"));\n return { status: \"absent\" };\n}\n\n/** Resolves the reviewed target of a step: the options targetref when present, otherwise the css target. */\nexport function resolvestep(step: toolstep, root: Document): stepresolution {\n const reference = parseoptionssafe(step).targetref;\n if (reference && typeof reference === \"object\" && !Array.isArray(reference)) return resolvetargetref(reference as Record<string, unknown>, root);\n if (!step.target?.trim()) return { status: \"none\" };\n const element = root.querySelector(step.target);\n if (element instanceof HTMLElement) return { status: \"resolved\", element, target: targetsummary(\"selector\", element) };\n return { status: \"absent\", mode: \"selector\" };\n}\n", "import type { toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\n\n/**\n * Read-only page observation for reviewed steps.\n * Every correlated rule for attribute, style, geometry, value, content, table, link, image, meta, form, storage reads, bounded-free waits, clickable maps and verify reads lives in this file.\n */\n\nimport type { stepresult } from \"./pageactions.js\";\nimport { buildclickablemap, collectclickable, elementlabel, elementselector } from \"./pageresolve.js\";\nimport { buildxtree, evaluatexpath } from \"./pagexpath.js\";\n\nconst highlightid = \"devthinkactionhighlight\";\n\nfunction clearhighlight(): void {\n document.getElementById(highlightid)?.remove();\n}\n\n/** Outlines one reviewed target without dispatching page events. */\nfunction highlighttarget(target: Element): stepresult {\n clearhighlight();\n const rect = target.getBoundingClientRect();\n const overlay = document.createElement(\"div\");\n overlay.id = highlightid;\n overlay.setAttribute(\"aria-hidden\", \"true\");\n Object.assign(overlay.style, { position: \"fixed\", left: `${Math.max(0, rect.left - 3)}px`, top: `${Math.max(0, rect.top - 3)}px`, width: `${rect.width + 6}px`, height: `${rect.height + 6}px`, border: \"3px solid #2f9e44\", borderRadius: \"6px\", pointerEvents: \"none\", zIndex: \"2147483647\", boxSizing: \"border-box\" });\n document.documentElement.append(overlay);\n window.setTimeout(clearhighlight, 5000);\n return { ok: true, summary: \"Target outlined for five seconds.\" };\n}\n\nfunction poll(root: Document, predicate: () => boolean, description: string, timeout: number): Promise<stepresult> {\n return new Promise(resolve => {\n const started = Date.now();\n const check = (): void => {\n if (predicate()) { resolve({ ok: true, summary: `${description} is now present on the page.` }); return; }\n if (timeout > 0 && Date.now() - started >= timeout) { resolve({ ok: false, summary: `${description} did not appear within ${timeout} milliseconds.` }); return; }\n window.setTimeout(check, 100);\n };\n check();\n });\n}\n\nfunction formstate(root: Document): Array<Record<string, unknown>> {\n return [...root.querySelectorAll(\"input, textarea, select\")].map(element => ({\n type: element.getAttribute(\"type\") ?? element.tagName.toLowerCase(),\n name: element.getAttribute(\"name\") ?? \"\",\n value: element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement ? element.value : \"\",\n ...(element instanceof HTMLInputElement && (element.type === \"checkbox\" || element.type === \"radio\") ? { checked: element.checked } : {}),\n }));\n}\n\n/** Runs one read-only page observation after the background policy gate. */\nexport function runpageread(step: toolstep, target: Element | null, root: Document = document): stepresult | Promise<stepresult> {\n const options = (() => { try { return parseoptions(step); } catch { return {} as Record<string, unknown>; } })();\n switch (step.kind) {\n case \"highlight\": {\n if (!target) return { ok: false, summary: \"Highlight target is no longer available.\" };\n return highlighttarget(target);\n }\n case \"readattribute\": {\n if (!target) return { ok: false, summary: \"Read target is no longer available.\" };\n const value = target.getAttribute(step.value ?? \"\");\n return value === null ? { ok: false, summary: `Attribute ${step.value} is absent.` } : { ok: true, summary: `Attribute ${step.value} read.`, details: { value } };\n }\n case \"readstyle\": {\n if (!target) return { ok: false, summary: \"Read target is no longer available.\" };\n const computed = getComputedStyle(target);\n const styles: Record<string, string> = {};\n for (let index = 0; index < computed.length; index += 1) { const property = computed.item(index); styles[property] = computed.getPropertyValue(property); }\n return { ok: true, summary: `Read ${computed.length} computed style properties.`, details: { styles } };\n }\n case \"readgeometry\": {\n if (!target) return { ok: false, summary: \"Read target is no longer available.\" };\n const rect = target.getBoundingClientRect();\n const geometry = { x: rect.x, y: rect.y, width: rect.width, height: rect.height, top: rect.top, right: rect.right, bottom: rect.bottom, left: rect.left };\n return { ok: true, summary: \"Target geometry read.\", details: { geometry } };\n }\n case \"readvalue\": {\n if (!(target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement)) return { ok: false, summary: \"Target does not hold a form value.\" };\n return { ok: true, summary: \"Form value read.\", details: { value: target.value } };\n }\n case \"readtext\": {\n if (!target) return { ok: false, summary: \"Read target is no longer available.\" };\n const text = target.textContent ?? \"\";\n return { ok: true, summary: `Read ${text.length} characters of text.`, details: { text } };\n }\n case \"readhtml\": {\n if (!target) return { ok: false, summary: \"Read target is no longer available.\" };\n return { ok: true, summary: \"Target markup read.\", details: { html: target.outerHTML } };\n }\n case \"countelements\": {\n const count = root.querySelectorAll(step.target ?? \"\").length;\n return { ok: true, summary: `Selector matches ${count} element${count === 1 ? \"\" : \"s\"}.`, details: { count } };\n }\n case \"readtable\": {\n if (!(target instanceof HTMLTableElement)) return { ok: false, summary: \"Read target is not a table element.\" };\n const rows = [...target.querySelectorAll(\"tr\")].map(row => [...row.querySelectorAll(\"th, td\")].map(cell => cell.textContent?.trim() ?? \"\"));\n const headers = rows[0] ?? [];\n const body = rows.slice(1);\n return { ok: true, summary: `Read table with ${headers.length} column${headers.length === 1 ? \"\" : \"s\"} and ${body.length} row${body.length === 1 ? \"\" : \"s\"}.`, details: { headers, rows: body } };\n }\n case \"readlinks\": {\n const links = [...root.querySelectorAll(\"a[href]\")].map(element => ({ text: element.textContent?.trim() ?? \"\", href: element.getAttribute(\"href\") ?? \"\" }));\n return { ok: true, summary: `Read ${links.length} link${links.length === 1 ? \"\" : \"s\"}.`, details: { links } };\n }\n case \"readimages\": {\n const images = [...root.querySelectorAll(\"img\")].map(element => ({ src: element.getAttribute(\"src\") ?? \"\", alt: element.getAttribute(\"alt\") ?? \"\" }));\n return { ok: true, summary: `Read ${images.length} image${images.length === 1 ? \"\" : \"s\"}.`, details: { images } };\n }\n case \"readmeta\": {\n const meta = [...root.querySelectorAll(\"meta\")].map(element => ({ name: element.getAttribute(\"name\") ?? \"\", property: element.getAttribute(\"property\") ?? \"\", content: element.getAttribute(\"content\") ?? \"\" }));\n return { ok: true, summary: `Read ${meta.length} meta entr${meta.length === 1 ? \"y\" : \"ies\"}.`, details: { meta } };\n }\n case \"readforms\": {\n const forms = formstate(root);\n return { ok: true, summary: `Read ${forms.length} form control${forms.length === 1 ? \"\" : \"s\"}.`, details: { forms } };\n }\n case \"readstorage\": {\n try {\n if (step.value) {\n const value = localStorage.getItem(step.value);\n return { ok: true, summary: `Read local storage entry ${step.value}.`, details: { value } };\n }\n const entries: Record<string, string | null> = {};\n for (let index = 0; index < localStorage.length; index += 1) { const key = localStorage.key(index); if (key !== null) entries[key] = localStorage.getItem(key); }\n return { ok: true, summary: `Read ${Object.keys(entries).length} local storage entr${Object.keys(entries).length === 1 ? \"y\" : \"ies\"}.`, details: { entries } };\n } catch (error) { return { ok: false, summary: `Local storage refused the read: ${error instanceof Error ? error.message : String(error)}` }; }\n }\n case \"waitfor\": {\n const selector = step.target ?? \"\";\n const timeout = typeof options.timeout === \"number\" ? options.timeout : 0;\n return poll(root, () => Boolean(root.querySelector(selector)), `Selector ${selector}`, timeout);\n }\n case \"waittext\": {\n const text = step.value ?? \"\";\n const timeout = typeof options.timeout === \"number\" ? options.timeout : 0;\n return poll(root, () => (root.body?.innerText ?? \"\").includes(text), `Text ${text}`, timeout);\n }\n case \"mapclicks\": {\n const candidates = collectclickable(root);\n const map = buildclickablemap(candidates, 0, 0);\n return { ok: true, summary: `Mapped ${map.entries.length} clickable element${map.entries.length === 1 ? \"\" : \"s\"}.`, details: { entries: map.entries } };\n }\n case \"verifyvisible\": {\n if (!target) return { ok: false, summary: \"Verify target is no longer available.\" };\n const rect = target.getBoundingClientRect();\n const rendered = rect.width > 0 && rect.height > 0;\n return { ok: rendered, summary: rendered ? `Target is rendered at ${Math.round(rect.x)},${Math.round(rect.y)} with size ${Math.round(rect.width)}x${Math.round(rect.height)}.` : \"Target is not rendered.\", details: { visible: rendered, geometry: { x: rect.x, y: rect.y, width: rect.width, height: rect.height } } };\n }\n case \"verifyenabled\": {\n if (!target) return { ok: false, summary: \"Verify target is no longer available.\" };\n const control = target as HTMLInputElement;\n const disabled = control.disabled === true || target.hasAttribute(\"disabled\");\n const readonly = control.readOnly === true || target.hasAttribute(\"readonly\");\n const enabled = !disabled && !readonly;\n return { ok: enabled, summary: enabled ? \"Target is enabled and writable.\" : disabled ? \"Target is disabled.\" : \"Target is readonly.\", details: { enabled, disabled, readonly } };\n }\n case \"resolvexpath\": {\n const reference = options.targetref as Record<string, unknown> | undefined;\n const expression = typeof reference?.xpath === \"string\" ? reference.xpath : \"\";\n if (!expression) return { ok: false, summary: \"The reviewed xpath expression is absent.\" };\n let matches: ReturnType<typeof evaluatexpath> = [];\n try { matches = evaluatexpath(buildxtree(root), expression); } catch (error) { return { ok: false, summary: `The reviewed xpath expression failed: ${error instanceof Error ? error.message : String(error)}` }; }\n const summaries = matches.map(node => ({ tag: node.tag, ...(node.element ? { selector: elementselector(node.element), label: elementlabel(node.element) } : {}) }));\n return { ok: matches.length > 0, summary: matches.length > 0 ? `Resolved ${matches.length} element${matches.length === 1 ? \"\" : \"s\"} for the reviewed xpath.` : \"The reviewed xpath matched no elements.\", details: { mode: \"xpath\", matches: summaries } };\n }\n default: return { ok: false, summary: \"Unsupported page read.\" };\n }\n}\n", "import type { keyholdstate, toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { clean } from \"./pageresolve.js\";\n\n/**\n * Field and control interactions for reviewed steps.\n * Every correlated rule for timed typing, value setting, key holds, search submission, multi selects, radio choices, sliders, dates, colors, details sections and the hold registry lives in this file.\n */\n\n/** Builds the per keystroke schedule of one reviewed typetime step. */\nexport function typetimeschedule(text: string, delay: number): Array<{ key: string; delay: number }> {\n return [...text].map((character, position) => ({ key: character, delay: position === 0 ? 0 : delay }));\n}\n\n/** Appends reviewed text to the current field value. */\nexport function appendvalue(current: string, addition: string): string {\n return current + addition;\n}\n\n/** Event order delivered after one reviewed value change. */\nexport function valueevents(): string[] {\n return [\"input\", \"change\"];\n}\n\n/** Splits reviewed multi select values into present and missing entries against the declared options. */\nexport function multichoices(values: string[], options: Array<{ value: string; label: string }>): { present: string[]; missing: string[] } {\n const present: string[] = [];\n const missing: string[] = [];\n for (const value of values) {\n const option = options.find(candidate => candidate.value === value || candidate.label === value);\n if (option) present.push(option.value);\n else missing.push(value);\n }\n return { present, missing };\n}\n\n/** Picks the reviewed radio input by value or label from one radio group; the index of the match or minus one. */\nexport function radiochoice(inputs: Array<{ value: string; label: string }>, choice: string): number {\n return inputs.findIndex(candidate => candidate.value === choice || candidate.label === choice);\n}\n\n/** Clamps one reviewed slider value to the declared range and step grid. */\nexport function slidervalue(requested: number, min: number, max: number, step: number): number {\n const lower = Math.min(min, max);\n const upper = Math.max(min, max);\n const clamped = Math.min(upper, Math.max(lower, requested));\n if (!Number.isFinite(step) || step <= 0) return clamped;\n return Math.round((clamped - lower) / step) * step + lower;\n}\n\n/** Validates and normalizes one reviewed yyyy-mm-dd date. */\nexport function datevalue(requested: string): string | null {\n if (!/^\\d{4}-\\d{2}-\\d{2}$/.test(requested)) return null;\n const parts = requested.split(\"-\").map(part => Number.parseInt(part, 10));\n const year = parts[0];\n const month = parts[1];\n const day = parts[2];\n if (!year || !month || !day || month < 1 || month > 12 || day < 1 || day > 31) return null;\n return requested;\n}\n\n/** Validates and normalizes one reviewed #rrggbb color. */\nexport function colorvalue(requested: string): string | null {\n if (!/^#[0-9a-fA-F]{6}$/.test(requested)) return null;\n return requested.toLowerCase();\n}\n\n/** Decides whether one reviewed details section still needs opening. */\nexport function expandstate(open: boolean): { open: boolean; changed: boolean } {\n return open ? { open: true, changed: false } : { open: true, changed: true };\n}\n\n/** Records one held key under its hold id; a repeated active hold id is refused. */\nexport function presshold(holds: keyholdstate[], hold: keyholdstate): { holds: keyholdstate[]; ok: boolean } {\n if (holds.some(existing => existing.holdid === hold.holdid && existing.releasedat === undefined)) return { holds, ok: false };\n return { holds: [...holds, hold], ok: true };\n}\n\n/** Releases one held key by hold id, keeping the release timestamp. */\nexport function releasehold(holds: keyholdstate[], holdid: string, releasedat: number): { holds: keyholdstate[]; released?: keyholdstate } {\n let released: keyholdstate | undefined;\n const next = holds.map(hold => {\n if (hold.holdid !== holdid || hold.releasedat !== undefined) return hold;\n released = { ...hold, releasedat };\n return released;\n });\n return { holds: next, ...(released ? { released } : {}) };\n}\n\n/** Returns the keys currently held, optionally filtered to one tab. */\nexport function heldkeys(holds: keyholdstate[], tabid?: number): keyholdstate[] {\n return holds.filter(hold => hold.releasedat === undefined && (tabid === undefined || hold.tabid === undefined || hold.tabid === tabid));\n}\n\nfunction events(target: Element): void {\n target.dispatchEvent(new Event(\"input\", { bubbles: true }));\n target.dispatchEvent(new Event(\"change\", { bubbles: true }));\n}\n\nfunction modifiers(options: Record<string, unknown>): string[] {\n return Array.isArray(options.modifiers) ? options.modifiers.filter((item): item is string => typeof item === \"string\") : [];\n}\n\nfunction keyevent(type: \"keydown\" | \"keyup\", key: string, mods: string[]): KeyboardEvent {\n const code = key.length === 1 ? `Key${key.toUpperCase()}` : key;\n return new KeyboardEvent(type, { key, code, bubbles: true, cancelable: true, composed: true, ctrlKey: mods.includes(\"ctrl\"), shiftKey: mods.includes(\"shift\"), altKey: mods.includes(\"alt\"), metaKey: mods.includes(\"meta\") });\n}\n\nfunction fieldlike(target: Element | null): HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null {\n return target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement ? target : null;\n}\n\nfunction receiver(target: Element | null): HTMLElement {\n return target instanceof HTMLElement ? target : document.activeElement instanceof HTMLElement ? document.activeElement : document.body;\n}\n\nfunction wait(delay: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, delay));\n}\n\n/** Focuses the reviewed target before typing; an explicit reviewed focus option forces or suppresses the focus. */\nfunction focuswhenneeded(target: Element, options: Record<string, unknown>): void {\n if (!(target instanceof HTMLElement)) return;\n if (options.focus === false) return;\n if (options.focus === true || document.activeElement !== target) target.focus();\n}\n\nfunction pollfor(predicate: () => boolean, description: string, timeout: number): Promise<stepresult> {\n return new Promise(resolve => {\n const started = Date.now();\n const check = (): void => {\n if (predicate()) { resolve({ ok: true, summary: `${description} is now present on the page.` }); return; }\n if (timeout > 0 && Date.now() - started >= timeout) { resolve({ ok: false, summary: `${description} did not appear within ${timeout} milliseconds.` }); return; }\n window.setTimeout(check, 100);\n };\n check();\n });\n}\n\n/** Runs one reviewed control interaction after the background policy gate and a fresh target check; frame routed steps receive their frame document as the root. */\nexport function runpagecontrol(step: toolstep, target: Element | null, root: Document = document): stepresult | Promise<stepresult> {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n switch (step.kind) {\n case \"typetime\": {\n const field = fieldlike(target);\n if (!field) return { ok: false, summary: \"Target cannot receive timed text.\" };\n const text = step.value ?? \"\";\n const delay = typeof options.delay === \"number\" && options.delay > 0 ? options.delay : 0;\n focuswhenneeded(field, options);\n const schedule = typetimeschedule(text, delay);\n return (async (): Promise<stepresult> => {\n for (const entry of schedule) {\n await wait(entry.delay);\n field.dispatchEvent(keyevent(\"keydown\", entry.key, []));\n field.dispatchEvent(new KeyboardEvent(\"keypress\", { key: entry.key, bubbles: true, cancelable: true }));\n field.value = `${field.value}${entry.key}`;\n field.dispatchEvent(new Event(\"input\", { bubbles: true }));\n }\n field.dispatchEvent(new Event(\"change\", { bubbles: true }));\n return { ok: true, summary: `Typed ${text.length} character${text.length === 1 ? \"\" : \"s\"} with a per keystroke delay of ${delay} milliseconds.` };\n })();\n }\n case \"appendtext\": {\n const field = fieldlike(target);\n if (!field) return { ok: false, summary: \"Target cannot hold a value.\" };\n focuswhenneeded(field, options);\n field.value = appendvalue(field.value, step.value ?? \"\");\n events(field);\n return { ok: true, summary: \"Reviewed text appended to the current field value.\" };\n }\n case \"setvalue\": {\n const field = fieldlike(target);\n if (!field) return { ok: false, summary: \"Target cannot hold a value.\" };\n focuswhenneeded(field, options);\n field.value = step.value ?? \"\";\n events(field);\n return { ok: true, summary: `Field value set through the dom property with ${valueevents().join(\" and \")} events.` };\n }\n case \"typeedit\": {\n if (!(target instanceof HTMLElement) || !target.isContentEditable) return { ok: false, summary: \"Target is not a content editable region.\" };\n focuswhenneeded(target, options);\n const text = step.value ?? \"\";\n return (async (): Promise<stepresult> => {\n for (const character of [...text]) {\n target.dispatchEvent(new InputEvent(\"beforeinput\", { bubbles: true, cancelable: true, data: character, inputType: \"insertText\" }));\n target.append(document.createTextNode(character));\n target.dispatchEvent(new InputEvent(\"input\", { bubbles: true, data: character, inputType: \"insertText\" }));\n }\n return { ok: true, summary: `Typed ${text.length} character${text.length === 1 ? \"\" : \"s\"} into the content editable region.` };\n })();\n }\n case \"keyhold\": {\n const key = step.value ?? \"\";\n const mods = modifiers(options);\n receiver(target).dispatchEvent(keyevent(\"keydown\", key, mods));\n const holdid = typeof options.holdid === \"string\" && options.holdid ? options.holdid : \"\";\n return { ok: true, summary: `Key ${key} pressed and held${holdid ? ` under hold id ${holdid}` : \"\"}.`, details: { ...(holdid ? { holdid } : {}), modifiers: mods } };\n }\n case \"keyrelease\": {\n const key = step.value ?? \"\";\n const mods = modifiers(options);\n receiver(target).dispatchEvent(keyevent(\"keyup\", key, mods));\n return { ok: true, summary: `Key ${key} released.`, details: { modifiers: mods } };\n }\n case \"submitsearch\": {\n const field = fieldlike(target);\n if (!field) return { ok: false, summary: \"Target is not a search field.\" };\n const results = typeof options.results === \"string\" ? options.results : \"\";\n const timeout = typeof options.timeout === \"number\" ? options.timeout : 0;\n focuswhenneeded(field, options);\n field.dispatchEvent(keyevent(\"keydown\", \"Enter\", []));\n field.dispatchEvent(new KeyboardEvent(\"keypress\", { key: \"Enter\", bubbles: true, cancelable: true }));\n field.dispatchEvent(keyevent(\"keyup\", \"Enter\", []));\n return pollfor(() => Boolean(document.querySelector(results)), `Results region ${results}`, timeout);\n }\n case \"selectmulti\": {\n if (!(target instanceof HTMLSelectElement) || !target.multiple) return { ok: false, summary: \"Target is not a multi select control.\" };\n const choices = [...target.options].map(option => ({ value: option.value, label: clean(option.textContent || option.value) }));\n const requested = Array.isArray(options.values) ? options.values.filter((item): item is string => typeof item === \"string\") : [];\n const outcome = multichoices(requested, choices);\n if (outcome.missing.length > 0) return { ok: false, summary: `Reviewed option${outcome.missing.length === 1 ? \"\" : \"s\"} ${outcome.missing.join(\", \")} ${outcome.missing.length === 1 ? \"is\" : \"are\"} not part of the select control.` };\n for (const option of target.options) option.selected = outcome.present.includes(option.value);\n events(target);\n return { ok: true, summary: `Selected ${outcome.present.length} reviewed option${outcome.present.length === 1 ? \"\" : \"s\"} in the multi select control.`, details: { selected: outcome.present } };\n }\n case \"chooseradio\": {\n const radios = target instanceof HTMLInputElement && target.type === \"radio\"\n ? [...root.querySelectorAll<HTMLInputElement>(`input[type=radio][name=\"${CSS.escape(target.name)}\"]`)]\n : target ? [...(target as ParentNode).querySelectorAll<HTMLInputElement>(\"input[type=radio]\")] : [];\n if (radios.length === 0) return { ok: false, summary: \"No radio group owns the reviewed target.\" };\n const inputs = radios.map(radio => ({ value: radio.value, label: radio.labels && radio.labels.length > 0 ? clean(radio.labels[0]?.textContent || \"\") || radio.value : radio.value }));\n const index = radiochoice(inputs, step.value ?? \"\");\n const chosen = radios[index];\n if (!chosen) return { ok: false, summary: \"The reviewed radio option is not part of the group.\" };\n chosen.checked = true;\n events(chosen);\n return { ok: true, summary: `Picked reviewed radio option ${step.value}.`, details: { value: chosen.value } };\n }\n case \"setslider\": {\n if (!(target instanceof HTMLInputElement) || target.type !== \"range\") return { ok: false, summary: \"Target is not a range slider.\" };\n const requested = Number(step.value);\n if (!Number.isFinite(requested)) return { ok: false, summary: \"The reviewed slider value is not a number.\" };\n focuswhenneeded(target, options);\n const value = slidervalue(requested, Number(target.min), Number(target.max), Number(target.step));\n target.value = String(value);\n events(target);\n return { ok: true, summary: `Slider dragged to the reviewed value ${value}.`, details: { value } };\n }\n case \"setdate\": {\n if (!(target instanceof HTMLInputElement) || target.type !== \"date\") return { ok: false, summary: \"Target is not a date input.\" };\n const value = datevalue(step.value ?? \"\");\n if (value === null) return { ok: false, summary: \"The reviewed date is invalid.\" };\n focuswhenneeded(target, options);\n target.value = value;\n events(target);\n return { ok: true, summary: `Date input set to ${value}.`, details: { value } };\n }\n case \"setcolor\": {\n if (!(target instanceof HTMLInputElement) || target.type !== \"color\") return { ok: false, summary: \"Target is not a color input.\" };\n const value = colorvalue(step.value ?? \"\");\n if (value === null) return { ok: false, summary: \"The reviewed color is invalid.\" };\n focuswhenneeded(target, options);\n target.value = value;\n events(target);\n return { ok: true, summary: `Color input set to ${value}.`, details: { value } };\n }\n case \"expanddetails\": {\n const details = target instanceof HTMLElement ? target.closest(\"details\") : null;\n if (!details) return { ok: false, summary: \"Target is not inside a details section.\" };\n const outcome = expandstate(details.open);\n details.open = outcome.open;\n return { ok: true, summary: outcome.changed ? \"Collapsed details section opened.\" : \"Details section was already open.\", details: { changed: outcome.changed } };\n }\n default: return { ok: false, summary: \"Unsupported control action.\" };\n }\n}\n", "import type { pointpath, speedprofile, toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { resolvestep, type stepresolution } from \"./pageresolve.js\";\n\n/**\n * Pointer path and coordinate click logics for reviewed steps.\n * Every correlated rule for waypoint interpolation, easing, jitter, hop settling, pointer sequences and coordinate clicks lives in this file.\n */\n\n/** One interpolated pointer position with the settle delay before it is dispatched. */\nexport interface pointerhop {\n x: number;\n y: number;\n delay: number;\n}\n\n/** One planned pointer or mouse event of a coordinate click sequence. */\nexport interface plannedevent {\n type: string;\n eventkind: \"pointer\" | \"mouse\";\n x: number;\n y: number;\n shift: boolean;\n}\n\nconst basecadence = 16;\n\nfunction distance(a: { x: number; y: number }, b: { x: number; y: number }): number {\n return Math.hypot(b.x - a.x, b.y - a.y);\n}\n\nfunction ease(easing: \"linear\" | \"easeinout\", progress: number): number {\n if (easing === \"easeinout\") return progress * progress * (3 - 2 * progress);\n return progress;\n}\n\nfunction ispointref(value: unknown): value is { x: number; y: number } {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return false;\n const point = value as Record<string, unknown>;\n return typeof point.x === \"number\" && Number.isFinite(point.x) && typeof point.y === \"number\" && Number.isFinite(point.y);\n}\n\n/** Interpolates one reviewed pointer path into settle-delimited hops, honoring the reviewed easing, peak velocity and jitter window. */\nexport function pathhops(path: pointpath, profile: speedprofile | undefined, random: () => number = Math.random, cadence: number = basecadence): pointerhop[] {\n const easing = profile?.easing === \"easeinout\" ? \"easeinout\" : \"linear\";\n const peak = typeof profile?.peak === \"number\" && profile.peak > 0 ? profile.peak : undefined;\n const jitter = typeof profile?.jitter === \"number\" && profile.jitter > 0 ? profile.jitter : 0;\n const points: Array<{ x: number; y: number }> = [path.start, ...(path.waypoints ?? []), path.end];\n const lengths: number[] = [];\n let total = 0;\n for (let index = 1; index < points.length; index += 1) {\n const length = distance(points[index - 1] as { x: number; y: number }, points[index] as { x: number; y: number });\n lengths.push(length);\n total += length;\n }\n const reviewedduration = typeof path.duration === \"number\" && Number.isFinite(path.duration) && path.duration > 0 ? path.duration : undefined;\n const duration = reviewedduration ?? (peak !== undefined && total > 0 ? (total / peak) * 1000 : 300);\n const hops: pointerhop[] = [];\n let previous = points[0] as { x: number; y: number };\n for (let index = 1; index < points.length; index += 1) {\n const from = points[index - 1] as { x: number; y: number };\n const to = points[index] as { x: number; y: number };\n const length = lengths[index - 1] ?? 0;\n if (total <= 0 || length <= 0) {\n hops.push({ x: to.x, y: to.y, delay: 0 });\n previous = to;\n continue;\n }\n const segmentduration = (duration * length) / total;\n const count = Math.max(1, Math.ceil(segmentduration / Math.max(1, cadence)));\n for (let hop = 1; hop <= count; hop += 1) {\n const progress = hop / count;\n const eased = ease(easing, progress);\n const position = { x: from.x + (to.x - from.x) * eased, y: from.y + (to.y - from.y) * eased };\n const step = distance(previous, position);\n const base = segmentduration / count;\n const capped = peak !== undefined ? Math.max(base, (step / peak) * 1000) : base;\n hops.push({ x: position.x, y: position.y, delay: Math.max(0, capped + (jitter > 0 ? random() * jitter : 0)) });\n previous = position;\n }\n }\n return hops;\n}\n\n/** Builds the pointer event sequence around a reviewed path: pointerover, one pointermove per hop, then pointerout. */\nexport function pointersequence(hopcount: number): string[] {\n return [\"pointerover\", ...Array.from({ length: Math.max(0, hopcount) }, () => \"pointermove\"), \"pointerout\"];\n}\n\n/** Builds the full pointer click sequence for reviewed coordinates and modifiers. */\nexport function clickplan(x: number, y: number, modifiers: string[]): plannedevent[] {\n const shift = modifiers.includes(\"shift\");\n const pointer = (type: string): plannedevent => ({ type, eventkind: \"pointer\", x, y, shift });\n const mouse = (type: string): plannedevent => ({ type, eventkind: \"mouse\", x, y, shift });\n return [pointer(\"pointerover\"), pointer(\"pointermove\"), pointer(\"pointerdown\"), mouse(\"mousedown\"), pointer(\"pointerup\"), mouse(\"mouseup\"), mouse(\"click\")];\n}\n\n/** Dispatches one planned event on an element with the reviewed coordinates and modifier flags. */\nfunction dispatchplanned(element: Element, event: plannedevent): void {\n const init: MouseEventInit & PointerEventInit = { bubbles: true, cancelable: true, composed: true, clientX: event.x, clientY: event.y, shiftKey: event.shift };\n if (event.eventkind === \"pointer\") element.dispatchEvent(new PointerEvent(event.type, init));\n else element.dispatchEvent(new MouseEvent(event.type, init));\n}\n\n/** Dispatches the full pointer click sequence on one element with the reviewed modifiers. */\nexport function dispatchclick(element: HTMLElement, modifiers: string[] = []): void {\n const rect = element.getBoundingClientRect();\n const x = rect.left + rect.width / 2;\n const y = rect.top + rect.height / 2;\n for (const event of clickplan(x, y, modifiers)) dispatchplanned(element, event);\n}\n\n/** Scrolls one target into view before any pointer interaction. */\nexport function ensurevisible(element: HTMLElement): void {\n try { element.scrollIntoView({ block: \"center\", inline: \"nearest\", behavior: \"auto\" }); } catch { /* scroll containers may refuse; the interaction still proceeds */ }\n}\n\nfunction settle(delay: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, delay));\n}\n\n/** Dispatches a pointermove event at one viewport position on the element under the pointer. */\nfunction dispatchmove(x: number, y: number): void {\n const element = document.elementFromPoint(x, y);\n const receiver = element ?? document.documentElement;\n receiver.dispatchEvent(new PointerEvent(\"pointermove\", { bubbles: true, cancelable: true, composed: true, clientX: x, clientY: y }));\n}\n\n/** Travels one reviewed pointpath with dispatched pointermove events, settling each hop before the next one. */\nasync function travel(path: pointpath, profile: speedprofile | undefined): Promise<stepresult> {\n const hops = pathhops(path, profile);\n const startelement = document.elementFromPoint(path.start.x, path.start.y) ?? document.documentElement;\n startelement.dispatchEvent(new PointerEvent(\"pointerover\", { bubbles: true, cancelable: true, composed: true, clientX: path.start.x, clientY: path.start.y }));\n for (const hop of hops) {\n await settle(hop.delay);\n dispatchmove(hop.x, hop.y);\n }\n const endelement = document.elementFromPoint(path.end.x, path.end.y) ?? document.documentElement;\n endelement.dispatchEvent(new PointerEvent(\"pointerout\", { bubbles: true, cancelable: true, composed: true, clientX: path.end.x, clientY: path.end.y }));\n return { ok: true, summary: `Pointer traveled ${hops.length} hop${hops.length === 1 ? \"\" : \"s\"} to the reviewed end point.` };\n}\n\n/** Runs one reviewed pointer step: movepointer paths, clickpoint coordinate clicks and shiftclick modified clicks. */\nexport function runpointerstep(step: toolstep, resolution: stepresolution): stepresult | Promise<stepresult> {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n if (step.kind === \"movepointer\") {\n const path = options.pointpath as Record<string, unknown> | undefined;\n if (!path || !ispointref(path.start) || !ispointref(path.end)) return { ok: false, summary: \"The reviewed pointer path is absent.\" };\n const waypoints = Array.isArray(path.waypoints) && path.waypoints.every(item => ispointref(item)) ? path.waypoints as pointpath[\"waypoints\"] : undefined;\n const fullpath: pointpath = { start: path.start, end: path.end, ...(waypoints ? { waypoints } : {}), ...(typeof path.duration === \"number\" && Number.isFinite(path.duration) ? { duration: path.duration } : {}) };\n return travel(fullpath, options.speedprofile as speedprofile | undefined);\n }\n if (step.kind === \"clickpoint\") {\n const reference = options.targetref as Record<string, unknown> | undefined;\n const x = Number(reference?.x);\n const y = Number(reference?.y);\n if (!Number.isFinite(x) || !Number.isFinite(y)) return { ok: false, summary: \"The reviewed click coordinates are absent.\" };\n const element = document.elementFromPoint(x, y);\n if (!(element instanceof HTMLElement)) return { ok: false, summary: \"No element is rendered at the reviewed coordinates.\" };\n ensurevisible(element);\n for (const event of clickplan(x, y, [])) dispatchplanned(element, event);\n return { ok: true, summary: `Clicked the element at the reviewed coordinates ${x},${y}.` };\n }\n if (step.kind === \"shiftclick\") {\n if (resolution.status === \"ambiguous\") return { ok: false, summary: `The reviewed reference matched ${resolution.candidates.length} elements; choose one candidate.`, details: { mode: resolution.mode, candidates: resolution.candidates } };\n if (resolution.status !== \"resolved\") return { ok: false, summary: \"Action target is no longer available.\" };\n ensurevisible(resolution.element);\n dispatchclick(resolution.element, [\"shift\"]);\n return { ok: true, summary: `Shift click delivered to ${resolution.target.label || resolution.target.tag}.`, details: { mode: resolution.target.mode, resolvedtarget: resolution.target } };\n }\n return { ok: false, summary: \"Unsupported pointer action.\" };\n}\n", "import type { retryrule, toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { describeframes, queryscoped, queryshadowchain, resolvestep, targetsummary, walkframepath } from \"./pageresolve.js\";\nimport { dispatchclick, ensurevisible } from \"./pagepointer.js\";\n\n/**\n * Resolution driven interactions for reviewed steps.\n * Every correlated rule for text, aria and name clicks, shadow piercing, frame routing, wrapper step extraction and the retry loop lives in this file.\n */\n\n/** Reads the reviewed options of a step without throwing on malformed payloads. */\nfunction optionsof(step: toolstep): Record<string, unknown> {\n try { return parseoptions(step); } catch { return {}; }\n}\n\n/** Extracts the reviewed inner step of a retry or frame wrapper from its inline options. */\nexport function innerstep(step: toolstep): toolstep | null {\n const options = optionsof(step);\n const kind = options.kind;\n if (typeof kind !== \"string\" || !kind.trim()) return null;\n const inneroptions = options.options;\n return {\n id: `${step.id}inner`,\n kind: kind as toolstep[\"kind\"],\n summary: step.summary,\n risk: step.risk,\n ...(typeof options.target === \"string\" ? { target: options.target } : {}),\n ...(typeof options.value === \"string\" ? { value: options.value } : {}),\n ...(inneroptions && typeof inneroptions === \"object\" && !Array.isArray(inneroptions) ? { options: JSON.stringify(inneroptions) } : {}),\n };\n}\n\nfunction pointdistance(a: { x: number; y: number }, b: { x: number; y: number }): number {\n return Math.hypot(b.x - a.x, b.y - a.y);\n}\n\nfunction settle(delay: number): Promise<void> {\n return new Promise(resolve => setTimeout(resolve, delay));\n}\n\nexport interface retryoutcomeplan {\n ok: boolean;\n attempts: number;\n movement: number;\n summary: string;\n}\n\n/** Runs the reviewed retry loop: each failed attempt waits the settle window, revalidates the target geometry and reruns only when the element moved beyond the tolerance. */\nexport async function runretries(rule: retryrule, probe: () => Promise<{ x: number; y: number } | null>, execute: (attempt: number) => Promise<{ ok: boolean; summary: string }>): Promise<retryoutcomeplan> {\n const attempts = Math.max(1, Number.isFinite(rule.attempts) ? Math.floor(rule.attempts) : 1);\n const tolerance = typeof rule.tolerance === \"number\" && Number.isFinite(rule.tolerance) ? rule.tolerance : 0;\n const settles = typeof rule.settle === \"number\" && Number.isFinite(rule.settle) ? rule.settle : 0;\n let previous = await probe();\n let movement = 0;\n let made = 0;\n let last = \"\";\n for (let attempt = 1; attempt <= attempts; attempt += 1) {\n made = attempt;\n const result = await execute(attempt);\n last = result.summary;\n if (result.ok) return { ok: true, attempts: made, movement, summary: `Retry interaction succeeded on attempt ${made} after ${movement.toFixed(1)} pixels of observed movement.` };\n if (attempt >= attempts) break;\n if (settles > 0) await settle(settles);\n const current = await probe();\n if (!current) { previous = null; continue; }\n if (previous) {\n const delta = pointdistance(previous, current);\n movement = Math.max(movement, delta);\n if (delta <= tolerance) return { ok: false, attempts: made, movement, summary: `The target stayed within the reviewed tolerance of ${tolerance} pixels; retry stopped after attempt ${made}. ${last}` };\n }\n previous = current;\n }\n return { ok: false, attempts: made, movement, summary: `Retry interaction failed after ${made} attempt${made === 1 ? \"\" : \"s\"} with ${movement.toFixed(1)} pixels of observed movement. ${last}` };\n}\n\n/** Dispatches one reviewed click on a resolved element and reports the target mode used. */\nfunction clickresolved(stepkind: string, resolution: ReturnType<typeof resolvestep>): stepresult {\n if (resolution.status === \"ambiguous\") return { ok: false, summary: `The reviewed ${resolution.mode} reference matched ${resolution.candidates.length} elements: ${resolution.candidates.join(\"; \")}.`, details: { mode: resolution.mode, candidates: resolution.candidates } };\n if (resolution.status !== \"resolved\") return { ok: false, summary: \"The reviewed target is no longer available.\" };\n ensurevisible(resolution.element);\n dispatchclick(resolution.element);\n return { ok: true, summary: `Clicked ${resolution.target.label || resolution.target.tag} resolved by ${stepkind} ${resolution.target.mode} mode.`, details: { mode: resolution.target.mode, resolvedtarget: resolution.target } };\n}\n\n/** Runs one resolution driven interaction: text, aria and name clicks, shadow piercing and frame routing. */\nexport function runinteractstep(step: toolstep, expectedorigin: string, dispatch: (inner: toolstep, origin: string, root?: Document) => stepresult | Promise<stepresult>): stepresult | Promise<stepresult> {\n if (step.kind === \"clicktext\" || step.kind === \"clickaria\" || step.kind === \"clickname\") {\n return clickresolved(step.kind, resolvestep(step, document));\n }\n if (step.kind === \"pierceshadow\") {\n const options = optionsof(step);\n const shadow = Array.isArray(options.shadow) ? options.shadow.filter((item): item is string => typeof item === \"string\" && item.trim().length > 0) : [];\n const element = shadow.length > 0 ? queryshadowchain(document, shadow) : queryscoped(document, step.target ?? \"\");\n if (!(element instanceof HTMLElement)) return { ok: false, summary: \"The reviewed shadow target is not available.\" };\n ensurevisible(element);\n dispatchclick(element);\n const summary = targetsummary(\"selector\", element);\n return { ok: true, summary: `Clicked ${summary.label || summary.tag} resolved through ${shadow.length > 0 ? \"the reviewed shadow path\" : \"open shadow roots\"}.`, details: { mode: \"selector\", resolvedtarget: summary } };\n }\n if (step.kind === \"enterframe\") {\n const options = optionsof(step);\n const path = Array.isArray(options.framepath) ? options.framepath.filter((item): item is number => typeof item === \"number\" && Number.isInteger(item) && item >= 0) : [];\n const walk = walkframepath(describeframes(document), path);\n if (!walk.ok) return { ok: false, summary: walk.reason };\n const framedocument = walk.document.live;\n if (!framedocument) return { ok: false, summary: \"The reviewed frame document is not available.\" };\n const inner = innerstep(step);\n if (!inner) return { ok: false, summary: \"The reviewed inner step is absent.\" };\n return dispatch(inner, expectedorigin, framedocument);\n }\n return { ok: false, summary: \"Unsupported interaction action.\" };\n}\n", "import type { navtarget, toolstep, urlpattern, waitoverride, waitprofile } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\n\n/**\n * Page navigation logics for reviewed steps.\n * Every correlated rule for navigation targets, container resolution, wait profiles, url patterns, link following, spa routing, query rewriting, deep links and recent tabs lives in this file.\n */\n\n/** One link shape matched by followlink and spanav, serializable for fixtures. */\nexport interface linkshape {\n text: string;\n href: string;\n selector: string;\n}\n\n/** One collected load signal sample with the signals that held true at its time. */\nexport interface signalsample {\n at: number;\n signals: string[];\n}\n\n/** Resolved container plan of one navigation target across tabs, windows and private profiles. */\nexport interface containerplan {\n kind: \"current\" | \"tab\" | \"window\" | \"private\";\n incognito: boolean;\n windowid?: number;\n position: \"adjacent\" | \"end\";\n}\n\n/** Reads the reviewed navtarget of a navigation step; null when the step reviews none. */\nexport function parsenavtarget(step: toolstep): navtarget | null {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const value = options.navtarget;\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return null;\n const target = value as Record<string, unknown>;\n if (typeof target.url !== \"string\" || !target.url) return null;\n const container = target.container === \"current\" || target.container === \"window\" || target.container === \"private\" ? target.container : \"tab\";\n return {\n url: target.url,\n container,\n ...(target.position === \"end\" ? { position: \"end\" } : { position: \"adjacent\" }),\n private: container === \"private\" || target.private === true,\n };\n}\n\n/** Resolves the container plan of one navigation target: the current task tab, a new tab, a new window or a private window profile separated from normal windows. */\nexport function resolvecontainer(target: navtarget, windows: Array<{ id: number; incognito: boolean; focused: boolean }>): containerplan {\n if (target.container === \"current\") return { kind: \"current\", incognito: false, position: target.position ?? \"adjacent\" };\n if (target.container === \"private\" || target.private) return { kind: \"private\", incognito: true, position: target.position ?? \"adjacent\" };\n if (target.container === \"window\") {\n const focused = windows.find(item => item.focused);\n return { kind: \"window\", incognito: false, ...(focused ? { windowid: focused.id } : {}), position: target.position ?? \"adjacent\" };\n }\n const normal = windows.find(item => !item.incognito && item.focused) ?? windows.find(item => !item.incognito);\n return { kind: \"tab\", incognito: false, ...(normal ? { windowid: normal.id } : {}), position: target.position ?? \"adjacent\" };\n}\n\n/** Reads the reviewed waitprofile of a navprofile step; null when the step reviews none. */\nexport function parsewaitprofile(step: toolstep): waitprofile | null {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const value = options.waitprofile;\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return null;\n const profile = value as Record<string, unknown>;\n const signals = Array.isArray(profile.signals) ? profile.signals.filter((item): item is string => typeof item === \"string\" && item.trim().length > 0) : [];\n if (signals.length === 0) return null;\n const overrides: waitoverride[] = [];\n if (Array.isArray(profile.overrides)) {\n for (const item of profile.overrides) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) continue;\n const override = item as Record<string, unknown>;\n if (typeof override.origin !== \"string\" || !override.origin) continue;\n const overridesignals = Array.isArray(override.signals) ? override.signals.filter((entry): entry is string => typeof entry === \"string\" && entry.trim().length > 0) : undefined;\n overrides.push({\n origin: override.origin,\n ...(overridesignals && overridesignals.length > 0 ? { signals: overridesignals } : {}),\n ...(typeof override.idle === \"number\" && Number.isFinite(override.idle) && override.idle >= 0 ? { idle: override.idle } : {}),\n ...(typeof override.timeout === \"number\" && Number.isFinite(override.timeout) && override.timeout >= 0 ? { timeout: override.timeout } : {}),\n });\n }\n }\n return {\n signals,\n ...(typeof profile.idle === \"number\" && Number.isFinite(profile.idle) && profile.idle >= 0 ? { idle: profile.idle } : {}),\n ...(typeof profile.timeout === \"number\" && Number.isFinite(profile.timeout) && profile.timeout >= 0 ? { timeout: profile.timeout } : {}),\n ...(overrides.length > 0 ? { overrides } : {}),\n };\n}\n\n/** Resolves the effective signals and thresholds of one wait profile for an origin by folding the per origin overrides in. */\nexport function profilefororigin(profile: waitprofile, origin: string): { signals: string[]; idle: number; timeout: number } {\n let signals = [...profile.signals];\n let idle = profile.idle ?? 0;\n let timeout = profile.timeout ?? 0;\n for (const override of profile.overrides ?? []) {\n if (!override.origin || new URL(override.origin).origin !== origin) continue;\n if (override.signals && override.signals.length > 0) signals = [...override.signals];\n if (override.idle !== undefined) idle = override.idle;\n if (override.timeout !== undefined) timeout = override.timeout;\n }\n return { signals, idle, timeout };\n}\n\n/** Maps a document ready state onto the live navigation load phase. */\nexport function loadphase(readystate: string): \"loading\" | \"interactive\" | \"complete\" {\n if (readystate === \"interactive\") return \"interactive\";\n if (readystate === \"complete\") return \"complete\";\n return \"loading\";\n}\n\n/** Decides the wait outcome of a wait profile from collected load signal samples: every required signal must hold in the latest sample inside the timeout. */\nexport function evaluatesignals(required: string[], samples: signalsample[], timeout: number): { ok: boolean; satisfied: string[]; waited: number; samples: number } {\n const start = samples[0]?.at ?? 0;\n const last = samples[samples.length - 1];\n const waited = Math.max(0, (last?.at ?? 0) - start);\n const held = last?.signals ?? [];\n const satisfied = required.filter(signal => held.includes(signal));\n if (required.length > 0 && satisfied.length === required.length) return { ok: true, satisfied, waited, samples: samples.length };\n if (timeout > 0 && waited >= timeout) return { ok: false, satisfied, waited, samples: samples.length };\n return { ok: false, satisfied, waited, samples: samples.length };\n}\n\n/** Reads the reviewed urlpattern of a waiturl, spanav or spawait step; null when the step reviews none. */\nexport function parseurlpattern(step: toolstep, key: string = \"urlpattern\"): urlpattern | null {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const value = options[key];\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return null;\n const pattern = value as Record<string, unknown>;\n if (typeof pattern.url !== \"string\" || !pattern.url) return null;\n const mode = pattern.mode === \"exact\" || pattern.mode === \"host\" || pattern.mode === \"pattern\" ? pattern.mode : \"prefix\";\n const query: Record<string, string> = {};\n if (pattern.query && typeof pattern.query === \"object\" && !Array.isArray(pattern.query)) {\n for (const [name, item] of Object.entries(pattern.query as Record<string, unknown>)) if (typeof item === \"string\") query[name] = item;\n }\n return {\n mode,\n url: pattern.url,\n ...(Object.keys(query).length > 0 ? { query } : {}),\n ...(typeof pattern.fragment === \"string\" && pattern.fragment ? { fragment: pattern.fragment } : {}),\n };\n}\n\n/** Matches a wildcard path segment against one url path segment. */\nfunction segmentmatches(pattern: string, actual: string): boolean {\n if (pattern === \"*\" || pattern === \"**\") return true;\n if (!pattern.includes(\"*\")) return pattern === actual;\n const parts = pattern.split(\"*\");\n let index = 0;\n for (let position = 0; position < parts.length; position += 1) {\n const part = parts[position] as string;\n if (part === \"\") continue;\n const found = actual.indexOf(part, index);\n if (found < 0) return false;\n if (position === 0 && found !== 0) return false;\n index = found + part.length;\n }\n const last = parts[parts.length - 1] as string;\n return last === \"\" || actual.endsWith(last);\n}\n\n/** Matches one url against a reviewed urlpattern by mode plus required query and fragment parts. */\nexport function urlmatches(url: string, pattern: urlpattern): boolean {\n let parsed: URL;\n try { parsed = new URL(url); } catch { return false; }\n let expected: URL;\n try { expected = new URL(pattern.url); } catch { return false; }\n if (pattern.mode === \"exact\" && parsed.toString() !== expected.toString()) return false;\n if (pattern.mode === \"prefix\" && !parsed.toString().startsWith(pattern.url)) return false;\n if (pattern.mode === \"host\" && parsed.origin !== expected.origin) return false;\n if (pattern.mode === \"pattern\") {\n if (parsed.origin !== expected.origin) return false;\n const expectedsegments = expected.pathname.split(\"/\").filter(segment => segment !== \"\");\n const actualsegments = parsed.pathname.split(\"/\").filter(segment => segment !== \"\");\n if (expectedsegments.includes(\"**\")) {\n const cut = expectedsegments.indexOf(\"**\");\n const head = expectedsegments.slice(0, cut);\n const tail = expectedsegments.slice(cut + 1);\n if (actualsegments.length < head.length + tail.length) return false;\n if (!head.every((segment, position) => segmentmatches(segment, actualsegments[position] ?? \"\"))) return false;\n if (!tail.every((segment, position) => segmentmatches(segment, actualsegments[actualsegments.length - tail.length + position] ?? \"\"))) return false;\n } else if (expectedsegments.length !== actualsegments.length || !expectedsegments.every((segment, position) => segmentmatches(segment, actualsegments[position] ?? \"\"))) return false;\n }\n const values = parsed.searchParams;\n for (const [name, value] of Object.entries(pattern.query ?? {})) {\n if (!values.has(name)) return false;\n if (value !== \"*\" && values.get(name) !== value) return false;\n }\n if (pattern.fragment !== undefined && parsed.hash.slice(1) !== pattern.fragment) return false;\n return true;\n}\n\n/** Reads the current query parameters of a url. */\nfunction readquery(url: string): Record<string, string> {\n const values: Record<string, string> = {};\n try {\n for (const [name, value] of new URL(url).searchParams.entries()) values[name] = value;\n } catch { /* an unparseable url has no readable parameters */ }\n return values;\n}\n\n/** Result of one query rewrite with the parameters read, written and the new url. */\nexport interface queryrewriteoutcome {\n url: string;\n before: Record<string, string>;\n after: Record<string, string>;\n set: string[];\n removed: string[];\n}\n\n/** Rewrites the query parameters of one url: reviewed values are set, reviewed names removed and the rest preserved. */\nexport function rewritequeryurl(url: string, set: Record<string, string>, remove: string[]): queryrewriteoutcome {\n const parsed = new URL(url);\n const before = readquery(url);\n for (const name of remove) parsed.searchParams.delete(name);\n for (const [name, value] of Object.entries(set)) parsed.searchParams.set(name, value);\n parsed.hash = \"\";\n return { url: parsed.toString(), before, after: readquery(parsed.toString()), set: Object.keys(set), removed: [...remove] };\n}\n\n/** Applies one reviewed fragment to a url, keeping every other part untouched. */\nexport function fragmenturl(url: string, fragment: string): string {\n const parsed = new URL(url);\n parsed.hash = fragment.replace(/^#/, \"\");\n return parsed.toString();\n}\n\n/** Matches links by their visible text, case insensitively, refusing ambiguity through multiple matches. */\nexport function matchlinktext(links: linkshape[], text: string): linkshape[] {\n const wanted = text.trim().toLowerCase();\n return links.filter(link => link.text.trim().toLowerCase() === wanted);\n}\n\n/** Matches links by their href fragment when the reviewed option asks for fragment resolution. */\nexport function matchlinkfragment(links: linkshape[], fragment: string): linkshape[] {\n const wanted = fragment.trim().replace(/^#/, \"\");\n return links.filter(link => {\n try { return new URL(link.href, \"https://example.invalid\").hash.replace(/^#/, \"\") === wanted; } catch { return false; }\n });\n}\n\n/** Builds a deep link into a common web app from a reviewed app pattern and its string params; unknown apps are refused. */\nexport function deeplinkurl(app: string, params: Record<string, string>): string | null {\n const value = (name: string): string | undefined => {\n const item = params[name];\n return typeof item === \"string\" && item.trim() ? item.trim() : undefined;\n };\n switch (app.trim().toLowerCase()) {\n case \"github\": {\n const owner = value(\"owner\");\n const repo = value(\"repo\");\n if (!owner || !repo) return null;\n const path = value(\"path\");\n return `https://github.com/${owner}/${repo}${path ? `/${path.replace(/^\\/+/, \"\")}` : \"\"}`;\n }\n case \"youtube\": {\n const id = value(\"id\");\n if (id) return `https://www.youtube.com/watch?v=${encodeURIComponent(id)}`;\n const search = value(\"search\");\n if (search) return `https://www.youtube.com/results?search_query=${encodeURIComponent(search)}`;\n return null;\n }\n case \"maps\": {\n const query = value(\"query\");\n if (!query) return null;\n return `https://www.google.com/maps/search/${encodeURIComponent(query)}`;\n }\n case \"wikipedia\": {\n const title = value(\"title\");\n if (!title) return null;\n const language = value(\"language\") ?? \"en\";\n return `https://${language}.wikipedia.org/wiki/${encodeURIComponent(title.replace(/\\s+/g, \"_\"))}`;\n }\n case \"amazon\": {\n const search = value(\"search\");\n if (!search) return null;\n return `https://www.amazon.com/s?k=${encodeURIComponent(search)}`;\n }\n case \"x\": {\n const user = value(\"user\");\n if (!user) return null;\n return `https://x.com/${user.replace(/^@/, \"\")}`;\n }\n default: return null;\n }\n}\n\n/** True when the current url of a single page app changed without a reload, keeping the origin. */\nexport function spauroutechanged(previousurl: string, currenturl: string): boolean {\n if (previousurl === currenturl) return false;\n try {\n return new URL(previousurl).origin === new URL(currenturl).origin;\n } catch { return false; }\n}\n\n/** Picks the most recently closed tab whose url is no longer open; null when every recent tab is already restored. */\nexport function pickrecenttab(recenttabs: Array<{ url: string; tabid: number; closedat: number }>, openurls: string[]): { url: string; tabid: number; closedat: number } | null {\n return recenttabs.find(tab => !openurls.includes(tab.url)) ?? null;\n}\n\nfunction wait(ms: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, ms));\n}\n\nfunction stepoptions(step: toolstep): Record<string, unknown> {\n try { return parseoptions(step); } catch { return {}; }\n}\n\nfunction collectlinks(root: Document): linkshape[] {\n return [...root.querySelectorAll(\"a[href]\")].map(element => ({\n text: element.textContent?.trim() ?? \"\",\n href: element instanceof HTMLAnchorElement ? element.href : element.getAttribute(\"href\") ?? \"\",\n selector: element.getAttribute(\"href\") ?? \"\",\n }));\n}\n\n/** Resolves the reviewed link of a followlink or spanav step by visible text or href fragment, refusing links outside the allowed origins. */\nfunction resolvelink(step: toolstep, root: Document): { ok: true; element: HTMLAnchorElement } | { ok: false; summary: string } {\n const options = stepoptions(step);\n const allowedorigins = Array.isArray(options.allowedorigins) ? options.allowedorigins.filter((item): item is string => typeof item === \"string\") : [];\n const links = collectlinks(root);\n const matches = options.fragment === true ? matchlinkfragment(links, step.value ?? \"\") : matchlinktext(links, step.value ?? \"\");\n if (matches.length === 0) return { ok: false, summary: `No link matches the reviewed reference \"${step.value ?? \"\"}\".` };\n if (matches.length > 1) return { ok: false, summary: `The reviewed link reference matched ${matches.length} links; review a unique one.` };\n const element = [...root.querySelectorAll(\"a[href]\")].find(candidate => (candidate instanceof HTMLAnchorElement ? candidate.href : candidate.getAttribute(\"href\") ?? \"\") === matches[0]?.href);\n if (!(element instanceof HTMLAnchorElement)) return { ok: false, summary: \"The reviewed link is no longer available.\" };\n if (allowedorigins.length > 0) {\n let origin = \"\";\n try { origin = new URL(element.href).origin; } catch { origin = \"\"; }\n if (!allowedorigins.includes(origin)) return { ok: false, summary: `The reviewed link leaves the session origin grants for ${origin}.` };\n }\n return { ok: true, element };\n}\n\n/** Waits for the page load event and reports the ready state and load phase. */\nasync function runwaitload(step: toolstep): Promise<stepresult> {\n const options = stepoptions(step);\n const timeout = typeof options.timeout === \"number\" && Number.isFinite(options.timeout) && options.timeout > 0 ? options.timeout : 0;\n const started = Date.now();\n for (;;) {\n const phase = loadphase(document.readyState);\n if (phase === \"complete\") return { ok: true, summary: `The page load event fired and the document is complete after ${Date.now() - started} milliseconds.`, details: { phase, readystate: document.readyState, waited: Date.now() - started } };\n if (timeout > 0 && Date.now() - started >= timeout) return { ok: false, summary: `The page did not reach the complete load phase within the reviewed timeout of ${timeout} milliseconds.`, details: { phase, readystate: document.readyState, waited: Date.now() - started } };\n await wait(50);\n }\n}\n\n/** Waits until the current url matches the reviewed urlpattern. */\nasync function runwaiturl(step: toolstep): Promise<stepresult> {\n const options = stepoptions(step);\n const pattern = parseurlpattern(step);\n if (!pattern) return { ok: false, summary: \"A reviewed urlpattern is required.\" };\n const timeout = typeof options.timeout === \"number\" && Number.isFinite(options.timeout) && options.timeout > 0 ? options.timeout : 0;\n const poll = typeof options.poll === \"number\" && Number.isFinite(options.poll) && options.poll > 0 ? options.poll : 100;\n const started = Date.now();\n for (;;) {\n if (urlmatches(location.href, pattern)) return { ok: true, summary: `The url matched the reviewed ${pattern.mode} pattern after ${Date.now() - started} milliseconds.`, details: { url: location.href, mode: pattern.mode, waited: Date.now() - started } };\n if (timeout > 0 && Date.now() - started >= timeout) return { ok: false, summary: `The url did not match the reviewed ${pattern.mode} pattern within the reviewed timeout of ${timeout} milliseconds.`, details: { url: location.href, mode: pattern.mode, waited: Date.now() - started } };\n await wait(poll);\n }\n}\n\n/** Follows one reviewed link, reporting the href travelled to. */\nasync function runfollowlink(step: toolstep, root: Document): Promise<stepresult> {\n const resolution = resolvelink(step, root);\n if (!resolution.ok) return { ok: false, summary: resolution.summary };\n const href = resolution.element.href;\n resolution.element.click();\n return { ok: true, summary: `Followed the reviewed link to ${href}.`, details: { href } };\n}\n\n/** Navigates a single page app by clicking the reviewed control and waiting for the route change without a reload. */\nasync function runspanav(step: toolstep, root: Document): Promise<stepresult> {\n const options = stepoptions(step);\n const resolution = resolvelink(step, root);\n if (!resolution.ok) return { ok: false, summary: resolution.summary };\n const timeout = typeof options.timeout === \"number\" && Number.isFinite(options.timeout) && options.timeout > 0 ? options.timeout : 0;\n const pattern = parseurlpattern(step, \"routepattern\");\n const before = location.href;\n resolution.element.click();\n const started = Date.now();\n for (;;) {\n const changed = spauroutechanged(before, location.href);\n const matched = pattern ? urlmatches(location.href, pattern) : changed;\n if (matched) return { ok: true, summary: `The single page app route changed to ${location.href} without a reload.`, details: { from: before, to: location.href, waited: Date.now() - started } };\n if (timeout > 0 && Date.now() - started >= timeout) return { ok: false, summary: `The single page app route did not change within the reviewed timeout of ${timeout} milliseconds.`, details: { from: before, to: location.href, waited: Date.now() - started } };\n await wait(50);\n }\n}\n\n/** Waits for a url change inside a single page app without a reload, through popstate, hashchange and history polling. */\nasync function runspawait(step: toolstep): Promise<stepresult> {\n const options = stepoptions(step);\n const timeout = typeof options.timeout === \"number\" && Number.isFinite(options.timeout) && options.timeout > 0 ? options.timeout : 0;\n const poll = typeof options.poll === \"number\" && Number.isFinite(options.poll) && options.poll > 0 ? options.poll : 100;\n const pattern = parseurlpattern(step);\n const before = location.href;\n const started = Date.now();\n let detected = false;\n const onroute = (): void => { if (spauroutechanged(before, location.href)) detected = true; };\n window.addEventListener(\"popstate\", onroute);\n window.addEventListener(\"hashchange\", onroute);\n try {\n for (;;) {\n if (pattern ? urlmatches(location.href, pattern) : detected || spauroutechanged(before, location.href)) {\n return { ok: true, summary: `The single page app url changed to ${location.href} without a reload.`, details: { from: before, to: location.href, waited: Date.now() - started } };\n }\n if (timeout > 0 && Date.now() - started >= timeout) return { ok: false, summary: `The single page app url did not change within the reviewed timeout of ${timeout} milliseconds.`, details: { from: before, to: location.href, waited: Date.now() - started } };\n await wait(poll);\n }\n } finally {\n window.removeEventListener(\"popstate\", onroute);\n window.removeEventListener(\"hashchange\", onroute);\n }\n}\n\n/** Reads and rewrites the query parameters of the current url with pushstate, reporting the parameters read and the new url. */\nfunction runrewritequery(step: toolstep): stepresult {\n const options = stepoptions(step);\n const set: Record<string, string> = {};\n if (options.set && typeof options.set === \"object\" && !Array.isArray(options.set)) {\n for (const [name, value] of Object.entries(options.set as Record<string, unknown>)) if (typeof value === \"string\") set[name] = value;\n }\n const remove = Array.isArray(options.remove) ? options.remove.filter((item): item is string => typeof item === \"string\" && item.trim().length > 0) : [];\n const outcome = rewritequeryurl(location.href, set, remove);\n history.pushState(history.state, document.title, outcome.url);\n return { ok: true, summary: `Rewrote ${outcome.set.length + outcome.removed.length} query parameter${outcome.set.length + outcome.removed.length === 1 ? \"\" : \"s\"}; the url is now ${outcome.url}.`, details: { url: outcome.url, before: outcome.before, after: outcome.after, set: outcome.set, removed: outcome.removed } };\n}\n\n/** Sets the reviewed url fragment and scrolls to its anchor with smooth behavior. */\nasync function runsetfragment(step: toolstep): Promise<stepresult> {\n const fragment = (step.value ?? \"\").replace(/^#/, \"\");\n if (!fragment) return { ok: false, summary: \"A reviewed fragment is required.\" };\n const url = fragmenturl(location.href, fragment);\n history.pushState(history.state, document.title, url);\n const anchor = document.getElementById(fragment);\n anchor?.scrollIntoView({ behavior: \"smooth\", block: \"start\" });\n return { ok: true, summary: `Set the url fragment to ${fragment} and scrolled to its anchor.`, details: { url, fragment, anchored: Boolean(anchor) } };\n}\n\n/** Stops a pending navigation by halting the document load. */\nfunction runstopnav(): stepresult {\n window.stop();\n return { ok: true, summary: \"Stopped the pending navigation of the page.\" };\n}\n\n/** Prefetches the reviewed urls by injecting prefetch hints into the document head. */\nfunction runprefetch(step: toolstep): stepresult {\n const options = stepoptions(step);\n const urls = Array.isArray(options.urls) ? options.urls.filter((item): item is string => typeof item === \"string\" && item.trim().length > 0) : [];\n if (urls.length === 0) return { ok: false, summary: \"A reviewed list of prefetch urls is required.\" };\n for (const url of urls) {\n const hint = document.createElement(\"link\");\n hint.rel = \"prefetch\";\n hint.href = url;\n document.head.append(hint);\n }\n return { ok: true, summary: `Queued ${urls.length} prefetch hint${urls.length === 1 ? \"\" : \"s\"}.`, details: { urls } };\n}\n\n/** Preconnects to the reviewed origins by injecting preconnect hints into the document head. */\nfunction runpreconnect(step: toolstep): stepresult {\n const options = stepoptions(step);\n const origins = Array.isArray(options.origins) ? options.origins.filter((item): item is string => typeof item === \"string\" && item.trim().length > 0) : [];\n if (origins.length === 0) return { ok: false, summary: \"A reviewed list of preconnect origins is required.\" };\n for (const origin of origins) {\n const hint = document.createElement(\"link\");\n hint.rel = \"preconnect\";\n hint.href = origin;\n document.head.append(hint);\n }\n return { ok: true, summary: `Opened ${origins.length} preconnect hint${origins.length === 1 ? \"\" : \"s\"}.`, details: { origins } };\n}\n\n/** Prints the page through the browser print pipeline; the artifact routing happens in the background. */\nfunction runprintpdf(): stepresult {\n window.print();\n return { ok: true, summary: \"Sent the page to the browser print pipeline.\" };\n}\n\n/** Runs one reviewed page navigation kind inside the page world. */\nexport function runpagenav(step: toolstep, root: Document = document): stepresult | Promise<stepresult> {\n switch (step.kind) {\n case \"waitload\": return runwaitload(step);\n case \"waiturl\": return runwaiturl(step);\n case \"followlink\": return runfollowlink(step, root);\n case \"spanav\": return runspanav(step, root);\n case \"spawait\": return runspawait(step);\n case \"rewritequery\": return runrewritequery(step);\n case \"setfragment\": return runsetfragment(step);\n case \"stopnav\": return runstopnav();\n case \"prefetch\": return runprefetch(step);\n case \"preconnect\": return runpreconnect(step);\n case \"printpdf\": return runprintpdf();\n default: return { ok: false, summary: \"Unsupported navigation action.\" };\n }\n}\n", "import type { dialogpolicy, toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\n\n/**\n * Dialog policy logics for reviewed steps.\n * Every correlated rule for policy parsing, dialog answers, main world handler installation and observed dialog harvesting lives in this file.\n * The isolated world cannot override window.confirm, window.alert or window.prompt, so the background installs these wrappers through the scripting api in the main world.\n */\n\n/** One dialog observed by the main world handler, recorded on the shared document. */\nexport interface observeddialog {\n dialog: string;\n text: string;\n result: string | boolean | null;\n at: number;\n}\n\n/** Parses the reviewed dialog policy from step options; prompts need a reviewed answer. */\nexport function parsedialogpolicy(step: toolstep): dialogpolicy | null {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { return null; }\n const accept = options.accept;\n const answer = options.answer;\n if (typeof accept !== \"boolean\" && typeof answer !== \"string\") return null;\n return { accept: accept === true, ...(typeof answer === \"string\" && answer.trim() ? { answer } : {}) };\n}\n\n/** Decides how the reviewed policy answers one dialog kind; a prompt without a reviewed answer is dismissed. */\nexport function dialoganswer(policy: dialogpolicy, dialog: \"confirm\" | \"alert\" | \"prompt\"): { accept: boolean; answer?: string } {\n if (dialog === \"prompt\") {\n if (policy.answer === undefined || policy.answer === \"\") return { accept: false };\n return { accept: policy.accept !== false, ...(policy.answer !== undefined ? { answer: policy.answer } : {}) };\n }\n return { accept: policy.accept };\n}\n\n/** Reads and clears the dialog log the main world handler recorded on the shared document. */\nexport function harvestdialoglog(root: Document): observeddialog[] {\n const raw = root.documentElement.dataset.devthinkdialoglog;\n if (!raw) return [];\n delete root.documentElement.dataset.devthinkdialoglog;\n try {\n const parsed: unknown = JSON.parse(raw);\n if (!Array.isArray(parsed)) return [];\n return parsed.filter((item): item is observeddialog => Boolean(item) && typeof item === \"object\" && typeof (item as Record<string, unknown>).dialog === \"string\");\n } catch { return []; }\n}\n\n/** Installs confirm, alert and prompt wrappers in the page main world answering per the reviewed policy; runs through chrome.scripting with world main. */\nexport function installdialoghandler(accept: boolean, answer: string, persistent: boolean): void {\n const world = globalThis as typeof globalThis & { devthinkoriginaldialogs?: { confirm: (text?: string) => boolean; alert: (text?: string) => void; prompt: (text?: string, defaultvalue?: string) => string | null } };\n const originals = world.devthinkoriginaldialogs ?? { confirm: window.confirm.bind(window), alert: window.alert.bind(window), prompt: window.prompt.bind(window) };\n world.devthinkoriginaldialogs = originals;\n const decide = (dialog: string): { accept: boolean; answer: string | null } => {\n if (dialog === \"prompt\") return answer ? { accept: true, answer } : { accept: false, answer: null };\n return { accept, answer: null };\n };\n const record = (dialog: string, text: string, result: string | boolean | null): void => {\n try {\n const root = document.documentElement;\n const log = JSON.parse(root.dataset.devthinkdialoglog ?? \"[]\") as unknown[];\n log.push({ dialog, text, result, at: Date.now() });\n root.dataset.devthinkdialoglog = JSON.stringify(log);\n } catch { /* a locked down page refuses dataset writes; the handler still answers */ }\n };\n window.confirm = (text?: string): boolean => {\n const decision = decide(\"confirm\");\n record(\"confirm\", text ?? \"\", decision.accept);\n if (!persistent) window.confirm = originals.confirm;\n return decision.accept;\n };\n window.alert = (text?: string): void => {\n record(\"alert\", text ?? \"\", true);\n if (!persistent) window.alert = originals.alert;\n };\n window.prompt = (text?: string, defaultvalue?: string): string | null => {\n const decision = decide(\"prompt\");\n const outcome = decision.accept ? (decision.answer ?? defaultvalue ?? \"\") : null;\n record(\"prompt\", text ?? \"\", outcome);\n if (!persistent) window.prompt = originals.prompt;\n return outcome;\n };\n}\n", "import type { a11ynode, readerarticle, toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { clean, describescopes, elementlabel, elementselector, implicitrole, owntext, type candidatefields, type scopetree } from \"./pageresolve.js\";\n\n/**\n * Semantic page observation for reviewed steps.\n * Every correlated rule for the shadow and frame piercing walker, the accessibility tree, visible text, the reader heuristic, the heading outline, the selection read, open graph extraction, language detection and the shadow and frame inventories lives in this file.\n */\n\n/** Serializable page node used by the observation engine; the live glue attaches the element. */\nexport interface pagenode {\n tag: string;\n selector: string;\n id: string;\n classes: string[];\n role: string;\n name: string;\n text: string;\n value: string;\n states: string[];\n hidden: boolean;\n children: pagenode[];\n element?: Element;\n}\n\n/** Deepest same origin frame nesting the walker pierces; deeper frames stay opaque so recursive self embedding cannot loop. */\nconst maxframedepth = 4;\n\nfunction elementstates(element: Element): string[] {\n const states: string[] = [];\n if (element.hasAttribute(\"disabled\") || element.getAttribute(\"aria-disabled\") === \"true\") states.push(\"disabled\");\n if (element instanceof HTMLInputElement && (element.type === \"checkbox\" || element.type === \"radio\") && element.checked) states.push(\"checked\");\n const expanded = element.getAttribute(\"aria-expanded\");\n if (expanded !== null) states.push(`expanded ${expanded}`);\n if (element.getAttribute(\"aria-selected\") === \"true\") states.push(\"selected\");\n if (element.hasAttribute(\"required\") || element.getAttribute(\"aria-required\") === \"true\") states.push(\"required\");\n if (element.hasAttribute(\"readonly\") || element.getAttribute(\"aria-readonly\") === \"true\") states.push(\"readonly\");\n if (element.getAttribute(\"aria-hidden\") === \"true\") states.push(\"hidden\");\n return states;\n}\n\nfunction elementvalue(element: Element): string {\n if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement) return element.value;\n return \"\";\n}\n\nfunction elementhidden(element: Element): boolean {\n if (element instanceof HTMLInputElement && element.type === \"hidden\") return true;\n if (element.hasAttribute(\"hidden\") || element.getAttribute(\"aria-hidden\") === \"true\") return true;\n try {\n const style = element.ownerDocument?.defaultView?.getComputedStyle(element);\n if (style && (style.display === \"none\" || style.visibility === \"hidden\")) return true;\n } catch { /* detached documents refuse computed styles; the node stays visible */ }\n return false;\n}\n\nfunction framenode(frame: HTMLIFrameElement, depth: number): pagenode {\n let content: Document | null = null;\n try { content = frame.contentDocument; } catch { content = null; }\n let sameorigin = false;\n try { sameorigin = content !== null && frame.contentWindow?.location.origin === location.origin; } catch { sameorigin = false; }\n const node = wrap(frame, depth);\n if (sameorigin && content && depth < maxframedepth) node.children.push(...wrapchildren(content, depth + 1));\n return node;\n}\n\nfunction wrap(element: Element, depth: number): pagenode {\n const shadow = element.shadowRoot;\n const node: pagenode = {\n tag: element.tagName.toLowerCase(),\n selector: elementselector(element),\n id: element.id,\n classes: [...element.classList],\n role: element.getAttribute(\"role\")?.toLowerCase() || implicitrole(element),\n name: elementlabel(element),\n text: owntext(element),\n value: elementvalue(element),\n states: elementstates(element),\n hidden: elementhidden(element),\n children: [],\n element,\n };\n if (shadow) node.children.push(...wrapchildren(shadow, depth));\n if (element instanceof HTMLIFrameElement) return framenode(element, depth);\n node.children.push(...wrapchildren(element, depth));\n return node;\n}\n\nfunction wrapchildren(scope: ParentNode, depth: number): pagenode[] {\n return [...scope.querySelectorAll(\":scope > *\")].map(child => wrap(child, depth));\n}\n\n/** Builds the serializable page node tree, piercing open shadow roots and same origin iframes. */\nexport function buildpagetree(scope: ParentNode): pagenode {\n const root: pagenode = {\n tag: \"#document\",\n selector: \"\",\n id: \"\",\n classes: [],\n role: \"document\",\n name: \"\",\n text: \"\",\n value: \"\",\n states: [],\n hidden: false,\n children: [],\n };\n if (scope instanceof Document) {\n root.children = scope.documentElement ? [wrap(scope.documentElement, 0)] : [];\n } else {\n root.children = [...wrapchildren(scope, 0)];\n }\n return root;\n}\n\nfunction countnodes(node: a11ynode): number {\n return 1 + node.children.reduce((total, child) => total + countnodes(child), 0);\n}\n\n/** Converts one page node tree into the accessibility tree with roles, names, states and child refs; hidden subtrees stay excluded. */\nexport function builda11ytree(node: pagenode): a11ynode {\n const children = node.children.filter(child => !child.hidden).map(builda11ytree);\n return {\n role: node.role || \"generic\",\n name: node.name,\n states: node.states,\n ...(node.value ? { value: node.value } : {}),\n childcount: children.length,\n children,\n };\n}\n\n/** Returns the rendered text of each visible element; hidden nodes and their subtrees stay excluded. */\nexport function visibleentries(node: pagenode): Array<{ selector: string; text: string }> {\n if (node.hidden) return [];\n const entries: Array<{ selector: string; text: string }> = node.text ? [{ selector: node.selector || node.tag, text: node.text }] : [];\n for (const child of node.children) entries.push(...visibleentries(child));\n return entries;\n}\n\n/** Joins the rendered text of every visible node into one stream. */\nexport function visibletext(node: pagenode): string {\n return visibleentries(node).map(entry => entry.text).join(\" \");\n}\n\nfunction nodetextlength(node: pagenode): number {\n return node.text.length + node.children.reduce((total, child) => total + nodetextlength(child), 0);\n}\n\nfunction nodelinktext(node: pagenode): number {\n const own = node.tag === \"a\" ? node.text.length : 0;\n return own + node.children.reduce((total, child) => total + nodelinktext(child), 0);\n}\n\nfunction wordsin(text: string): number {\n return text.split(/\\s+/).filter(Boolean).length;\n}\n\nfunction findbyline(node: pagenode): string {\n const markers = [\"byline\", \"author\"];\n const direct = node.classes.some(item => markers.some(marker => item.toLowerCase().includes(marker))) || markers.some(marker => node.id.toLowerCase().includes(marker));\n if (direct && node.text) return node.text;\n for (const child of node.children) {\n const found = findbyline(child);\n if (found) return found;\n }\n return \"\";\n}\n\nfunction findheading(node: pagenode, tags: string[]): string {\n if (tags.includes(node.tag) && node.text) return node.text;\n for (const child of node.children) {\n const found = findheading(child, tags);\n if (found) return found;\n }\n return \"\";\n}\n\n/** Scores page nodes by text density and splits the densest article region into reader blocks, separating them from page chrome. */\nexport function buildreader(root: pagenode, title: string): readerarticle {\n let best: pagenode | undefined;\n let bestscore = 0;\n const walk = (node: pagenode): void => {\n if (node.tag !== \"#document\") {\n const length = nodetextlength(node);\n const links = nodelinktext(node);\n const score = length * (1 - (length > 0 ? links / length : 0));\n if (score > bestscore) { bestscore = score; best = node; }\n }\n for (const child of node.children) walk(child);\n };\n walk(root);\n const article = best ?? root;\n const blocks = article.children.filter(child => !child.hidden && child.text).map(child => ({ kind: child.tag, text: child.text, words: wordsin(child.text) }));\n const ownblock = article.text ? [{ kind: article.tag, text: article.text, words: wordsin(article.text) }] : [];\n const allblocks = [...ownblock, ...blocks];\n return {\n title: findheading(article, [\"h1\"]) || findheading(root, [\"h1\"]) || title,\n byline: findbyline(article) || findbyline(root),\n blocks: allblocks,\n words: allblocks.reduce((total, block) => total + block.words, 0),\n characters: allblocks.reduce((total, block) => total + block.text.length, 0),\n };\n}\n\n/** Returns the title and headings outline of the page. */\nexport function pageoutline(root: pagenode, title: string): { title: string; headings: Array<{ level: number; text: string }> } {\n const headings: Array<{ level: number; text: string }> = [];\n const walk = (node: pagenode): void => {\n const level = /^h([1-6])$/.exec(node.tag);\n if (level && node.text) headings.push({ level: Number.parseInt(level[1] as string, 10), text: node.text });\n for (const child of node.children) walk(child);\n };\n walk(root);\n return { title: findheading(root, [\"h1\"]) || title, headings };\n}\n\n/** Reads the text of the current user selection through the document selection api. */\nexport function captureselection(root: { getSelection?: () => { toString(): string } | null }): { text: string; length: number } {\n const selection = root.getSelection?.() ?? null;\n const text = selection ? clean(selection.toString()) : \"\";\n return { text, length: text.length };\n}\n\n/** Extracts open graph meta properties and structured data payloads; malformed structured payloads are refused and counted. */\nexport function opengraphfields(meta: Array<{ property: string; name: string; content: string }>, jsonld: string[]): { graph: Record<string, string>; structured: unknown[]; refused: number } {\n const graph: Record<string, string> = {};\n for (const entry of meta) {\n if (entry.property.startsWith(\"og:\") && entry.content) graph[entry.property] = entry.content;\n }\n const structured: unknown[] = [];\n let refused = 0;\n for (const raw of jsonld) {\n try { structured.push(JSON.parse(raw)); } catch { refused += 1; }\n }\n return { graph, structured, refused };\n}\n\nconst stopwords: Record<string, string[]> = {\n en: [\"the\", \"is\", \"at\", \"which\", \"on\", \"and\", \"of\", \"to\", \"in\", \"that\", \"it\", \"with\"],\n pt: [\"de\", \"que\", \"n\u00E3o\", \"uma\", \"para\", \"com\", \"por\", \"mais\", \"como\", \"p\u00E1gina\", \"este\", \"voc\u00EA\"],\n es: [\"que\", \"el\", \"las\", \"los\", \"por\", \"una\", \"para\", \"con\", \"como\", \"p\u00E1gina\", \"m\u00E1s\", \"este\"],\n fr: [\"le\", \"les\", \"des\", \"que\", \"pour\", \"dans\", \"est\", \"sur\", \"avec\", \"page\", \"plus\", \"cette\"],\n de: [\"der\", \"die\", \"und\", \"das\", \"ist\", \"von\", \"mit\", \"f\u00FCr\", \"auf\", \"den\", \"nicht\", \"seite\"],\n it: [\"che\", \"il\", \"la\", \"per\", \"una\", \"del\", \"sono\", \"non\", \"con\", \"pagina\", \"pi\u00F9\", \"questo\"],\n nl: [\"het\", \"een\", \"en\", \"van\", \"is\", \"dat\", \"op\", \"te\", \"voor\", \"met\", \"niet\", \"pagina\"],\n};\n\n/** Detects the language of extracted text from stopword frequency; an undetermined text returns an empty code. */\nexport function detecttextlanguage(text: string): string {\n const words = text.toLowerCase().split(/[^a-z\u00E0-\u00FF]+/).filter(Boolean);\n if (words.length === 0) return \"\";\n let best = \"\";\n let bestscore = 0;\n for (const [language, dictionary] of Object.entries(stopwords)) {\n const score = words.filter(word => dictionary.includes(word)).length;\n if (score > bestscore) { bestscore = score; best = language; }\n }\n return best;\n}\n\n/** Tags extracted text with its detected language code, routing it to the matching language stream. */\nexport function taglanguage(text: string): { text: string; language: string } {\n return { text, language: detecttextlanguage(text) };\n}\n\n/** Resolves the page language from the document lang attribute, the content language meta and the content signals, in that order. */\nexport function documentlanguage(signals: { lang: string; meta: string; text: string }): { language: string; source: string } {\n if (signals.lang.trim()) return { language: signals.lang.trim(), source: \"document\" };\n if (signals.meta.trim()) return { language: signals.meta.trim(), source: \"meta\" };\n return { language: detecttextlanguage(signals.text), source: \"content\" };\n}\n\n/** Lists the host paths of every open shadow root nested inside one scope tree. */\nexport function shadowpaths(scope: scopetree<candidatefields>): string[] {\n const paths: string[] = [];\n const walk = (tree: scopetree<candidatefields>, prefix: string): void => {\n for (const shadow of tree.shadows) {\n if (!shadow.host) continue;\n const path = prefix ? `${prefix} > ${shadow.host.selector}` : shadow.host.selector;\n paths.push(path);\n walk(shadow, path);\n }\n };\n walk(scope, \"\");\n return paths;\n}\n\n/** Enumerates the iframes of one document with their origins and sizes; cross origin frames report no origin. */\nexport function framelist(root: Document): Array<{ index: number; origin: string; sameorigin: boolean; width: number; height: number }> {\n return [...root.querySelectorAll(\"iframe\")].map((frame, index) => {\n let origin = \"\";\n try { origin = frame.contentWindow?.location.origin ?? \"\"; } catch { origin = \"\"; }\n const rect = frame.getBoundingClientRect();\n return { index, origin, sameorigin: origin !== \"\" && origin === location.origin, width: Math.round(rect.width), height: Math.round(rect.height) };\n });\n}\n\nfunction stepoptions(step: toolstep): Record<string, unknown> {\n try { return parseoptions(step); } catch { return {} as Record<string, unknown>; }\n}\n\n/** Runs one passive page observation after the background policy gate; frame routed steps receive their frame document as the root. */\nexport function runpageobservation(step: toolstep, target: Element | null, root: Document = document): stepresult | Promise<stepresult> {\n switch (step.kind) {\n case \"a11ytree\": {\n const tree = builda11ytree(buildpagetree(root));\n const count = countnodes(tree);\n return { ok: true, summary: `Captured the accessibility tree with ${count} node${count === 1 ? \"\" : \"s\"}.`, details: { tree, nodecount: count } };\n }\n case \"readvisible\": {\n const scope: ParentNode = target ?? root;\n const tree = buildpagetree(scope);\n const entries = visibleentries(tree);\n return { ok: true, summary: `Read the rendered text of ${entries.length} visible element${entries.length === 1 ? \"\" : \"s\"}.`, details: { entries, text: visibletext(tree) } };\n }\n case \"readertree\": {\n const article = buildreader(buildpagetree(root), root.title);\n return { ok: true, summary: `Extracted the reader view with ${article.blocks.length} block${article.blocks.length === 1 ? \"\" : \"s\"} and ${article.words} words.`, details: { article } };\n }\n case \"readoutline\": {\n const outline = pageoutline(buildpagetree(root), root.title);\n return { ok: true, summary: `Read the outline with ${outline.headings.length} heading${outline.headings.length === 1 ? \"\" : \"s\"}.`, details: { title: outline.title, headings: outline.headings } };\n }\n case \"readselection\": {\n const selection = captureselection(root);\n return { ok: true, summary: selection.text ? `Read ${selection.length} characters of the current selection.` : \"No text is currently selected.\", details: { text: selection.text, length: selection.length } };\n }\n case \"readopengraph\": {\n const meta = [...root.querySelectorAll(\"meta\")].map(element => ({ property: element.getAttribute(\"property\") ?? \"\", name: element.getAttribute(\"name\") ?? \"\", content: element.getAttribute(\"content\") ?? \"\" }));\n const jsonld = [...root.querySelectorAll('script[type=\"application/ld+json\"]')].map(element => element.textContent ?? \"\");\n const fields = opengraphfields(meta, jsonld);\n return { ok: true, summary: `Read ${Object.keys(fields.graph).length} open graph entr${Object.keys(fields.graph).length === 1 ? \"y\" : \"ies\"} and ${fields.structured.length} structured payload${fields.structured.length === 1 ? \"\" : \"s\"}${fields.refused > 0 ? `; ${fields.refused} malformed payload${fields.refused === 1 ? \" was\" : \"s were\"} refused` : \"\"}.`, details: { graph: fields.graph, structured: fields.structured, refused: fields.refused } };\n }\n case \"readlang\": {\n const metatag = root.querySelector('meta[http-equiv=\"content-language\"]')?.getAttribute(\"content\") ?? \"\";\n const outcome = documentlanguage({ lang: root.documentElement?.getAttribute(\"lang\") ?? \"\", meta: metatag, text: root.body?.innerText ?? \"\" });\n return { ok: true, summary: `Detected page language ${outcome.language || \"unknown\"} from the ${outcome.source} signal.`, details: { language: outcome.language, source: outcome.source } };\n }\n case \"detectlanguage\": {\n const options = stepoptions(step);\n const text = typeof options.text === \"string\" && options.text ? options.text : target?.textContent ?? root.body?.innerText ?? \"\";\n const routed = taglanguage(clean(text));\n return { ok: routed.language !== \"\", summary: routed.language ? `Detected language ${routed.language} for the extracted text.` : \"The extracted text language is undetermined.\", details: { language: routed.language, routed } };\n }\n case \"listshadow\": {\n const paths = shadowpaths(describescopes(root));\n return { ok: true, summary: `Listed ${paths.length} open shadow root${paths.length === 1 ? \"\" : \"s\"}.`, details: { shadows: paths } };\n }\n case \"listframes\": {\n const frames = framelist(root);\n return { ok: true, summary: `Listed ${frames.length} iframe${frames.length === 1 ? \"\" : \"s\"}.`, details: { frames } };\n }\n default: return { ok: false, summary: \"Unsupported page observation.\" };\n }\n}\n", "import type { bannerreport, listpattern, tableshape, toolstep } from \"../types.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { clean, elementselector } from \"./pageresolve.js\";\n\n/**\n * Page shape detection for reviewed steps.\n * Every correlated rule for repeated list detection, table shape normalization, pagination estimates, infinite scroll ranges, virtualization, lazy images, sticky overlays, scroll locks, consent banner shapes, template classification, section fingerprints and scroll positions lives in this file.\n */\n\n/** Serializable sibling sample of one container element used by the list detector. */\nexport interface siblingsample {\n container: string;\n children: Array<{ tag: string; classes: string; text: string; selector: string }>;\n}\n\n/** Finds repeated item lists with a shared item selector from sibling samples. */\nexport function detectlistpatterns(samples: siblingsample[]): listpattern[] {\n const patterns: listpattern[] = [];\n for (const sample of samples) {\n const groups = new Map<string, Array<{ tag: string; classes: string; text: string }>>();\n for (const child of sample.children) {\n const key = `${child.tag}|${child.classes}`;\n const group = groups.get(key) ?? [];\n group.push(child);\n groups.set(key, group);\n }\n for (const [key, group] of groups) {\n if (group.length < 2) continue;\n if (!group.some(item => item.text)) continue;\n const [tag, classes] = key.split(\"|\") as [string, string?];\n const classpart = (classes ?? \"\").split(\" \").filter(Boolean).map(name => `.${name}`).join(\"\");\n patterns.push({ container: sample.container, itemselector: `${tag}${classpart}`, repeat: group.length, samples: group.map(item => item.text).filter(Boolean) });\n }\n }\n return patterns;\n}\n\n/** Normalizes one table row structure into header row, column specs and caption. */\nexport function normalizetable(rows: Array<{ cells: string[]; header: boolean }>, caption: string): { headers: string[]; columns: Array<{ label: string; cells: number }>; rows: number; caption: string } {\n const firstheader = rows.find(row => row.header);\n const headers = firstheader?.cells ?? [];\n const body = firstheader ? rows.filter(row => row !== firstheader) : rows;\n const width = rows.reduce((largest, row) => Math.max(largest, row.cells.length), 0);\n const columns: Array<{ label: string; cells: number }> = [];\n for (let index = 0; index < width; index += 1) {\n const label = headers[index] ?? `column ${index + 1}`;\n const cells = body.filter(row => Boolean((row.cells[index] ?? \"\").trim())).length;\n columns.push({ label, cells });\n }\n return { headers, columns, rows: body.length, caption };\n}\n\n/** Counts pagination entries and estimates the total page count from the numbers they carry. */\nexport function paginationestimate(entries: Array<{ text: string; selector: string; current: boolean }>): { current: number; total: number; links: number; pages: number[] } {\n const pages: number[] = [];\n let current = 0;\n for (const entry of entries) {\n const parsed = /^\\d+$/.exec(entry.text.trim());\n if (parsed) {\n const page = Number.parseInt(parsed[0] as string, 10);\n pages.push(page);\n if (entry.current) current = page;\n }\n }\n const total = Math.max(0, ...pages, current);\n return { current, total, links: entries.length, pages };\n}\n\n/** Serializable scroll range input of one scrollable container. */\nexport interface scrollrangeshape {\n selector: string;\n scrollheight: number;\n clientheight: number;\n triggers: string[];\n}\n\n/** Flags infinite scroll containers from measured scroll ranges and their load more triggers. */\nexport function infinitescrollranges(ranges: scrollrangeshape[]): Array<{ selector: string; scrollrange: number; triggers: string[] }> {\n return ranges\n .filter(range => range.scrollheight > range.clientheight && range.triggers.length > 0)\n .map(range => ({ selector: range.selector, scrollrange: range.scrollheight - range.clientheight, triggers: range.triggers }));\n}\n\n/** Detects virtualized lists whose uniform rendered rows do not fill the measured scroll range. */\nexport function virtualizedcontainers(containers: Array<{ selector: string; scrollheight: number; rows: Array<{ selector: string; height: number; classes: string }> }>): Array<{ selector: string; rendered: number; estimated: number }> {\n const results: Array<{ selector: string; rendered: number; estimated: number }> = [];\n for (const container of containers) {\n const first = container.rows[0];\n if (!first || container.rows.length < 2 || first.height <= 0) continue;\n if (!container.rows.every(row => row.height === first.height)) continue;\n if (container.scrollheight <= container.rows.length * first.height) continue;\n results.push({ selector: container.selector, rendered: container.rows.length, estimated: Math.floor(container.scrollheight / first.height) });\n }\n return results;\n}\n\n/** Serializable image shape used by the lazy detector. */\nexport interface imageshape {\n selector: string;\n src: string;\n datasrc: string;\n loading: string;\n width: number;\n height: number;\n}\n\n/** Detects lazy loaded images from loading attributes and deferred sources, and placeholder states from inline data or empty sources. */\nexport function lazysurvey(images: imageshape[]): { lazy: Array<{ selector: string; reason: string }>; placeholders: Array<{ selector: string; reason: string }> } {\n const lazy: Array<{ selector: string; reason: string }> = [];\n const placeholders: Array<{ selector: string; reason: string }> = [];\n for (const image of images) {\n if (image.loading === \"lazy\") lazy.push({ selector: image.selector, reason: \"loading attribute\" });\n else if (image.datasrc) lazy.push({ selector: image.selector, reason: \"deferred source\" });\n if (!image.src) placeholders.push({ selector: image.selector, reason: \"empty source\" });\n else if (image.src.startsWith(\"data:\")) placeholders.push({ selector: image.selector, reason: \"inline data placeholder\" });\n }\n return { lazy, placeholders };\n}\n\n/** Detects sticky headers and overlays from fixed and sticky geometry measured against the viewport. */\nexport function overlaygeometry(elements: Array<{ selector: string; position: string; top: number; height: number; width: number }>, viewport: { width: number; height: number }): Array<{ selector: string; position: string; coverage: number; hides: boolean }> {\n const area = viewport.width * viewport.height;\n return elements\n .filter(element => (element.position === \"sticky\" || element.position === \"fixed\") && element.top <= 0 && element.height > 0)\n .map(element => {\n const coverage = area > 0 ? (element.height * element.width) / area : 0;\n return { selector: element.selector, position: element.position, coverage: Math.round(coverage * 1000) / 1000, hides: coverage >= overlaythreshold };\n });\n}\n\n/** Detects scroll locks and modal states from body overflow, body position and modal presence signals. */\nexport function scrolllockstate(signals: { bodyoverflow: string; htmloverflow: string; bodyposition: string; modal: boolean; scrollable: boolean }): { locked: boolean; reasons: string[]; scrollable: boolean } {\n const reasons: string[] = [];\n if (signals.bodyoverflow.includes(\"hidden\") || signals.htmloverflow.includes(\"hidden\")) reasons.push(\"overflow hidden\");\n if (signals.bodyposition === \"fixed\") reasons.push(\"fixed body\");\n if (signals.modal) reasons.push(\"modal open\");\n return { locked: reasons.length > 0, reasons, scrollable: signals.scrollable };\n}\n\n/** Serializable consent banner candidate collected by the banner watcher glue. */\nexport interface bannercandidate {\n selector: string;\n id: string;\n classes: string[];\n text: string;\n controls: string[];\n}\n\n/** Consent banner keywords the shape matcher recognizes across locales. */\nexport const consentkeywords = [\"cookie\", \"consent\", \"gdpr\", \"lgpd\", \"privacy\", \"ccpa\"];\n\n/** Matches consent banner shapes against the known keyword vocabulary and reports their controls. */\nexport function bannermatches(candidates: bannercandidate[], at: number): bannerreport[] {\n const reports: bannerreport[] = [];\n for (const candidate of candidates) {\n const haystack = `${candidate.id} ${candidate.classes.join(\" \")} ${candidate.text}`.toLowerCase();\n const keyword = consentkeywords.find(word => haystack.includes(word));\n if (!keyword) continue;\n if (!candidate.text && candidate.controls.length === 0) continue;\n reports.push({ kind: keyword, selector: candidate.selector, text: candidate.text.slice(0, 200), controls: candidate.controls, at });\n }\n return reports;\n}\n\n/** Classifies the page template from its dominant structural signals. */\nexport function classifytemplate(signals: { paragraphs: number; headings: number; lists: number; tables: number; forms: number; inputs: number; password: boolean }): string {\n if (signals.password) return \"login\";\n if (signals.paragraphs >= 3) return \"article\";\n if (signals.tables > 0) return \"table\";\n if (signals.forms > 0 && signals.inputs > 0) return \"form\";\n if (signals.lists > 0) return \"list\";\n return \"generic\";\n}\n\n/** Computes a stable structural fingerprint of one page section from its tag, attributes, child count and text length. */\nexport function sectionfingerprint(section: { tag: string; attributes: Record<string, string>; children: number; textlength: number }): string {\n const canonical = [section.tag, String(section.children), String(section.textlength), ...Object.keys(section.attributes).sort().map(key => `${key}=${section.attributes[key] ?? \"\"}`)].join(\"|\");\n let hash = 5381;\n for (let index = 0; index < canonical.length; index += 1) hash = ((hash << 5) + hash + canonical.charCodeAt(index)) >>> 0;\n return `fp${hash.toString(16)}`;\n}\n\n/** Normalizes the scroll position of the window and its scrollable containers with edge flags. */\nexport function scrollreport(window: { scrollx: number; scrolly: number; scrollheight: number; clientheight: number }, containers: Array<{ selector: string; scrolltop: number; scrollleft: number; scrollheight: number; clientheight: number }>): { window: { x: number; y: number; attop: boolean; atbottom: boolean; height: number }; containers: Array<{ selector: string; scrolltop: number; scrollleft: number; scrollrange: number; atbottom: boolean }> } {\n const range = Math.max(0, window.scrollheight - window.clientheight);\n return {\n window: { x: window.scrollx, y: window.scrolly, attop: window.scrolly <= 0, atbottom: window.scrolly >= range, height: window.scrollheight },\n containers: containers.map(container => {\n const containerrange = Math.max(0, container.scrollheight - container.clientheight);\n return { selector: container.selector, scrolltop: container.scrolltop, scrollleft: container.scrollleft, scrollrange: containerrange, atbottom: container.scrolltop >= containerrange };\n }),\n };\n}\n\nconst loadmorepattern = /(load more|show more|see more|ver mais|carregar mais|load older|afficher plus|mehr anzeigen)/i;\nconst paginationtext = /^(next|prev|previous|last|first|next page|previous page|\u00BB|\u00AB|\u203A|\u2039|\\d+)$/i;\n/** Viewport coverage fraction at which a sticky or fixed overlay is reported as hiding content. */\nconst overlaythreshold = 0.25;\n\nfunction signatureof(element: Element): string {\n return `${element.tagName.toLowerCase()}|${[...element.classList].sort().join(\" \")}`;\n}\n\n/** Collects the sibling samples of one document for the list detector. */\nexport function collectsiblings(root: Document): siblingsample[] {\n const samples: siblingsample[] = [];\n for (const element of [...root.querySelectorAll(\"*\")]) {\n const children = [...element.children];\n if (children.length < 2) continue;\n const counts = new Map<string, number>();\n for (const child of children) {\n const key = signatureof(child);\n counts.set(key, (counts.get(key) ?? 0) + 1);\n }\n if (![...counts.values()].some(count => count >= 2)) continue;\n samples.push({\n container: elementselector(element),\n children: children.map(child => ({ tag: child.tagName.toLowerCase(), classes: [...child.classList].sort().join(\" \"), text: clean(child.textContent ?? \"\"), selector: elementselector(child) })),\n });\n }\n return samples;\n}\n\n/** Collects the data table structures of one document for the table shape normalizer. */\nexport function collecttables(root: Document): Array<{ selector: string; rows: Array<{ cells: string[]; header: boolean }>; caption: string }> {\n return [...root.querySelectorAll(\"table\")].map(table => ({\n selector: elementselector(table),\n rows: [...table.querySelectorAll(\"tr\")].map(row => ({ cells: [...row.querySelectorAll(\"th, td\")].map(cell => clean(cell.textContent ?? \"\")), header: Boolean(row.querySelector(\"th\")) })),\n caption: clean(table.querySelector(\"caption\")?.textContent ?? \"\"),\n }));\n}\n\nfunction collectpagination(root: Document): Array<{ text: string; selector: string; current: boolean }> {\n const entries: Array<{ text: string; selector: string; current: boolean }> = [];\n for (const element of [...root.querySelectorAll(\"a[href], button, [role=button], [role=link], li, span\")]) {\n const text = clean(element.textContent ?? \"\");\n if (!text || !paginationtext.test(text)) continue;\n if (!element.closest(\"nav, footer, [class*=pag i], [id*=pag i]\")) continue;\n const current = element.getAttribute(\"aria-current\") === \"page\" || [...element.classList].some(name => /current|active|selecionado/i.test(name));\n entries.push({ text, selector: elementselector(element), current });\n }\n return entries;\n}\n\nfunction collecttriggers(scope: ParentNode): string[] {\n const triggers: string[] = [];\n for (const element of [...scope.querySelectorAll(\"button, a[href], [role=button], [class*=loading i], [class*=sentinel i], [class*=spinner i]\")]) {\n const label = clean(element.getAttribute(\"aria-label\") ?? element.textContent ?? \"\");\n if (loadmorepattern.test(label)) triggers.push(elementselector(element));\n }\n return triggers;\n}\n\nfunction collectscrollranges(root: Document): scrollrangeshape[] {\n const ranges: scrollrangeshape[] = [];\n const scrolling = root.scrollingElement ?? root.documentElement;\n const viewheight = root.defaultView?.innerHeight ?? 0;\n if (scrolling && scrolling.scrollHeight > viewheight) ranges.push({ selector: \"window\", scrollheight: scrolling.scrollHeight, clientheight: viewheight, triggers: collecttriggers(root) });\n for (const element of [...root.querySelectorAll(\"*\")]) {\n if (!(element instanceof HTMLElement)) continue;\n if (element.scrollHeight <= element.clientHeight) continue;\n ranges.push({ selector: elementselector(element), scrollheight: element.scrollHeight, clientheight: element.clientHeight, triggers: collecttriggers(element) });\n }\n return ranges;\n}\n\nfunction collectvirtual(root: Document): Array<{ selector: string; scrollheight: number; rows: Array<{ selector: string; height: number; classes: string }> }> {\n const containers: Array<{ selector: string; scrollheight: number; rows: Array<{ selector: string; height: number; classes: string }> }> = [];\n for (const element of [...root.querySelectorAll(\"*\")]) {\n const children = [...element.children];\n const first = children[0];\n if (!first || children.length < 2) continue;\n if (!children.every(child => signatureof(child) === signatureof(first))) continue;\n const heights = children.map(child => child.getBoundingClientRect().height);\n if (!heights.every(height => height > 0 && height === heights[0])) continue;\n containers.push({ selector: elementselector(element), scrollheight: element.scrollHeight, rows: children.map(child => ({ selector: elementselector(child), height: child.getBoundingClientRect().height, classes: [...child.classList].join(\" \") })) });\n }\n return containers;\n}\n\nfunction collectimages(root: Document): imageshape[] {\n return [...root.querySelectorAll(\"img\")].map(image => ({\n selector: elementselector(image),\n src: image.getAttribute(\"src\") ?? \"\",\n datasrc: image.getAttribute(\"data-src\") ?? image.getAttribute(\"data-original\") ?? \"\",\n loading: image.getAttribute(\"loading\") ?? \"\",\n width: image.naturalWidth,\n height: image.naturalHeight,\n }));\n}\n\nfunction collectoverlays(root: Document): Array<{ selector: string; position: string; top: number; height: number; width: number }> {\n const elements: Array<{ selector: string; position: string; top: number; height: number; width: number }> = [];\n for (const element of [...root.querySelectorAll(\"*\")]) {\n if (!(element instanceof HTMLElement)) continue;\n const view = element.ownerDocument.defaultView;\n const position = view ? view.getComputedStyle(element).position : \"\";\n if (position !== \"sticky\" && position !== \"fixed\") continue;\n const rect = element.getBoundingClientRect();\n elements.push({ selector: elementselector(element), position, top: rect.top, height: rect.height, width: rect.width });\n }\n return elements;\n}\n\nfunction collectlocksignals(root: Document): { bodyoverflow: string; htmloverflow: string; bodyposition: string; modal: boolean; scrollable: boolean } {\n const view = root.defaultView;\n const bodystyle = root.body ? (view ? view.getComputedStyle(root.body) : undefined) : undefined;\n const htmlstyle = view ? view.getComputedStyle(root.documentElement) : undefined;\n return {\n bodyoverflow: bodystyle?.overflow ?? \"\",\n htmloverflow: htmlstyle?.overflow ?? \"\",\n bodyposition: bodystyle?.position ?? \"\",\n modal: Boolean(root.querySelector(\"dialog[open], [aria-modal=true]\")),\n scrollable: root.documentElement.scrollHeight > root.documentElement.clientHeight,\n };\n}\n\nconst bannerselector = '[id*=\"cookie\" i], [class*=\"cookie\" i], [id*=\"consent\" i], [class*=\"consent\" i], [id*=\"gdpr\" i], [class*=\"gdpr\" i], [id*=\"privacy\" i], [class*=\"privacy\" i], [id*=\"banner\" i], [class*=\"banner\" i], dialog, [role=\"dialog\"], [aria-modal=\"true\"]';\n\n/** Collects the consent banner candidates of one document for the banner shape matcher. */\nexport function collectbannercandidates(root: Document): bannercandidate[] {\n const found = [...root.querySelectorAll(bannerselector)];\n return found\n .filter(element => !found.some(other => other !== element && other.contains(element)))\n .map(element => ({\n selector: elementselector(element),\n id: element.id,\n classes: [...element.classList],\n text: clean(element.textContent ?? \"\").slice(0, 200),\n controls: [...element.querySelectorAll(\"button, a[href], [role=button]\")].map(control => clean(control.getAttribute(\"aria-label\") ?? control.textContent ?? \"\")).filter(Boolean),\n }));\n}\n\n/** Runs one page shape detection after the background policy gate; frame routed steps receive their frame document as the root. */\nexport function runpagedetection(step: toolstep, target: Element | null, root: Document = document): stepresult | Promise<stepresult> {\n switch (step.kind) {\n case \"detectlists\": {\n const patterns = detectlistpatterns(collectsiblings(root));\n return { ok: true, summary: `Detected ${patterns.length} repeated list${patterns.length === 1 ? \"\" : \"s\"}.`, details: { lists: patterns } };\n }\n case \"detecttables\": {\n const tables: tableshape[] = collecttables(root).map(entry => {\n const shape = normalizetable(entry.rows, entry.caption);\n return { selector: entry.selector, headers: shape.headers, columns: shape.columns, rows: shape.rows, caption: shape.caption };\n });\n return { ok: true, summary: `Detected ${tables.length} data table${tables.length === 1 ? \"\" : \"s\"}.`, details: { tables } };\n }\n case \"countpages\": {\n const estimate = paginationestimate(collectpagination(root));\n return { ok: true, summary: `Counted ${estimate.links} pagination entr${estimate.links === 1 ? \"y\" : \"ies\"} and estimated ${estimate.total} total page${estimate.total === 1 ? \"\" : \"s\"}.`, details: { current: estimate.current, total: estimate.total, links: estimate.links, pages: estimate.pages } };\n }\n case \"detectinfinitescroll\": {\n const containers = infinitescrollranges(collectscrollranges(root));\n return { ok: true, summary: `Detected ${containers.length} infinite scroll container${containers.length === 1 ? \"\" : \"s\"}.`, details: { containers } };\n }\n case \"detectvirtual\": {\n const containers = virtualizedcontainers(collectvirtual(root));\n return { ok: true, summary: `Detected ${containers.length} virtualized list${containers.length === 1 ? \"\" : \"s\"}.`, details: { containers } };\n }\n case \"detectlazy\": {\n const survey = lazysurvey(collectimages(root));\n return { ok: true, summary: `Detected ${survey.lazy.length} lazy image${survey.lazy.length === 1 ? \"\" : \"s\"} and ${survey.placeholders.length} placeholder${survey.placeholders.length === 1 ? \"\" : \"s\"}.`, details: { lazy: survey.lazy, placeholders: survey.placeholders } };\n }\n case \"detectsticky\": {\n const overlays = overlaygeometry(collectoverlays(root), { width: root.defaultView?.innerWidth ?? 0, height: root.defaultView?.innerHeight ?? 0 });\n return { ok: true, summary: `Detected ${overlays.length} sticky or fixed overlay${overlays.length === 1 ? \"\" : \"s\"}.`, details: { overlays } };\n }\n case \"detectscrolllock\": {\n const lock = scrolllockstate(collectlocksignals(root));\n return { ok: true, summary: lock.locked ? `Scroll is locked: ${lock.reasons.join(\", \")}.` : \"Scroll is not locked.\", details: { locked: lock.locked, reasons: lock.reasons, scrollable: lock.scrollable } };\n }\n case \"classifypage\": {\n const signals = {\n paragraphs: root.querySelectorAll(\"p\").length,\n headings: root.querySelectorAll(\"h1, h2, h3, h4, h5, h6\").length,\n lists: root.querySelectorAll(\"ul, ol\").length,\n tables: root.querySelectorAll(\"table\").length,\n forms: root.querySelectorAll(\"form\").length,\n inputs: root.querySelectorAll(\"input, textarea, select\").length,\n password: Boolean(root.querySelector(\"input[type=password]\")),\n };\n const template = classifytemplate(signals);\n const fingerprint = sectionfingerprint({ tag: \"body\", attributes: {}, children: root.body?.children.length ?? 0, textlength: (root.body?.innerText ?? \"\").length });\n return { ok: true, summary: `Classified the page template as ${template}.`, details: { template, fingerprint } };\n }\n case \"fingerprintsection\": {\n if (!target) return { ok: false, summary: \"Fingerprint target is no longer available.\" };\n const attributes: Record<string, string> = {};\n for (const attribute of [...target.attributes]) attributes[attribute.name] = attribute.value;\n const fingerprint = sectionfingerprint({ tag: target.tagName.toLowerCase(), attributes, children: target.children.length, textlength: (target.textContent ?? \"\").length });\n return { ok: true, summary: `Computed section fingerprint ${fingerprint}.`, details: { fingerprint, section: elementselector(target) } };\n }\n case \"readscrollpos\": {\n const report = scrollreport(\n { scrollx: root.defaultView?.scrollX ?? 0, scrolly: root.defaultView?.scrollY ?? 0, scrollheight: root.documentElement.scrollHeight, clientheight: root.defaultView?.innerHeight ?? 0 },\n [...root.querySelectorAll(\"*\")].filter(element => element instanceof HTMLElement && element.scrollHeight > element.clientHeight).map(element => ({ selector: elementselector(element), scrolltop: element.scrollTop, scrollleft: element.scrollLeft, scrollheight: element.scrollHeight, clientheight: element.clientHeight })),\n );\n return { ok: true, summary: `Read the scroll position at ${Math.round(report.window.x)},${Math.round(report.window.y)} with ${report.containers.length} scrollable container${report.containers.length === 1 ? \"\" : \"s\"}.`, details: { scroll: report } };\n }\n default: return { ok: false, summary: \"Unsupported page detection.\" };\n }\n}\n", "import type { bannerreport, diffentry, focusevent, jsonstate, mutationevent, mutationwatch, quietrule, selectorcandidate, toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { bannermatches, collectbannercandidates } from \"./pagedetect.js\";\nimport { clean, elementselector } from \"./pageresolve.js\";\n\n/**\n * Watched page observation for reviewed steps.\n * Every correlated rule for watch option parsing, mutation batching, focus tracking, the network quiet probe, the snapshot diff engine, the json state scanner and the selector scorer lives in this file.\n */\n\n/** Internal sampling cadence used when a watch step reviews no poll interval. */\nconst defaultpoll = 250;\n\n/** Parsed watch options of one watch step: the mutationwatch fields plus the poll interval. */\nexport type watchoptions = mutationwatch & { watchid: string; poll: number };\n\n/** Parses the reviewed watch options of one watch step into its mutationwatch shape with a poll interval. */\nexport function parsewatchoptions(step: toolstep, fallbackid: string): watchoptions {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const scopes = Array.isArray(options.scopes) ? options.scopes.filter((item): item is string => typeof item === \"string\" && item.trim().length > 0) : undefined;\n const events = Array.isArray(options.events) ? options.events.filter((item): item is string => typeof item === \"string\" && item.trim().length > 0) : undefined;\n const lifetime = typeof options.lifetime === \"number\" && Number.isFinite(options.lifetime) && options.lifetime > 0 ? options.lifetime : 0;\n return {\n watchid: typeof options.watchid === \"string\" && options.watchid.trim() ? options.watchid : fallbackid,\n ...(scopes ? { scopes } : {}),\n ...(events ? { events } : {}),\n lifetime,\n poll: typeof options.poll === \"number\" && Number.isFinite(options.poll) && options.poll >= 0 ? options.poll : defaultpoll,\n };\n}\n\n/** Batches mutation records so records arriving inside one throttle window flush to the bridge together. */\nexport function batchmutations(records: mutationevent[], windowms: number): mutationevent[][] {\n const batches: mutationevent[][] = [];\n let current: mutationevent[] = [];\n let opened = -1;\n for (const record of records) {\n if (current.length === 0 || (windowms > 0 && record.at - opened >= windowms)) {\n if (current.length > 0) batches.push(current);\n current = [record];\n opened = record.at;\n } else current.push(record);\n }\n if (current.length > 0) batches.push(current);\n return batches;\n}\n\n/** Measures how long the network has stayed quiet from the resource timing entries completed so far. */\nexport function quietfor(entries: Array<{ responseend: number }>, now: number): number {\n let last = 0;\n for (const entry of entries) if (entry.responseend > last) last = entry.responseend;\n return Math.max(0, now - last);\n}\n\n/** Decides the network quiet outcome from collected probe samples against the reviewed idle threshold and timeout. */\nexport function quietresolution(samples: Array<{ at: number; quietfor: number }>, idle: number, timeout: number): { ok: boolean; quietfor: number; waited: number; samples: number } {\n const start = samples[0]?.at ?? 0;\n const last = samples[samples.length - 1];\n const waited = Math.max(0, (last?.at ?? 0) - start);\n const reached = samples.find(sample => sample.quietfor >= idle);\n if (reached) return { ok: true, quietfor: reached.quietfor, waited: reached.at - start, samples: samples.length };\n return { ok: false, quietfor: last?.quietfor ?? 0, waited, samples: samples.length };\n}\n\n/** Serializable node summary the diff engine compares between observation versions. */\nexport interface nodesummary {\n selector: string;\n tag: string;\n text: string;\n attributes: Record<string, string>;\n}\n\n/** Hashes one node summary into a stable digest used by the diff engine. */\nexport function nodehash(summary: nodesummary): string {\n const canonical = [summary.tag, summary.text, ...Object.keys(summary.attributes).sort().map(key => `${key}=${summary.attributes[key] ?? \"\"}`)].join(\"|\");\n let hash = 5381;\n for (let index = 0; index < canonical.length; index += 1) hash = ((hash << 5) + hash + canonical.charCodeAt(index)) >>> 0;\n return hash.toString(16);\n}\n\n/** Diffs two node summary sets into added, removed and changed entries by hashing node summaries. */\nexport function diffsummaries(base: nodesummary[], target: nodesummary[]): { added: diffentry[]; removed: diffentry[]; changed: diffentry[] } {\n const basemap = new Map(base.map(node => [node.selector, node]));\n const targetmap = new Map(target.map(node => [node.selector, node]));\n const added: diffentry[] = [];\n const removed: diffentry[] = [];\n const changed: diffentry[] = [];\n for (const [selector, node] of targetmap) {\n const previous = basemap.get(selector);\n if (!previous) { added.push({ kind: \"added\", selector, summary: node.text || node.tag }); continue; }\n if (nodehash(previous) !== nodehash(node)) changed.push({ kind: \"changed\", selector, summary: `${previous.text || previous.tag} became ${node.text || node.tag}` });\n }\n for (const [selector, node] of basemap) {\n if (!targetmap.has(selector)) removed.push({ kind: \"removed\", selector, summary: node.text || node.tag });\n }\n return { added, removed, changed };\n}\n\n/** Scans inline script payloads for embedded json state; malformed payloads are refused and counted. */\nexport function scanjson(scripts: Array<{ src: string; type: string; id: string; content: string }>): { states: jsonstate[]; refused: number } {\n const states: jsonstate[] = [];\n let refused = 0;\n for (const script of scripts) {\n if (script.src) continue;\n const content = script.content.trim();\n if (!(script.type.includes(\"json\") || content.startsWith(\"{\") || content.startsWith(\"[\"))) continue;\n try { states.push({ scripturl: script.src, rootpath: script.id, payload: JSON.parse(content) }); } catch { refused += 1; }\n }\n return { states, refused };\n}\n\n/** Ranks selector candidates of one element shape by stability: id, attribute, text and structural strategies. */\nexport function rankselectors(shape: { id: string; tag: string; attributes: Record<string, string>; text: string; index: number; siblings: number }): selectorcandidate[] {\n const candidates: selectorcandidate[] = [];\n if (shape.id) candidates.push({ selector: `#${shape.id}`, strategy: \"id\", score: 100 });\n for (const [name, value] of Object.entries(shape.attributes)) {\n if (!value) continue;\n if (name === \"name\" || name.startsWith(\"data-\") || name.startsWith(\"aria-\")) candidates.push({ selector: `${shape.tag}[${name}=\"${value}\"]`, strategy: \"attribute\", score: 80 });\n }\n if (shape.text) candidates.push({ selector: shape.text, strategy: \"text\", score: 60 });\n if (shape.index > 0) candidates.push({ selector: `${shape.tag}:nth-of-type(${shape.index})`, strategy: \"structural\", score: 40 });\n return candidates.sort((left, right) => right.score - left.score);\n}\n\nfunction wait(ms: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, ms));\n}\n\nfunction quietruleof(step: toolstep): quietrule {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const rule = options.quietrule;\n if (!rule || typeof rule !== \"object\" || Array.isArray(rule)) return { idle: 0 };\n const quiet = rule as Record<string, unknown>;\n return {\n idle: typeof quiet.idle === \"number\" && Number.isFinite(quiet.idle) && quiet.idle > 0 ? quiet.idle : 0,\n ...(typeof quiet.poll === \"number\" && Number.isFinite(quiet.poll) && quiet.poll >= 0 ? { poll: quiet.poll } : {}),\n ...(typeof quiet.timeout === \"number\" && Number.isFinite(quiet.timeout) && quiet.timeout >= 0 ? { timeout: quiet.timeout } : {}),\n };\n}\n\n/** Observes dom mutations inside the reviewed selector scopes for the reviewed lifetime, batching records through the throttle window. */\nasync function watchmutations(step: toolstep, root: Document): Promise<stepresult> {\n const options = parsewatchoptions(step, step.id);\n if (options.lifetime <= 0) return { ok: false, summary: \"The reviewed mutation watch lifetime is absent.\" };\n const roots: ParentNode[] = options.scopes ? options.scopes.flatMap(selector => [...root.querySelectorAll(selector)]) : [root];\n if (roots.length === 0) return { ok: false, summary: \"The reviewed watch scopes match no elements.\" };\n const allowed = options.events;\n const collected: mutationevent[] = [];\n const observer = new MutationObserver(records => {\n for (const record of records) {\n if (allowed && !allowed.includes(record.type)) continue;\n const target = record.target instanceof Element ? record.target : null;\n collected.push({ watchid: options.watchid, event: record.type, targetpath: target ? elementselector(target) : \"#text\", at: Date.now() });\n }\n });\n for (const scope of roots) observer.observe(scope, { childList: true, attributes: true, characterData: true, subtree: true });\n await wait(options.lifetime);\n observer.disconnect();\n const batches = batchmutations(collected, options.poll);\n return { ok: true, summary: `Watched ${collected.length} mutation${collected.length === 1 ? \"\" : \"s\"} in ${batches.length} batch${batches.length === 1 ? \"\" : \"es\"} for the reviewed lifetime of ${options.lifetime} milliseconds.`, details: { events: collected, batches: batches.length, watchid: options.watchid, lifetime: options.lifetime, scopes: options.scopes ?? [] } };\n}\n\n/** Records focus and blur events with element paths for the reviewed lifetime. */\nasync function watchfocus(step: toolstep, root: Document): Promise<stepresult> {\n const options = parsewatchoptions(step, step.id);\n if (options.lifetime <= 0) return { ok: false, summary: \"The reviewed focus watch lifetime is absent.\" };\n const collected: focusevent[] = [];\n const record = (kind: \"focus\" | \"blur\") => (event: Event): void => {\n const target = event.target instanceof Element ? event.target : null;\n collected.push({ watchid: options.watchid, kind, targetpath: target ? elementselector(target) : \"#document\", at: Date.now() });\n };\n const onfocus = record(\"focus\");\n const onblur = record(\"blur\");\n root.addEventListener(\"focusin\", onfocus, true);\n root.addEventListener(\"focusout\", onblur, true);\n await wait(options.lifetime);\n root.removeEventListener(\"focusin\", onfocus, true);\n root.removeEventListener(\"focusout\", onblur, true);\n return { ok: true, summary: `Watched ${collected.length} focus change${collected.length === 1 ? \"\" : \"s\"} for the reviewed lifetime of ${options.lifetime} milliseconds.`, details: { events: collected, watchid: options.watchid, lifetime: options.lifetime } };\n}\n\n/** Watches for cookie and consent banners for the reviewed lifetime and reports their controls. */\nasync function watchbanners(step: toolstep, root: Document): Promise<stepresult> {\n const options = parsewatchoptions(step, step.id);\n if (options.lifetime <= 0) return { ok: false, summary: \"The reviewed banner watch lifetime is absent.\" };\n const started = Date.now();\n const seen = new Map<string, bannerreport>();\n while (Date.now() - started < options.lifetime) {\n const at = Date.now();\n for (const report of bannermatches(collectbannercandidates(root), at)) {\n if (!seen.has(report.selector)) seen.set(report.selector, report);\n }\n await wait(options.poll);\n }\n const reports = [...seen.values()];\n return { ok: true, summary: `Watched for consent banners for the reviewed lifetime of ${options.lifetime} milliseconds and observed ${reports.length} banner${reports.length === 1 ? \"\" : \"s\"}.`, details: { banners: reports, watchid: options.watchid, lifetime: options.lifetime } };\n}\n\n/** Waits until the network stays quiet for the reviewed idle threshold, sampling in flight requests through the performance timeline. */\nasync function waitquiet(step: toolstep): Promise<stepresult> {\n const rule = quietruleof(step);\n if (rule.idle <= 0) return { ok: false, summary: \"The reviewed quiet idle threshold is absent.\" };\n const poll = rule.poll ?? 100;\n const timeout = rule.timeout ?? 0;\n const started = performance.now();\n const samples: Array<{ at: number; quietfor: number }> = [];\n for (;;) {\n const now = performance.now();\n const entries = (performance.getEntriesByType(\"resource\") as PerformanceResourceTiming[]).map(entry => ({ responseend: entry.responseEnd }));\n samples.push({ at: now - started, quietfor: quietfor(entries, now) });\n const latest = samples[samples.length - 1];\n if (latest && latest.quietfor >= rule.idle) break;\n if (timeout > 0 && now - started >= timeout) break;\n await wait(poll);\n }\n const outcome = quietresolution(samples, rule.idle, timeout);\n return {\n ok: outcome.ok,\n summary: outcome.ok\n ? `The network stayed quiet for ${Math.round(outcome.quietfor)} milliseconds, meeting the reviewed idle threshold of ${rule.idle} milliseconds.`\n : `The network did not stay quiet for ${rule.idle} milliseconds${timeout > 0 ? ` within the reviewed timeout of ${timeout} milliseconds` : \"\"}.`,\n details: { samples, idle: rule.idle, timeout, waited: Math.round(outcome.waited) },\n };\n}\n\nfunction scriptsurfaces(target: Element | null, root: Document): Array<{ src: string; type: string; id: string; content: string }> {\n const elements = target ? [target] : [...root.querySelectorAll(\"script\")];\n return elements.map(element => ({ src: element.getAttribute(\"src\") ?? \"\", type: element.getAttribute(\"type\") ?? \"\", id: element.id, content: element.textContent ?? \"\" }));\n}\n\n/** Extracts embedded json state from inline scripts and refuses malformed payloads. */\nfunction readjson(step: toolstep, target: Element | null, root: Document): stepresult {\n const outcome = scanjson(scriptsurfaces(target, root));\n if (target && outcome.states.length === 0 && outcome.refused > 0) return { ok: false, summary: \"The reviewed json payload is malformed and was refused.\" };\n return { ok: true, summary: `Extracted ${outcome.states.length} embedded json state${outcome.states.length === 1 ? \"\" : \"s\"}${outcome.refused > 0 ? ` and refused ${outcome.refused} malformed payload${outcome.refused === 1 ? \"\" : \"s\"}` : \"\"}.`, details: { states: outcome.states, refused: outcome.refused } };\n}\n\nfunction tonodesummaries(value: unknown): nodesummary[] | null {\n if (!Array.isArray(value)) return null;\n const summaries: nodesummary[] = [];\n for (const entry of value) {\n if (!entry || typeof entry !== \"object\" || Array.isArray(entry)) continue;\n const candidate = entry as Record<string, unknown>;\n if (typeof candidate.selector !== \"string\") continue;\n const attributes: Record<string, string> = {};\n if (candidate.attributes && typeof candidate.attributes === \"object\" && !Array.isArray(candidate.attributes)) {\n for (const [key, item] of Object.entries(candidate.attributes as Record<string, unknown>)) if (typeof item === \"string\") attributes[key] = item;\n }\n summaries.push({ selector: candidate.selector, tag: typeof candidate.tag === \"string\" ? candidate.tag : \"\", text: typeof candidate.text === \"string\" ? candidate.text : \"\", attributes });\n }\n return summaries;\n}\n\n/** Diffs the two reviewed observation versions injected by the background into added, removed and changed nodes. */\nfunction diffsnapshots(step: toolstep): stepresult {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const base = tonodesummaries(options.base);\n const target = tonodesummaries(options.target);\n if (!base || !target) return { ok: false, summary: \"Two stored observation versions must be reviewed before diffing.\" };\n const versions = Array.isArray(options.versions) && options.versions.length === 2 ? (options.versions as number[]) : [0, 0];\n const diff = diffsummaries(base, target);\n return { ok: true, summary: `Diffed observation versions ${versions[0] ?? 0} and ${versions[1] ?? 0}: ${diff.added.length} added, ${diff.removed.length} removed and ${diff.changed.length} changed node${diff.added.length + diff.removed.length + diff.changed.length === 1 ? \"\" : \"s\"}.`, details: { versions, added: diff.added, removed: diff.removed, changed: diff.changed } };\n}\n\n/** Derives ranked selector candidates for one reviewed element. */\nfunction deriveselector(target: Element | null): stepresult {\n if (!(target instanceof Element)) return { ok: false, summary: \"Derivation target is no longer available.\" };\n const attributes: Record<string, string> = {};\n for (const attribute of [...target.attributes]) attributes[attribute.name] = attribute.value;\n const parent = target.parentElement;\n const siblings = parent ? [...parent.children].filter(node => node.tagName === target.tagName) : [target];\n const candidates = rankselectors({ id: target.id, tag: target.tagName.toLowerCase(), attributes, text: clean(target.textContent ?? \"\").slice(0, 80), index: siblings.indexOf(target) + 1, siblings: siblings.length });\n const best = candidates[0];\n return { ok: candidates.length > 0, summary: best ? `Derived ${candidates.length} selector candidate${candidates.length === 1 ? \"\" : \"s\"}; the most stable is ${best.selector} through the ${best.strategy} strategy with stability ${best.score}.` : \"No selector candidate could be derived.\", details: { candidates } };\n}\n\n/** Runs one watched observation after the background policy gate; the reviewed lifetime window bounds every watch. */\nexport function runpagewatch(step: toolstep, target: Element | null, root: Document = document): stepresult | Promise<stepresult> {\n switch (step.kind) {\n case \"watchmutate\": return watchmutations(step, root);\n case \"watchfocus\": return watchfocus(step, root);\n case \"watchbanner\": return watchbanners(step, root);\n case \"waitquiet\": return waitquiet(step);\n case \"readjson\": return readjson(step, target, root);\n case \"diffsnapshots\": return diffsnapshots(step);\n case \"deriveselector\": return deriveselector(target);\n default: return { ok: false, summary: \"Unsupported watched observation.\" };\n }\n}\n", "import type { toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport { blackboxruleof, devicepresetof, familyofkind, locationpresetof, networkpresetof, agentpresetof, permissiongrantof } from \"../emulation.js\";\nimport type { stepresult } from \"./pageactions.js\";\n\n/**\n * Page-side emulation for the reviewed 1.1.48 steps.\n * Correlated rules for the injected mask layer live in the root emulation module while this file applies and reverts the masks inside the page through the scripting api: the device layer overrides the pixel ratio and the mobile hint beside the viewport bounds the window update api carries, the network layer registers the reviewed latency, throughput and offline bounds for the transport the extension initiates, the location layer overrides navigator geolocation with the reviewed coordinates, the agent layer overrides the navigator user agent, platform and brand list together and scoped to the run tab only, the permission layer answers navigator permission queries with the reviewed state, and the blackbox layer registers the reviewed patterns so stack traces hide third party frames.\n * True device metric, network condition, geolocation and user agent override needs the debugger permission or platform permissions the manifest gate forbids, so every mask is a page-injected derivation recorded honestly on every layer; the browser headers and the true device state stay untouched.\n */\n\n/** The shared registry key of the active page-side masks so reverts find the applied values after the apply. */\nconst registrykey = \"devthinkemulation\";\n\ninterface maskregistry {\n pixelratio?: number;\n geolocation?: Geolocation;\n useragent?: string;\n platform?: string;\n brands?: string[];\n permissions?: Permissions;\n blackbox?: string[];\n}\n\nfunction registry(): maskregistry {\n const holder = globalThis as typeof globalThis & { devthinkemulation?: maskregistry };\n if (holder.devthinkemulation === undefined) holder.devthinkemulation = {};\n return holder.devthinkemulation;\n}\n\n/** Captures the prior page state of one family so the revert restores the exact values. */\nfunction priorsnapshot(family: string): Record<string, unknown> {\n if (family === \"device\") return { pixelratio: window.devicePixelRatio, viewportwidth: window.innerWidth, viewportheight: window.innerHeight };\n if (family === \"agent\") return { useragent: navigator.userAgent, platform: navigator.platform };\n if (family === \"permission\") return { note: \"the browser permission state stays untouched and the override restores by removing the page-side answer\" };\n return { note: \"the layer adds no prior page state to restore\" };\n}\n\n/** Overrides the page pixel ratio and mobile hint; the viewport bounds apply through the window update api in the background because the page cannot resize itself. */\nfunction applydevice(width: number, height: number, pixelratio: number, mobile: boolean): void {\n const state = registry();\n if (state.pixelratio === undefined) state.pixelratio = window.devicePixelRatio;\n Object.defineProperty(window, \"devicePixelRatio\", { configurable: true, get: () => pixelratio });\n document.documentElement.dataset.devthinkMobile = mobile ? \"true\" : \"false\";\n document.documentElement.dataset.devthinkViewport = `${width}x${height}`;\n}\n\n/** Restores the prior pixel ratio and clears the mobile hint and viewport marker. */\nfunction revertdevice(prior: Record<string, unknown> | undefined): void {\n const state = registry();\n const restored = typeof prior?.pixelratio === \"number\" ? prior.pixelratio : state.pixelratio ?? window.devicePixelRatio;\n Object.defineProperty(window, \"devicePixelRatio\", { configurable: true, get: () => restored });\n delete document.documentElement.dataset.devthinkMobile;\n delete document.documentElement.dataset.devthinkViewport;\n delete state.pixelratio;\n}\n\n/** Overrides navigator geolocation with the reviewed coordinates and accuracy; the true browser location stays untouched. */\nfunction applylocation(latitude: number, longitude: number, accuracy: number): void {\n const state = registry();\n if (state.geolocation === undefined) state.geolocation = navigator.geolocation;\n const position = (): GeolocationPosition => ({\n coords: { latitude, longitude, accuracy, altitude: null, altitudeAccuracy: null, heading: null, speed: null } as GeolocationCoordinates,\n timestamp: Date.now(),\n } as GeolocationPosition);\n const overridden: Geolocation = {\n getCurrentPosition: success => { success(position()); },\n watchPosition: success => { success(position()); return 0; },\n clearWatch: () => { /* the page-side watch holds no timer */ },\n };\n Object.defineProperty(navigator, \"geolocation\", { configurable: true, get: () => overridden });\n}\n\n/** Restores the true navigator geolocation. */\nfunction revertlocation(): void {\n const state = registry();\n if (state.geolocation !== undefined) Object.defineProperty(navigator, \"geolocation\", { configurable: true, get: () => state.geolocation as Geolocation });\n delete state.geolocation;\n}\n\n/** Overrides the navigator user agent, platform and brand list together so page checks read the reviewed agent of the run tab only. */\nfunction applyagent(useragent: string, platform: string, brands: string[]): void {\n const state = registry();\n if (state.useragent === undefined) state.useragent = navigator.userAgent;\n if (state.platform === undefined) state.platform = navigator.platform;\n if (state.brands === undefined) state.brands = brands;\n Object.defineProperty(navigator, \"userAgent\", { configurable: true, get: () => useragent });\n Object.defineProperty(navigator, \"platform\", { configurable: true, get: () => platform });\n const branded = brands.map((brand, index) => ({ brand, version: `${index + 1}.0.0.0` }));\n const dataholder = navigator as Navigator & { userAgentData?: { brands: Array<{ brand: string; version: string }> } };\n if (dataholder.userAgentData !== undefined) Object.defineProperty(dataholder, \"userAgentData\", { configurable: true, get: () => ({ brands: branded }) });\n}\n\n/** Restores the true navigator user agent, platform and brand list. */\nfunction revertagent(prior: Record<string, unknown> | undefined): void {\n const state = registry();\n const useragent = typeof prior?.useragent === \"string\" ? prior.useragent : state.useragent ?? navigator.userAgent;\n const platform = typeof prior?.platform === \"string\" ? prior.platform : state.platform ?? navigator.platform;\n Object.defineProperty(navigator, \"userAgent\", { configurable: true, get: () => useragent });\n Object.defineProperty(navigator, \"platform\", { configurable: true, get: () => platform });\n delete state.useragent;\n delete state.platform;\n delete state.brands;\n}\n\n/** Answers navigator permission queries with the reviewed state while the browser permission itself stays untouched. */\nfunction applypermission(name: string, state: string): void {\n const holder = navigator as Navigator & { devthinkpermission?: Record<string, string> };\n if (holder.devthinkpermission === undefined) holder.devthinkpermission = {};\n holder.devthinkpermission[name] = state;\n const state0 = registry();\n if (state0.permissions === undefined && navigator.permissions !== undefined) state0.permissions = navigator.permissions;\n if (navigator.permissions === undefined) return;\n const overridden: Permissions = {\n query: description => new Promise(resolve => {\n const applied = holder.devthinkpermission?.[description.name];\n resolve({ state: (applied ?? \"prompt\") as PermissionState, name: description.name, onchange: null } as PermissionStatus);\n }),\n };\n Object.defineProperty(navigator, \"permissions\", { configurable: true, get: () => overridden });\n}\n\n/** Removes the page-side permission answers so the browser permission state returns. */\nfunction revertpermission(): void {\n const state = registry();\n if (state.permissions !== undefined) Object.defineProperty(navigator, \"permissions\", { configurable: true, get: () => state.permissions as Permissions });\n delete state.permissions;\n delete (navigator as Navigator & { devthinkpermission?: Record<string, string> }).devthinkpermission;\n}\n\n/** Registers the blackbox patterns in the page registry so stack captures hide third party frames; the rules shape traces only and read no page state. */\nfunction applyblackbox(patterns: string[]): void {\n registry().blackbox = patterns;\n}\n\n/** Returns the active blackbox patterns of the page registry for the stack capture filters. */\nexport function activeblackboxpatterns(): string[] {\n return registry().blackbox ?? [];\n}\n\n/** Runs one reviewed emulation step inside the page: the family of the kind decides the mask, the prior state is captured for the exact revert and the honest derivation note stays beside the result. */\nexport async function runemulationstep(step: toolstep): Promise<stepresult> {\n const options = (() => { try { return parseoptions(step); } catch { return {}; } })();\n const family = familyofkind(step.kind);\n const derivation = \"The mask is a page-injected override through the scripting api; the browser device metrics, network stack, true location, request headers and permission state stay untouched because no debugger or platform permission exists in the manifest.\";\n if (step.kind === \"emulatedevice\") {\n const preset = devicepresetof(options.device);\n if (!preset) return { ok: false, summary: \"The reviewed device preset is absent or malformed.\" };\n const prior = priorsnapshot(\"device\");\n applydevice(preset.width, preset.height, preset.pixelratio, preset.mobile);\n return { ok: true, summary: `Applied the device preset ${preset.name} of ${preset.width} by ${preset.height} css pixels, pixel ratio ${preset.pixelratio} and the ${preset.mobile ? \"mobile\" : \"desktop\"} hint to the run tab.`, details: { prior, preset: { name: preset.name, width: preset.width, height: preset.height, pixelratio: preset.pixelratio, mobile: preset.mobile }, derivation } };\n }\n if (step.kind === \"emulatenetwork\") {\n const preset = networkpresetof(options.network);\n if (!preset) return { ok: false, summary: \"The reviewed network preset is absent or malformed.\" };\n const window0 = typeof options.window === \"number\" ? options.window : undefined;\n return { ok: true, summary: `Applied the network preset ${preset.name} with ${preset.latency} milliseconds latency, ${preset.download} and ${preset.upload} kilobit per second bounds${preset.offline ? ` and the offline flag${window0 !== undefined ? ` for the reviewed window of ${window0} milliseconds` : \"\"}` : \"\"}; the bounds shape the traffic the extension itself initiates.`, details: { preset: { name: preset.name, latency: preset.latency, download: preset.download, upload: preset.upload, offline: preset.offline }, ...(window0 !== undefined ? { window: window0 } : {}), derivation } };\n }\n if (step.kind === \"emulatelocate\") {\n const preset = locationpresetof(options.location);\n if (!preset) return { ok: false, summary: \"The reviewed location preset is absent or malformed.\" };\n applylocation(preset.latitude, preset.longitude, preset.accuracy);\n return { ok: true, summary: `Applied the location preset ${preset.name} of ${preset.latitude}, ${preset.longitude} with the ${preset.accuracy} meter accuracy radius to the run tab.`, details: { preset: { name: preset.name, latitude: preset.latitude, longitude: preset.longitude, accuracy: preset.accuracy }, derivation } };\n }\n if (step.kind === \"setuseragent\") {\n const preset = agentpresetof(options.agent);\n if (!preset) return { ok: false, summary: \"The reviewed agent preset is absent or malformed.\" };\n const prior = priorsnapshot(\"agent\");\n applyagent(preset.useragent, preset.platform, preset.brands);\n return { ok: true, summary: `Applied the agent preset ${preset.name} with the reviewed user agent string, platform ${preset.platform} and ${preset.brands.length} brand${preset.brands.length === 1 ? \"\" : \"s\"} together, scoped to the run tab only.`, details: { prior, preset: { name: preset.name, platform: preset.platform, brands: preset.brands }, derivation } };\n }\n if (step.kind === \"overridepermission\") {\n const grant = permissiongrantof(options.permission);\n if (!grant) return { ok: false, summary: \"The reviewed permission override is absent or malformed.\" };\n const prior = priorsnapshot(\"permission\");\n applypermission(grant.name, grant.state);\n return { ok: true, summary: `Answered the ${grant.name} permission queries of the run tab with the reviewed ${grant.state} state${grant.runscope ? \" for the run scope\" : \"\"}; the browser permission itself stays untouched.`, details: { prior, permission: { name: grant.name, state: grant.state, runscope: grant.runscope }, derivation } };\n }\n if (step.kind === \"blackboxscripts\") {\n const rules = (Array.isArray(options.rules) ? options.rules : []).flatMap(rule => { const parsed = blackboxruleof(rule); return parsed !== undefined ? [parsed] : []; });\n if (rules.length === 0) return { ok: false, summary: \"The reviewed blackbox rule list is absent or malformed.\" };\n applyblackbox(rules.flatMap(rule => rule.urlpatterns));\n return { ok: true, summary: `Marked ${rules.flatMap(rule => rule.urlpatterns).length} third party url pattern${rules.flatMap(rule => rule.urlpatterns).length === 1 ? \"\" : \"s\"} as blackboxed in the traces of the run; the rules read no page state.`, details: { rules, derivation: \"Blackbox rules shape stack traces and profiles of the run only; they read no page state and touch no third party script.\" } };\n }\n void family;\n return { ok: false, summary: \"The emulation step is not part of the mask family.\" };\n}\n\n/** Reverts one emulation layer inside the page by restoring the captured prior state; the revert is idempotent for a context the navigation already destroyed. */\nexport function revertemulationlayer(family: string, prior: Record<string, unknown> | undefined): { ok: boolean; summary: string } {\n if (family === \"device\") { revertdevice(prior); return { ok: true, summary: \"Restored the prior pixel ratio and cleared the device hint of the run tab.\" }; }\n if (family === \"location\") { revertlocation(); return { ok: true, summary: \"Restored the true navigator geolocation of the run tab.\" }; }\n if (family === \"agent\") { revertagent(prior); return { ok: true, summary: \"Restored the true navigator user agent, platform and brand list of the run tab.\" }; }\n if (family === \"permission\") { revertpermission(); return { ok: true, summary: \"Removed the page-side permission answers so the browser permission state returns.\" }; }\n if (family === \"blackbox\") { delete registry().blackbox; return { ok: true, summary: \"Removed the blackbox pattern registry of the run.\" }; }\n return { ok: true, summary: \"The network layer holds no page state to restore; the transport bounds ended with the run.\" };\n}\n", "import type { consoleentry, errorrecord, loglevel, longtaskentry, rejectionrecord, timelineentry, toolstep, timelinesource } from \"../types.js\";\nimport { consolecapture, errorcapture, levelrank, loglevels, longtaskcapture, rejectioncapture, serializearg, stackframes } from \"../runtimeline.js\";\nimport { breakpointinputof, stepmodeof, watchexpressionof, overrideinputof } from \"../cdpbus.js\";\nimport { blackboxmatches } from \"../emulation.js\";\nimport { activeblackboxpatterns } from \"./pageemulate.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\n\n/**\n * Page-side debugging capture for reviewed watch steps.\n * Correlated rules for the debug watch option parsing live here while the capture, spam, rotation and diff math lives in the root runtimeline module; console methods are hooked, error and rejection listeners installed and the longtask performance buffer observed for the reviewed window only, and every hook detaches cleanly when the window closes or the tab navigates.\n * Console, error and task watching derives from page-injected listeners through the scripting api, so no debugger permission exists anywhere in the manifest.\n */\n\n/** The instrumented devtools harness key on the page global; the harness is the honest derivation of the devtools protocol because the debugger permission stays outside the manifest. */\nconst harnesskey = \"__devthinkcdp\";\n\n/** One instrumented devtools harness of the page: the enabled domains, the registered breakpoints, the applied overrides, the observed event buffer and the pause state of the instrumented debugger. */\ninterface cdpharness {\n domains: string[];\n breakpoints: Array<{ id: string; url: string; line: number; column?: number; condition?: string; hits: number }>;\n overrides: Array<{ id: string; urlpattern: string; source: string }>;\n events: Array<{ domain: string; event: string; payload?: string; at: number }>;\n paused?: { reason: string; hitbreakpoint?: string; frames: Array<{ functionname?: string; url: string; line: number; column?: number }>; scope: Record<string, unknown>; cursor: number; lines: number };\n hooks: Array<() => void>;\n}\n\n/** Reads the instrumented devtools harness of the page, if one is attached. */\nfunction readharness(): cdpharness | undefined {\n return (globalThis as typeof globalThis & Record<string, unknown>)[harnesskey] as cdpharness | undefined;\n}\n\n/** Writes or clears the instrumented devtools harness of the page. */\nfunction writeharness(harness: cdpharness | undefined): void {\n if (harness === undefined) delete (globalThis as typeof globalThis & Record<string, unknown>)[harnesskey];\n else (globalThis as typeof globalThis & Record<string, unknown>)[harnesskey] = harness;\n}\n\n/** The instrumented devtools method surface: the domains the harness enables, runtime evaluation, dom snapshots and page navigation history; every other method of the raw protocol reports the honest uninstrumented error class. */\nconst instrumentedmethods: ReadonlySet<string> = new Set([\"Runtime.evaluate\", \"Log.enable\", \"Debugger.enable\", \"DOM.enable\", \"Network.enable\", \"Page.enable\", \"DOM.getSnapshot\", \"Page.getNavigationHistory\"]);\n\n/** Parsed cdp step options: the enabled domains, the teardown plan, the raw command, the event rules with the watch window, the breakpoint, the step mode, the watch expression and the script override. */\nexport interface cdpstepoptions {\n domains: string[];\n teardown?: { revertsteps: string[]; resumepolicy: string };\n command?: { method: string; params?: Record<string, unknown>; resultpath?: string };\n events?: Array<{ domain: string; event: string; match?: string }>;\n watchwindow: number;\n breakpoint?: { url: string; line: number; column?: number; condition?: string };\n mode?: string;\n expression?: { expression: string; scope: string };\n override?: { urlpattern: string; source: string };\n}\n\n/** Parses the reviewed cdp options of one devtools protocol step through the shared cdpbus normalizers. */\nexport function cdpstepoptions(step: toolstep): cdpstepoptions {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const teardown = options.teardown && typeof options.teardown === \"object\" && !Array.isArray(options.teardown) && Array.isArray((options.teardown as Record<string, unknown>).revertsteps) ? { revertsteps: ((options.teardown as Record<string, unknown>).revertsteps as unknown[]).filter((item): item is string => typeof item === \"string\"), resumepolicy: String((options.teardown as Record<string, unknown>).resumepolicy ?? \"ask\") } : undefined;\n const command = options.command && typeof options.command === \"object\" && !Array.isArray(options.command) ? options.command as Record<string, unknown> : undefined;\n const watch = options.watch && typeof options.watch === \"object\" && !Array.isArray(options.watch) ? options.watch as Record<string, unknown> : {};\n const breakpoint = breakpointinputof(options.breakpoint);\n const expression = watchexpressionof(options.expression);\n const override = overrideinputof(options.override);\n return {\n domains: Array.isArray(options.domains) ? options.domains.filter((domain): domain is string => typeof domain === \"string\") : [],\n ...(teardown !== undefined ? { teardown } : {}),\n ...(command !== undefined && typeof command.method === \"string\" ? { command: { method: command.method, ...(command.params && typeof command.params === \"object\" && !Array.isArray(command.params) ? { params: command.params as Record<string, unknown> } : {}), ...(typeof command.resultpath === \"string\" ? { resultpath: command.resultpath } : {}) } } : {}),\n events: Array.isArray(options.events) ? options.events.flatMap(rule => {\n const parsed = rule && typeof rule === \"object\" && !Array.isArray(rule) ? rule as Record<string, unknown> : undefined;\n if (!parsed || typeof parsed.domain !== \"string\" || typeof parsed.event !== \"string\") return [];\n return [{ domain: parsed.domain, event: parsed.event, ...(typeof parsed.match === \"string\" ? { match: parsed.match } : {}) }];\n }) : [],\n watchwindow: typeof watch.window === \"number\" && Number.isFinite(watch.window) && watch.window >= 0 ? watch.window : 0,\n ...(breakpoint !== undefined ? { breakpoint } : {}),\n ...(options.mode !== undefined && stepmodeof(options.mode) !== undefined ? { mode: options.mode as string } : {}),\n ...(expression !== undefined ? { expression } : {}),\n ...(override !== undefined ? { override } : {}),\n };\n}\n\n/** Captures the light dom state of the page at a pause: the url, title, node and form counts, derived through the page bridge snapshot seam because no debugger permission exists. */\nfunction domstate(): { url: string; title: string; nodes: number; forms: number } {\n return { url: location.href, title: document.title, nodes: document.querySelectorAll(\"*\").length, forms: document.forms.length };\n}\n\n/** Runs one reviewed devtools protocol step inside the page through the instrumented harness: the harness attaches and detaches cleanly, raw commands of the instrumented surface run with duration and error class, domain events observe through the console and navigation hooks, breakpoints pause instrumented probes, stepping advances the pause, watch expressions evaluate in the pause scope and script overrides apply the reviewed fixture. */\nexport async function runcdpstep(step: toolstep): Promise<stepresult> {\n const options = cdpstepoptions(step);\n if (step.kind === \"attachcdp\") {\n const existing = readharness();\n if (existing) { for (const detach of existing.hooks) detach(); }\n const harness: cdpharness = { domains: options.domains, breakpoints: [], overrides: [], events: [], hooks: [] };\n if (options.domains.includes(\"Log\") || options.domains.includes(\"Runtime\")) {\n for (const level of loglevels) {\n const original = console[level] as (...args: unknown[]) => void;\n const hooked = (...args: unknown[]): void => {\n try { original.apply(console, args); } catch { /* the page console may refuse the passthrough; capture still proceeds */ }\n harness.events.push({ domain: \"Log\", event: \"entryAdded\", payload: args.map(arg => serializearg(arg, 2)).join(\" \"), at: Date.now() });\n };\n (console as unknown as Record<string, unknown>)[level] = hooked;\n harness.hooks.push(() => { (console as unknown as Record<string, unknown>)[level] = original; });\n }\n }\n writeharness(harness);\n const iframes = [...document.querySelectorAll(\"iframe[src]\")].map(frame => (frame as HTMLIFrameElement).src).filter(src => src.startsWith(\"https://\"));\n const serviceworker = \"serviceWorker\" in navigator && navigator.serviceWorker.controller ? navigator.serviceWorker.controller.scriptURL : undefined;\n return { ok: true, summary: `Attached the instrumented devtools harness with the reviewed domains ${options.domains.join(\", \")} enabled.`, details: { attached: true, domains: [...harness.domains], targets: { iframes, ...(serviceworker !== undefined ? { serviceworker } : {}) }, derivation: \"The chrome devtools protocol needs the debugger permission, which the manifest gate forbids; the session runs through the page-instrumented harness injected by the scripting api and the iframe and service worker targets derive from the page frame list and controller state.\" } };\n }\n if (step.kind === \"detachcdp\") {\n const harness = readharness();\n if (!harness) return { ok: false, summary: \"No instrumented devtools harness is attached to this page.\" };\n for (const detach of harness.hooks) detach();\n const reverted = { breakpoints: harness.breakpoints.length, overrides: harness.overrides.length };\n writeharness(undefined);\n return { ok: true, summary: `Detached the instrumented devtools harness cleanly after reverting ${reverted.breakpoints} breakpoint${reverted.breakpoints === 1 ? \"\" : \"s\"} and ${reverted.overrides} override${reverted.overrides === 1 ? \"\" : \"s\"}.`, details: { detached: true, ...reverted } };\n }\n if (step.kind === \"cdpcmd\") {\n const harness = readharness();\n if (!harness) return { ok: false, summary: \"No instrumented devtools harness is attached to this page.\" };\n const method = options.command?.method ?? \"\";\n const params = options.command?.params ?? {};\n if (!instrumentedmethods.has(method)) return { ok: false, summary: `The reviewed command ${method} reports the uninstrumented error class: the page harness implements ${[...instrumentedmethods].join(\", \")} only.`, details: { method, errorclass: \"uninstrumented\" } };\n const started = Date.now();\n try {\n if (method === \"Runtime.evaluate\") {\n const expression = typeof params.expression === \"string\" ? params.expression : \"\";\n const probeurl = typeof params.url === \"string\" ? params.url : \"inline\";\n const scope = params.scope && typeof params.scope === \"object\" && !Array.isArray(params.scope) ? params.scope as Record<string, unknown> : {};\n const override = harness.overrides.find(spec => overridematch(spec.urlpattern, probeurl));\n const source = override !== undefined && overridematch(override.urlpattern, probeurl) ? override.source : expression;\n const value = new Function(...Object.keys(scope), `\"use strict\"; return (${source});`)(...Object.values(scope));\n let tripped: cdpharness[\"paused\"];\n for (const breakpoint of harness.breakpoints) {\n if (breakpoint.url !== probeurl) continue;\n const conditionok = breakpoint.condition === undefined ? true : Boolean(new Function(...Object.keys(scope), `\"use strict\"; return (${breakpoint.condition});`)(...Object.values(scope)));\n if (!conditionok) continue;\n breakpoint.hits += 1;\n tripped = { reason: \"breakpoint\", hitbreakpoint: breakpoint.id, frames: stackframes(new Error().stack ?? \"\"), scope, cursor: breakpoint.line, lines: Math.max(1, source.split(\"\\n\").length) };\n harness.paused = tripped;\n break;\n }\n const serialized = serializearg(value, 3);\n harness.events.push({ domain: \"Runtime\", event: \"executionContextDestroyed\", payload: serialized.slice(0, 200), at: Date.now() });\n return { ok: true, summary: `The reviewed command ${method} returned in ${Date.now() - started} milliseconds${tripped !== undefined ? \" and paused the run on the reviewed breakpoint\" : \"\"}.`, details: { method, duration: Date.now() - started, result: { value: serialized }, ...(tripped !== undefined ? { paused: { reason: tripped.reason, hitbreakpoint: tripped.hitbreakpoint, frames: tripped.frames } } : {}) } };\n }\n if (method === \"DOM.getSnapshot\") {\n const state = domstate();\n return { ok: true, summary: `The reviewed command ${method} returned the dom snapshot of ${state.nodes} nodes in ${Date.now() - started} milliseconds.`, details: { method, duration: Date.now() - started, result: state } };\n }\n if (method === \"Page.getNavigationHistory\") {\n const state = domstate();\n return { ok: true, summary: `The reviewed command ${method} returned the page navigation history in ${Date.now() - started} milliseconds.`, details: { method, duration: Date.now() - started, result: { url: state.url, title: state.title } } };\n }\n return { ok: true, summary: `The reviewed command ${method} enabled its domain through the instrumented harness in ${Date.now() - started} milliseconds.`, details: { method, duration: Date.now() - started, result: {} } };\n } catch (error) {\n return { ok: false, summary: `The reviewed command ${method} failed with the evaluationerror class: ${error instanceof Error ? error.message : String(error)}.`, details: { method, duration: Date.now() - started, errorclass: \"evaluationerror\" } };\n }\n }\n if (step.kind === \"watchcdp\") {\n const harness = readharness();\n if (!harness) return { ok: false, summary: \"No instrumented devtools harness is attached to this page.\" };\n const started = Date.now();\n const navigation = performance.getEntriesByType(\"navigation\")[0] as PerformanceNavigationTiming | undefined;\n if (harness.domains.includes(\"Page\") && navigation !== undefined && navigation.loadEventStart > 0) harness.events.push({ domain: \"Page\", event: \"loadEventFired\", payload: location.href, at: started });\n await wait(options.watchwindow);\n const observed = harness.events.filter(event => event.at >= started);\n return { ok: true, summary: `Observed ${observed.length} domain event${observed.length === 1 ? \"\" : \"s\"} for the reviewed window of ${options.watchwindow} milliseconds.`, details: { events: observed, watchwindow: options.watchwindow, derivation: \"Domain events derive from the instrumented console hooks and the page performance navigation buffer because no debugger permission exists in the manifest.\" } };\n }\n if (step.kind === \"setbreakpoint\") {\n const harness = readharness();\n if (!harness) return { ok: false, summary: \"No instrumented devtools harness is attached to this page.\" };\n if (!options.breakpoint) return { ok: false, summary: \"The breakpoint input is absent.\" };\n const id = `bp-${options.breakpoint.url}-${options.breakpoint.line}-${options.breakpoint.column ?? 0}`;\n const registered = { id, ...options.breakpoint, hits: 0 };\n harness.breakpoints.push(registered);\n return { ok: true, summary: `Registered the reviewed breakpoint at ${options.breakpoint.url}:${options.breakpoint.line}${options.breakpoint.condition !== undefined ? ` under the condition ${options.breakpoint.condition}` : \"\"}.`, details: { breakpoint: registered } };\n }\n if (step.kind === \"stepcode\") {\n const harness = readharness();\n if (!harness) return { ok: false, summary: \"No instrumented devtools harness is attached to this page.\" };\n const mode = stepmodeof(options.mode);\n if (mode === undefined) return { ok: false, summary: \"The step code mode is absent.\" };\n if (harness.paused === undefined) return { ok: false, summary: \"No paused instrumented probe exists to step through; pause on a reviewed breakpoint first.\" };\n if (mode === \"resume\" || mode === \"stepout\") {\n const reason = harness.paused.reason;\n const frames = harness.paused.frames;\n delete harness.paused;\n return { ok: true, summary: `The ${mode} mode ${mode === \"resume\" ? \"resumed\" : \"stepped out of\"} the paused probe after ${frames.length} call frame${frames.length === 1 ? \"\" : \"s\"}.`, details: { mode, paused: false, reason } };\n }\n harness.paused.cursor += 1;\n const state = domstate();\n return { ok: true, summary: `The ${mode} mode advanced to line ${harness.paused.cursor} of the paused probe and captured the pause state with ${harness.paused.frames.length} call frame${harness.paused.frames.length === 1 ? \"\" : \"s\"} and the dom state.`, details: { mode, paused: true, pausestate: { reason: harness.paused.reason, ...(harness.paused.hitbreakpoint !== undefined ? { hitbreakpoint: harness.paused.hitbreakpoint } : {}), frames: harness.paused.frames, cursor: harness.paused.cursor, dom: state } } };\n }\n if (step.kind === \"watchexpr\") {\n const harness = readharness();\n if (!harness) return { ok: false, summary: \"No instrumented devtools harness is attached to this page.\" };\n if (!options.expression) return { ok: false, summary: \"The watch expression input is absent.\" };\n if (harness.paused === undefined) return { ok: false, summary: \"No paused instrumented probe exists to evaluate the watch expression in; pause on a reviewed breakpoint first.\" };\n try {\n const value = new Function(...Object.keys(harness.paused.scope), `\"use strict\"; return (${options.expression.expression});`)(...Object.values(harness.paused.scope));\n return { ok: true, summary: `Evaluated the reviewed watch expression at the pause in the ${options.expression.scope} scope.`, details: { expression: options.expression.expression, scope: options.expression.scope, value: serializearg(value, 3) } };\n } catch (error) {\n return { ok: false, summary: `The reviewed watch expression failed with the evaluationerror class: ${error instanceof Error ? error.message : String(error)}.`, details: { errorclass: \"evaluationerror\" } };\n }\n }\n if (step.kind === \"overridescript\") {\n const harness = readharness();\n if (!harness) return { ok: false, summary: \"No instrumented devtools harness is attached to this page.\" };\n if (!options.override) return { ok: false, summary: \"The script override input is absent.\" };\n const id = `ov-${options.override.urlpattern}`;\n harness.overrides = harness.overrides.filter(spec => spec.id !== id);\n harness.overrides.push({ id, urlpattern: options.override.urlpattern, source: options.override.source });\n try {\n new Function(options.override.source)();\n return { ok: true, summary: `Applied the reviewed script fixture for ${options.override.urlpattern} on the current document and on later instrumented evaluations of the pattern.`, details: { override: { id, urlpattern: options.override.urlpattern, applied: true } } };\n } catch (error) {\n return { ok: false, summary: `The reviewed script fixture failed with the evaluationerror class: ${error instanceof Error ? error.message : String(error)}.`, details: { errorclass: \"evaluationerror\" } };\n }\n }\n return { ok: false, summary: \"The devtools step is not part of the instrumented family.\" };\n}\n\n/** Matches one instrumented probe url against an override pattern with single star segments and double star subtrees. */\nfunction overridematch(urlpattern: string, url: string): boolean {\n const patternmatch = /^(https:\\/\\/[^/]+|inline)(\\/.*)?$/.exec(urlpattern);\n const urlmatch = /^(https:\\/\\/[^/]+|inline)(\\/.*)?$/.exec(url);\n if (!patternmatch || !urlmatch) return false;\n if (patternmatch[1] !== urlmatch[1]) return false;\n const patternpath = (patternmatch[2] ?? \"/\").split(\"/\").filter(segment => segment.length > 0);\n const urlpath = (urlmatch[2] ?? \"/\").split(\"/\").filter(segment => segment.length > 0);\n const walk = (patternindex: number, urlindex: number): boolean => {\n if (patternindex >= patternpath.length) return urlindex >= urlpath.length;\n const segment = patternpath[patternindex];\n if (segment === \"**\") return walk(patternindex + 1, urlindex) || (urlindex < urlpath.length && walk(patternindex, urlindex + 1));\n if (urlindex >= urlpath.length) return false;\n if (segment !== \"*\" && segment !== urlpath[urlindex]) return false;\n return walk(patternindex + 1, urlindex + 1);\n };\n return walk(0, 0);\n}\n\n/** Parsed debug watch options: the window, level floor, serialization depth, redaction patterns, spam rule, rotation rule and long task threshold. */\nexport interface debugwatchoptions {\n window: number;\n level?: loglevel;\n depth: number;\n redact: string[];\n spam?: { pattern: string; windowsize: number; collapse: number };\n rotation?: { maxentries: number; overflowtarget: string };\n threshold: number;\n}\n\n/** Parses the reviewed debug watch options of one watch step; absent windows watch nothing and the depth bound defaults shallow. */\nexport function debugwatchoptions(step: toolstep): debugwatchoptions {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const watch = options.watch && typeof options.watch === \"object\" && !Array.isArray(options.watch) ? options.watch as Record<string, unknown> : {};\n const spam = options.spam && typeof options.spam === \"object\" && !Array.isArray(options.spam) ? options.spam as Record<string, unknown> : undefined;\n const rotation = options.rotation && typeof options.rotation === \"object\" && !Array.isArray(options.rotation) ? options.rotation as Record<string, unknown> : undefined;\n return {\n window: typeof watch.window === \"number\" && Number.isFinite(watch.window) && watch.window >= 0 ? watch.window : 0,\n ...(typeof options.level === \"string\" && (loglevels as string[]).includes(options.level) ? { level: options.level as loglevel } : {}),\n depth: typeof options.depth === \"number\" && Number.isInteger(options.depth) && options.depth >= 1 ? options.depth : 2,\n redact: Array.isArray(options.redact) ? options.redact.filter((pattern): pattern is string => typeof pattern === \"string\" && pattern.length > 0) : [],\n ...(spam && typeof spam.pattern === \"string\" && typeof spam.windowsize === \"number\" && typeof spam.collapse === \"number\" ? { spam: { pattern: spam.pattern, windowsize: spam.windowsize, collapse: spam.collapse } } : {}),\n ...(rotation && typeof rotation.maxentries === \"number\" && typeof rotation.overflowtarget === \"string\" ? { rotation: { maxentries: rotation.maxentries, overflowtarget: rotation.overflowtarget } } : {}),\n threshold: typeof options.threshold === \"number\" && Number.isFinite(options.threshold) && options.threshold >= 0 ? options.threshold : 0,\n };\n}\n\nfunction wait(milliseconds: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, Math.max(0, milliseconds)));\n}\n\n/** Runs one reviewed debugging watch inside the page: console methods are hooked, error and rejection listeners installed and the longtask buffer observed for the reviewed window; every hook detaches cleanly at the end. */\nexport async function rundebugwatch(step: toolstep): Promise<stepresult> {\n const options = debugwatchoptions(step);\n if (options.window <= 0) return { ok: false, summary: \"The reviewed debug watch window is absent.\" };\n const started = Date.now();\n const entries: Array<Omit<timelineentry, \"id\" | \"runid\">> = [];\n const consoleentries: consoleentry[] = [];\n const errors: Array<Omit<errorrecord, \"id\" | \"runid\">> = [];\n const rejections: Array<Omit<rejectionrecord, \"id\" | \"runid\">> = [];\n const resources: Array<{ message: string; element: string; sourceurl: string }> = [];\n const longtasks: Array<Omit<longtaskentry, \"id\" | \"runid\">> = [];\n const floor = options.level !== undefined ? levelrank(options.level) : undefined;\n const capture = (level: loglevel, source: timelinesource, message: string, at: number): void => {\n if (floor !== undefined && levelrank(level) > floor) return;\n entries.push({ stepid: step.id, time: at, level, source, message });\n };\n const hooks: Array<() => void> = [];\n if (step.kind === \"watchconsole\") {\n for (const level of loglevels) {\n const original = console[level] as (...args: unknown[]) => void;\n const hooked = (...args: unknown[]): void => {\n try { original.apply(console, args); } catch { /* the page console may refuse the passthrough; capture still proceeds */ }\n const entry = consolecapture({ level, args, depth: options.depth, redact: options.redact });\n if (floor === undefined || levelrank(level) <= floor) consoleentries.push(entry);\n capture(level, \"console\", entry.text, Date.now());\n };\n (console as unknown as Record<string, unknown>)[level] = hooked;\n hooks.push(() => { (console as unknown as Record<string, unknown>)[level] = original; });\n }\n }\n if (step.kind === \"watcherrors\") {\n const blackbox = activeblackboxpatterns();\n const hideframes = <T extends { frames: Array<{ url: string }> }>(record: T): T => ({ ...record, frames: record.frames.filter(frame => !blackbox.some(pattern => blackboxmatches(pattern, frame.url))) });\n const onerror = (event: ErrorEvent): void => {\n const record = hideframes(errorcapture({ message: event.message, sourceurl: event.filename, line: event.lineno, ...(event.error instanceof Error ? { stacktext: event.error.stack } : {}), redact: options.redact }));\n errors.push({ ...record, stepid: step.id, at: Date.now() });\n capture(\"error\", \"error\", record.message, Date.now());\n };\n const onrejection = (event: PromiseRejectionEvent): void => {\n const reason = event.reason instanceof Error ? `${event.reason.name}: ${event.reason.message}` : String(event.reason);\n const record = hideframes(rejectioncapture({ reason, ...(event.reason instanceof Error ? { stacktext: event.reason.stack } : {}), redact: options.redact }));\n rejections.push({ ...record, stepid: step.id, at: Date.now() });\n capture(\"error\", \"rejection\", record.reason, Date.now());\n };\n const onresource = (event: Event): void => {\n const target = event.target;\n if (!(target instanceof Element)) return;\n const element = target.tagName.toLowerCase() + (target.id ? `#${target.id}` : \"\");\n const sourceurl = target instanceof HTMLImageElement || target instanceof HTMLScriptElement ? (target.src ?? \"\") : target instanceof HTMLLinkElement ? (target.href ?? \"\") : \"\";\n const message = `Failed to load ${element}${sourceurl ? ` from ${sourceurl}` : \"\"}.`;\n resources.push({ message, element, sourceurl });\n capture(\"error\", \"resource\", message, Date.now());\n };\n window.addEventListener(\"error\", onerror, true);\n window.addEventListener(\"unhandledrejection\", onrejection, true);\n window.addEventListener(\"error\", onresource, true);\n hooks.push(() => {\n window.removeEventListener(\"error\", onerror, true);\n window.removeEventListener(\"unhandledrejection\", onrejection, true);\n window.removeEventListener(\"error\", onresource, true);\n });\n }\n if (step.kind === \"watchtasks\") {\n const observer = new PerformanceObserver(list => {\n for (const entry of list.getEntries()) {\n const detail = entry as { duration: number; startTime: number; attribution?: Array<{ name?: string }> };\n const attributions = (detail.attribution ?? []).map(container => String(container.name ?? \"\")).filter(name => name.length > 0);\n longtasks.push({ stepid: step.id, duration: Math.round(detail.duration), starttime: Math.round(detail.startTime), attributions, at: Date.now() });\n }\n });\n observer.observe({ entryTypes: [\"longtask\"] });\n hooks.push(() => observer.disconnect());\n }\n await wait(options.window);\n for (const detach of hooks) detach();\n if (step.kind === \"watchtasks\") {\n const filtered = longtaskcapture({ entries: longtasks, threshold: options.threshold });\n longtasks.length = 0;\n longtasks.push(...filtered.map(task => ({ ...task, stepid: step.id, at: started })));\n for (const task of longtasks) capture(\"info\", \"longtask\", `Long task of ${task.duration} milliseconds blocked the main thread${task.attributions.length > 0 ? ` (${task.attributions.join(\", \")})` : \"\"}.`, task.at);\n }\n const summary = step.kind === \"watchconsole\"\n ? `Captured ${consoleentries.length} console call${consoleentries.length === 1 ? \"\" : \"s\"} at every level for the reviewed window of ${options.window} milliseconds.`\n : step.kind === \"watcherrors\"\n ? `Captured ${errors.length} error${errors.length === 1 ? \"\" : \"s\"}, ${rejections.length} rejection${rejections.length === 1 ? \"\" : \"s\"} and ${resources.length} resource failure${resources.length === 1 ? \"\" : \"s\"} for the reviewed window of ${options.window} milliseconds.`\n : `Captured ${longtasks.length} long task${longtasks.length === 1 ? \"\" : \"s\"} for the reviewed window of ${options.window} milliseconds.`;\n return { ok: true, summary, details: { entries, console: consoleentries, errors, rejections, resources, longtasks, watchwindow: options.window, depth: options.depth, derivation: \"Console, error and task watching derives from page-injected listeners and the performance buffers through the scripting api; no debugger permission exists in the manifest.\" } };\n}\n", "import type { toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\n\n/**\n * Page-side profiling capture for the reviewed 1.1.47 steps.\n * Correlated rules for the profiling option parsing live here while the flow, heap, cpu, shift, trace and source map math lives in the root profilers module: performance marks carry the step windows of the flow spec, the paint, navigation, longtask, layout shift and event buffers are observed for the reviewed window only, heap samples derive from the page performance memory buffer and the dom node count, and source map declarations are read from the loaded same origin scripts.\n * Every measurement derives from the performance timeline buffers and the injected instrumentation probes through the scripting api, so no debugger permission exists anywhere in the manifest.\n */\n\n/** Parses the reviewed profiling options of one profiling step; absent windows watch nothing and the depth of capture stays the reviewed window only. */\nexport function profilestepoptions(step: toolstep): {\n flow?: { prefix: string; steps: string[]; metrics: string[] };\n watchwindow: number;\n heapinterval: number;\n growth?: { slope: number; interval: number };\n duration: number;\n threshold: number;\n categories: string[];\n exporttarget?: string;\n traceid?: string;\n scripts: string[];\n} {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const watch = options.watch && typeof options.watch === \"object\" && !Array.isArray(options.watch) ? options.watch as Record<string, unknown> : {};\n const flow = options.flow && typeof options.flow === \"object\" && !Array.isArray(options.flow) ? options.flow as Record<string, unknown> : undefined;\n const heap = options.heap && typeof options.heap === \"object\" && !Array.isArray(options.heap) ? options.heap as Record<string, unknown> : {};\n const growth = options.growth && typeof options.growth === \"object\" && !Array.isArray(options.growth) ? options.growth as Record<string, unknown> : undefined;\n const profile = options.profile && typeof options.profile === \"object\" && !Array.isArray(options.profile) ? options.profile as Record<string, unknown> : {};\n const trace = options.trace && typeof options.trace === \"object\" && !Array.isArray(options.trace) ? options.trace as Record<string, unknown> : {};\n return {\n ...(flow !== undefined && typeof flow.prefix === \"string\" && Array.isArray(flow.steps) && Array.isArray(flow.metrics) ? { flow: { prefix: flow.prefix, steps: flow.steps.filter((item): item is string => typeof item === \"string\"), metrics: flow.metrics.filter((item): item is string => typeof item === \"string\") } } : {}),\n watchwindow: typeof watch.window === \"number\" && Number.isFinite(watch.window) && watch.window >= 0 ? watch.window : 0,\n heapinterval: typeof heap.interval === \"number\" && Number.isFinite(heap.interval) && heap.interval >= 0 ? heap.interval : 0,\n ...(growth !== undefined && typeof growth.slope === \"number\" ? { growth: { slope: growth.slope, interval: typeof growth.interval === \"number\" && Number.isFinite(growth.interval) && growth.interval >= 0 ? growth.interval : 0 } } : {}),\n duration: typeof profile.duration === \"number\" && Number.isFinite(profile.duration) && profile.duration >= 0 ? profile.duration : 0,\n threshold: typeof options.threshold === \"number\" && Number.isFinite(options.threshold) && options.threshold >= 0 ? options.threshold : 0,\n categories: Array.isArray(trace.categories) ? trace.categories.filter((category): category is string => typeof category === \"string\") : [],\n ...(typeof trace.exporttarget === \"string\" ? { exporttarget: trace.exporttarget } : {}),\n ...(typeof trace.traceid === \"string\" ? { traceid: trace.traceid } : {}),\n scripts: Array.isArray(options.scripts) ? options.scripts.filter((url): url is string => typeof url === \"string\") : [],\n };\n}\n\n/** Resolves the reviewed trace category of one performance entry: navigation entries stay navigation, marks, measures, long tasks and event timing stay scripting, paint and layout shifts stay painting, resources stay loading while fetch and xmlhttprequest initiators stay network. */\nfunction categoryof(entry: PerformanceEntry, initiator?: string): string {\n if (entry.entryType === \"navigation\") return \"navigation\";\n if (entry.entryType === \"paint\" || entry.entryType === \"largest-contentful-paint\" || entry.entryType === \"layout-shift\") return \"painting\";\n if (entry.entryType === \"resource\") return initiator === \"fetch\" || initiator === \"xmlhttprequest\" ? \"network\" : \"loading\";\n return \"scripting\";\n}\n\n/** Collects the performance buffer entries of the reviewed types for the observed window as plain rows; the buffered observer covers the paint, navigation, longtask, layout shift, event and first input entries the getEntriesByType buffer misses. */\nasync function collectentries(watchwindow: number, types: string[]): Promise<Array<{ name: string; type: string; start: number; duration: number; initiator?: string }>> {\n const rows: Array<{ name: string; type: string; start: number; duration: number; initiator?: string }> = [];\n for (const type of [\"navigation\", \"paint\", \"mark\", \"measure\", \"resource\", \"longtask\"]) {\n for (const entry of performance.getEntriesByType(type)) {\n rows.push({ name: entry.name, type: entry.entryType, start: entry.startTime, duration: entry.duration, ...(type === \"resource\" ? { initiator: (entry as PerformanceResourceTiming).initiatorType } : {}) });\n }\n }\n if (types.length > 0) {\n await new Promise<void>(resolve => {\n const observer = new PerformanceObserver(list => {\n for (const entry of list.getEntries()) rows.push({ name: entry.name, type: entry.entryType, start: entry.startTime, duration: entry.duration });\n });\n observer.observe({ entryTypes: types, buffered: true } as PerformanceObserverInit);\n window.setTimeout(() => { observer.disconnect(); resolve(); }, Math.max(0, watchwindow));\n });\n } else {\n await new Promise(resolve => window.setTimeout(resolve, Math.max(0, watchwindow)));\n }\n return rows;\n}\n\n/** Reads the page heap sample: the used and limit bytes of the performance memory buffer with the dom node count, the honest derivation because no heap profiler exists without the debugger permission. */\nfunction heapsample(): { usedbytes: number; limitbytes: number; nodecount: number } {\n const memory = (performance as Performance & { memory?: { usedJSHeapSize: number; totalJSHeapSize: number; jsHeapSizeLimit: number } }).memory;\n return { usedbytes: memory?.usedJSHeapSize ?? 0, limitbytes: memory?.jsHeapSizeLimit ?? memory?.totalJSHeapSize ?? 0, nodecount: document.querySelectorAll(\"*\").length };\n}\n\nfunction wait(milliseconds: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, Math.max(0, milliseconds)));\n}\n\n/** Runs one reviewed profiling step inside the page: the flow spec marks the start and end of every step in the window while the performance buffers feed the metric math of the root profilers module, the heap snapshot reads the memory buffer and the node count, the cpu window observes the long task and event timing samples, the layout shift watch scores the shifts of the window with their impacted selectors, the trace record categorizes the observed entries of the reviewed categories, and the source map capture reads the sourceMappingURL declarations of the loaded same origin scripts. */\nexport async function runprofilestep(step: toolstep): Promise<stepresult> {\n const options = profilestepoptions(step);\n if (step.kind === \"measureflow\") {\n if (!options.flow || options.watchwindow <= 0) return { ok: false, summary: \"The reviewed flow spec with its watch window is absent.\" };\n const started = performance.now();\n for (const stepid of options.flow.steps) performance.mark(`${options.flow.prefix}:${stepid}:start`);\n const entries = await collectentries(options.watchwindow, [\"largest-contentful-paint\", \"first-input\", \"event\", \"longtask\"]);\n for (const stepid of options.flow.steps) performance.mark(`${options.flow.prefix}:${stepid}:end`);\n for (const stepid of options.flow.steps) performance.measure(`${options.flow.prefix}:${stepid}`, `${options.flow.prefix}:${stepid}:start`, `${options.flow.prefix}:${stepid}:end`);\n return { ok: true, summary: `Marked the start and end of ${options.flow.steps.length} step${options.flow.steps.length === 1 ? \"\" : \"s\"} of the flow ${options.flow.prefix} and collected ${entries.length} performance entr${entries.length === 1 ? \"y\" : \"ies\"} for the reviewed window of ${options.watchwindow} milliseconds.`, details: { entries, watchwindow: options.watchwindow, started, derivation: \"Flow measurement derives from the performance timeline buffers and the injected marks through the scripting api; no debugger permission exists in the manifest.\" } };\n }\n if (step.kind === \"heapshot\") {\n const sample = heapsample();\n return { ok: true, summary: `Captured the on demand heap sample of ${sample.usedbytes} used bytes against the ${sample.limitbytes} byte limit with ${sample.nodecount} dom node${sample.nodecount === 1 ? \"\" : \"s\"}.`, details: { ...sample, derivation: \"Heap bytes derive from the page performance memory buffer and the node count from the dom because no heap profiler exists without the debugger permission.\" } };\n }\n if (step.kind === \"trackmemory\") {\n if (!options.growth) return { ok: false, summary: \"The reviewed growth slope is absent.\" };\n const sample = heapsample();\n return { ok: true, summary: `Took the heap sample of ${sample.usedbytes} used bytes beside the step for the growth tracking of slope ${options.growth.slope} bytes per millisecond.`, details: { ...sample, slope: options.growth.slope, interval: options.growth.interval, derivation: \"Growth samples derive from the page performance memory buffer beside every step of the run.\" } };\n }\n if (step.kind === \"profilecpu\") {\n if (options.duration <= 0) return { ok: false, summary: \"The reviewed cpu profile duration is absent.\" };\n const started = performance.now();\n const entries = await collectentries(options.duration, [\"longtask\", \"event\", \"first-input\"]);\n const samples = entries.filter(entry => entry.type === \"longtask\" || entry.type === \"event\" || entry.type === \"first-input\").map(entry => ({ name: entry.name || entry.type, time: entry.duration }));\n return { ok: true, summary: `Profiled the cpu window of ${options.duration} milliseconds with ${samples.length} sample${samples.length === 1 ? \"\" : \"s\"} from the long task and event timing buffers.`, details: { samples, duration: options.duration, started, derivation: \"Cpu samples derive from the long task attribution and event timing buffers because no sampling profiler exists without the debugger permission.\" } };\n }\n if (step.kind === \"watchshifts\") {\n if (options.watchwindow <= 0) return { ok: false, summary: \"The reviewed layout shift window is absent.\" };\n const shifts: Array<{ score: number; starttime: number; selectors: string[] }> = [];\n await new Promise<void>(resolve => {\n const observer = new PerformanceObserver(list => {\n for (const entry of list.getEntries()) {\n const shift = entry as PerformanceEntry & { value?: number; sources?: Array<{ node?: Node }> };\n const selectors = (shift.sources ?? []).flatMap(source => source.node instanceof Element ? [source.node.tagName.toLowerCase() + (source.node.id ? `#${source.node.id}` : \"\")] : []);\n const score = typeof shift.value === \"number\" ? shift.value : 0;\n if (options.threshold > 0 && score < options.threshold) continue;\n shifts.push({ score, starttime: shift.startTime, selectors });\n }\n });\n observer.observe({ entryTypes: [\"layout-shift\"], buffered: true } as PerformanceObserverInit);\n window.setTimeout(() => { observer.disconnect(); resolve(); }, options.watchwindow);\n });\n return { ok: true, summary: `Watched ${shifts.length} layout shift${shifts.length === 1 ? \"\" : \"s\"} for the reviewed window of ${options.watchwindow} milliseconds${options.threshold > 0 ? ` with the score threshold ${options.threshold}` : \"\"}.`, details: { shifts, watchwindow: options.watchwindow, derivation: \"Layout shifts derive from the performance layout-shift buffer with the impacted element selectors of the shift sources.\" } };\n }\n if (step.kind === \"traceload\") {\n if (options.categories.length === 0 || options.watchwindow <= 0) return { ok: false, summary: \"The reviewed trace categories or window are absent.\" };\n const started = performance.now();\n const entries = await collectentries(options.watchwindow, [\"largest-contentful-paint\", \"first-input\", \"event\", \"longtask\", \"layout-shift\"]);\n const events = entries.filter(entry => options.categories.includes(categoryof({ name: entry.name, entryType: entry.type, startTime: entry.start, duration: entry.duration } as PerformanceEntry, entry.initiator))).map(entry => ({ name: entry.name, category: categoryof({ name: entry.name, entryType: entry.type, startTime: entry.start, duration: entry.duration } as PerformanceEntry, entry.initiator), offset: Math.round(entry.start - started) }));\n return { ok: true, summary: `Recorded ${events.length} trace event${events.length === 1 ? \"\" : \"s\"} of the reviewed categories ${options.categories.join(\", \")} for the window of ${options.watchwindow} milliseconds and derived the exportable trace file.`, details: { events, categories: options.categories, watchwindow: options.watchwindow, started, ...(options.exporttarget !== undefined ? { exporttarget: options.exporttarget } : {}), derivation: \"The trace file derives from the performance timeline entries of the reviewed categories; it is not the devtools binary trace format because no debugger permission exists in the manifest.\" } };\n }\n if (step.kind === \"capturesourcemaps\") {\n const scripts: Array<{ url: string; mapurl?: string }> = [];\n for (const element of document.querySelectorAll(\"script[src]\")) {\n const src = (element as HTMLScriptElement).src;\n if (!src.startsWith(location.origin)) continue;\n if (options.scripts.length > 0 && !options.scripts.includes(src)) continue;\n let mapurl: string | undefined;\n try {\n const response = await fetch(src, { credentials: \"same-origin\" });\n const source = await response.text();\n const match = /[#@]\\s*sourceMappingURL=(\\S+)/.exec(source);\n if (match !== null && match[1] !== undefined) mapurl = new URL(match[1], src).toString();\n } catch { /* a script the page refuses to re-fetch stays without a captured map; the capture continues */ }\n scripts.push({ url: src, ...(mapurl !== undefined ? { mapurl } : {}) });\n }\n const withmaps = scripts.filter(script => script.mapurl !== undefined);\n return { ok: true, summary: `Read the sourceMappingURL declarations of ${scripts.length} same origin script${scripts.length === 1 ? \"\" : \"s\"} of ${location.origin} and found ${withmaps.length} map declaration${withmaps.length === 1 ? \"\" : \"s\"}; the script sources stay in the page bridge and only the map urls leave it.`, details: { scripts, origin: location.origin, derivation: \"Source map declarations are read by re-fetching the loaded same origin scripts of the page; cross origin scripts stay outside the capture and no map content enters the page bridge.\" } };\n }\n return { ok: false, summary: \"The profiling step is not part of the instrumented family.\" };\n}\n", "import type { fielderror, fieldkind, fieldmatch, formrecord, formentry, formreport, toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { clean, elementlabel as label, elementselector as selector } from \"./pageresolve.js\";\n\n/**\n * Form field logics for reviewed steps.\n * Every correlated rule for field matching, field kind classification, seeded value generation, native value fills, honeypot detection, login and template detection, error association and form record parsing lives in this file.\n */\n\n/** One serializable form field shape resolved against the page controls. */\nexport interface fieldshape {\n selector: string;\n tag: string;\n type: string;\n name: string;\n label: string;\n placeholder: string;\n arialabel: string;\n autocomplete: string;\n options?: string[];\n}\n\n/** One surveyed field shape carrying the visibility, geometry and timing evidence the honeypot detector reads. */\nexport interface fieldsurvey extends fieldshape {\n hidden: boolean;\n offscreen: boolean;\n createdat?: number;\n}\n\n/** One honeypot field flagged by hidden, offscreen or time trap evidence. */\nexport interface honeypotevidence {\n selector: string;\n reason: \"hidden\" | \"offscreen\" | \"timetrap\";\n}\n\n/** One surveyed field with the aria describedby ref and the sibling message texts the error reader associates. */\nexport interface errorcontext extends fieldshape {\n describedby?: string;\n siblings: string[];\n}\n\n/** Resolves controls by label, placeholder, aria label and name attributes; matches are case insensitive substrings. */\nexport function matchfield(fields: fieldshape[], match: fieldmatch): fieldshape[] {\n const key = match.mode === \"label\" ? \"label\" : match.mode === \"placeholder\" ? \"placeholder\" : match.mode === \"arialabel\" ? \"arialabel\" : \"name\";\n const needle = (match[key] ?? \"\").trim().toLowerCase();\n if (!needle) return [];\n return fields.filter(field => {\n const primary = (field[key] as string).toLowerCase();\n const secondary = match.mode === \"label\" || match.mode === \"name\" ? field.name.toLowerCase() : match.mode === \"placeholder\" ? field.arialabel.toLowerCase() : field.placeholder.toLowerCase();\n return primary.includes(needle) || secondary.includes(needle);\n });\n}\n\n/** Infers the field kind of one control from its input type, autocomplete hint and label text. */\nexport function classifyfield(input: { type: string; autocomplete: string; label: string }): fieldkind {\n const type = input.type.toLowerCase();\n const autocomplete = input.autocomplete.toLowerCase();\n const label = input.label.toLowerCase();\n if (type === \"password\") return \"password\";\n if (autocomplete.startsWith(\"cc-\") || label.includes(\"card number\") || label.includes(\"credit card\") || label.includes(\"cardholder\")) return \"card\";\n if (autocomplete.includes(\"one-time-code\") || autocomplete.includes(\"otp\") || label.includes(\"one time code\") || label.includes(\"verification code\") || label.includes(\"otp\")) return \"code\";\n if (type === \"email\" || autocomplete.includes(\"email\") || label.includes(\"email\")) return \"email\";\n if (type === \"tel\" || autocomplete.includes(\"tel\") || label.includes(\"phone\") || label.includes(\"telephone\")) return \"phone\";\n if (type === \"date\") return \"date\";\n if (type === \"number\") return \"number\";\n if (type === \"checkbox\") return \"check\";\n if (type === \"radio\") return \"radio\";\n if (type === \"file\") return \"file\";\n if (type === \"select\" || type === \"select-one\") return \"select\";\n return \"text\";\n}\n\nconst firstnames: Record<string, string[]> = { en: [\"alex\", \"jordan\", \"taylor\", \"morgan\", \"casey\"], pt: [\"ana\", \"bruno\", \"carla\", \"diego\", \"helena\"] };\nconst lastnames: Record<string, string[]> = { en: [\"brooks\", \"carter\", \"diaz\", \"evans\", \"reyes\"], pt: [\"alves\", \"costa\", \"lima\", \"souza\", \"moraes\"] };\n\nfunction localekey(locale: string): string {\n const normalized = locale.toLowerCase();\n if (normalized.startsWith(\"pt\")) return \"pt\";\n return \"en\";\n}\n\n/** Generates one realistic value for a field kind, deterministically seeded and locale aware for names, emails and phones. */\nexport function generatevalue(kind: fieldkind, rule: { locale?: string; seed?: number }): string {\n const seed = typeof rule.seed === \"number\" && Number.isFinite(rule.seed) ? Math.abs(Math.floor(rule.seed)) : 1;\n const names = firstnames[localekey(rule.locale ?? \"en\")] ?? firstnames.en ?? [\"alex\"];\n const surnames = lastnames[localekey(rule.locale ?? \"en\")] ?? lastnames.en ?? [\"brooks\"];\n let state = seed * 1103515245 + 12345;\n const next = (): number => { state = (state * 1103515245 + 12345) % 2147483648; return state / 2147483648; };\n const pick = <T>(items: T[]): T => items[Math.floor(next() * items.length) % items.length] ?? items[0] as T;\n const digits = (count: number): string => Array.from({ length: count }, () => String(Math.floor(next() * 10))).join(\"\");\n const person = `${pick(names)} ${pick(surnames)}`;\n switch (kind) {\n case \"email\": return `${person.replace(\" \", \".\")}${digits(2)}@example.com`;\n case \"phone\": return localekey(rule.locale ?? \"en\") === \"pt\" ? `+55 (11) 9${digits(4)}-${digits(4)}` : `+1 (555) 010-${digits(4)}`;\n case \"date\": return `${2024 + Math.floor(next() * 2)}-${String(1 + Math.floor(next() * 12)).padStart(2, \"0\")}-${String(1 + Math.floor(next() * 28)).padStart(2, \"0\")}`;\n case \"number\": return String(Math.floor(next() * 1000));\n case \"select\": return `option ${1 + Math.floor(next() * 5)}`;\n case \"check\": return next() > 0.5 ? \"true\" : \"false\";\n case \"radio\": return `choice ${1 + Math.floor(next() * 4)}`;\n case \"file\": return `sample${digits(2)}.pdf`;\n case \"password\": return `pw-${digits(6)}-${pick(names)}`;\n case \"card\": return `4111 ${digits(4)} ${digits(4)} ${digits(4)}`;\n case \"code\": return digits(6);\n default: return person;\n }\n}\n\n/** Builds a deterministic values hash of the reviewed field values a submission ticket records. */\nexport function valueshash(values: Array<{ label: string; value: string }>): string {\n const source = values.map(entry => `${entry.label}=${entry.value}`).join(\"|\");\n let hash = 5381;\n for (let index = 0; index < source.length; index += 1) hash = ((hash * 33) ^ source.charCodeAt(index)) >>> 0;\n return hash.toString(16);\n}\n\n/** Parses the reviewed structured form record of a step; null when the step reviews none or the shape is invalid. */\nexport function parseformrecord(value: unknown): formrecord | null {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return null;\n const record = value as Record<string, unknown>;\n if (!Array.isArray(record.entries)) return null;\n const entries: formentry[] = [];\n for (const item of record.entries) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) continue;\n const entry = item as Record<string, unknown>;\n const match = entry.match;\n if (!match || typeof match !== \"object\" || Array.isArray(match)) continue;\n const shapes = match as Record<string, unknown>;\n if (typeof shapes.mode !== \"string\") continue;\n const fieldmatch: fieldmatch = {\n mode: shapes.mode as fieldmatch[\"mode\"],\n ...(typeof shapes.label === \"string\" ? { label: shapes.label } : {}),\n ...(typeof shapes.placeholder === \"string\" ? { placeholder: shapes.placeholder } : {}),\n ...(typeof shapes.arialabel === \"string\" ? { arialabel: shapes.arialabel } : {}),\n ...(typeof shapes.name === \"string\" ? { name: shapes.name } : {}),\n };\n if (typeof entry.kind !== \"string\" || typeof entry.value !== \"string\") continue;\n entries.push({ match: fieldmatch, kind: entry.kind as fieldkind, value: entry.value });\n }\n if (entries.length === 0) return null;\n return { ...(typeof record.form === \"string\" && record.form ? { form: record.form } : {}), entries };\n}\n\n/** Builds form record entries from reviewed label or placeholder value pairs. */\nexport function pairentries(pairs: Array<{ label?: string; placeholder?: string; value: string }>, mode: \"label\" | \"placeholder\"): formrecord {\n return { entries: pairs.map(pair => ({ match: mode === \"label\" ? { mode, label: pair.label ?? \"\" } : { mode, placeholder: pair.placeholder ?? \"\" }, kind: \"text\", value: pair.value })) };\n}\n\n/** One fill operation outcome: the entry, the matched control, the honeypot skip flag or the refusal reason. */\nexport interface filloutcome {\n entry: formentry;\n matched?: fieldshape;\n skipped?: boolean;\n reason?: string;\n}\n\n/** Resolves every entry of a form record against the surveyed fields, skipping honeypots and refusing unmatched or ambiguous entries. */\nexport function filloperations(record: formrecord, fields: fieldshape[], skippedselectors: string[] = []): filloutcome[] {\n return record.entries.map(entry => {\n const matches = matchfield(fields, entry.match);\n if (matches.length === 0) return { entry, reason: \"unmatched\" };\n if (matches.length > 1) return { entry, reason: \"ambiguous\" };\n const matched = matches[0] as fieldshape;\n if (skippedselectors.includes(matched.selector)) return { entry, matched, skipped: true };\n return { entry, matched };\n });\n}\n\n/** Masks one card segment so side panels can render card fills without exposing the full value. */\nexport function cardmask(value: string): string {\n const trimmed = value.trim();\n if (/^\\d[\\d\\s-]{11,18}$/.test(trimmed)) {\n const compact = trimmed.replace(/[\\s-]/g, \"\");\n const last = compact.slice(-4);\n return `${\"\u2022\".repeat(Math.max(0, compact.length - 4))}${last}`;\n }\n return \"\u2022\".repeat(trimmed.length);\n}\n\n/** Flags hidden, offscreen and time trap fields so fill steps skip them instead of tripping anti bot defenses. */\nexport function detecthoneypots(surveys: fieldsurvey[], loadedat: number): honeypotevidence[] {\n const traps: honeypotevidence[] = [];\n for (const field of surveys) {\n if (field.hidden) traps.push({ selector: field.selector, reason: \"hidden\" });\n else if (field.offscreen) traps.push({ selector: field.selector, reason: \"offscreen\" });\n else if (field.createdat !== undefined && loadedat > 0 && field.createdat > loadedat) traps.push({ selector: field.selector, reason: \"timetrap\" });\n }\n return traps;\n}\n\n/** Detects a login form: a password field plus an identifier field with session links nearby. */\nexport function detectlogin(fields: fieldshape[], links: string[]): { login: boolean; markers: string[] } {\n const markers: string[] = [];\n const password = fields.find(field => classifyfield(field) === \"password\");\n if (password) markers.push(\"password field\");\n const identifier = fields.find(field => {\n const kind = classifyfield(field);\n return kind === \"email\" || (kind === \"text\" && /user|login|account|identifier/i.test(`${field.name} ${field.label}`));\n });\n if (identifier) markers.push(\"identifier field\");\n const sessionlink = links.some(link => /sign in|log in|log on|forgot|create account|sign up/i.test(link));\n if (sessionlink) markers.push(\"session link\");\n return { login: Boolean(password && identifier && sessionlink), markers };\n}\n\nconst signupmarkers = [\"sign up\", \"create account\", \"register\", \"confirm password\", \"terms\"];\nconst checkoutmarkers = [\"checkout\", \"payment\", \"billing\", \"shipping\", \"card number\", \"place order\", \"cart\"];\n\n/** Detects signup and checkout templates by matching the field labels, autocompletes and page text against known markers. */\nexport function detecttemplate(fields: fieldshape[], text: string): { template: \"signup\" | \"checkout\" | \"unknown\"; markers: string[] } {\n const corpus = [text, ...fields.map(field => `${field.label} ${field.name} ${field.placeholder} ${field.arialabel} ${field.autocomplete}`)].join(\" \").toLowerCase();\n const signup = signupmarkers.filter(marker => corpus.includes(marker));\n const checkout = checkoutmarkers.filter(marker => corpus.includes(marker));\n if (signup.length >= 2 && signup.length >= checkout.length) return { template: \"signup\", markers: signup };\n if (checkout.length >= 2) return { template: \"checkout\", markers: checkout };\n return { template: \"unknown\", markers: [...signup, ...checkout] };\n}\n\n/** Associates validation messages with fields through aria describedby refs and the sibling text next to each field. */\nexport function associateerrors(contexts: errorcontext[], messages: Array<{ id?: string; text: string }>): fielderror[] {\n const errors: fielderror[] = [];\n for (const field of contexts) {\n const byref = field.describedby ? messages.find(message => message.id === field.describedby && message.text.trim()) : undefined;\n if (byref) { errors.push({ field: field.selector, message: byref.text.trim() }); continue; }\n const sibling = field.siblings.map(text => text.trim()).find(text => text.length > 0);\n if (sibling) errors.push({ field: field.selector, message: sibling });\n }\n return errors;\n}\n\n/** Resolves one reviewed artifact name against the run store before a file input is filled. */\nexport function attachplan(name: string, artifacts: Array<{ id: string; name: string; kind: string }>): { artifact?: { id: string; name: string; kind: string }; reason?: string } {\n const artifact = artifacts.find(item => item.name === name || item.id === name);\n if (!artifact) return { reason: \"The reviewed artifact name is not part of the run store.\" };\n return { artifact };\n}\n\n/** Selectors the captcha detector probes; a hit hands control back to the user instead of forcing the page. */\nexport const captchamarkers = ['iframe[src*=\"recaptcha\"]', 'iframe[title*=\"recaptcha\" i]', '.g-recaptcha', '[data-sitekey]', 'iframe[title*=\"captcha\" i]', '.h-captcha'];\n\n/** True when any captcha marker matched, so the plan pauses and hands control to the user. */\nexport function captchadetected(matched: string[]): boolean {\n return matched.length > 0;\n}\n\nfunction events(target: Element): void {\n target.dispatchEvent(new Event(\"input\", { bubbles: true }));\n target.dispatchEvent(new Event(\"change\", { bubbles: true }));\n}\n\n/** Fills one control through the native setter with input and change events; checks, radios and selects use their own grammar. */\nexport function fillcontrol(element: Element, entry: formentry): boolean {\n if (element instanceof HTMLInputElement && (entry.kind === \"check\" || element.type === \"checkbox\")) { element.checked = entry.value === \"true\" || entry.value === \"on\" || entry.value === \"checked\"; events(element); return true; }\n if (element instanceof HTMLInputElement && (entry.kind === \"radio\" || element.type === \"radio\")) { element.checked = true; events(element); return true; }\n if (element instanceof HTMLSelectElement) {\n const option = [...element.options].find(candidate => candidate.value === entry.value || candidate.textContent?.trim() === entry.value);\n if (!option) return false;\n element.value = option.value;\n events(element);\n return true;\n }\n if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement) {\n if (element instanceof HTMLInputElement && element.type === \"file\") return false;\n element.focus();\n const setter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(element), \"value\")?.set;\n if (setter) setter.call(element, entry.value); else element.value = entry.value;\n events(element);\n return true;\n }\n return false;\n}\n\nfunction controlshape(element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement): fieldshape {\n return {\n selector: selector(element),\n tag: element.tagName.toLowerCase(),\n type: element instanceof HTMLSelectElement ? \"select\" : element.getAttribute(\"type\") || \"text\",\n name: element.getAttribute(\"name\") || \"\",\n label: label(element),\n placeholder: element.getAttribute(\"placeholder\") || \"\",\n arialabel: element.getAttribute(\"aria-label\") || \"\",\n autocomplete: element.getAttribute(\"autocomplete\") || \"\",\n ...(element instanceof HTMLSelectElement ? { options: [...element.options].map(option => option.value) } : {}),\n };\n}\n\n/** Collects the serializable field shapes of one form scope; absent scopes survey the whole document. */\nfunction collectfields(root: Document, formscope?: string): Array<{ shape: fieldshape; element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement }> {\n const scope = formscope ? root.querySelector(formscope) : root;\n if (!scope) return [];\n const controls = [...scope.querySelectorAll<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>(\"input, select, textarea\")];\n return controls.filter(element => element.type !== \"hidden\").map(element => ({ shape: controlshape(element), element }));\n}\n\n/** Surveys the visibility and geometry evidence the honeypot detector reads for one form scope. */\nfunction surveyfields(root: Document, formscope?: string): Array<{ survey: fieldsurvey; element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement }> {\n const viewport = { left: 0, top: 0, right: window.innerWidth || 0, bottom: window.innerHeight || 0 };\n return collectfields(root, formscope).map(({ shape, element }) => {\n const rect = element.getBoundingClientRect();\n const hidden = element.getAttribute(\"aria-hidden\") === \"true\" || element.tabIndex < 0 && (element as HTMLElement).offsetParent === null || (element as HTMLElement).offsetParent === null && rect.width === 0 && rect.height === 0;\n const offscreen = rect.width > 0 && rect.height > 0 && (rect.bottom < viewport.top || rect.top > viewport.bottom || rect.right < viewport.left || rect.left > viewport.right);\n return { survey: { ...shape, hidden, offscreen }, element };\n });\n}\n\n/** Reads the error context of one form scope: describedby refs and the sibling texts after each field. */\nfunction collecterrorcontext(root: Document, formscope?: string): Array<{ context: errorcontext; element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement }> {\n return collectfields(root, formscope).map(({ shape, element }) => {\n const siblings: string[] = [];\n let neighbor = element.nextElementSibling;\n for (let index = 0; neighbor && index < 3; index += 1) {\n const text = clean(neighbor.textContent || \"\");\n if (text && text !== shape.label) siblings.push(text);\n neighbor = neighbor.nextElementSibling;\n }\n const describedby = element.getAttribute(\"aria-describedby\");\n return { context: { ...shape, ...(describedby ? { describedby } : {}), siblings }, element };\n });\n}\n\n/** Runs one reviewed forms and data step inside the page: fills, surveys, detects and reads errors without leaving the form scope. */\nexport function runpageform(step: toolstep, target: Element | null, root: Document = document): stepresult | Promise<stepresult> {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const formscope = typeof options.form === \"string\" && options.form ? options.form : step.target;\n switch (step.kind) {\n case \"fillform\": {\n const record = parseformrecord(options.formrecord);\n if (!record) return { ok: false, summary: \"A reviewed form record with entries is required in options.\" };\n const surveys = surveyfields(root, record.form);\n const honeypots = detecthoneypots(surveys.map(entry => entry.survey), 0);\n const operations = filloperations(record, surveys.map(entry => entry.survey), honeypots.map(trap => trap.selector));\n let filled = 0;\n const skipped: string[] = [];\n const failures: string[] = [];\n const values: Array<{ label: string; value: string }> = [];\n for (const operation of operations) {\n if (operation.skipped && operation.matched) { skipped.push(operation.matched.selector); continue; }\n if (!operation.matched) { failures.push(`${operation.reason}: ${operation.entry.match.label ?? operation.entry.match.name ?? operation.entry.match.placeholder ?? \"field\"}`); continue; }\n const element = surveys.find(entry => entry.survey.selector === operation.matched?.selector)?.element;\n if (!element || !fillcontrol(element, operation.entry)) { failures.push(`unfillable: ${operation.matched.selector}`); continue; }\n filled += 1;\n values.push({ label: operation.matched.label || operation.matched.name, value: operation.entry.kind === \"password\" ? \"\" : operation.entry.value });\n }\n const report: formreport = { form: record.form ?? \"\", fields: operations.map(operation => ({ selector: operation.matched?.selector ?? \"\", label: operation.matched?.label ?? operation.entry.match.label ?? \"\", kind: operation.entry.kind, matched: Boolean(operation.matched) })) };\n return {\n ok: failures.length === 0,\n summary: failures.length === 0 ? `Filled ${filled} reviewed field${filled === 1 ? \"\" : \"s\"} from the structured record${skipped.length > 0 ? ` and skipped ${skipped.length} honeypot field${skipped.length === 1 ? \"\" : \"s\"}` : \"\"}.` : `Filled ${filled} of ${record.entries.length} reviewed fields; ${failures.length} refusals: ${failures.join(\"; \")}.`,\n details: { filled, skipped, failures, values, report },\n };\n }\n case \"filllabel\":\n case \"fillplaceholder\": {\n const mode = step.kind === \"filllabel\" ? \"label\" : \"placeholder\";\n const pairs = Array.isArray(options.fields) ? (options.fields as Array<Record<string, unknown>>).filter(item => item && typeof item === \"object\") : [];\n const record = pairentries(pairs.map(pair => ({ label: typeof pair.label === \"string\" ? pair.label : \"\", placeholder: typeof pair.placeholder === \"string\" ? pair.placeholder : \"\", value: typeof pair.value === \"string\" ? pair.value : \"\" })), mode);\n if (record.entries.length === 0) return { ok: false, summary: \"A reviewed non-empty list of field pairs is required in options.\" };\n const surveys = surveyfields(root, formscope);\n const honeypots = detecthoneypots(surveys.map(entry => entry.survey), 0);\n const operations = filloperations(record, surveys.map(entry => entry.survey), honeypots.map(trap => trap.selector));\n let filled = 0;\n const failures: string[] = [];\n for (const operation of operations) {\n if (operation.skipped) continue;\n if (!operation.matched) { failures.push(`${operation.reason}: ${mode === \"label\" ? operation.entry.match.label : operation.entry.match.placeholder}`); continue; }\n const element = surveys.find(entry => entry.survey.selector === operation.matched?.selector)?.element;\n const refined: formentry = { ...operation.entry, kind: classifyfield(operation.matched) };\n if (!element || !fillcontrol(element, refined)) { failures.push(`unfillable: ${operation.matched.selector}`); continue; }\n filled += 1;\n }\n return { ok: failures.length === 0, summary: failures.length === 0 ? `Filled ${filled} field${filled === 1 ? \"\" : \"s\"} matched by ${mode}.` : `Filled ${filled} of ${record.entries.length} fields matched by ${mode}; ${failures.join(\"; \")}.`, details: { filled, failures, mode } };\n }\n case \"detectfields\": {\n const collected = collectfields(root, formscope);\n const report: formreport = { form: formscope ?? \"\", fields: collected.map(entry => ({ selector: entry.shape.selector, label: entry.shape.label || entry.shape.name, kind: classifyfield(entry.shape), matched: Boolean(entry.shape.label || entry.shape.name) })) };\n return { ok: true, summary: `Detected ${collected.length} form field${collected.length === 1 ? \"\" : \"s\"} with their kinds.`, details: { report, count: collected.length } };\n }\n case \"generatevalues\": {\n const rule = options.valuegen && typeof options.valuegen === \"object\" && !Array.isArray(options.valuegen) ? options.valuegen as Record<string, unknown> : {};\n const locale = typeof rule.locale === \"string\" ? rule.locale : \"en\";\n const seed = typeof rule.seed === \"number\" && Number.isFinite(rule.seed) ? rule.seed : 1;\n const surveys = surveyfields(root, formscope);\n const honeypots = detecthoneypots(surveys.map(entry => entry.survey), 0);\n const skippedselectors = new Set(honeypots.map(trap => trap.selector));\n const candidates = surveys.filter(entry => !skippedselectors.has(entry.survey.selector));\n const values = candidates.map(entry => ({ label: entry.survey.label || entry.survey.name || entry.survey.selector, kind: classifyfield(entry.survey), value: generatevalue(classifyfield(entry.survey), { locale, seed }) }));\n const single = values.length === 0 && typeof rule.kind === \"string\" ? [{ label: rule.kind, kind: rule.kind, value: generatevalue(rule.kind as fieldkind, { locale, seed }) }] : values;\n return { ok: true, summary: `Generated ${single.length} realistic value${single.length === 1 ? \"\" : \"s\"} for the detected field kinds.`, details: { values: single, locale, seed } };\n }\n case \"readerrors\": {\n const contexts = collecterrorcontext(root, formscope);\n const messages = [...root.querySelectorAll<HTMLElement>(\"[id]\")].map(element => ({ id: element.id, text: clean(element.textContent || \"\") })).filter(message => message.text.length > 0);\n const errors = associateerrors(contexts.map(entry => entry.context), messages);\n return { ok: true, summary: errors.length === 0 ? \"No validation error was found next to the reviewed fields.\" : `Collected ${errors.length} inline validation message${errors.length === 1 ? \"\" : \"s\"}.`, details: { errors, form: formscope ?? \"\" } };\n }\n case \"skiphoneypot\": {\n const surveys = surveyfields(root, formscope);\n const traps = detecthoneypots(surveys.map(entry => entry.survey), 0);\n return { ok: true, summary: traps.length === 0 ? \"No honeypot field was detected.\" : `Skipped ${traps.length} honeypot field${traps.length === 1 ? \"\" : \"s\"}: ${traps.map(trap => `${trap.selector} (${trap.reason})`).join(\", \")}.`, details: { skipped: traps } };\n }\n case \"detectlogin\": {\n const collected = collectfields(root, formscope);\n const links = [...(formscope ? root.querySelectorAll(formscope) : [root] as unknown as Element[])].flatMap(scope => [...scope.querySelectorAll(\"a[href], button\")]).map(element => clean(element.textContent || \"\"));\n const detection = detectlogin(collected.map(entry => entry.shape), links);\n return { ok: true, summary: detection.login ? `Login form detected with ${detection.markers.join(\", \")}.` : \"No login form was detected.\", details: { login: detection.login, markers: detection.markers } };\n }\n case \"detecttemplate\": {\n const collected = collectfields(root, formscope);\n const text = clean(root.body?.innerText || \"\");\n const detection = detecttemplate(collected.map(entry => entry.shape), text);\n return { ok: true, summary: detection.template === \"unknown\" ? \"No signup or checkout template was detected.\" : `${detection.template} template detected with markers ${detection.markers.join(\", \")}.`, details: { template: detection.template, markers: detection.markers } };\n }\n case \"handoffcaptcha\": {\n const matched = captchamarkers.filter(marker => root.querySelector(marker) !== null);\n return { ok: true, summary: captchadetected(matched) ? `Captcha presence detected (${matched.join(\", \")}); control hands back to the user.` : \"No captcha was detected.\", details: { captcha: captchadetected(matched), markers: matched } };\n }\n case \"asksubmit\": {\n const collected = collectfields(root, step.value || undefined);\n const values = collected.map(entry => ({ label: entry.shape.label || entry.shape.name || entry.shape.selector, value: entry.element instanceof HTMLSelectElement ? entry.element.value : (entry.element as HTMLInputElement).value }));\n return { ok: true, summary: `Read ${values.length} field value${values.length === 1 ? \"\" : \"s\"} for the submission review.`, details: { values } };\n }\n case \"submitform\": {\n const form = target instanceof HTMLFormElement ? target : target instanceof HTMLElement ? target.closest(\"form\") : null;\n if (!form) return { ok: false, summary: \"No owning form was found for the reviewed submission.\" };\n form.requestSubmit();\n return { ok: true, summary: \"Form submitted programmatically through its owning form.\" };\n }\n case \"consentpassword\": {\n if (!(target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement)) return { ok: false, summary: \"The reviewed password target cannot receive text.\" };\n const entry: formentry = { match: { mode: \"name\", name: target.name || target.getAttribute(\"id\") || \"\" }, kind: \"password\", value: step.value ?? \"\" };\n if (!fillcontrol(target, entry)) return { ok: false, summary: \"The password field refused the native setter fill.\" };\n return { ok: true, summary: \"Password field filled after the reviewed consent; the value never appears in the audit trail.\" };\n }\n case \"attachfile\": {\n if (!(target instanceof HTMLInputElement) || target.type !== \"file\") return { ok: false, summary: \"The reviewed target is not a file input.\" };\n const artifactname = typeof options.artifactname === \"string\" && options.artifactname ? options.artifactname : \"artifact\";\n try {\n const file = new File([new Blob([\"devthink artifact\"], { type: \"application/octet-stream\" })], artifactname);\n const transfer = new DataTransfer();\n transfer.items.add(file);\n target.files = transfer.files;\n events(target);\n return { ok: true, summary: `Artifact ${artifactname} attached to the reviewed file input.`, details: { artifact: options.artifact, artifactname } };\n } catch {\n return { ok: false, summary: \"The reviewed file input refused the artifact attachment.\" };\n }\n }\n default: return { ok: false, summary: \"Unsupported forms and data action.\" };\n }\n}\n", "import type { toolstep, wizardstate } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { cardmask } from \"./pageforms.js\";\nimport { clean } from \"./pageresolve.js\";\n\n/**\n * Wizard, dependent control and payment field logics for reviewed steps.\n * Every correlated rule for wizard tracking, dependent option waits, typeahead picks, calendar navigation, card segment typing, one time code sources and submission retry backoff lives in this file.\n */\n\n/** Advances one wizard step, recording the completion signal of the executed step and the new step index. */\nexport function wizardadvance(state: wizardstate, completed: boolean, at: number): wizardstate {\n const flags = [...state.completed];\n while (flags.length < state.index + 1) flags.push(false);\n flags[state.index] = completed;\n return { ...state, index: Math.min(state.index + 1, state.steps), completed: flags, at };\n}\n\n/** True when the dependent child options finished loading after a parent selection changed their count. */\nexport function dependentloaded(previouscount: number, currentcount: number): boolean {\n return currentcount !== previouscount;\n}\n\n/** Picks the reviewed suggestion entry from a typeahead list, case insensitive; undefined when the entry is absent. */\nexport function typeaheadpick(suggestions: string[], pick: string): string | undefined {\n const needle = pick.trim().toLowerCase();\n return suggestions.find(suggestion => suggestion.trim().toLowerCase() === needle);\n}\n\n/** Parses one reviewed yyyy-mm-dd date into its year, month and day parts; null when the form is invalid. */\nexport function parsedateparts(value: string): { year: number; month: number; day: number } | null {\n if (!/^\\d{4}-\\d{2}-\\d{2}$/.test(value)) return null;\n const parts = value.split(\"-\").map(part => Number.parseInt(part, 10));\n const year = parts[0] ?? 0;\n const month = parts[1] ?? 0;\n const day = parts[2] ?? 0;\n if (month < 1 || month > 12 || day < 1 || day > 31) return null;\n return { year, month, day };\n}\n\n/** Plans the calendar navigation to the reviewed date: the month delta and the day cell to click. */\nexport function calendarplan(view: { year: number; month: number }, date: { year: number; month: number; day: number }): { months: number; day: number } {\n return { months: (date.year - view.year) * 12 + (date.month - view.month), day: date.day };\n}\n\n/** Splits one card number into its typed groups so the filler pauses between groups. */\nexport function cardgroups(number: string): string[] {\n const groups = number.replace(/[-\\s]+/g, \" \").trim().split(\" \");\n return groups.filter(group => group.length > 0);\n}\n\n/** True when the reviewed one time code source is ready to deliver the code before typing. */\nexport function codeready(source: string, value: string | undefined): boolean {\n if (source === \"reviewed\") return typeof value === \"string\" && value.trim().length > 0;\n return false;\n}\n\n/** One reviewed retry backoff rule with an attempt count, a wait window and a growth factor, all user configured. */\nexport interface backoffrule {\n attempts: number;\n wait: number;\n factor: number;\n}\n\n/** Parses the reviewed backoff rule of a retryform step; null when the step reviews none. */\nexport function parsebackoff(step: toolstep): backoffrule | null {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const backoff = options.backoff;\n if (!backoff || typeof backoff !== \"object\" || Array.isArray(backoff)) return null;\n const rule = backoff as Record<string, unknown>;\n const wait = rule.wait;\n const factor = rule.factor;\n if (typeof wait !== \"number\" || !Number.isFinite(wait) || wait <= 0) return null;\n if (typeof factor !== \"number\" || !Number.isFinite(factor) || factor < 1) return null;\n const attempts = typeof options.attempts === \"number\" && Number.isInteger(options.attempts) && options.attempts >= 1 ? options.attempts : 2;\n return { attempts, wait, factor };\n}\n\n/** Computes the reviewed backoff windows between submission retries; no code ceiling applies. */\nexport function backoffwaits(attempts: number, wait: number, factor: number): number[] {\n const windows: number[] = [];\n let current = wait;\n for (let index = 1; index < attempts; index += 1) {\n windows.push(current);\n current *= factor;\n }\n return windows;\n}\n\nfunction wait(delay: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, delay));\n}\n\nfunction pollfor(predicate: () => boolean, description: string, timeout: number): Promise<stepresult> {\n return new Promise(resolve => {\n const started = Date.now();\n const check = (): void => {\n if (predicate()) { resolve({ ok: true, summary: `${description} is now present on the page.` }); return; }\n if (timeout > 0 && Date.now() - started >= timeout) { resolve({ ok: false, summary: `${description} did not appear within ${timeout} milliseconds.` }); return; }\n window.setTimeout(check, 100);\n };\n check();\n });\n}\n\n/** Runs one reviewed wizard or payment field step inside the page, from multi step wizards to card segments and one time codes. */\nexport function runpagewizard(step: toolstep, target: Element | null, root: Document = document): stepresult | Promise<stepresult> {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n switch (step.kind) {\n case \"runwizard\": {\n const scope = target instanceof HTMLElement ? target : root.body ?? root.documentElement;\n const steps = typeof options.steps === \"number\" && Number.isInteger(options.steps) && options.steps > 0 ? options.steps : 1;\n const state: wizardstate = { index: 0, steps, completed: [], at: Date.now() };\n const next = scope.querySelector<HTMLElement>(\"button[type=submit], button[name=next], [data-next]\");\n if (!next) return { ok: false, summary: \"No next step control was found inside the wizard scope.\", details: { wizard: state } };\n next.click();\n const advanced = wizardadvance(state, true, Date.now());\n return { ok: advanced.index >= steps, summary: `Wizard advanced to step ${advanced.index + 1} of ${steps}${advanced.index >= steps ? \" and completed\" : \"\"}.`, details: { wizard: advanced } };\n }\n case \"selectchain\": {\n if (!(target instanceof HTMLSelectElement)) return { ok: false, summary: \"The reviewed parent target is not a select element.\" };\n const childselector = typeof options.child === \"string\" ? options.child : \"\";\n const child = root.querySelector<HTMLSelectElement>(childselector);\n if (!child) return { ok: false, summary: \"The reviewed dependent child control was not found.\" };\n const previouscount = child.options.length;\n const option = [...target.options].find(candidate => candidate.value === step.value || candidate.textContent?.trim() === step.value);\n if (!option) return { ok: false, summary: \"The reviewed parent option is not part of the select element.\" };\n target.value = option.value;\n target.dispatchEvent(new Event(\"input\", { bubbles: true }));\n target.dispatchEvent(new Event(\"change\", { bubbles: true }));\n const waitwindow = typeof options.wait === \"number\" && options.wait > 0 ? options.wait : 0;\n return pollfor(() => dependentloaded(previouscount, child.options.length), \"Dependent options of the child control\", waitwindow);\n }\n case \"picktypeahead\": {\n const field = target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement ? target : null;\n if (!field) return { ok: false, summary: \"The reviewed typeahead target cannot receive text.\" };\n const pick = typeof options.pick === \"string\" ? options.pick : \"\";\n const timeout = typeof options.timeout === \"number\" && options.timeout > 0 ? options.timeout : 0;\n field.focus();\n field.value = step.value ?? \"\";\n field.dispatchEvent(new Event(\"input\", { bubbles: true }));\n const list = root.querySelector<HTMLElement>('[role=listbox], .suggestions, ul.autocomplete, [data-typeahead]');\n const suggestions = list ? [...list.querySelectorAll<HTMLElement>(\"[role=option], li, .suggestion\")].map(entry => clean(entry.textContent || \"\")) : [];\n const chosen = typeaheadpick(suggestions, pick);\n if (chosen === undefined) return { ok: false, summary: `The reviewed suggestion \"${pick}\" is not part of the typeahead list.`, details: { suggestions } };\n const entry = [...(list?.querySelectorAll<HTMLElement>(\"[role=option], li, .suggestion\") ?? [])].find(item => clean(item.textContent || \"\").trim().toLowerCase() === chosen.trim().toLowerCase());\n entry?.click();\n field.dispatchEvent(new Event(\"change\", { bubbles: true }));\n return { ok: Boolean(entry), summary: `Picked the reviewed typeahead entry \"${chosen}\".`, details: { pick: chosen, query: step.value ?? \"\" } };\n }\n case \"pickdate\": {\n const calendar = target instanceof HTMLElement ? target : root.body ?? root.documentElement;\n const date = parsedateparts(step.value ?? \"\");\n if (!date) return { ok: false, summary: \"The reviewed date must use the yyyy-mm-dd form.\" };\n const header = clean(calendar.querySelector<HTMLElement>(\"[data-calendar-title], .calendar-title, header\")?.textContent || \"\");\n const match = /(\\d{4})/.exec(header);\n const monthnames = [\"january\", \"february\", \"march\", \"april\", \"may\", \"june\", \"july\", \"august\", \"september\", \"october\", \"november\", \"december\"];\n const viewyear = match ? Number.parseInt(match[1] ?? \"0\", 10) : new Date().getFullYear();\n const viewmonth = monthnames.findIndex(name => header.toLowerCase().includes(name)) >= 0 ? monthnames.findIndex(name => header.toLowerCase().includes(name)) : new Date().getMonth() + 1;\n const plan = calendarplan({ year: viewyear, month: viewmonth }, date);\n const forward = plan.months >= 0;\n for (let index = 0; index < Math.abs(plan.months); index += 1) {\n calendar.querySelector<HTMLElement>(forward ? \"[data-next-month], .next-month, [aria-label=next]\" : \"[data-prev-month], .prev-month, [aria-label=previous]\")?.click();\n }\n const day = [...calendar.querySelectorAll<HTMLElement>(\"[role=gridcell], [data-day], td\")].find(cell => Number.parseInt(clean(cell.textContent || \"\"), 10) === plan.day);\n if (!day) return { ok: false, summary: `The reviewed day cell ${plan.day} was not found in the calendar widget.` };\n day.click();\n return { ok: true, summary: `Picked ${step.value} from the calendar widget after ${Math.abs(plan.months)} month navigation${Math.abs(plan.months) === 1 ? \"\" : \"s\"}.`, details: { months: plan.months, day: plan.day } };\n }\n case \"fillcard\": {\n const segments = Array.isArray(options.segments) ? (options.segments as Array<Record<string, unknown>>).filter(item => item && typeof item === \"object\") : [];\n if (segments.length === 0) return { ok: false, summary: \"A reviewed non-empty list of card segments is required in options.\" };\n const pause = typeof options.pause === \"number\" && options.pause > 0 ? options.pause : 0;\n const filled: Array<{ label: string; masked: string }> = [];\n const failures: string[] = [];\n const fillsegment = async (segment: Record<string, unknown>): Promise<void> => {\n const match = segment.match as Record<string, unknown> | undefined;\n const value = typeof segment.value === \"string\" ? segment.value : \"\";\n if (!match || typeof match !== \"object\") { failures.push(\"segment without a reviewed match\"); return; }\n const scope = root.body ?? root.documentElement;\n const candidates = [...scope.querySelectorAll<HTMLInputElement>(\"input, select\")];\n const key = match.mode === \"label\" ? \"label\" : match.mode === \"placeholder\" ? \"placeholder\" : match.mode === \"arialabel\" ? \"arialabel\" : \"name\";\n const needle = String(match[key] ?? \"\").toLowerCase();\n const element = candidates.find(input => (key === \"label\" ? input.name.toLowerCase() || input.getAttribute(\"aria-label\")?.toLowerCase() || \"\" : (input.getAttribute(key) ?? input.name).toLowerCase()).includes(needle) || (input.getAttribute(\"aria-label\") ?? \"\").toLowerCase().includes(needle));\n if (!element) { failures.push(`unmatched: ${needle}`); return; }\n element.focus();\n for (const group of cardgroups(value)) {\n element.value = group;\n element.dispatchEvent(new Event(\"input\", { bubbles: true }));\n if (pause > 0) await wait(pause);\n }\n element.dispatchEvent(new Event(\"change\", { bubbles: true }));\n filled.push({ label: String(match[key] ?? \"\"), masked: cardmask(value) });\n };\n return (async (): Promise<stepresult> => {\n for (const segment of segments) await fillsegment(segment);\n return {\n ok: failures.length === 0,\n summary: failures.length === 0 ? `Filled ${filled.length} card segment${filled.length === 1 ? \"\" : \"s\"} with pauses between card number groups.` : `Filled ${filled.length} of ${segments.length} card segments; ${failures.join(\"; \")}.`,\n details: { segments: filled, failures, pause },\n };\n })();\n }\n case \"fillcode\": {\n const field = target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement ? target : null;\n if (!field) return { ok: false, summary: \"The reviewed one time code target cannot receive text.\" };\n const source = typeof options.source === \"string\" ? options.source : \"\";\n const timeout = typeof options.timeout === \"number\" && options.timeout > 0 ? options.timeout : 0;\n const code = step.value ?? \"\";\n return pollfor(() => codeready(source, code), \"The reviewed one time code source\", timeout).then(result => {\n if (!codeready(source, code)) return { ok: false, summary: `The reviewed code source ${source} is not ready to deliver the code yet.` };\n field.focus();\n field.value = code;\n field.dispatchEvent(new Event(\"input\", { bubbles: true }));\n field.dispatchEvent(new Event(\"change\", { bubbles: true }));\n return { ok: true, summary: `One time code typed from the reviewed source ${source}.`, details: { source } };\n });\n }\n default: return { ok: false, summary: \"Unsupported wizard action.\" };\n }\n}\n", "import type { columnspec, datasetrow, sourceref, toolstep, transformrule } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { clean, elementselector as selector } from \"./pageresolve.js\";\n\n/**\n * Table and dataset logics for reviewed steps.\n * Every correlated rule for header normalization, span expansion, nested table walking, pagination following, row hashing, transform expressions, dataset merging, row sampling and csv, json and excel serialization lives in this file.\n */\n\n/** One raw table cell with its text, header flag, span geometry and an optional nested table. */\nexport interface cellshape {\n text: string;\n header: boolean;\n rowspan: number;\n colspan: number;\n nested?: { selector: string; rows: rowshape[] };\n}\n\n/** One raw table row of span carrying cells. */\nexport interface rowshape {\n cells: cellshape[];\n header: boolean;\n}\n\n/** One nested child table linked to the body row it belongs to. */\nexport interface childtable {\n parentrow: number;\n selector: string;\n columns: columnspec[];\n rows: datasetrow[];\n}\n\n/** Result of the table reader: normalized column specs, keyed body rows and child datasets. */\nexport interface gridresult {\n columns: columnspec[];\n rows: datasetrow[];\n children: childtable[];\n}\n\n/** Trims, lowercases and slugifies one header cell into a stable column key. */\nexport function normalizeheader(label: string): string {\n const slug = label.trim().toLowerCase().replace(/[^\\p{L}\\p{N}]+/gu, \"-\").replace(/^-+|-+$/g, \"\");\n return slug || \"column\";\n}\n\n/** Builds one column spec from a header label with a unique stable key and the normalized name. */\nexport function columnspecof(label: string, used: Set<string> = new Set()): columnspec {\n const base = normalizeheader(label);\n let key = base;\n let suffix = 2;\n while (used.has(key)) {\n key = `${base}${suffix}`;\n suffix += 1;\n }\n used.add(key);\n return { key, label: label.trim(), kind: \"text\", normalized: label.trim().toLowerCase() };\n}\n\n/** Classifies a column as number only when every non-empty value parses as a number. */\nexport function classifycolumn(values: string[]): \"text\" | \"number\" {\n const present = values.filter(value => value.trim().length > 0);\n if (present.length === 0) return \"text\";\n return present.every(value => Number.isFinite(Number(value.replace(/,/g, \".\")))) ? \"number\" : \"text\";\n}\n\n/** Expands rowspan and colspan cells into a filled rectangular grid of values. */\nexport function expandspans(rows: rowshape[]): string[][] {\n const filled: Array<Array<string | undefined>> = [];\n const pending = new Map<string, string>();\n for (let index = 0; index < rows.length; index += 1) {\n const row: Array<string | undefined> = filled[index] ?? (filled[index] = []);\n let column = 0;\n for (const cell of rows[index]!.cells) {\n while (row[column] !== undefined || pending.has(`${index},${column}`)) {\n if (row[column] === undefined) row[column] = pending.get(`${index},${column}`);\n column += 1;\n }\n row[column] = cell.text;\n for (let spanrow = 0; spanrow < Math.max(1, cell.rowspan); spanrow += 1) {\n for (let spancol = 0; spancol < Math.max(1, cell.colspan); spancol += 1) {\n if (spanrow === 0 && spancol === 0) continue;\n pending.set(`${index + spanrow},${column + spancol}`, cell.text);\n }\n }\n column += Math.max(1, cell.colspan);\n }\n }\n for (const [key, value] of pending) {\n const [rowpart, columnpart] = key.split(\",\");\n const rowindex = Number.parseInt(rowpart ?? \"0\", 10);\n const columnindex = Number.parseInt(columnpart ?? \"0\", 10);\n const target = filled[rowindex] ?? (filled[rowindex] = []);\n if (target[columnindex] === undefined) target[columnindex] = value;\n }\n const width = filled.reduce((largest, row) => Math.max(largest, row.length), 0);\n return filled.map(row => Array.from({ length: width }, (_, column) => row[column] ?? \"\"));\n}\n\n/** Reads header and body rows into normalized column specs and keyed body rows, extracting nested tables into child datasets. */\nexport function readgrid(rows: rowshape[]): gridresult {\n const headerindex = rows.findIndex(row => row.header || (row.cells[0]?.header ?? false));\n const useheader = headerindex !== -1 ? headerindex : 0;\n const hasheader = headerindex !== -1 || rows.length > 0;\n const grid = expandspans(rows);\n const headercells = hasheader ? (grid[useheader] ?? []) : [];\n const used = new Set<string>();\n const columns = Array.from({ length: headercells.length || (grid[0]?.length ?? 0) }, (_, index) => columnspecof(headercells[index] ?? `column${index + 1}`, used));\n const bodyrows = grid.filter((_, index) => hasheader ? index !== useheader : true)\n .filter(line => line.some(value => value.trim().length > 0))\n .map(line => {\n const row: datasetrow = {};\n columns.forEach((column, index) => { row[column.key] = line[index] ?? \"\"; });\n return row;\n });\n for (const column of columns) column.kind = classifycolumn(bodyrows.map(row => row[column.key] ?? \"\"));\n const children: childtable[] = [];\n rows.forEach((row, rowindex) => {\n if (hasheader && rowindex === useheader) return;\n const parentrow = hasheader && rowindex > useheader ? rowindex - 1 : rowindex;\n row.cells.forEach(cell => {\n if (!cell.nested) return;\n const child = readgrid(cell.nested.rows);\n children.push({ parentrow, selector: cell.nested!.selector, columns: child.columns, rows: child.rows });\n });\n });\n return { columns, rows: bodyrows, children };\n}\n\n/** Resolves the next pagination control from numbered page entries: the entry after the current one or a next word control. */\nexport function nextcontrol(entries: Array<{ text: string; selector: string; current: boolean }>): string | undefined {\n const current = entries.findIndex(entry => entry.current);\n if (current !== -1 && current + 1 < entries.length) return entries[current + 1]?.selector;\n const nextwords = [\"next\", \"next page\", \">\", \">>\", \"\u203A\", \"\u00BB\", \"pr\u00F3xima\", \"seguinte\"];\n return entries.find(entry => nextwords.includes(entry.text.trim().toLowerCase()))?.selector;\n}\n\n/** True when the current row set contains at least one row the previous set did not carry. */\nexport function rowsfresh(previous: datasetrow[], current: datasetrow[]): boolean {\n if (current.length === 0) return false;\n const known = new Set(previous.map(row => JSON.stringify(row)));\n return current.some(row => !known.has(JSON.stringify(row)));\n}\n\n/** Computes a stable row hash over the reviewed keys; an empty key list hashes every column. */\nexport function rowhash(row: datasetrow, keys: string[]): string {\n const source = (keys.length > 0 ? keys : Object.keys(row).sort()).map(key => `${key}=${row[key] ?? \"\"}`).join(\"|\");\n let hash = 5381;\n for (let index = 0; index < source.length; index += 1) hash = ((hash * 33) ^ source.charCodeAt(index)) >>> 0;\n return hash.toString(16);\n}\n\n/** Deduplicates rows by reviewed keys, keeping the first occurrence of every key set. */\nexport function dedupebykeys(rows: datasetrow[], keys: string[]): { kept: datasetrow[]; removed: number } {\n const seen = new Set<string>();\n const kept: datasetrow[] = [];\n for (const row of rows) {\n const hash = rowhash(row, keys);\n if (seen.has(hash)) continue;\n seen.add(hash);\n kept.push(row);\n }\n return { kept, removed: rows.length - kept.length };\n}\n\n/** Applies one reviewed transform expression to a value; unsupported expressions are refused. */\nexport function applyexpression(value: string, expression: string): string {\n const split = expression.indexOf(\":\");\n const op = split === -1 ? expression : expression.slice(0, split);\n const argument = split === -1 ? undefined : expression.slice(split + 1);\n if (op === \"trim\") return value.trim();\n if (op === \"upper\") return value.toUpperCase();\n if (op === \"lower\") return value.toLowerCase();\n if (op === \"number\") return value.replace(/[^\\d.\\-]/g, \"\");\n if (op === \"prefix\") return `${argument ?? \"\"}${value}`;\n if (op === \"suffix\") return `${value}${argument ?? \"\"}`;\n if (op === \"replace\") {\n const separator = argument?.indexOf(\"=>\") ?? -1;\n if (separator === -1 || separator === 0) throw new Error(`The reviewed transform expression ${expression} needs the from=>to separator.`);\n const from = argument!.slice(0, separator);\n const to = argument!.slice(separator + 2);\n return value.split(from).join(to);\n }\n throw new Error(`The reviewed transform expression ${op} is not supported.`);\n}\n\n/** Applies reviewed transform rules to dataset rows, surfacing per rule errors and keeping the original values on failure. */\nexport function transformrows(rows: datasetrow[], rules: transformrule[]): { rows: datasetrow[]; errors: string[] } {\n const errors: string[] = [];\n const output = rows.map(row => ({ ...row }));\n for (const rule of rules) {\n const updated: datasetrow[] = [];\n try {\n for (const row of output) updated.push({ ...row, [rule.target]: applyexpression(rule.sources.map(source => row[source] ?? \"\").join(\" \"), rule.expression) });\n } catch (error) {\n errors.push(`${rule.target}: ${error instanceof Error ? error.message : String(error)}`);\n continue;\n }\n output.splice(0, output.length, ...updated);\n }\n return { rows: output, errors };\n}\n\n/** Merges datasets across pages: columns align by key with gaps filled empty and rows concatenate in page order. */\nexport function mergedatasets(datasets: Array<{ columns: columnspec[]; rows: datasetrow[] }>): { columns: columnspec[]; rows: datasetrow[] } {\n const columns: columnspec[] = [];\n const seen = new Set<string>();\n for (const dataset of datasets) {\n for (const column of dataset.columns) {\n if (seen.has(column.key)) continue;\n seen.add(column.key);\n columns.push(column);\n }\n }\n const rows = datasets.flatMap(dataset => dataset.rows.map(row => {\n const merged: datasetrow = {};\n for (const column of columns) merged[column.key] = row[column.key] ?? \"\";\n return merged;\n }));\n return { columns, rows };\n}\n\n/** Attaches the source url, timestamp and step ref to every row and returns the matching source refs. */\nexport function samplerows(rows: datasetrow[], url: string, stepid: string, at: number): { rows: datasetrow[]; sources: sourceref[] } {\n const stamped = rows.map(row => ({ ...row, source: url, capturedat: String(at), step: stepid }));\n const sources = stamped.map((row, index) => ({ row: index, url, at, stepid }));\n return { rows: stamped, sources };\n}\n\n/** Escapes one csv field, wrapping values that carry the delimiter, quotes or line breaks. */\nfunction csvfield(value: string, delimiter: string): string {\n return value.includes(delimiter) || value.includes(\"\\\"\") || value.includes(\"\\n\") ? `\"${value.replace(/\"/g, \"\\\"\\\"\")}\"` : value;\n}\n\n/** Serializes columns and rows into csv text. */\nexport function tocsv(columns: columnspec[], rows: datasetrow[], delimiter = \",\"): string {\n const lines = [columns.map(column => csvfield(column.label || column.key, delimiter)).join(delimiter)];\n for (const row of rows) lines.push(columns.map(column => csvfield(row[column.key] ?? \"\", delimiter)).join(delimiter));\n return lines.join(\"\\n\");\n}\n\n/** Parses csv text into headers and raw rows, honoring quoted fields and escaped quotes. */\nexport function parsecsv(text: string, delimiter = \",\"): { headers: string[]; rows: string[][] } {\n const records: string[][] = [];\n let field = \"\";\n let record: string[] = [];\n let quoted = false;\n for (let index = 0; index < text.length; index += 1) {\n const character = text[index]!;\n if (quoted) {\n if (character === \"\\\"\") {\n if (text[index + 1] === \"\\\"\") { field += \"\\\"\"; index += 1; }\n else quoted = false;\n } else field += character;\n continue;\n }\n if (character === \"\\\"\") { quoted = true; continue; }\n if (character === delimiter) { record.push(field); field = \"\"; continue; }\n if (character === \"\\n\" || character === \"\\r\") {\n if (character === \"\\r\" && text[index + 1] === \"\\n\") index += 1;\n record.push(field);\n field = \"\";\n if (record.some(value => value.length > 0) || record.length > 1) records.push(record);\n record = [];\n continue;\n }\n field += character;\n }\n record.push(field);\n if (record.some(value => value.length > 0) || record.length > 1) records.push(record);\n const [headers = [], ...rows] = records;\n return { headers, rows };\n}\n\n/** Maps parsed csv headers onto dataset column specs through a reviewed mapping of csv names to target keys. */\nexport function mapcolumns(headers: string[], mapping: Record<string, string> = {}): columnspec[] {\n const used = new Set<string>();\n return headers.map(header => {\n const target = mapping[header] ?? mapping[normalizeheader(header)] ?? header;\n return columnspecof(target, used);\n });\n}\n\n/** Serializes columns and rows into a json dataset payload. */\nexport function tojson(columns: columnspec[], rows: datasetrow[]): string {\n return JSON.stringify({ columns, rows });\n}\n\n/** Escapes one xml text node. */\nfunction xmltext(value: string): string {\n return value.replace(/&/g, \"&amp;\").replace(/</g, \"&lt;\").replace(/>/g, \"&gt;\").replace(/\"/g, \"&quot;\");\n}\n\n/** Serializes columns and rows into an Excel SpreadsheetML 2003 workbook that Excel opens natively. */\nexport function toexcel(columns: columnspec[], rows: datasetrow[], name: string): string {\n const head = columns.map(column => `<Cell ss:StyleID=\"head\"><Data ss:Type=\"String\">${xmltext(column.label || column.key)}</Data></Cell>`).join(\"\");\n const body = rows.map(row => `<Row>${columns.map(column => {\n const value = row[column.key] ?? \"\";\n const numeric = column.kind === \"number\" && value.trim() !== \"\" && Number.isFinite(Number(value));\n return numeric ? `<Cell><Data ss:Type=\"Number\">${xmltext(value)}</Data></Cell>` : `<Cell><Data ss:Type=\"String\">${xmltext(value)}</Data></Cell>`;\n }).join(\"\")}</Row>`).join(\"\");\n return `<?xml version=\"1.0\"?><?mso-application progid=\"Excel.Sheet\"?><Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"><Styles><Style ss:ID=\"head\"><Font ss:Bold=\"1\"/></Style></Styles><Worksheet ss:Name=\"${xmltext(name || \"dataset\").slice(0, 31)}\"><Table><Row>${head}</Row>${body}</Table></Worksheet></Workbook>`;\n}\n\n/** Collects the raw span carrying row shapes of one html table, separating nested tables into cell refs. */\nfunction collectrowshapes(table: HTMLTableElement): rowshape[] {\n return [...table.rows].map(row => ({\n header: [...row.cells].every(cell => cell.tagName === \"TH\") && row.cells.length > 0,\n cells: [...row.cells].map(cell => {\n const nested = cell.querySelector(\"table\");\n return {\n text: clean(nested ? `${nested.rows.length} rows` : cell.textContent ?? \"\"),\n header: cell.tagName === \"TH\",\n rowspan: cell.rowSpan,\n colspan: cell.colSpan,\n ...(nested instanceof HTMLTableElement ? { nested: { selector: selector(nested), rows: collectrowshapes(nested) } } : {}),\n };\n }),\n }));\n}\n\n/** Reads the reviewed table element into a grid result. */\nfunction readtable(target: Element | null, root: Document, fallbackselector: string | undefined): gridresult | null {\n const table = target instanceof HTMLTableElement ? target : (fallbackselector ? root.querySelector<HTMLTableElement>(fallbackselector) : root.querySelector<HTMLTableElement>(\"table\"));\n if (!table) return null;\n return readgrid(collectrowshapes(table));\n}\n\n/** Reads the live pagination entries of a table container for the next control resolution. */\nfunction paginationentries(root: Document, scope: string | undefined): Array<{ text: string; selector: string; current: boolean }> {\n const container = scope ? root.querySelector(scope) : root;\n if (!container) return [];\n return [...container.querySelectorAll(\"a[href], button\")].map(element => ({\n text: clean(element.textContent ?? \"\"),\n selector: selector(element),\n current: element.getAttribute(\"aria-current\") === \"page\" || element.classList.contains(\"active\") || element.classList.contains(\"current\"),\n }));\n}\n\n/** Runs one reviewed data step on the page: table scraping into datasets and pagination following with fresh row waits. */\nexport async function runpagedata(step: toolstep, target: Element | null, root: Document = document): Promise<stepresult> {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n if (step.kind === \"scrapetable\") {\n const grid = readtable(target, root, step.target);\n if (!grid) return { ok: false, summary: \"The reviewed table selector matches no table element.\" };\n const rowlimit = typeof options.rowlimit === \"number\" && Number.isInteger(options.rowlimit) && options.rowlimit > 0 ? options.rowlimit : grid.rows.length;\n const limited = grid.rows.slice(0, rowlimit);\n return {\n ok: true,\n summary: `Scraped ${limited.length} row${limited.length === 1 ? \"\" : \"s\"} into ${grid.columns.length} normalized column${grid.columns.length === 1 ? \"\" : \"s\"}${grid.children.length > 0 ? ` with ${grid.children.length} nested child table${grid.children.length === 1 ? \"\" : \"s\"}` : \"\"}.`,\n details: { grid: { columns: grid.columns, rows: limited, children: grid.children }, rows: limited.length, columns: grid.columns.length },\n };\n }\n if (step.kind === \"paginateextract\") {\n const nextselector = typeof options.next === \"string\" ? options.next : \"\";\n const pages = typeof options.pages === \"number\" && Number.isInteger(options.pages) && options.pages > 0 ? options.pages : 1;\n const wait = typeof options.wait === \"number\" && Number.isFinite(options.wait) && options.wait > 0 ? options.wait : 0;\n const cursor = typeof options.cursor === \"number\" && Number.isInteger(options.cursor) && options.cursor > 0 ? options.cursor : 0;\n const grids: gridresult[] = [];\n let previous: datasetrow[] = [];\n for (let page = 0; page < pages + cursor; page += 1) {\n if (page < cursor) {\n const control = root.querySelector<HTMLElement>(nextselector);\n if (!control) break;\n control.click();\n await new Promise(resolve => window.setTimeout(resolve, 0));\n continue;\n }\n if (page > cursor) {\n const control = root.querySelector<HTMLElement>(nextselector);\n if (!control) break;\n control.click();\n const deadline = Date.now() + wait;\n let fresh = false;\n while (!fresh && Date.now() < deadline) {\n await new Promise(resolve => window.setTimeout(resolve, Math.min(100, Math.max(16, deadline - Date.now()))));\n const probe = readtable(target, root, step.target);\n fresh = probe !== null && rowsfresh(previous, probe.rows);\n }\n }\n const grid = readtable(target, root, step.target);\n if (!grid) return { ok: false, summary: \"The reviewed table selector matches no table element.\" };\n grids.push(grid);\n previous = grid.rows;\n }\n const merged = mergedatasets(grids);\n const hasnext = Boolean(root.querySelector(nextselector));\n return {\n ok: grids.length > 0,\n summary: grids.length > 0 ? `Followed ${grids.length} page${grids.length === 1 ? \"\" : \"s\"} of the reviewed table into ${merged.rows.length} row${merged.rows.length === 1 ? \"\" : \"s\"}${hasnext ? \"; a next control remains\" : \"\"}.` : \"No page of the reviewed table was extracted.\",\n details: { grid: { columns: merged.columns, rows: merged.rows, children: grids.flatMap(grid => grid.children) }, pages: grids.length, rows: merged.rows.length, next: hasnext },\n };\n }\n return { ok: false, summary: \"Unsupported data step.\" };\n}\n\n/** Exposes the pagination entries of the current document so plan review can suggest next controls. */\nexport function pagepagination(root: Document = document): Array<{ text: string; selector: string; current: boolean }> {\n return paginationentries(root, undefined);\n}\n", "import type { artifactrecord, columnspec, dataset, datasetrow, exportedartifact, extractsession, provenancerecord, streamstate, toolstep, transformrule } from \"../types.js\";\nimport { tocsv, toexcel, tojson } from \"./pagedata.js\";\n\n/**\n * Dataset command logics for the background executors.\n * Every correlated rule for dataset records, artifact exports with checksums, chunked streaming with backpressure, extraction cursors, loop row variables, provenance records and artifact retention lives in this file.\n */\n\n/** Builds one dataset record from a scraped grid result. */\nexport function builddataset(id: string, name: string, grid: { columns: columnspec[]; rows: datasetrow[]; children?: Array<{ parentrow: number; selector: string; columns: columnspec[]; rows: datasetrow[] }> }, at: number): dataset {\n return { id, name: name || id, columns: grid.columns, rows: grid.rows, sources: [], at };\n}\n\n/** Computes the deterministic checksum of an exported artifact's content. */\nexport function checksum(value: string): string {\n let hash = 5381;\n for (let index = 0; index < value.length; index += 1) hash = ((hash * 33) ^ value.charCodeAt(index)) >>> 0;\n return `fnv1a-${hash.toString(16)}`;\n}\n\n/** Serializes one dataset into the reviewed export format. */\nexport function exportcontent(datasetvalue: dataset, format: \"csv\" | \"json\" | \"excel\", delimiter = \",\"): string {\n if (format === \"json\") return tojson(datasetvalue.columns, datasetvalue.rows);\n if (format === \"excel\") return toexcel(datasetvalue.columns, datasetvalue.rows, datasetvalue.name);\n return tocsv(datasetvalue.columns, datasetvalue.rows, delimiter);\n}\n\n/** Builds one exported artifact record with its content and checksum for the task artifact store. */\nexport function exportartifact(id: string, datasetvalue: dataset, format: \"csv\" | \"json\" | \"excel\", stepid: string, content: string, at: number): exportedartifact {\n const extension = format === \"excel\" ? \"xml\" : format;\n return { id, kind: format, name: `${datasetvalue.name || datasetvalue.id}.${extension}`, stepid, rowcount: datasetvalue.rows.length, content, checksum: checksum(content), at };\n}\n\n/** Converts one exported artifact into the artifact record shape the run store keeps. */\nexport function artifactrecordof(artifact: exportedartifact): artifactrecord {\n return { id: artifact.id, kind: artifact.kind, name: artifact.name, stepid: artifact.stepid, at: artifact.at };\n}\n\n/** Plans the chunk boundaries of a streaming export from a user configured chunk size with no code ceiling. */\nexport function chunkplan(rows: number, chunk: number): Array<{ index: number; from: number; to: number }> {\n const size = Math.max(1, Math.floor(chunk));\n const chunks: Array<{ index: number; from: number; to: number }> = [];\n for (let from = 0; from < rows || chunks.length === 0; from += size) {\n const to = Math.min(rows, from + size);\n chunks.push({ index: chunks.length, from, to });\n if (to >= rows) break;\n }\n return chunks;\n}\n\n/** True when the stream writer must wait for acknowledgements: pending writes reached the in-flight budget of one. */\nexport function backpressure(written: number, acknowledged: number): boolean {\n return written - acknowledged >= 1;\n}\n\n/** Advances one stream state by one acknowledged chunk of rows. */\nexport function advancestream(state: streamstate, chunk: { index: number; to: number }, at: number, done: boolean): streamstate {\n return { datasetid: state.datasetid, name: state.name, chunk: chunk.index + 1, chunks: state.chunks, written: chunk.to, ...(done ? { done: true } : {}), at };\n}\n\n/** Returns the first unwritten row index of a stream, starting a fresh stream at zero. */\nexport function streamfrom(state: streamstate | undefined, rows: number): number {\n if (!state || state.done) return 0;\n return Math.min(state.written, rows);\n}\n\n/** Builds the initial stream state of one dataset. */\nexport function newstream(datasetvalue: dataset, chunks: number, at: number): streamstate {\n return { datasetid: datasetvalue.id, name: datasetvalue.name, chunk: 0, chunks, written: 0, at };\n}\n\n/** Advances one extraction session by one extracted page with its row count. */\nexport function advancecursor(sessionvalue: extractsession, page: string, rows: number, at: number, done: boolean): extractsession {\n return {\n id: sessionvalue.id,\n datasetid: sessionvalue.datasetid,\n name: sessionvalue.name,\n target: sessionvalue.target,\n next: sessionvalue.next,\n planned: sessionvalue.planned,\n pages: [...sessionvalue.pages, page],\n rows: sessionvalue.rows + rows,\n cursor: sessionvalue.cursor + 1,\n ...(done || sessionvalue.cursor + 1 >= sessionvalue.planned ? { done: true } : {}),\n startedat: sessionvalue.startedat,\n updatedat: at,\n };\n}\n\n/** Builds the initial extraction session of one dataset extraction. */\nexport function newextractsession(id: string, datasetid: string, name: string, target: string, next: string, planned: number, at: number): extractsession {\n return { id, datasetid, name, target, next, planned, pages: [], rows: 0, cursor: 0, startedat: at, updatedat: at };\n}\n\n/** Returns the pages an interrupted extraction still owes after its stored cursor. */\nexport function remainingpages(sessionvalue: extractsession, planned: number): number {\n if (sessionvalue.done) return 0;\n return Math.max(0, Math.max(sessionvalue.planned, planned) - sessionvalue.cursor);\n}\n\n/** Builds one provenance record of an exported artifact with its source url, step ref, row range and checksum. */\nexport function provenancefor(artifact: { id: string; name: string; rowcount: number; checksum: string }, url: string, stepid: string, at: number): provenancerecord {\n return { artifact: artifact.id, name: artifact.name, url, stepid, rowstart: artifact.rowcount > 0 ? 1 : 0, rowend: artifact.rowcount, checksum: artifact.checksum, at };\n}\n\n/** Applies the user configured artifact retention to exported artifacts; an absent setting keeps everything. */\nexport function retainedexports<T>(records: T[], retention: number | undefined): T[] {\n return retention === undefined ? records : records.slice(0, retention);\n}\n\n/** Interpolates one text through the {{column}} tokens of a dataset row. */\nexport function interpolate(text: string, row: datasetrow): string {\n return text.replace(/\\{\\{([^}]+)\\}\\}/g, (_, key: string) => row[key.trim()] ?? \"\");\n}\n\n/** Substitutes the row variables of one looprows iteration into the target, value and options of the inner step. */\nexport function loopstep(step: toolstep, row: datasetrow): toolstep {\n return {\n ...step,\n ...(step.target !== undefined ? { target: interpolate(step.target, row) } : {}),\n ...(step.value !== undefined ? { value: interpolate(step.value, row) } : {}),\n ...(step.options !== undefined ? { options: interpolate(step.options, row) } : {}),\n };\n}\n\n/** Exposes one dataset row as the step variables of a looprows iteration. */\nexport function loopvariables(row: datasetrow): datasetrow {\n return { ...row };\n}\n\n/** Builds the grid preview of a dataset with its column order, total rows and sampled rows. */\nexport function gridpreview(datasetvalue: dataset, sample: number): { datasetid: string; columns: string[]; rows: number; sample: datasetrow[] } {\n return { datasetid: datasetvalue.id, columns: datasetvalue.columns.map(column => column.key), rows: datasetvalue.rows.length, sample: datasetvalue.rows.slice(0, Math.max(0, Math.floor(sample))) };\n}\n\n/** Sorts dataset rows by one column key in the reviewed direction with a stable fallback for equal values. */\nexport function sortrows(rows: datasetrow[], key: string, direction: \"asc\" | \"desc\"): datasetrow[] {\n const sign = direction === \"desc\" ? -1 : 1;\n return [...rows].sort((left, right) => {\n const a = left[key] ?? \"\";\n const b = right[key] ?? \"\";\n const numeric = Number(a);\n const numericb = Number(b);\n if (Number.isFinite(numeric) && Number.isFinite(numericb) && a.trim() !== \"\" && b.trim() !== \"\") return (numeric - numericb) * sign;\n return a.localeCompare(b) * sign;\n });\n}\n\n/** Builds the sheet push payload of one dataset for a reviewed sheet endpoint. */\nexport function sheetpayload(datasetvalue: dataset, sheet: string): { sheet: string; columns: string[]; rows: datasetrow[] } {\n return { sheet, columns: datasetvalue.columns.map(column => column.key), rows: datasetvalue.rows };\n}\n\n/** Merges reviewed transform rules and dedupe keys into the task rules record of one task. */\nexport function mergetaskrules(existing: { taskid: string; transforms: transformrule[]; dedupekeys: string[] } | undefined, taskid: string, transforms: transformrule[], dedupekeys: string[], at: number): { taskid: string; transforms: transformrule[]; dedupekeys: string[]; at: number } {\n return {\n taskid,\n transforms: transforms.length > 0 ? transforms : (existing?.transforms ?? []),\n dedupekeys: dedupekeys.length > 0 ? dedupekeys : (existing?.dedupekeys ?? []),\n at,\n };\n}\n", "import type { observation, regionrect, resolvedtarget, toolstep } from \"../types.js\";\nimport { runpageaction, type stepresult } from \"./pageactions.js\";\nimport { runpageread } from \"./pagereads.js\";\nimport { runpagecontrol } from \"./pagecontrols.js\";\nimport { runinteractstep } from \"./pageinteract.js\";\nimport { runpointerstep } from \"./pagepointer.js\";\nimport { runpagenav } from \"./pagenav.js\";\nimport { harvestdialoglog, type observeddialog } from \"./pagedialogs.js\";\nimport { builda11ytree, buildpagetree, buildreader, runpageobservation } from \"./pageobserve.js\";\nimport { collecttables, detectlistpatterns, collectsiblings, normalizetable, runpagedetection } from \"./pagedetect.js\";\nimport { runpagewatch } from \"./pagewatch.js\";\nimport { runcdpstep, rundebugwatch } from \"./pagedebug.js\";\nimport { runprofilestep } from \"./pageprofile.js\";\nimport { runemulationstep, revertemulationlayer } from \"./pageemulate.js\";\nimport { runpageform } from \"./pageforms.js\";\nimport { runpagewizard } from \"./pagewizards.js\";\nimport { runpagedata } from \"./pagedata.js\";\nimport { checksum } from \"./datacommand.js\";\nimport { clean, elementlabel as label, elementselector as selector, resolvestep } from \"./pageresolve.js\";\n\n/**\n * Page bridge for reviewed steps.\n * Correlated rules for the dispatch seam, semantic snapshots, target preview with resolution, dialog log reads and the original action set live in this file.\n */\n\nfunction stepoptions(step: toolstep): Record<string, unknown> {\n if (!step.options) return {};\n try {\n const parsed = JSON.parse(step.options);\n return parsed && typeof parsed === \"object\" && !Array.isArray(parsed) ? (parsed as Record<string, unknown>) : {};\n } catch { return {}; }\n}\n\nconst previewid = \"devthinktargetpreview\";\n\nfunction clearpreview(): void {\n document.getElementById(previewid)?.remove();\n}\n\n/** Shows an ephemeral outline only; it neither mutates page data nor dispatches page events. */\nexport function previewtarget(step: toolstep, expectedorigin: string): { ok: boolean; summary: string; resolvedtarget?: resolvedtarget; candidates?: string[] } {\n if (location.origin !== expectedorigin) return { ok: false, summary: \"Page origin changed before preview.\" };\n clearpreview();\n const resolution = resolvestep(step, document);\n if (resolution.status === \"ambiguous\") return { ok: false, summary: `The reviewed ${resolution.mode} reference matched ${resolution.candidates.length} elements: ${resolution.candidates.join(\"; \")}.`, candidates: resolution.candidates };\n if (resolution.status !== \"resolved\") return { ok: false, summary: \"Reviewed target is no longer available.\" };\n const target = resolution.element;\n const rect = target.getBoundingClientRect();\n if (rect.width <= 0 || rect.height <= 0) return { ok: false, summary: \"Reviewed target is not currently visible.\" };\n const overlay = document.createElement(\"div\");\n overlay.id = previewid;\n overlay.setAttribute(\"aria-hidden\", \"true\");\n Object.assign(overlay.style, { position: \"fixed\", left: `${Math.max(0, rect.left - 3)}px`, top: `${Math.max(0, rect.top - 3)}px`, width: `${rect.width + 6}px`, height: `${rect.height + 6}px`, border: \"3px solid #2f80ed\", borderRadius: \"6px\", boxShadow: \"0 0 0 3px rgba(47,128,237,.28)\", pointerEvents: \"none\", zIndex: \"2147483647\", boxSizing: \"border-box\" });\n document.documentElement.append(overlay);\n window.setTimeout(clearpreview, 5000);\n return { ok: true, summary: `Previewing ${label(target) || target.tagName.toLowerCase()} for five seconds.`, resolvedtarget: resolution.target };\n}\n\n/** Captures the complete semantic page context for a user-approved active tab, including the a11y, reader, listpattern and tableshape observation sections. */\nexport function capturesnapshot(): observation {\n const candidates = [...document.querySelectorAll(\"a[href], button, input, textarea, select, [role=button], [role=link], [role=combobox], [role=option], [role=checkbox], [role=radio], [role=switch], [role=tab], details, summary\")];\n const interactive = candidates.map(element => ({ selector: selector(element), role: element.getAttribute(\"role\") || element.tagName.toLowerCase(), label: label(element) })).filter(item => item.label || item.role);\n const forms = [...document.querySelectorAll(\"input, textarea, select\")].map(element => ({\n label: label(element),\n type: element.getAttribute(\"type\") || element.tagName.toLowerCase(),\n name: element.getAttribute(\"name\") || \"\",\n ...(element instanceof HTMLSelectElement ? { options: [...element.options].map(option => clean(option.textContent || option.value)) } : {}),\n }));\n const text = clean(document.body?.innerText || \"\");\n const tree = buildpagetree(document);\n const tables = collecttables(document).map(entry => {\n const shape = normalizetable(entry.rows, entry.caption);\n return { selector: entry.selector, headers: shape.headers, columns: shape.columns, rows: shape.rows, caption: shape.caption };\n });\n return {\n schemaversion: 3,\n url: location.href,\n title: clean(document.title),\n textpreview: text,\n textlength: document.body?.innerText.length ?? 0,\n forms,\n interactive,\n capturedat: Date.now(),\n mode: \"passive\",\n a11y: builda11ytree(tree),\n reader: buildreader(tree, clean(document.title)),\n listpattern: detectlistpatterns(collectsiblings(document)),\n tableshape: tables,\n };\n}\n\n/** Reads and clears the dialog log the main world handler recorded on the shared document. */\nexport function readdialogs(): observeddialog[] {\n return harvestdialoglog(document);\n}\n\n/** Extracts full, read-only structured content for a reviewed extraction step. */\nfunction extractcontent(targetselector: string | undefined, root: Document): stepresult {\n if (!targetselector) {\n const links = [...root.querySelectorAll(\"a[href]\")].map(element => {\n const href = element instanceof HTMLAnchorElement ? element.getAttribute(\"href\") ?? \"\" : \"\";\n return { text: element.textContent?.trim() ?? \"\", href };\n });\n return { ok: true, summary: `Extracted ${links.length} link entries.`, details: { links } };\n }\n const target = root.querySelector(targetselector);\n if (!target) return { ok: false, summary: \"Extraction target is no longer available.\" };\n const text = target.textContent ?? \"\";\n return { ok: true, summary: `Extracted ${text.length} characters of content.`, details: { text } };\n}\n\n/** Scrolls one reviewed target into view without reading or changing other page state. */\nfunction scrolltarget(target: HTMLElement): stepresult {\n target.scrollIntoView({ block: \"center\", inline: \"nearest\", behavior: \"auto\" });\n return { ok: true, summary: `Scrolled ${label(target) || target.tagName.toLowerCase()} into view.` };\n}\n\n/** Dispatches hover events on one reviewed target. */\nfunction hovertarget(target: HTMLElement): stepresult {\n for (const type of [\"pointerover\", \"mouseover\", \"pointerenter\"] as const) {\n target.dispatchEvent(new PointerEvent(type, { bubbles: type !== \"pointerenter\", cancelable: true, composed: true }));\n }\n target.dispatchEvent(new MouseEvent(\"mouseenter\", { bubbles: false, cancelable: true }));\n return { ok: true, summary: `Hover events delivered to ${label(target) || target.tagName.toLowerCase()}.` };\n}\n\n/** Selects one reviewed existing option; values outside the declared options are refused. */\nfunction selectoption(target: HTMLElement, value: string): stepresult {\n if (!(target instanceof HTMLSelectElement)) return { ok: false, summary: \"Target is not a select element.\" };\n const option = [...target.options].find(candidate => candidate.value === value || candidate.textContent?.trim() === value);\n if (!option) return { ok: false, summary: \"Reviewed option is not part of the select element.\" };\n target.value = option.value;\n target.dispatchEvent(new Event(\"input\", { bubbles: true }));\n target.dispatchEvent(new Event(\"change\", { bubbles: true }));\n return { ok: true, summary: `Selected ${clean(option.textContent || option.value)}.` };\n}\n\nconst readkinds: ReadonlySet<string> = new Set([\"readattribute\", \"readstyle\", \"readgeometry\", \"readvalue\", \"readtext\", \"readhtml\", \"countelements\", \"readtable\", \"readlinks\", \"readimages\", \"readmeta\", \"readforms\", \"readstorage\", \"waitfor\", \"waittext\", \"highlight\", \"mapclicks\", \"verifyvisible\", \"verifyenabled\", \"resolvexpath\"]);\nconst mutatingkinds: ReadonlySet<string> = new Set([\"presskey\", \"clickdeep\", \"rightclick\", \"doubleclick\", \"drag\", \"drop\", \"upload\", \"clear\", \"check\", \"uncheck\", \"toggle\", \"submit\", \"setattribute\", \"removeattribute\", \"writestorage\", \"evaluate\", \"fullscreen\"]);\nconst controlkinds: ReadonlySet<string> = new Set([\"typetime\", \"appendtext\", \"setvalue\", \"typeedit\", \"keyhold\", \"keyrelease\", \"submitsearch\", \"selectmulti\", \"chooseradio\", \"setslider\", \"setdate\", \"setcolor\", \"expanddetails\"]);\nconst interactkinds: ReadonlySet<string> = new Set([\"clicktext\", \"clickaria\", \"clickname\", \"pierceshadow\", \"enterframe\"]);\nconst pointerkinds: ReadonlySet<string> = new Set([\"movepointer\", \"clickpoint\", \"shiftclick\"]);\nconst observationkinds: ReadonlySet<string> = new Set([\"a11ytree\", \"readvisible\", \"readertree\", \"readoutline\", \"readselection\", \"readopengraph\", \"readlang\", \"detectlanguage\", \"listshadow\", \"listframes\"]);\nconst detectionkinds: ReadonlySet<string> = new Set([\"detectlists\", \"detecttables\", \"detectinfinitescroll\", \"detectvirtual\", \"detectlazy\", \"detectsticky\", \"detectscrolllock\", \"countpages\", \"classifypage\", \"fingerprintsection\", \"readscrollpos\"]);\nconst watchstepkinds: ReadonlySet<string> = new Set([\"watchmutate\", \"watchbanner\", \"watchfocus\", \"waitquiet\", \"readjson\", \"diffsnapshots\", \"deriveselector\"]);\nconst debugstepkinds: ReadonlySet<string> = new Set([\"watchconsole\", \"watcherrors\", \"watchtasks\"]);\nconst cdpstepkinds: ReadonlySet<string> = new Set([\"attachcdp\", \"detachcdp\", \"cdpcmd\", \"watchcdp\", \"setbreakpoint\", \"stepcode\", \"watchexpr\", \"overridescript\"]);\nconst profilestepkinds: ReadonlySet<string> = new Set([\"measureflow\", \"heapshot\", \"trackmemory\", \"profilecpu\", \"watchshifts\", \"traceload\", \"capturesourcemaps\"]);\nconst emulationstepkinds: ReadonlySet<string> = new Set([\"emulatedevice\", \"emulatenetwork\", \"emulatelocate\", \"setuseragent\", \"overridepermission\", \"blackboxscripts\"]);\nconst navstepkinds: ReadonlySet<string> = new Set([\"waitload\", \"waiturl\", \"followlink\", \"spanav\", \"spawait\", \"rewritequery\", \"setfragment\", \"stopnav\", \"prefetch\", \"preconnect\", \"printpdf\"]);\nconst formkinds: ReadonlySet<string> = new Set([\"fillform\", \"filllabel\", \"fillplaceholder\", \"detectfields\", \"generatevalues\", \"readerrors\", \"skiphoneypot\", \"detectlogin\", \"detecttemplate\", \"handoffcaptcha\", \"asksubmit\", \"submitform\", \"consentpassword\", \"attachfile\"]);\nconst wizardkinds: ReadonlySet<string> = new Set([\"runwizard\", \"selectchain\", \"picktypeahead\", \"pickdate\", \"fillcard\", \"fillcode\"]);\nconst datastepkinds: ReadonlySet<string> = new Set([\"scrapetable\", \"paginateextract\"]);\n\n/** Performs one local action after the background policy gate and a fresh target resolution. */\nexport async function performstep(step: toolstep, expectedorigin: string, rootdocument: Document = document): Promise<stepresult> {\n if (location.origin !== expectedorigin) return { ok: false, summary: \"Page origin changed before action.\" };\n if (step.kind === \"observe\") return { ok: true, summary: \"Observation completed.\" };\n if (step.kind === \"wait\") {\n const requested = step.value ? Number.parseInt(step.value, 10) : 250;\n const duration = Number.isFinite(requested) && requested > 0 ? requested : 0;\n return new Promise(resolve => window.setTimeout(() => resolve({ ok: true, summary: `Reviewed wait of ${duration} milliseconds completed.` }), duration));\n }\n if (step.kind === \"extract\") return extractcontent(step.target, rootdocument);\n if (step.kind === \"navigate\") {\n if (!step.value || new URL(step.value).origin !== expectedorigin) return { ok: false, summary: \"Navigation target is outside the approved origin.\" };\n location.assign(step.value);\n return { ok: true, summary: \"Navigation request sent.\" };\n }\n if (step.kind === \"reload\") { location.reload(); return { ok: true, summary: \"Page reload requested.\" }; }\n if (step.kind === \"back\") { history.back(); return { ok: true, summary: \"History back requested.\" }; }\n if (step.kind === \"forward\") { history.forward(); return { ok: true, summary: \"History forward requested.\" }; }\n if (navstepkinds.has(step.kind)) return await runpagenav(step, rootdocument);\n if (step.kind === \"writeclipboard\") {\n const text = step.value ?? \"\";\n await navigator.clipboard.writeText(text);\n return { ok: true, summary: `Wrote ${text.length} reviewed character${text.length === 1 ? \"\" : \"s\"} to the clipboard with payload hash ${checksum(text)}.`, details: { length: text.length, hash: checksum(text), destination: \"clipboard\" } };\n }\n if (step.kind === \"scrollpage\") {\n const options = stepoptions(step);\n window.scrollBy({ left: typeof options.x === \"number\" ? options.x : 0, top: typeof options.y === \"number\" ? options.y : 600, behavior: \"auto\" });\n return { ok: true, summary: \"Window scrolled by the reviewed amounts.\" };\n }\n if (step.kind === \"scrollend\") { window.scrollTo(0, document.documentElement.scrollHeight); return { ok: true, summary: \"Window scrolled to the page end.\" }; }\n if (step.kind === \"scrolltop\") { window.scrollTo(0, 0); return { ok: true, summary: \"Window scrolled to the page top.\" }; }\n const resolution = resolvestep(step, rootdocument);\n if (resolution.status === \"ambiguous\") {\n return { ok: false, summary: `The reviewed ${resolution.mode} reference matched ${resolution.candidates.length} elements: ${resolution.candidates.join(\"; \")}.`, details: { mode: resolution.mode, candidates: resolution.candidates } };\n }\n const element = resolution.status === \"resolved\" ? resolution.element : null;\n let result: stepresult | Promise<stepresult>;\n if (formkinds.has(step.kind)) result = runpageform(step, element, rootdocument);\n else if (wizardkinds.has(step.kind)) result = runpagewizard(step, element, rootdocument);\n else if (datastepkinds.has(step.kind)) result = runpagedata(step, element, rootdocument);\n else if (readkinds.has(step.kind)) result = runpageread(step, element, rootdocument);\n else if (controlkinds.has(step.kind)) result = runpagecontrol(step, element, rootdocument);\n else if (interactkinds.has(step.kind)) return await runinteractstep(step, expectedorigin, performstep);\n else if (pointerkinds.has(step.kind)) result = runpointerstep(step, resolution);\n else if (observationkinds.has(step.kind)) result = runpageobservation(step, element, rootdocument);\n else if (detectionkinds.has(step.kind)) result = runpagedetection(step, element, rootdocument);\n else if (watchstepkinds.has(step.kind)) result = runpagewatch(step, element, rootdocument);\n else if (debugstepkinds.has(step.kind)) return await rundebugwatch(step);\n else if (cdpstepkinds.has(step.kind)) return await runcdpstep(step);\n else if (profilestepkinds.has(step.kind)) return await runprofilestep(step);\n else if (emulationstepkinds.has(step.kind)) return await runemulationstep(step);\n else if (mutatingkinds.has(step.kind)) result = runpageaction(step, element);\n else {\n if (!element) return { ok: false, summary: \"Action target is no longer available.\" };\n if (step.kind === \"scrollby\") {\n const options = stepoptions(step);\n element.scrollBy({ left: typeof options.x === \"number\" ? options.x : 0, top: typeof options.y === \"number\" ? options.y : 600, behavior: \"auto\" });\n result = { ok: true, summary: \"Container scrolled by the reviewed amounts.\" };\n } else if (step.kind === \"focus\") { element.focus(); result = { ok: true, summary: \"Target focused.\" }; }\n else if (step.kind === \"inspect\") result = { ok: true, summary: `Target: ${label(element) || element.tagName.toLowerCase()}.` };\n else if (step.kind === \"click\") { element.click(); result = { ok: true, summary: \"Reviewed click completed.\" }; }\n else if (step.kind === \"scroll\") result = scrolltarget(element);\n else if (step.kind === \"hover\") result = hovertarget(element);\n else if (step.kind === \"select\") result = selectoption(element, step.value ?? \"\");\n else if (step.kind === \"type\") {\n if (!(element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement)) return { ok: false, summary: \"Target cannot receive text.\" };\n if (typeof step.value !== \"string\") return { ok: false, summary: \"Approved text is absent.\" };\n element.focus();\n element.value = step.value;\n element.dispatchEvent(new Event(\"input\", { bubbles: true }));\n element.dispatchEvent(new Event(\"change\", { bubbles: true }));\n result = { ok: true, summary: \"Approved text entered.\" };\n } else return { ok: false, summary: \"Unsupported action.\" };\n }\n const output = await result;\n if (resolution.status === \"resolved\") {\n return { ...output, details: { ...(output.details ?? {}), mode: resolution.target.mode, resolvedtarget: resolution.target } };\n }\n return output;\n}\n\n/** Measures the full scroll width and height before any stitching begins, beside the viewport geometry, the device pixel ratio and the current scroll position. */\nexport function measurepage(): { scrollwidth: number; scrollheight: number; viewportwidth: number; viewportheight: number; pixelratio: number; scrollx: number; scrolly: number } {\n const root = document.documentElement;\n return {\n scrollwidth: Math.max(root.scrollWidth, document.body?.scrollWidth ?? 0),\n scrollheight: Math.max(root.scrollHeight, document.body?.scrollHeight ?? 0),\n viewportwidth: window.innerWidth,\n viewportheight: window.innerHeight,\n pixelratio: window.devicePixelRatio || 1,\n scrollx: window.scrollX,\n scrolly: window.scrollY,\n };\n}\n\n/** Returns the pixel ratio scaled rect of one target element with the viewport crossing flag for the tiled fallback. */\nexport function elementrect(selector: string): { ok: boolean; rect?: regionrect; pixelratio?: number; crossesviewport?: boolean; summary: string } {\n const target = document.querySelector(selector);\n if (!target) return { ok: false, summary: \"The reviewed capture element is no longer available.\" };\n const bounds = target.getBoundingClientRect();\n if (bounds.width <= 0 || bounds.height <= 0) return { ok: false, summary: \"The reviewed capture element is not currently visible.\" };\n const viewport = { width: window.innerWidth, height: window.innerHeight };\n const rect: regionrect = { x: Math.round(bounds.left + window.scrollX), y: Math.round(bounds.top + window.scrollY), width: Math.round(bounds.width), height: Math.round(bounds.height) };\n const viewrect: regionrect = { x: bounds.left, y: bounds.top, width: bounds.width, height: bounds.height };\n return { ok: true, rect, pixelratio: window.devicePixelRatio || 1, crossesviewport: viewrect.x < 0 || viewrect.y < 0 || viewrect.x + viewrect.width > viewport.width || viewrect.y + viewrect.height > viewport.height, summary: `Measured the capture element at ${rect.width} by ${rect.height} css pixels.` };\n}\n\n/** Resolves the unique element references of one selector for the foreach executor of the workflow control engine: every matched element reports its stable selector without mutating the page. */\nexport function queryelements(query: string): { ok: boolean; selectors: string[]; summary: string } {\n const matched = [...document.querySelectorAll(query)];\n if (matched.length === 0) return { ok: true, selectors: [], summary: `The reviewed selector ${query} matched no element.` };\n return { ok: true, selectors: matched.map(element => selector(element)), summary: `Resolved ${matched.length} element reference${matched.length === 1 ? \"\" : \"s\"} of the reviewed selector ${query}.` };\n}\n\nconst scrollbarstyleid = \"devthinkcapturehider\";\n\n/** Prepares the page for capture: hides the capture scrollbars through a scoped style rule and returns the original scroll position. */\nexport function preparecapture(): { scrollx: number; scrolly: number } {\n if (!document.getElementById(scrollbarstyleid)) {\n const style = document.createElement(\"style\");\n style.id = scrollbarstyleid;\n style.setAttribute(\"aria-hidden\", \"true\");\n style.textContent = \"html::-webkit-scrollbar,body::-webkit-scrollbar{display:none!important}html{scrollbar-width:none!important}\";\n document.documentElement.append(style);\n }\n return { scrollx: window.scrollX, scrolly: window.scrollY };\n}\n\n/** Scrolls the page to one reviewed tile offset and reports the settled position. */\nexport function scrollcapture(x: number, y: number): { x: number; y: number } {\n window.scrollTo(x, y);\n return { x: window.scrollX, y: window.scrollY };\n}\n\n/** Restores the original scroll position after the last tile and removes the capture scrollbar hider. */\nexport function restorecapture(state: { scrollx: number; scrolly: number }): void {\n document.getElementById(scrollbarstyleid)?.remove();\n window.scrollTo(state.scrollx, state.scrolly);\n}\n\n/** Scrolls one scrollable container to a reviewed step top and reports its geometry. */\nexport function scrollcontainercapture(selector: string, top: number): { ok: boolean; top?: number; height?: number; viewportheight?: number; summary: string } {\n const container = document.querySelector(selector);\n if (!container) return { ok: false, summary: \"The reviewed scrollable container is no longer available.\" };\n if (!(container instanceof HTMLElement)) return { ok: false, summary: \"The reviewed scrollable container cannot scroll.\" };\n container.scrollTo({ top, behavior: \"auto\" });\n return { ok: true, top: container.scrollTop, height: container.scrollHeight, viewportheight: container.clientHeight, summary: `Scrolled the container to ${container.scrollTop} of ${container.scrollHeight} pixels.` };\n}\n\n/** Waits the reviewed settle time between capture tiles. */\nexport function waitsettle(milliseconds: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, Math.max(0, milliseconds)));\n}\n\n\n/** Collects the visible text of one pdf report segment: every block element whose bounds intersect the reviewed vertical range contributes its text, so paginated reports split at reviewed break points. */\nexport function pdfsegment(top: number, height: number): { ok: boolean; text: string; summary: string } {\n const blocks = [...document.querySelectorAll(\"h1,h2,h3,h4,h5,h6,p,li,td,th,blockquote,pre,figcaption,section,article > div\")];\n const lines: string[] = [];\n for (const block of blocks) {\n const bounds = block.getBoundingClientRect();\n const blocktop = bounds.top + window.scrollY;\n const blockbottom = blocktop + bounds.height;\n if (blockbottom <= top || blocktop >= top + height) continue;\n const text = clean(block.textContent ?? \"\");\n if (text) lines.push(text);\n }\n const text = lines.join(\"\\n\");\n return { ok: true, text, summary: `Collected ${text.length} characters of the report segment at ${Math.round(top)} to ${Math.round(top + height)} pixels.` };\n}\n\n/** Resolves the scroll tops of the reviewed pdf break point selectors for paginated reports. */\nexport function pdfbreaks(selectors: string[]): Array<{ selector: string; top: number }> {\n const resolved: Array<{ selector: string; top: number }> = [];\n for (const selector of selectors) {\n const target = document.querySelector(selector);\n if (!target) continue;\n resolved.push({ selector, top: Math.round(target.getBoundingClientRect().top + window.scrollY) });\n }\n return resolved;\n}\n\n/** Grabs one still frame of a video element at the reviewed timestamp: the video seeks, pauses and draws to a canvas that encodes as an image; cross origin videos without cors refuse the draw honestly. */\nexport async function videoframe(selector: string, timestamp: number | undefined, poster: boolean): Promise<{ ok: boolean; dataurl?: string; width?: number; height?: number; summary: string }> {\n const target = document.querySelector(selector);\n if (!(target instanceof HTMLVideoElement)) return { ok: false, summary: \"The reviewed frame source is not a video element.\" };\n target.pause();\n if (typeof timestamp === \"number\" && Number.isFinite(timestamp) && timestamp >= 0 && timestamp <= (target.duration || 0)) {\n await new Promise<void>(resolve => {\n const done = (): void => resolve();\n target.addEventListener(\"seeked\", done, { once: true });\n target.currentTime = timestamp;\n window.setTimeout(done, 1500);\n });\n }\n const width = target.videoWidth || target.clientWidth || 1;\n const height = target.videoHeight || target.clientHeight || 1;\n try {\n const canvas = document.createElement(\"canvas\");\n canvas.width = width;\n canvas.height = height;\n const context = canvas.getContext(\"2d\");\n if (!context) return { ok: false, summary: \"The frame grab could not create a canvas context.\" };\n context.drawImage(target, 0, 0, width, height);\n const dataurl = canvas.toDataURL(\"image/png\");\n return { ok: dataurl.length > 100, dataurl, width, height, summary: `Grabbed the video frame at ${target.currentTime.toFixed(2)} seconds of ${width} by ${height} pixels${poster ? \" as the poster frame\" : \"\"}.` };\n } catch {\n return { ok: false, summary: \"The video frame draw was refused; cross origin videos need cors headers before frames can be read.\" };\n }\n}\n\n/** Reads the content of one canvas element: plain 2d canvases return their buffer directly and webgl canvases request the buffer through a preserved read; tainted canvases refuse honestly. */\nexport function canvasdata(selector: string): { ok: boolean; context: \"2d\" | \"webgl\"; dataurl?: string; width?: number; height?: number; preserved?: boolean; summary: string } {\n const target = document.querySelector(selector);\n if (!(target instanceof HTMLCanvasElement)) return { ok: false, context: \"2d\", summary: \"The reviewed canvas element is no longer available.\" };\n const width = target.width || 1;\n const height = target.height || 1;\n const kind: \"2d\" | \"webgl\" = target.getContext(\"2d\") ? \"2d\" : \"webgl\";\n try {\n let dataurl = \"\";\n let preserved = false;\n if (kind === \"2d\") {\n dataurl = target.toDataURL(\"image/png\");\n } else {\n dataurl = target.toDataURL(\"image/png\");\n if (dataurl.length <= 100) {\n const gl = (target.getContext(\"webgl\") ?? target.getContext(\"experimental-webgl\")) as WebGLRenderingContext | null;\n if (gl) {\n const pixels = new Uint8Array(width * height * 4);\n gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);\n const canvas = document.createElement(\"canvas\");\n canvas.width = width;\n canvas.height = height;\n const context = canvas.getContext(\"2d\");\n if (context) {\n const image = context.createImageData(width, height);\n for (let row = 0; row < height; row += 1) {\n const source = (height - 1 - row) * width * 4;\n const destination = row * width * 4;\n image.data.set(pixels.subarray(source, source + width * 4), destination);\n }\n context.putImageData(image, 0, 0);\n dataurl = canvas.toDataURL(\"image/png\");\n preserved = true;\n }\n }\n }\n }\n return { ok: dataurl.length > 100, context: kind, dataurl, width, height, preserved, summary: `Read the ${kind} canvas buffer of ${width} by ${height} pixels${kind === \"webgl\" ? preserved ? \" through a preserved readPixels pass\" : \" through the preserved drawing buffer\" : \"\"}.` };\n } catch {\n return { ok: false, context: kind, summary: \"The canvas read was refused; tainted canvas content needs cross origin resources with cors headers.\" };\n }\n}\n\n/** Probes the media streams of the page: every video and audio element with a srcObject reports its track kinds, labels, settings and live states; peer connection statistics stay outside the isolated world bridge. */\nexport function streamelements(selector?: string): Array<Record<string, unknown>> {\n const root = selector ? document.querySelector(selector) : document;\n if (!root) return [];\n const entries: Array<Record<string, unknown>> = [];\n for (const element of [...root.querySelectorAll(\"video, audio\")]) {\n const media = element instanceof HTMLMediaElement ? element : null;\n if (!media) continue;\n const stream = media.srcObject;\n if (!(stream instanceof MediaStream)) continue;\n entries.push({\n kind: \"webrtc\",\n label: stream.id,\n live: stream.active,\n tracks: stream.getTracks().map(track => ({\n kind: track.kind,\n label: track.label,\n state: track.readyState,\n ...(track.kind === \"video\" ? { width: track.getSettings().width, height: track.getSettings().height, framerate: track.getSettings().frameRate } : {}),\n })),\n });\n }\n return entries;\n}\n\n/** Reads the embedded video and audio sources of the page with their formats, durations, dimensions, codecs and track lists. */\nexport function mediaelements(): Array<Record<string, unknown>> {\n const entries: Array<Record<string, unknown>> = [];\n for (const element of [...document.querySelectorAll(\"video, audio\")]) {\n if (!(element instanceof HTMLMediaElement)) continue;\n const source = element.querySelector(\"source\");\n const url = element.currentSrc || element.src || (source instanceof HTMLSourceElement ? source.src : \"\") || \"\";\n if (!url) continue;\n const type = (source instanceof HTMLSourceElement ? source.type : \"\") || \"\";\n const codecs = type.includes(\"codecs=\") ? type.slice(type.indexOf(\"codecs=\") + \"codecs=\".length).replace(/[\"']/g, \"\") : \"\";\n entries.push({\n url,\n mime: type.split(\";\")[0] || \"\",\n duration: Number.isFinite(element.duration) ? element.duration : 0,\n width: element instanceof HTMLVideoElement ? element.videoWidth : 0,\n height: element instanceof HTMLVideoElement ? element.videoHeight : 0,\n codecs,\n tracks: [...element.textTracks].map(track => track.label || track.kind).filter(Boolean),\n });\n }\n return entries;\n}\n\n/** Collects the page assets: declared favicons and apple touch icons with their sizes, manifest declared icons and logo candidates from meta images and header imagery. */\nexport async function pageassets(): Promise<Array<Record<string, unknown>>> {\n const entries: Array<Record<string, unknown>> = [];\n for (const link of [...document.querySelectorAll(\"link[rel]\")]) {\n const rel = (link.getAttribute(\"rel\") ?? \"\").toLowerCase();\n const href = link.getAttribute(\"href\");\n if (!href || !(rel.includes(\"icon\") || rel.includes(\"apple-touch\"))) continue;\n const resolved = new URL(href, location.href).toString();\n entries.push({ kind: \"favicon\", url: resolved, bytes: 0, sizes: link.getAttribute(\"sizes\") ?? \"any\" });\n }\n const og = document.querySelector('meta[property=\"og:image\"]');\n if (og instanceof HTMLMetaElement && og.content) entries.push({ kind: \"logo\", url: new URL(og.content, location.href).toString(), bytes: 0, sizes: \"og\" });\n for (const image of [...document.querySelectorAll(\"header img, nav img, img[alt*=logo i], img[src*=logo i]\")]) {\n const url = image instanceof HTMLImageElement ? image.currentSrc || image.src : \"\";\n if (!url) continue;\n entries.push({ kind: \"logo\", url: new URL(url, location.href).toString(), bytes: 0, sizes: image instanceof HTMLImageElement ? `${image.naturalWidth}x${image.naturalHeight}` : \"\" });\n }\n const manifestlink = document.querySelector('link[rel=\"manifest\"]');\n if (manifestlink instanceof HTMLLinkElement && manifestlink.href) {\n try {\n const response = await fetch(manifestlink.href);\n const manifest = await response.json() as { icons?: Array<{ src?: string; sizes?: string }> };\n for (const icon of manifest.icons ?? []) {\n if (!icon.src) continue;\n entries.push({ kind: \"favicon\", url: new URL(icon.src, location.href).toString(), bytes: 0, sizes: icon.sizes ?? \"any\" });\n }\n } catch { /* a refused manifest fetch leaves the declared icons unreported */ }\n }\n return entries;\n}\n\n/** Detects every image of the page inside an optional selector scope: urls, alt text, natural dimensions, transfer sizes from the performance entries and mime types. */\nexport function pageimages(selector?: string): Array<Record<string, unknown>> {\n const root = selector ? document.querySelector(selector) : document;\n if (!root) return [];\n const transfers = new Map<string, number>();\n for (const entry of performance.getEntriesByType(\"resource\")) {\n const resource = entry as PerformanceResourceTiming;\n if (resource.transferSize > 0) transfers.set(resource.name, resource.transferSize);\n }\n const images: Array<Record<string, unknown>> = [];\n for (const element of [...root.querySelectorAll(\"img\")]) {\n if (!(element instanceof HTMLImageElement)) continue;\n const url = element.currentSrc || element.src;\n if (!url) continue;\n const resolved = new URL(url, location.href).toString();\n const type = element.getAttribute(\"type\") ?? \"\";\n images.push({\n url: resolved,\n alt: element.alt ?? \"\",\n width: element.naturalWidth || element.width,\n height: element.naturalHeight || element.height,\n bytes: transfers.get(resolved) ?? 0,\n mime: type || (element.src.startsWith(\"data:\") ? element.src.slice(5, element.src.indexOf(\";\")) : \"image/*\"),\n });\n }\n return images;\n}\n/** Parses fetched markup through the page domparser and runs the reviewed html queries: attribute values, text and element counts per query. */\nexport function parsehtmlmarkup(body: string, queries: Array<{ selector: string; attribute?: string; multi?: boolean }>): Array<{ selector: string; attribute?: string; multi: boolean; count: number; values: string[] }> {\n const parsed = new DOMParser().parseFromString(body, \"text/html\");\n return queries.map(query => {\n const matches = [...parsed.querySelectorAll(query.selector)];\n const chosen = query.multi === true ? matches : matches.slice(0, 1);\n const values = chosen.map(element => query.attribute !== undefined ? element.getAttribute(query.attribute) ?? \"\" : element.textContent ?? \"\");\n return { selector: query.selector, ...(query.attribute !== undefined ? { attribute: query.attribute } : {}), multi: query.multi === true, count: matches.length, values };\n });\n}\n\n\n/** Reads the request lifecycle facts the page timing buffers expose: every resource and navigation entry with its url, initiator, timing, transfer size, protocol and the response status a navigation entry reports; the buffers expose no header names, body bytes or subresource status codes. */\nexport function resourcerecords(): Array<Record<string, unknown>> {\n const entries = [...performance.getEntriesByType(\"resource\"), ...performance.getEntriesByType(\"navigation\")];\n return entries.map(entry => {\n const resource = entry as PerformanceResourceTiming & { responseStatus?: number };\n return {\n name: resource.name,\n initiatorType: resource.initiatorType ?? \"\",\n entryType: resource.entryType,\n startTime: resource.startTime,\n duration: resource.duration,\n transferSize: resource.transferSize ?? 0,\n nextHopProtocol: resource.nextHopProtocol ?? \"\",\n ...(typeof resource.responseStatus === \"number\" ? { responseStatus: resource.responseStatus } : {}),\n };\n });\n}\n\n/** Writes reviewed cookies for the granted origin of the page through the page document cookie jar: every record writes its name, value and path with an optional expiry; the write happens on the page the user granted, never through a browser cookies permission. */\nexport function writecookies(records: Array<{ name: string; value: string; path: string; expiresat?: number }>): { written: number; summary: string } {\n let written = 0;\n for (const record of records) {\n const expiry = record.expiresat !== undefined ? `; expires=${new Date(record.expiresat).toUTCString()}` : \"\";\n document.cookie = `${record.name}=${record.value}; path=${record.path}${expiry}; samesite=lax`;\n written += 1;\n }\n return { written, summary: `Wrote ${written} reviewed cookie${written === 1 ? \"\" : \"s\"} through the page cookie jar of ${location.origin}.` };\n}\n\n/** Reads the cookies of the granted origin through the page document cookie jar: document.cookie exposes the name and value pairs of the origin only, with no domain, path or expiry metadata. */\nexport function readcookies(): Array<{ name: string; value: string }> {\n return document.cookie.split(\";\").map(pair => pair.trim()).filter(pair => pair.length > 0).map(pair => {\n const separator = pair.indexOf(\"=\");\n return separator === -1 ? { name: pair, value: \"\" } : { name: pair.slice(0, separator), value: pair.slice(separator + 1) };\n });\n}\n\n/** Clears the cookies of the granted origin through the page document cookie jar: every matched name is expired on the root path; an absent name list clears every cookie the origin jar exposes. */\nexport function clearcookies(names?: string[]): { cleared: number; summary: string } {\n const jar = readcookies();\n const targets = names !== undefined && names.length > 0 ? jar.filter(cookie => names.includes(cookie.name)) : jar;\n for (const cookie of targets) document.cookie = `${cookie.name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;\n return { cleared: targets.length, summary: `Cleared ${targets.length} cookie${targets.length === 1 ? \"\" : \"s\"} through the page cookie jar of ${location.origin}.` };\n}\n\nObject.assign(globalThis, { devthinkbridge: { capturesnapshot, previewtarget, performstep, readdialogs, measurepage, elementrect, queryelements, preparecapture, scrollcapture, restorecapture, scrollcontainercapture, waitsettle, pdfsegment, pdfbreaks, videoframe, canvasdata, streamelements, mediaelements, pageassets, pageimages, parsehtmlmarkup, resourcerecords, writecookies, readcookies, clearcookies, revertemulationlayer } });\n"],
4
+ "sourcesContent": ["import type { breakpointspec, cdpallowlist, cdpcommand, cdpeventrule, cdpsession, pausestate, scriptoverride, stackframe, stepmode, teardownplan, watchexpression } from \"./types.js\";\n\n/**\n * Devtools protocol bus for the 1.1.46 debugging family.\n * Every correlated rule for the reviewed instrumented devtools session lives in this file: the domain grammar, the attach and detach lifecycle with the honest derivation note, the raw command records with duration and error class, the per session command serialization in send order, the domain event rules with match filters and per domain event counts, the breakpoint input grammar, the pause state capture with call frames, the step mode grammar, the watch expression values per pause, the script override url patterns and the teardown plan that reverts every breakpoint and override and decides the resume policy when the user detaches the debugger.\n * The chrome devtools protocol is unavailable without the debugger permission, which the manifest gate forbids, so every command routes through the page-instrumented harness injected through the scripting api; the derivation is recorded on every session instead of hidden.\n */\n\n/** The debugging kinds of the devtools family, listed among the available capabilities of every proposal request. */\nexport const cdpkinds: string[] = [\"attachcdp\", \"detachcdp\", \"cdpcmd\", \"watchcdp\", \"setbreakpoint\", \"stepcode\", \"watchexpr\", \"overridescript\"];\n\n/** The reviewed devtools domain grammar: runtime evaluation, log capture, debugger state, dom snapshots, network facts and page lifecycle; the enabled subset stays a user choice bounded by this grammar only. */\nexport const cdpdomains: string[] = [\"Runtime\", \"Log\", \"Debugger\", \"DOM\", \"Network\", \"Page\"];\n\n/** Resolves the domain of one devtools method name of the form Domain.method; malformed methods resolve to undefined. */\nexport function methoddomain(method: string): string | undefined {\n const match = /^([A-Z][A-Za-z]*)\\.([a-zA-Z][A-Za-z0-9]*)$/.exec(method.trim());\n return match?.[1];\n}\n\n/** Normalizes one reviewed devtools domain allowlist: the enabled domains bounded by the reviewed domain grammar and the optional method gates inside the enabled domains; a gate list that names no method of the enabled domains is refused. */\nexport function cdpallowlistof(value: unknown): cdpallowlist | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const domains = Array.isArray(entry.domains) ? entry.domains.filter((domain): domain is string => typeof domain === \"string\" && cdpdomains.includes(domain)) : [];\n if (domains.length === 0) return undefined;\n if (entry.methods === undefined) return { domains };\n const methods = Array.isArray(entry.methods) ? entry.methods.filter((method): method is string => typeof method === \"string\" && methoddomain(method) !== undefined && domains.includes(methoddomain(method) as string)) : [];\n if (methods.length === 0) return undefined;\n return { domains, methods };\n}\n\n/** True when the allowlist covers one method: the method domain must be enabled and a present method gate list must name the method. */\nexport function allowlistcovers(allowlist: cdpallowlist, method: string): boolean {\n const domain = methoddomain(method);\n if (domain === undefined) return false;\n if (!allowlist.domains.includes(domain)) return false;\n if (allowlist.methods !== undefined && !allowlist.methods.includes(method)) return false;\n return true;\n}\n\n/** Attaches one instrumented devtools session to the run tab with the reviewed enabled domains and the honest debugger derivation note. */\nexport function attachcdpsession(input: { id: string; runid: string; stepid: string; tabid: number; origin: string; domains: string[]; now: number; debuggerversion: string }): cdpsession {\n return { id: input.id, runid: input.runid, stepid: input.stepid, tabid: input.tabid, origin: input.origin, attachedat: input.now, domains: [...new Set(input.domains)], debuggerversion: input.debuggerversion };\n}\n\n/** Detaches one session cleanly: the detach time stamps the record and the domains stay auditable after the detach. */\nexport function detachcdpsession(session: cdpsession, at: number, userdetached = false): cdpsession {\n return { ...session, detachedat: at, ...(userdetached ? { userdetached: true } : {}) };\n}\n\n/** Builds one raw protocol command record with its method, params, domain, dotted result path, duration and error class. */\nexport function sendcdpcommand(input: { id: string; sessionid: string; runid: string; stepid: string; method: string; params?: Record<string, unknown>; resultpath?: string; duration: number; errorclass?: string; at: number }): cdpcommand {\n const domain = methoddomain(input.method);\n if (domain === undefined) return { ...input, method: input.method.trim(), domain: \"\", duration: input.duration, ...(input.errorclass !== undefined ? { errorclass: input.errorclass } : { errorclass: \"malformedmethod\" }), at: input.at };\n return { ...input, method: input.method.trim(), domain, duration: input.duration, ...(input.errorclass !== undefined ? { errorclass: input.errorclass } : {}), at: input.at };\n}\n\n/** Appends one command to the per session send queue so concurrent commands serialize in send order. */\nexport function serializecdpcommand(queue: cdpcommand[], command: cdpcommand): cdpcommand[] {\n return [...queue, command];\n}\n\n/** Normalizes one reviewed domain event rule: the domain of the reviewed grammar, the event name and the optional payload match filter. */\nexport function cdpeventruleof(value: unknown): Pick<cdpeventrule, \"domain\" | \"event\" | \"match\"> | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const domain = typeof entry.domain === \"string\" && cdpdomains.includes(entry.domain) ? entry.domain : undefined;\n const event = typeof entry.event === \"string\" && entry.event.trim() ? entry.event.trim() : undefined;\n if (domain === undefined || event === undefined) return undefined;\n const match = typeof entry.match === \"string\" && entry.match.trim() ? entry.match.trim() : undefined;\n return { domain, event, ...(match !== undefined ? { match } : {}) };\n}\n\n/** Matches observed domain events against the reviewed rules and counts the matched events per domain; the match filter must appear in the payload before an event forwards. */\nexport function watchcdpevents(rules: cdpeventrule[], events: Array<{ domain: string; event: string; payload?: string }>): { matched: Array<{ ruleid: string; domain: string; event: string; payload?: string }>; counts: Record<string, number> } {\n const matched: Array<{ ruleid: string; domain: string; event: string; payload?: string }> = [];\n const counts: Record<string, number> = {};\n for (const domain of cdpdomains) counts[domain] = 0;\n for (const event of events) {\n for (const rule of rules) {\n if (rule.domain !== event.domain || rule.event !== event.event) continue;\n if (rule.match !== undefined && !(event.payload ?? \"\").includes(rule.match)) continue;\n matched.push({ ruleid: rule.id, domain: event.domain, event: event.event, ...(event.payload !== undefined ? { payload: event.payload } : {}) });\n counts[event.domain] = (counts[event.domain] ?? 0) + 1;\n }\n }\n return { matched, counts };\n}\n\n/** Normalizes one reviewed breakpoint input: the script url, the zero based line, the optional column and the condition of the reviewed expression grammar. */\nexport function breakpointinputof(value: unknown): Pick<breakpointspec, \"url\" | \"line\" | \"column\" | \"condition\"> | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const url = typeof entry.url === \"string\" && entry.url.trim() ? entry.url.trim() : undefined;\n const line = typeof entry.line === \"number\" && Number.isInteger(entry.line) && entry.line >= 0 ? entry.line : undefined;\n if (url === undefined || line === undefined) return undefined;\n const column = typeof entry.column === \"number\" && Number.isInteger(entry.column) && entry.column >= 0 ? entry.column : undefined;\n const condition = typeof entry.condition === \"string\" && entry.condition.trim() ? entry.condition.trim() : undefined;\n return { url, line, ...(column !== undefined ? { column } : {}), ...(condition !== undefined ? { condition } : {}) };\n}\n\n/** Captures one pause state from the instrumented pause: the reason, the call frames, the hit breakpoint and the dom snapshot reference of the page bridge capture. */\nexport function capturepause(input: { id: string; runid: string; stepid: string; reason: string; callframes: stackframe[]; hitbreakpoint?: string; domsnapshotid?: string; at: number }): pausestate {\n return { id: input.id, runid: input.runid, stepid: input.stepid, reason: input.reason, callframes: [...input.callframes], ...(input.hitbreakpoint !== undefined ? { hitbreakpoint: input.hitbreakpoint } : {}), ...(input.domsnapshotid !== undefined ? { domsnapshotid: input.domsnapshotid } : {}), at: input.at };\n}\n\n/** Normalizes one reviewed step mode of the instrumented debugger: stepover, stepinto, stepout or resume. */\nexport function stepmodeof(value: unknown): stepmode | undefined {\n const modes: stepmode[] = [\"stepover\", \"stepinto\", \"stepout\", \"resume\"];\n return typeof value === \"string\" && modes.includes(value as stepmode) ? value as stepmode : undefined;\n}\n\n/** Normalizes one reviewed watch expression input: the expression text and the pause scope it evaluates in. */\nexport function watchexpressionof(value: unknown): Pick<watchexpression, \"expression\" | \"scope\"> | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const expression = typeof entry.expression === \"string\" && entry.expression.trim() ? entry.expression.trim() : undefined;\n if (expression === undefined) return undefined;\n const scope = typeof entry.scope === \"string\" && entry.scope.trim() ? entry.scope.trim() : \"topframe\";\n return { expression, scope };\n}\n\n/** Records one watch value captured at a pause with the pause scope correlation. */\nexport function recordwatchvalue(expression: watchexpression, pauseid: string, value: string, at: number): watchexpression {\n return { ...expression, values: [...expression.values, { pauseid, value, at }] };\n}\n\n/** Normalizes one reviewed script override input: the url pattern that names its origin explicitly and the full fixture source. */\nexport function overrideinputof(value: unknown): Pick<scriptoverride, \"urlpattern\" | \"source\"> | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const urlpattern = typeof entry.urlpattern === \"string\" && entry.urlpattern.trim() ? entry.urlpattern.trim() : undefined;\n const source = typeof entry.source === \"string\" ? entry.source : undefined;\n if (urlpattern === undefined || source === undefined || source.trim().length === 0) return undefined;\n return { urlpattern, source };\n}\n\n/** Matches one script url against a reviewed override pattern of an explicit https origin with single star segments and double star subtrees. */\nexport function overridematches(urlpattern: string, url: string): boolean {\n const patternmatch = /^(https:\\/\\/[^/]+)(\\/.*)?$/.exec(urlpattern);\n const urlmatch = /^(https:\\/\\/[^/]+)(\\/.*)?$/.exec(url);\n if (!patternmatch || !urlmatch) return false;\n if (patternmatch[1] !== urlmatch[1]) return false;\n const patternpath = (patternmatch[2] ?? \"/\").split(\"/\").filter(segment => segment.length > 0);\n const urlpath = (urlmatch[2] ?? \"/\").split(\"/\").filter(segment => segment.length > 0);\n const walk = (patternindex: number, urlindex: number): boolean => {\n if (patternindex >= patternpath.length) return urlindex >= urlpath.length;\n const segment = patternpath[patternindex];\n if (segment === \"**\") return walk(patternindex + 1, urlindex) || (urlindex < urlpath.length && walk(patternindex, urlindex + 1));\n if (urlindex >= urlpath.length) return false;\n if (segment !== \"*\" && segment !== urlpath[urlindex]) return false;\n return walk(patternindex + 1, urlindex + 1);\n };\n return walk(0, 0);\n}\n\n/** Normalizes one reviewed teardown plan: the revert steps and the resume policy of resume, pause or ask. */\nexport function teardownplanof(value: unknown): teardownplan | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const revertsteps = Array.isArray(entry.revertsteps) ? entry.revertsteps.filter((step): step is string => typeof step === \"string\" && step.trim().length > 0) : [];\n const policy = entry.resumepolicy;\n if (revertsteps.length === 0) return undefined;\n if (policy !== undefined && policy !== \"resume\" && policy !== \"pause\" && policy !== \"ask\") return undefined;\n return { revertsteps, resumepolicy: policy ?? \"ask\" };\n}\n\n/** The teardown outcome of one session: every breakpoint and override reverts, the resume policy decides the continuation and a user detach keeps the session record alive for review while the run pauses. */\nexport interface teardowndecision {\n session: cdpsession;\n revertedbreakpoints: string[];\n revertedoverrides: string[];\n resumepolicy: teardownplan[\"resumepolicy\"];\n paused: boolean;\n keepsalive: boolean;\n}\n\n/** Tears one session down safely: the revert steps of the reviewed teardown plan revert every active breakpoint and override in order, the session detaches, and a user detach keeps the session record alive while the run pauses for review before continuing. */\nexport function teardowncdpsession(input: { session: cdpsession; breakpoints: breakpointspec[]; overrides: scriptoverride[]; plan: teardownplan | undefined; userdetached: boolean; at: number }): teardowndecision {\n const revertedbreakpoints = input.breakpoints.filter(spec => spec.revertedat === undefined).map(spec => spec.id);\n const revertedoverrides = input.overrides.filter(spec => spec.revertedat === undefined).map(spec => spec.id);\n const resumepolicy = input.userdetached ? \"pause\" : input.plan?.resumepolicy ?? \"ask\";\n return {\n session: detachcdpsession(input.session, input.at, input.userdetached),\n revertedbreakpoints,\n revertedoverrides,\n resumepolicy,\n paused: input.userdetached || resumepolicy === \"pause\",\n keepsalive: input.userdetached,\n };\n}\n", "import type { agentpreset, blackboxrule, devicepreset, emulationlayer, emulationstate, locationconsent, locationpreset, networkpreset, permissiongrant, permissionstate, presetlibrary, stackframe } from \"./types.js\";\n\n/**\n * Emulation layer engine for the 1.1.48 family.\n * Every correlated rule for the reviewed masks of the run lives in this file: the preset normalizers of the user curated device, network, location and agent libraries, the reviewed revert plan grammar, the layer apply and revert math with prior state capture and reverse order restore, the stacking conflict order where the last applied layer wins, the latitude, longitude and user agent grammars, the browser permission set, the blackbox pattern matching that hides third party frames from stack traces, the retention expiry of reverted layer states and the versioned preset library import and export.\n * True device metric, network condition, geolocation and user agent override needs browser debugger or platform permissions that the manifest gate forbids, so every layer applies page-injected overrides through the scripting api and shapes only the traffic the extension itself initiates; the derivation is recorded on every layer instead of hidden.\n */\n\n/** The emulation kinds of the 1.1.48 family, listed among the available capabilities of every proposal request. */\nexport const emulationkinds: string[] = [\"emulatedevice\", \"emulatenetwork\", \"emulatelocate\", \"setuseragent\", \"overridepermission\", \"blackboxscripts\"];\n\n/** The reviewed browser permission set of the override grammar; overrides outside this set are refused. */\nexport const browserpermissions: string[] = [\"geolocation\", \"notifications\", \"camera\", \"microphone\", \"clipboard-read\", \"clipboard-write\", \"midi\", \"persistent-storage\"];\n\n/** The reviewed permission states of an override: granted, denied or the browser default prompt. */\nexport const permissionstates: permissionstate[] = [\"granted\", \"denied\", \"prompt\"];\n\n/** The emulation family of one emulation action kind. */\nexport function familyofkind(kind: string): \"device\" | \"network\" | \"location\" | \"agent\" | \"permission\" | \"blackbox\" | undefined {\n if (kind === \"emulatedevice\") return \"device\";\n if (kind === \"emulatenetwork\") return \"network\";\n if (kind === \"emulatelocate\") return \"location\";\n if (kind === \"setuseragent\") return \"agent\";\n if (kind === \"overridepermission\") return \"permission\";\n if (kind === \"blackboxscripts\") return \"blackbox\";\n return undefined;\n}\n\n/** Normalizes one user curated device preset: the name, width, height, pixel ratio and the mobile flag with every bound a user choice only. */\nexport function devicepresetof(value: unknown): devicepreset | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const name = typeof entry.name === \"string\" && entry.name.trim() ? entry.name.trim() : undefined;\n const width = typeof entry.width === \"number\" && Number.isInteger(entry.width) && entry.width > 0 ? entry.width : undefined;\n const height = typeof entry.height === \"number\" && Number.isInteger(entry.height) && entry.height > 0 ? entry.height : undefined;\n const pixelratio = typeof entry.pixelratio === \"number\" && Number.isFinite(entry.pixelratio) && entry.pixelratio > 0 ? entry.pixelratio : undefined;\n if (name === undefined || width === undefined || height === undefined || pixelratio === undefined) return undefined;\n return { name, width, height, pixelratio, mobile: entry.mobile === true };\n}\n\n/** Normalizes one user curated network preset: the name, latency, download and upload bounds and the offline flag. */\nexport function networkpresetof(value: unknown): networkpreset | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const name = typeof entry.name === \"string\" && entry.name.trim() ? entry.name.trim() : undefined;\n const latency = typeof entry.latency === \"number\" && Number.isFinite(entry.latency) && entry.latency >= 0 ? entry.latency : undefined;\n const download = typeof entry.download === \"number\" && Number.isFinite(entry.download) && entry.download >= 0 ? entry.download : undefined;\n const upload = typeof entry.upload === \"number\" && Number.isFinite(entry.upload) && entry.upload >= 0 ? entry.upload : undefined;\n if (name === undefined || latency === undefined || download === undefined || upload === undefined) return undefined;\n return { name, latency, download, upload, offline: entry.offline === true };\n}\n\n/** Normalizes one user curated location preset: the name, the latitude and longitude inside the reviewed ranges and the non-negative accuracy radius. */\nexport function locationpresetof(value: unknown): locationpreset | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const name = typeof entry.name === \"string\" && entry.name.trim() ? entry.name.trim() : undefined;\n const latitude = typeof entry.latitude === \"number\" && Number.isFinite(entry.latitude) ? entry.latitude : undefined;\n const longitude = typeof entry.longitude === \"number\" && Number.isFinite(entry.longitude) ? entry.longitude : undefined;\n const accuracy = typeof entry.accuracy === \"number\" && Number.isFinite(entry.accuracy) && entry.accuracy >= 0 ? entry.accuracy : undefined;\n if (name === undefined || latitude === undefined || longitude === undefined || accuracy === undefined) return undefined;\n if (!locationrangevalid(latitude, longitude)) return undefined;\n return { name, latitude, longitude, accuracy };\n}\n\n/** Normalizes one user curated agent preset: the user agent string of the reviewed grammar, the platform and the non-empty brand list reported together. */\nexport function agentpresetof(value: unknown): agentpreset | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const name = typeof entry.name === \"string\" && entry.name.trim() ? entry.name.trim() : undefined;\n const useragent = typeof entry.useragent === \"string\" ? entry.useragent : undefined;\n const platform = typeof entry.platform === \"string\" && entry.platform.trim() ? entry.platform.trim() : undefined;\n const brands = Array.isArray(entry.brands) ? entry.brands.filter((brand): brand is string => typeof brand === \"string\" && brand.trim().length > 0) : [];\n if (name === undefined || useragent === undefined || platform === undefined || brands.length === 0) return undefined;\n if (!agentgrammarvalid(useragent)) return undefined;\n return { name, useragent, platform, brands: [...new Set(brands)] };\n}\n\n/** Normalizes one reviewed permission override: the name of the reviewed browser permission set, the state of the reviewed permission states and the run scope flag. */\nexport function permissiongrantof(value: unknown): permissiongrant | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const name = typeof entry.name === \"string\" && browserpermissions.includes(entry.name) ? entry.name : undefined;\n const state = typeof entry.state === \"string\" && permissionstates.includes(entry.state as permissionstate) ? entry.state as permissionstate : undefined;\n if (name === undefined || state === undefined) return undefined;\n return { name, state, runscope: entry.runscope !== false };\n}\n\n/** Normalizes one reviewed blackbox rule: a non-empty url pattern list where every pattern names its origin explicitly and the trace scope of profiles, traces or both. */\nexport function blackboxruleof(value: unknown): blackboxrule | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const urlpatterns = Array.isArray(entry.urlpatterns) ? entry.urlpatterns.filter((pattern): pattern is string => typeof pattern === \"string\" && /^https:\\/\\//.test(pattern)) : [];\n const tracescope = entry.tracescope;\n if (urlpatterns.length === 0) return undefined;\n if (tracescope !== \"profiles\" && tracescope !== \"traces\" && tracescope !== \"both\") return undefined;\n return { urlpatterns: [...new Set(urlpatterns)], tracescope };\n}\n\n/** Normalizes one reviewed revert plan: a non-empty ordered list of revert steps kept beside its layer; plans without steps are refused. */\nexport function revertplanof(value: unknown): string[] | undefined {\n const steps = Array.isArray(value) ? value.filter((step): step is string => typeof step === \"string\" && step.trim().length > 0) : [];\n return steps.length > 0 ? steps : undefined;\n}\n\n/** Builds one emulation layer record with its origin scope, apply time, captured prior state and reviewed revert plan. */\nexport function newlayer(input: { id: string; runid: string; stepid: string; family: emulationlayer[\"family\"]; name: string; originscope: string; revertplan: string[]; prior?: Record<string, unknown>; at: number }): emulationlayer {\n return { id: input.id, runid: input.runid, stepid: input.stepid, family: input.family, name: input.name, originscope: input.originscope, appliedat: input.at, ...(input.prior !== undefined ? { prior: input.prior } : {}), revertplan: [...input.revertplan] };\n}\n\n/** Builds the initial emulation state of one run scoped to its tab and origin. */\nexport function emulationstateof(input: { runid: string; tabid: number; origin: string; now: number }): emulationstate {\n return { runid: input.runid, tabid: input.tabid, origin: input.origin, layers: [], updatedat: input.now };\n}\n\n/** Stacks one layer onto the emulation state in apply order: a second layer of the same family replaces the first in effect while both stay in the history, so the last applied layer wins conflicts. */\nexport function applylayer(state: emulationstate, layer: emulationlayer, at: number): emulationstate {\n const layers = [...state.layers.filter(item => item.id !== layer.id), layer];\n return { ...state, layers, updatedat: at };\n}\n\n/** Reverts one layer of the state by its id: the revert time stamps the record while the layer history survives for review. */\nexport function revertlayer(state: emulationstate, layerid: string, at: number): emulationstate {\n const layers = state.layers.map(layer => layer.id === layerid && layer.revertedat === undefined ? { ...layer, revertedat: at } : layer);\n return { ...state, layers, updatedat: at };\n}\n\n/** Reverts every active layer of the state in reverse apply order: the reversed list is the exact restore sequence and every layer keeps its revert stamp. */\nexport function revertalllayers(state: emulationstate, at: number): { state: emulationstate; reverted: emulationlayer[] } {\n const reverted = [...state.layers].reverse().filter(layer => layer.revertedat === undefined);\n const layers = state.layers.map(layer => layer.revertedat === undefined ? { ...layer, revertedat: at } : layer);\n return { state: { ...state, layers, updatedat: at }, reverted };\n}\n\n/** Returns the active layers of the state in apply order. */\nexport function activelayers(state: emulationstate | undefined): emulationlayer[] {\n return state ? state.layers.filter(layer => layer.revertedat === undefined) : [];\n}\n\n/** Returns the names of the active layers for the response envelope and the review panel. */\nexport function layernames(state: emulationstate | undefined): string[] {\n return activelayers(state).map(layer => layer.name);\n}\n\n/** Counts the active layers of one family so the panel can warn when layers stack on one tab. */\nexport function stackedcount(state: emulationstate | undefined): number {\n return activelayers(state).length;\n}\n\n/** True when the reviewed latitude stays inside the -90 to 90 degree range and the longitude inside the -180 to 180 degree range. */\nexport function locationrangevalid(latitude: number, longitude: number): boolean {\n return Number.isFinite(latitude) && Number.isFinite(longitude) && latitude >= -90 && latitude <= 90 && longitude >= -180 && longitude <= 180;\n}\n\n/** Validates one user agent string against the reviewed grammar: tokens of word characters, separators, slashes, spaces, numbers and version dots; the string must carry at least one token pair and refuse line breaks. */\nexport function agentgrammarvalid(useragent: string): boolean {\n const text = useragent.trim();\n if (text.length === 0 || text.length > 512) return false;\n if (/[\\r\\n]/.test(text)) return false;\n if (!/^[A-Za-z0-9][A-Za-z0-9._+\\-()/:; ,]*$/.test(text)) return false;\n return /\\/\\d/.test(text) || /\\d+\\.\\d+/.test(text);\n}\n\n/** Grades one reviewed permission name by its power: location, camera, microphone and notification overrides carry the user's most sensitive signals and stay the highest grade. */\nexport function permissiongrade(name: string): \"powerful\" | \"standard\" {\n return name === \"geolocation\" || name === \"camera\" || name === \"microphone\" || name === \"notifications\" ? \"powerful\" : \"standard\";\n}\n\n/** Matches one script url against a reviewed blackbox pattern of an explicit https origin with single star segments and double star subtrees. */\nexport function blackboxmatches(urlpattern: string, url: string): boolean {\n const patternmatch = /^(https:\\/\\/[^/]+)(\\/.*)?$/.exec(urlpattern);\n const urlmatch = /^(https:\\/\\/[^/]+)(\\/.*)?$/.exec(url);\n if (!patternmatch || !urlmatch) return false;\n if (patternmatch[1] !== urlmatch[1]) return false;\n const patternpath = (patternmatch[2] ?? \"/\").split(\"/\").filter(segment => segment.length > 0);\n const urlpath = (urlmatch[2] ?? \"/\").split(\"/\").filter(segment => segment.length > 0);\n const walk = (patternindex: number, urlindex: number): boolean => {\n if (patternindex >= patternpath.length) return urlindex >= urlpath.length;\n const segment = patternpath[patternindex];\n if (segment === undefined) return false;\n if (segment === \"**\") return walk(patternindex + 1, urlindex) || (urlindex < urlpath.length && walk(patternindex, urlindex + 1));\n if (urlindex >= urlpath.length) return false;\n if (segment !== \"*\" && segment !== urlpath[urlindex]) return false;\n return walk(patternindex + 1, urlindex + 1);\n };\n return walk(0, 0);\n}\n\n/** Hides blackboxed frames from one stack trace: every frame whose url matches a rule pattern with a traces scope of traces or both is dropped so third party frames never enter the shaped trace. */\nexport function hideblackboxedframes(rules: blackboxrule[], frames: stackframe[]): stackframe[] {\n const patterns = rules.filter(rule => rule.tracescope === \"traces\" || rule.tracescope === \"both\").flatMap(rule => rule.urlpatterns);\n if (patterns.length === 0) return frames;\n return frames.filter(frame => !patterns.some(pattern => blackboxmatches(pattern, frame.url)));\n}\n\n/** Marks one url as blackboxed when any rule with a profiles or both trace scope matches it, so the trace shaping lists the hidden third party urls. */\nexport function blackboxedurls(rules: blackboxrule[], urls: string[]): string[] {\n const patterns = rules.flatMap(rule => rule.urlpatterns);\n return urls.filter(url => patterns.some(pattern => blackboxmatches(pattern, url)));\n}\n\n/** Expires the prior states of reverted layers after the retention window while the layer history itself always survives; an absent window keeps every prior state. */\nexport function expirelayers(state: emulationstate, retention: number | undefined, now: number): emulationstate {\n if (retention === undefined) return state;\n const layers = state.layers.map(layer => {\n if (layer.revertedat === undefined || layer.prior === undefined || layer.priorexpired === true) return layer;\n if (now - layer.revertedat <= retention) return layer;\n const { prior, ...metadata } = layer;\n void prior;\n return { ...metadata, priorexpired: true };\n });\n return { ...state, layers, updatedat: now };\n}\n\n/** Builds one shareable preset library file of the user curated presets; the version carries the library contract for imports through review. */\nexport function exportpresetlibrary(input: { devices: devicepreset[]; networks: networkpreset[]; locations: locationpreset[]; agents: agentpreset[]; now: number }): presetlibrary {\n return { version: 1, devices: [...input.devices], networks: [...input.networks], locations: [...input.locations], agents: [...input.agents], exportedat: input.now };\n}\n\n/** Parses one reviewed preset library file: every preset entry must pass its normalizer and a file without any valid preset is refused; unknown fields stay ignored. */\nexport function importpresetlibrary(value: unknown): presetlibrary | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const devices = (Array.isArray(entry.devices) ? entry.devices : []).flatMap(preset => { const parsed = devicepresetof(preset); return parsed !== undefined ? [parsed] : []; });\n const networks = (Array.isArray(entry.networks) ? entry.networks : []).flatMap(preset => { const parsed = networkpresetof(preset); return parsed !== undefined ? [parsed] : []; });\n const locations = (Array.isArray(entry.locations) ? entry.locations : []).flatMap(preset => { const parsed = locationpresetof(preset); return parsed !== undefined ? [parsed] : []; });\n const agents = (Array.isArray(entry.agents) ? entry.agents : []).flatMap(preset => { const parsed = agentpresetof(preset); return parsed !== undefined ? [parsed] : []; });\n if (devices.length + networks.length + locations.length + agents.length === 0) return undefined;\n return { version: typeof entry.version === \"number\" && Number.isInteger(entry.version) && entry.version >= 1 ? entry.version : 1, devices, networks, locations, agents, exportedat: typeof entry.exportedat === \"number\" ? entry.exportedat : Date.now() };\n}\n\n/** True when one approved location consent of that origin covers the reviewed coordinates; the prompt shows the exact latitude and longitude before the override applies. */\nexport function locationconsentcovers(origin: string, latitude: number, longitude: number, consents: locationconsent[]): boolean {\n return consents.some(consent => consent.origin === origin && consent.approved === true && consent.revokedat === undefined && consent.latitude === latitude && consent.longitude === longitude);\n}\n", "import type { consolediff, consoleentry, errorrecord, exchangerecord, loglevel, loglevelset, longtaskentry, netfailureentry, rejectionrecord, rotationrule, spamrule, stackframe, timelineentry, timelinesource } from \"./types.js\";\n\n/**\n * Run timeline logics for the 1.1.45 debugging family.\n * Every correlated rule for console capture with reviewed depth bounds and secret redaction, stack frame parsing, error, rejection and resource failure capture, long task attribution windows, the timeline binding to one run and its step ids, spam collapse with reviewed thresholds, log rotation without entry loss, level and source filters, the blocking duration per step window and the console diff between two runs lives in this file.\n * Console, error and task watching derives from page-injected listeners installed through the scripting api and the performance buffers, so no debugger permission exists anywhere in the manifest; redaction runs before any entry leaves the page bridge and captured text never carries reviewed secret patterns.\n */\n\n/** The debugging kinds, listed among the available capabilities of every proposal request. */\nexport const timelinekinds: string[] = [\"watchconsole\", \"watcherrors\", \"watchtasks\"];\n\n/** The reviewed level set, ordered from the most severe to the most verbose level. */\nexport const loglevels: loglevel[] = [\"error\", \"warn\", \"info\", \"log\", \"debug\", \"trace\"];\n\n/** The reviewed timeline source grammar: page console output, script errors, promise rejections, resource failures, long tasks, failed network requests and instrumented devtools protocol events. */\nexport const timelinesources: timelinesource[] = [\"console\", \"error\", \"rejection\", \"resource\", \"longtask\", \"network\", \"cdp\"];\n\n/** Ranks one level inside the reviewed level set; lower ranks are more severe. */\nexport function levelrank(level: loglevel): number {\n return loglevels.indexOf(level);\n}\n\n/** Redacts reviewed secret patterns from console text before any entry leaves the page bridge; matched patterns never survive into a stored entry. */\nexport function redactconsoletext(text: string, patterns: string[]): string {\n let redacted = text;\n for (const pattern of patterns) {\n if (!pattern) continue;\n while (redacted.includes(pattern)) redacted = redacted.replace(pattern, \"[redacted]\");\n }\n return redacted;\n}\n\n/** Classifies the kind tag of one console argument for the consoleentry argument kinds. */\nexport function argkind(value: unknown): string {\n if (value === null) return \"null\";\n if (Array.isArray(value)) return \"array\";\n if (value instanceof Error) return \"error\";\n switch (typeof value) {\n case \"string\": return \"string\";\n case \"number\": return \"number\";\n case \"boolean\": return \"boolean\";\n case \"bigint\": return \"bigint\";\n case \"symbol\": return \"symbol\";\n case \"function\": return \"function\";\n case \"undefined\": return \"undefined\";\n default: return \"object\";\n }\n}\n\n/** Serializes one console argument through the reviewed depth bound; deeper objects collapse to their constructor tag so the bound stays a user choice with no code ceiling. */\nexport function serializearg(value: unknown, depth: number): string {\n const render = (item: unknown, remaining: number): string => {\n if (item instanceof Error) return `${item.name}: ${item.message}`;\n if (typeof item === \"string\") return item;\n if (typeof item === \"function\") return `[function ${item.name || \"anonymous\"}]`;\n if (typeof item === \"bigint\") return `${item}n`;\n if (typeof item === \"symbol\") return item.toString();\n if (item === null || item === undefined || typeof item !== \"object\") return String(item);\n if (remaining <= 0) {\n const tag = Array.isArray(item) ? \"Array\" : (item as { constructor?: { name?: string } }).constructor?.name ?? \"Object\";\n return `[${tag}]`;\n }\n if (Array.isArray(item)) return `[${item.map(entry => render(entry, remaining - 1)).join(\", \")}]`;\n const record = item as Record<string, unknown>;\n return `{${Object.keys(record).map(key => `${key}: ${render(record[key], remaining - 1)}`).join(\", \")}}`;\n };\n return render(value, Math.max(0, depth));\n}\n\n/** Builds one captured console entry from a forwarded console call: the level, the redacted text of every serialized argument and the argument kinds, with redaction applied before the entry exists. */\nexport function consolecapture(input: { level: loglevel; args: unknown[]; depth: number; redact: string[] }): consoleentry {\n const parts = input.args.map(arg => serializearg(arg, input.depth));\n return { level: input.level, text: redactconsoletext(parts.join(\" \"), input.redact), argkinds: input.args.map(arg => argkind(arg)), repeat: 1 };\n}\n\n/** Parses one stack trace text into stack frames with function names, urls, lines and columns; unparseable lines are skipped instead of crashing capture. */\nexport function stackframes(stacktext: string): stackframe[] {\n const frames: stackframe[] = [];\n for (const row of stacktext.split(\"\\n\")) {\n const trimmed = row.trim();\n if (!trimmed.startsWith(\"at \")) continue;\n const body = trimmed.slice(3).trim();\n const location = body.match(/\\(([^()]*:\\d+:\\d+)\\)$/) ?? body.match(/^(.*:\\d+:\\d+)$/);\n const located = location?.[1];\n if (!located) continue;\n const segments = located.split(\":\");\n const column = Number.parseInt(segments.pop() ?? \"\", 10);\n const lineno = Number.parseInt(segments.pop() ?? \"\", 10);\n const url = segments.join(\":\");\n if (!Number.isFinite(lineno) || lineno < 0) continue;\n const name = body.endsWith(`(${located})`) ? body.slice(0, body.length - located.length - 2).trim() : \"\";\n frames.push({ ...(name ? { functionname: name } : {}), url, line: lineno, ...(Number.isFinite(column) ? { column } : {}) });\n }\n return frames;\n}\n\n/** Builds one captured javascript error record body from an error event: the redacted message, the parsed stack frames, the source url and the line. */\nexport function errorcapture(input: { message: string; sourceurl: string; line: number; stacktext?: string; redact: string[] }): Pick<errorrecord, \"message\" | \"frames\" | \"sourceurl\" | \"line\"> {\n return { message: redactconsoletext(input.message, input.redact), frames: input.stacktext !== undefined ? stackframes(input.stacktext) : [], sourceurl: input.sourceurl, line: input.line };\n}\n\n/** Builds one captured unhandled rejection record body from the rejection reason text and its stack frames, with the reason redacted before capture. */\nexport function rejectioncapture(input: { reason: string; stacktext?: string; redact: string[] }): Pick<rejectionrecord, \"reason\" | \"frames\"> {\n return { reason: redactconsoletext(input.reason, input.redact), frames: input.stacktext !== undefined ? stackframes(input.stacktext) : [] };\n}\n\n/** Builds the long task entry bodies of one watch window from performance longtask entries with their attribution names; the reviewed threshold filters entries below the user configured duration. */\nexport function longtaskcapture(input: { entries: Array<{ starttime: number; duration: number; attributions: string[] }>; threshold: number }): Array<Pick<longtaskentry, \"duration\" | \"starttime\" | \"attributions\">> {\n return input.entries.filter(entry => entry.duration >= input.threshold).map(entry => ({ duration: Math.round(entry.duration), starttime: Math.round(entry.starttime), attributions: [...entry.attributions] }));\n}\n\n/** One run timeline bound to its run and step ids. */\nexport interface timeline {\n runid: string;\n origin: string;\n stepids: string[];\n attachedat: number;\n entries: timelineentry[];\n}\n\n/** Binds one timeline to the run and its step ids; capture stays scoped to that run and every entry carries the run correlation id of its step. */\nexport function attachtimeline(input: { runid: string; origin: string; stepids: string[]; now: number }): timeline {\n return { runid: input.runid, origin: input.origin, stepids: [...input.stepids], attachedat: input.now, entries: [] };\n}\n\n/** Applies the reviewed level set to timeline entries: per step level floors drop entries more verbose than the floor and source filters drop entries from unreviewed sources. */\nexport function filterentries(entries: timelineentry[], levelset: loglevelset): timelineentry[] {\n return entries.filter(entry => {\n const floor = levelset.floors?.[entry.stepid] ?? levelset.floors?.[\"*\"];\n if (floor !== undefined && levelrank(entry.level) > levelrank(floor)) return false;\n if (levelset.sources !== undefined && levelset.sources.length > 0 && !levelset.sources.includes(entry.source)) return false;\n return true;\n });\n}\n\n/** Collapses repeated identical messages inside the reviewed spam window into counts and flags the patterns that exceed the reviewed collapse threshold. */\nexport function spamdetect(entries: timelineentry[], rule: spamrule): { entries: Array<timelineentry & { repeat: number }>; flagged: Array<{ message: string; count: number }> } {\n const collapsed: Array<timelineentry & { repeat: number }> = [];\n const counts = new Map<string, number>();\n for (const entry of entries) {\n if (rule.pattern !== \"\" && !entry.message.includes(rule.pattern)) { collapsed.push({ ...entry, repeat: 1 }); continue; }\n const key = `${entry.level}|${entry.source}|${entry.message}`;\n const previous = collapsed[collapsed.length - 1];\n if (previous && previous.repeat !== undefined && `${previous.level}|${previous.source}|${previous.message}` === key && entry.time - previous.time <= rule.windowsize) {\n previous.repeat += 1;\n continue;\n }\n collapsed.push({ ...entry, repeat: 1 });\n }\n for (const entry of collapsed) {\n if (entry.repeat > 1) counts.set(`${entry.level}|${entry.source}|${entry.message}`, entry.repeat);\n }\n const flagged = [...counts.entries()].filter(([, count]) => count > rule.collapse).map(([key, count]) => ({ message: key.split(\"|\").slice(2).join(\"|\"), count }));\n return { entries: collapsed, flagged };\n}\n\n/** Rotates the timeline of one run: the newest entries stay inside the reviewed max entries window while every overflow entry moves to the rotation target store without data loss. */\nexport function rotatelogs(entries: timelineentry[], rule: rotationrule): { kept: timelineentry[]; overflow: timelineentry[] } {\n if (entries.length <= rule.maxentries) return { kept: [...entries], overflow: [] };\n const kept = entries.slice(entries.length - rule.maxentries);\n const overflow = entries.slice(0, entries.length - rule.maxentries);\n return { kept, overflow };\n}\n\n/** Counts the timeline entries per level for the response envelope timeline block. */\nexport function timelinecounts(entries: timelineentry[]): Record<string, number> {\n const counts: Record<string, number> = {};\n for (const level of loglevels) counts[level] = 0;\n for (const entry of entries) counts[entry.level] = (counts[entry.level] ?? 0) + 1;\n return counts;\n}\n\n/** Sums the blocking duration of long task entries inside one step window; the window stays a reviewed value with no code ceiling. */\nexport function blockingduration(tasks: Array<{ starttime: number; duration: number }>, stepid: string, window: { startedat: number; endedat: number }): { stepid: string; blocking: number; tasks: number } {\n const inside = tasks.filter(task => task.starttime >= window.startedat && task.starttime <= window.endedat);\n return { stepid, blocking: inside.reduce((total, task) => total + task.duration, 0), tasks: inside.length };\n}\n\n/** Marks one failed request of the run in the timeline from its observed exchange: the url, status, error class and correlation id. */\nexport function netfailureentryof(input: { id: string; exchange: exchangerecord; at: number }): netfailureentry | null {\n const exchange = input.exchange;\n if (exchange.errorclass === undefined && exchange.status < 400) return null;\n return { id: input.id, runid: exchange.runid, stepid: exchange.stepid, url: exchange.url, status: exchange.status, errorclass: exchange.errorclass ?? \"httperror\", correlationid: exchange.correlationid, at: input.at };\n}\n\n/** Decides whether a watcher detached early because the run tab navigated inside its watch window: navigation timestamps inside the window detach the watcher at the navigation time. */\nexport function watcherdetached(input: { startedat: number; lifetime: number; navigations: number[] }): { detached: boolean; at?: number } {\n for (const navigation of input.navigations) {\n if (navigation >= input.startedat && navigation <= input.startedat + input.lifetime) return { detached: true, at: navigation };\n }\n return { detached: false };\n}\n\n/** Compares the console output of two runs and classifies every line as added, removed or repeated; repeated lines carry their repeat counts. */\nexport function consolediff(input: { baseid: string; targetid: string; baselines: string[]; targetlines: string[]; now: number }): consolediff {\n const base = input.baselines;\n const target = input.targetlines;\n const basemap = new Map<string, number>();\n for (const line of base) basemap.set(line, (basemap.get(line) ?? 0) + 1);\n const targetmap = new Map<string, number>();\n for (const line of target) targetmap.set(line, (targetmap.get(line) ?? 0) + 1);\n const lines = [];\n const added: string[] = [];\n const removed: string[] = [];\n const repeated: string[] = [];\n for (const [line, count] of targetmap) {\n const basecount = basemap.get(line) ?? 0;\n if (basecount === 0) {\n for (let index = 0; index < count; index += 1) { lines.push({ kind: \"added\" as const, text: line }); added.push(line); }\n continue;\n }\n const share = Math.min(basecount, count);\n for (let index = 0; index < share; index += 1) { lines.push({ kind: \"repeated\" as const, text: line, count: share }); repeated.push(line); }\n for (let index = share; index < count; index += 1) { lines.push({ kind: \"added\" as const, text: line }); added.push(line); }\n }\n for (const [line, count] of basemap) {\n const targetcount = targetmap.get(line) ?? 0;\n const missing = Math.max(0, count - targetcount);\n for (let index = 0; index < missing; index += 1) { lines.push({ kind: \"removed\" as const, text: line }); removed.push(line); }\n }\n return { base: input.baseid, target: input.targetid, lines, added: added.length, removed: removed.length, repeated: repeated.length, at: input.now };\n}\n", "import type { actionkind, agentbudget, agentidentity, agentmessage, agentplan, agentscope, agentsession, agentusage, allowlistentry, approvaltimeout, arbitrationrule, attachtarget, blackboardentry, callratelimit, captureexport, conflictwriter, costbudget, criticreview, draftstep, escalationrecord, handoffrecord, lockkind, mergerule, modeloutput, plandraft, providerconfig, replanrecord, resourcelock, resultreport, reviewrequest, spawnrequest, swarmaction, taskqueue, captureformat, capturenaming, captureoptions, cdpallowlist, cleanuprule, clientidentity, clientrecord, consoleconsentrecord, debuggergrant, delaystep, downloadspec, editormodel, endpointconfig, fieldkind, formprofile, locationconsent, loglevel, loglevelset, mimefilter, mcpserverconfig, observationmode, permissionstate, policyevaluation, protocoleventsubscription, quarantineentry, regionrect, rotationrule, runsettings, safetyverdict, samplingrequest, siteoverride, sourcemapconsent, spamrule, steptemplate, toolcallrecord, toolcatalog, tooldef, toolmock, toolnamespace, toolstep, tooldryrun, transformrule, verifiercheck, waitstep, watchdogconfig, workerassignment, workflowrecord, workflowstep } from \"./types.js\";\nimport { domainkinds, toolnamespaces } from \"./toolcatalog.js\";\nimport { channeloptionsof, channelorigin, pollcursorof, subscriptionoptionsof } from \"./socketbus.js\";\nimport { apireplayspecof, privatemime } from \"./netwatch.js\";\nimport { blockruleof, cookiedomaingranted, cookierecordof, mockspecof, patternorigin, proxyrouteof, headeruleof } from \"./netcontrol.js\";\nimport { allowlistcovers, breakpointinputof, cdpallowlistof, cdpdomains, cdpeventruleof, methoddomain, overrideinputof, stepmodeof, teardownplanof, watchexpressionof } from \"./cdpbus.js\";\nimport { annotationof, attachtargetof, flowspecof, tracecategories } from \"./profilers.js\";\nimport { agentgrammarvalid, agentpresetof, blackboxruleof, browserpermissions, devicepresetof, familyofkind, locationconsentcovers, locationpresetof, locationrangevalid, networkpresetof, permissiongrantof, permissiongrade, permissionstates, revertplanof } from \"./emulation.js\";\nimport { autointervalof, importsessionfile, restoreplanof, searchqueryof, sessionkinds, snapshotplanof } from \"./sessions.js\";\nimport { composeworkflow, expressionof, expressionoperators, regexruleof, steptemplateof, validateworkflow, workflowblockof, workflowstepof } from \"./workflow.js\";\nimport { branchof, conditionof, controlsteps, foreachof, iscontrolflowkind, loopof, parallelof, repeatuntilof, tryof, whileof } from \"./controlflow.js\";\nimport { armrule, cronparse, triggerfamilyof, triggerpayloadof, triggereventcatalog, webhooksecretok } from \"./trigger.js\";\nimport { formpayloadof, multipartpayloadof, oauthflowof } from \"./netauth.js\";\nimport { loglevels, timelinesources } from \"./runtimeline.js\";\n\nconst sensitiveactions = new Set<actionkind>([\"click\", \"type\", \"navigate\", \"select\", \"presskey\", \"drag\", \"drop\", \"upload\", \"clear\", \"check\", \"uncheck\", \"toggle\", \"submit\", \"reload\", \"back\", \"forward\", \"writestorage\", \"setattribute\", \"removeattribute\", \"evaluate\", \"tabcreate\", \"tabactivate\", \"tabclose\", \"tabreload\", \"windowcreate\", \"windowclose\", \"windowresize\", \"downloadfile\", \"clickpoint\", \"shiftclick\", \"dismissdialog\", \"enterframe\", \"typetime\", \"appendtext\", \"setvalue\", \"typeedit\", \"keyhold\", \"keyrelease\", \"submitsearch\", \"selectmulti\", \"chooseradio\", \"setslider\", \"setdate\", \"setcolor\", \"openlink\", \"openprivate\", \"reloadcache\", \"stopnav\", \"followlink\", \"spanav\", \"rewritequery\", \"setfragment\", \"navlist\", \"navprofile\", \"handleauth\", \"printpdf\", \"prefetch\", \"preconnect\", \"deeplink\", \"reopentab\", \"pausenav\", \"navrate\", \"openclipboard\", \"batchopen\", \"duplicatetab\", \"closepattern\", \"pintab\", \"mutetab\", \"movetab\", \"movetabwindow\", \"grouptabs\", \"colorgroup\", \"collapsegroup\", \"discardtab\", \"reloadtabs\", \"zoomin\", \"zoomout\", \"switchtab\", \"maximizewindow\", \"minimizewindow\", \"restorewindow\", \"focuswindow\", \"scratchwindow\", \"incognitowindow\", \"restoretab\", \"restorelayout\", \"reopenrun\", \"badgetab\", \"fillform\", \"filllabel\", \"fillplaceholder\", \"submitform\", \"retryform\", \"runwizard\", \"selectchain\", \"picktypeahead\", \"pickdate\", \"attachfile\", \"fillcard\", \"fillcode\", \"consentpassword\", \"exportcsv\", \"exportjson\", \"exportexcel\", \"copytable\", \"pushsheets\", \"streamdisk\", \"paginateextract\", \"resumeextract\", \"batchdownload\", \"pausedownload\", \"resumedownload\", \"interceptmime\", \"readclipboard\", \"writeclipboard\", \"copyscreen\", \"quarantinedownload\", \"scanvirus\", \"cleanupartifacts\", \"recordscreen\", \"captureaudio\", \"downloadimages\", \"callrest\", \"callgraphql\", \"sendmessage\", \"blockrequest\", \"mockresponse\", \"rewriteheaders\", \"setcookies\", \"clearcookies\", \"authflow\", \"saveapikey\", \"routeproxy\", \"postform\", \"postfiles\", \"attachcdp\", \"detachcdp\", \"cdpcmd\", \"overridescript\", \"heapshot\", \"profilecpu\", \"capturesourcemaps\", \"emulatedevice\", \"emulatenetwork\", \"emulatelocate\", \"setuseragent\", \"overridepermission\", \"restoresession\", \"exportsessions\", \"importsessions\", \"runworkflow\", \"visitrule\", \"urlrule\", \"menurule\", \"keyrule\", \"buttonrule\", \"cronrule\", \"intervalrule\", \"urllistrule\", \"webhookrule\", \"eventrule\"]);\nconst interactionactions = new Set<actionkind>([\"focus\", \"scroll\", \"hover\", \"clickdeep\", \"rightclick\", \"doubleclick\", \"scrollpage\", \"scrollby\", \"scrollend\", \"scrolltop\", \"fullscreen\", \"zoomset\", \"movepointer\", \"clicktext\", \"clickaria\", \"clickname\", \"expanddetails\", \"pierceshadow\", \"retryaction\", \"capturebodies\", \"setbreakpoint\", \"stepcode\", \"watchexpr\", \"loop\", \"repeatuntil\", \"whileloop\", \"foreach\", \"parallel\", \"trycatch\"]);\nconst readactions = new Set<actionkind>([\"observe\", \"inspect\", \"extract\", \"wait\", \"waitfor\", \"waittext\", \"readattribute\", \"readstyle\", \"readgeometry\", \"readvalue\", \"readtext\", \"readhtml\", \"countelements\", \"readtable\", \"readlinks\", \"readimages\", \"readmeta\", \"readforms\", \"readstorage\", \"highlight\", \"tablist\", \"windowlist\", \"tabsnapshot\", \"mapclicks\", \"verifyvisible\", \"verifyenabled\", \"resolvexpath\", \"a11ytree\", \"readvisible\", \"readertree\", \"detectlists\", \"detecttables\", \"readjson\", \"watchmutate\", \"waitquiet\", \"watchbanner\", \"detectinfinitescroll\", \"detectvirtual\", \"detectlazy\", \"readscrollpos\", \"readlang\", \"readoutline\", \"countpages\", \"listshadow\", \"listframes\", \"classifypage\", \"fingerprintsection\", \"diffsnapshots\", \"readselection\", \"watchfocus\", \"detectsticky\", \"detectscrolllock\", \"readopengraph\", \"detectlanguage\", \"deriveselector\", \"waitload\", \"waiturl\", \"spawait\", \"detecthttp\", \"readredirects\", \"readfinalurl\", \"trailaudit\", \"navintent\", \"checksafe\", \"querytabs\", \"watchtab\", \"findclones\", \"searchtabs\", \"listaudio\", \"snapshotsession\", \"savelayout\", \"attachmeta\", \"detectfields\", \"generatevalues\", \"saveprofiles\", \"asksubmit\", \"readerrors\", \"skiphoneypot\", \"detectlogin\", \"detecttemplate\", \"handoffcaptcha\", \"scrapetable\", \"importcsv\", \"looprows\", \"transformvalues\", \"deduperows\", \"mergepages\", \"stamplerows\", \"previewgrid\", \"logprovenance\", \"verifydownload\", \"exportnetlog\", \"namecaptures\", \"shotview\", \"shotfullpage\", \"shotelement\", \"shotregion\", \"contactsheet\", \"capturepdf\", \"captureframe\", \"readmedia\", \"readassets\", \"probestream\", \"timelapse\", \"shotcanvas\", \"convertimage\", \"makethumbs\", \"fetchurl\", \"parsejson\", \"parsehtml\", \"opensocket\", \"waitmessage\", \"watchrequests\", \"readheaders\", \"mapapi\", \"subscribesse\", \"longpoll\", \"extractapi\", \"readcookies\", \"watchconsole\", \"watcherrors\", \"watchtasks\", \"watchcdp\", \"measureflow\", \"trackmemory\", \"watchshifts\", \"traceload\", \"annotatetrace\", \"replaytrace\", \"blackboxscripts\", \"persiststate\", \"capturesession\", \"namedsessions\", \"diffsessions\", \"searchsessions\", \"composeworkflow\", \"savetemplate\", \"dryrun\", \"delay\", \"waitelement\", \"compute\", \"extractvars\", \"listruns\", \"condition\", \"branch\"]);\nconst allowedactions = new Set<actionkind>([...sensitiveactions, ...interactionactions, ...readactions]);\nconst watchactions = new Set<actionkind>([\"watchmutate\", \"watchbanner\", \"watchfocus\", \"watchtab\"]);\nconst targetactions = new Set<actionkind>([\"inspect\", \"focus\", \"click\", \"type\", \"scroll\", \"select\", \"hover\", \"clickdeep\", \"rightclick\", \"doubleclick\", \"drag\", \"drop\", \"upload\", \"clear\", \"check\", \"uncheck\", \"toggle\", \"submit\", \"readattribute\", \"readstyle\", \"readgeometry\", \"readvalue\", \"readtext\", \"readhtml\", \"countelements\", \"readtable\", \"highlight\", \"setattribute\", \"removeattribute\", \"waitfor\", \"shiftclick\", \"typetime\", \"appendtext\", \"setvalue\", \"typeedit\", \"submitsearch\", \"selectmulti\", \"chooseradio\", \"setslider\", \"setdate\", \"setcolor\", \"expanddetails\", \"verifyvisible\", \"verifyenabled\", \"pierceshadow\", \"deriveselector\", \"fingerprintsection\", \"submitform\", \"retryform\", \"selectchain\", \"picktypeahead\", \"pickdate\", \"attachfile\", \"fillcode\", \"consentpassword\", \"scrapetable\", \"paginateextract\", \"shotelement\", \"captureframe\", \"shotcanvas\"]);\nconst valueactions = new Set<actionkind>([\"presskey\", \"drag\", \"drop\", \"upload\", \"readattribute\", \"removeattribute\", \"waittext\", \"evaluate\", \"zoomset\", \"tabactivate\", \"tabclose\", \"tabreload\", \"windowclose\", \"windowresize\", \"tabcreate\", \"windowcreate\", \"downloadfile\", \"typetime\", \"appendtext\", \"setvalue\", \"typeedit\", \"keyhold\", \"keyrelease\", \"chooseradio\", \"setslider\", \"setdate\", \"setcolor\", \"followlink\", \"setfragment\", \"handleauth\", \"navintent\", \"openclipboard\", \"checksafe\", \"reopentab\", \"spanav\", \"duplicatetab\", \"pintab\", \"mutetab\", \"movetab\", \"movetabwindow\", \"searchtabs\", \"badgetab\", \"attachmeta\", \"focuswindow\", \"maximizewindow\", \"minimizewindow\", \"restorewindow\", \"incognitowindow\", \"asksubmit\", \"selectchain\", \"picktypeahead\", \"pickdate\", \"attachfile\", \"fillcode\", \"consentpassword\", \"pausedownload\", \"resumedownload\", \"verifydownload\", \"writeclipboard\", \"quarantinedownload\", \"scanvirus\"]);\nconst tabscommandactions = new Set<actionkind>([\"querytabs\", \"duplicatetab\", \"closepattern\", \"pintab\", \"mutetab\", \"movetab\", \"movetabwindow\", \"grouptabs\", \"colorgroup\", \"collapsegroup\", \"discardtab\", \"reloadtabs\", \"zoomin\", \"zoomout\", \"watchtab\", \"switchtab\", \"maximizewindow\", \"minimizewindow\", \"restorewindow\", \"focuswindow\", \"scratchwindow\", \"incognitowindow\", \"restoretab\", \"savelayout\", \"restorelayout\", \"findclones\", \"searchtabs\", \"badgetab\", \"attachmeta\", \"listaudio\", \"reopenrun\", \"snapshotsession\"]);\nconst formactions = new Set<actionkind>([\"fillform\", \"filllabel\", \"fillplaceholder\", \"detectfields\", \"generatevalues\", \"saveprofiles\", \"asksubmit\", \"submitform\", \"readerrors\", \"retryform\", \"runwizard\", \"selectchain\", \"picktypeahead\", \"pickdate\", \"attachfile\", \"handoffcaptcha\", \"fillcard\", \"fillcode\", \"consentpassword\", \"skiphoneypot\", \"detectlogin\", \"detecttemplate\"]);\n/** Extraction, transform, export and provenance kinds of the forms and data part two family. */\nconst datasetactions = new Set<actionkind>([\"scrapetable\", \"exportcsv\", \"exportjson\", \"exportexcel\", \"copytable\", \"pushsheets\", \"importcsv\", \"looprows\", \"transformvalues\", \"deduperows\", \"paginateextract\", \"mergepages\", \"stamplerows\", \"previewgrid\", \"streamdisk\", \"resumeextract\", \"logprovenance\"]);\n/** Export kinds that move extracted data out of local memory to disk, the clipboard or a reviewed sheet endpoint. */\nconst exportactions = new Set<actionkind>([\"exportcsv\", \"exportjson\", \"exportexcel\", \"copytable\", \"pushsheets\", \"streamdisk\"]);\n/** Files, clipboard and downloads kinds of the batch queue, interception, clipboard, quarantine, naming and cleanup family. */\nconst filesactions = new Set<actionkind>([\"batchdownload\", \"pausedownload\", \"resumedownload\", \"verifydownload\", \"interceptmime\", \"exportnetlog\", \"readclipboard\", \"writeclipboard\", \"copyscreen\", \"quarantinedownload\", \"scanvirus\", \"namecaptures\", \"cleanupartifacts\"]);\nconst captureactions = new Set<actionkind>([\"shotview\", \"shotfullpage\", \"shotelement\", \"shotregion\", \"contactsheet\"]);\n/** Media capture part two kinds of the pdf, recording, image, canvas, stream, asset, lapse, conversion and thumbnail family. */\nconst mediaactions = new Set<actionkind>([\"capturepdf\", \"recordscreen\", \"captureaudio\", \"captureframe\", \"downloadimages\", \"shotcanvas\", \"probestream\", \"readmedia\", \"readassets\", \"timelapse\", \"convertimage\", \"makethumbs\"]);\n\nconst httpactions = new Set<actionkind>([\"fetchurl\", \"parsejson\", \"parsehtml\", \"callrest\", \"callgraphql\"]);\n\nconst socketactions = new Set<actionkind>([\"opensocket\", \"sendmessage\", \"waitmessage\", \"subscribesse\", \"longpoll\"]);\n\nconst netwatchactions = new Set<actionkind>([\"watchrequests\", \"readheaders\", \"capturebodies\", \"mapapi\", \"extractapi\"]);\n\n/** Network control kinds of the 1.1.44 family: blocking, mocking, header rewriting, cookies, auth, api keys, proxy routing and uploads. */\nconst controlactions = new Set<actionkind>([\"blockrequest\", \"mockresponse\", \"rewriteheaders\", \"setcookies\", \"readcookies\", \"clearcookies\", \"authflow\", \"saveapikey\", \"routeproxy\", \"postform\", \"postfiles\"]);\n\n/** Debugging kinds of the 1.1.45 family: console, error and task watching stays read only timeline capture. */\nconst debugactions = new Set<actionkind>([\"watchconsole\", \"watcherrors\", \"watchtasks\"]);\n\n/** The devtools protocol kinds of the 1.1.46 debugging family: attach, detach, raw commands, event watches, breakpoints, stepping, watch expressions and script overrides. */\nconst cdpactions = new Set<actionkind>([\"attachcdp\", \"detachcdp\", \"cdpcmd\", \"watchcdp\", \"setbreakpoint\", \"stepcode\", \"watchexpr\", \"overridescript\"]);\n\n/** The profiling kinds of the 1.1.47 debugging part three family: flow measurement, heap snapshots, memory growth tracking, cpu profiles, layout shift watches, trace records, trace annotation, offline trace replay and source map capture. */\nconst profileractions = new Set<actionkind>([\"measureflow\", \"heapshot\", \"trackmemory\", \"profilecpu\", \"watchshifts\", \"traceload\", \"annotatetrace\", \"replaytrace\", \"capturesourcemaps\"]);\n\nconst emulationactions = new Set<actionkind>([\"emulatedevice\", \"emulatenetwork\", \"emulatelocate\", \"setuseragent\", \"overridepermission\", \"blackboxscripts\"]);\n\n/** The session memory kinds of the 1.1.49 family: task state persistence, session capture, restore, naming, diffing, search, export and import. */\nconst sessionactions = new Set<actionkind>([\"persiststate\", \"capturesession\", \"restoresession\", \"namedsessions\", \"diffsessions\", \"searchsessions\", \"exportsessions\", \"importsessions\"]);\n\n/** The workflow kinds of the 1.1.50 and 1.1.51 families: composition, templates, runs, dry runs, jittered delays, element waits, expressions, variable extraction, and the control flow family of conditionals, branching, loops, parallel branches with joins and try catch with retries and timeouts. */\nconst workflowactions = new Set<actionkind>([\"composeworkflow\", \"savetemplate\", \"runworkflow\", \"dryrun\", \"delay\", \"waitelement\", \"compute\", \"extractvars\", \"condition\", \"branch\", \"loop\", \"repeatuntil\", \"whileloop\", \"foreach\", \"parallel\", \"trycatch\"]);\n\n/** The trigger kinds of the 1.1.52 family: page visit, url pattern, context menu, keyboard shortcut, toolbar button, cron, interval, url list, webhook and page event rules that launch reviewed workflows; every rule arms behind the explicit arm review and grades sensitive because it launches runs automatically. */\nconst triggeractions = new Set<actionkind>([\"visitrule\", \"urlrule\", \"menurule\", \"keyrule\", \"buttonrule\", \"cronrule\", \"intervalrule\", \"urllistrule\", \"webhookrule\", \"eventrule\"]);\n\n/** Header names that carry credentials; sending any of them needs the explicit consent that names the header. */\nconst credentialheaders = new Set([\"authorization\", \"proxy-authorization\", \"cookie\", \"cookie2\", \"set-cookie\", \"api-key\", \"x-api-key\", \"x-auth-token\", \"x-session-token\", \"proxy-authorization\"]);\n/** Field kinds the form grammar accepts inside records, profiles and value rules. */\nconst fieldkinds: fieldkind[] = [\"text\", \"email\", \"phone\", \"date\", \"number\", \"select\", \"check\", \"radio\", \"file\", \"password\", \"card\", \"code\"];\nconst layoutmutationactions = new Set<actionkind>([\"grouptabs\", \"colorgroup\", \"collapsegroup\", \"savelayout\", \"restorelayout\"]);\n/** Chromium tab group colors accepted as reviewed group color choices. */\nconst groupcolors = [\"grey\", \"blue\", \"red\", \"yellow\", \"green\", \"pink\", \"purple\", \"cyan\", \"orange\"];\n\n/** Normalizes a user supplied HTTPS endpoint without preserving a provider lock-in. */\nexport function normalizeendpoint(value: string): endpointconfig {\n const endpoint = new URL(value.trim());\n if (endpoint.protocol !== \"https:\") throw new Error(\"Devthink accepts HTTPS endpoints only.\");\n if (endpoint.username || endpoint.password) throw new Error(\"Endpoint credentials are not allowed in the URL.\");\n return { endpoint: endpoint.toString(), origin: endpoint.origin, configuredat: Date.now() };\n}\n\n/** Creates the exact optional host pattern requested from Chromium. */\nexport function hostpattern(origin: string): string {\n const parsed = new URL(origin);\n if (parsed.protocol !== \"https:\") throw new Error(\"Only HTTPS origins can be granted.\");\n return `${parsed.origin}/*`;\n}\n\n/** True when the kind belongs to the session memory family of the 1.1.49 release. */\nexport function issessionkind(kind: actionkind): boolean {\n return sessionactions.has(kind);\n}\n\n/** True when the kind belongs to the workflow family of the 1.1.50 release. */\nexport function isworkflowkind(kind: actionkind): boolean {\n return workflowactions.has(kind);\n}\n\n/** True when the kind belongs to the trigger family of the 1.1.52 release: every trigger kind arms an automatic launcher and needs the explicit arm review. */\nexport function istriggeraction(kind: actionkind): boolean {\n return triggeractions.has(kind);\n}\n\n/** True when the action kind observes the page over a reviewed lifetime window. */\nexport function iswatchkind(kind: actionkind): boolean {\n return watchactions.has(kind);\n}\n\n/** True when the kind belongs to the debugging family of console, error and task watching. */\nexport function isdebugkind(kind: actionkind): boolean {\n return debugactions.has(kind);\n}\n\n/** True when the kind belongs to the devtools protocol family of attaches, raw commands, event watches, breakpoints, stepping, watch expressions and script overrides. */\nexport function iscdpkind(kind: actionkind): boolean {\n return cdpactions.has(kind);\n}\n\n/** True when the kind belongs to the profiling family of flow, heap, cpu, shift, trace and source map instruments. */\nexport function isprofilekind(kind: actionkind): boolean {\n return profileractions.has(kind);\n}\n\n/** True when the kind belongs to the emulation family of device, network, location, agent and permission layers plus blackbox trace shaping. */\nexport function isemulationkind(kind: actionkind): boolean {\n return emulationactions.has(kind);\n}\n\n/** Grades the observation mode of a kind: passive capture, watched lifetimes or diffing passes. */\nexport function observationmodeof(kind: actionkind): observationmode {\n if (watchactions.has(kind) || debugactions.has(kind) || profileractions.has(kind) && kind !== \"heapshot\" && kind !== \"replaytrace\" && kind !== \"annotatetrace\" && kind !== \"capturesourcemaps\" && kind !== \"profilecpu\" || cdpactions.has(kind) && kind === \"watchcdp\" || kind === \"waitquiet\") return \"watching\";\n if (kind === \"diffsnapshots\") return \"diffing\";\n return \"passive\";\n}\n\n/** Defines action risk from the fixed local allowlist. */\nexport function actionrisk(kind: actionkind): \"read\" | \"interaction\" | \"sensitive\" {\n if (!allowedactions.has(kind)) throw new Error(\"Unsupported browser action.\");\n if (sensitiveactions.has(kind)) return \"sensitive\";\n return interactionactions.has(kind) ? \"interaction\" : \"read\";\n}\n\n/** True when the action kind accepts a css selector target or a reviewed targetref. */\nexport function needstarget(kind: actionkind): boolean {\n return targetactions.has(kind);\n}\n\n/** Parses the reviewed JSON options of a step; malformed payloads are rejected early. */\nexport function parseoptions(step: toolstep): Record<string, unknown> {\n if (step.options === undefined) return {};\n let parsed: unknown;\n try { parsed = JSON.parse(step.options); } catch { throw new Error(\"Step options must be a JSON object.\"); }\n if (!parsed || typeof parsed !== \"object\" || Array.isArray(parsed)) throw new Error(\"Step options must be a JSON object.\");\n return parsed as Record<string, unknown>;\n}\n\n/** Maps an action kind to the optional browser permission it requires, if any. */\nexport function requiredcapability(kind: actionkind): string | undefined {\n if (kind === \"tablist\") return \"tabs\";\n if (kind === \"downloadfile\") return \"downloads\";\n if (kind === \"openclipboard\") return \"clipboardRead\";\n if (kind === \"copytable\") return \"clipboardWrite\";\n if (kind === \"batchdownload\" || kind === \"pausedownload\" || kind === \"resumedownload\" || kind === \"verifydownload\" || kind === \"interceptmime\" || kind === \"quarantinedownload\" || kind === \"scanvirus\") return \"downloads\";\n if (kind === \"readclipboard\") return \"clipboardRead\";\n if (kind === \"writeclipboard\" || kind === \"copyscreen\") return \"clipboardWrite\";\n if (kind === \"downloadimages\") return \"downloads\";\n if (kind === \"authflow\") return \"tabs\";\n if (kind === \"capturesession\" || kind === \"restoresession\") return \"tabs\";\n if (kind === \"exportsessions\") return \"downloads\";\n if (kind === \"openlink\" || kind === \"openprivate\" || kind === \"navlist\" || kind === \"batchopen\" || kind === \"reopentab\" || kind === \"deeplink\") return \"tabs\";\n if (tabscommandactions.has(kind)) return \"tabs\";\n return undefined;\n}\n\n/** True when the kind commands tabs or windows beyond the active tab and needs the optional tabs capability. */\nexport function istabscommandkind(kind: actionkind): boolean {\n return tabscommandactions.has(kind);\n}\n\n/** True when the kind mutates tab groups or layouts and therefore stays inside the active session. */\nexport function islayoutkind(kind: actionkind): boolean {\n return layoutmutationactions.has(kind);\n}\n\n/** True when the kind belongs to the forms and data family. */\nexport function isformkind(kind: actionkind): boolean {\n return formactions.has(kind);\n}\n\n/** True when the kind belongs to the extraction, transform, export and provenance family. */\nexport function isdatasetkind(kind: actionkind): boolean {\n return datasetactions.has(kind);\n}\n\n/** True when the kind exports extracted data out of local memory to disk, the clipboard or a reviewed sheet endpoint. */\nexport function isexportkind(kind: actionkind): boolean {\n return exportactions.has(kind);\n}\n\n/** True when the kind belongs to the files, clipboard and downloads family. */\nexport function isfileskind(kind: actionkind): boolean {\n return filesactions.has(kind);\n}\n\n/** True when the kind belongs to the media capture family of viewport, full page, element, region and contact sheet shots. */\nexport function iscapturekind(kind: actionkind): boolean {\n return captureactions.has(kind);\n}\n\n/** Requires the active tab grant of the live session before any capture kind runs: the session tab and origin must match and the origin grant must cover the active origin. */\nexport function capturegate(session: agentsession | undefined, tabid: number, origin: string, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the capture.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot capture.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot capture.\" };\n if (session.tabid !== tabid) return { allowed: false, reason: `The capture needs the active tab grant of session tab ${session.tabid} and refuses tab ${tabid}.` };\n if (!origingranted(session, origin)) return { allowed: false, reason: `The capture of ${origin} needs the session origin grants first.` };\n return { allowed: true };\n}\n\n/** Validates one reviewed capture options payload: format inside the png, jpeg and webp set, quality bounded only by the format range, pixel ratio from one up with no code ceiling, and a known export target. */\nexport function validatecaptureoptions(value: unknown): policyevaluation {\n if (value === undefined) return { allowed: true };\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"The reviewed capture options must be an object in options.capture.\" };\n const options = value as Record<string, unknown>;\n if (options.format !== undefined && options.format !== \"png\" && options.format !== \"jpeg\" && options.format !== \"webp\") return { allowed: false, reason: \"The reviewed capture format must be png, jpeg or webp.\" };\n if (options.quality !== undefined && (typeof options.quality !== \"number\" || !Number.isFinite(options.quality) || options.quality < 0 || options.quality > 100)) return { allowed: false, reason: \"The reviewed capture quality must stay between zero and one hundred; any value in that range is the user choice with no code cap.\" };\n if (options.pixelratio !== undefined && (typeof options.pixelratio !== \"number\" || !Number.isFinite(options.pixelratio) || options.pixelratio < 1)) return { allowed: false, reason: \"The reviewed pixel ratio starts at one and climbs to any user configured ceiling with no code ceiling.\" };\n if (options.annotate !== undefined && typeof options.annotate !== \"boolean\") return { allowed: false, reason: \"The reviewed capture annotation flag must be a boolean.\" };\n if (options.exporttarget !== undefined && options.exporttarget !== \"memory\" && options.exporttarget !== \"download\" && options.exporttarget !== \"clipboard\") return { allowed: false, reason: \"The reviewed capture export target must be memory, download or clipboard.\" };\n return { allowed: true };\n}\n\n/** Validates one reviewed region rectangle in css pixels; negative coordinates and non positive sizes are refused. */\nexport function validateregionrect(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed regionrect with x, y, width and height in css pixels is required in options.\" };\n const rect = value as Record<string, unknown>;\n for (const field of [\"x\", \"y\", \"width\", \"height\"]) {\n if (typeof rect[field] !== \"number\" || !Number.isFinite(rect[field] as number)) return { allowed: false, reason: `The reviewed regionrect needs a numeric ${field} in css pixels.` };\n }\n if ((rect.x as number) < 0 || (rect.y as number) < 0) return { allowed: false, reason: \"The reviewed regionrect refuses negative coordinates.\" };\n if ((rect.width as number) <= 0 || (rect.height as number) <= 0) return { allowed: false, reason: \"The reviewed regionrect needs positive width and height values.\" };\n return { allowed: true };\n}\n\n/** Validates one reviewed capture naming rule against the allowed segment set: run, step, sequence and kind flags only. */\nexport function validatecapturenaming(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed capturenaming rule with run, step, sequence and kind flags is required.\" };\n const rule = value as Record<string, unknown>;\n const segments = [\"run\", \"step\", \"sequence\", \"kind\"];\n for (const key of Object.keys(rule)) {\n if (!segments.includes(key)) return { allowed: false, reason: `The reviewed capturenaming rule refuses the unknown ${key} segment; only run, step, sequence and kind participate.` };\n }\n for (const segment of segments) {\n if (rule[segment] !== undefined && typeof rule[segment] !== \"boolean\") return { allowed: false, reason: `The reviewed capturenaming ${segment} flag must be a boolean.` };\n }\n if (!segments.some(segment => rule[segment] === true)) return { allowed: false, reason: \"The reviewed capturenaming rule needs at least one enabled segment of run, step, sequence and kind.\" };\n return { allowed: true };\n}\n\n/** Routes the capture export target: memory stays local, clipboard needs the clipboardwrite grant and disk writes only run through the reviewed download flow. */\nexport function captureexportgranted(target: captureexport | undefined): policyevaluation {\n if (target === undefined || target === \"memory\") return { allowed: true };\n if (target === \"clipboard\") return { allowed: true, reason: \"The clipboard capture export runs behind the optional clipboardwrite capability, negotiated through the permissions api before the copy.\" };\n if (target === \"download\") return { allowed: true, reason: \"The download capture export runs only through the reviewed download flow behind the optional downloads capability.\" };\n return { allowed: false, reason: \"The capture export target must be memory, download or clipboard; no other disk route exists.\" };\n}\n\n/** Keeps the stitching scroll budget inside the reviewed wait window: the settle windows of every tile must fit the reviewed wait window with no code ceiling on either side. */\nexport function stitchbudgetallowed(tiles: number, settle: number, wait: number): policyevaluation {\n if (tiles <= 0) return { allowed: false, reason: \"The stitch budget needs at least one tile.\" };\n if (settle < 0 || wait < 0) return { allowed: false, reason: \"The reviewed settle and wait windows must be zero or positive milliseconds.\" };\n if (tiles * settle > wait) return { allowed: false, reason: `The stitching scroll budget of ${tiles} tiles at ${settle} milliseconds exceeds the reviewed wait window of ${wait} milliseconds; review a wider window or a smaller settle.` };\n return { allowed: true };\n}\n\n/** Allows beforeafter state capture to wrap any existing action kind except the capture kinds themselves; pixel evidence around sensitive actions grades as reviewable evidence. */\nexport function beforeafterwrapallowed(kind: actionkind): boolean {\n return allowedactions.has(kind) && !captureactions.has(kind);\n}\n\n/** Exposes the capture retention window as a user configured choice; an absent value keeps every capture byte forever with no code ceiling. */\nexport function captureretentionwindow(settings: runsettings | undefined): number | undefined {\n return settings?.captureretention;\n}\n\n/** Validates the reviewed media capture parameter grammar; pixel ratios, quality values, cell counts and retention windows stay user choices with no code ceilings. */\nfunction validatecapturegrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n const optioncheck = validatecaptureoptions(options.capture);\n if (!optioncheck.allowed) return optioncheck;\n if (options.settle !== undefined && (typeof options.settle !== \"number\" || !Number.isFinite(options.settle) || options.settle < 0)) return { allowed: false, reason: \"The reviewed capture settle window must be zero or a positive number of milliseconds.\" };\n if (options.overlap !== undefined && (typeof options.overlap !== \"number\" || !Number.isInteger(options.overlap) || options.overlap < 0)) return { allowed: false, reason: \"The reviewed stitch overlap must be zero or a positive number of rows.\" };\n if (options.wait !== undefined && (typeof options.wait !== \"number\" || !Number.isFinite(options.wait) || options.wait < 0)) return { allowed: false, reason: \"The reviewed capture wait window must be zero or a positive number of milliseconds.\" };\n if (options.naming !== undefined) {\n const namingcheck = validatecapturenaming(options.naming);\n if (!namingcheck.allowed) return namingcheck;\n }\n if (kind === \"shotregion\") {\n const rectcheck = validateregionrect(options.regionrect);\n if (!rectcheck.allowed) return rectcheck;\n if (options.reviewed !== true) return { allowed: false, reason: \"Every reviewed regionrect needs the explicit reviewed flag before shotregion runs.\" };\n if (options.container !== undefined && !isnonempty(options.container)) return { allowed: false, reason: \"The reviewed scrollable container selector must be a non-empty string.\" };\n if (options.steps !== undefined && (typeof options.steps !== \"number\" || !Number.isInteger(options.steps) || options.steps < 1)) return { allowed: false, reason: \"The reviewed container scroll steps must be a positive integer with no code ceiling.\" };\n }\n if (kind === \"contactsheet\") {\n const elements = options.elements;\n if (!Array.isArray(elements) || elements.length === 0 || !elements.every(item => isnonempty(item))) return { allowed: false, reason: \"A reviewed non-empty list of element selectors is required in options for the contact sheet; the cell count stays the user choice.\" };\n const layout = options.sheet;\n if (layout !== undefined) {\n if (!layout || typeof layout !== \"object\" || Array.isArray(layout)) return { allowed: false, reason: \"The reviewed sheetlayout must be an object with cellsize, columns and label.\" };\n const sheet = layout as Record<string, unknown>;\n if (typeof sheet.cellsize !== \"number\" || !Number.isFinite(sheet.cellsize) || sheet.cellsize <= 0) return { allowed: false, reason: \"The reviewed contact sheet cell size must be a positive number of pixels.\" };\n if (typeof sheet.columns !== \"number\" || !Number.isInteger(sheet.columns) || sheet.columns < 1) return { allowed: false, reason: \"The reviewed contact sheet column count must be a positive integer with no code ceiling.\" };\n if (sheet.label !== undefined && sheet.label !== \"none\" && sheet.label !== \"index\" && sheet.label !== \"selector\" && sheet.label !== \"both\") return { allowed: false, reason: \"The reviewed contact sheet label style must be none, index, selector or both.\" };\n }\n }\n return { allowed: true };\n}\n\n/** Refuses any export that would leave local memory while the session origin grants do not cover the active origin. */\nexport function exportgranted(session: agentsession | undefined, origin: string): policyevaluation {\n if (!origingranted(session, origin)) return { allowed: false, reason: `The export of extracted data from ${origin} needs the session origin grants before it leaves local memory.` };\n return { allowed: true };\n}\n\n/** Validates the reviewed fieldmatch grammar of one form field entry. */\nexport function validatefieldmatch(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed field match is required in options.\" };\n const match = value as Record<string, unknown>;\n if (match.mode !== \"label\" && match.mode !== \"placeholder\" && match.mode !== \"arialabel\" && match.mode !== \"name\") return { allowed: false, reason: \"The reviewed field match mode must be label, placeholder, arialabel or name.\" };\n const key = match.mode === \"label\" ? \"label\" : match.mode === \"placeholder\" ? \"placeholder\" : match.mode === \"arialabel\" ? \"arialabel\" : \"name\";\n if (!isnonempty(match[key])) return { allowed: false, reason: `The reviewed ${match.mode} field match needs a non-empty ${key}.` };\n return { allowed: true };\n}\n\n/** Validates a reviewed structured form record; password entries are refused because passwords need the explicit consentpassword consent. */\nexport function validateformrecord(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed form record with entries is required in options.\" };\n const record = value as Record<string, unknown>;\n if (record.form !== undefined && !isnonempty(record.form)) return { allowed: false, reason: \"The reviewed form record form selector must be a non-empty string.\" };\n if (!Array.isArray(record.entries) || record.entries.length === 0) return { allowed: false, reason: \"The reviewed form record needs a non-empty list of entries.\" };\n for (const item of record.entries) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) return { allowed: false, reason: \"Every reviewed form record entry must be an object.\" };\n const entry = item as Record<string, unknown>;\n const matchcheck = validatefieldmatch(entry.match);\n if (!matchcheck.allowed) return matchcheck;\n if (typeof entry.kind !== \"string\" || !fieldkinds.includes(entry.kind as fieldkind)) return { allowed: false, reason: \"Every reviewed form record entry needs a known field kind.\" };\n if (typeof entry.value !== \"string\") return { allowed: false, reason: \"Every reviewed form record entry needs a string value.\" };\n if (entry.kind === \"password\") return { allowed: false, reason: \"Password entries are refused inside form records; use consentpassword with a reviewed consent ref.\" };\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed valuegen grammar of a generatevalues step. */\nexport function validatevaluegen(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed valuegen rule with a field kind is required in options.\" };\n const rule = value as Record<string, unknown>;\n if (typeof rule.kind !== \"string\" || !fieldkinds.includes(rule.kind as fieldkind)) return { allowed: false, reason: \"The reviewed valuegen kind must be a known field kind.\" };\n if (rule.locale !== undefined && !isnonempty(rule.locale)) return { allowed: false, reason: \"The reviewed valuegen locale must be a non-empty string.\" };\n if (rule.seed !== undefined && (typeof rule.seed !== \"number\" || !Number.isFinite(rule.seed))) return { allowed: false, reason: \"The reviewed valuegen seed must be a finite number.\" };\n return { allowed: true };\n}\n\n/** Validates a reviewed list of label or placeholder value pairs for filllabel and fillplaceholder steps. */\nfunction validatefieldpairs(options: Record<string, unknown>, mode: \"label\" | \"placeholder\"): policyevaluation {\n const pairs = options.fields;\n if (!Array.isArray(pairs) || pairs.length === 0) return { allowed: false, reason: \"A reviewed non-empty list of field pairs is required in options.\" };\n for (const item of pairs) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) return { allowed: false, reason: \"Every reviewed field pair must be an object.\" };\n const pair = item as Record<string, unknown>;\n if (!isnonempty(pair[mode])) return { allowed: false, reason: `Every reviewed field pair needs a non-empty ${mode}.` };\n if (typeof pair.value !== \"string\" || !pair.value.trim()) return { allowed: false, reason: \"Every reviewed field pair needs a non-empty value.\" };\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed card segment grammar of a fillcard step. */\nfunction validatecardsegments(value: unknown): policyevaluation {\n if (!Array.isArray(value) || value.length === 0) return { allowed: false, reason: \"A reviewed non-empty list of card segments is required in options.\" };\n for (const item of value) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) return { allowed: false, reason: \"Every reviewed card segment must be an object.\" };\n const segment = item as Record<string, unknown>;\n const matchcheck = validatefieldmatch(segment.match);\n if (!matchcheck.allowed) return matchcheck;\n if (typeof segment.value !== \"string\" || !segment.value.trim()) return { allowed: false, reason: \"Every reviewed card segment needs a non-empty value.\" };\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed forms and data parameter grammar of the form family. */\nfunction validateformgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"fillform\" || (kind === \"saveprofiles\" && options.formrecord !== undefined)) {\n const recordcheck = validateformrecord(options.formrecord);\n if (!recordcheck.allowed) return recordcheck;\n }\n if (kind === \"filllabel\" || kind === \"fillplaceholder\") {\n const paircheck = validatefieldpairs(options, kind === \"filllabel\" ? \"label\" : \"placeholder\");\n if (!paircheck.allowed) return paircheck;\n }\n if (kind === \"generatevalues\" && options.valuegen !== undefined) {\n const rulecheck = validatevaluegen(options.valuegen);\n if (!rulecheck.allowed) return rulecheck;\n }\n if (kind === \"saveprofiles\" && !isnonempty(options.name)) return { allowed: false, reason: \"A reviewed profile name is required in options.\" };\n if (kind === \"submitform\" && !isnonempty(options.consentref)) return { allowed: false, reason: \"A reviewed consent ref of an approved asksubmit ticket is required in options.\" };\n if (kind === \"retryform\") {\n const backoff = options.backoff;\n if (!backoff || typeof backoff !== \"object\" || Array.isArray(backoff)) return { allowed: false, reason: \"A reviewed backoff rule with wait and factor is required in options.\" };\n const rule = backoff as Record<string, unknown>;\n if (typeof rule.wait !== \"number\" || !Number.isFinite(rule.wait) || rule.wait <= 0) return { allowed: false, reason: \"The reviewed retry backoff wait must be a positive number of milliseconds with no code ceiling.\" };\n if (typeof rule.factor !== \"number\" || !Number.isFinite(rule.factor) || rule.factor < 1) return { allowed: false, reason: \"The reviewed retry backoff factor must be one or greater with no code ceiling.\" };\n if (options.attempts !== undefined && (typeof options.attempts !== \"number\" || !Number.isInteger(options.attempts) || options.attempts < 1)) return { allowed: false, reason: \"The reviewed retry attempts must be a positive integer with no code ceiling.\" };\n }\n if (kind === \"runwizard\" && options.steps !== undefined && (typeof options.steps !== \"number\" || !Number.isInteger(options.steps) || options.steps < 1)) return { allowed: false, reason: \"The reviewed wizard step count must be a positive integer with no code ceiling.\" };\n if (kind === \"selectchain\") {\n if (!isnonempty(options.child)) return { allowed: false, reason: \"A reviewed child selector of the dependent control is required in options.\" };\n if (!nonnegativeoption(options, \"wait\")) return { allowed: false, reason: \"The reviewed dependent wait must be zero or a positive number of milliseconds.\" };\n }\n if (kind === \"picktypeahead\") {\n if (!isnonempty(options.pick)) return { allowed: false, reason: \"A reviewed suggestion entry to pick is required in options.\" };\n if (!nonnegativeoption(options, \"timeout\")) return { allowed: false, reason: \"The reviewed typeahead timeout must be zero or a positive number of milliseconds.\" };\n }\n if (kind === \"pickdate\" && !/^\\d{4}-\\d{2}-\\d{2}$/.test(step.value ?? \"\")) return { allowed: false, reason: \"The reviewed date must use the yyyy-mm-dd form.\" };\n if (kind === \"fillcard\") {\n const segmentcheck = validatecardsegments(options.segments);\n if (!segmentcheck.allowed) return segmentcheck;\n if (!nonnegativeoption(options, \"pause\")) return { allowed: false, reason: \"The reviewed card typing pause must be zero or a positive number of milliseconds.\" };\n }\n if (kind === \"fillcode\" && !isnonempty(options.source)) return { allowed: false, reason: \"A reviewed one time code source is required in options.\" };\n if (kind === \"consentpassword\" && !isnonempty(options.consentref)) return { allowed: false, reason: \"A reviewed consent ref is required in options before any password is filled.\" };\n return { allowed: true };\n}\n\n/** Validates one reviewed transform rule: a supported expression, source columns and a target column. */\nexport function validatetransformrule(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed transform rule with an expression, sources and a target is required in options.\" };\n const rule = value as Record<string, unknown>;\n const expression = rule.expression;\n if (typeof expression !== \"string\" || !/^(trim|upper|lower|number|prefix|suffix|replace)(?::.+)?$/.test(expression)) return { allowed: false, reason: \"The reviewed transform expression must be trim, upper, lower, number, prefix, suffix or replace with an optional argument.\" };\n if (expression.startsWith(\"replace\") && !expression.slice(\"replace\".length).includes(\"=>\")) return { allowed: false, reason: \"The reviewed replace expression needs the from=>to separator.\" };\n if (expression.startsWith(\"replace\") && expression.slice(\"replace:\".length).split(\"=>\")[0] === \"\") return { allowed: false, reason: \"The reviewed replace expression needs a non-empty from part.\" };\n if (!Array.isArray(rule.sources) || rule.sources.length === 0 || !rule.sources.every(source => isnonempty(source))) return { allowed: false, reason: \"Every reviewed transform rule needs a non-empty list of source columns.\" };\n if (!isnonempty(rule.target)) return { allowed: false, reason: \"Every reviewed transform rule needs a non-empty target column.\" };\n return { allowed: true };\n}\n\n/** Validates a reviewed dataset id list in options. */\nfunction validatedatasetids(options: Record<string, unknown>, key: string): policyevaluation {\n const ids = options[key];\n if (!Array.isArray(ids) || ids.length === 0 || !ids.every(id => isnonempty(id))) return { allowed: false, reason: `A reviewed non-empty list of dataset ids is required in options as ${key}.` };\n return { allowed: true };\n}\n\n/** Validates the reviewed extraction, transform, export and provenance parameter grammar of the data family. */\nfunction validatedatagrammar(step: toolstep, options: Record<string, unknown>, origin: string): policyevaluation {\n const kind = step.kind;\n if (kind === \"scrapetable\") {\n if (options.name !== undefined && !isnonempty(options.name)) return { allowed: false, reason: \"The reviewed dataset name must be a non-empty string.\" };\n if (options.rowlimit !== undefined && (typeof options.rowlimit !== \"number\" || !Number.isInteger(options.rowlimit) || options.rowlimit < 1)) return { allowed: false, reason: \"The reviewed row limit must be a positive integer with no code ceiling.\" };\n }\n if (kind === \"paginateextract\") {\n if (!isnonempty(options.next)) return { allowed: false, reason: \"A reviewed next control selector is required in options.\" };\n if (options.pages !== undefined && (typeof options.pages !== \"number\" || !Number.isInteger(options.pages) || options.pages < 1)) return { allowed: false, reason: \"The reviewed page count must be a positive integer with no code ceiling.\" };\n if (!nonnegativeoption(options, \"wait\")) return { allowed: false, reason: \"The reviewed row freshness wait must be zero or a positive number of milliseconds.\" };\n }\n if (kind === \"exportcsv\" || kind === \"exportjson\" || kind === \"exportexcel\" || kind === \"copytable\" || kind === \"streamdisk\") {\n if (!isnonempty(options.dataset)) return { allowed: false, reason: \"A reviewed dataset id is required in options.\" };\n if (options.name !== undefined && !isnonempty(options.name)) return { allowed: false, reason: \"The reviewed artifact name must be a non-empty string.\" };\n }\n if (kind === \"exportcsv\" && options.delimiter !== undefined && (typeof options.delimiter !== \"string\" || options.delimiter.length !== 1)) return { allowed: false, reason: \"The reviewed csv delimiter must be a single character.\" };\n if (kind === \"streamdisk\" && (typeof options.chunk !== \"number\" || !Number.isInteger(options.chunk) || options.chunk < 1)) return { allowed: false, reason: \"The reviewed streaming chunk size must be a positive integer with no code ceiling.\" };\n if (kind === \"pushsheets\") {\n if (!isnonempty(options.dataset)) return { allowed: false, reason: \"A reviewed dataset id is required in options.\" };\n if (!isnonempty(options.sheet)) return { allowed: false, reason: \"A reviewed sheet endpoint url is required in options.\" };\n if (!ishttpsurl(options.sheet)) return { allowed: false, reason: \"The reviewed sheet endpoint url must use HTTPS.\" };\n if (options.reviewed !== true) return { allowed: false, reason: \"The sheet push needs the explicit reviewed flag before any data leaves local memory.\" };\n }\n if (kind === \"importcsv\") {\n if (typeof options.csv !== \"string\" || !options.csv.trim()) return { allowed: false, reason: \"Reviewed csv content is required in options.\" };\n if (options.name !== undefined && !isnonempty(options.name)) return { allowed: false, reason: \"The reviewed dataset name must be a non-empty string.\" };\n if (options.mapping !== undefined) {\n const mapping = options.mapping;\n if (!mapping || typeof mapping !== \"object\" || Array.isArray(mapping) || !Object.values(mapping).every(item => typeof item === \"string\")) return { allowed: false, reason: \"The reviewed csv column mapping must be an object of string values.\" };\n }\n }\n if (kind === \"looprows\") {\n if (!isnonempty(options.dataset)) return { allowed: false, reason: \"A reviewed dataset id is required in options.\" };\n if (options.variable !== undefined && !isnonempty(options.variable)) return { allowed: false, reason: \"The reviewed row variable name must be a non-empty string.\" };\n const inner = validateinnerstep(options, origin);\n if (!inner.allowed) return inner;\n }\n if (kind === \"transformvalues\") {\n if (!isnonempty(options.dataset)) return { allowed: false, reason: \"A reviewed dataset id is required in options.\" };\n const rules = options.rules;\n if (!Array.isArray(rules) || rules.length === 0) return { allowed: false, reason: \"A reviewed non-empty list of transform rules is required in options.\" };\n for (const item of rules) {\n const rulecheck = validatetransformrule(item);\n if (!rulecheck.allowed) return rulecheck;\n }\n }\n if (kind === \"deduperows\") {\n if (!isnonempty(options.dataset)) return { allowed: false, reason: \"A reviewed dataset id is required in options.\" };\n const keys = options.keys;\n if (!Array.isArray(keys) || keys.length === 0 || !keys.every(key => isnonempty(key))) return { allowed: false, reason: \"A reviewed non-empty list of dedupe column keys is required in options.\" };\n }\n if (kind === \"mergepages\") {\n const listcheck = validatedatasetids(options, \"datasets\");\n if (!listcheck.allowed) return listcheck;\n }\n if (kind === \"stamplerows\") {\n if (!isnonempty(options.dataset)) return { allowed: false, reason: \"A reviewed dataset id is required in options.\" };\n if (options.url !== undefined && !ishttpsurl(options.url)) return { allowed: false, reason: \"The reviewed source url must use HTTPS.\" };\n }\n if (kind === \"previewgrid\") {\n if (!isnonempty(options.dataset)) return { allowed: false, reason: \"A reviewed dataset id is required in options.\" };\n if (options.sample !== undefined && (typeof options.sample !== \"number\" || !Number.isInteger(options.sample) || options.sample < 1)) return { allowed: false, reason: \"The reviewed sample row count must be a positive integer with no code ceiling.\" };\n }\n if (kind === \"resumeextract\" && !isnonempty(options.session)) return { allowed: false, reason: \"A reviewed extract session id is required in options.\" };\n if (kind === \"logprovenance\" && !isnonempty(options.artifact)) return { allowed: false, reason: \"A reviewed artifact id or name is required in options.\" };\n return { allowed: true };\n}\n\n/** Validates a reviewed batch download specification: a non-empty HTTPS url list, an optional filename rule and an optional completion criterion. */\nexport function validatedownloadspec(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed downloadspec with a url list is required in options.\" };\n const spec = value as Record<string, unknown>;\n if (!Array.isArray(spec.urls) || spec.urls.length === 0 || !spec.urls.every(url => ishttpsurl(url))) return { allowed: false, reason: \"The reviewed downloadspec needs a non-empty list of HTTPS urls.\" };\n if (spec.filename !== undefined && !isnonempty(spec.filename)) return { allowed: false, reason: \"The reviewed downloadspec filename rule must be a non-empty string.\" };\n if (spec.complete !== undefined && spec.complete !== \"size\" && spec.complete !== \"checksum\") return { allowed: false, reason: \"The reviewed downloadspec completion criterion must be size or checksum.\" };\n return { allowed: true };\n}\n\n/** Validates a reviewed mime interception filter: include and exclude patterns plus the deny default for unlisted mime types. */\nexport function validatemimefilter(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed mimefilter with include and exclude patterns is required in options.\" };\n const filter = value as Record<string, unknown>;\n if (!Array.isArray(filter.include) || filter.include.length === 0 || !filter.include.every(pattern => isnonempty(pattern))) return { allowed: false, reason: \"The reviewed mimefilter needs a non-empty list of include patterns.\" };\n if (filter.exclude !== undefined && (!Array.isArray(filter.exclude) || !filter.exclude.every(pattern => isnonempty(pattern)))) return { allowed: false, reason: \"The reviewed mimefilter exclude patterns must be a list of non-empty strings.\" };\n if (filter.default !== \"deny\" && filter.default !== \"allow\") return { allowed: false, reason: \"The reviewed mimefilter needs the deny or allow default for unlisted mime types.\" };\n return { allowed: true };\n}\n\n/** Validates one reviewed cleanup rule: a positive age window with no code ceiling, an artifact kind and a keep policy. */\nexport function validatecleanuprule(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed cleanuprule with an age, a kind and a keep policy is required.\" };\n const rule = value as Record<string, unknown>;\n if (typeof rule.age !== \"number\" || !Number.isFinite(rule.age) || rule.age <= 0) return { allowed: false, reason: \"The reviewed cleanup age window must be a positive number of milliseconds with no code ceiling.\" };\n if (!isnonempty(rule.kind)) return { allowed: false, reason: \"The reviewed cleanup rule needs a non-empty artifact kind, or any to match every kind.\" };\n if (rule.keep !== \"none\" && rule.keep !== \"latest\" && rule.keep !== \"all\") return { allowed: false, reason: \"The reviewed cleanup keep policy must be none, latest or all.\" };\n return { allowed: true };\n}\n\n/** Validates the reviewed files, clipboard and downloads parameter grammar; batch sizes, concurrent windows and cleanup ages stay user configured with no code ceilings. */\nfunction validatefilesgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"batchdownload\") {\n const speccheck = validatedownloadspec(options.downloadspec);\n if (!speccheck.allowed) return speccheck;\n if (options.concurrent !== undefined && (typeof options.concurrent !== \"number\" || !Number.isInteger(options.concurrent) || options.concurrent < 1)) return { allowed: false, reason: \"The reviewed concurrent download window must be a positive integer with no code ceiling.\" };\n }\n if (kind === \"pausedownload\" || kind === \"resumedownload\" || kind === \"verifydownload\" || kind === \"quarantinedownload\" || kind === \"scanvirus\") {\n if (!isnonempty(step.value)) return { allowed: false, reason: \"A reviewed download or quarantine reference is required.\" };\n if (kind === \"verifydownload\") {\n if (options.checksum !== undefined && !isnonempty(options.checksum)) return { allowed: false, reason: \"The reviewed expected checksum must be a non-empty string.\" };\n if (options.bytes !== undefined && (typeof options.bytes !== \"number\" || !Number.isFinite(options.bytes) || options.bytes < 0)) return { allowed: false, reason: \"The reviewed expected size must be zero or a positive number of bytes.\" };\n }\n if (kind === \"scanvirus\" && options.scanner !== undefined && !isnonempty(options.scanner)) return { allowed: false, reason: \"The reviewed scanner name must be a non-empty string.\" };\n if (kind === \"quarantinedownload\" && options.reason !== undefined && !isnonempty(options.reason)) return { allowed: false, reason: \"The reviewed quarantine reason must be a non-empty string.\" };\n }\n if (kind === \"interceptmime\") {\n const filtercheck = validatemimefilter(options.mimefilter);\n if (!filtercheck.allowed) return filtercheck;\n }\n if (kind === \"readclipboard\") {\n if (!isnonempty(options.consentref)) return { allowed: false, reason: \"A clipboard read requires a reviewed consent ref of an approved consent prompt in options.\" };\n if (options.prompt !== undefined && !isnonempty(options.prompt)) return { allowed: false, reason: \"The reviewed clipboard consent prompt must be a non-empty string.\" };\n }\n if (kind === \"exportnetlog\" && options.stepid !== undefined && !isnonempty(options.stepid)) return { allowed: false, reason: \"The reviewed netlog step filter must be a non-empty step id.\" };\n if (kind === \"namecaptures\") {\n if (!isnonempty(options.task)) return { allowed: false, reason: \"A reviewed task id is required in options for capture naming.\" };\n if (options.steps !== undefined && (!Array.isArray(options.steps) || options.steps.length === 0 || !options.steps.every(item => isnonempty(item)))) return { allowed: false, reason: \"The reviewed capture steps must be a non-empty list of step ids when present.\" };\n if (options.extension !== undefined && !isnonempty(options.extension)) return { allowed: false, reason: \"The reviewed capture extension must be a non-empty string.\" };\n }\n if (kind === \"cleanupartifacts\" && options.rules !== undefined) {\n const rules = options.rules;\n if (!Array.isArray(rules) || rules.length === 0) return { allowed: false, reason: \"The reviewed cleanup rules must be a non-empty list when present.\" };\n for (const item of rules) {\n const rulecheck = validatecleanuprule(item);\n if (!rulecheck.allowed) return rulecheck;\n }\n }\n return { allowed: true };\n}\n\n/** Requires an approved consent prompt before any clipboard read; every read consumes its own prompt. */\nexport function clipboardconsentgranted(step: toolstep): policyevaluation {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const consentref = options.consentref;\n if (typeof consentref !== \"string\" || !consentref.trim()) return { allowed: false, reason: \"A clipboard read requires a reviewed consent ref in options.\" };\n return { allowed: true };\n}\n\n/** Refuses to release any quarantined file before a clean scan verdict exists. */\nexport function quarantinereleasegranted(entry: quarantineentry): policyevaluation {\n if (entry.scan !== \"clean\") return { allowed: false, reason: `The quarantined file ${entry.path} cannot leave quarantine with the ${entry.scan} scan verdict; only a clean verdict releases it.` };\n return { allowed: true };\n}\n\n/** Refuses downloads and download interception that fall outside the session origin grants. */\nexport function downloadgranted(session: agentsession | undefined, url: string): policyevaluation {\n let origin = \"\";\n try { origin = new URL(url).origin; } catch { return { allowed: false, reason: \"The reviewed download URL is invalid.\" }; }\n if (!origingranted(session, origin)) return { allowed: false, reason: `The download from ${origin} leaves the session origin grants and needs a session grant first.` };\n return { allowed: true };\n}\n\n/** Masks a clipboard payload for every log line; the full text never persists anywhere. */\nexport function maskclipboard(payload: string): string {\n return `[clipboard payload of ${payload.length} character${payload.length === 1 ? \"\" : \"s\"}]`;\n}\n\n/** Requires an asksubmit review step before every form submission step. */\nexport function submitreviewgranted(steps: toolstep[], submitid: string): policyevaluation {\n const position = steps.findIndex(candidate => candidate.id === submitid);\n const asked = steps.some((candidate, index) => candidate.kind === \"asksubmit\" && (position === -1 || index < position));\n return asked ? { allowed: true } : { allowed: false, reason: \"Form submission requires an asksubmit review step before it.\" };\n}\n\n/** Requires a reviewed consent ref before any password field is filled. */\nexport function passwordconsentgranted(step: toolstep): policyevaluation {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const consentref = options.consentref;\n if (typeof consentref !== \"string\" || !consentref.trim()) return { allowed: false, reason: \"A password fill requires a reviewed consent ref in options.\" };\n return { allowed: true };\n}\n\n/** True when a numeric value passes the Luhn checksum used by card networks. */\nfunction luhnvalid(digits: string): boolean {\n let sum = 0;\n let double = false;\n for (let index = digits.length - 1; index >= 0; index -= 1) {\n let value = Number.parseInt(digits[index] ?? \"\", 10);\n if (!Number.isFinite(value)) return false;\n if (double) { value *= 2; if (value > 9) value -= 9; }\n sum += value;\n double = !double;\n }\n return sum % 10 === 0;\n}\n\n/** Refuses generated values that look like real card numbers or personal identifiers; test prefixed card values stay allowed. */\nexport function generatedvalueallowed(value: string): policyevaluation {\n const compact = value.replace(/[\\s-]/g, \"\");\n if (/^\\d{13,19}$/.test(compact) && luhnvalid(compact) && !compact.startsWith(\"4111\")) return { allowed: false, reason: \"The generated value looks like a real card number and is refused; generated card values use the 4111 test prefix.\" };\n if (/^\\d{3}-\\d{2}-\\d{4}$/.test(value.trim())) return { allowed: false, reason: \"The generated value looks like a personal identifier and is refused.\" };\n return { allowed: true };\n}\n\n/** Requires the origin grants of a saved profile to cover the origin before its values fill a page. */\nexport function profilegrantgranted(profile: formprofile, origin: string): policyevaluation {\n if (!profile.grants.includes(origin)) return { allowed: false, reason: `The saved profile ${profile.name} is not granted to ${origin}; add the origin to the profile grants first.` };\n return { allowed: true };\n}\n\n/** Restricts group and layout mutations to the active session: they refuse without a live session. */\nexport function layoutmutationgranted(session: agentsession | undefined, now: number): policyevaluation {\n if (!session || session.stoppedat || session.expiresat <= now) return { allowed: false, reason: \"Group and layout mutations stay inside the active session.\" };\n return { allowed: true };\n}\n\n/** Requires explicit review before closing a window that holds more than one task tab. */\nexport function windowclosegate(tasktabcount: number, reviewed: boolean): policyevaluation {\n if (tasktabcount > 1 && !reviewed) return { allowed: false, reason: `The window holds ${tasktabcount} task tabs and needs explicit review before it closes.` };\n return { allowed: true };\n}\n\n/** Reads the user configured concurrent task tab ceiling; an absent value never refuses a tab. */\nexport function tasktabceiling(settings: runsettings | undefined): number | undefined {\n const ceiling = settings?.tasktabceiling;\n return typeof ceiling === \"number\" && Number.isFinite(ceiling) && ceiling >= 0 ? ceiling : undefined;\n}\n\n/** Parses the reviewed wait duration of a wait step with no upper bound. */\nexport function waitduration(step: toolstep): number {\n const requested = step.value ? Number.parseInt(step.value, 10) : 250;\n if (!Number.isFinite(requested) || requested < 0) throw new Error(\"Wait duration must be zero or a positive number of milliseconds.\");\n return requested;\n}\n\nfunction isnumericid(value: unknown): value is string {\n return typeof value === \"string\" && /^\\d+$/.test(value);\n}\n\nfunction numericoption(options: Record<string, unknown>, key: string): boolean {\n return options[key] === undefined || (typeof options[key] === \"number\" && Number.isFinite(options[key] as number));\n}\n\n/** True when an optional numeric option is absent or a finite number of zero or more. */\nfunction nonnegativeoption(options: Record<string, unknown>, key: string): boolean {\n return numericoption(options, key) && !(typeof options[key] === \"number\" && (options[key] as number) < 0);\n}\n\nfunction isnonempty(value: unknown): value is string {\n return typeof value === \"string\" && value.trim().length > 0;\n}\n\nfunction ispoint(value: unknown): boolean {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return false;\n const point = value as Record<string, unknown>;\n return typeof point.x === \"number\" && Number.isFinite(point.x) && typeof point.y === \"number\" && Number.isFinite(point.y);\n}\n\n/** Grades a resolution match count: zero is absent, one is resolved and more than one is refused as ambiguous. */\nexport function resolutionverdict(count: number): \"absent\" | \"resolved\" | \"ambiguous\" {\n if (!Number.isFinite(count) || count <= 0) return \"absent\";\n return count === 1 ? \"resolved\" : \"ambiguous\";\n}\n\n/** Validates the reviewed targetref grammar of every resolution mode and rejects empty references. */\nexport function validatetargetref(reference: unknown): policyevaluation {\n if (!reference || typeof reference !== \"object\" || Array.isArray(reference)) return { allowed: false, reason: \"The reviewed target reference must be an object.\" };\n const ref = reference as Record<string, unknown>;\n if (ref.mode === \"selector\") return isnonempty(ref.selector) ? { allowed: true } : { allowed: false, reason: \"The selector target reference needs a non-empty selector.\" };\n if (ref.mode === \"text\") return isnonempty(ref.text) ? { allowed: true } : { allowed: false, reason: \"The text target reference needs non-empty text.\" };\n if (ref.mode === \"aria\") {\n if (!isnonempty(ref.role)) return { allowed: false, reason: \"The aria target reference needs a non-empty role.\" };\n return isnonempty(ref.name) ? { allowed: true } : { allowed: false, reason: \"The aria target reference needs a non-empty name.\" };\n }\n if (ref.mode === \"name\") return isnonempty(ref.name) ? { allowed: true } : { allowed: false, reason: \"The name target reference needs a non-empty name.\" };\n if (ref.mode === \"xpath\") return isnonempty(ref.xpath) ? { allowed: true } : { allowed: false, reason: \"The xpath target reference needs a non-empty expression.\" };\n if (ref.mode === \"index\") {\n const index = ref.index;\n return typeof index === \"number\" && Number.isInteger(index) && index >= 1 ? { allowed: true } : { allowed: false, reason: \"The index target reference needs a positive integer map number.\" };\n }\n if (ref.mode === \"point\") {\n const pointok = typeof ref.x === \"number\" && Number.isFinite(ref.x) && typeof ref.y === \"number\" && Number.isFinite(ref.y);\n return pointok ? { allowed: true } : { allowed: false, reason: \"The point target reference needs numeric x and y coordinates.\" };\n }\n return { allowed: false, reason: \"The target reference mode must be selector, text, aria, name, xpath, index or point.\" };\n}\n\n/** True when the session origin grants cover the given origin; a session without grants only allows its own origin. */\nexport function origingranted(session: agentsession | undefined, origin: string): boolean {\n if (!session) return false;\n const grants = session.grants ?? [session.origin];\n return grants.includes(origin);\n}\n\n/** Decides whether an unreviewed origin may open: the session grants cover it or a safe checksafe verdict vouches for it. */\nexport function originverified(url: string, grants: string[], verdicts: safetyverdict[]): policyevaluation {\n let origin = \"\";\n try { origin = new URL(url).origin; } catch { return { allowed: false, reason: \"The reviewed navigation URL is invalid.\" }; }\n if (grants.includes(origin)) return { allowed: true };\n const covered = verdicts.find(verdict => verdict.safe && (verdict.url === url || (safeorigin(verdict.url) === origin)));\n if (covered) return { allowed: true };\n return { allowed: false, reason: `The origin ${origin} is outside the session grants and has no safe checksafe verdict; run checksafe and review it first.` };\n}\n\nfunction safeorigin(url: string): string {\n try { return new URL(url).origin; } catch { return \"\"; }\n}\n\n/** Refuses navigation that would move a granted task tab outside the session origin grants until the user consents. */\nexport function navigationgranted(session: agentsession | undefined, url: string): policyevaluation {\n let origin = \"\";\n try { origin = new URL(url).origin; } catch { return { allowed: false, reason: \"The reviewed navigation URL is invalid.\" }; }\n if (origingranted(session, origin)) return { allowed: true };\n return { allowed: false, reason: `Navigation to ${origin} leaves the task tab origins and needs the user consent of a session grant first.` };\n}\n\n/** Validates the reviewed inner step of a retry or frame wrapper against the same rules as a top-level step. */\nfunction validateinnerstep(options: Record<string, unknown>, origin: string): policyevaluation {\n const stepid = options.stepid;\n const kind = options.kind;\n if (isnonempty(stepid)) {\n if (kind !== undefined) return { allowed: false, reason: \"The reviewed wrapper must reference a step id or an inline step, not both.\" };\n return { allowed: true };\n }\n if (typeof kind !== \"string\" || !kind.trim()) return { allowed: false, reason: \"A reviewed step id or inline step kind is required in options.\" };\n if (kind === \"retryaction\" || kind === \"enterframe\" || kind === \"looprows\") return { allowed: false, reason: \"The reviewed inner step cannot be another wrapper kind.\" };\n if (!allowedactions.has(kind as actionkind)) return { allowed: false, reason: \"The reviewed inner step kind is unsupported.\" };\n const inneroptions = options.options;\n if (inneroptions !== undefined && (!inneroptions || typeof inneroptions !== \"object\" || Array.isArray(inneroptions))) return { allowed: false, reason: \"The reviewed inner step options must be an object.\" };\n const inner: toolstep = {\n id: \"inner\",\n kind: kind as actionkind,\n summary: \"Reviewed inner step.\",\n risk: actionrisk(kind as actionkind),\n ...(isnonempty(options.target) ? { target: options.target } : {}),\n ...(isnonempty(options.value) ? { value: options.value } : {}),\n ...(inneroptions !== undefined ? { options: JSON.stringify(inneroptions) } : {}),\n };\n return validatestep(inner, origin);\n}\n\n/** True when a reviewed https url parses. */\nfunction ishttpsurl(value: unknown): value is string {\n if (typeof value !== \"string\" || !value.trim()) return false;\n try { return new URL(value).protocol === \"https:\"; } catch { return false; }\n}\n\n/** Validates the reviewed navtarget grammar of a navigation step. */\nfunction validatenavtarget(value: unknown, kind: string): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed navtarget with a url is required in options.\" };\n const target = value as Record<string, unknown>;\n if (!ishttpsurl(target.url)) return { allowed: false, reason: \"The reviewed navtarget url must use HTTPS.\" };\n const container = target.container ?? \"tab\";\n if (container !== \"current\" && container !== \"tab\" && container !== \"window\" && container !== \"private\") return { allowed: false, reason: \"The reviewed navtarget container must be current, tab, window or private.\" };\n if (target.position !== undefined && target.position !== \"adjacent\" && target.position !== \"end\") return { allowed: false, reason: \"The reviewed navtarget position must be adjacent or end.\" };\n if (kind === \"openprivate\" && container !== \"private\") return { allowed: false, reason: \"The openprivate step requires the private container.\" };\n if (kind === \"openlink\" && container === \"private\") return { allowed: false, reason: \"The openlink step cannot open the private container; use openprivate.\" };\n return { allowed: true };\n}\n\n/** Validates the reviewed waitprofile grammar with its load signals, thresholds and per origin overrides. */\nfunction validatewaitprofile(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed waitprofile with load signals is required in options.\" };\n const profile = value as Record<string, unknown>;\n if (!Array.isArray(profile.signals) || profile.signals.length === 0 || !profile.signals.every(signal => isnonempty(signal))) return { allowed: false, reason: \"The reviewed waitprofile needs a non-empty list of load signals.\" };\n if (!nonnegativeoption(profile, \"idle\")) return { allowed: false, reason: \"The reviewed waitprofile idle threshold must be zero or a positive number of milliseconds.\" };\n if (!nonnegativeoption(profile, \"timeout\")) return { allowed: false, reason: \"The reviewed waitprofile timeout must be zero or a positive number of milliseconds.\" };\n if (profile.overrides !== undefined) {\n if (!Array.isArray(profile.overrides) || profile.overrides.length === 0) return { allowed: false, reason: \"The reviewed waitprofile overrides must be a non-empty list when present.\" };\n for (const entry of profile.overrides) {\n if (!entry || typeof entry !== \"object\" || Array.isArray(entry)) return { allowed: false, reason: \"Every reviewed waitprofile override must be an object with an origin.\" };\n const override = entry as Record<string, unknown>;\n if (!ishttpsurl(override.origin)) return { allowed: false, reason: \"Every reviewed waitprofile override origin must use HTTPS.\" };\n if (override.signals !== undefined && (!Array.isArray(override.signals) || !override.signals.every(signal => isnonempty(signal)))) return { allowed: false, reason: \"The reviewed waitprofile override signals must be a list of non-empty strings.\" };\n if (!nonnegativeoption(override, \"idle\") || !nonnegativeoption(override, \"timeout\")) return { allowed: false, reason: \"The reviewed waitprofile override thresholds must be zero or positive numbers.\" };\n }\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed urlpattern grammar with its match mode plus query and fragment parts. */\nexport function validateurlpattern(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed urlpattern is required in options.\" };\n const pattern = value as Record<string, unknown>;\n if (pattern.mode !== \"exact\" && pattern.mode !== \"prefix\" && pattern.mode !== \"host\" && pattern.mode !== \"pattern\") return { allowed: false, reason: \"The reviewed urlpattern mode must be exact, prefix, host or pattern.\" };\n if (!ishttpsurl(pattern.url)) return { allowed: false, reason: \"The reviewed urlpattern url must use HTTPS.\" };\n if (pattern.query !== undefined) {\n if (!pattern.query || typeof pattern.query !== \"object\" || Array.isArray(pattern.query)) return { allowed: false, reason: \"The reviewed urlpattern query part must be an object of parameter names and values.\" };\n for (const item of Object.values(pattern.query)) if (typeof item !== \"string\") return { allowed: false, reason: \"The reviewed urlpattern query values must be strings.\" };\n }\n if (pattern.fragment !== undefined && !isnonempty(pattern.fragment)) return { allowed: false, reason: \"The reviewed urlpattern fragment must be a non-empty string.\" };\n return { allowed: true };\n}\n\n/** Validates a reviewed non-empty list of HTTPS urls in options. */\nfunction validateurllist(options: Record<string, unknown>, key: string): policyevaluation {\n const urls = options[key];\n if (!Array.isArray(urls) || urls.length === 0 || !urls.every(url => ishttpsurl(url))) return { allowed: false, reason: `A reviewed non-empty list of HTTPS urls is required in options as ${key}.` };\n return { allowed: true };\n}\n\n/** Validates the reviewed ratelimit grammar of a navrate step; the window and ceiling stay user configured with no hardcoded cap. */\nfunction validateratelimit(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed ratelimit with a window and a ceiling is required in options.\" };\n const limit = value as Record<string, unknown>;\n if (limit.domain !== undefined && !isnonempty(limit.domain)) return { allowed: false, reason: \"The reviewed ratelimit domain must be a non-empty string.\" };\n if (typeof limit.window !== \"number\" || !Number.isFinite(limit.window) || limit.window <= 0) return { allowed: false, reason: \"The reviewed ratelimit window must be a positive number of milliseconds with no code ceiling.\" };\n if (typeof limit.ceiling !== \"number\" || !Number.isInteger(limit.ceiling) || limit.ceiling < 1) return { allowed: false, reason: \"The reviewed ratelimit ceiling must be a positive integer with no code ceiling.\" };\n return { allowed: true };\n}\n\n/** Validates the reviewed tabquery grammar with url, title, id and pattern matchers; at least one matcher is required. */\nexport function validatetabquery(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed tabquery with at least one matcher is required in options.\" };\n const query = value as Record<string, unknown>;\n const hasmatcher = query.url !== undefined || query.title !== undefined || query.id !== undefined || query.pattern !== undefined;\n if (!hasmatcher) return { allowed: false, reason: \"The reviewed tabquery needs a url, title, id or pattern matcher.\" };\n if (query.url !== undefined && !isnonempty(query.url)) return { allowed: false, reason: \"The reviewed tabquery url matcher must be a non-empty string.\" };\n if (query.title !== undefined && !isnonempty(query.title)) return { allowed: false, reason: \"The reviewed tabquery title matcher must be a non-empty string.\" };\n if (query.pattern !== undefined && !isnonempty(query.pattern)) return { allowed: false, reason: \"The reviewed tabquery pattern matcher must be a non-empty string.\" };\n if (query.id !== undefined && (typeof query.id !== \"number\" || !Number.isInteger(query.id) || query.id < 0)) return { allowed: false, reason: \"The reviewed tabquery id matcher must be a non-negative integer tab id.\" };\n return { allowed: true };\n}\n\n/** Validates a reviewed group color choice against the Chromium tab group palette. */\nfunction validategroupcolor(value: unknown): boolean {\n return typeof value === \"string\" && (groupcolors as string[]).includes(value);\n}\n\n/** Validates a reviewed list of numeric browser ids in options. */\nfunction validateidlist(options: Record<string, unknown>, key: string): boolean {\n const ids = options[key];\n return Array.isArray(ids) && ids.length > 0 && ids.every(id => typeof id === \"number\" && Number.isInteger(id) && id >= 0);\n}\n\n/** Validates the reviewed tab and window parameter grammar of the tabs and windows command family. */\nfunction validatetabsgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"querytabs\" || kind === \"closepattern\") {\n const querycheck = validatetabquery(options.tabquery);\n if (!querycheck.allowed) return querycheck;\n if (kind === \"closepattern\" && options.reviewed !== true) return { allowed: false, reason: \"The close pattern needs the explicit reviewed flag before any tab closes.\" };\n }\n if (kind === \"duplicatetab\" || kind === \"pintab\" || kind === \"mutetab\" || kind === \"movetab\" || kind === \"movetabwindow\" || kind === \"badgetab\" || kind === \"attachmeta\") {\n if (!isnumericid(step.value)) return { allowed: false, reason: \"A numeric browser tab id is required.\" };\n }\n if (kind === \"focuswindow\" || kind === \"maximizewindow\" || kind === \"minimizewindow\" || kind === \"restorewindow\") {\n if (!isnumericid(step.value)) return { allowed: false, reason: \"A numeric browser window id is required.\" };\n }\n if (kind === \"pintab\" && typeof options.pinned !== \"boolean\") return { allowed: false, reason: \"A reviewed pinned flag is required in options.\" };\n if (kind === \"mutetab\" && typeof options.muted !== \"boolean\") return { allowed: false, reason: \"A reviewed muted flag is required in options.\" };\n if (kind === \"movetab\") {\n if (typeof options.index !== \"number\" || !Number.isInteger(options.index) || options.index < 0) return { allowed: false, reason: \"A reviewed non-negative target index is required in options.\" };\n }\n if (kind === \"movetabwindow\") {\n if (typeof options.windowid !== \"number\" || !Number.isInteger(options.windowid) || options.windowid < 0) return { allowed: false, reason: \"A reviewed target window id is required in options.\" };\n }\n if (kind === \"grouptabs\") {\n const group = options.group;\n if (!group || typeof group !== \"object\" || Array.isArray(group)) return { allowed: false, reason: \"A reviewed group with a name is required in options.\" };\n const spec = group as Record<string, unknown>;\n if (!isnonempty(spec.name)) return { allowed: false, reason: \"The reviewed group needs a non-empty name.\" };\n if (!validategroupcolor(spec.color)) return { allowed: false, reason: \"The reviewed group color must be a Chromium tab group color.\" };\n if (!validateidlist(spec, \"tabids\")) return { allowed: false, reason: \"The reviewed group needs a non-empty list of member tab ids.\" };\n }\n if (kind === \"colorgroup\") {\n if (!isnonempty(options.name)) return { allowed: false, reason: \"A reviewed group name is required in options.\" };\n if (!validategroupcolor(options.color)) return { allowed: false, reason: \"The reviewed group color must be a Chromium tab group color.\" };\n }\n if (kind === \"collapsegroup\") {\n if (!isnonempty(options.name)) return { allowed: false, reason: \"A reviewed group name is required in options.\" };\n if (typeof options.collapsed !== \"boolean\") return { allowed: false, reason: \"A reviewed collapsed flag is required in options.\" };\n }\n if (kind === \"discardtab\" || kind === \"reloadtabs\") {\n if (!isnumericid(step.value) && !validateidlist(options, \"tabs\")) return { allowed: false, reason: \"A numeric tab id or a reviewed list of tab ids is required.\" };\n }\n if (kind === \"zoomin\" || kind === \"zoomout\") {\n if (options.step !== undefined && (typeof options.step !== \"number\" || !Number.isFinite(options.step) || options.step <= 0)) return { allowed: false, reason: \"The reviewed zoom step must be a positive number with no code ceiling.\" };\n if (step.value !== undefined && step.value !== \"\" && !isnumericid(step.value)) return { allowed: false, reason: \"The reviewed zoom target must be a numeric tab id.\" };\n }\n if (kind === \"switchtab\") {\n if (options.direction !== \"next\" && options.direction !== \"previous\") return { allowed: false, reason: \"A reviewed switch direction of next or previous is required in options.\" };\n }\n if (kind === \"restorewindow\") {\n const bounds = options.bounds;\n if (bounds !== undefined) {\n if (!bounds || typeof bounds !== \"object\" || Array.isArray(bounds)) return { allowed: false, reason: \"The reviewed window bounds must be an object.\" };\n const shape = bounds as Record<string, unknown>;\n for (const field of [\"left\", \"top\", \"width\", \"height\"]) {\n if (typeof shape[field] !== \"number\" || !Number.isFinite(shape[field])) return { allowed: false, reason: \"The reviewed window bounds need numeric left, top, width and height.\" };\n }\n }\n }\n if (kind === \"scratchwindow\") {\n if (step.value !== undefined && step.value !== \"\" && !ishttpsurl(step.value)) return { allowed: false, reason: \"The reviewed scratch window url must use HTTPS.\" };\n }\n if (kind === \"incognitowindow\" && !ishttpsurl(step.value)) return { allowed: false, reason: \"A reviewed HTTPS url is required to open an incognito window.\" };\n if (kind === \"restoretab\" && step.value !== undefined && step.value !== \"\" && !ishttpsurl(step.value)) return { allowed: false, reason: \"The reviewed restore url must use HTTPS.\" };\n if (kind === \"savelayout\" || kind === \"restorelayout\") {\n if (!isnonempty(options.name)) return { allowed: false, reason: \"A reviewed layout name is required in options.\" };\n }\n if (kind === \"badgetab\") {\n if (!isnonempty(options.label)) return { allowed: false, reason: \"A reviewed badge label is required in options.\" };\n if (options.taskid !== undefined && !isnonempty(options.taskid)) return { allowed: false, reason: \"The reviewed badge task id must be a non-empty string.\" };\n }\n if (kind === \"attachmeta\") {\n const labels = options.labels;\n const taskrefs = options.taskrefs;\n const haslabels = Array.isArray(labels) && labels.length > 0 && labels.every(label => isnonempty(label));\n const hastaskrefs = Array.isArray(taskrefs) && taskrefs.length > 0 && taskrefs.every(ref => isnonempty(ref));\n if (!haslabels && !hastaskrefs) return { allowed: false, reason: \"Reviewed labels or task refs are required in options to attach metadata.\" };\n if (options.provenance !== undefined && !isnonempty(options.provenance)) return { allowed: false, reason: \"The reviewed provenance must be a non-empty string.\" };\n }\n if (kind === \"reopenrun\" && !isnonempty(options.run)) return { allowed: false, reason: \"A reviewed run id is required in options to reopen its tabs.\" };\n return { allowed: true };\n}\n\n/** True when the kind belongs to the media capture family of pdf documents, recordings, images, canvases, streams, assets, lapses, conversions and thumbnails. */\nexport function ismediakind(kind: actionkind): boolean {\n return mediaactions.has(kind);\n}\n\n/** True when the kind belongs to the network observation family of fetching, parsing and typed calls. */\nexport function ishttpkind(kind: actionkind): boolean {\n return httpactions.has(kind);\n}\n\n/** True when the kind belongs to the socket and stream family of channels, messages, subscriptions and poll loops. */\nexport function issocketkind(kind: actionkind): boolean {\n return socketactions.has(kind);\n}\n\n/** True when the kind belongs to the request observation family of watches, headers, bodies and page api discovery. */\nexport function isnetwatchkind(kind: actionkind): boolean {\n return netwatchactions.has(kind);\n}\n\n/** True when the kind belongs to the network control family of blocking, mocking, header rewriting, cookies, auth, api keys, proxy routing and uploads. */\nexport function iscontrolkind(kind: actionkind): boolean {\n return controlactions.has(kind);\n}\n\n/** Resolves the reviewed risk of one step: capturebodies grades sensitive when the reviewed mime list carries private payload types and extractapi grades sensitive when the replay verb mutates, while every other kind keeps its risk table grade. */\nexport function resolvedrisk(step: toolstep): \"read\" | \"interaction\" | \"sensitive\" {\n if (step.kind === \"capturebodies\") {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const body = options.body;\n const mimes = body && typeof body === \"object\" && !Array.isArray(body) ? (body as Record<string, unknown>).mimes : undefined;\n if (Array.isArray(mimes) && mimes.some(mime => typeof mime === \"string\" && privatemime(mime))) return \"sensitive\";\n return \"interaction\";\n }\n if (step.kind === \"extractapi\") {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const replay = options.replay;\n const verb = replay && typeof replay === \"object\" && !Array.isArray(replay) ? (replay as Record<string, unknown>).verb : undefined;\n if (typeof verb === \"string\" && ![\"GET\", \"HEAD\", \"OPTIONS\"].includes(verb.trim().toUpperCase())) return \"sensitive\";\n return \"read\";\n }\n return actionrisk(step.kind);\n}\n\n/** Restricts every outbound channel to a granted origin: wss websocket and https event stream urls map onto their https origin, carry no embedded credentials and stay inside the session origin grants. */\nexport function socketgate(session: agentsession | undefined, url: string): policyevaluation {\n let parsed: URL;\n try { parsed = new URL(url); } catch { return { allowed: false, reason: \"The channel needs a valid url before it can be reviewed.\" }; }\n if (parsed.protocol !== \"wss:\" && parsed.protocol !== \"https:\") return { allowed: false, reason: \"Channels use wss websocket urls or https event stream urls only.\" };\n if (parsed.username || parsed.password) return { allowed: false, reason: \"Channel credentials are not allowed in the url.\" };\n const origin = channelorigin(url);\n if (!origingranted(session, origin)) return { allowed: false, reason: `The channel to ${origin} stays outside the session origin grants.` };\n return { allowed: true };\n}\n\n/** Requires the user granted request watching before any watchrequests step runs; the observation derives from the page timing buffers and the grant adds no manifest permission. */\nexport function watchgate(session: agentsession | undefined, settings: runsettings | undefined, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the request watch.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot watch requests.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot watch requests.\" };\n if (settings?.webrequestgrant !== true) return { allowed: false, reason: \"Request watching needs the webrequest grant in the review panel first; the observation derives from the page timing buffers and adds no manifest permission.\" };\n return { allowed: true };\n}\n\n/** Requires the host grant for every observed origin before header reads, body captures and endpoint replays touch an exchange. */\nexport function observedorigingranted(session: agentsession | undefined, url: string): policyevaluation {\n let origin = \"\";\n try { origin = new URL(url).origin; } catch { return { allowed: false, reason: \"The observed exchange url does not parse for an origin check.\" }; }\n if (!origingranted(session, origin)) return { allowed: false, reason: `The observed origin ${origin} stays outside the session origin grants; grant it before reading headers, bodies or replays.` };\n return { allowed: true };\n}\n\n/** Restricts every outbound request to a granted origin: the url must be a reviewed HTTPS url inside the session origin grants. */\nexport function origincheck(session: agentsession | undefined, url: string): policyevaluation {\n let parsed: URL;\n try { parsed = new URL(url); } catch { return { allowed: false, reason: \"The outbound request needs a valid url before it can be reviewed.\" }; }\n if (parsed.protocol !== \"https:\") return { allowed: false, reason: \"Outbound requests use HTTPS urls only.\" };\n if (parsed.username || parsed.password) return { allowed: false, reason: \"Endpoint credentials are not allowed in the url.\" };\n if (!origingranted(session, parsed.origin)) return { allowed: false, reason: `The outbound request to ${parsed.origin} stays outside the session origin grants.` };\n return { allowed: true };\n}\n\n/** True when a header name carries credentials and therefore needs the explicit consent that names it. */\nexport function credentialheadername(name: string): boolean {\n return credentialheaders.has(name.trim().toLowerCase());\n}\n\n/** Requires a reviewed consent ref before any custom header leaves the extension; requests without custom headers need no prompt. */\nexport function fetchconsentrefgranted(step: toolstep): policyevaluation {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const request = options.fetch;\n const headers = request && typeof request === \"object\" && !Array.isArray(request) ? (request as Record<string, unknown>).headers : undefined;\n const names = headers && typeof headers === \"object\" && !Array.isArray(headers) ? Object.keys(headers as Record<string, unknown>) : [];\n if (names.length === 0) return { allowed: true };\n const empty = names.some(name => !name.trim());\n if (empty) return { allowed: false, reason: \"Header allowlists with empty names are refused.\" };\n const credential = names.find(name => credentialheadername(name));\n if (credential !== undefined && !isnonempty(options.consentref)) return { allowed: false, reason: `The credential bearing header ${credential} needs the explicit reviewed consent that names it before it is sent.` };\n if (!isnonempty(options.consentref)) return { allowed: false, reason: `The ${names.length} reviewed custom header${names.length === 1 ? \"\" : \"s\"} need a reviewed consent ref in options before any send.` };\n return { allowed: true };\n}\n\n/** True when one stored fetch consent still covers the origin and every header name inside its expiry window. */\nexport function fetchconsentcovers(consent: { origin: string; headers: Array<{ name: string }>; approved?: boolean; expiresat: number }, origin: string, headernames: string[], now: number): boolean {\n if (consent.approved !== true) return false;\n if (consent.expiresat <= now) return false;\n if (consent.origin !== origin) return false;\n const covered = new Set(consent.headers.map(header => header.name.trim().toLowerCase()));\n return headernames.every(name => covered.has(name.trim().toLowerCase()));\n}\n\n/** True when the reviewed call mutates: rest verbs beyond get, head and options or a graphql mutation; mutating calls grade sensitive. */\nexport function mutationcallof(step: toolstep): boolean {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n if (step.kind === \"callgraphql\") {\n const request = options.graphql;\n return Boolean(request && typeof request === \"object\" && !Array.isArray(request) && (request as Record<string, unknown>).operationkind === \"mutation\");\n }\n if (step.kind === \"callrest\") {\n const method = typeof options.method === \"string\" ? options.method.trim().toUpperCase() : undefined;\n if (method !== undefined) return ![\"GET\", \"HEAD\", \"OPTIONS\"].includes(method);\n }\n return false;\n}\n\n/** Keeps the reviewed fetch waits inside the reviewed wait budget: the worst case of every timeout plus every backoff wait must fit; every bound itself stays a user choice with no code ceiling. */\nexport function fetchbudgetallowed(timeout: number | undefined, retries: number | undefined, backoff: number | undefined, wait: number | undefined): policyevaluation {\n for (const [label, value] of [[\"timeout\", timeout], [\"retries\", retries], [\"backoff\", backoff]] as Array<[string, number | undefined]>) {\n if (value !== undefined && (typeof value !== \"number\" || !Number.isFinite(value) || value < 0)) return { allowed: false, reason: `The reviewed fetch ${label} must be zero or a positive number with no code ceiling.` };\n }\n if (wait !== undefined && (typeof wait !== \"number\" || !Number.isFinite(wait) || wait < 0)) return { allowed: false, reason: \"The reviewed fetch wait budget must be zero or a positive number of milliseconds.\" };\n if (wait === undefined || timeout === undefined) return { allowed: true };\n const attempts = Math.max(1, Math.floor((retries ?? 0)) + 1);\n const waits = (backoff ?? 0) * (attempts * (attempts - 1)) / 2;\n const worstcase = timeout * attempts + waits;\n if (worstcase > wait) return { allowed: false, reason: `The fetch worst case of ${worstcase} milliseconds exceeds the reviewed wait budget of ${wait} milliseconds; review a wider budget or fewer retries.` };\n return { allowed: true };\n}\n\n/** Resolves the outbound url of an http step at review time: the fetch request url of a fetchurl step and nothing for typed calls whose endpoints resolve at execution. */\nexport function outboundtarget(step: toolstep): string | undefined {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const request = options.fetch;\n if (request && typeof request === \"object\" && !Array.isArray(request)) {\n const url = (request as Record<string, unknown>).url;\n if (typeof url === \"string\" && url.trim()) return url.trim();\n }\n return undefined;\n}\n\n/** Validates one reviewed typed endpoint definition: name, method, HTTPS url template with variables, header allowlist with non-empty names and a payload schema with kinds, required flags and defaults. */\nexport function validateendpointrecord(value: unknown): policyevaluation {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"A reviewed endpoint record is required.\" };\n const record = value as Record<string, unknown>;\n if (!isnonempty(record.name)) return { allowed: false, reason: \"The endpoint record needs a reviewed non-empty name.\" };\n if (!isnonempty(record.method)) return { allowed: false, reason: \"The endpoint record needs a reviewed method.\" };\n if (!ishttpsurl(record.url)) return { allowed: false, reason: \"The endpoint record url template must be an HTTPS url.\" };\n if (record.headers !== undefined) {\n if (!record.headers || typeof record.headers !== \"object\" || Array.isArray(record.headers)) return { allowed: false, reason: \"The endpoint header allowlist must be an object of reviewed headers.\" };\n for (const name of Object.keys(record.headers as Record<string, unknown>)) {\n if (!name.trim()) return { allowed: false, reason: \"Endpoint header allowlists with empty names are refused.\" };\n const headervalue = (record.headers as Record<string, unknown>)[name];\n if (typeof headervalue !== \"string\") return { allowed: false, reason: `The endpoint header ${name} needs a reviewed string value.` };\n }\n }\n const schema = record.schema;\n if (!schema || typeof schema !== \"object\" || Array.isArray(schema)) return { allowed: false, reason: \"Every typed endpoint call needs a reviewed payload schema; endpoint records without schemas are refused.\" };\n const fields = (schema as Record<string, unknown>).fields;\n if (!Array.isArray(fields) || fields.length === 0) return { allowed: false, reason: \"The endpoint payload schema needs a non-empty field list.\" };\n for (const item of fields) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) return { allowed: false, reason: \"Every payload schema field must be an object.\" };\n const field = item as Record<string, unknown>;\n if (!isnonempty(field.name)) return { allowed: false, reason: \"Every payload schema field needs a non-empty name.\" };\n if (field.kind !== \"string\" && field.kind !== \"number\" && field.kind !== \"boolean\") return { allowed: false, reason: `The payload schema field ${field.name} must be a string, number or boolean kind.` };\n if (field.required !== undefined && typeof field.required !== \"boolean\") return { allowed: false, reason: `The payload schema field ${field.name} required flag must be a boolean.` };\n if (field.default !== undefined && typeof field.default !== \"string\" && typeof field.default !== \"number\" && typeof field.default !== \"boolean\") return { allowed: false, reason: `The payload schema field ${field.name} default must match its kind.` };\n }\n return { allowed: true };\n}\n\n/** Validates one dotted json path against the path grammar: non-empty segments of names, digits, underscores or hyphens. */\nfunction validpath(path: string): boolean {\n return path.split(\".\").every(segment => /^[A-Za-z0-9_-]+$/.test(segment));\n}\n\n/** Validates the reviewed network observation parameter grammar of the 1.1.42 family: fetch requests with header allowlists, fetch policies with timeout, retries, backoff and follow limit, stream budgets, dotted json paths, html queries, graphql operations and typed endpoint references; every bound stays a user choice with no code ceiling. */\nfunction validatehttpgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"fetchurl\") {\n const request = options.fetch;\n if (!request || typeof request !== \"object\" || Array.isArray(request)) return { allowed: false, reason: \"A reviewed fetch request with a url is required in options.fetch.\" };\n const fetchrequest = request as Record<string, unknown>;\n if (typeof fetchrequest.url !== \"string\" || !fetchrequest.url.trim()) return { allowed: false, reason: \"The reviewed fetch request needs a non-empty url.\" };\n if (fetchrequest.method !== undefined && (typeof fetchrequest.method !== \"string\" || ![\"GET\", \"HEAD\", \"POST\", \"PUT\", \"PATCH\", \"DELETE\", \"OPTIONS\"].includes(fetchrequest.method.trim().toUpperCase()))) return { allowed: false, reason: \"The reviewed fetch method must be a known HTTP verb.\" };\n if (fetchrequest.headers !== undefined) {\n if (!fetchrequest.headers || typeof fetchrequest.headers !== \"object\" || Array.isArray(fetchrequest.headers)) return { allowed: false, reason: \"The reviewed header allowlist must be an object of custom headers.\" };\n for (const name of Object.keys(fetchrequest.headers as Record<string, unknown>)) {\n if (!name.trim()) return { allowed: false, reason: \"Header allowlists with empty names are refused.\" };\n if (typeof (fetchrequest.headers as Record<string, unknown>)[name] !== \"string\") return { allowed: false, reason: `The reviewed header ${name} needs a string value.` };\n }\n }\n if (fetchrequest.body !== undefined && typeof fetchrequest.body !== \"string\") return { allowed: false, reason: \"The reviewed fetch body must be a string.\" };\n if (fetchrequest.mode !== undefined && fetchrequest.mode !== \"cors\" && fetchrequest.mode !== \"no-cors\" && fetchrequest.mode !== \"same-origin\") return { allowed: false, reason: \"The reviewed fetch mode must be cors, no-cors or same-origin.\" };\n const consentgate = fetchconsentrefgranted(step);\n if (!consentgate.allowed) return consentgate;\n const policycheck = validatefetchoptions(options.fetchoptions);\n if (!policycheck.allowed) return policycheck;\n const fetchpolicy = fetchoptionsvalues(options.fetchoptions);\n const budget = fetchbudgetallowed(fetchpolicy.timeout, fetchpolicy.retries, fetchpolicy.backoff, fetchnumeric(options, \"wait\"));\n if (!budget.allowed) return budget;\n if (options.stream !== undefined) {\n if (!options.stream || typeof options.stream !== \"object\" || Array.isArray(options.stream)) return { allowed: false, reason: \"The reviewed stream window must be an object with an optional byte budget.\" };\n const streambudget = (options.stream as Record<string, unknown>).budget;\n if (streambudget !== undefined && (typeof streambudget !== \"number\" || !Number.isFinite(streambudget) || streambudget < 0)) return { allowed: false, reason: \"The reviewed stream byte budget must be zero or a positive number of bytes with no code ceiling.\" };\n }\n }\n if (kind === \"parsejson\") {\n if (!isnonempty(options.call)) return { allowed: false, reason: \"A reviewed stored call id is required in options.call before the body parses.\" };\n const fields = options.fields;\n if (!Array.isArray(fields) || fields.length === 0) return { allowed: false, reason: \"A reviewed non-empty list of json path rules is required in options.fields.\" };\n for (const item of fields) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) return { allowed: false, reason: \"Every json path rule must be an object.\" };\n const rule = item as Record<string, unknown>;\n if (!isnonempty(rule.name)) return { allowed: false, reason: \"Every json path rule needs a non-empty field name.\" };\n if (typeof rule.path !== \"string\" || !rule.path.trim() || !validpath(rule.path.trim())) return { allowed: false, reason: `The json path of ${rule.name} must be a dotted path of non-empty segments.` };\n if (rule.kind !== undefined && rule.kind !== \"text\" && rule.kind !== \"number\" && rule.kind !== \"boolean\" && rule.kind !== \"json\") return { allowed: false, reason: `The json path kind of ${rule.name} must be text, number, boolean or json.` };\n }\n }\n if (kind === \"parsehtml\") {\n if (!isnonempty(options.call)) return { allowed: false, reason: \"A reviewed stored call id is required in options.call before the markup parses.\" };\n const queries = options.queries;\n if (!Array.isArray(queries) || queries.length === 0) return { allowed: false, reason: \"A reviewed non-empty list of html queries is required in options.queries.\" };\n for (const item of queries) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) return { allowed: false, reason: \"Every html query must be an object.\" };\n const query = item as Record<string, unknown>;\n if (!isnonempty(query.selector)) return { allowed: false, reason: \"Every html query needs a selector from the reviewed selector grammar.\" };\n if (query.attribute !== undefined && !isnonempty(query.attribute)) return { allowed: false, reason: \"The reviewed html query attribute must be a non-empty attribute name.\" };\n if (query.multi !== undefined && typeof query.multi !== \"boolean\") return { allowed: false, reason: \"The reviewed html query multi flag must be a boolean.\" };\n }\n }\n if (kind === \"callrest\" || kind === \"callgraphql\") {\n if (!isnonempty(options.endpoint)) return { allowed: false, reason: \"A reviewed typed endpoint name is required in options.endpoint.\" };\n if (kind === \"callrest\") {\n if (options.payload !== undefined && (!options.payload || typeof options.payload !== \"object\" || Array.isArray(options.payload))) return { allowed: false, reason: \"The reviewed rest payload must be an object of reviewed values.\" };\n if (options.method !== undefined && (typeof options.method !== \"string\" || ![\"GET\", \"HEAD\", \"POST\", \"PUT\", \"PATCH\", \"DELETE\", \"OPTIONS\"].includes(options.method.trim().toUpperCase()))) return { allowed: false, reason: \"The reviewed endpoint method override must be a known HTTP verb.\" };\n if (options.success !== undefined && (!Array.isArray(options.success) || !options.success.every(code => typeof code === \"number\" && Number.isInteger(code)))) return { allowed: false, reason: \"The reviewed success status list must be a list of integer status codes.\" };\n }\n if (kind === \"callgraphql\") {\n const request = options.graphql;\n if (!request || typeof request !== \"object\" || Array.isArray(request)) return { allowed: false, reason: \"A reviewed graphql request with an operation is required in options.graphql.\" };\n const graphql = request as Record<string, unknown>;\n if (typeof graphql.query !== \"string\" || !graphql.query.trim()) return { allowed: false, reason: \"The reviewed graphql operation text must be a non-empty string.\" };\n if (graphql.operationkind !== \"query\" && graphql.operationkind !== \"mutation\") return { allowed: false, reason: \"The reviewed graphql operation kind must be query or mutation; unknown operation kinds are refused.\" };\n if (graphql.variables !== undefined && (!graphql.variables || typeof graphql.variables !== \"object\" || Array.isArray(graphql.variables))) return { allowed: false, reason: \"The reviewed graphql variables must be an object of reviewed values.\" };\n if (graphql.operationname !== undefined && !isnonempty(graphql.operationname)) return { allowed: false, reason: \"The reviewed graphql operation name must be a non-empty string.\" };\n }\n if (options.apikeys !== undefined && (!Array.isArray(options.apikeys) || !options.apikeys.every(name => isnonempty(name)))) return { allowed: false, reason: \"The reviewed api key reference list must be a list of non-empty stored names.\" };\n const policycheck = validatefetchoptions(options.fetchoptions);\n if (!policycheck.allowed) return policycheck;\n const fetchpolicy = fetchoptionsvalues(options.fetchoptions);\n const budget = fetchbudgetallowed(fetchpolicy.timeout, fetchpolicy.retries, fetchpolicy.backoff, fetchnumeric(options, \"wait\"));\n if (!budget.allowed) return budget;\n }\n return { allowed: true };\n}\n\n/** Validates one reviewed fetch policy object: timeout, retries, backoff base and redirect follow limit stay user choices with no code ceiling. */\nfunction validatefetchoptions(value: unknown): policyevaluation {\n if (value === undefined) return { allowed: true };\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return { allowed: false, reason: \"The reviewed fetch options must be an object with timeout, retries, backoff and follow.\" };\n const options = value as Record<string, unknown>;\n for (const key of [\"timeout\", \"backoff\"]) {\n if (options[key] !== undefined && (typeof options[key] !== \"number\" || !Number.isFinite(options[key]) || options[key] < 0)) return { allowed: false, reason: `The reviewed fetch ${key} must be zero or a positive number with no code ceiling.` };\n }\n for (const key of [\"retries\", \"follow\"]) {\n if (options[key] !== undefined && (typeof options[key] !== \"number\" || !Number.isInteger(options[key]) || options[key] < 0)) return { allowed: false, reason: `The reviewed fetch ${key} must be zero or a positive integer with no code ceiling.` };\n }\n return { allowed: true };\n}\n\n/** Reads the numeric fetch policy fields of one reviewed fetch options object. */\nfunction fetchoptionsvalues(value: unknown): { timeout?: number | undefined; retries?: number | undefined; backoff?: number | undefined } {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return {};\n const options = value as Record<string, unknown>;\n return { timeout: fetchnumeric(options, \"timeout\"), retries: fetchnumeric(options, \"retries\"), backoff: fetchnumeric(options, \"backoff\") };\n}\n\n/** Reads one numeric fetch policy field from the step options. */\nfunction fetchnumeric(options: Record<string, unknown>, key: string): number | undefined {\n const value = options[key];\n return typeof value === \"number\" && Number.isFinite(value) ? value : undefined;\n}\n\n/** True when the kind records user activity and needs the reviewed recording consent before it starts. */\nexport function isrecordingkind(kind: actionkind): boolean {\n return kind === \"recordscreen\" || kind === \"captureaudio\";\n}\n\n/** Validates the reviewed socket and stream parameter grammar of the 1.1.43 family: channel urls with protocols, reconnect budgets and backoff ceilings, multiplexed message payloads, message filters with dotted paths and match limits, event subscriptions with cancellation paths and long poll cursors with intervals kept inside the reviewed wait budget; every bound stays a user choice with no code ceiling. */\nfunction validatesocketgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"opensocket\") {\n const channel = channeloptionsof(options.socket);\n if (!channel) return { allowed: false, reason: \"A reviewed socket with a url is required in options.socket.\" };\n if (channel.options.reconnect !== undefined && !Number.isInteger(channel.options.reconnect)) return { allowed: false, reason: \"The reviewed socket reconnect budget must be an integer attempt count with no code ceiling.\" };\n for (const label of [\"backoff\", \"backoffceiling\"] as const) {\n const value = channel.options[label];\n if (value !== undefined && (typeof value !== \"number\" || !Number.isFinite(value) || value < 0)) return { allowed: false, reason: `The reviewed socket ${label} must be zero or a positive number of milliseconds with no code ceiling.` };\n }\n if (channel.options.lifetime !== undefined && (typeof channel.options.lifetime !== \"number\" || !Number.isFinite(channel.options.lifetime) || channel.options.lifetime <= 0)) return { allowed: false, reason: \"The reviewed socket lifetime window must be a positive number of milliseconds.\" };\n }\n if (kind === \"sendmessage\") {\n const message = options.message;\n if (!message || typeof message !== \"object\" || Array.isArray(message)) return { allowed: false, reason: \"A reviewed message with a channel, stream and payload is required in options.message.\" };\n const envelope = message as Record<string, unknown>;\n if (!isnonempty(envelope.channel)) return { allowed: false, reason: \"The reviewed message needs the open channel id in options.message.channel.\" };\n if (envelope.stream !== undefined && !isnonempty(envelope.stream)) return { allowed: false, reason: \"The reviewed message stream name must be a non-empty string.\" };\n if (typeof envelope.payload !== \"string\") return { allowed: false, reason: \"The reviewed message payload must be a string.\" };\n }\n if (kind === \"waitmessage\") {\n if (options.filter !== undefined) {\n const filter = options.filter;\n if (!filter || typeof filter !== \"object\" || Array.isArray(filter)) return { allowed: false, reason: \"The reviewed message filter must be an object of stream, path and limit.\" };\n const reviewed = filter as Record<string, unknown>;\n if (reviewed.stream !== undefined && !isnonempty(reviewed.stream)) return { allowed: false, reason: \"The reviewed message filter stream name must be a non-empty string.\" };\n if (reviewed.path !== undefined && (typeof reviewed.path !== \"string\" || !validpath(reviewed.path.trim()))) return { allowed: false, reason: \"The reviewed message filter path must be a dotted path of non-empty segments.\" };\n if (reviewed.limit !== undefined && (typeof reviewed.limit !== \"number\" || !Number.isInteger(reviewed.limit) || reviewed.limit < 1)) return { allowed: false, reason: \"The reviewed message match limit must be a positive integer with no code ceiling.\" };\n }\n if (options.wait !== undefined && (typeof options.wait !== \"number\" || !Number.isFinite(options.wait) || options.wait < 0)) return { allowed: false, reason: \"The reviewed message wait budget must be zero or a positive number of milliseconds.\" };\n }\n if (kind === \"subscribesse\") {\n const subscription = subscriptionoptionsof(options.subscription);\n if (!subscription) return { allowed: false, reason: \"A reviewed subscription with an event stream url and a cancellation path is required in options.subscription.\" };\n const rawlifetime = options.subscription && typeof options.subscription === \"object\" && !Array.isArray(options.subscription) ? (options.subscription as Record<string, unknown>).lifetime : undefined;\n if (rawlifetime !== undefined && (typeof rawlifetime !== \"number\" || !Number.isFinite(rawlifetime) || rawlifetime <= 0)) return { allowed: false, reason: \"The reviewed subscription lifetime window must be a positive number of milliseconds.\" };\n }\n if (kind === \"longpoll\") {\n const cursor = pollcursorof(options.poll);\n if (!cursor) return { allowed: false, reason: \"A reviewed poll cursor with a url, cursor field, interval and stop condition is required in options.poll.\" };\n const wait = options.wait;\n if (wait !== undefined && (typeof wait !== \"number\" || !Number.isFinite(wait) || wait < 0)) return { allowed: false, reason: \"The reviewed long poll wait budget must be zero or a positive number of milliseconds.\" };\n if (wait !== undefined && cursor.interval > wait) return { allowed: false, reason: `The long poll interval of ${cursor.interval} milliseconds exceeds the reviewed wait budget of ${wait} milliseconds; review a wider budget or a shorter interval.` };\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed request observation parameter grammar of the 1.1.43 family: watch windows with user configured match limits, header filters whose redaction list is required before any header value is stored, body filters with url patterns, mime lists and byte ceilings and api replay specs with known verbs and dotted extraction paths. */\nfunction validatenetwatchgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"watchrequests\") {\n if (options.watch !== undefined) {\n const watch = options.watch;\n if (!watch || typeof watch !== \"object\" || Array.isArray(watch)) return { allowed: false, reason: \"The reviewed watch window must be an object.\" };\n const reviewed = watch as Record<string, unknown>;\n if (reviewed.window !== undefined && (typeof reviewed.window !== \"number\" || !Number.isFinite(reviewed.window) || reviewed.window < 0)) return { allowed: false, reason: \"The reviewed watch window must be zero or a positive number of milliseconds.\" };\n }\n if (options.limit !== undefined && (typeof options.limit !== \"number\" || !Number.isInteger(options.limit) || options.limit < 1)) return { allowed: false, reason: \"The reviewed watch match limit must be a positive integer with no code ceiling.\" };\n }\n if (kind === \"readheaders\") {\n const headers = options.headers;\n if (!headers || typeof headers !== \"object\" || Array.isArray(headers)) return { allowed: false, reason: \"A reviewed header filter with a name allowlist and a redaction list is required in options.headers.\" };\n const reviewed = headers as Record<string, unknown>;\n if (!Array.isArray(reviewed.allow) || reviewed.allow.length === 0 || !reviewed.allow.every((name): name is string => isnonempty(name))) return { allowed: false, reason: \"The reviewed header allowlist must be a non-empty list of header names.\" };\n if (!Array.isArray(reviewed.redact) || reviewed.redact.length === 0 || !reviewed.redact.every((name): name is string => isnonempty(name))) return { allowed: false, reason: \"Header capture requires a reviewed redaction list before any header value is stored.\" };\n }\n if (kind === \"capturebodies\") {\n const body = options.body;\n if (!body || typeof body !== \"object\" || Array.isArray(body)) return { allowed: false, reason: \"A reviewed body filter with a url pattern, mime list and byte ceiling is required in options.body.\" };\n const reviewed = body as Record<string, unknown>;\n if (reviewed.urlpattern !== undefined && !isnonempty(reviewed.urlpattern)) return { allowed: false, reason: \"The reviewed body url pattern must be a non-empty string.\" };\n if (reviewed.mimes !== undefined && (!Array.isArray(reviewed.mimes) || reviewed.mimes.length === 0 || !reviewed.mimes.every((mime): mime is string => isnonempty(mime)))) return { allowed: false, reason: \"The reviewed body mime list must be a non-empty list of mime types.\" };\n if (reviewed.ceiling !== undefined && (typeof reviewed.ceiling !== \"number\" || !Number.isFinite(reviewed.ceiling) || reviewed.ceiling < 0)) return { allowed: false, reason: \"The reviewed body byte ceiling must be zero or a positive number of bytes with no code ceiling.\" };\n }\n if (kind === \"mapapi\") {\n if (options.limit !== undefined && (typeof options.limit !== \"number\" || !Number.isInteger(options.limit) || options.limit < 1)) return { allowed: false, reason: \"The reviewed mapapi match limit must be a positive integer with no code ceiling.\" };\n }\n if (kind === \"extractapi\") {\n const replay = apireplayspecof(options.replay);\n if (!replay) return { allowed: false, reason: \"A reviewed replay spec with an endpoint is required in options.replay.\" };\n if (!ishttpsurl(replay.endpoint)) return { allowed: false, reason: \"The reviewed replay endpoint must be an HTTPS url.\" };\n if (replay.verb !== undefined && ![\"GET\", \"HEAD\", \"POST\", \"PUT\", \"PATCH\", \"DELETE\", \"OPTIONS\"].includes(replay.verb)) return { allowed: false, reason: \"The reviewed replay verb must be a known HTTP verb.\" };\n for (const path of replay.paths ?? []) {\n if (!validpath(path.trim())) return { allowed: false, reason: `The reviewed replay extraction path ${path} must be a dotted path of non-empty segments.` };\n }\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed network control parameter grammar of the 1.1.44 family: block rules with url patterns that name their origin, mock fixtures reviewed with their full body, header rewrite rules with named origin patterns and set, append and remove operations, cookie records scoped to granted domains, oauth flows with provider consent refs, api key entries behind explicit consent, proxy routes with required bypass lists, urlencoded form payloads and multipart uploads whose every file carries the explicit reviewed flag; every bound stays a user choice with no code ceiling. */\nfunction validatecontrolgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"blockrequest\") {\n const rule = blockruleof(options.block);\n if (!rule) return { allowed: false, reason: \"A reviewed block rule with a url pattern is required in options.block.\" };\n if (patternorigin(rule.urlpattern) === undefined) return { allowed: false, reason: \"Block rules need an https origin pattern; patterns without a named origin are refused.\" };\n if ((options.block as Record<string, unknown>).reviewed !== true) return { allowed: false, reason: \"The block rule carries the explicit reviewed flag before any request is blocked.\" };\n }\n if (kind === \"mockresponse\") {\n const spec = mockspecof(options.mock);\n if (!spec) return { allowed: false, reason: \"A reviewed mock fixture with a url pattern, status and its reviewed body or a captured body ref is required in options.mock.\" };\n if (patternorigin(spec.urlpattern) === undefined) return { allowed: false, reason: \"Mock fixtures need an https origin pattern; patterns without a named origin are refused.\" };\n if (spec.reviewed !== true) return { allowed: false, reason: \"Every mock fixture is reviewed with its full body or the referenced captured body through the explicit reviewed flag before it serves.\" };\n }\n if (kind === \"rewriteheaders\") {\n const rules = options.rules;\n if (!Array.isArray(rules) || rules.length === 0) return { allowed: false, reason: \"A reviewed non-empty list of header rewrite rules is required in options.rules.\" };\n for (const item of rules) {\n const rule = headeruleof(item);\n if (!rule) return { allowed: false, reason: \"Every header rewrite rule needs a url pattern, header name, a set, append or remove operation and its value.\" };\n if (patternorigin(rule.urlpattern) === undefined) return { allowed: false, reason: \"Header rewrite rules must name their origin pattern explicitly; patterns without a named origin are refused.\" };\n }\n }\n if (kind === \"setcookies\") {\n const cookies = options.cookies;\n if (!Array.isArray(cookies) || cookies.length === 0) return { allowed: false, reason: \"A reviewed non-empty list of cookie records is required in options.cookies.\" };\n for (const item of cookies) {\n if (!cookierecordof(item)) return { allowed: false, reason: \"Every cookie record needs a name, domain, path and reviewed string value with an optional expiry.\" };\n }\n }\n if (kind === \"readcookies\" && options.domain !== undefined && !isnonempty(options.domain)) return { allowed: false, reason: \"The reviewed cookie read domain must be a non-empty host.\" };\n if (kind === \"clearcookies\") {\n if (!isnonempty(options.domain)) return { allowed: false, reason: \"A reviewed cookie domain is required before cookies are cleared.\" };\n if (options.names !== undefined && (!Array.isArray(options.names) || options.names.length === 0 || !options.names.every((name): name is string => isnonempty(name)))) return { allowed: false, reason: \"The reviewed cookie clear list must be a non-empty list of cookie names when present.\" };\n }\n if (kind === \"authflow\") {\n const flow = oauthflowof(options.oauth);\n if (!flow) return { allowed: false, reason: \"A reviewed oauth flow with provider, authorize url, token url, scopes and redirect origin is required in options.oauth.\" };\n if (!ishttpsurl(flow.authorizeurl) || !ishttpsurl(flow.tokenurl)) return { allowed: false, reason: \"The oauth authorize and token urls must use HTTPS.\" };\n if (!ishttpsurl(flow.redirectorigin) && !/^https:\\/\\/[^/]+\\/?$/.test(flow.redirectorigin)) return { allowed: false, reason: \"The oauth redirect origin must be an HTTPS origin inside the grants.\" };\n const consent = authconsentgranted(step);\n if (!consent.allowed) return consent;\n }\n if (kind === \"saveapikey\") {\n const key = options.key;\n if (!key || typeof key !== \"object\" || Array.isArray(key)) return { allowed: false, reason: \"A reviewed api key entry with name, origin scopes and header is required in options.key.\" };\n const entry = key as Record<string, unknown>;\n if (!isnonempty(entry.name)) return { allowed: false, reason: \"The api key entry needs a reviewed non-empty name.\" };\n if (!Array.isArray(entry.origins) || entry.origins.length === 0 || !entry.origins.every((item): item is string => ishttpsurl(item))) return { allowed: false, reason: \"The api key needs a reviewed non-empty list of HTTPS origin scopes.\" };\n if (!isnonempty(entry.header)) return { allowed: false, reason: \"The api key entry needs a reviewed non-empty header name.\" };\n if (typeof entry.value !== \"string\" || !entry.value) return { allowed: false, reason: \"The api key needs its secret value in the reviewed options; it never enters the audit trail.\" };\n const consent = apikeyconsentgranted(step);\n if (!consent.allowed) return consent;\n }\n if (kind === \"routeproxy\") {\n if (!proxyrouteof(options.proxy)) return { allowed: false, reason: \"A reviewed proxy route with scheme, host, port and a non-empty bypass list is required in options.proxy.\" };\n if (!isnonempty(options.consentref)) return { allowed: false, reason: \"Proxy routing needs the explicit reviewed consent ref before any route applies.\" };\n }\n if (kind === \"postform\") {\n const form = formpayloadof(options.form);\n if (!form) return { allowed: false, reason: \"A reviewed form payload with a url and a non-empty field list is required in options.form.\" };\n if (!ishttpsurl(form.url)) return { allowed: false, reason: \"The form submission target must use HTTPS.\" };\n if (options.wait !== undefined && (typeof options.wait !== \"number\" || !Number.isFinite(options.wait) || options.wait < 0)) return { allowed: false, reason: \"The reviewed rate limit wait budget must be zero or a positive number of milliseconds.\" };\n }\n if (kind === \"postfiles\") {\n const upload = multipartpayloadof(options.upload);\n if (!upload) return { allowed: false, reason: \"A reviewed multipart upload with a url and reviewed files is required in options.upload; every file carries the explicit reviewed flag.\" };\n if (!ishttpsurl(upload.url)) return { allowed: false, reason: \"The multipart upload target must use HTTPS.\" };\n if (options.wait !== undefined && (typeof options.wait !== \"number\" || !Number.isFinite(options.wait) || options.wait < 0)) return { allowed: false, reason: \"The reviewed rate limit wait budget must be zero or a positive number of milliseconds.\" };\n }\n return { allowed: true };\n}\n\n/** Requires the reviewed block rule of a live session before any blockrequest runs: the rule must carry the explicit reviewed flag and the session must stay active, unpaused and unexpired; every rule applies for the run only and reverts at run end. */\nexport function blockgate(session: agentsession | undefined, step: toolstep, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the request block.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot block requests.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot block requests.\" };\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const rule = options.block;\n if (!rule || typeof rule !== \"object\" || Array.isArray(rule) || (rule as Record<string, unknown>).reviewed !== true) return { allowed: false, reason: \"Request blocking needs its reviewed block rule with the explicit reviewed flag before any rule applies.\" };\n if (!blockruleof(rule)) return { allowed: false, reason: \"The block rule needs a url pattern and an optional resource type list.\" };\n return { allowed: true };\n}\n\n/** Scopes every cookie kind to a granted domain of a live session: the domain must equal a granted origin host or sit beneath it, and every other domain is refused. */\nexport function cookiegate(session: agentsession | undefined, domain: string, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the cookie operation.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot touch cookies.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot touch cookies.\" };\n const grants = session.grants ?? [session.origin];\n if (!cookiedomaingranted(domain, grants)) return { allowed: false, reason: `The cookie domain ${domain} stays outside the session origin grants; cookie control refuses domains beyond the grants.` };\n return { allowed: true };\n}\n\n/** Requires the explicit reviewed consent before routeproxy changes routing: a live session, a reviewed consent ref and a valid route with its bypass list; the route applies for the run only and restores the previous state at run end. */\nexport function proxygate(session: agentsession | undefined, step: toolstep, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the proxy route.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot change routing.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot change routing.\" };\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n if (!isnonempty(options.consentref)) return { allowed: false, reason: \"Proxy routing needs the explicit reviewed consent ref before any route applies.\" };\n if (!proxyrouteof(options.proxy)) return { allowed: false, reason: \"The proxy route needs a scheme, host, port and a non-empty bypass list of origins that stay direct.\" };\n return { allowed: true };\n}\n\n/** Requires the reviewed provider consent prompt ref before any authflow runs. */\nexport function authconsentgranted(step: toolstep): policyevaluation {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const consentref = options.consentref;\n if (typeof consentref !== \"string\" || !consentref.trim()) return { allowed: false, reason: \"An oauth flow requires the reviewed provider consent prompt ref in options before it starts.\" };\n return { allowed: true };\n}\n\n/** Requires the explicit consent prompt ref before saveapikey stores a key. */\nexport function apikeyconsentgranted(step: toolstep): policyevaluation {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const consentref = options.consentref;\n if (typeof consentref !== \"string\" || !consentref.trim()) return { allowed: false, reason: \"Storing an api key requires the explicit reviewed consent prompt ref in options before anything is stored.\" };\n return { allowed: true };\n}\n\n/** Keeps the rate limit wait inside the reviewed wait budget as user configured behavior: the wait until the reset window passes must fit when a budget was reviewed; both bounds stay user choices with no code ceiling. */\nexport function ratelimitbudgetallowed(wait: number | undefined, budget: number | undefined): policyevaluation {\n if (wait !== undefined && (typeof wait !== \"number\" || !Number.isFinite(wait) || wait < 0)) return { allowed: false, reason: \"The rate limit wait must be zero or a positive number of milliseconds.\" };\n if (budget !== undefined && (typeof budget !== \"number\" || !Number.isFinite(budget) || budget < 0)) return { allowed: false, reason: \"The reviewed rate limit budget must be zero or a positive number of milliseconds.\" };\n if (wait !== undefined && budget !== undefined && wait > budget) return { allowed: false, reason: `The rate limit wait of ${wait} milliseconds exceeds the reviewed budget of ${budget} milliseconds; review a wider budget or submit later.` };\n return { allowed: true };\n}\n\n/** Requires the active tab grant of the live session for every debugging kind: the timeline gate scopes console, error and task capture to the run tab only and refuses every other tab. */\nexport function timelinegate(session: agentsession | undefined, tabid: number, origin: string, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the timeline capture.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot capture the timeline.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot capture the timeline.\" };\n if (session.tabid !== tabid) return { allowed: false, reason: `The timeline capture needs the run tab ${session.tabid} and refuses tab ${tabid}.` };\n if (!origingranted(session, origin)) return { allowed: false, reason: `The timeline capture of ${origin} needs the session origin grants first.` };\n return { allowed: true };\n}\n\n/** True when one approved console capture consent of that origin exists; console capture on a new origin prompts once and the approved decision persists. */\nexport function consoleconsentcovers(origin: string, consents: consoleconsentrecord[]): policyevaluation {\n if (consents.some(consent => consent.origin === origin && consent.approved === true)) return { allowed: true };\n return { allowed: false, reason: `Console capture on ${origin} needs the reviewed console consent first; approve the prompt in the review panel.` };\n}\n\n/** Requires the granted origin before stack frames are captured; stack capture outside the granted origin is refused. */\nexport function stackgate(session: agentsession | undefined, origin: string): policyevaluation {\n if (!origingranted(session, origin)) return { allowed: false, reason: `Stack capture of ${origin} stays outside the session origin grants.` };\n return { allowed: true };\n}\n\n/** Keeps the debug watch window inside the reviewed wait budget: the watch wait must fit the reviewed budget when one was reviewed; both bounds stay user choices with no code ceiling. */\nexport function debugwaitbudgetallowed(watchwindow: number | undefined, wait: number | undefined): policyevaluation {\n if (watchwindow !== undefined && (typeof watchwindow !== \"number\" || !Number.isFinite(watchwindow) || watchwindow < 0)) return { allowed: false, reason: \"The debug watch window must be zero or a positive number of milliseconds.\" };\n if (wait !== undefined && (typeof wait !== \"number\" || !Number.isFinite(wait) || wait < 0)) return { allowed: false, reason: \"The reviewed debug wait budget must be zero or a positive number of milliseconds.\" };\n if (watchwindow !== undefined && wait !== undefined && watchwindow > wait) return { allowed: false, reason: `The debug watch window of ${watchwindow} milliseconds exceeds the reviewed wait budget of ${wait} milliseconds; review a wider budget or a shorter window.` };\n return { allowed: true };\n}\n\n/** Exposes the timeline retention window as a user configured choice; an absent value keeps every timeline entry forever while the level count summaries always survive. */\nexport function timelineretentionwindow(settings: runsettings | undefined): number | undefined {\n return settings?.timelineretention;\n}\n\n/** Grades console diffing as read only comparison evidence: the diff compares two stored console outputs and touches no page or browser state. */\nexport function diffreviewgrade(): { risk: \"read\"; mode: \"diffing\"; evidence: \"comparison\" } {\n return { risk: \"read\", mode: \"diffing\", evidence: \"comparison\" };\n}\n\n/** Validates the reviewed debugging parameter grammar of the 1.1.45 family: a watch window inside the reviewed wait budget, level floors from the reviewed level set, source filters from the reviewed source grammar, spam rules with user configured thresholds, serialization depth bounds, rotation rules with no hardcoded entry ceiling and the required redaction pattern list before any console text is captured. */\nfunction validatetimelinegrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n let watchwindow: number | undefined;\n if (options.watch !== undefined) {\n const watch = options.watch;\n if (!watch || typeof watch !== \"object\" || Array.isArray(watch)) return { allowed: false, reason: \"The reviewed debug watch window must be an object.\" };\n const reviewed = watch as Record<string, unknown>;\n if (reviewed.window !== undefined) {\n if (typeof reviewed.window !== \"number\" || !Number.isFinite(reviewed.window) || reviewed.window < 0) return { allowed: false, reason: \"The reviewed debug watch window must be zero or a positive number of milliseconds.\" };\n watchwindow = reviewed.window;\n }\n }\n const budgetcheck = debugwaitbudgetallowed(watchwindow, typeof options.wait === \"number\" ? options.wait : undefined);\n if (!budgetcheck.allowed) return budgetcheck;\n if (options.level !== undefined && !loglevels.includes(options.level as loglevel)) return { allowed: false, reason: `The reviewed level floor must be one of ${loglevels.join(\", \")}.` };\n if (options.sources !== undefined) {\n if (!Array.isArray(options.sources) || options.sources.length === 0 || !options.sources.every(source => timelinesources.includes(source as never))) return { allowed: false, reason: `The reviewed source filters must be a non-empty list of the reviewed timeline sources: ${timelinesources.join(\", \")}.` };\n }\n if (kind === \"watchconsole\") {\n if (options.redact === undefined || !Array.isArray(options.redact) || options.redact.length === 0 || !options.redact.every(pattern => isnonempty(pattern))) return { allowed: false, reason: \"Console capture requires a reviewed non-empty redaction pattern list before any console text is captured.\" };\n if (options.depth !== undefined && (typeof options.depth !== \"number\" || !Number.isInteger(options.depth) || options.depth < 1)) return { allowed: false, reason: \"The reviewed serialization depth bound must be a positive integer with no code ceiling.\" };\n if (options.spam !== undefined) {\n const rule = spamruleof(options.spam);\n if (!rule) return { allowed: false, reason: \"The reviewed spam rule needs a pattern, a window size and a collapse threshold.\" };\n if (rule.collapse < 1) return { allowed: false, reason: \"The reviewed spam collapse threshold must be a positive integer of user configured value with no code ceiling.\" };\n }\n if (options.rotation !== undefined) {\n const rule = rotationruleof(options.rotation);\n if (!rule) return { allowed: false, reason: \"The reviewed rotation rule needs a max entry count and an overflow target.\" };\n }\n }\n if (kind === \"watchtasks\") {\n if (options.threshold !== undefined && (typeof options.threshold !== \"number\" || !Number.isFinite(options.threshold) || options.threshold < 0)) return { allowed: false, reason: \"The reviewed long task threshold must be zero or a positive number of milliseconds with no code ceiling.\" };\n }\n return { allowed: true };\n}\n\n/** Normalizes a reviewed spam rule: the pattern, the window size and the collapse threshold as user configured values. */\nexport function spamruleof(value: unknown): spamrule | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const pattern = typeof entry.pattern === \"string\" ? entry.pattern : \"\";\n const windowsize = typeof entry.windowsize === \"number\" && Number.isFinite(entry.windowsize) && entry.windowsize >= 0 ? entry.windowsize : undefined;\n const collapse = typeof entry.collapse === \"number\" && Number.isInteger(entry.collapse) ? entry.collapse : undefined;\n if (windowsize === undefined || collapse === undefined) return undefined;\n return { pattern, windowsize, collapse };\n}\n\n/** Normalizes a reviewed log rotation rule: the max entries per run and the overflow target store with no hardcoded entry ceiling. */\nexport function rotationruleof(value: unknown): rotationrule | undefined {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return undefined;\n const entry = value as Record<string, unknown>;\n const maxentries = typeof entry.maxentries === \"number\" && Number.isInteger(entry.maxentries) && entry.maxentries >= 1 ? entry.maxentries : undefined;\n const overflowtarget = typeof entry.overflowtarget === \"string\" && entry.overflowtarget.trim() ? entry.overflowtarget.trim() : undefined;\n if (maxentries === undefined || overflowtarget === undefined) return undefined;\n return { maxentries, overflowtarget };\n}\n\n/** Requires the active run tab grant of the live session for every devtools protocol kind: the debug gate scopes attaches, commands, watches, breakpoints, steps and overrides to the run tab only and refuses every other tab. */\nexport function debuggate(session: agentsession | undefined, tabid: number, origin: string, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the devtools protocol step.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot run a devtools protocol step.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot run a devtools protocol step.\" };\n if (session.tabid !== tabid) return { allowed: false, reason: `The devtools protocol step needs the run tab ${session.tabid} and refuses tab ${tabid}.` };\n if (!origingranted(session, origin)) return { allowed: false, reason: `The devtools protocol step on ${origin} needs the session origin grants first.` };\n return { allowed: true };\n}\n\n/** True when one approved debugger consent of that origin covers every requested domain; the first attachcdp of a run needs the approved record and revocation removes the coverage. */\nexport function debuggerconsentcovers(origin: string, domains: string[], grants: debuggergrant[]): policyevaluation {\n const needed = [...new Set(domains)];\n const covering = grants.find(grant => grant.origin === origin && grant.approved === true && grant.revokedat === undefined && needed.every(domain => grant.domains.includes(domain)));\n if (covering) return { allowed: true };\n if (grants.some(grant => grant.origin === origin && grant.revokedat !== undefined)) return { allowed: false, reason: `The debugger consent on ${origin} was revoked; approve a new prompt before the devtools protocol runs again.` };\n return { allowed: false, reason: `The devtools protocol on ${origin} needs the reviewed debugger consent for ${needed.join(\", \")} first; approve the prompt with the domain allowlist shown in the review panel.` };\n}\n\n/** The profiling target gate of every 1.1.47 kind: the live session run tab and origin grants come first, every iframe, worker and service worker target stays inside the granted origins, and the reviewed debugger grant of the origin covers every profiling instrument because profiling is debugger grade instrumentation. */\nexport function targetgate(input: { session: agentsession | undefined; tabid: number; origin: string; targets: attachtarget[]; grants: debuggergrant[] | undefined; now: number }): policyevaluation {\n const base = debuggate(input.session, input.tabid, input.origin, input.now);\n if (!base.allowed) return base;\n for (const target of input.targets) {\n if (target.kind === \"page\") continue;\n const origincheckresult = origincheck(input.session, target.url);\n if (!origincheckresult.allowed) return { allowed: false, reason: `The ${target.kind} target ${target.url} stays outside the granted origins; profiling refuses to attach.` };\n }\n if (input.grants === undefined) return { allowed: true };\n const consent = debuggerconsentcovers(input.origin, [], input.grants);\n if (!consent.allowed) return { allowed: false, reason: `The profiling step on ${input.origin} needs the reviewed debugger grant of the origin first; approve the prompt with the profiling derivation shown in the review panel.` };\n return { allowed: true };\n}\n\n/** True when one approved source map capture consent of that origin covers the capture; revocation removes the coverage and the next capture needs a new reviewed prompt. */\nexport function sourcemapconsentcovers(origin: string, consents: sourcemapconsent[]): policyevaluation {\n const covering = consents.find(consent => consent.origin === origin && consent.approved === true && consent.revokedat === undefined);\n if (covering) return { allowed: true };\n if (consents.some(consent => consent.origin === origin && consent.revokedat !== undefined)) return { allowed: false, reason: `The source map capture consent on ${origin} was revoked; approve a new prompt before another map file is fetched.` };\n return { allowed: false, reason: `The source map capture on ${origin} needs the reviewed per origin consent first; approve the prompt shown in the review panel.` };\n}\n\n/** Exposes the user configured retention window for the heavy profile bytes; an absent window keeps every snapshot, sample and trace file. */\nexport function profileretentionwindow(settings: runsettings | undefined): number | undefined {\n return settings?.profileretention;\n}\n\n/** Exposes the user configured trace byte ceiling; an absent value never refuses a trace export because the cap stays a user choice only. */\nexport function traceceilingof(settings: runsettings | undefined): number | undefined {\n return settings?.traceceiling;\n}\n\n/** Validates one breakpoint condition against the reviewed expression grammar: member chains, literals of number, string, boolean and null, comparison and logic operators, negation and parentheses; assignments, calls and statements are refused. */\nexport function validatebreakpointcondition(condition: string): policyevaluation {\n const expression = condition.trim();\n if (expression.length === 0) return { allowed: false, reason: \"The breakpoint condition must not be empty.\" };\n if (/(?<![=!<>])=(?!=)/.test(expression)) return { allowed: false, reason: \"Breakpoint conditions refuse assignment because the reviewed grammar is comparison only.\" };\n if (/[A-Za-z_$][\\w$]*\\s*\\(/.test(expression)) return { allowed: false, reason: \"Breakpoint conditions refuse calls because the reviewed grammar is comparison only.\" };\n const literal = /^(?:-?\\d+(?:\\.\\d+)?|\"(?:[^\"\\\\]|\\\\.)*\"|'(?:[^'\\\\]|\\\\.)*'|true|false|null)$/;\n const tokens = expression.match(/(?:[A-Za-z_$][\\w$]*|-?\\d+(?:\\.\\d+)?|\"(?:[^\"\\\\]|\\\\.)*\"|'(?:[^'\\\\]|\\\\.)*'|===|!==|==|!=|>=|<=|&&|\\|\\||[!.<>()+\\-*\\/%])/g);\n if (tokens === null || tokens.join(\"\") !== expression.replace(/\\s+/g, \"\")) return { allowed: false, reason: \"The breakpoint condition must use the reviewed expression grammar of member chains, literals, comparisons, logic operators, negation and parentheses.\" };\n const identifierlike = /^(?:true|false|null)$/;\n for (const token of tokens) {\n if (literal.test(token) || identifierlike.test(token)) continue;\n if ([\"===\", \"!==\", \"==\", \"!=\", \">=\", \"<=\", \"&&\", \"||\", \"!\", \".\", \"(\", \")\", \"<\", \">\", \"+\", \"-\", \"*\", \"/\", \"%\"].includes(token)) continue;\n if (/^[A-Za-z_$][\\w$]*$/.test(token)) continue;\n return { allowed: false, reason: `The token ${token} of the breakpoint condition stays outside the reviewed expression grammar.` };\n }\n return { allowed: true };\n}\n\n/** Keeps the breakpoint count of one run inside the user configured ceiling: an absent ceiling never refuses a breakpoint because the cap stays a user choice only. */\nexport function breakpointbudgetallowed(active: number, ceiling: number | undefined): policyevaluation {\n if (ceiling === undefined) return { allowed: true };\n if (typeof ceiling !== \"number\" || !Number.isInteger(ceiling) || ceiling < 0) return { allowed: false, reason: \"The reviewed breakpoint ceiling must be zero or a positive integer of user configured value with no code ceiling.\" };\n if (active >= ceiling) return { allowed: false, reason: `The run already holds ${active} active breakpoint${active === 1 ? \"\" : \"s\"} and the reviewed breakpoint ceiling is ${ceiling}; revert one or review a wider ceiling.` };\n return { allowed: true };\n}\n\n/** Exposes the pause capture retention window as a user configured choice; an absent value keeps every pause capture with its call frames. */\nexport function pauseretentionwindow(settings: runsettings | undefined): number | undefined {\n return settings?.pauseretention;\n}\n\n/** Exposes the user configured breakpoint ceiling; an absent value never refuses a breakpoint because the cap stays a user choice only. */\nexport function breakpointceilingof(settings: runsettings | undefined): number | undefined {\n return settings?.breakpointceiling;\n}\n\n/** Requires the review of every emulation layer before it applies: a live session on the run tab, an approved plan, the explicit reviewed flag on the layer options and the reviewed revert plan beside it. */\nexport function emugate(input: { session: agentsession | undefined; plan: agentplan | undefined; step: toolstep; tabid: number; origin: string; now: number }): policyevaluation {\n const gate = sessiongate({ session: input.session, tabid: input.tabid, origin: input.origin, now: input.now, action: \"emulate the run tab\" });\n if (!gate.allowed) return gate;\n if (!input.plan || input.plan.state !== \"approved\") return { allowed: false, reason: \"Emulation layers need an approved plan before they apply.\" };\n let options: Record<string, unknown> = {};\n try { options = parseoptions(input.step); } catch { options = {}; }\n if (options.reviewed !== true) return { allowed: false, reason: `The ${input.step.kind} layer needs the explicit reviewed flag before any mask applies.` };\n if (revertplanof(options.revertplan) === undefined) return { allowed: false, reason: `Every ${input.step.kind} layer needs a reviewed revert plan beside it before any mask applies.` };\n return { allowed: true };\n}\n\n/** Allows layer stacking only when the reviewed plan lists the steps: a second layer of one family needs at least two reviewed steps of that family in the same plan because the last applied layer wins conflicts. */\nexport function emulationstackallowed(plan: agentplan | undefined, kind: actionkind, active: number): policyevaluation {\n if (!plan) return { allowed: false, reason: \"Layer stacking needs the reviewed plan first.\" };\n const listed = plan.steps.filter(step => step.kind === kind).length;\n if (active >= listed) return { allowed: false, reason: `The plan lists ${listed} reviewed ${kind} step${listed === 1 ? \"\" : \"s\"} and ${active} layer${active === 1 ? \"\" : \"s\"} of that family are already active; stacking beyond the reviewed plan is refused.` };\n return { allowed: true };\n}\n\n/** True when one approved location consent of that origin covers the reviewed coordinates; the prompt shows the exact latitude and longitude before emulatelocate applies. */\nexport function locationconsentgate(origin: string, latitude: number, longitude: number, consents: locationconsent[]): policyevaluation {\n if (consents.some(consent => consent.origin === origin && consent.revokedat !== undefined)) return { allowed: false, reason: `The location consent on ${origin} was revoked; approve a new prompt before the location override runs again.` };\n if (locationconsentcovers(origin, latitude, longitude, consents)) return { allowed: true };\n return { allowed: false, reason: `The location override of ${latitude}, ${longitude} on ${origin} needs the reviewed location consent first; approve the prompt with the coordinates shown in the review panel.` };\n}\n\n/** Exposes the user configured retention window for reverted emulation layer states; an absent window keeps every prior state while the layer history always survives. */\nexport function emulationretentionwindow(settings: runsettings | undefined): number | undefined {\n return settings?.emulationretention;\n}\n\n/** Validates the reviewed emulation parameter grammar of the 1.1.48 family: device presets with width, height, pixel ratio and the mobile flag plus the reviewed reload flag, network presets with latency, download and upload bounds and the offline window, location presets inside the latitude and longitude ranges behind the location consent, agent presets of the reviewed user agent grammar with platform and brand list, permission overrides of the reviewed browser permission set graded by name, blackbox rules of explicit origin patterns with their trace scope, and the reviewed revert plan beside every layer. */\nfunction validateemulationgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (revertplanof(options.revertplan) === undefined) return { allowed: false, reason: `Every ${kind} layer needs a reviewed revert plan before any mask applies.` };\n if (kind === \"emulatedevice\") {\n const preset = devicepresetof(options.device);\n if (!preset) return { allowed: false, reason: \"The device layer needs a reviewed preset with a name, positive integer width and height and a positive pixel ratio.\" };\n if (options.reload !== undefined && typeof options.reload !== \"boolean\") return { allowed: false, reason: \"The reviewed reload flag must be a boolean; the page reloads only when the reviewed plan asks.\" };\n return { allowed: true };\n }\n if (kind === \"emulatenetwork\") {\n const preset = networkpresetof(options.network);\n if (!preset) return { allowed: false, reason: \"The network layer needs a reviewed preset with a name and zero or positive latency, download and upload bounds.\" };\n if (options.window !== undefined && (typeof options.window !== \"number\" || !Number.isFinite(options.window) || options.window < 0)) return { allowed: false, reason: \"The reviewed offline window must be zero or a positive number of milliseconds with no code ceiling.\" };\n return { allowed: true };\n }\n if (kind === \"emulatelocate\") {\n const preset = locationpresetof(options.location);\n if (!preset) return { allowed: false, reason: \"The location layer needs a reviewed preset with a name, a latitude inside -90 and 90, a longitude inside -180 and 180 and a zero or positive accuracy radius.\" };\n if (!locationrangevalid(preset.latitude, preset.longitude)) return { allowed: false, reason: \"The reviewed latitude must stay inside -90 and 90 degrees and the longitude inside -180 and 180 degrees.\" };\n return { allowed: true };\n }\n if (kind === \"setuseragent\") {\n const preset = agentpresetof(options.agent);\n if (!preset) return { allowed: false, reason: \"The agent layer needs a reviewed preset with a user agent string of the reviewed grammar, a platform and a non-empty brand list.\" };\n if (!agentgrammarvalid(preset.useragent)) return { allowed: false, reason: \"The reviewed user agent string must use the reviewed grammar of tokens, separators and version marks without line breaks.\" };\n return { allowed: true };\n }\n if (kind === \"overridepermission\") {\n const grant = permissiongrantof(options.permission);\n if (!grant) return { allowed: false, reason: `The permission override needs a reviewed name of the browser permission set (${browserpermissions.join(\", \")}) and a state of ${permissionstates.join(\", \")}.` };\n void permissiongrade(grant.name);\n return { allowed: true };\n }\n if (kind === \"blackboxscripts\") {\n const rules = Array.isArray(options.rules) ? options.rules.flatMap(rule => { const parsed = blackboxruleof(rule); return parsed !== undefined ? [parsed] : []; }) : [];\n if (rules.length === 0) return { allowed: false, reason: \"The blackbox layer needs a reviewed non-empty rule list where every pattern names its origin explicitly and carries a trace scope.\" };\n return { allowed: true };\n }\n return { allowed: true };\n}\n\n/** Validates one permission override name against the reviewed browser permission set. */\nexport function permissionnamevalid(name: string): policyevaluation {\n if (!browserpermissions.includes(name)) return { allowed: false, reason: `The permission ${name} stays outside the reviewed browser permission set: ${browserpermissions.join(\", \")}.` };\n return { allowed: true };\n}\n\n/** Validates the reviewed session parameter grammar of the 1.1.49 memory family: snapshot plans with the scope, the section toggles of the reviewed grammar and the optional auto interval whose period, maximum snapshot count and expiry stay user choices with no code ceiling, restore plans with their tab, form and capture policies behind the explicit restore review, session filings with unique reviewed names and folders, diffs of two saved records, searches with the term grammar and the field set, exports behind the explicit export review and imports of the known file format behind the full record review. */\nfunction validatesessiongrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"persiststate\") {\n if (options.resume !== undefined && typeof options.resume !== \"boolean\") return { allowed: false, reason: \"The reviewed resume flag must be a boolean.\" };\n return { allowed: true };\n }\n if (kind === \"capturesession\") {\n const plan = snapshotplanof(options.snapshot);\n if (!plan) return { allowed: false, reason: \"The session capture needs a reviewed snapshot plan with its scope, a non-empty section list of the reviewed grammar (tabs, scroll, forms, storage, cookies) and the capture link flag.\" };\n if (plan.auto !== undefined) {\n const interval = autointervalof((options.snapshot as Record<string, unknown>).auto);\n if (interval === undefined) return { allowed: false, reason: \"The reviewed auto snapshot interval needs a positive period, a positive maximum snapshot count and a zero or positive expiry window with no code ceiling.\" };\n }\n return { allowed: true };\n }\n if (kind === \"restoresession\") {\n if (typeof options.sessionid !== \"string\" || !options.sessionid.trim()) return { allowed: false, reason: \"The session restore needs the reviewed session id of the saved record.\" };\n if (restoreplanof(options.restore) === undefined) return { allowed: false, reason: \"The session restore needs a reviewed restore plan with its tab, form and capture policies.\" };\n if (options.reviewed !== true) return { allowed: false, reason: \"Every session restore needs the explicit restore review with its tabs, form state and captures listed before it reopens anything.\" };\n return { allowed: true };\n }\n if (kind === \"namedsessions\") {\n if (typeof options.sessionid !== \"string\" || !options.sessionid.trim()) return { allowed: false, reason: \"The session filing needs the reviewed session id of the saved record.\" };\n if (typeof options.name !== \"string\" || !options.name.trim()) return { allowed: false, reason: \"The session filing needs a reviewed non-empty session name.\" };\n if (options.folder !== undefined && (typeof options.folder !== \"string\" || !options.folder.trim())) return { allowed: false, reason: \"The reviewed folder name must be a non-empty string.\" };\n if (options.tags !== undefined && (!Array.isArray(options.tags) || !options.tags.every(tag => typeof tag === \"string\" && tag.trim()))) return { allowed: false, reason: \"The reviewed tag list must be a list of non-empty strings.\" };\n return { allowed: true };\n }\n if (kind === \"diffsessions\") {\n if (typeof options.left !== \"string\" || !options.left.trim() || typeof options.right !== \"string\" || !options.right.trim()) return { allowed: false, reason: \"The session diff needs the reviewed ids of both saved sessions.\" };\n return { allowed: true };\n }\n if (kind === \"searchsessions\") {\n if (searchqueryof(options.query) === undefined) return { allowed: false, reason: \"The session search needs a reviewed query with a non-empty term list, fields of the reviewed grammar (urls, titles, names, text) and an optional time window.\" };\n return { allowed: true };\n }\n if (kind === \"exportsessions\") {\n if (options.reviewed !== true) return { allowed: false, reason: \"Session exports need the explicit export review before any session file leaves the device.\" };\n if (options.ids !== undefined && (!Array.isArray(options.ids) || options.ids.length === 0 || !options.ids.every(id => typeof id === \"string\" && id.trim()))) return { allowed: false, reason: \"The reviewed export id list must be a non-empty list of saved session ids.\" };\n return { allowed: true };\n }\n if (kind === \"importsessions\") {\n if (options.reviewed !== true) return { allowed: false, reason: \"Session imports need the explicit full record review before any record joins the library.\" };\n if (importsessionfile(options.file) === undefined) return { allowed: false, reason: \"The session import needs a reviewed file of the known format version with an intact checksum.\" };\n return { allowed: true };\n }\n return { allowed: true };\n}\n\n/** Requires the explicit restore review flag and the reviewed restore plan before any session restore reopens a tab; the review lists every tab, form state and capture first. */\nexport function restorereviewgranted(step: toolstep): policyevaluation {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n if (restoreplanof(options.restore) === undefined) return { allowed: false, reason: \"Every session restore needs a reviewed restore plan with its tab, form and capture policies.\" };\n if (options.reviewed !== true) return { allowed: false, reason: \"The session restore needs the explicit restore review of its tabs, form state and captures before it reopens anything.\" };\n return { allowed: true };\n}\n\n/** The session consent gate of every session memory step: a live session, an approved plan and the restore review of every restore; crash restore prompts stay inside the same consent model. */\nexport function sessionrestoregate(input: { session: agentsession | undefined; plan: agentplan | undefined; step: toolstep; tabid: number; origin: string; now: number }): policyevaluation {\n const gate = sessiongate({ session: input.session, tabid: input.tabid, origin: input.origin, now: input.now, action: \"run the session memory step\" });\n if (!gate.allowed) return gate;\n if (!input.plan || input.plan.state !== \"approved\") return { allowed: false, reason: \"Session memory steps need an approved plan before they run.\" };\n if (input.step.kind === \"restoresession\") return restorereviewgranted(input.step);\n return { allowed: true };\n}\n\n/** Returns the origins a restore reopens outside the grants so the restore skips and reports them; captures and cookies restore only with their origin grants. */\nexport function restoreoriginsgranted(urls: string[], grants: string[]): { allowed: boolean; skippedorigins: string[] } {\n const covered = new Set(grants);\n const skippedorigins: string[] = [];\n for (const url of urls) {\n let origin = \"\";\n try { origin = new URL(url).origin; } catch { origin = \"\"; }\n if (!origin || !covered.has(origin)) skippedorigins.push(origin || url);\n }\n return { allowed: skippedorigins.length === 0, skippedorigins: [...new Set(skippedorigins)] };\n}\n\n/** Requires session names to stay unique inside the library so a filing never shadows another saved session. */\nexport function sessionnameunique(name: string, records: Array<{ id: string; name: string }>, recordid?: string): policyevaluation {\n if (records.some(record => record.name === name && record.id !== recordid)) return { allowed: false, reason: `The session name ${name} already exists in the library; review a unique name.` };\n return { allowed: true };\n}\n\n/** Requires folder names to stay unique inside the folder tree so one folder never shadows another. */\nexport function sessionfolderunique(name: string, folders: Array<{ name: string }>): policyevaluation {\n if (folders.some(folder => folder.name === name)) return { allowed: false, reason: `The folder name ${name} already exists in the library; review a unique folder name.` };\n return { allowed: true };\n}\n\n/** Exposes the user configured retention window for saved session sections; an absent window keeps every section and no code ceiling applies. */\nexport function snapshotretentionwindow(settings: runsettings | undefined): number | undefined {\n return settings?.sessionretention;\n}\n\n/** Validates the reviewed workflow parameter grammar of the 1.1.50 and 1.1.51 families: composition with the expanded block list so no step stays hidden, shareable step templates, workflow runs behind the explicit run review, dry runs of the known workflow, jittered delays and element waits of user configured bounds with no code ceiling, expressions whose operators match the operand kinds and result kinds, regex rules of bounded backtracking shapes applied to reviewed text, and the control flow payloads of conditionals, branching, loops with user configured safety bounds, foreach selectors, parallel branches with join policies and try catch with retry and timeout policies whose child kinds all stay inside the reviewed vocabulary. */\nfunction validateworkflowgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"composeworkflow\") {\n const payload = options.workflow;\n if (!payload || typeof payload !== \"object\" || Array.isArray(payload)) return { allowed: false, reason: \"The workflow composition needs the reviewed workflow payload with its name, version, origins, steps and blocks.\" };\n const candidate = payload as Record<string, unknown>;\n if (typeof candidate.name !== \"string\" || !candidate.name.trim()) return { allowed: false, reason: \"The workflow composition needs a reviewed non-empty name.\" };\n if (typeof candidate.version !== \"number\" || !Number.isInteger(candidate.version) || candidate.version < 1) return { allowed: false, reason: \"The workflow version must be a positive integer.\" };\n if (!Array.isArray(candidate.origins) || candidate.origins.length === 0 || !candidate.origins.every(origin => typeof origin === \"string\" && origin.startsWith(\"https://\"))) return { allowed: false, reason: \"The workflow needs at least one granted HTTPS origin so every step stays inside the grants.\" };\n if (!Array.isArray(candidate.steps) || candidate.steps.length === 0 || !candidate.steps.every(entry => workflowstepof(entry) !== undefined || (entry && typeof entry === \"object\" && typeof (entry as Record<string, unknown>).block === \"string\"))) return { allowed: false, reason: \"The workflow needs a non-empty reviewed step list of the workflow step grammar or block invocations.\" };\n const blocks = Array.isArray(candidate.blocks) ? candidate.blocks.flatMap(block => { const parsed = workflowblockof(block); return parsed !== undefined ? [parsed] : []; }) : [];\n if (Array.isArray(candidate.blocks) && blocks.length !== (candidate.blocks as unknown[]).length) return { allowed: false, reason: \"The reviewed block list must carry unique lowercase names, labels and valid child steps.\" };\n try {\n const record = composeworkflow({ name: candidate.name, version: candidate.version, origins: candidate.origins as string[], steps: (candidate.steps as Array<Record<string, unknown>>).map(entry => \"block\" in entry ? { block: entry.block as string, label: typeof entry.label === \"string\" ? entry.label : entry.block as string } : workflowstepof(entry) as workflowstep), blocks, now: 0, kindallowed: candidatekind => { try { actionrisk(candidatekind as actionkind); return true; } catch { return false; } }, riskof: candidatekind => actionrisk(candidatekind as actionkind) });\n const inputs = Array.isArray(candidate.inputs) ? candidate.inputs.flatMap(name => typeof name === \"string\" ? [name] : []) : undefined;\n const checked = validateworkflow(record, { kindallowed: workflowkind => { try { actionrisk(workflowkind as actionkind); return true; } catch { return false; } }, ...(inputs !== undefined ? { inputs } : {}) });\n if (!checked.allowed) return checked;\n } catch (error) {\n return { allowed: false, reason: error instanceof Error ? error.message : \"The workflow payload failed its composition validation.\" };\n }\n return { allowed: true };\n }\n if (kind === \"savetemplate\") {\n const payload = options.template && typeof options.template === \"object\" && !Array.isArray(options.template) ? options.template as Record<string, unknown> : {};\n const template = steptemplateof({ id: \"templatereview\", origin: \"https://example.com\", sharedat: 0, ...payload });\n if (!template) return { allowed: false, reason: \"The step template needs a reviewed name and a valid workflow step it shares across workflows.\" };\n return { allowed: true };\n }\n if (kind === \"runworkflow\") {\n if (typeof options.workflowid !== \"string\" || !options.workflowid.trim()) return { allowed: false, reason: \"The workflow run needs the reviewed id of the composed workflow.\" };\n if (options.reviewed !== true) return { allowed: false, reason: \"Every real workflow run needs the explicit run review with its expanded step list shown before the first step executes.\" };\n if (options.variables !== undefined && (!options.variables || typeof options.variables !== \"object\" || Array.isArray(options.variables) || !Object.values(options.variables).every(value => typeof value === \"string\" || typeof value === \"number\" || typeof value === \"boolean\"))) return { allowed: false, reason: \"The reviewed run variables must be an object of string, number or boolean values.\" };\n return { allowed: true };\n }\n if (kind === \"dryrun\") {\n if (typeof options.workflowid !== \"string\" || !options.workflowid.trim()) return { allowed: false, reason: \"The dry run needs the reviewed id of the composed workflow.\" };\n return { allowed: true };\n }\n if (kind === \"delay\") {\n const delay = options.delay;\n if (!delay || typeof delay !== \"object\" || Array.isArray(delay)) return { allowed: false, reason: \"The delay needs a reviewed base and jitter window in options.\" };\n const reviewed = delay as Record<string, unknown>;\n if (typeof reviewed.base !== \"number\" || !Number.isFinite(reviewed.base) || reviewed.base < 0) return { allowed: false, reason: \"The reviewed delay base must be zero or a positive number of milliseconds.\" };\n if (typeof reviewed.jitter !== \"number\" || !Number.isFinite(reviewed.jitter) || reviewed.jitter < 0) return { allowed: false, reason: \"The reviewed delay jitter window must be zero or a positive number of milliseconds with no code ceiling.\" };\n return { allowed: true };\n }\n if (kind === \"waitelement\") {\n const wait = options.wait;\n if (!wait || typeof wait !== \"object\" || Array.isArray(wait)) return { allowed: false, reason: \"The element wait needs a reviewed selector, timeout and poll interval in options.\" };\n const reviewed = wait as Record<string, unknown>;\n if (typeof reviewed.selector !== \"string\" || !reviewed.selector.trim()) return { allowed: false, reason: \"The element wait needs a reviewed non-empty selector.\" };\n if (typeof reviewed.timeout !== \"number\" || !Number.isFinite(reviewed.timeout) || reviewed.timeout < 0) return { allowed: false, reason: \"The reviewed element wait timeout must be zero or a positive number of milliseconds with no code ceiling.\" };\n if (typeof reviewed.poll !== \"number\" || !Number.isFinite(reviewed.poll) || reviewed.poll < 0) return { allowed: false, reason: \"The reviewed element wait poll interval must be zero or a positive number of milliseconds with no code ceiling.\" };\n return { allowed: true };\n }\n if (kind === \"compute\") {\n const expression = expressionof(options.expression);\n if (!expression) return { allowed: false, reason: `The expression step needs a reviewed expression with operands, an operator of the reviewed set (${expressionoperators.join(\", \")}) and a result variable of a reviewed kind.` };\n const operatorcheck = validatexpressionoperators(expression);\n if (!operatorcheck.allowed) return operatorcheck;\n return { allowed: true };\n }\n if (kind === \"extractvars\") {\n const rule = regexruleof(options.rule);\n if (!rule) return { allowed: false, reason: \"The variable extraction needs a reviewed regex rule with its pattern, flags and named capture groups.\" };\n const shapecheck = validateregexrule(rule.pattern);\n if (!shapecheck.allowed) return shapecheck;\n if (typeof options.text !== \"string\") return { allowed: false, reason: \"The variable extraction needs the reviewed text the regex rule applies to.\" };\n return { allowed: true };\n }\n if (kind === \"condition\") {\n const condition = conditionof(options.condition);\n if (!condition) return { allowed: false, reason: \"The condition step needs a reviewed boolean expression in its options.\" };\n const operatorcheck = validatexpressionoperators(condition.expression);\n if (!operatorcheck.allowed) return operatorcheck;\n return { allowed: true };\n }\n if (kind === \"branch\") {\n const branch = branchof(options.branch);\n if (!branch) return { allowed: false, reason: \"The branch step needs reviewed unique paths with boolean match expressions and an else path in its options so every branch terminates.\" };\n for (const path of [...branch.paths, branch.else]) {\n if (path.when === undefined) continue;\n const operatorcheck = validatexpressionoperators(path.when);\n if (!operatorcheck.allowed) return operatorcheck;\n }\n return controlchildkinds(step);\n }\n if (kind === \"loop\") {\n const loop = loopof(options.loop);\n if (!loop) return { allowed: false, reason: \"The loop step needs a reviewed list variable, distinct item and index variables, an optional positive safety bound and a non-empty body in its options; an absent bound keeps the documented default.\" };\n return controlchildkinds(step);\n }\n if (kind === \"repeatuntil\") {\n const repeat = repeatuntilof(options.repeatuntil);\n if (!repeat) return { allowed: false, reason: \"The repeat until step needs a reviewed convergence expression, an optional positive safety bound and a non-empty body in its options.\" };\n const operatorcheck = validatexpressionoperators(repeat.until);\n if (!operatorcheck.allowed) return operatorcheck;\n return controlchildkinds(step);\n }\n if (kind === \"whileloop\") {\n const condition = whileof(options.while);\n if (!condition) return { allowed: false, reason: \"The while step needs a reviewed condition, a mandatory positive safety bound and a non-empty body in its options; a while loop without a safety bound is refused.\" };\n const operatorcheck = validatexpressionoperators(condition.while);\n if (!operatorcheck.allowed) return operatorcheck;\n return controlchildkinds(step);\n }\n if (kind === \"foreach\") {\n const foreach = foreachof(options.foreach);\n if (!foreach) return { allowed: false, reason: \"The foreach step needs a reviewed non-empty selector, distinct item and index variables and a non-empty body in its options.\" };\n return controlchildkinds(step);\n }\n if (kind === \"parallel\") {\n const parallel = parallelof(options.parallel);\n if (!parallel) return { allowed: false, reason: \"The parallel step needs uniquely identified branches with bodies and a join policy of the first, last or fail strategy with cancel or continue on branch failure in its options.\" };\n return controlchildkinds(step);\n }\n if (kind === \"trycatch\") {\n const fragile = tryof(options.try);\n if (!fragile) return { allowed: false, reason: \"The try step needs a fragile body, a catch handler and optional retry and timeout policies in its options: attempts stay user configured with no code ceiling, backoff is fixed or exponential and budgets are positive.\" };\n return controlchildkinds(step);\n }\n return { allowed: true };\n}\n\n/** Checks every child step of a control payload against the reviewed action vocabulary so no control construct hides an unreviewed kind behind its body. */\nfunction controlchildkinds(step: toolstep): policyevaluation {\n const children = controlsteps({ id: step.id, kind: step.kind, label: step.summary, ...(step.options !== undefined ? { options: step.options } : {}) });\n for (const child of children) {\n try { actionrisk(child.kind); } catch { return { allowed: false, reason: `The ${child.kind} step inside the control payload of the ${step.kind} step is not a reviewed action kind.` }; }\n }\n return { allowed: true };\n}\n\n/** Rejects unbounded backtracking shapes of reviewed regex patterns: a quantified group whose body itself ends with an unbounded quantifier can explode on adversarial text, so the shape is refused while bounded repetitions stay user choices. */\nexport function validateregexrule(pattern: string): policyevaluation {\n try { new RegExp(pattern); } catch { return { allowed: false, reason: \"The reviewed regex pattern does not compile.\" }; }\n const nestedquantifier = /\\((?:[^()\\\\]|\\\\.)*[+*}]\\)[+*{]/.test(pattern) || /\\(\\)[+*{]/.test(pattern);\n if (nestedquantifier) return { allowed: false, reason: \"The reviewed regex pattern nests an unbounded quantifier inside a quantified group and is refused because adversarial text could explode the backtracking.\" };\n const unboundedrepeat = /\\{\\d+,\\}/.test(pattern);\n if (unboundedrepeat && /\\([^)]*\\{\\d+,\\}[^)]*\\)[+*{]/.test(pattern)) return { allowed: false, reason: \"The reviewed regex pattern repeats an unbounded group and is refused because adversarial text could explode the backtracking.\" };\n return { allowed: true };\n}\n\n/** Validates the reviewed expression operators against the operand kinds and the result kind: arithmetic needs numbers and returns numbers, logic needs booleans and returns booleans, comparison needs numbers and returns booleans, text operators return strings or booleans and length returns a number. */\nfunction validatexpressionoperators(expression: import(\"./types.js\").expressiontype): policyevaluation {\n const numeric = new Set([\"add\", \"subtract\", \"multiply\", \"divide\", \"modulo\"]);\n const logic = new Set([\"and\", \"or\", \"not\"]);\n const comparison = new Set([\"less\", \"greater\", \"lessequal\", \"greaterequal\"]);\n const text = new Set([\"concat\", \"contains\"]);\n const operator = expression.operator;\n if (numeric.has(operator)) {\n for (const operand of [expression.left, expression.right]) {\n if (operand === undefined) continue;\n if (operand.literal !== undefined && typeof operand.literal === \"boolean\") return { allowed: false, reason: `The ${operator} operator needs numeric operands; boolean literals are refused.` };\n }\n if (expression.resultkind !== \"number\" && expression.resultkind !== \"string\") return { allowed: false, reason: `The ${operator} operator needs a number result kind.` };\n }\n if (logic.has(operator)) {\n for (const operand of [expression.left, expression.right]) {\n if (operand === undefined) continue;\n if (operand.literal !== undefined && typeof operand.literal !== \"boolean\") return { allowed: false, reason: `The ${operator} operator needs boolean operands; non boolean literals are refused.` };\n }\n if (expression.resultkind !== \"boolean\") return { allowed: false, reason: `The ${operator} operator needs a boolean result kind.` };\n if (operator === \"not\" && expression.right !== undefined) return { allowed: false, reason: \"The not operator takes one operand only.\" };\n }\n if (comparison.has(operator) && expression.resultkind !== \"boolean\") return { allowed: false, reason: `The ${operator} operator needs a boolean result kind.` };\n if (text.has(operator) && expression.resultkind !== \"boolean\" && expression.resultkind !== \"string\") return { allowed: false, reason: `The ${operator} operator needs a string or boolean result kind.` };\n if (operator === \"contains\" && expression.resultkind !== \"boolean\") return { allowed: false, reason: \"The contains operator needs a boolean result kind.\" };\n if (operator === \"length\") {\n if (expression.right !== undefined) return { allowed: false, reason: \"The length operator takes one operand only.\" };\n if (expression.resultkind !== \"number\") return { allowed: false, reason: \"The length operator needs a number result kind.\" };\n }\n if ((operator === \"equal\" || operator === \"notequal\") && !new Set([\"boolean\", \"string\", \"number\"]).has(expression.resultkind)) return { allowed: false, reason: \"The equality operator needs a primitive result kind.\" };\n return { allowed: true };\n}\n\n/** The workflow consent gate: a live session, an approved plan and the explicit run review of every real run; dry runs stay read only inside the same session and plan gates. */\nexport function workflowgate(input: { session: agentsession | undefined; plan: agentplan | undefined; step: toolstep; tabid: number; origin: string; now: number }): policyevaluation {\n const gate = sessiongate({ session: input.session, tabid: input.tabid, origin: input.origin, now: input.now, action: \"run the workflow step\" });\n if (!gate.allowed) return gate;\n if (!input.plan || input.plan.state !== \"approved\") return { allowed: false, reason: \"Workflow steps need the approved plan review before they run.\" };\n if (input.step.kind === \"runworkflow\") {\n let runoptions: Record<string, unknown> = {};\n try { runoptions = parseoptions(input.step); } catch { runoptions = {}; }\n if (runoptions.reviewed !== true) return { allowed: false, reason: \"Every real workflow run needs the explicit run review with its expanded step list shown before the first step executes.\" };\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed trigger parameter grammar of the 1.1.52 family: every kind arms exactly one rule behind the explicit arm review, the workflow reference must name a composed workflow, the match payloads follow their family grammar \u2014 visit origins and url list entries must be HTTPS urls, url patterns must parse as HTTPS globs, cron expressions must parse as five field schedules with named weekdays and months and a resolvable timezone, interval periods stay positive with zero or positive jitter, webhook secrets must clear the documented entropy floor with a non-empty payload schema, event names must come from the observed event catalog and context menu titles stay non-empty \u2014 while cooldown windows stay user configured positive values with the documented default of the webhook and event families winning only when the review configures none. */\nfunction validatetriggergrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const family = triggerfamilyof(step.kind);\n if (family === undefined) return { allowed: false, reason: \"The trigger step is not a reviewed trigger kind.\" };\n if (typeof options.workflowid !== \"string\" || !options.workflowid.trim()) return { allowed: false, reason: \"Every trigger rule needs the reviewed id of the composed workflow it launches.\" };\n if (options.reviewed !== true) return { allowed: false, reason: \"Every trigger rule needs the explicit arm review with its match fields and bound workflow shown before it arms.\" };\n if (options.label !== undefined && (typeof options.label !== \"string\" || !options.label.trim())) return { allowed: false, reason: \"The reviewed trigger label must be a non-empty string.\" };\n if (options.cooldown !== undefined && (typeof options.cooldown !== \"number\" || !Number.isFinite(options.cooldown) || options.cooldown <= 0)) return { allowed: false, reason: \"The reviewed cooldown window must be a positive number of milliseconds with no code ceiling; the webhook and event families keep the documented default when the review configures none.\" };\n const payload = options.rule;\n if (!payload || typeof payload !== \"object\" || Array.isArray(payload)) return { allowed: false, reason: `The ${step.kind} step needs its reviewed rule payload in options.` };\n if (triggerpayloadof(family, payload) === undefined) {\n if (family === \"visit\") return { allowed: false, reason: \"The visit rule needs a non-empty reviewed list of HTTPS origins it fires on.\" };\n if (family === \"url\") return { allowed: false, reason: \"The url rule needs a reviewed HTTPS glob url pattern; `*` spans one path segment and `**` spans across segments.\" };\n if (family === \"menu\") return { allowed: false, reason: \"The menu rule needs a reviewed non-empty context menu entry title.\" };\n if (family === \"key\") return { allowed: false, reason: \"The keyboard shortcut rule needs a reviewed lowercase command name and an optional suggested key binding.\" };\n if (family === \"cron\") return { allowed: false, reason: \"The cron rule needs a reviewed five field cron expression of minutes, hours, days, months and weekdays with named weekdays and months and an optional resolvable timezone; unparseable schedules are refused.\" };\n if (family === \"interval\") return { allowed: false, reason: \"The interval rule needs a reviewed positive period in milliseconds with an optional zero or positive jitter window.\" };\n if (family === \"urllist\") return { allowed: false, reason: \"The url list rule needs a reviewed non-empty list of HTTPS urls its workflow runs across.\" };\n if (family === \"webhook\") return { allowed: false, reason: `The webhook rule needs a reviewed shared secret of at least twenty four characters mixing letters and digits and a non-empty payload schema of named string, number or boolean fields.` };\n if (family === \"event\") return { allowed: false, reason: `The page event rule needs a reviewed non-empty list of event names of the observed event catalog: ${triggereventcatalog.join(\", \")}.` };\n return { allowed: false, reason: \"The trigger rule payload does not follow its family grammar.\" };\n }\n if (family === \"cron\") {\n const candidate = payload as Record<string, unknown>;\n if (typeof candidate.cron === \"string\" && cronparse(candidate.cron) === undefined) return { allowed: false, reason: \"The cron expression does not parse as a five field schedule and is refused.\" };\n }\n if (family === \"webhook\") {\n const candidate = payload as Record<string, unknown>;\n if (typeof candidate.secret === \"string\" && !webhooksecretok(candidate.secret)) return { allowed: false, reason: \"The webhook shared secret must hold at least twenty four characters mixing letters and digits; the entropy floor is a floor, never a cap.\" };\n }\n const armed = armrule({ family, workflowid: options.workflowid, ...(typeof options.label === \"string\" && options.label.trim() ? { label: options.label } : {}), payload, ...(typeof options.cooldown === \"number\" ? { cooldown: options.cooldown } : {}), now: 0 });\n if (armed === undefined) return { allowed: false, reason: \"The trigger rule payload does not arm as a reviewed rule.\" };\n return { allowed: true };\n}\n\n/** The trigger consent gate: a live session, an approved plan and the explicit arm review of every rule; automatic launchers never arm outside the consent gates. */\nexport function triggergate(input: { session: agentsession | undefined; plan: agentplan | undefined; step: toolstep; tabid: number; origin: string; now: number }): policyevaluation {\n const gate = sessiongate({ session: input.session, tabid: input.tabid, origin: input.origin, now: input.now, action: \"arm the trigger rule\" });\n if (!gate.allowed) return gate;\n if (!input.plan || input.plan.state !== \"approved\") return { allowed: false, reason: \"Trigger rules need the approved plan review before they arm.\" };\n let triggeroptions: Record<string, unknown> = {};\n try { triggeroptions = parseoptions(input.step); } catch { triggeroptions = {}; }\n if (triggeroptions.reviewed !== true) return { allowed: false, reason: \"Every trigger rule needs the explicit arm review with its match fields and bound workflow shown before it arms.\" };\n return { allowed: true };\n}\n\n/** Returns the match origins of one reviewed trigger rule so callers can keep every rule inside the workflow grant list; triggers on origins outside the grants are refused. */\nexport function triggerorigins(step: toolstep): string[] {\n let triggeroptions: Record<string, unknown> = {};\n try { triggeroptions = parseoptions(step); } catch { return []; }\n const family = triggerfamilyof(step.kind);\n if (family === undefined) return [];\n const armed = armrule({ family, workflowid: typeof triggeroptions.workflowid === \"string\" ? triggeroptions.workflowid : \"\", payload: triggeroptions.rule, ...(typeof triggeroptions.cooldown === \"number\" ? { cooldown: triggeroptions.cooldown } : {}), now: 0 });\n if (armed === undefined) return [];\n const origins: string[] = [];\n for (const origin of armed.origins ?? []) origins.push(origin);\n if (armed.pattern !== undefined) { try { origins.push(new URL(armed.pattern).origin); } catch { /* the pattern grammar already refused unparseable patterns */ } }\n for (const url of armed.urls ?? []) { try { origins.push(new URL(url).origin); } catch { /* the url list grammar already refused unparseable urls */ } }\n return [...new Set(origins)];\n}\n\n/** Returns the read only projection of one workflow step for dry runs: read class steps report their would be outcome while interaction and mutation steps carry no projection and the dry run refuses them; a control step projects only when every child step of its payload grades read. */\nexport function dryrunprojection(step: workflowstep): string | undefined {\n if (iscontrolflowkind(step.kind)) {\n for (const child of controlsteps(step)) {\n const childrisk = resolvedrisk({ id: child.id, kind: child.kind, summary: child.label, risk: \"read\", ...(child.target !== undefined ? { target: child.target } : {}), ...(child.value !== undefined ? { value: child.value } : {}), ...(child.options !== undefined ? { options: child.options } : {}) });\n if (childrisk !== \"read\") return undefined;\n }\n if (step.kind === \"condition\") return \"The condition step would evaluate its reviewed expression over the extracted values with no page side effect.\";\n if (step.kind === \"branch\") return \"The branch step would choose one reviewed path by page state and only the chosen path would run.\";\n if (step.kind === \"loop\") return \"The loop step would iterate its reviewed list binding the item and index variables per iteration inside the safety bound.\";\n if (step.kind === \"repeatuntil\") return \"The repeat until step would rerun its body until the convergence expression holds inside the safety bound.\";\n if (step.kind === \"whileloop\") return \"The while step would loop while its condition holds inside the reviewed safety bound.\";\n if (step.kind === \"foreach\") return \"The foreach step would iterate the elements of its reviewed selector binding the item and index variables per iteration.\";\n if (step.kind === \"parallel\") return \"The parallel step would run its branches concurrently and join their outcomes under the reviewed strategy.\";\n return \"The try step would run its fragile body and only the catch handler on failure.\";\n }\n const risk = resolvedrisk({ id: step.id, kind: step.kind, summary: step.label, risk: \"read\", ...(step.target !== undefined ? { target: step.target } : {}), ...(step.value !== undefined ? { value: step.value } : {}), ...(step.options !== undefined ? { options: step.options } : {}) });\n if (risk !== \"read\") return undefined;\n if (step.kind === \"delay\") return `The delay step would sleep its reviewed base inside the jitter window.`;\n if (step.kind === \"waitelement\") return `The element wait step would poll ${step.target ?? \"the reviewed selector\"} until appearance or the reviewed timeout.`;\n if (step.kind === \"compute\") return `The compute step would evaluate its reviewed expression into the result variable.`;\n if (step.kind === \"extractvars\") return `The variable extraction step would apply its reviewed regex rule and store the named captures.`;\n return `The ${step.kind} step would run read only and mutate nothing.`;\n}\n\n/** Validates one reviewed permission state of an override. */\nexport function permissionstatevalid(state: string): policyevaluation {\n if (!permissionstates.includes(state as permissionstate)) return { allowed: false, reason: `The reviewed permission state must be one of ${permissionstates.join(\", \")}.` };\n return { allowed: true };\n}\n\n/** Validates the reviewed devtools parameter grammar of the 1.1.46 family: enabled domains bounded by the reviewed domain grammar, the required teardown plan of every attach, raw commands of the Domain.method form, domain event rules with match filters inside the reviewed watch window, breakpoints with conditions of the reviewed expression grammar, step modes, reviewed watch expressions, and script overrides with the explicit reviewed flag and a url pattern that names its origin. */\nfunction validatecdpgrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"attachcdp\") {\n if (!Array.isArray(options.domains) || options.domains.length === 0 || !options.domains.every((domain): domain is string => typeof domain === \"string\" && cdpdomains.includes(domain))) return { allowed: false, reason: `The attach needs a non-empty enabled domain list of the reviewed domain grammar: ${cdpdomains.join(\", \")}.` };\n if (teardownplanof(options.teardown) === undefined) return { allowed: false, reason: \"Every attach needs a reviewed teardown plan with its revert steps and resume policy before approval.\" };\n if (options.allowlist !== undefined) {\n const allowlist = cdpallowlistof(options.allowlist);\n if (!allowlist || !allowlist.domains.every(domain => (options.domains as string[]).includes(domain))) return { allowed: false, reason: \"The reviewed method allowlist must stay inside the enabled domains of the attach.\" };\n }\n const budgetcheck = debugwaitbudgetallowed(typeof options.wait === \"number\" ? options.wait : undefined, undefined);\n if (!budgetcheck.allowed) return budgetcheck;\n return { allowed: true };\n }\n if (kind === \"detachcdp\") return { allowed: true };\n if (kind === \"cdpcmd\") {\n const command = options.command && typeof options.command === \"object\" && !Array.isArray(options.command) ? options.command as Record<string, unknown> : undefined;\n if (!command || typeof command.method !== \"string\" || methoddomain(command.method) === undefined) return { allowed: false, reason: \"The raw command needs a reviewed method of the Domain.method form.\" };\n if (command.params !== undefined && (typeof command.params !== \"object\" || Array.isArray(command.params))) return { allowed: false, reason: \"The raw command params must be a JSON object.\" };\n if (command.resultpath !== undefined && typeof command.resultpath !== \"string\") return { allowed: false, reason: \"The reviewed result path must be a dotted path string.\" };\n return { allowed: true };\n }\n if (kind === \"watchcdp\") {\n if (!Array.isArray(options.events) || options.events.length === 0 || !options.events.every(rule => cdpeventruleof(rule) !== undefined)) return { allowed: false, reason: \"The event watch needs a non-empty reviewed list of domain event rules of the reviewed domain grammar.\" };\n let watchwindow: number | undefined;\n if (options.watch !== undefined) {\n const watch = options.watch;\n if (!watch || typeof watch !== \"object\" || Array.isArray(watch)) return { allowed: false, reason: \"The reviewed event watch window must be an object.\" };\n const reviewed = watch as Record<string, unknown>;\n if (reviewed.window !== undefined) {\n if (typeof reviewed.window !== \"number\" || !Number.isFinite(reviewed.window) || reviewed.window < 0) return { allowed: false, reason: \"The reviewed event watch window must be zero or a positive number of milliseconds.\" };\n watchwindow = reviewed.window;\n }\n }\n if (watchwindow === undefined) return { allowed: false, reason: \"The event watch needs a reviewed lifetime window before any domain event is observed.\" };\n const budgetcheck = debugwaitbudgetallowed(watchwindow, typeof options.wait === \"number\" ? options.wait : undefined);\n if (!budgetcheck.allowed) return budgetcheck;\n return { allowed: true };\n }\n if (kind === \"setbreakpoint\") {\n const breakpoint = breakpointinputof(options.breakpoint);\n if (!breakpoint) return { allowed: false, reason: \"The breakpoint needs a reviewed script url and a zero based line.\" };\n if (!ishttpsurl(breakpoint.url)) return { allowed: false, reason: \"The breakpoint script url must be a reviewed HTTPS url.\" };\n if (breakpoint.condition !== undefined) {\n const conditioncheck = validatebreakpointcondition(breakpoint.condition);\n if (!conditioncheck.allowed) return conditioncheck;\n }\n return { allowed: true };\n }\n if (kind === \"stepcode\") {\n if (stepmodeof(options.mode) === undefined) return { allowed: false, reason: \"The step code mode must be one of stepover, stepinto, stepout or resume.\" };\n return { allowed: true };\n }\n if (kind === \"watchexpr\") {\n if (watchexpressionof(options.expression) === undefined) return { allowed: false, reason: \"The watch expression needs the reviewed expression text.\" };\n if (options.reviewed !== true) return { allowed: false, reason: \"Watch expressions must be reviewed before evaluation; set the explicit reviewed flag on the step.\" };\n return { allowed: true };\n }\n if (kind === \"overridescript\") {\n const override = overrideinputof(options.override);\n if (!override) return { allowed: false, reason: \"The script override needs a reviewed url pattern and its full fixture source.\" };\n if (patternorigin(override.urlpattern) === undefined) return { allowed: false, reason: \"Script overrides without a named https origin pattern are refused.\" };\n if (options.reviewed !== true) return { allowed: false, reason: \"The full fixture source must be reviewed before the script override runs; set the explicit reviewed flag on the step.\" };\n return { allowed: true };\n }\n return { allowed: true };\n}\n\n/** Validates the reviewed profiling parameter grammar of the 1.1.47 family: flow specs of the reviewed metric set inside a reviewed watch window, heap snapshots with the user chosen interval only, growth tracking with the reviewed slope, cpu profiles bounded by the reviewed wait budget, layout shift watches with the user chosen window only, trace records bounded by the reviewed category list and byte ceiling, trace annotations that carry step ids, offline replays of stored traces and source map capture scripts of explicit https urls. */\nfunction validateprofilegrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"measureflow\") {\n if (flowspecof(options.flow) === undefined) return { allowed: false, reason: `The flow measurement needs a reviewed flow spec with its mark prefix, step window and metric list of the reviewed metric set: navigation, paint, lcp, fid, interaction, blocking.` };\n const watch = options.watch && typeof options.watch === \"object\" && !Array.isArray(options.watch) ? options.watch as Record<string, unknown> : {};\n if (typeof watch.window !== \"number\" || !Number.isFinite(watch.window) || watch.window < 0) return { allowed: false, reason: \"The flow measurement needs a reviewed watch window of zero or more milliseconds.\" };\n const budgetcheck = debugwaitbudgetallowed(watch.window, typeof options.wait === \"number\" ? options.wait : undefined);\n if (!budgetcheck.allowed) return budgetcheck;\n return { allowed: true };\n }\n if (kind === \"heapshot\") {\n const heap = options.heap && typeof options.heap === \"object\" && !Array.isArray(options.heap) ? options.heap as Record<string, unknown> : {};\n if (heap.interval !== undefined && (typeof heap.interval !== \"number\" || !Number.isFinite(heap.interval) || heap.interval < 0)) return { allowed: false, reason: \"The reviewed heap snapshot interval must be zero or a positive number of milliseconds and stays a user choice with no code ceiling.\" };\n return { allowed: true };\n }\n if (kind === \"trackmemory\") {\n const growth = options.growth && typeof options.growth === \"object\" && !Array.isArray(options.growth) ? options.growth as Record<string, unknown> : undefined;\n if (!growth || typeof growth.slope !== \"number\" || !Number.isFinite(growth.slope) || growth.slope < 0) return { allowed: false, reason: \"Memory growth tracking needs the reviewed slope in bytes per millisecond before any sample is flagged.\" };\n if (growth.interval !== undefined && (typeof growth.interval !== \"number\" || !Number.isFinite(growth.interval) || growth.interval < 0)) return { allowed: false, reason: \"The reviewed sampling interval must be zero or a positive number of milliseconds and stays a user choice with no code ceiling.\" };\n return { allowed: true };\n }\n if (kind === \"profilecpu\") {\n const profile = options.profile && typeof options.profile === \"object\" && !Array.isArray(options.profile) ? options.profile as Record<string, unknown> : undefined;\n if (!profile || typeof profile.duration !== \"number\" || !Number.isFinite(profile.duration) || profile.duration < 0) return { allowed: false, reason: \"The cpu profile needs a reviewed duration of zero or more milliseconds.\" };\n const budgetcheck = debugwaitbudgetallowed(profile.duration, typeof options.wait === \"number\" ? options.wait : undefined);\n if (!budgetcheck.allowed) return budgetcheck;\n return { allowed: true };\n }\n if (kind === \"watchshifts\") {\n const watch = options.watch && typeof options.watch === \"object\" && !Array.isArray(options.watch) ? options.watch as Record<string, unknown> : {};\n if (typeof watch.window !== \"number\" || !Number.isFinite(watch.window) || watch.window < 0) return { allowed: false, reason: \"The layout shift watch needs a reviewed observation window of zero or more milliseconds; the window stays a user choice with no code ceiling.\" };\n if (options.threshold !== undefined && (typeof options.threshold !== \"number\" || !Number.isFinite(options.threshold) || options.threshold < 0)) return { allowed: false, reason: \"The reviewed shift score threshold must be zero or a positive number.\" };\n const budgetcheck = debugwaitbudgetallowed(watch.window, typeof options.wait === \"number\" ? options.wait : undefined);\n if (!budgetcheck.allowed) return budgetcheck;\n return { allowed: true };\n }\n if (kind === \"traceload\") {\n const trace = options.trace && typeof options.trace === \"object\" && !Array.isArray(options.trace) ? options.trace as Record<string, unknown> : undefined;\n if (!trace || !Array.isArray(trace.categories) || trace.categories.length === 0 || !trace.categories.every((category): category is string => typeof category === \"string\" && tracecategories.includes(category))) return { allowed: false, reason: `The trace record needs a non-empty reviewed category list of the reviewed category grammar: ${tracecategories.join(\", \")}.` };\n if (typeof trace.window !== \"number\" || !Number.isFinite(trace.window) || trace.window < 0) return { allowed: false, reason: \"The trace record needs a reviewed window of zero or more milliseconds and stops at the reviewed window end.\" };\n if (trace.exporttarget !== undefined && trace.exporttarget !== \"memory\" && trace.exporttarget !== \"download\") return { allowed: false, reason: \"The trace export target must be memory or download.\" };\n const budgetcheck = debugwaitbudgetallowed(trace.window, typeof options.wait === \"number\" ? options.wait : undefined);\n if (!budgetcheck.allowed) return budgetcheck;\n return { allowed: true };\n }\n if (kind === \"annotatetrace\" || kind === \"replaytrace\") {\n const trace = options.trace && typeof options.trace === \"object\" && !Array.isArray(options.trace) ? options.trace as Record<string, unknown> : undefined;\n if (!trace || typeof trace.traceid !== \"string\" || !trace.traceid.trim()) return { allowed: false, reason: `The ${kind === \"annotatetrace\" ? \"trace annotation\" : \"trace replay\"} needs the stored trace id of a recorded trace.` };\n if (kind === \"replaytrace\") return { allowed: true };\n if (!Array.isArray(options.annotations) || options.annotations.length === 0 || !options.annotations.every(annotation => annotationof(annotation) !== undefined)) return { allowed: false, reason: \"Exported traces carry their step annotations: every annotation needs a step id, a label and an optional offset from the trace start.\" };\n return { allowed: true };\n }\n if (kind === \"capturesourcemaps\") {\n if (options.scripts !== undefined) {\n if (!Array.isArray(options.scripts) || options.scripts.length === 0 || !options.scripts.every((url): url is string => typeof url === \"string\" && ishttpsurl(url))) return { allowed: false, reason: \"The source map capture scripts must be a non-empty list of reviewed HTTPS urls.\" };\n }\n return { allowed: true };\n }\n return { allowed: true };\n}\n\n/** Resolves the reviewed cdp allowlist of one plan: the enabled domains and method gates of its attachcdp step, the reviewable contract every later cdp kind of the plan must stay inside. */\nexport function planallowlist(steps: toolstep[]): cdpallowlist | undefined {\n const attach = steps.find(step => step.kind === \"attachcdp\");\n if (!attach) return undefined;\n let options: Record<string, unknown> = {};\n try { options = parseoptions(attach); } catch { options = {}; }\n const domains = Array.isArray(options.domains) ? options.domains.filter((domain): domain is string => typeof domain === \"string\" && cdpdomains.includes(domain)) : [];\n if (domains.length === 0) return undefined;\n const gated = cdpallowlistof(options.allowlist);\n return { domains, ...(gated?.methods !== undefined ? { methods: gated.methods } : {}) };\n}\n\n/** Resolves the reviewed outbound url of a network control step at review time: the form url of postform, the upload url of postfiles and the token url of authflow. */\nexport function controltarget(step: toolstep): string | undefined {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n for (const key of [\"form\", \"upload\"] as const) {\n const value = options[key];\n if (value && typeof value === \"object\" && !Array.isArray(value)) {\n const url = (value as Record<string, unknown>).url;\n if (typeof url === \"string\" && url.trim()) return url.trim();\n }\n }\n if (step.kind === \"authflow\") {\n const flow = oauthflowof(options.oauth);\n if (flow) return flow.tokenurl;\n }\n return undefined;\n}\n\n/** Resolves the reviewed channel url of a socket step at review time: the socket url of opensocket, the event stream url of subscribesse and the poll url of longpoll. */\nexport function sockettarget(step: toolstep): string | undefined {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n for (const key of [\"socket\", \"subscription\", \"poll\"] as const) {\n const value = options[key];\n if (value && typeof value === \"object\" && !Array.isArray(value)) {\n const url = (value as Record<string, unknown>).url;\n if (typeof url === \"string\" && url.trim()) return url.trim();\n }\n }\n return undefined;\n}\n\n/** Requires the active tab grant of the live session for every media kind: the session tab and origin must match and the origin grant must cover the active origin. */\nexport function mediagate(session: agentsession | undefined, tabid: number, origin: string, now: number): policyevaluation {\n if (!session || session.stoppedat) return { allowed: false, reason: \"No active browser session exists for the media capture.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and cannot capture media.\" };\n if (session.pausedat) return { allowed: false, reason: \"The browser session is paused and cannot capture media.\" };\n if (session.tabid !== tabid) return { allowed: false, reason: `The media capture needs the active tab grant of session tab ${session.tabid} and refuses tab ${tabid}.` };\n if (!origingranted(session, origin)) return { allowed: false, reason: `The media capture of ${origin} needs the session origin grants first.` };\n return { allowed: true };\n}\n\n/** Requires an approved recording consent prompt before any recording of user activity starts; every start consumes its own prompt. */\nexport function recordingconsentgranted(step: toolstep): policyevaluation {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const consentref = options.consentref;\n if (typeof consentref !== \"string\" || !consentref.trim()) return { allowed: false, reason: \"A recording of user activity requires a reviewed consent ref in options before it starts.\" };\n return { allowed: true };\n}\n\n/** Exposes the recording duration window as a user configured choice in milliseconds; an absent window leaves the duration to the reviewed step options with no code ceiling. */\nexport function recordingwindow(settings: runsettings | undefined): number | undefined {\n const window = settings?.recordingwindow;\n return typeof window === \"number\" && Number.isFinite(window) && window > 0 ? window : undefined;\n}\n\n/** Keeps the reviewed lapse plan inside the reviewed wait budget: the whole lapse duration must fit the wait window with no code ceiling on either side. */\nexport function lapsebudgetallowed(interval: number, duration: number, wait: number | undefined): policyevaluation {\n if (!(interval > 0)) return { allowed: false, reason: \"The reviewed lapse interval must be a positive number of milliseconds.\" };\n if (!(duration > 0)) return { allowed: false, reason: \"The reviewed lapse duration must be a positive number of milliseconds.\" };\n if (wait !== undefined && !(wait >= 0)) return { allowed: false, reason: \"The reviewed wait budget must be zero or a positive number of milliseconds.\" };\n if (wait !== undefined && duration > wait) return { allowed: false, reason: `The lapse duration of ${duration} milliseconds exceeds the reviewed wait budget of ${wait} milliseconds; review a wider budget or a shorter duration.` };\n return { allowed: true };\n}\n\n/** Validates the reviewed media capture parameter grammar of the 1.1.41 family: pdf paper sizes, recording scopes and windows, image filters, lapse plans, conversion targets and thumbnail directives stay user choices with no code ceilings. */\nfunction validatemediagrammar(step: toolstep, options: Record<string, unknown>): policyevaluation {\n const kind = step.kind;\n if (kind === \"capturepdf\") {\n const pdf = options.pdf;\n if (pdf !== undefined) {\n if (!pdf || typeof pdf !== \"object\" || Array.isArray(pdf)) return { allowed: false, reason: \"The reviewed pdf options must be an object in options.pdf.\" };\n const pdfoptions = pdf as Record<string, unknown>;\n if (pdfoptions.paperwidth !== undefined && (typeof pdfoptions.paperwidth !== \"number\" || !Number.isFinite(pdfoptions.paperwidth) || pdfoptions.paperwidth <= 0)) return { allowed: false, reason: \"The reviewed pdf paper width must be a positive number of inches with no code cap.\" };\n if (pdfoptions.paperheight !== undefined && (typeof pdfoptions.paperheight !== \"number\" || !Number.isFinite(pdfoptions.paperheight) || pdfoptions.paperheight <= 0)) return { allowed: false, reason: \"The reviewed pdf paper height must be a positive number of inches with no code cap.\" };\n if (pdfoptions.margins !== undefined) {\n const margins = pdfoptions.margins;\n if (!margins || typeof margins !== \"object\" || Array.isArray(margins)) return { allowed: false, reason: \"The reviewed pdf margins must be an object with top, right, bottom and left inches.\" };\n for (const side of [\"top\", \"right\", \"bottom\", \"left\"]) {\n const value = (margins as Record<string, unknown>)[side];\n if (value === undefined) continue;\n if (typeof value !== \"number\" || !Number.isFinite(value) || value < 0) return { allowed: false, reason: `The reviewed pdf ${side} margin must be zero or a positive number of inches; negative margins are refused.` };\n }\n }\n if (pdfoptions.scale !== undefined && (typeof pdfoptions.scale !== \"number\" || !Number.isFinite(pdfoptions.scale) || pdfoptions.scale <= 0)) return { allowed: false, reason: \"The reviewed pdf scale must be a positive number with no code cap.\" };\n if (pdfoptions.landscape !== undefined && typeof pdfoptions.landscape !== \"boolean\") return { allowed: false, reason: \"The reviewed pdf landscape flag must be a boolean.\" };\n if (pdfoptions.paginate !== undefined && typeof pdfoptions.paginate !== \"boolean\") return { allowed: false, reason: \"The reviewed pdf paginate flag must be a boolean.\" };\n }\n if (options.breakpoints !== undefined && (!Array.isArray(options.breakpoints) || options.breakpoints.length === 0 || !options.breakpoints.every(item => isnonempty(item)))) return { allowed: false, reason: \"The reviewed pdf break points must be a non-empty list of selectors when present.\" };\n if (options.exporttarget !== undefined && options.exporttarget !== \"memory\" && options.exporttarget !== \"download\") return { allowed: false, reason: \"The reviewed pdf export target must be memory or download; pdf documents do not route to the clipboard.\" };\n if (options.name !== undefined && !isnonempty(options.name)) return { allowed: false, reason: \"The reviewed pdf artifact name must be a non-empty string.\" };\n }\n if (kind === \"recordscreen\" || kind === \"captureaudio\") {\n const recording = options.recording;\n if (recording !== undefined) {\n if (!recording || typeof recording !== \"object\" || Array.isArray(recording)) return { allowed: false, reason: \"The reviewed recording options must be an object in options.recording.\" };\n const recordoptions = recording as Record<string, unknown>;\n if (recordoptions.scope !== undefined && recordoptions.scope !== \"tab\" && recordoptions.scope !== \"run\") return { allowed: false, reason: \"The reviewed recording scope must be tab or run.\" };\n if (recordoptions.fps !== undefined && (typeof recordoptions.fps !== \"number\" || !Number.isFinite(recordoptions.fps) || recordoptions.fps <= 0)) return { allowed: false, reason: \"The reviewed recording fps must be a positive number with no code ceiling.\" };\n if (recordoptions.bitrate !== undefined && (typeof recordoptions.bitrate !== \"number\" || !Number.isFinite(recordoptions.bitrate) || recordoptions.bitrate <= 0)) return { allowed: false, reason: \"The reviewed recording bitrate must be a positive number with no code ceiling.\" };\n if (recordoptions.audio !== undefined && typeof recordoptions.audio !== \"boolean\") return { allowed: false, reason: \"The reviewed recording audio flag must be a boolean.\" };\n }\n if (options.duration !== undefined && (typeof options.duration !== \"number\" || !Number.isFinite(options.duration) || options.duration <= 0)) return { allowed: false, reason: \"The reviewed recording duration must be a positive number of milliseconds with no code ceiling.\" };\n const consent = recordingconsentgranted(step);\n if (!consent.allowed) return consent;\n }\n if (kind === \"captureframe\") {\n if (options.timestamp !== undefined && (typeof options.timestamp !== \"number\" || !Number.isFinite(options.timestamp) || options.timestamp < 0)) return { allowed: false, reason: \"The reviewed frame timestamp must be zero or a positive number of seconds.\" };\n if (options.poster !== undefined && typeof options.poster !== \"boolean\") return { allowed: false, reason: \"The reviewed poster flag must be a boolean.\" };\n const capturecheck = validatecaptureoptions(options.capture);\n if (!capturecheck.allowed) return capturecheck;\n }\n if (kind === \"downloadimages\") {\n const filter = options.imagefilter;\n if (!filter || typeof filter !== \"object\" || Array.isArray(filter)) return { allowed: false, reason: \"A reviewed imagefilter is required in options before any image downloads.\" };\n const imagefilter = filter as Record<string, unknown>;\n if (imagefilter.selector !== undefined && !isnonempty(imagefilter.selector)) return { allowed: false, reason: \"The reviewed imagefilter selector must be a non-empty selector from the reviewed selector grammar.\" };\n if (imagefilter.minwidth !== undefined && (typeof imagefilter.minwidth !== \"number\" || !Number.isFinite(imagefilter.minwidth) || imagefilter.minwidth < 0)) return { allowed: false, reason: \"The reviewed imagefilter minimum width must be zero or a positive number of pixels.\" };\n if (imagefilter.minheight !== undefined && (typeof imagefilter.minheight !== \"number\" || !Number.isFinite(imagefilter.minheight) || imagefilter.minheight < 0)) return { allowed: false, reason: \"The reviewed imagefilter minimum height must be zero or a positive number of pixels.\" };\n if (imagefilter.formats !== undefined && (!Array.isArray(imagefilter.formats) || imagefilter.formats.length === 0 || !imagefilter.formats.every(item => isnonempty(item)))) return { allowed: false, reason: \"The reviewed imagefilter format list must be a non-empty list of mime or extension patterns when present.\" };\n if (options.naming !== undefined) {\n const namingcheck = validatecapturenaming(options.naming);\n if (!namingcheck.allowed) return namingcheck;\n }\n }\n if (kind === \"shotcanvas\") {\n const capturecheck = validatecaptureoptions(options.capture);\n if (!capturecheck.allowed) return capturecheck;\n }\n if (kind === \"probestream\" && options.selector !== undefined && !isnonempty(options.selector)) return { allowed: false, reason: \"The reviewed stream probe scope selector must be a non-empty string.\" };\n if (kind === \"timelapse\") {\n const lapse = options.lapse;\n if (!lapse || typeof lapse !== \"object\" || Array.isArray(lapse)) return { allowed: false, reason: \"A reviewed lapse plan with interval, duration and format is required in options.\" };\n const plan = lapse as Record<string, unknown>;\n if (typeof plan.interval !== \"number\" || !Number.isFinite(plan.interval) || plan.interval <= 0) return { allowed: false, reason: \"The reviewed lapse interval must be a positive number of milliseconds with no code ceiling.\" };\n if (typeof plan.duration !== \"number\" || !Number.isFinite(plan.duration) || plan.duration <= 0) return { allowed: false, reason: \"The reviewed lapse duration must be a positive number of milliseconds with no code ceiling.\" };\n if (plan.format !== undefined && plan.format !== \"png\" && plan.format !== \"jpeg\" && plan.format !== \"webp\") return { allowed: false, reason: \"The reviewed lapse format must be png, jpeg or webp.\" };\n const budget = lapsebudgetallowed(plan.interval as number, plan.duration as number, typeof options.wait === \"number\" ? options.wait : undefined);\n if (!budget.allowed) return budget;\n const capturecheck = validatecaptureoptions(options.capture);\n if (!capturecheck.allowed) return capturecheck;\n }\n if (kind === \"convertimage\" || kind === \"makethumbs\") {\n const single = options.capture;\n const list = options.captures;\n const hasone = isnonempty(single);\n const haslist = Array.isArray(list) && list.length > 0 && list.every(item => isnonempty(item));\n if (!hasone && !haslist) return { allowed: false, reason: \"A reviewed capture id or a reviewed non-empty capture id list is required in options.\" };\n if (hasone && haslist) return { allowed: false, reason: \"The reviewed step needs one capture id or a capture id list, not both.\" };\n }\n if (kind === \"convertimage\") {\n const convert = options.convert;\n if (!convert || typeof convert !== \"object\" || Array.isArray(convert)) return { allowed: false, reason: \"A reviewed convert directive with a target format is required in options.\" };\n const directive = convert as Record<string, unknown>;\n if (directive.target !== \"png\" && directive.target !== \"jpeg\" && directive.target !== \"webp\") return { allowed: false, reason: \"The reviewed conversion target must be png, jpeg or webp.\" };\n if (directive.source !== undefined && directive.source !== \"png\" && directive.source !== \"jpeg\" && directive.source !== \"webp\") return { allowed: false, reason: \"The reviewed conversion source must be png, jpeg or webp.\" };\n if (directive.quality !== undefined && (typeof directive.quality !== \"number\" || !Number.isFinite(directive.quality) || directive.quality < 0 || directive.quality > 100)) return { allowed: false, reason: \"The reviewed conversion quality must stay between zero and one hundred with no code cap inside that range.\" };\n }\n if (kind === \"makethumbs\") {\n const thumb = options.thumb;\n if (!thumb || typeof thumb !== \"object\" || Array.isArray(thumb)) return { allowed: false, reason: \"A reviewed thumb directive with size, fit and suffix is required in options.\" };\n const directive = thumb as Record<string, unknown>;\n if (typeof directive.size !== \"number\" || !Number.isFinite(directive.size) || directive.size <= 0) return { allowed: false, reason: \"The reviewed thumbnail size must be a positive number of pixels with no fixed set.\" };\n if (directive.fit !== \"cover\" && directive.fit !== \"contain\") return { allowed: false, reason: \"The reviewed thumbnail fit must be cover or contain.\" };\n if (!isnonempty(directive.suffix)) return { allowed: false, reason: \"The reviewed thumbnail naming suffix must be a non-empty string.\" };\n }\n return { allowed: true };\n}\n\n/** Validates a single proposal against the active tab origin and local policy. */\nexport function validatestep(step: toolstep, origin: string): policyevaluation {\n if (!allowedactions.has(step.kind)) return { allowed: false, reason: \"Unsupported action kind.\" };\n if (!step.summary.trim()) return { allowed: false, reason: \"A human-readable action summary is required.\" };\n let options: Record<string, unknown>;\n try { options = parseoptions(step); } catch { return { allowed: false, reason: \"Step options must be a JSON object.\" }; }\n const hastargetref = options.targetref !== undefined;\n if (targetactions.has(step.kind) && !step.target?.trim() && !hastargetref) return { allowed: false, reason: \"A page target is required.\" };\n if (valueactions.has(step.kind) && !step.value?.trim()) return { allowed: false, reason: \"A reviewed value is required.\" };\n if (step.kind === \"select\" && !step.value?.trim()) return { allowed: false, reason: \"A reviewed option value is required.\" };\n if (step.kind === \"navigate\" && !step.value) return { allowed: false, reason: \"A navigation URL is required.\" };\n if (hastargetref) {\n const reference = validatetargetref(options.targetref);\n if (!reference.allowed) return reference;\n }\n if (step.kind === \"wait\") {\n try { waitduration(step); } catch { return { allowed: false, reason: \"Wait duration must be zero or a positive number of milliseconds.\" }; }\n }\n if (step.kind === \"navigate\") {\n try {\n if (new URL(step.value ?? \"\").origin !== origin) return { allowed: false, reason: \"Navigation must remain within the approved origin.\" };\n } catch {\n return { allowed: false, reason: \"Navigation URL is invalid.\" };\n }\n }\n if (step.kind === \"tabcreate\" || step.kind === \"windowcreate\" || step.kind === \"downloadfile\") {\n try {\n const url = new URL(step.value ?? \"\");\n if (url.protocol !== \"https:\") return { allowed: false, reason: \"The reviewed URL must use HTTPS.\" };\n } catch {\n return { allowed: false, reason: \"The reviewed URL is invalid.\" };\n }\n }\n if (step.kind === \"tabactivate\" || step.kind === \"tabclose\" || step.kind === \"tabreload\" || step.kind === \"windowclose\" || step.kind === \"windowresize\") {\n if (!isnumericid(step.value)) return { allowed: false, reason: \"A numeric browser id is required.\" };\n }\n if (step.kind === \"zoomset\") {\n const zoom = Number(step.value);\n if (!Number.isFinite(zoom) || zoom <= 0) return { allowed: false, reason: \"The reviewed zoom must be a positive number.\" };\n }\n if (step.kind === \"setattribute\" || step.kind === \"writestorage\") {\n const keyname = step.kind === \"setattribute\" ? \"name\" : \"key\";\n if (typeof options[keyname] !== \"string\" || !(options[keyname] as string).trim()) return { allowed: false, reason: `A reviewed ${keyname} is required in options.` };\n if (typeof options.value !== \"string\") return { allowed: false, reason: \"A reviewed value is required in options.\" };\n }\n if (step.kind === \"windowresize\") {\n if (typeof options.width !== \"number\" || typeof options.height !== \"number\" || !Number.isFinite(options.width) || !Number.isFinite(options.height)) return { allowed: false, reason: \"Reviewed width and height numbers are required in options.\" };\n }\n if ((step.kind === \"scrollpage\" || step.kind === \"scrollby\") && (!numericoption(options, \"x\") || !numericoption(options, \"y\"))) return { allowed: false, reason: \"Scroll amounts must be numbers in options.\" };\n if (step.kind === \"waitfor\" && options.timeout !== undefined && (typeof options.timeout !== \"number\" || options.timeout < 0)) return { allowed: false, reason: \"The waitfor timeout must be zero or a positive number of milliseconds.\" };\n if (step.kind === \"movepointer\") {\n const path = options.pointpath;\n if (!path || typeof path !== \"object\" || Array.isArray(path)) return { allowed: false, reason: \"A reviewed pointpath with start and end points is required in options.\" };\n const points = path as Record<string, unknown>;\n if (!ispoint(points.start) || !ispoint(points.end)) return { allowed: false, reason: \"The reviewed pointpath needs numeric start and end points.\" };\n if (points.waypoints !== undefined && (!Array.isArray(points.waypoints) || !points.waypoints.every(waypoint => ispoint(waypoint)))) return { allowed: false, reason: \"The reviewed pointpath waypoints must be numeric points.\" };\n if (!nonnegativeoption(points, \"duration\")) return { allowed: false, reason: \"The reviewed pointpath duration must be zero or a positive number of milliseconds.\" };\n const speed = options.speedprofile;\n if (speed !== undefined) {\n if (!speed || typeof speed !== \"object\" || Array.isArray(speed)) return { allowed: false, reason: \"The reviewed speed profile must be an object.\" };\n const profile = speed as Record<string, unknown>;\n if (profile.easing !== undefined && profile.easing !== \"linear\" && profile.easing !== \"easeinout\") return { allowed: false, reason: \"The reviewed easing must be linear or easeinout.\" };\n if (!nonnegativeoption(profile, \"peak\")) return { allowed: false, reason: \"The reviewed peak velocity must be zero or a positive number.\" };\n if (!nonnegativeoption(profile, \"jitter\")) return { allowed: false, reason: \"The reviewed jitter window must be zero or a positive number of milliseconds.\" };\n }\n }\n if (step.kind === \"clickpoint\" && (!hastargetref || (options.targetref as Record<string, unknown>).mode !== \"point\")) return { allowed: false, reason: \"A reviewed point target reference is required in options.\" };\n if (step.kind === \"clicktext\" && (!hastargetref || (options.targetref as Record<string, unknown>).mode !== \"text\")) return { allowed: false, reason: \"A reviewed text target reference is required in options.\" };\n if (step.kind === \"clickaria\" && (!hastargetref || (options.targetref as Record<string, unknown>).mode !== \"aria\")) return { allowed: false, reason: \"A reviewed aria target reference is required in options.\" };\n if (step.kind === \"clickname\" && (!hastargetref || (options.targetref as Record<string, unknown>).mode !== \"name\")) return { allowed: false, reason: \"A reviewed name target reference is required in options.\" };\n if (step.kind === \"resolvexpath\" && (!hastargetref || (options.targetref as Record<string, unknown>).mode !== \"xpath\")) return { allowed: false, reason: \"A reviewed xpath target reference is required in options.\" };\n if (step.kind === \"typetime\" && options.delay !== undefined && (typeof options.delay !== \"number\" || !Number.isFinite(options.delay) || options.delay < 0)) return { allowed: false, reason: \"The reviewed per keystroke delay must be zero or a positive number of milliseconds.\" };\n if (step.kind === \"submitsearch\") {\n if (!isnonempty(options.results)) return { allowed: false, reason: \"A reviewed results region selector is required in options.\" };\n if (options.timeout !== undefined && (typeof options.timeout !== \"number\" || !Number.isFinite(options.timeout) || options.timeout < 0)) return { allowed: false, reason: \"The submitsearch timeout must be zero or a positive number of milliseconds.\" };\n }\n if (step.kind === \"selectmulti\") {\n const values = options.values;\n if (!Array.isArray(values) || values.length === 0 || !values.every(value => isnonempty(value))) return { allowed: false, reason: \"A reviewed list of option values is required in options.\" };\n }\n if (step.kind === \"setslider\") {\n const slider = Number(step.value);\n if (!Number.isFinite(slider)) return { allowed: false, reason: \"The reviewed slider value must be a number.\" };\n }\n if (step.kind === \"setdate\" && !/^\\d{4}-\\d{2}-\\d{2}$/.test(step.value ?? \"\")) return { allowed: false, reason: \"The reviewed date must use the yyyy-mm-dd form.\" };\n if (step.kind === \"setcolor\" && !/^#[0-9a-fA-F]{6}$/.test(step.value ?? \"\")) return { allowed: false, reason: \"The reviewed color must use the #rrggbb form.\" };\n if (step.kind === \"keyhold\" && options.holdid !== undefined && !isnonempty(options.holdid)) return { allowed: false, reason: \"The reviewed hold id must be a non-empty string.\" };\n if (step.kind === \"dismissdialog\") {\n const accept = options.accept;\n const answer = options.answer;\n if (accept === undefined && !isnonempty(answer)) return { allowed: false, reason: \"A reviewed accept flag or prompt answer is required in options.\" };\n if (accept !== undefined && typeof accept !== \"boolean\") return { allowed: false, reason: \"The reviewed dialog accept flag must be a boolean.\" };\n if (answer !== undefined && !isnonempty(answer)) return { allowed: false, reason: \"The reviewed prompt answer must be a non-empty string.\" };\n }\n if (step.kind === \"pierceshadow\" && options.shadow !== undefined) {\n if (!Array.isArray(options.shadow) || !options.shadow.every(item => isnonempty(item))) return { allowed: false, reason: \"The reviewed shadow path must be a list of non-empty selectors.\" };\n }\n if (step.kind === \"enterframe\") {\n const path = options.framepath;\n if (!Array.isArray(path) || path.length === 0 || !path.every(item => typeof item === \"number\" && Number.isInteger(item) && item >= 0)) return { allowed: false, reason: \"A reviewed frame path of frame indexes is required in options.\" };\n return validateinnerstep(options, origin);\n }\n if (step.kind === \"retryaction\") {\n const inner = validateinnerstep(options, origin);\n if (!inner.allowed) return inner;\n const rule = options.retryrule;\n if (!rule || typeof rule !== \"object\" || Array.isArray(rule)) return { allowed: false, reason: \"A reviewed retry rule with attempts is required in options.\" };\n const retry = rule as Record<string, unknown>;\n if (typeof retry.attempts !== \"number\" || !Number.isInteger(retry.attempts) || retry.attempts < 1) return { allowed: false, reason: \"The reviewed retry attempts must be a positive integer with no code ceiling.\" };\n if (!nonnegativeoption(retry, \"settle\")) return { allowed: false, reason: \"The reviewed retry settle window must be zero or a positive number of milliseconds.\" };\n if (!nonnegativeoption(retry, \"tolerance\")) return { allowed: false, reason: \"The reviewed retry movement tolerance must be zero or a positive number of pixels.\" };\n }\n if (watchactions.has(step.kind)) {\n if (typeof options.lifetime !== \"number\" || !Number.isFinite(options.lifetime) || options.lifetime <= 0) return { allowed: false, reason: \"A reviewed watch lifetime window in milliseconds is required in options.\" };\n if (options.scopes !== undefined && (!Array.isArray(options.scopes) || !options.scopes.every(scope => isnonempty(scope)))) return { allowed: false, reason: \"The reviewed watch scopes must be a list of non-empty selectors.\" };\n if (options.events !== undefined && (!Array.isArray(options.events) || !options.events.every(event => isnonempty(event)))) return { allowed: false, reason: \"The reviewed watch event kinds must be a list of non-empty strings.\" };\n if (!nonnegativeoption(options, \"poll\")) return { allowed: false, reason: \"The reviewed watch poll interval must be zero or a positive number of milliseconds.\" };\n }\n if (step.kind === \"waitquiet\") {\n const rule = options.quietrule;\n if (!rule || typeof rule !== \"object\" || Array.isArray(rule)) return { allowed: false, reason: \"A reviewed quietrule with an idle threshold is required in options.\" };\n const quiet = rule as Record<string, unknown>;\n if (typeof quiet.idle !== \"number\" || !Number.isFinite(quiet.idle) || quiet.idle <= 0) return { allowed: false, reason: \"The reviewed quiet idle threshold must be a positive number of milliseconds with no code ceiling.\" };\n if (!nonnegativeoption(quiet, \"poll\")) return { allowed: false, reason: \"The reviewed quiet poll interval must be zero or a positive number of milliseconds.\" };\n if (!nonnegativeoption(quiet, \"timeout\")) return { allowed: false, reason: \"The reviewed quiet timeout must be zero or a positive number of milliseconds.\" };\n }\n if (step.kind === \"diffsnapshots\") {\n const versions = options.versions;\n if (!Array.isArray(versions) || versions.length !== 2 || !versions.every(version => typeof version === \"number\" && Number.isInteger(version) && version >= 1)) return { allowed: false, reason: \"Two reviewed observation version numbers are required in options.\" };\n }\n if (step.kind === \"openlink\" || step.kind === \"openprivate\" || step.kind === \"deeplink\") {\n const targetcheck = validatenavtarget(options.navtarget, step.kind);\n if (!targetcheck.allowed) return targetcheck;\n if (step.kind === \"deeplink\") {\n const app = options.app;\n if (!isnonempty(app)) return { allowed: false, reason: \"A reviewed deep link app pattern is required in options.\" };\n const params = options.params;\n if (params !== undefined && (!params || typeof params !== \"object\" || Array.isArray(params) || !Object.values(params).every(item => typeof item === \"string\"))) return { allowed: false, reason: \"The reviewed deep link params must be an object of string values.\" };\n }\n }\n if (step.kind === \"waitload\" && !nonnegativeoption(options, \"timeout\")) return { allowed: false, reason: \"The waitload timeout must be zero or a positive number of milliseconds.\" };\n if (step.kind === \"waiturl\" || step.kind === \"spawait\") {\n if (step.kind === \"waiturl\") {\n const patterncheck = validateurlpattern(options.urlpattern);\n if (!patterncheck.allowed) return patterncheck;\n }\n if (!nonnegativeoption(options, \"timeout\")) return { allowed: false, reason: \"The wait timeout must be zero or a positive number of milliseconds.\" };\n if (!nonnegativeoption(options, \"poll\")) return { allowed: false, reason: \"The wait poll interval must be zero or a positive number of milliseconds.\" };\n }\n if (step.kind === \"followlink\") {\n if (options.fragment !== undefined && typeof options.fragment !== \"boolean\") return { allowed: false, reason: \"The reviewed followlink fragment flag must be a boolean.\" };\n }\n if (step.kind === \"spanav\") {\n if (options.routepattern !== undefined) {\n const routecheck = validateurlpattern(options.routepattern);\n if (!routecheck.allowed) return routecheck;\n }\n if (!nonnegativeoption(options, \"timeout\")) return { allowed: false, reason: \"The spanav route timeout must be zero or a positive number of milliseconds.\" };\n }\n if (step.kind === \"rewritequery\") {\n const set = options.set;\n const remove = options.remove;\n if (set === undefined && remove === undefined) return { allowed: false, reason: \"Reviewed query parameters to set or remove are required in options.\" };\n if (set !== undefined && (!set || typeof set !== \"object\" || Array.isArray(set) || !Object.values(set).every(item => typeof item === \"string\"))) return { allowed: false, reason: \"The reviewed query parameters to set must be an object of string values.\" };\n if (remove !== undefined && (!Array.isArray(remove) || !remove.every(item => isnonempty(item)))) return { allowed: false, reason: \"The reviewed query parameters to remove must be a list of non-empty names.\" };\n }\n if (step.kind === \"navlist\") {\n const listcheck = validateurllist(options, \"urls\");\n if (!listcheck.allowed) return listcheck;\n }\n if (step.kind === \"navprofile\") {\n const profilecheck = validatewaitprofile(options.waitprofile);\n if (!profilecheck.allowed) return profilecheck;\n }\n if (step.kind === \"handleauth\" && !ishttpsurl(step.value)) return { allowed: false, reason: \"A reviewed HTTPS origin or url is required as the auth target.\" };\n if (step.kind === \"printpdf\" && options.name !== undefined && !isnonempty(options.name)) return { allowed: false, reason: \"The reviewed artifact name must be a non-empty string.\" };\n if (step.kind === \"prefetch\") {\n const listcheck = validateurllist(options, \"urls\");\n if (!listcheck.allowed) return listcheck;\n }\n if (step.kind === \"preconnect\") {\n const origins = options.origins;\n if (!Array.isArray(origins) || origins.length === 0 || !origins.every(originurl => ishttpsurl(originurl))) return { allowed: false, reason: \"A reviewed non-empty list of HTTPS origins is required in options.\" };\n }\n if (step.kind === \"reopentab\" && step.value !== undefined && !ishttpsurl(step.value)) return { allowed: false, reason: \"The reviewed reopen url must use HTTPS.\" };\n if (step.kind === \"navrate\") {\n const limitcheck = validateratelimit(options.ratelimit);\n if (!limitcheck.allowed) return limitcheck;\n }\n if (step.kind === \"checksafe\" && !ishttpsurl(step.value)) return { allowed: false, reason: \"A reviewed HTTPS url is required for the safety check.\" };\n if (step.kind === \"batchopen\") {\n const listcheck = validateurllist(options, \"urls\");\n if (!listcheck.allowed) return listcheck;\n }\n if (istabscommandkind(step.kind)) {\n const tabscheck = validatetabsgrammar(step, options);\n if (!tabscheck.allowed) return tabscheck;\n }\n if (isformkind(step.kind)) {\n const formcheck = validateformgrammar(step, options);\n if (!formcheck.allowed) return formcheck;\n }\n if (isdatasetkind(step.kind)) {\n const datacheck = validatedatagrammar(step, options, origin);\n if (!datacheck.allowed) return datacheck;\n }\n if (isfileskind(step.kind)) {\n const filescheck = validatefilesgrammar(step, options);\n if (!filescheck.allowed) return filescheck;\n }\n if (iscapturekind(step.kind)) {\n const capturecheck = validatecapturegrammar(step, options);\n if (!capturecheck.allowed) return capturecheck;\n }\n if (ismediakind(step.kind)) {\n const mediacheck = validatemediagrammar(step, options);\n if (!mediacheck.allowed) return mediacheck;\n }\n if (ishttpkind(step.kind)) {\n const httpcheck = validatehttpgrammar(step, options);\n if (!httpcheck.allowed) return httpcheck;\n }\n if (issocketkind(step.kind)) {\n const socketcheck = validatesocketgrammar(step, options);\n if (!socketcheck.allowed) return socketcheck;\n }\n if (isnetwatchkind(step.kind)) {\n const netwatchcheck = validatenetwatchgrammar(step, options);\n if (!netwatchcheck.allowed) return netwatchcheck;\n }\n if (iscontrolkind(step.kind)) {\n const controlcheck = validatecontrolgrammar(step, options);\n if (!controlcheck.allowed) return controlcheck;\n }\n if (isdebugkind(step.kind)) {\n const timelinecheck = validatetimelinegrammar(step, options);\n if (!timelinecheck.allowed) return timelinecheck;\n }\n if (iscdpkind(step.kind)) {\n const cdpcheck = validatecdpgrammar(step, options);\n if (!cdpcheck.allowed) return cdpcheck;\n }\n if (isprofilekind(step.kind)) {\n const profilecheck = validateprofilegrammar(step, options);\n if (!profilecheck.allowed) return profilecheck;\n }\n if (isemulationkind(step.kind)) {\n const emulationcheck = validateemulationgrammar(step, options);\n if (!emulationcheck.allowed) return emulationcheck;\n }\n if (issessionkind(step.kind)) {\n const sessioncheck = validatesessiongrammar(step, options);\n if (!sessioncheck.allowed) return sessioncheck;\n }\n if (isworkflowkind(step.kind)) {\n const workflowcheck = validateworkflowgrammar(step, options);\n if (!workflowcheck.allowed) return workflowcheck;\n }\n if (istriggeraction(step.kind)) {\n const triggercheck = validatetriggergrammar(step, options);\n if (!triggercheck.allowed) return triggercheck;\n }\n if (step.kind === \"tabcreate\") {\n if (options.background !== undefined && typeof options.background !== \"boolean\") return { allowed: false, reason: \"The reviewed background flag must be a boolean.\" };\n if (options.window !== undefined && (typeof options.window !== \"number\" || !Number.isInteger(options.window) || options.window < 0)) return { allowed: false, reason: \"The reviewed target window id must be a non-negative integer.\" };\n }\n if (step.kind === \"windowcreate\") {\n for (const field of [\"left\", \"top\", \"width\", \"height\"]) {\n if (options[field] !== undefined && (typeof options[field] !== \"number\" || !Number.isFinite(options[field]))) return { allowed: false, reason: `The reviewed window ${field} must be a number.` };\n }\n if (options.state !== undefined && ![\"normal\", \"maximized\", \"minimized\", \"fullscreen\"].includes(options.state as string)) return { allowed: false, reason: \"The reviewed window state must be normal, maximized, minimized or fullscreen.\" };\n }\n return { allowed: true };\n}\n\n/** Shared session gate: a live, unpaused session that still matches the active tab. */\nfunction sessiongate(input: { session: agentsession | undefined; tabid: number; origin: string; now: number; action: string }): policyevaluation {\n if (!input.session || input.session.stoppedat) return { allowed: false, reason: \"No active browser session exists.\" };\n if (input.session.expiresat <= input.now) return { allowed: false, reason: \"The browser session has expired.\" };\n if (input.session.pausedat) return { allowed: false, reason: `The browser session is paused and cannot ${input.action}.` };\n if (input.session.tabid !== input.tabid || input.session.origin !== input.origin) return { allowed: false, reason: `The ${input.action} is outside the approved tab or origin.` };\n return { allowed: true };\n}\n\n/** Applies the consent gate immediately before an action reaches the page bridge. */\nexport function canexecute(input: { session: agentsession | undefined; plan: agentplan | undefined; step: toolstep; tabid: number; origin: string; now?: number; verdicts?: safetyverdict[]; settings?: runsettings }): policyevaluation {\n const now = input.now ?? Date.now();\n const gate = sessiongate({ session: input.session, tabid: input.tabid, origin: input.origin, now, action: \"execute an action\" });\n if (!gate.allowed) return gate;\n if (!input.plan || input.plan.state !== \"approved\") return { allowed: false, reason: \"The plan has not received explicit approval.\" };\n if (input.plan.expiresat <= now) return { allowed: false, reason: \"The approved plan has expired.\" };\n if ((input.step.kind === \"pierceshadow\" || input.step.kind === \"enterframe\") && !origingranted(input.session, input.origin)) return { allowed: false, reason: \"The shadow or frame step is outside the session origin grants.\" };\n if (input.step.kind === \"readjson\" && !origingranted(input.session, input.origin)) return { allowed: false, reason: \"The json state read is outside the session origin grants.\" };\n if (isexportkind(input.step.kind)) {\n const exportgate = exportgranted(input.session, input.origin);\n if (!exportgate.allowed) return exportgate;\n }\n if (input.step.kind === \"navlist\") {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(input.step); } catch { options = {}; }\n for (const url of Array.isArray(options.urls) ? options.urls : []) {\n if (typeof url !== \"string\") continue;\n const navigation = navigationgranted(input.session, url);\n if (!navigation.allowed) return navigation;\n }\n }\n if (islayoutkind(input.step.kind) && !layoutmutationgranted(input.session, now).allowed) return { allowed: false, reason: \"Group and layout mutations stay inside the active session.\" };\n if (input.step.kind === \"submitform\" || input.step.kind === \"retryform\") {\n if (!input.plan) return { allowed: false, reason: \"Form submission requires an asksubmit review step before it.\" };\n const reviewgate = submitreviewgranted(input.plan.steps, input.step.id);\n if (!reviewgate.allowed) return reviewgate;\n }\n if (input.step.kind === \"consentpassword\") {\n const consentgate = passwordconsentgranted(input.step);\n if (!consentgate.allowed) return consentgate;\n }\n if (input.step.kind === \"readclipboard\") {\n const clipgate = clipboardconsentgranted(input.step);\n if (!clipgate.allowed) return clipgate;\n }\n if (input.step.kind === \"interceptmime\" && !origingranted(input.session, input.origin)) return { allowed: false, reason: \"The download interception is outside the session origin grants.\" };\n if (iscapturekind(input.step.kind)) {\n const capturegatecheck = capturegate(input.session, input.tabid, input.origin, now);\n if (!capturegatecheck.allowed) return capturegatecheck;\n let captureoptions: Record<string, unknown> = {};\n try { captureoptions = parseoptions(input.step); } catch { captureoptions = {}; }\n const target = (captureoptions.capture as Record<string, unknown> | undefined)?.exporttarget;\n if (target !== undefined && target !== \"memory\" && target !== \"download\" && target !== \"clipboard\") return { allowed: false, reason: \"The capture export target must be memory, download or clipboard.\" };\n }\n if (ismediakind(input.step.kind)) {\n const mediagatecheck = mediagate(input.session, input.tabid, input.origin, now);\n if (!mediagatecheck.allowed) return mediagatecheck;\n }\n if (isrecordingkind(input.step.kind)) {\n const recordinggate = recordingconsentgranted(input.step);\n if (!recordinggate.allowed) return recordinggate;\n }\n if (ishttpkind(input.step.kind)) {\n const target = outboundtarget(input.step);\n if (target !== undefined) {\n const outboundgate = origincheck(input.session, target);\n if (!outboundgate.allowed) return outboundgate;\n }\n if (input.step.kind === \"fetchurl\" || input.step.kind === \"callrest\" || input.step.kind === \"callgraphql\") {\n const consentgate = fetchconsentrefgranted(input.step);\n if (!consentgate.allowed) return consentgate;\n }\n }\n if (issocketkind(input.step.kind)) {\n const channelurl = sockettarget(input.step);\n if (channelurl !== undefined) {\n const channelgate = socketgate(input.session, channelurl);\n if (!channelgate.allowed) return channelgate;\n }\n }\n if (input.step.kind === \"watchrequests\") {\n const watchgatecheck = watchgate(input.session, input.settings, now);\n if (!watchgatecheck.allowed) return watchgatecheck;\n }\n if (isdebugkind(input.step.kind)) {\n const timelinegatecheck = timelinegate(input.session, input.tabid, input.origin, now);\n if (!timelinegatecheck.allowed) return timelinegatecheck;\n }\n if (iscdpkind(input.step.kind)) {\n const debuggatecheck = debuggate(input.session, input.tabid, input.origin, now);\n if (!debuggatecheck.allowed) return debuggatecheck;\n if (!input.plan) return { allowed: false, reason: \"The devtools protocol steps need an approved plan.\" };\n const allowlist = planallowlist(input.plan.steps);\n if (input.step.kind !== \"attachcdp\") {\n if (allowlist === undefined) return { allowed: false, reason: \"The devtools protocol step needs the attachcdp step of the same plan with its enabled domains first.\" };\n if (input.step.kind === \"cdpcmd\") {\n let cdpoptions: Record<string, unknown> = {};\n try { cdpoptions = parseoptions(input.step); } catch { cdpoptions = {}; }\n const command = cdpoptions.command && typeof cdpoptions.command === \"object\" && !Array.isArray(cdpoptions.command) ? cdpoptions.command as Record<string, unknown> : undefined;\n const method = typeof command?.method === \"string\" ? command.method : \"\";\n if (methoddomain(method) === undefined || !allowlistcovers(allowlist, method)) return { allowed: false, reason: `The raw command ${method || \"\"} stays outside the enabled domain allowlist of the plan attach; review the attach domains or the method gates.` };\n }\n }\n let cdpoptions: Record<string, unknown> = {};\n try { cdpoptions = parseoptions(input.step); } catch { cdpoptions = {}; }\n if (input.step.kind === \"setbreakpoint\") {\n const breakpoint = breakpointinputof(cdpoptions.breakpoint);\n if (breakpoint) {\n const targetgate = origincheck(input.session, breakpoint.url);\n if (!targetgate.allowed) return targetgate;\n }\n }\n if (input.step.kind === \"overridescript\") {\n const override = overrideinputof(cdpoptions.override);\n if (override) {\n const targetgate = origincheck(input.session, override.urlpattern);\n if (!targetgate.allowed) return targetgate;\n }\n }\n }\n if (isprofilekind(input.step.kind)) {\n let profileoptions: Record<string, unknown> = {};\n try { profileoptions = parseoptions(input.step); } catch { profileoptions = {}; }\n const targets = [\n ...(attachtargetof(profileoptions.target) !== undefined ? [attachtargetof(profileoptions.target) as attachtarget] : []),\n ...(Array.isArray(profileoptions.attachtargets) ? profileoptions.attachtargets.flatMap(target => { const parsed = attachtargetof(target); return parsed !== undefined ? [parsed] : []; }) : []),\n ];\n const targetgatecheck = targetgate({ session: input.session, tabid: input.tabid, origin: input.origin, targets, grants: undefined, now });\n if (!targetgatecheck.allowed) return targetgatecheck;\n if (input.step.kind === \"capturesourcemaps\") {\n for (const url of Array.isArray(profileoptions.scripts) ? profileoptions.scripts : []) {\n if (typeof url !== \"string\") continue;\n const scriptgate = origincheck(input.session, url);\n if (!scriptgate.allowed) return scriptgate;\n }\n }\n }\n if (isemulationkind(input.step.kind)) {\n const emugatecheck = emugate({ session: input.session, plan: input.plan, step: input.step, tabid: input.tabid, origin: input.origin, now });\n if (!emugatecheck.allowed) return emugatecheck;\n }\n if (issessionkind(input.step.kind)) {\n const sessiongatecheck = sessionrestoregate({ session: input.session, plan: input.plan, step: input.step, tabid: input.tabid, origin: input.origin, now });\n if (!sessiongatecheck.allowed) return sessiongatecheck;\n if (input.step.kind === \"restoresession\") {\n let restoreoptions: Record<string, unknown> = {};\n try { restoreoptions = parseoptions(input.step); } catch { restoreoptions = {}; }\n for (const url of Array.isArray(restoreoptions.origins) ? restoreoptions.origins : []) {\n if (typeof url !== \"string\" || !url) continue;\n const origingate = origincheck(input.session, url);\n if (!origingate.allowed) return { allowed: false, reason: `The session restore reopens ${url} outside the session origin grants; review the restore record or grant the origin.` };\n }\n }\n }\n if (isworkflowkind(input.step.kind)) {\n const workflowgatecheck = workflowgate({ session: input.session, plan: input.plan, step: input.step, tabid: input.tabid, origin: input.origin, now });\n if (!workflowgatecheck.allowed) return workflowgatecheck;\n }\n if (istriggeraction(input.step.kind)) {\n const triggergatecheck = triggergate({ session: input.session, plan: input.plan, step: input.step, tabid: input.tabid, origin: input.origin, now });\n if (!triggergatecheck.allowed) return triggergatecheck;\n }\n if (iscontrolkind(input.step.kind)) {\n const controlgate = sessiongate({ session: input.session, tabid: input.tabid, origin: input.origin, now, action: \"control the network\" });\n if (!controlgate.allowed) return controlgate;\n let controloptions: Record<string, unknown> = {};\n try { controloptions = parseoptions(input.step); } catch { controloptions = {}; }\n if (input.step.kind === \"blockrequest\") {\n const blockgatecheck = blockgate(input.session, input.step, now);\n if (!blockgatecheck.allowed) return blockgatecheck;\n const rule = blockruleof(controloptions.block);\n if (rule) {\n const blockorigin = origincheck(input.session, rule.urlpattern);\n if (!blockorigin.allowed) return blockorigin;\n }\n }\n if (input.step.kind === \"mockresponse\" || input.step.kind === \"rewriteheaders\") {\n const patterns = input.step.kind === \"mockresponse\" ? [mockspecof(controloptions.mock)?.urlpattern ?? \"\"] : (Array.isArray(controloptions.rules) ? controloptions.rules.map(item => item && typeof item === \"object\" && !Array.isArray(item) ? String((item as Record<string, unknown>).urlpattern ?? \"\") : \"\") : []);\n for (const pattern of patterns) {\n const patterngate = origincheck(input.session, pattern);\n if (!patterngate.allowed) return patterngate;\n }\n }\n if (input.step.kind === \"setcookies\" || input.step.kind === \"readcookies\" || input.step.kind === \"clearcookies\") {\n const domain = typeof controloptions.domain === \"string\" && controloptions.domain.trim() ? controloptions.domain : Array.isArray(controloptions.cookies) ? String((controloptions.cookies[0] as Record<string, unknown> | undefined)?.domain ?? \"\") : \"\";\n if (!domain) return { allowed: false, reason: \"A reviewed cookie domain is required before cookie control runs.\" };\n const cookiegatecheck = cookiegate(input.session, domain, now);\n if (!cookiegatecheck.allowed) return cookiegatecheck;\n }\n if (input.step.kind === \"authflow\") {\n const authconsent = authconsentgranted(input.step);\n if (!authconsent.allowed) return authconsent;\n }\n if (input.step.kind === \"saveapikey\") {\n const keyconsent = apikeyconsentgranted(input.step);\n if (!keyconsent.allowed) return keyconsent;\n }\n if (input.step.kind === \"routeproxy\") {\n const proxygatecheck = proxygate(input.session, input.step, now);\n if (!proxygatecheck.allowed) return proxygatecheck;\n }\n const target = controltarget(input.step);\n if (target !== undefined) {\n const targetgate = origincheck(input.session, target);\n if (!targetgate.allowed) return targetgate;\n }\n }\n if (input.step.kind === \"extractapi\") {\n let replayoptions: Record<string, unknown> = {};\n try { replayoptions = parseoptions(input.step); } catch { replayoptions = {}; }\n const replay = apireplayspecof(replayoptions.replay);\n if (replay !== undefined) {\n const replaygate = origincheck(input.session, replay.endpoint);\n if (!replaygate.allowed) return replaygate;\n }\n }\n if (input.step.kind === \"openlink\" || input.step.kind === \"openprivate\" || input.step.kind === \"batchopen\" || input.step.kind === \"prefetch\" || input.step.kind === \"deeplink\" || input.step.kind === \"reopentab\") {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(input.step); } catch { options = {}; }\n const grammar = validatestep(input.step, input.origin);\n if (!grammar.allowed) return grammar;\n const grants = input.session?.grants ?? [input.session?.origin ?? input.origin];\n const targets: unknown[] = input.step.kind === \"batchopen\" || input.step.kind === \"prefetch\" ? (Array.isArray(options.urls) ? options.urls : []) : input.step.kind === \"reopentab\" ? [input.step.value] : [(options.navtarget as Record<string, unknown> | undefined)?.url];\n for (const target of targets) {\n if (typeof target !== \"string\" || !target) continue;\n const verified = originverified(target, grants, input.verdicts ?? []);\n if (!verified.allowed) return verified;\n }\n }\n return validatestep(input.step, input.origin);\n}\n\n/** Allows a non-mutating, temporary target preview during plan review. */\nexport function canpreview(input: { session: agentsession | undefined; plan: agentplan | undefined; step: toolstep; tabid: number; origin: string; now?: number }): policyevaluation {\n const now = input.now ?? Date.now();\n const gate = sessiongate({ session: input.session, tabid: input.tabid, origin: input.origin, now, action: \"preview a target\" });\n if (!gate.allowed) return gate;\n if (!input.plan || ![\"pending\", \"approved\"].includes(input.plan.state)) return { allowed: false, reason: \"Only a reviewed pending or approved plan can be previewed.\" };\n if (input.plan.expiresat <= now) return { allowed: false, reason: \"The reviewed plan has expired.\" };\n let options: Record<string, unknown> = {};\n try { options = parseoptions(input.step); } catch { options = {}; }\n if (!targetactions.has(input.step.kind) && options.targetref === undefined) return { allowed: false, reason: \"Only a target-based action can be previewed.\" };\n return validatestep(input.step, input.origin);\n}\n\n/** Lists every reviewed action kind of the policy table so the step library of the editor browses the whole vocabulary. */\nexport function reviewedkinds(): string[] {\n return [...allowedactions].sort();\n}\n\n/** The editor save gate: the canvas model of a save needs a live session and an approved plan like every other reviewed artifact, its nodes must be steps or block invocations with unique ids, its edges must reference existing steps and run forward only so no cycle forms, and the composed record still passes the full workflow grammar through the composition the save triggers. */\nexport function editorsavegate(input: { session: agentsession | undefined; plan: agentplan | undefined; model: editormodel; now: number }): policyevaluation {\n const gate = sessiongate({ session: input.session, tabid: input.session?.tabid ?? 0, origin: input.session?.origin ?? \"https://example.com\", now: input.now, action: \"save the workflow editor canvas\" });\n if (!gate.allowed) return gate;\n if (!input.plan || input.plan.state !== \"approved\") return { allowed: false, reason: \"Editor saves need the approved plan review before a new workflow version composes.\" };\n const model = input.model;\n if (typeof model.name !== \"string\" || !model.name.trim()) return { allowed: false, reason: \"The workflow name of the canvas must be a non-empty string.\" };\n if (typeof model.version !== \"number\" || !Number.isInteger(model.version) || model.version < 1) return { allowed: false, reason: \"The workflow version of the canvas must be a positive integer.\" };\n if (!Array.isArray(model.origins) || model.origins.length === 0) return { allowed: false, reason: \"The canvas needs at least one granted HTTPS origin.\" };\n const ids = new Set<string>();\n for (const node of model.nodes) {\n if ((node.step === undefined) === (node.invocation === undefined)) return { allowed: false, reason: \"Every canvas node must be exactly one workflow step or one block invocation.\" };\n const id = node.id ?? (node.step !== undefined ? node.step.id : (node.invocation as { block: string }).block);\n if (!id || ids.has(id)) return { allowed: false, reason: `The canvas node id ${id || \"(empty)\"} must be unique.` };\n ids.add(id);\n }\n const reachable = new Set<string>();\n for (const node of model.nodes) {\n if (node.step !== undefined) { reachable.add(node.step.id); continue; }\n const walk = (entries: Array<{ id?: string; kind?: string; label?: string; block?: string }>): void => {\n for (const entry of entries) {\n if (typeof entry.id === \"string\" && typeof entry.kind === \"string\") { reachable.add(entry.id); continue; }\n if (typeof entry.block === \"string\") {\n const nested = model.blocks.find(candidate => candidate.name === entry.block);\n if (nested) walk(nested.steps as Array<{ id?: string; kind?: string; label?: string; block?: string }>);\n }\n }\n };\n const block = model.blocks.find(candidate => candidate.name === (node.invocation as { block: string }).block);\n if (!block) return { allowed: false, reason: `The block ${(node.invocation as { block: string }).block} of the canvas has no definition.` };\n walk(block.steps as Array<{ id?: string; kind?: string; label?: string; block?: string }>);\n }\n let order = 0;\n const positionof = new Map<string, number>();\n for (const node of model.nodes) {\n if (node.step !== undefined) { positionof.set(node.step.id, order); order += 1; continue; }\n const walk = (entries: Array<{ id?: string; kind?: string; label?: string; block?: string }>): void => {\n for (const entry of entries) {\n if (typeof entry.id === \"string\" && typeof entry.kind === \"string\") { positionof.set(entry.id, order); order += 1; continue; }\n if (typeof entry.block === \"string\") {\n const nested = model.blocks.find(candidate => candidate.name === entry.block);\n if (nested) walk(nested.steps as Array<{ id?: string; kind?: string; label?: string; block?: string }>);\n }\n }\n };\n walk((model.blocks.find(candidate => candidate.name === (node.invocation as { block: string }).block) as { steps: Array<{ id?: string; kind?: string; label?: string; block?: string }> }).steps);\n }\n for (const edge of model.edges) {\n if (!reachable.has(edge.from)) return { allowed: false, reason: `The canvas edge of ${edge.variable} references the unknown source step ${edge.from}.` };\n if (!reachable.has(edge.to)) return { allowed: false, reason: `The canvas edge of ${edge.variable} references the unknown target step ${edge.to}.` };\n if ((positionof.get(edge.from) ?? -1) >= (positionof.get(edge.to) ?? -1)) return { allowed: false, reason: `The canvas edge of ${edge.variable} runs backwards and would form a cycle.` };\n }\n return { allowed: true };\n}\n\n/** Refuses to run a workflow whose review state stays pending: an imported workflow or a version rollback stays unreviewed until the user approves its expanded step list through the import or rollback review. */\nexport function runreviewgranted(record: workflowrecord): policyevaluation {\n if (record.reviewstate === \"pending\") return { allowed: false, reason: \"The workflow stays unreviewed: the import or rollback review must approve its expanded step list before any run.\" };\n return { allowed: true };\n}\n\n/** The reviewed policy knobs a per site override may adjust: loop safety bounds, per step and per run timeout budgets, element wait timeouts and delay bases. */\nconst overrideknobs = [\"loopbound\", \"stepms\", \"runms\", \"waitms\", \"delaybase\"];\n\n/** Validates one per site policy override so overrides only adjust reviewed knobs: the pattern must be an https origin or a `*` subdomain glob of one and every delta must name a reviewed knob with a positive user value and no code ceiling. */\nexport function validatesiteoverride(override: { pattern: string; deltas: Record<string, number> }): policyevaluation {\n if (typeof override.pattern !== \"string\" || !override.pattern.startsWith(\"https://\") || !/[a-z0-9.-]+/i.test(override.pattern.slice(8))) return { allowed: false, reason: \"The override pattern must be an https origin or a `*` subdomain glob of one.\" };\n if (!override.pattern.includes(\"*\")) {\n try {\n if (new URL(override.pattern).origin !== override.pattern) return { allowed: false, reason: \"The override pattern must be a bare https origin or a `*` subdomain glob, never a path.\" };\n } catch {\n return { allowed: false, reason: \"The override pattern must parse as an https origin or a `*` subdomain glob of one.\" };\n }\n }\n for (const [knob, delta] of Object.entries(override.deltas)) {\n if (!overrideknobs.includes(knob)) return { allowed: false, reason: `The override knob ${knob} is not one of the reviewed knobs: ${overrideknobs.join(\", \")}.` };\n if (typeof delta !== \"number\" || !Number.isFinite(delta) || delta <= 0) return { allowed: false, reason: `The override delta of ${knob} must be a positive user value with no code ceiling.` };\n }\n return { allowed: true };\n}\n\n/** Validates the export contents of a workflow file so secrets never leave the browser: every step options object of the workflow and of every packed template is parsed and any field that names a secret, token, api key, password or authorization header refuses the export. */\nexport function exportcontentreview(file: { workflow: workflowrecord; templates: steptemplate[] }): policyevaluation {\n const secretkeys = /(secret|token|apikey|api_key|password|authorization|credential)/i;\n const scan = (label: string, options: string | undefined): policyevaluation | undefined => {\n if (options === undefined) return undefined;\n let payload: unknown;\n try { payload = JSON.parse(options); } catch { return undefined; }\n const walk = (value: unknown, path: string): policyevaluation | undefined => {\n if (!value || typeof value !== \"object\") return undefined;\n for (const [key, entry] of Object.entries(value as Record<string, unknown>)) {\n if (secretkeys.test(key)) return { allowed: false, reason: `The export of ${label} carries the secret field ${path}${key} and secrets never leave the browser.` };\n const nested = walk(entry, `${path}${key}.`);\n if (nested !== undefined) return nested;\n }\n return undefined;\n };\n return walk(payload, \"\");\n };\n for (const step of file.workflow.steps) {\n const refusal = scan(`the step ${step.id}`, step.options);\n if (refusal !== undefined) return refusal;\n }\n for (const template of file.templates) {\n const refusal = scan(`the template ${template.name}`, template.step.options);\n if (refusal !== undefined) return refusal;\n }\n return { allowed: true };\n}\n\n/** Validates one watchdog configuration: the stall threshold stays a positive user value with no code ceiling, the recovery action is one of retry, pause or cancel and the zombie window, when configured, stays positive with no ceiling. */\nexport function watchdogconfigvalid(config: watchdogconfig): policyevaluation {\n if (typeof config.enabled !== \"boolean\") return { allowed: false, reason: \"The watchdog enabled flag must be a boolean.\" };\n if (typeof config.stallthreshold !== \"number\" || !Number.isFinite(config.stallthreshold) || config.stallthreshold <= 0) return { allowed: false, reason: \"The watchdog stall threshold must be a positive number of milliseconds with no code ceiling.\" };\n if (![\"retry\", \"pause\", \"cancel\"].includes(config.action)) return { allowed: false, reason: \"The watchdog recovery action must be retry, pause or cancel.\" };\n if (config.zombiewindow !== undefined && (typeof config.zombiewindow !== \"number\" || !Number.isFinite(config.zombiewindow) || config.zombiewindow <= 0)) return { allowed: false, reason: \"The watchdog zombie window, when configured, must be a positive number of milliseconds with no code ceiling.\" };\n return { allowed: true };\n}\n\n/** Validates one mcp tool catalog against the action kind grammar: every tool name stays namespaced and unique, every wrapped kind belongs to the reviewed vocabulary, every namespace keeps its tools inside its domain kinds and every input schema carries typed properties with its required list. */\nexport function validatetoolcatalog(catalog: toolcatalog): policyevaluation {\n if (!Array.isArray(catalog.domains) || catalog.domains.length === 0) return { allowed: false, reason: \"The tool catalog needs its tool domains.\" };\n const seen = new Set<string>();\n for (const domain of catalog.domains) {\n if (!toolnamespaces.includes(domain.namespace)) return { allowed: false, reason: `The tool domain ${String(domain.namespace)} is not a reviewed namespace.` };\n if (!Array.isArray(domain.tools) || domain.tools.length === 0) return { allowed: false, reason: `The ${domain.namespace} domain exposes no tools.` };\n for (const tool of domain.tools) {\n if (typeof tool.name !== \"string\" || !tool.name.startsWith(`${domain.namespace}.`)) return { allowed: false, reason: `The tool ${String(tool.name)} does not carry its ${domain.namespace} namespace prefix.` };\n if (seen.has(tool.name)) return { allowed: false, reason: `The tool name ${tool.name} is not unique across the catalog.` };\n seen.add(tool.name);\n if (!allowedactions.has(tool.kind)) return { allowed: false, reason: `The tool ${tool.name} wraps ${String(tool.kind)} which is outside the reviewed action kind grammar.` };\n if (!domainkinds[domain.namespace].includes(tool.kind)) return { allowed: false, reason: `The tool ${tool.name} wraps ${String(tool.kind)} which does not belong to the ${domain.namespace} domain.` };\n if (typeof tool.description !== \"string\" || tool.description.trim() === \"\") return { allowed: false, reason: `The tool ${tool.name} needs its plain language description.` };\n const schema = tool.inputschema;\n if (!schema || schema.type !== \"object\" || schema.properties === undefined || schema.properties === null || typeof schema.properties !== \"object\" || Array.isArray(schema.properties) || Object.keys(schema.properties).length === 0) return { allowed: false, reason: `The tool ${tool.name} needs its json schema inputs of at least one typed property.` };\n for (const [name, property] of Object.entries(schema.properties)) {\n if (![\"string\", \"number\", \"boolean\", \"object\", \"array\"].includes(property.type)) return { allowed: false, reason: `The ${tool.name} input ${name} carries an untyped property.` };\n if (typeof property.description !== \"string\" || property.description.trim() === \"\") return { allowed: false, reason: `The ${tool.name} input ${name} needs its plain language description.` };\n }\n for (const name of schema.required) {\n if (!(name in schema.properties)) return { allowed: false, reason: `The tool ${tool.name} marks ${name} required outside its properties.` };\n }\n }\n }\n return { allowed: true };\n}\n\n/** Grades one tooldef with the risk class of its action kind and refuses a tool whose declared grade disagrees with the grammar. */\nexport function toolriskgrade(tool: tooldef): policyevaluation {\n const grade = actionrisk(tool.kind);\n if (grade !== tool.risk) return { allowed: false, reason: `The tool ${tool.name} declares the ${tool.risk} grade while its kind ${String(tool.kind)} grades ${grade}.` };\n return { allowed: true };\n}\n\n/** Requires consent metadata on every tool with side effects: read only tools stay free of the extra review while interaction and sensitive tools must declare their review requirement. */\nexport function toolconsentrequired(tool: tooldef): policyevaluation {\n if (tool.risk === \"read\") return { allowed: true };\n if (tool.consentmeta === undefined || typeof tool.consentmeta.review !== \"string\" || tool.consentmeta.review.trim() === \"\") return { allowed: false, reason: `The tool ${tool.name} has side effects and needs its consent metadata with the review requirement.` };\n return { allowed: true };\n}\n\n/** Grades one server bind configuration: the localhost bind stays the reviewed default while a bind outside localhost grades sensitive and needs the explicit remote review flag. */\nexport function serverbindgate(config: mcpserverconfig): policyevaluation {\n const bind = config.bind !== undefined && config.bind.trim() !== \"\" ? config.bind.trim() : \"127.0.0.1\";\n const local = bind === \"127.0.0.1\" || bind === \"localhost\" || bind === \"::1\";\n if (!local && config.remote !== true) return { allowed: false, reason: `The bind ${bind} leaves localhost and grades sensitive: the explicit remote review must approve it first.` };\n return { allowed: true };\n}\n\n/** Refuses one tool whose version stays below the negotiated compatibility floor so a client never receives a tool older than it can parse. */\nexport function toolversionfloor(tool: tooldef, floor: number): policyevaluation {\n if (typeof floor === \"number\" && Number.isFinite(floor) && tool.version < floor) return { allowed: false, reason: `The tool ${tool.name} of version ${tool.version} stays below the negotiated compatibility floor of ${floor}.` };\n return { allowed: true };\n}\n\n/** Requires the explicit user enablement before the mcp server ever starts; a disabled or unreviewed config never listens. */\nexport function serverenablementgate(config: mcpserverconfig): policyevaluation {\n if (config.enabled !== true) return { allowed: false, reason: \"The mcp server starts only after the user enables it; the protocol surface stays closed by default.\" };\n const bind = serverbindgate(config);\n if (!bind.allowed) return bind;\n if (!Array.isArray(config.transports) || config.transports.length === 0) return { allowed: false, reason: \"The mcp server needs at least one allowed transport of stdio or http.\" };\n if (!config.transports.every(transport => transport === \"stdio\" || transport === \"http\")) return { allowed: false, reason: \"The allowed transports of the mcp server are stdio and http.\" };\n if (typeof config.port !== \"number\" || !Number.isFinite(config.port) || config.port <= 0 || config.port > 65535) return { allowed: false, reason: \"The http listener port must be a valid port number.\" };\n if (config.framesize !== undefined && (typeof config.framesize !== \"number\" || !Number.isFinite(config.framesize) || config.framesize <= 0)) return { allowed: false, reason: \"The user configured frame size must stay a positive number with no code ceiling.\" };\n if (config.queuedepth !== undefined && (typeof config.queuedepth !== \"number\" || !Number.isFinite(config.queuedepth) || config.queuedepth <= 0)) return { allowed: false, reason: \"The user configured queue depth must stay a positive number with no code ceiling.\" };\n const remote = remoteenablementgate(config);\n if (!remote.allowed) return remote;\n return { allowed: true };\n}\n\n/** Validates the namespace membership of one tool: the name prefix must name the domain the tool lives in and the wrapped kind must belong to that domain so no tool drifts out of its namespace. */\nexport function toolnamespacegate(tool: tooldef): policyevaluation {\n const namespace = tool.name.split(\".\")[0];\n if (!toolnamespaces.includes(namespace as never)) return { allowed: false, reason: `The tool ${tool.name} carries no reviewed namespace prefix.` };\n if (!domainkinds[namespace as keyof typeof domainkinds].includes(tool.kind)) return { allowed: false, reason: `The tool ${tool.name} wraps ${String(tool.kind)} which does not belong to the ${namespace} domain.` };\n return { allowed: true };\n}\n\n/** The mcp tool dispatch gate: the client must be paired, the session live, the plan approved and the origin inside the session grants; read only tools pass under the dryrun risk class without extra approval while every tool with side effects must name the approved plan step of its own kind it executes. The full canexecute gates re-run at execution time. */\nexport function tooldispatchgate(input: { client: clientrecord; tool: tooldef; session: agentsession | undefined; plan: agentplan | undefined; origin: string; stepid?: string; now: number }): policyevaluation {\n if (input.client.disconnectedat !== undefined) return { allowed: false, reason: \"The mcp client is disconnected and its tool calls are refused.\" };\n if (!input.client.paired) return { allowed: false, reason: \"The mcp client waits for the user pairing approval; unpaired clients never dispatch tools.\" };\n if (!input.session || input.session.stoppedat || input.session.pausedat) return { allowed: false, reason: \"Tool dispatch needs the live browser session behind the consent gates.\" };\n if (input.session.expiresat <= input.now) return { allowed: false, reason: \"The browser session has expired and tool dispatch is refused.\" };\n if (!input.plan || input.plan.state !== \"approved\") return { allowed: false, reason: \"Tool dispatch needs the approved plan review before any tool runs.\" };\n if (!origingranted(input.session, input.origin)) return { allowed: false, reason: `The tool call origin ${input.origin} stays outside the session grants and is refused.` };\n if (input.tool.risk === \"read\") return { allowed: true };\n if (input.stepid === undefined || input.stepid.trim() === \"\") return { allowed: false, reason: `The ${input.tool.name} tool has side effects and needs the id of the approved plan step it executes.` };\n const step = input.plan.steps.find(candidate => candidate.id === input.stepid);\n if (step === undefined) return { allowed: false, reason: `The tool call names the step ${input.stepid} which the approved plan does not carry.` };\n if (step.kind !== input.tool.kind) return { allowed: false, reason: `The tool call names the step ${input.stepid} whose kind ${String(step.kind)} does not match the ${input.tool.name} tool.` };\n return { allowed: true };\n}\n\n/** Grades the consent metadata of every sensitive tool: the risk class must match the policy grading of the wrapped kind, the approval gate requirement must be explicit and the origin scope must stay the session grants. */\nexport function consentmetagrade(tool: tooldef): policyevaluation {\n if (tool.risk === \"read\") return { allowed: true };\n if (tool.consentmeta === undefined) return { allowed: false, reason: `The tool ${tool.name} has side effects and needs its consent metadata.` };\n if (tool.consentmeta.riskclass !== actionrisk(tool.kind)) return { allowed: false, reason: `The consent metadata of ${tool.name} declares the ${String(tool.consentmeta.riskclass)} risk class while policy grades its kind ${String(tool.kind)} as ${actionrisk(tool.kind)}.` };\n if (tool.consentmeta.approvalrequired !== true) return { allowed: false, reason: `The tool ${tool.name} has side effects and its consent metadata must require the explicit approval gate.` };\n if (tool.consentmeta.originscope !== \"session\") return { allowed: false, reason: `The tool ${tool.name} must scope its calls to the session grants.` };\n return { allowed: true };\n}\n\n/** Validates one allowlist entry against the known client identities: the fingerprint must belong to a stored identity, the display name must be non empty and every granted namespace must be a reviewed namespace. */\nexport function allowlistentryvalid(entry: allowlistentry, identities: clientidentity[]): policyevaluation {\n if (typeof entry.fingerprint !== \"string\" || entry.fingerprint.trim() === \"\") return { allowed: false, reason: \"The allowlist entry needs the client fingerprint it grants.\" };\n if (!identities.some(identity => identity.fingerprint === entry.fingerprint)) return { allowed: false, reason: `The allowlist entry ${entry.fingerprint} matches no known client identity.` };\n if (typeof entry.displayname !== \"string\" || entry.displayname.trim() === \"\") return { allowed: false, reason: `The allowlist entry ${entry.fingerprint} needs its display name.` };\n if (!Array.isArray(entry.namespaces) || entry.namespaces.length === 0) return { allowed: false, reason: `The allowlist entry ${entry.displayname} grants no tool namespace.` };\n if (!entry.namespaces.every(namespace => toolnamespaces.includes(namespace))) return { allowed: false, reason: `The allowlist entry ${entry.displayname} grants an unreviewed namespace.` };\n return { allowed: true };\n}\n\n/** Validates one session token lifetime as a user configured value: an absent lifetime keeps the documented default while a configured window must stay positive with no code ceiling. */\nexport function tokenlifetimevalid(lifetime: number | undefined): policyevaluation {\n if (lifetime === undefined) return { allowed: true };\n if (typeof lifetime !== \"number\" || !Number.isFinite(lifetime) || lifetime <= 0) return { allowed: false, reason: \"The token lifetime must stay a positive user value with no code ceiling.\" };\n return { allowed: true };\n}\n\n/** Requires tls for any non localhost transport: a configured remote access policy or a bind outside localhost must carry the on or required tls mode before any remote traffic passes. */\nexport function remotetransporttls(config: mcpserverconfig): policyevaluation {\n const bind = config.bind !== undefined && config.bind.trim() !== \"\" ? config.bind.trim() : \"127.0.0.1\";\n const local = bind === \"127.0.0.1\" || bind === \"localhost\" || bind === \"::1\";\n const tls = config.remoteaccess?.tls ?? config.httpstream?.tls;\n if ((config.remoteaccess !== undefined || !local) && (tls === undefined || tls.mode === \"off\")) return { allowed: false, reason: `The ${config.remoteaccess !== undefined ? \"remote transport\" : `bind ${bind}`} leaves localhost and every non localhost transport requires tls before any remote traffic.` };\n return { allowed: true };\n}\n\n/** Refuses the pairing flow when no session is active: pairing codes issue only while the live browser session exists, so no remote client pairs against a closed surface. */\nexport function pairingreadinessgate(session: agentsession | undefined, now: number): policyevaluation {\n if (!session || session.stoppedat || session.pausedat) return { allowed: false, reason: \"The pairing flow needs the live browser session before any code issues.\" };\n if (session.expiresat <= now) return { allowed: false, reason: \"The browser session has expired and the pairing flow is refused.\" };\n return { allowed: true };\n}\n\n/** Grades the remote transport enablement as a sensitive user choice: a configured remote access policy requires the explicit remote review and tls before the remote surface opens. */\nexport function remoteenablementgate(config: mcpserverconfig): policyevaluation {\n if (config.remoteaccess === undefined) return { allowed: true };\n if (config.remote !== true) return { allowed: false, reason: \"The remote transport enablement is a sensitive user choice and needs the explicit remote review.\" };\n const tls = remotetransporttls(config);\n if (!tls.allowed) return tls;\n if (typeof config.remoteaccess.endpoint !== \"string\" || config.remoteaccess.endpoint.trim() === \"\") return { allowed: false, reason: \"The remote access policy needs its user configured endpoint.\" };\n if (config.remoteaccess.maxclients !== undefined && (typeof config.remoteaccess.maxclients !== \"number\" || !Number.isFinite(config.remoteaccess.maxclients) || config.remoteaccess.maxclients <= 0)) return { allowed: false, reason: \"The user configured client ceiling must stay a positive value with no code ceiling.\" };\n const lifetime = tokenlifetimevalid(config.remoteaccess.tokenlifetimems);\n if (!lifetime.allowed) return lifetime;\n const timeout = approvaltimeoutvalid(config.remoteaccess.approvaltimeout);\n if (!timeout.allowed) return timeout;\n return { allowed: true };\n}\n\n/** Limits the token scopes to the namespaces the user granted: every scope must be a reviewed namespace the grant list carries, so a token never widens beyond the allowlist. */\nexport function tokenscopevalid(scopes: toolnamespace[], granted: toolnamespace[]): policyevaluation {\n if (!Array.isArray(scopes) || scopes.length === 0) return { allowed: false, reason: \"A session token needs at least one granted tool namespace.\" };\n for (const scope of scopes) {\n if (!toolnamespaces.includes(scope)) return { allowed: false, reason: `The scope ${String(scope)} is not a reviewed tool namespace.` };\n if (!granted.includes(scope)) return { allowed: false, reason: `The scope ${scope} stays outside the namespaces the user granted.` };\n }\n return { allowed: true };\n}\n\n/** Validates one approval timeout as a user configured positive window with the documented refusal default; an absent timeout keeps the documented default. */\nexport function approvaltimeoutvalid(timeout: approvaltimeout | undefined): policyevaluation {\n if (timeout === undefined) return { allowed: true };\n if (typeof timeout.windowms !== \"number\" || !Number.isFinite(timeout.windowms) || timeout.windowms <= 0) return { allowed: false, reason: \"The approval timeout must stay a positive user window with no code ceiling.\" };\n if (timeout.ontimeout !== \"refuse\") return { allowed: false, reason: \"The documented disposition of an unanswered approval gate is refusal.\" };\n return { allowed: true };\n}\n\n/** Grades the token revocation as an always available user action: no gate, review or state ever blocks the user from revoking a paired client. */\nexport function revocationgate(): policyevaluation {\n return { allowed: true };\n}\n\n/** Grades one client event subscription as read only when its filters exclude the mutation mirror: a subscription that listens to the callstarted kind must narrow itself with an origin or tool filter so it never streams the side effect calls of unreviewed origins. */\nexport function subscriptiongrade(subscription: protocoleventsubscription): policyevaluation {\n if (!Array.isArray(subscription.kinds) || subscription.kinds.length === 0) return { allowed: false, reason: \"An event subscription needs at least one protocol event kind.\" };\n if (subscription.kinds.includes(\"callstarted\") && subscription.origin === undefined && subscription.tool === undefined) return { allowed: false, reason: \"An event subscription that mirrors the callstarted events of tools with side effects needs its origin or tool filter so it never widens what the session grants.\" };\n return { allowed: true };\n}\n\n/** Grades sampling callbacks as sensitive: the prompt leaves the browser, so page content rides a callback only behind the explicit user grant and the granted maximum tokens stay a positive user value. */\nexport function samplinggrade(input: { request: samplingrequest; pagegrant: boolean }): policyevaluation {\n if (!input.pagegrant && input.request.pagecontent !== undefined) return { allowed: false, reason: \"The sampling callback carries page content the user never granted and is refused.\" };\n if (input.request.maxtokens !== undefined && (!Number.isFinite(input.request.maxtokens) || input.request.maxtokens <= 0)) return { allowed: false, reason: \"The granted maximum tokens of a sampling callback must stay a positive user value with no code ceiling.\" };\n if (input.request.prompt.trim() === \"\") return { allowed: false, reason: \"A sampling callback needs its prompt.\" };\n return { allowed: true };\n}\n\n/** Validates one per client rate limit as a user configured value: the window and budget stay positive when set while an absent limit or budget documents the unbounded choice instead of a silent default. */\nexport function callratelimitvalid(limit: callratelimit | undefined): policyevaluation {\n if (limit === undefined) return { allowed: true };\n if (typeof limit.windowms !== \"number\" || !Number.isFinite(limit.windowms) || limit.windowms <= 0) return { allowed: false, reason: \"The rate limit window must stay a positive user value with no code ceiling.\" };\n if (limit.budget !== undefined && (typeof limit.budget !== \"number\" || !Number.isFinite(limit.budget) || limit.budget <= 0)) return { allowed: false, reason: \"The rate limit budget must stay a positive user value with no code ceiling.\" };\n if (limit.clientid.trim() === \"\") return { allowed: false, reason: \"A per client rate limit needs the client it counts.\" };\n return { allowed: true };\n}\n\n/** Requires one audit entry for every tool call without exception: every call record of the runtime must appear in the audit set so no call ever leaves the trail. */\nexport function callauditcomplete(input: { calls: toolcallrecord[]; audit: toolcallrecord[] }): policyevaluation {\n const audited = new Set(input.audit.map(record => record.id));\n const missing = input.calls.filter(record => !audited.has(record.id));\n if (missing.length > 0) return { allowed: false, reason: `${missing.length} tool call${missing.length === 1 ? \"\" : \"s\"} carry no audit entry and the audit trail must name every call without exception.` };\n return { allowed: true };\n}\n\n/** Grades one batch call by its most sensitive member: a batch that carries a sensitive member takes the sensitive grade and runs only behind the approval gates while a read only batch stays read. */\nexport function batchgrade(input: { calls: Array<{ risk: tooldef[\"risk\"] }>; approved: boolean }): policyevaluation {\n if (input.calls.length === 0) return { allowed: false, reason: \"A batch call needs at least one ordered tool call.\" };\n const sensitive = input.calls.some(call => call.risk === \"sensitive\");\n if (sensitive && !input.approved) return { allowed: false, reason: \"The batch grades sensitive through its most sensitive member and runs only behind the approval gates.\" };\n return { allowed: true };\n}\n\n/** Keeps one tool dry run free of page mutations: a dry run record that claims execution or lists page mutations is refused because a dry run evaluates arguments and consent and never executes anything. */\nexport function dryrunpurity(dryrun: tooldryrun): policyevaluation {\n if (dryrun.executed) return { allowed: false, reason: `The ${dryrun.tool} dry run claims execution and a dry run never executes anything.` };\n if (dryrun.mutations.length > 0) return { allowed: false, reason: `The ${dryrun.tool} dry run lists ${dryrun.mutations.length} page mutation${dryrun.mutations.length === 1 ? \"\" : \"s\"} and a dry run leaves the page untouched.` };\n return { allowed: true };\n}\n\n/** Validates tool mock usage to test contexts only: a mock outside a test context is refused while a test context mock must name a tool and carry a canned result. */\nexport function mockusagevalid(mock: toolmock): policyevaluation {\n if (mock.testcontext !== true) return { allowed: false, reason: `The ${mock.tool} mock stays outside a test context and is refused; tool mocks never answer real calls.` };\n if (mock.tool.trim() === \"\") return { allowed: false, reason: \"A tool mock needs the namespaced tool it stands in for.\" };\n if (typeof mock.result.content !== \"string\") return { allowed: false, reason: \"A tool mock needs its canned result content.\" };\n return { allowed: true };\n}\n\n/**\n * Llm integration gates of the 1.1.57 family.\n * Every model side gate lives here: the provider validation that keeps every endpoint, model and protocol shape a user configured value, the data egress grading of provider calls, the explicit consent requirement before page content leaves the browser, the local endpoint preference for sensitive extractions, the review requirement of model drafted plans, the fresh review requirement of replanned steps, the cost budget validation, the guard verdict gate that refuses invalid model output and the plan lint that checks model drafts against the action grammar before review.\n * No provider, endpoint, model, key or ceiling is ever hardcoded: the gates validate user choices and refuse everything else.\n */\n\n/** Validates one provider config: the endpoint stays a user configured http or https url, the model list stays non-empty free text, the protocol shape stays one of the four wire shapes and the auth reference stays a storage id reference that never carries key material. */\nexport function providervalid(config: providerconfig): policyevaluation {\n if (config.name.trim() === \"\") return { allowed: false, reason: \"The provider config needs its name.\" };\n if (config.endpoint.trim() === \"\") return { allowed: false, reason: \"The provider config needs the user configured endpoint url; no default endpoint ever applies.\" };\n let parsed: URL;\n try { parsed = new URL(config.endpoint); } catch { return { allowed: false, reason: \"The provider endpoint must be a well-formed url.\" }; }\n if (parsed.protocol !== \"https:\" && parsed.protocol !== \"http:\") return { allowed: false, reason: \"The provider endpoint must speak http or https.\" };\n if (config.models.length === 0) return { allowed: false, reason: \"The provider config needs at least one user configured model name.\" };\n if (config.models.some(model => model.trim() === \"\")) return { allowed: false, reason: \"Every provider model name must stay non-empty free text.\" };\n if (config.style !== \"chatcompletions\" && config.style !== \"responses\" && config.style !== \"messages\" && config.style !== \"gemini\") return { allowed: false, reason: \"The provider protocol shape must be one of the four wire shapes the user picks.\" };\n if (config.authref !== undefined && config.authref.storageid.trim() === \"\") return { allowed: false, reason: \"The provider auth reference needs the storage id of the stored key; the key material never enters the config.\" };\n return { allowed: true };\n}\n\n/** Grades one provider call as a data egress event for the audit trail: every remote completion leaves the browser with its prompt text, so the audit names the endpoint, the model and the token counts; a local endpoint grades as the local preference. */\nexport function provideregressgrade(input: { provider: providerconfig; local: boolean }): policyevaluation {\n const valid = providervalid(input.provider);\n if (!valid.allowed) return valid;\n return { allowed: true, reason: input.local ? \"The model call stays on the local machine endpoint and grades as the local data egress preference.\" : \"The model call leaves the browser for the user configured endpoint and grades as a data egress event with its endpoint, model and token counts in the audit trail.\" };\n}\n\n/** Requires explicit consent before any page content leaves the browser: page content inside a call the user has not granted refuses the call, while calls without page content pass untouched. */\nexport function egressconsentgate(input: { pagecontent?: string; granted: boolean }): policyevaluation {\n if (input.pagecontent !== undefined && input.pagecontent.trim() !== \"\" && input.granted !== true) return { allowed: false, reason: \"The model call carries page content the user has not granted, so the content stays in the browser and the call refuses.\" };\n return { allowed: true };\n}\n\n/** Grades the local endpoint preference for sensitive extractions: a sensitive extraction prefers the local model endpoint, and the grade names the preference while a local endpoint satisfies it. */\nexport function localsensitivegrade(input: { sensitive: boolean; local: boolean }): policyevaluation {\n if (input.sensitive && !input.local) return { allowed: true, reason: \"The sensitive extraction prefers the local model endpoint; the user keeps the choice of the remote provider.\" };\n return { allowed: true, reason: input.local ? \"The local model endpoint satisfies the sensitive extraction preference.\" : \"The extraction stays non-sensitive and every configured endpoint serves it.\" };\n}\n\n/** Requires the plan review before any model drafted plan executes: only an approved draft may turn into a plan, and the plan itself still passes the same human plan review every local plan passes. */\nexport function plandraftreviewgate(draft: plandraft): policyevaluation {\n if (draft.state !== \"approved\") return { allowed: false, reason: \"The model drafted plan stays unreviewed; the human review approves the draft before any step executes.\" };\n if (draft.steps.length === 0) return { allowed: false, reason: \"The model drafted plan carries no step, so nothing executes.\" };\n return { allowed: true };\n}\n\n/** Requires fresh review for replanned steps: a pending replan never executes and every revised tail step carries the fresh review marker, so the human review sees the changed tail before it runs. */\nexport function replanreviewgate(replan: replanrecord): policyevaluation {\n if (replan.state !== \"approved\") return { allowed: false, reason: \"The replanned tail stays unreviewed; the fresh review approves the changed steps before any of them executes.\" };\n if (replan.tail.some(step => step.freshreview !== true)) return { allowed: false, reason: \"Every revised step of a replan must carry the fresh review marker.\" };\n return { allowed: true };\n}\n\n/** Validates one cost budget: the token and currency ceilings stay positive user values with no code ceiling, the currency names the unit of the cost ceiling and a budget without any ceiling documents the unbounded choice instead of inventing one. */\nexport function costbudgetvalid(budget: costbudget): policyevaluation {\n if (budget.maxtokens !== undefined && (!Number.isFinite(budget.maxtokens) || budget.maxtokens <= 0)) return { allowed: false, reason: \"The token ceiling of a cost budget must stay a positive user value.\" };\n if (budget.maxcost !== undefined && (!Number.isFinite(budget.maxcost) || budget.maxcost <= 0)) return { allowed: false, reason: \"The cost ceiling of a cost budget must stay a positive user value.\" };\n if (budget.maxcost !== undefined && (budget.currency === undefined || budget.currency.trim() === \"\")) return { allowed: false, reason: \"The cost ceiling of a cost budget needs its currency unit.\" };\n if (budget.maxtokens === undefined && budget.maxcost === undefined) return { allowed: false, reason: \"The cost budget needs at least one ceiling the user configured; an absent budget stays the documented unbounded choice.\" };\n return { allowed: true };\n}\n\n/** Refuses tool calls the guardrails marked invalid: only a valid guard verdict passes, an invalid or refused model output never executes. */\nexport function guardverdictgate(output: modeloutput): policyevaluation {\n if (output.verdict === \"invalid\") return { allowed: false, reason: output.reason ?? \"The guardrails marked the model output invalid.\" };\n if (output.verdict === \"refused\") return { allowed: false, reason: output.reason ?? \"The model refused the request, so nothing executes.\" };\n return { allowed: true };\n}\n\n/** Lints one model drafted plan against the action grammar before review: every drafted step maps onto the tool step grammar with its derived risk, and every violation lands in the findings the review sees; an empty origin skips the origin bound checks the way the review pipeline runs them later. */\n/** Derives the risk of one drafted step for the grammar check; an unknown kind grades sensitive so the grammar check names the violation instead of crashing. */\nfunction draftriskof(step: draftstep): \"read\" | \"interaction\" | \"sensitive\" {\n try { return resolvedrisk({ id: step.id, kind: step.kind as actionkind, ...(step.target !== undefined ? { target: step.target } : {}), ...(step.value !== undefined ? { value: step.value } : {}), summary: step.summary, risk: \"sensitive\" }); } catch { return \"sensitive\"; }\n}\n\nexport function planlint(draft: plandraft, origin: string): string[] {\n const findings: string[] = [];\n if (draft.goal.trim() === \"\") findings.push(\"The drafted plan carries no goal.\");\n if (draft.steps.length === 0) findings.push(\"The drafted plan carries no step.\");\n for (const step of draft.steps) {\n const mapped: toolstep = { id: step.id, kind: step.kind as actionkind, ...(step.target !== undefined ? { target: step.target } : {}), ...(step.value !== undefined ? { value: step.value } : {}), summary: step.summary, risk: draftriskof(step) } as toolstep;\n const verdict = validatestep(mapped, origin);\n if (!verdict.allowed) findings.push(`The drafted step ${step.id || \"without id\"} of kind ${step.kind || \"unknown\"} violates the action grammar: ${verdict.reason ?? \"the step failed its grammar check.\"}`);\n }\n return findings;\n}\n\n/**\n * Multi agent part one gates of the 1.1.58 family.\n * Every swarm side gate lives here: the task queue validation of the user configured lanes, priorities and completion policy, the work stealing grade that permits stealing only inside one user approved swarm, the per agent budget validation of positive user ceilings, the per agent scope validation against the session grant list, the spawn grading with the risk class of the requested role, the killswitch gate that stays available with no configuration barrier, the egress grading of cross agent messages that carry page content and the blackboard consent grade that inherits the class of the source extraction.\n * No agent count, lane name, priority scale, depth ceiling or freshness window is ever hardcoded: the gates validate user choices and refuse everything else, and no swarm coordination ever bypasses the human review.\n */\n\n/** Validates one task queue: the lanes stay non-empty unique user names, the priorities stay finite user values, the completion policy stays all or any, and every item waits in a configured lane under a configured priority when the user configured the scales. */\nexport function queuelanesvalid(queue: taskqueue): policyevaluation {\n if (queue.lanes.some(lane => lane.trim() === \"\")) return { allowed: false, reason: \"Every queue lane needs its user configured name.\" };\n if (new Set(queue.lanes).size !== queue.lanes.length) return { allowed: false, reason: \"The queue lane names must stay unique.\" };\n if (queue.priorities.some(priority => !Number.isFinite(priority))) return { allowed: false, reason: \"Every queue priority must stay a finite user value.\" };\n if (queue.completionpolicy !== \"all\" && queue.completionpolicy !== \"any\") return { allowed: false, reason: \"The queue completion policy must stay all or any.\" };\n for (const item of queue.items) {\n if (item.payload.trim() === \"\") return { allowed: false, reason: `The task ${item.id} carries no payload.` };\n if (queue.lanes.length > 0 && !queue.lanes.includes(item.lane)) return { allowed: false, reason: `The task ${item.id} waits in the lane ${item.lane} which the user did not configure.` };\n if (queue.priorities.length > 0 && !queue.priorities.includes(item.priority)) return { allowed: false, reason: `The task ${item.id} carries the priority ${item.priority} which the user did not configure.` };\n }\n return { allowed: true };\n}\n\n/** Grades one work stealing attempt: stealing is permitted only inside one user approved swarm, and a steal outside an approved swarm refuses because lane ownership only exists inside the reviewed swarm. */\nexport function workstealgrade(input: { swarmapproved: boolean; agentrole: string; lane: string; ownership?: Array<{ lane: string; roles: string[] }> }): policyevaluation {\n if (!input.swarmapproved) return { allowed: false, reason: \"Work stealing runs only inside one user approved swarm; an unapproved swarm keeps every lane closed.\" };\n const rule = (input.ownership ?? []).find(entry => entry.lane === input.lane);\n if (rule && !rule.roles.includes(input.agentrole)) return { allowed: false, reason: `The lane ${input.lane} only opens its tasks to the roles ${rule.roles.join(\", \")} the user configured; the ${input.agentrole} agent may not steal.` };\n return { allowed: true, reason: rule === undefined ? `The lane ${input.lane} carries no ownership rule, so every agent of the approved swarm may steal its tasks.` : `The lane ${input.lane} opens its tasks to the ${input.agentrole} role the user configured.` };\n}\n\n/** Validates one per agent budget: the token, cost and step ceilings stay positive user values with no code ceiling, the cost ceiling names its currency and a budget without any ceiling documents the unbounded choice instead of inventing one. */\nexport function agentbudgetvalid(budget: agentbudget): policyevaluation {\n if (budget.agentid.trim() === \"\") return { allowed: false, reason: \"The agent budget needs its agent id.\" };\n if (budget.maxtokens !== undefined && (!Number.isFinite(budget.maxtokens) || budget.maxtokens <= 0)) return { allowed: false, reason: \"The token ceiling of an agent budget must stay a positive user value.\" };\n if (budget.maxcost !== undefined && (!Number.isFinite(budget.maxcost) || budget.maxcost <= 0)) return { allowed: false, reason: \"The cost ceiling of an agent budget must stay a positive user value.\" };\n if (budget.maxsteps !== undefined && (!Number.isFinite(budget.maxsteps) || budget.maxsteps <= 0)) return { allowed: false, reason: \"The step ceiling of an agent budget must stay a positive user value.\" };\n if (budget.maxcost !== undefined && (budget.currency === undefined || budget.currency.trim() === \"\")) return { allowed: false, reason: \"The cost ceiling of an agent budget needs its currency unit.\" };\n if (budget.maxtokens === undefined && budget.maxcost === undefined && budget.maxsteps === undefined) return { allowed: false, reason: \"The agent budget needs at least one ceiling the user configured; an absent budget stays the documented unbounded choice.\" };\n return { allowed: true };\n}\n\n/** Validates one per agent scope against the session grant list: every granted origin must sit inside the session grants and every granted tool namespace must be one of the catalog namespaces, so no agent scope ever widens the session. */\nexport function agentscopevalid(input: { scope: agentscope; grants: string[] }): policyevaluation {\n if (input.scope.agentid.trim() === \"\") return { allowed: false, reason: \"The agent scope needs its agent id.\" };\n if (input.scope.origins.some(origin => origin.trim() === \"\")) return { allowed: false, reason: \"Every origin of an agent scope needs its user configured name.\" };\n if (input.scope.toolnamespaces.some(namespace => !toolnamespaces.includes(namespace))) return { allowed: false, reason: \"Every tool namespace of an agent scope must be one of the catalog namespaces.\" };\n const outside = input.scope.origins.filter(origin => input.grants.length > 0 && !input.grants.includes(origin));\n if (outside.length > 0) return { allowed: false, reason: `The agent scope grants the origins ${outside.join(\", \")} which the session grant list does not carry; no agent scope widens the session.` };\n return { allowed: true };\n}\n\n/** Grades one spawn request with the risk class of the requested role: a planner or observer spawn grades read side while a worker or custom role spawn grades sensitive because it may execute reviewed steps, and the grade names the class the review sees. */\nexport function spawngrade(request: spawnrequest): policyevaluation {\n if (request.parentid.trim() === \"\") return { allowed: false, reason: \"The spawn request needs its parent agent id.\" };\n if (request.task.trim() === \"\") return { allowed: false, reason: \"The spawn request needs its task in plain language.\" };\n if (request.depth < 1) return { allowed: false, reason: \"The spawn request depth must sit at one or deeper because every sub agent lives under a parent.\" };\n const risk = request.role === \"planner\" || request.role === \"observer\" ? \"read\" : \"sensitive\";\n return { allowed: true, reason: risk === \"read\" ? `The spawn of the ${request.role} sub agent grades read side: the role composes or observes and never executes page actions.` : `The spawn of the ${request.role} sub agent grades sensitive: the role may execute reviewed steps, so every proposal it drafts still passes the same human review.` };\n}\n\n/** Keeps the killswitch available with no configuration barrier: the switch never needs a setting, an approval or a state to fire, so the user halts every agent at once at any time. */\nexport function killswitchgate(): policyevaluation {\n return { allowed: true, reason: \"The killswitch stays available with no configuration barrier: the user halts every agent of the swarm at once at any time.\" };\n}\n\n/** Grades one cross agent message that carries page content as a data egress event: the delivery stays inside the swarm while the copied page content lands its egress class in the audit trail so the reviewer reads what moved between agents. */\nexport function messageegressgrade(input: { message: agentmessage; carriespagecontent: boolean }): policyevaluation {\n if (input.message.payload.trim() === \"\") return { allowed: false, reason: \"The agent message needs its payload.\" };\n if (input.carriespagecontent) return { allowed: true, reason: `The message ${input.message.id} from ${input.message.senderid} to ${input.message.recipient} carries page content and grades as a data egress event with its sender, recipient and routing in the audit trail.` };\n return { allowed: true, reason: `The message ${input.message.id} from ${input.message.senderid} to ${input.message.recipient} carries no page content and stays a plain swarm delivery.` };\n}\n\n/** Grades one blackboard entry by the consent class of its source extraction: the entry inherits the class exactly, a sensitive extraction stays sensitive on the board and every reader sees the class beside the value. */\nexport function blackboardconsentgrade(entry: blackboardentry): policyevaluation {\n if (entry.key.trim() === \"\") return { allowed: false, reason: \"The blackboard entry needs its key.\" };\n if (entry.consentclass !== \"read\" && entry.consentclass !== \"interaction\" && entry.consentclass !== \"sensitive\") return { allowed: false, reason: \"The blackboard entry inherits one of the three consent classes of its source extraction.\" };\n if (entry.consentclass === \"sensitive\") return { allowed: true, reason: `The blackboard entry ${entry.key} inherits the sensitive class of its source extraction; every agent reads the class beside the value.` };\n return { allowed: true, reason: `The blackboard entry ${entry.key} inherits the ${entry.consentclass} class of its source extraction; every agent reads the class beside the value.` };\n}\n\n/**\n * Multi agent part two gates of the 1.1.59 family.\n * Every orchestration side gate lives here: the leader election validation of the user configured rule, the critic review grade that stays read only over agent outputs, the verifier method grade against the methods the user allows, the handoff grant gate that preserves the original session grants, the lock scope validation that keeps one lock inside one origin, the conflict resolution grade that marks overwriting rules sensitive, the escalation gate that keeps every lifted decision human, the consensus quorum validation of the user configured value, the worker scale bound validation with no engine cap and the merge egress grade of exported reports that include page content.\n * No quorum, worker bound, election rule or merge rule is ever hardcoded: the gates validate user choices and refuse everything else, and no coordination path bypasses the human review.\n */\n\n/** Validates one leader election rule as the user configured it: first takes the first registration while named takes one agent id the user typed, and a named rule without its agent or an unknown rule kind refuses. */\nexport function leaderelectionvalid(input: { rule: { kind: string; agentid?: string }; agents: agentidentity[] }): policyevaluation {\n if (input.rule.kind !== \"first\" && input.rule.kind !== \"named\") return { allowed: false, reason: \"The leader election rule stays first or named as the user configured it.\" };\n if (input.rule.kind === \"named\") {\n if (input.rule.agentid === undefined || input.rule.agentid.trim() === \"\") return { allowed: false, reason: \"The named leader election rule needs the agent id the user named.\" };\n if (!input.agents.some(agent => agent.id === input.rule.agentid && agent.state !== \"stopped\")) return { allowed: false, reason: `The named leader election rule names the agent ${input.rule.agentid} which is not a live agent of the swarm.` };\n }\n return { allowed: true, reason: input.rule.kind === \"first\" ? \"The first registration rule elects the leader exactly as the user configured.\" : `The named rule elects the agent ${input.rule.agentid} exactly as the user configured.` };\n}\n\n/** Grades one critic review as read only over agent outputs: the critic reads the output of the subject agent and returns its verdict, its issues and its required changes without ever acting on the page, and a review without its reviewer, subject or verdict refuses. */\nexport function criticreviewgrade(review: criticreview): policyevaluation {\n if (review.reviewerid.trim() === \"\") return { allowed: false, reason: \"The critic review needs its reviewing agent.\" };\n if (review.subjectagentid.trim() === \"\") return { allowed: false, reason: \"The critic review needs the subject agent whose output it reviews.\" };\n if (review.verdict !== \"approve\" && review.verdict !== \"changes\" && review.verdict !== \"reject\") return { allowed: false, reason: \"The critic review carries one of the three verdicts approve, changes or reject.\" };\n if (review.verdict === \"changes\" && review.requiredchanges.length === 0) return { allowed: false, reason: \"A changes verdict needs its required changes in plain language.\" };\n if (review.verdict === \"reject\" && review.issues.length === 0) return { allowed: false, reason: \"A reject verdict needs the issues the critic found.\" };\n return { allowed: true, reason: `The critic review of the output of ${review.subjectagentid} stays read only: the critic ${review.reviewerid} returns its ${review.verdict} verdict and never acts on the page; the rework still passes the same human review.` };\n}\n\n/** Grades one verifier check by the methods the user allows: an empty allowed list keeps every method open as the documented user choice while a configured list restricts the verifier to the methods it names. */\nexport function verifiermethodgrade(input: { method: string; allowed: string[] }): policyevaluation {\n if (input.method.trim() === \"\") return { allowed: false, reason: \"The verifier check needs the method it used.\" };\n if (input.allowed.length > 0 && !input.allowed.includes(input.method)) return { allowed: false, reason: `The verifier method ${input.method} is not one of the methods the user allowed: ${input.allowed.join(\", \")}.` };\n return { allowed: true, reason: input.allowed.length === 0 ? `The verifier method ${input.method} runs under the documented open method list the user chose not to narrow.` : `The verifier method ${input.method} sits inside the methods the user allowed.` };\n}\n\n/** Requires one handoff to preserve the original session grants: the receiving agent scope stays inside the session grant list exactly like every agent scope, so a tab transfer never widens what the session granted. */\nexport function handoffgrantgate(input: { record: handoffrecord; toscope: agentscope | undefined; sessiongrants: string[] }): policyevaluation {\n if (input.record.toagentid.trim() === \"\" || input.record.fromagentid.trim() === \"\") return { allowed: false, reason: \"The handoff names its transferring and receiving agents.\" };\n if (input.toscope === undefined) return { allowed: true, reason: `The receiving agent ${input.record.toagentid} carries no narrowed scope, so the handoff stays unbounded inside the original session grants.` };\n const outside = input.toscope.origins.filter(origin => input.sessiongrants.length > 0 && !input.sessiongrants.includes(origin));\n if (outside.length > 0) return { allowed: false, reason: `The handoff to ${input.record.toagentid} would need the origins ${outside.join(\", \")} which the session grant list does not carry; a tab transfer never widens the session grants.` };\n return { allowed: true, reason: `The handoff from ${input.record.fromagentid} to ${input.record.toagentid} preserves the original session grants; the receiving scope stays inside them.` };\n}\n\n/** Validates one lock scope so a lock never spans unrelated origins: the key composes of exactly one origin and one selector, both non-empty, and the kind stays exclusive or shared. */\nexport function lockscopevalid(lock: resourcelock): policyevaluation {\n if (lock.key.trim() === \"\") return { allowed: false, reason: \"The resource lock needs its key.\" };\n if (lock.origin.trim() === \"\" || lock.selector.trim() === \"\") return { allowed: false, reason: \"The resource lock names exactly one origin and one selector; a lock never spans unrelated origins.\" };\n if (lock.key !== `${lock.origin}|${lock.selector}`) return { allowed: false, reason: \"The lock key must compose of its one origin and its one selector so the scope never spans unrelated origins.\" };\n if (lock.kind !== \"exclusive\" && lock.kind !== \"shared\") return { allowed: false, reason: \"The lock kind stays exclusive or shared.\" };\n return { allowed: true, reason: `The lock ${lock.key} spans exactly one target of one origin for the holder ${lock.holder}.` };\n}\n\n/** Grades one conflict resolution rule: the overwriting rules last and preferagent grade sensitive because one parallel value overwrites another inside the report, while first and fail grade read side. */\nexport function conflictresolutiongrade(rule: mergerule): policyevaluation {\n if (rule !== \"first\" && rule !== \"last\" && rule !== \"preferagent\" && rule !== \"fail\") return { allowed: false, reason: \"The conflict resolution rule stays first, last, preferagent or fail as the user configured it.\" };\n if (rule === \"last\" || rule === \"preferagent\") return { allowed: true, reason: `The ${rule} conflict resolution rule overwrites one parallel value with another, so it grades sensitive and the merged report still passes the human review.` };\n return { allowed: true, reason: `The ${rule} conflict resolution rule keeps or refuses the parallel values without overwriting, so it grades read side.` };\n}\n\n/** Grades one escalation as always human decided: the open escalation waits for the user and only the user writes the decision; an escalation without its subject or full context refuses because the user decides on what the agent saw. */\nexport function escalationgate(escalation: escalationrecord): policyevaluation {\n if (escalation.agentid.trim() === \"\") return { allowed: false, reason: \"The escalation names the agent whose decision it lifts.\" };\n if (escalation.subject.trim() === \"\") return { allowed: false, reason: \"The escalation needs its subject.\" };\n if (escalation.context.trim() === \"\") return { allowed: false, reason: \"The escalation needs its full context in plain language; the user decides on what the agent saw.\" };\n if (escalation.state === \"decided\" && (escalation.decision === undefined || escalation.decision.trim() === \"\")) return { allowed: false, reason: \"A decided escalation carries the decision the user wrote.\" };\n return { allowed: true, reason: `The escalation of ${escalation.agentid} stays human decided: the agent lifts the stalled decision with its full context and the user alone writes the outcome.` };\n}\n\n/** Validates one consensus quorum as a user configured value: the quorum stays a positive whole number and never exceeds the live voters the user counted, so no round carries an unreachable quorum. */\nexport function consensusquorumvalid(input: { quorum: number; voters: number }): policyevaluation {\n if (!Number.isInteger(input.quorum) || input.quorum < 1) return { allowed: false, reason: \"The consensus quorum stays a positive whole number the user configured.\" };\n if (input.quorum > input.voters) return { allowed: false, reason: `The consensus quorum ${input.quorum} exceeds the ${input.voters} voting agents the user counted; an unreachable quorum never carries.` };\n return { allowed: true, reason: `The consensus quorum ${input.quorum} of ${input.voters} voting agents stays the user configured value with no engine default.` };\n}\n\n/** Validates the worker scale bound as a user choice with no engine cap: an absent bound stays the documented unbounded choice while a configured bound stays a positive user value the scaling respects. */\nexport function workerscalevalid(bound: number | undefined): policyevaluation {\n if (bound === undefined) return { allowed: true, reason: \"No worker bound is configured, so the worker scale stays the user choice alone with no engine cap.\" };\n if (!Number.isFinite(bound) || bound < 1) return { allowed: false, reason: \"The worker scale bound stays a positive user value; no engine cap exists.\" };\n return { allowed: true, reason: `The worker scale bound ${bound} stays the user configured value; the scaling never passes it and no engine cap exists.` };\n}\n\n/** Grades one exported merged report that includes page content as a data egress event: the export carries the report with its sources into the audit trail so the reviewer reads what left the browser. */\nexport function mergeegressgrade(input: { report: resultreport; carriespagecontent: boolean }): policyevaluation {\n if (input.report.title.trim() === \"\") return { allowed: false, reason: \"The merged report needs its title before any export.\" };\n if (input.carriespagecontent) return { allowed: true, reason: `The export of the report ${input.report.title} carries page content from the sources ${input.report.sources.join(\", \")} and grades as a data egress event in the audit trail.` };\n return { allowed: true, reason: `The export of the report ${input.report.title} carries no page content and stays a plain report export.` };\n}\n", "import type { toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\n\n/**\n * Mutating page interactions for reviewed steps.\n * Every correlated rule for pointer, key, drag, upload, form, attribute, storage and evaluation actions lives in this file.\n */\n\nexport type stepresult = { ok: boolean; summary: string; details?: Record<string, unknown> };\n\nfunction events(target: Element): void {\n target.dispatchEvent(new Event(\"input\", { bubbles: true }));\n target.dispatchEvent(new Event(\"change\", { bubbles: true }));\n}\n\nfunction modifiers(options: Record<string, unknown>): string[] {\n return Array.isArray(options.modifiers) ? options.modifiers.filter((item): item is string => typeof item === \"string\") : [];\n}\n\nfunction keyevent(type: \"keydown\" | \"keypress\" | \"keyup\", key: string, mods: string[]): KeyboardEvent {\n const code = key.length === 1 ? `Key${key.toUpperCase()}` : key;\n return new KeyboardEvent(type, { key, code, bubbles: true, cancelable: true, composed: true, ctrlKey: mods.includes(\"ctrl\"), shiftKey: mods.includes(\"shift\"), altKey: mods.includes(\"alt\"), metaKey: mods.includes(\"meta\") });\n}\n\nfunction fieldlike(target: Element | null): HTMLInputElement | HTMLTextAreaElement | null {\n return target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement ? target : null;\n}\n\nfunction stringify(value: unknown): unknown {\n try { return JSON.parse(JSON.stringify(value)) ?? null; } catch { return String(value); }\n}\n\n/** Runs one mutating page action after the background policy gate and a fresh target check. */\nexport function runpageaction(step: toolstep, target: Element | null): stepresult | Promise<stepresult> {\n const options = (() => { try { return parseoptions(step); } catch { return {} as Record<string, unknown>; } })();\n switch (step.kind) {\n case \"presskey\": {\n const receiver = target instanceof HTMLElement ? target : document.activeElement instanceof HTMLElement ? document.activeElement : document.body;\n const key = step.value ?? \"\";\n const mods = modifiers(options);\n receiver.dispatchEvent(keyevent(\"keydown\", key, mods));\n receiver.dispatchEvent(keyevent(\"keypress\", key, mods));\n receiver.dispatchEvent(keyevent(\"keyup\", key, mods));\n return { ok: true, summary: `Key ${key} delivered with ${mods.length} modifier${mods.length === 1 ? \"\" : \"s\"}.` };\n }\n case \"clickdeep\": {\n if (!(target instanceof HTMLElement)) return { ok: false, summary: \"Action target is no longer available.\" };\n target.dispatchEvent(new PointerEvent(\"pointerdown\", { bubbles: true, cancelable: true, composed: true }));\n target.dispatchEvent(new MouseEvent(\"mousedown\", { bubbles: true, cancelable: true }));\n target.dispatchEvent(new PointerEvent(\"pointerup\", { bubbles: true, cancelable: true, composed: true }));\n target.dispatchEvent(new MouseEvent(\"mouseup\", { bubbles: true, cancelable: true }));\n target.click();\n return { ok: true, summary: \"Full pointer click sequence delivered.\" };\n }\n case \"rightclick\": {\n if (!(target instanceof HTMLElement)) return { ok: false, summary: \"Action target is no longer available.\" };\n const init: MouseEventInit = { bubbles: true, cancelable: true, button: 2, buttons: 2 };\n target.dispatchEvent(new PointerEvent(\"pointerdown\", { ...init, composed: true }));\n target.dispatchEvent(new MouseEvent(\"mousedown\", init));\n target.dispatchEvent(new MouseEvent(\"contextmenu\", init));\n return { ok: true, summary: \"Context menu events delivered.\" };\n }\n case \"doubleclick\": {\n if (!(target instanceof HTMLElement)) return { ok: false, summary: \"Action target is no longer available.\" };\n target.click();\n target.click();\n target.dispatchEvent(new MouseEvent(\"dblclick\", { bubbles: true, cancelable: true, detail: 2 }));\n return { ok: true, summary: \"Double click sequence delivered.\" };\n }\n case \"drag\": {\n if (!(target instanceof HTMLElement)) return { ok: false, summary: \"Drag source is no longer available.\" };\n const destination = document.querySelector(step.value ?? \"\");\n if (!destination) return { ok: false, summary: \"Drag destination is no longer available.\" };\n const transfer = new DataTransfer();\n if (typeof options.data === \"string\") transfer.setData(\"text/plain\", options.data);\n target.dispatchEvent(new DragEvent(\"dragstart\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n destination.dispatchEvent(new DragEvent(\"dragenter\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n destination.dispatchEvent(new DragEvent(\"dragover\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n destination.dispatchEvent(new DragEvent(\"drop\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n target.dispatchEvent(new DragEvent(\"dragend\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n return { ok: true, summary: \"Drag and drop sequence delivered.\" };\n }\n case \"drop\": {\n if (!(target instanceof HTMLElement)) return { ok: false, summary: \"Drop zone is no longer available.\" };\n const transfer = new DataTransfer();\n transfer.setData(\"text/plain\", step.value ?? \"\");\n target.dispatchEvent(new DragEvent(\"dragenter\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n target.dispatchEvent(new DragEvent(\"dragover\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n target.dispatchEvent(new DragEvent(\"drop\", { bubbles: true, cancelable: true, dataTransfer: transfer }));\n return { ok: true, summary: \"Drop payload delivered.\" };\n }\n case \"upload\": {\n if (!(target instanceof HTMLInputElement) || target.type !== \"file\") return { ok: false, summary: \"Target is not a file input.\" };\n const transfer = new DataTransfer();\n transfer.items.add(new File([typeof options.content === \"string\" ? options.content : \"\"], step.value ?? \"upload\", { type: typeof options.type === \"string\" ? options.type : \"text/plain\" }));\n target.files = transfer.files;\n events(target);\n return { ok: true, summary: `Uploaded ${step.value ?? \"file\"} into the reviewed input.` };\n }\n case \"clear\": {\n const field = fieldlike(target);\n if (!field) return { ok: false, summary: \"Target cannot hold a value.\" };\n field.value = \"\";\n events(field);\n return { ok: true, summary: \"Field cleared.\" };\n }\n case \"check\":\n case \"uncheck\":\n case \"toggle\": {\n if (!(target instanceof HTMLInputElement) || (target.type !== \"checkbox\" && target.type !== \"radio\")) return { ok: false, summary: \"Target is not a checkbox or radio control.\" };\n if (step.kind === \"uncheck\" && target.type === \"radio\") return { ok: false, summary: \"A radio control cannot be unchecked.\" };\n target.checked = step.kind === \"toggle\" ? !target.checked : step.kind === \"check\";\n events(target);\n return { ok: true, summary: `Control is now ${target.checked ? \"checked\" : \"unchecked\"}.` };\n }\n case \"submit\": {\n const form = target instanceof HTMLFormElement ? target : target instanceof HTMLElement ? target.closest(\"form\") : null;\n if (!form) return { ok: false, summary: \"No form owns the reviewed target.\" };\n try { form.requestSubmit(target instanceof HTMLFormElement ? undefined : (target as HTMLElement)); } catch { form.submit(); }\n return { ok: true, summary: \"Form submission requested.\" };\n }\n case \"setattribute\": {\n if (!target) return { ok: false, summary: \"Action target is no longer available.\" };\n const name = typeof options.name === \"string\" ? options.name : \"\";\n target.setAttribute(name, typeof options.value === \"string\" ? options.value : \"\");\n return { ok: true, summary: `Attribute ${name} set.` };\n }\n case \"removeattribute\": {\n if (!target) return { ok: false, summary: \"Action target is no longer available.\" };\n const name = step.value ?? \"\";\n target.removeAttribute(name);\n return { ok: true, summary: `Attribute ${name} removed.` };\n }\n case \"writestorage\": {\n try {\n localStorage.setItem(typeof options.key === \"string\" ? options.key : \"\", typeof options.value === \"string\" ? options.value : \"\");\n return { ok: true, summary: `Local storage entry ${String(options.key)} written.` };\n } catch (error) { return { ok: false, summary: `Local storage refused the write: ${error instanceof Error ? error.message : String(error)}` }; }\n }\n case \"evaluate\": {\n try {\n let outcome: unknown;\n try { outcome = new Function(`\"use strict\"; return (${step.value ?? \"undefined\"});`)(); } catch { outcome = new Function(`\"use strict\"; ${step.value ?? \"\"}`)(); }\n return { ok: true, summary: `Reviewed expression returned ${outcome === undefined ? \"no value\" : \"a value\"}.`, details: { result: stringify(outcome) } };\n } catch (error) { return { ok: false, summary: `Reviewed expression failed: ${error instanceof Error ? error.message : String(error)}` }; }\n }\n case \"fullscreen\": {\n const element = target instanceof HTMLElement ? target : document.documentElement;\n return document.fullscreenElement === element\n ? document.exitFullscreen().then(() => ({ ok: true, summary: \"Fullscreen state cleared.\" }) as stepresult)\n : element.requestFullscreen().then(() => ({ ok: true, summary: \"Fullscreen state entered.\" }) as stepresult).catch(error => ({ ok: false, summary: `Fullscreen was refused: ${error instanceof Error ? error.message : String(error)}` }));\n }\n default: return { ok: false, summary: \"Unsupported page action.\" };\n }\n}\n", "/**\n * Xpath resolution subset for reviewed steps.\n * Every correlated rule for expression parsing, node trees and evaluation of the supported xpath grammar lives in this file.\n * The engine supports absolute and descendant steps, tag or wildcard names, attribute predicates, text predicates and positional predicates.\n */\n\n/** Serializable dom node used by the xpath engine; the live glue attaches the element. */\nexport interface xnode {\n tag: string;\n attributes: Record<string, string>;\n text: string;\n children: xnode[];\n element?: Element;\n}\n\ntype xpathpredicate =\n | { kind: \"attr\"; name: string; value?: string; contains?: boolean }\n | { kind: \"text\"; value: string; contains?: boolean }\n | { kind: \"position\"; index: number };\n\ninterface xpathstep {\n descendant: boolean;\n tag: string;\n predicates: xpathpredicate[];\n}\n\nfunction owntext(element: Element): string {\n let combined = \"\";\n for (const node of element.childNodes) if (node.nodeType === Node.TEXT_NODE) combined += node.textContent ?? \"\";\n return combined.replace(/\\s+/g, \" \").trim();\n}\n\nfunction attributesof(element: Element): Record<string, string> {\n const attributes: Record<string, string> = {};\n for (const attribute of [...element.attributes]) attributes[attribute.name] = attribute.value;\n return attributes;\n}\n\nfunction wrap(element: Element): xnode {\n return { tag: element.tagName.toLowerCase(), attributes: attributesof(element), text: owntext(element), children: [...element.children].map(wrap), element };\n}\n\n/** Builds the serializable node tree of one live document for the xpath engine. */\nexport function buildxtree(root: Document): xnode {\n return { tag: \"#document\", attributes: {}, text: \"\", children: root.documentElement ? [wrap(root.documentElement)] : [] };\n}\n\n/** Parses one predicate body into the supported predicate shapes. */\nfunction parsepredicate(raw: string): xpathpredicate | null {\n const body = raw.trim();\n let match = /^@([\\w-]+)$/.exec(body);\n if (match) return { kind: \"attr\", name: match[1] as string };\n match = /^@([\\w-]+)\\s*=\\s*['\"]([^'\"]*)['\"]$/.exec(body);\n if (match) return { kind: \"attr\", name: match[1] as string, value: match[2] as string };\n match = /^contains\\(\\s*@([\\w-]+)\\s*,\\s*['\"]([^'\"]*)['\"]\\s*\\)$/.exec(body);\n if (match) return { kind: \"attr\", name: match[1] as string, value: match[2] as string, contains: true };\n match = /^text\\(\\)\\s*=\\s*['\"]([^'\"]*)['\"]$/.exec(body);\n if (match) return { kind: \"text\", value: match[1] as string };\n match = /^contains\\(\\s*text\\(\\)\\s*,\\s*['\"]([^'\"]*)['\"]\\s*\\)$/.exec(body);\n if (match) return { kind: \"text\", value: match[1] as string, contains: true };\n match = /^(\\d+)$/.exec(body);\n if (match) return { kind: \"position\", index: Number.parseInt(match[1] as string, 10) };\n return null;\n}\n\n/** Parses the reviewed xpath expression into evaluation steps; unsupported syntax is refused. */\nexport function parsexpath(expression: string): xpathstep[] {\n const trimmed = expression.trim();\n if (!trimmed.startsWith(\"/\")) throw new Error(\"The reviewed xpath expression must start with a slash.\");\n const steps: xpathstep[] = [];\n let index = 0;\n while (index < trimmed.length) {\n if (trimmed[index] !== \"/\") throw new Error(\"The reviewed xpath expression contains an unsupported segment.\");\n let slashes = 0;\n while (index < trimmed.length && trimmed[index] === \"/\") { slashes += 1; index += 1; }\n const start = index;\n let quote = \"\";\n while (index < trimmed.length) {\n const character = trimmed[index];\n if (quote) { if (character === quote) quote = \"\"; }\n else if (character === \"'\" || character === '\"') quote = character;\n else if (character === \"/\") break;\n index += 1;\n }\n const body = trimmed.slice(start, index);\n if (!body) throw new Error(\"The reviewed xpath expression contains an empty step.\");\n const parsed = /^(\\*|[a-zA-Z][\\w-]*)((?:\\[[^\\]]*\\])*)$/.exec(body);\n if (!parsed) throw new Error(`The reviewed xpath step ${body} is not supported.`);\n const predicates: xpathpredicate[] = [];\n const pattern = /\\[([^\\]]*)\\]/g;\n let predicate: RegExpExecArray | null;\n while ((predicate = pattern.exec(parsed[2] ?? \"\")) !== null) {\n const parsedpredicate = parsepredicate(predicate[1] as string);\n if (!parsedpredicate) throw new Error(`The reviewed xpath predicate [${predicate[1]}] is not supported.`);\n predicates.push(parsedpredicate);\n }\n steps.push({ descendant: slashes > 1, tag: (parsed[1] as string).toLowerCase(), predicates });\n }\n return steps;\n}\n\nfunction descendants(node: xnode, includeself: boolean): xnode[] {\n const result: xnode[] = includeself ? [node] : [];\n for (const child of node.children) { result.push(child); result.push(...descendants(child, false)); }\n return result;\n}\n\nfunction applypredicates(nodes: xnode[], predicates: xpathpredicate[]): xnode[] {\n let result = nodes;\n for (const predicate of predicates) {\n if (predicate.kind === \"position\") {\n const entry = result[predicate.index - 1];\n result = entry ? [entry] : [];\n continue;\n }\n result = result.filter(node => {\n if (predicate.kind === \"attr\") {\n const value = node.attributes[predicate.name];\n if (value === undefined) return false;\n if (predicate.value === undefined) return true;\n return predicate.contains ? value.includes(predicate.value) : value === predicate.value;\n }\n return predicate.contains ? node.text.includes(predicate.value) : node.text === predicate.value;\n });\n }\n return result;\n}\n\n/** Evaluates the supported xpath subset against a serializable node tree and returns every matched node in document order. */\nexport function evaluatexpath(root: xnode, expression: string): xnode[] {\n const steps = parsexpath(expression);\n let current: xnode[] = [root];\n let first = true;\n for (const step of steps) {\n let matched: xnode[] = [];\n for (const node of current) {\n const pool = step.descendant ? descendants(node, first) : node.children;\n matched = matched.concat(pool.filter(candidate => candidate.tag === step.tag || step.tag === \"*\"));\n }\n current = applypredicates(matched, step.predicates);\n first = false;\n }\n return current;\n}\n", "import { parseoptions, resolutionverdict } from \"../policy.js\";\nimport type { clickablemap, mapentry, resolvedtarget, targetmode, toolstep } from \"../types.js\";\nimport { buildxtree, evaluatexpath } from \"./pagexpath.js\";\n\n/**\n * Target resolution engine for reviewed steps.\n * Every correlated rule for element summaries, targetref modes, ambiguity reports, shadow piercing, frame walking and the numbered clickable map lives in this file.\n */\n\n/** Descriptive fields shared by every resolution candidate; the live dom glue attaches the element. */\nexport interface candidatefields {\n tag: string;\n id: string;\n role: string;\n name: string;\n label: string;\n text: string;\n selector: string;\n}\n\n/** One live dom candidate: descriptive fields plus the element itself. */\nexport interface livecandidate extends candidatefields {\n element: Element;\n}\n\n/** Serializable description of one document with nested frames; the glue attaches the live document. */\nexport interface framedescription {\n frames: frameentry[];\n live?: Document;\n}\n\n/** One nested frame: same origin frames expose their document, cross origin frames do not. */\nexport interface frameentry {\n sameorigin: boolean;\n document?: framedescription;\n}\n\n/** Serializable scope tree: one scope's candidates plus the open shadow scopes nested inside it. */\nexport interface scopetree<T extends candidatefields = livecandidate> {\n host?: T;\n candidates: T[];\n shadows: scopetree<T>[];\n}\n\nexport function clean(value: string): string {\n return value.replace(/\\s+/g, \" \").trim();\n}\n\nfunction cssescape(value: string): string {\n return typeof CSS !== \"undefined\" && typeof CSS.escape === \"function\" ? CSS.escape(value) : value.replace(/[^a-zA-Z0-9_-]/g, \"\\\\$&\");\n}\n\n/** Computes the accessible style label of an element from aria hints, linked labels, title and content. */\nexport function elementlabel(element: Element): string {\n const aria = element.getAttribute(\"aria-label\");\n let linked = \"\";\n const labelledby = element.getAttribute(\"aria-labelledby\");\n if (labelledby) {\n try {\n const owner = element.ownerDocument?.getElementById(labelledby);\n if (owner) linked = owner.textContent ?? \"\";\n } catch { /* detached documents refuse lookups; the label falls back */ }\n }\n let forlabel = \"\";\n if (element.id) {\n try {\n const label = element.ownerDocument?.querySelector(`label[for=\"${cssescape(element.id)}\"]`);\n if (label instanceof HTMLElement) forlabel = label.textContent ?? \"\";\n } catch { /* the document may be detached; the label falls back */ }\n }\n return clean(aria || linked || forlabel || element.getAttribute(\"title\") || element.textContent || \"\");\n}\n\n/** Computes the implicit aria role of common elements when no explicit role attribute exists. */\nexport function implicitrole(element: Element): string {\n const tag = element.tagName.toLowerCase();\n if (tag === \"button\") return \"button\";\n if (tag === \"a\" && element.getAttribute(\"href\")) return \"link\";\n if (tag === \"select\") return \"combobox\";\n if (tag === \"textarea\") return \"textbox\";\n if (tag === \"details\") return \"group\";\n if (tag === \"input\") {\n const type = element.getAttribute(\"type\") ?? \"text\";\n if (type === \"checkbox\") return \"checkbox\";\n if (type === \"radio\") return \"radio\";\n if (type === \"button\" || type === \"submit\" || type === \"reset\") return \"button\";\n if (type === \"range\") return \"slider\";\n return \"textbox\";\n }\n return \"\";\n}\n\n/** Builds the css selector that re-finds one element inside its document. */\nexport function elementselector(element: Element): string {\n if (element.id) return `#${cssescape(element.id)}`;\n const role = element.getAttribute(\"role\");\n const name = element.getAttribute(\"name\");\n if (role && name) return `[role=\"${cssescape(role)}\"][name=\"${cssescape(name)}\"]`;\n if (name) return `${element.tagName.toLowerCase()}[name=\"${cssescape(name)}\"]`;\n const tag = element.tagName.toLowerCase();\n const parent = element.parentElement;\n if (!parent) return tag;\n const peers = [...parent.children].filter(node => node.tagName === element.tagName);\n return `${tag}:nth-of-type(${peers.indexOf(element) + 1})`;\n}\n\n/** Own text of an element: only its direct text nodes, so parents do not shadow their children. */\nexport function owntext(element: Element): string {\n let combined = \"\";\n for (const node of element.childNodes) if (node.nodeType === Node.TEXT_NODE) combined += node.textContent ?? \"\";\n return clean(combined);\n}\n\n/** Summarizes one live element into the candidate shape used by every resolution mode. */\nexport function summarize(element: Element): livecandidate {\n return {\n tag: element.tagName.toLowerCase(),\n id: element.id,\n role: element.getAttribute(\"role\")?.toLowerCase() || implicitrole(element),\n name: element.getAttribute(\"name\") ?? \"\",\n label: elementlabel(element),\n text: owntext(element),\n selector: elementselector(element),\n element,\n };\n}\n\nconst clickableselector = \"a[href], button, input, textarea, select, summary, [role=button], [role=link], [role=combobox], [role=option], [role=checkbox], [role=radio], [role=switch], [role=tab]\";\n\n/** Collects every clickable candidate in document order. */\nexport function collectclickable(root: ParentNode): livecandidate[] {\n return [...root.querySelectorAll(clickableselector)].map(summarize);\n}\n\n/** Collects every element as a resolution candidate in document order. */\nexport function collectcandidates(root: ParentNode): livecandidate[] {\n return [...root.querySelectorAll(\"*\")].map(summarize);\n}\n\n/** Matches candidates whose visible own text or label equals or contains the reviewed text. */\nexport function matchtext<T extends candidatefields>(candidates: T[], text: string): T[] {\n const wanted = clean(text).toLowerCase();\n if (!wanted) return [];\n const exact = candidates.filter(candidate => candidate.text.toLowerCase() === wanted || candidate.label.toLowerCase() === wanted);\n if (exact.length > 0) return exact;\n return candidates.filter(candidate => candidate.text.toLowerCase().includes(wanted) || candidate.label.toLowerCase().includes(wanted));\n}\n\n/** Matches candidates by the aria role and accessible name pair. */\nexport function matcharia<T extends candidatefields>(candidates: T[], role: string, name: string): T[] {\n const wantedrole = clean(role).toLowerCase();\n const wantedname = clean(name).toLowerCase();\n if (!wantedrole || !wantedname) return [];\n return candidates.filter(candidate => candidate.role.toLowerCase() === wantedrole && (candidate.label.toLowerCase() === wantedname || candidate.name.toLowerCase() === wantedname));\n}\n\n/** Matches candidates by accessible name, preferring clickable elements when several share one name. */\nexport function matchname<T extends candidatefields>(candidates: T[], name: string, clickable?: (candidate: T) => boolean): T[] {\n const wanted = clean(name).toLowerCase();\n if (!wanted) return [];\n const matches = candidates.filter(candidate => candidate.label.toLowerCase() === wanted || candidate.name.toLowerCase() === wanted);\n if (matches.length > 1 && clickable) {\n const interactive = matches.filter(clickable);\n if (interactive.length === 1) return interactive;\n }\n return matches;\n}\n\n/** Matches one clickable map number; map numbers are one based and stable inside one observation version. */\nexport function matchindex<T extends candidatefields>(candidates: T[], index: number): T[] {\n if (!Number.isInteger(index) || index < 1) return [];\n const entry = candidates[index - 1];\n return entry ? [entry] : [];\n}\n\n/** Builds the numbered clickable map from clickable candidates in document order. */\nexport function buildclickablemap<T extends candidatefields>(candidates: T[], version: number, builtat = 0): clickablemap {\n const entries: mapentry[] = candidates.map((candidate, position) => ({ number: position + 1, selector: candidate.selector, role: candidate.role || candidate.tag, label: candidate.label, mode: \"selector\" as const }));\n return { version, entries, builtat };\n}\n\n/** Describes the nested frame structure of one live document; cross origin frames stay opaque. */\nexport function describeframes(root: Document): framedescription {\n const frames = [...root.querySelectorAll(\"iframe\")].map(frame => {\n let content: Document | null = null;\n try { content = frame.contentDocument; } catch { content = null; }\n let sameorigin = false;\n try { sameorigin = content !== null && frame.contentWindow?.location.origin === location.origin; } catch { sameorigin = false; }\n return sameorigin && content ? { sameorigin: true, document: describeframes(content) } : { sameorigin: false };\n });\n return { frames, live: root };\n}\n\n/** Walks a reviewed frame path through same origin frames and refuses cross origin or absent hops. */\nexport function walkframepath(root: framedescription, path: number[]): { ok: true; document: framedescription } | { ok: false; reason: string } {\n let current = root;\n for (const index of path) {\n if (!Number.isInteger(index) || index < 0) return { ok: false, reason: \"The reviewed frame path contains an invalid frame index.\" };\n const entry = current.frames[index];\n if (!entry) return { ok: false, reason: `Frame ${index} of the reviewed frame path is absent.` };\n if (!entry.sameorigin || !entry.document) return { ok: false, reason: `Frame ${index} of the reviewed frame path is cross origin and was refused.` };\n current = entry.document;\n }\n return { ok: true, document: current };\n}\n\n/** Describes one document or shadow root scope with every nested open shadow scope. */\nexport function describescopes(root: ParentNode): scopetree {\n const elements = [...root.querySelectorAll(\"*\")];\n const shadows: scopetree[] = [];\n for (const element of elements) {\n const shadow = element.shadowRoot;\n if (shadow) {\n const nested = describescopes(shadow);\n nested.host = summarize(element);\n shadows.push(nested);\n }\n }\n return { candidates: elements.map(summarize), shadows };\n}\n\n/** Searches one scope tree depth first for candidates matching the predicate, piercing open shadow scopes recursively. */\nexport function piercescopes<T extends candidatefields>(scope: scopetree<T>, match: (candidate: T) => boolean): T[] {\n const found: T[] = [];\n for (const candidate of scope.candidates) if (match(candidate)) found.push(candidate);\n for (const shadow of scope.shadows) found.push(...piercescopes(shadow, match));\n return found;\n}\n\n/** Resolves a reviewed selector chain through open shadow roots: each selector resolves inside the previous scope. */\nexport function queryshadowchain(root: ParentNode, selectors: string[]): Element | null {\n let scope: ParentNode = root;\n for (let position = 0; position < selectors.length; position += 1) {\n const found = scope.querySelector(selectors[position] as string);\n if (!found) return null;\n if (position === selectors.length - 1) return found;\n const shadow = found.shadowRoot;\n if (!shadow) return null;\n scope = shadow;\n }\n return null;\n}\n\n/** Finds the first element matching a selector in the scope or any nested open shadow root. */\nexport function queryscoped(root: ParentNode, selector: string): Element | null {\n const direct = root.querySelector(selector);\n if (direct) return direct;\n for (const element of [...root.querySelectorAll(\"*\")]) {\n const shadow = element.shadowRoot;\n if (shadow) {\n const found = queryscoped(shadow, selector);\n if (found) return found;\n }\n }\n return null;\n}\n\nfunction parseoptionssafe(step: toolstep): Record<string, unknown> {\n try { return parseoptions(step); } catch { return {}; }\n}\n\n/** Builds the matched element summary attached to step results for review. */\nexport function targetsummary(mode: targetmode, element: HTMLElement): resolvedtarget {\n const rect = element.getBoundingClientRect();\n return { mode, selector: elementselector(element), tag: element.tagName.toLowerCase(), label: elementlabel(element), geometry: { x: rect.x, y: rect.y, width: rect.width, height: rect.height } };\n}\n\nexport type stepresolution =\n | { status: \"none\" }\n | { status: \"resolved\"; element: HTMLElement; target: resolvedtarget }\n | { status: \"ambiguous\"; mode: targetmode; candidates: string[] }\n | { status: \"absent\"; mode?: targetmode };\n\nfunction singleresolution(mode: targetmode, matches: livecandidate[]): stepresolution {\n const verdict = resolutionverdict(matches.length);\n if (verdict === \"resolved\") {\n const winner = matches[0];\n if (winner && winner.element instanceof HTMLElement) return { status: \"resolved\", element: winner.element, target: targetsummary(mode, winner.element) };\n return { status: \"absent\", mode };\n }\n if (verdict === \"ambiguous\") return { status: \"ambiguous\", mode, candidates: matches.slice(0, 8).map(candidate => candidate.label || candidate.selector) };\n return { status: \"absent\", mode };\n}\n\n/** Resolves one reviewed targetref mode against the live dom in a single pass. */\nfunction resolvetargetref(reference: Record<string, unknown>, root: Document): stepresolution {\n const mode = reference.mode;\n if (mode === \"selector\") {\n const selector = typeof reference.selector === \"string\" ? reference.selector : \"\";\n const element = selector ? root.querySelector(selector) : null;\n return element instanceof HTMLElement ? { status: \"resolved\", element, target: targetsummary(\"selector\", element) } : { status: \"absent\", mode: \"selector\" };\n }\n if (mode === \"point\") {\n const x = Number(reference.x);\n const y = Number(reference.y);\n if (!Number.isFinite(x) || !Number.isFinite(y)) return { status: \"absent\", mode: \"point\" };\n const element = root.elementFromPoint(x, y);\n return element instanceof HTMLElement ? { status: \"resolved\", element, target: targetsummary(\"point\", element) } : { status: \"absent\", mode: \"point\" };\n }\n if (mode === \"xpath\") {\n const expression = typeof reference.xpath === \"string\" ? reference.xpath : \"\";\n if (!expression) return { status: \"absent\", mode: \"xpath\" };\n const matches = evaluatexpath(buildxtree(root), expression);\n const first = matches[0];\n return first?.element instanceof HTMLElement ? { status: \"resolved\", element: first.element, target: targetsummary(\"xpath\", first.element) } : { status: \"absent\", mode: \"xpath\" };\n }\n if (mode === \"index\") {\n const matches = matchindex(collectclickable(root), Number(reference.index));\n return singleresolution(\"index\", matches);\n }\n const candidates = collectcandidates(root);\n if (mode === \"text\") return singleresolution(\"text\", matchtext(candidates, typeof reference.text === \"string\" ? reference.text : \"\"));\n if (mode === \"aria\") return singleresolution(\"aria\", matcharia(candidates, typeof reference.role === \"string\" ? reference.role : \"\", typeof reference.name === \"string\" ? reference.name : \"\"));\n if (mode === \"name\") return singleresolution(\"name\", matchname(candidates, typeof reference.name === \"string\" ? reference.name : \"\"));\n return { status: \"absent\" };\n}\n\n/** Resolves the reviewed target of a step: the options targetref when present, otherwise the css target. */\nexport function resolvestep(step: toolstep, root: Document): stepresolution {\n const reference = parseoptionssafe(step).targetref;\n if (reference && typeof reference === \"object\" && !Array.isArray(reference)) return resolvetargetref(reference as Record<string, unknown>, root);\n if (!step.target?.trim()) return { status: \"none\" };\n const element = root.querySelector(step.target);\n if (element instanceof HTMLElement) return { status: \"resolved\", element, target: targetsummary(\"selector\", element) };\n return { status: \"absent\", mode: \"selector\" };\n}\n", "import type { toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\n\n/**\n * Read-only page observation for reviewed steps.\n * Every correlated rule for attribute, style, geometry, value, content, table, link, image, meta, form, storage reads, bounded-free waits, clickable maps and verify reads lives in this file.\n */\n\nimport type { stepresult } from \"./pageactions.js\";\nimport { buildclickablemap, collectclickable, elementlabel, elementselector } from \"./pageresolve.js\";\nimport { buildxtree, evaluatexpath } from \"./pagexpath.js\";\n\nconst highlightid = \"devthinkactionhighlight\";\n\nfunction clearhighlight(): void {\n document.getElementById(highlightid)?.remove();\n}\n\n/** Outlines one reviewed target without dispatching page events. */\nfunction highlighttarget(target: Element): stepresult {\n clearhighlight();\n const rect = target.getBoundingClientRect();\n const overlay = document.createElement(\"div\");\n overlay.id = highlightid;\n overlay.setAttribute(\"aria-hidden\", \"true\");\n Object.assign(overlay.style, { position: \"fixed\", left: `${Math.max(0, rect.left - 3)}px`, top: `${Math.max(0, rect.top - 3)}px`, width: `${rect.width + 6}px`, height: `${rect.height + 6}px`, border: \"3px solid #2f9e44\", borderRadius: \"6px\", pointerEvents: \"none\", zIndex: \"2147483647\", boxSizing: \"border-box\" });\n document.documentElement.append(overlay);\n window.setTimeout(clearhighlight, 5000);\n return { ok: true, summary: \"Target outlined for five seconds.\" };\n}\n\nfunction poll(root: Document, predicate: () => boolean, description: string, timeout: number): Promise<stepresult> {\n return new Promise(resolve => {\n const started = Date.now();\n const check = (): void => {\n if (predicate()) { resolve({ ok: true, summary: `${description} is now present on the page.` }); return; }\n if (timeout > 0 && Date.now() - started >= timeout) { resolve({ ok: false, summary: `${description} did not appear within ${timeout} milliseconds.` }); return; }\n window.setTimeout(check, 100);\n };\n check();\n });\n}\n\nfunction formstate(root: Document): Array<Record<string, unknown>> {\n return [...root.querySelectorAll(\"input, textarea, select\")].map(element => ({\n type: element.getAttribute(\"type\") ?? element.tagName.toLowerCase(),\n name: element.getAttribute(\"name\") ?? \"\",\n value: element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement ? element.value : \"\",\n ...(element instanceof HTMLInputElement && (element.type === \"checkbox\" || element.type === \"radio\") ? { checked: element.checked } : {}),\n }));\n}\n\n/** Runs one read-only page observation after the background policy gate. */\nexport function runpageread(step: toolstep, target: Element | null, root: Document = document): stepresult | Promise<stepresult> {\n const options = (() => { try { return parseoptions(step); } catch { return {} as Record<string, unknown>; } })();\n switch (step.kind) {\n case \"highlight\": {\n if (!target) return { ok: false, summary: \"Highlight target is no longer available.\" };\n return highlighttarget(target);\n }\n case \"readattribute\": {\n if (!target) return { ok: false, summary: \"Read target is no longer available.\" };\n const value = target.getAttribute(step.value ?? \"\");\n return value === null ? { ok: false, summary: `Attribute ${step.value} is absent.` } : { ok: true, summary: `Attribute ${step.value} read.`, details: { value } };\n }\n case \"readstyle\": {\n if (!target) return { ok: false, summary: \"Read target is no longer available.\" };\n const computed = getComputedStyle(target);\n const styles: Record<string, string> = {};\n for (let index = 0; index < computed.length; index += 1) { const property = computed.item(index); styles[property] = computed.getPropertyValue(property); }\n return { ok: true, summary: `Read ${computed.length} computed style properties.`, details: { styles } };\n }\n case \"readgeometry\": {\n if (!target) return { ok: false, summary: \"Read target is no longer available.\" };\n const rect = target.getBoundingClientRect();\n const geometry = { x: rect.x, y: rect.y, width: rect.width, height: rect.height, top: rect.top, right: rect.right, bottom: rect.bottom, left: rect.left };\n return { ok: true, summary: \"Target geometry read.\", details: { geometry } };\n }\n case \"readvalue\": {\n if (!(target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement)) return { ok: false, summary: \"Target does not hold a form value.\" };\n return { ok: true, summary: \"Form value read.\", details: { value: target.value } };\n }\n case \"readtext\": {\n if (!target) return { ok: false, summary: \"Read target is no longer available.\" };\n const text = target.textContent ?? \"\";\n return { ok: true, summary: `Read ${text.length} characters of text.`, details: { text } };\n }\n case \"readhtml\": {\n if (!target) return { ok: false, summary: \"Read target is no longer available.\" };\n return { ok: true, summary: \"Target markup read.\", details: { html: target.outerHTML } };\n }\n case \"countelements\": {\n const count = root.querySelectorAll(step.target ?? \"\").length;\n return { ok: true, summary: `Selector matches ${count} element${count === 1 ? \"\" : \"s\"}.`, details: { count } };\n }\n case \"readtable\": {\n if (!(target instanceof HTMLTableElement)) return { ok: false, summary: \"Read target is not a table element.\" };\n const rows = [...target.querySelectorAll(\"tr\")].map(row => [...row.querySelectorAll(\"th, td\")].map(cell => cell.textContent?.trim() ?? \"\"));\n const headers = rows[0] ?? [];\n const body = rows.slice(1);\n return { ok: true, summary: `Read table with ${headers.length} column${headers.length === 1 ? \"\" : \"s\"} and ${body.length} row${body.length === 1 ? \"\" : \"s\"}.`, details: { headers, rows: body } };\n }\n case \"readlinks\": {\n const links = [...root.querySelectorAll(\"a[href]\")].map(element => ({ text: element.textContent?.trim() ?? \"\", href: element.getAttribute(\"href\") ?? \"\" }));\n return { ok: true, summary: `Read ${links.length} link${links.length === 1 ? \"\" : \"s\"}.`, details: { links } };\n }\n case \"readimages\": {\n const images = [...root.querySelectorAll(\"img\")].map(element => ({ src: element.getAttribute(\"src\") ?? \"\", alt: element.getAttribute(\"alt\") ?? \"\" }));\n return { ok: true, summary: `Read ${images.length} image${images.length === 1 ? \"\" : \"s\"}.`, details: { images } };\n }\n case \"readmeta\": {\n const meta = [...root.querySelectorAll(\"meta\")].map(element => ({ name: element.getAttribute(\"name\") ?? \"\", property: element.getAttribute(\"property\") ?? \"\", content: element.getAttribute(\"content\") ?? \"\" }));\n return { ok: true, summary: `Read ${meta.length} meta entr${meta.length === 1 ? \"y\" : \"ies\"}.`, details: { meta } };\n }\n case \"readforms\": {\n const forms = formstate(root);\n return { ok: true, summary: `Read ${forms.length} form control${forms.length === 1 ? \"\" : \"s\"}.`, details: { forms } };\n }\n case \"readstorage\": {\n try {\n if (step.value) {\n const value = localStorage.getItem(step.value);\n return { ok: true, summary: `Read local storage entry ${step.value}.`, details: { value } };\n }\n const entries: Record<string, string | null> = {};\n for (let index = 0; index < localStorage.length; index += 1) { const key = localStorage.key(index); if (key !== null) entries[key] = localStorage.getItem(key); }\n return { ok: true, summary: `Read ${Object.keys(entries).length} local storage entr${Object.keys(entries).length === 1 ? \"y\" : \"ies\"}.`, details: { entries } };\n } catch (error) { return { ok: false, summary: `Local storage refused the read: ${error instanceof Error ? error.message : String(error)}` }; }\n }\n case \"waitfor\": {\n const selector = step.target ?? \"\";\n const timeout = typeof options.timeout === \"number\" ? options.timeout : 0;\n return poll(root, () => Boolean(root.querySelector(selector)), `Selector ${selector}`, timeout);\n }\n case \"waittext\": {\n const text = step.value ?? \"\";\n const timeout = typeof options.timeout === \"number\" ? options.timeout : 0;\n return poll(root, () => (root.body?.innerText ?? \"\").includes(text), `Text ${text}`, timeout);\n }\n case \"mapclicks\": {\n const candidates = collectclickable(root);\n const map = buildclickablemap(candidates, 0, 0);\n return { ok: true, summary: `Mapped ${map.entries.length} clickable element${map.entries.length === 1 ? \"\" : \"s\"}.`, details: { entries: map.entries } };\n }\n case \"verifyvisible\": {\n if (!target) return { ok: false, summary: \"Verify target is no longer available.\" };\n const rect = target.getBoundingClientRect();\n const rendered = rect.width > 0 && rect.height > 0;\n return { ok: rendered, summary: rendered ? `Target is rendered at ${Math.round(rect.x)},${Math.round(rect.y)} with size ${Math.round(rect.width)}x${Math.round(rect.height)}.` : \"Target is not rendered.\", details: { visible: rendered, geometry: { x: rect.x, y: rect.y, width: rect.width, height: rect.height } } };\n }\n case \"verifyenabled\": {\n if (!target) return { ok: false, summary: \"Verify target is no longer available.\" };\n const control = target as HTMLInputElement;\n const disabled = control.disabled === true || target.hasAttribute(\"disabled\");\n const readonly = control.readOnly === true || target.hasAttribute(\"readonly\");\n const enabled = !disabled && !readonly;\n return { ok: enabled, summary: enabled ? \"Target is enabled and writable.\" : disabled ? \"Target is disabled.\" : \"Target is readonly.\", details: { enabled, disabled, readonly } };\n }\n case \"resolvexpath\": {\n const reference = options.targetref as Record<string, unknown> | undefined;\n const expression = typeof reference?.xpath === \"string\" ? reference.xpath : \"\";\n if (!expression) return { ok: false, summary: \"The reviewed xpath expression is absent.\" };\n let matches: ReturnType<typeof evaluatexpath> = [];\n try { matches = evaluatexpath(buildxtree(root), expression); } catch (error) { return { ok: false, summary: `The reviewed xpath expression failed: ${error instanceof Error ? error.message : String(error)}` }; }\n const summaries = matches.map(node => ({ tag: node.tag, ...(node.element ? { selector: elementselector(node.element), label: elementlabel(node.element) } : {}) }));\n return { ok: matches.length > 0, summary: matches.length > 0 ? `Resolved ${matches.length} element${matches.length === 1 ? \"\" : \"s\"} for the reviewed xpath.` : \"The reviewed xpath matched no elements.\", details: { mode: \"xpath\", matches: summaries } };\n }\n default: return { ok: false, summary: \"Unsupported page read.\" };\n }\n}\n", "import type { keyholdstate, toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { clean } from \"./pageresolve.js\";\n\n/**\n * Field and control interactions for reviewed steps.\n * Every correlated rule for timed typing, value setting, key holds, search submission, multi selects, radio choices, sliders, dates, colors, details sections and the hold registry lives in this file.\n */\n\n/** Builds the per keystroke schedule of one reviewed typetime step. */\nexport function typetimeschedule(text: string, delay: number): Array<{ key: string; delay: number }> {\n return [...text].map((character, position) => ({ key: character, delay: position === 0 ? 0 : delay }));\n}\n\n/** Appends reviewed text to the current field value. */\nexport function appendvalue(current: string, addition: string): string {\n return current + addition;\n}\n\n/** Event order delivered after one reviewed value change. */\nexport function valueevents(): string[] {\n return [\"input\", \"change\"];\n}\n\n/** Splits reviewed multi select values into present and missing entries against the declared options. */\nexport function multichoices(values: string[], options: Array<{ value: string; label: string }>): { present: string[]; missing: string[] } {\n const present: string[] = [];\n const missing: string[] = [];\n for (const value of values) {\n const option = options.find(candidate => candidate.value === value || candidate.label === value);\n if (option) present.push(option.value);\n else missing.push(value);\n }\n return { present, missing };\n}\n\n/** Picks the reviewed radio input by value or label from one radio group; the index of the match or minus one. */\nexport function radiochoice(inputs: Array<{ value: string; label: string }>, choice: string): number {\n return inputs.findIndex(candidate => candidate.value === choice || candidate.label === choice);\n}\n\n/** Clamps one reviewed slider value to the declared range and step grid. */\nexport function slidervalue(requested: number, min: number, max: number, step: number): number {\n const lower = Math.min(min, max);\n const upper = Math.max(min, max);\n const clamped = Math.min(upper, Math.max(lower, requested));\n if (!Number.isFinite(step) || step <= 0) return clamped;\n return Math.round((clamped - lower) / step) * step + lower;\n}\n\n/** Validates and normalizes one reviewed yyyy-mm-dd date. */\nexport function datevalue(requested: string): string | null {\n if (!/^\\d{4}-\\d{2}-\\d{2}$/.test(requested)) return null;\n const parts = requested.split(\"-\").map(part => Number.parseInt(part, 10));\n const year = parts[0];\n const month = parts[1];\n const day = parts[2];\n if (!year || !month || !day || month < 1 || month > 12 || day < 1 || day > 31) return null;\n return requested;\n}\n\n/** Validates and normalizes one reviewed #rrggbb color. */\nexport function colorvalue(requested: string): string | null {\n if (!/^#[0-9a-fA-F]{6}$/.test(requested)) return null;\n return requested.toLowerCase();\n}\n\n/** Decides whether one reviewed details section still needs opening. */\nexport function expandstate(open: boolean): { open: boolean; changed: boolean } {\n return open ? { open: true, changed: false } : { open: true, changed: true };\n}\n\n/** Records one held key under its hold id; a repeated active hold id is refused. */\nexport function presshold(holds: keyholdstate[], hold: keyholdstate): { holds: keyholdstate[]; ok: boolean } {\n if (holds.some(existing => existing.holdid === hold.holdid && existing.releasedat === undefined)) return { holds, ok: false };\n return { holds: [...holds, hold], ok: true };\n}\n\n/** Releases one held key by hold id, keeping the release timestamp. */\nexport function releasehold(holds: keyholdstate[], holdid: string, releasedat: number): { holds: keyholdstate[]; released?: keyholdstate } {\n let released: keyholdstate | undefined;\n const next = holds.map(hold => {\n if (hold.holdid !== holdid || hold.releasedat !== undefined) return hold;\n released = { ...hold, releasedat };\n return released;\n });\n return { holds: next, ...(released ? { released } : {}) };\n}\n\n/** Returns the keys currently held, optionally filtered to one tab. */\nexport function heldkeys(holds: keyholdstate[], tabid?: number): keyholdstate[] {\n return holds.filter(hold => hold.releasedat === undefined && (tabid === undefined || hold.tabid === undefined || hold.tabid === tabid));\n}\n\nfunction events(target: Element): void {\n target.dispatchEvent(new Event(\"input\", { bubbles: true }));\n target.dispatchEvent(new Event(\"change\", { bubbles: true }));\n}\n\nfunction modifiers(options: Record<string, unknown>): string[] {\n return Array.isArray(options.modifiers) ? options.modifiers.filter((item): item is string => typeof item === \"string\") : [];\n}\n\nfunction keyevent(type: \"keydown\" | \"keyup\", key: string, mods: string[]): KeyboardEvent {\n const code = key.length === 1 ? `Key${key.toUpperCase()}` : key;\n return new KeyboardEvent(type, { key, code, bubbles: true, cancelable: true, composed: true, ctrlKey: mods.includes(\"ctrl\"), shiftKey: mods.includes(\"shift\"), altKey: mods.includes(\"alt\"), metaKey: mods.includes(\"meta\") });\n}\n\nfunction fieldlike(target: Element | null): HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null {\n return target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement ? target : null;\n}\n\nfunction receiver(target: Element | null): HTMLElement {\n return target instanceof HTMLElement ? target : document.activeElement instanceof HTMLElement ? document.activeElement : document.body;\n}\n\nfunction wait(delay: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, delay));\n}\n\n/** Focuses the reviewed target before typing; an explicit reviewed focus option forces or suppresses the focus. */\nfunction focuswhenneeded(target: Element, options: Record<string, unknown>): void {\n if (!(target instanceof HTMLElement)) return;\n if (options.focus === false) return;\n if (options.focus === true || document.activeElement !== target) target.focus();\n}\n\nfunction pollfor(predicate: () => boolean, description: string, timeout: number): Promise<stepresult> {\n return new Promise(resolve => {\n const started = Date.now();\n const check = (): void => {\n if (predicate()) { resolve({ ok: true, summary: `${description} is now present on the page.` }); return; }\n if (timeout > 0 && Date.now() - started >= timeout) { resolve({ ok: false, summary: `${description} did not appear within ${timeout} milliseconds.` }); return; }\n window.setTimeout(check, 100);\n };\n check();\n });\n}\n\n/** Runs one reviewed control interaction after the background policy gate and a fresh target check; frame routed steps receive their frame document as the root. */\nexport function runpagecontrol(step: toolstep, target: Element | null, root: Document = document): stepresult | Promise<stepresult> {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n switch (step.kind) {\n case \"typetime\": {\n const field = fieldlike(target);\n if (!field) return { ok: false, summary: \"Target cannot receive timed text.\" };\n const text = step.value ?? \"\";\n const delay = typeof options.delay === \"number\" && options.delay > 0 ? options.delay : 0;\n focuswhenneeded(field, options);\n const schedule = typetimeschedule(text, delay);\n return (async (): Promise<stepresult> => {\n for (const entry of schedule) {\n await wait(entry.delay);\n field.dispatchEvent(keyevent(\"keydown\", entry.key, []));\n field.dispatchEvent(new KeyboardEvent(\"keypress\", { key: entry.key, bubbles: true, cancelable: true }));\n field.value = `${field.value}${entry.key}`;\n field.dispatchEvent(new Event(\"input\", { bubbles: true }));\n }\n field.dispatchEvent(new Event(\"change\", { bubbles: true }));\n return { ok: true, summary: `Typed ${text.length} character${text.length === 1 ? \"\" : \"s\"} with a per keystroke delay of ${delay} milliseconds.` };\n })();\n }\n case \"appendtext\": {\n const field = fieldlike(target);\n if (!field) return { ok: false, summary: \"Target cannot hold a value.\" };\n focuswhenneeded(field, options);\n field.value = appendvalue(field.value, step.value ?? \"\");\n events(field);\n return { ok: true, summary: \"Reviewed text appended to the current field value.\" };\n }\n case \"setvalue\": {\n const field = fieldlike(target);\n if (!field) return { ok: false, summary: \"Target cannot hold a value.\" };\n focuswhenneeded(field, options);\n field.value = step.value ?? \"\";\n events(field);\n return { ok: true, summary: `Field value set through the dom property with ${valueevents().join(\" and \")} events.` };\n }\n case \"typeedit\": {\n if (!(target instanceof HTMLElement) || !target.isContentEditable) return { ok: false, summary: \"Target is not a content editable region.\" };\n focuswhenneeded(target, options);\n const text = step.value ?? \"\";\n return (async (): Promise<stepresult> => {\n for (const character of [...text]) {\n target.dispatchEvent(new InputEvent(\"beforeinput\", { bubbles: true, cancelable: true, data: character, inputType: \"insertText\" }));\n target.append(document.createTextNode(character));\n target.dispatchEvent(new InputEvent(\"input\", { bubbles: true, data: character, inputType: \"insertText\" }));\n }\n return { ok: true, summary: `Typed ${text.length} character${text.length === 1 ? \"\" : \"s\"} into the content editable region.` };\n })();\n }\n case \"keyhold\": {\n const key = step.value ?? \"\";\n const mods = modifiers(options);\n receiver(target).dispatchEvent(keyevent(\"keydown\", key, mods));\n const holdid = typeof options.holdid === \"string\" && options.holdid ? options.holdid : \"\";\n return { ok: true, summary: `Key ${key} pressed and held${holdid ? ` under hold id ${holdid}` : \"\"}.`, details: { ...(holdid ? { holdid } : {}), modifiers: mods } };\n }\n case \"keyrelease\": {\n const key = step.value ?? \"\";\n const mods = modifiers(options);\n receiver(target).dispatchEvent(keyevent(\"keyup\", key, mods));\n return { ok: true, summary: `Key ${key} released.`, details: { modifiers: mods } };\n }\n case \"submitsearch\": {\n const field = fieldlike(target);\n if (!field) return { ok: false, summary: \"Target is not a search field.\" };\n const results = typeof options.results === \"string\" ? options.results : \"\";\n const timeout = typeof options.timeout === \"number\" ? options.timeout : 0;\n focuswhenneeded(field, options);\n field.dispatchEvent(keyevent(\"keydown\", \"Enter\", []));\n field.dispatchEvent(new KeyboardEvent(\"keypress\", { key: \"Enter\", bubbles: true, cancelable: true }));\n field.dispatchEvent(keyevent(\"keyup\", \"Enter\", []));\n return pollfor(() => Boolean(document.querySelector(results)), `Results region ${results}`, timeout);\n }\n case \"selectmulti\": {\n if (!(target instanceof HTMLSelectElement) || !target.multiple) return { ok: false, summary: \"Target is not a multi select control.\" };\n const choices = [...target.options].map(option => ({ value: option.value, label: clean(option.textContent || option.value) }));\n const requested = Array.isArray(options.values) ? options.values.filter((item): item is string => typeof item === \"string\") : [];\n const outcome = multichoices(requested, choices);\n if (outcome.missing.length > 0) return { ok: false, summary: `Reviewed option${outcome.missing.length === 1 ? \"\" : \"s\"} ${outcome.missing.join(\", \")} ${outcome.missing.length === 1 ? \"is\" : \"are\"} not part of the select control.` };\n for (const option of target.options) option.selected = outcome.present.includes(option.value);\n events(target);\n return { ok: true, summary: `Selected ${outcome.present.length} reviewed option${outcome.present.length === 1 ? \"\" : \"s\"} in the multi select control.`, details: { selected: outcome.present } };\n }\n case \"chooseradio\": {\n const radios = target instanceof HTMLInputElement && target.type === \"radio\"\n ? [...root.querySelectorAll<HTMLInputElement>(`input[type=radio][name=\"${CSS.escape(target.name)}\"]`)]\n : target ? [...(target as ParentNode).querySelectorAll<HTMLInputElement>(\"input[type=radio]\")] : [];\n if (radios.length === 0) return { ok: false, summary: \"No radio group owns the reviewed target.\" };\n const inputs = radios.map(radio => ({ value: radio.value, label: radio.labels && radio.labels.length > 0 ? clean(radio.labels[0]?.textContent || \"\") || radio.value : radio.value }));\n const index = radiochoice(inputs, step.value ?? \"\");\n const chosen = radios[index];\n if (!chosen) return { ok: false, summary: \"The reviewed radio option is not part of the group.\" };\n chosen.checked = true;\n events(chosen);\n return { ok: true, summary: `Picked reviewed radio option ${step.value}.`, details: { value: chosen.value } };\n }\n case \"setslider\": {\n if (!(target instanceof HTMLInputElement) || target.type !== \"range\") return { ok: false, summary: \"Target is not a range slider.\" };\n const requested = Number(step.value);\n if (!Number.isFinite(requested)) return { ok: false, summary: \"The reviewed slider value is not a number.\" };\n focuswhenneeded(target, options);\n const value = slidervalue(requested, Number(target.min), Number(target.max), Number(target.step));\n target.value = String(value);\n events(target);\n return { ok: true, summary: `Slider dragged to the reviewed value ${value}.`, details: { value } };\n }\n case \"setdate\": {\n if (!(target instanceof HTMLInputElement) || target.type !== \"date\") return { ok: false, summary: \"Target is not a date input.\" };\n const value = datevalue(step.value ?? \"\");\n if (value === null) return { ok: false, summary: \"The reviewed date is invalid.\" };\n focuswhenneeded(target, options);\n target.value = value;\n events(target);\n return { ok: true, summary: `Date input set to ${value}.`, details: { value } };\n }\n case \"setcolor\": {\n if (!(target instanceof HTMLInputElement) || target.type !== \"color\") return { ok: false, summary: \"Target is not a color input.\" };\n const value = colorvalue(step.value ?? \"\");\n if (value === null) return { ok: false, summary: \"The reviewed color is invalid.\" };\n focuswhenneeded(target, options);\n target.value = value;\n events(target);\n return { ok: true, summary: `Color input set to ${value}.`, details: { value } };\n }\n case \"expanddetails\": {\n const details = target instanceof HTMLElement ? target.closest(\"details\") : null;\n if (!details) return { ok: false, summary: \"Target is not inside a details section.\" };\n const outcome = expandstate(details.open);\n details.open = outcome.open;\n return { ok: true, summary: outcome.changed ? \"Collapsed details section opened.\" : \"Details section was already open.\", details: { changed: outcome.changed } };\n }\n default: return { ok: false, summary: \"Unsupported control action.\" };\n }\n}\n", "import type { pointpath, speedprofile, toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { resolvestep, type stepresolution } from \"./pageresolve.js\";\n\n/**\n * Pointer path and coordinate click logics for reviewed steps.\n * Every correlated rule for waypoint interpolation, easing, jitter, hop settling, pointer sequences and coordinate clicks lives in this file.\n */\n\n/** One interpolated pointer position with the settle delay before it is dispatched. */\nexport interface pointerhop {\n x: number;\n y: number;\n delay: number;\n}\n\n/** One planned pointer or mouse event of a coordinate click sequence. */\nexport interface plannedevent {\n type: string;\n eventkind: \"pointer\" | \"mouse\";\n x: number;\n y: number;\n shift: boolean;\n}\n\nconst basecadence = 16;\n\nfunction distance(a: { x: number; y: number }, b: { x: number; y: number }): number {\n return Math.hypot(b.x - a.x, b.y - a.y);\n}\n\nfunction ease(easing: \"linear\" | \"easeinout\", progress: number): number {\n if (easing === \"easeinout\") return progress * progress * (3 - 2 * progress);\n return progress;\n}\n\nfunction ispointref(value: unknown): value is { x: number; y: number } {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return false;\n const point = value as Record<string, unknown>;\n return typeof point.x === \"number\" && Number.isFinite(point.x) && typeof point.y === \"number\" && Number.isFinite(point.y);\n}\n\n/** Interpolates one reviewed pointer path into settle-delimited hops, honoring the reviewed easing, peak velocity and jitter window. */\nexport function pathhops(path: pointpath, profile: speedprofile | undefined, random: () => number = Math.random, cadence: number = basecadence): pointerhop[] {\n const easing = profile?.easing === \"easeinout\" ? \"easeinout\" : \"linear\";\n const peak = typeof profile?.peak === \"number\" && profile.peak > 0 ? profile.peak : undefined;\n const jitter = typeof profile?.jitter === \"number\" && profile.jitter > 0 ? profile.jitter : 0;\n const points: Array<{ x: number; y: number }> = [path.start, ...(path.waypoints ?? []), path.end];\n const lengths: number[] = [];\n let total = 0;\n for (let index = 1; index < points.length; index += 1) {\n const length = distance(points[index - 1] as { x: number; y: number }, points[index] as { x: number; y: number });\n lengths.push(length);\n total += length;\n }\n const reviewedduration = typeof path.duration === \"number\" && Number.isFinite(path.duration) && path.duration > 0 ? path.duration : undefined;\n const duration = reviewedduration ?? (peak !== undefined && total > 0 ? (total / peak) * 1000 : 300);\n const hops: pointerhop[] = [];\n let previous = points[0] as { x: number; y: number };\n for (let index = 1; index < points.length; index += 1) {\n const from = points[index - 1] as { x: number; y: number };\n const to = points[index] as { x: number; y: number };\n const length = lengths[index - 1] ?? 0;\n if (total <= 0 || length <= 0) {\n hops.push({ x: to.x, y: to.y, delay: 0 });\n previous = to;\n continue;\n }\n const segmentduration = (duration * length) / total;\n const count = Math.max(1, Math.ceil(segmentduration / Math.max(1, cadence)));\n for (let hop = 1; hop <= count; hop += 1) {\n const progress = hop / count;\n const eased = ease(easing, progress);\n const position = { x: from.x + (to.x - from.x) * eased, y: from.y + (to.y - from.y) * eased };\n const step = distance(previous, position);\n const base = segmentduration / count;\n const capped = peak !== undefined ? Math.max(base, (step / peak) * 1000) : base;\n hops.push({ x: position.x, y: position.y, delay: Math.max(0, capped + (jitter > 0 ? random() * jitter : 0)) });\n previous = position;\n }\n }\n return hops;\n}\n\n/** Builds the pointer event sequence around a reviewed path: pointerover, one pointermove per hop, then pointerout. */\nexport function pointersequence(hopcount: number): string[] {\n return [\"pointerover\", ...Array.from({ length: Math.max(0, hopcount) }, () => \"pointermove\"), \"pointerout\"];\n}\n\n/** Builds the full pointer click sequence for reviewed coordinates and modifiers. */\nexport function clickplan(x: number, y: number, modifiers: string[]): plannedevent[] {\n const shift = modifiers.includes(\"shift\");\n const pointer = (type: string): plannedevent => ({ type, eventkind: \"pointer\", x, y, shift });\n const mouse = (type: string): plannedevent => ({ type, eventkind: \"mouse\", x, y, shift });\n return [pointer(\"pointerover\"), pointer(\"pointermove\"), pointer(\"pointerdown\"), mouse(\"mousedown\"), pointer(\"pointerup\"), mouse(\"mouseup\"), mouse(\"click\")];\n}\n\n/** Dispatches one planned event on an element with the reviewed coordinates and modifier flags. */\nfunction dispatchplanned(element: Element, event: plannedevent): void {\n const init: MouseEventInit & PointerEventInit = { bubbles: true, cancelable: true, composed: true, clientX: event.x, clientY: event.y, shiftKey: event.shift };\n if (event.eventkind === \"pointer\") element.dispatchEvent(new PointerEvent(event.type, init));\n else element.dispatchEvent(new MouseEvent(event.type, init));\n}\n\n/** Dispatches the full pointer click sequence on one element with the reviewed modifiers. */\nexport function dispatchclick(element: HTMLElement, modifiers: string[] = []): void {\n const rect = element.getBoundingClientRect();\n const x = rect.left + rect.width / 2;\n const y = rect.top + rect.height / 2;\n for (const event of clickplan(x, y, modifiers)) dispatchplanned(element, event);\n}\n\n/** Scrolls one target into view before any pointer interaction. */\nexport function ensurevisible(element: HTMLElement): void {\n try { element.scrollIntoView({ block: \"center\", inline: \"nearest\", behavior: \"auto\" }); } catch { /* scroll containers may refuse; the interaction still proceeds */ }\n}\n\nfunction settle(delay: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, delay));\n}\n\n/** Dispatches a pointermove event at one viewport position on the element under the pointer. */\nfunction dispatchmove(x: number, y: number): void {\n const element = document.elementFromPoint(x, y);\n const receiver = element ?? document.documentElement;\n receiver.dispatchEvent(new PointerEvent(\"pointermove\", { bubbles: true, cancelable: true, composed: true, clientX: x, clientY: y }));\n}\n\n/** Travels one reviewed pointpath with dispatched pointermove events, settling each hop before the next one. */\nasync function travel(path: pointpath, profile: speedprofile | undefined): Promise<stepresult> {\n const hops = pathhops(path, profile);\n const startelement = document.elementFromPoint(path.start.x, path.start.y) ?? document.documentElement;\n startelement.dispatchEvent(new PointerEvent(\"pointerover\", { bubbles: true, cancelable: true, composed: true, clientX: path.start.x, clientY: path.start.y }));\n for (const hop of hops) {\n await settle(hop.delay);\n dispatchmove(hop.x, hop.y);\n }\n const endelement = document.elementFromPoint(path.end.x, path.end.y) ?? document.documentElement;\n endelement.dispatchEvent(new PointerEvent(\"pointerout\", { bubbles: true, cancelable: true, composed: true, clientX: path.end.x, clientY: path.end.y }));\n return { ok: true, summary: `Pointer traveled ${hops.length} hop${hops.length === 1 ? \"\" : \"s\"} to the reviewed end point.` };\n}\n\n/** Runs one reviewed pointer step: movepointer paths, clickpoint coordinate clicks and shiftclick modified clicks. */\nexport function runpointerstep(step: toolstep, resolution: stepresolution): stepresult | Promise<stepresult> {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n if (step.kind === \"movepointer\") {\n const path = options.pointpath as Record<string, unknown> | undefined;\n if (!path || !ispointref(path.start) || !ispointref(path.end)) return { ok: false, summary: \"The reviewed pointer path is absent.\" };\n const waypoints = Array.isArray(path.waypoints) && path.waypoints.every(item => ispointref(item)) ? path.waypoints as pointpath[\"waypoints\"] : undefined;\n const fullpath: pointpath = { start: path.start, end: path.end, ...(waypoints ? { waypoints } : {}), ...(typeof path.duration === \"number\" && Number.isFinite(path.duration) ? { duration: path.duration } : {}) };\n return travel(fullpath, options.speedprofile as speedprofile | undefined);\n }\n if (step.kind === \"clickpoint\") {\n const reference = options.targetref as Record<string, unknown> | undefined;\n const x = Number(reference?.x);\n const y = Number(reference?.y);\n if (!Number.isFinite(x) || !Number.isFinite(y)) return { ok: false, summary: \"The reviewed click coordinates are absent.\" };\n const element = document.elementFromPoint(x, y);\n if (!(element instanceof HTMLElement)) return { ok: false, summary: \"No element is rendered at the reviewed coordinates.\" };\n ensurevisible(element);\n for (const event of clickplan(x, y, [])) dispatchplanned(element, event);\n return { ok: true, summary: `Clicked the element at the reviewed coordinates ${x},${y}.` };\n }\n if (step.kind === \"shiftclick\") {\n if (resolution.status === \"ambiguous\") return { ok: false, summary: `The reviewed reference matched ${resolution.candidates.length} elements; choose one candidate.`, details: { mode: resolution.mode, candidates: resolution.candidates } };\n if (resolution.status !== \"resolved\") return { ok: false, summary: \"Action target is no longer available.\" };\n ensurevisible(resolution.element);\n dispatchclick(resolution.element, [\"shift\"]);\n return { ok: true, summary: `Shift click delivered to ${resolution.target.label || resolution.target.tag}.`, details: { mode: resolution.target.mode, resolvedtarget: resolution.target } };\n }\n return { ok: false, summary: \"Unsupported pointer action.\" };\n}\n", "import type { retryrule, toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { describeframes, queryscoped, queryshadowchain, resolvestep, targetsummary, walkframepath } from \"./pageresolve.js\";\nimport { dispatchclick, ensurevisible } from \"./pagepointer.js\";\n\n/**\n * Resolution driven interactions for reviewed steps.\n * Every correlated rule for text, aria and name clicks, shadow piercing, frame routing, wrapper step extraction and the retry loop lives in this file.\n */\n\n/** Reads the reviewed options of a step without throwing on malformed payloads. */\nfunction optionsof(step: toolstep): Record<string, unknown> {\n try { return parseoptions(step); } catch { return {}; }\n}\n\n/** Extracts the reviewed inner step of a retry or frame wrapper from its inline options. */\nexport function innerstep(step: toolstep): toolstep | null {\n const options = optionsof(step);\n const kind = options.kind;\n if (typeof kind !== \"string\" || !kind.trim()) return null;\n const inneroptions = options.options;\n return {\n id: `${step.id}inner`,\n kind: kind as toolstep[\"kind\"],\n summary: step.summary,\n risk: step.risk,\n ...(typeof options.target === \"string\" ? { target: options.target } : {}),\n ...(typeof options.value === \"string\" ? { value: options.value } : {}),\n ...(inneroptions && typeof inneroptions === \"object\" && !Array.isArray(inneroptions) ? { options: JSON.stringify(inneroptions) } : {}),\n };\n}\n\nfunction pointdistance(a: { x: number; y: number }, b: { x: number; y: number }): number {\n return Math.hypot(b.x - a.x, b.y - a.y);\n}\n\nfunction settle(delay: number): Promise<void> {\n return new Promise(resolve => setTimeout(resolve, delay));\n}\n\nexport interface retryoutcomeplan {\n ok: boolean;\n attempts: number;\n movement: number;\n summary: string;\n}\n\n/** Runs the reviewed retry loop: each failed attempt waits the settle window, revalidates the target geometry and reruns only when the element moved beyond the tolerance. */\nexport async function runretries(rule: retryrule, probe: () => Promise<{ x: number; y: number } | null>, execute: (attempt: number) => Promise<{ ok: boolean; summary: string }>): Promise<retryoutcomeplan> {\n const attempts = Math.max(1, Number.isFinite(rule.attempts) ? Math.floor(rule.attempts) : 1);\n const tolerance = typeof rule.tolerance === \"number\" && Number.isFinite(rule.tolerance) ? rule.tolerance : 0;\n const settles = typeof rule.settle === \"number\" && Number.isFinite(rule.settle) ? rule.settle : 0;\n let previous = await probe();\n let movement = 0;\n let made = 0;\n let last = \"\";\n for (let attempt = 1; attempt <= attempts; attempt += 1) {\n made = attempt;\n const result = await execute(attempt);\n last = result.summary;\n if (result.ok) return { ok: true, attempts: made, movement, summary: `Retry interaction succeeded on attempt ${made} after ${movement.toFixed(1)} pixels of observed movement.` };\n if (attempt >= attempts) break;\n if (settles > 0) await settle(settles);\n const current = await probe();\n if (!current) { previous = null; continue; }\n if (previous) {\n const delta = pointdistance(previous, current);\n movement = Math.max(movement, delta);\n if (delta <= tolerance) return { ok: false, attempts: made, movement, summary: `The target stayed within the reviewed tolerance of ${tolerance} pixels; retry stopped after attempt ${made}. ${last}` };\n }\n previous = current;\n }\n return { ok: false, attempts: made, movement, summary: `Retry interaction failed after ${made} attempt${made === 1 ? \"\" : \"s\"} with ${movement.toFixed(1)} pixels of observed movement. ${last}` };\n}\n\n/** Dispatches one reviewed click on a resolved element and reports the target mode used. */\nfunction clickresolved(stepkind: string, resolution: ReturnType<typeof resolvestep>): stepresult {\n if (resolution.status === \"ambiguous\") return { ok: false, summary: `The reviewed ${resolution.mode} reference matched ${resolution.candidates.length} elements: ${resolution.candidates.join(\"; \")}.`, details: { mode: resolution.mode, candidates: resolution.candidates } };\n if (resolution.status !== \"resolved\") return { ok: false, summary: \"The reviewed target is no longer available.\" };\n ensurevisible(resolution.element);\n dispatchclick(resolution.element);\n return { ok: true, summary: `Clicked ${resolution.target.label || resolution.target.tag} resolved by ${stepkind} ${resolution.target.mode} mode.`, details: { mode: resolution.target.mode, resolvedtarget: resolution.target } };\n}\n\n/** Runs one resolution driven interaction: text, aria and name clicks, shadow piercing and frame routing. */\nexport function runinteractstep(step: toolstep, expectedorigin: string, dispatch: (inner: toolstep, origin: string, root?: Document) => stepresult | Promise<stepresult>): stepresult | Promise<stepresult> {\n if (step.kind === \"clicktext\" || step.kind === \"clickaria\" || step.kind === \"clickname\") {\n return clickresolved(step.kind, resolvestep(step, document));\n }\n if (step.kind === \"pierceshadow\") {\n const options = optionsof(step);\n const shadow = Array.isArray(options.shadow) ? options.shadow.filter((item): item is string => typeof item === \"string\" && item.trim().length > 0) : [];\n const element = shadow.length > 0 ? queryshadowchain(document, shadow) : queryscoped(document, step.target ?? \"\");\n if (!(element instanceof HTMLElement)) return { ok: false, summary: \"The reviewed shadow target is not available.\" };\n ensurevisible(element);\n dispatchclick(element);\n const summary = targetsummary(\"selector\", element);\n return { ok: true, summary: `Clicked ${summary.label || summary.tag} resolved through ${shadow.length > 0 ? \"the reviewed shadow path\" : \"open shadow roots\"}.`, details: { mode: \"selector\", resolvedtarget: summary } };\n }\n if (step.kind === \"enterframe\") {\n const options = optionsof(step);\n const path = Array.isArray(options.framepath) ? options.framepath.filter((item): item is number => typeof item === \"number\" && Number.isInteger(item) && item >= 0) : [];\n const walk = walkframepath(describeframes(document), path);\n if (!walk.ok) return { ok: false, summary: walk.reason };\n const framedocument = walk.document.live;\n if (!framedocument) return { ok: false, summary: \"The reviewed frame document is not available.\" };\n const inner = innerstep(step);\n if (!inner) return { ok: false, summary: \"The reviewed inner step is absent.\" };\n return dispatch(inner, expectedorigin, framedocument);\n }\n return { ok: false, summary: \"Unsupported interaction action.\" };\n}\n", "import type { navtarget, toolstep, urlpattern, waitoverride, waitprofile } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\n\n/**\n * Page navigation logics for reviewed steps.\n * Every correlated rule for navigation targets, container resolution, wait profiles, url patterns, link following, spa routing, query rewriting, deep links and recent tabs lives in this file.\n */\n\n/** One link shape matched by followlink and spanav, serializable for fixtures. */\nexport interface linkshape {\n text: string;\n href: string;\n selector: string;\n}\n\n/** One collected load signal sample with the signals that held true at its time. */\nexport interface signalsample {\n at: number;\n signals: string[];\n}\n\n/** Resolved container plan of one navigation target across tabs, windows and private profiles. */\nexport interface containerplan {\n kind: \"current\" | \"tab\" | \"window\" | \"private\";\n incognito: boolean;\n windowid?: number;\n position: \"adjacent\" | \"end\";\n}\n\n/** Reads the reviewed navtarget of a navigation step; null when the step reviews none. */\nexport function parsenavtarget(step: toolstep): navtarget | null {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const value = options.navtarget;\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return null;\n const target = value as Record<string, unknown>;\n if (typeof target.url !== \"string\" || !target.url) return null;\n const container = target.container === \"current\" || target.container === \"window\" || target.container === \"private\" ? target.container : \"tab\";\n return {\n url: target.url,\n container,\n ...(target.position === \"end\" ? { position: \"end\" } : { position: \"adjacent\" }),\n private: container === \"private\" || target.private === true,\n };\n}\n\n/** Resolves the container plan of one navigation target: the current task tab, a new tab, a new window or a private window profile separated from normal windows. */\nexport function resolvecontainer(target: navtarget, windows: Array<{ id: number; incognito: boolean; focused: boolean }>): containerplan {\n if (target.container === \"current\") return { kind: \"current\", incognito: false, position: target.position ?? \"adjacent\" };\n if (target.container === \"private\" || target.private) return { kind: \"private\", incognito: true, position: target.position ?? \"adjacent\" };\n if (target.container === \"window\") {\n const focused = windows.find(item => item.focused);\n return { kind: \"window\", incognito: false, ...(focused ? { windowid: focused.id } : {}), position: target.position ?? \"adjacent\" };\n }\n const normal = windows.find(item => !item.incognito && item.focused) ?? windows.find(item => !item.incognito);\n return { kind: \"tab\", incognito: false, ...(normal ? { windowid: normal.id } : {}), position: target.position ?? \"adjacent\" };\n}\n\n/** Reads the reviewed waitprofile of a navprofile step; null when the step reviews none. */\nexport function parsewaitprofile(step: toolstep): waitprofile | null {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const value = options.waitprofile;\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return null;\n const profile = value as Record<string, unknown>;\n const signals = Array.isArray(profile.signals) ? profile.signals.filter((item): item is string => typeof item === \"string\" && item.trim().length > 0) : [];\n if (signals.length === 0) return null;\n const overrides: waitoverride[] = [];\n if (Array.isArray(profile.overrides)) {\n for (const item of profile.overrides) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) continue;\n const override = item as Record<string, unknown>;\n if (typeof override.origin !== \"string\" || !override.origin) continue;\n const overridesignals = Array.isArray(override.signals) ? override.signals.filter((entry): entry is string => typeof entry === \"string\" && entry.trim().length > 0) : undefined;\n overrides.push({\n origin: override.origin,\n ...(overridesignals && overridesignals.length > 0 ? { signals: overridesignals } : {}),\n ...(typeof override.idle === \"number\" && Number.isFinite(override.idle) && override.idle >= 0 ? { idle: override.idle } : {}),\n ...(typeof override.timeout === \"number\" && Number.isFinite(override.timeout) && override.timeout >= 0 ? { timeout: override.timeout } : {}),\n });\n }\n }\n return {\n signals,\n ...(typeof profile.idle === \"number\" && Number.isFinite(profile.idle) && profile.idle >= 0 ? { idle: profile.idle } : {}),\n ...(typeof profile.timeout === \"number\" && Number.isFinite(profile.timeout) && profile.timeout >= 0 ? { timeout: profile.timeout } : {}),\n ...(overrides.length > 0 ? { overrides } : {}),\n };\n}\n\n/** Resolves the effective signals and thresholds of one wait profile for an origin by folding the per origin overrides in. */\nexport function profilefororigin(profile: waitprofile, origin: string): { signals: string[]; idle: number; timeout: number } {\n let signals = [...profile.signals];\n let idle = profile.idle ?? 0;\n let timeout = profile.timeout ?? 0;\n for (const override of profile.overrides ?? []) {\n if (!override.origin || new URL(override.origin).origin !== origin) continue;\n if (override.signals && override.signals.length > 0) signals = [...override.signals];\n if (override.idle !== undefined) idle = override.idle;\n if (override.timeout !== undefined) timeout = override.timeout;\n }\n return { signals, idle, timeout };\n}\n\n/** Maps a document ready state onto the live navigation load phase. */\nexport function loadphase(readystate: string): \"loading\" | \"interactive\" | \"complete\" {\n if (readystate === \"interactive\") return \"interactive\";\n if (readystate === \"complete\") return \"complete\";\n return \"loading\";\n}\n\n/** Decides the wait outcome of a wait profile from collected load signal samples: every required signal must hold in the latest sample inside the timeout. */\nexport function evaluatesignals(required: string[], samples: signalsample[], timeout: number): { ok: boolean; satisfied: string[]; waited: number; samples: number } {\n const start = samples[0]?.at ?? 0;\n const last = samples[samples.length - 1];\n const waited = Math.max(0, (last?.at ?? 0) - start);\n const held = last?.signals ?? [];\n const satisfied = required.filter(signal => held.includes(signal));\n if (required.length > 0 && satisfied.length === required.length) return { ok: true, satisfied, waited, samples: samples.length };\n if (timeout > 0 && waited >= timeout) return { ok: false, satisfied, waited, samples: samples.length };\n return { ok: false, satisfied, waited, samples: samples.length };\n}\n\n/** Reads the reviewed urlpattern of a waiturl, spanav or spawait step; null when the step reviews none. */\nexport function parseurlpattern(step: toolstep, key: string = \"urlpattern\"): urlpattern | null {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const value = options[key];\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return null;\n const pattern = value as Record<string, unknown>;\n if (typeof pattern.url !== \"string\" || !pattern.url) return null;\n const mode = pattern.mode === \"exact\" || pattern.mode === \"host\" || pattern.mode === \"pattern\" ? pattern.mode : \"prefix\";\n const query: Record<string, string> = {};\n if (pattern.query && typeof pattern.query === \"object\" && !Array.isArray(pattern.query)) {\n for (const [name, item] of Object.entries(pattern.query as Record<string, unknown>)) if (typeof item === \"string\") query[name] = item;\n }\n return {\n mode,\n url: pattern.url,\n ...(Object.keys(query).length > 0 ? { query } : {}),\n ...(typeof pattern.fragment === \"string\" && pattern.fragment ? { fragment: pattern.fragment } : {}),\n };\n}\n\n/** Matches a wildcard path segment against one url path segment. */\nfunction segmentmatches(pattern: string, actual: string): boolean {\n if (pattern === \"*\" || pattern === \"**\") return true;\n if (!pattern.includes(\"*\")) return pattern === actual;\n const parts = pattern.split(\"*\");\n let index = 0;\n for (let position = 0; position < parts.length; position += 1) {\n const part = parts[position] as string;\n if (part === \"\") continue;\n const found = actual.indexOf(part, index);\n if (found < 0) return false;\n if (position === 0 && found !== 0) return false;\n index = found + part.length;\n }\n const last = parts[parts.length - 1] as string;\n return last === \"\" || actual.endsWith(last);\n}\n\n/** Matches one url against a reviewed urlpattern by mode plus required query and fragment parts. */\nexport function urlmatches(url: string, pattern: urlpattern): boolean {\n let parsed: URL;\n try { parsed = new URL(url); } catch { return false; }\n let expected: URL;\n try { expected = new URL(pattern.url); } catch { return false; }\n if (pattern.mode === \"exact\" && parsed.toString() !== expected.toString()) return false;\n if (pattern.mode === \"prefix\" && !parsed.toString().startsWith(pattern.url)) return false;\n if (pattern.mode === \"host\" && parsed.origin !== expected.origin) return false;\n if (pattern.mode === \"pattern\") {\n if (parsed.origin !== expected.origin) return false;\n const expectedsegments = expected.pathname.split(\"/\").filter(segment => segment !== \"\");\n const actualsegments = parsed.pathname.split(\"/\").filter(segment => segment !== \"\");\n if (expectedsegments.includes(\"**\")) {\n const cut = expectedsegments.indexOf(\"**\");\n const head = expectedsegments.slice(0, cut);\n const tail = expectedsegments.slice(cut + 1);\n if (actualsegments.length < head.length + tail.length) return false;\n if (!head.every((segment, position) => segmentmatches(segment, actualsegments[position] ?? \"\"))) return false;\n if (!tail.every((segment, position) => segmentmatches(segment, actualsegments[actualsegments.length - tail.length + position] ?? \"\"))) return false;\n } else if (expectedsegments.length !== actualsegments.length || !expectedsegments.every((segment, position) => segmentmatches(segment, actualsegments[position] ?? \"\"))) return false;\n }\n const values = parsed.searchParams;\n for (const [name, value] of Object.entries(pattern.query ?? {})) {\n if (!values.has(name)) return false;\n if (value !== \"*\" && values.get(name) !== value) return false;\n }\n if (pattern.fragment !== undefined && parsed.hash.slice(1) !== pattern.fragment) return false;\n return true;\n}\n\n/** Reads the current query parameters of a url. */\nfunction readquery(url: string): Record<string, string> {\n const values: Record<string, string> = {};\n try {\n for (const [name, value] of new URL(url).searchParams.entries()) values[name] = value;\n } catch { /* an unparseable url has no readable parameters */ }\n return values;\n}\n\n/** Result of one query rewrite with the parameters read, written and the new url. */\nexport interface queryrewriteoutcome {\n url: string;\n before: Record<string, string>;\n after: Record<string, string>;\n set: string[];\n removed: string[];\n}\n\n/** Rewrites the query parameters of one url: reviewed values are set, reviewed names removed and the rest preserved. */\nexport function rewritequeryurl(url: string, set: Record<string, string>, remove: string[]): queryrewriteoutcome {\n const parsed = new URL(url);\n const before = readquery(url);\n for (const name of remove) parsed.searchParams.delete(name);\n for (const [name, value] of Object.entries(set)) parsed.searchParams.set(name, value);\n parsed.hash = \"\";\n return { url: parsed.toString(), before, after: readquery(parsed.toString()), set: Object.keys(set), removed: [...remove] };\n}\n\n/** Applies one reviewed fragment to a url, keeping every other part untouched. */\nexport function fragmenturl(url: string, fragment: string): string {\n const parsed = new URL(url);\n parsed.hash = fragment.replace(/^#/, \"\");\n return parsed.toString();\n}\n\n/** Matches links by their visible text, case insensitively, refusing ambiguity through multiple matches. */\nexport function matchlinktext(links: linkshape[], text: string): linkshape[] {\n const wanted = text.trim().toLowerCase();\n return links.filter(link => link.text.trim().toLowerCase() === wanted);\n}\n\n/** Matches links by their href fragment when the reviewed option asks for fragment resolution. */\nexport function matchlinkfragment(links: linkshape[], fragment: string): linkshape[] {\n const wanted = fragment.trim().replace(/^#/, \"\");\n return links.filter(link => {\n try { return new URL(link.href, \"https://example.invalid\").hash.replace(/^#/, \"\") === wanted; } catch { return false; }\n });\n}\n\n/** Builds a deep link into a common web app from a reviewed app pattern and its string params; unknown apps are refused. */\nexport function deeplinkurl(app: string, params: Record<string, string>): string | null {\n const value = (name: string): string | undefined => {\n const item = params[name];\n return typeof item === \"string\" && item.trim() ? item.trim() : undefined;\n };\n switch (app.trim().toLowerCase()) {\n case \"github\": {\n const owner = value(\"owner\");\n const repo = value(\"repo\");\n if (!owner || !repo) return null;\n const path = value(\"path\");\n return `https://github.com/${owner}/${repo}${path ? `/${path.replace(/^\\/+/, \"\")}` : \"\"}`;\n }\n case \"youtube\": {\n const id = value(\"id\");\n if (id) return `https://www.youtube.com/watch?v=${encodeURIComponent(id)}`;\n const search = value(\"search\");\n if (search) return `https://www.youtube.com/results?search_query=${encodeURIComponent(search)}`;\n return null;\n }\n case \"maps\": {\n const query = value(\"query\");\n if (!query) return null;\n return `https://www.google.com/maps/search/${encodeURIComponent(query)}`;\n }\n case \"wikipedia\": {\n const title = value(\"title\");\n if (!title) return null;\n const language = value(\"language\") ?? \"en\";\n return `https://${language}.wikipedia.org/wiki/${encodeURIComponent(title.replace(/\\s+/g, \"_\"))}`;\n }\n case \"amazon\": {\n const search = value(\"search\");\n if (!search) return null;\n return `https://www.amazon.com/s?k=${encodeURIComponent(search)}`;\n }\n case \"x\": {\n const user = value(\"user\");\n if (!user) return null;\n return `https://x.com/${user.replace(/^@/, \"\")}`;\n }\n default: return null;\n }\n}\n\n/** True when the current url of a single page app changed without a reload, keeping the origin. */\nexport function spauroutechanged(previousurl: string, currenturl: string): boolean {\n if (previousurl === currenturl) return false;\n try {\n return new URL(previousurl).origin === new URL(currenturl).origin;\n } catch { return false; }\n}\n\n/** Picks the most recently closed tab whose url is no longer open; null when every recent tab is already restored. */\nexport function pickrecenttab(recenttabs: Array<{ url: string; tabid: number; closedat: number }>, openurls: string[]): { url: string; tabid: number; closedat: number } | null {\n return recenttabs.find(tab => !openurls.includes(tab.url)) ?? null;\n}\n\nfunction wait(ms: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, ms));\n}\n\nfunction stepoptions(step: toolstep): Record<string, unknown> {\n try { return parseoptions(step); } catch { return {}; }\n}\n\nfunction collectlinks(root: Document): linkshape[] {\n return [...root.querySelectorAll(\"a[href]\")].map(element => ({\n text: element.textContent?.trim() ?? \"\",\n href: element instanceof HTMLAnchorElement ? element.href : element.getAttribute(\"href\") ?? \"\",\n selector: element.getAttribute(\"href\") ?? \"\",\n }));\n}\n\n/** Resolves the reviewed link of a followlink or spanav step by visible text or href fragment, refusing links outside the allowed origins. */\nfunction resolvelink(step: toolstep, root: Document): { ok: true; element: HTMLAnchorElement } | { ok: false; summary: string } {\n const options = stepoptions(step);\n const allowedorigins = Array.isArray(options.allowedorigins) ? options.allowedorigins.filter((item): item is string => typeof item === \"string\") : [];\n const links = collectlinks(root);\n const matches = options.fragment === true ? matchlinkfragment(links, step.value ?? \"\") : matchlinktext(links, step.value ?? \"\");\n if (matches.length === 0) return { ok: false, summary: `No link matches the reviewed reference \"${step.value ?? \"\"}\".` };\n if (matches.length > 1) return { ok: false, summary: `The reviewed link reference matched ${matches.length} links; review a unique one.` };\n const element = [...root.querySelectorAll(\"a[href]\")].find(candidate => (candidate instanceof HTMLAnchorElement ? candidate.href : candidate.getAttribute(\"href\") ?? \"\") === matches[0]?.href);\n if (!(element instanceof HTMLAnchorElement)) return { ok: false, summary: \"The reviewed link is no longer available.\" };\n if (allowedorigins.length > 0) {\n let origin = \"\";\n try { origin = new URL(element.href).origin; } catch { origin = \"\"; }\n if (!allowedorigins.includes(origin)) return { ok: false, summary: `The reviewed link leaves the session origin grants for ${origin}.` };\n }\n return { ok: true, element };\n}\n\n/** Waits for the page load event and reports the ready state and load phase. */\nasync function runwaitload(step: toolstep): Promise<stepresult> {\n const options = stepoptions(step);\n const timeout = typeof options.timeout === \"number\" && Number.isFinite(options.timeout) && options.timeout > 0 ? options.timeout : 0;\n const started = Date.now();\n for (;;) {\n const phase = loadphase(document.readyState);\n if (phase === \"complete\") return { ok: true, summary: `The page load event fired and the document is complete after ${Date.now() - started} milliseconds.`, details: { phase, readystate: document.readyState, waited: Date.now() - started } };\n if (timeout > 0 && Date.now() - started >= timeout) return { ok: false, summary: `The page did not reach the complete load phase within the reviewed timeout of ${timeout} milliseconds.`, details: { phase, readystate: document.readyState, waited: Date.now() - started } };\n await wait(50);\n }\n}\n\n/** Waits until the current url matches the reviewed urlpattern. */\nasync function runwaiturl(step: toolstep): Promise<stepresult> {\n const options = stepoptions(step);\n const pattern = parseurlpattern(step);\n if (!pattern) return { ok: false, summary: \"A reviewed urlpattern is required.\" };\n const timeout = typeof options.timeout === \"number\" && Number.isFinite(options.timeout) && options.timeout > 0 ? options.timeout : 0;\n const poll = typeof options.poll === \"number\" && Number.isFinite(options.poll) && options.poll > 0 ? options.poll : 100;\n const started = Date.now();\n for (;;) {\n if (urlmatches(location.href, pattern)) return { ok: true, summary: `The url matched the reviewed ${pattern.mode} pattern after ${Date.now() - started} milliseconds.`, details: { url: location.href, mode: pattern.mode, waited: Date.now() - started } };\n if (timeout > 0 && Date.now() - started >= timeout) return { ok: false, summary: `The url did not match the reviewed ${pattern.mode} pattern within the reviewed timeout of ${timeout} milliseconds.`, details: { url: location.href, mode: pattern.mode, waited: Date.now() - started } };\n await wait(poll);\n }\n}\n\n/** Follows one reviewed link, reporting the href travelled to. */\nasync function runfollowlink(step: toolstep, root: Document): Promise<stepresult> {\n const resolution = resolvelink(step, root);\n if (!resolution.ok) return { ok: false, summary: resolution.summary };\n const href = resolution.element.href;\n resolution.element.click();\n return { ok: true, summary: `Followed the reviewed link to ${href}.`, details: { href } };\n}\n\n/** Navigates a single page app by clicking the reviewed control and waiting for the route change without a reload. */\nasync function runspanav(step: toolstep, root: Document): Promise<stepresult> {\n const options = stepoptions(step);\n const resolution = resolvelink(step, root);\n if (!resolution.ok) return { ok: false, summary: resolution.summary };\n const timeout = typeof options.timeout === \"number\" && Number.isFinite(options.timeout) && options.timeout > 0 ? options.timeout : 0;\n const pattern = parseurlpattern(step, \"routepattern\");\n const before = location.href;\n resolution.element.click();\n const started = Date.now();\n for (;;) {\n const changed = spauroutechanged(before, location.href);\n const matched = pattern ? urlmatches(location.href, pattern) : changed;\n if (matched) return { ok: true, summary: `The single page app route changed to ${location.href} without a reload.`, details: { from: before, to: location.href, waited: Date.now() - started } };\n if (timeout > 0 && Date.now() - started >= timeout) return { ok: false, summary: `The single page app route did not change within the reviewed timeout of ${timeout} milliseconds.`, details: { from: before, to: location.href, waited: Date.now() - started } };\n await wait(50);\n }\n}\n\n/** Waits for a url change inside a single page app without a reload, through popstate, hashchange and history polling. */\nasync function runspawait(step: toolstep): Promise<stepresult> {\n const options = stepoptions(step);\n const timeout = typeof options.timeout === \"number\" && Number.isFinite(options.timeout) && options.timeout > 0 ? options.timeout : 0;\n const poll = typeof options.poll === \"number\" && Number.isFinite(options.poll) && options.poll > 0 ? options.poll : 100;\n const pattern = parseurlpattern(step);\n const before = location.href;\n const started = Date.now();\n let detected = false;\n const onroute = (): void => { if (spauroutechanged(before, location.href)) detected = true; };\n window.addEventListener(\"popstate\", onroute);\n window.addEventListener(\"hashchange\", onroute);\n try {\n for (;;) {\n if (pattern ? urlmatches(location.href, pattern) : detected || spauroutechanged(before, location.href)) {\n return { ok: true, summary: `The single page app url changed to ${location.href} without a reload.`, details: { from: before, to: location.href, waited: Date.now() - started } };\n }\n if (timeout > 0 && Date.now() - started >= timeout) return { ok: false, summary: `The single page app url did not change within the reviewed timeout of ${timeout} milliseconds.`, details: { from: before, to: location.href, waited: Date.now() - started } };\n await wait(poll);\n }\n } finally {\n window.removeEventListener(\"popstate\", onroute);\n window.removeEventListener(\"hashchange\", onroute);\n }\n}\n\n/** Reads and rewrites the query parameters of the current url with pushstate, reporting the parameters read and the new url. */\nfunction runrewritequery(step: toolstep): stepresult {\n const options = stepoptions(step);\n const set: Record<string, string> = {};\n if (options.set && typeof options.set === \"object\" && !Array.isArray(options.set)) {\n for (const [name, value] of Object.entries(options.set as Record<string, unknown>)) if (typeof value === \"string\") set[name] = value;\n }\n const remove = Array.isArray(options.remove) ? options.remove.filter((item): item is string => typeof item === \"string\" && item.trim().length > 0) : [];\n const outcome = rewritequeryurl(location.href, set, remove);\n history.pushState(history.state, document.title, outcome.url);\n return { ok: true, summary: `Rewrote ${outcome.set.length + outcome.removed.length} query parameter${outcome.set.length + outcome.removed.length === 1 ? \"\" : \"s\"}; the url is now ${outcome.url}.`, details: { url: outcome.url, before: outcome.before, after: outcome.after, set: outcome.set, removed: outcome.removed } };\n}\n\n/** Sets the reviewed url fragment and scrolls to its anchor with smooth behavior. */\nasync function runsetfragment(step: toolstep): Promise<stepresult> {\n const fragment = (step.value ?? \"\").replace(/^#/, \"\");\n if (!fragment) return { ok: false, summary: \"A reviewed fragment is required.\" };\n const url = fragmenturl(location.href, fragment);\n history.pushState(history.state, document.title, url);\n const anchor = document.getElementById(fragment);\n anchor?.scrollIntoView({ behavior: \"smooth\", block: \"start\" });\n return { ok: true, summary: `Set the url fragment to ${fragment} and scrolled to its anchor.`, details: { url, fragment, anchored: Boolean(anchor) } };\n}\n\n/** Stops a pending navigation by halting the document load. */\nfunction runstopnav(): stepresult {\n window.stop();\n return { ok: true, summary: \"Stopped the pending navigation of the page.\" };\n}\n\n/** Prefetches the reviewed urls by injecting prefetch hints into the document head. */\nfunction runprefetch(step: toolstep): stepresult {\n const options = stepoptions(step);\n const urls = Array.isArray(options.urls) ? options.urls.filter((item): item is string => typeof item === \"string\" && item.trim().length > 0) : [];\n if (urls.length === 0) return { ok: false, summary: \"A reviewed list of prefetch urls is required.\" };\n for (const url of urls) {\n const hint = document.createElement(\"link\");\n hint.rel = \"prefetch\";\n hint.href = url;\n document.head.append(hint);\n }\n return { ok: true, summary: `Queued ${urls.length} prefetch hint${urls.length === 1 ? \"\" : \"s\"}.`, details: { urls } };\n}\n\n/** Preconnects to the reviewed origins by injecting preconnect hints into the document head. */\nfunction runpreconnect(step: toolstep): stepresult {\n const options = stepoptions(step);\n const origins = Array.isArray(options.origins) ? options.origins.filter((item): item is string => typeof item === \"string\" && item.trim().length > 0) : [];\n if (origins.length === 0) return { ok: false, summary: \"A reviewed list of preconnect origins is required.\" };\n for (const origin of origins) {\n const hint = document.createElement(\"link\");\n hint.rel = \"preconnect\";\n hint.href = origin;\n document.head.append(hint);\n }\n return { ok: true, summary: `Opened ${origins.length} preconnect hint${origins.length === 1 ? \"\" : \"s\"}.`, details: { origins } };\n}\n\n/** Prints the page through the browser print pipeline; the artifact routing happens in the background. */\nfunction runprintpdf(): stepresult {\n window.print();\n return { ok: true, summary: \"Sent the page to the browser print pipeline.\" };\n}\n\n/** Runs one reviewed page navigation kind inside the page world. */\nexport function runpagenav(step: toolstep, root: Document = document): stepresult | Promise<stepresult> {\n switch (step.kind) {\n case \"waitload\": return runwaitload(step);\n case \"waiturl\": return runwaiturl(step);\n case \"followlink\": return runfollowlink(step, root);\n case \"spanav\": return runspanav(step, root);\n case \"spawait\": return runspawait(step);\n case \"rewritequery\": return runrewritequery(step);\n case \"setfragment\": return runsetfragment(step);\n case \"stopnav\": return runstopnav();\n case \"prefetch\": return runprefetch(step);\n case \"preconnect\": return runpreconnect(step);\n case \"printpdf\": return runprintpdf();\n default: return { ok: false, summary: \"Unsupported navigation action.\" };\n }\n}\n", "import type { dialogpolicy, toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\n\n/**\n * Dialog policy logics for reviewed steps.\n * Every correlated rule for policy parsing, dialog answers, main world handler installation and observed dialog harvesting lives in this file.\n * The isolated world cannot override window.confirm, window.alert or window.prompt, so the background installs these wrappers through the scripting api in the main world.\n */\n\n/** One dialog observed by the main world handler, recorded on the shared document. */\nexport interface observeddialog {\n dialog: string;\n text: string;\n result: string | boolean | null;\n at: number;\n}\n\n/** Parses the reviewed dialog policy from step options; prompts need a reviewed answer. */\nexport function parsedialogpolicy(step: toolstep): dialogpolicy | null {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { return null; }\n const accept = options.accept;\n const answer = options.answer;\n if (typeof accept !== \"boolean\" && typeof answer !== \"string\") return null;\n return { accept: accept === true, ...(typeof answer === \"string\" && answer.trim() ? { answer } : {}) };\n}\n\n/** Decides how the reviewed policy answers one dialog kind; a prompt without a reviewed answer is dismissed. */\nexport function dialoganswer(policy: dialogpolicy, dialog: \"confirm\" | \"alert\" | \"prompt\"): { accept: boolean; answer?: string } {\n if (dialog === \"prompt\") {\n if (policy.answer === undefined || policy.answer === \"\") return { accept: false };\n return { accept: policy.accept !== false, ...(policy.answer !== undefined ? { answer: policy.answer } : {}) };\n }\n return { accept: policy.accept };\n}\n\n/** Reads and clears the dialog log the main world handler recorded on the shared document. */\nexport function harvestdialoglog(root: Document): observeddialog[] {\n const raw = root.documentElement.dataset.devthinkdialoglog;\n if (!raw) return [];\n delete root.documentElement.dataset.devthinkdialoglog;\n try {\n const parsed: unknown = JSON.parse(raw);\n if (!Array.isArray(parsed)) return [];\n return parsed.filter((item): item is observeddialog => Boolean(item) && typeof item === \"object\" && typeof (item as Record<string, unknown>).dialog === \"string\");\n } catch { return []; }\n}\n\n/** Installs confirm, alert and prompt wrappers in the page main world answering per the reviewed policy; runs through chrome.scripting with world main. */\nexport function installdialoghandler(accept: boolean, answer: string, persistent: boolean): void {\n const world = globalThis as typeof globalThis & { devthinkoriginaldialogs?: { confirm: (text?: string) => boolean; alert: (text?: string) => void; prompt: (text?: string, defaultvalue?: string) => string | null } };\n const originals = world.devthinkoriginaldialogs ?? { confirm: window.confirm.bind(window), alert: window.alert.bind(window), prompt: window.prompt.bind(window) };\n world.devthinkoriginaldialogs = originals;\n const decide = (dialog: string): { accept: boolean; answer: string | null } => {\n if (dialog === \"prompt\") return answer ? { accept: true, answer } : { accept: false, answer: null };\n return { accept, answer: null };\n };\n const record = (dialog: string, text: string, result: string | boolean | null): void => {\n try {\n const root = document.documentElement;\n const log = JSON.parse(root.dataset.devthinkdialoglog ?? \"[]\") as unknown[];\n log.push({ dialog, text, result, at: Date.now() });\n root.dataset.devthinkdialoglog = JSON.stringify(log);\n } catch { /* a locked down page refuses dataset writes; the handler still answers */ }\n };\n window.confirm = (text?: string): boolean => {\n const decision = decide(\"confirm\");\n record(\"confirm\", text ?? \"\", decision.accept);\n if (!persistent) window.confirm = originals.confirm;\n return decision.accept;\n };\n window.alert = (text?: string): void => {\n record(\"alert\", text ?? \"\", true);\n if (!persistent) window.alert = originals.alert;\n };\n window.prompt = (text?: string, defaultvalue?: string): string | null => {\n const decision = decide(\"prompt\");\n const outcome = decision.accept ? (decision.answer ?? defaultvalue ?? \"\") : null;\n record(\"prompt\", text ?? \"\", outcome);\n if (!persistent) window.prompt = originals.prompt;\n return outcome;\n };\n}\n", "import type { a11ynode, readerarticle, toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { clean, describescopes, elementlabel, elementselector, implicitrole, owntext, type candidatefields, type scopetree } from \"./pageresolve.js\";\n\n/**\n * Semantic page observation for reviewed steps.\n * Every correlated rule for the shadow and frame piercing walker, the accessibility tree, visible text, the reader heuristic, the heading outline, the selection read, open graph extraction, language detection and the shadow and frame inventories lives in this file.\n */\n\n/** Serializable page node used by the observation engine; the live glue attaches the element. */\nexport interface pagenode {\n tag: string;\n selector: string;\n id: string;\n classes: string[];\n role: string;\n name: string;\n text: string;\n value: string;\n states: string[];\n hidden: boolean;\n children: pagenode[];\n element?: Element;\n}\n\n/** Deepest same origin frame nesting the walker pierces; deeper frames stay opaque so recursive self embedding cannot loop. */\nconst maxframedepth = 4;\n\nfunction elementstates(element: Element): string[] {\n const states: string[] = [];\n if (element.hasAttribute(\"disabled\") || element.getAttribute(\"aria-disabled\") === \"true\") states.push(\"disabled\");\n if (element instanceof HTMLInputElement && (element.type === \"checkbox\" || element.type === \"radio\") && element.checked) states.push(\"checked\");\n const expanded = element.getAttribute(\"aria-expanded\");\n if (expanded !== null) states.push(`expanded ${expanded}`);\n if (element.getAttribute(\"aria-selected\") === \"true\") states.push(\"selected\");\n if (element.hasAttribute(\"required\") || element.getAttribute(\"aria-required\") === \"true\") states.push(\"required\");\n if (element.hasAttribute(\"readonly\") || element.getAttribute(\"aria-readonly\") === \"true\") states.push(\"readonly\");\n if (element.getAttribute(\"aria-hidden\") === \"true\") states.push(\"hidden\");\n return states;\n}\n\nfunction elementvalue(element: Element): string {\n if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSelectElement) return element.value;\n return \"\";\n}\n\nfunction elementhidden(element: Element): boolean {\n if (element instanceof HTMLInputElement && element.type === \"hidden\") return true;\n if (element.hasAttribute(\"hidden\") || element.getAttribute(\"aria-hidden\") === \"true\") return true;\n try {\n const style = element.ownerDocument?.defaultView?.getComputedStyle(element);\n if (style && (style.display === \"none\" || style.visibility === \"hidden\")) return true;\n } catch { /* detached documents refuse computed styles; the node stays visible */ }\n return false;\n}\n\nfunction framenode(frame: HTMLIFrameElement, depth: number): pagenode {\n let content: Document | null = null;\n try { content = frame.contentDocument; } catch { content = null; }\n let sameorigin = false;\n try { sameorigin = content !== null && frame.contentWindow?.location.origin === location.origin; } catch { sameorigin = false; }\n const node = wrap(frame, depth);\n if (sameorigin && content && depth < maxframedepth) node.children.push(...wrapchildren(content, depth + 1));\n return node;\n}\n\nfunction wrap(element: Element, depth: number): pagenode {\n const shadow = element.shadowRoot;\n const node: pagenode = {\n tag: element.tagName.toLowerCase(),\n selector: elementselector(element),\n id: element.id,\n classes: [...element.classList],\n role: element.getAttribute(\"role\")?.toLowerCase() || implicitrole(element),\n name: elementlabel(element),\n text: owntext(element),\n value: elementvalue(element),\n states: elementstates(element),\n hidden: elementhidden(element),\n children: [],\n element,\n };\n if (shadow) node.children.push(...wrapchildren(shadow, depth));\n if (element instanceof HTMLIFrameElement) return framenode(element, depth);\n node.children.push(...wrapchildren(element, depth));\n return node;\n}\n\nfunction wrapchildren(scope: ParentNode, depth: number): pagenode[] {\n return [...scope.querySelectorAll(\":scope > *\")].map(child => wrap(child, depth));\n}\n\n/** Builds the serializable page node tree, piercing open shadow roots and same origin iframes. */\nexport function buildpagetree(scope: ParentNode): pagenode {\n const root: pagenode = {\n tag: \"#document\",\n selector: \"\",\n id: \"\",\n classes: [],\n role: \"document\",\n name: \"\",\n text: \"\",\n value: \"\",\n states: [],\n hidden: false,\n children: [],\n };\n if (scope instanceof Document) {\n root.children = scope.documentElement ? [wrap(scope.documentElement, 0)] : [];\n } else {\n root.children = [...wrapchildren(scope, 0)];\n }\n return root;\n}\n\nfunction countnodes(node: a11ynode): number {\n return 1 + node.children.reduce((total, child) => total + countnodes(child), 0);\n}\n\n/** Converts one page node tree into the accessibility tree with roles, names, states and child refs; hidden subtrees stay excluded. */\nexport function builda11ytree(node: pagenode): a11ynode {\n const children = node.children.filter(child => !child.hidden).map(builda11ytree);\n return {\n role: node.role || \"generic\",\n name: node.name,\n states: node.states,\n ...(node.value ? { value: node.value } : {}),\n childcount: children.length,\n children,\n };\n}\n\n/** Returns the rendered text of each visible element; hidden nodes and their subtrees stay excluded. */\nexport function visibleentries(node: pagenode): Array<{ selector: string; text: string }> {\n if (node.hidden) return [];\n const entries: Array<{ selector: string; text: string }> = node.text ? [{ selector: node.selector || node.tag, text: node.text }] : [];\n for (const child of node.children) entries.push(...visibleentries(child));\n return entries;\n}\n\n/** Joins the rendered text of every visible node into one stream. */\nexport function visibletext(node: pagenode): string {\n return visibleentries(node).map(entry => entry.text).join(\" \");\n}\n\nfunction nodetextlength(node: pagenode): number {\n return node.text.length + node.children.reduce((total, child) => total + nodetextlength(child), 0);\n}\n\nfunction nodelinktext(node: pagenode): number {\n const own = node.tag === \"a\" ? node.text.length : 0;\n return own + node.children.reduce((total, child) => total + nodelinktext(child), 0);\n}\n\nfunction wordsin(text: string): number {\n return text.split(/\\s+/).filter(Boolean).length;\n}\n\nfunction findbyline(node: pagenode): string {\n const markers = [\"byline\", \"author\"];\n const direct = node.classes.some(item => markers.some(marker => item.toLowerCase().includes(marker))) || markers.some(marker => node.id.toLowerCase().includes(marker));\n if (direct && node.text) return node.text;\n for (const child of node.children) {\n const found = findbyline(child);\n if (found) return found;\n }\n return \"\";\n}\n\nfunction findheading(node: pagenode, tags: string[]): string {\n if (tags.includes(node.tag) && node.text) return node.text;\n for (const child of node.children) {\n const found = findheading(child, tags);\n if (found) return found;\n }\n return \"\";\n}\n\n/** Scores page nodes by text density and splits the densest article region into reader blocks, separating them from page chrome. */\nexport function buildreader(root: pagenode, title: string): readerarticle {\n let best: pagenode | undefined;\n let bestscore = 0;\n const walk = (node: pagenode): void => {\n if (node.tag !== \"#document\") {\n const length = nodetextlength(node);\n const links = nodelinktext(node);\n const score = length * (1 - (length > 0 ? links / length : 0));\n if (score > bestscore) { bestscore = score; best = node; }\n }\n for (const child of node.children) walk(child);\n };\n walk(root);\n const article = best ?? root;\n const blocks = article.children.filter(child => !child.hidden && child.text).map(child => ({ kind: child.tag, text: child.text, words: wordsin(child.text) }));\n const ownblock = article.text ? [{ kind: article.tag, text: article.text, words: wordsin(article.text) }] : [];\n const allblocks = [...ownblock, ...blocks];\n return {\n title: findheading(article, [\"h1\"]) || findheading(root, [\"h1\"]) || title,\n byline: findbyline(article) || findbyline(root),\n blocks: allblocks,\n words: allblocks.reduce((total, block) => total + block.words, 0),\n characters: allblocks.reduce((total, block) => total + block.text.length, 0),\n };\n}\n\n/** Returns the title and headings outline of the page. */\nexport function pageoutline(root: pagenode, title: string): { title: string; headings: Array<{ level: number; text: string }> } {\n const headings: Array<{ level: number; text: string }> = [];\n const walk = (node: pagenode): void => {\n const level = /^h([1-6])$/.exec(node.tag);\n if (level && node.text) headings.push({ level: Number.parseInt(level[1] as string, 10), text: node.text });\n for (const child of node.children) walk(child);\n };\n walk(root);\n return { title: findheading(root, [\"h1\"]) || title, headings };\n}\n\n/** Reads the text of the current user selection through the document selection api. */\nexport function captureselection(root: { getSelection?: () => { toString(): string } | null }): { text: string; length: number } {\n const selection = root.getSelection?.() ?? null;\n const text = selection ? clean(selection.toString()) : \"\";\n return { text, length: text.length };\n}\n\n/** Extracts open graph meta properties and structured data payloads; malformed structured payloads are refused and counted. */\nexport function opengraphfields(meta: Array<{ property: string; name: string; content: string }>, jsonld: string[]): { graph: Record<string, string>; structured: unknown[]; refused: number } {\n const graph: Record<string, string> = {};\n for (const entry of meta) {\n if (entry.property.startsWith(\"og:\") && entry.content) graph[entry.property] = entry.content;\n }\n const structured: unknown[] = [];\n let refused = 0;\n for (const raw of jsonld) {\n try { structured.push(JSON.parse(raw)); } catch { refused += 1; }\n }\n return { graph, structured, refused };\n}\n\nconst stopwords: Record<string, string[]> = {\n en: [\"the\", \"is\", \"at\", \"which\", \"on\", \"and\", \"of\", \"to\", \"in\", \"that\", \"it\", \"with\"],\n pt: [\"de\", \"que\", \"n\u00E3o\", \"uma\", \"para\", \"com\", \"por\", \"mais\", \"como\", \"p\u00E1gina\", \"este\", \"voc\u00EA\"],\n es: [\"que\", \"el\", \"las\", \"los\", \"por\", \"una\", \"para\", \"con\", \"como\", \"p\u00E1gina\", \"m\u00E1s\", \"este\"],\n fr: [\"le\", \"les\", \"des\", \"que\", \"pour\", \"dans\", \"est\", \"sur\", \"avec\", \"page\", \"plus\", \"cette\"],\n de: [\"der\", \"die\", \"und\", \"das\", \"ist\", \"von\", \"mit\", \"f\u00FCr\", \"auf\", \"den\", \"nicht\", \"seite\"],\n it: [\"che\", \"il\", \"la\", \"per\", \"una\", \"del\", \"sono\", \"non\", \"con\", \"pagina\", \"pi\u00F9\", \"questo\"],\n nl: [\"het\", \"een\", \"en\", \"van\", \"is\", \"dat\", \"op\", \"te\", \"voor\", \"met\", \"niet\", \"pagina\"],\n};\n\n/** Detects the language of extracted text from stopword frequency; an undetermined text returns an empty code. */\nexport function detecttextlanguage(text: string): string {\n const words = text.toLowerCase().split(/[^a-z\u00E0-\u00FF]+/).filter(Boolean);\n if (words.length === 0) return \"\";\n let best = \"\";\n let bestscore = 0;\n for (const [language, dictionary] of Object.entries(stopwords)) {\n const score = words.filter(word => dictionary.includes(word)).length;\n if (score > bestscore) { bestscore = score; best = language; }\n }\n return best;\n}\n\n/** Tags extracted text with its detected language code, routing it to the matching language stream. */\nexport function taglanguage(text: string): { text: string; language: string } {\n return { text, language: detecttextlanguage(text) };\n}\n\n/** Resolves the page language from the document lang attribute, the content language meta and the content signals, in that order. */\nexport function documentlanguage(signals: { lang: string; meta: string; text: string }): { language: string; source: string } {\n if (signals.lang.trim()) return { language: signals.lang.trim(), source: \"document\" };\n if (signals.meta.trim()) return { language: signals.meta.trim(), source: \"meta\" };\n return { language: detecttextlanguage(signals.text), source: \"content\" };\n}\n\n/** Lists the host paths of every open shadow root nested inside one scope tree. */\nexport function shadowpaths(scope: scopetree<candidatefields>): string[] {\n const paths: string[] = [];\n const walk = (tree: scopetree<candidatefields>, prefix: string): void => {\n for (const shadow of tree.shadows) {\n if (!shadow.host) continue;\n const path = prefix ? `${prefix} > ${shadow.host.selector}` : shadow.host.selector;\n paths.push(path);\n walk(shadow, path);\n }\n };\n walk(scope, \"\");\n return paths;\n}\n\n/** Enumerates the iframes of one document with their origins and sizes; cross origin frames report no origin. */\nexport function framelist(root: Document): Array<{ index: number; origin: string; sameorigin: boolean; width: number; height: number }> {\n return [...root.querySelectorAll(\"iframe\")].map((frame, index) => {\n let origin = \"\";\n try { origin = frame.contentWindow?.location.origin ?? \"\"; } catch { origin = \"\"; }\n const rect = frame.getBoundingClientRect();\n return { index, origin, sameorigin: origin !== \"\" && origin === location.origin, width: Math.round(rect.width), height: Math.round(rect.height) };\n });\n}\n\nfunction stepoptions(step: toolstep): Record<string, unknown> {\n try { return parseoptions(step); } catch { return {} as Record<string, unknown>; }\n}\n\n/** Runs one passive page observation after the background policy gate; frame routed steps receive their frame document as the root. */\nexport function runpageobservation(step: toolstep, target: Element | null, root: Document = document): stepresult | Promise<stepresult> {\n switch (step.kind) {\n case \"a11ytree\": {\n const tree = builda11ytree(buildpagetree(root));\n const count = countnodes(tree);\n return { ok: true, summary: `Captured the accessibility tree with ${count} node${count === 1 ? \"\" : \"s\"}.`, details: { tree, nodecount: count } };\n }\n case \"readvisible\": {\n const scope: ParentNode = target ?? root;\n const tree = buildpagetree(scope);\n const entries = visibleentries(tree);\n return { ok: true, summary: `Read the rendered text of ${entries.length} visible element${entries.length === 1 ? \"\" : \"s\"}.`, details: { entries, text: visibletext(tree) } };\n }\n case \"readertree\": {\n const article = buildreader(buildpagetree(root), root.title);\n return { ok: true, summary: `Extracted the reader view with ${article.blocks.length} block${article.blocks.length === 1 ? \"\" : \"s\"} and ${article.words} words.`, details: { article } };\n }\n case \"readoutline\": {\n const outline = pageoutline(buildpagetree(root), root.title);\n return { ok: true, summary: `Read the outline with ${outline.headings.length} heading${outline.headings.length === 1 ? \"\" : \"s\"}.`, details: { title: outline.title, headings: outline.headings } };\n }\n case \"readselection\": {\n const selection = captureselection(root);\n return { ok: true, summary: selection.text ? `Read ${selection.length} characters of the current selection.` : \"No text is currently selected.\", details: { text: selection.text, length: selection.length } };\n }\n case \"readopengraph\": {\n const meta = [...root.querySelectorAll(\"meta\")].map(element => ({ property: element.getAttribute(\"property\") ?? \"\", name: element.getAttribute(\"name\") ?? \"\", content: element.getAttribute(\"content\") ?? \"\" }));\n const jsonld = [...root.querySelectorAll('script[type=\"application/ld+json\"]')].map(element => element.textContent ?? \"\");\n const fields = opengraphfields(meta, jsonld);\n return { ok: true, summary: `Read ${Object.keys(fields.graph).length} open graph entr${Object.keys(fields.graph).length === 1 ? \"y\" : \"ies\"} and ${fields.structured.length} structured payload${fields.structured.length === 1 ? \"\" : \"s\"}${fields.refused > 0 ? `; ${fields.refused} malformed payload${fields.refused === 1 ? \" was\" : \"s were\"} refused` : \"\"}.`, details: { graph: fields.graph, structured: fields.structured, refused: fields.refused } };\n }\n case \"readlang\": {\n const metatag = root.querySelector('meta[http-equiv=\"content-language\"]')?.getAttribute(\"content\") ?? \"\";\n const outcome = documentlanguage({ lang: root.documentElement?.getAttribute(\"lang\") ?? \"\", meta: metatag, text: root.body?.innerText ?? \"\" });\n return { ok: true, summary: `Detected page language ${outcome.language || \"unknown\"} from the ${outcome.source} signal.`, details: { language: outcome.language, source: outcome.source } };\n }\n case \"detectlanguage\": {\n const options = stepoptions(step);\n const text = typeof options.text === \"string\" && options.text ? options.text : target?.textContent ?? root.body?.innerText ?? \"\";\n const routed = taglanguage(clean(text));\n return { ok: routed.language !== \"\", summary: routed.language ? `Detected language ${routed.language} for the extracted text.` : \"The extracted text language is undetermined.\", details: { language: routed.language, routed } };\n }\n case \"listshadow\": {\n const paths = shadowpaths(describescopes(root));\n return { ok: true, summary: `Listed ${paths.length} open shadow root${paths.length === 1 ? \"\" : \"s\"}.`, details: { shadows: paths } };\n }\n case \"listframes\": {\n const frames = framelist(root);\n return { ok: true, summary: `Listed ${frames.length} iframe${frames.length === 1 ? \"\" : \"s\"}.`, details: { frames } };\n }\n default: return { ok: false, summary: \"Unsupported page observation.\" };\n }\n}\n", "import type { bannerreport, listpattern, tableshape, toolstep } from \"../types.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { clean, elementselector } from \"./pageresolve.js\";\n\n/**\n * Page shape detection for reviewed steps.\n * Every correlated rule for repeated list detection, table shape normalization, pagination estimates, infinite scroll ranges, virtualization, lazy images, sticky overlays, scroll locks, consent banner shapes, template classification, section fingerprints and scroll positions lives in this file.\n */\n\n/** Serializable sibling sample of one container element used by the list detector. */\nexport interface siblingsample {\n container: string;\n children: Array<{ tag: string; classes: string; text: string; selector: string }>;\n}\n\n/** Finds repeated item lists with a shared item selector from sibling samples. */\nexport function detectlistpatterns(samples: siblingsample[]): listpattern[] {\n const patterns: listpattern[] = [];\n for (const sample of samples) {\n const groups = new Map<string, Array<{ tag: string; classes: string; text: string }>>();\n for (const child of sample.children) {\n const key = `${child.tag}|${child.classes}`;\n const group = groups.get(key) ?? [];\n group.push(child);\n groups.set(key, group);\n }\n for (const [key, group] of groups) {\n if (group.length < 2) continue;\n if (!group.some(item => item.text)) continue;\n const [tag, classes] = key.split(\"|\") as [string, string?];\n const classpart = (classes ?? \"\").split(\" \").filter(Boolean).map(name => `.${name}`).join(\"\");\n patterns.push({ container: sample.container, itemselector: `${tag}${classpart}`, repeat: group.length, samples: group.map(item => item.text).filter(Boolean) });\n }\n }\n return patterns;\n}\n\n/** Normalizes one table row structure into header row, column specs and caption. */\nexport function normalizetable(rows: Array<{ cells: string[]; header: boolean }>, caption: string): { headers: string[]; columns: Array<{ label: string; cells: number }>; rows: number; caption: string } {\n const firstheader = rows.find(row => row.header);\n const headers = firstheader?.cells ?? [];\n const body = firstheader ? rows.filter(row => row !== firstheader) : rows;\n const width = rows.reduce((largest, row) => Math.max(largest, row.cells.length), 0);\n const columns: Array<{ label: string; cells: number }> = [];\n for (let index = 0; index < width; index += 1) {\n const label = headers[index] ?? `column ${index + 1}`;\n const cells = body.filter(row => Boolean((row.cells[index] ?? \"\").trim())).length;\n columns.push({ label, cells });\n }\n return { headers, columns, rows: body.length, caption };\n}\n\n/** Counts pagination entries and estimates the total page count from the numbers they carry. */\nexport function paginationestimate(entries: Array<{ text: string; selector: string; current: boolean }>): { current: number; total: number; links: number; pages: number[] } {\n const pages: number[] = [];\n let current = 0;\n for (const entry of entries) {\n const parsed = /^\\d+$/.exec(entry.text.trim());\n if (parsed) {\n const page = Number.parseInt(parsed[0] as string, 10);\n pages.push(page);\n if (entry.current) current = page;\n }\n }\n const total = Math.max(0, ...pages, current);\n return { current, total, links: entries.length, pages };\n}\n\n/** Serializable scroll range input of one scrollable container. */\nexport interface scrollrangeshape {\n selector: string;\n scrollheight: number;\n clientheight: number;\n triggers: string[];\n}\n\n/** Flags infinite scroll containers from measured scroll ranges and their load more triggers. */\nexport function infinitescrollranges(ranges: scrollrangeshape[]): Array<{ selector: string; scrollrange: number; triggers: string[] }> {\n return ranges\n .filter(range => range.scrollheight > range.clientheight && range.triggers.length > 0)\n .map(range => ({ selector: range.selector, scrollrange: range.scrollheight - range.clientheight, triggers: range.triggers }));\n}\n\n/** Detects virtualized lists whose uniform rendered rows do not fill the measured scroll range. */\nexport function virtualizedcontainers(containers: Array<{ selector: string; scrollheight: number; rows: Array<{ selector: string; height: number; classes: string }> }>): Array<{ selector: string; rendered: number; estimated: number }> {\n const results: Array<{ selector: string; rendered: number; estimated: number }> = [];\n for (const container of containers) {\n const first = container.rows[0];\n if (!first || container.rows.length < 2 || first.height <= 0) continue;\n if (!container.rows.every(row => row.height === first.height)) continue;\n if (container.scrollheight <= container.rows.length * first.height) continue;\n results.push({ selector: container.selector, rendered: container.rows.length, estimated: Math.floor(container.scrollheight / first.height) });\n }\n return results;\n}\n\n/** Serializable image shape used by the lazy detector. */\nexport interface imageshape {\n selector: string;\n src: string;\n datasrc: string;\n loading: string;\n width: number;\n height: number;\n}\n\n/** Detects lazy loaded images from loading attributes and deferred sources, and placeholder states from inline data or empty sources. */\nexport function lazysurvey(images: imageshape[]): { lazy: Array<{ selector: string; reason: string }>; placeholders: Array<{ selector: string; reason: string }> } {\n const lazy: Array<{ selector: string; reason: string }> = [];\n const placeholders: Array<{ selector: string; reason: string }> = [];\n for (const image of images) {\n if (image.loading === \"lazy\") lazy.push({ selector: image.selector, reason: \"loading attribute\" });\n else if (image.datasrc) lazy.push({ selector: image.selector, reason: \"deferred source\" });\n if (!image.src) placeholders.push({ selector: image.selector, reason: \"empty source\" });\n else if (image.src.startsWith(\"data:\")) placeholders.push({ selector: image.selector, reason: \"inline data placeholder\" });\n }\n return { lazy, placeholders };\n}\n\n/** Detects sticky headers and overlays from fixed and sticky geometry measured against the viewport. */\nexport function overlaygeometry(elements: Array<{ selector: string; position: string; top: number; height: number; width: number }>, viewport: { width: number; height: number }): Array<{ selector: string; position: string; coverage: number; hides: boolean }> {\n const area = viewport.width * viewport.height;\n return elements\n .filter(element => (element.position === \"sticky\" || element.position === \"fixed\") && element.top <= 0 && element.height > 0)\n .map(element => {\n const coverage = area > 0 ? (element.height * element.width) / area : 0;\n return { selector: element.selector, position: element.position, coverage: Math.round(coverage * 1000) / 1000, hides: coverage >= overlaythreshold };\n });\n}\n\n/** Detects scroll locks and modal states from body overflow, body position and modal presence signals. */\nexport function scrolllockstate(signals: { bodyoverflow: string; htmloverflow: string; bodyposition: string; modal: boolean; scrollable: boolean }): { locked: boolean; reasons: string[]; scrollable: boolean } {\n const reasons: string[] = [];\n if (signals.bodyoverflow.includes(\"hidden\") || signals.htmloverflow.includes(\"hidden\")) reasons.push(\"overflow hidden\");\n if (signals.bodyposition === \"fixed\") reasons.push(\"fixed body\");\n if (signals.modal) reasons.push(\"modal open\");\n return { locked: reasons.length > 0, reasons, scrollable: signals.scrollable };\n}\n\n/** Serializable consent banner candidate collected by the banner watcher glue. */\nexport interface bannercandidate {\n selector: string;\n id: string;\n classes: string[];\n text: string;\n controls: string[];\n}\n\n/** Consent banner keywords the shape matcher recognizes across locales. */\nexport const consentkeywords = [\"cookie\", \"consent\", \"gdpr\", \"lgpd\", \"privacy\", \"ccpa\"];\n\n/** Matches consent banner shapes against the known keyword vocabulary and reports their controls. */\nexport function bannermatches(candidates: bannercandidate[], at: number): bannerreport[] {\n const reports: bannerreport[] = [];\n for (const candidate of candidates) {\n const haystack = `${candidate.id} ${candidate.classes.join(\" \")} ${candidate.text}`.toLowerCase();\n const keyword = consentkeywords.find(word => haystack.includes(word));\n if (!keyword) continue;\n if (!candidate.text && candidate.controls.length === 0) continue;\n reports.push({ kind: keyword, selector: candidate.selector, text: candidate.text.slice(0, 200), controls: candidate.controls, at });\n }\n return reports;\n}\n\n/** Classifies the page template from its dominant structural signals. */\nexport function classifytemplate(signals: { paragraphs: number; headings: number; lists: number; tables: number; forms: number; inputs: number; password: boolean }): string {\n if (signals.password) return \"login\";\n if (signals.paragraphs >= 3) return \"article\";\n if (signals.tables > 0) return \"table\";\n if (signals.forms > 0 && signals.inputs > 0) return \"form\";\n if (signals.lists > 0) return \"list\";\n return \"generic\";\n}\n\n/** Computes a stable structural fingerprint of one page section from its tag, attributes, child count and text length. */\nexport function sectionfingerprint(section: { tag: string; attributes: Record<string, string>; children: number; textlength: number }): string {\n const canonical = [section.tag, String(section.children), String(section.textlength), ...Object.keys(section.attributes).sort().map(key => `${key}=${section.attributes[key] ?? \"\"}`)].join(\"|\");\n let hash = 5381;\n for (let index = 0; index < canonical.length; index += 1) hash = ((hash << 5) + hash + canonical.charCodeAt(index)) >>> 0;\n return `fp${hash.toString(16)}`;\n}\n\n/** Normalizes the scroll position of the window and its scrollable containers with edge flags. */\nexport function scrollreport(window: { scrollx: number; scrolly: number; scrollheight: number; clientheight: number }, containers: Array<{ selector: string; scrolltop: number; scrollleft: number; scrollheight: number; clientheight: number }>): { window: { x: number; y: number; attop: boolean; atbottom: boolean; height: number }; containers: Array<{ selector: string; scrolltop: number; scrollleft: number; scrollrange: number; atbottom: boolean }> } {\n const range = Math.max(0, window.scrollheight - window.clientheight);\n return {\n window: { x: window.scrollx, y: window.scrolly, attop: window.scrolly <= 0, atbottom: window.scrolly >= range, height: window.scrollheight },\n containers: containers.map(container => {\n const containerrange = Math.max(0, container.scrollheight - container.clientheight);\n return { selector: container.selector, scrolltop: container.scrolltop, scrollleft: container.scrollleft, scrollrange: containerrange, atbottom: container.scrolltop >= containerrange };\n }),\n };\n}\n\nconst loadmorepattern = /(load more|show more|see more|ver mais|carregar mais|load older|afficher plus|mehr anzeigen)/i;\nconst paginationtext = /^(next|prev|previous|last|first|next page|previous page|\u00BB|\u00AB|\u203A|\u2039|\\d+)$/i;\n/** Viewport coverage fraction at which a sticky or fixed overlay is reported as hiding content. */\nconst overlaythreshold = 0.25;\n\nfunction signatureof(element: Element): string {\n return `${element.tagName.toLowerCase()}|${[...element.classList].sort().join(\" \")}`;\n}\n\n/** Collects the sibling samples of one document for the list detector. */\nexport function collectsiblings(root: Document): siblingsample[] {\n const samples: siblingsample[] = [];\n for (const element of [...root.querySelectorAll(\"*\")]) {\n const children = [...element.children];\n if (children.length < 2) continue;\n const counts = new Map<string, number>();\n for (const child of children) {\n const key = signatureof(child);\n counts.set(key, (counts.get(key) ?? 0) + 1);\n }\n if (![...counts.values()].some(count => count >= 2)) continue;\n samples.push({\n container: elementselector(element),\n children: children.map(child => ({ tag: child.tagName.toLowerCase(), classes: [...child.classList].sort().join(\" \"), text: clean(child.textContent ?? \"\"), selector: elementselector(child) })),\n });\n }\n return samples;\n}\n\n/** Collects the data table structures of one document for the table shape normalizer. */\nexport function collecttables(root: Document): Array<{ selector: string; rows: Array<{ cells: string[]; header: boolean }>; caption: string }> {\n return [...root.querySelectorAll(\"table\")].map(table => ({\n selector: elementselector(table),\n rows: [...table.querySelectorAll(\"tr\")].map(row => ({ cells: [...row.querySelectorAll(\"th, td\")].map(cell => clean(cell.textContent ?? \"\")), header: Boolean(row.querySelector(\"th\")) })),\n caption: clean(table.querySelector(\"caption\")?.textContent ?? \"\"),\n }));\n}\n\nfunction collectpagination(root: Document): Array<{ text: string; selector: string; current: boolean }> {\n const entries: Array<{ text: string; selector: string; current: boolean }> = [];\n for (const element of [...root.querySelectorAll(\"a[href], button, [role=button], [role=link], li, span\")]) {\n const text = clean(element.textContent ?? \"\");\n if (!text || !paginationtext.test(text)) continue;\n if (!element.closest(\"nav, footer, [class*=pag i], [id*=pag i]\")) continue;\n const current = element.getAttribute(\"aria-current\") === \"page\" || [...element.classList].some(name => /current|active|selecionado/i.test(name));\n entries.push({ text, selector: elementselector(element), current });\n }\n return entries;\n}\n\nfunction collecttriggers(scope: ParentNode): string[] {\n const triggers: string[] = [];\n for (const element of [...scope.querySelectorAll(\"button, a[href], [role=button], [class*=loading i], [class*=sentinel i], [class*=spinner i]\")]) {\n const label = clean(element.getAttribute(\"aria-label\") ?? element.textContent ?? \"\");\n if (loadmorepattern.test(label)) triggers.push(elementselector(element));\n }\n return triggers;\n}\n\nfunction collectscrollranges(root: Document): scrollrangeshape[] {\n const ranges: scrollrangeshape[] = [];\n const scrolling = root.scrollingElement ?? root.documentElement;\n const viewheight = root.defaultView?.innerHeight ?? 0;\n if (scrolling && scrolling.scrollHeight > viewheight) ranges.push({ selector: \"window\", scrollheight: scrolling.scrollHeight, clientheight: viewheight, triggers: collecttriggers(root) });\n for (const element of [...root.querySelectorAll(\"*\")]) {\n if (!(element instanceof HTMLElement)) continue;\n if (element.scrollHeight <= element.clientHeight) continue;\n ranges.push({ selector: elementselector(element), scrollheight: element.scrollHeight, clientheight: element.clientHeight, triggers: collecttriggers(element) });\n }\n return ranges;\n}\n\nfunction collectvirtual(root: Document): Array<{ selector: string; scrollheight: number; rows: Array<{ selector: string; height: number; classes: string }> }> {\n const containers: Array<{ selector: string; scrollheight: number; rows: Array<{ selector: string; height: number; classes: string }> }> = [];\n for (const element of [...root.querySelectorAll(\"*\")]) {\n const children = [...element.children];\n const first = children[0];\n if (!first || children.length < 2) continue;\n if (!children.every(child => signatureof(child) === signatureof(first))) continue;\n const heights = children.map(child => child.getBoundingClientRect().height);\n if (!heights.every(height => height > 0 && height === heights[0])) continue;\n containers.push({ selector: elementselector(element), scrollheight: element.scrollHeight, rows: children.map(child => ({ selector: elementselector(child), height: child.getBoundingClientRect().height, classes: [...child.classList].join(\" \") })) });\n }\n return containers;\n}\n\nfunction collectimages(root: Document): imageshape[] {\n return [...root.querySelectorAll(\"img\")].map(image => ({\n selector: elementselector(image),\n src: image.getAttribute(\"src\") ?? \"\",\n datasrc: image.getAttribute(\"data-src\") ?? image.getAttribute(\"data-original\") ?? \"\",\n loading: image.getAttribute(\"loading\") ?? \"\",\n width: image.naturalWidth,\n height: image.naturalHeight,\n }));\n}\n\nfunction collectoverlays(root: Document): Array<{ selector: string; position: string; top: number; height: number; width: number }> {\n const elements: Array<{ selector: string; position: string; top: number; height: number; width: number }> = [];\n for (const element of [...root.querySelectorAll(\"*\")]) {\n if (!(element instanceof HTMLElement)) continue;\n const view = element.ownerDocument.defaultView;\n const position = view ? view.getComputedStyle(element).position : \"\";\n if (position !== \"sticky\" && position !== \"fixed\") continue;\n const rect = element.getBoundingClientRect();\n elements.push({ selector: elementselector(element), position, top: rect.top, height: rect.height, width: rect.width });\n }\n return elements;\n}\n\nfunction collectlocksignals(root: Document): { bodyoverflow: string; htmloverflow: string; bodyposition: string; modal: boolean; scrollable: boolean } {\n const view = root.defaultView;\n const bodystyle = root.body ? (view ? view.getComputedStyle(root.body) : undefined) : undefined;\n const htmlstyle = view ? view.getComputedStyle(root.documentElement) : undefined;\n return {\n bodyoverflow: bodystyle?.overflow ?? \"\",\n htmloverflow: htmlstyle?.overflow ?? \"\",\n bodyposition: bodystyle?.position ?? \"\",\n modal: Boolean(root.querySelector(\"dialog[open], [aria-modal=true]\")),\n scrollable: root.documentElement.scrollHeight > root.documentElement.clientHeight,\n };\n}\n\nconst bannerselector = '[id*=\"cookie\" i], [class*=\"cookie\" i], [id*=\"consent\" i], [class*=\"consent\" i], [id*=\"gdpr\" i], [class*=\"gdpr\" i], [id*=\"privacy\" i], [class*=\"privacy\" i], [id*=\"banner\" i], [class*=\"banner\" i], dialog, [role=\"dialog\"], [aria-modal=\"true\"]';\n\n/** Collects the consent banner candidates of one document for the banner shape matcher. */\nexport function collectbannercandidates(root: Document): bannercandidate[] {\n const found = [...root.querySelectorAll(bannerselector)];\n return found\n .filter(element => !found.some(other => other !== element && other.contains(element)))\n .map(element => ({\n selector: elementselector(element),\n id: element.id,\n classes: [...element.classList],\n text: clean(element.textContent ?? \"\").slice(0, 200),\n controls: [...element.querySelectorAll(\"button, a[href], [role=button]\")].map(control => clean(control.getAttribute(\"aria-label\") ?? control.textContent ?? \"\")).filter(Boolean),\n }));\n}\n\n/** Runs one page shape detection after the background policy gate; frame routed steps receive their frame document as the root. */\nexport function runpagedetection(step: toolstep, target: Element | null, root: Document = document): stepresult | Promise<stepresult> {\n switch (step.kind) {\n case \"detectlists\": {\n const patterns = detectlistpatterns(collectsiblings(root));\n return { ok: true, summary: `Detected ${patterns.length} repeated list${patterns.length === 1 ? \"\" : \"s\"}.`, details: { lists: patterns } };\n }\n case \"detecttables\": {\n const tables: tableshape[] = collecttables(root).map(entry => {\n const shape = normalizetable(entry.rows, entry.caption);\n return { selector: entry.selector, headers: shape.headers, columns: shape.columns, rows: shape.rows, caption: shape.caption };\n });\n return { ok: true, summary: `Detected ${tables.length} data table${tables.length === 1 ? \"\" : \"s\"}.`, details: { tables } };\n }\n case \"countpages\": {\n const estimate = paginationestimate(collectpagination(root));\n return { ok: true, summary: `Counted ${estimate.links} pagination entr${estimate.links === 1 ? \"y\" : \"ies\"} and estimated ${estimate.total} total page${estimate.total === 1 ? \"\" : \"s\"}.`, details: { current: estimate.current, total: estimate.total, links: estimate.links, pages: estimate.pages } };\n }\n case \"detectinfinitescroll\": {\n const containers = infinitescrollranges(collectscrollranges(root));\n return { ok: true, summary: `Detected ${containers.length} infinite scroll container${containers.length === 1 ? \"\" : \"s\"}.`, details: { containers } };\n }\n case \"detectvirtual\": {\n const containers = virtualizedcontainers(collectvirtual(root));\n return { ok: true, summary: `Detected ${containers.length} virtualized list${containers.length === 1 ? \"\" : \"s\"}.`, details: { containers } };\n }\n case \"detectlazy\": {\n const survey = lazysurvey(collectimages(root));\n return { ok: true, summary: `Detected ${survey.lazy.length} lazy image${survey.lazy.length === 1 ? \"\" : \"s\"} and ${survey.placeholders.length} placeholder${survey.placeholders.length === 1 ? \"\" : \"s\"}.`, details: { lazy: survey.lazy, placeholders: survey.placeholders } };\n }\n case \"detectsticky\": {\n const overlays = overlaygeometry(collectoverlays(root), { width: root.defaultView?.innerWidth ?? 0, height: root.defaultView?.innerHeight ?? 0 });\n return { ok: true, summary: `Detected ${overlays.length} sticky or fixed overlay${overlays.length === 1 ? \"\" : \"s\"}.`, details: { overlays } };\n }\n case \"detectscrolllock\": {\n const lock = scrolllockstate(collectlocksignals(root));\n return { ok: true, summary: lock.locked ? `Scroll is locked: ${lock.reasons.join(\", \")}.` : \"Scroll is not locked.\", details: { locked: lock.locked, reasons: lock.reasons, scrollable: lock.scrollable } };\n }\n case \"classifypage\": {\n const signals = {\n paragraphs: root.querySelectorAll(\"p\").length,\n headings: root.querySelectorAll(\"h1, h2, h3, h4, h5, h6\").length,\n lists: root.querySelectorAll(\"ul, ol\").length,\n tables: root.querySelectorAll(\"table\").length,\n forms: root.querySelectorAll(\"form\").length,\n inputs: root.querySelectorAll(\"input, textarea, select\").length,\n password: Boolean(root.querySelector(\"input[type=password]\")),\n };\n const template = classifytemplate(signals);\n const fingerprint = sectionfingerprint({ tag: \"body\", attributes: {}, children: root.body?.children.length ?? 0, textlength: (root.body?.innerText ?? \"\").length });\n return { ok: true, summary: `Classified the page template as ${template}.`, details: { template, fingerprint } };\n }\n case \"fingerprintsection\": {\n if (!target) return { ok: false, summary: \"Fingerprint target is no longer available.\" };\n const attributes: Record<string, string> = {};\n for (const attribute of [...target.attributes]) attributes[attribute.name] = attribute.value;\n const fingerprint = sectionfingerprint({ tag: target.tagName.toLowerCase(), attributes, children: target.children.length, textlength: (target.textContent ?? \"\").length });\n return { ok: true, summary: `Computed section fingerprint ${fingerprint}.`, details: { fingerprint, section: elementselector(target) } };\n }\n case \"readscrollpos\": {\n const report = scrollreport(\n { scrollx: root.defaultView?.scrollX ?? 0, scrolly: root.defaultView?.scrollY ?? 0, scrollheight: root.documentElement.scrollHeight, clientheight: root.defaultView?.innerHeight ?? 0 },\n [...root.querySelectorAll(\"*\")].filter(element => element instanceof HTMLElement && element.scrollHeight > element.clientHeight).map(element => ({ selector: elementselector(element), scrolltop: element.scrollTop, scrollleft: element.scrollLeft, scrollheight: element.scrollHeight, clientheight: element.clientHeight })),\n );\n return { ok: true, summary: `Read the scroll position at ${Math.round(report.window.x)},${Math.round(report.window.y)} with ${report.containers.length} scrollable container${report.containers.length === 1 ? \"\" : \"s\"}.`, details: { scroll: report } };\n }\n default: return { ok: false, summary: \"Unsupported page detection.\" };\n }\n}\n", "import type { bannerreport, diffentry, focusevent, jsonstate, mutationevent, mutationwatch, quietrule, selectorcandidate, toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { bannermatches, collectbannercandidates } from \"./pagedetect.js\";\nimport { clean, elementselector } from \"./pageresolve.js\";\n\n/**\n * Watched page observation for reviewed steps.\n * Every correlated rule for watch option parsing, mutation batching, focus tracking, the network quiet probe, the snapshot diff engine, the json state scanner and the selector scorer lives in this file.\n */\n\n/** Internal sampling cadence used when a watch step reviews no poll interval. */\nconst defaultpoll = 250;\n\n/** Parsed watch options of one watch step: the mutationwatch fields plus the poll interval. */\nexport type watchoptions = mutationwatch & { watchid: string; poll: number };\n\n/** Parses the reviewed watch options of one watch step into its mutationwatch shape with a poll interval. */\nexport function parsewatchoptions(step: toolstep, fallbackid: string): watchoptions {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const scopes = Array.isArray(options.scopes) ? options.scopes.filter((item): item is string => typeof item === \"string\" && item.trim().length > 0) : undefined;\n const events = Array.isArray(options.events) ? options.events.filter((item): item is string => typeof item === \"string\" && item.trim().length > 0) : undefined;\n const lifetime = typeof options.lifetime === \"number\" && Number.isFinite(options.lifetime) && options.lifetime > 0 ? options.lifetime : 0;\n return {\n watchid: typeof options.watchid === \"string\" && options.watchid.trim() ? options.watchid : fallbackid,\n ...(scopes ? { scopes } : {}),\n ...(events ? { events } : {}),\n lifetime,\n poll: typeof options.poll === \"number\" && Number.isFinite(options.poll) && options.poll >= 0 ? options.poll : defaultpoll,\n };\n}\n\n/** Batches mutation records so records arriving inside one throttle window flush to the bridge together. */\nexport function batchmutations(records: mutationevent[], windowms: number): mutationevent[][] {\n const batches: mutationevent[][] = [];\n let current: mutationevent[] = [];\n let opened = -1;\n for (const record of records) {\n if (current.length === 0 || (windowms > 0 && record.at - opened >= windowms)) {\n if (current.length > 0) batches.push(current);\n current = [record];\n opened = record.at;\n } else current.push(record);\n }\n if (current.length > 0) batches.push(current);\n return batches;\n}\n\n/** Measures how long the network has stayed quiet from the resource timing entries completed so far. */\nexport function quietfor(entries: Array<{ responseend: number }>, now: number): number {\n let last = 0;\n for (const entry of entries) if (entry.responseend > last) last = entry.responseend;\n return Math.max(0, now - last);\n}\n\n/** Decides the network quiet outcome from collected probe samples against the reviewed idle threshold and timeout. */\nexport function quietresolution(samples: Array<{ at: number; quietfor: number }>, idle: number, timeout: number): { ok: boolean; quietfor: number; waited: number; samples: number } {\n const start = samples[0]?.at ?? 0;\n const last = samples[samples.length - 1];\n const waited = Math.max(0, (last?.at ?? 0) - start);\n const reached = samples.find(sample => sample.quietfor >= idle);\n if (reached) return { ok: true, quietfor: reached.quietfor, waited: reached.at - start, samples: samples.length };\n return { ok: false, quietfor: last?.quietfor ?? 0, waited, samples: samples.length };\n}\n\n/** Serializable node summary the diff engine compares between observation versions. */\nexport interface nodesummary {\n selector: string;\n tag: string;\n text: string;\n attributes: Record<string, string>;\n}\n\n/** Hashes one node summary into a stable digest used by the diff engine. */\nexport function nodehash(summary: nodesummary): string {\n const canonical = [summary.tag, summary.text, ...Object.keys(summary.attributes).sort().map(key => `${key}=${summary.attributes[key] ?? \"\"}`)].join(\"|\");\n let hash = 5381;\n for (let index = 0; index < canonical.length; index += 1) hash = ((hash << 5) + hash + canonical.charCodeAt(index)) >>> 0;\n return hash.toString(16);\n}\n\n/** Diffs two node summary sets into added, removed and changed entries by hashing node summaries. */\nexport function diffsummaries(base: nodesummary[], target: nodesummary[]): { added: diffentry[]; removed: diffentry[]; changed: diffentry[] } {\n const basemap = new Map(base.map(node => [node.selector, node]));\n const targetmap = new Map(target.map(node => [node.selector, node]));\n const added: diffentry[] = [];\n const removed: diffentry[] = [];\n const changed: diffentry[] = [];\n for (const [selector, node] of targetmap) {\n const previous = basemap.get(selector);\n if (!previous) { added.push({ kind: \"added\", selector, summary: node.text || node.tag }); continue; }\n if (nodehash(previous) !== nodehash(node)) changed.push({ kind: \"changed\", selector, summary: `${previous.text || previous.tag} became ${node.text || node.tag}` });\n }\n for (const [selector, node] of basemap) {\n if (!targetmap.has(selector)) removed.push({ kind: \"removed\", selector, summary: node.text || node.tag });\n }\n return { added, removed, changed };\n}\n\n/** Scans inline script payloads for embedded json state; malformed payloads are refused and counted. */\nexport function scanjson(scripts: Array<{ src: string; type: string; id: string; content: string }>): { states: jsonstate[]; refused: number } {\n const states: jsonstate[] = [];\n let refused = 0;\n for (const script of scripts) {\n if (script.src) continue;\n const content = script.content.trim();\n if (!(script.type.includes(\"json\") || content.startsWith(\"{\") || content.startsWith(\"[\"))) continue;\n try { states.push({ scripturl: script.src, rootpath: script.id, payload: JSON.parse(content) }); } catch { refused += 1; }\n }\n return { states, refused };\n}\n\n/** Ranks selector candidates of one element shape by stability: id, attribute, text and structural strategies. */\nexport function rankselectors(shape: { id: string; tag: string; attributes: Record<string, string>; text: string; index: number; siblings: number }): selectorcandidate[] {\n const candidates: selectorcandidate[] = [];\n if (shape.id) candidates.push({ selector: `#${shape.id}`, strategy: \"id\", score: 100 });\n for (const [name, value] of Object.entries(shape.attributes)) {\n if (!value) continue;\n if (name === \"name\" || name.startsWith(\"data-\") || name.startsWith(\"aria-\")) candidates.push({ selector: `${shape.tag}[${name}=\"${value}\"]`, strategy: \"attribute\", score: 80 });\n }\n if (shape.text) candidates.push({ selector: shape.text, strategy: \"text\", score: 60 });\n if (shape.index > 0) candidates.push({ selector: `${shape.tag}:nth-of-type(${shape.index})`, strategy: \"structural\", score: 40 });\n return candidates.sort((left, right) => right.score - left.score);\n}\n\nfunction wait(ms: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, ms));\n}\n\nfunction quietruleof(step: toolstep): quietrule {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const rule = options.quietrule;\n if (!rule || typeof rule !== \"object\" || Array.isArray(rule)) return { idle: 0 };\n const quiet = rule as Record<string, unknown>;\n return {\n idle: typeof quiet.idle === \"number\" && Number.isFinite(quiet.idle) && quiet.idle > 0 ? quiet.idle : 0,\n ...(typeof quiet.poll === \"number\" && Number.isFinite(quiet.poll) && quiet.poll >= 0 ? { poll: quiet.poll } : {}),\n ...(typeof quiet.timeout === \"number\" && Number.isFinite(quiet.timeout) && quiet.timeout >= 0 ? { timeout: quiet.timeout } : {}),\n };\n}\n\n/** Observes dom mutations inside the reviewed selector scopes for the reviewed lifetime, batching records through the throttle window. */\nasync function watchmutations(step: toolstep, root: Document): Promise<stepresult> {\n const options = parsewatchoptions(step, step.id);\n if (options.lifetime <= 0) return { ok: false, summary: \"The reviewed mutation watch lifetime is absent.\" };\n const roots: ParentNode[] = options.scopes ? options.scopes.flatMap(selector => [...root.querySelectorAll(selector)]) : [root];\n if (roots.length === 0) return { ok: false, summary: \"The reviewed watch scopes match no elements.\" };\n const allowed = options.events;\n const collected: mutationevent[] = [];\n const observer = new MutationObserver(records => {\n for (const record of records) {\n if (allowed && !allowed.includes(record.type)) continue;\n const target = record.target instanceof Element ? record.target : null;\n collected.push({ watchid: options.watchid, event: record.type, targetpath: target ? elementselector(target) : \"#text\", at: Date.now() });\n }\n });\n for (const scope of roots) observer.observe(scope, { childList: true, attributes: true, characterData: true, subtree: true });\n await wait(options.lifetime);\n observer.disconnect();\n const batches = batchmutations(collected, options.poll);\n return { ok: true, summary: `Watched ${collected.length} mutation${collected.length === 1 ? \"\" : \"s\"} in ${batches.length} batch${batches.length === 1 ? \"\" : \"es\"} for the reviewed lifetime of ${options.lifetime} milliseconds.`, details: { events: collected, batches: batches.length, watchid: options.watchid, lifetime: options.lifetime, scopes: options.scopes ?? [] } };\n}\n\n/** Records focus and blur events with element paths for the reviewed lifetime. */\nasync function watchfocus(step: toolstep, root: Document): Promise<stepresult> {\n const options = parsewatchoptions(step, step.id);\n if (options.lifetime <= 0) return { ok: false, summary: \"The reviewed focus watch lifetime is absent.\" };\n const collected: focusevent[] = [];\n const record = (kind: \"focus\" | \"blur\") => (event: Event): void => {\n const target = event.target instanceof Element ? event.target : null;\n collected.push({ watchid: options.watchid, kind, targetpath: target ? elementselector(target) : \"#document\", at: Date.now() });\n };\n const onfocus = record(\"focus\");\n const onblur = record(\"blur\");\n root.addEventListener(\"focusin\", onfocus, true);\n root.addEventListener(\"focusout\", onblur, true);\n await wait(options.lifetime);\n root.removeEventListener(\"focusin\", onfocus, true);\n root.removeEventListener(\"focusout\", onblur, true);\n return { ok: true, summary: `Watched ${collected.length} focus change${collected.length === 1 ? \"\" : \"s\"} for the reviewed lifetime of ${options.lifetime} milliseconds.`, details: { events: collected, watchid: options.watchid, lifetime: options.lifetime } };\n}\n\n/** Watches for cookie and consent banners for the reviewed lifetime and reports their controls. */\nasync function watchbanners(step: toolstep, root: Document): Promise<stepresult> {\n const options = parsewatchoptions(step, step.id);\n if (options.lifetime <= 0) return { ok: false, summary: \"The reviewed banner watch lifetime is absent.\" };\n const started = Date.now();\n const seen = new Map<string, bannerreport>();\n while (Date.now() - started < options.lifetime) {\n const at = Date.now();\n for (const report of bannermatches(collectbannercandidates(root), at)) {\n if (!seen.has(report.selector)) seen.set(report.selector, report);\n }\n await wait(options.poll);\n }\n const reports = [...seen.values()];\n return { ok: true, summary: `Watched for consent banners for the reviewed lifetime of ${options.lifetime} milliseconds and observed ${reports.length} banner${reports.length === 1 ? \"\" : \"s\"}.`, details: { banners: reports, watchid: options.watchid, lifetime: options.lifetime } };\n}\n\n/** Waits until the network stays quiet for the reviewed idle threshold, sampling in flight requests through the performance timeline. */\nasync function waitquiet(step: toolstep): Promise<stepresult> {\n const rule = quietruleof(step);\n if (rule.idle <= 0) return { ok: false, summary: \"The reviewed quiet idle threshold is absent.\" };\n const poll = rule.poll ?? 100;\n const timeout = rule.timeout ?? 0;\n const started = performance.now();\n const samples: Array<{ at: number; quietfor: number }> = [];\n for (;;) {\n const now = performance.now();\n const entries = (performance.getEntriesByType(\"resource\") as PerformanceResourceTiming[]).map(entry => ({ responseend: entry.responseEnd }));\n samples.push({ at: now - started, quietfor: quietfor(entries, now) });\n const latest = samples[samples.length - 1];\n if (latest && latest.quietfor >= rule.idle) break;\n if (timeout > 0 && now - started >= timeout) break;\n await wait(poll);\n }\n const outcome = quietresolution(samples, rule.idle, timeout);\n return {\n ok: outcome.ok,\n summary: outcome.ok\n ? `The network stayed quiet for ${Math.round(outcome.quietfor)} milliseconds, meeting the reviewed idle threshold of ${rule.idle} milliseconds.`\n : `The network did not stay quiet for ${rule.idle} milliseconds${timeout > 0 ? ` within the reviewed timeout of ${timeout} milliseconds` : \"\"}.`,\n details: { samples, idle: rule.idle, timeout, waited: Math.round(outcome.waited) },\n };\n}\n\nfunction scriptsurfaces(target: Element | null, root: Document): Array<{ src: string; type: string; id: string; content: string }> {\n const elements = target ? [target] : [...root.querySelectorAll(\"script\")];\n return elements.map(element => ({ src: element.getAttribute(\"src\") ?? \"\", type: element.getAttribute(\"type\") ?? \"\", id: element.id, content: element.textContent ?? \"\" }));\n}\n\n/** Extracts embedded json state from inline scripts and refuses malformed payloads. */\nfunction readjson(step: toolstep, target: Element | null, root: Document): stepresult {\n const outcome = scanjson(scriptsurfaces(target, root));\n if (target && outcome.states.length === 0 && outcome.refused > 0) return { ok: false, summary: \"The reviewed json payload is malformed and was refused.\" };\n return { ok: true, summary: `Extracted ${outcome.states.length} embedded json state${outcome.states.length === 1 ? \"\" : \"s\"}${outcome.refused > 0 ? ` and refused ${outcome.refused} malformed payload${outcome.refused === 1 ? \"\" : \"s\"}` : \"\"}.`, details: { states: outcome.states, refused: outcome.refused } };\n}\n\nfunction tonodesummaries(value: unknown): nodesummary[] | null {\n if (!Array.isArray(value)) return null;\n const summaries: nodesummary[] = [];\n for (const entry of value) {\n if (!entry || typeof entry !== \"object\" || Array.isArray(entry)) continue;\n const candidate = entry as Record<string, unknown>;\n if (typeof candidate.selector !== \"string\") continue;\n const attributes: Record<string, string> = {};\n if (candidate.attributes && typeof candidate.attributes === \"object\" && !Array.isArray(candidate.attributes)) {\n for (const [key, item] of Object.entries(candidate.attributes as Record<string, unknown>)) if (typeof item === \"string\") attributes[key] = item;\n }\n summaries.push({ selector: candidate.selector, tag: typeof candidate.tag === \"string\" ? candidate.tag : \"\", text: typeof candidate.text === \"string\" ? candidate.text : \"\", attributes });\n }\n return summaries;\n}\n\n/** Diffs the two reviewed observation versions injected by the background into added, removed and changed nodes. */\nfunction diffsnapshots(step: toolstep): stepresult {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const base = tonodesummaries(options.base);\n const target = tonodesummaries(options.target);\n if (!base || !target) return { ok: false, summary: \"Two stored observation versions must be reviewed before diffing.\" };\n const versions = Array.isArray(options.versions) && options.versions.length === 2 ? (options.versions as number[]) : [0, 0];\n const diff = diffsummaries(base, target);\n return { ok: true, summary: `Diffed observation versions ${versions[0] ?? 0} and ${versions[1] ?? 0}: ${diff.added.length} added, ${diff.removed.length} removed and ${diff.changed.length} changed node${diff.added.length + diff.removed.length + diff.changed.length === 1 ? \"\" : \"s\"}.`, details: { versions, added: diff.added, removed: diff.removed, changed: diff.changed } };\n}\n\n/** Derives ranked selector candidates for one reviewed element. */\nfunction deriveselector(target: Element | null): stepresult {\n if (!(target instanceof Element)) return { ok: false, summary: \"Derivation target is no longer available.\" };\n const attributes: Record<string, string> = {};\n for (const attribute of [...target.attributes]) attributes[attribute.name] = attribute.value;\n const parent = target.parentElement;\n const siblings = parent ? [...parent.children].filter(node => node.tagName === target.tagName) : [target];\n const candidates = rankselectors({ id: target.id, tag: target.tagName.toLowerCase(), attributes, text: clean(target.textContent ?? \"\").slice(0, 80), index: siblings.indexOf(target) + 1, siblings: siblings.length });\n const best = candidates[0];\n return { ok: candidates.length > 0, summary: best ? `Derived ${candidates.length} selector candidate${candidates.length === 1 ? \"\" : \"s\"}; the most stable is ${best.selector} through the ${best.strategy} strategy with stability ${best.score}.` : \"No selector candidate could be derived.\", details: { candidates } };\n}\n\n/** Runs one watched observation after the background policy gate; the reviewed lifetime window bounds every watch. */\nexport function runpagewatch(step: toolstep, target: Element | null, root: Document = document): stepresult | Promise<stepresult> {\n switch (step.kind) {\n case \"watchmutate\": return watchmutations(step, root);\n case \"watchfocus\": return watchfocus(step, root);\n case \"watchbanner\": return watchbanners(step, root);\n case \"waitquiet\": return waitquiet(step);\n case \"readjson\": return readjson(step, target, root);\n case \"diffsnapshots\": return diffsnapshots(step);\n case \"deriveselector\": return deriveselector(target);\n default: return { ok: false, summary: \"Unsupported watched observation.\" };\n }\n}\n", "import type { toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport { blackboxruleof, devicepresetof, familyofkind, locationpresetof, networkpresetof, agentpresetof, permissiongrantof } from \"../emulation.js\";\nimport type { stepresult } from \"./pageactions.js\";\n\n/**\n * Page-side emulation for the reviewed 1.1.48 steps.\n * Correlated rules for the injected mask layer live in the root emulation module while this file applies and reverts the masks inside the page through the scripting api: the device layer overrides the pixel ratio and the mobile hint beside the viewport bounds the window update api carries, the network layer registers the reviewed latency, throughput and offline bounds for the transport the extension initiates, the location layer overrides navigator geolocation with the reviewed coordinates, the agent layer overrides the navigator user agent, platform and brand list together and scoped to the run tab only, the permission layer answers navigator permission queries with the reviewed state, and the blackbox layer registers the reviewed patterns so stack traces hide third party frames.\n * True device metric, network condition, geolocation and user agent override needs the debugger permission or platform permissions the manifest gate forbids, so every mask is a page-injected derivation recorded honestly on every layer; the browser headers and the true device state stay untouched.\n */\n\n/** The shared registry key of the active page-side masks so reverts find the applied values after the apply. */\nconst registrykey = \"devthinkemulation\";\n\ninterface maskregistry {\n pixelratio?: number;\n geolocation?: Geolocation;\n useragent?: string;\n platform?: string;\n brands?: string[];\n permissions?: Permissions;\n blackbox?: string[];\n}\n\nfunction registry(): maskregistry {\n const holder = globalThis as typeof globalThis & { devthinkemulation?: maskregistry };\n if (holder.devthinkemulation === undefined) holder.devthinkemulation = {};\n return holder.devthinkemulation;\n}\n\n/** Captures the prior page state of one family so the revert restores the exact values. */\nfunction priorsnapshot(family: string): Record<string, unknown> {\n if (family === \"device\") return { pixelratio: window.devicePixelRatio, viewportwidth: window.innerWidth, viewportheight: window.innerHeight };\n if (family === \"agent\") return { useragent: navigator.userAgent, platform: navigator.platform };\n if (family === \"permission\") return { note: \"the browser permission state stays untouched and the override restores by removing the page-side answer\" };\n return { note: \"the layer adds no prior page state to restore\" };\n}\n\n/** Overrides the page pixel ratio and mobile hint; the viewport bounds apply through the window update api in the background because the page cannot resize itself. */\nfunction applydevice(width: number, height: number, pixelratio: number, mobile: boolean): void {\n const state = registry();\n if (state.pixelratio === undefined) state.pixelratio = window.devicePixelRatio;\n Object.defineProperty(window, \"devicePixelRatio\", { configurable: true, get: () => pixelratio });\n document.documentElement.dataset.devthinkMobile = mobile ? \"true\" : \"false\";\n document.documentElement.dataset.devthinkViewport = `${width}x${height}`;\n}\n\n/** Restores the prior pixel ratio and clears the mobile hint and viewport marker. */\nfunction revertdevice(prior: Record<string, unknown> | undefined): void {\n const state = registry();\n const restored = typeof prior?.pixelratio === \"number\" ? prior.pixelratio : state.pixelratio ?? window.devicePixelRatio;\n Object.defineProperty(window, \"devicePixelRatio\", { configurable: true, get: () => restored });\n delete document.documentElement.dataset.devthinkMobile;\n delete document.documentElement.dataset.devthinkViewport;\n delete state.pixelratio;\n}\n\n/** Overrides navigator geolocation with the reviewed coordinates and accuracy; the true browser location stays untouched. */\nfunction applylocation(latitude: number, longitude: number, accuracy: number): void {\n const state = registry();\n if (state.geolocation === undefined) state.geolocation = navigator.geolocation;\n const position = (): GeolocationPosition => ({\n coords: { latitude, longitude, accuracy, altitude: null, altitudeAccuracy: null, heading: null, speed: null } as GeolocationCoordinates,\n timestamp: Date.now(),\n } as GeolocationPosition);\n const overridden: Geolocation = {\n getCurrentPosition: success => { success(position()); },\n watchPosition: success => { success(position()); return 0; },\n clearWatch: () => { /* the page-side watch holds no timer */ },\n };\n Object.defineProperty(navigator, \"geolocation\", { configurable: true, get: () => overridden });\n}\n\n/** Restores the true navigator geolocation. */\nfunction revertlocation(): void {\n const state = registry();\n if (state.geolocation !== undefined) Object.defineProperty(navigator, \"geolocation\", { configurable: true, get: () => state.geolocation as Geolocation });\n delete state.geolocation;\n}\n\n/** Overrides the navigator user agent, platform and brand list together so page checks read the reviewed agent of the run tab only. */\nfunction applyagent(useragent: string, platform: string, brands: string[]): void {\n const state = registry();\n if (state.useragent === undefined) state.useragent = navigator.userAgent;\n if (state.platform === undefined) state.platform = navigator.platform;\n if (state.brands === undefined) state.brands = brands;\n Object.defineProperty(navigator, \"userAgent\", { configurable: true, get: () => useragent });\n Object.defineProperty(navigator, \"platform\", { configurable: true, get: () => platform });\n const branded = brands.map((brand, index) => ({ brand, version: `${index + 1}.0.0.0` }));\n const dataholder = navigator as Navigator & { userAgentData?: { brands: Array<{ brand: string; version: string }> } };\n if (dataholder.userAgentData !== undefined) Object.defineProperty(dataholder, \"userAgentData\", { configurable: true, get: () => ({ brands: branded }) });\n}\n\n/** Restores the true navigator user agent, platform and brand list. */\nfunction revertagent(prior: Record<string, unknown> | undefined): void {\n const state = registry();\n const useragent = typeof prior?.useragent === \"string\" ? prior.useragent : state.useragent ?? navigator.userAgent;\n const platform = typeof prior?.platform === \"string\" ? prior.platform : state.platform ?? navigator.platform;\n Object.defineProperty(navigator, \"userAgent\", { configurable: true, get: () => useragent });\n Object.defineProperty(navigator, \"platform\", { configurable: true, get: () => platform });\n delete state.useragent;\n delete state.platform;\n delete state.brands;\n}\n\n/** Answers navigator permission queries with the reviewed state while the browser permission itself stays untouched. */\nfunction applypermission(name: string, state: string): void {\n const holder = navigator as Navigator & { devthinkpermission?: Record<string, string> };\n if (holder.devthinkpermission === undefined) holder.devthinkpermission = {};\n holder.devthinkpermission[name] = state;\n const state0 = registry();\n if (state0.permissions === undefined && navigator.permissions !== undefined) state0.permissions = navigator.permissions;\n if (navigator.permissions === undefined) return;\n const overridden: Permissions = {\n query: description => new Promise(resolve => {\n const applied = holder.devthinkpermission?.[description.name];\n resolve({ state: (applied ?? \"prompt\") as PermissionState, name: description.name, onchange: null } as PermissionStatus);\n }),\n };\n Object.defineProperty(navigator, \"permissions\", { configurable: true, get: () => overridden });\n}\n\n/** Removes the page-side permission answers so the browser permission state returns. */\nfunction revertpermission(): void {\n const state = registry();\n if (state.permissions !== undefined) Object.defineProperty(navigator, \"permissions\", { configurable: true, get: () => state.permissions as Permissions });\n delete state.permissions;\n delete (navigator as Navigator & { devthinkpermission?: Record<string, string> }).devthinkpermission;\n}\n\n/** Registers the blackbox patterns in the page registry so stack captures hide third party frames; the rules shape traces only and read no page state. */\nfunction applyblackbox(patterns: string[]): void {\n registry().blackbox = patterns;\n}\n\n/** Returns the active blackbox patterns of the page registry for the stack capture filters. */\nexport function activeblackboxpatterns(): string[] {\n return registry().blackbox ?? [];\n}\n\n/** Runs one reviewed emulation step inside the page: the family of the kind decides the mask, the prior state is captured for the exact revert and the honest derivation note stays beside the result. */\nexport async function runemulationstep(step: toolstep): Promise<stepresult> {\n const options = (() => { try { return parseoptions(step); } catch { return {}; } })();\n const family = familyofkind(step.kind);\n const derivation = \"The mask is a page-injected override through the scripting api; the browser device metrics, network stack, true location, request headers and permission state stay untouched because no debugger or platform permission exists in the manifest.\";\n if (step.kind === \"emulatedevice\") {\n const preset = devicepresetof(options.device);\n if (!preset) return { ok: false, summary: \"The reviewed device preset is absent or malformed.\" };\n const prior = priorsnapshot(\"device\");\n applydevice(preset.width, preset.height, preset.pixelratio, preset.mobile);\n return { ok: true, summary: `Applied the device preset ${preset.name} of ${preset.width} by ${preset.height} css pixels, pixel ratio ${preset.pixelratio} and the ${preset.mobile ? \"mobile\" : \"desktop\"} hint to the run tab.`, details: { prior, preset: { name: preset.name, width: preset.width, height: preset.height, pixelratio: preset.pixelratio, mobile: preset.mobile }, derivation } };\n }\n if (step.kind === \"emulatenetwork\") {\n const preset = networkpresetof(options.network);\n if (!preset) return { ok: false, summary: \"The reviewed network preset is absent or malformed.\" };\n const window0 = typeof options.window === \"number\" ? options.window : undefined;\n return { ok: true, summary: `Applied the network preset ${preset.name} with ${preset.latency} milliseconds latency, ${preset.download} and ${preset.upload} kilobit per second bounds${preset.offline ? ` and the offline flag${window0 !== undefined ? ` for the reviewed window of ${window0} milliseconds` : \"\"}` : \"\"}; the bounds shape the traffic the extension itself initiates.`, details: { preset: { name: preset.name, latency: preset.latency, download: preset.download, upload: preset.upload, offline: preset.offline }, ...(window0 !== undefined ? { window: window0 } : {}), derivation } };\n }\n if (step.kind === \"emulatelocate\") {\n const preset = locationpresetof(options.location);\n if (!preset) return { ok: false, summary: \"The reviewed location preset is absent or malformed.\" };\n applylocation(preset.latitude, preset.longitude, preset.accuracy);\n return { ok: true, summary: `Applied the location preset ${preset.name} of ${preset.latitude}, ${preset.longitude} with the ${preset.accuracy} meter accuracy radius to the run tab.`, details: { preset: { name: preset.name, latitude: preset.latitude, longitude: preset.longitude, accuracy: preset.accuracy }, derivation } };\n }\n if (step.kind === \"setuseragent\") {\n const preset = agentpresetof(options.agent);\n if (!preset) return { ok: false, summary: \"The reviewed agent preset is absent or malformed.\" };\n const prior = priorsnapshot(\"agent\");\n applyagent(preset.useragent, preset.platform, preset.brands);\n return { ok: true, summary: `Applied the agent preset ${preset.name} with the reviewed user agent string, platform ${preset.platform} and ${preset.brands.length} brand${preset.brands.length === 1 ? \"\" : \"s\"} together, scoped to the run tab only.`, details: { prior, preset: { name: preset.name, platform: preset.platform, brands: preset.brands }, derivation } };\n }\n if (step.kind === \"overridepermission\") {\n const grant = permissiongrantof(options.permission);\n if (!grant) return { ok: false, summary: \"The reviewed permission override is absent or malformed.\" };\n const prior = priorsnapshot(\"permission\");\n applypermission(grant.name, grant.state);\n return { ok: true, summary: `Answered the ${grant.name} permission queries of the run tab with the reviewed ${grant.state} state${grant.runscope ? \" for the run scope\" : \"\"}; the browser permission itself stays untouched.`, details: { prior, permission: { name: grant.name, state: grant.state, runscope: grant.runscope }, derivation } };\n }\n if (step.kind === \"blackboxscripts\") {\n const rules = (Array.isArray(options.rules) ? options.rules : []).flatMap(rule => { const parsed = blackboxruleof(rule); return parsed !== undefined ? [parsed] : []; });\n if (rules.length === 0) return { ok: false, summary: \"The reviewed blackbox rule list is absent or malformed.\" };\n applyblackbox(rules.flatMap(rule => rule.urlpatterns));\n return { ok: true, summary: `Marked ${rules.flatMap(rule => rule.urlpatterns).length} third party url pattern${rules.flatMap(rule => rule.urlpatterns).length === 1 ? \"\" : \"s\"} as blackboxed in the traces of the run; the rules read no page state.`, details: { rules, derivation: \"Blackbox rules shape stack traces and profiles of the run only; they read no page state and touch no third party script.\" } };\n }\n void family;\n return { ok: false, summary: \"The emulation step is not part of the mask family.\" };\n}\n\n/** Reverts one emulation layer inside the page by restoring the captured prior state; the revert is idempotent for a context the navigation already destroyed. */\nexport function revertemulationlayer(family: string, prior: Record<string, unknown> | undefined): { ok: boolean; summary: string } {\n if (family === \"device\") { revertdevice(prior); return { ok: true, summary: \"Restored the prior pixel ratio and cleared the device hint of the run tab.\" }; }\n if (family === \"location\") { revertlocation(); return { ok: true, summary: \"Restored the true navigator geolocation of the run tab.\" }; }\n if (family === \"agent\") { revertagent(prior); return { ok: true, summary: \"Restored the true navigator user agent, platform and brand list of the run tab.\" }; }\n if (family === \"permission\") { revertpermission(); return { ok: true, summary: \"Removed the page-side permission answers so the browser permission state returns.\" }; }\n if (family === \"blackbox\") { delete registry().blackbox; return { ok: true, summary: \"Removed the blackbox pattern registry of the run.\" }; }\n return { ok: true, summary: \"The network layer holds no page state to restore; the transport bounds ended with the run.\" };\n}\n", "import type { consoleentry, errorrecord, loglevel, longtaskentry, rejectionrecord, timelineentry, toolstep, timelinesource } from \"../types.js\";\nimport { consolecapture, errorcapture, levelrank, loglevels, longtaskcapture, rejectioncapture, serializearg, stackframes } from \"../runtimeline.js\";\nimport { breakpointinputof, stepmodeof, watchexpressionof, overrideinputof } from \"../cdpbus.js\";\nimport { blackboxmatches } from \"../emulation.js\";\nimport { activeblackboxpatterns } from \"./pageemulate.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\n\n/**\n * Page-side debugging capture for reviewed watch steps.\n * Correlated rules for the debug watch option parsing live here while the capture, spam, rotation and diff math lives in the root runtimeline module; console methods are hooked, error and rejection listeners installed and the longtask performance buffer observed for the reviewed window only, and every hook detaches cleanly when the window closes or the tab navigates.\n * Console, error and task watching derives from page-injected listeners through the scripting api, so no debugger permission exists anywhere in the manifest.\n */\n\n/** The instrumented devtools harness key on the page global; the harness is the honest derivation of the devtools protocol because the debugger permission stays outside the manifest. */\nconst harnesskey = \"__devthinkcdp\";\n\n/** One instrumented devtools harness of the page: the enabled domains, the registered breakpoints, the applied overrides, the observed event buffer and the pause state of the instrumented debugger. */\ninterface cdpharness {\n domains: string[];\n breakpoints: Array<{ id: string; url: string; line: number; column?: number; condition?: string; hits: number }>;\n overrides: Array<{ id: string; urlpattern: string; source: string }>;\n events: Array<{ domain: string; event: string; payload?: string; at: number }>;\n paused?: { reason: string; hitbreakpoint?: string; frames: Array<{ functionname?: string; url: string; line: number; column?: number }>; scope: Record<string, unknown>; cursor: number; lines: number };\n hooks: Array<() => void>;\n}\n\n/** Reads the instrumented devtools harness of the page, if one is attached. */\nfunction readharness(): cdpharness | undefined {\n return (globalThis as typeof globalThis & Record<string, unknown>)[harnesskey] as cdpharness | undefined;\n}\n\n/** Writes or clears the instrumented devtools harness of the page. */\nfunction writeharness(harness: cdpharness | undefined): void {\n if (harness === undefined) delete (globalThis as typeof globalThis & Record<string, unknown>)[harnesskey];\n else (globalThis as typeof globalThis & Record<string, unknown>)[harnesskey] = harness;\n}\n\n/** The instrumented devtools method surface: the domains the harness enables, runtime evaluation, dom snapshots and page navigation history; every other method of the raw protocol reports the honest uninstrumented error class. */\nconst instrumentedmethods: ReadonlySet<string> = new Set([\"Runtime.evaluate\", \"Log.enable\", \"Debugger.enable\", \"DOM.enable\", \"Network.enable\", \"Page.enable\", \"DOM.getSnapshot\", \"Page.getNavigationHistory\"]);\n\n/** Parsed cdp step options: the enabled domains, the teardown plan, the raw command, the event rules with the watch window, the breakpoint, the step mode, the watch expression and the script override. */\nexport interface cdpstepoptions {\n domains: string[];\n teardown?: { revertsteps: string[]; resumepolicy: string };\n command?: { method: string; params?: Record<string, unknown>; resultpath?: string };\n events?: Array<{ domain: string; event: string; match?: string }>;\n watchwindow: number;\n breakpoint?: { url: string; line: number; column?: number; condition?: string };\n mode?: string;\n expression?: { expression: string; scope: string };\n override?: { urlpattern: string; source: string };\n}\n\n/** Parses the reviewed cdp options of one devtools protocol step through the shared cdpbus normalizers. */\nexport function cdpstepoptions(step: toolstep): cdpstepoptions {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const teardown = options.teardown && typeof options.teardown === \"object\" && !Array.isArray(options.teardown) && Array.isArray((options.teardown as Record<string, unknown>).revertsteps) ? { revertsteps: ((options.teardown as Record<string, unknown>).revertsteps as unknown[]).filter((item): item is string => typeof item === \"string\"), resumepolicy: String((options.teardown as Record<string, unknown>).resumepolicy ?? \"ask\") } : undefined;\n const command = options.command && typeof options.command === \"object\" && !Array.isArray(options.command) ? options.command as Record<string, unknown> : undefined;\n const watch = options.watch && typeof options.watch === \"object\" && !Array.isArray(options.watch) ? options.watch as Record<string, unknown> : {};\n const breakpoint = breakpointinputof(options.breakpoint);\n const expression = watchexpressionof(options.expression);\n const override = overrideinputof(options.override);\n return {\n domains: Array.isArray(options.domains) ? options.domains.filter((domain): domain is string => typeof domain === \"string\") : [],\n ...(teardown !== undefined ? { teardown } : {}),\n ...(command !== undefined && typeof command.method === \"string\" ? { command: { method: command.method, ...(command.params && typeof command.params === \"object\" && !Array.isArray(command.params) ? { params: command.params as Record<string, unknown> } : {}), ...(typeof command.resultpath === \"string\" ? { resultpath: command.resultpath } : {}) } } : {}),\n events: Array.isArray(options.events) ? options.events.flatMap(rule => {\n const parsed = rule && typeof rule === \"object\" && !Array.isArray(rule) ? rule as Record<string, unknown> : undefined;\n if (!parsed || typeof parsed.domain !== \"string\" || typeof parsed.event !== \"string\") return [];\n return [{ domain: parsed.domain, event: parsed.event, ...(typeof parsed.match === \"string\" ? { match: parsed.match } : {}) }];\n }) : [],\n watchwindow: typeof watch.window === \"number\" && Number.isFinite(watch.window) && watch.window >= 0 ? watch.window : 0,\n ...(breakpoint !== undefined ? { breakpoint } : {}),\n ...(options.mode !== undefined && stepmodeof(options.mode) !== undefined ? { mode: options.mode as string } : {}),\n ...(expression !== undefined ? { expression } : {}),\n ...(override !== undefined ? { override } : {}),\n };\n}\n\n/** Captures the light dom state of the page at a pause: the url, title, node and form counts, derived through the page bridge snapshot seam because no debugger permission exists. */\nfunction domstate(): { url: string; title: string; nodes: number; forms: number } {\n return { url: location.href, title: document.title, nodes: document.querySelectorAll(\"*\").length, forms: document.forms.length };\n}\n\n/** Runs one reviewed devtools protocol step inside the page through the instrumented harness: the harness attaches and detaches cleanly, raw commands of the instrumented surface run with duration and error class, domain events observe through the console and navigation hooks, breakpoints pause instrumented probes, stepping advances the pause, watch expressions evaluate in the pause scope and script overrides apply the reviewed fixture. */\nexport async function runcdpstep(step: toolstep): Promise<stepresult> {\n const options = cdpstepoptions(step);\n if (step.kind === \"attachcdp\") {\n const existing = readharness();\n if (existing) { for (const detach of existing.hooks) detach(); }\n const harness: cdpharness = { domains: options.domains, breakpoints: [], overrides: [], events: [], hooks: [] };\n if (options.domains.includes(\"Log\") || options.domains.includes(\"Runtime\")) {\n for (const level of loglevels) {\n const original = console[level] as (...args: unknown[]) => void;\n const hooked = (...args: unknown[]): void => {\n try { original.apply(console, args); } catch { /* the page console may refuse the passthrough; capture still proceeds */ }\n harness.events.push({ domain: \"Log\", event: \"entryAdded\", payload: args.map(arg => serializearg(arg, 2)).join(\" \"), at: Date.now() });\n };\n (console as unknown as Record<string, unknown>)[level] = hooked;\n harness.hooks.push(() => { (console as unknown as Record<string, unknown>)[level] = original; });\n }\n }\n writeharness(harness);\n const iframes = [...document.querySelectorAll(\"iframe[src]\")].map(frame => (frame as HTMLIFrameElement).src).filter(src => src.startsWith(\"https://\"));\n const serviceworker = \"serviceWorker\" in navigator && navigator.serviceWorker.controller ? navigator.serviceWorker.controller.scriptURL : undefined;\n return { ok: true, summary: `Attached the instrumented devtools harness with the reviewed domains ${options.domains.join(\", \")} enabled.`, details: { attached: true, domains: [...harness.domains], targets: { iframes, ...(serviceworker !== undefined ? { serviceworker } : {}) }, derivation: \"The chrome devtools protocol needs the debugger permission, which the manifest gate forbids; the session runs through the page-instrumented harness injected by the scripting api and the iframe and service worker targets derive from the page frame list and controller state.\" } };\n }\n if (step.kind === \"detachcdp\") {\n const harness = readharness();\n if (!harness) return { ok: false, summary: \"No instrumented devtools harness is attached to this page.\" };\n for (const detach of harness.hooks) detach();\n const reverted = { breakpoints: harness.breakpoints.length, overrides: harness.overrides.length };\n writeharness(undefined);\n return { ok: true, summary: `Detached the instrumented devtools harness cleanly after reverting ${reverted.breakpoints} breakpoint${reverted.breakpoints === 1 ? \"\" : \"s\"} and ${reverted.overrides} override${reverted.overrides === 1 ? \"\" : \"s\"}.`, details: { detached: true, ...reverted } };\n }\n if (step.kind === \"cdpcmd\") {\n const harness = readharness();\n if (!harness) return { ok: false, summary: \"No instrumented devtools harness is attached to this page.\" };\n const method = options.command?.method ?? \"\";\n const params = options.command?.params ?? {};\n if (!instrumentedmethods.has(method)) return { ok: false, summary: `The reviewed command ${method} reports the uninstrumented error class: the page harness implements ${[...instrumentedmethods].join(\", \")} only.`, details: { method, errorclass: \"uninstrumented\" } };\n const started = Date.now();\n try {\n if (method === \"Runtime.evaluate\") {\n const expression = typeof params.expression === \"string\" ? params.expression : \"\";\n const probeurl = typeof params.url === \"string\" ? params.url : \"inline\";\n const scope = params.scope && typeof params.scope === \"object\" && !Array.isArray(params.scope) ? params.scope as Record<string, unknown> : {};\n const override = harness.overrides.find(spec => overridematch(spec.urlpattern, probeurl));\n const source = override !== undefined && overridematch(override.urlpattern, probeurl) ? override.source : expression;\n const value = new Function(...Object.keys(scope), `\"use strict\"; return (${source});`)(...Object.values(scope));\n let tripped: cdpharness[\"paused\"];\n for (const breakpoint of harness.breakpoints) {\n if (breakpoint.url !== probeurl) continue;\n const conditionok = breakpoint.condition === undefined ? true : Boolean(new Function(...Object.keys(scope), `\"use strict\"; return (${breakpoint.condition});`)(...Object.values(scope)));\n if (!conditionok) continue;\n breakpoint.hits += 1;\n tripped = { reason: \"breakpoint\", hitbreakpoint: breakpoint.id, frames: stackframes(new Error().stack ?? \"\"), scope, cursor: breakpoint.line, lines: Math.max(1, source.split(\"\\n\").length) };\n harness.paused = tripped;\n break;\n }\n const serialized = serializearg(value, 3);\n harness.events.push({ domain: \"Runtime\", event: \"executionContextDestroyed\", payload: serialized.slice(0, 200), at: Date.now() });\n return { ok: true, summary: `The reviewed command ${method} returned in ${Date.now() - started} milliseconds${tripped !== undefined ? \" and paused the run on the reviewed breakpoint\" : \"\"}.`, details: { method, duration: Date.now() - started, result: { value: serialized }, ...(tripped !== undefined ? { paused: { reason: tripped.reason, hitbreakpoint: tripped.hitbreakpoint, frames: tripped.frames } } : {}) } };\n }\n if (method === \"DOM.getSnapshot\") {\n const state = domstate();\n return { ok: true, summary: `The reviewed command ${method} returned the dom snapshot of ${state.nodes} nodes in ${Date.now() - started} milliseconds.`, details: { method, duration: Date.now() - started, result: state } };\n }\n if (method === \"Page.getNavigationHistory\") {\n const state = domstate();\n return { ok: true, summary: `The reviewed command ${method} returned the page navigation history in ${Date.now() - started} milliseconds.`, details: { method, duration: Date.now() - started, result: { url: state.url, title: state.title } } };\n }\n return { ok: true, summary: `The reviewed command ${method} enabled its domain through the instrumented harness in ${Date.now() - started} milliseconds.`, details: { method, duration: Date.now() - started, result: {} } };\n } catch (error) {\n return { ok: false, summary: `The reviewed command ${method} failed with the evaluationerror class: ${error instanceof Error ? error.message : String(error)}.`, details: { method, duration: Date.now() - started, errorclass: \"evaluationerror\" } };\n }\n }\n if (step.kind === \"watchcdp\") {\n const harness = readharness();\n if (!harness) return { ok: false, summary: \"No instrumented devtools harness is attached to this page.\" };\n const started = Date.now();\n const navigation = performance.getEntriesByType(\"navigation\")[0] as PerformanceNavigationTiming | undefined;\n if (harness.domains.includes(\"Page\") && navigation !== undefined && navigation.loadEventStart > 0) harness.events.push({ domain: \"Page\", event: \"loadEventFired\", payload: location.href, at: started });\n await wait(options.watchwindow);\n const observed = harness.events.filter(event => event.at >= started);\n return { ok: true, summary: `Observed ${observed.length} domain event${observed.length === 1 ? \"\" : \"s\"} for the reviewed window of ${options.watchwindow} milliseconds.`, details: { events: observed, watchwindow: options.watchwindow, derivation: \"Domain events derive from the instrumented console hooks and the page performance navigation buffer because no debugger permission exists in the manifest.\" } };\n }\n if (step.kind === \"setbreakpoint\") {\n const harness = readharness();\n if (!harness) return { ok: false, summary: \"No instrumented devtools harness is attached to this page.\" };\n if (!options.breakpoint) return { ok: false, summary: \"The breakpoint input is absent.\" };\n const id = `bp-${options.breakpoint.url}-${options.breakpoint.line}-${options.breakpoint.column ?? 0}`;\n const registered = { id, ...options.breakpoint, hits: 0 };\n harness.breakpoints.push(registered);\n return { ok: true, summary: `Registered the reviewed breakpoint at ${options.breakpoint.url}:${options.breakpoint.line}${options.breakpoint.condition !== undefined ? ` under the condition ${options.breakpoint.condition}` : \"\"}.`, details: { breakpoint: registered } };\n }\n if (step.kind === \"stepcode\") {\n const harness = readharness();\n if (!harness) return { ok: false, summary: \"No instrumented devtools harness is attached to this page.\" };\n const mode = stepmodeof(options.mode);\n if (mode === undefined) return { ok: false, summary: \"The step code mode is absent.\" };\n if (harness.paused === undefined) return { ok: false, summary: \"No paused instrumented probe exists to step through; pause on a reviewed breakpoint first.\" };\n if (mode === \"resume\" || mode === \"stepout\") {\n const reason = harness.paused.reason;\n const frames = harness.paused.frames;\n delete harness.paused;\n return { ok: true, summary: `The ${mode} mode ${mode === \"resume\" ? \"resumed\" : \"stepped out of\"} the paused probe after ${frames.length} call frame${frames.length === 1 ? \"\" : \"s\"}.`, details: { mode, paused: false, reason } };\n }\n harness.paused.cursor += 1;\n const state = domstate();\n return { ok: true, summary: `The ${mode} mode advanced to line ${harness.paused.cursor} of the paused probe and captured the pause state with ${harness.paused.frames.length} call frame${harness.paused.frames.length === 1 ? \"\" : \"s\"} and the dom state.`, details: { mode, paused: true, pausestate: { reason: harness.paused.reason, ...(harness.paused.hitbreakpoint !== undefined ? { hitbreakpoint: harness.paused.hitbreakpoint } : {}), frames: harness.paused.frames, cursor: harness.paused.cursor, dom: state } } };\n }\n if (step.kind === \"watchexpr\") {\n const harness = readharness();\n if (!harness) return { ok: false, summary: \"No instrumented devtools harness is attached to this page.\" };\n if (!options.expression) return { ok: false, summary: \"The watch expression input is absent.\" };\n if (harness.paused === undefined) return { ok: false, summary: \"No paused instrumented probe exists to evaluate the watch expression in; pause on a reviewed breakpoint first.\" };\n try {\n const value = new Function(...Object.keys(harness.paused.scope), `\"use strict\"; return (${options.expression.expression});`)(...Object.values(harness.paused.scope));\n return { ok: true, summary: `Evaluated the reviewed watch expression at the pause in the ${options.expression.scope} scope.`, details: { expression: options.expression.expression, scope: options.expression.scope, value: serializearg(value, 3) } };\n } catch (error) {\n return { ok: false, summary: `The reviewed watch expression failed with the evaluationerror class: ${error instanceof Error ? error.message : String(error)}.`, details: { errorclass: \"evaluationerror\" } };\n }\n }\n if (step.kind === \"overridescript\") {\n const harness = readharness();\n if (!harness) return { ok: false, summary: \"No instrumented devtools harness is attached to this page.\" };\n if (!options.override) return { ok: false, summary: \"The script override input is absent.\" };\n const id = `ov-${options.override.urlpattern}`;\n harness.overrides = harness.overrides.filter(spec => spec.id !== id);\n harness.overrides.push({ id, urlpattern: options.override.urlpattern, source: options.override.source });\n try {\n new Function(options.override.source)();\n return { ok: true, summary: `Applied the reviewed script fixture for ${options.override.urlpattern} on the current document and on later instrumented evaluations of the pattern.`, details: { override: { id, urlpattern: options.override.urlpattern, applied: true } } };\n } catch (error) {\n return { ok: false, summary: `The reviewed script fixture failed with the evaluationerror class: ${error instanceof Error ? error.message : String(error)}.`, details: { errorclass: \"evaluationerror\" } };\n }\n }\n return { ok: false, summary: \"The devtools step is not part of the instrumented family.\" };\n}\n\n/** Matches one instrumented probe url against an override pattern with single star segments and double star subtrees. */\nfunction overridematch(urlpattern: string, url: string): boolean {\n const patternmatch = /^(https:\\/\\/[^/]+|inline)(\\/.*)?$/.exec(urlpattern);\n const urlmatch = /^(https:\\/\\/[^/]+|inline)(\\/.*)?$/.exec(url);\n if (!patternmatch || !urlmatch) return false;\n if (patternmatch[1] !== urlmatch[1]) return false;\n const patternpath = (patternmatch[2] ?? \"/\").split(\"/\").filter(segment => segment.length > 0);\n const urlpath = (urlmatch[2] ?? \"/\").split(\"/\").filter(segment => segment.length > 0);\n const walk = (patternindex: number, urlindex: number): boolean => {\n if (patternindex >= patternpath.length) return urlindex >= urlpath.length;\n const segment = patternpath[patternindex];\n if (segment === \"**\") return walk(patternindex + 1, urlindex) || (urlindex < urlpath.length && walk(patternindex, urlindex + 1));\n if (urlindex >= urlpath.length) return false;\n if (segment !== \"*\" && segment !== urlpath[urlindex]) return false;\n return walk(patternindex + 1, urlindex + 1);\n };\n return walk(0, 0);\n}\n\n/** Parsed debug watch options: the window, level floor, serialization depth, redaction patterns, spam rule, rotation rule and long task threshold. */\nexport interface debugwatchoptions {\n window: number;\n level?: loglevel;\n depth: number;\n redact: string[];\n spam?: { pattern: string; windowsize: number; collapse: number };\n rotation?: { maxentries: number; overflowtarget: string };\n threshold: number;\n}\n\n/** Parses the reviewed debug watch options of one watch step; absent windows watch nothing and the depth bound defaults shallow. */\nexport function debugwatchoptions(step: toolstep): debugwatchoptions {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const watch = options.watch && typeof options.watch === \"object\" && !Array.isArray(options.watch) ? options.watch as Record<string, unknown> : {};\n const spam = options.spam && typeof options.spam === \"object\" && !Array.isArray(options.spam) ? options.spam as Record<string, unknown> : undefined;\n const rotation = options.rotation && typeof options.rotation === \"object\" && !Array.isArray(options.rotation) ? options.rotation as Record<string, unknown> : undefined;\n return {\n window: typeof watch.window === \"number\" && Number.isFinite(watch.window) && watch.window >= 0 ? watch.window : 0,\n ...(typeof options.level === \"string\" && (loglevels as string[]).includes(options.level) ? { level: options.level as loglevel } : {}),\n depth: typeof options.depth === \"number\" && Number.isInteger(options.depth) && options.depth >= 1 ? options.depth : 2,\n redact: Array.isArray(options.redact) ? options.redact.filter((pattern): pattern is string => typeof pattern === \"string\" && pattern.length > 0) : [],\n ...(spam && typeof spam.pattern === \"string\" && typeof spam.windowsize === \"number\" && typeof spam.collapse === \"number\" ? { spam: { pattern: spam.pattern, windowsize: spam.windowsize, collapse: spam.collapse } } : {}),\n ...(rotation && typeof rotation.maxentries === \"number\" && typeof rotation.overflowtarget === \"string\" ? { rotation: { maxentries: rotation.maxentries, overflowtarget: rotation.overflowtarget } } : {}),\n threshold: typeof options.threshold === \"number\" && Number.isFinite(options.threshold) && options.threshold >= 0 ? options.threshold : 0,\n };\n}\n\nfunction wait(milliseconds: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, Math.max(0, milliseconds)));\n}\n\n/** Runs one reviewed debugging watch inside the page: console methods are hooked, error and rejection listeners installed and the longtask buffer observed for the reviewed window; every hook detaches cleanly at the end. */\nexport async function rundebugwatch(step: toolstep): Promise<stepresult> {\n const options = debugwatchoptions(step);\n if (options.window <= 0) return { ok: false, summary: \"The reviewed debug watch window is absent.\" };\n const started = Date.now();\n const entries: Array<Omit<timelineentry, \"id\" | \"runid\">> = [];\n const consoleentries: consoleentry[] = [];\n const errors: Array<Omit<errorrecord, \"id\" | \"runid\">> = [];\n const rejections: Array<Omit<rejectionrecord, \"id\" | \"runid\">> = [];\n const resources: Array<{ message: string; element: string; sourceurl: string }> = [];\n const longtasks: Array<Omit<longtaskentry, \"id\" | \"runid\">> = [];\n const floor = options.level !== undefined ? levelrank(options.level) : undefined;\n const capture = (level: loglevel, source: timelinesource, message: string, at: number): void => {\n if (floor !== undefined && levelrank(level) > floor) return;\n entries.push({ stepid: step.id, time: at, level, source, message });\n };\n const hooks: Array<() => void> = [];\n if (step.kind === \"watchconsole\") {\n for (const level of loglevels) {\n const original = console[level] as (...args: unknown[]) => void;\n const hooked = (...args: unknown[]): void => {\n try { original.apply(console, args); } catch { /* the page console may refuse the passthrough; capture still proceeds */ }\n const entry = consolecapture({ level, args, depth: options.depth, redact: options.redact });\n if (floor === undefined || levelrank(level) <= floor) consoleentries.push(entry);\n capture(level, \"console\", entry.text, Date.now());\n };\n (console as unknown as Record<string, unknown>)[level] = hooked;\n hooks.push(() => { (console as unknown as Record<string, unknown>)[level] = original; });\n }\n }\n if (step.kind === \"watcherrors\") {\n const blackbox = activeblackboxpatterns();\n const hideframes = <T extends { frames: Array<{ url: string }> }>(record: T): T => ({ ...record, frames: record.frames.filter(frame => !blackbox.some(pattern => blackboxmatches(pattern, frame.url))) });\n const onerror = (event: ErrorEvent): void => {\n const record = hideframes(errorcapture({ message: event.message, sourceurl: event.filename, line: event.lineno, ...(event.error instanceof Error ? { stacktext: event.error.stack } : {}), redact: options.redact }));\n errors.push({ ...record, stepid: step.id, at: Date.now() });\n capture(\"error\", \"error\", record.message, Date.now());\n };\n const onrejection = (event: PromiseRejectionEvent): void => {\n const reason = event.reason instanceof Error ? `${event.reason.name}: ${event.reason.message}` : String(event.reason);\n const record = hideframes(rejectioncapture({ reason, ...(event.reason instanceof Error ? { stacktext: event.reason.stack } : {}), redact: options.redact }));\n rejections.push({ ...record, stepid: step.id, at: Date.now() });\n capture(\"error\", \"rejection\", record.reason, Date.now());\n };\n const onresource = (event: Event): void => {\n const target = event.target;\n if (!(target instanceof Element)) return;\n const element = target.tagName.toLowerCase() + (target.id ? `#${target.id}` : \"\");\n const sourceurl = target instanceof HTMLImageElement || target instanceof HTMLScriptElement ? (target.src ?? \"\") : target instanceof HTMLLinkElement ? (target.href ?? \"\") : \"\";\n const message = `Failed to load ${element}${sourceurl ? ` from ${sourceurl}` : \"\"}.`;\n resources.push({ message, element, sourceurl });\n capture(\"error\", \"resource\", message, Date.now());\n };\n window.addEventListener(\"error\", onerror, true);\n window.addEventListener(\"unhandledrejection\", onrejection, true);\n window.addEventListener(\"error\", onresource, true);\n hooks.push(() => {\n window.removeEventListener(\"error\", onerror, true);\n window.removeEventListener(\"unhandledrejection\", onrejection, true);\n window.removeEventListener(\"error\", onresource, true);\n });\n }\n if (step.kind === \"watchtasks\") {\n const observer = new PerformanceObserver(list => {\n for (const entry of list.getEntries()) {\n const detail = entry as { duration: number; startTime: number; attribution?: Array<{ name?: string }> };\n const attributions = (detail.attribution ?? []).map(container => String(container.name ?? \"\")).filter(name => name.length > 0);\n longtasks.push({ stepid: step.id, duration: Math.round(detail.duration), starttime: Math.round(detail.startTime), attributions, at: Date.now() });\n }\n });\n observer.observe({ entryTypes: [\"longtask\"] });\n hooks.push(() => observer.disconnect());\n }\n await wait(options.window);\n for (const detach of hooks) detach();\n if (step.kind === \"watchtasks\") {\n const filtered = longtaskcapture({ entries: longtasks, threshold: options.threshold });\n longtasks.length = 0;\n longtasks.push(...filtered.map(task => ({ ...task, stepid: step.id, at: started })));\n for (const task of longtasks) capture(\"info\", \"longtask\", `Long task of ${task.duration} milliseconds blocked the main thread${task.attributions.length > 0 ? ` (${task.attributions.join(\", \")})` : \"\"}.`, task.at);\n }\n const summary = step.kind === \"watchconsole\"\n ? `Captured ${consoleentries.length} console call${consoleentries.length === 1 ? \"\" : \"s\"} at every level for the reviewed window of ${options.window} milliseconds.`\n : step.kind === \"watcherrors\"\n ? `Captured ${errors.length} error${errors.length === 1 ? \"\" : \"s\"}, ${rejections.length} rejection${rejections.length === 1 ? \"\" : \"s\"} and ${resources.length} resource failure${resources.length === 1 ? \"\" : \"s\"} for the reviewed window of ${options.window} milliseconds.`\n : `Captured ${longtasks.length} long task${longtasks.length === 1 ? \"\" : \"s\"} for the reviewed window of ${options.window} milliseconds.`;\n return { ok: true, summary, details: { entries, console: consoleentries, errors, rejections, resources, longtasks, watchwindow: options.window, depth: options.depth, derivation: \"Console, error and task watching derives from page-injected listeners and the performance buffers through the scripting api; no debugger permission exists in the manifest.\" } };\n}\n", "import type { toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\n\n/**\n * Page-side profiling capture for the reviewed 1.1.47 steps.\n * Correlated rules for the profiling option parsing live here while the flow, heap, cpu, shift, trace and source map math lives in the root profilers module: performance marks carry the step windows of the flow spec, the paint, navigation, longtask, layout shift and event buffers are observed for the reviewed window only, heap samples derive from the page performance memory buffer and the dom node count, and source map declarations are read from the loaded same origin scripts.\n * Every measurement derives from the performance timeline buffers and the injected instrumentation probes through the scripting api, so no debugger permission exists anywhere in the manifest.\n */\n\n/** Parses the reviewed profiling options of one profiling step; absent windows watch nothing and the depth of capture stays the reviewed window only. */\nexport function profilestepoptions(step: toolstep): {\n flow?: { prefix: string; steps: string[]; metrics: string[] };\n watchwindow: number;\n heapinterval: number;\n growth?: { slope: number; interval: number };\n duration: number;\n threshold: number;\n categories: string[];\n exporttarget?: string;\n traceid?: string;\n scripts: string[];\n} {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const watch = options.watch && typeof options.watch === \"object\" && !Array.isArray(options.watch) ? options.watch as Record<string, unknown> : {};\n const flow = options.flow && typeof options.flow === \"object\" && !Array.isArray(options.flow) ? options.flow as Record<string, unknown> : undefined;\n const heap = options.heap && typeof options.heap === \"object\" && !Array.isArray(options.heap) ? options.heap as Record<string, unknown> : {};\n const growth = options.growth && typeof options.growth === \"object\" && !Array.isArray(options.growth) ? options.growth as Record<string, unknown> : undefined;\n const profile = options.profile && typeof options.profile === \"object\" && !Array.isArray(options.profile) ? options.profile as Record<string, unknown> : {};\n const trace = options.trace && typeof options.trace === \"object\" && !Array.isArray(options.trace) ? options.trace as Record<string, unknown> : {};\n return {\n ...(flow !== undefined && typeof flow.prefix === \"string\" && Array.isArray(flow.steps) && Array.isArray(flow.metrics) ? { flow: { prefix: flow.prefix, steps: flow.steps.filter((item): item is string => typeof item === \"string\"), metrics: flow.metrics.filter((item): item is string => typeof item === \"string\") } } : {}),\n watchwindow: typeof watch.window === \"number\" && Number.isFinite(watch.window) && watch.window >= 0 ? watch.window : 0,\n heapinterval: typeof heap.interval === \"number\" && Number.isFinite(heap.interval) && heap.interval >= 0 ? heap.interval : 0,\n ...(growth !== undefined && typeof growth.slope === \"number\" ? { growth: { slope: growth.slope, interval: typeof growth.interval === \"number\" && Number.isFinite(growth.interval) && growth.interval >= 0 ? growth.interval : 0 } } : {}),\n duration: typeof profile.duration === \"number\" && Number.isFinite(profile.duration) && profile.duration >= 0 ? profile.duration : 0,\n threshold: typeof options.threshold === \"number\" && Number.isFinite(options.threshold) && options.threshold >= 0 ? options.threshold : 0,\n categories: Array.isArray(trace.categories) ? trace.categories.filter((category): category is string => typeof category === \"string\") : [],\n ...(typeof trace.exporttarget === \"string\" ? { exporttarget: trace.exporttarget } : {}),\n ...(typeof trace.traceid === \"string\" ? { traceid: trace.traceid } : {}),\n scripts: Array.isArray(options.scripts) ? options.scripts.filter((url): url is string => typeof url === \"string\") : [],\n };\n}\n\n/** Resolves the reviewed trace category of one performance entry: navigation entries stay navigation, marks, measures, long tasks and event timing stay scripting, paint and layout shifts stay painting, resources stay loading while fetch and xmlhttprequest initiators stay network. */\nfunction categoryof(entry: PerformanceEntry, initiator?: string): string {\n if (entry.entryType === \"navigation\") return \"navigation\";\n if (entry.entryType === \"paint\" || entry.entryType === \"largest-contentful-paint\" || entry.entryType === \"layout-shift\") return \"painting\";\n if (entry.entryType === \"resource\") return initiator === \"fetch\" || initiator === \"xmlhttprequest\" ? \"network\" : \"loading\";\n return \"scripting\";\n}\n\n/** Collects the performance buffer entries of the reviewed types for the observed window as plain rows; the buffered observer covers the paint, navigation, longtask, layout shift, event and first input entries the getEntriesByType buffer misses. */\nasync function collectentries(watchwindow: number, types: string[]): Promise<Array<{ name: string; type: string; start: number; duration: number; initiator?: string }>> {\n const rows: Array<{ name: string; type: string; start: number; duration: number; initiator?: string }> = [];\n for (const type of [\"navigation\", \"paint\", \"mark\", \"measure\", \"resource\", \"longtask\"]) {\n for (const entry of performance.getEntriesByType(type)) {\n rows.push({ name: entry.name, type: entry.entryType, start: entry.startTime, duration: entry.duration, ...(type === \"resource\" ? { initiator: (entry as PerformanceResourceTiming).initiatorType } : {}) });\n }\n }\n if (types.length > 0) {\n await new Promise<void>(resolve => {\n const observer = new PerformanceObserver(list => {\n for (const entry of list.getEntries()) rows.push({ name: entry.name, type: entry.entryType, start: entry.startTime, duration: entry.duration });\n });\n observer.observe({ entryTypes: types, buffered: true } as PerformanceObserverInit);\n window.setTimeout(() => { observer.disconnect(); resolve(); }, Math.max(0, watchwindow));\n });\n } else {\n await new Promise(resolve => window.setTimeout(resolve, Math.max(0, watchwindow)));\n }\n return rows;\n}\n\n/** Reads the page heap sample: the used and limit bytes of the performance memory buffer with the dom node count, the honest derivation because no heap profiler exists without the debugger permission. */\nfunction heapsample(): { usedbytes: number; limitbytes: number; nodecount: number } {\n const memory = (performance as Performance & { memory?: { usedJSHeapSize: number; totalJSHeapSize: number; jsHeapSizeLimit: number } }).memory;\n return { usedbytes: memory?.usedJSHeapSize ?? 0, limitbytes: memory?.jsHeapSizeLimit ?? memory?.totalJSHeapSize ?? 0, nodecount: document.querySelectorAll(\"*\").length };\n}\n\nfunction wait(milliseconds: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, Math.max(0, milliseconds)));\n}\n\n/** Runs one reviewed profiling step inside the page: the flow spec marks the start and end of every step in the window while the performance buffers feed the metric math of the root profilers module, the heap snapshot reads the memory buffer and the node count, the cpu window observes the long task and event timing samples, the layout shift watch scores the shifts of the window with their impacted selectors, the trace record categorizes the observed entries of the reviewed categories, and the source map capture reads the sourceMappingURL declarations of the loaded same origin scripts. */\nexport async function runprofilestep(step: toolstep): Promise<stepresult> {\n const options = profilestepoptions(step);\n if (step.kind === \"measureflow\") {\n if (!options.flow || options.watchwindow <= 0) return { ok: false, summary: \"The reviewed flow spec with its watch window is absent.\" };\n const started = performance.now();\n for (const stepid of options.flow.steps) performance.mark(`${options.flow.prefix}:${stepid}:start`);\n const entries = await collectentries(options.watchwindow, [\"largest-contentful-paint\", \"first-input\", \"event\", \"longtask\"]);\n for (const stepid of options.flow.steps) performance.mark(`${options.flow.prefix}:${stepid}:end`);\n for (const stepid of options.flow.steps) performance.measure(`${options.flow.prefix}:${stepid}`, `${options.flow.prefix}:${stepid}:start`, `${options.flow.prefix}:${stepid}:end`);\n return { ok: true, summary: `Marked the start and end of ${options.flow.steps.length} step${options.flow.steps.length === 1 ? \"\" : \"s\"} of the flow ${options.flow.prefix} and collected ${entries.length} performance entr${entries.length === 1 ? \"y\" : \"ies\"} for the reviewed window of ${options.watchwindow} milliseconds.`, details: { entries, watchwindow: options.watchwindow, started, derivation: \"Flow measurement derives from the performance timeline buffers and the injected marks through the scripting api; no debugger permission exists in the manifest.\" } };\n }\n if (step.kind === \"heapshot\") {\n const sample = heapsample();\n return { ok: true, summary: `Captured the on demand heap sample of ${sample.usedbytes} used bytes against the ${sample.limitbytes} byte limit with ${sample.nodecount} dom node${sample.nodecount === 1 ? \"\" : \"s\"}.`, details: { ...sample, derivation: \"Heap bytes derive from the page performance memory buffer and the node count from the dom because no heap profiler exists without the debugger permission.\" } };\n }\n if (step.kind === \"trackmemory\") {\n if (!options.growth) return { ok: false, summary: \"The reviewed growth slope is absent.\" };\n const sample = heapsample();\n return { ok: true, summary: `Took the heap sample of ${sample.usedbytes} used bytes beside the step for the growth tracking of slope ${options.growth.slope} bytes per millisecond.`, details: { ...sample, slope: options.growth.slope, interval: options.growth.interval, derivation: \"Growth samples derive from the page performance memory buffer beside every step of the run.\" } };\n }\n if (step.kind === \"profilecpu\") {\n if (options.duration <= 0) return { ok: false, summary: \"The reviewed cpu profile duration is absent.\" };\n const started = performance.now();\n const entries = await collectentries(options.duration, [\"longtask\", \"event\", \"first-input\"]);\n const samples = entries.filter(entry => entry.type === \"longtask\" || entry.type === \"event\" || entry.type === \"first-input\").map(entry => ({ name: entry.name || entry.type, time: entry.duration }));\n return { ok: true, summary: `Profiled the cpu window of ${options.duration} milliseconds with ${samples.length} sample${samples.length === 1 ? \"\" : \"s\"} from the long task and event timing buffers.`, details: { samples, duration: options.duration, started, derivation: \"Cpu samples derive from the long task attribution and event timing buffers because no sampling profiler exists without the debugger permission.\" } };\n }\n if (step.kind === \"watchshifts\") {\n if (options.watchwindow <= 0) return { ok: false, summary: \"The reviewed layout shift window is absent.\" };\n const shifts: Array<{ score: number; starttime: number; selectors: string[] }> = [];\n await new Promise<void>(resolve => {\n const observer = new PerformanceObserver(list => {\n for (const entry of list.getEntries()) {\n const shift = entry as PerformanceEntry & { value?: number; sources?: Array<{ node?: Node }> };\n const selectors = (shift.sources ?? []).flatMap(source => source.node instanceof Element ? [source.node.tagName.toLowerCase() + (source.node.id ? `#${source.node.id}` : \"\")] : []);\n const score = typeof shift.value === \"number\" ? shift.value : 0;\n if (options.threshold > 0 && score < options.threshold) continue;\n shifts.push({ score, starttime: shift.startTime, selectors });\n }\n });\n observer.observe({ entryTypes: [\"layout-shift\"], buffered: true } as PerformanceObserverInit);\n window.setTimeout(() => { observer.disconnect(); resolve(); }, options.watchwindow);\n });\n return { ok: true, summary: `Watched ${shifts.length} layout shift${shifts.length === 1 ? \"\" : \"s\"} for the reviewed window of ${options.watchwindow} milliseconds${options.threshold > 0 ? ` with the score threshold ${options.threshold}` : \"\"}.`, details: { shifts, watchwindow: options.watchwindow, derivation: \"Layout shifts derive from the performance layout-shift buffer with the impacted element selectors of the shift sources.\" } };\n }\n if (step.kind === \"traceload\") {\n if (options.categories.length === 0 || options.watchwindow <= 0) return { ok: false, summary: \"The reviewed trace categories or window are absent.\" };\n const started = performance.now();\n const entries = await collectentries(options.watchwindow, [\"largest-contentful-paint\", \"first-input\", \"event\", \"longtask\", \"layout-shift\"]);\n const events = entries.filter(entry => options.categories.includes(categoryof({ name: entry.name, entryType: entry.type, startTime: entry.start, duration: entry.duration } as PerformanceEntry, entry.initiator))).map(entry => ({ name: entry.name, category: categoryof({ name: entry.name, entryType: entry.type, startTime: entry.start, duration: entry.duration } as PerformanceEntry, entry.initiator), offset: Math.round(entry.start - started) }));\n return { ok: true, summary: `Recorded ${events.length} trace event${events.length === 1 ? \"\" : \"s\"} of the reviewed categories ${options.categories.join(\", \")} for the window of ${options.watchwindow} milliseconds and derived the exportable trace file.`, details: { events, categories: options.categories, watchwindow: options.watchwindow, started, ...(options.exporttarget !== undefined ? { exporttarget: options.exporttarget } : {}), derivation: \"The trace file derives from the performance timeline entries of the reviewed categories; it is not the devtools binary trace format because no debugger permission exists in the manifest.\" } };\n }\n if (step.kind === \"capturesourcemaps\") {\n const scripts: Array<{ url: string; mapurl?: string }> = [];\n for (const element of document.querySelectorAll(\"script[src]\")) {\n const src = (element as HTMLScriptElement).src;\n if (!src.startsWith(location.origin)) continue;\n if (options.scripts.length > 0 && !options.scripts.includes(src)) continue;\n let mapurl: string | undefined;\n try {\n const response = await fetch(src, { credentials: \"same-origin\" });\n const source = await response.text();\n const match = /[#@]\\s*sourceMappingURL=(\\S+)/.exec(source);\n if (match !== null && match[1] !== undefined) mapurl = new URL(match[1], src).toString();\n } catch { /* a script the page refuses to re-fetch stays without a captured map; the capture continues */ }\n scripts.push({ url: src, ...(mapurl !== undefined ? { mapurl } : {}) });\n }\n const withmaps = scripts.filter(script => script.mapurl !== undefined);\n return { ok: true, summary: `Read the sourceMappingURL declarations of ${scripts.length} same origin script${scripts.length === 1 ? \"\" : \"s\"} of ${location.origin} and found ${withmaps.length} map declaration${withmaps.length === 1 ? \"\" : \"s\"}; the script sources stay in the page bridge and only the map urls leave it.`, details: { scripts, origin: location.origin, derivation: \"Source map declarations are read by re-fetching the loaded same origin scripts of the page; cross origin scripts stay outside the capture and no map content enters the page bridge.\" } };\n }\n return { ok: false, summary: \"The profiling step is not part of the instrumented family.\" };\n}\n", "import type { fielderror, fieldkind, fieldmatch, formrecord, formentry, formreport, toolstep } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { clean, elementlabel as label, elementselector as selector } from \"./pageresolve.js\";\n\n/**\n * Form field logics for reviewed steps.\n * Every correlated rule for field matching, field kind classification, seeded value generation, native value fills, honeypot detection, login and template detection, error association and form record parsing lives in this file.\n */\n\n/** One serializable form field shape resolved against the page controls. */\nexport interface fieldshape {\n selector: string;\n tag: string;\n type: string;\n name: string;\n label: string;\n placeholder: string;\n arialabel: string;\n autocomplete: string;\n options?: string[];\n}\n\n/** One surveyed field shape carrying the visibility, geometry and timing evidence the honeypot detector reads. */\nexport interface fieldsurvey extends fieldshape {\n hidden: boolean;\n offscreen: boolean;\n createdat?: number;\n}\n\n/** One honeypot field flagged by hidden, offscreen or time trap evidence. */\nexport interface honeypotevidence {\n selector: string;\n reason: \"hidden\" | \"offscreen\" | \"timetrap\";\n}\n\n/** One surveyed field with the aria describedby ref and the sibling message texts the error reader associates. */\nexport interface errorcontext extends fieldshape {\n describedby?: string;\n siblings: string[];\n}\n\n/** Resolves controls by label, placeholder, aria label and name attributes; matches are case insensitive substrings. */\nexport function matchfield(fields: fieldshape[], match: fieldmatch): fieldshape[] {\n const key = match.mode === \"label\" ? \"label\" : match.mode === \"placeholder\" ? \"placeholder\" : match.mode === \"arialabel\" ? \"arialabel\" : \"name\";\n const needle = (match[key] ?? \"\").trim().toLowerCase();\n if (!needle) return [];\n return fields.filter(field => {\n const primary = (field[key] as string).toLowerCase();\n const secondary = match.mode === \"label\" || match.mode === \"name\" ? field.name.toLowerCase() : match.mode === \"placeholder\" ? field.arialabel.toLowerCase() : field.placeholder.toLowerCase();\n return primary.includes(needle) || secondary.includes(needle);\n });\n}\n\n/** Infers the field kind of one control from its input type, autocomplete hint and label text. */\nexport function classifyfield(input: { type: string; autocomplete: string; label: string }): fieldkind {\n const type = input.type.toLowerCase();\n const autocomplete = input.autocomplete.toLowerCase();\n const label = input.label.toLowerCase();\n if (type === \"password\") return \"password\";\n if (autocomplete.startsWith(\"cc-\") || label.includes(\"card number\") || label.includes(\"credit card\") || label.includes(\"cardholder\")) return \"card\";\n if (autocomplete.includes(\"one-time-code\") || autocomplete.includes(\"otp\") || label.includes(\"one time code\") || label.includes(\"verification code\") || label.includes(\"otp\")) return \"code\";\n if (type === \"email\" || autocomplete.includes(\"email\") || label.includes(\"email\")) return \"email\";\n if (type === \"tel\" || autocomplete.includes(\"tel\") || label.includes(\"phone\") || label.includes(\"telephone\")) return \"phone\";\n if (type === \"date\") return \"date\";\n if (type === \"number\") return \"number\";\n if (type === \"checkbox\") return \"check\";\n if (type === \"radio\") return \"radio\";\n if (type === \"file\") return \"file\";\n if (type === \"select\" || type === \"select-one\") return \"select\";\n return \"text\";\n}\n\nconst firstnames: Record<string, string[]> = { en: [\"alex\", \"jordan\", \"taylor\", \"morgan\", \"casey\"], pt: [\"ana\", \"bruno\", \"carla\", \"diego\", \"helena\"] };\nconst lastnames: Record<string, string[]> = { en: [\"brooks\", \"carter\", \"diaz\", \"evans\", \"reyes\"], pt: [\"alves\", \"costa\", \"lima\", \"souza\", \"moraes\"] };\n\nfunction localekey(locale: string): string {\n const normalized = locale.toLowerCase();\n if (normalized.startsWith(\"pt\")) return \"pt\";\n return \"en\";\n}\n\n/** Generates one realistic value for a field kind, deterministically seeded and locale aware for names, emails and phones. */\nexport function generatevalue(kind: fieldkind, rule: { locale?: string; seed?: number }): string {\n const seed = typeof rule.seed === \"number\" && Number.isFinite(rule.seed) ? Math.abs(Math.floor(rule.seed)) : 1;\n const names = firstnames[localekey(rule.locale ?? \"en\")] ?? firstnames.en ?? [\"alex\"];\n const surnames = lastnames[localekey(rule.locale ?? \"en\")] ?? lastnames.en ?? [\"brooks\"];\n let state = seed * 1103515245 + 12345;\n const next = (): number => { state = (state * 1103515245 + 12345) % 2147483648; return state / 2147483648; };\n const pick = <T>(items: T[]): T => items[Math.floor(next() * items.length) % items.length] ?? items[0] as T;\n const digits = (count: number): string => Array.from({ length: count }, () => String(Math.floor(next() * 10))).join(\"\");\n const person = `${pick(names)} ${pick(surnames)}`;\n switch (kind) {\n case \"email\": return `${person.replace(\" \", \".\")}${digits(2)}@example.com`;\n case \"phone\": return localekey(rule.locale ?? \"en\") === \"pt\" ? `+55 (11) 9${digits(4)}-${digits(4)}` : `+1 (555) 010-${digits(4)}`;\n case \"date\": return `${2024 + Math.floor(next() * 2)}-${String(1 + Math.floor(next() * 12)).padStart(2, \"0\")}-${String(1 + Math.floor(next() * 28)).padStart(2, \"0\")}`;\n case \"number\": return String(Math.floor(next() * 1000));\n case \"select\": return `option ${1 + Math.floor(next() * 5)}`;\n case \"check\": return next() > 0.5 ? \"true\" : \"false\";\n case \"radio\": return `choice ${1 + Math.floor(next() * 4)}`;\n case \"file\": return `sample${digits(2)}.pdf`;\n case \"password\": return `pw-${digits(6)}-${pick(names)}`;\n case \"card\": return `4111 ${digits(4)} ${digits(4)} ${digits(4)}`;\n case \"code\": return digits(6);\n default: return person;\n }\n}\n\n/** Builds a deterministic values hash of the reviewed field values a submission ticket records. */\nexport function valueshash(values: Array<{ label: string; value: string }>): string {\n const source = values.map(entry => `${entry.label}=${entry.value}`).join(\"|\");\n let hash = 5381;\n for (let index = 0; index < source.length; index += 1) hash = ((hash * 33) ^ source.charCodeAt(index)) >>> 0;\n return hash.toString(16);\n}\n\n/** Parses the reviewed structured form record of a step; null when the step reviews none or the shape is invalid. */\nexport function parseformrecord(value: unknown): formrecord | null {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return null;\n const record = value as Record<string, unknown>;\n if (!Array.isArray(record.entries)) return null;\n const entries: formentry[] = [];\n for (const item of record.entries) {\n if (!item || typeof item !== \"object\" || Array.isArray(item)) continue;\n const entry = item as Record<string, unknown>;\n const match = entry.match;\n if (!match || typeof match !== \"object\" || Array.isArray(match)) continue;\n const shapes = match as Record<string, unknown>;\n if (typeof shapes.mode !== \"string\") continue;\n const fieldmatch: fieldmatch = {\n mode: shapes.mode as fieldmatch[\"mode\"],\n ...(typeof shapes.label === \"string\" ? { label: shapes.label } : {}),\n ...(typeof shapes.placeholder === \"string\" ? { placeholder: shapes.placeholder } : {}),\n ...(typeof shapes.arialabel === \"string\" ? { arialabel: shapes.arialabel } : {}),\n ...(typeof shapes.name === \"string\" ? { name: shapes.name } : {}),\n };\n if (typeof entry.kind !== \"string\" || typeof entry.value !== \"string\") continue;\n entries.push({ match: fieldmatch, kind: entry.kind as fieldkind, value: entry.value });\n }\n if (entries.length === 0) return null;\n return { ...(typeof record.form === \"string\" && record.form ? { form: record.form } : {}), entries };\n}\n\n/** Builds form record entries from reviewed label or placeholder value pairs. */\nexport function pairentries(pairs: Array<{ label?: string; placeholder?: string; value: string }>, mode: \"label\" | \"placeholder\"): formrecord {\n return { entries: pairs.map(pair => ({ match: mode === \"label\" ? { mode, label: pair.label ?? \"\" } : { mode, placeholder: pair.placeholder ?? \"\" }, kind: \"text\", value: pair.value })) };\n}\n\n/** One fill operation outcome: the entry, the matched control, the honeypot skip flag or the refusal reason. */\nexport interface filloutcome {\n entry: formentry;\n matched?: fieldshape;\n skipped?: boolean;\n reason?: string;\n}\n\n/** Resolves every entry of a form record against the surveyed fields, skipping honeypots and refusing unmatched or ambiguous entries. */\nexport function filloperations(record: formrecord, fields: fieldshape[], skippedselectors: string[] = []): filloutcome[] {\n return record.entries.map(entry => {\n const matches = matchfield(fields, entry.match);\n if (matches.length === 0) return { entry, reason: \"unmatched\" };\n if (matches.length > 1) return { entry, reason: \"ambiguous\" };\n const matched = matches[0] as fieldshape;\n if (skippedselectors.includes(matched.selector)) return { entry, matched, skipped: true };\n return { entry, matched };\n });\n}\n\n/** Masks one card segment so side panels can render card fills without exposing the full value. */\nexport function cardmask(value: string): string {\n const trimmed = value.trim();\n if (/^\\d[\\d\\s-]{11,18}$/.test(trimmed)) {\n const compact = trimmed.replace(/[\\s-]/g, \"\");\n const last = compact.slice(-4);\n return `${\"\u2022\".repeat(Math.max(0, compact.length - 4))}${last}`;\n }\n return \"\u2022\".repeat(trimmed.length);\n}\n\n/** Flags hidden, offscreen and time trap fields so fill steps skip them instead of tripping anti bot defenses. */\nexport function detecthoneypots(surveys: fieldsurvey[], loadedat: number): honeypotevidence[] {\n const traps: honeypotevidence[] = [];\n for (const field of surveys) {\n if (field.hidden) traps.push({ selector: field.selector, reason: \"hidden\" });\n else if (field.offscreen) traps.push({ selector: field.selector, reason: \"offscreen\" });\n else if (field.createdat !== undefined && loadedat > 0 && field.createdat > loadedat) traps.push({ selector: field.selector, reason: \"timetrap\" });\n }\n return traps;\n}\n\n/** Detects a login form: a password field plus an identifier field with session links nearby. */\nexport function detectlogin(fields: fieldshape[], links: string[]): { login: boolean; markers: string[] } {\n const markers: string[] = [];\n const password = fields.find(field => classifyfield(field) === \"password\");\n if (password) markers.push(\"password field\");\n const identifier = fields.find(field => {\n const kind = classifyfield(field);\n return kind === \"email\" || (kind === \"text\" && /user|login|account|identifier/i.test(`${field.name} ${field.label}`));\n });\n if (identifier) markers.push(\"identifier field\");\n const sessionlink = links.some(link => /sign in|log in|log on|forgot|create account|sign up/i.test(link));\n if (sessionlink) markers.push(\"session link\");\n return { login: Boolean(password && identifier && sessionlink), markers };\n}\n\nconst signupmarkers = [\"sign up\", \"create account\", \"register\", \"confirm password\", \"terms\"];\nconst checkoutmarkers = [\"checkout\", \"payment\", \"billing\", \"shipping\", \"card number\", \"place order\", \"cart\"];\n\n/** Detects signup and checkout templates by matching the field labels, autocompletes and page text against known markers. */\nexport function detecttemplate(fields: fieldshape[], text: string): { template: \"signup\" | \"checkout\" | \"unknown\"; markers: string[] } {\n const corpus = [text, ...fields.map(field => `${field.label} ${field.name} ${field.placeholder} ${field.arialabel} ${field.autocomplete}`)].join(\" \").toLowerCase();\n const signup = signupmarkers.filter(marker => corpus.includes(marker));\n const checkout = checkoutmarkers.filter(marker => corpus.includes(marker));\n if (signup.length >= 2 && signup.length >= checkout.length) return { template: \"signup\", markers: signup };\n if (checkout.length >= 2) return { template: \"checkout\", markers: checkout };\n return { template: \"unknown\", markers: [...signup, ...checkout] };\n}\n\n/** Associates validation messages with fields through aria describedby refs and the sibling text next to each field. */\nexport function associateerrors(contexts: errorcontext[], messages: Array<{ id?: string; text: string }>): fielderror[] {\n const errors: fielderror[] = [];\n for (const field of contexts) {\n const byref = field.describedby ? messages.find(message => message.id === field.describedby && message.text.trim()) : undefined;\n if (byref) { errors.push({ field: field.selector, message: byref.text.trim() }); continue; }\n const sibling = field.siblings.map(text => text.trim()).find(text => text.length > 0);\n if (sibling) errors.push({ field: field.selector, message: sibling });\n }\n return errors;\n}\n\n/** Resolves one reviewed artifact name against the run store before a file input is filled. */\nexport function attachplan(name: string, artifacts: Array<{ id: string; name: string; kind: string }>): { artifact?: { id: string; name: string; kind: string }; reason?: string } {\n const artifact = artifacts.find(item => item.name === name || item.id === name);\n if (!artifact) return { reason: \"The reviewed artifact name is not part of the run store.\" };\n return { artifact };\n}\n\n/** Selectors the captcha detector probes; a hit hands control back to the user instead of forcing the page. */\nexport const captchamarkers = ['iframe[src*=\"recaptcha\"]', 'iframe[title*=\"recaptcha\" i]', '.g-recaptcha', '[data-sitekey]', 'iframe[title*=\"captcha\" i]', '.h-captcha'];\n\n/** True when any captcha marker matched, so the plan pauses and hands control to the user. */\nexport function captchadetected(matched: string[]): boolean {\n return matched.length > 0;\n}\n\nfunction events(target: Element): void {\n target.dispatchEvent(new Event(\"input\", { bubbles: true }));\n target.dispatchEvent(new Event(\"change\", { bubbles: true }));\n}\n\n/** Fills one control through the native setter with input and change events; checks, radios and selects use their own grammar. */\nexport function fillcontrol(element: Element, entry: formentry): boolean {\n if (element instanceof HTMLInputElement && (entry.kind === \"check\" || element.type === \"checkbox\")) { element.checked = entry.value === \"true\" || entry.value === \"on\" || entry.value === \"checked\"; events(element); return true; }\n if (element instanceof HTMLInputElement && (entry.kind === \"radio\" || element.type === \"radio\")) { element.checked = true; events(element); return true; }\n if (element instanceof HTMLSelectElement) {\n const option = [...element.options].find(candidate => candidate.value === entry.value || candidate.textContent?.trim() === entry.value);\n if (!option) return false;\n element.value = option.value;\n events(element);\n return true;\n }\n if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement) {\n if (element instanceof HTMLInputElement && element.type === \"file\") return false;\n element.focus();\n const setter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(element), \"value\")?.set;\n if (setter) setter.call(element, entry.value); else element.value = entry.value;\n events(element);\n return true;\n }\n return false;\n}\n\nfunction controlshape(element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement): fieldshape {\n return {\n selector: selector(element),\n tag: element.tagName.toLowerCase(),\n type: element instanceof HTMLSelectElement ? \"select\" : element.getAttribute(\"type\") || \"text\",\n name: element.getAttribute(\"name\") || \"\",\n label: label(element),\n placeholder: element.getAttribute(\"placeholder\") || \"\",\n arialabel: element.getAttribute(\"aria-label\") || \"\",\n autocomplete: element.getAttribute(\"autocomplete\") || \"\",\n ...(element instanceof HTMLSelectElement ? { options: [...element.options].map(option => option.value) } : {}),\n };\n}\n\n/** Collects the serializable field shapes of one form scope; absent scopes survey the whole document. */\nfunction collectfields(root: Document, formscope?: string): Array<{ shape: fieldshape; element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement }> {\n const scope = formscope ? root.querySelector(formscope) : root;\n if (!scope) return [];\n const controls = [...scope.querySelectorAll<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement>(\"input, select, textarea\")];\n return controls.filter(element => element.type !== \"hidden\").map(element => ({ shape: controlshape(element), element }));\n}\n\n/** Surveys the visibility and geometry evidence the honeypot detector reads for one form scope. */\nfunction surveyfields(root: Document, formscope?: string): Array<{ survey: fieldsurvey; element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement }> {\n const viewport = { left: 0, top: 0, right: window.innerWidth || 0, bottom: window.innerHeight || 0 };\n return collectfields(root, formscope).map(({ shape, element }) => {\n const rect = element.getBoundingClientRect();\n const hidden = element.getAttribute(\"aria-hidden\") === \"true\" || element.tabIndex < 0 && (element as HTMLElement).offsetParent === null || (element as HTMLElement).offsetParent === null && rect.width === 0 && rect.height === 0;\n const offscreen = rect.width > 0 && rect.height > 0 && (rect.bottom < viewport.top || rect.top > viewport.bottom || rect.right < viewport.left || rect.left > viewport.right);\n return { survey: { ...shape, hidden, offscreen }, element };\n });\n}\n\n/** Reads the error context of one form scope: describedby refs and the sibling texts after each field. */\nfunction collecterrorcontext(root: Document, formscope?: string): Array<{ context: errorcontext; element: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement }> {\n return collectfields(root, formscope).map(({ shape, element }) => {\n const siblings: string[] = [];\n let neighbor = element.nextElementSibling;\n for (let index = 0; neighbor && index < 3; index += 1) {\n const text = clean(neighbor.textContent || \"\");\n if (text && text !== shape.label) siblings.push(text);\n neighbor = neighbor.nextElementSibling;\n }\n const describedby = element.getAttribute(\"aria-describedby\");\n return { context: { ...shape, ...(describedby ? { describedby } : {}), siblings }, element };\n });\n}\n\n/** Runs one reviewed forms and data step inside the page: fills, surveys, detects and reads errors without leaving the form scope. */\nexport function runpageform(step: toolstep, target: Element | null, root: Document = document): stepresult | Promise<stepresult> {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const formscope = typeof options.form === \"string\" && options.form ? options.form : step.target;\n switch (step.kind) {\n case \"fillform\": {\n const record = parseformrecord(options.formrecord);\n if (!record) return { ok: false, summary: \"A reviewed form record with entries is required in options.\" };\n const surveys = surveyfields(root, record.form);\n const honeypots = detecthoneypots(surveys.map(entry => entry.survey), 0);\n const operations = filloperations(record, surveys.map(entry => entry.survey), honeypots.map(trap => trap.selector));\n let filled = 0;\n const skipped: string[] = [];\n const failures: string[] = [];\n const values: Array<{ label: string; value: string }> = [];\n for (const operation of operations) {\n if (operation.skipped && operation.matched) { skipped.push(operation.matched.selector); continue; }\n if (!operation.matched) { failures.push(`${operation.reason}: ${operation.entry.match.label ?? operation.entry.match.name ?? operation.entry.match.placeholder ?? \"field\"}`); continue; }\n const element = surveys.find(entry => entry.survey.selector === operation.matched?.selector)?.element;\n if (!element || !fillcontrol(element, operation.entry)) { failures.push(`unfillable: ${operation.matched.selector}`); continue; }\n filled += 1;\n values.push({ label: operation.matched.label || operation.matched.name, value: operation.entry.kind === \"password\" ? \"\" : operation.entry.value });\n }\n const report: formreport = { form: record.form ?? \"\", fields: operations.map(operation => ({ selector: operation.matched?.selector ?? \"\", label: operation.matched?.label ?? operation.entry.match.label ?? \"\", kind: operation.entry.kind, matched: Boolean(operation.matched) })) };\n return {\n ok: failures.length === 0,\n summary: failures.length === 0 ? `Filled ${filled} reviewed field${filled === 1 ? \"\" : \"s\"} from the structured record${skipped.length > 0 ? ` and skipped ${skipped.length} honeypot field${skipped.length === 1 ? \"\" : \"s\"}` : \"\"}.` : `Filled ${filled} of ${record.entries.length} reviewed fields; ${failures.length} refusals: ${failures.join(\"; \")}.`,\n details: { filled, skipped, failures, values, report },\n };\n }\n case \"filllabel\":\n case \"fillplaceholder\": {\n const mode = step.kind === \"filllabel\" ? \"label\" : \"placeholder\";\n const pairs = Array.isArray(options.fields) ? (options.fields as Array<Record<string, unknown>>).filter(item => item && typeof item === \"object\") : [];\n const record = pairentries(pairs.map(pair => ({ label: typeof pair.label === \"string\" ? pair.label : \"\", placeholder: typeof pair.placeholder === \"string\" ? pair.placeholder : \"\", value: typeof pair.value === \"string\" ? pair.value : \"\" })), mode);\n if (record.entries.length === 0) return { ok: false, summary: \"A reviewed non-empty list of field pairs is required in options.\" };\n const surveys = surveyfields(root, formscope);\n const honeypots = detecthoneypots(surveys.map(entry => entry.survey), 0);\n const operations = filloperations(record, surveys.map(entry => entry.survey), honeypots.map(trap => trap.selector));\n let filled = 0;\n const failures: string[] = [];\n for (const operation of operations) {\n if (operation.skipped) continue;\n if (!operation.matched) { failures.push(`${operation.reason}: ${mode === \"label\" ? operation.entry.match.label : operation.entry.match.placeholder}`); continue; }\n const element = surveys.find(entry => entry.survey.selector === operation.matched?.selector)?.element;\n const refined: formentry = { ...operation.entry, kind: classifyfield(operation.matched) };\n if (!element || !fillcontrol(element, refined)) { failures.push(`unfillable: ${operation.matched.selector}`); continue; }\n filled += 1;\n }\n return { ok: failures.length === 0, summary: failures.length === 0 ? `Filled ${filled} field${filled === 1 ? \"\" : \"s\"} matched by ${mode}.` : `Filled ${filled} of ${record.entries.length} fields matched by ${mode}; ${failures.join(\"; \")}.`, details: { filled, failures, mode } };\n }\n case \"detectfields\": {\n const collected = collectfields(root, formscope);\n const report: formreport = { form: formscope ?? \"\", fields: collected.map(entry => ({ selector: entry.shape.selector, label: entry.shape.label || entry.shape.name, kind: classifyfield(entry.shape), matched: Boolean(entry.shape.label || entry.shape.name) })) };\n return { ok: true, summary: `Detected ${collected.length} form field${collected.length === 1 ? \"\" : \"s\"} with their kinds.`, details: { report, count: collected.length } };\n }\n case \"generatevalues\": {\n const rule = options.valuegen && typeof options.valuegen === \"object\" && !Array.isArray(options.valuegen) ? options.valuegen as Record<string, unknown> : {};\n const locale = typeof rule.locale === \"string\" ? rule.locale : \"en\";\n const seed = typeof rule.seed === \"number\" && Number.isFinite(rule.seed) ? rule.seed : 1;\n const surveys = surveyfields(root, formscope);\n const honeypots = detecthoneypots(surveys.map(entry => entry.survey), 0);\n const skippedselectors = new Set(honeypots.map(trap => trap.selector));\n const candidates = surveys.filter(entry => !skippedselectors.has(entry.survey.selector));\n const values = candidates.map(entry => ({ label: entry.survey.label || entry.survey.name || entry.survey.selector, kind: classifyfield(entry.survey), value: generatevalue(classifyfield(entry.survey), { locale, seed }) }));\n const single = values.length === 0 && typeof rule.kind === \"string\" ? [{ label: rule.kind, kind: rule.kind, value: generatevalue(rule.kind as fieldkind, { locale, seed }) }] : values;\n return { ok: true, summary: `Generated ${single.length} realistic value${single.length === 1 ? \"\" : \"s\"} for the detected field kinds.`, details: { values: single, locale, seed } };\n }\n case \"readerrors\": {\n const contexts = collecterrorcontext(root, formscope);\n const messages = [...root.querySelectorAll<HTMLElement>(\"[id]\")].map(element => ({ id: element.id, text: clean(element.textContent || \"\") })).filter(message => message.text.length > 0);\n const errors = associateerrors(contexts.map(entry => entry.context), messages);\n return { ok: true, summary: errors.length === 0 ? \"No validation error was found next to the reviewed fields.\" : `Collected ${errors.length} inline validation message${errors.length === 1 ? \"\" : \"s\"}.`, details: { errors, form: formscope ?? \"\" } };\n }\n case \"skiphoneypot\": {\n const surveys = surveyfields(root, formscope);\n const traps = detecthoneypots(surveys.map(entry => entry.survey), 0);\n return { ok: true, summary: traps.length === 0 ? \"No honeypot field was detected.\" : `Skipped ${traps.length} honeypot field${traps.length === 1 ? \"\" : \"s\"}: ${traps.map(trap => `${trap.selector} (${trap.reason})`).join(\", \")}.`, details: { skipped: traps } };\n }\n case \"detectlogin\": {\n const collected = collectfields(root, formscope);\n const links = [...(formscope ? root.querySelectorAll(formscope) : [root] as unknown as Element[])].flatMap(scope => [...scope.querySelectorAll(\"a[href], button\")]).map(element => clean(element.textContent || \"\"));\n const detection = detectlogin(collected.map(entry => entry.shape), links);\n return { ok: true, summary: detection.login ? `Login form detected with ${detection.markers.join(\", \")}.` : \"No login form was detected.\", details: { login: detection.login, markers: detection.markers } };\n }\n case \"detecttemplate\": {\n const collected = collectfields(root, formscope);\n const text = clean(root.body?.innerText || \"\");\n const detection = detecttemplate(collected.map(entry => entry.shape), text);\n return { ok: true, summary: detection.template === \"unknown\" ? \"No signup or checkout template was detected.\" : `${detection.template} template detected with markers ${detection.markers.join(\", \")}.`, details: { template: detection.template, markers: detection.markers } };\n }\n case \"handoffcaptcha\": {\n const matched = captchamarkers.filter(marker => root.querySelector(marker) !== null);\n return { ok: true, summary: captchadetected(matched) ? `Captcha presence detected (${matched.join(\", \")}); control hands back to the user.` : \"No captcha was detected.\", details: { captcha: captchadetected(matched), markers: matched } };\n }\n case \"asksubmit\": {\n const collected = collectfields(root, step.value || undefined);\n const values = collected.map(entry => ({ label: entry.shape.label || entry.shape.name || entry.shape.selector, value: entry.element instanceof HTMLSelectElement ? entry.element.value : (entry.element as HTMLInputElement).value }));\n return { ok: true, summary: `Read ${values.length} field value${values.length === 1 ? \"\" : \"s\"} for the submission review.`, details: { values } };\n }\n case \"submitform\": {\n const form = target instanceof HTMLFormElement ? target : target instanceof HTMLElement ? target.closest(\"form\") : null;\n if (!form) return { ok: false, summary: \"No owning form was found for the reviewed submission.\" };\n form.requestSubmit();\n return { ok: true, summary: \"Form submitted programmatically through its owning form.\" };\n }\n case \"consentpassword\": {\n if (!(target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement)) return { ok: false, summary: \"The reviewed password target cannot receive text.\" };\n const entry: formentry = { match: { mode: \"name\", name: target.name || target.getAttribute(\"id\") || \"\" }, kind: \"password\", value: step.value ?? \"\" };\n if (!fillcontrol(target, entry)) return { ok: false, summary: \"The password field refused the native setter fill.\" };\n return { ok: true, summary: \"Password field filled after the reviewed consent; the value never appears in the audit trail.\" };\n }\n case \"attachfile\": {\n if (!(target instanceof HTMLInputElement) || target.type !== \"file\") return { ok: false, summary: \"The reviewed target is not a file input.\" };\n const artifactname = typeof options.artifactname === \"string\" && options.artifactname ? options.artifactname : \"artifact\";\n try {\n const file = new File([new Blob([\"devthink artifact\"], { type: \"application/octet-stream\" })], artifactname);\n const transfer = new DataTransfer();\n transfer.items.add(file);\n target.files = transfer.files;\n events(target);\n return { ok: true, summary: `Artifact ${artifactname} attached to the reviewed file input.`, details: { artifact: options.artifact, artifactname } };\n } catch {\n return { ok: false, summary: \"The reviewed file input refused the artifact attachment.\" };\n }\n }\n default: return { ok: false, summary: \"Unsupported forms and data action.\" };\n }\n}\n", "import type { toolstep, wizardstate } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { cardmask } from \"./pageforms.js\";\nimport { clean } from \"./pageresolve.js\";\n\n/**\n * Wizard, dependent control and payment field logics for reviewed steps.\n * Every correlated rule for wizard tracking, dependent option waits, typeahead picks, calendar navigation, card segment typing, one time code sources and submission retry backoff lives in this file.\n */\n\n/** Advances one wizard step, recording the completion signal of the executed step and the new step index. */\nexport function wizardadvance(state: wizardstate, completed: boolean, at: number): wizardstate {\n const flags = [...state.completed];\n while (flags.length < state.index + 1) flags.push(false);\n flags[state.index] = completed;\n return { ...state, index: Math.min(state.index + 1, state.steps), completed: flags, at };\n}\n\n/** True when the dependent child options finished loading after a parent selection changed their count. */\nexport function dependentloaded(previouscount: number, currentcount: number): boolean {\n return currentcount !== previouscount;\n}\n\n/** Picks the reviewed suggestion entry from a typeahead list, case insensitive; undefined when the entry is absent. */\nexport function typeaheadpick(suggestions: string[], pick: string): string | undefined {\n const needle = pick.trim().toLowerCase();\n return suggestions.find(suggestion => suggestion.trim().toLowerCase() === needle);\n}\n\n/** Parses one reviewed yyyy-mm-dd date into its year, month and day parts; null when the form is invalid. */\nexport function parsedateparts(value: string): { year: number; month: number; day: number } | null {\n if (!/^\\d{4}-\\d{2}-\\d{2}$/.test(value)) return null;\n const parts = value.split(\"-\").map(part => Number.parseInt(part, 10));\n const year = parts[0] ?? 0;\n const month = parts[1] ?? 0;\n const day = parts[2] ?? 0;\n if (month < 1 || month > 12 || day < 1 || day > 31) return null;\n return { year, month, day };\n}\n\n/** Plans the calendar navigation to the reviewed date: the month delta and the day cell to click. */\nexport function calendarplan(view: { year: number; month: number }, date: { year: number; month: number; day: number }): { months: number; day: number } {\n return { months: (date.year - view.year) * 12 + (date.month - view.month), day: date.day };\n}\n\n/** Splits one card number into its typed groups so the filler pauses between groups. */\nexport function cardgroups(number: string): string[] {\n const groups = number.replace(/[-\\s]+/g, \" \").trim().split(\" \");\n return groups.filter(group => group.length > 0);\n}\n\n/** True when the reviewed one time code source is ready to deliver the code before typing. */\nexport function codeready(source: string, value: string | undefined): boolean {\n if (source === \"reviewed\") return typeof value === \"string\" && value.trim().length > 0;\n return false;\n}\n\n/** One reviewed retry backoff rule with an attempt count, a wait window and a growth factor, all user configured. */\nexport interface backoffrule {\n attempts: number;\n wait: number;\n factor: number;\n}\n\n/** Parses the reviewed backoff rule of a retryform step; null when the step reviews none. */\nexport function parsebackoff(step: toolstep): backoffrule | null {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n const backoff = options.backoff;\n if (!backoff || typeof backoff !== \"object\" || Array.isArray(backoff)) return null;\n const rule = backoff as Record<string, unknown>;\n const wait = rule.wait;\n const factor = rule.factor;\n if (typeof wait !== \"number\" || !Number.isFinite(wait) || wait <= 0) return null;\n if (typeof factor !== \"number\" || !Number.isFinite(factor) || factor < 1) return null;\n const attempts = typeof options.attempts === \"number\" && Number.isInteger(options.attempts) && options.attempts >= 1 ? options.attempts : 2;\n return { attempts, wait, factor };\n}\n\n/** Computes the reviewed backoff windows between submission retries; no code ceiling applies. */\nexport function backoffwaits(attempts: number, wait: number, factor: number): number[] {\n const windows: number[] = [];\n let current = wait;\n for (let index = 1; index < attempts; index += 1) {\n windows.push(current);\n current *= factor;\n }\n return windows;\n}\n\nfunction wait(delay: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, delay));\n}\n\nfunction pollfor(predicate: () => boolean, description: string, timeout: number): Promise<stepresult> {\n return new Promise(resolve => {\n const started = Date.now();\n const check = (): void => {\n if (predicate()) { resolve({ ok: true, summary: `${description} is now present on the page.` }); return; }\n if (timeout > 0 && Date.now() - started >= timeout) { resolve({ ok: false, summary: `${description} did not appear within ${timeout} milliseconds.` }); return; }\n window.setTimeout(check, 100);\n };\n check();\n });\n}\n\n/** Runs one reviewed wizard or payment field step inside the page, from multi step wizards to card segments and one time codes. */\nexport function runpagewizard(step: toolstep, target: Element | null, root: Document = document): stepresult | Promise<stepresult> {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n switch (step.kind) {\n case \"runwizard\": {\n const scope = target instanceof HTMLElement ? target : root.body ?? root.documentElement;\n const steps = typeof options.steps === \"number\" && Number.isInteger(options.steps) && options.steps > 0 ? options.steps : 1;\n const state: wizardstate = { index: 0, steps, completed: [], at: Date.now() };\n const next = scope.querySelector<HTMLElement>(\"button[type=submit], button[name=next], [data-next]\");\n if (!next) return { ok: false, summary: \"No next step control was found inside the wizard scope.\", details: { wizard: state } };\n next.click();\n const advanced = wizardadvance(state, true, Date.now());\n return { ok: advanced.index >= steps, summary: `Wizard advanced to step ${advanced.index + 1} of ${steps}${advanced.index >= steps ? \" and completed\" : \"\"}.`, details: { wizard: advanced } };\n }\n case \"selectchain\": {\n if (!(target instanceof HTMLSelectElement)) return { ok: false, summary: \"The reviewed parent target is not a select element.\" };\n const childselector = typeof options.child === \"string\" ? options.child : \"\";\n const child = root.querySelector<HTMLSelectElement>(childselector);\n if (!child) return { ok: false, summary: \"The reviewed dependent child control was not found.\" };\n const previouscount = child.options.length;\n const option = [...target.options].find(candidate => candidate.value === step.value || candidate.textContent?.trim() === step.value);\n if (!option) return { ok: false, summary: \"The reviewed parent option is not part of the select element.\" };\n target.value = option.value;\n target.dispatchEvent(new Event(\"input\", { bubbles: true }));\n target.dispatchEvent(new Event(\"change\", { bubbles: true }));\n const waitwindow = typeof options.wait === \"number\" && options.wait > 0 ? options.wait : 0;\n return pollfor(() => dependentloaded(previouscount, child.options.length), \"Dependent options of the child control\", waitwindow);\n }\n case \"picktypeahead\": {\n const field = target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement ? target : null;\n if (!field) return { ok: false, summary: \"The reviewed typeahead target cannot receive text.\" };\n const pick = typeof options.pick === \"string\" ? options.pick : \"\";\n const timeout = typeof options.timeout === \"number\" && options.timeout > 0 ? options.timeout : 0;\n field.focus();\n field.value = step.value ?? \"\";\n field.dispatchEvent(new Event(\"input\", { bubbles: true }));\n const list = root.querySelector<HTMLElement>('[role=listbox], .suggestions, ul.autocomplete, [data-typeahead]');\n const suggestions = list ? [...list.querySelectorAll<HTMLElement>(\"[role=option], li, .suggestion\")].map(entry => clean(entry.textContent || \"\")) : [];\n const chosen = typeaheadpick(suggestions, pick);\n if (chosen === undefined) return { ok: false, summary: `The reviewed suggestion \"${pick}\" is not part of the typeahead list.`, details: { suggestions } };\n const entry = [...(list?.querySelectorAll<HTMLElement>(\"[role=option], li, .suggestion\") ?? [])].find(item => clean(item.textContent || \"\").trim().toLowerCase() === chosen.trim().toLowerCase());\n entry?.click();\n field.dispatchEvent(new Event(\"change\", { bubbles: true }));\n return { ok: Boolean(entry), summary: `Picked the reviewed typeahead entry \"${chosen}\".`, details: { pick: chosen, query: step.value ?? \"\" } };\n }\n case \"pickdate\": {\n const calendar = target instanceof HTMLElement ? target : root.body ?? root.documentElement;\n const date = parsedateparts(step.value ?? \"\");\n if (!date) return { ok: false, summary: \"The reviewed date must use the yyyy-mm-dd form.\" };\n const header = clean(calendar.querySelector<HTMLElement>(\"[data-calendar-title], .calendar-title, header\")?.textContent || \"\");\n const match = /(\\d{4})/.exec(header);\n const monthnames = [\"january\", \"february\", \"march\", \"april\", \"may\", \"june\", \"july\", \"august\", \"september\", \"october\", \"november\", \"december\"];\n const viewyear = match ? Number.parseInt(match[1] ?? \"0\", 10) : new Date().getFullYear();\n const viewmonth = monthnames.findIndex(name => header.toLowerCase().includes(name)) >= 0 ? monthnames.findIndex(name => header.toLowerCase().includes(name)) : new Date().getMonth() + 1;\n const plan = calendarplan({ year: viewyear, month: viewmonth }, date);\n const forward = plan.months >= 0;\n for (let index = 0; index < Math.abs(plan.months); index += 1) {\n calendar.querySelector<HTMLElement>(forward ? \"[data-next-month], .next-month, [aria-label=next]\" : \"[data-prev-month], .prev-month, [aria-label=previous]\")?.click();\n }\n const day = [...calendar.querySelectorAll<HTMLElement>(\"[role=gridcell], [data-day], td\")].find(cell => Number.parseInt(clean(cell.textContent || \"\"), 10) === plan.day);\n if (!day) return { ok: false, summary: `The reviewed day cell ${plan.day} was not found in the calendar widget.` };\n day.click();\n return { ok: true, summary: `Picked ${step.value} from the calendar widget after ${Math.abs(plan.months)} month navigation${Math.abs(plan.months) === 1 ? \"\" : \"s\"}.`, details: { months: plan.months, day: plan.day } };\n }\n case \"fillcard\": {\n const segments = Array.isArray(options.segments) ? (options.segments as Array<Record<string, unknown>>).filter(item => item && typeof item === \"object\") : [];\n if (segments.length === 0) return { ok: false, summary: \"A reviewed non-empty list of card segments is required in options.\" };\n const pause = typeof options.pause === \"number\" && options.pause > 0 ? options.pause : 0;\n const filled: Array<{ label: string; masked: string }> = [];\n const failures: string[] = [];\n const fillsegment = async (segment: Record<string, unknown>): Promise<void> => {\n const match = segment.match as Record<string, unknown> | undefined;\n const value = typeof segment.value === \"string\" ? segment.value : \"\";\n if (!match || typeof match !== \"object\") { failures.push(\"segment without a reviewed match\"); return; }\n const scope = root.body ?? root.documentElement;\n const candidates = [...scope.querySelectorAll<HTMLInputElement>(\"input, select\")];\n const key = match.mode === \"label\" ? \"label\" : match.mode === \"placeholder\" ? \"placeholder\" : match.mode === \"arialabel\" ? \"arialabel\" : \"name\";\n const needle = String(match[key] ?? \"\").toLowerCase();\n const element = candidates.find(input => (key === \"label\" ? input.name.toLowerCase() || input.getAttribute(\"aria-label\")?.toLowerCase() || \"\" : (input.getAttribute(key) ?? input.name).toLowerCase()).includes(needle) || (input.getAttribute(\"aria-label\") ?? \"\").toLowerCase().includes(needle));\n if (!element) { failures.push(`unmatched: ${needle}`); return; }\n element.focus();\n for (const group of cardgroups(value)) {\n element.value = group;\n element.dispatchEvent(new Event(\"input\", { bubbles: true }));\n if (pause > 0) await wait(pause);\n }\n element.dispatchEvent(new Event(\"change\", { bubbles: true }));\n filled.push({ label: String(match[key] ?? \"\"), masked: cardmask(value) });\n };\n return (async (): Promise<stepresult> => {\n for (const segment of segments) await fillsegment(segment);\n return {\n ok: failures.length === 0,\n summary: failures.length === 0 ? `Filled ${filled.length} card segment${filled.length === 1 ? \"\" : \"s\"} with pauses between card number groups.` : `Filled ${filled.length} of ${segments.length} card segments; ${failures.join(\"; \")}.`,\n details: { segments: filled, failures, pause },\n };\n })();\n }\n case \"fillcode\": {\n const field = target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement ? target : null;\n if (!field) return { ok: false, summary: \"The reviewed one time code target cannot receive text.\" };\n const source = typeof options.source === \"string\" ? options.source : \"\";\n const timeout = typeof options.timeout === \"number\" && options.timeout > 0 ? options.timeout : 0;\n const code = step.value ?? \"\";\n return pollfor(() => codeready(source, code), \"The reviewed one time code source\", timeout).then(result => {\n if (!codeready(source, code)) return { ok: false, summary: `The reviewed code source ${source} is not ready to deliver the code yet.` };\n field.focus();\n field.value = code;\n field.dispatchEvent(new Event(\"input\", { bubbles: true }));\n field.dispatchEvent(new Event(\"change\", { bubbles: true }));\n return { ok: true, summary: `One time code typed from the reviewed source ${source}.`, details: { source } };\n });\n }\n default: return { ok: false, summary: \"Unsupported wizard action.\" };\n }\n}\n", "import type { columnspec, datasetrow, sourceref, toolstep, transformrule } from \"../types.js\";\nimport { parseoptions } from \"../policy.js\";\nimport type { stepresult } from \"./pageactions.js\";\nimport { clean, elementselector as selector } from \"./pageresolve.js\";\n\n/**\n * Table and dataset logics for reviewed steps.\n * Every correlated rule for header normalization, span expansion, nested table walking, pagination following, row hashing, transform expressions, dataset merging, row sampling and csv, json and excel serialization lives in this file.\n */\n\n/** One raw table cell with its text, header flag, span geometry and an optional nested table. */\nexport interface cellshape {\n text: string;\n header: boolean;\n rowspan: number;\n colspan: number;\n nested?: { selector: string; rows: rowshape[] };\n}\n\n/** One raw table row of span carrying cells. */\nexport interface rowshape {\n cells: cellshape[];\n header: boolean;\n}\n\n/** One nested child table linked to the body row it belongs to. */\nexport interface childtable {\n parentrow: number;\n selector: string;\n columns: columnspec[];\n rows: datasetrow[];\n}\n\n/** Result of the table reader: normalized column specs, keyed body rows and child datasets. */\nexport interface gridresult {\n columns: columnspec[];\n rows: datasetrow[];\n children: childtable[];\n}\n\n/** Trims, lowercases and slugifies one header cell into a stable column key. */\nexport function normalizeheader(label: string): string {\n const slug = label.trim().toLowerCase().replace(/[^\\p{L}\\p{N}]+/gu, \"-\").replace(/^-+|-+$/g, \"\");\n return slug || \"column\";\n}\n\n/** Builds one column spec from a header label with a unique stable key and the normalized name. */\nexport function columnspecof(label: string, used: Set<string> = new Set()): columnspec {\n const base = normalizeheader(label);\n let key = base;\n let suffix = 2;\n while (used.has(key)) {\n key = `${base}${suffix}`;\n suffix += 1;\n }\n used.add(key);\n return { key, label: label.trim(), kind: \"text\", normalized: label.trim().toLowerCase() };\n}\n\n/** Classifies a column as number only when every non-empty value parses as a number. */\nexport function classifycolumn(values: string[]): \"text\" | \"number\" {\n const present = values.filter(value => value.trim().length > 0);\n if (present.length === 0) return \"text\";\n return present.every(value => Number.isFinite(Number(value.replace(/,/g, \".\")))) ? \"number\" : \"text\";\n}\n\n/** Expands rowspan and colspan cells into a filled rectangular grid of values. */\nexport function expandspans(rows: rowshape[]): string[][] {\n const filled: Array<Array<string | undefined>> = [];\n const pending = new Map<string, string>();\n for (let index = 0; index < rows.length; index += 1) {\n const row: Array<string | undefined> = filled[index] ?? (filled[index] = []);\n let column = 0;\n for (const cell of rows[index]!.cells) {\n while (row[column] !== undefined || pending.has(`${index},${column}`)) {\n if (row[column] === undefined) row[column] = pending.get(`${index},${column}`);\n column += 1;\n }\n row[column] = cell.text;\n for (let spanrow = 0; spanrow < Math.max(1, cell.rowspan); spanrow += 1) {\n for (let spancol = 0; spancol < Math.max(1, cell.colspan); spancol += 1) {\n if (spanrow === 0 && spancol === 0) continue;\n pending.set(`${index + spanrow},${column + spancol}`, cell.text);\n }\n }\n column += Math.max(1, cell.colspan);\n }\n }\n for (const [key, value] of pending) {\n const [rowpart, columnpart] = key.split(\",\");\n const rowindex = Number.parseInt(rowpart ?? \"0\", 10);\n const columnindex = Number.parseInt(columnpart ?? \"0\", 10);\n const target = filled[rowindex] ?? (filled[rowindex] = []);\n if (target[columnindex] === undefined) target[columnindex] = value;\n }\n const width = filled.reduce((largest, row) => Math.max(largest, row.length), 0);\n return filled.map(row => Array.from({ length: width }, (_, column) => row[column] ?? \"\"));\n}\n\n/** Reads header and body rows into normalized column specs and keyed body rows, extracting nested tables into child datasets. */\nexport function readgrid(rows: rowshape[]): gridresult {\n const headerindex = rows.findIndex(row => row.header || (row.cells[0]?.header ?? false));\n const useheader = headerindex !== -1 ? headerindex : 0;\n const hasheader = headerindex !== -1 || rows.length > 0;\n const grid = expandspans(rows);\n const headercells = hasheader ? (grid[useheader] ?? []) : [];\n const used = new Set<string>();\n const columns = Array.from({ length: headercells.length || (grid[0]?.length ?? 0) }, (_, index) => columnspecof(headercells[index] ?? `column${index + 1}`, used));\n const bodyrows = grid.filter((_, index) => hasheader ? index !== useheader : true)\n .filter(line => line.some(value => value.trim().length > 0))\n .map(line => {\n const row: datasetrow = {};\n columns.forEach((column, index) => { row[column.key] = line[index] ?? \"\"; });\n return row;\n });\n for (const column of columns) column.kind = classifycolumn(bodyrows.map(row => row[column.key] ?? \"\"));\n const children: childtable[] = [];\n rows.forEach((row, rowindex) => {\n if (hasheader && rowindex === useheader) return;\n const parentrow = hasheader && rowindex > useheader ? rowindex - 1 : rowindex;\n row.cells.forEach(cell => {\n if (!cell.nested) return;\n const child = readgrid(cell.nested.rows);\n children.push({ parentrow, selector: cell.nested!.selector, columns: child.columns, rows: child.rows });\n });\n });\n return { columns, rows: bodyrows, children };\n}\n\n/** Resolves the next pagination control from numbered page entries: the entry after the current one or a next word control. */\nexport function nextcontrol(entries: Array<{ text: string; selector: string; current: boolean }>): string | undefined {\n const current = entries.findIndex(entry => entry.current);\n if (current !== -1 && current + 1 < entries.length) return entries[current + 1]?.selector;\n const nextwords = [\"next\", \"next page\", \">\", \">>\", \"\u203A\", \"\u00BB\", \"pr\u00F3xima\", \"seguinte\"];\n return entries.find(entry => nextwords.includes(entry.text.trim().toLowerCase()))?.selector;\n}\n\n/** True when the current row set contains at least one row the previous set did not carry. */\nexport function rowsfresh(previous: datasetrow[], current: datasetrow[]): boolean {\n if (current.length === 0) return false;\n const known = new Set(previous.map(row => JSON.stringify(row)));\n return current.some(row => !known.has(JSON.stringify(row)));\n}\n\n/** Computes a stable row hash over the reviewed keys; an empty key list hashes every column. */\nexport function rowhash(row: datasetrow, keys: string[]): string {\n const source = (keys.length > 0 ? keys : Object.keys(row).sort()).map(key => `${key}=${row[key] ?? \"\"}`).join(\"|\");\n let hash = 5381;\n for (let index = 0; index < source.length; index += 1) hash = ((hash * 33) ^ source.charCodeAt(index)) >>> 0;\n return hash.toString(16);\n}\n\n/** Deduplicates rows by reviewed keys, keeping the first occurrence of every key set. */\nexport function dedupebykeys(rows: datasetrow[], keys: string[]): { kept: datasetrow[]; removed: number } {\n const seen = new Set<string>();\n const kept: datasetrow[] = [];\n for (const row of rows) {\n const hash = rowhash(row, keys);\n if (seen.has(hash)) continue;\n seen.add(hash);\n kept.push(row);\n }\n return { kept, removed: rows.length - kept.length };\n}\n\n/** Applies one reviewed transform expression to a value; unsupported expressions are refused. */\nexport function applyexpression(value: string, expression: string): string {\n const split = expression.indexOf(\":\");\n const op = split === -1 ? expression : expression.slice(0, split);\n const argument = split === -1 ? undefined : expression.slice(split + 1);\n if (op === \"trim\") return value.trim();\n if (op === \"upper\") return value.toUpperCase();\n if (op === \"lower\") return value.toLowerCase();\n if (op === \"number\") return value.replace(/[^\\d.\\-]/g, \"\");\n if (op === \"prefix\") return `${argument ?? \"\"}${value}`;\n if (op === \"suffix\") return `${value}${argument ?? \"\"}`;\n if (op === \"replace\") {\n const separator = argument?.indexOf(\"=>\") ?? -1;\n if (separator === -1 || separator === 0) throw new Error(`The reviewed transform expression ${expression} needs the from=>to separator.`);\n const from = argument!.slice(0, separator);\n const to = argument!.slice(separator + 2);\n return value.split(from).join(to);\n }\n throw new Error(`The reviewed transform expression ${op} is not supported.`);\n}\n\n/** Applies reviewed transform rules to dataset rows, surfacing per rule errors and keeping the original values on failure. */\nexport function transformrows(rows: datasetrow[], rules: transformrule[]): { rows: datasetrow[]; errors: string[] } {\n const errors: string[] = [];\n const output = rows.map(row => ({ ...row }));\n for (const rule of rules) {\n const updated: datasetrow[] = [];\n try {\n for (const row of output) updated.push({ ...row, [rule.target]: applyexpression(rule.sources.map(source => row[source] ?? \"\").join(\" \"), rule.expression) });\n } catch (error) {\n errors.push(`${rule.target}: ${error instanceof Error ? error.message : String(error)}`);\n continue;\n }\n output.splice(0, output.length, ...updated);\n }\n return { rows: output, errors };\n}\n\n/** Merges datasets across pages: columns align by key with gaps filled empty and rows concatenate in page order. */\nexport function mergedatasets(datasets: Array<{ columns: columnspec[]; rows: datasetrow[] }>): { columns: columnspec[]; rows: datasetrow[] } {\n const columns: columnspec[] = [];\n const seen = new Set<string>();\n for (const dataset of datasets) {\n for (const column of dataset.columns) {\n if (seen.has(column.key)) continue;\n seen.add(column.key);\n columns.push(column);\n }\n }\n const rows = datasets.flatMap(dataset => dataset.rows.map(row => {\n const merged: datasetrow = {};\n for (const column of columns) merged[column.key] = row[column.key] ?? \"\";\n return merged;\n }));\n return { columns, rows };\n}\n\n/** Attaches the source url, timestamp and step ref to every row and returns the matching source refs. */\nexport function samplerows(rows: datasetrow[], url: string, stepid: string, at: number): { rows: datasetrow[]; sources: sourceref[] } {\n const stamped = rows.map(row => ({ ...row, source: url, capturedat: String(at), step: stepid }));\n const sources = stamped.map((row, index) => ({ row: index, url, at, stepid }));\n return { rows: stamped, sources };\n}\n\n/** Escapes one csv field, wrapping values that carry the delimiter, quotes or line breaks. */\nfunction csvfield(value: string, delimiter: string): string {\n return value.includes(delimiter) || value.includes(\"\\\"\") || value.includes(\"\\n\") ? `\"${value.replace(/\"/g, \"\\\"\\\"\")}\"` : value;\n}\n\n/** Serializes columns and rows into csv text. */\nexport function tocsv(columns: columnspec[], rows: datasetrow[], delimiter = \",\"): string {\n const lines = [columns.map(column => csvfield(column.label || column.key, delimiter)).join(delimiter)];\n for (const row of rows) lines.push(columns.map(column => csvfield(row[column.key] ?? \"\", delimiter)).join(delimiter));\n return lines.join(\"\\n\");\n}\n\n/** Parses csv text into headers and raw rows, honoring quoted fields and escaped quotes. */\nexport function parsecsv(text: string, delimiter = \",\"): { headers: string[]; rows: string[][] } {\n const records: string[][] = [];\n let field = \"\";\n let record: string[] = [];\n let quoted = false;\n for (let index = 0; index < text.length; index += 1) {\n const character = text[index]!;\n if (quoted) {\n if (character === \"\\\"\") {\n if (text[index + 1] === \"\\\"\") { field += \"\\\"\"; index += 1; }\n else quoted = false;\n } else field += character;\n continue;\n }\n if (character === \"\\\"\") { quoted = true; continue; }\n if (character === delimiter) { record.push(field); field = \"\"; continue; }\n if (character === \"\\n\" || character === \"\\r\") {\n if (character === \"\\r\" && text[index + 1] === \"\\n\") index += 1;\n record.push(field);\n field = \"\";\n if (record.some(value => value.length > 0) || record.length > 1) records.push(record);\n record = [];\n continue;\n }\n field += character;\n }\n record.push(field);\n if (record.some(value => value.length > 0) || record.length > 1) records.push(record);\n const [headers = [], ...rows] = records;\n return { headers, rows };\n}\n\n/** Maps parsed csv headers onto dataset column specs through a reviewed mapping of csv names to target keys. */\nexport function mapcolumns(headers: string[], mapping: Record<string, string> = {}): columnspec[] {\n const used = new Set<string>();\n return headers.map(header => {\n const target = mapping[header] ?? mapping[normalizeheader(header)] ?? header;\n return columnspecof(target, used);\n });\n}\n\n/** Serializes columns and rows into a json dataset payload. */\nexport function tojson(columns: columnspec[], rows: datasetrow[]): string {\n return JSON.stringify({ columns, rows });\n}\n\n/** Escapes one xml text node. */\nfunction xmltext(value: string): string {\n return value.replace(/&/g, \"&amp;\").replace(/</g, \"&lt;\").replace(/>/g, \"&gt;\").replace(/\"/g, \"&quot;\");\n}\n\n/** Serializes columns and rows into an Excel SpreadsheetML 2003 workbook that Excel opens natively. */\nexport function toexcel(columns: columnspec[], rows: datasetrow[], name: string): string {\n const head = columns.map(column => `<Cell ss:StyleID=\"head\"><Data ss:Type=\"String\">${xmltext(column.label || column.key)}</Data></Cell>`).join(\"\");\n const body = rows.map(row => `<Row>${columns.map(column => {\n const value = row[column.key] ?? \"\";\n const numeric = column.kind === \"number\" && value.trim() !== \"\" && Number.isFinite(Number(value));\n return numeric ? `<Cell><Data ss:Type=\"Number\">${xmltext(value)}</Data></Cell>` : `<Cell><Data ss:Type=\"String\">${xmltext(value)}</Data></Cell>`;\n }).join(\"\")}</Row>`).join(\"\");\n return `<?xml version=\"1.0\"?><?mso-application progid=\"Excel.Sheet\"?><Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"><Styles><Style ss:ID=\"head\"><Font ss:Bold=\"1\"/></Style></Styles><Worksheet ss:Name=\"${xmltext(name || \"dataset\").slice(0, 31)}\"><Table><Row>${head}</Row>${body}</Table></Worksheet></Workbook>`;\n}\n\n/** Collects the raw span carrying row shapes of one html table, separating nested tables into cell refs. */\nfunction collectrowshapes(table: HTMLTableElement): rowshape[] {\n return [...table.rows].map(row => ({\n header: [...row.cells].every(cell => cell.tagName === \"TH\") && row.cells.length > 0,\n cells: [...row.cells].map(cell => {\n const nested = cell.querySelector(\"table\");\n return {\n text: clean(nested ? `${nested.rows.length} rows` : cell.textContent ?? \"\"),\n header: cell.tagName === \"TH\",\n rowspan: cell.rowSpan,\n colspan: cell.colSpan,\n ...(nested instanceof HTMLTableElement ? { nested: { selector: selector(nested), rows: collectrowshapes(nested) } } : {}),\n };\n }),\n }));\n}\n\n/** Reads the reviewed table element into a grid result. */\nfunction readtable(target: Element | null, root: Document, fallbackselector: string | undefined): gridresult | null {\n const table = target instanceof HTMLTableElement ? target : (fallbackselector ? root.querySelector<HTMLTableElement>(fallbackselector) : root.querySelector<HTMLTableElement>(\"table\"));\n if (!table) return null;\n return readgrid(collectrowshapes(table));\n}\n\n/** Reads the live pagination entries of a table container for the next control resolution. */\nfunction paginationentries(root: Document, scope: string | undefined): Array<{ text: string; selector: string; current: boolean }> {\n const container = scope ? root.querySelector(scope) : root;\n if (!container) return [];\n return [...container.querySelectorAll(\"a[href], button\")].map(element => ({\n text: clean(element.textContent ?? \"\"),\n selector: selector(element),\n current: element.getAttribute(\"aria-current\") === \"page\" || element.classList.contains(\"active\") || element.classList.contains(\"current\"),\n }));\n}\n\n/** Runs one reviewed data step on the page: table scraping into datasets and pagination following with fresh row waits. */\nexport async function runpagedata(step: toolstep, target: Element | null, root: Document = document): Promise<stepresult> {\n let options: Record<string, unknown> = {};\n try { options = parseoptions(step); } catch { options = {}; }\n if (step.kind === \"scrapetable\") {\n const grid = readtable(target, root, step.target);\n if (!grid) return { ok: false, summary: \"The reviewed table selector matches no table element.\" };\n const rowlimit = typeof options.rowlimit === \"number\" && Number.isInteger(options.rowlimit) && options.rowlimit > 0 ? options.rowlimit : grid.rows.length;\n const limited = grid.rows.slice(0, rowlimit);\n return {\n ok: true,\n summary: `Scraped ${limited.length} row${limited.length === 1 ? \"\" : \"s\"} into ${grid.columns.length} normalized column${grid.columns.length === 1 ? \"\" : \"s\"}${grid.children.length > 0 ? ` with ${grid.children.length} nested child table${grid.children.length === 1 ? \"\" : \"s\"}` : \"\"}.`,\n details: { grid: { columns: grid.columns, rows: limited, children: grid.children }, rows: limited.length, columns: grid.columns.length },\n };\n }\n if (step.kind === \"paginateextract\") {\n const nextselector = typeof options.next === \"string\" ? options.next : \"\";\n const pages = typeof options.pages === \"number\" && Number.isInteger(options.pages) && options.pages > 0 ? options.pages : 1;\n const wait = typeof options.wait === \"number\" && Number.isFinite(options.wait) && options.wait > 0 ? options.wait : 0;\n const cursor = typeof options.cursor === \"number\" && Number.isInteger(options.cursor) && options.cursor > 0 ? options.cursor : 0;\n const grids: gridresult[] = [];\n let previous: datasetrow[] = [];\n for (let page = 0; page < pages + cursor; page += 1) {\n if (page < cursor) {\n const control = root.querySelector<HTMLElement>(nextselector);\n if (!control) break;\n control.click();\n await new Promise(resolve => window.setTimeout(resolve, 0));\n continue;\n }\n if (page > cursor) {\n const control = root.querySelector<HTMLElement>(nextselector);\n if (!control) break;\n control.click();\n const deadline = Date.now() + wait;\n let fresh = false;\n while (!fresh && Date.now() < deadline) {\n await new Promise(resolve => window.setTimeout(resolve, Math.min(100, Math.max(16, deadline - Date.now()))));\n const probe = readtable(target, root, step.target);\n fresh = probe !== null && rowsfresh(previous, probe.rows);\n }\n }\n const grid = readtable(target, root, step.target);\n if (!grid) return { ok: false, summary: \"The reviewed table selector matches no table element.\" };\n grids.push(grid);\n previous = grid.rows;\n }\n const merged = mergedatasets(grids);\n const hasnext = Boolean(root.querySelector(nextselector));\n return {\n ok: grids.length > 0,\n summary: grids.length > 0 ? `Followed ${grids.length} page${grids.length === 1 ? \"\" : \"s\"} of the reviewed table into ${merged.rows.length} row${merged.rows.length === 1 ? \"\" : \"s\"}${hasnext ? \"; a next control remains\" : \"\"}.` : \"No page of the reviewed table was extracted.\",\n details: { grid: { columns: merged.columns, rows: merged.rows, children: grids.flatMap(grid => grid.children) }, pages: grids.length, rows: merged.rows.length, next: hasnext },\n };\n }\n return { ok: false, summary: \"Unsupported data step.\" };\n}\n\n/** Exposes the pagination entries of the current document so plan review can suggest next controls. */\nexport function pagepagination(root: Document = document): Array<{ text: string; selector: string; current: boolean }> {\n return paginationentries(root, undefined);\n}\n", "import type { artifactrecord, columnspec, dataset, datasetrow, exportedartifact, extractsession, provenancerecord, streamstate, toolstep, transformrule } from \"../types.js\";\nimport { tocsv, toexcel, tojson } from \"./pagedata.js\";\n\n/**\n * Dataset command logics for the background executors.\n * Every correlated rule for dataset records, artifact exports with checksums, chunked streaming with backpressure, extraction cursors, loop row variables, provenance records and artifact retention lives in this file.\n */\n\n/** Builds one dataset record from a scraped grid result. */\nexport function builddataset(id: string, name: string, grid: { columns: columnspec[]; rows: datasetrow[]; children?: Array<{ parentrow: number; selector: string; columns: columnspec[]; rows: datasetrow[] }> }, at: number): dataset {\n return { id, name: name || id, columns: grid.columns, rows: grid.rows, sources: [], at };\n}\n\n/** Computes the deterministic checksum of an exported artifact's content. */\nexport function checksum(value: string): string {\n let hash = 5381;\n for (let index = 0; index < value.length; index += 1) hash = ((hash * 33) ^ value.charCodeAt(index)) >>> 0;\n return `fnv1a-${hash.toString(16)}`;\n}\n\n/** Serializes one dataset into the reviewed export format. */\nexport function exportcontent(datasetvalue: dataset, format: \"csv\" | \"json\" | \"excel\", delimiter = \",\"): string {\n if (format === \"json\") return tojson(datasetvalue.columns, datasetvalue.rows);\n if (format === \"excel\") return toexcel(datasetvalue.columns, datasetvalue.rows, datasetvalue.name);\n return tocsv(datasetvalue.columns, datasetvalue.rows, delimiter);\n}\n\n/** Builds one exported artifact record with its content and checksum for the task artifact store. */\nexport function exportartifact(id: string, datasetvalue: dataset, format: \"csv\" | \"json\" | \"excel\", stepid: string, content: string, at: number): exportedartifact {\n const extension = format === \"excel\" ? \"xml\" : format;\n return { id, kind: format, name: `${datasetvalue.name || datasetvalue.id}.${extension}`, stepid, rowcount: datasetvalue.rows.length, content, checksum: checksum(content), at };\n}\n\n/** Converts one exported artifact into the artifact record shape the run store keeps. */\nexport function artifactrecordof(artifact: exportedartifact): artifactrecord {\n return { id: artifact.id, kind: artifact.kind, name: artifact.name, stepid: artifact.stepid, at: artifact.at };\n}\n\n/** Plans the chunk boundaries of a streaming export from a user configured chunk size with no code ceiling. */\nexport function chunkplan(rows: number, chunk: number): Array<{ index: number; from: number; to: number }> {\n const size = Math.max(1, Math.floor(chunk));\n const chunks: Array<{ index: number; from: number; to: number }> = [];\n for (let from = 0; from < rows || chunks.length === 0; from += size) {\n const to = Math.min(rows, from + size);\n chunks.push({ index: chunks.length, from, to });\n if (to >= rows) break;\n }\n return chunks;\n}\n\n/** True when the stream writer must wait for acknowledgements: pending writes reached the in-flight budget of one. */\nexport function backpressure(written: number, acknowledged: number): boolean {\n return written - acknowledged >= 1;\n}\n\n/** Advances one stream state by one acknowledged chunk of rows. */\nexport function advancestream(state: streamstate, chunk: { index: number; to: number }, at: number, done: boolean): streamstate {\n return { datasetid: state.datasetid, name: state.name, chunk: chunk.index + 1, chunks: state.chunks, written: chunk.to, ...(done ? { done: true } : {}), at };\n}\n\n/** Returns the first unwritten row index of a stream, starting a fresh stream at zero. */\nexport function streamfrom(state: streamstate | undefined, rows: number): number {\n if (!state || state.done) return 0;\n return Math.min(state.written, rows);\n}\n\n/** Builds the initial stream state of one dataset. */\nexport function newstream(datasetvalue: dataset, chunks: number, at: number): streamstate {\n return { datasetid: datasetvalue.id, name: datasetvalue.name, chunk: 0, chunks, written: 0, at };\n}\n\n/** Advances one extraction session by one extracted page with its row count. */\nexport function advancecursor(sessionvalue: extractsession, page: string, rows: number, at: number, done: boolean): extractsession {\n return {\n id: sessionvalue.id,\n datasetid: sessionvalue.datasetid,\n name: sessionvalue.name,\n target: sessionvalue.target,\n next: sessionvalue.next,\n planned: sessionvalue.planned,\n pages: [...sessionvalue.pages, page],\n rows: sessionvalue.rows + rows,\n cursor: sessionvalue.cursor + 1,\n ...(done || sessionvalue.cursor + 1 >= sessionvalue.planned ? { done: true } : {}),\n startedat: sessionvalue.startedat,\n updatedat: at,\n };\n}\n\n/** Builds the initial extraction session of one dataset extraction. */\nexport function newextractsession(id: string, datasetid: string, name: string, target: string, next: string, planned: number, at: number): extractsession {\n return { id, datasetid, name, target, next, planned, pages: [], rows: 0, cursor: 0, startedat: at, updatedat: at };\n}\n\n/** Returns the pages an interrupted extraction still owes after its stored cursor. */\nexport function remainingpages(sessionvalue: extractsession, planned: number): number {\n if (sessionvalue.done) return 0;\n return Math.max(0, Math.max(sessionvalue.planned, planned) - sessionvalue.cursor);\n}\n\n/** Builds one provenance record of an exported artifact with its source url, step ref, row range and checksum. */\nexport function provenancefor(artifact: { id: string; name: string; rowcount: number; checksum: string }, url: string, stepid: string, at: number): provenancerecord {\n return { artifact: artifact.id, name: artifact.name, url, stepid, rowstart: artifact.rowcount > 0 ? 1 : 0, rowend: artifact.rowcount, checksum: artifact.checksum, at };\n}\n\n/** Applies the user configured artifact retention to exported artifacts; an absent setting keeps everything. */\nexport function retainedexports<T>(records: T[], retention: number | undefined): T[] {\n return retention === undefined ? records : records.slice(0, retention);\n}\n\n/** Interpolates one text through the {{column}} tokens of a dataset row. */\nexport function interpolate(text: string, row: datasetrow): string {\n return text.replace(/\\{\\{([^}]+)\\}\\}/g, (_, key: string) => row[key.trim()] ?? \"\");\n}\n\n/** Substitutes the row variables of one looprows iteration into the target, value and options of the inner step. */\nexport function loopstep(step: toolstep, row: datasetrow): toolstep {\n return {\n ...step,\n ...(step.target !== undefined ? { target: interpolate(step.target, row) } : {}),\n ...(step.value !== undefined ? { value: interpolate(step.value, row) } : {}),\n ...(step.options !== undefined ? { options: interpolate(step.options, row) } : {}),\n };\n}\n\n/** Exposes one dataset row as the step variables of a looprows iteration. */\nexport function loopvariables(row: datasetrow): datasetrow {\n return { ...row };\n}\n\n/** Builds the grid preview of a dataset with its column order, total rows and sampled rows. */\nexport function gridpreview(datasetvalue: dataset, sample: number): { datasetid: string; columns: string[]; rows: number; sample: datasetrow[] } {\n return { datasetid: datasetvalue.id, columns: datasetvalue.columns.map(column => column.key), rows: datasetvalue.rows.length, sample: datasetvalue.rows.slice(0, Math.max(0, Math.floor(sample))) };\n}\n\n/** Sorts dataset rows by one column key in the reviewed direction with a stable fallback for equal values. */\nexport function sortrows(rows: datasetrow[], key: string, direction: \"asc\" | \"desc\"): datasetrow[] {\n const sign = direction === \"desc\" ? -1 : 1;\n return [...rows].sort((left, right) => {\n const a = left[key] ?? \"\";\n const b = right[key] ?? \"\";\n const numeric = Number(a);\n const numericb = Number(b);\n if (Number.isFinite(numeric) && Number.isFinite(numericb) && a.trim() !== \"\" && b.trim() !== \"\") return (numeric - numericb) * sign;\n return a.localeCompare(b) * sign;\n });\n}\n\n/** Builds the sheet push payload of one dataset for a reviewed sheet endpoint. */\nexport function sheetpayload(datasetvalue: dataset, sheet: string): { sheet: string; columns: string[]; rows: datasetrow[] } {\n return { sheet, columns: datasetvalue.columns.map(column => column.key), rows: datasetvalue.rows };\n}\n\n/** Merges reviewed transform rules and dedupe keys into the task rules record of one task. */\nexport function mergetaskrules(existing: { taskid: string; transforms: transformrule[]; dedupekeys: string[] } | undefined, taskid: string, transforms: transformrule[], dedupekeys: string[], at: number): { taskid: string; transforms: transformrule[]; dedupekeys: string[]; at: number } {\n return {\n taskid,\n transforms: transforms.length > 0 ? transforms : (existing?.transforms ?? []),\n dedupekeys: dedupekeys.length > 0 ? dedupekeys : (existing?.dedupekeys ?? []),\n at,\n };\n}\n", "import type { observation, regionrect, resolvedtarget, toolstep } from \"../types.js\";\nimport { runpageaction, type stepresult } from \"./pageactions.js\";\nimport { runpageread } from \"./pagereads.js\";\nimport { runpagecontrol } from \"./pagecontrols.js\";\nimport { runinteractstep } from \"./pageinteract.js\";\nimport { runpointerstep } from \"./pagepointer.js\";\nimport { runpagenav } from \"./pagenav.js\";\nimport { harvestdialoglog, type observeddialog } from \"./pagedialogs.js\";\nimport { builda11ytree, buildpagetree, buildreader, runpageobservation } from \"./pageobserve.js\";\nimport { collecttables, detectlistpatterns, collectsiblings, normalizetable, runpagedetection } from \"./pagedetect.js\";\nimport { runpagewatch } from \"./pagewatch.js\";\nimport { runcdpstep, rundebugwatch } from \"./pagedebug.js\";\nimport { runprofilestep } from \"./pageprofile.js\";\nimport { runemulationstep, revertemulationlayer } from \"./pageemulate.js\";\nimport { runpageform } from \"./pageforms.js\";\nimport { runpagewizard } from \"./pagewizards.js\";\nimport { runpagedata } from \"./pagedata.js\";\nimport { checksum } from \"./datacommand.js\";\nimport { clean, elementlabel as label, elementselector as selector, resolvestep } from \"./pageresolve.js\";\n\n/**\n * Page bridge for reviewed steps.\n * Correlated rules for the dispatch seam, semantic snapshots, target preview with resolution, dialog log reads and the original action set live in this file.\n */\n\nfunction stepoptions(step: toolstep): Record<string, unknown> {\n if (!step.options) return {};\n try {\n const parsed = JSON.parse(step.options);\n return parsed && typeof parsed === \"object\" && !Array.isArray(parsed) ? (parsed as Record<string, unknown>) : {};\n } catch { return {}; }\n}\n\nconst previewid = \"devthinktargetpreview\";\n\nfunction clearpreview(): void {\n document.getElementById(previewid)?.remove();\n}\n\n/** Shows an ephemeral outline only; it neither mutates page data nor dispatches page events. */\nexport function previewtarget(step: toolstep, expectedorigin: string): { ok: boolean; summary: string; resolvedtarget?: resolvedtarget; candidates?: string[] } {\n if (location.origin !== expectedorigin) return { ok: false, summary: \"Page origin changed before preview.\" };\n clearpreview();\n const resolution = resolvestep(step, document);\n if (resolution.status === \"ambiguous\") return { ok: false, summary: `The reviewed ${resolution.mode} reference matched ${resolution.candidates.length} elements: ${resolution.candidates.join(\"; \")}.`, candidates: resolution.candidates };\n if (resolution.status !== \"resolved\") return { ok: false, summary: \"Reviewed target is no longer available.\" };\n const target = resolution.element;\n const rect = target.getBoundingClientRect();\n if (rect.width <= 0 || rect.height <= 0) return { ok: false, summary: \"Reviewed target is not currently visible.\" };\n const overlay = document.createElement(\"div\");\n overlay.id = previewid;\n overlay.setAttribute(\"aria-hidden\", \"true\");\n Object.assign(overlay.style, { position: \"fixed\", left: `${Math.max(0, rect.left - 3)}px`, top: `${Math.max(0, rect.top - 3)}px`, width: `${rect.width + 6}px`, height: `${rect.height + 6}px`, border: \"3px solid #2f80ed\", borderRadius: \"6px\", boxShadow: \"0 0 0 3px rgba(47,128,237,.28)\", pointerEvents: \"none\", zIndex: \"2147483647\", boxSizing: \"border-box\" });\n document.documentElement.append(overlay);\n window.setTimeout(clearpreview, 5000);\n return { ok: true, summary: `Previewing ${label(target) || target.tagName.toLowerCase()} for five seconds.`, resolvedtarget: resolution.target };\n}\n\n/** Captures the complete semantic page context for a user-approved active tab, including the a11y, reader, listpattern and tableshape observation sections. */\nexport function capturesnapshot(): observation {\n const candidates = [...document.querySelectorAll(\"a[href], button, input, textarea, select, [role=button], [role=link], [role=combobox], [role=option], [role=checkbox], [role=radio], [role=switch], [role=tab], details, summary\")];\n const interactive = candidates.map(element => ({ selector: selector(element), role: element.getAttribute(\"role\") || element.tagName.toLowerCase(), label: label(element) })).filter(item => item.label || item.role);\n const forms = [...document.querySelectorAll(\"input, textarea, select\")].map(element => ({\n label: label(element),\n type: element.getAttribute(\"type\") || element.tagName.toLowerCase(),\n name: element.getAttribute(\"name\") || \"\",\n ...(element instanceof HTMLSelectElement ? { options: [...element.options].map(option => clean(option.textContent || option.value)) } : {}),\n }));\n const text = clean(document.body?.innerText || \"\");\n const tree = buildpagetree(document);\n const tables = collecttables(document).map(entry => {\n const shape = normalizetable(entry.rows, entry.caption);\n return { selector: entry.selector, headers: shape.headers, columns: shape.columns, rows: shape.rows, caption: shape.caption };\n });\n return {\n schemaversion: 3,\n url: location.href,\n title: clean(document.title),\n textpreview: text,\n textlength: document.body?.innerText.length ?? 0,\n forms,\n interactive,\n capturedat: Date.now(),\n mode: \"passive\",\n a11y: builda11ytree(tree),\n reader: buildreader(tree, clean(document.title)),\n listpattern: detectlistpatterns(collectsiblings(document)),\n tableshape: tables,\n };\n}\n\n/** Reads and clears the dialog log the main world handler recorded on the shared document. */\nexport function readdialogs(): observeddialog[] {\n return harvestdialoglog(document);\n}\n\n/** Extracts full, read-only structured content for a reviewed extraction step. */\nfunction extractcontent(targetselector: string | undefined, root: Document): stepresult {\n if (!targetselector) {\n const links = [...root.querySelectorAll(\"a[href]\")].map(element => {\n const href = element instanceof HTMLAnchorElement ? element.getAttribute(\"href\") ?? \"\" : \"\";\n return { text: element.textContent?.trim() ?? \"\", href };\n });\n return { ok: true, summary: `Extracted ${links.length} link entries.`, details: { links } };\n }\n const target = root.querySelector(targetselector);\n if (!target) return { ok: false, summary: \"Extraction target is no longer available.\" };\n const text = target.textContent ?? \"\";\n return { ok: true, summary: `Extracted ${text.length} characters of content.`, details: { text } };\n}\n\n/** Scrolls one reviewed target into view without reading or changing other page state. */\nfunction scrolltarget(target: HTMLElement): stepresult {\n target.scrollIntoView({ block: \"center\", inline: \"nearest\", behavior: \"auto\" });\n return { ok: true, summary: `Scrolled ${label(target) || target.tagName.toLowerCase()} into view.` };\n}\n\n/** Dispatches hover events on one reviewed target. */\nfunction hovertarget(target: HTMLElement): stepresult {\n for (const type of [\"pointerover\", \"mouseover\", \"pointerenter\"] as const) {\n target.dispatchEvent(new PointerEvent(type, { bubbles: type !== \"pointerenter\", cancelable: true, composed: true }));\n }\n target.dispatchEvent(new MouseEvent(\"mouseenter\", { bubbles: false, cancelable: true }));\n return { ok: true, summary: `Hover events delivered to ${label(target) || target.tagName.toLowerCase()}.` };\n}\n\n/** Selects one reviewed existing option; values outside the declared options are refused. */\nfunction selectoption(target: HTMLElement, value: string): stepresult {\n if (!(target instanceof HTMLSelectElement)) return { ok: false, summary: \"Target is not a select element.\" };\n const option = [...target.options].find(candidate => candidate.value === value || candidate.textContent?.trim() === value);\n if (!option) return { ok: false, summary: \"Reviewed option is not part of the select element.\" };\n target.value = option.value;\n target.dispatchEvent(new Event(\"input\", { bubbles: true }));\n target.dispatchEvent(new Event(\"change\", { bubbles: true }));\n return { ok: true, summary: `Selected ${clean(option.textContent || option.value)}.` };\n}\n\nconst readkinds: ReadonlySet<string> = new Set([\"readattribute\", \"readstyle\", \"readgeometry\", \"readvalue\", \"readtext\", \"readhtml\", \"countelements\", \"readtable\", \"readlinks\", \"readimages\", \"readmeta\", \"readforms\", \"readstorage\", \"waitfor\", \"waittext\", \"highlight\", \"mapclicks\", \"verifyvisible\", \"verifyenabled\", \"resolvexpath\"]);\nconst mutatingkinds: ReadonlySet<string> = new Set([\"presskey\", \"clickdeep\", \"rightclick\", \"doubleclick\", \"drag\", \"drop\", \"upload\", \"clear\", \"check\", \"uncheck\", \"toggle\", \"submit\", \"setattribute\", \"removeattribute\", \"writestorage\", \"evaluate\", \"fullscreen\"]);\nconst controlkinds: ReadonlySet<string> = new Set([\"typetime\", \"appendtext\", \"setvalue\", \"typeedit\", \"keyhold\", \"keyrelease\", \"submitsearch\", \"selectmulti\", \"chooseradio\", \"setslider\", \"setdate\", \"setcolor\", \"expanddetails\"]);\nconst interactkinds: ReadonlySet<string> = new Set([\"clicktext\", \"clickaria\", \"clickname\", \"pierceshadow\", \"enterframe\"]);\nconst pointerkinds: ReadonlySet<string> = new Set([\"movepointer\", \"clickpoint\", \"shiftclick\"]);\nconst observationkinds: ReadonlySet<string> = new Set([\"a11ytree\", \"readvisible\", \"readertree\", \"readoutline\", \"readselection\", \"readopengraph\", \"readlang\", \"detectlanguage\", \"listshadow\", \"listframes\"]);\nconst detectionkinds: ReadonlySet<string> = new Set([\"detectlists\", \"detecttables\", \"detectinfinitescroll\", \"detectvirtual\", \"detectlazy\", \"detectsticky\", \"detectscrolllock\", \"countpages\", \"classifypage\", \"fingerprintsection\", \"readscrollpos\"]);\nconst watchstepkinds: ReadonlySet<string> = new Set([\"watchmutate\", \"watchbanner\", \"watchfocus\", \"waitquiet\", \"readjson\", \"diffsnapshots\", \"deriveselector\"]);\nconst debugstepkinds: ReadonlySet<string> = new Set([\"watchconsole\", \"watcherrors\", \"watchtasks\"]);\nconst cdpstepkinds: ReadonlySet<string> = new Set([\"attachcdp\", \"detachcdp\", \"cdpcmd\", \"watchcdp\", \"setbreakpoint\", \"stepcode\", \"watchexpr\", \"overridescript\"]);\nconst profilestepkinds: ReadonlySet<string> = new Set([\"measureflow\", \"heapshot\", \"trackmemory\", \"profilecpu\", \"watchshifts\", \"traceload\", \"capturesourcemaps\"]);\nconst emulationstepkinds: ReadonlySet<string> = new Set([\"emulatedevice\", \"emulatenetwork\", \"emulatelocate\", \"setuseragent\", \"overridepermission\", \"blackboxscripts\"]);\nconst navstepkinds: ReadonlySet<string> = new Set([\"waitload\", \"waiturl\", \"followlink\", \"spanav\", \"spawait\", \"rewritequery\", \"setfragment\", \"stopnav\", \"prefetch\", \"preconnect\", \"printpdf\"]);\nconst formkinds: ReadonlySet<string> = new Set([\"fillform\", \"filllabel\", \"fillplaceholder\", \"detectfields\", \"generatevalues\", \"readerrors\", \"skiphoneypot\", \"detectlogin\", \"detecttemplate\", \"handoffcaptcha\", \"asksubmit\", \"submitform\", \"consentpassword\", \"attachfile\"]);\nconst wizardkinds: ReadonlySet<string> = new Set([\"runwizard\", \"selectchain\", \"picktypeahead\", \"pickdate\", \"fillcard\", \"fillcode\"]);\nconst datastepkinds: ReadonlySet<string> = new Set([\"scrapetable\", \"paginateextract\"]);\n\n/** Performs one local action after the background policy gate and a fresh target resolution. */\nexport async function performstep(step: toolstep, expectedorigin: string, rootdocument: Document = document): Promise<stepresult> {\n if (location.origin !== expectedorigin) return { ok: false, summary: \"Page origin changed before action.\" };\n if (step.kind === \"observe\") return { ok: true, summary: \"Observation completed.\" };\n if (step.kind === \"wait\") {\n const requested = step.value ? Number.parseInt(step.value, 10) : 250;\n const duration = Number.isFinite(requested) && requested > 0 ? requested : 0;\n return new Promise(resolve => window.setTimeout(() => resolve({ ok: true, summary: `Reviewed wait of ${duration} milliseconds completed.` }), duration));\n }\n if (step.kind === \"extract\") return extractcontent(step.target, rootdocument);\n if (step.kind === \"navigate\") {\n if (!step.value || new URL(step.value).origin !== expectedorigin) return { ok: false, summary: \"Navigation target is outside the approved origin.\" };\n location.assign(step.value);\n return { ok: true, summary: \"Navigation request sent.\" };\n }\n if (step.kind === \"reload\") { location.reload(); return { ok: true, summary: \"Page reload requested.\" }; }\n if (step.kind === \"back\") { history.back(); return { ok: true, summary: \"History back requested.\" }; }\n if (step.kind === \"forward\") { history.forward(); return { ok: true, summary: \"History forward requested.\" }; }\n if (navstepkinds.has(step.kind)) return await runpagenav(step, rootdocument);\n if (step.kind === \"writeclipboard\") {\n const text = step.value ?? \"\";\n await navigator.clipboard.writeText(text);\n return { ok: true, summary: `Wrote ${text.length} reviewed character${text.length === 1 ? \"\" : \"s\"} to the clipboard with payload hash ${checksum(text)}.`, details: { length: text.length, hash: checksum(text), destination: \"clipboard\" } };\n }\n if (step.kind === \"scrollpage\") {\n const options = stepoptions(step);\n window.scrollBy({ left: typeof options.x === \"number\" ? options.x : 0, top: typeof options.y === \"number\" ? options.y : 600, behavior: \"auto\" });\n return { ok: true, summary: \"Window scrolled by the reviewed amounts.\" };\n }\n if (step.kind === \"scrollend\") { window.scrollTo(0, document.documentElement.scrollHeight); return { ok: true, summary: \"Window scrolled to the page end.\" }; }\n if (step.kind === \"scrolltop\") { window.scrollTo(0, 0); return { ok: true, summary: \"Window scrolled to the page top.\" }; }\n const resolution = resolvestep(step, rootdocument);\n if (resolution.status === \"ambiguous\") {\n return { ok: false, summary: `The reviewed ${resolution.mode} reference matched ${resolution.candidates.length} elements: ${resolution.candidates.join(\"; \")}.`, details: { mode: resolution.mode, candidates: resolution.candidates } };\n }\n const element = resolution.status === \"resolved\" ? resolution.element : null;\n let result: stepresult | Promise<stepresult>;\n if (formkinds.has(step.kind)) result = runpageform(step, element, rootdocument);\n else if (wizardkinds.has(step.kind)) result = runpagewizard(step, element, rootdocument);\n else if (datastepkinds.has(step.kind)) result = runpagedata(step, element, rootdocument);\n else if (readkinds.has(step.kind)) result = runpageread(step, element, rootdocument);\n else if (controlkinds.has(step.kind)) result = runpagecontrol(step, element, rootdocument);\n else if (interactkinds.has(step.kind)) return await runinteractstep(step, expectedorigin, performstep);\n else if (pointerkinds.has(step.kind)) result = runpointerstep(step, resolution);\n else if (observationkinds.has(step.kind)) result = runpageobservation(step, element, rootdocument);\n else if (detectionkinds.has(step.kind)) result = runpagedetection(step, element, rootdocument);\n else if (watchstepkinds.has(step.kind)) result = runpagewatch(step, element, rootdocument);\n else if (debugstepkinds.has(step.kind)) return await rundebugwatch(step);\n else if (cdpstepkinds.has(step.kind)) return await runcdpstep(step);\n else if (profilestepkinds.has(step.kind)) return await runprofilestep(step);\n else if (emulationstepkinds.has(step.kind)) return await runemulationstep(step);\n else if (mutatingkinds.has(step.kind)) result = runpageaction(step, element);\n else {\n if (!element) return { ok: false, summary: \"Action target is no longer available.\" };\n if (step.kind === \"scrollby\") {\n const options = stepoptions(step);\n element.scrollBy({ left: typeof options.x === \"number\" ? options.x : 0, top: typeof options.y === \"number\" ? options.y : 600, behavior: \"auto\" });\n result = { ok: true, summary: \"Container scrolled by the reviewed amounts.\" };\n } else if (step.kind === \"focus\") { element.focus(); result = { ok: true, summary: \"Target focused.\" }; }\n else if (step.kind === \"inspect\") result = { ok: true, summary: `Target: ${label(element) || element.tagName.toLowerCase()}.` };\n else if (step.kind === \"click\") { element.click(); result = { ok: true, summary: \"Reviewed click completed.\" }; }\n else if (step.kind === \"scroll\") result = scrolltarget(element);\n else if (step.kind === \"hover\") result = hovertarget(element);\n else if (step.kind === \"select\") result = selectoption(element, step.value ?? \"\");\n else if (step.kind === \"type\") {\n if (!(element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement)) return { ok: false, summary: \"Target cannot receive text.\" };\n if (typeof step.value !== \"string\") return { ok: false, summary: \"Approved text is absent.\" };\n element.focus();\n element.value = step.value;\n element.dispatchEvent(new Event(\"input\", { bubbles: true }));\n element.dispatchEvent(new Event(\"change\", { bubbles: true }));\n result = { ok: true, summary: \"Approved text entered.\" };\n } else return { ok: false, summary: \"Unsupported action.\" };\n }\n const output = await result;\n if (resolution.status === \"resolved\") {\n return { ...output, details: { ...(output.details ?? {}), mode: resolution.target.mode, resolvedtarget: resolution.target } };\n }\n return output;\n}\n\n/** Measures the full scroll width and height before any stitching begins, beside the viewport geometry, the device pixel ratio and the current scroll position. */\nexport function measurepage(): { scrollwidth: number; scrollheight: number; viewportwidth: number; viewportheight: number; pixelratio: number; scrollx: number; scrolly: number } {\n const root = document.documentElement;\n return {\n scrollwidth: Math.max(root.scrollWidth, document.body?.scrollWidth ?? 0),\n scrollheight: Math.max(root.scrollHeight, document.body?.scrollHeight ?? 0),\n viewportwidth: window.innerWidth,\n viewportheight: window.innerHeight,\n pixelratio: window.devicePixelRatio || 1,\n scrollx: window.scrollX,\n scrolly: window.scrollY,\n };\n}\n\n/** Returns the pixel ratio scaled rect of one target element with the viewport crossing flag for the tiled fallback. */\nexport function elementrect(selector: string): { ok: boolean; rect?: regionrect; pixelratio?: number; crossesviewport?: boolean; summary: string } {\n const target = document.querySelector(selector);\n if (!target) return { ok: false, summary: \"The reviewed capture element is no longer available.\" };\n const bounds = target.getBoundingClientRect();\n if (bounds.width <= 0 || bounds.height <= 0) return { ok: false, summary: \"The reviewed capture element is not currently visible.\" };\n const viewport = { width: window.innerWidth, height: window.innerHeight };\n const rect: regionrect = { x: Math.round(bounds.left + window.scrollX), y: Math.round(bounds.top + window.scrollY), width: Math.round(bounds.width), height: Math.round(bounds.height) };\n const viewrect: regionrect = { x: bounds.left, y: bounds.top, width: bounds.width, height: bounds.height };\n return { ok: true, rect, pixelratio: window.devicePixelRatio || 1, crossesviewport: viewrect.x < 0 || viewrect.y < 0 || viewrect.x + viewrect.width > viewport.width || viewrect.y + viewrect.height > viewport.height, summary: `Measured the capture element at ${rect.width} by ${rect.height} css pixels.` };\n}\n\n/** Resolves the unique element references of one selector for the foreach executor of the workflow control engine: every matched element reports its stable selector without mutating the page. */\nexport function queryelements(query: string): { ok: boolean; selectors: string[]; summary: string } {\n const matched = [...document.querySelectorAll(query)];\n if (matched.length === 0) return { ok: true, selectors: [], summary: `The reviewed selector ${query} matched no element.` };\n return { ok: true, selectors: matched.map(element => selector(element)), summary: `Resolved ${matched.length} element reference${matched.length === 1 ? \"\" : \"s\"} of the reviewed selector ${query}.` };\n}\n\nconst scrollbarstyleid = \"devthinkcapturehider\";\n\n/** Prepares the page for capture: hides the capture scrollbars through a scoped style rule and returns the original scroll position. */\nexport function preparecapture(): { scrollx: number; scrolly: number } {\n if (!document.getElementById(scrollbarstyleid)) {\n const style = document.createElement(\"style\");\n style.id = scrollbarstyleid;\n style.setAttribute(\"aria-hidden\", \"true\");\n style.textContent = \"html::-webkit-scrollbar,body::-webkit-scrollbar{display:none!important}html{scrollbar-width:none!important}\";\n document.documentElement.append(style);\n }\n return { scrollx: window.scrollX, scrolly: window.scrollY };\n}\n\n/** Scrolls the page to one reviewed tile offset and reports the settled position. */\nexport function scrollcapture(x: number, y: number): { x: number; y: number } {\n window.scrollTo(x, y);\n return { x: window.scrollX, y: window.scrollY };\n}\n\n/** Restores the original scroll position after the last tile and removes the capture scrollbar hider. */\nexport function restorecapture(state: { scrollx: number; scrolly: number }): void {\n document.getElementById(scrollbarstyleid)?.remove();\n window.scrollTo(state.scrollx, state.scrolly);\n}\n\n/** Scrolls one scrollable container to a reviewed step top and reports its geometry. */\nexport function scrollcontainercapture(selector: string, top: number): { ok: boolean; top?: number; height?: number; viewportheight?: number; summary: string } {\n const container = document.querySelector(selector);\n if (!container) return { ok: false, summary: \"The reviewed scrollable container is no longer available.\" };\n if (!(container instanceof HTMLElement)) return { ok: false, summary: \"The reviewed scrollable container cannot scroll.\" };\n container.scrollTo({ top, behavior: \"auto\" });\n return { ok: true, top: container.scrollTop, height: container.scrollHeight, viewportheight: container.clientHeight, summary: `Scrolled the container to ${container.scrollTop} of ${container.scrollHeight} pixels.` };\n}\n\n/** Waits the reviewed settle time between capture tiles. */\nexport function waitsettle(milliseconds: number): Promise<void> {\n return new Promise(resolve => window.setTimeout(resolve, Math.max(0, milliseconds)));\n}\n\n\n/** Collects the visible text of one pdf report segment: every block element whose bounds intersect the reviewed vertical range contributes its text, so paginated reports split at reviewed break points. */\nexport function pdfsegment(top: number, height: number): { ok: boolean; text: string; summary: string } {\n const blocks = [...document.querySelectorAll(\"h1,h2,h3,h4,h5,h6,p,li,td,th,blockquote,pre,figcaption,section,article > div\")];\n const lines: string[] = [];\n for (const block of blocks) {\n const bounds = block.getBoundingClientRect();\n const blocktop = bounds.top + window.scrollY;\n const blockbottom = blocktop + bounds.height;\n if (blockbottom <= top || blocktop >= top + height) continue;\n const text = clean(block.textContent ?? \"\");\n if (text) lines.push(text);\n }\n const text = lines.join(\"\\n\");\n return { ok: true, text, summary: `Collected ${text.length} characters of the report segment at ${Math.round(top)} to ${Math.round(top + height)} pixels.` };\n}\n\n/** Resolves the scroll tops of the reviewed pdf break point selectors for paginated reports. */\nexport function pdfbreaks(selectors: string[]): Array<{ selector: string; top: number }> {\n const resolved: Array<{ selector: string; top: number }> = [];\n for (const selector of selectors) {\n const target = document.querySelector(selector);\n if (!target) continue;\n resolved.push({ selector, top: Math.round(target.getBoundingClientRect().top + window.scrollY) });\n }\n return resolved;\n}\n\n/** Grabs one still frame of a video element at the reviewed timestamp: the video seeks, pauses and draws to a canvas that encodes as an image; cross origin videos without cors refuse the draw honestly. */\nexport async function videoframe(selector: string, timestamp: number | undefined, poster: boolean): Promise<{ ok: boolean; dataurl?: string; width?: number; height?: number; summary: string }> {\n const target = document.querySelector(selector);\n if (!(target instanceof HTMLVideoElement)) return { ok: false, summary: \"The reviewed frame source is not a video element.\" };\n target.pause();\n if (typeof timestamp === \"number\" && Number.isFinite(timestamp) && timestamp >= 0 && timestamp <= (target.duration || 0)) {\n await new Promise<void>(resolve => {\n const done = (): void => resolve();\n target.addEventListener(\"seeked\", done, { once: true });\n target.currentTime = timestamp;\n window.setTimeout(done, 1500);\n });\n }\n const width = target.videoWidth || target.clientWidth || 1;\n const height = target.videoHeight || target.clientHeight || 1;\n try {\n const canvas = document.createElement(\"canvas\");\n canvas.width = width;\n canvas.height = height;\n const context = canvas.getContext(\"2d\");\n if (!context) return { ok: false, summary: \"The frame grab could not create a canvas context.\" };\n context.drawImage(target, 0, 0, width, height);\n const dataurl = canvas.toDataURL(\"image/png\");\n return { ok: dataurl.length > 100, dataurl, width, height, summary: `Grabbed the video frame at ${target.currentTime.toFixed(2)} seconds of ${width} by ${height} pixels${poster ? \" as the poster frame\" : \"\"}.` };\n } catch {\n return { ok: false, summary: \"The video frame draw was refused; cross origin videos need cors headers before frames can be read.\" };\n }\n}\n\n/** Reads the content of one canvas element: plain 2d canvases return their buffer directly and webgl canvases request the buffer through a preserved read; tainted canvases refuse honestly. */\nexport function canvasdata(selector: string): { ok: boolean; context: \"2d\" | \"webgl\"; dataurl?: string; width?: number; height?: number; preserved?: boolean; summary: string } {\n const target = document.querySelector(selector);\n if (!(target instanceof HTMLCanvasElement)) return { ok: false, context: \"2d\", summary: \"The reviewed canvas element is no longer available.\" };\n const width = target.width || 1;\n const height = target.height || 1;\n const kind: \"2d\" | \"webgl\" = target.getContext(\"2d\") ? \"2d\" : \"webgl\";\n try {\n let dataurl = \"\";\n let preserved = false;\n if (kind === \"2d\") {\n dataurl = target.toDataURL(\"image/png\");\n } else {\n dataurl = target.toDataURL(\"image/png\");\n if (dataurl.length <= 100) {\n const gl = (target.getContext(\"webgl\") ?? target.getContext(\"experimental-webgl\")) as WebGLRenderingContext | null;\n if (gl) {\n const pixels = new Uint8Array(width * height * 4);\n gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);\n const canvas = document.createElement(\"canvas\");\n canvas.width = width;\n canvas.height = height;\n const context = canvas.getContext(\"2d\");\n if (context) {\n const image = context.createImageData(width, height);\n for (let row = 0; row < height; row += 1) {\n const source = (height - 1 - row) * width * 4;\n const destination = row * width * 4;\n image.data.set(pixels.subarray(source, source + width * 4), destination);\n }\n context.putImageData(image, 0, 0);\n dataurl = canvas.toDataURL(\"image/png\");\n preserved = true;\n }\n }\n }\n }\n return { ok: dataurl.length > 100, context: kind, dataurl, width, height, preserved, summary: `Read the ${kind} canvas buffer of ${width} by ${height} pixels${kind === \"webgl\" ? preserved ? \" through a preserved readPixels pass\" : \" through the preserved drawing buffer\" : \"\"}.` };\n } catch {\n return { ok: false, context: kind, summary: \"The canvas read was refused; tainted canvas content needs cross origin resources with cors headers.\" };\n }\n}\n\n/** Probes the media streams of the page: every video and audio element with a srcObject reports its track kinds, labels, settings and live states; peer connection statistics stay outside the isolated world bridge. */\nexport function streamelements(selector?: string): Array<Record<string, unknown>> {\n const root = selector ? document.querySelector(selector) : document;\n if (!root) return [];\n const entries: Array<Record<string, unknown>> = [];\n for (const element of [...root.querySelectorAll(\"video, audio\")]) {\n const media = element instanceof HTMLMediaElement ? element : null;\n if (!media) continue;\n const stream = media.srcObject;\n if (!(stream instanceof MediaStream)) continue;\n entries.push({\n kind: \"webrtc\",\n label: stream.id,\n live: stream.active,\n tracks: stream.getTracks().map(track => ({\n kind: track.kind,\n label: track.label,\n state: track.readyState,\n ...(track.kind === \"video\" ? { width: track.getSettings().width, height: track.getSettings().height, framerate: track.getSettings().frameRate } : {}),\n })),\n });\n }\n return entries;\n}\n\n/** Reads the embedded video and audio sources of the page with their formats, durations, dimensions, codecs and track lists. */\nexport function mediaelements(): Array<Record<string, unknown>> {\n const entries: Array<Record<string, unknown>> = [];\n for (const element of [...document.querySelectorAll(\"video, audio\")]) {\n if (!(element instanceof HTMLMediaElement)) continue;\n const source = element.querySelector(\"source\");\n const url = element.currentSrc || element.src || (source instanceof HTMLSourceElement ? source.src : \"\") || \"\";\n if (!url) continue;\n const type = (source instanceof HTMLSourceElement ? source.type : \"\") || \"\";\n const codecs = type.includes(\"codecs=\") ? type.slice(type.indexOf(\"codecs=\") + \"codecs=\".length).replace(/[\"']/g, \"\") : \"\";\n entries.push({\n url,\n mime: type.split(\";\")[0] || \"\",\n duration: Number.isFinite(element.duration) ? element.duration : 0,\n width: element instanceof HTMLVideoElement ? element.videoWidth : 0,\n height: element instanceof HTMLVideoElement ? element.videoHeight : 0,\n codecs,\n tracks: [...element.textTracks].map(track => track.label || track.kind).filter(Boolean),\n });\n }\n return entries;\n}\n\n/** Collects the page assets: declared favicons and apple touch icons with their sizes, manifest declared icons and logo candidates from meta images and header imagery. */\nexport async function pageassets(): Promise<Array<Record<string, unknown>>> {\n const entries: Array<Record<string, unknown>> = [];\n for (const link of [...document.querySelectorAll(\"link[rel]\")]) {\n const rel = (link.getAttribute(\"rel\") ?? \"\").toLowerCase();\n const href = link.getAttribute(\"href\");\n if (!href || !(rel.includes(\"icon\") || rel.includes(\"apple-touch\"))) continue;\n const resolved = new URL(href, location.href).toString();\n entries.push({ kind: \"favicon\", url: resolved, bytes: 0, sizes: link.getAttribute(\"sizes\") ?? \"any\" });\n }\n const og = document.querySelector('meta[property=\"og:image\"]');\n if (og instanceof HTMLMetaElement && og.content) entries.push({ kind: \"logo\", url: new URL(og.content, location.href).toString(), bytes: 0, sizes: \"og\" });\n for (const image of [...document.querySelectorAll(\"header img, nav img, img[alt*=logo i], img[src*=logo i]\")]) {\n const url = image instanceof HTMLImageElement ? image.currentSrc || image.src : \"\";\n if (!url) continue;\n entries.push({ kind: \"logo\", url: new URL(url, location.href).toString(), bytes: 0, sizes: image instanceof HTMLImageElement ? `${image.naturalWidth}x${image.naturalHeight}` : \"\" });\n }\n const manifestlink = document.querySelector('link[rel=\"manifest\"]');\n if (manifestlink instanceof HTMLLinkElement && manifestlink.href) {\n try {\n const response = await fetch(manifestlink.href);\n const manifest = await response.json() as { icons?: Array<{ src?: string; sizes?: string }> };\n for (const icon of manifest.icons ?? []) {\n if (!icon.src) continue;\n entries.push({ kind: \"favicon\", url: new URL(icon.src, location.href).toString(), bytes: 0, sizes: icon.sizes ?? \"any\" });\n }\n } catch { /* a refused manifest fetch leaves the declared icons unreported */ }\n }\n return entries;\n}\n\n/** Detects every image of the page inside an optional selector scope: urls, alt text, natural dimensions, transfer sizes from the performance entries and mime types. */\nexport function pageimages(selector?: string): Array<Record<string, unknown>> {\n const root = selector ? document.querySelector(selector) : document;\n if (!root) return [];\n const transfers = new Map<string, number>();\n for (const entry of performance.getEntriesByType(\"resource\")) {\n const resource = entry as PerformanceResourceTiming;\n if (resource.transferSize > 0) transfers.set(resource.name, resource.transferSize);\n }\n const images: Array<Record<string, unknown>> = [];\n for (const element of [...root.querySelectorAll(\"img\")]) {\n if (!(element instanceof HTMLImageElement)) continue;\n const url = element.currentSrc || element.src;\n if (!url) continue;\n const resolved = new URL(url, location.href).toString();\n const type = element.getAttribute(\"type\") ?? \"\";\n images.push({\n url: resolved,\n alt: element.alt ?? \"\",\n width: element.naturalWidth || element.width,\n height: element.naturalHeight || element.height,\n bytes: transfers.get(resolved) ?? 0,\n mime: type || (element.src.startsWith(\"data:\") ? element.src.slice(5, element.src.indexOf(\";\")) : \"image/*\"),\n });\n }\n return images;\n}\n/** Parses fetched markup through the page domparser and runs the reviewed html queries: attribute values, text and element counts per query. */\nexport function parsehtmlmarkup(body: string, queries: Array<{ selector: string; attribute?: string; multi?: boolean }>): Array<{ selector: string; attribute?: string; multi: boolean; count: number; values: string[] }> {\n const parsed = new DOMParser().parseFromString(body, \"text/html\");\n return queries.map(query => {\n const matches = [...parsed.querySelectorAll(query.selector)];\n const chosen = query.multi === true ? matches : matches.slice(0, 1);\n const values = chosen.map(element => query.attribute !== undefined ? element.getAttribute(query.attribute) ?? \"\" : element.textContent ?? \"\");\n return { selector: query.selector, ...(query.attribute !== undefined ? { attribute: query.attribute } : {}), multi: query.multi === true, count: matches.length, values };\n });\n}\n\n\n/** Reads the request lifecycle facts the page timing buffers expose: every resource and navigation entry with its url, initiator, timing, transfer size, protocol and the response status a navigation entry reports; the buffers expose no header names, body bytes or subresource status codes. */\nexport function resourcerecords(): Array<Record<string, unknown>> {\n const entries = [...performance.getEntriesByType(\"resource\"), ...performance.getEntriesByType(\"navigation\")];\n return entries.map(entry => {\n const resource = entry as PerformanceResourceTiming & { responseStatus?: number };\n return {\n name: resource.name,\n initiatorType: resource.initiatorType ?? \"\",\n entryType: resource.entryType,\n startTime: resource.startTime,\n duration: resource.duration,\n transferSize: resource.transferSize ?? 0,\n nextHopProtocol: resource.nextHopProtocol ?? \"\",\n ...(typeof resource.responseStatus === \"number\" ? { responseStatus: resource.responseStatus } : {}),\n };\n });\n}\n\n/** Writes reviewed cookies for the granted origin of the page through the page document cookie jar: every record writes its name, value and path with an optional expiry; the write happens on the page the user granted, never through a browser cookies permission. */\nexport function writecookies(records: Array<{ name: string; value: string; path: string; expiresat?: number }>): { written: number; summary: string } {\n let written = 0;\n for (const record of records) {\n const expiry = record.expiresat !== undefined ? `; expires=${new Date(record.expiresat).toUTCString()}` : \"\";\n document.cookie = `${record.name}=${record.value}; path=${record.path}${expiry}; samesite=lax`;\n written += 1;\n }\n return { written, summary: `Wrote ${written} reviewed cookie${written === 1 ? \"\" : \"s\"} through the page cookie jar of ${location.origin}.` };\n}\n\n/** Reads the cookies of the granted origin through the page document cookie jar: document.cookie exposes the name and value pairs of the origin only, with no domain, path or expiry metadata. */\nexport function readcookies(): Array<{ name: string; value: string }> {\n return document.cookie.split(\";\").map(pair => pair.trim()).filter(pair => pair.length > 0).map(pair => {\n const separator = pair.indexOf(\"=\");\n return separator === -1 ? { name: pair, value: \"\" } : { name: pair.slice(0, separator), value: pair.slice(separator + 1) };\n });\n}\n\n/** Clears the cookies of the granted origin through the page document cookie jar: every matched name is expired on the root path; an absent name list clears every cookie the origin jar exposes. */\nexport function clearcookies(names?: string[]): { cleared: number; summary: string } {\n const jar = readcookies();\n const targets = names !== undefined && names.length > 0 ? jar.filter(cookie => names.includes(cookie.name)) : jar;\n for (const cookie of targets) document.cookie = `${cookie.name}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;\n return { cleared: targets.length, summary: `Cleared ${targets.length} cookie${targets.length === 1 ? \"\" : \"s\"} through the page cookie jar of ${location.origin}.` };\n}\n\nObject.assign(globalThis, { devthinkbridge: { capturesnapshot, previewtarget, performstep, readdialogs, measurepage, elementrect, queryelements, preparecapture, scrollcapture, restorecapture, scrollcontainercapture, waitsettle, pdfsegment, pdfbreaks, videoframe, canvasdata, streamelements, mediaelements, pageassets, pageimages, parsehtmlmarkup, resourcerecords, writecookies, readcookies, clearcookies, revertemulationlayer } });\n"],
5
5
  "mappings": ";;;AA2FO,WAAS,kBAAkB,OAA2F;AAC3H,QAAI,CAAC,SAAS,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,EAAG,QAAO;AACxE,UAAM,QAAQ;AACd,UAAM,MAAM,OAAO,MAAM,QAAQ,YAAY,MAAM,IAAI,KAAK,IAAI,MAAM,IAAI,KAAK,IAAI;AACnF,UAAM,OAAO,OAAO,MAAM,SAAS,YAAY,OAAO,UAAU,MAAM,IAAI,KAAK,MAAM,QAAQ,IAAI,MAAM,OAAO;AAC9G,QAAI,QAAQ,UAAa,SAAS,OAAW,QAAO;AACpD,UAAM,SAAS,OAAO,MAAM,WAAW,YAAY,OAAO,UAAU,MAAM,MAAM,KAAK,MAAM,UAAU,IAAI,MAAM,SAAS;AACxH,UAAM,YAAY,OAAO,MAAM,cAAc,YAAY,MAAM,UAAU,KAAK,IAAI,MAAM,UAAU,KAAK,IAAI;AAC3G,WAAO,EAAE,KAAK,MAAM,GAAI,WAAW,SAAY,EAAE,OAAO,IAA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,aAAa,eAAe,gBAAgB,eAAe,gBAAgB,wBAAwB,YAAY,YAAY,WAAW,YAAY,YAAY,gBAAgB,eAAe,YAAY,YAAY,iBAAiB,iBAAiB,cAAc,aAAa,cAAc,qBAAqB,EAAE,CAAC;",
6
6
  "names": ["location", "receiver", "owntext", "events", "modifiers", "keyevent", "fieldlike", "modifiers", "receiver", "wait", "poll", "wrap", "owntext", "stepoptions", "window", "events", "wait", "poll", "wait", "events", "events", "wait", "pollfor", "entry", "wait", "stepoptions", "text"]
7
7
  }