@bitkyc08/opencodex 2.19.0 → 2.21.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 (130) hide show
  1. package/gui/dist/assets/index-BOFeam5a.js +76 -0
  2. package/gui/dist/assets/index-Xq49CY8F.css +1 -0
  3. package/gui/dist/index.html +2 -2
  4. package/package.json +1 -1
  5. package/src/adapters/anthropic.ts +2 -28
  6. package/src/adapters/google.ts +31 -3
  7. package/src/adapters/mimo-free.ts +17 -0
  8. package/src/adapters/openai-chat.ts +392 -40
  9. package/src/adapters/registry.ts +144 -0
  10. package/src/adapters/responses-tool-schema.ts +67 -0
  11. package/src/bridge.ts +15 -2
  12. package/src/chat/inbound.ts +13 -7
  13. package/src/claude/gateway-cache.ts +41 -4
  14. package/src/cli/claude.ts +3 -2
  15. package/src/cli/combo.ts +3 -0
  16. package/src/cli/dispatch.ts +8 -0
  17. package/src/cli/export-command.ts +2 -2
  18. package/src/cli/help.ts +2 -0
  19. package/src/cli/index.ts +3 -2
  20. package/src/cli/lab.ts +135 -1
  21. package/src/cli/minimax.ts +491 -0
  22. package/src/cli/models-runtime.ts +22 -1
  23. package/src/cli/models.ts +67 -2
  24. package/src/cli/opencode.ts +2 -1
  25. package/src/cli/registry.ts +22 -2
  26. package/src/clients/config-export.ts +125 -7
  27. package/src/codex/app-server-processes.ts +57 -2
  28. package/src/codex/app-server-restart-service.ts +232 -0
  29. package/src/codex/catalog/aggregation.ts +10 -1
  30. package/src/codex/catalog/effort.ts +15 -3
  31. package/src/codex/catalog/parsing.ts +3 -1
  32. package/src/codex/catalog/provider-fetch.ts +45 -5
  33. package/src/codex/catalog/sync.ts +74 -4
  34. package/src/codex/convergence.ts +2 -0
  35. package/src/combos/index.ts +1 -0
  36. package/src/combos/request.ts +30 -0
  37. package/src/combos/types.ts +6 -0
  38. package/src/config.ts +52 -0
  39. package/src/generated/compatibility-version.json +241 -85
  40. package/src/images/loop.ts +26 -6
  41. package/src/integrations/registry.ts +7 -0
  42. package/src/lab/conformance/jcs.ts +42 -2
  43. package/src/lab/conformance/negative-controls.ts +6 -2
  44. package/src/lab/conformance/runner.ts +16 -5
  45. package/src/lab/fabric/observe.ts +49 -14
  46. package/src/lab/index.ts +16 -0
  47. package/src/lab/ledger/purge.ts +152 -83
  48. package/src/lab/ledger/store.ts +168 -54
  49. package/src/lab/observe/from-conformance.ts +8 -6
  50. package/src/lab/observe/from-live.ts +8 -2
  51. package/src/lab/paths.ts +23 -0
  52. package/src/lab/public/bundle.ts +217 -0
  53. package/src/lab/public/community-authority.ts +175 -0
  54. package/src/lab/public/community-files.ts +29 -0
  55. package/src/lab/public/community.ts +479 -0
  56. package/src/lab/public/file-safety.ts +155 -0
  57. package/src/lab/public/ids.ts +26 -0
  58. package/src/lab/public/index.ts +16 -0
  59. package/src/lab/public/mutation-lock.ts +424 -0
  60. package/src/lab/public/operator.ts +353 -0
  61. package/src/lab/public/origin-purge.ts +79 -0
  62. package/src/lab/public/origin.ts +203 -0
  63. package/src/lab/public/privacy.ts +143 -0
  64. package/src/lab/public/private-file.ts +261 -0
  65. package/src/lab/public/project.ts +124 -0
  66. package/src/lab/public/purge-test-fault.ts +21 -0
  67. package/src/lab/public/purge.ts +223 -0
  68. package/src/lab/public/registry.ts +44 -0
  69. package/src/lab/public/revocation.ts +252 -0
  70. package/src/lab/public/signature.ts +219 -0
  71. package/src/lab/public/storage.ts +105 -0
  72. package/src/lab/public/strict-json.ts +206 -0
  73. package/src/lab/public/time.ts +26 -0
  74. package/src/lab/public/types.ts +172 -0
  75. package/src/lab/public/validate.ts +391 -0
  76. package/src/lib/codex-restart-contract.ts +120 -0
  77. package/src/lib/lab-activation.ts +109 -47
  78. package/src/lib/lab-live-pinned-sender.ts +16 -5
  79. package/src/lib/pinned-http.ts +70 -16
  80. package/src/lib/self-launch-argv.ts +15 -0
  81. package/src/lib/state-store-registrations.ts +2 -0
  82. package/src/lib/upstream-reachability.ts +4 -0
  83. package/src/lib/windows-elevation.ts +10 -1
  84. package/src/providers/derive.ts +24 -4
  85. package/src/providers/registry.ts +14 -4
  86. package/src/providers/request-pacing.ts +310 -0
  87. package/src/providers/service-tier.ts +143 -0
  88. package/src/providers/static-model-discovery.ts +86 -0
  89. package/src/reasoning-effort.ts +27 -1
  90. package/src/responses/custom-tool-compat.ts +4 -1
  91. package/src/responses/parser.ts +7 -1
  92. package/src/responses/provider-opaque-metadata.ts +73 -0
  93. package/src/responses/schema.ts +6 -0
  94. package/src/router.ts +23 -6
  95. package/src/routing/capability.ts +4 -2
  96. package/src/routing/compatibility/behavior.ts +5 -1
  97. package/src/server/adapter-resolve.ts +2 -32
  98. package/src/server/auth-cors.ts +50 -6
  99. package/src/server/chat-completions.ts +74 -36
  100. package/src/server/chat-native-sse.ts +331 -0
  101. package/src/server/chat-native.ts +371 -0
  102. package/src/server/management/combo-routes.ts +16 -2
  103. package/src/server/management/config-routes.ts +6 -4
  104. package/src/server/management/context.ts +17 -0
  105. package/src/server/management/lab-routes.ts +181 -19
  106. package/src/server/management/model-routes.ts +76 -2
  107. package/src/server/management/model-rows.ts +8 -0
  108. package/src/server/management/provider-capability-config.ts +48 -0
  109. package/src/server/management/provider-routes.ts +76 -4
  110. package/src/server/management/system-restart.ts +4 -2
  111. package/src/server/management/system-routes.ts +38 -0
  112. package/src/server/relay.ts +17 -3
  113. package/src/server/responses/compact.ts +4 -1
  114. package/src/server/responses/core.ts +257 -46
  115. package/src/server/responses/empty-completion-guard.ts +276 -0
  116. package/src/server/responses/fetch-helpers.ts +35 -4
  117. package/src/server/responses/pacing-overload.ts +13 -0
  118. package/src/server/responses/policy-fallback.ts +16 -2
  119. package/src/server/responses/terminal-guard.ts +1 -1
  120. package/src/server/responses/upstream-error.ts +5 -0
  121. package/src/server/responses.ts +17 -2
  122. package/src/server/system-env.ts +1 -1
  123. package/src/types.ts +84 -5
  124. package/src/update/index.ts +6 -5
  125. package/src/update/job.ts +5 -6
  126. package/src/update/notify.ts +5 -3
  127. package/src/usage/log.ts +11 -1
  128. package/src/web-search/loop.ts +21 -5
  129. package/gui/dist/assets/index-CQ7bIKee.css +0 -1
  130. package/gui/dist/assets/index-D_JUZLEC.js +0 -76
