@burdenoff/ui-engine 2026.1127.4 → 2026.1129.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 (77) hide show
  1. package/README.md +68 -4
  2. package/dist/analyze/analyze.d.ts +19 -0
  3. package/dist/analyze/context.d.ts +40 -0
  4. package/dist/analyze/index.d.ts +10 -0
  5. package/dist/analyze/messages.d.ts +46 -0
  6. package/dist/analyze/rules/fixed-wait.d.ts +6 -0
  7. package/dist/analyze/rules/hardware-input.d.ts +2 -0
  8. package/dist/analyze/rules/heal-consequential.d.ts +2 -0
  9. package/dist/analyze/rules/index.d.ts +3 -0
  10. package/dist/analyze/rules/inline-target.d.ts +2 -0
  11. package/dist/analyze/rules/masked-value.d.ts +8 -0
  12. package/dist/analyze/rules/no-cleanup.d.ts +2 -0
  13. package/dist/analyze/rules/plain-text-secret.d.ts +2 -0
  14. package/dist/analyze/rules/popup-handler-loop.d.ts +2 -0
  15. package/dist/analyze/rules/rule.d.ts +15 -0
  16. package/dist/analyze/rules/secret-origin.d.ts +7 -0
  17. package/dist/analyze/rules/sensitive-screenshot.d.ts +2 -0
  18. package/dist/analyze/rules/single-method-target.d.ts +2 -0
  19. package/dist/analyze/rules/unknown-variable.d.ts +2 -0
  20. package/dist/analyze/rules/unused-value.d.ts +2 -0
  21. package/dist/analyze/sensitive.d.ts +14 -0
  22. package/dist/analyze/types.d.ts +89 -0
  23. package/dist/analyze.d.ts +2 -0
  24. package/dist/analyze.js +26 -0
  25. package/dist/dom-runtime.iife.js +1 -1
  26. package/dist/dom-runtime.js +2 -2
  27. package/dist/drivers.js +13 -9
  28. package/dist/heal/answer.d.ts +26 -0
  29. package/dist/heal/consequence.d.ts +74 -0
  30. package/dist/heal/index.d.ts +19 -0
  31. package/dist/heal/modes.d.ts +25 -0
  32. package/dist/heal/popup-limits.d.ts +11 -0
  33. package/dist/heal/popup.d.ts +48 -0
  34. package/dist/heal/question.d.ts +41 -0
  35. package/dist/heal/target-from-node.d.ts +9 -0
  36. package/dist/heal/types.d.ts +134 -0
  37. package/dist/heal/verify-rules.d.ts +19 -0
  38. package/dist/heal/verify.d.ts +52 -0
  39. package/dist/heal.d.ts +2 -0
  40. package/dist/heal.js +106 -0
  41. package/dist/index-20bbwwc9.js +37 -0
  42. package/dist/{index-pm13mvvc.js → index-2g060ryg.js} +3 -196
  43. package/dist/index-6h7ah1dw.js +328 -0
  44. package/dist/{index-h88y5qgv.js → index-6kva2gvd.js} +6 -4
  45. package/dist/{index-ct2xctrq.js → index-70vm0qz8.js} +2 -2
  46. package/dist/{index-3f1j8386.js → index-8w31tpfq.js} +38 -19
  47. package/dist/{index-zzhysbkj.js → index-eff47v1g.js} +436 -20
  48. package/dist/{index-3jymbm5t.js → index-g7724awk.js} +1 -92
  49. package/dist/index-gvavb597.js +195 -0
  50. package/dist/index-hw5npwsv.js +63 -0
  51. package/dist/{index-ycs2bz1a.js → index-mhv6a02p.js} +11 -166
  52. package/dist/index-p5823rpv.js +0 -0
  53. package/dist/{index-5k26keah.js → index-qg9yfr7r.js} +16 -67
  54. package/dist/{index-895kscx3.js → index-qmv3ar39.js} +7 -5
  55. package/dist/index-t55wvhvh.js +229 -0
  56. package/dist/{index-77pcz6n3.js → index-vnv9tp11.js} +4 -2
  57. package/dist/index-w9jc25y9.js +306 -0
  58. package/dist/index-xdtanv9b.js +843 -0
  59. package/dist/{index-vg8r40y7.js → index-yc0jgph6.js} +7 -0
  60. package/dist/index-zhbwhdya.js +407 -0
  61. package/dist/index.d.ts +2 -0
  62. package/dist/index.js +156 -26
  63. package/dist/interpret/driver.d.ts +14 -0
  64. package/dist/interpret/errors.d.ts +11 -2
  65. package/dist/interpret/events.d.ts +4 -0
  66. package/dist/interpret/run.d.ts +33 -0
  67. package/dist/interpret.js +5 -4
  68. package/dist/script.js +29 -23
  69. package/dist/target/index.d.ts +2 -0
  70. package/dist/target/roles.d.ts +11 -0
  71. package/dist/target/snapshot-mask.d.ts +49 -0
  72. package/dist/target/snapshot-tree.d.ts +65 -0
  73. package/dist/target/snapshot-url.d.ts +6 -0
  74. package/dist/target/snapshot.d.ts +1 -6
  75. package/dist/target.js +55 -13
  76. package/dist/testing.js +2 -2
  77. package/package.json +11 -3
