@zvada/agent-server 0.3.21 → 0.3.23

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 (367) hide show
  1. package/CHANGELOG.md +332 -0
  2. package/README.md +2 -1
  3. package/dist/types/accounts/chatgpt.d.ts +1 -1
  4. package/dist/types/accounts/chatgpt.d.ts.map +1 -1
  5. package/dist/types/accounts/http.d.ts +2 -4
  6. package/dist/types/accounts/http.d.ts.map +1 -1
  7. package/dist/types/client/client.d.ts +37 -11
  8. package/dist/types/client/client.d.ts.map +1 -1
  9. package/dist/types/client/connection.d.ts +66 -13
  10. package/dist/types/client/connection.d.ts.map +1 -1
  11. package/dist/types/client/index.d.ts +1 -1
  12. package/dist/types/client/index.d.ts.map +1 -1
  13. package/dist/types/client/logins.d.ts +4 -3
  14. package/dist/types/client/logins.d.ts.map +1 -1
  15. package/dist/types/client/streams.d.ts +9 -3
  16. package/dist/types/client/streams.d.ts.map +1 -1
  17. package/dist/types/client/turns.d.ts +1 -1
  18. package/dist/types/client/turns.d.ts.map +1 -1
  19. package/dist/types/core/agents/acp/acp-agent.d.ts +34 -1
  20. package/dist/types/core/agents/acp/acp-agent.d.ts.map +1 -1
  21. package/dist/types/core/agents/acp/adapter.d.ts.map +1 -1
  22. package/dist/types/core/agents/acp/client.d.ts +12 -3
  23. package/dist/types/core/agents/acp/client.d.ts.map +1 -1
  24. package/dist/types/core/agents/acp/mappings.d.ts +8 -2
  25. package/dist/types/core/agents/acp/mappings.d.ts.map +1 -1
  26. package/dist/types/core/agents/base.d.ts +22 -8
  27. package/dist/types/core/agents/base.d.ts.map +1 -1
  28. package/dist/types/core/agents/claude-code/adapter.d.ts +7 -0
  29. package/dist/types/core/agents/claude-code/adapter.d.ts.map +1 -1
  30. package/dist/types/core/agents/claude-code/approvals.d.ts +4 -1
  31. package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
  32. package/dist/types/core/agents/claude-code/claude-agent.d.ts +16 -5
  33. package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
  34. package/dist/types/core/agents/claude-code/events.d.ts +9 -1
  35. package/dist/types/core/agents/claude-code/events.d.ts.map +1 -1
  36. package/dist/types/core/agents/claude-code/login.d.ts +4 -1
  37. package/dist/types/core/agents/claude-code/login.d.ts.map +1 -1
  38. package/dist/types/core/agents/claude-code/mcp.d.ts +21 -7
  39. package/dist/types/core/agents/claude-code/mcp.d.ts.map +1 -1
  40. package/dist/types/core/agents/claude-code/models.d.ts +5 -1
  41. package/dist/types/core/agents/claude-code/models.d.ts.map +1 -1
  42. package/dist/types/core/agents/claude-code/options.d.ts +10 -4
  43. package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
  44. package/dist/types/core/agents/claude-code/outcome.d.ts +3 -5
  45. package/dist/types/core/agents/claude-code/outcome.d.ts.map +1 -1
  46. package/dist/types/core/agents/claude-code/results.d.ts +16 -0
  47. package/dist/types/core/agents/claude-code/results.d.ts.map +1 -0
  48. package/dist/types/core/agents/claude-code/sdk-pair.d.ts +13 -0
  49. package/dist/types/core/agents/claude-code/sdk-pair.d.ts.map +1 -0
  50. package/dist/types/core/agents/claude-code/session.d.ts +50 -12
  51. package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
  52. package/dist/types/core/agents/claude-code/spend.d.ts +2 -1
  53. package/dist/types/core/agents/claude-code/spend.d.ts.map +1 -1
  54. package/dist/types/core/agents/claude-code/tools.d.ts +26 -3
  55. package/dist/types/core/agents/claude-code/tools.d.ts.map +1 -1
  56. package/dist/types/core/agents/codex-app-server/adapter.d.ts +2 -2
  57. package/dist/types/core/agents/codex-app-server/adapter.d.ts.map +1 -1
  58. package/dist/types/core/agents/codex-app-server/client.d.ts +21 -2
  59. package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -1
  60. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts +61 -14
  61. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts.map +1 -1
  62. package/dist/types/core/agents/codex-app-server/commands.d.ts +2 -2
  63. package/dist/types/core/agents/codex-app-server/commands.d.ts.map +1 -1
  64. package/dist/types/core/agents/codex-app-server/credentials.d.ts +5 -1
  65. package/dist/types/core/agents/codex-app-server/credentials.d.ts.map +1 -1
  66. package/dist/types/core/agents/codex-app-server/effort.d.ts +2 -1
  67. package/dist/types/core/agents/codex-app-server/effort.d.ts.map +1 -1
  68. package/dist/types/core/agents/codex-app-server/homes.d.ts +26 -5
  69. package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
  70. package/dist/types/core/agents/codex-app-server/items.d.ts +27 -2
  71. package/dist/types/core/agents/codex-app-server/items.d.ts.map +1 -1
  72. package/dist/types/core/agents/codex-app-server/login.d.ts +4 -1
  73. package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -1
  74. package/dist/types/core/agents/codex-app-server/mcp.d.ts +45 -20
  75. package/dist/types/core/agents/codex-app-server/mcp.d.ts.map +1 -1
  76. package/dist/types/core/agents/codex-app-server/models.d.ts +3 -10
  77. package/dist/types/core/agents/codex-app-server/models.d.ts.map +1 -1
  78. package/dist/types/core/agents/codex-app-server/options.d.ts +32 -5
  79. package/dist/types/core/agents/codex-app-server/options.d.ts.map +1 -1
  80. package/dist/types/core/agents/codex-app-server/plan-approval.d.ts +2 -1
  81. package/dist/types/core/agents/codex-app-server/plan-approval.d.ts.map +1 -1
  82. package/dist/types/core/agents/codex-app-server/protocol.d.ts +16 -3
  83. package/dist/types/core/agents/codex-app-server/protocol.d.ts.map +1 -1
  84. package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
  85. package/dist/types/core/agents/codex-app-server/session.d.ts +70 -33
  86. package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
  87. package/dist/types/core/agents/codex-app-server/subagents.d.ts +9 -4
  88. package/dist/types/core/agents/codex-app-server/subagents.d.ts.map +1 -1
  89. package/dist/types/core/agents/codex-app-server/thread-settings.d.ts +3 -2
  90. package/dist/types/core/agents/codex-app-server/thread-settings.d.ts.map +1 -1
  91. package/dist/types/core/agents/codex-app-server/turn-items.d.ts +2 -1
  92. package/dist/types/core/agents/codex-app-server/turn-items.d.ts.map +1 -1
  93. package/dist/types/core/agents/codex-app-server/turn.d.ts +37 -8
  94. package/dist/types/core/agents/codex-app-server/turn.d.ts.map +1 -1
  95. package/dist/types/core/agents/codex-app-server/user-login.d.ts +28 -0
  96. package/dist/types/core/agents/codex-app-server/user-login.d.ts.map +1 -0
  97. package/dist/types/core/agents/credentials.d.ts +3 -1
  98. package/dist/types/core/agents/credentials.d.ts.map +1 -1
  99. package/dist/types/core/agents/diffs.d.ts +9 -0
  100. package/dist/types/core/agents/diffs.d.ts.map +1 -0
  101. package/dist/types/core/agents/elicitation.d.ts +3 -2
  102. package/dist/types/core/agents/elicitation.d.ts.map +1 -1
  103. package/dist/types/core/agents/login.d.ts +6 -0
  104. package/dist/types/core/agents/login.d.ts.map +1 -1
  105. package/dist/types/core/agents/mcp-config.d.ts +0 -4
  106. package/dist/types/core/agents/mcp-config.d.ts.map +1 -1
  107. package/dist/types/core/agents/plan.d.ts +2 -13
  108. package/dist/types/core/agents/plan.d.ts.map +1 -1
  109. package/dist/types/core/agents/process-pool.d.ts +193 -0
  110. package/dist/types/core/agents/process-pool.d.ts.map +1 -0
  111. package/dist/types/core/agents/scripted/adapter.d.ts +68 -0
  112. package/dist/types/core/agents/scripted/adapter.d.ts.map +1 -0
  113. package/dist/types/core/agents/scripted/mcp-call.d.ts +56 -0
  114. package/dist/types/core/agents/scripted/mcp-call.d.ts.map +1 -0
  115. package/dist/types/core/agents/scripted/scripted-agent.d.ts +73 -0
  116. package/dist/types/core/agents/scripted/scripted-agent.d.ts.map +1 -0
  117. package/dist/types/core/agents/scripted/scripts.d.ts +29 -0
  118. package/dist/types/core/agents/scripted/scripts.d.ts.map +1 -0
  119. package/dist/types/core/agents/scripted/steps.d.ts +132 -0
  120. package/dist/types/core/agents/scripted/steps.d.ts.map +1 -0
  121. package/dist/types/core/agents/session-agent.d.ts +49 -6
  122. package/dist/types/core/agents/session-agent.d.ts.map +1 -1
  123. package/dist/types/core/diagnostics.d.ts +4 -1
  124. package/dist/types/core/diagnostics.d.ts.map +1 -1
  125. package/dist/types/core/index.d.ts +6 -1
  126. package/dist/types/core/index.d.ts.map +1 -1
  127. package/dist/types/core/presets.d.ts +32 -6
  128. package/dist/types/core/presets.d.ts.map +1 -1
  129. package/dist/types/core/processes/lifeline.d.ts +0 -20
  130. package/dist/types/core/processes/lifeline.d.ts.map +1 -1
  131. package/dist/types/core/provision/index.d.ts +2 -2
  132. package/dist/types/core/provision/index.d.ts.map +1 -1
  133. package/dist/types/core/provision/install-build.d.ts +2 -1
  134. package/dist/types/core/provision/install-build.d.ts.map +1 -1
  135. package/dist/types/core/provision/npm.d.ts +20 -8
  136. package/dist/types/core/provision/npm.d.ts.map +1 -1
  137. package/dist/types/core/provision/provisioner.d.ts +4 -1
  138. package/dist/types/core/provision/provisioner.d.ts.map +1 -1
  139. package/dist/types/core/provision/tools.d.ts +2 -1
  140. package/dist/types/core/provision/tools.d.ts.map +1 -1
  141. package/dist/types/core/runtime/agent-runtime.d.ts +18 -9
  142. package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
  143. package/dist/types/core/runtime/event-processor.d.ts +5 -1
  144. package/dist/types/core/runtime/event-processor.d.ts.map +1 -1
  145. package/dist/types/core/runtime/harness-clis.d.ts +9 -0
  146. package/dist/types/core/runtime/harness-clis.d.ts.map +1 -1
  147. package/dist/types/core/runtime/harness-logins.d.ts +33 -5
  148. package/dist/types/core/runtime/harness-logins.d.ts.map +1 -1
  149. package/dist/types/core/runtime/model-lists.d.ts +2 -0
  150. package/dist/types/core/runtime/model-lists.d.ts.map +1 -1
  151. package/dist/types/core/runtime/permission-policy.d.ts +5 -3
  152. package/dist/types/core/runtime/permission-policy.d.ts.map +1 -1
  153. package/dist/types/core/runtime/request-broker.d.ts +2 -1
  154. package/dist/types/core/runtime/request-broker.d.ts.map +1 -1
  155. package/dist/types/core/runtime/turn.d.ts +14 -1
  156. package/dist/types/core/runtime/turn.d.ts.map +1 -1
  157. package/dist/types/core/utils/deadline.d.ts +6 -0
  158. package/dist/types/core/utils/deadline.d.ts.map +1 -1
  159. package/dist/types/core/utils/errors.d.ts +15 -1
  160. package/dist/types/core/utils/errors.d.ts.map +1 -1
  161. package/dist/types/operator/index.d.ts +11 -0
  162. package/dist/types/operator/index.d.ts.map +1 -0
  163. package/dist/types/operator/mcp.d.ts +33 -0
  164. package/dist/types/operator/mcp.d.ts.map +1 -0
  165. package/dist/types/operator/operator.d.ts +206 -0
  166. package/dist/types/operator/operator.d.ts.map +1 -0
  167. package/dist/types/operator/render.d.ts +89 -0
  168. package/dist/types/operator/render.d.ts.map +1 -0
  169. package/dist/types/operator/view.d.ts +44 -0
  170. package/dist/types/operator/view.d.ts.map +1 -0
  171. package/dist/types/protocol/auth.d.ts +54 -24
  172. package/dist/types/protocol/auth.d.ts.map +1 -1
  173. package/dist/types/protocol/clis.d.ts +14 -7
  174. package/dist/types/protocol/clis.d.ts.map +1 -1
  175. package/dist/types/protocol/config.d.ts +2 -0
  176. package/dist/types/protocol/config.d.ts.map +1 -1
  177. package/dist/types/protocol/conversation.d.ts +40 -3
  178. package/dist/types/protocol/conversation.d.ts.map +1 -1
  179. package/dist/types/protocol/credentials.d.ts +7 -0
  180. package/dist/types/protocol/credentials.d.ts.map +1 -1
  181. package/dist/types/protocol/execution.d.ts +2 -1
  182. package/dist/types/protocol/execution.d.ts.map +1 -1
  183. package/dist/types/protocol/factories.d.ts +12 -2
  184. package/dist/types/protocol/factories.d.ts.map +1 -1
  185. package/dist/types/protocol/harness.d.ts +6 -2
  186. package/dist/types/protocol/harness.d.ts.map +1 -1
  187. package/dist/types/protocol/history.d.ts +19 -0
  188. package/dist/types/protocol/history.d.ts.map +1 -1
  189. package/dist/types/protocol/lifecycle.d.ts +47 -8
  190. package/dist/types/protocol/lifecycle.d.ts.map +1 -1
  191. package/dist/types/protocol/model-catalog.d.ts.map +1 -1
  192. package/dist/types/protocol/models.d.ts +3 -0
  193. package/dist/types/protocol/models.d.ts.map +1 -1
  194. package/dist/types/protocol/parts.d.ts +14 -0
  195. package/dist/types/protocol/parts.d.ts.map +1 -1
  196. package/dist/types/protocol/plan.d.ts +20 -0
  197. package/dist/types/protocol/plan.d.ts.map +1 -0
  198. package/dist/types/protocol/tool-state.d.ts +12 -0
  199. package/dist/types/protocol/tool-state.d.ts.map +1 -1
  200. package/dist/types/protocol/wire-api.d.ts +129 -23
  201. package/dist/types/protocol/wire-api.d.ts.map +1 -1
  202. package/dist/types/protocol/wire.d.ts +70 -10
  203. package/dist/types/protocol/wire.d.ts.map +1 -1
  204. package/dist/types/server/acp/binding.d.ts +6 -1
  205. package/dist/types/server/acp/binding.d.ts.map +1 -1
  206. package/dist/types/server/acp/translate.d.ts +33 -7
  207. package/dist/types/server/acp/translate.d.ts.map +1 -1
  208. package/dist/types/server/agent-server.d.ts +6 -0
  209. package/dist/types/server/agent-server.d.ts.map +1 -1
  210. package/dist/types/server/feedback.d.ts +2 -1
  211. package/dist/types/server/feedback.d.ts.map +1 -1
  212. package/dist/types/server/install.d.ts +11 -0
  213. package/dist/types/server/install.d.ts.map +1 -1
  214. package/dist/types/server/session-gate.d.ts +2 -1
  215. package/dist/types/server/session-gate.d.ts.map +1 -1
  216. package/dist/types/server/session-store.d.ts +1 -0
  217. package/dist/types/server/session-store.d.ts.map +1 -1
  218. package/dist/types/server/subscriptions.d.ts +2 -1
  219. package/dist/types/server/subscriptions.d.ts.map +1 -1
  220. package/dist/types/server/transports.d.ts.map +1 -1
  221. package/dist/types/sync/fit.d.ts +8 -7
  222. package/dist/types/sync/fit.d.ts.map +1 -1
  223. package/dist/types/sync/history-sqlite.d.ts.map +1 -1
  224. package/dist/types/sync/index.d.ts +1 -0
  225. package/dist/types/sync/index.d.ts.map +1 -1
  226. package/dist/types/sync/journal-sqlite.d.ts +2 -1
  227. package/dist/types/sync/journal-sqlite.d.ts.map +1 -1
  228. package/dist/types/sync/journal.d.ts +11 -3
  229. package/dist/types/sync/journal.d.ts.map +1 -1
  230. package/dist/types/sync/projection.d.ts +6 -0
  231. package/dist/types/sync/projection.d.ts.map +1 -1
  232. package/dist/types/sync/recovery.d.ts +2 -1
  233. package/dist/types/sync/recovery.d.ts.map +1 -1
  234. package/dist/types/sync/schema.d.ts +10 -0
  235. package/dist/types/sync/schema.d.ts.map +1 -1
  236. package/dist/types/sync/sql.d.ts +2 -1
  237. package/dist/types/sync/sql.d.ts.map +1 -1
  238. package/dist/types/sync/worker.d.ts.map +1 -1
  239. package/docs/consuming.md +464 -40
  240. package/docs/deploy.md +36 -8
  241. package/docs/durable-sessions.md +35 -5
  242. package/docs/harnesses.md +396 -30
  243. package/package.json +11 -1
  244. package/src/accounts/chatgpt.ts +2 -2
  245. package/src/accounts/http.ts +2 -16
  246. package/src/client/client.ts +92 -31
  247. package/src/client/connection.ts +113 -22
  248. package/src/client/index.ts +1 -0
  249. package/src/client/logins.ts +24 -15
  250. package/src/client/streams.ts +12 -4
  251. package/src/client/turns.ts +1 -1
  252. package/src/core/agents/acp/acp-agent.ts +181 -52
  253. package/src/core/agents/acp/adapter.ts +9 -1
  254. package/src/core/agents/acp/client.ts +16 -4
  255. package/src/core/agents/acp/mappings.ts +27 -0
  256. package/src/core/agents/base.ts +25 -10
  257. package/src/core/agents/claude-code/adapter.ts +47 -17
  258. package/src/core/agents/claude-code/approvals.ts +6 -5
  259. package/src/core/agents/claude-code/claude-agent.ts +60 -30
  260. package/src/core/agents/claude-code/events.ts +11 -1
  261. package/src/core/agents/claude-code/login.ts +14 -2
  262. package/src/core/agents/claude-code/mcp.ts +31 -15
  263. package/src/core/agents/claude-code/models.ts +7 -2
  264. package/src/core/agents/claude-code/options.ts +11 -3
  265. package/src/core/agents/claude-code/outcome.ts +10 -2
  266. package/src/core/agents/claude-code/results.ts +73 -0
  267. package/src/core/agents/claude-code/sdk-pair.ts +66 -0
  268. package/src/core/agents/claude-code/session.ts +166 -37
  269. package/src/core/agents/claude-code/spend.ts +1 -1
  270. package/src/core/agents/claude-code/tools.ts +108 -12
  271. package/src/core/agents/codex-app-server/adapter.ts +37 -12
  272. package/src/core/agents/codex-app-server/client.ts +69 -10
  273. package/src/core/agents/codex-app-server/codex-agent.ts +141 -55
  274. package/src/core/agents/codex-app-server/commands.ts +2 -2
  275. package/src/core/agents/codex-app-server/credentials.ts +10 -5
  276. package/src/core/agents/codex-app-server/effort.ts +1 -1
  277. package/src/core/agents/codex-app-server/homes.ts +59 -10
  278. package/src/core/agents/codex-app-server/items.ts +219 -31
  279. package/src/core/agents/codex-app-server/login.ts +14 -2
  280. package/src/core/agents/codex-app-server/mcp.ts +142 -26
  281. package/src/core/agents/codex-app-server/models.ts +9 -8
  282. package/src/core/agents/codex-app-server/options.ts +79 -11
  283. package/src/core/agents/codex-app-server/plan-approval.ts +1 -1
  284. package/src/core/agents/codex-app-server/protocol.ts +14 -4
  285. package/src/core/agents/codex-app-server/requests.ts +14 -6
  286. package/src/core/agents/codex-app-server/session.ts +156 -81
  287. package/src/core/agents/codex-app-server/subagents.ts +21 -14
  288. package/src/core/agents/codex-app-server/thread-settings.ts +18 -8
  289. package/src/core/agents/codex-app-server/turn-items.ts +1 -1
  290. package/src/core/agents/codex-app-server/turn.ts +96 -35
  291. package/src/core/agents/codex-app-server/user-login.ts +107 -0
  292. package/src/core/agents/credentials.ts +5 -2
  293. package/src/core/agents/diffs.ts +25 -0
  294. package/src/core/agents/elicitation.ts +2 -2
  295. package/src/core/agents/login.ts +6 -0
  296. package/src/core/agents/mcp-config.ts +2 -2
  297. package/src/core/agents/plan.ts +3 -19
  298. package/src/core/agents/process-pool.ts +432 -0
  299. package/src/core/agents/scripted/adapter.ts +142 -0
  300. package/src/core/agents/scripted/mcp-call.ts +134 -0
  301. package/src/core/agents/scripted/scripted-agent.ts +418 -0
  302. package/src/core/agents/scripted/scripts.ts +171 -0
  303. package/src/core/agents/scripted/steps.ts +127 -0
  304. package/src/core/agents/session-agent.ts +105 -8
  305. package/src/core/diagnostics.ts +9 -1
  306. package/src/core/index.ts +12 -0
  307. package/src/core/presets.ts +99 -14
  308. package/src/core/processes/lifeline.ts +99 -25
  309. package/src/core/provision/index.ts +1 -2
  310. package/src/core/provision/install-build.ts +4 -1
  311. package/src/core/provision/npm.ts +309 -97
  312. package/src/core/provision/provisioner.ts +22 -8
  313. package/src/core/provision/tools.ts +44 -19
  314. package/src/core/runtime/agent-runtime.ts +70 -34
  315. package/src/core/runtime/event-processor.ts +18 -2
  316. package/src/core/runtime/harness-clis.ts +36 -1
  317. package/src/core/runtime/harness-logins.ts +82 -31
  318. package/src/core/runtime/model-lists.ts +1 -1
  319. package/src/core/runtime/permission-policy.ts +4 -3
  320. package/src/core/runtime/request-broker.ts +1 -1
  321. package/src/core/runtime/turn.ts +53 -3
  322. package/src/core/utils/deadline.ts +23 -0
  323. package/src/core/utils/errors.ts +24 -4
  324. package/src/operator/index.ts +40 -0
  325. package/src/operator/mcp.ts +171 -0
  326. package/src/operator/operator.ts +782 -0
  327. package/src/operator/render.ts +473 -0
  328. package/src/operator/view.ts +82 -0
  329. package/src/protocol/auth.ts +30 -23
  330. package/src/protocol/clis.ts +12 -4
  331. package/src/protocol/credentials.ts +19 -0
  332. package/src/protocol/execution.ts +5 -0
  333. package/src/protocol/factories.ts +25 -3
  334. package/src/protocol/harness.ts +11 -1
  335. package/src/protocol/history.ts +20 -0
  336. package/src/protocol/lifecycle.ts +10 -1
  337. package/src/protocol/model-catalog.ts +19 -8
  338. package/src/protocol/parts.ts +12 -1
  339. package/src/protocol/plan.ts +35 -0
  340. package/src/protocol/tool-state.ts +14 -1
  341. package/src/protocol/wire.ts +14 -0
  342. package/src/server/acp/binding.ts +113 -22
  343. package/src/server/acp/translate.ts +161 -8
  344. package/src/server/agent-server.ts +10 -1
  345. package/src/server/bin.ts +83 -9
  346. package/src/server/feedback.ts +1 -1
  347. package/src/server/install.ts +82 -4
  348. package/src/server/session-gate.ts +1 -1
  349. package/src/server/session-store.ts +5 -1
  350. package/src/server/subscriptions.ts +1 -1
  351. package/src/server/transports.ts +14 -0
  352. package/src/sync/fit.ts +115 -28
  353. package/src/sync/history-sqlite.ts +6 -2
  354. package/src/sync/index.ts +1 -0
  355. package/src/sync/journal-sqlite.ts +20 -2
  356. package/src/sync/journal.ts +10 -2
  357. package/src/sync/projection.ts +10 -0
  358. package/src/sync/recovery.ts +11 -4
  359. package/src/sync/schema.ts +7 -5
  360. package/src/sync/sql.ts +1 -1
  361. package/src/sync/worker.ts +4 -0
  362. package/dist/types/core/agents/claude-code/session-pool.d.ts +0 -36
  363. package/dist/types/core/agents/claude-code/session-pool.d.ts.map +0 -1
  364. package/dist/types/core/agents/session-pool.d.ts +0 -41
  365. package/dist/types/core/agents/session-pool.d.ts.map +0 -1
  366. package/src/core/agents/claude-code/session-pool.ts +0 -136
  367. package/src/core/agents/session-pool.ts +0 -111
