rea-agents 2.4.0 → 3.0.0

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 (231) hide show
  1. package/README.md +91 -19
  2. package/bridge/ghidra/ReaGhidraBridge.java +524 -4
  3. package/bridge/hopper_bridge.py +313 -28
  4. package/dist/application/AnalysisClientCleanup.js +14 -0
  5. package/dist/application/AnalysisContextQueries.js +130 -0
  6. package/dist/application/AnalysisSnapshotCache.js +4 -2
  7. package/dist/application/ApplicationWorkflowEvidenceResolver.js +31 -13
  8. package/dist/application/ArtifactInventory/scanCanonical.js +1 -1
  9. package/dist/application/BinarySession.js +20 -8
  10. package/dist/application/BinarySessionComposition.js +21 -0
  11. package/dist/application/BinarySessionRecords.js +79 -9
  12. package/dist/application/BinarySessionStatus.js +31 -1
  13. package/dist/application/BrowserEvidence.js +14 -0
  14. package/dist/application/BrowserScenarioCapturePort.js +1 -0
  15. package/dist/application/BrowserScenarioCaptureService.js +53 -0
  16. package/dist/application/BrowserScenarioEvidence.js +40 -0
  17. package/dist/application/CallPathTracing.js +160 -0
  18. package/dist/application/CapabilityInventory.js +84 -14
  19. package/dist/application/ClientRegistrationStatus.js +4 -1
  20. package/dist/application/CompositeProvider.js +10 -1
  21. package/dist/application/DirectAnalysis.js +13 -4
  22. package/dist/application/EnhancedLiteralTracing.js +118 -0
  23. package/dist/application/EnhancedTools.js +71 -127
  24. package/dist/application/EvidenceLedger.js +45 -16
  25. package/dist/application/JavaScriptApplicationWorkflowEvidence.js +13 -0
  26. package/dist/application/JavaScriptApplicationWorkflowService.js +27 -1
  27. package/dist/application/JavaScriptArtifactAnalysis.js +13 -4
  28. package/dist/application/JavaScriptRuntimeObservationEvidence.js +34 -0
  29. package/dist/application/JavaScriptRuntimeObservationPort.js +1 -0
  30. package/dist/application/JavaScriptRuntimeObservationService.js +46 -0
  31. package/dist/application/JavaScriptSemanticGraphAsyncProjection.js +147 -0
  32. package/dist/application/JavaScriptSemanticGraphBuilder.js +34 -6
  33. package/dist/application/JavaScriptSemanticGraphChildProcessProjection.js +156 -0
  34. package/dist/application/JavaScriptSemanticGraphConstruction.js +2 -2
  35. package/dist/application/JavaScriptSemanticGraphDataProjection.js +203 -0
  36. package/dist/application/JavaScriptSemanticGraphFingerprintProjection.js +25 -0
  37. package/dist/application/JavaScriptSemanticGraphFlowProjection.js +129 -8
  38. package/dist/application/JavaScriptSemanticGraphObjectProjection.js +81 -0
  39. package/dist/application/JavaScriptSemanticGraphProjection.js +14 -7
  40. package/dist/application/JavaScriptSemanticGraphResourceProjection.js +59 -0
  41. package/dist/application/JavaScriptSemanticGraphValueProjection.js +77 -0
  42. package/dist/application/LazyAnalysisProvider.js +77 -0
  43. package/dist/application/LoopbackReplay.js +1 -1
  44. package/dist/application/NativeApiInspection.js +80 -0
  45. package/dist/application/ProcessReactiveEffects.js +6 -0
  46. package/dist/application/ProcessReactiveHarness.js +16 -1
  47. package/dist/application/ProjectPermissionStore.js +1 -0
  48. package/dist/application/ReconstructionObligationAuthority.js +72 -0
  49. package/dist/application/ReconstructionObligationCandidates.js +265 -0
  50. package/dist/application/ReconstructionObligationLedgerDiagnostics.js +47 -0
  51. package/dist/application/ReconstructionObligationLedgerEvaluation.js +263 -0
  52. package/dist/application/ReconstructionObligationLedgerProjection.js +86 -0
  53. package/dist/application/ReconstructionObligationLedgerService.js +72 -0
  54. package/dist/application/ReconstructionObligationOriginalCases.js +33 -0
  55. package/dist/application/ReconstructionObligationPolicies.js +132 -0
  56. package/dist/application/ReconstructionReadinessService.js +48 -0
  57. package/dist/application/SessionProviderRouter.js +6 -6
  58. package/dist/application/SetupClientConfiguration.js +4 -2
  59. package/dist/application/runtime.js +26 -9
  60. package/dist/artifacts/ArtifactProvider.js +87 -22
  61. package/dist/browser/BrowserScenarioSecrets.js +53 -0
  62. package/dist/browser/BrowserScenarioSessionPort.js +1 -0
  63. package/dist/browser/CdpBrowserProvider.js +2 -2
  64. package/dist/browser/CdpCaptureDocuments.js +57 -0
  65. package/dist/browser/CdpCaptureStorage.js +71 -29
  66. package/dist/browser/CdpCaptureStorageFingerprints.js +328 -0
  67. package/dist/browser/CdpEndpoint.js +8 -6
  68. package/dist/browser/CdpPageCapture.js +2 -0
  69. package/dist/browser/JavaScriptRuntimeScope.js +38 -0
  70. package/dist/browser/PlaywrightBrowserScenarioProvider.js +259 -0
  71. package/dist/browser/PlaywrightExecutionBoundary.js +42 -0
  72. package/dist/browser/PlaywrightScenarioActions.js +62 -0
  73. package/dist/browser/PlaywrightScenarioArtifacts.js +238 -0
  74. package/dist/browser/PlaywrightScenarioBrowser.js +144 -0
  75. package/dist/browser/PlaywrightScenarioEvents.js +301 -0
  76. package/dist/browser/PlaywrightScenarioSession.js +300 -0
  77. package/dist/browser/V8InspectorCaptureProjection.js +110 -0
  78. package/dist/browser/V8InspectorEndpoint.js +72 -0
  79. package/dist/browser/V8InspectorProvider.js +267 -0
  80. package/dist/catalogIdentity.js +11 -3
  81. package/dist/cli/artifactCommands.js +44 -3
  82. package/dist/cli/coreAnalysisCommands.js +78 -0
  83. package/dist/cli.js +4 -0
  84. package/dist/cliApplicationCommands.js +30 -2
  85. package/dist/cliBrowserAdvancedCommands.js +17 -5
  86. package/dist/cliBrowserCommands.js +1 -0
  87. package/dist/cliBrowserScenarioCommands.js +43 -0
  88. package/dist/cliCommandNames.js +9 -0
  89. package/dist/cliJavaScriptRuntimeCommands.js +164 -0
  90. package/dist/cliObservationOptions.js +4 -0
  91. package/dist/cliPolicyCommands.js +1 -0
  92. package/dist/config/environment.js +12 -0
  93. package/dist/config/parseConfig.js +88 -17
  94. package/dist/contracts/analysisSearchContract.js +14 -0
  95. package/dist/contracts/applicationToolContracts.js +86 -2
  96. package/dist/contracts/applicationWorkflowInputContracts.js +18 -0
  97. package/dist/contracts/artifactToolContracts.js +29 -2
  98. package/dist/contracts/browserProviderToolContracts.js +7 -0
  99. package/dist/contracts/browserScenarioToolContracts.js +81 -0
  100. package/dist/contracts/browserToolContracts.js +88 -98
  101. package/dist/contracts/enhancedInputs.js +35 -6
  102. package/dist/contracts/functionInstructionContract.js +8 -0
  103. package/dist/contracts/functionWorkflowToolContracts.js +30 -0
  104. package/dist/contracts/hopperMemoryContracts.js +22 -0
  105. package/dist/contracts/javascriptApplicationWorkflowExamples.js +42 -0
  106. package/dist/contracts/javascriptRuntimeObservationToolContracts.js +58 -0
  107. package/dist/contracts/promptContracts.js +14 -15
  108. package/dist/contracts/reconstructionReadinessExample.js +436 -0
  109. package/dist/contracts/toolContractExamples.js +8 -0
  110. package/dist/contracts/toolContractTypes.js +1 -0
  111. package/dist/contracts/toolContracts.js +34 -25
  112. package/dist/contracts/toolEffects.js +25 -0
  113. package/dist/contracts/toolOutputSchemaGroups.js +120 -30
  114. package/dist/contracts/toolOutputSchemaPrimitives.js +37 -0
  115. package/dist/domain/analysisErrorPresentation.js +8 -2
  116. package/dist/domain/analysisErrorProjection.js +9 -0
  117. package/dist/domain/artifactInspection.js +347 -0
  118. package/dist/domain/browserCaptureComparison.js +82 -0
  119. package/dist/domain/browserObservation.js +8 -1
  120. package/dist/domain/browserObservationErrors.js +1 -0
  121. package/dist/domain/browserObservationSchemas.js +26 -0
  122. package/dist/domain/browserScenario.js +165 -0
  123. package/dist/domain/browserScenarioCapture.js +57 -0
  124. package/dist/domain/browserScenarioCaptureValues.js +204 -0
  125. package/dist/domain/browserScenarioDiff.js +324 -0
  126. package/dist/domain/browserScenarioDiffValues.js +157 -0
  127. package/dist/domain/browserScenarioNormalization.js +45 -0
  128. package/dist/domain/browserScenarioValues.js +372 -0
  129. package/dist/domain/bytecodeProvider.js +179 -0
  130. package/dist/domain/conformancePackage.js +169 -0
  131. package/dist/domain/conformanceReplay.js +104 -0
  132. package/dist/domain/conformanceTrustGate.js +193 -0
  133. package/dist/domain/customProtocolCapture.js +137 -0
  134. package/dist/domain/eventProcessTree.js +214 -0
  135. package/dist/domain/evidenceBundle.js +47 -0
  136. package/dist/domain/hopperStartupFailure.js +1 -1
  137. package/dist/domain/hopperValues.js +14 -1
  138. package/dist/domain/inputIssueProjection.js +7 -1
  139. package/dist/domain/javascriptRuntimeObservation.js +170 -0
  140. package/dist/domain/javascriptRuntimeReconciliationParsing.js +120 -2
  141. package/dist/domain/javascriptRuntimeReconciliationResult.js +3 -3
  142. package/dist/domain/javascriptRuntimeReconciliationRuntime.js +2 -2
  143. package/dist/domain/javascriptRuntimeReconciliationSchemas.js +1 -1
  144. package/dist/domain/javascriptSemanticAnalysis.js +13 -16
  145. package/dist/domain/javascriptSemanticAsyncEffects.js +253 -0
  146. package/dist/domain/javascriptSemanticChildProcesses.js +237 -0
  147. package/dist/domain/javascriptSemanticDataEffectHelpers.js +125 -0
  148. package/dist/domain/javascriptSemanticDataEffects.js +284 -0
  149. package/dist/domain/javascriptSemanticDerivedAnalysis.js +41 -0
  150. package/dist/domain/javascriptSemanticFingerprints.js +174 -0
  151. package/dist/domain/javascriptSemanticIr.js +21 -1
  152. package/dist/domain/javascriptSemanticObjects.js +115 -0
  153. package/dist/domain/javascriptSemanticPromises.js +290 -0
  154. package/dist/domain/javascriptSemanticResources.js +136 -0
  155. package/dist/domain/javascriptSourceParser.js +15 -0
  156. package/dist/domain/javascriptStaticAnalysis.js +12 -15
  157. package/dist/domain/javascriptStaticAnalysisBundler.js +38 -0
  158. package/dist/domain/javascriptStaticAnalysisCalls.js +23 -1
  159. package/dist/domain/javascriptStaticAnalysisHelpers.js +8 -0
  160. package/dist/domain/mobileApplicationInvestigation.js +159 -0
  161. package/dist/domain/nativeApiBoundary.js +130 -0
  162. package/dist/domain/objcSwiftMetadata.js +150 -0
  163. package/dist/domain/packageAnalysis.js +123 -0
  164. package/dist/domain/peInspection.js +196 -0
  165. package/dist/domain/processCapture.js +8 -2
  166. package/dist/domain/processReactiveScenario.js +1 -0
  167. package/dist/domain/processReactiveTransition.js +9 -4
  168. package/dist/domain/protocolCapture.js +243 -0
  169. package/dist/domain/providerCleanupError.js +11 -0
  170. package/dist/domain/reconstructionObligationLedgerSchemas.js +278 -0
  171. package/dist/domain/reconstructionReadiness.js +121 -0
  172. package/dist/domain/reconstructionReadinessFindingHelpers.js +17 -0
  173. package/dist/domain/reconstructionReadinessIntegrityFindings.js +118 -0
  174. package/dist/domain/reconstructionReadinessJourneyFindings.js +193 -0
  175. package/dist/domain/reconstructionReadinessSchemas.js +240 -0
  176. package/dist/domain/sourceToBundleComparison.js +335 -0
  177. package/dist/domain/sourceToBundleComparisonSchemas.js +133 -0
  178. package/dist/domain/sourceToBundleSignals.js +202 -0
  179. package/dist/domain/webCaptureDiff.js +141 -27
  180. package/dist/domain/webScreenshot.js +1 -1
  181. package/dist/dotnet/ManagedStaticProvider.js +34 -5
  182. package/dist/generatedMcpToolCatalog.js +37 -0
  183. package/dist/generatedPackageMetadata.js +4 -4
  184. package/dist/ghidra/GhidraDefaults.js +1 -1
  185. package/dist/ghidra/GhidraFunctionValues.js +40 -2
  186. package/dist/ghidra/GhidraProviderCapabilities.js +47 -38
  187. package/dist/hopper/HopperCleanup.js +158 -0
  188. package/dist/hopper/HopperClient.js +93 -99
  189. package/dist/hopper/HopperProvider.js +61 -30
  190. package/dist/hopper/HopperRequestQueue.js +215 -0
  191. package/dist/hopper/HopperResponseStream.js +46 -0
  192. package/dist/hopper/protocol.js +63 -20
  193. package/dist/main/transport.js +24 -27
  194. package/dist/main.js +14 -6
  195. package/dist/mcpDoctor.js +27 -4
  196. package/dist/mcpStartupPolicy.js +14 -0
  197. package/dist/process/ProcessCommandIdentity.js +19 -0
  198. package/dist/process/ProcessOwnership.js +11 -8
  199. package/dist/server/DynamicToolAvailability.js +70 -0
  200. package/dist/server/LazyToolCatalog.js +134 -0
  201. package/dist/server/createServer.js +92 -154
  202. package/dist/server/hydrateServerTools.js +191 -0
  203. package/dist/server/lifecycleProgress.js +10 -0
  204. package/dist/server/permissionFailure.js +12 -0
  205. package/dist/server/registerApplicationTools/compareSourceToBundle.js +35 -0
  206. package/dist/server/registerApplicationTools/helpers.js +11 -7
  207. package/dist/server/registerApplicationTools/obligationLedger.js +27 -0
  208. package/dist/server/registerApplicationTools/readiness.js +55 -0
  209. package/dist/server/registerApplicationTools.js +6 -0
  210. package/dist/server/registerArtifactTools.js +9 -1
  211. package/dist/server/registerBrowserScenarioTool.js +25 -0
  212. package/dist/server/registerBrowserTools.js +2 -2
  213. package/dist/server/registerCloseLifecycleTool.js +57 -0
  214. package/dist/server/registerEnhancedTools.js +28 -7
  215. package/dist/server/registerEvidenceResources.js +54 -101
  216. package/dist/server/registerEvidenceTools.js +9 -2
  217. package/dist/server/registerInvestigationTools.js +1 -2
  218. package/dist/server/registerJavaScriptApplicationGraphResource.js +0 -4
  219. package/dist/server/registerJavaScriptRuntimeObservationTools.js +37 -0
  220. package/dist/server/registerReconstructionObligationLedgerResource.js +32 -0
  221. package/dist/server/registerReconstructionReadinessResource.js +32 -0
  222. package/dist/server/registerSessionRecordTools.js +82 -12
  223. package/dist/server/registerSessionStatusTool.js +11 -8
  224. package/dist/server/registerSessionTools.js +32 -58
  225. package/dist/server/sessionAvailabilityPolicy.js +10 -8
  226. package/dist/server/toolRegistrationOptions.js +21 -6
  227. package/dist/server/toolResult.js +12 -0
  228. package/package.json +28 -11
  229. package/scripts/package-runner-bootstrap.mjs +72 -0
  230. package/scripts/rea.mjs +11 -1
  231. package/skills/reverse-engineer-anything/SKILL.md +10 -3
