@validate.qa/runner 1.0.3 → 1.0.5

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 (3) hide show
  1. package/dist/cli.js +2246 -763
  2. package/dist/cli.mjs +2256 -773
  3. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -615,12 +615,19 @@ var require_ai_provider = __commonJS({
615
615
  function getMobileAIClient() {
616
616
  if ((process.env.SERVER_URL ?? "").trim().length > 0)
617
617
  return getServerProxiedAIClient();
618
- return getAIClient();
618
+ if (isMobileAiUnitTestMode())
619
+ return getAIClient();
620
+ throw new Error("Mobile AI requires the platform server URL (SERVER_URL). Customer runners do not provision provider API keys \u2014 the platform proxies every mobile AI completion through /api/runner/ai/execute, which lives on the shared server with the keys. If you reached this from `validate-runner start`, the credentials file is missing `serverUrl` or the install is broken; re-run `npx @validate.qa/runner login`. If you are calling the runner-core library directly, pass `options.aiClient` in tests or set VALIDATEQA_TEST_MODE=1 to opt into the local-provider fallback.");
619
621
  }
620
622
  function getMobileClientForModel(modelId) {
621
623
  if ((process.env.SERVER_URL ?? "").trim().length > 0)
622
624
  return getServerProxiedClientForModel(modelId);
623
- return getClientForModel(modelId);
625
+ if (isMobileAiUnitTestMode())
626
+ return getClientForModel(modelId);
627
+ throw new Error(`Mobile AI for model "${modelId}" requires the platform server URL (SERVER_URL). Customer runners do not provision provider API keys \u2014 the platform proxies every mobile AI completion through /api/runner/ai/execute. See getMobileAIClient() for remediation steps.`);
628
+ }
629
+ function isMobileAiUnitTestMode() {
630
+ return (process.env.VALIDATEQA_TEST_MODE ?? "").trim() === "1" || (process.env.NODE_ENV ?? "").trim() === "test";
624
631
  }
625
632
  }
626
633
  });
@@ -4432,6 +4439,15 @@ var require_mobile_explorer = __commonJS({
4432
4439
  var MAX_RECENT_EXCHANGES = 24;
4433
4440
  var OBSERVATION_MAX_ELEMENTS = 50;
4434
4441
  var NO_SNAPSHOT_NUDGE_THRESHOLD = 4;
4442
+ var ANDROID_TRANSIENT_SYSTEM_APP_IDS = /* @__PURE__ */ new Set([
4443
+ "com.android.permissioncontroller",
4444
+ "com.google.android.permissioncontroller",
4445
+ "com.android.packageinstaller",
4446
+ "com.google.android.packageinstaller"
4447
+ ]);
4448
+ var IOS_TRANSIENT_SYSTEM_APP_IDS = /* @__PURE__ */ new Set([
4449
+ "com.apple.springboard"
4450
+ ]);
4435
4451
  var MOBILE_SNAPSHOT_TOOL = {
4436
4452
  type: "function",
4437
4453
  function: {
@@ -4654,6 +4670,40 @@ var require_mobile_explorer = __commonJS({
4654
4670
  return "timeout";
4655
4671
  return "ai_error";
4656
4672
  }
4673
+ function ownerFromScreenSignal(signal, targetAppId) {
4674
+ const bundleId = signal.bundleId?.trim();
4675
+ if (bundleId)
4676
+ return bundleId;
4677
+ const activity = signal.activity?.trim();
4678
+ if (activity && targetAppId && (activity === targetAppId || activity.startsWith(`${targetAppId}.`))) {
4679
+ return targetAppId;
4680
+ }
4681
+ return null;
4682
+ }
4683
+ function isTransientSystemOwner(owner, platform3) {
4684
+ return platform3 === "ANDROID" ? ANDROID_TRANSIENT_SYSTEM_APP_IDS.has(owner) : IOS_TRANSIENT_SYSTEM_APP_IDS.has(owner);
4685
+ }
4686
+ function classifyMobileScreenBoundary(signal, targetAppId, platform3) {
4687
+ if (!targetAppId)
4688
+ return { kind: "target" };
4689
+ const owner = ownerFromScreenSignal(signal, targetAppId);
4690
+ if (!owner)
4691
+ return { kind: "unknown" };
4692
+ if (owner === targetAppId)
4693
+ return { kind: "target" };
4694
+ if (isTransientSystemOwner(owner, platform3)) {
4695
+ return { kind: "transient_external", owner, targetAppId };
4696
+ }
4697
+ return { kind: "external", owner, targetAppId };
4698
+ }
4699
+ function boundaryNote(boundary) {
4700
+ return [
4701
+ "",
4702
+ "## App Boundary",
4703
+ `Foreground app is ${boundary.owner}, outside target app ${boundary.targetAppId}.`,
4704
+ boundary.kind === "transient_external" ? "This looks like a system overlay. Handle or dismiss it if needed, but do not capture it as an app screen." : "The runner excluded this external app screen from the survey evidence."
4705
+ ].join("\n");
4706
+ }
4657
4707
  function resolveSnapshot(xml, windowSize, driverSignal, platform3) {
4658
4708
  const parsed = (0, mobile_source_parser_js_1.parseMobileSource)(xml, platform3);
4659
4709
  const signal = {
@@ -4734,8 +4784,9 @@ var require_mobile_explorer = __commonJS({
4734
4784
  }
4735
4785
  return sections.join("\n");
4736
4786
  }
4737
- function buildSystemPrompt(mode, platform3, enableRecordJourney, maxIterations, appBrief) {
4787
+ function buildSystemPrompt(mode, platform3, enableRecordJourney, maxIterations, appBrief, targetAppId) {
4738
4788
  const platformLabel = platform3 === "IOS" ? "iOS" : "Android";
4789
+ const targetLine = targetAppId ? ` Target app package/bundle: ${targetAppId}.` : "";
4739
4790
  const surveyBody = `You are a mobile QA exploration agent driving a real ${platformLabel} app on a connected device. Your goal: MAP every reachable screen so downstream agents know what to test. You are NOT testing \u2014 you are building a complete screen map.
4740
4791
 
4741
4792
  ## How the loop works
@@ -4753,7 +4804,7 @@ var require_mobile_explorer = __commonJS({
4753
4804
 
4754
4805
  ## Boundaries (survey = map, do not mutate)
4755
4806
  - Do NOT submit forms, save, delete, purchase, or send anything. You MAY open modals/menus to catalog them, then dismiss.
4756
- - Stay inside this app. Do not leave to the system home screen or other apps (mobile_home backgrounds \u2014 avoid it).
4807
+ - Stay inside this app.${targetLine} Do not leave to the system home screen or other apps (mobile_home backgrounds \u2014 avoid it). If a tap opens an app store, settings, browser, or another app, do not explore it; return with mobile_back or mobile_relaunch.
4757
4808
  - If you hit a login wall and have no credentials, capture the auth screen and explore what is reachable.
4758
4809
 
4759
4810
  Max iterations: ${maxIterations}. You will be warned near the end.`;
@@ -4771,6 +4822,7 @@ Max iterations: ${maxIterations}. You will be warned near the end.`;
4771
4822
  4. Use mobile_relaunch for a clean, hermetic restart before testing a distinct flow that needs fresh state.
4772
4823
  5. mobile_swipe to scroll; mobile_back to return.
4773
4824
  6. Do NOT trigger irreversible side effects (real payments, sending messages/invites to others). Note them instead.
4825
+ 7. Stay inside this app.${targetLine} If a tap opens an app store, settings, browser, or another app, do not explore it; return with mobile_back or mobile_relaunch.
4774
4826
 
4775
4827
  Max iterations: ${maxIterations}. You will be warned near the end.`;
4776
4828
  const briefBlock = appBrief ? `
@@ -4796,6 +4848,7 @@ Use this to prioritize which screens and flows to cover.` : "";
4796
4848
  const mode = ctx.mode === "deep" ? "deep" : "survey";
4797
4849
  const platform3 = platformForMedium(ctx.medium);
4798
4850
  const enableRecordJourney = config?.enableRecordJourney ?? mode !== "survey";
4851
+ const targetAppId = ctx.target?.appId?.trim() || void 0;
4799
4852
  const defaultBudget = mode === "deep" ? MAX_MOBILE_ITERATIONS_DEEP : MAX_MOBILE_ITERATIONS_SURVEY;
4800
4853
  const maxIterations = config?.maxIterations && config.maxIterations > 0 ? Math.floor(config.maxIterations) : defaultBudget;
4801
4854
  const modelOverride = ctx.exploreModel ?? null;
@@ -4846,6 +4899,81 @@ Use this to prioritize which screens and flows to cover.` : "";
4846
4899
  return null;
4847
4900
  return resolveSnapshot(xml, windowSize, driverSignal, platform3);
4848
4901
  };
4902
+ const currentBoundary = () => {
4903
+ const current = getLatest();
4904
+ return current ? classifyMobileScreenBoundary(current.signal, targetAppId, platform3) : { kind: "unknown" };
4905
+ };
4906
+ const absorbResolvedObservation = async (resolved, screenshot, iterationForObservation, source) => {
4907
+ const boundary = classifyMobileScreenBoundary(resolved.signal, targetAppId, platform3);
4908
+ if (boundary.kind === "target" || boundary.kind === "unknown") {
4909
+ const screenId = ingestSnapshot(resolved, iterationForObservation);
4910
+ captureScreenshot(screenshot, screenId);
4911
+ return { observation: resolved.observation, screenId };
4912
+ }
4913
+ latest = resolved;
4914
+ lastSnapshotIteration = iterationForObservation;
4915
+ if (boundary.kind === "transient_external") {
4916
+ log2(` System overlay detected from ${source}: ${boundary.owner}; not adding it to target-app survey evidence.`);
4917
+ return {
4918
+ observation: `${resolved.observation}${boundaryNote(boundary)}`,
4919
+ screenId: null,
4920
+ transientExternal: true,
4921
+ externalOwner: boundary.owner
4922
+ };
4923
+ }
4924
+ log2(` External app detected from ${source}: ${boundary.owner}; excluding it from survey evidence and relaunching ${boundary.targetAppId}.`);
4925
+ try {
4926
+ await driver.relaunch();
4927
+ const snap = await driver.snapshot();
4928
+ const recovered = buildResolved(snap.xml, snap.windowSize, snap.screenSignal);
4929
+ if (!recovered) {
4930
+ return {
4931
+ observation: `(left target app ${boundary.targetAppId} for ${boundary.owner}; relaunched target app, but no UI tree was returned)`,
4932
+ screenId: null,
4933
+ externalBlocked: true,
4934
+ externalOwner: boundary.owner
4935
+ };
4936
+ }
4937
+ const recoveredBoundary = classifyMobileScreenBoundary(recovered.signal, targetAppId, platform3);
4938
+ latest = recovered;
4939
+ lastSnapshotIteration = iterationForObservation;
4940
+ if (recoveredBoundary.kind === "target" || recoveredBoundary.kind === "unknown") {
4941
+ const screenId = ingestSnapshot(recovered, iterationForObservation);
4942
+ captureScreenshot(snap.screenshot, screenId);
4943
+ return {
4944
+ observation: recovered.observation,
4945
+ screenId,
4946
+ externalBlocked: true,
4947
+ externalOwner: boundary.owner
4948
+ };
4949
+ }
4950
+ if (recoveredBoundary.kind === "transient_external") {
4951
+ log2(` Relaunch surfaced system overlay ${recoveredBoundary.owner}; not adding it to target-app survey evidence.`);
4952
+ return {
4953
+ observation: `${recovered.observation}${boundaryNote(recoveredBoundary)}`,
4954
+ screenId: null,
4955
+ externalBlocked: true,
4956
+ transientExternal: true,
4957
+ externalOwner: boundary.owner
4958
+ };
4959
+ }
4960
+ log2(` Relaunch still outside target app (${recoveredBoundary.owner}); no screen recorded.`);
4961
+ return {
4962
+ observation: `(left target app ${boundary.targetAppId} for ${boundary.owner}; relaunch still showed ${recoveredBoundary.owner}, so no screen was recorded)`,
4963
+ screenId: null,
4964
+ externalBlocked: true,
4965
+ externalOwner: boundary.owner
4966
+ };
4967
+ } catch (err) {
4968
+ const message = err instanceof Error ? err.message : String(err);
4969
+ return {
4970
+ observation: `(left target app ${boundary.targetAppId} for ${boundary.owner}; failed to relaunch target app: ${message})`,
4971
+ screenId: null,
4972
+ externalBlocked: true,
4973
+ externalOwner: boundary.owner
4974
+ };
4975
+ }
4976
+ };
4849
4977
  const resolveRef = (ref) => {
4850
4978
  if (typeof ref !== "string" || !latest)
4851
4979
  return null;
@@ -4854,7 +4982,7 @@ Use this to prioritize which screens and flows to cover.` : "";
4854
4982
  return null;
4855
4983
  return { element, bounds: element.bounds };
4856
4984
  };
4857
- const systemPrompt = buildSystemPrompt(mode, platform3, enableRecordJourney, maxIterations, ctx.appBrief);
4985
+ const systemPrompt = buildSystemPrompt(mode, platform3, enableRecordJourney, maxIterations, ctx.appBrief, targetAppId);
4858
4986
  const kickoff = buildKickoffMessage(mode);
4859
4987
  const recentExchanges = [];
4860
4988
  const transientDirectives = [];
@@ -4884,9 +5012,13 @@ Use this to prioritize which screens and flows to cover.` : "";
4884
5012
  const seed = await driver.snapshot();
4885
5013
  const resolved = buildResolved(seed.xml, seed.windowSize, seed.screenSignal);
4886
5014
  if (resolved) {
4887
- const screenId = ingestSnapshot(resolved, 0);
4888
- captureScreenshot(seed.screenshot, screenId);
4889
- log2(`Seed snapshot: screen ${screenId} (${resolved.screen.elements.length} elements)`);
5015
+ const absorbed = await absorbResolvedObservation(resolved, seed.screenshot, 0, "seed snapshot");
5016
+ if (absorbed.screenId) {
5017
+ const elementCount = state2.getScreen(absorbed.screenId)?.elements.length ?? resolved.screen.elements.length;
5018
+ log2(`Seed snapshot: screen ${absorbed.screenId} (${elementCount} elements)`);
5019
+ } else {
5020
+ log2("Seed snapshot was outside the target app and was not recorded.");
5021
+ }
4890
5022
  } else {
4891
5023
  log2("Seed snapshot returned no XML \u2014 the agent must call mobile_snapshot.");
4892
5024
  }
@@ -5027,9 +5159,9 @@ Exploration complete: ${summary}`);
5027
5159
  mode,
5028
5160
  latest: getLatest,
5029
5161
  buildResolved,
5030
- ingestSnapshot,
5162
+ absorbResolvedObservation,
5031
5163
  resolveRef,
5032
- captureScreenshot,
5164
+ currentBoundary,
5033
5165
  collectedTransitions,
5034
5166
  counters,
5035
5167
  journeys,
@@ -5064,9 +5196,21 @@ Exploration complete: ${summary}`);
5064
5196
  counters.pagesDiscovered = collectedPages.length;
5065
5197
  }
5066
5198
  log2(`Mobile explore phase done \u2014 ${collectedPages.length} screens, ${collectedTransitions.length} transitions, ${journeys.length} journeys, ${counters.elementsFound} elements observed.`);
5199
+ const recordedJourneys = journeys.map((journey) => {
5200
+ const routes = journey.screenIds && journey.screenIds.length > 0 ? journey.screenIds : [journey.screenId];
5201
+ return {
5202
+ name: journey.name,
5203
+ route: journey.screenId,
5204
+ routes,
5205
+ steps: journey.steps,
5206
+ expectedOutcome: journey.expectedOutcome,
5207
+ iteration: journey.iteration
5208
+ };
5209
+ });
5067
5210
  const result = {
5068
5211
  collectedPages,
5069
5212
  collectedTransitions,
5213
+ recordedJourneys,
5070
5214
  screenshots,
5071
5215
  exploreStats: {
5072
5216
  pagesDiscovered: counters.pagesDiscovered,
@@ -5087,15 +5231,13 @@ Exploration complete: ${summary}`);
5087
5231
  return result;
5088
5232
  }
5089
5233
  async function dispatchMobileTool(args) {
5090
- const { toolName, toolArgs, iteration, driver, state: state2, enableRecordJourney, latest, buildResolved, ingestSnapshot, resolveRef, captureScreenshot, collectedTransitions, counters, journeys, log: log2 } = args;
5091
- const absorbActionResult = (xml, screenshot) => {
5092
- const resolved = buildResolved(xml, latest()?.screen.windowSize ?? null, void 0);
5234
+ const { toolName, toolArgs, iteration, driver, state: state2, enableRecordJourney, latest, buildResolved, absorbResolvedObservation, resolveRef, currentBoundary, collectedTransitions, counters, journeys, log: log2 } = args;
5235
+ const absorbActionResult = (actionResult, source) => {
5236
+ const resolved = buildResolved(actionResult.source, latest()?.screen.windowSize ?? null, actionResult.screenSignal);
5093
5237
  if (!resolved) {
5094
- return { observation: "(no UI tree returned)", screenId: null };
5238
+ return Promise.resolve({ observation: "(no UI tree returned)", screenId: null });
5095
5239
  }
5096
- const screenId = ingestSnapshot(resolved, iteration);
5097
- captureScreenshot(screenshot, screenId);
5098
- return { observation: resolved.observation, screenId };
5240
+ return absorbResolvedObservation(resolved, actionResult.screenshot, iteration, source);
5099
5241
  };
5100
5242
  switch (toolName) {
5101
5243
  // ----- Observation -----
@@ -5105,12 +5247,16 @@ Exploration complete: ${summary}`);
5105
5247
  if (!resolved) {
5106
5248
  return { ok: false, error: "snapshot returned no UI tree" };
5107
5249
  }
5108
- const screenId = ingestSnapshot(resolved, iteration);
5109
- captureScreenshot(snap.screenshot, screenId);
5110
- log2(` Snapshot: screen ${screenId} (${resolved.screen.elements.length} elements)`);
5250
+ const absorbed = await absorbResolvedObservation(resolved, snap.screenshot, iteration, "mobile_snapshot");
5251
+ if (absorbed.screenId) {
5252
+ const elementCount = state2.getScreen(absorbed.screenId)?.elements.length ?? resolved.screen.elements.length;
5253
+ log2(` Snapshot: screen ${absorbed.screenId} (${elementCount} elements)`);
5254
+ }
5111
5255
  return {
5112
- screenId,
5113
- observation: resolved.observation,
5256
+ screenId: absorbed.screenId,
5257
+ observation: absorbed.observation,
5258
+ ...absorbed.externalBlocked ? { blockedExternalApp: true, externalApp: absorbed.externalOwner } : {},
5259
+ ...absorbed.transientExternal ? { transientExternalApp: true, externalApp: absorbed.externalOwner } : {},
5114
5260
  progress: `${state2.getScreenCount()} screens, ${collectedTransitions.length} transitions, ${journeys.length} journeys`
5115
5261
  };
5116
5262
  }
@@ -5121,14 +5267,21 @@ Exploration complete: ${summary}`);
5121
5267
  return { ok: false, error: `Unknown element ref "${String(toolArgs.ref)}". Call mobile_snapshot first; refs are only valid for the latest snapshot.` };
5122
5268
  }
5123
5269
  const fromScreenId = state2.getCurrentScreenId();
5270
+ const boundaryBeforeAction = currentBoundary();
5271
+ const startedOutsideTarget = boundaryBeforeAction.kind !== "target" && boundaryBeforeAction.kind !== "unknown";
5124
5272
  const actionResult = await driver.tap(resolved.bounds);
5125
- const { observation, screenId } = absorbActionResult(actionResult.source, actionResult.screenshot);
5126
- autoRecordTransition(state2, collectedTransitions, counters, fromScreenId, screenId, String(toolArgs.ref), "tap");
5273
+ const absorbed = await absorbActionResult(actionResult, "mobile_tap");
5274
+ const { observation, screenId } = absorbed;
5275
+ if (!startedOutsideTarget && !absorbed.externalBlocked && !absorbed.transientExternal) {
5276
+ autoRecordTransition(state2, collectedTransitions, counters, fromScreenId, screenId, String(toolArgs.ref), "tap");
5277
+ }
5127
5278
  return {
5128
5279
  ok: actionResult.ok,
5129
5280
  ...actionResult.error ? { error: actionResult.error } : {},
5130
5281
  screenId,
5131
- observation
5282
+ observation,
5283
+ ...absorbed.externalBlocked ? { blockedExternalApp: true, externalApp: absorbed.externalOwner } : {},
5284
+ ...absorbed.transientExternal ? { transientExternalApp: true, externalApp: absorbed.externalOwner } : {}
5132
5285
  };
5133
5286
  }
5134
5287
  // ----- Type -----
@@ -5139,12 +5292,15 @@ Exploration complete: ${summary}`);
5139
5292
  }
5140
5293
  const value = typeof toolArgs.value === "string" ? toolArgs.value : String(toolArgs.value ?? "");
5141
5294
  const actionResult = await driver.type(value, resolved.bounds);
5142
- const { observation, screenId } = absorbActionResult(actionResult.source, actionResult.screenshot);
5295
+ const absorbed = await absorbActionResult(actionResult, "mobile_type");
5296
+ const { observation, screenId } = absorbed;
5143
5297
  return {
5144
5298
  ok: actionResult.ok,
5145
5299
  ...actionResult.error ? { error: actionResult.error } : {},
5146
5300
  screenId,
5147
- observation
5301
+ observation,
5302
+ ...absorbed.externalBlocked ? { blockedExternalApp: true, externalApp: absorbed.externalOwner } : {},
5303
+ ...absorbed.transientExternal ? { transientExternalApp: true, externalApp: absorbed.externalOwner } : {}
5148
5304
  };
5149
5305
  }
5150
5306
  // ----- Clear -----
@@ -5154,12 +5310,15 @@ Exploration complete: ${summary}`);
5154
5310
  return { ok: false, error: `Unknown element ref "${String(toolArgs.ref)}". Call mobile_snapshot first.` };
5155
5311
  }
5156
5312
  const actionResult = await driver.clear(resolved.bounds);
5157
- const { observation, screenId } = absorbActionResult(actionResult.source, actionResult.screenshot);
5313
+ const absorbed = await absorbActionResult(actionResult, "mobile_clear");
5314
+ const { observation, screenId } = absorbed;
5158
5315
  return {
5159
5316
  ok: actionResult.ok,
5160
5317
  ...actionResult.error ? { error: actionResult.error } : {},
5161
5318
  screenId,
5162
- observation
5319
+ observation,
5320
+ ...absorbed.externalBlocked ? { blockedExternalApp: true, externalApp: absorbed.externalOwner } : {},
5321
+ ...absorbed.transientExternal ? { transientExternalApp: true, externalApp: absorbed.externalOwner } : {}
5163
5322
  };
5164
5323
  }
5165
5324
  // ----- Swipe -----
@@ -5170,36 +5329,49 @@ Exploration complete: ${summary}`);
5170
5329
  }
5171
5330
  const distance = typeof toolArgs.distance === "number" ? toolArgs.distance : void 0;
5172
5331
  const actionResult = await driver.swipe(direction, distance);
5173
- const { observation, screenId } = absorbActionResult(actionResult.source, actionResult.screenshot);
5332
+ const absorbed = await absorbActionResult(actionResult, "mobile_swipe");
5333
+ const { observation, screenId } = absorbed;
5174
5334
  return {
5175
5335
  ok: actionResult.ok,
5176
5336
  ...actionResult.error ? { error: actionResult.error } : {},
5177
5337
  screenId,
5178
- observation
5338
+ observation,
5339
+ ...absorbed.externalBlocked ? { blockedExternalApp: true, externalApp: absorbed.externalOwner } : {},
5340
+ ...absorbed.transientExternal ? { transientExternalApp: true, externalApp: absorbed.externalOwner } : {}
5179
5341
  };
5180
5342
  }
5181
5343
  // ----- Back -----
5182
5344
  case "mobile_back": {
5183
5345
  const fromScreenId = state2.getCurrentScreenId();
5346
+ const boundaryBeforeAction = currentBoundary();
5347
+ const startedOutsideTarget = boundaryBeforeAction.kind !== "target" && boundaryBeforeAction.kind !== "unknown";
5184
5348
  const actionResult = await driver.back();
5185
- const { observation, screenId } = absorbActionResult(actionResult.source, actionResult.screenshot);
5186
- autoRecordTransition(state2, collectedTransitions, counters, fromScreenId, screenId, void 0, "back");
5349
+ const absorbed = await absorbActionResult(actionResult, "mobile_back");
5350
+ const { observation, screenId } = absorbed;
5351
+ if (!startedOutsideTarget && !absorbed.externalBlocked && !absorbed.transientExternal) {
5352
+ autoRecordTransition(state2, collectedTransitions, counters, fromScreenId, screenId, void 0, "back");
5353
+ }
5187
5354
  return {
5188
5355
  ok: actionResult.ok,
5189
5356
  ...actionResult.error ? { error: actionResult.error } : {},
5190
5357
  screenId,
5191
- observation
5358
+ observation,
5359
+ ...absorbed.externalBlocked ? { blockedExternalApp: true, externalApp: absorbed.externalOwner } : {},
5360
+ ...absorbed.transientExternal ? { transientExternalApp: true, externalApp: absorbed.externalOwner } : {}
5192
5361
  };
5193
5362
  }
5194
5363
  // ----- Home (backgrounds only) -----
5195
5364
  case "mobile_home": {
5196
5365
  const actionResult = await driver.home();
5197
- const { observation, screenId } = absorbActionResult(actionResult.source, actionResult.screenshot);
5366
+ const absorbed = await absorbActionResult(actionResult, "mobile_home");
5367
+ const { observation, screenId } = absorbed;
5198
5368
  return {
5199
5369
  ok: actionResult.ok,
5200
5370
  ...actionResult.error ? { error: actionResult.error } : {},
5201
5371
  screenId,
5202
5372
  observation,
5373
+ ...absorbed.externalBlocked ? { blockedExternalApp: true, externalApp: absorbed.externalOwner } : {},
5374
+ ...absorbed.transientExternal ? { transientExternalApp: true, externalApp: absorbed.externalOwner } : {},
5203
5375
  note: "Home only backgrounds the app. Use mobile_relaunch for a clean restart."
5204
5376
  };
5205
5377
  }
@@ -5212,9 +5384,15 @@ Exploration complete: ${summary}`);
5212
5384
  if (!resolved) {
5213
5385
  return { ok: true, note: "relaunched; no UI tree returned \u2014 call mobile_snapshot." };
5214
5386
  }
5215
- const screenId = ingestSnapshot(resolved, iteration);
5216
- captureScreenshot(snap.screenshot, screenId);
5217
- return { ok: true, screenId, observation: resolved.observation, note: "App relaunched (clean state)." };
5387
+ const absorbed = await absorbResolvedObservation(resolved, snap.screenshot, iteration, "mobile_relaunch");
5388
+ return {
5389
+ ok: true,
5390
+ screenId: absorbed.screenId,
5391
+ observation: absorbed.observation,
5392
+ ...absorbed.externalBlocked ? { blockedExternalApp: true, externalApp: absorbed.externalOwner } : {},
5393
+ ...absorbed.transientExternal ? { transientExternalApp: true, externalApp: absorbed.externalOwner } : {},
5394
+ note: "App relaunched (clean state)."
5395
+ };
5218
5396
  }
