@zvada/agent-server 0.3.22 → 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 (348) hide show
  1. package/CHANGELOG.md +261 -0
  2. package/dist/types/accounts/chatgpt.d.ts +1 -1
  3. package/dist/types/accounts/chatgpt.d.ts.map +1 -1
  4. package/dist/types/accounts/http.d.ts +2 -4
  5. package/dist/types/accounts/http.d.ts.map +1 -1
  6. package/dist/types/client/client.d.ts +37 -11
  7. package/dist/types/client/client.d.ts.map +1 -1
  8. package/dist/types/client/connection.d.ts +64 -13
  9. package/dist/types/client/connection.d.ts.map +1 -1
  10. package/dist/types/client/index.d.ts +1 -1
  11. package/dist/types/client/index.d.ts.map +1 -1
  12. package/dist/types/client/logins.d.ts +2 -1
  13. package/dist/types/client/logins.d.ts.map +1 -1
  14. package/dist/types/client/streams.d.ts +9 -3
  15. package/dist/types/client/streams.d.ts.map +1 -1
  16. package/dist/types/client/turns.d.ts +1 -1
  17. package/dist/types/client/turns.d.ts.map +1 -1
  18. package/dist/types/core/agents/acp/acp-agent.d.ts +34 -1
  19. package/dist/types/core/agents/acp/acp-agent.d.ts.map +1 -1
  20. package/dist/types/core/agents/acp/adapter.d.ts.map +1 -1
  21. package/dist/types/core/agents/acp/client.d.ts +12 -3
  22. package/dist/types/core/agents/acp/client.d.ts.map +1 -1
  23. package/dist/types/core/agents/acp/mappings.d.ts +8 -2
  24. package/dist/types/core/agents/acp/mappings.d.ts.map +1 -1
  25. package/dist/types/core/agents/base.d.ts +22 -8
  26. package/dist/types/core/agents/base.d.ts.map +1 -1
  27. package/dist/types/core/agents/claude-code/adapter.d.ts +7 -0
  28. package/dist/types/core/agents/claude-code/adapter.d.ts.map +1 -1
  29. package/dist/types/core/agents/claude-code/approvals.d.ts +4 -1
  30. package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
  31. package/dist/types/core/agents/claude-code/claude-agent.d.ts +16 -5
  32. package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
  33. package/dist/types/core/agents/claude-code/events.d.ts +9 -1
  34. package/dist/types/core/agents/claude-code/events.d.ts.map +1 -1
  35. package/dist/types/core/agents/claude-code/login.d.ts +4 -1
  36. package/dist/types/core/agents/claude-code/login.d.ts.map +1 -1
  37. package/dist/types/core/agents/claude-code/mcp.d.ts +21 -7
  38. package/dist/types/core/agents/claude-code/mcp.d.ts.map +1 -1
  39. package/dist/types/core/agents/claude-code/models.d.ts +5 -1
  40. package/dist/types/core/agents/claude-code/models.d.ts.map +1 -1
  41. package/dist/types/core/agents/claude-code/options.d.ts +10 -4
  42. package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
  43. package/dist/types/core/agents/claude-code/outcome.d.ts +3 -5
  44. package/dist/types/core/agents/claude-code/outcome.d.ts.map +1 -1
  45. package/dist/types/core/agents/claude-code/results.d.ts +16 -0
  46. package/dist/types/core/agents/claude-code/results.d.ts.map +1 -0
  47. package/dist/types/core/agents/claude-code/sdk-pair.d.ts +13 -0
  48. package/dist/types/core/agents/claude-code/sdk-pair.d.ts.map +1 -0
  49. package/dist/types/core/agents/claude-code/session.d.ts +50 -12
  50. package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
  51. package/dist/types/core/agents/claude-code/spend.d.ts +2 -1
  52. package/dist/types/core/agents/claude-code/spend.d.ts.map +1 -1
  53. package/dist/types/core/agents/claude-code/tools.d.ts +26 -3
  54. package/dist/types/core/agents/claude-code/tools.d.ts.map +1 -1
  55. package/dist/types/core/agents/codex-app-server/adapter.d.ts +2 -2
  56. package/dist/types/core/agents/codex-app-server/adapter.d.ts.map +1 -1
  57. package/dist/types/core/agents/codex-app-server/client.d.ts +12 -2
  58. package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -1
  59. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts +58 -13
  60. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts.map +1 -1
  61. package/dist/types/core/agents/codex-app-server/commands.d.ts +2 -2
  62. package/dist/types/core/agents/codex-app-server/commands.d.ts.map +1 -1
  63. package/dist/types/core/agents/codex-app-server/credentials.d.ts +5 -1
  64. package/dist/types/core/agents/codex-app-server/credentials.d.ts.map +1 -1
  65. package/dist/types/core/agents/codex-app-server/effort.d.ts +2 -1
  66. package/dist/types/core/agents/codex-app-server/effort.d.ts.map +1 -1
  67. package/dist/types/core/agents/codex-app-server/homes.d.ts +19 -0
  68. package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
  69. package/dist/types/core/agents/codex-app-server/items.d.ts +27 -2
  70. package/dist/types/core/agents/codex-app-server/items.d.ts.map +1 -1
  71. package/dist/types/core/agents/codex-app-server/login.d.ts +4 -1
  72. package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -1
  73. package/dist/types/core/agents/codex-app-server/mcp.d.ts +15 -30
  74. package/dist/types/core/agents/codex-app-server/mcp.d.ts.map +1 -1
  75. package/dist/types/core/agents/codex-app-server/models.d.ts +3 -10
  76. package/dist/types/core/agents/codex-app-server/models.d.ts.map +1 -1
  77. package/dist/types/core/agents/codex-app-server/options.d.ts +27 -4
  78. package/dist/types/core/agents/codex-app-server/options.d.ts.map +1 -1
  79. package/dist/types/core/agents/codex-app-server/plan-approval.d.ts +2 -1
  80. package/dist/types/core/agents/codex-app-server/plan-approval.d.ts.map +1 -1
  81. package/dist/types/core/agents/codex-app-server/protocol.d.ts +16 -3
  82. package/dist/types/core/agents/codex-app-server/protocol.d.ts.map +1 -1
  83. package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
  84. package/dist/types/core/agents/codex-app-server/session.d.ts +68 -40
  85. package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
  86. package/dist/types/core/agents/codex-app-server/subagents.d.ts +9 -4
  87. package/dist/types/core/agents/codex-app-server/subagents.d.ts.map +1 -1
  88. package/dist/types/core/agents/codex-app-server/thread-settings.d.ts +3 -2
  89. package/dist/types/core/agents/codex-app-server/thread-settings.d.ts.map +1 -1
  90. package/dist/types/core/agents/codex-app-server/turn-items.d.ts +2 -1
  91. package/dist/types/core/agents/codex-app-server/turn-items.d.ts.map +1 -1
  92. package/dist/types/core/agents/codex-app-server/turn.d.ts +31 -8
  93. package/dist/types/core/agents/codex-app-server/turn.d.ts.map +1 -1
  94. package/dist/types/core/agents/codex-app-server/user-login.d.ts +28 -0
  95. package/dist/types/core/agents/codex-app-server/user-login.d.ts.map +1 -0
  96. package/dist/types/core/agents/credentials.d.ts +3 -1
  97. package/dist/types/core/agents/credentials.d.ts.map +1 -1
  98. package/dist/types/core/agents/diffs.d.ts +9 -0
  99. package/dist/types/core/agents/diffs.d.ts.map +1 -0
  100. package/dist/types/core/agents/elicitation.d.ts +3 -2
  101. package/dist/types/core/agents/elicitation.d.ts.map +1 -1
  102. package/dist/types/core/agents/login.d.ts +6 -0
  103. package/dist/types/core/agents/login.d.ts.map +1 -1
  104. package/dist/types/core/agents/mcp-config.d.ts +0 -4
  105. package/dist/types/core/agents/mcp-config.d.ts.map +1 -1
  106. package/dist/types/core/agents/process-pool.d.ts +193 -0
  107. package/dist/types/core/agents/process-pool.d.ts.map +1 -0
  108. package/dist/types/core/agents/scripted/adapter.d.ts +68 -0
  109. package/dist/types/core/agents/scripted/adapter.d.ts.map +1 -0
  110. package/dist/types/core/agents/scripted/mcp-call.d.ts +56 -0
  111. package/dist/types/core/agents/scripted/mcp-call.d.ts.map +1 -0
  112. package/dist/types/core/agents/scripted/scripted-agent.d.ts +73 -0
  113. package/dist/types/core/agents/scripted/scripted-agent.d.ts.map +1 -0
  114. package/dist/types/core/agents/scripted/scripts.d.ts +29 -0
  115. package/dist/types/core/agents/scripted/scripts.d.ts.map +1 -0
  116. package/dist/types/core/agents/scripted/steps.d.ts +132 -0
  117. package/dist/types/core/agents/scripted/steps.d.ts.map +1 -0
  118. package/dist/types/core/agents/session-agent.d.ts +49 -6
  119. package/dist/types/core/agents/session-agent.d.ts.map +1 -1
  120. package/dist/types/core/diagnostics.d.ts +4 -1
  121. package/dist/types/core/diagnostics.d.ts.map +1 -1
  122. package/dist/types/core/index.d.ts +5 -0
  123. package/dist/types/core/index.d.ts.map +1 -1
  124. package/dist/types/core/presets.d.ts +32 -6
  125. package/dist/types/core/presets.d.ts.map +1 -1
  126. package/dist/types/core/processes/lifeline.d.ts +0 -20
  127. package/dist/types/core/processes/lifeline.d.ts.map +1 -1
  128. package/dist/types/core/provision/index.d.ts +2 -2
  129. package/dist/types/core/provision/index.d.ts.map +1 -1
  130. package/dist/types/core/provision/install-build.d.ts +2 -1
  131. package/dist/types/core/provision/install-build.d.ts.map +1 -1
  132. package/dist/types/core/provision/npm.d.ts +20 -8
  133. package/dist/types/core/provision/npm.d.ts.map +1 -1
  134. package/dist/types/core/provision/provisioner.d.ts +4 -1
  135. package/dist/types/core/provision/provisioner.d.ts.map +1 -1
  136. package/dist/types/core/provision/tools.d.ts +2 -1
  137. package/dist/types/core/provision/tools.d.ts.map +1 -1
  138. package/dist/types/core/runtime/agent-runtime.d.ts +16 -8
  139. package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
  140. package/dist/types/core/runtime/event-processor.d.ts +5 -1
  141. package/dist/types/core/runtime/event-processor.d.ts.map +1 -1
  142. package/dist/types/core/runtime/harness-clis.d.ts +9 -0
  143. package/dist/types/core/runtime/harness-clis.d.ts.map +1 -1
  144. package/dist/types/core/runtime/harness-logins.d.ts +33 -5
  145. package/dist/types/core/runtime/harness-logins.d.ts.map +1 -1
  146. package/dist/types/core/runtime/model-lists.d.ts +2 -0
  147. package/dist/types/core/runtime/model-lists.d.ts.map +1 -1
  148. package/dist/types/core/runtime/permission-policy.d.ts +5 -3
  149. package/dist/types/core/runtime/permission-policy.d.ts.map +1 -1
  150. package/dist/types/core/runtime/request-broker.d.ts +2 -1
  151. package/dist/types/core/runtime/request-broker.d.ts.map +1 -1
  152. package/dist/types/core/runtime/turn.d.ts +12 -1
  153. package/dist/types/core/runtime/turn.d.ts.map +1 -1
  154. package/dist/types/core/utils/deadline.d.ts +6 -0
  155. package/dist/types/core/utils/deadline.d.ts.map +1 -1
  156. package/dist/types/core/utils/errors.d.ts +7 -1
  157. package/dist/types/core/utils/errors.d.ts.map +1 -1
  158. package/dist/types/operator/operator.d.ts.map +1 -1
  159. package/dist/types/operator/render.d.ts +6 -4
  160. package/dist/types/operator/render.d.ts.map +1 -1
  161. package/dist/types/protocol/auth.d.ts +54 -24
  162. package/dist/types/protocol/auth.d.ts.map +1 -1
  163. package/dist/types/protocol/clis.d.ts +14 -7
  164. package/dist/types/protocol/clis.d.ts.map +1 -1
  165. package/dist/types/protocol/config.d.ts +2 -0
  166. package/dist/types/protocol/config.d.ts.map +1 -1
  167. package/dist/types/protocol/conversation.d.ts +40 -3
  168. package/dist/types/protocol/conversation.d.ts.map +1 -1
  169. package/dist/types/protocol/credentials.d.ts +7 -0
  170. package/dist/types/protocol/credentials.d.ts.map +1 -1
  171. package/dist/types/protocol/execution.d.ts +2 -1
  172. package/dist/types/protocol/execution.d.ts.map +1 -1
  173. package/dist/types/protocol/factories.d.ts +12 -2
  174. package/dist/types/protocol/factories.d.ts.map +1 -1
  175. package/dist/types/protocol/harness.d.ts +6 -2
  176. package/dist/types/protocol/harness.d.ts.map +1 -1
  177. package/dist/types/protocol/history.d.ts +19 -0
  178. package/dist/types/protocol/history.d.ts.map +1 -1
  179. package/dist/types/protocol/lifecycle.d.ts +47 -8
  180. package/dist/types/protocol/lifecycle.d.ts.map +1 -1
  181. package/dist/types/protocol/model-catalog.d.ts.map +1 -1
  182. package/dist/types/protocol/models.d.ts +3 -0
  183. package/dist/types/protocol/models.d.ts.map +1 -1
  184. package/dist/types/protocol/parts.d.ts +14 -0
  185. package/dist/types/protocol/parts.d.ts.map +1 -1
  186. package/dist/types/protocol/tool-state.d.ts +12 -0
  187. package/dist/types/protocol/tool-state.d.ts.map +1 -1
  188. package/dist/types/protocol/wire-api.d.ts +129 -23
  189. package/dist/types/protocol/wire-api.d.ts.map +1 -1
  190. package/dist/types/protocol/wire.d.ts +70 -10
  191. package/dist/types/protocol/wire.d.ts.map +1 -1
  192. package/dist/types/server/acp/binding.d.ts +6 -1
  193. package/dist/types/server/acp/binding.d.ts.map +1 -1
  194. package/dist/types/server/acp/translate.d.ts +33 -7
  195. package/dist/types/server/acp/translate.d.ts.map +1 -1
  196. package/dist/types/server/agent-server.d.ts +6 -0
  197. package/dist/types/server/agent-server.d.ts.map +1 -1
  198. package/dist/types/server/feedback.d.ts +2 -1
  199. package/dist/types/server/feedback.d.ts.map +1 -1
  200. package/dist/types/server/install.d.ts +11 -0
  201. package/dist/types/server/install.d.ts.map +1 -1
  202. package/dist/types/server/session-gate.d.ts +2 -1
  203. package/dist/types/server/session-gate.d.ts.map +1 -1
  204. package/dist/types/server/session-store.d.ts +1 -0
  205. package/dist/types/server/session-store.d.ts.map +1 -1
  206. package/dist/types/server/subscriptions.d.ts +2 -1
  207. package/dist/types/server/subscriptions.d.ts.map +1 -1
  208. package/dist/types/server/transports.d.ts.map +1 -1
  209. package/dist/types/sync/fit.d.ts +8 -7
  210. package/dist/types/sync/fit.d.ts.map +1 -1
  211. package/dist/types/sync/history-sqlite.d.ts.map +1 -1
  212. package/dist/types/sync/index.d.ts +1 -0
  213. package/dist/types/sync/index.d.ts.map +1 -1
  214. package/dist/types/sync/journal-sqlite.d.ts +2 -1
  215. package/dist/types/sync/journal-sqlite.d.ts.map +1 -1
  216. package/dist/types/sync/journal.d.ts +11 -3
  217. package/dist/types/sync/journal.d.ts.map +1 -1
  218. package/dist/types/sync/projection.d.ts +6 -0
  219. package/dist/types/sync/projection.d.ts.map +1 -1
  220. package/dist/types/sync/recovery.d.ts +2 -1
  221. package/dist/types/sync/recovery.d.ts.map +1 -1
  222. package/dist/types/sync/schema.d.ts +10 -0
  223. package/dist/types/sync/schema.d.ts.map +1 -1
  224. package/dist/types/sync/sql.d.ts +2 -1
  225. package/dist/types/sync/sql.d.ts.map +1 -1
  226. package/docs/consuming.md +278 -28
  227. package/docs/deploy.md +36 -8
  228. package/docs/durable-sessions.md +35 -5
  229. package/docs/harnesses.md +385 -24
  230. package/package.json +6 -1
  231. package/src/accounts/chatgpt.ts +2 -2
  232. package/src/accounts/http.ts +2 -16
  233. package/src/client/client.ts +92 -31
  234. package/src/client/connection.ts +103 -22
  235. package/src/client/index.ts +1 -0
  236. package/src/client/logins.ts +1 -1
  237. package/src/client/streams.ts +12 -4
  238. package/src/client/turns.ts +1 -1
  239. package/src/core/agents/acp/acp-agent.ts +181 -52
  240. package/src/core/agents/acp/adapter.ts +9 -1
  241. package/src/core/agents/acp/client.ts +16 -4
  242. package/src/core/agents/acp/mappings.ts +27 -0
  243. package/src/core/agents/base.ts +25 -10
  244. package/src/core/agents/claude-code/adapter.ts +47 -17
  245. package/src/core/agents/claude-code/approvals.ts +6 -5
  246. package/src/core/agents/claude-code/claude-agent.ts +60 -30
  247. package/src/core/agents/claude-code/events.ts +11 -1
  248. package/src/core/agents/claude-code/login.ts +14 -2
  249. package/src/core/agents/claude-code/mcp.ts +31 -15
  250. package/src/core/agents/claude-code/models.ts +7 -2
  251. package/src/core/agents/claude-code/options.ts +11 -3
  252. package/src/core/agents/claude-code/outcome.ts +10 -2
  253. package/src/core/agents/claude-code/results.ts +73 -0
  254. package/src/core/agents/claude-code/sdk-pair.ts +66 -0
  255. package/src/core/agents/claude-code/session.ts +166 -37
  256. package/src/core/agents/claude-code/spend.ts +1 -1
  257. package/src/core/agents/claude-code/tools.ts +108 -12
  258. package/src/core/agents/codex-app-server/adapter.ts +37 -12
  259. package/src/core/agents/codex-app-server/client.ts +37 -8
  260. package/src/core/agents/codex-app-server/codex-agent.ts +115 -50
  261. package/src/core/agents/codex-app-server/commands.ts +2 -2
  262. package/src/core/agents/codex-app-server/credentials.ts +10 -5
  263. package/src/core/agents/codex-app-server/effort.ts +1 -1
  264. package/src/core/agents/codex-app-server/homes.ts +43 -1
  265. package/src/core/agents/codex-app-server/items.ts +219 -31
  266. package/src/core/agents/codex-app-server/login.ts +14 -2
  267. package/src/core/agents/codex-app-server/mcp.ts +47 -32
  268. package/src/core/agents/codex-app-server/models.ts +9 -8
  269. package/src/core/agents/codex-app-server/options.ts +69 -10
  270. package/src/core/agents/codex-app-server/plan-approval.ts +1 -1
  271. package/src/core/agents/codex-app-server/protocol.ts +14 -4
  272. package/src/core/agents/codex-app-server/requests.ts +14 -6
  273. package/src/core/agents/codex-app-server/session.ts +151 -113
  274. package/src/core/agents/codex-app-server/subagents.ts +21 -14
  275. package/src/core/agents/codex-app-server/thread-settings.ts +18 -8
  276. package/src/core/agents/codex-app-server/turn-items.ts +1 -1
  277. package/src/core/agents/codex-app-server/turn.ts +79 -32
  278. package/src/core/agents/codex-app-server/user-login.ts +107 -0
  279. package/src/core/agents/credentials.ts +5 -2
  280. package/src/core/agents/diffs.ts +25 -0
  281. package/src/core/agents/elicitation.ts +2 -2
  282. package/src/core/agents/login.ts +6 -0
  283. package/src/core/agents/mcp-config.ts +2 -2
  284. package/src/core/agents/process-pool.ts +432 -0
  285. package/src/core/agents/scripted/adapter.ts +142 -0
  286. package/src/core/agents/scripted/mcp-call.ts +134 -0
  287. package/src/core/agents/scripted/scripted-agent.ts +418 -0
  288. package/src/core/agents/scripted/scripts.ts +171 -0
  289. package/src/core/agents/scripted/steps.ts +127 -0
  290. package/src/core/agents/session-agent.ts +105 -8
  291. package/src/core/diagnostics.ts +9 -1
  292. package/src/core/index.ts +11 -0
  293. package/src/core/presets.ts +99 -14
  294. package/src/core/processes/lifeline.ts +99 -25
  295. package/src/core/provision/index.ts +1 -2
  296. package/src/core/provision/install-build.ts +4 -1
  297. package/src/core/provision/npm.ts +309 -97
  298. package/src/core/provision/provisioner.ts +22 -8
  299. package/src/core/provision/tools.ts +44 -19
  300. package/src/core/runtime/agent-runtime.ts +48 -18
  301. package/src/core/runtime/event-processor.ts +18 -2
  302. package/src/core/runtime/harness-clis.ts +36 -1
  303. package/src/core/runtime/harness-logins.ts +82 -31
  304. package/src/core/runtime/model-lists.ts +1 -1
  305. package/src/core/runtime/permission-policy.ts +4 -3
  306. package/src/core/runtime/request-broker.ts +1 -1
  307. package/src/core/runtime/turn.ts +51 -3
  308. package/src/core/utils/deadline.ts +23 -0
  309. package/src/core/utils/errors.ts +11 -4
  310. package/src/operator/operator.ts +8 -4
  311. package/src/operator/render.ts +46 -8
  312. package/src/protocol/auth.ts +30 -23
  313. package/src/protocol/clis.ts +12 -4
  314. package/src/protocol/credentials.ts +19 -0
  315. package/src/protocol/execution.ts +5 -0
  316. package/src/protocol/factories.ts +25 -3
  317. package/src/protocol/harness.ts +11 -1
  318. package/src/protocol/history.ts +20 -0
  319. package/src/protocol/lifecycle.ts +10 -1
  320. package/src/protocol/model-catalog.ts +19 -8
  321. package/src/protocol/parts.ts +12 -1
  322. package/src/protocol/tool-state.ts +14 -1
  323. package/src/protocol/wire.ts +14 -0
  324. package/src/server/acp/binding.ts +113 -23
  325. package/src/server/acp/translate.ts +161 -8
  326. package/src/server/agent-server.ts +10 -1
  327. package/src/server/bin.ts +83 -9
  328. package/src/server/feedback.ts +1 -1
  329. package/src/server/install.ts +82 -4
  330. package/src/server/session-gate.ts +1 -1
  331. package/src/server/session-store.ts +5 -1
  332. package/src/server/subscriptions.ts +1 -1
  333. package/src/server/transports.ts +14 -0
  334. package/src/sync/fit.ts +115 -28
  335. package/src/sync/history-sqlite.ts +6 -2
  336. package/src/sync/index.ts +1 -0
  337. package/src/sync/journal-sqlite.ts +20 -2
  338. package/src/sync/journal.ts +10 -2
  339. package/src/sync/projection.ts +10 -0
  340. package/src/sync/recovery.ts +11 -4
  341. package/src/sync/schema.ts +7 -5
  342. package/src/sync/sql.ts +1 -1
  343. package/dist/types/core/agents/claude-code/session-pool.d.ts +0 -36
  344. package/dist/types/core/agents/claude-code/session-pool.d.ts.map +0 -1
  345. package/dist/types/core/agents/session-pool.d.ts +0 -41
  346. package/dist/types/core/agents/session-pool.d.ts.map +0 -1
  347. package/src/core/agents/claude-code/session-pool.ts +0 -136
  348. package/src/core/agents/session-pool.ts +0 -111
