@bitkyc08/opencodex 2.55.0 → 2.56.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 (167) hide show
  1. package/gui/dist/assets/{index-VuoiWj9J.js → index-D4zuyIxQ.js} +1 -1
  2. package/gui/dist/index.html +1 -1
  3. package/package.json +2 -1
  4. package/src/adapters/base.ts +21 -0
  5. package/src/adapters/cursor/transport-retry.ts +46 -1
  6. package/src/adapters/cursor.ts +4 -0
  7. package/src/adapters/kiro/adapter.ts +42 -1
  8. package/src/adapters/kiro-retry.ts +23 -4
  9. package/src/adapters/openai-chat/errors.ts +116 -0
  10. package/src/adapters/openai-chat/messages.ts +346 -0
  11. package/src/adapters/openai-chat/passthrough.ts +146 -0
  12. package/src/adapters/openai-chat/response-events.ts +117 -0
  13. package/src/adapters/openai-chat/tool-call-validation.ts +200 -0
  14. package/src/adapters/openai-chat/tool-schema.ts +477 -0
  15. package/src/adapters/openai-chat/wire.ts +50 -0
  16. package/src/adapters/openai-chat.ts +33 -1445
  17. package/src/adapters/openai-responses/canonical-forward.ts +202 -0
  18. package/src/adapters/openai-responses/image-gen.ts +406 -0
  19. package/src/adapters/openai-responses/internal.ts +3 -0
  20. package/src/adapters/openai-responses/passthrough.ts +611 -0
  21. package/src/adapters/openai-responses/prompt-cache.ts +83 -0
  22. package/src/adapters/openai-responses/reasoning.ts +220 -0
  23. package/src/adapters/openai-responses/request-strips.ts +185 -0
  24. package/src/adapters/openai-responses/tool-output-recovery.ts +509 -0
  25. package/src/adapters/openai-responses/tool-schema.ts +293 -0
  26. package/src/adapters/openai-responses/web-search.ts +156 -0
  27. package/src/adapters/openai-responses.ts +4 -2625
  28. package/src/bridge/errors.ts +34 -0
  29. package/src/bridge/internal.ts +174 -0
  30. package/src/bridge/response-json.ts +624 -0
  31. package/src/bridge/sse.ts +1444 -0
  32. package/src/bridge.ts +5 -2204
  33. package/src/chat/inbound.ts +12 -1
  34. package/src/codex/account-lifecycle.ts +3 -0
  35. package/src/codex/account-store.ts +71 -9
  36. package/src/codex/auth-api/account-list.ts +507 -0
  37. package/src/codex/auth-api/http.ts +32 -0
  38. package/src/codex/auth-api/login-flow.ts +554 -0
  39. package/src/codex/auth-api/login-state.ts +64 -0
  40. package/src/codex/auth-api/main-account-probe.ts +331 -0
  41. package/src/codex/auth-api/pool-mode-gate.ts +274 -0
  42. package/src/codex/auth-api/pool-quota-probe.ts +512 -0
  43. package/src/codex/auth-api/reset-credit-service.ts +422 -0
  44. package/src/codex/auth-api/routes.ts +425 -0
  45. package/src/codex/auth-api/runtime-config.ts +48 -0
  46. package/src/codex/auth-api.ts +27 -3118
  47. package/src/codex/auth-context.ts +95 -28
  48. package/src/codex/catalog/auto-review.ts +507 -0
  49. package/src/codex/catalog/build-entries.ts +981 -0
  50. package/src/codex/catalog/combo-member.ts +375 -0
  51. package/src/codex/catalog/derive-entry.ts +229 -0
  52. package/src/codex/catalog/effort.ts +0 -1
  53. package/src/codex/catalog/gated-native-warn.ts +63 -0
  54. package/src/codex/catalog/gather-capture.ts +533 -0
  55. package/src/codex/catalog/model-hints.ts +691 -0
  56. package/src/codex/catalog/model-visibility.ts +304 -0
  57. package/src/codex/catalog/provider-fetch.ts +52 -2942
  58. package/src/codex/catalog/provider-models.ts +685 -0
  59. package/src/codex/catalog/restore.ts +132 -0
  60. package/src/codex/catalog/retained-sync.ts +706 -0
  61. package/src/codex/catalog/routed-gather.ts +858 -0
  62. package/src/codex/catalog/subagent-roster.ts +176 -0
  63. package/src/codex/catalog/sync.ts +52 -2698
  64. package/src/codex/inject/config-toml.ts +563 -0
  65. package/src/codex/inject/remove.ts +192 -0
  66. package/src/codex/inject/restore.ts +540 -0
  67. package/src/codex/inject/routing-classify.ts +109 -0
  68. package/src/codex/inject/routing-target.ts +125 -0
  69. package/src/codex/inject.ts +81 -1436
  70. package/src/codex/lineage.ts +458 -0
  71. package/src/codex/pool-refresh-backoff.ts +152 -0
  72. package/src/codex/routing/active-account.ts +194 -0
  73. package/src/codex/routing/cooldown-math.ts +275 -0
  74. package/src/codex/routing/health-store.ts +402 -0
  75. package/src/codex/routing/probe-lease.ts +358 -0
  76. package/src/codex/routing/selection.ts +703 -0
  77. package/src/codex/routing/thread-affinity.ts +538 -0
  78. package/src/codex/routing.ts +353 -2234
  79. package/src/codex/shim-fingerprint.ts +223 -0
  80. package/src/codex/shim-inspect.ts +175 -0
  81. package/src/codex/shim-probe.ts +367 -0
  82. package/src/codex/shim-restore-lock.ts +169 -0
  83. package/src/codex/shim-state-file.ts +151 -0
  84. package/src/codex/shim-templates.ts +265 -0
  85. package/src/codex/shim.ts +48 -1268
  86. package/src/config/diagnostics.ts +705 -0
  87. package/src/config/feature-flags.ts +55 -0
  88. package/src/config/live-reconcile.ts +403 -0
  89. package/src/config/load-degrade.ts +880 -0
  90. package/src/config/mutation-lock.ts +244 -0
  91. package/src/config/openai-tier-backup.ts +268 -0
  92. package/src/config/persist-unlocked.ts +92 -0
  93. package/src/config/proxy-env.ts +188 -0
  94. package/src/config/salvage.ts +244 -0
  95. package/src/config/schema/config-schema.ts +640 -0
  96. package/src/config/schema/leaf-validators.ts +855 -0
  97. package/src/config/warn-memo.ts +28 -0
  98. package/src/config.ts +234 -4481
  99. package/src/generated/compatibility-version.json +539 -39
  100. package/src/lib/request-execution-budget.ts +69 -20
  101. package/src/lib/spend-reservation-ledger.ts +940 -0
  102. package/src/lib/upstream-retry.ts +55 -11
  103. package/src/lib/workflow-budget.ts +553 -30
  104. package/src/providers/quota/account-cache.ts +441 -0
  105. package/src/providers/quota/antigravity.ts +295 -0
  106. package/src/providers/quota/report-cache.ts +320 -0
  107. package/src/providers/quota/vendor-probes-key.ts +1243 -0
  108. package/src/providers/quota/vendor-probes-oauth.ts +590 -0
  109. package/src/providers/quota.ts +324 -3079
  110. package/src/providers/registry/entries-core.ts +1221 -0
  111. package/src/providers/registry/entries-extended.ts +1204 -0
  112. package/src/providers/registry/model-seeds.ts +908 -0
  113. package/src/providers/registry/types.ts +352 -0
  114. package/src/providers/registry.ts +24 -3536
  115. package/src/responses/continuation-ownership.ts +29 -0
  116. package/src/responses/state/replay-fingerprint.ts +80 -0
  117. package/src/responses/state/snapshot-codec.ts +104 -0
  118. package/src/responses/state/spill-failure.ts +118 -0
  119. package/src/responses/state/spill-queue.ts +665 -0
  120. package/src/responses/state/temp-recovery.ts +257 -0
  121. package/src/responses/state.ts +82 -1143
  122. package/src/routing/identity-domains.ts +449 -0
  123. package/src/routing/probe-lease.ts +511 -0
  124. package/src/server/index/bounded-request.ts +88 -0
  125. package/src/server/index/live-sideband.ts +565 -0
  126. package/src/server/index/serve-options.ts +1766 -0
  127. package/src/server/index/startup-warnings.ts +213 -0
  128. package/src/server/index/websocket-handler.ts +335 -0
  129. package/src/server/index.ts +40 -2547
  130. package/src/server/management/route-registry.ts +26 -23
  131. package/src/server/management/shared.ts +8 -5
  132. package/src/server/management/workflow-budget-routes.ts +133 -0
  133. package/src/server/management-api.ts +12 -0
  134. package/src/server/request-log-conversation.ts +9 -7
  135. package/src/server/request-log.ts +245 -1
  136. package/src/server/responses/account-change-state.ts +233 -0
  137. package/src/server/responses/adapter-continuation.ts +514 -0
  138. package/src/server/responses/adapter-delivery.ts +214 -0
  139. package/src/server/responses/adapter-dispatch.ts +971 -0
  140. package/src/server/responses/compact.ts +59 -4
  141. package/src/server/responses/completion-policy.ts +33 -0
  142. package/src/server/responses/core-auth.ts +527 -0
  143. package/src/server/responses/core-codex-account.ts +859 -0
  144. package/src/server/responses/core-combo-failure.ts +210 -0
  145. package/src/server/responses/core-combo.ts +707 -0
  146. package/src/server/responses/core-errors.ts +152 -0
  147. package/src/server/responses/core-lifetime.ts +95 -0
  148. package/src/server/responses/core-normalize.ts +350 -0
  149. package/src/server/responses/core-opaque-recovery.ts +380 -0
  150. package/src/server/responses/core-options.ts +159 -0
  151. package/src/server/responses/core-replay.ts +225 -0
  152. package/src/server/responses/core.ts +192 -8893
  153. package/src/server/responses/passthrough-delivery.ts +856 -0
  154. package/src/server/responses/passthrough-dispatch.ts +1476 -0
  155. package/src/server/responses/passthrough-execution.ts +54 -0
  156. package/src/server/responses/request-prepare.ts +970 -0
  157. package/src/server/responses/request-send-budget.ts +164 -0
  158. package/src/server/responses/request-sidecar-auth.ts +149 -0
  159. package/src/server/responses/request-transport.ts +744 -0
  160. package/src/server/responses/response-effects.ts +157 -0
  161. package/src/server/responses/run-turn-execution.ts +448 -0
  162. package/src/server/responses/sidecar-execution.ts +469 -0
  163. package/src/server/responses-image-gen-repair.ts +1 -1
  164. package/src/server/workflow-refusal.ts +84 -0
  165. package/src/types/config.ts +30 -0
  166. package/src/usage/log.ts +146 -0
  167. package/src/usage/summary.ts +171 -21