@@ -0,0 +1,276 @@
1
+ import type { AdapterEvent, OcxConfig, OcxUsage } from "../../types";
2
+
3
+ /**
4
+ * Empty-completion guard for Responses turns (port of codex-router's
5
+ * empty-completion-guard + single retry, PR #145).
6
+ *
7
+ * Failure mode: the upstream answers 200 and completes the turn but never
8
+ * produced output text or a tool call (a reasoning-only stream that ends with
9
+ * nothing is the canonical shape). The client has no code path for "the model
10
+ * said nothing", so it silently records the turn as done — the "random stop"
11
+ * nobody can explain. The guard holds pre-content events (reasoning deltas
12
+ * are deliberately NOT content), and when a terminal event arrives with no
13
+ * content it suppresses the terminal and retries the IDENTICAL turn once
14
+ * (same request bytes, same headers). If the retry is also empty — or fails
15
+ * upstream — the client sees a stated failure instead of a second silent
16
+ * success.
17
+ *
18
+ * The retry is explicitly enabled by top-level config. The environment switch
19
+ * is a disable-only emergency override: OCX_EMPTY_COMPLETION_RETRY=0 restores
20
+ * the previous relay behavior without editing the persisted config.
21
+ */
22
+ export const EMPTY_COMPLETION_RETRY_ENV = "OCX_EMPTY_COMPLETION_RETRY";
23
+
24
+ /** Retained pre-content events are bounded independently by count and encoded size. */
25
+ export const EMPTY_COMPLETION_MAX_BUFFERED_EVENTS = 1_024;
26
+ export const EMPTY_COMPLETION_MAX_BUFFERED_BYTES = 1_048_576;
27
+
28
+ export function emptyCompletionRetryEnabled(
29
+ config: Pick<OcxConfig, "emptyCompletionRetry">,
30
+ env: Record<string, string | undefined> = process.env,
31
+ ): boolean {
32
+ return config.emptyCompletionRetry === true && env[EMPTY_COMPLETION_RETRY_ENV] !== "0";
33
+ }
34
+
35
+ /** Surfaced when the single retry was also empty or failed upstream. */
36
+ export const EMPTY_COMPLETION_RETRY_FAILED_CODE = "empty_completion_retry_failed";
37
+
38
+ /**
39
+ * Terminal stop reasons the bridge renders as a visible `response.incomplete`
40
+ * (max_tokens / content_filter). Those are already a stated failure, not the
41
+ * silent empty success this guard exists to catch, and retrying the identical
42
+ * request would burn tokens for the same truncated result.
43
+ */
44
+ const VISIBLE_INCOMPLETE_STOP_REASONS = new Set(["max_tokens", "content_filter"]);
45
+ const UTF8_ENCODER = new TextEncoder();
46
+
47
+ function retainedEventBytes(event: AdapterEvent): number {
48
+ return UTF8_ENCODER.encode(JSON.stringify(event)).byteLength;
49
+ }
50
+
51
+ function isReasoningEvent(event: AdapterEvent): boolean {
52
+ return event.type === "thinking_delta"
53
+ || event.type === "thinking_signature"
54
+ || event.type === "redacted_thinking"
55
+ || event.type === "kiro_redacted_reasoning"
56
+ || event.type === "reasoning_raw_delta";
57
+ }
58
+
59
+ function isTerminalEvent(
60
+ event: AdapterEvent,
61
+ ): event is Extract<AdapterEvent, { type: "done" | "incomplete" | "error" }> {
62
+ return event.type === "done" || event.type === "incomplete" || event.type === "error";
63
+ }
64
+
65
+ /**
66
+ * Content means something the client can act on: output text or a tool call
67
+ * (web-search cells included). Reasoning deltas are deliberately not content —
68
+ * a turn that streams only reasoning and then completes with nothing is
69
+ * exactly the empty completion this guard exists to catch. Empty text deltas
70
+ * (some batch adapters always carry `""`) are not content either.
71
+ */
72
+ export function isContentEvent(event: AdapterEvent): boolean {
73
+ switch (event.type) {
74
+ case "text_delta":
75
+ return event.text.length > 0;
76
+ case "tool_call_start":
77
+ case "tool_call_delta":
78
+ case "tool_call_end":
79
+ case "web_search_call_begin":
80
+ case "web_search_call_end":
81
+ return true;
82
+ default:
83
+ return false;
84
+ }
85
+ }
86
+
87
+ export function emptyCompletionRetryFailedEvent(
88
+ usage?: OcxUsage,
89
+ retryFailedUpstream = false,
90
+ ): Extract<AdapterEvent, { type: "error" }> {
91
+ return {
92
+ type: "error",
93
+ status: 502,
94
+ errorType: "upstream_error",
95
+ code: EMPTY_COMPLETION_RETRY_FAILED_CODE,
96
+ message: retryFailedUpstream
97
+ ? "The model returned an empty completion and the retry failed upstream."
98
+ : "The model returned an empty completion. opencodex retried once and the completion was empty again.",
99
+ ...(usage ? { usage } : {}),
100
+ };
101
+ }
102
+
103
+ /**
104
+ * Sum two usage snapshots. Same semantics as terminal-guard's mergeUsage and
105
+ * request-log's aggregateAttemptUsage: token totals add across the attempts;
106
+ * `estimated` wins when either attempt only estimated.
107
+ */
108
+ export function mergeUsage(
109
+ first: OcxUsage | undefined,
110
+ second: OcxUsage | undefined,
111
+ ): OcxUsage | undefined {
112
+ if (!first) return second;
113
+ if (!second) return first;
114
+ const sumOptional = (key: keyof OcxUsage): number | undefined => {
115
+ const left = first[key];
116
+ const right = second[key];
117
+ return typeof left === "number" || typeof right === "number"
118
+ ? (typeof left === "number" ? left : 0) + (typeof right === "number" ? right : 0)
119
+ : undefined;
120
+ };
121
+ const cachedInputTokens = sumOptional("cachedInputTokens");
122
+ const cacheReadInputTokens = sumOptional("cacheReadInputTokens");
123
+ const cacheCreationInputTokens = sumOptional("cacheCreationInputTokens");
124
+ const reasoningOutputTokens = sumOptional("reasoningOutputTokens");
125
+ const contextTotalTokens = second.contextTotalTokens ?? first.contextTotalTokens;
126
+ const inputTokens = first.inputTokens + second.inputTokens;
127
+ const outputTokens = first.outputTokens + second.outputTokens;
128
+ return {
129
+ inputTokens,
130
+ outputTokens,
131
+ totalTokens: inputTokens + outputTokens,
132
+ ...(contextTotalTokens !== undefined ? { contextTotalTokens } : {}),
133
+ ...(cachedInputTokens !== undefined ? { cachedInputTokens } : {}),
134
+ ...(cacheReadInputTokens !== undefined ? { cacheReadInputTokens } : {}),
135
+ ...(cacheCreationInputTokens !== undefined ? { cacheCreationInputTokens } : {}),
136
+ ...(reasoningOutputTokens !== undefined ? { reasoningOutputTokens } : {}),
137
+ ...(first.estimated || second.estimated ? { estimated: true } : {}),
138
+ };
139
+ }
140
+
141
+ export interface EmptyCompletionGuardOptions {
142
+ firstEvents: AsyncIterable<AdapterEvent>;
143
+ /**
144
+ * Re-run the IDENTICAL turn: same request body, same headers, same signal.
145
+ * Receives no arguments — the request must not be modified between attempts.
146
+ */
147
+ continuation: () => AsyncIterable<AdapterEvent> | Promise<AsyncIterable<AdapterEvent>>;
148
+ /** How many times an empty completion is retried; default 1 (the router's single retry). */
149
+ maxRetries?: number;
150
+ }
151
+
152
+ /**
153
+ * Watch an adapter event stream for the empty-completion failure mode. Events
154
+ * are held until the turn produces content or ends: reasoning and other
155
+ * pre-content events stay buffered (released in order on first content), the
156
+ * terminal is withheld, and an empty terminal triggers one identical-turn
157
+ * retry through `continuation`. Usage is merged across attempts so the bridge
158
+ * and request log meter the whole turn, not just the attempt that succeeded.
159
+ *
160
+ * Heartbeats always pass through untouched: they feed the bridge's stall
161
+ * watchdog, so holding them behind the content gate would trip false
162
+ * upstream_stall_timeout failures on slow reasoning-only turns.
163
+ */
164
+ export async function* guardEmptyCompletionEventStream(
165
+ options: EmptyCompletionGuardOptions,
166
+ ): AsyncGenerator<AdapterEvent> {
167
+ const maxRetries = Math.max(0, Math.floor(options.maxRetries ?? 1));
168
+ let source = options.firstEvents;
169
+ let held: AdapterEvent[] = [];
170
+ let heldBytes = 0;
171
+ let sawContent = false;
172
+ let passthrough = false;
173
+ let retries = 0;
174
+ let usage: OcxUsage | undefined;
175
+
176
+ const withUsage = (event: AdapterEvent & { usage?: OcxUsage }): AdapterEvent => {
177
+ const merged = mergeUsage(usage, event.usage);
178
+ return merged ? { ...event, ...(merged ? { usage: merged } : {}) } : event;
179
+ };
180
+ const releaseHeld = (): AdapterEvent[] => {
181
+ const released = held;
182
+ held = [];
183
+ heldBytes = 0;
184
+ return released;
185
+ };
186
+
187
+ while (true) {
188
+ let terminalSeen = false;
189
+ for await (const event of source) {
190
+ if (event.type === "heartbeat") {
191
+ yield event;
192
+ continue;
193
+ }
194
+ if (sawContent || passthrough) {
195
+ // Buffered content is already flowing; everything downstream passes
196
+ // through. Every terminal carries usage merged across every attempt.
197
+ yield isTerminalEvent(event) ? withUsage(event) : event;
198
+ continue;
199
+ }
200
+ if (isContentEvent(event)) {
201
+ sawContent = true;
202
+ yield* releaseHeld();
203
+ yield event;
204
+ continue;
205
+ }
206
+ if (event.type === "done") {
207
+ usage = mergeUsage(usage, event.usage);
208
+ if (event.stopReason !== undefined && VISIBLE_INCOMPLETE_STOP_REASONS.has(event.stopReason)) {
209
+ // Rendered as response.incomplete: a stated failure, not the silent
210
+ // empty success this guard exists to catch.
211
+ yield* releaseHeld();
212
+ yield { ...event, ...(usage ? { usage } : {}) };
213
+ return;
214
+ }
215
+ if (retries < maxRetries) {
216
+ // Suppress the terminal: the client must never see a completed event
217
+ // for a turn that produced nothing. Retry the identical turn.
218
+ retries += 1;
219
+ try {
220
+ source = await options.continuation();
221
+ } catch {
222
+ yield emptyCompletionRetryFailedEvent(usage, true);
223
+ return;
224
+ }
225
+ terminalSeen = true;
226
+ break;
227
+ }
228
+ // The retry was also empty: a stated failure, not a second silent
229
+ // success.
230
+ yield emptyCompletionRetryFailedEvent(usage);
231
+ return;
232
+ }
233
+ if (event.type === "error") {
234
+ if (retries > 0 && event.status !== 499) {
235
+ // The retry failed upstream. Its body cannot reach the client (the
236
+ // 200 head went out with the first attempt), so state the failure in
237
+ // the stream's own error framing — same move as the router's
238
+ // empty_completion_retry_failed. Client cancels (499) pass through.
239
+ yield emptyCompletionRetryFailedEvent(mergeUsage(usage, event.usage), true);
240
+ return;
241
+ }
242
+ yield* releaseHeld();
243
+ yield withUsage(event);
244
+ return;
245
+ }
246
+ if (event.type === "incomplete") {
247
+ // A structured incomplete is already a visible failure; never convert
248
+ // it into an empty completion.
249
+ yield* releaseHeld();
250
+ yield withUsage(event);
251
+ return;
252
+ }
253
+ const eventBytes = retainedEventBytes(event);
254
+ if (held.length + 1 > EMPTY_COMPLETION_MAX_BUFFERED_EVENTS
255
+ || heldBytes + eventBytes > EMPTY_COMPLETION_MAX_BUFFERED_BYTES) {
256
+ // Preserve data rather than retaining without bound: release the prefix,
257
+ // emit this event, and stop attempting an empty-completion retry for the turn.
258
+ yield* releaseHeld();
259
+ yield event;
260
+ passthrough = true;
261
+ continue;
262
+ }
263
+ held.push(event);
264
+ heldBytes += eventBytes;
265
+ // The bridge watchdog sees only yielded events. Feed it while reasoning is
266
+ // held so a long reasoning-only prefix remains live without exposing it early.
267
+ if (isReasoningEvent(event)) yield { type: "heartbeat" };
268
+ }
269
+ if (!terminalSeen) {
270
+ // The source ended without a terminal event (truncated stream). Release
271
+ // what was held so the bridge can mark the stream incomplete.
272
+ yield* releaseHeld();
273
+ return;
274
+ }
275
+ }
276
+ }
@@ -102,6 +102,7 @@ import {
102
102
  } from "../relay";
