@bitkyc08/opencodex 2.96.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.
Files changed (240) hide show
  1. package/bin/ocx.mjs +214 -19
  2. package/gui/dist/assets/{App-bwugJRKn.js → App-DuNhggE1.js} +15 -15
  3. package/gui/dist/assets/{Tray-BOPQyDbH.js → Tray-CEw0y0jR.js} +1 -1
  4. package/gui/dist/assets/index-CynE_Rue.js +94 -0
  5. package/gui/dist/assets/{quota-summary-6HsrHQAy.js → quota-summary-VHFSlUuM.js} +1 -1
  6. package/gui/dist/index.html +1 -1
  7. package/package.json +1 -1
  8. package/src/adapters/command-code.ts +15 -16
  9. package/src/adapters/cursor/transport-retry.ts +2 -2
  10. package/src/adapters/devin/cloud-direct/chat.ts +15 -6
  11. package/src/adapters/kiro/stream.ts +63 -4
  12. package/src/adapters/kiro-constants.ts +15 -1
  13. package/src/adapters/kiro-events.ts +31 -1
  14. package/src/adapters/kiro-retry.ts +3 -3
  15. package/src/adapters/ollama-native.ts +11 -1
  16. package/src/adapters/openai-responses/empty-assistant.ts +15 -3
  17. package/src/adapters/openai-responses/passthrough.ts +5 -4
  18. package/src/adapters/openai-responses/reasoning.ts +9 -4
  19. package/src/adapters/openai-responses/request-strips.ts +1 -1
  20. package/src/adapters/physical-send.ts +3 -3
  21. package/src/adapters/zed-openai-stream.ts +2 -1
  22. package/src/bridge/errors.ts +11 -2
  23. package/src/bridge/response-json.ts +2 -1
  24. package/src/bridge/sse.ts +14 -6
  25. package/src/chat/failure-verdict.ts +21 -0
  26. package/src/chat/outbound.ts +139 -21
  27. package/src/claude/outbound.ts +49 -2
  28. package/src/cli/capabilities-base.ts +25 -3
  29. package/src/cli/capabilities-command.ts +12 -3
  30. package/src/cli/capability-contracts.ts +2 -0
  31. package/src/cli/codex-launch-auth.ts +30 -1
  32. package/src/cli/codex-launcher.ts +12 -5
  33. package/src/cli/codex-shim-autorestore.ts +5 -4
  34. package/src/cli/dispatch.ts +11 -176
  35. package/src/cli/help.ts +2 -2
  36. package/src/cli/index.ts +5 -1
  37. package/src/cli/link.ts +1 -1
  38. package/src/cli/registry.ts +11 -3
  39. package/src/cli/root.ts +8 -0
  40. package/src/cli/sync-command.ts +243 -0
  41. package/src/cli/sync-fanout-process.ts +92 -0
  42. package/src/cli/sync-fanout.ts +150 -0
  43. package/src/cli/sync-flags.ts +10 -0
  44. package/src/cli/sync-link-status.ts +39 -0
  45. package/src/cli/sync-update-guard.ts +18 -0
  46. package/src/client/connect.ts +31 -14
  47. package/src/client/link-home.ts +3 -2
  48. package/src/client/machine-listener.ts +3 -0
  49. package/src/client/restore-ownership.ts +19 -1
  50. package/src/client/runtime-restart.ts +12 -4
  51. package/src/client/runtime.ts +40 -11
  52. package/src/client/stop-routing.ts +62 -0
  53. package/src/client/sync-runtime.ts +65 -0
  54. package/src/clients/config-export.ts +32 -3
  55. package/src/codex/auth-context.ts +49 -103
  56. package/src/codex/catalog/antigravity-discovery.ts +16 -5
  57. package/src/codex/catalog/provider-models.ts +6 -8
  58. package/src/codex/forward-transport-headers.ts +35 -6
  59. package/src/codex/inject/config-toml.ts +88 -0
  60. package/src/codex/inject/multi-agent-v2.ts +30 -4
  61. package/src/codex/inject/plan.ts +34 -7
  62. package/src/codex/inject-coordination.ts +11 -0
  63. package/src/codex/inject.ts +18 -7
  64. package/src/codex/lineage.ts +13 -7
  65. package/src/codex/quota.ts +22 -6
  66. package/src/codex/request-owned-affinity-key.ts +11 -0
  67. package/src/codex/request-owned-affinity.ts +118 -0
  68. package/src/codex/request-owned-main-pin.ts +86 -0
  69. package/src/codex/role-capability.ts +52 -41
  70. package/src/codex/routing/active-account.ts +13 -5
  71. package/src/codex/routing/selection.ts +40 -1
  72. package/src/codex/routing/thread-affinity-store.ts +916 -0
  73. package/src/codex/routing/thread-affinity.ts +90 -12
  74. package/src/codex/toml-source-lines.ts +72 -13
  75. package/src/combos/failover.ts +6 -109
  76. package/src/combos/request.ts +2 -2
  77. package/src/combos/resolve.ts +29 -11
  78. package/src/config/process-state.ts +5 -0
  79. package/src/config/schema/leaf-validators.ts +1 -0
  80. package/src/generated/compatibility-version.json +433 -165
  81. package/src/images/loop.ts +2 -1
  82. package/src/lib/client-error-body.ts +184 -0
  83. package/src/lib/client-retry-after.ts +66 -0
  84. package/src/lib/config-ownership.ts +4 -0
  85. package/src/lib/context-overflow-verdict.ts +113 -0
  86. package/src/lib/desktop-cli-handoff.d.mts +1 -1
  87. package/src/lib/desktop-cli-handoff.mjs +11 -1
  88. package/src/lib/empty-tool-catalog.ts +25 -3
  89. package/src/lib/error-destination.ts +47 -0
  90. package/src/lib/failure-detail.ts +66 -0
  91. package/src/lib/model-identity.ts +44 -0
  92. package/src/lib/private-sqlite-path.ts +124 -0
  93. package/src/lib/response-service-tier.ts +17 -0
  94. package/src/lib/retry-after.ts +3 -0
  95. package/src/lib/spend-ledger-owner.ts +43 -4
  96. package/src/lib/upstream-condition.ts +142 -0
  97. package/src/lib/upstream-retry.ts +14 -11
  98. package/src/lib/windows-owner-acl.ts +13 -6
  99. package/src/link/remote-platform.ts +14 -0
  100. package/src/link/remote-windows.ts +51 -9
  101. package/src/link/ssh-argv.ts +63 -10
  102. package/src/oauth/generic-account-failover.ts +13 -11
  103. package/src/oauth/index.ts +36 -74
  104. package/src/oauth/oauth-errors.ts +67 -0
  105. package/src/oauth/store.ts +1 -1
  106. package/src/protocols/codecs/ollama.ts +197 -0
  107. package/src/protocols/encoders/adapter-events.ts +4 -0
  108. package/src/protocols/encoders/chat.ts +18 -10
  109. package/src/protocols/trace.ts +18 -0
  110. package/src/providers/fastwire.ts +3 -2
  111. package/src/providers/quota/anthropic-usage-windows.ts +13 -3
  112. package/src/providers/quota/vendor-probes-key.ts +60 -24
  113. package/src/providers/request-pacing.ts +3 -2
  114. package/src/responses/apply-patch-envelope.ts +45 -19
  115. package/src/responses/compaction-history-framing.ts +5 -1
  116. package/src/responses/freeform-wrapper-scan.ts +5 -3
  117. package/src/responses/progressive-freeform-input.ts +354 -45
  118. package/src/routing/fallback-policy.ts +3 -1
  119. package/src/server/admission-model-scope.ts +17 -6
  120. package/src/server/admitted-http-turn.ts +56 -0
  121. package/src/server/auth-cors.ts +5 -0
  122. package/src/server/chat-completions.ts +45 -20
  123. package/src/server/chat-native-sse.ts +52 -7
  124. package/src/server/chat-native.ts +60 -35
  125. package/src/server/claude-bridge-pool-session.ts +59 -0
  126. package/src/server/claude-messages.ts +51 -24
  127. package/src/server/context-history.ts +5 -1
  128. package/src/server/inbound-body-admission.ts +11 -4
  129. package/src/server/index/ollama-routes.ts +89 -0
  130. package/src/server/index/serve-options.ts +21 -4
  131. package/src/server/index/websocket-handler.ts +4 -2
  132. package/src/server/index.ts +6 -36
  133. package/src/server/inference/client-encoder-delivery.ts +4 -2
  134. package/src/server/json-serialized-bytes.ts +44 -0
  135. package/src/server/management/agent-settings-routes.ts +24 -21
  136. package/src/server/management/codex-cli-update-routes.ts +2 -1
  137. package/src/server/management/route-registry.ts +1 -1
  138. package/src/server/messages-native-scope.ts +2 -1
  139. package/src/server/messages-native-tool-guard.ts +1 -1
  140. package/src/server/messages-native.ts +12 -7
  141. package/src/server/messages-shared.ts +6 -2
  142. package/src/server/native-final-log.ts +93 -0
  143. package/src/server/ollama-inbound.ts +349 -0
  144. package/src/server/ollama-native-paths.ts +21 -0
  145. package/src/server/ollama-route-policy.ts +73 -0
  146. package/src/server/relay.ts +22 -3
  147. package/src/server/request-decompress.ts +4 -2
  148. package/src/server/request-log-failure-attribution.ts +22 -0
  149. package/src/server/request-log-failure-detail.ts +80 -0
  150. package/src/server/request-log-terminal-status.ts +4 -2
  151. package/src/server/request-log.ts +79 -42
  152. package/src/server/response-log-body.ts +6 -1
  153. package/src/server/responses/account-change-state.ts +17 -6
  154. package/src/server/responses/adapter-continuation.ts +9 -4
  155. package/src/server/responses/adapter-delivery.ts +5 -4
  156. package/src/server/responses/adapter-dispatch.ts +31 -30
  157. package/src/server/responses/anthropic-thinking-recovery.ts +3 -0
  158. package/src/server/responses/codex-auth-error.ts +19 -18
  159. package/src/server/responses/codex-ws-exchange.ts +11 -8
  160. package/src/server/responses/codex-ws-metadata.ts +94 -21
  161. package/src/server/responses/codex-ws-request.ts +3 -1
  162. package/src/server/responses/compact.ts +53 -23
  163. package/src/server/responses/compaction-recovery.ts +4 -2
  164. package/src/server/responses/compaction-routing.ts +9 -4
  165. package/src/server/responses/core-auth.ts +32 -14
  166. package/src/server/responses/core-codex-account.ts +17 -6
  167. package/src/server/responses/core-combo-failure.ts +5 -1
  168. package/src/server/responses/core-combo-native.ts +2 -1
  169. package/src/server/responses/core-combo.ts +41 -27
  170. package/src/server/responses/core-errors.ts +22 -20
  171. package/src/server/responses/core-lifetime.ts +6 -3
  172. package/src/server/responses/core-normalize.ts +1 -0
  173. package/src/server/responses/core-options.ts +7 -0
  174. package/src/server/responses/core-pipeline.ts +4 -3
  175. package/src/server/responses/fetch-helpers.ts +7 -1
  176. package/src/server/responses/input-admission.ts +28 -2
  177. package/src/server/responses/native-injection.ts +1 -1
  178. package/src/server/responses/native-response-json.ts +13 -4
  179. package/src/server/responses/native-response-output.ts +1 -2
  180. package/src/server/responses/non-replayable-error.ts +2 -1
  181. package/src/server/responses/pacing-overload.ts +3 -2
  182. package/src/server/responses/passthrough-delivery.ts +55 -14
  183. package/src/server/responses/passthrough-dispatch.ts +19 -17
  184. package/src/server/responses/passthrough-error.ts +9 -9
  185. package/src/server/responses/policy-fallback.ts +2 -1
  186. package/src/server/responses/request-prepare.ts +67 -48
  187. package/src/server/responses/request-transport.ts +35 -31
  188. package/src/server/responses/run-turn-execution.ts +6 -6
  189. package/src/server/responses/sidecar-execution.ts +16 -12
  190. package/src/server/responses/spend-storage-error.ts +2 -1
  191. package/src/server/responses/ws-upstream.ts +9 -0
  192. package/src/server/responses-custom-tool-repair.ts +14 -3
  193. package/src/server/responses-terminal-repair.ts +5 -2
  194. package/src/server/responses-undeclared-tool-guard.ts +39 -16
  195. package/src/server/safe-response-headers.ts +162 -13
  196. package/src/server/turn-state-handles.ts +164 -0
  197. package/src/server/workflow-refusal.ts +2 -1
  198. package/src/service/ownership-mutation-lease.d.mts +11 -2
  199. package/src/service/ownership-mutation-lease.mjs +53 -11
  200. package/src/types/config.ts +2 -0
  201. package/src/update/index.ts +133 -25
  202. package/src/update/npm-install.d.mts +8 -0
  203. package/src/update/npm-install.mjs +20 -1
  204. package/src/update/orchestrated-admission.d.mts +4 -0
  205. package/src/update/orchestrated-admission.mjs +12 -0
  206. package/src/update/orchestrated-arguments.d.mts +12 -0
  207. package/src/update/orchestrated-arguments.mjs +64 -0
  208. package/src/update/orchestrated-child.d.mts +12 -0
  209. package/src/update/orchestrated-child.mjs +70 -0
  210. package/src/update/orchestrated-entry.ts +234 -0
  211. package/src/update/orchestrated-identity-probe-entry.ts +20 -0
  212. package/src/update/orchestrated-identity.ts +53 -0
  213. package/src/update/orchestrated-live-identity.ts +48 -0
  214. package/src/update/orchestrated-node-driver.mjs +99 -0
  215. package/src/update/orchestrated-outcome.d.mts +21 -0
  216. package/src/update/orchestrated-outcome.mjs +40 -0
  217. package/src/update/orchestrated-plan.ts +49 -0
  218. package/src/update/orchestrated-pnpm.d.mts +8 -0
  219. package/src/update/orchestrated-pnpm.mjs +33 -0
  220. package/src/update/orchestrated-session.d.mts +37 -0
  221. package/src/update/orchestrated-session.mjs +264 -0
  222. package/src/update/orchestrated-token.d.mts +10 -0
  223. package/src/update/orchestrated-token.mjs +21 -0
  224. package/src/update/orchestrated-token.ts +3 -0
  225. package/src/update/orchestrated-tree.d.mts +1 -0
  226. package/src/update/orchestrated-tree.mjs +14 -0
  227. package/src/update/pnpm-global-install.d.mts +6 -0
  228. package/src/update/pnpm-global-install.mjs +9 -2
  229. package/src/update/posix-install-supervisor-entry.mjs +6 -0
  230. package/src/update/posix-install-supervisor.d.mts +10 -0
  231. package/src/update/posix-install-supervisor.mjs +88 -0
  232. package/src/update/sync-update-mutex.d.mts +10 -0
  233. package/src/update/sync-update-mutex.mjs +164 -0
  234. package/src/update/sync-update-mutex.ts +1 -0
  235. package/src/update/test-home-guard.mjs +33 -0
  236. package/src/update/transactional-install.mjs +2 -2
  237. package/src/usage/error-detail.ts +225 -0
  238. package/src/usage/log.ts +60 -27
  239. package/src/web-search/loop.ts +31 -5
  240. package/gui/dist/assets/index-ebuVUtB7.js +0 -94