@@ -9,7 +9,8 @@ import type { CodexAuth } from "./credentials.ts";
9
9
  * Why a turn without tools (`disableTools`) fails on Codex. With every
10
10
  * feature that offers one turned off, Codex 0.160 still gives a code-mode
11
11
  * model its `exec` and `wait`, `apply_patch` inside `exec`, its sub-agent
12
- * tools and `request_user_input` (`.context/rehearsal/m3-tools.ts`), so the
12
+ * tools and `request_user_input` (LLP 0017, Codex's own tools, which found
13
+ * the same with every `builtInTools` group off), so the
13
14
  * model would still act: the turn is refused instead of running tools the
14
15
  * caller turned off.
15
16
  */
@@ -91,12 +92,16 @@ export function codexSessionCompatible(
91
92
  );
92
93
  }
93
94
 
94
- /** How the harness starts `codex app-server` for a folder, its `env` and a login. */
95
+ /**
96
+ * How the harness starts `codex app-server` for a folder, its `env` and a
97
+ * login, at the operator's `inheritConfig`.
98
+ */
95
99
  export function clientOptionsFor(
96
100
  codexPath: string,
97
101
  { cwd, env }: { cwd: string; env?: Record<string, string> },
98
102
  auth: CodexAuth | undefined,
99
103
  experimentalApi: boolean,
104
+ inheritConfig: InheritConfig = "all",
100
105
  ): Omit<CodexAppServerClientOptions, "processes"> {
101
106
  return {
102
107
  codexPath,
@@ -104,6 +109,12 @@ export function clientOptionsFor(
104
109
  env: env ? { ...process.env, ...env } : process.env,
105
110
  ...(experimentalApi && { experimentalApi: true }),
106
111
  ...(auth && { ephemeralCredentials: true }),
112
+ // @ref LLP 0001#d17--a-turn-inherits-what-the-operator-chooses-all-the-projects-or-none — a session's home loads no plugins
113
+ // Below `all` a home a turn names is its session's own: none of the
114
+ // user's plugins belong there, and a fresh home clones Codex's plugin
115
+ // catalogue (about 84 MB) as the process starts, before a thread's
116
+ // config could turn it off.
117
+ ...(inheritConfig !== "all" && env?.CODEX_HOME && { pluginsOff: true }),
107
118
  };
108
119
  }
109
120
 
@@ -111,6 +122,28 @@ export function clientOptionsFor(
111
122
  const MANAGED_SUBAGENTS =
112
123
  "Collect the results of every subagent before ending your turn. Stop any worker you no longer need. Delegated work cannot continue after this task completes.";
113
124
 
125
+ /**
126
+ * Codex's own tools, by group, that the operator leaves on (the default for
127
+ * each, Codex's own behaviour) or turns off for every thread (LLP 0017).
128
+ */
129
+ export interface CodexBuiltInTools {
130
+ /** The local environment: the shell, unified exec, `apply_patch`, `view_image`, `write_stdin`. */
131
+ local?: boolean;
132
+ webSearch?: boolean;
133
+ imageGeneration?: boolean;
134
+ }
135
+
136
+ /** What the operator decides for every thread the harness opens. Never from the wire. */
137
+ export interface CodexThreadPolicy {
138
+ inheritConfig?: InheritConfig;
139
+ builtInTools?: CodexBuiltInTools;
140
+ }
141
+
142
+ /** `thread/start` and `thread/resume` params, as far as the engine reads them. */
143
+ export interface CodexThreadParams extends Record<string, unknown> {
144
+ config: Record<string, unknown>;
145
+ }
146
+
114
147
  /**
115
148
  * The params `thread/start` and `thread/resume` take for a turn with these
116
149
  * options. `mcpServers`: the servers the thread carries in its own config,
@@ -118,22 +151,48 @@ const MANAGED_SUBAGENTS =
118
151
  */
119
152
  export function threadParams(
120
153
  options: AgentExecuteOptions,
121
- inheritConfig: InheritConfig = "all",
154
+ policy: CodexThreadPolicy = {},
122
155
  mcpServers?: Record<string, unknown>,
123
- ): Record<string, unknown> {
156
+ ): CodexThreadParams {
124
157
  const identity = processIdentity(options);
125
- // @ref LLP 0001#d14--nothing-the-agent-does-is-off-the-stream — Codex continues a goal by itself
126
- // While a thread goal is active, Codex starts the next turn itself.
127
- const config: Record<string, unknown> = { "features.goals": false };
158
+ const tools = policy.builtInTools ?? {};
159
+ // @ref LLP 0017#one-nested-config — dotted keys beside a table apply in no fixed order
160
+ // One nested object, never a dotted key: Codex applies a dotted key and a
161
+ // table of the same root in an order that varies.
162
+ const features: Record<string, boolean> = {
163
+ // @ref LLP 0001#d14--nothing-the-agent-does-is-off-the-stream — Codex continues a goal by itself
164
+ // While a thread goal is active, Codex starts the next turn itself.
165
+ goals: false,
166
+ // @ref LLP 0001#d12--what-the-agent-asks-the-user-is-first-class-protocol-opted-into-per-turn — questions outside plan mode
167
+ // Outside plan mode Codex answers its question tool "unavailable"
168
+ // without this. With it a question reaches the engine, which declines
169
+ // it for a turn whose client answers none, as in plan mode.
170
+ default_mode_request_user_input: true,
171
+ };
172
+ if (options.subagentMode === "turn") features.multi_agent = true;
173
+ if (tools.local === false) {
174
+ features.shell_tool = false;
175
+ features.unified_exec = false;
176
+ }
177
+ if (tools.imageGeneration === false) features.image_generation = false;
178
+ // @ref LLP 0001#d17--a-turn-inherits-what-the-operator-chooses-all-the-projects-or-none — the account's connectors
179
+ // Codex's apps (`codex_apps`) are the ChatGPT account's connectors, which
180
+ // follow the login into any home: below `all` they stay out, as Claude's
181
+ // claude.ai connectors do. Plugins are off there too, on the command line
182
+ // (`clientOptionsFor`).
183
+ if (policy.inheritConfig !== undefined && policy.inheritConfig !== "all") features.apps = false;
184
+ const config: Record<string, unknown> = { features };
128
185
  // Extra directories become writable roots of the workspace-write sandbox;
129
186
  // read-only reads everywhere already, full access writes too.
130
187
  if (options.additionalDirectories?.length)
131
- config["sandbox_workspace_write.writable_roots"] = [...new Set(options.additionalDirectories)];
132
- if (options.subagentMode === "turn") config["features.multi_agent"] = true;
188
+ config.sandbox_workspace_write = {
189
+ writable_roots: [...new Set(options.additionalDirectories)],
190
+ };
191
+ if (tools.webSearch === false) config.web_search = "disabled";
133
192
  // @ref LLP 0001#d17--a-turn-inherits-what-the-operator-chooses-all-the-projects-or-none — the repository's AGENTS.md
134
193
  // Codex reads every AGENTS.md from the repository's root to the working
135
194
  // folder by itself; at `none`, none of them. The home's own still loads.
136
- if (inheritConfig === "none") config.project_doc_max_bytes = 0;
195
+ if (policy.inheritConfig === "none") config.project_doc_max_bytes = 0;
137
196
  // @ref LLP 0001#d21--a-codex-turn-that-names-no-home-carries-its-mcp-servers-in-its-thread — the thread's own config
138
197
  // Beside the servers of Codex's own configuration, by name; no file is
139
198
  // written, and a resume without them has none of them.
@@ -19,7 +19,7 @@ const WORKSPACE_PROFILE = ":workspace";
19
19
  const IMPLEMENT_THE_PLAN = "Implement the plan.";
20
20
 
21
21
  /** Codex's turn that carries out the user's answer to its plan. */
22
- export interface NextTurn {
22
+ interface NextTurn {
23
23
  params: Record<string, unknown>;
24
24
  settings: ThreadSettings;
25
25
  /** The thread's sandbox once it runs. */
@@ -48,6 +48,13 @@ export interface CodexItem {
48
48
  error?: { message?: string } | string;
49
49
  /** A `webSearch`. */
50
50
  query?: string;
51
+ /**
52
+ * An `imageGeneration`: the prompt Codex wrote, once done; where Codex
53
+ * saved the picture; why it failed. (Its base64 `result` is not read.)
54
+ */
55
+ revisedPrompt?: string | null;
56
+ savedPath?: string;
57
+ failure?: { type?: string } | null;
51
58
  /** A `collabAgentToolCall`: a sub-agent spawned, messaged or waited on. */
52
59
  receiverThreadIds?: string[];
53
60
  prompt?: string | null;
@@ -62,7 +69,7 @@ export interface CodexItem {
62
69
  }
63
70
 
64
71
  /** A turn as `turn/started` and `turn/completed` name it (`Turn`). */
65
- export interface CodexTurnInfo {
72
+ interface CodexTurnInfo {
66
73
  id?: string;
67
74
  /** `completed`, `interrupted` (stopped: a cancel, or an approval answered cancel), `failed`. */
68
75
  status?: string;
@@ -106,9 +113,12 @@ export function codexTurn(n: CodexNotification): CodexTurnInfo | undefined {
106
113
  };
107
114
  }
108
115
 
109
- /** Whether a notification ends Codex's turn: its completion, or an error Codex will not retry. */
110
- export function endsNativeTurn(n: CodexNotification): boolean {
111
- return n.method === "turn/completed" || (n.method === "error" && n.params.willRetry !== true);
116
+ /**
117
+ * An `error` Codex will not retry: its turn failed, and it ends that turn with
118
+ * `turn/completed` next. Seen on 0.160, some time later.
119
+ */
120
+ export function failing(n: CodexNotification): boolean {
121
+ return n.method === "error" && n.params.willRetry !== true;
112
122
  }
113
123
 
114
124
  /** An `error` Codex retries what failed after: the turn goes on, and more is to come. */
@@ -13,7 +13,7 @@ import {
13
13
  unanswered,
14
14
  } from "../elicitation.ts";
15
15
  import { type CodexAuth, refreshTokens } from "./credentials.ts";
16
- import { CODEX_TOOL_KINDS } from "./items.ts";
16
+ import { CODEX_TOOL_KINDS, codexChangeView, codexCommandLine } from "./items.ts";
17
17
  import type { CodexSession } from "./session.ts";
18
18
  import type { CodexTurnSubagents } from "./subagents.ts";
19
19
  import type { TurnItems } from "./turn-items.ts";
@@ -161,6 +161,10 @@ function mcpConsentToolCall(params: Record<string, unknown>, items: TurnItems):
161
161
  toolCallId: call?.id ?? generateUUIDv7(),
162
162
  toolName: `${server}.${call?.tool ?? named ?? "tool"}`,
163
163
  kind: CODEX_TOOL_KINDS.mcp,
164
+ // Both as Codex names them: the consent's server, and the tool of the
165
+ // call it was linked to. Its message's words are no name.
166
+ ...(typeof params.serverName === "string" &&
167
+ call?.tool !== undefined && { mcp: { server: params.serverName, tool: call.tool } }),
164
168
  ...(message && { title: message }),
165
169
  rawInput: isRecord(input) ? input : { arguments: input },
166
170
  };
@@ -278,27 +282,31 @@ function approvalToolCall(
278
282
  items: TurnItems,
279
283
  ): PermissionToolCall {
280
284
  const id = typeof params.itemId === "string" ? params.itemId : "approval";
285
+ // @ref LLP 0002#a-tool-call-says-what-it-acts-on — Codex's reason is why it asks, not the call's title
281
286
  const reason = typeof params.reason === "string" ? params.reason : undefined;
282
287
  if (method === "item/commandExecution/requestApproval") {
283
288
  const command = typeof params.command === "string" ? params.command : "";
289
+ const line = codexCommandLine(command);
284
290
  return {
285
291
  toolCallId: id,
286
292
  toolName: "shell",
287
- kind: "execute",
288
- ...(reason && { title: reason }),
293
+ kind: CODEX_TOOL_KINDS.shell,
294
+ ...(line && { command: line }),
295
+ ...(reason && { reason }),
289
296
  rawInput: { command, ...(params.cwd ? { cwd: params.cwd } : {}) },
290
297
  };
291
298
  }
292
299
  // A file-change request names its item, not the edits: those arrived on the
293
300
  // item, which Codex starts before it asks. The approval shows them.
294
301
  const changes = items.changesOf(id);
302
+ const { locations } = codexChangeView(changes);
295
303
  const grantRoot = typeof params.grantRoot === "string" ? params.grantRoot : undefined;
296
304
  return {
297
305
  toolCallId: id,
298
306
  toolName: "apply_patch",
299
- kind: "edit",
300
- ...(reason && { title: reason }),
301
- ...(changes.length && { locations: changes.map((change) => ({ path: change.path })) }),
307
+ kind: CODEX_TOOL_KINDS.apply_patch,
308
+ ...(reason && { reason }),
309
+ ...(locations.length && { locations }),
302
310
  rawInput: { changes, ...(grantRoot && { grantRoot }) },
303
311
  };
304
312
  }
@@ -1,15 +1,17 @@
1
1
  import { onAbort } from "../../utils/deadline.ts";
2
2
  import { SessionResumeError } from "../../utils/errors.ts";
3
3
  import type { AgentExecuteOptions } from "../base.ts";
4
- import type { InheritConfig } from "../inherit-config.ts";
5
- import { SessionPool } from "../session-pool.ts";
4
+ import { ProcessPool } from "../process-pool.ts";
6
5
  import type { CodexConnection } from "./client.ts";
7
6
  import { type CodexAuth, authFingerprint, logIn } from "./credentials.ts";
8
- import { CodexHomes } from "./homes.ts";
9
- import { CodexMcpState, type CodexTurnMcp, codexTurnMcp, threadMcpFingerprint } from "./mcp.ts";
7
+ import { CodexHomes, holdHome, homeIdentity } from "./homes.ts";
8
+ import { codexLoginStore } from "./login.ts";
9
+ import { CodexMcpState, type CodexTurnMcp, codexTurnMcp, threadServersFingerprint } from "./mcp.ts";
10
10
  import type { CodexModelThinking } from "./models.ts";
11
11
  import {
12
12
  type CodexProcessIdentity,
13
+ type CodexThreadParams,
14
+ type CodexThreadPolicy,
13
15
  codexSessionCompatible,
14
16
  processIdentity,
15
17
  threadParams,
@@ -26,28 +28,41 @@ export interface CodexSession extends CodexProcessIdentity {
26
28
  authFingerprint?: string;
27
29
  mcp: CodexMcpState;
28
30
  /**
29
- * The MCP servers its thread was opened with, in the thread's own config
30
- * (`threadMcpFingerprint`): a turn with another set runs in another process.
31
+ * The `mcp_servers` table its thread was opened with
32
+ * (`threadServersFingerprint`): a turn that needs another runs in another
33
+ * process.
31
34
  */
32
- threadMcp: string;
35
+ threadServers: string;
36
+ /**
37
+ * The environments each `turn/start` names, when the operator turned
38
+ * Codex's local tools off (`CodexTurn` sends them; it knows no policy).
39
+ */
40
+ environments?: [];
33
41
  /** The logical session's thread settings, which outlive this process. */
34
42
  thread: CodexThread;
35
43
  /** The turn that has the session: Codex's requests answer for it alone. */
36
44
  turn?: CodexTurnScope;
37
45
  /**
38
- * Host suspension left its MCP connections stale: the turn running on it
39
- * finishes, and the next turn replaces the process.
46
+ * Whether a host resume came since it started, which left its MCP
47
+ * connections dead: no `turn/start` is sent on it, and the next turn
48
+ * replaces it (the pool's `startedBeforeResume`).
40
49
  */
41
- stale?: boolean;
42
- /** The login it started with (`CodexSessions.loginChanged`): an older one's next turn replaces it. */
43
- login: number;
50
+ startedBeforeResume(): boolean;
51
+ /** The folder its `CODEX_HOME` was when it started (`homeIdentity`): another one's turn replaces it. */
52
+ home?: string;
44
53
  /** What each model accepts, from this process's `model/list`; read once. */
45
54
  modelThinking?: Promise<ReadonlyMap<string, CodexModelThinking> | undefined>;
46
- idleTimer?: ReturnType<typeof setTimeout>;
55
+ }
56
+
57
+ /** What a Codex turn brings to the pool: its options, its scope (the account it runs as) and its MCP servers. */
58
+ interface CodexPoolTurn {
59
+ options: AgentExecuteOptions;
60
+ scope: CodexTurnScope;
61
+ mcp: CodexTurnMcp;
47
62
  }
48
63
 
49
64
  /** Starts a connection to a new `codex app-server` for a turn's folder, `env` and login. */
50
- export type CodexConnector = (
65
+ type CodexConnector = (
51
66
  options: AgentExecuteOptions,
52
67
  auth: CodexAuth | undefined,
53
68
  signal: AbortSignal,
@@ -55,17 +70,14 @@ export type CodexConnector = (
55
70
 
56
71
  /**
57
72
  * Start the session's thread, or resume the one it names: never a new one in
58
- * its place, so a resume that fails fails the turn. `mcpServers`: the
59
- * servers the thread carries in its own config.
73
+ * its place, so a resume that fails fails the turn.
60
74
  */
61
75
  async function openThread(
62
76
  client: CodexConnection,
63
77
  options: AgentExecuteOptions,
64
- inheritConfig: InheritConfig,
65
- mcpServers: Record<string, unknown> | undefined,
78
+ params: CodexThreadParams,
66
79
  ): Promise<CodexThreadOpened & { threadId: string }> {
67
80
  const resume = options.resumeSessionId;
68
- const params = threadParams(options, inheritConfig, mcpServers);
69
81
  const opened = (await client
70
82
  .request(
71
83
  resume ? "thread/resume" : "thread/start",
@@ -89,28 +101,44 @@ async function openThread(
89
101
  }
90
102
 
91
103
  /**
92
- * The Codex harness's sessions: each logical session's process, while it
93
- * lives, and what outlives it (its thread settings, its home), plus the
94
- * managed sessions whose native cleanup was not confirmed.
104
+ * The Codex harness's sessions: what is Codex's own of each logical
105
+ * session's process (its thread, its home, its account, its MCP table), on
106
+ * the process pool, which decides when one is reused, replaced or ended;
107
+ * what outlives a process (its thread settings, its home); and the managed
108
+ * sessions whose native cleanup was not confirmed.
95
109
  */
96
110
  export class CodexSessions {
97
- private readonly pool = new SessionPool<CodexSession>();
111
+ private readonly pool: ProcessPool<CodexSession, CodexPoolTurn>;
98
112
  private readonly threads = new Map<string, CodexThread>();
99
113
  private readonly homes: CodexHomes;
100
114
  /** Managed sessions whose process was not confirmed stopped: no successor runs until it is. */
101
115
  private readonly unsettled = new Map<string, CodexConnection>();
102
- /** Counts the login changes: a process keeps the login it read at its start. */
103
- private login = 0;
104
- /** Each session's process that was ended and has not exited yet: when it has. */
105
- private readonly leaving = new Map<string, Promise<void>>();
106
116
 
107
117
  constructor(
108
118
  private readonly connect: CodexConnector,
109
- private readonly idleMs: number,
110
- /** The operator's: what a thread loads by itself, and whether every turn needs a home. */
111
- private readonly inheritConfig: InheritConfig = "all",
119
+ idleMs: number,
120
+ /** The operator's: what a thread loads by itself, whether every turn needs a home, and Codex's own tools. */
121
+ private readonly policy: CodexThreadPolicy = {},
112
122
  ) {
113
- this.homes = new CodexHomes(inheritConfig);
123
+ this.homes = new CodexHomes(policy.inheritConfig);
124
+ this.pool = new ProcessPool<CodexSession, CodexPoolTurn>(
125
+ {
126
+ // An app-server reads its login from its home at its start.
127
+ store: ({ options }) => codexLoginStore(options.env),
128
+ resumeOf: ({ options }) => options.resumeSessionId,
129
+ conversationsOf: (session) => [session.threadId],
130
+ fits: (session, { options, scope }) =>
131
+ codexSessionCompatible(session, options) &&
132
+ session.authFingerprint === authFingerprint(scope.auth) &&
133
+ // A home removed or made again under the process is not the one it
134
+ // runs in: a new process opens the thread there, or fails to, as a
135
+ // cold start would.
136
+ session.home === homeIdentity(options.env?.CODEX_HOME),
137
+ applyLiveChange: (session, turn, signal) => this.applyLiveChange(session, turn, signal),
138
+ spawn: (turn, own, signal) => this.spawn(turn, own, signal),
139
+ },
140
+ { idleMs },
141
+ );
114
142
  }
115
143
 
116
144
  get(sessionId: string): CodexSession | undefined {
@@ -123,41 +151,31 @@ export class CodexSessions {
123
151
  }
124
152
 
125
153
  /**
126
- * The session's process for the turn, which takes it: the running one when
127
- * the turn can run there as the same account, else a new one, on the same
128
- * thread. Codex caches an authenticated model WebSocket across turns, so
154
+ * The session's process for the turn, which takes it (the pool's rule):
155
+ * the running one when the turn can run there as the same account, else a
156
+ * new one, on the same thread. Codex caches an authenticated model WebSocket across turns, so
129
157
  * login alone cannot switch its identity: an account or key change
130
158
  * restarts the process and resumes the thread. Same-account token renewal
131
- * keeps it warm. So does an unchanged set of the servers the thread
132
- * carries; a thread's config is fixed once it is open, so another set
133
- * resumes the thread in a new process.
159
+ * keeps it warm. So does an unchanged `mcp_servers` table, the servers
160
+ * the thread carries. A thread's config is fixed once it is open, so
161
+ * another table resumes the thread in a new process, which checks them
162
+ * against Codex's configuration again before the thread opens: the user
163
+ * may change it while the old process exits. A home removed or made again
164
+ * under the process starts a new one too.
134
165
  */
135
- async acquire(
166
+ acquire(
136
167
  options: AgentExecuteOptions,
137
168
  turn: CodexTurnScope,
138
169
  mcp: CodexTurnMcp,
139
170
  ): Promise<CodexSession> {
140
- turn.signal.throwIfAborted();
141
- const existing = this.pool.get(options.sessionId);
142
- if (existing && !existing.client.closed) {
143
- if (
144
- codexSessionCompatible(existing, options) &&
145
- existing.authFingerprint === authFingerprint(turn.auth) &&
146
- !existing.stale &&
147
- existing.login === this.login &&
148
- existing.threadMcp === threadMcpFingerprint(mcp)
149
- )
150
- return this.reuse(existing, turn);
151
- this.pool.close(options.sessionId);
152
- }
153
- return this.spawn(options, turn, mcp);
171
+ return this.pool.acquire(options.sessionId, { options, scope: turn, mcp }, turn.signal);
154
172
  }
155
173
 
156
174
  /** The turn is done with the session: an open process waits for the next one, at most `idleMs`. */
157
175
  leave(sessionId: string, session: CodexSession, turn: CodexTurnScope): void {
158
176
  if (session.turn !== turn) return;
159
177
  session.turn = undefined;
160
- if (!session.client.closed) this.pool.armIdle(sessionId, session, this.idleMs);
178
+ this.pool.leave(sessionId, session);
161
179
  }
162
180
 
163
181
  /** Keep a managed session whose process could not be confirmed stopped from another turn. */
@@ -186,90 +204,105 @@ export class CodexSessions {
186
204
 
187
205
  /**
188
206
  * Unchanged MCP config reloads retain dead remote connections after VM
189
- * resume: every process goes, but a turn that is running finishes first,
190
- * and its session is replaced before the next (the strict thread/resume
191
- * path).
207
+ * resume: each session's next turn replaces its process and resumes its
208
+ * thread (the strict thread/resume path). Nothing is ended now; a turn
209
+ * still setting up sends no `turn/start` on its process (`CodexTurn`).
192
210
  */
193
211
  invalidate(): void {
194
- const active = new Set(
195
- [...this.pool.values()].filter((session) => session.turn).map((session) => session.client),
196
- );
197
- for (const session of this.pool.values()) session.stale = true;
198
- for (const client of this.pool.clients()) if (!active.has(client)) client.close();
212
+ this.pool.invalidate();
199
213
  }
200
214
 
201
- // @ref LLP 0011#a-login-change-starts-the-next-turn-on-a-new-cli — a generation, never a close under a turn
202
215
  /**
203
- * The login changed: an app-server keeps the one it read at its start, so
204
- * each session's next turn starts a new one, resuming its thread. A turn
216
+ * The login changed in `store` (every store when none is named): an
217
+ * app-server keeps the one it read at its start, so each of that store's
218
+ * sessions starts its next turn on a new one, resuming its thread. A turn
205
219
  * already setting up or running finishes on its own.
206
220
  */
207
- loginChanged(): void {
208
- this.login++;
221
+ loginChanged(store?: string): void {
222
+ this.pool.loginChanged(store);
209
223
  }
210
224
 
211
- /** Forget the session: its process, its thread settings and its home. */
212
- release(sessionId: string): void {
213
- this.pool.close(sessionId);
225
+ /**
226
+ * Forget the session: its process, its thread settings and its home.
227
+ * Settles once every app-server of the session has exited, one an idle
228
+ * eviction or a replacement ended earlier included (the pool's
229
+ * `release`): a product removes the session's home on this answer, and an
230
+ * exiting app-server still writes there.
231
+ */
232
+ release(sessionId: string): Promise<void> {
233
+ const released = this.pool.release(sessionId);
214
234
  this.threads.delete(sessionId);
215
235
  this.homes.release(sessionId);
236
+ return released;
216
237
  }
217
238
 
218
- terminateAll(): void {
219
- this.pool.closeAll();
239
+ /** End every app-server, and settle once each has exited. */
240
+ terminateAll(): Promise<void> {
241
+ const closed = this.pool.closeAll();
220
242
  this.threads.clear();
221
243
  this.homes.clear();
244
+ return closed;
222
245
  }
223
246
 
224
- private async reuse(session: CodexSession, turn: CodexTurnScope): Promise<CodexSession> {
225
- this.pool.clearIdle(session);
247
+ /**
248
+ * What changes on the warm process without a restart, for the turn: its
249
+ * account logged in again (a renewed token), and the `mcp_servers` table
250
+ * its thread needs checked against Codex's own configuration. Another
251
+ * table cannot be changed on an open thread, so the thread resumes in a
252
+ * new process. A cancel meanwhile ends the process, as Codex's answer is
253
+ * not waited for; a refused login ends it too, and a refused table read
254
+ * leaves it for the session's next turn.
255
+ */
256
+ private async applyLiveChange(
257
+ session: CodexSession,
258
+ { scope: turn, mcp }: CodexPoolTurn,
259
+ signal: AbortSignal,
260
+ ): Promise<boolean> {
226
261
  session.turn = turn;
227
- const offAbort = onAbort(turn.signal, () => session.client.close());
262
+ const offAbort = onAbort(signal, () => session.client.close());
228
263
  try {
229
- await authenticate(session.client, turn);
230
- return session;
231
- } catch (error) {
264
+ try {
265
+ await authenticate(session.client, turn);
266
+ } catch (error) {
267
+ session.turn = undefined;
268
+ session.client.close();
269
+ throw error;
270
+ }
271
+ let servers: Record<string, unknown> | undefined;
272
+ try {
273
+ servers = await session.mcp.threadServers(mcp);
274
+ turn.signal.throwIfAborted();
275
+ } catch (error) {
276
+ session.turn = undefined;
277
+ throw error;
278
+ }
279
+ if (session.threadServers === threadServersFingerprint(servers)) return true;
232
280
  session.turn = undefined;
233
- session.client.close();
234
- throw error;
281
+ return false;
235
282
  } finally {
236
283
  offAbort();
237
284
  }
238
285
  }
239
286
 
287
+ /**
288
+ * Start an app-server for the turn and open the session's thread on it.
289
+ * The pool starts it once every app-server of the session before it has
290
+ * exited.
291
+ */
240
292
  private async spawn(
241
- options: AgentExecuteOptions,
242
- turn: CodexTurnScope,
243
- mcp: CodexTurnMcp,
293
+ { options, scope: turn, mcp }: CodexPoolTurn,
294
+ own: (client: CodexConnection) => void,
295
+ signal: AbortSignal,
244
296
  ): Promise<CodexSession> {
245
- const { signal } = turn;
246
- const { sessionId } = options;
247
297
  // @ref LLP 0001#d21--a-codex-turn-that-names-no-home-carries-its-mcp-servers-in-its-thread — one writer to a thread
248
298
  // Codex lets one process write a thread, and refuses a resume while the
249
- // one before still exits: replaced just now, or ended a while ago. The
250
- // wait is the stop's grace at most, and a cancelled turn does not sit it out.
251
- const leaving = this.leaving.get(sessionId);
252
- if (leaving) {
253
- let offCancel = () => {};
254
- const cancelled = new Promise<void>((resolve) => {
255
- offCancel = onAbort(signal, resolve);
256
- });
257
- await Promise.race([leaving, cancelled]);
258
- offCancel();
259
- signal.throwIfAborted();
260
- }
299
+ // one before still exits: the pool waited for it.
261
300
  const client = await this.connect(options, turn.auth, signal);
262
- // Self-heal: if the subprocess ever exits, drop it so the next turn respawns.
263
- client.onClose(() => {
264
- this.pool.dropIfCurrent(sessionId, client);
265
- const gone = client.gone?.();
266
- if (!gone) return;
267
- this.leaving.set(sessionId, gone);
268
- void gone.then(() => {
269
- if (this.leaving.get(sessionId) === gone) this.leaving.delete(sessionId);
270
- });
271
- });
272
- const untrack = this.pool.trackPending(options.sessionId, client);
301
+ own(client);
302
+ // Held while the process lives, so the folder's identity stays its own.
303
+ const release = holdHome(options.env?.CODEX_HOME);
304
+ client.onClose(release);
305
+ const home = homeIdentity(options.env?.CODEX_HOME);
273
306
  const offAbort = onAbort(signal, () => client.close());
274
307
  let thread = this.threads.get(options.sessionId);
275
308
  if (!thread) {
@@ -281,9 +314,12 @@ export class CodexSessions {
281
314
  client,
282
315
  threadId: "",
283
316
  authFingerprint: authFingerprint(turn.auth),
284
- login: this.login,
317
+ startedBeforeResume: () => this.pool.startedBeforeResume(session),
318
+ ...(home && { home }),
285
319
  mcp: new CodexMcpState(client, options.cwd),
286
- threadMcp: threadMcpFingerprint(mcp),
320
+ threadServers: "",
321
+ // @ref LLP 0017#each-turn-names-its-environments — a resumed thread is back on its local one
322
+ ...(this.policy.builtInTools?.local === false && { environments: [] }),
287
323
  thread,
288
324
  turn,
289
325
  };
@@ -294,22 +330,24 @@ export class CodexSessions {
294
330
  await client.initialize();
295
331
  await authenticate(client, turn);
296
332
  // Before the thread opens: a home is cleared of the turn before's
297
- // servers, and a set the thread carries is checked against Codex's own.
333
+ // servers; a set the thread carries is checked against Codex's own.
298
334
  await session.mcp.configure(mcp);
299
- const opened = await openThread(client, options, this.inheritConfig, mcp.thread);
335
+ const servers = await session.mcp.threadServers(mcp);
336
+ session.threadServers = threadServersFingerprint(servers);
337
+ const opened = await openThread(client, options, threadParams(options, this.policy, servers));
300
338
  signal.throwIfAborted();
301
339
  session.threadId = opened.threadId;
302
340
  thread.adopt(opened, options);
303
- this.pool.set(options.sessionId, session);
304
341
  return session;
305
342
  } catch (error) {
306
343
  session.turn = undefined;
307
344
  client.setRequestHandler(undefined);
308
345
  client.close();
346
+ // A client closed before its hold was taken does not call it.
347
+ release();
309
348
  throw error;
310
349
  } finally {
311
350
  offAbort();
312
- untrack();
313
351
  }
314
352
  }
315
353
  }