@jeffawe/understudy 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +127 -0
- package/README.md +69 -172
- package/REASONER.md +76 -86
- package/dist/adapters/recorder/script.d.ts +25 -2
- package/dist/adapters/recorder/script.js +126 -31
- package/dist/core/outline.d.ts +43 -0
- package/dist/core/outline.js +121 -0
- package/dist/core/recording.d.ts +19 -1
- package/dist/core/recording.js +38 -2
- package/dist/core/replay.d.ts +62 -1
- package/dist/core/replay.js +162 -106
- package/dist/entry/cli.js +110 -29
- package/dist/entry/mcp.d.ts +1 -0
- package/dist/entry/mcp.js +114 -23
- package/dist/flows/destructive.js +1 -1
- package/dist/flows/distill.js +11 -6
- package/dist/flows/emit.d.ts +7 -0
- package/dist/flows/emit.js +26 -10
- package/dist/flows/execute.d.ts +12 -0
- package/dist/flows/execute.js +23 -16
- package/dist/flows/runs.d.ts +2 -0
- package/dist/flows/runs.js +7 -3
- package/dist/flows/save.d.ts +5 -2
- package/dist/flows/save.js +38 -23
- package/dist/flows/session.d.ts +176 -0
- package/dist/flows/session.js +383 -0
- package/dist/flows/variants.d.ts +81 -0
- package/dist/flows/variants.js +139 -0
- package/dist/memory/calibrate.js +3 -1
- package/dist/memory/pages.d.ts +34 -0
- package/dist/memory/pages.js +60 -0
- package/dist/memory/recall.d.ts +56 -2
- package/dist/memory/recall.js +194 -30
- package/dist/plan/planner.d.ts +11 -1
- package/dist/plan/planner.js +36 -19
- package/dist/store/app-settings.d.ts +26 -0
- package/dist/store/app-settings.js +49 -0
- package/dist/store/db.js +8 -1
- package/dist/store/types.d.ts +18 -0
- package/package.json +3 -2
package/dist/core/replay.d.ts
CHANGED
|
@@ -21,9 +21,11 @@
|
|
|
21
21
|
* sig(), compare, capture. This is not scaffolding for the recorder; it is the
|
|
22
22
|
* first half of the thing that runs tests.
|
|
23
23
|
*/
|
|
24
|
+
import { type Control } from './outline.js';
|
|
25
|
+
import { type Page, type Response } from 'playwright';
|
|
24
26
|
import { type BrowserLease } from './browser.js';
|
|
25
27
|
import { type VisualCheck } from './visual.js';
|
|
26
|
-
import { type HumanUntil, type RawEvent, type RawRecording } from './recording.js';
|
|
28
|
+
import { type HumanUntil, type ResponseCheck, type RawEvent, type RawRecording } from './recording.js';
|
|
27
29
|
import type { StorageState } from './auth-cache.js';
|
|
28
30
|
import type { PendingDecision } from './types.js';
|
|
29
31
|
export interface StepOutcome {
|
|
@@ -32,6 +34,8 @@ export interface StepOutcome {
|
|
|
32
34
|
ok: boolean;
|
|
33
35
|
/** Why it failed, in one line. */
|
|
34
36
|
error?: string;
|
|
37
|
+
/** The page's controls after the step (no field values) — kept on the page map, compared next time. */
|
|
38
|
+
outline?: Control[];
|
|
35
39
|
/** Page fingerprint AFTER the step. Concatenated into sigSequence. */
|
|
36
40
|
sig?: string;
|
|
37
41
|
durationMs: number;
|
|
@@ -395,4 +399,61 @@ export declare function describeLocator(event: RawEvent): string;
|
|
|
395
399
|
* A glob with no scheme matches anywhere in the URL, as a path would.
|
|
396
400
|
*/
|
|
397
401
|
export declare function urlGlob(glob: string): RegExp;
|
|
402
|
+
/** Every response the page got, by step, for `wait_response` and human steps to look through. */
|
|
403
|
+
declare class ResponseLog {
|
|
404
|
+
private readonly keepBody;
|
|
405
|
+
private seen;
|
|
406
|
+
constructor(keepBody: (url: string) => boolean);
|
|
407
|
+
add(step: number, res: Response): void;
|
|
408
|
+
/**
|
|
409
|
+
* The first unused answer since `since` that matches, waiting up to
|
|
410
|
+
* `timeoutMs` for one. A wait step uses it up, so two waits for the same
|
|
411
|
+
* address need two answers; a human step only looks — it checks the same
|
|
412
|
+
* answer twice (while waiting, and after they say done).
|
|
413
|
+
*/
|
|
414
|
+
wait(check: ResponseCheck, since: number, timeoutMs: number, consume?: boolean): Promise<void>;
|
|
415
|
+
}
|
|
416
|
+
/** Where one step sits, for the parts of running it that depend on its neighbours. */
|
|
417
|
+
export interface StepContext {
|
|
418
|
+
/** A prelude step: how the run got to the start, not part of the path. */
|
|
419
|
+
prelude?: boolean;
|
|
420
|
+
/** Stop before this step: look at its control, don't use it (see ReplayOptions.stopBefore). */
|
|
421
|
+
holding?: boolean;
|
|
422
|
+
/** The pages the steps either side expect — landing on one is a step out of phase, not a surprise. */
|
|
423
|
+
neighbours?: (string | undefined)[];
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Runs steps, one at a time, on one page — what replay walks a recording with,
|
|
427
|
+
* and what an agent's session drives step by step. ONE executor: a step an
|
|
428
|
+
* agent tries in a session behaves exactly as it will when the saved flow is
|
|
429
|
+
* replayed.
|
|
430
|
+
*
|
|
431
|
+
* Owns what the steps share: the console, error, request and response logs
|
|
432
|
+
* (each tagged with the step it happened during), the page fingerprints seen,
|
|
433
|
+
* and which route the last step left the page on.
|
|
434
|
+
*/
|
|
435
|
+
export declare class Stepper {
|
|
436
|
+
private readonly opts;
|
|
437
|
+
readonly page: Page;
|
|
438
|
+
readonly signals: CapturedSignal[];
|
|
439
|
+
readonly sigSequence: string[];
|
|
440
|
+
readonly escalations: Escalation[];
|
|
441
|
+
readonly visualChecks: VisualCheck[];
|
|
442
|
+
readonly responses: ResponseLog;
|
|
443
|
+
/** The step running now, so a signal says which one it happened during. */
|
|
444
|
+
currentStep: number;
|
|
445
|
+
/** The last step that did something, so a wait only counts what came after it. */
|
|
446
|
+
lastActionSeq: number;
|
|
447
|
+
/** Route of the page we are on, so a lesson can be scoped to one page. */
|
|
448
|
+
lastPattern: string | undefined;
|
|
449
|
+
private readonly maxAttempts;
|
|
450
|
+
/**
|
|
451
|
+
* `events` are the steps it will run, when known, so the responses a wait
|
|
452
|
+
* will check keep their bodies. `keepBody` decides instead, when they aren't.
|
|
453
|
+
*/
|
|
454
|
+
constructor(lease: BrowserLease, opts?: ReplayOptions, events?: RawEvent[], keepBody?: (url: string) => boolean);
|
|
455
|
+
/** Run one step: lessons first, then the step, retried while it is safe to. */
|
|
456
|
+
run(event: RawEvent, ctx?: StepContext): Promise<StepOutcome>;
|
|
457
|
+
}
|
|
398
458
|
export declare function replay(recording: RawRecording, opts?: ReplayOptions): Promise<ReplayResult>;
|
|
459
|
+
export {};
|
package/dist/core/replay.js
CHANGED
|
@@ -21,11 +21,12 @@
|
|
|
21
21
|
* sig(), compare, capture. This is not scaffolding for the recorder; it is the
|
|
22
22
|
* first half of the thing that runs tests.
|
|
23
23
|
*/
|
|
24
|
+
import { parseOutline, OUTLINE_MAX } from './outline.js';
|
|
24
25
|
import {} from 'playwright';
|
|
25
26
|
import { openLease } from './browser.js';
|
|
26
27
|
import { computeSig, urlPattern, waitForAriaStable } from './sig.js';
|
|
27
28
|
import { acceptBaseline, captureCheckpoint, worthJudging, VISUAL_SEVERE } from './visual.js';
|
|
28
|
-
import { describeResponse, forced, humanUntil, responseCheckOf } from './recording.js';
|
|
29
|
+
import { describeResponse, forced, humanUntil, responseCheckOf, literalValue } from './recording.js';
|
|
29
30
|
/** An assertion that failed, carrying what it actually saw. */
|
|
30
31
|
class AssertionFailed extends Error {
|
|
31
32
|
saw;
|
|
@@ -224,9 +225,11 @@ export function urlGlob(glob) {
|
|
|
224
225
|
}
|
|
225
226
|
/** Steps that only look, so a response wait still counts what came before them. */
|
|
226
227
|
const WAITS = new Set(['wait_url', 'wait_text', 'wait_response', 'assert', 'snapshot']);
|
|
227
|
-
/** A substring of the address,
|
|
228
|
-
function urlMatches(
|
|
229
|
-
|
|
228
|
+
/** A substring of the address, a glob over it, or a regex. */
|
|
229
|
+
function urlMatches(check, href) {
|
|
230
|
+
if (check.regexFlags !== undefined)
|
|
231
|
+
return new RegExp(check.url, check.regexFlags).test(href);
|
|
232
|
+
return check.url.includes('*') ? urlGlob(check.url).test(href) : href.includes(check.url);
|
|
230
233
|
}
|
|
231
234
|
/** `items.0.id` of a parsed JSON body. */
|
|
232
235
|
function fieldAt(body, path) {
|
|
@@ -264,7 +267,7 @@ class ResponseLog {
|
|
|
264
267
|
const near = new Set();
|
|
265
268
|
for (;;) {
|
|
266
269
|
for (const r of this.seen) {
|
|
267
|
-
if (r.used || r.step < since || !urlMatches(check
|
|
270
|
+
if (r.used || r.step < since || !urlMatches(check, r.url))
|
|
268
271
|
continue;
|
|
269
272
|
const said = `${r.method} ${new URL(r.url).pathname} → ${r.status}`;
|
|
270
273
|
if ((check.method && r.method !== check.method) || (check.status !== undefined && r.status !== check.status)) {
|
|
@@ -377,6 +380,16 @@ async function checkAssert(page, event, values, timeoutMs) {
|
|
|
377
380
|
case 'checked':
|
|
378
381
|
case 'unchecked':
|
|
379
382
|
return until(() => target.isChecked({ timeout: 1000 }), (v) => v === (kind === 'checked'), `expected ${desc} to be ${kind}`);
|
|
383
|
+
// PAGE-READY CHECKS. The control is there but not usable yet — a sign-in
|
|
384
|
+
// button that stays disabled until the app has loaded. Polled like the rest,
|
|
385
|
+
// so the step waits exactly as long as the page needs, not a fixed sleep.
|
|
386
|
+
case 'enabled':
|
|
387
|
+
case 'disabled':
|
|
388
|
+
return until(() => target.isEnabled({ timeout: 1000 }), (v) => v === (kind === 'enabled'), `expected ${desc} to be ${kind}`, async () => ((await target.isEnabled({ timeout: 1000 })) ? 'enabled' : 'disabled'));
|
|
389
|
+
case 'editable':
|
|
390
|
+
return until(() => target.isEditable({ timeout: 1000 }), (v) => v === true, `expected ${desc} to be editable`, async () => 'not editable');
|
|
391
|
+
case 'focused':
|
|
392
|
+
return until(() => target.evaluate((el) => el.ownerDocument.activeElement === el, undefined, { timeout: 1000 }), (v) => v === true, `expected ${desc} to be focused`, async () => 'not focused');
|
|
380
393
|
default:
|
|
381
394
|
throw new Error(`replay does not implement assert '${kind}'`);
|
|
382
395
|
}
|
|
@@ -422,105 +435,112 @@ async function waitForUrlSettled(page, opts = {}, quietMs = 400, budgetMs = 5000
|
|
|
422
435
|
const NAVIGATING_ACTIONS = new Set(['click', 'press', 'goto', 'select']);
|
|
423
436
|
/** Resolve a step's value, whether literal or a reference. */
|
|
424
437
|
function valueFor(event, values) {
|
|
425
|
-
|
|
426
|
-
|
|
438
|
+
const literal = literalValue(event);
|
|
439
|
+
if (literal !== undefined)
|
|
440
|
+
return literal;
|
|
427
441
|
if (event.valueRef === undefined)
|
|
428
442
|
return undefined;
|
|
429
443
|
return values[event.valueRef];
|
|
430
444
|
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
// fired. A 500 is interesting; a 500 *during checkout* is a finding.
|
|
452
|
-
let currentStep = -1;
|
|
445
|
+
/**
|
|
446
|
+
* Runs steps, one at a time, on one page — what replay walks a recording with,
|
|
447
|
+
* and what an agent's session drives step by step. ONE executor: a step an
|
|
448
|
+
* agent tries in a session behaves exactly as it will when the saved flow is
|
|
449
|
+
* replayed.
|
|
450
|
+
*
|
|
451
|
+
* Owns what the steps share: the console, error, request and response logs
|
|
452
|
+
* (each tagged with the step it happened during), the page fingerprints seen,
|
|
453
|
+
* and which route the last step left the page on.
|
|
454
|
+
*/
|
|
455
|
+
export class Stepper {
|
|
456
|
+
opts;
|
|
457
|
+
page;
|
|
458
|
+
signals = [];
|
|
459
|
+
sigSequence = [];
|
|
460
|
+
escalations = [];
|
|
461
|
+
visualChecks = [];
|
|
462
|
+
responses;
|
|
463
|
+
/** The step running now, so a signal says which one it happened during. */
|
|
464
|
+
currentStep = -1;
|
|
453
465
|
/** The last step that did something, so a wait only counts what came after it. */
|
|
454
|
-
|
|
455
|
-
// EVERY RESPONSE IS LOGGED, not just the ones a wait is waiting on now. The
|
|
456
|
-
// click that triggers `POST /api/visits` usually gets its answer before the
|
|
457
|
-
// wait step after it even starts; a wait that only listened from then on
|
|
458
|
-
// would miss it every time.
|
|
459
|
-
const checks = [
|
|
460
|
-
...recording.events.filter((e) => e.action === 'wait_response').map((e) => responseCheckOf(e.value, e.hints)),
|
|
461
|
-
...recording.events.filter((e) => e.action === 'human').map((e) => humanUntil(e)?.response),
|
|
462
|
-
].filter((c) => Boolean(c?.field));
|
|
463
|
-
const responses = new ResponseLog((url) => checks.some((c) => urlMatches(c.url, url)));
|
|
466
|
+
lastActionSeq = -1;
|
|
464
467
|
/** Route of the page we are on, so a lesson can be scoped to one page. */
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
468
|
+
lastPattern;
|
|
469
|
+
maxAttempts;
|
|
470
|
+
/**
|
|
471
|
+
* `events` are the steps it will run, when known, so the responses a wait
|
|
472
|
+
* will check keep their bodies. `keepBody` decides instead, when they aren't.
|
|
473
|
+
*/
|
|
474
|
+
constructor(lease, opts = {}, events = [], keepBody) {
|
|
475
|
+
this.opts = opts;
|
|
476
|
+
this.page = lease.page;
|
|
477
|
+
this.maxAttempts = Math.max(1, opts.budget?.stepAttempts ?? 1);
|
|
478
|
+
const captureBodies = opts.captureBodies ?? true;
|
|
479
|
+
// `currentStep` is what turns a pile of console noise into evidence: it is
|
|
480
|
+
// the correlation between a signal and the intent that was executing when it
|
|
481
|
+
// fired. A 500 is interesting; a 500 *during checkout* is a finding.
|
|
482
|
+
/** The last step that did something, so a wait only counts what came after it. */
|
|
483
|
+
// EVERY RESPONSE IS LOGGED, not just the ones a wait is waiting on now. The
|
|
484
|
+
// click that triggers `POST /api/visits` usually gets its answer before the
|
|
485
|
+
// wait step after it even starts; a wait that only listened from then on
|
|
486
|
+
// would miss it every time.
|
|
487
|
+
const checks = [
|
|
488
|
+
...events.filter((e) => e.action === 'wait_response').map((e) => responseCheckOf(e.value, e.hints)),
|
|
489
|
+
...events.filter((e) => e.action === 'human').map((e) => humanUntil(e)?.response),
|
|
490
|
+
].filter((c) => Boolean(c?.field));
|
|
491
|
+
this.responses = new ResponseLog((url) => keepBody?.(url) ?? checks.some((c) => urlMatches(c, url)));
|
|
492
|
+
lease.page.on('console', (msg) => {
|
|
493
|
+
if (msg.type() !== 'error' && msg.type() !== 'warning')
|
|
494
|
+
return;
|
|
495
|
+
this.signals.push({ kind: 'console', level: msg.type() === 'error' ? 'error' : 'warning', duringStep: this.currentStep, text: msg.text().slice(0, 500) });
|
|
480
496
|
});
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
responses.add(currentStep, res);
|
|
484
|
-
const status = res.status();
|
|
485
|
-
const contentType = res.headers()['content-type'] ?? '';
|
|
486
|
-
const interesting = status >= 400 || contentType.includes('application/json');
|
|
487
|
-
if (!interesting)
|
|
488
|
-
return;
|
|
489
|
-
let body;
|
|
490
|
-
if (captureBodies && status >= 400) {
|
|
491
|
-
// Bodies can fail to read on redirects and aborted requests; a missing
|
|
492
|
-
// body should never take down the replay.
|
|
493
|
-
body = await res.text().then((t) => t.slice(0, MAX_BODY)).catch(() => undefined);
|
|
494
|
-
}
|
|
495
|
-
signals.push({
|
|
496
|
-
kind: 'http',
|
|
497
|
-
duringStep: currentStep,
|
|
498
|
-
text: `${res.request().method()} ${status}`,
|
|
499
|
-
url: res.url(),
|
|
500
|
-
status,
|
|
501
|
-
...(body ? { body } : {}),
|
|
497
|
+
lease.page.on('pageerror', (err) => {
|
|
498
|
+
this.signals.push({ kind: 'pageerror', duringStep: this.currentStep, text: String(err).slice(0, 500) });
|
|
502
499
|
});
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
500
|
+
lease.page.on('requestfailed', (req) => {
|
|
501
|
+
this.signals.push({
|
|
502
|
+
kind: 'requestfailed',
|
|
503
|
+
duringStep: this.currentStep,
|
|
504
|
+
text: req.failure()?.errorText ?? 'request failed',
|
|
505
|
+
url: req.url(),
|
|
506
|
+
});
|
|
507
|
+
});
|
|
508
|
+
lease.page.on('response', async (res) => {
|
|
509
|
+
this.responses.add(this.currentStep, res);
|
|
510
|
+
const status = res.status();
|
|
511
|
+
const contentType = res.headers()['content-type'] ?? '';
|
|
512
|
+
const interesting = status >= 400 || contentType.includes('application/json');
|
|
513
|
+
if (!interesting)
|
|
514
|
+
return;
|
|
515
|
+
let body;
|
|
516
|
+
if (captureBodies && status >= 400) {
|
|
517
|
+
// Bodies can fail to read on redirects and aborted requests; a missing
|
|
518
|
+
// body should never take down the replay.
|
|
519
|
+
body = await res.text().then((t) => t.slice(0, MAX_BODY)).catch(() => undefined);
|
|
520
|
+
}
|
|
521
|
+
this.signals.push({
|
|
522
|
+
kind: 'http',
|
|
523
|
+
duringStep: this.currentStep,
|
|
524
|
+
text: `${res.request().method()} ${status}`,
|
|
525
|
+
url: res.url(),
|
|
526
|
+
status,
|
|
527
|
+
...(body ? { body } : {}),
|
|
528
|
+
});
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
/** Run one step: lessons first, then the step, retried while it is safe to. */
|
|
532
|
+
async run(event, ctx = {}) {
|
|
533
|
+
const { page, responses, sigSequence, escalations, visualChecks } = this;
|
|
534
|
+
const { values = {}, visualCheck, onDecision, lessonsFor, onHuman } = this.opts;
|
|
535
|
+
// A step may say it needs longer than the rest (`hints.timeoutMs`): the
|
|
536
|
+
// login that takes 15 s to become usable, without slowing every other step.
|
|
537
|
+
const own = event.hints?.timeoutMs;
|
|
538
|
+
const timeoutMs = typeof own === 'number' && own > 0 ? own : this.opts.timeoutMs ?? 10_000;
|
|
539
|
+
this.currentStep = event.seq;
|
|
519
540
|
/** Set the instant an action goes out, so a retry cannot double-submit. */
|
|
520
541
|
let dispatched = false;
|
|
521
|
-
const stepStart = Date.now();
|
|
522
542
|
if (!WAITS.has(event.action))
|
|
523
|
-
lastActionSeq = event.seq;
|
|
543
|
+
this.lastActionSeq = event.seq;
|
|
524
544
|
const here = page.url();
|
|
525
545
|
const outcome = {
|
|
526
546
|
seq: event.seq, action: event.action, ok: false, durationMs: 0,
|
|
@@ -529,13 +549,13 @@ export async function replay(recording, opts = {}) {
|
|
|
529
549
|
// THE COMMIT STEP OF A stopBefore REPLAY. A control is looked at below,
|
|
530
550
|
// just before it would be dispatched; a step with no control (a goto to the
|
|
531
551
|
// payment site, a wait) is simply not done.
|
|
532
|
-
const holding =
|
|
552
|
+
const holding = Boolean(ctx.holding);
|
|
533
553
|
const holdsWithoutLooking = holding && ['goto', 'wait_url', 'wait_text', 'assert', 'snapshot', 'human'].includes(event.action);
|
|
534
554
|
if (holdsWithoutLooking) {
|
|
535
555
|
outcome.ok = true;
|
|
536
556
|
outcome.held = true;
|
|
537
557
|
}
|
|
538
|
-
const attemptsHere = holdsWithoutLooking ? 0 : maxAttempts;
|
|
558
|
+
const attemptsHere = holdsWithoutLooking ? 0 : this.maxAttempts;
|
|
539
559
|
// WHAT HAVE WE LEARNED ABOUT THIS STEP? Consulted BEFORE acting, because a
|
|
540
560
|
// lesson's whole purpose is "do Y first".
|
|
541
561
|
let applied = [];
|
|
@@ -545,7 +565,7 @@ export async function replay(recording, opts = {}) {
|
|
|
545
565
|
// would never fire if we only ever looked backwards.
|
|
546
566
|
const contextPattern = event.action === 'goto' && (event.value ?? event.url)
|
|
547
567
|
? urlPattern(event.value ?? event.url)
|
|
548
|
-
: lastPattern;
|
|
568
|
+
: this.lastPattern;
|
|
549
569
|
applied = await lessonsFor({
|
|
550
570
|
...(contextPattern ? { url_pattern: contextPattern } : {}),
|
|
551
571
|
action: event.action,
|
|
@@ -623,7 +643,7 @@ export async function replay(recording, opts = {}) {
|
|
|
623
643
|
const check = responseCheckOf(event.value, event.hints);
|
|
624
644
|
if (!check)
|
|
625
645
|
throw new Error('wait_response has no address to wait for');
|
|
626
|
-
await responses.wait(check, lastActionSeq, timeoutMs);
|
|
646
|
+
await responses.wait(check, this.lastActionSeq, timeoutMs);
|
|
627
647
|
outcome.ok = true;
|
|
628
648
|
}
|
|
629
649
|
else if (event.action === 'human') {
|
|
@@ -635,7 +655,7 @@ export async function replay(recording, opts = {}) {
|
|
|
635
655
|
if (!onHuman)
|
|
636
656
|
throw new Error(`needs a person: ${instruction} — nobody is here to ask`);
|
|
637
657
|
const until = humanUntil(event);
|
|
638
|
-
const waitMs = opts.humanTimeoutMs ?? 15 * 60_000;
|
|
658
|
+
const waitMs = this.opts.humanTimeoutMs ?? 15 * 60_000;
|
|
639
659
|
const stop = new AbortController();
|
|
640
660
|
const shown = until ? pageShows(page, until, waitMs, responses, event.seq).then(() => 'page') : new Promise(() => { });
|
|
641
661
|
shown.catch(() => { });
|
|
@@ -928,17 +948,20 @@ export async function replay(recording, opts = {}) {
|
|
|
928
948
|
// ("starts at /overview, execution is at /auth/otp") and binds its
|
|
929
949
|
// neighbour instead.
|
|
930
950
|
await waitForUrlSettled(page, { mayNavigate: NAVIGATING_ACTIONS.has(event.action) });
|
|
931
|
-
|
|
932
|
-
const
|
|
951
|
+
// The settled tree is read once, for the fingerprint and the outline both.
|
|
952
|
+
const tree = await waitForAriaStable(page, 1500);
|
|
953
|
+
const sig = await computeSig(page, tree || undefined);
|
|
933
954
|
outcome.sig = sig.sig;
|
|
934
|
-
|
|
955
|
+
if (tree)
|
|
956
|
+
outcome.outline = parseOutline(tree).slice(0, OUTLINE_MAX);
|
|
957
|
+
this.lastPattern = sig.urlPattern;
|
|
935
958
|
// PRELUDE SIGS ARE NOT PART OF THE FLOW'S PATH. `sigSequence[0]` becomes
|
|
936
959
|
// the flow's `start_state` at ingest and `[last]` its `end_state`, so
|
|
937
960
|
// letting the prelude in shifts the whole boundary — and the consecutive-
|
|
938
961
|
// dedupe just below would additionally SWALLOW the recording's real first
|
|
939
962
|
// sig whenever it equals the prelude's last, silently, which is exactly
|
|
940
963
|
// the off-by-one the comment above is about.
|
|
941
|
-
if (!
|
|
964
|
+
if (!ctx.prelude && sigSequence[sigSequence.length - 1] !== sig.sig) {
|
|
942
965
|
sigSequence.push(sig.sig);
|
|
943
966
|
}
|
|
944
967
|
// AM I WHERE I EXPECTED TO BE?
|
|
@@ -954,8 +977,7 @@ export async function replay(recording, opts = {}) {
|
|
|
954
977
|
// the page the previous step expected (not there yet) or the one the
|
|
955
978
|
// next step expects (there already) is the same path, a step out of
|
|
956
979
|
// phase. Never for the first step: that one detects a stale session.
|
|
957
|
-
const
|
|
958
|
-
const inPhase = neighbours.includes(sig.sig);
|
|
980
|
+
const inPhase = (ctx.neighbours ?? []).includes(sig.sig);
|
|
959
981
|
if (event.expectedSig && event.expectedSig !== sig.sig && !inPhase) {
|
|
960
982
|
outcome.unexpectedPage = { expected: event.expectedSig, observed: sig.sig };
|
|
961
983
|
// THE ESCALATION. An unexpected page is not automatically wrong — an
|
|
@@ -1007,6 +1029,40 @@ export async function replay(recording, opts = {}) {
|
|
|
1007
1029
|
await waitForAriaStable(page, 1500).catch(() => { });
|
|
1008
1030
|
outcome.attempts = attempt + 1;
|
|
1009
1031
|
}
|
|
1032
|
+
return outcome;
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
export async function replay(recording, opts = {}) {
|
|
1036
|
+
const { headless = true, timeoutMs = 10_000, onDecision, storageState, prelude, onHandoff, budget, stopBefore } = opts;
|
|
1037
|
+
let stoppedBefore;
|
|
1038
|
+
const handoffs = [];
|
|
1039
|
+
// OWNERSHIP IS THE CALLER'S, NOT THE LEASE'S. A lease handed in belongs to
|
|
1040
|
+
// whoever opened it and may well be replayed into again — closing it here
|
|
1041
|
+
// would tear down the context a prelude just walked, one line before the
|
|
1042
|
+
// recorder was going to capture from it.
|
|
1043
|
+
const ownsLease = !opts.lease;
|
|
1044
|
+
const lease = opts.lease ?? await openLease({ headless, ...(storageState ? { storageState } : {}) });
|
|
1045
|
+
const page = lease.page;
|
|
1046
|
+
const steps = [];
|
|
1047
|
+
const startedAt = Date.now();
|
|
1048
|
+
const stepper = new Stepper(lease, opts, recording.events);
|
|
1049
|
+
const { sigSequence, signals, escalations, visualChecks } = stepper;
|
|
1050
|
+
// The prelude is walked through the SAME step walker — a prelude step that
|
|
1051
|
+
// silently behaved differently from a recorded one would be a second executor,
|
|
1052
|
+
// which is the thing this file exists not to have.
|
|
1053
|
+
const walk = [
|
|
1054
|
+
...(prelude ?? []).map((event) => ({ event, isPrelude: true })),
|
|
1055
|
+
...recording.events.map((event) => ({ event, isPrelude: false })),
|
|
1056
|
+
];
|
|
1057
|
+
const preludeSteps = [];
|
|
1058
|
+
let preludeFailed = false;
|
|
1059
|
+
for (let i = 0; i < walk.length; i++) {
|
|
1060
|
+
const { event, isPrelude } = walk[i];
|
|
1061
|
+
const budgetSpent = budget?.exceeded?.();
|
|
1062
|
+
const stepStart = Date.now();
|
|
1063
|
+
const holding = !isPrelude && stopBefore === event.seq;
|
|
1064
|
+
const neighbours = i > 0 ? [walk[i - 1]?.event.expectedSig, walk[i + 1]?.event.expectedSig] : [];
|
|
1065
|
+
const outcome = await stepper.run(event, { prelude: isPrelude, holding, neighbours });
|
|
1010
1066
|
// A step that FAILED is the other place judgement is needed: a missing
|
|
1011
1067
|
// element may be rot to heal or a genuine gap to ask about, and only the
|
|
1012
1068
|
// selector's health tells them apart.
|
|
@@ -1067,7 +1123,7 @@ export async function replay(recording, opts = {}) {
|
|
|
1067
1123
|
if (sigSequence[sigSequence.length - 1] !== landed.sig)
|
|
1068
1124
|
sigSequence.push(landed.sig);
|
|
1069
1125
|
outcome.sig = landed.sig;
|
|
1070
|
-
lastPattern = landed.urlPattern;
|
|
1126
|
+
stepper.lastPattern = landed.urlPattern;
|
|
1071
1127
|
handoffs.push({ seq: event.seq, trigger, reason: request.reason, steps: 0, url: page.url() });
|
|
1072
1128
|
if (trigger === 'unresolved_seam') {
|
|
1073
1129
|
delete event.hints.seamGapBefore;
|
|
@@ -1101,7 +1157,7 @@ export async function replay(recording, opts = {}) {
|
|
|
1101
1157
|
if (sigSequence[sigSequence.length - 1] !== landed.sig)
|
|
1102
1158
|
sigSequence.push(landed.sig);
|
|
1103
1159
|
outcome.sig = landed.sig;
|
|
1104
|
-
lastPattern = landed.urlPattern;
|
|
1160
|
+
stepper.lastPattern = landed.urlPattern;
|
|
1105
1161
|
}
|
|
1106
1162
|
handoffs.push({
|
|
1107
1163
|
seq: event.seq,
|