@@ -0,0 +1,157 @@
1
+ import { z } from "zod";
2
+ import { browserScenarioCaptureSchema } from "./browserScenarioCapture.js";
3
+ /** Scenario artifact families compared after step alignment. */
4
+ export const browserScenarioArtifactKindSchema = z.enum([
5
+ "action_state",
6
+ "screenshot",
7
+ "dom",
8
+ "accessibility",
9
+ "url",
10
+ "history",
11
+ "storage",
12
+ "events",
13
+ ]);
14
+ const normalizableArtifactKindSchema = z.enum([
15
+ "action_state",
16
+ "dom",
17
+ "accessibility",
18
+ "url",
19
+ "history",
20
+ "storage",
21
+ "events",
22
+ ]);
23
+ const normalizationRuleSchema = z.strictObject({
24
+ rule_id: z
25
+ .string()
26
+ .trim()
27
+ .min(1)
28
+ .max(64)
29
+ .describe("Stable rule identity; rules execute in rule_id order."),
30
+ artifacts: z
31
+ .array(normalizableArtifactKindSchema)
32
+ .min(1)
33
+ .max(7)
34
+ .describe("Artifact families whose durable string fields use this rule."),
35
+ match: z
36
+ .string()
37
+ .min(1)
38
+ .max(4_096)
39
+ .describe("Exact public literal to replace; regular expressions are not used."),
40
+ replacement: z
41
+ .string()
42
+ .max(4_096)
43
+ .describe("Recorded replacement text used in normalized comparison."),
44
+ });
45
+ const normalizationInputSchema = z
46
+ .strictObject({
47
+ rules: z.array(normalizationRuleSchema).max(128).default([]),
48
+ })
49
+ .superRefine((normalization, context) => {
50
+ const ruleIds = new Set();
51
+ for (const [index, rule] of normalization.rules.entries()) {
52
+ if (ruleIds.has(rule.rule_id))
53
+ context.addIssue({
54
+ code: "custom",
55
+ path: ["rules", index, "rule_id"],
56
+ message: "Normalization rule IDs must be unique",
57
+ });
58
+ ruleIds.add(rule.rule_id);
59
+ if (new Set(rule.artifacts).size !== rule.artifacts.length)
60
+ context.addIssue({
61
+ code: "custom",
62
+ path: ["rules", index, "artifacts"],
63
+ message: "Normalization rule artifact kinds must be unique",
64
+ });
65
+ }
66
+ });
67
+ /** Input for step-aligned comparison of two browser scenario captures. */
68
+ export const compareBrowserScenariosInputSchema = z.strictObject({
69
+ before_scenario: browserScenarioCaptureSchema.describe("Earlier complete capture_browser_scenario result."),
70
+ after_scenario: browserScenarioCaptureSchema.describe("Later complete capture_browser_scenario result."),
71
+ normalization: normalizationInputSchema
72
+ .default({ rules: [] })
73
+ .describe("Bounded exact-literal rules committed in the comparison result."),
74
+ max_changes: z
75
+ .number()
76
+ .int()
77
+ .min(1)
78
+ .max(20_000)
79
+ .default(2_000)
80
+ .describe("Maximum changed or unknown artifact records to retain."),
81
+ });
82
+ const evidenceStateSchema = z.enum([
83
+ "captured",
84
+ "not_requested",
85
+ "missing",
86
+ "truncated",
87
+ ]);
88
+ const artifactDiffSchema = z.strictObject({
89
+ artifact: browserScenarioArtifactKindSchema,
90
+ status: z.enum(["changed", "unknown"]),
91
+ before_state: evidenceStateSchema,
92
+ after_state: evidenceStateSchema,
93
+ before_sha256: z
94
+ .string()
95
+ .regex(/^[a-f0-9]{64}$/u)
96
+ .nullable(),
97
+ after_sha256: z
98
+ .string()
99
+ .regex(/^[a-f0-9]{64}$/u)
100
+ .nullable(),
101
+ reason: z.string().min(1).max(2_048).nullable(),
102
+ });
103
+ const alignmentFailureSchema = z.strictObject({
104
+ code: z.enum([
105
+ "duplicate_before_step_id",
106
+ "duplicate_after_step_id",
107
+ "missing_before_step",
108
+ "missing_after_step",
109
+ "action_mismatch",
110
+ "capture_context_mismatch",
111
+ ]),
112
+ step_id: z.string().min(1).max(64).nullable(),
113
+ reason: z.string().min(1).max(2_048),
114
+ });
115
+ const builtInNormalizationSchema = z.enum([
116
+ "ignore_step_elapsed_ms",
117
+ "ignore_event_sequence",
118
+ "ignore_event_step_index",
119
+ "compare_screenshot_content_digest",
120
+ "compare_normalized_text_content",
121
+ ]);
122
+ /** Step alignment, recorded normalization, and bounded artifact differences. */
123
+ export const browserScenarioDiffSchema = z.strictObject({
124
+ schema_version: z.literal(1),
125
+ comparison_kind: z.literal("browser_scenario"),
126
+ overall_status: z.enum(["changed", "unchanged", "unknown"]),
127
+ normalization: z.strictObject({
128
+ built_in_rules: z.array(builtInNormalizationSchema).length(5),
129
+ rules: z.array(normalizationRuleSchema).max(128),
130
+ sha256: z.string().regex(/^[a-f0-9]{64}$/u),
131
+ }),
132
+ alignment: z.strictObject({
133
+ status: z.enum(["aligned", "partial", "failed"]),
134
+ aligned_steps: z.number().int().min(0).max(129),
135
+ before_only: z.array(z.string().min(1).max(64)).max(129),
136
+ after_only: z.array(z.string().min(1).max(64)).max(129),
137
+ failures: z.array(alignmentFailureSchema).max(520),
138
+ }),
139
+ steps: z
140
+ .array(z.strictObject({
141
+ step_id: z.string().min(1).max(64),
142
+ before_step_index: z.number().int().min(0).max(128),
143
+ after_step_index: z.number().int().min(0).max(128),
144
+ before_action: z.string().min(1).max(64),
145
+ after_action: z.string().min(1).max(64),
146
+ status: z.enum(["changed", "unchanged", "unknown"]),
147
+ artifact_diffs: z.array(artifactDiffSchema).max(8),
148
+ omitted_artifact_diffs: z.number().int().min(0).max(8),
149
+ }))
150
+ .max(129),
151
+ artifact_diffs: z.strictObject({
152
+ total: z.number().int().min(0).max(1_032),
153
+ retained: z.number().int().min(0).max(1_032),
154
+ omitted: z.number().int().min(0).max(1_032),
155
+ }),
156
+ limitations: z.array(z.string().min(1).max(2_048)).max(64),
157
+ });
@@ -0,0 +1,45 @@
1
+ import { createHash } from "node:crypto";
2
+ import canonicalize from "canonicalize";
3
+ const BUILT_IN_RULES = [
4
+ "ignore_step_elapsed_ms",
5
+ "ignore_event_sequence",
6
+ "ignore_event_step_index",
7
+ "compare_screenshot_content_digest",
8
+ "compare_normalized_text_content",
9
+ ];
10
+ /** Canonically order and commit a parsed browser scenario normalization policy. */
11
+ export const commitBrowserScenarioNormalization = (input) => {
12
+ const rules = [...input.rules]
13
+ .map((rule) => ({
14
+ ...rule,
15
+ artifacts: [...rule.artifacts].sort(),
16
+ }))
17
+ .sort((left, right) => left.rule_id < right.rule_id ? -1 : left.rule_id > right.rule_id ? 1 : 0);
18
+ const commitment = { built_in_rules: [...BUILT_IN_RULES], rules };
19
+ return { ...commitment, sha256: digestCanonicalJson(commitment) };
20
+ };
21
+ /** Digest one artifact after applying committed exact-literal rules. */
22
+ export const digestNormalizedScenarioValue = (value, artifact, rules) => digestCanonicalJson(normalize(value, artifact, rules));
23
+ /** Compute a canonical SHA-256 commitment for JSON-compatible domain data. */
24
+ export const digestCanonicalJson = (value) => {
25
+ const encoded = canonicalize(value);
26
+ if (encoded === undefined)
27
+ throw new TypeError("Expected canonical JSON");
28
+ return createHash("sha256").update(encoded).digest("hex");
29
+ };
30
+ const normalize = (value, artifact, rules) => {
31
+ if (artifact === "screenshot")
32
+ return value;
33
+ if (typeof value === "string")
34
+ return rules
35
+ .filter(({ artifacts }) => artifacts.includes(artifact))
36
+ .reduce((normalized, rule) => normalized.split(rule.match).join(rule.replacement), value);
37
+ if (Array.isArray(value))
38
+ return value.map((item) => normalize(item, artifact, rules));
39
+ if (typeof value !== "object" || value === null)
40
+ return value;
41
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [
42
+ key,
43
+ normalize(item, artifact, rules),
44
+ ]));
45
+ };
@@ -0,0 +1,372 @@
1
+ import { z } from "zod";
2
+ import { browserAllowedOriginsSchema, browserEndpointSchema, browserOriginSchema, } from "./browserObservation.js";
3
+ export const BROWSER_SCENARIO_LIMITS = {
4
+ actions: 128,
5
+ secrets: 64,
6
+ storageEntries: 512,
7
+ replayRoutes: 256,
8
+ durationMs: 300_000,
9
+ actionTimeoutMs: 60_000,
10
+ };
11
+ export const scenarioIdentifierSchema = z
12
+ .string()
13
+ .regex(/^[A-Za-z][A-Za-z0-9._-]{0,63}$/u);
14
+ const absoluteExecutablePathSchema = z
15
+ .string()
16
+ .min(1)
17
+ .max(4_096)
18
+ .refine((value) => value.startsWith("/") ||
19
+ /^[A-Za-z]:[\\/]/u.test(value) ||
20
+ value.startsWith("\\\\"), "Expected an absolute browser executable path");
21
+ const browserScenarioBaseUrlSchema = z
22
+ .string()
23
+ .min(1)
24
+ .max(65_536)
25
+ .transform((value, context) => {
26
+ let url;
27
+ try {
28
+ url = new URL(value);
29
+ }
30
+ catch {
31
+ context.addIssue({ code: "custom", message: "Invalid browser URL" });
32
+ return z.NEVER;
33
+ }
34
+ if ((url.protocol !== "http:" && url.protocol !== "https:") ||
35
+ url.username !== "" ||
36
+ url.password !== "" ||
37
+ url.search !== "" ||
38
+ url.hash !== "") {
39
+ context.addIssue({
40
+ code: "custom",
41
+ message: "Browser URLs must be HTTP(S), omit credentials, and declare query values separately",
42
+ });
43
+ return z.NEVER;
44
+ }
45
+ return url.toString();
46
+ });
47
+ export const browserScenarioValueSchema = z.discriminatedUnion("source", [
48
+ z.strictObject({
49
+ source: z.literal("literal"),
50
+ value: z.string().max(65_536),
51
+ classification: z.literal("public"),
52
+ }),
53
+ z.strictObject({
54
+ source: z.literal("secret"),
55
+ secret_id: scenarioIdentifierSchema,
56
+ }),
57
+ ]);
58
+ const queryEntrySchema = z.strictObject({
59
+ name: z.string().min(1).max(256),
60
+ value: browserScenarioValueSchema,
61
+ });
62
+ /** URL whose values remain explicit public literals or declared secret references. */
63
+ export const browserScenarioUrlSchema = z.strictObject({
64
+ url: browserScenarioBaseUrlSchema,
65
+ query: z.array(queryEntrySchema).max(64).default([]),
66
+ });
67
+ export const browserScenarioBrowserSchema = z.discriminatedUnion("mode", [
68
+ z.strictObject({
69
+ mode: z.literal("launch"),
70
+ executable_path: absoluteExecutablePathSchema,
71
+ headless: z.literal(true),
72
+ user_data: z.literal("temporary-owned"),
73
+ cleanup: z.literal("close-and-delete-profile"),
74
+ }),
75
+ z.strictObject({
76
+ mode: z.literal("connect"),
77
+ cdp_endpoint: browserEndpointSchema,
78
+ target_id: z.string().trim().min(1).max(256),
79
+ ownership: z.literal("external"),
80
+ cleanup: z.literal("disconnect-only"),
81
+ }),
82
+ ]);
83
+ export const browserScenarioEnvironmentSchema = z.strictObject({
84
+ viewport: z.strictObject({
85
+ width: z.number().int().min(320).max(7_680),
86
+ height: z.number().int().min(240).max(4_320),
87
+ device_scale_factor: z.number().min(0.5).max(4).default(1),
88
+ }),
89
+ locale: z
90
+ .string()
91
+ .min(2)
92
+ .max(64)
93
+ .regex(/^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$/u),
94
+ timezone: z
95
+ .string()
96
+ .min(1)
97
+ .max(128)
98
+ .regex(/^[A-Za-z0-9_+-]+(?:\/[A-Za-z0-9_+-]+)*$/u),
99
+ color_scheme: z.enum(["light", "dark", "no-preference"]),
100
+ reduced_motion: z.enum(["reduce", "no-preference"]),
101
+ service_workers: z.literal("block"),
102
+ });
103
+ const locatorSchema = z.discriminatedUnion("kind", [
104
+ z.strictObject({
105
+ kind: z.literal("test_id"),
106
+ value: z.string().min(1).max(512),
107
+ }),
108
+ z.strictObject({
109
+ kind: z.literal("role"),
110
+ role: z.enum([
111
+ "button",
112
+ "checkbox",
113
+ "combobox",
114
+ "dialog",
115
+ "link",
116
+ "listbox",
117
+ "menuitem",
118
+ "option",
119
+ "radio",
120
+ "slider",
121
+ "spinbutton",
122
+ "switch",
123
+ "tab",
124
+ "textbox",
125
+ ]),
126
+ name: z.string().min(1).max(1_024),
127
+ exact: z.literal(true),
128
+ }),
129
+ z.strictObject({
130
+ kind: z.literal("css"),
131
+ selector: z.string().min(1).max(4_096),
132
+ approved: z.literal(true),
133
+ }),
134
+ ]);
135
+ const stepBase = {
136
+ step_id: scenarioIdentifierSchema,
137
+ timeout_ms: z
138
+ .number()
139
+ .int()
140
+ .min(1)
141
+ .max(BROWSER_SCENARIO_LIMITS.actionTimeoutMs)
142
+ .optional(),
143
+ };
144
+ export const browserScenarioActionSchema = z.discriminatedUnion("action", [
145
+ z.strictObject({
146
+ ...stepBase,
147
+ action: z.literal("goto"),
148
+ destination: browserScenarioUrlSchema,
149
+ wait_until: z.enum(["commit", "domcontentloaded", "load"]),
150
+ }),
151
+ z.strictObject({
152
+ ...stepBase,
153
+ action: z.literal("click"),
154
+ locator: locatorSchema,
155
+ button: z.enum(["left", "middle", "right"]).default("left"),
156
+ click_count: z.number().int().min(1).max(3).default(1),
157
+ }),
158
+ z.strictObject({
159
+ ...stepBase,
160
+ action: z.literal("fill"),
161
+ locator: locatorSchema,
162
+ value: browserScenarioValueSchema,
163
+ }),
164
+ z.strictObject({
165
+ ...stepBase,
166
+ action: z.literal("press"),
167
+ locator: locatorSchema,
168
+ key: z.string().min(1).max(64),
169
+ }),
170
+ z.strictObject({
171
+ ...stepBase,
172
+ action: z.literal("select_option"),
173
+ locator: locatorSchema,
174
+ value: browserScenarioValueSchema,
175
+ }),
176
+ z.strictObject({
177
+ ...stepBase,
178
+ action: z.enum(["check", "uncheck"]),
179
+ locator: locatorSchema,
180
+ }),
181
+ z.strictObject({
182
+ ...stepBase,
183
+ action: z.literal("wait_for"),
184
+ locator: locatorSchema,
185
+ state: z.enum(["attached", "detached", "visible", "hidden"]),
186
+ }),
187
+ z.strictObject({
188
+ step_id: scenarioIdentifierSchema,
189
+ action: z.literal("wait_for_timeout"),
190
+ duration_ms: z
191
+ .number()
192
+ .int()
193
+ .min(1)
194
+ .max(BROWSER_SCENARIO_LIMITS.actionTimeoutMs),
195
+ }),
196
+ ]);
197
+ const storageEntrySchema = z.strictObject({
198
+ name: z.string().min(1).max(1_024),
199
+ value: browserScenarioValueSchema,
200
+ });
201
+ export const browserScenarioStorageSchema = z.strictObject({
202
+ cookies: z
203
+ .array(z.strictObject({
204
+ name: z.string().min(1).max(1_024),
205
+ value: browserScenarioValueSchema,
206
+ destination: browserScenarioUrlSchema,
207
+ http_only: z.boolean(),
208
+ secure: z.boolean(),
209
+ same_site: z.enum(["Strict", "Lax", "None"]),
210
+ }))
211
+ .max(128)
212
+ .default([]),
213
+ local_storage: z
214
+ .array(z.strictObject({
215
+ origin: browserOriginSchema,
216
+ entries: z.array(storageEntrySchema).max(128),
217
+ }))
218
+ .max(32)
219
+ .default([]),
220
+ session_storage: z
221
+ .array(z.strictObject({
222
+ origin: browserOriginSchema,
223
+ entries: z.array(storageEntrySchema).max(128),
224
+ }))
225
+ .max(32)
226
+ .default([]),
227
+ });
228
+ const headerNameSchema = z
229
+ .string()
230
+ .min(1)
231
+ .max(256)
232
+ .regex(/^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/u)
233
+ .transform((value) => value.toLowerCase());
234
+ const replayHeaderSchema = z.strictObject({
235
+ name: headerNameSchema,
236
+ value: browserScenarioValueSchema,
237
+ });
238
+ const replayResponseSchema = z.discriminatedUnion("kind", [
239
+ z.strictObject({
240
+ kind: z.literal("response"),
241
+ status: z.number().int().min(100).max(599),
242
+ headers: z.array(replayHeaderSchema).max(64).default([]),
243
+ body: browserScenarioValueSchema.optional(),
244
+ }),
245
+ z.strictObject({
246
+ kind: z.literal("redirect"),
247
+ status: z.union([
248
+ z.literal(301),
249
+ z.literal(302),
250
+ z.literal(303),
251
+ z.literal(307),
252
+ z.literal(308),
253
+ ]),
254
+ destination: browserScenarioUrlSchema,
255
+ }),
256
+ ]);
257
+ const replayRouteSchema = z.strictObject({
258
+ route_id: scenarioIdentifierSchema,
259
+ method: z.enum(["GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"]),
260
+ request: browserScenarioUrlSchema,
261
+ response: replayResponseSchema,
262
+ });
263
+ export const browserScenarioRequestReplaySchema = z.discriminatedUnion("mode", [
264
+ z.strictObject({ mode: z.literal("disabled") }),
265
+ z.strictObject({
266
+ mode: z.literal("exact"),
267
+ unmatched: z.enum(["abort", "passthrough-approved-origins"]),
268
+ routes: z
269
+ .array(replayRouteSchema)
270
+ .min(1)
271
+ .max(BROWSER_SCENARIO_LIMITS.replayRoutes),
272
+ }),
273
+ ]);
274
+ export const browserScenarioSecretSchema = z.strictObject({
275
+ secret_id: scenarioIdentifierSchema,
276
+ environment_variable: z.string().regex(/^[A-Z_][A-Z0-9_]{0,127}$/u),
277
+ purpose: z.enum(["input", "storage", "request-replay"]),
278
+ redaction: z.literal("replace-with-secret-reference"),
279
+ });
280
+ const REQUIRED_REDACTED_HEADERS = [
281
+ "authorization",
282
+ "cookie",
283
+ "proxy-authorization",
284
+ "set-cookie",
285
+ ];
286
+ const normalizedNames = (maximum) => z
287
+ .array(z
288
+ .string()
289
+ .trim()
290
+ .min(1)
291
+ .max(256)
292
+ .transform((value) => value.toLowerCase()))
293
+ .max(maximum)
294
+ .transform((values) => [...new Set(values)].sort());
295
+ export const browserScenarioRedactionSchema = z
296
+ .strictObject({
297
+ secret_values: z.literal("replace-with-secret-reference"),
298
+ query_parameter_names: normalizedNames(128).default([]),
299
+ header_names: normalizedNames(128).default([...REQUIRED_REDACTED_HEADERS]),
300
+ })
301
+ .superRefine(({ header_names: names }, context) => {
302
+ for (const required of REQUIRED_REDACTED_HEADERS)
303
+ if (!names.includes(required))
304
+ context.addIssue({
305
+ code: "custom",
306
+ path: ["header_names"],
307
+ message: `Required credential header ${required} must be redacted`,
308
+ });
309
+ });
310
+ const snapshotKindSchema = z.enum([
311
+ "screenshot",
312
+ "dom",
313
+ "accessibility",
314
+ "url",
315
+ "history",
316
+ "storage",
317
+ ]);
318
+ export const browserScenarioCaptureSchema = z.strictObject({
319
+ after_each_step: z.array(snapshotKindSchema).max(6),
320
+ at_end: z.array(snapshotKindSchema).max(6),
321
+ events: z.array(z.enum([
322
+ "console",
323
+ "page-errors",
324
+ "network",
325
+ "websockets",
326
+ "frames",
327
+ "workers",
328
+ "popups",
329
+ "downloads",
330
+ ])),
331
+ });
332
+ export const browserScenarioCaptureLimitsSchema = z.strictObject({
333
+ max_duration_ms: z
334
+ .number()
335
+ .int()
336
+ .min(100)
337
+ .max(BROWSER_SCENARIO_LIMITS.durationMs),
338
+ action_timeout_ms: z
339
+ .number()
340
+ .int()
341
+ .min(1)
342
+ .max(BROWSER_SCENARIO_LIMITS.actionTimeoutMs),
343
+ navigation_timeout_ms: z
344
+ .number()
345
+ .int()
346
+ .min(1)
347
+ .max(BROWSER_SCENARIO_LIMITS.actionTimeoutMs),
348
+ max_events: z.number().int().min(1).max(100_000),
349
+ max_frames: z.number().int().min(1).max(1_000),
350
+ max_workers: z.number().int().min(0).max(1_000),
351
+ max_popups: z.number().int().min(0).max(100),
352
+ max_websockets: z.number().int().min(0).max(10_000),
353
+ max_dom_nodes: z.number().int().min(1).max(100_000),
354
+ max_accessibility_nodes: z.number().int().min(1).max(100_000),
355
+ max_screenshots: z.number().int().min(0).max(256),
356
+ max_screenshot_bytes: z
357
+ .number()
358
+ .int()
359
+ .min(1)
360
+ .max(8 * 1_024 * 1_024),
361
+ max_storage_entries: z
362
+ .number()
363
+ .int()
364
+ .min(0)
365
+ .max(BROWSER_SCENARIO_LIMITS.storageEntries),
366
+ max_total_metadata_bytes: z
367
+ .number()
368
+ .int()
369
+ .min(1_024)
370
+ .max(64 * 1_024 * 1_024),
371
+ });
372
+ export const browserScenarioAllowedOriginsSchema = browserAllowedOriginsSchema;