103
103
  import { hasResponsesItemIdRepair, relaySseWithResponsesItemIdRepair } from "../responses-item-id-repair";
104
104
  import type { EffectiveSubagentRoster, SpawnAgentSurface } from "../../codex/catalog";
105
+ import { waitForProviderRequestSlot } from "../../providers/request-pacing";
105
106
 
106
107
 
107
108
  export function disableResponsesRequestTimeout(req: Request, server: Pick<Server<WsData>, "timeout"> | undefined): boolean {
@@ -136,21 +137,48 @@ export function safeOriginLabel(url: string): string {
136
137
 
137
138
 
138
139
 
140
+ export interface PaceAwareFetch {
141
+ waitForPacing?: (signal?: AbortSignal) => Promise<void>;
142
+ unpacedFetch?: typeof globalThis.fetch;
143
+ }
144
+
145
+ export type ProviderFetch = typeof globalThis.fetch & PaceAwareFetch;
146
+
147
+ export interface ProviderFetchOptions {
148
+ providerName?: string;
149
+ modelId?: string;
150
+ }
151
+
139
152
  export function providerFetch(
140
153
  provider: OcxProviderConfig,
141
154
  runtime: BunRuntimeGateInput = currentBunRuntimeIdentity(),
142
- ): typeof globalThis.fetch {
155
+ options: ProviderFetchOptions = {},
156
+ ): ProviderFetch {
143
157
  const base = (provider as OcxProviderConfig & { fetch?: typeof globalThis.fetch }).fetch ?? globalThis.fetch;
144
158
  // ChatGPT Codex backend: streaming turns ride the responses_websockets
145
159
  // transport (measured ~3s faster TTFT than the SSE POST queue); everything
146
160
  // else keeps the provider's HTTP fetch. See ws-upstream.ts for the details.
147
- const wrapped = (input: Parameters<typeof globalThis.fetch>[0], init?: RequestInit) => {
161
+ const unpaced = async (input: Parameters<typeof globalThis.fetch>[0], init?: RequestInit) => {
148
162
  if (typeof input === "string" && init && shouldUseCodexWsUpstream(input, init, runtime)) {
149
163
  return codexWsUpstreamFetch(input, init, base, runtime);
150
164
  }
151
165
  return base(input, init);
152
166
  };
153
- return wrapped as typeof globalThis.fetch;
167
+ const waitForPacing = (signal?: AbortSignal) => options.providerName
168
+ ? waitForProviderRequestSlot(options.providerName, provider, options.modelId, signal)
169
+ : Promise.resolve();
170
+ const wrapped = async (input: Parameters<typeof globalThis.fetch>[0], init?: RequestInit) => {
171
+ await waitForPacing(init?.signal ?? undefined);
172
+ return unpaced(input, init);
173
+ };
174
+ const preconnect = (...args: Parameters<typeof globalThis.fetch.preconnect>): void => {
175
+ base.preconnect?.(...args);
176
+ };
177
+ return Object.assign(wrapped, {
178
+ preconnect,
179
+ waitForPacing,
180
+ unpacedFetch: Object.assign(unpaced, { preconnect }),
181
+ });
154
182
  }
155
183
 
156
184
 
@@ -164,6 +192,9 @@ export async function fetchWithHeaderTimeout(
164
192
  executor: typeof globalThis.fetch = globalThis.fetch,
165
193
  manualRedirect = false,
166
194
  ): Promise<Response> {
195
+ const pacing = executor as ProviderFetch;
196
+ await pacing.waitForPacing?.(abortSignal);
197
+ const fetchExecutor = pacing.unpacedFetch ?? executor;
167
198
  const timeout = new AbortController();
168
199
  const timer = setTimeout(() => {
169
200
  if (!timeout.signal.aborted) timeout.abort(new DOMException("Timeout elapsed", "TimeoutError"));
@@ -175,7 +206,7 @@ export async function fetchWithHeaderTimeout(
175
206
  headers.set("accept-encoding", "identity");
176
207
  }
177
208
  try {
178
- return await executor(url, {
209
+ return await fetchExecutor(url, {
179
210
  ...init,
180
211
  headers,
181
212
  // Credential-bearing sends opt into manual redirects so a 3xx is relayed
@@ -0,0 +1,13 @@
1
+ import { formatErrorResponse } from "../../bridge";
2
+ import { RequestPacingQueueOverloadError } from "../../providers/request-pacing";
3
+
4
+ /** Convert local request-pacing admission failures into a retryable HTTP response. */
5
+ export function requestPacingOverloadResponse(error: unknown): Response | undefined {
6
+ if (!(error instanceof RequestPacingQueueOverloadError)) return undefined;
7
+ return formatErrorResponse(
8
+ 429,
9
+ "rate_limit_error",
10
+ error.message,
11
+ { retryAfter: String(error.retryAfterSeconds) },
12
+ );
13
+ }
@@ -5,6 +5,7 @@ import { finishRequestAttempt, type RequestLogContext } from "../request-log";
5
5
  import type { OcxConfig } from "../../types";
6
6
  import type { RouteCandidateTrace, RouteDecisionTraceV1 } from "../../routing/trace";
7
7
  import { handleResponses as handleResponsesCore } from "./core";
8
+ import { requestPacingOverloadResponse } from "./pacing-overload";
8
9
 
9
10
  type CoreHandler = typeof handleResponsesCore;
10
11
  type CoreOptions = Parameters<CoreHandler>[3];
@@ -133,7 +134,14 @@ export async function handleResponsesWithPolicyFallback(
133
134
  // Core owns the client-facing parse/decompression error.
134
135
  }
135
136
 
136
- let response = await runCore(req, config, logCtx, coreOptions);
137
+ let response: Response;
138
+ try {
139
+ response = await runCore(req, config, logCtx, coreOptions);
140
+ } catch (error) {
141
+ const overload = requestPacingOverloadResponse(error);
142
+ if (overload) return overload;
143
+ throw error;
144
+ }
137
145
  const initialTrace = logCtx.routeDecision;
138
146
  const initialRequestedModel = logCtx.requestedModel;
139
147
  if (!rawBody || !isPolicyDecision(initialTrace)) return response;
@@ -151,7 +159,13 @@ export async function handleResponsesWithPolicyFallback(
151
159
  finishFailedPolicyAttempt(logCtx, response.status);
152
160
  const retryRequest = requestWithCandidate(req, rawBody, next);
153
161
  try {
154
- response = await runCore(retryRequest, config, logCtx, coreOptions);
162
+ try {
163
+ response = await runCore(retryRequest, config, logCtx, coreOptions);
164
+ } catch (error) {
165
+ const overload = requestPacingOverloadResponse(error);
166
+ if (overload) return overload;
167
+ throw error;
168
+ }
155
169
  } finally {
156
170
  logCtx.requestedModel = initialRequestedModel;
157
171
  logCtx.routeDecision = initialTrace;
@@ -195,7 +195,7 @@ export async function* guardTerminalEventStream(options: GuardedEventStreamOptio
195
195
  for await (const event of source) {
196
196
  if (event.type === "done") {
197
197
  terminalSeen = true;
198
- const analysis = options.adapterName === "anthropic"
198
+ const analysis = (options.adapterName === "anthropic" || options.adapterName === "openai-chat")
199
199
  ? analyzeTerminalTurn(parsed, seen)
200
200
  : { decision: "pass" as const };
201
201
  const normalStop = event.stopReason !== "max_tokens" && event.stopReason !== "content_filter";
@@ -5,7 +5,12 @@
5
5
  * #553 looking for an adapter URL bug that does not exist. Name the likely cause and the
6
6
  * command that settles it.
7
7
  */
8
+ import { RequestPacingQueueOverloadError } from "../../providers/request-pacing";
9
+
8
10
  export function describeUpstreamConnectFailure(err: unknown, connectMs: number): string {
11
+ // Local pacing admission is not a transport failure. Let the outer response boundary
12
+ // preserve its retryable 429 identity instead of laundering it into a 502.
13
+ if (err instanceof RequestPacingQueueOverloadError) throw err;
9
14
  if (err instanceof Error && err.name === "TimeoutError") {
10
15
  return `Provider connect timeout after ${connectMs}ms`;
11
16
  }
@@ -1,10 +1,25 @@
1
1
  // AUTO-SPLIT facade: original responses.ts body moved into ./responses/* modules.
2
2
  // Public surface preserved exactly; importers keep using "src/server/responses".
3
+ import { handleResponsesCompact as handleResponsesCompactImpl } from "./responses/compact";
4
+ import { requestPacingOverloadResponse } from "./responses/pacing-overload";
5
+
3
6
  export { buildToolBridgeMaps, isV1CollabSurface, collabSurface, multiAgentGuidanceText, V2_GUIDANCE_CHAR_BUDGET, injectDeveloperMessage } from "./responses/collaboration";
4
7
  export type { MultiAgentGuidanceOptions, MultiAgentGuidanceDeps } from "./responses/collaboration";
5
8
  export { hasUnreadableEncryptedAgentTask, sanitizeEncryptedContentInPlace } from "./responses/encrypted-payload";
6
- export { COMPACT_RESPONSE_MAX_BYTES, bufferCompactResponse, handleResponsesCompact } from "./responses/compact";
9
+ export { COMPACT_RESPONSE_MAX_BYTES, bufferCompactResponse } from "./responses/compact";
7
10
  export { disableResponsesRequestTimeout, safeHostLabel, fetchWithHeaderTimeout } from "./responses/fetch-helpers";
8
11
  export { sidecarOutcomeRecorder, isShadowSourceModel, codexLogAccountId, usesCodexForwardPoolAuth, codexForwardTerminalOutcomeRecorder, decodeRequestErrorResponse, buildComboChildHeaders, linkAbortSignal } from "./responses/core";
9
12
  export { handleResponses, handleResponsesWithPolicyFallback, rankPolicyFallbackCandidates } from "./responses/policy-fallback";
10
- export { adapterNeedsForcedContinuation } from "./responses/core";
13
+ export { adapterNeedsForcedContinuation } from "./responses/core";
14
+
15
+ export async function handleResponsesCompact(
16
+ ...args: Parameters<typeof handleResponsesCompactImpl>
17
+ ): Promise<Response> {
18
+ try {
19
+ return await handleResponsesCompactImpl(...args);
20
+ } catch (error) {
21
+ const overload = requestPacingOverloadResponse(error);
22
+ if (overload) return overload;
23
+ throw error;
24
+ }
25
+ }
@@ -335,7 +335,7 @@ export async function injectSystemEnv(port: number, config: OcxConfig): Promise<
335
335
  // without a token — keep it in sync with this proxy's /v1/models. Best-effort.
336
336
  try {
337
337
  const { refreshGatewayModelCacheFromProxy } = await import("../claude/gateway-cache");
338
- await refreshGatewayModelCacheFromProxy(port);
338
+ await refreshGatewayModelCacheFromProxy(port, { admissionConfig: config });
339
339
  } catch { /* best-effort */ }
340
340
 
341
341
  // Roster agent definitions (devlog 070): same launch-time sync for plain `claude`.
package/src/types.ts CHANGED
@@ -180,10 +180,27 @@ export interface OcxToolCall {
180
180
  arguments: Record<string, unknown>;
181
181
  customWireName?: string;
182
182
  thoughtSignature?: string;
183
+ /**
184
+ * Provider-issued opaque metadata that must survive the whole round trip unchanged
185
+ * (issue #1735). A signed Gemini part is only valid when its signature comes back on the
186
+ * SAME part it was issued for, so this travels with the individual tool call rather than
187
+ * being matched by name/arguments after the fact.
188
+ */
189
+ providerMetadata?: OcxProviderOpaqueToolCallMetadata;
183
190
  /** MCP namespace (e.g. "mcp__context7") when this call targets a namespaced tool. */
184
191
  namespace?: string;
185
192
  }
186
193
 
194
+ /**
195
+ * Opaque, provider-scoped tool-call metadata. Values are never parsed, merged, re-encoded, or
196
+ * synthesized — they are carried verbatim or not at all.
197
+ */
198
+ export interface OcxProviderOpaqueToolCallMetadata {
199
+ google?: {
200
+ thoughtSignature?: string;
201
+ };
202
+ }
203
+
187
204
  export type OcxAssistantContentPart = OcxTextContent | OcxThinkingContent | OcxToolCall;
188
205
 
189
206
  export interface OcxTool {
@@ -328,7 +345,7 @@ export type AdapterEvent =
328
345
  // Never rendered — it only rides the reasoning item's envelope so the next request can replay it.
329
346
  | { type: "kiro_redacted_reasoning"; data: string }
330
347
  | { type: "reasoning_raw_delta"; text: string }
331
- | { type: "tool_call_start"; id: string; name: string }
348
+ | { type: "tool_call_start"; id: string; name: string; providerMetadata?: OcxProviderOpaqueToolCallMetadata }
332
349
  | { type: "tool_call_delta"; arguments: string }
333
350
  | { type: "tool_call_end" }
334
351
  /** Internal boundary between a guarded first pass and its one-shot continuation. */
@@ -587,6 +604,13 @@ export interface OcxCustomModel {
587
604
  contextWindow?: number;
588
605
  /** 입력 모달리티 (선택, 기본 ["text"]) */
589
606
  inputModalities?: string[];
607
+ /**
608
+ * Reasoning ladder (Codex labels) this custom row explicitly advertises. An empty array
609
+ * hides the effort control; an omitted key leaves the provider-derived ladder in charge.
610
+ */
611
+ reasoningEfforts?: string[];
612
+ /** Default effort label when `reasoningEfforts` is non-empty. */
613
+ defaultReasoningEffort?: string;
590
614
  /** 추가 시각 (ISO 8601) */
591
615
  addedAt?: string;
592
616
  }
@@ -623,6 +647,8 @@ export interface OcxClientIntegrationsConfig {
623
647
 
624
648
  export interface OcxConfig {
625
649
  port: number;
650
+ /** Opt in to one identical-turn retry when a Responses completion has no text or tool call. */
651
+ emptyCompletionRetry?: boolean;
626
652
  /** Maximum usage-log bytes read for one management snapshot. */
627
653
  managementUsageMaxReadBytes?: number;
628
654
  providers: Record<string, OcxProviderConfig>;
@@ -645,6 +671,20 @@ export interface OcxConfig {
645
671
  * into a selector-qualified group; Codex still advertises only the first 5 visible rows.
646
672
  */
647
673
  subagentModels?: string[];
674
+ /**
675
+ * Optional full picker ordering for the Codex model catalog, independent of the
676
+ * 5-slot `subagentModels` spawn_agent cap. DISPLAY-ONLY: it controls the visual order of
677
+ * the Codex model picker for large routed catalogs (10-20+ models) that would otherwise sort
678
+ * arbitrarily and reshuffle on every rebuild. Values are routed `<provider>/<model>` catalog
679
+ * slugs (matched by exact slug or `provider/id`); native OpenAI passthrough rows and
680
+ * account-qualified native rows are not reordered (order native rows via `subagentModels`).
681
+ * Listed routed rows appear in array order; rows not listed keep their normal display order.
682
+ * `subagentModels`-featured rows keep their top position. When unset or empty, catalog
683
+ * priority is unchanged. This changes ONLY what the user sees in the picker: the spawn_agent
684
+ * candidate set is derived from each row's natural priority and is provably unaffected, even
685
+ * when every routed row is listed (see opencodex_spawn_priority / effectiveSubagentRoster).
686
+ */
687
+ modelPickerOrder?: string[];
648
688
  /**
649
689
  * Priority-ordered fallback models for spawned sub-agents. When the requested
650
690
  * model is quota-exhausted or recently failed, opencodex rewrites the child
@@ -966,6 +1006,12 @@ export interface OcxComboConfig {
966
1006
  stickyLimit?: number;
967
1007
  /** Used when the client omits reasoning.effort. null/omitted leaves the target default unchanged. */
968
1008
  defaultEffort?: OcxComboDefaultEffort | null;
1009
+ /**
1010
+ * Disable image input even when every target supports it.
1011
+ * Omitted / `"auto"` keeps automatic capability derivation (default: enabled when
1012
+ * the target intersection includes image).
1013
+ */
1014
+ imageInput?: "auto" | "disabled";
969
1015
  /**
970
1016
  * Optional public model name replacing the default `combo/<id>` slug. Bare names
971
1017
  * without "/" are allowed (e.g. "deepseek-v4-flash") so the combo can answer to a
@@ -1234,12 +1280,28 @@ export interface ProviderCostOverlay {
1234
1280
  cacheWrite: number;
1235
1281
  }
1236
1282
 
1283
+ export interface RequestPacingRule {
1284
+ /** Evenly spread request starts to this many requests per minute. */
1285
+ requestsPerMinute?: number;
1286
+ /** Minimum delay between request starts. The slower configured value wins. */
1287
+ minIntervalMs?: number;
1288
+ }
1289
+
1290
+ export interface ProviderRequestPacingConfig extends RequestPacingRule {
1291
+ /** False preserves legacy behavior with no client-side waiting. */
1292
+ enabled: boolean;
1293
+ /** Exact upstream model-id overrides; other models inherit the provider rule. */
1294
+ models?: Record<string, RequestPacingRule>;
1295
+ }
1296
+
1237
1297
  /**
1238
1298
  * One configured provider entry. `authMode` (default `"key"`) decides whether same-target 429
1239
1299
  * retries are allowed; OAuth/forward credentials and local runtimes are never replayed.
1240
1300
  */
1241
1301
  export interface OcxProviderConfig {
1242
1302
  adapter: string;
1303
+ /** Optional outbound request-start pacing shared by this provider and its model overrides. */
1304
+ requestPacing?: ProviderRequestPacingConfig;
1243
1305
  /** Cursor MCP compatibility bounds; positive integers when configured. */
1244
1306
  mcpMaxTools?: number;
1245
1307
  mcpMaxSchemaBytes?: number;
@@ -1282,8 +1344,10 @@ export interface OcxProviderConfig {
1282
1344
  */
1283
1345
  requiresAdjacentResponsesToolResults?: boolean;
1284
1346
  /**
1285
- * Whether this provider's Responses route honours the OpenAI `service_tier`
1286
- * parameter. Tri-state: `true` lets fast mode inject/remove the field (an unset
1347
+ * Provider fallback for the OpenAI `service_tier` parameter. On Responses routes this
1348
+ * is the complete wire opt-in; Chat routes additionally require `chatServiceTier` or an
1349
+ * exact-model true declaration.
1350
+ * Tri-state: `true` lets fast mode inject/remove the field (an unset
1287
1351
  * fast mode preserves a caller-supplied value); `false` strips the field and
1288
1352
  * never injects, because an upstream documented as not supporting the parameter
1289
1353
  * must not receive it; absent (`undefined`) leaves the provider unclassified —
@@ -1291,6 +1355,8 @@ export interface OcxProviderConfig {
1291
1355
  * An explicit config value always wins over the registry default.
1292
1356
  */
1293
1357
  supportsServiceTier?: boolean;
1358
+ /** Exact upstream model ids that override the provider-level service-tier capability. */
1359
+ modelSupportsServiceTier?: Record<string, boolean>;
1294
1360
  /**
1295
1361
  * Responses upstream whose native contract accepts plaintext reasoning replay
1296
1362
  * (DeepSeek documents reasoning items with plaintext content). When set, the
@@ -1468,6 +1534,18 @@ export interface OcxProviderConfig {
1468
1534
  * No effect unless `parallelToolCalls === false`; ignored by non-`openai-chat` adapters.
1469
1535
  */
1470
1536
  pinParallelToolCallsFalse?: boolean;
1537
+ /**
1538
+ * Opt-in: extend the no-tool-call terminal continuation guard to this provider's
1539
+ * `openai-chat` routed turns. The guard (originally Anthropic-only, see
1540
+ * devlog/_fin/260706_previous-response-id-400) issues one bounded internal re-ask when a
1541
+ * model announces work but ends the turn without emitting a tool call. Self-hosted
1542
+ * OpenAI-compatible gateways (GLM/Kimi-family, etc.) hit the same premature-completion
1543
+ * pattern, but the heuristic that decides a "suspicious no-tool stop" was tuned on
1544
+ * Anthropic turns, so it stays OFF by default for the many registry providers that share
1545
+ * the `openai-chat` adapter. Enable only for a provider whose models are known to stop
1546
+ * mid-work; non-`openai-chat` adapters ignore this flag.
1547
+ */
1548
+ terminalContinuationGuard?: boolean;
1471
1549
  /**
1472
1550
  * Opt-in: forward `prompt_cache_key` to the upstream `/chat/completions` body.
1473
1551
  * OpenAI-specific extension; strict backends (Groq, Cerebras, etc.) reject unknown
@@ -1479,8 +1557,9 @@ export interface OcxProviderConfig {
1479
1557
  * OpenAI-specific extension with the same hazard as `promptCacheKey` — strict backends
1480
1558
  * reject unknown fields, and 66 registry providers share the `openai-chat` adapter, so a
1481
1559
  * caller-supplied `service_tier` would otherwise turn working requests into upstream 400s.
1482
- * `supportsServiceTier` is the Responses-wire flag and does not apply here.
1483
- * Default off; only enable for providers that document this parameter on the chat wire.
1560
+ * Exact models may opt in through `modelSupportsServiceTier` instead; provider-level
1561
+ * `supportsServiceTier: false` remains a global denial. Default off; only enable for
1562
+ * providers that document this parameter on the chat wire.
1484
1563
  */
1485
1564
  chatServiceTier?: boolean;
1486
1565
  /**