UX refactor (#52)

* feat(ltl): bound fields on AlwaysFormula and named thunks

Add StepBound/Duration/Deadline to AlwaysFormula as the dual of bounded
Eventually, give ThunkFormula a Name for stable identity, add ThunkNamed,
and surface both in describe() and MarshalJSON.

* feat(ltl): negation normal form pass

nnf/pushNot rewrite a formula so every Not wraps only a Thunk or Error
leaf, dualizing Always<->Eventually and preserving bounds.

* feat(ltl): NNF in NewEvaluator, bounded-always, Finalize, collapse

Apply nnf on construction, reduce bounded Always symmetric to bounded
Eventually (vacuous holds once the window closes), add Finalize to
resolve undischarged liveness obligations to Violated at run end, and
collapse structurally-identical pending obligations.

* test(ltl): property-based NNF laws

Lock double-negation identity, Always/Eventually duality with bound
preservation, leaf pushdown, and not(always true) reaching Violated.

* test(ltl): Finalize, bounded eventually, latch, collapse

Property tests for monotonic violation latch and eventually-within
violating iff n consecutive false, plus Finalize and collapse cases.

* feat(inspect): within clause on always residual node

A negated bounded eventually serializes as a bounded always; render its
bound instead of dropping it.

* feat(ltl): witness violations and (bool,error) predicate thunks

* test(ltl): migrate thunk call sites to (bool,error)

* feat(ltl): flag thrown-predicate witnesses with IsError

* refactor(verifier): replace predicate err side-channel with violation witness

* test(verifier): witness API for thrown predicates

* feat(trace): witnesses map and skipped-verification marker on Step

* feat(runner): thread violation witnesses, finalize, skip marker into trace

* test(ltl): lock violation witness reason, IsError, and step

* test(verifier): finalize surfaces unmet eventually with witness

* fix(ltl): eliminate implies and bounded-always false-negatives

Rewrite a -> b to (not a) or b in NNF so a pending temporal antecedent
can no longer defer the whole implication and drop a consequent that was
false at the current step. Carry a pending inner past a bounded-Always
window close instead of dropping it to holds, so a deferred obligation is
resolved by a later step or Finalize.

* test(ltl): lock implies and bounded-always false-negative regressions

* fix(web-runtime): seed PRNG for reproducible runs and align weighted pick

* feat(testrun): inject seed into web bundle via SANDERLING_SEED define

* test: cover web-runtime seeded PRNG, weighted pick, and seed define wiring

* test(spec): add Go math/rand/v2 PCG oracle and golden fixture

* feat(spec): bit-exact PCG port of Go math/rand/v2

* test(spec): assert pcg.ts matches the PCG golden fixture

* feat(spec): shared input corpus and press-key pools

* feat(spec): action-tree types and Host interface

* feat(spec): verb support matrix and warn-once helper

* feat(spec): deterministic shared action picker

* test(spec): verb matrix and warn-once semantics

* test(spec): picker draw-order and determinism

* refactor(spec): actions.ts returns pure GeneratorNode data trees

* refactor(spec): wire from() sampling through the picker rng

* feat(spec): shared runtime-entry installs next-action over pick.ts

* feat(spec): export LongPress/Scroll/longPresses/scrolls factories

* test(spec): assert data-tree shapes for action factories

* test(spec): runtime-entry serializeAction wire-contract round-trip

* refactor(spec): bridge data-tree nodes to the legacy goja picker tags

* fix(spec): web runtime walks the spec's globalThis.actions data tree

* test(spec): tolerate legacy bridge fields on builtin nodes

* refactor(spec): installRuntime accepts a lazy root resolver

The web bundle imports the runtime before the spec, so the action root
on globalThis.actions only exists after the spec evaluates. Accept a
function form so the goja and web hosts resolve the root per tick.

* refactor(spec): web-runtime becomes the WEB Host, delegates to shared picker

Delete the duplicate picker (resolveGenerator/pickWeighted/randomTap/
randomInput/randomSwipe/randomPressKey/pickFromArray, the mulberry32 PRNG,
and the snake_case serializeAction) plus the __sanderling__ action factory
binds. web-runtime now implements Host (platform/seedHi/seedLo from the
injected 64-bit seed via BigInt, queryCandidates over the live DOM with a
per-tick cache, reportUnsupported) and calls installRuntime so both engines
run pick.ts over the same Pcg. Swipe/longPress/scroll follow the verbs.ts
matrix instead of silently returning null. Keeps the DOM helpers (selector
translation, queryElement, elementHandle, buildState, sanitize, extractors)
and the global locking. Net -214 lines (741 -> 527).

* test(spec): cover the WEB Host surface and seed precision

Replace the deleted-picker tests with Host coverage: platform()==web,
seedHi() parsing a 64-bit seed without Number precision loss, seedLo()==0,
reportUnsupported warning, the installed next-action/extractor globals, and
queryCandidates verb routing + per-tick caching over a querySelectorAll stub.

* refactor(spec): picker emits native selector + scroll endpoints, setup precedence

* feat(spec): goja runtime entry wires the shared picker over the Go host

* feat(bundler): optional RuntimeFile prepends a runtime-entry import via stdin

* feat(testrun): bundle the goja runtime entry so the verifier runs the shared picker

* refactor(spec): drop the legacy goja bridge fields from action factories

* feat(spec): serialize selector-only string targets for the runner to re-resolve

* refactor(verifier): one DecodeAction reads the unified flat wire contract

* refactor(verifier): goja host + shared picker replace the duplicate Go picker

* refactor(runner): decode V8 actions via the unified DecodeAction; wire goja runtime

* test(verifier): author specs through the shared picker path

* test(runner): bundle authored specs with the goja runtime entry

* feat(verifier): collect unsupported verbs for the run report

* refactor(runner): collapse WebDriver forks behind ActionSource/ExtractorSource

* feat(testrun): surface unsupported verbs in run report

* test(verifier): cross-runtime goja/node parity gate on the shared picker

* test(verifier): unsupported verbs collected deduped in first-seen order

* test(runner): summary reports no unsupported verbs on a clean run

* test(spec): golden-fixture cross-runtime parity gate for the node picker

Replace the env-driven parity harness with a shared scenario module and a
committed golden the node picker asserts independently. The goja side asserts
the same golden, so neither runtime invokes the other at test time.

* test(verifier): assert goja picker against the same cross-runtime golden

Drop the node-subprocess coupling: the goja side now installs a stub
__sanderlingHost__ with the fixed candidate list and asserts the committed
golden, matching pkg/spec/test/parity.test.ts.

* refactor(spec): rename pressKey generator export to pressKeys

* refactor(spec): update barrel re-exports for pressKeys

* test(spec): update pressKeys generator export name

* docs(spec): rename pressKey generator to pressKeys

* refactor(spec): extract samplerRng into shared sampler-rng module

* feat(spec): add fluent seeded value generators (strings/integers/emails/edgeCaseText)

* test(spec): cover fluent value generators determinism and chaining

* refactor(bundler): inject globalThis trailer from spec named exports

* refactor(bundler): reuse registration trailer in web bundler

* test(bundler): cover named-export globalThis registration

* feat(spec): add named() to Extracted handle type

* feat(web-runtime): named() and cross-extractor read guard

* feat(verifier): named() and cross-extractor read guard in goja

* test(verifier): cross-extractor read guard and named()

* test(web-runtime): export runtime and extractors for tests

* test(web-runtime): named() and cross-extractor read guard

* refactor(folio): drop manual globalThis trailer (bundler injects it)

* refactor(folio): seed txn amounts via integers().between(1,500)

* refactor(folio-web): drop manual globalThis trailer (bundler injects it)

* fix(folio-web): seed card/txn-type selection via from().generate() for reproducible runs

* refactor(folio-web): weight valid generators against edgeCaseText for names/amounts

* refactor(folio-web): name extractors so violation witnesses are readable

* fix(web-runtime): propagate extractor getter throws and unpoison locked global

Stop swallowing getter errors in evaluateExtractors so the cross-extractor read guard aborts loudly, matching goja's PushSnapshot. Make the __sanderling__ lock configurable (still non-writable) so a shared test process can reinstall a fake.

* test(spec): install fake runtime via defineProperty to survive locked global

* test(web-runtime): assert uncaught cross-extractor read aborts evaluateExtractors

* feat(runner): add MaxSteps bound to Options

* test(runner): MaxSteps stops after exactly N steps

* test(driverpb): drop proto getter round-trip tautology

* test(sidecar): drop stub-mode placeholder tautology tests

* test(mock): drop default-field-value assertion test

* test(ltl): drop Verdict.String tautology tests

* refactor(runner): extract RenderSummary for snapshot testing

* test(runner): golden snapshots for trace stream and violation summary

* feat(web-runtime): capture uncaught errors into state.exceptions

* test(integration): add throwing and counter web fixtures

* test(integration): add specs for the web fixtures

* test(integration): drive web fixtures through the real pipeline in headless Chrome

* chore(make): add test-browser target for the Chrome-driven suite

* ci: run the Chrome-driven browser suite in a separate job

* refactor(test): relocate browser suite to test/browser

* refactor(permissions): delete dead internal/permissions package

* refactor(test): rename package to browser_test

* refactor(sidecarassets): rename internal/sidecar to internal/sidecarassets

* chore(make): point test-browser at test/browser

* docs(decisions): record internal/permissions deletion

* refactor(doctor): use sidecarassets package

* refactor(testrun): use sidecarassets package

* fix(test): resolve testdata relative to browser_test.go

* refactor(verifier): remove dead __sanderlingIndex compat alias

* refactor(bundler): use encoding/json for JS string literals

* docs(action-space): use vendor-neutral native driver wording

* refactor(hierarchy): scrub backend tool name from comments

* refactor(driver): scrub backend tool name from comments

* refactor(driver): add DoubleTap and DoubleTapSelector to DeviceDriver

* refactor(sidecar): implement DoubleTap with the sub-100ms inter-tap gap

* refactor(chrome): implement DoubleTap as two taps with the gap

* refactor(mock): record DoubleTap and DoubleTapSelector actions

* refactor(runner): delegate double-tap to driver, drop gesture timing

* test(runner): assert double-tap delegates to driver DoubleTap

* docs(cmd): add package docs to CLI and developer tools

* docs(driver): add package docs to driver interface and chrome backend

* docs(driver): add package docs to mock and sidecar backends

* docs(platform): add package docs to android and ios device prep

* docs: add package docs to bundler and inspect

* docs(ltl): add package doc to temporal logic evaluator

* docs: add package docs to runner and testrun pipeline

* docs: add package docs to trace and verifier

* docs(sidecarassets): add package doc for embedded JAR loader

* fix(chrome): add disable-dev-shm-usage so Chrome starts in CI

* test(chrome): gate real-Chrome driver tests behind the browser tag

* chore(make): run chrome driver tests in the browser job

* fix(web-runtime): guard global error listeners for non-browser hosts

The module registered window error/unhandledrejection listeners at top
level, which threw under Node (the spec-api test runner) where
globalThis.addEventListener is absent. Register only when the API exists;
the real browser run is unaffected.

* ci(browser): re-enable unprivileged user namespaces for headless Chrome

ubuntu-latest moved to 24.04, whose AppArmor restriction on unprivileged
user namespaces stops headless Chrome from opening its DevTools socket
even with --no-sandbox, surfacing as the driver's 'websocket url timeout'.
Relax the sysctl for the job and add a direct launch check so a future
breakage shows Chrome's own stderr rather than an opaque driver timeout.

* ci(browser): pin stable Chrome for the driver tests

setup-chrome's default latest pulled a dev Chromium (150) whose remote
debugging socket never came up under chromedp, while plain --dump-dom
worked. Pin the stable channel, which the driver is tested against.

* feat(defaults): add scroll and rebalance action weights

Use relative-integer weights (taps/typing co-primary 100, scrolls 50,
swipes 25, doubleTaps 10); the picker normalizes by their total. Adds
scrolls to defaultActions as a first-class reveal behavior.

* feat(defaults): trim scroll action weight wiring

* fix(build): point sidecar jar ignore and embed paths at sidecarassets

* test(defaults): drop stale longPresses re-export assertion

longPresses is opt-in vocabulary, no longer re-exported from
defaults/actions.ts since e0d3b20; its builtin resolution is already
covered by api.test.ts. Trim the defaults test to scrolls, which is an
actual default export.

* fix(chrome): raise DevTools websocket read timeout to 60s

Chrome cold-start on a loaded CI runner can exceed chromedp's 20s
default for reading the DevTools websocket URL, flaking the browser
tests with "websocket url timeout reached". Give launch more headroom.
This commit is contained in:
pj authored and GitHub committed 2026-06-02 09:52:53 +05:30
1 parent 88db9653e5
commit c5bb176be8
98 files changed
+6898 -2227

No files matched your search

+90
View File
@@ -0,0 +1,90 @@
// Shared action-generator tree and host interface for W2 approach B.
//
// Both engines (the goja verifier and the V8 web runtime) walk ONE tree with
// ONE picker (pick.ts), drawing through the shared PCG (pcg.ts). This module
// only declares the data shapes; pick.ts holds the traversal and the parity
// contract for draw order.
//
// Element references never cross the V8/host boundary: builtin generators emit
// an ActionDescriptor whose target is already a resolved Point (plus an
// optional selector on native), so the host serializer never has to chase a
// live element handle.
import type {
AccessibilityElement,
AttrSelector,
Direction,
Point,
SelectorPath,
} from "./types.ts";
// Target is any target shape an action may carry before serialization. Author
// specs supply a string/selector/element; builtin generators resolve to a
// {x, y} Point (so no element handle crosses the V8/host boundary).
export type Target = string | AttrSelector | SelectorPath | AccessibilityElement | Point;
// BuiltinVerb names a leaf generator backed by host-enumerated candidates
// rather than author-supplied actions.
export type BuiltinVerb =
| "taps"
| "doubleTaps"
| "longPresses"
| "scrolls"
| "typing"
| "swipes"
| "waitOnce"
| "pressKeys";
// ActionDescriptor is the camelCase author shape an action takes before the
// engine serializes it to the wire format. Mirrors the factory return shapes
// in actions.ts (Tap/DoubleTap/LongPress {on}; Scroll {direction; in?};
// InputText {into; text}; Swipe {from; to; durationMillis?}; PressKey {key};
// Wait {durationMillis}).
export type ActionDescriptor =
| { kind: "Tap"; on: Target }
| { kind: "DoubleTap"; on: Target }
| { kind: "LongPress"; on: Target }
// Scroll carries the author target (in) for spec-written scrolls; the builtin
// generator additionally pre-computes the swipe from/to so the wire contract
// carries a real gesture rather than a zero-length one.
| { kind: "Scroll"; direction: Direction; in?: Target; from?: Target; to?: Target }
| { kind: "InputText"; into: Target; text: string }
| { kind: "Swipe"; from: Target; to: Target; durationMillis?: number }
| { kind: "PressKey"; key: string }
| { kind: "Wait"; durationMillis: number };
// GeneratorNode is a node in the action-generator tree.
// weighted: probabilistic choice over child nodes, scanned ascending.
// actions: author callback returning a list to pick uniformly from.
// builtin: host-backed leaf identified by a verb.
export type GeneratorNode =
| { kind: "weighted"; branches: ReadonlyArray<readonly [number, GeneratorNode]> }
| { kind: "actions"; generate: () => ActionDescriptor[] }
| { kind: "builtin"; verb: BuiltinVerb };
// Candidate is one host-enumerated target for a builtin verb. The host
// resolves geometry (and a native selector) so no element handle crosses into
// the picker. width/height let swipe/scroll size a gesture off the element.
export interface Candidate {
x: number;
y: number;
selector?: string;
width?: number;
height?: number;
}
// Host is the platform backing the picker draws against. In this foundation
// workflow only the interface is defined and exercised against a stub; the
// goja and DOM implementations land in the rewire workflow.
export interface Host {
platform(): "android" | "ios" | "web";
// queryCandidates returns the host-enumerated targets for a verb, in a
// deterministic order. The picker indexes into this list with the PCG, so
// the order is part of the parity contract.
queryCandidates(verb: BuiltinVerb): Candidate[];
// reportUnsupported is invoked at most once per verb@platform (see verbs.ts)
// when a verb has no support on this platform.
reportUnsupported(verb: BuiltinVerb): void;
seedHi(): bigint;
seedLo(): bigint;
}
+48 -45
View File
@@ -1,7 +1,12 @@
// Author-facing action factories. Each returns plain GeneratorNode /
// ActionDescriptor DATA (see action-tree.ts); the shared picker (pick.ts) walks
// the tree and the runtime entry (runtime-entry.ts) serializes the result. No
// factory forwards to globalThis.__sanderling__ anymore: the same data tree
// drives both the goja verifier and the V8 web runtime.
import type {
AccessibilityElement,
Action,
ActionGenerator,
Direction,
DoubleTapAction,
InputTextAction,
@@ -16,16 +21,24 @@ import type {
WaitAction,
WeightedEntry,
} from "./types.ts";
import type { ActionDescriptor, BuiltinVerb, GeneratorNode } from "./action-tree.ts";
import { getSamplerRng } from "./sampler-rng.ts";
export function actions(generator: () => Action[]): ActionGenerator {
return globalThis.__sanderling__.actions(generator);
export { setSamplerRng } from "./sampler-rng.ts";
function builtinNode(verb: BuiltinVerb): GeneratorNode {
return { kind: "builtin", verb };
}
export function actions(generator: () => Action[]): GeneratorNode {
return { kind: "actions", generate: generator as () => ActionDescriptor[] };
}
export function whenRoute(
routeExtractor: { readonly current: string | null },
routes: string | readonly string[],
body: () => Action[],
): ActionGenerator {
): GeneratorNode {
const allowed = typeof routes === "string" ? [routes] : routes;
return actions(() => {
const current = routeExtractor.current;
@@ -34,38 +47,45 @@ export function whenRoute(
});
}
export function weighted(...entries: WeightedEntry[]): ActionGenerator {
return globalThis.__sanderling__.weighted(...entries);
export function weighted(...entries: WeightedEntry[]): GeneratorNode {
return { kind: "weighted", branches: entries };
}
export function from<T>(items: readonly T[]): Sampler<T> {
return globalThis.__sanderling__.from(items);
return {
generate(): T {
if (items.length <= 1) return items[0] as T;
const rng = getSamplerRng();
const index = rng ? rng.intN(items.length) : 0;
return items[index] as T;
},
};
}
export function Tap(parameters: { on: string | AccessibilityElement }): TapAction {
return globalThis.__sanderling__.tap(parameters);
return { kind: "Tap", on: parameters.on };
}
export function DoubleTap(parameters: { on: string | AccessibilityElement }): DoubleTapAction {
return globalThis.__sanderling__.doubleTap(parameters);
return { kind: "DoubleTap", on: parameters.on };
}
export function LongPress(parameters: { on: string | AccessibilityElement }): LongPressAction {
return globalThis.__sanderling__.longPress(parameters);
return { kind: "LongPress", on: parameters.on };
}
export function Scroll(parameters: {
direction: Direction;
in?: string | AccessibilityElement;
}): ScrollAction {
return globalThis.__sanderling__.scroll(parameters);
return { kind: "Scroll", direction: parameters.direction, in: parameters.in };
}
export function InputText(parameters: {
into: string | AccessibilityElement;
text: string;
}): InputTextAction {
return globalThis.__sanderling__.inputText(parameters);
return { kind: "InputText", into: parameters.into, text: parameters.text };
}
export function Swipe(parameters: {
@@ -73,44 +93,27 @@ export function Swipe(parameters: {
to: Point | AccessibilityElement;
durationMillis?: number;
}): SwipeAction {
return globalThis.__sanderling__.swipe(parameters);
return {
kind: "Swipe",
from: parameters.from,
to: parameters.to,
durationMillis: parameters.durationMillis,
};
}
export function PressKey(parameters: { key: Key }): PressKeyAction {
return globalThis.__sanderling__.pressKey(parameters);
return { kind: "PressKey", key: parameters.key };
}
export function Wait(parameters: { durationMillis: number }): WaitAction {
return globalThis.__sanderling__.wait(parameters);
return { kind: "Wait", durationMillis: parameters.durationMillis };
}
function builtinGenerator(
name:
| "taps"
| "doubleTaps"
| "longPresses"
| "scrolls"
| "typing"
| "swipes"
| "waitOnce"
| "pressKeys",
): ActionGenerator {
return new Proxy({} as ActionGenerator, {
get(_target, property) {
const runtime = globalThis.__sanderling__[name] as unknown as Record<
string | symbol,
unknown
>;
return runtime[property];
},
});
}
export const taps: ActionGenerator = builtinGenerator("taps");
export const doubleTaps: ActionGenerator = builtinGenerator("doubleTaps");
export const longPresses: ActionGenerator = builtinGenerator("longPresses");
export const scrolls: ActionGenerator = builtinGenerator("scrolls");
export const typing: ActionGenerator = builtinGenerator("typing");
export const swipes: ActionGenerator = builtinGenerator("swipes");
export const waitOnce: ActionGenerator = builtinGenerator("waitOnce");
export const pressKey: ActionGenerator = builtinGenerator("pressKeys");
export const taps: GeneratorNode = builtinNode("taps");
export const doubleTaps: GeneratorNode = builtinNode("doubleTaps");
export const longPresses: GeneratorNode = builtinNode("longPresses");
export const scrolls: GeneratorNode = builtinNode("scrolls");
export const typing: GeneratorNode = builtinNode("typing");
export const swipes: GeneratorNode = builtinNode("swipes");
export const waitOnce: GeneratorNode = builtinNode("waitOnce");
export const pressKeys: GeneratorNode = builtinNode("pressKeys");
+47
View File
@@ -0,0 +1,47 @@
// Single source of truth for the data both action-generator engines draw from:
// the goja verifier (internal/verifier/worker.go) and the V8 web runtime
// (web-runtime.ts). The entries and their ORDER are load-bearing: both engines
// index into these arrays with the shared PCG, so any reordering shifts every
// downstream pick for a given seed and breaks cross-engine reproducibility.
// INPUT_CORPUS is the edge-case string pool the typing builtin draws from to
// stress field parsing: empty, single char, overflow length, unicode, blank /
// whitespace, numeric boundaries, and common injection payloads. It must stay
// byte-for-byte identical to worker.go inputCorpus.
export const INPUT_CORPUS: readonly string[] = [
"",
"a",
"a".repeat(4096),
"🙂🔥💸",
" ",
"\t\n",
"-1",
"999999999999999999999",
"0.0000001",
"1e10",
"'; DROP TABLE--",
"<script>alert(1)</script>",
"../../etc/passwd",
"%s%n",
"NaN",
];
// NATIVE_PRESS_KEYS is the key pool the native (goja) press-key builtin may
// emit. Exploration stays gentle: only "back" is in play today, because
// "home"/"menu" navigate away from the app under test. Kept as an array so the
// matrix can declare native support over the full set without the picker
// hardcoding a single value.
export const NATIVE_PRESS_KEYS: readonly string[] = ["back"];
// WEB_PRESS_KEYS is the key pool the web press-key builtin draws from. Only
// keys with meaningful in-page semantics are included; "back"/"home" would not
// navigate a browser tab the way they navigate a native app.
export const WEB_PRESS_KEYS: readonly string[] = [
"enter",
"tab",
"escape",
"up",
"down",
"left",
"right",
];
+7 -19
View File
@@ -1,24 +1,12 @@
import { doubleTaps, swipes, taps, typing, weighted } from "../actions.ts";
import { doubleTaps, scrolls, swipes, taps, typing, weighted } from "../actions.ts";
import type { ActionGenerator } from "../types.ts";
export { doubleTaps, pressKey, swipes, taps, typing, waitOnce } from "../actions.ts";
// longPresses and scrolls are opt-in generators: not part of defaultActions,
// but authors can include them in their own weighted() set.
export { longPresses, scrolls } from "../actions.ts";
export { doubleTaps, scrolls, swipes, taps, typing } from "../actions.ts";
// A broad exploration generator: tap things, type edge-case values into fields,
// and swipe. Layer it under targeted depth flows so the fuzzer wanders the whole
// app while still driving the paths an author wrote. It deliberately omits the
// hardware back key: backing out past the app's root screen exits the app under
// test, and exploration must stay within the app.
//
// doubleTaps fires the same target twice ~50ms apart inside one step. Real users
// double-tap (image zoom, like-to-favorite, play/pause); without this the fuzzer
// can never produce sub-100ms event spacing, so race-window bugs (debounce gaps,
// in-flight guards, init races) stay unreachable.
export const defaultActions: ActionGenerator = weighted(
[45, taps],
[15, doubleTaps],
[25, typing],
[15, swipes],
[100, taps],
[100, typing],
[50, scrolls],
[25, swipes],
[10, doubleTaps],
);
+23
View File
@@ -0,0 +1,23 @@
// Goja-side runtime entry for the native verifier (internal/verifier).
//
// Go installs globalThis.__sanderlingHost__ (platform/seed/queryCandidates/
// reportUnsupported, implemented over the hierarchy tree in bindings.go) before
// the spec evaluates. This module bundles AFTER the spec, reads that host, and
// wires the shared picker via installRuntime so the goja verifier and the V8 web
// runtime run the SAME pick.ts over the SAME Pcg.
//
// Extractors are driven by Go (bindExtract + PushSnapshot advance current/
// previous against the Go-built `state`), so the extractor callback is a no-op
// here; Go never invokes __sanderlingExtractors__ on native.
import { installRuntime } from "./runtime-entry.ts";
import type { GeneratorNode, Host } from "./action-tree.ts";
const host = (globalThis as { __sanderlingHost__?: Host }).__sanderlingHost__;
if (!host) throw new Error("goja runtime: __sanderlingHost__ not installed");
installRuntime(
host,
() => (globalThis as { actions?: GeneratorNode }).actions ?? null,
() => ({}),
);
+6 -1
View File
@@ -33,14 +33,18 @@ export { always, eventually, next, now } from "./ltl.ts";
export {
DoubleTap,
InputText,
LongPress,
PressKey,
Scroll,
Swipe,
Tap,
Wait,
actions,
doubleTaps,
from,
pressKey,
longPresses,
pressKeys,
scrolls,
swipes,
taps,
typing,
@@ -48,3 +52,4 @@ export {
weighted,
whenRoute,
} from "./actions.ts";
export { edgeCaseText, emails, integers, strings } from "./values.ts";
+96
View File
@@ -0,0 +1,96 @@
// Bit-exact port of Go's math/rand/v2 PCG source wrapped in rand.Rand.
//
// This MUST match `rand.New(rand.NewPCG(hi, lo))` draw-for-draw, because every
// random decision the action picker makes is shared between the goja verifier
// and the V8 web runtime. Divergence here breaks reproducibility across engines.
//
// The golden fixture in test/fixtures/pcg-golden.json is generated by
// internal/_oracle/main.go and pins the exact sequences asserted in pcg.test.ts.
const MASK64 = (1n << 64n) - 1n;
const MUL_HI = 2549297995355413924n;
const MUL_LO = 4865540595714422341n;
const INC_HI = 6364136223846793005n;
const INC_LO = 1442695040888963407n;
const CHEAP_MUL = 0xda942042e4dd58b5n;
const POW53 = 1n << 53n;
// mul64 returns the 128-bit product of two uint64 values split into high and
// low 64-bit halves, matching Go's math/bits.Mul64.
function mul64(a: bigint, b: bigint): { hi: bigint; lo: bigint } {
const product = a * b;
return { hi: (product >> 64n) & MASK64, lo: product & MASK64 };
}
export class Pcg {
private hi: bigint;
private lo: bigint;
constructor(hi: bigint, lo: bigint) {
this.hi = hi & MASK64;
this.lo = lo & MASK64;
}
// next advances the 128-bit LCG state and returns the new (hi, lo) pair.
private next(): { hi: bigint; lo: bigint } {
// 128-bit multiply of state by the 128-bit multiplier, keeping 128 bits.
let { hi, lo } = mul64(this.lo, MUL_LO);
hi = (hi + this.hi * MUL_LO + this.lo * MUL_HI) & MASK64;
// 128-bit add of the increment with carry propagation.
const loSum = lo + INC_LO;
const carry = loSum >> 64n;
lo = loSum & MASK64;
hi = (hi + INC_HI + carry) & MASK64;
this.lo = lo;
this.hi = hi;
return { hi, lo };
}
// uint64 mirrors PCG.Uint64: the DXSM output permutation over next().
uint64(): bigint {
let { hi, lo } = this.next();
hi ^= hi >> 32n;
hi = (hi * CHEAP_MUL) & MASK64;
hi ^= hi >> 48n;
hi = (hi * (lo | 1n)) & MASK64;
return hi & MASK64;
}
// float64 mirrors rand.Rand.Float64: float64(Uint64()<<11>>11) / (1<<53),
// which keeps the low 53 bits and divides by 2^53.
float64(): number {
const bits = this.uint64() & (POW53 - 1n);
return Number(bits) / Number(POW53);
}
// intN mirrors rand.Rand.IntN via uint64n (Lemire with a rejection threshold).
// The host arch is 64-bit, so the 32-bit fast path never applies.
intN(n: number | bigint): number {
const bound = typeof n === "bigint" ? n : BigInt(n);
if (bound <= 0n) {
throw new Error("invalid argument to intN");
}
return Number(this.uint64n(bound));
}
private uint64n(n: bigint): bigint {
if ((n & (n - 1n)) === 0n) {
// n is a power of two: mask the low bits.
return this.uint64() & (n - 1n);
}
let { hi, lo } = mul64(this.uint64(), n);
if (lo < n) {
// Go computes thresh := -n % n in uint64 math, where -n wraps to 2^64 - n.
const threshold = ((1n << 64n) - n) % n;
while (lo < threshold) {
({ hi, lo } = mul64(this.uint64(), n));
}
}
return hi;
}
}
+248
View File
@@ -0,0 +1,248 @@
// The shared deterministic action picker for W2 approach B.
//
// walk() traverses a GeneratorNode tree, and nextAction() wraps it with the
// 16-attempt retry that matches worker.go NextAction. Both engines (the goja
// verifier and the V8 web runtime) run THIS code, drawing through the shared
// Pcg, so a given seed yields an identical action stream on every platform.
//
// PARITY CONTRACT - draw order.
// Every random decision goes through the Pcg in a FIXED, pinned order. Changing
// this order shifts the stream for a seed and breaks cross-engine
// reproducibility, so treat it as load-bearing:
//
// weighted node: ONE float64() draw, then an ASCENDING cumulative scan over
// max(0, weight). (matches worker.go pickWeighted.)
// actions node: the generator runs FIRST (any from(...).generate() inside
// draws intN(itemCount) for >1 items, nothing otherwise),
// THEN if the returned list has >1 entry, ONE intN(len) draw;
// a 0- or 1-element list draws nothing. (pickFromResult.)
// builtin node, per verb, in this exact sequence:
// taps/doubleTaps/longPresses: intN(candidateCount) [1 draw]
// typing: intN(candidateCount), intN(corpusLength)
// swipes: intN(candidateCount),
// 200 + intN(401) magnitude, intN(4) direction
// scrolls: intN(candidateCount), intN(4) direction
// pressKeys: intN(keyCount)
// waitOnce: no draw
//
// Builtin targets are resolved to a {x, y} Point by the host BEFORE the picker
// sees them, so no element handle crosses into this module.
import type { Pcg } from "./pcg.ts";
import type {
ActionDescriptor,
BuiltinVerb,
GeneratorNode,
Host,
} from "./action-tree.ts";
import type { Direction, Point } from "./types.ts";
import { INPUT_CORPUS, NATIVE_PRESS_KEYS, WEB_PRESS_KEYS } from "./corpus.ts";
import { setSamplerRng } from "./sampler-rng.ts";
import { supports, warnUnsupportedOnce } from "./verbs.ts";
// SWIPE_MIN_MAGNITUDE / SWIPE_MAGNITUDE_SPAN reproduce worker.go's
// `200 + rng.IntN(401)` swipe distance in pixels (200..600 inclusive).
const SWIPE_MIN_MAGNITUDE = 200;
const SWIPE_MAGNITUDE_SPAN = 401;
const SWIPE_DURATION_MILLIS = 250;
const DIRECTIONS: readonly Direction[] = ["up", "down", "left", "right"];
const MAX_RETRIES = 16;
// nextAction resolves an action for the current step, retrying walk() up to 16
// times when it yields null (matches worker.go NextAction). Returns null when
// every attempt comes up empty.
export function nextAction(
root: GeneratorNode,
rng: Pcg,
host: Host,
): ActionDescriptor | null {
for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
const action = walk(root, rng, host);
if (action !== null) return action;
}
return null;
}
// walk resolves a single node to an ActionDescriptor or null.
export function walk(
node: GeneratorNode,
rng: Pcg,
host: Host,
): ActionDescriptor | null {
switch (node.kind) {
case "weighted":
return walkWeighted(node.branches, rng, host);
case "actions": {
// Expose the picker's rng to from(...).generate() calls inside the
// generator so author sampling shares this single deterministic stream.
setSamplerRng(rng);
try {
return walkActions(node.generate(), rng);
} finally {
setSamplerRng(null);
}
}
case "builtin":
return walkBuiltin(node.verb, rng, host);
}
}
function walkWeighted(
branches: ReadonlyArray<readonly [number, GeneratorNode]>,
rng: Pcg,
host: Host,
): ActionDescriptor | null {
let total = 0;
for (const [weight] of branches) total += Math.max(0, weight);
if (total <= 0) return null;
const draw = rng.float64() * total;
let cumulative = 0;
for (const [weight, child] of branches) {
cumulative += Math.max(0, weight);
if (draw < cumulative) return walk(child, rng, host);
}
// Floating-point slack: draw can equal total. Fall to the last branch,
// matching worker.go's trailing `return generators[length-1]`.
const last = branches[branches.length - 1];
return last ? walk(last[1], rng, host) : null;
}
function walkActions(
generated: ActionDescriptor[],
rng: Pcg,
): ActionDescriptor | null {
if (generated.length === 0) return null;
if (generated.length === 1) return generated[0] ?? null;
return generated[rng.intN(generated.length)] ?? null;
}
function walkBuiltin(
verb: BuiltinVerb,
rng: Pcg,
host: Host,
): ActionDescriptor | null {
if (!supports(verb, host.platform())) {
warnUnsupportedOnce(host, verb);
return null;
}
if (verb === "waitOnce") {
return { kind: "Wait", durationMillis: 500 };
}
if (verb === "pressKeys") {
return walkPressKey(rng, host);
}
const candidates = host.queryCandidates(verb);
if (candidates.length === 0) return null;
const picked = candidates[rng.intN(candidates.length)];
if (!picked) return null;
const point: Point = { x: picked.x, y: picked.y };
switch (verb) {
case "taps":
return tapDescriptor("Tap", point, picked.selector);
case "doubleTaps":
return tapDescriptor("DoubleTap", point, picked.selector);
case "longPresses":
return tapDescriptor("LongPress", point, picked.selector);
case "typing": {
const text = INPUT_CORPUS[rng.intN(INPUT_CORPUS.length)] ?? "";
return { kind: "InputText", into: withSelector(point, picked.selector), text };
}
case "swipes":
return buildSwipe(point, rng);
case "scrolls": {
const direction = DIRECTIONS[rng.intN(DIRECTIONS.length)] ?? "down";
return buildScroll(point, direction, picked, rng);
}
}
}
// withSelector attaches a native selector to a resolved Point so the runner can
// re-resolve the target by id/text. The web host omits it (point-only).
function withSelector(point: Point, selector?: string): Point {
if (selector === undefined) return point;
return { ...point, selector } as Point;
}
function tapDescriptor(
kind: "Tap" | "DoubleTap" | "LongPress",
point: Point,
selector?: string,
): ActionDescriptor {
return { kind, on: withSelector(point, selector) } as ActionDescriptor;
}
// buildScroll lowers a scroll to a swipe over the container, matching
// worker.go's geometry: the gesture drags opposite the named content motion,
// magnitude 40% of the container extent. Missing width/height (web root) yields
// a zero-length endpoint, which the runner re-derives from container bounds.
function buildScroll(
from: Point,
direction: Direction,
candidate: { width?: number; height?: number },
_rng: Pcg,
): ActionDescriptor {
const width = candidate.width ?? 0;
const height = candidate.height ?? 0;
let toX = from.x;
let toY = from.y;
switch (direction) {
case "down":
toY = from.y - Math.trunc((4 * height) / 10);
break;
case "up":
toY = from.y + Math.trunc((4 * height) / 10);
break;
case "left":
toX = from.x + Math.trunc((4 * width) / 10);
break;
case "right":
toX = from.x - Math.trunc((4 * width) / 10);
break;
}
return {
kind: "Scroll",
direction,
in: from,
from,
to: { x: Math.max(0, toX), y: Math.max(0, toY) },
} as ActionDescriptor;
}
function walkPressKey(rng: Pcg, host: Host): ActionDescriptor | null {
const keys = host.platform() === "web" ? WEB_PRESS_KEYS : NATIVE_PRESS_KEYS;
if (keys.length === 0) return null;
const key = keys[rng.intN(keys.length)] ?? keys[0];
if (key === undefined) return null;
return { kind: "PressKey", key };
}
function buildSwipe(from: Point, rng: Pcg): ActionDescriptor {
const magnitude = SWIPE_MIN_MAGNITUDE + rng.intN(SWIPE_MAGNITUDE_SPAN);
let toX = from.x;
let toY = from.y;
switch (rng.intN(4)) {
case 0:
toY = from.y - magnitude;
break;
case 1:
toY = from.y + magnitude;
break;
case 2:
toX = from.x - magnitude;
break;
case 3:
toX = from.x + magnitude;
break;
}
return {
kind: "Swipe",
from,
to: { x: Math.max(0, toX), y: Math.max(0, toY) },
durationMillis: SWIPE_DURATION_MILLIS,
};
}
+155
View File
@@ -0,0 +1,155 @@
// The single next-action entry shared by both engines (goja verifier and V8 web
// runtime). installRuntime wires the spec's root generator, the shared Pcg, and
// a Host into the globals the host invokes each tick:
// __sanderlingNextAction__() -> one serialized action (unified wire contract)
// __sanderlingExtractors__() -> the engine's extractor snapshot
//
// Both engines call THIS picker over the SAME Pcg, so a given seed yields an
// identical action stream by construction.
import { Pcg } from "./pcg.ts";
import { nextAction, walk } from "./pick.ts";
import type { ActionDescriptor, GeneratorNode, Host } from "./action-tree.ts";
import type { Point } from "./types.ts";
// SerializedAction is the flat, camelCase wire shape JS emits and Go decodes
// (ONE decoder on each side). Builtin targets are already resolved to a Point,
// and serializeAction collapses author targets that carry {x, y}; a target that
// does not resolve to coordinates drops the action (returns null).
export type SerializedAction =
| { kind: "Tap" | "DoubleTap" | "LongPress"; x: number; y: number; selector?: string }
| { kind: "InputText"; x: number; y: number; text: string; selector?: string }
| { kind: "Swipe"; fromX: number; fromY: number; toX: number; toY: number; durationMillis: number }
| {
kind: "Scroll";
direction: string;
fromX: number;
fromY: number;
toX: number;
toY: number;
durationMillis: number;
}
| { kind: "PressKey"; key: string }
| { kind: "Wait"; durationMillis: number };
const DEFAULT_SWIPE_DURATION = 250;
// pointOf resolves a target to {x, y, selector?}. Builtins and resolved ax
// elements carry numeric x/y; a bare selector string carries no geometry, so it
// serializes with (0, 0) and the selector, leaving the native runner to
// re-resolve coordinates by id/text. A target with neither shape (null, an
// unrecognized object) returns undefined so the action is dropped.
function pointOf(target: unknown): (Point & { selector?: string }) | undefined {
if (typeof target === "string") {
return target.length > 0 ? { x: 0, y: 0, selector: target } : undefined;
}
if (!target || typeof target !== "object") return undefined;
const obj = target as Record<string, unknown>;
if (typeof obj.x === "number" && typeof obj.y === "number") {
const point: Point & { selector?: string } = { x: obj.x, y: obj.y };
// The picker's builtin candidates carry `selector`; a goja ax element
// carries it under the runtime tag. Either lets the runner re-resolve.
const selector = obj.selector ?? obj.__sanderlingSelector;
if (typeof selector === "string" && selector.length > 0) point.selector = selector;
return point;
}
return undefined;
}
export function serializeAction(action: ActionDescriptor | null): SerializedAction | null {
if (!action) return null;
switch (action.kind) {
case "Tap":
case "DoubleTap":
case "LongPress": {
const point = pointOf(action.on);
if (!point) return null;
const out: SerializedAction = { kind: action.kind, x: point.x, y: point.y };
if (point.selector) out.selector = point.selector;
return out;
}
case "InputText": {
const point = pointOf(action.into);
if (!point) return null;
const out: SerializedAction = { kind: "InputText", x: point.x, y: point.y, text: action.text };
if (point.selector) out.selector = point.selector;
return out;
}
case "Swipe": {
const from = pointOf(action.from);
const to = pointOf(action.to);
if (!from || !to) return null;
return {
kind: "Swipe",
fromX: from.x,
fromY: from.y,
toX: to.x,
toY: to.y,
durationMillis: action.durationMillis ?? DEFAULT_SWIPE_DURATION,
};
}
case "Scroll": {
const from = pointOf(action.from) ?? pointOf(action.in);
if (!from) return null;
// The builtin generator pre-computes `to`; an author Scroll without it
// collapses to a zero-length gesture the runner re-derives from bounds.
const to = pointOf(action.to) ?? from;
return {
kind: "Scroll",
direction: action.direction,
fromX: from.x,
fromY: from.y,
toX: to.x,
toY: to.y,
durationMillis: DEFAULT_SWIPE_DURATION,
};
}
case "PressKey":
return { kind: "PressKey", key: action.key };
case "Wait":
return { kind: "Wait", durationMillis: action.durationMillis };
}
}
// installRuntime defines the next-action and extractor globals for one engine.
// root is the generator the spec assigned; pass a function when the spec runs
// AFTER this call (the web bundle imports the runtime before the spec, so the
// root only exists on globalThis.actions once the spec has evaluated).
// evaluateExtractors is the engine's snapshot of the spec's extract() handles.
//
// Setup precedence: when the spec assigned globalThis.setup, it is walked ONCE
// per tick first; if it yields an action that wins, otherwise the call falls
// through to the action root's 16-attempt retry. This matches the native
// verifier's prior NextAction precedence and applies on both engines.
export function installRuntime(
host: Host,
root: GeneratorNode | null | (() => GeneratorNode | null),
evaluateExtractors: () => Record<number, unknown>,
): void {
const rng = new Pcg(host.seedHi(), host.seedLo());
const resolveRoot = typeof root === "function" ? root : () => root;
const resolveSetup = () =>
(globalThis as { setup?: GeneratorNode }).setup ?? null;
defineLockedGlobal("__sanderlingExtractors__", () => evaluateExtractors());
defineLockedGlobal("__sanderlingNextAction__", () => {
// resolveRoot runs first: on web it also resets the per-tick candidate
// cache, which setup's walk below must see fresh.
const current = resolveRoot();
const setup = resolveSetup();
if (setup) {
const setupAction = walk(setup, rng, host);
if (setupAction) return serializeAction(setupAction);
}
if (!current) return null;
return serializeAction(nextAction(current, rng, host));
});
}
function defineLockedGlobal(name: string, value: unknown): void {
Object.defineProperty(globalThis, name, {
value,
writable: false,
configurable: false,
enumerable: false,
});
}
+17
View File
@@ -0,0 +1,17 @@
// samplerRng is the picker's Pcg while it evaluates an `actions` node's
// generator (set by pick.ts walkActions). Author sampling, from(...).generate()
// and the values.ts generators, draws from it so every sample shares the single
// deterministic stream. It is null outside a walk; eager spec-time generate()
// calls then fall back to a fixed deterministic default.
import type { Pcg } from "./pcg.ts";
let samplerRng: Pcg | null = null;
export function setSamplerRng(rng: Pcg | null): void {
samplerRng = rng;
}
export function getSamplerRng(): Pcg | null {
return samplerRng;
}
+10 -33
View File
@@ -125,6 +125,7 @@ export interface WebState extends State {
export interface Extracted<T> {
readonly current: T;
readonly previous: T | undefined;
named(name: string): Extracted<T>;
}
export interface Point {
@@ -175,10 +176,10 @@ export type Key =
| "left"
| "right";
export interface ActionGenerator {
readonly __sanderlingActionGenerator: true;
generate(): Action[];
}
// ActionGenerator is a node in the action-generator tree (see action-tree.ts).
// Author specs treat it as an opaque handle they compose with weighted(); the
// shared picker walks the underlying GeneratorNode.
export type ActionGenerator = GeneratorNode;
export interface Formula {
readonly __sanderlingFormula: true;
@@ -196,45 +197,21 @@ export interface Sampler<T> {
generate(): T;
}
// SanderlingRuntime is the host-bound surface that stays on
// globalThis.__sanderling__: extract plus the LTL formula constructors. Action
// factories no longer live here; they return plain data trees (see actions.ts).
export interface SanderlingRuntime {
extract: <T>(getter: (state: State) => T, name?: string) => Extracted<T>;
always: (predicateOrFormula: (() => boolean) | Formula) => Formula;
now: (predicate: () => boolean) => Formula;
next: (predicate: () => boolean) => Formula;
eventually: (predicate: () => boolean) => EventuallyFormula;
actions: (generator: () => Action[]) => ActionGenerator;
weighted: (...entries: WeightedEntry[]) => ActionGenerator;
from: <T>(items: readonly T[]) => Sampler<T>;
tap: (parameters: { on: string | AccessibilityElement }) => TapAction;
doubleTap: (parameters: { on: string | AccessibilityElement }) => DoubleTapAction;
longPress: (parameters: { on: string | AccessibilityElement }) => LongPressAction;
scroll: (parameters: {
direction: Direction;
in?: string | AccessibilityElement;
}) => ScrollAction;
inputText: (parameters: {
into: string | AccessibilityElement;
text: string;
}) => InputTextAction;
swipe: (parameters: {
from: Point | AccessibilityElement;
to: Point | AccessibilityElement;
durationMillis?: number;
}) => SwipeAction;
pressKey: (parameters: { key: Key }) => PressKeyAction;
wait: (parameters: { durationMillis: number }) => WaitAction;
taps: ActionGenerator;
doubleTaps: ActionGenerator;
longPresses: ActionGenerator;
scrolls: ActionGenerator;
typing: ActionGenerator;
swipes: ActionGenerator;
waitOnce: ActionGenerator;
pressKeys: ActionGenerator;
}
export type WeightedEntry = readonly [number, ActionGenerator];
import type { GeneratorNode } from "./action-tree.ts";
declare global {
// eslint-disable-next-line no-var
var __sanderling__: SanderlingRuntime;
+104
View File
@@ -0,0 +1,104 @@
// Author-facing, fluent, seeded value generators. Each builder is itself a
// Sampler<T> (it has .generate()), so `integers().between(0, 9)` is usable
// anywhere a Sampler is expected and chains read left to right.
//
// Every draw goes through the shared samplerRng (sampler-rng.ts) that from()
// uses, so generators are deterministic AND identical across the goja verifier
// and the V8 web runtime. Outside an actions() walk samplerRng is null and each
// generator returns a fixed deterministic default (mirroring from()'s index 0),
// so module-load-time calls never throw and never use an unseeded source.
import type { Pcg } from "./pcg.ts";
import type { Sampler } from "./types.ts";
import { getSamplerRng } from "./sampler-rng.ts";
import { INPUT_CORPUS } from "./corpus.ts";
const ALPHA = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const DIGITS = "0123456789";
const ALPHANUMERIC = ALPHA + DIGITS;
function draw(rng: Pcg | null, bound: number): number {
return rng && bound > 1 ? rng.intN(bound) : 0;
}
class StringBuilder implements Sampler<string> {
private minLength = 1;
private maxLength = 16;
private charset = ALPHANUMERIC;
length(min: number, max: number): this {
this.minLength = min;
this.maxLength = max;
return this;
}
alpha(): this {
this.charset = ALPHA;
return this;
}
generate(): string {
const rng = getSamplerRng();
const span = this.maxLength - this.minLength + 1;
const count = this.minLength + draw(rng, span);
let result = "";
for (let i = 0; i < count; i++) {
result += this.charset[draw(rng, this.charset.length)];
}
return result;
}
}
class IntegerBuilder implements Sampler<number> {
private minValue = 0;
private maxValue = 2 ** 31 - 1;
between(min: number, max: number): this {
this.minValue = min;
this.maxValue = max;
return this;
}
generate(): number {
const rng = getSamplerRng();
const span = this.maxValue - this.minValue + 1;
return this.minValue + draw(rng, span);
}
}
class EmailBuilder implements Sampler<string> {
private host = "example.com";
domain(domain: string): this {
this.host = domain;
return this;
}
generate(): string {
const local = new StringBuilder().length(3, 8).alpha().generate();
return `${local}@${this.host}`;
}
}
class EdgeCaseTextBuilder implements Sampler<string> {
generate(): string {
const rng = getSamplerRng();
return INPUT_CORPUS[draw(rng, INPUT_CORPUS.length)] ?? "";
}
}
export function strings(): StringBuilder {
return new StringBuilder();
}
export function integers(): IntegerBuilder {
return new IntegerBuilder();
}
export function emails(): EmailBuilder {
return new EmailBuilder();
}
export function edgeCaseText(): EdgeCaseTextBuilder {
return new EdgeCaseTextBuilder();
}
+58
View File
@@ -0,0 +1,58 @@
// Verb support matrix and a warn-once helper, shared by both engines.
//
// The matrix is the single declared decision about which builtin verbs each
// platform supports. The picker (pick.ts) consults it before drawing so an
// unsupported verb is surfaced once (via host.reportUnsupported) rather than
// silently producing a null action every tick.
import type { BuiltinVerb, Host } from "./action-tree.ts";
export type Platform = "android" | "ios" | "web";
// VERB_SUPPORT declares, per verb, the platforms that can execute it.
// Tap/DoubleTap/InputText/Wait: native + web.
// PressKey: native (full key set incl. back/home) + web (enter/tab/escape/arrows).
// Swipe/LongPress/Scroll: native yes; web yes (the chrome driver supports
// pointer drags / long-press / wheel), declared here rather than silently
// no-op as the older web-runtime did.
const VERB_SUPPORT: Record<BuiltinVerb, ReadonlyArray<Platform>> = {
taps: ["android", "ios", "web"],
doubleTaps: ["android", "ios", "web"],
typing: ["android", "ios", "web"],
waitOnce: ["android", "ios", "web"],
pressKeys: ["android", "ios", "web"],
swipes: ["android", "ios", "web"],
longPresses: ["android", "ios", "web"],
scrolls: ["android", "ios", "web"],
};
// supports reports whether a verb is executable on a platform.
export function supports(verb: BuiltinVerb, platform: Platform): boolean {
return VERB_SUPPORT[verb].includes(platform);
}
// A warn-once registry keyed by `${verb}@${platform}`. Module-level so the
// "warn at most once" guarantee holds across every picker walk in a run.
const warnedKeys = new Set<string>();
function warnKey(verb: BuiltinVerb, platform: Platform): string {
return `${verb}@${platform}`;
}
// warnUnsupportedOnce calls host.reportUnsupported(verb) at most once per
// verb@platform for the lifetime of the process. Returns true on the first
// (reporting) call for a key, false on subsequent suppressed calls. The picker
// uses the boolean only for testability; behavior is the single report.
export function warnUnsupportedOnce(host: Host, verb: BuiltinVerb): boolean {
const key = warnKey(verb, host.platform());
if (warnedKeys.has(key)) return false;
warnedKeys.add(key);
host.reportUnsupported(verb);
return true;
}
// resetWarnings clears the warn-once registry. Test-only: production never
// resets, so a verb is reported once per process.
export function resetWarnings(): void {
warnedKeys.clear();
}
+228 -300
View File
@@ -2,36 +2,75 @@
// V8-side runtime for `sanderling test --platform web`.
//
// The user spec is bundled with this file as the first import so that
// globalThis.__sanderling__ is installed before the spec evaluates. The host
// invokes window.__sanderlingExtractors__() and window.__sanderlingNextAction__()
// over CDP each tick. LTL predicates are intentionally stubbed: properties
// run host-side in goja, which loads its own bundle of the same spec.
// This file is the WEB Host. It installs globalThis.__sanderling__ (extract +
// LTL formula binds) before the spec evaluates, implements the Host interface
// (platform/seed/queryCandidates/reportUnsupported) over the live DOM, and then
// delegates ALL action generation to the shared picker via installRuntime
// (runtime-entry.ts -> pick.ts). The goja verifier runs the SAME picker over the
// SAME Pcg, so a given seed yields an identical action stream by construction.
//
// Element references never cross V8/host. Action targets that reference an
// AccessibilityElement collapse to `{x, y}` from getBoundingClientRect()
// before serialization.
// The host invokes window.__sanderlingExtractors__() and
// window.__sanderlingNextAction__() over CDP each tick. LTL predicates are
// stubbed: properties run host-side in goja, which loads its own bundle.
//
// Element references never cross V8/host. queryCandidates resolves each element
// to a {x, y} Point via getBoundingClientRect before the picker sees it.
import { installRuntime } from "./runtime-entry.ts";
import type { BuiltinVerb, Candidate, Host } from "./action-tree.ts";
interface Handle {
current: unknown;
previous: unknown;
readonly current: unknown;
readonly previous: unknown;
named(name: string): Handle;
}
interface ExtractorEntry {
getter: (state: unknown) => unknown;
handle: Handle;
name: string;
}
interface ActionGeneratorHandle {
__sanderlingActionGenerator: true;
__sanderlingKind: string;
generate?: () => unknown;
entries?: readonly [number, ActionGeneratorHandle][];
currentValue: unknown;
previousValue: unknown;
}
const extractors: ExtractorEntry[] = [];
let actionsRoot: ActionGeneratorHandle | null = null;
// extracting is true only while an extractor getter is running. The current/
// previous accessors consult it so a getter that reaches into another
// extractor's handle throws instead of reading a stale cross-extractor value.
let extracting = false;
function checkNotExtracting(slot: "current" | "previous"): void {
if (extracting) {
throw new Error(
`reading .${slot} of an extractor inside another extractor is not allowed; extractor getters may read only from the state argument`,
);
}
}
// SANDERLING_SEED is the host-computed 64-bit seed, injected as a decimal
// string via the bundle define. We parse it into a BigInt without ever going
// through a JS Number (which loses precision above 2^53), matching the goja
// side's rand.NewPCG(seed, 0): hi = seed, lo = 0.
function injectedSeed(): string | undefined {
try {
return process.env.SANDERLING_SEED;
} catch {
return undefined;
}
}
function seedBigInt(): bigint {
const raw = injectedSeed();
if (!raw) return 0n;
try {
return BigInt(raw);
} catch {
return 0n;
}
}
const SEED_HI = seedBigInt();
function noopFormula(): unknown {
const formula: Record<string, unknown> = { __sanderlingFormula: true };
@@ -277,6 +316,38 @@ function buildAx(): unknown {
};
}
// Uncaught errors and unhandled rejections are buffered here as they fire, so
// the default noUncaughtExceptions property can observe them. Without this the
// web state.exceptions would always be empty and a page that throws would
// silently pass.
interface CapturedException {
class: string;
message: string;
stackTrace: string;
unixMillis: number;
}
const capturedExceptions: CapturedException[] = [];
function recordException(error: unknown): void {
const asError = error instanceof Error ? error : undefined;
capturedExceptions.push({
class: asError?.name ?? "Error",
message: asError?.message ?? String(error),
stackTrace: asError?.stack ?? "",
unixMillis: Date.now(),
});
}
if (typeof globalThis.addEventListener === "function") {
globalThis.addEventListener("error", (event: ErrorEvent) => {
recordException(event.error ?? event.message);
});
globalThis.addEventListener("unhandledrejection", (event: PromiseRejectionEvent) => {
recordException(event.reason);
});
}
function buildState(): unknown {
return {
snapshots: {},
@@ -286,91 +357,42 @@ function buildState(): unknown {
lastAction: null,
time: 0,
logs: [],
exceptions: [],
exceptions: capturedExceptions.slice(),
};
}
const runtime = {
extract<T>(getter: (state: unknown) => T, name?: string): Handle {
const handle: Handle = { current: undefined, previous: undefined };
const resolvedName = name && name.length > 0 ? name : `extractor_${extractors.length}`;
extractors.push({
const entry: ExtractorEntry = {
getter: getter as (s: unknown) => unknown,
handle,
handle: undefined as unknown as Handle,
name: resolvedName,
});
currentValue: undefined,
previousValue: undefined,
};
const handle: Handle = {
get current() {
checkNotExtracting("current");
return entry.currentValue;
},
get previous() {
checkNotExtracting("previous");
return entry.previousValue;
},
named(name: string): Handle {
entry.name = name;
return handle;
},
};
entry.handle = handle;
extractors.push(entry);
return handle;
},
always: noopFormula,
now: noopFormula,
next: noopFormula,
eventually: noopFormula,
actions(generator: () => unknown): ActionGeneratorHandle {
const handle: ActionGeneratorHandle = {
__sanderlingActionGenerator: true,
__sanderlingKind: "actions",
generate: generator,
};
if (!actionsRoot) actionsRoot = handle;
return handle;
},
weighted(...entries: [number, ActionGeneratorHandle][]): ActionGeneratorHandle {
const handle: ActionGeneratorHandle = {
__sanderlingActionGenerator: true,
__sanderlingKind: "weighted",
entries,
};
actionsRoot = handle;
return handle;
},
from<T>(items: readonly T[]): { generate: () => T | undefined } {
return {
generate(): T | undefined {
if (items.length === 0) return undefined;
return items[Math.floor(Math.random() * items.length)];
},
};
},
tap(p: { on: unknown }): unknown {
return { kind: "Tap", on: p.on };
},
doubleTap(p: { on: unknown }): unknown {
return { kind: "DoubleTap", on: p.on };
},
longPress(): unknown {
// Why: web has no long-press gesture for v1; the factory returns null so LongPress() calls no-op.
return null;
},
scroll(): unknown {
// Why: web has no native scroll gesture for v1; the factory returns null so Scroll() calls no-op.
return null;
},
inputText(p: { into: unknown; text: string }): unknown {
return { kind: "InputText", into: p.into, text: p.text };
},
swipe(_p: { from: unknown; to: unknown; durationMillis?: number }): unknown {
// Why: web has no swipe gesture; the factory returns null so Swipe() calls in specs no-op.
return null;
},
pressKey(p: { key: string }): unknown {
if (!WEB_PRESS_KEYS.includes(p.key)) {
throw new Error(
`pressKey: unsupported key ${JSON.stringify(p.key)} (allowed: ${WEB_PRESS_KEYS.join(", ")})`,
);
}
return { kind: "PressKey", key: p.key };
},
wait(p: { durationMillis: number }): unknown {
return { kind: "Wait", durationMillis: p.durationMillis };
},
taps: { __sanderlingActionGenerator: true, __sanderlingKind: "taps" } as ActionGeneratorHandle,
doubleTaps: { __sanderlingActionGenerator: true, __sanderlingKind: "doubleTaps" } as ActionGeneratorHandle,
longPresses: { __sanderlingActionGenerator: true, __sanderlingKind: "longPresses" } as ActionGeneratorHandle,
scrolls: { __sanderlingActionGenerator: true, __sanderlingKind: "scrolls" } as ActionGeneratorHandle,
typing: { __sanderlingActionGenerator: true, __sanderlingKind: "typing" } as ActionGeneratorHandle,
swipes: { __sanderlingActionGenerator: true, __sanderlingKind: "swipes" } as ActionGeneratorHandle,
waitOnce: { __sanderlingActionGenerator: true, __sanderlingKind: "waitOnce" } as ActionGeneratorHandle,
pressKeys: { __sanderlingActionGenerator: true, __sanderlingKind: "pressKey" } as ActionGeneratorHandle,
};
// Lock the runtime globals so a misbehaving (or malicious) page script can't
@@ -378,11 +400,16 @@ const runtime = {
// the host invoking the extractor/next-action callbacks.
defineLockedGlobal("__sanderling__", runtime);
// writable:false stops a page script from shadowing the runtime via plain
// assignment (the realistic in-page threat). configurable:true is required so
// unit tests sharing one process can reinstall a fake via defineProperty; a
// non-configurable lock would poison globalThis.__sanderling__ for every later
// test in the run.
function defineLockedGlobal(name: string, value: unknown): void {
Object.defineProperty(globalThis, name, {
value,
writable: false,
configurable: false,
configurable: true,
enumerable: false,
});
}
@@ -393,14 +420,19 @@ function evaluateExtractors(): Record<number, unknown> {
for (let i = 0; i < extractors.length; i++) {
const entry = extractors[i];
if (!entry) continue;
entry.handle.previous = entry.handle.current;
entry.previousValue = entry.currentValue;
// Let getter throws propagate, matching the goja side, where a getter
// error aborts PushSnapshot rather than yielding undefined. Swallowing
// here would silence the cross-extractor read guard (and every other
// author error) on web only, breaking cross-engine parity.
let value: unknown;
extracting = true;
try {
value = entry.getter(state);
} catch {
value = undefined;
} finally {
extracting = false;
}
entry.handle.current = value;
entry.currentValue = value;
result[i] = sanitize(value);
}
return result;
@@ -435,96 +467,21 @@ function sanitizeAt(value: unknown, depth: number, seen: WeakSet<object>): unkno
return out;
}
function pickWeighted(handle: ActionGeneratorHandle): ActionGeneratorHandle | null {
const entries = handle.entries ?? [];
if (entries.length === 0) return null;
let total = 0;
for (const [weight] of entries) total += Math.max(0, weight);
if (total <= 0) return null;
let pick = Math.random() * total;
for (const [weight, generator] of entries) {
pick -= Math.max(0, weight);
if (pick <= 0) return generator;
}
return entries[entries.length - 1]?.[1] ?? null;
}
function resolveGenerator(handle: ActionGeneratorHandle): unknown {
switch (handle.__sanderlingKind) {
case "actions": {
const generated = handle.generate?.();
return pickFromArray(generated);
}
case "weighted": {
const inner = pickWeighted(handle);
if (!inner) return null;
return resolveGenerator(inner);
}
case "taps":
return randomTap("Tap");
case "doubleTaps":
return randomTap("DoubleTap");
case "typing":
return randomInput();
case "swipes":
return randomSwipe();
case "longPresses":
case "scrolls":
// Why: web has no long-press or native scroll gesture for v1; both no-op.
return null;
case "waitOnce":
return { kind: "Wait", durationMillis: 500 };
case "pressKey":
return randomPressKey();
default:
return null;
}
}
// Per-tick cache so the 16-attempt retry loop in __sanderlingNextAction__
// doesn't re-walk the DOM and re-flush layout on every iteration.
let randomTapCandidates: HTMLElement[] | null = null;
function randomTap(kind: "Tap" | "DoubleTap"): unknown {
if (!randomTapCandidates) {
randomTapCandidates = Array.from(
document.querySelectorAll<HTMLElement>(
'a, button, input, select, textarea, [role="button"], [onclick]',
),
).filter((element) => {
if ((element as HTMLButtonElement).disabled) return false;
const rect = element.getBoundingClientRect();
return rect.width > 0 && rect.height > 0;
});
}
const candidates = randomTapCandidates;
if (candidates.length === 0) return null;
const picked = candidates[Math.floor(Math.random() * candidates.length)];
if (!picked) return null;
const rect = picked.getBoundingClientRect();
return {
kind,
on: {
x: Math.round(rect.left + rect.width / 2),
y: Math.round(rect.top + rect.height / 2),
},
};
}
// Per-tick cache mirroring randomTapCandidates, reset each NextAction tick.
let randomInputCandidates: HTMLElement[] | null = null;
// Per-verb DOM selector sets. The tappable set backs taps/doubleTaps/longPresses
// (every tappable element is also a valid long-press target); the editable set
// backs typing; swipes/scrolls target the scrollable element.
const TAPPABLE_SELECTOR = 'a, button, input, select, textarea, [role="button"], [onclick]';
const EDITABLE_SELECTOR = "input, textarea, [contenteditable]";
const NON_TEXT_INPUT_TYPES = [
"button", "submit", "checkbox", "radio", "range", "color", "file", "image", "reset",
];
// Edge-case input pool mirroring the goja-side inputCorpus: empty, whitespace,
// overflow length, unicode, numeric boundaries, and common injection payloads.
const WEB_INPUT_CORPUS = [
"", "a", "a".repeat(4096), "🙂🔥💸", " ", "\t\n", "-1",
"999999999999999999999", "0.0000001", "1e10", "'; DROP TABLE--",
"<script>alert(1)</script>", "../../etc/passwd", "%s%n", "NaN",
];
function isVisible(element: HTMLElement): boolean {
if ((element as HTMLButtonElement).disabled) return false;
const rect = element.getBoundingClientRect();
return rect.width > 0 && rect.height > 0;
}
function isEditableElement(element: HTMLElement): boolean {
if (element.isContentEditable) return true;
@@ -537,141 +494,112 @@ function isEditableElement(element: HTMLElement): boolean {
return false;
}
function randomInput(): unknown {
if (!randomInputCandidates) {
randomInputCandidates = Array.from(
document.querySelectorAll<HTMLElement>("input, textarea, [contenteditable]"),
).filter((element) => {
if (!isEditableElement(element)) return false;
if ((element as HTMLInputElement).disabled) return false;
const rect = element.getBoundingClientRect();
return rect.width > 0 && rect.height > 0;
});
}
const candidates = randomInputCandidates;
if (candidates.length === 0) return null;
const picked = candidates[Math.floor(Math.random() * candidates.length)];
if (!picked) return null;
const rect = picked.getBoundingClientRect();
const text = WEB_INPUT_CORPUS[Math.floor(Math.random() * WEB_INPUT_CORPUS.length)];
function pointOf(element: Element): Candidate {
const rect = element.getBoundingClientRect();
return {
kind: "InputText",
into: {
x: Math.round(rect.left + rect.width / 2),
y: Math.round(rect.top + rect.height / 2),
},
text,
x: Math.round(rect.left + rect.width / 2),
y: Math.round(rect.top + rect.height / 2),
width: Math.round(rect.width),
height: Math.round(rect.height),
};
}
function randomSwipe(): unknown {
// Why: web has no swipe gesture; pointer drags into empty divs are noise.
return null;
function tappableCandidates(): Candidate[] {
return Array.from(document.querySelectorAll<HTMLElement>(TAPPABLE_SELECTOR))
.filter(isVisible)
.map(pointOf);
}
const WEB_PRESS_KEYS = ["enter", "tab", "escape", "up", "down", "left", "right"];
function randomPressKey(): unknown {
// Why: only emit keys with meaningful browser semantics; "back"/"home" don't navigate.
const key = WEB_PRESS_KEYS[Math.floor(Math.random() * WEB_PRESS_KEYS.length)];
return { kind: "PressKey", key };
function editableCandidates(): Candidate[] {
return Array.from(document.querySelectorAll<HTMLElement>(EDITABLE_SELECTOR))
.filter((element) => isEditableElement(element) && isVisible(element))
.map(pointOf);
}
function pickFromArray(value: unknown): unknown {
if (!value) return null;
if (!Array.isArray(value)) return value;
if (value.length === 0) return null;
return value[Math.floor(Math.random() * value.length)];
}
function serializeAction(action: unknown): unknown {
if (!action || typeof action !== "object") return null;
const obj = action as Record<string, unknown>;
switch (obj.kind) {
case "Tap": {
const point = pointOf(obj.on);
if (!point) {
console.warn("[sanderling] Tap target did not resolve to coordinates");
return null;
}
return { kind: "Tap", x: point.x, y: point.y };
// Scrollable candidates back swipe/scroll: elements that overflow their box,
// plus the scrolling root so a page-level scroll always has a target.
function scrollableCandidates(): Candidate[] {
const root = document.scrollingElement ?? document.documentElement;
const candidates: Candidate[] = root ? [pointOf(root)] : [];
for (const element of Array.from(document.querySelectorAll<HTMLElement>("*"))) {
if (element === root) continue;
if (element.scrollHeight <= element.clientHeight && element.scrollWidth <= element.clientWidth) {
continue;
}
case "DoubleTap": {
const point = pointOf(obj.on);
if (!point) {
console.warn("[sanderling] DoubleTap target did not resolve to coordinates");
return null;
}
return { kind: "DoubleTap", x: point.x, y: point.y };
}
case "InputText": {
const point = pointOf(obj.into);
if (!point) {
console.warn("[sanderling] InputText target did not resolve to coordinates");
return null;
}
return {
kind: "InputText",
x: point.x,
y: point.y,
text: obj.text ?? "",
};
}
case "Swipe": {
const from = pointOf(obj.from);
const to = pointOf(obj.to);
if (!from || !to) {
console.warn("[sanderling] Swipe endpoints did not resolve to coordinates");
return null;
}
return {
kind: "Swipe",
from_x: from.x,
from_y: from.y,
to_x: to.x,
to_y: to.y,
duration_millis: obj.durationMillis ?? 250,
};
}
case "LongPress":
case "Scroll":
// Why: web has no long-press or native scroll gesture for v1; drop the action.
return null;
case "PressKey":
return { kind: "PressKey", key: obj.key };
case "Wait":
return { kind: "Wait", duration_millis: obj.durationMillis ?? 0 };
default:
return null;
if (!isVisible(element)) continue;
candidates.push(pointOf(element));
}
return candidates;
}
function pointOf(value: unknown): { x: number; y: number } | undefined {
if (!value || typeof value !== "object") return undefined;
const obj = value as Record<string, unknown>;
if (typeof obj.x === "number" && typeof obj.y === "number") {
return { x: obj.x, y: obj.y };
}
return undefined;
// Per-tick candidate cache: the picker's 16-attempt retry re-queries the same
// verb, so we avoid re-walking the DOM and re-flushing layout within one tick.
// installRuntime resets it before each __sanderlingNextAction__ invocation.
const candidateCache = new Map<BuiltinVerb, Candidate[]>();
function resetCandidateCache(): void {
candidateCache.clear();
}
defineLockedGlobal("__sanderlingExtractors__", function (): Record<number, unknown> {
return evaluateExtractors();
});
const host: Host = {
platform: () => "web",
seedHi: () => SEED_HI,
// lo = 0 matches the goja side's rand.NewPCG(seed, 0).
seedLo: () => 0n,
queryCandidates(verb: BuiltinVerb): Candidate[] {
const cached = candidateCache.get(verb);
if (cached) return cached;
let candidates: Candidate[];
switch (verb) {
case "taps":
case "doubleTaps":
case "longPresses":
candidates = tappableCandidates();
break;
case "typing":
candidates = editableCandidates();
break;
case "swipes":
case "scrolls":
candidates = scrollableCandidates();
break;
default:
candidates = [];
}
candidateCache.set(verb, candidates);
return candidates;
},
reportUnsupported(verb: BuiltinVerb): void {
console.warn(`[sanderling] verb ${verb} is unsupported on web`);
},
};
defineLockedGlobal("__sanderlingNextAction__", function (): unknown {
if (!actionsRoot) return null;
// Reset per-tick caches so each invocation gets a fresh DOM scan.
randomTapCandidates = null;
randomInputCandidates = null;
// Match the goja runtime: retry up to 16 times when a weighted entry's
// generator returns []. Otherwise on routes where most generators are
// gated to other pages, ~80% of ticks would emit no action.
for (let attempt = 0; attempt < 16; attempt++) {
const action = serializeAction(resolveGenerator(actionsRoot));
if (action !== null) return action;
}
return null;
});
// The spec assigns its action root to globalThis.actions, and it runs AFTER
// this module (the web bundle imports the runtime first). Resolve the root
// lazily so installRuntime captures it once the spec has evaluated. The root
// resolver runs once per __sanderlingNextAction__ tick (before the retry loop),
// so it is also where we reset the per-tick candidate cache.
installRuntime(
host,
() => {
resetCandidateCache();
return (globalThis as { actions?: import("./action-tree.ts").GeneratorNode }).actions ?? null;
},
evaluateExtractors,
);
// Test-only exports. The IIFE bundle the host installs has no export surface,
// so these are stripped from production output; they only exist for unit tests.
export const __testing__ = {
host,
seedBigInt,
tappableCandidates,
editableCandidates,
scrollableCandidates,
resetCandidateCache,
runtime,
extractors,
evaluateExtractors,
};
export {};