@@ -60,12 +60,27 @@ function codexError(value: unknown): { message?: string; codexErrorInfo?: unknow
60
60
  };
61
61
  }
62
62
 
63
+ /**
64
+ * Which attempt a Codex retry is, of how many. Codex 0.160 sends the count
65
+ * only in its text ("Reconnecting... 2/5"), with no field for it and no
66
+ * wait: a text of any other shape, or a count past its total, gives none.
67
+ */
68
+ // @ref LLP 0002#plan-usage-and-retries — a field the harness did not say is left out
69
+ function codexRetryCount(message: string): { attempt?: number; maxAttempts?: number } {
70
+ const match = /^Reconnecting\.\.\. (\d{1,3})\/(\d{1,3})$/.exec(message);
71
+ if (!match) return {};
72
+ const attempt = Number(match[1]);
73
+ const maxAttempts = Number(match[2]);
74
+ if (attempt < 1 || attempt > maxAttempts) return {};
75
+ return { attempt, maxAttempts };
76
+ }
77
+
63
78
  /**
64
79
  * Parses `codex app-server` v2 notifications into AdapterEvents. This harness
65
80
  * streams token-level deltas (item/agentMessage/delta, item/reasoning/*Delta),
66
81
  * so no diffing is needed. Items carry camelCase fields. Tool-like items
67
- * (commandExecution/fileChange/mcpToolCall/webSearch) become tool parts that
68
- * complete on item/completed.
82
+ * (commandExecution/fileChange/mcpToolCall/webSearch/imageGeneration) become
83
+ * tool parts that complete on item/completed.
69
84
  */
