@adhdev/daemon-core 0.9.82-rc.34 → 0.9.82-rc.340

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 (318) hide show
  1. package/dist/boot/daemon-lifecycle.d.ts +8 -0
  2. package/dist/boot/process-hardening.d.ts +50 -0
  3. package/dist/build-info.d.ts +37 -0
  4. package/dist/chat/source-machine.d.ts +166 -0
  5. package/dist/chat/source-resolver.d.ts +104 -0
  6. package/dist/chat/subscription-updates.d.ts +1 -0
  7. package/dist/cli-adapter-types.d.ts +46 -2
  8. package/dist/cli-adapters/cli-script-runner.d.ts +117 -0
  9. package/dist/cli-adapters/cli-state-engine.d.ts +198 -0
  10. package/dist/cli-adapters/provider-cli-adapter.d.ts +116 -64
  11. package/dist/cli-adapters/provider-cli-parse.d.ts +4 -0
  12. package/dist/cli-adapters/provider-cli-runtime.d.ts +34 -0
  13. package/dist/cli-adapters/provider-cli-shared.d.ts +53 -0
  14. package/dist/cli-adapters/raw-terminal-io.d.ts +37 -0
  15. package/dist/cli-adapters/resolve-executable.d.ts +14 -0
  16. package/dist/cli-adapters/terminal-backends/ghostty-vt-backend.d.ts +6 -3
  17. package/dist/cli-adapters/terminal-backends/types.d.ts +10 -2
  18. package/dist/cli-adapters/terminal-screen.d.ts +4 -11
  19. package/dist/commands/cli-manager.d.ts +2 -1
  20. package/dist/commands/handler.d.ts +110 -0
  21. package/dist/commands/mesh-coordinator.d.ts +85 -1
  22. package/dist/commands/router.d.ts +308 -1
  23. package/dist/commands/upgrade-helper.d.ts +12 -0
  24. package/dist/config/chat-history.d.ts +9 -0
  25. package/dist/config/config.d.ts +5 -0
  26. package/dist/config/mesh-config.d.ts +77 -1
  27. package/dist/detection/ide-detector.d.ts +13 -0
  28. package/dist/detection/win32-ide-version.d.ts +37 -0
  29. package/dist/git/change-impact-config.d.ts +159 -0
  30. package/dist/git/git-commands.d.ts +11 -1
  31. package/dist/git/git-diff.d.ts +6 -0
  32. package/dist/git/git-executor.d.ts +11 -0
  33. package/dist/git/git-status.d.ts +23 -0
  34. package/dist/git/git-types.d.ts +2 -50
  35. package/dist/git/index.d.ts +2 -0
  36. package/dist/index.d.ts +52 -13
  37. package/dist/index.js +39439 -14533
  38. package/dist/index.js.map +1 -1
  39. package/dist/index.mjs +39000 -14215
  40. package/dist/index.mjs.map +1 -1
  41. package/dist/installer.d.ts +1 -4
  42. package/dist/ipc/local-ipc-server.d.ts +91 -0
  43. package/dist/launch.d.ts +1 -1
  44. package/dist/logging/async-batch-writer.d.ts +10 -0
  45. package/dist/logging/log-redactor.d.ts +24 -0
  46. package/dist/logging/log-tail-reader.d.ts +46 -0
  47. package/dist/mesh/contracts.d.ts +164 -0
  48. package/dist/mesh/coordinator-prompt.d.ts +36 -0
  49. package/dist/mesh/coordinator-registry.d.ts +59 -0
  50. package/dist/mesh/mesh-active-work.d.ts +174 -0
  51. package/dist/mesh/mesh-delivery-policy.d.ts +131 -0
  52. package/dist/mesh/mesh-events-coordinator.d.ts +169 -0
  53. package/dist/mesh/mesh-events-pending.d.ts +45 -0
  54. package/dist/mesh/mesh-events-stale.d.ts +40 -0
  55. package/dist/mesh/mesh-events-utils.d.ts +66 -0
  56. package/dist/mesh/mesh-events.d.ts +6 -49
  57. package/dist/mesh/mesh-fast-forward.d.ts +81 -0
  58. package/dist/mesh/mesh-host-ownership.d.ts +9 -0
  59. package/dist/mesh/mesh-init.d.ts +86 -0
  60. package/dist/mesh/mesh-ledger-reconciliation.d.ts +23 -3
  61. package/dist/mesh/mesh-ledger.d.ts +77 -1
  62. package/dist/mesh/mesh-missions.d.ts +116 -0
  63. package/dist/mesh/mesh-reconcile-loop.d.ts +7 -0
  64. package/dist/mesh/mesh-refine-batch.d.ts +68 -0
  65. package/dist/mesh/mesh-refine-status.d.ts +62 -0
  66. package/dist/mesh/mesh-review-inbox.d.ts +90 -0
  67. package/dist/mesh/mesh-routing.d.ts +70 -0
  68. package/dist/mesh/mesh-runtime-store.d.ts +418 -0
  69. package/dist/mesh/mesh-task-stats.d.ts +49 -0
  70. package/dist/mesh/mesh-unresolved-forward-outbox.d.ts +30 -0
  71. package/dist/mesh/mesh-work-queue.d.ts +208 -5
  72. package/dist/mesh/preview-freshness.d.ts +18 -0
  73. package/dist/mesh/refine-config.d.ts +216 -0
  74. package/dist/mesh/worktree-bootstrap-config.d.ts +135 -0
  75. package/dist/providers/acp-provider-instance.d.ts +2 -0
  76. package/dist/providers/approval-utils.d.ts +13 -0
  77. package/dist/providers/chat-message-normalization.d.ts +12 -0
  78. package/dist/providers/cli-provider-instance.d.ts +76 -3
  79. package/dist/providers/contracts.d.ts +139 -6
  80. package/dist/providers/external-sources.d.ts +71 -0
  81. package/dist/providers/native-history/antigravity-cli-transcript.d.ts +100 -0
  82. package/dist/providers/native-history/claude-cli-transcript.d.ts +70 -0
  83. package/dist/providers/native-history/codex-cli-transcript.d.ts +73 -0
  84. package/dist/providers/native-history/dispatcher.d.ts +27 -0
  85. package/dist/providers/native-history/hermes-cli-transcript.d.ts +30 -0
  86. package/dist/providers/native-history/index.d.ts +13 -0
  87. package/dist/providers/provider-instance-manager.d.ts +13 -0
  88. package/dist/providers/provider-instance.d.ts +13 -1
  89. package/dist/providers/provider-loader.d.ts +26 -4
  90. package/dist/providers/provider-trust.d.ts +31 -0
  91. package/dist/providers/read-chat-contract.d.ts +29 -0
  92. package/dist/providers/sdk/v1/builders/acp/detect-status.d.ts +68 -0
  93. package/dist/providers/sdk/v1/builders/cli/detect-status.d.ts +85 -0
  94. package/dist/providers/sdk/v1/builders/cli/parse-approval-squash.d.ts +59 -0
  95. package/dist/providers/sdk/v1/builders/cli/parse-approval.d.ts +65 -0
  96. package/dist/providers/sdk/v1/builders/cli/parse-session.d.ts +91 -0
  97. package/dist/providers/sdk/v1/builders/cli/visible-region.d.ts +42 -0
  98. package/dist/providers/sdk/v1/fixture-tooling/format.d.ts +126 -0
  99. package/dist/providers/sdk/v1/fixture-tooling/index.d.ts +8 -0
  100. package/dist/providers/sdk/v1/fixture-tooling/replay.d.ts +38 -0
  101. package/dist/providers/sdk/v1/index.d.ts +30 -0
  102. package/dist/providers/sdk/v1/sandbox/README-design.d.ts +193 -0
  103. package/dist/providers/sdk/v1/sandbox/require-whitelist.d.ts +74 -0
  104. package/dist/providers/sdk/v1/sandbox/script-runner.d.ts +98 -0
  105. package/dist/providers/sdk/v1/types/cli/index.d.ts +277 -0
  106. package/dist/providers/sdk/v1/types/common/index.d.ts +203 -0
  107. package/dist/providers/sdk/v1/validators/index.d.ts +5 -0
  108. package/dist/providers/sdk/v1/validators/manifest.d.ts +40 -0
  109. package/dist/providers/sdk/v1/validators/taint.d.ts +52 -0
  110. package/dist/providers/spec/adapter.d.ts +69 -0
  111. package/dist/providers/spec/cli-adapter.d.ts +217 -0
  112. package/dist/providers/spec/evaluator.d.ts +23 -0
  113. package/dist/providers/spec/fsm-driver.d.ts +306 -0
  114. package/dist/providers/spec/fsm-evaluator.d.ts +72 -0
  115. package/dist/providers/spec/fsm-loader.d.ts +14 -0
  116. package/dist/providers/spec/fsm-types.d.ts +144 -0
  117. package/dist/providers/spec/native-history-executor.d.ts +41 -0
  118. package/dist/providers/spec/pre-launch-trust.d.ts +16 -0
  119. package/dist/providers/spec/route.d.ts +4 -0
  120. package/dist/providers/spec/types.d.ts +180 -0
  121. package/dist/providers/status-monitor.d.ts +7 -7
  122. package/dist/providers/transcript-v2.d.ts +176 -0
  123. package/dist/providers/types/interactive-prompt.d.ts +84 -0
  124. package/dist/providers/working-dir.d.ts +17 -0
  125. package/dist/repo-mesh-types.d.ts +295 -13
  126. package/dist/sessions/registry.d.ts +6 -0
  127. package/dist/shared-types-extra.d.ts +2 -4
  128. package/dist/shared-types.d.ts +59 -2
  129. package/dist/status/normalize.d.ts +1 -1
  130. package/dist/status/normalize.js +1 -0
  131. package/dist/status/normalize.js.map +1 -1
  132. package/dist/status/normalize.mjs +1 -0
  133. package/dist/status/normalize.mjs.map +1 -1
  134. package/dist/status/reporter.d.ts +2 -0
  135. package/dist/status/snapshot.d.ts +26 -0
  136. package/dist/system/load-better-sqlite3.d.ts +21 -0
  137. package/dist/types.d.ts +5 -0
  138. package/package.json +7 -3
  139. package/src/agent-stream/poller.ts +2 -3
  140. package/src/agent-stream/provider-adapter.ts +1 -1
  141. package/src/boot/daemon-lifecycle.ts +55 -12
  142. package/src/boot/process-hardening.ts +89 -0
  143. package/src/build-info.ts +73 -0
  144. package/src/chat/source-machine.ts +534 -0
  145. package/src/chat/source-resolver.ts +0 -0
  146. package/src/chat/subscription-updates.ts +20 -1
  147. package/src/cli-adapter-types.d.ts +1 -0
  148. package/src/cli-adapter-types.ts +47 -2
  149. package/src/cli-adapters/cli-script-runner.ts +421 -0
  150. package/src/cli-adapters/cli-state-engine.ts +1215 -0
  151. package/src/cli-adapters/provider-cli-adapter.d.ts +1 -1
  152. package/src/cli-adapters/provider-cli-adapter.ts +774 -1141
  153. package/src/cli-adapters/provider-cli-parse.d.ts +1 -0
  154. package/src/cli-adapters/provider-cli-parse.ts +13 -0
  155. package/src/cli-adapters/provider-cli-runtime.ts +56 -7
  156. package/src/cli-adapters/provider-cli-shared.d.ts +4 -0
  157. package/src/cli-adapters/provider-cli-shared.ts +100 -11
  158. package/src/cli-adapters/pty-transport.ts +2 -1
  159. package/src/cli-adapters/raw-terminal-io.ts +252 -0
  160. package/src/cli-adapters/resolve-executable.ts +90 -0
  161. package/src/cli-adapters/session-host-transport.ts +2 -1
  162. package/src/cli-adapters/terminal-backends/ghostty-vt-backend.ts +53 -29
  163. package/src/cli-adapters/terminal-backends/types.ts +10 -3
  164. package/src/cli-adapters/terminal-screen.ts +16 -81
  165. package/src/commands/chat-commands.ts +1823 -62
  166. package/src/commands/cli-manager.ts +286 -14
  167. package/src/commands/handler.ts +809 -2
  168. package/src/commands/mesh-coordinator.ts +332 -122
  169. package/src/commands/router.ts +7433 -1317
  170. package/src/commands/stream-commands.ts +8 -0
  171. package/src/commands/upgrade-helper.ts +81 -30
  172. package/src/config/chat-history.ts +483 -24
  173. package/src/config/config.ts +12 -0
  174. package/src/config/mesh-config.ts +388 -6
  175. package/src/config/recent-activity.ts +8 -2
  176. package/src/daemon/dev-auto-implement.ts +3 -2
  177. package/src/daemon/dev-cli-debug.ts +10 -1
  178. package/src/detection/cli-detector.ts +28 -9
  179. package/src/detection/ide-detector.ts +55 -16
  180. package/src/detection/win32-ide-version.ts +106 -0
  181. package/src/git/change-impact-config.ts +354 -0
  182. package/src/git/git-commands.ts +55 -14
  183. package/src/git/git-diff.ts +53 -0
  184. package/src/git/git-executor.ts +12 -0
  185. package/src/git/git-status.ts +574 -43
  186. package/src/git/git-types.ts +14 -62
  187. package/src/git/git-worktree.ts +8 -1
  188. package/src/git/index.ts +16 -0
  189. package/src/index.ts +146 -12
  190. package/src/installer.d.ts +1 -1
  191. package/src/installer.ts +8 -6
  192. package/src/ipc/local-ipc-server.ts +278 -0
  193. package/src/launch.d.ts +1 -1
  194. package/src/launch.ts +37 -28
  195. package/src/logging/async-batch-writer.ts +55 -0
  196. package/src/logging/log-redactor.ts +100 -0
  197. package/src/logging/log-tail-reader.ts +220 -0
  198. package/src/logging/logger.ts +2 -1
  199. package/src/mesh/contracts.ts +329 -0
  200. package/src/mesh/coordinator-prompt.ts +234 -31
  201. package/src/mesh/coordinator-registry.ts +121 -0
  202. package/src/mesh/mesh-active-work.ts +623 -0
  203. package/src/mesh/mesh-delivery-policy.ts +315 -0
  204. package/src/mesh/mesh-events-coordinator.ts +2144 -0
  205. package/src/mesh/mesh-events-pending.ts +508 -0
  206. package/src/mesh/mesh-events-stale.ts +290 -0
  207. package/src/mesh/mesh-events-utils.ts +300 -0
  208. package/src/mesh/mesh-events.ts +30 -1035
  209. package/src/mesh/mesh-fast-forward.ts +839 -0
  210. package/src/mesh/mesh-host-ownership.ts +73 -0
  211. package/src/mesh/mesh-init.ts +260 -0
  212. package/src/mesh/mesh-ledger-reconciliation.ts +12 -5
  213. package/src/mesh/mesh-ledger.ts +589 -102
  214. package/src/mesh/mesh-missions.ts +248 -0
  215. package/src/mesh/mesh-reconcile-loop.ts +822 -0
  216. package/src/mesh/mesh-refine-batch.ts +197 -0
  217. package/src/mesh/mesh-refine-status.ts +231 -0
  218. package/src/mesh/mesh-review-inbox.ts +307 -0
  219. package/src/mesh/mesh-routing.ts +291 -0
  220. package/src/mesh/mesh-runtime-store.ts +1626 -0
  221. package/src/mesh/mesh-task-stats.ts +154 -0
  222. package/src/mesh/mesh-unresolved-forward-outbox.ts +185 -0
  223. package/src/mesh/mesh-work-queue.ts +888 -141
  224. package/src/mesh/preview-freshness.ts +118 -0
  225. package/src/mesh/refine-config.ts +423 -0
  226. package/src/mesh/worktree-bootstrap-config.ts +331 -0
  227. package/src/providers/acp-provider-instance.ts +25 -9
  228. package/src/providers/approval-utils.d.ts +4 -0
  229. package/src/providers/approval-utils.ts +47 -5
  230. package/src/providers/chat-message-normalization.ts +45 -5
  231. package/src/providers/cli-provider-instance.ts +1038 -99
  232. package/src/providers/contracts.d.ts +55 -0
  233. package/src/providers/contracts.ts +150 -6
  234. package/src/providers/extension-provider-instance.ts +12 -7
  235. package/src/providers/external-sources.ts +218 -0
  236. package/src/providers/ide-provider-instance.ts +35 -12
  237. package/src/providers/native-history/antigravity-cli-transcript.ts +643 -0
  238. package/src/providers/native-history/claude-cli-transcript.ts +396 -0
  239. package/src/providers/native-history/codex-cli-transcript.ts +479 -0
  240. package/src/providers/native-history/dispatcher.ts +340 -0
  241. package/src/providers/native-history/hermes-cli-transcript.ts +230 -0
  242. package/src/providers/native-history/index.ts +30 -0
  243. package/src/providers/provider-instance-manager.ts +30 -0
  244. package/src/providers/provider-instance.ts +10 -1
  245. package/src/providers/provider-loader.ts +670 -50
  246. package/src/providers/provider-schema.ts +87 -14
  247. package/src/providers/provider-trust.ts +114 -0
  248. package/src/providers/read-chat-contract.ts +76 -16
  249. package/src/providers/sdk/README.md +49 -0
  250. package/src/providers/sdk/v1/builders/acp/detect-status.ts +144 -0
  251. package/src/providers/sdk/v1/builders/cli/detect-status.ts +262 -0
  252. package/src/providers/sdk/v1/builders/cli/parse-approval-squash.ts +158 -0
  253. package/src/providers/sdk/v1/builders/cli/parse-approval.ts +250 -0
  254. package/src/providers/sdk/v1/builders/cli/parse-session.ts +276 -0
  255. package/src/providers/sdk/v1/builders/cli/visible-region.ts +143 -0
  256. package/src/providers/sdk/v1/fixture-tooling/format.ts +130 -0
  257. package/src/providers/sdk/v1/fixture-tooling/index.ts +22 -0
  258. package/src/providers/sdk/v1/fixture-tooling/replay.ts +352 -0
  259. package/src/providers/sdk/v1/index.ts +152 -0
  260. package/src/providers/sdk/v1/sandbox/README-design.ts +195 -0
  261. package/src/providers/sdk/v1/sandbox/require-whitelist.ts +472 -0
  262. package/src/providers/sdk/v1/sandbox/script-runner.ts +150 -0
  263. package/src/providers/sdk/v1/schemas/cli/provider.schema.json +504 -0
  264. package/src/providers/sdk/v1/schemas/primitives/acp-session-protocol-v1.json +131 -0
  265. package/src/providers/sdk/v1/schemas/primitives/native-history-codex-rollout-v1.json +66 -0
  266. package/src/providers/sdk/v1/schemas/primitives/tui-approval-squash-v1.json +91 -0
  267. package/src/providers/sdk/v1/schemas/primitives/tui-assistant-block-v1.json +91 -0
  268. package/src/providers/sdk/v1/schemas/primitives/tui-cue-ordering-v1.json +47 -0
  269. package/src/providers/sdk/v1/schemas/primitives/tui-dispatch-order-v1.json +32 -0
  270. package/src/providers/sdk/v1/schemas/primitives/tui-footer-chrome-v1.json +42 -0
  271. package/src/providers/sdk/v1/schemas/primitives/tui-index-finder-v1.json +27 -0
  272. package/src/providers/sdk/v1/schemas/primitives/tui-modal-v1.json +125 -0
  273. package/src/providers/sdk/v1/schemas/primitives/tui-prompt-marker-v1.json +45 -0
  274. package/src/providers/sdk/v1/schemas/primitives/tui-session-id-extraction-v1.json +46 -0
  275. package/src/providers/sdk/v1/schemas/primitives/tui-settled-prompt-v1.json +71 -0
  276. package/src/providers/sdk/v1/schemas/primitives/tui-spinner-v1.json +83 -0
  277. package/src/providers/sdk/v1/schemas/primitives/tui-transcript-pty-v1.json +83 -0
  278. package/src/providers/sdk/v1/schemas/primitives/tui-visible-region-v1.json +57 -0
  279. package/src/providers/sdk/v1/schemas/primitives/tui-welcome-screen-v1.json +35 -0
  280. package/src/providers/sdk/v1/types/cli/index.ts +365 -0
  281. package/src/providers/sdk/v1/types/common/index.ts +229 -0
  282. package/src/providers/sdk/v1/validators/index.ts +19 -0
  283. package/src/providers/sdk/v1/validators/manifest.ts +110 -0
  284. package/src/providers/sdk/v1/validators/taint.ts +309 -0
  285. package/src/providers/spec/adapter.ts +168 -0
  286. package/src/providers/spec/cli-adapter.ts +986 -0
  287. package/src/providers/spec/evaluator.ts +345 -0
  288. package/src/providers/spec/fsm-driver.ts +993 -0
  289. package/src/providers/spec/fsm-evaluator.ts +255 -0
  290. package/src/providers/spec/fsm-loader.ts +120 -0
  291. package/src/providers/spec/fsm-types.ts +238 -0
  292. package/src/providers/spec/native-history-executor.ts +1136 -0
  293. package/src/providers/spec/pre-launch-trust.ts +104 -0
  294. package/src/providers/spec/route.ts +51 -0
  295. package/src/providers/spec/types.ts +226 -0
  296. package/src/providers/status-monitor.d.ts +7 -7
  297. package/src/providers/status-monitor.ts +37 -22
  298. package/src/providers/transcript-v2.ts +567 -0
  299. package/src/providers/types/interactive-prompt.ts +536 -0
  300. package/src/providers/version-archive.ts +64 -24
  301. package/src/providers/working-dir.ts +23 -0
  302. package/src/repo-mesh-types.ts +378 -14
  303. package/src/sessions/registry.ts +6 -0
  304. package/src/shared-types-extra.ts +2 -4
  305. package/src/shared-types.d.ts +8 -0
  306. package/src/shared-types.ts +64 -1
  307. package/src/status/builders.ts +26 -6
  308. package/src/status/normalize.ts +2 -0
  309. package/src/status/reporter.ts +19 -1
  310. package/src/status/snapshot.ts +95 -26
  311. package/src/system/host-memory.ts +29 -12
  312. package/src/system/load-better-sqlite3.ts +68 -0
  313. package/src/types.ts +5 -0
  314. package/dist/cli-adapters/terminal-backends/xterm-backend.d.ts +0 -17
  315. package/dist/mesh/mesh-sync.d.ts +0 -53
  316. package/src/cli-adapters/terminal-backends/ghostty-vt-backend.d.ts +0 -16
  317. package/src/cli-adapters/terminal-backends/xterm-backend.ts +0 -97
  318. package/src/mesh/mesh-sync.ts +0 -111
