@bitkyc08/opencodex 2.99.0-preview.20261108 → 2.100.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 (258) hide show
  1. package/AGENTS_INSTALL.md +19 -1
  2. package/README.md +37 -33
  3. package/SPONSORS.md +5 -1
  4. package/bin/ocx.mjs +1 -1
  5. package/gui/dist/assets/{App-C1hGwaN1.css → App-COo8Yopd.css} +1 -1
  6. package/gui/dist/assets/App-CitvEefy.js +59 -0
  7. package/gui/dist/assets/Tray-CeKVWQyB.js +1 -0
  8. package/gui/dist/assets/{brand-mark-DpiBmFGI.js → brand-mark-M92UnxX2.js} +1 -1
  9. package/gui/dist/assets/index-BP47do6-.js +94 -0
  10. package/gui/dist/assets/index-DmDooSAG.css +2 -0
  11. package/gui/dist/assets/{segment-thumb-features-JcszQ18T.js → segment-thumb-features-CcxZBnxv.js} +1 -1
  12. package/gui/dist/classic/assets/App-aYgIQMxI.js +58 -0
  13. package/gui/dist/classic/assets/{Tray-5v86r9Az.js → Tray-D8jfUXpQ.js} +1 -1
  14. package/gui/dist/classic/assets/index-CfGqHooN.js +94 -0
  15. package/gui/dist/classic/assets/quota-summary-R-ih7OlS.js +1 -0
  16. package/gui/dist/classic/index.html +1 -1
  17. package/gui/dist/index.html +2 -2
  18. package/native/ocx-child/Cargo.lock +215 -0
  19. package/native/ocx-child/Cargo.toml +31 -0
  20. package/native/ocx-child/src/cli.rs +1901 -0
  21. package/native/ocx-child/src/codex_home.rs +429 -0
  22. package/native/ocx-child/src/codex_settings.rs +231 -0
  23. package/native/ocx-child/src/daemon.rs +363 -0
  24. package/native/ocx-child/src/http_wire.rs +619 -0
  25. package/native/ocx-child/src/journal.rs +977 -0
  26. package/native/ocx-child/src/lib.rs +22 -0
  27. package/native/ocx-child/src/main.rs +9 -0
  28. package/native/ocx-child/src/parity.rs +102 -0
  29. package/native/ocx-child/src/platform.rs +1487 -0
  30. package/native/ocx-child/src/relay.rs +1288 -0
  31. package/native/ocx-child/src/relay_auth.rs +166 -0
  32. package/native/ocx-child/src/routes.rs +236 -0
  33. package/native/ocx-child/src/state.rs +190 -0
  34. package/native/ocx-child/src/test_hooks.rs +58 -0
  35. package/native/ocx-child/src/toml_surgeon.rs +1156 -0
  36. package/package.json +8 -2
  37. package/src/adapters/anthropic.ts +28 -2
  38. package/src/adapters/claude-cli/adapter.ts +5 -25
  39. package/src/adapters/claude-cli/instruction-images.ts +31 -0
  40. package/src/adapters/claude-cli/stable-replay.ts +42 -14
  41. package/src/adapters/claude-cli/usage-admission.ts +53 -64
  42. package/src/adapters/coding-agent/protocol.ts +1 -1
  43. package/src/adapters/devin/live-models.ts +3 -0
  44. package/src/adapters/google-http.ts +3 -0
  45. package/src/adapters/google-vertex-key-project.ts +10 -2
  46. package/src/adapters/google.ts +40 -8
  47. package/src/adapters/kiro/usage.ts +3 -13
  48. package/src/adapters/openai-responses/passthrough.ts +5 -2
  49. package/src/adapters/openai-responses/request-strips.ts +32 -0
  50. package/src/adapters/openai-responses/tool-schema.ts +9 -4
  51. package/src/chat/inbound.ts +1 -1
  52. package/src/claude/auth-detect.ts +26 -8
  53. package/src/claude/inbound-content-options.ts +18 -3
  54. package/src/claude/inbound.ts +5 -2
  55. package/src/cli/bundled-bun-diagnostic.ts +91 -0
  56. package/src/cli/capabilities-agents-routing.ts +2 -2
  57. package/src/cli/capabilities-observe-system.ts +60 -0
  58. package/src/cli/combo-input.ts +6 -0
  59. package/src/cli/combo-result.ts +1 -1
  60. package/src/cli/combo.ts +4 -0
  61. package/src/cli/config-command.ts +16 -2
  62. package/src/cli/connect.ts +12 -10
  63. package/src/cli/dispatch.ts +4 -0
  64. package/src/cli/doctor.ts +11 -0
  65. package/src/cli/help.ts +1 -0
  66. package/src/cli/init.ts +4 -0
  67. package/src/cli/link-enrollment.ts +152 -25
  68. package/src/cli/link-local-control.ts +49 -0
  69. package/src/cli/link.ts +37 -10
  70. package/src/cli/provider-lifecycle-runtime.ts +4 -2
  71. package/src/cli/registry.ts +13 -0
  72. package/src/cli/star-prompt.ts +2 -0
  73. package/src/cli/status.ts +1 -1
  74. package/src/cli/telemetry-profile.ts +206 -0
  75. package/src/cli/telemetry.ts +260 -0
  76. package/src/client/hub-relay.ts +26 -0
  77. package/src/client/machine-api.ts +3 -0
  78. package/src/clients/config-export/t3code.ts +44 -5
  79. package/src/clients/omo-role-models.ts +1 -1
  80. package/src/codex/catalog/bundled.ts +44 -2
  81. package/src/codex/runtime.ts +12 -0
  82. package/src/codex/shim-restore-lock.ts +67 -30
  83. package/src/combos/types.ts +21 -1
  84. package/src/config/atomic-write.ts +24 -1
  85. package/src/config/diagnostics.ts +15 -0
  86. package/src/config/live-reconcile.ts +17 -0
  87. package/src/config/load-degrade.ts +16 -0
  88. package/src/config/provider-validation.ts +29 -0
  89. package/src/config/schema/config-schema.ts +24 -0
  90. package/src/config/schema/leaf-validators.ts +17 -0
  91. package/src/config/telemetry-carry.ts +30 -0
  92. package/src/config.ts +13 -1
  93. package/src/generated/compatibility-version.json +405 -153
  94. package/src/generated/model-metadata.ts +3 -3
  95. package/src/integrations/aside-profiles.ts +7 -1
  96. package/src/integrations/owned-refresh.ts +9 -2
  97. package/src/lab/artifacts/secure-fs.ts +12 -11
  98. package/src/lab/fabric/scratch.ts +11 -9
  99. package/src/lab/public/community.ts +2 -3
  100. package/src/lab/public/file-safety.ts +18 -20
  101. package/src/lab/public/mutation-lock.ts +109 -337
  102. package/src/lab/public/private-file.ts +4 -3
  103. package/src/lib/bun-runtime-preflight.ts +1 -1
  104. package/src/lib/codex-catalog-read-only.ts +122 -0
  105. package/src/lib/config-ownership.ts +49 -18
  106. package/src/lib/errors.ts +11 -0
  107. package/src/lib/gcp-adc.ts +167 -48
  108. package/src/lib/jsonc.ts +4 -2
  109. package/src/lib/local-control-socket.ts +88 -0
  110. package/src/lib/privacy.ts +15 -0
  111. package/src/lib/provider-client-headers.ts +3 -0
  112. package/src/lib/token-estimate.ts +13 -2
  113. package/src/link/child-binary-source.ts +228 -0
  114. package/src/link/ephemeral-child.ts +45 -0
  115. package/src/link/remote-plant.ts +306 -0
  116. package/src/link/remote-windows.ts +59 -5
  117. package/src/link/ssh-argv.ts +71 -8
  118. package/src/link/status-projection.ts +2 -0
  119. package/src/link/store.ts +5 -1
  120. package/src/oauth/claude-keychain-read.ts +165 -0
  121. package/src/oauth/local-token-detect.ts +14 -17
  122. package/src/protocols/envelope.ts +20 -2
  123. package/src/providers/api-key-selection.ts +8 -2
  124. package/src/providers/claude-cli-usage-state.ts +93 -0
  125. package/src/providers/github-copilot-auto.ts +25 -5
  126. package/src/providers/meta-muse-plan-refusal.ts +94 -0
  127. package/src/providers/model-rename-fields.ts +3 -0
  128. package/src/providers/quota/vendor-probes-key.ts +23 -4
  129. package/src/providers/quota/vendor-probes-oauth.ts +59 -0
  130. package/src/providers/quota/workbuddy.ts +2 -2
  131. package/src/providers/quota.ts +25 -18
  132. package/src/providers/registry/entries-core.ts +7 -4
  133. package/src/providers/registry/entries-extended.ts +39 -7
  134. package/src/providers/registry/model-seeds.ts +10 -2
  135. package/src/providers/resolved-model-policy.ts +4 -0
  136. package/src/providers/routed-config.ts +30 -3
  137. package/src/providers/stale-no-vision-seed-migration.ts +7 -0
  138. package/src/providers/vertex-environment-authority.ts +50 -0
  139. package/src/providers/workbuddy-credit-balance.ts +38 -6
  140. package/src/quota-pool/assemble.ts +14 -6
  141. package/src/router.ts +1 -0
  142. package/src/server/adapter-resolve.ts +11 -6
  143. package/src/server/audio-dictation.ts +21 -7
  144. package/src/server/auth-cors.ts +19 -0
  145. package/src/server/background-lifecycle.ts +71 -0
  146. package/src/server/chat-completions.ts +2 -0
  147. package/src/server/claude-messages.ts +30 -3
  148. package/src/server/index/live-sideband.ts +15 -1
  149. package/src/server/index/local-control-listener.ts +515 -0
  150. package/src/server/index/optional-listeners.ts +21 -1
  151. package/src/server/index/serve-options.ts +4 -3
  152. package/src/server/index/websocket-handler.ts +2 -0
  153. package/src/server/index.ts +3 -0
  154. package/src/server/management/combo-routes.ts +6 -1
  155. package/src/server/management/context.ts +1 -0
  156. package/src/server/management/link-child-preflight.ts +74 -0
  157. package/src/server/management/link-enrollment.ts +190 -25
  158. package/src/server/management/link-route-state.ts +6 -1
  159. package/src/server/management/link-routes.ts +27 -9
  160. package/src/server/management/model-rows.ts +4 -0
  161. package/src/server/management/oauth-account-routes.ts +9 -4
  162. package/src/server/management/provider-overwrite-carry.ts +9 -2
  163. package/src/server/management/provider-routes.ts +14 -0
  164. package/src/server/management/provider-thinking-display-patch.ts +54 -0
  165. package/src/server/management/route-registry.ts +7 -0
  166. package/src/server/management/session-routes.ts +4 -1
  167. package/src/server/management/telemetry-routes.ts +37 -0
  168. package/src/server/management/usage-aggregate-cache.ts +290 -67
  169. package/src/server/management-api.ts +11 -2
  170. package/src/server/messages-native-preflight.ts +166 -0
  171. package/src/server/messages-native.ts +143 -43
  172. package/src/server/messages-shared.ts +14 -0
  173. package/src/server/proxy-liveness.ts +99 -6
  174. package/src/server/request-log-conversation.ts +18 -2
  175. package/src/server/request-log.ts +7 -2
  176. package/src/server/responses/adapter-continuation.ts +1 -0
  177. package/src/server/responses/adapter-dispatch.ts +11 -28
  178. package/src/server/responses/adapter-failure-message.ts +68 -0
  179. package/src/server/responses/combo-stream-preflight.ts +14 -2
  180. package/src/server/responses/core-auth.ts +26 -5
  181. package/src/server/responses/core-codex-account.ts +8 -1
  182. package/src/server/responses/core-combo-failure.ts +6 -1
  183. package/src/server/responses/core-combo-native.ts +150 -37
  184. package/src/server/responses/core-combo.ts +15 -9
  185. package/src/server/responses/core-normalize.ts +1 -0
  186. package/src/server/responses/core-options.ts +7 -1
  187. package/src/server/responses/core-pipeline.ts +21 -1
  188. package/src/server/responses/fetch-helpers.ts +5 -0
  189. package/src/server/responses/opengateway-completion-guard.ts +7 -3
  190. package/src/server/responses/passthrough-delivery.ts +8 -1
  191. package/src/server/responses/passthrough-error.ts +22 -0
  192. package/src/server/responses/request-prepare.ts +7 -1
  193. package/src/server/responses/request-transport.ts +28 -5
  194. package/src/server/responses/run-turn-execution.ts +13 -0
  195. package/src/server/sse-frame-buffer.ts +5 -0
  196. package/src/server/ws-bridge.ts +10 -0
  197. package/src/service/launchd.ts +78 -4
  198. package/src/telemetry/aggregate.ts +348 -0
  199. package/src/telemetry/consent.ts +128 -0
  200. package/src/telemetry/contract.ts +403 -0
  201. package/src/telemetry/disclosure.ts +19 -0
  202. package/src/telemetry/install-id.ts +226 -0
  203. package/src/telemetry/payload.ts +129 -0
  204. package/src/telemetry/profile/client.ts +150 -0
  205. package/src/telemetry/profile/contract.ts +288 -0
  206. package/src/telemetry/profile/disclosure.ts +14 -0
  207. package/src/telemetry/profile/paths.ts +13 -0
  208. package/src/telemetry/profile/routes.ts +99 -0
  209. package/src/telemetry/profile/service.ts +234 -0
  210. package/src/telemetry/profile/state.ts +189 -0
  211. package/src/telemetry/profile/sync.ts +276 -0
  212. package/src/telemetry/routes.ts +183 -0
  213. package/src/telemetry/scheduler.ts +386 -0
  214. package/src/telemetry/sender.ts +97 -0
  215. package/src/telemetry/state.ts +334 -0
  216. package/src/types/combo-jev-effort-mode.ts +11 -0
  217. package/src/types/config.ts +3 -0
  218. package/src/types/provider.ts +11 -1
  219. package/src/types/request.ts +2 -0
  220. package/src/types/telemetry.ts +15 -0
  221. package/src/types/wire.ts +10 -0
  222. package/src/types.ts +5 -0
  223. package/src/update/index.ts +7 -1
  224. package/src/update/minisign.d.mts +17 -0
  225. package/src/update/minisign.mjs +88 -0
  226. package/src/update/signed-supply.d.mts +59 -0
  227. package/src/update/signed-supply.mjs +289 -0
  228. package/src/update/trust-root.json +38 -0
  229. package/src/update/update-bounded-json.d.mts +4 -0
  230. package/src/update/update-bounded-json.mjs +113 -0
  231. package/src/update/update-bytes.d.mts +2 -0
  232. package/src/update/update-bytes.mjs +23 -0
  233. package/src/update/update-candidate-id.d.mts +2 -0
  234. package/src/update/update-candidate-id.mjs +23 -0
  235. package/src/update/update-checkpoint-codec.d.mts +33 -0
  236. package/src/update/update-checkpoint-codec.mjs +217 -0
  237. package/src/update/update-contracts.d.mts +24 -0
  238. package/src/update/update-contracts.mjs +207 -0
  239. package/src/update/update-signature-threshold.d.mts +12 -0
  240. package/src/update/update-signature-threshold.mjs +47 -0
  241. package/src/update/update-signed-version.d.mts +5 -0
  242. package/src/update/update-signed-version.mjs +25 -0
  243. package/src/update/update-trust-anchor.d.mts +2 -0
  244. package/src/update/update-trust-anchor.mjs +2 -0
  245. package/src/usage/cache-diagnostic.ts +195 -50
  246. package/src/usage/expected-prices.ts +20 -2
  247. package/src/usage/filter-index.ts +1469 -0
  248. package/src/usage/ledger-scanner.ts +245 -4
  249. package/src/usage/source-filter.ts +10 -2
  250. package/src/usage/summary.ts +16 -0
  251. package/src/web-search/parse.ts +244 -38
  252. package/gui/dist/assets/App-CWz0areF.js +0 -59
  253. package/gui/dist/assets/Tray-BosDoskm.js +0 -1
  254. package/gui/dist/assets/index-CAxjGWkm.js +0 -94
  255. package/gui/dist/assets/index-bnUWypyz.css +0 -2
  256. package/gui/dist/classic/assets/App-BOEsFmiN.js +0 -58
  257. package/gui/dist/classic/assets/index-ClZ_zZ77.js +0 -94
  258. package/gui/dist/classic/assets/quota-summary-xwvDtvu7.js +0 -1
