@bitkyc08/opencodex 2.100.1 → 2.100.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (163) hide show
  1. package/gui/dist/assets/App-BpJOzw4F.js +59 -0
  2. package/gui/dist/assets/Tray-BcaI-VCr.js +1 -0
  3. package/gui/dist/assets/{brand-mark-nr3ziR-T.js → brand-mark-DU-0Vdkr.js} +1 -1
  4. package/gui/dist/assets/index-Bbor_y80.js +94 -0
  5. package/gui/dist/assets/index-DJEkIDAi.css +2 -0
  6. package/gui/dist/assets/{segment-thumb-features-DKCVUUTi.js → segment-thumb-features-e9A_FogJ.js} +1 -1
  7. package/gui/dist/classic/assets/{App-CgPBKUP-.js → App-BrIlUiLc.js} +18 -18
  8. package/gui/dist/classic/assets/{Tray-BedmOsE7.js → Tray-DT146AhS.js} +1 -1
  9. package/gui/dist/classic/assets/index-DyJHagvT.js +94 -0
  10. package/gui/dist/classic/assets/quota-summary-CakPe9Ni.js +1 -0
  11. package/gui/dist/classic/index.html +1 -1
  12. package/gui/dist/index.html +2 -2
  13. package/package.json +2 -2
  14. package/src/adapters/client-fingerprint.ts +5 -3
  15. package/src/adapters/cursor/native-exec.ts +3 -2
  16. package/src/adapters/cursor/tool-guidance.ts +3 -3
  17. package/src/adapters/cursor/tool-wording.ts +10 -1
  18. package/src/adapters/devin/cloud-direct/tool-schema.ts +20 -2
  19. package/src/adapters/google-antigravity-wire.ts +13 -6
  20. package/src/adapters/google-truncation.ts +4 -2
  21. package/src/adapters/google.ts +8 -2
  22. package/src/adapters/openai-chat/tool-schema.ts +9 -0
  23. package/src/adapters/openai-responses/tool-schema.ts +18 -4
  24. package/src/adapters/run-turn-queue.ts +139 -5
  25. package/src/adapters/upstream-http-error.ts +4 -2
  26. package/src/bridge/sse.ts +8 -1
  27. package/src/claude/chat-tool-dispatch.ts +233 -0
  28. package/src/claude/inbound-tool-loading.ts +42 -17
  29. package/src/cli/account-api.ts +6 -0
  30. package/src/cli/account-extended.ts +60 -6
  31. package/src/cli/account-key-quota.ts +22 -2
  32. package/src/cli/account.ts +1 -1
  33. package/src/cli/capabilities-access-remote.ts +10 -0
  34. package/src/cli/capabilities-agents-routing.ts +2 -2
  35. package/src/cli/capabilities-base.ts +2 -2
  36. package/src/cli/claude.ts +19 -14
  37. package/src/cli/config-command.ts +24 -1
  38. package/src/cli/connect.ts +36 -3
  39. package/src/cli/gui-pair-client.ts +23 -2
  40. package/src/cli/help.ts +1 -1
  41. package/src/cli/hub.ts +18 -1
  42. package/src/cli/link-child-ops.ts +136 -0
  43. package/src/cli/link-status-child.ts +94 -0
  44. package/src/cli/link.ts +22 -9
  45. package/src/cli/registry.ts +3 -2
  46. package/src/cli/sync-command.ts +3 -1
  47. package/src/cli/sync-fanout.ts +8 -81
  48. package/src/client/link-readiness.ts +16 -6
  49. package/src/client/link-tunnel-status.ts +76 -0
  50. package/src/clients/config-export.ts +14 -1
  51. package/src/codex/account-resume-health.ts +41 -0
  52. package/src/codex/auth-api/login-flow.ts +6 -0
  53. package/src/codex/auth-api/routes.ts +35 -2
  54. package/src/codex/catalog/gather-capture.ts +2 -0
  55. package/src/codex/catalog/provider-models.ts +13 -8
  56. package/src/codex/convergence-types.ts +2 -0
  57. package/src/codex/quota.ts +111 -6
  58. package/src/combos/cooldown-store.ts +208 -0
  59. package/src/combos/failover.ts +432 -35
  60. package/src/config/salvage.ts +1 -0
  61. package/src/config/schema/config-schema.ts +6 -3
  62. package/src/config/schema/leaf-validators.ts +34 -12
  63. package/src/generated/compatibility-version.json +213 -121
  64. package/src/lib/catalog-etag.ts +10 -0
  65. package/src/lib/client-error-body.ts +15 -0
  66. package/src/lib/context-overflow-verdict.ts +266 -9
  67. package/src/lib/failure-detail.ts +72 -3
  68. package/src/lib/hard-refusal.ts +259 -0
  69. package/src/link/child-activity.ts +57 -0
  70. package/src/link/child-catalog-freshness.ts +149 -0
  71. package/src/link/child-gated-ocx.ts +117 -0
  72. package/src/link/child-observations.ts +205 -0
  73. package/src/link/child-observe.ts +121 -0
  74. package/src/link/child-ops-engine.ts +162 -0
  75. package/src/link/child-update-contract.ts +165 -0
  76. package/src/link/paths.ts +5 -0
  77. package/src/link/remote-windows.ts +42 -6
  78. package/src/link/ssh-argv.ts +126 -32
  79. package/src/link/status-projection.ts +73 -0
  80. package/src/link/sync-fanout-classify.ts +98 -0
  81. package/src/link/tunnel-state.ts +10 -4
  82. package/src/messaging/mcp-queue-rpc.ts +2 -1
  83. package/src/messaging/mcp-queue-server.ts +5 -0
  84. package/src/messaging/mcp-reply-tools.ts +24 -9
  85. package/src/messaging/mcp-reply-wait.ts +70 -0
  86. package/src/oauth/account-resume-health.ts +60 -0
  87. package/src/oauth/index.ts +1 -0
  88. package/src/oauth/store.ts +44 -1
  89. package/src/providers/api-key-pause-state.ts +23 -0
  90. package/src/providers/api-key-pause.ts +68 -10
  91. package/src/providers/api-key-selection-capture.ts +3 -1
  92. package/src/providers/api-key-selection.ts +3 -2
  93. package/src/providers/api-keys.ts +18 -5
  94. package/src/providers/claude-cli-usage-state.ts +11 -6
  95. package/src/providers/claude-cli-usage.ts +27 -9
  96. package/src/providers/codex-capacity.ts +11 -2
  97. package/src/providers/key-failover.ts +13 -6
  98. package/src/providers/model-discovery-config.ts +31 -0
  99. package/src/providers/model-discovery-diagnostic.ts +34 -3
  100. package/src/providers/model-discovery.ts +43 -7
  101. package/src/providers/model-rename-fields.ts +2 -0
  102. package/src/providers/openai-sidecar.ts +15 -2
  103. package/src/providers/provider-id-rewrite.ts +3 -0
  104. package/src/providers/quota/account-cache.ts +52 -34
  105. package/src/providers/quota/anthropic-usage-windows.ts +19 -9
  106. package/src/providers/quota/report-cache.ts +53 -5
  107. package/src/providers/quota/vendor-probes-key.ts +73 -0
  108. package/src/providers/quota/vendor-probes-oauth.ts +57 -3
  109. package/src/providers/quota-key-accounts.ts +12 -5
  110. package/src/providers/quota-mistral-budget.ts +18 -0
  111. package/src/providers/quota-types.ts +25 -0
  112. package/src/providers/quota.ts +22 -22
  113. package/src/providers/reasoning-metadata.ts +1051 -12
  114. package/src/quota/reset-observer.ts +4 -2
  115. package/src/quota/reset-seen-store.ts +9 -0
  116. package/src/quota/window-mapping.ts +1 -0
  117. package/src/quota-pool/assemble.ts +4 -1
  118. package/src/responses/hosted-tool-policy.ts +56 -12
  119. package/src/router.ts +2 -0
  120. package/src/server/auth-cors.ts +11 -8
  121. package/src/server/catalog-download.ts +3 -5
  122. package/src/server/chat-native.ts +11 -2
  123. package/src/server/claude-messages.ts +2 -1
  124. package/src/server/images.ts +41 -13
  125. package/src/server/index/link-listener.ts +16 -2
  126. package/src/server/index/link-relay-sessions.ts +43 -2
  127. package/src/server/index/optional-listeners.ts +18 -0
  128. package/src/server/management/config-routes.ts +7 -1
  129. package/src/server/management/context.ts +1 -1
  130. package/src/server/management/link-child-ops-routes.ts +116 -0
  131. package/src/server/management/link-route-state.ts +10 -0
  132. package/src/server/management/link-routes.ts +10 -0
  133. package/src/server/management/oauth-account-routes.ts +33 -4
  134. package/src/server/management/provider-overwrite-carry.ts +28 -3
  135. package/src/server/management/provider-routes.ts +16 -11
  136. package/src/server/management/route-registry.ts +3 -0
  137. package/src/server/messages-native.ts +5 -0
  138. package/src/server/request-log-failure-detail.ts +59 -1
  139. package/src/server/responses/adapter-dispatch.ts +35 -44
  140. package/src/server/responses/combo-stream-preflight.ts +146 -29
  141. package/src/server/responses/compact.ts +16 -5
  142. package/src/server/responses/context-overflow.ts +58 -2
  143. package/src/server/responses/core-combo-failure.ts +29 -4
  144. package/src/server/responses/core-combo-native.ts +3 -1
  145. package/src/server/responses/core-combo.ts +30 -3
  146. package/src/server/responses/core-errors.ts +3 -1
  147. package/src/server/responses/core-opaque-recovery.ts +45 -2
  148. package/src/server/responses/core-options.ts +12 -0
  149. package/src/server/responses/passthrough-dispatch.ts +30 -38
  150. package/src/server/responses/request-transport.ts +43 -14
  151. package/src/server/responses/reset-replay.ts +58 -2
  152. package/src/server/responses/run-turn-execution.ts +56 -30
  153. package/src/types/claude-tool-loading.ts +48 -0
  154. package/src/types/config.ts +5 -35
  155. package/src/types/provider.ts +21 -0
  156. package/src/types.ts +1 -0
  157. package/src/web-search/parse.ts +21 -2
  158. package/gui/dist/assets/App-g3Cm2xhZ.js +0 -59
  159. package/gui/dist/assets/Tray-BMDkAiXy.js +0 -1
  160. package/gui/dist/assets/index-Bziu8gTy.css +0 -2
  161. package/gui/dist/assets/index-OYzhQUNP.js +0 -94
  162. package/gui/dist/classic/assets/index-BR-HhNrp.js +0 -94
  163. package/gui/dist/classic/assets/quota-summary-CXKhvSmQ.js +0 -1