@@ -1,25 +1,54 @@
1
1
  import {
2
+ uiTargetSchema2,
2
3
  UiScriptError2,
3
4
  parseUiScript2
4
- } from "./index-895kscx3.js";
5
+ } from "./index-qmv3ar39.js";
6
+ import {
7
+ UI_CONSEQUENCES2,
8
+ parseHealMode2,
9
+ decideHeal2,
10
+ usablePopupHandlers2,
11
+ matchesUrlPattern2,
12
+ verifyHealCandidate2
13
+ } from "./index-w9jc25y9.js";
14
+ import {
15
+ isActingStep,
16
+ classifyConsequence2,
17
+ elementInfoFromTarget2,
18
+ HEALABLE_ACTIONS2,
19
+ MAX_POPUP_HANDLERS_PER_STEP2,
20
+ POPUP_HANDLER_STEP_WAIT_MS2
21
+ } from "./index-xdtanv9b.js";
5
22
  import {
6
23
  isUiErrorCode2,
7
24
  isRetryableInScript2,
8
25
  UiRunError2,
9
26
  UiDriverError2,
10
27
  realClock2
11
- } from "./index-vg8r40y7.js";
28
+ } from "./index-yc0jgph6.js";
29
+ import {
30
+ describeTarget2,
31
+ describeStep2
32
+ } from "./index-gvavb597.js";
12
33
  import {
13
- walkSteps2,
14
- stepTargets2,
15
34
  missingCapabilities2
16
- } from "./index-3jymbm5t.js";
35
+ } from "./index-hw5npwsv.js";
36
+ import {
37
+ walkSteps2,
38
+ stepTargets2
39
+ } from "./index-g7724awk.js";
40
+ import {
41
+ isSnapshotPath2,
42
+ indexSnapshot2,
43
+ maskSnapshot2
44
+ } from "./index-zhbwhdya.js";
17
45
  import {
18
46
  MAX_TYPED_TEXT2
19
- } from "./index-77pcz6n3.js";
47
+ } from "./index-vnv9tp11.js";
20
48
  import {
21
- containsText2
22
- } from "./index-ycs2bz1a.js";
49
+ containsText2,
50
+ snapshotUrl2
51
+ } from "./index-t55wvhvh.js";
23
52
  import {
24
53
  redactCredentials2
25
54
  } from "./index-mgztm37r.js";
