@adhdev/daemon-core 0.9.82-rc.44 → 0.9.82-rc.441

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 (425) hide show
  1. package/dist/boot/daemon-lifecycle.d.ts +9 -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 +68 -2
  8. package/dist/cli-adapters/cli-script-runner.d.ts +117 -0
  9. package/dist/cli-adapters/cli-state-engine.d.ts +200 -0
  10. package/dist/cli-adapters/provider-cli-adapter.d.ts +126 -64
  11. package/dist/cli-adapters/provider-cli-parse.d.ts +5 -0
  12. package/dist/cli-adapters/provider-cli-runtime.d.ts +34 -0
  13. package/dist/cli-adapters/provider-cli-shared.d.ts +74 -0
  14. package/dist/cli-adapters/pty-write-chunking.d.ts +34 -0
  15. package/dist/cli-adapters/raw-terminal-io.d.ts +37 -0
  16. package/dist/cli-adapters/resolve-executable.d.ts +69 -0
  17. package/dist/cli-adapters/terminal-backends/ghostty-vt-backend.d.ts +6 -3
  18. package/dist/cli-adapters/terminal-backends/types.d.ts +10 -2
  19. package/dist/cli-adapters/terminal-screen.d.ts +4 -11
  20. package/dist/commands/chat-commands-debug-bundle.d.ts +14 -0
  21. package/dist/commands/chat-commands-read.d.ts +7 -0
  22. package/dist/commands/chat-commands-scope.d.ts +39 -0
  23. package/dist/commands/chat-commands-shared.d.ts +33 -0
  24. package/dist/commands/chat-commands-write.d.ts +14 -0
  25. package/dist/commands/chat-commands.d.ts +10 -23
  26. package/dist/commands/cli-manager.d.ts +15 -1
  27. package/dist/commands/handler.d.ts +125 -0
  28. package/dist/commands/high-family/index.d.ts +3 -0
  29. package/dist/commands/high-family/mesh-coordinator-launch.d.ts +2 -0
  30. package/dist/commands/high-family/mesh-events.d.ts +2 -0
  31. package/dist/commands/high-family/mesh-status.d.ts +2 -0
  32. package/dist/commands/high-family/types.d.ts +60 -0
  33. package/dist/commands/low-family/coordinator-prompt.d.ts +9 -0
  34. package/dist/commands/low-family/daemon-lifecycle.d.ts +2 -0
  35. package/dist/commands/low-family/diagnostics.d.ts +2 -0
  36. package/dist/commands/low-family/index.d.ts +3 -0
  37. package/dist/commands/low-family/mesh-ledger.d.ts +10 -0
  38. package/dist/commands/low-family/mesh-node-logs.d.ts +2 -0
  39. package/dist/commands/low-family/notification.d.ts +2 -0
  40. package/dist/commands/low-family/refine-config.d.ts +2 -0
  41. package/dist/commands/low-family/session-host.d.ts +2 -0
  42. package/dist/commands/low-family/spec-providerdev.d.ts +11 -0
  43. package/dist/commands/low-family/status-meta.d.ts +2 -0
  44. package/dist/commands/low-family/types.d.ts +33 -0
  45. package/dist/commands/med-family/cli-agent.d.ts +2 -0
  46. package/dist/commands/med-family/fast-forward.d.ts +2 -0
  47. package/dist/commands/med-family/ide.d.ts +10 -0
  48. package/dist/commands/med-family/index.d.ts +3 -0
  49. package/dist/commands/med-family/mesh-crud.d.ts +29 -0
  50. package/dist/commands/med-family/mesh-host-pairing.d.ts +2 -0
  51. package/dist/commands/med-family/mesh-queue.d.ts +2 -0
  52. package/dist/commands/med-family/mesh-restart.d.ts +2 -0
  53. package/dist/commands/med-family/types.d.ts +142 -0
  54. package/dist/commands/mesh-coordinator.d.ts +85 -1
  55. package/dist/commands/router.d.ts +276 -2
  56. package/dist/commands/upgrade-helper.d.ts +41 -1
  57. package/dist/config/chat-history.d.ts +9 -0
  58. package/dist/config/config.d.ts +5 -0
  59. package/dist/config/mesh-config.d.ts +120 -1
  60. package/dist/config/mesh-json-config.d.ts +199 -0
  61. package/dist/config/repo-settings.d.ts +77 -0
  62. package/dist/daemon/dev-server.d.ts +0 -2
  63. package/dist/detection/ide-detector.d.ts +13 -0
  64. package/dist/detection/win32-ide-version.d.ts +37 -0
  65. package/dist/git/change-impact-config.d.ts +159 -0
  66. package/dist/git/git-commands.d.ts +11 -1
  67. package/dist/git/git-diff.d.ts +6 -0
  68. package/dist/git/git-executor.d.ts +11 -0
  69. package/dist/git/git-status.d.ts +57 -0
  70. package/dist/git/git-types.d.ts +2 -50
  71. package/dist/git/git-worktree.d.ts +71 -1
  72. package/dist/git/index.d.ts +3 -1
  73. package/dist/index.d.ts +64 -14
  74. package/dist/index.js +50719 -20120
  75. package/dist/index.js.map +1 -1
  76. package/dist/index.mjs +50317 -19882
  77. package/dist/index.mjs.map +1 -1
  78. package/dist/installer.d.ts +1 -4
  79. package/dist/ipc/local-ipc-server.d.ts +91 -0
  80. package/dist/launch.d.ts +1 -1
  81. package/dist/logging/async-batch-writer.d.ts +10 -0
  82. package/dist/logging/log-redactor.d.ts +24 -0
  83. package/dist/logging/log-tail-reader.d.ts +81 -0
  84. package/dist/logging/logger.d.ts +1 -1
  85. package/dist/mesh/contracts.d.ts +164 -0
  86. package/dist/mesh/coordinator-prompt.d.ts +89 -0
  87. package/dist/mesh/coordinator-registry.d.ts +59 -0
  88. package/dist/mesh/mesh-active-work.d.ts +174 -0
  89. package/dist/mesh/mesh-clone-grace.d.ts +8 -0
  90. package/dist/mesh/mesh-coordinator-config.d.ts +21 -0
  91. package/dist/mesh/mesh-delivery-policy.d.ts +131 -0
  92. package/dist/mesh/mesh-event-classify.d.ts +5 -0
  93. package/dist/mesh/mesh-event-forwarding.d.ts +41 -0
  94. package/dist/mesh/mesh-event-trace.d.ts +21 -0
  95. package/dist/mesh/mesh-events-coordinator.d.ts +4 -0
  96. package/dist/mesh/mesh-events-pending.d.ts +69 -0
  97. package/dist/mesh/mesh-events-stale.d.ts +61 -0
  98. package/dist/mesh/mesh-events-utils.d.ts +75 -0
  99. package/dist/mesh/mesh-events.d.ts +6 -49
  100. package/dist/mesh/mesh-fast-forward.d.ts +81 -0
  101. package/dist/mesh/mesh-host-ownership.d.ts +29 -0
  102. package/dist/mesh/mesh-init.d.ts +128 -0
  103. package/dist/mesh/mesh-ledger-reconciliation.d.ts +23 -4
  104. package/dist/mesh/mesh-ledger.d.ts +77 -1
  105. package/dist/mesh/mesh-magi-status.d.ts +63 -0
  106. package/dist/mesh/mesh-missions.d.ts +189 -0
  107. package/dist/mesh/mesh-node-identity.d.ts +289 -0
  108. package/dist/mesh/mesh-queue-assignment.d.ts +113 -0
  109. package/dist/mesh/mesh-reconcile-loop.d.ts +65 -0
  110. package/dist/mesh/mesh-refine-batch.d.ts +68 -0
  111. package/dist/mesh/mesh-refine-gates.d.ts +428 -0
  112. package/dist/mesh/mesh-refine-status.d.ts +62 -0
  113. package/dist/mesh/mesh-review-inbox.d.ts +90 -0
  114. package/dist/mesh/mesh-routing.d.ts +70 -0
  115. package/dist/mesh/mesh-runtime-store.d.ts +450 -0
  116. package/dist/mesh/mesh-scheduling-runtime.d.ts +78 -0
  117. package/dist/mesh/mesh-task-inflight.d.ts +46 -0
  118. package/dist/mesh/mesh-task-stats.d.ts +49 -0
  119. package/dist/mesh/mesh-unresolved-forward-outbox.d.ts +30 -0
  120. package/dist/mesh/mesh-warmup-deadline.d.ts +68 -0
  121. package/dist/mesh/mesh-work-queue.d.ts +300 -5
  122. package/dist/mesh/preview-freshness.d.ts +18 -0
  123. package/dist/mesh/refine-config.d.ts +216 -0
  124. package/dist/mesh/worktree-bootstrap-config.d.ts +145 -0
  125. package/dist/providers/acp-provider-instance.d.ts +5 -0
  126. package/dist/providers/approval-utils.d.ts +20 -0
  127. package/dist/providers/chat-message-normalization.d.ts +31 -1
  128. package/dist/providers/cli-provider-instance.d.ts +252 -3
  129. package/dist/providers/contracts.d.ts +149 -6
  130. package/dist/providers/external-sources.d.ts +71 -0
  131. package/dist/providers/manual-attendance.d.ts +63 -0
  132. package/dist/providers/native-history/antigravity-cli-transcript.d.ts +100 -0
  133. package/dist/providers/native-history/claude-cli-transcript.d.ts +70 -0
  134. package/dist/providers/native-history/codex-cli-transcript.d.ts +73 -0
  135. package/dist/providers/native-history/constants.d.ts +12 -0
  136. package/dist/providers/native-history/dispatcher.d.ts +27 -0
  137. package/dist/providers/native-history/hermes-cli-transcript.d.ts +30 -0
  138. package/dist/providers/native-history/index.d.ts +13 -0
  139. package/dist/providers/provider-instance-manager.d.ts +29 -0
  140. package/dist/providers/provider-instance.d.ts +23 -1
  141. package/dist/providers/provider-loader.d.ts +26 -4
  142. package/dist/providers/provider-trust.d.ts +31 -0
  143. package/dist/providers/read-chat-contract.d.ts +29 -0
  144. package/dist/providers/sdk/v1/builders/acp/detect-status.d.ts +68 -0
  145. package/dist/providers/sdk/v1/builders/cli/detect-status.d.ts +86 -0
  146. package/dist/providers/sdk/v1/builders/cli/parse-approval-squash.d.ts +59 -0
  147. package/dist/providers/sdk/v1/builders/cli/parse-approval.d.ts +65 -0
  148. package/dist/providers/sdk/v1/builders/cli/parse-session.d.ts +91 -0
  149. package/dist/providers/sdk/v1/builders/cli/visible-region.d.ts +42 -0
  150. package/dist/providers/sdk/v1/fixture-tooling/format.d.ts +126 -0
  151. package/dist/providers/sdk/v1/fixture-tooling/index.d.ts +8 -0
  152. package/dist/providers/sdk/v1/fixture-tooling/replay.d.ts +38 -0
  153. package/dist/providers/sdk/v1/index.d.ts +30 -0
  154. package/dist/providers/sdk/v1/sandbox/README-design.d.ts +193 -0
  155. package/dist/providers/sdk/v1/sandbox/require-whitelist.d.ts +74 -0
  156. package/dist/providers/sdk/v1/sandbox/script-runner.d.ts +98 -0
  157. package/dist/providers/sdk/v1/types/cli/index.d.ts +277 -0
  158. package/dist/providers/sdk/v1/types/common/index.d.ts +203 -0
  159. package/dist/providers/sdk/v1/validators/index.d.ts +5 -0
  160. package/dist/providers/sdk/v1/validators/manifest.d.ts +40 -0
  161. package/dist/providers/sdk/v1/validators/taint.d.ts +52 -0
  162. package/dist/providers/spec/adapter.d.ts +91 -0
  163. package/dist/providers/spec/cli-adapter.d.ts +223 -0
  164. package/dist/providers/spec/evaluator.d.ts +45 -0
  165. package/dist/providers/spec/fsm-driver.d.ts +422 -0
  166. package/dist/providers/spec/fsm-evaluator.d.ts +76 -0
  167. package/dist/providers/spec/fsm-loader.d.ts +14 -0
  168. package/dist/providers/spec/fsm-types.d.ts +174 -0
  169. package/dist/providers/spec/native-history-executor.d.ts +41 -0
  170. package/dist/providers/spec/pre-launch-trust.d.ts +16 -0
  171. package/dist/providers/spec/route.d.ts +4 -0
  172. package/dist/providers/spec/types.d.ts +222 -0
  173. package/dist/providers/status-monitor.d.ts +7 -7
  174. package/dist/providers/transcript-v2.d.ts +176 -0
  175. package/dist/providers/types/interactive-prompt.d.ts +84 -0
  176. package/dist/providers/working-dir.d.ts +17 -0
  177. package/dist/repo-mesh-types.d.ts +509 -13
  178. package/dist/runtime-defaults.d.ts +2 -0
  179. package/dist/sessions/registry.d.ts +6 -0
  180. package/dist/shared-types-extra.d.ts +2 -4
  181. package/dist/shared-types.d.ts +59 -2
  182. package/dist/status/normalize.d.ts +1 -1
  183. package/dist/status/normalize.js +1 -0
  184. package/dist/status/normalize.js.map +1 -1
  185. package/dist/status/normalize.mjs +1 -0
  186. package/dist/status/normalize.mjs.map +1 -1
  187. package/dist/status/reporter.d.ts +2 -0
  188. package/dist/status/snapshot.d.ts +26 -0
  189. package/dist/system/hash.d.ts +8 -0
  190. package/dist/system/load-better-sqlite3.d.ts +21 -0
  191. package/dist/types.d.ts +5 -0
  192. package/package.json +7 -3
  193. package/src/agent-stream/poller.ts +2 -3
  194. package/src/agent-stream/provider-adapter.ts +1 -1
  195. package/src/boot/daemon-lifecycle.ts +63 -12
  196. package/src/boot/process-hardening.ts +89 -0
  197. package/src/build-info.ts +73 -0
  198. package/src/chat/source-machine.ts +534 -0
  199. package/src/chat/source-resolver.ts +0 -0
  200. package/src/chat/subscription-updates.ts +20 -1
  201. package/src/cli-adapter-types.d.ts +3 -1
  202. package/src/cli-adapter-types.ts +68 -2
  203. package/src/cli-adapters/cli-script-runner.ts +421 -0
  204. package/src/cli-adapters/cli-state-engine.ts +1246 -0
  205. package/src/cli-adapters/provider-cli-adapter.d.ts +1 -1
  206. package/src/cli-adapters/provider-cli-adapter.ts +914 -1144
  207. package/src/cli-adapters/provider-cli-parse.d.ts +2 -0
  208. package/src/cli-adapters/provider-cli-parse.ts +19 -0
  209. package/src/cli-adapters/provider-cli-runtime.ts +56 -7
  210. package/src/cli-adapters/provider-cli-shared.d.ts +4 -0
  211. package/src/cli-adapters/provider-cli-shared.ts +121 -11
  212. package/src/cli-adapters/pty-transport.ts +2 -1
  213. package/src/cli-adapters/pty-write-chunking.ts +106 -0
  214. package/src/cli-adapters/raw-terminal-io.ts +252 -0
  215. package/src/cli-adapters/resolve-executable.ts +204 -0
  216. package/src/cli-adapters/session-host-transport.ts +2 -1
  217. package/src/cli-adapters/terminal-backends/ghostty-vt-backend.ts +63 -29
  218. package/src/cli-adapters/terminal-backends/types.ts +10 -3
  219. package/src/cli-adapters/terminal-screen.ts +16 -81
  220. package/src/commands/WINDOWS-UPGRADE-LOCK-FAILURE.md +198 -0
  221. package/src/commands/chat-commands-debug-bundle.ts +398 -0
  222. package/src/commands/chat-commands-read.ts +2447 -0
  223. package/src/commands/chat-commands-scope.ts +54 -0
  224. package/src/commands/chat-commands-shared.ts +114 -0
  225. package/src/commands/chat-commands-write.ts +891 -0
  226. package/src/commands/chat-commands.ts +19 -1841
  227. package/src/commands/cli-manager.ts +572 -27
  228. package/src/commands/handler.ts +841 -2
  229. package/src/commands/high-family/index.ts +28 -0
  230. package/src/commands/high-family/mesh-coordinator-launch.ts +678 -0
  231. package/src/commands/high-family/mesh-events.ts +80 -0
  232. package/src/commands/high-family/mesh-status.ts +714 -0
  233. package/src/commands/high-family/types.ts +76 -0
  234. package/src/commands/low-family/coordinator-prompt.ts +72 -0
  235. package/src/commands/low-family/daemon-lifecycle.ts +107 -0
  236. package/src/commands/low-family/diagnostics.ts +57 -0
  237. package/src/commands/low-family/index.ts +37 -0
  238. package/src/commands/low-family/mesh-ledger.ts +62 -0
  239. package/src/commands/low-family/mesh-node-logs.ts +87 -0
  240. package/src/commands/low-family/notification.ts +116 -0
  241. package/src/commands/low-family/refine-config.ts +106 -0
  242. package/src/commands/low-family/session-host.ts +274 -0
  243. package/src/commands/low-family/spec-providerdev.ts +217 -0
  244. package/src/commands/low-family/status-meta.ts +112 -0
  245. package/src/commands/low-family/types.ts +39 -0
  246. package/src/commands/med-family/cli-agent.ts +238 -0
  247. package/src/commands/med-family/fast-forward.ts +230 -0
  248. package/src/commands/med-family/ide.ts +163 -0
  249. package/src/commands/med-family/index.ts +37 -0
  250. package/src/commands/med-family/mesh-crud.ts +1242 -0
  251. package/src/commands/med-family/mesh-host-pairing.ts +234 -0
  252. package/src/commands/med-family/mesh-queue.ts +167 -0
  253. package/src/commands/med-family/mesh-restart.ts +92 -0
  254. package/src/commands/med-family/types.ts +143 -0
  255. package/src/commands/mesh-coordinator.ts +334 -124
  256. package/src/commands/router.ts +2846 -3484
  257. package/src/commands/stream-commands.ts +8 -0
  258. package/src/commands/upgrade-helper.ts +310 -45
  259. package/src/config/chat-history.ts +493 -24
  260. package/src/config/config.ts +12 -0
  261. package/src/config/mesh-config.ts +593 -22
  262. package/src/config/mesh-json-config.ts +376 -0
  263. package/src/config/recent-activity.ts +8 -2
  264. package/src/config/repo-settings.ts +111 -0
  265. package/src/daemon/dev-auto-implement.ts +3 -2
  266. package/src/daemon/dev-cli-debug.ts +10 -1
  267. package/src/daemon/dev-server.ts +0 -541
  268. package/src/detection/cli-detector.ts +28 -9
  269. package/src/detection/ide-detector.ts +55 -16
  270. package/src/detection/win32-ide-version.ts +106 -0
  271. package/src/git/change-impact-config.ts +354 -0
  272. package/src/git/git-commands.ts +59 -15
  273. package/src/git/git-diff.ts +81 -11
  274. package/src/git/git-executor.ts +12 -0
  275. package/src/git/git-status.ts +767 -48
  276. package/src/git/git-types.ts +14 -62
  277. package/src/git/git-worktree.ts +261 -4
  278. package/src/git/index.ts +17 -0
  279. package/src/index.ts +196 -12
  280. package/src/installer.d.ts +1 -1
  281. package/src/installer.ts +8 -6
  282. package/src/ipc/local-ipc-server.ts +278 -0
  283. package/src/launch.d.ts +1 -1
  284. package/src/launch.ts +37 -28
  285. package/src/logging/async-batch-writer.ts +55 -0
  286. package/src/logging/command-log.ts +7 -5
  287. package/src/logging/log-redactor.ts +100 -0
  288. package/src/logging/log-tail-reader.ts +341 -0
  289. package/src/logging/logger.ts +14 -7
  290. package/src/mesh/contracts.ts +338 -0
  291. package/src/mesh/coordinator-prompt.ts +415 -34
  292. package/src/mesh/coordinator-registry.ts +121 -0
  293. package/src/mesh/mesh-active-work.ts +645 -0
  294. package/src/mesh/mesh-clone-grace.ts +68 -0
  295. package/src/mesh/mesh-coordinator-config.ts +97 -0
  296. package/src/mesh/mesh-delivery-policy.ts +315 -0
  297. package/src/mesh/mesh-event-classify.ts +51 -0
  298. package/src/mesh/mesh-event-forwarding.ts +1897 -0
  299. package/src/mesh/mesh-event-trace.ts +67 -0
  300. package/src/mesh/mesh-events-coordinator.ts +32 -0
  301. package/src/mesh/mesh-events-pending.ts +656 -0
  302. package/src/mesh/mesh-events-stale.ts +389 -0
  303. package/src/mesh/mesh-events-utils.ts +443 -0
  304. package/src/mesh/mesh-events.ts +33 -1035
  305. package/src/mesh/mesh-fast-forward.ts +843 -0
  306. package/src/mesh/mesh-host-ownership.ts +111 -0
  307. package/src/mesh/mesh-init.ts +350 -0
  308. package/src/mesh/mesh-ledger-reconciliation.ts +12 -7
  309. package/src/mesh/mesh-ledger.ts +656 -102
  310. package/src/mesh/mesh-magi-status.ts +223 -0
  311. package/src/mesh/mesh-missions.ts +449 -0
  312. package/src/mesh/mesh-node-identity.ts +1887 -0
  313. package/src/mesh/mesh-queue-assignment.ts +1924 -0
  314. package/src/mesh/mesh-reconcile-loop.ts +1997 -0
  315. package/src/mesh/mesh-refine-batch.ts +205 -0
  316. package/src/mesh/mesh-refine-gates.ts +1673 -0
  317. package/src/mesh/mesh-refine-status.ts +231 -0
  318. package/src/mesh/mesh-review-inbox.ts +307 -0
  319. package/src/mesh/mesh-routing.ts +291 -0
  320. package/src/mesh/mesh-runtime-store.ts +1853 -0
  321. package/src/mesh/mesh-scheduling-runtime.ts +198 -0
  322. package/src/mesh/mesh-task-inflight.ts +70 -0
  323. package/src/mesh/mesh-task-stats.ts +161 -0
  324. package/src/mesh/mesh-unresolved-forward-outbox.ts +185 -0
  325. package/src/mesh/mesh-warmup-deadline.ts +152 -0
  326. package/src/mesh/mesh-work-queue.ts +1254 -141
  327. package/src/mesh/preview-freshness.ts +118 -0
  328. package/src/mesh/refine-config.ts +423 -0
  329. package/src/mesh/worktree-bootstrap-config.ts +450 -0
  330. package/src/providers/acp-provider-instance.ts +43 -10
  331. package/src/providers/approval-utils.d.ts +5 -0
  332. package/src/providers/approval-utils.ts +57 -5
  333. package/src/providers/chat-message-normalization.ts +92 -4
  334. package/src/providers/cli-provider-instance.ts +2001 -106
  335. package/src/providers/contracts.d.ts +55 -0
  336. package/src/providers/contracts.ts +160 -6
  337. package/src/providers/extension-provider-instance.ts +12 -7
  338. package/src/providers/external-sources.ts +218 -0
  339. package/src/providers/ide-provider-instance.ts +35 -12
  340. package/src/providers/manual-attendance.ts +85 -0
  341. package/src/providers/native-history/antigravity-cli-transcript.ts +643 -0
  342. package/src/providers/native-history/claude-cli-transcript.ts +396 -0
  343. package/src/providers/native-history/codex-cli-transcript.ts +479 -0
  344. package/src/providers/native-history/constants.ts +19 -0
  345. package/src/providers/native-history/dispatcher.ts +345 -0
  346. package/src/providers/native-history/hermes-cli-transcript.ts +260 -0
  347. package/src/providers/native-history/index.ts +30 -0
  348. package/src/providers/provider-instance-manager.ts +71 -0
  349. package/src/providers/provider-instance.ts +19 -1
  350. package/src/providers/provider-loader.ts +668 -50
  351. package/src/providers/provider-schema.ts +87 -14
  352. package/src/providers/provider-trust.ts +114 -0
  353. package/src/providers/read-chat-contract.ts +76 -16
  354. package/src/providers/sdk/README.md +49 -0
  355. package/src/providers/sdk/v1/builders/acp/detect-status.ts +144 -0
  356. package/src/providers/sdk/v1/builders/cli/detect-status.ts +313 -0
  357. package/src/providers/sdk/v1/builders/cli/parse-approval-squash.ts +158 -0
  358. package/src/providers/sdk/v1/builders/cli/parse-approval.ts +250 -0
  359. package/src/providers/sdk/v1/builders/cli/parse-session.ts +276 -0
  360. package/src/providers/sdk/v1/builders/cli/visible-region.ts +143 -0
  361. package/src/providers/sdk/v1/fixture-tooling/format.ts +130 -0
  362. package/src/providers/sdk/v1/fixture-tooling/index.ts +22 -0
  363. package/src/providers/sdk/v1/fixture-tooling/replay.ts +352 -0
  364. package/src/providers/sdk/v1/index.ts +152 -0
  365. package/src/providers/sdk/v1/sandbox/README-design.ts +195 -0
  366. package/src/providers/sdk/v1/sandbox/require-whitelist.ts +472 -0
  367. package/src/providers/sdk/v1/sandbox/script-runner.ts +150 -0
  368. package/src/providers/sdk/v1/schemas/cli/provider.schema.json +509 -0
  369. package/src/providers/sdk/v1/schemas/primitives/acp-session-protocol-v1.json +131 -0
  370. package/src/providers/sdk/v1/schemas/primitives/native-history-codex-rollout-v1.json +66 -0
  371. package/src/providers/sdk/v1/schemas/primitives/tui-approval-squash-v1.json +91 -0
  372. package/src/providers/sdk/v1/schemas/primitives/tui-assistant-block-v1.json +91 -0
  373. package/src/providers/sdk/v1/schemas/primitives/tui-cue-ordering-v1.json +47 -0
  374. package/src/providers/sdk/v1/schemas/primitives/tui-dispatch-order-v1.json +32 -0
  375. package/src/providers/sdk/v1/schemas/primitives/tui-footer-chrome-v1.json +42 -0
  376. package/src/providers/sdk/v1/schemas/primitives/tui-index-finder-v1.json +27 -0
  377. package/src/providers/sdk/v1/schemas/primitives/tui-modal-v1.json +125 -0
  378. package/src/providers/sdk/v1/schemas/primitives/tui-prompt-marker-v1.json +45 -0
  379. package/src/providers/sdk/v1/schemas/primitives/tui-session-id-extraction-v1.json +46 -0
  380. package/src/providers/sdk/v1/schemas/primitives/tui-settled-prompt-v1.json +71 -0
  381. package/src/providers/sdk/v1/schemas/primitives/tui-spinner-v1.json +83 -0
  382. package/src/providers/sdk/v1/schemas/primitives/tui-transcript-pty-v1.json +83 -0
  383. package/src/providers/sdk/v1/schemas/primitives/tui-visible-region-v1.json +57 -0
  384. package/src/providers/sdk/v1/schemas/primitives/tui-welcome-screen-v1.json +35 -0
  385. package/src/providers/sdk/v1/types/cli/index.ts +365 -0
  386. package/src/providers/sdk/v1/types/common/index.ts +229 -0
  387. package/src/providers/sdk/v1/validators/index.ts +19 -0
  388. package/src/providers/sdk/v1/validators/manifest.ts +110 -0
  389. package/src/providers/sdk/v1/validators/taint.ts +309 -0
  390. package/src/providers/spec/adapter.ts +235 -0
  391. package/src/providers/spec/cli-adapter.ts +1056 -0
  392. package/src/providers/spec/evaluator.ts +407 -0
  393. package/src/providers/spec/fsm-driver.ts +1410 -0
  394. package/src/providers/spec/fsm-evaluator.ts +272 -0
  395. package/src/providers/spec/fsm-loader.ts +120 -0
  396. package/src/providers/spec/fsm-types.ts +273 -0
  397. package/src/providers/spec/native-history-executor.ts +1143 -0
  398. package/src/providers/spec/pre-launch-trust.ts +104 -0
  399. package/src/providers/spec/route.ts +51 -0
  400. package/src/providers/spec/types.ts +262 -0
  401. package/src/providers/status-monitor.d.ts +7 -7
  402. package/src/providers/status-monitor.ts +37 -22
  403. package/src/providers/transcript-v2.ts +567 -0
  404. package/src/providers/types/interactive-prompt.ts +536 -0
  405. package/src/providers/version-archive.ts +64 -24
  406. package/src/providers/working-dir.ts +23 -0
  407. package/src/repo-mesh-types.ts +748 -14
  408. package/src/runtime-defaults.ts +39 -0
  409. package/src/sessions/registry.ts +6 -0
  410. package/src/shared-types-extra.ts +2 -4
  411. package/src/shared-types.d.ts +8 -0
  412. package/src/shared-types.ts +64 -1
  413. package/src/status/builders.ts +26 -6
  414. package/src/status/normalize.ts +2 -0
  415. package/src/status/reporter.ts +19 -1
  416. package/src/status/snapshot.ts +95 -26
  417. package/src/system/hash.ts +23 -0
  418. package/src/system/host-memory.ts +29 -12
  419. package/src/system/load-better-sqlite3.ts +68 -0
  420. package/src/types.ts +5 -0
  421. package/dist/cli-adapters/terminal-backends/xterm-backend.d.ts +0 -17
  422. package/dist/mesh/mesh-sync.d.ts +0 -53
  423. package/src/cli-adapters/terminal-backends/ghostty-vt-backend.d.ts +0 -16
  424. package/src/cli-adapters/terminal-backends/xterm-backend.ts +0 -97
  425. package/src/mesh/mesh-sync.ts +0 -111
