@bitkyc08/opencodex 2.99.0 → 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-wzPe3R-J.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-fYEMxBaW.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-BqZueZOx.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-BepstgEN.js +0 -59
  253. package/gui/dist/assets/Tray-BinxbEZo.js +0 -1
  254. package/gui/dist/assets/index-C-9X2yaI.js +0 -94
  255. package/gui/dist/assets/index-bnUWypyz.css +0 -2
  256. package/gui/dist/classic/assets/App-DX88_GCW.js +0 -58
  257. package/gui/dist/classic/assets/index-fSP71pLB.js +0 -94
  258. package/gui/dist/classic/assets/quota-summary-y2fl0WBv.js +0 -1
@@ -0,0 +1,403 @@
1
+ /**
2
+ * Shared wire contract for anonymous daily product telemetry (schemaVersion 1).
3
+ *
4
+ * This module is deliberately import-free so both the local proxy and the Cloudflare Worker
5
+ * collector (services/telemetry-collector) can import it directly without pulling runtime
6
+ * dependencies across project boundaries.
7
+ */
8
+
9
+ export const TELEMETRY_SCHEMA_VERSION = 1 as const;
10
+ export const TELEMETRY_MAX_MODELS = 64;
11
+ export const TELEMETRY_VERSION_RE = /^\d+\.\d+\.\d+(-preview\.\d{8}(\.\d+)?)?$/;
12
+
13
+ export const PROVIDER_KINDS = Object.freeze([
14
+ "openai",
15
+ "chatgpt",
16
+ "anthropic",
17
+ "claude-subscription",
18
+ "google",
19
+ "vertex",
20
+ "xai",
21
+ "deepseek",
22
+ "moonshot",
23
+ "qwen",
24
+ "zai",
25
+ "minimax",
26
+ "mistral",
27
+ "openrouter",
28
+ "groq",
29
+ "cerebras",
30
+ "together",
31
+ "fireworks",
32
+ "cursor",
33
+ "kiro",
34
+ "copilot",
35
+ "devin",
36
+ "ollama",
37
+ "lmstudio",
38
+ "local-other",
39
+ "other",
40
+ ] as const);
41
+
42
+ export type TelemetryProviderKind = typeof PROVIDER_KINDS[number];
43
+ export type ProviderKind = TelemetryProviderKind;
44
+
45
+ export const MODEL_FAMILIES = Object.freeze([
46
+ "gpt-6",
47
+ "gpt-5",
48
+ "gpt-4",
49
+ "o-series",
50
+ "codex",
51
+ "claude-opus",
52
+ "claude-sonnet",
53
+ "claude-haiku",
54
+ "gemini-3",
55
+ "gemini-2",
56
+ "grok",
57
+ "deepseek",
58
+ "qwen",
59
+ "kimi",
60
+ "glm",
61
+ "minimax",
62
+ "mistral",
63
+ "llama",
64
+ "other",
65
+ ] as const);
66
+
67
+ export type TelemetryModelFamily = typeof MODEL_FAMILIES[number];
68
+ export type ModelFamily = TelemetryModelFamily;
69
+
70
+ export const INSTALL_CHANNELS = Object.freeze([
71
+ "npm-global",
72
+ "pnpm-global",
73
+ "bun-global",
74
+ "mise",
75
+ "standalone",
76
+ "desktop",
77
+ "docker",
78
+ "source",
79
+ "unknown",
80
+ ] as const);
81
+
82
+ export type TelemetryInstallChannel = typeof INSTALL_CHANNELS[number];
83
+ export type InstallChannel = TelemetryInstallChannel;
84
+
85
+ export const RELEASE_CHANNELS = Object.freeze([
86
+ "latest",
87
+ "preview",
88
+ ] as const);
89
+
90
+ export type TelemetryReleaseChannel = typeof RELEASE_CHANNELS[number];
91
+ export type ReleaseChannel = TelemetryReleaseChannel;
92
+
93
+ export const OS_VALUES = Object.freeze([
94
+ "darwin",
95
+ "linux",
96
+ "win32",
97
+ "other",
98
+ ] as const);
99
+
100
+ export type TelemetryOs = typeof OS_VALUES[number];
101
+
102
+ export const ARCH_VALUES = Object.freeze([
103
+ "x64",
104
+ "arm64",
105
+ "other",
106
+ ] as const);
107
+
108
+ export type TelemetryArch = typeof ARCH_VALUES[number];
109
+
110
+ export const REQUEST_OUTCOME_CLASSES = Object.freeze([
111
+ "completed",
112
+ "failed",
113
+ "incomplete",
114
+ "aborted",
115
+ ] as const);
116
+ export const TELEMETRY_OUTCOME_CLASSES = REQUEST_OUTCOME_CLASSES;
117
+ export type TelemetryOutcomeClass = typeof REQUEST_OUTCOME_CLASSES[number];
118
+
119
+ export const REQUEST_FAILURE_CAUSES = Object.freeze([
120
+ "transport-unsent",
121
+ "transport-ambiguous",
122
+ "upstream-declined",
123
+ "rate-limit",
124
+ "quota-exhausted",
125
+ "credential-rejected",
126
+ "policy-refusal",
127
+ "parameter-rejected",
128
+ "ciphertext-refusal",
129
+ "payload-too-large",
130
+ "payload-rejected",
131
+ "upstream-fault",
132
+ "empty-output",
133
+ "client-cancelled",
134
+ "local-refusal",
135
+ ] as const);
136
+ export const TELEMETRY_FAILURE_CAUSES = REQUEST_FAILURE_CAUSES;
137
+ export type TelemetryFailureCause = typeof REQUEST_FAILURE_CAUSES[number];
138
+
139
+ export interface TelemetryEnvV1 {
140
+ version: string;
141
+ releaseChannel: TelemetryReleaseChannel;
142
+ os: TelemetryOs;
143
+ arch: TelemetryArch;
144
+ installChannel: TelemetryInstallChannel;
145
+ }
146
+
147
+ export interface TelemetryRequestsV1 {
148
+ completed: number;
149
+ failed: number;
150
+ incomplete: number;
151
+ aborted: number;
152
+ }
153
+
154
+ export interface TelemetryModelRowV1 {
155
+ providerKind: TelemetryProviderKind;
156
+ modelFamily: TelemetryModelFamily;
157
+ requests: number;
158
+ inputTokens: number;
159
+ cacheReadTokens: number;
160
+ cacheWriteTokens: number;
161
+ outputTokens: number;
162
+ reasoningTokens: number;
163
+ }
164
+
165
+ export type TelemetryErrorsV1 = Partial<Record<TelemetryFailureCause, number>>;
166
+
167
+ export interface TelemetryPayloadV1 {
168
+ schemaVersion: typeof TELEMETRY_SCHEMA_VERSION;
169
+ batchId: string;
170
+ day: string;
171
+ installDayHash: string;
172
+ newInstall: boolean;
173
+ env: TelemetryEnvV1;
174
+ activeMinutes: number;
175
+ requests: TelemetryRequestsV1;
176
+ models: TelemetryModelRowV1[];
177
+ errors: TelemetryErrorsV1;
178
+ }
179
+
180
+ const PROVIDER_KIND_SET: ReadonlySet<string> = new Set(PROVIDER_KINDS);
181
+ const MODEL_FAMILY_SET: ReadonlySet<string> = new Set(MODEL_FAMILIES);
182
+ const INSTALL_CHANNEL_SET: ReadonlySet<string> = new Set(INSTALL_CHANNELS);
183
+ const RELEASE_CHANNEL_SET: ReadonlySet<string> = new Set(RELEASE_CHANNELS);
184
+ const OS_VALUE_SET: ReadonlySet<string> = new Set(OS_VALUES);
185
+ const ARCH_VALUE_SET: ReadonlySet<string> = new Set(ARCH_VALUES);
186
+ const FAILURE_CAUSE_SET: ReadonlySet<string> = new Set(REQUEST_FAILURE_CAUSES);
187
+
188
+ const TOP_LEVEL_KEYS = [
189
+ "schemaVersion",
190
+ "batchId",
191
+ "day",
192
+ "installDayHash",
193
+ "newInstall",
194
+ "env",
195
+ "activeMinutes",
196
+ "requests",
197
+ "models",
198
+ "errors",
199
+ ] as const;
200
+ const ENV_KEYS = ["version", "releaseChannel", "os", "arch", "installChannel"] as const;
201
+ const REQUESTS_KEYS = ["completed", "failed", "incomplete", "aborted"] as const;
202
+ const MODEL_ROW_KEYS = [
203
+ "providerKind",
204
+ "modelFamily",
205
+ "requests",
206
+ "inputTokens",
207
+ "cacheReadTokens",
208
+ "cacheWriteTokens",
209
+ "outputTokens",
210
+ "reasoningTokens",
211
+ ] as const;
212
+
213
+ const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
214
+ const HEX64_RE = /^[0-9a-f]{64}$/;
215
+ const DAY_RE = /^(\d{4})-(\d{2})-(\d{2})$/;
216
+ const DAY_MS = 86_400_000;
217
+ const MAX_REQUEST_COUNT = 10_000_000;
218
+ const MAX_TOKEN_COUNT = 1_000_000_000_000;
219
+
220
+ function isPlainRecord(value: unknown): value is Record<string, unknown> {
221
+ if (!value || typeof value !== "object" || Array.isArray(value)) return false;
222
+ const proto = Object.getPrototypeOf(value);
223
+ return proto === Object.prototype || proto === null;
224
+ }
225
+
226
+ function hasExactKeys(record: Record<string, unknown>, expected: readonly string[]): boolean {
227
+ const keys = Object.keys(record);
228
+ if (keys.length !== expected.length) return false;
229
+ for (const key of expected) {
230
+ if (!Object.hasOwn(record, key)) return false;
231
+ }
232
+ return true;
233
+ }
234
+
235
+ function isBoundedInt(value: unknown, max: number): value is number {
236
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 && value <= max;
237
+ }
238
+
239
+ export function parseCalendarUtcDayMs(day: string): number | null {
240
+ const match = DAY_RE.exec(day);
241
+ if (!match) return null;
242
+ const year = Number(match[1]);
243
+ const month = Number(match[2]);
244
+ const date = Number(match[3]);
245
+ const ms = Date.UTC(year, month - 1, date);
246
+ const check = new Date(ms);
247
+ if (
248
+ check.getUTCFullYear() !== year
249
+ || check.getUTCMonth() !== month - 1
250
+ || check.getUTCDate() !== date
251
+ ) {
252
+ return null;
253
+ }
254
+ return ms;
255
+ }
256
+
257
+ export function validateTelemetryPayloadV1(
258
+ input: unknown,
259
+ now: number = Date.now(),
260
+ ): { ok: true; payload: TelemetryPayloadV1 } | { ok: false; error: string } {
261
+ if (!isPlainRecord(input)) return { ok: false, error: "payload must be a plain object" };
262
+ if (!hasExactKeys(input, TOP_LEVEL_KEYS)) {
263
+ return { ok: false, error: "payload has missing or unexpected top-level keys" };
264
+ }
265
+ if (input.schemaVersion !== TELEMETRY_SCHEMA_VERSION) {
266
+ return { ok: false, error: "schemaVersion must be 1" };
267
+ }
268
+ if (typeof input.batchId !== "string" || !UUID_RE.test(input.batchId)) {
269
+ return { ok: false, error: "batchId must be a UUID" };
270
+ }
271
+ if (typeof input.day !== "string") {
272
+ return { ok: false, error: "day must be a YYYY-MM-DD string" };
273
+ }
274
+ const dayMs = parseCalendarUtcDayMs(input.day);
275
+ if (dayMs === null) {
276
+ return { ok: false, error: "day must be a valid calendar date in YYYY-MM-DD format" };
277
+ }
278
+ const nowDate = new Date(now);
279
+ const todayUtcMs = Date.UTC(nowDate.getUTCFullYear(), nowDate.getUTCMonth(), nowDate.getUTCDate());
280
+ if (dayMs < todayUtcMs - 7 * DAY_MS || dayMs > todayUtcMs + DAY_MS) {
281
+ return { ok: false, error: "day must be within [today - 7d, today + 1d]" };
282
+ }
283
+ if (typeof input.installDayHash !== "string" || !HEX64_RE.test(input.installDayHash)) {
284
+ return { ok: false, error: "installDayHash must be 64 lowercase hex characters" };
285
+ }
286
+ if (typeof input.newInstall !== "boolean") {
287
+ return { ok: false, error: "newInstall must be a boolean" };
288
+ }
289
+ if (!isPlainRecord(input.env) || !hasExactKeys(input.env, ENV_KEYS)) {
290
+ return { ok: false, error: "env has missing or unexpected keys" };
291
+ }
292
+ const env = input.env;
293
+ if (typeof env.version !== "string" || !TELEMETRY_VERSION_RE.test(env.version)) {
294
+ return { ok: false, error: "env.version must match strict semver without build metadata" };
295
+ }
296
+ if (typeof env.releaseChannel !== "string" || !RELEASE_CHANNEL_SET.has(env.releaseChannel)) {
297
+ return { ok: false, error: "env.releaseChannel is not in RELEASE_CHANNELS" };
298
+ }
299
+ if (typeof env.os !== "string" || !OS_VALUE_SET.has(env.os)) {
300
+ return { ok: false, error: "env.os is not in OS_VALUES" };
301
+ }
302
+ if (typeof env.arch !== "string" || !ARCH_VALUE_SET.has(env.arch)) {
303
+ return { ok: false, error: "env.arch is not in ARCH_VALUES" };
304
+ }
305
+ if (typeof env.installChannel !== "string" || !INSTALL_CHANNEL_SET.has(env.installChannel)) {
306
+ return { ok: false, error: "env.installChannel is not in INSTALL_CHANNELS" };
307
+ }
308
+ if (!isBoundedInt(input.activeMinutes, 1440)) {
309
+ return { ok: false, error: "activeMinutes must be an integer between 0 and 1440" };
310
+ }
311
+ if (!isPlainRecord(input.requests) || !hasExactKeys(input.requests, REQUESTS_KEYS)) {
312
+ return { ok: false, error: "requests has missing or unexpected keys" };
313
+ }
314
+ for (const key of REQUESTS_KEYS) {
315
+ if (!isBoundedInt(input.requests[key], MAX_REQUEST_COUNT)) {
316
+ return { ok: false, error: `requests.${key} must be a bounded non-negative integer` };
317
+ }
318
+ }
319
+ if (!Array.isArray(input.models) || input.models.length > TELEMETRY_MAX_MODELS) {
320
+ return { ok: false, error: `models must be an array of at most ${TELEMETRY_MAX_MODELS} rows` };
321
+ }
322
+ const seenPairs = new Set<string>();
323
+ const models: TelemetryModelRowV1[] = [];
324
+ for (const row of input.models) {
325
+ if (!isPlainRecord(row) || !hasExactKeys(row, MODEL_ROW_KEYS)) {
326
+ return { ok: false, error: "models row has missing or unexpected keys" };
327
+ }
328
+ if (typeof row.providerKind !== "string" || !PROVIDER_KIND_SET.has(row.providerKind)) {
329
+ return { ok: false, error: "models row providerKind is not in PROVIDER_KINDS" };
330
+ }
331
+ if (typeof row.modelFamily !== "string" || !MODEL_FAMILY_SET.has(row.modelFamily)) {
332
+ return { ok: false, error: "models row modelFamily is not in MODEL_FAMILIES" };
333
+ }
334
+ const pairKey = `${row.providerKind}\0${row.modelFamily}`;
335
+ if (seenPairs.has(pairKey)) {
336
+ return { ok: false, error: "models rows must have unique (providerKind, modelFamily) pairs" };
337
+ }
338
+ seenPairs.add(pairKey);
339
+ if (!isBoundedInt(row.requests, MAX_REQUEST_COUNT)) {
340
+ return { ok: false, error: "models row requests must be a bounded non-negative integer" };
341
+ }
342
+ for (const tokenField of [
343
+ "inputTokens",
344
+ "cacheReadTokens",
345
+ "cacheWriteTokens",
346
+ "outputTokens",
347
+ "reasoningTokens",
348
+ ] as const) {
349
+ if (!isBoundedInt(row[tokenField], MAX_TOKEN_COUNT)) {
350
+ return { ok: false, error: `models row ${tokenField} must be a bounded non-negative integer` };
351
+ }
352
+ }
353
+ models.push({
354
+ providerKind: row.providerKind as TelemetryProviderKind,
355
+ modelFamily: row.modelFamily as TelemetryModelFamily,
356
+ requests: row.requests,
357
+ inputTokens: row.inputTokens as number,
358
+ cacheReadTokens: row.cacheReadTokens as number,
359
+ cacheWriteTokens: row.cacheWriteTokens as number,
360
+ outputTokens: row.outputTokens as number,
361
+ reasoningTokens: row.reasoningTokens as number,
362
+ });
363
+ }
364
+ if (!isPlainRecord(input.errors)) {
365
+ return { ok: false, error: "errors must be a plain object" };
366
+ }
367
+ const errors: TelemetryErrorsV1 = {};
368
+ for (const [cause, count] of Object.entries(input.errors)) {
369
+ if (!FAILURE_CAUSE_SET.has(cause)) {
370
+ return { ok: false, error: `errors key "${cause}" is not in REQUEST_FAILURE_CAUSES` };
371
+ }
372
+ if (!isBoundedInt(count, MAX_REQUEST_COUNT)) {
373
+ return { ok: false, error: `errors["${cause}"] must be a bounded non-negative integer` };
374
+ }
375
+ errors[cause as TelemetryFailureCause] = count;
376
+ }
377
+ return {
378
+ ok: true,
379
+ payload: {
380
+ schemaVersion: TELEMETRY_SCHEMA_VERSION,
381
+ batchId: input.batchId.toLowerCase(),
382
+ day: input.day,
383
+ installDayHash: input.installDayHash,
384
+ newInstall: input.newInstall,
385
+ env: {
386
+ version: env.version,
387
+ releaseChannel: env.releaseChannel as TelemetryReleaseChannel,
388
+ os: env.os as TelemetryOs,
389
+ arch: env.arch as TelemetryArch,
390
+ installChannel: env.installChannel as TelemetryInstallChannel,
391
+ },
392
+ activeMinutes: input.activeMinutes,
393
+ requests: {
394
+ completed: input.requests.completed as number,
395
+ failed: input.requests.failed as number,
396
+ incomplete: input.requests.incomplete as number,
397
+ aborted: input.requests.aborted as number,
398
+ },
399
+ models,
400
+ errors,
401
+ },
402
+ };
403
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The telemetry disclosure a person sees before sending can start: the interactive CLI prompt prints
3
+ * these points, and the Windows installer dialog (`desktop/src-tauri/wix/telemetry-choice.wxs`,
4
+ * generated by `desktop/scripts/telemetry-installer-dialog.ts`) shows the same text with a link to
5
+ * the full policy. Import-free so the installer generator can read it without loading the CLI.
6
+ */
7
+
8
+ /** The published privacy policy, linked from the installer dialog and the code signing policy. */
9
+ export const TELEMETRY_POLICY_URL = "https://opencodex.me/reference/code-signing/#privacy";
10
+
11
+ export const TELEMETRY_DISCLOSURE_HEADING = "Anonymous daily usage telemetry:";
12
+
13
+ export const TELEMETRY_DISCLOSURE_POINTS: readonly string[] = Object.freeze([
14
+ "Sent daily to telemetry.opencodex.me: a daily-changing install hash, version, release channel, OS/arch, install channel, first-report flag, provider kind, model family, active minutes, request/token/error counts.",
15
+ "Never sent: prompts, model outputs, request/response bodies, API keys, OAuth tokens, account IDs or emails, custom base URLs, hostnames, file paths, or error text.",
16
+ "Public aggregates: project-wide totals are published at opencodex.me/dashboard.",
17
+ "See the exact payload: ocx telemetry status --json",
18
+ "Turn off anytime: ocx telemetry disable, the Proxy details toggle in the dashboard, or OCX_TELEMETRY=0 / DO_NOT_TRACK=1 in the proxy's own environment (a service does not see variables set in your shell).",
19
+ ]);
@@ -0,0 +1,226 @@
1
+ import { createHash, randomBytes, randomUUID } from "node:crypto";
2
+ import {
3
+ closeSync,
4
+ constants as fsConstants,
5
+ fchmodSync,
6
+ fsyncSync,
7
+ linkSync,
8
+ lstatSync,
9
+ mkdirSync,
10
+ openSync,
11
+ readFileSync,
12
+ unlinkSync,
13
+ writeSync,
14
+ } from "node:fs";
15
+ import { dirname, join } from "node:path";
16
+ import { atomicWriteFileNoFollow } from "../config/atomic-write";
17
+ import { getConfigDir } from "../config/paths";
18
+ import { recordOwnedConfigPath } from "../lib/config-ownership";
19
+
20
+ export const TELEMETRY_ID_FILE = "telemetry-id.json";
21
+ export const TELEMETRY_PREVIEW_HASH_PLACEHOLDER = "0".repeat(64);
22
+
23
+ export interface TelemetryInstallIdentity {
24
+ installId: string;
25
+ createdAt: string;
26
+ firstReportSent?: boolean;
27
+ }
28
+
29
+ const UUID_V4_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
30
+ const NOFOLLOW = process.platform === "win32" ? 0 : fsConstants.O_NOFOLLOW;
31
+ const PUBLISHED_READ_ATTEMPTS = 50;
32
+ const PUBLISHED_READ_INTERVAL_MS = 10;
33
+
34
+ let memo: { path: string; identity: TelemetryInstallIdentity } | null = null;
35
+
36
+ export function telemetryIdPath(configDir = getConfigDir()): string {
37
+ return join(configDir, TELEMETRY_ID_FILE);
38
+ }
39
+
40
+ function parseTelemetryId(text: string): TelemetryInstallIdentity | null {
41
+ try {
42
+ const raw = JSON.parse(text) as Record<string, unknown>;
43
+ if (raw?.version !== 1 || typeof raw.installId !== "string" || typeof raw.createdAt !== "string") {
44
+ return null;
45
+ }
46
+ if (!UUID_V4_RE.test(raw.installId) || Number.isNaN(Date.parse(raw.createdAt))) {
47
+ return null;
48
+ }
49
+ return {
50
+ installId: raw.installId.toLowerCase(),
51
+ createdAt: raw.createdAt,
52
+ firstReportSent: raw.firstReportSent === true,
53
+ };
54
+ } catch {
55
+ return null;
56
+ }
57
+ }
58
+
59
+ /** Read-only: returns the persisted telemetry install identity, or null when absent/invalid. */
60
+ export function readTelemetryInstallId(configDir = getConfigDir()): TelemetryInstallIdentity | null {
61
+ const path = telemetryIdPath(configDir);
62
+ try {
63
+ const stat = lstatSync(path);
64
+ if (!stat.isFile() || stat.isSymbolicLink() || stat.size > 1024) {
65
+ if (memo?.path === path) memo = null;
66
+ return null;
67
+ }
68
+ } catch {
69
+ // Another process (`ocx telemetry disable`, an env opt-out) may have deleted it: forget the memo.
70
+ if (memo?.path === path) memo = null;
71
+ return null;
72
+ }
73
+ if (memo?.path === path) return memo.identity;
74
+ let identity: TelemetryInstallIdentity | null;
75
+ try {
76
+ identity = parseTelemetryId(readFileSync(path, "utf8"));
77
+ } catch {
78
+ return null;
79
+ }
80
+ if (identity) memo = { path, identity };
81
+ return identity;
82
+ }
83
+
84
+ function errorCode(error: unknown): string | undefined {
85
+ return (error as NodeJS.ErrnoException | null)?.code;
86
+ }
87
+
88
+ function createExclusive(path: string, content: string): void {
89
+ const fd = openSync(path, fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_EXCL | NOFOLLOW, 0o600);
90
+ try {
91
+ if (process.platform !== "win32") {
92
+ try { fchmodSync(fd, 0o600); } catch { /* best-effort */ }
93
+ }
94
+ writeSync(fd, content);
95
+ fsyncSync(fd);
96
+ } finally {
97
+ closeSync(fd);
98
+ }
99
+ }
100
+
101
+ function syncParentDirectory(path: string): void {
102
+ if (process.platform === "win32") return;
103
+ try {
104
+ const fd = openSync(dirname(path), fsConstants.O_RDONLY);
105
+ try { fsyncSync(fd); } finally { closeSync(fd); }
106
+ } catch { /* best-effort */ }
107
+ }
108
+
109
+ function pause(ms: number): void {
110
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
111
+ }
112
+
113
+ function adoptPublishedIdentity(configDir: string, path: string): TelemetryInstallIdentity {
114
+ for (let attempt = 0; attempt < PUBLISHED_READ_ATTEMPTS; attempt++) {
115
+ let stat;
116
+ try {
117
+ stat = lstatSync(path);
118
+ } catch (error) {
119
+ if (errorCode(error) !== "ENOENT") throw error;
120
+ stat = null;
121
+ }
122
+ if (stat && (stat.isSymbolicLink() || !stat.isFile())) break;
123
+ const identity = readTelemetryInstallId(configDir);
124
+ if (identity) return identity;
125
+ if (attempt + 1 < PUBLISHED_READ_ATTEMPTS) pause(PUBLISHED_READ_INTERVAL_MS);
126
+ }
127
+ throw new Error("telemetry identity file is not a valid regular file; remove it to rotate the identity");
128
+ }
129
+
130
+ /**
131
+ * Create `<OPENCODEX_HOME>/telemetry-id.json` (`0o600`, exclusive no-follow) on the first send.
132
+ * Returns `{ identity, freshlyCreated }` so the caller knows when a brand-new id was minted.
133
+ */
134
+ export function ensureTelemetryInstallId(
135
+ configDir = getConfigDir(),
136
+ ): { identity: TelemetryInstallIdentity; freshlyCreated: boolean } {
137
+ const existing = readTelemetryInstallId(configDir);
138
+ if (existing) return { identity: existing, freshlyCreated: false };
139
+ mkdirSync(configDir, { recursive: true, mode: 0o700 });
140
+ const path = telemetryIdPath(configDir);
141
+ try { recordOwnedConfigPath(configDir, path); } catch { /* best-effort */ }
142
+ let present = true;
143
+ try {
144
+ lstatSync(path);
145
+ } catch (error) {
146
+ if (errorCode(error) !== "ENOENT") throw error;
147
+ present = false;
148
+ }
149
+ if (present) {
150
+ return { identity: adoptPublishedIdentity(configDir, path), freshlyCreated: false };
151
+ }
152
+ const identity: TelemetryInstallIdentity = {
153
+ installId: randomUUID().toLowerCase(),
154
+ createdAt: new Date().toISOString(),
155
+ firstReportSent: false,
156
+ };
157
+ const content = `${JSON.stringify({ version: 1, ...identity })}\n`;
158
+ const temp = `${path}.${process.pid}.${randomBytes(6).toString("hex")}.tmp`;
159
+ let published = false;
160
+ try {
161
+ createExclusive(temp, content);
162
+ try {
163
+ linkSync(temp, path);
164
+ published = true;
165
+ } catch (error) {
166
+ const code = errorCode(error);
167
+ if (code === "EEXIST") {
168
+ return { identity: adoptPublishedIdentity(configDir, path), freshlyCreated: false };
169
+ }
170
+ if (code !== "EPERM" && code !== "ENOTSUP" && code !== "EXDEV" && code !== "ENOSYS") throw error;
171
+ }
172
+ } finally {
173
+ try { unlinkSync(temp); } catch { /* best-effort */ }
174
+ }
175
+ if (!published) {
176
+ try {
177
+ createExclusive(path, content);
178
+ } catch (error) {
179
+ if (errorCode(error) !== "EEXIST") throw error;
180
+ return { identity: adoptPublishedIdentity(configDir, path), freshlyCreated: false };
181
+ }
182
+ }
183
+ syncParentDirectory(path);
184
+ memo = { path, identity };
185
+ return { identity, freshlyCreated: true };
186
+ }
187
+
188
+ /** Mark `firstReportSent: true` inside `<OPENCODEX_HOME>/telemetry-id.json` after the first report succeeds. */
189
+ export function markTelemetryFirstReportSent(configDir = getConfigDir()): void {
190
+ const existing = readTelemetryInstallId(configDir);
191
+ if (!existing || existing.firstReportSent === true) return;
192
+ const path = telemetryIdPath(configDir);
193
+ const updated: TelemetryInstallIdentity = {
194
+ installId: existing.installId,
195
+ createdAt: existing.createdAt,
196
+ firstReportSent: true,
197
+ };
198
+ try { recordOwnedConfigPath(configDir, path); } catch { /* best-effort */ }
199
+ atomicWriteFileNoFollow(path, `${JSON.stringify({ version: 1, ...updated })}\n`);
200
+ memo = { path, identity: updated };
201
+ }
202
+
203
+ /** Delete `<OPENCODEX_HOME>/telemetry-id.json` when telemetry is declined. */
204
+ export function deleteTelemetryInstallId(configDir = getConfigDir()): boolean {
205
+ const path = telemetryIdPath(configDir);
206
+ if (memo?.path === path) memo = null;
207
+ let deleted = false;
208
+ try {
209
+ const stat = lstatSync(path);
210
+ if (!stat.isSymbolicLink() && stat.isFile()) {
211
+ unlinkSync(path);
212
+ deleted = true;
213
+ }
214
+ } catch (error) {
215
+ if (errorCode(error) !== "ENOENT") deleted = false;
216
+ }
217
+ return deleted;
218
+ }
219
+
220
+ export function computeInstallDayHash(installId: string, day: string): string {
221
+ return createHash("sha256").update(`${installId}:${day}`).digest("hex");
222
+ }
223
+
224
+ export function resetTelemetryInstallIdMemoForTests(): void {
225
+ memo = null;
226
+ }