5219
5397
  // ----- Deep-link (guarded) -----
5220
5398
  case "mobile_open_deeplink": {
@@ -5223,18 +5401,32 @@ Exploration complete: ${summary}`);
5223
5401
  return { ok: false, error: `Refused to open deep link "${deeplink}" (empty or dangerous scheme).` };
5224
5402
  }
5225
5403
  const fromScreenId = state2.getCurrentScreenId();
5404
+ const boundaryBeforeAction = currentBoundary();
5405
+ const startedOutsideTarget = boundaryBeforeAction.kind !== "target" && boundaryBeforeAction.kind !== "unknown";
5226
5406
  const actionResult = await driver.openDeepLink(deeplink);
5227
- const { observation, screenId } = absorbActionResult(actionResult.source, actionResult.screenshot);
5228
- autoRecordTransition(state2, collectedTransitions, counters, fromScreenId, screenId, void 0, "deeplink");
5407
+ const absorbed = await absorbActionResult(actionResult, "mobile_open_deeplink");
5408
+ const { observation, screenId } = absorbed;
5409
+ if (!startedOutsideTarget && !absorbed.externalBlocked && !absorbed.transientExternal) {
5410
+ autoRecordTransition(state2, collectedTransitions, counters, fromScreenId, screenId, void 0, "deeplink");
5411
+ }
5229
5412
  return {
5230
5413
  ok: actionResult.ok,
5231
5414
  ...actionResult.error ? { error: actionResult.error } : {},
5232
5415
  screenId,
5233
- observation
5416
+ observation,
5417
+ ...absorbed.externalBlocked ? { blockedExternalApp: true, externalApp: absorbed.externalOwner } : {},
5418
+ ...absorbed.transientExternal ? { transientExternalApp: true, externalApp: absorbed.externalOwner } : {}
5234
5419
  };
5235
5420
  }
5236
5421
  // ----- capture_screen (mark + annotate current screen) -----
5237
5422
  case "capture_screen": {
5423
+ const boundary = currentBoundary();
5424
+ if (boundary.kind === "external" || boundary.kind === "transient_external") {
5425
+ return {
5426
+ ok: false,
5427
+ error: `Current foreground app is ${boundary.owner}, outside target app ${boundary.targetAppId}. It was not captured. Return to the target app with mobile_back or mobile_relaunch.`
5428
+ };
5429
+ }
5238
5430
  const screenId = state2.getCurrentScreenId();
5239
5431
  if (!screenId) {
5240
5432
  return { ok: false, error: "No current screen \u2014 call mobile_snapshot first." };
@@ -5285,6 +5477,13 @@ Exploration complete: ${summary}`);
5285
5477
  if (!enableRecordJourney) {
5286
5478
  return { ok: false, error: "record_journey is not available in survey mode." };
5287
5479
  }
5480
+ const boundary = currentBoundary();
5481
+ if (boundary.kind === "external" || boundary.kind === "transient_external") {
5482
+ return {
5483
+ ok: false,
5484
+ error: `Current foreground app is ${boundary.owner}, outside target app ${boundary.targetAppId}. External app screens cannot be recorded as a target-app journey.`
5485
+ };
5486
+ }
5288
5487
  const name = typeof toolArgs.name === "string" ? toolArgs.name : "";
5289
5488
  const screenId = typeof toolArgs.screenId === "string" && toolArgs.screenId ? toolArgs.screenId : state2.getCurrentScreenId() ?? "";
5290
5489
  const steps = Array.isArray(toolArgs.steps) ? toolArgs.steps.filter((s) => typeof s === "string") : [];
@@ -7075,7 +7274,7 @@ Each entry is exactly one of:
7075
7274
  const existingNamesLower = new Set(existingTestNames.map((n) => n.toLowerCase()));
7076
7275
  const usedNames = /* @__PURE__ */ new Set();
7077
7276
  const validTypes = /* @__PURE__ */ new Set(["HAPPY_PATH", "EDGE_CASE", "USER_JOURNEY", "REGRESSION", "BUG_REPORT"]);
7078
- const validOutcomeTypes = /* @__PURE__ */ new Set(["url", "element-visible", "text-visible", "value-match", "element-hidden"]);
7277
+ const validOutcomeTypes = /* @__PURE__ */ new Set(["url", "screen-visible", "element-visible", "text-visible", "value-match", "element-hidden"]);
7079
7278
  const validVariants = /* @__PURE__ */ new Set(["happy", "validation", "bug"]);
7080
7279
  const validCriticalities = /* @__PURE__ */ new Set(["critical", "high", "medium", "low"]);
7081
7280
  const inferOutcomeTypeFromDescription = (description) => /\b(?:heading|button|link|menu\s*item|menuitem|tab|card|modal|dialog|sidebar(?:\s+item)?|nav\s+item|navigation\s+link|form\s+label|field\s+label|avatar|banner)\b/i.test(description) ? "element-visible" : "text-visible";
@@ -7549,10 +7748,10 @@ Each entry is exactly one of:
7549
7748
  const agentModel = options.modelOverride ? (0, ai_provider_js_1.resolveModelForTask)(options.modelOverride, "agent") : (0, ai_provider_js_1.getModel)("agent");
7550
7749
  const plannerMaxTokens = getAIPlannerMaxTokens(agentModel);
7551
7750
  logs2.push(`AI planner [${context.mode}]: ${siteMap.pages.length} pages, ${evidence.recordedJourneys.length} journeys, budget ${context.testBudget}`);
7552
- const systemPrompt = context.mode === "survey" ? SURVEY_SYSTEM_PROMPT : exports2.SCENARIO_SYSTEM_PROMPT;
7553
- const userPrompt = formatEvidenceForPlanner(compressed, evidence, context);
7751
+ const systemPrompt = options.systemPromptOverride ?? (context.mode === "survey" ? SURVEY_SYSTEM_PROMPT : exports2.SCENARIO_SYSTEM_PROMPT);
7752
+ const userPrompt = options.userPromptOverride ?? formatEvidenceForPlanner(compressed, evidence, context);
7554
7753
  logs2.push(`AI planner: ~${Math.ceil(userPrompt.length / 3.5)} input tokens`);
7555
- const aiClient = options.modelOverride ? (0, ai_provider_js_1.getClientForModel)(options.modelOverride) : (0, ai_provider_js_1.getAIClient)();
7754
+ const aiClient = options.aiClient ?? (options.modelOverride ? (0, ai_provider_js_1.getClientForModel)(options.modelOverride) : (0, ai_provider_js_1.getAIClient)());
7556
7755
  const messages = [
7557
7756
  { role: "system", content: systemPrompt },
7558
7757
  { role: "user", content: userPrompt }
@@ -7764,7 +7963,16 @@ var require_mobile_discovery_prompts = __commonJS({
7764
7963
  exports2.formatMobileEvidenceForPlanner = formatMobileEvidenceForPlanner;
7765
7964
  exports2.buildMobileSiteMap = buildMobileSiteMap;
7766
7965
  exports2.runMobileAIPlanner = runMobileAIPlanner;
7966
+ var ai_provider_js_1 = require_ai_provider();
7767
7967
  var discover_ai_planner_js_1 = require_discover_ai_planner();
7968
+ function isTargetAppScreenId(screenId, appId) {
7969
+ if (!appId)
7970
+ return true;
7971
+ return screenId === appId || screenId.startsWith(`${appId}#`) || screenId.startsWith(`${appId}.`);
7972
+ }
7973
+ function targetAppScreens(graph, appId) {
7974
+ return [...graph.screens.values()].filter((state2) => isTargetAppScreenId(state2.screenId, appId));
7975
+ }
7768
7976
  exports2.MOBILE_SURVEY_SYSTEM_PROMPT = `You are a feature analyst for a native mobile application (iOS or Android). You receive evidence from an AI explorer that mapped the app's screens by tapping, typing, and swiping through the live UI. Your job is to organize screens into meaningful feature groups.
7769
7977
 
7770
7978
  ## Output Discipline
@@ -7994,7 +8202,8 @@ ${targetBits.length > 0 ? targetBits.join(" ") : "native mobile app"}`);
7994
8202
  if (context.featureName) {
7995
8203
  sections.push(`## Feature: ${context.featureName}`);
7996
8204
  }
7997
- const orderedScreens = [...graph.screens.values()].sort((a, b) => a.firstSeenIteration - b.firstSeenIteration);
8205
+ const orderedScreens = targetAppScreens(graph, context.appId).sort((a, b) => a.firstSeenIteration - b.firstSeenIteration);
8206
+ const includedScreenIds = new Set(orderedScreens.map((state2) => state2.screenId));
7998
8207
  sections.push(`## Screens Discovered (${orderedScreens.length})`);
7999
8208
  for (const state2 of orderedScreens) {
8000
8209
  const name = screenDisplayName(state2);
@@ -8016,7 +8225,7 @@ ${targetBits.length > 0 ? targetBits.join(" ") : "native mobile app"}`);
8016
8225
  for (const state2 of orderedScreens)
8017
8226
  nameById.set(state2.screenId, screenDisplayName(state2));
8018
8227
  const lines = ["## Observed Screen Transitions"];
8019
- for (const t of graph.transitions.slice(0, MAX_TRANSITIONS)) {
8228
+ for (const t of graph.transitions.filter((transition) => includedScreenIds.has(transition.fromScreenId) && includedScreenIds.has(transition.toScreenId)).slice(0, MAX_TRANSITIONS)) {
8020
8229
  const from = nameById.get(t.fromScreenId) ?? t.fromScreenId;
8021
8230
  const to = nameById.get(t.toScreenId) ?? t.toScreenId;
8022
8231
  const via = t.viaRef ? ` via ${t.viaRef}` : "";
@@ -8102,7 +8311,7 @@ ${constraints.join("\n")}`);
8102
8311
  const now = /* @__PURE__ */ new Date();
8103
8312
  const idByScreen = /* @__PURE__ */ new Map();
8104
8313
  let pageSeq = 0;
8105
- const orderedScreens = [...graph.screens.values()].sort((a, b) => a.firstSeenIteration - b.firstSeenIteration);
8314
+ const orderedScreens = targetAppScreens(graph, meta.appId).sort((a, b) => a.firstSeenIteration - b.firstSeenIteration);
8106
8315
  for (const state2 of orderedScreens) {
8107
8316
  idByScreen.set(state2.screenId, `mpage-${pageSeq++}`);
8108
8317
  }
@@ -8161,17 +8370,149 @@ ${constraints.join("\n")}`);
8161
8370
  seedFixtures: []
8162
8371
  };
8163
8372
  }
8373
+ function buildMobileFeatureGroupsFromGraph(graph, appId) {
8374
+ const groups = /* @__PURE__ */ new Map();
8375
+ const orderedScreens = targetAppScreens(graph, appId).filter((state2) => state2.screenType?.toLowerCase() !== "error").sort((a, b) => a.firstSeenIteration - b.firstSeenIteration);
8376
+ const includedScreenIds = new Set(orderedScreens.map((state2) => state2.screenId));
8377
+ for (const state2 of orderedScreens) {
8378
+ const groupName = state2.screenType === "auth" ? "Authentication" : screenDisplayName(state2);
8379
+ const group = groups.get(groupName) ?? {
8380
+ name: groupName,
8381
+ description: `Native mobile screens related to ${groupName}.`,
8382
+ routes: [],
8383
+ criticality: state2.screenType === "auth" ? "critical" : "medium",
8384
+ pages: [],
8385
+ flows: []
8386
+ };
8387
+ group.routes.push(state2.screenId);
8388
+ group.pages?.push({
8389
+ route: state2.screenId,
8390
+ type: state2.screenType ?? "UNKNOWN",
8391
+ summary: `${state2.elements.filter((el) => el.actionable).length} actionable element(s)`
8392
+ });
8393
+ groups.set(groupName, group);
8394
+ }
8395
+ const nameById = /* @__PURE__ */ new Map();
8396
+ for (const state2 of graph.screens.values()) {
8397
+ nameById.set(state2.screenId, screenDisplayName(state2));
8398
+ }
8399
+ for (const transition of graph.transitions) {
8400
+ if (!includedScreenIds.has(transition.fromScreenId) || !includedScreenIds.has(transition.toScreenId))
8401
+ continue;
8402
+ const from = graph.screens.get(transition.fromScreenId);
8403
+ const to = graph.screens.get(transition.toScreenId);
8404
+ if (!from || !to)
8405
+ continue;
8406
+ const groupName = from.screenType === "auth" ? "Authentication" : screenDisplayName(from);
8407
+ const group = groups.get(groupName);
8408
+ if (!group)
8409
+ continue;
8410
+ const via = transition.viaRef ? ` via ${transition.viaRef}` : "";
8411
+ group.flows?.push(`${nameById.get(transition.fromScreenId) ?? transition.fromScreenId} \u2192 ${nameById.get(transition.toScreenId) ?? transition.toScreenId} (${transition.action}${via})`);
8412
+ }
8413
+ return [...groups.values()];
8414
+ }
8415
+ function buildMobileFallbackScenarios(input) {
8416
+ if (input.mode === "survey")
8417
+ return [];
8418
+ const orderedScreens = targetAppScreens(input.graph, input.appId).filter((state2) => state2.screenType?.toLowerCase() !== "error").filter((state2) => state2.visited || state2.firstSeenIteration === 0).sort((a, b) => a.firstSeenIteration - b.firstSeenIteration);
8419
+ if (orderedScreens.length === 0)
8420
+ return [];
8421
+ const entryScreenId = orderedScreens[0].screenId;
8422
+ const usedNames = new Set((input.existingTestNames ?? []).map((name) => name.toLowerCase()));
8423
+ const scenarios = [];
8424
+ const budget = Math.max(1, input.testBudget || orderedScreens.length);
8425
+ for (const state2 of orderedScreens) {
8426
+ if (scenarios.length >= budget)
8427
+ break;
8428
+ const screenName = screenDisplayName(state2);
8429
+ const baseName = `${screenName} - Screen Reachability`;
8430
+ const name = usedNames.has(baseName.toLowerCase()) ? `${baseName} ${state2.firstSeenIteration + 1}` : baseName;
8431
+ if (usedNames.has(name.toLowerCase()))
8432
+ continue;
8433
+ usedNames.add(name.toLowerCase());
8434
+ const actionableLabels = state2.elements.filter((el) => el.actionable).map((el) => el.label?.trim() || el.text?.trim() || el.accessibilityId || shortResourceId(el.resourceId ?? "") || compactRole(el.role)).filter((label) => Boolean(label)).slice(0, 3);
8435
+ const steps = [
8436
+ { action: "Relaunch the app from a clean state", target: entryScreenId },
8437
+ {
8438
+ action: state2.screenId === entryScreenId ? `Verify the launch screen is ${screenName}` : `Navigate through the app until the ${screenName} screen is visible`,
8439
+ target: state2.screenId
8440
+ }
8441
+ ];
8442
+ if (actionableLabels.length > 0) {
8443
+ steps.push({
8444
+ action: `Verify expected native controls are available: ${actionableLabels.join(", ")}`,
8445
+ target: state2.screenId
8446
+ });
8447
+ }
8448
+ scenarios.push({
8449
+ name,
8450
+ type: "HAPPY_PATH",
8451
+ priority: state2.screenType === "auth" ? 1 : 3,
8452
+ variant: "happy",
8453
+ featureGroup: state2.screenType === "auth" ? "Authentication" : screenName,
8454
+ goal: `User can reach and inspect the ${screenName} screen`,
8455
+ steps,
8456
+ expectedOutcomes: [{
8457
+ type: "screen-visible",
8458
+ description: `${screenName} screen is visible`,
8459
+ reliability: "medium"
8460
+ }],
8461
+ entryRoute: entryScreenId,
8462
+ startFromLanding: true,
8463
+ targetPages: [state2.screenId],
8464
+ evidence: { observedLocators: [], observedTransitions: [] },
8465
+ prerequisites: []
8466
+ });
8467
+ }
8468
+ return scenarios;
8469
+ }
8470
+ function filterRecordedJourneysToSiteMap(journeys, siteMap) {
8471
+ if (!journeys?.length)
8472
+ return [];
8473
+ const validRoutes = new Set(siteMap.pages.map((page) => page.route));
8474
+ const filtered = [];
8475
+ for (const journey of journeys) {
8476
+ const routes = (journey.routes?.length ? journey.routes : [journey.route]).filter((route2) => validRoutes.has(route2));
8477
+ if (routes.length === 0)
8478
+ continue;
8479
+ filtered.push({
8480
+ ...journey,
8481
+ route: validRoutes.has(journey.route) ? journey.route : routes[0],
8482
+ routes
8483
+ });
8484
+ }
8485
+ return filtered;
8486
+ }
8487
+ function filterFeatureGroupsToSiteMap(groups, siteMap) {
8488
+ if (groups.length === 0)
8489
+ return [];
8490
+ const validRoutes = new Set(siteMap.pages.map((page) => page.route));
8491
+ return groups.flatMap((group) => {
8492
+ const routes = group.routes.filter((route2) => validRoutes.has(route2));
8493
+ if (routes.length === 0)
8494
+ return [];
8495
+ return [{
8496
+ ...group,
8497
+ routes,
8498
+ pages: group.pages?.filter((page) => validRoutes.has(page.route))
8499
+ }];
8500
+ });
8501
+ }
8164
8502
  async function runMobileAIPlanner(input, options = {}) {
8165
8503
  const planner = options.planner ?? discover_ai_planner_js_1.runAIPlanner;
8504
+ const shouldBuildMobileAIClient = (process.env.SERVER_URL ?? "").trim().length > 0 || !options.planner;
8505
+ const aiClient = options.aiClient ?? (shouldBuildMobileAIClient ? options.modelOverride ? (0, ai_provider_js_1.getMobileClientForModel)(options.modelOverride) : (0, ai_provider_js_1.getMobileAIClient)() : void 0);
8166
8506
  const siteMap = buildMobileSiteMap(input.graph, {
8167
8507
  projectId: input.projectId,
8168
8508
  appId: input.appId
8169
8509
  });
8510
+ const recordedJourneys = filterRecordedJourneysToSiteMap(input.recordedJourneys, siteMap);
8170
8511
  const collectedPages = input.collectedPages ?? [];
8171
8512
  const collectedTransitions = input.collectedTransitions ?? [];
8172
- const visitedRoutes = [...input.graph.screens.values()].filter((s) => s.visited).map((s) => s.screenId);
8173
- return planner(siteMap, {
8174
- recordedJourneys: input.recordedJourneys ?? [],
8513
+ const visitedRoutes = targetAppScreens(input.graph, input.appId).filter((s) => s.visited).map((s) => s.screenId);
8514
+ const result = await planner(siteMap, {
8515
+ recordedJourneys,
8175
8516
  collectedPages,
8176
8517
  collectedTransitions,
8177
8518
  visitedRoutes
@@ -8188,8 +8529,53 @@ ${constraints.join("\n")}`);
8188
8529
  testBudget: input.testBudget
8189
8530
  }, {
8190
8531
  modelOverride: options.modelOverride,
8191
- onAICall: options.onAICall
8532
+ onAICall: options.onAICall,
8533
+ ...aiClient ? { aiClient } : {},
8534
+ systemPromptOverride: input.mode === "survey" ? exports2.MOBILE_SURVEY_SYSTEM_PROMPT : exports2.MOBILE_SCENARIO_SYSTEM_PROMPT,
8535
+ userPromptOverride: formatMobileEvidenceForPlanner(input.graph, {
8536
+ appBrief: input.appBrief,
8537
+ appId: input.appId,
8538
+ platform: input.platform,
8539
+ featureName: input.featureName,
8540
+ existingTestNames: input.existingTestNames,
8541
+ existingFeatureNames: input.existingFeatureNames,
8542
+ recordedJourneys,
8543
+ credentials: input.credentials,
8544
+ testBudget: input.testBudget,
8545
+ incremental: input.incremental
8546
+ })
8192
8547
  });
8548
+ const plannerFeatureGroups = filterFeatureGroupsToSiteMap(result.featureGroups, siteMap);
8549
+ const mobileFeatureGroups = plannerFeatureGroups.length > 0 ? plannerFeatureGroups : buildMobileFeatureGroupsFromGraph(input.graph, input.appId);
8550
+ if (input.mode !== "survey" && result.scenarios.length === 0 && input.graph.screens.size > 0) {
8551
+ const fallbackScenarios = buildMobileFallbackScenarios(input);
8552
+ if (fallbackScenarios.length > 0) {
8553
+ return {
8554
+ ...result,
8555
+ scenarios: fallbackScenarios,
8556
+ featureGroups: mobileFeatureGroups,
8557
+ logs: [
8558
+ ...result.logs,
8559
+ `Mobile fallback: synthesized ${fallbackScenarios.length} screen-reachability scenario(s) from the native screen graph`
8560
+ ],
8561
+ fallbackUsed: true,
8562
+ fallbackReason: result.fallbackReason ?? "Mobile planner produced no scenarios"
8563
+ };
8564
+ }
8565
+ }
8566
+ if (input.mode === "survey" && result.featureGroups.length === 0 && mobileFeatureGroups.length > 0) {
8567
+ return {
8568
+ ...result,
8569
+ featureGroups: mobileFeatureGroups,
8570
+ logs: [
8571
+ ...result.logs,
8572
+ `Mobile fallback: synthesized ${mobileFeatureGroups.length} feature group(s) from the native screen graph`
8573
+ ],
8574
+ fallbackUsed: true,
8575
+ fallbackReason: result.fallbackReason ?? "Mobile planner produced no feature groups"
8576
+ };
8577
+ }
8578
+ return { ...result, featureGroups: mobileFeatureGroups };
8193
8579
  }
8194
8580
  }
8195
8581
  });