@@ -0,0 +1,348 @@
1
+ import type { OcxUsage } from "../types";
2
+ import { normalizeCostTokens } from "../usage/cost";
3
+ import { scanUsageLedgerCooperatively } from "../usage/ledger-scanner";
4
+ import { isKnownRequestFailureCause, type PersistedUsageEntry } from "../usage/log";
5
+ import { classifyRequestOutcome } from "../usage/request-outcome";
6
+ import { usageDisplayTotalTokens } from "../usage/totals";
7
+ import {
8
+ TELEMETRY_MAX_MODELS,
9
+ parseCalendarUtcDayMs,
10
+ type TelemetryErrorsV1,
11
+ type TelemetryFailureCause,
12
+ type TelemetryModelFamily,
13
+ type TelemetryModelRowV1,
14
+ type TelemetryProviderKind,
15
+ type TelemetryRequestsV1,
16
+ } from "./contract";
17
+
18
+ export interface DisjointTelemetryTokens {
19
+ inputTokens: number;
20
+ cacheReadTokens: number;
21
+ cacheWriteTokens: number;
22
+ outputTokens: number;
23
+ reasoningTokens: number;
24
+ }
25
+
26
+ export interface DailyTelemetryAggregate {
27
+ day: string;
28
+ activeMinutes: number;
29
+ requests: TelemetryRequestsV1;
30
+ models: TelemetryModelRowV1[];
31
+ errors: TelemetryErrorsV1;
32
+ }
33
+
34
+ const ACCOUNT_SUFFIX_RE = /-(?:main|[po][0-9a-f]{6}|k[0-9a-f]{32})$/i;
35
+
36
+ const PRESET_TO_PROVIDER_KIND: Readonly<Record<string, TelemetryProviderKind>> = Object.freeze({
37
+ openai: "openai",
38
+ "openai-apikey": "openai",
39
+ "azure-openai": "openai",
40
+ chatgpt: "chatgpt",
41
+ "openai-multi": "chatgpt",
42
+ anthropic: "anthropic",
43
+ "anthropic-apikey": "anthropic",
44
+ "claude-subscription": "claude-subscription",
45
+ "claude-cli": "claude-subscription",
46
+ anthropic2: "claude-subscription",
47
+ google: "google",
48
+ "google-antigravity": "google",
49
+ vertex: "vertex",
50
+ "google-vertex": "vertex",
51
+ xai: "xai",
52
+ grok: "xai",
53
+ deepseek: "deepseek",
54
+ moonshot: "moonshot",
55
+ kimi: "moonshot",
56
+ "kimi-responses": "moonshot",
57
+ "kimi-code": "moonshot",
58
+ qwen: "qwen",
59
+ "qwen-cloud": "qwen",
60
+ alibaba: "qwen",
61
+ "alibaba-token-plan": "qwen",
62
+ "alibaba-token-plan-intl": "qwen",
63
+ zai: "zai",
64
+ "zhipu-bigmodel": "zai",
65
+ "zhipu-bigmodel-coding": "zai",
66
+ "zhipu-bigmodel-responses": "zai",
67
+ minimax: "minimax",
68
+ "minimax-cn": "minimax",
69
+ mistral: "mistral",
70
+ openrouter: "openrouter",
71
+ groq: "groq",
72
+ cerebras: "cerebras",
73
+ together: "together",
74
+ fireworks: "fireworks",
75
+ firepass: "fireworks",
76
+ cursor: "cursor",
77
+ kiro: "kiro",
78
+ copilot: "copilot",
79
+ "github-copilot": "copilot",
80
+ devin: "devin",
81
+ ollama: "ollama",
82
+ "ollama-cloud": "ollama",
83
+ lmstudio: "lmstudio",
84
+ "lm-studio": "lmstudio",
85
+ "local-other": "local-other",
86
+ vllm: "local-other",
87
+ litellm: "local-other",
88
+ });
89
+
90
+ /**
91
+ * Map a logged provider identifier to the closed vendor-level `TelemetryProviderKind` roster.
92
+ * Custom/unknown provider keys never enter the payload; they map to `"other"`.
93
+ */
94
+ export function classifyProviderKind(provider: string): TelemetryProviderKind {
95
+ const trimmed = provider.trim().toLowerCase();
96
+ if (!trimmed) return "other";
97
+ const direct = PRESET_TO_PROVIDER_KIND[trimmed];
98
+ if (direct) return direct;
99
+ const stripped = trimmed.replace(ACCOUNT_SUFFIX_RE, "");
100
+ return PRESET_TO_PROVIDER_KIND[stripped] ?? "other";
101
+ }
102
+
103
+ /**
104
+ * Map a model identifier to the closed `TelemetryModelFamily` prefix roster.
105
+ */
106
+ export function classifyModelFamily(model: string): TelemetryModelFamily {
107
+ const trimmed = model.trim().toLowerCase();
108
+ if (!trimmed) return "other";
109
+ const slash = trimmed.lastIndexOf("/");
110
+ const tail = (slash >= 0 ? trimmed.slice(slash + 1) : trimmed).replace(/--[a-z0-9_-]+$/, "");
111
+ if (!tail) return "other";
112
+ if (tail.startsWith("gpt-6")) return "gpt-6";
113
+ if (tail.startsWith("gpt-5")) return "gpt-5";
114
+ if (tail.startsWith("gpt-4")) return "gpt-4";
115
+ if (tail === "o-series" || /^o[134](?:$|[-._])/.test(tail)) return "o-series";
116
+ if (tail.startsWith("codex")) return "codex";
117
+ if (/^claude-(?:[\d.-]+-)?opus(?:$|[-._])/.test(tail) || tail === "claude-opus") return "claude-opus";
118
+ if (/^claude-(?:[\d.-]+-)?sonnet(?:$|[-._])/.test(tail) || tail === "claude-sonnet") return "claude-sonnet";
119
+ if (/^claude-(?:[\d.-]+-)?haiku(?:$|[-._])/.test(tail) || tail === "claude-haiku") return "claude-haiku";
120
+ if (tail.startsWith("gemini-3")) return "gemini-3";
121
+ if (tail.startsWith("gemini-2")) return "gemini-2";
122
+ if (tail.startsWith("grok")) return "grok";
123
+ if (tail.startsWith("deepseek")) return "deepseek";
124
+ if (tail.startsWith("qwen") || tail.startsWith("qwq")) return "qwen";
125
+ if (tail.startsWith("kimi") || tail.startsWith("moonshot")) return "kimi";
126
+ if (tail.startsWith("glm") || tail.startsWith("chatglm")) return "glm";
127
+ if (tail.startsWith("minimax") || tail.startsWith("abab")) return "minimax";
128
+ if (
129
+ tail.startsWith("mistral")
130
+ || tail.startsWith("mixtral")
131
+ || tail.startsWith("codestral")
132
+ || tail.startsWith("pixtral")
133
+ || tail.startsWith("ministral")
134
+ ) {
135
+ return "mistral";
136
+ }
137
+ if (tail.startsWith("llama") || tail.startsWith("meta-llama")) return "llama";
138
+ return "other";
139
+ }
140
+
141
+ function nonNegInt(value: number | undefined): number {
142
+ return typeof value === "number" && Number.isFinite(value) && value > 0
143
+ ? Math.floor(value)
144
+ : 0;
145
+ }
146
+
147
+ /**
148
+ * Decompose `OcxUsage` into five disjoint token buckets whose sum equals `usageDisplayTotalTokens(usage)`:
149
+ * - `inputTokens`: uncached input (`normalizeCostTokens`)
150
+ * - `cacheReadTokens`
151
+ * - `cacheWriteTokens`
152
+ * - `outputTokens`: output minus reasoning
153
+ * - `reasoningTokens`
154
+ */
155
+ export function disjointTelemetryTokens(usage: OcxUsage | undefined): DisjointTelemetryTokens {
156
+ if (!usage) {
157
+ return {
158
+ inputTokens: 0,
159
+ cacheReadTokens: 0,
160
+ cacheWriteTokens: 0,
161
+ outputTokens: 0,
162
+ reasoningTokens: 0,
163
+ };
164
+ }
165
+ const normalized = normalizeCostTokens(usage);
166
+ let inputTokens: number;
167
+ let cacheReadTokens: number;
168
+ let cacheWriteTokens: number;
169
+ let totalOutput: number;
170
+ if (normalized) {
171
+ inputTokens = nonNegInt(normalized.input);
172
+ cacheReadTokens = nonNegInt(normalized.cacheRead);
173
+ cacheWriteTokens = nonNegInt(normalized.cacheWrite);
174
+ totalOutput = nonNegInt(normalized.output);
175
+ } else {
176
+ const rawInput = nonNegInt(usage.inputTokens);
177
+ cacheWriteTokens = Math.min(rawInput, nonNegInt(usage.cacheCreationInputTokens));
178
+ cacheReadTokens = Math.min(
179
+ rawInput - cacheWriteTokens,
180
+ nonNegInt(usage.cacheReadInputTokens ?? usage.cachedInputTokens),
181
+ );
182
+ inputTokens = Math.max(0, rawInput - cacheReadTokens - cacheWriteTokens);
183
+ totalOutput = nonNegInt(usage.outputTokens);
184
+ }
185
+ const reasoningTokens = Math.min(totalOutput, nonNegInt(usage.reasoningOutputTokens));
186
+ const outputTokens = Math.max(0, totalOutput - reasoningTokens);
187
+ const displayTotal = nonNegInt(usageDisplayTotalTokens(usage));
188
+ const accounted = inputTokens + cacheReadTokens + cacheWriteTokens + outputTokens + reasoningTokens;
189
+ if (displayTotal > accounted) {
190
+ inputTokens += displayTotal - accounted;
191
+ }
192
+ return {
193
+ inputTokens,
194
+ cacheReadTokens,
195
+ cacheWriteTokens,
196
+ outputTokens,
197
+ reasoningTokens,
198
+ };
199
+ }
200
+
201
+ export interface DailyTelemetryAccumulator {
202
+ onEntry: (entry: PersistedUsageEntry) => void;
203
+ finalize: () => DailyTelemetryAggregate;
204
+ }
205
+
206
+ /**
207
+ * Streaming accumulator for one completed UTC day.
208
+ *
209
+ * Accumulates counters directly inside `onEntry` for entries falling inside `day`
210
+ * and never retains `PersistedUsageEntry` rows in memory.
211
+ */
212
+ export function createDailyTelemetryAccumulator(day: string): DailyTelemetryAccumulator {
213
+ const dayStartMs = parseCalendarUtcDayMs(day);
214
+ if (dayStartMs === null) {
215
+ throw new Error(`Invalid UTC day: ${day}`);
216
+ }
217
+ const dayEndMs = dayStartMs + 86_400_000;
218
+ const minutes = new Set<number>();
219
+ const requests: TelemetryRequestsV1 = {
220
+ completed: 0,
221
+ failed: 0,
222
+ incomplete: 0,
223
+ aborted: 0,
224
+ };
225
+ const modelMap = new Map<string, TelemetryModelRowV1>();
226
+ const errors: TelemetryErrorsV1 = {};
227
+
228
+ const addModelAttribution = (provider: string, model: string, usage: OcxUsage | undefined) => {
229
+ const providerKind = classifyProviderKind(provider);
230
+ const modelFamily = classifyModelFamily(model);
231
+ const key = `${providerKind}\0${modelFamily}`;
232
+ let row = modelMap.get(key);
233
+ if (!row) {
234
+ row = {
235
+ providerKind,
236
+ modelFamily,
237
+ requests: 0,
238
+ inputTokens: 0,
239
+ cacheReadTokens: 0,
240
+ cacheWriteTokens: 0,
241
+ outputTokens: 0,
242
+ reasoningTokens: 0,
243
+ };
244
+ modelMap.set(key, row);
245
+ }
246
+ row.requests += 1;
247
+ const tokens = disjointTelemetryTokens(usage);
248
+ row.inputTokens += tokens.inputTokens;
249
+ row.cacheReadTokens += tokens.cacheReadTokens;
250
+ row.cacheWriteTokens += tokens.cacheWriteTokens;
251
+ row.outputTokens += tokens.outputTokens;
252
+ row.reasoningTokens += tokens.reasoningTokens;
253
+ };
254
+
255
+ const onEntry = (entry: PersistedUsageEntry): void => {
256
+ if (typeof entry.timestamp !== "number" || entry.timestamp < dayStartMs || entry.timestamp >= dayEndMs) {
257
+ return;
258
+ }
259
+ minutes.add(Math.floor(entry.timestamp / 60_000));
260
+ const outcome = classifyRequestOutcome(entry);
261
+ requests[outcome] += 1;
262
+
263
+ const cause = entry.failureCause ?? entry.attempts?.at(-1)?.failureCause;
264
+ if (cause && isKnownRequestFailureCause(cause)) {
265
+ errors[cause as TelemetryFailureCause] = (errors[cause as TelemetryFailureCause] ?? 0) + 1;
266
+ }
267
+
268
+ if (entry.provider === "combo" && entry.attempts && entry.attempts.length > 0) {
269
+ for (const attempt of entry.attempts) {
270
+ addModelAttribution(attempt.provider, attempt.model, attempt.usage);
271
+ }
272
+ } else {
273
+ addModelAttribution(entry.provider, entry.model, entry.usage);
274
+ }
275
+ };
276
+
277
+ const finalize = (): DailyTelemetryAggregate => {
278
+ let models = [...modelMap.values()].sort((a, b) => {
279
+ if (b.requests !== a.requests) return b.requests - a.requests;
280
+ const aTokens = a.inputTokens + a.cacheReadTokens + a.cacheWriteTokens + a.outputTokens + a.reasoningTokens;
281
+ const bTokens = b.inputTokens + b.cacheReadTokens + b.cacheWriteTokens + b.outputTokens + b.reasoningTokens;
282
+ if (bTokens !== aTokens) return bTokens - aTokens;
283
+ if (a.providerKind !== b.providerKind) return a.providerKind.localeCompare(b.providerKind);
284
+ return a.modelFamily.localeCompare(b.modelFamily);
285
+ });
286
+
287
+ if (models.length > TELEMETRY_MAX_MODELS) {
288
+ const kept = models.slice(0, TELEMETRY_MAX_MODELS - 1);
289
+ const overflow = models.slice(TELEMETRY_MAX_MODELS - 1);
290
+ let otherRow = kept.find(r => r.providerKind === "other" && r.modelFamily === "other");
291
+ if (!otherRow) {
292
+ otherRow = {
293
+ providerKind: "other",
294
+ modelFamily: "other",
295
+ requests: 0,
296
+ inputTokens: 0,
297
+ cacheReadTokens: 0,
298
+ cacheWriteTokens: 0,
299
+ outputTokens: 0,
300
+ reasoningTokens: 0,
301
+ };
302
+ kept.push(otherRow);
303
+ }
304
+ for (const row of overflow) {
305
+ otherRow.requests += row.requests;
306
+ otherRow.inputTokens += row.inputTokens;
307
+ otherRow.cacheReadTokens += row.cacheReadTokens;
308
+ otherRow.cacheWriteTokens += row.cacheWriteTokens;
309
+ otherRow.outputTokens += row.outputTokens;
310
+ otherRow.reasoningTokens += row.reasoningTokens;
311
+ }
312
+ models = kept;
313
+ }
314
+
315
+ return {
316
+ day,
317
+ activeMinutes: Math.min(1440, minutes.size),
318
+ requests: { ...requests },
319
+ models,
320
+ errors: { ...errors },
321
+ };
322
+ };
323
+
324
+ return { onEntry, finalize };
325
+ }
326
+
327
+ export function aggregateTelemetryEntriesForDay(
328
+ day: string,
329
+ entries: Iterable<PersistedUsageEntry>,
330
+ ): DailyTelemetryAggregate {
331
+ const accumulator = createDailyTelemetryAccumulator(day);
332
+ for (const entry of entries) {
333
+ accumulator.onEntry(entry);
334
+ }
335
+ return accumulator.finalize();
336
+ }
337
+
338
+ export async function aggregateTelemetryForDay(
339
+ day: string,
340
+ options: { signal?: AbortSignal } = {},
341
+ ): Promise<DailyTelemetryAggregate> {
342
+ const accumulator = createDailyTelemetryAccumulator(day);
343
+ await scanUsageLedgerCooperatively({
344
+ signal: options.signal,
345
+ onEntry: accumulator.onEntry,
346
+ });
347
+ return accumulator.finalize();
348
+ }
@@ -0,0 +1,128 @@
1
+ import type { OcxTelemetryConfig, OcxTelemetryDecision } from "../types/telemetry";
2
+
3
+ export type TelemetryStatusReason =
4
+ | "env-ocx-telemetry"
5
+ | "env-do-not-track"
6
+ | "declined"
7
+ | "accepted"
8
+ | "default-on"
9
+ | "awaiting-notice";
10
+
11
+ export type TelemetryEnvOverride = "OCX_TELEMETRY" | "DO_NOT_TRACK" | null;
12
+
13
+ export interface TelemetryConsentStatus {
14
+ effective: boolean;
15
+ reason: TelemetryStatusReason;
16
+ decision: OcxTelemetryDecision | null;
17
+ decidedAt: string | null;
18
+ noticeShownAt: string | null;
19
+ envOverride: TelemetryEnvOverride;
20
+ }
21
+
22
+ const OCX_TELEMETRY_OFF_VALUES = new Set(["0", "false", "off", "no"]);
23
+ const DO_NOT_TRACK_ON_VALUES = new Set(["1", "true", "yes"]);
24
+
25
+ /**
26
+ * Consent lives only in the config's `telemetry` block. A whole config is never read as a block, so
27
+ * a top-level key such as `noticeShownAt` (which `ocx config set` can write) is not consent.
28
+ */
29
+ function extractTelemetryConfig(
30
+ input: { telemetry?: OcxTelemetryConfig | null } | null | undefined,
31
+ ): OcxTelemetryConfig | undefined {
32
+ if (!input || typeof input !== "object") return undefined;
33
+ const nested = (input as { telemetry?: unknown }).telemetry;
34
+ return nested && typeof nested === "object" && !Array.isArray(nested)
35
+ ? (nested as OcxTelemetryConfig)
36
+ : undefined;
37
+ }
38
+
39
+ function validIsoString(value: unknown): string | null {
40
+ if (typeof value !== "string") return null;
41
+ const trimmed = value.trim();
42
+ if (!trimmed || Number.isNaN(Date.parse(trimmed))) return null;
43
+ return trimmed;
44
+ }
45
+
46
+ export function isTelemetryEnvOptOut(env: Record<string, string | undefined> = process.env): TelemetryEnvOverride {
47
+ const ocx = env.OCX_TELEMETRY?.trim().toLowerCase();
48
+ if (ocx !== undefined && OCX_TELEMETRY_OFF_VALUES.has(ocx)) {
49
+ return "OCX_TELEMETRY";
50
+ }
51
+ const dnt = env.DO_NOT_TRACK?.trim().toLowerCase();
52
+ if (dnt !== undefined && DO_NOT_TRACK_ON_VALUES.has(dnt)) {
53
+ return "DO_NOT_TRACK";
54
+ }
55
+ return null;
56
+ }
57
+
58
+ export function resolveTelemetryStatus(
59
+ config: { telemetry?: OcxTelemetryConfig | null } | null | undefined,
60
+ env: Record<string, string | undefined> = process.env,
61
+ ): TelemetryConsentStatus {
62
+ const telemetry = extractTelemetryConfig(config);
63
+ const decision = telemetry?.decision === "accepted" || telemetry?.decision === "declined"
64
+ ? telemetry.decision
65
+ : null;
66
+ const decidedAt = validIsoString(telemetry?.decidedAt);
67
+ const noticeShownAt = validIsoString(telemetry?.noticeShownAt);
68
+ const envOverride = isTelemetryEnvOptOut(env);
69
+
70
+ if (envOverride === "OCX_TELEMETRY") {
71
+ return {
72
+ effective: false,
73
+ reason: "env-ocx-telemetry",
74
+ decision,
75
+ decidedAt,
76
+ noticeShownAt,
77
+ envOverride,
78
+ };
79
+ }
80
+ if (envOverride === "DO_NOT_TRACK") {
81
+ return {
82
+ effective: false,
83
+ reason: "env-do-not-track",
84
+ decision,
85
+ decidedAt,
86
+ noticeShownAt,
87
+ envOverride,
88
+ };
89
+ }
90
+ if (decision === "declined") {
91
+ return {
92
+ effective: false,
93
+ reason: "declined",
94
+ decision,
95
+ decidedAt,
96
+ noticeShownAt,
97
+ envOverride: null,
98
+ };
99
+ }
100
+ if (decision === "accepted") {
101
+ return {
102
+ effective: true,
103
+ reason: "accepted",
104
+ decision,
105
+ decidedAt,
106
+ noticeShownAt,
107
+ envOverride: null,
108
+ };
109
+ }
110
+ if (noticeShownAt !== null) {
111
+ return {
112
+ effective: true,
113
+ reason: "default-on",
114
+ decision: null,
115
+ decidedAt,
116
+ noticeShownAt,
117
+ envOverride: null,
118
+ };
119
+ }
120
+ return {
121
+ effective: false,
122
+ reason: "awaiting-notice",
123
+ decision: null,
124
+ decidedAt,
125
+ noticeShownAt: null,
126
+ envOverride: null,
127
+ };
128
+ }