@@ -0,0 +1,262 @@
1
+ /**
2
+ * buildDetectStatusFromTui
3
+ *
4
+ * Turns a manifest's declarative TUI block into a runtime
5
+ * `(input: CliStatusInput) => CliStatus | null` function.
6
+ *
7
+ * Inputs handled (all optional except spinner):
8
+ * - tui/spinner@1 → contributes `generating`
9
+ * - tui/settled-prompt@1 → contributes `idle`
10
+ * - tui/modal@1 → contributes `waiting_approval`
11
+ *
12
+ * Dispatch order (sprint-2026-06 contract):
13
+ * 1. Active generation cues win over idle prompt — checked first.
14
+ * Reason: codex/claude keep model footer visible during `Working` and
15
+ * `hasReadyPrompt` falsely fired idle. Audit §4.2 introduces
16
+ * `tui/cue-ordering` as the named primitive for this rule.
17
+ * 2. Modal cue (if present) is checked next — `waiting_approval`.
18
+ * 3. Settled-prompt cue — `idle`.
19
+ * 4. Default: `null` (no change).
20
+ */
21
+
22
+ import type {
23
+ CliStatusInput,
24
+ CliStatus,
25
+ CliDetectStatusFn,
26
+ } from '../../types/cli/index.js';
27
+ import {
28
+ applyVisibleRegion,
29
+ type VisibleRegionSpec,
30
+ } from './visible-region.js';
31
+
32
+ // ─── Primitive spec shapes (mirror the JSON schemas) ───────────────────
33
+
34
+ interface RegexLikeSpec {
35
+ regex: string;
36
+ flags?: string;
37
+ }
38
+
39
+ interface SpinnerPatternSpec extends RegexLikeSpec {
40
+ description?: string;
41
+ }
42
+
43
+ interface SpinnerSpec {
44
+ $schema: 'adhdev:tui/spinner@1';
45
+ patterns: SpinnerPatternSpec[];
46
+ scope?: 'live-frame-tail' | 'whole-screen' | 'recent-buffer';
47
+ scopeWindowLines?: number;
48
+ }
49
+
50
+ interface SettledPromptFooterSpec {
51
+ pattern: string;
52
+ kind?: 'substring' | 'regex';
53
+ flags?: string;
54
+ }
55
+
56
+ interface SettledPromptSpec {
57
+ $schema: 'adhdev:tui/settled-prompt@1';
58
+ regex: string;
59
+ flags?: string;
60
+ withFooter?: SettledPromptFooterSpec[];
61
+ scope?: 'last-n-lines' | 'whole-screen';
62
+ scopeWindowLines?: number;
63
+ }
64
+
65
+ interface ModalSpec {
66
+ $schema: 'adhdev:tui/modal@1';
67
+ questionPattern: string;
68
+ questionFlags?: string;
69
+ questionVariants?: Array<{ regex: string; flags?: string; label?: string }>;
70
+ buttonPattern: string;
71
+ buttonFlags?: string;
72
+ }
73
+
74
+ export type DispatchGroup =
75
+ | 'spinner'
76
+ | 'modal'
77
+ | 'settled-prompt'
78
+ | 'cue-ordering'
79
+ | 'error-detection'
80
+ | 'approval-stitching';
81
+
82
+ export interface DispatchOrderSpec {
83
+ $schema: 'adhdev:tui/dispatch-order@1';
84
+ order: DispatchGroup[];
85
+ onNoMatch?: 'idle' | 'unknown' | 'preserve-last';
86
+ }
87
+
88
+ export interface DetectStatusTuiSpec {
89
+ spinner?: SpinnerSpec;
90
+ settledPrompt?: SettledPromptSpec;
91
+ modal?: ModalSpec;
92
+ dispatchOrder?: DispatchOrderSpec;
93
+ visibleRegion?: VisibleRegionSpec;
94
+ }
95
+
96
+ // ─── Helpers ───────────────────────────────────────────────────────────
97
+
98
+ function compile(re: string, flags?: string): RegExp {
99
+ try {
100
+ return new RegExp(re, flags ?? '');
101
+ } catch (e) {
102
+ throw new Error(`Invalid regex /${re}/${flags ?? ''}: ${(e as Error).message}`);
103
+ }
104
+ }
105
+
106
+ function takeTail(text: string, lines: number): string {
107
+ if (!text) return '';
108
+ const split = text.split('\n');
109
+ if (split.length <= lines) return text;
110
+ return split.slice(-lines).join('\n');
111
+ }
112
+
113
+ function scopeText(
114
+ input: CliStatusInput,
115
+ scope: SpinnerSpec['scope'] | SettledPromptSpec['scope'],
116
+ windowLines: number | undefined,
117
+ ): string {
118
+ const lines = windowLines && windowLines > 0 ? windowLines : 8;
119
+ switch (scope) {
120
+ case 'whole-screen':
121
+ return input.screenText ?? '';
122
+ case 'recent-buffer':
123
+ return input.tail ?? '';
124
+ case 'last-n-lines':
125
+ case 'live-frame-tail':
126
+ case undefined:
127
+ default:
128
+ return takeTail(input.screenText ?? '', lines);
129
+ }
130
+ }
131
+
132
+ function compileSpinnerMatchers(spec: SpinnerSpec): RegExp[] {
133
+ return spec.patterns.map((p) => compile(p.regex, p.flags ?? 'i'));
134
+ }
135
+
136
+ function spinnerMatches(spec: SpinnerSpec, input: CliStatusInput): boolean {
137
+ const matchers = compileSpinnerMatchers(spec);
138
+ const text = scopeText(input, spec.scope, spec.scopeWindowLines);
139
+ return matchers.some((re) => re.test(text));
140
+ }
141
+
142
+ function compileSettledPromptMatchers(spec: SettledPromptSpec): {
143
+ prompt: RegExp;
144
+ footers: Array<{ test: (s: string) => boolean }>;
145
+ } {
146
+ const prompt = compile(spec.regex, spec.flags ?? 'm');
147
+ const footers = (spec.withFooter ?? []).map((f) => {
148
+ if (f.kind === 'regex') {
149
+ const re = compile(f.pattern, f.flags ?? 'i');
150
+ return { test: (s: string) => re.test(s) };
151
+ }
152
+ const needle = f.pattern.toLowerCase();
153
+ return { test: (s: string) => s.toLowerCase().includes(needle) };
154
+ });
155
+ return { prompt, footers };
156
+ }
157
+
158
+ function settledPromptMatches(spec: SettledPromptSpec, input: CliStatusInput): boolean {
159
+ const { prompt, footers } = compileSettledPromptMatchers(spec);
160
+ const text = scopeText(input, spec.scope, spec.scopeWindowLines);
161
+ if (!prompt.test(text)) return false;
162
+ if (footers.length === 0) return true;
163
+ return footers.every((f) => f.test(text));
164
+ }
165
+
166
+ function modalMatches(spec: ModalSpec, input: CliStatusInput): boolean {
167
+ // Status-level modal detection is cue-only — does the question appear at all?
168
+ // Button extraction lives in buildParseApprovalFromTui.
169
+ const text = input.screenText ?? '';
170
+ const question = compile(spec.questionPattern, spec.questionFlags ?? 'i');
171
+ if (question.test(text)) return true;
172
+ for (const variant of spec.questionVariants ?? []) {
173
+ const re = compile(variant.regex, variant.flags ?? 'i');
174
+ if (re.test(text)) return true;
175
+ }
176
+ return false;
177
+ }
178
+
179
+ // ─── Public builder ────────────────────────────────────────────────────
180
+
181
+ const DEFAULT_ORDER: DispatchGroup[] = ['spinner', 'modal', 'settled-prompt'];
182
+
183
+ function evaluateGroup(
184
+ group: DispatchGroup,
185
+ spec: DetectStatusTuiSpec,
186
+ input: CliStatusInput,
187
+ compiled: {
188
+ spinner: RegExp[] | null;
189
+ settled: ReturnType<typeof compileSettledPromptMatchers> | null;
190
+ },
191
+ ): CliStatus | null {
192
+ switch (group) {
193
+ case 'spinner': {
194
+ if (!spec.spinner || !compiled.spinner) return null;
195
+ const text = scopeText(input, spec.spinner.scope, spec.spinner.scopeWindowLines);
196
+ return compiled.spinner.some((re) => re.test(text)) ? 'generating' : null;
197
+ }
198
+ case 'modal': {
199
+ if (!spec.modal) return null;
200
+ return modalMatches(spec.modal, input) ? 'waiting_approval' : null;
201
+ }
202
+ case 'settled-prompt': {
203
+ if (!spec.settledPrompt || !compiled.settled) return null;
204
+ const text = scopeText(input, spec.settledPrompt.scope, spec.settledPrompt.scopeWindowLines);
205
+ if (!compiled.settled.prompt.test(text)) return null;
206
+ if (compiled.settled.footers.length === 0) return 'idle';
207
+ return compiled.settled.footers.every((f) => f.test(text)) ? 'idle' : null;
208
+ }
209
+ // Groups declared in the catalog but not yet implemented as builder steps
210
+ // return null so they are no-ops in dispatch — declaring them in `order`
211
+ // is forward-compatible. Phase 2 Week 8+ will wire them.
212
+ case 'cue-ordering':
213
+ case 'error-detection':
214
+ case 'approval-stitching':
215
+ return null;
216
+ default:
217
+ return null;
218
+ }
219
+ }
220
+
221
+ export function buildDetectStatusFromTui(spec: DetectStatusTuiSpec): CliDetectStatusFn {
222
+ const compiledSpinner = spec.spinner ? compileSpinnerMatchers(spec.spinner) : null;
223
+ const compiledSettled = spec.settledPrompt
224
+ ? compileSettledPromptMatchers(spec.settledPrompt)
225
+ : null;
226
+ const compiled = { spinner: compiledSpinner, settled: compiledSettled };
227
+ const order = spec.dispatchOrder?.order && spec.dispatchOrder.order.length > 0
228
+ ? spec.dispatchOrder.order
229
+ : DEFAULT_ORDER;
230
+
231
+ return function detectStatus(input: CliStatusInput): CliStatus | null {
232
+ // Apply visible-region scoping before running any matchers.
233
+ const effectiveInput: CliStatusInput = spec.visibleRegion
234
+ ? {
235
+ ...input,
236
+ screenText: applyVisibleRegion(spec.visibleRegion, input.screenText ?? ''),
237
+ tail: applyVisibleRegion(spec.visibleRegion, input.tail),
238
+ }
239
+ : input;
240
+
241
+ for (const group of order) {
242
+ const verdict = evaluateGroup(group, spec, effectiveInput, compiled);
243
+ if (verdict !== null) return verdict;
244
+ }
245
+ // onNoMatch policy: the builder always returns null when nothing matched.
246
+ // The daemon's outer state machine maps null per the policy below:
247
+ // 'idle' → caller treats null as idle (default)
248
+ // 'unknown' → caller keeps status unknown / shows last
249
+ // 'preserve-last' → caller preserves the last reported verdict
250
+ // Encoding this here would require call-site state; instead the daemon
251
+ // reads `dispatchOrder.onNoMatch` from the manifest directly.
252
+ return null;
253
+ };
254
+ }
255
+
256
+ // Internal exports for builder reuse + tests.
257
+ export const __internal = {
258
+ scopeText,
259
+ spinnerMatches,
260
+ settledPromptMatches,
261
+ modalMatches,
262
+ };
@@ -0,0 +1,158 @@
1
+ /**
2
+ * buildParseApprovalFromSquash
3
+ *
4
+ * Companion to buildParseApprovalFromTui. Handles the squashed-modal case:
5
+ * the terminal has re-flowed the approval frame so the rendered text is a
6
+ * single run of lowercase-alnum bytes (e.g. `doyoutrustthecontentsofthisdirectory…1yescontinue2noquit`).
7
+ *
8
+ * Author declares:
9
+ * - `cues` — compact substrings/regexes that identify the modal
10
+ * - `footers` — optional compact substrings that must also be present
11
+ * - `buttonRules` — compact→labels mappings used to recover button labels
12
+ *
13
+ * Returns the same shape as buildParseApprovalFromTui — a CliApprovalModal
14
+ * (message + buttons) or null when no rule matches. Caller composes the two
15
+ * builders: try the regular modal first, then fall back to squash. Codex
16
+ * needs both because at narrow widths the terminal renders the squash form,
17
+ * and at wider widths it renders the regular framed form.
18
+ */
19
+
20
+ import type {
21
+ CliApprovalInput,
22
+ CliApprovalModal,
23
+ CliParseApprovalFn,
24
+ } from '../../types/cli/index.js';
25
+
26
+ // ─── Spec mirrors the JSON schema ─────────────────────────────────────
27
+
28
+ interface SquashCueSpec {
29
+ compact: string;
30
+ kind?: 'substring' | 'regex';
31
+ label?: string;
32
+ messageWhenMatched?: string;
33
+ }
34
+
35
+ interface SquashFooterSpec {
36
+ compact: string;
37
+ kind?: 'substring' | 'regex';
38
+ }
39
+
40
+ interface SquashButtonRuleSpec {
41
+ compact: string;
42
+ kind?: 'substring' | 'regex';
43
+ labels: string[];
44
+ }
45
+
46
+ export interface ApprovalSquashSpec {
47
+ $schema: 'adhdev:tui/approval-squash@1';
48
+ cues: SquashCueSpec[];
49
+ footers?: SquashFooterSpec[];
50
+ buttonRules: SquashButtonRuleSpec[];
51
+ scope?: 'tail-window' | 'whole-screen';
52
+ tailWindowLines?: number;
53
+ }
54
+
55
+ // ─── Internals ────────────────────────────────────────────────────────
56
+
57
+ const COMPACT_RE = /[^a-z0-9]+/g;
58
+
59
+ /**
60
+ * The canonical normalisation. Identical to codex-cli's `compactText`:
61
+ * lowercase, then strip everything that is not a-z or 0-9.
62
+ *
63
+ * Exported for tests and for the (rare) provider that wants to compose this
64
+ * with extra normalisation steps.
65
+ */
66
+ export function compactText(value: string): string {
67
+ return String(value || '').toLowerCase().replace(COMPACT_RE, '');
68
+ }
69
+
70
+ function compileRegex(source: string, flags = 'i'): RegExp {
71
+ try { return new RegExp(source, flags); }
72
+ catch (e) { throw new Error(`Invalid regex /${source}/${flags}: ${(e as Error).message}`); }
73
+ }
74
+
75
+ function matchSubstringOrRegex(
76
+ needle: string,
77
+ kind: 'substring' | 'regex' | undefined,
78
+ haystack: string,
79
+ ): boolean {
80
+ if (kind === 'regex') return compileRegex(needle).test(haystack);
81
+ return haystack.includes(needle);
82
+ }
83
+
84
+ function scopeText(spec: ApprovalSquashSpec, input: CliApprovalInput): string {
85
+ const scope = spec.scope ?? 'tail-window';
86
+ const candidates = [
87
+ String(input.screenText || ''),
88
+ String(input.rawBuffer || ''),
89
+ String(input.buffer || ''),
90
+ String(input.tail || ''),
91
+ ].filter(Boolean);
92
+ const joined = candidates.join('\n');
93
+ if (scope === 'whole-screen') return joined;
94
+ const tailLines = spec.tailWindowLines ?? 24;
95
+ return joined.split('\n').slice(-tailLines).join('\n');
96
+ }
97
+
98
+ // ─── Public builder ───────────────────────────────────────────────────
99
+
100
+ export function buildParseApprovalFromSquash(spec: ApprovalSquashSpec): CliParseApprovalFn {
101
+ if (!Array.isArray(spec.cues) || spec.cues.length === 0) {
102
+ throw new Error('approval-squash spec must declare at least one cue.');
103
+ }
104
+ if (!Array.isArray(spec.buttonRules) || spec.buttonRules.length === 0) {
105
+ throw new Error('approval-squash spec must declare at least one button rule.');
106
+ }
107
+
108
+ return function parseApproval(input: CliApprovalInput): CliApprovalModal | null {
109
+ const text = scopeText(spec, input);
110
+ if (!text) return null;
111
+ const compact = compactText(text);
112
+ if (!compact) return null;
113
+
114
+ // 1) Find the first matching cue. Its messageWhenMatched (if present) is
115
+ // the human-readable modal message; otherwise we fall back to a label
116
+ // or empty string.
117
+ let matchedCue: SquashCueSpec | null = null;
118
+ for (const cue of spec.cues) {
119
+ if (matchSubstringOrRegex(cue.compact, cue.kind, compact)) {
120
+ matchedCue = cue;
121
+ break;
122
+ }
123
+ }
124
+ if (!matchedCue) return null;
125
+
126
+ // 2) If footers are declared, at least one must also match.
127
+ if (spec.footers && spec.footers.length > 0) {
128
+ const anyFooter = spec.footers.some((f) =>
129
+ matchSubstringOrRegex(f.compact, f.kind, compact),
130
+ );
131
+ if (!anyFooter) return null;
132
+ }
133
+
134
+ // 3) Find a button rule whose compact pattern is present in the compacted
135
+ // text. First match wins.
136
+ let labels: string[] | null = null;
137
+ for (const rule of spec.buttonRules) {
138
+ if (matchSubstringOrRegex(rule.compact, rule.kind, compact)) {
139
+ labels = rule.labels;
140
+ break;
141
+ }
142
+ }
143
+ if (!labels || labels.length === 0) return null;
144
+
145
+ const message = matchedCue.messageWhenMatched
146
+ || matchedCue.label
147
+ || '';
148
+
149
+ return { message, buttons: [...labels] };
150
+ };
151
+ }
152
+
153
+ // Internal exports for tests.
154
+ export const __internal = {
155
+ compactText,
156
+ matchSubstringOrRegex,
157
+ scopeText,
158
+ };
@@ -0,0 +1,250 @@
1
+ /**
2
+ * buildParseApprovalFromTui
3
+ *
4
+ * Turns a `tui/modal@1` block into a runtime
5
+ * `(input: CliApprovalInput) => CliApprovalModal | null` function.
6
+ *
7
+ * Contract rules enforced (sprint-2026-06):
8
+ * - Returns null when the question pattern is not visible.
9
+ * - Returns null when the button list is empty or shorter than minButtons.
10
+ * - Button labels are raw text; never rewritten (e.g. "Yes, and don't ask
11
+ * again for X" stays verbatim).
12
+ * - Modal scope respects between-last-two-separators / window-around-question
13
+ * / whole-screen to avoid false-positives from numbered lists in
14
+ * assistant prose.
15
+ */
16
+
17
+ import type {
18
+ CliApprovalInput,
19
+ CliApprovalModal,
20
+ CliParseApprovalFn,
21
+ } from '../../types/cli/index.js';
22
+ import {
23
+ applyVisibleRegion,
24
+ type VisibleRegionSpec,
25
+ } from './visible-region.js';
26
+
27
+ // ─── Spec shapes ───────────────────────────────────────────────────────
28
+
29
+ interface ModalQuestionVariant {
30
+ regex: string;
31
+ flags?: string;
32
+ label?: string;
33
+ }
34
+
35
+ interface ModalContextHeader {
36
+ regex: string;
37
+ flags?: string;
38
+ }
39
+
40
+ export interface ModalTuiSpec {
41
+ $schema: 'adhdev:tui/modal@1';
42
+ questionPattern: string;
43
+ questionFlags?: string;
44
+ questionVariants?: ModalQuestionVariant[];
45
+ buttonPattern: string;
46
+ buttonFlags?: string;
47
+ buttonLabelGroup?: number;
48
+ /**
49
+ * Optional fallback for terminals that render all options on a single line
50
+ * (e.g. Antigravity feedback survey: `[0] skip [1] yes [2] no [3] still using`).
51
+ * When per-line button extraction collects fewer than `minButtons`, the
52
+ * builder makes a second pass with this regex (global match, capture group 1
53
+ * is the label).
54
+ */
55
+ inlineButtonPattern?: string;
56
+ inlineButtonFlags?: string;
57
+ scope?: 'between-last-two-separators' | 'window-around-question' | 'whole-screen';
58
+ scopeWindowLines?: number;
59
+ contextHeader?: ModalContextHeader;
60
+ continuationLines?: boolean;
61
+ minButtons?: number;
62
+ }
63
+
64
+ // ─── Helpers ───────────────────────────────────────────────────────────
65
+
66
+ const SEPARATOR_RE = /^(?:─|━|═|━){10,}\s*$/;
67
+
68
+ function compile(re: string, flags?: string): RegExp {
69
+ try {
70
+ return new RegExp(re, flags);
71
+ } catch (e) {
72
+ throw new Error(`Invalid regex /${re}/${flags ?? ''}: ${(e as Error).message}`);
73
+ }
74
+ }
75
+
76
+ function findQuestionLineIndex(
77
+ spec: ModalTuiSpec,
78
+ lines: string[],
79
+ ): { index: number; matchedSource: 'primary' | string } | null {
80
+ const primary = compile(spec.questionPattern, spec.questionFlags ?? 'i');
81
+ // Search bottom-up to prefer the most recent modal.
82
+ for (let i = lines.length - 1; i >= 0; i -= 1) {
83
+ if (primary.test(lines[i])) return { index: i, matchedSource: 'primary' };
84
+ }
85
+ for (const variant of spec.questionVariants ?? []) {
86
+ const re = compile(variant.regex, variant.flags ?? 'i');
87
+ for (let i = lines.length - 1; i >= 0; i -= 1) {
88
+ if (re.test(lines[i])) return { index: i, matchedSource: variant.label ?? 'variant' };
89
+ }
90
+ }
91
+ return null;
92
+ }
93
+
94
+ function scopeLines(
95
+ spec: ModalTuiSpec,
96
+ lines: string[],
97
+ questionIndex: number,
98
+ ): { start: number; end: number } {
99
+ const scope = spec.scope ?? 'between-last-two-separators';
100
+ if (scope === 'whole-screen') {
101
+ return { start: 0, end: lines.length };
102
+ }
103
+ if (scope === 'window-around-question') {
104
+ const window = spec.scopeWindowLines ?? 16;
105
+ return {
106
+ start: Math.max(0, questionIndex - 2),
107
+ end: Math.min(lines.length, questionIndex + window),
108
+ };
109
+ }
110
+ // between-last-two-separators
111
+ let lastSep = -1;
112
+ let prevSep = -1;
113
+ for (let i = lines.length - 1; i >= 0; i -= 1) {
114
+ if (SEPARATOR_RE.test(lines[i].trim())) {
115
+ if (lastSep < 0) lastSep = i;
116
+ else if (prevSep < 0) {
117
+ prevSep = i;
118
+ break;
119
+ }
120
+ }
121
+ }
122
+ if (lastSep >= 0 && prevSep >= 0) {
123
+ return { start: prevSep, end: lastSep + 1 };
124
+ }
125
+ // Fallback: window around question line.
126
+ return {
127
+ start: Math.max(0, questionIndex - 4),
128
+ end: Math.min(lines.length, questionIndex + 16),
129
+ };
130
+ }
131
+
132
+ function extractButtons(
133
+ spec: ModalTuiSpec,
134
+ lines: string[],
135
+ windowStart: number,
136
+ windowEnd: number,
137
+ ): string[] {
138
+ const buttonRe = compile(spec.buttonPattern, spec.buttonFlags ?? 'm');
139
+ const out: string[] = [];
140
+ const labelGroup = Number.isInteger(spec.buttonLabelGroup) && (spec.buttonLabelGroup ?? 0) > 0
141
+ ? spec.buttonLabelGroup!
142
+ : 1;
143
+ let i = windowStart;
144
+ while (i < windowEnd) {
145
+ const line = lines[i];
146
+ const m = buttonRe.exec(line);
147
+ const captured = m?.[labelGroup] ?? (labelGroup === 1 && m && m.length > 2 ? m[m.length - 1] : undefined);
148
+ if (m && captured) {
149
+ let label = captured.trim();
150
+ // Continuation lines: when enabled, append indented lines below until
151
+ // the next button or blank.
152
+ if (spec.continuationLines) {
153
+ let j = i + 1;
154
+ while (j < windowEnd) {
155
+ const next = lines[j];
156
+ if (!next.trim()) break;
157
+ if (buttonRe.test(next)) break;
158
+ // continuation must be indented (not at column 0 like a new section).
159
+ if (!/^\s+/.test(next)) break;
160
+ label += ' ' + next.trim();
161
+ j += 1;
162
+ }
163
+ i = j;
164
+ } else {
165
+ i += 1;
166
+ }
167
+ out.push(label);
168
+ } else {
169
+ i += 1;
170
+ }
171
+ }
172
+ return out;
173
+ }
174
+
175
+ function buildMessage(
176
+ spec: ModalTuiSpec,
177
+ lines: string[],
178
+ questionIndex: number,
179
+ windowStart: number,
180
+ windowEnd: number,
181
+ ): string {
182
+ const messageParts: string[] = [];
183
+ if (spec.contextHeader) {
184
+ const ctxRe = compile(spec.contextHeader.regex, (spec.contextHeader.flags ?? 'i') + 'm');
185
+ const haystack = lines.slice(windowStart, windowEnd).join('\n');
186
+ const m = ctxRe.exec(haystack);
187
+ if (m) messageParts.push(m[1] ? m[1].trim() : m[0].trim());
188
+ }
189
+ messageParts.push(lines[questionIndex].trim());
190
+ return messageParts.filter(Boolean).join(' — ');
191
+ }
192
+
193
+ // ─── Public builder ────────────────────────────────────────────────────
194
+
195
+ function extractInlineButtons(
196
+ spec: ModalTuiSpec,
197
+ lines: string[],
198
+ windowStart: number,
199
+ windowEnd: number,
200
+ ): string[] {
201
+ if (!spec.inlineButtonPattern) return [];
202
+ // Always force the `g` flag so we can iterate all matches in the line.
203
+ const declaredFlags = spec.inlineButtonFlags ?? 'gi';
204
+ const flags = declaredFlags.includes('g') ? declaredFlags : declaredFlags + 'g';
205
+ const re = compile(spec.inlineButtonPattern, flags);
206
+ const out: string[] = [];
207
+ for (let i = windowStart; i < windowEnd; i += 1) {
208
+ let m: RegExpExecArray | null;
209
+ re.lastIndex = 0;
210
+ while ((m = re.exec(lines[i])) !== null) {
211
+ const label = (m[1] ?? m[0]).trim();
212
+ if (label && !out.includes(label)) out.push(label);
213
+ if (m.index === re.lastIndex) re.lastIndex += 1; // safety against zero-width matches
214
+ }
215
+ }
216
+ return out;
217
+ }
218
+
219
+ export function buildParseApprovalFromTui(
220
+ spec: ModalTuiSpec,
221
+ visibleRegion?: VisibleRegionSpec,
222
+ ): CliParseApprovalFn {
223
+ const minButtons = spec.minButtons ?? 2;
224
+
225
+ return function parseApproval(input: CliApprovalInput): CliApprovalModal | null {
226
+ const rawText = input.screenText ?? input.buffer ?? '';
227
+ if (!rawText) return null;
228
+ const text = visibleRegion ? applyVisibleRegion(visibleRegion, rawText) : rawText;
229
+ const lines = text.split('\n');
230
+ const question = findQuestionLineIndex(spec, lines);
231
+ if (!question) return null;
232
+ const { start, end } = scopeLines(spec, lines, question.index);
233
+ if (question.index < start || question.index >= end) return null;
234
+ let buttons = extractButtons(spec, lines, question.index + 1, end);
235
+ // Inline fallback: when per-line extraction came up short, the terminal
236
+ // may have rendered every option on the same line ("[0] skip [1] yes …").
237
+ if (buttons.length < minButtons && spec.inlineButtonPattern) {
238
+ buttons = extractInlineButtons(spec, lines, question.index, end);
239
+ }
240
+ if (buttons.length < minButtons) return null;
241
+ const message = buildMessage(spec, lines, question.index, start, end);
242
+ return { message, buttons };
243
+ };
244
+ }
245
+
246
+ export const __internal = {
247
+ findQuestionLineIndex,
248
+ scopeLines,
249
+ extractButtons,
250
+ };