@@ -8915,13 +9301,17 @@ ${statePacket}` },
8915
9301
  log2(` scenario "${scenario.name}" produced no compilable test \u2014 skipped.`);
8916
9302
  continue;
8917
9303
  }
8918
- tests.push(test);
8919
9304
  log2(` \u2713 generated "${test.name}" (verified: ${test.verified}, steps: ${test.steps?.length ?? 0})`);
8920
9305
  try {
8921
- onTestGenerated?.(test);
9306
+ await onTestGenerated?.(test);
8922
9307
  } catch (cbErr) {
9308
+ if (cbErr?.isPlanLimitReached) {
9309
+ log2(" Plan limit reached \u2014 stopping further generation");
9310
+ break;
9311
+ }
8923
9312
  log2(` onTestGenerated callback error: ${cbErr instanceof Error ? cbErr.message : String(cbErr)}`);
8924
9313
  }
9314
+ tests.push(test);
8925
9315
  } catch (err) {
8926
9316
  log2(` scenario "${scenario.name}" failed: ${err instanceof Error ? err.message : String(err)} \u2014 skipping.`);
8927
9317
  continue;
@@ -12602,6 +12992,17 @@ var require_snapshot_observation = __commonJS({
12602
12992
  for (const container of group.containers) {
12603
12993
  lines.push(` - ${yamlScalar(container ?? "(none)")}`);
12604
12994
  }
12995
+ const hasRowContext = group.rowContexts.some((rc) => !!rc);
12996
+ if (hasRowContext) {
12997
+ lines.push(' distinguish_by_row: # same name in different rows \u2014 scope by this text, e.g. getByRole(role, { name }).filter({ has: getByText("<row>") })');
12998
+ for (let i = 0; i < group.rowContexts.length; i++) {
12999
+ const rowContext = group.rowContexts[i];
13000
+ if (!rowContext)
13001
+ continue;
13002
+ const ref = group.refs[i];
13003
+ lines.push(` - ${yamlScalar(`${ref ? `[${ref}] ` : ""}${rowContext}`)}`);
13004
+ }
13005
+ }
12605
13006
  }
12606
13007
  }
12607
13008
  lines.push("interactive_elements:");
@@ -12633,6 +13034,29 @@ var require_snapshot_observation = __commonJS({
12633
13034
  if (topLocator) {
12634
13035
  lines.push(` locator: ${yamlScalar((0, snapshot_parser_js_1.formatLocator)(topLocator))}`);
12635
13036
  }
13037
+ const enr = rankedElement.element.enrichment;
13038
+ if (enr?.rowContext) {
13039
+ lines.push(` row_context: ${yamlScalar(enr.rowContext)}`);
13040
+ }
13041
+ if (enr && (enr.occluded || enr.position && enr.position !== "in-view")) {
13042
+ const vis = [];
13043
+ if (enr.position && enr.position !== "in-view")
13044
+ vis.push(`off-screen-${enr.position}`);
13045
+ if (enr.occluded)
13046
+ vis.push("occluded");
13047
+ lines.push(` visibility: [${vis.map((token) => yamlScalar(token)).join(", ")}]`);
13048
+ }
13049
+ }
13050
+ }
13051
+ const recoveredClickables = options.recoveredClickables ?? [];
13052
+ if (recoveredClickables.length > 0) {
13053
+ lines.push("non_semantic_clickables:");
13054
+ lines.push(' # Clickable elements with NO accessibility role (e.g. <div onclick> / cursor:pointer) that the a11y snapshot drops. Some may be decorative \u2014 verify before relying on them, and prefer a scoped locator (e.g. page.getByRole("main").getByText("...")) over a bare getByText to avoid strict-mode ambiguity.');
13055
+ for (const clickable of recoveredClickables.slice(0, 8)) {
13056
+ lines.push(` - text: ${yamlScalar(clickable.text)}`);
13057
+ if (clickable.rowContext) {
13058
+ lines.push(` row_context: ${yamlScalar(clickable.rowContext)}`);
13059
+ }
12636
13060
  }
12637
13061
  }
12638
13062
  lines.push("observation_cues:");
@@ -12806,6 +13230,9 @@ var require_snapshot_observation = __commonJS({
12806
13230
  score += 12;
12807
13231
  }
12808
13232
  }
13233
+ const enr = element.enrichment;
13234
+ if (enr?.rowContext)
13235
+ score += 4;
12809
13236
  return score;
12810
13237
  }
12811
13238
  function buildStateSummary(element) {
@@ -12981,10 +13408,12 @@ var require_snapshot_observation = __commonJS({
12981
13408
  const key = `${role}\0${name}`;
12982
13409
  let group = groups.get(key);
12983
13410
  if (!group) {
12984
- group = { role, name, containers: [] };
13411
+ group = { role, name, containers: [], rowContexts: [], refs: [] };
12985
13412
  groups.set(key, group);
12986
13413
  }
12987
13414
  group.containers.push(element.containerPath ?? null);
13415
+ group.rowContexts.push(element.enrichment?.rowContext ?? null);
13416
+ group.refs.push(element.ref ?? null);
12988
13417
  }
12989
13418
  return [...groups.values()].filter((group) => group.containers.length > 1);
12990
13419
  }
@@ -234638,6 +235067,154 @@ var require_preflight_syntax = __commonJS({
234638
235067
  }
234639
235068
  });
234640
235069
 
235070
+ // ../runner-core/dist/services/selector-registry-context.js
235071
+ var require_selector_registry_context = __commonJS({
235072
+ "../runner-core/dist/services/selector-registry-context.js"(exports2) {
235073
+ "use strict";
235074
+ Object.defineProperty(exports2, "__esModule", { value: true });
235075
+ exports2.formatSelectorRegistryContextForPrompt = formatSelectorRegistryContextForPrompt;
235076
+ function isRecord(value) {
235077
+ return value !== null && typeof value === "object" && !Array.isArray(value);
235078
+ }
235079
+ function asString(value, fallback = "") {
235080
+ return typeof value === "string" ? value : fallback;
235081
+ }
235082
+ function asNumber(value, fallback = 0) {
235083
+ return typeof value === "number" && Number.isFinite(value) ? value : fallback;
235084
+ }
235085
+ function asStringArray(value) {
235086
+ return Array.isArray(value) ? value.filter((item) => typeof item === "string") : [];
235087
+ }
235088
+ function truncateForSelectorPrompt(value, maxLength = 180) {
235089
+ const normalized = (value ?? "").replace(/\s+/g, " ").trim();
235090
+ if (normalized.length <= maxLength)
235091
+ return normalized;
235092
+ return `${normalized.slice(0, Math.max(0, maxLength - 3))}...`;
235093
+ }
235094
+ function inlineCode(value, maxLength = 180) {
235095
+ return `\`${truncateForSelectorPrompt(value, maxLength).replace(/`/g, "'")}\``;
235096
+ }
235097
+ function normalizeRef(value) {
235098
+ if (!isRecord(value))
235099
+ return null;
235100
+ const expression = asString(value.expression).trim();
235101
+ if (!expression)
235102
+ return null;
235103
+ return {
235104
+ route: asString(value.route, "*"),
235105
+ usage: asString(value.usage, "locator"),
235106
+ source: asString(value.source, "generated"),
235107
+ expression,
235108
+ occurrenceIndex: asNumber(value.occurrenceIndex, 0)
235109
+ };
235110
+ }
235111
+ function normalizeSibling(value) {
235112
+ if (!isRecord(value))
235113
+ return null;
235114
+ const testCaseId = asString(value.testCaseId).trim();
235115
+ const expression = asString(value.expression).trim();
235116
+ if (!testCaseId || !expression)
235117
+ return null;
235118
+ return {
235119
+ testCaseId,
235120
+ name: asString(value.name, testCaseId),
235121
+ suite: typeof value.suite === "string" ? value.suite : null,
235122
+ lastPassedAt: typeof value.lastPassedAt === "string" ? value.lastPassedAt : null,
235123
+ expression,
235124
+ usage: asString(value.usage, "locator"),
235125
+ route: asString(value.route, "*"),
235126
+ source: asString(value.source, "generated")
235127
+ };
235128
+ }
235129
+ function normalizeEntry(value) {
235130
+ if (!isRecord(value))
235131
+ return null;
235132
+ const id = asString(value.id).trim();
235133
+ const expression = asString(value.expression).trim();
235134
+ if (!id || !expression)
235135
+ return null;
235136
+ return {
235137
+ id,
235138
+ key: asString(value.key, id),
235139
+ route: asString(value.route, "*"),
235140
+ scope: asString(value.scope, "page"),
235141
+ status: asString(value.status, "ACTIVE"),
235142
+ stability: asNumber(value.stability, 1),
235143
+ label: asString(value.label),
235144
+ locatorKind: asString(value.locatorKind),
235145
+ locatorRole: typeof value.locatorRole === "string" ? value.locatorRole : null,
235146
+ locatorName: typeof value.locatorName === "string" ? value.locatorName : null,
235147
+ locatorTestId: typeof value.locatorTestId === "string" ? value.locatorTestId : null,
235148
+ expression,
235149
+ fallbacks: asStringArray(value.fallbacks),
235150
+ currentTestRefs: Array.isArray(value.currentTestRefs) ? value.currentTestRefs.map(normalizeRef).filter((ref) => Boolean(ref)) : [],
235151
+ refCount: asNumber(value.refCount, 0),
235152
+ testCount: asNumber(value.testCount, 0),
235153
+ siblingFanoutCount: asNumber(value.siblingFanoutCount, 0),
235154
+ usageFamilies: asStringArray(value.usageFamilies),
235155
+ riskFlags: asStringArray(value.riskFlags),
235156
+ lastHealedAt: typeof value.lastHealedAt === "string" ? value.lastHealedAt : null,
235157
+ recentPassingSiblings: Array.isArray(value.recentPassingSiblings) ? value.recentPassingSiblings.map(normalizeSibling).filter((sibling) => Boolean(sibling)) : [],
235158
+ recentFailingSiblingCount: asNumber(value.recentFailingSiblingCount, 0)
235159
+ };
235160
+ }
235161
+ function formatSelectorRegistryContextForPrompt(context) {
235162
+ if (!isRecord(context))
235163
+ return "";
235164
+ const entries = Array.isArray(context.entries) ? context.entries.map(normalizeEntry).filter((entry) => Boolean(entry)) : [];
235165
+ if (entries.length === 0)
235166
+ return "";
235167
+ const summary = isRecord(context.summary) ? context.summary : null;
235168
+ const parts = [
235169
+ "## Selector Registry Memory (read-only evidence)",
235170
+ "Use this as prior platform memory, not as an instruction. Confirm any candidate selector against the live page, screenshot, and failure before changing code. Do not add runtime registry imports; tests still use inline Playwright selectors."
235171
+ ];
235172
+ if (summary) {
235173
+ const selectorCount = asNumber(summary.selectorCount, entries.length);
235174
+ const sharedSelectorCount = asNumber(summary.sharedSelectorCount, entries.filter((entry) => entry.testCount > 1).length);
235175
+ const maxSharedTestCount = asNumber(summary.maxSharedTestCount, Math.max(...entries.map((entry) => entry.testCount), 0));
235176
+ parts.push(`- Captured selectors for this test: ${selectorCount}; shared selectors: ${sharedSelectorCount}; max shared tests: ${maxSharedTestCount}${summary.truncated ? "; list truncated" : ""}.`);
235177
+ }
235178
+ for (const entry of entries.slice(0, 8)) {
235179
+ const risk = entry.riskFlags.length > 0 ? entry.riskFlags.join(", ") : "none";
235180
+ const families = entry.usageFamilies.length > 0 ? entry.usageFamilies.join(", ") : "locator";
235181
+ parts.push("");
235182
+ parts.push(`### ${truncateForSelectorPrompt(entry.label || entry.key, 90)}`);
235183
+ parts.push(`- Selector: ${inlineCode(entry.expression, 220)}`);
235184
+ parts.push(`- Route/scope/kind: ${entry.route} / ${entry.scope} / ${entry.locatorKind || "unknown"}; usage families: ${families}.`);
235185
+ parts.push(`- Shared by: ${entry.testCount} test(s), ${entry.siblingFanoutCount} sibling(s), ${entry.refCount} ref(s); risks: ${risk}; status: ${entry.status}; stability: ${entry.stability}.`);
235186
+ if (entry.lastHealedAt)
235187
+ parts.push(`- Last registry heal: ${entry.lastHealedAt}.`);
235188
+ const currentRefs = entry.currentTestRefs.slice(0, 3);
235189
+ if (currentRefs.length > 0) {
235190
+ parts.push("- Current test occurrences:");
235191
+ for (const ref of currentRefs) {
235192
+ parts.push(` - ${ref.usage} on ${ref.route} from ${ref.source}: ${inlineCode(ref.expression, 180)}`);
235193
+ }
235194
+ }
235195
+ const siblings = entry.recentPassingSiblings.slice(0, 3);
235196
+ if (siblings.length > 0) {
235197
+ parts.push("- Recent passing sibling tests using this registry entry:");
235198
+ for (const sibling of siblings) {
235199
+ const suite = sibling.suite ? ` (${truncateForSelectorPrompt(sibling.suite, 50)})` : "";
235200
+ const passed = sibling.lastPassedAt ? ` passed ${sibling.lastPassedAt}` : "";
235201
+ parts.push(` - ${truncateForSelectorPrompt(sibling.name, 80)}${suite}${passed}: ${inlineCode(sibling.expression, 180)}`);
235202
+ }
235203
+ }
235204
+ if (entry.recentFailingSiblingCount && entry.recentFailingSiblingCount > 0) {
235205
+ parts.push(`- Other recent failing siblings on this entry: ${entry.recentFailingSiblingCount}.`);
235206
+ }
235207
+ const fallbacks = (entry.fallbacks ?? []).slice(0, 3);
235208
+ if (fallbacks.length > 0) {
235209
+ parts.push(`- Registry fallbacks: ${fallbacks.map((fallback) => inlineCode(fallback, 120)).join(", ")}.`);
235210
+ }
235211
+ }
235212
+ return `${parts.join("\n")}
235213
+ `;
235214
+ }
235215
+ }
235216
+ });
235217
+
234641
235218
  // ../runner-core/dist/services/mcp-healer.js
