@zvada/agent-server 0.3.18 → 0.3.20

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 (572) hide show
  1. package/AGENTS.md +11 -26
  2. package/CHANGELOG.md +1041 -1
  3. package/README.md +40 -40
  4. package/dist/types/accounts/chatgpt.d.ts +34 -0
  5. package/dist/types/accounts/chatgpt.d.ts.map +1 -0
  6. package/dist/types/accounts/check.d.ts +24 -0
  7. package/dist/types/accounts/check.d.ts.map +1 -0
  8. package/dist/types/accounts/claude.d.ts +21 -0
  9. package/dist/types/accounts/claude.d.ts.map +1 -0
  10. package/dist/types/accounts/endpoint.d.ts +50 -0
  11. package/dist/types/accounts/endpoint.d.ts.map +1 -0
  12. package/dist/types/accounts/http.d.ts +38 -0
  13. package/dist/types/accounts/http.d.ts.map +1 -0
  14. package/dist/types/accounts/index.d.ts +6 -0
  15. package/dist/types/accounts/index.d.ts.map +1 -0
  16. package/dist/types/client/client.d.ts +272 -0
  17. package/dist/types/client/client.d.ts.map +1 -0
  18. package/dist/types/client/connection.d.ts +160 -0
  19. package/dist/types/client/connection.d.ts.map +1 -0
  20. package/dist/types/client/history.d.ts +8 -0
  21. package/dist/types/client/history.d.ts.map +1 -0
  22. package/dist/types/client/index.d.ts +5 -0
  23. package/dist/types/client/index.d.ts.map +1 -0
  24. package/dist/types/client/listeners.d.ts +8 -0
  25. package/dist/types/client/listeners.d.ts.map +1 -0
  26. package/dist/types/client/logins.d.ts +35 -0
  27. package/dist/types/client/logins.d.ts.map +1 -0
  28. package/dist/types/client/rpc.d.ts +46 -0
  29. package/dist/types/client/rpc.d.ts.map +1 -0
  30. package/dist/types/client/streams.d.ts +150 -0
  31. package/dist/types/client/streams.d.ts.map +1 -0
  32. package/dist/types/client/transports.d.ts +35 -0
  33. package/dist/types/client/transports.d.ts.map +1 -0
  34. package/dist/types/client/turns.d.ts +81 -0
  35. package/dist/types/client/turns.d.ts.map +1 -0
  36. package/dist/types/core/agents/acp/acp-agent.d.ts +68 -0
  37. package/dist/types/core/agents/acp/acp-agent.d.ts.map +1 -0
  38. package/dist/types/core/agents/acp/adapter.d.ts +39 -0
  39. package/dist/types/core/agents/acp/adapter.d.ts.map +1 -0
  40. package/dist/types/core/agents/acp/client.d.ts +90 -0
  41. package/dist/types/core/agents/acp/client.d.ts.map +1 -0
  42. package/dist/types/core/agents/acp/known-agents.d.ts +35 -0
  43. package/dist/types/core/agents/acp/known-agents.d.ts.map +1 -0
  44. package/dist/types/core/agents/acp/mappings.d.ts +46 -0
  45. package/dist/types/core/agents/acp/mappings.d.ts.map +1 -0
  46. package/dist/types/core/agents/acp/requests.d.ts +26 -0
  47. package/dist/types/core/agents/acp/requests.d.ts.map +1 -0
  48. package/dist/types/core/agents/attachments.d.ts +11 -0
  49. package/dist/types/core/agents/attachments.d.ts.map +1 -0
  50. package/dist/types/core/agents/base.d.ts +227 -0
  51. package/dist/types/core/agents/base.d.ts.map +1 -0
  52. package/dist/types/core/agents/claude-code/adapter.d.ts +58 -0
  53. package/dist/types/core/agents/claude-code/adapter.d.ts.map +1 -0
  54. package/dist/types/core/agents/claude-code/approvals.d.ts +26 -0
  55. package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -0
  56. package/dist/types/core/agents/claude-code/claude-agent.d.ts +70 -0
  57. package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -0
  58. package/dist/types/core/agents/claude-code/credentials.d.ts +54 -0
  59. package/dist/types/core/agents/claude-code/credentials.d.ts.map +1 -0
  60. package/dist/types/core/agents/claude-code/events.d.ts +42 -0
  61. package/dist/types/core/agents/claude-code/events.d.ts.map +1 -0
  62. package/dist/types/core/agents/claude-code/login.d.ts +61 -0
  63. package/dist/types/core/agents/claude-code/login.d.ts.map +1 -0
  64. package/dist/types/core/agents/claude-code/mcp.d.ts +52 -0
  65. package/dist/types/core/agents/claude-code/mcp.d.ts.map +1 -0
  66. package/dist/types/core/agents/claude-code/models.d.ts +30 -0
  67. package/dist/types/core/agents/claude-code/models.d.ts.map +1 -0
  68. package/dist/types/core/agents/claude-code/options.d.ts +183 -0
  69. package/dist/types/core/agents/claude-code/options.d.ts.map +1 -0
  70. package/dist/types/core/agents/claude-code/outcome.d.ts +50 -0
  71. package/dist/types/core/agents/claude-code/outcome.d.ts.map +1 -0
  72. package/dist/types/core/agents/claude-code/session-pool.d.ts +36 -0
  73. package/dist/types/core/agents/claude-code/session-pool.d.ts.map +1 -0
  74. package/dist/types/core/agents/claude-code/session.d.ts +148 -0
  75. package/dist/types/core/agents/claude-code/session.d.ts.map +1 -0
  76. package/dist/types/core/agents/claude-code/spawn.d.ts +29 -0
  77. package/dist/types/core/agents/claude-code/spawn.d.ts.map +1 -0
  78. package/dist/types/core/agents/claude-code/spend.d.ts +58 -0
  79. package/dist/types/core/agents/claude-code/spend.d.ts.map +1 -0
  80. package/dist/types/core/agents/claude-code/tools.d.ts +4 -0
  81. package/dist/types/core/agents/claude-code/tools.d.ts.map +1 -0
  82. package/dist/types/core/agents/claude-code/turn-usage.d.ts +52 -0
  83. package/dist/types/core/agents/claude-code/turn-usage.d.ts.map +1 -0
  84. package/dist/types/core/agents/claude-code/turn.d.ts +121 -0
  85. package/dist/types/core/agents/claude-code/turn.d.ts.map +1 -0
  86. package/dist/types/core/agents/codex-app-server/adapter.d.ts +59 -0
  87. package/dist/types/core/agents/codex-app-server/adapter.d.ts.map +1 -0
  88. package/dist/types/core/agents/codex-app-server/client.d.ts +116 -0
  89. package/dist/types/core/agents/codex-app-server/client.d.ts.map +1 -0
  90. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts +113 -0
  91. package/dist/types/core/agents/codex-app-server/codex-agent.d.ts.map +1 -0
  92. package/dist/types/core/agents/codex-app-server/commands.d.ts +66 -0
  93. package/dist/types/core/agents/codex-app-server/commands.d.ts.map +1 -0
  94. package/dist/types/core/agents/codex-app-server/credentials.d.ts +49 -0
  95. package/dist/types/core/agents/codex-app-server/credentials.d.ts.map +1 -0
  96. package/dist/types/core/agents/codex-app-server/effort.d.ts +12 -0
  97. package/dist/types/core/agents/codex-app-server/effort.d.ts.map +1 -0
  98. package/dist/types/core/agents/codex-app-server/events.d.ts +45 -0
  99. package/dist/types/core/agents/codex-app-server/events.d.ts.map +1 -0
  100. package/dist/types/core/agents/codex-app-server/homes.d.ts +20 -0
  101. package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -0
  102. package/dist/types/core/agents/codex-app-server/input.d.ts +26 -0
  103. package/dist/types/core/agents/codex-app-server/input.d.ts.map +1 -0
  104. package/dist/types/core/agents/codex-app-server/items.d.ts +31 -0
  105. package/dist/types/core/agents/codex-app-server/items.d.ts.map +1 -0
  106. package/dist/types/core/agents/codex-app-server/login.d.ts +38 -0
  107. package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -0
  108. package/dist/types/core/agents/codex-app-server/mcp.d.ts +62 -0
  109. package/dist/types/core/agents/codex-app-server/mcp.d.ts.map +1 -0
  110. package/dist/types/core/agents/codex-app-server/models.d.ts +67 -0
  111. package/dist/types/core/agents/codex-app-server/models.d.ts.map +1 -0
  112. package/dist/types/core/agents/codex-app-server/options.d.ts +40 -0
  113. package/dist/types/core/agents/codex-app-server/options.d.ts.map +1 -0
  114. package/dist/types/core/agents/codex-app-server/plan-approval.d.ts +19 -0
  115. package/dist/types/core/agents/codex-app-server/plan-approval.d.ts.map +1 -0
  116. package/dist/types/core/agents/codex-app-server/protocol.d.ts +99 -0
  117. package/dist/types/core/agents/codex-app-server/protocol.d.ts.map +1 -0
  118. package/dist/types/core/agents/codex-app-server/requests.d.ts +42 -0
  119. package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -0
  120. package/dist/types/core/agents/codex-app-server/session.d.ts +96 -0
  121. package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -0
  122. package/dist/types/core/agents/codex-app-server/subagents.d.ts +53 -0
  123. package/dist/types/core/agents/codex-app-server/subagents.d.ts.map +1 -0
  124. package/dist/types/core/agents/codex-app-server/thread-settings.d.ts +87 -0
  125. package/dist/types/core/agents/codex-app-server/thread-settings.d.ts.map +1 -0
  126. package/dist/types/core/agents/codex-app-server/turn-items.d.ts +33 -0
  127. package/dist/types/core/agents/codex-app-server/turn-items.d.ts.map +1 -0
  128. package/dist/types/core/agents/codex-app-server/turn.d.ts +163 -0
  129. package/dist/types/core/agents/codex-app-server/turn.d.ts.map +1 -0
  130. package/dist/types/core/agents/config-fingerprint.d.ts +5 -0
  131. package/dist/types/core/agents/config-fingerprint.d.ts.map +1 -0
  132. package/dist/types/core/agents/credentials.d.ts +28 -0
  133. package/dist/types/core/agents/credentials.d.ts.map +1 -0
  134. package/dist/types/core/agents/elicitation.d.ts +37 -0
  135. package/dist/types/core/agents/elicitation.d.ts.map +1 -0
  136. package/dist/types/core/agents/inherit-config.d.ts +18 -0
  137. package/dist/types/core/agents/inherit-config.d.ts.map +1 -0
  138. package/dist/types/core/agents/login.d.ts +67 -0
  139. package/dist/types/core/agents/login.d.ts.map +1 -0
  140. package/dist/types/core/agents/mcp-config.d.ts +57 -0
  141. package/dist/types/core/agents/mcp-config.d.ts.map +1 -0
  142. package/dist/types/core/agents/plan.d.ts +16 -0
  143. package/dist/types/core/agents/plan.d.ts.map +1 -0
  144. package/dist/types/core/agents/registry.d.ts +30 -0
  145. package/dist/types/core/agents/registry.d.ts.map +1 -0
  146. package/dist/types/core/agents/session-agent.d.ts +48 -0
  147. package/dist/types/core/agents/session-agent.d.ts.map +1 -0
  148. package/dist/types/core/agents/session-pool.d.ts +41 -0
  149. package/dist/types/core/agents/session-pool.d.ts.map +1 -0
  150. package/dist/types/core/agents/stderr-tail.d.ts +28 -0
  151. package/dist/types/core/agents/stderr-tail.d.ts.map +1 -0
  152. package/dist/types/core/agents/thinking-levels.d.ts +4 -0
  153. package/dist/types/core/agents/thinking-levels.d.ts.map +1 -0
  154. package/dist/types/core/agents/types.d.ts +124 -0
  155. package/dist/types/core/agents/types.d.ts.map +1 -0
  156. package/dist/types/core/diagnostics.d.ts +36 -0
  157. package/dist/types/core/diagnostics.d.ts.map +1 -0
  158. package/dist/types/core/index.d.ts +29 -0
  159. package/dist/types/core/index.d.ts.map +1 -0
  160. package/dist/types/core/presets.d.ts +80 -0
  161. package/dist/types/core/presets.d.ts.map +1 -0
  162. package/dist/types/core/processes/identity.d.ts +31 -0
  163. package/dist/types/core/processes/identity.d.ts.map +1 -0
  164. package/dist/types/core/processes/lifeline.d.ts +47 -0
  165. package/dist/types/core/processes/lifeline.d.ts.map +1 -0
  166. package/dist/types/core/processes/native-processes.d.ts +130 -0
  167. package/dist/types/core/processes/native-processes.d.ts.map +1 -0
  168. package/dist/types/core/processes/process-table.d.ts +43 -0
  169. package/dist/types/core/processes/process-table.d.ts.map +1 -0
  170. package/dist/types/core/provision/cache-trust.d.ts +13 -0
  171. package/dist/types/core/provision/cache-trust.d.ts.map +1 -0
  172. package/dist/types/core/provision/cache-usage.d.ts +14 -0
  173. package/dist/types/core/provision/cache-usage.d.ts.map +1 -0
  174. package/dist/types/core/provision/checksums.d.ts +46 -0
  175. package/dist/types/core/provision/checksums.d.ts.map +1 -0
  176. package/dist/types/core/provision/extract.d.ts +7 -0
  177. package/dist/types/core/provision/extract.d.ts.map +1 -0
  178. package/dist/types/core/provision/index.d.ts +5 -0
  179. package/dist/types/core/provision/index.d.ts.map +1 -0
  180. package/dist/types/core/provision/install-build.d.ts +36 -0
  181. package/dist/types/core/provision/install-build.d.ts.map +1 -0
  182. package/dist/types/core/provision/npm.d.ts +40 -0
  183. package/dist/types/core/provision/npm.d.ts.map +1 -0
  184. package/dist/types/core/provision/pins.d.ts +24 -0
  185. package/dist/types/core/provision/pins.d.ts.map +1 -0
  186. package/dist/types/core/provision/platform.d.ts +28 -0
  187. package/dist/types/core/provision/platform.d.ts.map +1 -0
  188. package/dist/types/core/provision/provisioner.d.ts +189 -0
  189. package/dist/types/core/provision/provisioner.d.ts.map +1 -0
  190. package/dist/types/core/provision/releases.d.ts +8 -0
  191. package/dist/types/core/provision/releases.d.ts.map +1 -0
  192. package/dist/types/core/provision/tools.d.ts +81 -0
  193. package/dist/types/core/provision/tools.d.ts.map +1 -0
  194. package/dist/types/core/proxy/anthropic-proxy.d.ts +52 -0
  195. package/dist/types/core/proxy/anthropic-proxy.d.ts.map +1 -0
  196. package/dist/types/core/proxy/api-key-store.d.ts +37 -0
  197. package/dist/types/core/proxy/api-key-store.d.ts.map +1 -0
  198. package/dist/types/core/proxy/index.d.ts +3 -0
  199. package/dist/types/core/proxy/index.d.ts.map +1 -0
  200. package/dist/types/core/proxy/loopback.d.ts +14 -0
  201. package/dist/types/core/proxy/loopback.d.ts.map +1 -0
  202. package/dist/types/core/runtime/agent-runtime.d.ts +181 -0
  203. package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -0
  204. package/dist/types/core/runtime/event-processor.d.ts +87 -0
  205. package/dist/types/core/runtime/event-processor.d.ts.map +1 -0
  206. package/dist/types/core/runtime/event-sink.d.ts +44 -0
  207. package/dist/types/core/runtime/event-sink.d.ts.map +1 -0
  208. package/dist/types/core/runtime/harness-clis.d.ts +10 -0
  209. package/dist/types/core/runtime/harness-clis.d.ts.map +1 -0
  210. package/dist/types/core/runtime/harness-logins.d.ts +68 -0
  211. package/dist/types/core/runtime/harness-logins.d.ts.map +1 -0
  212. package/dist/types/core/runtime/login-reads.d.ts +28 -0
  213. package/dist/types/core/runtime/login-reads.d.ts.map +1 -0
  214. package/dist/types/core/runtime/model-lists.d.ts +37 -0
  215. package/dist/types/core/runtime/model-lists.d.ts.map +1 -0
  216. package/dist/types/core/runtime/permission-policy.d.ts +61 -0
  217. package/dist/types/core/runtime/permission-policy.d.ts.map +1 -0
  218. package/dist/types/core/runtime/request-broker.d.ts +49 -0
  219. package/dist/types/core/runtime/request-broker.d.ts.map +1 -0
  220. package/dist/types/core/runtime/steering.d.ts +38 -0
  221. package/dist/types/core/runtime/steering.d.ts.map +1 -0
  222. package/dist/types/core/runtime/turn-outcome.d.ts +21 -0
  223. package/dist/types/core/runtime/turn-outcome.d.ts.map +1 -0
  224. package/dist/types/core/runtime/turn.d.ts +121 -0
  225. package/dist/types/core/runtime/turn.d.ts.map +1 -0
  226. package/dist/types/core/utils/bounded.d.ts +6 -0
  227. package/dist/types/core/utils/bounded.d.ts.map +1 -0
  228. package/dist/types/core/utils/deadline.d.ts +14 -0
  229. package/dist/types/core/utils/deadline.d.ts.map +1 -0
  230. package/dist/types/core/utils/errors.d.ts +104 -0
  231. package/dist/types/core/utils/errors.d.ts.map +1 -0
  232. package/dist/types/core/utils/is-record.d.ts +3 -0
  233. package/dist/types/core/utils/is-record.d.ts.map +1 -0
  234. package/dist/types/core/utils/loopback.d.ts +6 -0
  235. package/dist/types/core/utils/loopback.d.ts.map +1 -0
  236. package/dist/types/index.d.ts +2 -0
  237. package/dist/types/index.d.ts.map +1 -0
  238. package/dist/types/protocol/account.d.ts +26 -0
  239. package/dist/types/protocol/account.d.ts.map +1 -0
  240. package/dist/types/protocol/async-queue.d.ts +21 -0
  241. package/dist/types/protocol/async-queue.d.ts.map +1 -0
  242. package/dist/types/protocol/auth.d.ts +228 -0
  243. package/dist/types/protocol/auth.d.ts.map +1 -0
  244. package/dist/types/protocol/canonical.d.ts +9 -0
  245. package/dist/types/protocol/canonical.d.ts.map +1 -0
  246. package/dist/types/protocol/clis.d.ts +74 -0
  247. package/dist/types/protocol/clis.d.ts.map +1 -0
  248. package/dist/types/protocol/config.d.ts +200 -0
  249. package/dist/types/protocol/config.d.ts.map +1 -0
  250. package/dist/types/protocol/conversation.d.ts +942 -0
  251. package/dist/types/protocol/conversation.d.ts.map +1 -0
  252. package/dist/types/protocol/credentials.d.ts +145 -0
  253. package/dist/types/protocol/credentials.d.ts.map +1 -0
  254. package/dist/types/protocol/errors.d.ts +36 -0
  255. package/dist/types/protocol/errors.d.ts.map +1 -0
  256. package/dist/types/protocol/execution.d.ts +28 -0
  257. package/dist/types/protocol/execution.d.ts.map +1 -0
  258. package/dist/types/protocol/factories.d.ts +94 -0
  259. package/dist/types/protocol/factories.d.ts.map +1 -0
  260. package/dist/types/protocol/guards.d.ts +55 -0
  261. package/dist/types/protocol/guards.d.ts.map +1 -0
  262. package/dist/types/protocol/harness.d.ts +70 -0
  263. package/dist/types/protocol/harness.d.ts.map +1 -0
  264. package/dist/types/protocol/history.d.ts +318 -0
  265. package/dist/types/protocol/history.d.ts.map +1 -0
  266. package/dist/types/protocol/ids.d.ts +9 -0
  267. package/dist/types/protocol/ids.d.ts.map +1 -0
  268. package/dist/types/protocol/index.d.ts +35 -0
  269. package/dist/types/protocol/index.d.ts.map +1 -0
  270. package/dist/types/protocol/lifecycle.d.ts +1190 -0
  271. package/dist/types/protocol/lifecycle.d.ts.map +1 -0
  272. package/dist/types/protocol/meta-keys.d.ts +17 -0
  273. package/dist/types/protocol/meta-keys.d.ts.map +1 -0
  274. package/dist/types/protocol/meta.d.ts +13 -0
  275. package/dist/types/protocol/meta.d.ts.map +1 -0
  276. package/dist/types/protocol/model-catalog.d.ts +11 -0
  277. package/dist/types/protocol/model-catalog.d.ts.map +1 -0
  278. package/dist/types/protocol/models.d.ts +107 -0
  279. package/dist/types/protocol/models.d.ts.map +1 -0
  280. package/dist/types/protocol/part-input.d.ts +101 -0
  281. package/dist/types/protocol/part-input.d.ts.map +1 -0
  282. package/dist/types/protocol/parts.d.ts +357 -0
  283. package/dist/types/protocol/parts.d.ts.map +1 -0
  284. package/dist/types/protocol/reduce.d.ts +26 -0
  285. package/dist/types/protocol/reduce.d.ts.map +1 -0
  286. package/dist/types/protocol/selectors.d.ts +64 -0
  287. package/dist/types/protocol/selectors.d.ts.map +1 -0
  288. package/dist/types/protocol/seq-cursor.d.ts +66 -0
  289. package/dist/types/protocol/seq-cursor.d.ts.map +1 -0
  290. package/dist/types/protocol/stop-reasons.d.ts +33 -0
  291. package/dist/types/protocol/stop-reasons.d.ts.map +1 -0
  292. package/dist/types/protocol/thinking.d.ts +28 -0
  293. package/dist/types/protocol/thinking.d.ts.map +1 -0
  294. package/dist/types/protocol/time.d.ts +21 -0
  295. package/dist/types/protocol/time.d.ts.map +1 -0
  296. package/dist/types/protocol/tokens.d.ts +29 -0
  297. package/dist/types/protocol/tokens.d.ts.map +1 -0
  298. package/dist/types/protocol/tool-closure.d.ts +25 -0
  299. package/dist/types/protocol/tool-closure.d.ts.map +1 -0
  300. package/dist/types/protocol/tool-state.d.ts +209 -0
  301. package/dist/types/protocol/tool-state.d.ts.map +1 -0
  302. package/dist/types/protocol/verify.d.ts +52 -0
  303. package/dist/types/protocol/verify.d.ts.map +1 -0
  304. package/dist/types/protocol/vocabulary.d.ts +19 -0
  305. package/dist/types/protocol/vocabulary.d.ts.map +1 -0
  306. package/dist/types/protocol/wire-api.d.ts +1983 -0
  307. package/dist/types/protocol/wire-api.d.ts.map +1 -0
  308. package/dist/types/protocol/wire.d.ts +1758 -0
  309. package/dist/types/protocol/wire.d.ts.map +1 -0
  310. package/dist/types/server/acp/binding.d.ts +33 -0
  311. package/dist/types/server/acp/binding.d.ts.map +1 -0
  312. package/dist/types/server/acp/session-records.d.ts +17 -0
  313. package/dist/types/server/acp/session-records.d.ts.map +1 -0
  314. package/dist/types/server/acp/translate.d.ts +48 -0
  315. package/dist/types/server/acp/translate.d.ts.map +1 -0
  316. package/dist/types/server/agent-server.d.ts +121 -0
  317. package/dist/types/server/agent-server.d.ts.map +1 -0
  318. package/dist/types/server/feedback.d.ts +37 -0
  319. package/dist/types/server/feedback.d.ts.map +1 -0
  320. package/dist/types/server/history.d.ts +15 -0
  321. package/dist/types/server/history.d.ts.map +1 -0
  322. package/dist/types/server/index.d.ts +7 -0
  323. package/dist/types/server/index.d.ts.map +1 -0
  324. package/dist/types/server/install.d.ts +42 -0
  325. package/dist/types/server/install.d.ts.map +1 -0
  326. package/dist/types/server/open-parts.d.ts +27 -0
  327. package/dist/types/server/open-parts.d.ts.map +1 -0
  328. package/dist/types/server/routes.d.ts +61 -0
  329. package/dist/types/server/routes.d.ts.map +1 -0
  330. package/dist/types/server/secrets.d.ts +11 -0
  331. package/dist/types/server/secrets.d.ts.map +1 -0
  332. package/dist/types/server/session-gate.d.ts +32 -0
  333. package/dist/types/server/session-gate.d.ts.map +1 -0
  334. package/dist/types/server/session-log.d.ts +39 -0
  335. package/dist/types/server/session-log.d.ts.map +1 -0
  336. package/dist/types/server/session-store.d.ts +119 -0
  337. package/dist/types/server/session-store.d.ts.map +1 -0
  338. package/dist/types/server/subscriptions.d.ts +74 -0
  339. package/dist/types/server/subscriptions.d.ts.map +1 -0
  340. package/dist/types/server/transports.d.ts +74 -0
  341. package/dist/types/server/transports.d.ts.map +1 -0
  342. package/dist/types/server/wire-error.d.ts +9 -0
  343. package/dist/types/server/wire-error.d.ts.map +1 -0
  344. package/dist/types/sync/event-log.d.ts +45 -0
  345. package/dist/types/sync/event-log.d.ts.map +1 -0
  346. package/dist/types/sync/fit.d.ts +40 -0
  347. package/dist/types/sync/fit.d.ts.map +1 -0
  348. package/dist/types/sync/history-reader.d.ts +8 -0
  349. package/dist/types/sync/history-reader.d.ts.map +1 -0
  350. package/dist/types/sync/history-sqlite.d.ts +37 -0
  351. package/dist/types/sync/history-sqlite.d.ts.map +1 -0
  352. package/dist/types/sync/history.d.ts +70 -0
  353. package/dist/types/sync/history.d.ts.map +1 -0
  354. package/dist/types/sync/index.d.ts +4 -0
  355. package/dist/types/sync/index.d.ts.map +1 -0
  356. package/dist/types/sync/journal-sqlite.d.ts +51 -0
  357. package/dist/types/sync/journal-sqlite.d.ts.map +1 -0
  358. package/dist/types/sync/journal.d.ts +107 -0
  359. package/dist/types/sync/journal.d.ts.map +1 -0
  360. package/dist/types/sync/live-tails.d.ts +30 -0
  361. package/dist/types/sync/live-tails.d.ts.map +1 -0
  362. package/dist/types/sync/node-sqlite.d.ts +7 -0
  363. package/dist/types/sync/node-sqlite.d.ts.map +1 -0
  364. package/dist/types/sync/projection.d.ts +10 -0
  365. package/dist/types/sync/projection.d.ts.map +1 -0
  366. package/dist/types/sync/recovery.d.ts +25 -0
  367. package/dist/types/sync/recovery.d.ts.map +1 -0
  368. package/dist/types/sync/retry.d.ts +25 -0
  369. package/dist/types/sync/retry.d.ts.map +1 -0
  370. package/dist/types/sync/schema.d.ts +4 -0
  371. package/dist/types/sync/schema.d.ts.map +1 -0
  372. package/dist/types/sync/sql.d.ts +38 -0
  373. package/dist/types/sync/sql.d.ts.map +1 -0
  374. package/dist/types/sync/sqlite-file.d.ts +9 -0
  375. package/dist/types/sync/sqlite-file.d.ts.map +1 -0
  376. package/dist/types/sync/sqlite.d.ts +6 -0
  377. package/dist/types/sync/sqlite.d.ts.map +1 -0
  378. package/dist/types/sync/worker.d.ts +63 -0
  379. package/dist/types/sync/worker.d.ts.map +1 -0
  380. package/docs/consuming.md +452 -53
  381. package/docs/deploy.md +218 -47
  382. package/docs/{sync-experiment.md → durable-sessions.md} +77 -72
  383. package/docs/feedback.md +116 -0
  384. package/docs/harnesses.md +212 -88
  385. package/docs/sync-history.md +29 -13
  386. package/docs/sync-integration.md +52 -66
  387. package/package.json +45 -28
  388. package/src/accounts/chatgpt.ts +194 -0
  389. package/src/accounts/check.ts +112 -0
  390. package/src/accounts/claude.ts +111 -0
  391. package/src/accounts/endpoint.ts +151 -0
  392. package/src/accounts/http.ts +97 -0
  393. package/src/accounts/index.ts +19 -0
  394. package/src/client/client.ts +604 -670
  395. package/src/client/connection.ts +371 -0
  396. package/src/client/history.ts +29 -52
  397. package/src/client/index.ts +11 -2
  398. package/src/client/listeners.ts +20 -0
  399. package/src/client/logins.ts +105 -0
  400. package/src/client/rpc.ts +155 -0
  401. package/src/client/streams.ts +488 -0
  402. package/src/client/transports.ts +62 -8
  403. package/src/client/turns.ts +171 -0
  404. package/src/core/agents/acp/acp-agent.ts +94 -118
  405. package/src/core/agents/acp/adapter.ts +20 -9
  406. package/src/core/agents/acp/client.ts +125 -47
  407. package/src/core/agents/acp/known-agents.ts +36 -4
  408. package/src/core/agents/acp/mappings.ts +64 -18
  409. package/src/core/agents/acp/requests.ts +125 -0
  410. package/src/core/agents/attachments.ts +18 -0
  411. package/src/core/agents/base.ts +120 -42
  412. package/src/core/agents/claude-code/adapter.ts +177 -238
  413. package/src/core/agents/claude-code/approvals.ts +270 -0
  414. package/src/core/agents/claude-code/claude-agent.ts +175 -158
  415. package/src/core/agents/claude-code/credentials.ts +171 -0
  416. package/src/core/agents/claude-code/events.ts +55 -0
  417. package/src/core/agents/claude-code/login.ts +269 -0
  418. package/src/core/agents/claude-code/mcp.ts +206 -0
  419. package/src/core/agents/claude-code/models.ts +15 -9
  420. package/src/core/agents/claude-code/options.ts +326 -39
  421. package/src/core/agents/claude-code/outcome.ts +209 -0
  422. package/src/core/agents/claude-code/session-pool.ts +136 -0
  423. package/src/core/agents/claude-code/session.ts +513 -0
  424. package/src/core/agents/claude-code/spawn.ts +144 -0
  425. package/src/core/agents/claude-code/spend.ts +160 -0
  426. package/src/core/agents/claude-code/tools.ts +47 -0
  427. package/src/core/agents/claude-code/turn-usage.ts +135 -0
  428. package/src/core/agents/claude-code/turn.ts +232 -0
  429. package/src/core/agents/codex-app-server/adapter.ts +314 -117
  430. package/src/core/agents/codex-app-server/client.ts +186 -84
  431. package/src/core/agents/codex-app-server/codex-agent.ts +343 -0
  432. package/src/core/agents/codex-app-server/commands.ts +228 -0
  433. package/src/core/agents/codex-app-server/credentials.ts +126 -0
  434. package/src/core/agents/codex-app-server/effort.ts +29 -0
  435. package/src/core/agents/codex-app-server/events.ts +47 -0
  436. package/src/core/agents/codex-app-server/homes.ts +64 -0
  437. package/src/core/agents/codex-app-server/input.ts +60 -0
  438. package/src/core/agents/codex-app-server/items.ts +149 -0
  439. package/src/core/agents/codex-app-server/login.ts +180 -0
  440. package/src/core/agents/codex-app-server/mcp.ts +182 -37
  441. package/src/core/agents/codex-app-server/models.ts +99 -46
  442. package/src/core/agents/codex-app-server/options.ts +146 -0
  443. package/src/core/agents/codex-app-server/plan-approval.ts +135 -0
  444. package/src/core/agents/codex-app-server/protocol.ts +117 -0
  445. package/src/core/agents/codex-app-server/requests.ts +332 -0
  446. package/src/core/agents/codex-app-server/session.ts +285 -0
  447. package/src/core/agents/codex-app-server/subagents.ts +108 -42
  448. package/src/core/agents/codex-app-server/thread-settings.ts +279 -0
  449. package/src/core/agents/codex-app-server/turn-items.ts +88 -0
  450. package/src/core/agents/codex-app-server/turn.ts +463 -0
  451. package/src/core/agents/config-fingerprint.ts +7 -14
  452. package/src/core/agents/credentials.ts +98 -0
  453. package/src/core/agents/elicitation.ts +75 -0
  454. package/src/core/agents/inherit-config.ts +21 -0
  455. package/src/core/agents/login.ts +116 -0
  456. package/src/core/agents/mcp-config.ts +112 -8
  457. package/src/core/agents/plan.ts +25 -0
  458. package/src/core/agents/registry.ts +21 -4
  459. package/src/core/agents/session-agent.ts +96 -4
  460. package/src/core/agents/{session-store.ts → session-pool.ts} +11 -5
  461. package/src/core/agents/stderr-tail.ts +75 -0
  462. package/src/core/agents/types.ts +40 -5
  463. package/src/core/index.ts +58 -48
  464. package/src/core/presets.ts +100 -39
  465. package/src/core/processes/identity.ts +113 -0
  466. package/src/core/processes/lifeline.ts +170 -0
  467. package/src/core/processes/native-processes.ts +379 -0
  468. package/src/core/processes/process-table.ts +174 -0
  469. package/src/core/provision/cache-trust.ts +121 -0
  470. package/src/core/provision/cache-usage.ts +69 -0
  471. package/src/core/provision/checksums.ts +64 -0
  472. package/src/core/provision/extract.ts +11 -2
  473. package/src/core/provision/index.ts +4 -1
  474. package/src/core/provision/install-build.ts +136 -0
  475. package/src/core/provision/npm.ts +112 -17
  476. package/src/core/provision/pins.ts +9 -33
  477. package/src/core/provision/platform.ts +12 -1
  478. package/src/core/provision/provisioner.ts +329 -397
  479. package/src/core/provision/releases.ts +86 -0
  480. package/src/core/provision/tools.ts +329 -0
  481. package/src/core/proxy/anthropic-proxy.ts +55 -25
  482. package/src/core/proxy/api-key-store.ts +2 -12
  483. package/src/core/proxy/loopback.ts +106 -0
  484. package/src/core/runtime/agent-runtime.ts +327 -498
  485. package/src/core/runtime/event-processor.ts +197 -70
  486. package/src/core/runtime/harness-clis.ts +61 -0
  487. package/src/core/runtime/harness-logins.ts +328 -0
  488. package/src/core/runtime/login-reads.ts +78 -0
  489. package/src/core/runtime/model-lists.ts +33 -48
  490. package/src/core/runtime/permission-policy.ts +113 -0
  491. package/src/core/runtime/request-broker.ts +204 -0
  492. package/src/core/runtime/steering.ts +112 -0
  493. package/src/core/runtime/turn-outcome.ts +30 -0
  494. package/src/core/runtime/turn.ts +360 -0
  495. package/src/core/utils/bounded.ts +12 -0
  496. package/src/core/utils/deadline.ts +38 -0
  497. package/src/core/utils/errors.ts +113 -21
  498. package/src/core/utils/is-record.ts +4 -0
  499. package/src/core/utils/loopback.ts +10 -0
  500. package/src/protocol/account.ts +104 -0
  501. package/src/protocol/auth.ts +199 -0
  502. package/src/protocol/canonical.ts +16 -0
  503. package/src/protocol/clis.ts +69 -0
  504. package/src/protocol/config.ts +60 -6
  505. package/src/protocol/conversation.ts +272 -0
  506. package/src/protocol/credentials.ts +145 -0
  507. package/src/protocol/errors.ts +78 -20
  508. package/src/protocol/execution.ts +5 -2
  509. package/src/protocol/factories.ts +55 -7
  510. package/src/protocol/guards.ts +55 -2
  511. package/src/protocol/harness.ts +56 -6
  512. package/src/protocol/history.ts +72 -64
  513. package/src/protocol/ids.ts +0 -12
  514. package/src/protocol/index.ts +9 -0
  515. package/src/protocol/lifecycle.ts +250 -79
  516. package/src/protocol/meta-keys.ts +17 -0
  517. package/src/protocol/meta.ts +3 -3
  518. package/src/protocol/models.ts +2 -43
  519. package/src/protocol/part-input.ts +2 -1
  520. package/src/protocol/parts.ts +12 -3
  521. package/src/protocol/reduce.ts +302 -413
  522. package/src/protocol/selectors.ts +1 -20
  523. package/src/protocol/seq-cursor.ts +14 -0
  524. package/src/protocol/stop-reasons.ts +1 -1
  525. package/src/protocol/thinking.ts +1 -30
  526. package/src/protocol/time.ts +2 -2
  527. package/src/protocol/tool-closure.ts +93 -0
  528. package/src/protocol/tool-state.ts +19 -4
  529. package/src/protocol/verify.ts +222 -92
  530. package/src/protocol/vocabulary.ts +21 -5
  531. package/src/protocol/wire-api.ts +232 -0
  532. package/src/protocol/wire.ts +351 -64
  533. package/src/server/acp/binding.ts +63 -103
  534. package/src/server/acp/session-records.ts +53 -0
  535. package/src/server/acp/translate.ts +129 -20
  536. package/src/server/agent-server.ts +223 -387
  537. package/src/server/bin.ts +280 -78
  538. package/src/server/feedback.ts +204 -14
  539. package/src/server/history.ts +116 -122
  540. package/src/server/index.ts +3 -8
  541. package/src/server/install.ts +107 -8
  542. package/src/server/open-parts.ts +95 -0
  543. package/src/server/routes.ts +391 -0
  544. package/src/server/secrets.ts +24 -0
  545. package/src/server/session-gate.ts +69 -0
  546. package/src/server/session-log.ts +14 -2
  547. package/src/server/session-store.ts +136 -72
  548. package/src/server/subscriptions.ts +232 -0
  549. package/src/server/transports.ts +125 -25
  550. package/src/server/wire-error.ts +31 -45
  551. package/src/sync/event-log.ts +31 -3
  552. package/src/sync/fit.ts +27 -11
  553. package/src/sync/history-reader.ts +10 -5
  554. package/src/sync/history-sqlite.ts +37 -42
  555. package/src/sync/history.ts +9 -15
  556. package/src/sync/index.ts +17 -5
  557. package/src/sync/journal-sqlite.ts +162 -76
  558. package/src/sync/journal.ts +91 -32
  559. package/src/sync/live-tails.ts +107 -0
  560. package/src/sync/projection.ts +35 -0
  561. package/src/sync/recovery.ts +80 -51
  562. package/src/sync/schema.ts +27 -6
  563. package/docs/rfds/0001-deterministic-echo-ids.md +0 -44
  564. package/docs/rfds/0002-native-subagents.md +0 -197
  565. package/src/client/request.ts +0 -19
  566. package/src/core/agents/claude-code/generator-session.ts +0 -572
  567. package/src/core/agents/claude-code/session-manager.ts +0 -166
  568. package/src/core/agents/codex-app-server/codex-app-server-agent.ts +0 -962
  569. package/src/core/agents/codex-items.ts +0 -149
  570. package/src/core/agents/codex-sdk/adapter.ts +0 -237
  571. package/src/core/agents/codex-sdk/codex-sdk-agent.ts +0 -330
  572. package/src/core/agents/tool-meta.ts +0 -68