@@ -0,0 +1,197 @@
1
+ /** Pure Ollama native request and Chat response projections. */
2
+ type Obj = Record<string, unknown>;
3
+ export type OllamaKind = "chat" | "generate";
4
+ export class OllamaCodecError extends Error {
5
+ readonly status = 400;
6
+ readonly code = "invalid_ollama_request";
7
+ constructor(message: string) { super(message); this.name = "OllamaCodecError"; }
8
+ }
9
+ function fail(message: string): never { throw new OllamaCodecError(message); }
10
+ function object(value: unknown, label: string): Obj {
11
+ if (!value || typeof value !== "object" || Array.isArray(value)) fail(`Invalid ${label}`);
12
+ return value as Obj;
13
+ }
14
+ function keys(value: Obj, allowed: readonly string[], label: string): void {
15
+ if (Object.keys(value).some(k => !allowed.includes(k))) fail(`Unsupported ${label} field`);
16
+ }
17
+ function string(value: unknown, label: string, nonempty = false): string {
18
+ if (typeof value !== "string" || (nonempty && !value.trim())) fail(`Invalid ${label}`);
19
+ return value;
20
+ }
21
+ function image(value: unknown): Obj {
22
+ const data = string(value, "image", true);
23
+ if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(data)) fail("Invalid image encoding");
24
+ let bytes: Uint8Array;
25
+ // The pattern above already validated the whole string; decode only the signature window.
26
+ try { bytes = Uint8Array.from(atob(data.slice(0, 16)), c => c.charCodeAt(0)); } catch { return fail("Invalid image encoding"); }
27
+ const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
28
+ if ((data.endsWith("==") && (alphabet.indexOf(data[data.length - 3]!) & 15) !== 0) ||
29
+ (data.endsWith("=") && !data.endsWith("==") && (alphabet.indexOf(data[data.length - 2]!) & 3) !== 0)) fail("Invalid image encoding");
30
+ const starts = (...prefix: number[]) => prefix.every((b, i) => bytes[i] === b);
31
+ const mime = starts(137,80,78,71,13,10,26,10) ? "image/png" : starts(255,216,255) ? "image/jpeg" : starts(71,73,70,56,55,97) || starts(71,73,70,56,57,97) ? "image/gif" : starts(82,73,70,70) && bytes.length >= 12 && bytes[8] === 87 && bytes[9] === 69 && bytes[10] === 66 && bytes[11] === 80 ? "image/webp" : undefined;
32
+ if (!mime) fail("Unsupported image media");
33
+ return { type: "image_url", image_url: { url: `data:${mime};base64,${data}` } };
34
+ }
35
+ function content(text: unknown, images: unknown): unknown {
36
+ const value = string(text, "content");
37
+ if (images === undefined) return value;
38
+ if (!Array.isArray(images)) fail("Invalid images");
39
+ if (!images.length) return value;
40
+ return [{ type: "text", text: value }, ...images.map(image)];
41
+ }
42
+ function requestTools(value: unknown): Obj[] {
43
+ if (!Array.isArray(value)) fail("Invalid tools");
44
+ return value.map(item => {
45
+ const tool = object(item, "tool"); keys(tool, ["type", "function"], "tool");
46
+ if (tool.type !== "function") fail("Unsupported tool type");
47
+ const fn = object(tool.function, "tool function"); keys(fn, ["name", "description", "parameters"], "tool function");
48
+ string(fn.name, "tool name", true);
49
+ if (fn.description !== undefined) string(fn.description, "tool description");
50
+ object(fn.parameters, "tool parameters");
51
+ return { type: "function", function: { ...fn } };
52
+ });
53
+ }
54
+ export function ollamaToChat(kind: OllamaKind, input: unknown): Obj {
55
+ const body = object(input, "request");
56
+ keys(body, kind === "chat" ? ["model", "messages", "stream", "options", "format", "tools"] : ["model", "prompt", "system", "images", "stream", "options", "format"], "request");
57
+ const model = string(body.model, "model", true);
58
+ if (body.stream !== undefined && typeof body.stream !== "boolean") fail("Invalid stream");
59
+ const result: Obj = { model, stream: body.stream ?? true, messages: [] };
60
+ const messages: Obj[] = [];
61
+ if (kind === "generate") {
62
+ if (body.system !== undefined) messages.push({ role: "system", content: string(body.system, "system") });
63
+ messages.push({ role: "user", content: content(body.prompt, body.images) });
64
+ } else {
65
+ if (!Array.isArray(body.messages)) fail("Invalid messages");
66
+ const pending: { id: string; name: string; message: number; matched: boolean }[] = [];
67
+ body.messages.forEach((raw, mi) => {
68
+ const msg = object(raw, "message");
69
+ const role = string(msg.role, "role");
70
+ if (!["system", "user", "assistant", "tool"].includes(role)) fail("Unsupported role");
71
+ keys(msg, role === "user" ? ["role", "content", "images"] : role === "assistant" ? ["role", "content", "tool_calls"] : role === "tool" ? ["role", "content", "tool_name", "tool_call_id"] : ["role", "content"], "message");
72
+ const projected: Obj = { role, content: content(msg.content, role === "user" ? msg.images : undefined) };
73
+ if (role === "assistant" && msg.tool_calls !== undefined) {
74
+ if (!Array.isArray(msg.tool_calls)) fail("Invalid tool calls");
75
+ projected.tool_calls = msg.tool_calls.map((rawCall, ci) => {
76
+ // A replayed response call keeps the id this proxy returned for it; older clients send none.
77
+ const call = object(rawCall, "tool call"); keys(call, ["id", "function"], "tool call");
78
+ const fn = object(call.function, "tool call function"); keys(fn, ["name", "arguments"], "tool call function");
79
+ const name = string(fn.name, "tool name", true); const args = object(fn.arguments, "tool arguments");
80
+ const id = call.id === undefined ? `call_ollama_${mi}_${ci}` : string(call.id, "tool call id", true);
81
+ if (pending.some(p => p.id === id)) fail("Duplicate tool call id");
82
+ pending.push({ id, name, message: mi, matched: false });
83
+ return { id, type: "function", function: { name, arguments: JSON.stringify(args) } };
84
+ });
85
+ }
86
+ if (role === "tool") {
87
+ const id = msg.tool_call_id === undefined ? undefined : string(msg.tool_call_id, "tool call id", true);
88
+ const name = msg.tool_name === undefined ? undefined : string(msg.tool_name, "tool name", true);
89
+ const candidates = pending.filter(p => !p.matched && (id ? p.id === id && (!name || p.name === name) : name && p.name === name));
90
+ if (!id && name && candidates.length) {
91
+ const latestMessage = candidates[candidates.length - 1]!.message;
92
+ const latest = candidates.filter(p => p.message === latestMessage);
93
+ if (latest.length !== 1) fail("Ambiguous tool result");
94
+ latest[0]!.matched = true; projected.tool_call_id = latest[0]!.id;
95
+ } else {
96
+ if (candidates.length !== 1) fail("Unmatched tool result");
97
+ candidates[0]!.matched = true; projected.tool_call_id = candidates[0]!.id;
98
+ }
99
+ }
100
+ messages.push(projected);
101
+ });
102
+ if (body.tools !== undefined) result.tools = requestTools(body.tools);
103
+ }
104
+ result.messages = messages;
105
+ if (body.options !== undefined) {
106
+ const opts = object(body.options, "options"); keys(opts, ["temperature", "top_p", "num_predict", "stop"], "option");
107
+ for (const key of ["temperature", "top_p", "num_predict"] as const) if (opts[key] !== undefined) {
108
+ const v = opts[key]; if (typeof v !== "number" || !Number.isFinite(v) || (key === "num_predict" && (!Number.isSafeInteger(v) || v < 1))) fail("Invalid option");
109
+ result[key === "num_predict" ? "max_tokens" : key] = v;
110
+ }
111
+ if (opts.stop !== undefined) {
112
+ if (!Array.isArray(opts.stop) || !opts.stop.every(v => typeof v === "string")) fail("Invalid stop option");
113
+ result.stop = opts.stop;
114
+ }
115
+ }
116
+ if (body.format !== undefined) { if (body.format !== "json") fail("Unsupported format"); result.response_format = { type: "json_object" }; }
117
+ return result;
118
+ }
119
+
120
+ function usage(value: unknown): Obj {
121
+ if (!value || typeof value !== "object" || Array.isArray(value)) return {};
122
+ const u = value as Obj; const out: Obj = {};
123
+ if (typeof u.prompt_tokens === "number" && Number.isSafeInteger(u.prompt_tokens) && u.prompt_tokens >= 0) out.prompt_eval_count = u.prompt_tokens;
124
+ if (typeof u.completion_tokens === "number" && Number.isSafeInteger(u.completion_tokens) && u.completion_tokens >= 0) out.eval_count = u.completion_tokens;
125
+ return out;
126
+ }
127
+ function finish(value: unknown): string | undefined { return value === "stop" || value === "tool_calls" || value === "length" ? value : undefined; }
128
+ function nativeError(): Obj { return { error: "Chat completion did not finish successfully" }; }
129
+ function nativeCalls(calls: unknown): Obj[] {
130
+ if (!Array.isArray(calls)) throw new Error("Invalid Chat tool calls");
131
+ return calls.map((raw, i) => {
132
+ const call = object(raw, "response tool call"); const fn = object(call.function, "response tool function");
133
+ const name = string(fn.name, "response tool name", true);
134
+ let args: unknown; try { args = typeof fn.arguments === "string" ? JSON.parse(fn.arguments) : fn.arguments; } catch { throw new Error("Invalid Chat tool arguments"); }
135
+ object(args, "response tool arguments");
136
+ return { id: typeof call.id === "string" && call.id ? call.id : `call_ollama_response_${i}`, function: { name, arguments: args } };
137
+ });
138
+ }
139
+ function payload(kind: OllamaKind, text: string, calls?: Obj[]): Obj {
140
+ return kind === "chat" ? { message: { role: "assistant", content: text, ...(calls?.length ? { tool_calls: calls } : {}) } } : { response: text };
141
+ }
142
+ export function ollamaBufferedResponse(kind: OllamaKind, model: string, input: unknown, createdAt: string): Obj {
143
+ try {
144
+ const body = object(input, "Chat response"); if (!Array.isArray(body.choices) || body.choices.length !== 1) return nativeError();
145
+ const choice = object(body.choices[0], "Chat choice"); const reason = finish(choice.finish_reason); if (!reason) return nativeError();
146
+ const msg = object(choice.message, "Chat message");
147
+ if (msg.content != null && typeof msg.content !== "string") return nativeError();
148
+ const calls = msg.tool_calls === undefined ? undefined : nativeCalls(msg.tool_calls);
149
+ if (kind === "generate" && calls?.length) return nativeError();
150
+ return { model, created_at: createdAt, ...payload(kind, (msg.content as string | null | undefined) ?? "", calls), done: true, done_reason: reason, ...usage(body.usage) };
151
+ } catch { return nativeError(); }
152
+ }
153
+ type StreamTool = { id?: string; name: string; arguments: string };
154
+ export interface OllamaStreamState { kind: OllamaKind; model: string; createdAt: string; finishReason?: unknown; usage: Obj; tools: Map<number, StreamTool>; toolsEmitted: boolean; }
155
+ export function createOllamaStreamState(kind: OllamaKind, model: string, createdAt: string): OllamaStreamState {
156
+ return { kind, model, createdAt, usage: {}, tools: new Map(), toolsEmitted: false };
157
+ }
158
+ /** Consume one parsed Chat chunk. The caller owns settlement and may discard these records on failure. */
159
+ export function consumeOllamaChatChunk(state: OllamaStreamState, input: unknown): Obj[] {
160
+ const body = object(input, "Chat chunk");
161
+ if (body.error !== undefined) throw new Error("Chat stream error");
162
+ if (body.usage != null) state.usage = usage(body.usage);
163
+ if (!Array.isArray(body.choices) || body.choices.length > 1) throw new Error("Invalid Chat chunk choices");
164
+ if (!body.choices.length) return [];
165
+ const choice = object(body.choices[0], "Chat choice"); const delta = object(choice.delta, "Chat delta");
166
+ if (state.finishReason !== undefined) throw new Error("Chat chunk after finish");
167
+ const records: Obj[] = [];
168
+ if (delta.content != null) {
169
+ const text = string(delta.content, "Chat content");
170
+ if (text) records.push({ model: state.model, created_at: state.createdAt, ...payload(state.kind, text), done: false });
171
+ }
172
+ if (delta.tool_calls !== undefined) {
173
+ if (state.kind !== "chat" || !Array.isArray(delta.tool_calls) || state.toolsEmitted) throw new Error("Invalid Chat tool delta");
174
+ for (const raw of delta.tool_calls) {
175
+ const call = object(raw, "tool delta"); if (!Number.isSafeInteger(call.index) || (call.index as number) < 0) throw new Error("Invalid tool index");
176
+ const index = call.index as number; const current = state.tools.get(index) ?? { name: "", arguments: "" };
177
+ if (call.id !== undefined) { const id = string(call.id, "tool id", true); if (current.id && current.id !== id) throw new Error("Conflicting tool id"); current.id = id; }
178
+ if (call.function !== undefined) { const fn = object(call.function, "tool function"); if (fn.name !== undefined) current.name += string(fn.name, "tool name"); if (fn.arguments !== undefined) current.arguments += string(fn.arguments, "tool arguments"); }
179
+ state.tools.set(index, current);
180
+ }
181
+ }
182
+ if (choice.finish_reason != null) {
183
+ if (state.finishReason !== undefined) throw new Error("Duplicate Chat finish");
184
+ state.finishReason = choice.finish_reason;
185
+ if (choice.finish_reason === "tool_calls" && state.tools.size) {
186
+ const calls = [...state.tools.entries()].sort((a, b) => a[0] - b[0]).map(([index, tool]) => ({ id: tool.id ?? `call_ollama_response_${index}`, function: { name: tool.name, arguments: tool.arguments } }));
187
+ records.push({ model: state.model, created_at: state.createdAt, ...payload(state.kind, "", nativeCalls(calls)), done: false }); state.toolsEmitted = true;
188
+ }
189
+ }
190
+ return records;
191
+ }
192
+ /** Call only after the outer handler confirms successful upstream settlement. */
193
+ export function ollamaStreamTerminal(state: OllamaStreamState): Obj {
194
+ const reason = finish(state.finishReason);
195
+ if (!reason || (state.tools.size > 0 && !state.toolsEmitted)) return nativeError();
196
+ return { model: state.model, created_at: state.createdAt, ...payload(state.kind, ""), done: true, done_reason: reason, ...state.usage };
197
+ }
@@ -1,3 +1,4 @@
1
+ import { markProxyError, isProxyError } from "../../lib/client-error-body";
1
2
  /**
2
3
  * AdapterEvent -> client wire, without the internal Responses SSE (PF-09).
3
4
  *
@@ -525,6 +526,7 @@ export function encodeAdapterEventStream(
525
526
  reportUsage: false,
526
527
  durable: false,
527
528
  };
529
+ markProxyError(terminal);
528
530
  runBeforeTerminalSync(terminal);
529
531
  try {
530
532
  writer.terminal(terminal);
@@ -766,6 +768,7 @@ export function encodeAdapterEventStream(
766
768
  reportUsage: true,
767
769
  durable: true,
768
770
  };
771
+ if (isProxyError(event)) markProxyError(terminal);
769
772
  break;
770
773
  }
771
774
  case "error": {
@@ -845,6 +848,7 @@ export function encodeAdapterEventStream(
845
848
  reportUsage: true,
846
849
  durable: true,
847
850
  };
851
+ markProxyError(terminal);
848
852
  try {
849
853
  if (!await deliverTerminalGuarded(terminal)) return;
850
854
  } catch { /* see the catch path above */ }
