@bitkyc08/opencodex 2.94.0 → 2.95.0

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/gui/dist/assets/App-8yedBkhm.js +58 -0
  2. package/gui/dist/assets/App-B4j-Txmb.css +1 -0
  3. package/gui/dist/assets/{Tray-CZatQdoF.js → Tray-erSMzxPG.js} +1 -1
  4. package/gui/dist/assets/index-D7DCm5eQ.js +94 -0
  5. package/gui/dist/assets/quota-summary-BK4VuQNc.js +1 -0
  6. package/gui/dist/index.html +1 -1
  7. package/package.json +1 -1
  8. package/src/adapters/anthropic.ts +10 -3
  9. package/src/adapters/command-code-project-context.ts +3 -3
  10. package/src/adapters/kiro/adapter.ts +4 -1
  11. package/src/adapters/kiro/stream.ts +1 -0
  12. package/src/adapters/kiro-events.ts +1 -0
  13. package/src/adapters/openai-chat/messages.ts +20 -7
  14. package/src/adapters/openai-chat/passthrough.ts +4 -2
  15. package/src/adapters/openai-chat/single-system.ts +24 -0
  16. package/src/adapters/openai-responses/muse-tool-catalog.ts +87 -0
  17. package/src/adapters/openai-responses/passthrough.ts +42 -7
  18. package/src/adapters/openai-responses/prompt-cache.ts +3 -1
  19. package/src/adapters/openai-responses/request-strips.ts +23 -4
  20. package/src/adapters/openai-responses/tool-output-recovery.ts +21 -8
  21. package/src/adapters/openai-responses/tool-schema.ts +20 -8
  22. package/src/adapters/opencode-go-additional-tools.ts +39 -6
  23. package/src/adapters/routed-agent-messages.ts +3 -0
  24. package/src/adapters/xai-web-search.ts +6 -3
  25. package/src/adapters/zed-additional-tools.ts +7 -7
  26. package/src/adapters/zed.ts +1 -6
  27. package/src/advisor/consult.ts +22 -10
  28. package/src/advisor/runtime.ts +10 -4
  29. package/src/bridge/max-output-completion.ts +41 -0
  30. package/src/bridge/response-json.ts +9 -4
  31. package/src/bridge/sse.ts +13 -2
  32. package/src/chatgpt/app-server-shim/bundle-trust-policy.ts +5 -0
  33. package/src/chatgpt/app-server-shim/launcher.ts +40 -6
  34. package/src/claude/compatibility.ts +5 -1
  35. package/src/claude/desktop-3p.ts +27 -6
  36. package/src/claude/desktop-model-visibility.ts +141 -0
  37. package/src/claude/desktop-remote-store-io.ts +8 -8
  38. package/src/claude/inbound-content-options.ts +11 -1
  39. package/src/claude/intercept/proxy-auth.ts +16 -16
  40. package/src/cli/account-main.ts +3 -2
  41. package/src/cli/capabilities-provider-models.ts +7 -0
  42. package/src/cli/chatgpt-command.ts +1 -1
  43. package/src/cli/codex-launcher.ts +20 -8
  44. package/src/cli/codex-shim-autorestore.ts +19 -1
  45. package/src/cli/dispatch.ts +6 -6
  46. package/src/cli/doctor.ts +5 -5
  47. package/src/cli/index.ts +4 -9
  48. package/src/cli/init.ts +2 -2
  49. package/src/cli/internal-command.ts +29 -2
  50. package/src/cli/provider-cc-switch-import.ts +185 -0
  51. package/src/cli/provider.ts +5 -0
  52. package/src/cli/registry.ts +1 -0
  53. package/src/cli/restart-scope.ts +2 -2
  54. package/src/cli/root.ts +6 -6
  55. package/src/cli/stop-approval.ts +15 -1
  56. package/src/cli/v2.ts +69 -19
  57. package/src/client/machine-api.ts +2 -2
  58. package/src/clients/config-export.ts +14 -3
  59. package/src/codex/account-cooldown-response.ts +18 -0
  60. package/src/codex/app-server-restart-service.ts +2 -2
  61. package/src/codex/auth-api/login-flow.ts +2 -2
  62. package/src/codex/auth-api/pool-quota-probe.ts +2 -2
  63. package/src/codex/auth-context.ts +4 -4
  64. package/src/codex/catalog/bundled.ts +10 -3
  65. package/src/codex/catalog/metadata.ts +4 -5
  66. package/src/codex/catalog/native-omission.ts +28 -0
  67. package/src/codex/catalog/native-template.ts +58 -0
  68. package/src/codex/catalog/parsing.ts +3 -29
  69. package/src/codex/catalog/provider-models.ts +2 -1
  70. package/src/codex/catalog/retained-sync.ts +15 -5
  71. package/src/codex/catalog/write-audit.ts +18 -18
  72. package/src/codex/cli-install-provenance.ts +46 -45
  73. package/src/codex/cli-update-artifacts.ts +8 -6
  74. package/src/codex/cli-update-job-store-windows.ts +99 -27
  75. package/src/codex/cli-update-job-worker.ts +3 -11
  76. package/src/codex/cli-update-recovery.ts +54 -8
  77. package/src/codex/cli-update-selection-gate.ts +74 -4
  78. package/src/codex/cli-windows-plan-provenance.ts +37 -19
  79. package/src/codex/codex-home-owner.ts +9 -8
  80. package/src/codex/convergence.ts +13 -3
  81. package/src/codex/coordinator-doctor.ts +14 -8
  82. package/src/codex/desktop-app/handoff.ts +1 -1
  83. package/src/codex/desktop-app-restart.ts +11 -1
  84. package/src/codex/exec-boundary.ts +26 -5
  85. package/src/codex/history-provider.ts +11 -10
  86. package/src/codex/inject/bounded-config-reader.ts +14 -13
  87. package/src/codex/main-account.ts +6 -6
  88. package/src/codex/native-main-lock-file.ts +13 -12
  89. package/src/codex/native-residue.ts +18 -14
  90. package/src/codex/orca-auth-source.ts +8 -7
  91. package/src/codex/quota-types.ts +5 -0
  92. package/src/codex/quota.ts +49 -7
  93. package/src/codex/role-auto-assign.ts +50 -26
  94. package/src/codex/role-capability.ts +160 -0
  95. package/src/codex/role-sizing.ts +24 -6
  96. package/src/codex/routing/cooldown-math.ts +3 -2
  97. package/src/codex/runtime.ts +43 -3
  98. package/src/codex/shim-fingerprint.ts +11 -9
  99. package/src/codex/shim-inspect.ts +3 -2
  100. package/src/codex/shim-restore-lock.ts +11 -14
  101. package/src/codex/shim-state-file.ts +22 -14
  102. package/src/codex/shim-wrapper-write.ts +7 -6
  103. package/src/codex/shim.ts +25 -2
  104. package/src/codex/transition-state.ts +5 -4
  105. package/src/combos/failover.ts +43 -4
  106. package/src/config/atomic-write.ts +38 -7
  107. package/src/config/diagnostics.ts +3 -0
  108. package/src/config/initialize.ts +6 -6
  109. package/src/config/load-degrade.ts +2 -0
  110. package/src/config/process-state.ts +23 -5
  111. package/src/config/salvage.ts +1 -0
  112. package/src/config/schema/config-schema.ts +29 -1
  113. package/src/config/schema/leaf-validators.ts +7 -0
  114. package/src/config/schema/workflow-budget.ts +43 -0
  115. package/src/config/tracked-runtime-inspection.ts +188 -0
  116. package/src/generated/compatibility-version.json +313 -213
  117. package/src/integrations/aside-profile-context.ts +1 -1
  118. package/src/integrations/droid-settings.ts +4 -4
  119. package/src/integrations/hermes-profiles.ts +3 -3
  120. package/src/lib/bounded-file-read.ts +3 -9
  121. package/src/lib/bun-runtime-preflight.ts +9 -8
  122. package/src/lib/file-identity.ts +15 -0
  123. package/src/lib/local-chat-completion.ts +8 -6
  124. package/src/lib/local-destinations.ts +32 -3
  125. package/src/lib/redact-folding.ts +9 -0
  126. package/src/lib/spend-pool-alias-validation.ts +6 -6
  127. package/src/lib/state-store-registrations.ts +2 -0
  128. package/src/lib/test-home-guard.ts +37 -9
  129. package/src/lib/windows-secret-acl.ts +47 -29
  130. package/src/lib/workflow-budget-config.ts +39 -0
  131. package/src/local-credentials/generation.ts +6 -2
  132. package/src/local-credentials/inventory.ts +132 -8
  133. package/src/messaging/remote-forward-failure.ts +35 -0
  134. package/src/messaging/remote-ports.ts +27 -5
  135. package/src/messaging/remote-process.ts +30 -4
  136. package/src/messaging/remote-tunnels.ts +80 -15
  137. package/src/oauth/antigravity-discovery-diagnostics.ts +65 -0
  138. package/src/oauth/google-antigravity.ts +23 -10
  139. package/src/oauth/index.ts +2 -0
  140. package/src/oauth/kiro-device-login.ts +87 -30
  141. package/src/oauth/kiro-iam-identity-center.ts +107 -0
  142. package/src/oauth/store.ts +9 -7
  143. package/src/providers/account-quota-disk.ts +18 -4
  144. package/src/providers/cc-switch-import-bytes.ts +991 -0
  145. package/src/providers/cc-switch-import-source.ts +1083 -0
  146. package/src/providers/cc-switch-import.ts +942 -0
  147. package/src/providers/derive.ts +6 -0
  148. package/src/providers/model-discovery-diagnostic.ts +4 -0
  149. package/src/providers/model-discovery.ts +4 -4
  150. package/src/providers/model-rename-fields.ts +2 -0
  151. package/src/providers/model-rename-migration.ts +41 -6
  152. package/src/providers/quota/report-cache.ts +17 -4
  153. package/src/providers/quota.ts +2 -2
  154. package/src/providers/reasoning-metadata.ts +31 -15
  155. package/src/providers/registry/entries-core.ts +1 -1
  156. package/src/providers/registry/entries-extended.ts +1 -1
  157. package/src/providers/registry/model-ids.ts +1 -0
  158. package/src/providers/registry/model-seeds.ts +2 -1
  159. package/src/providers/registry/types.ts +3 -1
  160. package/src/providers/resolved-model-policy.ts +2 -2
  161. package/src/providers/routed-config.ts +2 -0
  162. package/src/providers/stale-model-roster-migration.ts +5 -1
  163. package/src/providers/xai-transport.ts +4 -4
  164. package/src/remote-control/workspace-codex-runtime.ts +2 -2
  165. package/src/remote-control/workspace-executor.ts +14 -13
  166. package/src/responses/custom-tool-compat.ts +2 -1
  167. package/src/responses/item-provenance.ts +79 -0
  168. package/src/responses/namespace-tool-compat.ts +21 -3
  169. package/src/router.ts +21 -2
  170. package/src/routing/analytics.ts +3 -5
  171. package/src/routing/fallback-policy.ts +7 -3
  172. package/src/routing/health.ts +5 -3
  173. package/src/routing/history/build-file.ts +6 -0
  174. package/src/routing/history/indexer.ts +158 -31
  175. package/src/routing/history/ingest.ts +1 -1
  176. package/src/routing/history/integrity.ts +208 -0
  177. package/src/routing/history/rebuild-job.ts +20 -6
  178. package/src/routing/history/rebuild-protocol.ts +2 -1
  179. package/src/routing/history/rebuild-worker.ts +18 -0
  180. package/src/routing/history/sqlite-errors.ts +9 -0
  181. package/src/server/admission-model-scope.ts +21 -4
  182. package/src/server/advisor-admission-scope.ts +29 -0
  183. package/src/server/auth-cors.ts +5 -2
  184. package/src/server/capture-admission-policy.ts +91 -0
  185. package/src/server/chat-completions.ts +33 -7
  186. package/src/server/claude-messages.ts +90 -2
  187. package/src/server/codex-model-list.ts +2 -1
  188. package/src/server/configured-credentials.ts +5 -0
  189. package/src/server/index/serve-options.ts +12 -2
  190. package/src/server/index/websocket-handler.ts +3 -0
  191. package/src/server/management/agent-settings-routes.ts +34 -9
  192. package/src/server/management/codex-agent-role-routes.ts +1 -0
  193. package/src/server/management/codex-role-auto-assign.ts +50 -6
  194. package/src/server/management/config-routes.ts +15 -4
  195. package/src/server/management/context.ts +3 -0
  196. package/src/server/management/native-integration-routes.ts +22 -0
  197. package/src/server/management/oauth-account-routes.ts +20 -4
  198. package/src/server/management/provider-overwrite-carry.ts +21 -0
  199. package/src/server/management/provider-routes.ts +3 -1
  200. package/src/server/management/route-registry.ts +2 -0
  201. package/src/server/management/shared.ts +17 -1
  202. package/src/server/management/workflow-budget-routes.ts +70 -3
  203. package/src/server/messages-native-eligibility.ts +6 -5
  204. package/src/server/messages-native-scope.ts +6 -1
  205. package/src/server/messages-shared.ts +57 -17
  206. package/src/server/persistent-gui-session.ts +4 -4
  207. package/src/server/request-log.ts +30 -2
  208. package/src/server/responses/adapter-delivery.ts +4 -0
  209. package/src/server/responses/advisor-plan-slot.ts +4 -0
  210. package/src/server/responses/advisor-slot.ts +73 -46
  211. package/src/server/responses/codex-auth-error.ts +5 -1
  212. package/src/server/responses/combo-required-tool-contract.ts +27 -0
  213. package/src/server/responses/compact.ts +4 -1
  214. package/src/server/responses/core-combo-failure.ts +2 -0
  215. package/src/server/responses/core-combo.ts +66 -5
  216. package/src/server/responses/core-lifetime.ts +4 -0
  217. package/src/server/responses/core-options.ts +9 -0
  218. package/src/server/responses/core.ts +2 -0
  219. package/src/server/responses/jev-model-invoke.ts +4 -2
  220. package/src/server/responses/passthrough-dispatch.ts +5 -4
  221. package/src/server/responses/policy-fallback.ts +2 -0
  222. package/src/server/responses/request-prepare.ts +4 -5
  223. package/src/server/responses/request-send-budget.ts +2 -1
  224. package/src/server/responses/request-sidecar-auth.ts +1 -0
  225. package/src/server/responses/request-transport.ts +2 -3
  226. package/src/server/responses/run-turn-execution.ts +4 -0
  227. package/src/server/responses/sidecar-execution.ts +21 -5
  228. package/src/server/responses-snapshot-repair.ts +18 -4
  229. package/src/server/responses-undeclared-tool-guard.ts +4 -0
  230. package/src/server/workflow-refusal.ts +2 -1
  231. package/src/service/state-lock.ts +12 -11
  232. package/src/service-manager-probe.ts +53 -8
  233. package/src/types/admission-policy.ts +13 -0
  234. package/src/types/config.ts +25 -0
  235. package/src/types/provider.ts +10 -0
  236. package/src/types/request.ts +7 -0
  237. package/src/types.ts +1 -0
  238. package/src/update/job.ts +20 -0
  239. package/src/update/worker-launch.ts +71 -1
  240. package/src/usage/log.ts +25 -4
  241. package/src/vision/index.ts +2 -0
  242. package/src/vision/plan.ts +4 -0
  243. package/src/vision/routed-describe.ts +38 -23
  244. package/src/web-search/loop.ts +104 -9
  245. package/src/web-search/parse.ts +10 -2
  246. package/gui/dist/assets/App-DMps4OLk.css +0 -1
  247. package/gui/dist/assets/App-DmvwlhQq.js +0 -58
  248. package/gui/dist/assets/index-3EPO_KbX.js +0 -94
  249. package/gui/dist/assets/quota-summary-BHvWg9mi.js +0 -1
