@skillcdn/console 0.6.0 → 0.6.2

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 (415) hide show
  1. package/README.md +7 -7
  2. package/dist/agent-page.d.ts.map +1 -1
  3. package/dist/agent-page.js +58 -35
  4. package/dist/agent-page.js.map +1 -1
  5. package/dist/agent-usage.d.ts +51 -0
  6. package/dist/agent-usage.d.ts.map +1 -0
  7. package/dist/agent-usage.js +77 -0
  8. package/dist/agent-usage.js.map +1 -0
  9. package/dist/api.d.ts +4 -2
  10. package/dist/api.d.ts.map +1 -1
  11. package/dist/api.js +4 -2
  12. package/dist/api.js.map +1 -1
  13. package/dist/cli/cli.d.ts +9 -2
  14. package/dist/cli/cli.d.ts.map +1 -1
  15. package/dist/cli/cli.js +491 -125
  16. package/dist/cli/cli.js.map +1 -1
  17. package/dist/cli/format.d.ts +46 -4
  18. package/dist/cli/format.d.ts.map +1 -1
  19. package/dist/cli/format.js +163 -45
  20. package/dist/cli/format.js.map +1 -1
  21. package/dist/cli/help.d.ts.map +1 -1
  22. package/dist/cli/help.js +62 -13
  23. package/dist/cli/help.js.map +1 -1
  24. package/dist/cli/machine-check.d.ts +24 -5
  25. package/dist/cli/machine-check.d.ts.map +1 -1
  26. package/dist/cli/machine-check.js +128 -16
  27. package/dist/cli/machine-check.js.map +1 -1
  28. package/dist/cli/pack.d.ts +11 -8
  29. package/dist/cli/pack.d.ts.map +1 -1
  30. package/dist/cli/pack.js +63 -28
  31. package/dist/cli/pack.js.map +1 -1
  32. package/dist/cli/runner-host.d.ts +2 -0
  33. package/dist/cli/runner-host.d.ts.map +1 -1
  34. package/dist/cli/runner-host.js +1 -0
  35. package/dist/cli/runner-host.js.map +1 -1
  36. package/dist/cli/secret-notes.d.ts.map +1 -1
  37. package/dist/cli/secret-notes.js +9 -1
  38. package/dist/cli/secret-notes.js.map +1 -1
  39. package/dist/cli/terminal.d.ts +10 -2
  40. package/dist/cli/terminal.d.ts.map +1 -1
  41. package/dist/cli/terminal.js +16 -4
  42. package/dist/cli/terminal.js.map +1 -1
  43. package/dist/client.d.ts +30 -7
  44. package/dist/client.d.ts.map +1 -1
  45. package/dist/client.js +13 -1
  46. package/dist/client.js.map +1 -1
  47. package/dist/components/agent-page.d.ts +76 -18
  48. package/dist/components/agent-page.d.ts.map +1 -1
  49. package/dist/components/agent-page.js +183 -53
  50. package/dist/components/agent-page.js.map +1 -1
  51. package/dist/components/agents.d.ts +2 -0
  52. package/dist/components/agents.d.ts.map +1 -1
  53. package/dist/components/agents.js +6 -5
  54. package/dist/components/agents.js.map +1 -1
  55. package/dist/components/calendar.d.ts +24 -4
  56. package/dist/components/calendar.d.ts.map +1 -1
  57. package/dist/components/calendar.js +79 -36
  58. package/dist/components/calendar.js.map +1 -1
  59. package/dist/components/categories.js +2 -2
  60. package/dist/components/categories.js.map +1 -1
  61. package/dist/components/comments.js +2 -2
  62. package/dist/components/comments.js.map +1 -1
  63. package/dist/components/decision-list.d.ts.map +1 -1
  64. package/dist/components/decision-list.js +5 -5
  65. package/dist/components/decision-list.js.map +1 -1
  66. package/dist/components/documents.d.ts.map +1 -1
  67. package/dist/components/documents.js +4 -4
  68. package/dist/components/documents.js.map +1 -1
  69. package/dist/components/event-feed.d.ts +2 -3
  70. package/dist/components/event-feed.d.ts.map +1 -1
  71. package/dist/components/event-feed.js +4 -184
  72. package/dist/components/event-feed.js.map +1 -1
  73. package/dist/components/goals.d.ts.map +1 -1
  74. package/dist/components/goals.js +3 -3
  75. package/dist/components/goals.js.map +1 -1
  76. package/dist/components/home.js +9 -9
  77. package/dist/components/home.js.map +1 -1
  78. package/dist/components/icon.d.ts.map +1 -1
  79. package/dist/components/icon.js +11 -2
  80. package/dist/components/icon.js.map +1 -1
  81. package/dist/components/notifications.d.ts +65 -0
  82. package/dist/components/notifications.d.ts.map +1 -0
  83. package/dist/components/notifications.js +162 -0
  84. package/dist/components/notifications.js.map +1 -0
  85. package/dist/components/pack-connect.js +1 -1
  86. package/dist/components/pack-connect.js.map +1 -1
  87. package/dist/components/pack-token.js +1 -1
  88. package/dist/components/pack-token.js.map +1 -1
  89. package/dist/components/people.js +2 -2
  90. package/dist/components/people.js.map +1 -1
  91. package/dist/components/pictures.js +1 -1
  92. package/dist/components/pictures.js.map +1 -1
  93. package/dist/components/plugin-settings.d.ts +22 -1
  94. package/dist/components/plugin-settings.d.ts.map +1 -1
  95. package/dist/components/plugin-settings.js +63 -11
  96. package/dist/components/plugin-settings.js.map +1 -1
  97. package/dist/components/projects.js +1 -1
  98. package/dist/components/projects.js.map +1 -1
  99. package/dist/components/roadmap.js +2 -2
  100. package/dist/components/roadmap.js.map +1 -1
  101. package/dist/components/rules.js +2 -2
  102. package/dist/components/rules.js.map +1 -1
  103. package/dist/components/runs.d.ts.map +1 -1
  104. package/dist/components/runs.js +1 -1
  105. package/dist/components/runs.js.map +1 -1
  106. package/dist/components/sessions.js +1 -1
  107. package/dist/components/sessions.js.map +1 -1
  108. package/dist/components/shell.d.ts +2 -0
  109. package/dist/components/shell.d.ts.map +1 -1
  110. package/dist/components/shell.js +1 -1
  111. package/dist/components/shell.js.map +1 -1
  112. package/dist/components/sign-in.js +2 -2
  113. package/dist/components/sign-in.js.map +1 -1
  114. package/dist/components/skills.d.ts.map +1 -1
  115. package/dist/components/skills.js +1 -1
  116. package/dist/components/skills.js.map +1 -1
  117. package/dist/components/task-view.d.ts +6 -0
  118. package/dist/components/task-view.d.ts.map +1 -1
  119. package/dist/components/task-view.js +3 -2
  120. package/dist/components/task-view.js.map +1 -1
  121. package/dist/components/tokens.js +1 -1
  122. package/dist/components/tokens.js.map +1 -1
  123. package/dist/components/ui.d.ts +21 -2
  124. package/dist/components/ui.d.ts.map +1 -1
  125. package/dist/components/ui.js +24 -3
  126. package/dist/components/ui.js.map +1 -1
  127. package/dist/components/work.js +1 -1
  128. package/dist/components/work.js.map +1 -1
  129. package/dist/console.css +506 -289
  130. package/dist/console.d.ts.map +1 -1
  131. package/dist/console.js +193 -16
  132. package/dist/console.js.map +1 -1
  133. package/dist/data.d.ts +17 -1
  134. package/dist/data.d.ts.map +1 -1
  135. package/dist/data.js +31 -0
  136. package/dist/data.js.map +1 -1
  137. package/dist/desk-page.js +5 -5
  138. package/dist/desk-page.js.map +1 -1
  139. package/dist/feed-words.d.ts +5 -0
  140. package/dist/feed-words.d.ts.map +1 -0
  141. package/dist/feed-words.js +193 -0
  142. package/dist/feed-words.js.map +1 -0
  143. package/dist/i18n/en.d.ts +93 -1
  144. package/dist/i18n/en.d.ts.map +1 -1
  145. package/dist/i18n/en.js +103 -15
  146. package/dist/i18n/en.js.map +1 -1
  147. package/dist/i18n/ko.d.ts.map +1 -1
  148. package/dist/i18n/ko.js +105 -22
  149. package/dist/i18n/ko.js.map +1 -1
  150. package/dist/icons/glyphs.d.ts +370 -104
  151. package/dist/icons/glyphs.d.ts.map +1 -1
  152. package/dist/icons/glyphs.js +72 -66
  153. package/dist/icons/glyphs.js.map +1 -1
  154. package/dist/icons/index.d.ts.map +1 -1
  155. package/dist/icons/index.js +9 -1
  156. package/dist/icons/index.js.map +1 -1
  157. package/dist/index.d.ts +3 -2
  158. package/dist/index.d.ts.map +1 -1
  159. package/dist/index.js +3 -2
  160. package/dist/index.js.map +1 -1
  161. package/dist/limits.d.ts +7 -0
  162. package/dist/limits.d.ts.map +1 -1
  163. package/dist/limits.js +7 -0
  164. package/dist/limits.js.map +1 -1
  165. package/dist/notifications.d.ts +124 -0
  166. package/dist/notifications.d.ts.map +1 -0
  167. package/dist/notifications.js +147 -0
  168. package/dist/notifications.js.map +1 -0
  169. package/dist/onboarding.d.ts.map +1 -1
  170. package/dist/onboarding.js +7 -12
  171. package/dist/onboarding.js.map +1 -1
  172. package/dist/plugin/capabilities.d.ts +1 -1
  173. package/dist/plugin/capabilities.d.ts.map +1 -1
  174. package/dist/plugin/capabilities.js +10 -0
  175. package/dist/plugin/capabilities.js.map +1 -1
  176. package/dist/plugin/context.d.ts +85 -1
  177. package/dist/plugin/context.d.ts.map +1 -1
  178. package/dist/plugin/limits.d.ts +7 -0
  179. package/dist/plugin/limits.d.ts.map +1 -1
  180. package/dist/plugin/limits.js +7 -0
  181. package/dist/plugin/limits.js.map +1 -1
  182. package/dist/plugin/manifest.d.ts +8 -0
  183. package/dist/plugin/manifest.d.ts.map +1 -1
  184. package/dist/plugin/manifest.js +13 -0
  185. package/dist/plugin/manifest.js.map +1 -1
  186. package/dist/plugin/rest-schemas.d.ts +22 -0
  187. package/dist/plugin/rest-schemas.d.ts.map +1 -1
  188. package/dist/plugin/rest-schemas.js +19 -3
  189. package/dist/plugin/rest-schemas.js.map +1 -1
  190. package/dist/plugin/testing.d.ts +2 -0
  191. package/dist/plugin/testing.d.ts.map +1 -1
  192. package/dist/plugin/testing.js +2 -0
  193. package/dist/plugin/testing.js.map +1 -1
  194. package/dist/plugin.d.ts +1 -1
  195. package/dist/plugin.d.ts.map +1 -1
  196. package/dist/plugin.js +1 -1
  197. package/dist/plugin.js.map +1 -1
  198. package/dist/router.d.ts +6 -0
  199. package/dist/router.d.ts.map +1 -1
  200. package/dist/router.js +5 -0
  201. package/dist/router.js.map +1 -1
  202. package/dist/routes.d.ts +2 -0
  203. package/dist/routes.d.ts.map +1 -1
  204. package/dist/routes.js +2 -0
  205. package/dist/routes.js.map +1 -1
  206. package/dist/rules.js +1 -1
  207. package/dist/rules.js.map +1 -1
  208. package/dist/runner/acp.d.ts.map +1 -1
  209. package/dist/runner/acp.js +12 -97
  210. package/dist/runner/acp.js.map +1 -1
  211. package/dist/runner/browser.d.ts +50 -0
  212. package/dist/runner/browser.d.ts.map +1 -0
  213. package/dist/runner/browser.js +104 -0
  214. package/dist/runner/browser.js.map +1 -0
  215. package/dist/runner/claude.d.ts +1 -2
  216. package/dist/runner/claude.d.ts.map +1 -1
  217. package/dist/runner/claude.js +352 -13
  218. package/dist/runner/claude.js.map +1 -1
  219. package/dist/runner/codex.d.ts +9 -10
  220. package/dist/runner/codex.d.ts.map +1 -1
  221. package/dist/runner/codex.js +809 -346
  222. package/dist/runner/codex.js.map +1 -1
  223. package/dist/runner/config.d.ts +14 -3
  224. package/dist/runner/config.d.ts.map +1 -1
  225. package/dist/runner/config.js +27 -7
  226. package/dist/runner/config.js.map +1 -1
  227. package/dist/runner/desk.d.ts +3 -1
  228. package/dist/runner/desk.d.ts.map +1 -1
  229. package/dist/runner/desk.js +17 -5
  230. package/dist/runner/desk.js.map +1 -1
  231. package/dist/runner/message.d.ts +53 -3
  232. package/dist/runner/message.d.ts.map +1 -1
  233. package/dist/runner/message.js +201 -20
  234. package/dist/runner/message.js.map +1 -1
  235. package/dist/runner/registry.d.ts +6 -0
  236. package/dist/runner/registry.d.ts.map +1 -1
  237. package/dist/runner/registry.js.map +1 -1
  238. package/dist/runner/rpc.d.ts +31 -0
  239. package/dist/runner/rpc.d.ts.map +1 -0
  240. package/dist/runner/rpc.js +102 -0
  241. package/dist/runner/rpc.js.map +1 -0
  242. package/dist/runner/runner.d.ts +41 -1
  243. package/dist/runner/runner.d.ts.map +1 -1
  244. package/dist/runner/runner.js +695 -52
  245. package/dist/runner/runner.js.map +1 -1
  246. package/dist/runner/slack.d.ts +17 -8
  247. package/dist/runner/slack.d.ts.map +1 -1
  248. package/dist/runner/slack.js +1250 -78
  249. package/dist/runner/slack.js.map +1 -1
  250. package/dist/runner/types.d.ts +160 -8
  251. package/dist/runner/types.d.ts.map +1 -1
  252. package/dist/runner/types.js.map +1 -1
  253. package/dist/runner/usage.d.ts +105 -0
  254. package/dist/runner/usage.d.ts.map +1 -0
  255. package/dist/runner/usage.js +218 -0
  256. package/dist/runner/usage.js.map +1 -0
  257. package/dist/runner.d.ts +4 -4
  258. package/dist/runner.d.ts.map +1 -1
  259. package/dist/runner.js +3 -3
  260. package/dist/runner.js.map +1 -1
  261. package/dist/schemas.d.ts +673 -0
  262. package/dist/schemas.d.ts.map +1 -1
  263. package/dist/schemas.js +205 -1
  264. package/dist/schemas.js.map +1 -1
  265. package/dist/services.js +3 -3
  266. package/dist/services.js.map +1 -1
  267. package/dist/setup-guide.d.ts +4 -3
  268. package/dist/setup-guide.d.ts.map +1 -1
  269. package/dist/setup-guide.js +11 -13
  270. package/dist/setup-guide.js.map +1 -1
  271. package/dist/text.d.ts +7 -0
  272. package/dist/text.d.ts.map +1 -1
  273. package/dist/text.js +18 -0
  274. package/dist/text.js.map +1 -1
  275. package/dist/vocabulary.d.ts +4 -2
  276. package/dist/vocabulary.d.ts.map +1 -1
  277. package/dist/vocabulary.js +4 -1
  278. package/dist/vocabulary.js.map +1 -1
  279. package/docs/specs/cli.md +26 -19
  280. package/docs/specs/plugins.md +16 -10
  281. package/docs/specs/rest.md +28 -10
  282. package/docs/specs/runner.md +132 -44
  283. package/package.json +4 -4
  284. package/skills/building-a-console/SKILL.md +2 -2
  285. package/skills/building-a-console/references/configuring.md +1 -1
  286. package/skills/building-a-plugin/SKILL.md +4 -4
  287. package/skills/planning-the-board/SKILL.md +1 -1
  288. package/skills/planning-the-board/references/setting-up.md +2 -2
  289. package/skills/running-an-agent/SKILL.md +15 -64
  290. package/skills/running-an-agent/references/preparing-the-machine.md +17 -0
  291. package/skills/running-an-agent/references/several-tasks.md +37 -0
  292. package/skills/running-an-agent/references/slack-app.md +70 -13
  293. package/skills/running-an-agent/references/the-account-and-the-invitation.md +5 -0
  294. package/skills/running-an-agent/references/what-was-not-seen-run-yet.md +3 -0
  295. package/skills/running-an-agent/references/when-the-agent-could-not-go-on.md +16 -0
  296. package/skills/running-an-agent/references/your-first-task.md +14 -0
  297. package/skills/working-the-board/SKILL.md +9 -12
  298. package/skills/working-the-board/references/a-pack-skills-questions.md +3 -0
  299. package/skills/working-the-board/references/a-plugins-commands.md +3 -0
  300. package/skills/working-the-board/references/a-review.md +3 -0
  301. package/skills/working-the-board/references/an-open-run.md +3 -0
  302. package/skills/working-the-board/references/requirements.md +7 -0
  303. package/skills/working-the-board/references/the-pack.md +2 -2
  304. package/skills/working-the-board/references/what-they-were-told.md +3 -0
  305. package/skills/working-the-board/references/work-given-in-chat.md +5 -0
  306. package/src/agent-page.tsx +76 -47
  307. package/src/agent-usage.ts +113 -0
  308. package/src/api.ts +62 -0
  309. package/src/cli/cli.ts +634 -162
  310. package/src/cli/format.ts +190 -47
  311. package/src/cli/help.ts +77 -12
  312. package/src/cli/machine-check.ts +182 -29
  313. package/src/cli/pack.ts +81 -40
  314. package/src/cli/runner-host.ts +3 -0
  315. package/src/cli/secret-notes.ts +9 -1
  316. package/src/cli/terminal.ts +20 -4
  317. package/src/client.ts +94 -6
  318. package/src/components/agent-page.tsx +280 -63
  319. package/src/components/agents.tsx +23 -3
  320. package/src/components/calendar.tsx +93 -37
  321. package/src/components/categories.tsx +2 -2
  322. package/src/components/comments.tsx +5 -5
  323. package/src/components/decision-list.tsx +13 -10
  324. package/src/components/documents.tsx +13 -8
  325. package/src/components/event-feed.tsx +4 -203
  326. package/src/components/goals.tsx +6 -4
  327. package/src/components/home.tsx +12 -12
  328. package/src/components/icon.tsx +16 -3
  329. package/src/components/notifications.tsx +419 -0
  330. package/src/components/pack-connect.tsx +1 -1
  331. package/src/components/pack-token.tsx +5 -5
  332. package/src/components/people.tsx +3 -3
  333. package/src/components/pictures.tsx +1 -1
  334. package/src/components/plugin-settings.tsx +128 -23
  335. package/src/components/projects.tsx +1 -1
  336. package/src/components/roadmap.tsx +2 -2
  337. package/src/components/rules.tsx +2 -2
  338. package/src/components/runs.tsx +6 -3
  339. package/src/components/sessions.tsx +2 -2
  340. package/src/components/shell.tsx +3 -0
  341. package/src/components/sign-in.tsx +3 -3
  342. package/src/components/skills.tsx +5 -3
  343. package/src/components/task-view.tsx +18 -7
  344. package/src/components/tokens.tsx +2 -2
  345. package/src/components/ui.tsx +70 -5
  346. package/src/components/work.tsx +2 -2
  347. package/src/console.tsx +269 -12
  348. package/src/data.ts +54 -0
  349. package/src/desk-page.tsx +5 -5
  350. package/src/feed-words.ts +214 -0
  351. package/src/i18n/en.ts +114 -15
  352. package/src/i18n/ko.ts +115 -22
  353. package/src/icons/glyphs.ts +74 -68
  354. package/src/icons/index.ts +9 -1
  355. package/src/index.ts +11 -0
  356. package/src/limits.ts +7 -0
  357. package/src/notifications.ts +205 -0
  358. package/src/onboarding.ts +10 -14
  359. package/src/plugin/capabilities.ts +13 -0
  360. package/src/plugin/context.ts +87 -0
  361. package/src/plugin/limits.ts +7 -0
  362. package/src/plugin/manifest.ts +14 -0
  363. package/src/plugin/rest-schemas.ts +27 -3
  364. package/src/plugin/testing.ts +4 -0
  365. package/src/plugin.ts +2 -0
  366. package/src/router.ts +7 -0
  367. package/src/routes.ts +2 -0
  368. package/src/rules.ts +1 -1
  369. package/src/runner/acp.ts +12 -118
  370. package/src/runner/browser.ts +128 -0
  371. package/src/runner/claude.ts +399 -12
  372. package/src/runner/codex.ts +904 -400
  373. package/src/runner/config.ts +30 -7
  374. package/src/runner/desk.ts +22 -6
  375. package/src/runner/message.ts +251 -21
  376. package/src/runner/registry.ts +6 -0
  377. package/src/runner/rpc.ts +140 -0
  378. package/src/runner/runner.ts +863 -53
  379. package/src/runner/slack.ts +1535 -85
  380. package/src/runner/types.ts +150 -5
  381. package/src/runner/usage.ts +299 -0
  382. package/src/runner.ts +16 -1
  383. package/src/schemas.ts +249 -0
  384. package/src/services.ts +3 -3
  385. package/src/setup-guide.ts +12 -14
  386. package/src/styles/README.md +11 -7
  387. package/src/styles/console.css +2 -0
  388. package/src/styles/drawn-by-pages.json +7 -82
  389. package/src/styles/pages/agents.css +46 -35
  390. package/src/styles/pages/calendar.css +0 -1
  391. package/src/styles/pages/connect.css +0 -14
  392. package/src/styles/pages/decisions.css +2 -43
  393. package/src/styles/pages/documents.css +0 -29
  394. package/src/styles/pages/home.css +4 -16
  395. package/src/styles/pages/people.css +2 -17
  396. package/src/styles/pages/plugins.css +9 -12
  397. package/src/styles/pages/roadmap.css +0 -16
  398. package/src/styles/pages/settings.css +0 -28
  399. package/src/styles/pages/sign-in.css +2 -17
  400. package/src/styles/pages/thread.css +4 -50
  401. package/src/styles/pages/work.css +0 -5
  402. package/src/styles/parts/button.css +31 -0
  403. package/src/styles/parts/count.css +10 -0
  404. package/src/styles/parts/divider.css +17 -0
  405. package/src/styles/parts/frame.css +12 -0
  406. package/src/styles/parts/icon.css +39 -0
  407. package/src/styles/parts/menu.css +184 -0
  408. package/src/styles/parts/panel.css +7 -0
  409. package/src/styles/parts/progress.css +63 -0
  410. package/src/styles/parts/settings.css +2 -0
  411. package/src/styles/parts/sheet.css +28 -0
  412. package/src/styles/parts/text.css +34 -0
  413. package/src/text.ts +19 -0
  414. package/src/vocabulary.ts +4 -1
  415. package/template/plugin/package.json +1 -1
