@bitkyc08/opencodex 2.100.3 → 2.100.5-preview.20261118.1

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 (249) hide show
  1. package/AGENTS_INSTALL.md +23 -1
  2. package/bin/ocx.mjs +8 -1
  3. package/gui/dist/assets/{App-Dnb6VMsP.js → App-BiOGtxDS.js} +21 -20
  4. package/gui/dist/assets/{Tray-BV4ms9U9.js → Tray-BwsMejUO.js} +1 -1
  5. package/gui/dist/assets/brand-mark-BY1dgACL.js +1 -0
  6. package/gui/dist/assets/{index-DCdXsMyZ.js → index-CwHSKMdG.js} +46 -13
  7. package/gui/dist/assets/index-DJxM-imn.css +2 -0
  8. package/gui/dist/assets/{segment-thumb-features-DXSidY6D.js → segment-thumb-features-Bds9Hb_7.js} +1 -1
  9. package/gui/dist/classic/assets/{App-DA-scMPq.js → App-CGLOiqQL.js} +20 -19
  10. package/gui/dist/classic/assets/{Tray-BrHaVRAq.js → Tray-Da4r2O70.js} +1 -1
  11. package/gui/dist/classic/assets/{index-BqOaGLf5.js → index-CvoKXKL9.js} +46 -13
  12. package/gui/dist/classic/assets/quota-summary-DvqzfCmr.js +1 -0
  13. package/gui/dist/classic/index.html +1 -1
  14. package/gui/dist/index.html +2 -2
  15. package/native/ocx-child/src/cli.rs +5 -3
  16. package/native/ocx-child/src/codex_home.rs +1 -0
  17. package/native/ocx-child/src/link_ws.rs +24 -19
  18. package/native/ocx-child/src/link_ws_frame.rs +136 -50
  19. package/package.json +3 -3
  20. package/src/adapters/anthropic/per-message-effort.ts +296 -0
  21. package/src/adapters/anthropic-input-schema.ts +270 -0
  22. package/src/adapters/anthropic.ts +4 -63
  23. package/src/adapters/claude-cli/adapter.ts +1 -0
  24. package/src/adapters/command-code-tool-text.ts +9 -1
  25. package/src/adapters/command-code.ts +18 -2
  26. package/src/adapters/devin/cloud-direct/chat.ts +95 -14
  27. package/src/adapters/devin/live-models.ts +11 -1
  28. package/src/adapters/google-errors.ts +14 -2
  29. package/src/adapters/kiro/reasoning.ts +3 -0
  30. package/src/adapters/kiro-tools.ts +14 -9
  31. package/src/adapters/openai-responses/passthrough.ts +3 -1
  32. package/src/adapters/openai-responses/request-strips.ts +1 -0
  33. package/src/adapters/qoder/adapter.ts +9 -1
  34. package/src/adapters/tool-catalog-nudge.ts +1 -1
  35. package/src/adapters/zed.ts +4 -2
  36. package/src/claude/chat-tool-dispatch.ts +4 -1
  37. package/src/claude/desktop-remote-store.ts +60 -10
  38. package/src/cli/account-api.ts +4 -0
  39. package/src/cli/account-auth.ts +30 -2
  40. package/src/cli/account-extended.ts +33 -2
  41. package/src/cli/capabilities-access-remote.ts +3 -3
  42. package/src/cli/capabilities-accounts.ts +3 -3
  43. package/src/cli/capabilities-base.ts +11 -6
  44. package/src/cli/capabilities-observe-system.ts +2 -2
  45. package/src/cli/claude.ts +3 -3
  46. package/src/cli/disconnect-runtime.ts +16 -6
  47. package/src/cli/dispatch.ts +7 -1
  48. package/src/cli/index.ts +2 -2
  49. package/src/cli/link-apply-restart.ts +4 -2
  50. package/src/cli/link-routes.ts +4 -1
  51. package/src/cli/link.ts +5 -4
  52. package/src/cli/logout-command.ts +23 -4
  53. package/src/cli/preflight-exemptions.ts +3 -0
  54. package/src/cli/provider.ts +3 -2
  55. package/src/cli/registry.ts +5 -2
  56. package/src/cli/resolve.ts +23 -8
  57. package/src/cli/restart-failure.ts +11 -1
  58. package/src/cli/runtime-api.ts +4 -0
  59. package/src/cli/status.ts +4 -1
  60. package/src/cli/system-command.ts +1 -1
  61. package/src/cli/system-restart-client.ts +9 -3
  62. package/src/cli/system-settings-parity.ts +2 -1
  63. package/src/client/connect.ts +73 -11
  64. package/src/client/desktop-refusal.ts +38 -0
  65. package/src/client/lifecycle-lock.ts +29 -4
  66. package/src/client/runtime-restart.ts +27 -15
  67. package/src/client/runtime.ts +4 -2
  68. package/src/client/state.ts +14 -0
  69. package/src/clients/config-export.ts +12 -7
  70. package/src/codex/account-lifecycle.ts +3 -1
  71. package/src/codex/account-pause.ts +4 -0
  72. package/src/codex/account-runtime-state.ts +4 -0
  73. package/src/codex/auth-api/main-account-probe.ts +3 -1
  74. package/src/codex/auth-api/main-hard-lock-refusal-read.ts +172 -0
  75. package/src/codex/auth-context.ts +40 -2
  76. package/src/codex/catalog/build-entries.ts +10 -0
  77. package/src/codex/catalog/derive-entry.ts +7 -0
  78. package/src/codex/catalog/metadata.ts +8 -0
  79. package/src/codex/catalog/native-models.ts +16 -6
  80. package/src/codex/catalog/pinned-models.ts +1 -0
  81. package/src/codex/catalog/provider-models.ts +5 -5
  82. package/src/codex/catalog/reserve-warn.ts +7 -5
  83. package/src/codex/catalog/reserve.ts +4 -3
  84. package/src/codex/catalog/retained-sync.ts +2 -0
  85. package/src/codex/catalog/routed-defaults.ts +35 -0
  86. package/src/codex/catalog-auto-refresh.ts +13 -2
  87. package/src/codex/config-drift-heal.ts +40 -12
  88. package/src/codex/config-write-lock.ts +39 -1
  89. package/src/codex/convergence.ts +2 -0
  90. package/src/codex/data/roster-pinned-models.json +346 -1
  91. package/src/codex/inject/config-write-section.ts +39 -1
  92. package/src/codex/inject/plan.ts +10 -8
  93. package/src/codex/inject/remove.ts +21 -19
  94. package/src/codex/inject/restore.ts +5 -1
  95. package/src/codex/inject-coordination.ts +58 -7
  96. package/src/codex/inject.ts +10 -12
  97. package/src/codex/journal-config-restore.ts +42 -0
  98. package/src/codex/journal.ts +62 -8
  99. package/src/codex/loopback-target.ts +22 -8
  100. package/src/codex/main-account.ts +94 -30
  101. package/src/codex/native-main-auth-file.ts +14 -0
  102. package/src/codex/native-main-credentials.ts +276 -0
  103. package/src/codex/native-main-owner.ts +3 -0
  104. package/src/codex/native-main-refresh-mode.ts +14 -1
  105. package/src/codex/quota.ts +63 -14
  106. package/src/combos/failover.ts +195 -1
  107. package/src/combos/index.ts +5 -0
  108. package/src/combos/resolve.ts +115 -19
  109. package/src/config/diagnostics.ts +3 -0
  110. package/src/config/live-reconcile.ts +6 -3
  111. package/src/config/load-degrade.ts +2 -0
  112. package/src/config/persisted-mutation.ts +2 -2
  113. package/src/config/persisted-projections.ts +18 -0
  114. package/src/config/process-state.ts +4 -3
  115. package/src/config/reserve-capability.ts +96 -0
  116. package/src/config/schema/config-schema.ts +6 -0
  117. package/src/config/schema/leaf-validators.ts +2 -0
  118. package/src/config/schema/routed-catalog-defaults.ts +49 -0
  119. package/src/config/subagent-models.ts +20 -12
  120. package/src/config.ts +7 -7
  121. package/src/desktop-update/acquire.ts +202 -0
  122. package/src/desktop-update/cli.ts +168 -0
  123. package/src/desktop-update/drain.ts +40 -0
  124. package/src/desktop-update/headless.ts +285 -0
  125. package/src/desktop-update/image.ts +85 -0
  126. package/src/desktop-update/install.ts +53 -0
  127. package/src/desktop-update/msi.ts +72 -0
  128. package/src/desktop-update/release.ts +128 -0
  129. package/src/desktop-update/runtime.ts +155 -0
  130. package/src/desktop-update/staging.ts +118 -0
  131. package/src/desktop-update/trust.ts +45 -0
  132. package/src/generated/compatibility-version.json +327 -195
  133. package/src/generated/model-metadata.ts +3 -3
  134. package/src/images/fulfill-video.ts +3 -3
  135. package/src/images/loop.ts +6 -2
  136. package/src/images/plan.ts +147 -11
  137. package/src/images/types.ts +13 -4
  138. package/src/images/xai-video-client.ts +72 -10
  139. package/src/lib/client-error-body.ts +6 -0
  140. package/src/lib/developer-role-rejection.ts +15 -0
  141. package/src/lib/gcp-adc.ts +48 -4
  142. package/src/lib/offline-resend.ts +150 -0
  143. package/src/lib/paced-stream.ts +3 -3
  144. package/src/lib/provider-outbound.ts +17 -6
  145. package/src/lib/request-failure-model.ts +2 -0
  146. package/src/lib/response-body-inactivity.ts +23 -1
  147. package/src/lib/upstream-retry.ts +39 -6
  148. package/src/link/explicit-route.ts +4 -4
  149. package/src/link/remote-windows.ts +79 -31
  150. package/src/link/residual-recovery.ts +20 -8
  151. package/src/messaging/budget.ts +23 -5
  152. package/src/messaging/remote-ports.ts +22 -10
  153. package/src/messaging/rpc.ts +4 -4
  154. package/src/oauth/antigravity-risk.ts +126 -0
  155. package/src/oauth/credential-siblings.ts +32 -0
  156. package/src/oauth/generic-account-failover.ts +113 -30
  157. package/src/oauth/generic-session-affinity.ts +10 -3
  158. package/src/oauth/index.ts +22 -14
  159. package/src/oauth/login-cli.ts +35 -1
  160. package/src/oauth/pool-kernel.ts +3 -3
  161. package/src/oauth/store.ts +38 -7
  162. package/src/providers/anthropic-billing-refusal.ts +27 -3
  163. package/src/providers/anthropic-reset-grants.ts +4 -0
  164. package/src/providers/claude-cli-usage.ts +2 -0
  165. package/src/providers/codebuddy-models.ts +11 -0
  166. package/src/providers/google-vertex-model-discovery.ts +49 -15
  167. package/src/providers/kiro-models.ts +5 -0
  168. package/src/providers/model-rename-fields.ts +2 -0
  169. package/src/providers/quota/workbuddy.ts +6 -2
  170. package/src/providers/registry/entries-core.ts +9 -3
  171. package/src/providers/registry/entries-extended.ts +20 -9
  172. package/src/providers/registry/model-seeds.ts +4 -1
  173. package/src/providers/stale-context-window-migration.ts +2 -0
  174. package/src/providers/workbuddy-credit-balance.ts +125 -16
  175. package/src/responses/citation-markers.ts +26 -23
  176. package/src/responses/effort-history.ts +732 -0
  177. package/src/routing/history/integrity.ts +41 -4
  178. package/src/server/auth-cors.ts +13 -3
  179. package/src/server/codex-model-list.ts +2 -1
  180. package/src/server/direct-local-http.ts +26 -3
  181. package/src/server/index/link-listener.ts +20 -14
  182. package/src/server/index/serve-options.ts +1 -0
  183. package/src/server/index.ts +3 -2
  184. package/src/server/link-turn.ts +17 -5
  185. package/src/server/management/config-routes.ts +39 -3
  186. package/src/server/management/link-child-preflight.ts +1 -1
  187. package/src/server/management/link-enrollment.ts +3 -3
  188. package/src/server/management/link-route-edit.ts +20 -1
  189. package/src/server/management/link-route-request.ts +11 -5
  190. package/src/server/management/oauth-account-routes.ts +43 -13
  191. package/src/server/management/provider-credential-release.ts +44 -0
  192. package/src/server/management/provider-overwrite-carry.ts +3 -0
  193. package/src/server/management/provider-routes.ts +12 -12
  194. package/src/server/management/static-catalog-probe.ts +33 -0
  195. package/src/server/management/usage-aggregate-cache.ts +6 -3
  196. package/src/server/messages-native.ts +16 -2
  197. package/src/server/proxy-liveness.ts +54 -9
  198. package/src/server/relay.ts +6 -1
  199. package/src/server/request-log-conversation.ts +8 -4
  200. package/src/server/request-log-failure-detail.ts +26 -9
  201. package/src/server/request-log.ts +5 -1
  202. package/src/server/reserve-capability-startup.ts +32 -0
  203. package/src/server/responses/adapter-continuation.ts +59 -25
  204. package/src/server/responses/adapter-delivery.ts +2 -0
  205. package/src/server/responses/adapter-dispatch.ts +39 -18
  206. package/src/server/responses/antigravity-fault-rotation.ts +24 -0
  207. package/src/server/responses/antigravity-validation-refusal.ts +43 -6
  208. package/src/server/responses/codex-tool-catalog.ts +47 -10
  209. package/src/server/responses/combo-selection-keepalive.ts +254 -0
  210. package/src/server/responses/combo-session-recall.ts +26 -1
  211. package/src/server/responses/combo-stream-preflight.ts +35 -26
  212. package/src/server/responses/core-combo-failure.ts +6 -0
  213. package/src/server/responses/core-combo-native.ts +2 -1
  214. package/src/server/responses/core-combo.ts +67 -28
  215. package/src/server/responses/core-errors.ts +9 -6
  216. package/src/server/responses/core-options.ts +10 -0
  217. package/src/server/responses/core.ts +21 -0
  218. package/src/server/responses/estimated-usage.ts +115 -0
  219. package/src/server/responses/input-admission.ts +41 -3
  220. package/src/server/responses/jev-model-invoke.ts +7 -2
  221. package/src/server/responses/passthrough-completion.ts +6 -3
  222. package/src/server/responses/passthrough-delivery.ts +47 -8
  223. package/src/server/responses/passthrough-dispatch.ts +3 -0
  224. package/src/server/responses/request-prepare.ts +9 -5
  225. package/src/server/responses/request-send-budget.ts +1 -1
  226. package/src/server/responses/request-transport.ts +10 -2
  227. package/src/server/responses/sidecar-execution.ts +11 -4
  228. package/src/service/desktop-supervision.d.mts +2 -0
  229. package/src/service/desktop-supervision.mjs +5 -0
  230. package/src/service/windows-checked-stop-live.ts +28 -5
  231. package/src/stall-timeout.ts +11 -0
  232. package/src/types/config.ts +15 -92
  233. package/src/types/provider.ts +13 -0
  234. package/src/types/request.ts +23 -3
  235. package/src/types/routed-catalog-defaults.ts +17 -0
  236. package/src/types/routing-profile.ts +90 -0
  237. package/src/types.ts +1 -0
  238. package/src/update/index.ts +1 -1
  239. package/src/usage/expected-prices.ts +35 -0
  240. package/src/usage/ledger-scanner.ts +44 -10
  241. package/src/usage/log.ts +14 -4
  242. package/src/usage/telemetry-contract.ts +1 -0
  243. package/src/web-search/loop.ts +20 -2
  244. package/src/web-search/parse.ts +2 -12
  245. package/src/web-search/passthrough-bridge.ts +7 -0
  246. package/src/web-search/sources.ts +13 -0
  247. package/gui/dist/assets/brand-mark-XOLJbomA.js +0 -1
  248. package/gui/dist/assets/index-DQtCRNAw.css +0 -2
  249. package/gui/dist/classic/assets/quota-summary-Dy5_wfTT.js +0 -1
