@sedum-dev/core 0.1.0-alpha.18 → 0.1.0-alpha.20

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/dist/index.d.ts CHANGED
@@ -3537,7 +3537,6 @@ interface SentencePresentation {
3537
3537
  /** A question the test branches on: judged once and never gating. */
3538
3538
  readonly check?: boolean;
3539
3539
  }
3540
- /** @internal Execute one classified sentence and record it as one step. */
3541
3540
  declare function executeSentence(page: BrowserPage, step: ClassifiedFlowSentence, dependencies: AttemptDependencies, data: Record<string, ResolvedDataEntry>, opaqueEntries: ResolvedDataEntry[], presentation?: SentencePresentation): Promise<"continue" | "failed" | FlowRunResult>;
3542
3541
 
3543
3542
  /** Capture the current safe page and selected target for a terminal goal failure. */
package/dist/index.js CHANGED
@@ -1894,6 +1894,23 @@ var BrowserDriverError = class extends Error {
1894
1894
  var DEFAULT_NAVIGATION_TIMEOUT_MS = 3e4;
1895
1895
  var DEFAULT_SETTLE_TIMEOUT_MS = 4e3;
1896
1896
  var DEFAULT_ACTION_TIMEOUT_MS = 8e3;
1897
+ var PAGE_CLOSE_TIMEOUT_MS = 2e3;
1898
+ function clickDispatched(error2) {
1899
+ const message = error2 instanceof Error ? error2.message : "";
1900
+ if (!message.includes("\nCall log:")) return true;
1901
+ return message.includes("performing click action");
1902
+ }
1903
+ function clickFailure(hover, error2) {
1904
+ if (hover || clickDispatched(error2))
1905
+ return {
1906
+ actionable: false,
1907
+ reason: "action_started",
1908
+ retryable: false,
1909
+ callLog: safeCallLog(error2)
1910
+ };
1911
+ const message = error2 instanceof Error ? error2.message : "";
1912
+ return message.includes("not attached to the DOM") ? { actionable: false, reason: "stale" } : { actionable: false, reason: "not_actionable" };
1913
+ }
1897
1914
  function actionTimeout(value) {
1898
1915
  if (value === void 0) return DEFAULT_ACTION_TIMEOUT_MS;
1899
1916
  if (!Number.isFinite(value) || value <= 0 || value > 2147483647)
@@ -1925,7 +1942,7 @@ function errorText(error2) {
1925
1942
  return error2 instanceof Error ? error2.message : String(error2);
1926
1943
  }
1927
1944
  function isMissingExecutable(error2) {
1928
- return /executable does not exist|executable.*doesn't exist|executable.*not found|channel .* is not installed|ENOENT/i.test(
1945
+ return /executable does not exist|executable.*doesn't exist|executable.*not found|channel .* is not installed|distribution '.*' is not found|ENOENT/i.test(
1929
1946
  errorText(error2)
1930
1947
  );
1931
1948
  }
@@ -2016,7 +2033,7 @@ var PlaywrightPage = class {
2016
2033
  }
2017
2034
  async terminateTimedOutInput() {
2018
2035
  this.terminalAfterInputTimeout = true;
2019
- if (!this.page.isClosed()) await this.page.close().catch(() => void 0);
2036
+ await this.close().catch(() => void 0);
2020
2037
  }
2021
2038
  state() {
2022
2039
  return {
@@ -2289,12 +2306,7 @@ var PlaywrightPage = class {
2289
2306
  });
2290
2307
  return { actionable: true, aim: dispatchAim };
2291
2308
  } catch (error2) {
2292
- return {
2293
- actionable: false,
2294
- reason: "action_started",
2295
- retryable: false,
2296
- callLog: safeCallLog(error2)
2297
- };
2309
+ return clickFailure(Boolean(aim.hover), error2);
2298
2310
  } finally {
2299
2311
  await hide();
2300
2312
  }
@@ -2375,9 +2387,34 @@ var PlaywrightPage = class {
2375
2387
  throw safeOperationError(error2, this.state(), "Scroll");
2376
2388
  }
2377
2389
  }