@@ -0,0 +1,1056 @@
1
+ /**
2
+ * SpecCliAdapter — bridges SpecDriver into the daemon's CliAdapter
3
+ * interface so an existing CliProviderInstance can drive a spec-backed
4
+ * provider without rewriting the surrounding session machinery.
5
+ *
6
+ * Translation:
7
+ * spec state.id 'approval' (with modal_buttons that produced a modal)
8
+ * → CliAdapterStatus.status='waiting_approval', activeModal={...}
9
+ * spec state.id 'busy' / any non-decision state with a 'busy' label
10
+ * → status='generating'
11
+ * spec state.id 'idle' or default
12
+ * → status='idle'
13
+ *
14
+ * Methods that the round-3 spec model doesn't have an opinion on
15
+ * (transcript reading, slash commands, history, runtime metadata
16
+ * surfacing) are minimal stubs. They satisfy the daemon's call sites
17
+ * without pretending to implement anything.
18
+ */
19
+ 'use strict';
20
+
21
+ import { FsmDriver, type DashboardEvent, type ISpecDriver } from './fsm-driver.js';
22
+ import { lastContiguousNumberedBlock } from './evaluator.js';
23
+ import { executeNativeHistory } from './native-history-executor.js';
24
+ import * as fs from 'node:fs';
25
+ import type { NativeHistoryConfig, Control, ControlAction } from './types.js';
26
+ import type { CliAdapter, CliAdapterStatus } from '../../cli-adapter-types.js';
27
+ import type { ChatMessage } from '../../types.js';
28
+ import type { PtyTransportFactory } from '../../cli-adapters/pty-transport.js';
29
+ import { LOG } from '../../logging/logger.js';
30
+ import {
31
+ buildClaudeInteractiveTuiAnswerSteps,
32
+ buildClaudeInteractiveToolResult,
33
+ detectClaudeAskUserQuestionPromptFromJson,
34
+ detectClaudeAskUserQuestionPromptFromTuiPages,
35
+ detectClaudeTuiMultiSelect,
36
+ readFocusedClaudeTuiQuestion,
37
+ type ClaudeInteractiveTuiPage,
38
+ type InteractivePrompt,
39
+ type InteractivePromptResponse,
40
+ } from '../types/interactive-prompt.js';
41
+
42
+
43
+ function stripAnsi(text: string): string {
44
+ // eslint-disable-next-line no-control-regex
45
+ return String(text || '')
46
+ .replace(/\x1B\][^\x07]*(?:\x07|\x1B\\)/g, '')
47
+ .replace(/\x1B[P^_X][\s\S]*?(?:\x07|\x1B\\)/g, '')
48
+ .replace(/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g, '');
49
+ }
50
+
51
+ function delay(ms: number): Promise<void> {
52
+ return new Promise(resolve => setTimeout(resolve, ms));
53
+ }
54
+
55
+ export class SpecCliAdapter implements CliAdapter {
56
+ readonly cliType: string;
57
+ readonly cliName: string;
58
+ readonly workingDir: string;
59
+ /**
60
+ * Marker the daemon's finalization gate checks: `getStatus()` returns
61
+ * `messages: []` by design here (chat history lives in the daemon's
62
+ * native-history pipeline, not the adapter). Without this flag,
63
+ * cli-provider-instance's `missing_final_assistant` gate would stall
64
+ * every turn until the 30s safety timeout because it expects the
65
+ * adapter to surface the final assistant message.
66
+ */
67
+ readonly chatMessagesOwnedExternally = true as const;
68
+
69
+ private driver: ISpecDriver;
70
+ /** Common spec fields the adapter reads, present in both v3 and v4. */
71
+ private spec: {
72
+ id: string;
73
+ name: string;
74
+ control_bar?: Control[];
75
+ native_history?: NativeHistoryConfig;
76
+ };
77
+ private lastEvent: DashboardEvent | null = null;
78
+ private latestState: { id: string; label: string; title: string | null; status: 'idle' | 'generating' | 'approval' } | null = null;
79
+ private latestModal: { title: string | null; buttons: { index: number; label: string }[]; kind?: 'approval' | 'picker' | 'confirm' | null } | null = null;
80
+ private statusCallback: (() => void) | null = null;
81
+ private ptyDataCallback: ((data: string) => void) | null = null;
82
+ private partialResponse = '';
83
+ private activeInteractivePrompt: InteractivePrompt | null = null;
84
+ private interactivePromptTransport: 'stream-json' | 'tui' | null = null;
85
+ private claudeTuiPromptCaptureInFlight = false;
86
+ /**
87
+ * Wall clock of the first frame on which a held interactive prompt was
88
+ * observed to have left the screen. Mirrors the approval FSM's
89
+ * `modalLostAt` hysteresis (see cli-state-engine.ts): claude-cli's TUI
90
+ * repaints the choice picker as several PTY chunks, so a single frame
91
+ * with no "Enter to select" footer is not proof the prompt is gone — it
92
+ * may just be mid-repaint. We only clear the held prompt once it has
93
+ * been absent across a short grace window. Reset to null the moment the
94
+ * prompt footer reappears.
95
+ *
96
+ * Without this, a choice prompt resolved *directly in the terminal* (the
97
+ * user picked an option without going through ADHDev's
98
+ * setInteractivePromptResponse) was never cleared from
99
+ * `activeInteractivePrompt`, so getStatus() re-emitted the same prompt
100
+ * forever — the choice-resolve-stuck bug.
101
+ */
102
+ private interactivePromptLostAt: number | null = null;
103
+ private jsonLineTail = '';
104
+ private exited = false;
105
+ private spawned = false;
106
+ private providerSessionId: string | undefined;
107
+ /** Wall clock at the moment spawn() ran. Used as the cutoff for
108
+ * native-history file selection so a prior session's transcript
109
+ * can't leak into this session before the agent has written its
110
+ * own records. */
111
+ private spawnedAtMs = 0;
112
+ /** Env vars the daemon set on the spawned child. Mesh coordinator
113
+ * points hermes at a per-coordinator HERMES_HOME so the dashboard's
114
+ * native-history reader needs that override to find the right
115
+ * state.db; without it the reader sees ~/.hermes/state.db which
116
+ * the coordinator-launched hermes never writes to. The choice to
117
+ * redirect HERMES_HOME is a workaround for an unresolved hermes
118
+ * upstream feature gap (see hermes-agent#23130 — runtime-supplied
119
+ * MCP config), so this routing keeps the dashboard honest until
120
+ * hermes ships a runtime MCP override. */
121
+ private spawnedEnv: Record<string, string> = {};
122
+
123
+ constructor(
124
+ specPath: string,
125
+ workingDir: string,
126
+ cliArgs: string[],
127
+ extraEnv: Record<string, string>,
128
+ transportFactory?: PtyTransportFactory,
129
+ ) {
130
+ const raw = JSON.parse(fs.readFileSync(specPath, 'utf8'));
131
+ this.spec = {
132
+ id: raw.id,
133
+ name: raw.name,
134
+ control_bar: raw.control_bar,
135
+ native_history: raw.native_history,
136
+ };
137
+ this.cliType = this.spec.id;
138
+ this.cliName = this.spec.name;
139
+ this.workingDir = workingDir;
140
+ this.spawnedEnv = { ...extraEnv };
141
+
142
+ // cli-manager.ts allocates providerSessionId per launch and threads
143
+ // it through resume.newSessionArgs as additional cliArgs (e.g.
144
+ // ["--session-id", "<uuid>"]). We must hand those to the driver
145
+ // so the agent uses the daemon's id, otherwise (claude case) the
146
+ // agent generates its own id and the chat-history pipeline can't
147
+ // pair the on-disk transcript with the live session.
148
+ this.driver = new FsmDriver({
149
+ specPath,
150
+ workingDir,
151
+ extraEnv,
152
+ hotReload: true,
153
+ emitTrace: false,
154
+ transportFactory,
155
+ extraCliArgs: cliArgs,
156
+ });
157
+ this.driver.subscribe((ev) => this.handleEvent(ev));
158
+ }
159
+
160
+ async spawn(): Promise<void> {
161
+ if (this.spawned) return;
162
+ this.driver.start();
163
+ this.spawned = true;
164
+ this.spawnedAtMs = Date.now();
165
+ }
166
+
167
+ async sendMessage(text: string): Promise<void> {
168
+ // Content-free at info — the prompt body is user data.
169
+ LOG.info('SpecAdapter', `[${this.cliType}] sendMessage(len=${text.length})`);
170
+ LOG.debug('SpecAdapter', `[${this.cliType}] sendMessage body=${JSON.stringify(text.slice(0, 80))}${text.length > 80 ? '…' : ''}`);
171
+ this.driver.dispatch({ kind: 'send_message', text });
172
+ }
173
+
174
+ getStatus(): CliAdapterStatus {
175
+ const sessionFields = this.providerSessionId ? { providerSessionId: this.providerSessionId } : {};
176
+ if (this.exited) return { status: 'stopped', messages: [], activeModal: null, activeInteractivePrompt: this.activeInteractivePrompt, ...sessionFields };
177
+ if (!this.spawned) return { status: 'starting', messages: [], activeModal: null, activeInteractivePrompt: this.activeInteractivePrompt, ...sessionFields };
178
+
179
+ // Refresh native history lazily — the watch_path is cheap to stat,
180
+ // but parsing a full session.jsonl every call would be wasteful.
181
+ this.maybeRefreshNativeHistory();
182
+
183
+ const state = this.latestState;
184
+ if (!state) return { status: 'starting', messages: [], activeModal: null, activeInteractivePrompt: this.activeInteractivePrompt, ...sessionFields };
185
+
186
+ // The FSM state is authoritative for status. We do NOT infer status from whether
187
+ // a modal was parsed this frame: a modal/approval state whose buttons briefly fail
188
+ // to parse (PTY repaint) must still report waiting_approval, not collapse to idle —
189
+ // that collapse fired false completions while a session sat at an approval prompt.
190
+ const modal = this.latestModal;
191
+ if (state.status === 'approval') {
192
+ return {
193
+ status: 'waiting_approval',
194
+ messages: [],
195
+ // Surface buttons when we have them; an approval state with no parsed
196
+ // modal this frame still stays waiting_approval (no activeModal yet).
197
+ // `kind` carries the semantic modal class through to the auto-approve
198
+ // gate so a /model picker (kind='picker') is never auto-answered.
199
+ activeModal: modal
200
+ ? { message: modal.title ?? state.label, buttons: modal.buttons.map(b => b.label), kind: modal.kind ?? null }
201
+ : null,
202
+ activeInteractivePrompt: this.activeInteractivePrompt,
203
+ ...sessionFields,
204
+ };
205
+ }
206
+ if (state.status === 'generating') {
207
+ return { status: 'generating', messages: [], activeModal: null, activeInteractivePrompt: this.activeInteractivePrompt, ...sessionFields };
208
+ }
209
+ // fsmReadySeen lets CliProviderInstance re-arm the queue-claim agent:ready
210
+ // on the first genuine ready (prompt drawn), independent of the boot-time
211
+ // starting→idle one-shot that the provider-instance otherwise relies on.
212
+ // Surfaced only on idle so the provider-instance fires agent:ready exactly
213
+ // when the worker is actually ready to claim.
214
+ return { status: 'idle', messages: [], activeModal: null, activeInteractivePrompt: this.activeInteractivePrompt, fsmReadySeen: this.driver.hasSeenReady?.() ?? false, ...sessionFields };
215
+ }
216
+
217
+ private maybeRefreshNativeHistory(): void {
218
+ // Native history is now sourced by daemon's chat-history pipeline
219
+ // (which calls provider.scripts.readNativeHistory wired by
220
+ // provider-loader). SpecCliAdapter no longer polls or caches.
221
+ }
222
+
223
+ getScriptParsedStatus(): unknown {
224
+ const providerSessionId = this.extractProviderSessionIdFromScreen();
225
+ if (providerSessionId) this.providerSessionId = providerSessionId;
226
+ const status = this.getStatus();
227
+ return {
228
+ ...status,
229
+ messages: this.readClaudeScreenAssistantMessages(),
230
+ ...(this.providerSessionId ? { providerSessionId: this.providerSessionId } : {}),
231
+ };
232
+ }
233
+
234
+ getPartialResponse(): string {
235
+ return this.partialResponse;
236
+ }
237
+
238
+ shutdown(): void {
239
+ try { this.driver.dispatch({ kind: 'shutdown' }); } catch { /* ignore */ }
240
+ }
241
+
242
+ cancel(): void {
243
+ try { this.driver.dispatch({ kind: 'cancel' }); } catch { /* ignore */ }
244
+ }
245
+
246
+ isProcessing(): boolean {
247
+ return this.getStatus().status === 'generating';
248
+ }
249
+
250
+ isReady(): boolean {
251
+ return this.spawned && !this.exited;
252
+ }
253
+
254
+ setOnStatusChange(cb: () => void): void {
255
+ this.statusCallback = cb;
256
+ }
257
+
258
+ setOnPtyData(cb: (data: string) => void): void {
259
+ this.ptyDataCallback = cb;
260
+ }
261
+
262
+ writeRaw(data: string): void {
263
+ // Raw pty input — typed characters, escape codes, arrow keys —
264
+ // goes straight to the underlying terminal. send_message would
265
+ // append submit_key after every chunk, which is why typing in
266
+ // the dashboard terminal felt like "enter on every keystroke".
267
+ this.driver.dispatch({ kind: 'pty_write', data });
268
+ }
269
+
270
+ resize(cols: number, rows: number): void {
271
+ this.driver.dispatch({ kind: 'resize', cols, rows });
272
+ }
273
+
274
+ resolveModal(buttonIndex: number): void {
275
+ // CliAdapter buttonIndex is 0-based; spec buttons are 1-based.
276
+ this.driver.dispatch({ kind: 'click_modal_button', index: buttonIndex + 1 });
277
+ }
278
+
279
+ async resolveAction(data: unknown): Promise<void> {
280
+ const args = (data && typeof data === 'object') ? (data as any) : {};
281
+ const explicitIndex = typeof args.buttonIndex === 'number' ? args.buttonIndex : -1;
282
+ if (explicitIndex >= 0) { this.resolveModal(explicitIndex); return; }
283
+ const action = typeof args.action === 'string' ? args.action : 'approve';
284
+ const buttons = this.latestModal?.buttons ?? [];
285
+ if (buttons.length === 0) return;
286
+ let target = -1;
287
+ if (action === 'reject' || action === 'deny') {
288
+ target = buttons.findIndex(b => /^(no|deny|reject|cancel)\b/i.test(b.label));
289
+ if (target < 0) target = buttons.length - 1;
290
+ } else {
291
+ target = buttons.findIndex(b => /^(yes|allow|approve|accept|continue|proceed|update)\b/i.test(b.label));
292
+ if (target < 0) target = 0;
293
+ }
294
+ this.resolveModal(target);
295
+ }
296
+
297
+ async setInteractivePromptResponse(response: InteractivePromptResponse): Promise<void> {
298
+ const prompt = this.activeInteractivePrompt;
299
+ if (!prompt || prompt.promptId !== response.promptId) throw new Error('Interactive prompt response does not match active prompt');
300
+ if (this.cliType !== 'claude-cli') return;
301
+ if (this.interactivePromptTransport === 'tui') {
302
+ const steps = buildClaudeInteractiveTuiAnswerSteps(prompt, response);
303
+ for (const step of steps) {
304
+ this.driver.dispatch({ kind: 'pty_write', data: step });
305
+ await new Promise(resolve => setTimeout(resolve, 180));
306
+ }
307
+ } else {
308
+ this.driver.dispatch({ kind: 'pty_write', data: `${buildClaudeInteractiveToolResult(response)}\n` });
309
+ }
310
+ this.activeInteractivePrompt = null;
311
+ this.interactivePromptTransport = null;
312
+ this.statusCallback?.();
313
+ }
314
+
315
+ isApprovalRecentlyResolved(): boolean { return false; }
316
+ clearHistory(): void { /* no transcript buffer yet */ }
317
+ updateRuntimeSettings(): void { /* no runtime settings in spec model yet */ }
318
+ setServerConn(): void { /* server conn unused by SpecDriver */ }
319
+ /**
320
+ * Map an invokeScript(name, args) call onto a control_bar entry.
321
+ *
322
+ * scriptName is matched against control.id. The control's action.type
323
+ * drives the dispatch:
324
+ *
325
+ * send_keys → click_control (e.g. stop)
326
+ * open_picker → two roles, driven by the screen, not a hardcoded list:
327
+ * - LIST (no choice arg): open the picker, wait for it
328
+ * to render, parse the on-screen options via
329
+ * `extract_choices`, and return them as
330
+ * `controlResult.options` (+ `currentValue`). This is
331
+ * how the dashboard's Model/Mode controls learn what is
332
+ * actually selectable in this CLI right now.
333
+ * - SELECT (args.choiceIndex / args.choiceLabel): drive
334
+ * the picker to that option using `submit_key`.
335
+ * attach_image → attach_image dispatch; expects args.blob (data url
336
+ * or base64) and args.mime
337
+ *
338
+ * Callers that pass an unknown control id get a { not_found } response.
339
+ * No control matched, no driver call — keeps the surface honest.
340
+ */
341
+ invokeScript(scriptName: string, args?: Record<string, unknown>): Promise<unknown> {
342
+ const controls = this.spec.control_bar ?? [];
343
+ const ctl = controls.find(c => c.id === scriptName);
344
+ if (!ctl) {
345
+ return Promise.resolve({ ok: false, error: `unknown control: ${scriptName}` });
346
+ }
347
+ // Args may arrive as either { blob, mime } (direct invocation) or
348
+ // { params: { blob, mime } } (when the dashboard wraps script args
349
+ // in a params bag). Look at both.
350
+ const flat: Record<string, unknown> = { ...(args || {}) };
351
+ if (args && typeof args.params === 'object' && args.params) {
352
+ Object.assign(flat, args.params as Record<string, unknown>);
353
+ }
354
+ const action = ctl.action;
355
+ if (action.type === 'attach_image') {
356
+ const blob = typeof flat.blob === 'string' ? flat.blob : '';
357
+ const mime = typeof flat.mime === 'string' ? flat.mime : 'image/png';
358
+ if (!blob) return Promise.resolve({ ok: false, error: 'attach_image requires args.blob (base64 or data URL)' });
359
+ this.driver.dispatch({ kind: 'attach_image', blob, mime });
360
+ return Promise.resolve({ ok: true, effects: [{ type: 'attached_image', controlId: ctl.id }] });
361
+ }
362
+ if (action.type === 'open_picker') {
363
+ const choiceIndex = typeof flat.choiceIndex === 'number' ? flat.choiceIndex
364
+ : typeof flat.choiceIndex === 'string' && flat.choiceIndex.trim() ? Number(flat.choiceIndex)
365
+ : undefined;
366
+ // `value` is the arg the dashboard's generic value-control set path
367
+ // sends ({ value: <chosen option> }). control_bar pickers are
368
+ // surfaced to the dashboard as dynamic `select` controls whose
369
+ // option values are the screen-parsed labels, so a bare `value`
370
+ // is just a label to match against the live choices.
371
+ const choiceLabel = typeof flat.choiceLabel === 'string' ? flat.choiceLabel
372
+ : typeof flat.choice === 'string' ? flat.choice
373
+ : typeof flat.value === 'string' ? flat.value
374
+ : undefined;
375
+ if ((typeof choiceIndex === 'number' && Number.isFinite(choiceIndex)) || (choiceLabel && choiceLabel.trim())) {
376
+ return this.selectPickerChoice(ctl, action, choiceIndex, choiceLabel);
377
+ }
378
+ return this.openPickerAndListChoices(ctl, action);
379
+ }
380
+ // send_keys routes through click_control.
381
+ this.driver.dispatch({ kind: 'click_control', control_id: ctl.id, payload: flat });
382
+ return Promise.resolve({ ok: true, effects: [{ type: 'sent_keys', controlId: ctl.id }] });
383
+ }
384
+
385
+ /**
386
+ * Open an `open_picker` control and return the options the CLI is showing,
387
+ * parsed live from the screen via `extract_choices`. Nothing is selected —
388
+ * the picker is left open so a follow-up SELECT invoke can commit a choice.
389
+ */
390
+ private async openPickerAndListChoices(
391
+ ctl: Control,
392
+ action: Extract<ControlAction, { type: 'open_picker' }>,
393
+ ): Promise<unknown> {
394
+ this.driver.dispatch({ kind: 'click_control', control_id: ctl.id });
395
+ const ready = await this.waitForPickerRendered(action);
396
+ const options = this.extractPickerChoices(action);
397
+ const currentValue = options.find(o => o.current)?.label;
398
+ return {
399
+ ok: true,
400
+ effects: [{ type: 'opened_picker', controlId: ctl.id }],
401
+ controlResult: {
402
+ options: options.map(o => ({ value: o.label, label: o.label, current: o.current })),
403
+ ...(currentValue ? { currentValue } : {}),
404
+ source: 'screen-parse',
405
+ ...(ready ? {} : { warning: 'picker_render_timeout' }),
406
+ },
407
+ };
408
+ }
409
+
410
+ /**
411
+ * Drive an already-listable picker to a specific option. The option can be
412
+ * named (choiceLabel — matched against the parsed on-screen labels) or
413
+ * positional (choiceIndex — the on-screen number). The actual keystrokes
414
+ * come from the spec's `submit_key` with `{index}` substituted, so the spec
415
+ * — not this code — decides how a selection is keyed for each CLI.
416
+ */
417
+ private async selectPickerChoice(
418
+ ctl: Control,
419
+ action: Extract<ControlAction, { type: 'open_picker' }>,
420
+ choiceIndex: number | undefined,
421
+ choiceLabel: string | undefined,
422
+ ): Promise<unknown> {
423
+ // Open + wait so the choice list is on screen before we resolve the
424
+ // label → index mapping. The picker is normally ALREADY open here (a
425
+ // preceding list invoke leaves it rendered), so only send the trigger
426
+ // when it is not on screen. Re-sending the trigger to an open picker is
427
+ // NOT a harmless no-op on claude-cli: the trailing CR of `/model\r`
428
+ // lands as Enter on the cursor's current row and commits the wrong
429
+ // model before we navigate. De-dup the open to avoid that.
430
+ let options = this.extractPickerChoicesIfRendered(action);
431
+ if (!options) {
432
+ this.driver.dispatch({ kind: 'click_control', control_id: ctl.id });
433
+ await this.waitForPickerRendered(action);
434
+ options = this.extractPickerChoices(action);
435
+ }
436
+
437
+ let index = choiceIndex;
438
+ if ((index == null || !Number.isFinite(index)) && choiceLabel) {
439
+ const needle = choiceLabel.trim().toLowerCase();
440
+ const match = options.find(o => o.label.toLowerCase().includes(needle));
441
+ if (!match) {
442
+ return { ok: false, error: `choice not found on screen: ${choiceLabel}`, controlResult: { options: options.map(o => ({ value: o.label, label: o.label })) } };
443
+ }
444
+ index = match.index;
445
+ }
446
+ if (index == null || !Number.isFinite(index)) {
447
+ return { ok: false, error: 'choiceIndex or choiceLabel required to select' };
448
+ }
449
+
450
+ if (action.select_mode === 'arrow_keys') {
451
+ // Cursor-list picker (claude-cli /model): number keys are ignored.
452
+ // The cursor starts on the active row (extract flags it `current`);
453
+ // step it to the target row with arrows, then confirm.
454
+ const current = options.find(o => o.current);
455
+ if (current == null) {
456
+ // Without a known cursor position a blind Enter would commit
457
+ // whatever row the cursor sits on — fail loud instead.
458
+ return {
459
+ ok: false,
460
+ error: 'arrow-nav picker: current cursor row not detected on screen',
461
+ controlResult: { options: options.map(o => ({ value: o.label, label: o.label, current: o.current })) },
462
+ };
463
+ }
464
+ const up = action.cursor_keys?.up ?? '';
465
+ const down = action.cursor_keys?.down ?? '';
466
+ const delta = index - current.index;
467
+ const step = delta >= 0 ? down : up;
468
+ const nav = step.repeat(Math.abs(delta));
469
+ // Confirm key = submit_key with the (unused) {index} placeholder
470
+ // stripped — e.g. `{index}\r` → `\r`.
471
+ const confirm = (action.submit_key || '\r').replace(/\{index\}/g, '') || '\r';
472
+ if (nav) this.driver.dispatch({ kind: 'pty_write', data: nav });
473
+ this.driver.dispatch({ kind: 'pty_write', data: confirm });
474
+ } else {
475
+ const keys = (action.submit_key || '{index}\r').replace(/\{index\}/g, String(index));
476
+ this.driver.dispatch({ kind: 'pty_write', data: keys });
477
+ }
478
+ const selected = options.find(o => o.index === index);
479
+ return {
480
+ ok: true,
481
+ effects: [{ type: 'selected_choice', controlId: ctl.id }],
482
+ controlResult: {
483
+ ok: true,
484
+ ...(selected ? { currentValue: selected.label } : {}),
485
+ selectedIndex: index,
486
+ },
487
+ };
488
+ }
489
+
490
+ /** Parse the picker choices only if the picker already appears rendered on
491
+ * the live screen (its `wait_for` condition currently matches and at least
492
+ * one choice parses). Returns the parsed choices when open, else null so
493
+ * the caller knows it must send the trigger to open it. Used to de-dup the
494
+ * picker open in {@link selectPickerChoice}. */
495
+ private extractPickerChoicesIfRendered(
496
+ action: Extract<ControlAction, { type: 'open_picker' }>,
497
+ ): Array<{ index: number; label: string; current: boolean }> | null {
498
+ const wf = action.wait_for;
499
+ if (wf?.regex) {
500
+ const re = new RegExp(wf.regex, wf.flags ?? 'i');
501
+ if (!re.test(this.readScreenSectionText(wf.section))) return null;
502
+ }
503
+ const options = this.extractPickerChoices(action);
504
+ return options.length > 0 ? options : null;
505
+ }
506
+
507
+ /** Poll the live screen until the picker's `wait_for` condition matches,
508
+ * up to a short budget. Returns true if it rendered, false on timeout. */
509
+ private async waitForPickerRendered(action: Extract<ControlAction, { type: 'open_picker' }>): Promise<boolean> {
510
+ const wf = action.wait_for;
511
+ if (!wf?.regex) { await delay(250); return true; }
512
+ const re = new RegExp(wf.regex, wf.flags ?? 'i');
513
+ const deadline = Date.now() + 2500;
514
+ while (Date.now() < deadline) {
515
+ await delay(120);
516
+ const hay = this.readScreenSectionText(wf.section);
517
+ if (re.test(hay)) return true;
518
+ }
519
+ return false;
520
+ }
521
+
522
+ /** Parse the picker's `extract_choices` pattern against the live screen.
523
+ * Each match yields { index, label, current }. `current` is true for the
524
+ * line the CLI marks with its cursor glyph (❯ ›). Purely screen-driven —
525
+ * no model/mode names are baked in. */
526
+ private extractPickerChoices(action: Extract<ControlAction, { type: 'open_picker' }>): Array<{ index: number; label: string; current: boolean }> {
527
+ const ec = action.extract_choices;
528
+ if (!ec?.pattern) return [];
529
+ const text = this.readScreenSectionText(ec.section);
530
+ // Collect EVERY matching line in screen order with no top-down de-dup.
531
+ // The picker section can include conversation history above it (a stray
532
+ // "1./2./3." list, blockquote `>` lines); a `seen.has(idx)` first-wins
533
+ // scan would let those body lines claim the option indices and shadow
534
+ // the real choices — committing the wrong model under arrow-key nav.
535
+ const all: Array<{ index: number; label: string; current: boolean }> = [];
536
+ for (const rawLine of text.split('\n')) {
537
+ const line = rawLine.replace(/\r$/, '');
538
+ const m = new RegExp(ec.pattern, ec.flags ?? '').exec(line);
539
+ if (!m) continue;
540
+ const idx = Number(m[1]);
541
+ if (!Number.isFinite(idx) || idx <= 0) continue;
542
+ const label = (m[2] ?? '').replace(/\s+/g, ' ').trim();
543
+ if (!label) continue;
544
+ const current = /^\s*[❯›>]/.test(line) || /[✔✓●]\s*$/.test(label);
545
+ all.push({ index: idx, label, current });
546
+ }
547
+ // Real options are the bottom-most contiguous numbered block; this also
548
+ // confines the `current` cursor flag to that block so a body `>` line is
549
+ // never mistaken for the cursor row.
550
+ return lastContiguousNumberedBlock(all);
551
+ }
552
+
553
+ /** Live text of a named screen section (or the whole screen when no
554
+ * section is named), resolved from the driver's current sections. */
555
+ private readScreenSectionText(sectionId?: string): string {
556
+ try {
557
+ const sections = this.driver.getSections();
558
+ if (sectionId && sections) {
559
+ const hit = sections.find(s => s.id === sectionId);
560
+ if (hit) return hit.text;
561
+ }
562
+ return this.driver.getScreen();
563
+ } catch {
564
+ return '';
565
+ }
566
+ }
567
+ getDebugSnapshot(): unknown {
568
+ let screen = '';
569
+ let sections: Record<string, string> | undefined;
570
+ try {
571
+ screen = this.driver.snapshot();
572
+ const driverSections = this.driver.getSections?.();
573
+ if (driverSections) {
574
+ sections = Object.fromEntries(driverSections.map(s => [s.id, s.text]));
575
+ } else {
576
+ sections = this.readCurrentScreenSections(screen);
577
+ }
578
+ } catch { /* best-effort */ }
579
+ // Read native transcript messages for the debug snapshot
580
+ let messages: any[] = [];
581
+ if (this.spec.native_history?.source) {
582
+ try {
583
+ const nhResult = executeNativeHistory(this.spec.native_history, {
584
+ agentType: this.cliType,
585
+ providerSessionId: this.providerSessionId,
586
+ sessionStartedAtMs: this.spawnedAtMs,
587
+ envOverrides: this.spawnedEnv,
588
+ workspace: this.workingDir,
589
+ });
590
+ if (nhResult && Array.isArray(nhResult.messages)) messages = nhResult.messages;
591
+ } catch { /* best-effort */ }
592
+ } else {
593
+ messages = this.readClaudeScreenAssistantMessages();
594
+ }
595
+ return {
596
+ cliType: this.cliType,
597
+ spec_id: this.spec.id,
598
+ current_state: this.latestState,
599
+ current_modal: this.latestModal,
600
+ activeInteractivePrompt: this.activeInteractivePrompt,
601
+ exited: this.exited,
602
+ screen,
603
+ sections,
604
+ stateHistory: this.driver.getStateHistory(),
605
+ idleHoldPending: this.driver.hasIdleHoldPending(),
606
+ lastBusyAt: this.driver.getLastBusyAt(),
607
+ specPath: this.driver.getSpecPath(),
608
+ cursorPosition: this.driver.getCursorPosition(),
609
+ completionIdleDebounce: this.driver.getCompletionIdleDebounceState(),
610
+ // v4 FSM live transition table (null for v3 specs). Every outgoing
611
+ // transition from the current state with its per-condition match
612
+ // result + countdown — the canonical "why isn't it moving" answer.
613
+ fsm: this.driver.getFsmDebug?.() ?? null,
614
+ // v4 FSM transition snapshot history (null for v3 specs). The full
615
+ // pre-transition evaluation table captured at each transition —
616
+ // answers "why did this rule fire" after the fact, unlike the live
617
+ // `fsm` field which only reflects the current instant.
618
+ fsmHistory: this.driver.getFsmSnapshotHistory?.() ?? null,
619
+ // PTY input/output/resize/cursor event timeline (debug-only) so the
620
+ // snapshot shows what we typed / what the PTY printed around each
621
+ // status transition. Null for drivers without the timeline.
622
+ eventTimeline: this.driver.getEventTimeline?.() ?? null,
623
+ // Extended fields
624
+ name: this.cliName,
625
+ status: this.getStatus().status,
626
+ workingDir: this.workingDir,
627
+ spawnedAtMs: this.spawnedAtMs,
628
+ providerSessionId: this.providerSessionId ?? null,
629
+ messages,
630
+ committedMessages: messages,
631
+ };
632
+ }
633
+ getRuntimeMetadata(): unknown {
634
+ return {
635
+ runtimeId: this.spec.id,
636
+ runtimeKey: this.spec.id,
637
+ displayName: this.spec.name,
638
+ spawnedAtMs: this.spawnedAtMs,
639
+ spawnedEnv: this.spawnedEnv,
640
+ ...(this.providerSessionId ? { providerSessionId: this.providerSessionId } : {}),
641
+ };
642
+ }
643
+ updateRuntimeMeta(meta?: Record<string, unknown>): void {
644
+ if (meta && typeof meta.providerSessionId === 'string') {
645
+ this.providerSessionId = meta.providerSessionId;
646
+ }
647
+ }
648
+ refreshProviderDefinition(): void { /* hot reload handled by SpecDriver fs.watch */ }
649
+
650
+ private handleEvent(ev: DashboardEvent): void {
651
+ this.lastEvent = ev;
652
+ switch (ev.kind) {
653
+ case 'state_changed':
654
+ this.latestState = ev.state;
655
+ this.latestModal = ev.modal;
656
+ // info-level keeps only spec-defined identifiers (state.id /
657
+ // state.label / button count). The extracted title can carry
658
+ // user data — file paths, command text, ticket titles — so
659
+ // it stays at debug.
660
+ LOG.info('SpecAdapter', `[${this.cliType}] state=${ev.state.id} (${ev.state.label}) modal=${ev.modal ? `${ev.modal.buttons.length}-buttons` : 'none'}`);
661
+ if (ev.state.title) {
662
+ LOG.debug('SpecAdapter', `[${this.cliType}] state.title=${JSON.stringify(ev.state.title)}`);
663
+ }
664
+ this.maybeClearResolvedClaudeTuiPrompt();
665
+ this.maybeCaptureClaudeTuiPrompt();
666
+ this.maybeUpgradeClaudeTuiMultiSelect();
667
+ this.statusCallback?.();
668
+ return;
669
+ case 'pty_data':
670
+ this.detectInteractivePromptFromPtyChunk(ev.chunk);
671
+ this.maybeClearResolvedClaudeTuiPrompt();
672
+ this.maybeCaptureClaudeTuiPrompt();
673
+ this.maybeUpgradeClaudeTuiMultiSelect();
674
+ try { this.ptyDataCallback?.(ev.chunk); } catch { /* ignore */ }
675
+ return;
676
+ case 'exit':
677
+ this.exited = true;
678
+ this.statusCallback?.();
679
+ return;
680
+ case 'spec_error':
681
+ LOG.warn('SpecAdapter', `[${this.cliType}] spec reload error: ${ev.errors.join('; ')}`);
682
+ return;
683
+ default:
684
+ return;
685
+ }
686
+ }
687
+
688
+ private detectInteractivePromptFromPtyChunk(chunk: string): void {
689
+ if (this.cliType !== 'claude-cli' || !chunk) return;
690
+ this.jsonLineTail += chunk;
691
+ if (this.jsonLineTail.length > 64 * 1024) this.jsonLineTail = this.jsonLineTail.slice(-64 * 1024);
692
+ const lines = this.jsonLineTail.split(/\r?\n/);
693
+ this.jsonLineTail = lines.pop() || '';
694
+ for (const line of lines) {
695
+ const trimmed = line.trim();
696
+ if (!trimmed.startsWith('{') || !trimmed.includes('AskUserQuestion')) continue;
697
+ try {
698
+ const parsed = JSON.parse(trimmed);
699
+ const prompt = detectClaudeAskUserQuestionPromptFromJson(parsed, this.cliType);
700
+ if (!prompt) continue;
701
+ this.activeInteractivePrompt = prompt;
702
+ this.interactivePromptTransport = 'stream-json';
703
+ this.interactivePromptLostAt = null;
704
+ this.statusCallback?.();
705
+ } catch {
706
+ // PTY output is not guaranteed to be machine JSON.
707
+ }
708
+ }
709
+ }
710
+
711
+ private readCurrentScreenSections(_screenText: string): Record<string, string> {
712
+ try {
713
+ const sections = this.driver.getSections() ?? [];
714
+ return Object.fromEntries(sections.map(section => [section.id, section.text]));
715
+ } catch {
716
+ return {};
717
+ }
718
+ }
719
+
720
+ private extractProviderSessionIdFromScreen(): string | undefined {
721
+ if (this.cliType !== 'codex-cli') return this.providerSessionId;
722
+ let screenText = '';
723
+ try {
724
+ screenText = this.driver.snapshot();
725
+ } catch {
726
+ return this.providerSessionId;
727
+ }
728
+ const clean = stripAnsi(screenText);
729
+ const match = clean.match(/(?:gpt-|o\d|codex-)[^\n·]*·[^\n·]*·\s*([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/i);
730
+ return match?.[1] || this.providerSessionId;
731
+ }
732
+
733
+ private readClaudeScreenAssistantMessages(): ChatMessage[] {
734
+ if (this.cliType !== 'claude-cli') return [];
735
+ let screenText = '';
736
+ try {
737
+ screenText = this.driver.snapshot();
738
+ } catch {
739
+ return [];
740
+ }
741
+ const sections = this.readCurrentScreenSections(screenText);
742
+ const body = sections.body || screenText;
743
+ const messages: ChatMessage[] = [];
744
+ const seen = new Set<string>();
745
+ for (const line of body.split(/\r?\n/)) {
746
+ const match = line.match(/^\s*⏺\s+(.+?)\s*$/);
747
+ const content = match?.[1]?.trim();
748
+ if (!content || seen.has(content)) continue;
749
+ seen.add(content);
750
+ messages.push({
751
+ role: 'assistant',
752
+ kind: 'standard',
753
+ content,
754
+ source: 'assistant_text',
755
+ userFacing: true,
756
+ bubbleState: 'final',
757
+ });
758
+ }
759
+ return messages;
760
+ }
761
+
762
+ /**
763
+ * Grace window a held interactive prompt must be absent from the screen
764
+ * before we treat it as resolved-in-terminal and clear it. claude-cli
765
+ * repaints the picker across multiple PTY chunks, so a single
766
+ * footer-less frame is not proof the prompt is gone. Sized in the same
767
+ * spirit as the approval FSM's `approvalCooldown` modal-lost hysteresis.
768
+ */
769
+ private static readonly INTERACTIVE_PROMPT_LOST_GRACE_MS = 1500;
770
+
771
+ /**
772
+ * Multi-question TUI capture: after Tabbing to a page, re-snapshot at this
773
+ * interval until its checkbox glyph column has settled, up to the timeout.
774
+ * The interval matches the legacy single fixed wait (120ms); the timeout
775
+ * bounds total capture time so a single-select page (which never shows
776
+ * glyphs) doesn't stall the capture indefinitely.
777
+ */
778
+ private static readonly CLAUDE_TUI_PAGE_POLL_INTERVAL_MS = 120;
779
+ private static readonly CLAUDE_TUI_PAGE_SETTLE_TIMEOUT_MS = 600;
780
+
781
+ /**
782
+ * Clear a held interactive prompt once the user has resolved it directly
783
+ * in the terminal (the choice picker leaves the screen without going
784
+ * through setInteractivePromptResponse). The approval path already does
785
+ * this via the FSM's modal-lost hysteresis; the interactive-prompt path
786
+ * had no equivalent, so a terminal-side answer left activeInteractivePrompt
787
+ * set and getStatus() re-emitted the same choice modal forever.
788
+ *
789
+ * Detection mirrors capture: the claude TUI picker is on-screen exactly
790
+ * while its "Enter to select" footer is rendered. When the footer is gone
791
+ * for INTERACTIVE_PROMPT_LOST_GRACE_MS the prompt is genuinely resolved.
792
+ */
793
+ private maybeClearResolvedClaudeTuiPrompt(): void {
794
+ if (this.cliType !== 'claude-cli' || !this.activeInteractivePrompt) return;
795
+ // stream-json prompts are tracked by their tool-call lifecycle, not by
796
+ // screen footer, but claude renders the same TUI picker for both
797
+ // transports while awaiting an answer — so screen presence is a valid
798
+ // resolved-signal for either. (If the screen read fails, keep holding.)
799
+ let screenText = '';
800
+ try {
801
+ screenText = this.driver.snapshot();
802
+ } catch {
803
+ return;
804
+ }
805
+ const stillOnScreen = screenText.includes('Enter to select');
806
+ if (stillOnScreen) {
807
+ // Prompt reappeared / never left — reset the hysteresis timer.
808
+ this.interactivePromptLostAt = null;
809
+ return;
810
+ }
811
+ const lostAt = this.interactivePromptLostAt ?? Date.now();
812
+ if (this.interactivePromptLostAt === null) this.interactivePromptLostAt = lostAt;
813
+ if (Date.now() - lostAt < SpecCliAdapter.INTERACTIVE_PROMPT_LOST_GRACE_MS) return;
814
+ // Resolved in the terminal — drop the held prompt so getStatus() stops
815
+ // re-emitting it.
816
+ this.activeInteractivePrompt = null;
817
+ this.interactivePromptTransport = null;
818
+ this.interactivePromptLostAt = null;
819
+ this.statusCallback?.();
820
+ }
821
+
822
+ private maybeCaptureClaudeTuiPrompt(): void {
823
+ if (this.cliType !== 'claude-cli'
824
+ || this.activeInteractivePrompt
825
+ || this.claudeTuiPromptCaptureInFlight) return;
826
+ const screenText = this.driver.snapshot();
827
+ const headers = this.readClaudeTuiHeaders(screenText);
828
+ if (!screenText.includes('Enter to select')) return;
829
+ if (headers.length === 0) {
830
+ const prompt = detectClaudeAskUserQuestionPromptFromTuiPages([{ screenText }], {
831
+ promptId: `ask-user-${this.providerSessionId || 'claude'}-${Date.now()}`,
832
+ providerType: this.cliType,
833
+ });
834
+ if (!prompt) return;
835
+ this.activeInteractivePrompt = prompt;
836
+ this.interactivePromptTransport = 'tui';
837
+ this.interactivePromptLostAt = null;
838
+ this.statusCallback?.();
839
+ return;
840
+ }
841
+ this.claudeTuiPromptCaptureInFlight = true;
842
+ void this.captureClaudeTuiPrompt(screenText, headers).finally(() => {
843
+ this.claudeTuiPromptCaptureInFlight = false;
844
+ });
845
+ }
846
+
847
+ /**
848
+ * The TUI prompt is captured on the FIRST frame that renders the
849
+ * "Enter to select" footer. At that instant the option rows' checkbox
850
+ * column may not have drawn yet, so `detectClaudeTuiMultiSelect` returns
851
+ * false and the prompt is frozen as single-select — the dashboard then
852
+ * renders radio buttons even though the picker is multi-select.
853
+ *
854
+ * While the same TUI prompt is still on screen, re-check the live snapshot:
855
+ * if checkbox glyphs have since appeared, promote any single-select
856
+ * question to multi-select and re-emit status. Promotion is one-way
857
+ * (false→true only) — once a question is known multi-select we never demote
858
+ * it, since the glyph column can scroll out of view on later frames.
859
+ *
860
+ * For MULTI-question prompts the per-page Tab capture is the actual source
861
+ * of the bug: pages 2..N are snapshotted ~120ms after the Tab keypress,
862
+ * before their option-row glyph column has redrawn, so those questions
863
+ * freeze as single-select while page 1 (already settled) is correct. We
864
+ * cannot upgrade blindly — the live snapshot shows only ONE focused page —
865
+ * but we CAN read that page's question text/header and upgrade the matching
866
+ * question. As the user navigates the picker (or it settles), each page is
867
+ * eventually re-read and repaired.
868
+ */
869
+ private maybeUpgradeClaudeTuiMultiSelect(): void {
870
+ if (this.cliType !== 'claude-cli'
871
+ || this.interactivePromptTransport !== 'tui'
872
+ || !this.activeInteractivePrompt) return;
873
+ const questions = this.activeInteractivePrompt.questions;
874
+ if (questions.every(q => q.multiSelect)) return;
875
+ let screenText = '';
876
+ try {
877
+ screenText = this.driver.snapshot();
878
+ } catch {
879
+ return;
880
+ }
881
+ if (!screenText.includes('Enter to select')) return;
882
+
883
+ if (questions.length === 1) {
884
+ if (questions[0].multiSelect) return;
885
+ if (!detectClaudeTuiMultiSelect(screenText)) return;
886
+ questions[0].multiSelect = true;
887
+ this.statusCallback?.();
888
+ return;
889
+ }
890
+
891
+ // Multi-question: attribute the focused page's glyphs to its question by
892
+ // matching header (preferred) or question text, then upgrade just that
893
+ // one. Never demote — a settled non-multi page is left as captured.
894
+ const focused = readFocusedClaudeTuiQuestion(screenText);
895
+ if (!focused || !focused.multiSelect) return;
896
+ const match = questions.find(q =>
897
+ (focused.header && q.header && q.header === focused.header)
898
+ || q.question === focused.question);
899
+ if (!match || match.multiSelect) return;
900
+ match.multiSelect = true;
901
+ this.statusCallback?.();
902
+ }
903
+
904
+ private readClaudeTuiHeaders(screenText: string): string[] {
905
+ const navLine = screenText.split(/\r?\n/).find(line => line.includes('✔ Submit') && /[☐☒]/.test(line));
906
+ if (!navLine) return [];
907
+ const headers: string[] = [];
908
+ for (const match of navLine.matchAll(/[☐☒]\s+(.+?)(?=\s+[☐☒]|\s+✔\s+Submit)/g)) {
909
+ const header = match[1]?.trim();
910
+ if (header) headers.push(header);
911
+ }
912
+ return headers;
913
+ }
914
+
915
+ /**
916
+ * Snapshot the currently-focused claude TUI page, polling until its
917
+ * option-row checkbox glyph column has settled (or a bounded timeout).
918
+ *
919
+ * Why poll: right after a Tab keypress the newly-focused page's glyph
920
+ * column has not redrawn yet, so an immediate snapshot shows the question +
921
+ * option labels but NO per-option checkbox markers — freezing that page as
922
+ * single-select. A fixed delay either races (too short) or is wasteful (too
923
+ * long). Instead we re-snapshot at a fixed interval and stop as soon as the
924
+ * frame shows multi-select glyphs, falling back to the last frame at the
925
+ * timeout. Single-select pages never show glyphs, so they always poll to the
926
+ * timeout — bounded small to keep capture snappy.
927
+ */
928
+ private async snapshotSettledClaudeTuiPage(): Promise<string> {
929
+ let screenText = this.driver.snapshot();
930
+ const deadline = Date.now() + SpecCliAdapter.CLAUDE_TUI_PAGE_SETTLE_TIMEOUT_MS;
931
+ while (!detectClaudeTuiMultiSelect(screenText) && Date.now() < deadline) {
932
+ await new Promise(resolve => setTimeout(resolve, SpecCliAdapter.CLAUDE_TUI_PAGE_POLL_INTERVAL_MS));
933
+ screenText = this.driver.snapshot();
934
+ }
935
+ return screenText;
936
+ }
937
+
938
+ private async captureClaudeTuiPrompt(firstScreen: string, headers: string[]): Promise<void> {
939
+ const pages: ClaudeInteractiveTuiPage[] = [{ screenText: firstScreen, header: headers[0] }];
940
+ // Forward pass: Tab to each page 2..N and snapshot once its glyph column
941
+ // has settled, so pages 2+ capture their checkbox markers (not a racy
942
+ // pre-redraw frame).
943
+ for (let index = 1; index < headers.length; index += 1) {
944
+ this.driver.dispatch({ kind: 'pty_write', data: '\t' });
945
+ await new Promise(resolve => setTimeout(resolve, SpecCliAdapter.CLAUDE_TUI_PAGE_POLL_INTERVAL_MS));
946
+ pages.push({ screenText: await this.snapshotSettledClaudeTuiPage(), header: headers[index] });
947
+ }
948
+ // Return pass: Shift-Tab back through pages N..2. As we land on each page
949
+ // again re-read it and OR-in any now-visible multi-select glyphs — a
950
+ // second chance to repair a page whose forward-pass frame was still racy.
951
+ for (let index = headers.length - 1; index > 0; index -= 1) {
952
+ this.driver.dispatch({ kind: 'pty_write', data: '\x1b[Z' });
953
+ await new Promise(resolve => setTimeout(resolve, SpecCliAdapter.CLAUDE_TUI_PAGE_POLL_INTERVAL_MS));
954
+ const reread = await this.snapshotSettledClaudeTuiPage();
955
+ // The page we just Shift-Tab'd ONTO is index-1 (we move backwards).
956
+ const landed = pages[index - 1];
957
+ if (landed
958
+ && !detectClaudeTuiMultiSelect(landed.screenText)
959
+ && detectClaudeTuiMultiSelect(reread)) {
960
+ landed.screenText = reread;
961
+ }
962
+ }
963
+
964
+ const prompt = detectClaudeAskUserQuestionPromptFromTuiPages(pages, {
965
+ promptId: `ask-user-${this.providerSessionId || 'claude'}-${Date.now()}`,
966
+ providerType: this.cliType,
967
+ });
968
+ if (!prompt) return;
969
+ this.activeInteractivePrompt = prompt;
970
+ this.interactivePromptTransport = 'tui';
971
+ this.interactivePromptLostAt = null;
972
+ this.statusCallback?.();
973
+ }
974
+
975
+ getDebugState(): Record<string, any> {
976
+ const screen = this.driver.getScreen?.() ?? '';
977
+ const history = this.driver.getStateHistory();
978
+ const status = this.getStatus();
979
+
980
+ let messages: any[] = [];
981
+ if (this.spec.native_history?.source) {
982
+ try {
983
+ const result = executeNativeHistory(this.spec.native_history, {
984
+ agentType: this.cliType,
985
+ providerSessionId: this.providerSessionId,
986
+ sessionStartedAtMs: this.spawnedAtMs,
987
+ envOverrides: this.spawnedEnv,
988
+ workspace: this.workingDir,
989
+ });
990
+ if (result && Array.isArray(result.messages)) {
991
+ messages = result.messages;
992
+ }
993
+ } catch (e) {
994
+ // Ignore native history read errors in debug state
995
+ }
996
+ } else {
997
+ messages = this.readClaudeScreenAssistantMessages();
998
+ }
999
+
1000
+ const latestState = this.latestState;
1001
+ const latestModal = this.latestModal;
1002
+ return {
1003
+ type: this.cliType,
1004
+ name: this.cliName,
1005
+ status: status.status,
1006
+ rawStatus: status.status,
1007
+ projectedStatus: status.status,
1008
+ ready: this.spawned,
1009
+ // Legacy snapshot-style fields for panels that read getDebugSnapshot shape
1010
+ spec_id: this.spec.id,
1011
+ current_state: latestState ?? null,
1012
+ current_modal: latestModal ?? null,
1013
+ exited: this.exited,
1014
+ idleHoldPending: this.driver.hasIdleHoldPending?.() ?? false,
1015
+ lastBusyAt: this.driver.getLastBusyAt?.() ?? 0,
1016
+ screen: screen,
1017
+ screenText: screen,
1018
+ workingDir: this.workingDir,
1019
+ spawnedAtMs: this.spawnedAtMs,
1020
+ providerSessionId: this.providerSessionId ?? null,
1021
+ sections: this.driver.getSections?.() ?? null,
1022
+ stateHistory: history,
1023
+ specPath: this.driver.getSpecPath?.() ?? null,
1024
+ // v4 FSM live transition table — present only for FsmDriver. Lets
1025
+ // the panel (and the daemon API) show, for the current instant,
1026
+ // every outgoing transition with its per-condition match result
1027
+ // and countdown. This is the canonical "why isn't it transitioning"
1028
+ // answer — no screenshots needed.
1029
+ fsm: this.driver.getFsmDebug?.() ?? null,
1030
+ // v4 FSM transition snapshot history — the captured pre-transition
1031
+ // evaluation table at each transition (null for v3 specs).
1032
+ fsmHistory: this.driver.getFsmSnapshotHistory?.() ?? null,
1033
+ // PTY input/output/resize/cursor event timeline (debug-only).
1034
+ eventTimeline: this.driver.getEventTimeline?.() ?? null,
1035
+ messages,
1036
+ committedMessages: messages,
1037
+ };
1038
+ }
1039
+
1040
+ getTraceState(limit = 120): Record<string, any> {
1041
+ const history = this.driver.getStateHistory();
1042
+ return {
1043
+ status: this.getStatus().status,
1044
+ stateHistory: history.slice(-limit),
1045
+ screenText: this.driver.getScreen?.() ?? '',
1046
+ };
1047
+ }
1048
+
1049
+ getProviderResolutionMeta(): Record<string, any> {
1050
+ return {
1051
+ type: this.cliType,
1052
+ providerDir: null,
1053
+ resolvedVersion: null,
1054
+ };
1055
+ }
1056
+ }