@@ -1,16 +1,35 @@
1
+ import { randomUUID } from "node:crypto";
2
+ import { connectionOf, METHOD_NOT_FOUND, RpcError } from "./rpc.js";
1
3
  import { errorsShown, notStarted, plainCut, plainCutEnd, } from "./types.js";
2
- // The `codex` driver (docs/specs/runner.md): the Codex binary, unmodified, run with nobody at
3
- // its terminal, `codex exec --json`. A process a turn: the message on its standard input, an
4
- // event a line on its standard output, and the conversation gone on with by the id its first
5
- // turn named. The product takes no message while a turn runs, so one that arrives then waits
6
- // for the turn to end, and one that is to be read now ends the turn's process first.
4
+ import { saysUsageLimit } from "./usage.js";
5
+ // The `codex` driver (docs/specs/runner.md, ADR-0153): the Codex binary, unmodified, run as its
6
+ // app server, `codex app-server`: one process a session, spoken to over its standard input and
7
+ // output, a JSON-RPC message a line. A conversation is a thread of the product's, begun or gone
8
+ // on with when the session opens. A message starts a turn, and one that arrives while a turn
9
+ // runs is put into that turn (`turn/steer`), where the product reads it after the step under
10
+ // way; one that is to be read now stops the turn first.
7
11
  //