@@ -0,0 +1,343 @@
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
+ import type {
5
+ AgentExecuteOptions,
6
+ CancelResult,
7
+ ListModelsOptions,
8
+ RawAgentEvent,
9
+ } from "../base.ts";
10
+ import { configFingerprint } from "../config-fingerprint.ts";
11
+ import type { CredentialSource } from "../credentials.ts";
12
+ import type { InheritConfig } from "../inherit-config.ts";
13
+ import { type DeliverSteer, SessionAgent } from "../session-agent.ts";
14
+ import { IDLE_TIMEOUT_MS } from "../session-pool.ts";
15
+ import {
16
+ CodexAppServerClient,
17
+ type CodexAppServerClientOptions,
18
+ type CodexConnection,
19
+ } from "./client.ts";
20
+ import { TurnCommands } from "./commands.ts";
21
+ import { type CodexAuth, authFor, logIn } from "./credentials.ts";
22
+ import { toUserInput } from "./input.ts";
23
+ import { CodexCliLogin } from "./login.ts";
24
+ import { codexModelDescriptors, listCodexModels, readCodexListing } from "./models.ts";
25
+ import { CODEX_TOOLS_REQUIRED, clientOptionsFor } from "./options.ts";
26
+ import type { CodexTurnScope } from "./requests.ts";
27
+ import { type CodexSession, CodexSessions } from "./session.ts";
28
+ import { CodexTurnSubagents, ManagedStop } from "./subagents.ts";
29
+ import { TurnItems } from "./turn-items.ts";
30
+ import { CodexTurn, type CodexTurnTiming, InterruptStop, type TurnStop } from "./turn.ts";
31
+
32
+ const CAPABILITIES: AgentCapabilities = {
33
+ multiTurn: true,
34
+ sessionResume: true,
35
+ // Model/effort are overridable per turn/start, so no restart is needed.
36
+ modelSwitch: "in_session",
37
+ thinkingLevels: true,
38
+ images: true,
39
+ // Codex has no input for file content: text is inlined (`input.ts`).
40
+ files: ["text"],
41
+ mcpServers: true,
42
+ permissionRequests: true,
43
+ // Its approval's answer is a bare verdict.
44
+ rejectionMessages: false,
45
+ turnScopedSubagents: true,
46
+ steering: true,
47
+ };
48
+
49
+ /** How long a retryable error may go unanswered before the turn is failed. */
50
+ const RETRY_STALL_TIMEOUT_MS = 120_000;
51
+
52
+ /** How long Codex has to confirm a stop, unless `cancelGraceMs` says otherwise. */
53
+ const CANCEL_GRACE_MS = 2_000;
54
+
55
+ export interface CodexAppServerAgentOptions {
56
+ /** Make the connection to `codex app-server` (a test's fake); a `CodexAppServerClient` by default. */
57
+ createClient?: (options: CodexAppServerClientOptions) => CodexConnection;
58
+ idleTimeoutMs?: number;
59
+ /** Fail a turn if the app-server goes silent this long after a retryable error. */
60
+ retryStallTimeoutMs?: number;
61
+ /**
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.
69
+ */
70
+ cancelGraceMs?: number;
71
+ /**
72
+ * Resolve the `codex` binary to spawn (typically the provisioner's,
73
+ * memoized). Falls back to `$CODEX_CLI_PATH`, then PATH. Operator-level
74
+ * only — never sourced from the wire.
75
+ */
76
+ resolveCliPath?: () => Promise<string | undefined>;
77
+ /**
78
+ * Credentials the product holds (LLP 0012), asked before each turn: an API
79
+ * key, or a ChatGPT token kept in the app-server's memory and asked for
80
+ * again when Codex needs a fresher one; none runs the turn as it would
81
+ * without them (its own `apiKey`, else the CLI's own login). Operator-level
82
+ * only.
83
+ */
84
+ credentials?: CredentialSource;
85
+ /**
86
+ * Where the app-servers start: each in a group of its own, held by the
87
+ * lifeline and recorded for its session (`createAgentRegistry` passes the
88
+ * runtime's). Operator-level only.
89
+ */
90
+ processes?: NativeProcesses;
91
+ /**
92
+ * 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`
94
+ * every turn must name a home of its session's own (absolute, not the
95
+ * shared one, one session to a home): the user's configuration stays out,
96
+ * the repository's `AGENTS.md` and skills load. `none` also keeps the
97
+ * repository's `AGENTS.md` out (`project_doc_max_bytes = 0`). Codex loads
98
+ * the repository's skills and `~/.agents/skills` at every level.
99
+ */
100
+ inheritConfig?: InheritConfig;
101
+ }
102
+
103
+ /**
104
+ * Codex harness backed by a long-lived `codex app-server` subprocess speaking
105
+ * JSON-RPC. Unlike the SDK harness this streams token-level deltas. A client +
106
+ * thread is created per logical session and reused for multi-turn; model/effort
107
+ * are passed per `turn/start`. Resume uses `thread/resume` with the native id.
108
+ */
109
+ export class CodexAppServerAgent extends SessionAgent {
110
+ readonly harness = "codex-app-server" as const;
111
+ readonly capabilities = CAPABILITIES;
112
+ /** Its CLIs start here: the runtime's, or this OS process's own when it was made without one. */
113
+ readonly processes: NativeProcesses;
114
+ private readonly timing: CodexTurnTiming;
115
+ 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
+ readonly login = new CodexCliLogin({
119
+ connect: (env) =>
120
+ this.connect({ cwd: process.cwd(), ...(env && { env }) }, { experimentalApi: false }),
121
+ cliPath: async () => (await this.cliPath()) ?? "codex",
122
+ });
123
+
124
+ constructor(private readonly agentOptions: CodexAppServerAgentOptions = {}) {
125
+ super();
126
+ this.processes = agentOptions.processes ?? sharedNativeProcesses();
127
+ this.timing = {
128
+ graceMs: agentOptions.cancelGraceMs ?? CANCEL_GRACE_MS,
129
+ retryStallMs: agentOptions.retryStallTimeoutMs ?? RETRY_STALL_TIMEOUT_MS,
130
+ };
131
+ this.sessions = new CodexSessions(
132
+ // Plan mode is a collaboration mode, which Codex 0.160 takes only from a
133
+ // client that opted into its experimental API (as host-managed ChatGPT
134
+ // auth and turn-scoped subagents need anyway); a session can enter plan
135
+ // mode on any later turn.
136
+ (options, auth, signal) =>
137
+ this.connect(options, {
138
+ auth,
139
+ experimentalApi: true,
140
+ signal,
141
+ sessionId: options.sessionId,
142
+ }),
143
+ agentOptions.idleTimeoutMs ?? IDLE_TIMEOUT_MS,
144
+ agentOptions.inheritConfig,
145
+ );
146
+ }
147
+
148
+ /** On a product's credentials, each session may list as a different account. */
149
+ get modelsPerSession(): boolean {
150
+ return Boolean(this.agentOptions.credentials);
151
+ }
152
+
153
+ protected override async *executeTurn(
154
+ input: AgentInput,
155
+ options: AgentExecuteOptions,
156
+ controller: AbortController,
157
+ ): AsyncIterableIterator<RawAgentEvent> {
158
+ const { signal } = controller;
159
+ const scope: CodexTurnScope = {
160
+ onPermissionRequest: options.onPermissionRequest,
161
+ onElicitationRequest: options.onElicitationRequest,
162
+ signal,
163
+ items: new TurnItems(),
164
+ mode: options.permissionMode ?? "default",
165
+ };
166
+ let session: CodexSession | undefined;
167
+ let stop: TurnStop | undefined;
168
+ let turn: CodexTurn | undefined;
169
+ try {
170
+ signal.throwIfAborted();
171
+ // @ref LLP 0001#d5--messages-map-to-model-messages-no-silent-surface — no silent surface
172
+ // A turn Codex cannot run as asked fails before any process starts:
173
+ // its tools are not to be turned off, and an attachment it cannot take
174
+ // would go unread.
175
+ if (options.disableTools) throw new Error(CODEX_TOOLS_REQUIRED);
176
+ const userInput = toUserInput(input);
177
+ const predecessor = this.sessions.stopUnsettled(options.sessionId);
178
+ if (predecessor && !(await predecessor))
179
+ throw new Error("Previous Codex execution has not stopped");
180
+ const mcp = this.sessions.mcpFor(options);
181
+ scope.auth = await authFor({
182
+ credentials: this.agentOptions.credentials,
183
+ sessionId: options.sessionId,
184
+ turnId: options.turnId,
185
+ apiKey: options.apiKey,
186
+ signal,
187
+ });
188
+ signal.throwIfAborted();
189
+ session = await this.sessions.acquire(options, scope, mcp);
190
+ options.onNativeSession?.(session.threadId, {
191
+ resumed: session.threadId === options.resumeSessionId,
192
+ });
193
+ const commands = new TurnCommands(session.client, session.threadId, this.timing.graceMs);
194
+ stop = this.stopFor(session, scope, commands, options.sessionId);
195
+ signal.throwIfAborted();
196
+ const offPreparing = onAbort(signal, () => void stop?.cancel());
197
+ try {
198
+ await session.mcp.prepare(session.threadId, mcp, options.onWarning);
199
+ signal.throwIfAborted();
200
+ } finally {
201
+ offPreparing();
202
+ }
203
+ const steering = {
204
+ open: (deliver: DeliverSteer) => void this.openSteering(options, deliver),
205
+ hold: () => this.holdSteering(options),
206
+ };
207
+ turn = new CodexTurn(session, scope, options, commands, stop, steering, this.timing);
208
+ this.turns.set(controller, turn);
209
+ yield* turn.run(userInput);
210
+ } catch (error) {
211
+ if (!signal.aborted) throw error;
212
+ if (turn) yield* turn.endCancelled();
213
+ throw new Error("turn aborted");
214
+ } finally {
215
+ await stop?.dispose();
216
+ turn?.dispose();
217
+ if (session) this.sessions.leave(options.sessionId, session, scope);
218
+ }
219
+ }
220
+
221
+ /** How the turn stops: by Codex's interrupt, or, managed, with its native cleanup. */
222
+ private stopFor(
223
+ session: CodexSession,
224
+ scope: CodexTurnScope,
225
+ commands: TurnCommands,
226
+ sessionId: string,
227
+ ): TurnStop {
228
+ const { client, threadId } = session;
229
+ const { graceMs } = this.timing;
230
+ if (session.subagentMode !== "turn")
231
+ return new InterruptStop(session, commands, scope.signal, graceMs);
232
+ scope.subagents = new CodexTurnSubagents(client, threadId, graceMs);
233
+ return new ManagedStop(scope.subagents, client, threadId, commands, scope.signal, graceMs, () =>
234
+ this.sessions.quarantine(sessionId, client),
235
+ );
236
+ }
237
+
238
+ protected override checkSteerInput(input: AgentInput): void {
239
+ toUserInput(input);
240
+ }
241
+
242
+ /**
243
+ * The models Codex offers this login. Reuses the session's process when
244
+ * `sessionId` has one started with the same `env` (its `CODEX_HOME` decides
245
+ * the login and config); otherwise starts `codex app-server` just to ask,
246
+ * with the account the product's `credentials` hold for that session.
247
+ */
248
+ async listModels(options: ListModelsOptions): Promise<ModelDescriptor[]> {
249
+ const live = options.sessionId ? this.sessions.get(options.sessionId) : undefined;
250
+ if (live && !live.client.closed && live.envFingerprint === configFingerprint(options.env)) {
251
+ const listing = await readCodexListing(live.client, options.cwd);
252
+ return codexModelDescriptors(listing.models, this.harness, listing.configuredModel);
253
+ }
254
+ // The account a turn with these options would log in as.
255
+ const auth = await authFor({
256
+ credentials: this.agentOptions.credentials,
257
+ sessionId: options.sessionId,
258
+ apiKey: options.apiKey,
259
+ ...(options.signal && { signal: options.signal }),
260
+ });
261
+ options.signal?.throwIfAborted();
262
+ const client = await this.connect(options, {
263
+ auth,
264
+ experimentalApi: auth?.type === "chatgptAuthTokens",
265
+ });
266
+ const listing = await listCodexModels(client, options.cwd, {
267
+ signal: options.signal,
268
+ ...(auth && { login: (connection) => logIn(connection, auth) }),
269
+ });
270
+ return codexModelDescriptors(listing.models, this.harness, listing.configuredModel);
271
+ }
272
+
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 };
287
+ }
288
+
289
+ /** Unchanged MCP config reloads retain dead remote connections after VM resume. */
290
+ invalidateMcpConnections(): void {
291
+ this.sessions.invalidate();
292
+ }
293
+
294
+ loginChanged(): void {
295
+ this.sessions.loginChanged();
296
+ }
297
+
298
+ override async release(sessionId: string): Promise<void> {
299
+ // A host may release this harness before switching providers. Do not let
300
+ // that path discard ownership while managed child cleanup is unconfirmed.
301
+ if (this.sessions.managed(sessionId) && !(await this.cancel(sessionId)).confirmed)
302
+ throw new Error("Codex native cleanup was not confirmed; cannot release the session");
303
+ await super.release(sessionId);
304
+ this.sessions.release(sessionId);
305
+ }
306
+
307
+ override async terminateAll(): Promise<void> {
308
+ await super.terminateAll();
309
+ this.sessions.terminateAll();
310
+ }
311
+
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;
315
+ }
316
+
317
+ /**
318
+ * A connection to a new `codex app-server` for a folder, its `env` and a
319
+ * login; recorded for `sessionId`, which a model listing has none of.
320
+ */
321
+ private async connect(
322
+ options: { cwd: string; env?: Record<string, string> },
323
+ spawn: {
324
+ /** The account it logs in as; none runs the store's own login. */
325
+ auth?: CodexAuth;
326
+ experimentalApi: boolean;
327
+ signal?: AbortSignal;
328
+ sessionId?: string;
329
+ },
330
+ ): Promise<CodexConnection> {
331
+ const { auth, experimentalApi, signal, sessionId } = spawn;
332
+ const codexPath = (await this.cliPath()) ?? "codex";
333
+ signal?.throwIfAborted();
334
+ const clientOptions = {
335
+ ...clientOptionsFor(codexPath, options, auth, experimentalApi),
336
+ processes: this.processes,
337
+ ...(sessionId !== undefined && { sessionId }),
338
+ };
339
+ return (
340
+ this.agentOptions.createClient?.(clientOptions) ?? new CodexAppServerClient(clientOptions)
341
+ );
342
+ }
343
+ }
@@ -0,0 +1,228 @@
1
+ import { type CodexConnection, pages } from "./client.ts";
2
+ import { type CodexRawEvent, isSyntheticEvent } from "./events.ts";
3
+ import { type CodexItem, type CodexNotification, codexItem } from "./protocol.ts";
4
+
5
+ /** A command Codex keeps running for a thread in its own terminal (0.160). */
6
+ interface CodexBackgroundTerminal {
7
+ itemId?: string;
8
+ processId?: string;
9
+ }
10
+
11
+ /** How many pages of a thread's terminals are read before Codex is taken to not know. */
12
+ const TERMINAL_PAGES = 20;
13
+
14
+ /** Every background terminal of a thread. Throws when Codex cannot say. */
15
+ export async function listBackgroundTerminals(
16
+ client: Pick<CodexConnection, "request">,
17
+ threadId: string,
18
+ timeoutMs: number,
19
+ ): Promise<CodexBackgroundTerminal[]> {
20
+ const terminals: CodexBackgroundTerminal[] = [];
21
+ for await (const page of pages<CodexBackgroundTerminal>(
22
+ client,
23
+ "thread/backgroundTerminals/list",
24
+ { threadId },
25
+ { timeoutMs, maxPages: TERMINAL_PAGES },
26
+ ))
27
+ terminals.push(...page);
28
+ return terminals;
29
+ }
30
+
31
+ /** A command a turn started, which a cancel of that turn stops. */
32
+ interface TurnCommand {
33
+ /** Codex's process for it, when it runs in a terminal Codex keeps. */
34
+ processId?: string;
35
+ /** Its item completed: what it ran has ended. */
36
+ done: boolean;
37
+ /** Its completion reached the stream, which shows how it ended. */
38
+ shown: boolean;
39
+ }
40
+
41
+ /**
42
+ * Whether a command item starts something the turn owns. A poll of, or input
43
+ * to, a process an earlier turn started (`unifiedExecInteraction`) does not,
44
+ * and neither does the user's own shell command.
45
+ */
46
+ function startsCommand(source: unknown): boolean {
47
+ return source !== "unifiedExecInteraction" && source !== "userShell";
48
+ }
49
+
50
+ /** How often a stop reads Codex's terminals again while one of the turn's is unaccounted for. */
51
+ const STOP_POLL_MS = 100;
52
+
53
+ // @ref LLP 0002#tools-a-turn-leaves-open — a cancel stops the commands the turn started
54
+ /**
55
+ * Stop the commands a cancelled turn started, and say which could not be
56
+ * confirmed stopped. Codex 0.160 ends a turn on `turn/interrupt` but keeps its
57
+ * running command alive as a background terminal, which can register after
58
+ * the turn ended. A command is stopped once its item completed, or once it was
59
+ * listed, terminated and is gone from a later list; the list is read again
60
+ * until each is, or `timeoutMs` passes. Terminals of earlier turns are left
61
+ * alone. Returns the item ids of the commands whose stop was not confirmed.
62
+ */
63
+ async function stopTurnCommands(
64
+ client: Pick<CodexConnection, "request">,
65
+ threadId: string,
66
+ commands: ReadonlyMap<string, TurnCommand>,
67
+ timeoutMs: number,
68
+ ): Promise<string[]> {
69
+ const deadline = Date.now() + timeoutMs;
70
+ const remaining = () => Math.max(1, deadline - Date.now());
71
+ /** The turn's commands seen running in a list: one gone from a later list stopped. */
72
+ const listed = new Set<string>();
73
+ for (;;) {
74
+ const open = [...commands].filter(([, command]) => !command.done).map(([itemId]) => itemId);
75
+ if (open.length === 0) return [];
76
+ const byProcess = new Map<string, string>();
77
+ for (const itemId of open) {
78
+ const processId = commands.get(itemId)?.processId;
79
+ if (processId) byProcess.set(processId, itemId);
80
+ }
81
+ let terminals: CodexBackgroundTerminal[];
82
+ try {
83
+ terminals = await listBackgroundTerminals(client, threadId, remaining());
84
+ } catch {
85
+ // Codex cannot say what still runs.
86
+ return open;
87
+ }
88
+ /** The turn's open commands running now, with their processes. */
89
+ const running = new Map<string, string | undefined>();
90
+ for (const terminal of terminals) {
91
+ const itemId =
92
+ terminal.itemId !== undefined && open.includes(terminal.itemId)
93
+ ? terminal.itemId
94
+ : terminal.processId !== undefined
95
+ ? byProcess.get(terminal.processId)
96
+ : undefined;
97
+ if (itemId) running.set(itemId, terminal.processId);
98
+ }
99
+ // Neither listed yet nor completed: it may still register, or complete.
100
+ const unaccounted = open.filter((itemId) => !running.has(itemId) && !listed.has(itemId));
101
+ if (running.size === 0 && unaccounted.length === 0) return [];
102
+ if (Date.now() >= deadline) return [...running.keys(), ...unaccounted];
103
+ for (const itemId of running.keys()) listed.add(itemId);
104
+ // One refused or unanswered terminate leaves that command running, no other.
105
+ await Promise.allSettled(
106
+ [...running.values()].map((processId) =>
107
+ processId === undefined
108
+ ? undefined
109
+ : client.request(
110
+ "thread/backgroundTerminals/terminate",
111
+ { threadId, processId },
112
+ remaining(),
113
+ ),
114
+ ),
115
+ );
116
+ await new Promise((done) => setTimeout(done, Math.min(STOP_POLL_MS, remaining())));
117
+ }
118
+ }
119
+
120
+ /**
121
+ * The commands a turn started, by item id: which still run, which the stream
122
+ * showed ending, and what a cancel of the turn could not stop. When a turn
123
+ * ends cancelled, a command's outcome is unknown when its stop could not be
124
+ * confirmed, or, with nothing stopped, when the stream never showed its end.
125
+ */
126
+ export class TurnCommands {
127
+ private readonly commands = new Map<string, TurnCommand>();
128
+ /** Completions that came after Codex's turn ended, which its events no longer take. */
129
+ private readonly late: CodexNotification[] = [];
130
+ /** Codex's turn was interrupted: what it started is to stop. */
131
+ private interrupting = false;
132
+ private stopping?: Promise<string[]>;
133
+
134
+ constructor(
135
+ private readonly client: CodexConnection,
136
+ private readonly threadId: string,
137
+ /** How long a stop has to confirm the commands stopped (`cancelGraceMs`). */
138
+ private readonly timeoutMs: number,
139
+ ) {}
140
+
141
+ /** Note an item Codex started: a command the turn owns, unless it is another's. */
142
+ started(item: CodexItem | undefined): void {
143
+ if (item?.type !== "commandExecution" || !startsCommand(item.source)) return;
144
+ this.commands.set(item.id, {
145
+ ...(typeof item.processId === "string" && { processId: item.processId }),
146
+ done: false,
147
+ shown: false,
148
+ });
149
+ }
150
+
151
+ /** Note an item Codex completed: whether it was one of the turn's commands. */
152
+ completed(item: CodexItem | undefined): boolean {
153
+ const command = item && this.commands.get(item.id);
154
+ if (command) command.done = true;
155
+ return Boolean(command);
156
+ }
157
+
158
+ /** Note an event reaching the stream: a command's completion shows how it ended. */
159
+ shown(event: CodexRawEvent): void {
160
+ if (isSyntheticEvent(event) || event.method !== "item/completed") return;
161
+ const item = codexItem(event);
162
+ const command = item && this.commands.get(item.id);
163
+ if (command) command.shown = true;
164
+ }
165
+
166
+ /** Keep a completion that came after Codex's turn ended, for the stream to show later. */
167
+ holdLate(n: CodexNotification): void {
168
+ this.late.push(n);
169
+ }
170
+
171
+ /**
172
+ * The completions kept since Codex's turn ended, to the stream, until none
173
+ * is left: shown before anything that follows. Once Codex's turn was
174
+ * interrupted there are none to show: a kill's completion is no news.
175
+ */
176
+ *drainLate(): Generator<CodexRawEvent> {
177
+ if (this.interrupting) return;
178
+ for (let done = this.late.shift(); done; done = this.late.shift()) {
179
+ this.shown(done);
180
+ yield done;
181
+ }
182
+ }
183
+
184
+ /** Whether a command of the turn still runs, as far as Codex said. */
185
+ get running(): boolean {
186
+ return [...this.commands.values()].some((command) => !command.done);
187
+ }
188
+
189
+ /** Codex's turn was interrupted, by a cancel or by Codex itself: what it started is to stop. */
190
+ interrupted(): void {
191
+ this.interrupting = true;
192
+ }
193
+
194
+ /**
195
+ * Stop what the turn started, once Codex's turn has ended, at most once.
196
+ * Codex's own shutdown (EOF) stops every terminal it keeps: the last
197
+ * resort, which also ends this session's process (the next turn resumes
198
+ * the thread). Resolves the commands whose stop was not confirmed.
199
+ */
200
+ stop(): Promise<string[]> {
201
+ this.interrupting = true;
202
+ this.stopping ??= (async () => {
203
+ const unstopped = await stopTurnCommands(
204
+ this.client,
205
+ this.threadId,
206
+ this.commands,
207
+ this.timeoutMs,
208
+ );
209
+ return unstopped.length && (await this.client.closeAndWait(this.timeoutMs)) ? [] : unstopped;
210
+ })();
211
+ return this.stopping;
212
+ }
213
+
214
+ /**
215
+ * The commands whose outcome a turn that ended cancelled leaves unknown:
216
+ * the ones a stop could not confirm stopped, or, when nothing was to stop,
217
+ * the ones whose end the stream never showed, left running on purpose.
218
+ */
219
+ async outcomeUnknown(): Promise<string[]> {
220
+ if (this.interrupting) return this.stop();
221
+ return [...this.commands].filter(([, command]) => !command.shown).map(([itemId]) => itemId);
222
+ }
223
+
224
+ /** The commands whose item never completed. */
225
+ notDone(): string[] {
226
+ return [...this.commands].filter(([, command]) => !command.done).map(([itemId]) => itemId);
227
+ }
228
+ }
@@ -0,0 +1,126 @@
1
+ import { type CodexCredential, tokenDigest } from "../../../protocol/index.ts";
2
+ import { configFingerprint } from "../config-fingerprint.ts";
3
+ import { type CredentialSource, credentialFor } from "../credentials.ts";
4
+ import type { CodexConnection } from "./client.ts";
5
+ import type { CodexSession } from "./session.ts";
6
+
7
+ /** ChatGPT tokens as Codex's app-server takes them (`chatgptAuthTokens`). */
8
+ interface ChatGptTokens {
9
+ accessToken: string;
10
+ chatgptAccountId: string;
11
+ chatgptPlanType?: string | null;
12
+ }
13
+
14
+ /**
15
+ * What a Codex process logs in with when the engine chooses: a key, or
16
+ * ChatGPT tokens held in its memory and how to get fresher ones. None: the
17
+ * CLI's own login.
18
+ */
19
+ export type CodexAuth =
20
+ | { type: "apiKey"; apiKey: string }
21
+ | { type: "chatgptAuthTokens"; tokens: ChatGptTokens; refresh(): Promise<ChatGptTokens> };
22
+
23
+ const chatGptTokens = (credential: Extract<CodexCredential, { type: "chatgpt" }>) => ({
24
+ accessToken: credential.accessToken,
25
+ chatgptAccountId: credential.accountId,
26
+ ...(credential.plan !== undefined && { chatgptPlanType: credential.plan }),
27
+ });
28
+
29
+ // @ref LLP 0012#what-the-engine-holds-for-the-third — no refresh token enters the app-server
30
+ /**
31
+ * The login a turn or a model list runs Codex on: the credential the product
32
+ * holds (`credentials`), an API key or a ChatGPT access token kept in the
33
+ * app-server's memory, which asks the product again (with the token it
34
+ * refused) when it needs a fresher one; without one, the turn's own `apiKey`;
35
+ * without that, the CLI's own login.
36
+ */
37
+ export async function authFor(ask: {
38
+ credentials: CredentialSource | undefined;
39
+ /** Absent for a model listing of no session: no credential is asked for. */
40
+ sessionId: string | undefined;
41
+ turnId?: string;
42
+ apiKey: string | undefined;
43
+ signal?: AbortSignal;
44
+ }): 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();
48
+ const request = { harness: "codex-app-server" as const, sessionId, ...(turnId && { turnId }) };
49
+ const credential = await credentialFor(credentials, request, signal);
50
+ if (!credential) return ownKey();
51
+ if (credential.type === "api_key") return { type: "apiKey", apiKey: credential.apiKey };
52
+ let current = credential;
53
+ return {
54
+ type: "chatgptAuthTokens",
55
+ tokens: chatGptTokens(current),
56
+ refresh: async () => {
57
+ const fresher = await credentialFor(credentials, {
58
+ ...request,
59
+ rejectedSha256: await tokenDigest(current.accessToken),
60
+ });
61
+ if (fresher?.type !== "chatgpt") {
62
+ throw new Error("The product holds no ChatGPT credential for this session any more");
63
+ }
64
+ current = fresher;
65
+ return chatGptTokens(current);
66
+ },
67
+ };
68
+ }
69
+
70
+ /** Who a process is logged in as: an account or key change needs a new process. */
71
+ export function authFingerprint(auth: CodexAuth | undefined): string | undefined {
72
+ if (!auth) return undefined;
73
+ return configFingerprint(
74
+ auth.type === "apiKey" ? auth : { type: auth.type, accountId: auth.tokens.chatgptAccountId },
75
+ );
76
+ }
77
+
78
+ /** Log the account in on Codex's process: without one, it runs the native login. */
79
+ export async function logIn(
80
+ client: Pick<CodexConnection, "request">,
81
+ auth: CodexAuth | undefined,
82
+ ): Promise<void> {
83
+ if (!auth) return;
84
+ await client.request(
85
+ "account/login/start",
86
+ auth.type === "apiKey"
87
+ ? { type: "apiKey", apiKey: auth.apiKey }
88
+ : {
89
+ type: "chatgptAuthTokens",
90
+ accessToken: auth.tokens.accessToken,
91
+ chatgptAccountId: auth.tokens.chatgptAccountId,
92
+ chatgptPlanType: auth.tokens.chatgptPlanType ?? null,
93
+ },
94
+ );
95
+ }
96
+
97
+ /**
98
+ * Codex asks for fresh ChatGPT tokens (`account/chatgptAuthTokens/refresh`):
99
+ * the product's source renews them, for the account the turn runs as and
100
+ * never another, and only while that turn is the session's.
101
+ */
102
+ export async function refreshTokens(
103
+ session: CodexSession,
104
+ params: Record<string, unknown>,
105
+ ): Promise<Record<string, unknown>> {
106
+ const turn = session.turn;
107
+ if (turn?.auth?.type !== "chatgptAuthTokens" || turn.signal.aborted || session.client.closed) {
108
+ throw new Error("Codex ChatGPT refresh has no active authenticated turn");
109
+ }
110
+ const accountId = turn.auth.tokens.chatgptAccountId;
111
+ if (params.previousAccountId != null && params.previousAccountId !== accountId) {
112
+ throw new Error("Codex ChatGPT refresh account does not match the selected account");
113
+ }
114
+ const tokens = await turn.auth.refresh();
115
+ if (session.turn !== turn || turn.signal.aborted || session.client.closed) {
116
+ throw new Error("Codex ChatGPT refresh outlived its turn");
117
+ }
118
+ if (tokens.chatgptAccountId !== accountId) {
119
+ throw new Error("Codex ChatGPT refresh returned a different account");
120
+ }
121
+ return {
122
+ accessToken: tokens.accessToken,
123
+ chatgptAccountId: tokens.chatgptAccountId,
124
+ chatgptPlanType: tokens.chatgptPlanType ?? null,
125
+ };
126
+ }