@@ -463,14 +492,33 @@ class StepFailure extends Error {
463
492
  stepPath;
464
493
  stepId;
465
494
  detail;
466
- constructor(code, message, stepPath, stepId, detail) {
495
+ primary;
496
+ heal;
497
+ constructor(code, message, stepPath, stepId, detail, primary = false, heal) {
467
498
  super(message);
468
499
  this.code = code;
469
500
  this.stepPath = stepPath;
470
501
  this.stepId = stepId;
471
502
  this.detail = detail;
503
+ this.primary = primary;
504
+ this.heal = heal;
472
505
  }
473
506
  }
507
+ var POPUP_CODES = new Set(["ELEMENT_NOT_FOUND", "ELEMENT_NOT_INTERACTABLE"]);
508
+ var HEAL_CODES = new Set(["ELEMENT_NOT_FOUND", "ELEMENT_AMBIGUOUS"]);
509
+ function boundedHandlerStep(step) {
510
+ const timeoutMs = Math.min(step.timeoutMs ?? POPUP_HANDLER_STEP_WAIT_MS2, POPUP_HANDLER_STEP_WAIT_MS2);
511
+ if (step.action === "waitFor" && step.milliseconds !== undefined) {
512
+ return { ...step, timeoutMs, milliseconds: Math.min(step.milliseconds, POPUP_HANDLER_STEP_WAIT_MS2) };
513
+ }
514
+ return { ...step, timeoutMs };
515
+ }
516
+ function healFailureCode(code) {
517
+ return code === "ELEMENT_AMBIGUOUS" ? "ELEMENT_AMBIGUOUS" : "ELEMENT_NOT_FOUND";
518
+ }
519
+ function isConsequence(value) {
520
+ return typeof value === "string" && UI_CONSEQUENCES2.some((consequence) => consequence === value);
521
+ }
474
522
  var FLOW_ACTIONS = new Set(["if", "forEach", "while", "retryScope", "set", "log", "call"]);
475
523
  function clipMessage(text) {
476
524
  return redactCredentials2(text).slice(0, MAX_EVENT_MESSAGE2);
@@ -572,6 +620,14 @@ class Interpreter {
572
620
  ending = false;
573
621
  inCleanup = false;
574
622
  iterations = [];
623
+ overrides = new Map;
624
+ healed = new Map;
625
+ healTried = new Set;
626
+ healedSteps = new Set;
627
+ popupsHandled = 0;
628
+ popupHandlers = [];
629
+ popupRejections = [];
630
+ inRepair = false;
575
631
  constructor(driver, options) {
576
632
  this.driver = driver;
577
633
  this.options = options;
@@ -593,6 +649,13 @@ class Interpreter {
593
649
  const failure = error instanceof UiRunError2 ? error : new UiRunError2("INVALID_SCRIPT", "The UI script could not be read");
594
650
  return this.finish("failed", { code: failure.code, family: failure.family, message: failure.message }, failure.detail);
595
651
  }
652
+ if (this.popupRejections.length > 0) {
653
+ await this.emit({
654
+ kind: "LOG",
655
+ message: `${this.popupRejections.length} pop-up handler(s) were not used: they are not valid or could loop`,
656
+ detail: { level: "warn", popupHandlersRejected: this.popupRejections }
657
+ });
658
+ }
596
659
  let status = "succeeded";
597
660
  let failure;
598
661
  let failureDetailValue;
@@ -659,7 +722,8 @@ class Interpreter {
659
722
  family: known.family,
660
723
  message: error.message,
661
724
  stepPath: error.stepPath,
662
- stepId: error.stepId
725
+ stepId: error.stepId,
726
+ ...error.heal ? { heal: error.heal } : {}
663
727
  };
664
728
  }
665
729
  async finish(status, failure, detail, outputs = {}) {
@@ -690,7 +754,8 @@ class Interpreter {
690
754
  dropped: this.emitter.droppedCount,
691
755
  truncated: this.emitter.isTruncated
692
756
  },
693
- durationMs
757
+ durationMs,
758
+ ...this.healedSteps.size > 0 || this.popupsHandled > 0 ? { repairs: { healed: this.healedSteps.size, popupsHandled: this.popupsHandled } } : {}
694
759
  };
695
760
  }
696
761
  preflight(input) {
@@ -712,19 +777,50 @@ class Interpreter {
712
777
  missing: needed
713
778
  });
714
779
  }
780
+ const disabledBlocks = [];
715
781
  for (const { step, path } of walkSteps2(this.script)) {
716
- if (step.action === "type" && step.masked === true && step.text === undefined && step.disabled !== true) {
782
+ if (disabledBlocks.some((prefix) => path.startsWith(prefix)))
783
+ continue;
784
+ if (step.disabled === true) {
785
+ disabledBlocks.push(`${path}.`);
786
+ continue;
787
+ }
788
+ if (step.action === "type" && step.masked === true && step.text === undefined) {
717
789
  throw new UiRunError2("INVALID_SCRIPT", `Step ${step.id} types a masked value and needs a value before it can run`, {
718
790
  path
719
791
  });
720
792
  }
721
793
  }
722
794
  this.checkRange();
795
+ this.bindOverrides();
796
+ if (this.options.popupHandlers && this.options.popupHandlers.length > 0) {
797
+ const usable = usablePopupHandlers2(this.options.popupHandlers);
798
+ this.popupHandlers = usable.handlers;
799
+ this.popupRejections = usable.rejected;
800
+ }
723
801
  this.bindInputs();
724
802
  for (const variable of this.script.variables ?? []) {
725
803
  this.variables.set(variable.name, variable.initial === undefined ? null : this.evaluateOrInvalid(variable.initial, `variables.${variable.name}`));
726
804
  }
727
805
  }
806
+ bindOverrides() {
807
+ const overrides = this.options.targetOverrides;
808
+ if (!overrides)
809
+ return;
810
+ const steps = new Map;
811
+ for (const { step } of walkSteps2(this.script))
812
+ steps.set(step.id, step);
813
+ for (const [stepId, value] of Object.entries(overrides)) {
814
+ const step = steps.get(stepId);
815
+ const parsed = uiTargetSchema2.safeParse(value);
816
+ if (!step || !HEALABLE_ACTIONS2.has(step.action) || !this.primaryRef(step) || !parsed.success) {
817
+ throw new UiRunError2("INVALID_SCRIPT", "A target override names no step it can apply to, or is not a valid target", {
818
+ targetOverride: stepId.slice(0, 64)
819
+ });
820
+ }
821
+ this.overrides.set(stepId, parsed.data);
822
+ }
823
+ }
728
824
  evaluateOrInvalid(expression, where) {
729
825
  try {
730
826
  return evaluateExpression2(expression, this.scope);
@@ -993,6 +1089,16 @@ class Interpreter {
993
1089
  await this.sleep(step.retry?.delayMs ?? DEFAULT_RETRY_DELAY_MS2);
994
1090
  }
995
1091
  }
1092
+ let repaired = {};
1093
+ if (failure && this.canRepair(step, path, failure)) {
1094
+ const result = await this.repair(step, path, tried, failure);
1095
+ if ("outcome" in result) {
1096
+ outcome = result.outcome;
1097
+ repaired = result.info;
1098
+ failure = undefined;
1099
+ } else
1100
+ failure = result.failure;
1101
+ }
996
1102
  const durationMs = Math.max(0, this.clock.now() - began);
997
1103
  const elementId = this.elementIdOf(step);
998
1104
  if (failure) {
@@ -1023,7 +1129,7 @@ class Interpreter {
1023
1129
  ...screenshotFileId ? { screenshotFileId } : {},
1024
1130
  error: failure.code
1025
1131
  });
1026
- if (step.onError === "continue") {
1132
+ if (step.onError === "continue" && failure.code !== "HEAL_APPROVAL_REQUIRED") {
1027
1133
  if (step.saveAs !== undefined)
1028
1134
  this.store(step.saveAs, null);
1029
1135
  this.endIfStopAfter(path);
@@ -1042,8 +1148,9 @@ class Interpreter {
1042
1148
  durationMs,
1043
1149
  ...outcome?.resolvedBy ? { resolvedBy: outcome.resolvedBy } : {},
1044
1150
  ...screenshotFileId ? { screenshotFileId } : {},
1045
- detail: this.iterationDetail()
1151
+ detail: { ...this.iterationDetail(), ...this.repairDetail(step, repaired) }
1046
1152
  });
1153
+ const healed = this.healed.has(step.id) && this.primaryRef(step) !== undefined;
1047
1154
  this.log({
1048
1155
  path,
1049
1156
  stepId: step.id,
@@ -1052,7 +1159,9 @@ class Interpreter {
1052
1159
  durationMs,
1053
1160
  ...outcome?.resolvedBy ? { method: outcome.resolvedBy } : {},
1054
1161
  ...elementId ? { elementId } : {},
1055
- ...screenshotFileId ? { screenshotFileId } : {}
1162
+ ...screenshotFileId ? { screenshotFileId } : {},
1163
+ ...healed ? { healed: true } : {},
1164
+ ...repaired.popupHandlerId ? { popupHandlerId: repaired.popupHandlerId } : {}
1056
1165
  });
1057
1166
  if (!FLOW_ACTIONS.has(step.action)) {
1058
1167
  this.lastCompleted = path;
@@ -1060,6 +1169,301 @@ class Interpreter {
1060
1169
  }
1061
1170
  this.endIfStopAfter(path);
1062
1171
  }
1172
+ repairDetail(step, repaired) {
1173
+ const healed = this.primaryRef(step) ? this.healed.get(step.id) : undefined;
1174
+ const overridden = !healed && this.overrides.has(step.id);
1175
+ return {
1176
+ ...healed ? {
1177
+ healed: {
1178
+ method: "model",
1179
+ nodePath: healed.nodePath,
1180
+ ...healed.proposalId ? { proposalId: healed.proposalId } : {}
1181
+ }
1182
+ } : {},
1183
+ ...repaired.popupHandlerId ? { popupHandlerId: repaired.popupHandlerId } : {},
1184
+ ...overridden ? { targetOverride: true } : {}
1185
+ };
1186
+ }
1187
+ canRepair(step, path, failure) {
1188
+ if (this.inRepair || this.driver.replay || failure.stepPath !== path || !failure.primary)
1189
+ return false;
1190
+ const popups = this.popupHandlers.length > 0 && POPUP_CODES.has(failure.code);
1191
+ const heal = this.options.onTargetNotFound !== undefined && HEAL_CODES.has(failure.code) && HEALABLE_ACTIONS2.has(step.action);
1192
+ return popups || heal;
1193
+ }
1194
+ async repair(step, path, attempt, failure) {
1195
+ let current = failure;
1196
+ if (this.popupHandlers.length > 0 && POPUP_CODES.has(current.code)) {
1197
+ const tried = new Set;
1198
+ for (let used = 0;used < MAX_POPUP_HANDLERS_PER_STEP2; used += 1) {
1199
+ const handler = await this.findPopupHandler(step, path, attempt, tried);
1200
+ if (!handler)
1201
+ break;
1202
+ tried.add(handler.id);
1203
+ if (!await this.runPopupHandler(handler, step, path))
1204
+ continue;
1205
+ try {
1206
+ const outcome = await this.attemptStep(step, path, attempt + 1, false);
1207
+ this.popupsHandled += 1;
1208
+ return { outcome, info: { popupHandlerId: handler.id } };
1209
+ } catch (error) {
1210
+ if (!(error instanceof StepFailure))
1211
+ throw error;
1212
+ current = error;
1213
+ if (current.stepPath !== path || !current.primary || !POPUP_CODES.has(current.code))
1214
+ break;
1215
+ }
1216
+ }
1217
+ }
1218
+ if (HEAL_CODES.has(current.code) && current.stepPath === path && current.primary) {
1219
+ return this.heal(step, path, attempt, current);
1220
+ }
1221
+ return { failure: current };
1222
+ }
1223
+ async findPopupHandler(step, path, attempt, tried) {
1224
+ const context = this.context(step, path, attempt);
1225
+ let url;
1226
+ for (const handler of this.popupHandlers) {
1227
+ this.checkCancelled();
1228
+ if (tried.has(handler.id))
1229
+ continue;
1230
+ try {
1231
+ if (handler.match.urlPattern !== undefined) {
1232
+ url ??= await this.driver.currentUrl(context);
1233
+ if (!matchesUrlPattern2(url, handler.match.urlPattern))
1234
+ continue;
1235
+ }
1236
+ const resolution = await this.driver.resolve(handler.match.target, { state: "visible" }, context);
1237
+ if (resolution.status === "found")
1238
+ return handler;
1239
+ } catch {
1240
+ if (this.options.signal?.aborted && !this.inCleanup)
1241
+ throw new StopSignal("cancelled");
1242
+ }
1243
+ }
1244
+ return;
1245
+ }
1246
+ async runPopupHandler(handler, step, path) {
1247
+ this.inRepair = true;
1248
+ try {
1249
+ for (const [index, handlerStep] of handler.steps.entries()) {
1250
+ if (handlerStep.disabled === true)
1251
+ continue;
1252
+ const bounded = boundedHandlerStep(handlerStep);
1253
+ await this.perform(bounded, `${path}.popup.${index}`, 1, false);
1254
+ }
1255
+ } catch (error) {
1256
+ if (!(error instanceof StepFailure))
1257
+ throw error;
1258
+ await this.emit({
1259
+ kind: "LOG",
1260
+ stepPath: path,
1261
+ stepId: step.id,
1262
+ action: step.action,
1263
+ errorCode: error.code,
1264
+ message: clipMessage(`The pop-up handler ${handler.id} did not finish`),
1265
+ detail: { level: "warn", popupHandlerId: handler.id }
1266
+ });
1267
+ return false;
1268
+ } finally {
1269
+ this.inRepair = false;
1270
+ }
1271
+ await this.emit({
1272
+ kind: "LOG",
1273
+ stepPath: path,
1274
+ stepId: step.id,
1275
+ action: step.action,
1276
+ message: clipMessage(`The pop-up handler ${handler.id} ran`),
1277
+ detail: { level: "info", popupHandlerId: handler.id }
1278
+ });
1279
+ return true;
1280
+ }
1281
+ withHeal(failure, heal) {
1282
+ return new StepFailure(failure.code, failure.message, failure.stepPath, failure.stepId, { ...failure.detail ?? {}, heal }, failure.primary, heal);
1283
+ }
1284
+ checkedAnswer(answer, snapshot) {
1285
+ if (answer === null || typeof answer !== "object" || !("kind" in answer)) {
1286
+ return { kind: "unavailable", reason: "INVALID_ANSWER" };
1287
+ }
1288
+ if (answer.kind === "not_present")
1289
+ return { kind: "not_present" };
1290
+ if (answer.kind !== "candidate" || !("candidate" in answer)) {
1291
+ const reason = "reason" in answer && typeof answer.reason === "string" ? answer.reason.slice(0, 64) : "UNAVAILABLE";
1292
+ return { kind: "unavailable", reason };
1293
+ }
1294
+ const candidate = answer.candidate;
1295
+ if (candidate === null || typeof candidate !== "object")
1296
+ return { kind: "unavailable", reason: "INVALID_ANSWER" };
1297
+ const nodePath = "nodePath" in candidate ? candidate.nodePath : undefined;
1298
+ const target = uiTargetSchema2.safeParse("proposedTarget" in candidate ? candidate.proposedTarget : undefined);
1299
+ const confidence = "confidence" in candidate ? candidate.confidence : undefined;
1300
+ if (!isSnapshotPath2(nodePath) || !target.success)
1301
+ return { kind: "unavailable", reason: "INVALID_ANSWER" };
1302
+ if (!indexSnapshot2(snapshot).byPath.has(nodePath))
1303
+ return { kind: "unavailable", reason: "INVALID_ANSWER" };
1304
+ const usableConfidence = typeof confidence === "number" && Number.isFinite(confidence) && confidence >= 0 && confidence <= 1;
1305
+ return {
1306
+ kind: "candidate",
1307
+ candidate: { nodePath, proposedTarget: target.data, ...usableConfidence ? { confidence } : {} }
1308
+ };
1309
+ }
1310
+ async askHeal(hook, request) {
1311
+ const signal = this.inCleanup ? undefined : this.options.signal;
1312
+ if (!signal)
1313
+ return hook(request, {});
1314
+ if (signal.aborted)
1315
+ throw new StopSignal("cancelled");
1316
+ return new Promise((resolve, reject) => {
1317
+ const onAbort = () => reject(new StopSignal("cancelled"));
1318
+ signal.addEventListener("abort", onAbort, { once: true });
1319
+ hook(request, { signal }).then((result) => {
1320
+ signal.removeEventListener("abort", onAbort);
1321
+ resolve(result);
1322
+ }, (error) => {
1323
+ signal.removeEventListener("abort", onAbort);
1324
+ reject(error instanceof Error ? error : new Error("The heal hook failed"));
1325
+ });
1326
+ });
1327
+ }
1328
+ async heal(step, path, attempt, failure) {
1329
+ const hook = this.options.onTargetNotFound;
1330
+ const ref = this.primaryRef(step);
1331
+ if (!hook || !ref || !HEALABLE_ACTIONS2.has(step.action) || this.healTried.has(step.id))
1332
+ return { failure };
1333
+ this.healTried.add(step.id);
1334
+ const failedTarget = this.stepTarget(step, ref);
1335
+ const context = this.context(step, path, attempt);
1336
+ let snapshot = null;
1337
+ let url = "";
1338
+ try {
1339
+ snapshot = await this.driver.treeSnapshot?.(context) ?? null;
1340
+ url = await this.driver.currentUrl(context);
1341
+ } catch {
1342
+ if (this.options.signal?.aborted && !this.inCleanup)
1343
+ throw new StopSignal("cancelled");
1344
+ }
1345
+ if (!snapshot)
1346
+ return { failure: this.withHeal(failure, { outcome: "unavailable", reason: "NO_SNAPSHOT" }) };
1347
+ const masked = maskSnapshot2(snapshot, { maskedPaths: this.options.healing?.maskedPaths ?? [] });
1348
+ const elements = this.script.elements ?? {};
1349
+ const element = "element" in ref ? elements[ref.element] : undefined;
1350
+ const request = {
1351
+ stepPath: path,
1352
+ stepId: step.id,
1353
+ stepSentence: describeStep2(step, elements).fallback,
1354
+ action: step.action,
1355
+ ..."element" in ref ? {
1356
+ elementRef: {
1357
+ element: ref.element,
1358
+ ...element ? { elementId: element.elementId, version: element.version } : {}
1359
+ }
1360
+ } : {},
1361
+ target: failedTarget,
1362
+ description: describeTarget2(ref, elements),
1363
+ failureCode: healFailureCode(failure.code),
1364
+ url: snapshotUrl2(url) ?? "",
1365
+ snapshot: masked.snapshot,
1366
+ maskedPaths: masked.maskedPaths,
1367
+ screenshotMask: masked.screenshotMask
1368
+ };
1369
+ let result;
1370
+ try {
1371
+ result = await this.askHeal(hook, request);
1372
+ } catch (error) {
1373
+ if (error instanceof StopSignal)
1374
+ throw error;
1375
+ return { failure: this.withHeal(failure, { outcome: "unavailable", reason: "HOOK_FAILED" }) };
1376
+ }
1377
+ if (result === null || typeof result !== "object") {
1378
+ return { failure: this.withHeal(failure, { outcome: "unavailable", reason: "HOOK_FAILED" }) };
1379
+ }
1380
+ const mode = parseHealMode2(result.mode) ?? "off";
1381
+ if (mode === "off")
1382
+ return { failure };
1383
+ await this.emit({
1384
+ kind: "LOG",
1385
+ stepPath: path,
1386
+ stepId: step.id,
1387
+ action: step.action,
1388
+ errorCode: failure.code,
1389
+ message: "The target was not found; healing was asked for a repaired target",
1390
+ detail: { level: "info", healRequested: true, mode }
1391
+ });
1392
+ const proposalId = typeof result.proposalId === "string" && result.proposalId.length > 0 && result.proposalId.length <= 128 ? result.proposalId : undefined;
1393
+ const withProposal = proposalId ? { proposalId } : {};
1394
+ const answer = this.checkedAnswer(result.answer, masked.snapshot);
1395
+ if (answer.kind === "not_present") {
1396
+ return { failure: this.withHeal(failure, { outcome: "not_present", mode, ...withProposal }) };
1397
+ }
1398
+ if (answer.kind === "unavailable") {
1399
+ return {
1400
+ failure: this.withHeal(failure, { outcome: "unavailable", mode, reason: answer.reason, ...withProposal })
1401
+ };
1402
+ }
1403
+ const candidate = answer.candidate;
1404
+ const check = await verifyHealCandidate2(this.driver, candidate, step, {
1405
+ snapshot: masked.snapshot,
1406
+ failedTarget,
1407
+ stepContext: context
1408
+ });
1409
+ if (!check.ok) {
1410
+ return {
1411
+ failure: this.withHeal(failure, {
1412
+ outcome: "check_failed",
1413
+ mode,
1414
+ candidate,
1415
+ reason: check.reason,
1416
+ ...withProposal
1417
+ })
1418
+ };
1419
+ }
1420
+ const classified = {
1421
+ action: step.action,
1422
+ ...step.action === "type" && step.pressEnter === true ? { pressEnter: true } : {}
1423
+ };
1424
+ const keywords = this.options.healing?.keywords;
1425
+ const verdict = classifyConsequence2(check.element, classified, keywords);
1426
+ const intended = classifyConsequence2(elementInfoFromTarget2(failedTarget, element?.name), classified, keywords);
1427
+ const consequence = verdict.consequence ?? intended.consequence ?? (isConsequence(result.consequence) ? result.consequence : undefined);
1428
+ const unreadable = isActingStep(classified) && (check.sensitive || check.element.name?.trim() === "");
1429
+ const alwaysAsk = result.alwaysAsk === true || verdict.keyword !== null || intended.keyword !== null || unreadable || (this.options.healing?.alwaysAskStepIds ?? []).includes(step.id);
1430
+ const decision = decideHeal2(mode, { consequence: consequence ?? null, alwaysAsk }) ?? "stop_suggest";
1431
+ const report = {
1432
+ outcome: decision === "continue" ? "healed" : decision === "ask" ? "approval_required" : "suggested",
1433
+ mode,
1434
+ decision,
1435
+ candidate,
1436
+ ...withProposal,
1437
+ ...consequence ? { consequence } : {},
1438
+ ...alwaysAsk ? { alwaysAsk: true } : {}
1439
+ };
1440
+ if (decision === "stop_suggest")
1441
+ return { failure: this.withHeal(failure, report) };
1442
+ if (decision === "ask") {
1443
+ return {
1444
+ failure: new StepFailure("HEAL_APPROVAL_REQUIRED", "A person must approve the repaired target before the step runs", path, step.id, { ...failure.detail ?? {}, originalCode: failure.code, heal: report }, true, report)
1445
+ };
1446
+ }
1447
+ this.healed.set(step.id, { target: candidate.proposedTarget, nodePath: candidate.nodePath, ...withProposal });
1448
+ try {
1449
+ const outcome = await this.attemptStep(step, path, attempt + 1, false);
1450
+ this.healedSteps.add(step.id);
1451
+ return { outcome, info: {} };
1452
+ } catch (error) {
1453
+ if (!(error instanceof StepFailure))
1454
+ throw error;
1455
+ this.healed.delete(step.id);
1456
+ return {
1457
+ failure: this.withHeal(error, {
1458
+ ...report,
1459
+ outcome: "check_failed",
1460
+ reason: "STEP_FAILED",
1461
+ stepFailureCode: error.code,
1462
+ attempted: true
1463
+ })
1464
+ };
1465
+ }
1466
+ }
1063
1467
  endIfStopAfter(path) {
1064
1468
  if (this.started && this.options.stopAfter === path)
1065
1469
  throw new StopSignal("rangeEnd");
@@ -1101,7 +1505,7 @@ class Interpreter {
1101
1505
  return outcome;
1102
1506
  }
1103
1507
  fail(step, path, failure) {
1104
- throw new StepFailure(failure.code, failure.message, path, step.id, failure.detail);
1508
+ throw new StepFailure(failure.code, failure.message, path, step.id, failure.detail, failure.primary === true);
1105
1509
  }
1106
1510
  evaluate(step, path, expression) {
1107
1511
  try {
@@ -1119,6 +1523,17 @@ class Interpreter {
1119
1523
  targetOf(ref) {
1120
1524
  return "element" in ref ? this.script.elements?.[ref.element]?.target ?? { methods: [] } : ref;
1121
1525
  }
1526
+ primaryRef(step) {
1527
+ return "target" in step ? step.target : undefined;
1528
+ }
1529
+ stepTarget(step, ref) {
1530
+ if (!this.inRepair && ref === this.primaryRef(step)) {
1531
+ const replaced = this.healed.get(step.id)?.target ?? this.overrides.get(step.id);
1532
+ if (replaced)
1533
+ return replaced;
1534
+ }
1535
+ return this.targetOf(ref);
1536
+ }
1122
1537
  waitFor(step) {
1123
1538
  const settings = this.script.settings;
1124
1539
  if (step.timeoutMs !== undefined) {
@@ -1160,7 +1575,7 @@ class Interpreter {
1160
1575
  }
1161
1576
  }
1162
1577
  async onTarget(step, path, attempt, ref, run, waitMs = this.waitFor(step)) {
1163
- const target = this.targetOf(ref);
1578
+ const target = this.stepTarget(step, ref);
1164
1579
  const context = this.context(step, path, attempt);
1165
1580
  const { result, waitedMs } = await this.poll(step, path, waitMs, () => run(target, context));
1166
1581
  if (!result.ok) {
@@ -1168,7 +1583,8 @@ class Interpreter {
1168
1583
  this.fail(step, path, {
1169
1584
  code,
1170
1585
  message: FAILURE_MESSAGES[code] ?? "The step failed",
1171
- detail: failureDetail(result, waitedMs)
1586
+ detail: failureDetail(result, waitedMs),
1587
+ primary: !this.inRepair && ref === this.primaryRef(step)
1172
1588
  });
1173
1589
  }
1174
1590
  return {
@@ -1494,7 +1910,7 @@ class Interpreter {
1494
1910
  outcome = await this.act(step, path, attempt, step.target, { kind: "type", text, append: step.clear === false });
1495
1911
  }
1496
1912
  if (step.pressEnter === true) {
1497
- const target = step.target ? this.targetOf(step.target) : null;
1913
+ const target = step.target ? this.stepTarget(step, step.target) : null;
1498
1914
  const context = this.context(step, path, attempt);
1499
1915
  const result = await this.call(step, path, () => this.driver.pressKeys(target, ["Enter"], context));
1500
1916
  if (!result.ok) {
@@ -1654,7 +2070,7 @@ class Interpreter {
1654
2070
  }
1655
2071
  if (this.driver.replay)
1656
2072
  return {};
1657
- const target = this.targetOf(step.target);
2073
+ const target = this.stepTarget(step, step.target);
1658
2074
  const resolveState = state === "hidden" ? "visible" : "attached";
1659
2075
  const gone = await this.waitUntil(step, path, this.waitFor(step), async (context) => {
1660
2076
  const resolution = await this.driver.resolve(target, { state: resolveState }, context);