2390
+ /**
2391
+ * Chromium can accept a close while the page reloads and keep the tab open,
2392
+ * and Playwright then waits for it forever. Give up after a bound: closing
2393
+ * the page's context still disposes it, and every owner closes that next.
2394
+ */
2378
2395
  async close() {
2379
2396
  if (this.page.isClosed()) return;
2380
- await this.page.close();
2397
+ const closing = this.page.close();
2398
+ let timer;
2399
+ try {
2400
+ await Promise.race([
2401
+ closing,
2402
+ new Promise((_resolve, reject) => {
2403
+ timer = setTimeout(
2404
+ () => reject(
2405
+ new BrowserDriverError(
2406
+ "operation-failed",
2407
+ "The page did not close; close its context to dispose it."
2408
+ )
2409
+ ),
2410
+ PAGE_CLOSE_TIMEOUT_MS
2411
+ );
2412
+ })
2413
+ ]);
2414
+ } finally {
2415
+ clearTimeout(timer);
2416
+ closing.catch(() => void 0);
2417
+ }
2381
2418
  }
2382
2419
  };
2383
2420
  var PlaywrightContext = class {
@@ -3083,22 +3120,15 @@ async function observeRoute(page, op, before, remaining) {
3083
3120
  throw new StepExecutionError(op, "action_uncertain", "post_dispatch");
3084
3121
  }
3085
3122
  }
3086
- if (after.document !== before.document) {
3087
- const settled = await page.settle({
3088
- state: "domcontentloaded",
3089
- timeoutMs: remaining(4e3, "post_dispatch")
3090
- }).catch(() => ({ settled: false }));
3091
- if (!settled.settled)
3092
- throw new StepExecutionError(op, "action_uncertain", "post_dispatch");
3093
- }
3094
- if (after.route !== before.route) {
3095
- const observed = await quietPage(
3096
- page,
3097
- 80,
3098
- remaining(1e3, "post_dispatch")
3099
- ).catch(() => ({ quiet: false }));
3100
- if (!observed.quiet)
3101
- throw new StepExecutionError(op, "action_uncertain", "post_dispatch");
3123
+ try {
3124
+ if (after.document !== before.document)
3125
+ await page.settle({
3126
+ state: "domcontentloaded",
3127
+ timeoutMs: remaining(4e3, "post_dispatch")
3128
+ });
3129
+ if (after.route !== before.route)
3130
+ await quietPage(page, 80, remaining(1e3, "post_dispatch"));
3131
+ } catch {
3102
3132
  }
3103
3133
  return after.route !== before.route ? "route_changed" : "no_route_change";
3104
3134
  }
@@ -9379,33 +9409,26 @@ function failureMessage(claim2, found) {
9379
9409
  const frequency = found === 1 ? "once" : `${found} times`;
9380
9410
  return `The text appears ${frequency} on the page.`;
9381
9411
  }
