explorbot 0.1.10 → 0.1.11

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 (84) hide show
  1. package/README.md +27 -1
  2. package/bin/explorbot-cli.ts +27 -18
  3. package/dist/bin/explorbot-cli.js +26 -18
  4. package/dist/package.json +2 -2
  5. package/dist/rules/navigator/output.md +9 -0
  6. package/dist/rules/navigator/verification-actions.md +2 -0
  7. package/dist/src/action-result.js +23 -1
  8. package/dist/src/action.js +46 -38
  9. package/dist/src/ai/bosun.js +11 -1
  10. package/dist/src/ai/conversation.js +39 -0
  11. package/dist/src/ai/historian/codeceptjs.js +109 -0
  12. package/dist/src/ai/historian/experience.js +320 -0
  13. package/dist/src/ai/historian/mixin.js +2 -0
  14. package/dist/src/ai/historian/playwright.js +145 -0
  15. package/dist/src/ai/historian/utils.js +18 -0
  16. package/dist/src/ai/historian.js +19 -405
  17. package/dist/src/ai/navigator.js +82 -29
  18. package/dist/src/ai/pilot.js +232 -13
  19. package/dist/src/ai/planner.js +29 -9
  20. package/dist/src/ai/provider.js +54 -17
  21. package/dist/src/ai/researcher.js +41 -32
  22. package/dist/src/ai/rules.js +26 -14
  23. package/dist/src/ai/tester.js +90 -26
  24. package/dist/src/ai/tools.js +13 -7
  25. package/dist/src/browser-server.js +16 -3
  26. package/dist/src/commands/add-rule-command.js +11 -8
  27. package/dist/src/commands/clean-command.js +2 -1
  28. package/dist/src/commands/explore-command.js +27 -15
  29. package/dist/src/commands/init-command.js +9 -8
  30. package/dist/src/commands/plan-command.js +32 -0
  31. package/dist/src/commands/plan-save-command.js +19 -7
  32. package/dist/src/commands/rerun-command.js +4 -0
  33. package/dist/src/components/App.js +15 -5
  34. package/dist/src/execution-controller.js +13 -2
  35. package/dist/src/experience-tracker.js +20 -64
  36. package/dist/src/explorbot.js +5 -8
  37. package/dist/src/explorer.js +9 -2
  38. package/dist/src/observability.js +50 -99
  39. package/dist/src/playwright-recorder.js +309 -0
  40. package/dist/src/test-plan.js +12 -0
  41. package/dist/src/utils/aria.js +37 -1
  42. package/dist/src/utils/error-page.js +20 -7
  43. package/dist/src/utils/next-steps.js +37 -0
  44. package/package.json +2 -2
  45. package/rules/navigator/output.md +9 -0
  46. package/rules/navigator/verification-actions.md +2 -0
  47. package/src/action-result.ts +26 -1
  48. package/src/action.ts +44 -37
  49. package/src/ai/bosun.ts +11 -1
  50. package/src/ai/conversation.ts +37 -0
  51. package/src/ai/historian/codeceptjs.ts +130 -0
  52. package/src/ai/historian/experience.ts +383 -0
  53. package/src/ai/historian/mixin.ts +4 -0
  54. package/src/ai/historian/playwright.ts +169 -0
  55. package/src/ai/historian/utils.ts +23 -0
  56. package/src/ai/historian.ts +35 -473
  57. package/src/ai/navigator.ts +82 -29
  58. package/src/ai/pilot.ts +237 -14
  59. package/src/ai/planner.ts +29 -9
  60. package/src/ai/provider.ts +51 -17
  61. package/src/ai/researcher.ts +45 -33
  62. package/src/ai/rules.ts +27 -14
  63. package/src/ai/tester.ts +94 -26
  64. package/src/ai/tools.ts +47 -25
  65. package/src/browser-server.ts +17 -3
  66. package/src/commands/add-rule-command.ts +11 -7
  67. package/src/commands/clean-command.ts +2 -1
  68. package/src/commands/explore-command.ts +29 -15
  69. package/src/commands/init-command.ts +9 -8
  70. package/src/commands/plan-command.ts +35 -0
  71. package/src/commands/plan-save-command.ts +18 -7
  72. package/src/commands/rerun-command.ts +5 -0
  73. package/src/components/App.tsx +16 -5
  74. package/src/config.ts +6 -1
  75. package/src/execution-controller.ts +14 -3
  76. package/src/experience-tracker.ts +21 -72
  77. package/src/explorbot.ts +5 -8
  78. package/src/explorer.ts +11 -2
  79. package/src/observability.ts +50 -109
  80. package/src/playwright-recorder.ts +305 -0
  81. package/src/test-plan.ts +12 -0
  82. package/src/utils/aria.ts +38 -1
  83. package/src/utils/error-page.ts +22 -7
  84. package/src/utils/next-steps.ts +51 -0
