@opengeni/worker-bundle 2.0.1 → 2.0.2-canary.38686408778001

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 (379) hide show
  1. package/dist/activities/agent-run-admission.d.ts +1 -12
  2. package/dist/activities/agent-turn/admission.d.ts +88 -10
  3. package/dist/activities/agent-turn/agent-build.d.ts +14 -3
  4. package/dist/activities/agent-turn/claim.d.ts +4 -1
  5. package/dist/activities/agent-turn/claude-core.d.ts +195 -0
  6. package/dist/activities/agent-turn/code-search.d.ts +60 -0
  7. package/dist/activities/agent-turn/codex-capacity.d.ts +3 -0
  8. package/dist/activities/agent-turn/codex-core-capacity.d.ts +37 -0
  9. package/dist/activities/agent-turn/codex-core-claim.d.ts +40 -0
  10. package/dist/activities/agent-turn/codex-core-errors.d.ts +102 -0
  11. package/dist/activities/agent-turn/codex-core-failover.d.ts +15 -0
  12. package/dist/activities/agent-turn/codex-core-requests.d.ts +28 -0
  13. package/dist/activities/agent-turn/codex-core-settlement.d.ts +77 -0
  14. package/dist/activities/agent-turn/codex-credit-policy.d.ts +30 -0
  15. package/dist/activities/agent-turn/codex-plan-entitlement.d.ts +105 -0
  16. package/dist/activities/agent-turn/codex-usage-policy.d.ts +2 -0
  17. package/dist/activities/agent-turn/codex.d.ts +2 -19
  18. package/dist/activities/agent-turn/compaction-interruption.d.ts +32 -0
  19. package/dist/activities/agent-turn/compaction-prep.d.ts +30 -1
  20. package/dist/activities/agent-turn/credential-leases.d.ts +64 -37
  21. package/dist/activities/agent-turn/credential-restriction.d.ts +3 -0
  22. package/dist/activities/agent-turn/errors.d.ts +172 -28
  23. package/dist/activities/agent-turn/failure-settlement.d.ts +10 -43
  24. package/dist/activities/agent-turn/final-reply.d.ts +18 -0
  25. package/dist/activities/agent-turn/finalization-monitor.d.ts +7 -2
  26. package/dist/activities/agent-turn/finalization.d.ts +2 -0
  27. package/dist/activities/agent-turn/github-repository-bindings.d.ts +2 -0
  28. package/dist/activities/agent-turn/governance-model.d.ts +13 -1
  29. package/dist/activities/agent-turn/history-sink.d.ts +3 -1
  30. package/dist/activities/agent-turn/history.d.ts +2 -0
  31. package/dist/activities/agent-turn/input-wait-reply.d.ts +76 -0
  32. package/dist/activities/agent-turn/meaningful-output.d.ts +9 -0
  33. package/dist/activities/agent-turn/media-artifacts.d.ts +5 -3
  34. package/dist/activities/agent-turn/model-call-admission.d.ts +15 -0
  35. package/dist/activities/agent-turn/model-usage.d.ts +56 -14
  36. package/dist/activities/agent-turn/model-wire-dispatch.d.ts +15 -0
  37. package/dist/activities/agent-turn/optional-repositories.d.ts +59 -0
  38. package/dist/activities/agent-turn/prompt-tool-availability.d.ts +17 -0
  39. package/dist/activities/agent-turn/provider-dispatch-barrier.d.ts +12 -1
  40. package/dist/activities/agent-turn/provider-recovery-copy.d.ts +24 -0
  41. package/dist/activities/agent-turn/provider-recovery-metrics.d.ts +18 -0
  42. package/dist/activities/agent-turn/provider-recovery-policy.d.ts +6 -0
  43. package/dist/activities/agent-turn/quiescence.d.ts +6 -7
  44. package/dist/activities/agent-turn/recovery-warning.d.ts +6 -5
  45. package/dist/activities/agent-turn/refresh-credentials.d.ts +6 -0
  46. package/dist/activities/agent-turn/run-credentials.d.ts +9 -5
  47. package/dist/activities/agent-turn/run.d.ts +27 -2
  48. package/dist/activities/agent-turn/sandbox-establish.d.ts +1 -0
  49. package/dist/activities/agent-turn/sandbox-provision.d.ts +25 -1
  50. package/dist/activities/agent-turn/sandbox-route.d.ts +3 -0
  51. package/dist/activities/agent-turn/sandbox-runtime.d.ts +1 -1
  52. package/dist/activities/agent-turn/secret-resolver.d.ts +64 -0
  53. package/dist/activities/agent-turn/session-title.d.ts +60 -3
  54. package/dist/activities/agent-turn/session-usage-custody.d.ts +66 -0
  55. package/dist/activities/agent-turn/skill-checkout.d.ts +25 -6
  56. package/dist/activities/agent-turn/skill-read.d.ts +70 -2
  57. package/dist/activities/agent-turn/skill-search.d.ts +2 -0
  58. package/dist/activities/agent-turn/skill-tools.d.ts +41 -4
  59. package/dist/activities/agent-turn/skill-transfer.d.ts +18 -6
  60. package/dist/activities/agent-turn/stream-attempt.d.ts +6 -3
  61. package/dist/activities/agent-turn/subscription-capacity-arming.d.ts +41 -0
  62. package/dist/activities/agent-turn/subscription-core-capacity.d.ts +86 -0
  63. package/dist/activities/agent-turn/subscription-core-failover.d.ts +69 -0
  64. package/dist/activities/agent-turn/subscription-core-placed.d.ts +14 -0
  65. package/dist/activities/agent-turn/subscription-core-requests.d.ts +109 -0
  66. package/dist/activities/agent-turn/subscription-core-settlement.d.ts +112 -0
  67. package/dist/activities/agent-turn/subscription-lease.d.ts +44 -0
  68. package/dist/activities/agent-turn/subscription-serving-capacity.d.ts +6 -0
  69. package/dist/activities/agent-turn/subscription-serving-catalog.d.ts +13 -0
  70. package/dist/activities/agent-turn/subscription-serving-claim.d.ts +30 -0
  71. package/dist/activities/agent-turn/subscription-serving-providers.d.ts +2 -0
  72. package/dist/activities/agent-turn/tool-environment.d.ts +30 -6
  73. package/dist/activities/agent-turn/tool-policy.d.ts +55 -3
  74. package/dist/activities/agent-turn/turn-context.d.ts +104 -5
  75. package/dist/activities/agent-turn/web-search.d.ts +57 -0
  76. package/dist/activities/agent-turn/xai-core.d.ts +96 -0
  77. package/dist/activities/automations.d.ts +3 -1
  78. package/dist/activities/browser-deadline-checkpoint.d.ts +34 -0
  79. package/dist/activities/capabilities.d.ts +8 -20
  80. package/dist/activities/codemode-dispatcher.d.ts +20 -3
  81. package/dist/activities/codex-capacity.d.ts +1 -56
  82. package/dist/activities/codex-image-generation.d.ts +49 -3
  83. package/dist/activities/common.d.ts +3 -1
  84. package/dist/activities/context-compaction.d.ts +41 -2
  85. package/dist/activities/environment.d.ts +1 -19
  86. package/dist/activities/gmail-files.d.ts +50 -0
  87. package/dist/activities/goals.d.ts +1 -9
  88. package/dist/activities/incident-telemetry-authority.d.ts +5 -2
  89. package/dist/activities/incident-telemetry-preflight.d.ts +10 -3
  90. package/dist/activities/knowledge-indexing.d.ts +19 -0
  91. package/dist/activities/mcp-account-routes.d.ts +2 -21
  92. package/dist/activities/model-call-fact-reconciler.d.ts +34 -0
  93. package/dist/activities/open-suffix-resume.d.ts +15 -0
  94. package/dist/activities/parent-wake.d.ts +17 -12
  95. package/dist/activities/programmatic-approvals.d.ts +10 -0
  96. package/dist/activities/requester-unavailable-receipt.d.ts +8 -0
  97. package/dist/activities/retained-screenshots.d.ts +7 -2
  98. package/dist/activities/run-credential-renewal.d.ts +1 -0
  99. package/dist/activities/run-credentials.d.ts +16 -4
  100. package/dist/activities/run-input.d.ts +17 -0
  101. package/dist/activities/sandbox-lease.d.ts +132 -12
  102. package/dist/activities/scheduled-tasks.d.ts +4 -35
  103. package/dist/activities/session-archive.d.ts +58 -0
  104. package/dist/activities/session-state.d.ts +15 -3
  105. package/dist/activities/session-storage.d.ts +44 -0
  106. package/dist/activities/subscription-core-claude-waits.d.ts +2 -0
  107. package/dist/activities/subscription-core-codex-quota.d.ts +25 -0
  108. package/dist/activities/subscription-core-codex-waits.d.ts +153 -0
  109. package/dist/activities/subscription-core-image-operation.d.ts +29 -0
  110. package/dist/activities/subscription-core-usage-recheck.d.ts +49 -0
  111. package/dist/activities/subscription-core-wait-bindings.d.ts +3 -0
  112. package/dist/activities/subscription-core-xai-quota.d.ts +30 -0
  113. package/dist/activities/subscription-core-xai-waits.d.ts +2 -0
  114. package/dist/activities/types.d.ts +68 -5
  115. package/dist/activities/video-generation-admission.d.ts +2 -8
  116. package/dist/activities/video-generation-credential.d.ts +15 -12
  117. package/dist/activities/video-generation-subscription-core.d.ts +70 -0
  118. package/dist/activities/workspace-capture.d.ts +2 -0
  119. package/dist/activities/workspace-credential-provider.d.ts +47 -0
  120. package/dist/activities/xai-auth.d.ts +6 -18
  121. package/dist/activities/xai-image-generation.d.ts +22 -6
  122. package/dist/{activities-control-QA6OLRQY.js → activities-control-NTXZHYET.js} +1552 -2876
  123. package/dist/activities-control-NTXZHYET.js.map +1 -0
  124. package/dist/activities-control.d.ts +64 -4
  125. package/dist/{activities-turn-YVVKCGAK.js → activities-turn-RKRI5GY4.js} +9238 -4392
  126. package/dist/activities-turn-RKRI5GY4.js.map +1 -0
  127. package/dist/activities.d.ts +64 -4
  128. package/dist/artifact-materializer-entry.js +104 -35
  129. package/dist/artifact-materializer-entry.js.map +1 -1
  130. package/dist/artifact-outbox-entry.js +132 -61
  131. package/dist/artifact-outbox-entry.js.map +1 -1
  132. package/dist/browser-deadline-checkpoint-contract.d.ts +2 -0
  133. package/dist/{chunk-GDCLDKSH.js → chunk-24GP3BX5.js} +79 -72
  134. package/dist/chunk-24GP3BX5.js.map +1 -0
  135. package/dist/{chunk-RAMNYSAW.js → chunk-4ER3YXOT.js} +22 -4
  136. package/dist/chunk-4ER3YXOT.js.map +1 -0
  137. package/dist/{chunk-I7HKKMUJ.js → chunk-5NNS4327.js} +734 -58
  138. package/dist/chunk-5NNS4327.js.map +1 -0
  139. package/dist/{chunk-M3NVMF2V.js → chunk-7BIHFUGB.js} +2293 -3956
  140. package/dist/chunk-7BIHFUGB.js.map +1 -0
  141. package/dist/chunk-MVESOYFC.js +3746 -0
  142. package/dist/chunk-MVESOYFC.js.map +1 -0
  143. package/dist/chunk-NQDVJ6RI.js +2823 -0
  144. package/dist/chunk-NQDVJ6RI.js.map +1 -0
  145. package/dist/chunk-O7UPBWXK.js +79 -0
  146. package/dist/chunk-O7UPBWXK.js.map +1 -0
  147. package/dist/chunk-W2QSM5HR.js +414 -0
  148. package/dist/chunk-W2QSM5HR.js.map +1 -0
  149. package/dist/chunk-ZN3XJR33.js +227 -0
  150. package/dist/chunk-ZN3XJR33.js.map +1 -0
  151. package/dist/editable-artifact-outbox-dispatcher.d.ts +12 -1
  152. package/dist/editable-artifact-outbox-service.d.ts +3 -1
  153. package/dist/host-export-pump.d.ts +3 -1
  154. package/dist/host-export.d.ts +2 -2
  155. package/dist/host-export.js +1 -1
  156. package/dist/http.d.ts +1 -1
  157. package/dist/index.d.ts +49 -48
  158. package/dist/index.js +835 -256
  159. package/dist/index.js.map +1 -1
  160. package/dist/internal-maintenance.d.ts +63 -0
  161. package/dist/maintenance-cadence.d.ts +43 -0
  162. package/dist/observability-metrics.d.ts +293 -24
  163. package/dist/onepassword-vault-provider.d.ts +7 -0
  164. package/dist/parent-wake-PCLH7A3Y.js +24 -0
  165. package/dist/parent-wake-PCLH7A3Y.js.map +1 -0
  166. package/dist/personal-github-git-credentials.d.ts +1 -1
  167. package/dist/readiness-checks.d.ts +39 -0
  168. package/dist/retained-process-retry.d.ts +7 -0
  169. package/dist/sandbox-lease-BJYU72N3.js +84 -0
  170. package/dist/sandbox-lease-BJYU72N3.js.map +1 -0
  171. package/dist/sandbox-reaper-contract.d.ts +25 -0
  172. package/dist/sandbox-resume.d.ts +72 -4
  173. package/dist/sandbox-routing.d.ts +4 -2
  174. package/dist/session-event-fanout.d.ts +6 -0
  175. package/dist/site-auth-maintenance-WK3MR2PV.js +12 -0
  176. package/dist/site-auth-maintenance-WK3MR2PV.js.map +1 -0
  177. package/dist/telemetry-scheduler.d.ts +14 -0
  178. package/dist/temporal-runtime-logger.d.ts +22 -0
  179. package/dist/turn-task-queue-reader.d.ts +18 -0
  180. package/dist/turn-worker-activity-telemetry.d.ts +47 -0
  181. package/dist/worker-service-lifecycle.d.ts +12 -0
  182. package/dist/workflow-bundle.js +468 -71
  183. package/dist/workflows/activities.d.ts +1 -1
  184. package/dist/workflows/browser-deadline-checkpoint.d.ts +10 -0
  185. package/dist/workflows/sandbox-reaper.d.ts +10 -1
  186. package/dist/workflows/scheduled-tasks.d.ts +4 -0
  187. package/dist/workflows/session-storage.d.ts +2 -0
  188. package/dist/workflows/session.d.ts +36 -0
  189. package/dist/workflows/site-auth-maintenance.d.ts +2 -1
  190. package/dist/workflows/workflow-wake.d.ts +14 -3
  191. package/dist/workflows.d.ts +3 -1
  192. package/package.json +26 -20
  193. package/src/activities/agent-run-admission.ts +1 -82
  194. package/src/activities/agent-turn/admission.ts +175 -24
  195. package/src/activities/agent-turn/agent-build.ts +226 -145
  196. package/src/activities/agent-turn/claim.ts +363 -64
  197. package/src/activities/agent-turn/claude-core.ts +587 -0
  198. package/src/activities/agent-turn/code-search.ts +275 -0
  199. package/src/activities/agent-turn/codex-capacity.ts +30 -757
  200. package/src/activities/agent-turn/codex-core-capacity.ts +190 -0
  201. package/src/activities/agent-turn/codex-core-claim.ts +83 -0
  202. package/src/activities/agent-turn/codex-core-errors.ts +308 -0
  203. package/src/activities/agent-turn/codex-core-failover.ts +73 -0
  204. package/src/activities/agent-turn/codex-core-requests.ts +86 -0
  205. package/src/activities/agent-turn/codex-core-settlement.ts +263 -0
  206. package/src/activities/agent-turn/codex-credit-policy.ts +247 -0
  207. package/src/activities/agent-turn/codex-plan-entitlement.ts +254 -0
  208. package/src/activities/agent-turn/codex-usage-policy.ts +2 -0
  209. package/src/activities/agent-turn/codex.ts +8 -76
  210. package/src/activities/agent-turn/compaction-interruption.ts +56 -0
  211. package/src/activities/agent-turn/compaction-prep.ts +249 -36
  212. package/src/activities/agent-turn/credential-leases.ts +269 -214
  213. package/src/activities/agent-turn/credential-restriction.ts +14 -0
  214. package/src/activities/agent-turn/errors.ts +1134 -82
  215. package/src/activities/agent-turn/failure-settlement.ts +879 -886
  216. package/src/activities/agent-turn/final-reply.ts +46 -0
  217. package/src/activities/agent-turn/finalization-monitor.ts +17 -5
  218. package/src/activities/agent-turn/finalization.ts +107 -88
  219. package/src/activities/agent-turn/github-repository-bindings.ts +3 -1
  220. package/src/activities/agent-turn/governance-model.ts +90 -22
  221. package/src/activities/agent-turn/history-sink.ts +26 -4
  222. package/src/activities/agent-turn/history.ts +25 -5
  223. package/src/activities/agent-turn/input-wait-reply.ts +139 -0
  224. package/src/activities/agent-turn/knowledge-source-tools.ts +2 -0
  225. package/src/activities/agent-turn/meaningful-output.ts +44 -0
  226. package/src/activities/agent-turn/media-artifacts.ts +3 -1
  227. package/src/activities/agent-turn/model-call-admission.ts +94 -0
  228. package/src/activities/agent-turn/model-usage.ts +299 -233
  229. package/src/activities/agent-turn/model-wire-dispatch.ts +27 -0
  230. package/src/activities/agent-turn/optional-repositories.ts +224 -0
  231. package/src/activities/agent-turn/prompt-tool-availability.ts +27 -0
  232. package/src/activities/agent-turn/provider-dispatch-barrier.ts +22 -0
  233. package/src/activities/agent-turn/provider-recovery-copy.ts +76 -0
  234. package/src/activities/agent-turn/provider-recovery-metrics.ts +64 -0
  235. package/src/activities/agent-turn/provider-recovery-policy.ts +25 -0
  236. package/src/activities/agent-turn/quiescence.ts +7 -7
  237. package/src/activities/agent-turn/recovery-warning.ts +6 -8
  238. package/src/activities/agent-turn/refresh-credentials.ts +44 -0
  239. package/src/activities/agent-turn/run-credentials.ts +124 -76
  240. package/src/activities/agent-turn/run.ts +501 -112
  241. package/src/activities/agent-turn/sandbox-establish.ts +152 -75
  242. package/src/activities/agent-turn/sandbox-provision.ts +54 -1
  243. package/src/activities/agent-turn/sandbox-route.ts +4 -1
  244. package/src/activities/agent-turn/sandbox-runtime.ts +319 -11
  245. package/src/activities/agent-turn/secret-resolver.ts +155 -0
  246. package/src/activities/agent-turn/session-title.ts +177 -12
  247. package/src/activities/agent-turn/session-usage-custody.ts +170 -0
  248. package/src/activities/agent-turn/skill-checkout.ts +111 -24
  249. package/src/activities/agent-turn/skill-install.ts +1 -1
  250. package/src/activities/agent-turn/skill-read.ts +374 -28
  251. package/src/activities/agent-turn/skill-save.ts +35 -7
  252. package/src/activities/agent-turn/skill-search.ts +9 -0
  253. package/src/activities/agent-turn/skill-selection.ts +4 -0
  254. package/src/activities/agent-turn/skill-tools.ts +208 -21
  255. package/src/activities/agent-turn/skill-transfer.ts +68 -23
  256. package/src/activities/agent-turn/stream-attempt.ts +599 -110
  257. package/src/activities/agent-turn/subscription-capacity-arming.ts +81 -0
  258. package/src/activities/agent-turn/subscription-core-capacity.ts +439 -0
  259. package/src/activities/agent-turn/subscription-core-failover.ts +229 -0
  260. package/src/activities/agent-turn/subscription-core-placed.ts +37 -0
  261. package/src/activities/agent-turn/subscription-core-requests.ts +271 -0
  262. package/src/activities/agent-turn/subscription-core-settlement.ts +234 -0
  263. package/src/activities/agent-turn/subscription-lease.ts +140 -0
  264. package/src/activities/agent-turn/subscription-serving-capacity.ts +195 -0
  265. package/src/activities/agent-turn/subscription-serving-catalog.ts +71 -0
  266. package/src/activities/agent-turn/subscription-serving-claim.ts +178 -0
  267. package/src/activities/agent-turn/subscription-serving-providers.ts +15 -0
  268. package/src/activities/agent-turn/tool-environment.ts +384 -292
  269. package/src/activities/agent-turn/tool-policy.ts +104 -4
  270. package/src/activities/agent-turn/tool-result-spill.ts +1 -0
  271. package/src/activities/agent-turn/turn-context.ts +124 -8
  272. package/src/activities/agent-turn/web-search.ts +431 -0
  273. package/src/activities/agent-turn/xai-core.ts +301 -0
  274. package/src/activities/automations.ts +81 -6
  275. package/src/activities/browser-deadline-checkpoint.ts +297 -0
  276. package/src/activities/capabilities.ts +53 -213
  277. package/src/activities/codemode-dispatcher.ts +236 -14
  278. package/src/activities/codex-capacity.ts +27 -380
  279. package/src/activities/codex-image-generation.ts +252 -42
  280. package/src/activities/common.ts +12 -0
  281. package/src/activities/connector-attachments.ts +3 -1
  282. package/src/activities/context-compaction.ts +162 -21
  283. package/src/activities/environment.ts +18 -106
  284. package/src/activities/gmail-files.ts +108 -0
  285. package/src/activities/goals.ts +120 -204
  286. package/src/activities/image-generation-operation.ts +1 -1
  287. package/src/activities/incident-telemetry-authority.ts +80 -50
  288. package/src/activities/incident-telemetry-preflight.ts +27 -15
  289. package/src/activities/knowledge-indexing.ts +287 -17
  290. package/src/activities/knowledge-source-sync.ts +10 -0
  291. package/src/activities/mcp-account-routes.ts +2 -124
  292. package/src/activities/model-call-fact-reconciler.ts +112 -0
  293. package/src/activities/open-suffix-resume.ts +122 -88
  294. package/src/activities/parent-wake.ts +89 -21
  295. package/src/activities/programmatic-approvals.ts +26 -0
  296. package/src/activities/requester-unavailable-receipt.ts +14 -0
  297. package/src/activities/retained-screenshots.ts +135 -24
  298. package/src/activities/run-credential-renewal.ts +13 -1
  299. package/src/activities/run-credentials.ts +61 -19
  300. package/src/activities/run-input.ts +116 -9
  301. package/src/activities/sandbox-lease.ts +1615 -73
  302. package/src/activities/scheduled-tasks.ts +758 -141
  303. package/src/activities/session-archive.ts +382 -0
  304. package/src/activities/session-state.ts +219 -10
  305. package/src/activities/session-storage.ts +124 -0
  306. package/src/activities/site-auth-maintenance.ts +5 -2
  307. package/src/activities/subscription-core-claude-waits.ts +49 -0
  308. package/src/activities/subscription-core-codex-quota.ts +46 -0
  309. package/src/activities/subscription-core-codex-waits.ts +488 -0
  310. package/src/activities/subscription-core-image-operation.ts +126 -0
  311. package/src/activities/subscription-core-usage-recheck.ts +146 -0
  312. package/src/activities/subscription-core-wait-bindings.ts +12 -0
  313. package/src/activities/subscription-core-xai-quota.ts +85 -0
  314. package/src/activities/subscription-core-xai-waits.ts +48 -0
  315. package/src/activities/types.ts +85 -9
  316. package/src/activities/video-generation-admission.ts +1 -36
  317. package/src/activities/video-generation-credential.ts +38 -56
  318. package/src/activities/video-generation-reconciliation.ts +150 -131
  319. package/src/activities/video-generation-subscription-core.ts +180 -0
  320. package/src/activities/workspace-capture.ts +13 -2
  321. package/src/activities/workspace-credential-provider.ts +302 -0
  322. package/src/activities/xai-auth.ts +34 -112
  323. package/src/activities/xai-image-generation.ts +84 -36
  324. package/src/activities-control.ts +8 -0
  325. package/src/activities-turn.ts +5 -0
  326. package/src/activities.ts +17 -0
  327. package/src/activity-services.ts +29 -4
  328. package/src/browser-deadline-checkpoint-contract.ts +5 -0
  329. package/src/editable-artifact-hint-broker.ts +3 -0
  330. package/src/editable-artifact-materializer-service.ts +79 -38
  331. package/src/editable-artifact-materializer-subprocess.ts +47 -2
  332. package/src/editable-artifact-outbox-dispatcher.ts +107 -46
  333. package/src/editable-artifact-outbox-service.ts +55 -25
  334. package/src/github-rest-mcp.ts +1 -1
  335. package/src/host-export-pump.ts +26 -4
  336. package/src/host-export.ts +4 -1
  337. package/src/http.ts +4 -2
  338. package/src/index.ts +222 -282
  339. package/src/internal-maintenance.ts +347 -0
  340. package/src/maintenance-cadence.ts +156 -0
  341. package/src/observability-metrics.ts +1173 -73
  342. package/src/onepassword-vault-provider.ts +11 -0
  343. package/src/personal-github-git-credentials.ts +3 -1
  344. package/src/pr-review-credentials.ts +1 -1
  345. package/src/readiness-checks.ts +113 -0
  346. package/src/retained-process-retry.ts +36 -0
  347. package/src/sandbox-reaper-contract.ts +33 -0
  348. package/src/sandbox-resume.ts +444 -73
  349. package/src/sandbox-routing.ts +230 -75
  350. package/src/session-event-fanout.ts +23 -0
  351. package/src/telemetry-scheduler.ts +53 -0
  352. package/src/temporal-runtime-logger.ts +44 -0
  353. package/src/turn-task-queue-reader.ts +110 -0
  354. package/src/turn-worker-activity-telemetry.ts +209 -0
  355. package/src/worker-service-lifecycle.ts +54 -1
  356. package/src/workflows/activities.ts +16 -1
  357. package/src/workflows/browser-deadline-checkpoint.ts +55 -0
  358. package/src/workflows/file-upload-reaper.ts +2 -0
  359. package/src/workflows/sandbox-reaper.ts +88 -0
  360. package/src/workflows/scheduled-tasks.ts +26 -13
  361. package/src/workflows/session-storage.ts +19 -0
  362. package/src/workflows/session.ts +238 -33
  363. package/src/workflows/site-auth-maintenance.ts +2 -1
  364. package/src/workflows/workflow-wake.ts +26 -4
  365. package/src/workflows.ts +7 -0
  366. package/dist/activities/agent-turn/xai-capacity.d.ts +0 -2
  367. package/dist/activities/codex-fleet-shadow.d.ts +0 -74
  368. package/dist/activities/codex-rotation.d.ts +0 -200
  369. package/dist/activities/xai-quota.d.ts +0 -3
  370. package/dist/activities-control-QA6OLRQY.js.map +0 -1
  371. package/dist/activities-turn-YVVKCGAK.js.map +0 -1
  372. package/dist/chunk-GDCLDKSH.js.map +0 -1
  373. package/dist/chunk-I7HKKMUJ.js.map +0 -1
  374. package/dist/chunk-M3NVMF2V.js.map +0 -1
  375. package/dist/chunk-RAMNYSAW.js.map +0 -1
  376. package/src/activities/agent-turn/xai-capacity.ts +0 -257
  377. package/src/activities/codex-fleet-shadow.ts +0 -306
  378. package/src/activities/codex-rotation.ts +0 -685
  379. package/src/activities/xai-quota.ts +0 -35