@@ -10,6 +10,33 @@ export type RequiredToolContract =
10
10
  | { kind: "any"; hostedCallTypes: ReadonlySet<string> }
11
11
  | { kind: "named"; name: string };
12
12
 
13
+ const allowedHostedSelectors = new Set([
14
+ "web_search", "web_search_preview", "file_search", "computer_use_preview", "computer_use",
15
+ "code_interpreter", "image_generation", "image_gen", "tool_search",
16
+ ]);
17
+
18
+ /** Only definite, well-formed mandatory selectors justify excluding a destination. */
19
+ export function hasMandatoryToolChoice(rawBody: unknown): boolean {
20
+ if (!rawBody || typeof rawBody !== "object" || Array.isArray(rawBody)) return false;
21
+ const choice = (rawBody as Record<string, unknown>).tool_choice;
22
+ if (choice === "required") return true;
23
+ if (!choice || typeof choice !== "object" || Array.isArray(choice)) return false;
24
+ const selector = choice as Record<string, unknown>;
25
+ if (selector.type === "function" || selector.type === "custom") {
26
+ return typeof selector.name === "string" && selector.name.trim().length > 0;
27
+ }
28
+ return selector.type === "allowed_tools" && selector.mode === "required"
29
+ && Array.isArray(selector.tools) && selector.tools.length > 0
30
+ && selector.tools.every((tool: unknown) => {
31
+ if (!tool || typeof tool !== "object" || Array.isArray(tool)) return false;
32
+ const entry = tool as Record<string, unknown>;
33
+ if (entry.name !== undefined && (typeof entry.name !== "string" || !entry.name.trim())) return false;
34
+ if (entry.type === "function" || entry.type === "custom") return typeof entry.name === "string";
35
+ if (entry.type === "mcp") return typeof entry.server_label === "string" && !!entry.server_label.trim();
36
+ return typeof entry.type === "string" && allowedHostedSelectors.has(entry.type);
37
+ });
38
+ }
39
+
13
40
  const hostedCalls = new Map([
14
41
  ["web_search", "web_search_call"], ["web_search_preview", "web_search_call"],
15
42
  ["file_search", "file_search_call"], ["code_interpreter", "code_interpreter_call"],
@@ -1,5 +1,6 @@
1
1
  import { captureAccountAutoPauseAttempt, wasAccountAutoPaused, wasPermanentAccountRefusal } from "../../oauth/account-auto-pause";
2
2
  import { spendLedgerStorageErrorResponse } from "./spend-storage-error";
3
+ import { captureAdmissionPolicy } from "../capture-admission-policy";
3
4
  import { createPhysicalSendReporter, rebindPhysicalSend } from "../../lib/request-execution-budget";
4
5
  import { hasResponseMediaType } from "../../lib/http-response-semantics";
5
6
  import { createInferenceSendBudget } from "../inference/context";
@@ -319,6 +320,7 @@ function compactHandoffRoute(
319
320
  }
320
321
 
321
322
  export interface HandleResponsesCompactOptions {
323
+ advisorAdmissionPolicy?: import("../../types/admission-policy").CapturedAdmissionPolicy;
322
324
  compactionRoutingOverride?: CompactionRoutingOverride | null;
323
325
  nativeMainRefreshDependencies?: NativeMainRefreshDependencies;
324
326
  /** Release the listener's idle guard only after the complete request body is accepted. */
@@ -660,6 +662,7 @@ export async function handleResponsesCompact(
660
662
  admission?: DataPlaneAdmission,
661
663
  options: HandleResponsesCompactOptions = {},
662
664
  ): Promise<Response> {
665
+ options = { ...options, advisorAdmissionPolicy: options.advisorAdmissionPolicy ?? captureAdmissionPolicy(config, admission) };
663
666
  let body: unknown;
664
667
  try {
665
668
  body = await readJsonRequestBody(req, undefined, resolveInboundBodyLimitBytes(config.maxInboundBodyBytes));
@@ -1739,7 +1742,7 @@ export async function handleResponsesCompact(
1739
1742
  // fallback spend four more.
1740
1743
  // internalReq keeps only FORWARD_HEADERS, so the caller principal is resolved from req.
1741
1744
  const replayPrincipal = { clientPrincipalId: resolveContextPrincipal(req, config, admission) };
1742
- const response = await handleResponses(internalReq, config, logCtx, { abortSignal: req.signal, turnAdmissionLease, sendBudget, compactionRecoveryKind: "compaction-v1", compactionRoutingOverride: options.compactionRoutingOverride, replayPrincipal, ...(admission ? { admission } : {}) });
1745
+ const response = await handleResponses(internalReq, config, logCtx, { abortSignal: req.signal, turnAdmissionLease, sendBudget, compactionRecoveryKind: "compaction-v1", compactionRoutingOverride: options.compactionRoutingOverride, advisorAdmissionPolicy: options.advisorAdmissionPolicy, replayPrincipal, ...(admission ? { admission } : {}) });
1743
1746
  if (req.signal.aborted) {
1744
1747
  void response.body?.cancel(req.signal.reason).catch(() => undefined);
1745
1748
  return formatErrorResponse(499, "client_cancelled", "Client cancelled compact request");
@@ -4,6 +4,7 @@ import type { ConsumedComboFailure, HandleResponsesOptions } from "./core-option
4
4
  import type { OcxUsage } from "../../types";
5
5
  import { readBoundedResponseBody } from "../../lib/bounded-body";
6
6
  import { carryAnthropicRefusalCredential } from "../../oauth/anthropic-account-refusal";
7
+ import { carryCodexAccountCooldownResponse } from "../../codex/account-cooldown-response";
7
8
  import { codexQuotaFailureMessage, codexQuotaOutcomeMeta } from "./core-codex-account";
8
9
  import {
9
10
  isRateLimitOrQuotaFailureMessage,
@@ -141,6 +142,7 @@ export async function consumeComboFailure(
141
142
  carryReplayRefusal(response, failureResponse);
142
143
  if (verifiedPlanRefusal) markVerifiedModelNotInPlanResponse(failureResponse);
143
144
  carryAnthropicRefusalCredential(response, failureResponse);
145
+ carryCodexAccountCooldownResponse(response, failureResponse);
144
146
  return {
145
147
  response: failureResponse,
146
148
  ...(nonReplayable ? { nonReplayable: true } : {}),
@@ -1,6 +1,10 @@
1
1
  import { effectiveAzureOutputBudget } from "../../adapters/azure";
2
+ import { buildResponsesTransport } from "../../adapters/openai-responses/transport";
3
+ import { isMetaAiResponsesDestination } from "../../responses/muse-tool-name-alias";
4
+ import { hasMandatoryToolChoice } from "./combo-required-tool-contract";
5
+ import { isCodexAccountCooldownResponse } from "../../codex/account-cooldown-response";
2
6
  import { resolveWireProtocolOverride } from "../adapter-resolve";
3
- import { comboCooldownRetryAfterSeconds, type CodexAccountModelRefusal } from "../../combos/failover";
7
+ import { comboCooldownRetryAfterSeconds, isDefiniteContextOverflow, type CodexAccountModelRefusal } from "../../combos/failover";
4
8
  import { fallbackTriggerForStatus, guardFallbackHop } from "../../routing/fallback-policy";
5
9
  import {
6
10
  isCodexReasoningEffort,
@@ -55,7 +59,7 @@ import {
55
59
  previousResponseProviderState,
56
60
  } from "../../responses/state";
57
61
  import { hasUnreadableEncryptedAgentTask } from "./encrypted-payload";
58
- import { routeConcreteModel, comboRouteDecisionTrace } from "../../router";
62
+ import { captureRouteStaticPolicy, routeConcreteModel, comboRouteDecisionTrace } from "../../router";
59
63
  import { memoryModelRouteReason } from "./memory-models";
60
64
  import { baseProviderLabel, poolAccountProviderLabel } from "../../providers/label";
61
65
  import { getAccountSet } from "../../oauth/store";
@@ -532,10 +536,29 @@ export async function executeComboResponses(
532
536
  const payloadEligible = (target: (typeof combo.targets)[number]): boolean =>
533
537
  comboPayloadReadable || !unreadableEncryptedAgentTask || canDecryptUnreadableAgentTask(target);
534
538
  const meteredAllowed = admissionMayUseMeteredComboTargets(config, options.admission, combo);
539
+ const museToolEligible = (target: (typeof combo.targets)[number]): boolean => {
540
+ if (!hasMandatoryToolChoice(body)) return true;
541
+ try {
542
+ const route = routeConcreteModel(config, `${target.provider}/${target.model}`);
543
+ const inboundWire = options.inboundWire ?? "responses";
544
+ const policy = captureRouteStaticPolicy(route.providerName, route.modelId, route.provider,
545
+ route.staticPolicy.effectiveAlias, inboundWire);
546
+ const provider = resolveWireProtocolOverride(route.providerName, route.modelId, route.provider, inboundWire, policy);
547
+ return provider.adapter !== "openai-responses"
548
+ || !isMetaAiResponsesDestination(buildResponsesTransport(provider, { headers: req.headers }).url);
549
+ } catch {
550
+ return true;
551
+ }
552
+ };
553
+ const onlyMuseToolIncompatible = (): boolean => {
554
+ const enabled = combo.targets.filter(target => config.providers[target.provider]?.disabled !== true);
555
+ return enabled.length > 0 && enabled.every(target => !museToolEligible(target));
556
+ };
535
557
  // Recovery can replace unreadable encrypted input, but cannot relax other admission checks.
536
558
  const recoveryEligible = (target: (typeof combo.targets)[number]): boolean =>
537
559
  (meteredAllowed || !target.metered)
538
560
  && (combo.strategy !== "jev" || target.provider !== JEV_PROVIDER_ID)
561
+ && museToolEligible(target)
539
562
  && reasoningReplayEligible(target)
540
563
  && (protocolLanes?.pickable(target) ?? true);
541
564
  const targetEligible = (target: (typeof combo.targets)[number]): boolean =>
@@ -650,6 +673,9 @@ export async function executeComboResponses(
650
673
  config,
651
674
  { parentThreadId: inboundClientThreadId },
652
675
  );
676
+ if (!options.abortSignal?.aborted && onlyMuseToolIncompatible()) {
677
+ return comboUnavailableResponse(`No available targets for combo: ${comboId}; Meta Responses does not support mandatory tool_choice`);
678
+ }
653
679
  return options.abortSignal?.aborted
654
680
  ? clientCancelledResponse()
655
681
  : comboUnavailable(comboId, undefined,
@@ -667,6 +693,9 @@ export async function executeComboResponses(
667
693
  const protocolRefusal = protocolLanes?.refusal();
668
694
  if (protocolRefusal) return protocolRefusal;
669
695
  if (onlyReplayIncompatibleTargetsRemain()) return targetIncompatibleResponse();
696
+ if (!options.abortSignal?.aborted && onlyMuseToolIncompatible()) {
697
+ return comboUnavailableResponse(`No available targets for combo: ${comboId}; Meta Responses does not support mandatory tool_choice`);
698
+ }
670
699
  return options.abortSignal?.aborted
671
700
  ? clientCancelledResponse()
672
701
  : comboUnavailable(comboId, undefined, comboExclusionReasons(config, comboId, targetEligible));
@@ -868,10 +897,15 @@ export async function executeComboResponses(
868
897
  // "OpenAI account pool has no usable account credential" while every Claude account was spent.
869
898
  let quotaFailure: Response | undefined;
870
899
  let quotaFailedChildLog: RequestLogContext | undefined;
900
+ let contextFailure: { response: Response; childLog: RequestLogContext } | undefined;
871
901
  // Snapshotted before dispatch: a 401/403 inside this ladder cools its own provider, and that
872
902
  // fresh cooldown is not a reason the request found no target.
873
903
  const quotaCooldownSnapshot = snapshotComboQuotaCooldowns(config, comboId);
874
904
  const exhaustedFailure = (): Response => {
905
+ if (contextFailure) {
906
+ adoptFailedChildLog(contextFailure.childLog);
907
+ return contextFailure.response;
908
+ }
875
909
  const returnsQuota = lastFailure && [400, 401, 403].includes(lastFailure.status) && quotaFailure;
876
910
  const failedChildLog = returnsQuota ? quotaFailedChildLog : lastFailedChildLog;
877
911
  if (failedChildLog) adoptFailedChildLog(failedChildLog);
@@ -1067,6 +1101,19 @@ export async function executeComboResponses(
1067
1101
  finishRequestAttempt(attempt, 499, Date.now() - started, childLog.usage);
1068
1102
  attemptRetained = true;
1069
1103
  };
1104
+ // The row is written from `logCtx`, which copies `childLog` only when the child commits. A
1105
+ // bridge child binds its usage later, at the terminal frame, so the binding is forwarded here
1106
+ // after that point. Left on `childLog`, the row would re-read the bridged wire instead of the
1107
+ // adapter's usage, and that wire has no providerCredits and a context-derived input count.
1108
+ // A binding that arrives before the commit waits for it, and a child that never commits
1109
+ // forwards nothing: both flags belong to this target only.
1110
+ let childCommitted = false;
1111
+ let childUsageBound = false;
1112
+ const forwardChildUsage = (): void => {
1113
+ if (!childCommitted || !childUsageBound) return;
1114
+ logCtx.usageFromBridge = true;
1115
+ if (childLog.usage) logCtx.usage = childLog.usage;
1116
+ };
1070
1117
  const completedTarget = { provider: pick.target.provider, model: pick.target.model };
1071
1118
  const writerGeneration = pick.writerGeneration;
1072
1119
  let consumedChildFailure: ConsumedComboFailure | undefined;
@@ -1147,6 +1194,10 @@ export async function executeComboResponses(
1147
1194
  comboDispatchPermit: hopDecision?.allowed ? hopDecision.permit : undefined,
1148
1195
  comboReplaySnapshot,
1149
1196
  deferCodexResetDerivedCooldown,
1197
+ onBridgeUsageBound: () => {
1198
+ childUsageBound = true;
1199
+ forwardChildUsage();
1200
+ },
1150
1201
  // Attempt-relative TTFT is recorded HERE (not via childLog.firstOutputMs — a later
1151
1202
  // Object.assign(logCtx, childLog) would overwrite the request-relative value).
1152
1203
  onFirstOutput: () => {
@@ -1246,6 +1297,8 @@ export async function executeComboResponses(
1246
1297
  options.onCodexAuthContextResolved?.(resolvedAuth);
1247
1298
  options.setTerminalOutcomeRecorder?.(terminalRecorder);
1248
1299
  callbackGate.commit();
1300
+ childCommitted = true;
1301
+ forwardChildUsage();
1249
1302
  return response;
1250
1303
  }
1251
1304
 
@@ -1382,6 +1435,12 @@ export async function executeComboResponses(
1382
1435
  }
1383
1436
  return lastFailure;
1384
1437
  }
1438
+ // Retain a confirmed target context refusal when later retryable failures exhaust the ladder.
1439
+ // Hard stops above still return their own verdict, and every attempt keeps its own usage.
1440
+ if (!contextFailure && failure.response.status === 400 && isDefiniteContextOverflow(400, failure.classificationText)) {
1441
+ contextFailure = { childLog, response: formatErrorResponse(400, "invalid_request_error",
1442
+ failure.upstreamMessage ?? failure.classificationText, { code: "context_length_exceeded", classified: true }) };
1443
+ }
1385
1444
  // A bare status made a revoked credential read as a provider outage. Log only the parsed
1386
1445
  // code and message, never the raw envelope, as one redacted, bounded line.
1387
1446
  const failureReason = sanitizeLogMetadataString(
@@ -1415,9 +1474,10 @@ export async function executeComboResponses(
1415
1474
  const poolLocalRefusal = isAnthropicPoolLocalRefusal(
1416
1475
  config, pick.target.provider, failure.response.status, failedAccount, failureNow,
1417
1476
  );
1477
+ const codexLocalRefusal = isCodexAccountCooldownResponse(failure.response);
1418
1478
  const nextPick = advanceComboAfterFailure(config, pick, {
1419
- retryAfter: poolLocalRefusal ? undefined : failure.retryAfter,
1420
- resetAt: failure.resetAt,
1479
+ retryAfter: poolLocalRefusal || codexLocalRefusal ? undefined : failure.retryAfter,
1480
+ resetAt: codexLocalRefusal ? undefined : failure.resetAt,
1421
1481
  cooldownMs: combo.cooldownMs,
1422
1482
  now: failureNow,
1423
1483
  cooldownScope: failureCooldownScope,
@@ -1479,6 +1539,7 @@ export async function executeComboResponses(
1479
1539
  if (!pick) {
1480
1540
  if (options.abortSignal?.aborted) return clientCancelledResponse();
1481
1541
  if (onlyReplayIncompatibleTargetsRemain(attemptedTargets)) {
1542
+ if (contextFailure) return exhaustedFailure();
1482
1543
  adoptFailedChildLog(childLog);
1483
1544
  return targetIncompatibleResponse();
1484
1545
  }
@@ -1505,7 +1566,7 @@ export async function executeComboResponses(
1505
1566
  }
1506
1567
  const failure = exhaustedFailure();
1507
1568
  if (
1508
- lastFailure?.status === 413
1569
+ failure === lastFailure && lastFailure?.status === 413
1509
1570
  && lastFailureClassifiesOverflow
1510
1571
  ) {
1511
1572
  if (localAdmissionRefusals?.length) logCtx.errorCode = "input_admission_refused";
@@ -3,6 +3,7 @@ import { carryResponseToolOwnership } from "./response-tool-ownership";
3
3
  import { finalizeTranslatorBudgetResponse } from "../../lib/translator-budget";
4
4
  import { carryReplayRefusal, isNonReplayableResponse, markResponseNonReplayable } from "../../lib/upstream-retry";
5
5
  import { isVerifiedModelNotInPlanResponse, markVerifiedModelNotInPlanResponse } from "../../lib/errors";
6
+ import { carryCodexAccountCooldownResponse } from "../../codex/account-cooldown-response";
6
7
  import {
7
8
  isNativePassthroughSseResponse,
8
9
  markNativePassthroughSseResponse,
@@ -53,6 +54,7 @@ export function finalizeOwnedTranslatorBudget(response: Response, budget: Transl
53
54
  if (isNonReplayableResponse(response)) markResponseNonReplayable(finalizedResponse);
54
55
  if (isVerifiedModelNotInPlanResponse(response)) markVerifiedModelNotInPlanResponse(finalizedResponse);
55
56
  if (runTurnAdapterSseResponses.has(response)) runTurnAdapterSseResponses.add(finalizedResponse);
57
+ carryCodexAccountCooldownResponse(response, finalizedResponse);
56
58
  return carryResponseToolOwnership(response, finalizedResponse);
57
59
  }
58
60
 
@@ -79,6 +81,8 @@ export function finalizeAccountLease(response: Response, release: () => void, on
79
81
  if (isEagerRelaySseResponse(response)) markEagerRelaySseResponse(wrapped);
80
82
  if (isPreinspectedJsonResponse(response)) markPreinspectedJsonResponse(wrapped);
81
83
  if (runTurnAdapterSseResponses.has(response)) runTurnAdapterSseResponses.add(wrapped);
84
+ // Retained compaction delivers through this lease wrapper; keep a local cooldown refusal local (#7850).
85
+ carryCodexAccountCooldownResponse(response, wrapped);
82
86
  return carryResponseToolOwnership(response, carryReplayRefusal(response, wrapped));
83
87
  }
84
88
 
@@ -98,6 +98,8 @@ export interface DirectRequestOption {
98
98
  export interface HandleResponsesOptions {
99
99
  /** Immutable caller scope inherited by a capability-authenticated internal sidecar request. */
100
100
  admissionScope?: AdmissionModelScope;
101
+ /** Captured synchronously at ingress, before parsing or child replay can suspend. */
102
+ advisorAdmissionPolicy?: import("../../types/admission-policy").CapturedAdmissionPolicy;
101
103
  /** Internal request-owned policy authorization; never read from client headers or body. */
102
104
  policyRequestScope?: PolicyRequestScope;
103
105
  /** Internal concrete selector chosen from the original policy evaluation. */
@@ -215,6 +217,13 @@ export interface HandleResponsesOptions {
215
217
  callerDirectAuth?: CallerDirectAuth | null;
216
218
  /** Internal recursion guard; callers outside this module must not set it. */
217
219
  comboAttempt?: boolean;
220
+ /**
221
+ * Set by the combo loop for each child. A child logs into its own context, which the combo
222
+ * copies once when the child commits, and the bridge reports usage after that, at the terminal
223
+ * frame. The transport calls this when it binds that usage on the child's context, so the combo
224
+ * can carry the binding to the context that writes the row.
225
+ */
226
+ onBridgeUsageBound?: () => void;
218
227
  /** Child-owned prepaid initial send; capacity remains charged until dispatch or unsent release. */
219
228
  comboInitialSend?: { permit: SingleUseDispatchPermit; producerOwned?: boolean };
220
229
  /** Exact externally booked combo hop, used for its child's spend preflight and send reports. */
@@ -14,6 +14,7 @@ import type { TranslatorBudget } from "../../lib/translator-budget";
14
14
  import { executeComboResponses } from "./core-combo";
15
15
  import { registerTurn, unregisterTurn } from "../lifecycle";
16
16
  import { runWithCompactionRecovery } from "./compaction-recovery";
17
+ import { captureAdmissionPolicy } from "../capture-admission-policy";
17
18
  /**
18
19
  * Route one `/v1/responses` request through the adapter pipeline: recovery loop, passthrough
19
20
  * wire, image/web-search bridges, and the terminal-guard continuation.
@@ -35,6 +36,7 @@ export async function handleResponses(
35
36
  try {
36
37
  const response = await runWithCompactionRecovery(req, config, logCtx, {
37
38
  ...options, abortSignal,
39
+ advisorAdmissionPolicy: options.advisorAdmissionPolicy ?? captureAdmissionPolicy(config, options.admission, options.admissionScope),
38
40
  openAiSidecarAuth: options.openAiSidecarAuth === undefined
39
41
  ? captureExplicitOpenAiCallerAuth(req.headers, config) : options.openAiSidecarAuth,
40
42
  nativeCallerAuth: options.nativeCallerAuth === undefined
@@ -24,8 +24,8 @@ export interface JevModelInvokerContext {
24
24
  /** The request that owns the combo; only its spend root is reused, never its headers. */
25
25
  req: Pick<Request, "headers">;
26
26
  config: OcxConfig;
27
- /** Parent options: only the resolved admission scope and Codex auth policy cross over. */
28
- options: Pick<HandleResponsesOptions, "admission" | "admissionScope" | "codexAuthPolicy">;
27
+ /** Parent options: only the resolved admission scope, Advisor caller policy and Codex auth policy cross over. */
28
+ options: Pick<HandleResponsesOptions, "admission" | "admissionScope" | "advisorAdmissionPolicy" | "codexAuthPolicy">;
29
29
  handleResponses: HandleResponses;
30
30
  }
31
31
 
@@ -154,6 +154,8 @@ export function createJevModelInvoker(context: JevModelInvokerContext): JevModel
154
154
  // capability-authenticated internal call carries the caller's scope while its own
155
155
  // admission is the host's broad key.
156
156
  ...(context.options.admissionScope ? { admissionScope: context.options.admissionScope } : {}),
157
+ // The parent's ingress snapshot; without it the detached turn would re-read mutable config (#7859).
158
+ ...(context.options.advisorAdmissionPolicy ? { advisorAdmissionPolicy: context.options.advisorAdmissionPolicy } : {}),
157
159
  ...(context.options.codexAuthPolicy ? { codexAuthPolicy: context.options.codexAuthPolicy } : {}),
158
160
  // Explicit nulls: handleResponses captures caller credentials only when these are undefined.
159
161
  callerDirectAuth: null,
@@ -54,6 +54,7 @@ import {
54
54
  import { restoreRoutedCustomCalls, RoutedCustomToolCompatError } from "../../responses/custom-tool-compat";
55
55
  import { XaiToolSchemaCompatibilityError } from "../../adapters/xai-tool-schema";
56
56
  import { MuseToolChoiceCompatibilityError } from "../../adapters/openai-responses/muse-tool-choice";
57
+ import { MuseToolCatalogCompatibilityError } from "../../adapters/openai-responses/muse-tool-catalog";
57
58
  import { OpenGatewayInputCompatibilityError } from "../../adapters/openai-responses/opengateway-input";
58
59
  import { formatErrorResponse } from "../../bridge";
59
60
  import { redactSecretString } from "../../lib/redact";
@@ -389,6 +390,7 @@ export async function preparePassthroughExchange(
389
390
  || error instanceof XaiToolSchemaCompatibilityError
390
391
  || error instanceof RoutedCustomToolCompatError
391
392
  || error instanceof MuseToolChoiceCompatibilityError
393
+ || error instanceof MuseToolCatalogCompatibilityError
392
394
  || error instanceof OpenGatewayInputCompatibilityError
393
395
  ) {
394
396
  return formatErrorResponse(400, "invalid_request_error", redactSecretString(error.message));
@@ -480,10 +482,9 @@ export async function preparePassthroughExchange(
480
482
  for (const callType of collectProviderExecutedCallTypes(clientToolAuthorizationBody)) {
481
483
  providerExecutedCallTypes.add(callType);
482
484
  }
483
- const currentOutboundCatalog = currentTurnWireToolCatalogBody(
484
- outboundRequestBody,
485
- replayedInputPrefixLength,
486
- );
485
+ // Serialization drops provenance, so never slice transformed input by the raw prefix.
486
+ // Current wrappers were captured above; adapter-injected hosted tools are top-level.
487
+ const currentOutboundCatalog = { tools: outboundRequestBody?.tools };
487
488
  for (const callType of collectProviderExecutedCallTypes(currentOutboundCatalog)) {
488
489
  providerExecutedCallTypes.add(callType);
489
490
  }
@@ -15,6 +15,7 @@ import { parseSyntheticRowId } from "../fast-row";
15
15
  import { routeConcreteModel } from "../../router";
16
16
  import { isPolicyCandidateRefusal, policyWireDestination, type PolicyRequestScope } from "./policy-request-scope";
17
17
  import { fallbackTriggerForStatus, guardFallbackHop } from "../../routing/fallback-policy";
18
+ import { captureAdmissionPolicy } from "../capture-admission-policy";
18
19
 
19
20
  type CoreHandler = typeof handleResponsesCore;
20
21
  type CoreOptions = Parameters<CoreHandler>[3];
@@ -182,6 +183,7 @@ export async function handleResponsesWithPolicyFallback(
182
183
  let rawBody: Record<string, unknown> | null = null;
183
184
  const coreOptions: CoreOptions = {
184
185
  ...options,
186
+ advisorAdmissionPolicy: options.advisorAdmissionPolicy ?? captureAdmissionPolicy(config, options.admission, options.admissionScope),
185
187
  policyRequestScope: policyScope,
186
188
  openAiSidecarAuth: options.openAiSidecarAuth === undefined
187
189
  ? captureExplicitOpenAiCallerAuth(req.headers, config) : options.openAiSidecarAuth,
@@ -76,7 +76,7 @@ import { anthropicSessionKeyFromParts } from "../../oauth/anthropic-routing";
76
76
  import { configuredAnthropicInstance } from "../../providers/anthropic-instance";
77
77
  import { isTranslatorBudgetExceededError } from "../../lib/translator-budget";
78
78
  import { bindTurnTerminationScope, rememberDeliveredFinalAnswer } from "../../responses/turn-termination";
79
- import { observeCacheDiagnosticInbound, rebindCacheDiagnosticBody, requestLogSpeedLabel, readConfiguredCodexServiceTier } from "../request-log";
79
+ import { noteCodexSelectionAffinity, observeCacheDiagnosticInbound, rebindCacheDiagnosticBody, requestLogSpeedLabel, readConfiguredCodexServiceTier } from "../request-log";
80
80
  import type { RouteResult } from "../../router";
81
81
  import {
82
82
  captureRouteStaticPolicy,
@@ -665,6 +665,7 @@ export async function prepareResponsesRequest(
665
665
  return formatErrorResponse(401, "authentication_error", "Anthropic Pool 2 requires an enabled configured provider");
666
666
  }
667
667
  const admissionScope = options.admissionScope ?? resolveAdmissionModelScope(config, options.admission);
668
+ const advisorAdmissionPolicy = options.advisorAdmissionPolicy;
668
669
  const captureInboundRoutePolicy = (candidate: RouteResult, captureRequestPolicy = true): RouteResult => {
669
670
  // Router-collapsed aliases bypass the combo loop. Re-pick before capturing destination
670
671
  // policy so random/RR cannot refuse while an unmetered target is still usable.
@@ -1585,10 +1586,7 @@ export async function prepareResponsesRequest(
1585
1586
  // A move is the expensive event: it discards the prefix warmed on the previous account. Record
1586
1587
  // it as an event with its cause, so the operator reads it off one line instead of inferring it
1587
1588
  // from account labels across many (#4546).
1588
- if (admissionState.authCtx.kind === "pool" && admissionState.authCtx.affinityDecision) {
1589
- logCtx.affinity = admissionState.authCtx.affinityDecision.move;
1590
- logCtx.affinityReason = admissionState.authCtx.affinityDecision.reason;
1591
- }
1589
+ noteCodexSelectionAffinity(logCtx, admissionState.authCtx);
1592
1590
  {
1593
1591
  const binding = conversationStateBindingFromAuth(admissionState.authCtx, poolAffinityKey);
1594
1592
  if (binding) {
@@ -1632,6 +1630,7 @@ export async function prepareResponsesRequest(
1632
1630
  // the key scope as it stood when this request was admitted.
1633
1631
  inboundSelector,
1634
1632
  admissionScope,
1633
+ advisorAdmissionPolicy,
1635
1634
  translatorBudget,
1636
1635
  parsed,
1637
1636
  toolBridgeMaps,
@@ -6,6 +6,7 @@ import {
6
6
  workflowSendCeilingReached,
7
7
  workflowSpendCeilingReached,
8
8
  } from "../../lib/workflow-budget";
9
+ import { currentWorkflowBudgetPolicy } from "../../lib/workflow-budget-config";
9
10
  import { poolContinuityRefusalReason, workflowRefusalResponse } from "../workflow-refusal";
10
11
  import type { AttemptRecoveryKind, AttemptRecoveryWithheld } from "../../usage/log";
11
12
  import { noteAttemptRecoveryWithheld, noteAttemptSend } from "../request-log";
@@ -93,7 +94,7 @@ export function createResponsesSendBudget(
93
94
  // dropping the record to make room would hand the fan-out a fresh allowance, which is the
94
95
  // laundering this ceiling exists to stop. The client is told the task needs a new grant
95
96
  // rather than being given a synthetic upstream error.
96
- if (workflowSendCeilingReached(workflowRootId)) {
97
+ if (workflowSendCeilingReached(workflowRootId, currentWorkflowBudgetPolicy())) {
97
98
  // A log context exists here, unlike at HTTP admission, so the row this request writes is
98
99
  // marked synthetic rather than reading as a request that vanished with zero sends.
99
100
  return workflowRefusalResponse("workflow-sends-exhausted", logCtx, undefined, workflowRootId);
@@ -131,6 +131,7 @@ export async function prepareResponsesSidecarAuth(
131
131
  ? undefined
132
132
  : planVisionSidecar(config, route.provider, route.modelId, parsed, openAiSidecar, {
133
133
  admission: options.admission, codexAuthPolicy: options.codexAuthPolicy, providerName: route.providerName,
134
+ advisorAdmissionPolicy: options.advisorAdmissionPolicy,
134
135
  });
135
136
  const recordSidecarOutcome = openAiSidecar?.recordOutcome;
136
137
  if (visionPlan) {
@@ -541,12 +541,11 @@ export async function prepareResponsesTransport(
541
541
  logCtx.usageFromBridge = true;
542
542
  if (usesApiKeyAccount(route.provider)) {
543
543
  logCtx.usage = logCtx.activeAttempt?.usage;
544
- return;
545
- }
546
- if (usage) {
544
+ } else if (usage) {
547
545
  logCtx.usage = usage;
548
546
  if (logCtx.activeAttempt) logCtx.activeAttempt.usage = usage;
549
547
  }
548
+ options.onBridgeUsageBound?.();
550
549
  };
551
550
  const selectionIsCurrent = (binding: DispatchBinding | undefined): boolean => {
552
551
  if (!anthropicAdmissionIsCurrent()) return false;
@@ -911,6 +911,8 @@ export async function executeResponsesRunTurn(
911
911
  toolParameterSchemas,
912
912
  ...(options.onFirstOutput ? { onFirstOutput: options.onFirstOutput } : {}),
913
913
  ...(routedCompaction ? { compaction: true, compactionRetention: parsed._compactionRetention } : {}),
914
+ completeOnMaxOutputTokens: config.completeOnMaxOutputTokens === true && enforceDeclaredToolNames,
915
+ textFormat: parsed.options.textFormat,
914
916
  // grok-build's strict decoder dies on the typed response.heartbeat frame; its
915
917
  // eventsource layer tolerates comment keep-alives. Codex needs the opposite.
916
918
  ...(logCtx.surface === "grok" ? { heartbeatStyle: "comment" as const } : {}),
@@ -1072,6 +1074,8 @@ export async function executeResponsesRunTurn(
1072
1074
  bareCustomToolNames,
1073
1075
  toolSearchToolNames,
1074
1076
  ...(routedCompaction ? { compaction: true, compactionRetention: parsed._compactionRetention } : {}),
1077
+ completeOnMaxOutputTokens: config.completeOnMaxOutputTokens === true && enforceDeclaredToolNames,
1078
+ textFormat: parsed.options.textFormat,
1075
1079
  onProviderState: state => { providerState = state; },
1076
1080
  onUsage: usage => {
1077
1081
  transportState.bindKeyUsageFromBridge(usage);
@@ -9,7 +9,7 @@ import type { ResponsesSendBudget } from "./request-send-budget";
9
9
  import { formatErrorResponse } from "../../bridge";
10
10
  import { planWebSearch, buildWebSearchTool, runWithWebSearch } from "../../web-search";
11
11
  import { createRegisteredAdvisorPlan } from "./advisor-plan-slot";
12
- import { ADVISOR_TOOL_NAME } from "./advisor-slot";
12
+ import { ADVISOR_TOOL_NAME, createAdvisorSidecarStep } from "./advisor-slot";
13
13
  import { buildToolBridgeMaps } from "./collaboration";
14
14
  import {
15
15
  planImageBridge,
@@ -75,6 +75,7 @@ export async function executeResponsesSidecars(
75
75
  | "rememberKiroDeliveredFinalAnswer"
76
76
  | "responseStateOptions"
77
77
  | "admissionScope"
78
+ | "advisorAdmissionPolicy"
78
79
  >,
79
80
  transportState: Pick<
80
81
  ResponsesTransport,
@@ -195,6 +196,7 @@ export async function executeResponsesSidecars(
195
196
  workerIdentity: `${route.modelId} (provider ${route.providerName})`,
196
197
  workerModelId: route.modelId,
197
198
  admissionScope,
199
+ admissionPolicy: requestState.advisorAdmissionPolicy,
198
200
  abortSignal: options.abortSignal,
199
201
  });
200
202
  // Preflight only on real worker turns: a routed-compaction turn is a summarization request,
@@ -655,7 +657,21 @@ export async function executeResponsesSidecars(
655
657
  // Placed BEFORE the runTurn early-return for non-runTurn adapters so dual-tool turns dispatch
656
658
  // through web-search instead of being swallowed. runTurn adapters never enter this branch.
657
659
  if (canRunWebSearch && wsPlan) {
658
- parsed.context.tools = [...(parsed.context.tools ?? []), buildWebSearchTool()];
660
+ const webSearchAdvisor = advisorPlan && !routedCompaction
661
+ && typeof advisorPlan.consult === "function" && typeof advisorPlan.formatUnavailable === "function"
662
+ ? advisorPlan
663
+ : undefined;
664
+ parsed.context.tools = [
665
+ ...(parsed.context.tools ?? []).filter(tool => !webSearchAdvisor || (!tool.advisor && tool.name !== ADVISOR_TOOL_NAME)),
666
+ buildWebSearchTool(),
667
+ ...(webSearchAdvisor ? [webSearchAdvisor.tool] : []),
668
+ ];
669
+ if (webSearchAdvisor) {
670
+ requestState.toolBridgeMaps = buildToolBridgeMaps(parsed);
671
+ translatorBudget.chargeRetained(new TextEncoder().encode(ADVISOR_TOOL_NAME).byteLength * 2, {
672
+ kind: "retained_collectors",
673
+ });
674
+ }
659
675
  // Resolve the mutable route at send time: a 429 rotation replaces route.provider, so retaining
660
676
  // one pre-rotation providerFetch would keep the old credential and transport pin.
661
677
  const routedProviderFetch = ((input: Parameters<typeof globalThis.fetch>[0], init?: RequestInit) =>
@@ -666,6 +682,7 @@ export async function executeResponsesSidecars(
666
682
  })(input, init)) as typeof globalThis.fetch;
667
683
  const loopResponse = await runWithWebSearch({
668
684
  parsed, adapter: transportState.adapter,
685
+ ...(webSearchAdvisor ? { advisor: createAdvisorSidecarStep(webSearchAdvisor as Parameters<typeof createAdvisorSidecarStep>[0]) } : {}),
669
686
  chatRawReasoningVisible: options.chatRawReasoningVisible,
670
687
  ...(transportState.adapter.name === "anthropic" ? { thinkingRecovery } : {}),
671
688
  clientToolCatalogRestriction: route.provider.authMode === "forward" ? undefined : requestState.clientToolCatalogRestriction,
@@ -773,9 +790,8 @@ export async function executeResponsesSidecars(
773
790
 
774
791
  // Advisor synthetic tool + stream guard: attach ONLY when this function is about to hand the
775
792
  // request back to the normal translated exchange (no sidecar loop claimed the turn, not a
776
- // run-turn adapter, not compaction). The web-search/image loops own their own event handling;
777
- // run-turn adapters execute their own loop; both would leak the synthetic tool call they
778
- // cannot intercept. Preflight (above) still applies to those paths — only the tool does not.
793
+ // run-turn adapter, not compaction). Web-search handles advisor in its own loop above; the
794
+ // image/video and run-turn loops cannot intercept it. Preflight still applies to those paths.
779
795
  if (advisorPlan && !routedCompaction && !transportState.adapter.runTurn && !wsPlan && !imgPlan && !vidPlan) {
780
796
  // A client-declared tool named "advisor" would collide with the synthetic injection (one
781
797
  // wire name, two schemas); the synthetic runtime owns the name for this turn.
@@ -169,6 +169,9 @@ type OpenItem = {
169
169
  /** The gateway sent content_part.done for this item. */
170
170
  partDone: boolean;
171
171
  text: string;
172
+ /** Exact UTF-8 size and boundary unit, without rescanning the accumulated text. */
173
+ textBytes: number;
174
+ lastCodeUnit: number;
172
175
  item: Record<string, unknown>;
173
176
  };
174
177
 
@@ -219,7 +222,7 @@ export function createResponsesSnapshotBlockRewrite(
219
222
  const closeOpenItem = (index: number): void => {
220
223
  const open = openItems.get(index);
221
224
  if (!open) return;
222
- aggregateOpenTextBytes -= Buffer.byteLength(open.text, "utf8");
225
+ aggregateOpenTextBytes -= open.textBytes;
223
226
  openItems.delete(index);
224
227
  };
225
228
 
@@ -313,6 +316,8 @@ export function createResponsesSnapshotBlockRewrite(
313
316
  textDone: false,
314
317
  partDone: false,
315
318
  text: "",
319
+ textBytes: 0,
320
+ lastCodeUnit: -1,
316
321
  item: event.item,
317
322
  });
318
323
  }
@@ -407,12 +412,19 @@ export function createResponsesSnapshotBlockRewrite(
407
412
  return [changed ? jsonBlock(nextEvent) : block];
408
413
  }
409
414
  if (open && (itemId === undefined || itemId === open.itemId)) {
410
- const deltaBytes = Buffer.byteLength(event.delta, "utf8");
415
+ let deltaBytes = Buffer.byteLength(event.delta, "utf8");
416
+ const first = event.delta.charCodeAt(0);
417
+ // Joining two isolated surrogate halves replaces two 3-byte replacements with 4 bytes.
418
+ if (open.lastCodeUnit >= 0xd800 && open.lastCodeUnit <= 0xdbff && first >= 0xdc00 && first <= 0xdfff) {
419
+ deltaBytes -= 2;
420
+ }
421
+ if (event.delta.length > 0) open.lastCodeUnit = event.delta.charCodeAt(event.delta.length - 1);
411
422
  open.text += event.delta;
423
+ open.textBytes += deltaBytes;
412
424
  aggregateOpenTextBytes += deltaBytes;
413
425
  // Unbounded text accumulation is a retention hole (#893 review):
414
426
  // overshoot per-item or aggregate caps and the stream goes fail-closed.
415
- if (Buffer.byteLength(open.text, "utf8") > MAX_COMPLETED_OUTPUT_ITEM_SOURCE_BYTES
427
+ if (open.textBytes > MAX_COMPLETED_OUTPUT_ITEM_SOURCE_BYTES
416
428
  || aggregateOpenTextBytes > MAX_OPEN_ITEM_AGGREGATE_TEXT_BYTES) {
417
429
  taintAndRelease();
418
430
  }
@@ -437,9 +449,11 @@ export function createResponsesSnapshotBlockRewrite(
437
449
  if (typeof event.text === "string") {
438
450
  // Replacement must keep the aggregate honest and bounded, the
439
451
  // same as delta accumulation (#893 review).
440
- const previousBytes = Buffer.byteLength(open.text, "utf8");
452
+ const previousBytes = open.textBytes;
441
453
  const nextBytes = Buffer.byteLength(event.text, "utf8");
442
454
  open.text = event.text;
455
+ open.textBytes = nextBytes;
456
+ open.lastCodeUnit = event.text.charCodeAt(event.text.length - 1);
443
457
  aggregateOpenTextBytes += nextBytes - previousBytes;
444
458
  if (nextBytes > MAX_COMPLETED_OUTPUT_ITEM_SOURCE_BYTES
445
459
  || aggregateOpenTextBytes > MAX_OPEN_ITEM_AGGREGATE_TEXT_BYTES) {
@@ -1,4 +1,5 @@
1
1
  import { UNDECLARED_TOOL_CALL_ERROR_CODE } from "../lib/errors";
2
+ import { hasItemProvenance, isHistoricalItem } from "../responses/item-provenance";
2
3
  import {
3
4
  collectAmbiguousDottedAliases,
4
5
  dottedAliasIsUnambiguous,
@@ -136,6 +137,9 @@ export function currentTurnWireToolCatalogBody(
136
137
  replayPrefixLength: number | undefined,
137
138
  ): unknown {
138
139
  if (!isPlainObject(body) || !Array.isArray(body.input)) return body;
140
+ if (body.input.some(hasItemProvenance)) {
141
+ return { ...body, input: body.input.filter(item => !isHistoricalItem(item)) };
142
+ }
139
143
  if (typeof replayPrefixLength !== "number" || !Number.isFinite(replayPrefixLength)) return body;
140
144
  const start = Math.min(body.input.length, Math.max(0, Math.trunc(replayPrefixLength)));
141
145
  if (start === 0) return body;