@@ -23,7 +23,7 @@ import {
23
23
  } from "../../reasoning-effort";
24
24
  import { admissionMayUseMeteredComboTargets, resolveAdmissionModelScope, routeAllowedByScope } from "../admission-model-scope";
25
25
  import { recordAttemptRequestedEffort } from "../request-log";
26
- import { failLinkTurnExpansion, markLinkTurnMissing, observeLinkTurnExpansion } from "../link-turn";
26
+ import { bindLinkTurnChild, failLinkTurnExpansion, markLinkTurnMissing, observeLinkTurnExpansion } from "../link-turn";
27
27
  import {
28
28
  CODEX_TEXT_GUARDED_BUDGET_POLICY,
29
29
  deriveRequestExecutionBudget,
@@ -47,7 +47,9 @@ import {
47
47
  comboDefaultEffort,
48
48
  isComboTargetInCooldown,
49
49
  snapshotComboQuotaCooldowns,
50
- comboExclusionReasons,
50
+ comboExclusion,
51
+ comboUnavailableDiagnostics,
52
+ comboTargetUnavailableCause,
51
53
  comboTargetSkipTrigger,
52
54
  noteComboSuccess,
53
55
  settleComboReprobeSuccess,
@@ -118,7 +120,7 @@ import {
118
120
  import type { CodexAuthContext } from "../../codex/auth-context";
119
121
  import type { ResponsesTerminalStatus } from "../../bridge";
120
122
  import { beginRequestAttempt, sealRequestAttemptIdentity, finishRequestAttempt } from "../request-log";
121
- import { rememberComboForLane } from "./combo-session-recall";
123
+ import { recallComboTurnEffortForLane, rememberComboForLane, type ComboTurnEffort } from "./combo-session-recall";
122
124
  import { COMBO_TOOL_LOOP_PIN_REASON, toolLoopPinnedPick } from "./combo-tool-loop-pin";
123
125
  import { JEV_SESSION_PIN_ROUTE_REASON, jevSessionPinScope } from "./jev-session-pin";
124
126
  import { runTurnAdapterSseResponses } from "./core-lifetime";
@@ -605,6 +607,10 @@ export async function executeComboResponses(
605
607
  const enabled = combo.targets.filter(target => config.providers[target.provider]?.disabled !== true);
606
608
  return enabled.length > 0 && enabled.every(target => !museToolEligible(target));
607
609
  };
610
+ // A mandatory tool_choice no target preserves is unsupported (#7965), but shared exclusion evidence
611
+ // (provider state, quota, live cooldowns) keeps precedence, exactly as in the readable reasons.
612
+ const unsupportedToolChoice = (attempted: number) => comboUnavailableDiagnostics(combo.targets.length, attempted,
613
+ combo.targets.map(target => comboTargetUnavailableCause(config, comboId, target, museToolEligible) ?? "unsupported"));
608
614
  // Recovery can replace unreadable encrypted input, but cannot relax other admission checks.
609
615
  const recoveryEligible = (target: (typeof combo.targets)[number]): boolean =>
610
616
  (meteredAllowed || !target.metered)
@@ -710,6 +716,17 @@ export async function executeComboResponses(
710
716
  const jevPin = options.compactionRoutingOverride || options.mainHardLockFallbackOverride
711
717
  ? undefined
712
718
  : jevSessionPinScope({ combo, comboId, admission: options.admission, lane: sessionLaneIdFromRequest(req.headers) });
719
+ // A tool-loop continuation never asks JEV again, so under `decide` it keeps the effort the
720
+ // decision set for its turn while the target still offers it. Without this the rest of the
721
+ // turn ran at the caller's effort and only the first request at the decided one.
722
+ let toolLoopTurnEffort: ComboTurnEffort | undefined;
723
+ if (toolLoopPin && combo.strategy === "jev" && combo.jevEffortMode !== "client") {
724
+ const carried = recallComboTurnEffortForLane(sessionLaneIdFromRequest(req.headers), comboId, toolLoopPin.target);
725
+ const offered = carried ? jevChoiceEfforts(config, toolLoopPin.target, false)?.reasoningEfforts : undefined;
726
+ if (carried && offered && (carried.effort === null ? offered.length === 0 : offered.includes(carried.effort))) {
727
+ toolLoopTurnEffort = carried;
728
+ }
729
+ }
713
730
 
714
731
  if (unreadableEncryptedAgentTask && !pick) {
715
732
  pick = await pickWithWait({
@@ -725,12 +742,13 @@ export async function executeComboResponses(
725
742
  { parentThreadId: inboundClientThreadId },
726
743
  );
727
744
  if (!options.abortSignal?.aborted && onlyMuseToolIncompatible()) {
728
- return comboUnavailableResponse("No available targets for combo; targets cannot preserve mandatory tool_choice");
745
+ return comboUnavailableResponse("No available targets for combo; targets cannot preserve mandatory tool_choice",
746
+ { diagnostics: unsupportedToolChoice(0) });
729
747
  }
730
748
  return options.abortSignal?.aborted
731
749
  ? clientCancelledResponse()
732
750
  : comboUnavailable(comboId, undefined,
733
- comboExclusionReasons(config, comboId, recoveryEligible));
751
+ comboExclusion(config, comboId, recoveryEligible));
734
752
  }
735
753
  if (!(await recoverUnreadableEncryptedTask())) {
736
754
  return options.abortSignal?.aborted
@@ -745,11 +763,12 @@ export async function executeComboResponses(
745
763
  if (protocolRefusal) return protocolRefusal;
746
764
  if (onlyReplayIncompatibleTargetsRemain()) return targetIncompatibleResponse();
747
765
  if (!options.abortSignal?.aborted && onlyMuseToolIncompatible()) {
748
- return comboUnavailableResponse("No available targets for combo; targets cannot preserve mandatory tool_choice");
766
+ return comboUnavailableResponse("No available targets for combo; targets cannot preserve mandatory tool_choice",
767
+ { diagnostics: unsupportedToolChoice(0) });
749
768
  }
750
769
  return options.abortSignal?.aborted
751
770
  ? clientCancelledResponse()
752
- : comboUnavailable(comboId, undefined, comboExclusionReasons(config, comboId, targetEligible));
771
+ : comboUnavailable(comboId, undefined, comboExclusion(config, comboId, targetEligible));
753
772
  }
754
773
  // OCX-11: an ordered failover combo that starts on a later target than its first one, for any
755
774
  // reason (cooling down, provider disabled, cached quota spent, ineligible for this request),
@@ -780,7 +799,7 @@ export async function executeComboResponses(
780
799
  const choices = eligibleJevComboChoices(config, comboId, targetEligible, Date.now(), pick.cooldownReprobeTargets);
781
800
  const first = choices[0];
782
801
  if (!first) {
783
- return comboUnavailable(comboId, undefined, comboExclusionReasons(config, comboId, target =>
802
+ return comboUnavailable(comboId, undefined, comboExclusion(config, comboId, target =>
784
803
  targetEligible(target) && target.provider !== JEV_PROVIDER_ID && jevChoiceEfforts(config, target, false) !== undefined));
785
804
  }
786
805
  const fallbackFor = (choice: JevComboChoice): Pick<JevDecision, "targetKey" | "effort"> => {
@@ -894,7 +913,7 @@ export async function executeComboResponses(
894
913
  });
895
914
  // A completed decision belongs to the parent log even if no model can be dispatched.
896
915
  if (selectionUnavailable) {
897
- return comboUnavailable(comboId, undefined, comboExclusionReasons(config, comboId, target =>
916
+ return comboUnavailable(comboId, undefined, comboExclusion(config, comboId, target =>
898
917
  targetEligible(target) && target.provider !== JEV_PROVIDER_ID && jevChoiceEfforts(config, target, false) !== undefined));
899
918
  }
900
919
  }
@@ -970,12 +989,13 @@ export async function executeComboResponses(
970
989
  const returnsQuota = lastFailure && [400, 401, 403].includes(lastFailure.status) && quotaFailure;
971
990
  const failedChildLog = returnsQuota ? quotaFailedChildLog : lastFailedChildLog;
972
991
  if (failedChildLog) adoptFailedChildLog(failedChildLog);
973
- if (targetsExhausted && toolChoiceRefusalsOnly === true) return comboUnavailableResponse("No available targets for combo; attempted targets cannot preserve mandatory tool_choice");
992
+ if (targetsExhausted && toolChoiceRefusalsOnly === true) return comboUnavailableResponse("No available targets for combo; attempted targets cannot preserve mandatory tool_choice", { diagnostics: unsupportedToolChoice(comboTargetsDispatched) });
974
993
  if (!lastFailure || ![400, 401, 403].includes(lastFailure.status)) return lastFailure!;
975
994
  if (quotaFailure) return quotaFailure;
976
995
  // The quota-refused target was cooled before this request picked, so the ladder never saw
977
996
  // its refusal. The cooldown, with its Retry-After, is still the honest answer.
978
997
  let quotaCooldownUntil: number | undefined;
998
+ let quotaCooledTargets = 0;
979
999
  const now = Date.now();
980
1000
  for (const { target, cooldownUntil } of quotaCooldownSnapshot) {
981
1001
  if (cooldownUntil <= now) continue;
@@ -986,11 +1006,15 @@ export async function executeComboResponses(
986
1006
  continue;
987
1007
  }
988
1008
  quotaCooldownUntil = Math.min(quotaCooldownUntil ?? cooldownUntil, cooldownUntil);
1009
+ quotaCooledTargets += 1;
989
1010
  }
990
1011
  const remainingQuotaCooldownMs = quotaCooldownUntil === undefined ? 0 : quotaCooldownUntil - Date.now();
991
1012
  return remainingQuotaCooldownMs > 0
992
1013
  ? comboUnavailableResponse("No available targets for combo", {
993
1014
  retryAfter: String(Math.max(1, Math.ceil(remainingQuotaCooldownMs / 1000))),
1015
+ // Only quota-cooled eligible targets can produce this answer, so its class is quota.
1016
+ diagnostics: comboUnavailableDiagnostics(combo.targets.length, comboTargetsDispatched,
1017
+ Array.from({ length: quotaCooledTargets }, () => "quota" as const)),
994
1018
  }) : lastFailure;
995
1019
  };
996
1020
  // The caller's output ceiling, captured once so a floor applied to one target never
@@ -1093,15 +1117,21 @@ export async function executeComboResponses(
1093
1117
  ...(logCtx.affinityLane ? { affinityLane: logCtx.affinityLane } : {}),
1094
1118
  ...(logCtx.surface ? { surface: logCtx.surface } : {}),
1095
1119
  };
1120
+ // A link turn's child reads its upstream only as the client drains, as the parent would (#8601).
1121
+ bindLinkTurnChild(logCtx, childLog);
1096
1122
  const targetReasoningEfforts = supportedLadderFor({
1097
1123
  provider: targetRoute.provider,
1098
1124
  modelId: targetRoute.modelId,
1099
1125
  });
1100
1126
  const initialJevDecision = firstComboTarget ? jevDecision : undefined;
1127
+ // The effort a decision owns on this dispatch: its own first request, or the turn a
1128
+ // tool-loop continuation resumes. Failover targets keep ordinary Combo policy either way.
1129
+ const turnEffort: ComboTurnEffort | undefined = initialJevDecision
1130
+ ?? (firstComboTarget ? toolLoopTurnEffort : undefined);
1101
1131
  // `client` keeps the decision's target but not its tier: the caller and the target ladder
1102
1132
  // own the wire effort, so a provider whose effort swaps the upstream model id is never
1103
1133
  // re-tiered mid-session and its prompt cache survives the turn.
1104
- const jevEffortApplies = initialJevDecision !== undefined && combo.jevEffortMode !== "client";
1134
+ const jevEffortApplies = turnEffort !== undefined && combo.jevEffortMode !== "client";
1105
1135
  const childInput = options.droidDefaultEffort && isPlainObject(body) ? { ...body } : body;
1106
1136
  applyDroidResponsesReasoningDefault(childInput, options.droidDefaultEffort, {
1107
1137
  provider: targetRoute.provider,
@@ -1110,21 +1140,21 @@ export async function executeComboResponses(
1110
1140
  const childBody = concreteComboRequestBody(
1111
1141
  childInput,
1112
1142
  pick.target,
1113
- jevEffortApplies ? initialJevDecision.effort : comboDefaultEffort(config, comboId),
1114
- jevEffortApplies && initialJevDecision.effort === null ? [] : targetReasoningEfforts,
1143
+ jevEffortApplies ? turnEffort.effort : comboDefaultEffort(config, comboId),
1144
+ jevEffortApplies && turnEffort.effort === null ? [] : targetReasoningEfforts,
1115
1145
  combo.reasoningEffortMode,
1116
1146
  jevEffortApplies
1117
- ? (initialJevDecision.effort === null ? "fallback" : "force")
1147
+ ? (turnEffort.effort === null ? "fallback" : "force")
1118
1148
  : combo.defaultEffortMode,
1119
1149
  );
1120
- if (initialJevDecision) {
1150
+ if (turnEffort) {
1121
1151
  if (jevEffortApplies) delete childBody.service_tier;
1122
- if (jevEffortApplies && initialJevDecision.effort !== null) {
1152
+ if (jevEffortApplies && turnEffort.effort !== null) {
1123
1153
  const childReasoning = childBody.reasoning;
1124
1154
  const preservedReasoning = childReasoning && typeof childReasoning === "object" && !Array.isArray(childReasoning)
1125
1155
  ? childReasoning as Record<string, unknown>
1126
1156
  : {};
1127
- childBody.reasoning = { ...preservedReasoning, effort: initialJevDecision.effort };
1157
+ childBody.reasoning = { ...preservedReasoning, effort: turnEffort.effort };
1128
1158
  delete childBody.reasoning_effort;
1129
1159
  delete childBody.thinking_budget;
1130
1160
  delete childBody.thinking;
@@ -1158,6 +1188,10 @@ export async function executeComboResponses(
1158
1188
  childLog.activeAttemptStartedAt = started;
1159
1189
  childLog.attempts = logCtx.attempts ??= [];
1160
1190
  childLog.attempts.push(attempt);
1191
+ // A key or account rotation inside this target seals the previous segment as its own attempt
1192
+ // and moves `childLog.activeAttemptStartedAt` forward (noteProviderAttemptSend). Timing the
1193
+ // live segment from the target's start would count every sealed segment a second time.
1194
+ const segmentStartedAt = (): number => childLog.activeAttemptStartedAt ?? started;
1161
1195
  let attemptRetained = false;
1162
1196
  const retainCancelledAttempt = (): void => {
1163
1197
  if (attemptRetained) return;
@@ -1167,7 +1201,7 @@ export async function executeComboResponses(
1167
1201
  childLog.providerAdapter ?? attempt.adapter,
1168
1202
  childLog.accountLogLabel,
1169
1203
  );
1170
- finishRequestAttempt(attempt, 499, Date.now() - started, childLog.usage);
1204
+ finishRequestAttempt(attempt, 499, Date.now() - segmentStartedAt(), childLog.usage);
1171
1205
  attemptRetained = true;
1172
1206
  };
1173
1207
  // The row is written from `logCtx`, which copies `childLog` only when the child commits. A
@@ -1197,7 +1231,10 @@ export async function executeComboResponses(
1197
1231
  && Object.hasOwn(config.providers, completedTarget.provider)
1198
1232
  && provider && provider.disabled !== true
1199
1233
  && currentCombo?.targets.some(target => targetKey(target) === targetKey(completedTarget))) {
1200
- rememberComboForLane(sessionLaneIdFromRequest(req.headers), comboId, completedTarget, model, writerGeneration);
1234
+ rememberComboForLane(
1235
+ sessionLaneIdFromRequest(req.headers), comboId, completedTarget, model, writerGeneration,
1236
+ jevEffortApplies ? turnEffort : undefined,
1237
+ );
1201
1238
  jevPin?.complete(targetKey(completedTarget), initialJevDecision, writerGeneration);
1202
1239
  }
1203
1240
  options.onResponseComplete?.(model);
@@ -1228,7 +1265,7 @@ export async function executeComboResponses(
1228
1265
  && targetEligible(target)
1229
1266
  && !isComboTargetInCooldown(comboId, target),
1230
1267
  );
1231
- const nativeChild = protocolLanes?.nativeChild(pick.target, targetRoute, targetSendBudget, initialJevDecision, jevEffortApplies);
1268
+ const nativeChild = protocolLanes?.nativeChild(pick.target, targetRoute, targetSendBudget, turnEffort, jevEffortApplies);
1232
1269
  response = nativeChild ? await dispatchNativeComboChild({
1233
1270
  source: options.protocolSource!,
1234
1271
  plan: nativeChild,
@@ -1241,7 +1278,7 @@ export async function executeComboResponses(
1241
1278
  // Attempt-relative TTFT, recorded here for the same reason as the bridge child below.
1242
1279
  onFirstOutput: () => {
1243
1280
  if (attempt.firstOutputMs === undefined) {
1244
- attempt.firstOutputMs = Math.max(0, Date.now() - started);
1281
+ attempt.firstOutputMs = Math.max(0, Date.now() - segmentStartedAt());
1245
1282
  }
1246
1283
  options.onFirstOutput?.();
1247
1284
  },
@@ -1272,7 +1309,7 @@ export async function executeComboResponses(
1272
1309
  // Object.assign(logCtx, childLog) would overwrite the request-relative value).
1273
1310
  onFirstOutput: () => {
1274
1311
  if (attempt.firstOutputMs === undefined) {
1275
- attempt.firstOutputMs = Math.max(0, Date.now() - started);
1312
+ attempt.firstOutputMs = Math.max(0, Date.now() - segmentStartedAt());
1276
1313
  }
1277
1314
  options.onFirstOutput?.();
1278
1315
  },
@@ -1285,7 +1322,7 @@ export async function executeComboResponses(
1285
1322
  onResponseComplete: callbackGate.onResponseComplete,
1286
1323
  });
1287
1324
  // Both lanes applied the initial choice before provider pins/caps; retain those transitions.
1288
- restoreOriginalRequestedEffort(childLog, jevEffortApplies ? initialJevDecision?.effort : undefined);
1325
+ restoreOriginalRequestedEffort(childLog, jevEffortApplies ? turnEffort?.effort : undefined);
1289
1326
  } catch (error) {
1290
1327
  callbackGate.discard();
1291
1328
  if (options.abortSignal?.aborted) {
@@ -1293,7 +1330,7 @@ export async function executeComboResponses(
1293
1330
  return clientCancelledResponse();
1294
1331
  }
1295
1332
  jevPin?.forget();
1296
- finishRequestAttempt(attempt, 502, Date.now() - started, childLog.usage);
1333
+ finishRequestAttempt(attempt, 502, Date.now() - segmentStartedAt(), childLog.usage);
1297
1334
  throw error;
1298
1335
  }
1299
1336
 
@@ -1321,7 +1358,7 @@ export async function executeComboResponses(
1321
1358
  return clientCancelledResponse();
1322
1359
  }
1323
1360
  jevPin?.forget();
1324
- finishRequestAttempt(attempt, 502, Date.now() - started, childLog.usage);
1361
+ finishRequestAttempt(attempt, 502, Date.now() - segmentStartedAt(), childLog.usage);
1325
1362
  throw error;
1326
1363
  }
1327
1364
  if (preflight.kind === "failed") {
@@ -1363,7 +1400,7 @@ export async function executeComboResponses(
1363
1400
  routeDecision: logCtx.routeDecision,
1364
1401
  attempts: logCtx.attempts,
1365
1402
  activeAttempt: attempt,
1366
- activeAttemptStartedAt: started,
1403
+ activeAttemptStartedAt: segmentStartedAt(),
1367
1404
  resolvedModel: childLog.resolvedModel ?? childLog.model,
1368
1405
  });
1369
1406
  options.onCodexAuthContextResolved?.(resolvedAuth);
@@ -1391,7 +1428,7 @@ export async function executeComboResponses(
1391
1428
  return clientCancelledResponse();
1392
1429
  }
1393
1430
  jevPin?.forget();
1394
- finishRequestAttempt(attempt, 502, Date.now() - started, childLog.usage);
1431
+ finishRequestAttempt(attempt, 502, Date.now() - segmentStartedAt(), childLog.usage);
1395
1432
  throw error;
1396
1433
  }
1397
1434
  if (options.abortSignal?.aborted) {
@@ -1411,7 +1448,7 @@ export async function executeComboResponses(
1411
1448
  finishRequestAttempt(
1412
1449
  attempt,
1413
1450
  failure.response.status,
1414
- Date.now() - started,
1451
+ Date.now() - segmentStartedAt(),
1415
1452
  failure.usage ?? childLog.usage,
1416
1453
  );
1417
1454
  if (isReplayRefusalResponse(failure.response)) {
@@ -1463,6 +1500,7 @@ export async function executeComboResponses(
1463
1500
  verifiedPlanRefusal: failure.verifiedPlanRefusal,
1464
1501
  malformedResponseFormat: failure.malformedResponseFormat,
1465
1502
  openRouterOpaque400: failure.openRouterOpaque400,
1503
+ unofferedToolCallRefusal: failure.unofferedToolCallRefusal,
1466
1504
  providerModerationRefusal: failure.providerModerationRefusal,
1467
1505
  // The adapter the failed leg actually resolved: a configured `kiro` provider can route a
1468
1506
  // model through another wire via `modelAdapters`, and an unresolved leg grants no hop.
@@ -1542,6 +1580,7 @@ export async function executeComboResponses(
1542
1580
  code: failure.upstreamCode,
1543
1581
  malformedResponseFormat: failure.malformedResponseFormat,
1544
1582
  openRouterOpaque400: failure.openRouterOpaque400,
1583
+ unofferedToolCallRefusal: failure.unofferedToolCallRefusal,
1545
1584
  providerModerationRefusal: failure.providerModerationRefusal,
1546
1585
  });
1547
1586
  const failedTargetKey = targetKey(pick.target);
@@ -1,4 +1,4 @@
1
- import type { ComboExclusionReasons } from "../../combos/resolve";
1
+ import type { ComboExclusion, ComboUnavailableDiagnostics } from "../../combos/resolve";
2
2
  import { markProxyError } from "../../lib/client-error-body";
3
3
  import { appendClientRetryIn, formatClientRetryAfter } from "../../lib/client-retry-after";
4
4
  import { readBoundedResponseBody } from "../../lib/bounded-body";
@@ -104,7 +104,7 @@ export function decodeRequestErrorResponse(err: unknown, label: string): Respons
104
104
 
105
105
  export function comboUnavailableResponse(
106
106
  message: string,
107
- options?: { retryAfter?: string | null; presentation?: "combo" },
107
+ options?: { retryAfter?: string | null; presentation?: "combo"; diagnostics?: ComboUnavailableDiagnostics },
108
108
  ): Response {
109
109
  const headers = new Headers({ "Content-Type": "application/json" });
110
110
  // Advice leaves in this proxy's format, whatever form the earliest cooldown was recorded in.
@@ -115,17 +115,20 @@ export function comboUnavailableResponse(
115
115
  return markProxyError(new Response(
116
116
  JSON.stringify({
117
117
  // Built from the header value itself, so an HTTP-date cannot make the body and header disagree.
118
- error: { message: appendClientRetryIn(message, advice), type: "server_error", code: "combo_unavailable" },
118
+ // `combo` carries the machine-readable cause class and target counts (#7965).
119
+ error: { message: appendClientRetryIn(message, advice), type: "server_error", code: "combo_unavailable",
120
+ ...(options?.diagnostics ? { combo: options.diagnostics } : {}) },
119
121
  }),
120
122
  { status: 503, headers },
121
123
  ), options?.presentation);
122
124
  }
123
125
 
124
126
 
125
- export function comboUnavailable(comboId: string, now = Date.now(), reasons?: ComboExclusionReasons): Response {
126
- return comboUnavailableResponse(new NoAvailableComboTargetsError(comboId, reasons).message, {
127
+ export function comboUnavailable(comboId: string, now = Date.now(), exclusion?: ComboExclusion): Response {
128
+ return comboUnavailableResponse(new NoAvailableComboTargetsError(comboId, exclusion).message, {
127
129
  retryAfter: comboCooldownRetryAfterSeconds(comboId, now),
128
- presentation: reasons === undefined ? undefined : "combo",
130
+ presentation: exclusion === undefined ? undefined : "combo",
131
+ ...(exclusion ? { diagnostics: exclusion.diagnostics } : {}),
129
132
  });
130
133
  }
131
134
 
@@ -37,6 +37,8 @@ export interface ConsumedComboFailure {
37
37
  malformedResponseFormat?: boolean;
38
38
  /** Complete bounded-envelope OpenRouter opaque-400 verdict captured before display truncation. */
39
39
  openRouterOpaque400?: boolean;
40
+ /** Complete bounded-envelope unoffered-tool-call verdict captured before display truncation. */
41
+ unofferedToolCallRefusal?: boolean;
40
42
  /** Complete bounded-envelope provider moderation verdict captured before display truncation. */
41
43
  providerModerationRefusal?: boolean;
42
44
  /** Structured upstream `error.code` when present in the failure body. */
@@ -226,6 +228,14 @@ export interface HandleResponsesOptions {
226
228
  protocolSource?: import("./core-combo-native").ComboProtocolSource;
227
229
  /** Internal transport identity for route-scoped upstream compatibility policy. */
228
230
  inboundTransport?: "websocket";
231
+ /**
232
+ * Set by the HTTP `/v1/responses` ingress only: a streaming combo turn whose target selection
233
+ * outlasts `COMBO_SELECTION_KEEPALIVE_THRESHOLD_MS` commits a 200 SSE head and sends keepalive
234
+ * comments (#8326). `clock` is a test seam; production uses the global timers.
235
+ */
236
+ comboSelectionKeepalive?: {
237
+ clock?: { setTimeout(callback: () => void, ms: number): unknown; clearTimeout(handle: unknown): void };
238
+ };
229
239
  /**
230
240
  * Claude replay may add native-main auth so OpenAI sidecars remain available.
231
241
  * Strip only that internal credential when the final route is a noncanonical
@@ -12,6 +12,11 @@ import { createInferenceSendBudget } from "../inference/context";
12
12
  import { finalizeOwnedTranslatorBudget, finalizeAccountLease } from "./core-lifetime";
13
13
  import type { TranslatorBudget } from "../../lib/translator-budget";
14
14
  import { executeComboResponses } from "./core-combo";
15
+ import {
16
+ comboSelectionKeepaliveApplies,
17
+ comboSelectionKeepaliveLadderOptions,
18
+ selectComboWithKeepalive,
19
+ } from "./combo-selection-keepalive";
15
20
  import { registerTurn, unregisterTurn } from "../lifecycle";
16
21
  import { runWithCompactionRecovery } from "./compaction-recovery";
17
22
  import { captureAdmissionPolicy } from "../capture-admission-policy";
@@ -71,6 +76,22 @@ export async function handleComboResponses(
71
76
  config: OcxConfig,
72
77
  logCtx: RequestLogContext,
73
78
  options: HandleResponsesOptions & { translatorBudget: TranslatorBudget },
79
+ ): Promise<Response> {
80
+ if (!comboSelectionKeepaliveApplies(rawBody, options)) return runComboLadder(req, rawBody, comboId, config, logCtx, options);
81
+ // A slow selection may answer with a keepalive head while the ladder below keeps running (#8326).
82
+ const model = (rawBody as { model?: unknown }).model;
83
+ return selectComboWithKeepalive(
84
+ hooks => runComboLadder(req, rawBody, comboId, config, logCtx, comboSelectionKeepaliveLadderOptions(options, hooks)),
85
+ { model: typeof model === "string" ? model : comboId, logCtx, signal: options.abortSignal, clock: options.comboSelectionKeepalive?.clock },
86
+ );
87
+ }
88
+ async function runComboLadder(
89
+ req: Request,
90
+ rawBody: unknown,
91
+ comboId: string,
92
+ config: OcxConfig,
93
+ logCtx: RequestLogContext,
94
+ options: HandleResponsesOptions & { translatorBudget: TranslatorBudget },
74
95
  ): Promise<Response> {
75
96
  // Each child relay binds the turn's lease and unbinds it when its stream ends; the lease is
76
97
  // released once nothing is bound. Hold it for the whole ladder so a refused child's relay
@@ -0,0 +1,115 @@
1
+ import { estimateTokens } from "../../lib/token-estimate";
2
+ import type { SsePayloadRewrite } from "../sse-payload-rewrite";
3
+ import type { HandleResponsesOptions } from "./core-options";
4
+ import type { OcxProviderConfig } from "../../types";
5
+
6
+ /**
7
+ * Response header marking estimated usage (#7427). `true`: the JSON body's usage is an estimate.
8
+ * `when-missing`: the stream's usage is estimated wherever the provider omitted it.
9
+ */
10
+ export const ESTIMATED_USAGE_HEADER = "x-opencodex-usage-estimated";
11
+
12
+ /** Terminal events whose `response.usage` a client reads for context accounting. */
13
+ const TERMINAL_TYPES = new Set(["response.completed", "response.incomplete"]);
14
+ /** Response statuses that end a turn with output a client counts; failed and running ones do not. */
15
+ const TERMINAL_STATUSES = new Set(["completed", "incomplete"]);
16
+
17
+ /**
18
+ * Whether this turn may carry an estimate: the provider opted in, and the client spoke HTTP
19
+ * Responses. The Chat Completions and Claude Messages bridges and the WebSocket ingress rebuild
20
+ * their own usage fields from the response and would pass an estimate on without the marker, so
21
+ * they never get one. An internal decision call (JEV) settles spend from its response body, so it
22
+ * never gets one either.
23
+ */
24
+ export function estimatesMissingUsage(
25
+ provider: Pick<OcxProviderConfig, "estimateMissingUsage">,
26
+ options: Pick<HandleResponsesOptions, "inboundWire" | "inboundTransport" | "internalDecisionCall">,
27
+ ): boolean {
28
+ return provider.estimateMissingUsage === true
29
+ && (options.inboundWire ?? "responses") === "responses"
30
+ && options.inboundTransport !== "websocket"
31
+ && options.internalDecisionCall !== true;
32
+ }
33
+
34
+ function isRecord(value: unknown): value is Record<string, unknown> {
35
+ return value !== null && typeof value === "object" && !Array.isArray(value);
36
+ }
37
+
38
+ /** Text a model produced in a Responses `output` array: messages, refusals, tool input, summaries. */
39
+ function outputText(output: unknown): string {
40
+ if (!Array.isArray(output)) return "";
41
+ const parts: string[] = [];
42
+ for (const item of output) {
43
+ if (!isRecord(item)) continue;
44
+ for (const field of ["arguments", "input", "name"] as const) {
45
+ if (typeof item[field] === "string") parts.push(item[field] as string);
46
+ }
47
+ for (const list of [item.content, item.summary]) {
48
+ if (!Array.isArray(list)) continue;
49
+ for (const part of list) {
50
+ if (!isRecord(part)) continue;
51
+ if (typeof part.text === "string") parts.push(part.text);
52
+ if (typeof part.refusal === "string") parts.push(part.refusal);
53
+ }
54
+ }
55
+ }
56
+ return parts.join("\n");
57
+ }
58
+
59
+ /**
60
+ * The response with an estimated `usage` when the provider sent none, or `undefined` when it has
61
+ * usage already or is not a response object. Input comes from the request estimate, output from
62
+ * the response's own output items. A client's context meter reads it; OpenCodex's cost, spend and
63
+ * quota accounting never does, because they read the provider's original body.
64
+ */
65
+ export function withEstimatedUsage(
66
+ response: unknown, inputTokens: number, modelId: string, streamedOutputTokens?: number,
67
+ ): Record<string, unknown> | undefined {
68
+ // Only a finished Responses object with output; error bodies, failed turns and partial events
69
+ // are left alone even when they arrive with HTTP 200.
70
+ if (!isRecord(response) || isRecord(response.usage) || !Array.isArray(response.output)) return undefined;
71
+ if (typeof response.status !== "string" || !TERMINAL_STATUSES.has(response.status)) return undefined;
72
+ if (response.error !== undefined && response.error !== null) return undefined;
73
+ const input = Math.max(0, Math.round(inputTokens));
74
+ // A terminal that closes with an empty list counts the items streamed before it instead.
75
+ const output = response.output.length === 0 && streamedOutputTokens !== undefined
76
+ ? streamedOutputTokens
77
+ : estimateTokens(outputText(response.output), modelId);
78
+ return { ...response, usage: { input_tokens: input, output_tokens: output, total_tokens: input + output } };
79
+ }
80
+
81
+ /**
82
+ * SSE payload rewrite for opted-in native routes; `onApplied` fires once per estimated terminal.
83
+ * Each `response.output_item.done` adds its estimate to one running count, so a terminal whose
84
+ * `output` is empty is still estimated without the stream retaining any item.
85
+ */
86
+ export function createEstimatedUsagePayloadRewrite(
87
+ inputTokens: () => number, modelId: string, onApplied?: () => void,
88
+ ): SsePayloadRewrite {
89
+ let streamedOutputTokens = 0;
90
+ return payload => {
91
+ // Cheap gate: only finished items and terminal events are parsed, never every delta.
92
+ const itemDone = payload.includes("response.output_item.done");
93
+ if (!itemDone && !payload.includes("response.completed") && !payload.includes("response.incomplete")) return payload;
94
+ let parsed: unknown;
95
+ try { parsed = JSON.parse(payload); } catch { return payload; }
96
+ if (!isRecord(parsed) || typeof parsed.type !== "string") return payload;
97
+ if (parsed.type === "response.output_item.done") {
98
+ streamedOutputTokens += estimateTokens(outputText([parsed.item]), modelId);
99
+ return payload;
100
+ }
101
+ if (!TERMINAL_TYPES.has(parsed.type)) return payload;
102
+ const response = withEstimatedUsage(parsed.response, inputTokens(), modelId, streamedOutputTokens);
103
+ if (!response) return payload;
104
+ onApplied?.();
105
+ return JSON.stringify({ ...parsed, response });
106
+ };
107
+ }
108
+
109
+ /** The JSON body with estimated usage, or `undefined` when it already had usage or is not JSON. */
110
+ export function estimatedUsageJson(json: string, inputTokens: number, modelId: string): string | undefined {
111
+ let parsed: unknown;
112
+ try { parsed = JSON.parse(json); } catch { return undefined; }
113
+ const response = withEstimatedUsage(parsed, inputTokens, modelId);
114
+ return response ? JSON.stringify(response) : undefined;
115
+ }
@@ -11,6 +11,7 @@ import { clientFacingModelText, type ClientFacingModelLabel } from "../../lib/er
11
11
  * catches the pathological case and stays out of the way otherwise. Every uncertainty
12
12
  * resolves toward admitting.
13
13
  */
14
+ import { collapseDevinModelUid, DEVIN_MODEL_CONTEXT_WINDOWS } from "../../adapters/devin/live-models";
14
15
  import {
15
16
  nativeOpenAiContextWindow,
16
17
  nativeOpenAiMaxInputTokens,
@@ -171,6 +172,35 @@ interface ResolvedContextLimits {
171
172
  ceiling: number | null;
172
173
  }
173
174
 
175
+ /**
176
+ * Devin spells one model dotted in catalog families (`swe-1.7`) and hyphenated in uids
177
+ * (`swe-1-7`), with optional effort suffixes (`swe-2-flash-medium`). A per-model record is
178
+ * matched on the exact id first, then the effort-collapsed id, then both hyphenated forms, and
179
+ * finally on record keys folded the same way (`swe-1.7` serves a `swe-1-7-medium` request), so
180
+ * an operator override on any of those spellings binds before a provider-wide default.
181
+ */
182
+ function devinRecordValue(record: Record<string, number> | undefined, modelId: string): number | null {
183
+ const normalized = modelId.replace(/\./g, "-");
184
+ // Lower-case before collapsing so an upper-case effort suffix (SWE-1-7-MEDIUM) is removed too.
185
+ const collapsed = collapseDevinModelUid(normalized.toLowerCase());
186
+ for (const key of new Set([modelId, collapseDevinModelUid(modelId), normalized, collapsed])) {
187
+ const value = positive(modelRecordValue(record, key));
188
+ if (value !== null) return value;
189
+ }
190
+ if (!record) return null;
191
+ // Exact spellings missed; fold dotted record keys only now so an exact key always wins.
192
+ for (const target of new Set([normalized, collapsed])) {
193
+ // Fold case too, like modelRecordValue's exact-spelling lookup.
194
+ const folded = target.toLowerCase();
195
+ for (const [key, raw] of Object.entries(record)) {
196
+ if (key.replace(/\./g, "-").toLowerCase() !== folded) continue;
197
+ const value = positive(raw);
198
+ if (value !== null) return value;
199
+ }
200
+ }
201
+ return null;
202
+ }
203
+
174
204
  function resolveContextLimits(
175
205
  provider: OcxProviderConfig,
176
206
  providerName: string,
@@ -180,7 +210,10 @@ function resolveContextLimits(
180
210
  // `modelRecordValue`, not a bare lookup: the catalog resolves these same two maps that
181
211
  // way, so a `gpt-oss` entry covers `gpt-oss:120b`. Reading raw here made the gate fall
182
212
  // back to the provider-wide window and refuse turns the model can plainly hold.
183
- const configured = positive(modelRecordValue(provider.modelContextWindows, modelId))
213
+ const devin = provider.adapter === "devin";
214
+ const configured = (devin
215
+ ? devinRecordValue(provider.modelContextWindows, modelId)
216
+ : positive(modelRecordValue(provider.modelContextWindows, modelId)))
184
217
  ?? positive(provider.contextWindow);
185
218
 
186
219
  // The canonical `openai` registry entry declares no context fields, so without this the
@@ -202,9 +235,14 @@ function resolveContextLimits(
202
235
  : null;
203
236
  const nativeMaxInput = canonicalNativeBare ? positive(nativeOpenAiMaxInputTokens(modelId, nativeLimits)) : null;
204
237
 
205
- const window = canonicalNativeBare ? (native ?? generatedNativeWindow(modelId, configured, nativeContextCap)) : configured;
238
+ // Devin's live catalog windows reach the Codex model list but never `provider.modelContextWindows`;
239
+ // without the adapter table every devin combo target resolved no window and the strict gate stayed open.
240
+ const window = canonicalNativeBare ? (native ?? generatedNativeWindow(modelId, configured, nativeContextCap))
241
+ : (configured ?? (devin ? devinRecordValue(DEVIN_MODEL_CONTEXT_WINDOWS, modelId) : null));
206
242
  // modelMaxInputTokens is an input-only cap, so it can only tighten the window.
207
- const configuredMaxInput = positive(modelRecordValue(provider.modelMaxInputTokens, modelId));
243
+ const configuredMaxInput = devin
244
+ ? devinRecordValue(provider.modelMaxInputTokens, modelId)
245
+ : positive(modelRecordValue(provider.modelMaxInputTokens, modelId));
208
246
  const limits = [window, configuredMaxInput, nativeMaxInput].filter((v): v is number => v !== null);
209
247
  return { window, ceiling: limits.length === 0 ? null : Math.min(...limits) };
210
248
  }
@@ -12,6 +12,7 @@ import { tryAdmitTurn } from "../lifecycle";
12
12
  import type { RequestLogContext } from "../request-log";
13
13
  import { createSseInspector } from "../relay";
14
14
  import type { HandleResponsesOptions } from "./core-options";
15
+ import { ESTIMATED_USAGE_HEADER } from "./estimated-usage";
15
16
 
16
17
  type HandleResponses = (
17
18
  req: Request,
@@ -183,8 +184,12 @@ export function createJevModelInvoker(context: JevModelInvokerContext): JevModel
183
184
  if (!isRecord(parsed)) throw new JevModelInvokeError("malformed", "decision response is not an object");
184
185
  return terminalResult(parsed, "");
185
186
  })();
186
- usage = result.usage;
187
- return quotaOmitted ? { ...result, quotaOmitted: true } : result;
187
+ // A marked response carries an estimate for a client's display, never spend evidence.
188
+ const estimated = response.headers.has(ESTIMATED_USAGE_HEADER);
189
+ const { usage: bodyUsage, ...withoutUsage } = result;
190
+ const settled = estimated ? withoutUsage : result;
191
+ usage = estimated ? undefined : bodyUsage;
192
+ return quotaOmitted ? { ...settled, quotaOmitted: true } : settled;
188
193
  } finally {
189
194
  // No final log row is written for this detached context, so settle its spend here.
190
195
  childLog.spendTracker?.settle(childLog.usage ?? (usage