9382
- function unreadable(context) {
9383
- const message = "The page text could not be read.";
9384
- return context.record(
9385
- unsupported(context.step.source.file, context.step.source, message),
9386
- { error: { code: "text_unreadable", message } }
9387
- );
9412
+ function lastReadFailure(context, claim2, found) {
9413
+ const error2 = found === null ? { code: "text_unreadable", message: "The page text could not be read." } : { code: "text_check", message: failureMessage(claim2, found) };
9414
+ return context.record("failed", { error: error2 });
9388
9415
  }
9389
9416
  async function executeTextAssertion(context, claim2, limitMs) {
9390
9417
  const deadline = performance.now() + limitMs;
9391
9418
  const wanted = (claim2.alternatives ?? [claim2.text]).map(
9392
9419
  (text) => revealValues(text, context.data)
9393
9420
  );
9394
- let message = "";
9421
+ let found = null;
9395
9422
  for (; ; ) {
9396
9423
  const before = await readPageVersion(context.page);
9397
9424
  await quietPage(context.page, 80, 2e3).catch(() => void 0);
9398
- let found;
9399
- try {
9400
- found = await countMatches(context, claim2, wanted);
9401
- } catch {
9402
- return unreadable(context);
9403
- }
9404
- if (expectationHolds(claim2, found)) return context.record("continue");
9405
- message = failureMessage(claim2, found);
9406
- if (!await waitForPageChange(context, before, deadline, false)) break;
9425
+ found = await countMatches(context, claim2, wanted).catch(() => null);
9426
+ if (found !== null && expectationHolds(claim2, found))
9427
+ return context.record("continue");
9428
+ const since = found === null ? null : before;
9429
+ if (!await waitForPageChange(context, since, deadline, false)) break;
9407
9430
  }
9408
- return context.record("failed", { error: { code: "text_check", message } });
9431
+ return lastReadFailure(context, claim2, found);
9409
9432
  }
9410
9433
 
9411
9434
  // src/flow-runner/assertions.ts
@@ -9510,6 +9533,7 @@ async function detectStep(page, detector, report, signal) {
9510
9533
 
9511
9534
  // src/flow-runner/action-recovery.ts
9512
9535
  import { setTimeout as delay2 } from "timers/promises";
9536
+ var DEFAULT_ACTION_BUDGET_MS = 8e3;
9513
9537
  async function refreshAfterQuiet(page, remainingMs, signal, locate) {
9514
9538
  const deadline = performance.now() + remainingMs;
9515
9539
  const quiet = await quietPage(page, 80, Math.min(4e3, remainingMs)).catch(
@@ -9534,7 +9558,7 @@ async function performWithRecovery(options) {
9534
9558
  var ActionAttempts = class {
9535
9559
  constructor(options) {
9536
9560
  this.options = options;
9537
- const budget = options.timeoutMs ?? 8e3;
9561
+ const budget = options.timeoutMs ?? DEFAULT_ACTION_BUDGET_MS;
9538
9562
  this.deadline = this.started + budget;
9539
9563
  const timeout = AbortSignal.timeout(Math.max(1, Math.ceil(budget)));
9540
9564
  this.signal = options.signal ? AbortSignal.any([options.signal, timeout]) : timeout;
@@ -9546,6 +9570,7 @@ var ActionAttempts = class {
9546
9570
  signal;
9547
9571
  last;
9548
9572
  attempts = 0;
9573
+ staleRefusals = 0;
9549
9574
  remaining() {
9550
9575
  return this.signal.aborted ? 0 : Math.max(0, this.deadline - performance.now());
9551
9576
  }
@@ -9578,9 +9603,9 @@ var ActionAttempts = class {
9578
9603
  retryable(error2) {
9579
9604
  if (!(error2 instanceof StepExecutionError)) return false;
9580
9605
  if (!error2.retryable || !this.options.allowed()) return false;
9581
- if (this.options.op !== "click")
9582
- return this.attempts === 0 && error2.code === "stale";
9583
- return ["stale", "not_actionable"].includes(error2.code);
9606
+ if (error2.code === "not_actionable") return true;
9607
+ if (error2.code !== "stale") return false;
9608
+ return this.options.op === "click" || this.staleRefusals++ === 0;
9584
9609
  }
9585
9610
  async perform() {
9586
9611
  try {
@@ -9626,39 +9651,16 @@ var LocateRetryError = class extends Error {
9626
9651
  }
9627
9652
  priorCalls;
9628
9653
  };
9629
- function shouldRetryStale2(result, enabled) {
9630
- if (!enabled || result.kind === "resolved") return false;
9631
- return result.reason === "stale";
9632
- }
9633
- async function retryStaleResolution(page, resolved, locate, enabled) {
9634
- if (!shouldRetryStale2(resolved, enabled)) return resolved;
9635
- const priorCalls = resolved.calls;
9636
- const settled = await quietPage(page, 1e3, 4e3).catch(() => ({
9637
- quiet: false
9638
- }));
9639
- if (!settled.quiet) return resolved;
9640
- try {
9641
- const retried = await locate();
9642
- return { ...retried, calls: [...priorCalls, ...retried.calls] };
9643
- } catch (error2) {
9644
- if (isRunWideProviderError(error2)) throw error2;
9645
- throw new LocateRetryError(priorCalls);
9646
- }
9647
- }
9648
- function hasNoCandidates(result) {
9649
- if (result.kind === "resolved") return false;
9650
- return result.reason === "no_candidates";
9654
+ function recoveryFor(result, staleRetry) {
9655
+ if (result.kind === "resolved") return "none";
9656
+ if (result.reason === "no_candidates") return "empty";
9657
+ return staleRetry && result.reason === "stale" ? "stale" : "none";
9651
9658
  }
9652
9659
  function mayRetry(deadline, signal) {
9653
9660
  if (performance.now() >= deadline) return false;
9654
9661
  return signal?.aborted !== true;
9655
9662
  }
9656
- async function observeAfterDelay(page, locate) {
9657
- await new Promise((resolve) => setTimeout(resolve, 400));
9658
- const settled = await quietPage(page, 80, 1e3).catch(() => ({
9659
- quiet: false
9660
- }));
9661
- if (!settled.quiet) return void 0;
9663
+ async function tryLocate(locate) {
9662
9664
  try {
9663
9665
  return await locate();
9664
9666
  } catch (error2) {
@@ -9666,30 +9668,34 @@ async function observeAfterDelay(page, locate) {
9666
9668
  return void 0;
9667
9669
  }
9668
9670
  }
9669
- async function pollEmptyObservation(page, resolved, context) {
9670
- if (!mayRetry(context.deadline, context.signal)) return resolved;
9671
- const fresh = await observeAfterDelay(page, context.locate);
9672
- if (!fresh) return pollEmptyObservation(page, resolved, context);
9673
- const accumulated = { ...fresh, calls: [...resolved.calls, ...fresh.calls] };
9674
- if (!hasNoCandidates(accumulated)) return accumulated;
9675
- return pollEmptyObservation(page, accumulated, context);
9676
- }
9677
- async function retryEmptyObservation(page, initial, locate, signal) {
9678
- if (!hasNoCandidates(initial)) return initial;
9679
- return pollEmptyObservation(page, initial, {
9680
- locate,
9681
- deadline: performance.now() + 8e3,
9682
- ...signal ? { signal } : {}
9683
- });
9671
+ async function relocateAfterRender(page, locate, priorCalls) {
9672
+ await quietPage(page, 80, 1e3).catch(() => void 0);
9673
+ const fresh = await tryLocate(locate);
9674
+ if (!fresh) throw new LocateRetryError(priorCalls);
9675
+ return fresh;
9676
+ }
9677
+ async function relocateWhenFilled(page, locate) {
9678
+ await new Promise((resolve) => setTimeout(resolve, 400));
9679
+ const settled = await quietPage(page, 80, 1e3).catch(() => ({
9680
+ quiet: false
9681
+ }));
9682
+ return settled.quiet ? tryLocate(locate) : void 0;
9683
+ }
9684
+ async function readAgain(page, recovery, locate, current) {
9685
+ const fresh = recovery === "stale" ? await relocateAfterRender(page, locate, current.calls) : await relocateWhenFilled(page, locate);
9686
+ return fresh ? { ...fresh, calls: [...current.calls, ...fresh.calls] } : current;
9684
9687
  }
9685
9688
  async function reobserveLocator(page, first, locate, options) {
9686
- const fresh = await retryStaleResolution(
9687
- page,
9688
- first,
9689
- locate,
9690
- options.staleRetry
9691
- );
9692
- return retryEmptyObservation(page, fresh, locate, options.signal);
9689
+ const deadline = performance.now() + 8e3;
9690
+ let staleReads = 0;
9691
+ let current = first;
9692
+ for (; ; ) {
9693
+ const recovery = recoveryFor(current, options.staleRetry && staleReads < 4);
9694
+ if (recovery === "none" || !mayRetry(deadline, options.signal))
9695
+ return current;
9696
+ staleReads += Number(recovery === "stale");
9697
+ current = await readAgain(page, recovery, locate, current);
9698
+ }
9693
9699
  }
9694
9700
  function unresolvedReason(reason, op) {
9695
9701
  if (reason === "none")
@@ -9698,6 +9704,10 @@ function unresolvedReason(reason, op) {
9698
9704
  const action = op === "type" ? "type into" : op;
9699
9705
  return `the page had nothing to ${action}`;
9700
9706
  }
9707
+ if (reason === "stale")
9708
+ return "the page kept replacing it while it was located; wait for the page to settle first";
9709
+ if (reason === "provider_error")
9710
+ return "the model call that picks the target failed; run the test again";
9701
9711
  return "several elements match it; name the one you mean more precisely";
9702
9712
  }
9703
9713
  function visionFailure(vision) {
@@ -9707,7 +9717,8 @@ function visionFallbackNote(resolved) {
9707
9717
  const vision = resolved.diagnostic.vision;
9708
9718
  if (vision?.outcome === "abstained")
9709
9719
  return "the vision model did not find it either";
9710
- if (vision || resolved.reason === "no_candidates") return void 0;
9720
+ if (vision || ["no_candidates", "stale"].includes(resolved.reason))
9721
+ return void 0;
9711
9722
  return "vision fallback could not run (it needs 2 to 40 fully visible, unobscured controls on screen)";
9712
9723
  }
9713
9724
  function unresolvedLocatorWhy(resolved, op, visionEnabled) {
@@ -9751,6 +9762,20 @@ function chooseOption(sentence, labels) {
9751
9762
  }
9752
9763
 
9753
9764
  // src/flow-runner/sentence-execution.ts
9765
+ var FAILED_LOCATOR_REASONS = [
9766
+ "none",
9767
+ "ambiguous",
9768
+ "no_candidates",
9769
+ "stale",
9770
+ // Run-wide provider failures are thrown before this; what reaches here is
9771
+ // one failed model call, which a retry can get past.
9772
+ "provider_error"
9773
+ ];
9774
+ var FAILED_ACTION_CODES = [
9775
+ "not_actionable",
9776
+ "stale",
9777
+ "invalid_input"
9778
+ ];
9754
9779
  async function executeSentence(page, step, dependencies, data, opaqueEntries, presentation = {}) {
9755
9780
  const started = performance.now();
9756
9781
  const report = dependencies.report;
@@ -9899,24 +9924,18 @@ async function executeSentence(page, step, dependencies, data, opaqueEntries, pr
9899
9924
  });
9900
9925
  return error2;
9901
9926
  };
9902
- const quiet = await quietPage(page, 80, 4e3).catch(() => ({
9927
+ const settleMs = Math.max(4e3, dependencies.verifyGraceMs ?? 0);
9928
+ const quiet = await quietPage(page, 80, settleMs).catch(() => ({
9903
9929
  quiet: false
9904
9930
  }));
9905
9931
  const waits = step.op === "verify" && waitUntilTimeoutMs(step.text) !== null;
9906
9932
  if (!quiet.quiet && !waits)
9907
- return record(
9908
- unsupported(
9909
- step.source.file,
9910
- step.source,
9911
- "The page did not settle before this step could be resolved."
9912
- ),
9913
- {
9914
- error: {
9915
- code: "observation_timeout",
9916
- message: "The page did not settle before this step could be resolved."
9917
- }
9933
+ return record("failed", {
9934
+ error: {
9935
+ code: "observation_timeout",
9936
+ message: "The page did not settle before this step could be resolved."
9918
9937
  }
9919
- );
9938
+ });
9920
9939
  let typeValue;
9921
9940
  if (step.op === "type") {
9922
9941
  const operand = validateTypeOperand(step);
@@ -10287,7 +10306,7 @@ async function executeSentence(page, step, dependencies, data, opaqueEntries, pr
10287
10306
  }
10288
10307
  );
10289
10308
  if (resolved.kind !== "resolved") {
10290
- if (resolved.reason === "none" || resolved.reason === "ambiguous" || resolved.reason === "no_candidates")
10309
+ if (FAILED_LOCATOR_REASONS.includes(resolved.reason))
10291
10310
  return record("failed", {
10292
10311
  locator: resolved,
10293
10312
  error: {
@@ -10349,6 +10368,9 @@ async function executeSentence(page, step, dependencies, data, opaqueEntries, pr
10349
10368
  let observation = 1;
10350
10369
  await performWithRecovery({
10351
10370
  op: step.op,
10371
+ // A target can stay disabled while the app works; wait as long as the
10372
+ // step's grace allows, never less than the default action budget.
10373
+ timeoutMs: Math.max(DEFAULT_ACTION_BUDGET_MS, actionGraceMs),
10352
10374
  ...dependencies.signal ? { signal: dependencies.signal } : {},
10353
10375
  perform: performAction,
10354
10376
  allowed: () => !visionAttempted,
@@ -10427,7 +10449,7 @@ async function executeSentence(page, step, dependencies, data, opaqueEntries, pr
10427
10449
  } catch (error2) {
10428
10450
  if (isRunWideProviderError(error2)) throw await runWide(error2);
10429
10451
  const actionError2 = error2 instanceof StepExecutionError ? error2 : null;
10430
- const failed = actionError2 && (actionError2.code === "not_actionable" || actionError2.code === "invalid_input");
10452
+ const failed = actionError2 && FAILED_ACTION_CODES.includes(actionError2.code);
10431
10453
  const facts = {
10432
10454
  locator: resolved,
10433
10455
  replayFrame,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sedum-dev/core",
3
- "version": "0.1.0-alpha.18",
3
+ "version": "0.1.0-alpha.20",
4
4
  "repository": "https://github.com/sedum-dev/sedum",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -32,7 +32,7 @@
32
32
  "playwright-core": "1.63.0",
33
33
  "sharp": "0.35.5",
34
34
  "yaml": "2.9.1",
35
- "zod": "4.6.5"
35
+ "zod": "4.6.7"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@hegeldev/hegel": "0.4.7"