8
- // Written from what the binary printed when it was run (0.164.0), with its own help for the
9
- // words it is started with; what was not seen there is marked where it is read. Everything the
10
- // product prints is data: read by shape, never run, and bounded by the runner before it is
11
- // sent on.
12
- /** How long a process asked to end is given before it is killed. */
13
- const KILL_GRACE_MS = 5000;
12
+ // Written from the protocol as the binary itself writes it out (`codex app-server
13
+ // generate-ts`, 0.172.0), from the product's own source at that build, and from what the binary
14
+ // answered when it was spoken to signed out, 0.139.0, 0.167.0 and 0.172.0: a thread begun and
15
+ // gone on with, a turn started, a message put into it and said back, a turn stopped, a turn
16
+ // that failed for the sign-in, and what it refuses. A turn of a signed-in product, with its
17
+ // tools, is in the shapes of that protocol and was not seen run. Everything the product writes
18
+ // is data: read by shape, never run, and bounded by the runner before it is sent on.
19
+ /** The words the product is started with: its app server, on its standard input and output. */
20
+ const ARGUMENTS = ["app-server"];
21
+ /**
22
+ * The oldest build the driver speaks to: the first whose app server says a message back under
23
+ * the id it was handed over with, which is how the driver knows a message put into a turn was
24
+ * read. An older one has the app server and no such id: a message put into a turn there would
25
+ * be read and, never said back, handed over once more with the next turn.
26
+ */
27
+ const OLDEST = [0, 139, 0];
28
+ /** How long a session that is stopped is given to end by itself, then after it was asked to. */
29
+ const STOP_GRACE_MS = 5000;
30
+ const KILL_GRACE_MS = 2000;
31
+ /** How long the product is given to open a conversation. */
32
+ const START_TIMEOUT_MS = 120_000;
14
33
  /** How much of a tool's input a tool call shows. */
15
34
  const MAX_INPUT_LENGTH = 4000;
16
35
  /** How many models, and how many levels of effort, are read from the product's own list. */
@@ -18,17 +37,113 @@ const MAX_OFFERED = 50;
18
37
  /** How much of its list of models is read: it carries each model's own instructions. */
19
38
  const MAX_CATALOG_LENGTH = 8 * 1024 * 1024;
20
39
  /**
21
- * A word that goes on the product's command line: a model, a level of effort, a conversation's
22
- * id. Anything else is no such word, and is left out: it never becomes an option of the
23
- * product's, or a setting beside the one it was meant for.
40
+ * A word the product is handed as one: a model, a level of effort, a conversation's id.
41
+ * Anything else is no such word, and is left out.
24
42
  */
25
43
  const WORD = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/;
26
44
  const word = (value) => typeof value === "string" && WORD.test(value) ? value : undefined;
27
45
  /**
28
- * What an error says when the product is not signed in or its login ran out. Its words for it
29
- * were not seen, since that takes signing it out: read by pattern, and no contract.
46
+ * What an error says when the product is not signed in or its login ran out, beside the status
47
+ * the service answered with. Seen signed out (0.172.0): `unexpected status 401 Unauthorized:
48
+ * Missing bearer or basic authentication in header`. The rest is read by pattern, and no contract.
30
49
  */
31
50
  const SIGN_IN = /not (?:logged|signed) in|codex login|log ?in again|sign in again|\b401\b|unauthorized|token (?:has )?(?:expired|been revoked)|invalid (?:api )?key/i;
51
+ /**
52
+ * The build that answered, from what it calls itself when it is greeted: the client's name, a
53
+ * stroke, and its own version (`skillcdn-console-runner/0.171.0-alpha.13.2 (Windows ...)`, seen
54
+ * with that build). Nothing where it says none that reads as one.
55
+ */
56
+ export function codexBuild(userAgent) {
57
+ const found = typeof userAgent === "string"
58
+ ? /^[^/\s]{1,80}\/((\d{1,4})\.(\d{1,5})\.(\d{1,6})[0-9A-Za-z.+-]{0,40})(?:\s|$)/.exec(userAgent)
59
+ : null;
60
+ return found === null
61
+ ? undefined
62
+ : { text: found[1] ?? "", numbers: [Number(found[2]), Number(found[3]), Number(found[4])] };
63
+ }
64
+ /** Whether a build is older than the oldest the driver speaks to. */
65
+ const tooOld = (numbers) => {
66
+ for (const [at, least] of OLDEST.entries()) {
67
+ const has = numbers[at] ?? 0;
68
+ if (has !== least) {
69
+ return has < least;
70
+ }
71
+ }
72
+ return false;
73
+ };
74
+ /** The product's sandboxes, from the one that allows the least to the one that allows the most, by its word for each in an answer. */
75
+ const SANDBOXES = [
76
+ ["readOnly", "read-only"],
77
+ ["workspaceWrite", "workspace-write"],
78
+ ["dangerFullAccess", "danger-full-access"],
79
+ ];
80
+ /** The sandbox a mode asks for, in the product's word for it. */
81
+ const sandboxOf = (mode) => {
82
+ const asked = modeSettings(mode).sandbox;
83
+ return typeof asked === "string" ? asked : LEAST_MODE;
84
+ };
85
+ /** The bucket of the plan's own allowance, by the product's id for it (seen with 0.171.0). */
86
+ const PLAN_BUCKET = "codex";
87
+ /** How much of a bucket's name is said after a window's own. */
88
+ const MAX_BUCKET_NAME = 24;
89
+ /**
90
+ * The windows of the product's allowance, as it says them (`account/rateLimits/updated`): each
91
+ * with the share used and when it resets, named by how long it lasts. Seen with 0.171.0, asked
92
+ * of a signed-in product with no turn: a share in hundredths, a length in minutes and a moment
93
+ * in seconds (`{ usedPercent: 4, windowDurationMins: 10080, resetsAt: 1795927747 }`). An
94
+ * update is sparse: a window it says nothing of is not one that reset. An update is of one
95
+ * bucket, which it names (`limitId`): the plan's own is `codex`, and any other is a quota kept
96
+ * apart, one model's say, whose windows are said with the bucket's name after their own, so
97
+ * that an update of one is never read as the plan's.
98
+ */
99
+ function allowanceOf(limits) {
100
+ const said = isObject(limits) ? limits : {};
101
+ const bucket = textOf(said.limitId);
102
+ const apart = bucket !== undefined && bucket !== PLAN_BUCKET;
103
+ // By the name the product shows for the bucket, or its id where it gives none: one line,
104
+ // and short, since the window's own name stands before it.
105
+ const called = (textOf(said.limitName) ?? "").replaceAll(/\s+/g, " ").trim();
106
+ const of = apart ? (called === "" ? bucket : called).slice(0, MAX_BUCKET_NAME) : "";
107
+ const windows = [];
108
+ for (const [place, one] of [
109
+ ["primary", said.primary],
110
+ ["secondary", said.secondary],
111
+ ]) {
112
+ if (!isObject(one) || typeof one.usedPercent !== "number" || !(one.usedPercent >= 0)) {
113
+ continue;
114
+ }
115
+ const minutes = typeof one.windowDurationMins === "number" && one.windowDurationMins > 0
116
+ ? Math.round(one.windowDurationMins)
117
+ : undefined;
118
+ const name = minutes === undefined
119
+ ? place
120
+ : minutes === 10_080
121
+ ? "weekly"
122
+ : minutes % 1440 === 0
123
+ ? `${minutes / 1440}-day`
124
+ : minutes % 60 === 0
125
+ ? `${minutes / 60}-hour`
126
+ : `${minutes}-minute`;
127
+ const resets = typeof one.resetsAt === "number" && one.resetsAt > 0 && one.resetsAt < 1e11
128
+ ? Math.floor(one.resetsAt * 1000)
129
+ : undefined;
130
+ windows.push({
131
+ name: of === "" ? name : `${name} ${of}`,
132
+ used: Math.min(1, one.usedPercent / 100),
133
+ resetsAt: resets,
134
+ ...(apart ? { apart } : {}),
135
+ });
136
+ }
137
+ return windows;
138
+ }
139
+ /**
140
+ * Whether a turn's error says that what the subscription allows is used up: the product's own
141
+ * word for it (`usageLimitExceeded`, as its protocol has it, not seen said), or its words.
142
+ */
143
+ function usageLimited(error) {
144
+ const info = isObject(error) ? error.codexErrorInfo : undefined;
145
+ return info === "usageLimitExceeded" || saysUsageLimit(errorOf(error));
146
+ }
32
147
  /** The product's words for what runs without asking: its three sandboxes, and the flag by which its own reviewer answers what a sandbox would ask. */
