The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(desktop): give the act engine's pure logic real modules
Brooklyn Nicholsondf06be0a16f1
diff --git a/apps/desktop/src/app/chat/right-rail/preview-act.ts b/apps/desktop/src/app/chat/right-rail/preview-act.tsindex 790196c2b10..ee092466434 100644--- a/apps/desktop/src/app/chat/right-rail/preview-act.ts+++ b/apps/desktop/src/app/chat/right-rail/preview-act.ts@@ -27,7 +27,7 @@* out of the boot path.*/-import { actInPage, type PreviewActAction, type PreviewActResult } from '@/lib/preview-act/act-in-page'+import { actEngineSource, type PreviewActAction, type PreviewActResult } from '@/lib/preview-act/act-in-page'import { watchInPage } from '@/lib/preview-act/watch-in-page'import { clickAt, glideTo, pointerPlaced, pressKey, selectAll, typeText, wheelBy } from './preview-drive'@@ -98,7 +98,7 @@ const preamble = () => ` var w = window;// tab to whichever build first touched it. The holder is the exception — it// carries the aimed element from the locate trip to the act trip.w.__hermesActHolder = w.__hermesActHolder || {};- w.__hermesAct = (${actInPage.toString()});+ w.__hermesAct = ${actEngineSource()};w.__hermesWatch_fn = (${watchInPage.toString()});w.__hermesWatchTag = ${WATCH_TAG};var holder = w.__hermesActHolder;@@ -452,6 +452,7 @@ async function driveScroll(action: PreviewActAction,): Promise<PreviewActResult> {const far = action.amount ?? 0+const trip = await runJson(run,buildScrollAnchorScript()@@ -529,6 +530,7 @@ export async function actOnActivePreview(: typed.kind === 'pin'? buildPinScript(typed, typed.text || ''): buildUnpinScript(typed)+const trip = await runJson(run, mark)if (trip.kind === 'failed') {diff --git a/apps/desktop/src/lib/preview-act/act-in-page.test.ts b/apps/desktop/src/lib/preview-act/act-in-page.test.tsindex 081b67c6f11..f92b1efcc34 100644--- a/apps/desktop/src/lib/preview-act/act-in-page.test.ts+++ b/apps/desktop/src/lib/preview-act/act-in-page.test.ts@@ -1,6 +1,6 @@import { beforeEach, describe, expect, it, vi } from 'vitest'-import { actInPage, type PreviewActHolder } from './act-in-page'+import { actEngineSource, actInPage, type PreviewActHolder } from './act-in-page'/** jsdom lays nothing out, so every rect is 0×0 and the engine's visibility* check would reject the whole page. Give elements a plausible box, honouring@@ -45,17 +45,24 @@ beforeEach(() => {})describe('self-containment', () => {- // The pane injects `actInPage.toString()` into the guest page, where module- // scope does not exist. A single free identifier (a module-level constant, an- // imported helper) is a ReferenceError on every call, and Electron reports it- // only as "Script failed to execute" — so evaluate the source the way the- // guest does and run a real action through it.+ // The pane injects `actEngineSource()` into the guest page, where module+ // scope does not exist. A single free identifier — a module-level constant,+ // an imported helper, or a kit factory the assembler forgot to ship — is a+ // ReferenceError on every call, and Electron reports it only as "Script+ // failed to execute". Evaluating the bundle the way the guest does is the+ // only way to catch that here: a plain import of these functions resolves+ // the very names the guest would be missing.+ //+ // It is also the guard on the build. The renderer minifies, so an imported+ // binding named inside the stringified core would be renamed to something+ // the bundle never declares — green in dev and in this suite if the source+ // were assembled any other way, broken only once packaged.it('runs after being stringified and eval’d with no module scope', () => {const page = document.createElement('div')page.innerHTML = '<button id="save">Save</button>'document.body.replaceChildren(page)- const injected = new Function('return (' + actInPage.toString() + ')')() as typeof actInPage+ const injected = new Function('return ' + actEngineSource())() as typeof actInPageconst holder: PreviewActHolder = {}const [save] = injected(document, holder, { kind: 'elements' }).elements!@@ -315,6 +322,7 @@ describe('delta', () => {<a href="/help">Help</a><a href="/terms">Terms</a>`)+inventory(holder);(document.getElementById('q') as HTMLInputElement).value = 'shoes'@@ -329,6 +337,7 @@ describe('delta', () => {<a href="/help">Help</a><a href="/terms">Terms</a>`)+inventory(holder);(document.getElementById('go') as HTMLButtonElement).disabled = falsediff --git a/apps/desktop/src/lib/preview-act/act-in-page.ts b/apps/desktop/src/lib/preview-act/act-in-page.tsindex 1c5428319af..20b3637dd4c 100644--- a/apps/desktop/src/lib/preview-act/act-in-page.ts+++ b/apps/desktop/src/lib/preview-act/act-in-page.ts@@ -2,166 +2,86 @@* PREVIEW ACT ENGINE — the one function that performs an interaction inside a* page, so the agent can drive the in-app browser instead of only reading it.*- * `elements` hands back a numbered inventory of what can be interacted with- * (`@e1`, `@e2`, … — the same ref shape the browser_* tools use, so a model- * that knows one knows the other) and parks the matching nodes on a holder.- * Every other verb resolves its target from that holder, falling back to a raw- * CSS selector.+ * `elements` hands back an inventory of what can be interacted with, each under+ * a durable handle that says what it is — `btn-sign-in`, `inp-email` — and+ * parks the matching nodes on a holder. Every other verb resolves its target+ * from that holder. Once the agent has a baseline for a page, later looks+ * answer with a delta instead of the whole inventory (see `survey`).*- * Injected into the preview webview as source (`actInPage.toString()`), so it- * MUST stay self-contained: no imports, no closure references, no renderer- * globals — everything arrives as a parameter. The structural types below- * erase at compile time, so the stringified function stays plain JS.- */--/** One interactable node, as the agent sees it. */-export interface PreviewElement {- /** Present and true only when the control is non-interactive right now. */- disabled?: boolean- /** Human-readable label (aria-label, text, placeholder, value …). */- label: string- /** Durable handle for as long as this page is open: 'btn-sign-in'. Legible on- * purpose — see the ref-minting note in `actInPage`. */- ref: string- /** Explicit ARIA role, else the tag name. */- role: string- /** The element's `#id` or `[data-testid]`, when it has one. Absent otherwise- * — address the element by its `ref`. */- selector?: string- /** Current value of a form control, truncated. */- value?: string-}--/** An element that is still itself but no longer reads the same.+ * SELF-CONTAINMENT, and why the helpers arrive as an argument.*- * Only the fields that actually moved are present. Role and selector are- * absent by construction rather than by omission: a change in either would- * mean this is a different element, which the re-bind ladder would have- * refused to match in the first place. */-export interface PreviewElementChange {- /** Present only when the control's availability flipped. */- disabled?: boolean- label?: string- ref: string- value?: string-}+ * The core is injected into the guest page as source, where module scope does+ * not exist: a single free identifier is a ReferenceError on every call, which+ * Electron reports only as "Script failed to execute". That much has always+ * been true. The subtler half is that the renderer build MINIFIES, so an+ * imported binding named here would be renamed to something the injected+ * prelude never declared — and it would break in packaged builds only, staying+ * green in dev and in tests. So the core names nothing it does not receive:+ * `kit` carries the naming, visibility, and identity helpers, and+ * `actEngineSource` is what assembles them into one injectable string.+ *+ * `actInPage` below is the ordinary in-process entry point. It is NOT+ * stringified and is free to import.+ */-/** What changed on the page since the last look. Sent instead of the whole- * inventory once the agent has a baseline for the page — see `survey`. */-export interface PreviewActDelta {- /** Elements seen for the first time, in full. */- added?: PreviewElement[]- /** Same handle, new label/value/disabled state — and nothing else. */- changed?: PreviewElementChange[]- /** Handles that are gone from the page. */- removed?: string[]- /** Handles whose element was destroyed and recreated by a re-render. The- * handle still works; nothing about them needs re-reading. */- rebound?: string[]- /** How many handles were on the page and untouched. */- same?: number+import { identityKit } from './identity'+import type { IdentityKit } from './identity'+import { namingKit } from './naming'+import type { NamingKit } from './naming'+import type {+ PreviewActAction,+ PreviewActBinding,+ PreviewActDelta,+ PreviewActHolder,+ PreviewActResult,+ PreviewElement,+ PreviewElementChange+} from './types'+import { visibilityKit } from './visibility'+import type { VisibilityKit } from './visibility'++export type {+ PreviewActAction,+ PreviewActBinding,+ PreviewActDelta,+ PreviewActHolder,+ PreviewActResult,+ PreviewElement,+ PreviewElementChange+} from './types'++/** Everything the core needs that it cannot define for itself. */+export interface ActKit {+ identity: IdentityKit+ naming: NamingKit+ visibility: VisibilityKit}-/** A normalized action. `kind` is the verb; the rest is per-verb payload. */-export interface PreviewActAction {- /** scroll distance in px. Defaults to ~90% of the viewport height. */- amount?: number- key?: string- /** `pin`/`unpin`/`hold` never reach the engine — they resolve their targets- * through `locate`/`elements` and then talk to the overlay — but they arrive- * on the same wire. */- kind:- | 'click'- | 'elements'- | 'hold'- | 'hover'- | 'locate'- | 'pin'- | 'press'- | 'scroll'- | 'strobe'- | 'type'- | 'unpin'- /** locate: also give the target keyboard focus, for a key press that must not- * be preceded by a click (which would activate the control instead). */- focus?: boolean- /** elements: answer with the whole inventory rather than a delta. */- full?: boolean- /** Cap on the returned inventory. */- max?: number- ref?: string- selector?: string- /** type: press Enter (and submit the owning form) after entering text. */- submit?: boolean- text?: string- to?: 'bottom' | 'top'-}+/** Assemble the kits for one document. Used in-process; the guest page gets+ * the same three factories through `actEngineSource`. */+export function buildActKit(doc: Document): ActKit {+ const naming = namingKit(doc)-export interface PreviewActResult {- /** What the action landed on, for the agent's own log. */- acted?: string- /** What moved since the last look. Present INSTEAD of `elements` once the- * agent holds a baseline for this page. */- delta?: PreviewActDelta- /** The full inventory. Sent on the first look at a page, and again whenever- * the page changed too much for a delta to be the cheaper answer. */- elements?: PreviewElement[]- error?: string- note?: string- /** Viewport centre of a located target, for aiming real pointer input at it. */- point?: { x: number; y: number }- success: boolean- title?: string- /** locate: whether the target actually takes typed text. */- typable?: boolean- /** Live document URL after the action — a change means it navigated. */- url?: string+ return { identity: identityKit(naming), naming, visibility: visibilityKit(doc, doc.defaultView) }}-/** One element the agent has a handle on, remembered across actions. */-export interface PreviewActBinding {- el: Element- /** What it read as last time. Kept field by field rather than as one hash so- * a change can be reported as only the part that moved. */- label: string- /** The accessible name at mint time, for re-finding this element after a- * re-render destroys and recreates its node. */- name: string- /** Whether the control was unavailable last time. */- off: boolean- /** Nearest-landmark path plus position among same-role siblings. */- path: string- ref: string- role: string- /** `id` / `name` / `data-testid` / `aria-label`, if the page provides one.- * The strongest re-bind signal there is, and the only one a rewrite of the- * surrounding markup cannot disturb. */- stable: string- value: string+/** Run one action against `doc`, resolving refs through `holder`. */+export function actInPage(doc: Document, holder: PreviewActHolder, action: PreviewActAction): PreviewActResult {+ return actInPageCore(doc, holder, action, buildActKit(doc))}-/** Where the surface keeps what it knows between actions (a window global in- * the preview page), so a handle still means something on the next call. */-export interface PreviewActHolder {- /** Target of the action in flight, for the watch overlay to draw onto. */- aimed?: Element | null- /** Every handle minted on this page, live or not yet retired. */- book?: PreviewActBinding[]- /** Next disambiguating suffix per ref stem, so two "Edit" buttons become- * `btn-edit` and `btn-edit-1`. Never rewound: a retired handle's name is- * not handed to a different element later in the same page. */- coined?: Record<string, number>- /** The on-screen subset, for the overlay to outline. Diverges from `nodes` in- * both directions: it drops what is below the fold, and it is not capped at- * the inventory's size. */- field?: Element[]- nodes?: Element[]- /** URL the snapshot was taken on; a navigation retires every handle. */- url?: string-}+/** The injectable core. Everything it touches is a parameter — see the+ * self-containment note above. */+export function actInPageCore(+ doc: Document,+ holder: PreviewActHolder,+ action: PreviewActAction,+ kit: ActKit+): PreviewActResult {+ const { anchorOf, labelOf, selectorFor, stableOf, valueOf } = kit.naming+ const { affinity, coin, shifted } = kit.identity+ const { onScreen, visible } = kit.visibility-/** Run one action against `doc`, resolving refs through `holder`. Self-contained. */-export function actInPage(doc: Document, holder: PreviewActHolder, action: PreviewActAction): PreviewActResult {// Declared inside, not at module scope: this body is stringified and eval'd// in the guest page, where a module-level constant is simply not defined.const maxElements = 120@@ -182,276 +102,6 @@ export function actInPage(doc: Document, holder: PreviewActHolder, action: Previconst still = !!(win && win.matchMedia && win.matchMedia('(prefers-reduced-motion: reduce)').matches)const glide: ScrollBehavior = still ? 'auto' : 'smooth'- const cssEscape = (value: string) =>- typeof CSS !== 'undefined' && CSS.escape ? CSS.escape(value) : value.replace(/["\\]/g, '\\$&')-- const clamp = (text: string, max: number) => (text.length > max ? text.slice(0, max - 1) + '…' : text)-- const labelOf = (el: Element): string => {- const aria = el.getAttribute('aria-label')-- if (aria) {- return clamp(aria, 80)- }-- const labelledBy = el.getAttribute('aria-labelledby')- const labelled = labelledBy ? doc.getElementById(labelledBy) : null- const text = ((labelled || el).textContent || '').trim().replace(/\s+/g, ' ')-- if (text) {- return clamp(text, 80)- }-- for (const attr of ['placeholder', 'title', 'alt', 'name', 'value']) {- const value = el.getAttribute(attr)-- if (value) {- return clamp(value, 80)- }- }-- return ''- }-- /** The strongest identity signal the page offers, if it offers one. Nothing a- * re-render does to the surrounding markup disturbs these. */- const stableOf = (el: Element): string =>- el.id ||- el.getAttribute('data-testid') ||- el.getAttribute('name') ||- el.getAttribute('aria-label') ||- ''-- /** Handle stems by role, so a handle says what it is before it says which- * one. Anything unrecognised is `el`. */- const stemOf = (role: string): string => {- if (role === 'button' || role === 'summary') {- return 'btn'- }-- if (role === 'a' || role === 'link') {- return 'lnk'- }-- if (role === 'input:search' || role === 'searchbox') {- return 'srch'- }-- if (role === 'input:checkbox' || role === 'checkbox') {- return 'chk'- }-- if (role === 'input:radio' || role === 'radio') {- return 'rdo'- }-- if (role === 'select' || role === 'combobox') {- return 'sel'- }-- if (role === 'textarea') {- return 'txt'- }-- if (role === 'switch') {- return 'sw'- }-- if (role === 'tab' || role === 'menuitem' || role === 'option') {- return role === 'menuitem' ? 'mi' : role === 'option' ? 'opt' : 'tab'- }-- // Every `input:*` that isn't one of the special cases above, plus the ARIA- // textbox. A date picker and an email field are both places text goes.- return role.indexOf('input') === 0 || role === 'textbox' ? 'inp' : 'el'- }-- /** Lowercase, hyphenated, and short enough to read at a glance. */- const slug = (name: string): string => {- let out = ''- let dash = false-- for (let i = 0; i < name.length && out.length < 24; i++) {- const ch = name[i]-- if (/[a-zA-Z0-9]/.test(ch)) {- out += ch.toLowerCase()- dash = false- } else if (!dash && out) {- out += '-'- dash = true- }- }-- return out.replace(/-+$/, '')- }-- /** Where the element sits, coarsely: the nearest landmark plus its position- * among same-role elements inside it. Deliberately NOT the CSS selector- * below — a wrapper div appearing anywhere in the chain changes that string- * completely, which is exactly the churn a re-bind has to see through. */- const anchorOf = (el: Element): string => {- const near = el.closest(- 'main,nav,header,footer,aside,[role="main"],[role="navigation"],[role="banner"],' +- '[role="contentinfo"],[role="complementary"],[role="search"],form[aria-label],section[aria-label]'- )-- if (!near) {- return 'root'- }-- const named = near.getAttribute('aria-label') || ''-- return near.tagName.toLowerCase() + (named ? '#' + slug(named) : '')- }-- /** The element's own selector, when the page gives it one worth having.- *- * Deliberately identity-only. This used to fall back to a chain of up to- * eight `:nth-child` rungs, and on a real app shell that column was 74% of- * the entire inventory — the single biggest thing the agent was paying for.- * It bought nothing: nothing downstream reads it, a positional chain is- * wrong the moment a sibling appears, and re-finding a node is what the- * durable ref now does properly. An `#id` is short, stable, and the one- * case where naming the node is genuinely useful to the model. */- const selectorFor = (el: Element): string => {- if (el.id) {- return '#' + cssEscape(el.id)- }-- const testId = el.getAttribute('data-testid')-- return testId ? '[data-testid="' + cssEscape(testId) + '"]' : ''- }-- /** On screen right now, and worth drawing a box around. The field is strictly- * viewport-bound: a mark past the fold is one nobody can see, it spends the- * budget that should have gone to what IS on screen, and the ones parked far- * off to the side were landing as stray labels in the corner. */- const onScreen = (el: Element): boolean => {- if (!win) {- return false- }-- const box = el.getBoundingClientRect()-- if (box.right <= 0 || box.bottom <= 0 || box.left >= win.innerWidth || box.top >= win.innerHeight) {- return false- }-- // Page-sized wrappers. Outlining one draws a rectangle around the whole- // screen, which says nothing and frames everything inside it as if it- // mattered. Full-width banners are fine — it takes both dimensions.- return !(box.width >= win.innerWidth * 0.95 && box.height >= win.innerHeight * 0.9)- }-- /** Painted at all, ancestors included. */- const shown = (el: Element): boolean => {- // Folds in display/visibility/opacity/content-visibility inherited from an- // ANCESTOR, which this element's own computed style does not report: inside- // a parent at opacity 0, every child still reads back opacity 1.- const seen = (el as Element & { checkVisibility?: (opts: object) => boolean }).checkVisibility-- if (typeof seen === 'function' && !seen.call(el, { checkOpacity: true, checkVisibilityCSS: true })) {- return false- }-- const style = win && win.getComputedStyle ? win.getComputedStyle(el) : null-- if (style) {- if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') {- return false- }-- // The screen-reader-only recipe, both spellings: the deprecated `clip` and- // the modern `clip-path: inset(50%)`. Neither exists for any purpose other- // than hiding something visually while keeping it focusable.- if ((style.clip && style.clip !== 'auto') || (style.clipPath || '').indexOf('inset(50%') !== -1) {- return false- }- }-- return true- }-- const visible = (el: Element): boolean => {- if (!shown(el)) {- return false- }-- // Things the page itself declares are not for interacting with, neither of- // which shows up in a computed style or a bounding box: an aria-hidden- // subtree is invisible to every other assistive client, and `inert` cannot- // be clicked at all. Disabled controls are deliberately NOT filtered here —- // they stay in the inventory so the agent is told a button is disabled- // rather than hunting for one that appears not to exist.- if (el.closest('[aria-hidden="true"], [inert]')) {- return false- }-- const rect = el.getBoundingClientRect()-- // Below the fold is fine — we scroll to it. Off to the LEFT or ABOVE is the- // other half of the sr-only trick (`left: -9999px`, `top: -9999px`) and- // scrolling never brings those back.- if (rect.right <= 0 || rect.bottom <= 0) {- return false- }-- // Bigger than the 1px box sr-only collapses to. Nothing a person can aim at- // is 2px across, and admitting those is what put the cursor in the corner:- // they cluster at the document origin, so they sort to the FRONT of the- // inventory and the agent reaches for one as @e1.- if (rect.width < 3 || rect.height < 3) {- return false- }-- // Occlusion, and the last filter for a reason: everything above is cheap- // and this one forces layout. If the middle of the element is on screen,- // whatever the browser reports at that point has to BE this element — its- // own descendant (an icon inside a link) or the box it paints inside are- // fine, anything else means it is buried under a sticky header, a cookie- // wall, or a transparent layer the page put on top. Those are exactly the- // targets the agent aims at and then misses. Elements below the fold cannot- // be hit-tested from here, so they are taken on trust and land back in this- // function after the scroll.- const midX = rect.left + rect.width / 2- const midY = rect.top + rect.height / 2-- const under = (doc as Document & { elementFromPoint?: (x: number, y: number) => Element | null })- .elementFromPoint-- if (- typeof under === 'function' &&- win &&- midX >= 0 &&- midY >= 0 &&- midX < win.innerWidth &&- midY < win.innerHeight- ) {- const over = under.call(doc, midX, midY)-- if (!over || !(over === el || el.contains(over) || over.contains(el))) {- return false- }- }-- return true- }-- const valueOf = (el: Element): string => {- const control = el as HTMLInputElement-- if (typeof control.value === 'string' && control.value) {- return clamp(control.value, 60)- }-- if (control.checked !== undefined && (el.tagName === 'INPUT' || el.getAttribute('role') === 'checkbox')) {- return control.checked ? 'checked' : 'unchecked'- }-- return ''- }/** Walk the page and hand back what is interactable, in document order. The* handles are assigned afterwards, by `survey`. */@@ -534,97 +184,6 @@ export function actInPage(doc: Document, holder: PreviewActHolder, action: Previreturn { elements, nodes }}- /** What moved on an element that kept its handle, or nothing if it held- * still. Field by field, so a status line ticking over costs the agent one- * short line instead of a re-run of everything already known about it. */- const shifted = (was: PreviewActBinding, entry: PreviewElement): PreviewElementChange | null => {- const off = !!entry.disabled- const value = entry.value || ''-- if (was.label === entry.label && was.value === value && was.off === off) {- return null- }-- const moved: PreviewElementChange = { ref: was.ref }-- if (was.label !== entry.label) {- moved.label = entry.label- }-- if (was.value !== value) {- moved.value = value- }-- if (was.off !== off) {- moved.disabled = off- }-- return moved- }-- /** Do two labels share at least half their words? Tolerates the count badge- * and the copy edit — "Inbox" against "Inbox (3)". */- const alike = (a: string, b: string): boolean => {- if (!a || !b) {- return false- }-- const one = a.toLowerCase().split(/\s+/).filter(Boolean)- const two = b.toLowerCase().split(/\s+/).filter(Boolean)- const both = one.filter(word => two.indexOf(word) !== -1).length- const all = one.length + two.filter(word => one.indexOf(word) === -1).length-- return all > 0 && both / all >= 0.5- }-- /** How strongly a remembered element matches one just observed, 0 to 1.- *- * Ported from anchortree's re-bind ladder (Apache-2.0), minus its geometry- * rung: a centroid is only ever worth 0.1 there, it never reaches the 0.6- * bar on its own, and carrying coordinates through the book to buy a- * tie-break is not worth the measurement. */- const affinity = (was: PreviewActBinding, now: PreviewActBinding): number => {- // A button is not a link, however alike the rest of it reads.- if (was.role !== now.role) {- return 0- }-- // Two elements that BOTH carry a stable attribute and disagree are the page- // telling us outright that they are different things.- if (was.stable && now.stable) {- return was.stable === now.stable ? 1 : 0- }-- let score = 0-- if (was.name && was.name === now.name) {- score += 0.6- } else if (alike(was.name, now.name)) {- score += 0.4- }-- if (was.path && was.path === now.path) {- score += 0.3- }-- return score- }-- /** Mint a handle. Legible on purpose: the agent reads `btn-sign-in` in a- * three-line delta on turn nine and knows what it is, where `@e42` would- * send it back to an inventory twenty thousand tokens ago. Suffixes are- * never rewound, so a retired handle's name is not later handed to a- * different element on the same page. */- const coin = (role: string, name: string): string => {- const coined = holder.coined || (holder.coined = {})- const named = slug(name)- const stem = stemOf(role) + (named ? '-' + named : '')- const nth = coined[stem] || 0-- coined[stem] = nth + 1-- return nth ? stem + '-' + nth : stem- }/** Look at the page and say what is there — or, once there is something to* compare against, only what moved.@@ -644,6 +203,7 @@ export function actInPage(doc: Document, holder: PreviewActHolder, action: Previ}const book = holder.book || (holder.book = [])+ const coined = holder.coined || (holder.coined = {})const seen = sight(max)const claimed: Record<string, boolean> = {}const kept: PreviewActBinding[] = []@@ -678,6 +238,7 @@ export function actInPage(doc: Document, holder: PreviewActHolder, action: Previif (!moved) {same+++continue}@@ -747,7 +308,7 @@ export function actInPage(doc: Document, holder: PreviewActHolder, action: Previcontinue}- now.ref = coin(entry.role, now.name)+ now.ref = coin(coined, entry.role, now.name)entry.ref = now.refclaimed[now.ref] = truekept.push(now)@@ -1052,3 +613,22 @@ export function actInPage(doc: Document, holder: PreviewActHolder, action: Previreturn fail('Unknown action: ' + String(action.kind))}++/** The engine as one injectable expression: `function (doc, holder, action)`.+ *+ * The four factories are stringified together so the core's `kit` is built+ * inside the guest page, out of sources that travelled with it. This is the+ * ONLY supported way to get the engine's source — taking `.toString()` of any+ * one piece yields a function whose helpers are not defined, which fails at+ * call time rather than at injection. */+export function actEngineSource(): string {+ return `(function (doc, holder, action) {+ var naming = (${namingKit.toString()})(doc);+ var kit = {+ naming: naming,+ identity: (${identityKit.toString()})(naming),+ visibility: (${visibilityKit.toString()})(doc, doc.defaultView)+ };+ return (${actInPageCore.toString()})(doc, holder, action, kit);+})`+}diff --git a/apps/desktop/src/lib/preview-act/identity.ts b/apps/desktop/src/lib/preview-act/identity.tsnew file mode 100644index 00000000000..6c47e895941--- /dev/null+++ b/apps/desktop/src/lib/preview-act/identity.ts@@ -0,0 +1,129 @@+/**+ * IDENTITY — deciding whether the element in front of us is one we already+ * have a handle on, and naming it if it is not.+ *+ * This is what makes a delta possible at all. If handles churned every time a+ * framework re-rendered, every look at the page would be a wall of removals and+ * additions and there would be nothing to send but the whole inventory again.+ *+ * The re-bind ladder is ported from anchortree (Apache-2.0), minus its geometry+ * rung. It is deliberately conservative: below the bar we mint a new handle+ * instead of guessing, because a re-render costing the agent a re-read is a+ * cheap mistake and a handle silently pointing at the wrong button is not.+ *+ * SELF-CONTAINMENT: see `naming.ts`. Same contract, same reason for the+ * factory shape — with one extra consequence. `identityKit` takes the naming+ * helpers as an argument rather than importing them, because an import would+ * be a cross-module identifier the bundler is free to rename out from under+ * the stringified body.+ */++import type { NamingKit } from './naming'+import type { PreviewActBinding, PreviewElement, PreviewElementChange } from './types'++export interface IdentityKit {+ /** How strongly a remembered element matches one just observed, 0 to 1. */+ affinity(was: PreviewActBinding, now: PreviewActBinding): number+ /** Do two labels share at least half their words? */+ alike(a: string, b: string): boolean+ /** Mint a handle, disambiguating against the ones already minted. */+ coin(coined: Record<string, number>, role: string, name: string): string+ /** What moved on an element that kept its handle, or null if it held still. */+ shifted(was: PreviewActBinding, entry: PreviewElement): null | PreviewElementChange+}++/** Build the identity helpers on top of a naming kit. */+export function identityKit(naming: NamingKit): IdentityKit {+ /** What moved on an element that kept its handle, or nothing if it held+ * still. Field by field, so a status line ticking over costs the agent one+ * short line instead of a re-run of everything already known about it. */+ const shifted = (was: PreviewActBinding, entry: PreviewElement): null | PreviewElementChange => {+ const off = !!entry.disabled+ const value = entry.value || ''++ if (was.label === entry.label && was.value === value && was.off === off) {+ return null+ }++ const moved: PreviewElementChange = { ref: was.ref }++ if (was.label !== entry.label) {+ moved.label = entry.label+ }++ if (was.value !== value) {+ moved.value = value+ }++ if (was.off !== off) {+ moved.disabled = off+ }++ return moved+ }++ /** Do two labels share at least half their words? Tolerates the count badge+ * and the copy edit — "Inbox" against "Inbox (3)". */+ const alike = (a: string, b: string): boolean => {+ if (!a || !b) {+ return false+ }++ const one = a.toLowerCase().split(/\s+/).filter(Boolean)+ const two = b.toLowerCase().split(/\s+/).filter(Boolean)+ const both = one.filter(word => two.indexOf(word) !== -1).length+ const all = one.length + two.filter(word => one.indexOf(word) === -1).length++ return all > 0 && both / all >= 0.5+ }++ /** How strongly a remembered element matches one just observed, 0 to 1.+ *+ * Ported from anchortree's re-bind ladder (Apache-2.0), minus its geometry+ * rung: a centroid is only ever worth 0.1 there, it never reaches the 0.6+ * bar on its own, and carrying coordinates through the book to buy a+ * tie-break is not worth the measurement. */+ const affinity = (was: PreviewActBinding, now: PreviewActBinding): number => {+ // A button is not a link, however alike the rest of it reads.+ if (was.role !== now.role) {+ return 0+ }++ // Two elements that BOTH carry a stable attribute and disagree are the page+ // telling us outright that they are different things.+ if (was.stable && now.stable) {+ return was.stable === now.stable ? 1 : 0+ }++ let score = 0++ if (was.name && was.name === now.name) {+ score += 0.6+ } else if (alike(was.name, now.name)) {+ score += 0.4+ }++ if (was.path && was.path === now.path) {+ score += 0.3+ }++ return score+ }++ /** Mint a handle. Legible on purpose: the agent reads `btn-sign-in` in a+ * three-line delta on turn nine and knows what it is, where `@e42` would+ * send it back to an inventory twenty thousand tokens ago. Suffixes are+ * never rewound, so a retired handle's name is not later handed to a+ * different element on the same page. */+ const coin = (coined: Record<string, number>, role: string, name: string): string => {+ const named = naming.slug(name)+ const stem = naming.stemOf(role) + (named ? '-' + named : '')+ const nth = coined[stem] || 0++ coined[stem] = nth + 1++ return nth ? stem + '-' + nth : stem+ }++ return { affinity, alike, coin, shifted }+}diff --git a/apps/desktop/src/lib/preview-act/naming.test.ts b/apps/desktop/src/lib/preview-act/naming.test.tsnew file mode 100644index 00000000000..3875062a03d--- /dev/null+++ b/apps/desktop/src/lib/preview-act/naming.test.ts@@ -0,0 +1,238 @@+import { beforeEach, describe, expect, it } from 'vitest'++import { identityKit } from './identity'+import { namingKit } from './naming'+import type { PreviewActBinding } from './types'++const naming = () => namingKit(document)++function el(html: string): Element {+ document.body.innerHTML = html++ return document.body.firstElementChild!+}++/** A remembered element, with only the fields a given test cares about set. */+function bound(over: Partial<PreviewActBinding>): PreviewActBinding {+ return {+ el: document.body,+ label: '',+ name: '',+ off: false,+ path: 'root',+ ref: 'btn-x',+ role: 'button',+ stable: '',+ value: '',+ ...over+ }+}++beforeEach(() => {+ document.body.innerHTML = ''+})++describe('slug', () => {+ it('reads as words a person would recognise', () => {+ expect(naming().slug('Sign In')).toBe('sign-in')+ expect(naming().slug(' Add to cart! ')).toBe('add-to-cart')+ })++ it('collapses runs of punctuation rather than stacking hyphens', () => {+ expect(naming().slug('Save — and / continue')).toBe('save-and-continue')+ })++ it('stays short enough to read at a glance, and never ends on a hyphen', () => {+ const long = naming().slug('Subscribe to our weekly newsletter for updates')++ expect(long.length).toBeLessThanOrEqual(24)+ expect(long.endsWith('-')).toBe(false)+ })++ it('has nothing to say about an unlabelled element', () => {+ expect(naming().slug('!!!')).toBe('')+ })+})++describe('stemOf', () => {+ // The stem is the part of a handle that survives translation: whatever the+ // button says, `btn-` tells the agent it is a button.+ it('gives each interactive role its own prefix', () => {+ const { stemOf } = naming()++ expect(stemOf('button')).toBe('btn')+ expect(stemOf('a')).toBe('lnk')+ expect(stemOf('input:search')).toBe('srch')+ expect(stemOf('input:checkbox')).toBe('chk')+ expect(stemOf('select')).toBe('sel')+ })++ it('treats every other kind of text field as one thing', () => {+ const { stemOf } = naming()++ expect(stemOf('input:email')).toBe('inp')+ expect(stemOf('input:date')).toBe('inp')+ expect(stemOf('textbox')).toBe('inp')+ })++ it('falls back rather than inventing a prefix for something unknown', () => {+ expect(naming().stemOf('marquee')).toBe('el')+ })+})++describe('labelOf', () => {+ it('prefers what the page tells assistive clients', () => {+ expect(naming().labelOf(el('<button aria-label="Close dialog">×</button>'))).toBe('Close dialog')+ })++ it('follows aria-labelledby to the element that names it', () => {+ document.body.innerHTML = '<h2 id="t">Billing</h2><button aria-labelledby="t">→</button>'++ expect(naming().labelOf(document.querySelector('button')!)).toBe('Billing')+ })++ it('falls back through placeholder and title before giving up', () => {+ expect(naming().labelOf(el('<input placeholder="Your email" />'))).toBe('Your email')+ expect(naming().labelOf(el('<a href="/x" title="Help"></a>'))).toBe('Help')+ expect(naming().labelOf(el('<button></button>'))).toBe('')+ })++ it('flattens the whitespace a formatted template leaves behind', () => {+ expect(naming().labelOf(el('<button>\n Save\n changes\n</button>'))).toBe('Save changes')+ })+})++describe('selectorFor', () => {+ // Identity only. A positional chain was 74% of the inventory and wrong the+ // moment a sibling appeared.+ it('names the element only when the page gave it a durable name', () => {+ const { selectorFor } = naming()++ expect(selectorFor(el('<button id="save">S</button>'))).toBe('#save')+ expect(selectorFor(el('<button data-testid="save">S</button>'))).toBe('[data-testid="save"]')+ expect(selectorFor(el('<button class="btn primary">S</button>'))).toBe('')+ })+})++describe('valueOf', () => {+ it('reports what a control currently holds', () => {+ const input = el('<input value="shoes" />')++ expect(naming().valueOf(input)).toBe('shoes')+ })++ // FIXME(checkbox): a checkbox's `.value` is the string "on" unless the page+ // sets one, and that branch is tested first — so both states read back the+ // same and the agent cannot tell a ticked box from an empty one. Pinned as-is+ // here so the extraction is provably behaviour-preserving; fixed next commit.+ it('reports a checkbox as the value the DOM gives it', () => {+ const box = el('<input type="checkbox" />') as HTMLInputElement++ expect(naming().valueOf(box)).toBe('on')++ box.checked = true++ expect(naming().valueOf(box)).toBe('on')+ })+})++describe('anchorOf', () => {+ // Coarse on purpose: a wrapper div appearing in the chain must not change+ // this string, because that churn is exactly what a re-bind sees through.+ it('places an element by the landmark it sits in', () => {+ document.body.innerHTML = '<nav><div><div><button>Home</button></div></div></nav>'++ expect(naming().anchorOf(document.querySelector('button')!)).toBe('nav')+ })++ it('tells two same-tag landmarks apart by their label', () => {+ document.body.innerHTML = '<nav aria-label="Primary nav"><button>Home</button></nav>'++ expect(naming().anchorOf(document.querySelector('button')!)).toBe('nav#primary-nav')+ })++ it('says so when there is no landmark to place it in', () => {+ expect(naming().anchorOf(el('<button>Home</button>'))).toBe('root')+ })+})++describe('alike', () => {+ const { alike } = identityKit(naming())++ it('sees through a count badge and a copy edit', () => {+ expect(alike('Inbox', 'Inbox (3)')).toBe(true)+ expect(alike('Save changes', 'Save your changes')).toBe(true)+ })++ it('does not call two unrelated labels the same thing', () => {+ expect(alike('Sign in', 'Delete account')).toBe(false)+ expect(alike('Save', '')).toBe(false)+ })+})++describe('affinity', () => {+ const { affinity } = identityKit(naming())++ it('is certain when both sides carry the same stable attribute', () => {+ expect(affinity(bound({ stable: 'save' }), bound({ stable: 'save' }))).toBe(1)+ })++ // The page is telling us outright that these are different things.+ it('refuses outright when two stable attributes disagree', () => {+ expect(affinity(bound({ stable: 'save' }), bound({ stable: 'cancel' }))).toBe(0)+ })++ it('refuses to move a handle across roles, however alike the rest reads', () => {+ const was = bound({ name: 'Save', role: 'button' })+ const now = bound({ name: 'Save', role: 'a' })++ expect(affinity(was, now)).toBe(0)+ })++ it('clears the bar on name and place together, and not on place alone', () => {+ const named = affinity(bound({ name: 'Save', path: 'main' }), bound({ name: 'Save', path: 'main' }))+ const placed = affinity(bound({ name: 'Save', path: 'main' }), bound({ name: 'Publish', path: 'main' }))++ expect(named).toBeGreaterThanOrEqual(0.6)+ expect(placed).toBeLessThan(0.6)+ })+})++describe('coin', () => {+ const mint = () => {+ const { coin } = identityKit(naming())+ const coined: Record<string, number> = {}++ return (role: string, name: string) => coin(coined, role, name)+ }++ it('names a handle after what the thing is and what it says', () => {+ const coin = mint()++ expect(coin('button', 'Sign in')).toBe('btn-sign-in')+ expect(coin('input:email', 'Email')).toBe('inp-email')+ })++ it('tells repeats apart without renaming the first one', () => {+ const coin = mint()++ expect(coin('button', 'Edit')).toBe('btn-edit')+ expect(coin('button', 'Edit')).toBe('btn-edit-1')+ expect(coin('button', 'Edit')).toBe('btn-edit-2')+ })++ // Never rewound, so a retired handle's name is not later handed to a+ // different element on the same page.+ it('does not reissue a name after the element holding it goes away', () => {+ const coin = mint()++ coin('button', 'Edit')+ coin('button', 'Edit')++ expect(coin('button', 'Edit')).toBe('btn-edit-2')+ })++ it('still mints something for a role it has no name for', () => {+ expect(mint()('button', '')).toBe('btn')+ })+})diff --git a/apps/desktop/src/lib/preview-act/naming.ts b/apps/desktop/src/lib/preview-act/naming.tsnew file mode 100644index 00000000000..afadb937f3f--- /dev/null+++ b/apps/desktop/src/lib/preview-act/naming.ts@@ -0,0 +1,199 @@+/**+ * NAMING — what an element is called, and what it is called by.+ *+ * Everything here turns an element into a short string: the accessible name,+ * the durable handle's stem and slug, the landmark it sits under, the identity+ * selector. No page state, no holder, no side effects.+ *+ * SELF-CONTAINMENT. `namingKit` is stringified into the guest page along with+ * the engine that uses it (see `act-in-page.ts`'s contract). Module scope does+ * not exist there, so the factory closes over nothing but its own argument and+ * declares every helper inside itself. A single free identifier is a+ * ReferenceError on every call, and Electron reports it only as "Script failed+ * to execute".+ *+ * That constraint is also why this is a factory rather than nine exports: nine+ * exports would be nine names for the bundler to mangle independently of the+ * call sites inside the stringified engine. One function that stringifies whole+ * has no cross-module references to break.+ */++export interface NamingKit {+ /** Where the element sits, coarsely — the nearest landmark. */+ anchorOf(el: Element): string+ clamp(text: string, max: number): string+ cssEscape(value: string): string+ /** The accessible name, by the usual ladder, truncated. */+ labelOf(el: Element): string+ /** An `#id` or `[data-testid]`, or empty when the page offers neither. */+ selectorFor(el: Element): string+ /** Lowercase, hyphenated, and short enough to read at a glance. */+ slug(name: string): string+ /** The strongest identity signal the page offers, if it offers one. */+ stableOf(el: Element): string+ /** The handle stem for a role: `btn`, `inp`, `lnk` … */+ stemOf(role: string): string+ /** A form control's current value, or its checked state. */+ valueOf(el: Element): string+}++/** Build the naming helpers against one document. */+export function namingKit(doc: Document): NamingKit {+ const cssEscape = (value: string) =>+ typeof CSS !== 'undefined' && CSS.escape ? CSS.escape(value) : value.replace(/["\\]/g, '\\$&')++ const clamp = (text: string, max: number) => (text.length > max ? text.slice(0, max - 1) + '…' : text)++ const labelOf = (el: Element): string => {+ const aria = el.getAttribute('aria-label')++ if (aria) {+ return clamp(aria, 80)+ }++ const labelledBy = el.getAttribute('aria-labelledby')+ const labelled = labelledBy ? doc.getElementById(labelledBy) : null+ const text = ((labelled || el).textContent || '').trim().replace(/\s+/g, ' ')++ if (text) {+ return clamp(text, 80)+ }++ for (const attr of ['placeholder', 'title', 'alt', 'name', 'value']) {+ const value = el.getAttribute(attr)++ if (value) {+ return clamp(value, 80)+ }+ }++ return ''+ }++ /** The strongest identity signal the page offers, if it offers one. Nothing a+ * re-render does to the surrounding markup disturbs these. */+ const stableOf = (el: Element): string =>+ el.id ||+ el.getAttribute('data-testid') ||+ el.getAttribute('name') ||+ el.getAttribute('aria-label') ||+ ''++ /** Handle stems by role, so a handle says what it is before it says which+ * one. Anything unrecognised is `el`. */+ const stemOf = (role: string): string => {+ if (role === 'button' || role === 'summary') {+ return 'btn'+ }++ if (role === 'a' || role === 'link') {+ return 'lnk'+ }++ if (role === 'input:search' || role === 'searchbox') {+ return 'srch'+ }++ if (role === 'input:checkbox' || role === 'checkbox') {+ return 'chk'+ }++ if (role === 'input:radio' || role === 'radio') {+ return 'rdo'+ }++ if (role === 'select' || role === 'combobox') {+ return 'sel'+ }++ if (role === 'textarea') {+ return 'txt'+ }++ if (role === 'switch') {+ return 'sw'+ }++ if (role === 'tab' || role === 'menuitem' || role === 'option') {+ return role === 'menuitem' ? 'mi' : role === 'option' ? 'opt' : 'tab'+ }++ // Every `input:*` that isn't one of the special cases above, plus the ARIA+ // textbox. A date picker and an email field are both places text goes.+ return role.indexOf('input') === 0 || role === 'textbox' ? 'inp' : 'el'+ }++ /** Lowercase, hyphenated, and short enough to read at a glance. */+ const slug = (name: string): string => {+ let out = ''+ let dash = false++ for (let i = 0; i < name.length && out.length < 24; i++) {+ const ch = name[i]++ if (/[a-zA-Z0-9]/.test(ch)) {+ out += ch.toLowerCase()+ dash = false+ } else if (!dash && out) {+ out += '-'+ dash = true+ }+ }++ return out.replace(/-+$/, '')+ }++ /** Where the element sits, coarsely: the nearest landmark plus its position+ * among same-role elements inside it. Deliberately NOT the CSS selector+ * below — a wrapper div appearing anywhere in the chain changes that string+ * completely, which is exactly the churn a re-bind has to see through. */+ const anchorOf = (el: Element): string => {+ const near = el.closest(+ 'main,nav,header,footer,aside,[role="main"],[role="navigation"],[role="banner"],' ++ '[role="contentinfo"],[role="complementary"],[role="search"],form[aria-label],section[aria-label]'+ )++ if (!near) {+ return 'root'+ }++ const named = near.getAttribute('aria-label') || ''++ return near.tagName.toLowerCase() + (named ? '#' + slug(named) : '')+ }++ /** The element's own selector, when the page gives it one worth having.+ *+ * Deliberately identity-only. This used to fall back to a chain of up to+ * eight `:nth-child` rungs, and on a real app shell that column was 74% of+ * the entire inventory — the single biggest thing the agent was paying for.+ * It bought nothing: nothing downstream reads it, a positional chain is+ * wrong the moment a sibling appears, and re-finding a node is what the+ * durable ref now does properly. An `#id` is short, stable, and the one+ * case where naming the node is genuinely useful to the model. */+ const selectorFor = (el: Element): string => {+ if (el.id) {+ return '#' + cssEscape(el.id)+ }++ const testId = el.getAttribute('data-testid')++ return testId ? '[data-testid="' + cssEscape(testId) + '"]' : ''+ }++ const valueOf = (el: Element): string => {+ const control = el as HTMLInputElement++ if (typeof control.value === 'string' && control.value) {+ return clamp(control.value, 60)+ }++ if (control.checked !== undefined && (el.tagName === 'INPUT' || el.getAttribute('role') === 'checkbox')) {+ return control.checked ? 'checked' : 'unchecked'+ }++ return ''+ }++ return { anchorOf, clamp, cssEscape, labelOf, selectorFor, slug, stableOf, stemOf, valueOf }+}diff --git a/apps/desktop/src/lib/preview-act/types.ts b/apps/desktop/src/lib/preview-act/types.tsnew file mode 100644index 00000000000..cbbc59f1ecf--- /dev/null+++ b/apps/desktop/src/lib/preview-act/types.ts@@ -0,0 +1,157 @@+/**+ * PREVIEW ACT TYPES — the shapes the engine, the overlay, and the Python tool+ * all agree on.+ *+ * Declarations only. They erase at compile time, which is what lets the engine+ * modules import them while still stringifying into the guest page as plain JS+ * (see `naming.ts` for the self-containment contract they have to satisfy).+ *+ * Re-exported from `act-in-page.ts`, so every existing import site keeps+ * working and nothing outside this directory needs to know they moved.+ */++/** One interactable node, as the agent sees it. */+export interface PreviewElement {+ /** Present and true only when the control is non-interactive right now. */+ disabled?: boolean+ /** Human-readable label (aria-label, text, placeholder, value …). */+ label: string+ /** Durable handle for as long as this page is open: 'btn-sign-in'. Legible on+ * purpose — see the ref-minting note in `actInPage`. */+ ref: string+ /** Explicit ARIA role, else the tag name. */+ role: string+ /** The element's `#id` or `[data-testid]`, when it has one. Absent otherwise+ * — address the element by its `ref`. */+ selector?: string+ /** Current value of a form control, truncated. */+ value?: string+}++/** An element that is still itself but no longer reads the same.+ *+ * Only the fields that actually moved are present. Role and selector are+ * absent by construction rather than by omission: a change in either would+ * mean this is a different element, which the re-bind ladder would have+ * refused to match in the first place. */+export interface PreviewElementChange {+ /** Present only when the control's availability flipped. */+ disabled?: boolean+ label?: string+ ref: string+ value?: string+}++/** What changed on the page since the last look. Sent instead of the whole+ * inventory once the agent has a baseline for the page — see `survey`. */+export interface PreviewActDelta {+ /** Elements seen for the first time, in full. */+ added?: PreviewElement[]+ /** Same handle, new label/value/disabled state — and nothing else. */+ changed?: PreviewElementChange[]+ /** Handles that are gone from the page. */+ removed?: string[]+ /** Handles whose element was destroyed and recreated by a re-render. The+ * handle still works; nothing about them needs re-reading. */+ rebound?: string[]+ /** How many handles were on the page and untouched. */+ same?: number+}++/** A normalized action. `kind` is the verb; the rest is per-verb payload. */+export interface PreviewActAction {+ /** scroll distance in px. Defaults to ~90% of the viewport height. */+ amount?: number+ key?: string+ /** `pin`/`unpin`/`hold` never reach the engine — they resolve their targets+ * through `locate`/`elements` and then talk to the overlay — but they arrive+ * on the same wire. */+ kind:+ | 'click'+ | 'elements'+ | 'hold'+ | 'hover'+ | 'locate'+ | 'pin'+ | 'press'+ | 'scroll'+ | 'strobe'+ | 'type'+ | 'unpin'+ /** locate: also give the target keyboard focus, for a key press that must not+ * be preceded by a click (which would activate the control instead). */+ focus?: boolean+ /** elements: answer with the whole inventory rather than a delta. */+ full?: boolean+ /** Cap on the returned inventory. */+ max?: number+ ref?: string+ selector?: string+ /** type: press Enter (and submit the owning form) after entering text. */+ submit?: boolean+ text?: string+ to?: 'bottom' | 'top'+}++export interface PreviewActResult {+ /** What the action landed on, for the agent's own log. */+ acted?: string+ /** What moved since the last look. Present INSTEAD of `elements` once the+ * agent holds a baseline for this page. */+ delta?: PreviewActDelta+ /** The full inventory. Sent on the first look at a page, and again whenever+ * the page changed too much for a delta to be the cheaper answer. */+ elements?: PreviewElement[]+ error?: string+ note?: string+ /** Viewport centre of a located target, for aiming real pointer input at it. */+ point?: { x: number; y: number }+ success: boolean+ title?: string+ /** locate: whether the target actually takes typed text. */+ typable?: boolean+ /** Live document URL after the action — a change means it navigated. */+ url?: string+}++/** One element the agent has a handle on, remembered across actions. */+export interface PreviewActBinding {+ el: Element+ /** What it read as last time. Kept field by field rather than as one hash so+ * a change can be reported as only the part that moved. */+ label: string+ /** The accessible name at mint time, for re-finding this element after a+ * re-render destroys and recreates its node. */+ name: string+ /** Whether the control was unavailable last time. */+ off: boolean+ /** Nearest-landmark path plus position among same-role siblings. */+ path: string+ ref: string+ role: string+ /** `id` / `name` / `data-testid` / `aria-label`, if the page provides one.+ * The strongest re-bind signal there is, and the only one a rewrite of the+ * surrounding markup cannot disturb. */+ stable: string+ value: string+}++/** Where the surface keeps what it knows between actions (a window global in+ * the preview page), so a handle still means something on the next call. */+export interface PreviewActHolder {+ /** Target of the action in flight, for the watch overlay to draw onto. */+ aimed?: Element | null+ /** Every handle minted on this page, live or not yet retired. */+ book?: PreviewActBinding[]+ /** Next disambiguating suffix per ref stem, so two "Edit" buttons become+ * `btn-edit` and `btn-edit-1`. Never rewound: a retired handle's name is+ * not handed to a different element later in the same page. */+ coined?: Record<string, number>+ /** The on-screen subset, for the overlay to outline. Diverges from `nodes` in+ * both directions: it drops what is below the fold, and it is not capped at+ * the inventory's size. */+ field?: Element[]+ nodes?: Element[]+ /** URL the snapshot was taken on; a navigation retires every handle. */+ url?: string+}diff --git a/apps/desktop/src/lib/preview-act/visibility.ts b/apps/desktop/src/lib/preview-act/visibility.tsnew file mode 100644index 00000000000..d1a3ffe697b--- /dev/null+++ b/apps/desktop/src/lib/preview-act/visibility.ts@@ -0,0 +1,148 @@+/**+ * VISIBILITY — is this element actually there, and is it on screen.+ *+ * Two different questions with two different answers. `visible` decides whether+ * an element belongs in the inventory at all: painted, not screen-reader-only,+ * not buried under something else. `onScreen` decides whether the overlay+ * should draw a box around it, which is a narrower question — the inventory+ * includes things below the fold the agent will scroll to, and the overlay+ * does not.+ *+ * Most of what is here exists because of a specific class of thing that looks+ * interactive and is not: the sr-only recipe in both its spellings, a control+ * inside an `aria-hidden` subtree, a 1px box parked at the document origin, an+ * element under a cookie wall. Each one of those, admitted, is an action the+ * agent takes that visibly does nothing.+ *+ * SELF-CONTAINMENT: see `naming.ts`. Same contract, same reason for the+ * factory shape.+ */++export interface VisibilityKit {+ /** In the viewport right now, and not a page-sized wrapper. */+ onScreen(el: Element): boolean+ /** Painted at all, ancestors included. */+ shown(el: Element): boolean+ /** Painted, reachable, big enough to aim at, and not buried. */+ visible(el: Element): boolean+}++/** Build the visibility tests against one document and its view. */+export function visibilityKit(doc: Document, win: null | Window): VisibilityKit {+ /** On screen right now, and worth drawing a box around. The field is strictly+ * viewport-bound: a mark past the fold is one nobody can see, it spends the+ * budget that should have gone to what IS on screen, and the ones parked far+ * off to the side were landing as stray labels in the corner. */+ const onScreen = (el: Element): boolean => {+ if (!win) {+ return false+ }++ const box = el.getBoundingClientRect()++ if (box.right <= 0 || box.bottom <= 0 || box.left >= win.innerWidth || box.top >= win.innerHeight) {+ return false+ }++ // Page-sized wrappers. Outlining one draws a rectangle around the whole+ // screen, which says nothing and frames everything inside it as if it+ // mattered. Full-width banners are fine — it takes both dimensions.+ return !(box.width >= win.innerWidth * 0.95 && box.height >= win.innerHeight * 0.9)+ }++ /** Painted at all, ancestors included. */+ const shown = (el: Element): boolean => {+ // Folds in display/visibility/opacity/content-visibility inherited from an+ // ANCESTOR, which this element's own computed style does not report: inside+ // a parent at opacity 0, every child still reads back opacity 1.+ const seen = (el as Element & { checkVisibility?: (opts: object) => boolean }).checkVisibility++ if (typeof seen === 'function' && !seen.call(el, { checkOpacity: true, checkVisibilityCSS: true })) {+ return false+ }++ const style = win && win.getComputedStyle ? win.getComputedStyle(el) : null++ if (style) {+ if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') {+ return false+ }++ // The screen-reader-only recipe, both spellings: the deprecated `clip` and+ // the modern `clip-path: inset(50%)`. Neither exists for any purpose other+ // than hiding something visually while keeping it focusable.+ if ((style.clip && style.clip !== 'auto') || (style.clipPath || '').indexOf('inset(50%') !== -1) {+ return false+ }+ }++ return true+ }++ const visible = (el: Element): boolean => {+ if (!shown(el)) {+ return false+ }++ // Things the page itself declares are not for interacting with, neither of+ // which shows up in a computed style or a bounding box: an aria-hidden+ // subtree is invisible to every other assistive client, and `inert` cannot+ // be clicked at all. Disabled controls are deliberately NOT filtered here —+ // they stay in the inventory so the agent is told a button is disabled+ // rather than hunting for one that appears not to exist.+ if (el.closest('[aria-hidden="true"], [inert]')) {+ return false+ }++ const rect = el.getBoundingClientRect()++ // Below the fold is fine — we scroll to it. Off to the LEFT or ABOVE is the+ // other half of the sr-only trick (`left: -9999px`, `top: -9999px`) and+ // scrolling never brings those back.+ if (rect.right <= 0 || rect.bottom <= 0) {+ return false+ }++ // Bigger than the 1px box sr-only collapses to. Nothing a person can aim at+ // is 2px across, and admitting those is what put the cursor in the corner:+ // they cluster at the document origin, so they sort to the FRONT of the+ // inventory and the agent reaches for one as the first thing on the page.+ if (rect.width < 3 || rect.height < 3) {+ return false+ }++ // Occlusion, and the last filter for a reason: everything above is cheap+ // and this one forces layout. If the middle of the element is on screen,+ // whatever the browser reports at that point has to BE this element — its+ // own descendant (an icon inside a link) or the box it paints inside are+ // fine, anything else means it is buried under a sticky header, a cookie+ // wall, or a transparent layer the page put on top. Those are exactly the+ // targets the agent aims at and then misses. Elements below the fold cannot+ // be hit-tested from here, so they are taken on trust and land back in this+ // function after the scroll.+ const midX = rect.left + rect.width / 2+ const midY = rect.top + rect.height / 2++ const under = (doc as Document & { elementFromPoint?: (x: number, y: number) => Element | null })+ .elementFromPoint++ if (+ typeof under === 'function' &&+ win &&+ midX >= 0 &&+ midY >= 0 &&+ midX < win.innerWidth &&+ midY < win.innerHeight+ ) {+ const over = under.call(doc, midX, midY)++ if (!over || !(over === el || el.contains(over) || over.contains(el))) {+ return false+ }+ }++ return true+ }++ return { onScreen, shown, visible }+}