@@ -6,6 +6,7 @@ import { ConfigParser } from "../config.js";
6
6
  import { renderExperienceToc } from "../experience-tracker.js";
7
7
  import { TestResult } from "../test-plan.js";
8
8
  import { collectInteractiveNodes, detectFocusArea, extractFocusedElement } from "../utils/aria.js";
9
+ import { ErrorPageError } from "../utils/error-page.js";
9
10
  import { createDebug, tag } from "../utils/logger.js";
10
11
  const debugLog = createDebug('explorbot:pilot');
11
12
  import { truncateJson } from "../utils/strings.js";
@@ -42,22 +43,27 @@ export class Pilot {
42
43
  return null;
43
44
  return this.conversation.getLastMessage() || null;
44
45
  }
45
- async reviewStop(task, currentState, testerConversation) {
46
- return this.reviewDecision('stop', task, currentState, testerConversation);
46
+ async reviewStop(task, currentState, testerConversation, navigator) {
47
+ return this.reviewDecision('stop', task, currentState, testerConversation, navigator);
47
48
  }
48
- async reviewFinish(task, currentState, testerConversation) {
49
- return this.reviewDecision('finish', task, currentState, testerConversation);
49
+ async reviewFinish(task, currentState, testerConversation, navigator) {
50
+ return this.reviewDecision('finish', task, currentState, testerConversation, navigator);
50
51
  }
51
- async reviewCompletion(task, currentState, testerConversation) {
52
+ async reviewCompletion(task, currentState, testerConversation, navigator) {
52
53
  const verdictType = task.hasAchievedAny() ? 'finish' : 'stop';
53
- return this.reviewDecision(verdictType, task, currentState, testerConversation);
54
+ return this.reviewDecision(verdictType, task, currentState, testerConversation, navigator);
54
55
  }
55
- async finalReview(task, currentState, testerConversation) {
56
+ async finalReview(task, currentState, testerConversation, navigator) {
56
57
  if (task.hasFinished)
57
58
  return false;
58
- return this.reviewCompletion(task, currentState, testerConversation);
59
+ return this.reviewCompletion(task, currentState, testerConversation, navigator);
59
60
  }
60
- async reviewDecision(type, task, currentState, testerConversation) {
61
+ async reviewReset(task, currentState, reason, testerConversation) {
62
+ return this.reviewResetDecision(task, currentState, reason, testerConversation);
63
+ }
64
+ async reviewDecision(type, task, currentState, testerConversation, navigator) {
65
+ if (task.hasFinished)
66
+ return false;
61
67
  tag('substep').log(`Pilot reviewing ${type} verdict...`);
62
68
  const sessionLog = this.formatSessionLog(testerConversation);
63
69
  const stateContext = this.buildStateContext(currentState);
@@ -79,6 +85,10 @@ export class Pilot {
79
85
  decision: z.enum(['pass', 'fail', 'continue', 'skipped']).describe('pass = test succeeded, fail = test failed, continue = tester should keep going, skipped = scenario is irrelevant OR systematic execution failures prevented testing'),
80
86
  reason: z.string().describe('What happened and why (1-2 sentences). Do NOT repeat the decision status (e.g. "scenario goal achieved/not achieved") — just explain the evidence. For continue: explain why rejected and suggest alternatives.'),
81
87
  guidance: z.string().nullable().describe('Required for "continue": specific actionable instruction for the tester — what exactly to verify, retry differently, or complete next. Be concrete.'),
88
+ requestVerification: z
89
+ .string()
90
+ .nullable()
91
+ .describe('REQUIRED whenever decision is "pass" — provide a specific assertion that proves the scenario goal on the current page (e.g., "New test suite \\"Foo\\" is visible in the suites list"). The system runs it and bakes the resulting assertion into the generated test file; without it the test file has no verifiable expect(). Also use when evidence is insufficient before deciding pass/fail. Leave null for "continue", "fail", or "skipped".'),
82
92
  });
83
93
  const userContent = dedent `
84
94
  Tester wants to ${type} the test.
@@ -106,6 +116,12 @@ export class Pilot {
106
116
  - "continue" if tester hasn't completed the scenario goal yet — even if milestones were checked
107
117
  - If evidence is mixed, but final state indicates goal completion, choose "pass"
108
118
  - If evidence is mixed and final state is unclear, prefer "continue" over "fail"
119
+
120
+ When deciding "pass", you MUST also set requestVerification to a CodeceptJS assertion that
121
+ proves the scenario goal on the current page. Choose the strongest single evidence (a unique
122
+ element/text that exists ONLY because the scenario succeeded). The assertion is executed and
123
+ then converted into the spec file's expect() — without it the generated test has nothing to
124
+ assert and is worthless.
109
125
  `;
110
126
  const messages = [
111
127
  {
@@ -124,6 +140,30 @@ export class Pilot {
124
140
  task.finish(TestResult.FAILED);
125
141
  return false;
126
142
  }
143
+ if (result.requestVerification && navigator) {
144
+ tag('substep').log(`Pilot requesting verification: ${result.requestVerification}`);
145
+ try {
146
+ const verifyResult = await navigator.verifyState(result.requestVerification, currentState);
147
+ if (verifyResult.verified) {
148
+ if (verifyResult.assertionSteps?.length) {
149
+ this.explorer.getPlaywrightRecorder().recordVerification(verifyResult.assertionSteps);
150
+ }
151
+ tag('substep').log(`Pilot verified: ${result.requestVerification}`);
152
+ }
153
+ else {
154
+ tag('substep').log(`Pilot verification failed: ${result.requestVerification}`);
155
+ if (result.decision === 'pass') {
156
+ const flipMessage = `Verification "${result.requestVerification}" did not match the page. Adjust approach and re-verify before finishing.`;
157
+ result.decision = 'continue';
158
+ result.reason = flipMessage;
159
+ result.guidance = result.guidance ?? flipMessage;
160
+ }
161
+ }
162
+ }
163
+ catch (verifyErr) {
164
+ tag('warning').log(`Pilot verification errored: ${verifyErr.message}`);
165
+ }
166
+ }
127
167
  tag('info').log(`Pilot: ${result.decision} — ${result.reason}`);
128
168
  task.summary = result.reason;
129
169
  if (result.decision === 'pass') {
@@ -152,6 +192,131 @@ export class Pilot {
152
192
  return false;
153
193
  }
154
194
  }
195
+ async reviewResetDecision(task, currentState, reason, testerConversation) {
196
+ if (task.hasFinished)
197
+ return false;
198
+ tag('substep').log(`Pilot reviewing reset (count=${task.resetCount})...`);
199
+ const sessionLog = this.formatSessionLog(testerConversation);
200
+ const stateContext = this.buildStateContext(currentState);
201
+ const notes = task.notesToString() || 'No notes recorded.';
202
+ const schema = z.object({
203
+ decision: z.enum(['allow', 'fail', 'continue', 'skipped']).describe('allow = reset proceeds, fail = test failed (stop looping), continue = veto reset, tester should act on current page instead, skipped = scenario is irrelevant or cannot be executed'),
204
+ reason: z.string().describe('What evidence justifies this decision (1-2 sentences). Do not restate the decision.'),
205
+ guidance: z.string().nullable().describe('Required for "continue": concrete instruction for what the tester should do instead of resetting (e.g. which tool to call, what to verify).'),
206
+ });
207
+ const userContent = dedent `
208
+ Tester requested reset. Previous reset count: ${task.resetCount - 1}.
209
+
210
+ Reason given by tester: ${reason || '(none)'}
211
+
212
+ <state>
213
+ ${stateContext}
214
+ </state>
215
+
216
+ ${this.formatExpectations(task)}
217
+
218
+ <notes>
219
+ ${notes}
220
+ </notes>
221
+
222
+ <session_log>
223
+ ${sessionLog || 'No actions recorded'}
224
+ </session_log>
225
+
226
+ Decide:
227
+ - "allow" — the reset is legitimate (navigation dead-end, wrong page, irrecoverable error on current page).
228
+ - "continue" — veto the reset; something on the current page can still be used to progress or verify. Provide guidance.
229
+ - "fail" — reset-looping: tester has already reset and the underlying obstacle will not change. Stop the test as failed.
230
+ - "skipped" — the scenario is inapplicable to this application or cannot be executed here.
231
+ `;
232
+ const messages = [
233
+ {
234
+ role: 'system',
235
+ content: this.buildResetSystemPrompt(task),
236
+ },
237
+ { role: 'user', content: userContent },
238
+ ];
239
+ try {
240
+ const response = await this.provider.generateObject(messages, schema, this.provider.getAgenticModel('pilot'), {
241
+ agentName: 'pilot',
242
+ experimental_telemetry: { functionId: 'pilot.reviewReset' },
243
+ });
244
+ const result = response?.object;
245
+ if (!result) {
246
+ return true;
247
+ }
248
+ tag('info').log(`Pilot reset verdict: ${result.decision} — ${result.reason}`);
249
+ if (result.decision === 'allow') {
250
+ tag('substep').log(`Pilot allowed reset: ${result.reason}`);
251
+ return true;
252
+ }
253
+ if (result.decision === 'fail') {
254
+ task.addNote(`Pilot: reset refused — ${result.reason}`, TestResult.FAILED);
255
+ task.finish(TestResult.FAILED);
256
+ return false;
257
+ }
258
+ if (result.decision === 'skipped') {
259
+ task.addNote(`Pilot: skipped — ${result.reason}`, TestResult.SKIPPED);
260
+ task.finish(TestResult.SKIPPED);
261
+ return false;
262
+ }
263
+ tag('substep').log(`Pilot vetoed reset: ${result.reason}`);
264
+ const guidanceText = result.guidance ? `\n\nWhat to do instead: ${result.guidance}` : '';
265
+ testerConversation.addUserText(`Pilot vetoed reset: ${result.reason}${guidanceText}`);
266
+ return false;
267
+ }
268
+ catch (error) {
269
+ tag('warning').log(`Pilot reset review failed: ${error.message}`);
270
+ return true;
271
+ }
272
+ }
273
+ buildResetSystemPrompt(task) {
274
+ return dedent `
275
+ You are Pilot — the supervisor that decides whether a reset is legitimate.
276
+ Tester wants to reset (navigate back to the start URL and discard progress).
277
+
278
+ SCENARIO: ${task.scenario}
279
+
280
+ Reset is DESTRUCTIVE. It abandons all work done in this iteration. In stateful apps, any
281
+ side effects (records created, forms submitted) persist on the server — resetting does not
282
+ undo them. Unnecessary resets create duplicate data and loop forever.
283
+
284
+ LEGITIMATE RESET (decide "allow"):
285
+ - The current page is unrelated to the scenario and no path leads back.
286
+ - Navigation is stuck in an error state with no recoverable action.
287
+ - The tester arrived on a page that cannot host the scenario at all.
288
+
289
+ ILLEGITIMATE RESET (decide "continue"):
290
+ - The previous action already succeeded (URL changed to a success/detail page, record visible,
291
+ confirmation shown) and tester wants to redo it because an assertion did not match.
292
+ The work is done — verify, record, or finish instead of restarting.
293
+ - A single expectation / milestone does not match app reality but the scenario goal may still
294
+ have been achieved. Do not redo — instruct the tester to verify the actual outcome.
295
+ - Tester wants to "try again with different input" after a form was submitted. Submitting
296
+ again creates a duplicate; guide toward editing the existing record or accepting the state.
297
+
298
+ RESET-LOOP (decide "fail"):
299
+ - resetCount >= 2 and the previous resets did not change the underlying situation.
300
+ - The same flow has been attempted twice with the same failure mode.
301
+ - Repeating the reset cannot produce new information.
302
+
303
+ SCENARIO INAPPLICABLE (decide "skipped"):
304
+ - The feature the scenario targets does not exist on this app, or prerequisites cannot be met.
305
+
306
+ PRIORITY:
307
+ 1) Evidence of successful side effects in session_log (URL transition, new record visible).
308
+ If present, almost never allow the reset — the work is done.
309
+ 2) resetCount. Each prior reset raises the bar for allowing another.
310
+ 3) Tester's stated reason. Weigh it against the observed evidence, do not trust it blindly.
311
+
312
+ GUIDANCE FIELD (required when decision is "continue"):
313
+ Give a specific next action on the current page: which tool to call, what to verify, or how to
314
+ record the outcome. Do not suggest repeating actions that already succeeded.
315
+
316
+ EXPECTED RESULTS (milestones, not the goal):
317
+ ${task.expected.map((e) => `- ${e}`).join('\n')}
318
+ `;
319
+ }
155
320
  buildVerdictSystemPrompt(type, task) {
156
321
  return dedent `
157
322
  You are Pilot — the final decision maker for test pass/fail.
@@ -248,8 +413,12 @@ export class Pilot {
248
413
  the elements needed for the scenario. The page summary does not list every element.
249
414
  Prefer interacting with the current page over navigating away.
250
415
 
416
+ If you load a recipe via learn_experience, do NOT rewrite its code in your plan — the
417
+ raw recipe is forwarded to Tester automatically. Reference it by step ("apply recipe
418
+ steps 1–3, then…") and call out anywhere your scenario diverges from it.
419
+
251
420
  Be concise and specific. Tester will follow your plan.
252
- `, 'pilot.planTest', { tools: true, maxToolRoundtrips: 3, task });
421
+ `, 'pilot.planTest', { tools: true, planningOnly: true, maxToolRoundtrips: 3, task });
253
422
  }
254
423
  async reviewNewPage(task, currentState) {
255
424
  if (!this.conversation)
@@ -329,7 +498,10 @@ export class Pilot {
329
498
  }
330
499
  }
331
500
  this.conversation.addUserText(finalUserText);
332
- let tools = opts.tools ? this.agentTools : undefined;
501
+ let tools;
502
+ if (opts.tools) {
503
+ tools = opts.planningOnly ? this.pickPlanningTools() : this.agentTools;
504
+ }
333
505
  if (opts.tools && opts.task) {
334
506
  tools = { ...tools, ...this.buildPreconditionTool(opts.task) };
335
507
  }
@@ -338,7 +510,20 @@ export class Pilot {
338
510
  agentName: 'pilot',
339
511
  experimental_telemetry: { functionId },
340
512
  });
341
- return result?.response?.text || '';
513
+ const text = result?.response?.text || '';
514
+ const learned = (result?.toolExecutions || []).filter((e) => e.toolName === 'learn_experience' && e.output?.content).map((e) => e.output.content);
515
+ if (learned.length === 0)
516
+ return text;
517
+ return dedent `
518
+ ${text}
519
+
520
+ <applied_experience>
521
+ Recipes from prior successful runs that Pilot judged relevant. Locators worked then; the page may have changed since.
522
+ Treat code blocks below as a starting hypothesis. If a locator misses, fall back to ARIA/UI-map.
523
+
524
+ ${learned.join('\n\n')}
525
+ </applied_experience>
526
+ `;
342
527
  }
343
528
  getExperienceToc() {
344
529
  if (!this.experienceTracker)
@@ -350,6 +535,25 @@ export class Pilot {
350
535
  const toc = this.experienceTracker.getExperienceTableOfContents(actionResult);
351
536
  return renderExperienceToc(toc);
352
537
  }
538
+ pickPlanningTools() {
539
+ const { see, context, verify, research, getVisitedStates, xpathCheck, learn_experience } = this.agentTools ?? {};
540
+ const planning = {};
541
+ if (see)
542
+ planning.see = see;
543
+ if (context)
544
+ planning.context = context;
545
+ if (verify)
546
+ planning.verify = verify;
547
+ if (research)
548
+ planning.research = research;
549
+ if (getVisitedStates)
550
+ planning.getVisitedStates = getVisitedStates;
551
+ if (xpathCheck)
552
+ planning.xpathCheck = xpathCheck;
553
+ if (learn_experience)
554
+ planning.learn_experience = learn_experience;
555
+ return planning;
556
+ }
353
557
  buildPreconditionTool(task) {
354
558
  return {
355
559
  precondition: tool({
@@ -483,7 +687,15 @@ export class Pilot {
483
687
  }
484
688
  }
485
689
  if (text.includes('ATTACH_UI_MAP')) {
486
- const uiMap = await this.researcher.research(currentState);
690
+ let uiMap = '';
691
+ try {
692
+ uiMap = await this.researcher.research(currentState);
693
+ }
694
+ catch (err) {
695
+ if (!(err instanceof ErrorPageError))
696
+ throw err;
697
+ tag('warning').log(`Pilot UI map skipped: ${err.message}`);
698
+ }
487
699
  if (uiMap) {
488
700
  parts.push(dedent `
489
701
  <page_ui_map>
@@ -635,6 +847,13 @@ export class Pilot {
635
847
  - If the goal was achieved by a previous action (SUCCESS in recent_actions with confirming ariaDiff): instruct Tester to verify() the result and finish(). Do NOT repeat the same action.
636
848
  - If Tester keeps re-opening the same panel and re-submitting the same data — STOP. The action was already completed.
637
849
 
850
+ Action-goal alignment — classify every recent successful action:
851
+ - GOAL-ADVANCING: creates, edits, removes, submits, or verifies the scenario's subject data (the object the scenario actually changes).
852
+ - VIEW-ONLY: toggles layout, filters, tabs, segment controls, sort orders, collapse/expand — changes which data is shown without modifying it.
853
+ - A single VIEW-ONLY action is legitimate when needed to reveal a target element for the next GOAL-ADVANCING action.
854
+ - A run of two or more consecutive successful VIEW-ONLY actions with no interleaved GOAL-ADVANCING action is thrashing — Tester is exploring UI instead of executing the scenario. Redirect Tester to the specific mutation or verification the scenario requires.
855
+ - VIEW-ONLY actions also tend to produce large page diffs with many htmlParts; if you see that pattern repeatedly in recent_actions, treat it as evidence of thrashing.
856
+
638
857
  Navigation awareness — always compare current page url to START URL:
639
858
  - subpage navigation (deeper path from START URL) — OK, scenario may need sub-pages
640
859
  - outer-page navigation (parent/sibling path from START URL) — SUSPICIOUS. The scenario target is on the START page. Do NOT rationalize leaving it. Instruct Tester to back() or reset().
@@ -400,6 +400,34 @@ export class Planner extends PlannerBase {
400
400
  const allTests = this.currentPlan.getAllTests();
401
401
  const titleListing = allTests.map((t) => `- "${t.scenario}" [${t.result || 'pending'}]`).join('\n');
402
402
  const compactContext = planToCompactAiContext(this.currentPlan);
403
+ let planningStrategy;
404
+ if (feature) {
405
+ planningStrategy = dedent `
406
+ <planning_strategy>
407
+ Stay strictly inside the "${feature}" feature area. Do NOT switch to a different, unrelated feature even if it has no coverage.
408
+ Propose ${this.MIN_TASKS}-${this.MAX_TASKS} additional scenarios for "${feature}" that are not already in the tested list.
409
+ Use the <approach> above to decide which new angles to explore — different controls, inputs, states, outcome categories, or combinations — all within "${feature}".
410
+ Return an empty scenarios array only when no genuinely new scenario for "${feature}" remains.
411
+ </planning_strategy>
412
+ `;
413
+ }
414
+ else {
415
+ let extendedResearchHint = '';
416
+ if (mdq(plannerResearch).query('section("Extended Research")').count() > 0) {
417
+ extendedResearchHint = 'IMPORTANT: The research contains "Extended Research" sections with dropdowns, modals, and panels. Prioritize testing features from Extended Research that have no coverage yet.';
418
+ }
419
+ planningStrategy = dedent `
420
+ <planning_strategy>
421
+ Find a feature area in the research that has NO or minimal test coverage.
422
+ Pick that ONE feature and propose ${this.MIN_TASKS}-${this.MAX_TASKS} tests for it.
423
+ ${extendedResearchHint}
424
+
425
+ Follow the <approach> described above when proposing tests for this feature.
426
+
427
+ If ALL features across ALL research sections are covered, return empty scenarios array.
428
+ </planning_strategy>
429
+ `;
430
+ }
403
431
  conversation.addUserText(dedent `
404
432
  CRITICAL: This plan already has tests.
405
433
 
@@ -419,15 +447,7 @@ export class Planner extends PlannerBase {
419
447
  ${compactContext}
420
448
  </tested_scenarios>
421
449
 
422
- <planning_strategy>
423
- Find a feature area in the research that has NO or minimal test coverage.
424
- Pick that ONE feature and propose ${this.MIN_TASKS}-${this.MAX_TASKS} tests for it.
425
- ${mdq(plannerResearch).query('section("Extended Research")').count() > 0 ? 'IMPORTANT: The research contains "Extended Research" sections with dropdowns, modals, and panels. Prioritize testing features from Extended Research that have no coverage yet.' : ''}
426
-
427
- Follow the <approach> described above when proposing tests for this feature.
428
-
429
- If ALL features across ALL research sections are covered, return empty scenarios array.
430
- </planning_strategy>
450
+ ${planningStrategy}
431
451
 
432
452
  <context_from_previous_tests>
433
453
  During testing, the following pages were visited:
@@ -1,6 +1,6 @@
1
1
  import { LangfuseSpanProcessor } from '@langfuse/otel';
2
2
  import { NodeSDK } from '@opentelemetry/sdk-node';
3
- import { generateObject, generateText } from 'ai';
3
+ import { generateObject, generateText, stepCountIs } from 'ai';
4
4
  import { clearActivity, setActivity } from "../activity.js";
5
5
  import { executionController } from "../execution-controller.js";
6
6
  import { Observability } from "../observability.js";
@@ -16,6 +16,20 @@ class AiError extends Error {
16
16
  }
17
17
  export class ContextLengthError extends Error {
18
18
  }
19
+ function rejectAfterIdle(ms, signal) {
20
+ return new Promise((_, reject) => {
21
+ const tick = () => {
22
+ if (signal.cancelled)
23
+ return;
24
+ if (executionController.isAwaitingInput()) {
25
+ setTimeout(tick, ms);
26
+ return;
27
+ }
28
+ reject(new Error('AI request timeout'));
29
+ };
30
+ setTimeout(tick, ms);
31
+ });
32
+ }
19
33
  export class Provider {
20
34
  config;
21
35
  telemetryEnabled = false;
@@ -247,13 +261,19 @@ export class Provider {
247
261
  promptLog('Available tools:', toolNames);
248
262
  promptLog(messages[messages.length - 1].content);
249
263
  const telemetry = this.getTelemetry(options);
264
+ const maxRoundtrips = options.maxToolRoundtrips ?? 5;
265
+ const extraStop = options.stopWhen;
266
+ const stopConditions = [stepCountIs(maxRoundtrips)];
267
+ if (extraStop)
268
+ stopConditions.push(extraStop);
269
+ const { stopWhen: _ignoredStopWhen, ...optionsWithoutStop } = options;
250
270
  const config = this.mergeProviderOptions({
251
271
  tools,
252
272
  maxTokens: 16384,
253
- maxToolRoundtrips: options.maxToolRoundtrips ?? 5,
254
273
  toolChoice: 'auto',
255
274
  ...(this.config.config || {}),
256
- ...options,
275
+ ...optionsWithoutStop,
276
+ stopWhen: stopConditions,
257
277
  ...(telemetry ? { experimental_telemetry: telemetry } : {}),
258
278
  model,
259
279
  abortSignal: executionController.getAbortSignal(),
@@ -261,13 +281,24 @@ export class Provider {
261
281
  try {
262
282
  const response = await withRetry(async () => {
263
283
  const timeout = config.timeout || 30000;
264
- return (await Promise.race([
265
- generateText({
266
- messages,
267
- ...config,
268
- }),
269
- new Promise((_, reject) => setTimeout(() => reject(new Error('AI request timeout')), timeout)),
270
- ]));
284
+ const cancel = { cancelled: false };
285
+ try {
286
+ const result = (await Promise.race([
287
+ generateText({
288
+ messages,
289
+ ...config,
290
+ }),
291
+ rejectAfterIdle(timeout, cancel),
292
+ ]));
293
+ const hasToolCall = (result.toolCalls?.length || 0) > 0;
294
+ if (!result.text && !hasToolCall && result.finishReason === 'length') {
295
+ throw new ContextLengthError('AI response empty: output truncated at maxTokens. Increase maxTokens in config or use a model with higher output capacity.');
296
+ }
297
+ return result;
298
+ }
299
+ finally {
300
+ cancel.cancelled = true;
301
+ }
271
302
  }, this.getRetryOptions(options));
272
303
  clearActivity();
273
304
  // Log tool usage summary
@@ -330,13 +361,19 @@ export class Provider {
330
361
  promptLog(messages[messages.length - 1].content);
331
362
  const response = await withRetry(async () => {
332
363
  const timeout = config.timeout || 30000;
333
- return (await Promise.race([
334
- generateObject({
335
- messages,
336
- ...config,
337
- }),
338
- new Promise((_, reject) => setTimeout(() => reject(new Error('AI request timeout')), timeout)),
339
- ]));
364
+ const cancel = { cancelled: false };
365
+ try {
366
+ return (await Promise.race([
367
+ generateObject({
368
+ messages,
369
+ ...config,
370
+ }),
371
+ rejectAfterIdle(timeout, cancel),
372
+ ]));
373
+ }
374
+ finally {
375
+ cancel.cancelled = true;
376
+ }
340
377
  }, this.getRetryOptions(options));
341
378
  clearActivity();
342
379
  responseLog(response.object);
@@ -6,12 +6,11 @@ import { executionController } from "../execution-controller.js";
6
6
  import { Observability } from "../observability.js";
7
7
  import { Stats } from "../stats.js";
8
8
  import { diffAriaSnapshots } from "../utils/aria.js";
9
- import { ErrorPageError, isErrorPage } from "../utils/error-page.js";
9
+ import { ErrorPageError, detectPageCondition } from "../utils/error-page.js";
10
10
  import { HooksRunner } from "../utils/hooks-runner.js";
11
11
  import { isBodyEmpty } from "../utils/html.js";
12
12
  import { createDebug, pluralize, tag } from '../utils/logger.js';
13
13
  import { mdq } from "../utils/markdown-query.js";
14
- import { withRetry } from "../utils/retry.js";
15
14
  import { RulesLoader } from "../utils/rules-loader.js";
16
15
  import { ContextLengthError } from './provider.js';
17
16
  import { findSimilarResearch, getCachedResearch, saveResearch } from "./researcher/cache.js";
@@ -98,11 +97,15 @@ export class Researcher extends ResearcherBase {
98
97
  const annotatedElements = await this.explorer.annotateElements();
99
98
  debugLog(`Annotated ${annotatedElements.length} interactive elements with eidx`);
100
99
  this.actionResult = await this.explorer.createAction().capturePageState({ includeScreenshot: screenshot && this.provider.hasVision() });
101
- if (isErrorPage(this.actionResult)) {
102
- const recovered = await this.waitForPageLoad(screenshot);
103
- if (!recovered) {
104
- tag('warning').log(`Detected error page at ${state.url}`);
105
- throw new ErrorPageError(state.url, this.actionResult.title);
100
+ const condition = detectPageCondition(this.actionResult);
101
+ if (condition === 'error') {
102
+ tag('warning').log(`Detected error page at ${state.url}`);
103
+ throw new ErrorPageError(state.url, this.actionResult.title);
104
+ }
105
+ if (condition === 'loading') {
106
+ const settled = await this.waitUntilSettled(screenshot);
107
+ if (!settled) {
108
+ tag('warning').log(`Page at ${state.url} did not finish loading within timeout, continuing with best-effort research`);
106
109
  }
107
110
  }
108
111
  debugLog('Researching web page:', this.actionResult.url);
@@ -285,41 +288,47 @@ export class Researcher extends ResearcherBase {
285
288
  }
286
289
  return;
287
290
  }
288
- if (isEmpty) {
289
- debugLog('HTML body is empty, refreshing page');
290
- tag('step').log('Page body is empty, refreshing...');
291
- }
292
- else {
293
- debugLog('Not on current state, navigating to URL');
294
- tag('step').log('Navigating to URL...');
291
+ if (isEmpty && isOnCurrentState) {
292
+ debugLog('HTML body empty on current URL, waiting for content');
293
+ tag('step').log('Page body is empty, waiting for content...');
294
+ await this.waitUntilSettled(screenshot ?? false);
295
+ return;
295
296
  }
297
+ debugLog('Not on current state, navigating to URL');
298
+ tag('step').log('Navigating to URL...');
296
299
  await this.explorer.visit(url);
297
300
  this.actionResult = await this.explorer.createAction().capturePageState({ includeScreenshot: screenshot ?? false });
298
301
  }
299
- async waitForPageLoad(screenshot) {
302
+ async waitUntilSettled(screenshot) {
300
303
  const errorPageTimeout = this.explorer.getConfig().ai?.agents?.researcher?.errorPageTimeout ?? 10;
301
304
  if (errorPageTimeout <= 0)
302
305
  return false;
306
+ const page = this.explorer.playwrightHelper.page;
307
+ const includeScreenshot = screenshot && this.provider.hasVision();
303
308
  try {
304
- await withRetry(async () => {
305
- await this.explorer.annotateElements();
306
- this.actionResult = await this.explorer.createAction().capturePageState({
307
- includeScreenshot: screenshot && this.provider.hasVision(),
308
- });
309
- if (isErrorPage(this.actionResult))
310
- throw new Error('Error page detected');
311
- }, {
312
- maxAttempts: Math.ceil(errorPageTimeout / 3) + 1,
313
- baseDelay: 1000,
314
- maxDelay: 5000,
315
- backoffMultiplier: 2,
316
- retryCondition: (e) => e.message === 'Error page detected',
317
- });
318
- return true;
309
+ await page?.waitForLoadState('networkidle', { timeout: errorPageTimeout * 1000 });
319
310
  }
320
- catch {
321
- return false;
311
+ catch { }
312
+ await this.explorer.annotateElements();
313
+ this.actionResult = await this.explorer.createAction().capturePageState({ includeScreenshot });
314
+ let condition = detectPageCondition(this.actionResult);
315
+ if (condition === 'error') {
316
+ throw new ErrorPageError(this.actionResult.url, this.actionResult.title);
317
+ }
318
+ if (condition === 'ok')
319
+ return true;
320
+ for (let i = 0; i < 3; i++) {
321
+ await new Promise((r) => setTimeout(r, 1000));
322
+ await this.explorer.annotateElements();
323
+ this.actionResult = await this.explorer.createAction().capturePageState({ includeScreenshot });
324
+ condition = detectPageCondition(this.actionResult);
325
+ if (condition === 'error') {
326
+ throw new ErrorPageError(this.actionResult.url, this.actionResult.title);
327
+ }
328
+ if (condition === 'ok')
329
+ return true;
322
330
  }
331
+ return false;
323
332
  }
324
333
  getConfiguredSections() {
325
334
  const configSections = this.explorer.getConfig().ai?.agents?.researcher?.sections;