@@ -1,3 +1,6 @@
1
+ import { isProxyError, hasProxyErrorMessage } from "../../lib/client-error-body";
2
+ import type { FailureVerdictSink } from "../../chat/failure-verdict";
3
+ import { ownedChatErrorResponse } from "../../chat/outbound";
1
4
  /**
2
5
  * AdapterEvent -> OpenAI Chat Completions, without the internal Responses SSE (PF-09).
3
6
  *
@@ -14,7 +17,6 @@ import type { TranslatorBudget } from "../../lib/translator-budget";
14
17
  import { responsesUsage } from "../../bridge/internal";
15
18
  import {
16
19
  appendedUtf8Bytes,
17
- chatCompletionsErrorResponse,
18
20
  chatCompletionsFailedResponse,
19
21
  chatCompletionsIncompleteOutcome,
20
22
  chatCompletionsStreamErrorPayload,
@@ -40,9 +42,11 @@ type Rec = Record<string, unknown>;
40
42
  export interface ChatCompletionEncodeOptions extends AdapterEventEncodeOptions {
41
43
  /** The model string the client asked for; every chunk carries it. */
42
44
  model: string;
45
+ failureVerdictSink?: FailureVerdictSink;
46
+ proxyErrorContext?: object;
43
47
  }
44
48
 
