@bitkyc08/opencodex 2.95.0 → 2.97.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.
- package/bin/ocx.mjs +214 -19
- package/gui/dist/assets/{App-8yedBkhm.js → App-DuNhggE1.js} +18 -18
- package/gui/dist/assets/{Tray-erSMzxPG.js → Tray-CEw0y0jR.js} +1 -1
- package/gui/dist/assets/index-CynE_Rue.js +94 -0
- package/gui/dist/assets/{quota-summary-BK4VuQNc.js → quota-summary-VHFSlUuM.js} +1 -1
- package/gui/dist/index.html +1 -1
- package/package.json +1 -1
- package/src/adapters/command-code.ts +15 -16
- package/src/adapters/cursor/transport-retry.ts +2 -2
- package/src/adapters/devin/cloud-direct/chat.ts +15 -6
- package/src/adapters/kiro/stream.ts +63 -4
- package/src/adapters/kiro-constants.ts +15 -1
- package/src/adapters/kiro-events.ts +31 -1
- package/src/adapters/kiro-retry.ts +3 -3
- package/src/adapters/ollama-native.ts +21 -7
- package/src/adapters/openai-responses/canonical-forward.ts +50 -13
- package/src/adapters/openai-responses/empty-assistant.ts +15 -3
- package/src/adapters/openai-responses/passthrough.ts +8 -8
- package/src/adapters/openai-responses/reasoning.ts +9 -4
- package/src/adapters/openai-responses/request-strips.ts +1 -1
- package/src/adapters/openai-responses/transport.ts +17 -26
- package/src/adapters/physical-send.ts +3 -3
- package/src/adapters/qoder/adapter.ts +1 -1
- package/src/adapters/qoder/image-input.ts +5 -2
- package/src/adapters/zed-openai-stream.ts +2 -1
- package/src/bridge/errors.ts +11 -2
- package/src/bridge/response-json.ts +2 -1
- package/src/bridge/sse.ts +14 -6
- package/src/chat/failure-verdict.ts +21 -0
- package/src/chat/outbound.ts +139 -21
- package/src/claude/outbound.ts +49 -2
- package/src/cli/agent.ts +44 -15
- package/src/cli/capabilities-agents-routing.ts +9 -0
- package/src/cli/capabilities-base.ts +25 -3
- package/src/cli/capabilities-command.ts +12 -3
- package/src/cli/capability-contracts.ts +2 -0
- package/src/cli/codex-launch-auth.ts +30 -1
- package/src/cli/codex-launcher.ts +12 -5
- package/src/cli/codex-shim-autorestore.ts +5 -4
- package/src/cli/dispatch.ts +11 -176
- package/src/cli/ensure-desired-integrations.ts +4 -3
- package/src/cli/help.ts +2 -2
- package/src/cli/index.ts +9 -2
- package/src/cli/link.ts +1 -1
- package/src/cli/message-command.ts +2 -1
- package/src/cli/message-mcp-command.ts +29 -0
- package/src/cli/registry.ts +12 -4
- package/src/cli/root.ts +8 -0
- package/src/cli/sync-command.ts +243 -0
- package/src/cli/sync-fanout-process.ts +92 -0
- package/src/cli/sync-fanout.ts +150 -0
- package/src/cli/sync-flags.ts +10 -0
- package/src/cli/sync-link-status.ts +39 -0
- package/src/cli/sync-update-guard.ts +18 -0
- package/src/client/connect.ts +31 -14
- package/src/client/link-home.ts +3 -2
- package/src/client/machine-listener.ts +3 -0
- package/src/client/restore-ownership.ts +19 -1
- package/src/client/runtime-restart.ts +12 -4
- package/src/client/runtime.ts +40 -11
- package/src/client/stop-routing.ts +62 -0
- package/src/client/sync-runtime.ts +65 -0
- package/src/clients/config-export.ts +32 -3
- package/src/codex/account-store.ts +1 -0
- package/src/codex/auth-context-provider.ts +15 -0
- package/src/codex/auth-context.ts +59 -122
- package/src/codex/catalog/antigravity-discovery.ts +16 -5
- package/src/codex/catalog/provider-models.ts +6 -8
- package/src/codex/forward-transport-headers.ts +65 -36
- package/src/codex/inject/config-toml.ts +88 -0
- package/src/codex/inject/multi-agent-v2.ts +30 -4
- package/src/codex/inject/plan.ts +34 -7
- package/src/codex/inject-coordination.ts +11 -0
- package/src/codex/inject.ts +18 -7
- package/src/codex/installation-id.ts +72 -0
- package/src/codex/lineage.ts +13 -7
- package/src/codex/quota.ts +22 -6
- package/src/codex/request-owned-affinity-key.ts +11 -0
- package/src/codex/request-owned-affinity.ts +118 -0
- package/src/codex/request-owned-main-pin.ts +86 -0
- package/src/codex/role-capability.ts +52 -41
- package/src/codex/routing/active-account.ts +13 -5
- package/src/codex/routing/selection.ts +40 -1
- package/src/codex/routing/thread-affinity-store.ts +916 -0
- package/src/codex/routing/thread-affinity.ts +90 -12
- package/src/codex/toml-source-lines.ts +72 -13
- package/src/combos/failover.ts +6 -109
- package/src/combos/request.ts +2 -2
- package/src/combos/resolve.ts +29 -11
- package/src/config/process-state.ts +5 -0
- package/src/config/schema/leaf-validators.ts +1 -0
- package/src/generated/compatibility-version.json +496 -192
- package/src/grok/sync.ts +10 -1
- package/src/images/loop.ts +2 -1
- package/src/lib/client-error-body.ts +184 -0
- package/src/lib/client-retry-after.ts +66 -0
- package/src/lib/config-ownership.ts +4 -0
- package/src/lib/context-overflow-verdict.ts +113 -0
- package/src/lib/desktop-cli-handoff.d.mts +1 -1
- package/src/lib/desktop-cli-handoff.mjs +11 -1
- package/src/lib/empty-tool-catalog.ts +25 -3
- package/src/lib/error-destination.ts +47 -0
- package/src/lib/failure-detail.ts +66 -0
- package/src/lib/model-identity.ts +44 -0
- package/src/lib/private-sqlite-path.ts +124 -0
- package/src/lib/response-service-tier.ts +17 -0
- package/src/lib/retry-after.ts +3 -0
- package/src/lib/spend-ledger-owner.ts +43 -4
- package/src/lib/upstream-condition.ts +142 -0
- package/src/lib/upstream-retry.ts +14 -11
- package/src/lib/windows-owner-acl.ts +13 -6
- package/src/lib/windows-secret-acl.ts +2 -2
- package/src/link/remote-platform.ts +14 -0
- package/src/link/remote-windows.ts +51 -9
- package/src/link/ssh-argv.ts +63 -10
- package/src/messaging/mcp-queue-rpc.ts +102 -0
- package/src/messaging/mcp-queue-server.ts +42 -0
- package/src/messaging/mcp-queue-tools.ts +39 -0
- package/src/oauth/anthropic-identity.ts +2 -6
- package/src/oauth/generic-account-failover.ts +13 -11
- package/src/oauth/index.ts +41 -74
- package/src/oauth/oauth-errors.ts +67 -0
- package/src/oauth/store.ts +1 -1
- package/src/protocols/codecs/ollama.ts +197 -0
- package/src/protocols/encoders/adapter-events.ts +4 -0
- package/src/protocols/encoders/chat.ts +18 -10
- package/src/protocols/trace.ts +18 -0
- package/src/providers/anthropic-subscription.ts +25 -0
- package/src/providers/claude-cli-usage.ts +15 -67
- package/src/providers/fastwire.ts +3 -2
- package/src/providers/quota/anthropic-decision-windows.ts +5 -19
- package/src/providers/quota/anthropic-usage-windows.ts +65 -0
- package/src/providers/quota/vendor-probes-key.ts +60 -24
- package/src/providers/request-pacing.ts +3 -2
- package/src/responses/apply-patch-envelope.ts +45 -19
- package/src/responses/compaction-history-framing.ts +5 -1
- package/src/responses/freeform-wrapper-scan.ts +5 -3
- package/src/responses/parser-tools.ts +1 -0
- package/src/responses/progressive-freeform-input.ts +354 -45
- package/src/responses/schema.ts +5 -2
- package/src/routing/fallback-policy.ts +3 -1
- package/src/server/admission-model-scope.ts +17 -6
- package/src/server/admitted-http-turn.ts +56 -0
- package/src/server/audio-dictation.ts +3 -0
- package/src/server/audio-live.ts +8 -2
- package/src/server/audio-upstream.ts +2 -0
- package/src/server/auth-cors.ts +5 -0
- package/src/server/chat-completions.ts +45 -20
- package/src/server/chat-native-sse.ts +52 -7
- package/src/server/chat-native.ts +60 -35
- package/src/server/claude-bridge-pool-session.ts +59 -0
- package/src/server/claude-messages.ts +57 -27
- package/src/server/context-history.ts +14 -7
- package/src/server/images.ts +14 -5
- package/src/server/inbound-body-admission.ts +11 -4
- package/src/server/index/live-sideband.ts +16 -2
- package/src/server/index/ollama-routes.ts +89 -0
- package/src/server/index/serve-options.ts +22 -4
- package/src/server/index/websocket-handler.ts +5 -3
- package/src/server/index.ts +6 -36
- package/src/server/inference/client-encoder-delivery.ts +4 -2
- package/src/server/json-serialized-bytes.ts +44 -0
- package/src/server/live.ts +30 -5
- package/src/server/management/agent-settings-routes.ts +24 -21
- package/src/server/management/codex-cli-update-routes.ts +2 -1
- package/src/server/management/route-registry.ts +1 -1
- package/src/server/messages-native-scope.ts +2 -1
- package/src/server/messages-native-tool-guard.ts +1 -1
- package/src/server/messages-native.ts +12 -7
- package/src/server/messages-shared.ts +6 -2
- package/src/server/native-final-log.ts +93 -0
- package/src/server/ollama-inbound.ts +349 -0
- package/src/server/ollama-native-paths.ts +21 -0
- package/src/server/ollama-route-policy.ts +73 -0
- package/src/server/relay.ts +22 -3
- package/src/server/request-decompress.ts +4 -2
- package/src/server/request-log-failure-attribution.ts +22 -0
- package/src/server/request-log-failure-detail.ts +80 -0
- package/src/server/request-log-terminal-status.ts +4 -2
- package/src/server/request-log.ts +79 -42
- package/src/server/response-log-body.ts +6 -1
- package/src/server/responses/account-change-state.ts +17 -6
- package/src/server/responses/adapter-continuation.ts +9 -4
- package/src/server/responses/adapter-delivery.ts +5 -4
- package/src/server/responses/adapter-dispatch.ts +36 -30
- package/src/server/responses/anthropic-thinking-recovery.ts +3 -0
- package/src/server/responses/codex-auth-error.ts +22 -17
- package/src/server/responses/codex-ws-exchange.ts +11 -8
- package/src/server/responses/codex-ws-metadata.ts +94 -21
- package/src/server/responses/codex-ws-request.ts +3 -1
- package/src/server/responses/combo-required-tool-contract.ts +24 -12
- package/src/server/responses/compact.ts +89 -41
- package/src/server/responses/compaction-recovery.ts +4 -2
- package/src/server/responses/compaction-routing.ts +9 -4
- package/src/server/responses/core-auth.ts +32 -14
- package/src/server/responses/core-codex-account.ts +34 -12
- package/src/server/responses/core-combo-failure.ts +5 -1
- package/src/server/responses/core-combo-native.ts +2 -1
- package/src/server/responses/core-combo.ts +51 -33
- package/src/server/responses/core-errors.ts +22 -20
- package/src/server/responses/core-lifetime.ts +6 -3
- package/src/server/responses/core-normalize.ts +8 -1
- package/src/server/responses/core-options.ts +7 -0
- package/src/server/responses/core-pipeline.ts +4 -3
- package/src/server/responses/fetch-helpers.ts +7 -1
- package/src/server/responses/input-admission.ts +28 -2
- package/src/server/responses/native-injection.ts +1 -1
- package/src/server/responses/native-response-json.ts +13 -4
- package/src/server/responses/native-response-output.ts +1 -2
- package/src/server/responses/non-replayable-error.ts +2 -1
- package/src/server/responses/pacing-overload.ts +3 -2
- package/src/server/responses/passthrough-build-error.ts +36 -0
- package/src/server/responses/passthrough-delivery.ts +55 -14
- package/src/server/responses/passthrough-dispatch.ts +26 -39
- package/src/server/responses/passthrough-error.ts +9 -9
- package/src/server/responses/policy-fallback.ts +2 -1
- package/src/server/responses/request-prepare.ts +67 -48
- package/src/server/responses/request-sidecar-auth.ts +3 -1
- package/src/server/responses/request-transport.ts +35 -31
- package/src/server/responses/run-turn-execution.ts +6 -6
- package/src/server/responses/sidecar-execution.ts +16 -12
- package/src/server/responses/spend-storage-error.ts +2 -1
- package/src/server/responses/ws-upstream.ts +9 -0
- package/src/server/responses-custom-tool-repair.ts +14 -3
- package/src/server/responses-terminal-repair.ts +5 -2
- package/src/server/responses-undeclared-tool-guard.ts +39 -16
- package/src/server/safe-response-headers.ts +162 -13
- package/src/server/search.ts +13 -4
- package/src/server/turn-state-handles.ts +164 -0
- package/src/server/workflow-refusal.ts +2 -1
- package/src/server/ws-bridge.ts +5 -2
- package/src/service/ownership-mutation-lease.d.mts +11 -2
- package/src/service/ownership-mutation-lease.mjs +53 -11
- package/src/types/config.ts +2 -0
- package/src/update/index.ts +133 -25
- package/src/update/npm-install.d.mts +8 -0
- package/src/update/npm-install.mjs +20 -1
- package/src/update/orchestrated-admission.d.mts +4 -0
- package/src/update/orchestrated-admission.mjs +12 -0
- package/src/update/orchestrated-arguments.d.mts +12 -0
- package/src/update/orchestrated-arguments.mjs +64 -0
- package/src/update/orchestrated-child.d.mts +12 -0
- package/src/update/orchestrated-child.mjs +70 -0
- package/src/update/orchestrated-entry.ts +234 -0
- package/src/update/orchestrated-identity-probe-entry.ts +20 -0
- package/src/update/orchestrated-identity.ts +53 -0
- package/src/update/orchestrated-live-identity.ts +48 -0
- package/src/update/orchestrated-node-driver.mjs +99 -0
- package/src/update/orchestrated-outcome.d.mts +21 -0
- package/src/update/orchestrated-outcome.mjs +40 -0
- package/src/update/orchestrated-plan.ts +49 -0
- package/src/update/orchestrated-pnpm.d.mts +8 -0
- package/src/update/orchestrated-pnpm.mjs +33 -0
- package/src/update/orchestrated-session.d.mts +37 -0
- package/src/update/orchestrated-session.mjs +264 -0
- package/src/update/orchestrated-token.d.mts +10 -0
- package/src/update/orchestrated-token.mjs +21 -0
- package/src/update/orchestrated-token.ts +3 -0
- package/src/update/orchestrated-tree.d.mts +1 -0
- package/src/update/orchestrated-tree.mjs +14 -0
- package/src/update/pnpm-global-install.d.mts +6 -0
- package/src/update/pnpm-global-install.mjs +9 -2
- package/src/update/posix-install-supervisor-entry.mjs +6 -0
- package/src/update/posix-install-supervisor.d.mts +10 -0
- package/src/update/posix-install-supervisor.mjs +88 -0
- package/src/update/sync-update-mutex.d.mts +10 -0
- package/src/update/sync-update-mutex.mjs +164 -0
- package/src/update/sync-update-mutex.ts +1 -0
- package/src/update/test-home-guard.mjs +33 -0
- package/src/update/transactional-install.mjs +2 -2
- package/src/usage/error-detail.ts +225 -0
- package/src/usage/log.ts +60 -27
- package/src/vision/describe.ts +4 -7
- package/src/web-search/executor.ts +4 -7
- package/src/web-search/loop.ts +31 -5
- package/gui/dist/assets/index-D7DCm5eQ.js +0 -94
|
@@ -6,10 +6,13 @@ import type { CodexThreadLineage } from "../lineage";
|
|
|
6
6
|
import type { CodexQuotaScope } from "./health-store";
|
|
7
7
|
import type { TransientProbeLease } from "../../routing/probe-lease";
|
|
8
8
|
import { clearPoolRecoveryState } from "../../routing/probe-lease";
|
|
9
|
+
import { identityFence, adoptStoredThreadRows, adoptStoredIssuer, CODEX_UNKNOWN_CONVERSATION_STATE_ISSUER, registerCodexAffinityLiveState, recordThreadBind, recordThreadTouch, recordThreadDelete, recordThreadAccountDeleted, recordAccountPinsCleared, recordAllCleared, recordThreadsCleared, recordIssuerSet, recordIssuerTouch, recordIssuerDelete, recordIssuersCleared } from "./thread-affinity-store";
|
|
10
|
+
export { CODEX_UNKNOWN_CONVERSATION_STATE_ISSUER } from "./thread-affinity-store";
|
|
9
11
|
|
|
10
12
|
export type ThreadAffinityEntry = {
|
|
11
13
|
accountId: string;
|
|
12
14
|
generation: number;
|
|
15
|
+
identityFence?: string;
|
|
13
16
|
createdAt: number;
|
|
14
17
|
lastUsedAt: number;
|
|
15
18
|
// Last time the bound account's quota threshold was re-evaluated for this
|
|
@@ -86,7 +89,9 @@ export type CodexAffinityMove =
|
|
|
86
89
|
/** There was no live binding; this request established one. */
|
|
87
90
|
| "new_bind"
|
|
88
91
|
/** The binding was released without a replacement on this request. */
|
|
89
|
-
| "cleared"
|
|
92
|
+
| "cleared"
|
|
93
|
+
/** No binding was established for this selection. */
|
|
94
|
+
| "unbound";
|
|
90
95
|
|
|
91
96
|
/**
|
|
92
97
|
* Why. A move is the expensive event -- it discards the prompt-cache prefix warmed on the old
|
|
@@ -95,6 +100,10 @@ export type CodexAffinityMove =
|
|
|
95
100
|
*/
|
|
96
101
|
export type CodexAffinityReason =
|
|
97
102
|
| "healthy"
|
|
103
|
+
/** The request had no usable conversation key. */
|
|
104
|
+
| "no_conversation"
|
|
105
|
+
/** A caller-owned credential did not opt into affinity. */
|
|
106
|
+
| "request_owned"
|
|
98
107
|
| "quota_headroom"
|
|
99
108
|
| "quota_refusal"
|
|
100
109
|
| "transient"
|
|
@@ -194,15 +203,32 @@ let threadAffinityEntryTotal = 0;
|
|
|
194
203
|
* Which pool account minted the conversation's carried OpenAI state
|
|
195
204
|
* (`previous_response_id`, encrypted reasoning, provider conversation/file ids).
|
|
196
205
|
* Keyed by the same affinity key as {@link threadAccountMap}, bounded the same
|
|
197
|
-
* way
|
|
206
|
+
* way. Durable rows use a separate keyed digest; raw account ids never reach a log.
|
|
198
207
|
*/
|
|
199
208
|
type ConversationStateIssuerEntry = {
|
|
200
209
|
accountId: string;
|
|
201
210
|
lastUsedAt: number;
|
|
211
|
+
fence?: string;
|
|
212
|
+
storedAccountId?: string;
|
|
213
|
+
storedFence?: string | null;
|
|
202
214
|
};
|
|
203
215
|
const conversationStateIssuerMap = new Map<string, ConversationStateIssuerEntry>();
|
|
204
216
|
|
|
205
|
-
export function
|
|
217
|
+
export function registerThreadAffinityLiveState(): void {
|
|
218
|
+
registerCodexAffinityLiveState({
|
|
219
|
+
thread: (key, scope) => threadAccountMap.get(key)?.get(scope as ThreadAffinityScope),
|
|
220
|
+
issuer: key => {
|
|
221
|
+
const entry = conversationStateIssuerMap.get(key);
|
|
222
|
+
return entry && { accountId: entry.storedAccountId ?? entry.accountId, fence: entry.storedAccountId ? entry.storedFence ?? undefined : entry.fence, lastUsedAt: entry.lastUsedAt };
|
|
223
|
+
},
|
|
224
|
+
clearLive: () => { threadAccountMap.clear(); threadAffinityEntryTotal = 0; conversationStateIssuerMap.clear(); },
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
registerThreadAffinityLiveState();
|
|
228
|
+
|
|
229
|
+
export function clearThreadAccountMap(options: { preserveConversationStateIssuers?: boolean } = {}): void {
|
|
230
|
+
if (options.preserveConversationStateIssuers) recordThreadsCleared();
|
|
231
|
+
else recordAllCleared();
|
|
206
232
|
threadAccountMap.clear();
|
|
207
233
|
threadAffinityEntryTotal = 0;
|
|
208
234
|
// A refresh cooldown is per-account runtime state learned alongside these bindings. Leaving it
|
|
@@ -213,10 +239,13 @@ export function clearThreadAccountMap(): void {
|
|
|
213
239
|
// going away. A held account nobody may probe because of a lease issued against the previous
|
|
214
240
|
// roster is a recovery that never starts.
|
|
215
241
|
clearPoolRecoveryState();
|
|
216
|
-
|
|
242
|
+
// A manual account switch clears routing affinity, not the issuer of carried state. The next
|
|
243
|
+
// account needs that record to scrub or refuse account-bound continuation.
|
|
244
|
+
if (!options.preserveConversationStateIssuers) conversationStateIssuerMap.clear();
|
|
217
245
|
}
|
|
218
246
|
|
|
219
247
|
export function clearConversationStateIssuerMap(): void {
|
|
248
|
+
recordIssuersCleared();
|
|
220
249
|
conversationStateIssuerMap.clear();
|
|
221
250
|
}
|
|
222
251
|
|
|
@@ -224,6 +253,7 @@ export function clearThreadAccountMapForAccount(
|
|
|
224
253
|
accountId: string,
|
|
225
254
|
reason: CodexAffinityReason = "unusable",
|
|
226
255
|
): void {
|
|
256
|
+
recordAccountPinsCleared(accountId);
|
|
227
257
|
for (const [threadId, affinities] of threadAccountMap) {
|
|
228
258
|
for (const [scope, entry] of affinities) {
|
|
229
259
|
if (entry.accountId === accountId && affinities.delete(scope)) {
|
|
@@ -239,6 +269,7 @@ function pruneConversationStateIssuers(now: number): void {
|
|
|
239
269
|
for (const [key, entry] of conversationStateIssuerMap) {
|
|
240
270
|
if (now - entry.lastUsedAt > CODEX_THREAD_AFFINITY_IDLE_TTL_MS) {
|
|
241
271
|
conversationStateIssuerMap.delete(key);
|
|
272
|
+
recordIssuerDelete(key);
|
|
242
273
|
}
|
|
243
274
|
}
|
|
244
275
|
while (conversationStateIssuerMap.size > CODEX_THREAD_AFFINITY_MAX_ENTRIES) {
|
|
@@ -252,22 +283,30 @@ function pruneConversationStateIssuers(now: number): void {
|
|
|
252
283
|
}
|
|
253
284
|
if (!oldestKey) break;
|
|
254
285
|
conversationStateIssuerMap.delete(oldestKey);
|
|
286
|
+
recordIssuerDelete(oldestKey);
|
|
255
287
|
}
|
|
256
288
|
}
|
|
257
289
|
|
|
258
290
|
/**
|
|
259
291
|
* Record the pool account that just issued carried conversation state for this
|
|
260
|
-
* binding key.
|
|
292
|
+
* binding key. Persisted when an anchor is available; the id is never logged.
|
|
261
293
|
*/
|
|
262
294
|
export function rememberConversationStateIssuer(
|
|
263
295
|
bindingKey: string,
|
|
264
296
|
accountId: string,
|
|
265
297
|
now = Date.now(),
|
|
298
|
+
credential?: { generation: number; chatgptAccountId: string },
|
|
266
299
|
): void {
|
|
267
300
|
if (!bindingKey.trim() || !accountId.trim()) return;
|
|
268
301
|
if (!admissibleAffinityComponent(bindingKey) || !admissibleAffinityComponent(accountId)) return;
|
|
269
302
|
pruneConversationStateIssuers(now);
|
|
270
|
-
|
|
303
|
+
const record = accountId === MAIN_CODEX_ACCOUNT_ID ? undefined : readCodexAccountRecord(accountId);
|
|
304
|
+
const fence = record?.credential && record.deletedAt == null && credential
|
|
305
|
+
&& record.generation === credential.generation && record.credential.chatgptAccountId === credential.chatgptAccountId
|
|
306
|
+
? identityFence(record.quotaHistoryIdentity) : undefined;
|
|
307
|
+
if (accountId === MAIN_CODEX_ACCOUNT_ID) recordIssuerDelete(bindingKey);
|
|
308
|
+
else if (accountId !== CODEX_UNKNOWN_CONVERSATION_STATE_ISSUER) recordIssuerSet(bindingKey, accountId, fence ?? null, now);
|
|
309
|
+
conversationStateIssuerMap.set(bindingKey, { accountId, lastUsedAt: now, fence });
|
|
271
310
|
pruneConversationStateIssuers(now);
|
|
272
311
|
}
|
|
273
312
|
|
|
@@ -278,12 +317,25 @@ export function peekConversationStateIssuer(
|
|
|
278
317
|
): string | undefined {
|
|
279
318
|
if (!bindingKey.trim() || !admissibleAffinityComponent(bindingKey)) return undefined;
|
|
280
319
|
pruneConversationStateIssuers(now);
|
|
281
|
-
|
|
320
|
+
recordIssuerTouch(bindingKey);
|
|
321
|
+
let entry = conversationStateIssuerMap.get(bindingKey);
|
|
322
|
+
if (!entry) {
|
|
323
|
+
const stored = adoptStoredIssuer(bindingKey, now);
|
|
324
|
+
if (stored !== undefined) {
|
|
325
|
+
entry = { accountId: stored.issuer, lastUsedAt: now, storedAccountId: stored.storedAccountId, storedFence: stored.storedFence };
|
|
326
|
+
conversationStateIssuerMap.set(bindingKey, entry);
|
|
327
|
+
pruneConversationStateIssuers(now);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
282
330
|
if (!entry) return undefined;
|
|
283
331
|
entry.lastUsedAt = now;
|
|
284
332
|
return entry.accountId;
|
|
285
333
|
}
|
|
286
334
|
|
|
335
|
+
export function peekConversationStateStoredIssuerAccount(bindingKey: string): string | undefined {
|
|
336
|
+
return conversationStateIssuerMap.get(bindingKey)?.storedAccountId;
|
|
337
|
+
}
|
|
338
|
+
|
|
287
339
|
/**
|
|
288
340
|
* Why a binding was released, held until that thread's next resolve can report it (#4546).
|
|
289
341
|
*
|
|
@@ -294,7 +346,7 @@ export function peekConversationStateIssuer(
|
|
|
294
346
|
const pendingReleaseReasons = new Map<string, CodexAffinityReason>();
|
|
295
347
|
const MAX_PENDING_RELEASE_REASONS = 4096;
|
|
296
348
|
|
|
297
|
-
function notePendingReleaseReason(threadId: string | null, reason: CodexAffinityReason): void {
|
|
349
|
+
export function notePendingReleaseReason(threadId: string | null, reason: CodexAffinityReason): void {
|
|
298
350
|
if (threadId === null) return;
|
|
299
351
|
if (!pendingReleaseReasons.has(threadId) && pendingReleaseReasons.size >= MAX_PENDING_RELEASE_REASONS) {
|
|
300
352
|
const oldest = pendingReleaseReasons.keys().next();
|
|
@@ -341,9 +393,25 @@ function getThreadAffinityForScope(
|
|
|
341
393
|
scope: ThreadAffinityScope,
|
|
342
394
|
): ThreadAffinityEntry | undefined {
|
|
343
395
|
if (!admissibleAffinityComponent(threadId)) return undefined;
|
|
396
|
+
recordThreadTouch(threadId, scope);
|
|
397
|
+
if (!threadAccountMap.get(threadId)?.get(scope)) adoptThreadAffinities(threadId, Date.now(), scope);
|
|
344
398
|
return threadAccountMap.get(threadId)?.get(scope);
|
|
345
399
|
}
|
|
346
400
|
|
|
401
|
+
function adoptThreadAffinities(threadId: string, now: number, scopeName?: ThreadAffinityScope): void {
|
|
402
|
+
for (const row of adoptStoredThreadRows(threadId, now, scopeName)) {
|
|
403
|
+
const record = readCodexAccountRecord(row.accountId);
|
|
404
|
+
if (!record?.credential || record.deletedAt != null || identityFence(record.quotaHistoryIdentity) !== row.fence) continue;
|
|
405
|
+
const scope = row.scope as ThreadAffinityScope;
|
|
406
|
+
const affinities = threadAccountMap.get(threadId) ?? new Map<ThreadAffinityScope, ThreadAffinityEntry>();
|
|
407
|
+
if (affinities.has(scope)) continue;
|
|
408
|
+
affinities.set(scope, { accountId: row.accountId, generation: record.generation, identityFence: row.fence, createdAt: row.createdAt, lastUsedAt: row.lastUsedAt, lastReevalAt: now });
|
|
409
|
+
threadAccountMap.set(threadId, affinities);
|
|
410
|
+
threadAffinityEntryTotal += 1;
|
|
411
|
+
}
|
|
412
|
+
pruneLruThreadAffinities();
|
|
413
|
+
}
|
|
414
|
+
|
|
347
415
|
export function getThreadAffinity(threadId: string, quotaScope?: CodexQuotaScope): ThreadAffinityEntry | undefined {
|
|
348
416
|
return getThreadAffinityForScope(threadId, threadAffinityScope(quotaScope));
|
|
349
417
|
}
|
|
@@ -359,6 +427,7 @@ export function getModelDetourAffinity(
|
|
|
359
427
|
|
|
360
428
|
function deleteThreadAffinityForScope(threadId: string, scope: ThreadAffinityScope): void {
|
|
361
429
|
if (!admissibleAffinityComponent(threadId)) return;
|
|
430
|
+
recordThreadDelete(threadId, scope);
|
|
362
431
|
const affinities = threadAccountMap.get(threadId);
|
|
363
432
|
if (!affinities) return;
|
|
364
433
|
if (affinities.delete(scope)) {
|
|
@@ -383,10 +452,12 @@ export function deleteModelDetourAffinity(
|
|
|
383
452
|
/** Remove only the matching failed account's affinities for one thread. */
|
|
384
453
|
export function deleteThreadAffinitiesForAccount(threadId: string, accountId: string): void {
|
|
385
454
|
if (!admissibleAffinityComponent(threadId) || !admissibleAffinityComponent(accountId)) return;
|
|
455
|
+
recordThreadAccountDeleted(threadId, accountId);
|
|
386
456
|
const affinities = threadAccountMap.get(threadId);
|
|
387
457
|
if (!affinities) return;
|
|
388
458
|
for (const [scope, entry] of affinities) {
|
|
389
459
|
if (entry.accountId === accountId && affinities.delete(scope)) {
|
|
460
|
+
recordThreadDelete(threadId, scope);
|
|
390
461
|
threadAffinityEntryTotal = Math.max(0, threadAffinityEntryTotal - 1);
|
|
391
462
|
}
|
|
392
463
|
}
|
|
@@ -459,6 +530,7 @@ function pruneExpiredThreadAffinities(now: number): void {
|
|
|
459
530
|
for (const [threadId, affinities] of threadAccountMap) {
|
|
460
531
|
for (const [scope, entry] of affinities) {
|
|
461
532
|
if (isThreadAffinityExpired(entry, now) && affinities.delete(scope)) {
|
|
533
|
+
recordThreadDelete(threadId, scope);
|
|
462
534
|
threadAffinityEntryTotal = Math.max(0, threadAffinityEntryTotal - 1);
|
|
463
535
|
}
|
|
464
536
|
}
|
|
@@ -502,15 +574,21 @@ function bindThreadAffinityForScope(
|
|
|
502
574
|
const record = accountId === MAIN_CODEX_ACCOUNT_ID ? undefined : readCodexAccountRecord(accountId);
|
|
503
575
|
if (accountId !== MAIN_CODEX_ACCOUNT_ID && (!record?.credential || record.deletedAt != null)) return;
|
|
504
576
|
pruneExpiredThreadAffinities(now);
|
|
577
|
+
recordThreadTouch(threadId, scope);
|
|
505
578
|
const affinities = threadAccountMap.get(threadId) ?? new Map<ThreadAffinityScope, ThreadAffinityEntry>();
|
|
506
579
|
const previous = affinities.get(scope);
|
|
507
|
-
|
|
580
|
+
const fence = identityFence(record?.quotaHistoryIdentity);
|
|
581
|
+
const entry: ThreadAffinityEntry = {
|
|
508
582
|
accountId,
|
|
509
583
|
generation: accountId === MAIN_CODEX_ACCOUNT_ID ? 0 : record!.generation,
|
|
584
|
+
identityFence: fence,
|
|
510
585
|
createdAt: previous?.createdAt ?? now,
|
|
511
586
|
lastUsedAt: now,
|
|
512
587
|
lastReevalAt: now,
|
|
513
|
-
}
|
|
588
|
+
};
|
|
589
|
+
if (fence) recordThreadBind(threadId, scope, { scope, accountId, fence, createdAt: entry.createdAt, lastUsedAt: now });
|
|
590
|
+
else recordThreadDelete(threadId, scope);
|
|
591
|
+
affinities.set(scope, entry);
|
|
514
592
|
if (!previous) threadAffinityEntryTotal += 1;
|
|
515
593
|
threadAccountMap.set(threadId, affinities);
|
|
516
594
|
pruneLruThreadAffinities();
|
|
@@ -540,14 +618,14 @@ export function bindModelDetourAffinity(
|
|
|
540
618
|
export function getThreadAffinityScopes(
|
|
541
619
|
threadId: string,
|
|
542
620
|
): ReadonlyMap<ThreadAffinityScope, ThreadAffinityEntry> | undefined {
|
|
621
|
+
adoptThreadAffinities(threadId, Date.now());
|
|
543
622
|
return threadAccountMap.get(threadId);
|
|
544
623
|
}
|
|
545
624
|
|
|
546
625
|
/**
|
|
547
626
|
* Move one scope's binding from the pre-#4546 RAW parent key onto the key this thread uses now.
|
|
548
627
|
*
|
|
549
|
-
*
|
|
550
|
-
* discards every binding and there is nothing to migrate. The case this exists for is the
|
|
628
|
+
* Legacy raw-parent keys are process-local and are not persisted. The case this exists for is the
|
|
551
629
|
* narrow one: a code swap under a live conversation, where the map still holds entries made by
|
|
552
630
|
* the old rule. Rebinding those cold is precisely the defect the lineage work exists to prevent,
|
|
553
631
|
* so the conversation keeps its account and the legacy entry is retired in the same step.
|
|
@@ -84,22 +84,73 @@ export function sourceAssignmentSpan(lines: readonly SourceLine[], start: number
|
|
|
84
84
|
const decoded = sourceAssignment(lines, start, limit);
|
|
85
85
|
if (decoded) return decoded;
|
|
86
86
|
const span = lexicalAssignmentSpan(lines, start, limit);
|
|
87
|
-
|
|
88
|
-
//
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (integer < -(1n << 63n) || integer >= (1n << 63n)) return null;
|
|
95
|
-
// Keep the parser responsible for the key, separator and comment grammar. Replacing
|
|
96
|
-
// this already validated integer for validation never edits or decodes the source value.
|
|
87
|
+
if (!span) return null;
|
|
88
|
+
// Bun rejects valid TOML integers outside the JS safe-integer range, as scalars and
|
|
89
|
+
// inside arrays or inline tables alike. Validate a copy in which each such integer that
|
|
90
|
+
// fits signed 64 bits reads `0`; the parser stays responsible for every other piece of
|
|
91
|
+
// grammar, and the returned span keeps the original bytes untouched.
|
|
92
|
+
const masked = maskWideIntegers(span.text);
|
|
93
|
+
if (masked === null) return null;
|
|
97
94
|
try {
|
|
98
|
-
const parsed = Bun.TOML.parse(
|
|
95
|
+
const parsed = Bun.TOML.parse(masked);
|
|
99
96
|
return Object.keys(parsed).length === 1 && Object.hasOwn(parsed, span.key) ? span : null;
|
|
100
97
|
} catch { return null; }
|
|
101
98
|
}
|
|
102
99
|
|
|
100
|
+
const integerToken = /^(?:[+-]?(?:0|[1-9](?:_?[0-9])*)|0x[0-9A-Fa-f](?:_?[0-9A-Fa-f])*|0o[0-7](?:_?[0-7])*|0b[01](?:_?[01])*)$/;
|
|
101
|
+
const bareToken = /[A-Za-z0-9_+.:-]/;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The assignment with every integer outside the JS safe range replaced by `0`, skipping
|
|
105
|
+
* strings and comments. Null when an integer does not fit signed 64 bits (TOML requires a
|
|
106
|
+
* reader to reject it) or when nothing needed masking, so the original parse failure stands.
|
|
107
|
+
*/
|
|
108
|
+
function maskWideIntegers(text: string): string | null {
|
|
109
|
+
let out = "";
|
|
110
|
+
let masked = false;
|
|
111
|
+
let index = 0;
|
|
112
|
+
const copyUntil = (end: number) => { out += text.slice(index, end); index = end; };
|
|
113
|
+
while (index < text.length) {
|
|
114
|
+
const char = text[index]!;
|
|
115
|
+
if (text.startsWith('"""', index) || text.startsWith("'''", index)) {
|
|
116
|
+
const delimiter = text.slice(index, index + 3);
|
|
117
|
+
let cursor = index + 3;
|
|
118
|
+
while (cursor < text.length && !text.startsWith(delimiter, cursor)) cursor += delimiter === '"""' && text[cursor] === "\\" ? 2 : 1;
|
|
119
|
+
cursor = Math.min(text.length, cursor + 3);
|
|
120
|
+
// A closing delimiter may carry up to two more quote characters of content.
|
|
121
|
+
for (let extra = 0; extra < 2 && text[cursor] === delimiter[0]; extra += 1) cursor += 1;
|
|
122
|
+
copyUntil(cursor);
|
|
123
|
+
} else if (char === '"' || char === "'") {
|
|
124
|
+
let cursor = index + 1;
|
|
125
|
+
while (cursor < text.length && text[cursor] !== char && text[cursor] !== "\n") cursor += char === '"' && text[cursor] === "\\" ? 2 : 1;
|
|
126
|
+
copyUntil(Math.min(text.length, cursor + 1));
|
|
127
|
+
} else if (char === "#") {
|
|
128
|
+
const newline = text.indexOf("\n", index);
|
|
129
|
+
copyUntil(newline === -1 ? text.length : newline);
|
|
130
|
+
} else if (bareToken.test(char)) {
|
|
131
|
+
let cursor = index + 1;
|
|
132
|
+
while (cursor < text.length && bareToken.test(text[cursor]!)) cursor += 1;
|
|
133
|
+
const token = text.slice(index, cursor);
|
|
134
|
+
// A digit-only bare key (`{ 9223372036854775808 = 1 }`) is a name, not a number.
|
|
135
|
+
const isKey = /^[ \t]*[=.]/.test(text.slice(cursor));
|
|
136
|
+
if (!isKey && integerToken.test(token)) {
|
|
137
|
+
const integer = BigInt(token.replaceAll("_", ""));
|
|
138
|
+
if (integer < -(1n << 63n) || integer >= (1n << 63n)) return null;
|
|
139
|
+
if (integer > BigInt(Number.MAX_SAFE_INTEGER) || integer < -BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
140
|
+
out += "0";
|
|
141
|
+
index = cursor;
|
|
142
|
+
masked = true;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
copyUntil(cursor);
|
|
147
|
+
} else {
|
|
148
|
+
copyUntil(index + 1);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return masked ? out : null;
|
|
152
|
+
}
|
|
153
|
+
|
|
103
154
|
/** Cosmetic document whitespace only; opaque value lines retain every byte. */
|
|
104
155
|
export function normalizeStructuralWhitespace(content: string): string {
|
|
105
156
|
const { bom, lines } = rootSourceLines(content);
|
|
@@ -122,6 +173,13 @@ export function normalizeStructuralWhitespace(content: string): string {
|
|
|
122
173
|
|
|
123
174
|
type MultilineStringKind = "basic" | "literal" | null;
|
|
124
175
|
|
|
176
|
+
/** Length of a closing multiline delimiter: TOML allows a run of three to five quotes. */
|
|
177
|
+
function quoteRunLength(text: string, index: number, quote: string): number {
|
|
178
|
+
let end = index;
|
|
179
|
+
while (end < text.length && text[end] === quote && end - index < 5) end += 1;
|
|
180
|
+
return end - index;
|
|
181
|
+
}
|
|
182
|
+
|
|
125
183
|
/**
|
|
126
184
|
* TOML table-looking text inside a multiline string is data, not syntax. Keep
|
|
127
185
|
* a deliberately small lexical state machine so the format-preserving editor
|
|
@@ -139,8 +197,9 @@ function markStructuralLines(lines: SourceLine[]): void {
|
|
|
139
197
|
for (let index = 0; index < line.text.length;) {
|
|
140
198
|
if (multiline === "basic") {
|
|
141
199
|
if (line.text.startsWith('"""', index)) {
|
|
200
|
+
// A closing delimiter may carry up to two content quotes before it.
|
|
142
201
|
multiline = null;
|
|
143
|
-
index +=
|
|
202
|
+
index += quoteRunLength(line.text, index, '"');
|
|
144
203
|
} else if (line.text[index] === "\\") {
|
|
145
204
|
index += 2;
|
|
146
205
|
} else {
|
|
@@ -151,7 +210,7 @@ function markStructuralLines(lines: SourceLine[]): void {
|
|
|
151
210
|
if (multiline === "literal") {
|
|
152
211
|
if (line.text.startsWith("'''", index)) {
|
|
153
212
|
multiline = null;
|
|
154
|
-
index +=
|
|
213
|
+
index += quoteRunLength(line.text, index, "'");
|
|
155
214
|
} else {
|
|
156
215
|
index += 1;
|
|
157
216
|
}
|
package/src/combos/failover.ts
CHANGED
|
@@ -5,6 +5,10 @@ import { isAnthropicBillingFailure } from "../providers/anthropic-billing-refusa
|
|
|
5
5
|
import { isKiroAccountProfileRefusal } from "../adapters/kiro-refusal";
|
|
6
6
|
import type { OcxComboTarget } from "../types";
|
|
7
7
|
import { targetKey } from "./types";
|
|
8
|
+
import { isDefiniteContextOverflow } from "../lib/context-overflow-verdict";
|
|
9
|
+
|
|
10
|
+
// The verdict lives in a dependency-free leaf so the presentation classifier can share it.
|
|
11
|
+
export { isDefiniteContextOverflow };
|
|
8
12
|
import {
|
|
9
13
|
captureConfigGeneration,
|
|
10
14
|
sweepExpiredOnWrite,
|
|
@@ -433,6 +437,7 @@ function isProviderScopedQuotaCap(
|
|
|
433
437
|
message: string,
|
|
434
438
|
code?: string | null,
|
|
435
439
|
): boolean {
|
|
440
|
+
if (status === 400 || status === 422) return false; // Validation prose may reflect the caller's input.
|
|
436
441
|
const normalizedCode = normalizedFailureCode(code);
|
|
437
442
|
const text = message.toLowerCase();
|
|
438
443
|
if (
|
|
@@ -463,7 +468,7 @@ function isRequestLocalFreePromptCap(
|
|
|
463
468
|
message: string,
|
|
464
469
|
code?: string | null,
|
|
465
470
|
): boolean {
|
|
466
|
-
if (status !== 400) return false;
|
|
471
|
+
if (status !== 400 && status !== 422) return false;
|
|
467
472
|
const text = message.toLowerCase();
|
|
468
473
|
if (normalizedFailureCode(code) === "free_rate_limited") return true;
|
|
469
474
|
if (text.includes("err_free_prompt_cap")) return true;
|
|
@@ -704,114 +709,6 @@ function isProviderTargetContextOverflow(
|
|
|
704
709
|
&& /\bprompt\s+\d+\s*>\s*\d+\s+maximum context length\b/i.test(message);
|
|
705
710
|
}
|
|
706
711
|
|
|
707
|
-
/** A status can carry a verdict about the REQUEST; 401/403/429 speak about the credential. */
|
|
708
|
-
const CONTEXT_VERDICT_STATUSES: ReadonlySet<number> = new Set([400, 413, 422]);
|
|
709
|
-
|
|
710
|
-
/**
|
|
711
|
-
* Phrases a provider emits when the INPUT does not fit this model's context window. Matched
|
|
712
|
-
* against the innermost provider message only, so an unrelated refusal that merely quotes one
|
|
713
|
-
* of these tokens in a code field cannot authorize a replay.
|
|
714
|
-
*/
|
|
715
|
-
const DEFINITE_CONTEXT_OVERFLOW_PHRASES = [
|
|
716
|
-
"exceeds the context window",
|
|
717
|
-
"exceed the context window",
|
|
718
|
-
"context window exceeded",
|
|
719
|
-
"context length exceeded",
|
|
720
|
-
"maximum context length",
|
|
721
|
-
"maximum context window",
|
|
722
|
-
"too many tokens",
|
|
723
|
-
];
|
|
724
|
-
|
|
725
|
-
/**
|
|
726
|
-
* Short provider verdicts that a validation error could otherwise echo as the caller's own value
|
|
727
|
-
* (#7443). Each counts only at the very start of the provider's leaf message, after JSON envelopes
|
|
728
|
-
* are unwrapped, and Anthropic's only with its token counts. Text that merely contains one, including
|
|
729
|
-
* a serialized SDK exception or a quoted caller object, is not evidence: only the phrases dev already
|
|
730
|
-
* matched as substrings keep that wider reach.
|
|
731
|
-
*/
|
|
732
|
-
const ANCHORED_CONTEXT_OVERFLOW_VERDICTS: readonly RegExp[] = [
|
|
733
|
-
/^prompt is too long: \d+ tokens > \d+ maximum/i,
|
|
734
|
-
/^input is too long for requested model\b/i,
|
|
735
|
-
/^please reduce the length of the messages or completion\b/i,
|
|
736
|
-
];
|
|
737
|
-
|
|
738
|
-
/** Wrapper envelopes unwrapped before the leaf message is read. */
|
|
739
|
-
const MAX_CONTEXT_OVERFLOW_ENVELOPES = 4;
|
|
740
|
-
|
|
741
|
-
/**
|
|
742
|
-
* Parameter validation echoes the caller's own value, so its prose is not a context verdict even
|
|
743
|
-
* when the value reads like one: "Invalid 'tools[0].function.name': 'prompt is too long'". These
|
|
744
|
-
* markers are read anywhere in the text, so an SDK error wrapper that serializes such a record as a
|
|
745
|
-
* Python dict is refused too, while a quoted genuine verdict in the same wrapper stays evidence.
|
|
746
|
-
*/
|
|
747
|
-
const CONTEXT_PARAM_NAMES = "messages|input|input_text|input_tokens|prompt|contents?|max_tokens|max_completion_tokens|max_output_tokens";
|
|
748
|
-
/** A `param` naming the conversation or a token budget is consistent with an overflow. */
|
|
749
|
-
const CONTEXT_PARAM = new RegExp(`^(?:${CONTEXT_PARAM_NAMES})(?:$|[.[])`, "i");
|
|
750
|
-
const PARAMETER_VALIDATION_CODES: ReadonlySet<string> = new Set(["invalid_value", "invalid_type"]);
|
|
751
|
-
const PARAMETER_VALIDATION_MARKERS: readonly RegExp[] = [
|
|
752
|
-
/\binvalid_(?:value|type)\b/i,
|
|
753
|
-
new RegExp(`['"]param['"]\\s*:\\s*['"](?!(?:${CONTEXT_PARAM_NAMES})(?:['".[]))`, "i"),
|
|
754
|
-
/\binvalid\s+(?:value\s+for\s+)?['"`\u2018\u201c]/i,
|
|
755
|
-
/\binvalid\s+(?:tool|function|parameter|argument|field|value)\b/i,
|
|
756
|
-
/\binput_value\s*=/i,
|
|
757
|
-
/\bvalidation errors? for\b/i,
|
|
758
|
-
];
|
|
759
|
-
|
|
760
|
-
function isParameterValidationRecord(record: Record<string, unknown>): boolean {
|
|
761
|
-
if (typeof record.code === "string" && PARAMETER_VALIDATION_CODES.has(record.code.toLowerCase())) return true;
|
|
762
|
-
return typeof record.param === "string" && record.param.length > 0 && !CONTEXT_PARAM.test(record.param);
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
function isDefiniteContextOverflowMessage(text: string): boolean {
|
|
766
|
-
if (PARAMETER_VALIDATION_MARKERS.some(marker => marker.test(text))) return false;
|
|
767
|
-
const normalized = text.toLowerCase();
|
|
768
|
-
return normalized === "context_length_exceeded"
|
|
769
|
-
|| DEFINITE_CONTEXT_OVERFLOW_PHRASES.some(phrase => normalized.includes(phrase))
|
|
770
|
-
|| ANCHORED_CONTEXT_OVERFLOW_VERDICTS.some(verdict => verdict.test(text.trim()));
|
|
771
|
-
}
|
|
772
|
-
|
|
773
|
-
/**
|
|
774
|
-
* Confirm a context overflow from the provider MESSAGE rather than from a code token that
|
|
775
|
-
* merely appears somewhere in the envelope. An upstream controls both fields and can emit a
|
|
776
|
-
* contradictory pair -- `context_length_exceeded` beside `Unsupported parameter: user` -- and
|
|
777
|
-
* that is not evidence the turn is too large for this model. `classifyError` reads the whole
|
|
778
|
-
* blob, which is exactly the looseness this must not inherit.
|
|
779
|
-
*
|
|
780
|
-
* A JSON-shaped body that fails to parse is truncated or corrupt, not prose: `classificationText`
|
|
781
|
-
* is capped at 500 characters by `normalizeUpstreamErrorText` before it reaches this function, so
|
|
782
|
-
* a long envelope arrives here as a JSON prefix. Reading that prefix as plain text would let an
|
|
783
|
-
* arbitrary field that happens to sit in the first 500 bytes authorize a hop, so it fails closed.
|
|
784
|
-
*
|
|
785
|
-
* Only the exact proxy wrapper is unwrapped, within a fixed envelope budget and 16,384 characters.
|
|
786
|
-
* Exported for the sidecar loops, which classify their own provider failures with the same rule.
|
|
787
|
-
*/
|
|
788
|
-
export function isDefiniteContextOverflow(status: number, message: string): boolean {
|
|
789
|
-
if (!CONTEXT_VERDICT_STATUSES.has(status) && status < 500) return false;
|
|
790
|
-
if (message.length > 16_384) return false;
|
|
791
|
-
let text = message.trim();
|
|
792
|
-
// One pass per unwrapped envelope, plus one for the leaf the last envelope yields.
|
|
793
|
-
for (let unwrapped = 0; unwrapped <= MAX_CONTEXT_OVERFLOW_ENVELOPES; unwrapped += 1) {
|
|
794
|
-
const providerPrefix = /^Provider error \d{3}:\s*/.exec(text);
|
|
795
|
-
if (providerPrefix) text = text.slice(providerPrefix[0].length).trim();
|
|
796
|
-
if (!text.startsWith("{")) return isDefiniteContextOverflowMessage(text);
|
|
797
|
-
if (unwrapped === MAX_CONTEXT_OVERFLOW_ENVELOPES) return false;
|
|
798
|
-
let payload: unknown;
|
|
799
|
-
try { payload = JSON.parse(text); } catch { return false; }
|
|
800
|
-
if (!payload || typeof payload !== "object" || Array.isArray(payload)) return false;
|
|
801
|
-
const record = payload as Record<string, unknown>;
|
|
802
|
-
const response = record.response && typeof record.response === "object" && !Array.isArray(record.response)
|
|
803
|
-
? record.response as Record<string, unknown>
|
|
804
|
-
: undefined;
|
|
805
|
-
const source = [record.error, response?.error, response?.last_error, record.last_error, record]
|
|
806
|
-
.find((candidate): candidate is Record<string, unknown> =>
|
|
807
|
-
!!candidate && typeof candidate === "object" && !Array.isArray(candidate)
|
|
808
|
-
&& typeof (candidate as Record<string, unknown>).message === "string");
|
|
809
|
-
if (!source || isParameterValidationRecord(source)) return false;
|
|
810
|
-
text = (source.message as string).trim();
|
|
811
|
-
}
|
|
812
|
-
return false;
|
|
813
|
-
}
|
|
814
|
-
|
|
815
712
|
const CODEX_ACCOUNT_MODEL_REFUSAL = /^The '[^']{1,256}' model is not supported when using Codex with a ChatGPT account\.$/;
|
|
816
713
|
|
|
817
714
|
export type CodexAccountModelRefusal = "other" | "refusal" | "ambiguous";
|
package/src/combos/request.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { errorDestinationText, providerErrorDestinationLabel } from "../lib/error-destination";
|
|
1
2
|
import type { OcxComboDefaultEffort, OcxComboDefaultEffortMode, OcxComboReasoningEffortMode, OcxComboTarget, OcxConfig } from "../types";
|
|
2
3
|
import { isCodexReasoningEffort, isDeclaredReasoningEffort, resolveEffortAtOrBelow } from "../reasoning-effort";
|
|
3
4
|
import { resolveComboId } from "./types";
|
|
@@ -109,8 +110,7 @@ export function concreteComboRequestBody(
|
|
|
109
110
|
if (!warnedUnsupportedDefaults.has(key)) {
|
|
110
111
|
warnedUnsupportedDefaults.add(key);
|
|
111
112
|
console.debug("[opencodex] combo default effort omitted", {
|
|
112
|
-
provider: target.provider,
|
|
113
|
-
model: target.model,
|
|
113
|
+
provider: errorDestinationText(providerErrorDestinationLabel(target.provider)),
|
|
114
114
|
requestedEffort: defaultEffort,
|
|
115
115
|
capability: targetReasoningEfforts === undefined ? "unknown" : "unsupported",
|
|
116
116
|
});
|
package/src/combos/resolve.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { errorDestinationText, providerErrorDestinationLabel } from "../lib/error-destination";
|
|
1
2
|
import type { OcxComboTarget, OcxConfig } from "../types";
|
|
2
3
|
import { getCachedProviderRoutingQuota } from "../providers/quota-routing-cache";
|
|
3
4
|
import type { ProviderQuota, ProviderQuotaWindow } from "../providers/quota-types";
|
|
@@ -57,16 +58,31 @@ export class UnknownComboError extends Error {
|
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
|
|
61
|
+
declare const exclusionBrand: unique symbol;
|
|
62
|
+
/** Service diagnostics name configured positions and validated providers only. */
|
|
63
|
+
export function comboLogLabel(config: OcxConfig, comboId: string): string {
|
|
64
|
+
const index = Object.keys(config.combos ?? {}).indexOf(comboId);
|
|
65
|
+
return index < 0 ? "combo" : `combo ${index + 1}`;
|
|
66
|
+
}
|
|
67
|
+
export function comboTargetLogLabel(config: OcxConfig, comboId: string, target: OcxComboTarget): string {
|
|
68
|
+
const index = getCombo(config, comboId)?.targets.findIndex(item => targetKey(item) === targetKey(target)) ?? -1;
|
|
69
|
+
return `target ${index + 1} (${errorDestinationText(providerErrorDestinationLabel(target.provider))})`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type ComboExclusionReasons = string & { readonly [exclusionBrand]: true };
|
|
73
|
+
|
|
60
74
|
export class NoAvailableComboTargetsError extends Error {
|
|
61
75
|
readonly code = "combo_unavailable";
|
|
62
76
|
|
|
63
|
-
constructor(readonly comboId: string, readonly reasons
|
|
64
|
-
super(`No available targets for combo
|
|
77
|
+
constructor(readonly comboId: string, readonly reasons?: ComboExclusionReasons) {
|
|
78
|
+
super(`No available targets for combo${reasons ? ` (${reasons})` : ""}`);
|
|
65
79
|
this.name = "NoAvailableComboTargetsError";
|
|
66
80
|
}
|
|
67
81
|
}
|
|
68
82
|
|
|
69
|
-
|
|
83
|
+
type ComboExclusionReason = "provider missing" | "provider disabled" | "quota exhausted" | "cooldown" | "ineligible for this request";
|
|
84
|
+
|
|
85
|
+
function providerExclusionReason(config: OcxConfig, target: OcxComboTarget, now: number): ComboExclusionReason | undefined {
|
|
70
86
|
const provider = Object.hasOwn(config.providers, target.provider) ? config.providers[target.provider] : undefined;
|
|
71
87
|
if (!provider) return "provider missing";
|
|
72
88
|
if (provider.disabled === true) return "provider disabled";
|
|
@@ -93,18 +109,19 @@ export function comboTargetSkipTrigger(
|
|
|
93
109
|
|
|
94
110
|
/**
|
|
95
111
|
* Why each target of a combo cannot be picked right now, for a zero-attempt refusal. Only the
|
|
96
|
-
*
|
|
112
|
+
* target index, validated provider label and a closed exclusion reason are named.
|
|
97
113
|
*/
|
|
98
114
|
export function comboExclusionReasons(
|
|
99
115
|
config: OcxConfig,
|
|
100
116
|
comboId: string,
|
|
101
117
|
eligible?: (target: NormalizedComboTarget) => boolean,
|
|
102
118
|
now = Date.now(),
|
|
103
|
-
):
|
|
104
|
-
|
|
119
|
+
): ComboExclusionReasons {
|
|
120
|
+
const targets = getCombo(config, comboId)?.targets ?? [];
|
|
121
|
+
const reasons = targets.flatMap((target, index) => {
|
|
105
122
|
let reason = providerExclusionReason(config, target, now);
|
|
106
123
|
const cooldown = reason ? undefined : earliestComboCooldown(comboId, [target], now);
|
|
107
|
-
if (cooldown) reason =
|
|
124
|
+
if (cooldown) reason = "cooldown";
|
|
108
125
|
if (!reason && eligible) {
|
|
109
126
|
try {
|
|
110
127
|
if (!eligible(target)) reason = "ineligible for this request";
|
|
@@ -113,8 +130,9 @@ export function comboExclusionReasons(
|
|
|
113
130
|
reason = "ineligible for this request";
|
|
114
131
|
}
|
|
115
132
|
}
|
|
116
|
-
return reason ? [
|
|
133
|
+
return reason ? [`target ${index + 1} (${errorDestinationText(providerErrorDestinationLabel(target.provider))}): ${reason}`] : [];
|
|
117
134
|
}).join("; ");
|
|
135
|
+
return `${targets.length} target${targets.length === 1 ? "" : "s"}${reasons ? `; ${reasons}` : ""}` as ComboExclusionReasons;
|
|
118
136
|
}
|
|
119
137
|
|
|
120
138
|
/** Capture usable providers' quota cooldowns; request eligibility is deferred to exhaustion. */
|
|
@@ -324,7 +342,7 @@ function reserveAllCooledTargets(
|
|
|
324
342
|
const next = state ?? { successes: 0, currentWeights: new Map<string, number>(), successfulUses: new Map<string, number>() };
|
|
325
343
|
next.lastCooldownReprobeAt = now;
|
|
326
344
|
selectionState.set(comboId, next);
|
|
327
|
-
console.warn(`[combo] ${comboId}: reserving all-cooled targets for one request; minimum reprobe interval ${combo.cooldownReprobe.intervalMs}ms`);
|
|
345
|
+
console.warn(`[combo] ${comboLogLabel(config, comboId)}: reserving all-cooled targets for one request; minimum reprobe interval ${combo.cooldownReprobe.intervalMs}ms`);
|
|
328
346
|
return reserved;
|
|
329
347
|
}
|
|
330
348
|
|
|
@@ -624,7 +642,7 @@ export async function pickComboTargetWithWait(
|
|
|
624
642
|
const normalDelay = soonest === undefined ? undefined : soonest.expiry - now;
|
|
625
643
|
if (normalDelay !== undefined && normalDelay <= options.waitForCooldownMs) {
|
|
626
644
|
console.warn(
|
|
627
|
-
`[combo] ${comboId}: deferring last resort, waiting ${normalDelay}ms for ${
|
|
645
|
+
`[combo] ${comboLogLabel(config, comboId)}: deferring last resort, waiting ${normalDelay}ms for ${comboTargetLogLabel(config, comboId, soonest!.target)}`,
|
|
628
646
|
);
|
|
629
647
|
try {
|
|
630
648
|
await (options.sleep ?? sleepWithAbort)(normalDelay, options.abortSignal);
|
|
@@ -677,7 +695,7 @@ export async function pickComboTargetWithWait(
|
|
|
677
695
|
// Its target preserves configured order for ties.
|
|
678
696
|
const target = earliest.target;
|
|
679
697
|
console.warn(
|
|
680
|
-
`[combo] ${comboId}: all targets cooling, waiting ${delay}ms for ${
|
|
698
|
+
`[combo] ${comboLogLabel(config, comboId)}: all targets cooling, waiting ${delay}ms for ${comboTargetLogLabel(config, comboId, target)}`,
|
|
681
699
|
);
|
|
682
700
|
try {
|
|
683
701
|
await (options.sleep ?? sleepWithAbort)(delay, options.abortSignal);
|
|
@@ -35,8 +35,12 @@ export type RuntimePortState = {
|
|
|
35
35
|
pid: number;
|
|
36
36
|
port: number;
|
|
37
37
|
hostname?: string;
|
|
38
|
+
/** Absolute relaunch prefix written by runtimes that identify their serving install. */
|
|
39
|
+
command?: string[];
|
|
38
40
|
/** Per-process proof key; protected by the config directory and never served. */
|
|
39
41
|
attestationSecret?: string;
|
|
42
|
+
/** A Link runtime is draining; new and in-flight sync commits must stand down. */
|
|
43
|
+
clientStopping?: true;
|
|
40
44
|
/**
|
|
41
45
|
* The live proxy's port when this runtime is a sibling instance started beside it
|
|
42
46
|
* (`src/codex/sibling-start.ts`). `ocx stop` reads it to leave shared client routing alone.
|
|
@@ -59,6 +63,7 @@ export function isValidRuntimePortState(value: unknown): value is RuntimePortSta
|
|
|
59
63
|
&& Number(state.port) <= 65535
|
|
60
64
|
&& hostnameOk
|
|
61
65
|
&& attestationOk
|
|
66
|
+
&& (state.clientStopping === undefined || state.clientStopping === true)
|
|
62
67
|
&& siblingOk;
|
|
63
68
|
}
|
|
64
69
|
|