@@ -0,0 +1,1444 @@
1
+ import type {
2
+ AdapterEvent,
3
+ OcxMessagePhase,
4
+ OcxProviderContinuationState,
5
+ OcxProviderOpaqueToolCallMetadata,
6
+ OcxReasoningReplayScopeRef,
7
+ OcxUsage,
8
+ } from "../types";
9
+ import { coerceIntegerToolArguments } from "../lib/tool-argument-integers";
10
+ import {
11
+ adapterFailureFromMessage,
12
+ classifyError,
13
+ cyberPolicyErrorType,
14
+ CYBER_POLICY_ERROR_CODE,
15
+ isCyberPolicyCode,
16
+ type OcxErrorPayload,
17
+ } from "../lib/errors";
18
+ import { redactSecretString } from "../lib/redact";
19
+ import { mayBecomePatchEnvelope, repairFreeformToolInput } from "../responses/apply-patch-envelope";
20
+ import { encodeCompactionSummary } from "../responses/compaction";
21
+ import { compileCodeModeHelperInput, resolveCodeModeHelperName } from "../responses/code-mode-helper-compat";
22
+ import { isTruncatedStopReason, truncationReasonFor } from "../responses/truncated-stop-reason";
23
+ import { encodeReasoningEnvelope, type ReasoningEnvelope } from "../responses/reasoning-envelope";
24
+ import { rememberReasoningForCall } from "../responses/reasoning-replay-cache";
25
+ import {
26
+ rememberAndSerializeExtraContent,
27
+ rememberExtraContentForReplay,
28
+ awaitThoughtSignatureDurability,
29
+ } from "../responses/thought-signature-replay";
30
+ import { resolveStallTimeoutSec } from "../stall-timeout";
31
+ import {
32
+ createCitationMarkerFilter,
33
+ stripCitationMarkers,
34
+ type CitationMarkerFilter,
35
+ } from "../responses/citation-markers";
36
+ import { declaresCodeModeExec, normalizeDeclaredToolName } from "../types";
37
+ import { appendSafeWebSearchSource, safeWebSearchSources } from "../web-search/sources";
38
+ import {
39
+ isTranslatorBudgetExceededError,
40
+ releaseTranslatedEvent,
41
+ createTranslatorBudget,
42
+ type TranslatorBudget,
43
+ type TranslatorBufferKind,
44
+ } from "../lib/translator-budget";
45
+ import { adapterFailureFromEvent, emptyChunks, joinChunks, ownedBudgetAbandonedMs, responsesUsage, toolCallArgumentsUsable, uuid, webSearchAction } from "./internal";
46
+ import type { OutputItem, StringChunks } from "./internal";
47
+
48
+ function sseEvent(name: string, data: Record<string, unknown>): string {
49
+ return `event: ${name}\ndata: ${JSON.stringify(data)}\n\n`;
50
+ }
51
+
52
+ function responseError(status: number, type: string, message: string): OcxErrorPayload {
53
+ return classifyError(status, type, message);
54
+ }
55
+
56
+ export type ResponsesTerminalStatus = "completed" | "failed" | "incomplete";
57
+
58
+ export function bridgeToResponsesSSE(
59
+ events: AsyncIterable<AdapterEvent>,
60
+ modelId: string,
61
+ toolNsMap?: Map<string, { namespace: string; name: string; freeform?: true }>,
62
+ freeformToolNames?: Set<string>,
63
+ toolSearchToolNames?: Set<string>,
64
+ onCancel?: () => void,
65
+ heartbeatMs = 2_000,
66
+ options?: {
67
+ responseId?: string;
68
+ stallTimeoutSec?: number;
69
+ hideThinkingSummary?: boolean;
70
+ /**
71
+ * Remote compaction v2 turn: accumulate all assistant text and, on done, emit ONE synthetic
72
+ * `{type:"compaction", encrypted_content:"ocx1:"+base64(text)}` output item before
73
+ * response.completed — codex-rs collect_compaction_output requires exactly one.
74
+ */
75
+ compaction?: boolean;
76
+ /** One-shot: first non-empty text/thinking/raw-reasoning delta observed (WP4 TTFT). */
77
+ onFirstOutput?: () => void;
78
+ onTerminal?: (status: ResponsesTerminalStatus) => void;
79
+ onCompletedResponse?: (response: Record<string, unknown>, providerState?: OcxProviderContinuationState) => void;
80
+ /**
81
+ * Raw adapter-reported usage at the terminal event, BEFORE wire normalization.
82
+ * responsesUsage() always emits token-detail objects with zero defaults for strict
83
+ * clients (grok-build), which makes the wire unusable as a provenance source: the
84
+ * request log must not read synthetic zeros as measured cache/reasoning numbers
85
+ * (cache_detail_missing would be silently suppressed). Callers set logCtx.usage
86
+ * from this callback instead of re-parsing the bridged SSE.
87
+ */
88
+ onUsage?: (usage: OcxUsage | undefined) => void;
89
+ /** Request-visible tool names. When present, an upstream call outside this set fails closed. */
90
+ declaredToolNames?: ReadonlySet<string>;
91
+ /** Declared parameter schema per tool name; repairs integral-float integer args (#1611). */
92
+ toolParameterSchemas?: ReadonlyMap<string, Record<string, unknown>>;
93
+ /**
94
+ * Wire keep-alive shape. Codex-rs parses at the EVENT level (timeout(idle_timeout,
95
+ * stream.next()) over an eventsource_stream), so an SSE comment line dispatches no event
96
+ * and does NOT re-arm its idle timer — the keep-alive must be a typed frame the parser
97
+ * ignores via its catch-all (110 RCA, 30_patch-direction.md). grok-build's strict
98
+ * async-openai fork is the opposite: it dies on the unknown `response.heartbeat`
99
+ * variant but, being eventsource-based at the byte level, its idle handling tolerates
100
+ * comment lines. Default stays the typed frame; the grok surface opts into comments.
101
+ */
102
+ heartbeatStyle?: "typed" | "comment";
103
+ translatorBudget?: TranslatorBudget;
104
+ /**
105
+ * Conversation identity for the reasoning replay cache (issue #950).
106
+ * Provider call ids are not globally unique; scoping by thread keeps one
107
+ * conversation's reasoning out of another's continuations.
108
+ */
109
+ replayCacheScope?: OcxReasoningReplayScopeRef;
110
+ /**
111
+ * Test seam for the wire/stall beat loop. Production omits this and uses the
112
+ * global timers; injecting here must not change scheduling semantics.
113
+ */
114
+ timers?: {
115
+ setInterval: (handler: () => void, ms: number) => unknown;
116
+ clearInterval: (id: unknown) => void;
117
+ };
118
+ },
119
+ ): ReadableStream<Uint8Array> {
120
+ const replayCacheScope = options?.replayCacheScope;
121
+ const setBeatInterval = options?.timers?.setInterval ?? ((handler: () => void, ms: number) => setInterval(handler, ms));
122
+ const clearBeatInterval = options?.timers?.clearInterval ?? ((id: unknown) => clearInterval(id as ReturnType<typeof setInterval>));
123
+ // Freeform/custom tools (apply_patch, code-mode exec) carry their body in `input`; the
124
+ // model is given a function with `{input:string}`, so unwrap it here when relaying back
125
+ // as a custom_tool_call. Decorated apply_patch envelopes are repaired at this boundary.
126
+ const freeformInput = (
127
+ args: string,
128
+ toolName: string,
129
+ namespace?: string,
130
+ codeModeHelperName?: string,
131
+ ): string => {
132
+ const helper = resolveCodeModeHelperName(codeModeHelperName, toolName, args, namespace, options?.declaredToolNames);
133
+ return helper
134
+ ? compileCodeModeHelperInput(args, helper)
135
+ : repairFreeformToolInput(args, toolName, namespace);
136
+ };
137
+ // Best-effort unwrap of a PARTIAL freeform arg buffer for live input streaming
138
+ // (`response.custom_tool_call_input.delta` — codex-rs uses it for UI preview only;
139
+ // the completed custom_tool_call item stays authoritative). Compact `{"input":"...`
140
+ // buffers get their string value progressively unescaped; anything else streams raw.
141
+ const FREEFORM_WRAP_PREFIX = '{"input":"';
142
+ const freeformPartialInput = (args: string): string => {
143
+ if (!args.startsWith(FREEFORM_WRAP_PREFIX)) return args;
144
+ const body = args.slice(FREEFORM_WRAP_PREFIX.length);
145
+ let out = "";
146
+ for (let i = 0; i < body.length; i++) {
147
+ const c = body[i];
148
+ if (c === '"') break; // unescaped closing quote: value complete
149
+ if (c === "\\") {
150
+ const n = body[i + 1];
151
+ if (n === undefined) break; // escape split across chunks: wait for more
152
+ i++;
153
+ if (n === "n") out += "\n";
154
+ else if (n === "t") out += "\t";
155
+ else if (n === "r") out += "\r";
156
+ else if (n === "u") {
157
+ const hex = body.slice(i + 1, i + 5);
158
+ if (hex.length === 4 && /^[0-9a-fA-F]{4}$/.test(hex)) { out += String.fromCharCode(parseInt(hex, 16)); i += 4; }
159
+ else break; // incomplete \uXXXX: wait for more
160
+ } else out += n; // \" \\ \/ etc.
161
+ } else out += c;
162
+ }
163
+ return out;
164
+ };
165
+ // tool_search_call carries arguments as a JSON object ({query, limit}); parse the model's arg string.
166
+ const parseArgsObj = (args: string): Record<string, unknown> => {
167
+ try { const o = JSON.parse(args); return o && typeof o === "object" ? o : {}; } catch { return {}; }
168
+ };
169
+ const encoder = new TextEncoder();
170
+ // Default-budget safety net: omission is SAFE (default turn limits), never
171
+ // unbounded. Production callers always pass one; an owned default is disposed
172
+ // at terminal/cancel below.
173
+ const ownsBudget = !options?.translatorBudget;
174
+ const budget = options?.translatorBudget ?? createTranslatorBudget();
175
+ // Idempotent: safe to call at every stream-death path; disposal must come
176
+ // AFTER the final charges (emitDone), never inside reportTerminal.
177
+ const disposeOwnedBudget = () => { if (ownsBudget) budget.dispose(); };
178
+ // A dropped stream (never read, never cancelled) reaches no terminal path,
179
+ // so the owned budget would sit in liveBudgets for the process lifetime.
180
+ // One unref'd watchdog per owned budget bounds that to a timeout and clears
181
+ // itself on any settle (the delay is test-overridable).
182
+ const ownedWatchdog = ownsBudget
183
+ ? setTimeout(() => disposeOwnedBudget(), ownedBudgetAbandonedMs)
184
+ : undefined;
185
+ ownedWatchdog?.unref?.();
186
+ const clearOwnedWatchdog = () => {
187
+ if (ownedWatchdog !== undefined) clearTimeout(ownedWatchdog);
188
+ };
189
+ const bytesOf = (value: string): number => Buffer.byteLength(value);
190
+ const appendString = (
191
+ previous: StringChunks,
192
+ fragment: string,
193
+ kind: TranslatorBufferKind,
194
+ callId?: string,
195
+ ): StringChunks => {
196
+ const fragmentBytes = bytesOf(fragment);
197
+ if (fragmentBytes === 0) return previous;
198
+ const nextBytes = previous.bytes + fragmentBytes;
199
+ const scope = { kind, ...(callId ? { callId } : {}) };
200
+ const reservation = budget.reserveTransient(nextBytes, scope);
201
+ try {
202
+ previous.chunks.push(fragment);
203
+ const result: StringChunks = { chunks: previous.chunks, bytes: nextBytes };
204
+ reservation.commitRetained();
205
+ budget.releaseRetained(previous.bytes, scope);
206
+ return result;
207
+ } catch (error) {
208
+ reservation.release();
209
+ throw error;
210
+ }
211
+ };
212
+ // Tool-call arguments deliberately use plain string concatenation because
213
+ // downstream parsers and intermediate inspectors perform incremental JSON reads mid-stream.
214
+ // Converting tool args to StringChunks would require frequent join operations.
215
+ const appendStringDirect = (
216
+ previous: string,
217
+ previousBytes: number,
218
+ fragment: string,
219
+ kind: TranslatorBufferKind,
220
+ callId?: string,
221
+ ): { value: string; bytes: number } => {
222
+ const fragmentBytes = bytesOf(fragment);
223
+ const nextBytes = previousBytes + fragmentBytes;
224
+ const scope = { kind, ...(callId ? { callId } : {}) };
225
+ const reservation = budget.reserveTransient(nextBytes, scope);
226
+ try {
227
+ const value = previous + fragment;
228
+ reservation.commitRetained();
229
+ budget.releaseRetained(previousBytes, scope);
230
+ return { value, bytes: nextBytes };
231
+ } catch (error) {
232
+ reservation.release();
233
+ throw error;
234
+ }
235
+ };
236
+ const replaceRetainedString = (previousBytes: number, next: string, kind: TranslatorBufferKind): number => {
237
+ const nextBytes = bytesOf(next);
238
+ if (!budget) return nextBytes;
239
+ const reservation = budget.reserveTransient(nextBytes, { kind });
240
+ reservation.commitRetained();
241
+ budget.releaseRetained(previousBytes, { kind });
242
+ return nextBytes;
243
+ };
244
+ const chargeValue = (value: unknown, kind: TranslatorBufferKind): number => {
245
+ const bytes = bytesOf(JSON.stringify(value));
246
+ budget?.chargeRetained(bytes, { kind });
247
+ return bytes;
248
+ };
249
+ const responseId = options?.responseId ?? `resp_${uuid()}`;
250
+ let seq = 0;
251
+ // Set once the client is gone (cancel) or an enqueue throws on a torn-down controller, so we
252
+ // never enqueue again and never throw a second time inside start() — the RC2 double-throw that
253
+ // otherwise surfaced as proxy-side stream noise on every client disconnect.
254
+ let closed = false;
255
+ let clientCancelled = false;
256
+ let terminalReported = false;
257
+ const reportTerminal = (status: ResponsesTerminalStatus) => {
258
+ if (terminalReported || clientCancelled || closed) return;
259
+ terminalReported = true;
260
+ try { options?.onTerminal?.(status); } catch { /* terminal metrics must not break the stream */ }
261
+ clearOwnedWatchdog();
262
+ };
263
+ // RC3 keep-alive: Codex's idle timer is timeout(idle_timeout, stream.next()) over an
264
+ // eventsource_stream, which parses at the EVENT level — a comment-only frame dispatches no
265
+ // event, so it does NOT re-arm the timer (110 RCA). The default keep-alive is therefore a
266
+ // typed `response.heartbeat` frame the codex-rs parser ignores via `_ => Ok(None)`. The
267
+ // grok surface (strict async-openai decoder that dies on unknown variants) opts into SSE
268
+ // comment lines instead via options.heartbeatStyle. Emit whenever the *wire* has been
269
+ // silent, even if invisible adapter heartbeats are still flowing (web-search buffering +
270
+ // raw-byte progress). Upstream activity only resets the stall watchdog.
271
+ let upstreamActivity = false;
272
+ let wireActivity = false;
273
+ let beat: unknown;
274
+ let controller: ReadableStreamDefaultController<Uint8Array>;
275
+ let emittedFrames = 0;
276
+ let gated = false;
277
+ let stepping = false;
278
+ let terminateForTranslatorOverflow: ((error: unknown) => void) | undefined;
279
+ const emit = (name: string, data: Record<string, unknown>) => {
280
+ if (closed) return;
281
+ wireActivity = true;
282
+ try {
283
+ const frameText = sseEvent(name, { type: name, sequence_number: seq++, ...data });
284
+ const frameBytes = bytesOf(frameText);
285
+ const reservation = budget?.reserveTransient(frameBytes, { kind: "live_transient" });
286
+ const frame = encoder.encode(frameText);
287
+ reservation?.commitRetained();
288
+ controller.enqueue(frame);
289
+ budget?.releaseRetained(frameBytes, { kind: "live_transient" });
290
+ emittedFrames++;
291
+ } catch (error) {
292
+ if (isTranslatorBudgetExceededError(error)) {
293
+ terminateForTranslatorOverflow?.(error);
294
+ return;
295
+ }
296
+ closed = true;
297
+ disposeOwnedBudget();
298
+ }
299
+ };
300
+ const emitDone = () => {
301
+ if (closed) return;
302
+ try {
303
+ const done = "data: [DONE]\n\n";
304
+ const doneBytes = bytesOf(done);
305
+ const reservation = budget?.reserveTransient(doneBytes, { kind: "live_transient" });
306
+ const frame = encoder.encode(done);
307
+ reservation?.commitRetained();
308
+ controller.enqueue(frame);
309
+ budget?.releaseRetained(doneBytes, { kind: "live_transient" });
310
+ emittedFrames++;
311
+ } catch (error) {
312
+ if (isTranslatorBudgetExceededError(error)) {
313
+ terminateForTranslatorOverflow?.(error);
314
+ return;
315
+ }
316
+ closed = true;
317
+ }
318
+ };
319
+
320
+ const createdAt = Math.floor(Date.now() / 1000);
321
+ let outputIndex = 0;
322
+ const finishedItems: OutputItem[] = [];
323
+ const retainFinishedItem = (item: OutputItem, replacedBytes = 0, kind: TranslatorBufferKind = "retained_collectors") => {
324
+ const itemBytes = bytesOf(JSON.stringify(item));
325
+ const reservation = budget?.reserveTransient(itemBytes, { kind });
326
+ finishedItems.push(item);
327
+ reservation?.commitRetained();
328
+ if (replacedBytes > 0) budget?.releaseRetained(replacedBytes, { kind });
329
+ };
330
+
331
+ const responseSnapshot = (status: string, output: OutputItem[], endTurn?: boolean) => ({
332
+ id: responseId, object: "response", created_at: createdAt,
333
+ status, model: modelId, output, usage: null,
334
+ ...(endTurn !== undefined ? { end_turn: endTurn } : {}),
335
+ });
336
+
337
+ const heartbeatFrame = options?.heartbeatStyle === "comment"
338
+ ? encoder.encode(': opencodex heartbeat\n\n')
339
+ : encoder.encode('event: response.heartbeat\ndata: {"type":"response.heartbeat"}\n\n');
340
+ let stallTicks = 0;
341
+ const stallSec = resolveStallTimeoutSec(options?.stallTimeoutSec);
342
+ const maxStallTicks = Math.ceil((stallSec * 1000) / heartbeatMs);
343
+
344
+ let currentMsg: {
345
+ itemId: string;
346
+ outputIndex: number;
347
+ text: StringChunks;
348
+ citationFilter: CitationMarkerFilter;
349
+ phase?: OcxMessagePhase;
350
+ } | null = null;
351
+ let currentReasoning: { itemId: string; outputIndex: number; text: StringChunks } | null = null;
352
+ let currentRawReasoning: { itemId: string; outputIndex: number; text: StringChunks } | null = null;
353
+ // Anthropic extended-thinking round-trip state: the signature signs the CURRENT thinking
354
+ // block; redacted blocks are opaque payloads replayed verbatim. Attached to the reasoning
355
+ // item as an ocxr1 encrypted_content envelope on close. hiddenThinkingText collects the
356
+ // suppressed text under hideThinkingSummary so the signed text still round-trips.
357
+ let pendingSignature: string | undefined;
358
+ let pendingSignatureBytes = 0;
359
+ let pendingRedacted: string[] = [];
360
+ let hiddenThinking = emptyChunks();
361
+ const takeReasoningEnvelope = (hiddenText?: string): string | undefined => {
362
+ if (!pendingSignature && pendingRedacted.length === 0) return undefined;
363
+ const envelope: ReasoningEnvelope = {};
364
+ if (pendingSignature) envelope.sig = pendingSignature;
365
+ if (pendingRedacted.length > 0) envelope.red = pendingRedacted;
366
+ if (hiddenText) envelope.txt = hiddenText;
367
+ const previousBytes = pendingSignatureBytes
368
+ + pendingRedacted.reduce((sum, value) => sum + bytesOf(value), 0)
369
+ + (hiddenText ? hiddenThinking.bytes : 0);
370
+ const encoded = encodeReasoningEnvelope(envelope, budget);
371
+ const reservation = budget?.reserveTransient(bytesOf(encoded), { kind: "reasoning" });
372
+ pendingSignature = undefined;
373
+ pendingSignatureBytes = 0;
374
+ pendingRedacted = [];
375
+ reservation?.commitRetained();
376
+ budget?.releaseRetained(previousBytes, { kind: "reasoning" });
377
+ return encoded;
378
+ };
379
+ // hideThinkingSummary path: no visible reasoning item exists, but a signed thinking block
380
+ // must still round-trip — emit an envelope-only reasoning item (empty summary, no text leak).
381
+ const flushHiddenReasoningEnvelope = () => {
382
+ const hiddenText = joinChunks(hiddenThinking);
383
+ const encrypted = takeReasoningEnvelope(hiddenText || undefined);
384
+ hiddenThinking = emptyChunks();
385
+ if (!encrypted) return;
386
+ const itemId = `rs_${uuid()}`;
387
+ const item = { type: "reasoning", id: itemId, summary: [] as never[], encrypted_content: encrypted };
388
+ emit("response.output_item.added", { output_index: outputIndex, item });
389
+ emit("response.output_item.done", { output_index: outputIndex, item });
390
+ retainFinishedItem(item as OutputItem, bytesOf(encrypted), "reasoning");
391
+ outputIndex++;
392
+ };
393
+ // hideThinkingSummary for RAW reasoning (openai-chat reasoning_content, kiro tags): no
394
+ // visible reasoning item is emitted — the app renders nothing, so tool cells keep grouping
395
+ // like native models — but the text still round-trips in a txt-only ocxr1 envelope so
396
+ // preserveReasoningContentModels replay (GLM interleaved thinking) keeps working. Direct
397
+ // encodeReasoningEnvelope: takeReasoningEnvelope's sig/red guard would drop txt-only.
398
+ let hiddenRawReasoning = emptyChunks();
399
+ // Raw reasoning text flushed most recently, waiting for the tool call it
400
+ // preceded. Recorded into the replay cache on tool_call_start so a later
401
+ // continuation can re-attach it when history lost the reasoning item
402
+ // (issue #950). Kept until new reasoning/text arrives: parallel tool
403
+ // calls share the same preceding reasoning block.
404
+ let rawReasoningForNextToolCall = "";
405
+ const flushHiddenRawReasoning = () => {
406
+ const hiddenRawText = joinChunks(hiddenRawReasoning);
407
+ if (!hiddenRawText) return;
408
+ rawReasoningForNextToolCall = hiddenRawText;
409
+ const previousBytes = hiddenRawReasoning.bytes;
410
+ const encrypted = encodeReasoningEnvelope({ txt: hiddenRawText }, budget);
411
+ const reservation = budget?.reserveTransient(bytesOf(encrypted), { kind: "reasoning" });
412
+ hiddenRawReasoning = emptyChunks();
413
+ reservation?.commitRetained();
414
+ budget?.releaseRetained(previousBytes, { kind: "reasoning" });
415
+ const itemId = `rs_${uuid()}`;
416
+ const item = { type: "reasoning", id: itemId, summary: [] as never[], encrypted_content: encrypted };
417
+ emit("response.output_item.added", { output_index: outputIndex, item });
418
+ emit("response.output_item.done", { output_index: outputIndex, item });
419
+ retainFinishedItem(item as OutputItem, bytesOf(encrypted), "reasoning");
420
+ outputIndex++;
421
+ };
422
+ // Kiro reasoning round-trip. Kiro sends its encrypted blob at the END of a turn, while the
423
+ // assistant message is still open, so this CANNOT emit on arrival: the open message still
424
+ // owns `outputIndex` (it only advances on close), and an item emitted here would both reuse
425
+ // that index and land BEFORE the message — where the parser's backwards pairing drops it as
426
+ // orphaned. Stash it and flush after `done` has closed every open item instead.
427
+ let pendingKiroRedacted: string | undefined;
428
+ let pendingKiroRedactedBytes = 0;
429
+ const flushKiroRedactedReasoning = () => {
430
+ if (!pendingKiroRedacted) return;
431
+ const previousBytes = pendingKiroRedactedBytes;
432
+ const encrypted = encodeReasoningEnvelope({ krc: pendingKiroRedacted }, budget);
433
+ const reservation = budget?.reserveTransient(bytesOf(encrypted), { kind: "reasoning" });
434
+ pendingKiroRedacted = undefined;
435
+ pendingKiroRedactedBytes = 0;
436
+ reservation?.commitRetained();
437
+ budget?.releaseRetained(previousBytes, { kind: "reasoning" });
438
+ const itemId = `rs_${uuid()}`;
439
+ const item = { type: "reasoning", id: itemId, summary: [] as never[], encrypted_content: encrypted };
440
+ emit("response.output_item.added", { output_index: outputIndex, item });
441
+ emit("response.output_item.done", { output_index: outputIndex, item });
442
+ retainFinishedItem(item as OutputItem, bytesOf(encrypted), "reasoning");
443
+ outputIndex++;
444
+ };
445
+ // Full assistant text of a compaction turn (across message boundaries) — becomes the
446
+ // synthetic compaction item's payload on done.
447
+ let compaction = emptyChunks();
448
+ let currentToolCall: { itemId: string; outputIndex: number; callId: string; name: string; args: string; argsBytes: number; namespace?: string; freeform?: boolean; toolSearch?: boolean; inputEmitted?: string; codeModeHelperName?: string; providerMetadata?: OcxProviderOpaqueToolCallMetadata } | null = null;
449
+ // Open native web-search cell (between begin and end). Holds the output index allocated on
450
+ // begin so the matching done reuses it; closed as `failed` if the stream terminates early.
451
+ let currentWebSearch: { itemId: string; eventId: string; outputIndex: number } | null = null;
452
+ // Sources from completed web searches, awaiting the next assistant message. Attached as
453
+ // url_citation annotations on that message (the desktop app's Sources chip), then cleared so
454
+ // they bind to exactly one message. Deduped by URL across multiple searches in the turn.
455
+ let pendingWebSources: { url: string; title?: string }[] = [];
456
+ let pendingWebSourceBytes = 0;
457
+ const releasePendingWebSources = () => {
458
+ if (pendingWebSources.length === 0) return;
459
+ pendingWebSources = [];
460
+ budget?.releaseRetained(pendingWebSourceBytes, { kind: "tool_search_sources" });
461
+ pendingWebSourceBytes = 0;
462
+ };
463
+ const takeWebAnnotations = (): { type: string; url: string; title?: string; start_index: number; end_index: number }[] => {
464
+ if (pendingWebSources.length === 0) return [];
465
+ const anns = pendingWebSources.map(s => ({
466
+ type: "url_citation", url: s.url, ...(s.title ? { title: s.title } : {}), start_index: 0, end_index: 0,
467
+ }));
468
+ const annotationBytes = bytesOf(JSON.stringify(anns));
469
+ const reservation = budget?.reserveTransient(annotationBytes, { kind: "retained_collectors" });
470
+ reservation?.commitRetained();
471
+ releasePendingWebSources();
472
+ return anns;
473
+ };
474
+
475
+ const closeCurrentMessage = (inferredPhase?: OcxMessagePhase) => {
476
+ if (!currentMsg) return;
477
+ // Release anything the citation filter was holding for this message, then strip the
478
+ // accumulated text: closeCurrentMessage re-sends it in output_text.done and
479
+ // output_item.done, so filtering only the deltas would leave the markers in both.
480
+ const trailing = currentMsg.citationFilter.flush();
481
+ if (trailing) {
482
+ emit("response.output_text.delta", {
483
+ item_id: currentMsg.itemId, output_index: currentMsg.outputIndex,
484
+ content_index: 0, delta: trailing,
485
+ });
486
+ }
487
+ const messageText = stripCitationMarkers(joinChunks(currentMsg.text));
488
+ // Chat Completions has no message-phase field. Keep its live item provisional, then
489
+ // classify it only when the next adapter event proves whether this text led into more
490
+ // work or completed the turn. Explicit adapter phases always outrank this inference.
491
+ const phase = currentMsg.phase ?? inferredPhase;
492
+ // Bind any pending web-search citations to this assistant message (then they clear).
493
+ const annotations = takeWebAnnotations();
494
+ // Finalize the text part (Responses protocol). Without these .done events Codex never
495
+ // commits the content part and renders the message as truncated / cut off.
496
+ emit("response.output_text.done", {
497
+ item_id: currentMsg.itemId, output_index: currentMsg.outputIndex, content_index: 0, text: messageText,
498
+ });
499
+ emit("response.content_part.done", {
500
+ item_id: currentMsg.itemId, output_index: currentMsg.outputIndex, content_index: 0,
501
+ part: { type: "output_text", text: messageText, annotations },
502
+ });
503
+ const item = {
504
+ type: "message", id: currentMsg.itemId, status: "completed", role: "assistant",
505
+ content: [{ type: "output_text", text: messageText, annotations }],
506
+ ...(phase ? { phase } : {}),
507
+ };
508
+ emit("response.output_item.done", { output_index: currentMsg.outputIndex, item });
509
+ retainFinishedItem(item as OutputItem, currentMsg.text.bytes + bytesOf(JSON.stringify(annotations)));
510
+ outputIndex++;
511
+ currentMsg = null;
512
+ };
513
+
514
+ const closeCurrentReasoning = () => {
515
+ if (!currentReasoning) return;
516
+ const reasoningText = joinChunks(currentReasoning.text);
517
+ emit("response.reasoning_summary_text.done", {
518
+ item_id: currentReasoning.itemId, output_index: currentReasoning.outputIndex, summary_index: 0, text: reasoningText,
519
+ });
520
+ emit("response.reasoning_summary_part.done", {
521
+ item_id: currentReasoning.itemId, output_index: currentReasoning.outputIndex, summary_index: 0,
522
+ part: { type: "summary_text", text: reasoningText },
523
+ });
524
+ const encrypted = takeReasoningEnvelope();
525
+ const item = {
526
+ type: "reasoning", id: currentReasoning.itemId,
527
+ summary: [{ type: "summary_text", text: reasoningText }],
528
+ ...(encrypted ? { encrypted_content: encrypted } : {}),
529
+ };
530
+ emit("response.output_item.done", { output_index: currentReasoning.outputIndex, item });
531
+ retainFinishedItem(item as OutputItem, currentReasoning.text.bytes + bytesOf(encrypted ?? ""), "reasoning");
532
+ outputIndex++;
533
+ currentReasoning = null;
534
+ };
535
+
536
+ const closeCurrentRawReasoning = () => {
537
+ if (!currentRawReasoning) return;
538
+ const rawText = joinChunks(currentRawReasoning.text);
539
+ rawReasoningForNextToolCall = rawText;
540
+ emit("response.reasoning_text.done", {
541
+ item_id: currentRawReasoning.itemId, output_index: currentRawReasoning.outputIndex, content_index: 0, text: rawText,
542
+ });
543
+ const item = {
544
+ type: "reasoning", id: currentRawReasoning.itemId,
545
+ summary: [] as never[],
546
+ content: [{ type: "reasoning_text", text: rawText }],
547
+ };
548
+ emit("response.output_item.done", { output_index: currentRawReasoning.outputIndex, item });
549
+ retainFinishedItem(item as OutputItem, currentRawReasoning.text.bytes, "reasoning");
550
+ outputIndex++;
551
+ currentRawReasoning = null;
552
+ };
553
+
554
+ const closeCurrentToolCall = () => {
555
+ if (!currentToolCall) return;
556
+ // Empty input (no-arg tools like computer_use get_app_state / list_apps) must serialize as
557
+ // "{}", never "" — Codex echoes the call back as a function_call next turn, and JSON.parse("")
558
+ // would 400 the whole session ("invalid JSON arguments"), poisoning all later turns.
559
+ // #1611: Grok serializes integer arguments through a float, so `120000.0`
560
+ // reaches Codex and is REJECTED before the tool runs. Repair integral floats
561
+ // against the declared schema; a non-integral value stays an error.
562
+ const argsStr = coerceIntegerToolArguments(
563
+ currentToolCall.args || "{}",
564
+ options?.toolParameterSchemas?.get(currentToolCall.name),
565
+ currentToolCall.namespace === undefined ? currentToolCall.name : undefined,
566
+ );
567
+ // Finalize streamed function-call arguments so Codex commits the call (incl. MCP / computer_use).
568
+ if (!currentToolCall.freeform && !currentToolCall.toolSearch) {
569
+ emit("response.function_call_arguments.done", {
570
+ item_id: currentToolCall.itemId, output_index: currentToolCall.outputIndex, arguments: argsStr,
571
+ });
572
+ }
573
+ if (currentToolCall.freeform) {
574
+ emit("response.custom_tool_call_input.done", {
575
+ item_id: currentToolCall.itemId, output_index: currentToolCall.outputIndex,
576
+ ...(currentToolCall.namespace ? { namespace: currentToolCall.namespace } : {}),
577
+ input: freeformInput(currentToolCall.args, currentToolCall.name, currentToolCall.namespace, currentToolCall.codeModeHelperName),
578
+ });
579
+ }
580
+ // Freeform tools serialize as custom_tool_call without extra_content; remember the
581
+ // signature server-side regardless so the replayed call can be re-signed (#1735).
582
+ void rememberExtraContentForReplay(currentToolCall.callId, currentToolCall.providerMetadata, replayCacheScope);
583
+ const item = currentToolCall.toolSearch
584
+ ? {
585
+ type: "tool_search_call", id: currentToolCall.itemId,
586
+ call_id: currentToolCall.callId, execution: "client",
587
+ arguments: parseArgsObj(currentToolCall.args), status: "completed",
588
+ }
589
+ : currentToolCall.freeform
590
+ ? {
591
+ type: "custom_tool_call", id: currentToolCall.itemId,
592
+ call_id: currentToolCall.callId, name: currentToolCall.name,
593
+ ...(currentToolCall.namespace ? { namespace: currentToolCall.namespace } : {}),
594
+ input: freeformInput(currentToolCall.args, currentToolCall.name, currentToolCall.namespace, currentToolCall.codeModeHelperName), status: "completed",
595
+ }
596
+ : {
597
+ type: "function_call", id: currentToolCall.itemId,
598
+ call_id: currentToolCall.callId, name: currentToolCall.name,
599
+ arguments: argsStr, status: "completed",
600
+ ...(currentToolCall.namespace ? { namespace: currentToolCall.namespace } : {}),
601
+ // Provider-opaque metadata (issue #1735) rides the item so a client that replays
602
+ // this history can hand the signature back on the part it belongs to. The proxy
603
+ // also remembers it server-side for clients that never echo extra_content.
604
+ ...(rememberAndSerializeExtraContent(currentToolCall.callId, currentToolCall.providerMetadata, replayCacheScope).extra ?? {}),
605
+ };
606
+ emit("response.output_item.done", { output_index: currentToolCall.outputIndex, item });
607
+ retainFinishedItem(item as OutputItem);
608
+ budget?.closeCall(currentToolCall.callId);
609
+ outputIndex++;
610
+ currentToolCall = null;
611
+ };
612
+
613
+ // Terminal-error / incomplete path for an open tool call (#765 remainder).
614
+ // Closing via closeCurrentToolCall() would emit function_call_arguments.done and
615
+ // status:"completed" BEFORE response.failed — the client still sees an issued call.
616
+ // Cancel instead: no *.done argument frames, status:"incomplete" (same pattern as an
617
+ // in-flight web_search_call closing as "failed"). Args still serialize as "{}" when
618
+ // empty so echoed items cannot poison the next turn with JSON.parse("").
619
+ const failCurrentToolCall = () => {
620
+ if (!currentToolCall) return;
621
+ const argsStr = currentToolCall.args || "{}";
622
+ void rememberExtraContentForReplay(currentToolCall.callId, currentToolCall.providerMetadata, replayCacheScope);
623
+ const item = currentToolCall.toolSearch
624
+ ? {
625
+ type: "tool_search_call", id: currentToolCall.itemId,
626
+ call_id: currentToolCall.callId, execution: "client",
627
+ arguments: parseArgsObj(currentToolCall.args), status: "incomplete",
628
+ }
629
+ : currentToolCall.freeform
630
+ ? {
631
+ type: "custom_tool_call", id: currentToolCall.itemId,
632
+ call_id: currentToolCall.callId, name: currentToolCall.name,
633
+ ...(currentToolCall.namespace ? { namespace: currentToolCall.namespace } : {}),
634
+ input: freeformInput(currentToolCall.args, currentToolCall.name, currentToolCall.namespace, currentToolCall.codeModeHelperName), status: "incomplete",
635
+ }
636
+ : {
637
+ type: "function_call", id: currentToolCall.itemId,
638
+ call_id: currentToolCall.callId, name: currentToolCall.name,
639
+ arguments: argsStr, status: "incomplete",
640
+ ...(currentToolCall.namespace ? { namespace: currentToolCall.namespace } : {}),
641
+ // An incomplete call can still be persisted and replayed (max_output_tokens), so it
642
+ // carries the same metadata as the completed item — otherwise SSE and buffered JSON
643
+ // would disagree about whether the signature survives.
644
+ ...(rememberAndSerializeExtraContent(currentToolCall.callId, currentToolCall.providerMetadata, replayCacheScope).extra ?? {}),
645
+ };
646
+ emit("response.output_item.done", { output_index: currentToolCall.outputIndex, item });
647
+ retainFinishedItem(item as OutputItem);
648
+ budget?.closeCall(currentToolCall.callId);
649
+ outputIndex++;
650
+ currentToolCall = null;
651
+ };
652
+
653
+ const abortCurrentToolCallForTranslatorOverflow = () => {
654
+ if (!currentToolCall) return;
655
+ budget?.closeCall(currentToolCall.callId);
656
+ currentToolCall = null;
657
+ };
658
+
659
+ // Finalize an open web-search cell. `status` is "completed" on a normal end, or "failed" when
660
+ // the stream terminates (error/incomplete) while a search was still in flight, so Codex never
661
+ // leaves a "Searching the web" spinner spinning forever.
662
+ // `sources` rides on the done item (additive field; codex-rs serde ignores unknown fields) so
663
+ // downstream translators (claude outbound) can fill web_search_tool_result content.
664
+ const closeCurrentWebSearch = (status: "completed" | "failed", queries: string[], sources?: { url: string; title?: string }[]) => {
665
+ if (!currentWebSearch) return;
666
+ const item = {
667
+ type: "web_search_call", id: currentWebSearch.itemId, status,
668
+ action: webSearchAction(queries),
669
+ ...(sources && sources.length > 0 ? { sources } : {}),
670
+ };
671
+ emit("response.output_item.done", { output_index: currentWebSearch.outputIndex, item });
672
+ retainFinishedItem(item as OutputItem);
673
+ outputIndex++;
674
+ currentWebSearch = null;
675
+ };
676
+
677
+ // RC1: guarantee the Responses stream always ends with exactly one terminal event. Set true
678
+ // when a done/error/catch terminal is emitted; if the adapter generator returns without one
679
+ // we synthesize a terminal below, so Codex never hits the parser's
680
+ // "stream closed before response.completed" (responses.rs) -> ApiError::Stream.
681
+ // That synthesized terminal is response.incomplete with reason "adapter_eof", NOT
682
+ // response.completed: a generator that returns without a terminal event is a truncated
683
+ // stream, and reporting it as a clean finish is the failure mode this whole path exists
684
+ // to avoid. The comment said "completed" long after the code stopped doing that.
685
+ let terminated = false;
686
+ let firstOutputReported = false;
687
+ const reportFirstOutput = (event: AdapterEvent): void => {
688
+ if (firstOutputReported) return;
689
+ const nonEmpty = event.type === "text_delta"
690
+ ? event.text.length > 0
691
+ : event.type === "thinking_delta"
692
+ ? event.thinking.length > 0
693
+ : event.type === "reasoning_raw_delta"
694
+ ? event.text.length > 0
695
+ : false;
696
+ if (!nonEmpty) return;
697
+ firstOutputReported = true;
698
+ try { options?.onFirstOutput?.(); } catch { /* metrics must not break the stream */ }
699
+ };
700
+ const it = events[Symbol.asyncIterator]();
701
+ let iteratorStarted = false;
702
+ let iteratorReturned = false;
703
+ let upstreamDone = false;
704
+ const returnIterator = () => {
705
+ if (iteratorReturned) return;
706
+ iteratorReturned = true;
707
+ const finishReturn = () => {
708
+ try {
709
+ void it.return?.()?.catch(() => {});
710
+ } catch {
711
+ /* synchronous iterator cleanup failure is also best-effort */
712
+ }
713
+ };
714
+ // Async-generator return() before the first next() does not enter the generator, so its
715
+ // finally blocks cannot cancel prepared upstream bodies. The cancel hook has already
716
+ // aborted the turn; bootstrap one cleanup step, then close the iterator without awaiting it.
717
+ if (!iteratorStarted) {
718
+ iteratorStarted = true;
719
+ try {
720
+ void it.next().then(finishReturn, () => {}).catch(() => {});
721
+ } catch {
722
+ /* synchronous iterator start failure is also best-effort */
723
+ }
724
+ return;
725
+ }
726
+ finishReturn();
727
+ };
728
+ let upstreamCancelled = false;
729
+ const cancelUpstreamOnce = () => {
730
+ if (upstreamCancelled) return;
731
+ upstreamCancelled = true;
732
+ try { onCancel?.(); } catch { /* cancellation must not strand the client stream */ }
733
+ returnIterator();
734
+ };
735
+ let handlingTranslatorOverflow = false;
736
+ terminateForTranslatorOverflow = _error => {
737
+ if (handlingTranslatorOverflow || terminated || clientCancelled || closed) return;
738
+ handlingTranslatorOverflow = true;
739
+ abortCurrentToolCallForTranslatorOverflow();
740
+ currentWebSearch = null;
741
+ releasePendingWebSources();
742
+ const failure = adapterFailureFromEvent({
743
+ type: "error",
744
+ status: 502,
745
+ errorType: "upstream_error",
746
+ code: "translation_buffer_limit",
747
+ message: "upstream translation buffer exceeded the safe limit",
748
+ }).error;
749
+ const failedFrame = sseEvent("response.failed", {
750
+ type: "response.failed",
751
+ sequence_number: seq++,
752
+ response: {
753
+ ...responseSnapshot("failed", finishedItems),
754
+ error: failure,
755
+ last_error: failure,
756
+ },
757
+ });
758
+ try {
759
+ controller.enqueue(encoder.encode(failedFrame));
760
+ controller.enqueue(encoder.encode("data: [DONE]\n\n"));
761
+ emittedFrames += 2;
762
+ } catch {
763
+ /* client already tore down the stream */
764
+ }
765
+ reportTerminal("failed");
766
+ terminated = true;
767
+ cancelUpstreamOnce();
768
+ if (beat !== undefined) clearBeatInterval(beat);
769
+ beat = undefined;
770
+ try { controller.close(); } catch { /* already closed */ }
771
+ closed = true;
772
+ disposeOwnedBudget();
773
+ gated = true;
774
+ stepping = false;
775
+ };
776
+ const attemptTerminationCleanup = (action: () => void): boolean => {
777
+ try {
778
+ action();
779
+ return !terminated && !closed;
780
+ } catch (error) {
781
+ if (!isTranslatorBudgetExceededError(error)) throw error;
782
+ terminateForTranslatorOverflow(error);
783
+ return false;
784
+ }
785
+ };
786
+ const step = async () => {
787
+ if (stepping || closed) return;
788
+ stepping = true;
789
+ gated = false;
790
+ const emittedAtStart = emittedFrames;
791
+ try {
792
+ while (!terminated && !closed && emittedFrames === emittedAtStart) {
793
+ iteratorStarted = true;
794
+ const next = await it.next();
795
+ // A cancel during this await disposes the owned budget; a late event
796
+ // must never be processed or charged against it. Exit step() outright:
797
+ // falling into EOF synthesis would let closeCurrentMessage() charge
798
+ // finished-item retention against the disposed budget.
799
+ if (closed || clientCancelled) {
800
+ gated = true;
801
+ stepping = false;
802
+ return;
803
+ }
804
+ if (next.done) { upstreamDone = true; break; }
805
+ const event = next.value;
806
+ let terminalEvent = false;
807
+ // Invisible adapter heartbeats (and buffered web-search progress) count as upstream
808
+ // liveness only — they must not suppress wire keepalives that re-arm Codex idle timers.
809
+ upstreamActivity = true;
810
+ stallTicks = 0;
811
+ reportFirstOutput(event);
812
+ // Compaction turns emit ONLY the synthetic compaction item + response.completed. The
813
+ // summary text is accumulated silently: emitting it as a normal assistant message would
814
+ // duplicate the summary if this response is ever replayed via previous_response_id
815
+ // expansion (rememberResponseState stores input + output). Codex ignores extra items but
816
+ // its compaction UI renders nothing mid-turn, so nothing is lost visually.
817
+ if (options?.compaction) {
818
+ if (event.type === "text_delta") {
819
+ compaction = appendString(
820
+ compaction,
821
+ event.text,
822
+ "retained_collectors",
823
+ );
824
+ continue;
825
+ }
826
+ if (event.type !== "done" && event.type !== "incomplete" && event.type !== "error") continue;
827
+ }
828
+ // Anthropic signature_delta supplies the latest signature, not an append-only
829
+ // fragment (anthropic-sdk-typescript MessageStream). Keep consecutive updates
830
+ // together; the next semantic event belongs to the following block.
831
+ if (pendingSignature !== undefined && event.type !== "thinking_signature" && event.type !== "heartbeat") {
832
+ if (currentReasoning) closeCurrentReasoning();
833
+ else flushHiddenReasoningEnvelope();
834
+ }
835
+ switch (event.type) {
836
+ case "assistant_boundary": {
837
+ // A guarded continuation starts a fresh assistant output item while keeping the
838
+ // intermediate, suspicious text in the same Responses turn.
839
+ if (currentMsg) closeCurrentMessage("commentary");
840
+ if (currentReasoning) closeCurrentReasoning();
841
+ if (currentRawReasoning) closeCurrentRawReasoning();
842
+ flushHiddenRawReasoning();
843
+ rawReasoningForNextToolCall = "";
844
+ if (currentToolCall) closeCurrentToolCall();
845
+ flushHiddenReasoningEnvelope();
846
+ break;
847
+ }
848
+ case "text_delta": {
849
+ if (currentReasoning) closeCurrentReasoning();
850
+ if (currentRawReasoning) closeCurrentRawReasoning();
851
+ flushHiddenRawReasoning();
852
+ // Reasoning consumed by a REAL text turn, not a tool call: no cache target.
853
+ // Empty text deltas must not wipe reasoning that precedes a tool call
854
+ // (chat-completions providers emit empty content deltas mid-tool-turn).
855
+ if (event.text.length > 0) rawReasoningForNextToolCall = "";
856
+ if (currentToolCall) closeCurrentToolCall();
857
+ // Only flush on an explicit phase change. A later delta that omits `phase` must
858
+ // keep appending to the current message rather than wiping the earlier phase.
859
+ if (currentMsg && event.phase !== undefined && currentMsg.phase !== event.phase) {
860
+ closeCurrentMessage("commentary");
861
+ }
862
+ if (!currentMsg) {
863
+ const itemId = `msg_${uuid()}`;
864
+ const item = {
865
+ type: "message", id: itemId, status: "in_progress", role: "assistant",
866
+ content: [] as { type: string; text: string; annotations: never[] }[],
867
+ ...(event.phase ? { phase: event.phase } : {}),
868
+ };
869
+ emit("response.output_item.added", { output_index: outputIndex, item });
870
+ emit("response.content_part.added", {
871
+ item_id: itemId, output_index: outputIndex, content_index: 0,
872
+ part: { type: "output_text", text: "", annotations: [] },
873
+ });
874
+ currentMsg = {
875
+ itemId, outputIndex, text: emptyChunks(),
876
+ citationFilter: createCitationMarkerFilter(),
877
+ ...(event.phase ? { phase: event.phase } : {}),
878
+ };
879
+ }
880
+ currentMsg.text = appendString(
881
+ currentMsg.text,
882
+ event.text,
883
+ "retained_collectors",
884
+ );
885
+ // A citation span can straddle a delta boundary, so the filter withholds an
886
+ // unterminated tail and releases it at close (#3150). The accumulator above
887
+ // keeps the raw text; it is stripped once in closeCurrentMessage.
888
+ const visible = currentMsg.citationFilter.push(event.text);
889
+ if (visible) {
890
+ emit("response.output_text.delta", {
891
+ item_id: currentMsg.itemId, output_index: currentMsg.outputIndex,
892
+ content_index: 0, delta: visible,
893
+ });
894
+ }
895
+ break;
896
+ }
897
+ case "thinking_delta": {
898
+ if (options?.hideThinkingSummary) {
899
+ // The hidden branch returns early, so flush any raw reasoning
900
+ // that preceded the thinking block and clear the replay-cache
901
+ // candidate — otherwise a stale reasoning_raw_delta would be
902
+ // recorded for a LATER tool call (CodeRabbit on #971).
903
+ flushHiddenRawReasoning();
904
+ rawReasoningForNextToolCall = "";
905
+ hiddenThinking = appendString(
906
+ hiddenThinking,
907
+ event.thinking,
908
+ "reasoning",
909
+ );
910
+ break;
911
+ }
912
+ if (currentMsg) closeCurrentMessage("commentary");
913
+ if (currentRawReasoning) closeCurrentRawReasoning();
914
+ flushHiddenRawReasoning();
915
+ if (event.thinking.length > 0) rawReasoningForNextToolCall = "";
916
+ if (currentToolCall) closeCurrentToolCall();
917
+ if (!currentReasoning) {
918
+ const itemId = `rs_${uuid()}`;
919
+ const item = { type: "reasoning", id: itemId, summary: [] as { type: string; text: string }[] };
920
+ emit("response.output_item.added", { output_index: outputIndex, item });
921
+ emit("response.reasoning_summary_part.added", {
922
+ item_id: itemId, output_index: outputIndex, summary_index: 0,
923
+ part: { type: "summary_text", text: "" },
924
+ });
925
+ currentReasoning = { itemId, outputIndex, text: emptyChunks() };
926
+ }
927
+ currentReasoning.text = appendString(
928
+ currentReasoning.text,
929
+ event.thinking,
930
+ "reasoning",
931
+ );
932
+ emit("response.reasoning_summary_text.delta", {
933
+ item_id: currentReasoning.itemId, output_index: currentReasoning.outputIndex,
934
+ summary_index: 0, delta: event.thinking,
935
+ });
936
+ break;
937
+ }
938
+ case "thinking_signature": {
939
+ pendingSignatureBytes = replaceRetainedString(pendingSignatureBytes, event.signature, "reasoning");
940
+ pendingSignature = event.signature;
941
+ // Delay closing until the next semantic event so a signature update cannot
942
+ // create another block or become attached to the following thinking text.
943
+ break;
944
+ }
945
+ case "redacted_thinking": {
946
+ if (currentMsg) closeCurrentMessage("commentary");
947
+ if (currentReasoning) closeCurrentReasoning();
948
+ if (currentRawReasoning) closeCurrentRawReasoning();
949
+ flushHiddenRawReasoning();
950
+ if (currentToolCall) closeCurrentToolCall();
951
+ budget?.chargeRetained(bytesOf(event.data), { kind: "reasoning" });
952
+ pendingRedacted.push(event.data);
953
+ // A redacted block is complete at content_block_start. Emit it here,
954
+ // not with a later thinking block or after a tool call at turn end.
955
+ flushHiddenReasoningEnvelope();
956
+ break;
957
+ }
958
+ case "kiro_redacted_reasoning": {
959
+ // Stash only — see flushKiroRedactedReasoning. One blob per turn, so last wins.
960
+ pendingKiroRedactedBytes = replaceRetainedString(pendingKiroRedactedBytes, event.data, "reasoning");
961
+ pendingKiroRedacted = event.data;
962
+ break;
963
+ }
964
+ case "reasoning_raw_delta": {
965
+ if (options?.hideThinkingSummary) {
966
+ hiddenRawReasoning = appendString(
967
+ hiddenRawReasoning,
968
+ event.text,
969
+ "reasoning",
970
+ );
971
+ break;
972
+ }
973
+ if (currentMsg) closeCurrentMessage("commentary");
974
+ if (currentReasoning) closeCurrentReasoning();
975
+ if (currentToolCall) closeCurrentToolCall();
976
+ if (!currentRawReasoning) {
977
+ const itemId = `rs_${uuid()}`;
978
+ const item = { type: "reasoning", id: itemId, summary: [] as { type: string; text: string }[] };
979
+ emit("response.output_item.added", { output_index: outputIndex, item });
980
+ currentRawReasoning = { itemId, outputIndex, text: emptyChunks() };
981
+ }
982
+ currentRawReasoning.text = appendString(
983
+ currentRawReasoning.text,
984
+ event.text,
985
+ "reasoning",
986
+ );
987
+ // Raw reasoning (openai-chat reasoning_content, kiro tags) rides the CONTENT
988
+ // channel. Clients control raw-reasoning display; this text is not a
989
+ // provider-authored summary.
990
+ emit("response.reasoning_text.delta", {
991
+ item_id: currentRawReasoning.itemId, output_index: currentRawReasoning.outputIndex,
992
+ content_index: 0, delta: event.text,
993
+ });
994
+ break;
995
+ }
996
+ case "tool_call_start": {
997
+ if (currentMsg) closeCurrentMessage("commentary");
998
+ if (currentReasoning) closeCurrentReasoning();
999
+ if (currentRawReasoning) closeCurrentRawReasoning();
1000
+ flushHiddenRawReasoning();
1001
+ if (rawReasoningForNextToolCall) {
1002
+ rememberReasoningForCall(event.id, rawReasoningForNextToolCall, replayCacheScope);
1003
+ }
1004
+ if (currentToolCall) closeCurrentToolCall();
1005
+ const effectiveName = normalizeDeclaredToolName(event.name, options?.declaredToolNames);
1006
+ const codeModeHelperName = effectiveName === "exec" && event.name !== effectiveName
1007
+ ? event.name
1008
+ : undefined;
1009
+ const mapped = toolNsMap?.get(effectiveName);
1010
+ const realName = mapped?.name ?? effectiveName;
1011
+ if (options?.declaredToolNames && !options.declaredToolNames.has(effectiveName)) {
1012
+ const failure = responseError(
1013
+ 502,
1014
+ "upstream_error",
1015
+ `routed provider emitted undeclared client tool "${effectiveName}"; only request-declared tools may be called`,
1016
+ );
1017
+ emit("response.failed", {
1018
+ response: {
1019
+ ...responseSnapshot("failed", finishedItems),
1020
+ error: failure,
1021
+ last_error: failure,
1022
+ },
1023
+ });
1024
+ reportTerminal("failed");
1025
+ terminalEvent = true;
1026
+ break;
1027
+ }
1028
+ const ns = mapped?.namespace;
1029
+ const toolSearch = toolSearchToolNames?.has(realName) ?? false;
1030
+ const freeform = !toolSearch && (mapped
1031
+ ? mapped.freeform === true
1032
+ : (freeformToolNames?.has(realName) ?? false));
1033
+ const itemId = `${toolSearch ? "tsc" : freeform ? "ctc" : "fc"}_${uuid()}`;
1034
+ const item = toolSearch
1035
+ ? { type: "tool_search_call", id: itemId, call_id: event.id, execution: "client", arguments: {}, status: "in_progress" }
1036
+ : freeform
1037
+ ? { type: "custom_tool_call", id: itemId, call_id: event.id, name: realName, ...(ns ? { namespace: ns } : {}), input: "", status: "in_progress" }
1038
+ : { type: "function_call", id: itemId, call_id: event.id, name: realName, arguments: "", status: "in_progress", ...(ns ? { namespace: ns } : {}) };
1039
+ emit("response.output_item.added", { output_index: outputIndex, item });
1040
+ currentToolCall = { itemId, outputIndex, callId: event.id, name: realName, args: "", argsBytes: 0, namespace: ns, freeform, toolSearch, codeModeHelperName, providerMetadata: event.providerMetadata };
1041
+ budget?.openCall(event.id);
1042
+ break;
1043
+ }
1044
+ case "tool_call_delta": {
1045
+ if (currentToolCall) {
1046
+ ({ value: currentToolCall.args, bytes: currentToolCall.argsBytes } = appendStringDirect(
1047
+ currentToolCall.args,
1048
+ currentToolCall.argsBytes,
1049
+ event.arguments,
1050
+ "tool_args",
1051
+ currentToolCall.callId,
1052
+ ));
1053
+ if (!currentToolCall.freeform && !currentToolCall.toolSearch) {
1054
+ emit("response.function_call_arguments.delta", {
1055
+ item_id: currentToolCall.itemId, output_index: currentToolCall.outputIndex,
1056
+ delta: event.arguments,
1057
+ });
1058
+ }
1059
+ if (currentToolCall.freeform && !currentToolCall.codeModeHelperName) {
1060
+ // Hold while the buffer is still an ambiguous prefix of the JSON wrapper,
1061
+ // then stream only the unwrapped input suffix (never rewind on mode flips).
1062
+ if (!FREEFORM_WRAP_PREFIX.startsWith(currentToolCall.args)) {
1063
+ const full = freeformPartialInput(currentToolCall.args);
1064
+ const emitted = currentToolCall.inputEmitted ?? "";
1065
+ // Also hold a buffer that could still become a complete patch envelope:
1066
+ // at completion such a body is recompiled into an apply_patch helper call,
1067
+ // and streaming the envelope bytes first would be that same rewind.
1068
+ const mayCompile = declaresCodeModeExec(options?.declaredToolNames)
1069
+ && !currentToolCall.namespace
1070
+ && currentToolCall.name === "exec";
1071
+ if (!(mayCompile && mayBecomePatchEnvelope(full)) && full.startsWith(emitted) && full.length > emitted.length) {
1072
+ emit("response.custom_tool_call_input.delta", {
1073
+ item_id: currentToolCall.itemId, output_index: currentToolCall.outputIndex,
1074
+ delta: full.slice(emitted.length),
1075
+ });
1076
+ currentToolCall.inputEmitted = full;
1077
+ }
1078
+ }
1079
+ }
1080
+ }
1081
+ break;
1082
+ }
1083
+ case "tool_call_end": {
1084
+ // Fragments already streamed cannot be repaired. Refuse to complete a function call
1085
+ // whose assembled arguments do not parse — cancel the item and fail the turn so the
1086
+ // client never sees status:"completed" for unusable args (#765 stream remainder).
1087
+ if (
1088
+ currentToolCall
1089
+ && !currentToolCall.freeform
1090
+ && !currentToolCall.toolSearch
1091
+ && !toolCallArgumentsUsable(currentToolCall.args)
1092
+ ) {
1093
+ failCurrentToolCall();
1094
+ const failure = responseError(
1095
+ 502,
1096
+ "upstream_error",
1097
+ "upstream stream produced malformed tool call arguments",
1098
+ );
1099
+ emit("response.failed", {
1100
+ response: {
1101
+ ...responseSnapshot("failed", finishedItems),
1102
+ error: failure,
1103
+ last_error: failure,
1104
+ },
1105
+ });
1106
+ reportTerminal("failed");
1107
+ terminalEvent = true;
1108
+ break;
1109
+ }
1110
+ closeCurrentToolCall();
1111
+ break;
1112
+ }
1113
+ case "web_search_call_begin": {
1114
+ // Open the native search cell so Codex shows the "Searching the web" spinner WHILE the
1115
+ // sidecar runs. Close any other open item first, allocate this item's output index, and
1116
+ // hold it open until the matching `web_search_call_end` (or a terminal close).
1117
+ if (currentMsg) closeCurrentMessage("commentary");
1118
+ if (currentReasoning) closeCurrentReasoning();
1119
+ if (currentRawReasoning) closeCurrentRawReasoning();
1120
+ flushHiddenRawReasoning();
1121
+ if (currentToolCall) closeCurrentToolCall();
1122
+ if (currentWebSearch) closeCurrentWebSearch("completed", []);
1123
+ const wsItemId = `ws_${uuid()}`;
1124
+ emit("response.output_item.added", {
1125
+ output_index: outputIndex,
1126
+ item: { type: "web_search_call", id: wsItemId, status: "in_progress" },
1127
+ });
1128
+ currentWebSearch = { itemId: wsItemId, eventId: event.id, outputIndex };
1129
+ break;
1130
+ }
1131
+ case "web_search_call_end": {
1132
+ // The sidecar resolved — finalize the cell as "Searched <query>". If no begin opened
1133
+ // (defensive), synthesize the added frame first so the done has a matching item.
1134
+ if (!currentWebSearch || currentWebSearch.eventId !== event.id) {
1135
+ if (currentWebSearch) closeCurrentWebSearch("completed", []);
1136
+ const wsItemId2 = `ws_${uuid()}`;
1137
+ emit("response.output_item.added", {
1138
+ output_index: outputIndex,
1139
+ item: { type: "web_search_call", id: wsItemId2, status: "in_progress" },
1140
+ });
1141
+ currentWebSearch = { itemId: wsItemId2, eventId: event.id, outputIndex };
1142
+ }
1143
+ const safeSources = safeWebSearchSources(event.sources);
1144
+ closeCurrentWebSearch(event.status ?? "completed", event.queries, safeSources);
1145
+ // Queue this search's sources for the next assistant message (dedup by URL).
1146
+ if (safeSources.length > 0) {
1147
+ for (const source of safeSources) {
1148
+ if (appendSafeWebSearchSource(pendingWebSources, source)) {
1149
+ pendingWebSourceBytes += chargeValue(source, "tool_search_sources");
1150
+ }
1151
+ }
1152
+ }
1153
+ break;
1154
+ }
1155
+ case "done": {
1156
+ if (currentMsg) closeCurrentMessage(event.stopReason ? undefined : "final_answer");
1157
+ if (currentReasoning) closeCurrentReasoning();
1158
+ if (currentRawReasoning) closeCurrentRawReasoning();
1159
+ flushHiddenRawReasoning();
1160
+ if (currentToolCall) {
1161
+ if (isTruncatedStopReason(event.stopReason)) failCurrentToolCall();
1162
+ else closeCurrentToolCall();
1163
+ }
1164
+ // A search still in flight when upstream truncates never returned results, so it
1165
+ // takes the same "failed" status as the error/incomplete terminals below.
1166
+ if (currentWebSearch) closeCurrentWebSearch(isTruncatedStopReason(event.stopReason) ? "failed" : "completed", []);
1167
+ releasePendingWebSources();
1168
+ // Redacted-only turns (or hidden thinking without a trailing signature event) still
1169
+ // need their envelope-only reasoning item so the blocks replay next turn.
1170
+ flushHiddenReasoningEnvelope();
1171
+ // After every close above, so the blob lands AFTER the assistant message it belongs
1172
+ // to and the parser's backwards pairing finds it.
1173
+ flushKiroRedactedReasoning();
1174
+ // Truncated turns must never install replacement history (#422). The buffered path
1175
+ // has always checked this; streaming emitted the item BEFORE reading stopReason, so
1176
+ // a max_tokens/content_filter turn shipped a half-written summary and then declared
1177
+ // itself incomplete — the same hazard, one branch over.
1178
+ if (options?.compaction && !isTruncatedStopReason(event.stopReason)) {
1179
+ // Exactly one compaction item per turn; codex-rs takes the first and fatals on 0.
1180
+ const item = {
1181
+ type: "compaction", id: `cmp_${uuid()}`,
1182
+ encrypted_content: event.compactionEncryptedContent ?? encodeCompactionSummary(joinChunks(compaction)),
1183
+ };
1184
+ emit("response.output_item.done", { output_index: outputIndex, item });
1185
+ retainFinishedItem(item as OutputItem, event.compactionEncryptedContent
1186
+ ? bytesOf(event.compactionEncryptedContent)
1187
+ : compaction.bytes);
1188
+ outputIndex++;
1189
+ }
1190
+ // Recognize every adapter's truncation vocabulary, not just the canonical pair.
1191
+ // Suppression and terminal status must agree: withholding the compaction item while
1192
+ // still reporting success hands codex-rs a completed response with zero compaction
1193
+ // items, which it treats as fatal.
1194
+ if (truncationReasonFor(event.stopReason)) {
1195
+ // Upstream stopped before a normal completion. Surface as incomplete so the
1196
+ // client can distinguish a truncated/filtered turn from a finished one.
1197
+ // #1926 gap 2: bound the window in which a handed-out thought signature is
1198
+ // not yet durable before the turn becomes externally terminal.
1199
+ await awaitThoughtSignatureDurability();
1200
+ const response = {
1201
+ ...responseSnapshot("incomplete", finishedItems, event.endTurn),
1202
+ usage: responsesUsage(event.usage),
1203
+ incomplete_details: {
1204
+ reason: truncationReasonFor(event.stopReason) ?? "content_filter",
1205
+ },
1206
+ };
1207
+ // Cache max-output partials so previous_response_id replay can continue them;
1208
+ // rememberResponseState rejects content-filtered incomplete responses.
1209
+ options?.onCompletedResponse?.(response, event.providerState);
1210
+ options?.onUsage?.(event.usage);
1211
+ emit("response.incomplete", { response });
1212
+ reportTerminal("incomplete");
1213
+ } else {
1214
+ await awaitThoughtSignatureDurability();
1215
+ const response = { ...responseSnapshot("completed", finishedItems, event.endTurn), usage: responsesUsage(event.usage) };
1216
+ options?.onCompletedResponse?.(response, event.providerState);
1217
+ options?.onUsage?.(event.usage);
1218
+ emit("response.completed", {
1219
+ response,
1220
+ });
1221
+ reportTerminal("completed");
1222
+ }
1223
+ terminalEvent = true;
1224
+ break;
1225
+ }
1226
+ case "incomplete": {
1227
+ if (currentMsg) closeCurrentMessage();
1228
+ if (currentReasoning) closeCurrentReasoning();
1229
+ if (currentRawReasoning) closeCurrentRawReasoning();
1230
+ flushHiddenRawReasoning();
1231
+ if (currentToolCall) failCurrentToolCall();
1232
+ if (currentWebSearch) closeCurrentWebSearch("failed", []);
1233
+ releasePendingWebSources();
1234
+ flushHiddenReasoningEnvelope();
1235
+ options?.onUsage?.(event.usage);
1236
+ await awaitThoughtSignatureDurability();
1237
+ emit("response.incomplete", {
1238
+ response: {
1239
+ ...responseSnapshot("incomplete", finishedItems, event.endTurn),
1240
+ usage: responsesUsage(event.usage),
1241
+ incomplete_details: {
1242
+ reason: event.reason,
1243
+ ...(event.message ? { message: event.message } : {}),
1244
+ ...(event.retryable !== undefined ? { retryable: event.retryable } : {}),
1245
+ },
1246
+ },
1247
+ });
1248
+ reportTerminal("incomplete");
1249
+ terminalEvent = true;
1250
+ break;
1251
+ }
1252
+ case "error": {
1253
+ if (event.code === "translation_buffer_limit") {
1254
+ terminateForTranslatorOverflow(event);
1255
+ return;
1256
+ }
1257
+ if (currentMsg) closeCurrentMessage();
1258
+ if (currentReasoning) closeCurrentReasoning();
1259
+ if (currentRawReasoning) closeCurrentRawReasoning();
1260
+ flushHiddenRawReasoning();
1261
+ if (currentToolCall) failCurrentToolCall();
1262
+ if (currentWebSearch) closeCurrentWebSearch("failed", []);
1263
+ releasePendingWebSources();
1264
+ const failure = adapterFailureFromEvent(event);
1265
+ if (event.usage) options?.onUsage?.(event.usage);
1266
+ await awaitThoughtSignatureDurability();
1267
+ emit("response.failed", {
1268
+ response: {
1269
+ ...responseSnapshot("failed", finishedItems),
1270
+ // Partial consumption from a mid-stream upstream failure: surfaced so the request
1271
+ // log can record real tokens instead of usageStatus "unreported" with 0.
1272
+ ...(event.usage ? { usage: responsesUsage(event.usage) } : {}),
1273
+ error: failure.error,
1274
+ last_error: failure.error,
1275
+ ...(isCyberPolicyCode(failure.error.code)
1276
+ ? { retryable: false }
1277
+ : event.retryable !== undefined ? { retryable: event.retryable } : {}),
1278
+ },
1279
+ });
1280
+ reportTerminal("failed");
1281
+ terminalEvent = true;
1282
+ break;
1283
+ }
1284
+ }
1285
+ if (terminalEvent) {
1286
+ cancelUpstreamOnce();
1287
+ terminated = true;
1288
+ break;
1289
+ }
1290
+ }
1291
+ } catch (err) {
1292
+ if (isTranslatorBudgetExceededError(err)) {
1293
+ terminateForTranslatorOverflow(err);
1294
+ return;
1295
+ }
1296
+ if (!terminated) {
1297
+ if (!attemptTerminationCleanup(() => {
1298
+ flushHiddenRawReasoning();
1299
+ if (currentToolCall) failCurrentToolCall();
1300
+ if (currentWebSearch) closeCurrentWebSearch("failed", []);
1301
+ releasePendingWebSources();
1302
+ })) return;
1303
+ const failure = responseError(
1304
+ 500,
1305
+ "proxy_error",
1306
+ redactSecretString(err instanceof Error ? err.message : String(err)),
1307
+ );
1308
+ emit("response.failed", {
1309
+ response: {
1310
+ ...responseSnapshot("failed", finishedItems),
1311
+ error: failure,
1312
+ last_error: failure,
1313
+ ...(isCyberPolicyCode(failure.code) ? { retryable: false } : {}),
1314
+ },
1315
+ });
1316
+ reportTerminal("failed");
1317
+ cancelUpstreamOnce();
1318
+ terminated = true;
1319
+ }
1320
+ }
1321
+
1322
+ if (!terminated && !upstreamDone) {
1323
+ gated = true;
1324
+ stepping = false;
1325
+ return;
1326
+ }
1327
+ if (beat !== undefined) { clearBeatInterval(beat); beat = undefined; }
1328
+
1329
+ if (!terminated) {
1330
+ // The adapter generator ended without an explicit done/error event. Mark as incomplete
1331
+ // rather than completed so Codex can distinguish a clean finish from a truncated stream.
1332
+ if (!attemptTerminationCleanup(() => {
1333
+ if (currentMsg) closeCurrentMessage();
1334
+ if (currentReasoning) closeCurrentReasoning();
1335
+ if (currentRawReasoning) closeCurrentRawReasoning();
1336
+ flushHiddenRawReasoning();
1337
+ if (currentToolCall) failCurrentToolCall();
1338
+ if (currentWebSearch) closeCurrentWebSearch("failed", []);
1339
+ releasePendingWebSources();
1340
+ })) return;
1341
+ options?.onUsage?.(undefined);
1342
+ await awaitThoughtSignatureDurability();
1343
+ emit("response.incomplete", {
1344
+ response: {
1345
+ ...responseSnapshot("incomplete", finishedItems),
1346
+ usage: responsesUsage(undefined),
1347
+ incomplete_details: { reason: "adapter_eof" },
1348
+ },
1349
+ });
1350
+ reportTerminal("incomplete");
1351
+ terminated = true;
1352
+ }
1353
+
1354
+ emitDone();
1355
+ try {
1356
+ controller.close();
1357
+ } catch {
1358
+ /* already closed (e.g. client cancelled) */
1359
+ }
1360
+ closed = true;
1361
+ disposeOwnedBudget();
1362
+ gated = true;
1363
+ stepping = false;
1364
+ };
1365
+
1366
+ const startStream = () => {
1367
+ emit("response.created", { response: responseSnapshot("in_progress", []) });
1368
+ // Responses spec parity: clients expect an explicit in_progress frame after created.
1369
+ emit("response.in_progress", { response: responseSnapshot("in_progress", []) });
1370
+ // The default ReadableStream strategy has HWM=1. Once one event's frames fill that
1371
+ // queue, pull stepping pauses; no custom FIFO or queuing strategy is layered on top.
1372
+ gated = true;
1373
+ beat = setBeatInterval(() => {
1374
+ if (closed || gated) return;
1375
+ if (upstreamActivity) {
1376
+ upstreamActivity = false;
1377
+ stallTicks = 0;
1378
+ } else if (++stallTicks >= maxStallTicks) {
1379
+ if (!attemptTerminationCleanup(() => {
1380
+ if (currentMsg) closeCurrentMessage();
1381
+ if (currentReasoning) closeCurrentReasoning();
1382
+ if (currentRawReasoning) closeCurrentRawReasoning();
1383
+ flushHiddenRawReasoning();
1384
+ if (currentToolCall) failCurrentToolCall();
1385
+ if (currentWebSearch) closeCurrentWebSearch("failed", []);
1386
+ releasePendingWebSources();
1387
+ })) return;
1388
+ // #1926 gap 2 residual: this beat callback is synchronous, so the durability
1389
+ // barrier is not awaited on the stall-timeout kill path. The in-memory store is
1390
+ // already updated; only a crash between here and the queued write loses it,
1391
+ // which is the pre-#1926 status quo for an already-abnormal termination.
1392
+ emit("response.incomplete", {
1393
+ response: {
1394
+ ...responseSnapshot("incomplete", finishedItems),
1395
+ incomplete_details: { reason: "upstream_stall_timeout" },
1396
+ },
1397
+ });
1398
+ reportTerminal("incomplete");
1399
+ cancelUpstreamOnce();
1400
+ terminated = true;
1401
+ emitDone();
1402
+ if (beat !== undefined) clearBeatInterval(beat);
1403
+ beat = undefined;
1404
+ try { controller.close(); } catch { /* already closed */ }
1405
+ closed = true;
1406
+ disposeOwnedBudget();
1407
+ return;
1408
+ }
1409
+ // Wire silence is independent of upstream adapter heartbeats.
1410
+ if (wireActivity) {
1411
+ wireActivity = false;
1412
+ return;
1413
+ }
1414
+ try {
1415
+ controller.enqueue(heartbeatFrame);
1416
+ emittedFrames++;
1417
+ } catch {
1418
+ closed = true;
1419
+ disposeOwnedBudget();
1420
+ }
1421
+ }, heartbeatMs);
1422
+ };
1423
+
1424
+ return new ReadableStream<Uint8Array>({
1425
+ start(streamController) {
1426
+ controller = streamController;
1427
+ startStream();
1428
+ },
1429
+ pull() {
1430
+ return step();
1431
+ },
1432
+ cancel() {
1433
+ // Client (Codex) disconnected. Stop emitting and let the caller abort the upstream fetch so a
1434
+ // cancelled turn does not leak the upstream stream or keep draining tokens (RC2).
1435
+ clientCancelled = true;
1436
+ closed = true;
1437
+ clearOwnedWatchdog();
1438
+ if (beat !== undefined) clearBeatInterval(beat);
1439
+ cancelUpstreamOnce();
1440
+ releasePendingWebSources();
1441
+ disposeOwnedBudget();
1442
+ },
1443
+ });
1444
+ }