@@ -1,8 +1,21 @@
1
1
  import {
2
+ agentToolCallOutcome,
3
+ agentToolMetricFamily,
4
+ interactionInterventionMetricObserver,
5
+ measureMcpPhase,
6
+ recordAgentToolCall,
7
+ withMcpCallIdentity,
8
+ type AgentToolMetricFamily,
9
+ } from "@opengeni/observability";
10
+ import {
11
+ appendSessionHistoryItems,
12
+ sessionTurnFinalReplyFacts,
13
+ sessionTurnHasFinalReplyNudge,
2
14
  getSessionEvent,
3
15
  getHumanInputResumeForEvent,
4
16
  getSessionHumanInputRequest,
5
17
  getWorkspace,
18
+ listTurnCodemodeApprovals,
6
19
  recordUsageEvent,
7
20
  registerPendingSessionToolCall,
8
21
  recordPendingSessionToolCallResult,
@@ -14,20 +27,28 @@ import {
14
27
  updateSessionTitleWithEvent,
15
28
  } from "@opengeni/db";
16
29
  import { publishDurableSessionEvents } from "@opengeni/events";
30
+ import { recordProviderRecoveryOutcome } from "./provider-recovery-metrics";
17
31
  import {
32
+ AssistantMessagePhaseTracker,
18
33
  normalizeModelCallUsage,
19
34
  normalizeSdkEvent,
20
35
  withMcpToolDisplayMetadata,
36
+ withToolCallFamily,
21
37
  extractOpenSuffixFromRunState,
22
38
  assertOpenSuffixResumable,
23
39
  interruptionKindForCallItem,
24
40
  releaseMcpResultCustomDataFromSdkEvent,
25
41
  findCompactionNeededError,
42
+ findAnthropicRequestSizeError,
43
+ type AnthropicRequestSizeError,
44
+ ANTHROPIC_REQUEST_MAX_BYTES,
26
45
  compactionProviderRejection,
27
46
  withRunCredentialsSession,
28
47
  runOwnedSandboxSetup,
48
+ modelResponseHasVisibleText,
29
49
  type SandboxFileDownload,
30
50
  type OpenGeniRuntime,
51
+ type GeneratedSessionTitle,
31
52
  type HistoryProviderApi,
32
53
  type GitCredentialTokenWriterSession,
33
54
  type NormalizedRunCredentialMaterial,
@@ -36,6 +57,10 @@ import {
36
57
  } from "@opengeni/runtime";
37
58
  import { serviceTierForLatencyMode, type Settings } from "@opengeni/config";
38
59
  import { maybeCompactContext, settleFailedContextCompactionLandmark } from "../context-compaction";
60
+ import {
61
+ isContextCompactionInterruption,
62
+ settleContextCompactionInterruption,
63
+ } from "./compaction-interruption";
39
64
  import { TurnAttemptFencedError } from "../turn-attempt-fenced";
40
65
  import { type MintedRunGitCredentials } from "../environment";
41
66
  import { withFirstPartyTools } from "../goals";
@@ -61,7 +86,7 @@ import {
61
86
  import {
62
87
  compactRetainedScreenshotHistory,
63
88
  sdkEventContainsInlineImage,
64
- retainComputerScreenshot,
89
+ retainSessionScreenshot,
65
90
  typedScreenshotFromSdkEvent,
66
91
  unavailableRetainedSessionImage,
67
92
  } from "../retained-screenshots";
@@ -70,6 +95,8 @@ import {
70
95
  generatedImageFromSdkEvent,
71
96
  isCompletedGeneratedImageSdkEvent,
72
97
  } from "../generated-images";
98
+ import { programmaticApproval, programmaticContinuationNote } from "../programmatic-approvals";
99
+ import { CodemodeAttemptDispatcher } from "../codemode-dispatcher";
73
100
  import { ToolResultSpill } from "./tool-result-spill";
74
101
  import { ownedTurnSandboxForAgent } from "./turn-sandbox-access";
75
102
  import { createTurnCredentialLeases } from "./credential-leases";
@@ -82,24 +109,30 @@ import {
82
109
  import {
83
110
  OPEN_SUFFIX_RUN_STATE_BLOB,
84
111
  resolveWorkspaceAgentHumanInputEnabled,
112
+ withPublicApprovalFields,
85
113
  type RetainedArtifactMetadata,
86
114
  type SessionEvent,
115
+ type SessionTurn,
87
116
  } from "@opengeni/contracts";
88
117
  import { createModelCheckpointMemoryCollector } from "../../model-checkpoint-memory-collector";
118
+ import { createModelCallAdmission } from "./model-call-admission";
119
+ import { isCoreSourceDisconnectedError } from "./subscription-core-capacity";
120
+ import { revalidatePlacedSubscriptionCore } from "./subscription-core-placed";
89
121
 
90
122
  import {
91
123
  assertWorkspaceHumanInputAllowed,
92
124
  stableHumanInputRequestId,
93
125
  stableInteractionInterventionId,
94
126
  stableInteractionInterventionOperationId,
95
- BudgetExhaustedError,
96
- ensureRunAllowed,
127
+ ensureRunAllowedBetweenModelCalls,
97
128
  } from "./admission";
98
129
  import {
130
+ assertProviderOverloadRecoveryActive,
99
131
  compactionFailureReason,
100
132
  safeErrorDiagnostic,
101
133
  compactionFailureReasonFromError,
102
134
  compactionFailureTurnEventPayload,
135
+ modelPreparationFailureEventPayload,
103
136
  isCompactionSummaryFailure,
104
137
  PostCompactionContinuationEmptyError,
105
138
  shouldRecoverCompactionProviderFailure,
@@ -108,10 +141,11 @@ import {
108
141
  import {
109
142
  pendingToolCallFromSdkEvent,
110
143
  toolCallProducesRetainableSessionImage,
144
+ retainableBrowserScreenshotToolCall,
111
145
  completedToolCallFromSdkEvent,
112
146
  } from "./history";
113
- import { checkpointHistoryBeforeProviderDispatch } from "./provider-dispatch-barrier";
114
147
  import {
148
+ aggregateCreditPolicyRevision,
115
149
  modelUsageSourceKey,
116
150
  recordCompletedModelCallBeforeOwnershipFences,
117
151
  TurnEventPublisher,
@@ -124,15 +158,27 @@ import {
124
158
  emitModelCallUsage,
125
159
  recordModelUsageAndDebitCredits,
126
160
  recordAuthoritativeModelCallFact,
161
+ recordAuthoritativeModelUsageMetrics,
127
162
  } from "./model-usage";
128
- import { startParallelSessionTitleGeneration } from "./session-title";
163
+ import { flushHeldSessionUsageReceipts } from "./session-usage-custody";
164
+ import {
165
+ sessionTitleGenerationOptions,
166
+ sessionTitleRoute,
167
+ startParallelSessionTitleGeneration,
168
+ } from "./session-title";
129
169
  import {
130
170
  assertAgentStreamNotCancelled,
131
171
  assertSuccessfulAgentStreamCompletion,
132
172
  requireAgentStreamFinalOutput,
133
173
  } from "./quiescence";
174
+ import {
175
+ inputWaitReply,
176
+ inputWaitReplyRefusal,
177
+ latestDurableTurnMessageText,
178
+ } from "./input-wait-reply";
134
179
  import { waitForTurnOperation } from "./sandbox-provision";
135
180
  import { createSharedRigSetupCoordinator } from "./sandbox-shared-preparation";
181
+ import { finalReplyNudge, needsFinalReply } from "./final-reply";
136
182
 
137
183
  import type { CompactionSummarizer } from "../context-compaction";
138
184
  import type { TurnExecutionPolicyV1 } from "@opengeni/contracts";
@@ -147,6 +193,8 @@ import type {
147
193
  SandboxRuntimeState,
148
194
  TurnControlState,
149
195
  } from "./turn-context";
196
+ import { sdkResponseHasMeaningfulOutput } from "./meaningful-output";
197
+ import { subscriptionCoreTurnOf } from "./turn-context";
150
198
 
151
199
  export type TurnStreamAttemptDeps = {
152
200
  input: RunAgentTurnInput;
@@ -173,11 +221,12 @@ export type TurnStreamAttemptDeps = {
173
221
  providerTurn: ProviderTurnState;
174
222
  leases: ReturnType<typeof createTurnCredentialLeases>;
175
223
  historySink: ReturnType<typeof createTurnHistorySink>;
224
+ checkpointBeforeProviderDispatch: () => Promise<void>;
176
225
  media: ReturnType<typeof createTurnMediaArtifacts>;
177
226
  toolResultSpill: ToolResultSpill;
178
227
  claimedResult: ClaimedResult;
179
228
  flushRuntimeBatcher: () => Promise<void>;
180
- finalizeTurnOpStreamOps: () => Promise<void>;
229
+ finalizeTurnOpStreamOps: (toolCallIds?: readonly string[]) => Promise<void>;
181
230
  runWorkspaceMutationForSandbox: <T>(
182
231
  sandbox: ResumedTurnSandbox,
183
232
  operation: string,
@@ -225,7 +274,13 @@ export type TurnStreamAttemptDeps = {
225
274
  activeSandboxBackend: Settings["sandboxBackend"] | undefined;
226
275
  groupBoxBackend: Settings["sandboxBackend"];
227
276
  turnExecutionPolicy: TurnExecutionPolicyV1;
228
- turn: { executionGeneration: number; model: string; source?: string };
277
+ turn: Pick<SessionTurn, "initiator" | "initiatorContext"> & {
278
+ initiatingHumanSubjectId: string | null;
279
+ id: string;
280
+ executionGeneration: number;
281
+ model: string;
282
+ source?: string;
283
+ };
229
284
  trigger: NonNullable<Awaited<ReturnType<typeof getSessionEvent>>>;
230
285
  humanInputResume: Awaited<ReturnType<typeof getHumanInputResumeForEvent>>;
231
286
  attachPendingUpdatesAfterOpenSuffix: () => Promise<boolean>;
@@ -291,6 +346,7 @@ export async function runTurnStreamAttempt(
291
346
  providerTurn,
292
347
  leases,
293
348
  historySink,
349
+ checkpointBeforeProviderDispatch,
294
350
  media,
295
351
  toolResultSpill,
296
352
  claimedResult,
@@ -361,15 +417,24 @@ export async function runTurnStreamAttempt(
361
417
  );
362
418
  let parallelSessionTitle: ReturnType<typeof startParallelSessionTitleGeneration> | null = null;
363
419
  let parallelSessionTitleFinished = false;
364
- const finishParallelSessionTitle = async (): Promise<void> => {
365
- if (parallelSessionTitleFinished) return;
366
- parallelSessionTitleFinished = true;
367
- const generated = await parallelSessionTitle?.finish();
368
- if (!generated) return;
420
+ let creditPolicyRevision: number | undefined;
421
+ let titleCreditPolicyRevision: number | undefined;
422
+ let titleUsageModelId: string | null = null;
423
+ const persistParallelSessionTitle = async (
424
+ generated: GeneratedSessionTitle | null,
425
+ ): Promise<void> => {
426
+ if (!generated) {
427
+ // A cancelled title may have received a complete provider response before
428
+ // its result was discarded. Checkpoint only that positive observation;
429
+ // pending or unknown requests keep their replay fence.
430
+ await subscriptionCoreTurnOf(providerTurn)?.titleRequests?.checkpoint();
431
+ return;
432
+ }
369
433
 
370
434
  if (generated.usage) {
371
435
  await processSessionTitleModelUsageEvent({
372
436
  usage: generated.usage,
437
+ creditPolicyRevision: titleCreditPolicyRevision,
373
438
  state: sessionTitleUsageState,
374
439
  dispatchId: modelUsageDispatchId,
375
440
  settings,
@@ -383,19 +448,23 @@ export async function runTurnStreamAttempt(
383
448
  turnAttemptId: input.attemptId,
384
449
  provider: resolvedModel?.provider.id ?? settings.openaiProvider,
385
450
  providerApi: resolvedModel?.provider.api ?? "responses",
386
- model: resolvedModel?.configured.id ?? turn.model,
451
+ model: titleUsageModelId ?? resolvedModel?.configured.id ?? turn.model,
387
452
  externallyBilled: billingState.isExternallyBilledTurn,
388
453
  chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
389
454
  countsTowardTokenCap: billingState.countsTowardTokenCap,
390
455
  servingCredentialId: providerTurn.effectiveCodexCredentialId,
391
456
  priorSessionCredentialId: providerTurn.priorSessionCodexCredentialId,
457
+ subscriptionConnectionId: subscriptionCoreTurnOf(providerTurn)?.connectionId ?? null,
392
458
  emittedSourceKeys: emittedModelUsageSourceKeys,
393
459
  renewLease: () => leases.renewServing("model_usage"),
394
460
  leaseLost: leases.servingLost,
395
461
  leaseLostMessage: "Provider credential lease expired during session title generation",
396
462
  });
397
463
  }
398
- if (!generated.title) return;
464
+ if (!generated.title) {
465
+ await subscriptionCoreTurnOf(providerTurn)?.titleRequests?.checkpoint();
466
+ return;
467
+ }
399
468
 
400
469
  try {
401
470
  const result = await updateSessionTitleWithEvent(db, {
@@ -404,6 +473,7 @@ export async function runTurnStreamAttempt(
404
473
  title: generated.title,
405
474
  source: "agent",
406
475
  });
476
+ await subscriptionCoreTurnOf(providerTurn)?.titleRequests?.checkpoint();
407
477
  if (result.events.length > 0) {
408
478
  await publishDurableSessionEvents(bus, input.workspaceId, input.sessionId, result.events);
409
479
  }
@@ -416,12 +486,19 @@ export async function runTurnStreamAttempt(
416
486
  });
417
487
  }
418
488
  };
489
+ const finishParallelSessionTitle = async (): Promise<void> => {
490
+ if (parallelSessionTitleFinished) return;
491
+ parallelSessionTitleFinished = true;
492
+ await parallelSessionTitle?.finish();
493
+ };
419
494
  const cancelParallelSessionTitle = async (): Promise<void> => {
420
495
  if (parallelSessionTitleFinished) return;
421
496
  parallelSessionTitleFinished = true;
422
497
  await parallelSessionTitle?.cancel();
423
498
  };
424
499
  let runInput: Awaited<ReturnType<typeof turnInput>>["input"] | null = null;
500
+ let codemodeContinuationNote: string | undefined;
501
+ let programmaticApprovalAcknowledged = false;
425
502
  const prepareRunAttemptInput = async () => {
426
503
  const historyPreparationStartedAt = performance.now();
427
504
  let historyPreparationOutcome: "completed" | "failed" = "completed";
@@ -434,11 +511,18 @@ export async function runTurnStreamAttempt(
434
511
  subjectId: fileAuthoritySubjectId,
435
512
  },
436
513
  recovering: turn.executionGeneration > 1,
514
+ programmaticApprovalAcknowledged,
515
+ ...(codemodeContinuationNote ? { codemodeContinuationNote } : {}),
437
516
  ...(unavailableSandboxFilesNote ? { unavailableSandboxFilesNote } : {}),
438
517
  ...(runCredentialsNote ? { runCredentialsNote } : {}),
439
518
  ...(mcpAvailabilityNote ? { mcpAvailabilityNote } : {}),
440
519
  ...(knowledgeSourcePreparationNote ? { knowledgeSourcePreparationNote } : {}),
441
520
  providerApi,
521
+ ...(settings.subscriptionCrossProviderFailoverEnabled &&
522
+ compactionModeOptions.codexCompactionMode === "portable" &&
523
+ turnExecutionPolicy.credentialRestriction === undefined
524
+ ? { servingProviderId: turnExecutionPolicy.providerId }
525
+ : {}),
442
526
  projectCanonicalHistory: generatedImageHistoryProjector,
443
527
  materializeModelHistory: media.materializeScreenshotHistory,
444
528
  projectModelHistory: modelHistoryProjector,
@@ -518,6 +602,7 @@ export async function runTurnStreamAttempt(
518
602
  const forceContextCompaction = async (
519
603
  triggerLabel: "overflow" | "proactive" | "operator",
520
604
  recoverySignalTokens: number | null,
605
+ requestSizeError?: AnthropicRequestSizeError | null,
521
606
  ) => {
522
607
  const outcome = await waitForTurnOperation(
523
608
  maybeCompactContext(
@@ -542,6 +627,19 @@ export async function runTurnStreamAttempt(
542
627
  force: true,
543
628
  ...(triggerLabel === "operator" ? { clearRequestedCompaction: true } : {}),
544
629
  trigger: triggerLabel,
630
+ ...(requestSizeError
631
+ ? {
632
+ requestSizeRecovery: {
633
+ // A proxy may reject below the documented API maximum. The
634
+ // checkpoint uses at most half the rejected payload size.
635
+ maxBytes: Math.min(
636
+ ANTHROPIC_REQUEST_MAX_BYTES,
637
+ Math.floor(requestSizeError.requestSize.requestBytes / 2),
638
+ ),
639
+ size: requestSizeError.requestSize,
640
+ },
641
+ }
642
+ : {}),
545
643
  materializeHistory: media.materializeScreenshotHistory,
546
644
  projectModelInput: compactionModelHistoryProjector,
547
645
  ...compactionModeOptions,
@@ -564,15 +662,51 @@ export async function runTurnStreamAttempt(
564
662
  // calling runStreamAttempt again; resetting this state there would reuse
565
663
  // the first no-response-ID fallback key and suppress a real model call.
566
664
  const modelResponseState = createModelResponseEventState(claimedModelUsageSourceKeys);
665
+ // Text of the newest assistant message any stream of this activity completed
666
+ // durably: the reply a wait-ended human turn records on turn.completed.
667
+ let latestAssistantMessageText: string | null = null;
668
+ // Waits refused because they would leave a person's message unanswered.
669
+ let inputWaitReplyRefusals = 0;
567
670
  let workerPreparationTotalRecorded = false;
671
+ let toolsExecuted = false;
672
+ let finalReplyNudged = false;
673
+ const revalidateModelCallAdmission = async () => {
674
+ await historySink.reconcileConversationTruth({ requireDurable: true });
675
+ await revalidatePlacedSubscriptionCore(db, providerTurn);
676
+ creditPolicyRevision = await ensureRunAllowedBetweenModelCalls({
677
+ modelId: resolvedModel?.configured.id ?? turn.model,
678
+ settings,
679
+ db,
680
+ accountId: input.accountId,
681
+ workspaceId: input.workspaceId,
682
+ isExternallyBilledTurn: billingState.isExternallyBilledTurn,
683
+ entitlements,
684
+ chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
685
+ countsTowardTokenCap: billingState.countsTowardTokenCap,
686
+ initiatingHumanSubjectId: turn.initiatingHumanSubjectId,
687
+ sessionId: input.sessionId,
688
+ });
689
+ };
568
690
  const runStreamAttempt = async (options: {
569
691
  requireTerminalModelResponse: boolean;
570
- }): Promise<RunAgentTurnResult> => {
692
+ }): Promise<RunAgentTurnResult | "reply_nudge"> => {
571
693
  if (!runInput) {
572
694
  throw new Error("Run input was not prepared");
573
695
  }
574
- const responseCountBeforeStream = modelResponseState.responseCount;
696
+ // The previous stream was persisted before compaction; the sink is now
697
+ // seeded from its durable replacement. Do not reconcile the old prefix
698
+ // against that replacement while checking the next stream's admission.
575
699
  eventing.stream = undefined;
700
+ // Compaction commits its paid usage and replacement history before this
701
+ // boundary, both during preparation and in-activity recovery. Revalidate
702
+ // the accepted turn's frozen human before another stream can dispatch.
703
+ if (options.requireTerminalModelResponse) await revalidateModelCallAdmission();
704
+ const modelCallAdmission = createModelCallAdmission({
705
+ signal: runtimeCancellationSignal,
706
+ admit: revalidateModelCallAdmission,
707
+ });
708
+ const responseCreditPolicyRevisions = new Set<number | undefined>();
709
+ const responseCountBeforeStream = modelResponseState.responseCount;
576
710
  eventing.batcher = null;
577
711
  // The SDK emits every processed call item for one model response before
578
712
  // it emits any result for that response. Keep that response-local batch
@@ -580,8 +714,19 @@ export async function runTurnStreamAttempt(
580
714
  // calls. Durable recovery remains call/result based and needs no batch
581
715
  // schema or compatibility state.
582
716
  let currentToolBatchCallIds = new Set<string>();
717
+ // Content-free per-call family + start time for opengeni_agent_tool_calls_total.
718
+ const toolCallMetricStarts = new Map<
719
+ string,
720
+ { family: AgentToolMetricFamily; startedAtMs: number }
721
+ >();
583
722
  let currentToolBatchCompletedCallIds = new Set<string>();
584
723
  let streamSawPerResponseUsage = false;
724
+ // Deltas learn the phase a provider declares when it announces a message;
725
+ // undeclared messages the SDK runs past (same response asks for tools)
726
+ // are commentary. Every SDK event of this stream is normalized once.
727
+ const messagePhases = new AssistantMessagePhaseTracker();
728
+ // Text of the newest assistant message this stream completed durably.
729
+ let latestStreamedAssistantText: string | null = null;
585
730
  // Actual input tokens of the most recent model response this turn; the
586
731
  // pre-read trigger for the NEXT turn. Persisted at every turn-end path.
587
732
  throwIfWorkerShuttingDown();
@@ -590,10 +735,20 @@ export async function runTurnStreamAttempt(
590
735
  const providerPublishesNativeRequestEvents =
591
736
  resolvedModel?.provider.kind === "codex-subscription" ||
592
737
  resolvedModel?.provider.kind === "xai-subscription";
738
+ // Bounded provider label for the streaming SLIs — the resolved registry
739
+ // provider id (or the built-in OpenAI/Azure provider), never a raw
740
+ // user-supplied model string. Created before the stream starts so the
741
+ // first request's model entry and provider dispatch are observed.
742
+ const streamTiming = new StreamTimingMetrics(observability, {
743
+ provider: streamProvider,
744
+ });
745
+ // Native Codex/SuperGrok transports report their literal dispatch through
746
+ // the shared turn context; generic transports use the wire-dispatch clock.
747
+ eventing.providerDispatchObserver = () => streamTiming.onProviderDispatch();
593
748
  let fallbackProviderRequestStartedAt: number | null = null;
594
749
  let fallbackProviderRequestLifecycleStartedAt: number | null = null;
595
750
  const recordFallbackProviderDispatchAtWire = async (): Promise<void> => {
596
- await checkpointHistoryBeforeProviderDispatch(historySink);
751
+ await checkpointBeforeProviderDispatch();
597
752
  if (
598
753
  providerPublishesNativeRequestEvents ||
599
754
  eventing.firstModelRequestPreparationRecorded ||
@@ -653,6 +808,13 @@ export async function runTurnStreamAttempt(
653
808
  count: turnTools.length,
654
809
  });
655
810
  }
811
+ // The required audit itself may have consumed the remaining window.
812
+ // This awaited observer still owns the literal pre-fetch boundary.
813
+ assertProviderOverloadRecoveryActive({
814
+ failureCode: attempt.providerRecoveryPolicyCode,
815
+ providerRecoveryCount: attempt.providerRecoveryCount,
816
+ recoveryStartedAt: attempt.providerRecoveryStartedAt,
817
+ });
656
818
  };
657
819
  const ownedEstablished = ownedTurnSandboxForAgent(sandboxState);
658
820
  const runStreamOnce = async (): ReturnType<OpenGeniRuntime["runStream"]> => {
@@ -782,6 +944,14 @@ export async function runTurnStreamAttempt(
782
944
  }
783
945
  attempt.modelRequestStarted = true;
784
946
  return await runtime.runStream(agent, runInput!, eventing.modelRunSettings, {
947
+ beforeModelRequest: () => {
948
+ streamTiming.onModelRequestEntry();
949
+ return modelCallAdmission.beforeModelRequest();
950
+ },
951
+ onModelResponse: (event) => {
952
+ responseCreditPolicyRevisions.add(creditPolicyRevision);
953
+ return modelCallAdmission.onModelResponse(event);
954
+ },
785
955
  signal: runtimeCancellationSignal,
786
956
  sandboxEnvironment,
787
957
  onModelVisibleContext: async (snapshot) => {
@@ -888,6 +1058,13 @@ export async function runTurnStreamAttempt(
888
1058
  ...(!providerPublishesNativeRequestEvents
889
1059
  ? {
890
1060
  onModelTransportStarted: recordFallbackProviderDispatchAtWire,
1061
+ onModelTransportDispatched: (clock) => {
1062
+ streamTiming.onProviderDispatch();
1063
+ eventing.initialModelWireDispatch.record(
1064
+ { provider: streamProvider, dispatchId },
1065
+ clock,
1066
+ );
1067
+ },
891
1068
  }
892
1069
  : {}),
893
1070
  ...(eventing.toolCancellationFenceRef.current
@@ -917,13 +1094,36 @@ export async function runTurnStreamAttempt(
917
1094
  if (leases.xai.lost) {
918
1095
  throw new Error("xAI credential lease expired before the model run");
919
1096
  }
920
- eventing.stream = await withProviderRequestContext(runStreamOnce);
921
- // Bounded provider label for the streaming SLIs — the resolved registry
922
- // provider id (or the built-in OpenAI/Azure provider), never a raw
923
- // user-supplied model string.
924
- const streamTiming = new StreamTimingMetrics(observability, {
925
- provider: streamProvider,
926
- });
1097
+ try {
1098
+ eventing.stream = await withProviderRequestContext(runStreamOnce);
1099
+ const stream = eventing.stream;
1100
+ eventing.inputWaitReplyGuard = async () => {
1101
+ const refusal = await inputWaitReplyRefusal({
1102
+ turn,
1103
+ refusalsSoFar: inputWaitReplyRefusals,
1104
+ hasVisibleReply: async () =>
1105
+ Boolean(latestAssistantMessageText?.trim()) ||
1106
+ modelResponseHasVisibleText(
1107
+ (stream.state as { _lastTurnResponse?: unknown })._lastTurnResponse,
1108
+ ) ||
1109
+ Boolean(
1110
+ (
1111
+ await latestDurableTurnMessageText(db, {
1112
+ workspaceId: input.workspaceId,
1113
+ sessionId: input.sessionId,
1114
+ turnId: turn.id,
1115
+ })
1116
+ )?.trim(),
1117
+ ),
1118
+ });
1119
+ if (refusal) inputWaitReplyRefusals += 1;
1120
+ return refusal;
1121
+ };
1122
+ } catch (error) {
1123
+ modelCallAdmission.fail(error);
1124
+ modelCallAdmission.close();
1125
+ throw error;
1126
+ }
927
1127
  eventing.batcher = createRuntimeBatcher(
928
1128
  async (events) => {
929
1129
  await eventing.publish!(events);
@@ -963,6 +1163,7 @@ export async function runTurnStreamAttempt(
963
1163
  attemptId: input.attemptId,
964
1164
  dispatchId,
965
1165
  executionGeneration: attempt.executionGeneration,
1166
+ ...eventing.initialModelWireDispatch.payload({ provider: streamProvider, dispatchId }),
966
1167
  },
967
1168
  },
968
1169
  ]);
@@ -980,6 +1181,7 @@ export async function runTurnStreamAttempt(
980
1181
  }
981
1182
  await settleFallbackProviderFirstByte();
982
1183
  let stableToolCallIdsToClear: string[] | null = null;
1184
+ let newlyDurableToolCallId: string | null = null;
983
1185
  let completedCurrentToolBatch = false;
984
1186
  let retainedScreenshotMetadata: RetainedArtifactMetadata | null = null;
985
1187
  let normalizedSdkEvents: ReturnType<typeof normalizeSdkEvent> | null = null;
@@ -993,6 +1195,7 @@ export async function runTurnStreamAttempt(
993
1195
  ? await media.retainNativeGeneratedImage(generatedImage)
994
1196
  : null;
995
1197
  const responseResult = await processModelResponseTerminalEvent({
1198
+ creditPolicyRevision,
996
1199
  event: next.value,
997
1200
  state: modelResponseState,
998
1201
  dispatchId: modelUsageDispatchId,
@@ -1007,7 +1210,7 @@ export async function runTurnStreamAttempt(
1007
1210
  turnAttemptId: input.attemptId,
1008
1211
  provider: resolvedModel?.provider.id ?? settings.openaiProvider,
1009
1212
  providerApi: resolvedModel?.provider.api ?? "responses",
1010
- model: turn.model,
1213
+ model: turnExecutionPolicy.productModelId,
1011
1214
  latencyMode: turnExecutionPolicy.latencyMode,
1012
1215
  metricProvider: streamProvider,
1013
1216
  externallyBilled: billingState.isExternallyBilledTurn,
@@ -1015,6 +1218,7 @@ export async function runTurnStreamAttempt(
1015
1218
  countsTowardTokenCap: billingState.countsTowardTokenCap,
1016
1219
  servingCredentialId: providerTurn.effectiveCodexCredentialId,
1017
1220
  priorSessionCredentialId: providerTurn.priorSessionCodexCredentialId,
1221
+ subscriptionConnectionId: subscriptionCoreTurnOf(providerTurn)?.connectionId ?? null,
1018
1222
  emittedSourceKeys: emittedModelUsageSourceKeys,
1019
1223
  renewLease: () => leases.renewServing("model_usage"),
1020
1224
  leaseLost: leases.servingLost,
@@ -1025,7 +1229,7 @@ export async function runTurnStreamAttempt(
1025
1229
  assertModelResponseLatencyMode({
1026
1230
  event: next.value,
1027
1231
  requested: turnExecutionPolicy.latencyMode,
1028
- model: turn.model,
1232
+ model: turnExecutionPolicy.productModelId,
1029
1233
  ...(resolvedModel?.provider.id ? { providerId: resolvedModel.provider.id } : {}),
1030
1234
  });
1031
1235
  if (responseResult.status === "processed") {
@@ -1049,11 +1253,30 @@ export async function runTurnStreamAttempt(
1049
1253
  attemptId: input.attemptId,
1050
1254
  dispatchId,
1051
1255
  executionGeneration: attempt.executionGeneration,
1256
+ ...eventing.initialModelWireDispatch.payload({
1257
+ provider: streamProvider,
1258
+ dispatchId,
1259
+ }),
1260
+ // A core-placed turn reports real work for the shared
1261
+ // capacity breaker (providers with their own request events
1262
+ // set it there).
1263
+ ...(subscriptionCoreTurnOf(providerTurn)
1264
+ ? { meaningfulOutput: sdkResponseHasMeaningfulOutput(next.value) }
1265
+ : {}),
1052
1266
  },
1053
1267
  },
1054
1268
  ]);
1055
1269
  attempt.providerRecoveryCount = 0;
1056
1270
  }
1271
+ if (attempt.providerRecoveryObservation) {
1272
+ recordProviderRecoveryOutcome(observability, {
1273
+ route: attempt.modelMetricRoute,
1274
+ cause: attempt.providerRecoveryObservation.cause,
1275
+ outcome: "recovered",
1276
+ elapsedMs: Date.now() - attempt.providerRecoveryObservation.startedAt,
1277
+ });
1278
+ attempt.providerRecoveryObservation = undefined;
1279
+ }
1057
1280
  const rawStreamHistory = (eventing.stream.state as { history?: unknown[] }).history;
1058
1281
  if (Array.isArray(rawStreamHistory)) {
1059
1282
  // The completed image item is normally retained from its own
@@ -1070,35 +1293,10 @@ export async function runTurnStreamAttempt(
1070
1293
  await historySink.reconcileConversationTruth();
1071
1294
  turnLifecycleMetricsFor(observability).progress({ attemptId: input.attemptId });
1072
1295
  modelCheckpointMemoryCollector.schedule(observability);
1073
- try {
1074
- await ensureRunAllowed(
1075
- settings,
1076
- db,
1077
- input.accountId,
1078
- input.workspaceId,
1079
- billingState.isExternallyBilledTurn,
1080
- entitlements,
1081
- billingState.chargesOpenGeniCredits,
1082
- billingState.countsTowardTokenCap,
1083
- );
1084
- } catch (limitError) {
1085
- // Capture the run state at the boundary so the budget valve in
1086
- // the outer catch can end this segment gracefully with full
1087
- // conversation context preserved for the post-top-up resume.
1088
- let serializedRunState: string | null = null;
1089
- try {
1090
- serializedRunState = media.compactMediaRunState(
1091
- String(eventing.stream.state.toString()),
1092
- );
1093
- } catch {
1094
- serializedRunState = null;
1095
- }
1096
- throw new BudgetExhaustedError(
1097
- limitError instanceof Error ? limitError.message : String(limitError),
1098
- serializedRunState,
1099
- );
1100
- }
1101
1296
  }
1297
+ // Release after settlement. The producer checks fresh admission before
1298
+ // its next request; a final response keeps the revision it ran under.
1299
+ modelCallAdmission.settle(next.value);
1102
1300
  const durableSdkEvent = generatedImageReceipt
1103
1301
  ? compactGeneratedImageSdkEvent(next.value, generatedImageReceipt)
1104
1302
  : next.value;
@@ -1125,10 +1323,17 @@ export async function runTurnStreamAttempt(
1125
1323
  currentToolBatchCallIds.add(pendingToolCall.callId);
1126
1324
  if (toolCallProducesRetainableSessionImage(pendingToolCall.callName)) {
1127
1325
  media.retainedSessionImageCallIds.add(pendingToolCall.callId);
1326
+ if (retainableBrowserScreenshotToolCall(pendingToolCall.callName)) {
1327
+ media.retainedSessionImageKindsByCallId.set(
1328
+ pendingToolCall.callId,
1329
+ "browser_screenshot",
1330
+ );
1331
+ }
1128
1332
  }
1129
1333
  }
1130
1334
  const completedToolCall = completedToolCallFromSdkEvent(durableSdkEvent);
1131
1335
  if (completedToolCall) {
1336
+ toolsExecuted = true;
1132
1337
  retainedScreenshotMetadata =
1133
1338
  media.retainedScreenshotReceiptsByCallId.get(completedToolCall.callId) ?? null;
1134
1339
  const typedScreenshot = retainedScreenshotMetadata
@@ -1147,6 +1352,7 @@ export async function runTurnStreamAttempt(
1147
1352
  toolCallId: completedToolCall.callId,
1148
1353
  toolOutputId: completedToolCall.callId,
1149
1354
  reason: "unsupported",
1355
+ kind: media.retainedSessionImageKindsByCallId.get(completedToolCall.callId),
1150
1356
  });
1151
1357
  media.retainedScreenshotReceiptsByCallId.set(
1152
1358
  completedToolCall.callId,
@@ -1169,12 +1375,13 @@ export async function runTurnStreamAttempt(
1169
1375
  toolCallId: typedScreenshot.callId,
1170
1376
  toolOutputId: typedScreenshot.toolOutputId,
1171
1377
  reason: "pending",
1378
+ kind: media.retainedSessionImageKindsByCallId.get(completedToolCall.callId),
1172
1379
  });
1173
1380
  media.retainedScreenshotReceiptsByCallId.set(
1174
1381
  completedToolCall.callId,
1175
1382
  retainedScreenshotMetadata,
1176
1383
  );
1177
- retainedScreenshotMetadata = await retainComputerScreenshot({
1384
+ retainedScreenshotMetadata = await retainSessionScreenshot({
1178
1385
  db,
1179
1386
  objectStorage,
1180
1387
  accountId: input.accountId,
@@ -1183,6 +1390,7 @@ export async function runTurnStreamAttempt(
1183
1390
  turnId: activeTurnId,
1184
1391
  attemptId: input.attemptId,
1185
1392
  output: typedScreenshot,
1393
+ kind: media.retainedSessionImageKindsByCallId.get(completedToolCall.callId),
1186
1394
  });
1187
1395
  media.retainedScreenshotReceiptsByCallId.set(
1188
1396
  completedToolCall.callId,
@@ -1193,6 +1401,7 @@ export async function runTurnStreamAttempt(
1193
1401
  durableSdkEvent as typeof next.value,
1194
1402
  retainedScreenshotMetadata
1195
1403
  ? {
1404
+ messagePhases,
1196
1405
  toolOutputOverride: retainedScreenshotMetadata,
1197
1406
  retainedOutputEvidence: retainedScreenshotMetadata.available
1198
1407
  ? retainedScreenshotMetadata
@@ -1201,7 +1410,7 @@ export async function runTurnStreamAttempt(
1201
1410
  reason: retainedScreenshotMetadata.reason,
1202
1411
  },
1203
1412
  }
1204
- : {},
1413
+ : { messagePhases },
1205
1414
  );
1206
1415
  const normalizedToolOutput = normalizedSdkEvents.find(
1207
1416
  (event) =>
@@ -1249,6 +1458,7 @@ export async function runTurnStreamAttempt(
1249
1458
  "turn attempt ended while recording a tool-call result",
1250
1459
  );
1251
1460
  }
1461
+ if (recorded.recorded) newlyDurableToolCallId = completedToolCall.callId;
1252
1462
  const videoAcceptance = videoGenerationAcceptancesByCallId.get(completedToolCall.callId);
1253
1463
  if (videoAcceptance && startVideoGenerationWorkflow) {
1254
1464
  try {
@@ -1294,6 +1504,7 @@ export async function runTurnStreamAttempt(
1294
1504
  durableSdkEvent as typeof next.value,
1295
1505
  retainedScreenshotMetadata
1296
1506
  ? {
1507
+ messagePhases,
1297
1508
  toolOutputOverride: retainedScreenshotMetadata,
1298
1509
  retainedOutputEvidence: retainedScreenshotMetadata.available
1299
1510
  ? retainedScreenshotMetadata
@@ -1302,16 +1513,62 @@ export async function runTurnStreamAttempt(
1302
1513
  reason: retainedScreenshotMetadata.reason,
1303
1514
  },
1304
1515
  }
1305
- : {},
1516
+ : { messagePhases },
1306
1517
  );
1307
1518
  for (const event of normalized) {
1308
- if (event.type === "agent.toolCall.created")
1309
- event.payload = withMcpToolDisplayMetadata(
1310
- eventing.preparedTools?.mcpServers ?? [],
1311
- event.payload,
1519
+ if (event.type === "agent.toolCall.created") {
1520
+ const preparedServers = eventing.preparedTools?.mcpServers ?? [];
1521
+ // Display metadata and the content-free analytics family are
1522
+ // event-copy enrichments only; the executable call is unchanged.
1523
+ event.payload = withToolCallFamily(
1524
+ preparedServers,
1525
+ runSettings.mcpServers,
1526
+ withMcpToolDisplayMetadata(preparedServers, event.payload),
1312
1527
  );
1528
+ const created = event.payload as {
1529
+ id?: unknown;
1530
+ name?: unknown;
1531
+ toolName?: unknown;
1532
+ toolFamily?: unknown;
1533
+ };
1534
+ if (typeof created.id === "string") {
1535
+ toolCallMetricStarts.set(created.id, {
1536
+ family: agentToolMetricFamily(
1537
+ typeof created.toolFamily === "string" ? created.toolFamily : null,
1538
+ typeof created.name === "string"
1539
+ ? created.name
1540
+ : typeof created.toolName === "string"
1541
+ ? created.toolName
1542
+ : null,
1543
+ ),
1544
+ startedAtMs: performance.now(),
1545
+ });
1546
+ }
1547
+ }
1548
+ if (event.type === "agent.toolCall.output") {
1549
+ const id = (event.payload as { id?: unknown }).id;
1550
+ const started = typeof id === "string" ? toolCallMetricStarts.get(id) : undefined;
1551
+ if (typeof id === "string") toolCallMetricStarts.delete(id);
1552
+ recordAgentToolCall(observability, {
1553
+ family: started?.family ?? "other",
1554
+ outcome: agentToolCallOutcome(event.payload),
1555
+ ...(started ? { durationMs: performance.now() - started.startedAtMs } : {}),
1556
+ });
1557
+ }
1313
1558
  streamTiming.onEvent(event.type);
1314
- await eventing.batcher.push(event);
1559
+ if (event.type === "agent.toolCall.output") {
1560
+ const batcher = eventing.batcher;
1561
+ await withMcpCallIdentity((event.payload as { id: string }).id, () =>
1562
+ measureMcpPhase("event_persistence", () => batcher.push(event)),
1563
+ );
1564
+ } else {
1565
+ await eventing.batcher.push(event);
1566
+ }
1567
+ if (event.type === "agent.message.completed") {
1568
+ // Completed messages are structural: push returns once durable.
1569
+ latestStreamedAssistantText = (event.payload as { text: string }).text;
1570
+ latestAssistantMessageText = latestStreamedAssistantText;
1571
+ }
1315
1572
  }
1316
1573
  // Structural tool-output events await their durable append before
1317
1574
  // push returns. The complete result is now retained in the event
@@ -1339,10 +1596,20 @@ export async function runTurnStreamAttempt(
1339
1596
  }
1340
1597
  for (const callId of stableToolCallIdsToClear) {
1341
1598
  media.retainedSessionImageCallIds.delete(callId);
1599
+ media.retainedSessionImageKindsByCallId.delete(callId);
1342
1600
  }
1343
1601
  }
1602
+ if (newlyDurableToolCallId) {
1603
+ // The exact call/result receipt and structural output event are now
1604
+ // durable even when parallel SDK history is still non-monotonic.
1605
+ // Release only this result owner's completed foreground ops. A
1606
+ // missing/duplicate receipt cannot license new output collection;
1607
+ // the complete-turn hook remains its later durability boundary.
1608
+ await finalizeTurnOpStreamOps([newlyDurableToolCallId]);
1609
+ }
1344
1610
  }
1345
1611
  } catch (error) {
1612
+ modelCallAdmission.fail(error);
1346
1613
  // Event processing can fail while SDK completion is still pending.
1347
1614
  // Close this stream before any failure publication; a legitimate
1348
1615
  // compaction retry may start a new, independently fenced generation.
@@ -1365,6 +1632,10 @@ export async function runTurnStreamAttempt(
1365
1632
  attemptId: input.attemptId,
1366
1633
  dispatchId,
1367
1634
  executionGeneration: attempt.executionGeneration,
1635
+ ...eventing.initialModelWireDispatch.payload({
1636
+ provider: streamProvider,
1637
+ dispatchId,
1638
+ }),
1368
1639
  },
1369
1640
  },
1370
1641
  ]);
@@ -1389,15 +1660,13 @@ export async function runTurnStreamAttempt(
1389
1660
  await eventing.publish!([
1390
1661
  {
1391
1662
  type: "turn.startup.phase.failed",
1392
- payload: {
1393
- phase: "model_preparation",
1394
- durationMs: Math.round(durationMs),
1395
- },
1663
+ payload: modelPreparationFailureEventPayload(error, durationMs),
1396
1664
  },
1397
1665
  ]);
1398
1666
  }
1399
1667
  throw error;
1400
1668
  } finally {
1669
+ modelCallAdmission.close();
1401
1670
  if (!streamDone) {
1402
1671
  // ReadableStream cancellation synchronously trips the Agents SDK's
1403
1672
  // abort controller, but its returned promise may wait for an
@@ -1419,7 +1688,10 @@ export async function runTurnStreamAttempt(
1419
1688
  // External Codemode stays reachable until finalization. Close wait
1420
1689
  // admission before any terminal output/history decision, and drain an
1421
1690
  // already-admitted wait before consulting the actual runner-yield latch.
1422
- await eventing.preparedTools?.inputWaitYield?.sealForSettlement(runtimeCancellationSignal);
1691
+ // Drain trusted receipts before deciding whether an empty stream may need
1692
+ // a handoff. Do not terminally seal the attempt until that decision: the
1693
+ // one same-turn stream still uses the ordinary input-wait admission gate.
1694
+ await eventing.preparedTools?.inputWaitYield?.drainForHandoff(runtimeCancellationSignal);
1423
1695
  if (
1424
1696
  options.requireTerminalModelResponse &&
1425
1697
  !eventing.preparedTools?.inputWaitYield?.yielded &&
@@ -1441,6 +1713,12 @@ export async function runTurnStreamAttempt(
1441
1713
  if (!streamSawPerResponseUsage) {
1442
1714
  const aggregateUsage = eventing.stream.state.usage;
1443
1715
  const normalizedAggregateUsage = normalizeModelCallUsage(aggregateUsage);
1716
+ const aggregatePolicyRevision = aggregateCreditPolicyRevision({
1717
+ responseRevisions: responseCreditPolicyRevisions,
1718
+ lastAdmittedRevision: creditPolicyRevision,
1719
+ chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
1720
+ totalTokens: normalizedAggregateUsage.totalTokens,
1721
+ });
1444
1722
  const aggregateInput = normalizedAggregateUsage.telemetry.inputTokens;
1445
1723
  const aggregateSourceKey = modelUsageSourceKey({
1446
1724
  responseId: null,
@@ -1463,12 +1741,13 @@ export async function runTurnStreamAttempt(
1463
1741
  leaseLostMessage: "Provider credential lease expired during the active turn",
1464
1742
  recordUsage: async () => {
1465
1743
  const billing = await recordModelUsageAndDebitCredits(settings, db, {
1744
+ creditPolicyRevision: aggregatePolicyRevision,
1466
1745
  accountId: input.accountId,
1467
1746
  workspaceId: input.workspaceId,
1468
1747
  sessionId: input.sessionId,
1469
1748
  turnId: activeTurnId,
1470
1749
  turnAttemptId: input.attemptId,
1471
- model: turn.model,
1750
+ model: turnExecutionPolicy.productModelId,
1472
1751
  externallyBilled: billingState.isExternallyBilledTurn,
1473
1752
  chargesOpenGeniCredits: billingState.chargesOpenGeniCredits,
1474
1753
  countsTowardTokenCap: billingState.countsTowardTokenCap,
@@ -1477,6 +1756,7 @@ export async function runTurnStreamAttempt(
1477
1756
  sourceKey: aggregateSourceKey,
1478
1757
  latencyMode: turnExecutionPolicy.latencyMode,
1479
1758
  observability,
1759
+ metricProvider: resolvedModel?.provider.id ?? settings.openaiProvider,
1480
1760
  });
1481
1761
  const aggregateProvider = resolvedModel?.provider.id ?? settings.openaiProvider;
1482
1762
  const aggregateProviderApi = resolvedModel?.provider.api ?? "responses";
@@ -1489,7 +1769,8 @@ export async function runTurnStreamAttempt(
1489
1769
  turnId: activeTurnId,
1490
1770
  provider: aggregateProvider,
1491
1771
  providerApi: aggregateProviderApi,
1492
- model: turn.model,
1772
+ model: turnExecutionPolicy.productModelId,
1773
+ purpose: "main",
1493
1774
  sourceKey: aggregateSourceKey,
1494
1775
  usage: { usage: aggregateUsage },
1495
1776
  normalizedUsage: normalizedAggregateUsage,
@@ -1511,10 +1792,20 @@ export async function runTurnStreamAttempt(
1511
1792
  sourceKey: aggregateSourceKey,
1512
1793
  provider: aggregateProvider,
1513
1794
  providerApi: aggregateProviderApi,
1514
- model: turn.model,
1795
+ model: turnExecutionPolicy.productModelId,
1515
1796
  billing,
1797
+ subscriptionConnectionId:
1798
+ subscriptionCoreTurnOf(providerTurn)?.connectionId ?? null,
1516
1799
  contextContributions: eventing.companyBrainContextContributions,
1517
1800
  });
1801
+ recordAuthoritativeModelUsageMetrics({
1802
+ observability,
1803
+ settings,
1804
+ provider: aggregateProvider,
1805
+ model: turnExecutionPolicy.productModelId,
1806
+ externallyBilled: billingState.isExternallyBilledTurn,
1807
+ billing,
1808
+ });
1518
1809
  }
1519
1810
  if (aggregateAuthoritative && aggregateInput !== null && aggregateInput > 0) {
1520
1811
  recordModelInputTokens(observability, streamProvider, aggregateInput);
@@ -1531,9 +1822,21 @@ export async function runTurnStreamAttempt(
1531
1822
  });
1532
1823
  }
1533
1824
  }
1534
- if (eventing.stream.interruptions.length > 0) {
1825
+ const programmaticPending = (
1826
+ await listTurnCodemodeApprovals(db, {
1827
+ accountId: input.accountId,
1828
+ workspaceId: input.workspaceId,
1829
+ sessionId: input.sessionId,
1830
+ turnId: activeTurnId,
1831
+ })
1832
+ )
1833
+ .filter((operation) => operation.state === "waiting_for_approval")
1834
+ .map(programmaticApproval);
1835
+ if (eventing.stream.interruptions.length > 0 || programmaticPending.length > 0) {
1836
+ await eventing.preparedTools?.inputWaitYield?.sealForSettlement(runtimeCancellationSignal);
1535
1837
  await historySink.reconcileConversationTruth({ requireDurable: true });
1536
- const approvals = runtime.serializeApprovals(eventing.stream.interruptions);
1838
+ const sdkApprovals = runtime.serializeApprovals(eventing.stream.interruptions);
1839
+ const approvals = [...sdkApprovals, ...programmaticPending];
1537
1840
  const humanInputInterruptions =
1538
1841
  runtime.serializeHumanInputRequests?.(eventing.stream.interruptions) ?? [];
1539
1842
  const interactionInterventionInterruptions =
@@ -1613,32 +1916,43 @@ export async function runTurnStreamAttempt(
1613
1916
  const interruptionCallIds = interruptionCallIdsFromPause({
1614
1917
  humanInputRequests,
1615
1918
  interactionInterventionRequests,
1616
- pendingApprovals,
1919
+ pendingApprovals: [
1920
+ ...sdkApprovals,
1921
+ ...interactionInterventionInterruptions.map((interruption) => interruption.approval),
1922
+ ],
1617
1923
  });
1618
1924
  assertOpenSuffixResumable(suffixMembers, interruptionCallIds);
1619
1925
  const suffixByCallId = new Map(suffixMembers.map((member) => [member.callId, member]));
1620
- const attached = await attachOpenSuffixToPendingToolCalls(db, {
1621
- accountId: input.accountId,
1622
- workspaceId: input.workspaceId,
1623
- sessionId: input.sessionId,
1624
- turnId: activeTurnId,
1625
- executionGeneration: attempt.executionGeneration,
1626
- attemptId: input.attemptId,
1627
- members: interruptionCallIds.map((callId) => {
1628
- const member = suffixByCallId.get(callId)!;
1629
- return {
1630
- callId,
1631
- interruptionKind: interruptionKindForCallItem(
1632
- member.callItem as Record<string, unknown>,
1633
- ),
1634
- reasoningItems: member.reasoningItems as Array<Record<string, unknown>>,
1635
- };
1636
- }),
1637
- });
1926
+ const attached =
1927
+ interruptionCallIds.length === 0
1928
+ ? { accepted: true }
1929
+ : await attachOpenSuffixToPendingToolCalls(db, {
1930
+ accountId: input.accountId,
1931
+ workspaceId: input.workspaceId,
1932
+ sessionId: input.sessionId,
1933
+ turnId: activeTurnId,
1934
+ executionGeneration: attempt.executionGeneration,
1935
+ attemptId: input.attemptId,
1936
+ members: interruptionCallIds.map((callId) => {
1937
+ const member = suffixByCallId.get(callId)!;
1938
+ return {
1939
+ callId,
1940
+ interruptionKind: interruptionKindForCallItem(
1941
+ member.callItem as Record<string, unknown>,
1942
+ ),
1943
+ reasoningItems: member.reasoningItems as Array<Record<string, unknown>>,
1944
+ };
1945
+ }),
1946
+ });
1638
1947
  if (!attached.accepted) {
1639
1948
  return claimedResult({ status: "cancelled" });
1640
1949
  }
1950
+ // History plus the exact approval suffix now own the complete response.
1951
+ await subscriptionCoreTurnOf(providerTurn)?.requests?.checkpoint();
1641
1952
  await finishParallelSessionTitle();
1953
+ // Held task-usage receipts are durable before the attempt waits on a
1954
+ // person (the resume may run in another worker process).
1955
+ await flushHeldSessionUsageReceipts(db, input.sessionId);
1642
1956
  if (
1643
1957
  !(await eventing.settle!({
1644
1958
  events: [
@@ -1649,9 +1963,11 @@ export async function runTurnStreamAttempt(
1649
1963
  type: "session.requiresAction" as const,
1650
1964
  payload: {
1651
1965
  approvals: approvals.map((approval) =>
1652
- withMcpToolDisplayMetadata(
1653
- eventing.preparedTools?.mcpServers ?? [],
1654
- approval,
1966
+ withPublicApprovalFields(
1967
+ withMcpToolDisplayMetadata(
1968
+ eventing.preparedTools?.mcpServers ?? [],
1969
+ approval,
1970
+ ),
1655
1971
  ),
1656
1972
  ),
1657
1973
  },
@@ -1676,6 +1992,13 @@ export async function runTurnStreamAttempt(
1676
1992
  ) {
1677
1993
  return claimedResult({ status: "cancelled" });
1678
1994
  }
1995
+ // Agent-requested human handoffs open here, in the worker, never through
1996
+ // the API intervention route, so they are counted at their durable boundary.
1997
+ const observeIntervention = interactionInterventionMetricObserver(observability);
1998
+ for (const interruption of interactionInterventionInterruptions) {
1999
+ if (interruption.input.operation !== "request") continue;
2000
+ observeIntervention({ kind: interruption.input.kind, outcome: "opened" });
2001
+ }
1679
2002
  // The interruption and its preceding tool results are now durable.
1680
2003
  await finalizeTurnOpStreamOps();
1681
2004
  control.activityStatus = "requires_action";
@@ -1686,6 +2009,78 @@ export async function runTurnStreamAttempt(
1686
2009
  const finalOutput = String(
1687
2010
  requireAgentStreamFinalOutput(eventing.stream.finalOutput, inputWaitYielded),
1688
2011
  );
2012
+ const durableReplyFacts =
2013
+ !inputWaitYielded && finalOutput.trim().length === 0
2014
+ ? await sessionTurnFinalReplyFacts(db, input.workspaceId, input.sessionId, activeTurnId)
2015
+ : { toolsExecuted: false, completedGoal: false };
2016
+ let emptyFinalReply = false;
2017
+ if (
2018
+ needsFinalReply({
2019
+ output: finalOutput,
2020
+ inputWaitYielded:
2021
+ inputWaitYielded || eventing.preparedTools?.inputWaitYield?.requested === true,
2022
+ interrupted: false, // interruption settlement returned above
2023
+ maintenance: turn.source === "compaction",
2024
+ toolsExecuted: toolsExecuted || finalReplyNudged || durableReplyFacts.toolsExecuted,
2025
+ completedGoal: durableReplyFacts.completedGoal,
2026
+ })
2027
+ ) {
2028
+ await historySink.reconcileConversationTruth({ requireDurable: true });
2029
+ await subscriptionCoreTurnOf(providerTurn)?.requests?.checkpoint();
2030
+ // Consult retained truth, including inactive compacted rows, so an
2031
+ // attempt replacement or compaction cannot spend this bound again.
2032
+ if (
2033
+ finalReplyNudged ||
2034
+ (await sessionTurnHasFinalReplyNudge(
2035
+ db,
2036
+ input.workspaceId,
2037
+ input.sessionId,
2038
+ activeTurnId,
2039
+ finalReplyNudge(activeTurnId).content[0]!.text,
2040
+ ))
2041
+ ) {
2042
+ // A second empty response is a delivery-quality notice, not failed
2043
+ // execution: preserve goal continuation and later machine-input wakes.
2044
+ emptyFinalReply = true;
2045
+ } else {
2046
+ const appended = await appendSessionHistoryItems(db, {
2047
+ accountId: input.accountId,
2048
+ workspaceId: input.workspaceId,
2049
+ sessionId: input.sessionId,
2050
+ turnId: activeTurnId,
2051
+ expectedExecutionGeneration: attempt.executionGeneration,
2052
+ expectedAttemptId: input.attemptId,
2053
+ items: [
2054
+ {
2055
+ position: await nextSessionHistoryPosition(db, input.workspaceId, input.sessionId),
2056
+ item: finalReplyNudge(activeTurnId),
2057
+ },
2058
+ ],
2059
+ });
2060
+ if (!appended) throw new TurnAttemptFencedError("turn ended before final reply handoff");
2061
+ finalReplyNudged = true;
2062
+ await prepareRunAttemptInput();
2063
+ return "reply_nudge";
2064
+ }
2065
+ }
2066
+ await eventing.preparedTools?.inputWaitYield?.sealForSettlement(runtimeCancellationSignal);
2067
+ // The final output is the newest message this stream completed, already
2068
+ // durable with its provider identity and phase. A phase-less settlement
2069
+ // copy is published only when this stream did not complete that text.
2070
+ const finalOutputAlreadyCompleted = latestStreamedAssistantText === finalOutput;
2071
+ // A wait ends the turn with empty output; a human's message still gets
2072
+ // its answer recorded for unread attention and Slack.
2073
+ const reply = await inputWaitReply({
2074
+ inputWaitYielded,
2075
+ turn,
2076
+ latestAssistantMessageText,
2077
+ readLatestDurableTurnMessage: async () =>
2078
+ await latestDurableTurnMessageText(db, {
2079
+ workspaceId: input.workspaceId,
2080
+ sessionId: input.sessionId,
2081
+ turnId: turn.id,
2082
+ }),
2083
+ });
1689
2084
  await historySink.reconcileConversationTruth({ requireDurable: true });
1690
2085
  // Op-stream durability fence: the tool outputs are now durably in the
1691
2086
  // history store (a redispatch would NOT re-execute them), so this
@@ -1693,15 +2088,26 @@ export async function runTurnStreamAttempt(
1693
2088
  // then wire final ack (licensing the runner to GC its retained
1694
2089
  // frames). Best-effort: a miss leaves the runner's retention TTL to
1695
2090
  // reap, never fails a completed turn.
2091
+ await subscriptionCoreTurnOf(providerTurn)?.requests?.checkpoint();
1696
2092
  await finalizeTurnOpStreamOps();
1697
2093
  await finishParallelSessionTitle();
2094
+ // Every task-usage receipt held after a failed write is durable before
2095
+ // the turn completes (a persistent failure enters DB-only recovery).
2096
+ await flushHeldSessionUsageReceipts(db, input.sessionId);
1698
2097
  if (
1699
2098
  !(await eventing.settle!({
1700
2099
  events: [
1701
- ...(inputWaitYielded
2100
+ ...(inputWaitYielded || finalOutputAlreadyCompleted
1702
2101
  ? []
1703
2102
  : [{ type: "agent.message.completed" as const, payload: { text: finalOutput } }]),
1704
- { type: "turn.completed", payload: { output: finalOutput } },
2103
+ {
2104
+ type: "turn.completed",
2105
+ payload: {
2106
+ output: finalOutput,
2107
+ ...(reply === null ? {} : { reply }),
2108
+ ...(emptyFinalReply ? { emptyFinalReply: true } : {}),
2109
+ },
2110
+ },
1705
2111
  { type: "session.status.changed", payload: { status: "idle" } },
1706
2112
  ],
1707
2113
  turnStatus: "completed",
@@ -1729,7 +2135,41 @@ export async function runTurnStreamAttempt(
1729
2135
  return claimedResult({ status: "idle" });
1730
2136
  };
1731
2137
 
2138
+ // Recover committed waits/results before any model dispatch, including crash replacement.
2139
+ // A new attempt uses its own signed caller; origin provenance never changes.
2140
+ // Join-on-use: lazy MCP preparation is awaited only when stored work must resume.
2141
+ const codemodeOperations = await CodemodeAttemptDispatcher.resumeApproved(
2142
+ db,
2143
+ {
2144
+ accountId: input.accountId,
2145
+ workspaceId: input.workspaceId,
2146
+ sessionId: input.sessionId,
2147
+ turnId: activeTurnId,
2148
+ attemptId: input.attemptId,
2149
+ executionGeneration: attempt.executionGeneration,
2150
+ },
2151
+ `sandbox:${input.attemptId}`,
2152
+ trigger.type === "user.approvalDecision"
2153
+ ? (trigger.payload as { approvalId: string }).approvalId
2154
+ : undefined,
2155
+ async () => {
2156
+ await eventing.toolPreparationReady;
2157
+ return eventing.codemodeDispatcher;
2158
+ },
2159
+ );
2160
+ codemodeContinuationNote = programmaticContinuationNote(codemodeOperations);
2161
+ programmaticApprovalAcknowledged =
2162
+ trigger.type === "user.approvalDecision" &&
2163
+ codemodeOperations.some(
2164
+ (operation) =>
2165
+ operation.operationId === (trigger.payload as { approvalId?: unknown }).approvalId,
2166
+ );
2167
+ const pendingProgrammaticApprovals = codemodeOperations
2168
+ .filter((operation) => operation.state === "waiting_for_approval")
2169
+ .map(programmaticApproval);
1732
2170
  const openSuffixResume = await settleOpenSuffixResumeIfNeeded({
2171
+ additionalApprovals: pendingProgrammaticApprovals,
2172
+ programmaticApprovalAcknowledged,
1733
2173
  db,
1734
2174
  agent,
1735
2175
  accountId: input.accountId,
@@ -1761,6 +2201,9 @@ export async function runTurnStreamAttempt(
1761
2201
  ) {
1762
2202
  return claimedResult({ status: "cancelled" });
1763
2203
  }
2204
+ // Preparation may have spent the last allowance on a completed summary.
2205
+ // Neither the title sidecar nor ordinary inference may dispatch afterward.
2206
+ await revalidateModelCallAdmission();
1764
2207
  if (
1765
2208
  turn.source !== "compaction" &&
1766
2209
  generateSessionTitleInParallel &&
@@ -1771,22 +2214,28 @@ export async function runTurnStreamAttempt(
1771
2214
  turnExecutionPolicy.providerId,
1772
2215
  turnExecutionPolicy.latencyMode,
1773
2216
  );
2217
+ titleCreditPolicyRevision = creditPolicyRevision;
2218
+ const titleRoute = sessionTitleRoute({
2219
+ resolvedModel,
2220
+ modelName: turnExecutionPolicy.upstreamModelId,
2221
+ resolveTurnModel: (modelId) => runtime.resolveTurnModel(runSettings, modelId),
2222
+ });
2223
+ titleUsageModelId = titleRoute.usageModelId;
1774
2224
  parallelSessionTitle = startParallelSessionTitleGeneration({
1775
2225
  signal: runtimeCancellationSignal,
2226
+ onFinish: persistParallelSessionTitle,
1776
2227
  generate: async (signal) =>
1777
2228
  await withSessionTitleProviderRequestContext(() =>
1778
- runtime.generateSessionTitle!(runSettings, sessionTitlePrompt, {
1779
- ...(resolvedModel
1780
- ? {
1781
- client: resolvedModel.client,
1782
- provider: resolvedModel.provider,
1783
- model: resolvedModel.model,
1784
- }
1785
- : {}),
1786
- modelName: turnExecutionPolicy.upstreamModelId,
1787
- ...(serviceTier ? { serviceTier } : {}),
1788
- signal,
1789
- }),
2229
+ runtime.generateSessionTitle!(
2230
+ runSettings,
2231
+ sessionTitlePrompt,
2232
+ sessionTitleGenerationOptions({
2233
+ resolvedModel: titleRoute.resolvedModel,
2234
+ modelName: titleRoute.modelName,
2235
+ serviceTier,
2236
+ signal,
2237
+ }),
2238
+ ),
1790
2239
  ),
1791
2240
  onError: (error) => {
1792
2241
  observability.warn("parallel session title generation failed", {
@@ -1800,11 +2249,19 @@ export async function runTurnStreamAttempt(
1800
2249
  }
1801
2250
  try {
1802
2251
  let retriedAfterCompaction = false;
2252
+ finalReplyNudged = await sessionTurnHasFinalReplyNudge(
2253
+ db,
2254
+ input.workspaceId,
2255
+ input.sessionId,
2256
+ activeTurnId,
2257
+ finalReplyNudge(activeTurnId).content[0]!.text,
2258
+ );
1803
2259
  while (true) {
1804
2260
  try {
1805
2261
  const result = await runStreamAttempt({
1806
2262
  requireTerminalModelResponse: retriedAfterCompaction,
1807
2263
  });
2264
+ if (result === "reply_nudge") continue;
1808
2265
  if (retriedAfterCompaction) {
1809
2266
  observability.info("context compaction recovery succeeded after in-activity retry", {
1810
2267
  sessionId: input.sessionId,
@@ -1813,13 +2270,14 @@ export async function runTurnStreamAttempt(
1813
2270
  }
1814
2271
  return result;
1815
2272
  } catch (attemptError) {
2273
+ const requestSizeError = findAnthropicRequestSizeError(attemptError);
1816
2274
  const overflow = classifyContextWindowOverflowError(attemptError);
1817
2275
  const compactionNeeded = findCompactionNeededError(attemptError);
1818
2276
  const recoveryKind = compactionNeeded
1819
2277
  ? compactionNeeded.trigger === "operator"
1820
2278
  ? "operator"
1821
2279
  : "proactive"
1822
- : overflow
2280
+ : overflow || requestSizeError
1823
2281
  ? "overflow"
1824
2282
  : null;
1825
2283
  if (!recoveryKind || !eventing.publish || !eventing.turnStartedPublished) {
@@ -1834,6 +2292,7 @@ export async function runTurnStreamAttempt(
1834
2292
  ...safeErrorDiagnostic(attemptError),
1835
2293
  signalTokens: compactionNeeded?.signalTokens,
1836
2294
  thresholdTokens: compactionNeeded?.thresholdTokens,
2295
+ ...(requestSizeError ? { ...requestSizeError.requestSize } : {}),
1837
2296
  });
1838
2297
  let compacted = false;
1839
2298
  let compactionHandled = false;
@@ -1844,6 +2303,7 @@ export async function runTurnStreamAttempt(
1844
2303
  const outcome = await forceContextCompaction(
1845
2304
  recoveryKind,
1846
2305
  compactionNeeded?.signalTokens ?? null,
2306
+ requestSizeError,
1847
2307
  );
1848
2308
  compacted = outcome.compacted;
1849
2309
  if (outcome.compacted) {
@@ -1858,8 +2318,31 @@ export async function runTurnStreamAttempt(
1858
2318
  // Transient checkpoint-provider failures recover this same accepted
1859
2319
  // turn through the normal provider/capacity path. They are not an
1860
2320
  // empty summary and must not create a new goal continuation.
1861
- if (shouldRecoverCompactionProviderFailure(compactError)) throw compactError;
2321
+ if (
2322
+ shouldRecoverCompactionProviderFailure(compactError) ||
2323
+ isCoreSourceDisconnectedError(compactError)
2324
+ )
2325
+ throw compactError;
1862
2326
  if (compactError instanceof TurnAttemptFencedError) throw compactError;
2327
+ if (isContextCompactionInterruption(compactError, cancellationSignal)) {
2328
+ // Graceful worker shutdown (or a control's activity cancellation)
2329
+ // aborted the in-flight checkpoint request. That is not a
2330
+ // summarization failure: record it truthfully, keep any operator
2331
+ // request pending, and let the same-turn recovery path run.
2332
+ await settleContextCompactionInterruption(
2333
+ db,
2334
+ {
2335
+ accountId: input.accountId,
2336
+ workspaceId: input.workspaceId,
2337
+ sessionId: input.sessionId,
2338
+ turnId: activeTurnId!,
2339
+ executionGeneration: attempt.executionGeneration,
2340
+ attemptId: input.attemptId,
2341
+ },
2342
+ { publishLiveEvents: publishCompactionLiveEvents, observability },
2343
+ );
2344
+ throw compactError;
2345
+ }
1863
2346
  const landmark = await settleFailedContextCompactionLandmark(
1864
2347
  db,
1865
2348
  {
@@ -1935,6 +2418,7 @@ export async function runTurnStreamAttempt(
1935
2418
  const deferredSteer = await settleDeferredSteerAfterCompaction();
1936
2419
  if (deferredSteer) return deferredSteer;
1937
2420
  if (turn.source === "compaction") {
2421
+ await flushHeldSessionUsageReceipts(db, input.sessionId);
1938
2422
  const settled = await eventing.settle!({
1939
2423
  events: [
1940
2424
  {
@@ -1969,6 +2453,11 @@ export async function runTurnStreamAttempt(
1969
2453
  await prepareRunAttemptInput();
1970
2454
  }
1971
2455
  }
2456
+ } catch (error) {
2457
+ // A lifecycle disconnect fences the next request, not the already admitted
2458
+ // title stream. Join and persist its bounded response before recovery.
2459
+ if (isCoreSourceDisconnectedError(error)) await finishParallelSessionTitle();
2460
+ throw error;
1972
2461
  } finally {
1973
2462
  await cancelParallelSessionTitle();
1974
2463
  }