234642
235219
  var require_mcp_healer = __commonJS({
234643
235220
  "../runner-core/dist/services/mcp-healer.js"(exports2) {
@@ -234732,6 +235309,7 @@ var require_mcp_healer = __commonJS({
234732
235309
  var snapshot_context_compactor_js_1 = require_snapshot_context_compactor();
234733
235310
  var rate_limit_detector_js_1 = require_rate_limit_detector();
234734
235311
  var preflight_syntax_js_1 = require_preflight_syntax();
235312
+ var selector_registry_context_js_1 = require_selector_registry_context();
234735
235313
  function stripCodeFences(code) {
234736
235314
  let cleaned = code.replace(/<think>[\s\S]*?<\/think>/gi, "").trim();
234737
235315
  cleaned = cleaned.replace(/^\s*```(?:typescript|ts|javascript|js)?\s*\n?/, "").replace(/\n?\s*```\s*$/, "");
@@ -235293,7 +235871,7 @@ ${lines.join("\n")}
235293
235871
  }).join("\n");
235294
235872
  }
235295
235873
  async function executeScriptDrivenHeal(playwrightCode, baseUrl, options) {
235296
- const { testName, testVariables, surfaceIntelligence, healingContext, lastFailureError, failureScreenshot, lastHealAttempt, consecutiveFails, modelOverride, onAICall, stepResults, tags } = options;
235874
+ const { testName, testVariables, surfaceIntelligence, healingContext, selectorRegistryContext, lastFailureError, failureScreenshot, lastHealAttempt, consecutiveFails, modelOverride, onAICall, stepResults, tags } = options;
235297
235875
  const landingViolation = (tags ?? []).includes("@landing-violation");
235298
235876
  const agentModel = modelOverride ? (0, ai_provider_js_1.resolveModelForTask)(modelOverride, "strong") : (0, ai_provider_js_1.getModel)("strong");
235299
235877
  const provider = modelOverride ? (0, ai_provider_js_1.getProviderForModel)(modelOverride) : (0, ai_provider_js_1.getAIProvider)();
@@ -235356,6 +235934,11 @@ ${lastHealAttempt.healReason.slice(0, 800)}`);
235356
235934
  if (healingContext) {
235357
235935
  parts.push(`
235358
235936
  ${formatHealingContextForPrompt(healingContext)}`);
235937
+ }
235938
+ const selectorRegistryPrompt = (0, selector_registry_context_js_1.formatSelectorRegistryContextForPrompt)(selectorRegistryContext);
235939
+ if (selectorRegistryPrompt) {
235940
+ parts.push(`
235941
+ ${selectorRegistryPrompt}`);
235359
235942
  }
235360
235943
  if (surfaceIntelligence) {
235361
235944
  parts.push(`
@@ -235963,6 +236546,7 @@ ${(verifyResult.logs ?? "").slice(0, 2e3)}`;
235963
236546
  testVariables: options?.testVariables,
235964
236547
  surfaceIntelligence: options?.surfaceIntelligence,
235965
236548
  healingContext: options?.healingContext,
236549
+ selectorRegistryContext: options?.selectorRegistryContext,
235966
236550
  lastFailureError: options?.lastFailureError,
235967
236551
  failureScreenshot: options?.failureScreenshot,
235968
236552
  lastHealAttempt: options?.lastHealAttempt,
@@ -236503,6 +237087,7 @@ ${publicLines}` : ""}${credBlock}
236503
237087
  `;
236504
237088
  })()}
236505
237089
  ${options?.healingContext ? `${formatHealingContextForPrompt(options.healingContext)}` : ""}
237090
+ ${(0, selector_registry_context_js_1.formatSelectorRegistryContextForPrompt)(options?.selectorRegistryContext)}
236506
237091
  ${surfaceIntelligence ? `## Original Recorded Actions (what the test was trying to do)
236507
237092
  ${surfaceIntelligence.slice(0, 1500)}
236508
237093
  ` : ""}${landingViolation ? `## STRUCTURAL REQUIREMENT
@@ -238549,6 +239134,7 @@ var require_grok_per_test_healer = __commonJS({
238549
239134
  "use strict";
238550
239135
  Object.defineProperty(exports2, "__esModule", { value: true });
238551
239136
  exports2.executeGrokPerTestHeal = executeGrokPerTestHeal2;
239137
+ exports2.buildHealBrief = buildHealBrief;
238552
239138
  var node_child_process_1 = require("child_process");
238553
239139
  var promises_1 = require("fs/promises");
238554
239140
  var node_os_1 = require("os");
@@ -238565,6 +239151,7 @@ var require_grok_per_test_healer = __commonJS({
238565
239151
  var playwright_mcp_config_js_1 = require_playwright_mcp_config();
238566
239152
  var heal_session_tailer_js_1 = require_heal_session_tailer();
238567
239153
  var proven_actions_adapter_js_1 = require_proven_actions_adapter();
239154
+ var selector_registry_context_js_1 = require_selector_registry_context();
238568
239155
  var HEAL_MCP_CAPABILITIES = ["core", "core-navigation", "core-input", "core-tabs", "testing"];
238569
239156
  var grok_cli_js_1 = require_grok_cli();
238570
239157
  var DEFAULT_MAX_ATTEMPTS = 3;
@@ -238670,6 +239257,7 @@ var require_grok_per_test_healer = __commonJS({
238670
239257
  lastDiagnosis,
238671
239258
  publicVars: publics,
238672
239259
  secretKeys: Object.keys(secrets),
239260
+ selectorRegistryContext: options.selectorRegistryContext ?? null,
238673
239261
  attempt,
238674
239262
  maxAttempts
238675
239263
  }), "utf8");
@@ -238897,6 +239485,7 @@ ${input.lastDiagnosis ? `
238897
239485
  ## Your prior diagnosis (do not repeat the same fix)
238898
239486
  ${input.lastDiagnosis}
238899
239487
  ` : ""}
239488
+ ${(0, selector_registry_context_js_1.formatSelectorRegistryContextForPrompt)(input.selectorRegistryContext)}
238900
239489
 
238901
239490
  ## Tools available
238902
239491
  - **bash** \u2014 \`curl\`, \`sed\`, \`grep\` for static inspection of the app under test.
@@ -299611,6 +300200,699 @@ var require_capture_page_normalizer = __commonJS({
299611
300200
  }
299612
300201
  });
299613
300202
 
300203
+ // ../runner-core/dist/services/perception-enricher.js
300204
+ var require_perception_enricher = __commonJS({
300205
+ "../runner-core/dist/services/perception-enricher.js"(exports2) {
300206
+ "use strict";
300207
+ Object.defineProperty(exports2, "__esModule", { value: true });
300208
+ exports2.enrichCapturedPage = enrichCapturedPage;
300209
+ exports2.mergePerceptionOntoElements = mergePerceptionOntoElements;
300210
+ exports2.elementRoleClass = elementRoleClass;
300211
+ exports2.normalizeName = normalizeName;
300212
+ exports2.urlMatchesRoute = urlMatchesRoute;
300213
+ var DEFAULT_CONFIG = {
300214
+ maxCandidates: 220,
300215
+ maxScan: 4e3,
300216
+ recoverNonSemantic: false,
300217
+ budgetMs: 1500
300218
+ };
300219
+ var BLACKOUT_THRESHOLD = 8;
300220
+ var EVALUATE_TIMEOUT_MS = 1800;
300221
+ var MAX_RECOVERED = 12;
300222
+ async function enrichCapturedPage(page, input, opts = {}) {
300223
+ const empty = { recovered: [], enriched: 0, perceived: 0 };
300224
+ if (!page)
300225
+ return { ...empty, skipReason: "no-page" };
300226
+ try {
300227
+ const url = page.url();
300228
+ if (url && input.route && !urlMatchesRoute(url, input.route)) {
300229
+ opts.onLog?.(`[perception] skip: page ${shortUrl(url)} != captured route ${input.route}`);
300230
+ return { ...empty, skipReason: "route-mismatch" };
300231
+ }
300232
+ } catch {
300233
+ }
300234
+ const elements = input.elements ?? [];
300235
+ const parsedInteractive = input.parsedInteractiveCount ?? elements.length;
300236
+ const cfg = {
300237
+ ...DEFAULT_CONFIG,
300238
+ recoverNonSemantic: parsedInteractive < BLACKOUT_THRESHOLD
300239
+ };
300240
+ let perceived;
300241
+ try {
300242
+ const raw = await withTimeout(page.evaluate(collectPerception, cfg), opts.timeoutMs ?? EVALUATE_TIMEOUT_MS);
300243
+ if (!Array.isArray(raw))
300244
+ return { ...empty, skipReason: "bad-result" };
300245
+ perceived = raw;
300246
+ } catch (err) {
300247
+ opts.onLog?.(`[perception] evaluate failed \u2014 ${err instanceof Error ? err.message : String(err)}`);
300248
+ return { ...empty, skipReason: "evaluate-error" };
300249
+ }
300250
+ const { enriched } = mergePerceptionOntoElements(elements, perceived);
300251
+ const recovered = collectRecovered(perceived);
300252
+ opts.onLog?.(`[perception] enriched ${enriched}/${elements.length} elements, ${recovered.length} non-semantic clickables (${perceived.length} perceived)`);
300253
+ return { recovered, enriched, perceived: perceived.length };
300254
+ }
300255
+ function mergePerceptionOntoElements(elements, perceived) {
300256
+ const perceivedGroups = /* @__PURE__ */ new Map();
300257
+ for (const p of perceived) {
300258
+ if (p.nonSemantic || !p.roleClass || !p.name)
300259
+ continue;
300260
+ const key = `${p.roleClass}\0${p.name}`;
300261
+ let arr = perceivedGroups.get(key);
300262
+ if (!arr) {
300263
+ arr = [];
300264
+ perceivedGroups.set(key, arr);
300265
+ }
300266
+ arr.push(p);
300267
+ }
300268
+ for (const arr of perceivedGroups.values())
300269
+ arr.sort((a, b) => a.domOrder - b.domOrder);
300270
+ const elementGroups = /* @__PURE__ */ new Map();
300271
+ for (const el of elements) {
300272
+ const roleClass = elementRoleClass(el);
300273
+ const name = normalizeName(el.label);
300274
+ if (!roleClass || !name)
300275
+ continue;
300276
+ const key = `${roleClass}\0${name}`;
300277
+ let arr = elementGroups.get(key);
300278
+ if (!arr) {
300279
+ arr = [];
300280
+ elementGroups.set(key, arr);
300281
+ }
300282
+ arr.push(el);
300283
+ }
300284
+ let enriched = 0;
300285
+ for (const [key, els] of elementGroups) {
300286
+ const ps = perceivedGroups.get(key);
300287
+ if (!ps)
300288
+ continue;
300289
+ if (ps.length !== els.length)
300290
+ continue;
300291
+ for (let i = 0; i < els.length; i++) {
300292
+ const enrichment = toEnrichment(ps[i]);
300293
+ if (enrichment) {
300294
+ els[i].enrichment = enrichment;
300295
+ enriched++;
300296
+ }
300297
+ }
300298
+ }
300299
+ return { enriched };
300300
+ }
300301
+ function toEnrichment(p) {
300302
+ const e = {};
300303
+ if (p.rowContext)
300304
+ e.rowContext = p.rowContext;
300305
+ if (p.position && p.position !== "in-view")
300306
+ e.position = p.position;
300307
+ if (p.occluded)
300308
+ e.occluded = true;
300309
+ if (p.cursorPointer)
300310
+ e.cursorPointer = true;
300311
+ return Object.keys(e).length > 0 ? e : null;
300312
+ }
300313
+ function collectRecovered(perceived) {
300314
+ const out = [];
300315
+ const seen = /* @__PURE__ */ new Set();
300316
+ for (const p of perceived) {
300317
+ if (!p.nonSemantic || !p.text)
300318
+ continue;
300319
+ const key = `${p.text}\0${p.rowContext ?? ""}`;
300320
+ if (seen.has(key))
300321
+ continue;
300322
+ seen.add(key);
300323
+ const rc = { text: p.text };
300324
+ if (p.rowContext)
300325
+ rc.rowContext = p.rowContext;
300326
+ out.push(rc);
300327
+ if (out.length >= MAX_RECOVERED)
300328
+ break;
300329
+ }
300330
+ return out;
300331
+ }
300332
+ function elementRoleClass(el) {
300333
+ const r = (el.role ?? "").toLowerCase().trim();
300334
+ const mapped = roleBucket(r);
300335
+ if (mapped)
300336
+ return mapped;
300337
+ const t = (el.elementType ?? "").toLowerCase();
300338
+ if (t === "link")
300339
+ return "link";
300340
+ if (t === "button")
300341
+ return "button";
300342
+ if (t.startsWith("input")) {
300343
+ const it = (el.inputType ?? "").toLowerCase();
300344
+ if (it === "checkbox")
300345
+ return "checkbox";
300346
+ if (it === "radio")
300347
+ return "radio";
300348
+ if (it === "submit" || it === "button")
300349
+ return "button";
300350
+ return "textbox";
300351
+ }
300352
+ return t || "";
300353
+ }
300354
+ function roleBucket(r) {
300355
+ if (!r)
300356
+ return null;
300357
+ if (r === "link")
300358
+ return "link";
300359
+ if (r === "button")
300360
+ return "button";
300361
+ if (r === "textbox" || r === "searchbox" || r === "spinbutton")
300362
+ return "textbox";
300363
+ if (r === "combobox" || r === "listbox")
300364
+ return "combobox";
300365
+ if (r === "checkbox" || r === "switch")
300366
+ return "checkbox";
300367
+ if (r === "radio")
300368
+ return "radio";
300369
+ if (r === "tab")
300370
+ return "tab";
300371
+ if (r === "menuitem" || r === "menuitemcheckbox" || r === "menuitemradio")
300372
+ return "menuitem";
300373
+ if (r === "option")
300374
+ return "option";
300375
+ if (r === "slider")
300376
+ return "slider";
300377
+ return r;
300378
+ }
300379
+ function normalizeName(name) {
300380
+ return (name ?? "").replace(/\s+/g, " ").trim().toLowerCase().slice(0, 80);
300381
+ }
300382
+ function withTimeout(p, ms) {
300383
+ return new Promise((resolve, reject) => {
300384
+ const t = setTimeout(() => reject(new Error(`timeout after ${ms}ms`)), ms);
300385
+ p.then((v) => {
300386
+ clearTimeout(t);
300387
+ resolve(v);
300388
+ }, (e) => {
300389
+ clearTimeout(t);
300390
+ reject(e);
300391
+ });
300392
+ });
300393
+ }
300394
+ function parseLoc(input) {
300395
+ try {
300396
+ const u = new URL(input, "http://__rel__");
300397
+ return {
300398
+ origin: u.host === "__rel__" ? "" : u.host,
300399
+ // '' when input was path-relative
300400
+ path: (u.pathname || "/").replace(/\/+$/, "") || "/",
300401
+ search: u.search,
300402
+ hash: u.hash
300403
+ };
300404
+ } catch {
300405
+ const hashI = input.indexOf("#");
300406
+ const hash = hashI >= 0 ? input.slice(hashI) : "";
300407
+ const beforeHash = hashI >= 0 ? input.slice(0, hashI) : input;
300408
+ const qi = beforeHash.indexOf("?");
300409
+ const search = qi >= 0 ? beforeHash.slice(qi) : "";
300410
+ const path = ((qi >= 0 ? beforeHash.slice(0, qi) : beforeHash) || "/").replace(/\/+$/, "") || "/";
300411
+ return { origin: "", path, search, hash };
300412
+ }
300413
+ }
300414
+ function urlMatchesRoute(url, route2) {
300415
+ const u = parseLoc(url);
300416
+ const r = parseLoc(route2);
300417
+ if (u.path !== r.path)
300418
+ return false;
300419
+ if (r.origin && u.origin && r.origin !== u.origin)
300420
+ return false;
300421
+ if (r.search && u.search !== r.search)
300422
+ return false;
300423
+ if (r.hash && u.hash !== r.hash)
300424
+ return false;
300425
+ return true;
300426
+ }
300427
+ function shortUrl(url) {
300428
+ return url.length > 80 ? `${url.slice(0, 77)}...` : url;
300429
+ }
300430
+ function collectPerception(cfg) {
300431
+ const NATIVE = {
300432
+ A: true,
300433
+ BUTTON: true,
300434
+ INPUT: true,
300435
+ SELECT: true,
300436
+ TEXTAREA: true,
300437
+ SUMMARY: true,
300438
+ OPTION: true
300439
+ };
300440
+ const INTERACTIVE_ROLE = {
300441
+ button: true,
300442
+ link: true,
300443
+ textbox: true,
300444
+ searchbox: true,
300445
+ spinbutton: true,
300446
+ combobox: true,
300447
+ listbox: true,
300448
+ checkbox: true,
300449
+ radio: true,
300450
+ switch: true,
300451
+ menuitem: true,
300452
+ menuitemcheckbox: true,
300453
+ menuitemradio: true,
300454
+ tab: true,
300455
+ option: true,
300456
+ slider: true
300457
+ };
300458
+ const norm = (s) => (s || "").replace(/\s+/g, " ").trim();
300459
+ const lower = (s) => s.toLowerCase().slice(0, 80);
300460
+ const roleClassOf = (tag, roleAttr, inputType) => {
300461
+ const r = roleAttr.toLowerCase();
300462
+ if (r) {
300463
+ if (r === "link")
300464
+ return "link";
300465
+ if (r === "button")
300466
+ return "button";
300467
+ if (r === "textbox" || r === "searchbox" || r === "spinbutton")
300468
+ return "textbox";
300469
+ if (r === "combobox" || r === "listbox")
300470
+ return "combobox";
300471
+ if (r === "switch" || r === "checkbox")
300472
+ return "checkbox";
300473
+ if (r === "radio")
300474
+ return "radio";
300475
+ if (r === "tab")
300476
+ return "tab";
300477
+ if (r === "menuitem" || r === "menuitemcheckbox" || r === "menuitemradio")
300478
+ return "menuitem";
300479
+ if (r === "option")
300480
+ return "option";
300481
+ if (r === "slider")
300482
+ return "slider";
300483
+ return r;
300484
+ }
300485
+ if (tag === "A")
300486
+ return "link";
300487
+ if (tag === "BUTTON" || tag === "SUMMARY")
300488
+ return "button";
300489
+ if (tag === "SELECT")
300490
+ return "combobox";
300491
+ if (tag === "TEXTAREA")
300492
+ return "textbox";
300493
+ if (tag === "OPTION")
300494
+ return "option";
300495
+ if (tag === "INPUT") {
300496
+ const t = (inputType || "text").toLowerCase();
300497
+ if (t === "checkbox")
300498
+ return "checkbox";
300499
+ if (t === "radio")
300500
+ return "radio";
300501
+ if (t === "submit" || t === "button" || t === "image" || t === "reset")
300502
+ return "button";
300503
+ if (t === "range")
300504
+ return "slider";
300505
+ return "textbox";
300506
+ }
300507
+ return "other";
300508
+ };
300509
+ const accName = (el) => {
300510
+ const al = norm(el.getAttribute("aria-label"));
300511
+ if (al)
300512
+ return al;
300513
+ const lb = el.getAttribute("aria-labelledby");
300514
+ if (lb) {
300515
+ const ids = lb.split(/\s+/);
300516
+ let txt = "";
300517
+ for (let i = 0; i < ids.length; i++) {
300518
+ const t = el.ownerDocument.getElementById(ids[i]);
300519
+ if (t)
300520
+ txt += (txt ? " " : "") + norm(t.textContent);
300521
+ }
300522
+ if (txt)
300523
+ return txt;
300524
+ }
300525
+ if (el.tagName === "INPUT" || el.tagName === "TEXTAREA" || el.tagName === "SELECT") {
300526
+ const id = el.getAttribute("id");
300527
+ if (id) {
300528
+ try {
300529
+ const lab = el.ownerDocument.querySelector('label[for="' + (window.CSS && CSS.escape ? CSS.escape(id) : id) + '"]');
300530
+ const t = lab ? norm(lab.textContent) : "";
300531
+ if (t)
300532
+ return t;
300533
+ } catch {
300534
+ }
300535
+ }
300536
+ const val = norm(el.value);
300537
+ if (val)
300538
+ return val;
300539
+ const ph = norm(el.getAttribute("placeholder"));
300540
+ if (ph)
300541
+ return ph;
300542
+ return norm(el.getAttribute("name"));
300543
+ }
300544
+ const text = norm(el.textContent);
300545
+ if (text)
300546
+ return text;
300547
+ return norm(el.getAttribute("title")) || norm(el.getAttribute("alt"));
300548
+ };
300549
+ const ROW_SEL = 'tr,li,[role="row"],[role="listitem"],[role="article"],article,[role="option"]';
300550
+ const isCardish = (el) => {
300551
+ const cls = (el.getAttribute("class") || "").toLowerCase();
300552
+ return /(^|[\s_-])(card|row|item|listitem|cell|tile)([\s_-]|$)/.test(cls);
300553
+ };
300554
+ const rowContextOf = (el, ownNameLower) => {
300555
+ let cur = el.parentElement;
300556
+ let depth = 0;
300557
+ while (cur && depth < 8) {
300558
+ let isRow = false;
300559
+ try {
300560
+ isRow = cur.matches(ROW_SEL);
300561
+ } catch {
300562
+ isRow = false;
300563
+ }
300564
+ if (isRow || isCardish(cur)) {
300565
+ const head = cur.querySelector('h1,h2,h3,h4,h5,h6,[role="heading"],strong,b,th,td');
300566
+ let txt = head ? norm(head.textContent) : "";
300567
+ if (!txt)
300568
+ txt = norm(cur.textContent);
300569
+ txt = txt.slice(0, 80);
300570
+ const low = txt.toLowerCase();
300571
+ if (txt && low !== ownNameLower && low.length > 1)
300572
+ return txt;
300573
+ return null;
300574
+ }
300575
+ cur = cur.parentElement;
300576
+ depth++;
300577
+ }
300578
+ return null;
300579
+ };
300580
+ const vh = window.innerHeight || 0;
300581
+ const vw = window.innerWidth || 0;
300582
+ const visibleRect = (el) => {
300583
+ let rect;
300584
+ let style;
300585
+ try {
300586
+ rect = el.getBoundingClientRect();
300587
+ style = window.getComputedStyle(el);
300588
+ } catch {
300589
+ return null;
300590
+ }
300591
+ if (style.display === "none" || style.visibility === "hidden")
300592
+ return null;
300593
+ if (parseFloat(style.opacity || "1") === 0)
300594
+ return null;
300595
+ if (rect.width <= 0 || rect.height <= 0)
300596
+ return null;
300597
+ try {
300598
+ if (el.closest('[aria-hidden="true"],[inert]'))
300599
+ return null;
300600
+ } catch {
300601
+ }
300602
+ return { rect, style };
300603
+ };
300604
+ const occlusionOf = (el, rect) => {
300605
+ const cx = rect.left + rect.width / 2;
300606
+ const cy = rect.top + rect.height / 2;
300607
+ if (cx < 0 || cy < 0 || cx > vw || cy > vh)
300608
+ return false;
300609
+ try {
300610
+ const hit = document.elementFromPoint(cx, cy);
300611
+ if (!hit || hit === el || el.contains(hit) || hit.contains(el))
300612
+ return false;
300613
+ let anc = hit;
300614
+ let guard = 0;
300615
+ while (anc && guard < 12) {
300616
+ const pos = window.getComputedStyle(anc).position;
300617
+ if (pos === "fixed" || pos === "sticky")
300618
+ return false;
300619
+ anc = anc.parentElement;
300620
+ guard++;
300621
+ }
300622
+ return true;
300623
+ } catch {
300624
+ return false;
300625
+ }
300626
+ };
300627
+ const positionOf = (rect) => rect.bottom < 0 ? "above" : rect.top > vh ? "below" : "in-view";
300628
+ const t0 = typeof performance !== "undefined" && performance.now ? performance.now() : 0;
300629
+ const overBudget = () => t0 > 0 && performance.now() - t0 > cfg.budgetMs;
300630
+ const out = [];
300631
+ let order = 0;
300632
+ const SEL = 'a[href],button,input:not([type="hidden"]),textarea,select,summary,option,[role],[onclick],[tabindex]';
300633
+ const all = document.querySelectorAll(SEL);
300634
+ const limit = all.length < cfg.maxScan ? all.length : cfg.maxScan;
300635
+ for (let i = 0; i < limit; i++) {
300636
+ if (out.length >= cfg.maxCandidates)
300637
+ break;
300638
+ if ((i & 63) === 0 && overBudget())
300639
+ break;
300640
+ const el = all[i];
300641
+ const vis = visibleRect(el);
300642
+ if (!vis)
300643
+ continue;
300644
+ const tag = el.tagName;
300645
+ const roleAttr = el.getAttribute("role") || "";
300646
+ const inputType = tag === "INPUT" ? el.type || "" : "";
300647
+ const cursorPointer = vis.style.cursor === "pointer";
300648
+ const native = NATIVE[tag] === true;
300649
+ const roleLower = roleAttr.toLowerCase();
300650
+ const hasInteractiveRole = !!roleLower && INTERACTIVE_ROLE[roleLower] === true;
300651
+ const hasOnclick = el.hasAttribute("onclick");
300652
+ const tabAttr = el.getAttribute("tabindex");
300653
+ const tabbable = tabAttr != null && tabAttr !== "-1";
300654
+ const semantic = native || hasInteractiveRole;
300655
+ const nonSemantic = !semantic && (cursorPointer || hasOnclick || tabbable);
300656
+ if (!semantic && !nonSemantic)
300657
+ continue;
300658
+ const myOrder = order++;
300659
+ const name = accName(el);
300660
+ const lname = lower(name);
300661
+ const rect = vis.rect;
300662
+ out.push({
300663
+ domOrder: myOrder,
300664
+ roleClass: roleClassOf(tag, roleAttr, inputType),
300665
+ name: lname,
300666
+ rowContext: rowContextOf(el, lname),
300667
+ position: positionOf(rect),
300668
+ occluded: occlusionOf(el, rect),
300669
+ cursorPointer,
300670
+ nonSemantic,
300671
+ text: nonSemantic ? name.slice(0, 80) || null : null
300672
+ });
300673
+ }
300674
+ if (cfg.recoverNonSemantic) {
300675
+ const extra = document.querySelectorAll("div,span,li,td,p");
300676
+ const extraLimit = extra.length < cfg.maxScan ? extra.length : cfg.maxScan;
300677
+ for (let i = 0; i < extraLimit; i++) {
300678
+ if (out.length >= cfg.maxCandidates)
300679
+ break;
300680
+ if ((i & 63) === 0 && overBudget())
300681
+ break;
300682
+ const el = extra[i];
300683
+ const vis = visibleRect(el);
300684
+ if (!vis)
300685
+ continue;
300686
+ if (vis.style.cursor !== "pointer")
300687
+ continue;
300688
+ if (el.querySelector('a,button,input,select,textarea,[role="button"],[role="link"]'))
300689
+ continue;
300690
+ const rect = vis.rect;
300691
+ if (rect.width < 6 || rect.height < 6)
300692
+ continue;
300693
+ if (rect.width > vw * 0.98)
300694
+ continue;
300695
+ if (positionOf(rect) !== "in-view")
300696
+ continue;
300697
+ if (occlusionOf(el, rect))
300698
+ continue;
300699
+ const name = accName(el);
300700
+ if (!name)
300701
+ continue;
300702
+ const lname = lower(name);
300703
+ out.push({
300704
+ domOrder: order++,
300705
+ roleClass: "button",
300706
+ name: lname,
300707
+ rowContext: rowContextOf(el, lname),
300708
+ position: "in-view",
300709
+ occluded: false,
300710
+ cursorPointer: true,
300711
+ nonSemantic: true,
300712
+ text: name.slice(0, 80) || null
300713
+ });
300714
+ }
300715
+ }
300716
+ return out;
300717
+ }
300718
+ }
300719
+ });
300720
+
300721
+ // ../runner-core/dist/services/stuck-detector.js
300722
+ var require_stuck_detector = __commonJS({
300723
+ "../runner-core/dist/services/stuck-detector.js"(exports2) {
300724
+ "use strict";
300725
+ Object.defineProperty(exports2, "__esModule", { value: true });
300726
+ exports2.StuckDetector = void 0;
300727
+ exports2.isPageAffectingTool = isPageAffectingTool;
300728
+ exports2.normalizeStuckSignature = normalizeStuckSignature;
300729
+ var PAGE_AFFECTING_TOOLS = /* @__PURE__ */ new Set([
300730
+ "browser_click",
300731
+ "browser_type",
300732
+ "browser_fill_form",
300733
+ "browser_select_option",
300734
+ "browser_navigate",
300735
+ "browser_press_key",
300736
+ "browser_hover",
300737
+ "browser_drag"
300738
+ ]);
300739
+ function isPageAffectingTool(toolName) {
300740
+ return PAGE_AFFECTING_TOOLS.has(toolName);
300741
+ }
300742
+ function normalizeStuckSignature(toolName, toolArgs) {
300743
+ const rawRef = typeof toolArgs.ref === "string" ? toolArgs.ref.trim() : "";
300744
+ const rawElement = typeof toolArgs.element === "string" ? toolArgs.element.trim() : "";
300745
+ const elementKey = rawRef ? rawRef : rawElement ? rawElement.toLowerCase().replace(/\S+@\S+/g, "[email]").slice(0, 60) : "";
300746
+ switch (toolName) {
300747
+ case "browser_click":
300748
+ case "browser_hover":
300749
+ case "browser_select_option":
300750
+ case "browser_type":
300751
+ case "browser_fill_form":
300752
+ case "browser_drag":
300753
+ return elementKey ? `${toolName}:${elementKey}` : null;
300754
+ case "browser_navigate": {
300755
+ const url = typeof toolArgs.url === "string" ? canonicalizeUrlPath(toolArgs.url) : "";
300756
+ return `browser_navigate:${url}`;
300757
+ }
300758
+ case "browser_press_key": {
300759
+ const key = typeof toolArgs.key === "string" ? toolArgs.key : "";
300760
+ return `browser_press_key:${key}`;
300761
+ }
300762
+ default:
300763
+ return null;
300764
+ }
300765
+ }
300766
+ function canonicalizeUrlPath(url) {
300767
+ try {
300768
+ const u = new URL(url, "http://x");
300769
+ return u.pathname.replace(/\/+$/, "") || "/";
300770
+ } catch {
300771
+ return url.trim().split(/[?#]/)[0] || url.trim();
300772
+ }
300773
+ }
300774
+ var NO_VERDICT = Object.freeze({
300775
+ tier: 0,
300776
+ kind: null,
300777
+ repeatedAction: null,
300778
+ recover: false,
300779
+ yieldRun: false
300780
+ });
300781
+ var DEFAULTS = {
300782
+ window: 8,
300783
+ repeatThreshold: 3,
300784
+ stagnationThreshold: 4,
300785
+ tier2After: 3,
300786
+ tier3After: 5,
300787
+ maxRecoveries: 2,
300788
+ repetitionReachesRecovery: true
300789
+ };
300790
+ var StuckDetector = class {
300791
+ opts;
300792
+ records = [];
300793
+ stallStreak = 0;
300794
+ recoveryCount = 0;
300795
+ lastEmittedTier = 0;
300796
+ constructor(options = {}) {
300797
+ this.opts = { ...DEFAULTS, ...options };
300798
+ }
300799
+ record(input) {
300800
+ this.records.push(input);
300801
+ if (this.records.length > this.opts.window) {
300802
+ this.records.splice(0, this.records.length - this.opts.window);
300803
+ }
300804
+ const stall = this.detectStall();
300805
+ if (!stall.stalled) {
300806
+ this.stallStreak = 0;
300807
+ this.lastEmittedTier = 0;
300808
+ return NO_VERDICT;
300809
+ }
300810
+ this.stallStreak += 1;
300811
+ let desired = 1;
300812
+ if (this.stallStreak >= this.opts.tier3After)
300813
+ desired = 3;
300814
+ else if (this.stallStreak >= this.opts.tier2After)
300815
+ desired = 2;
300816
+ if (stall.kind === "stagnation" && desired > 2)
300817
+ desired = 2;
300818
+ if (!this.opts.repetitionReachesRecovery && stall.kind === "repetition" && desired > 2)
300819
+ desired = 2;
300820
+ if (desired <= this.lastEmittedTier)
300821
+ return NO_VERDICT;
300822
+ if (desired === 3) {
300823
+ if (this.recoveryCount >= this.opts.maxRecoveries) {
300824
+ this.lastEmittedTier = 4;
300825
+ return {
300826
+ tier: 4,
300827
+ kind: stall.kind,
300828
+ repeatedAction: stall.repeatedAction,
300829
+ recover: false,
300830
+ yieldRun: true
300831
+ };
300832
+ }
300833
+ this.recoveryCount += 1;
300834
+ this.records = [];
300835
+ this.stallStreak = 0;
300836
+ this.lastEmittedTier = 0;
300837
+ return {
300838
+ tier: 3,
300839
+ kind: stall.kind,
300840
+ repeatedAction: stall.repeatedAction,
300841
+ recover: true,
300842
+ yieldRun: false
300843
+ };
300844
+ }
300845
+ this.lastEmittedTier = desired;
300846
+ return {
300847
+ tier: desired,
300848
+ kind: stall.kind,
300849
+ repeatedAction: stall.repeatedAction,
300850
+ recover: false,
300851
+ yieldRun: false
300852
+ };
300853
+ }
300854
+ detectStall() {
300855
+ const { stagnationThreshold, repeatThreshold } = this.opts;
300856
+ if (this.records.length < stagnationThreshold) {
300857
+ return { stalled: false, kind: null, repeatedAction: null };
300858
+ }
300859
+ const recent = this.records.slice(-stagnationThreshold);
300860
+ const progressStalled = recent.length >= stagnationThreshold && recent.every((r) => r.progressKey === recent[recent.length - 1].progressKey);
300861
+ if (progressStalled) {
300862
+ const counts = /* @__PURE__ */ new Map();
300863
+ for (const rec of this.records) {
300864
+ for (const sig of rec.actionSignatures) {
300865
+ counts.set(sig, (counts.get(sig) ?? 0) + 1);
300866
+ }
300867
+ }
300868
+ let repeatedAction = null;
300869
+ let max = 0;
300870
+ for (const [sig, count] of counts) {
300871
+ if (count > max) {
300872
+ max = count;
300873
+ repeatedAction = sig;
300874
+ }
300875
+ }
300876
+ if (repeatedAction && max >= repeatThreshold) {
300877
+ return { stalled: true, kind: "repetition", repeatedAction };
300878
+ }
300879
+ }
300880
+ if (progressStalled) {
300881
+ const sameRoute = recent.every((r) => r.route === recent[recent.length - 1].route);
300882
+ const pageAffectingCount = recent.filter((r) => r.pageAffecting).length;
300883
+ const distinctSignatures = new Set(recent.flatMap((r) => r.actionSignatures));
300884
+ const lowDiversity = distinctSignatures.size >= 1 && distinctSignatures.size <= Math.max(1, Math.floor(pageAffectingCount / 2));
300885
+ if (sameRoute && pageAffectingCount >= Math.ceil(stagnationThreshold / 2) && lowDiversity) {
300886
+ return { stalled: true, kind: "stagnation", repeatedAction: null };
300887
+ }
300888
+ }
300889
+ return { stalled: false, kind: null, repeatedAction: null };
300890
+ }
300891
+ };
300892
+ exports2.StuckDetector = StuckDetector;
300893
+ }
300894
+ });
300895
+
299614
300896
  // ../runner-core/dist/services/evidence-harvester.js
299615
300897
  var require_evidence_harvester = __commonJS({
299616
300898
  "../runner-core/dist/services/evidence-harvester.js"(exports2) {
@@ -299907,8 +301189,10 @@ var require_discover_explorer = __commonJS({
299907
301189
  var capture_page_normalizer_js_1 = require_capture_page_normalizer();
299908
301190
  var snapshot_observation_js_1 = require_snapshot_observation();
299909
301191
  var snapshot_context_compactor_js_1 = require_snapshot_context_compactor();
301192
+ var perception_enricher_js_1 = require_perception_enricher();
299910
301193
  var exploration_prompt_builder_js_2 = require_exploration_prompt_builder();
299911
301194
  var exploration_state_js_1 = require_exploration_state();
301195
+ var stuck_detector_js_1 = require_stuck_detector();
299912
301196
  var evidence_harvester_js_1 = require_evidence_harvester();
299913
301197
  var exploration_parser_js_1 = require_exploration_parser();
299914
301198
  var snapshot_context_compactor_js_2 = require_snapshot_context_compactor();
@@ -300691,6 +301975,7 @@ ${credentialBlock}${redactedBlock}${inboxBlock}`;
300691
301975
  const v3Env = process.env.DISCOVERY_PROMPT_V3;
300692
301976
  const useV3 = v3Env !== "false";
300693
301977
  const useV2 = true;
301978
+ const perceptionEnricherEnabled = (process.env.DISCOVERY_PERCEPTION_ENRICHER === "true" || process.env.DISCOVERY_PERCEPTION_ENRICHER === "1") && typeof options?.getPerceptionPage === "function";
300694
301979
  const state2 = (0, exploration_state_js_1.createExplorationState)();
300695
301980
  const recentExchanges = [];
300696
301981
  const transientDirectives = [];
@@ -300815,6 +302100,8 @@ ${inboxPromptBlock}`;
300815
302100
  let lastBriefRoute = "";
300816
302101
  let lastBriefIteration = 0;
300817
302102
  const BRIEF_INTERVAL = 5;
302103
+ const stuckDetectorEnabled = process.env.DISCOVERY_STUCK_DETECTOR === "1" || process.env.DISCOVERY_STUCK_DETECTOR === "true";
302104
+ const stuckDetector = stuckDetectorEnabled ? new stuck_detector_js_1.StuckDetector({ repetitionReachesRecovery: !isDeepMode }) : null;
300818
302105
  while (iteration < maxIterations && !explorationComplete) {
300819
302106
  iteration++;
300820
302107
  (0, exploration_state_js_1.reduceEvent)(state2, { type: "ITERATION_STARTED", iteration });
@@ -301286,6 +302573,19 @@ ${currentSummary}`;
301286
302573
  const elapsed = Date.now() - readinessStartedAt;
301287
302574
  logs2.push(` [discover-explorer] proceeded with not-ready snapshot after ${readinessRetries} retries / ${elapsed}ms`);
301288
302575
  }
302576
+ let recoveredClickables = [];
302577
+ if (perceptionEnricherEnabled) {
302578
+ try {
302579
+ const result = await (0, perception_enricher_js_1.enrichCapturedPage)(options?.getPerceptionPage?.(), {
302580
+ route: normalized.route,
302581
+ elements: normalized.elements,
302582
+ parsedInteractiveCount: normalized.snapshotMetrics?.parsedInteractiveCount
302583
+ }, { onLog: (msg) => logs2.push(` ${msg}`) });
302584
+ recoveredClickables = result.recovered;
302585
+ } catch (perceptionErr) {
302586
+ logs2.push(` [perception] enrich skipped \u2014 ${perceptionErr instanceof Error ? perceptionErr.message : String(perceptionErr)}`);
302587
+ }
302588
+ }
301289
302589
  (0, exploration_state_js_1.reduceEvent)(state2, {
301290
302590
  type: "PAGE_CAPTURED",
301291
302591
  route: normalized.route,
@@ -301354,7 +302654,7 @@ ${currentSummary}`;
301354
302654
  logs2.push(` Captured page: ${normalized.route}, ${normalized.provenCommands.length} proven commands${normalized.alreadyCaptured ? " (RE-VISIT)" : ""}`);
301355
302655
  const transitionHint = !normalized.alreadyCaptured && isSurveyMode ? { tip: `Also call record_transition with fromRoute, toRoute, triggerType, and triggerLabel (the exact link text you clicked to get here).` } : {};
301356
302656
  const lastAction = normalized.observations.length > 0 ? normalized.observations[normalized.observations.length - 1].action : void 0;
301357
- const snapshotBlock = (0, snapshot_observation_js_1.renderCompactSnapshotBlock)(normalized, { lastAction }, { elementLimit: 6, formLimit: 3, observationLimit: 3, mode: options?.mode ?? "full" });
302657
+ const snapshotBlock = (0, snapshot_observation_js_1.renderCompactSnapshotBlock)(normalized, { lastAction }, { elementLimit: 6, formLimit: 3, observationLimit: 3, mode: options?.mode ?? "full", recoveredClickables });
301358
302658
  toolResults.push({
301359
302659
  role: "tool",
301360
302660
  tool_call_id: toolCall.id,
@@ -302293,6 +303593,59 @@ Exploration complete: ${summary}`);
302293
303593
  }
302294
303594
  delete toolResults.__accountAlreadyExistsDirective;
302295
303595
  }
303596
+ if (stuckDetector && assistantMessage.tool_calls && assistantMessage.tool_calls.length > 0) {
303597
+ const actionSignatures = [];
303598
+ let pageAffecting = false;
303599
+ for (const tc of assistantMessage.tool_calls) {
303600
+ if ((0, stuck_detector_js_1.isPageAffectingTool)(tc.function.name))
303601
+ pageAffecting = true;
303602
+ let parsedArgs = {};
303603
+ try {
303604
+ parsedArgs = JSON.parse(tc.function.arguments || "{}");
303605
+ } catch {
303606
+ }
303607
+ const sig = (0, stuck_detector_js_1.normalizeStuckSignature)(tc.function.name, parsedArgs);
303608
+ if (sig)
303609
+ actionSignatures.push(sig);
303610
+ }
303611
+ const progressKey = `${state2.currentRoute}|${state2.visitedRoutes.size}|${state2.recordedJourneys.length}|${lastCaptureIteration}`;
303612
+ const verdict = stuckDetector.record({
303613
+ iteration,
303614
+ actionSignatures,
303615
+ route: state2.currentRoute,
303616
+ progressKey,
303617
+ pageAffecting
303618
+ });
303619
+ if (verdict.tier > 0) {
303620
+ const topFrontier = (0, exploration_state_js_1.getRankedFrontier)(state2).slice(0, 3);
303621
+ const frontierHint = topFrontier.length > 0 ? ` (e.g. ${topFrontier.join(", ")})` : "";
303622
+ const repeated = verdict.repeatedAction ? verdict.repeatedAction.slice(0, 80) : null;
303623
+ const act = repeated ? `"${repeated}"` : "the same actions";
303624
+ let directive = null;
303625
+ if (verdict.yieldRun) {
303626
+ logs2.push(` Stuck detector: yielding after repeated local loop (${verdict.kind}, ${act})`);
303627
+ explorationComplete = true;
303628
+ summary = `Exploration stopped after ${iteration} iterations - the agent was stuck in a local loop (${verdict.kind}${repeated ? `: ${repeated}` : ""}) and harness recovery did not break it. Visited ${state2.visitedRoutes.size} pages, recorded ${state2.recordedJourneys.length} journeys.`;
303629
+ break;
303630
+ } else if (verdict.recover) {
303631
+ directive = `You are stuck in a loop on ${state2.currentRoute}: you repeated ${act} several times with no new progress, and it is not working. Stop retrying it. You MUST now do something different - navigate to an unvisited route${frontierHint}, open an unexplored tab/menu, or call finish_exploration if you have explored everything reachable. Check the "Already interacted with" and "Do Not Retry" lists before acting.`;
303632
+ logs2.push(` Stuck detector: harness recovery (tier 3, ${verdict.kind}, ${act})`);
303633
+ } else if (verdict.tier === 2) {
303634
+ directive = `STOP repeating ${act} - you have made no new progress for several iterations on ${state2.currentRoute}. ${topFrontier.length > 0 ? `Navigate to one of these unvisited routes now: ${topFrontier.join(", ")}.` : "Open an unexplored tab/menu, or call capture_page for this page and then move on."} If you have explored everything reachable, call finish_exploration. Check the "Already interacted with" and "Do Not Retry" lists before acting.`;
303635
+ logs2.push(` Stuck detector: firm nudge (tier 2, ${verdict.kind}, ${act})`);
303636
+ } else {
303637
+ directive = `You appear to be repeating ${act} on ${state2.currentRoute} without making progress. Try a different element, open an unexplored tab/menu, or navigate to an unvisited route${frontierHint}. Check the "Already interacted with" and "Do Not Retry" lists before acting.`;
303638
+ logs2.push(` Stuck detector: soft nudge (tier 1, ${verdict.kind}, ${act})`);
303639
+ }
303640
+ if (directive) {
303641
+ if (useV3) {
303642
+ transientDirectives.push(directive);
303643
+ } else {
303644
+ messages.push({ role: "user", content: directive });
303645
+ }
303646
+ }
303647
+ }
303648
+ }
302296
303649
  }
302297
303650
  if (!explorationComplete) {
302298
303651
  logs2.push(`
@@ -303001,7 +304354,8 @@ ${credentialLines}
303001
304354
  userNotes: featureOpts.userNotes,
303002
304355
  onAICall: featureOpts.onAICall,
303003
304356
  onProgress: featureOpts.onProgress,
303004
- onCredentialUpdate: featureOpts.onCredentialUpdate
304357
+ onCredentialUpdate: featureOpts.onCredentialUpdate,
304358
+ getPerceptionPage: featureOpts.getPerceptionPage
303005
304359
  }, config);
303006
304360
  }
303007
304361
  }
@@ -310045,54 +311399,13 @@ var require_mobile_exploration_state = __commonJS({
310045
311399
  var require_mobile_discovery_agent = __commonJS({
310046
311400
  "../runner-core/dist/services/mobile/mobile-discovery-agent.js"(exports2) {
310047
311401
  "use strict";
310048
- var __createBinding = exports2 && exports2.__createBinding || (Object.create ? (function(o, m, k, k2) {
310049
- if (k2 === void 0) k2 = k;
310050
- var desc = Object.getOwnPropertyDescriptor(m, k);
310051
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
310052
- desc = { enumerable: true, get: function() {
310053
- return m[k];
310054
- } };
310055
- }
310056
- Object.defineProperty(o, k2, desc);
310057
- }) : (function(o, m, k, k2) {
310058
- if (k2 === void 0) k2 = k;
310059
- o[k2] = m[k];
310060
- }));
310061
- var __setModuleDefault = exports2 && exports2.__setModuleDefault || (Object.create ? (function(o, v) {
310062
- Object.defineProperty(o, "default", { enumerable: true, value: v });
310063
- }) : function(o, v) {
310064
- o["default"] = v;
310065
- });
310066
- var __importStar = exports2 && exports2.__importStar || /* @__PURE__ */ (function() {
310067
- var ownKeys = function(o) {
310068
- ownKeys = Object.getOwnPropertyNames || function(o2) {
310069
- var ar = [];
310070
- for (var k in o2) if (Object.prototype.hasOwnProperty.call(o2, k)) ar[ar.length] = k;
310071
- return ar;
310072
- };
310073
- return ownKeys(o);
310074
- };
310075
- return function(mod) {
310076
- if (mod && mod.__esModule) return mod;
310077
- var result = {};
310078
- if (mod != null) {
310079
- for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
310080
- }
310081
- __setModuleDefault(result, mod);
310082
- return result;
310083
- };
310084
- })();
310085
311402
  Object.defineProperty(exports2, "__esModule", { value: true });
310086
311403
  exports2.runMobileDiscoveryOnRunner = runMobileDiscoveryOnRunner2;
310087
311404
  var mobile_explorer_js_1 = require_mobile_explorer();
310088
311405
  var mobile_discovery_prompts_js_1 = require_mobile_discovery_prompts();
311406
+ var mobile_generator_js_1 = require_mobile_generator();
310089
311407
  var mobile_exploration_state_js_1 = require_mobile_exploration_state();
310090
311408
  var discover_planner_js_1 = require_discover_planner();
310091
- var GENERATOR_MODULE = "./mobile-generator.js";
310092
- var defaultRunGenerate = async (args) => {
310093
- const mod = await Promise.resolve(`${GENERATOR_MODULE}`).then((s) => __importStar(require(s)));
310094
- return mod.runMobileGeneratePhase(args);
310095
- };
310096
311409
  var EXPLORE_BUDGET_DEEP = 60;
310097
311410
  var EXPLORE_BUDGET_SURVEY = 40;
310098
311411
  function platformForMedium(medium) {
@@ -310127,7 +311440,7 @@ var require_mobile_discovery_agent = __commonJS({
310127
311440
  const onScreenshot = callbacks?.onScreenshot;
310128
311441
  const runExplore = callbacks?.runExplore ?? mobile_explorer_js_1.runMobileExplorePhase;
310129
311442
  const runPlan = callbacks?.runPlan ?? mobile_discovery_prompts_js_1.runMobileAIPlanner;
310130
- const runGenerate = callbacks?.runGenerate ?? defaultRunGenerate;
311443
+ const runGenerate = callbacks?.runGenerate ?? mobile_generator_js_1.runMobileGeneratePhase;
310131
311444
  const log2 = (line) => {
310132
311445
  logs2.push(line);
310133
311446
  try {
@@ -310200,6 +311513,7 @@ Explore complete: ${explore.exploreStats.pagesDiscovered} screens, ${explore.exp
310200
311513
  featureName: ctx.featureName,
310201
311514
  existingTestNames: ctx.existingTestNames,
310202
311515
  existingFeatureNames: ctx.existingFeatureNames,
311516
+ recordedJourneys: explore.recordedJourneys,
310203
311517
  collectedPages: screenMap,
310204
311518
  collectedTransitions,
310205
311519
  credentials: {
@@ -310267,9 +311581,7 @@ Plan: ${planResult.scenarios.length} scenarios, ${planResult.featureGroups.lengt
310267
311581
  ctx,
310268
311582
  onLog: (line) => log2(line),
310269
311583
  onAICall,
310270
- onTestGenerated: (test) => {
310271
- callbacks?.onTestGenerated?.(test, { phase: "verified" });
310272
- }
311584
+ onTestGenerated: (test) => callbacks?.onTestGenerated?.(test, { phase: "verified" })
310273
311585
  });
310274
311586
  const generateElapsed = ((Date.now() - startTime) / 1e3).toFixed(1);
310275
311587
  log2(`
@@ -310793,7 +312105,8 @@ Respond with ONLY valid JSON:
310793
312105
  userNotes: ctx.userNotes,
310794
312106
  onAICall,
310795
312107
  onProgress,
310796
- onCredentialUpdate: ctx.onCredentialUpdate
312108
+ onCredentialUpdate: ctx.onCredentialUpdate,
312109
+ getPerceptionPage: () => handle?.page ?? null
310797
312110
  });
310798
312111
  logs2.push(...exploreResult.logs);
310799
312112
  screenshots.push(...exploreResult.screenshots);
@@ -311332,6 +312645,7 @@ Fatal error: ${msg}`);
311332
312645
  onAICall,
311333
312646
  onProgress,
311334
312647
  onCredentialUpdate: ctx.onCredentialUpdate,
312648
+ getPerceptionPage: () => handle?.page ?? null,
311335
312649
  onTestPhaseChange: (phase) => {
311336
312650
  healthObserver?.setTestPhase(phase);
311337
312651
  liveCollector?.setTestPhase(phase);
@@ -323971,6 +325285,8 @@ var require_live_browser_registry = __commonJS({
323971
325285
  exports2.liveBrowserRegistry = void 0;
323972
325286
  var THUMBNAIL_INTERVAL_MS = parseInt(process.env.LIVE_BROWSER_THUMBNAIL_INTERVAL_MS ?? "4000", 10) || 4e3;
323973
325287
  var THUMBNAIL_QUALITY = parseInt(process.env.LIVE_BROWSER_THUMBNAIL_QUALITY ?? "60", 10) || 60;
325288
+ var BROWSER_THUMBNAIL_MAX_BASE64_BYTES = parseInt(process.env.LIVE_BROWSER_THUMBNAIL_MAX_BASE64_BYTES ?? "400000", 10) || 4e5;
325289
+ var MOBILE_THUMBNAIL_MAX_BASE64_BYTES = parseInt(process.env.LIVE_MOBILE_THUMBNAIL_MAX_BASE64_BYTES ?? "2000000", 10) || 2e6;
323974
325290
  var STALE_SESSION_MS = parseInt(process.env.LIVE_BROWSER_STALE_MS ?? `${10 * 60 * 1e3}`, 10) || 10 * 60 * 1e3;
323975
325291
  var MAX_SESSIONS = parseInt(process.env.LIVE_BROWSER_MAX_SESSIONS ?? "32", 10) || 32;
323976
325292
  var LiveBrowserRegistry = class {
@@ -324065,7 +325381,8 @@ var require_live_browser_registry = __commonJS({
324065
325381
  entry.currentUrl = snapshot.currentUrl;
324066
325382
  }
324067
325383
  const thumbnail = snapshot.thumbnailJpegBase64;
324068
- if (thumbnail && thumbnail.length <= 16e4) {
325384
+ const thumbnailLimit = entry.meta.surface === "mobile" ? MOBILE_THUMBNAIL_MAX_BASE64_BYTES : BROWSER_THUMBNAIL_MAX_BASE64_BYTES;
325385
+ if (thumbnail && thumbnail.length <= thumbnailLimit) {
324069
325386
  entry.thumbnailJpegBase64 = thumbnail;
324070
325387
  entry.thumbnailCapturedAt = typeof snapshot.thumbnailCapturedAt === "number" && Number.isFinite(snapshot.thumbnailCapturedAt) ? snapshot.thumbnailCapturedAt : Date.now();
324071
325388
  entry.thumbnailWidth = typeof snapshot.thumbnailWidth === "number" && Number.isFinite(snapshot.thumbnailWidth) ? snapshot.thumbnailWidth : void 0;
@@ -324199,7 +325516,7 @@ var require_live_browser_registry = __commonJS({
324199
325516
  scale: "css"
324200
325517
  });
324201
325518
  const base64 = buf.toString("base64");
324202
- if (base64.length > 16e4) {
325519
+ if (base64.length > BROWSER_THUMBNAIL_MAX_BASE64_BYTES) {
324203
325520
  return;
324204
325521
  }
324205
325522
  entry.thumbnailJpegBase64 = base64;
@@ -324511,6 +325828,7 @@ var require_dist2 = __commonJS({
324511
325828
  return api_call_redaction_js_1.headerVal;
324512
325829
  } });
324513
325830
  __exportStar(require_mcp_healer(), exports2);
325831
+ __exportStar(require_selector_registry_context(), exports2);
324514
325832
  var grok_per_test_healer_js_1 = require_grok_per_test_healer();
324515
325833
  Object.defineProperty(exports2, "executeGrokPerTestHeal", { enumerable: true, get: function() {
324516
325834
  return grok_per_test_healer_js_1.executeGrokPerTestHeal;
@@ -324796,7 +326114,7 @@ var require_package4 = __commonJS({
324796
326114
  "package.json"(exports2, module2) {
324797
326115
  module2.exports = {
324798
326116
  name: "@validate.qa/runner",
324799
- version: "1.0.3",
326117
+ version: "1.0.5",
324800
326118
  description: "validate local test runner - execute Playwright tests on your machine, connected to validate.qa cloud.",
324801
326119
  bin: {
324802
326120
  "validate-runner": "dist/cli.js"
@@ -325468,6 +326786,644 @@ function getRunnerCapabilities() {
325468
326786
  };
325469
326787
  }
325470
326788
 
326789
+ // src/services/mobile/mobile-network-capture.ts
326790
+ var import_node_child_process = require("child_process");
326791
+ var import_node_crypto = require("crypto");
326792
+ var import_promises = require("fs/promises");
326793
+ var import_node_os = require("os");
326794
+ var import_node_path = require("path");
326795
+ var import_node_util = require("util");
326796
+ var import_mockttp = require("mockttp");
326797
+ var import_runner_core7 = __toESM(require_dist2());
326798
+ var execFileAsync = (0, import_node_util.promisify)(import_node_child_process.execFile);
326799
+ var MAX_CALLS = 1e3;
326800
+ var MAX_BODY_CAPTURE_BYTES = 64 * 1024;
326801
+ var DEVICE_CMD_TIMEOUT_MS = 15e3;
326802
+ var CA_KEY_BITS = 2048;
326803
+ var ANDROID_EMULATOR_HOST_LOOPBACK = "10.0.2.2";
326804
+ var PENDING_CLEANUP_DIR = (0, import_node_path.join)((0, import_node_os.tmpdir)(), "validateqa-mobile-capture-pending");
326805
+ function envFlag(name) {
326806
+ const value = (process.env[name] ?? "").trim().toLowerCase();
326807
+ return value === "1" || value === "true" || value === "yes" || value === "on";
326808
+ }
326809
+ function rawHeadersToPairs(rawHeaders) {
326810
+ return rawHeaders.map(([name, value]) => ({ name, value }));
326811
+ }
326812
+ function headerValue(pairs, name) {
326813
+ const lower = name.toLowerCase();
326814
+ return pairs.find((h) => h.name.toLowerCase() === lower)?.value;
326815
+ }
326816
+ function resolveHostIp() {
326817
+ const ifaces = (0, import_node_os.networkInterfaces)();
326818
+ for (const addrs of Object.values(ifaces)) {
326819
+ if (!addrs) continue;
326820
+ for (const addr of addrs) {
326821
+ if (addr.family === "IPv4" && !addr.internal) return addr.address;
326822
+ }
326823
+ }
326824
+ return "127.0.0.1";
326825
+ }
326826
+ function resolveProxyHostForDevice(platform3, isPhysical) {
326827
+ if (platform3 === "ANDROID" && !isPhysical) return ANDROID_EMULATOR_HOST_LOOPBACK;
326828
+ if (platform3 === "IOS" && !isPhysical) return "127.0.0.1";
326829
+ return resolveHostIp();
326830
+ }
326831
+ function normalizeRemoteAddress(addr) {
326832
+ if (!addr) return "";
326833
+ return addr.startsWith("::ffff:") ? addr.slice("::ffff:".length) : addr;
326834
+ }
326835
+ function isLoopbackAddress(addr) {
326836
+ return addr === "127.0.0.1" || addr.startsWith("127.") || addr === "::1" || addr === "";
326837
+ }
326838
+ function isPrivateAddress(addr) {
326839
+ if (/^10\./.test(addr)) return true;
326840
+ if (/^192\.168\./.test(addr)) return true;
326841
+ if (/^172\.(1[6-9]|2\d|3[01])\./.test(addr)) return true;
326842
+ if (/^169\.254\./.test(addr)) return true;
326843
+ const lower = addr.toLowerCase();
326844
+ if (lower.startsWith("fe80:")) return true;
326845
+ if (lower.startsWith("fc") || lower.startsWith("fd")) return true;
326846
+ return false;
326847
+ }
326848
+ function isAllowedProxyPeer(remoteAddress, isPhysical) {
326849
+ const addr = normalizeRemoteAddress(remoteAddress);
326850
+ if (isLoopbackAddress(addr)) return true;
326851
+ return isPhysical && isPrivateAddress(addr);
326852
+ }
326853
+ function installSourceIpGuard(server3, isPhysical, onLog) {
326854
+ const underlying = server3.server;
326855
+ if (!underlying || typeof underlying.on !== "function") {
326856
+ onLog?.(
326857
+ "[mobile-net] WARNING: could not attach source-IP guard to the proxy listener; the port is bound to all interfaces \u2014 firewall it to the device subnet."
326858
+ );
326859
+ return;
326860
+ }
326861
+ underlying.on("connection", (socket) => {
326862
+ if (!isAllowedProxyPeer(socket.remoteAddress, isPhysical)) {
326863
+ onLog?.(`[mobile-net] rejected proxy connection from disallowed source ${socket.remoteAddress}`);
326864
+ socket.destroy();
326865
+ }
326866
+ });
326867
+ }
326868
+ function isTextish(contentType) {
326869
+ if (!contentType) return false;
326870
+ return /json|text|xml|graphql|html|csv|javascript|urlencoded/i.test(contentType);
326871
+ }
326872
+ async function readBodyText(body, contentType) {
326873
+ if (!isTextish(contentType)) return void 0;
326874
+ try {
326875
+ const decoded = await body.getDecodedBuffer();
326876
+ if (!decoded || decoded.length === 0) return void 0;
326877
+ if (decoded.length > MAX_BODY_CAPTURE_BYTES) {
326878
+ return decoded.subarray(0, MAX_BODY_CAPTURE_BYTES).toString("utf-8");
326879
+ }
326880
+ return decoded.toString("utf-8");
326881
+ } catch {
326882
+ return void 0;
326883
+ }
326884
+ }
326885
+ var MitmProxy = class {
326886
+ constructor(caKeyPem, caCertPem, caCertPath, isPhysical, tlsPassthroughAll, onLog) {
326887
+ this.caKeyPem = caKeyPem;
326888
+ this.caCertPem = caCertPem;
326889
+ this.caCertPath = caCertPath;
326890
+ this.isPhysical = isPhysical;
326891
+ this.tlsPassthroughAll = tlsPassthroughAll;
326892
+ this.onLog = onLog;
326893
+ }
326894
+ server = null;
326895
+ inFlight = /* @__PURE__ */ new Map();
326896
+ calls = [];
326897
+ dropped = 0;
326898
+ /** Flips true once any HTTPS response is successfully MITM-ed. */
326899
+ httpsIntercepted = false;
326900
+ /** Best-known reason interception is unavailable, refined by TLS failures. */
326901
+ tlsFailureReason = null;
326902
+ /** Count of opaque HTTPS tunnels when TLS passthrough is enabled. */
326903
+ tlsPassthroughCount = 0;
326904
+ /** Start listening; resolves with the actual bound port. */
326905
+ async listen(preferredPort) {
326906
+ const server3 = (0, import_mockttp.getLocal)({
326907
+ // Our generated CA — mockttp mints per-host leaf certs signed by it.
326908
+ https: {
326909
+ key: this.caKeyPem,
326910
+ cert: this.caCertPem,
326911
+ ...this.tlsPassthroughAll ? { tlsPassthrough: [{ hostname: "*" }] } : {}
326912
+ },
326913
+ // HTTP/2 with HTTP/1.1 fallback; mobile clients negotiate either.
326914
+ http2: "fallback",
326915
+ // Keep the proxy quiet unless explicitly debugging.
326916
+ recordTraffic: false
326917
+ });
326918
+ await server3.forAnyRequest().thenPassThrough({ ignoreHostHttpsErrors: true });
326919
+ await server3.on("request", (req) => this.onRequest(req));
326920
+ await server3.on("response", (res) => this.onResponse(res));
326921
+ await server3.on("tls-client-error", (failure) => this.onTlsClientError(failure));
326922
+ await server3.on("tls-passthrough-opened", (event) => this.onTlsPassthroughOpened(event));
326923
+ await (preferredPort > 0 ? server3.start(preferredPort) : server3.start());
326924
+ installSourceIpGuard(server3, this.isPhysical, this.onLog);
326925
+ this.server = server3;
326926
+ return server3.port;
326927
+ }
326928
+ /** Captured calls, finalized through the shared redaction constructor. */
326929
+ finalize() {
326930
+ return this.calls.map(
326931
+ (c) => (0, import_runner_core7.buildApiCallSummary)({
326932
+ method: c.method,
326933
+ url: c.url,
326934
+ status: c.status,
326935
+ durationMs: c.durationMs,
326936
+ authHeader: c.authHeader,
326937
+ cookieHeader: c.cookieHeader,
326938
+ requestContentType: c.requestContentType,
326939
+ responseContentType: c.responseContentType,
326940
+ respHeaders: c.respHeaders,
326941
+ reqBodyText: c.reqBodyText,
326942
+ respBodyText: c.respBodyText,
326943
+ responseSize: c.responseSize,
326944
+ timestamp: c.startedAt,
326945
+ testPhase: "UNKNOWN"
326946
+ })
326947
+ );
326948
+ }
326949
+ /** True once at least one HTTPS exchange was decrypted (CA trusted). */
326950
+ get isIntercepting() {
326951
+ return this.httpsIntercepted;
326952
+ }
326953
+ /** A precise reason interception is (still) unavailable, or null if it works. */
326954
+ get interceptionReason() {
326955
+ if (this.httpsIntercepted) return null;
326956
+ if (this.tlsPassthroughAll) {
326957
+ return "HTTPS TLS passthrough active: HTTPS bodies are not decrypted, but app traffic continues. Set VALIDATEQA_MOBILE_FORCE_TLS_MITM=1 for debug apps/devices that trust the validate.qa CA" + (this.caCertPath ? ` at ${this.caCertPath}.` : ".");
326958
+ }
326959
+ return this.tlsFailureReason;
326960
+ }
326961
+ async close() {
326962
+ const server3 = this.server;
326963
+ this.server = null;
326964
+ this.inFlight.clear();
326965
+ if (!server3) return;
326966
+ try {
326967
+ await server3.stop();
326968
+ } catch (err) {
326969
+ this.onLog?.(`[mobile-net] proxy stop error (ignored): ${errMsg(err)}`);
326970
+ }
326971
+ this.onLog?.(
326972
+ `[mobile-net] proxy closed: ${this.calls.length} calls captured` + (this.dropped > 0 ? ` (${this.dropped} dropped over cap)` : "")
326973
+ );
326974
+ }
326975
+ // ── request → buffer by id ────────────────────────────────────────────────
326976
+ async onRequest(req) {
326977
+ try {
326978
+ const reqHeaders = rawHeadersToPairs(req.rawHeaders);
326979
+ const requestContentType = headerValue(reqHeaders, "content-type");
326980
+ const isHttps = isHttpsUrl(req.url);
326981
+ const reqBodyText = (0, import_runner_core7.isApiCall)(req.url, requestContentType) ? await readBodyText(req.body, requestContentType) : void 0;
326982
+ this.inFlight.set(req.id, {
326983
+ method: req.method,
326984
+ url: req.url,
326985
+ startedAt: req.timingEvents.startTime,
326986
+ reqHeaders,
326987
+ requestContentType,
326988
+ authHeader: headerValue(reqHeaders, "authorization"),
326989
+ cookieHeader: headerValue(reqHeaders, "cookie"),
326990
+ reqBodyText,
326991
+ isHttps
326992
+ });
326993
+ } catch (err) {
326994
+ this.onLog?.(`[mobile-net] request capture error (ignored): ${errMsg(err)}`);
326995
+ }
326996
+ }
326997
+ // ── response → finalize the matched request ───────────────────────────────
326998
+ async onResponse(res) {
326999
+ const pending = this.inFlight.get(res.id);
327000
+ if (!pending) return;
327001
+ this.inFlight.delete(res.id);
327002
+ try {
327003
+ if (pending.isHttps && !this.httpsIntercepted) {
327004
+ this.httpsIntercepted = true;
327005
+ this.tlsFailureReason = null;
327006
+ this.onLog?.("[mobile-net] HTTPS interception confirmed (CA trusted; bodies decrypted).");
327007
+ }
327008
+ const respHeaders = rawHeadersToPairs(res.rawHeaders);
327009
+ const responseContentType = headerValue(respHeaders, "content-type");
327010
+ const api = (0, import_runner_core7.isApiCall)(pending.url, responseContentType);
327011
+ if (!api && (0, import_runner_core7.isStaticAssetByExt)(pending.url)) return;
327012
+ if (this.calls.length >= MAX_CALLS) {
327013
+ this.dropped++;
327014
+ return;
327015
+ }
327016
+ const respBodyText = api ? await readBodyText(res.body, responseContentType) : void 0;
327017
+ const responseSize = await measureBody(res.body);
327018
+ const durationMs = computeDuration(pending.startedAt, res);
327019
+ this.calls.push({
327020
+ method: pending.method,
327021
+ url: pending.url,
327022
+ status: res.statusCode,
327023
+ startedAt: pending.startedAt,
327024
+ durationMs,
327025
+ reqHeaders: pending.reqHeaders,
327026
+ respHeaders,
327027
+ requestContentType: pending.requestContentType,
327028
+ responseContentType,
327029
+ authHeader: pending.authHeader,
327030
+ cookieHeader: pending.cookieHeader,
327031
+ reqBodyText: pending.reqBodyText,
327032
+ respBodyText,
327033
+ responseSize,
327034
+ intercepted: true
327035
+ });
327036
+ } catch (err) {
327037
+ this.onLog?.(`[mobile-net] response capture error (ignored): ${errMsg(err)}`);
327038
+ }
327039
+ }
327040
+ // ── tls-client-error → record WHY interception failed ─────────────────────
327041
+ onTlsClientError(failure) {
327042
+ if (this.httpsIntercepted) return;
327043
+ const host = failure.destination?.hostname ?? failure.tlsMetadata.sniHostname ?? "unknown host";
327044
+ const trustHint = this.caCertPath ? `Trust the validate.qa CA at ${this.caCertPath} to enable HTTPS body capture.` : "Trust the generated validate.qa CA to enable HTTPS body capture.";
327045
+ let reason;
327046
+ switch (failure.failureCause) {
327047
+ case "cert-rejected":
327048
+ reason = `TLS handshake to ${host} rejected our certificate: the device does not trust the validate.qa CA, so HTTPS bodies cannot be decrypted (plaintext HTTP still captured). ` + trustHint;
327049
+ break;
327050
+ case "closed":
327051
+ case "reset":
327052
+ reason = `TLS handshake to ${host} was dropped after our certificate was offered \u2014 this is the signature of certificate pinning in the app. HTTPS bodies for pinned hosts cannot be decrypted (plaintext HTTP and connect-level host metadata still captured).`;
327053
+ break;
327054
+ case "no-shared-cipher":
327055
+ reason = `TLS handshake to ${host} failed: no shared cipher with the client.`;
327056
+ break;
327057
+ case "handshake-timeout":
327058
+ reason = `TLS handshake to ${host} timed out before completing.`;
327059
+ break;
327060
+ default:
327061
+ reason = `TLS handshake to ${host} failed (${failure.failureCause}); HTTPS not intercepted for it.`;
327062
+ }
327063
+ if (!this.tlsFailureReason || failure.failureCause === "cert-rejected" || failure.failureCause === "closed" || failure.failureCause === "reset") {
327064
+ this.tlsFailureReason = reason;
327065
+ }
327066
+ this.onLog?.(`[mobile-net] ${reason}`);
327067
+ }
327068
+ onTlsPassthroughOpened(event) {
327069
+ this.tlsPassthroughCount++;
327070
+ if (this.tlsPassthroughCount === 1) {
327071
+ const host = event.destination?.hostname ?? event.hostname ?? "unknown host";
327072
+ this.onLog?.(
327073
+ `[mobile-net] HTTPS TLS passthrough active for ${host}; app traffic is preserved, but HTTPS bodies are not decrypted.`
327074
+ );
327075
+ }
327076
+ }
327077
+ };
327078
+ function isHttpsUrl(url) {
327079
+ try {
327080
+ return new URL(url).protocol === "https:";
327081
+ } catch {
327082
+ return false;
327083
+ }
327084
+ }
327085
+ async function measureBody(body) {
327086
+ try {
327087
+ const decoded = await body.getDecodedBuffer();
327088
+ return decoded ? decoded.length : 0;
327089
+ } catch {
327090
+ return 0;
327091
+ }
327092
+ }
327093
+ function computeDuration(startedAt, res) {
327094
+ const sent = res.timingEvents.responseSentTimestamp;
327095
+ const start = res.timingEvents.startTimestamp;
327096
+ if (typeof sent === "number" && typeof start === "number" && sent >= start) {
327097
+ return Math.round(sent - start);
327098
+ }
327099
+ const elapsed = Date.now() - startedAt;
327100
+ return elapsed > 0 ? elapsed : 0;
327101
+ }
327102
+ async function generateCaCert(dir, onLog) {
327103
+ try {
327104
+ const { key, cert } = await (0, import_mockttp.generateCACertificate)({
327105
+ subject: {
327106
+ commonName: "validate.qa Mobile Capture CA",
327107
+ organizationName: "validate.qa"
327108
+ },
327109
+ bits: CA_KEY_BITS
327110
+ // mockttp's generated CA carries its own (multi-year) validity window; we
327111
+ // rely on stop() to remove the cert from the device promptly after the run
327112
+ // rather than on a short expiry.
327113
+ });
327114
+ const caCertPath = (0, import_node_path.join)(dir, "validateqa-mobile-ca.pem");
327115
+ await (0, import_promises.writeFile)(caCertPath, cert, "utf-8");
327116
+ return { caCertPath, caKeyPem: key, caCertPem: cert };
327117
+ } catch (err) {
327118
+ onLog?.(`[mobile-net] CA generation failed; running plaintext-only: ${errMsg(err)}`);
327119
+ return null;
327120
+ }
327121
+ }
327122
+ async function androidReadProxy(deviceId) {
327123
+ try {
327124
+ const { stdout } = await execFileAsync(
327125
+ "adb",
327126
+ ["-s", deviceId, "shell", "settings", "get", "global", "http_proxy"],
327127
+ { timeout: DEVICE_CMD_TIMEOUT_MS }
327128
+ );
327129
+ const value = stdout.trim();
327130
+ return value === "" || value === "null" ? null : value;
327131
+ } catch {
327132
+ return null;
327133
+ }
327134
+ }
327135
+ async function androidSetProxy(deviceId, hostPort, onLog) {
327136
+ try {
327137
+ await execFileAsync(
327138
+ "adb",
327139
+ ["-s", deviceId, "shell", "settings", "put", "global", "http_proxy", hostPort],
327140
+ { timeout: DEVICE_CMD_TIMEOUT_MS }
327141
+ );
327142
+ return true;
327143
+ } catch (err) {
327144
+ onLog?.(`[mobile-net] adb set http_proxy failed: ${errMsg(err)}`);
327145
+ return false;
327146
+ }
327147
+ }
327148
+ async function androidClearProxy(deviceId, originalProxy, onLog) {
327149
+ const restoreValue = originalProxy && originalProxy.length > 0 ? originalProxy : ":0";
327150
+ try {
327151
+ await execFileAsync(
327152
+ "adb",
327153
+ ["-s", deviceId, "shell", "settings", "put", "global", "http_proxy", restoreValue],
327154
+ { timeout: DEVICE_CMD_TIMEOUT_MS }
327155
+ );
327156
+ } catch (err) {
327157
+ onLog?.(`[mobile-net] adb restore http_proxy failed: ${errMsg(err)}`);
327158
+ }
327159
+ }
327160
+ async function androidInstallCa(deviceId, caCertPath) {
327161
+ const devicePath = "/sdcard/validateqa-mobile-ca.pem";
327162
+ try {
327163
+ await execFileAsync("adb", ["-s", deviceId, "push", caCertPath, devicePath], {
327164
+ timeout: DEVICE_CMD_TIMEOUT_MS
327165
+ });
327166
+ return {
327167
+ installed: false,
327168
+ note: `CA copied from ${caCertPath} to ${devicePath} but NOT auto-trusted: Android 7+ apps must opt into user CAs (network_security_config), and the system store requires a rooted/writable image. HTTPS bodies decrypt only for apps that trust user CAs; pinned apps never decrypt.`
327169
+ };
327170
+ } catch (err) {
327171
+ return { installed: false, note: `CA push failed: ${errMsg(err)}` };
327172
+ }
327173
+ }
327174
+ async function androidRemoveCa(deviceId, onLog) {
327175
+ try {
327176
+ await execFileAsync(
327177
+ "adb",
327178
+ ["-s", deviceId, "shell", "rm", "-f", "/sdcard/validateqa-mobile-ca.pem"],
327179
+ { timeout: DEVICE_CMD_TIMEOUT_MS }
327180
+ );
327181
+ } catch (err) {
327182
+ onLog?.(`[mobile-net] adb remove CA failed: ${errMsg(err)}`);
327183
+ }
327184
+ }
327185
+ async function iosSimTrustCa(deviceId, caCertPath) {
327186
+ try {
327187
+ await execFileAsync("xcrun", ["simctl", "keychain", deviceId, "add-root-cert", caCertPath], {
327188
+ timeout: DEVICE_CMD_TIMEOUT_MS
327189
+ });
327190
+ return {
327191
+ trusted: true,
327192
+ note: "CA added to simulator keychain (add-root-cert). NOTE: the simulator has no per-app HTTP proxy setting controllable via simctl \u2014 it shares the host network, so only traffic that actually routes through the proxy is intercepted; pinned apps still never decrypt."
327193
+ };
327194
+ } catch (err) {
327195
+ return { trusted: false, note: `simctl add-root-cert failed: ${errMsg(err)}` };
327196
+ }
327197
+ }
327198
+ async function iosSimRemoveCa(deviceId, onLog) {
327199
+ if (process.env.VALIDATEQA_MOBILE_SIM_KEYCHAIN_RESET === "1") {
327200
+ try {
327201
+ await execFileAsync("xcrun", ["simctl", "keychain", deviceId, "reset"], {
327202
+ timeout: DEVICE_CMD_TIMEOUT_MS
327203
+ });
327204
+ onLog?.(`[mobile-net] simctl keychain reset run for ${deviceId} (opt-in; cleared ALL sim certs).`);
327205
+ } catch (err) {
327206
+ onLog?.(`[mobile-net] simctl keychain reset failed: ${errMsg(err)}`);
327207
+ }
327208
+ return;
327209
+ }
327210
+ onLog?.(
327211
+ `[mobile-net] NOTE: the validate.qa capture CA remains TRUSTED in simulator ${deviceId}. simctl has no scoped remove (only a full keychain reset that wipes ALL certs), so we leave the single CA in place. Use a throwaway simulator, or set VALIDATEQA_MOBILE_SIM_KEYCHAIN_RESET=1 to reset the whole sim keychain on teardown.`
327212
+ );
327213
+ }
327214
+ function errMsg(err) {
327215
+ return err instanceof Error ? err.message : String(err);
327216
+ }
327217
+ var activeCleanups = /* @__PURE__ */ new Map();
327218
+ var exitHandlersInstalled = false;
327219
+ function markerPath(sessionKey) {
327220
+ return (0, import_node_path.join)(PENDING_CLEANUP_DIR, `${sessionKey}.json`);
327221
+ }
327222
+ async function writePendingCleanup(sessionKey, marker) {
327223
+ try {
327224
+ await (0, import_promises.mkdir)(PENDING_CLEANUP_DIR, { recursive: true });
327225
+ await (0, import_promises.writeFile)(markerPath(sessionKey), JSON.stringify(marker), "utf-8");
327226
+ } catch {
327227
+ }
327228
+ }
327229
+ async function removePendingCleanup(sessionKey) {
327230
+ try {
327231
+ await (0, import_promises.unlink)(markerPath(sessionKey));
327232
+ } catch {
327233
+ }
327234
+ }
327235
+ function restoreDeviceSync(marker, onLog) {
327236
+ const run2 = (args) => {
327237
+ try {
327238
+ (0, import_node_child_process.execFileSync)("adb", args, { timeout: DEVICE_CMD_TIMEOUT_MS, stdio: "ignore" });
327239
+ } catch {
327240
+ }
327241
+ };
327242
+ if (marker.platform === "ANDROID") {
327243
+ if (marker.proxySet) {
327244
+ const restoreValue = marker.originalProxy && marker.originalProxy.length > 0 ? marker.originalProxy : ":0";
327245
+ run2(["-s", marker.deviceId, "shell", "settings", "put", "global", "http_proxy", restoreValue]);
327246
+ }
327247
+ if (marker.caInstalledOnDevice) {
327248
+ run2(["-s", marker.deviceId, "shell", "rm", "-f", "/sdcard/validateqa-mobile-ca.pem"]);
327249
+ }
327250
+ }
327251
+ onLog?.(`[mobile-net] restored device ${marker.deviceId} from pending-cleanup marker.`);
327252
+ }
327253
+ async function reconcilePendingCleanups(onLog) {
327254
+ let files;
327255
+ try {
327256
+ files = await (0, import_promises.readdir)(PENDING_CLEANUP_DIR);
327257
+ } catch {
327258
+ return;
327259
+ }
327260
+ for (const file of files) {
327261
+ if (!file.endsWith(".json")) continue;
327262
+ const full = (0, import_node_path.join)(PENDING_CLEANUP_DIR, file);
327263
+ try {
327264
+ const raw = await (0, import_promises.readFile)(full, "utf-8");
327265
+ const marker = JSON.parse(raw);
327266
+ if (marker && typeof marker.deviceId === "string" && marker.platform) {
327267
+ onLog?.(`[mobile-net] reconciling orphaned capture cleanup for ${marker.deviceId}.`);
327268
+ restoreDeviceSync(marker, onLog);
327269
+ }
327270
+ } catch {
327271
+ }
327272
+ try {
327273
+ await (0, import_promises.unlink)(full);
327274
+ } catch {
327275
+ }
327276
+ }
327277
+ }
327278
+ function ensureExitHandlers() {
327279
+ if (exitHandlersInstalled) return;
327280
+ exitHandlersInstalled = true;
327281
+ const drain = () => {
327282
+ for (const marker of activeCleanups.values()) {
327283
+ restoreDeviceSync(marker);
327284
+ }
327285
+ };
327286
+ process.on("beforeExit", drain);
327287
+ process.on("exit", drain);
327288
+ for (const signal of ["SIGTERM", "SIGINT"]) {
327289
+ process.on(signal, () => {
327290
+ drain();
327291
+ process.removeAllListeners(signal);
327292
+ process.kill(process.pid, signal);
327293
+ });
327294
+ }
327295
+ }
327296
+ async function startMobileNetworkCapture(options) {
327297
+ const { platform: platform3, deviceId, isPhysical = false, preferredPort = 0, onLog } = options;
327298
+ const forceTlsMitm = options.forceTlsMitm ?? envFlag("VALIDATEQA_MOBILE_FORCE_TLS_MITM");
327299
+ const tlsPassthroughAll = !forceTlsMitm && (platform3 === "ANDROID" || platform3 === "IOS" && isPhysical);
327300
+ await reconcilePendingCleanups(onLog);
327301
+ ensureExitHandlers();
327302
+ const sessionKey = (0, import_node_crypto.randomUUID)();
327303
+ const originalProxy = platform3 === "ANDROID" ? await androidReadProxy(deviceId) : null;
327304
+ let tmpDir = null;
327305
+ let caCertPath = null;
327306
+ let caKeyPem = null;
327307
+ let caCertPem = null;
327308
+ try {
327309
+ tmpDir = await (0, import_promises.mkdtemp)((0, import_node_path.join)((0, import_node_os.tmpdir)(), "validateqa-mobile-ca-"));
327310
+ const ca = await generateCaCert(tmpDir, onLog);
327311
+ if (ca) {
327312
+ caCertPath = ca.caCertPath;
327313
+ caKeyPem = ca.caKeyPem;
327314
+ caCertPem = ca.caCertPem;
327315
+ }
327316
+ } catch (err) {
327317
+ onLog?.(`[mobile-net] CA setup degraded: ${errMsg(err)}`);
327318
+ }
327319
+ if (!caKeyPem || !caCertPem) {
327320
+ try {
327321
+ const fallback = await (0, import_mockttp.generateCACertificate)();
327322
+ caKeyPem = fallback.key;
327323
+ caCertPem = fallback.cert;
327324
+ } catch (err) {
327325
+ onLog?.(`[mobile-net] fatal CA failure, cannot start proxy: ${errMsg(err)}`);
327326
+ if (tmpDir) {
327327
+ await (0, import_promises.rm)(tmpDir, { recursive: true, force: true }).catch(() => void 0);
327328
+ }
327329
+ throw err instanceof Error ? err : new Error(String(err));
327330
+ }
327331
+ }
327332
+ const proxy = new MitmProxy(caKeyPem, caCertPem, caCertPath, isPhysical, tlsPassthroughAll, onLog);
327333
+ const proxyPort = await proxy.listen(preferredPort);
327334
+ const proxyHost = resolveProxyHostForDevice(platform3, isPhysical);
327335
+ const hostPort = `${proxyHost}:${proxyPort}`;
327336
+ onLog?.(
327337
+ `[mobile-net] ${tlsPassthroughAll ? "capture proxy" : "MITM proxy"} listening; device target ${hostPort} (${platform3}${isPhysical ? " physical" : ""})`
327338
+ );
327339
+ let wiringReason = "HTTPS interception pending: capture begins once the device routes a TLS handshake through the proxy.";
327340
+ let proxySet = false;
327341
+ let caInstalledOnDevice = false;
327342
+ try {
327343
+ if (platform3 === "ANDROID") {
327344
+ proxySet = await androidSetProxy(deviceId, hostPort, onLog);
327345
+ if (!proxySet) {
327346
+ wiringReason = "adb proxy configuration failed; no device traffic will be routed through the proxy.";
327347
+ } else if (tlsPassthroughAll) {
327348
+ wiringReason = "Android HTTPS is tunneled without MITM by default because release apps usually do not trust user-installed CAs. Plaintext HTTP is still captured; HTTPS bodies are not decrypted. For debug apps/devices that trust the CA, set VALIDATEQA_MOBILE_FORCE_TLS_MITM=1" + (caCertPath ? ` and trust ${caCertPath}.` : ".");
327349
+ } else if (caCertPath) {
327350
+ const { note } = await androidInstallCa(deviceId, caCertPath);
327351
+ caInstalledOnDevice = true;
327352
+ wiringReason = note;
327353
+ }
327354
+ } else {
327355
+ if (!isPhysical && caCertPath) {
327356
+ const { trusted, note } = await iosSimTrustCa(deviceId, caCertPath);
327357
+ caInstalledOnDevice = trusted;
327358
+ wiringReason = note;
327359
+ } else if (isPhysical) {
327360
+ wiringReason = "Physical iOS HTTPS is tunneled without MITM by default. Set the Wi-Fi HTTP proxy and trust the CA manually, then run with VALIDATEQA_MOBILE_FORCE_TLS_MITM=1 to decrypt HTTPS bodies" + (caCertPath ? ` (${caCertPath}).` : ".");
327361
+ }
327362
+ }
327363
+ } catch (err) {
327364
+ wiringReason = `device wiring degraded: ${errMsg(err)}`;
327365
+ onLog?.(`[mobile-net] ${wiringReason}`);
327366
+ }
327367
+ if (proxySet || caInstalledOnDevice) {
327368
+ const marker = {
327369
+ platform: platform3,
327370
+ deviceId,
327371
+ isPhysical,
327372
+ proxySet,
327373
+ originalProxy,
327374
+ caInstalledOnDevice
327375
+ };
327376
+ activeCleanups.set(sessionKey, marker);
327377
+ await writePendingCleanup(sessionKey, marker);
327378
+ }
327379
+ const capturedTmpDir = tmpDir;
327380
+ const capturedCaCertPath = caCertPath;
327381
+ let stopped = false;
327382
+ let finalCalls = [];
327383
+ const getCapabilities = () => {
327384
+ if (proxy.isIntercepting) return { intercepting: true };
327385
+ const reason = proxy.interceptionReason ?? wiringReason;
327386
+ return reason ? { intercepting: false, reason } : { intercepting: false };
327387
+ };
327388
+ const stop = async () => {
327389
+ if (stopped) return finalCalls;
327390
+ stopped = true;
327391
+ if (proxySet && platform3 === "ANDROID") {
327392
+ await androidClearProxy(deviceId, originalProxy, onLog);
327393
+ }
327394
+ if (caInstalledOnDevice && capturedCaCertPath) {
327395
+ if (platform3 === "ANDROID") {
327396
+ await androidRemoveCa(deviceId, onLog);
327397
+ } else if (!isPhysical) {
327398
+ await iosSimRemoveCa(deviceId, onLog);
327399
+ }
327400
+ }
327401
+ activeCleanups.delete(sessionKey);
327402
+ await removePendingCleanup(sessionKey);
327403
+ finalCalls = proxy.finalize();
327404
+ await proxy.close();
327405
+ if (capturedTmpDir) {
327406
+ try {
327407
+ await (0, import_promises.rm)(capturedTmpDir, { recursive: true, force: true });
327408
+ } catch (err) {
327409
+ onLog?.(`[mobile-net] temp CA cleanup failed: ${errMsg(err)}`);
327410
+ }
327411
+ }
327412
+ const apiCount = finalCalls.filter((c) => c.isApi).length;
327413
+ onLog?.(
327414
+ `[mobile-net] stopped: ${finalCalls.length} calls captured (${apiCount} API; HTTPS interception ${proxy.isIntercepting ? "ACTIVE" : "inactive"}).`
327415
+ );
327416
+ return finalCalls;
327417
+ };
327418
+ return {
327419
+ proxyPort,
327420
+ proxyHost,
327421
+ caCertPath: capturedCaCertPath,
327422
+ getCapabilities,
327423
+ stop
327424
+ };
327425
+ }
327426
+
325471
327427
  // src/services/appium-executor.ts
325472
327428
  var DEFAULT_STEP_TIMEOUT_MS = 1e4;
325473
327429
  var WebDriverTransportError = class extends Error {
@@ -325853,7 +327809,24 @@ async function executeMobileTest(options) {
325853
327809
  const startTime = Date.now();
325854
327810
  const stepResults = [];
325855
327811
  const screenshots = [];
327812
+ let apiCalls = [];
325856
327813
  let sessionId = null;
327814
+ let capture = null;
327815
+ let captureStopped = false;
327816
+ const stopCapture = async () => {
327817
+ if (!capture || captureStopped) return apiCalls;
327818
+ captureStopped = true;
327819
+ try {
327820
+ const captured = await capture.stop();
327821
+ apiCalls = captured.map((call) => ({
327822
+ ...call,
327823
+ pageUrl: call.pageUrl ?? `mobile-test:${options.target.appId}`
327824
+ }));
327825
+ } catch (err) {
327826
+ log2(`[mobile-net] capture stop failed: ${err instanceof Error ? err.message : String(err)}`);
327827
+ }
327828
+ return apiCalls;
327829
+ };
325857
327830
  try {
325858
327831
  log2("Checking Appium server health...");
325859
327832
  const health = await checkAppiumHealth();
@@ -325881,6 +327854,21 @@ async function executeMobileTest(options) {
325881
327854
  selectedDevice = matchingDevices[0];
325882
327855
  }
325883
327856
  log2(`Using device: ${selectedDevice.name} (${selectedDevice.id})`);
327857
+ try {
327858
+ capture = await startMobileNetworkCapture({
327859
+ platform: options.target.platform,
327860
+ deviceId: selectedDevice.id,
327861
+ appId: options.target.appId,
327862
+ isPhysical: selectedDevice.isPhysical,
327863
+ onLog: (line) => log2(line)
327864
+ });
327865
+ const caps = capture.getCapabilities();
327866
+ if (!caps.intercepting) {
327867
+ log2(`[mobile-net] HTTPS interception unavailable: ${caps.reason ?? "unknown reason"} (continuing - plaintext/metadata only)`);
327868
+ }
327869
+ } catch (err) {
327870
+ log2(`[mobile-net] capture unavailable: ${err instanceof Error ? err.message : String(err)} (continuing without network log)`);
327871
+ }
325884
327872
  if (selectedDevice.installedApps && selectedDevice.installedApps.length > 0) {
325885
327873
  if (!selectedDevice.installedApps.includes(options.target.appId)) {
325886
327874
  throw new Error(
@@ -325990,10 +327978,12 @@ async function executeMobileTest(options) {
325990
327978
  }
325991
327979
  const allPassed = !environmental && stepResults.every((r) => r.status === "passed" || r.status === "skipped");
325992
327980
  const firstError = environmental ?? (stepResults.find((r) => r.status === "failed")?.error ?? null);
327981
+ await stopCapture();
325993
327982
  return {
325994
327983
  passed: allPassed,
325995
327984
  stepResults,
325996
327985
  screenshots,
327986
+ apiCalls,
325997
327987
  logs: logLines.join("\n"),
325998
327988
  error: firstError,
325999
327989
  duration: Date.now() - startTime,
@@ -326002,10 +327992,12 @@ async function executeMobileTest(options) {
326002
327992
  } catch (err) {
326003
327993
  const errorMsg = err instanceof Error ? err.message : String(err);
326004
327994
  log2(`Fatal error: ${errorMsg}`);
327995
+ await stopCapture();
326005
327996
  return {
326006
327997
  passed: false,
326007
327998
  stepResults,
326008
327999
  screenshots,
328000
+ apiCalls,
326009
328001
  logs: logLines.join("\n"),
326010
328002
  error: errorMsg,
326011
328003
  duration: Date.now() - startTime,
@@ -326015,6 +328007,7 @@ async function executeMobileTest(options) {
326015
328007
  environmental: errorMsg
326016
328008
  };
326017
328009
  } finally {
328010
+ await stopCapture();
326018
328011
  if (sessionId) {
326019
328012
  log2("Tearing down Appium session...");
326020
328013
  await deleteSession(sessionId);
@@ -326022,7 +328015,7 @@ async function executeMobileTest(options) {
326022
328015
  }
326023
328016
  }
326024
328017
  }
326025
- async function createMobileDiscoverySession(target) {
328018
+ async function createMobileDiscoverySession(target, options) {
326026
328019
  const health = await checkAppiumHealth();
326027
328020
  if (!health.ok) {
326028
328021
  await startAppiumServer();
@@ -326049,6 +328042,12 @@ async function createMobileDiscoverySession(target) {
326049
328042
  `App ${target.appId} not installed on ${selectedDevice.name}. Install it first.`
326050
328043
  );
326051
328044
  }
328045
+ await options?.onDeviceSelected?.({
328046
+ deviceId: selectedDevice.id,
328047
+ name: selectedDevice.name,
328048
+ isPhysical: selectedDevice.isPhysical === true,
328049
+ platformVersion: selectedDevice.platformVersion
328050
+ });
326052
328051
  const sessionId = await createSession({
326053
328052
  appId: target.appId,
326054
328053
  platform: target.platform,
@@ -326314,6 +328313,7 @@ var polling = false;
326314
328313
  var sessionStartInFlight = null;
326315
328314
  var sessionReaperTimer = null;
326316
328315
  var mirrorClientsEmptyAt = null;
328316
+ var mirrorSessionOwner = null;
326317
328317
  var executorBusy = false;
326318
328318
  var rateLimitBuckets = /* @__PURE__ */ new Map();
326319
328319
  function isRateLimited(clientIp) {
@@ -326650,6 +328650,7 @@ async function startSession(validated, res) {
326650
328650
  }
326651
328651
  state.appiumSessionId = null;
326652
328652
  state.platform = null;
328653
+ mirrorSessionOwner = null;
326653
328654
  }
326654
328655
  stopAllMirrorClients();
326655
328656
  const realCaps = isPhysicalDevice(deviceId, targetPlatform) ? realDeviceCapabilities2(targetPlatform) : {};
@@ -326680,11 +328681,14 @@ async function startSession(validated, res) {
326680
328681
  })
326681
328682
  });
326682
328683
  state.appiumSessionId = result?.value?.sessionId ?? result?.sessionId ?? null;
326683
- state.platform = targetPlatform === "IOS" ? "IOS" : "ANDROID";
326684
328684
  if (!state.appiumSessionId) {
328685
+ state.platform = null;
328686
+ mirrorSessionOwner = null;
326685
328687
  sendJSON(res, 500, { error: "Appium session creation returned no session ID" });
326686
328688
  return;
326687
328689
  }
328690
+ state.platform = targetPlatform === "IOS" ? "IOS" : "ANDROID";
328691
+ mirrorSessionOwner = "recording";
326688
328692
  if (launchMode === "DEEP_LINK" && deeplink) {
326689
328693
  try {
326690
328694
  const args = targetPlatform === "IOS" ? [{ url: deeplink, bundleId: appId }] : [{ url: deeplink, package: appId }];
@@ -326709,6 +328713,7 @@ async function handleSessionStop(_req, res) {
326709
328713
  }
326710
328714
  state.appiumSessionId = null;
326711
328715
  state.platform = null;
328716
+ mirrorSessionOwner = null;
326712
328717
  }
326713
328718
  stopAllMirrorClients();
326714
328719
  stopSessionReaper();
@@ -326789,6 +328794,7 @@ async function pollAndBroadcastFrame() {
326789
328794
  if (!alive) {
326790
328795
  state.appiumSessionId = null;
326791
328796
  state.platform = null;
328797
+ mirrorSessionOwner = null;
326792
328798
  stopAllMirrorClients();
326793
328799
  return;
326794
328800
  }
@@ -326859,6 +328865,7 @@ async function reapIdleSession() {
326859
328865
  const sessionId = state.appiumSessionId;
326860
328866
  state.appiumSessionId = null;
326861
328867
  state.platform = null;
328868
+ mirrorSessionOwner = null;
326862
328869
  mirrorClientsEmptyAt = null;
326863
328870
  stopSessionReaper();
326864
328871
  try {
@@ -327169,6 +329176,7 @@ async function stopMobileBridge() {
327169
329176
  }
327170
329177
  state.appiumSessionId = null;
327171
329178
  state.platform = null;
329179
+ mirrorSessionOwner = null;
327172
329180
  }
327173
329181
  if (ngrokProcess) {
327174
329182
  try {
@@ -327199,16 +329207,40 @@ function getBridgeState() {
327199
329207
  tunnelUrl: state.tunnelUrl,
327200
329208
  tunnelToken: state.tunnelToken,
327201
329209
  hasActiveSession: !!state.appiumSessionId,
327202
- recordingActive: !!state.appiumSessionId,
329210
+ recordingActive: !!state.appiumSessionId && mirrorSessionOwner === "recording",
327203
329211
  platform: state.platform
327204
329212
  };
327205
329213
  }
327206
329214
  function setExecutorBusy(busy) {
327207
329215
  executorBusy = busy;
327208
329216
  }
329217
+ function attachMirrorSession(sessionId, platform3) {
329218
+ validateSessionId(sessionId);
329219
+ if (state.appiumSessionId && state.appiumSessionId !== sessionId) {
329220
+ stopAllMirrorClients();
329221
+ }
329222
+ state.appiumSessionId = sessionId;
329223
+ state.platform = platform3;
329224
+ mirrorSessionOwner = "runner";
329225
+ mirrorNullTicks = 0;
329226
+ mirrorClientsEmptyAt = null;
329227
+ ensureSessionReaper();
329228
+ if (state.mirrorClients.size > 0) {
329229
+ ensureMirrorPolling();
329230
+ void pollAndBroadcastFrame();
329231
+ }
329232
+ }
329233
+ function detachMirrorSession(sessionId) {
329234
+ if (sessionId && state.appiumSessionId && state.appiumSessionId !== sessionId) return;
329235
+ state.appiumSessionId = null;
329236
+ state.platform = null;
329237
+ mirrorSessionOwner = null;
329238
+ stopAllMirrorClients();
329239
+ stopSessionReaper();
329240
+ }
327209
329241
 
327210
329242
  // src/services/mobile/mobile-bridge-driver.ts
327211
- var import_runner_core7 = __toESM(require_dist2());
329243
+ var import_runner_core8 = __toESM(require_dist2());
327212
329244
  init_dist();
327213
329245
  var APPIUM_SESSION_ID_PATTERN2 = /^[a-f0-9-]{1,64}$/i;
327214
329246
  function readStringValue(result) {
@@ -327218,6 +329250,15 @@ function readStringValue(result) {
327218
329250
  }
327219
329251
  return void 0;
327220
329252
  }
329253
+ function normalizeAndroidActivity(activity, pkg2) {
329254
+ const trimmed = activity?.trim();
329255
+ if (!trimmed) return void 0;
329256
+ const packageName = pkg2?.trim();
329257
+ if (!packageName) return trimmed;
329258
+ if (trimmed.startsWith(".")) return `${packageName}${trimmed}`;
329259
+ if (!trimmed.includes(".")) return `${packageName}.${trimmed}`;
329260
+ return trimmed;
329261
+ }
327221
329262
  var MobileBridgeDriver = class {
327222
329263
  sessionId;
327223
329264
  platform;
@@ -327244,6 +329285,38 @@ var MobileBridgeDriver = class {
327244
329285
  appLifecycleArgs() {
327245
329286
  return this.platform === "ANDROID" ? { appId: this.appId } : { bundleId: this.appId };
327246
329287
  }
329288
+ baseScreenSignal(source) {
329289
+ const parsed = (0, import_runner_core8.parseMobileSource)(source, this.platform);
329290
+ return {
329291
+ platform: this.platform,
329292
+ screenHash: parsed.screenSignal.screenHash,
329293
+ ...parsed.screenSignal.activity ? { activity: parsed.screenSignal.activity } : {},
329294
+ ...parsed.screenSignal.bundleId ? { bundleId: parsed.screenSignal.bundleId } : {}
329295
+ };
329296
+ }
329297
+ async enrichScreenSignal(source) {
329298
+ const signal = this.baseScreenSignal(source);
329299
+ try {
329300
+ if (this.platform === "ANDROID") {
329301
+ const [activityRes, packageRes] = await Promise.all([
329302
+ this.executeScript("mobile: getCurrentActivity", []).catch(() => void 0),
329303
+ this.executeScript("mobile: getCurrentPackage", []).catch(() => void 0)
329304
+ ]);
329305
+ const activity = readStringValue(activityRes);
329306
+ const pkg2 = readStringValue(packageRes);
329307
+ if (pkg2) signal.bundleId = pkg2;
329308
+ const normalizedActivity = normalizeAndroidActivity(activity, pkg2);
329309
+ if (normalizedActivity) signal.activity = normalizedActivity;
329310
+ } else {
329311
+ const infoRes = await this.executeScript("mobile: activeAppInfo", []).catch(() => void 0);
329312
+ const bundleId = readActiveBundleId(infoRes);
329313
+ if (bundleId) signal.bundleId = bundleId;
329314
+ }
329315
+ } catch {
329316
+ }
329317
+ if (!signal.bundleId) signal.bundleId = this.appId;
329318
+ return signal;
329319
+ }
327247
329320
  /**
327248
329321
  * Run a `mobile:` script via execute/sync. Used by the NEW lifecycle
327249
329322
  * primitives (terminateApp/activateApp/deepLink/getCurrentActivity) that have
@@ -327258,9 +329331,7 @@ var MobileBridgeDriver = class {
327258
329331
  // ── Observation ────────────────────────────────────────
327259
329332
  /**
327260
329333
  * One round-trip observation: UI-tree XML + base64 screenshot + window size,
327261
- * plus a best-effort screen signal derived from the parsed XML (cheap — no
327262
- * extra device round-trip; the activity/bundleId enrichment is left to
327263
- * getScreenSignal()).
329334
+ * plus a best-effort screen signal enriched with the foreground app identity.
327264
329335
  */
327265
329336
  async snapshot() {
327266
329337
  const [xml, screenshot, windowSize] = await Promise.all([
@@ -327269,10 +329340,7 @@ var MobileBridgeDriver = class {
327269
329340
  getWindowSize2(this.sessionPath)
327270
329341
  ]);
327271
329342
  const source = xml ?? "";
327272
- const screenSignal = {
327273
- platform: this.platform,
327274
- screenHash: (0, import_runner_core7.parseMobileSource)(source, this.platform).screenSignal.screenHash
327275
- };
329343
+ const screenSignal = await this.enrichScreenSignal(source);
327276
329344
  return {
327277
329345
  xml: source,
327278
329346
  screenshot,
@@ -327301,7 +329369,8 @@ var MobileBridgeDriver = class {
327301
329369
  getScreenshot(this.sessionPath),
327302
329370
  getPageSource(this.sessionPath)
327303
329371
  ]);
327304
- return { ok: true, screenshot, source };
329372
+ const screenSignal = await this.enrichScreenSignal(source ?? "");
329373
+ return { ok: true, screenshot, source, screenSignal };
327305
329374
  }
327306
329375
  // ── Action primitives (1:1 with the bridge handleAction verbs) ──
327307
329376
  /** Center-tap the given bounds (coordinate-only; the loop always supplies bounds). */
@@ -327379,27 +329448,7 @@ var MobileBridgeDriver = class {
327379
329448
  */
327380
329449
  async getScreenSignal() {
327381
329450
  const source = await getPageSource(this.sessionPath) ?? "";
327382
- const screenHash = (0, import_runner_core7.parseMobileSource)(source, this.platform).screenSignal.screenHash;
327383
- const signal = { platform: this.platform, screenHash };
327384
- try {
327385
- if (this.platform === "ANDROID") {
327386
- const [activityRes, packageRes] = await Promise.all([
327387
- this.executeScript("mobile: getCurrentActivity", []).catch(() => void 0),
327388
- this.executeScript("mobile: getCurrentPackage", []).catch(() => void 0)
327389
- ]);
327390
- const activity = readStringValue(activityRes);
327391
- const pkg2 = readStringValue(packageRes);
327392
- if (activity) signal.activity = activity;
327393
- signal.bundleId = pkg2 ?? this.appId;
327394
- } else {
327395
- const infoRes = await this.executeScript("mobile: activeAppInfo", []).catch(() => void 0);
327396
- const bundleId = readActiveBundleId(infoRes);
327397
- signal.bundleId = bundleId ?? this.appId;
327398
- }
327399
- } catch {
327400
- if (!signal.bundleId) signal.bundleId = this.appId;
327401
- }
327402
- return signal;
329451
+ return this.enrichScreenSignal(source);
327403
329452
  }
327404
329453
  };
327405
329454
  function readActiveBundleId(result) {
@@ -327413,614 +329462,6 @@ function readActiveBundleId(result) {
327413
329462
  return void 0;
327414
329463
  }
327415
329464
 
327416
- // src/services/mobile/mobile-network-capture.ts
327417
- var import_node_child_process = require("child_process");
327418
- var import_node_crypto = require("crypto");
327419
- var import_promises = require("fs/promises");
327420
- var import_node_os = require("os");
327421
- var import_node_path = require("path");
327422
- var import_node_util = require("util");
327423
- var import_mockttp = require("mockttp");
327424
- var import_runner_core8 = __toESM(require_dist2());
327425
- var execFileAsync = (0, import_node_util.promisify)(import_node_child_process.execFile);
327426
- var MAX_CALLS = 1e3;
327427
- var MAX_BODY_CAPTURE_BYTES = 64 * 1024;
327428
- var DEVICE_CMD_TIMEOUT_MS = 15e3;
327429
- var CA_KEY_BITS = 2048;
327430
- var ANDROID_EMULATOR_HOST_LOOPBACK = "10.0.2.2";
327431
- var PENDING_CLEANUP_DIR = (0, import_node_path.join)((0, import_node_os.tmpdir)(), "validateqa-mobile-capture-pending");
327432
- function rawHeadersToPairs(rawHeaders) {
327433
- return rawHeaders.map(([name, value]) => ({ name, value }));
327434
- }
327435
- function headerValue(pairs, name) {
327436
- const lower = name.toLowerCase();
327437
- return pairs.find((h) => h.name.toLowerCase() === lower)?.value;
327438
- }
327439
- function resolveHostIp() {
327440
- const ifaces = (0, import_node_os.networkInterfaces)();
327441
- for (const addrs of Object.values(ifaces)) {
327442
- if (!addrs) continue;
327443
- for (const addr of addrs) {
327444
- if (addr.family === "IPv4" && !addr.internal) return addr.address;
327445
- }
327446
- }
327447
- return "127.0.0.1";
327448
- }
327449
- function resolveProxyHostForDevice(platform3, isPhysical) {
327450
- if (platform3 === "ANDROID" && !isPhysical) return ANDROID_EMULATOR_HOST_LOOPBACK;
327451
- if (platform3 === "IOS" && !isPhysical) return "127.0.0.1";
327452
- return resolveHostIp();
327453
- }
327454
- function normalizeRemoteAddress(addr) {
327455
- if (!addr) return "";
327456
- return addr.startsWith("::ffff:") ? addr.slice("::ffff:".length) : addr;
327457
- }
327458
- function isLoopbackAddress(addr) {
327459
- return addr === "127.0.0.1" || addr.startsWith("127.") || addr === "::1" || addr === "";
327460
- }
327461
- function isPrivateAddress(addr) {
327462
- if (/^10\./.test(addr)) return true;
327463
- if (/^192\.168\./.test(addr)) return true;
327464
- if (/^172\.(1[6-9]|2\d|3[01])\./.test(addr)) return true;
327465
- if (/^169\.254\./.test(addr)) return true;
327466
- const lower = addr.toLowerCase();
327467
- if (lower.startsWith("fe80:")) return true;
327468
- if (lower.startsWith("fc") || lower.startsWith("fd")) return true;
327469
- return false;
327470
- }
327471
- function isAllowedProxyPeer(remoteAddress, isPhysical) {
327472
- const addr = normalizeRemoteAddress(remoteAddress);
327473
- if (isLoopbackAddress(addr)) return true;
327474
- return isPhysical && isPrivateAddress(addr);
327475
- }
327476
- function installSourceIpGuard(server3, isPhysical, onLog) {
327477
- const underlying = server3.server;
327478
- if (!underlying || typeof underlying.on !== "function") {
327479
- onLog?.(
327480
- "[mobile-net] WARNING: could not attach source-IP guard to the proxy listener; the port is bound to all interfaces \u2014 firewall it to the device subnet."
327481
- );
327482
- return;
327483
- }
327484
- underlying.on("connection", (socket) => {
327485
- if (!isAllowedProxyPeer(socket.remoteAddress, isPhysical)) {
327486
- onLog?.(`[mobile-net] rejected proxy connection from disallowed source ${socket.remoteAddress}`);
327487
- socket.destroy();
327488
- }
327489
- });
327490
- }
327491
- function isTextish(contentType) {
327492
- if (!contentType) return false;
327493
- return /json|text|xml|graphql|html|csv|javascript|urlencoded/i.test(contentType);
327494
- }
327495
- async function readBodyText(body, contentType) {
327496
- if (!isTextish(contentType)) return void 0;
327497
- try {
327498
- const decoded = await body.getDecodedBuffer();
327499
- if (!decoded || decoded.length === 0) return void 0;
327500
- if (decoded.length > MAX_BODY_CAPTURE_BYTES) {
327501
- return decoded.subarray(0, MAX_BODY_CAPTURE_BYTES).toString("utf-8");
327502
- }
327503
- return decoded.toString("utf-8");
327504
- } catch {
327505
- return void 0;
327506
- }
327507
- }
327508
- var MitmProxy = class {
327509
- constructor(caKeyPem, caCertPem, isPhysical, onLog) {
327510
- this.caKeyPem = caKeyPem;
327511
- this.caCertPem = caCertPem;
327512
- this.isPhysical = isPhysical;
327513
- this.onLog = onLog;
327514
- }
327515
- server = null;
327516
- inFlight = /* @__PURE__ */ new Map();
327517
- calls = [];
327518
- dropped = 0;
327519
- /** Flips true once any HTTPS response is successfully MITM-ed. */
327520
- httpsIntercepted = false;
327521
- /** Best-known reason interception is unavailable, refined by TLS failures. */
327522
- tlsFailureReason = null;
327523
- /** Start listening; resolves with the actual bound port. */
327524
- async listen(preferredPort) {
327525
- const server3 = (0, import_mockttp.getLocal)({
327526
- // Our generated CA — mockttp mints per-host leaf certs signed by it.
327527
- https: { key: this.caKeyPem, cert: this.caCertPem },
327528
- // HTTP/2 with HTTP/1.1 fallback; mobile clients negotiate either.
327529
- http2: "fallback",
327530
- // Keep the proxy quiet unless explicitly debugging.
327531
- recordTraffic: false
327532
- });
327533
- await server3.forAnyRequest().thenPassThrough({ ignoreHostHttpsErrors: true });
327534
- await server3.on("request", (req) => this.onRequest(req));
327535
- await server3.on("response", (res) => this.onResponse(res));
327536
- await server3.on("tls-client-error", (failure) => this.onTlsClientError(failure));
327537
- await (preferredPort > 0 ? server3.start(preferredPort) : server3.start());
327538
- installSourceIpGuard(server3, this.isPhysical, this.onLog);
327539
- this.server = server3;
327540
- return server3.port;
327541
- }
327542
- /** Captured calls, finalized through the shared redaction constructor. */
327543
- finalize() {
327544
- return this.calls.map(
327545
- (c) => (0, import_runner_core8.buildApiCallSummary)({
327546
- method: c.method,
327547
- url: c.url,
327548
- status: c.status,
327549
- durationMs: c.durationMs,
327550
- authHeader: c.authHeader,
327551
- cookieHeader: c.cookieHeader,
327552
- requestContentType: c.requestContentType,
327553
- responseContentType: c.responseContentType,
327554
- respHeaders: c.respHeaders,
327555
- reqBodyText: c.reqBodyText,
327556
- respBodyText: c.respBodyText,
327557
- responseSize: c.responseSize,
327558
- timestamp: c.startedAt,
327559
- testPhase: "UNKNOWN"
327560
- })
327561
- );
327562
- }
327563
- /** True once at least one HTTPS exchange was decrypted (CA trusted). */
327564
- get isIntercepting() {
327565
- return this.httpsIntercepted;
327566
- }
327567
- /** A precise reason interception is (still) unavailable, or null if it works. */
327568
- get interceptionReason() {
327569
- if (this.httpsIntercepted) return null;
327570
- return this.tlsFailureReason;
327571
- }
327572
- async close() {
327573
- const server3 = this.server;
327574
- this.server = null;
327575
- this.inFlight.clear();
327576
- if (!server3) return;
327577
- try {
327578
- await server3.stop();
327579
- } catch (err) {
327580
- this.onLog?.(`[mobile-net] proxy stop error (ignored): ${errMsg(err)}`);
327581
- }
327582
- this.onLog?.(
327583
- `[mobile-net] proxy closed: ${this.calls.length} calls captured` + (this.dropped > 0 ? ` (${this.dropped} dropped over cap)` : "")
327584
- );
327585
- }
327586
- // ── request → buffer by id ────────────────────────────────────────────────
327587
- async onRequest(req) {
327588
- try {
327589
- const reqHeaders = rawHeadersToPairs(req.rawHeaders);
327590
- const requestContentType = headerValue(reqHeaders, "content-type");
327591
- const isHttps = isHttpsUrl(req.url);
327592
- const reqBodyText = (0, import_runner_core8.isApiCall)(req.url, requestContentType) ? await readBodyText(req.body, requestContentType) : void 0;
327593
- this.inFlight.set(req.id, {
327594
- method: req.method,
327595
- url: req.url,
327596
- startedAt: req.timingEvents.startTime,
327597
- reqHeaders,
327598
- requestContentType,
327599
- authHeader: headerValue(reqHeaders, "authorization"),
327600
- cookieHeader: headerValue(reqHeaders, "cookie"),
327601
- reqBodyText,
327602
- isHttps
327603
- });
327604
- } catch (err) {
327605
- this.onLog?.(`[mobile-net] request capture error (ignored): ${errMsg(err)}`);
327606
- }
327607
- }
327608
- // ── response → finalize the matched request ───────────────────────────────
327609
- async onResponse(res) {
327610
- const pending = this.inFlight.get(res.id);
327611
- if (!pending) return;
327612
- this.inFlight.delete(res.id);
327613
- try {
327614
- if (pending.isHttps && !this.httpsIntercepted) {
327615
- this.httpsIntercepted = true;
327616
- this.tlsFailureReason = null;
327617
- this.onLog?.("[mobile-net] HTTPS interception confirmed (CA trusted; bodies decrypted).");
327618
- }
327619
- const respHeaders = rawHeadersToPairs(res.rawHeaders);
327620
- const responseContentType = headerValue(respHeaders, "content-type");
327621
- const api = (0, import_runner_core8.isApiCall)(pending.url, responseContentType);
327622
- if (!api && (0, import_runner_core8.isStaticAssetByExt)(pending.url)) return;
327623
- if (this.calls.length >= MAX_CALLS) {
327624
- this.dropped++;
327625
- return;
327626
- }
327627
- const respBodyText = api ? await readBodyText(res.body, responseContentType) : void 0;
327628
- const responseSize = await measureBody(res.body);
327629
- const durationMs = computeDuration(pending.startedAt, res);
327630
- this.calls.push({
327631
- method: pending.method,
327632
- url: pending.url,
327633
- status: res.statusCode,
327634
- startedAt: pending.startedAt,
327635
- durationMs,
327636
- reqHeaders: pending.reqHeaders,
327637
- respHeaders,
327638
- requestContentType: pending.requestContentType,
327639
- responseContentType,
327640
- authHeader: pending.authHeader,
327641
- cookieHeader: pending.cookieHeader,
327642
- reqBodyText: pending.reqBodyText,
327643
- respBodyText,
327644
- responseSize,
327645
- intercepted: true
327646
- });
327647
- } catch (err) {
327648
- this.onLog?.(`[mobile-net] response capture error (ignored): ${errMsg(err)}`);
327649
- }
327650
- }
327651
- // ── tls-client-error → record WHY interception failed ─────────────────────
327652
- onTlsClientError(failure) {
327653
- if (this.httpsIntercepted) return;
327654
- const host = failure.destination?.hostname ?? failure.tlsMetadata.sniHostname ?? "unknown host";
327655
- let reason;
327656
- switch (failure.failureCause) {
327657
- case "cert-rejected":
327658
- reason = `TLS handshake to ${host} rejected our certificate: the device does not trust the validate.qa CA, so HTTPS bodies cannot be decrypted (plaintext HTTP still captured). Install + trust the CA at caCertPath to enable interception.`;
327659
- break;
327660
- case "closed":
327661
- case "reset":
327662
- reason = `TLS handshake to ${host} was dropped after our certificate was offered \u2014 this is the signature of certificate pinning in the app. HTTPS bodies for pinned hosts cannot be decrypted (plaintext HTTP and connect-level host metadata still captured).`;
327663
- break;
327664
- case "no-shared-cipher":
327665
- reason = `TLS handshake to ${host} failed: no shared cipher with the client.`;
327666
- break;
327667
- case "handshake-timeout":
327668
- reason = `TLS handshake to ${host} timed out before completing.`;
327669
- break;
327670
- default:
327671
- reason = `TLS handshake to ${host} failed (${failure.failureCause}); HTTPS not intercepted for it.`;
327672
- }
327673
- if (!this.tlsFailureReason || failure.failureCause === "cert-rejected" || failure.failureCause === "closed" || failure.failureCause === "reset") {
327674
- this.tlsFailureReason = reason;
327675
- }
327676
- this.onLog?.(`[mobile-net] ${reason}`);
327677
- }
327678
- };
327679
- function isHttpsUrl(url) {
327680
- try {
327681
- return new URL(url).protocol === "https:";
327682
- } catch {
327683
- return false;
327684
- }
327685
- }
327686
- async function measureBody(body) {
327687
- try {
327688
- const decoded = await body.getDecodedBuffer();
327689
- return decoded ? decoded.length : 0;
327690
- } catch {
327691
- return 0;
327692
- }
327693
- }
327694
- function computeDuration(startedAt, res) {
327695
- const sent = res.timingEvents.responseSentTimestamp;
327696
- const start = res.timingEvents.startTimestamp;
327697
- if (typeof sent === "number" && typeof start === "number" && sent >= start) {
327698
- return Math.round(sent - start);
327699
- }
327700
- const elapsed = Date.now() - startedAt;
327701
- return elapsed > 0 ? elapsed : 0;
327702
- }
327703
- async function generateCaCert(dir, onLog) {
327704
- try {
327705
- const { key, cert } = await (0, import_mockttp.generateCACertificate)({
327706
- subject: {
327707
- commonName: "validate.qa Mobile Capture CA",
327708
- organizationName: "validate.qa"
327709
- },
327710
- bits: CA_KEY_BITS
327711
- // mockttp's generated CA carries its own (multi-year) validity window; we
327712
- // rely on stop() to remove the cert from the device promptly after the run
327713
- // rather than on a short expiry.
327714
- });
327715
- const caCertPath = (0, import_node_path.join)(dir, "validateqa-mobile-ca.pem");
327716
- await (0, import_promises.writeFile)(caCertPath, cert, "utf-8");
327717
- return { caCertPath, caKeyPem: key, caCertPem: cert };
327718
- } catch (err) {
327719
- onLog?.(`[mobile-net] CA generation failed; running plaintext-only: ${errMsg(err)}`);
327720
- return null;
327721
- }
327722
- }
327723
- async function androidReadProxy(deviceId) {
327724
- try {
327725
- const { stdout } = await execFileAsync(
327726
- "adb",
327727
- ["-s", deviceId, "shell", "settings", "get", "global", "http_proxy"],
327728
- { timeout: DEVICE_CMD_TIMEOUT_MS }
327729
- );
327730
- const value = stdout.trim();
327731
- return value === "" || value === "null" ? null : value;
327732
- } catch {
327733
- return null;
327734
- }
327735
- }
327736
- async function androidSetProxy(deviceId, hostPort, onLog) {
327737
- try {
327738
- await execFileAsync(
327739
- "adb",
327740
- ["-s", deviceId, "shell", "settings", "put", "global", "http_proxy", hostPort],
327741
- { timeout: DEVICE_CMD_TIMEOUT_MS }
327742
- );
327743
- return true;
327744
- } catch (err) {
327745
- onLog?.(`[mobile-net] adb set http_proxy failed: ${errMsg(err)}`);
327746
- return false;
327747
- }
327748
- }
327749
- async function androidClearProxy(deviceId, originalProxy, onLog) {
327750
- const restoreValue = originalProxy && originalProxy.length > 0 ? originalProxy : ":0";
327751
- try {
327752
- await execFileAsync(
327753
- "adb",
327754
- ["-s", deviceId, "shell", "settings", "put", "global", "http_proxy", restoreValue],
327755
- { timeout: DEVICE_CMD_TIMEOUT_MS }
327756
- );
327757
- } catch (err) {
327758
- onLog?.(`[mobile-net] adb restore http_proxy failed: ${errMsg(err)}`);
327759
- }
327760
- }
327761
- async function androidInstallCa(deviceId, caCertPath) {
327762
- const devicePath = "/sdcard/validateqa-mobile-ca.pem";
327763
- try {
327764
- await execFileAsync("adb", ["-s", deviceId, "push", caCertPath, devicePath], {
327765
- timeout: DEVICE_CMD_TIMEOUT_MS
327766
- });
327767
- return {
327768
- installed: false,
327769
- note: "CA pushed to /sdcard but NOT auto-trusted: Android 7+ apps must opt into user CAs (network_security_config), and the system store requires a rooted/writable image. HTTPS bodies decrypt only for apps that trust user CAs; pinned apps never decrypt."
327770
- };
327771
- } catch (err) {
327772
- return { installed: false, note: `CA push failed: ${errMsg(err)}` };
327773
- }
327774
- }
327775
- async function androidRemoveCa(deviceId, onLog) {
327776
- try {
327777
- await execFileAsync(
327778
- "adb",
327779
- ["-s", deviceId, "shell", "rm", "-f", "/sdcard/validateqa-mobile-ca.pem"],
327780
- { timeout: DEVICE_CMD_TIMEOUT_MS }
327781
- );
327782
- } catch (err) {
327783
- onLog?.(`[mobile-net] adb remove CA failed: ${errMsg(err)}`);
327784
- }
327785
- }
327786
- async function iosSimTrustCa(deviceId, caCertPath) {
327787
- try {
327788
- await execFileAsync("xcrun", ["simctl", "keychain", deviceId, "add-root-cert", caCertPath], {
327789
- timeout: DEVICE_CMD_TIMEOUT_MS
327790
- });
327791
- return {
327792
- trusted: true,
327793
- note: "CA added to simulator keychain (add-root-cert). NOTE: the simulator has no per-app HTTP proxy setting controllable via simctl \u2014 it shares the host network, so only traffic that actually routes through the proxy is intercepted; pinned apps still never decrypt."
327794
- };
327795
- } catch (err) {
327796
- return { trusted: false, note: `simctl add-root-cert failed: ${errMsg(err)}` };
327797
- }
327798
- }
327799
- async function iosSimRemoveCa(deviceId, onLog) {
327800
- if (process.env.VALIDATEQA_MOBILE_SIM_KEYCHAIN_RESET === "1") {
327801
- try {
327802
- await execFileAsync("xcrun", ["simctl", "keychain", deviceId, "reset"], {
327803
- timeout: DEVICE_CMD_TIMEOUT_MS
327804
- });
327805
- onLog?.(`[mobile-net] simctl keychain reset run for ${deviceId} (opt-in; cleared ALL sim certs).`);
327806
- } catch (err) {
327807
- onLog?.(`[mobile-net] simctl keychain reset failed: ${errMsg(err)}`);
327808
- }
327809
- return;
327810
- }
327811
- onLog?.(
327812
- `[mobile-net] NOTE: the validate.qa capture CA remains TRUSTED in simulator ${deviceId}. simctl has no scoped remove (only a full keychain reset that wipes ALL certs), so we leave the single CA in place. Use a throwaway simulator, or set VALIDATEQA_MOBILE_SIM_KEYCHAIN_RESET=1 to reset the whole sim keychain on teardown.`
327813
- );
327814
- }
327815
- function errMsg(err) {
327816
- return err instanceof Error ? err.message : String(err);
327817
- }
327818
- var activeCleanups = /* @__PURE__ */ new Map();
327819
- var exitHandlersInstalled = false;
327820
- function markerPath(sessionKey) {
327821
- return (0, import_node_path.join)(PENDING_CLEANUP_DIR, `${sessionKey}.json`);
327822
- }
327823
- async function writePendingCleanup(sessionKey, marker) {
327824
- try {
327825
- await (0, import_promises.mkdir)(PENDING_CLEANUP_DIR, { recursive: true });
327826
- await (0, import_promises.writeFile)(markerPath(sessionKey), JSON.stringify(marker), "utf-8");
327827
- } catch {
327828
- }
327829
- }
327830
- async function removePendingCleanup(sessionKey) {
327831
- try {
327832
- await (0, import_promises.unlink)(markerPath(sessionKey));
327833
- } catch {
327834
- }
327835
- }
327836
- function restoreDeviceSync(marker, onLog) {
327837
- const run2 = (args) => {
327838
- try {
327839
- (0, import_node_child_process.execFileSync)("adb", args, { timeout: DEVICE_CMD_TIMEOUT_MS, stdio: "ignore" });
327840
- } catch {
327841
- }
327842
- };
327843
- if (marker.platform === "ANDROID") {
327844
- if (marker.proxySet) {
327845
- const restoreValue = marker.originalProxy && marker.originalProxy.length > 0 ? marker.originalProxy : ":0";
327846
- run2(["-s", marker.deviceId, "shell", "settings", "put", "global", "http_proxy", restoreValue]);
327847
- }
327848
- if (marker.caInstalledOnDevice) {
327849
- run2(["-s", marker.deviceId, "shell", "rm", "-f", "/sdcard/validateqa-mobile-ca.pem"]);
327850
- }
327851
- }
327852
- onLog?.(`[mobile-net] restored device ${marker.deviceId} from pending-cleanup marker.`);
327853
- }
327854
- async function reconcilePendingCleanups(onLog) {
327855
- let files;
327856
- try {
327857
- files = await (0, import_promises.readdir)(PENDING_CLEANUP_DIR);
327858
- } catch {
327859
- return;
327860
- }
327861
- for (const file of files) {
327862
- if (!file.endsWith(".json")) continue;
327863
- const full = (0, import_node_path.join)(PENDING_CLEANUP_DIR, file);
327864
- try {
327865
- const raw = await (0, import_promises.readFile)(full, "utf-8");
327866
- const marker = JSON.parse(raw);
327867
- if (marker && typeof marker.deviceId === "string" && marker.platform) {
327868
- onLog?.(`[mobile-net] reconciling orphaned capture cleanup for ${marker.deviceId}.`);
327869
- restoreDeviceSync(marker, onLog);
327870
- }
327871
- } catch {
327872
- }
327873
- try {
327874
- await (0, import_promises.unlink)(full);
327875
- } catch {
327876
- }
327877
- }
327878
- }
327879
- function ensureExitHandlers() {
327880
- if (exitHandlersInstalled) return;
327881
- exitHandlersInstalled = true;
327882
- const drain = () => {
327883
- for (const marker of activeCleanups.values()) {
327884
- restoreDeviceSync(marker);
327885
- }
327886
- };
327887
- process.on("beforeExit", drain);
327888
- process.on("exit", drain);
327889
- for (const signal of ["SIGTERM", "SIGINT"]) {
327890
- process.on(signal, () => {
327891
- drain();
327892
- process.removeAllListeners(signal);
327893
- process.kill(process.pid, signal);
327894
- });
327895
- }
327896
- }
327897
- async function startMobileNetworkCapture(options) {
327898
- const { platform: platform3, deviceId, isPhysical = false, preferredPort = 0, onLog } = options;
327899
- await reconcilePendingCleanups(onLog);
327900
- ensureExitHandlers();
327901
- const sessionKey = (0, import_node_crypto.randomUUID)();
327902
- const originalProxy = platform3 === "ANDROID" ? await androidReadProxy(deviceId) : null;
327903
- let tmpDir = null;
327904
- let caCertPath = null;
327905
- let caKeyPem = null;
327906
- let caCertPem = null;
327907
- try {
327908
- tmpDir = await (0, import_promises.mkdtemp)((0, import_node_path.join)((0, import_node_os.tmpdir)(), "validateqa-mobile-ca-"));
327909
- const ca = await generateCaCert(tmpDir, onLog);
327910
- if (ca) {
327911
- caCertPath = ca.caCertPath;
327912
- caKeyPem = ca.caKeyPem;
327913
- caCertPem = ca.caCertPem;
327914
- }
327915
- } catch (err) {
327916
- onLog?.(`[mobile-net] CA setup degraded: ${errMsg(err)}`);
327917
- }
327918
- if (!caKeyPem || !caCertPem) {
327919
- try {
327920
- const fallback = await (0, import_mockttp.generateCACertificate)();
327921
- caKeyPem = fallback.key;
327922
- caCertPem = fallback.cert;
327923
- } catch (err) {
327924
- onLog?.(`[mobile-net] fatal CA failure, cannot start proxy: ${errMsg(err)}`);
327925
- if (tmpDir) {
327926
- await (0, import_promises.rm)(tmpDir, { recursive: true, force: true }).catch(() => void 0);
327927
- }
327928
- throw err instanceof Error ? err : new Error(String(err));
327929
- }
327930
- }
327931
- const proxy = new MitmProxy(caKeyPem, caCertPem, isPhysical, onLog);
327932
- const proxyPort = await proxy.listen(preferredPort);
327933
- const proxyHost = resolveProxyHostForDevice(platform3, isPhysical);
327934
- const hostPort = `${proxyHost}:${proxyPort}`;
327935
- onLog?.(
327936
- `[mobile-net] MITM proxy listening; device target ${hostPort} (${platform3}${isPhysical ? " physical" : ""})`
327937
- );
327938
- let wiringReason = "HTTPS interception pending: capture begins once the device routes a TLS handshake through the proxy.";
327939
- let proxySet = false;
327940
- let caInstalledOnDevice = false;
327941
- try {
327942
- if (platform3 === "ANDROID") {
327943
- proxySet = await androidSetProxy(deviceId, hostPort, onLog);
327944
- if (!proxySet) {
327945
- wiringReason = "adb proxy configuration failed; no device traffic will be routed through the proxy.";
327946
- } else if (caCertPath) {
327947
- const { note } = await androidInstallCa(deviceId, caCertPath);
327948
- caInstalledOnDevice = true;
327949
- wiringReason = note;
327950
- }
327951
- } else {
327952
- if (!isPhysical && caCertPath) {
327953
- const { trusted, note } = await iosSimTrustCa(deviceId, caCertPath);
327954
- caInstalledOnDevice = trusted;
327955
- wiringReason = note;
327956
- } else if (isPhysical) {
327957
- wiringReason = "Physical iOS device: set the Wi-Fi HTTP proxy + trust the CA in Settings \u2192 General \u2192 About \u2192 Certificate Trust Settings manually; capture is limited to whatever routes through.";
327958
- }
327959
- }
327960
- } catch (err) {
327961
- wiringReason = `device wiring degraded: ${errMsg(err)}`;
327962
- onLog?.(`[mobile-net] ${wiringReason}`);
327963
- }
327964
- if (proxySet || caInstalledOnDevice) {
327965
- const marker = {
327966
- platform: platform3,
327967
- deviceId,
327968
- isPhysical,
327969
- proxySet,
327970
- originalProxy,
327971
- caInstalledOnDevice
327972
- };
327973
- activeCleanups.set(sessionKey, marker);
327974
- await writePendingCleanup(sessionKey, marker);
327975
- }
327976
- const capturedTmpDir = tmpDir;
327977
- const capturedCaCertPath = caCertPath;
327978
- let stopped = false;
327979
- let finalCalls = [];
327980
- const getCapabilities = () => {
327981
- if (proxy.isIntercepting) return { intercepting: true };
327982
- const reason = proxy.interceptionReason ?? wiringReason;
327983
- return reason ? { intercepting: false, reason } : { intercepting: false };
327984
- };
327985
- const stop = async () => {
327986
- if (stopped) return finalCalls;
327987
- stopped = true;
327988
- if (proxySet && platform3 === "ANDROID") {
327989
- await androidClearProxy(deviceId, originalProxy, onLog);
327990
- }
327991
- if (caInstalledOnDevice && capturedCaCertPath) {
327992
- if (platform3 === "ANDROID") {
327993
- await androidRemoveCa(deviceId, onLog);
327994
- } else if (!isPhysical) {
327995
- await iosSimRemoveCa(deviceId, onLog);
327996
- }
327997
- }
327998
- activeCleanups.delete(sessionKey);
327999
- await removePendingCleanup(sessionKey);
328000
- finalCalls = proxy.finalize();
328001
- await proxy.close();
328002
- if (capturedTmpDir) {
328003
- try {
328004
- await (0, import_promises.rm)(capturedTmpDir, { recursive: true, force: true });
328005
- } catch (err) {
328006
- onLog?.(`[mobile-net] temp CA cleanup failed: ${errMsg(err)}`);
328007
- }
328008
- }
328009
- const apiCount = finalCalls.filter((c) => c.isApi).length;
328010
- onLog?.(
328011
- `[mobile-net] stopped: ${finalCalls.length} calls captured (${apiCount} API; HTTPS interception ${proxy.isIntercepting ? "ACTIVE" : "inactive"}).`
328012
- );
328013
- return finalCalls;
328014
- };
328015
- return {
328016
- proxyPort,
328017
- proxyHost,
328018
- caCertPath: capturedCaCertPath,
328019
- getCapabilities,
328020
- stop
328021
- };
328022
- }
328023
-
328024
329465
  // src/services/doctor.ts
328025
329466
  var import_child_process4 = require("child_process");
328026
329467
  var import_fs3 = require("fs");
@@ -328238,7 +329679,20 @@ function printDoctorReport(report) {
328238
329679
  init_dist();
328239
329680
  var import_runner_core11 = __toESM(require_dist2());
328240
329681
  function fingerprintTest(test) {
328241
- return (0, import_node_crypto2.createHash)("sha256").update(test.name).update("\n--suite--\n").update(test.suiteName ?? "").update("\n--type--\n").update(test.testType ?? "").update("\n--code--\n").update(test.playwrightCode).digest("hex").slice(0, 24);
329682
+ const code = test.framework === "APPIUM" ? [test.appiumCode ?? "", JSON.stringify(test.steps ?? [])].join("\n--steps--\n") : test.playwrightCode ?? "";
329683
+ return (0, import_node_crypto2.createHash)("sha256").update(test.name).update("\n--suite--\n").update(test.suiteName ?? "").update("\n--type--\n").update(test.testType ?? "").update("\n--framework--\n").update(test.framework ?? "").update("\n--code--\n").update(code).digest("hex").slice(0, 24);
329684
+ }
329685
+ function mobileDiscoveryNetworkContext(result, appId) {
329686
+ const page = result?.collectedPages?.find((p) => p.screenId || p.route);
329687
+ const screenId = page?.screenId ?? page?.route;
329688
+ return screenId ? `mobile:${screenId}` : `mobile:${appId}`;
329689
+ }
329690
+ function attachMobileDiscoveryNetworkContext(calls, result, appId) {
329691
+ const pageUrl = mobileDiscoveryNetworkContext(result, appId);
329692
+ return calls.map((call) => ({
329693
+ ...call,
329694
+ pageUrl: call.pageUrl ?? pageUrl
329695
+ }));
328242
329696
  }
328243
329697
  var SECRET_VAR_PATTERN = /password|secret|token|api[_]?key|pass\b|credential|bearer|pat\b/i;
328244
329698
  var IS_CLOUD = process.env.RUNNER_CLOUD === "true";
@@ -329198,6 +330652,7 @@ ${finalVerifyResult.logs ?? ""}`;
329198
330652
  tags: run2.tags ?? [],
329199
330653
  surfaceIntelligence: run2.surfaceIntelligence,
329200
330654
  healingContext: run2.healingContext ?? null,
330655
+ selectorRegistryContext: run2.selectorRegistryContext ?? null,
329201
330656
  lastFailureError: run2.lastFailureError,
329202
330657
  testVariables: run2.testVariables,
329203
330658
  onAICall: healAudit,
@@ -329432,6 +330887,7 @@ ${finalVerifyResult.logs ?? ""}`;
329432
330887
  tags: failedTest.tags ?? [],
329433
330888
  surfaceIntelligence: failedTest.healingContext ? null : run2.surfaceIntelligence,
329434
330889
  healingContext: failedTest.healingContext ?? null,
330890
+ selectorRegistryContext: failedTest.selectorRegistryContext ?? null,
329435
330891
  lastFailureError: failedTest.error,
329436
330892
  testVariables: run2.testVariables,
329437
330893
  onAICall: healAudit,
@@ -329850,6 +331306,14 @@ ${finalVerifyResult.logs ?? ""}`;
329850
331306
  throw new Error("CONFIG_ISSUE: the shared mobile device is busy with an active recording session; mobile discovery will retry once it ends.");
329851
331307
  }
329852
331308
  const mobileOnLog = (line) => onLog("progress", line);
331309
+ liveBrowserHandle.patch({
331310
+ mode: ctx.mode === "survey" ? "survey" : "discovery",
331311
+ testName: ctx.featureName ?? "Mobile discovery",
331312
+ note: `${platform3} mobile discovery`,
331313
+ surface: "mobile",
331314
+ mobilePlatform: platform3
331315
+ });
331316
+ requestLiveBrowserHeartbeat(true);
329853
331317
  mobileOnLog(`Mobile discovery (${ctx.mode}) \u2014 ${platform3} / ${mobileTarget.appId}`);
329854
331318
  setExecutorBusy(true);
329855
331319
  let session = null;
@@ -329862,30 +331326,47 @@ ${finalVerifyResult.logs ?? ""}`;
329862
331326
  recordedDeviceId: mobileTarget.recordedDeviceId,
329863
331327
  launchMode: mobileTarget.launchMode,
329864
331328
  deeplink: mobileTarget.deeplink
331329
+ }, {
331330
+ onDeviceSelected: async (device) => {
331331
+ try {
331332
+ capture = await startMobileNetworkCapture({
331333
+ platform: platform3,
331334
+ deviceId: device.deviceId,
331335
+ appId: mobileTarget.appId,
331336
+ isPhysical: device.isPhysical,
331337
+ onLog: mobileOnLog
331338
+ });
331339
+ const caps = capture.getCapabilities();
331340
+ if (!caps.intercepting) {
331341
+ mobileOnLog(`[mobile-net] HTTPS interception unavailable: ${caps.reason ?? "unknown reason"} (continuing \u2014 plaintext/metadata only)`);
331342
+ }
331343
+ } catch (captureErr) {
331344
+ mobileOnLog(`[mobile-net] capture unavailable: ${captureErr instanceof Error ? captureErr.message : String(captureErr)} (continuing without network log)`);
331345
+ }
331346
+ }
329865
331347
  });
329866
331348
  mobileOnLog(`Appium session created: ${session.sessionId} on device ${session.deviceId}`);
331349
+ attachMirrorSession(session.sessionId, platform3);
329867
331350
  const driver = new MobileBridgeDriver({
329868
331351
  sessionId: session.sessionId,
329869
331352
  platform: platform3,
329870
331353
  appId: session.appId
329871
331354
  });
329872
- capture = await startMobileNetworkCapture({
329873
- platform: platform3,
329874
- deviceId: session.deviceId,
329875
- appId: session.appId,
329876
- isPhysical: session.isPhysical,
329877
- onLog: mobileOnLog
329878
- });
329879
- const caps = capture.getCapabilities();
329880
- if (!caps.intercepting) {
329881
- mobileOnLog(`[mobile-net] HTTPS interception unavailable: ${caps.reason ?? "unknown reason"} (continuing \u2014 plaintext/metadata only)`);
329882
- }
329883
331355
  discoveryResult = await (0, import_runner_core4.runMobileDiscoveryOnRunner)(ctx, driver, {
329884
331356
  onLog: mobileOnLog,
329885
331357
  onAICall: discoveryAudit,
329886
331358
  // Stream the live device mirror through the existing page-screenshot
329887
- // relay (mobile has no route, so key it as 'mobile'). Fire-and-forget.
331359
+ // relay (mobile has no route, so key it as 'mobile') and the
331360
+ // heartbeat live-session feed. The heartbeat frame gives the web
331361
+ // UI a reliable mobile fallback when the direct bridge SSE is
331362
+ // still opening or blocked by the user's network/tunnel.
329888
331363
  onScreenshot: (base64) => {
331364
+ liveBrowserHandle.updateSnapshot({
331365
+ currentUrl: mobileTarget.appId,
331366
+ thumbnailJpegBase64: base64,
331367
+ thumbnailCapturedAt: Date.now()
331368
+ });
331369
+ requestLiveBrowserHeartbeat();
329889
331370
  void onScreenshot("mobile", base64);
329890
331371
  },
329891
331372
  onExploreComplete,
@@ -329897,13 +331378,14 @@ ${finalVerifyResult.logs ?? ""}`;
329897
331378
  try {
329898
331379
  const apiCalls = await capture.stop();
329899
331380
  if (discoveryResult) {
329900
- discoveryResult.apiCalls = apiCalls.length ? apiCalls : void 0;
331381
+ discoveryResult.apiCalls = apiCalls.length ? attachMobileDiscoveryNetworkContext(apiCalls, discoveryResult, session?.appId ?? mobileTarget.appId) : void 0;
329901
331382
  }
329902
331383
  } catch (capErr) {
329903
331384
  mobileOnLog(`[mobile-net] capture stop failed: ${capErr instanceof Error ? capErr.message : String(capErr)}`);
329904
331385
  }
329905
331386
  }
329906
331387
  if (session) {
331388
+ detachMirrorSession(session.sessionId);
329907
331389
  try {
329908
331390
  await session.stop();
329909
331391
  } catch {
@@ -330213,6 +331695,7 @@ ${finalVerifyResult.logs ?? ""}`;
330213
331695
  status,
330214
331696
  stepResults: result2.stepResults,
330215
331697
  screenshots: result2.screenshots,
331698
+ apiCalls: result2.apiCalls,
330216
331699
  logs: result2.logs,
330217
331700
  error: reportedError,
330218
331701
  duration: duration2