45
- function createChatCompletionWriter(sink: ClientFrameSink, model: string): ClientWireWriter {
49
+ function createChatCompletionWriter(sink: ClientFrameSink, model: string, failureVerdictSink?: FailureVerdictSink, proxyErrorContext?: object): ClientWireWriter {
46
50
  const id = completionId();
47
51
  const created = Math.floor(Date.now() / 1000);
48
52
  let started = false;
@@ -149,10 +153,11 @@ function createChatCompletionWriter(sink: ClientFrameSink, model: string): Clien
149
153
  terminated = true;
150
154
  failed = true;
151
155
  // A real error event, then an abnormal close without [DONE].
152
- const { payload, bounded } = chatCompletionsStreamErrorPayload(message, details);
156
+ const { payload, bounded, verdict } = chatCompletionsStreamErrorPayload(message, details);
153
157
  try {
154
158
  if (bounded) sink.emitBounded(dataFrame(payload));
155
159
  else sink.emit(dataFrame(payload), { terminal: true });
160
+ if (failureVerdictSink) { failureVerdictSink.verdict = verdict; failureVerdictSink.contract = details?.contract; failureVerdictSink.proxyAuthored = details?.proxyAuthored; }
156
161
  } catch {
157
162
  /* the converter drops an unadmittable error frame the same way */
158
163
  }
@@ -236,12 +241,13 @@ function createChatCompletionWriter(sink: ClientFrameSink, model: string): Clien
236
241
  if ("finishReason" in outcome) {
237
242
  finish(outcome.finishReason, terminal.usageOnWire ? responsesUsage(terminal.usage) : null);
238
243
  } else {
239
- fail(outcome.failMessage);
244
+ fail(outcome.originalFailMessage, { contract: "stream_incomplete",
245
+ proxyAuthored: isProxyError(terminal) });
240
246
  }
241
247
  return;
242
248
  }
243
249
  const failure = chatCompletionsFailedResponse((terminal.error ?? {}) as unknown as Rec);
244
- fail(failure.message, failure.details);
250
+ fail(failure.message, { ...failure.details, proxyAuthored: hasProxyErrorMessage(proxyErrorContext, failure.message, failure.details) });
245
251
  },
