@bitkyc08/opencodex 2.100.3 → 2.100.4
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.
- package/AGENTS_INSTALL.md +23 -1
- package/bin/ocx.mjs +8 -1
- package/gui/dist/assets/{App-Dnb6VMsP.js → App-DcpyI78f.js} +20 -19
- package/gui/dist/assets/{Tray-BV4ms9U9.js → Tray-CzjPJprT.js} +1 -1
- package/gui/dist/assets/brand-mark-BTIi7e69.js +1 -0
- package/gui/dist/assets/index-DJxM-imn.css +2 -0
- package/gui/dist/assets/index-DVgU6S3y.js +138 -0
- package/gui/dist/assets/{segment-thumb-features-DXSidY6D.js → segment-thumb-features-Bn6Y-g3R.js} +1 -1
- package/gui/dist/classic/assets/{App-DA-scMPq.js → App-eYJAyNHM.js} +19 -18
- package/gui/dist/classic/assets/{Tray-BrHaVRAq.js → Tray-xHMYxLkV.js} +1 -1
- package/gui/dist/classic/assets/index-DSaVRW4F.js +138 -0
- package/gui/dist/classic/assets/quota-summary-DyzHlkFl.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/native/ocx-child/src/cli.rs +5 -3
- package/native/ocx-child/src/codex_home.rs +1 -0
- package/package.json +2 -2
- package/src/adapters/anthropic/per-message-effort.ts +296 -0
- package/src/adapters/anthropic.ts +3 -0
- package/src/adapters/claude-cli/adapter.ts +1 -0
- package/src/adapters/devin/cloud-direct/chat.ts +71 -14
- package/src/adapters/google-errors.ts +14 -2
- package/src/adapters/qoder/adapter.ts +9 -1
- package/src/adapters/zed.ts +4 -2
- package/src/claude/desktop-remote-store.ts +60 -10
- package/src/cli/account-api.ts +4 -0
- package/src/cli/account-auth.ts +30 -2
- package/src/cli/account-extended.ts +33 -2
- package/src/cli/capabilities-access-remote.ts +3 -3
- package/src/cli/capabilities-accounts.ts +3 -3
- package/src/cli/capabilities-base.ts +11 -6
- package/src/cli/capabilities-observe-system.ts +2 -2
- package/src/cli/claude.ts +3 -3
- package/src/cli/disconnect-runtime.ts +16 -6
- package/src/cli/dispatch.ts +7 -1
- package/src/cli/index.ts +2 -2
- package/src/cli/link-apply-restart.ts +4 -2
- package/src/cli/link-routes.ts +4 -1
- package/src/cli/link.ts +5 -4
- package/src/cli/preflight-exemptions.ts +3 -0
- package/src/cli/registry.ts +5 -2
- package/src/cli/resolve.ts +23 -8
- package/src/cli/restart-failure.ts +11 -1
- package/src/cli/runtime-api.ts +4 -0
- package/src/cli/status.ts +4 -1
- package/src/cli/system-command.ts +1 -1
- package/src/cli/system-restart-client.ts +9 -3
- package/src/cli/system-settings-parity.ts +2 -1
- package/src/client/connect.ts +63 -9
- package/src/client/desktop-refusal.ts +38 -0
- package/src/client/lifecycle-lock.ts +29 -4
- package/src/client/runtime-restart.ts +27 -15
- package/src/client/runtime.ts +4 -2
- package/src/client/state.ts +14 -0
- package/src/codex/account-pause.ts +4 -0
- package/src/codex/account-runtime-state.ts +4 -0
- package/src/codex/auth-api/main-account-probe.ts +3 -1
- package/src/codex/auth-api/main-hard-lock-refusal-read.ts +172 -0
- package/src/codex/auth-context.ts +40 -2
- package/src/codex/catalog/build-entries.ts +10 -0
- package/src/codex/catalog/derive-entry.ts +7 -0
- package/src/codex/catalog/reserve-warn.ts +7 -5
- package/src/codex/catalog/reserve.ts +4 -3
- package/src/codex/catalog/retained-sync.ts +2 -0
- package/src/codex/catalog/routed-defaults.ts +35 -0
- package/src/codex/convergence.ts +2 -0
- package/src/codex/loopback-target.ts +22 -8
- package/src/codex/main-account.ts +88 -29
- package/src/codex/native-main-auth-file.ts +14 -0
- package/src/codex/native-main-credentials.ts +270 -0
- package/src/codex/native-main-owner.ts +3 -0
- package/src/codex/native-main-refresh-mode.ts +14 -1
- package/src/codex/quota.ts +63 -14
- package/src/combos/failover.ts +12 -1
- package/src/combos/index.ts +5 -0
- package/src/combos/resolve.ts +115 -19
- package/src/config/diagnostics.ts +3 -0
- package/src/config/live-reconcile.ts +6 -3
- package/src/config/load-degrade.ts +2 -0
- package/src/config/persisted-mutation.ts +2 -2
- package/src/config/persisted-projections.ts +18 -0
- package/src/config/process-state.ts +4 -3
- package/src/config/reserve-capability.ts +96 -0
- package/src/config/schema/config-schema.ts +6 -0
- package/src/config/schema/leaf-validators.ts +2 -0
- package/src/config/schema/routed-catalog-defaults.ts +49 -0
- package/src/config.ts +7 -7
- package/src/desktop-update/acquire.ts +202 -0
- package/src/desktop-update/cli.ts +168 -0
- package/src/desktop-update/drain.ts +40 -0
- package/src/desktop-update/headless.ts +285 -0
- package/src/desktop-update/image.ts +85 -0
- package/src/desktop-update/install.ts +53 -0
- package/src/desktop-update/msi.ts +72 -0
- package/src/desktop-update/release.ts +128 -0
- package/src/desktop-update/runtime.ts +155 -0
- package/src/desktop-update/staging.ts +118 -0
- package/src/desktop-update/trust.ts +45 -0
- package/src/generated/compatibility-version.json +246 -130
- package/src/images/fulfill-video.ts +3 -3
- package/src/images/loop.ts +6 -2
- package/src/images/plan.ts +147 -11
- package/src/images/types.ts +13 -4
- package/src/images/xai-video-client.ts +72 -10
- package/src/lib/gcp-adc.ts +48 -4
- package/src/lib/offline-resend.ts +150 -0
- package/src/lib/paced-stream.ts +3 -3
- package/src/lib/provider-outbound.ts +17 -6
- package/src/lib/request-failure-model.ts +2 -0
- package/src/lib/response-body-inactivity.ts +23 -1
- package/src/lib/upstream-retry.ts +38 -5
- package/src/link/explicit-route.ts +4 -4
- package/src/link/remote-windows.ts +79 -31
- package/src/link/residual-recovery.ts +20 -8
- package/src/messaging/budget.ts +23 -5
- package/src/messaging/rpc.ts +4 -4
- package/src/oauth/antigravity-risk.ts +126 -0
- package/src/oauth/generic-account-failover.ts +89 -18
- package/src/oauth/generic-session-affinity.ts +10 -3
- package/src/oauth/login-cli.ts +35 -1
- package/src/oauth/pool-kernel.ts +3 -3
- package/src/providers/anthropic-billing-refusal.ts +27 -3
- package/src/providers/google-vertex-model-discovery.ts +6 -1
- package/src/providers/model-rename-fields.ts +2 -0
- package/src/providers/quota/workbuddy.ts +6 -2
- package/src/providers/workbuddy-credit-balance.ts +125 -16
- package/src/responses/effort-history.ts +732 -0
- package/src/routing/history/integrity.ts +41 -4
- package/src/server/auth-cors.ts +13 -3
- package/src/server/codex-model-list.ts +2 -1
- package/src/server/direct-local-http.ts +26 -3
- package/src/server/index/link-listener.ts +20 -14
- package/src/server/index/serve-options.ts +1 -0
- package/src/server/index.ts +3 -2
- package/src/server/link-turn.ts +17 -5
- package/src/server/management/config-routes.ts +39 -3
- package/src/server/management/link-child-preflight.ts +1 -1
- package/src/server/management/link-route-edit.ts +20 -1
- package/src/server/management/oauth-account-routes.ts +22 -5
- package/src/server/management/provider-overwrite-carry.ts +3 -0
- package/src/server/management/provider-routes.ts +4 -4
- package/src/server/management/static-catalog-probe.ts +33 -0
- package/src/server/proxy-liveness.ts +54 -9
- package/src/server/relay.ts +6 -1
- package/src/server/request-log-conversation.ts +2 -2
- package/src/server/request-log.ts +5 -1
- package/src/server/reserve-capability-startup.ts +32 -0
- package/src/server/responses/adapter-delivery.ts +2 -0
- package/src/server/responses/adapter-dispatch.ts +31 -10
- package/src/server/responses/antigravity-fault-rotation.ts +24 -0
- package/src/server/responses/antigravity-validation-refusal.ts +43 -6
- package/src/server/responses/combo-selection-keepalive.ts +254 -0
- package/src/server/responses/core-combo.ts +24 -9
- package/src/server/responses/core-errors.ts +9 -6
- package/src/server/responses/core-options.ts +8 -0
- package/src/server/responses/core.ts +21 -0
- package/src/server/responses/estimated-usage.ts +115 -0
- package/src/server/responses/jev-model-invoke.ts +7 -2
- package/src/server/responses/passthrough-completion.ts +6 -3
- package/src/server/responses/passthrough-delivery.ts +47 -8
- package/src/server/responses/passthrough-dispatch.ts +3 -0
- package/src/server/responses/request-prepare.ts +9 -5
- package/src/server/responses/sidecar-execution.ts +5 -0
- package/src/service/desktop-supervision.d.mts +2 -0
- package/src/service/desktop-supervision.mjs +5 -0
- package/src/stall-timeout.ts +11 -0
- package/src/types/config.ts +15 -92
- package/src/types/provider.ts +13 -0
- package/src/types/request.ts +19 -0
- package/src/types/routed-catalog-defaults.ts +17 -0
- package/src/types/routing-profile.ts +90 -0
- package/src/types.ts +1 -0
- package/src/update/index.ts +1 -1
- package/src/usage/log.ts +14 -4
- package/src/usage/telemetry-contract.ts +1 -0
- package/src/web-search/passthrough-bridge.ts +7 -0
- package/gui/dist/assets/brand-mark-XOLJbomA.js +0 -1
- package/gui/dist/assets/index-DCdXsMyZ.js +0 -105
- package/gui/dist/assets/index-DQtCRNAw.css +0 -2
- package/gui/dist/classic/assets/index-BqOaGLf5.js +0 -105
- package/gui/dist/classic/assets/quota-summary-Dy5_wfTT.js +0 -1
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
import type { RequestLogContext } from "../request-log";
|
|
2
|
+
import type { HandleResponsesOptions } from "./core-options";
|
|
3
|
+
import { hasResponseMediaType } from "../../lib/http-response-semantics";
|
|
4
|
+
import { readDisplaySafeErrorText } from "./core-errors";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* How long a streaming combo may spend choosing a child before the client gets a response head
|
|
8
|
+
* (#8326). Below it nothing changes: the selected child's own head is returned, zero-output
|
|
9
|
+
* failover stays invisible and an all-targets failure keeps its real HTTP status. Once it passes,
|
|
10
|
+
* a 200 SSE head is committed so an idle-limited intermediary sees bytes, and the eventual
|
|
11
|
+
* outcome is delivered inside that stream. It matches the native reset-selection heartbeat.
|
|
12
|
+
*/
|
|
13
|
+
export const COMBO_SELECTION_KEEPALIVE_THRESHOLD_MS = 15_000;
|
|
14
|
+
/** Gap between keepalive comments while selection continues behind a committed head. */
|
|
15
|
+
export const COMBO_SELECTION_KEEPALIVE_INTERVAL_MS = 15_000;
|
|
16
|
+
|
|
17
|
+
const KEEPALIVE = new TextEncoder().encode(": keepalive\n\n");
|
|
18
|
+
/** Proxy-owned copy for a ladder that threw after the head was committed; never the error text. */
|
|
19
|
+
const THROWN_SELECTION_MESSAGE = "opencodex could not complete this combo request";
|
|
20
|
+
|
|
21
|
+
/** Timer seam: production uses the global timers, tests advance a manual clock. */
|
|
22
|
+
export type ComboSelectionKeepaliveClock = NonNullable<NonNullable<HandleResponsesOptions["comboSelectionKeepalive"]>["clock"]>;
|
|
23
|
+
|
|
24
|
+
export interface ComboSelectionKeepaliveHooks {
|
|
25
|
+
/** The request's abort signal, also aborted when the client cancels the committed stream. */
|
|
26
|
+
signal: AbortSignal;
|
|
27
|
+
/** True once the keepalive head has been handed to the client. */
|
|
28
|
+
headCommitted(): boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const systemClock: ComboSelectionKeepaliveClock = {
|
|
32
|
+
setTimeout: (callback, ms) => setTimeout(callback, ms),
|
|
33
|
+
clearTimeout: handle => clearTimeout(handle as ReturnType<typeof setTimeout>),
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Only a streaming Responses turn from the HTTP ingress opts in. WebSocket frames carry their own
|
|
38
|
+
* status, Chat and Messages ingresses consume a different failure vocabulary, and internal
|
|
39
|
+
* decision calls read the HTTP status themselves.
|
|
40
|
+
*/
|
|
41
|
+
export function comboSelectionKeepaliveApplies(
|
|
42
|
+
rawBody: unknown,
|
|
43
|
+
options: Pick<HandleResponsesOptions, "comboSelectionKeepalive" | "inboundTransport" | "inboundWire" | "internalDecisionCall">,
|
|
44
|
+
): boolean {
|
|
45
|
+
return options.comboSelectionKeepalive !== undefined
|
|
46
|
+
&& (rawBody as { stream?: unknown } | null)?.stream === true
|
|
47
|
+
&& options.inboundTransport === undefined
|
|
48
|
+
&& (options.inboundWire === undefined || options.inboundWire === "responses")
|
|
49
|
+
&& options.internalDecisionCall !== true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The ladder's options behind a possible keepalive head. A committed head is an unmarked SSE
|
|
54
|
+
* response, so the ingress request-log tap records its terminal; the native passthrough log
|
|
55
|
+
* callbacks are dropped from then on so one request is never logged twice.
|
|
56
|
+
*/
|
|
57
|
+
export function comboSelectionKeepaliveLadderOptions<T extends HandleResponsesOptions>(
|
|
58
|
+
options: T,
|
|
59
|
+
hooks: ComboSelectionKeepaliveHooks,
|
|
60
|
+
): T {
|
|
61
|
+
const { comboSelectionKeepalive: _optIn, ...rest } = options;
|
|
62
|
+
const onTerminal = options.onNativePassthroughTerminal;
|
|
63
|
+
const onCancel = options.onNativePassthroughCancel;
|
|
64
|
+
return {
|
|
65
|
+
...rest,
|
|
66
|
+
abortSignal: hooks.signal,
|
|
67
|
+
...(onTerminal ? { onNativePassthroughTerminal: status => { if (!hooks.headCommitted()) onTerminal(status); } } : {}),
|
|
68
|
+
...(onCancel ? { onNativePassthroughCancel: () => { if (!hooks.headCommitted()) onCancel(); } } : {}),
|
|
69
|
+
} as T;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
73
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function failedFrame(model: string, error: { type?: string; code: string; message: string }): Uint8Array {
|
|
77
|
+
const failure = { ...(error.type ? { type: error.type } : {}), code: error.code, message: error.message };
|
|
78
|
+
const payload = {
|
|
79
|
+
type: "response.failed",
|
|
80
|
+
sequence_number: 0,
|
|
81
|
+
response: {
|
|
82
|
+
id: `resp_${crypto.randomUUID().replaceAll("-", "")}`,
|
|
83
|
+
object: "response",
|
|
84
|
+
created_at: Math.floor(Date.now() / 1000),
|
|
85
|
+
status: "failed",
|
|
86
|
+
model,
|
|
87
|
+
output: [],
|
|
88
|
+
error: failure,
|
|
89
|
+
last_error: failure,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
return new TextEncoder().encode(`event: response.failed\ndata: ${JSON.stringify(payload)}\n\n`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Restate a non-streamable selection (an error envelope) as the in-stream failure terminal. */
|
|
96
|
+
async function failureFromResponse(response: Response, signal: AbortSignal): Promise<{ type?: string; code: string; message: string }> {
|
|
97
|
+
const fallback = `opencodex combo request failed with HTTP ${response.status}`;
|
|
98
|
+
const text = await readDisplaySafeErrorText(response, signal, "");
|
|
99
|
+
let record: Record<string, unknown> | undefined;
|
|
100
|
+
try {
|
|
101
|
+
const parsed: unknown = JSON.parse(text);
|
|
102
|
+
if (isRecord(parsed)) record = isRecord(parsed.error) ? parsed.error : parsed;
|
|
103
|
+
} catch { /* not JSON: the status-only fallback below applies */ }
|
|
104
|
+
const type = typeof record?.type === "string" && record.type ? record.type : undefined;
|
|
105
|
+
const code = typeof record?.code === "string" && record.code ? record.code
|
|
106
|
+
: typeof record?.code === "number" && Number.isFinite(record.code) ? String(record.code)
|
|
107
|
+
: type ?? (response.ok ? "invalid_response" : "upstream_error");
|
|
108
|
+
const message = typeof record?.message === "string" && record.message.trim() ? record.message : fallback;
|
|
109
|
+
return { ...(type ? { type } : {}), code, message };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Run combo selection and return its response, or a keepalive SSE response once selection has
|
|
114
|
+
* waited past the threshold. Behind the keepalive head, an accepted SSE child is streamed as is and
|
|
115
|
+
* any other outcome becomes one `response.failed` with the same error code.
|
|
116
|
+
*/
|
|
117
|
+
export function selectComboWithKeepalive(
|
|
118
|
+
select: (hooks: ComboSelectionKeepaliveHooks) => Promise<Response>,
|
|
119
|
+
context: { model: string; logCtx: RequestLogContext; signal?: AbortSignal; clock?: ComboSelectionKeepaliveClock },
|
|
120
|
+
): Promise<Response> {
|
|
121
|
+
const clock = context.clock ?? systemClock;
|
|
122
|
+
const cancel = new AbortController();
|
|
123
|
+
const signal = context.signal ? AbortSignal.any([context.signal, cancel.signal]) : cancel.signal;
|
|
124
|
+
let committed = false;
|
|
125
|
+
let settled = false;
|
|
126
|
+
let closed = false;
|
|
127
|
+
let keepaliveOwed = true;
|
|
128
|
+
let source: ReadableStreamDefaultReader<Uint8Array> | undefined;
|
|
129
|
+
let wake: (() => void) | undefined;
|
|
130
|
+
let timer: unknown;
|
|
131
|
+
const stopTimer = (): void => {
|
|
132
|
+
if (timer !== undefined) clock.clearTimeout(timer);
|
|
133
|
+
timer = undefined;
|
|
134
|
+
};
|
|
135
|
+
const notify = (): void => { const next = wake; wake = undefined; next?.(); };
|
|
136
|
+
const setSource = (stream: ReadableStream<Uint8Array> | null): void => {
|
|
137
|
+
if (closed) {
|
|
138
|
+
void stream?.cancel("client cancelled the committed combo stream").catch(() => {});
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
source = (stream ?? new ReadableStream<Uint8Array>({ start: controller => controller.close() })).getReader();
|
|
142
|
+
notify();
|
|
143
|
+
};
|
|
144
|
+
const deliverFailure = (status: number, failure: { type?: string; code: string; message: string }): void => {
|
|
145
|
+
context.logCtx.terminalHttpStatus = status;
|
|
146
|
+
const frame = failedFrame(context.model, failure);
|
|
147
|
+
setSource(new ReadableStream<Uint8Array>({ start(controller) { controller.enqueue(frame); controller.close(); } }));
|
|
148
|
+
};
|
|
149
|
+
const deliver = async (response: Response): Promise<void> => {
|
|
150
|
+
if (closed) {
|
|
151
|
+
void response.body?.cancel("client cancelled the committed combo stream").catch(() => {});
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (response.ok && hasResponseMediaType(response.headers, "text/event-stream")) {
|
|
155
|
+
setSource(response.body);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (response.status === 499) {
|
|
159
|
+
void response.body?.cancel().catch(() => {});
|
|
160
|
+
setSource(null);
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const failure = await failureFromResponse(response, signal);
|
|
164
|
+
deliverFailure(response.ok ? 502 : response.status, failure);
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const body = new ReadableStream<Uint8Array>({
|
|
168
|
+
async pull(controller) {
|
|
169
|
+
try {
|
|
170
|
+
for (;;) {
|
|
171
|
+
if (closed) return;
|
|
172
|
+
if (source) {
|
|
173
|
+
const next = await source.read();
|
|
174
|
+
if (closed) return;
|
|
175
|
+
if (next.done) {
|
|
176
|
+
closed = true;
|
|
177
|
+
controller.close();
|
|
178
|
+
} else {
|
|
179
|
+
controller.enqueue(next.value);
|
|
180
|
+
}
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (keepaliveOwed) {
|
|
184
|
+
keepaliveOwed = false;
|
|
185
|
+
controller.enqueue(KEEPALIVE);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
await new Promise<void>(resolve => { wake = resolve; });
|
|
189
|
+
}
|
|
190
|
+
} catch (error) {
|
|
191
|
+
if (closed) return;
|
|
192
|
+
closed = true;
|
|
193
|
+
controller.error(error);
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
cancel(reason) {
|
|
197
|
+
closed = true;
|
|
198
|
+
stopTimer();
|
|
199
|
+
cancel.abort(reason);
|
|
200
|
+
notify();
|
|
201
|
+
if (source) void source.cancel(reason).catch(() => {});
|
|
202
|
+
},
|
|
203
|
+
}, { highWaterMark: 0 });
|
|
204
|
+
|
|
205
|
+
const scheduleKeepalive = (): void => {
|
|
206
|
+
timer = clock.setTimeout(() => {
|
|
207
|
+
timer = undefined;
|
|
208
|
+
if (closed || settled) return;
|
|
209
|
+
keepaliveOwed = true;
|
|
210
|
+
notify();
|
|
211
|
+
scheduleKeepalive();
|
|
212
|
+
}, COMBO_SELECTION_KEEPALIVE_INTERVAL_MS);
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
return new Promise<Response>((resolve, reject) => {
|
|
216
|
+
const selection = select({ signal, headCommitted: () => committed });
|
|
217
|
+
timer = clock.setTimeout(() => {
|
|
218
|
+
timer = undefined;
|
|
219
|
+
// A cancelled request is about to settle as a client cancellation; it needs no head.
|
|
220
|
+
if (settled || signal.aborted) return;
|
|
221
|
+
committed = true;
|
|
222
|
+
scheduleKeepalive();
|
|
223
|
+
resolve(new Response(body, {
|
|
224
|
+
status: 200,
|
|
225
|
+
headers: {
|
|
226
|
+
"Content-Type": "text/event-stream",
|
|
227
|
+
"Cache-Control": "no-cache",
|
|
228
|
+
Connection: "keep-alive",
|
|
229
|
+
"X-Accel-Buffering": "no",
|
|
230
|
+
},
|
|
231
|
+
}));
|
|
232
|
+
}, COMBO_SELECTION_KEEPALIVE_THRESHOLD_MS);
|
|
233
|
+
selection.then(response => {
|
|
234
|
+
settled = true;
|
|
235
|
+
stopTimer();
|
|
236
|
+
if (!committed) {
|
|
237
|
+
resolve(response);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
deliver(response).catch(() => {
|
|
241
|
+
deliverFailure(502, { type: "server_error", code: "server_error", message: THROWN_SELECTION_MESSAGE });
|
|
242
|
+
});
|
|
243
|
+
}, error => {
|
|
244
|
+
settled = true;
|
|
245
|
+
stopTimer();
|
|
246
|
+
if (!committed) {
|
|
247
|
+
reject(error);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
console.warn(`[combo] selection failed after its keepalive head: ${error instanceof Error ? error.name : typeof error}`);
|
|
251
|
+
deliverFailure(502, { type: "server_error", code: "server_error", message: THROWN_SELECTION_MESSAGE });
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
}
|
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
} from "../../reasoning-effort";
|
|
24
24
|
import { admissionMayUseMeteredComboTargets, resolveAdmissionModelScope, routeAllowedByScope } from "../admission-model-scope";
|
|
25
25
|
import { recordAttemptRequestedEffort } from "../request-log";
|
|
26
|
-
import { failLinkTurnExpansion, markLinkTurnMissing, observeLinkTurnExpansion } from "../link-turn";
|
|
26
|
+
import { bindLinkTurnChild, failLinkTurnExpansion, markLinkTurnMissing, observeLinkTurnExpansion } from "../link-turn";
|
|
27
27
|
import {
|
|
28
28
|
CODEX_TEXT_GUARDED_BUDGET_POLICY,
|
|
29
29
|
deriveRequestExecutionBudget,
|
|
@@ -47,7 +47,9 @@ import {
|
|
|
47
47
|
comboDefaultEffort,
|
|
48
48
|
isComboTargetInCooldown,
|
|
49
49
|
snapshotComboQuotaCooldowns,
|
|
50
|
-
|
|
50
|
+
comboExclusion,
|
|
51
|
+
comboUnavailableDiagnostics,
|
|
52
|
+
comboTargetUnavailableCause,
|
|
51
53
|
comboTargetSkipTrigger,
|
|
52
54
|
noteComboSuccess,
|
|
53
55
|
settleComboReprobeSuccess,
|
|
@@ -605,6 +607,10 @@ export async function executeComboResponses(
|
|
|
605
607
|
const enabled = combo.targets.filter(target => config.providers[target.provider]?.disabled !== true);
|
|
606
608
|
return enabled.length > 0 && enabled.every(target => !museToolEligible(target));
|
|
607
609
|
};
|
|
610
|
+
// A mandatory tool_choice no target preserves is unsupported (#7965), but shared exclusion evidence
|
|
611
|
+
// (provider state, quota, live cooldowns) keeps precedence, exactly as in the readable reasons.
|
|
612
|
+
const unsupportedToolChoice = (attempted: number) => comboUnavailableDiagnostics(combo.targets.length, attempted,
|
|
613
|
+
combo.targets.map(target => comboTargetUnavailableCause(config, comboId, target, museToolEligible) ?? "unsupported"));
|
|
608
614
|
// Recovery can replace unreadable encrypted input, but cannot relax other admission checks.
|
|
609
615
|
const recoveryEligible = (target: (typeof combo.targets)[number]): boolean =>
|
|
610
616
|
(meteredAllowed || !target.metered)
|
|
@@ -725,12 +731,13 @@ export async function executeComboResponses(
|
|
|
725
731
|
{ parentThreadId: inboundClientThreadId },
|
|
726
732
|
);
|
|
727
733
|
if (!options.abortSignal?.aborted && onlyMuseToolIncompatible()) {
|
|
728
|
-
return comboUnavailableResponse("No available targets for combo; targets cannot preserve mandatory tool_choice"
|
|
734
|
+
return comboUnavailableResponse("No available targets for combo; targets cannot preserve mandatory tool_choice",
|
|
735
|
+
{ diagnostics: unsupportedToolChoice(0) });
|
|
729
736
|
}
|
|
730
737
|
return options.abortSignal?.aborted
|
|
731
738
|
? clientCancelledResponse()
|
|
732
739
|
: comboUnavailable(comboId, undefined,
|
|
733
|
-
|
|
740
|
+
comboExclusion(config, comboId, recoveryEligible));
|
|
734
741
|
}
|
|
735
742
|
if (!(await recoverUnreadableEncryptedTask())) {
|
|
736
743
|
return options.abortSignal?.aborted
|
|
@@ -745,11 +752,12 @@ export async function executeComboResponses(
|
|
|
745
752
|
if (protocolRefusal) return protocolRefusal;
|
|
746
753
|
if (onlyReplayIncompatibleTargetsRemain()) return targetIncompatibleResponse();
|
|
747
754
|
if (!options.abortSignal?.aborted && onlyMuseToolIncompatible()) {
|
|
748
|
-
return comboUnavailableResponse("No available targets for combo; targets cannot preserve mandatory tool_choice"
|
|
755
|
+
return comboUnavailableResponse("No available targets for combo; targets cannot preserve mandatory tool_choice",
|
|
756
|
+
{ diagnostics: unsupportedToolChoice(0) });
|
|
749
757
|
}
|
|
750
758
|
return options.abortSignal?.aborted
|
|
751
759
|
? clientCancelledResponse()
|
|
752
|
-
: comboUnavailable(comboId, undefined,
|
|
760
|
+
: comboUnavailable(comboId, undefined, comboExclusion(config, comboId, targetEligible));
|
|
753
761
|
}
|
|
754
762
|
// OCX-11: an ordered failover combo that starts on a later target than its first one, for any
|
|
755
763
|
// reason (cooling down, provider disabled, cached quota spent, ineligible for this request),
|
|
@@ -780,7 +788,7 @@ export async function executeComboResponses(
|
|
|
780
788
|
const choices = eligibleJevComboChoices(config, comboId, targetEligible, Date.now(), pick.cooldownReprobeTargets);
|
|
781
789
|
const first = choices[0];
|
|
782
790
|
if (!first) {
|
|
783
|
-
return comboUnavailable(comboId, undefined,
|
|
791
|
+
return comboUnavailable(comboId, undefined, comboExclusion(config, comboId, target =>
|
|
784
792
|
targetEligible(target) && target.provider !== JEV_PROVIDER_ID && jevChoiceEfforts(config, target, false) !== undefined));
|
|
785
793
|
}
|
|
786
794
|
const fallbackFor = (choice: JevComboChoice): Pick<JevDecision, "targetKey" | "effort"> => {
|
|
@@ -894,7 +902,7 @@ export async function executeComboResponses(
|
|
|
894
902
|
});
|
|
895
903
|
// A completed decision belongs to the parent log even if no model can be dispatched.
|
|
896
904
|
if (selectionUnavailable) {
|
|
897
|
-
return comboUnavailable(comboId, undefined,
|
|
905
|
+
return comboUnavailable(comboId, undefined, comboExclusion(config, comboId, target =>
|
|
898
906
|
targetEligible(target) && target.provider !== JEV_PROVIDER_ID && jevChoiceEfforts(config, target, false) !== undefined));
|
|
899
907
|
}
|
|
900
908
|
}
|
|
@@ -970,12 +978,13 @@ export async function executeComboResponses(
|
|
|
970
978
|
const returnsQuota = lastFailure && [400, 401, 403].includes(lastFailure.status) && quotaFailure;
|
|
971
979
|
const failedChildLog = returnsQuota ? quotaFailedChildLog : lastFailedChildLog;
|
|
972
980
|
if (failedChildLog) adoptFailedChildLog(failedChildLog);
|
|
973
|
-
if (targetsExhausted && toolChoiceRefusalsOnly === true) return comboUnavailableResponse("No available targets for combo; attempted targets cannot preserve mandatory tool_choice");
|
|
981
|
+
if (targetsExhausted && toolChoiceRefusalsOnly === true) return comboUnavailableResponse("No available targets for combo; attempted targets cannot preserve mandatory tool_choice", { diagnostics: unsupportedToolChoice(comboTargetsDispatched) });
|
|
974
982
|
if (!lastFailure || ![400, 401, 403].includes(lastFailure.status)) return lastFailure!;
|
|
975
983
|
if (quotaFailure) return quotaFailure;
|
|
976
984
|
// The quota-refused target was cooled before this request picked, so the ladder never saw
|
|
977
985
|
// its refusal. The cooldown, with its Retry-After, is still the honest answer.
|
|
978
986
|
let quotaCooldownUntil: number | undefined;
|
|
987
|
+
let quotaCooledTargets = 0;
|
|
979
988
|
const now = Date.now();
|
|
980
989
|
for (const { target, cooldownUntil } of quotaCooldownSnapshot) {
|
|
981
990
|
if (cooldownUntil <= now) continue;
|
|
@@ -986,11 +995,15 @@ export async function executeComboResponses(
|
|
|
986
995
|
continue;
|
|
987
996
|
}
|
|
988
997
|
quotaCooldownUntil = Math.min(quotaCooldownUntil ?? cooldownUntil, cooldownUntil);
|
|
998
|
+
quotaCooledTargets += 1;
|
|
989
999
|
}
|
|
990
1000
|
const remainingQuotaCooldownMs = quotaCooldownUntil === undefined ? 0 : quotaCooldownUntil - Date.now();
|
|
991
1001
|
return remainingQuotaCooldownMs > 0
|
|
992
1002
|
? comboUnavailableResponse("No available targets for combo", {
|
|
993
1003
|
retryAfter: String(Math.max(1, Math.ceil(remainingQuotaCooldownMs / 1000))),
|
|
1004
|
+
// Only quota-cooled eligible targets can produce this answer, so its class is quota.
|
|
1005
|
+
diagnostics: comboUnavailableDiagnostics(combo.targets.length, comboTargetsDispatched,
|
|
1006
|
+
Array.from({ length: quotaCooledTargets }, () => "quota" as const)),
|
|
994
1007
|
}) : lastFailure;
|
|
995
1008
|
};
|
|
996
1009
|
// The caller's output ceiling, captured once so a floor applied to one target never
|
|
@@ -1093,6 +1106,8 @@ export async function executeComboResponses(
|
|
|
1093
1106
|
...(logCtx.affinityLane ? { affinityLane: logCtx.affinityLane } : {}),
|
|
1094
1107
|
...(logCtx.surface ? { surface: logCtx.surface } : {}),
|
|
1095
1108
|
};
|
|
1109
|
+
// A link turn's child reads its upstream only as the client drains, as the parent would (#8601).
|
|
1110
|
+
bindLinkTurnChild(logCtx, childLog);
|
|
1096
1111
|
const targetReasoningEfforts = supportedLadderFor({
|
|
1097
1112
|
provider: targetRoute.provider,
|
|
1098
1113
|
modelId: targetRoute.modelId,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ComboExclusion, ComboUnavailableDiagnostics } from "../../combos/resolve";
|
|
2
2
|
import { markProxyError } from "../../lib/client-error-body";
|
|
3
3
|
import { appendClientRetryIn, formatClientRetryAfter } from "../../lib/client-retry-after";
|
|
4
4
|
import { readBoundedResponseBody } from "../../lib/bounded-body";
|
|
@@ -104,7 +104,7 @@ export function decodeRequestErrorResponse(err: unknown, label: string): Respons
|
|
|
104
104
|
|
|
105
105
|
export function comboUnavailableResponse(
|
|
106
106
|
message: string,
|
|
107
|
-
options?: { retryAfter?: string | null; presentation?: "combo" },
|
|
107
|
+
options?: { retryAfter?: string | null; presentation?: "combo"; diagnostics?: ComboUnavailableDiagnostics },
|
|
108
108
|
): Response {
|
|
109
109
|
const headers = new Headers({ "Content-Type": "application/json" });
|
|
110
110
|
// Advice leaves in this proxy's format, whatever form the earliest cooldown was recorded in.
|
|
@@ -115,17 +115,20 @@ export function comboUnavailableResponse(
|
|
|
115
115
|
return markProxyError(new Response(
|
|
116
116
|
JSON.stringify({
|
|
117
117
|
// Built from the header value itself, so an HTTP-date cannot make the body and header disagree.
|
|
118
|
-
|
|
118
|
+
// `combo` carries the machine-readable cause class and target counts (#7965).
|
|
119
|
+
error: { message: appendClientRetryIn(message, advice), type: "server_error", code: "combo_unavailable",
|
|
120
|
+
...(options?.diagnostics ? { combo: options.diagnostics } : {}) },
|
|
119
121
|
}),
|
|
120
122
|
{ status: 503, headers },
|
|
121
123
|
), options?.presentation);
|
|
122
124
|
}
|
|
123
125
|
|
|
124
126
|
|
|
125
|
-
export function comboUnavailable(comboId: string, now = Date.now(),
|
|
126
|
-
return comboUnavailableResponse(new NoAvailableComboTargetsError(comboId,
|
|
127
|
+
export function comboUnavailable(comboId: string, now = Date.now(), exclusion?: ComboExclusion): Response {
|
|
128
|
+
return comboUnavailableResponse(new NoAvailableComboTargetsError(comboId, exclusion).message, {
|
|
127
129
|
retryAfter: comboCooldownRetryAfterSeconds(comboId, now),
|
|
128
|
-
presentation:
|
|
130
|
+
presentation: exclusion === undefined ? undefined : "combo",
|
|
131
|
+
...(exclusion ? { diagnostics: exclusion.diagnostics } : {}),
|
|
129
132
|
});
|
|
130
133
|
}
|
|
131
134
|
|
|
@@ -226,6 +226,14 @@ export interface HandleResponsesOptions {
|
|
|
226
226
|
protocolSource?: import("./core-combo-native").ComboProtocolSource;
|
|
227
227
|
/** Internal transport identity for route-scoped upstream compatibility policy. */
|
|
228
228
|
inboundTransport?: "websocket";
|
|
229
|
+
/**
|
|
230
|
+
* Set by the HTTP `/v1/responses` ingress only: a streaming combo turn whose target selection
|
|
231
|
+
* outlasts `COMBO_SELECTION_KEEPALIVE_THRESHOLD_MS` commits a 200 SSE head and sends keepalive
|
|
232
|
+
* comments (#8326). `clock` is a test seam; production uses the global timers.
|
|
233
|
+
*/
|
|
234
|
+
comboSelectionKeepalive?: {
|
|
235
|
+
clock?: { setTimeout(callback: () => void, ms: number): unknown; clearTimeout(handle: unknown): void };
|
|
236
|
+
};
|
|
229
237
|
/**
|
|
230
238
|
* Claude replay may add native-main auth so OpenAI sidecars remain available.
|
|
231
239
|
* Strip only that internal credential when the final route is a noncanonical
|
|
@@ -12,6 +12,11 @@ import { createInferenceSendBudget } from "../inference/context";
|
|
|
12
12
|
import { finalizeOwnedTranslatorBudget, finalizeAccountLease } from "./core-lifetime";
|
|
13
13
|
import type { TranslatorBudget } from "../../lib/translator-budget";
|
|
14
14
|
import { executeComboResponses } from "./core-combo";
|
|
15
|
+
import {
|
|
16
|
+
comboSelectionKeepaliveApplies,
|
|
17
|
+
comboSelectionKeepaliveLadderOptions,
|
|
18
|
+
selectComboWithKeepalive,
|
|
19
|
+
} from "./combo-selection-keepalive";
|
|
15
20
|
import { registerTurn, unregisterTurn } from "../lifecycle";
|
|
16
21
|
import { runWithCompactionRecovery } from "./compaction-recovery";
|
|
17
22
|
import { captureAdmissionPolicy } from "../capture-admission-policy";
|
|
@@ -71,6 +76,22 @@ export async function handleComboResponses(
|
|
|
71
76
|
config: OcxConfig,
|
|
72
77
|
logCtx: RequestLogContext,
|
|
73
78
|
options: HandleResponsesOptions & { translatorBudget: TranslatorBudget },
|
|
79
|
+
): Promise<Response> {
|
|
80
|
+
if (!comboSelectionKeepaliveApplies(rawBody, options)) return runComboLadder(req, rawBody, comboId, config, logCtx, options);
|
|
81
|
+
// A slow selection may answer with a keepalive head while the ladder below keeps running (#8326).
|
|
82
|
+
const model = (rawBody as { model?: unknown }).model;
|
|
83
|
+
return selectComboWithKeepalive(
|
|
84
|
+
hooks => runComboLadder(req, rawBody, comboId, config, logCtx, comboSelectionKeepaliveLadderOptions(options, hooks)),
|
|
85
|
+
{ model: typeof model === "string" ? model : comboId, logCtx, signal: options.abortSignal, clock: options.comboSelectionKeepalive?.clock },
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
async function runComboLadder(
|
|
89
|
+
req: Request,
|
|
90
|
+
rawBody: unknown,
|
|
91
|
+
comboId: string,
|
|
92
|
+
config: OcxConfig,
|
|
93
|
+
logCtx: RequestLogContext,
|
|
94
|
+
options: HandleResponsesOptions & { translatorBudget: TranslatorBudget },
|
|
74
95
|
): Promise<Response> {
|
|
75
96
|
// Each child relay binds the turn's lease and unbinds it when its stream ends; the lease is
|
|
76
97
|
// released once nothing is bound. Hold it for the whole ladder so a refused child's relay
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { estimateTokens } from "../../lib/token-estimate";
|
|
2
|
+
import type { SsePayloadRewrite } from "../sse-payload-rewrite";
|
|
3
|
+
import type { HandleResponsesOptions } from "./core-options";
|
|
4
|
+
import type { OcxProviderConfig } from "../../types";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Response header marking estimated usage (#7427). `true`: the JSON body's usage is an estimate.
|
|
8
|
+
* `when-missing`: the stream's usage is estimated wherever the provider omitted it.
|
|
9
|
+
*/
|
|
10
|
+
export const ESTIMATED_USAGE_HEADER = "x-opencodex-usage-estimated";
|
|
11
|
+
|
|
12
|
+
/** Terminal events whose `response.usage` a client reads for context accounting. */
|
|
13
|
+
const TERMINAL_TYPES = new Set(["response.completed", "response.incomplete"]);
|
|
14
|
+
/** Response statuses that end a turn with output a client counts; failed and running ones do not. */
|
|
15
|
+
const TERMINAL_STATUSES = new Set(["completed", "incomplete"]);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Whether this turn may carry an estimate: the provider opted in, and the client spoke HTTP
|
|
19
|
+
* Responses. The Chat Completions and Claude Messages bridges and the WebSocket ingress rebuild
|
|
20
|
+
* their own usage fields from the response and would pass an estimate on without the marker, so
|
|
21
|
+
* they never get one. An internal decision call (JEV) settles spend from its response body, so it
|
|
22
|
+
* never gets one either.
|
|
23
|
+
*/
|
|
24
|
+
export function estimatesMissingUsage(
|
|
25
|
+
provider: Pick<OcxProviderConfig, "estimateMissingUsage">,
|
|
26
|
+
options: Pick<HandleResponsesOptions, "inboundWire" | "inboundTransport" | "internalDecisionCall">,
|
|
27
|
+
): boolean {
|
|
28
|
+
return provider.estimateMissingUsage === true
|
|
29
|
+
&& (options.inboundWire ?? "responses") === "responses"
|
|
30
|
+
&& options.inboundTransport !== "websocket"
|
|
31
|
+
&& options.internalDecisionCall !== true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
35
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Text a model produced in a Responses `output` array: messages, refusals, tool input, summaries. */
|
|
39
|
+
function outputText(output: unknown): string {
|
|
40
|
+
if (!Array.isArray(output)) return "";
|
|
41
|
+
const parts: string[] = [];
|
|
42
|
+
for (const item of output) {
|
|
43
|
+
if (!isRecord(item)) continue;
|
|
44
|
+
for (const field of ["arguments", "input", "name"] as const) {
|
|
45
|
+
if (typeof item[field] === "string") parts.push(item[field] as string);
|
|
46
|
+
}
|
|
47
|
+
for (const list of [item.content, item.summary]) {
|
|
48
|
+
if (!Array.isArray(list)) continue;
|
|
49
|
+
for (const part of list) {
|
|
50
|
+
if (!isRecord(part)) continue;
|
|
51
|
+
if (typeof part.text === "string") parts.push(part.text);
|
|
52
|
+
if (typeof part.refusal === "string") parts.push(part.refusal);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return parts.join("\n");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The response with an estimated `usage` when the provider sent none, or `undefined` when it has
|
|
61
|
+
* usage already or is not a response object. Input comes from the request estimate, output from
|
|
62
|
+
* the response's own output items. A client's context meter reads it; OpenCodex's cost, spend and
|
|
63
|
+
* quota accounting never does, because they read the provider's original body.
|
|
64
|
+
*/
|
|
65
|
+
export function withEstimatedUsage(
|
|
66
|
+
response: unknown, inputTokens: number, modelId: string, streamedOutputTokens?: number,
|
|
67
|
+
): Record<string, unknown> | undefined {
|
|
68
|
+
// Only a finished Responses object with output; error bodies, failed turns and partial events
|
|
69
|
+
// are left alone even when they arrive with HTTP 200.
|
|
70
|
+
if (!isRecord(response) || isRecord(response.usage) || !Array.isArray(response.output)) return undefined;
|
|
71
|
+
if (typeof response.status !== "string" || !TERMINAL_STATUSES.has(response.status)) return undefined;
|
|
72
|
+
if (response.error !== undefined && response.error !== null) return undefined;
|
|
73
|
+
const input = Math.max(0, Math.round(inputTokens));
|
|
74
|
+
// A terminal that closes with an empty list counts the items streamed before it instead.
|
|
75
|
+
const output = response.output.length === 0 && streamedOutputTokens !== undefined
|
|
76
|
+
? streamedOutputTokens
|
|
77
|
+
: estimateTokens(outputText(response.output), modelId);
|
|
78
|
+
return { ...response, usage: { input_tokens: input, output_tokens: output, total_tokens: input + output } };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* SSE payload rewrite for opted-in native routes; `onApplied` fires once per estimated terminal.
|
|
83
|
+
* Each `response.output_item.done` adds its estimate to one running count, so a terminal whose
|
|
84
|
+
* `output` is empty is still estimated without the stream retaining any item.
|
|
85
|
+
*/
|
|
86
|
+
export function createEstimatedUsagePayloadRewrite(
|
|
87
|
+
inputTokens: () => number, modelId: string, onApplied?: () => void,
|
|
88
|
+
): SsePayloadRewrite {
|
|
89
|
+
let streamedOutputTokens = 0;
|
|
90
|
+
return payload => {
|
|
91
|
+
// Cheap gate: only finished items and terminal events are parsed, never every delta.
|
|
92
|
+
const itemDone = payload.includes("response.output_item.done");
|
|
93
|
+
if (!itemDone && !payload.includes("response.completed") && !payload.includes("response.incomplete")) return payload;
|
|
94
|
+
let parsed: unknown;
|
|
95
|
+
try { parsed = JSON.parse(payload); } catch { return payload; }
|
|
96
|
+
if (!isRecord(parsed) || typeof parsed.type !== "string") return payload;
|
|
97
|
+
if (parsed.type === "response.output_item.done") {
|
|
98
|
+
streamedOutputTokens += estimateTokens(outputText([parsed.item]), modelId);
|
|
99
|
+
return payload;
|
|
100
|
+
}
|
|
101
|
+
if (!TERMINAL_TYPES.has(parsed.type)) return payload;
|
|
102
|
+
const response = withEstimatedUsage(parsed.response, inputTokens(), modelId, streamedOutputTokens);
|
|
103
|
+
if (!response) return payload;
|
|
104
|
+
onApplied?.();
|
|
105
|
+
return JSON.stringify({ ...parsed, response });
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The JSON body with estimated usage, or `undefined` when it already had usage or is not JSON. */
|
|
110
|
+
export function estimatedUsageJson(json: string, inputTokens: number, modelId: string): string | undefined {
|
|
111
|
+
let parsed: unknown;
|
|
112
|
+
try { parsed = JSON.parse(json); } catch { return undefined; }
|
|
113
|
+
const response = withEstimatedUsage(parsed, inputTokens, modelId);
|
|
114
|
+
return response ? JSON.stringify(response) : undefined;
|
|
115
|
+
}
|
|
@@ -12,6 +12,7 @@ import { tryAdmitTurn } from "../lifecycle";
|
|
|
12
12
|
import type { RequestLogContext } from "../request-log";
|
|
13
13
|
import { createSseInspector } from "../relay";
|
|
14
14
|
import type { HandleResponsesOptions } from "./core-options";
|
|
15
|
+
import { ESTIMATED_USAGE_HEADER } from "./estimated-usage";
|
|
15
16
|
|
|
16
17
|
type HandleResponses = (
|
|
17
18
|
req: Request,
|
|
@@ -183,8 +184,12 @@ export function createJevModelInvoker(context: JevModelInvokerContext): JevModel
|
|
|
183
184
|
if (!isRecord(parsed)) throw new JevModelInvokeError("malformed", "decision response is not an object");
|
|
184
185
|
return terminalResult(parsed, "");
|
|
185
186
|
})();
|
|
186
|
-
|
|
187
|
-
|
|
187
|
+
// A marked response carries an estimate for a client's display, never spend evidence.
|
|
188
|
+
const estimated = response.headers.has(ESTIMATED_USAGE_HEADER);
|
|
189
|
+
const { usage: bodyUsage, ...withoutUsage } = result;
|
|
190
|
+
const settled = estimated ? withoutUsage : result;
|
|
191
|
+
usage = estimated ? undefined : bodyUsage;
|
|
192
|
+
return quotaOmitted ? { ...settled, quotaOmitted: true } : settled;
|
|
188
193
|
} finally {
|
|
189
194
|
// No final log row is written for this detached context, so settle its spend here.
|
|
190
195
|
childLog.spendTracker?.settle(childLog.usage ?? (usage
|
|
@@ -2,8 +2,8 @@ import type { AdapterRequest } from "../../adapters/base";
|
|
|
2
2
|
import { formatErrorResponse } from "../../bridge";
|
|
3
3
|
import type { MainQuotaDispatch } from "../../codex/main-account-cache";
|
|
4
4
|
import { isLocalUpstream } from "../../lib/local-upstream";
|
|
5
|
-
import { guardResponseBodyInactivity } from "../../lib/response-body-inactivity";
|
|
6
|
-
import { resolveStallTimeoutMs } from "../../stall-timeout";
|
|
5
|
+
import { guardResponseBodyInactivity, markResponseBodyInactivityGuarded } from "../../lib/response-body-inactivity";
|
|
6
|
+
import { explicitStallTimeoutMs, resolveStallTimeoutMs } from "../../stall-timeout";
|
|
7
7
|
import type { OcxConfig } from "../../types";
|
|
8
8
|
import { recordKeyWireAttemptUsage, usageFromResponsesPayload, type RequestLogContext } from "../request-log";
|
|
9
9
|
import { selectProtocolSafeResetResponse, type ProtocolSafeResetRecovery } from "./combo-stream-preflight";
|
|
@@ -79,8 +79,10 @@ export function selectPassthroughResetResponse(
|
|
|
79
79
|
const openGateway = options.request.url === "https://apis.opengateway.ai/v1/responses"
|
|
80
80
|
&& response.status === 200 && response.body !== null;
|
|
81
81
|
const stallMs = resolveStallTimeoutMs(options.config.stallTimeoutSec, { localUpstream: isLocalUpstream(options.request.url) });
|
|
82
|
+
// The same budget, but reported as a stall only when the operator set it explicitly (#8206).
|
|
83
|
+
const explicitBudget = explicitStallTimeoutMs(options.config.stallTimeoutSec) > 0;
|
|
82
84
|
const protect = (source: Response): Response =>
|
|
83
|
-
carryResponseToolOwnership(source, guardResponseBodyInactivity(source, options.signal, stallMs).response);
|
|
85
|
+
carryResponseToolOwnership(source, guardResponseBodyInactivity(source, options.signal, stallMs, { explicitBudget }).response);
|
|
84
86
|
const quotaOwner = (): MainQuotaDispatch | undefined =>
|
|
85
87
|
admission.authCtx.kind === "main" ? admission.authCtx.mainQuotaDispatch : undefined;
|
|
86
88
|
const originalOwner = quotaOwner();
|
|
@@ -101,6 +103,7 @@ export function selectPassthroughResetResponse(
|
|
|
101
103
|
signal: options.signal,
|
|
102
104
|
maxSelectionMs: stallMs > 0 ? stallMs : undefined,
|
|
103
105
|
onCommit: (published, owner) => {
|
|
106
|
+
if (stallMs > 0) markResponseBodyInactivityGuarded(published);
|
|
104
107
|
if (openGateway) guardedResetResponses.set(published, heldSince);
|
|
105
108
|
if (options.delivery === "buffered-json") bufferedSelectionStarts.set(published, selectionStartedAt);
|
|
106
109
|
bindPublishedMainQuotaDispatch(published, owner === "replacement" ? replacementOwner : originalOwner);
|