@monkeytestai/monkeytester 0.0.0-stage → 0.5.1

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 (113) hide show
  1. package/CHANGELOG.md +179 -0
  2. package/LICENSE +661 -0
  3. package/NOTICE +12 -0
  4. package/README.md +196 -2
  5. package/dist/cli.d.ts +43 -0
  6. package/dist/cli.d.ts.map +1 -0
  7. package/dist/cli.js +774 -0
  8. package/dist/cli.js.map +1 -0
  9. package/dist/diff/index.d.ts +78 -0
  10. package/dist/diff/index.d.ts.map +1 -0
  11. package/dist/diff/index.js +119 -0
  12. package/dist/diff/index.js.map +1 -0
  13. package/dist/events.d.ts +66 -0
  14. package/dist/events.d.ts.map +1 -0
  15. package/dist/events.js +77 -0
  16. package/dist/events.js.map +1 -0
  17. package/dist/explorer/index.d.ts +131 -0
  18. package/dist/explorer/index.d.ts.map +1 -0
  19. package/dist/explorer/index.js +675 -0
  20. package/dist/explorer/index.js.map +1 -0
  21. package/dist/explorer/queue.d.ts +94 -0
  22. package/dist/explorer/queue.d.ts.map +1 -0
  23. package/dist/explorer/queue.js +277 -0
  24. package/dist/explorer/queue.js.map +1 -0
  25. package/dist/index.d.ts +36 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +22 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/llm/coerce.d.ts +59 -0
  30. package/dist/llm/coerce.d.ts.map +1 -0
  31. package/dist/llm/coerce.js +246 -0
  32. package/dist/llm/coerce.js.map +1 -0
  33. package/dist/llm/index.d.ts +79 -0
  34. package/dist/llm/index.d.ts.map +1 -0
  35. package/dist/llm/index.js +392 -0
  36. package/dist/llm/index.js.map +1 -0
  37. package/dist/llm/presets.d.ts +61 -0
  38. package/dist/llm/presets.d.ts.map +1 -0
  39. package/dist/llm/presets.js +101 -0
  40. package/dist/llm/presets.js.map +1 -0
  41. package/dist/monkey.d.ts +151 -0
  42. package/dist/monkey.d.ts.map +1 -0
  43. package/dist/monkey.js +323 -0
  44. package/dist/monkey.js.map +1 -0
  45. package/dist/persistence/index.d.ts +118 -0
  46. package/dist/persistence/index.d.ts.map +1 -0
  47. package/dist/persistence/index.js +196 -0
  48. package/dist/persistence/index.js.map +1 -0
  49. package/dist/planner/faults.d.ts +24 -0
  50. package/dist/planner/faults.d.ts.map +1 -0
  51. package/dist/planner/faults.js +128 -0
  52. package/dist/planner/faults.js.map +1 -0
  53. package/dist/planner/forms.d.ts +33 -0
  54. package/dist/planner/forms.d.ts.map +1 -0
  55. package/dist/planner/forms.js +163 -0
  56. package/dist/planner/forms.js.map +1 -0
  57. package/dist/planner/index.d.ts +31 -0
  58. package/dist/planner/index.d.ts.map +1 -0
  59. package/dist/planner/index.js +252 -0
  60. package/dist/planner/index.js.map +1 -0
  61. package/dist/planner/salvage.d.ts +16 -0
  62. package/dist/planner/salvage.d.ts.map +1 -0
  63. package/dist/planner/salvage.js +107 -0
  64. package/dist/planner/salvage.js.map +1 -0
  65. package/dist/runner/actions.d.ts +91 -0
  66. package/dist/runner/actions.d.ts.map +1 -0
  67. package/dist/runner/actions.js +659 -0
  68. package/dist/runner/actions.js.map +1 -0
  69. package/dist/runner/evidence.d.ts +37 -0
  70. package/dist/runner/evidence.d.ts.map +1 -0
  71. package/dist/runner/evidence.js +122 -0
  72. package/dist/runner/evidence.js.map +1 -0
  73. package/dist/runner/http5xx.d.ts +40 -0
  74. package/dist/runner/http5xx.d.ts.map +1 -0
  75. package/dist/runner/http5xx.js +189 -0
  76. package/dist/runner/http5xx.js.map +1 -0
  77. package/dist/runner/index.d.ts +88 -0
  78. package/dist/runner/index.d.ts.map +1 -0
  79. package/dist/runner/index.js +600 -0
  80. package/dist/runner/index.js.map +1 -0
  81. package/dist/runner/replay.d.ts +42 -0
  82. package/dist/runner/replay.d.ts.map +1 -0
  83. package/dist/runner/replay.js +458 -0
  84. package/dist/runner/replay.js.map +1 -0
  85. package/dist/runner/timeout.d.ts +16 -0
  86. package/dist/runner/timeout.d.ts.map +1 -0
  87. package/dist/runner/timeout.js +38 -0
  88. package/dist/runner/timeout.js.map +1 -0
  89. package/dist/trace/fingerprint.d.ts +78 -0
  90. package/dist/trace/fingerprint.d.ts.map +1 -0
  91. package/dist/trace/fingerprint.js +273 -0
  92. package/dist/trace/fingerprint.js.map +1 -0
  93. package/dist/trace/serialize.d.ts +15 -0
  94. package/dist/trace/serialize.d.ts.map +1 -0
  95. package/dist/trace/serialize.js +86 -0
  96. package/dist/trace/serialize.js.map +1 -0
  97. package/dist/triager/index.d.ts +86 -0
  98. package/dist/triager/index.d.ts.map +1 -0
  99. package/dist/triager/index.js +322 -0
  100. package/dist/triager/index.js.map +1 -0
  101. package/dist/types.d.ts +497 -0
  102. package/dist/types.d.ts.map +1 -0
  103. package/dist/types.js +10 -0
  104. package/dist/types.js.map +1 -0
  105. package/dist/verify/index.d.ts +52 -0
  106. package/dist/verify/index.d.ts.map +1 -0
  107. package/dist/verify/index.js +140 -0
  108. package/dist/verify/index.js.map +1 -0
  109. package/dist/verify/verdict.d.ts +34 -0
  110. package/dist/verify/verdict.d.ts.map +1 -0
  111. package/dist/verify/verdict.js +89 -0
  112. package/dist/verify/verdict.js.map +1 -0
  113. package/package.json +90 -4
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Top-level engine orchestrator.
3
+ *
4
+ * Two ways to use it:
5
+ *
6
+ * 1. `await monkey.exploreAndPlan(url)` — one-shot promise; easiest for the
7
+ * CLI and for unit-testing the explorer/planner.
8
+ * 2. `await monkey.executeAndTriage(url, { events })` — full pipeline;
9
+ * pass an `EventBus` to receive realtime lifecycle events for a SaaS
10
+ * live-activity panel.
11
+ */
12
+ import { type ExploreOptions } from './explorer/index.js';
13
+ import { type PlannerOptions } from './planner/index.js';
14
+ import { type RunnerOptions } from './runner/index.js';
15
+ import { type ReplayOptions } from './runner/replay.js';
16
+ import { type VerifyOptions, type VerifyReport } from './verify/index.js';
17
+ import { EventBus } from './events.js';
18
+ import type { MonkeyLLMConfig, SiteMap, TestPlan, RunResult, MonkeyEvent } from './types.js';
19
+ export interface MonkeyConfig {
20
+ llm: MonkeyLLMConfig;
21
+ /**
22
+ * Fail a step on an uncaught page error, a document HTTP 404, an action
23
+ * HTTP ≥500, or a disabled control the step tried to use. Default **on**.
24
+ * Pass `false` to restore the previous "those signals stay evidence"
25
+ * bug counts. The CLI always passes `true`. A per-call `run.strictOracles`
26
+ * overrides the constructor.
27
+ */
28
+ strictOracles?: boolean;
29
+ /**
30
+ * Fail a passed or skipped step that observed a same-origin HTTP ≥500
31
+ * (document, xhr, or fetch) and triage it as a blocking bug. Default
32
+ * **on**. Pass `false` to leave those responses as evidence. The CLI
33
+ * defaults this on (`--no-fail-on-5xx` opts out). A per-call
34
+ * `run.failOn5xx` or replay `failOn5xx` overrides the constructor.
35
+ *
36
+ * `runPlan`, `replayTraces`, and `triage(run)` still leave this off unless
37
+ * the caller passes it. `Monkey` passes the constructor value through.
38
+ */
39
+ failOn5xx?: boolean;
40
+ }
41
+ /**
42
+ * Product defaults for hard-fault oracles. Both are on unless the host
43
+ * passes `false`. `runPlan` / `triage(run)` do not use this helper; `Monkey` does.
44
+ */
45
+ export declare function resolveMonkeyOracles(config?: Pick<MonkeyConfig, 'strictOracles' | 'failOn5xx'>): {
46
+ strictOracles: boolean;
47
+ failOn5xx: boolean;
48
+ };
49
+ export interface ExploreAndPlanOptions {
50
+ explore?: ExploreOptions;
51
+ plan?: PlannerOptions;
52
+ /** Optional live event bus. Receives `LiveEvent`s for the explorer + planner phases. */
53
+ events?: EventBus;
54
+ }
55
+ export interface ExecuteOptions extends ExploreAndPlanOptions {
56
+ /**
57
+ * Runner config. `screenshotsDir` is required because step screenshots
58
+ * are the most valuable artefact a SaaS report can show.
59
+ */
60
+ run: RunnerOptions;
61
+ }
62
+ /**
63
+ * Options for the `runFromPlan` re-run path. Skips explore + plan, executes
64
+ * an existing TestPlan and triages the result. This is the cheap, fast path
65
+ * for verifying that previously-found bugs are now fixed.
66
+ */
67
+ export interface ExecuteFromPlanOptions {
68
+ /** Required runner config; same shape as `executeAndTriage.run`. */
69
+ run: RunnerOptions;
70
+ /** Optional live event bus. Receives runner + triager events only. */
71
+ events?: EventBus;
72
+ }
73
+ export declare class Monkey {
74
+ private readonly explorerLLM;
75
+ private readonly plannerLLM;
76
+ private readonly runnerLLM;
77
+ /** Optional polish LLM; when present, the triager rewrites bug copy. */
78
+ private readonly triagerLLM?;
79
+ /** Page error, document 404, action 5xx, disabled control. On unless opted out. */
80
+ private readonly strictOracles;
81
+ /** Same-origin HTTP ≥500 promotion. On unless opted out. */
82
+ private readonly failOn5xx;
83
+ constructor(config: MonkeyConfig);
84
+ /**
85
+ * One-shot helper: crawl the site and produce the test plan.
86
+ */
87
+ exploreAndPlan(startUrl: string, opts?: ExploreAndPlanOptions): Promise<{
88
+ siteMap: SiteMap;
89
+ plan: TestPlan;
90
+ }>;
91
+ /**
92
+ * Full pipeline: explore → plan → run → triage.
93
+ *
94
+ * Resilient by design: if the explorer reaches no pages, or the planner
95
+ * fails, we still resolve with whatever evidence we have so the report
96
+ * can render the failure clearly instead of showing an empty page.
97
+ */
98
+ executeAndTriage(startUrl: string, opts: ExecuteOptions): Promise<{
99
+ siteMap: SiteMap;
100
+ plan: TestPlan;
101
+ run: RunResult;
102
+ }>;
103
+ /**
104
+ * Re-run a previously-stored `TestPlan` against the same (or a new) URL,
105
+ * skipping the explorer and planner phases entirely.
106
+ *
107
+ * This re-executes the stored plan. The runner still asks the LLM which
108
+ * tool to use at each step, so it is not a byte-for-byte replay.
109
+ *
110
+ * For a replay that does not call the LLM, use `replayRecorded` (the CLI
111
+ * `rerun` command) or `verifyRecorded` (`monkeytest verify`). Those walk
112
+ * the action trace captured on the original run.
113
+ *
114
+ * Re-using the stored plan still gives:
115
+ *
116
+ * - **Same flows** — no explorer or planner drift between runs.
117
+ * - **Speed + cost** — no explorer LLM calls and no planner call.
118
+ * - **Comparability** — pair the result with `diffRuns(prev, current)`
119
+ * to see new / fixed / regressed bugs by stable fingerprint.
120
+ *
121
+ * The plan's `startUrl` is honoured by default. If you want to re-run the
122
+ * same plan against a different environment (e.g. staging vs prod), edit
123
+ * `plan.flows[].startUrl` before passing it in.
124
+ */
125
+ runFromPlan(testPlan: TestPlan, opts: ExecuteFromPlanOptions): Promise<{
126
+ plan: TestPlan;
127
+ run: RunResult;
128
+ }>;
129
+ /**
130
+ * Replay traces captured on a previous run. Does not call the LLM unless
131
+ * `opts.heal` is set, in which case a missed locator is re-resolved with
132
+ * the runner model and that step is marked `healed`.
133
+ */
134
+ replayRecorded(source: RunResult, opts: ReplayOptions & {
135
+ heal?: boolean;
136
+ }): Promise<{
137
+ run: RunResult;
138
+ }>;
139
+ /**
140
+ * Replay each bug trace on `source` and return a fixed / still_broken /
141
+ * flaky / healed / blocked verdict. No LLM unless `opts.heal` is set.
142
+ */
143
+ verifyRecorded(source: RunResult, opts: VerifyOptions): Promise<VerifyReport>;
144
+ /**
145
+ * Async-iterator API. Same pipeline, but yields MonkeyEvents as it goes
146
+ * so the caller can stream them. v0.2 yields explore + plan + flow/step
147
+ * events.
148
+ */
149
+ run(startUrl: string, opts?: ExploreAndPlanOptions): AsyncGenerator<MonkeyEvent, void, void>;
150
+ }
151
+ //# sourceMappingURL=monkey.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"monkey.d.ts","sourceRoot":"","sources":["../src/monkey.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAGH,OAAO,EAAW,KAAK,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACnE,OAAO,EAA0B,KAAK,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACjF,OAAO,EAAW,KAAK,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAChE,OAAO,EAAgB,KAAK,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEtE,OAAO,EAAc,KAAK,aAAa,EAAE,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtF,OAAO,EAAE,QAAQ,E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G,cAAc,CAClB,QAAQ,EAAE,MAAM,EAChB,IAAI,GAAE,qBAA0B,GAC/B,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,IAAI,EAAE,QAAQ,CAAA;KAAE,CAAC;IAUhD;;;;;;OAMG;IACG,gBAAgB,CACpB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,cAAc,GACnB,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,IAAI,EAAE,QAAQ,CAAC;QAAC,GAAG,EAAE,SAAS,CAAA;KAAE,CAAC;IAgGhE;;;;;;;;;;;;;;;;;;;;;OAqBG;IACG,WAAW,CACf,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,sBAAsB,GAC3B,OAAO,CAAC;QAAE,IAAI,EAAE,QAAQ,CAAC;QAAC,GAAG,EAAE,SAAS,CAAA;KAAE,CAAC;IA+D9C;;;;OAIG;IACG,cAAc,CAClB,MAAM,EAAE,SAAS,EACjB,IAAI,EAAE,aAAa,GAAG;QAAE,IAAI,CAAC,EAAE,OAAO,CAAA;KAAE,GACvC,OAAO,CAAC;QAAE,GAAG,EAAE,SAAS,CAAA;KAAE,CAAC;IAkB9B;;;OAGG;IACG,cAAc,CAAC,MAAM,EAAE,SAAS,EAAE,IAAI,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC;IASnF;;;;OAIG;IACI,GAAG,CACR,QAAQ,EAAE,MAAM,EAChB,IAAI,GAAE,qBAA0B,GAC/B,cAAc,CAAC,WAAW,EAAE,IAAI,EAAE,IAAI,CAAC;CAmC3C"}
package/dist/monkey.js ADDED
@@ -0,0 +1,323 @@
1
+ /**
2
+ * Top-level engine orchestrator.
3
+ *
4
+ * Two ways to use it:
5
+ *
6
+ * 1. `await monkey.exploreAndPlan(url)` — one-shot promise; easiest for the
7
+ * CLI and for unit-testing the explorer/planner.
8
+ * 2. `await monkey.executeAndTriage(url, { events })` — full pipeline;
9
+ * pass an `EventBus` to receive realtime lifecycle events for a SaaS
10
+ * live-activity panel.
11
+ */
12
+ import { createLLM } from './llm/index.js';
13
+ import { explore } from './explorer/index.js';
14
+ import { groundedTestPlan, plan } from './planner/index.js';
15
+ import { runPlan } from './runner/index.js';
16
+ import { replayTraces } from './runner/replay.js';
17
+ import { triage, triagePolished } from './triager/index.js';
18
+ import { verifyBugs } from './verify/index.js';
19
+ import { noopBus } from './events.js';
20
+ /**
21
+ * Product defaults for hard-fault oracles. Both are on unless the host
22
+ * passes `false`. `runPlan` / `triage(run)` do not use this helper; `Monkey` does.
23
+ */
24
+ export function resolveMonkeyOracles(config = {}) {
25
+ return {
26
+ strictOracles: config.strictOracles !== false,
27
+ failOn5xx: config.failOn5xx !== false,
28
+ };
29
+ }
30
+ export class Monkey {
31
+ explorerLLM;
32
+ plannerLLM;
33
+ runnerLLM;
34
+ /** Optional polish LLM; when present, the triager rewrites bug copy. */
35
+ triagerLLM;
36
+ /** Page error, document 404, action 5xx, disabled control. On unless opted out. */
37
+ strictOracles;
38
+ /** Same-origin HTTP ≥500 promotion. On unless opted out. */
39
+ failOn5xx;
40
+ constructor(config) {
41
+ this.explorerLLM = createLLM(config.llm.explorer);
42
+ this.plannerLLM = createLLM(config.llm.planner);
43
+ // Runner picks its own LLM if provided; otherwise it reuses the planner.
44
+ // Most teams will want a *cheaper* runner (per-step tool resolution is
45
+ // hot-path) and a smarter planner (one-shot, high-stakes design call).
46
+ this.runnerLLM = config.llm.runner
47
+ ? createLLM(config.llm.runner)
48
+ : this.plannerLLM;
49
+ // Triager polish is fully opt-in. When unset, the deterministic
50
+ // rule-based triager runs alone (zero LLM cost).
51
+ this.triagerLLM = config.llm.triager ? createLLM(config.llm.triager) : undefined;
52
+ const oracles = resolveMonkeyOracles(config);
53
+ this.strictOracles = oracles.strictOracles;
54
+ this.failOn5xx = oracles.failOn5xx;
55
+ }
56
+ /**
57
+ * One-shot helper: crawl the site and produce the test plan.
58
+ */
59
+ async exploreAndPlan(startUrl, opts = {}) {
60
+ const events = opts.events ?? noopBus();
61
+ const siteMap = await explore(startUrl, this.explorerLLM, { ...opts.explore, events });
62
+ const testPlan = await plan(siteMap, this.plannerLLM, { ...opts.plan, events });
63
+ return { siteMap, plan: testPlan };
64
+ }
65
+ /**
66
+ * Full pipeline: explore → plan → run → triage.
67
+ *
68
+ * Resilient by design: if the explorer reaches no pages, or the planner
69
+ * fails, we still resolve with whatever evidence we have so the report
70
+ * can render the failure clearly instead of showing an empty page.
71
+ */
72
+ async executeAndTriage(startUrl, opts) {
73
+ const events = opts.events ?? noopBus();
74
+ const siteMap = await explore(startUrl, this.explorerLLM, { ...opts.explore, events });
75
+ let testPlan;
76
+ try {
77
+ testPlan = await plan(siteMap, this.plannerLLM, { ...opts.plan, events });
78
+ }
79
+ catch (err) {
80
+ const msg = err instanceof Error ? err.message : String(err);
81
+ events.emit({
82
+ phase: 'planner', type: 'engine.error',
83
+ message: `Planner failed: ${truncate(msg, 160)}`,
84
+ data: { error: msg },
85
+ });
86
+ const fallback = groundedTestPlan(siteMap, opts.plan?.maxFlows ?? 6, `Planner failed after exploration: ${msg}`);
87
+ if (fallback.flows.length === 0) {
88
+ return { siteMap, plan: fallback, run: emptyRun(startUrl) };
89
+ }
90
+ testPlan = fallback;
91
+ }
92
+ if (testPlan.flows.length === 0) {
93
+ events.emit({
94
+ phase: 'planner', type: 'plan.skipped',
95
+ message: 'Planner produced no flows — nothing to execute.',
96
+ });
97
+ return { siteMap, plan: testPlan, run: emptyRun(startUrl) };
98
+ }
99
+ let rawRun;
100
+ try {
101
+ events.emit({
102
+ phase: 'runner', type: 'run.start',
103
+ message: `Running ${testPlan.flows.length} flow${testPlan.flows.length === 1 ? '' : 's'}…`,
104
+ data: { flows: testPlan.flows.length },
105
+ });
106
+ rawRun = await runPlan(testPlan, this.runnerLLM, {
107
+ ...opts.run,
108
+ events,
109
+ strictOracles: opts.run.strictOracles ?? this.strictOracles,
110
+ failOn5xx: opts.run.failOn5xx ?? this.failOn5xx,
111
+ });
112
+ events.emit({
113
+ phase: 'runner', type: 'run.complete',
114
+ message: `Run complete: ${rawRun.stats.flowsPassed}/${rawRun.stats.flowsTotal} flows passed.`,
115
+ data: rawRun.stats,
116
+ });
117
+ }
118
+ catch (err) {
119
+ const msg = err instanceof Error ? err.message : String(err);
120
+ events.emit({
121
+ phase: 'runner', type: 'engine.error',
122
+ message: `Runner failed: ${truncate(msg, 160)}`,
123
+ data: { error: msg },
124
+ });
125
+ return {
126
+ siteMap,
127
+ plan: {
128
+ ...testPlan,
129
+ notes: [testPlan.notes, `Runner failed before executing flows: ${msg}`].filter(Boolean).join('\n'),
130
+ },
131
+ run: emptyRun(startUrl),
132
+ };
133
+ }
134
+ let run;
135
+ if (this.triagerLLM) {
136
+ // Polished path: rule-based triage runs first inside `triagePolished`,
137
+ // then each bug is rewritten by the LLM. Failures fall back to the
138
+ // deterministic copy per-bug — never lose results.
139
+ run = await triagePolished(rawRun, {
140
+ llm: this.triagerLLM,
141
+ events,
142
+ failOn5xx: opts.run.failOn5xx ?? this.failOn5xx,
143
+ });
144
+ }
145
+ else {
146
+ events.emit({ phase: 'triager', type: 'triage.start', message: 'Triaging findings…' });
147
+ run = triage(rawRun, { failOn5xx: opts.run.failOn5xx ?? this.failOn5xx });
148
+ events.emit({
149
+ phase: 'triager', type: 'triage.complete',
150
+ message: run.bugs.length === 0
151
+ ? 'Triage complete: no confirmed bugs.'
152
+ : `Triage complete: ${run.bugs.length} bug${run.bugs.length === 1 ? '' : 's'} reported.`,
153
+ data: { bugs: run.bugs.length, stats: run.stats },
154
+ });
155
+ }
156
+ return { siteMap, plan: testPlan, run };
157
+ }
158
+ /**
159
+ * Re-run a previously-stored `TestPlan` against the same (or a new) URL,
160
+ * skipping the explorer and planner phases entirely.
161
+ *
162
+ * This re-executes the stored plan. The runner still asks the LLM which
163
+ * tool to use at each step, so it is not a byte-for-byte replay.
164
+ *
165
+ * For a replay that does not call the LLM, use `replayRecorded` (the CLI
166
+ * `rerun` command) or `verifyRecorded` (`monkeytest verify`). Those walk
167
+ * the action trace captured on the original run.
168
+ *
169
+ * Re-using the stored plan still gives:
170
+ *
171
+ * - **Same flows** — no explorer or planner drift between runs.
172
+ * - **Speed + cost** — no explorer LLM calls and no planner call.
173
+ * - **Comparability** — pair the result with `diffRuns(prev, current)`
174
+ * to see new / fixed / regressed bugs by stable fingerprint.
175
+ *
176
+ * The plan's `startUrl` is honoured by default. If you want to re-run the
177
+ * same plan against a different environment (e.g. staging vs prod), edit
178
+ * `plan.flows[].startUrl` before passing it in.
179
+ */
180
+ async runFromPlan(testPlan, opts) {
181
+ const events = opts.events ?? noopBus();
182
+ if (testPlan.flows.length === 0) {
183
+ events.emit({
184
+ phase: 'planner', type: 'plan.skipped',
185
+ message: 'runFromPlan called with an empty plan — nothing to execute.',
186
+ });
187
+ return { plan: testPlan, run: emptyRun(testPlan.startUrl) };
188
+ }
189
+ let rawRun;
190
+ try {
191
+ events.emit({
192
+ phase: 'runner', type: 'run.start',
193
+ message: `Re-running ${testPlan.flows.length} stored flow${testPlan.flows.length === 1 ? '' : 's'}…`,
194
+ data: { flows: testPlan.flows.length, mode: 'rerun' },
195
+ });
196
+ rawRun = await runPlan(testPlan, this.runnerLLM, {
197
+ ...opts.run,
198
+ events,
199
+ strictOracles: opts.run.strictOracles ?? this.strictOracles,
200
+ failOn5xx: opts.run.failOn5xx ?? this.failOn5xx,
201
+ });
202
+ events.emit({
203
+ phase: 'runner', type: 'run.complete',
204
+ message: `Re-run complete: ${rawRun.stats.flowsPassed}/${rawRun.stats.flowsTotal} flows passed.`,
205
+ data: rawRun.stats,
206
+ });
207
+ }
208
+ catch (err) {
209
+ const msg = err instanceof Error ? err.message : String(err);
210
+ events.emit({
211
+ phase: 'runner', type: 'engine.error',
212
+ message: `Re-run failed: ${truncate(msg, 160)}`,
213
+ data: { error: msg },
214
+ });
215
+ return { plan: testPlan, run: emptyRun(testPlan.startUrl) };
216
+ }
217
+ let run;
218
+ if (this.triagerLLM) {
219
+ run = await triagePolished(rawRun, {
220
+ llm: this.triagerLLM,
221
+ events,
222
+ failOn5xx: opts.run.failOn5xx ?? this.failOn5xx,
223
+ });
224
+ }
225
+ else {
226
+ events.emit({ phase: 'triager', type: 'triage.start', message: 'Triaging findings…' });
227
+ run = triage(rawRun, { failOn5xx: opts.run.failOn5xx ?? this.failOn5xx });
228
+ events.emit({
229
+ phase: 'triager', type: 'triage.complete',
230
+ message: run.bugs.length === 0
231
+ ? 'Triage complete: no confirmed bugs.'
232
+ : `Triage complete: ${run.bugs.length} bug${run.bugs.length === 1 ? '' : 's'} reported.`,
233
+ data: { bugs: run.bugs.length, stats: run.stats },
234
+ });
235
+ }
236
+ return { plan: testPlan, run };
237
+ }
238
+ /**
239
+ * Replay traces captured on a previous run. Does not call the LLM unless
240
+ * `opts.heal` is set, in which case a missed locator is re-resolved with
241
+ * the runner model and that step is marked `healed`.
242
+ */
243
+ async replayRecorded(source, opts) {
244
+ const traces = source.flows
245
+ .map(flow => flow.trace)
246
+ .filter((trace) => !!trace && trace.steps.length > 0);
247
+ const raw = await replayTraces(traces, {
248
+ ...opts,
249
+ heal: opts.heal,
250
+ llm: opts.heal ? (opts.llm ?? this.runnerLLM) : opts.llm,
251
+ strictOracles: opts.strictOracles ?? this.strictOracles,
252
+ failOn5xx: opts.failOn5xx ?? this.failOn5xx,
253
+ });
254
+ const failOn5xx = opts.failOn5xx ?? this.failOn5xx;
255
+ const run = this.triagerLLM
256
+ ? await triagePolished(raw, { llm: this.triagerLLM, failOn5xx })
257
+ : triage(raw, { failOn5xx });
258
+ return { run };
259
+ }
260
+ /**
261
+ * Replay each bug trace on `source` and return a fixed / still_broken /
262
+ * flaky / healed / blocked verdict. No LLM unless `opts.heal` is set.
263
+ */
264
+ async verifyRecorded(source, opts) {
265
+ return verifyBugs(source, {
266
+ ...opts,
267
+ llm: opts.heal ? (opts.llm ?? this.runnerLLM) : opts.llm,
268
+ strictOracles: opts.strictOracles ?? this.strictOracles,
269
+ failOn5xx: opts.failOn5xx ?? this.failOn5xx,
270
+ });
271
+ }
272
+ /**
273
+ * Async-iterator API. Same pipeline, but yields MonkeyEvents as it goes
274
+ * so the caller can stream them. v0.2 yields explore + plan + flow/step
275
+ * events.
276
+ */
277
+ async *run(startUrl, opts = {}) {
278
+ yield { type: 'explore.start', url: startUrl };
279
+ const pageEvents = [];
280
+ const siteMap = await explore(startUrl, this.explorerLLM, {
281
+ ...opts.explore,
282
+ onPage: (page) => {
283
+ pageEvents.push({ type: 'explore.page', page });
284
+ },
285
+ });
286
+ for (const e of pageEvents)
287
+ yield e;
288
+ yield { type: 'explore.complete', siteMap };
289
+ yield { type: 'plan.start' };
290
+ let testPlan;
291
+ try {
292
+ testPlan = await plan(siteMap, this.plannerLLM, opts.plan);
293
+ }
294
+ catch (err) {
295
+ const msg = err instanceof Error ? err.message : String(err);
296
+ const fallback = groundedTestPlan(siteMap, opts.plan?.maxFlows ?? 6, `Planner failed after exploration: ${msg}`);
297
+ yield { type: 'error', phase: 'planner', message: msg };
298
+ if (fallback.flows.length === 0)
299
+ return;
300
+ testPlan = fallback;
301
+ }
302
+ yield { type: 'plan.complete', plan: testPlan };
303
+ }
304
+ }
305
+ function emptyRun(startUrl) {
306
+ const startedAt = new Date().toISOString();
307
+ return {
308
+ startUrl,
309
+ startedAt,
310
+ finishedAt: startedAt,
311
+ flows: [],
312
+ bugs: [],
313
+ stats: {
314
+ flowsTotal: 0, flowsPassed: 0, flowsFailed: 0, flowsBlocked: 0,
315
+ stepsTotal: 0, stepsPassed: 0, stepsFailed: 0,
316
+ bugsCritical: 0, bugsMajor: 0, bugsMinor: 0,
317
+ },
318
+ };
319
+ }
320
+ function truncate(s, n) {
321
+ return s.length > n ? `${s.slice(0, n - 1)}…` : s;
322
+ }
323
+ //# sourceMappingURL=monkey.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"monkey.js","sourceRoot":"","sources":["../src/monkey.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE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,EAAE,CAAC;YAC9D,UAAU,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC,EAAE,WAAW,EAAE,CAAC;YAC7C,YAAY,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC;SAC5C;KACF,CAAC;AACJ,CAAC;AAED,SAAS,QAAQ,CAAC,CAAS,EAAE,CAAS;IACpC,OAAO,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;AACpD,CAAC"}
@@ -0,0 +1,118 @@
1
+ /**
2
+ * On-disk persistence for plans and runs.
3
+ *
4
+ * The CLI and any host that wants to drive the bug-finding loop need a
5
+ * stable, versioned JSON format on disk. Defining the schema here (rather
6
+ * than letting every consumer roll their own) means:
7
+ *
8
+ * - `monkeytest run`, `monkeytest rerun`, `monkeytest diff` all agree on
9
+ * the file layout.
10
+ * - Future schema migrations are explicit (`schema: 1` -> `schema: 2`).
11
+ * - SaaS hosts can serialise/deserialise the engine's outputs with the
12
+ * same helpers.
13
+ *
14
+ * On-disk layout (the `.monkeytest/` convention):
15
+ *
16
+ * .monkeytest/
17
+ * ├── plan.json ← latest plan, tracked-friendly
18
+ * ├── runs/
19
+ * │ ├── <ISO-date>.json ← one stored run per file
20
+ * │ └── …
21
+ * └── shots/ ← screenshots + videos
22
+ * ├── page-*.png
23
+ * ├── flow-*-step-*-*.png
24
+ * └── flow-*.webm
25
+ *
26
+ * Plan files are tiny (a few KB) and well-suited to git. Run files contain
27
+ * absolute paths to evidence under `shots/`; consumers should treat those
28
+ * paths as opaque (or rebase to relative on read).
29
+ *
30
+ * Schema stays at 1. Action traces (`flow.trace`, `bug.trace`),
31
+ * `fingerprintV2`, and `fingerprintParts` are optional, so run files written
32
+ * by older engines still load. `fingerprint` remains the legacy v1 hash.
33
+ * Older bugs have no trace, so `verify` reports them `blocked`.
34
+ */
35
+ import { basename } from 'node:path';
36
+ import type { TestPlan, RunResult } from '../types.js';
37
+ /** Bumped only on backwards-incompatible changes. */
38
+ export declare const PLAN_FILE_SCHEMA = 1;
39
+ export declare const RUN_FILE_SCHEMA = 1;
40
+ export interface StoredPlan {
41
+ /** `plan.<schema>` for forwards-compat sniffing. */
42
+ schema: typeof PLAN_FILE_SCHEMA;
43
+ /** ISO timestamp the plan was generated. */
44
+ createdAt: string;
45
+ /** Engine version that produced this plan, when known. */
46
+ engine?: string;
47
+ /** Free-form label users can attach (`"smoke"`, `"checkout-only"`, …). */
48
+ label?: string;
49
+ /** The plan itself. */
50
+ plan: TestPlan;
51
+ }
52
+ export interface StoredRun {
53
+ schema: typeof RUN_FILE_SCHEMA;
54
+ /** ISO timestamp the run started. */
55
+ createdAt: string;
56
+ /** Engine version. */
57
+ engine?: string;
58
+ /** Stable id for cross-referencing (filename basename, no extension). */
59
+ id: string;
60
+ /** Optional reference to the StoredPlan that produced this run. */
61
+ planRef?: {
62
+ path?: string;
63
+ createdAt?: string;
64
+ };
65
+ /** The run result. */
66
+ run: RunResult;
67
+ }
68
+ /**
69
+ * Write a plan to disk. Returns the absolute path written.
70
+ */
71
+ export declare function savePlan(plan: TestPlan, opts?: {
72
+ dir?: string;
73
+ filename?: string;
74
+ label?: string;
75
+ engine?: string;
76
+ }): string;
77
+ /**
78
+ * Load a plan from disk. Accepts either:
79
+ * - a file path (e.g. `.monkeytest/plan.json`), or
80
+ * - a directory path that contains `plan.json`.
81
+ *
82
+ * Throws on missing file, schema mismatch, or invalid JSON. Forward-compat
83
+ * sniffs the `schema` field so future migrations have a clear upgrade path.
84
+ */
85
+ export declare function loadPlan(pathOrDir: string): StoredPlan;
86
+ /**
87
+ * Write a run result to disk under `<dir>/runs/<id>.json`. Returns the
88
+ * absolute path written.
89
+ *
90
+ * The file id defaults to a sortable ISO-like timestamp so directory
91
+ * listings show runs in chronological order.
92
+ */
93
+ export declare function saveRun(run: RunResult, opts?: {
94
+ dir?: string;
95
+ id?: string;
96
+ planRef?: StoredRun['planRef'];
97
+ engine?: string;
98
+ }): {
99
+ path: string;
100
+ id: string;
101
+ };
102
+ /**
103
+ * Load a stored run from disk. Accepts either:
104
+ * - a full path to a run JSON (`.monkeytest/runs/2026-05-02T08-12-00.json`),
105
+ * - a directory containing `runs/<id>.json` plus an `id`,
106
+ * - or just an id ("latest" returns the most recent file in `.monkeytest/runs/`).
107
+ */
108
+ export declare function loadRun(pathOrIdOrDir: string, opts?: {
109
+ dir?: string;
110
+ }): StoredRun;
111
+ /**
112
+ * List stored runs in a `.monkeytest/runs/` directory, newest-first.
113
+ * Returns just the basenames (without `.json`) so they're easy to feed
114
+ * back into `loadRun` or `monkeytest diff`.
115
+ */
116
+ export declare function listRuns(dir?: string): string[];
117
+ export { basename };
118
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/persistence/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAGH,OAAO,EAAoB,QAAQ,EAAQ,MAAM,WAAW,CAAC;AAC7D,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAEvD,qDAAqD;AACrD,eAAO,MAAM,gBAAgB,IAAI,CAAC;AAClC,eAAO,MAAM,eAAe,IAAK,CAAC;AAElC,MAAM,WAAW,UAAU;IACzB,oDAAoD;IACpD,MAAM,EAAE,OAAO,gBAAgB,CAAC;IAChC,4CAA4C;IAC5C,SAAS,EAAE,MAAM,CAAC;IAClB,0DAA0D;IAC1D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,0EAA0E;IAC1E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,uBAAuB;IACvB,IAAI,EAAE,QAAQ,CAAC;CAChB;AAED,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,OAAO,eAAe,CAAC;IAC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