logicsrc/packages/openerrand/src/browser.ts
Anthony Ettinger dde596b276
OpenErrand reference runner: @logicsrc/openerrand and logicsrc errand (#228)
* OpenErrand 0.1: an errand on a website with no API, with the human steps kept human

docs/openerrand.md mints OpenErrand: one JSON file per errand (register an
account, download a transcript) naming the site, the inputs with a
sensitivity class and ordered sources (document, vault, prompt, generate,
derive, candidate, literal), field rules matched by id then label, page and
wait steps, five human gates a runner never performs (declare,
identity-proofing, code, mail, captcha), outcomes, the never-retried shared
secret, vault and download outputs, hand-off cards that may name only public
inputs, the publisher index at /.well-known/openerrand.json, and thirteen
runner rules. The worked example is the MyFTB business registration that
cli-tools `ftb` performs (profullstack/cli-tools#125), with no personal data.

- @logicsrc/schemas: openerrand + openerrand-index schemas and fixtures
- @logicsrc/validators: semantic checks (references, templates, no personal
  or secret input on a card) and tests that validate the spec's own examples
- logicsrc-web: registry entry (process family), /openerrand landing page,
  the example and the index served as static files, contract tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* OpenErrand: hand-off cards stay on the surface that owns the data

Anthony's ruling: tax and finance data never touches a social or promotion
tool, and nothing is sent to a CPA or preparer.

- Hand-off cards are delivered only on the surface that owns the errand's
  data (for a tax or finance errand, the principal's finance app through its
  CLI, PWA, MCP server or API, such as CoinPay, or the runner's terminal),
  never a social, promotion or third-party posting service, and never to
  anyone but the principal. A card for an errand with personal or secret
  inputs does not leave that surface. Runner rule 9 says the same.
- The run record and the sample run name the card by an opaque id
  (pin-letter/7f3k2q) instead of a mynaposter.com URL; the myna mention is gone.
- `principal: represented` no longer cites a preparer with a power of attorney.
- The FTB card's last step no longer suggests sending the PIN to someone else.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* OpenErrand: user-agent rule, captcha solver policy, reference runner note

Anthony's answers on #227 ("go with your recommendations"):

- Rule 11: a runner may run headless with a normal desktop browser user agent
  (dropping HeadlessChrome) and nothing more: no fingerprint spoofing beyond
  the UA string, no stealth plugins, no solving or evading a bot challenge.
  A challenge the browser completes itself is a wait step; any other is a
  captcha gate.
- Captcha solvers: new site.sector and captcha step `solver`
  (forbidden by default | allowed). Never allowed on government, tax,
  financial, healthcare or identity-provider sites, nor on any errand with a
  declare or identity-proofing step or a secret input; elsewhere only when the
  file says so, with every use logged. The validator rejects `allowed` in the
  forbidden set or without a stated sector; six new tests. The FTB example
  states sector "tax".
- Reference runner: @logicsrc/openerrand / `logicsrc errand run`, marked in
  progress; ftb stays the runner the example was taken from.
- Name stays OpenErrand; family stays Agents and process.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* OpenErrand reference runner: @logicsrc/openerrand and logicsrc errand

Ship the runner docs/openerrand.md promised. `logicsrc errand run <file>`
reads an OpenErrand 0.1 file, validates it with @logicsrc/validators, and
drives headless Chrome through it under the spec's thirteen rules;
`errand validate` shows what a file will ask of you and `errand status`
shows the last run of each errand, its card and any lockout.

The engine is generalised from cli-tools `ftb` (PR #125) with no dependency
on cli-tools: the CDP client and Chrome finder from wcag.ts, the page reader,
native-setter fill and forward-button picker from ftb-run.ts, and the rule
matcher, throttle and outcome logic from ftb.ts, all now driven by the file.

- Inputs: document (an extractor hook; the one shipped runs a local command
  that reads JSON requests and prints records), vault (teams or OpenCreds),
  prompt (no echo for secrets), generate, derive, candidate, literal.
  `--input name=value` wins. Shared-secret candidates are ranked as the spec
  says, one is submitted, and a rejection lists the others for --candidate.
- Rules: id before label, step rules first, choices before text, an id match
  final, an unmatched required field stops the run naming it.
- Gates: declare only with --declare after the values are shown; identity
  proofing never touched (URL only) and handed over or stopped on; code from
  the terminal or a code file, used once, a wrong code waits for the next;
  mail ends the run waiting with the card; captcha is the person's, and a
  CaptchaSolver interface is called only where the spec permits (no solver is
  bundled); wait steps are polled, never solved.
- Throttle: 2 runs per errand and account in 30 minutes, 4 a day, 2 minutes
  between runs on a site, lockouts from metadata.lockout or a default, held
  per site and account, never lifted by --force.
- Outputs: credentials written before success to a teams vault by pull,
  merge, push (metadata.vault or --vault), else a 0600 file said aloud;
  downloads type-checked and never overwritten with different bytes; cards
  only in the local run record.
- The user agent is Chrome's own with HeadlessChrome replaced, given at
  launch: a CDP override did not reach a navigation the page's own script
  started, which is exactly the proof-of-work interstitial case.

Tests: 85 in the package (rule engine, inputs, gates, throttle, outcomes,
captcha gating, vaults, outputs, commands) including an integration test
that runs the published FTB example unchanged in real headless Chrome
against a local HTTPS fake site (Chrome maps webapp.ftb.ca.gov to it and
every other host to NOTFOUND; all data fictional), and 2 in the CLI.

CLI 0.6.0 -> 0.7.0; @logicsrc/schemas and @logicsrc/validators 0.3.0 ->
0.4.0 (the OpenErrand schemas, and the vocabularies now exported for
runners); PRD 0009; the spec's Reference runner section and the landing
page say it ships.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 09:17:06 -07:00

277 lines
10 KiB
TypeScript

/**
* Chrome over the DevTools protocol, with no dependency: find a binary, start
* it, and talk to it over one WebSocket in flat session mode.
*
* Ported from cli-tools' wcag.ts (findChrome, Cdp, launchBrowser), which `ftb`
* drives MyFTB with.
*/
import { type ChildProcess, spawn } from "node:child_process";
import { accessSync, constants, mkdirSync, mkdtempSync, readdirSync, rmSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
import { join } from "node:path";
import { ErrandError } from "./util.js";
const isExecutable = (path: string): boolean => {
try {
accessSync(path, constants.X_OK);
return true;
} catch {
return false;
}
};
const listDir = (path: string): string[] => {
try {
return readdirSync(path);
} catch {
return [];
}
};
/** `linux-152.0.7977.42` before `linux-131.0.6778.204`: newest first by version. */
const byVersionDesc = (a: string, b: string): number => {
const parse = (name: string): number[] => (/(\d+(?:\.\d+)*)/.exec(name)?.[1] ?? "0").split(".").map(Number);
const left = parse(a);
const right = parse(b);
for (let i = 0; i < Math.max(left.length, right.length); i += 1) {
const d = (right[i] ?? 0) - (left[i] ?? 0);
if (d !== 0) return d;
}
return 0;
};
/** Every place a Chrome might be: CHROME_PATH, PATH names, /opt, Puppeteer and Playwright caches, macOS bundles. */
export function chromeCandidates(env: NodeJS.ProcessEnv = process.env, home: string = homedir()): string[] {
const out: string[] = [];
if (env.CHROME_PATH) out.push(env.CHROME_PATH);
for (const name of ["google-chrome", "google-chrome-stable", "chromium", "chromium-browser", "chrome"]) {
for (const dir of (env.PATH ?? "").split(":").filter(Boolean)) out.push(join(dir, name));
}
out.push("/opt/google/chrome/chrome");
// Full Chrome before the headless shell: a person may need the window (--headful).
const puppeteer = join(home, ".cache", "puppeteer");
for (const flavour of ["chrome", "chrome-headless-shell"]) {
for (const build of listDir(join(puppeteer, flavour)).sort(byVersionDesc)) {
const dir = join(puppeteer, flavour, build);
const inner = listDir(dir).find((entry) => entry.startsWith(flavour));
if (inner) out.push(join(dir, inner, flavour));
}
}
const playwright = join(home, ".cache", "ms-playwright");
for (const build of listDir(playwright).sort(byVersionDesc)) {
if (build.startsWith("chromium-")) out.push(join(playwright, build, "chrome-linux", "chrome"));
}
out.push("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
out.push("/Applications/Chromium.app/Contents/MacOS/Chromium");
return out;
}
export function findChrome(env: NodeJS.ProcessEnv = process.env, home: string = homedir(), executable: (path: string) => boolean = isExecutable): string | null {
return chromeCandidates(env, home).find(executable) ?? null;
}
export const NO_CHROME = `no Chrome found. Pass --chrome PATH or set CHROME_PATH, install one
(apt install chromium, brew install --cask google-chrome), or let Puppeteer fetch one
(npx puppeteer browsers install chrome).`;
interface CdpMessage {
id?: number;
method?: string;
params?: Record<string, unknown>;
sessionId?: string;
result?: Record<string, unknown>;
error?: { message: string };
}
export interface CdpEvent {
method: string;
params: Record<string, unknown>;
sessionId?: string;
}
/** Requests with ids and events by name, over one socket for the browser and every page. */
export class Cdp {
private nextId = 1;
private readonly pending = new Map<number, { resolve: (value: Record<string, unknown>) => void; reject: (error: Error) => void }>();
private readonly listeners = new Set<(message: CdpMessage) => void>();
private readonly socket: WebSocket;
constructor(socket: WebSocket) {
this.socket = socket;
socket.addEventListener("message", (event) => {
const message = JSON.parse(String(event.data)) as CdpMessage;
if (message.id !== undefined && this.pending.has(message.id)) {
const { resolve, reject } = this.pending.get(message.id)!;
this.pending.delete(message.id);
if (message.error) reject(new ErrandError(message.error.message));
else resolve(message.result ?? {});
return;
}
for (const listener of this.listeners) listener(message);
});
socket.addEventListener("close", () => {
for (const { reject } of this.pending.values()) reject(new ErrandError("browser closed"));
this.pending.clear();
});
}
static async connect(url: string): Promise<Cdp> {
const socket = new WebSocket(url);
await new Promise<void>((resolve, reject) => {
socket.addEventListener("open", () => resolve(), { once: true });
socket.addEventListener("error", () => reject(new ErrandError(`could not connect to ${url}`)), { once: true });
});
return new Cdp(socket);
}
send(method: string, params: Record<string, unknown> = {}, sessionId?: string): Promise<Record<string, unknown>> {
const id = this.nextId;
this.nextId += 1;
return new Promise((resolve, reject) => {
this.pending.set(id, { resolve, reject });
this.socket.send(JSON.stringify(sessionId ? { id, method, params, sessionId } : { id, method, params }));
});
}
on(listener: (event: CdpEvent) => void): () => void {
const wrapped = (message: CdpMessage): void => {
if (message.method) listener({ method: message.method, params: message.params ?? {}, ...(message.sessionId ? { sessionId: message.sessionId } : {}) });
};
this.listeners.add(wrapped);
return () => this.listeners.delete(wrapped);
}
waitFor(method: string, sessionId: string, timeoutMs: number): Promise<void> {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
this.listeners.delete(listener);
reject(new ErrandError(`timed out after ${Math.round(timeoutMs / 1000)}s waiting for ${method}`));
}, timeoutMs);
const listener = (message: CdpMessage): void => {
if (message.method === method && message.sessionId === sessionId) {
clearTimeout(timer);
this.listeners.delete(listener);
resolve();
}
};
this.listeners.add(listener);
});
}
close(): void {
this.socket.close();
}
}
export interface Browser {
cdp: Cdp;
path: string;
/** Settles when Chrome ends, including a person closing its window. */
exited: Promise<void>;
close(): Promise<void>;
}
export interface LaunchOptions {
chrome?: string;
env?: NodeJS.ProcessEnv;
sandbox?: boolean;
timeoutMs?: number;
/** A profile directory to keep between runs; without one a throwaway profile is removed on close. */
profile?: string;
/** false opens a window a person can use; the default is headless. */
headless?: boolean;
/** Extra Chrome switches. The tests use this to point a hostname at a local fake site. */
args?: string[];
}
export async function launchBrowser(options: LaunchOptions = {}): Promise<Browser> {
const env = options.env ?? process.env;
const path = options.chrome ?? findChrome(env);
if (!path) throw new ErrandError(NO_CHROME);
if (!isExecutable(path)) throw new ErrandError(`${path} is not an executable`);
const keep = options.profile !== undefined;
if (keep) mkdirSync(options.profile!, { recursive: true, mode: 0o700 });
const profile = options.profile ?? mkdtempSync(join(tmpdir(), "logicsrc-errand-chrome-"));
const sandbox = options.sandbox ?? !(env.CHROME_NO_SANDBOX || process.getuid?.() === 0);
const args = [
...(options.headless === false ? [] : ["--headless=new"]),
"--remote-debugging-port=0",
`--user-data-dir=${profile}`,
"--no-first-run",
"--no-default-browser-check",
"--disable-gpu",
"--disable-extensions",
"--disable-background-networking",
"--window-size=1280,900",
...(sandbox ? [] : ["--no-sandbox"]),
...(options.args ?? []),
"about:blank",
];
const child: ChildProcess = spawn(path, args, { env, stdio: ["ignore", "ignore", "pipe"] });
const exited = new Promise<void>((resolve) => child.once("exit", () => resolve()));
const cleanup = (): void => {
if (keep) return;
try {
rmSync(profile, { recursive: true, force: true });
} catch {
// Still held open; the next run's tmpdir sweep takes it.
}
};
const url = await new Promise<string>((resolve, reject) => {
let stderr = "";
const timeoutMs = options.timeoutMs ?? 20_000;
const timer = setTimeout(() => reject(new ErrandError(`${path} did not start within ${timeoutMs / 1000}s\n${stderr}`)), timeoutMs);
child.stderr?.on("data", (chunk: Buffer) => {
stderr += chunk.toString();
const match = /DevTools listening on (ws:\/\/\S+)/.exec(stderr);
if (match?.[1]) {
clearTimeout(timer);
resolve(match[1]);
}
});
child.on("exit", (code) => {
clearTimeout(timer);
reject(new ErrandError(`${path} exited with ${code ?? "a signal"} before it was ready\n${stderr.trim()}`));
});
child.on("error", (error) => {
clearTimeout(timer);
reject(new ErrandError(`${path}: ${error.message}`));
});
}).catch((error: Error) => {
child.kill();
cleanup();
throw error;
});
const cdp = await Cdp.connect(url);
return {
cdp,
path,
exited,
async close() {
try {
await Promise.race([cdp.send("Browser.close"), new Promise((resolve) => setTimeout(resolve, 2000))]);
} catch {
// Already gone.
}
// Cookies reach a kept profile on a clean exit: give Chrome one before the kill.
if (keep) await Promise.race([exited, new Promise((resolve) => setTimeout(resolve, 10_000))]);
cdp.close();
child.kill();
cleanup();
},
};
}
/**
* Rule 11: a runner may present a normal desktop user agent by dropping
* `HeadlessChrome` from the string. That is all this does: no other header,
* no navigator patching, no fingerprint change.
*/
export function plainUserAgent(userAgent: string): string | null {
return userAgent.includes("HeadlessChrome") ? userAgent.replace("HeadlessChrome", "Chrome") : null;
}