@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.
- package/AGENTS_INSTALL.md +19 -1
- package/README.md +37 -33
- package/SPONSORS.md +5 -1
- package/bin/ocx.mjs +1 -1
- package/gui/dist/assets/{App-C1hGwaN1.css → App-COo8Yopd.css} +1 -1
- package/gui/dist/assets/App-CitvEefy.js +59 -0
- package/gui/dist/assets/Tray-CeKVWQyB.js +1 -0
- package/gui/dist/assets/{brand-mark-wzPe3R-J.js → brand-mark-M92UnxX2.js} +1 -1
- package/gui/dist/assets/index-BP47do6-.js +94 -0
- package/gui/dist/assets/index-DmDooSAG.css +2 -0
- package/gui/dist/assets/{segment-thumb-features-fYEMxBaW.js → segment-thumb-features-CcxZBnxv.js} +1 -1
- package/gui/dist/classic/assets/App-aYgIQMxI.js +58 -0
- package/gui/dist/classic/assets/{Tray-BqZueZOx.js → Tray-D8jfUXpQ.js} +1 -1
- package/gui/dist/classic/assets/index-CfGqHooN.js +94 -0
- package/gui/dist/classic/assets/quota-summary-R-ih7OlS.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/native/ocx-child/Cargo.lock +215 -0
- package/native/ocx-child/Cargo.toml +31 -0
- package/native/ocx-child/src/cli.rs +1901 -0
- package/native/ocx-child/src/codex_home.rs +429 -0
- package/native/ocx-child/src/codex_settings.rs +231 -0
- package/native/ocx-child/src/daemon.rs +363 -0
- package/native/ocx-child/src/http_wire.rs +619 -0
- package/native/ocx-child/src/journal.rs +977 -0
- package/native/ocx-child/src/lib.rs +22 -0
- package/native/ocx-child/src/main.rs +9 -0
- package/native/ocx-child/src/parity.rs +102 -0
- package/native/ocx-child/src/platform.rs +1487 -0
- package/native/ocx-child/src/relay.rs +1288 -0
- package/native/ocx-child/src/relay_auth.rs +166 -0
- package/native/ocx-child/src/routes.rs +236 -0
- package/native/ocx-child/src/state.rs +190 -0
- package/native/ocx-child/src/test_hooks.rs +58 -0
- package/native/ocx-child/src/toml_surgeon.rs +1156 -0
- package/package.json +8 -2
- package/src/adapters/anthropic.ts +28 -2
- package/src/adapters/claude-cli/adapter.ts +5 -25
- package/src/adapters/claude-cli/instruction-images.ts +31 -0
- package/src/adapters/claude-cli/stable-replay.ts +42 -14
- package/src/adapters/claude-cli/usage-admission.ts +53 -64
- package/src/adapters/coding-agent/protocol.ts +1 -1
- package/src/adapters/devin/live-models.ts +3 -0
- package/src/adapters/google-http.ts +3 -0
- package/src/adapters/google-vertex-key-project.ts +10 -2
- package/src/adapters/google.ts +40 -8
- package/src/adapters/kiro/usage.ts +3 -13
- package/src/adapters/openai-responses/passthrough.ts +5 -2
- package/src/adapters/openai-responses/request-strips.ts +32 -0
- package/src/adapters/openai-responses/tool-schema.ts +9 -4
- package/src/chat/inbound.ts +1 -1
- package/src/claude/auth-detect.ts +26 -8
- package/src/claude/inbound-content-options.ts +18 -3
- package/src/claude/inbound.ts +5 -2
- package/src/cli/bundled-bun-diagnostic.ts +91 -0
- package/src/cli/capabilities-agents-routing.ts +2 -2
- package/src/cli/capabilities-observe-system.ts +60 -0
- package/src/cli/combo-input.ts +6 -0
- package/src/cli/combo-result.ts +1 -1
- package/src/cli/combo.ts +4 -0
- package/src/cli/config-command.ts +16 -2
- package/src/cli/connect.ts +12 -10
- package/src/cli/dispatch.ts +4 -0
- package/src/cli/doctor.ts +11 -0
- package/src/cli/help.ts +1 -0
- package/src/cli/init.ts +4 -0
- package/src/cli/link-enrollment.ts +152 -25
- package/src/cli/link-local-control.ts +49 -0
- package/src/cli/link.ts +37 -10
- package/src/cli/provider-lifecycle-runtime.ts +4 -2
- package/src/cli/registry.ts +13 -0
- package/src/cli/star-prompt.ts +2 -0
- package/src/cli/status.ts +1 -1
- package/src/cli/telemetry-profile.ts +206 -0
- package/src/cli/telemetry.ts +260 -0
- package/src/client/hub-relay.ts +26 -0
- package/src/client/machine-api.ts +3 -0
- package/src/clients/config-export/t3code.ts +44 -5
- package/src/clients/omo-role-models.ts +1 -1
- package/src/codex/catalog/bundled.ts +44 -2
- package/src/codex/runtime.ts +12 -0
- package/src/codex/shim-restore-lock.ts +67 -30
- package/src/combos/types.ts +21 -1
- package/src/config/atomic-write.ts +24 -1
- package/src/config/diagnostics.ts +15 -0
- package/src/config/live-reconcile.ts +17 -0
- package/src/config/load-degrade.ts +16 -0
- package/src/config/provider-validation.ts +29 -0
- package/src/config/schema/config-schema.ts +24 -0
- package/src/config/schema/leaf-validators.ts +17 -0
- package/src/config/telemetry-carry.ts +30 -0
- package/src/config.ts +13 -1
- package/src/generated/compatibility-version.json +405 -153
- package/src/generated/model-metadata.ts +3 -3
- package/src/integrations/aside-profiles.ts +7 -1
- package/src/integrations/owned-refresh.ts +9 -2
- package/src/lab/artifacts/secure-fs.ts +12 -11
- package/src/lab/fabric/scratch.ts +11 -9
- package/src/lab/public/community.ts +2 -3
- package/src/lab/public/file-safety.ts +18 -20
- package/src/lab/public/mutation-lock.ts +109 -337
- package/src/lab/public/private-file.ts +4 -3
- package/src/lib/bun-runtime-preflight.ts +1 -1
- package/src/lib/codex-catalog-read-only.ts +122 -0
- package/src/lib/config-ownership.ts +49 -18
- package/src/lib/errors.ts +11 -0
- package/src/lib/gcp-adc.ts +167 -48
- package/src/lib/jsonc.ts +4 -2
- package/src/lib/local-control-socket.ts +88 -0
- package/src/lib/privacy.ts +15 -0
- package/src/lib/provider-client-headers.ts +3 -0
- package/src/lib/token-estimate.ts +13 -2
- package/src/link/child-binary-source.ts +228 -0
- package/src/link/ephemeral-child.ts +45 -0
- package/src/link/remote-plant.ts +306 -0
- package/src/link/remote-windows.ts +59 -5
- package/src/link/ssh-argv.ts +71 -8
- package/src/link/status-projection.ts +2 -0
- package/src/link/store.ts +5 -1
- package/src/oauth/claude-keychain-read.ts +165 -0
- package/src/oauth/local-token-detect.ts +14 -17
- package/src/protocols/envelope.ts +20 -2
- package/src/providers/api-key-selection.ts +8 -2
- package/src/providers/claude-cli-usage-state.ts +93 -0
- package/src/providers/github-copilot-auto.ts +25 -5
- package/src/providers/meta-muse-plan-refusal.ts +94 -0
- package/src/providers/model-rename-fields.ts +3 -0
- package/src/providers/quota/vendor-probes-key.ts +23 -4
- package/src/providers/quota/vendor-probes-oauth.ts +59 -0
- package/src/providers/quota/workbuddy.ts +2 -2
- package/src/providers/quota.ts +25 -18
- package/src/providers/registry/entries-core.ts +7 -4
- package/src/providers/registry/entries-extended.ts +39 -7
- package/src/providers/registry/model-seeds.ts +10 -2
- package/src/providers/resolved-model-policy.ts +4 -0
- package/src/providers/routed-config.ts +30 -3
- package/src/providers/stale-no-vision-seed-migration.ts +7 -0
- package/src/providers/vertex-environment-authority.ts +50 -0
- package/src/providers/workbuddy-credit-balance.ts +38 -6
- package/src/quota-pool/assemble.ts +14 -6
- package/src/router.ts +1 -0
- package/src/server/adapter-resolve.ts +11 -6
- package/src/server/audio-dictation.ts +21 -7
- package/src/server/auth-cors.ts +19 -0
- package/src/server/background-lifecycle.ts +71 -0
- package/src/server/chat-completions.ts +2 -0
- package/src/server/claude-messages.ts +30 -3
- package/src/server/index/live-sideband.ts +15 -1
- package/src/server/index/local-control-listener.ts +515 -0
- package/src/server/index/optional-listeners.ts +21 -1
- package/src/server/index/serve-options.ts +4 -3
- package/src/server/index/websocket-handler.ts +2 -0
- package/src/server/index.ts +3 -0
- package/src/server/management/combo-routes.ts +6 -1
- package/src/server/management/context.ts +1 -0
- package/src/server/management/link-child-preflight.ts +74 -0
- package/src/server/management/link-enrollment.ts +190 -25
- package/src/server/management/link-route-state.ts +6 -1
- package/src/server/management/link-routes.ts +27 -9
- package/src/server/management/model-rows.ts +4 -0
- package/src/server/management/oauth-account-routes.ts +9 -4
- package/src/server/management/provider-overwrite-carry.ts +9 -2
- package/src/server/management/provider-routes.ts +14 -0
- package/src/server/management/provider-thinking-display-patch.ts +54 -0
- package/src/server/management/route-registry.ts +7 -0
- package/src/server/management/session-routes.ts +4 -1
- package/src/server/management/telemetry-routes.ts +37 -0
- package/src/server/management/usage-aggregate-cache.ts +290 -67
- package/src/server/management-api.ts +11 -2
- package/src/server/messages-native-preflight.ts +166 -0
- package/src/server/messages-native.ts +143 -43
- package/src/server/messages-shared.ts +14 -0
- package/src/server/proxy-liveness.ts +99 -6
- package/src/server/request-log-conversation.ts +18 -2
- package/src/server/request-log.ts +7 -2
- package/src/server/responses/adapter-continuation.ts +1 -0
- package/src/server/responses/adapter-dispatch.ts +11 -28
- package/src/server/responses/adapter-failure-message.ts +68 -0
- package/src/server/responses/combo-stream-preflight.ts +14 -2
- package/src/server/responses/core-auth.ts +26 -5
- package/src/server/responses/core-codex-account.ts +8 -1
- package/src/server/responses/core-combo-failure.ts +6 -1
- package/src/server/responses/core-combo-native.ts +150 -37
- package/src/server/responses/core-combo.ts +15 -9
- package/src/server/responses/core-normalize.ts +1 -0
- package/src/server/responses/core-options.ts +7 -1
- package/src/server/responses/core-pipeline.ts +21 -1
- package/src/server/responses/fetch-helpers.ts +5 -0
- package/src/server/responses/opengateway-completion-guard.ts +7 -3
- package/src/server/responses/passthrough-delivery.ts +8 -1
- package/src/server/responses/passthrough-error.ts +22 -0
- package/src/server/responses/request-prepare.ts +7 -1
- package/src/server/responses/request-transport.ts +28 -5
- package/src/server/responses/run-turn-execution.ts +13 -0
- package/src/server/sse-frame-buffer.ts +5 -0
- package/src/server/ws-bridge.ts +10 -0
- package/src/service/launchd.ts +78 -4
- package/src/telemetry/aggregate.ts +348 -0
- package/src/telemetry/consent.ts +128 -0
- package/src/telemetry/contract.ts +403 -0
- package/src/telemetry/disclosure.ts +19 -0
- package/src/telemetry/install-id.ts +226 -0
- package/src/telemetry/payload.ts +129 -0
- package/src/telemetry/profile/client.ts +150 -0
- package/src/telemetry/profile/contract.ts +288 -0
- package/src/telemetry/profile/disclosure.ts +14 -0
- package/src/telemetry/profile/paths.ts +13 -0
- package/src/telemetry/profile/routes.ts +99 -0
- package/src/telemetry/profile/service.ts +234 -0
- package/src/telemetry/profile/state.ts +189 -0
- package/src/telemetry/profile/sync.ts +276 -0
- package/src/telemetry/routes.ts +183 -0
- package/src/telemetry/scheduler.ts +386 -0
- package/src/telemetry/sender.ts +97 -0
- package/src/telemetry/state.ts +334 -0
- package/src/types/combo-jev-effort-mode.ts +11 -0
- package/src/types/config.ts +3 -0
- package/src/types/provider.ts +11 -1
- package/src/types/request.ts +2 -0
- package/src/types/telemetry.ts +15 -0
- package/src/types/wire.ts +10 -0
- package/src/types.ts +5 -0
- package/src/update/index.ts +7 -1
- package/src/update/minisign.d.mts +17 -0
- package/src/update/minisign.mjs +88 -0
- package/src/update/signed-supply.d.mts +59 -0
- package/src/update/signed-supply.mjs +289 -0
- package/src/update/trust-root.json +38 -0
- package/src/update/update-bounded-json.d.mts +4 -0
- package/src/update/update-bounded-json.mjs +113 -0
- package/src/update/update-bytes.d.mts +2 -0
- package/src/update/update-bytes.mjs +23 -0
- package/src/update/update-candidate-id.d.mts +2 -0
- package/src/update/update-candidate-id.mjs +23 -0
- package/src/update/update-checkpoint-codec.d.mts +33 -0
- package/src/update/update-checkpoint-codec.mjs +217 -0
- package/src/update/update-contracts.d.mts +24 -0
- package/src/update/update-contracts.mjs +207 -0
- package/src/update/update-signature-threshold.d.mts +12 -0
- package/src/update/update-signature-threshold.mjs +47 -0
- package/src/update/update-signed-version.d.mts +5 -0
- package/src/update/update-signed-version.mjs +25 -0
- package/src/update/update-trust-anchor.d.mts +2 -0
- package/src/update/update-trust-anchor.mjs +2 -0
- package/src/usage/cache-diagnostic.ts +195 -50
- package/src/usage/expected-prices.ts +20 -2
- package/src/usage/filter-index.ts +1469 -0
- package/src/usage/ledger-scanner.ts +245 -4
- package/src/usage/source-filter.ts +10 -2
- package/src/usage/summary.ts +16 -0
- package/src/web-search/parse.ts +244 -38
- package/gui/dist/assets/App-BepstgEN.js +0 -59
- package/gui/dist/assets/Tray-BinxbEZo.js +0 -1
- package/gui/dist/assets/index-C-9X2yaI.js +0 -94
- package/gui/dist/assets/index-bnUWypyz.css +0 -2
- package/gui/dist/classic/assets/App-DX88_GCW.js +0 -58
- package/gui/dist/classic/assets/index-fSP71pLB.js +0 -94
- package/gui/dist/classic/assets/quota-summary-y2fl0WBv.js +0 -1
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
import { readConfigDiagnostics } from "../config/diagnostics";
|
|
2
|
+
import { getConfigDir } from "../config/paths";
|
|
3
|
+
import { registerOptionalShutdownHook } from "../lib/optional-shutdown-hooks";
|
|
4
|
+
import type { OcxConfig } from "../types";
|
|
5
|
+
import { aggregateTelemetryForDay, type DailyTelemetryAggregate } from "./aggregate";
|
|
6
|
+
import { isTelemetryEnvOptOut, resolveTelemetryStatus, type TelemetryConsentStatus } from "./consent";
|
|
7
|
+
import {
|
|
8
|
+
computeInstallDayHash,
|
|
9
|
+
ensureTelemetryInstallId,
|
|
10
|
+
markTelemetryFirstReportSent,
|
|
11
|
+
} from "./install-id";
|
|
12
|
+
import { buildTelemetryPayloadV1, resolveInstallChannel, type ResolveInstallChannelOptions } from "./payload";
|
|
13
|
+
import { sendTelemetryPayload, type TelemetrySendOptions, type TelemetrySendResult } from "./sender";
|
|
14
|
+
import {
|
|
15
|
+
endTelemetryEnvOptOut,
|
|
16
|
+
formatUtcDay,
|
|
17
|
+
readTelemetryState,
|
|
18
|
+
readTelemetryStateFile,
|
|
19
|
+
recordTelemetryEnvOptOut,
|
|
20
|
+
utcMidnightMs,
|
|
21
|
+
withPendingTelemetryDecision,
|
|
22
|
+
writeTelemetryState,
|
|
23
|
+
type TelemetryState,
|
|
24
|
+
} from "./state";
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
TELEMETRY_STATE_FILE,
|
|
28
|
+
clearPendingTelemetryDecision,
|
|
29
|
+
endTelemetryEnvOptOut,
|
|
30
|
+
formatUtcDay,
|
|
31
|
+
readTelemetryState,
|
|
32
|
+
readTelemetryStateFile,
|
|
33
|
+
recordPendingTelemetryDecision,
|
|
34
|
+
recordTelemetryEnvOptOut,
|
|
35
|
+
telemetryStatePath,
|
|
36
|
+
utcMidnightMs,
|
|
37
|
+
withPendingTelemetryDecision,
|
|
38
|
+
writeTelemetryState,
|
|
39
|
+
type TelemetryEnvOptOutOwner,
|
|
40
|
+
type TelemetryState,
|
|
41
|
+
} from "./state";
|
|
42
|
+
|
|
43
|
+
export const TELEMETRY_TICK_INTERVAL_MS = 6 * 60 * 60 * 1000;
|
|
44
|
+
export const TELEMETRY_STARTUP_BASE_DELAY_MS = 30_000;
|
|
45
|
+
export const TELEMETRY_STARTUP_JITTER_MS = 60_000;
|
|
46
|
+
export const TELEMETRY_INTERVAL_JITTER_MS = 15 * 60 * 1000;
|
|
47
|
+
const DAY_MS = 86_400_000;
|
|
48
|
+
const MAX_BACKLOG_DAYS = 7;
|
|
49
|
+
|
|
50
|
+
export function latestCompletedUtcDay(now = Date.now()): string {
|
|
51
|
+
return formatUtcDay(utcMidnightMs(now) - DAY_MS);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Resolve the oldest completed UTC day (within the 7-day window) that is eligible to be sent next:
|
|
56
|
+
* - `status.effective` must be true.
|
|
57
|
+
* - Strictly AFTER the UTC day of `decidedAt ?? noticeShownAt` (D1).
|
|
58
|
+
* - Strictly AFTER `state.ineligibleThroughDay` when set (D2).
|
|
59
|
+
* - Nothing while an env opt-out marker is still set: that period may have run until today (D2).
|
|
60
|
+
* - Not already recorded in `state.sentDays`.
|
|
61
|
+
*/
|
|
62
|
+
export function resolveNextEligibleTelemetryDay(
|
|
63
|
+
status: TelemetryConsentStatus,
|
|
64
|
+
state: TelemetryState,
|
|
65
|
+
now = Date.now(),
|
|
66
|
+
): { day: string | null; reason: string } {
|
|
67
|
+
if (!status.effective) {
|
|
68
|
+
return { day: null, reason: status.reason };
|
|
69
|
+
}
|
|
70
|
+
if (state.envOptOutActiveSince || (state.envOptOutOwners?.length ?? 0) > 0) {
|
|
71
|
+
return { day: null, reason: "env-opt-out-pending" };
|
|
72
|
+
}
|
|
73
|
+
const consentTimestamp = status.decidedAt ?? status.noticeShownAt;
|
|
74
|
+
if (!consentTimestamp) {
|
|
75
|
+
return { day: null, reason: "missing-consent-timestamp" };
|
|
76
|
+
}
|
|
77
|
+
const consentMs = Date.parse(consentTimestamp);
|
|
78
|
+
if (Number.isNaN(consentMs)) {
|
|
79
|
+
return { day: null, reason: "invalid-consent-timestamp" };
|
|
80
|
+
}
|
|
81
|
+
const consentDay = formatUtcDay(consentMs);
|
|
82
|
+
const todayMs = utcMidnightMs(now);
|
|
83
|
+
|
|
84
|
+
for (let offset = MAX_BACKLOG_DAYS; offset >= 1; offset--) {
|
|
85
|
+
const candidateDay = formatUtcDay(todayMs - offset * DAY_MS);
|
|
86
|
+
if (candidateDay <= consentDay) continue;
|
|
87
|
+
if (state.ineligibleThroughDay && candidateDay <= state.ineligibleThroughDay) continue;
|
|
88
|
+
if (state.sentDays[candidateDay]) continue;
|
|
89
|
+
return { day: candidateDay, reason: "eligible" };
|
|
90
|
+
}
|
|
91
|
+
return { day: null, reason: "up-to-date" };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function computeTelemetryStartupDelayMs(random: () => number = Math.random): number {
|
|
95
|
+
return TELEMETRY_STARTUP_BASE_DELAY_MS + Math.floor(random() * TELEMETRY_STARTUP_JITTER_MS);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function computeTelemetryIntervalDelayMs(
|
|
99
|
+
baseMs = TELEMETRY_TICK_INTERVAL_MS,
|
|
100
|
+
random: () => number = Math.random,
|
|
101
|
+
): number {
|
|
102
|
+
return baseMs + Math.floor(random() * TELEMETRY_INTERVAL_JITTER_MS);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface TelemetryTickOptions extends TelemetrySendOptions, ResolveInstallChannelOptions {
|
|
106
|
+
configDir?: string;
|
|
107
|
+
config?: Pick<OcxConfig, "telemetry">
|
|
108
|
+
readConfig?: () => Pick<OcxConfig, "telemetry">;
|
|
109
|
+
aggregateDay?: (day: string, options?: { signal?: AbortSignal }) => Promise<DailyTelemetryAggregate>;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface TelemetryTickResult {
|
|
113
|
+
sent: boolean;
|
|
114
|
+
day?: string;
|
|
115
|
+
reason: string;
|
|
116
|
+
sendResult?: TelemetrySendResult;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const activeTickControllers = new Set<AbortController>();
|
|
120
|
+
|
|
121
|
+
function abortActiveTicks(reason: string): void {
|
|
122
|
+
for (const controller of activeTickControllers) {
|
|
123
|
+
controller.abort(new Error(reason));
|
|
124
|
+
}
|
|
125
|
+
activeTickControllers.clear();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Run one telemetry tick:
|
|
130
|
+
* - Re-reads config passively from disk and re-evaluates `resolveTelemetryStatus`.
|
|
131
|
+
* - Persists `ineligibleThroughDay` and deletes the install id whenever an env opt-out is observed.
|
|
132
|
+
* - Sends only completed UTC days strictly after the UTC day of `decidedAt ?? noticeShownAt`
|
|
133
|
+
* and strictly after `ineligibleThroughDay`.
|
|
134
|
+
* - Re-checks consent and env immediately before transmission and cancels in-flight work on stop/nudge.
|
|
135
|
+
* - Sends at most ONE day per tick, oldest first, at most the last 7 completed days.
|
|
136
|
+
*/
|
|
137
|
+
export async function runTelemetryTick(options: TelemetryTickOptions = {}): Promise<TelemetryTickResult> {
|
|
138
|
+
const now = options.now ?? Date.now();
|
|
139
|
+
const configDir = options.configDir ?? getConfigDir();
|
|
140
|
+
const readCurrentEnv = (): Record<string, string | undefined> => options.env ?? process.env;
|
|
141
|
+
const readRawConfig = (): Pick<OcxConfig, "telemetry"> | undefined => {
|
|
142
|
+
if (options.readConfig) return options.readConfig();
|
|
143
|
+
const diskConfig = readConfigDiagnostics({ passive: true }).config;
|
|
144
|
+
if (options.config) {
|
|
145
|
+
if (diskConfig.telemetry !== undefined) {
|
|
146
|
+
const currentEnv = readCurrentEnv();
|
|
147
|
+
const optStatus = resolveTelemetryStatus(options.config, currentEnv);
|
|
148
|
+
const diskStatus = resolveTelemetryStatus(diskConfig, currentEnv);
|
|
149
|
+
return !optStatus.effective ? options.config : !diskStatus.effective ? diskConfig : options.config;
|
|
150
|
+
}
|
|
151
|
+
return options.config;
|
|
152
|
+
}
|
|
153
|
+
return diskConfig;
|
|
154
|
+
};
|
|
155
|
+
// A decision held from before config.json existed applies until the config carries its own.
|
|
156
|
+
const readCurrentConfig = (): Pick<OcxConfig, "telemetry"> | undefined =>
|
|
157
|
+
withPendingTelemetryDecision(readRawConfig(), configDir) ?? undefined;
|
|
158
|
+
|
|
159
|
+
const initialEnv = readCurrentEnv();
|
|
160
|
+
if (isTelemetryEnvOptOut(initialEnv) !== null) {
|
|
161
|
+
recordTelemetryEnvOptOut(configDir, now);
|
|
162
|
+
const status = resolveTelemetryStatus(readCurrentConfig(), initialEnv);
|
|
163
|
+
return { sent: false, reason: status.reason };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const status = resolveTelemetryStatus(readCurrentConfig(), initialEnv);
|
|
167
|
+
if (!status.effective) {
|
|
168
|
+
return { sent: false, reason: status.reason };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const stateFile = readTelemetryStateFile(configDir);
|
|
172
|
+
if (stateFile.unreadable) {
|
|
173
|
+
// A state file that exists but cannot be read may have held opt-out days; never send over it.
|
|
174
|
+
return { sent: false, reason: "state-unreadable" };
|
|
175
|
+
}
|
|
176
|
+
if (stateFile.state.envOptOutActiveSince || (stateFile.state.envOptOutOwners?.length ?? 0) > 0) {
|
|
177
|
+
// Ends the period only once no opted-out proxy is still running; otherwise extends it to today.
|
|
178
|
+
endTelemetryEnvOptOut(configDir, now);
|
|
179
|
+
}
|
|
180
|
+
const current = readTelemetryStateFile(configDir);
|
|
181
|
+
if (current.unreadable) return { sent: false, reason: "state-unreadable" };
|
|
182
|
+
const state = current.state;
|
|
183
|
+
if (state.retryAfterUntil) {
|
|
184
|
+
const retryUntilMs = Date.parse(state.retryAfterUntil);
|
|
185
|
+
if (!Number.isNaN(retryUntilMs) && now < retryUntilMs) {
|
|
186
|
+
return { sent: false, reason: "retry-after-backoff" };
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const { day: targetDay, reason: eligibilityReason } = resolveNextEligibleTelemetryDay(status, state, now);
|
|
191
|
+
if (!targetDay) {
|
|
192
|
+
return { sent: false, reason: eligibilityReason };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const tickController = new AbortController();
|
|
196
|
+
activeTickControllers.add(tickController);
|
|
197
|
+
const parentSignal = options.signal;
|
|
198
|
+
const onParentAbort = () => tickController.abort(parentSignal?.reason);
|
|
199
|
+
if (parentSignal?.aborted) {
|
|
200
|
+
activeTickControllers.delete(tickController);
|
|
201
|
+
return { sent: false, day: targetDay, reason: "aborted" };
|
|
202
|
+
}
|
|
203
|
+
parentSignal?.addEventListener("abort", onParentAbort, { once: true });
|
|
204
|
+
|
|
205
|
+
try {
|
|
206
|
+
let aggregate: DailyTelemetryAggregate;
|
|
207
|
+
try {
|
|
208
|
+
aggregate = await (options.aggregateDay
|
|
209
|
+
? options.aggregateDay(targetDay, { signal: tickController.signal })
|
|
210
|
+
: aggregateTelemetryForDay(targetDay, { signal: tickController.signal }));
|
|
211
|
+
} catch (error) {
|
|
212
|
+
if (tickController.signal.aborted) {
|
|
213
|
+
return { sent: false, day: targetDay, reason: "aborted" };
|
|
214
|
+
}
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const preSendEnv = readCurrentEnv();
|
|
219
|
+
if (isTelemetryEnvOptOut(preSendEnv) !== null) {
|
|
220
|
+
recordTelemetryEnvOptOut(configDir, now);
|
|
221
|
+
const latestStatus = resolveTelemetryStatus(readCurrentConfig(), preSendEnv);
|
|
222
|
+
return { sent: false, day: targetDay, reason: latestStatus.reason };
|
|
223
|
+
}
|
|
224
|
+
if (tickController.signal.aborted) {
|
|
225
|
+
return { sent: false, day: targetDay, reason: "aborted" };
|
|
226
|
+
}
|
|
227
|
+
const preSendStatus = resolveTelemetryStatus(readCurrentConfig(), preSendEnv);
|
|
228
|
+
if (!preSendStatus.effective) {
|
|
229
|
+
return { sent: false, day: targetDay, reason: preSendStatus.reason };
|
|
230
|
+
}
|
|
231
|
+
// The file may have become unreadable while the day was aggregated; never send over it.
|
|
232
|
+
const preSendFile = readTelemetryStateFile(configDir);
|
|
233
|
+
if (preSendFile.unreadable) {
|
|
234
|
+
return { sent: false, day: targetDay, reason: "state-unreadable" };
|
|
235
|
+
}
|
|
236
|
+
const preSendState = preSendFile.state;
|
|
237
|
+
const recheckedEligibility = resolveNextEligibleTelemetryDay(preSendStatus, preSendState, now);
|
|
238
|
+
if (recheckedEligibility.day !== targetDay) {
|
|
239
|
+
return { sent: false, day: targetDay, reason: recheckedEligibility.reason };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const { identity } = ensureTelemetryInstallId(configDir);
|
|
243
|
+
// An id minted after an opt-out is a returning install, not a new one.
|
|
244
|
+
const newInstall = identity.firstReportSent !== true && preSendState.everReported !== true;
|
|
245
|
+
const installDayHash = computeInstallDayHash(identity.installId, targetDay);
|
|
246
|
+
const payload = buildTelemetryPayloadV1({
|
|
247
|
+
aggregate,
|
|
248
|
+
installDayHash,
|
|
249
|
+
newInstall,
|
|
250
|
+
now,
|
|
251
|
+
installChannel: resolveInstallChannel({ ...options, env: preSendEnv }),
|
|
252
|
+
});
|
|
253
|
+
const sendResult = await sendTelemetryPayload(payload, {
|
|
254
|
+
...options,
|
|
255
|
+
signal: tickController.signal,
|
|
256
|
+
now,
|
|
257
|
+
});
|
|
258
|
+
if (!sendResult.ok) {
|
|
259
|
+
if (tickController.signal.aborted) {
|
|
260
|
+
return { sent: false, day: targetDay, reason: "aborted", sendResult };
|
|
261
|
+
}
|
|
262
|
+
if (typeof sendResult.retryAfterMs === "number" && sendResult.retryAfterMs > 0) {
|
|
263
|
+
const retryAfterUntil = new Date(now + sendResult.retryAfterMs).toISOString();
|
|
264
|
+
updateReadableTelemetryState(configDir, now, state => {
|
|
265
|
+
state.retryAfterUntil = retryAfterUntil;
|
|
266
|
+
});
|
|
267
|
+
return { sent: false, day: targetDay, reason: "retry-after-backoff", sendResult };
|
|
268
|
+
}
|
|
269
|
+
return { sent: false, day: targetDay, reason: "send-failed", sendResult };
|
|
270
|
+
}
|
|
271
|
+
markTelemetryFirstReportSent(configDir);
|
|
272
|
+
const nowIso = new Date(now).toISOString();
|
|
273
|
+
updateReadableTelemetryState(configDir, now, state => {
|
|
274
|
+
state.sentDays[targetDay] = nowIso;
|
|
275
|
+
state.everReported = true;
|
|
276
|
+
state.lastSentDay = targetDay;
|
|
277
|
+
state.lastSentAt = nowIso;
|
|
278
|
+
state.retryAfterUntil = null;
|
|
279
|
+
});
|
|
280
|
+
return { sent: true, day: targetDay, reason: "sent", sendResult };
|
|
281
|
+
} finally {
|
|
282
|
+
parentSignal?.removeEventListener("abort", onParentAbort);
|
|
283
|
+
activeTickControllers.delete(tickController);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Bookkeeping after a send. A file that became unreadable during the send is left as it is: rewriting
|
|
289
|
+
* it from an empty state would drop the opt-out days it held, and every later tick refuses to send
|
|
290
|
+
* until it can be read again.
|
|
291
|
+
*/
|
|
292
|
+
function updateReadableTelemetryState(configDir: string, now: number, update: (state: TelemetryState) => void): void {
|
|
293
|
+
const file = readTelemetryStateFile(configDir);
|
|
294
|
+
if (file.unreadable) return;
|
|
295
|
+
update(file.state);
|
|
296
|
+
writeTelemetryState(file.state, configDir, now);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
let startupTimer: ReturnType<typeof setTimeout> | null = null;
|
|
300
|
+
let intervalTimer: ReturnType<typeof setInterval> | null = null;
|
|
301
|
+
let nudgeTimer: ReturnType<typeof setTimeout> | null = null;
|
|
302
|
+
let detachShutdownHook: (() => void) | null = null;
|
|
303
|
+
let activeTick: Promise<TelemetryTickResult> | null = null;
|
|
304
|
+
let running = false;
|
|
305
|
+
|
|
306
|
+
function triggerBackgroundTick(): void {
|
|
307
|
+
if (!running) return;
|
|
308
|
+
if (isTelemetryEnvOptOut(process.env) !== null) {
|
|
309
|
+
try { recordTelemetryEnvOptOut(); } catch { /* best-effort */ }
|
|
310
|
+
stopTelemetryScheduler();
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
if (activeTick) return;
|
|
314
|
+
activeTick = runTelemetryTick()
|
|
315
|
+
.catch((): TelemetryTickResult => ({ sent: false, reason: "tick-error" }))
|
|
316
|
+
.finally(() => {
|
|
317
|
+
activeTick = null;
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export function startTelemetryScheduler(options: { startupDelayMs?: number; intervalMs?: number } = {}): () => void {
|
|
322
|
+
if (isTelemetryEnvOptOut(process.env) !== null) {
|
|
323
|
+
try { recordTelemetryEnvOptOut(); } catch { /* best-effort */ }
|
|
324
|
+
return () => {};
|
|
325
|
+
}
|
|
326
|
+
if (running) return stopTelemetryScheduler;
|
|
327
|
+
running = true;
|
|
328
|
+
detachShutdownHook = registerOptionalShutdownHook("telemetry", stopTelemetryScheduler);
|
|
329
|
+
const startupDelay = options.startupDelayMs ?? computeTelemetryStartupDelayMs();
|
|
330
|
+
const intervalMs = options.intervalMs ?? computeTelemetryIntervalDelayMs();
|
|
331
|
+
startupTimer = setTimeout(() => {
|
|
332
|
+
startupTimer = null;
|
|
333
|
+
triggerBackgroundTick();
|
|
334
|
+
}, startupDelay);
|
|
335
|
+
startupTimer.unref?.();
|
|
336
|
+
intervalTimer = setInterval(() => {
|
|
337
|
+
triggerBackgroundTick();
|
|
338
|
+
}, intervalMs);
|
|
339
|
+
intervalTimer.unref?.();
|
|
340
|
+
return stopTelemetryScheduler;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
export function stopTelemetryScheduler(): void {
|
|
344
|
+
running = false;
|
|
345
|
+
abortActiveTicks("Telemetry scheduler stopped");
|
|
346
|
+
if (startupTimer) {
|
|
347
|
+
clearTimeout(startupTimer);
|
|
348
|
+
startupTimer = null;
|
|
349
|
+
}
|
|
350
|
+
if (intervalTimer) {
|
|
351
|
+
clearInterval(intervalTimer);
|
|
352
|
+
intervalTimer = null;
|
|
353
|
+
}
|
|
354
|
+
if (nudgeTimer) {
|
|
355
|
+
clearTimeout(nudgeTimer);
|
|
356
|
+
nudgeTimer = null;
|
|
357
|
+
}
|
|
358
|
+
if (detachShutdownHook) {
|
|
359
|
+
const detach = detachShutdownHook;
|
|
360
|
+
detachShutdownHook = null;
|
|
361
|
+
detach();
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export function nudgeTelemetryScheduler(): void {
|
|
366
|
+
abortActiveTicks("Telemetry consent changed");
|
|
367
|
+
if (!running) return;
|
|
368
|
+
if (isTelemetryEnvOptOut(process.env) !== null) {
|
|
369
|
+
try { recordTelemetryEnvOptOut(); } catch { /* best-effort */ }
|
|
370
|
+
stopTelemetryScheduler();
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
if (startupTimer) {
|
|
374
|
+
clearTimeout(startupTimer);
|
|
375
|
+
startupTimer = null;
|
|
376
|
+
}
|
|
377
|
+
if (nudgeTimer) {
|
|
378
|
+
clearTimeout(nudgeTimer);
|
|
379
|
+
nudgeTimer = null;
|
|
380
|
+
}
|
|
381
|
+
nudgeTimer = setTimeout(() => {
|
|
382
|
+
nudgeTimer = null;
|
|
383
|
+
triggerBackgroundTick();
|
|
384
|
+
}, 10);
|
|
385
|
+
nudgeTimer.unref?.();
|
|
386
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { signalWithTimeout } from "../lib/abort";
|
|
2
|
+
import { validateTelemetryPayloadV1, type TelemetryPayloadV1 } from "./contract";
|
|
3
|
+
|
|
4
|
+
export const TELEMETRY_COLLECTOR_ENDPOINT = "https://telemetry.opencodex.me/v1/events";
|
|
5
|
+
export const TELEMETRY_SEND_TIMEOUT_MS = 10_000;
|
|
6
|
+
export const TELEMETRY_DEFAULT_RETRY_AFTER_MS = 15 * 60 * 1000;
|
|
7
|
+
export const TELEMETRY_RETRY_JITTER_MS = 5 * 60 * 1000;
|
|
8
|
+
const MAX_RETRY_AFTER_MS = 7 * 86_400_000;
|
|
9
|
+
|
|
10
|
+
export interface TelemetrySendOptions {
|
|
11
|
+
endpoint?: string;
|
|
12
|
+
fetch?: typeof fetch;
|
|
13
|
+
timeoutMs?: number;
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
now?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface TelemetrySendResult {
|
|
19
|
+
ok: boolean;
|
|
20
|
+
status?: number;
|
|
21
|
+
error?: string;
|
|
22
|
+
retryAfterMs?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Parse an HTTP `Retry-After` header value (delta-seconds or HTTP-date) into milliseconds.
|
|
27
|
+
*/
|
|
28
|
+
export function parseRetryAfterMs(headerValue: string | null | undefined, now = Date.now()): number | null {
|
|
29
|
+
if (typeof headerValue !== "string") return null;
|
|
30
|
+
const trimmed = headerValue.trim();
|
|
31
|
+
if (!trimmed) return null;
|
|
32
|
+
if (/^\d+$/.test(trimmed)) {
|
|
33
|
+
const seconds = Number(trimmed);
|
|
34
|
+
if (!Number.isFinite(seconds) || seconds <= 0) return null;
|
|
35
|
+
return Math.min(seconds * 1000, MAX_RETRY_AFTER_MS);
|
|
36
|
+
}
|
|
37
|
+
const targetMs = Date.parse(trimmed);
|
|
38
|
+
if (Number.isNaN(targetMs) || targetMs <= now) return null;
|
|
39
|
+
return Math.min(targetMs - now, MAX_RETRY_AFTER_MS);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let testOverrides: TelemetrySendOptions | null = null;
|
|
43
|
+
|
|
44
|
+
export function setTelemetrySenderOverridesForTests(overrides: TelemetrySendOptions | null): void {
|
|
45
|
+
testOverrides = overrides;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function sendTelemetryPayload(
|
|
49
|
+
payload: TelemetryPayloadV1,
|
|
50
|
+
options: TelemetrySendOptions = {},
|
|
51
|
+
): Promise<TelemetrySendResult> {
|
|
52
|
+
const now = options.now ?? testOverrides?.now ?? Date.now();
|
|
53
|
+
const validated = validateTelemetryPayloadV1(payload, now);
|
|
54
|
+
if (!validated.ok) {
|
|
55
|
+
return { ok: false, error: validated.error };
|
|
56
|
+
}
|
|
57
|
+
const endpoint = options.endpoint ?? testOverrides?.endpoint ?? TELEMETRY_COLLECTOR_ENDPOINT;
|
|
58
|
+
const fetchImpl = options.fetch ?? testOverrides?.fetch ?? globalThis.fetch;
|
|
59
|
+
const timeoutMs = options.timeoutMs ?? testOverrides?.timeoutMs ?? TELEMETRY_SEND_TIMEOUT_MS;
|
|
60
|
+
const parentSignal = options.signal ?? testOverrides?.signal;
|
|
61
|
+
|
|
62
|
+
const deadline = signalWithTimeout(timeoutMs, parentSignal);
|
|
63
|
+
try {
|
|
64
|
+
const response = await fetchImpl(endpoint, {
|
|
65
|
+
method: "POST",
|
|
66
|
+
headers: {
|
|
67
|
+
"content-type": "application/json",
|
|
68
|
+
},
|
|
69
|
+
body: JSON.stringify(validated.payload),
|
|
70
|
+
// The collector URL is a constant; a redirect would send the report to another host.
|
|
71
|
+
redirect: "error",
|
|
72
|
+
signal: deadline.signal,
|
|
73
|
+
});
|
|
74
|
+
if (!response.ok) {
|
|
75
|
+
if (response.status === 429 || response.status === 503) {
|
|
76
|
+
const headerRetryMs = parseRetryAfterMs(response.headers?.get?.("retry-after"), now);
|
|
77
|
+
const retryAfterMs = headerRetryMs
|
|
78
|
+
?? (TELEMETRY_DEFAULT_RETRY_AFTER_MS + Math.floor(Math.random() * TELEMETRY_RETRY_JITTER_MS));
|
|
79
|
+
return {
|
|
80
|
+
ok: false,
|
|
81
|
+
status: response.status,
|
|
82
|
+
error: `collector responded ${response.status}`,
|
|
83
|
+
retryAfterMs,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return { ok: false, status: response.status, error: `collector responded ${response.status}` };
|
|
87
|
+
}
|
|
88
|
+
return { ok: true, status: response.status };
|
|
89
|
+
} catch (error) {
|
|
90
|
+
return {
|
|
91
|
+
ok: false,
|
|
92
|
+
error: error instanceof Error ? error.message : String(error),
|
|
93
|
+
};
|
|
94
|
+
} finally {
|
|
95
|
+
deadline.cleanup();
|
|
96
|
+
}
|
|
97
|
+
}
|