33
148
  const MODES = ["read-only", "workspace-write", "approve-for-me", "danger-full-access"];
34
149
  /**
@@ -51,72 +166,74 @@ export function codexMode(named) {
51
166
  ? UNATTENDED_MODE
52
167
  : (MODES.find((mode) => mode === named) ?? LEAST_MODE);
53
168
  }
169
+ const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
170
+ const textOf = (value) => typeof value === "string" ? value : undefined;
54
171
  /**
55
- * A mode as the settings the product takes at any turn, a conversation begun or one gone on
56
- * with: a turn keeps nothing of the mode the turn before ran in. Whatever a mode does not allow
57
- * is asked of nobody: refused, or answered by the product's reviewer.
172
+ * A mode as the settings a conversation is begun or gone on with, each said whole: a thread
173
+ * keeps what it last ran with, and one gone on with ran in whatever the desk named then.
174
+ * Whatever a mode does not allow is asked of nobody: refused, or answered by the product's
175
+ * reviewer.
58
176
  */
59
- function modeArguments(mode) {
60
- const set = (key, value) => ["-c", `${key}="${value}"`];
177
+ function modeSettings(mode) {
61
178
  switch (mode) {
62
179
  case "approve-for-me":
63
- return [
64
- ...set("sandbox_mode", "workspace-write"),
65
- ...set("approval_policy", "on-request"),
66
- ...set("approvals_reviewer", "auto_review"),
67
- ];
180
+ return {
181
+ sandbox: "workspace-write",
182
+ approvalPolicy: "on-request",
183
+ approvalsReviewer: "auto_review",
184
+ };
68
185
  case "workspace-write":
69
186
  case "danger-full-access":
70
- return [...set("sandbox_mode", mode), ...set("approval_policy", "never")];
187
+ return { sandbox: mode, approvalPolicy: "never", approvalsReviewer: "user" };
71
188
  default:
72
- return [...set("sandbox_mode", "read-only"), ...set("approval_policy", "never")];
189
+ return { sandbox: "read-only", approvalPolicy: "never", approvalsReviewer: "user" };
73
190
  }
74
191
  }
75
- /** The arguments one turn of the product is started with. */
76
- export function codexArguments(turn) {
77
- const model = word(turn.model);
78
- const effort = word(turn.effort);
79
- const conversation = word(turn.conversation);
80
- return [
81
- "exec",
82
- ...(conversation === undefined ? [] : ["resume"]),
83
- // A picture each under its own option, and before an option that takes nothing: the
84
- // product's option takes several values, and would take what follows it for one.
85
- ...(turn.images ?? []).filter(isPicturePath).flatMap((path) => ["-i", path]),
86
- "--json",
87
- // The desk is the agent's own, a repository or not one yet.
88
- "--skip-git-repo-check",
89
- ...modeArguments(codexMode(turn.permissions)),
90
- ...(model === undefined ? [] : ["-m", model]),
91
- ...(effort === undefined ? [] : ["-c", `model_reasoning_effort="${effort}"`]),
92
- ...(conversation === undefined ? [] : [conversation]),
93
- // The message is read from the standard input.
94
- "-",
95
- ];
96
- }
97
192
  /**
98
- * A path that goes on the product's command line as a picture: one the runner wrote under the
99
- * desk's `work/inbox/`, and nothing that begins as an option.
193
+ * What the product is asked to keep to itself: a block as it grows, a piece at a time, where
194
+ * the driver reads whole blocks, and counts nobody asked for.
195
+ */
196
+ const UNREAD = [
197
+ "thread/started",
198
+ "item/agentMessage/delta",
199
+ "item/reasoning/summaryTextDelta",
200
+ "item/reasoning/summaryPartAdded",
201
+ "item/reasoning/textDelta",
202
+ "item/commandExecution/outputDelta",
203
+ "item/fileChange/outputDelta",
204
+ "item/plan/delta",
205
+ "thread/tokenUsage/updated",
206
+ ];
207
+ /** What the runner says of itself when it begins: nothing of the product's that is still an experiment is asked for. */
208
+ const hello = (version) => ({
209
+ clientInfo: {
210
+ name: "skillcdn-console-runner",
211
+ title: "SkillCDN Console runner",
212
+ version: version ?? "0.0.0",
213
+ },
214
+ capabilities: { experimentalApi: false, optOutNotificationMethods: UNREAD },
215
+ });
216
+ /**
217
+ * A path the product is handed as a picture: one the runner wrote under the desk's
218
+ * `work/inbox/`, and no other file of the machine.
100
219
  */
101
220
  const PICTURE_PATH = /^work\/inbox\/[1-9]\d{0,15}\/([^/\\]{1,200})$/;
102
221
  const isPicturePath = (path) => {
103
222
  const name = PICTURE_PATH.exec(path)?.[1];
104
223
  return name !== undefined && !/^\.+$/.test(name);
105
224
  };
106
- const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
107
- const textOf = (value) => typeof value === "string" ? value : undefined;
108
- /** One line of the product's output as an object with a type, or nothing for anything else. */
109
- function lineOf(line) {
110
- if (line.trim().length === 0) {
111
- return undefined;
112
- }
113
- try {
114
- const parsed = JSON.parse(line);
115
- return isObject(parsed) && typeof parsed.type === "string" ? parsed : undefined;
116
- }
117
- catch {
118
- return undefined;
119
- }
225
+ /**
226
+ * Messages as the input of a turn: what arrived together is read together, each under its own
227
+ * header, and every picture of it goes with it, as a file by its path in the desk.
228
+ */
229
+ function inputOf(messages, desk) {
230
+ const root = desk.replace(/[\\/]+$/, "");
231
+ return [
232
+ { type: "text", text: messages.map((message) => message.text).join("\n\n"), text_elements: [] },
233
+ ...messages.flatMap((message) => message.images.flatMap((picture) => picture.path !== undefined && isPicturePath(picture.path)
234
+ ? [{ type: "localImage", path: `${root}/${picture.path}` }]
235
+ : [])),
236
+ ];
120
237
  }
121
238
  /** Anything as a line of text a person reads, bounded. */
122
239
  function shown(value, cut) {
@@ -131,44 +248,62 @@ function shown(value, cut) {
131
248
  }
132
249
  }
133
250
  /**
134
- * What an error of the product says. It writes one as the answer of the service it asked, a
135
- * line of JSON inside its line, where the service answered; the words are read out of it.
251
+ * What an error of the product says: its message, with what it carries beside it. It may write
252
+ * the message as the answer of the service it asked, a line of JSON, where the service
253
+ * answered; the words are read out of it.
136
254
  */
137
- function errorOf(message) {
138
- const text = textOf(message) ?? "";
255
+ function errorOf(error) {
256
+ const said = isObject(error) ? error : {};
257
+ let message = textOf(said.message)?.trim() ?? "";
139
258
  try {
140
- const parsed = JSON.parse(text);
259
+ const parsed = JSON.parse(message);
141
260
  if (isObject(parsed) && isObject(parsed.error) && typeof parsed.error.message === "string") {
142
- return parsed.error.message;
261
+ message = parsed.error.message;
143
262
  }
144
263
  }
145
264
  catch {
146
265
  // Words, and not an answer of the service: said as they are.
147
266
  }
148
- return text;
267
+ const more = textOf(said.additionalDetails)?.trim() ?? "";
268
+ if (more.length === 0 || message.includes(more)) {
269
+ return message;
270
+ }
271
+ return message.length === 0 ? more : `${message}\n${more}`;
272
+ }
273
+ /**
274
+ * Whether an error says that the product is not signed in: in the product's own word for it,
275
+ * by the status the service refused it with, or in its words.
276
+ */
277
+ function signedOut(error) {
278
+ const info = isObject(error) ? error.codexErrorInfo : undefined;
279
+ const refused = isObject(info) &&
280
+ Object.values(info).some((one) => isObject(one) && one.httpStatusCode === 401);
281
+ return info === "unauthorized" || refused || SIGN_IN.test(errorOf(error));
149
282
  }
150
283
  /** A tool call of the product, by the kind of item it writes for one: its name and its input as a line. */
151
284
  function callOf(item, cut) {
152
285
  switch (item.type) {
153
- case "command_execution":
286
+ case "commandExecution":
154
287
  return { tool: "command_execution", text: shown(item.command ?? "", cut) };
155
- case "file_change": {
288
+ case "fileChange": {
156
289
  const changes = Array.isArray(item.changes) ? item.changes : [];
157
290
  return {
158
291
  tool: "file_change",
159
292
  text: cut(changes
160
293
  .filter(isObject)
161
- .map((change) => `${textOf(change.kind) ?? "change"} ${textOf(change.path) ?? ""}`)
294
+ .map((change) => {
295
+ const kind = isObject(change.kind) ? textOf(change.kind.type) : textOf(change.kind);
296
+ return `${kind ?? "change"} ${textOf(change.path) ?? ""}`;
297
+ })
162
298
  .join("\n"), MAX_INPUT_LENGTH),
163
299
  };
164
300
  }
165
- // The two below are in the shape the product's documentation gives them: not seen run.
166
- case "mcp_tool_call":
301
+ case "mcpToolCall":
167
302
  return {
168
303
  tool: `${textOf(item.server) ?? "mcp"}.${textOf(item.tool) ?? "tool"}`,
169
304
  text: item.arguments === undefined ? "" : shown(item.arguments, cut),
170
305
  };
171
- case "web_search":
306
+ case "webSearch":
172
307
  return { tool: "web_search", text: shown(item.query ?? "", cut) };
173
308
  default:
174
309
  return undefined;
@@ -176,10 +311,10 @@ function callOf(item, cut) {
176
311
  }
177
312
  /** What came back from a tool call, where the product says: a command's output, or what a tool answered. */
178
313
  function resultOf(item, cut) {
179
- if (item.type === "command_execution") {
180
- return textOf(item.aggregated_output) ?? "";
314
+ if (item.type === "commandExecution") {
315
+ return textOf(item.aggregatedOutput) ?? "";
181
316
  }
182
- if (item.type === "mcp_tool_call") {
317
+ if (item.type === "mcpToolCall") {
183
318
  if (isObject(item.error) && typeof item.error.message === "string") {
184
319
  return item.error.message;
185
320
  }
@@ -187,146 +322,20 @@ function resultOf(item, cut) {
187
322
  }
188
323
  return "";
189
324
  }
190
- /** The events of one line of the product's output. `read` is what the lines before it left. */
191
- function eventsOf(line, read, runs) {
192
- const events = [];
193
- const item = isObject(line.item) ? line.item : undefined;
194
- switch (line.type) {
195
- case "thread.started": {
196
- // Written at the start of every turn: said on once, and again where it is something else.
197
- const said = JSON.stringify([runs.model, runs.effort]);
198
- const conversation = textOf(line.thread_id);
199
- if (said !== read.runs) {
200
- // The product names neither its model nor its version here: what it was asked for.
201
- events.push({ type: "running", model: runs.model, effort: runs.effort, version: null });
202
- }
203
- if (conversation !== undefined && conversation !== read.conversation) {
204
- events.push({ type: "conversation", id: conversation });
205
- }
206
- if (!read.began) {
207
- events.push({
208
- type: "event",
209
- kind: "status",
210
- text: "The product started.",
211
- data: { model: runs.model, effort: runs.effort, version: null, mode: runs.mode },
212
- });
213
- }
214
- read.began = true;
215
- read.runs = said;
216
- read.conversation = conversation ?? read.conversation;
217
- return events;
218
- }
219
- case "item.started": {
220
- const call = item === undefined ? undefined : callOf(item, read.cut);
221
- const id = textOf(item?.id);
222
- if (call !== undefined) {
223
- events.push({
224
- type: "event",
225
- kind: "tool_call",
226
- text: call.text,
227
- data: { tool: call.tool, id: id ?? null },
228
- });
229
- if (id !== undefined) {
230
- read.called.add(id);
231
- }
232
- }
233
- return events;
234
- }
235
- case "item.completed": {
236
- if (item === undefined) {
237
- return events;
238
- }
239
- const id = textOf(item.id);
240
- if (item.type === "agent_message" && typeof item.text === "string") {
241
- events.push({ type: "event", kind: "assistant", text: item.text });
242
- }
243
- else if (item.type === "reasoning" && typeof item.text === "string") {
244
- // As the product's documentation has it: none was written in the runs it was tried in.
245
- events.push({ type: "event", kind: "thinking", text: item.text });
246
- }
247
- else if (item.type === "error") {
248
- // A word of warning inside a turn: the turn goes on.
249
- events.push({ type: "event", kind: "error", text: errorOf(item.message) });
250
- }
251
- else {
252
- const call = callOf(item, read.cut);
253
- if (call === undefined) {
254
- return events;
255
- }
256
- // A call the product wrote no beginning of is said with its end.
257
- if (id === undefined || !read.called.delete(id)) {
258
- events.push({
259
- type: "event",
260
- kind: "tool_call",
261
- text: call.text,
262
- data: { tool: call.tool, id: id ?? null },
263
- });
264
- }
265
- if (item.type === "web_search") {
266
- return events;
267
- }
268
- const exit = typeof item.exit_code === "number" ? item.exit_code : null;
269
- const data = {
270
- id: id ?? null,
271
- error: item.status === "failed" || (exit !== null && exit !== 0),
272
- };
273
- if (exit !== null) {
274
- data.exit = exit;
275
- }
276
- events.push({
277
- type: "event",
278
- kind: "tool_result",
279
- text: resultOf(item, read.cut),
280
- data,
281
- pictures: [],
282
- });
283
- }
284
- return events;
285
- }
286
- case "turn.started":
287
- // The turn's messages are the product's from here on: said once for each.
288
- if (!read.turned) {
289
- for (let taken = 0; taken < read.messages; taken += 1) {
290
- events.push({ type: "taken" });
291
- }
292
- }
293
- read.turned = true;
294
- return events;
295
- case "turn.completed":
296
- read.completed = true;
297
- return events;
298
- case "turn.failed":
299
- case "error": {
300
- const said = errorOf(line.type === "error" ? line.message : isObject(line.error) ? line.error.message : "");
301
- if (line.type === "turn.failed") {
302
- read.failed = said;
303
- }
304
- // The product writes what failed a turn twice: as an error, and as the turn's failing.
305
- if (said !== read.error) {
306
- events.push({
307
- type: "event",
308
- kind: "error",
309
- text: said.length === 0 ? "The turn ended with an error." : said,
310
- });
311
- }
312
- read.error = said;
313
- return events;
314
- }
315
- default:
316
- return events;
317
- }
318
- }
319
325
  const wait = (ms) => new Promise((resolve) => {
320
326
  setTimeout(() => resolve("timeout"), ms).unref?.();
321
327
  });
322
- /** Ends a process: asked first, then killed. */
328
+ /** Ends a process: its input closed first, then asked, then made to. */
323
329
  async function endOf(child) {
324
- child.kill("SIGTERM");
325
- if ((await Promise.race([child.exited, wait(KILL_GRACE_MS)])) === "timeout") {
326
- child.kill("SIGKILL");
330
+ child.end();
331
+ if ((await Promise.race([child.exited, wait(STOP_GRACE_MS)])) === "timeout") {
332
+ child.kill("SIGTERM");
333
+ if ((await Promise.race([child.exited, wait(KILL_GRACE_MS)])) === "timeout") {
334
+ child.kill("SIGKILL");
335
+ }
327
336
  }
328
337
  }
329
- function sessionOf(host, options) {
338
+ function sessionOf(child, host, options) {
330
339
  // What the session says, in order, for whoever reads it: until it ends.
331
340
  const said = [];
332
341
  let reader;
@@ -350,154 +359,607 @@ function sessionOf(host, options) {
350
359
  }
351
360
  }
352
361
  }
353
- const read = {
354
- began: false,
355
- runs: undefined,
356
- conversation: undefined,
357
- called: new Set(),
358
- error: undefined,
359
- completed: false,
360
- failed: undefined,
361
- turned: false,
362
- messages: 0,
363
- cut: options.cut ?? plainCut,
364
- };
362
+ const cut = options.cut ?? plainCut;
363
+ const connection = connectionOf(child, { cut, header: false });
365
364
  const mode = codexMode(options.permissions);
366
- let conversation = word(options.resume);
367
365
  let choice = { model: options.model, effort: options.effort };
368
- /** The turn under way: its process, why it was ended where it was, and when it is read to its end. */
366
+ /** What the conversation runs with, as the product said it or as it was last asked. */
367
+ let runs = { model: null, effort: null };
368
+ let version = null;
369
+ /** The conversation, once it is open. */
370
+ let thread;
371
+ /** Whether the product answered at all, whether the conversation is open, and whether the session gave up or is being stopped. */
372
+ let began = false;
373
+ let ready = false;
374
+ let gaveUp = false;
375
+ let stopping = false;
376
+ /** Whether the product's output closed: nothing more reaches a process that is gone. */
377
+ let over = false;
369
378
  let turn;
370
- /** The messages that arrived while a turn ran: the next turn's. */
379
+ /** The messages the product was not handed yet: the next turn's, or the next put into the one under way. */
371
380
  let waiting = [];
372
- let over = false;
373
- const run = (messages) => {
374
- const child = host.spawn(host.command, codexArguments({
375
- ...choice,
376
- permissions: options.permissions,
377
- conversation,
378
- // What arrived together is one turn's: every picture of it goes with the turn.
379
- images: messages.flatMap((message) => message.images.flatMap((picture) => (picture.path === undefined ? [] : [picture.path]))),
380
- }), { cwd: options.cwd, env: options.env });
381
- const mine = { child, cut: undefined, done: Promise.resolve() };
382
- const runs = {
383
- model: word(choice.model) ?? null,
384
- effort: word(choice.effort) ?? null,
385
- mode,
386
- };
387
- read.completed = false;
388
- read.failed = undefined;
389
- read.turned = false;
390
- read.messages = messages.length;
391
- read.error = undefined;
392
- read.called.clear();
381
+ let handed = [];
382
+ /** Whether a request that hands messages over is on its way: they are handed over one request at a time, in order. */
383
+ let busy = false;
384
+ /** The tool calls said already, by the product's id for each. */
385
+ const called = new Set();
386
+ /** The last error said, which the product writes twice where a turn fails of it. */
387
+ let lastError;
388
+ /**
389
+ * Whether the product says a message back under the id it was handed over with: a build
390
+ * known to, or one that did. Until then none is put into a turn, since one that is read and
391
+ * never said back would be handed over again with the next turn.
392
+ */
393
+ let saysBack = false;
394
+ /** The windows of the allowance as the product last said them, by name: an update says some of them. */
395
+ const allowance = new Map();
396
+ const taken = (count) => Array.from({ length: count }, () => ({ type: "taken" }));
397
+ const sayError = (words) => {
398
+ if (words !== lastError) {
399
+ emit({
400
+ type: "event",
401
+ kind: "error",
402
+ text: words.length === 0 ? "The turn ended with an error." : words,
403
+ });
404
+ }
405
+ lastError = words;
406
+ };
407
+ /** Asks the product to stop a turn: once it said which turn it is. */
408
+ const interruptOf = (one) => {
409
+ if (one.id === undefined || thread === undefined) {
410
+ return;
411
+ }
412
+ // Answered when the turn is over, which the product says by itself too.
413
+ void connection
414
+ .request("turn/interrupt", { threadId: thread, turnId: one.id })
415
+ .catch(() => undefined);
416
+ };
417
+ const stopTurn = (why) => {
418
+ if (turn === undefined || turn.cut !== undefined) {
419
+ return;
420
+ }
421
+ turn.cut = why;
422
+ interruptOf(turn);
423
+ };
424
+ /** Starts a turn with what waited: one message, each part under its own header. */
425
+ const start = async (messages) => {
426
+ const mine = { id: undefined, cut: undefined, steers: true };
427
+ const entry = { id: host.uuid(), messages, steered: false };
393
428
  turn = mine;
394
- mine.done = (async () => {
395
- try {
396
- // One message: what arrived together is read together, each under its own header.
397
- await child.write(messages.map((message) => message.text).join("\n\n"));
398
- }
399
- catch {
400
- // A process that is gone takes nothing: how it ended says the rest.
401
- }
402
- child.end();
403
- try {
404
- for await (const text of child.lines) {
405
- const line = lineOf(text);
406
- if (line !== undefined) {
407
- emit(...eventsOf(line, read, runs));
408
- }
429
+ handed.push(entry);
430
+ lastError = undefined;
431
+ // Said with the turn, and kept by the conversation from then on. A choice left at the
432
+ // product's own is not said: the conversation goes on with what it ran with.
433
+ const model = word(choice.model);
434
+ const effort = word(choice.effort);
435
+ if ((model !== undefined && model !== runs.model) ||
436
+ (effort !== undefined && effort !== runs.effort)) {
437
+ runs = { model: model ?? runs.model, effort: effort ?? runs.effort };
438
+ emit({ type: "running", model: runs.model, effort: runs.effort, version });
439
+ }
440
+ try {
441
+ const result = await connection.request("turn/start", {
442
+ threadId: thread ?? "",
443
+ clientUserMessageId: entry.id,
444
+ input: inputOf(messages, options.cwd),
445
+ ...(model === undefined ? {} : { model }),
446
+ ...(effort === undefined ? {} : { effort }),
447
+ });
448
+ if (turn === mine) {
449
+ mine.id ??= isObject(result) && isObject(result.turn) ? textOf(result.turn.id) : undefined;
450
+ // A stop that was asked before the product named the turn is said now.
451
+ if (mine.cut !== undefined) {
452
+ interruptOf(mine);
409
453
  }
410
454
  }
411
- catch {
412
- // The output closed in the middle of a line: how the process ended says the rest.
455
+ }
456
+ catch (error) {
457
+ if (turn === mine) {
458
+ turn = undefined;
413
459
  }
414
- const exit = await child.exited;
415
- conversation = word(read.conversation) ?? conversation;
416
- turn = undefined;
417
- if (mine.cut === "ended") {
460
+ handed = handed.filter((one) => one !== entry);
461
+ if (!(error instanceof RpcError)) {
462
+ // The process is gone: how it ended is said where its output ends.
463
+ return;
464
+ }
465
+ emit({
466
+ type: "event",
467
+ kind: "error",
468
+ text: `The product refused the turn: ${error.message}`,
469
+ });
470
+ if (SIGN_IN.test(error.message)) {
471
+ // Nothing was taken: the runner sends again what it sent.
472
+ waiting = [];
473
+ handed = [];
474
+ emit({ type: "cannot_go_on", state: "failed", reason: "sign_in" });
418
475
  return;
419
476
  }
420
- if (mine.cut !== undefined) {
477
+ // A turn the product itself refused is a turn that ended: it takes the next message.
478
+ emit(...taken(messages.length));
479
+ done();
480
+ }
481
+ };
482
+ /**
483
+ * Puts a message into the turn under way. The product answers that it was put in, and says
484
+ * the message back when it reads it, after the step under way: that is when it is taken.
485
+ */
486
+ const steer = async (message, into) => {
487
+ const entry = { id: host.uuid(), messages: [message], steered: true };
488
+ handed.push(entry);
489
+ try {
490
+ await connection.request("turn/steer", {
491
+ threadId: thread ?? "",
492
+ expectedTurnId: into.id ?? "",
493
+ clientUserMessageId: entry.id,
494
+ input: inputOf([message], options.cwd),
495
+ });
496
+ }
497
+ catch {
498
+ // Not put in: the turn is over, or is one a message is put into none of. It waits for
499
+ // the turn's end, first of what waits, unless that end came first and has it wait already.
500
+ into.steers = false;
501
+ if (handed.includes(entry)) {
502
+ handed = handed.filter((one) => one !== entry);
503
+ waiting.unshift(message);
504
+ }
505
+ }
506
+ };
507
+ /** Hands over what waits, one request at a time: a turn started, or a message put into the one under way. */
508
+ const pump = () => {
509
+ if (busy || !ready || gaveUp || stopping || over) {
510
+ return;
511
+ }
512
+ const head = waiting[0];
513
+ if (head === undefined) {
514
+ return;
515
+ }
516
+ const after = () => {
517
+ busy = false;
518
+ pump();
519
+ };
520
+ if (turn === undefined) {
521
+ busy = true;
522
+ void start(waiting.splice(0)).finally(after);
523
+ return;
524
+ }
525
+ // A turn that is being stopped, or takes no message, is waited for: its end hands over.
526
+ // So is a turn of a product that has not yet said a message back.
527
+ if (turn.cut !== undefined || !turn.steers || turn.id === undefined || !saysBack) {
528
+ return;
529
+ }
530
+ busy = true;
531
+ waiting.shift();
532
+ void steer(head, turn).finally(after);
533
+ };
534
+ /** A turn ended: the agent is done when nothing it was sent still waits. */
535
+ const done = () => {
536
+ // A session that is being stopped says nothing more of a turn: what it was handed and
537
+ // did not take is the runner's to settle, and a turn said completed here would have the
538
+ // runner count as read what the product never got.
539
+ if (stopping) {
540
+ return;
541
+ }
542
+ if (waiting.length > 0) {
543
+ pump();
544
+ }
545
+ else {
546
+ emit({ type: "turn_completed" });
547
+ }
548
+ };
549
+ /** The product said a message back: it read it, and the message is taken. */
550
+ const took = (id) => {
551
+ const entry = handed.find((one) => one.id === id);
552
+ if (entry !== undefined) {
553
+ saysBack = true;
554
+ handed = handed.filter((one) => one !== entry);
555
+ emit(...taken(entry.messages.length));
556
+ }
557
+ // The message a turn began with, said back: the turn is under way, and what waited for
558
+ // the product to say one back is put into it now.
559
+ pump();
560
+ };
561
+ const onItem = (params, completed) => {
562
+ const item = isObject(params.item) ? params.item : undefined;
563
+ if (item === undefined) {
564
+ return;
565
+ }
566
+ const id = textOf(item.id);
567
+ if (item.type === "userMessage") {
568
+ took(textOf(item.clientId));
569
+ return;
570
+ }
571
+ if (!completed) {
572
+ const call = callOf(item, cut);
573
+ if (call !== undefined) {
421
574
  emit({
422
575
  type: "event",
423
- kind: "status",
424
- text: mine.cut === "message"
425
- ? "The turn was stopped for a message."
426
- : "The turn was stopped.",
576
+ kind: "tool_call",
577
+ text: call.text,
578
+ data: { tool: call.tool, id: id ?? null },
427
579
  });
580
+ if (id !== undefined) {
581
+ called.add(id);
582
+ }
428
583
  }
429
- else if (exit.error !== undefined) {
430
- emit({ type: "event", kind: "error", text: notStarted(exit.error, host.command) }, { type: "cannot_go_on", state: "failed", reason: "start" });
431
- // Nothing was taken: the runner sends again what it sent.
584
+ return;
585
+ }
586
+ if (item.type === "agentMessage") {
587
+ if (typeof item.text === "string" && item.text.length > 0) {
588
+ emit({ type: "event", kind: "assistant", text: item.text });
589
+ }
590
+ return;
591
+ }
592
+ if (item.type === "reasoning") {
593
+ const parts = [item.summary, item.content].flatMap((one) => Array.isArray(one) ? one.filter((part) => typeof part === "string") : []);
594
+ if (parts.length > 0) {
595
+ emit({ type: "event", kind: "thinking", text: parts.join("\n\n") });
596
+ }
597
+ return;
598
+ }
599
+ const call = callOf(item, cut);
600
+ if (call === undefined) {
601
+ return;
602
+ }
603
+ // A call the product wrote no beginning of is said with its end.
604
+ if (id === undefined || !called.delete(id)) {
605
+ emit({
606
+ type: "event",
607
+ kind: "tool_call",
608
+ text: call.text,
609
+ data: { tool: call.tool, id: id ?? null },
610
+ });
611
+ }
612
+ if (item.type === "webSearch") {
613
+ return;
614
+ }
615
+ const exit = typeof item.exitCode === "number" ? item.exitCode : null;
616
+ const data = {
617
+ id: id ?? null,
618
+ // Declined is the product's word for a call its sandbox or its reviewer did not let run.
619
+ error: item.status === "failed" || item.status === "declined" || (exit !== null && exit !== 0),
620
+ };
621
+ if (exit !== null) {
622
+ data.exit = exit;
623
+ }
624
+ emit({ type: "event", kind: "tool_result", text: resultOf(item, cut), data, pictures: [] });
625
+ };
626
+ const onTurnCompleted = (params) => {
627
+ const ended = isObject(params.turn) ? params.turn : {};
628
+ const id = textOf(ended.id);
629
+ if (turn === undefined || (turn.id !== undefined && id !== undefined && turn.id !== id)) {
630
+ return;
631
+ }
632
+ const mine = turn;
633
+ turn = undefined;
634
+ called.clear();
635
+ if (stopping) {
636
+ return;
637
+ }
638
+ // What the product did not say it took. A turn began with its first message, which is the
639
+ // product's from then on. One put into the turn and never said back was not read: the
640
+ // product lets go of it where the turn is stopped first (seen with 0.139.0), so it is the
641
+ // next turn's, before whatever else waits.
642
+ const again = [];
643
+ for (const one of handed) {
644
+ if (one.steered) {
645
+ again.push(...one.messages);
646
+ }
647
+ else {
648
+ emit(...taken(one.messages.length));
649
+ }
650
+ }
651
+ handed = [];
652
+ waiting = [...again, ...waiting];
653
+ if (ended.status === "failed") {
654
+ sayError(errorOf(ended.error));
655
+ if (signedOut(ended.error)) {
656
+ // The runner sends again what it sent, once the product is signed in.
432
657
  waiting = [];
658
+ emit({ type: "cannot_go_on", state: "failed", reason: "sign_in" });
433
659
  return;
434
660
  }
435
- else if (!read.completed) {
436
- const errors = child.errors().trim();
437
- if (read.failed === undefined) {
438
- // Its end, cut after the secrets are out, so that none stands at the cut.
439
- const shown = errorsShown(errors, options.cutEnd ?? plainCutEnd);
440
- emit({
441
- type: "event",
442
- kind: "error",
443
- text: `The product's process ended${exit.signal === null ? ` with code ${exit.code}` : ` on ${exit.signal}`} before the turn did.${shown.length === 0 ? "" : `\n${shown}`}`,
444
- });
445
- }
446
- if (SIGN_IN.test(`${read.failed ?? ""}\n${errors}`)) {
447
- emit({ type: "cannot_go_on", state: "failed", reason: "sign_in" });
448
- waiting = [];
449
- return;
661
+ if (usageLimited(ended.error)) {
662
+ // Nothing goes better before a window resets: the runner waits, brings the agent back
663
+ // by itself, and hands over again what was not answered. Until when is the reset of
664
+ // the window the product last said is used up, where it said one.
665
+ const resets = [...allowance.values()]
666
+ .filter((window) => window.used !== undefined && window.used >= 1)
667
+ .flatMap((window) => (window.resetsAt === undefined ? [] : [window.resetsAt]));
668
+ waiting = [];
669
+ emit({
670
+ type: "cannot_go_on",
671
+ state: "failed",
672
+ reason: "usage_limit",
673
+ ...(resets.length === 0 ? {} : { until: Math.max(...resets) }),
674
+ });
675
+ return;
676
+ }
677
+ // A turn the product itself said failed is a turn that ended: the agent takes the next message.
678
+ }
679
+ else if (ended.status === "interrupted") {
680
+ emit({
681
+ type: "event",
682
+ kind: "status",
683
+ text: mine.cut === "message" ? "The turn was stopped for a message." : "The turn was stopped.",
684
+ });
685
+ }
686
+ done();
687
+ };
688
+ const onNotification = (method, params) => {
689
+ // The allowance is the account's, and of no thread: said on as the product says it, for
690
+ // the runner to tell its agent by.
691
+ if (method === "account/rateLimits/updated") {
692
+ // Said on where it says something new: the product writes it several times a turn
693
+ // (seen with 0.171.0, six times in a turn of one command, the same each time).
694
+ const windows = allowanceOf(params.rateLimits).filter((window) => {
695
+ const before = allowance.get(window.name);
696
+ return before?.used !== window.used || before?.resetsAt !== window.resetsAt;
697
+ });
698
+ for (const window of windows) {
699
+ allowance.set(window.name, window);
700
+ }
701
+ if (windows.length > 0 && !stopping) {
702
+ emit({ type: "usage", windows });
703
+ }
704
+ return;
705
+ }
706
+ // A thread of the product's other than the conversation's is none of the session's: what
707
+ // an agent it started does there is that agent's, and its turns are not this one's.
708
+ if (thread === undefined || params.threadId !== thread) {
709
+ return;
710
+ }
711
+ switch (method) {
712
+ case "turn/started": {
713
+ const id = isObject(params.turn) ? textOf(params.turn.id) : undefined;
714
+ if (turn === undefined) {
715
+ // A turn the product began by itself: a message that arrives now is put into it.
716
+ turn = { id, cut: undefined, steers: true };
717
+ pump();
450
718
  }
451
- if (read.failed === undefined) {
452
- // The product fell over without a word of the turn: nothing says that the next one
453
- // goes better, a conversation it cannot find again say (seen with 0.164.0), so the
454
- // agent cannot go on by itself. Before a turn started nothing was taken, and the
455
- // runner sends again what it sent; after, what the turn was at is not known.
456
- emit({
457
- type: "cannot_go_on",
458
- state: "failed",
459
- reason: read.turned ? "crashed" : "start",
460
- });
461
- waiting = [];
462
- return;
719
+ else {
720
+ turn.id ??= id;
463
721
  }
464
- // A turn the product itself said failed is a turn that ended: the agent takes the next message.
722
+ return;
465
723
  }
466
- // The agent is done when nothing it was sent still waits: what arrived meanwhile is its next turn.
467
- if (waiting.length > 0) {
468
- const next = waiting;
469
- waiting = [];
470
- run(next);
724
+ case "turn/completed":
725
+ onTurnCompleted(params);
726
+ return;
727
+ case "item/started":
728
+ onItem(params, false);
729
+ return;
730
+ case "item/completed":
731
+ onItem(params, true);
732
+ return;
733
+ case "error":
734
+ // A word of warning inside a turn, a try that is made again say: the turn goes on, and
735
+ // says by itself where it ends of one.
736
+ sayError(errorOf(params.error));
737
+ return;
738
+ default:
739
+ // Whatever else it writes, how the thread stands, what it plans, what its reviewer
740
+ // found, is left where it is.
741
+ return;
742
+ }
743
+ };
744
+ /** What the product asks of whoever is at its terminal: nobody is, so it is refused and recorded. */
745
+ const onRequest = (id, method, params) => {
746
+ const denied = (tool, what, answer) => {
747
+ const data = { tool, answer: "denied" };
748
+ const item = textOf(params.itemId);
749
+ if (item !== undefined) {
750
+ data.id = item;
471
751
  }
472
- else {
473
- emit({ type: "turn_completed" });
752
+ emit({
753
+ type: "event",
754
+ kind: "permission",
755
+ text: `Denied, with nobody at the terminal to ask: ${cut(what, MAX_INPUT_LENGTH)}`,
756
+ data,
757
+ });
758
+ void connection.answer(id, answer).catch(() => undefined);
759
+ };
760
+ const reason = textOf(params.reason)?.trim();
761
+ switch (method) {
762
+ case "item/commandExecution/requestApproval":
763
+ denied("command_execution", textOf(params.command) ?? reason ?? "a command", {
764
+ decision: "decline",
765
+ });
766
+ return;
767
+ case "item/fileChange/requestApproval":
768
+ denied("file_change", reason ?? "a change to files", { decision: "decline" });
769
+ return;
770
+ case "item/permissions/requestApproval":
771
+ // Leave to do more than the mode allows: none is given.
772
+ denied("permissions", reason ?? "more than the mode allows", {
773
+ permissions: {},
774
+ scope: "turn",
775
+ });
776
+ return;
777
+ case "item/tool/requestUserInput":
778
+ denied("request_user_input", "a question for a person", { answers: {} });
779
+ return;
780
+ case "mcpServer/elicitation/request":
781
+ denied(`${textOf(params.serverName) ?? "mcp"}.elicitation`, textOf(params.message) ?? "a question for a person", { action: "decline", content: null, _meta: null });
782
+ return;
783
+ default:
784
+ // No tool and no sign-in of the runner's own was offered: a product that asks for one
785
+ // all the same is told.
786
+ void connection
787
+ .refuse(id, METHOD_NOT_FOUND, `${method} is not offered`)
788
+ .catch(() => undefined);
789
+ }
790
+ };
791
+ /** Why the conversation could not open, said once; the process is ended, and nothing more is said of it. */
792
+ const cannotStart = (text, reason) => {
793
+ gaveUp = true;
794
+ waiting = [];
795
+ emit({ type: "event", kind: "error", text }, { type: "cannot_go_on", state: "failed", reason });
796
+ void endOf(child);
797
+ };
798
+ const open = async () => {
799
+ const greeted = await connection.request("initialize", hello(host.version));
800
+ began = true;
801
+ // The build that runs, from what it calls itself: a conversation says the build that began
802
+ // it, which is another once the product was updated.
803
+ const build = codexBuild(isObject(greeted) ? greeted.userAgent : undefined);
804
+ version = build?.text ?? null;
805
+ if (build !== undefined && tooOld(build.numbers)) {
806
+ cannotStart(`This Codex is ${build.text}, and the driver speaks to ${OLDEST.join(".")} or later, whose app server says a message back when it reads it. Update it, as it was installed.`, "start");
807
+ return;
808
+ }
809
+ // A build that says which it is, and is none of the old ones, says a message back; one
810
+ // that says nothing of itself is believed once it did.
811
+ saysBack = build !== undefined;
812
+ await connection.notify("initialized");
813
+ const model = word(choice.model);
814
+ const settings = {
815
+ cwd: options.cwd,
816
+ ...modeSettings(mode),
817
+ ...(model === undefined ? {} : { model }),
818
+ };
819
+ let opened;
820
+ const resume = word(options.resume);
821
+ if (resume !== undefined) {
822
+ try {
823
+ // Gone on with, without its history said again: nobody reads it here.
824
+ opened = await connection
825
+ .request("thread/resume", { threadId: resume, ...settings, excludeTurns: true })
826
+ .catch((error) => {
827
+ // A build that takes that word only as an experiment (0.139.0) is asked without it.
828
+ if (error instanceof RpcError && error.message.includes("excludeTurns")) {
829
+ return connection.request("thread/resume", { threadId: resume, ...settings });
830
+ }
831
+ throw error;
832
+ });
474
833
  }
475
- })();
834
+ catch (error) {
835
+ // Not signed in is said as that, and a process that is gone where its output ends.
836
+ if (!(error instanceof RpcError) || SIGN_IN.test(error.message)) {
837
+ throw error;
838
+ }
839
+ // A conversation the product has no more: said, and a new one begun, as the runner
840
+ // begins one where the conversation kept was another product's.
841
+ emit({
842
+ type: "event",
843
+ kind: "status",
844
+ text: `The product could not go on with the conversation it had (${error.message}): a new one begins.`,
845
+ });
846
+ }
847
+ }
848
+ opened ??= await connection.request("thread/start", settings);
849
+ const said = isObject(opened) ? opened : {};
850
+ const named = isObject(said.thread) ? said.thread : {};
851
+ thread = word(textOf(named.id));
852
+ if (thread === undefined) {
853
+ cannotStart("The product opened a conversation and did not say which.", "start");
854
+ return;
855
+ }
856
+ // The sandbox the product says the conversation runs in. More than was asked is never
857
+ // gone on with; less is said, since it is what the agent does without asking: a product
858
+ // whose own sandbox is not set up on the machine answers its read-only one (seen on
859
+ // Windows with 0.171.0, with a home it had set nothing up in).
860
+ const asked = sandboxOf(mode);
861
+ const answered = isObject(said.sandbox) ? textOf(said.sandbox.type) : undefined;
862
+ const width = (name, at) => SANDBOXES.findIndex((one) => one[at] === name);
863
+ const sandbox = SANDBOXES[width(answered, 0)]?.[1];
864
+ // One the driver does not know by its word is said as it came: how much it allows is not
865
+ // known here, and saying nothing would read as the sandbox that was asked for.
866
+ const unknown = sandbox === undefined ? word(answered) : undefined;
867
+ if (sandbox !== undefined && width(sandbox, 1) > width(asked, 1)) {
868
+ cannotStart(`The product opened the conversation in its sandbox ${sandbox}, which allows more than ${asked}, the one it was asked for.`, "start");
869
+ return;
870
+ }
871
+ runs = {
872
+ model: word(textOf(said.model)) ?? model ?? null,
873
+ effort: word(textOf(said.reasoningEffort)) ?? null,
874
+ };
875
+ emit({ type: "conversation", id: thread }, { type: "running", model: runs.model, effort: runs.effort, version }, {
876
+ type: "event",
877
+ kind: "status",
878
+ text: unknown !== undefined
879
+ ? `The product started, in a sandbox the driver does not know, ${unknown}, and not in ${asked}, which it was asked for.`
880
+ : sandbox === undefined || sandbox === asked
881
+ ? "The product started."
882
+ : `The product started, in its sandbox ${sandbox} and not in ${asked}, which it was asked for.`,
883
+ data: {
884
+ model: runs.model,
885
+ effort: runs.effort,
886
+ version,
887
+ mode,
888
+ ...(unknown !== undefined
889
+ ? { sandbox: unknown }
890
+ : sandbox === undefined || sandbox === asked
891
+ ? {}
892
+ : { sandbox }),
893
+ },
894
+ });
895
+ ready = true;
896
+ pump();
476
897
  };
898
+ void (async () => {
899
+ const reading = connection.read({ notification: onNotification, request: onRequest });
900
+ const opening = open().catch((error) => {
901
+ if (gaveUp || stopping) {
902
+ return;
903
+ }
904
+ if (error instanceof RpcError) {
905
+ const signIn = SIGN_IN.test(error.message);
906
+ cannotStart(signIn
907
+ ? `The product is not signed in: ${error.message}`
908
+ : `The product did not open a conversation: ${error.message}`, signIn ? "sign_in" : "start");
909
+ }
910
+ // Anything else is a process that is gone: how it ended is said where its output ends.
911
+ });
912
+ void wait(START_TIMEOUT_MS).then(() => {
913
+ if (!ready && !gaveUp && !stopping) {
914
+ cannotStart("The product did not open a conversation in time.", "start");
915
+ }
916
+ });
917
+ await reading;
918
+ over = true;
919
+ await opening;
920
+ const exit = await child.exited;
921
+ const clean = stopping || (exit.code === 0 && exit.signal === null && exit.error === undefined);
922
+ if (!clean && !gaveUp) {
923
+ const errors = child.errors().trim();
924
+ // Its end, cut after the secrets are out, so that none stands at the cut.
925
+ const shown = errorsShown(errors, options.cutEnd ?? plainCutEnd);
926
+ const how = exit.signal === null ? ` with code ${exit.code}` : ` on ${exit.signal}`;
927
+ emit({
928
+ type: "event",
929
+ kind: "error",
930
+ text: exit.error !== undefined
931
+ ? notStarted(exit.error, host.command)
932
+ : `The product's process ended${how}${began ? "" : " before it answered as its app server, which this driver speaks to (codex app-server)"}.${shown.length === 0 ? "" : `\n${shown}`}`,
933
+ }, {
934
+ type: "cannot_go_on",
935
+ state: "failed",
936
+ // Before the conversation was open nothing was taken, and the runner sends again
937
+ // what it sent; after, what a turn was at is not known.
938
+ // What it wrote of a sign-in counts before the conversation opened alone: after, a
939
+ // turn says so itself, and a line written long before is no reason for this end.
940
+ reason: ready ? "crashed" : SIGN_IN.test(errors) ? "sign_in" : "start",
941
+ });
942
+ }
943
+ emit({ type: "ended", how: clean ? "exited" : "crashed" });
944
+ })();
477
945
  return {
478
946
  events: events(),
479
947
  async send(message) {
480
- if (over) {
948
+ // Kept by the runner, where it is not handed over: a product that is gone takes nothing.
949
+ if (stopping || gaveUp || over) {
481
950
  throw new Error("the session takes no more input");
482
951
  }
483
- if (turn === undefined) {
484
- run([message]);
485
- return;
486
- }
487
952
  waiting.push(message);
488
- // The product takes no message in a turn: one to be read now ends the turn's process.
489
- if (message.priority === "now" && turn.cut === undefined) {
490
- turn.cut = "message";
491
- void endOf(turn.child);
953
+ // One that is to be read now stops the turn under way, and is the turn after.
954
+ if (message.priority === "now") {
955
+ stopTurn("message");
492
956
  }
957
+ pump();
493
958
  },
494
959
  async interrupt() {
495
- if (turn !== undefined && turn.cut === undefined) {
496
- turn.cut = "stopped";
497
- void endOf(turn.child);
498
- }
960
+ stopTurn("stopped");
499
961
  },
500
- // Each turn is started with them: taken from the next turn on.
962
+ // Said with the next turn that is started, and the conversation's from then on.
501
963
  async set(next) {
502
964
  choice = {
503
965
  model: next.model === undefined ? choice.model : next.model,
@@ -506,18 +968,12 @@ function sessionOf(host, options) {
506
968
  return true;
507
969
  },
508
970
  async stop() {
509
- if (over) {
971
+ if (stopping) {
510
972
  return;
511
973
  }
512
- over = true;
974
+ stopping = true;
513
975
  waiting = [];
514
- const open = turn;
515
- if (open !== undefined) {
516
- open.cut = "ended";
517
- await endOf(open.child);
518
- await open.done;
519
- }
520
- emit({ type: "ended", how: "exited" });
976
+ await endOf(child);
521
977
  },
522
978
  };
523
979
  }
@@ -553,10 +1009,17 @@ function offeredOf(text) {
553
1009
  }
554
1010
  return { models, efforts };
555
1011
  }
556
- /** The builds of the binary the driver was run against: what the fixtures of the tests were written down from. */
557
- const TRIED = ["0.164.0-alpha.11.1"];
1012
+ /**
1013
+ * The builds of the binary the driver ran a turn of a signed-in product against, on Windows
1014
+ * (2026-11-24): a turn with a command and a file written in the desk, a message put into a turn
1015
+ * while a command ran, a turn stopped for a message, and the allowance as the product says it.
1016
+ * The app server was spoken to signed out with 0.139.0, 0.167.0 and 0.172.0, which is where the
1017
+ * shapes of the tests that say so were written down from.
1018
+ */
1019
+ const TRIED = ["0.171.0-alpha.13.2"];
558
1020
  export function createCodexDriver(host) {
559
1021
  const command = host.command ?? "codex";
1022
+ const uuid = host.uuid ?? randomUUID;
560
1023
  /** What the product prints for a command that asks it something, or nothing where it could not be asked. */
561
1024
  const asked = async (args) => {
562
1025
  try {
@@ -578,9 +1041,9 @@ export function createCodexDriver(host) {
578
1041
  return {
579
1042
  name: "codex",
580
1043
  product: { name: "Codex", tried: TRIED },
581
- // No message is taken in a turn, and a picture is taken as a file, which the driver has
582
- // nowhere to write: neither is offered. The model and the effort are each turn's.
583
- can: { steer: false, images: true, imageFiles: true, resume: true, set: true },
1044
+ // A message is put into the turn under way. A picture is taken as a file, by its path in
1045
+ // the desk. The model and the effort are said with a turn, and taken from the next one on.
1046
+ can: { steer: true, images: true, imageFiles: true, resume: true, set: true },
584
1047
  permissions: { modes: MODES, unattended: UNATTENDED_MODE, least: LEAST_MODE },
585
1048
  async probe() {
586
1049
  // One line: what the command is called, then its version.
@@ -590,7 +1053,7 @@ export function createCodexDriver(host) {
590
1053
  return { version, models: offered.models, efforts: offered.efforts };
591
1054
  },
592
1055
  start(options) {
593
- return sessionOf({ spawn: host.spawn, command }, options);
1056
+ return sessionOf(host.spawn(command, ARGUMENTS, { cwd: options.cwd, env: options.env }), { command, version: host.version, uuid }, options);
594
1057
  },
595
1058
  };
596
1059
  }