70
85
  export class CodexAppServerTransformer implements EventTransformer<unknown> {
71
86
  private readonly ctx: StreamContext;
@@ -166,13 +181,14 @@ export class CodexAppServerTransformer implements EventTransformer<unknown> {
166
181
  case "account/rateLimits/updated":
167
182
  return codexLimits(params.rateLimits);
168
183
  case "turn/completed": {
169
- // (A pre-completion non-retry `error` cannot reach here in practice:
170
- // the agent's queue ends the turn on that notification.)
184
+ // A failed turn's `error` that Codex will not retry comes before this,
185
+ // some time before on 0.160, and the agent holds the turn open until
186
+ // this comes: what that error said stays the turn's.
171
187
  const turn = codexTurn(n);
172
- if (turn?.error) {
188
+ if (!this.error && turn?.error) {
173
189
  this.error = turn.error.message ?? "codex turn error";
174
190
  this.errorCategory = codexErrorCategory(turn.error.codexErrorInfo);
175
- } else if (turn?.status === "failed") {
191
+ } else if (!this.error && turn?.status === "failed") {
176
192
  this.error = "Codex ended the turn as failed without saying why";
177
193
  }
178
194
  // @ref LLP 0001#d4--permission-requests-are-first-class-protocol — a cancelled answer stops the turn
@@ -185,15 +201,22 @@ export class CodexAppServerTransformer implements EventTransformer<unknown> {
185
201
  }
186
202
  case "error": {
187
203
  // v2 errors carry a nested TurnError and may be transient. A retrying
188
- // one tells the user why the turn is waiting; turn/completed remains
189
- // authoritative.
204
+ // one tells the user why the turn is waiting; one Codex will not
205
+ // retry says why its turn failed, and its turn/completed follows.
190
206
  const error = codexError(params.error);
191
207
  const message =
192
208
  error?.message ??
193
209
  (typeof params.message === "string" ? params.message : "codex app-server error");
194
210
  const category = codexErrorCategory(error?.codexErrorInfo);
195
211
  if (params.willRetry) {
196
- return [{ kind: "retry", category: category ?? classifyError(message), message }];
212
+ return [
213
+ {
214
+ kind: "retry",
215
+ category: category ?? classifyError(message),
216
+ message,
217
+ ...codexRetryCount(message),
218
+ },
219
+ ];
197
220
  }
198
221
  this.error = message;
199
222
  this.errorCategory = category;
@@ -227,9 +250,9 @@ export class CodexAppServerTransformer implements EventTransformer<unknown> {
227
250
  return [{ kind: "part-open", part }];
228
251
  }
229
252
  default: {
230
- // commandExecution / fileChange / mcpToolCall / webSearch share one
231
- // descriptor vocabulary; userMessage and unknown item types produce
232
- // no assistant output.
253
+ // commandExecution / fileChange / mcpToolCall / webSearch /
254
+ // imageGeneration share one descriptor vocabulary; userMessage and
255
+ // unknown item types produce no assistant output.
233
256
  const d = codexToolDescriptor(item.type);
234
257
  return d ? [{ kind: "part-open", part: this.openTool(item, d) }] : [];
235
258
  }
@@ -271,6 +294,8 @@ export class CodexAppServerTransformer implements EventTransformer<unknown> {
271
294
  const d = codexToolDescriptor(item.type);
272
295
  if (!d) return [];
273
296
  const tool = this.tools.get(item.id) ?? this.openTool(item, d);
297
+ if (d.inputAtCompletion && tool.state.status === "in_progress")
298
+ tool.state = { ...tool.state, input: d.input(item) };
274
299
  setToolMeta(tool, d.meta(item));
275
300
  completeToolPart(tool, d.result(item));
276
301
  return [{ kind: "part-update", part: tool }];
@@ -8,6 +8,8 @@ const SHUTDOWN_FAILED =
8
8
  /failed to submit Shutdown|timed out waiting for .*shut down|shutdown timed out/;
9
9
  /** The stderr the shutdown sniff keeps between chunks: a line split across two is still read. */
10
10
  const SNIFF_MAX = 4096;
11
+ /** How long Codex's own shutdown (EOF) has before its process is stopped. */
12
+ const SHUTDOWN_TIMEOUT_MS = 2_000;
11
13
 
12
14
  /**
13
15
  * How the harness introduces itself to Codex. It has no version of its own to
@@ -46,12 +48,22 @@ export interface CodexConnection {
46
48
  onClose(handler: (error?: Error) => void): () => void;
47
49
  /** End the app-server now. Nothing confirms that what it ran stopped. */
48
50
  close(): void;
49
- /** End the app-server by its own shutdown: whether that confirmed its threads and tools stopped. */
51
+ /**
52
+ * End the app-server by its own shutdown: whether that confirmed its
53
+ * threads and tools stopped. It is stopped after `timeoutMs`
54
+ * (`SHUTDOWN_TIMEOUT_MS` by default) and given as long again to exit.
55
+ */
50
56
  closeAndWait(timeoutMs?: number): Promise<boolean>;
57
+ /**
58
+ * Settles once an app-server that was ended has exited, or could not be
59
+ * made to: Codex lets one process write a thread, so another resumes it
60
+ * after this. A connection without it has nothing to wait for.
61
+ */
62
+ gone?(): Promise<void>;
51
63
  }
52
64
 
53
65
  /** How far `pages` reads a list. */
54
- export interface PageLimits {
66
+ interface PageLimits {
55
67
  /** Each page's deadline. */
56
68
  timeoutMs?: number;
57
69
  /** Past this many pages the list did not end. */
@@ -123,6 +135,12 @@ export interface CodexAppServerClientOptions {
123
135
  * it would replace that home's own login.
124
136
  */
125
137
  ephemeralCredentials?: boolean;
138
+ /**
139
+ * Start with Codex's plugins off (`-c features.plugins=false`): set on the
140
+ * command line because a fresh `CODEX_HOME` clones the plugin catalogue
141
+ * as the process starts, before any thread's config is read.
142
+ */
143
+ pluginsOff?: boolean;
126
144
  startupTimeoutMs?: number;
127
145
  /** Default deadline for every JSON-RPC request. */
128
146
  requestTimeoutMs?: number;
@@ -156,6 +174,8 @@ export class CodexAppServerClient implements CodexConnection {
156
174
  private exited = false;
157
175
  private lastExit?: Error;
158
176
  private nativeShutdownFailed = false;
177
+ /** The app-server's exit, or the end of a stop that could not end it. */
178
+ private leaving?: Promise<unknown>;
159
179
 
160
180
  constructor(private readonly opts: CodexAppServerClientOptions) {}
161
181
 
@@ -201,7 +221,7 @@ export class CodexAppServerClient implements CodexConnection {
201
221
  const deadline = timeoutMs ?? this.opts.requestTimeoutMs ?? 30_000;
202
222
  const timer = setTimeout(() => {
203
223
  this.pending.delete(id);
204
- reject(new Error(`codex app-server '${method}' timed out`));
224
+ reject(new Error(`codex app-server '${method}' timed out after ${deadline}ms`));
205
225
  }, deadline);
206
226
  this.pending.set(id, { method, resolve, reject, timer });
207
227
  try {
@@ -225,12 +245,19 @@ export class CodexAppServerClient implements CodexConnection {
225
245
  this.rejectPending((method) => new Error(`codex app-server closed before '${method}'`));
226
246
  // SIGTERM stops its command at once (0.07 s, measured); the walk and the
227
247
  // SIGKILL after the grace cover an app-server that cannot act on it.
228
- void native?.stop();
248
+ if (native) this.leaving = Promise.race([native.exited, native.stop()]);
229
249
  if (wasOpen) this.notifyClosed();
230
250
  }
231
251
 
252
+ gone(): Promise<void> {
253
+ return Promise.resolve(this.leaving).then(
254
+ () => {},
255
+ () => {},
256
+ );
257
+ }
258
+
232
259
  /** EOF invokes native thread/tool cleanup. Killing the server alone proves nothing about workers. */
233
- closeAndWait(timeoutMs = 2_000): Promise<boolean> {
260
+ closeAndWait(timeoutMs = SHUTDOWN_TIMEOUT_MS): Promise<boolean> {
234
261
  if (this.shutdown) return this.shutdown;
235
262
  const native = this.native;
236
263
  if (!native) return Promise.resolve(true);
@@ -240,6 +267,15 @@ export class CodexAppServerClient implements CodexConnection {
240
267
  return this.shutdown;
241
268
  }
242
269
  this.exited = true;
270
+ // Gone when it exited, or when the stop a slow shutdown falls back to is
271
+ // done: that can outlast the answer this returns.
272
+ let stopped = () => {};
273
+ this.leaving = Promise.race([
274
+ native.exited,
275
+ new Promise<void>((resolve) => {
276
+ stopped = resolve;
277
+ }),
278
+ ]);
243
279
  this.shutdown = new Promise<boolean>((resolve) => {
244
280
  let forced = false;
245
281
  const finish = (confirmed: boolean) => {
@@ -254,7 +290,7 @@ export class CodexAppServerClient implements CodexConnection {
254
290
  // @ref LLP 0007#what-each-harness-does — a SIGKILLed app-server leaves its commands running
255
291
  const killTimer = setTimeout(() => {
256
292
  forced = true;
257
- void native.stop();
293
+ void native.stop().then(stopped, stopped);
258
294
  }, timeoutMs);
259
295
  const deadline = setTimeout(() => finish(false), timeoutMs * 2);
260
296
  // In stdio mode SIGTERM bypasses Codex's async shutdown. EOF drains
@@ -288,15 +324,18 @@ export class CodexAppServerClient implements CodexConnection {
288
324
  this.nativeShutdownFailed = false;
289
325
  const native = this.opts.processes.spawn({
290
326
  command: this.opts.codexPath ?? "codex",
291
- args: this.opts.ephemeralCredentials
292
- ? ["app-server", "-c", 'cli_auth_credentials_store="ephemeral"']
293
- : ["app-server"],
327
+ args: [
328
+ "app-server",
329
+ ...(this.opts.ephemeralCredentials ? ["-c", 'cli_auth_credentials_store="ephemeral"'] : []),
330
+ ...(this.opts.pluginsOff ? ["-c", "features.plugins=false"] : []),
331
+ ],
294
332
  cwd: this.opts.cwd,
295
333
  env: this.opts.env ?? process.env,
296
334
  harness: "codex-app-server",
297
335
  ...(this.opts.sessionId !== undefined && { sessionId: this.opts.sessionId }),
298
336
  });
299
337
  this.native = native;
338
+ this.leaving = native.exited;
300
339
  const proc = native.child;
301
340
  proc.stdout?.on("data", (chunk: Buffer) => this.onStdout(chunk));
302
341
  // A broken pipe usually means the process died: its exit, which says
@@ -394,11 +433,31 @@ export class CodexAppServerClient implements CodexConnection {
394
433
  if (this.exited) return;
395
434
  this.exited = true;
396
435
  this.lastExit = error;
436
+ // A broken pipe closes it while the app-server may still run (wedged,
437
+ // its stdin gone). It is stopped, so the process that takes its thread
438
+ // waits the stop's grace at most, not for an exit that never comes.
439
+ const native = this.native;
440
+ if (native) this.leaving = Promise.race([native.exited, native.stop()]);
397
441
  this.rejectPending(() => error);
398
442
  this.notifyClosed(error);
399
443
  }
400
444
 
401
445
  private write(payload: unknown): void {
402
- this.native?.child.stdin?.write(`${JSON.stringify(payload)}\n`);
446
+ this.native?.child.stdin?.write(`${JSON.stringify(payload, wellFormed)}\n`);
403
447
  }
404
448
  }
449
+
450
+ // @ref LLP 0013#f12-a-lone-surrogate-hung-codex — a line Codex cannot read is never answered
451
+ /**
452
+ * A `JSON.stringify` replacer that makes every string and every key
453
+ * well-formed (a lone UTF-16 surrogate becomes U+FFFD). JSON.stringify
454
+ * writes a lone surrogate as its `\ud83d` escape, which Codex 0.160 cannot
455
+ * parse ("unexpected end of hex escape"): it drops the line and never answers.
456
+ * A key is an MCP server's name, say.
457
+ */
458
+ function wellFormed(_key: string, value: unknown): unknown {
459
+ if (typeof value === "string") return value.toWellFormed();
460
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return value;
461
+ if (Object.keys(value).every((key) => key.isWellFormed())) return value;
462
+ return Object.fromEntries(Object.entries(value).map(([key, v]) => [key.toWellFormed(), v]));
463
+ }
@@ -1,17 +1,17 @@
1
- import type { AgentCapabilities, AgentInput, ModelDescriptor } from "../../../protocol/index.ts";
2
- import { type NativeProcesses, sharedNativeProcesses } from "../../processes/native-processes.ts";
3
- import { onAbort } from "../../utils/deadline.ts";
4
1
  import type {
5
- AgentExecuteOptions,
6
- CancelResult,
7
- ListModelsOptions,
8
- RawAgentEvent,
9
- } from "../base.ts";
2
+ AgentCapabilities,
3
+ AgentInput,
4
+ ModelDescriptor,
5
+ RunConfig,
6
+ } from "../../../protocol/index.ts";
7
+ import { type NativeProcesses, sharedNativeProcesses } from "../../processes/native-processes.ts";
8
+ import { onAbort, untilAborted } from "../../utils/deadline.ts";
9
+ import type { AgentExecuteOptions, ListModelsOptions, RawAgentEvent } from "../base.ts";
10
10
  import { configFingerprint } from "../config-fingerprint.ts";
11
11
  import type { CredentialSource } from "../credentials.ts";
12
12
  import type { InheritConfig } from "../inherit-config.ts";
13
+ import { IDLE_TIMEOUT_MS } from "../process-pool.ts";
13
14
  import { type DeliverSteer, SessionAgent } from "../session-agent.ts";
14
- import { IDLE_TIMEOUT_MS } from "../session-pool.ts";
15
15
  import {
16
16
  CodexAppServerClient,
17
17
  type CodexAppServerClientOptions,
@@ -22,12 +22,13 @@ import { type CodexAuth, authFor, logIn } from "./credentials.ts";
22
22
  import { toUserInput } from "./input.ts";
23
23
  import { CodexCliLogin } from "./login.ts";
24
24
  import { codexModelDescriptors, listCodexModels, readCodexListing } from "./models.ts";
25
- import { CODEX_TOOLS_REQUIRED, clientOptionsFor } from "./options.ts";
25
+ import { CODEX_TOOLS_REQUIRED, type CodexBuiltInTools, clientOptionsFor } from "./options.ts";
26
26
  import type { CodexTurnScope } from "./requests.ts";
27
27
  import { type CodexSession, CodexSessions } from "./session.ts";
28
28
  import { CodexTurnSubagents, ManagedStop } from "./subagents.ts";
29
29
  import { TurnItems } from "./turn-items.ts";
30
30
  import { CodexTurn, type CodexTurnTiming, InterruptStop, type TurnStop } from "./turn.ts";
31
+ import { type ReadUserLogin, userAuth, userLoginReader } from "./user-login.ts";
31
32
 
32
33
  const CAPABILITIES: AgentCapabilities = {
33
34
  multiTurn: true,
@@ -44,28 +45,39 @@ const CAPABILITIES: AgentCapabilities = {
44
45
  rejectionMessages: false,
45
46
  turnScopedSubagents: true,
46
47
  steering: true,
48
+ ignoredSettings: [
49
+ // Nothing in this harness sends a turn limit to the app-server.
50
+ "maxTurns",
51
+ // thread/resume continues a thread at its end; nothing here names a message in it.
52
+ "resumeSessionAt",
53
+ ] satisfies Array<keyof RunConfig>,
47
54
  };
48
55
 
49
56
  /** How long a retryable error may go unanswered before the turn is failed. */
50
57
  const RETRY_STALL_TIMEOUT_MS = 120_000;
51
58
 
52
- /** How long Codex has to confirm a stop, unless `cancelGraceMs` says otherwise. */
59
+ /** How long a cancelled turn has to stop before Codex's process is ended, unless `cancelGraceMs` says otherwise. */
53
60
  const CANCEL_GRACE_MS = 2_000;
54
61
 
55
62
  export interface CodexAppServerAgentOptions {
56
63
  /** Make the connection to `codex app-server` (a test's fake); a `CodexAppServerClient` by default. */
57
64
  createClient?: (options: CodexAppServerClientOptions) => CodexConnection;
65
+ /** Idle time before a session's app-server is ended (5 minutes by default); 0 never ends one for idleness. */
58
66
  idleTimeoutMs?: number;
59
67
  /** Fail a turn if the app-server goes silent this long after a retryable error. */
60
68
  retryStallTimeoutMs?: number;
61
69
  /**
62
- * How long Codex has to confirm a stop (2 s by default): to acknowledge a
63
- * cancel's `turn/interrupt` before the turn's process is ended, and to let
64
- * the interrupted turn drain; to stop the commands the turn started, and
65
- * for its own shutdown when they will not stop; to put a thread's settings
66
- * back after a failed turn (`thread/settings/update`); and for each request
67
- * of a managed turn's cleanup and its shutdown, five times it for the whole
68
- * cleanup.
70
+ * How long a cancel waits for Codex to confirm its turn stopped before it
71
+ * answers `confirmed: false` (`CANCEL_CONFIRM_MS`, 2 s, by default), and
72
+ * how long a failed turn waits for Codex to say it ended before its process
73
+ * is ended.
74
+ */
75
+ cancelConfirmMs?: number;
76
+ /**
77
+ * How long after a cancel Codex's process is ended when its turn has not
78
+ * stopped (2 s by default): its `turn/interrupt` unanswered, or the
79
+ * commands the turn started not confirmed stopped, which Codex's own
80
+ * shutdown then stops.
69
81
  */
70
82
  cancelGraceMs?: number;
71
83
  /**
@@ -90,7 +102,9 @@ export interface CodexAppServerAgentOptions {
90
102
  processes?: NativeProcesses;
91
103
  /**
92
104
  * What a thread loads by itself (LLP 0001, D17). At `all` (the default) a
93
- * turn runs in the `CODEX_HOME` it names, else the process's. At `project`
105
+ * turn runs in the `CODEX_HOME` it names, else the process's, which the
106
+ * engine never writes: there a turn's MCP servers go in its thread's own
107
+ * config (D21). At `project`
94
108
  * every turn must name a home of its session's own (absolute, not the
95
109
  * shared one, one session to a home): the user's configuration stays out,
96
110
  * the repository's `AGENTS.md` and skills load. `none` also keeps the
@@ -98,6 +112,30 @@ export interface CodexAppServerAgentOptions {
98
112
  * the repository's skills and `~/.agents/skills` at every level.
99
113
  */
100
114
  inheritConfig?: InheritConfig;
115
+ /**
116
+ * Codex's own tools the model gets, each on unless set `false` (LLP 0017):
117
+ * `local` its shell, `apply_patch`, `view_image` and `write_stdin`, so
118
+ * that nothing it runs reads or writes the disk; `webSearch`; and
119
+ * `imageGeneration`, whose pictures come as an `image_generation` tool
120
+ * part, saying where Codex saved each, when it is on. `local: false` holds
121
+ * for the sub-agents Codex spawns too. A product whose agent works only
122
+ * through its own MCP tools turns all three off. Code mode's `exec`, its
123
+ * sub-agent tools and questions stay: `disableTools` is still refused.
124
+ * Operator-level only.
125
+ */
126
+ builtInTools?: CodexBuiltInTools;
127
+ /**
128
+ * A turn that names a `CODEX_HOME` of its session's own (every turn below
129
+ * `inheritConfig: "all"` does) runs on the user's own Codex login: the one
130
+ * of the engine's own Codex home (`$CODEX_HOME`, else `~/.codex`), read
131
+ * from Codex before each turn and held in the session's process's memory,
132
+ * never written to the session's home (LLP 0017). A product's
133
+ * `credentials` and the turn's `apiKey` come first; with no login there
134
+ * the turn fails (`auth`) before its process starts. A product reads
135
+ * that login's status and signs it in with no `env` (`auth/*`). Off by
136
+ * default. Operator-level only.
137
+ */
138
+ userLogin?: boolean;
101
139
  }
102
140
 
103
141
  /**
@@ -113,18 +151,24 @@ export class CodexAppServerAgent extends SessionAgent {
113
151
  readonly processes: NativeProcesses;
114
152
  private readonly timing: CodexTurnTiming;
115
153
  private readonly sessions: CodexSessions;
116
- /** A cancel's acknowledgement belongs to one invocation, even after it drains. */
117
- private readonly turns = new WeakMap<AbortController, CodexTurn>();
118
154
  readonly login = new CodexCliLogin({
119
155
  connect: (env) =>
120
156
  this.connect({ cwd: process.cwd(), ...(env && { env }) }, { experimentalApi: false }),
121
157
  cliPath: async () => (await this.cliPath()) ?? "codex",
122
158
  });
159
+ /**
160
+ * The user's own login (`userLogin`), read from an app-server with the
161
+ * engine's own environment: never a session's `CODEX_HOME`.
162
+ */
163
+ private readonly readUserLogin: ReadUserLogin = userLoginReader(() =>
164
+ this.connect({ cwd: process.cwd() }, { experimentalApi: false }),
165
+ );
123
166
 
124
167
  constructor(private readonly agentOptions: CodexAppServerAgentOptions = {}) {
125
- super();
168
+ super({ cancelConfirmMs: agentOptions.cancelConfirmMs });
126
169
  this.processes = agentOptions.processes ?? sharedNativeProcesses();
127
170
  this.timing = {
171
+ confirmMs: this.cancelConfirmMs,
128
172
  graceMs: agentOptions.cancelGraceMs ?? CANCEL_GRACE_MS,
129
173
  retryStallMs: agentOptions.retryStallTimeoutMs ?? RETRY_STALL_TIMEOUT_MS,
130
174
  };
@@ -141,7 +185,10 @@ export class CodexAppServerAgent extends SessionAgent {
141
185
  sessionId: options.sessionId,
142
186
  }),
143
187
  agentOptions.idleTimeoutMs ?? IDLE_TIMEOUT_MS,
144
- agentOptions.inheritConfig,
188
+ {
189
+ inheritConfig: agentOptions.inheritConfig,
190
+ builtInTools: agentOptions.builtInTools,
191
+ },
145
192
  );
146
193
  }
147
194
 
@@ -166,6 +213,18 @@ export class CodexAppServerAgent extends SessionAgent {
166
213
  let session: CodexSession | undefined;
167
214
  let stop: TurnStop | undefined;
168
215
  let turn: CodexTurn | undefined;
216
+ // @ref LLP 0001#d21--a-codex-turn-that-names-no-home-carries-its-mcp-servers-in-its-thread — a thread is the session's once Codex takes a turn on it
217
+ // The thread the turn asked for is the session's already. One this turn
218
+ // started is not yet: Codex writes a thread at its first turn, so until
219
+ // then nothing of it can be resumed, and a turn that fails before
220
+ // leaves the session without a conversation, not with one no later
221
+ // process could resume.
222
+ let named = false;
223
+ const nameStarted = () => {
224
+ if (named || !session || !turn?.taken) return;
225
+ named = true;
226
+ options.onNativeSession?.(session.threadId, { resumed: false });
227
+ };
169
228
  try {
170
229
  signal.throwIfAborted();
171
230
  // @ref LLP 0001#d5--messages-map-to-model-messages-no-silent-surface — no silent surface
@@ -183,17 +242,21 @@ export class CodexAppServerAgent extends SessionAgent {
183
242
  sessionId: options.sessionId,
184
243
  turnId: options.turnId,
185
244
  apiKey: options.apiKey,
245
+ userLogin: this.userLoginFor(options.env),
186
246
  signal,
187
247
  });
188
248
  signal.throwIfAborted();
189
249
  session = await this.sessions.acquire(options, scope, mcp);
190
- options.onNativeSession?.(session.threadId, {
191
- resumed: session.threadId === options.resumeSessionId,
192
- });
250
+ named = session.threadId === options.resumeSessionId;
251
+ if (named) options.onNativeSession?.(session.threadId, { resumed: true });
193
252
  const commands = new TurnCommands(session.client, session.threadId, this.timing.graceMs);
194
- stop = this.stopFor(session, scope, commands, options.sessionId);
253
+ const turnStop = this.stopFor(session, scope, commands, options.sessionId);
254
+ stop = turnStop;
255
+ // Codex's turn, once there is one; before, its process, or a managed
256
+ // turn's cleanup.
257
+ this.cancelWith(options, () => turn?.cancel() ?? turnStop.cancel());
195
258
  signal.throwIfAborted();
196
- const offPreparing = onAbort(signal, () => void stop?.cancel());
259
+ const offPreparing = onAbort(signal, () => void turnStop.cancel());
197
260
  try {
198
261
  await session.mcp.prepare(session.threadId, mcp, options.onWarning);
199
262
  signal.throwIfAborted();
@@ -204,10 +267,17 @@ export class CodexAppServerAgent extends SessionAgent {
204
267
  open: (deliver: DeliverSteer) => void this.openSteering(options, deliver),
205
268
  hold: () => this.holdSteering(options),
206
269
  };
207
- turn = new CodexTurn(session, scope, options, commands, stop, steering, this.timing);
208
- this.turns.set(controller, turn);
209
- yield* turn.run(userInput);
270
+ turn = new CodexTurn(session, scope, options, commands, turnStop, steering, this.timing);
271
+ for await (const event of turn.run(userInput)) {
272
+ nameStarted();
273
+ yield event;
274
+ }
210
275
  } catch (error) {
276
+ // A turn Codex took is on the thread, whatever came of it. A session
277
+ // released meanwhile refuses the name; the turn ends as it was ending.
278
+ try {
279
+ nameStarted();
280
+ } catch {}
211
281
  if (!signal.aborted) throw error;
212
282
  if (turn) yield* turn.endCancelled();
213
283
  throw new Error("turn aborted");
@@ -218,6 +288,19 @@ export class CodexAppServerAgent extends SessionAgent {
218
288
  }
219
289
  }
220
290
 
291
+ /**
292
+ * The user's own login, for a turn (or a model list) whose `env` names a
293
+ * home, when the operator set `userLogin`: read again for each, so a login
294
+ * the user changed is the next turn's. None otherwise: a turn that names
295
+ * no home runs on its home's own login.
296
+ */
297
+ private userLoginFor(
298
+ env: Record<string, string> | undefined,
299
+ ): ((signal?: AbortSignal) => Promise<CodexAuth>) | undefined {
300
+ if (!this.agentOptions.userLogin || !env?.CODEX_HOME) return undefined;
301
+ return (signal) => userAuth(this.readUserLogin, signal);
302
+ }
303
+
221
304
  /** How the turn stops: by Codex's interrupt, or, managed, with its native cleanup. */
222
305
  private stopFor(
223
306
  session: CodexSession,
@@ -229,7 +312,7 @@ export class CodexAppServerAgent extends SessionAgent {
229
312
  const { graceMs } = this.timing;
230
313
  if (session.subagentMode !== "turn")
231
314
  return new InterruptStop(session, commands, scope.signal, graceMs);
232
- scope.subagents = new CodexTurnSubagents(client, threadId, graceMs);
315
+ scope.subagents = new CodexTurnSubagents(client, threadId);
233
316
  return new ManagedStop(scope.subagents, client, threadId, commands, scope.signal, graceMs, () =>
234
317
  this.sessions.quarantine(sessionId, client),
235
318
  );
@@ -256,6 +339,7 @@ export class CodexAppServerAgent extends SessionAgent {
256
339
  credentials: this.agentOptions.credentials,
257
340
  sessionId: options.sessionId,
258
341
  apiKey: options.apiKey,
342
+ userLogin: this.userLoginFor(options.env),
259
343
  ...(options.signal && { signal: options.signal }),
260
344
  });
261
345
  options.signal?.throwIfAborted();
@@ -270,20 +354,9 @@ export class CodexAppServerAgent extends SessionAgent {
270
354
  return codexModelDescriptors(listing.models, this.harness, listing.configuredModel);
271
355
  }
272
356
 
273
- override async cancel(sessionId: string): Promise<CancelResult> {
274
- // Snapshot the in-flight controllers before super.cancel() aborts them:
275
- // the abort unblocks execute()'s queue and finally's endTurn drains the
276
- // set on a later microtask, so reading afterwards would race that drain.
277
- const controllers = [...(this.inflight.get(sessionId) ?? [])];
278
- const base = await super.cancel(sessionId);
279
- if (!base.hadTurn) {
280
- const stopping = this.sessions.stopUnsettled(sessionId);
281
- return stopping ? { confirmed: await stopping, hadTurn: false } : base;
282
- }
283
- const acknowledgements = await Promise.all(
284
- controllers.map((controller) => this.turns.get(controller)?.acknowledgement ?? false),
285
- );
286
- return { confirmed: acknowledgements.every(Boolean), hadTurn: true };
357
+ /** With no turn running, a managed session's process not yet confirmed stopped. */
358
+ protected override stopWithoutTurn(sessionId: string): Promise<boolean> | undefined {
359
+ return this.sessions.stopUnsettled(sessionId);
287
360
  }
288
361
 
289
362
  /** Unchanged MCP config reloads retain dead remote connections after VM resume. */
@@ -291,8 +364,8 @@ export class CodexAppServerAgent extends SessionAgent {
291
364
  this.sessions.invalidate();
292
365
  }
293
366
 
294
- loginChanged(): void {
295
- this.sessions.loginChanged();
367
+ loginChanged(store?: string): void {
368
+ this.sessions.loginChanged(store);
296
369
  }
297
370
 
298
371
  override async release(sessionId: string): Promise<void> {
@@ -301,17 +374,24 @@ export class CodexAppServerAgent extends SessionAgent {
301
374
  if (this.sessions.managed(sessionId) && !(await this.cancel(sessionId)).confirmed)
302
375
  throw new Error("Codex native cleanup was not confirmed; cannot release the session");
303
376
  await super.release(sessionId);
304
- this.sessions.release(sessionId);
377
+ // @ref LLP 0001#d10--built-in-turns-own-their-logical-session-through-cleanup — released once its process has exited, as a Claude session is
378
+ await this.sessions.release(sessionId);
305
379
  }
306
380
 
307
381
  override async terminateAll(): Promise<void> {
308
382
  await super.terminateAll();
309
- this.sessions.terminateAll();
383
+ await this.sessions.terminateAll();
310
384
  }
311
385
 
312
- /** The CLI to run: the provisioner's, else `$CODEX_CLI_PATH`; PATH's `codex` when neither. */
313
- private async cliPath(): Promise<string | undefined> {
314
- return (await this.agentOptions.resolveCliPath?.()) ?? process.env.CODEX_CLI_PATH;
386
+ /**
387
+ * The CLI to run: the provisioner's, else `$CODEX_CLI_PATH`; PATH's `codex`
388
+ * when neither. A turn waits for it until the turn is cancelled.
389
+ */
390
+ private async cliPath(signal?: AbortSignal): Promise<string | undefined> {
391
+ // @ref LLP 0010#a-turn-waits-for-its-cli-until-it-is-cancelled — the install is the server's
392
+ const { resolveCliPath } = this.agentOptions;
393
+ const resolved = resolveCliPath && (await untilAborted(resolveCliPath(), signal));
394
+ return resolved ?? process.env.CODEX_CLI_PATH;
315
395
  }
316
396
 
317
397
  /**
@@ -329,10 +409,16 @@ export class CodexAppServerAgent extends SessionAgent {
329
409
  },
330
410
  ): Promise<CodexConnection> {
331
411
  const { auth, experimentalApi, signal, sessionId } = spawn;
332
- const codexPath = (await this.cliPath()) ?? "codex";
412
+ const codexPath = (await this.cliPath(signal)) ?? "codex";
333
413
  signal?.throwIfAborted();
334
414
  const clientOptions = {
335
- ...clientOptionsFor(codexPath, options, auth, experimentalApi),
415
+ ...clientOptionsFor(
416
+ codexPath,
417
+ options,
418
+ auth,
419
+ experimentalApi,
420
+ this.agentOptions.inheritConfig,
421
+ ),
336
422
  processes: this.processes,
337
423
  ...(sessionId !== undefined && { sessionId }),
338
424
  };
@@ -134,7 +134,7 @@ export class TurnCommands {
134
134
  constructor(
135
135
  private readonly client: CodexConnection,
136
136
  private readonly threadId: string,
137
- /** How long a stop has to confirm the commands stopped (`cancelGraceMs`). */
137
+ /** How long the commands have to stop before Codex's own shutdown stops them (`cancelGraceMs`). */
138
138
  private readonly timeoutMs: number,
139
139
  ) {}
140
140
 
@@ -206,7 +206,7 @@ export class TurnCommands {
206
206
  this.commands,
207
207
  this.timeoutMs,
208
208
  );
209
- return unstopped.length && (await this.client.closeAndWait(this.timeoutMs)) ? [] : unstopped;
209
+ return unstopped.length && (await this.client.closeAndWait()) ? [] : unstopped;
210
210
  })();
211
211
  return this.stopping;
212
212
  }
@@ -32,7 +32,9 @@ const chatGptTokens = (credential: Extract<CodexCredential, { type: "chatgpt" }>
32
32
  * holds (`credentials`), an API key or a ChatGPT access token kept in the
33
33
  * app-server's memory, which asks the product again (with the token it
34
34
  * refused) when it needs a fresher one; without one, the turn's own `apiKey`;
35
- * without that, the CLI's own login.
35
+ * without that, the user's own login (`userLogin`, given for a turn that
36
+ * names a home of its session's own when the operator set it); without
37
+ * that, the CLI's own login.
36
38
  */
37
39
  export async function authFor(ask: {
38
40
  credentials: CredentialSource | undefined;
@@ -40,14 +42,17 @@ export async function authFor(ask: {
40
42
  sessionId: string | undefined;
41
43
  turnId?: string;
42
44
  apiKey: string | undefined;
45
+ /** The user's own login, read from Codex (`user-login.ts`); it throws when there is none. */
46
+ userLogin?: (signal?: AbortSignal) => Promise<CodexAuth>;
43
47
  signal?: AbortSignal;
44
48
  }): Promise<CodexAuth | undefined> {
45
- const { credentials, sessionId, turnId, apiKey, signal } = ask;
46
- const ownKey = (): CodexAuth | undefined => (apiKey ? { type: "apiKey", apiKey } : undefined);
47
- if (!credentials || !sessionId) return ownKey();
49
+ const { credentials, sessionId, turnId, apiKey, userLogin, signal } = ask;
50
+ const uncredentialed = async (): Promise<CodexAuth | undefined> =>
51
+ apiKey ? { type: "apiKey", apiKey } : userLogin?.(signal);
52
+ if (!credentials || !sessionId) return uncredentialed();
48
53
  const request = { harness: "codex-app-server" as const, sessionId, ...(turnId && { turnId }) };
49
54
  const credential = await credentialFor(credentials, request, signal);
50
- if (!credential) return ownKey();
55
+ if (!credential) return uncredentialed();
51
56
  if (credential.type === "api_key") return { type: "apiKey", apiKey: credential.apiKey };
52
57
  let current = credential;
53
58
  return {