@riddledc/riddle-proof 0.6.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.cjs CHANGED
@@ -31,6 +31,25 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
31
31
  ));
32
32
 
33
33
  // src/proof-run-core.ts
34
+ function normalizedMode(value) {
35
+ return typeof value === "string" ? value.trim().toLowerCase() : "";
36
+ }
37
+ function previewModeFromWorkflowMode(value) {
38
+ const mode = normalizedMode(value);
39
+ return mode === "server" || mode === "static" ? mode : void 0;
40
+ }
41
+ function noImplementationModeInput(value) {
42
+ return value && typeof value === "object" ? value : {};
43
+ }
44
+ function noImplementationModeFor(params, state) {
45
+ const input = noImplementationModeInput(params);
46
+ const stateInput = noImplementationModeInput(state);
47
+ const mode = normalizedMode(input.mode ?? input.workflow_mode ?? stateInput.mode ?? stateInput.workflow_mode);
48
+ const implementationMode = normalizedMode(input.implementation_mode ?? stateInput.implementation_mode);
49
+ const requireDiff = input.require_diff ?? stateInput.require_diff;
50
+ const allowCodeChanges = input.allow_code_changes ?? stateInput.allow_code_changes;
51
+ return mode === "audit" || mode === "profile" || implementationMode === "none" || requireDiff === false || allowCodeChanges === false;
52
+ }
34
53
  function currentDistDir() {
35
54
  const meta = typeof import_meta === "object" ? import_meta : {};
36
55
  if (typeof meta.url === "string" && meta.url) {
@@ -114,7 +133,7 @@ function buildSetupArgs(params, config) {
114
133
  allow_static_preview_fallback: params.allow_static_preview_fallback ? "true" : "",
115
134
  context: params.context || "",
116
135
  reviewer: params.reviewer || config.defaultReviewer,
117
- mode: params.mode || "",
136
+ mode: previewModeFromWorkflowMode(params.mode) || "",
118
137
  build_command: params.build_command || "npm run build",
119
138
  build_output: params.build_output || "build",
120
139
  server_image: params.server_image || "node:20-slim",
@@ -539,7 +558,8 @@ function mergeStateFromParams(statePath, params) {
539
558
  "auth_cookies_json",
540
559
  "auth_headers_json",
541
560
  "proof_plan",
542
- "implementation_notes"
561
+ "implementation_notes",
562
+ "implementation_mode"
543
563
  ];
544
564
  for (const field of stringFields) {
545
565
  if (params[field] !== void 0) {
@@ -554,7 +574,17 @@ function mergeStateFromParams(statePath, params) {
554
574
  if (issues.length) state.implementation_environment_issues = issues;
555
575
  }
556
576
  if (params.reference !== void 0) state.reference = params.reference;
557
- if (params.mode !== void 0) state.mode = params.mode;
577
+ if (params.mode !== void 0) {
578
+ const previewMode = previewModeFromWorkflowMode(params.mode);
579
+ if (previewMode) {
580
+ state.mode = previewMode;
581
+ } else {
582
+ state.workflow_mode = normalizeOptionalString(params.mode);
583
+ }
584
+ }
585
+ if (params.implementation_mode !== void 0) state.implementation_mode = normalizeOptionalString(params.implementation_mode);
586
+ if (params.require_diff !== void 0) state.require_diff = params.require_diff;
587
+ if (params.allow_code_changes !== void 0) state.allow_code_changes = params.allow_code_changes;
558
588
  if (params.allow_static_preview_fallback !== void 0) {
559
589
  state.allow_static_preview_fallback = params.allow_static_preview_fallback;
560
590
  }
@@ -699,6 +729,11 @@ function summarizeState(state) {
699
729
  repo: state.repo || null,
700
730
  branch: state.branch || null,
701
731
  mode: state.mode || null,
732
+ workflow_mode: state.workflow_mode || null,
733
+ implementation_mode: state.implementation_mode || null,
734
+ require_diff: state.require_diff ?? null,
735
+ allow_code_changes: state.allow_code_changes ?? null,
736
+ no_implementation_mode: noImplementationModeFor(state),
702
737
  reference: state.reference || null,
703
738
  before_ref: state.before_ref || null,
704
739
  allow_static_preview_fallback: Boolean(state.allow_static_preview_fallback),
@@ -997,8 +1032,11 @@ function authorReady(state) {
997
1032
  function implementationReady(state) {
998
1033
  return ["changes_detected", "completed"].includes(state?.implementation_status || "");
999
1034
  }
1000
- function stageAfterAuthor(state) {
1001
- return implementationReady(state) ? "verify" : "implement";
1035
+ function implementationRequired(params, state) {
1036
+ return !noImplementationModeFor(params, state);
1037
+ }
1038
+ function stageAfterAuthor(state, params) {
1039
+ return implementationReady(state) || !implementationRequired(params, state) ? "verify" : "implement";
1002
1040
  }
1003
1041
  function latestReconAttempt(state) {
1004
1042
  const history = Array.isArray(state?.recon_results?.attempt_history) ? state.recon_results.attempt_history : [];
@@ -1208,7 +1246,7 @@ function recommendedAdvanceStage(state) {
1208
1246
  if (!state?.workspace_ready) return "setup";
1209
1247
  if (!state?.recon_results || ["needs_agent_decision", "needs_supervisor_judgment"].includes(state?.recon_status || "")) return "recon";
1210
1248
  if (!authorReady(state)) return "author";
1211
- if (!implementationReady(state)) return "implement";
1249
+ if (!implementationReady(state) && !noImplementationModeFor(state)) return "implement";
1212
1250
  if (state?.verify_status === "capture_incomplete") return verifyAssessment(state).continueWithStage || verifyAssessment(state).recommendedStage || "author";
1213
1251
  if (state?.verify_status === "evidence_captured") return verifyAssessment(state).continueWithStage || verifyAssessment(state).recommendedStage;
1214
1252
  if (!(state?.after_cdn || "").trim()) return "verify";
@@ -1833,6 +1871,7 @@ async function executeWorkflow(params, pluginConfig, resolvedConfig) {
1833
1871
  checkpoint: params.advance_stage === "setup" ? "setup_review" : null,
1834
1872
  autoApproved: setupRes.autoApproved || false
1835
1873
  });
1874
+ mergeStateFromParams(config.statePath, params);
1836
1875
  state = readState(config.statePath);
1837
1876
  if (params.advance_stage === "setup") {
1838
1877
  return checkpoint(
@@ -2130,7 +2169,8 @@ async function executeWorkflow(params, pluginConfig, resolvedConfig) {
2130
2169
  }
2131
2170
  );
2132
2171
  }
2133
- const authorNextStage = stageAfterAuthor(state);
2172
+ const noImplementationMode = !implementationRequired(params, state);
2173
+ const authorNextStage = stageAfterAuthor(state, params);
2134
2174
  const explicitAuthorDebug = params.advance_stage === "author";
2135
2175
  recordAttempt("author", "completed", "Author applied the supervising agent's proof packet to recon observations.", {
2136
2176
  autoApproved: authorRes.autoApproved || false,
@@ -2148,7 +2188,7 @@ async function executeWorkflow(params, pluginConfig, resolvedConfig) {
2148
2188
  return checkpoint(
2149
2189
  "author",
2150
2190
  "author_review",
2151
- authorNextStage === "verify" ? "Author applied the supervising agent's proof packet. Because implementation is already recorded, you can continue straight into verify." : "Author applied the supervising agent's proof packet. Inspect it if needed, then continue into implement.",
2191
+ authorNextStage === "verify" ? noImplementationMode ? "Author applied the supervising agent's proof packet. Audit/no-diff mode disables implementation, so you can continue straight into verify." : "Author applied the supervising agent's proof packet. Because implementation is already recorded, you can continue straight into verify." : "Author applied the supervising agent's proof packet. Inspect it if needed, then continue into implement.",
2152
2192
  {
2153
2193
  nextActions: authorNextStage === "verify" ? ["inspect_proof_packet", "advance_run_to_verify", "rerun_author"] : ["inspect_proof_packet", "advance_run_to_implement", "rerun_author"],
2154
2194
  advanceOptions: authorNextStage === "verify" ? ["author", "verify", "recon"] : ["author", "implement", "recon"],
@@ -2181,13 +2221,14 @@ async function executeWorkflow(params, pluginConfig, resolvedConfig) {
2181
2221
  }
2182
2222
  if (!effectiveAdvanceStage) {
2183
2223
  const recommended = recommendedAdvanceStage(state);
2224
+ const noImplementationMode = !implementationRequired(params, state);
2184
2225
  return checkpoint(
2185
- recommended || "implement",
2226
+ recommended || (noImplementationMode ? "verify" : "implement"),
2186
2227
  "awaiting_stage_advance",
2187
2228
  "Proof authoring is ready. The wrapper will not guess the next stage from here, explicitly choose whether to revisit recon/author, validate implementation, capture verify evidence, or ship.",
2188
2229
  {
2189
2230
  nextActions: ["inspect_state", "set_advance_stage", "resume_run"],
2190
- advanceOptions: ["recon", "author", "implement", "verify", "ship"],
2231
+ advanceOptions: noImplementationMode ? ["recon", "author", "verify", "ship"] : ["recon", "author", "implement", "verify", "ship"],
2191
2232
  recommendedAdvanceStage: recommended,
2192
2233
  details: { executed },
2193
2234
  executed
@@ -2195,6 +2236,26 @@ async function executeWorkflow(params, pluginConfig, resolvedConfig) {
2195
2236
  );
2196
2237
  }
2197
2238
  if (effectiveAdvanceStage === "implement") {
2239
+ if (!implementationRequired(params, state)) {
2240
+ recordAttempt("implement", "checkpoint", "Implementation stage was skipped because audit/no-diff mode disables code changes.", {
2241
+ checkpoint: "implement_disabled_for_audit",
2242
+ details: { executed }
2243
+ });
2244
+ return checkpoint(
2245
+ "verify",
2246
+ "implement_disabled_for_audit",
2247
+ "Audit/no-diff mode disables implementation. Continue to verify against the existing target; do not launch an implementation agent or require a git diff.",
2248
+ {
2249
+ nextActions: ["advance_run_to_verify", "inspect_author_packet", "rerun_recon_if_target_changed"],
2250
+ advanceOptions: ["verify", "author", "recon"],
2251
+ recommendedAdvanceStage: "verify",
2252
+ continueWithStage: "verify",
2253
+ blocking: false,
2254
+ details: { executed },
2255
+ executed
2256
+ }
2257
+ );
2258
+ }
2198
2259
  const implementRes = runOne("implement");
2199
2260
  executed.push(executedStep(implementRes));
2200
2261
  if (implementRes.haltedForApproval) {
@@ -2285,7 +2346,8 @@ ${implementRes.stderr || ""}`;
2285
2346
  }
2286
2347
  if (effectiveAdvanceStage === "verify") {
2287
2348
  state = readState(config.statePath);
2288
- if (!["changes_detected", "completed"].includes(state?.implementation_status || "")) {
2349
+ const needsImplementation = implementationRequired(params, state);
2350
+ if (needsImplementation && !implementationReady(state)) {
2289
2351
  return checkpoint(
2290
2352
  "implement",
2291
2353
  "implement_required",
@@ -2302,6 +2364,18 @@ ${implementRes.stderr || ""}`;
2302
2364
  }
2303
2365
  );
2304
2366
  }
2367
+ if (!needsImplementation && !implementationReady(state)) {
2368
+ recordAttempt("implement", "completed", "Implementation stage is not required for this audit/no-diff run.", {
2369
+ checkpoint: "implementation_not_required",
2370
+ details: { executed }
2371
+ });
2372
+ state = updateState(config.statePath, (currentState) => {
2373
+ currentState.implementation_status = "not_required";
2374
+ currentState.implementation_mode = currentState.implementation_mode || "none";
2375
+ if (currentState.require_diff === void 0) currentState.require_diff = false;
2376
+ if (currentState.allow_code_changes === void 0) currentState.allow_code_changes = false;
2377
+ });
2378
+ }
2305
2379
  const hasIncomingProofAssessment = typeof params.proof_assessment_json === "string" && params.proof_assessment_json.trim().length > 0;
2306
2380
  const canReuseVerifyEvidence = (params.advance_stage !== "verify" || hasIncomingProofAssessment) && state?.verify_status === "evidence_captured" && Boolean((state?.after_cdn || "").trim()) && (state?.active_checkpoint === "verify_supervisor_judgment" || hasSupervisorProofAssessment(state));
2307
2381
  let verifyRes = { ok: true, step: "verify", reusedEvidence: canReuseVerifyEvidence };
@@ -2324,8 +2398,13 @@ ${implementRes.stderr || ""}`;
2324
2398
  const verifySummary = state?.verify_summary || state?.proof_summary || null;
2325
2399
  const proofAssessment = verifyAssessment(state);
2326
2400
  const convergenceSignals = nonConvergenceSignals(state, proofAssessment);
2327
- const verifyRecommendedStage = proofAssessment.recommendedStage || null;
2328
- const verifyContinueWithStage = shouldEscalateVerifyToHuman(state, proofAssessment) ? null : proofAssessment.continueWithStage || verifyRecommendedStage || null;
2401
+ const rawVerifyRecommendedStage = proofAssessment.recommendedStage || null;
2402
+ const verifyRecommendedStage = !needsImplementation && rawVerifyRecommendedStage === "implement" ? "verify" : rawVerifyRecommendedStage;
2403
+ const rawVerifyContinueWithStage = shouldEscalateVerifyToHuman(state, proofAssessment) ? null : proofAssessment.continueWithStage || verifyRecommendedStage || null;
2404
+ const verifyContinueWithStage = !needsImplementation && rawVerifyContinueWithStage === "implement" ? "verify" : rawVerifyContinueWithStage;
2405
+ const verifyLoopAdvanceOptions = needsImplementation ? ["author", "verify", "implement", "recon"] : ["author", "verify", "recon"];
2406
+ const verifyReviewAdvanceOptions = needsImplementation ? ["verify", "author", "implement", "recon", "ship"] : ["verify", "author", "recon"];
2407
+ const verifyRetryAdvanceOptions = needsImplementation ? ["author", "implement", "ship", "verify", "recon"] : ["author", "verify", "recon"];
2329
2408
  const verifyDetails = {
2330
2409
  executed,
2331
2410
  verifyStatus,
@@ -2363,7 +2442,7 @@ ${implementRes.stderr || ""}`;
2363
2442
  {
2364
2443
  ok: true,
2365
2444
  nextActions: ["inspect_after_capture", "continue_internal_loop_with_checkpoint", "return_to_recon_if_baseline_is_wrong"],
2366
- advanceOptions: ["author", "verify", "implement", "recon"],
2445
+ advanceOptions: verifyLoopAdvanceOptions,
2367
2446
  recommendedAdvanceStage: verifyRecommendedStage || "author",
2368
2447
  continueWithStage: verifyContinueWithStage || "author",
2369
2448
  blocking: false,
@@ -2391,7 +2470,7 @@ ${implementRes.stderr || ""}`;
2391
2470
  summary,
2392
2471
  {
2393
2472
  nextActions: ["inspect_evidence", "author_proof_assessment_json", "continue_internal_loop_with_checkpoint"],
2394
- advanceOptions: ["verify", "author", "implement", "recon", "ship"],
2473
+ advanceOptions: verifyReviewAdvanceOptions,
2395
2474
  recommendedAdvanceStage: "verify",
2396
2475
  continueWithStage: "verify",
2397
2476
  blocking: false,
@@ -2421,7 +2500,7 @@ ${implementRes.stderr || ""}`;
2421
2500
  {
2422
2501
  ok: false,
2423
2502
  nextActions: ["inspect_retry_history", "summarize_internal_loop", "ask_human_for_direction"],
2424
- advanceOptions: ["author", "implement", "ship", "verify", "recon"],
2503
+ advanceOptions: verifyRetryAdvanceOptions,
2425
2504
  recommendedAdvanceStage: null,
2426
2505
  continueWithStage: null,
2427
2506
  blocking: true,
@@ -2436,7 +2515,7 @@ ${implementRes.stderr || ""}`;
2436
2515
  }
2437
2516
  );
2438
2517
  }
2439
- const shouldAutoShip = verifyContinueWithStage === "ship" && (params.ship_after_verify || params.continue_from_checkpoint || params.advance_stage !== "verify");
2518
+ const shouldAutoShip = verifyContinueWithStage === "ship" && needsImplementation && (params.ship_after_verify || params.continue_from_checkpoint || params.advance_stage !== "verify");
2440
2519
  if (shouldAutoShip) {
2441
2520
  const shipGate = validateShipGate(state);
2442
2521
  if (!shipGate.ok) {
@@ -2494,6 +2573,33 @@ ${implementRes.stderr || ""}`;
2494
2573
  };
2495
2574
  }
2496
2575
  if (proofAssessment.decision === "ready_to_ship") {
2576
+ if (!needsImplementation) {
2577
+ recordAttempt("verify", "completed", "Verify captured a proof packet for audit/no-diff mode; shipping remains disabled.", {
2578
+ autoApproved: verifyRes.autoApproved || false,
2579
+ checkpoint: "verify_audit_complete",
2580
+ details: verifyDetails
2581
+ });
2582
+ return checkpoint(
2583
+ "verify",
2584
+ "verify_audit_complete",
2585
+ "The supervising agent judged the audit proof sufficient. Audit/no-diff mode disables ship, PR creation, implementation agents, and git-diff requirements.",
2586
+ {
2587
+ nextActions: ["inspect_evidence", "report_audit_result", "rerun_verify_if_needed"],
2588
+ advanceOptions: ["verify", "author", "recon"],
2589
+ recommendedAdvanceStage: "verify",
2590
+ continueWithStage: null,
2591
+ blocking: false,
2592
+ details: verifyDetails,
2593
+ verifyStatus,
2594
+ verifySummary,
2595
+ afterCdn: state?.after_cdn || null,
2596
+ mergeRecommendation: state?.merge_recommendation || null,
2597
+ verifyDecisionRequest,
2598
+ proofAssessment: proofAssessment.raw,
2599
+ executed
2600
+ }
2601
+ );
2602
+ }
2497
2603
  const shipGate = validateShipGate(state);
2498
2604
  if (!shipGate.ok) {
2499
2605
  recordAttempt("verify", "checkpoint", "Verify cannot mark ship ready because the hard ship gate is missing required evidence or approval.", {
@@ -2550,8 +2656,8 @@ ${implementRes.stderr || ""}`;
2550
2656
  unresolvedSummary,
2551
2657
  {
2552
2658
  ok: true,
2553
- nextActions: convergenceSignals.warning ? ["inspect_retry_history", "decide_whether_to_keep_iterating_or_escalate", "continue_internal_loop_with_checkpoint"] : ["inspect_proof_assessment", "continue_internal_loop_with_checkpoint", "return_to_implement_if_fix_failed"],
2554
- advanceOptions: ["author", "implement", "ship", "verify", "recon"],
2659
+ nextActions: convergenceSignals.warning ? ["inspect_retry_history", "decide_whether_to_keep_iterating_or_escalate", "continue_internal_loop_with_checkpoint"] : needsImplementation ? ["inspect_proof_assessment", "continue_internal_loop_with_checkpoint", "return_to_implement_if_fix_failed"] : ["inspect_proof_assessment", "continue_internal_loop_with_checkpoint", "rerun_author_if_proof_contract_is_wrong"],
2660
+ advanceOptions: verifyRetryAdvanceOptions,
2555
2661
  recommendedAdvanceStage: verifyRecommendedStage,
2556
2662
  continueWithStage: verifyContinueWithStage,
2557
2663
  blocking: false,
@@ -2568,6 +2674,23 @@ ${implementRes.stderr || ""}`;
2568
2674
  }
2569
2675
  if (effectiveAdvanceStage === "ship") {
2570
2676
  state = readState(config.statePath);
2677
+ if (!implementationRequired(params, state)) {
2678
+ return checkpoint(
2679
+ "verify",
2680
+ "ship_disabled_for_audit",
2681
+ "Audit/no-diff mode disables ship and PR creation. Report the audit result from verify evidence instead.",
2682
+ {
2683
+ ok: false,
2684
+ nextActions: ["inspect_verify_state", "report_audit_result"],
2685
+ advanceOptions: ["verify", "author", "recon"],
2686
+ recommendedAdvanceStage: "verify",
2687
+ continueWithStage: "verify",
2688
+ blocking: true,
2689
+ details: { executed },
2690
+ executed
2691
+ }
2692
+ );
2693
+ }
2571
2694
  const shipAssessment = verifyAssessment(state);
2572
2695
  const shipGate = validateShipGate(state);
2573
2696
  if (state?.verify_status !== "evidence_captured") {
@@ -2772,6 +2895,7 @@ var init_proof_run_engine = __esm({
2772
2895
 
2773
2896
  // src/cli.ts
2774
2897
  var import_node_fs6 = require("fs");
2898
+ var import_node_path6 = __toESM(require("path"), 1);
2775
2899
 
2776
2900
  // src/engine-harness.ts
2777
2901
  var import_node_child_process2 = require("child_process");
@@ -3967,6 +4091,9 @@ function normalizeRunParams(input) {
3967
4091
  context: input.context,
3968
4092
  reviewer: input.reviewer,
3969
4093
  mode: input.mode,
4094
+ implementation_mode: input.implementation_mode,
4095
+ require_diff: input.require_diff,
4096
+ allow_code_changes: input.allow_code_changes,
3970
4097
  build_command: input.build_command,
3971
4098
  build_output: input.build_output,
3972
4099
  server_image: input.server_image,
@@ -4280,7 +4407,7 @@ function stageFromWorkflowParams(params) {
4280
4407
  if (params.ship_after_verify) return "ship";
4281
4408
  if (params.proof_assessment_json) return "verify";
4282
4409
  if (params.implementation_notes) return "verify";
4283
- if (params.author_packet_json) return "implement";
4410
+ if (params.author_packet_json) return noImplementationModeFor(params) ? "verify" : "implement";
4284
4411
  if (params.recon_assessment_json) return "author";
4285
4412
  return "setup";
4286
4413
  }
@@ -4310,6 +4437,9 @@ function initialRunParams(request, input, state) {
4310
4437
  context: request.context,
4311
4438
  reviewer: request.reviewer,
4312
4439
  mode: request.mode,
4440
+ implementation_mode: request.implementation_mode,
4441
+ require_diff: request.require_diff,
4442
+ allow_code_changes: request.allow_code_changes,
4313
4443
  build_command: request.build_command,
4314
4444
  build_output: request.build_output,
4315
4445
  server_image: request.server_image,
@@ -6631,6 +6761,778 @@ function createRiddleApiClient(config = {}) {
6631
6761
  };
6632
6762
  }
6633
6763
 
6764
+ // src/profile.ts
6765
+ var RIDDLE_PROOF_PROFILE_VERSION = "riddle-proof.profile.v1";
6766
+ var RIDDLE_PROOF_PROFILE_RESULT_VERSION = "riddle-proof.profile-result.v1";
6767
+ var RIDDLE_PROOF_PROFILE_STATUSES = [
6768
+ "passed",
6769
+ "product_regression",
6770
+ "proof_insufficient",
6771
+ "environment_blocked",
6772
+ "configuration_error",
6773
+ "needs_human_review"
6774
+ ];
6775
+ var RIDDLE_PROOF_PROFILE_CHECK_TYPES = [
6776
+ "route_loaded",
6777
+ "selector_visible",
6778
+ "selector_count_at_least",
6779
+ "text_visible",
6780
+ "text_absent",
6781
+ "no_horizontal_overflow",
6782
+ "no_mobile_horizontal_overflow",
6783
+ "no_fatal_console_errors"
6784
+ ];
6785
+ var DEFAULT_VIEWPORTS = [
6786
+ { name: "desktop", width: 1280, height: 800 }
6787
+ ];
6788
+ function isRecord(value) {
6789
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value);
6790
+ }
6791
+ function stringValue2(value) {
6792
+ return typeof value === "string" && value.trim() ? value.trim() : void 0;
6793
+ }
6794
+ function numberValue(value) {
6795
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
6796
+ }
6797
+ function jsonRecord(value) {
6798
+ if (!isRecord(value)) return void 0;
6799
+ return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, toJsonValue(child)]));
6800
+ }
6801
+ function toJsonValue(value) {
6802
+ if (value === null || value === void 0) return null;
6803
+ if (typeof value === "string" || typeof value === "boolean") return value;
6804
+ if (typeof value === "number") return Number.isFinite(value) ? value : null;
6805
+ if (Array.isArray(value)) return value.map(toJsonValue);
6806
+ if (isRecord(value)) return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, toJsonValue(child)]));
6807
+ return String(value);
6808
+ }
6809
+ function normalizeName(value, fallback) {
6810
+ const name = stringValue2(value) || fallback;
6811
+ return name.replace(/\s+/g, " ").trim();
6812
+ }
6813
+ function slugifyRiddleProofProfileName(value) {
6814
+ return value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 80) || "profile";
6815
+ }
6816
+ function normalizeViewport(input, index) {
6817
+ if (!isRecord(input)) throw new Error(`target.viewports[${index}] must be an object.`);
6818
+ const width = numberValue(input.width);
6819
+ const height = numberValue(input.height);
6820
+ if (!width || !height || width < 100 || height < 100) {
6821
+ throw new Error(`target.viewports[${index}] requires numeric width and height >= 100.`);
6822
+ }
6823
+ return {
6824
+ name: normalizeName(input.name || input.label, `viewport-${index + 1}`),
6825
+ width: Math.round(width),
6826
+ height: Math.round(height)
6827
+ };
6828
+ }
6829
+ function normalizeViewports(value) {
6830
+ if (value === void 0) return [...DEFAULT_VIEWPORTS];
6831
+ if (!Array.isArray(value) || value.length === 0) throw new Error("target.viewports must be a non-empty array.");
6832
+ return value.map(normalizeViewport);
6833
+ }
6834
+ function isSupportedCheckType(value) {
6835
+ return RIDDLE_PROOF_PROFILE_CHECK_TYPES.includes(value);
6836
+ }
6837
+ function normalizeCheck(input, index) {
6838
+ if (!isRecord(input)) throw new Error(`checks[${index}] must be an object.`);
6839
+ const type = stringValue2(input.type);
6840
+ if (!type) throw new Error(`checks[${index}].type is required.`);
6841
+ if (!isSupportedCheckType(type)) {
6842
+ throw new Error(`checks[${index}].type ${type} is not supported. Supported checks: ${RIDDLE_PROOF_PROFILE_CHECK_TYPES.join(", ")}`);
6843
+ }
6844
+ if ((type === "selector_visible" || type === "selector_count_at_least") && !stringValue2(input.selector)) {
6845
+ throw new Error(`checks[${index}] ${type} requires selector.`);
6846
+ }
6847
+ if ((type === "text_visible" || type === "text_absent") && !stringValue2(input.text) && !stringValue2(input.pattern)) {
6848
+ throw new Error(`checks[${index}] ${type} requires text or pattern.`);
6849
+ }
6850
+ if (type === "selector_count_at_least" && numberValue(input.min_count) === void 0) {
6851
+ throw new Error(`checks[${index}] selector_count_at_least requires min_count.`);
6852
+ }
6853
+ return {
6854
+ type,
6855
+ label: stringValue2(input.label),
6856
+ expected_path: stringValue2(input.expected_path),
6857
+ selector: stringValue2(input.selector),
6858
+ text: stringValue2(input.text),
6859
+ pattern: stringValue2(input.pattern),
6860
+ flags: stringValue2(input.flags),
6861
+ min_count: numberValue(input.min_count),
6862
+ max_overflow_px: numberValue(input.max_overflow_px)
6863
+ };
6864
+ }
6865
+ function normalizeFailurePolicy(input) {
6866
+ const defaults = {
6867
+ product_regression: "fail",
6868
+ proof_insufficient: "fail",
6869
+ environment_blocked: "neutral",
6870
+ configuration_error: "fail",
6871
+ needs_human_review: "fail"
6872
+ };
6873
+ if (!isRecord(input)) return defaults;
6874
+ const next = { ...defaults };
6875
+ for (const [key, value] of Object.entries(input)) {
6876
+ if (!RIDDLE_PROOF_PROFILE_STATUSES.includes(key)) continue;
6877
+ if (value === "fail" || value === "neutral" || value === "review") {
6878
+ next[key] = value;
6879
+ }
6880
+ }
6881
+ return next;
6882
+ }
6883
+ function normalizeRiddleProofProfile(input, options = {}) {
6884
+ if (!isRecord(input)) throw new Error("profile must be a JSON object.");
6885
+ const version = stringValue2(input.version) || RIDDLE_PROOF_PROFILE_VERSION;
6886
+ if (version !== RIDDLE_PROOF_PROFILE_VERSION) {
6887
+ throw new Error(`Unsupported profile version ${version}. Expected ${RIDDLE_PROOF_PROFILE_VERSION}.`);
6888
+ }
6889
+ const targetInput = isRecord(input.target) ? input.target : {};
6890
+ const checks = Array.isArray(input.checks) ? input.checks.map(normalizeCheck) : [];
6891
+ if (!checks.length) throw new Error("profile.checks must contain at least one check.");
6892
+ const targetUrl = stringValue2(options.url) || stringValue2(targetInput.url);
6893
+ const route = stringValue2(options.route) || stringValue2(targetInput.route);
6894
+ if (!targetUrl && !route) throw new Error("profile.target requires url or route, or pass --url.");
6895
+ return {
6896
+ version: RIDDLE_PROOF_PROFILE_VERSION,
6897
+ name: normalizeName(input.name, "riddle-proof-profile"),
6898
+ target: {
6899
+ url: targetUrl,
6900
+ route,
6901
+ viewports: options.viewports?.length ? options.viewports : normalizeViewports(targetInput.viewports),
6902
+ auth: stringValue2(targetInput.auth) || "none",
6903
+ wait_for_selector: stringValue2(targetInput.wait_for_selector) || stringValue2(targetInput.waitForSelector),
6904
+ wait_ms: numberValue(targetInput.wait_ms) ?? numberValue(targetInput.waitMs)
6905
+ },
6906
+ checks,
6907
+ artifacts: Array.isArray(input.artifacts) ? input.artifacts.map((item) => String(item)).filter(Boolean) : ["screenshot", "console", "dom_summary", "proof_json"],
6908
+ baseline_policy: stringValue2(input.baseline_policy) || stringValue2(input.baselinePolicy) || "invariant_only",
6909
+ failure_policy: normalizeFailurePolicy(input.failure_policy || input.failurePolicy),
6910
+ metadata: jsonRecord(input.metadata)
6911
+ };
6912
+ }
6913
+ function resolveRiddleProofProfileTargetUrl(profile) {
6914
+ const route = profile.target.route || "";
6915
+ const targetUrl = profile.target.url || "";
6916
+ if (/^https?:\/\//i.test(route)) return route;
6917
+ if (targetUrl && route) return new URL(route, targetUrl).href;
6918
+ if (targetUrl) return targetUrl;
6919
+ throw new Error("profile target URL could not be resolved.");
6920
+ }
6921
+ function routeForViewport(viewport) {
6922
+ return viewport?.route || {
6923
+ requested: "",
6924
+ observed: "",
6925
+ matched: false,
6926
+ error: "missing viewport evidence"
6927
+ };
6928
+ }
6929
+ function checkLabel(check) {
6930
+ return check.label || check.type;
6931
+ }
6932
+ function selectorKey(check) {
6933
+ return check.selector || "";
6934
+ }
6935
+ function textKey(check) {
6936
+ return check.pattern ? `pattern:${check.pattern}/${check.flags || ""}` : `text:${check.text || ""}`;
6937
+ }
6938
+ function matchText(sample, check) {
6939
+ if (check.pattern) {
6940
+ try {
6941
+ return new RegExp(check.pattern, check.flags || "").test(sample);
6942
+ } catch {
6943
+ return false;
6944
+ }
6945
+ }
6946
+ return sample.includes(check.text || "");
6947
+ }
6948
+ function successfulRoute(route) {
6949
+ return route.matched && !route.error && (route.http_status === null || route.http_status === void 0 || route.http_status < 400);
6950
+ }
6951
+ function assessCheckFromEvidence(check, evidence) {
6952
+ const viewports = evidence.viewports || [];
6953
+ if (!viewports.length) {
6954
+ return {
6955
+ type: check.type,
6956
+ label: checkLabel(check),
6957
+ status: "failed",
6958
+ evidence: {},
6959
+ message: "No viewport evidence was captured."
6960
+ };
6961
+ }
6962
+ if (check.type === "route_loaded") {
6963
+ const expectedPath = check.expected_path || new URL(evidence.target_url).pathname || "/";
6964
+ const failed = viewports.filter((viewport) => !successfulRoute({
6965
+ ...viewport.route,
6966
+ expected_path: expectedPath,
6967
+ matched: viewport.route.observed === expectedPath || viewport.route.matched
6968
+ }));
6969
+ return {
6970
+ type: check.type,
6971
+ label: checkLabel(check),
6972
+ status: failed.length ? "failed" : "passed",
6973
+ evidence: {
6974
+ expected_path: expectedPath,
6975
+ observed_paths: viewports.map((viewport) => viewport.route.observed),
6976
+ http_statuses: viewports.map((viewport) => viewport.route.http_status ?? null)
6977
+ },
6978
+ message: failed.length ? `Route did not load as ${expectedPath} in ${failed.length} viewport(s).` : void 0
6979
+ };
6980
+ }
6981
+ if (check.type === "selector_visible") {
6982
+ const key = selectorKey(check);
6983
+ const failed = viewports.filter((viewport) => (viewport.selectors?.[key]?.visible_count || 0) < 1);
6984
+ return {
6985
+ type: check.type,
6986
+ label: checkLabel(check),
6987
+ status: failed.length ? "failed" : "passed",
6988
+ evidence: {
6989
+ selector: key,
6990
+ visible_counts: viewports.map((viewport) => viewport.selectors?.[key]?.visible_count || 0)
6991
+ },
6992
+ message: failed.length ? `Selector ${key} was not visible in ${failed.length} viewport(s).` : void 0
6993
+ };
6994
+ }
6995
+ if (check.type === "selector_count_at_least") {
6996
+ const key = selectorKey(check);
6997
+ const minCount = check.min_count ?? 1;
6998
+ const failed = viewports.filter((viewport) => (viewport.selectors?.[key]?.count || 0) < minCount);
6999
+ return {
7000
+ type: check.type,
7001
+ label: checkLabel(check),
7002
+ status: failed.length ? "failed" : "passed",
7003
+ evidence: {
7004
+ selector: key,
7005
+ min_count: minCount,
7006
+ counts: viewports.map((viewport) => viewport.selectors?.[key]?.count || 0)
7007
+ },
7008
+ message: failed.length ? `Selector ${key} count was below ${minCount} in ${failed.length} viewport(s).` : void 0
7009
+ };
7010
+ }
7011
+ if (check.type === "text_visible" || check.type === "text_absent") {
7012
+ const key = textKey(check);
7013
+ const expectedVisible = check.type === "text_visible";
7014
+ const matches = viewports.map((viewport) => {
7015
+ const fromEvidence = viewport.text_matches?.[key];
7016
+ return typeof fromEvidence === "boolean" ? fromEvidence : matchText(viewport.body_text_sample || "", check);
7017
+ });
7018
+ const failed = matches.filter((matched) => matched !== expectedVisible).length;
7019
+ return {
7020
+ type: check.type,
7021
+ label: checkLabel(check),
7022
+ status: failed ? "failed" : "passed",
7023
+ evidence: {
7024
+ text: check.text || null,
7025
+ pattern: check.pattern || null,
7026
+ matches
7027
+ },
7028
+ message: failed ? `Text assertion failed in ${failed} viewport(s).` : void 0
7029
+ };
7030
+ }
7031
+ if (check.type === "no_horizontal_overflow" || check.type === "no_mobile_horizontal_overflow") {
7032
+ const maxOverflow = check.max_overflow_px ?? 4;
7033
+ const applicable = check.type === "no_mobile_horizontal_overflow" ? viewports.filter((viewport) => viewport.width <= 820) : viewports;
7034
+ if (!applicable.length) {
7035
+ return {
7036
+ type: check.type,
7037
+ label: checkLabel(check),
7038
+ status: "failed",
7039
+ evidence: { max_overflow_px: maxOverflow },
7040
+ message: "No applicable viewport evidence was captured for overflow check."
7041
+ };
7042
+ }
7043
+ const failed = applicable.filter((viewport) => (viewport.overflow_px ?? 0) > maxOverflow);
7044
+ return {
7045
+ type: check.type,
7046
+ label: checkLabel(check),
7047
+ status: failed.length ? "failed" : "passed",
7048
+ evidence: {
7049
+ max_overflow_px: maxOverflow,
7050
+ overflow_px: applicable.map((viewport) => viewport.overflow_px ?? null),
7051
+ viewports: applicable.map((viewport) => viewport.name)
7052
+ },
7053
+ message: failed.length ? `Horizontal overflow exceeded ${maxOverflow}px in ${failed.length} viewport(s).` : void 0
7054
+ };
7055
+ }
7056
+ if (check.type === "no_fatal_console_errors") {
7057
+ const fatalCount = (evidence.console?.fatal_count || 0) + (evidence.page_errors?.length || 0);
7058
+ return {
7059
+ type: check.type,
7060
+ label: checkLabel(check),
7061
+ status: fatalCount ? "failed" : "passed",
7062
+ evidence: {
7063
+ console_fatal_count: evidence.console?.fatal_count || 0,
7064
+ page_error_count: evidence.page_errors?.length || 0
7065
+ },
7066
+ message: fatalCount ? `${fatalCount} fatal browser error(s) were captured.` : void 0
7067
+ };
7068
+ }
7069
+ return {
7070
+ type: check.type,
7071
+ label: checkLabel(check),
7072
+ status: "needs_human_review",
7073
+ evidence: {},
7074
+ message: "Unsupported check type."
7075
+ };
7076
+ }
7077
+ function profileStatusFromEvidence(evidence, checks) {
7078
+ if (!evidence) return "proof_insufficient";
7079
+ const viewports = evidence.viewports || [];
7080
+ if (!viewports.length || !checks.length) return "proof_insufficient";
7081
+ if (viewports.some((viewport) => viewport.navigation_error)) return "environment_blocked";
7082
+ if (checks.some((check) => check.status === "needs_human_review")) return "needs_human_review";
7083
+ if (checks.some((check) => check.status === "failed")) return "product_regression";
7084
+ return "passed";
7085
+ }
7086
+ function assessRiddleProofProfileEvidence(profile, evidence, options = {}) {
7087
+ const capturedAt = evidence?.captured_at || (/* @__PURE__ */ new Date()).toISOString();
7088
+ const checks = evidence ? profile.checks.map((check) => assessCheckFromEvidence(check, evidence)) : [];
7089
+ const status = profileStatusFromEvidence(evidence, checks);
7090
+ const firstViewport = evidence?.viewports?.[0];
7091
+ const screenshots = (evidence?.viewports || []).map((viewport) => viewport.screenshot_label).filter((label) => Boolean(label));
7092
+ return {
7093
+ version: RIDDLE_PROOF_PROFILE_RESULT_VERSION,
7094
+ profile_name: profile.name,
7095
+ runner: options.runner || "riddle",
7096
+ status,
7097
+ baseline_policy: profile.baseline_policy,
7098
+ route: routeForViewport(firstViewport),
7099
+ artifacts: {
7100
+ screenshots,
7101
+ console: "console.json",
7102
+ proof_json: "proof.json",
7103
+ dom_summary: "dom-summary.json",
7104
+ riddle_artifacts: options.artifacts
7105
+ },
7106
+ checks,
7107
+ summary: summarizeRiddleProofProfileResult({ profile_name: profile.name, status, checks, viewports: evidence?.viewports || [] }),
7108
+ captured_at: capturedAt,
7109
+ evidence,
7110
+ riddle: options.riddle
7111
+ };
7112
+ }
7113
+ function summarizeRiddleProofProfileResult(input) {
7114
+ const passedChecks = input.checks.filter((check) => check.status === "passed").length;
7115
+ const failedChecks = input.checks.filter((check) => check.status === "failed").length;
7116
+ const viewportNames = input.viewports.map((viewport) => viewport.name).join(", ");
7117
+ if (input.status === "passed") {
7118
+ return `${input.profile_name} passed ${passedChecks} check(s) across ${input.viewports.length} viewport(s)${viewportNames ? ` (${viewportNames})` : ""}.`;
7119
+ }
7120
+ if (input.status === "product_regression") {
7121
+ return `${input.profile_name} failed ${failedChecks} product invariant(s) across ${input.viewports.length} viewport(s).`;
7122
+ }
7123
+ if (input.status === "environment_blocked") {
7124
+ return `${input.profile_name} could not collect reliable evidence because navigation or the browser environment was blocked.`;
7125
+ }
7126
+ if (input.status === "proof_insufficient") {
7127
+ return `${input.profile_name} did not produce enough evidence for a profile judgment.`;
7128
+ }
7129
+ if (input.status === "configuration_error") {
7130
+ return `${input.profile_name} has a profile configuration error.`;
7131
+ }
7132
+ return `${input.profile_name} collected artifacts but needs human review.`;
7133
+ }
7134
+ function profileStatusExitCode(profile, status) {
7135
+ if (status === "passed") return 0;
7136
+ return profile.failure_policy[status] === "neutral" ? 0 : 1;
7137
+ }
7138
+ function createRiddleProofProfileEnvironmentBlockedResult(input) {
7139
+ const message = input.error instanceof Error ? input.error.message : input.error ? String(input.error) : "Riddle runner did not complete successfully.";
7140
+ return {
7141
+ version: RIDDLE_PROOF_PROFILE_RESULT_VERSION,
7142
+ profile_name: input.profile.name,
7143
+ runner: input.runner || "riddle",
7144
+ status: "environment_blocked",
7145
+ baseline_policy: input.profile.baseline_policy,
7146
+ route: { requested: resolveRiddleProofProfileTargetUrl(input.profile), observed: "", matched: false, error: message },
7147
+ artifacts: { screenshots: [], proof_json: "proof.json", riddle_artifacts: input.artifacts },
7148
+ checks: [],
7149
+ summary: `${input.profile.name} could not collect reliable evidence because the runner was blocked.`,
7150
+ captured_at: (/* @__PURE__ */ new Date()).toISOString(),
7151
+ riddle: input.riddle,
7152
+ error: message
7153
+ };
7154
+ }
7155
+ function createRiddleProofProfileInsufficientResult(input) {
7156
+ const message = input.error instanceof Error ? input.error.message : input.error ? String(input.error) : "No proof.json profile result artifact was found.";
7157
+ return {
7158
+ version: RIDDLE_PROOF_PROFILE_RESULT_VERSION,
7159
+ profile_name: input.profile.name,
7160
+ runner: input.runner || "riddle",
7161
+ status: "proof_insufficient",
7162
+ baseline_policy: input.profile.baseline_policy,
7163
+ route: { requested: resolveRiddleProofProfileTargetUrl(input.profile), observed: "", matched: false, error: message },
7164
+ artifacts: { screenshots: [], proof_json: "proof.json", riddle_artifacts: input.artifacts },
7165
+ checks: [],
7166
+ summary: `${input.profile.name} did not produce enough evidence for a profile judgment.`,
7167
+ captured_at: (/* @__PURE__ */ new Date()).toISOString(),
7168
+ riddle: input.riddle,
7169
+ error: message
7170
+ };
7171
+ }
7172
+ function runtimeScriptAssessmentSource() {
7173
+ return String.raw`
7174
+ function routeOk(route) {
7175
+ return Boolean(route && route.matched && !route.error && (route.http_status == null || route.http_status < 400));
7176
+ }
7177
+ function textMatches(sample, check) {
7178
+ if (check.pattern) {
7179
+ try { return new RegExp(check.pattern, check.flags || "").test(sample || ""); } catch { return false; }
7180
+ }
7181
+ return String(sample || "").includes(check.text || "");
7182
+ }
7183
+ function assessProfile(profile, evidence) {
7184
+ const checks = [];
7185
+ const viewports = evidence.viewports || [];
7186
+ for (const check of profile.checks || []) {
7187
+ if (check.type === "route_loaded") {
7188
+ const expectedPath = check.expected_path || new URL(evidence.target_url).pathname || "/";
7189
+ const failed = viewports.filter((viewport) => {
7190
+ const route = { ...(viewport.route || {}), expected_path: expectedPath };
7191
+ route.matched = route.observed === expectedPath || route.matched;
7192
+ return !routeOk(route);
7193
+ });
7194
+ checks.push({
7195
+ type: check.type,
7196
+ label: check.label || check.type,
7197
+ status: failed.length ? "failed" : "passed",
7198
+ evidence: {
7199
+ expected_path: expectedPath,
7200
+ observed_paths: viewports.map((viewport) => viewport.route && viewport.route.observed),
7201
+ http_statuses: viewports.map((viewport) => viewport.route ? viewport.route.http_status ?? null : null),
7202
+ },
7203
+ message: failed.length ? "Route did not load as " + expectedPath + " in " + failed.length + " viewport(s)." : undefined,
7204
+ });
7205
+ continue;
7206
+ }
7207
+ if (check.type === "selector_visible") {
7208
+ const selector = check.selector || "";
7209
+ const failed = viewports.filter((viewport) => !viewport.selectors || !viewport.selectors[selector] || viewport.selectors[selector].visible_count < 1);
7210
+ checks.push({
7211
+ type: check.type,
7212
+ label: check.label || check.type,
7213
+ status: failed.length ? "failed" : "passed",
7214
+ evidence: { selector, visible_counts: viewports.map((viewport) => viewport.selectors && viewport.selectors[selector] ? viewport.selectors[selector].visible_count : 0) },
7215
+ message: failed.length ? "Selector " + selector + " was not visible in " + failed.length + " viewport(s)." : undefined,
7216
+ });
7217
+ continue;
7218
+ }
7219
+ if (check.type === "selector_count_at_least") {
7220
+ const selector = check.selector || "";
7221
+ const minCount = check.min_count == null ? 1 : check.min_count;
7222
+ const failed = viewports.filter((viewport) => !viewport.selectors || !viewport.selectors[selector] || viewport.selectors[selector].count < minCount);
7223
+ checks.push({
7224
+ type: check.type,
7225
+ label: check.label || check.type,
7226
+ status: failed.length ? "failed" : "passed",
7227
+ evidence: { selector, min_count: minCount, counts: viewports.map((viewport) => viewport.selectors && viewport.selectors[selector] ? viewport.selectors[selector].count : 0) },
7228
+ message: failed.length ? "Selector " + selector + " count was below " + minCount + " in " + failed.length + " viewport(s)." : undefined,
7229
+ });
7230
+ continue;
7231
+ }
7232
+ if (check.type === "text_visible" || check.type === "text_absent") {
7233
+ const key = check.pattern ? "pattern:" + check.pattern + "/" + (check.flags || "") : "text:" + (check.text || "");
7234
+ const expectedVisible = check.type === "text_visible";
7235
+ const matches = viewports.map((viewport) => viewport.text_matches && typeof viewport.text_matches[key] === "boolean" ? viewport.text_matches[key] : textMatches(viewport.body_text_sample || "", check));
7236
+ const failed = matches.filter((matched) => matched !== expectedVisible).length;
7237
+ checks.push({
7238
+ type: check.type,
7239
+ label: check.label || check.type,
7240
+ status: failed ? "failed" : "passed",
7241
+ evidence: { text: check.text, pattern: check.pattern, matches },
7242
+ message: failed ? "Text assertion failed in " + failed + " viewport(s)." : undefined,
7243
+ });
7244
+ continue;
7245
+ }
7246
+ if (check.type === "no_horizontal_overflow" || check.type === "no_mobile_horizontal_overflow") {
7247
+ const maxOverflow = check.max_overflow_px == null ? 4 : check.max_overflow_px;
7248
+ const applicable = check.type === "no_mobile_horizontal_overflow" ? viewports.filter((viewport) => viewport.width <= 820) : viewports;
7249
+ if (!applicable.length) {
7250
+ checks.push({
7251
+ type: check.type,
7252
+ label: check.label || check.type,
7253
+ status: "failed",
7254
+ evidence: { max_overflow_px: maxOverflow },
7255
+ message: "No applicable viewport evidence was captured for overflow check.",
7256
+ });
7257
+ continue;
7258
+ }
7259
+ const failed = applicable.filter((viewport) => (viewport.overflow_px || 0) > maxOverflow);
7260
+ checks.push({
7261
+ type: check.type,
7262
+ label: check.label || check.type,
7263
+ status: failed.length ? "failed" : "passed",
7264
+ evidence: { max_overflow_px: maxOverflow, overflow_px: applicable.map((viewport) => viewport.overflow_px ?? null), viewports: applicable.map((viewport) => viewport.name) },
7265
+ message: failed.length ? "Horizontal overflow exceeded " + maxOverflow + "px in " + failed.length + " viewport(s)." : undefined,
7266
+ });
7267
+ continue;
7268
+ }
7269
+ if (check.type === "no_fatal_console_errors") {
7270
+ const fatalCount = ((evidence.console && evidence.console.fatal_count) || 0) + ((evidence.page_errors || []).length);
7271
+ checks.push({
7272
+ type: check.type,
7273
+ label: check.label || check.type,
7274
+ status: fatalCount ? "failed" : "passed",
7275
+ evidence: { console_fatal_count: (evidence.console && evidence.console.fatal_count) || 0, page_error_count: (evidence.page_errors || []).length },
7276
+ message: fatalCount ? String(fatalCount) + " fatal browser error(s) were captured." : undefined,
7277
+ });
7278
+ continue;
7279
+ }
7280
+ checks.push({ type: check.type, label: check.label || check.type, status: "needs_human_review", evidence: {}, message: "Unsupported check type." });
7281
+ }
7282
+ let status = "passed";
7283
+ if (!viewports.length || !checks.length) status = "proof_insufficient";
7284
+ else if (viewports.some((viewport) => viewport.navigation_error)) status = "environment_blocked";
7285
+ else if (checks.some((check) => check.status === "needs_human_review")) status = "needs_human_review";
7286
+ else if (checks.some((check) => check.status === "failed")) status = "product_regression";
7287
+ const screenshotLabels = viewports.map((viewport) => viewport.screenshot_label).filter(Boolean);
7288
+ const route = viewports[0] && viewports[0].route ? viewports[0].route : { requested: evidence.target_url, observed: "", matched: false, error: "missing viewport evidence" };
7289
+ const passedChecks = checks.filter((check) => check.status === "passed").length;
7290
+ const failedChecks = checks.filter((check) => check.status === "failed").length;
7291
+ const viewportNames = viewports.map((viewport) => viewport.name).join(", ");
7292
+ let summary = profile.name + " collected artifacts but needs human review.";
7293
+ if (status === "passed") summary = profile.name + " passed " + passedChecks + " check(s) across " + viewports.length + " viewport(s)" + (viewportNames ? " (" + viewportNames + ")." : ".");
7294
+ if (status === "product_regression") summary = profile.name + " failed " + failedChecks + " product invariant(s) across " + viewports.length + " viewport(s).";
7295
+ if (status === "environment_blocked") summary = profile.name + " could not collect reliable evidence because navigation or the browser environment was blocked.";
7296
+ if (status === "proof_insufficient") summary = profile.name + " did not produce enough evidence for a profile judgment.";
7297
+ return {
7298
+ version: "riddle-proof.profile-result.v1",
7299
+ profile_name: profile.name,
7300
+ runner: "riddle",
7301
+ status,
7302
+ baseline_policy: profile.baseline_policy || "invariant_only",
7303
+ route,
7304
+ artifacts: { screenshots: screenshotLabels, console: "console.json", proof_json: "proof.json", dom_summary: "dom-summary.json" },
7305
+ checks,
7306
+ summary,
7307
+ captured_at: evidence.captured_at,
7308
+ evidence,
7309
+ };
7310
+ }
7311
+ `;
7312
+ }
7313
+ function buildRiddleProofProfileScript(profile) {
7314
+ const targetUrl = resolveRiddleProofProfileTargetUrl(profile);
7315
+ const slug = slugifyRiddleProofProfileName(profile.name);
7316
+ const serializableProfile = JSON.stringify(profile);
7317
+ const serializableTargetUrl = JSON.stringify(targetUrl);
7318
+ const serializableSlug = JSON.stringify(slug);
7319
+ return String.raw`
7320
+ const profile = ${serializableProfile};
7321
+ const targetUrl = ${serializableTargetUrl};
7322
+ const profileSlug = ${serializableSlug};
7323
+ const capturedAt = new Date().toISOString();
7324
+ const consoleEvents = [];
7325
+ const pageErrors = [];
7326
+ page.on("console", (message) => {
7327
+ const type = message.type();
7328
+ if (type === "error" || type === "warning" || type === "assert") {
7329
+ consoleEvents.push({
7330
+ type,
7331
+ text: message.text().slice(0, 1000),
7332
+ location: message.location ? message.location() : undefined,
7333
+ });
7334
+ }
7335
+ });
7336
+ page.on("pageerror", (error) => {
7337
+ pageErrors.push({ message: String(error && error.message ? error.message : error).slice(0, 1000) });
7338
+ });
7339
+ function textKey(check) {
7340
+ return check.pattern ? "pattern:" + check.pattern + "/" + (check.flags || "") : "text:" + (check.text || "");
7341
+ }
7342
+ function textMatches(sample, check) {
7343
+ if (check.pattern) {
7344
+ try { return new RegExp(check.pattern, check.flags || "").test(sample || ""); } catch { return false; }
7345
+ }
7346
+ return String(sample || "").includes(check.text || "");
7347
+ }
7348
+ function expectedPathFor(check) {
7349
+ return check.expected_path || new URL(targetUrl).pathname || "/";
7350
+ }
7351
+ async function selectorStats(selector) {
7352
+ return page.locator(selector).evaluateAll((elements) => {
7353
+ const isVisible = (element) => {
7354
+ const style = window.getComputedStyle(element);
7355
+ const rect = element.getBoundingClientRect();
7356
+ return style && style.visibility !== "hidden" && style.display !== "none" && rect.width > 0 && rect.height > 0;
7357
+ };
7358
+ return { count: elements.length, visible_count: elements.filter(isVisible).length };
7359
+ }).catch((error) => ({ count: 0, visible_count: 0, error: String(error && error.message ? error.message : error).slice(0, 500) }));
7360
+ }
7361
+ async function captureViewport(viewport) {
7362
+ await page.setViewportSize({ width: viewport.width, height: viewport.height });
7363
+ let httpStatus = null;
7364
+ let navigationError;
7365
+ let waitError;
7366
+ try {
7367
+ const response = await page.goto(targetUrl, { waitUntil: "domcontentloaded", timeout: 45000 });
7368
+ httpStatus = response ? response.status() : null;
7369
+ } catch (error) {
7370
+ navigationError = String(error && error.message ? error.message : error).slice(0, 1000);
7371
+ }
7372
+ if (!navigationError && profile.target.wait_for_selector) {
7373
+ try {
7374
+ await page.waitForSelector(profile.target.wait_for_selector, { state: "visible", timeout: 15000 });
7375
+ } catch (error) {
7376
+ waitError = String(error && error.message ? error.message : error).slice(0, 1000);
7377
+ }
7378
+ }
7379
+ if (!navigationError && profile.target.wait_ms) {
7380
+ await page.waitForTimeout(profile.target.wait_ms);
7381
+ }
7382
+ const dom = await page.evaluate(() => {
7383
+ const body = document.body;
7384
+ const documentElement = document.documentElement;
7385
+ const text = (body ? body.innerText : "").replace(/\s+/g, " ").trim();
7386
+ return {
7387
+ url: location.href,
7388
+ pathname: location.pathname,
7389
+ title: document.title,
7390
+ body_text_length: text.length,
7391
+ body_text_sample: text.slice(0, 8000),
7392
+ scroll_width: documentElement ? documentElement.scrollWidth : 0,
7393
+ client_width: documentElement ? documentElement.clientWidth : window.innerWidth,
7394
+ };
7395
+ }).catch((error) => ({
7396
+ url: page.url(),
7397
+ pathname: "",
7398
+ title: "",
7399
+ body_text_length: 0,
7400
+ body_text_sample: "",
7401
+ scroll_width: 0,
7402
+ client_width: viewport.width,
7403
+ evaluation_error: String(error && error.message ? error.message : error).slice(0, 1000),
7404
+ }));
7405
+ const selectors = {};
7406
+ const text_matches = {};
7407
+ for (const check of profile.checks || []) {
7408
+ if ((check.type === "selector_visible" || check.type === "selector_count_at_least") && check.selector) {
7409
+ selectors[check.selector] = await selectorStats(check.selector);
7410
+ }
7411
+ if ((check.type === "text_visible" || check.type === "text_absent") && (check.text || check.pattern)) {
7412
+ text_matches[textKey(check)] = textMatches(dom.body_text_sample || "", check);
7413
+ }
7414
+ }
7415
+ const screenshotLabel = profileSlug + "-" + viewport.name;
7416
+ try {
7417
+ if (typeof saveScreenshot === "function") await saveScreenshot(screenshotLabel);
7418
+ } catch (error) {
7419
+ pageErrors.push({ message: "saveScreenshot failed: " + String(error && error.message ? error.message : error).slice(0, 500) });
7420
+ }
7421
+ const expectedPath = profile.checks.find((check) => check.type === "route_loaded" && check.expected_path)?.expected_path || new URL(targetUrl).pathname || "/";
7422
+ return {
7423
+ name: viewport.name,
7424
+ width: viewport.width,
7425
+ height: viewport.height,
7426
+ url: dom.url,
7427
+ route: {
7428
+ requested: targetUrl,
7429
+ observed: dom.pathname,
7430
+ expected_path: expectedPath,
7431
+ matched: dom.pathname === expectedPath,
7432
+ http_status: httpStatus,
7433
+ error: navigationError || waitError || undefined,
7434
+ },
7435
+ title: dom.title,
7436
+ body_text_length: dom.body_text_length,
7437
+ body_text_sample: dom.body_text_sample,
7438
+ scroll_width: dom.scroll_width,
7439
+ client_width: dom.client_width,
7440
+ overflow_px: Math.max(0, (dom.scroll_width || 0) - (dom.client_width || viewport.width)),
7441
+ selectors,
7442
+ text_matches,
7443
+ screenshot_label: screenshotLabel,
7444
+ navigation_error: navigationError,
7445
+ wait_error: waitError,
7446
+ };
7447
+ }
7448
+ ${runtimeScriptAssessmentSource()}
7449
+ const viewports = [];
7450
+ for (const viewport of profile.target.viewports || []) {
7451
+ viewports.push(await captureViewport(viewport));
7452
+ }
7453
+ const evidence = {
7454
+ version: "riddle-proof.profile-evidence.v1",
7455
+ profile_name: profile.name,
7456
+ target_url: targetUrl,
7457
+ baseline_policy: profile.baseline_policy || "invariant_only",
7458
+ captured_at: capturedAt,
7459
+ viewports,
7460
+ console: {
7461
+ events: consoleEvents,
7462
+ fatal_count: consoleEvents.filter((event) => event.type === "error" || event.type === "assert").length,
7463
+ },
7464
+ page_errors: pageErrors,
7465
+ dom_summary: {
7466
+ viewport_count: viewports.length,
7467
+ routes: viewports.map((viewport) => viewport.route),
7468
+ titles: viewports.map((viewport) => viewport.title),
7469
+ overflow_px: viewports.map((viewport) => viewport.overflow_px),
7470
+ },
7471
+ };
7472
+ const result = assessProfile(profile, evidence);
7473
+ if (typeof saveJson === "function") {
7474
+ await saveJson("proof.json", result);
7475
+ await saveJson("console.json", { events: consoleEvents, page_errors: pageErrors });
7476
+ await saveJson("dom-summary.json", evidence.dom_summary);
7477
+ }
7478
+ return result;
7479
+ `.trim();
7480
+ }
7481
+ function collectRiddleProfileArtifactRefs(input) {
7482
+ const refs = [];
7483
+ const seen = /* @__PURE__ */ new Set();
7484
+ function add(item, source) {
7485
+ if (!isRecord(item)) return;
7486
+ const name = stringValue2(item.name) || stringValue2(item.filename) || "";
7487
+ const url = stringValue2(item.url);
7488
+ const path7 = stringValue2(item.path);
7489
+ if (!name && !url && !path7) return;
7490
+ const key = `${name}:${url || path7 || ""}`;
7491
+ if (seen.has(key)) return;
7492
+ seen.add(key);
7493
+ refs.push({
7494
+ name,
7495
+ url,
7496
+ path: path7,
7497
+ kind: stringValue2(item.kind) || stringValue2(item.type),
7498
+ content_type: stringValue2(item.content_type) || stringValue2(item.contentType),
7499
+ source
7500
+ });
7501
+ }
7502
+ function visit(value, source) {
7503
+ if (Array.isArray(value)) {
7504
+ for (const item of value) add(item, source);
7505
+ return;
7506
+ }
7507
+ if (!isRecord(value)) return;
7508
+ for (const key of ["artifacts", "outputs", "screenshots", "files"]) {
7509
+ if (Array.isArray(value[key])) visit(value[key], key);
7510
+ }
7511
+ }
7512
+ visit(input, "artifacts");
7513
+ return refs;
7514
+ }
7515
+ function extractRiddleProofProfileResult(input) {
7516
+ if (!isRecord(input)) return void 0;
7517
+ if (input.version === RIDDLE_PROOF_PROFILE_RESULT_VERSION) return input;
7518
+ const candidates = [
7519
+ input.result,
7520
+ input.return_value,
7521
+ input.value,
7522
+ input.profile_result,
7523
+ isRecord(input["proof.json"]) ? input["proof.json"] : void 0,
7524
+ isRecord(input._proof_json) ? input._proof_json : void 0
7525
+ ];
7526
+ for (const candidate of candidates) {
7527
+ const result = extractRiddleProofProfileResult(candidate);
7528
+ if (result) return result;
7529
+ }
7530
+ if (isRecord(input._artifact_json)) {
7531
+ return extractRiddleProofProfileResult(input._artifact_json["proof.json"]);
7532
+ }
7533
+ return void 0;
7534
+ }
7535
+
6634
7536
  // src/cli.ts
6635
7537
  function usage() {
6636
7538
  return [
@@ -6640,6 +7542,7 @@ function usage() {
6640
7542
  " riddle-proof-loop respond --state-path <path> --response-json <file|json|->",
6641
7543
  " riddle-proof-loop respond --state-path <path> --decision <decision> --summary <text> [--payload-json <file|json|->]",
6642
7544
  " riddle-proof-loop status --state-path <path>",
7545
+ " riddle-proof-loop run-profile --profile <file|json|-> --url <base-url> [--runner riddle] [--output <dir>]",
6643
7546
  " riddle-proof-loop riddle-preview-deploy <build-dir> <label>",
6644
7547
  " riddle-proof-loop riddle-server-preview <directory> --script-file <file> [--path /route] [--wait-for-selector selector]",
6645
7548
  " riddle-proof-loop riddle-run-script --url <url> --script-file <file> [--viewport 1280x720]",
@@ -6831,6 +7734,173 @@ function riddleClientConfig(options) {
6831
7734
  apiBaseUrl: optionString(options, "apiBaseUrl")
6832
7735
  };
6833
7736
  }
7737
+ function parseProfileViewports(value) {
7738
+ if (!value) return void 0;
7739
+ return value.split(",").map((part, index) => {
7740
+ const trimmed = part.trim();
7741
+ const named = /^([a-zA-Z0-9_-]+)=(\d+x\d+)$/.exec(trimmed);
7742
+ const viewport = parseRiddleViewport(named ? named[2] : trimmed);
7743
+ if (!viewport) throw new Error(`Invalid viewport ${trimmed}.`);
7744
+ return {
7745
+ name: named ? named[1] : `viewport-${index + 1}`,
7746
+ width: viewport.width,
7747
+ height: viewport.height
7748
+ };
7749
+ });
7750
+ }
7751
+ function normalizeProfileForCli(options) {
7752
+ const rawProfile = readJsonValue(optionString(options, "profile"), "--profile");
7753
+ return normalizeRiddleProofProfile(rawProfile, {
7754
+ url: optionString(options, "url"),
7755
+ route: optionString(options, "route"),
7756
+ viewports: parseProfileViewports(optionString(options, "viewports") || optionString(options, "viewport"))
7757
+ });
7758
+ }
7759
+ function profileResultMarkdown(result) {
7760
+ const lines = [
7761
+ `# Riddle Proof Profile: ${result.profile_name}`,
7762
+ "",
7763
+ `Status: ${result.status}`,
7764
+ `Runner: ${result.runner}`,
7765
+ `Captured: ${result.captured_at}`,
7766
+ "",
7767
+ result.summary,
7768
+ "",
7769
+ "## Checks",
7770
+ ""
7771
+ ];
7772
+ for (const check of result.checks) {
7773
+ lines.push(`- ${check.status}: ${check.label || check.type}`);
7774
+ if (check.message) lines.push(` ${check.message}`);
7775
+ }
7776
+ if (result.artifacts.riddle_artifacts?.length) {
7777
+ lines.push("", "## Riddle Artifacts", "");
7778
+ for (const artifact of result.artifacts.riddle_artifacts.slice(0, 40)) {
7779
+ lines.push(`- ${artifact.name || artifact.kind || "artifact"}${artifact.url ? `: ${artifact.url}` : ""}`);
7780
+ }
7781
+ }
7782
+ return `${lines.join("\n")}
7783
+ `;
7784
+ }
7785
+ function writeProfileOutput(outputDir, result) {
7786
+ if (!outputDir) return;
7787
+ (0, import_node_fs6.mkdirSync)(outputDir, { recursive: true });
7788
+ (0, import_node_fs6.writeFileSync)(import_node_path6.default.join(outputDir, "profile-result.json"), `${JSON.stringify(result, null, 2)}
7789
+ `);
7790
+ (0, import_node_fs6.writeFileSync)(import_node_path6.default.join(outputDir, "summary.md"), profileResultMarkdown(result));
7791
+ if (result.evidence) (0, import_node_fs6.writeFileSync)(import_node_path6.default.join(outputDir, "proof.json"), `${JSON.stringify(result, null, 2)}
7792
+ `);
7793
+ if (result.evidence?.console) (0, import_node_fs6.writeFileSync)(import_node_path6.default.join(outputDir, "console.json"), `${JSON.stringify(result.evidence.console, null, 2)}
7794
+ `);
7795
+ if (result.evidence?.dom_summary) (0, import_node_fs6.writeFileSync)(import_node_path6.default.join(outputDir, "dom-summary.json"), `${JSON.stringify(result.evidence.dom_summary, null, 2)}
7796
+ `);
7797
+ }
7798
+ async function readArtifactJson(artifact) {
7799
+ const target = artifact.url || artifact.path;
7800
+ if (!target) return void 0;
7801
+ try {
7802
+ const raw = artifact.url ? await (await fetch(artifact.url)).text() : (0, import_node_fs6.existsSync)(target) ? (0, import_node_fs6.readFileSync)(target, "utf-8") : "";
7803
+ if (!raw.trim()) return void 0;
7804
+ const parsed = JSON.parse(raw);
7805
+ return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : void 0;
7806
+ } catch {
7807
+ return void 0;
7808
+ }
7809
+ }
7810
+ async function profileResultFromRiddleArtifacts(profile, artifacts, fallbackInputs) {
7811
+ for (const input of fallbackInputs) {
7812
+ const result = extractRiddleProofProfileResult(input);
7813
+ if (result) return result;
7814
+ }
7815
+ const proofArtifacts = artifacts.filter((artifact) => /(^|\/)proof\.json(?:\.json)?$/i.test(artifact.name || artifact.url || artifact.path || "")).sort((left, right) => {
7816
+ const leftName = left.name || left.url || left.path || "";
7817
+ const rightName = right.name || right.url || right.path || "";
7818
+ return Number(/proof\.json\.json$/i.test(rightName)) - Number(/proof\.json\.json$/i.test(leftName));
7819
+ });
7820
+ for (const artifact of proofArtifacts) {
7821
+ const parsed = await readArtifactJson(artifact);
7822
+ const result = extractRiddleProofProfileResult(parsed);
7823
+ if (result) return result;
7824
+ }
7825
+ const evidenceArtifacts = artifacts.filter((artifact) => /profile-evidence|evidence\.json/i.test(artifact.name || artifact.url || artifact.path || ""));
7826
+ for (const artifact of evidenceArtifacts) {
7827
+ const parsed = await readArtifactJson(artifact);
7828
+ if (parsed?.version === "riddle-proof.profile-evidence.v1") {
7829
+ return assessRiddleProofProfileEvidence(profile, parsed, { artifacts });
7830
+ }
7831
+ }
7832
+ return void 0;
7833
+ }
7834
+ function withRiddleMetadata(result, input) {
7835
+ return {
7836
+ ...result,
7837
+ riddle: {
7838
+ ...result.riddle || {},
7839
+ job_id: input.job_id || result.riddle?.job_id,
7840
+ status: input.status ?? result.riddle?.status,
7841
+ terminal: input.terminal ?? result.riddle?.terminal
7842
+ },
7843
+ artifacts: {
7844
+ ...result.artifacts,
7845
+ riddle_artifacts: input.artifacts || result.artifacts.riddle_artifacts
7846
+ }
7847
+ };
7848
+ }
7849
+ async function runProfileForCli(profile, options) {
7850
+ const runner = optionString(options, "runner") || "riddle";
7851
+ if (runner !== "riddle") {
7852
+ throw new Error(`Unsupported --runner ${runner}. The current CLI supports --runner riddle.`);
7853
+ }
7854
+ const targetUrl = resolveRiddleProofProfileTargetUrl(profile);
7855
+ const client = createRiddleApiClient(riddleClientConfig(options));
7856
+ let created;
7857
+ try {
7858
+ created = await client.runScript({
7859
+ url: targetUrl,
7860
+ script: buildRiddleProofProfileScript(profile),
7861
+ viewport: profile.target.viewports[0],
7862
+ timeoutSec: optionString(options, "timeout") ? Number(optionString(options, "timeout")) : void 0,
7863
+ sync: options.sync === true ? true : void 0
7864
+ });
7865
+ } catch (error) {
7866
+ return createRiddleProofProfileEnvironmentBlockedResult({ profile, runner, error });
7867
+ }
7868
+ const jobId = typeof created.job_id === "string" ? created.job_id : typeof created.id === "string" ? created.id : "";
7869
+ if (!jobId) {
7870
+ const directResult = extractRiddleProofProfileResult(created);
7871
+ return directResult ? withRiddleMetadata(directResult, { artifacts: collectRiddleProfileArtifactRefs(created) }) : createRiddleProofProfileInsufficientResult({ profile, runner, error: "Riddle run response was missing job_id.", artifacts: collectRiddleProfileArtifactRefs(created) });
7872
+ }
7873
+ const poll = await client.pollJob(jobId, {
7874
+ wait: true,
7875
+ attempts: optionString(options, "attempts") ? Number(optionString(options, "attempts")) : void 0,
7876
+ intervalMs: optionString(options, "intervalMs") ? Number(optionString(options, "intervalMs")) : void 0
7877
+ });
7878
+ const artifacts = collectRiddleProfileArtifactRefs(poll.artifacts);
7879
+ if (!poll.ok || !poll.terminal) {
7880
+ return createRiddleProofProfileEnvironmentBlockedResult({
7881
+ profile,
7882
+ runner,
7883
+ error: `Riddle job ${jobId} ended with status ${poll.status || "unknown"}.`,
7884
+ riddle: { job_id: jobId, status: poll.status, terminal: poll.terminal },
7885
+ artifacts
7886
+ });
7887
+ }
7888
+ const artifactResult = await profileResultFromRiddleArtifacts(profile, artifacts, [poll.job, poll.artifacts, created]);
7889
+ if (!artifactResult) {
7890
+ return createRiddleProofProfileInsufficientResult({
7891
+ profile,
7892
+ runner,
7893
+ riddle: { job_id: jobId, status: poll.status, terminal: poll.terminal },
7894
+ artifacts
7895
+ });
7896
+ }
7897
+ return withRiddleMetadata(artifactResult, {
7898
+ job_id: jobId,
7899
+ status: poll.status,
7900
+ terminal: poll.terminal,
7901
+ artifacts
7902
+ });
7903
+ }
6834
7904
  function requestForRun(options) {
6835
7905
  const statePath = optionString(options, "statePath");
6836
7906
  const withEngineModuleUrl = (request) => {
@@ -6877,6 +7947,15 @@ async function main() {
6877
7947
  `);
6878
7948
  return;
6879
7949
  }
7950
+ if (command === "run-profile") {
7951
+ const profile = normalizeProfileForCli(options);
7952
+ const result = await runProfileForCli(profile, options);
7953
+ writeProfileOutput(optionString(options, "output"), result);
7954
+ process.stdout.write(`${JSON.stringify(result, null, 2)}
7955
+ `);
7956
+ process.exitCode = profileStatusExitCode(profile, result.status);
7957
+ return;
7958
+ }
6880
7959
  if (command === "riddle-preview-deploy") {
6881
7960
  const buildDir = positional[1];
6882
7961
  const label = positional[2];