@jeffawe/understudy 0.0.0-stage → 0.2.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.
Files changed (99) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +171 -2
  3. package/REASONER.md +67 -0
  4. package/build/extension/firefox/background.js +186 -0
  5. package/build/extension/firefox/icons/icon-128.png +0 -0
  6. package/build/extension/firefox/icons/icon-16.png +0 -0
  7. package/build/extension/firefox/icons/icon-32.png +0 -0
  8. package/build/extension/firefox/icons/icon-48.png +0 -0
  9. package/build/extension/firefox/manifest.json +54 -0
  10. package/build/extension/firefox/popup.html +52 -0
  11. package/build/extension/firefox/popup.js +106 -0
  12. package/build/extension/firefox/recorder.js +362 -0
  13. package/dist/adapters/recorder/accname-bundle.d.ts +12 -0
  14. package/dist/adapters/recorder/accname-bundle.js +12 -0
  15. package/dist/adapters/recorder/extension.d.ts +57 -0
  16. package/dist/adapters/recorder/extension.js +146 -0
  17. package/dist/adapters/recorder/injected.d.ts +21 -0
  18. package/dist/adapters/recorder/injected.js +352 -0
  19. package/dist/adapters/recorder/live.d.ts +105 -0
  20. package/dist/adapters/recorder/live.js +317 -0
  21. package/dist/adapters/recorder/script.d.ts +43 -0
  22. package/dist/adapters/recorder/script.js +530 -0
  23. package/dist/adapters/recorder/wire.d.ts +42 -0
  24. package/dist/adapters/recorder/wire.js +51 -0
  25. package/dist/core/auth-cache.d.ts +119 -0
  26. package/dist/core/auth-cache.js +216 -0
  27. package/dist/core/browser.d.ts +47 -0
  28. package/dist/core/browser.js +82 -0
  29. package/dist/core/distill.d.ts +118 -0
  30. package/dist/core/distill.js +197 -0
  31. package/dist/core/fingerprint.d.ts +15 -0
  32. package/dist/core/fingerprint.js +21 -0
  33. package/dist/core/recording-store.d.ts +34 -0
  34. package/dist/core/recording-store.js +64 -0
  35. package/dist/core/recording.d.ts +185 -0
  36. package/dist/core/recording.js +100 -0
  37. package/dist/core/replay.d.ts +307 -0
  38. package/dist/core/replay.js +804 -0
  39. package/dist/core/sig.d.ts +70 -0
  40. package/dist/core/sig.js +0 -0
  41. package/dist/core/trigger.d.ts +18 -0
  42. package/dist/core/trigger.js +35 -0
  43. package/dist/core/types.d.ts +28 -0
  44. package/dist/core/types.js +8 -0
  45. package/dist/core/visual.d.ts +80 -0
  46. package/dist/core/visual.js +111 -0
  47. package/dist/entry/cli.d.ts +9 -0
  48. package/dist/entry/cli.js +1008 -0
  49. package/dist/entry/mcp.d.ts +20 -0
  50. package/dist/entry/mcp.js +288 -0
  51. package/dist/flows/destructive.d.ts +19 -0
  52. package/dist/flows/destructive.js +48 -0
  53. package/dist/flows/distill.d.ts +28 -0
  54. package/dist/flows/distill.js +109 -0
  55. package/dist/flows/drift.d.ts +43 -0
  56. package/dist/flows/drift.js +116 -0
  57. package/dist/flows/emit.d.ts +23 -0
  58. package/dist/flows/emit.js +133 -0
  59. package/dist/flows/execute.d.ts +75 -0
  60. package/dist/flows/execute.js +133 -0
  61. package/dist/flows/findings.d.ts +38 -0
  62. package/dist/flows/findings.js +136 -0
  63. package/dist/flows/runs.d.ts +42 -0
  64. package/dist/flows/runs.js +93 -0
  65. package/dist/flows/save.d.ts +74 -0
  66. package/dist/flows/save.js +171 -0
  67. package/dist/memory/notes.d.ts +89 -0
  68. package/dist/memory/notes.js +163 -0
  69. package/dist/memory/placeholders.d.ts +41 -0
  70. package/dist/memory/placeholders.js +139 -0
  71. package/dist/memory/recall.d.ts +118 -0
  72. package/dist/memory/recall.js +147 -0
  73. package/dist/memory/vocabulary.d.ts +9 -0
  74. package/dist/memory/vocabulary.js +24 -0
  75. package/dist/plan/planner.d.ts +69 -0
  76. package/dist/plan/planner.js +211 -0
  77. package/dist/plan/seams.d.ts +58 -0
  78. package/dist/plan/seams.js +99 -0
  79. package/dist/store/db.d.ts +62 -0
  80. package/dist/store/db.js +183 -0
  81. package/dist/store/embed.d.ts +46 -0
  82. package/dist/store/embed.js +100 -0
  83. package/dist/store/reembed.d.ts +13 -0
  84. package/dist/store/reembed.js +32 -0
  85. package/dist/store/settings.d.ts +67 -0
  86. package/dist/store/settings.js +151 -0
  87. package/dist/store/types.d.ts +220 -0
  88. package/dist/store/types.js +14 -0
  89. package/extension/README.md +79 -0
  90. package/extension/background.js +186 -0
  91. package/extension/icons/icon-128.png +0 -0
  92. package/extension/icons/icon-16.png +0 -0
  93. package/extension/icons/icon-32.png +0 -0
  94. package/extension/icons/icon-48.png +0 -0
  95. package/extension/manifest.json +38 -0
  96. package/extension/popup.html +52 -0
  97. package/extension/popup.js +106 -0
  98. package/extension/recorder.js +362 -0
  99. package/package.json +68 -4
