@qontinui/shared-types 0.2.3 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (543) hide show
  1. package/README.md +128 -128
  2. package/dist/{Region.d-BbpFAWF4.d.cts → Region.d-DtT3UphX.d.cts} +81 -81
  3. package/dist/{Region.d-BbpFAWF4.d.ts → Region.d-DtT3UphX.d.ts} +81 -81
  4. package/dist/{TaskRunFindingActionType.d-B4VmmbB6.d.cts → TaskRunFindingActionType.d-CNIWogcU.d.cts} +53 -53
  5. package/dist/{TaskRunFindingActionType.d-B4VmmbB6.d.ts → TaskRunFindingActionType.d-CNIWogcU.d.ts} +53 -53
  6. package/dist/accessibility.cjs.map +1 -1
  7. package/dist/accessibility.d.cts +379 -379
  8. package/dist/accessibility.d.ts +379 -379
  9. package/dist/chat.cjs.map +1 -1
  10. package/dist/{chunk-ZK5MJEGT.js → chunk-CTBDIP7K.js} +1 -1
  11. package/dist/chunk-CTBDIP7K.js.map +1 -0
  12. package/dist/{chunk-YXD4WCYQ.js → chunk-ZUZ3OLOH.js} +1 -1
  13. package/dist/chunk-ZUZ3OLOH.js.map +1 -0
  14. package/dist/config.cjs.map +1 -1
  15. package/dist/config.d.cts +105 -105
  16. package/dist/config.d.ts +105 -105
  17. package/dist/constraints.cjs.map +1 -1
  18. package/dist/constraints.d.cts +372 -372
  19. package/dist/constraints.d.ts +372 -372
  20. package/dist/execution.cjs.map +1 -1
  21. package/dist/execution.d.cts +869 -869
  22. package/dist/execution.d.ts +869 -869
  23. package/dist/execution.js +1 -1
  24. package/dist/geometry.cjs.map +1 -1
  25. package/dist/geometry.d.cts +81 -81
  26. package/dist/geometry.d.ts +81 -81
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.cts +589 -589
  29. package/dist/index.d.ts +589 -589
  30. package/dist/index.js +2 -2
  31. package/dist/index.js.map +1 -1
  32. package/dist/library.cjs.map +1 -1
  33. package/dist/rag.cjs.map +1 -1
  34. package/dist/rag.d.cts +579 -579
  35. package/dist/rag.d.ts +579 -579
  36. package/dist/runner.cjs.map +1 -1
  37. package/dist/runner.d.cts +147 -147
  38. package/dist/runner.d.ts +147 -147
  39. package/dist/scheduler.cjs.map +1 -1
  40. package/dist/scheduler.d.cts +692 -692
  41. package/dist/scheduler.d.ts +692 -692
  42. package/dist/state-machine.cjs.map +1 -1
  43. package/dist/state-machine.d.cts +1128 -1128
  44. package/dist/state-machine.d.ts +1128 -1128
  45. package/dist/targets.cjs.map +1 -1
  46. package/dist/targets.d.cts +270 -270
  47. package/dist/targets.d.ts +270 -270
  48. package/dist/task-run.cjs.map +1 -1
  49. package/dist/task-run.d.cts +1431 -1431
  50. package/dist/task-run.d.ts +1431 -1431
  51. package/dist/tauri-events.cjs.map +1 -1
  52. package/dist/tauri-events.d.cts +315 -202
  53. package/dist/tauri-events.d.ts +315 -202
  54. package/dist/tree-events.cjs.map +1 -1
  55. package/dist/tree-events.d.cts +483 -483
  56. package/dist/tree-events.d.ts +483 -483
  57. package/dist/ui-bridge-ir.cjs +37 -0
  58. package/dist/ui-bridge-ir.cjs.map +1 -1
  59. package/dist/ui-bridge-ir.d.cts +90 -1
  60. package/dist/ui-bridge-ir.d.ts +90 -1
  61. package/dist/ui-bridge-ir.js +37 -0
  62. package/dist/ui-bridge-ir.js.map +1 -1
  63. package/dist/workflow.cjs.map +1 -1
  64. package/dist/workflow.d.cts +1634 -1634
  65. package/dist/workflow.d.ts +1634 -1634
  66. package/dist/workflow.js +1 -1
  67. package/package.json +141 -141
  68. package/src/accessibility/_api.ts +17 -17
  69. package/src/accessibility/index.ts +1 -1
  70. package/src/canvas/index.ts +319 -319
  71. package/src/chat/_api.ts +23 -23
  72. package/src/chat/index.ts +1 -1
  73. package/src/config/_api.ts +13 -13
  74. package/src/config/index.ts +1 -1
  75. package/src/constraints/_api.ts +17 -17
  76. package/src/constraints/index.ts +1 -1
  77. package/src/discovery.ts +188 -188
  78. package/src/execution/_api.ts +483 -483
  79. package/src/execution/index.ts +1 -1
  80. package/src/generated/A11yAction.d.ts +11 -11
  81. package/src/generated/AccessibilityBackend.d.ts +12 -12
  82. package/src/generated/AccessibilityBounds.d.ts +17 -17
  83. package/src/generated/AccessibilityNode.d.ts +73 -73
  84. package/src/generated/AccessibilityRole.d.ts +93 -93
  85. package/src/generated/AccessibilityRoleCriterion.d.ts +13 -13
  86. package/src/generated/AccessibilitySelector.d.ts +76 -76
  87. package/src/generated/AccessibilitySnapshot.d.ts +43 -43
  88. package/src/generated/AccessibilityState.d.ts +67 -67
  89. package/src/generated/ActionExecutionBatch.d.ts +23 -23
  90. package/src/generated/ActionExecutionBatchResponse.d.ts +24 -24
  91. package/src/generated/ActionExecutionCreate.d.ts +124 -124
  92. package/src/generated/ActionExecutionListResponse.d.ts +20 -20
  93. package/src/generated/ActionExecutionResponse.d.ts +24 -24
  94. package/src/generated/ActionReliabilityStats.d.ts +54 -54
  95. package/src/generated/ActionResponse.d.ts +45 -45
  96. package/src/generated/ActionStatus.d.ts +11 -11
  97. package/src/generated/ActionType.d.ts +41 -41
  98. package/src/generated/ActiveStatesResult.d.ts +32 -32
  99. package/src/generated/AgenticPhaseOutput.d.ts +50 -50
  100. package/src/generated/AgenticStatus.d.ts +11 -11
  101. package/src/generated/AiWorkflow.d.ts +76 -76
  102. package/src/generated/ApiAssertion.d.ts +35 -35
  103. package/src/generated/ApiAssertionOperator.d.ts +11 -11
  104. package/src/generated/ApiAssertionType.d.ts +11 -11
  105. package/src/generated/ApiContentType.d.ts +11 -11
  106. package/src/generated/ApiVariableExtraction.d.ts +24 -24
  107. package/src/generated/AvailableTransitionsResult.d.ts +34 -34
  108. package/src/generated/BaseStepFields.d.ts +72 -72
  109. package/src/generated/BatchComputeEmbeddingRequest.d.ts +27 -27
  110. package/src/generated/BatchComputeEmbeddingResponse.d.ts +23 -23
  111. package/src/generated/BatchEmbeddingResult.d.ts +23 -23
  112. package/src/generated/BetweenIterations.d.ts +35 -35
  113. package/src/generated/BoundingBox.d.ts +18 -18
  114. package/src/generated/BuildPhaseConfig.d.ts +24 -24
  115. package/src/generated/BuiltinOverrideProposal.d.ts +27 -27
  116. package/src/generated/CanonicalStep.d.ts +60 -60
  117. package/src/generated/CatchUpPolicy.d.ts +12 -12
  118. package/src/generated/Category.d.ts +24 -24
  119. package/src/generated/CheckIssueDetail.d.ts +40 -40
  120. package/src/generated/CheckType.d.ts +21 -21
  121. package/src/generated/CodeExecutionStep.d.ts +88 -88
  122. package/src/generated/CommandMode.d.ts +11 -11
  123. package/src/generated/CommandStep.d.ts +183 -183
  124. package/src/generated/CommandStepPhase.d.ts +11 -11
  125. package/src/generated/ComponentActionInfo.d.ts +24 -24
  126. package/src/generated/ComponentActionRequest.d.ts +22 -22
  127. package/src/generated/ComputeEmbeddingRequest.d.ts +24 -24
  128. package/src/generated/ComputeEmbeddingResponse.d.ts +38 -38
  129. package/src/generated/ComputeTextEmbeddingRequest.d.ts +20 -20
  130. package/src/generated/ComputeTextEmbeddingResponse.d.ts +32 -32
  131. package/src/generated/ConditionScheduleConfig.d.ts +17 -17
  132. package/src/generated/ConditionStatus.d.ts +29 -29
  133. package/src/generated/Confidence.d.ts +11 -11
  134. package/src/generated/ConfidenceLevel.d.ts +11 -11
  135. package/src/generated/Constraint.d.ts +33 -33
  136. package/src/generated/ConstraintCheck.d.ts +64 -64
  137. package/src/generated/ConstraintProposal.d.ts +37 -37
  138. package/src/generated/ConstraintResult.d.ts +32 -32
  139. package/src/generated/ConstraintSeverity.d.ts +16 -16
  140. package/src/generated/ConstraintViolation.d.ts +24 -24
  141. package/src/generated/Context.d.ts +50 -50
  142. package/src/generated/ContextAutoInclude.d.ts +34 -34
  143. package/src/generated/CoordinateSystem.d.ts +12 -12
  144. package/src/generated/Coordinates.d.ts +33 -33
  145. package/src/generated/CostTrendDataPoint.d.ts +36 -36
  146. package/src/generated/CostTrendResponse.d.ts +41 -41
  147. package/src/generated/CoverageData.d.ts +52 -52
  148. package/src/generated/CreateMcpCallInput.d.ts +59 -59
  149. package/src/generated/CreateMcpServerInput.d.ts +48 -48
  150. package/src/generated/CreateOlConfigRequest.d.ts +27 -27
  151. package/src/generated/CreateScheduledTaskRequest.d.ts +63 -63
  152. package/src/generated/CreateTaskRunRequest.d.ts +51 -51
  153. package/src/generated/CriterionOverride.d.ts +39 -39
  154. package/src/generated/CriterionType.d.ts +11 -11
  155. package/src/generated/DagApprovalStep.d.ts +80 -80
  156. package/src/generated/DagCancelStep.d.ts +76 -76
  157. package/src/generated/DagLoopStep.d.ts +80 -80
  158. package/src/generated/DecomposerConfig.d.ts +31 -31
  159. package/src/generated/DiagnosePhaseConfig.d.ts +34 -34
  160. package/src/generated/DiagnosticResult.d.ts +40 -40
  161. package/src/generated/DiscoveredElement.d.ts +48 -48
  162. package/src/generated/DiscoveredState.d.ts +49 -49
  163. package/src/generated/DiscoveredStateImage.d.ts +56 -56
  164. package/src/generated/DiscoveredTransition.d.ts +44 -44
  165. package/src/generated/DiscoveryBoundingBox.d.ts +32 -32
  166. package/src/generated/DiscoveryRequest.d.ts +40 -40
  167. package/src/generated/DiscoveryResponse.d.ts +32 -32
  168. package/src/generated/DiscoverySourceType.d.ts +14 -14
  169. package/src/generated/DiscoveryStrategy.d.ts +13 -13
  170. package/src/generated/DiscoveryTransitionTrigger.d.ts +38 -38
  171. package/src/generated/DisplayNode.d.ts +45 -45
  172. package/src/generated/DomainAssignment.d.ts +50 -50
  173. package/src/generated/DomainKnowledge.d.ts +29 -29
  174. package/src/generated/DomainVerificationResult.d.ts +36 -36
  175. package/src/generated/ElementActionRequest.d.ts +28 -28
  176. package/src/generated/ElementBbox.d.ts +36 -36
  177. package/src/generated/ElementIdentifier.d.ts +40 -40
  178. package/src/generated/ElementRect.d.ts +48 -48
  179. package/src/generated/ElementState.d.ts +47 -47
  180. package/src/generated/ElementType.d.ts +45 -45
  181. package/src/generated/EmbeddedElement.d.ts +36 -36
  182. package/src/generated/EmbeddingItem.d.ts +49 -49
  183. package/src/generated/EmbeddingListResponse.d.ts +22 -22
  184. package/src/generated/EmbeddingResultItem.d.ts +29 -29
  185. package/src/generated/EmbeddingResultsRequest.d.ts +19 -19
  186. package/src/generated/EmbeddingResultsResponse.d.ts +17 -17
  187. package/src/generated/ErrorType.d.ts +18 -18
  188. package/src/generated/ExecutePlaybookStep.d.ts +84 -84
  189. package/src/generated/ExecutionActionType.d.ts +40 -40
  190. package/src/generated/ExecutionIssueBatch.d.ts +19 -19
  191. package/src/generated/ExecutionIssueBatchResponse.d.ts +24 -24
  192. package/src/generated/ExecutionIssueCreate.d.ts +57 -57
  193. package/src/generated/ExecutionIssueDetail.d.ts +89 -89
  194. package/src/generated/ExecutionIssueListResponse.d.ts +26 -26
  195. package/src/generated/ExecutionIssueResponse.d.ts +24 -24
  196. package/src/generated/ExecutionIssueUpdate.d.ts +31 -31
  197. package/src/generated/ExecutionMatchLocation.d.ts +31 -31
  198. package/src/generated/ExecutionRunComplete.d.ts +34 -34
  199. package/src/generated/ExecutionRunCompleteResponse.d.ts +38 -38
  200. package/src/generated/ExecutionRunCreate.d.ts +40 -40
  201. package/src/generated/ExecutionRunDetail.d.ts +76 -76
  202. package/src/generated/ExecutionRunListResponse.d.ts +22 -22
  203. package/src/generated/ExecutionRunResponse.d.ts +41 -41
  204. package/src/generated/ExecutionScreenshotCreate.d.ts +59 -59
  205. package/src/generated/ExecutionScreenshotResponse.d.ts +36 -36
  206. package/src/generated/ExecutionStats.d.ts +56 -56
  207. package/src/generated/ExecutionStep.d.ts +78 -78
  208. package/src/generated/ExecutionTreeResponse.d.ts +42 -42
  209. package/src/generated/ExecutionTrendDataPoint.d.ts +36 -36
  210. package/src/generated/ExecutionTrendResponse.d.ts +46 -46
  211. package/src/generated/ExitCheckResult.d.ts +20 -20
  212. package/src/generated/ExitStrategy.d.ts +27 -27
  213. package/src/generated/ExportResult.d.ts +30 -30
  214. package/src/generated/ExtendIterationsRequest.d.ts +20 -20
  215. package/src/generated/FileChange.d.ts +20 -20
  216. package/src/generated/Finding.d.ts +40 -40
  217. package/src/generated/FindingActionType.d.ts +13 -13
  218. package/src/generated/FindingBatchCreate.d.ts +27 -27
  219. package/src/generated/FindingCategory.d.ts +25 -25
  220. package/src/generated/FindingCodeContext.d.ts +31 -31
  221. package/src/generated/FindingCreate.d.ts +52 -52
  222. package/src/generated/FindingDetail.d.ts +78 -78
  223. package/src/generated/FindingListResponse.d.ts +40 -40
  224. package/src/generated/FindingOutput.d.ts +36 -36
  225. package/src/generated/FindingSeverity.d.ts +14 -14
  226. package/src/generated/FindingStatus.d.ts +15 -15
  227. package/src/generated/FindingSummary.d.ts +53 -53
  228. package/src/generated/FindingUpdate.d.ts +33 -33
  229. package/src/generated/FindingUserInput.d.ts +27 -27
  230. package/src/generated/FindingsSummary.d.ts +47 -47
  231. package/src/generated/FullRunnerStep.d.ts +144 -144
  232. package/src/generated/GUIElementChunk.d.ts +151 -151
  233. package/src/generated/GateEvaluationResult.d.ts +36 -36
  234. package/src/generated/HealthCheckUrl.d.ts +35 -35
  235. package/src/generated/HistoricalActionQuery.d.ts +42 -42
  236. package/src/generated/HistoricalActionResult.d.ts +57 -57
  237. package/src/generated/HttpConfig.d.ts +27 -27
  238. package/src/generated/HttpMethod.d.ts +13 -13
  239. package/src/generated/IdleCondition.d.ts +17 -17
  240. package/src/generated/ImplementFixesConfig.d.ts +25 -25
  241. package/src/generated/IndividualCheckResult.d.ts +50 -50
  242. package/src/generated/InitialStateRef.d.ts +23 -23
  243. package/src/generated/InitialStatesSource.d.ts +18 -18
  244. package/src/generated/IssueSeverity.d.ts +11 -11
  245. package/src/generated/IssueSource.d.ts +11 -11
  246. package/src/generated/IssueStatus.d.ts +11 -11
  247. package/src/generated/IssueType.d.ts +21 -21
  248. package/src/generated/IterationResult.d.ts +65 -65
  249. package/src/generated/IterationVerificationResults.d.ts +53 -53
  250. package/src/generated/JobItem.d.ts +31 -31
  251. package/src/generated/JobListResponse.d.ts +20 -20
  252. package/src/generated/JobStatus.d.ts +11 -11
  253. package/src/generated/JobSummary.d.ts +30 -30
  254. package/src/generated/LLMCostSummary.d.ts +38 -38
  255. package/src/generated/LLMMetrics.d.ts +42 -42
  256. package/src/generated/LogSourceSelection.d.ts +25 -25
  257. package/src/generated/LoopInstanceStatus.d.ts +28 -28
  258. package/src/generated/LoopPhase.d.ts +26 -26
  259. package/src/generated/MatchAdjustment.d.ts +23 -23
  260. package/src/generated/MatchLocation.d.ts +22 -22
  261. package/src/generated/MatchMethod.d.ts +11 -11
  262. package/src/generated/McpCallRecord.d.ts +35 -35
  263. package/src/generated/McpCallsResult.d.ts +44 -44
  264. package/src/generated/McpConnectionRef.d.ts +25 -25
  265. package/src/generated/McpServerConfig.d.ts +77 -77
  266. package/src/generated/McpServerStatus.d.ts +42 -42
  267. package/src/generated/McpToolCallResult.d.ts +38 -38
  268. package/src/generated/McpToolInfo.d.ts +23 -23
  269. package/src/generated/McpToolInputSchema.d.ts +34 -34
  270. package/src/generated/McpTransport.d.ts +15 -15
  271. package/src/generated/ModelCostBreakdown.d.ts +36 -36
  272. package/src/generated/ModelOverrideConfig.d.ts +47 -47
  273. package/src/generated/Monitor.d.ts +48 -48
  274. package/src/generated/MonitorPosition.d.ts +13 -13
  275. package/src/generated/MouseButton.d.ts +13 -13
  276. package/src/generated/MultiLoopConfig.d.ts +32 -32
  277. package/src/generated/MultiLoopEntry.d.ts +32 -32
  278. package/src/generated/MultiLoopStatus.d.ts +37 -37
  279. package/src/generated/NativeAccessibilityStep.d.ts +121 -121
  280. package/src/generated/NavigationResult.d.ts +40 -40
  281. package/src/generated/NewConstraintProposal.d.ts +20 -20
  282. package/src/generated/NextTaskInfo.d.ts +24 -24
  283. package/src/generated/NodeMetadata.d.ts +47 -47
  284. package/src/generated/NodeStatus.d.ts +11 -11
  285. package/src/generated/NodeType.d.ts +11 -11
  286. package/src/generated/OcrEngine.d.ts +11 -11
  287. package/src/generated/OlConfig.d.ts +48 -48
  288. package/src/generated/OrchestrationLoopConfig.d.ts +87 -87
  289. package/src/generated/OrchestrationLoopStatus.d.ts +59 -59
  290. package/src/generated/Outcome.d.ts +15 -15
  291. package/src/generated/OutputLine.d.ts +23 -23
  292. package/src/generated/OutputStream.d.ts +11 -11
  293. package/src/generated/OverrideCollection.d.ts +18 -18
  294. package/src/generated/Pagination.d.ts +28 -28
  295. package/src/generated/ParserType.d.ts +17 -17
  296. package/src/generated/PathElement.d.ts +17 -17
  297. package/src/generated/PathfindingRequest.d.ts +24 -24
  298. package/src/generated/PathfindingResult.d.ts +30 -30
  299. package/src/generated/PathfindingStep.d.ts +36 -36
  300. package/src/generated/PatternOptions.d.ts +32 -32
  301. package/src/generated/PipelineConfig.d.ts +31 -31
  302. package/src/generated/PlaybackFrameRequest.d.ts +21 -21
  303. package/src/generated/PlaywrightExecutionMode.d.ts +11 -11
  304. package/src/generated/Point.d.ts +20 -20
  305. package/src/generated/PollingConfig.d.ts +14 -14
  306. package/src/generated/ProcessConfig.d.ts +96 -96
  307. package/src/generated/ProcessState.d.ts +15 -15
  308. package/src/generated/ProcessStatus.d.ts +41 -41
  309. package/src/generated/PromptStep.d.ts +92 -92
  310. package/src/generated/PromptStepPhase.d.ts +13 -13
  311. package/src/generated/RAGDashboardStats.d.ts +26 -26
  312. package/src/generated/RagCompletionEvent.d.ts +25 -25
  313. package/src/generated/RagProcessingStatus.d.ts +11 -11
  314. package/src/generated/RagProgressEvent.d.ts +27 -27
  315. package/src/generated/ReadConfigResponse.d.ts +20 -20
  316. package/src/generated/ReflectionFixOutput.d.ts +20 -20
  317. package/src/generated/Region.d.ts +39 -39
  318. package/src/generated/RepositoryInactiveCondition.d.ts +24 -24
  319. package/src/generated/RepositoryWatch.d.ts +20 -20
  320. package/src/generated/ResolvedInitialStates.d.ts +29 -29
  321. package/src/generated/ResolvedInitialStatesResult.d.ts +40 -40
  322. package/src/generated/ResourceLimits.d.ts +33 -33
  323. package/src/generated/RestartProcessStep.d.ts +88 -88
  324. package/src/generated/RetryPolicy.d.ts +24 -24
  325. package/src/generated/RetrySpec.d.ts +24 -24
  326. package/src/generated/RoleCriterion.d.ts +14 -14
  327. package/src/generated/RootCauseCategory.d.ts +18 -18
  328. package/src/generated/RoutingRule.d.ts +38 -38
  329. package/src/generated/RunPromptRequest.d.ts +56 -56
  330. package/src/generated/RunPromptResponse.d.ts +50 -50
  331. package/src/generated/RunPromptResponseData.d.ts +20 -20
  332. package/src/generated/RunStatus.d.ts +11 -11
  333. package/src/generated/RunType.d.ts +11 -11
  334. package/src/generated/Runner.d.ts +81 -81
  335. package/src/generated/RunnerCrash.d.ts +32 -32
  336. package/src/generated/RunnerFinding.d.ts +48 -48
  337. package/src/generated/RunnerFindingCodeContext.d.ts +20 -20
  338. package/src/generated/RunnerFindingUserInput.d.ts +19 -19
  339. package/src/generated/RunnerMetadata.d.ts +42 -42
  340. package/src/generated/RunnerStatus.d.ts +12 -12
  341. package/src/generated/RunnerUiError.d.ts +28 -28
  342. package/src/generated/RuntimeData.d.ts +40 -40
  343. package/src/generated/SaveWorkflowArtifactStep.d.ts +80 -80
  344. package/src/generated/ScheduleConditions.d.ts +29 -29
  345. package/src/generated/ScheduleExpression.d.ts +39 -39
  346. package/src/generated/ScheduledTask.d.ts +104 -104
  347. package/src/generated/ScheduledTaskStatus.d.ts +19 -19
  348. package/src/generated/ScheduledTaskType.d.ts +126 -126
  349. package/src/generated/SchedulerSettings.d.ts +28 -28
  350. package/src/generated/SchedulerStatus.d.ts +31 -31
  351. package/src/generated/ScreenshotAnnotation.d.ts +39 -39
  352. package/src/generated/ScreenshotAnnotationShape.d.ts +11 -11
  353. package/src/generated/ScreenshotType.d.ts +11 -11
  354. package/src/generated/ScrollDirection.d.ts +13 -13
  355. package/src/generated/SearchOptions.d.ts +33 -33
  356. package/src/generated/SearchResultItem.d.ts +19 -19
  357. package/src/generated/SearchStrategy.d.ts +14 -14
  358. package/src/generated/SemanticSearchRequest.d.ts +26 -26
  359. package/src/generated/SemanticSearchResponse.d.ts +21 -21
  360. package/src/generated/StageCondition.d.ts +37 -37
  361. package/src/generated/StageInput.d.ts +27 -27
  362. package/src/generated/StageOutput.d.ts +20 -20
  363. package/src/generated/StageTransition.d.ts +30 -30
  364. package/src/generated/StallDetectorConfig.d.ts +31 -31
  365. package/src/generated/StandardActionType.d.ts +40 -40
  366. package/src/generated/StateContext.d.ts +17 -17
  367. package/src/generated/StateDiscoveryResult.d.ts +104 -104
  368. package/src/generated/StateDiscoveryResultCreate.d.ts +65 -65
  369. package/src/generated/StateDiscoveryResultListResponse.d.ts +23 -23
  370. package/src/generated/StateDiscoveryResultSummary.d.ts +57 -57
  371. package/src/generated/StateDiscoveryResultUpdate.d.ts +47 -47
  372. package/src/generated/StateFilterItem.d.ts +18 -18
  373. package/src/generated/StateMachineConfig.d.ts +45 -45
  374. package/src/generated/StateMachineConfigCreate.d.ts +20 -20
  375. package/src/generated/StateMachineConfigFull.d.ts +67 -67
  376. package/src/generated/StateMachineConfigUpdate.d.ts +20 -20
  377. package/src/generated/StateMachineExport.d.ts +64 -64
  378. package/src/generated/StateMachineExportFormat.d.ts +39 -39
  379. package/src/generated/StateMachineImport.d.ts +25 -25
  380. package/src/generated/StateMachineState.d.ts +71 -71
  381. package/src/generated/StateMachineStateCreate.d.ts +52 -52
  382. package/src/generated/StateMachineStateUpdate.d.ts +48 -48
  383. package/src/generated/StateMachineTransition.d.ts +75 -75
  384. package/src/generated/StateMachineTransitionCreate.d.ts +53 -53
  385. package/src/generated/StateMachineTransitionUpdate.d.ts +53 -53
  386. package/src/generated/StateNodeData.d.ts +71 -71
  387. package/src/generated/StatesResponse.d.ts +19 -19
  388. package/src/generated/StdioConfig.d.ts +34 -34
  389. package/src/generated/StepExecutionConfig.d.ts +37 -37
  390. package/src/generated/StructuredFinding.d.ts +32 -32
  391. package/src/generated/StructuredOverride.d.ts +24 -24
  392. package/src/generated/StructuredSignal.d.ts +20 -20
  393. package/src/generated/SuccessCriterion.d.ts +57 -57
  394. package/src/generated/SummarizationConfig.d.ts +35 -35
  395. package/src/generated/TargetConfig.d.ts +108 -108
  396. package/src/generated/TaskCompletionResult.d.ts +90 -90
  397. package/src/generated/TaskExecutionRecord.d.ts +61 -61
  398. package/src/generated/TaskRun.d.ts +88 -88
  399. package/src/generated/TaskRunBackend.d.ts +86 -86
  400. package/src/generated/TaskRunBackendDetail.d.ts +99 -99
  401. package/src/generated/TaskRunCreate.d.ts +62 -62
  402. package/src/generated/TaskRunFilters.d.ts +38 -38
  403. package/src/generated/TaskRunFinding.d.ts +100 -100
  404. package/src/generated/TaskRunFindingActionType.d.ts +11 -11
  405. package/src/generated/TaskRunFindingCategory.d.ts +24 -24
  406. package/src/generated/TaskRunFindingCreate.d.ts +79 -79
  407. package/src/generated/TaskRunFindingFilters.d.ts +28 -28
  408. package/src/generated/TaskRunFindingSeverity.d.ts +11 -11
  409. package/src/generated/TaskRunFindingStatus.d.ts +11 -11
  410. package/src/generated/TaskRunFindingSummary.d.ts +34 -34
  411. package/src/generated/TaskRunFindingUpdate.d.ts +34 -34
  412. package/src/generated/TaskRunFindingsListResponse.d.ts +24 -24
  413. package/src/generated/TaskRunListResponse.d.ts +21 -21
  414. package/src/generated/TaskRunSession.d.ts +40 -40
  415. package/src/generated/TaskRunStatus.d.ts +11 -11
  416. package/src/generated/TaskRunUpdate.d.ts +47 -47
  417. package/src/generated/TaskType.d.ts +11 -11
  418. package/src/generated/TerminalExitEvent.d.ts +26 -26
  419. package/src/generated/TerminalInfo.d.ts +69 -69
  420. package/src/generated/TerminalOutputEvent.d.ts +29 -29
  421. package/src/generated/TestType.d.ts +11 -11
  422. package/src/generated/TextMatchType.d.ts +11 -11
  423. package/src/generated/TextSearchOptions.d.ts +30 -30
  424. package/src/generated/Ticket.d.ts +53 -53
  425. package/src/generated/TicketComment.d.ts +28 -28
  426. package/src/generated/TicketProviderConfig.d.ts +55 -55
  427. package/src/generated/TicketSource.d.ts +17 -17
  428. package/src/generated/TicketState.d.ts +15 -15
  429. package/src/generated/TimingInfo.d.ts +23 -23
  430. package/src/generated/TopMatch.d.ts +17 -17
  431. package/src/generated/TransitionAction.d.ts +93 -93
  432. package/src/generated/TransitionActionValue.d.ts +14 -14
  433. package/src/generated/TransitionEdgeData.d.ts +46 -46
  434. package/src/generated/TransitionExecutionResult.d.ts +30 -30
  435. package/src/generated/TransitionInfo.d.ts +29 -29
  436. package/src/generated/TransitionTriggerType.d.ts +13 -13
  437. package/src/generated/TreeEvent.d.ts +47 -47
  438. package/src/generated/TreeEventCreate.d.ts +30 -30
  439. package/src/generated/TreeEventListResponse.d.ts +30 -30
  440. package/src/generated/TreeEventResponse.d.ts +47 -47
  441. package/src/generated/TreeEventType.d.ts +20 -20
  442. package/src/generated/TreeNode.d.ts +54 -54
  443. package/src/generated/UIBridgeCapturedToast.d.ts +22 -22
  444. package/src/generated/UIBridgeComponent.d.ts +45 -45
  445. package/src/generated/UIBridgeElement.d.ts +62 -62
  446. package/src/generated/UIBridgeModalInfo.d.ts +40 -40
  447. package/src/generated/UIBridgeModalStack.d.ts +19 -19
  448. package/src/generated/UIBridgeSnapshot.d.ts +67 -67
  449. package/src/generated/UIBridgeToastContext.d.ts +17 -17
  450. package/src/generated/UIBridgeUndoContext.d.ts +19 -19
  451. package/src/generated/UiBridgeAction.d.ts +18 -18
  452. package/src/generated/UiBridgeAssertType.d.ts +11 -11
  453. package/src/generated/UiBridgeComparisonMode.d.ts +11 -11
  454. package/src/generated/UiBridgeDesignAuditStep.d.ts +76 -76
  455. package/src/generated/UiBridgeRequestEnvelope.d.ts +45 -0
  456. package/src/generated/UiBridgeResponseEnvelope.d.ts +66 -0
  457. package/src/generated/UiBridgeSeverity.d.ts +11 -11
  458. package/src/generated/UiBridgeStep.d.ts +127 -127
  459. package/src/generated/UiBridgeStepPhase.d.ts +14 -14
  460. package/src/generated/UiBridgeVisualAssertionStep.d.ts +97 -97
  461. package/src/generated/UnifiedStep.d.ts +57 -57
  462. package/src/generated/UnifiedWorkflow.d.ts +411 -411
  463. package/src/generated/UpdateMcpServerInput.d.ts +28 -28
  464. package/src/generated/UpdateOlConfigRequest.d.ts +32 -32
  465. package/src/generated/UpdateScheduledTaskRequest.d.ts +71 -71
  466. package/src/generated/ValidateConfigRequest.d.ts +16 -16
  467. package/src/generated/ValidateConfigResponse.d.ts +28 -28
  468. package/src/generated/VectorSearchResult.d.ts +48 -48
  469. package/src/generated/VerificationAgentContext.d.ts +25 -25
  470. package/src/generated/VerificationCategoryKind.d.ts +19 -19
  471. package/src/generated/VerificationMethod.d.ts +20 -20
  472. package/src/generated/VerificationPhaseResult.d.ts +63 -63
  473. package/src/generated/VerificationPlan.d.ts +43 -43
  474. package/src/generated/VerificationResult.d.ts +44 -44
  475. package/src/generated/VerificationResultResponse.d.ts +65 -65
  476. package/src/generated/VerificationResultsListResponse.d.ts +41 -41
  477. package/src/generated/VerificationStepDetails.d.ts +55 -55
  478. package/src/generated/VerificationStepResult.d.ts +68 -68
  479. package/src/generated/VgaAction.d.ts +61 -61
  480. package/src/generated/VgaActionKind.d.ts +14 -14
  481. package/src/generated/VgaAutomateStep.d.ts +110 -110
  482. package/src/generated/VirtualDesktop.d.ts +23 -23
  483. package/src/generated/VisualAssertionType.d.ts +11 -11
  484. package/src/generated/VisualComparisonResult.d.ts +40 -40
  485. package/src/generated/WaitOptions.d.ts +35 -35
  486. package/src/generated/WorkerCoordinationMessage.d.ts +93 -93
  487. package/src/generated/WorkerDomain.d.ts +29 -29
  488. package/src/generated/WorkerInstance.d.ts +62 -62
  489. package/src/generated/WorkerOutput.d.ts +53 -53
  490. package/src/generated/WorkerSignal.d.ts +48 -48
  491. package/src/generated/WorkerStatus.d.ts +11 -11
  492. package/src/generated/WorkflowArchitecture.d.ts +16 -16
  493. package/src/generated/WorkflowFixupMode.d.ts +11 -11
  494. package/src/generated/WorkflowFixupStep.d.ts +85 -85
  495. package/src/generated/WorkflowInfo.d.ts +28 -28
  496. package/src/generated/WorkflowMetadata.d.ts +44 -44
  497. package/src/generated/WorkflowRefStep.d.ts +94 -94
  498. package/src/generated/WorkflowStage.d.ts +126 -126
  499. package/src/generated/WorkflowStep.d.ts +80 -80
  500. package/src/generated/WorkflowStepPhase.d.ts +11 -11
  501. package/src/generated/WriteConfigRequest.d.ts +20 -20
  502. package/src/generated/WriteConfigResponse.d.ts +24 -24
  503. package/src/generated/index.ts +2 -0
  504. package/src/geometry/_api.ts +16 -16
  505. package/src/geometry/index.ts +1 -1
  506. package/src/index.ts +73 -73
  507. package/src/known-issues/index.ts +179 -179
  508. package/src/library/_api.ts +326 -326
  509. package/src/library/index.ts +1 -1
  510. package/src/rag/_api.ts +47 -47
  511. package/src/rag/index.ts +1 -1
  512. package/src/render-log.ts +371 -371
  513. package/src/runner/_api.ts +14 -14
  514. package/src/runner/index.ts +1 -1
  515. package/src/scheduler/_api.ts +21 -21
  516. package/src/scheduler/index.ts +1 -1
  517. package/src/state-machine/_api.ts +145 -145
  518. package/src/state-machine/index.ts +1 -1
  519. package/src/targets/_api.ts +47 -47
  520. package/src/targets/index.ts +1 -1
  521. package/src/task-run/_api.ts +108 -108
  522. package/src/task-run/index.ts +1 -1
  523. package/src/tauri-events/_api.ts +90 -76
  524. package/src/tauri-events/index.ts +1 -1
  525. package/src/tree-events/_api.ts +42 -42
  526. package/src/tree-events/index.ts +1 -1
  527. package/src/ui-bridge-ir/__tests__/projection.test.ts +1029 -1029
  528. package/src/ui-bridge-ir/adapter.ts +220 -220
  529. package/src/ui-bridge-ir/document.ts +114 -105
  530. package/src/ui-bridge-ir/element-criteria.ts +48 -48
  531. package/src/ui-bridge-ir/group.ts +84 -0
  532. package/src/ui-bridge-ir/index.ts +24 -23
  533. package/src/ui-bridge-ir/ir.schema.json +293 -235
  534. package/src/ui-bridge-ir/primitives.ts +102 -102
  535. package/src/ui-bridge-ir/projection.ts +921 -863
  536. package/src/ui-bridge-ir/state.ts +90 -90
  537. package/src/ui-bridge-ir/transition.ts +92 -92
  538. package/src/workflow/_api.ts +474 -474
  539. package/src/workflow/action-plan.ts +208 -208
  540. package/src/workflow/index.ts +1 -1
  541. package/src/workflow/skill.ts +152 -152
  542. package/dist/chunk-YXD4WCYQ.js.map +0 -1
  543. package/dist/chunk-ZK5MJEGT.js.map +0 -1