@@ -0,0 +1,10 @@
1
+ import { createHash } from "node:crypto";
2
+
3
+ /**
4
+ * Strong ETag of a serialized catalog body: the quoted SHA-256 hex of its UTF-8 bytes. Pure, so both the Home's
5
+ * catalog download (src/server) and the link layer's catalog freshness (src/link, which must not import server
6
+ * modules) compute the same value.
7
+ */
8
+ export function catalogEtag(body: string): string {
9
+ return `"${createHash("sha256").update(body).digest("hex")}"`;
10
+ }
@@ -60,6 +60,20 @@ export function carryProxyIncompleteContext<T extends object>(source: object, ta
60
60
  proxyIncompletes.set(target, reasons);
61
61
  return target;
62
62
  }
63
+ /**
64
+ * A translated `done` whose adapter stop reason was Anthropic's `pause_turn`.
65
+ *
66
+ * The bridge still reports that turn to the client as `incomplete/max_output_tokens`, the reason the
67
+ * wire has always carried there, so the wire cannot distinguish a paused long-running turn the client
68
+ * is expected to continue from an exhausted output budget. This request-scoped fact carries the
69
+ * difference without adding or changing a client-visible field: combo stream preflight reads it so a
70
+ * paused turn is neither replayed on another target nor read as a zero-output token limit.
71
+ */
72
+ const pausedTurns = new WeakSet<object>();
73
+ export function rememberPausedTurn(context: object): void { pausedTurns.add(context); }
74
+ export function isPausedTurn(context: object | undefined): boolean {
75
+ return context !== undefined && pausedTurns.has(context);
76
+ }
63
77
  const CODE_MESSAGES: ReadonlyMap<string, string> = new Map([
64
78
  ["context_length_exceeded", CLIENT_ERROR_CONTRACTS.context_overflow],
65
79
  ["upstream_incomplete", CLIENT_ERROR_CONTRACTS.stream_incomplete],
@@ -182,6 +196,7 @@ export function rememberProxyErrorMessage(context: object, message: string, fiel
182
196
  }
183
197
  export function carryProxyErrorContext(source: object, target: object): void {
184
198
  carryProxyIncompleteContext(source, target);
199
+ if (pausedTurns.has(source)) pausedTurns.add(target);
185
200
  let messages = proxyMessages.get(source);
186
201
  if (!messages) { messages = new Map(); proxyMessages.set(source, messages); }
187
202
  proxyMessages.set(target, messages);
@@ -2,15 +2,20 @@
2
2
  * The definite context-window overflow verdict, shared by combo failover (src/combos/failover.ts,
3
3
  * which re-exports it) and the presentation classifier (src/lib/upstream-condition.ts). It has no
4
4
  * imports, so the dashboard's type-only use of the classifier never pulls in server modules.
5
+ *
6
+ * The rule is deliberately conservative: a positive verdict needs an overflow predicate attached to
7
+ * a context subject, and it is discarded when a negator governs that predicate. Nothing here reads
8
+ * "a request failed" or "the service is busy" as an overflow -- only wording that says this input
9
+ * did not fit this model's window.
5
10
  */
6
11
 
7
12
  /** A status can carry a verdict about the REQUEST; 401/403/429 speak about the credential. */
8
13
  const CONTEXT_VERDICT_STATUSES: ReadonlySet<number> = new Set([400, 413, 422]);
9
14
 
10
15
  /**
11
- * Phrases a provider emits when the INPUT does not fit this model's context window. Matched
12
- * against the innermost provider message only, so an unrelated refusal that merely quotes one
13
- * of these tokens in a code field cannot authorize a replay.
16
+ * Phrases a provider emits when the INPUT does not fit this model's context window. These are the
17
+ * verdicts `dev` already matched as substrings, kept at the same reach so a provider that worded its
18
+ * refusal this way keeps failing over. Matched against the innermost provider message only.
14
19
  */
15
20
  const DEFINITE_CONTEXT_OVERFLOW_PHRASES = [
16
21
  "exceeds the context window",
@@ -22,6 +27,96 @@ const DEFINITE_CONTEXT_OVERFLOW_PHRASES = [
22
27
  "too many tokens",
23
28
  ];
24
29
 
30
+ /**
31
+ * A context subject: the input, or the conversation carrying it. Every generalized verdict below
32
+ * needs one, so capacity prose about a service ("shards exhausted", "service is at capacity")
33
+ * stays non-overflow.
34
+ */
35
+ const CONTEXT_SUBJECT = String.raw`(?:history|conversation|input|prompt|request|turn|messages?)`;
36
+ /** A context capacity, in the forms providers actually write it. */
37
+ const CONTEXT_NOUN = String.raw`(?:model(?:['\u2019]s)?\s+)?context\s+(?:window|length|limit)`;
38
+ /** A copula that can sit between a subject or capacity and its predicate. */
39
+ const CONTEXT_AUX = String.raw`(?:[ \t]+(?:is|are|was|were|has|have|had))?`;
40
+ /**
41
+ * The stretch between a capacity noun and its state predicate, over copulas, participles and
42
+ * adverbs ("has been", "is now completely", "has (already) been"). Parentheticals are allowed
43
+ * anywhere in it, which is why "has (already) been exceeded" reads as a verdict.
44
+ */
45
+ const CONTEXT_STATE_LINK = String.raw`(?:[ \t]+(?:is|are|was|were|has|have|had|been|now|currently|already|completely|fully|totally|entirely|still|just|absolutely)|\s*\([^()]*\))*`;
46
+
47
+ interface OverflowVerdict {
48
+ pattern: RegExp;
49
+ /** 1-based capture group holding the predicate whose negation governs the verdict. */
50
+ predicateGroup: number;
51
+ }
52
+
53
+ /** Wording that says the input is longer than the model's capacity. */
54
+ const CONTEXT_WINDOW_TOO_LONG_VERDICT: OverflowVerdict = {
55
+ pattern: new RegExp(
56
+ String.raw`\b(?:prompt|input|request|messages?)\s+(?:is|are|was|were)\s+(too\s+long)\s+for\b[^;!?\n.]{0,60}\b${CONTEXT_NOUN}\b`,
57
+ "i",
58
+ ),
59
+ predicateGroup: 1,
60
+ };
61
+
62
+ /**
63
+ * Wording that says a conversation/input reached or passed its model's context capacity: "its
64
+ * history is at or past the model's context window", "the request has reached the model context
65
+ * length limit". The subject must carry its own predicate and the capacity must follow it closely,
66
+ * so a predicate word in an unrelated clause is not a verdict; the phrase list above still covers
67
+ * the shorter dev-era wordings.
68
+ */
69
+ const CONTEXT_WINDOW_POSITION_VERDICT: OverflowVerdict = {
70
+ pattern: new RegExp(
71
+ String.raw`\b${CONTEXT_SUBJECT}\b${CONTEXT_AUX}\s+(?:[a-z'\u2019]+\s+){0,3}?`
72
+ + String.raw`(at\s+or\s+past|at\s+(?:the\s+)?(?:absolute\s+)?limit|past|over|beyond|exceed(?:s|ed|ing)?|reached)\b`
73
+ + String.raw`[ \t]+(?:(?:the|this|that|its|their|our|your|a|an|selected|maximum|allowed|available)\s+){0,2}\b${CONTEXT_NOUN}\b`,
74
+ "i",
75
+ ),
76
+ predicateGroup: 1,
77
+ };
78
+
79
+ /**
80
+ * Wording that puts the context capacity itself into the overflow state: "the model's context window
81
+ * is full", "the context length has been exceeded". Each predicate has to stand on its own, so
82
+ * "full of cached tokens", "full size", "full-duplex" and "context limit exceeded expectations"
83
+ * stay non-overflow.
84
+ */
85
+ const CONTEXT_WINDOW_STATE_VERDICTS: readonly OverflowVerdict[] = [
86
+ {
87
+ // "full" closes its clause; a trailing parenthetical ("full (8192 tokens used)") is allowed.
88
+ pattern: new RegExp(
89
+ String.raw`\b${CONTEXT_NOUN}${CONTEXT_STATE_LINK}[ \t]*(full)\b`
90
+ + String.raw`(?=\s*(?:\([^()]*\)\s*)?(?:$|[.!?;:\n]))`,
91
+ "i",
92
+ ),
93
+ predicateGroup: 1,
94
+ },
95
+ {
96
+ pattern: new RegExp(
97
+ String.raw`\b${CONTEXT_NOUN}${CONTEXT_STATE_LINK}[ \t]*(exhausted)\b`,
98
+ "i",
99
+ ),
100
+ predicateGroup: 1,
101
+ },
102
+ {
103
+ pattern: new RegExp(
104
+ String.raw`\b${CONTEXT_NOUN}${CONTEXT_STATE_LINK}[ \t]*(at\s+its\s+limit)\b`,
105
+ "i",
106
+ ),
107
+ predicateGroup: 1,
108
+ },
109
+ {
110
+ // "exceeded" closes the clause (optionally naming the overage); "exceeded expectations" does not.
111
+ pattern: new RegExp(
112
+ String.raw`\b${CONTEXT_NOUN}${CONTEXT_STATE_LINK}[ \t]*(exceed(?:s|ed|ing)?)\b`
113
+ + String.raw`(?=\s*(?:\([^()]*\)\s*)?(?:$|[.!?;:\n,]|\bby\s+\d))`,
114
+ "i",
115
+ ),
116
+ predicateGroup: 1,
117
+ },
118
+ ];
119
+
25
120
  /**
26
121
  * Short provider verdicts that a validation error could otherwise echo as the caller's own value
27
122
  * (#7443). Each counts only at the very start of the provider's leaf message, after JSON envelopes
@@ -50,6 +145,7 @@ const CONTEXT_PARAM = new RegExp(`^(?:${CONTEXT_PARAM_NAMES})(?:$|[.[])`, "i");
50
145
  const PARAMETER_VALIDATION_CODES: ReadonlySet<string> = new Set(["invalid_value", "invalid_type"]);
51
146
  const PARAMETER_VALIDATION_MARKERS: readonly RegExp[] = [
52
147
  /\binvalid_(?:value|type)\b/i,
148
+ /\bunsupported\s+parameter\b/i,
53
149
  new RegExp(`['"]param['"]\\s*:\\s*['"](?!(?:${CONTEXT_PARAM_NAMES})(?:['".[]))`, "i"),
54
150
  /\binvalid\s+(?:value\s+for\s+)?['"`\u2018\u201c]/i,
55
151
  /\binvalid\s+(?:tool|function|parameter|argument|field|value)\b/i,
@@ -57,15 +153,142 @@ const PARAMETER_VALIDATION_MARKERS: readonly RegExp[] = [
57
153
  /\bvalidation errors? for\b/i,
58
154
  ];
59
155
 
156
+ /**
157
+ * Capacity and availability framing. A "full" verdict inside such prose describes the service, not
158
+ * this turn: "Server busy: the context window is full" and "the context window is full, retry later"
159
+ * are target-level back-pressure, so they keep their cooldown instead of failing over.
160
+ *
161
+ * The predicate's own clause is read with the wide vocabulary, which covers inflections
162
+ * ("retrying", "loaded") and the serving-stack resources that run out under load (KV cache, GPU
163
+ * memory, replicas, workers, pools). What follows the clause (after a semicolon, or the next
164
+ * sentence) is usually the provider's remedy ("Remove older messages before retrying", "consider
165
+ * prompt caching"), so it is read only with the narrow availability vocabulary. On a 5xx the
166
+ * ambiguous predicates never count at all (see `isDefiniteContextOverflow`).
167
+ */
168
+ const CAPACITY_FRAMING = /\b(?:busy|overload\w*|capacity|retr(?:y|ies|ied|ying)|try(?:ing)?\s+again|peak\s+hours?|shards?|load\w*|servic\w*|allocat\w*|queu\w*|throttl\w*|cach\w*|kv|gpus?|memory|replicas?|workers?|pools?|scal(?:e|ed|es|ing)|stall\w*)\b/i;
169
+ /** Availability wording that marks back-pressure even in the trailing remedy text. */
170
+ const TRAILING_CAPACITY_FRAMING = /\b(?:busy|overload\w*|capacity|retry|try\s+again|peak\s+hours?|shards?|load|service|allocation|queue\w*|throttl\w*)\b/i;
171
+ /** State predicates that can describe either this input or the service's availability. */
172
+ const CAPACITY_AMBIGUOUS_PREDICATES: ReadonlySet<string> = new Set(["full", "exhausted", "at its limit"]);
173
+
60
174
  function isParameterValidationRecord(record: Record<string, unknown>): boolean {
61
175
  if (typeof record.code === "string" && PARAMETER_VALIDATION_CODES.has(record.code.toLowerCase())) return true;
62
176
  return typeof record.param === "string" && record.param.length > 0 && !CONTEXT_PARAM.test(record.param);
63
177
  }
64
178
 
65
- function isDefiniteContextOverflowMessage(text: string): boolean {
179
+ /**
180
+ * Negation is read at clause scope around the verdict's predicate. A negator in the same clause as
181
+ * the predicate, before it, denies the verdict: "does not exceed the context window", "never, as
182
+ * far as we can tell, exceeded", "has not been exceeded", "neither ... nor ... exceeded". A
183
+ * negation in an earlier clause ("could not respond, maximum context length exceeded") cannot
184
+ * cancel an overflow reported afterwards, which is the case three review rounds kept regressing.
185
+ */
186
+ const NEGATOR = String.raw`(?:\b(?:not|never|no|none|nothing|neither|nor|nobody|without)\b|\b[a-z]+n['\u2019]t\b)`;
187
+ /**
188
+ * Punctuation and conjunctions that start a new clause, ending a negator's reach. A full stop
189
+ * counts: "The input is not past the context limit. Maximum context length exceeded instead."
190
+ * negates one sentence and reports the overflow in the next.
191
+ */
192
+ const CLAUSE_BREAK = String.raw`[,.;:!?\n]|\b(?:and|but|because|although|though|while|whereas|since)\b`;
193
+ /**
194
+ * Curated asides that do not start a new clause: a negator still governs the predicate across
195
+ * "never, as far as we can tell, exceeded" and "does not approach, let alone exceed". Only these
196
+ * phrases are folded away; an arbitrary comma pair still separates clauses, because collapsing one
197
+ * was what turned a negated first clause into an overflow verdict. Each aside is matched as whole
198
+ * words: an unbounded `ever` would fold the negator out of "never" ("The input never exceeded the
199
+ * context window") and read the prohibition as an overflow report.
200
+ */
201
+ const NEGATION_ASIDE = String.raw`(?:in\s+fact|actually|ever|really|truly|yet|appear\s+to|seem\s+to|as\s+far\s+as\s+(?:we|i)\s+can\s+tell|per\s+(?:our\s+checks|the\s+tokenizer))`;
202
+ const NEGATION_ASIDE_PATTERN = new RegExp(`\\s*,?\\s*\\b${NEGATION_ASIDE}\\b\\s*,?\\s*`, "gi");
203
+ const LET_ALONE_PATTERN = /\s*,?\s*(?:approach|come\s+close\s+to)?\s*,?\s*let\s+alone\s*,?\s*/gi;
204
+
205
+ /** A count that shows this request crossed the limit, which overrides a modal prohibition. */
206
+ const OVERFLOW_COUNT_EVIDENCE = /\b\d+\s*(?:tokens?)?\s*(?:>|exceeds?|past)\s*\d+\b|\b(?:exceeded|exceeds|past|over)\s+(?:the\s+)?(?:limit|maximum|context\s+(?:window|length|limit))\s+by\s+\d+\b/i;
207
+ /** A rule about the limit ("must not exceed") rather than a report that this request crossed it. */
208
+ const MODAL_PROHIBITION = /\b(?:must|should|shall|may)\s+(?:not|never)\b|\b(?:mustn|shouldn)['\u2019]t\b|\bcan(?:not|['\u2019]t)\b/i;
209
+
210
+ /** How far before a predicate its clause is read. */
211
+ const NEGATION_LOOKBEHIND = 120;
212
+
213
+ /**
214
+ * True when a negator governs the predicate at `predicateStart`. A modal prohibition ("must not
215
+ * exceed") keeps the verdict only when the text also carries count evidence that this request
216
+ * crossed the limit: "Total tokens must not exceed the context window: 9000 > 8192" reports an
217
+ * overflow, while "the input must not exceed the model context window" states a rule.
218
+ *
219
+ * Parenthetical content is not part of the surrounding clause, so "not (per the tokenizer) beyond"
220
+ * keeps its negator and "No compression applied (maximum context length exceeded)" starts a fresh
221
+ * clause at the opening parenthesis instead of inheriting "No".
222
+ */
223
+ function isNegatedBefore(text: string, predicateStart: number): boolean {
224
+ let window = text.slice(Math.max(0, predicateStart - NEGATION_LOOKBEHIND), predicateStart)
225
+ .replace(/\([^()]*\)/g, " ")
226
+ .replace(NEGATION_ASIDE_PATTERN, " ")
227
+ .replace(LET_ALONE_PATTERN, " ");
228
+ const openParenthesis = window.lastIndexOf("(");
229
+ if (openParenthesis > window.lastIndexOf(")")) window = window.slice(openParenthesis + 1);
230
+ const boundary = new RegExp(CLAUSE_BREAK, "gi");
231
+ let clauseStart = 0;
232
+ for (const match of window.matchAll(boundary)) clauseStart = (match.index ?? 0) + match[0].length;
233
+ const clause = window.slice(clauseStart);
234
+ if (MODAL_PROHIBITION.test(clause)) return !OVERFLOW_COUNT_EVIDENCE.test(text);
235
+ return new RegExp(NEGATOR, "i").test(clause);
236
+ }
237
+
238
+ /**
239
+ * Capacity framing around an ambiguous verdict at `index`. The predicate's own clause (sentence
240
+ * start to the next `;` or sentence end) is checked with the wide vocabulary; the rest of that
241
+ * sentence and the next one with the narrow one, because back-pressure often arrives split across
242
+ * two sentences ("The context window is full. Please retry later.") while a remedy does too.
243
+ */
244
+ function hasCapacityFraming(text: string, index: number): boolean {
245
+ const before = text.slice(0, index);
246
+ const start = Math.max(before.lastIndexOf("."), before.lastIndexOf("!"), before.lastIndexOf("?"), before.lastIndexOf("\n")) + 1;
247
+ const clauseEnd = /[;.!?\n]/.exec(text.slice(index));
248
+ const clauseStop = clauseEnd === null ? text.length : index + (clauseEnd.index ?? 0);
249
+ if (CAPACITY_FRAMING.test(text.slice(start, clauseStop))) return true;
250
+ const firstEnd = /[.!?\n]/.exec(text.slice(index));
251
+ const resume = firstEnd === null ? text.length : index + (firstEnd.index ?? 0) + 1;
252
+ const secondEnd = /[.!?\n]/.exec(text.slice(resume));
253
+ const end = secondEnd === null ? text.length : resume + (secondEnd.index ?? 0) + 1;
254
+ return TRAILING_CAPACITY_FRAMING.test(text.slice(clauseStop, end));
255
+ }
256
+
257
+ function hasAffirmedVerdict(text: string, verdict: OverflowVerdict, allowAmbiguous: boolean): boolean {
258
+ const flags = verdict.pattern.flags.includes("g") ? verdict.pattern.flags : `${verdict.pattern.flags}g`;
259
+ const pattern = new RegExp(verdict.pattern.source, flags);
260
+ for (const match of text.matchAll(pattern)) {
261
+ const predicate = match[verdict.predicateGroup];
262
+ if (predicate === undefined) continue;
263
+ const predicateStart = (match.index ?? 0) + match[0].indexOf(predicate);
264
+ if (isNegatedBefore(text, predicateStart)) continue;
265
+ // "full", "exhausted" and "at its limit" are ambiguous between an input overflow and service
266
+ // back-pressure. They count only on a status that speaks about the request, and only when the
267
+ // text around the predicate is free of capacity framing.
268
+ // The predicate pattern allows any whitespace ("at its\tlimit"), so normalize it before the
269
+ // set lookup or a reflowed message would bypass the 5xx rule.
270
+ if (CAPACITY_AMBIGUOUS_PREDICATES.has(predicate.toLowerCase().replace(/\s+/g, " "))
271
+ && (!allowAmbiguous || hasCapacityFraming(text, predicateStart))) continue;
272
+ return true;
273
+ }
274
+ return false;
275
+ }
276
+
277
+ function hasAffirmedPhrase(text: string, phrase: string): boolean {
278
+ const pattern = new RegExp(phrase.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "gi");
279
+ for (const match of text.matchAll(pattern)) {
280
+ if (!isNegatedBefore(text, match.index ?? 0)) return true;
281
+ }
282
+ return false;
283
+ }
284
+
285
+ function isDefiniteContextOverflowMessage(text: string, allowAmbiguous = true): boolean {
66
286
  const normalized = text.toLowerCase();
67
287
  return normalized === "context_length_exceeded"
68
- || DEFINITE_CONTEXT_OVERFLOW_PHRASES.some(phrase => normalized.includes(phrase))
288
+ || DEFINITE_CONTEXT_OVERFLOW_PHRASES.some(phrase => hasAffirmedPhrase(text, phrase))
289
+ || hasAffirmedVerdict(text, CONTEXT_WINDOW_POSITION_VERDICT, allowAmbiguous)
290
+ || hasAffirmedVerdict(text, CONTEXT_WINDOW_TOO_LONG_VERDICT, allowAmbiguous)
291
+ || CONTEXT_WINDOW_STATE_VERDICTS.some(verdict => hasAffirmedVerdict(text, verdict, allowAmbiguous))
69
292
  || ANCHORED_CONTEXT_OVERFLOW_VERDICTS.some(verdict => verdict.test(text.trim()));
70
293
  }
71
294
 
@@ -84,16 +307,21 @@ function isDefiniteContextOverflowMessage(text: string): boolean {
84
307
  * Only the exact proxy wrapper is unwrapped, within a fixed envelope budget and 16,384 characters.
85
308
  * An optional caller matcher specializes the verdict on the unwrapped, validated leaf.
86
309
  * Exported for the sidecar loops, which classify their own provider failures with the same rule.
310
+ *
311
+ * On a 5xx the ambiguous state predicates ("full", "exhausted", "at its limit") never count: a
312
+ * server error that says the context window is exhausted is far more often KV-cache or capacity
313
+ * back-pressure than this turn being too large, and a hop there would skip the target cooldown.
87
314
  */
88
- export function isDefiniteContextOverflow(status: number, message: string, matchMessage = isDefiniteContextOverflowMessage): boolean {
315
+ export function isDefiniteContextOverflow(status: number, message: string, matchMessage?: (text: string) => boolean): boolean {
89
316
  if (!CONTEXT_VERDICT_STATUSES.has(status) && status < 500) return false;
90
317
  if (message.length > 16_384) return false;
318
+ const matches = matchMessage ?? ((leaf: string) => isDefiniteContextOverflowMessage(leaf, status < 500));
91
319
  let text = message.trim();
92
320
  // One pass per unwrapped envelope, plus one for the leaf the last envelope yields.
93
321
  for (let unwrapped = 0; unwrapped <= MAX_CONTEXT_OVERFLOW_ENVELOPES; unwrapped += 1) {
94
322
  const providerPrefix = /^Provider error \d{3}:\s*/.exec(text);
95
323
  if (providerPrefix) text = text.slice(providerPrefix[0].length).trim();
96
- if (!text.startsWith("{")) return !PARAMETER_VALIDATION_MARKERS.some(marker => marker.test(text)) && matchMessage(text);
324
+ if (!text.startsWith("{")) return !PARAMETER_VALIDATION_MARKERS.some(marker => marker.test(text)) && matches(text);
97
325
  if (unwrapped === MAX_CONTEXT_OVERFLOW_ENVELOPES) return false;
98
326
  let payload: unknown;
99
327
  try { payload = JSON.parse(text); } catch { return false; }
@@ -106,8 +334,37 @@ export function isDefiniteContextOverflow(status: number, message: string, match
106
334
  .find((candidate): candidate is Record<string, unknown> =>
107
335
  !!candidate && typeof candidate === "object" && !Array.isArray(candidate)
108
336
  && typeof (candidate as Record<string, unknown>).message === "string");
109
- if (!source || isParameterValidationRecord(source)) return false;
110
- text = (source.message as string).trim();
337
+ const errorObject = record.error && typeof record.error === "object" && !Array.isArray(record.error)
338
+ ? record.error as Record<string, unknown>
339
+ : undefined;
340
+ if (source) {
341
+ if (isParameterValidationRecord(record)
342
+ || (response !== undefined && isParameterValidationRecord(response))
343
+ || (errorObject !== undefined && isParameterValidationRecord(errorObject))
344
+ || isParameterValidationRecord(source)) return false;
345
+ text = (source.message as string).trim();
346
+ continue;
347
+ }
348
+ // Some gateways put their complete diagnostic in a string-valued `error` field rather
349
+ // than the usual `{ error: { message } }` envelope. Read only that bounded leaf; a code
350
+ // elsewhere in the JSON still cannot establish an overflow by itself.
351
+ const errorTextSource = [
352
+ { owner: record, value: record.error },
353
+ ...(response ? [
354
+ { owner: response, value: response.error },
355
+ { owner: response, value: response.last_error },
356
+ ] : []),
357
+ { owner: record, value: record.last_error },
358
+ ].find(candidate => typeof candidate.value === "string");
359
+ if (!errorTextSource || typeof errorTextSource.value !== "string") return false;
360
+ // A string-valued leaf can sit inside `response.error` while the outer envelope carries
361
+ // the validation code, or sit as a top-level `last_error` alongside `response`. Check
362
+ // all records before trusting the leaf's context wording.
363
+ if (isParameterValidationRecord(record)
364
+ || (response !== undefined && isParameterValidationRecord(response))
365
+ || (errorObject !== undefined && isParameterValidationRecord(errorObject))
366
+ || isParameterValidationRecord(errorTextSource.owner)) return false;
367
+ text = errorTextSource.value.trim();
111
368
  }
112
369
  return false;
113
370
  }
@@ -23,24 +23,43 @@ export const FAILURE_DETAIL_CODES = [
23
23
 
24
24
  export type FailureDetailCode = typeof FAILURE_DETAIL_CODES[number];
25
25
 
26
- /** Closed provider error classes a provider_error detail may name (the Anthropic error envelope types). */
26
+ /**
27
+ * Closed provider error classes a provider_error detail may name: the Anthropic error envelope types
28
+ * and the canonical Google RPC status names (google.rpc.Code) that Gemini, Vertex and Antigravity put in
29
+ * `error.status`. Both are published vocabularies, so naming one discloses nothing the provider wrote.
30
+ */
27
31
  export const FAILURE_DETAIL_PROVIDER_TYPES = [
28
32
  "invalid_request_error", "authentication_error", "permission_error", "not_found_error",
29
33
  "rate_limit_error", "api_error", "overloaded_error", "request_too_large",
34
+ "CANCELLED", "UNKNOWN", "INVALID_ARGUMENT", "DEADLINE_EXCEEDED", "NOT_FOUND", "ALREADY_EXISTS",
35
+ "PERMISSION_DENIED", "RESOURCE_EXHAUSTED", "FAILED_PRECONDITION", "ABORTED", "OUT_OF_RANGE",
36
+ "UNIMPLEMENTED", "INTERNAL", "UNAVAILABLE", "DATA_LOSS", "UNAUTHENTICATED",
30
37
  ] as const;
31
38
 
32
39
  export type FailureDetailProviderType = typeof FAILURE_DETAIL_PROVIDER_TYPES[number];
33
40
 
41
+ /** Gemini/Vertex candidate finishReason values (the published FinishReason enum) a provider_error may name. */
42
+ export const FAILURE_DETAIL_FINISH_REASONS = [
43
+ "FINISH_REASON_UNSPECIFIED", "STOP", "MAX_TOKENS", "SAFETY", "RECITATION", "LANGUAGE", "OTHER", "BLOCKLIST",
44
+ "PROHIBITED_CONTENT", "SPII", "MALFORMED_FUNCTION_CALL", "IMAGE_SAFETY", "IMAGE_PROHIBITED_CONTENT",
45
+ "IMAGE_RECITATION", "IMAGE_OTHER", "NO_IMAGE", "UNEXPECTED_TOOL_CALL", "TOO_MANY_TOOL_CALLS",
46
+ ] as const;
47
+
48
+ export type FailureDetailFinishReason = typeof FAILURE_DETAIL_FINISH_REASONS[number];
49
+
34
50
  export interface FailureDetail {
35
51
  code: FailureDetailCode;
36
52
  /** Milliseconds for timeouts and stalls, bytes for an overflow. */
37
53
  value?: number;
38
54
  type?: FailureDetailProviderType;
55
+ /** The provider's own closed finish reason for the turn that failed; provider_error only. */
56
+ finishReason?: FailureDetailFinishReason;
39
57
  validation?: ValidationIssue[];
40
58
  }
41
59
 
42
60
  const CODES: ReadonlySet<string> = new Set(FAILURE_DETAIL_CODES);
43
61
  const TYPES: ReadonlySet<string> = new Set(FAILURE_DETAIL_PROVIDER_TYPES);
62
+ const FINISH_REASONS: ReadonlySet<string> = new Set(FAILURE_DETAIL_FINISH_REASONS);
44
63
  const MAX_VALUE = 1e12;
45
64
 
46
65
  /** Codes whose value parameter carries meaning; the value is dropped for any other code. */
@@ -61,9 +80,59 @@ export function boundedFailureDetail(value: unknown): FailureDetail | undefined
61
80
  const valid = typeof number === "number" && Number.isSafeInteger(number) && number >= 0 && number <= MAX_VALUE && VALUED.has(code);
62
81
  const type = code === "provider_error" && typeof record.type === "string" && TYPES.has(record.type)
63
82
  ? record.type as FailureDetailProviderType : undefined;
83
+ const finishReason = code === "provider_error" && typeof record.finishReason === "string" && FINISH_REASONS.has(record.finishReason)
84
+ ? record.finishReason as FailureDetailFinishReason : undefined;
64
85
  const validation = code === "provider_error" ? boundedValidationIssues(record.validation) : undefined;
65
86
  const keys = Object.keys(record);
66
- const exact = keys.every(key => key === "code" || (key === "value" && valid) || (key === "type" && type !== undefined) || (key === "validation" && validation !== undefined && validation === record.validation));
87
+ const exact = keys.every(key => key === "code" || (key === "value" && valid) || (key === "type" && type !== undefined)
88
+ || (key === "finishReason" && finishReason !== undefined)
89
+ || (key === "validation" && validation !== undefined && validation === record.validation));
67
90
  if (exact) return record as unknown as FailureDetail;
68
- return { code, ...(valid ? { value: number as number } : {}), ...(type ? { type } : {}), ...(validation ? { validation } : {}) };
91
+ return {
92
+ code, ...(valid ? { value: number as number } : {}), ...(type ? { type } : {}),
93
+ ...(finishReason ? { finishReason } : {}), ...(validation ? { validation } : {}),
94
+ };
95
+ }
96
+
97
+ /** The closed facts an adapter can state about a provider failure it parsed off the wire. */
98
+ export interface ProviderFailureFacts {
99
+ type?: FailureDetailProviderType;
100
+ finishReason?: FailureDetailFinishReason;
101
+ }
102
+
103
+ // Keyed by the adapter event object, so the facts can never be serialized with it; an event a guard
104
+ // rebuilds simply carries none.
105
+ const providerFailureFacts = new WeakMap<object, ProviderFailureFacts>();
106
+
107
+ /**
108
+ * Attach the closed provider facts to an adapter error event. Values outside the closed sets are
109
+ * dropped here, so an adapter can pass what the provider sent without checking it first.
110
+ */
111
+ export function withProviderFailureFacts<T extends object>(event: T, facts: { type?: unknown; finishReason?: unknown }): T {
112
+ const type = typeof facts.type === "string" && TYPES.has(facts.type) ? facts.type as FailureDetailProviderType : undefined;
113
+ const finishReason = typeof facts.finishReason === "string" && FINISH_REASONS.has(facts.finishReason)
114
+ ? facts.finishReason as FailureDetailFinishReason : undefined;
115
+ if (type || finishReason) providerFailureFacts.set(event, { ...(type ? { type } : {}), ...(finishReason ? { finishReason } : {}) });
116
+ return event;
117
+ }
118
+
119
+ export function providerFailureDetailOf(event: object): ProviderFailureFacts | undefined {
120
+ return providerFailureFacts.get(event);
121
+ }
122
+
123
+ const isObject = (value: unknown): value is Record<string, unknown> =>
124
+ value !== null && typeof value === "object" && !Array.isArray(value);
125
+
126
+ /**
127
+ * The closed provider error class a parsed upstream error body names: the envelope `error.type`, Google's
128
+ * `error.status` (also as the first element of a streamed JSON array), or a top-level `type`.
129
+ */
130
+ export function providerErrorTypeOf(payload: unknown): FailureDetailProviderType | undefined {
131
+ const record = Array.isArray(payload) ? payload[0] : payload;
132
+ if (!isObject(record)) return undefined;
133
+ const nested = isObject(record.error) ? record.error : undefined;
134
+ for (const value of [nested?.type, nested?.status, record.type]) {
135
+ if (typeof value === "string" && TYPES.has(value)) return value as FailureDetailProviderType;
136
+ }
137
+ return undefined;
69
138
  }