@@ -0,0 +1,307 @@
1
+ /**
2
+ * Replay — re-run a RawRecording headless, and watch everything.
3
+ *
4
+ * Three jobs, and only the first is obvious:
5
+ *
6
+ * 1. VERIFY. A recording that will not reproduce must never become memory.
7
+ * PLAN.md's rule: won't replay → `needs_review`, never promoted. Bad
8
+ * memory is worse than no memory, because the agent acts on it confidently.
9
+ *
10
+ * 2. ENRICH. Per-step page fingerprint, console errors, network responses with
11
+ * bodies. All of it captured unconditionally and with no model involved,
12
+ * which is what makes findings detection free and what gives flow-drift its
13
+ * baseline.
14
+ *
15
+ * 3. CONFIRM ADDRESSING. Names are computed in-page at capture, but a name
16
+ * that resolved then may not resolve now. Replay is where a locator is
17
+ * proven to still find exactly one element.
18
+ *
19
+ * THE STEP WALKER HERE IS THE EXECUTOR'S STEP WALKER. Running a recording and
20
+ * running a bound plan are the same operation — resolve a locator, act, compute
21
+ * sig(), compare, capture. This is not scaffolding for the recorder; it is the
22
+ * first half of the thing that runs tests.
23
+ */
24
+ import { type BrowserLease } from './browser.js';
25
+ import { type VisualCheck } from './visual.js';
26
+ import type { RawEvent, RawRecording } from './recording.js';
27
+ import type { StorageState } from './auth-cache.js';
28
+ import type { PendingDecision } from './types.js';
29
+ export interface StepOutcome {
30
+ seq: number;
31
+ action: string;
32
+ ok: boolean;
33
+ /** Why it failed, in one line. */
34
+ error?: string;
35
+ /** Page fingerprint AFTER the step. Concatenated into sigSequence. */
36
+ sig?: string;
37
+ durationMs: number;
38
+ /** How many elements the locator matched. 1 is healthy; >1 is ambiguous. */
39
+ matched?: number;
40
+ /** Set when a filled value did not survive being written. */
41
+ roundTripMismatch?: {
42
+ expected: string;
43
+ actual: string;
44
+ };
45
+ /**
46
+ * Set when the page after this step is NOT the page the step recorded.
47
+ *
48
+ * This is the executor's "unexpected page" signal — where PLAN.md escalates
49
+ * to the reasoner. It is NOT automatically a failure: an app can legitimately
50
+ * gain a banner or a cookie prompt. It is a fact worth carrying, and the
51
+ * judgement about what it means belongs to the reasoner.
52
+ */
53
+ unexpectedPage?: {
54
+ expected: string;
55
+ observed: string;
56
+ };
57
+ /** Result of a visual checkpoint, when this step was one. */
58
+ visual?: VisualCheck;
59
+ /** Lessons whose trigger matched this step, and therefore fired. */
60
+ lessonsApplied?: Array<{
61
+ lessonId: string;
62
+ kind: string;
63
+ title: string;
64
+ }>;
65
+ /** Set when the step needed more than one try. Absent means it passed first time. */
66
+ attempts?: number;
67
+ /** Set when a human took this step over rather than the executor running it. */
68
+ viaHandoff?: boolean;
69
+ /**
70
+ * Set when role+name matched several elements and something more specific
71
+ * had to disambiguate. Feeds `selectors.fragility` — a name that is not
72
+ * unique is a latent flake even when the step passes.
73
+ */
74
+ ambiguousByName?: {
75
+ matched: number;
76
+ disambiguatedBy: string;
77
+ };
78
+ /**
79
+ * Set when role+name matched NOTHING and a captured fallback (test id or
80
+ * css) found the element instead. This is the other half of "raw CSS is the
81
+ * last resort" — a role means nothing when the app renders the control as a
82
+ * bare div/span with no ARIA semantics, which getByRole can never match no
83
+ * matter how accurately the name was captured.
84
+ */
85
+ roleHadNoMatch?: {
86
+ fellBackTo: string;
87
+ };
88
+ }
89
+ export interface CapturedSignal {
90
+ kind: 'console' | 'pageerror' | 'requestfailed' | 'http';
91
+ /** Step this happened during — the correlation that makes signals useful. */
92
+ duringStep: number;
93
+ text: string;
94
+ url?: string;
95
+ status?: number;
96
+ body?: string;
97
+ }
98
+ export interface Escalation {
99
+ step: number;
100
+ kind: PendingDecision['kind'];
101
+ question: Record<string, unknown>;
102
+ answer: Record<string, unknown>;
103
+ }
104
+ export interface ReplayResult {
105
+ hash: string;
106
+ ok: boolean;
107
+ /** True when the recording must NOT be promoted to memory. */
108
+ needsReview: boolean;
109
+ steps: StepOutcome[];
110
+ /** The observed path of page fingerprints — the flow-drift baseline. */
111
+ sigSequence: string[];
112
+ signals: CapturedSignal[];
113
+ durationMs: number;
114
+ /** Every point the executor stopped and asked, with what came back. */
115
+ escalations: Escalation[];
116
+ /** Visual checkpoints taken this run, judged or not. */
117
+ visualChecks: VisualCheck[];
118
+ /** Outcomes for the prelude steps, if any were walked. */
119
+ preludeSteps?: StepOutcome[];
120
+ /**
121
+ * False when the prelude itself failed.
122
+ *
123
+ * Distinct from `ok` on purpose: a prelude that will not replay is an
124
+ * ENVIRONMENT failure — the corpus it references has rotted, or the app
125
+ * changed upstream of what we are verifying. Reporting that as `needsReview`
126
+ * would quarantine a recording whose own steps never ran and were never in
127
+ * question.
128
+ */
129
+ preludeOk?: boolean;
130
+ /**
131
+ * Every point a human took over.
132
+ *
133
+ * A run that needed one is NOT a clean pass, and reporting it as one would
134
+ * manufacture exactly the kind of proof this codebase refuses to manufacture
135
+ * elsewhere. Callers surface this alongside the verdict.
136
+ */
137
+ handoffs: HandoffRecord[];
138
+ }
139
+ export interface ReplayOptions {
140
+ /**
141
+ * Values for `valueRef` steps, e.g. `{ 'SECRET.password': 'hunter2' }`.
142
+ *
143
+ * Recordings deliberately never store credentials, so a recording containing
144
+ * a password CANNOT replay without them. That is the tradeoff working as
145
+ * designed, and an unresolved ref fails the step loudly rather than filling
146
+ * an empty string and producing a confusing downstream failure.
147
+ */
148
+ values?: Record<string, string>;
149
+ /**
150
+ * Start from a saved browser session rather than a cold one.
151
+ *
152
+ * Handed in as a payload, never read from disk here — replay stays ignorant
153
+ * of storage, exactly as it does for lessons and visual baselines.
154
+ *
155
+ * A SESSION MAY ONLY SATISFY WHAT WAS NOT RECORDED. Passing one to verify a
156
+ * recording whose own steps are the login would let ingest "prove" a flow it
157
+ * never ran. Callers decide; replay cannot tell the difference.
158
+ */
159
+ storageState?: StorageState;
160
+ /**
161
+ * Run in a browser someone else owns, instead of launching one.
162
+ *
163
+ * This is what lets a prelude replay and a live capture share ONE context:
164
+ * server-side wizard progress is not in `storageState`, so the only way to
165
+ * start a recording past an intake is to walk it in the very context that is
166
+ * about to be recorded. A lease passed here is never closed by replay.
167
+ *
168
+ * `storageState` is ignored when a lease is supplied — the lease's context
169
+ * already exists and seeding it is the lease owner's decision.
170
+ */
171
+ lease?: BrowserLease;
172
+ /**
173
+ * Steps to walk BEFORE the recording's own, to reach the state it starts from.
174
+ *
175
+ * Resolved by the caller from `entry.prelude`, never looked up here — replay
176
+ * stays ignorant of the database exactly as it does for lessons and sessions.
177
+ * A recording captured with `record --after` holds only the tail, and this is
178
+ * what makes it replayable: a saved session carries a login, but wizard
179
+ * progress is server-side and has to be walked.
180
+ *
181
+ * Accounted for SEPARATELY from the recording's steps. They are how we got to
182
+ * the starting line, not part of what is being verified.
183
+ */
184
+ prelude?: RawEvent[];
185
+ /**
186
+ * Hand a stuck run to a human, and continue with what they reached.
187
+ *
188
+ * A CALLBACK OF ITS OWN, not another `onDecision` kind, because the request
189
+ * carries live `storageState` and every PendingDecision payload is written
190
+ * verbatim into `context_requests.payload`. Keeping the shapes separate is
191
+ * what makes it impossible to leak a session token into the database by
192
+ * adding a field.
193
+ *
194
+ * Omitted means no handoff: the run fails where it always did.
195
+ */
196
+ onHandoff?: (request: HandoffRequest) => Promise<HandoffOutcome>;
197
+ /**
198
+ * How many times to try a step, and when to stop asking and delegate.
199
+ *
200
+ * Read from the environment by the caller, so replay holds no policy.
201
+ */
202
+ budget?: {
203
+ stepAttempts?: number;
204
+ exceeded?: () => string | undefined;
205
+ };
206
+ headless?: boolean;
207
+ /** Per-step timeout. */
208
+ timeoutMs?: number;
209
+ /** Capture response bodies for non-2xx and JSON responses. */
210
+ captureBodies?: boolean;
211
+ /**
212
+ * Called when the executor cannot decide alone. Deliberately a callback
213
+ * rather than the Reasoner interface, so replay stays decoupled from who is
214
+ * answering — Bedrock, the host agent, or nobody at all.
215
+ *
216
+ * Omitted means NO ESCALATION: the run records what it saw and carries on,
217
+ * which is right for a verification replay and wrong for a real test.
218
+ */
219
+ onDecision?: (decision: PendingDecision) => Promise<Record<string, unknown>>;
220
+ /**
221
+ * Turn visual checkpoints on. Opt-in because it only means something when
222
+ * the reasoner can actually look at an image — Bedrock's adapter answers
223
+ * from text alone, so capturing for it would produce files nobody reads.
224
+ */
225
+ visualCheck?: {
226
+ appSlug: string;
227
+ runId: string;
228
+ };
229
+ /**
230
+ * Lessons that apply to a step, looked up before it runs.
231
+ *
232
+ * A callback again, so replay does not need to know about the database or
233
+ * which app it is running. Omitted means lessons are not consulted — correct
234
+ * for verifying a raw recording, wrong for a real run.
235
+ */
236
+ lessonsFor?: (context: {
237
+ url_pattern?: string;
238
+ action?: string;
239
+ role?: string;
240
+ name?: string;
241
+ }) => Promise<Array<{
242
+ lessonId: string;
243
+ kind: string;
244
+ title: string;
245
+ body: string;
246
+ }>>;
247
+ }
248
+ /** What a decision may tell the executor to do. */
249
+ export type DecisionAction = 'continue' | 'abort' | 'retry';
250
+ /** Why the executor gave up on doing this itself. */
251
+ export type HandoffTrigger = 'step_attempts' | 'unimplemented_action' | 'unresolved_seam' | 'budget';
252
+ export interface HandoffRequest {
253
+ trigger: HandoffTrigger;
254
+ /** Step the run is stuck on. */
255
+ seq: number;
256
+ reason: string;
257
+ url: string;
258
+ sig: string;
259
+ expectedSig?: string;
260
+ /**
261
+ * The paused browser's state, for seeding the window the human will drive.
262
+ *
263
+ * NEVER put this in a PendingDecision — it is a live credential and that
264
+ * payload is written to a JSONB column. The handoff implementation writes it
265
+ * to a 0600 file and hands over the PATH.
266
+ */
267
+ storageState: StorageState;
268
+ failed?: {
269
+ action: string;
270
+ target?: string;
271
+ error: string;
272
+ attempts: number;
273
+ };
274
+ }
275
+ /**
276
+ * What came back from a human who was asked to record the missing piece.
277
+ *
278
+ * `adopt` is the default and the only mode built, deliberately. The handoff
279
+ * window shares the paused run's SERVER-SIDE session, so by the time the human
280
+ * closes it the wizard has already advanced. Re-driving their captured steps
281
+ * in the run's own browser would walk an already-advanced flow — locators gone,
282
+ * and any side effect applied twice. So the run adopts the state they reached
283
+ * and continues from the NEXT step; the captured steps are still recorded, they
284
+ * are simply not executed again.
285
+ */
286
+ export type HandoffOutcome = {
287
+ action: 'adopt';
288
+ steps: RawEvent[];
289
+ url: string;
290
+ sig?: string;
291
+ storageState?: StorageState;
292
+ recordingHash?: string;
293
+ } | {
294
+ action: 'skip';
295
+ } | {
296
+ action: 'abort';
297
+ reason?: string;
298
+ };
299
+ export interface HandoffRecord {
300
+ seq: number;
301
+ trigger: HandoffTrigger;
302
+ reason: string;
303
+ recordingHash?: string;
304
+ steps: number;
305
+ url: string;
306
+ }
307
+ export declare function replay(recording: RawRecording, opts?: ReplayOptions): Promise<ReplayResult>;