@@ -1,863 +1,921 @@
1
- /**
2
- * IR -> Bundled-Page Legacy Spec projection.
3
- *
4
- * Single point of translation between authoring-time IR (`IRDocument`) and the
5
- * legacy `*.spec.uibridge.json` shape that existing tooling still consumes
6
- * (`/update-spec`, runner spec drift / verify, error monitor curator,
7
- * spec experimentation, AI session). See ADR-001 (decision #6 / #7) for the
8
- * boundary rationale; section 2 of the UI Bridge redesign for the projection's
9
- * role in keeping legacy consumers working through the migration.
10
- *
11
- * The projection is a PURE FUNCTION — same input always produces byte-identical
12
- * output. Keys within objects are sorted lexicographically, arrays preserve
13
- * input order, and no timestamps / random IDs are introduced. Downstream tests
14
- * may hash the JSON output for byte-stable assertions.
15
- *
16
- * ---------------------------------------------------------------------------
17
- * Mapping rules (worked example below)
18
- * ---------------------------------------------------------------------------
19
- *
20
- * IRDocument LegacySpec
21
- * ---------- ----------
22
- * doc.id -> metadata.component
23
- * doc.metadata?.purpose ?? doc.id -> metadata.component (purpose wins if present)
24
- * doc.description ?? doc.name -> description
25
- * doc.metadata?.tags -> metadata.tags
26
- *
27
- * For each IRState:
28
- * state -> one entry in groups[]
29
- * state.id -> group.id
30
- * state.name -> group.name
31
- * state.description ??
32
- * state.metadata?.description -> group.description
33
- * "element-presence" -> group.category (default)
34
- * state.provenance?.source ??
35
- * "ai-generated" -> group.source
36
- *
37
- * For each requiredElement of state:
38
- * (state, criteria, index) -> one assertion in group.assertions[]
39
- * "${state.id}-elem-${index}" -> assertion.id
40
- * state.metadata?.description ?? -> assertion.description
41
- * "Required element ${index} ..."
42
- * "element-presence" -> assertion.category
43
- * "critical" -> assertion.severity
44
- * "exists" -> assertion.assertionType
45
- * {
46
- * type: "search",
47
- * criteria: convert(criteria), -> assertion.target
48
- * label: "Required element ..."
49
- * }
50
- * "ai-generated" -> assertion.source
51
- * false -> assertion.reviewed
52
- * true -> assertion.enabled
53
- *
54
- * If state has zero requiredElements, the projected `group.assertions[]`
55
- * is the empty array — legacy specs ship architecture-only groups with
56
- * empty assertions, and the inverse projection's identity round-trip
57
- * depends on preserving that emptiness. (Earlier versions emitted a
58
- * placeholder assertion here, which silently grew the assertion count
59
- * during round-trip and tripped `check-spec-pairing`.)
60
- *
61
- * Each IRState -> one entry in stateMachine.states[]
62
- * state.id -> sm-state.id
63
- * state.name -> sm-state.name
64
- * state.description ?? "" -> sm-state.description
65
- * state.requiredElements (converted) -> sm-state.elements
66
- * state.isInitial ??
67
- * (state.id === doc.initialState) -> sm-state.isInitial
68
- * transitions originating from state -> sm-state.transitions
69
- *
70
- * For each IRTransition:
71
- * For each fromState in transition.fromStates:
72
- * transition -> one entry in
73
- * stateMachine.states[fromState].transitions[]
74
- * transition.id -> sm-transition.id
75
- * transition.name -> sm-transition.name
76
- * transition.activateStates -> sm-transition.activateStates
77
- * transition.exitStates ?? [] -> sm-transition.deactivateStates
78
- * (exitStates.length === 0) -> sm-transition.staysVisible
79
- *
80
- * For each action in transition.actions:
81
- * action.type -> process[].action (rename type -> action)
82
- * action.target -> process[].target (criteria converted)
83
- * action.waitAfter -> process[].waitAfter (passed through)
84
- *
85
- * Element-criteria conversion (IRElementCriteria -> legacy criteria):
86
- * text -> textContent
87
- * ariaLabel -> accessibleName
88
- * attributes -> dataAttributes
89
- * (others — role, textContains, id — pass through unchanged)
90
- *
91
- * ---------------------------------------------------------------------------
92
- * Worked example
93
- * ---------------------------------------------------------------------------
94
- *
95
- * const ir: IRDocument = {
96
- * version: "1.0",
97
- * id: "active",
98
- * name: "Active Dashboard",
99
- * description: "Real-time monitoring hub",
100
- * metadata: { tags: ["monitoring", "tier-1"] },
101
- * states: [
102
- * {
103
- * id: "running",
104
- * name: "Running",
105
- * description: "Workflow executing",
106
- * requiredElements: [{ role: "button", text: "Stop" }],
107
- * isInitial: false,
108
- * },
109
- * ],
110
- * transitions: [
111
- * {
112
- * id: "running-to-idle",
113
- * name: "Stop",
114
- * fromStates: ["running"],
115
- * activateStates: ["idle"],
116
- * exitStates: ["running"],
117
- * actions: [
118
- * {
119
- * type: "click",
120
- * target: { role: "button", text: "Stop" },
121
- * waitAfter: { type: "idle", timeout: 3000 },
122
- * },
123
- * ],
124
- * },
125
- * ],
126
- * initialState: "idle",
127
- * };
128
- *
129
- * projectIRToBundledPage(ir, "Notes from author")
130
- * // -> {
131
- * // version: "1.0.0",
132
- * // description: "Real-time monitoring hub\n\nNotes from author",
133
- * // groups: [{
134
- * // id: "running",
135
- * // name: "Running",
136
- * // description: "Workflow executing",
137
- * // category: "element-presence",
138
- * // assertions: [{
139
- * // id: "running-elem-0",
140
- * // description: "Required element 0 for state Running",
141
- * // category: "element-presence",
142
- * // severity: "critical",
143
- * // assertionType: "exists",
144
- * // target: {
145
- * // type: "search",
146
- * // criteria: { role: "button", textContent: "Stop" },
147
- * // label: "Required element for Running",
148
- * // },
149
- * // source: "ai-generated",
150
- * // reviewed: false,
151
- * // enabled: true,
152
- * // }],
153
- * // source: "ai-generated",
154
- * // }],
155
- * // stateMachine: {
156
- * // states: [{
157
- * // id: "running",
158
- * // name: "Running",
159
- * // description: "Workflow executing",
160
- * // elements: [{ role: "button", textContent: "Stop" }],
161
- * // isInitial: false,
162
- * // transitions: [{
163
- * // id: "running-to-idle",
164
- * // name: "Stop",
165
- * // activateStates: ["idle"],
166
- * // deactivateStates: ["running"],
167
- * // staysVisible: false,
168
- * // process: [{
169
- * // action: "click",
170
- * // target: { role: "button", textContent: "Stop" },
171
- * // waitAfter: { type: "idle", timeout: 3000 },
172
- * // }],
173
- * // }],
174
- * // }],
175
- * // },
176
- * // metadata: { component: "active", tags: ["monitoring", "tier-1"] },
177
- * // }
178
- */
179
-
180
- import type { IRElementCriteria } from "./element-criteria";
181
- import type { IRDocument } from "./document";
182
- import type { IRState } from "./state";
183
- import type { IRTransition, IRTransitionAction, IRWaitSpec } from "./transition";
184
-
185
- /**
186
- * Projection version. Bump on shape changes that consumers must adapt to.
187
- * Independent from `IRVersion` because the legacy spec shape and the IR shape
188
- * evolve on different cadences.
189
- */
190
- export const projectionVersion = "1.0";
191
-
192
- // ---------------------------------------------------------------------------
193
- // Legacy spec shapes — derived from active.spec.uibridge.json (the reference
194
- // the projection's output must structurally match).
195
- // ---------------------------------------------------------------------------
196
-
197
- /**
198
- * Legacy element-criteria shape (after conversion from `IRElementCriteria`).
199
- *
200
- * The differences from `IRElementCriteria`:
201
- * - `text` -> `textContent`
202
- * - `ariaLabel` -> `accessibleName` (when IR has no explicit `accessibleName`)
203
- * - `attributes` -> `dataAttributes`
204
- *
205
- * Other fields (`role`, `tagName`, `textContains`, `accessibleName`, `id`)
206
- * pass through unchanged. Index signature stays open for legacy fields the
207
- * IR doesn't yet model.
208
- */
209
- export interface LegacyCriteria {
210
- role?: string;
211
- tagName?: string;
212
- textContent?: string;
213
- textContains?: string;
214
- accessibleName?: string;
215
- id?: string;
216
- dataAttributes?: Record<string, string>;
217
- [key: string]: unknown;
218
- }
219
-
220
- /**
221
- * Legacy assertion target. Always `type: "search"` for the projection — the
222
- * legacy schema also supports point/region targets, but the IR doesn't
223
- * express those.
224
- */
225
- export interface LegacyAssertionTarget {
226
- type: "search";
227
- criteria: LegacyCriteria;
228
- label: string;
229
- }
230
-
231
- /**
232
- * Legacy assertion. Mirrors the shape used by `groups[].assertions[]` in
233
- * `*.spec.uibridge.json`.
234
- */
235
- export interface LegacyAssertion {
236
- id: string;
237
- description: string;
238
- category: string;
239
- severity: "critical" | "warning" | "info";
240
- assertionType: string;
241
- target: LegacyAssertionTarget;
242
- source: string;
243
- reviewed: boolean;
244
- enabled: boolean;
245
- precondition?: string;
246
- }
247
-
248
- /**
249
- * Legacy group. One per IR state.
250
- */
251
- export interface LegacyGroup {
252
- id: string;
253
- name: string;
254
- description: string;
255
- category: string;
256
- assertions: LegacyAssertion[];
257
- source: string;
258
- }
259
-
260
- /**
261
- * Process step within a transition. Note `action` (not `type`) — matches the
262
- * runtime engine's `TransitionAction` shape.
263
- */
264
- export interface LegacyProcessStep {
265
- action: string;
266
- target: LegacyCriteria;
267
- waitAfter?: IRWaitSpec;
268
- }
269
-
270
- /**
271
- * Legacy state-machine transition.
272
- */
273
- export interface LegacyTransition {
274
- id: string;
275
- name: string;
276
- activateStates: string[];
277
- deactivateStates: string[];
278
- staysVisible: boolean;
279
- process: LegacyProcessStep[];
280
- }
281
-
282
- /**
283
- * Legacy state-machine state.
284
- */
285
- export interface LegacyStateMachineState {
286
- id: string;
287
- name: string;
288
- description: string;
289
- elements: LegacyCriteria[];
290
- isInitial: boolean;
291
- transitions: LegacyTransition[];
292
- }
293
-
294
- /**
295
- * Legacy state-machine block.
296
- */
297
- export interface LegacyStateMachine {
298
- states: LegacyStateMachineState[];
299
- }
300
-
301
- /**
302
- * Legacy spec metadata block. The projection writes a deterministic subset;
303
- * downstream tooling may layer additional keys on hand-authored specs.
304
- */
305
- export interface LegacyMetadata {
306
- component: string;
307
- tags?: string[];
308
- [key: string]: unknown;
309
- }
310
-
311
- /**
312
- * Top-level legacy spec shape.
313
- */
314
- export interface LegacySpec {
315
- version: string;
316
- description: string;
317
- groups: LegacyGroup[];
318
- stateMachine: LegacyStateMachine;
319
- metadata: LegacyMetadata;
320
- }
321
-
322
- // ---------------------------------------------------------------------------
323
- // Internal helpers
324
- // ---------------------------------------------------------------------------
325
-
326
- /**
327
- * Recursively sort object keys lexicographically. Arrays preserve input order
328
- * (ordering is meaningful for groups / assertions / transitions).
329
- *
330
- * Used as the final stage of every projected output to guarantee byte-stable
331
- * JSON serialization downstream.
332
- */
333
- function sortKeys<T>(value: T): T {
334
- if (Array.isArray(value)) {
335
- return value.map(sortKeys) as unknown as T;
336
- }
337
- if (value !== null && typeof value === "object") {
338
- const sorted: Record<string, unknown> = {};
339
- const keys = Object.keys(value as Record<string, unknown>).sort();
340
- for (const key of keys) {
341
- sorted[key] = sortKeys((value as Record<string, unknown>)[key]);
342
- }
343
- return sorted as unknown as T;
344
- }
345
- return value;
346
- }
347
-
348
- /**
349
- * Convert IR element criteria to legacy criteria. Renames `text` ->
350
- * `textContent`, `attributes` -> `dataAttributes`. `accessibleName` (or
351
- * `ariaLabel` as a fallback) maps to legacy `accessibleName`. `tagName`,
352
- * `role`, `textContains`, `id` pass through unchanged.
353
- */
354
- function convertCriteria(criteria: IRElementCriteria): LegacyCriteria {
355
- const out: LegacyCriteria = {};
356
- if (criteria.role !== undefined) out.role = criteria.role;
357
- if (criteria.tagName !== undefined) out.tagName = criteria.tagName;
358
- if (criteria.text !== undefined) out.textContent = criteria.text;
359
- if (criteria.textContains !== undefined) out.textContains = criteria.textContains;
360
- // Prefer explicit `accessibleName` when present; fall back to `ariaLabel`
361
- // for IR documents authored against the section-1 shape.
362
- if (criteria.accessibleName !== undefined) {
363
- out.accessibleName = criteria.accessibleName;
364
- } else if (criteria.ariaLabel !== undefined) {
365
- out.accessibleName = criteria.ariaLabel;
366
- }
367
- if (criteria.id !== undefined) out.id = criteria.id;
368
- if (criteria.attributes !== undefined) out.dataAttributes = criteria.attributes;
369
- return out;
370
- }
371
-
372
- /**
373
- * Build a single legacy assertion from a state + index + criteria.
374
- *
375
- * Always produces one assertion per requiredElement; the caller is responsible
376
- * for iterating `state.requiredElements`. Empty `requiredElements` produces an
377
- * empty `assertions[]` — legitimate legacy specs (e.g. architecture-only
378
- * groups in `graphql-infrastructure`, `activity-timeline`, `opik-integration`,
379
- * `watchers`) ship with empty `assertions[]`, and the inverse projection
380
- * round-trip must preserve that emptiness or `check-spec-pairing` flags an
381
- * assertion-count drift.
382
- */
383
- function buildAssertion(
384
- state: IRState,
385
- index: number,
386
- criteria: IRElementCriteria,
387
- ): LegacyAssertion {
388
- const description =
389
- state.metadata?.description ?? `Required element ${index} for state ${state.name}`;
390
- const targetCriteria: LegacyCriteria = convertCriteria(criteria);
391
- const assertion: LegacyAssertion = {
392
- id: `${state.id}-elem-${index}`,
393
- description,
394
- category: "element-presence",
395
- severity: "critical",
396
- assertionType: "exists",
397
- target: {
398
- type: "search",
399
- criteria: targetCriteria,
400
- label: `Required element for ${state.name}`,
401
- },
402
- source: "ai-generated",
403
- reviewed: false,
404
- enabled: true,
405
- };
406
- if (state.precondition !== undefined) {
407
- assertion.precondition = state.precondition;
408
- }
409
- return assertion;
410
- }
411
-
412
- /**
413
- * Build the legacy `groups[]` entry for one IR state. Emits exactly one
414
- * assertion per `requiredElement`; states with zero requiredElements project
415
- * to a group with an empty `assertions[]` array. Legacy consumers (runner
416
- * spec drift / verify, error monitor curator, AI session) tolerate empty
417
- * groups — multiple shipping legacy specs already use them for
418
- * architecture-only / documentation-only groups.
419
- */
420
- function buildGroup(state: IRState): LegacyGroup {
421
- const elems = state.requiredElements ?? [];
422
- const assertions: LegacyAssertion[] = elems.map((c, i) =>
423
- buildAssertion(state, i, c),
424
- );
425
- return {
426
- id: state.id,
427
- name: state.name,
428
- description: state.description ?? state.metadata?.description ?? "",
429
- category: "element-presence",
430
- assertions,
431
- source: state.provenance?.source ?? "ai-generated",
432
- };
433
- }
434
-
435
- /**
436
- * Convert an IR transition action into the legacy `process[]` step shape.
437
- * Renames IR's `type` to legacy `action` (matches the runtime engine
438
- * convention — same as the IR -> WorkflowConfig adapter does for runtime).
439
- */
440
- function buildProcessStep(action: IRTransitionAction): LegacyProcessStep {
441
- const out: LegacyProcessStep = {
442
- action: action.type,
443
- target: convertCriteria(action.target),
444
- };
445
- if (action.waitAfter !== undefined) out.waitAfter = action.waitAfter;
446
- return out;
447
- }
448
-
449
- /**
450
- * Convert one IR transition into the legacy state-machine transition shape.
451
- * Each fromState gets its own copy in the projected output (legacy specs put
452
- * the transition under each originating state's `transitions[]`).
453
- */
454
- function buildTransition(transition: IRTransition): LegacyTransition {
455
- const exitStates = transition.exitStates ?? [];
456
- return {
457
- id: transition.id,
458
- name: transition.name,
459
- activateStates: transition.activateStates,
460
- deactivateStates: exitStates,
461
- // Modal-style transitions (no exit states) stay visible behind the
462
- // activated state. Otherwise the originating state(s) deactivate, so the
463
- // page no longer shows them.
464
- staysVisible: exitStates.length === 0,
465
- process: transition.actions.map(buildProcessStep),
466
- };
467
- }
468
-
469
- /**
470
- * Build the legacy `stateMachine.states[]` entry for one IR state. Includes
471
- * any transitions that originate from this state (i.e. where state.id is in
472
- * transition.fromStates).
473
- */
474
- function buildStateMachineState(
475
- state: IRState,
476
- transitions: IRTransition[],
477
- doc: IRDocument,
478
- ): LegacyStateMachineState {
479
- const outgoing = transitions
480
- .filter((t) => t.fromStates.includes(state.id))
481
- .map(buildTransition);
482
- const isInitial =
483
- state.isInitial !== undefined ? state.isInitial : state.id === doc.initialState;
484
- return {
485
- id: state.id,
486
- name: state.name,
487
- description: state.description ?? "",
488
- elements: (state.requiredElements ?? []).map(convertCriteria),
489
- isInitial,
490
- transitions: outgoing,
491
- };
492
- }
493
-
494
- // ---------------------------------------------------------------------------
495
- // Public entry point
496
- // ---------------------------------------------------------------------------
497
-
498
- /**
499
- * Project an IR document into the legacy bundled-page spec shape.
500
- *
501
- * Pure / deterministic: same input always produces structurally identical
502
- * output (and byte-identical when JSON-serialized via the standard
503
- * `JSON.stringify` ordering). Arrays preserve input order; object keys are
504
- * sorted lexicographically.
505
- *
506
- * @param doc The IR document to project.
507
- * @param notes Optional human-authored notes (carried in the page's
508
- * `notes.md` companion file). Appended to `description` as a
509
- * separate paragraph (separator: two newlines).
510
- */
511
- export function projectIRToBundledPage(doc: IRDocument, notes?: string): LegacySpec {
512
- const baseDescription = doc.description ?? doc.name;
513
- const description =
514
- notes !== undefined && notes.length > 0
515
- ? `${baseDescription}\n\n${notes}`
516
- : baseDescription;
517
-
518
- const component = doc.metadata?.purpose ?? doc.id;
519
- const metadata: LegacyMetadata = { component };
520
- if (doc.metadata?.tags !== undefined) metadata.tags = doc.metadata.tags;
521
-
522
- const groups: LegacyGroup[] = doc.states.map(buildGroup);
523
- const smStates: LegacyStateMachineState[] = doc.states.map((s) =>
524
- buildStateMachineState(s, doc.transitions, doc),
525
- );
526
-
527
- const spec: LegacySpec = {
528
- version: "1.0.0",
529
- description,
530
- groups,
531
- stateMachine: { states: smStates },
532
- metadata,
533
- };
534
-
535
- return sortKeys(spec);
536
- }
537
-
538
- // ---------------------------------------------------------------------------
539
- // Inverse projection — Legacy spec -> IR document
540
- // ---------------------------------------------------------------------------
541
-
542
- /**
543
- * Options for the inverse projection.
544
- *
545
- * - `docId` — explicit IRDocument.id override. Falls back to
546
- * `legacy.metadata.component` (or `fallbackName` if absent).
547
- * - `fallbackName` — used when `legacy.metadata.component` is missing AND no
548
- * `docId` was supplied.
549
- */
550
- export interface ProjectLegacyToIROptions {
551
- docId?: string;
552
- fallbackName?: string;
553
- }
554
-
555
- /**
556
- * Best-effort field-by-field invert of `LegacyCriteria` back into
557
- * `IRElementCriteria`.
558
- *
559
- * Rename map (the structural inverse of `convertCriteria`):
560
- * textContent -> text
561
- * accessibleName -> accessibleName (passes through; the forward direction
562
- * collapses ariaLabel into accessibleName,
563
- * so we cannot reliably distinguish them
564
- * on the inverse — pick accessibleName).
565
- * dataAttributes -> attributes
566
- * role / tagName / textContains / id pass through unchanged.
567
- *
568
- * Unknown legacy keys are dropped (lossy, but documented). Document-level
569
- * unknown criteria are rare; if they show up we'll surface them in Phase A4.
570
- */
571
- function invertCriteria(criteria: LegacyCriteria | undefined): IRElementCriteria {
572
- if (criteria === undefined || criteria === null) return {};
573
- const out: IRElementCriteria = {};
574
- if (criteria.role !== undefined) out.role = criteria.role;
575
- if (criteria.tagName !== undefined) out.tagName = criteria.tagName;
576
- if (criteria.textContent !== undefined) out.text = criteria.textContent;
577
- if (criteria.textContains !== undefined) out.textContains = criteria.textContains;
578
- if (criteria.accessibleName !== undefined) out.accessibleName = criteria.accessibleName;
579
- if (criteria.id !== undefined) out.id = criteria.id;
580
- if (criteria.dataAttributes !== undefined) out.attributes = criteria.dataAttributes;
581
- return out;
582
- }
583
-
584
- /**
585
- * Pick the first non-empty `precondition` across a group's assertions. Legacy
586
- * groups carry preconditions on individual assertions; IR carries it on the
587
- * state. If multiple distinct preconditions exist we keep the first
588
- * (deterministic, mirrors input ordering) — Phase A4 review will surface
589
- * conflicts for hand-fix.
590
- */
591
- function firstPrecondition(group: LegacyGroup): string | undefined {
592
- for (const a of group.assertions) {
593
- if (a.precondition !== undefined && a.precondition.length > 0) {
594
- return a.precondition;
595
- }
596
- }
597
- return undefined;
598
- }
599
-
600
- /**
601
- * Build an `IRState` from one legacy group + its corresponding state-machine
602
- * state (when present).
603
- *
604
- * IMPORTANT: `requiredElements` is sourced from the GROUP's assertion criteria
605
- * (one criteria per assertion, in input order), NOT from `smState.elements`.
606
- * This preserves the round-trip identity for the assertion-count check —
607
- * legacy specs sometimes carry an SM block whose `elements[]` is a coarse
608
- * page-state fingerprint (1-2 entries) while the group has many assertions.
609
- * Using `smState.elements` would silently collapse those into one
610
- * requiredElement per state, then the forward direction would emit only one
611
- * assertion per group.
612
- *
613
- * `smState` (when present and matched by id) contributes:
614
- * - `name` override (legacy `smState.name` is human-readable; group.name is
615
- * also human-readable but may differ — prefer smState's name since the
616
- * forward direction's `buildStateMachineState` echoes `state.name` to
617
- * `smState.name`).
618
- * - `description` fallback (when group.description is empty).
619
- * - `isInitial` (the only place this lives in legacy specs).
620
- */
621
- function buildIRState(
622
- group: LegacyGroup,
623
- smState: LegacyStateMachineState | undefined,
624
- ): IRState {
625
- // Always one requiredElement per assertion so the forward projection emits
626
- // the same number of assertions on the round-trip.
627
- const requiredElements: IRElementCriteria[] = group.assertions.map((a) =>
628
- invertCriteria(a.target?.criteria),
629
- );
630
-
631
- const description =
632
- group.description !== undefined && group.description.length > 0
633
- ? group.description
634
- : smState?.description;
635
- const precondition = firstPrecondition(group);
636
-
637
- const state: IRState = {
638
- id: group.id,
639
- name: smState?.name ?? group.name,
640
- requiredElements,
641
- };
642
- if (description !== undefined && description.length > 0) {
643
- state.description = description;
644
- }
645
- if (smState?.isInitial === true) {
646
- state.isInitial = true;
647
- } else if (smState?.isInitial === false) {
648
- state.isInitial = false;
649
- }
650
- if (precondition !== undefined) {
651
- state.precondition = precondition;
652
- }
653
- state.provenance = { source: "migrated" };
654
- return state;
655
- }
656
-
657
- /**
658
- * Convert one legacy `process[]` step back into `IRTransitionAction`. Renames
659
- * `action` -> `type`. Legacy specs occasionally omit a target on no-op steps;
660
- * we coerce to an empty criteria object in that case.
661
- */
662
- function invertProcessStep(step: LegacyProcessStep): IRTransitionAction {
663
- const out: IRTransitionAction = {
664
- type: step.action,
665
- target: invertCriteria(step.target),
666
- };
667
- if (step.waitAfter !== undefined) out.waitAfter = step.waitAfter;
668
- return out;
669
- }
670
-
671
- /**
672
- * Collapse duplicate transitions across multiple originating states. The
673
- * forward projection emits the same transition under EACH `fromState`'s
674
- * `transitions[]` entry; the inverse must merge them back into ONE
675
- * `IRTransition` whose `fromStates` lists every originator.
676
- *
677
- * Same-id transitions are assumed to be byte-identical except for the
678
- * (implicit) originating state. If they differ in `process[]` /
679
- * `activateStates` / `deactivateStates`, we keep the first occurrence and
680
- * append the additional fromState. (Genuine divergence here is a defect to
681
- * surface in Phase A4 review.)
682
- */
683
- function buildIRTransitions(stateMachine: LegacyStateMachine | undefined): IRTransition[] {
684
- if (stateMachine === undefined || !Array.isArray(stateMachine.states)) return [];
685
- const byId = new Map<string, IRTransition>();
686
- // Preserve insertion order — first state's transitions come first.
687
- const order: string[] = [];
688
-
689
- for (const smState of stateMachine.states) {
690
- const transitions = Array.isArray(smState.transitions) ? smState.transitions : [];
691
- for (const t of transitions) {
692
- const existing = byId.get(t.id);
693
- if (existing !== undefined) {
694
- if (!existing.fromStates.includes(smState.id)) {
695
- existing.fromStates.push(smState.id);
696
- }
697
- continue;
698
- }
699
- const ir: IRTransition = {
700
- id: t.id,
701
- name: t.name,
702
- fromStates: [smState.id],
703
- activateStates: Array.isArray(t.activateStates) ? [...t.activateStates] : [],
704
- actions: Array.isArray(t.process) ? t.process.map(invertProcessStep) : [],
705
- };
706
- // The forward direction maps `exitStates` -> `deactivateStates`; preserve
707
- // the field whenever it's present in legacy form (even if empty array)
708
- // so modal-style transitions (staysVisible: true, deactivateStates: [])
709
- // round-trip predictably.
710
- if (Array.isArray(t.deactivateStates)) {
711
- ir.exitStates = [...t.deactivateStates];
712
- }
713
- ir.provenance = { source: "migrated" };
714
- byId.set(t.id, ir);
715
- order.push(t.id);
716
- }
717
- }
718
-
719
- return order.map((id) => byId.get(id)!);
720
- }
721
-
722
- /**
723
- * Project a legacy bundled-page spec back into IR.
724
- *
725
- * Pure / deterministic: same input always produces structurally identical
726
- * output. Collapses transition duplicates (forward emits one copy per
727
- * `fromState`), preserves group/assertion ordering, and tags every node with
728
- * `provenance.source = "migrated"` so downstream tooling can distinguish
729
- * migrated content from hand-authored.
730
- *
731
- * The mapping is the structural inverse of `projectIRToBundledPage`:
732
- *
733
- * LegacySpec IRDocument
734
- * ---------- ----------
735
- * metadata.component -> id (or opts.docId override)
736
- * metadata.tags -> metadata.tags
737
- * description -> description (kept verbatim;
738
- * authored notes appended via
739
- * forward `notes` arg are not
740
- * stripped — that round-trip is
741
- * best-effort and Phase A4 will
742
- * split them back out)
743
- *
744
- * groups[] -> states[] (one per group)
745
- * group.id / name / description -> state.id / name / description
746
- * assertion.target.criteria -> state.requiredElements[i] (or,
747
- * preferred, smState.elements when
748
- * the stateMachine block is present)
749
- * first-found assertion.precondition -> state.precondition
750
- *
751
- * stateMachine.states[] -> contributes isInitial + elements
752
- * smState.transitions[] -> transitions[] (deduped by id)
753
- * transition.activateStates -> transition.activateStates
754
- * transition.deactivateStates -> transition.exitStates
755
- * transition.process[] -> transition.actions[]
756
- * step.action -> action.type
757
- * step.target -> action.target (criteria inverted)
758
- * step.waitAfter -> action.waitAfter
759
- *
760
- * Documented losses (acceptable for Phase A2):
761
- * - Legacy assertion ids (`{stateId}-elem-{i}`) are NOT preserved across the
762
- * round-trip — the forward direction regenerates them from index, so any
763
- * hand-edited ids are lost. See Phase A4.
764
- * - Legacy `assertionType` / `severity` / `category` / `source` per
765
- * assertion are reduced to defaults on round-trip.
766
- * - Legacy `expected` / `assertionType: "count"` and `notExists` semantics
767
- * are not modeled in IR — the assertion's criteria still survive (as a
768
- * state requiredElement) but the assertion-flavor metadata is dropped.
769
- * - Legacy `testing` block + extra metadata keys outside `component` /
770
- * `tags` are ignored. Authors keep them via the legacy spec file directly
771
- * during Phase A2 — they will move into IR companion files in Phase A4.
772
- * - The forward `notes` argument's appended paragraph is currently kept
773
- * in `description`; Phase A4 will split it back out into a dedicated
774
- * companion file.
775
- *
776
- * @param legacy The legacy spec to invert.
777
- * @param opts Optional doc-level overrides.
778
- */
779
- export function projectLegacyToIR(
780
- legacy: LegacySpec,
781
- opts?: ProjectLegacyToIROptions,
782
- ): IRDocument {
783
- const component =
784
- opts?.docId ??
785
- legacy.metadata?.component ??
786
- opts?.fallbackName ??
787
- "legacy-spec";
788
- const name = legacy.metadata?.component ?? opts?.fallbackName ?? component;
789
-
790
- // Index state-machine states by id for quick lookup when building IR states
791
- // out of groups. Older specs may not have a stateMachine block.
792
- const smByGroupId = new Map<string, LegacyStateMachineState>();
793
- if (legacy.stateMachine !== undefined && Array.isArray(legacy.stateMachine.states)) {
794
- for (const s of legacy.stateMachine.states) {
795
- smByGroupId.set(s.id, s);
796
- }
797
- }
798
-
799
- const groups = Array.isArray(legacy.groups) ? legacy.groups : [];
800
- const states: IRState[] = groups.map((group) =>
801
- buildIRState(group, smByGroupId.get(group.id)),
802
- );
803
-
804
- // NOTE: Some legacy specs declare `stateMachine.states[]` whose ids do NOT
805
- // correspond to any `groups[]` entry (e.g., page-level states like
806
- // `wrappers-installed-tab` vs assertion-category groups like
807
- // `page-structure`). The IR's group-state mapping is 1:1 with groups by
808
- // construction (the forward projection emits one group per IR state), so
809
- // these orphan SM states cannot survive the round-trip cleanly. They are
810
- // dropped here as a documented loss — Phase A4 will revisit by either
811
- // (a) introducing a separate IR concept for "page state" vs
812
- // "assertion group", or (b) hand-merging the legacy specs so groups and
813
- // SM states share ids.
814
- // Transitions originating from orphan SM states are likewise omitted from
815
- // IR (their fromStates would dangle). We filter them below.
816
- const groupIds = new Set(groups.map((g) => g.id));
817
-
818
- // Build transitions only from SM states whose ids appear in groups[]. Any
819
- // transition with a fromState that isn't a known group is dropped — the
820
- // forward direction would have nowhere to emit it.
821
- let filteredStateMachine: LegacyStateMachine | undefined = legacy.stateMachine;
822
- if (
823
- legacy.stateMachine !== undefined &&
824
- Array.isArray(legacy.stateMachine.states) &&
825
- legacy.stateMachine.states.some((s) => !groupIds.has(s.id))
826
- ) {
827
- filteredStateMachine = {
828
- states: legacy.stateMachine.states.filter((s) => groupIds.has(s.id)),
829
- };
830
- }
831
- const transitions = buildIRTransitions(filteredStateMachine);
832
-
833
- // Initial state: first state with isInitial:true wins (mirrors how the
834
- // forward direction picks the document's `initialState`). Only consider
835
- // SM states that correspond to a group (so initialState references a real
836
- // IR state).
837
- let initialState: string | undefined;
838
- if (legacy.stateMachine !== undefined && Array.isArray(legacy.stateMachine.states)) {
839
- const initial = legacy.stateMachine.states.find(
840
- (s) => s.isInitial === true && groupIds.has(s.id),
841
- );
842
- if (initial !== undefined) initialState = initial.id;
843
- }
844
-
845
- const doc: IRDocument = {
846
- version: "1.0",
847
- id: component,
848
- name,
849
- states,
850
- transitions,
851
- provenance: { source: "migrated" },
852
- };
853
- if (legacy.description !== undefined && legacy.description.length > 0) {
854
- doc.description = legacy.description;
855
- }
856
- if (legacy.metadata !== undefined && legacy.metadata.tags !== undefined) {
857
- doc.metadata = { tags: legacy.metadata.tags };
858
- }
859
- if (initialState !== undefined) {
860
- doc.initialState = initialState;
861
- }
862
- return doc;
863
- }
1
+ /**
2
+ * IR -> Bundled-Page Legacy Spec projection.
3
+ *
4
+ * Single point of translation between authoring-time IR (`IRDocument`) and the
5
+ * legacy `*.spec.uibridge.json` shape that existing tooling still consumes
6
+ * (`/update-spec`, runner spec drift / verify, error monitor curator,
7
+ * spec experimentation, AI session). See ADR-001 (decision #6 / #7) for the
8
+ * boundary rationale; section 2 of the UI Bridge redesign for the projection's
9
+ * role in keeping legacy consumers working through the migration.
10
+ *
11
+ * The projection is a PURE FUNCTION — same input always produces byte-identical
12
+ * output. Keys within objects are sorted lexicographically, arrays preserve
13
+ * input order, and no timestamps / random IDs are introduced. Downstream tests
14
+ * may hash the JSON output for byte-stable assertions.
15
+ *
16
+ * ---------------------------------------------------------------------------
17
+ * Mapping rules (worked example below)
18
+ * ---------------------------------------------------------------------------
19
+ *
20
+ * IRDocument LegacySpec
21
+ * ---------- ----------
22
+ * doc.id -> metadata.component
23
+ * doc.metadata?.purpose ?? doc.id -> metadata.component (purpose wins if present)
24
+ * doc.description ?? doc.name -> description
25
+ * doc.metadata?.tags -> metadata.tags
26
+ *
27
+ * For each IRState:
28
+ * state -> one entry in groups[]
29
+ * state.id -> group.id
30
+ * state.name -> group.name
31
+ * state.description ??
32
+ * state.metadata?.description -> group.description
33
+ * "element-presence" -> group.category (default)
34
+ * state.provenance?.source ??
35
+ * "ai-generated" -> group.source
36
+ *
37
+ * For each requiredElement of state:
38
+ * (state, criteria, index) -> one assertion in group.assertions[]
39
+ * "${state.id}-elem-${index}" -> assertion.id
40
+ * state.metadata?.description ?? -> assertion.description
41
+ * "Required element ${index} ..."
42
+ * "element-presence" -> assertion.category
43
+ * "critical" -> assertion.severity
44
+ * "exists" -> assertion.assertionType
45
+ * {
46
+ * type: "search",
47
+ * criteria: convert(criteria), -> assertion.target
48
+ * label: "Required element ..."
49
+ * }
50
+ * "ai-generated" -> assertion.source
51
+ * false -> assertion.reviewed
52
+ * true -> assertion.enabled
53
+ *
54
+ * If state has zero requiredElements, the projected `group.assertions[]`
55
+ * is the empty array — legacy specs ship architecture-only groups with
56
+ * empty assertions, and the inverse projection's identity round-trip
57
+ * depends on preserving that emptiness. (Earlier versions emitted a
58
+ * placeholder assertion here, which silently grew the assertion count
59
+ * during round-trip and tripped `check-spec-pairing`.)
60
+ *
61
+ * Each IRState -> one entry in stateMachine.states[]
62
+ * state.id -> sm-state.id
63
+ * state.name -> sm-state.name
64
+ * state.description ?? "" -> sm-state.description
65
+ * state.requiredElements (converted) -> sm-state.elements
66
+ * state.isInitial ??
67
+ * (state.id === doc.initialState) -> sm-state.isInitial
68
+ * transitions originating from state -> sm-state.transitions
69
+ *
70
+ * For each IRTransition:
71
+ * For each fromState in transition.fromStates:
72
+ * transition -> one entry in
73
+ * stateMachine.states[fromState].transitions[]
74
+ * transition.id -> sm-transition.id
75
+ * transition.name -> sm-transition.name
76
+ * transition.activateStates -> sm-transition.activateStates
77
+ * transition.exitStates ?? [] -> sm-transition.deactivateStates
78
+ * (exitStates.length === 0) -> sm-transition.staysVisible
79
+ *
80
+ * For each action in transition.actions:
81
+ * action.type -> process[].action (rename type -> action)
82
+ * action.target -> process[].target (criteria converted)
83
+ * action.waitAfter -> process[].waitAfter (passed through)
84
+ *
85
+ * Element-criteria conversion (IRElementCriteria -> legacy criteria):
86
+ * text -> textContent
87
+ * ariaLabel -> accessibleName
88
+ * attributes -> dataAttributes
89
+ * (others — role, textContains, id — pass through unchanged)
90
+ *
91
+ * ---------------------------------------------------------------------------
92
+ * Worked example
93
+ * ---------------------------------------------------------------------------
94
+ *
95
+ * const ir: IRDocument = {
96
+ * version: "1.0",
97
+ * id: "active",
98
+ * name: "Active Dashboard",
99
+ * description: "Real-time monitoring hub",
100
+ * metadata: { tags: ["monitoring", "tier-1"] },
101
+ * states: [
102
+ * {
103
+ * id: "running",
104
+ * name: "Running",
105
+ * description: "Workflow executing",
106
+ * requiredElements: [{ role: "button", text: "Stop" }],
107
+ * isInitial: false,
108
+ * },
109
+ * ],
110
+ * transitions: [
111
+ * {
112
+ * id: "running-to-idle",
113
+ * name: "Stop",
114
+ * fromStates: ["running"],
115
+ * activateStates: ["idle"],
116
+ * exitStates: ["running"],
117
+ * actions: [
118
+ * {
119
+ * type: "click",
120
+ * target: { role: "button", text: "Stop" },
121
+ * waitAfter: { type: "idle", timeout: 3000 },
122
+ * },
123
+ * ],
124
+ * },
125
+ * ],
126
+ * initialState: "idle",
127
+ * };
128
+ *
129
+ * projectIRToBundledPage(ir, "Notes from author")
130
+ * // -> {
131
+ * // version: "1.0.0",
132
+ * // description: "Real-time monitoring hub\n\nNotes from author",
133
+ * // groups: [{
134
+ * // id: "running",
135
+ * // name: "Running",
136
+ * // description: "Workflow executing",
137
+ * // category: "element-presence",
138
+ * // assertions: [{
139
+ * // id: "running-elem-0",
140
+ * // description: "Required element 0 for state Running",
141
+ * // category: "element-presence",
142
+ * // severity: "critical",
143
+ * // assertionType: "exists",
144
+ * // target: {
145
+ * // type: "search",
146
+ * // criteria: { role: "button", textContent: "Stop" },
147
+ * // label: "Required element for Running",
148
+ * // },
149
+ * // source: "ai-generated",
150
+ * // reviewed: false,
151
+ * // enabled: true,
152
+ * // }],
153
+ * // source: "ai-generated",
154
+ * // }],
155
+ * // stateMachine: {
156
+ * // states: [{
157
+ * // id: "running",
158
+ * // name: "Running",
159
+ * // description: "Workflow executing",
160
+ * // elements: [{ role: "button", textContent: "Stop" }],
161
+ * // isInitial: false,
162
+ * // transitions: [{
163
+ * // id: "running-to-idle",
164
+ * // name: "Stop",
165
+ * // activateStates: ["idle"],
166
+ * // deactivateStates: ["running"],
167
+ * // staysVisible: false,
168
+ * // process: [{
169
+ * // action: "click",
170
+ * // target: { role: "button", textContent: "Stop" },
171
+ * // waitAfter: { type: "idle", timeout: 3000 },
172
+ * // }],
173
+ * // }],
174
+ * // }],
175
+ * // },
176
+ * // metadata: { component: "active", tags: ["monitoring", "tier-1"] },
177
+ * // }
178
+ */
179
+
180
+ import type { IRElementCriteria } from "./element-criteria";
181
+ import type { IRDocument } from "./document";
182
+ import type { IRAssertion, IRGroup } from "./group";
183
+ import type { IRState } from "./state";
184
+ import type { IRTransition, IRTransitionAction, IRWaitSpec } from "./transition";
185
+
186
+ /**
187
+ * Projection version. Bump on shape changes that consumers must adapt to.
188
+ * Independent from `IRVersion` because the legacy spec shape and the IR shape
189
+ * evolve on different cadences.
190
+ */
191
+ export const projectionVersion = "1.0";
192
+
193
+ // ---------------------------------------------------------------------------
194
+ // Legacy spec shapes — derived from active.spec.uibridge.json (the reference
195
+ // the projection's output must structurally match).
196
+ // ---------------------------------------------------------------------------
197
+
198
+ /**
199
+ * Legacy element-criteria shape (after conversion from `IRElementCriteria`).
200
+ *
201
+ * The differences from `IRElementCriteria`:
202
+ * - `text` -> `textContent`
203
+ * - `ariaLabel` -> `accessibleName` (when IR has no explicit `accessibleName`)
204
+ * - `attributes` -> `dataAttributes`
205
+ *
206
+ * Other fields (`role`, `tagName`, `textContains`, `accessibleName`, `id`)
207
+ * pass through unchanged. Index signature stays open for legacy fields the
208
+ * IR doesn't yet model.
209
+ */
210
+ export interface LegacyCriteria {
211
+ role?: string;
212
+ tagName?: string;
213
+ textContent?: string;
214
+ textContains?: string;
215
+ accessibleName?: string;
216
+ id?: string;
217
+ dataAttributes?: Record<string, string>;
218
+ [key: string]: unknown;
219
+ }
220
+
221
+ /**
222
+ * Legacy assertion target. Always `type: "search"` for the projection — the
223
+ * legacy schema also supports point/region targets, but the IR doesn't
224
+ * express those.
225
+ */
226
+ export interface LegacyAssertionTarget {
227
+ type: "search";
228
+ criteria: LegacyCriteria;
229
+ label: string;
230
+ }
231
+
232
+ /**
233
+ * Legacy assertion. Mirrors the shape used by `groups[].assertions[]` in
234
+ * `*.spec.uibridge.json`.
235
+ */
236
+ export interface LegacyAssertion {
237
+ id: string;
238
+ description: string;
239
+ category: string;
240
+ severity: "critical" | "warning" | "info";
241
+ assertionType: string;
242
+ target: LegacyAssertionTarget;
243
+ source: string;
244
+ reviewed: boolean;
245
+ enabled: boolean;
246
+ precondition?: string;
247
+ }
248
+
249
+ /**
250
+ * Legacy group. One per IR state.
251
+ */
252
+ export interface LegacyGroup {
253
+ id: string;
254
+ name: string;
255
+ description: string;
256
+ category: string;
257
+ assertions: LegacyAssertion[];
258
+ source: string;
259
+ }
260
+
261
+ /**
262
+ * Process step within a transition. Note `action` (not `type`) — matches the
263
+ * runtime engine's `TransitionAction` shape.
264
+ */
265
+ export interface LegacyProcessStep {
266
+ action: string;
267
+ target: LegacyCriteria;
268
+ waitAfter?: IRWaitSpec;
269
+ }
270
+
271
+ /**
272
+ * Legacy state-machine transition.
273
+ */
274
+ export interface LegacyTransition {
275
+ id: string;
276
+ name: string;
277
+ activateStates: string[];
278
+ deactivateStates: string[];
279
+ staysVisible: boolean;
280
+ process: LegacyProcessStep[];
281
+ }
282
+
283
+ /**
284
+ * Legacy state-machine state.
285
+ */
286
+ export interface LegacyStateMachineState {
287
+ id: string;
288
+ name: string;
289
+ description: string;
290
+ elements: LegacyCriteria[];
291
+ isInitial: boolean;
292
+ transitions: LegacyTransition[];
293
+ }
294
+
295
+ /**
296
+ * Legacy state-machine block.
297
+ */
298
+ export interface LegacyStateMachine {
299
+ states: LegacyStateMachineState[];
300
+ }
301
+
302
+ /**
303
+ * Legacy spec metadata block. The projection writes a deterministic subset;
304
+ * downstream tooling may layer additional keys on hand-authored specs.
305
+ */
306
+ export interface LegacyMetadata {
307
+ component: string;
308
+ tags?: string[];
309
+ [key: string]: unknown;
310
+ }
311
+
312
+ /**
313
+ * Top-level legacy spec shape.
314
+ */
315
+ export interface LegacySpec {
316
+ version: string;
317
+ description: string;
318
+ groups: LegacyGroup[];
319
+ stateMachine: LegacyStateMachine;
320
+ metadata: LegacyMetadata;
321
+ }
322
+
323
+ // ---------------------------------------------------------------------------
324
+ // Internal helpers
325
+ // ---------------------------------------------------------------------------
326
+
327
+ /**
328
+ * Recursively sort object keys lexicographically. Arrays preserve input order
329
+ * (ordering is meaningful for groups / assertions / transitions).
330
+ *
331
+ * Used as the final stage of every projected output to guarantee byte-stable
332
+ * JSON serialization downstream.
333
+ */
334
+ function sortKeys<T>(value: T): T {
335
+ if (Array.isArray(value)) {
336
+ return value.map(sortKeys) as unknown as T;
337
+ }
338
+ if (value !== null && typeof value === "object") {
339
+ const sorted: Record<string, unknown> = {};
340
+ const keys = Object.keys(value as Record<string, unknown>).sort();
341
+ for (const key of keys) {
342
+ sorted[key] = sortKeys((value as Record<string, unknown>)[key]);
343
+ }
344
+ return sorted as unknown as T;
345
+ }
346
+ return value;
347
+ }
348
+
349
+ /**
350
+ * Convert IR element criteria to legacy criteria. Renames `text` ->
351
+ * `textContent`, `attributes` -> `dataAttributes`. `accessibleName` (or
352
+ * `ariaLabel` as a fallback) maps to legacy `accessibleName`. `tagName`,
353
+ * `role`, `textContains`, `id` pass through unchanged.
354
+ */
355
+ function convertCriteria(criteria: IRElementCriteria): LegacyCriteria {
356
+ const out: LegacyCriteria = {};
357
+ if (criteria.role !== undefined) out.role = criteria.role;
358
+ if (criteria.tagName !== undefined) out.tagName = criteria.tagName;
359
+ if (criteria.text !== undefined) out.textContent = criteria.text;
360
+ if (criteria.textContains !== undefined) out.textContains = criteria.textContains;
361
+ // Prefer explicit `accessibleName` when present; fall back to `ariaLabel`
362
+ // for IR documents authored against the section-1 shape.
363
+ if (criteria.accessibleName !== undefined) {
364
+ out.accessibleName = criteria.accessibleName;
365
+ } else if (criteria.ariaLabel !== undefined) {
366
+ out.accessibleName = criteria.ariaLabel;
367
+ }
368
+ if (criteria.id !== undefined) out.id = criteria.id;
369
+ if (criteria.attributes !== undefined) out.dataAttributes = criteria.attributes;
370
+ return out;
371
+ }
372
+
373
+ /**
374
+ * Build a single legacy assertion from a state + index + criteria.
375
+ *
376
+ * Always produces one assertion per requiredElement; the caller is responsible
377
+ * for iterating `state.requiredElements`. Empty `requiredElements` produces an
378
+ * empty `assertions[]` — legitimate legacy specs (e.g. architecture-only
379
+ * groups in `graphql-infrastructure`, `activity-timeline`, `opik-integration`,
380
+ * `watchers`) ship with empty `assertions[]`, and the inverse projection
381
+ * round-trip must preserve that emptiness or `check-spec-pairing` flags an
382
+ * assertion-count drift.
383
+ */
384
+ function buildAssertion(
385
+ state: IRState,
386
+ index: number,
387
+ criteria: IRElementCriteria,
388
+ ): LegacyAssertion {
389
+ const description =
390
+ state.metadata?.description ?? `Required element ${index} for state ${state.name}`;
391
+ const targetCriteria: LegacyCriteria = convertCriteria(criteria);
392
+ const assertion: LegacyAssertion = {
393
+ id: `${state.id}-elem-${index}`,
394
+ description,
395
+ category: "element-presence",
396
+ severity: "critical",
397
+ assertionType: "exists",
398
+ target: {
399
+ type: "search",
400
+ criteria: targetCriteria,
401
+ label: `Required element for ${state.name}`,
402
+ },
403
+ source: "ai-generated",
404
+ reviewed: false,
405
+ enabled: true,
406
+ };
407
+ if (state.precondition !== undefined) {
408
+ assertion.precondition = state.precondition;
409
+ }
410
+ return assertion;
411
+ }
412
+
413
+ /**
414
+ * Build the legacy `groups[]` entry for one IR state. Emits exactly one
415
+ * assertion per `requiredElement`; states with zero requiredElements project
416
+ * to a group with an empty `assertions[]` array. Legacy consumers (runner
417
+ * spec drift / verify, error monitor curator, AI session) tolerate empty
418
+ * groups — multiple shipping legacy specs already use them for
419
+ * architecture-only / documentation-only groups.
420
+ */
421
+ function buildGroup(state: IRState): LegacyGroup {
422
+ const elems = state.requiredElements ?? [];
423
+ const assertions: LegacyAssertion[] = elems.map((c, i) =>
424
+ buildAssertion(state, i, c),
425
+ );
426
+ return {
427
+ id: state.id,
428
+ name: state.name,
429
+ description: state.description ?? state.metadata?.description ?? "",
430
+ category: "element-presence",
431
+ assertions,
432
+ source: state.provenance?.source ?? "ai-generated",
433
+ };
434
+ }
435
+
436
+ /**
437
+ * Convert an `IRAssertion` (IR-side, free-form criteria) into a `LegacyAssertion`.
438
+ * The shapes are near-identical; the only meaningful difference is that the
439
+ * IR-side `target.criteria` is `Record<string, unknown>` while the legacy
440
+ * shape is `LegacyCriteria` (a typed but extensible record). We pass the
441
+ * criteria object through verbatim — the open `[key: string]: unknown` index
442
+ * signature on `LegacyCriteria` accepts any record shape.
443
+ */
444
+ function synthesizedAssertionToLegacy(assertion: IRAssertion): LegacyAssertion {
445
+ const target: LegacyAssertionTarget = {
446
+ type: "search",
447
+ criteria: assertion.target.criteria as LegacyCriteria,
448
+ label: assertion.target.label,
449
+ };
450
+ const out: LegacyAssertion = {
451
+ id: assertion.id,
452
+ description: assertion.description,
453
+ category: assertion.category,
454
+ severity: assertion.severity as LegacyAssertion["severity"],
455
+ assertionType: assertion.assertionType,
456
+ target,
457
+ source: assertion.source,
458
+ reviewed: assertion.reviewed,
459
+ enabled: assertion.enabled,
460
+ };
461
+ if (assertion.precondition !== undefined) {
462
+ out.precondition = assertion.precondition;
463
+ }
464
+ return out;
465
+ }
466
+
467
+ /**
468
+ * Convert a synthesized `IRGroup` into a `LegacyGroup`. Mirrors the legacy
469
+ * shape one-to-one (modulo the free-form criteria pass-through described in
470
+ * `synthesizedAssertionToLegacy`). The synthesized group always carries a
471
+ * `source` (synthesis emits `"ai-generated"`); fall back to `"ai-generated"`
472
+ * for hand-written IR documents that omit it.
473
+ */
474
+ function synthesizedGroupToLegacy(group: IRGroup): LegacyGroup {
475
+ return {
476
+ id: group.id,
477
+ name: group.name,
478
+ description: group.description,
479
+ category: group.category,
480
+ assertions: group.assertions.map(synthesizedAssertionToLegacy),
481
+ source: group.source ?? "ai-generated",
482
+ };
483
+ }
484
+
485
+ /**
486
+ * Convert an IR transition action into the legacy `process[]` step shape.
487
+ * Renames IR's `type` to legacy `action` (matches the runtime engine
488
+ * convention — same as the IR -> WorkflowConfig adapter does for runtime).
489
+ */
490
+ function buildProcessStep(action: IRTransitionAction): LegacyProcessStep {
491
+ const out: LegacyProcessStep = {
492
+ action: action.type,
493
+ target: convertCriteria(action.target),
494
+ };
495
+ if (action.waitAfter !== undefined) out.waitAfter = action.waitAfter;
496
+ return out;
497
+ }
498
+
499
+ /**
500
+ * Convert one IR transition into the legacy state-machine transition shape.
501
+ * Each fromState gets its own copy in the projected output (legacy specs put
502
+ * the transition under each originating state's `transitions[]`).
503
+ */
504
+ function buildTransition(transition: IRTransition): LegacyTransition {
505
+ const exitStates = transition.exitStates ?? [];
506
+ return {
507
+ id: transition.id,
508
+ name: transition.name,
509
+ activateStates: transition.activateStates,
510
+ deactivateStates: exitStates,
511
+ // Modal-style transitions (no exit states) stay visible behind the
512
+ // activated state. Otherwise the originating state(s) deactivate, so the
513
+ // page no longer shows them.
514
+ staysVisible: exitStates.length === 0,
515
+ process: transition.actions.map(buildProcessStep),
516
+ };
517
+ }
518
+
519
+ /**
520
+ * Build the legacy `stateMachine.states[]` entry for one IR state. Includes
521
+ * any transitions that originate from this state (i.e. where state.id is in
522
+ * transition.fromStates).
523
+ */
524
+ function buildStateMachineState(
525
+ state: IRState,
526
+ transitions: IRTransition[],
527
+ doc: IRDocument,
528
+ ): LegacyStateMachineState {
529
+ const outgoing = transitions
530
+ .filter((t) => t.fromStates.includes(state.id))
531
+ .map(buildTransition);
532
+ const isInitial =
533
+ state.isInitial !== undefined ? state.isInitial : state.id === doc.initialState;
534
+ return {
535
+ id: state.id,
536
+ name: state.name,
537
+ description: state.description ?? "",
538
+ elements: (state.requiredElements ?? []).map(convertCriteria),
539
+ isInitial,
540
+ transitions: outgoing,
541
+ };
542
+ }
543
+
544
+ // ---------------------------------------------------------------------------
545
+ // Public entry point
546
+ // ---------------------------------------------------------------------------
547
+
548
+ /**
549
+ * Project an IR document into the legacy bundled-page spec shape.
550
+ *
551
+ * Pure / deterministic: same input always produces structurally identical
552
+ * output (and byte-identical when JSON-serialized via the standard
553
+ * `JSON.stringify` ordering). Arrays preserve input order; object keys are
554
+ * sorted lexicographically.
555
+ *
556
+ * @param doc The IR document to project.
557
+ * @param notes Optional human-authored notes (carried in the page's
558
+ * `notes.md` companion file). Appended to `description` as a
559
+ * separate paragraph (separator: two newlines).
560
+ */
561
+ export function projectIRToBundledPage(doc: IRDocument, notes?: string): LegacySpec {
562
+ const baseDescription = doc.description ?? doc.name;
563
+ const description =
564
+ notes !== undefined && notes.length > 0
565
+ ? `${baseDescription}\n\n${notes}`
566
+ : baseDescription;
567
+
568
+ const component = doc.metadata?.purpose ?? doc.id;
569
+ const metadata: LegacyMetadata = { component };
570
+ if (doc.metadata?.tags !== undefined) metadata.tags = doc.metadata.tags;
571
+
572
+ const groups: LegacyGroup[] = doc.states.map(buildGroup);
573
+ if (doc.synthesizedGroups !== undefined) {
574
+ // Append synthesis-produced groups AFTER state-derived ones, in declared
575
+ // order. Byte-stable output (see file header) requires this exact ordering
576
+ // — anything else breaks `check-spec-pairing` style hashes.
577
+ for (const g of doc.synthesizedGroups) {
578
+ groups.push(synthesizedGroupToLegacy(g));
579
+ }
580
+ }
581
+ const smStates: LegacyStateMachineState[] = doc.states.map((s) =>
582
+ buildStateMachineState(s, doc.transitions, doc),
583
+ );
584
+
585
+ const spec: LegacySpec = {
586
+ version: "1.0.0",
587
+ description,
588
+ groups,
589
+ stateMachine: { states: smStates },
590
+ metadata,
591
+ };
592
+
593
+ return sortKeys(spec);
594
+ }
595
+
596
+ // ---------------------------------------------------------------------------
597
+ // Inverse projection — Legacy spec -> IR document
598
+ // ---------------------------------------------------------------------------
599
+
600
+ /**
601
+ * Options for the inverse projection.
602
+ *
603
+ * - `docId` — explicit IRDocument.id override. Falls back to
604
+ * `legacy.metadata.component` (or `fallbackName` if absent).
605
+ * - `fallbackName` — used when `legacy.metadata.component` is missing AND no
606
+ * `docId` was supplied.
607
+ */
608
+ export interface ProjectLegacyToIROptions {
609
+ docId?: string;
610
+ fallbackName?: string;
611
+ }
612
+
613
+ /**
614
+ * Best-effort field-by-field invert of `LegacyCriteria` back into
615
+ * `IRElementCriteria`.
616
+ *
617
+ * Rename map (the structural inverse of `convertCriteria`):
618
+ * textContent -> text
619
+ * accessibleName -> accessibleName (passes through; the forward direction
620
+ * collapses ariaLabel into accessibleName,
621
+ * so we cannot reliably distinguish them
622
+ * on the inverse — pick accessibleName).
623
+ * dataAttributes -> attributes
624
+ * role / tagName / textContains / id pass through unchanged.
625
+ *
626
+ * Unknown legacy keys are dropped (lossy, but documented). Document-level
627
+ * unknown criteria are rare; if they show up we'll surface them in Phase A4.
628
+ */
629
+ function invertCriteria(criteria: LegacyCriteria | undefined): IRElementCriteria {
630
+ if (criteria === undefined || criteria === null) return {};
631
+ const out: IRElementCriteria = {};
632
+ if (criteria.role !== undefined) out.role = criteria.role;
633
+ if (criteria.tagName !== undefined) out.tagName = criteria.tagName;
634
+ if (criteria.textContent !== undefined) out.text = criteria.textContent;
635
+ if (criteria.textContains !== undefined) out.textContains = criteria.textContains;
636
+ if (criteria.accessibleName !== undefined) out.accessibleName = criteria.accessibleName;
637
+ if (criteria.id !== undefined) out.id = criteria.id;
638
+ if (criteria.dataAttributes !== undefined) out.attributes = criteria.dataAttributes;
639
+ return out;
640
+ }
641
+
642
+ /**
643
+ * Pick the first non-empty `precondition` across a group's assertions. Legacy
644
+ * groups carry preconditions on individual assertions; IR carries it on the
645
+ * state. If multiple distinct preconditions exist we keep the first
646
+ * (deterministic, mirrors input ordering) — Phase A4 review will surface
647
+ * conflicts for hand-fix.
648
+ */
649
+ function firstPrecondition(group: LegacyGroup): string | undefined {
650
+ for (const a of group.assertions) {
651
+ if (a.precondition !== undefined && a.precondition.length > 0) {
652
+ return a.precondition;
653
+ }
654
+ }
655
+ return undefined;
656
+ }
657
+
658
+ /**
659
+ * Build an `IRState` from one legacy group + its corresponding state-machine
660
+ * state (when present).
661
+ *
662
+ * IMPORTANT: `requiredElements` is sourced from the GROUP's assertion criteria
663
+ * (one criteria per assertion, in input order), NOT from `smState.elements`.
664
+ * This preserves the round-trip identity for the assertion-count check —
665
+ * legacy specs sometimes carry an SM block whose `elements[]` is a coarse
666
+ * page-state fingerprint (1-2 entries) while the group has many assertions.
667
+ * Using `smState.elements` would silently collapse those into one
668
+ * requiredElement per state, then the forward direction would emit only one
669
+ * assertion per group.
670
+ *
671
+ * `smState` (when present and matched by id) contributes:
672
+ * - `name` override (legacy `smState.name` is human-readable; group.name is
673
+ * also human-readable but may differ — prefer smState's name since the
674
+ * forward direction's `buildStateMachineState` echoes `state.name` to
675
+ * `smState.name`).
676
+ * - `description` fallback (when group.description is empty).
677
+ * - `isInitial` (the only place this lives in legacy specs).
678
+ */
679
+ function buildIRState(
680
+ group: LegacyGroup,
681
+ smState: LegacyStateMachineState | undefined,
682
+ ): IRState {
683
+ // Always one requiredElement per assertion so the forward projection emits
684
+ // the same number of assertions on the round-trip.
685
+ const requiredElements: IRElementCriteria[] = group.assertions.map((a) =>
686
+ invertCriteria(a.target?.criteria),
687
+ );
688
+
689
+ const description =
690
+ group.description !== undefined && group.description.length > 0
691
+ ? group.description
692
+ : smState?.description;
693
+ const precondition = firstPrecondition(group);
694
+
695
+ const state: IRState = {
696
+ id: group.id,
697
+ name: smState?.name ?? group.name,
698
+ requiredElements,
699
+ };
700
+ if (description !== undefined && description.length > 0) {
701
+ state.description = description;
702
+ }
703
+ if (smState?.isInitial === true) {
704
+ state.isInitial = true;
705
+ } else if (smState?.isInitial === false) {
706
+ state.isInitial = false;
707
+ }
708
+ if (precondition !== undefined) {
709
+ state.precondition = precondition;
710
+ }
711
+ state.provenance = { source: "migrated" };
712
+ return state;
713
+ }
714
+
715
+ /**
716
+ * Convert one legacy `process[]` step back into `IRTransitionAction`. Renames
717
+ * `action` -> `type`. Legacy specs occasionally omit a target on no-op steps;
718
+ * we coerce to an empty criteria object in that case.
719
+ */
720
+ function invertProcessStep(step: LegacyProcessStep): IRTransitionAction {
721
+ const out: IRTransitionAction = {
722
+ type: step.action,
723
+ target: invertCriteria(step.target),
724
+ };
725
+ if (step.waitAfter !== undefined) out.waitAfter = step.waitAfter;
726
+ return out;
727
+ }
728
+
729
+ /**
730
+ * Collapse duplicate transitions across multiple originating states. The
731
+ * forward projection emits the same transition under EACH `fromState`'s
732
+ * `transitions[]` entry; the inverse must merge them back into ONE
733
+ * `IRTransition` whose `fromStates` lists every originator.
734
+ *
735
+ * Same-id transitions are assumed to be byte-identical except for the
736
+ * (implicit) originating state. If they differ in `process[]` /
737
+ * `activateStates` / `deactivateStates`, we keep the first occurrence and
738
+ * append the additional fromState. (Genuine divergence here is a defect to
739
+ * surface in Phase A4 review.)
740
+ */
741
+ function buildIRTransitions(stateMachine: LegacyStateMachine | undefined): IRTransition[] {
742
+ if (stateMachine === undefined || !Array.isArray(stateMachine.states)) return [];
743
+ const byId = new Map<string, IRTransition>();
744
+ // Preserve insertion order — first state's transitions come first.
745
+ const order: string[] = [];
746
+
747
+ for (const smState of stateMachine.states) {
748
+ const transitions = Array.isArray(smState.transitions) ? smState.transitions : [];
749
+ for (const t of transitions) {
750
+ const existing = byId.get(t.id);
751
+ if (existing !== undefined) {
752
+ if (!existing.fromStates.includes(smState.id)) {
753
+ existing.fromStates.push(smState.id);
754
+ }
755
+ continue;
756
+ }
757
+ const ir: IRTransition = {
758
+ id: t.id,
759
+ name: t.name,
760
+ fromStates: [smState.id],
761
+ activateStates: Array.isArray(t.activateStates) ? [...t.activateStates] : [],
762
+ actions: Array.isArray(t.process) ? t.process.map(invertProcessStep) : [],
763
+ };
764
+ // The forward direction maps `exitStates` -> `deactivateStates`; preserve
765
+ // the field whenever it's present in legacy form (even if empty array)
766
+ // so modal-style transitions (staysVisible: true, deactivateStates: [])
767
+ // round-trip predictably.
768
+ if (Array.isArray(t.deactivateStates)) {
769
+ ir.exitStates = [...t.deactivateStates];
770
+ }
771
+ ir.provenance = { source: "migrated" };
772
+ byId.set(t.id, ir);
773
+ order.push(t.id);
774
+ }
775
+ }
776
+
777
+ return order.map((id) => byId.get(id)!);
778
+ }
779
+
780
+ /**
781
+ * Project a legacy bundled-page spec back into IR.
782
+ *
783
+ * Pure / deterministic: same input always produces structurally identical
784
+ * output. Collapses transition duplicates (forward emits one copy per
785
+ * `fromState`), preserves group/assertion ordering, and tags every node with
786
+ * `provenance.source = "migrated"` so downstream tooling can distinguish
787
+ * migrated content from hand-authored.
788
+ *
789
+ * The mapping is the structural inverse of `projectIRToBundledPage`:
790
+ *
791
+ * LegacySpec IRDocument
792
+ * ---------- ----------
793
+ * metadata.component -> id (or opts.docId override)
794
+ * metadata.tags -> metadata.tags
795
+ * description -> description (kept verbatim;
796
+ * authored notes appended via
797
+ * forward `notes` arg are not
798
+ * stripped — that round-trip is
799
+ * best-effort and Phase A4 will
800
+ * split them back out)
801
+ *
802
+ * groups[] -> states[] (one per group)
803
+ * group.id / name / description -> state.id / name / description
804
+ * assertion.target.criteria -> state.requiredElements[i] (or,
805
+ * preferred, smState.elements when
806
+ * the stateMachine block is present)
807
+ * first-found assertion.precondition -> state.precondition
808
+ *
809
+ * stateMachine.states[] -> contributes isInitial + elements
810
+ * smState.transitions[] -> transitions[] (deduped by id)
811
+ * transition.activateStates -> transition.activateStates
812
+ * transition.deactivateStates -> transition.exitStates
813
+ * transition.process[] -> transition.actions[]
814
+ * step.action -> action.type
815
+ * step.target -> action.target (criteria inverted)
816
+ * step.waitAfter -> action.waitAfter
817
+ *
818
+ * Documented losses (acceptable for Phase A2):
819
+ * - Legacy assertion ids (`{stateId}-elem-{i}`) are NOT preserved across the
820
+ * round-trip — the forward direction regenerates them from index, so any
821
+ * hand-edited ids are lost. See Phase A4.
822
+ * - Legacy `assertionType` / `severity` / `category` / `source` per
823
+ * assertion are reduced to defaults on round-trip.
824
+ * - Legacy `expected` / `assertionType: "count"` and `notExists` semantics
825
+ * are not modeled in IR — the assertion's criteria still survive (as a
826
+ * state requiredElement) but the assertion-flavor metadata is dropped.
827
+ * - Legacy `testing` block + extra metadata keys outside `component` /
828
+ * `tags` are ignored. Authors keep them via the legacy spec file directly
829
+ * during Phase A2 — they will move into IR companion files in Phase A4.
830
+ * - The forward `notes` argument's appended paragraph is currently kept
831
+ * in `description`; Phase A4 will split it back out into a dedicated
832
+ * companion file.
833
+ *
834
+ * @param legacy The legacy spec to invert.
835
+ * @param opts Optional doc-level overrides.
836
+ */
837
+ export function projectLegacyToIR(
838
+ legacy: LegacySpec,
839
+ opts?: ProjectLegacyToIROptions,
840
+ ): IRDocument {
841
+ const component =
842
+ opts?.docId ??
843
+ legacy.metadata?.component ??
844
+ opts?.fallbackName ??
845
+ "legacy-spec";
846
+ const name = legacy.metadata?.component ?? opts?.fallbackName ?? component;
847
+
848
+ // Index state-machine states by id for quick lookup when building IR states
849
+ // out of groups. Older specs may not have a stateMachine block.
850
+ const smByGroupId = new Map<string, LegacyStateMachineState>();
851
+ if (legacy.stateMachine !== undefined && Array.isArray(legacy.stateMachine.states)) {
852
+ for (const s of legacy.stateMachine.states) {
853
+ smByGroupId.set(s.id, s);
854
+ }
855
+ }
856
+
857
+ const groups = Array.isArray(legacy.groups) ? legacy.groups : [];
858
+ const states: IRState[] = groups.map((group) =>
859
+ buildIRState(group, smByGroupId.get(group.id)),
860
+ );
861
+
862
+ // NOTE: Some legacy specs declare `stateMachine.states[]` whose ids do NOT
863
+ // correspond to any `groups[]` entry (e.g., page-level states like
864
+ // `wrappers-installed-tab` vs assertion-category groups like
865
+ // `page-structure`). The IR's group-state mapping is 1:1 with groups by
866
+ // construction (the forward projection emits one group per IR state), so
867
+ // these orphan SM states cannot survive the round-trip cleanly. They are
868
+ // dropped here as a documented loss — Phase A4 will revisit by either
869
+ // (a) introducing a separate IR concept for "page state" vs
870
+ // "assertion group", or (b) hand-merging the legacy specs so groups and
871
+ // SM states share ids.
872
+ // Transitions originating from orphan SM states are likewise omitted from
873
+ // IR (their fromStates would dangle). We filter them below.
874
+ const groupIds = new Set(groups.map((g) => g.id));
875
+
876
+ // Build transitions only from SM states whose ids appear in groups[]. Any
877
+ // transition with a fromState that isn't a known group is dropped — the
878
+ // forward direction would have nowhere to emit it.
879
+ let filteredStateMachine: LegacyStateMachine | undefined = legacy.stateMachine;
880
+ if (
881
+ legacy.stateMachine !== undefined &&
882
+ Array.isArray(legacy.stateMachine.states) &&
883
+ legacy.stateMachine.states.some((s) => !groupIds.has(s.id))
884
+ ) {
885
+ filteredStateMachine = {
886
+ states: legacy.stateMachine.states.filter((s) => groupIds.has(s.id)),
887
+ };
888
+ }
889
+ const transitions = buildIRTransitions(filteredStateMachine);
890
+
891
+ // Initial state: first state with isInitial:true wins (mirrors how the
892
+ // forward direction picks the document's `initialState`). Only consider
893
+ // SM states that correspond to a group (so initialState references a real
894
+ // IR state).
895
+ let initialState: string | undefined;
896
+ if (legacy.stateMachine !== undefined && Array.isArray(legacy.stateMachine.states)) {
897
+ const initial = legacy.stateMachine.states.find(
898
+ (s) => s.isInitial === true && groupIds.has(s.id),
899
+ );
900
+ if (initial !== undefined) initialState = initial.id;
901
+ }
902
+
903
+ const doc: IRDocument = {
904
+ version: "1.0",
905
+ id: component,
906
+ name,
907
+ states,
908
+ transitions,
909
+ provenance: { source: "migrated" },
910
+ };
911
+ if (legacy.description !== undefined && legacy.description.length > 0) {
912
+ doc.description = legacy.description;
913
+ }
914
+ if (legacy.metadata !== undefined && legacy.metadata.tags !== undefined) {
915
+ doc.metadata = { tags: legacy.metadata.tags };
916
+ }
917
+ if (initialState !== undefined) {
918
+ doc.initialState = initialState;
919
+ }
920
+ return doc;
921
+ }