246
252
  overflow() {
247
253
  fail("upstream translation buffer exceeded the safe limit", {
@@ -259,7 +265,7 @@ export function encodeChatCompletionSse(
259
265
  events: AsyncIterable<AdapterEvent>,
260
266
  options: ChatCompletionEncodeOptions,
261
267
  ): ReadableStream<Uint8Array> {
262
- return encodeAdapterEventStream(events, sink => createChatCompletionWriter(sink, options.model), options);
268
+ return encodeAdapterEventStream(events, sink => createChatCompletionWriter(sink, options.model, options.failureVerdictSink, options.proxyErrorContext), options);
263
269
  }
264
270
 
265
271
  /**
@@ -271,18 +277,19 @@ export async function collectChatCompletionResponse(
271
277
  stream: ReadableStream<Uint8Array>,
272
278
  model: string,
273
279
  translatorBudget: TranslatorBudget,
280
+ failureVerdictSink?: FailureVerdictSink,
274
281
  ): Promise<Response> {
275
282
  try {
276
- const completion = await collectChatCompletion(stream, model, translatorBudget);
283
+ const completion = await collectChatCompletion(stream, model, translatorBudget, failureVerdictSink);
277
284
  return new Response(JSON.stringify(completion), {
278
285
  status: 200,
279
286
  headers: { "Content-Type": "application/json" },
280
287
  });
281
288
  } catch (err) {
282
289
  if (isChatCompletionsStreamError(err)) {
283
- return chatCompletionsErrorResponse(err.status, err.message, err.type, err.code);
290
+ return ownedChatErrorResponse(err.status, err.message, err.type, err.code, failureVerdictSink?.contract, failureVerdictSink?.proxyAuthored);
284
291
  }
285
- return chatCompletionsErrorResponse(502, err instanceof Error ? err.message : String(err), "server_error");
292
+ return ownedChatErrorResponse(502, err instanceof Error ? err.message : String(err), "server_error");
286
293
  }
287
294
  }
288
295
 
@@ -291,5 +298,6 @@ export function foldChatCompletion(
291
298
  events: AsyncIterable<AdapterEvent>,
292
299
  options: ChatCompletionEncodeOptions,
293
300
  ): Promise<Response> {
294
- return collectChatCompletionResponse(encodeChatCompletionSse(events, options), options.model, options.translatorBudget);
301
+ const failureVerdictSink: FailureVerdictSink = {};
302
+ return collectChatCompletionResponse(encodeChatCompletionSse(events, { ...options, failureVerdictSink }), options.model, options.translatorBudget, failureVerdictSink);
295
303
  }
@@ -65,6 +65,18 @@ export interface ProtocolTraceAttempt {
65
65
  const requestMarks = new WeakMap<object, EntryMark | BlockedMark>();
66
66
  const attemptMarks = new WeakMap<object, AttemptMark>();
67
67
  const shadowInputs = new WeakMap<object, ProtocolPlanInput>();
68
+ const suppressedContexts = new WeakSet<object>();
69
+
70
+ /** Suppress diagnostics for an internal protocol bridge while preserving request behavior. */
71
+ export function suppressProtocolTrace(logCtx: object): void {
72
+ suppressedContexts.add(logCtx);
73
+ requestMarks.delete(logCtx);
74
+ shadowInputs.delete(logCtx);
75
+ }
76
+
77
+ export function protocolTraceSuppressed(logCtx: object): boolean {
78
+ return suppressedContexts.has(logCtx);
79
+ }
68
80
 
69
81
  function boundedReasons(codes: Iterable<ProtocolReasonCode>): ProtocolReasonCode[] {
70
82
  const out: ProtocolReasonCode[] = [];
@@ -95,6 +107,7 @@ export function markProtocolEntry(
95
107
  logCtx: object,
96
108
  mark: { inbound: Protocol; lane: ProtocolLane; reasonCodes?: Iterable<ProtocolReasonCode>; features?: ProtocolFeatureSource },
97
109
  ): void {
110
+ if (protocolTraceSuppressed(logCtx)) return;
98
111
  try {
99
112
  requestMarks.set(logCtx, {
100
113
  kind: "entry",
@@ -110,6 +123,7 @@ export function markProtocolEntry(
110
123
 
111
124
  /** Features the request's entry or blocked mark recorded, for the shadow plan input. */
112
125
  export function protocolMarkFeatures(logCtx: object): readonly ProtocolFeature[] | undefined {
126
+ if (protocolTraceSuppressed(logCtx)) return undefined;
113
127
  try {
114
128
  const mark = requestMarks.get(logCtx);
115
129
  return mark ? [...mark.features] : undefined;
@@ -123,6 +137,7 @@ export function protocolMarkFeatures(logCtx: object): readonly ProtocolFeature[]
123
137
  * fixed vocabulary and the provider and model names the server already exposes, nothing else.
124
138
  */
125
139
  export function markProtocolShadowPlanInput(logCtx: object, input: ProtocolPlanInput): void {
140
+ if (protocolTraceSuppressed(logCtx)) return;
126
141
  try {
127
142
  shadowInputs.set(logCtx, input);
128
143
  } catch {
@@ -135,6 +150,7 @@ export function markProtocolBlocked(
135
150
  logCtx: object,
136
151
  mark: { inbound: Protocol; reasonCodes: Iterable<ProtocolReasonCode>; features?: ProtocolFeatureSource },
137
152
  ): void {
153
+ if (protocolTraceSuppressed(logCtx)) return;
138
154
  try {
139
155
  requestMarks.set(logCtx, {
140
156
  kind: "blocked",
@@ -152,6 +168,7 @@ export function markProtocolBlocked(
152
168
  * No-op without an entry mark, and a blocked mark is never reopened.
153
169
  */
154
170
  export function addProtocolEntryReason(logCtx: object, code: ProtocolReasonCode): void {
171
+ if (protocolTraceSuppressed(logCtx)) return;
155
172
  try {
156
173
  const mark = requestMarks.get(logCtx);
157
174
  if (mark?.kind !== "entry") return;
@@ -246,6 +263,7 @@ export function protocolTraceForRequest(
246
263
  logCtx: ProtocolTraceContext,
247
264
  attempts: readonly ProtocolTraceAttempt[] | undefined,
248
265
  ): ProtocolTraceV1 | undefined {
266
+ if (protocolTraceSuppressed(logCtx)) return undefined;
249
267
  const trace = observedTrace(logCtx, attempts);
250
268
  return trace ? withShadowPlan(logCtx, trace) : undefined;
251
269
  }
@@ -1,3 +1,4 @@
1
+ import { boundedServiceTier } from "../lib/response-service-tier";
1
2
  import type {
2
3
  AttemptTierOutcome,
3
4
  FastWire,
@@ -361,7 +362,7 @@ export function createAdapterTierMetadata(
361
362
  return {
362
363
  outcome,
363
364
  observeResponseServiceTier(value: unknown) {
364
- const sanitized = sanitizeLogMetadataString(value);
365
+ const sanitized = boundedServiceTier(value);
365
366
  if (sanitized) outcome.responseServiceTier = sanitized;
366
367
  },
367
368
  markResponseUnparseable() {},
@@ -413,7 +414,7 @@ export function createAdapterTierMetadata(
413
414
  }
414
415
  return;
415
416
  }
416
- const sanitized = sanitizeLogMetadataString(value);
417
+ const sanitized = boundedServiceTier(value);
417
418
  if (sanitized) outcome.responseServiceTier = sanitized;
418
419
  if (!responseCanConfirmFast) return;
419
420
  if (canonicalFromWire(context.fastWire, value) === "priority") {
@@ -19,9 +19,11 @@ function scopedFamily(row: Record<string, unknown>): "fable" | "opus" | "sonnet"
19
19
  }
20
20
 
21
21
  /**
22
- * A limits entry supersedes its legacy duplicate even when inactive or unusable.
23
- * Missing is_active preserves the older limits contract; an unknown flag is not active.
24
- * Unknown kinds/families leave usable shared windows intact but cannot clear family refusals.
22
+ * Recognized rows supersede legacy duplicates even when skipped or malformed; duplicates
23
+ * and malformed shared measurements make family enumeration incomplete. Shared finite
24
+ * measurements apply regardless of the binding flag, though an unknown flag also makes
25
+ * enumeration incomplete. A null legacy five_hour requires an explicitly active session.
26
+ * Scoped rows retain active-only behavior; unknown kinds/families cannot clear refusals.
25
27
  */
26
28
  export function anthropicUsageBuckets(body: Record<string, unknown>): {
27
29
  buckets: AnthropicUsageBucket[]; familyEnumeration: boolean;
@@ -37,6 +39,14 @@ export function anthropicUsageBuckets(body: Record<string, unknown>): {
37
39
  if (!row || !window) { familyEnumeration = false; continue; }
38
40
  if (represented.has(window)) { familyEnumeration = false; continue; }
39
41
  represented.add(window);
42
+ if (window === "5h" || window === "weekly") {
43
+ const flag = row.is_active;
44
+ if (flag !== undefined && typeof flag !== "boolean") familyEnumeration = false;
45
+ if (toFiniteNumber(row.percent) === undefined) { familyEnumeration = false; continue; }
46
+ if (window === "5h" && body.five_hour === null && flag !== true) continue;
47
+ selected.set(window, { window, row, percentField: "percent" });
48
+ continue;
49
+ }
40
50
  if (row.is_active === false) continue;
41
51
  if (row.is_active !== undefined && row.is_active !== true) { familyEnumeration = false; continue; }
42
52
  if (toFiniteNumber(row.percent) === undefined) familyEnumeration = false;
@@ -1125,18 +1125,17 @@ export function isCanonicalCommandCodeBaseUrl(baseUrl: string): boolean {
1125
1125
  return normalized === COMMAND_CODE_BASE_URL || normalized === `${COMMAND_CODE_BASE_URL}/provider/v1`;
1126
1126
  }
1127
1127
 
1128
- /** Prefer the nested `data` shell when the outer object is only an envelope. */
1129
- function unwrapKimiQuotaPayload(value: unknown): Record<string, unknown> | null {
1130
- const body = asRecord(value);
1131
- if (!body) return null;
1132
- const nested = asRecord(body.data);
1133
- if (!nested) return body;
1134
- // A null/non-usable outer field is a placeholder, not data — an envelope like
1135
- // { usage: null, data: { usage: {...} } } must still unwrap to the nested payload.
1128
+ /**
1129
+ * Pick the envelope carrying the legacy windows (weekly, 5h, total credits). The monthly
1130
+ * `usages` container is located separately, so its presence never hides a nested window.
1131
+ */
1132
+ function kimiLegacyWindowBody(root: Record<string, unknown>): Record<string, unknown> {
1133
+ const nested = asRecord(root.data);
1134
+ if (!nested) return root;
1135
+ // A null outer field is a placeholder, not data — { usage: null, data: { usage: {...} } } unwraps.
1136
1136
  const usable = (field: unknown): boolean => field !== undefined && field !== null;
1137
- const outerHasUsage = usable(body.usage) || usable(body.limits) || usable(body.totalQuota);
1138
- const nestedHasUsage = usable(nested.usage) || usable(nested.limits) || usable(nested.totalQuota);
1139
- return !outerHasUsage && nestedHasUsage ? nested : body;
1137
+ const hasLegacy = (body: Record<string, unknown>): boolean => usable(body.usage) || usable(body.limits) || usable(body.totalQuota);
1138
+ return !hasLegacy(root) && hasLegacy(nested) ? nested : root;
1140
1139
  }
1141
1140
 
1142
1141
  function kimiLimitLabel(item: Record<string, unknown>, detail: Record<string, unknown>): string {
@@ -1146,21 +1145,43 @@ function kimiLimitLabel(item: Record<string, unknown>, detail: Record<string, un
1146
1145
  .toLowerCase();
1147
1146
  }
1148
1147
 
1148
+ /** Highest percentage a window with a positive remainder may report; 100 means exhausted. */
1149
+ const KIMI_BELOW_EXHAUSTED_PERCENT = 99.999;
1150
+
1151
+ /**
1152
+ * Counter-derived percentage, or undefined when the counters cannot be trusted. Any
1153
+ * exhaustion signal (used at or past the limit, or remaining at or below zero) wins first,
1154
+ * even beside an impossible companion counter: dropping it would let an over-quota account
1155
+ * look available. Otherwise a negative used or a remaining above the limit is impossible.
1156
+ * A positive remainder never rounds up to exhaustion.
1157
+ */
1158
+ function kimiCounterPercent(row: Record<string, unknown>): number | undefined {
1159
+ const limit = toFiniteNumber(row.limit);
1160
+ if (limit === undefined || limit <= 0) return undefined;
1161
+ const used = toFiniteNumber(row.used);
1162
+ const remaining = toFiniteNumber(row.remaining);
1163
+ if ((used !== undefined && used >= limit) || (remaining !== undefined && remaining <= 0)) return 100;
1164
+ if ((used !== undefined && used < 0) || (remaining !== undefined && remaining > limit)) return undefined;
1165
+ if (used !== undefined) return (used / limit) * 100;
1166
+ if (remaining === undefined) return undefined;
1167
+ return Math.min(((limit - remaining) / limit) * 100, KIMI_BELOW_EXHAUSTED_PERCENT);
1168
+ }
1169
+
1170
+ /** Parse consumed usage, preferring valid counters, then a valid [0, 1] ratio, then direct percentages. */
1149
1171
  function parseKimiQuotaRow(value: unknown, resetFallback?: Record<string, unknown>): { percent: number; resetAt?: number } | null {
1150
1172
  const row = asRecord(value);
1151
1173
  if (!row) return null;
1152
1174
  const resetAt = quotaResetAt(row) ?? (resetFallback ? quotaResetAt(resetFallback) : undefined);
1153
- const limit = toFiniteNumber(row.limit);
1154
- if (limit !== undefined && limit > 0) {
1155
- let used = toFiniteNumber(row.used);
1156
- if (used === undefined) {
1157
- const remaining = toFiniteNumber(row.remaining);
1158
- if (remaining !== undefined) used = limit - remaining;
1159
- }
1160
- if (used !== undefined) {
1161
- const percent = normalizePercent((used / limit) * 100);
1162
- if (percent !== undefined) return { percent, ...(resetAt !== undefined ? { resetAt } : {}) };
1163
- }
1175
+ const counterPercent = kimiCounterPercent(row);
1176
+ if (counterPercent !== undefined) {
1177
+ const percent = normalizePercent(counterPercent);
1178
+ if (percent !== undefined) return { percent, ...(resetAt !== undefined ? { resetAt } : {}) };
1179
+ }
1180
+ // Newer Kimi responses expose utilization as a fraction in [0, 1].
1181
+ const ratio = toFiniteNumber(row.used_ratio ?? row.usedRatio);
1182
+ if (ratio !== undefined && ratio >= 0 && ratio <= 1) {
1183
+ const percent = normalizePercent(ratio * 100);
1184
+ if (percent !== undefined) return { percent, ...(resetAt !== undefined ? { resetAt } : {}) };
1164
1185
  }
1165
1186
  // Some payloads expose utilisation directly when limit/used arithmetic is absent.
1166
1187
  const direct = normalizePercent(row.utilization ?? row.percent ?? row.usedPercent ?? row.used_percent);
@@ -1181,11 +1202,22 @@ function isKimiWeeklyLimit(item: Record<string, unknown>, detail: Record<string,
1181
1202
  return /weekly|7\s*(?:d|day)/.test(kimiLimitLabel(item, detail));
1182
1203
  }
1183
1204
 
1205
+ /** Keep legacy windows separate and use the first parseable monthly row across supported envelopes. */
1184
1206
  function parseKimiQuotaPayload(value: unknown): ProviderQuota | null {
1185
- const body = unwrapKimiQuotaPayload(value);
1186
- if (!body) return null;
1207
+ const root = asRecord(value);
1208
+ if (!root) return null;
1209
+ const body = kimiLegacyWindowBody(root);
1187
1210
  let weekly = parseKimiQuotaRow(body.usage);
1188
1211
  const total = parseKimiQuotaRow(body.totalQuota);
1212
+ const nested = asRecord(root.data);
1213
+ let monthly: { percent: number; resetAt?: number } | null = null;
1214
+ for (const container of [root, nested, asRecord(root.quota), asRecord(nested?.quota)]) {
1215
+ const usages = asRecord(container?.usages);
1216
+ if (!usages) continue;
1217
+ monthly = parseKimiQuotaRow(usages.limit_month_total ?? usages.monthTotal)
1218
+ ?? parseKimiQuotaRow(usages.limit_month_code ?? usages.monthCode);
1219
+ if (monthly) break;
1220
+ }
1189
1221
  let fiveHour: { percent: number; resetAt?: number } | null = null;
1190
1222
  if (Array.isArray(body.limits)) {
1191
1223
  for (const rawItem of body.limits) {
@@ -1211,6 +1243,10 @@ function parseKimiQuotaPayload(value: unknown): ProviderQuota | null {
1211
1243
  weeklyPercent: weekly.percent,
1212
1244
  ...(weekly.resetAt !== undefined ? { weeklyResetAt: weekly.resetAt } : {}),
1213
1245
  } : {}),
1246
+ ...(monthly ? {
1247
+ monthlyPercent: monthly.percent,
1248
+ ...(monthly.resetAt !== undefined ? { monthlyResetAt: monthly.resetAt } : {}),
1249
+ } : {}),
1214
1250
  ...(total ? { customWindows: [{ label: "Total subscription credits", percent: total.percent, ...(total.resetAt !== undefined ? { resetAt: total.resetAt } : {}) }] } : {}),
1215
1251
  updatedAt: Date.now(),
1216
1252
  };
@@ -3,6 +3,7 @@ import { createHash } from "node:crypto";
3
3
  import type { GenerationContext } from "../lib/state-store-sweeper";
4
4
  import { carryReplayRefusal, isNonReplayableResponse, markResponseNonReplayable } from "../lib/upstream-retry";
5
5
  import { redactSecretString } from "../lib/redact";
6
+ import { errorDestinationText, providerErrorDestinationLabel } from "../lib/error-destination";
6
7
 
7
8
  export const REQUEST_PACING_MAX_QUEUE_DEPTH = 256;
8
9
  export const REQUEST_PACING_MAX_QUEUE_AGE_MS = 60_000;
@@ -49,8 +50,8 @@ export class RequestPacingQueueOverloadError extends Error {
49
50
  public readonly retryAfterSeconds: number,
50
51
  ) {
51
52
  super(reason === "keys_exhausted" ? "No eligible API key remains; wait for the provider cooldown before retrying" : reason === "queue_full"
52
- ? `request pacing queue for provider '${providerName}' is full`
53
- : `request pacing queue for provider '${providerName}' exceeded the maximum queued age`);
53
+ ? `request pacing queue for provider '${errorDestinationText(providerErrorDestinationLabel(providerName))}' is full`
54
+ : `request pacing queue for provider '${errorDestinationText(providerErrorDestinationLabel(providerName))}' exceeded the maximum queued age`);
54
55
  this.name = "RequestPacingQueueOverloadError";
55
56
  }
56
57
  }