diff --git a/docs/docs/reference/security-faq.md b/docs/docs/reference/security-faq.md index ab524c7ac4..5132e924cf 100644 --- a/docs/docs/reference/security-faq.md +++ b/docs/docs/reference/security-faq.md @@ -166,9 +166,9 @@ export ALTIMATE_TELEMETRY_DISABLED=true - **Signed-in users:** Events carry your Altimate user ID and tenant, so they are not anonymous. No email address, hashed or not, is sent. See [User identification](telemetry.md) for how the ID is looked up and cached. - **Users who are not signed in:** A random UUID (`crypto.randomUUID()`) is generated on first run and stored at `~/.altimate/machine-id`. This is NOT tied to your hardware, OS, or identity — it's purely random. -- **Both identifiers** are only sent when telemetry is enabled. Disable with `ALTIMATE_TELEMETRY_DISABLED=true` or the `telemetry.disabled` config option. +- **Both identifiers** are only sent when telemetry is enabled. Disable with `ALTIMATE_TELEMETRY_DISABLED`, `ALTIMATE_CLI_DISABLE_TELEMETRY` or `OPENCODE_DISABLE_TELEMETRY` set to `1` or `true`, or the `telemetry.disabled` config option. - **No fingerprinting:** We do not use browser fingerprinting, hardware IDs, MAC addresses, or IP-based tracking. -- **CLI auth flow:** When you sign in via `altimate auth login`, the anonymous machine ID is included in the authorization URL and associated with your account in product analytics for funnel analysis. This is suppressed when you disable telemetry — via `ALTIMATE_TELEMETRY_DISABLED=true` or the `telemetry.disabled` config option — and the machine ID is omitted from the URL entirely. +- **CLI auth flow:** When you sign in via `altimate auth login`, the anonymous machine ID is included in the authorization URL and associated with your account in product analytics for funnel analysis. The machine ID is omitted from the URL entirely when you disable telemetry (any of those three environment variables, or the `telemetry.disabled` config option), and also while that setting cannot be resolved. ### What happens on first launch? @@ -183,8 +183,7 @@ No code, queries, file paths, or personal information is included. This event he The install scripts (`altimate.sh/install`, `install.ps1`), the npm postinstall, and the VS Code extension's installer send nothing themselves and contact no telemetry endpoint. They only record the version and installer name to a local file that the CLI reads on its next run, so the opt-out above decides whether anything is ever transmitted. -!!! warning "One caveat on the config-file opt-out" - The environment variables (`ALTIMATE_TELEMETRY_DISABLED`, `OPENCODE_DISABLE_TELEMETRY`) are always honoured. The `telemetry.disabled` **config key** is read from the config files at telemetry startup. If a config file cannot be read or parsed at that point, startup proceeds with telemetry enabled, so this event may still be transmitted. Use an environment variable if you need a guarantee. +The CLI sends zero usage telemetry requests when `ALTIMATE_TELEMETRY_DISABLED`, `ALTIMATE_CLI_DISABLE_TELEMETRY`, or `OPENCODE_DISABLE_TELEMETRY` is set to `1` or `true` (case-insensitive), or when `telemetry.disabled` is `true` in config. This also covers first launch and shutdown. If the opt-out setting is unresolved, the CLI holds events in memory and makes no telemetry requests. It discards them if telemetry is disabled, if the process exits before the setting resolves, or when a session ends for a project whose own config could not be read. A legacy global `config` (TOML) file that fails to import keeps the setting unresolved until it is fixed or removed and the config is reloaded (a long-running process reloads it on restart). One exception: a process that serves several projects (`serve`, an IDE) reads a project's own `telemetry.disabled` when that project's session starts, so a few of its events from before then can be sent if another, opted-in project already turned telemetry on. The environment variables are honoured process-wide from the start. A `telemetry.disabled` in your own files is normally read at CLI startup, before any project loads, so it holds for that process. A process that reads only a project's merged config (such as the TUI's worker thread) follows that config, where an organization config set by your admin is merged over your files. ## What happens when I authenticate via a well-known URL? diff --git a/docs/docs/reference/telemetry.md b/docs/docs/reference/telemetry.md index c5e1639caf..36f07bc1b3 100644 --- a/docs/docs/reference/telemetry.md +++ b/docs/docs/reference/telemetry.md @@ -65,7 +65,6 @@ We collect the following categories of events: | `gateway_auth_completed` / `gateway_auth_failed` | Gateway sign-in outcome. `reason` is `timeout`, `denied`, or `error` — never the underlying message, which can contain the instance name. An unrecognised callback state does not reject the pending attempt, so a CSRF mismatch surfaces as `timeout`. | | `instance_connected` | Credentials received and saved. `time_to_connect_ms` runs from the start of the authorize call, so it includes the browser launch. The event names no instance itself; like every event sent after sign-in, it carries the account's `user_id` and `tenant` once they are resolved (see [User Identification](#new-user-identification)). | | `onboarding_completed` | A model is ready and chat is live. | -| `telemetry_opted_out` | The last event from a machine that turns telemetry off, sent once (see [Disabling Telemetry](#disabling-telemetry)). Contains `opt_out_source` (`env` for an environment variable, `config` for `telemetry.disabled`), the machine ID, the CLI version and the client. It carries no account (`user_id`, `tenant`), project, session or launch ID. | | `scan_gate_shown` / `scan_gate_choice` | The "scan your environment?" gate appeared, and what the user did — `scan`, `skip`, or `dismissed` (esc / click-away). | | `environment_scan_completed` | A `project_scan` finished during onboarding — `has_dbt`, `has_warehouse`, `is_repo`, `connections_found`, and a bounded list of short `degraded` detection keys. No paths, hostnames, or connection details. Emitted only inside an onboarding session, and only once per session, so scans from `/discover` or a model-initiated call are excluded. | | `sample_setup_completed` | The sample dbt project was materialised. `success`, `models`, `tables`, and `reused` — the tool is deliberately re-callable, so this is per invocation. The target path is never sent. | @@ -88,7 +87,7 @@ We collect the following categories of events: | `plugin_session_start` | A Claude Code session started with the Altimate Code plugin, sent by the plugin through `altimate-code tool report-event`. Its payload is the plugin version, this altimate-code's version (`altimate_code_version`), the OS as Claude Code names it (`host_os`: `macos`, `linux`, `windows`, `wsl` or `unknown`; named apart from the `os` other events carry as `process.platform`) and the plugin's anonymous install id (a random UUID the plugin makes on first run and keeps in its Claude Code plugin store). It carries no session id, machine id, project id or account (`user_id`, `tenant`), nor the per-launch `launch_id`, so it cannot be joined to the CLI's other events: beside its own fields, only `cli_version`, `client` and `source`, keyed to the install id (`ai.user.id`). It is sent in a request of its own, never in one batch with the CLI's other events. | | `plugin_feature_used` | One of the plugin's panels or commands was opened (`lineage`, `dbt_show`, `profile_ctes`, `scan_cost`, `comments`, `sql_graph`, `health`, `runs`, `to_model`), or one of its gates held or allowed a command (`cost_gate`, `health_push_gate`). Its payload is the feature, the outcome (`opened`; `failed` when the panel could not open; `held` or `allowed` for a gate), the plugin version and the install id: no model, column, file or project names, SQL or session text. Like `plugin_session_start`, it carries no session id, machine id, project id, launch id or account: beside its own fields, only `cli_version`, `client` and `source`, keyed to the install id. | -Each event (except the plugin's `plugin_session_start` and `plugin_feature_used`, described above) includes a timestamp, session ID, a per-launch correlation ID (`launch_id` — a random value regenerated every process start, not persisted and not derived from your machine or identity; it exists only to group events from the same run), CLI version, and a machine ID (a random UUID stored in `~/.altimate/machine-id`, generated once and not derived from your hardware or identity). The final `telemetry_opted_out`, in the same table, is the other exception: it carries the timestamp, CLI version and machine ID, but no session ID, `launch_id` or account. When the machine is signed in to Altimate, each event also carries `user_id` and `tenant`, described under [User Identification](#new-user-identification). +Each event (except the plugin's `plugin_session_start` and `plugin_feature_used`, described above) includes a timestamp, session ID, a per-launch correlation ID (`launch_id` — a random value regenerated every process start, not persisted and not derived from your machine or identity; it exists only to group events from the same run), CLI version, and a machine ID when available (a random UUID stored in `~/.altimate/machine-id`, generated once and not derived from your hardware or identity). Signed-in events carry `user_id` and `tenant` once account identity resolves. See [User Identification](#new-user-identification). ### Workspace events @@ -183,13 +182,9 @@ You can also set the environment variable: export ALTIMATE_TELEMETRY_DISABLED=true ``` -When telemetry is disabled, a machine with an existing machine ID sends one final `telemetry_opted_out` event, then nothing: no further events and no requests to the telemetry endpoint until telemetry is turned back on. The event carries only the machine ID, the opt-out source, the CLI version and the client — see the event table above. With it, an opted-out machine can be counted instead of looking like a client that stopped working. +`ALTIMATE_CLI_DISABLE_TELEMETRY` and its older spelling `OPENCODE_DISABLE_TELEMETRY` are also supported. Each variable accepts `1` or `true` (case-insensitive). -- The event is sent only if `~/.altimate/machine-id` already exists. A machine that never ran with telemetry on sends nothing. -- Once the telemetry endpoint accepts the event, Altimate Code writes the marker file `~/.altimate/telemetry-opted-out`, and later launches send nothing. If the endpoint cannot be reached, no marker is written and a later launch tries again. -- While the event is being sent, a short-lived `~/.altimate/telemetry-opted-out.sending` file stops a second process from sending it too. -- Turning telemetry back on removes the marker, so a later opt-out is recorded again. -- No account lookup is made while telemetry is disabled. +When any of these variables or `telemetry.disabled: true` disables telemetry, the CLI makes zero usage telemetry requests. That includes startup, sessions, error reporting, shutdown, and the account lookup used to identify telemetry events. This applies even if the CLI has sent telemetry before. Until the CLI can read the setting (for example at startup, before the project config or a configured remote source that could set the flag has loaded), it sends nothing; see [Privacy](#privacy). Once a process reads the setting as disabled, that process sends nothing more, even for another project it serves. One exception: a process that serves several projects (`serve`, an IDE) reads a project's own `telemetry.disabled` when that project's session starts, so a few of its events from before then can be sent if another, opted-in project already turned telemetry on. The environment variables are honoured process-wide from the start. A `telemetry.disabled` in your own files is normally read at CLI startup, before any project loads, so it holds for that process. A process that reads only a project's merged config (such as the TUI's worker thread) follows that config, where an organization config set by your admin is merged over your files. ### Test runs are excluded @@ -207,7 +202,7 @@ Two escape hatches exist for reporting from a test run deliberately: is always honoured, which is how the project's own telemetry tests work. - Set `ALTIMATE_TELEMETRY_FORCE=true` to use the default endpoint anyway. -`ALTIMATE_TELEMETRY_DISABLED` and the config opt-out take precedence over both. +The environment opt-outs and the config opt-out take precedence over these settings. ## Privacy @@ -232,11 +227,11 @@ What identifies the sender of an event depends on whether the machine is signed The Claude Code plugin's `plugin_session_start` and `plugin_feature_used` are the exception: signed in or not, they carry neither the machine ID nor your account, and their `ai.user.id` is the plugin's own random install id. -These identifiers are sent only while telemetry is enabled. The final `telemetry_opted_out` event carries the machine ID and nothing that identifies your account, and no `/users/me` lookup is made while telemetry is disabled. Disable telemetry entirely with `ALTIMATE_TELEMETRY_DISABLED=true` or the config option above. +These identifiers are sent only while telemetry is enabled. While the opt-out setting is unresolved, Altimate Code holds events in a bounded in-memory buffer and skips telemetry requests, including the `/users/me` lookup. If a later initialization resolves telemetry as enabled, it sends the queued events. It discards them if it resolves as disabled, if the process exits while the setting is still unresolved, or when a session ends for a project whose own config could not be read. A legacy global `config` (TOML) file that fails to import keeps the setting unresolved until it is fixed or removed and the config is reloaded (a long-running process reloads it on restart). ### CLI Authentication Flow -When you sign in using the CLI browser auth flow (`altimate auth login`), the anonymous machine ID (a random UUID persisted at `~/.altimate/machine-id` — a device/installation identifier, reused across sessions) is included in the authorization URL and associated with your account in product analytics. This is used solely to correlate CLI install events with authenticated accounts in aggregate funnel analytics — it is not used for advertising or cross-site tracking. Your telemetry opt-out suppresses this: when you disable telemetry — via `ALTIMATE_TELEMETRY_DISABLED=true` **or** the `telemetry.disabled` config option — the machine ID is omitted from the authorization URL entirely. The machine ID is associated with your account in PostHog for this funnel analysis, separate from the Azure Application Insights pipeline used for other CLI telemetry events. +When you sign in using the CLI browser auth flow (`altimate auth login`), the anonymous machine ID (a random UUID persisted at `~/.altimate/machine-id` — a device/installation identifier, reused across sessions) is included in the authorization URL and associated with your account in product analytics. This is used solely to correlate CLI install events with authenticated accounts in aggregate funnel analytics — it is not used for advertising or cross-site tracking. Your telemetry opt-out suppresses this: when you disable telemetry — via any of the environment flags above or the `telemetry.disabled` config option — the machine ID is omitted from the authorization URL entirely. The URL also omits it while telemetry permission is unresolved. The machine ID is associated with your account in PostHog for this funnel analysis, separate from the Azure Application Insights pipeline used for other CLI telemetry events. ### Data Retention diff --git a/packages/opencode/src/altimate/debug/collect.ts b/packages/opencode/src/altimate/debug/collect.ts index 2d35a56241..fc8ab693a0 100644 --- a/packages/opencode/src/altimate/debug/collect.ts +++ b/packages/opencode/src/altimate/debug/collect.ts @@ -15,6 +15,7 @@ import { Global } from "@opencode-ai/core/global" import { InstallationVersion } from "@opencode-ai/core/installation/version" import { resolveInstall } from "@/installation" import { Telemetry } from "@/altimate/telemetry" +import { TelemetryConfig } from "@/altimate/telemetry/config-state" import { AltimateApi } from "@/altimate/api/client" import * as Registry from "@/altimate/native/connections/registry" import { resolveConfig } from "@/altimate/native/connections/credential-store" @@ -89,15 +90,21 @@ function envFlags(): string[] { }) } +function optOutReason(state: "pending" | "enabled" | "disabled") { + if (state === "pending") return "config not readable yet" + if (state === "disabled") return "disabled in config" + return "not running in this process (no telemetry sink, or this session's config not resolved yet)" +} + /** With `network`, telemetry has been started by the CLI; its start is awaited here so the answer is not read * before it settles. Without, it is not started (it would send), so only the environment switches are checked. */ async function telemetryFacts(network: boolean): Promise { - if (/^(1|true)$/i.test(process.env["ALTIMATE_TELEMETRY_DISABLED"] ?? "")) return { enabled: false, reason: "ALTIMATE_TELEMETRY_DISABLED is set" } - if (/^(1|true)$/i.test(process.env["OPENCODE_DISABLE_TELEMETRY"] ?? "")) return { enabled: false, reason: "OPENCODE_DISABLE_TELEMETRY is set" } + const optOut = TelemetryConfig.disablingEnvironmentVariable() + if (optOut) return { enabled: false, reason: `${optOut} is set` } if (!network) return { enabled: false, checked: false, reason: "not started with --no-network" } try { await Telemetry.init() - return Telemetry.isEnabled() ? { enabled: true } : { enabled: false, reason: "disabled in config" } + return Telemetry.isEnabled() ? { enabled: true } : { enabled: false, reason: optOutReason(Telemetry.optOutState()) } } catch { return { enabled: false, checked: false, reason: "could not be determined" } } diff --git a/packages/opencode/src/altimate/plugin/altimate.ts b/packages/opencode/src/altimate/plugin/altimate.ts index baf989aa1d..e00ee8451c 100644 --- a/packages/opencode/src/altimate/plugin/altimate.ts +++ b/packages/opencode/src/altimate/plugin/altimate.ts @@ -1,6 +1,8 @@ import type { Hooks, PluginInput } from "@opencode-ai/plugin" import { createServer } from "http" import { randomBytes } from "crypto" +import path from "path" // altimate_change — compare the handoff project with the Instance +import { Filesystem } from "@/util/filesystem" // altimate_change — canonicalize as Instance.provide does import open from "open" import { AltimateApi } from "../api/client" // altimate_change — onboarding telemetry for the gateway sign-in funnel @@ -8,8 +10,6 @@ import * as OnboardingTelemetry from "../telemetry/onboarding" import { Telemetry } from "../telemetry" // altimate_change — shared machine-id helper (race-safe, UUID-validated, size-capped) import { getOrCreateMachineId } from "../util/machine-id" -import { Config } from "@/config/config" -import { Flag } from "@/flag/flag" import { InstallationVersion } from "@opencode-ai/core/installation/version" import { Log } from "@/altimate/util/log" // altimate_change — reads the config telemetry opt-out without an Instance @@ -77,46 +77,53 @@ const log = Log.create({ service: "altimate-plugin" }) // and `cli_version` are strings, treat `cli_version: "local"` as a valid dev // build, and treat the payload as untrusted (anyone can craft a URL). An absent // `machine_id` means "do not attribute" — it must never be aliased on. +// altimate_change — the Instance's directory is canonical (symlinks resolved); compare like with like +function canonicalDirectory(directory: string): string { + try { + return Filesystem.resolve(directory) + } catch { + return path.resolve(directory) + } +} + /** `directory`: the project the command acts on, whose config may opt out; the process's working * directory when the command has none (`auth login`). */ export async function buildCliContext(machineIdPath?: string, directory?: string): Promise { // altimate_change start — honour both telemetry opt-out gates, mirroring // telemetry/index.ts::doInit: - // 1. ALTIMATE_TELEMETRY_DISABLED / OPENCODE_DISABLE_TELEMETRY env vars - // ("true"/"TRUE"/"1", case-insensitive — see Flag.truthyEnv) - // 2. config.telemetry.disabled (resolved via the async Config.get()) - let disabled = - Flag.truthyEnv("ALTIMATE_TELEMETRY_DISABLED") || Flag.truthyEnv("OPENCODE_DISABLE_TELEMETRY") + // 1. the opt-out env vars (TelemetryConfig.disablingEnvironmentVariable) + // 2. config.telemetry.disabled; a config that cannot be fully resolved omits the id + // A project this process already resolved as opted out stops it for every project it serves. + let disabled = Boolean(TelemetryConfig.disablingEnvironmentVariable()) || Telemetry.optOutState() === "disabled" if (!disabled) { - try { - const userConfig = (await Config.get()) as any - disabled = Boolean(userConfig.telemetry?.disabled) - } catch (err) { - // Config was unreadable — NOT the normal path. Server routes run inside - // Instance.provide() (AsyncLocalStorage-propagated across awaits), so - // Config.get() resolves during an ordinary browser authorize(). It fires on - // the paths that skip instance bootstrap: `altimate auth login ` and the + // Server routes run inside Instance.provide(), so the full config resolves during an + // ordinary browser authorize(). + const loaded = await TelemetryConfig.loadedState().catch(() => undefined) + const instance = TelemetryConfig.instanceDirectory() + // A handoff for another project than the one this Instance serves: that project's own files + // may opt out, so they must allow it too. The remote and organization sources are the same for + // both projects and the loaded config already covers them. + const otherProject = Boolean(instance && directory && canonicalDirectory(directory) !== instance) + if (loaded) { + disabled = loaded !== "enabled" || (otherProject && (await TelemetryConfig.disabledState(directory, { filesOnly: true })) !== "enabled") + } else if (instance) { + disabled = true + } else { + // The paths that skip instance bootstrap: `altimate auth login ` and the // create-and-link hand-off of `altimate-code link`. Read `telemetry.disabled` // from the config files directly: an opt-out there, or files that cannot be // read (an opt-out may be behind them), omit the id. Only a config that // readably leaves telemetry on defers to the telemetry module's own state - // (opted out by env, no sink, test runner). + // (no sink, test runner). const state = await TelemetryConfig.disabledState(directory) - if (state === "enabled") { - await Telemetry.init().catch(() => {}) - disabled = !Telemetry.isEnabled() - } else { - disabled = true - } - log.info("cli_context: config unavailable, read the opt-out from the files", { - code: (err as NodeJS.ErrnoException)?.code, - state, - enabled: !disabled, - }) + if (state === "enabled") await Telemetry.init().catch(() => {}) + disabled = state !== "enabled" || !Telemetry.isEnabled() + log.info("cli_context: no instance, read the opt-out from the files", { state, enabled: !disabled }) } } let machineId = "" - if (!disabled) { + // An opt-out adopted while the config was read still wins. + if (!disabled && Telemetry.optOutState() !== "disabled") { // getOrCreateMachineId returns "" on all error conditions (ENOENT excluded — // it mints a new UUID instead) and logs appropriately; no try/catch needed. machineId = getOrCreateMachineId(machineIdPath) diff --git a/packages/opencode/src/altimate/telemetry/config-state.ts b/packages/opencode/src/altimate/telemetry/config-state.ts index a5d1789755..fb9e680bcf 100644 --- a/packages/opencode/src/altimate/telemetry/config-state.ts +++ b/packages/opencode/src/altimate/telemetry/config-state.ts @@ -1,32 +1,38 @@ +import { Database as SQLite, constants as SQLiteConstants } from "bun:sqlite" import path from "path" import fs from "fs/promises" import { existsSync } from "fs" +import { pathToFileURL } from "url" import * as Effect from "effect/Effect" +import { InstanceRef } from "@/effect/instance-ref" +import { attach } from "@/effect/run-service" import { Flag, env as flagEnv } from "@opencode-ai/core/flag/flag" import { FSUtil } from "@opencode-ai/core/fs-util" +import { Database } from "@opencode-ai/core/database/database" +import { Auth } from "@/auth" import { Global } from "../../global" import { ConfigPaths } from "@/config/paths" import { ConfigManaged } from "@/config/managed" -import { validateLoaded } from "@/config/config" +import { Config, validateLoaded } from "@/config/config" /** * Whether the user config turns telemetry off, read without an Instance. * * `Config.get()` needs an Instance, and the CLI's main thread initialises telemetry in middleware - * before one exists, so there it throws. Treating that as "enabled" let an opted-out user's main - * thread clear the opt-out marker and resolve their identity. This reads the same files `Config` + * before one exists, so there it throws. This reads the same files `Config` * merges, in the same order (a later source overrides an earlier one), straight from disk: * the global config files (the legacy TOML `config` file included), `OPENCODE_CONFIG`, the project * files and `.altimate-code`/`.opencode` directories found from `directory`, the inline * `OPENCODE_CONFIG_CONTENT`, the managed config directory and macOS managed preferences. Remote and - * account configs are not read (they need the network). The read emits no telemetry of its own: it - * runs before telemetry knows whether it may. + * account configs are not fetched here. If either could still opt out, the answer is unknown. + * The read runs before telemetry knows whether it may send. * * - `disabled`: the last source that sets `telemetry.disabled` sets it to `true`. * - `enabled`: no source turns it off. * - `unreadable`: some source could not be read, parsed or validated as `Config` does (a missing * `{file:}` reference, a value its schema rejects). `Config` would fail to load at all, so the - * answer is unknown. Callers fail closed for anything an opt-out forbids. + * answer is unknown. A configured remote source that could still opt out is also unknown. + * Callers fail closed for anything an opt-out forbids. */ export type State = "disabled" | "enabled" | "unreadable" @@ -44,15 +50,146 @@ function worktreeOf(directory: string): string | undefined { } } -export async function disabledState(directory: string = process.cwd()): Promise { +export function disablingEnvironmentVariable(): string | undefined { + return ["ALTIMATE_TELEMETRY_DISABLED", "ALTIMATE_CLI_DISABLE_TELEMETRY", "OPENCODE_DISABLE_TELEMETRY"].find((key) => + /^(true|1)$/i.test(process.env[key] ?? ""), + ) +} + +function parseJson(text: string): unknown { + try { + return JSON.parse(text) + } catch { + return undefined + } +} + +async function hasWellKnownAuth(): Promise { + // As Auth reads it: OPENCODE_AUTH_CONTENT when it parses, otherwise the auth file. + const inline = process.env.OPENCODE_AUTH_CONTENT + let parsed: unknown = inline ? parseJson(inline) : undefined + if (parsed === undefined) { + let contents: string + try { + contents = await fs.readFile(Auth.filepath(), "utf8") + } catch (error) { + if ((error as NodeJS.ErrnoException)?.code === "ENOENT") return false + throw error + } + // Unparseable: a well-known entry may be behind it. `Config` would load without its remote config. + // An empty file (touched, or a crash between Auth.set's truncate and write) holds no entry, as Auth.all() + // reads it; anything else that does not parse (whitespace included) is unreadable there too. + parsed = contents === "" ? {} : JSON.parse(contents) + } + // Only a plain object is a store this probe can rule on. Anything else is unreadable (fail closed): some + // such values (null) fail Config's load outright, and it is not worth matching Auth case by case. + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error("Unreadable auth store") + // Any well-known-typed entry counts: Auth passes OPENCODE_AUTH_CONTENT through without decoding it, so + // Config can fetch remote config even for an entry the file path would reject. For the file this is + // looser than Auth's decode (a malformed entry also counts), which only errs toward holding (fail closed). + return Object.values(parsed).some( + (entry) => entry && typeof entry === "object" && (entry as { type?: unknown }).type === "wellknown", + ) +} + +export function instanceDirectory(): string | undefined { + return Effect.runSync(attach(InstanceRef))?.directory +} + +/** The opt-out from the full config; throws where `Config` cannot load (outside an Instance). */ +export async function loadedState(): Promise { + const config = await Config.get() + // An opt-out the readable sources already set stands, even if another source could not be read. + if (config.telemetry?.disabled === true) return "disabled" + // Config marks a load whose auth store could not be read (Auth.all() records it), so the store is + // not read again here: the load that built this config is what counts. + if (Config.telemetryConfigUnresolved(config)) return "unreadable" + return "enabled" +} + +/** The opt-out as the caller can best read it: the full config inside an Instance (a failed load can + * hide remote settings, so it is `unreadable`), the files on disk for commands that have none yet. */ +export async function resolve(directory?: string): Promise { + try { + return await loadedState() + } catch { + return instanceDirectory() ? "unreadable" : disabledState(directory) + } +} + +async function remoteMayDisable(local: boolean | undefined): Promise { + const wellKnown = await hasWellKnownAuth() + if (local === undefined && wellKnown) return true + + const dbPath = Database.path() + // An in-memory database belongs to the connection that opened it: this probe cannot see whether an + // organization was selected there, so the answer is unknown. + if (dbPath === ":memory:") return true + + // Only read a WAL database. In rollback-journal mode even a short SELECT + // takes a shared lock that can block a writer's commit. + const header = await fs.open(dbPath, "r").catch((error: NodeJS.ErrnoException) => { + if (error.code === "ENOENT") return undefined + throw error + }) + if (!header) return false + try { + const versions = Buffer.alloc(2) + const { bytesRead } = await header.read(versions, 0, 2, 18) + // An empty file is new: it holds no account yet. A short non-empty one is corrupt: unknown. + if (bytesRead !== 2) return (await header.stat()).size > 0 + // A non-WAL database is not read (see above), so an organization it may hold is unknown: fail closed. + if (versions[0] !== 2 || versions[1] !== 2) return true + } finally { + await header.close() + } + + // A read-only open of a WAL database needs its -wal/-shm files, which SQLite removes when the last + // connection closes cleanly (Linux, Windows). Without a -wal file every change is in the main file and + // no writer is attached, so read it as immutable (which reports its journal mode as "delete"). + const immutable = !existsSync(`${dbPath}-wal`) + const db = immutable + ? // escapes `#`, `?`, `%`; URI parsing must be asked for explicitly where SQLite is built without it (Linux) + new SQLite(`${pathToFileURL(dbPath).href}?immutable=1`, SQLiteConstants.SQLITE_OPEN_READONLY | SQLiteConstants.SQLITE_OPEN_URI) + : new SQLite(dbPath, { readonly: true }) + try { + db.exec("PRAGMA busy_timeout = 0") + // If the journal mode changed since the header read, defer to the full loader. + if (!immutable && db.query<{ journal_mode: string }, []>("PRAGMA journal_mode").get()?.journal_mode !== "wal") return true + const tables = db.query<{ count: number }, []>( + "SELECT count(*) AS count FROM sqlite_schema WHERE name IN ('account', 'account_state') AND type = 'table'", + ).get() + // An immutable read is a snapshot: a writer that attached since (a -wal file appeared) may have + // selected an organization, so the answer is unknown. + const writerAttached = () => immutable && existsSync(`${dbPath}-wal`) + if (tables?.count !== 2) return writerAttached() // A pre-migration database cannot select an organization. + const modern = db.query("SELECT 1 FROM pragma_table_info('account_state') WHERE name = 'active_org_id'").get() + // The earlier schema stored the selected org on the account row. The migration + // copies that value into account_state, so it can still enable remote config. + const orgColumn = modern ? "account_state.active_org_id" : "account.selected_org_id" + const selected = db.query(`SELECT 1 FROM account_state + JOIN account ON account.id = account_state.active_account_id + WHERE account_state.id = 1 AND ${orgColumn} IS NOT NULL`).get() !== null + return selected || writerAttached() + } finally { + db.close() + } +} + +/** `filesOnly` skips the remote and organization sources, for a caller whose loaded config already covers them. */ +export async function disabledState(directory: string = process.cwd(), opts?: { filesOnly?: boolean }): Promise { try { let value: boolean | undefined // Any source that fails to read, parse or validate fails Config's whole load, so the answer // is unknown however the other sources set it. let unknown = false + // Managed config is merged after the remote and organization configs, so a setting there wins. + let readingManaged = false + let managedSetting = false const take = (data: unknown) => { const disabled = (data as { telemetry?: { disabled?: unknown } } | undefined)?.telemetry?.disabled if (disabled === undefined) return + if (readingManaged) managedSetting = true if (typeof disabled !== "boolean") unknown = true else value = disabled } @@ -109,6 +246,7 @@ export async function disabledState(directory: string = process.cwd()): Promise< const inline = flagEnv("OPENCODE_CONFIG_CONTENT") if (inline) await readText(inline, "OPENCODE_CONFIG_CONTENT", directory) + readingManaged = true const managedDir = ConfigManaged.managedConfigDir() if (existsSync(managedDir)) for (const file of DIR_FILES) await readFile(path.join(managedDir, file)) const managed = await ConfigManaged.readManagedPreferences().catch(() => { @@ -118,7 +256,10 @@ export async function disabledState(directory: string = process.cwd()): Promise< if (managed) await readText(managed.text, managed.source, path.dirname(managed.source)) if (unknown) return "unreadable" - return value ? "disabled" : "enabled" + // The files themselves opt out: that stands, even if a remote config might turn it back on (fail closed). + if (value === true) return "disabled" + if (!opts?.filesOnly && !managedSetting && (await remoteMayDisable(value))) return "unreadable" + return "enabled" } catch { return "unreadable" } diff --git a/packages/opencode/src/altimate/telemetry/identity.ts b/packages/opencode/src/altimate/telemetry/identity.ts index 23625b2cf5..c2bb1d912e 100644 --- a/packages/opencode/src/altimate/telemetry/identity.ts +++ b/packages/opencode/src/altimate/telemetry/identity.ts @@ -135,7 +135,7 @@ export type Outcome = "ok" | "none" | "rejected" | "error" /** {@link resolve}, also saying how it ended, so a caller retries only a transient failure. */ export async function resolveWithOutcome( - opts: { refresh?: boolean } = {}, + opts: { refresh?: boolean; canSend?: () => boolean } = {}, ): Promise<{ identity?: Identity; outcome: Outcome }> { try { const creds = await loadCredential() @@ -149,6 +149,7 @@ export async function resolveWithOutcome( if (hit && hit.fingerprint === fp) return { identity: { user_id: hit.user_id, tenant: hit.tenant }, outcome: "ok" } } + if (opts.canSend && !opts.canSend()) return { outcome: "none" } const answer = await fetchMe(creds) if (answer.kind === "rejected") { const hit = await readCache() diff --git a/packages/opencode/src/altimate/telemetry/index.ts b/packages/opencode/src/altimate/telemetry/index.ts index 5df4d68dc3..2f1954815b 100644 --- a/packages/opencode/src/altimate/telemetry/index.ts +++ b/packages/opencode/src/altimate/telemetry/index.ts @@ -1,4 +1,3 @@ -import { Config } from "@/config/config" import { Flag } from "@/flag/flag" import { InstallationVersion } from "@opencode-ai/core/installation/version" import { Log } from "@/altimate/util/log" @@ -8,7 +7,6 @@ import { getOrCreateMachineId } from "@/altimate/util/machine-id" // altimate_change — the Claude Code plugin's actions, whose events are sent anonymous (see ANONYMOUS_EVENTS) import { PLUGIN_ACTIONS } from "@/altimate/tool-cli/report-event" import { TelemetryIdentity } from "./identity" -import { OptOutMarker } from "./opt-out" import { TelemetryConfig } from "./config-state" import { createHash, randomUUID } from "crypto" // altimate_change — first-run health: the event-loop stall monitor labels which thread stalled @@ -69,17 +67,9 @@ const log = Log.create({ service: "telemetry" }) */ function isAutomatedRun(): boolean { if (process.env.ALTIMATE_TELEMETRY_FORCE === "true") return false - return underTestRunner() -} - -// altimate_change start — the raw test-runner check, without the force flag. The opt-out marker -// reads and writes files in the real home directory, so under a test runner it stays inert unless -// a suite opts in through Telemetry.allowOptOutUnderTestRunner(); the force flag must not reach it. -function underTestRunner(): boolean { if (process.env.NODE_ENV === "test") return true return Boolean(process.env.BUN_TEST || process.env.VITEST || process.env.JEST_WORKER_ID) } -// altimate_change end // altimate_change start — composed path-masking rules (shared fragments) // R: one path-run character — anything but whitespace/separators/quotes, @@ -1058,14 +1048,6 @@ export namespace Telemetry { // The 0.11.0 first-run freeze (an in-process @npmcli/arborist install blocking the loop for minutes) // was invisible for two months: nothing timed startup or registration, and a blocked loop cannot // flush, so a killed process left only `session_start`. These events close that gap. - | { - /** The last event an opted-out machine sends; nothing follows it until telemetry is - * turned back on. Built directly in recordOptOut(), never through track(). */ - type: "telemetry_opted_out" - timestamp: number - /** Where the opt-out was set: an environment variable or the user config. */ - opt_out_source: "env" | "config" - } | { type: "startup_ready" timestamp: number @@ -2325,15 +2307,109 @@ export namespace Telemetry { // once by the stamp comparison; the backoff only throttles retries of the unchanged one. const IDENTITY_RETRY_MS = 60_000 let identityRetryAt = 0 // 0: no retry pending - // altimate_change start — bumped by shutdown(); a detached opt-out send started under an earlier - // lifecycle does not post. Its marker write keys on markerGeneration instead, which only an - // enabled init bumps: a send accepted across a shutdown while still opted out keeps its marker. - let lifecycleGeneration = 0 - let markerGeneration = 0 + // altimate_change start — process-wide opt-out gate. Events are tracked while the opt-out is + // unknown (CLI startup, before config can be read) but nothing leaves until an init resolves the + // config to enabled. An opt-out (env or config) makes it `disabled` for the rest of the process, + // so a later init never sends what an earlier one refused. Unlike `enabled`, it survives the + // per-session shutdown; it is never reset outside tests. + let gate: "pending" | "enabled" | "disabled" = "pending" + // While the config is unreadable, re-resolve it at most this often. + const CONFIG_RETRY_MS = 30_000 + let configRetry: { at: number } | undefined + // A project's own read (inside an Instance) came back unreadable: its held events are not kept past + // its session, where another project's decision could send them. + let unresolvedInProject = false + // An enabled opt-out resolved by another thread of this launch (the TUI worker), used by the next init. + let adopted: "enabled" | undefined + const decisionListeners = new Set<(state: "enabled" | "disabled") => void>() + function decide(next: "enabled" | "disabled") { + if (gate === next) return + gate = next + for (const listener of decisionListeners) { + try { + listener(next) + } catch { + // A listener (the TUI worker's RPC push) must not undo this thread's own decision. + } + } + } + /** The opt-out as this thread has resolved it. */ + export function optOutState() { + return gate + } + /** An init ran and could not settle the opt-out: a later init with more config (inside a project) may. */ + export function awaitingConfig() { + return gate === "pending" && configRetry !== undefined + } + /** For a CLI command that has just loaded its project: the startup init ran before the project + * existed, and if it could not settle the opt-out (a remote or organization config), read it now + * from the full config, so a command that starts no session neither drops nor leaks those events. */ + export async function settleInProject(): Promise { + if (gate !== "pending" && !initPromise) return // decided and idle: nothing to wait for + await settledOptOutState() // the startup init is not awaited; let it finish first + if (awaitingConfig()) await init().catch(() => {}) + } + /** Called each time this thread decides the opt-out, for another thread to adopt. */ + export function onOptOutDecided(listener: (state: "enabled" | "disabled") => void): () => void { + decisionListeners.add(listener) + return () => decisionListeners.delete(listener) + } + /** The opt-out once this thread's running init has settled (for another thread to adopt). */ + export async function settledOptOutState() { + await initPromise?.catch(() => {}) + return gate + } + /** Take the opt-out another thread of this launch resolved: the TUI main thread never loads a project, + * so where its own probe cannot read the config (a remote or organization config) it uses the + * worker's answer. Does nothing once this thread has decided, or while the other has not. */ + export function adoptOptOut(state: "pending" | "enabled" | "disabled"): Promise { + if (state === "pending" || gate === "disabled") return Promise.resolve() + if (state === "disabled") { + // An opt-out wins even after this thread enabled telemetry (a config reload in the worker): + // stop the running lifecycle too, so nothing more is sent from this process. + decide("disabled") + buffer = [] + droppedEvents = 0 + enabled = false + if (flushTimer) clearInterval(flushTimer) + flushTimer = undefined + activeFlushAbort?.abort() // a batch already taken must not post after the opt-out + return Promise.resolve() + } + // Not during a shutdown: a re-init chained behind the exit flush would start a lifecycle nobody ends. + if (gate !== "pending" || shutdownPromise) return Promise.resolve() + adopted = "enabled" + const shutdownsAtCall = shutdownsStarted + // Checked again once the running init settles: a shutdown may have started (or finished) meanwhile. + return (initPromise ?? Promise.resolve()) + .catch(() => {}) + .then(() => (shutdownsStarted !== shutdownsAtCall ? undefined : init())) + } + /** Test seam: forget the process-wide opt-out decision and the events held for it. A lifecycle a + * test left running and enabled keeps its decision, so it goes on sending as it did before the gate. */ + export function resetGateForTest() { + configRetry = undefined + unresolvedInProject = false + adopted = undefined + if (initDone && enabled) return + gate = "pending" + buffer = [] + droppedEvents = 0 + } + /** Test seam. */ + export function bufferedCountForTest() { + return buffer.length + } + // The test preload resets the gate between tests through this, without importing this module. Only + // when that preload asked for it: in a shipped process nothing else can reopen a decided opt-out. + if (process.env.ALTIMATE_TELEMETRY_TEST_HOOKS === "1") { + ;(globalThis as Record)[Symbol.for("altimate.telemetry.resetGateForTest")] = resetGateForTest + } + /** Test seam: end the unreadable-config retry backoff so the next init() re-resolves. */ + export function retryConfigNowForTest() { + if (configRetry) configRetry.at = 0 + } // altimate_change end - // altimate_change — false when this init could not read the user config: an opted-out user may be - // behind it, so no identity is resolved (at init, at a flush or on refresh) until an init that can. - let identityAllowed = true // altimate_change — the project the command will run in, for reading the config without an // Instance: the CLI initialises telemetry before a command changes into its `--dir` or project. let configDirectory: string | undefined @@ -2364,6 +2440,8 @@ export namespace Telemetry { // altimate_change — in-flight shutdown, declared with the rest of the module state so init() // above can consult it. See shutdown() for why concurrent shutdowns must be serialized. let shutdownPromise: Promise | undefined + // Counts shutdowns started, so a deferred init can tell one began while it waited. + let shutdownsStarted = 0 // altimate_change — init chained onto an in-flight shutdown; see init(). let reinitPromise: Promise | undefined // altimate_change — the currently running flush, so shutdown waits rather than racing it. @@ -2415,9 +2493,6 @@ export namespace Telemetry { return { iKey, endpoint: `${base}v2/track` } } - // altimate_change — `ids` lets a caller build envelopes for a machine and identity other than - // the module's current state (the opt-out marker runs while telemetry is disabled and must not - // touch the state an enabled init may be using concurrently). // altimate_change start — the Claude Code plugin's usage events are anonymous and unlinkable: they carry // none of the identifiers the CLI's other events share (account, machine id, project, session, launch id), // so they cannot be joined to a signed-in row. They are keyed to the plugin's own random install id only. @@ -2427,10 +2502,9 @@ export namespace Telemetry { function toAppInsightsEnvelopes( events: Event[], cfg: AppInsightsConfig, - ids?: { machineId: string; identity: TelemetryIdentity.Identity | undefined }, + ids: { machineId: string; identity: TelemetryIdentity.Identity | undefined }, ): object[] { - const mid = ids ? ids.machineId : machineId - const who = ids ? ids.identity : identity + const { machineId: mid, identity: who } = ids // Process-level client, read live from the env flag (defaults to "cli") so events // emitted before any prompt runs — startup, connection setup, standalone CLI // subcommands — are labelled correctly instead of stuck at a hardcoded default. @@ -2531,12 +2605,14 @@ export namespace Telemetry { // wait for the new init rather than joining the dying one. if (shutdownPromise) { if (!reinitPromise) { - reinitPromise = shutdownPromise + const chained: Promise = shutdownPromise .catch(() => {}) - .then(doInit) + .then(() => (retryThrottled() ? startLoopMonitor() : doInit())) // altimate_change — the throttle applies here too + .then(() => releaseUnresolved(chained)) // altimate_change — see releaseUnresolved .finally(() => { reinitPromise = undefined }) + reinitPromise = chained // Publish it as initPromise too. Without this, a caller arriving after the shutdown had // settled — but before this chained doInit() finished — saw an empty initPromise and // started a SECOND one, replacing the flush timer and racing over the buffer. @@ -2551,42 +2627,72 @@ export namespace Telemetry { } return reinitPromise } - return (initPromise ??= doInit()) + // While the config stays unreadable, re-read the files outside an Instance at most every + // CONFIG_RETRY_MS. Inside an Instance a read is the Instance's cached config (its own reloads keep + // it current), so every init reads it: one project's unreadable config never holds up another's. + // An opt-out adopted from another thread needs no read. + if (!initPromise && retryThrottled()) { + startLoopMonitor() // a shutdown stopped it; the stall watcher runs whether or not telemetry does + return Promise.resolve() + } + if (!initPromise) { + const started: Promise = doInit().then(() => releaseUnresolved(started)) + initPromise = started + } + return initPromise // altimate_change end } + // altimate_change — an unreadable config is re-read outside an Instance at most every CONFIG_RETRY_MS + function retryThrottled() { + return !adopted && configRetry !== undefined && Date.now() < configRetry.at && !TelemetryConfig.instanceDirectory() + } + + // altimate_change start — an init left unresolved stays pre-init so the next init() retries; only + // the init that still owns initPromise unpublishes it (not a re-init published during a shutdown) + function releaseUnresolved(owner: Promise) { + if (!initDone && initPromise === owner) initPromise = undefined + } + // altimate_change end + async function doInit() { + // altimate_change — an unreadable config leaves this init unfinished, so the next init() retries. + let unresolved = false try { - // altimate_change — accept "true"/"TRUE"/"1" (case-insensitive) via truthyEnv, - // and honor the OPENCODE_DISABLE_TELEMETRY fallback promised by v0.9.4's CHANGELOG - // (previously only wired in test fixtures, silent no-op in product). - if (Flag.truthyEnv("ALTIMATE_TELEMETRY_DISABLED") || Flag.truthyEnv("OPENCODE_DISABLE_TELEMETRY")) { + // altimate_change start — resolve the opt-out before anything can be sent. Env vars first, + // then the config: the full one inside an Instance, the files on disk outside one. + if (gate === "disabled" || TelemetryConfig.disablingEnvironmentVariable()) { + decide("disabled") buffer = [] - optOutSend = recordOptOut("env") + droppedEvents = 0 return } - // Config.get() may throw outside Instance context (e.g. CLI middleware - // before Instance.provide()). Telemetry stays enabled then, as before. - // altimate_change start — but the opt-out still counts: read `telemetry.disabled` from the - // config files directly, and when even that fails, skip what an opt-out forbids (clearing the - // opt-out marker, resolving the account) while ordinary rows keep flowing. - let configUnreadable = false - try { - const userConfig = (await Config.get()) as any - if (userConfig.telemetry?.disabled) { - buffer = [] - optOutSend = recordOptOut("config") - return - } - } catch { - const state = await TelemetryConfig.disabledState(configDirectory) - if (state === "disabled") { - buffer = [] - optOutSend = recordOptOut("config") - return - } - configUnreadable = state === "unreadable" + let state = adopted ?? (await TelemetryConfig.resolve(configDirectory)) + // Outside an Instance only the files can be read. Where they cannot settle it (a remote or + // organization config), an enabled decision a full config already made in this process stands; + // an opt-out the files do show still wins. + if (state === "unreadable" && gate === "enabled" && !TelemetryConfig.instanceDirectory()) state = "enabled" + // Another thread's opt-out may have been adopted while the read ran: it still wins. + if (state === "disabled" || optOutState() === "disabled") { + decide("disabled") + buffer = [] + droppedEvents = 0 + return + } + if (state === "unreadable") { + // An opted-out user may be behind it: hold the buffer (capped) and send nothing. + unresolved = true + const inProject = Boolean(TelemetryConfig.instanceDirectory()) + // A warning, once per hold: a corrupt auth file or a failed organization config fetch holds + // telemetry this way. + if (!configRetry) log.warn("telemetry held: opt-out unresolved", { instance: inProject }) + configRetry = { at: Date.now() + CONFIG_RETRY_MS } + if (inProject) unresolvedInProject = true + return } + decide("enabled") + configRetry = undefined + unresolvedInProject = false // altimate_change end // App Insights: env var overrides default (for dev/testing), otherwise use the baked-in key. // The baked-in key is refused under a test runner so suites never ship to the production @@ -2607,25 +2713,17 @@ export namespace Telemetry { return } appInsights = cfg - identityAllowed = !configUnreadable // altimate_change start — identity for the funnel join: the backend user uuid and tenant from // the stored Altimate credential, cached per credential so this costs no network unless the // credential changed. Replaces the hashed account email. Not awaited: events flushed before it // resolves carry only the machine id, and telemetry never waits on the backend. - if (identityAllowed) void resolveIdentity() + void resolveIdentity() // altimate_change end // altimate_change — use shared getOrCreateMachineId() from util/machine-id.ts. // Returns "" on all error conditions (ENOENT: mints new UUID; EACCES/corrupt/oversized: // logs + returns ""). No try/catch needed — all paths are handled inside. machineId = getOrCreateMachineId() enabled = true - // altimate_change — telemetry is on: forget the opt-out marker, so turning it off again is - // recorded again. The bump first, so a send still in flight never recreates it. Not when the - // config could not be read: the user may have opted out there. - if (!configUnreadable) { - markerGeneration++ - OptOutMarker.clear() - } log.info("telemetry initialized", { mode: "appinsights" }) // altimate_change — clear any existing interval before installing a new one. doInit() can // run more than once per process (init/shutdown cycles per session in prompt.ts), and @@ -2643,7 +2741,9 @@ export namespace Telemetry { } catch { buffer = [] } finally { - initDone = true + // altimate_change start — unresolved: stay pre-init so events keep buffering and init() retries + initDone = !unresolved + // altimate_change end // altimate_change start — the stall watcher also writes each stall to opencode.log, which `debug bundle` // reads, so it runs whether or not telemetry is on; with telemetry off its events are dropped by `track`. startLoopMonitor() @@ -2701,7 +2801,11 @@ export namespace Telemetry { const generation = ++identityGeneration identityStamp = TelemetryIdentity.credentialStamp() identityRetryAt = 0 - return TelemetryIdentity.resolveWithOutcome(opts) + // altimate_change — no account lookup (a backend request) once this lifecycle has ended + return TelemetryIdentity.resolveWithOutcome({ + ...opts, + canSend: () => generation === identityGeneration && gate === "enabled" && !shutdownPromise, + }) .then(({ identity: resolved, outcome }) => { if (generation !== identityGeneration) return identity = resolved @@ -2718,7 +2822,7 @@ export namespace Telemetry { * Does nothing while telemetry is disabled. */ export async function refreshIdentity(): Promise { // Opted out (or not initialised): no lookup; an enabled init resolves from the stored credential. - if (!enabled || !identityAllowed) return + if (!enabled) return await flush() identity = undefined await resolveIdentity({ refresh: true }) @@ -2735,74 +2839,6 @@ export namespace Telemetry { } // altimate_change end - // altimate_change start — the final event of an opted-out machine. Sent once, only from a - // machine that has sent telemetry before (a machine id exists; none is minted here), under the - // same sink rules as doInit(). It carries the machine id and the opt-out source, never the account, - // project or launch. One send at a time across threads and processes (OptOutMarker.claim). The - // marker is written only once the ingest endpoint accepted the event, so an offline launch tries - // again next time; after the marker, nothing is sent until telemetry is re-enabled. Not awaited: - // an opted-out launch must not wait on the network. - const OPT_OUT_TIMEOUT_MS = 3_000 - // The launch does not wait for the send, but shutdown does (bounded), so a short command such as - // `tool run` records the marker instead of sending the event again on every later launch. - let optOutSend: Promise | undefined - let optOutUnderTestRunner = false - /** Test seam: let a suite that redirects the home directory exercise recordOptOut(). */ - export function allowOptOutUnderTestRunner(on: boolean) { - optOutUnderTestRunner = on - } - async function recordOptOut(source: "env" | "config"): Promise { - const lifecycle = lifecycleGeneration - const marker = markerGeneration - try { - if (underTestRunner() && !optOutUnderTestRunner) return - // The force flag is a developer knob for running telemetry under a test runner; the opt-out - // marker is about real users, and "disabled" must keep winning over "force" to the letter. - if (process.env.ALTIMATE_TELEMETRY_FORCE === "true") return - if (OptOutMarker.exists()) return - const id = OptOutMarker.readMachineId() - if (!id) return - const explicit = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING - const cfg = parseConnectionString(explicit ?? DEFAULT_CONNECTION_STRING) - if (!cfg) return - const envelopes = toAppInsightsEnvelopes( - [{ type: "telemetry_opted_out", timestamp: Date.now(), opt_out_source: source }], - cfg, - { machineId: id, identity: undefined }, - ) as Array<{ data: { baseData: { properties: Record } } }> - for (const envelope of envelopes) { - delete envelope.data.baseData.properties.project_id - delete envelope.data.baseData.properties.launch_id - } - // A shutdown since this task started means a later init may own the marker now: do not - // send, and let the next disabled init start over. - if (lifecycle !== lifecycleGeneration) return - const claim = OptOutMarker.claim() - if (!claim) return - const controller = new AbortController() - const timeout = setTimeout(() => controller.abort(), OPT_OUT_TIMEOUT_MS) - try { - // Another send may have finished between the marker check above and the claim. - if (OptOutMarker.exists()) return - const res = await fetch(cfg.endpoint, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(envelopes), - signal: controller.signal, - }) - // Accepted: write the marker unless telemetry was turned back on meanwhile, in this process - // (the generation) or another one (an enabled init removes the claim). - if (res.ok && marker === markerGeneration && OptOutMarker.holds(claim)) OptOutMarker.write() - } finally { - clearTimeout(timeout) - OptOutMarker.release(claim) - } - } catch { - // The opt-out path never surfaces: a missed marker only means one more attempt next launch. - } - } - // altimate_change end - /** Returns the turn generation for the session; pass it to `endSession` so only this turn ends it. */ export function setContext(opts: { sessionId: string; projectId: string }): number { sessionId = opts.sessionId @@ -2975,14 +3011,24 @@ export namespace Telemetry { export function track(event: Event) { // Before init completes: buffer (flushed once init enables, or cleared if disabled). // After init completed and disabled telemetry: drop silently. - if (initDone && !enabled) return + // altimate_change — opted out (env or config): drop. Unresolved config keeps initDone false, so it buffers. + if (gate === "disabled" || (initDone && !enabled)) return // altimate_change start — stamp the client trace of the turn the event's own session is // running, now: the event is serialised at flush time, when that turn may be over. Only an // explicit per-event `session_id` is used — the process-global session can be another one. // Becomes the envelope's `ai.operation.id`, joining it to the extension's and backend's records. const eventSession = (event as { session_id?: unknown }).session_id const operationId = typeof eventSession === "string" ? TraceContext.activeTraceId(eventSession) : undefined - if (operationId) (event as any)._operation_id = operationId + // An event held while the opt-out is unknown can outlive this session: it keeps the session and + // project it was tracked under instead of taking the next one's at flush. + const held = event as { session_id?: unknown; project_id?: unknown } + const stamp = { + ...(operationId ? { _operation_id: operationId } : {}), + ...(!initDone && held.session_id === undefined && sessionId ? { session_id: sessionId } : {}), + ...(!initDone && held.project_id === undefined && projectId ? { project_id: projectId } : {}), + } + // Stamped on a copy, so a caller's object is left as it passed it. + if (Object.keys(stamp).length > 0) event = { ...event, ...stamp } as Event // altimate_change end // altimate_change — no ambient workspace stamping: a row names its workspace itself (the prompt // loop's rows, tool rows and memory rows carry the turn's; workspace rows carry their own) or it @@ -3028,7 +3074,7 @@ export namespace Telemetry { // change on disk and re-resolve (cheap: two stats, and the cache answers unless the key changed). const who = identity const mid = machineId - if (enabled && identityAllowed) { + if (enabled) { const stamp = TelemetryIdentity.credentialStamp() const retryDue = identityRetryAt !== 0 && Date.now() >= identityRetryAt if (stamp !== identityStamp || retryDue) { @@ -3082,7 +3128,8 @@ export namespace Telemetry { // altimate_change — no write-back during shutdown. The buffer is cleared a few lines later // regardless, so re-inserting here does not save these events; it only leaves them to be // shipped by whatever lifecycle comes next, under a different launch id. - if (shuttingDown) return + // Nor after an opt-out: it aborted this batch, and nothing may be held for sending. + if (shuttingDown || gate === "disabled") return // Re-add events that haven't been retried yet to avoid data loss const retriable = events.filter((e) => !sent.has(e) && !(e as any)._retried) // altimate_change — a batch never attempted (an earlier one failed) keeps its retry. @@ -3143,6 +3190,7 @@ export namespace Telemetry { if (timer) clearTimeout(timer) return } + shutdownsStarted++ shutdownPromise = doShutdown(opts?.timeoutMs).finally(() => { shutdownPromise = undefined }) @@ -3161,15 +3209,6 @@ export namespace Telemetry { // init failed — nothing to flush } } - // altimate_change start — let an opted-out launch's final event finish before the process exits - if (optOutSend) { - const send = optOutSend - optOutSend = undefined - let timer: ReturnType | undefined - await Promise.race([send, new Promise((resolve) => (timer = setTimeout(resolve, Math.min(timeoutMs ?? OPT_OUT_TIMEOUT_MS, OPT_OUT_TIMEOUT_MS))))]) - if (timer) clearTimeout(timer) - } - // altimate_change end // altimate_change — first-run health: stop the stall monitor only once init has settled, so a // shutdown that overlaps init cannot be undone by doInit() starting the monitor afterwards. stopLoopMonitor() @@ -3216,8 +3255,15 @@ export namespace Telemetry { shuttingDown = false enabled = false appInsights = undefined - buffer = [] - droppedEvents = 0 + // altimate_change — while the opt-out is undecided nothing was flushed: keep the events for a + // later init in this process to send or drop. At process exit they go with the process. Not when a + // project's own read failed: once its session ends, another project's decision must not send its + // events. (Sessions that overlap share one lifecycle, as before: see the docs' multi-project note.) + if (gate !== "pending" || unresolvedInProject) { + unresolvedInProject = false + buffer = [] + droppedEvents = 0 + } sessionId = "" projectId = "" machineId = "" @@ -3225,7 +3271,6 @@ export namespace Telemetry { // previous account on events; doInit() resolves it again from whatever credential is on disk. // The generation bump makes any resolution still in flight a no-op when it settles. identityGeneration++ - lifecycleGeneration++ identity = undefined identityStamp = "" identityRetryAt = 0 diff --git a/packages/opencode/src/altimate/telemetry/opt-out.ts b/packages/opencode/src/altimate/telemetry/opt-out.ts deleted file mode 100644 index 92a86ffba3..0000000000 --- a/packages/opencode/src/altimate/telemetry/opt-out.ts +++ /dev/null @@ -1,111 +0,0 @@ -import fs from "fs" -import { randomUUID } from "crypto" -import path from "path" -import { Global } from "../../global" -import { readExistingMachineId } from "@/altimate/util/machine-id" - -/** - * The marker that says this machine already sent its final ``telemetry_opted_out`` event. - * - * A machine that turns telemetry off goes silent, which on the dashboards looks exactly like a - * client that stopped emitting. So the last event an opted-out machine sends is the opt-out - * itself, once; the marker is what makes it once. It lives next to the machine id because both - * describe the machine, not the user, and it is removed when telemetry is turned back on so a - * later opt-out is recorded again. See ``recordOptOut`` in index.ts. - * - * Paths go through ``Global.Path.home`` so a test that sets ``OPENCODE_TEST_HOME`` never reads - * or writes the real home directory; in a real session that is the same directory the machine - * id lives in. - */ -function dir(): string { - return path.join(Global.Path.home, ".altimate") -} - -export function markerPath(): string { - return path.join(dir(), "telemetry-opted-out") -} - -/** The machine id minted by an earlier telemetry-enabled run, if any. Never mints one: a machine - * that never sent telemetry has nothing to mark. Bounded and symlink-safe, like the minting read. */ -export function readMachineId(): string | undefined { - return readExistingMachineId(path.join(dir(), "machine-id")) || undefined -} - -export function exists(): boolean { - return fs.existsSync(markerPath()) -} - -export function write(): void { - fs.mkdirSync(dir(), { recursive: true, mode: 0o700 }) - // Exclusive create: never follow a planted symlink; an existing entry fails the write. - fs.writeFileSync(markerPath(), `${new Date().toISOString()}\n`, { mode: 0o600, flag: "wx" }) -} - -/** A send older than this is a process that died mid-send; its claim no longer holds. The send - * itself is capped at 3 s. */ -const CLAIM_STALE_MS = 30_000 - -function claimPath(): string { - return path.join(dir(), "telemetry-opted-out.sending") -} - -/** - * Claim the one opt-out send, across threads and processes: an exclusive create of an in-flight - * file next to the marker. The TUI's main thread and its worker both init telemetry, so without - * this the first opted-out launch sent the event twice. Returns this send's token, or undefined when - * another send holds a fresh claim (or the file cannot be created); a claim left by a process that - * died is taken over. - */ -export function claim(): string | undefined { - const token = `${process.pid}:${randomUUID()}` - const create = () => fs.writeFileSync(claimPath(), `${token}\n`, { mode: 0o600, flag: "wx" }) - try { - create() - return token - } catch (err) { - if ((err as NodeJS.ErrnoException)?.code !== "EEXIST") return undefined - } - try { - const age = Date.now() - fs.lstatSync(claimPath()).mtimeMs - if (age < CLAIM_STALE_MS) return undefined - fs.rmSync(claimPath(), { force: true }) - create() - return token - } catch { - return undefined - } -} - -/** Whether `token`'s claim still stands. An enabled init in any process removes the claim with the - * marker (see `clear`), so a send it outlived must not write the marker back. */ -export function holds(token: string): boolean { - try { - return fs.readFileSync(claimPath(), "utf-8").trim() === token - } catch { - return false - } -} - -/** Give the claim back, if it is still this send's: after the marker is written, or when the send - * failed so a later launch tries. */ -export function release(token: string): void { - try { - if (holds(token)) fs.rmSync(claimPath(), { force: true }) - } catch { - // a leftover claim goes stale and is taken over - } -} - -/** Telemetry is on again: remove the marker and any in-flight claim, so a send still running in - * another process does not write the marker back. Best effort: an unremovable marker (read-only - * directory) must never fail an init. */ -export function clear(): void { - try { - fs.rmSync(markerPath(), { force: true }) - fs.rmSync(claimPath(), { force: true }) - } catch { - // leave it; the next opt-out is then not recorded again, which is the lesser failure - } -} - -export * as OptOutMarker from "./opt-out" diff --git a/packages/opencode/src/altimate/util/machine-id.ts b/packages/opencode/src/altimate/util/machine-id.ts index d65a9b96f5..30956ceb03 100644 --- a/packages/opencode/src/altimate/util/machine-id.ts +++ b/packages/opencode/src/altimate/util/machine-id.ts @@ -37,24 +37,6 @@ function readCappedUtf8(idPath: string): string { } } -/** - * Read an existing machine id without minting one. Same regular-file, size-cap and UUID - * checks as {@link getOrCreateMachineId}; returns `""` when the file is absent, unreadable, - * not a regular file, oversized or not a UUID. For callers that must not create a machine - * identity as a side effect (the telemetry opt-out marker). - */ -export function readExistingMachineId(machineIdPath?: string): string { - const idPath = machineIdPath ?? path.join(os.homedir(), ".altimate", "machine-id") - try { - const stat = fs.lstatSync(idPath) - if (!stat.isFile() || stat.size > MAX_BYTES) return "" - const raw = readCappedUtf8(idPath) - return UUID_RE.test(raw) ? raw : "" - } catch { - return "" - } -} - /** * Read the machine-id from `~/.altimate/machine-id`, minting a new random UUID * with `flag: "wx"` (exclusive create) if the file is absent. diff --git a/packages/opencode/src/auth/index.ts b/packages/opencode/src/auth/index.ts index c06d17254d..a540581c8e 100644 --- a/packages/opencode/src/auth/index.ts +++ b/packages/opencode/src/auth/index.ts @@ -1,5 +1,6 @@ import { LayerNode } from "@opencode-ai/core/effect/layer-node" import path from "path" +import { statSync } from "fs" // altimate_change — tell a missing or empty auth store from an unreadable one import { Effect, Layer, Record, Result, Schema, Context } from "effect" import { NonNegativeInt } from "@opencode-ai/core/schema" import { Global } from "@opencode-ai/core/global" @@ -67,6 +68,24 @@ export interface Interface { export class Service extends Context.Service()("@opencode/Auth.cli") {} // altimate_change end +// altimate_change start — stores returned in place of an auth file that exists but could not be read: +// Config skips their well-known remote configs, so its telemetry opt-out is unknown. +const unreadStores = new WeakSet() +export function readFailed(store: object): boolean { + return unreadStores.has(store) +} +function storeFileHasContent(): boolean { + try { + // A stat, not a second read of the file the read just failed on. Only an empty regular file is + // "no store"; anything else that failed to read counts as unreadable. + const stat = statSync(file) + return !stat.isFile() || stat.size > 0 + } catch (error) { + return (error as NodeJS.ErrnoException)?.code !== "ENOENT" + } +} +// altimate_change end + export const layer = Layer.effect( Service, Effect.gen(function* () { @@ -80,8 +99,18 @@ export const layer = Layer.effect( } catch (err) {} } - const data = (yield* fsys.readJson(file).pipe(Effect.orElseSucceed(() => ({})))) as Record - return Record.filterMap(data, (value) => Result.fromOption(decode(value), () => undefined)) + // altimate_change start — record a failed read of an auth file that exists and is not empty + let failed = false + const data = (yield* fsys.readJson(file).pipe( + Effect.orElseSucceed(() => { + failed = storeFileHasContent() + return {} + }), + )) as Record + const store = Record.filterMap(data, (value) => Result.fromOption(decode(value), () => undefined)) + if (failed) unreadStores.add(store) + return store + // altimate_change end }) const get = Effect.fn("Auth.get")(function* (providerID: string) { diff --git a/packages/opencode/src/cli/bootstrap.ts b/packages/opencode/src/cli/bootstrap.ts index e955396528..d3bac89c30 100644 --- a/packages/opencode/src/cli/bootstrap.ts +++ b/packages/opencode/src/cli/bootstrap.ts @@ -9,6 +9,7 @@ import { InstanceRuntime } from "../project/instance-runtime" // not provided" — silently disabling session tracing and breaking `skill list`/`create`/`test`. import { Instance } from "../project/instance" // altimate_change end +import { Telemetry } from "../altimate/telemetry" // altimate_change — settle a pending opt-out in the project export async function bootstrap(directory: string, cb: () => Promise) { // altimate_change start — upstream_fix: load the instance, then restore the loaded ctx on the @@ -16,7 +17,10 @@ export async function bootstrap(directory: string, cb: () => Promise) { // ALS, which left every plain-async facade reader without an instance). See import note above. const ctx = await InstanceRuntime.load({ directory }) try { - return await Instance.restore(ctx, cb) + return await Instance.restore(ctx, async () => { + await Telemetry.settleInProject() + return cb() + }) } finally { await InstanceRuntime.disposeInstance(ctx) } diff --git a/packages/opencode/src/cli/cmd/tui.ts b/packages/opencode/src/cli/cmd/tui.ts index 05cdde12ce..f279007664 100644 --- a/packages/opencode/src/cli/cmd/tui.ts +++ b/packages/opencode/src/cli/cmd/tui.ts @@ -214,6 +214,16 @@ export const TuiThreadCommand = cmd({ } as WorkerOptions) // altimate_change end const client = Rpc.client(worker) + // altimate_change start — this thread loads no project, so where its own read of the opt-out + // stays unresolved (a remote or organization config), it takes the worker's answer + client.on<"enabled" | "disabled">("telemetry.optOut", (state) => { + Telemetry.adoptOptOut(state).catch(() => {}) + }) + client + .call("telemetryOptOut", undefined) + .then(async (state) => Telemetry.adoptOptOut(await state)) + .catch(() => {}) + // altimate_change end const reload = () => { client.call("reload", undefined).catch(() => {}) } diff --git a/packages/opencode/src/cli/effect-cmd.ts b/packages/opencode/src/cli/effect-cmd.ts index 226c1cf183..ebecf8c41b 100644 --- a/packages/opencode/src/cli/effect-cmd.ts +++ b/packages/opencode/src/cli/effect-cmd.ts @@ -91,7 +91,12 @@ export const effectCmd = (opts: EffectCmdOpts) => try { // altimate_change start — preserve legacy Instance ALS for Promise-based services // still reached from Effect-native CLI handlers (for example Provider.list -> Instance.state). - await Instance.restore(ctx, () => AppRuntime.runPromise(opts.handler(args).pipe(Effect.provideService(InstanceRef, ctx)))) + // Also settle a telemetry opt-out the CLI's startup read left open (see Telemetry.settleInProject). + const { Telemetry } = await import("@/altimate/telemetry") + await Instance.restore(ctx, async () => { + await Telemetry.settleInProject() + return AppRuntime.runPromise(opts.handler(args).pipe(Effect.provideService(InstanceRef, ctx))) + }) // altimate_change end } finally { await AppRuntime.runPromise(store.dispose(ctx)) diff --git a/packages/opencode/src/cli/tui/worker.ts b/packages/opencode/src/cli/tui/worker.ts index f0d8daa516..d1da2a7c3b 100644 --- a/packages/opencode/src/cli/tui/worker.ts +++ b/packages/opencode/src/cli/tui/worker.ts @@ -135,7 +135,17 @@ const altimateBaseRegistration = FreeTierConsent.createRegistrationGate({ const pendingRequests = new Map() // altimate_change end +// altimate_change — a decision made after the main thread first asked (a later init) is pushed to it +Telemetry.onOptOutDecided((state) => Rpc.emit("telemetry.optOut", state)) + export const rpc = { + // altimate_change start — the opt-out as resolved from the project's full config, for the main + // thread, which loads no project and so cannot read a remote or organization config itself. + async telemetryOptOut() { + await traceReady.catch(() => {}) + return Telemetry.settledOptOutState() + }, + // altimate_change end // altimate_change start — no consent token: an explicit picker selection always registers if needed async registerAltimateBase() { return altimateBaseRegistration.register() diff --git a/packages/opencode/src/cli/welcome.ts b/packages/opencode/src/cli/welcome.ts index f8ffd3f954..c9ab214fac 100644 --- a/packages/opencode/src/cli/welcome.ts +++ b/packages/opencode/src/cli/welcome.ts @@ -123,19 +123,6 @@ export function showWelcomeBannerIfNeeded(): void { // first-install from auto-update) — carrying that in the marker would be the fix if a // strict install count is ever required. Documented in docs/docs/reference/telemetry.md. // - // FIXME(telemetry-init-config-opt-out): doInit() may run before Instance.provide() has made - // Config.get() resolvable (see the try/catch around Config.get in telemetry/index.ts::doInit - // — the catch branch proceeds with telemetry enabled). A user who opted out via the - // `telemetry.disabled` config key — with no env var set — can therefore still have early - // events transmitted. The env-var opt-out (ALTIMATE_TELEMETRY_DISABLED / - // OPENCODE_DISABLE_TELEMETRY) is unaffected — that check needs no Instance context. - // Pre-existing and NOT specific to this event: every event emitted from CLI middleware - // shares the same gate. It is called out here because this event's volume grew ~30x when - // the shell and extension installers began writing the marker, so the exposure window is - // now routinely hit rather than theoretical. Fixing it belongs in telemetry init (make - // Config resolvable there, or adopt an explicit fail-closed policy module-wide) rather - // than in this function, which cannot resolve config without duplicating the merge and - // JSONC semantics of config/config.ts. const machineIdPath = path.join(os.homedir(), ".altimate", "machine-id") const isUpgrade = fs.existsSync(machineIdPath) // altimate_change end diff --git a/packages/opencode/src/config/config.ts b/packages/opencode/src/config/config.ts index 345a4c2e87..93960fd9fd 100644 --- a/packages/opencode/src/config/config.ts +++ b/packages/opencode/src/config/config.ts @@ -51,6 +51,16 @@ import { withTransientReadRetry } from "@/util/effect-http-client" import { makeRuntime } from "@/effect/run-service" // altimate_change end +// altimate_change start — loaded configs in which a source that may set `telemetry.disabled` (the +// organization remote config, a legacy global config that failed to import, the well-known remote +// configs behind an unreadable auth store) could not be read. +const configsWithUnresolvedTelemetry = new WeakSet() +const incompleteGlobals = new WeakSet() +export function telemetryConfigUnresolved(config: Info): boolean { + return configsWithUnresolvedTelemetry.has(config) +} +// altimate_change end + // altimate_change start — bootstrap can inspect config already loaded by service initialization // without introducing another asynchronous dependency for opted-out learning capture. const loadedConfig = new Map() @@ -445,7 +455,10 @@ export const layer = Layer.effect( // altimate_change end const legacy = path.join(Global.Path.config, "config") + // altimate_change — a legacy file that fails to load may hold a telemetry opt-out + let legacyUnread = false if (existsSync(legacy)) { + legacyUnread = true yield* Effect.promise(() => import(pathToFileURL(legacy).href, { with: { type: "toml" } }) .then(async (mod) => { @@ -455,6 +468,7 @@ export const layer = Layer.effect( result["$schema"] = "https://altimate.ai/config.json" // altimate_change end result = mergeConfig(result, rest) + legacyUnread = false // altimate_change — merged; a failed migration below is harmless await fsNode.writeFile(path.join(Global.Path.config, "config.json"), JSON.stringify(result, null, 2)) await fsNode.unlink(legacy) }) @@ -462,6 +476,7 @@ export const layer = Layer.effect( ) } + if (legacyUnread) incompleteGlobals.add(result) // altimate_change return result }) @@ -500,6 +515,7 @@ export const layer = Layer.effect( const loadInstanceState = Effect.fn("Config.loadInstanceState")( function* (ctx: InstanceContext) { const auth = yield* authSvc.all().pipe(Effect.orDie) + const authUnreadable = Auth.readFailed(auth) // altimate_change — an unreadable store skipped its remote configs let result: Info = {} const authEnv: Record = {} @@ -683,9 +699,17 @@ export const layer = Layer.effect( yield* Effect.logDebug("loaded custom config from OPENCODE_CONFIG_CONTENT") } + // altimate_change start — an organization config that could not be read may opt out of telemetry + let orgConfigUnresolved = false const activeAccount = Option.getOrUndefined( - yield* accountSvc.active().pipe(Effect.catch(() => Effect.succeed(Option.none()))), + yield* accountSvc.active().pipe( + Effect.catch(() => { + orgConfigUnresolved = true + return Effect.succeed(Option.none()) + }), + ), ) + // altimate_change end if (activeAccount?.active_org_id) { const accountID = activeAccount.id const orgID = activeAccount.active_org_id @@ -716,11 +740,14 @@ export const layer = Layer.effect( } }).pipe( Effect.withSpan("Config.loadActiveOrgConfig"), - Effect.catch((err) => - Effect.logDebug("failed to fetch remote account config", { + // altimate_change start — mark the config unresolved for the telemetry opt-out + Effect.catch((err) => { + orgConfigUnresolved = true + return Effect.logDebug("failed to fetch remote account config", { error: err instanceof Error ? err.message : String(err), - }), - ), + }) + }), + // altimate_change end ) } @@ -893,6 +920,9 @@ export const layer = Layer.effect( } // altimate_change end + // altimate_change — a source that could set `telemetry.disabled` was not read + if (orgConfigUnresolved || authUnreadable || incompleteGlobals.has(global)) configsWithUnresolvedTelemetry.add(result) + return { config: result, directories, diff --git a/packages/opencode/test/altimate/altimate-plugin.test.ts b/packages/opencode/test/altimate/altimate-plugin.test.ts index 3993fce4f2..fc97a248f5 100644 --- a/packages/opencode/test/altimate/altimate-plugin.test.ts +++ b/packages/opencode/test/altimate/altimate-plugin.test.ts @@ -6,8 +6,11 @@ import { buildAuthorizeUrl, buildCliContext } from "../../src/altimate/plugin/al import { getOrCreateMachineId } from "../../src/altimate/util/machine-id" import { Config } from "../../src/config/config" import { Telemetry } from "../../src/altimate/telemetry" +import { TelemetryConfig } from "../../src/altimate/telemetry/config-state" import os from "os" import { tmpdir } from "../fixture/fixture" +import { Instance } from "../../src/project/instance" +import { Database } from "@opencode-ai/core/database/database" const VALID_UUID = "550e8400-e29b-41d4-a716-446655440000" @@ -21,19 +24,22 @@ function stubConfig(impl: () => Promise) { describe("buildCliContext", () => { let cfg: ReturnType - let savedDisabled: string | undefined + const optOutFlags = ["ALTIMATE_TELEMETRY_DISABLED", "ALTIMATE_CLI_DISABLE_TELEMETRY", "OPENCODE_DISABLE_TELEMETRY"] + let savedFlags: Record = {} beforeEach(() => { // Clear any real opt-out from the dev's shell so these "telemetry enabled" - // tests are not silently broken by an exported ALTIMATE_TELEMETRY_DISABLED. - savedDisabled = process.env.ALTIMATE_TELEMETRY_DISABLED - delete process.env.ALTIMATE_TELEMETRY_DISABLED + // tests are not silently broken (or skipped) by an exported opt-out variable. + savedFlags = Object.fromEntries(optOutFlags.map((flag) => [flag, process.env[flag]])) + for (const flag of optOutFlags) delete process.env[flag] cfg = stubConfig(async () => ({})) }) afterEach(() => { cfg.mockRestore() - if (savedDisabled === undefined) delete process.env.ALTIMATE_TELEMETRY_DISABLED - else process.env.ALTIMATE_TELEMETRY_DISABLED = savedDisabled + for (const flag of optOutFlags) { + if (savedFlags[flag] === undefined) delete process.env[flag] + else process.env[flag] = savedFlags[flag] + } }) test("returns a valid base64url-encoded JSON blob with machine_id", async () => { @@ -101,6 +107,114 @@ describe("buildCliContext", () => { expect(ctx["v"]).toBe(1) }) + async function contextHasMachineId(directory?: string) { + await using dir = await tmpdir() + const idPath = path.join(dir.path, "machine-id") + fs.writeFileSync(idPath, VALID_UUID, "utf8") + const read = () => buildCliContext(idPath) + const encoded = directory ? await Instance.provide({ directory, fn: read }) : await read() + const ctx = JSON.parse(Buffer.from(encoded, "base64url").toString("utf8")) as Record + return Object.prototype.hasOwnProperty.call(ctx, "machine_id") + } + + test("omits machine_id once this process resolved an opt-out, even if this config is enabled", async () => { + delete process.env.ALTIMATE_TELEMETRY_DISABLED + await Telemetry.adoptOptOut("disabled") + expect(await contextHasMachineId()).toBe(false) + }) + + test("omits machine_id when this process adopts an opt-out while the config is read", async () => { + // Every env opt-out short-circuits before the config read this test holds open. + const saved = ["ALTIMATE_TELEMETRY_DISABLED", "ALTIMATE_CLI_DISABLE_TELEMETRY", "OPENCODE_DISABLE_TELEMETRY"].map( + (key) => [key, process.env[key]] as const, + ) + for (const [key] of saved) delete process.env[key] + try { + let release!: (value: unknown) => void + let started!: () => void + const reading = new Promise((resolve) => (started = resolve)) + cfg.mockImplementation((() => { + started() + return new Promise((resolve) => (release = resolve)) + }) as never) + await using dir = await tmpdir() + const idPath = path.join(dir.path, "machine-id") + fs.writeFileSync(idPath, VALID_UUID, "utf8") + const building = buildCliContext(idPath) + await reading + await Telemetry.adoptOptOut("disabled") + release({}) + const ctx = JSON.parse(Buffer.from(await building, "base64url").toString("utf8")) as Record + expect(Object.prototype.hasOwnProperty.call(ctx, "machine_id")).toBe(false) + } finally { + for (const [key, value] of saved) { + if (value === undefined) delete process.env[key] + else process.env[key] = value + } + } + }) + + test("inside a project, omits machine_id when a remote config source could not be read", async () => { + delete process.env.ALTIMATE_TELEMETRY_DISABLED + await using project = await tmpdir({ git: true }) + const unresolved = spyOn(Config, "telemetryConfigUnresolved").mockReturnValue(true) + try { + expect(await contextHasMachineId(project.path)).toBe(false) + } finally { + unresolved.mockRestore() + } + expect(await contextHasMachineId(project.path)).toBe(true) + }) + + test("inside a project, omits machine_id for another project whose own files opt out", async () => { + // An inherited env opt-out would short-circuit before this branch. + if (TelemetryConfig.disablingEnvironmentVariable()) return + await using current = await tmpdir({ git: true }) + await using other = await tmpdir({ git: true }) + fs.mkdirSync(path.join(other.path, ".altimate-code")) + fs.writeFileSync(path.join(other.path, ".altimate-code", "altimate-code.json"), '{ "telemetry": { "disabled": true } }') + await using dir = await tmpdir() + const idPath = path.join(dir.path, "machine-id") + fs.writeFileSync(idPath, VALID_UUID, "utf8") + // The Instance's own project, named through a symlink, keeps the id: the comparison is canonical, and + // the omission below comes from the other-project branch. + const link = path.join(dir.path, "current-link") + fs.symlinkSync(current.path, link) + // The files-only read is made inconclusive, so treating the alias as another project would omit the id. + const filesOnly = spyOn(TelemetryConfig, "disabledState").mockResolvedValue("unreadable") + const own = await Instance.provide({ directory: current.path, fn: () => buildCliContext(idPath, link) }).finally( + () => filesOnly.mockRestore(), + ) + expect(Object.prototype.hasOwnProperty.call(JSON.parse(Buffer.from(own, "base64url").toString("utf8")), "machine_id")).toBe(true) + const encoded = await Instance.provide({ directory: current.path, fn: () => buildCliContext(idPath, other.path) }) + const ctx = JSON.parse(Buffer.from(encoded, "base64url").toString("utf8")) as Record + expect(Object.prototype.hasOwnProperty.call(ctx, "machine_id")).toBe(false) + }) + + test("inside a project, keeps machine_id for another project without an opt-out, without consulting the account database", async () => { + if (TelemetryConfig.disablingEnvironmentVariable()) return + await using current = await tmpdir({ git: true }) + await using other = await tmpdir({ git: true }) + await using dir = await tmpdir() + const idPath = path.join(dir.path, "machine-id") + fs.writeFileSync(idPath, VALID_UUID, "utf8") + // An account database the startup probe cannot read: only the loaded config can rule on remote sources. + const database = spyOn(Database, "path").mockReturnValue(":memory:") + const encoded = await Instance.provide({ directory: current.path, fn: () => buildCliContext(idPath, other.path) }).finally( + () => database.mockRestore(), + ) + expect(JSON.parse(Buffer.from(encoded, "base64url").toString("utf8"))["machine_id"]).toBe(VALID_UUID) + }) + + test("inside a project, omits machine_id when the full config fails to load", async () => { + delete process.env.ALTIMATE_TELEMETRY_DISABLED + await using project = await tmpdir({ git: true }) + cfg.mockImplementation((async () => { + throw new Error("config failed to load") + }) as never) + expect(await contextHasMachineId(project.path)).toBe(false) + }) + test("omits machine_id when config.telemetry.disabled is set", async () => { delete process.env.ALTIMATE_TELEMETRY_DISABLED cfg.mockImplementation((async () => ({ telemetry: { disabled: true } })) as never) diff --git a/packages/opencode/test/altimate/telemetry/identity.test.ts b/packages/opencode/test/altimate/telemetry/identity.test.ts index df63ec5995..fd293b9e79 100644 --- a/packages/opencode/test/altimate/telemetry/identity.test.ts +++ b/packages/opencode/test/altimate/telemetry/identity.test.ts @@ -4,6 +4,7 @@ import os from "os" import path from "path" import { Telemetry } from "../../../src/altimate/telemetry" import { Auth } from "../../../src/auth" +import { Config } from "../../../src/config/config" import { TelemetryIdentity } from "../../../src/altimate/telemetry/identity" const UUID = "0f3c2a1e-6b7d-4c8e-9f10-1a2b3c4d5e6f" @@ -29,7 +30,7 @@ async function until(pred: () => boolean, ms = 2_000) { return pred() } -const DISABLE_FLAGS = ["ALTIMATE_TELEMETRY_DISABLED", "OPENCODE_DISABLE_TELEMETRY"] as const +const DISABLE_FLAGS = ["ALTIMATE_TELEMETRY_DISABLED", "ALTIMATE_CLI_DISABLE_TELEMETRY", "OPENCODE_DISABLE_TELEMETRY"] as const describe("telemetry identity", () => { const savedFlags: Record = {} @@ -357,6 +358,29 @@ describe("telemetry identity", () => { expect(Telemetry.identityForTest()).toBeUndefined() }) + test.each(["before", "after"] as const)("an identity refresh queued %s shutdown cannot reach an opted-out init", async (when) => { + process.env.APPLICATIONINSIGHTS_CONNECTION_STRING = + "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://ingest.example.invalid/" + const config = spyOn(Config, "get").mockResolvedValue({} as Awaited>) + try { + writeCreds() + await Telemetry.shutdown() + await Telemetry.init() + expect(await until(() => Telemetry.identityForTest()?.user_id === UUID)).toBe(true) + + const queued = when === "before" ? Telemetry.refreshIdentity() : undefined + const shutdown = Telemetry.shutdown() + process.env.ALTIMATE_TELEMETRY_DISABLED = "1" + const disabledInit = Telemetry.init() + await Promise.all([queued ?? Telemetry.refreshIdentity(), shutdown, disabledInit]) + + expect(Telemetry.isEnabled()).toBe(false) + expect(meCalls()).toHaveLength(1) + } finally { + config.mockRestore() + } + }) + test("a rejected key is not polled; only a new credential is looked up again", async () => { process.env.APPLICATIONINSIGHTS_CONNECTION_STRING = "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://ingest.example.invalid/" diff --git a/packages/opencode/test/altimate/telemetry/opt-out.test.ts b/packages/opencode/test/altimate/telemetry/opt-out.test.ts index 8fe4750bfd..9529373a3f 100644 --- a/packages/opencode/test/altimate/telemetry/opt-out.test.ts +++ b/packages/opencode/test/altimate/telemetry/opt-out.test.ts @@ -1,433 +1,975 @@ +import { Database as SQLite } from "bun:sqlite" import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test" -import { existsSync, mkdirSync, mkdtempSync, rmSync, statSync, utimesSync, writeFileSync } from "fs" +import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "fs" import os from "os" import path from "path" +import { Database } from "@opencode-ai/core/database/database" import { Auth } from "../../../src/auth" import { Config } from "../../../src/config/config" +import { ConfigManaged } from "../../../src/config/managed" import { Telemetry } from "../../../src/altimate/telemetry" -import { TelemetryIdentity } from "../../../src/altimate/telemetry/identity" -import { OptOutMarker } from "../../../src/altimate/telemetry/opt-out" +import { TelemetryConfig } from "../../../src/altimate/telemetry/config-state" +import { Instance } from "../../../src/project/instance" +import { bootstrap } from "../../../src/cli/bootstrap" +import { tmpdir } from "../../fixture/fixture" -const MACHINE = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d" -const UUID = "0f3c2a1e-6b7d-4c8e-9f10-1a2b3c4d5e6f" -const API = "https://api.example.invalid" const CS = "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://ingest.example.invalid/" - -type Call = { url: string; body: string } - -async function waitFor(pred: () => boolean, ms = 2_000) { - const until = Date.now() + ms - while (!pred() && Date.now() < until) await new Promise((r) => setTimeout(r, 20)) - return pred() -} - -describe("telemetry opt-out marker", () => { +const ENV = [ + "NODE_ENV", + "BUN_TEST", + "VITEST", + "JEST_WORKER_ID", + "OPENCODE_TEST_HOME", + "APPLICATIONINSIGHTS_CONNECTION_STRING", + "ALTIMATE_TELEMETRY_DISABLED", + "OPENCODE_DISABLE_TELEMETRY", + "ALTIMATE_CLI_DISABLE_TELEMETRY", + "ALTIMATE_TELEMETRY_FORCE", + "OPENCODE_CONFIG_CONTENT", + "ALTIMATE_CLI_CONFIG_CONTENT", + "OPENCODE_AUTH_CONTENT", +] as const + +describe("telemetry opt-out", () => { let home: string - let calls: Call[] - let trackStatus = 200 - /** When set, the next /v2/track POST waits on it before answering. */ - let holdTrack: Promise | undefined - let fetchSpy: ReturnType - let homedirSpy: ReturnType - let authSpy: ReturnType + let fetchSpy: ReturnType> + let configSpy: ReturnType> + let homedirSpy: ReturnType> + let authFileSpy: ReturnType> + let databaseSpy: ReturnType> + let authSpy: ReturnType> const saved: Record = {} - const ENV = [ - "OPENCODE_TEST_HOME", - "APPLICATIONINSIGHTS_CONNECTION_STRING", - "ALTIMATE_TELEMETRY_DISABLED", - "OPENCODE_DISABLE_TELEMETRY", - "ALTIMATE_TELEMETRY_FORCE", - "OPENCODE_CONFIG_CONTENT", - ] - - const optOuts = () => - calls - .filter((c) => c.url.includes("/v2/track")) - .flatMap((c) => JSON.parse(c.body) as any[]) - .filter((e) => e.data?.baseData?.name === "telemetry_opted_out") - const trackPosts = () => calls.filter((c) => c.url.includes("/v2/track")) - - function writeMachineId() { - mkdirSync(path.join(home, ".altimate"), { recursive: true }) - writeFileSync(path.join(home, ".altimate", "machine-id"), MACHINE) - } - function writeCreds() { - mkdirSync(path.join(home, ".altimate"), { recursive: true }) + + beforeEach(async () => { + await Telemetry.shutdown() + Telemetry.resetGateForTest() + home = mkdtempSync(path.join(os.tmpdir(), "telemetry-opt-out-")) + mkdirSync(path.join(home, ".altimate")) + // Simulate a previously signed-in installation with a machine ID. + writeFileSync(path.join(home, ".altimate", "machine-id"), "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d") writeFileSync( path.join(home, ".altimate", "altimate.json"), - JSON.stringify({ altimateUrl: API, altimateInstanceName: "acme", altimateApiKey: "key-1" }), + JSON.stringify({ + altimateUrl: "https://api.example.invalid", + altimateInstanceName: "workspace", + altimateApiKey: "test-key", + }), ) - } - - beforeEach(() => { - home = mkdtempSync(path.join(os.tmpdir(), "telemetry-opt-out-")) - for (const k of ENV) saved[k] = process.env[k] + for (const key of ENV) { + saved[key] = process.env[key] + delete process.env[key] + } + // Exercise the production opt-out behavior with both transports intercepted. + process.env.NODE_ENV = "production" process.env.OPENCODE_TEST_HOME = home process.env.APPLICATIONINSIGHTS_CONNECTION_STRING = CS - delete process.env.ALTIMATE_TELEMETRY_DISABLED - delete process.env.OPENCODE_DISABLE_TELEMETRY - delete process.env.ALTIMATE_TELEMETRY_FORCE - homedirSpy = spyOn(os, "homedir").mockImplementation(() => home) - // The auth store lives in the real data directory; never let a test read it. + Telemetry.setConfigDirectory(home) + homedirSpy = spyOn(os, "homedir").mockReturnValue(home) + authFileSpy = spyOn(Auth, "filepath").mockReturnValue(path.join(home, "auth.json")) + databaseSpy = spyOn(Database, "path").mockReturnValue(path.join(home, "missing.db")) authSpy = spyOn(Auth, "get").mockResolvedValue(undefined) - Telemetry.allowOptOutUnderTestRunner(true) - calls = [] - trackStatus = 200 - holdTrack = undefined - fetchSpy = spyOn(globalThis, "fetch").mockImplementation((async (input: any, init?: RequestInit) => { - const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url - calls.push({ url, body: String(init?.body ?? "") }) - if (url === `${API}/users/me`) { - return new Response(JSON.stringify({ id: 7, uuid: UUID }), { - status: 200, - headers: { "content-type": "application/json" }, - }) - } - if (url.includes("/v2/track")) { - if (holdTrack) await holdTrack - return new Response("", { status: trackStatus }) - } - return new Response("", { status: 200 }) - }) as any) + configSpy = spyOn(Config, "get").mockRejectedValue(new Error("InstanceRef not provided")) + fetchSpy = spyOn(globalThis, "fetch").mockRejectedValue(new Error("Telemetry must not invoke the transport")) }) afterEach(async () => { await Telemetry.shutdown() - Telemetry.allowOptOutUnderTestRunner(false) + Telemetry.resetGateForTest() + Telemetry.setConfigDirectory(undefined) fetchSpy.mockRestore() + configSpy.mockRestore() homedirSpy.mockRestore() + authFileSpy.mockRestore() authSpy.mockRestore() - for (const k of ENV) { - if (saved[k] === undefined) delete process.env[k] - else process.env[k] = saved[k] + databaseSpy.mockRestore() + for (const key of ENV) { + if (saved[key] === undefined) delete process.env[key] + else process.env[key] = saved[key] } rmSync(home, { recursive: true, force: true }) }) - test("an opted-out machine sends one final event with its machine id only, then nothing", async () => { - writeMachineId() - writeCreds() - expect(await TelemetryIdentity.resolve()).toEqual({ user_id: UUID, tenant: "acme" }) - const meCallsBefore = calls.filter((c) => c.url.endsWith("/users/me")).length + async function expectSilentLifecycle() { + const startupEvent = { + type: "startup_ready", + session_id: "", + command: "test", + surface: "test", + duration_ms: 1, + fresh_install: false, + timestamp: Date.now(), + } as const + Telemetry.track(startupEvent) + await Telemetry.init() + const startupCalls = fetchSpy.mock.calls.length + const enabled = Telemetry.isEnabled() + + Telemetry.setContext({ sessionId: "session", projectId: "project" }) + Telemetry.track({ + type: "session_start", + timestamp: Date.now(), + session_id: "session", + project_id: "project", + model_id: "model", + provider_id: "provider", + agent: "agent", + os: process.platform, + arch: process.arch, + node_version: process.version, + }) + await Telemetry.refreshIdentity() + await Telemetry.flush() + const sessionCalls = fetchSpy.mock.calls.length - startupCalls - process.env.ALTIMATE_TELEMETRY_DISABLED = "1" + // Leave a final event for shutdown to drain if tracking was accidentally enabled. + Telemetry.track({ + type: "error", + timestamp: Date.now(), + session_id: "session", + error_name: "TestError", + error_message: "test", + context: "test", + }) await Telemetry.shutdown() - await Telemetry.init() - expect(await waitFor(() => optOuts().length > 0)).toBe(true) - const [event] = optOuts() - expect(event.data.baseData.properties.machine_id).toBe(MACHINE) - expect(event.data.baseData.properties.opt_out_source).toBe("env") - // Never the account, the project or the launch, even with a cached identity on disk. - for (const key of ["user_id", "tenant", "project_id", "launch_id"]) - expect(event.data.baseData.properties[key]).toBeUndefined() - expect(typeof event.data.baseData.properties.cli_version).toBe("string") - expect(event.tags["ai.user.id"]).toBe(MACHINE) - // No request to resolve an identity while opted out. - expect(calls.filter((c) => c.url.endsWith("/users/me")).length).toBe(meCallsBefore) - expect(await waitFor(() => OptOutMarker.exists())).toBe(true) - - // Nothing after it: tracked events are dropped, and a new init does not send again. - const posts = trackPosts().length + const shutdownCalls = fetchSpy.mock.calls.length - startupCalls - sessionCalls + expect({ startupCalls, sessionCalls, shutdownCalls }).toEqual({ startupCalls: 0, sessionCalls: 0, shutdownCalls: 0 }) + expect(enabled).toBe(false) + } + + test.each(["ALTIMATE_TELEMETRY_DISABLED", "ALTIMATE_CLI_DISABLE_TELEMETRY", "OPENCODE_DISABLE_TELEMETRY"])("%s suppresses all requests", async (key) => { + process.env[key] = "1" + await expectSilentLifecycle() + // A later launch must also stay silent with the same existing machine ID. + await expectSilentLifecycle() + }) + + test("a false canonical alias cannot override the legacy opt-out", async () => { + process.env.ALTIMATE_CLI_DISABLE_TELEMETRY = "false" + process.env.OPENCODE_DISABLE_TELEMETRY = "1" + await expectSilentLifecycle() + }) + + test("opt-out takes precedence over the test force flag and explicit sink", async () => { + process.env.ALTIMATE_TELEMETRY_DISABLED = "TRUE" + process.env.NODE_ENV = "test" + process.env.ALTIMATE_TELEMETRY_FORCE = "true" + await expectSilentLifecycle() + }) + + test("loaded telemetry.disabled suppresses all requests", async () => { + configSpy.mockResolvedValue({ telemetry: { disabled: true } } as Awaited>) + await expectSilentLifecycle() + }) + + test("project telemetry.disabled suppresses all requests before instance startup", async () => { + mkdirSync(path.join(home, ".altimate-code")) + writeFileSync(path.join(home, ".altimate-code", "altimate-code.json"), '{ "telemetry": { "disabled": true } }') + await expectSilentLifecycle() + }) + + test("a configured well-known source suppresses telemetry until full config is available", async () => { + process.env.OPENCODE_AUTH_CONTENT = JSON.stringify({ + "https://config.example.invalid": { type: "wellknown", key: "TEST_CONFIG_TOKEN", token: "test-token" }, + }) + await expectSilentLifecycle() + }) + + test("an unreadable auth store does not rule out a well-known opt-out", async () => { + writeFileSync(Auth.filepath(), "{") + await expectSilentLifecycle() + }) + + test("an opt-out in the readable config stands even when an organization config load failed", async () => { + configSpy.mockResolvedValue({ telemetry: { disabled: true } } as Awaited>) + const unresolved = spyOn(Config, "telemetryConfigUnresolved").mockReturnValue(true) + try { + expect(await TelemetryConfig.loadedState()).toBe("disabled") + } finally { + unresolved.mockRestore() + } + }) + + test("a partial config cannot enable telemetry after an organization config load failed", async () => { + configSpy.mockResolvedValue({} as Awaited>) + const unresolved = spyOn(Config, "telemetryConfigUnresolved").mockReturnValue(true) + try { + await expectSilentLifecycle() + } finally { + unresolved.mockRestore() + } + }) + + test("a configured organization source suppresses telemetry until full config is available", async () => { + const dbPath = path.join(home, "account.db") + const db = new SQLite(dbPath) + db.exec(` + PRAGMA journal_mode = WAL; + CREATE TABLE account (id TEXT PRIMARY KEY); + CREATE TABLE account_state (id INTEGER PRIMARY KEY, active_account_id TEXT, active_org_id TEXT); + INSERT INTO account VALUES ('account'); + INSERT INTO account_state VALUES (1, 'account', 'org'); + `) + db.close() + databaseSpy.mockReturnValue(dbPath) + await expectSilentLifecycle() + }) + + test("a legacy account schema still defers to its selected organization's config", async () => { + const dbPath = path.join(home, "account.db") + const db = new SQLite(dbPath) + try { + db.exec(` + PRAGMA journal_mode = WAL; + CREATE TABLE account (id TEXT PRIMARY KEY, selected_org_id TEXT); + CREATE TABLE account_state (id INTEGER PRIMARY KEY, active_account_id TEXT); + INSERT INTO account VALUES ('account', NULL); + INSERT INTO account_state VALUES (1, 'account'); + `) + databaseSpy.mockReturnValue(dbPath) + expect(await TelemetryConfig.disabledState(home)).toBe("enabled") + db.exec("UPDATE account SET selected_org_id = 'org'") + expect(await TelemetryConfig.disabledState(home)).toBe("unreadable") + } finally { + db.close() + } + }) + + test("the startup probe can read while an account writer holds a WAL transaction", async () => { + const dbPath = path.join(home, "account.db") + const writer = new SQLite(dbPath) + try { + writer.exec(` + PRAGMA journal_mode = WAL; + CREATE TABLE account (id TEXT PRIMARY KEY); + CREATE TABLE account_state (id INTEGER PRIMARY KEY, active_account_id TEXT, active_org_id TEXT); + INSERT INTO account VALUES ('account'); + BEGIN IMMEDIATE; + INSERT INTO account_state VALUES (1, 'account', 'org'); + `) + databaseSpy.mockReturnValue(dbPath) + expect(await TelemetryConfig.disabledState(home)).toBe("enabled") + writer.exec("COMMIT") + expect(await TelemetryConfig.disabledState(home)).toBe("unreadable") + } finally { + writer.close() + } + }) + + function trackStartup() { Telemetry.track({ type: "startup_ready", + timestamp: Date.now(), session_id: "", command: "test", surface: "test", duration_ms: 1, fresh_install: false, - timestamp: Date.now(), }) + } + + function recordIngestion() { + const names: string[] = [] + fetchSpy.mockImplementation(Object.assign(async (url: Parameters[0], request?: Parameters[1]) => { + if (url !== "https://ingest.example.invalid/v2/track") throw new Error("Unexpected telemetry request") + for (const envelope of JSON.parse(String(request?.body))) { + names.push(envelope.data.baseData.properties?.error_name ?? envelope.data.baseData.name) + } + return new Response(null, { status: 204 }) + }, { preconnect: fetch.preconnect })) + return names + } + + /** Make the next config read wait for the caller; resolves once that read has started. */ + function holdConfigRead(capture: (resolve: (value: Awaited>) => void) => void) { + let started!: () => void + const reading = new Promise((resolve) => (started = resolve)) + configSpy.mockImplementation(() => { + started() + return new Promise(capture) + }) + return reading + } + + function errorEvent(name: string) { + return { type: "error", timestamp: Date.now(), session_id: "", error_name: name, error_message: "test", context: "test" } as const + } + + /** No signed-in account, so the only requests are ingestion. */ + function signOut() { + rmSync(path.join(home, ".altimate", "altimate.json")) + } + + test("an unreadable config holds startup events; a later enabled init sends them once", async () => { + const names = recordIngestion() + signOut() + process.env.OPENCODE_CONFIG_CONTENT = "{" + trackStartup() + await Telemetry.init() await Telemetry.flush() - await Telemetry.shutdown() + expect(Telemetry.isEnabled()).toBe(false) + expect(fetchSpy).not.toHaveBeenCalled() + + configSpy.mockResolvedValue({}) + // While unreadable, init() re-resolves the config at most every 30 s. + const reads = configSpy.mock.calls.length await Telemetry.init() - await new Promise((r) => setTimeout(r, 150)) - expect(trackPosts().length).toBe(posts) - expect(optOuts()).toHaveLength(1) - }) + expect(configSpy.mock.calls.length).toBe(reads) + expect(fetchSpy).not.toHaveBeenCalled() - test("a short opted-out run that shuts down at once still writes the marker, so the next launch sends nothing", async () => { - writeMachineId() - holdTrack = new Promise((r) => setTimeout(r, 100)) // the ingest endpoint answers after 100ms - process.env.ALTIMATE_TELEMETRY_DISABLED = "1" + Telemetry.retryConfigNowForTest() + await Telemetry.init() + expect(Telemetry.isEnabled()).toBe(true) + await Telemetry.flush() await Telemetry.shutdown() + expect(names).toEqual(["startup_ready"]) + }) + + test("a session shutdown while unresolved keeps held events for a later init", async () => { + const names = recordIngestion() + signOut() + process.env.OPENCODE_CONFIG_CONTENT = "{" + trackStartup() await Telemetry.init() - await Telemetry.shutdown() // the command finished before the send did - expect(OptOutMarker.exists()).toBe(true) + await Telemetry.shutdown() + expect(fetchSpy).not.toHaveBeenCalled() + + configSpy.mockResolvedValue({}) + Telemetry.retryConfigNowForTest() await Telemetry.init() - await new Promise((r) => setTimeout(r, 100)) - expect(optOuts()).toHaveLength(1) + await Telemetry.flush() + expect(names).toEqual(["startup_ready"]) }) - test("a machine that never sent telemetry sends nothing and gets no marker", async () => { - process.env.ALTIMATE_TELEMETRY_DISABLED = "1" + test("an unreadable config that resolves to disabled sends nothing, then stays off for the process", async () => { + const names = recordIngestion() + process.env.OPENCODE_CONFIG_CONTENT = "{" + trackStartup() + await Telemetry.init() + configSpy.mockResolvedValue({ telemetry: { disabled: true } }) + Telemetry.retryConfigNowForTest() + await Telemetry.init() + await Telemetry.flush() + await Telemetry.refreshIdentity() + expect(Telemetry.isEnabled()).toBe(false) + expect(fetchSpy).not.toHaveBeenCalled() + + // Another session of the same process whose config reads as enabled. await Telemetry.shutdown() + signOut() + configSpy.mockResolvedValue({}) + trackStartup() await Telemetry.init() - await new Promise((r) => setTimeout(r, 150)) - expect(trackPosts()).toHaveLength(0) - expect(OptOutMarker.exists()).toBe(false) - expect(existsSync(path.join(home, ".altimate", "machine-id"))).toBe(false) + await Telemetry.flush() + expect(names).toEqual([]) }) - test("a corrupt or oversized machine-id file sends nothing", async () => { - mkdirSync(path.join(home, ".altimate"), { recursive: true }) - writeFileSync(path.join(home, ".altimate", "machine-id"), "not-a-uuid\n" + "x".repeat(4096)) - process.env.ALTIMATE_TELEMETRY_DISABLED = "1" - await Telemetry.shutdown() + test("exit while unresolved sends nothing", async () => { + process.env.OPENCODE_CONFIG_CONTENT = "{" + trackStartup() await Telemetry.init() - await new Promise((r) => setTimeout(r, 150)) - expect(trackPosts()).toHaveLength(0) - expect(OptOutMarker.exists()).toBe(false) + trackStartup() + await Telemetry.shutdown({ timeoutMs: Telemetry.EXIT_FLUSH_BUDGET_MS }) + expect(fetchSpy).not.toHaveBeenCalled() }) - test("the marker is written only once the sink accepted the event", async () => { - writeMachineId() - process.env.OPENCODE_DISABLE_TELEMETRY = "true" - trackStatus = 500 - await Telemetry.shutdown() + function wellKnownAuth() { + process.env.OPENCODE_AUTH_CONTENT = JSON.stringify({ + "https://config.example.invalid": { type: "wellknown", key: "TEST_CONFIG_TOKEN", token: "test-token" }, + }) + } + + test("a thread that cannot read a remote config sends its held events once the worker resolves enabled", async () => { + const names = recordIngestion() + signOut() + wellKnownAuth() + trackStartup() await Telemetry.init() - expect(await waitFor(() => trackPosts().length === 1)).toBe(true) - await new Promise((r) => setTimeout(r, 50)) - expect(OptOutMarker.exists()).toBe(false) + await Telemetry.flush() + expect(Telemetry.optOutState()).toBe("pending") + expect(fetchSpy).not.toHaveBeenCalled() - trackStatus = 200 - await Telemetry.shutdown() + await Telemetry.adoptOptOut("pending") + expect(Telemetry.optOutState()).toBe("pending") + await Telemetry.adoptOptOut("enabled") + expect(Telemetry.isEnabled()).toBe(true) + await Telemetry.flush() + expect(names).toEqual(["startup_ready"]) + }) + + test("an opt-out resolved by the worker drops the held events and stays off", async () => { + const names = recordIngestion() + signOut() + wellKnownAuth() + trackStartup() await Telemetry.init() - expect(await waitFor(() => trackPosts().length === 2)).toBe(true) - expect(await waitFor(() => OptOutMarker.exists())).toBe(true) - expect(optOuts()).toHaveLength(2) - expect(optOuts()[1].data.baseData.properties.user_id).toBeUndefined() - expect(optOuts()[1].tags["ai.user.id"]).toBe(MACHINE) + await Telemetry.adoptOptOut("disabled") + await Telemetry.adoptOptOut("enabled") + trackStartup() + await Telemetry.init() + await Telemetry.flush() + await Telemetry.shutdown() + expect(Telemetry.optOutState()).toBe("disabled") + expect(names).toEqual([]) }) - test("a send still pending when telemetry is re-enabled never recreates the marker", async () => { - writeMachineId() - let release: () => void = () => {} - holdTrack = new Promise((r) => (release = r)) + test("an env opt-out wins over an enabled answer from the worker", async () => { + signOut() + wellKnownAuth() + trackStartup() + await Telemetry.init() process.env.ALTIMATE_TELEMETRY_DISABLED = "1" + await Telemetry.adoptOptOut("enabled") + await Telemetry.flush() await Telemetry.shutdown() + expect(Telemetry.optOutState()).toBe("disabled") + expect(fetchSpy).not.toHaveBeenCalled() + }) + + test("the state handed to another thread waits for this thread's running init", async () => { + signOut() + let release!: (value: Awaited>) => void + const reading = holdConfigRead((resolve) => (release = resolve)) + void Telemetry.init() + const settled = Telemetry.settledOptOutState() + await reading + expect(Telemetry.optOutState()).toBe("pending") + release({ telemetry: { disabled: true } }) + expect(await settled).toBe("disabled") + }) + + test("a cleanly closed account database without WAL side files reads like any other", async () => { + const dbPath = path.join(home, "account.db") + databaseSpy.mockReturnValue(dbPath) + const db = new SQLite(dbPath) + db.exec(` + PRAGMA journal_mode = WAL; + CREATE TABLE account (id TEXT PRIMARY KEY); + CREATE TABLE account_state (id INTEGER PRIMARY KEY, active_account_id TEXT, active_org_id TEXT); + INSERT INTO account VALUES ('account'); + INSERT INTO account_state VALUES (1, 'account', NULL); + `) + db.close() + // SQLite removes these when the last connection closes on Linux and Windows. + rmSync(`${dbPath}-wal`, { force: true }) + rmSync(`${dbPath}-shm`, { force: true }) + expect(await TelemetryConfig.disabledState(home)).toBe("enabled") + + const writer = new SQLite(dbPath) + writer.exec("UPDATE account_state SET active_org_id = 'org'") + writer.close() + rmSync(`${dbPath}-wal`, { force: true }) + rmSync(`${dbPath}-shm`, { force: true }) + expect(await TelemetryConfig.disabledState(home)).toBe("unreadable") + }) + + test("events held across a session keep the session and project they were tracked under", async () => { + const recorded: Array<{ session_id: string; project_id: string }> = [] + fetchSpy.mockImplementation(Object.assign(async (_url: Parameters[0], request?: Parameters[1]) => { + for (const envelope of JSON.parse(String(request?.body))) { + recorded.push({ session_id: envelope.tags["ai.session.id"], project_id: envelope.data.baseData.properties.project_id }) + } + return new Response(null, { status: 204 }) + }, { preconnect: fetch.preconnect })) + signOut() + process.env.OPENCODE_CONFIG_CONTENT = "{" + await Telemetry.init() + Telemetry.setContext({ sessionId: "session-a", projectId: "project-a" }) + // An event that names no session of its own (an MCP or stall event) takes the process's. + const { session_id: _omitted, ...sessionless } = errorEvent("Held") + Telemetry.track(sessionless as unknown as Telemetry.Event) + // Stamped on the held copy: the caller's object is left as it passed it. + expect("session_id" in sessionless).toBe(false) + await Telemetry.shutdown() + + Telemetry.setContext({ sessionId: "session-b", projectId: "project-b" }) + configSpy.mockResolvedValue({}) + Telemetry.retryConfigNowForTest() await Telemetry.init() - expect(await waitFor(() => trackPosts().length === 1)).toBe(true) // the POST is in flight + await Telemetry.flush() + expect(recorded.map((p) => [p.session_id, p.project_id])).toEqual([["session-a", "project-a"]]) + }) + + test("each opt-out decision is announced once, for another thread to adopt", async () => { + const decisions: string[] = [] + const stop = Telemetry.onOptOutDecided((state) => decisions.push(state)) + try { + signOut() + configSpy.mockResolvedValue({}) + await Telemetry.init() + await Telemetry.shutdown() + await Telemetry.init() + expect(decisions).toEqual(["enabled"]) + } finally { + stop() + } + }) + + test("a listener that throws does not stop this thread's telemetry", async () => { + const names = recordIngestion() + signOut() + let listenerCalled = false + const stop = Telemetry.onOptOutDecided(() => { + listenerCalled = true + throw new Error("port closed") + }) + try { + configSpy.mockResolvedValue({}) + await Telemetry.init() + trackStartup() + await Telemetry.flush() + expect(listenerCalled).toBe(true) + expect(names).toEqual(["startup_ready"]) + } finally { + stop() + } + }) - delete process.env.ALTIMATE_TELEMETRY_DISABLED + test("an enabled decision from the full config stands for later inits outside a project", async () => { + const names = recordIngestion() + signOut() + wellKnownAuth() // the files alone cannot settle this user's opt-out + await using dir = await tmpdir({ git: true }) + configSpy.mockResolvedValue({}) + await Instance.provide({ directory: dir.path, fn: () => Telemetry.init() }) + expect(Telemetry.optOutState()).toBe("enabled") await Telemetry.shutdown() - await Telemetry.init() // telemetry on again; this lifecycle owns the marker now + + // Between sessions, outside any project: the full config cannot load here, and the files alone + // read as unreadable (a well-known login), so only the earlier enabled decision keeps it on. + configSpy.mockRejectedValue(new Error("InstanceRef not provided")) + expect(await TelemetryConfig.resolve(home)).toBe("unreadable") + trackStartup() + await Telemetry.init() expect(Telemetry.isEnabled()).toBe(true) - release() - await new Promise((r) => setTimeout(r, 100)) - expect(OptOutMarker.exists()).toBe(false) + await Telemetry.flush() + expect(names).toEqual(["startup_ready"]) + }) - // Opting out again is recorded afresh. - holdTrack = undefined - process.env.ALTIMATE_TELEMETRY_DISABLED = "1" + test("a file opt-out added mid-process wins over an earlier enabled decision, even with an organization selected", async () => { + const names = recordIngestion() + signOut() + const dbPath = path.join(home, "account.db") + const db = new SQLite(dbPath) + db.exec(` + PRAGMA journal_mode = WAL; + CREATE TABLE account (id TEXT PRIMARY KEY); + CREATE TABLE account_state (id INTEGER PRIMARY KEY, active_account_id TEXT, active_org_id TEXT); + INSERT INTO account VALUES ('account'); + INSERT INTO account_state VALUES (1, 'account', 'org'); + `) + db.close() + databaseSpy.mockReturnValue(dbPath) + await using dir = await tmpdir({ git: true }) + configSpy.mockResolvedValue({}) + await Instance.provide({ directory: dir.path, fn: () => Telemetry.init() }) + expect(Telemetry.optOutState()).toBe("enabled") await Telemetry.shutdown() + + configSpy.mockRejectedValue(new Error("InstanceRef not provided")) + // The selected organization makes the files alone inconclusive... + expect(await TelemetryConfig.disabledState(home)).toBe("unreadable") + // ...until the files themselves opt out. + process.env.OPENCODE_CONFIG_CONTENT = JSON.stringify({ telemetry: { disabled: true } }) + expect(await TelemetryConfig.disabledState(home)).toBe("disabled") + trackStartup() await Telemetry.init() - expect(await waitFor(() => optOuts().length === 2)).toBe(true) - expect(await waitFor(() => OptOutMarker.exists())).toBe(true) + await Telemetry.flush() + expect(Telemetry.optOutState()).toBe("disabled") + expect(names).toEqual([]) }) - test("a send accepted after a shutdown still writes the marker, so the next opted-out init sends nothing", async () => { - writeMachineId() - let release: () => void = () => {} - holdTrack = new Promise((r) => (release = r)) - process.env.ALTIMATE_TELEMETRY_DISABLED = "1" - await Telemetry.shutdown() + test("an opt-out adopted while this thread's own read runs still wins", async () => { + const names = recordIngestion() + signOut() + let release!: (value: Awaited>) => void + const reading = holdConfigRead((resolve) => (release = resolve)) + trackStartup() + const init = Telemetry.init() + await reading + await Telemetry.adoptOptOut("disabled") + release({}) + await init + await Telemetry.flush() + expect(Telemetry.optOutState()).toBe("disabled") + expect(names).toEqual([]) + }) + + test("a later opt-out from the worker stops telemetry this thread already enabled", async () => { + const names = recordIngestion() + signOut() + configSpy.mockResolvedValue({}) await Telemetry.init() - expect(await waitFor(() => trackPosts().length === 1)).toBe(true) // the POST is in flight + expect(Telemetry.isEnabled()).toBe(true) + await Telemetry.adoptOptOut("disabled") + trackStartup() + await Telemetry.flush() + await Telemetry.shutdown() + expect(Telemetry.isEnabled()).toBe(false) + expect(names).toEqual([]) + }) - await Telemetry.shutdown() // still opted out: nothing has cleared the marker - release() - expect(await waitFor(() => OptOutMarker.exists())).toBe(true) + test("the first init inside a project is not held back by an earlier unreadable read outside one", async () => { + const names = recordIngestion() + signOut() + process.env.OPENCODE_CONFIG_CONTENT = "{" + trackStartup() + await Telemetry.init() + expect(Telemetry.optOutState()).toBe("pending") - holdTrack = undefined + // Within the 30 s backoff: a read outside a project is throttled, one inside a project is not. + configSpy.mockResolvedValue({}) await Telemetry.init() - await new Promise((r) => setTimeout(r, 100)) - expect(optOuts()).toHaveLength(1) + expect(Telemetry.optOutState()).toBe("pending") + await using dir = await tmpdir({ git: true }) + await Instance.provide({ directory: dir.path, fn: () => Telemetry.init() }) + expect(Telemetry.optOutState()).toBe("enabled") + await Telemetry.flush() + expect(names).toEqual(["startup_ready"]) }) - test("an init that cannot read the config keeps the marker and resolves no account", async () => { - writeMachineId() - writeCreds() - OptOutMarker.write() - const configSpy = spyOn(Config, "get") + test("an unresolved init settling during a shutdown leaves the re-init published", async () => { + signOut() + let release!: (value: Awaited>) => void + const reading = holdConfigRead((resolve) => (release = resolve)) + void Telemetry.init() + await reading + const shutdown = Telemetry.shutdown() + const reinit = Telemetry.init() + // The first read now comes back unreadable while the shutdown waits for it. + const unresolved = spyOn(Config, "telemetryConfigUnresolved").mockReturnValue(true) + configSpy.mockResolvedValue({}) + release({}) + await shutdown + unresolved.mockRestore() + // A caller arriving now joins the re-init instead of starting a second one. + expect(Telemetry.init()).toBe(reinit) + await reinit + // The re-init is throttled like any instance-less retry, so the opt-out stays undecided. + expect(Telemetry.optOutState()).toBe("pending") + }) + + test("one project's unreadable config never holds up an opted-in project's next session", async () => { + const names = recordIngestion() + signOut() + await using a = await tmpdir({ git: true }) + await using b = await tmpdir({ git: true }) + const unresolvedConfig = {} as Awaited> + configSpy.mockImplementation(async () => (TelemetryConfig.instanceDirectory() === b.path ? unresolvedConfig : {})) + const unresolved = spyOn(Config, "telemetryConfigUnresolved").mockImplementation((config) => config === unresolvedConfig) try { - // The main thread's init: no Instance, so Config.get() throws, and the files cannot be read. - configSpy.mockRejectedValue(new Error("InstanceRef not provided")) - process.env.OPENCODE_CONFIG_CONTENT = "{ not json" + await Instance.provide({ directory: a.path, fn: () => Telemetry.init() }) await Telemetry.shutdown() - await Telemetry.init() - expect(Telemetry.isEnabled()).toBe(true) // ordinary rows keep flowing, as before - await Telemetry.flush() - await Telemetry.refreshIdentity() - await new Promise((r) => setTimeout(r, 50)) - expect(OptOutMarker.exists()).toBe(true) - expect(calls.filter((c) => c.url.endsWith("/users/me"))).toHaveLength(0) - expect(Telemetry.identityForTest()).toBeUndefined() - expect(optOuts()).toHaveLength(0) - - // The worker's init, inside an Instance: the config says disabled, and the marker held. - delete process.env.OPENCODE_CONFIG_CONTENT - configSpy.mockResolvedValue({ telemetry: { disabled: true } } as never) + await Instance.provide({ directory: b.path, fn: () => Telemetry.init() }) await Telemetry.shutdown() - await Telemetry.init() - await new Promise((r) => setTimeout(r, 50)) - expect(Telemetry.isEnabled()).toBe(false) - expect(optOuts()).toHaveLength(0) - expect(OptOutMarker.exists()).toBe(true) + // Within 30 s of B's failed read, A's next session still starts telemetry and sends its events. + await Instance.provide({ + directory: a.path, + fn: async () => { + await Telemetry.init() + expect(Telemetry.isEnabled()).toBe(true) + Telemetry.track(errorEvent("A")) + await Telemetry.flush() + }, + }) + expect(names).toEqual(["A"]) } finally { - configSpy.mockRestore() + unresolved.mockRestore() } }) - test("an init without an Instance honours a config opt-out read from the files", async () => { - writeMachineId() - const configSpy = spyOn(Config, "get").mockRejectedValue(new Error("InstanceRef not provided")) + test("a project whose opt-out never resolved does not have its events sent under another project", async () => { + const names = recordIngestion() + signOut() + await using a = await tmpdir({ git: true }) + await using b = await tmpdir({ git: true }) + const unresolvedConfig = {} as Awaited> + configSpy.mockImplementation(async () => (TelemetryConfig.instanceDirectory() === b.path ? unresolvedConfig : {})) + const unresolved = spyOn(Config, "telemetryConfigUnresolved").mockImplementation((config) => config === unresolvedConfig) try { - process.env.OPENCODE_CONFIG_CONTENT = JSON.stringify({ telemetry: { disabled: true } }) + await Instance.provide({ directory: a.path, fn: () => Telemetry.init() }) await Telemetry.shutdown() - await Telemetry.init() - expect(Telemetry.isEnabled()).toBe(false) - expect(await waitFor(() => optOuts().length === 1)).toBe(true) - expect(optOuts()[0].data.baseData.properties.opt_out_source).toBe("config") - expect(await waitFor(() => OptOutMarker.exists())).toBe(true) + await Instance.provide({ + directory: b.path, + fn: async () => { + await Telemetry.init() + Telemetry.track(errorEvent("B")) + }, + }) + await Telemetry.shutdown() + await Instance.provide({ directory: a.path, fn: () => Telemetry.init() }) + await Telemetry.flush() + expect(names).toEqual([]) } finally { - configSpy.mockRestore() + unresolved.mockRestore() } }) - test("OPENCODE_DISABLE_TELEMETRY records the opt-out with source env", async () => { - writeMachineId() - process.env.OPENCODE_DISABLE_TELEMETRY = "true" - await Telemetry.shutdown() + test("a managed telemetry setting settles the startup read even with an organization selected", async () => { + const dbPath = path.join(home, "account.db") + const db = new SQLite(dbPath) + db.exec(` + PRAGMA journal_mode = WAL; + CREATE TABLE account (id TEXT PRIMARY KEY); + CREATE TABLE account_state (id INTEGER PRIMARY KEY, active_account_id TEXT, active_org_id TEXT); + INSERT INTO account VALUES ('account'); + INSERT INTO account_state VALUES (1, 'account', 'org'); + `) + db.close() + databaseSpy.mockReturnValue(dbPath) + expect(await TelemetryConfig.disabledState(home)).toBe("unreadable") + + const managed = path.join(home, "managed") + mkdirSync(managed) + const managedDir = spyOn(ConfigManaged, "managedConfigDir").mockReturnValue(managed) + try { + writeFileSync(path.join(managed, "altimate-code.json"), '{ "telemetry": { "disabled": false } }') + expect(await TelemetryConfig.disabledState(home)).toBe("enabled") + writeFileSync(path.join(managed, "altimate-code.json"), '{ "telemetry": { "disabled": true } }') + expect(await TelemetryConfig.disabledState(home)).toBe("disabled") + } finally { + managedDir.mockRestore() + } + }) + + test("an opt-out adopted mid-flush aborts the batch already in flight", async () => { + signOut() + let aborted = false + let started!: () => void + const inFlight = new Promise((resolve) => (started = resolve)) + fetchSpy.mockImplementation(Object.assign( + (_url: Parameters[0], request?: Parameters[1]) => { + started() + return new Promise((_, reject) => { + request?.signal?.addEventListener("abort", () => { + aborted = true + reject(new Error("aborted")) + }, { once: true }) + }) + }, + { preconnect: fetch.preconnect }, + )) + configSpy.mockResolvedValue({}) await Telemetry.init() - expect(await waitFor(() => optOuts().length === 1)).toBe(true) - expect(optOuts()[0].data.baseData.properties.opt_out_source).toBe("env") - expect(await waitFor(() => OptOutMarker.exists())).toBe(true) + trackStartup() + // A long request budget, so only the opt-out can abort the batch (not the flush's own timeout). + const flushing = Telemetry.flush(600_000) + await inFlight + await Telemetry.adoptOptOut("disabled") + // Aborted by adoptOptOut itself, before it returned. + expect(aborted).toBe(true) + await flushing + // The aborted batch is not put back: nothing stays held after the opt-out. + expect(Telemetry.bufferedCountForTest()).toBe(0) }) - test("the config opt-out records the event with source config", async () => { - writeMachineId() - const configSpy = spyOn(Config, "get").mockResolvedValue({ telemetry: { disabled: true } } as never) + test("an enabled answer from the worker starts nothing once a shutdown began while this thread's init ran", async () => { + signOut() + wellKnownAuth() + let fail!: (error: Error) => void + let started!: () => void + const reading = new Promise((resolve) => (started = resolve)) + configSpy.mockImplementation(() => { + started() + return new Promise((_, reject) => (fail = reject)) + }) + trackStartup() + const initializing = Telemetry.init() + await reading + const adopting = Telemetry.adoptOptOut("enabled") + const exiting = Telemetry.shutdown() + // The startup read cannot see the remote config, so this thread's own init leaves it pending. + fail(new Error("InstanceRef not provided")) + await initializing + await exiting + await adopting + await Telemetry.flush() + expect(Telemetry.isEnabled()).toBe(false) + expect(fetchSpy).not.toHaveBeenCalled() + }) + + test("a command that loads a project settles an opt-out the startup read left open", async () => { + const names = recordIngestion() + signOut() + process.env.OPENCODE_CONFIG_CONTENT = "{" + trackStartup() + await Telemetry.init() + expect(Telemetry.awaitingConfig()).toBe(true) + + // The project's own load reads the real config; give it a readable one. + delete process.env.OPENCODE_CONFIG_CONTENT + configSpy.mockResolvedValue({}) + await using dir = await tmpdir({ git: true }) + await bootstrap(dir.path, async () => { + expect(Telemetry.optOutState()).toBe("enabled") + }) + await Telemetry.flush() + expect(names).toEqual(["startup_ready"]) + }) + + test("a project left unresolved drops its held events before another project decides", async () => { + const names = recordIngestion() + signOut() + await using a = await tmpdir({ git: true }) + await using b = await tmpdir({ git: true }) + const unresolvedConfig = {} as Awaited> + configSpy.mockImplementation(async () => (TelemetryConfig.instanceDirectory() === b.path ? unresolvedConfig : {})) + const unresolved = spyOn(Config, "telemetryConfigUnresolved").mockImplementation((config) => config === unresolvedConfig) try { + await Instance.provide({ + directory: b.path, + fn: async () => { + await Telemetry.init() + Telemetry.track(errorEvent("B")) + }, + }) await Telemetry.shutdown() - await Telemetry.init() - expect(Telemetry.isEnabled()).toBe(false) - expect(await waitFor(() => optOuts().length === 1)).toBe(true) - expect(optOuts()[0].data.baseData.properties.opt_out_source).toBe("config") + expect(Telemetry.optOutState()).toBe("pending") + await Instance.provide({ directory: a.path, fn: () => Telemetry.init() }) + await Telemetry.flush() + expect(names).toEqual([]) } finally { - configSpy.mockRestore() + unresolved.mockRestore() } }) - test("ALTIMATE_TELEMETRY_FORCE never sends the opt-out event", async () => { - writeMachineId() - process.env.ALTIMATE_TELEMETRY_DISABLED = "1" - process.env.ALTIMATE_TELEMETRY_FORCE = "true" - await Telemetry.shutdown() - await Telemetry.init() - await new Promise((r) => setTimeout(r, 100)) // nothing to wait for: the force check returns first - expect(trackPosts()).toHaveLength(0) - expect(OptOutMarker.exists()).toBe(false) - expect(existsSync(path.join(home, ".altimate", "telemetry-opted-out.sending"))).toBe(false) + test("a raw well-known entry in OPENCODE_AUTH_CONTENT holds telemetry, as Config can still fetch for it", async () => { + process.env.OPENCODE_AUTH_CONTENT = JSON.stringify({ "https://config.example.invalid": { type: "wellknown" } }) + expect(await TelemetryConfig.disabledState(home)).toBe("unreadable") }) - test("two disabled inits while the first send is in flight send the event once", async () => { - writeMachineId() - let release: () => void = () => {} - holdTrack = new Promise((r) => (release = r)) - process.env.ALTIMATE_TELEMETRY_DISABLED = "1" - await Telemetry.shutdown() - await Telemetry.init() - expect(await waitFor(() => trackPosts().length === 1)).toBe(true) // the first send holds the claim + test("an in-memory account database is an unknown organization source", async () => { + databaseSpy.mockReturnValue(":memory:") + expect(await TelemetryConfig.disabledState(home)).toBe("unreadable") + }) - await Telemetry.shutdown() - await Telemetry.init() // a second init, as the TUI's worker beside its main thread - await new Promise((r) => setTimeout(r, 100)) - expect(trackPosts()).toHaveLength(1) + test("an account database not in WAL mode is not read, so its organization is unknown", async () => { + const dbPath = path.join(home, "account.db") + const db = new SQLite(dbPath) + db.exec(` + PRAGMA journal_mode = DELETE; + CREATE TABLE account (id TEXT PRIMARY KEY); + CREATE TABLE account_state (id INTEGER PRIMARY KEY, active_account_id TEXT, active_org_id TEXT); + INSERT INTO account VALUES ('account'); + INSERT INTO account_state VALUES (1, 'account', NULL); + `) + db.close() + databaseSpy.mockReturnValue(dbPath) + expect(await TelemetryConfig.disabledState(home)).toBe("unreadable") + }) - release() - expect(await waitFor(() => OptOutMarker.exists())).toBe(true) - expect(existsSync(path.join(home, ".altimate", "telemetry-opted-out.sending"))).toBe(false) - expect(optOuts()).toHaveLength(1) + test("an empty auth file holds no well-known entry, but a whitespace-only one is unreadable, as Auth reads them", async () => { + writeFileSync(Auth.filepath(), "") + expect(await TelemetryConfig.disabledState(home)).toBe("enabled") + writeFileSync(Auth.filepath(), " \n") + expect(await TelemetryConfig.disabledState(home)).toBe("unreadable") }) - test("another process's fresh claim holds the send; a stale one is taken over", async () => { - writeMachineId() - const claimFile = path.join(home, ".altimate", "telemetry-opted-out.sending") - writeFileSync(claimFile, "123\n") - process.env.ALTIMATE_TELEMETRY_DISABLED = "1" - await Telemetry.shutdown() - await Telemetry.init() - await new Promise((r) => setTimeout(r, 100)) - expect(trackPosts()).toHaveLength(0) + test("auth content or an auth file that parses to anything but a plain object is unreadable (fail closed)", async () => { + for (const content of ["[]", '"x"', "1", "null"]) { + process.env.OPENCODE_AUTH_CONTENT = content + expect(await TelemetryConfig.disabledState(home)).toBe("unreadable") + } + delete process.env.OPENCODE_AUTH_CONTENT + for (const content of ["[]", '"x"', "5", "true", "null"]) { + writeFileSync(Auth.filepath(), content) + expect(await TelemetryConfig.disabledState(home)).toBe("unreadable") + } + }) - const old = (Date.now() - 60_000) / 1000 - utimesSync(claimFile, old, old) // the process that held it died mid-send - await Telemetry.shutdown() - await Telemetry.init() - expect(await waitFor(() => optOuts().length === 1)).toBe(true) - expect(await waitFor(() => OptOutMarker.exists())).toBe(true) + test("an explicit local telemetry setting outranks a well-known remote config, which Config merges first", async () => { + process.env.OPENCODE_AUTH_CONTENT = JSON.stringify({ "https://config.example.invalid": { type: "wellknown" } }) + expect(await TelemetryConfig.disabledState(home)).toBe("unreadable") + process.env.OPENCODE_CONFIG_CONTENT = JSON.stringify({ telemetry: { disabled: false } }) + expect(await TelemetryConfig.disabledState(home)).toBe("enabled") }) - test("a failed send gives the claim back, so the next launch tries again", async () => { - writeMachineId() - trackStatus = 500 - process.env.ALTIMATE_TELEMETRY_DISABLED = "1" + test("auth content that does not parse falls back to the auth file, as Auth does", async () => { + process.env.OPENCODE_AUTH_CONTENT = "{" + expect(await TelemetryConfig.disabledState(home)).toBe("enabled") + writeFileSync(Auth.filepath(), JSON.stringify({ + "https://config.example.invalid": { type: "wellknown", key: "TEST_CONFIG_TOKEN", token: "test-token" }, + })) + expect(await TelemetryConfig.disabledState(home)).toBe("unreadable") + }) + + test("inside a project the load decides: a later unreadable auth read does not hold a config that loaded fine", async () => { + configSpy.mockResolvedValue({}) + writeFileSync(Auth.filepath(), "{") + expect(await TelemetryConfig.loadedState()).toBe("enabled") + }) + + test("after a session enabled telemetry, an unreadable read outside a project keeps that decision", async () => { + recordIngestion() + signOut() + configSpy.mockResolvedValue({}) + await Telemetry.init() await Telemetry.shutdown() + + configSpy.mockRejectedValue(new Error("InstanceRef not provided")) + process.env.OPENCODE_CONFIG_CONTENT = "{" + const reads = configSpy.mock.calls.length await Telemetry.init() - expect(await waitFor(() => trackPosts().length === 1)).toBe(true) - const claimFile = path.join(home, ".altimate", "telemetry-opted-out.sending") - expect(await waitFor(() => !existsSync(claimFile))).toBe(true) - expect(OptOutMarker.exists()).toBe(false) + expect(Telemetry.isEnabled()).toBe(true) + expect(configSpy.mock.calls.length - reads).toBe(1) }) - test("telemetry turned back on in another process stops an in-flight send from writing the marker", async () => { - writeMachineId() - let release: () => void = () => {} - holdTrack = new Promise((r) => (release = r)) - process.env.ALTIMATE_TELEMETRY_DISABLED = "1" - await Telemetry.shutdown() + test("after an opt-out, events tracked between sessions are not even buffered", async () => { + configSpy.mockResolvedValue({ telemetry: { disabled: true } }) await Telemetry.init() - expect(await waitFor(() => trackPosts().length === 1)).toBe(true) - OptOutMarker.clear() // what an enabled init in another process does - release() - await new Promise((r) => setTimeout(r, 100)) - expect(OptOutMarker.exists()).toBe(false) - }) - - test("an init without an Instance reads the opt-out from the command's project directory", async () => { - writeMachineId() - const project = mkdtempSync(path.join(os.tmpdir(), "telemetry-opt-out-project-")) - mkdirSync(path.join(project, ".altimate-code")) - writeFileSync(path.join(project, ".altimate-code", "altimate-code.json"), '{ "telemetry": { "disabled": true } }') - const configSpy = spyOn(Config, "get").mockRejectedValue(new Error("InstanceRef not provided")) + await Telemetry.shutdown() + for (let i = 0; i < 5; i++) trackStartup() + expect(Telemetry.bufferedCountForTest()).toBe(0) + }) + + test("two opted-in projects in one process share one lifecycle and lose no events", async () => { + const names = recordIngestion() + signOut() + configSpy.mockResolvedValue({}) + await using a = await tmpdir({ git: true }) + await using b = await tmpdir({ git: true }) + const enabledDuringTurns: boolean[] = [] + let releaseA!: () => void + const aMidTurn = new Promise((resolve) => (releaseA = resolve)) + let markAStarted!: () => void + const aStarted = new Promise((resolve) => (markAStarted = resolve)) + + const turnA = Instance.provide({ + directory: a.path, + fn: async () => { + await Telemetry.init() + Telemetry.track(errorEvent("A1")) + markAStarted() + await aMidTurn + // B's turn started meanwhile: A's telemetry is still on. + enabledDuringTurns.push(Telemetry.isEnabled()) + Telemetry.track(errorEvent("A2")) + }, + }) + // A failing turn A rejects the wait instead of leaving it hanging. + await Promise.race([aStarted, turnA.then(() => Promise.reject(new Error("turn A ended before starting")))]) try { - Telemetry.setConfigDirectory(project) - await Telemetry.shutdown() - await Telemetry.init() - expect(Telemetry.isEnabled()).toBe(false) - expect(await waitFor(() => optOuts().length === 1)).toBe(true) - expect(optOuts()[0].data.baseData.properties.opt_out_source).toBe("config") + await Instance.provide({ + directory: b.path, + fn: async () => { + await Telemetry.init() + enabledDuringTurns.push(Telemetry.isEnabled()) + Telemetry.track(errorEvent("B1")) + }, + }) } finally { - Telemetry.setConfigDirectory(undefined) - configSpy.mockRestore() - rmSync(project, { recursive: true, force: true }) + releaseA() + await turnA } - }) + await Telemetry.flush() - test("the marker creates a missing ~/.altimate owner-only", () => { - expect(existsSync(path.join(home, ".altimate"))).toBe(false) - OptOutMarker.write() - expect(statSync(path.join(home, ".altimate")).mode & 0o777).toBe(0o700) + expect(enabledDuringTurns).toEqual([true, true]) + // B joined the running lifecycle: no shutdown and re-init, so the config was resolved once. + expect(configSpy).toHaveBeenCalledTimes(1) + expect(names.sort()).toEqual(["A1", "A2", "B1"]) }) - test("turning telemetry back on clears the marker", async () => { - writeMachineId() - OptOutMarker.write() - expect(OptOutMarker.exists()).toBe(true) - await Telemetry.shutdown() - await Telemetry.init() - expect(Telemetry.isEnabled()).toBe(true) - expect(OptOutMarker.exists()).toBe(false) - expect(optOuts()).toHaveLength(0) + test("an unreadable config suppresses all requests", async () => { + process.env.OPENCODE_CONFIG_CONTENT = "{" + await expectSilentLifecycle() }) }) diff --git a/packages/opencode/test/config/config.test.ts b/packages/opencode/test/config/config.test.ts index f32e56f635..4309158091 100644 --- a/packages/opencode/test/config/config.test.ts +++ b/packages/opencode/test/config/config.test.ts @@ -5,6 +5,7 @@ import { NamedError } from "@opencode-ai/core/util/error" import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/unstable/http" import { NodeFileSystem, NodePath } from "@effect/platform-node" import { Config } from "@/config/config" +import { Telemetry } from "@/altimate/telemetry" import { ConfigManaged } from "@/config/managed" import { ConfigParse } from "../../src/config/parse" import { EffectFlock } from "@opencode-ai/core/util/effect-flock" @@ -13,7 +14,7 @@ import { InstanceRef } from "../../src/effect/instance-ref" import type { InstanceContext } from "../../src/project/instance-context" import { Auth } from "../../src/auth" import { Account } from "../../src/account/account" -import { AccessToken, AccountID, OrgID } from "../../src/account/schema" +import { AccessToken, AccountID, AccountServiceError, OrgID } from "../../src/account/schema" import { FSUtil } from "@opencode-ai/core/fs-util" import { Env } from "../../src/env" import { @@ -687,9 +688,192 @@ accountTokenIt.instance("resolves env templates in account config with account t Effect.gen(function* () { const config = yield* Config.use.get() expect(config.provider?.["opencode"]?.options?.apiKey).toBe("st_test_token") + expect(Config.telemetryConfigUnresolved(config)).toBe(false) }), ) +for (const failure of ["account", "organization"] as const) { + const unavailable = Effect.fail(new AccountServiceError({ message: "Account config unavailable" })) + const failedAccountIt = configIt({ + account: Layer.mock(Account.Service)({ + active: () => + failure === "account" + ? unavailable + : Effect.succeed( + Option.some({ + id: AccountID.make("account-1"), + email: "user@example.com", + url: "https://control.example.com", + active_org_id: OrgID.make("org-1"), + }), + ), + config: () => unavailable, + token: () => Effect.succeed(Option.none()), + }), + }) + failedAccountIt.instance(`an unavailable ${failure} leaves the telemetry config unresolved`, () => + Effect.gen(function* () { + const config = yield* Config.use.get() + expect(Config.telemetryConfigUnresolved(config)).toBe(true) + }), + ) +} + +{ + let recovered = false + let orgLoads = 0 + const account = Layer.mock(Account.Service)({ + active: () => Effect.succeed(Option.some({ + id: AccountID.make("telemetry-account"), + email: "user@example.com", + url: "https://control.example.invalid", + active_org_id: OrgID.make("telemetry-org"), + })), + config: () => Effect.suspend(() => { + orgLoads++ + return recovered + ? Effect.succeed(Option.some({ telemetry: { disabled: false } })) + : Effect.fail(new AccountServiceError({ message: "Temporary organization config failure" })) + }), + token: () => Effect.succeed(Option.none()), + }) + configIt({ account }).instance("telemetry sends held events after an organization config fetch recovers", () => + Effect.gen(function* () { + const context = (yield* InstanceRef)! + const service = yield* Config.Service + yield* withProcessEnvs( + { + APPLICATIONINSIGHTS_CONNECTION_STRING: "InstrumentationKey=test;IngestionEndpoint=https://ingest.example.invalid/", + ALTIMATE_TELEMETRY_DISABLED: undefined, + OPENCODE_AUTH_CONTENT: undefined, + ALTIMATE_CLI_DISABLE_TELEMETRY: undefined, + OPENCODE_DISABLE_TELEMETRY: undefined, + }, + Effect.promise(async () => { + const events: string[] = [] + await Telemetry.shutdown() + Telemetry.resetGateForTest() + Telemetry.setConfigDirectory(context.directory) + const fetchSpy = spyOn(globalThis, "fetch").mockImplementation(Object.assign( + async (url: Parameters[0], request?: Parameters[1]) => { + if (url !== "https://ingest.example.invalid/v2/track") throw new Error("Unexpected telemetry request") + for (const envelope of JSON.parse(String(request?.body))) events.push(envelope.data.baseData.name) + return new Response(null, { status: 204 }) + }, + { preconnect: fetch.preconnect }, + )) + // Bridge the Promise facade to the same scoped service for each telemetry init. + const config = spyOn(Config, "get").mockImplementation(() => + Effect.runPromise(provideCurrentInstance(service.get(), context)), + ) + try { + const track = () => Telemetry.track({ + type: "startup_ready", timestamp: Date.now(), session_id: "", + command: "run", surface: "run", duration_ms: 1, fresh_install: false, + }) + track() + await Telemetry.init() + track() + await Telemetry.refreshIdentity() + await Telemetry.flush() + const telemetryCalls = fetchSpy.mock.calls.filter(([url]) => /\/(v2\/track|users\/me)$/.test(String(url))) + expect(telemetryCalls).toEqual([]) + expect(orgLoads).toBe(1) + + // A repeat init inside the Instance reads its cached config: no second org fetch. + await Telemetry.init() + expect(orgLoads).toBe(1) + + // A due retry reads the Instance's cached config: telemetry never forces a refetch. + recovered = true + Telemetry.retryConfigNowForTest() + await Telemetry.init() + expect(orgLoads).toBe(1) + expect(events).toEqual([]) + + // Once the app reloads its config, the next retry reads the recovered one. + await Effect.runPromise(provideCurrentInstance(service.invalidate(), context)) + Telemetry.retryConfigNowForTest() + await Telemetry.init() + await Telemetry.flush() + expect(events).toEqual(["startup_ready", "startup_ready"]) + expect(orgLoads).toBe(2) + } finally { + await Telemetry.shutdown() + Telemetry.setConfigDirectory(undefined) + config.mockRestore() + fetchSpy.mockRestore() + } + }), + ) + }), + ) +} + +it.effect("a global config that fails validation fails the load instead of dropping its opt-out", () => + withGlobalConfig({}, ({ dir }) => + Effect.gen(function* () { + yield* FSUtil.use.writeWithDirs( + path.join(dir, "altimate-code.json"), + JSON.stringify({ telemetry: { disabled: true }, notARealKey: 1 }), + ) + // The load fails outright (it does not fall back to defaults that drop the opt-out), which telemetry + // reads as unresolved inside a project. + const exit = yield* Config.use.get().pipe(provideInstanceEffect(dir), Effect.exit) + expect(Exit.isFailure(exit)).toBe(true) + }).pipe(Effect.provide(testInstanceStoreLayer), Effect.provide(CrossSpawnSpawner.defaultLayer)), + ), +) + +it.effect("a legacy global config that fails to load leaves the telemetry opt-out unresolved", () => + withGlobalConfig({}, ({ dir }) => + Effect.gen(function* () { + yield* FSUtil.use.writeWithDirs(path.join(dir, "config"), "broken = [") + const config = yield* Config.use.get().pipe(provideInstanceEffect(dir)) + expect(Config.telemetryConfigUnresolved(config)).toBe(true) + }).pipe(Effect.provide(testInstanceStoreLayer), Effect.provide(CrossSpawnSpawner.defaultLayer)), + ), +) + +// Writes the auth store (undefined removes it) for one effect, then restores the previous file. +const withAuthStore = (contents: string | undefined, effect: Effect.Effect) => + Effect.gen(function* () { + const authFile = Auth.filepath() + const previous = yield* Effect.promise(() => fs.readFile(authFile, "utf8").catch(() => undefined)) + yield* Effect.promise(async () => { + await fs.mkdir(path.dirname(authFile), { recursive: true }) + if (contents === undefined) await fs.rm(authFile, { force: true }) + else await fs.writeFile(authFile, contents) + }) + return yield* effect.pipe( + Effect.ensuring( + Effect.promise(() => (previous === undefined ? fs.rm(authFile, { force: true }) : fs.writeFile(authFile, previous))), + ), + ) + }) + +for (const [label, contents, unresolved] of [ + ["a missing", undefined, false], + ["an empty", "", false], + ["a whitespace-only", " \n", true], + ["an unparseable", "{", true], +] as const) { + configIt({ auth: Auth.defaultLayer }).instance( + `a config loaded with ${label} auth store leaves the telemetry opt-out ${unresolved ? "unresolved" : "resolved"}`, + () => + withProcessEnv( + "OPENCODE_AUTH_CONTENT", + undefined, + withAuthStore( + contents, + Effect.gen(function* () { + expect(Config.telemetryConfigUnresolved(yield* Config.use.get())).toBe(unresolved) + }), + ), + ), + ) +} + it.instance("validates config schema and throws on invalid fields", () => Effect.gen(function* () { const test = yield* TestInstance diff --git a/packages/opencode/test/install/install-telemetry.test.ts b/packages/opencode/test/install/install-telemetry.test.ts index 4ef0dfe49b..37c31eaa64 100644 --- a/packages/opencode/test/install/install-telemetry.test.ts +++ b/packages/opencode/test/install/install-telemetry.test.ts @@ -27,6 +27,7 @@ import { join } from "node:path" import os from "os" import path from "path" import { Telemetry } from "@/altimate/telemetry" +import { Config } from "@/config/config" const REPO_ROOT = join(import.meta.dir, "..", "..", "..", "..") const INSTALL_SH = readFileSync(join(REPO_ROOT, "install"), "utf-8") @@ -301,8 +302,10 @@ describe("is_upgrade ordering", () => { // would make the "machine-id exists after await" assertion fail on a machine or CI // runner that exports OPENCODE_DISABLE_TELEMETRY. const origOpencodeDisabled = process.env.OPENCODE_DISABLE_TELEMETRY + const origCliDisabled = process.env.ALTIMATE_CLI_DISABLE_TELEMETRY const tmpHome = mkdtempSync(join(os.tmpdir(), "install-telemetry-home-")) spyOn(os, "homedir").mockImplementation(() => tmpHome) + spyOn(Config, "get").mockResolvedValue({}) spyOn(global, "fetch").mockImplementation((async () => new Response("", { status: 200 })) as any) try { @@ -310,6 +313,7 @@ describe("is_upgrade ordering", () => { // when it has no connection string — which would make this test vacuously pass. delete process.env.ALTIMATE_TELEMETRY_DISABLED delete process.env.OPENCODE_DISABLE_TELEMETRY + delete process.env.ALTIMATE_CLI_DISABLE_TELEMETRY process.env.APPLICATIONINSIGHTS_CONNECTION_STRING = "InstrumentationKey=k;IngestionEndpoint=https://example.invalid" // init() is `initPromise ??= doInit()`; shutdown() is the only seam that clears it, so @@ -334,6 +338,8 @@ describe("is_upgrade ordering", () => { else delete process.env.ALTIMATE_TELEMETRY_DISABLED if (origOpencodeDisabled !== undefined) process.env.OPENCODE_DISABLE_TELEMETRY = origOpencodeDisabled else delete process.env.OPENCODE_DISABLE_TELEMETRY + if (origCliDisabled !== undefined) process.env.ALTIMATE_CLI_DISABLE_TELEMETRY = origCliDisabled + else delete process.env.ALTIMATE_CLI_DISABLE_TELEMETRY rmSync(tmpHome, { recursive: true, force: true }) } }) diff --git a/packages/opencode/test/preload.ts b/packages/opencode/test/preload.ts index 1367e86df2..010237a6fc 100644 --- a/packages/opencode/test/preload.ts +++ b/packages/opencode/test/preload.ts @@ -9,6 +9,9 @@ import { afterAll } from "bun:test" import { afterEach, beforeEach } from "bun:test" // altimate_change end +// altimate_change — telemetry exposes its between-test gate reset only when this is set (see below) +process.env.ALTIMATE_TELEMETRY_TEST_HOOKS = "1" + // Set XDG env vars FIRST, before any src/ imports const dir = path.join(os.tmpdir(), "opencode-test-data-" + process.pid) await fs.mkdir(dir, { recursive: true }) @@ -158,6 +161,13 @@ let runningAtStart = new Set>() beforeEach(() => { runningAtStart = new Set(runningSyncs().map(([, sync]) => sync)) }) +// Telemetry's opt-out gate is process-wide and outlives shutdown(); files share a process, so each +// test starts from a fresh launch's state. Only once a test has loaded telemetry: importing it here +// would change module load order for files that mock its dependencies. +beforeEach(() => { + const reset = (globalThis as Record void) | undefined>)[Symbol.for("altimate.telemetry.resetGateForTest")] + reset?.() +}) afterEach(() => { const left = runningSyncs().filter(([, sync]) => !reportedSyncs.has(sync)) if (left.length === 0) return diff --git a/packages/opencode/test/skill/release-v0.12.9-adversarial.test.ts b/packages/opencode/test/skill/release-v0.12.9-adversarial.test.ts index 9c8d529ed7..768637b62b 100644 --- a/packages/opencode/test/skill/release-v0.12.9-adversarial.test.ts +++ b/packages/opencode/test/skill/release-v0.12.9-adversarial.test.ts @@ -2,7 +2,7 @@ * Adversarial coverage for the v0.12.9 release payload (v0.12.8..HEAD): the model-free `tool` CLI and * `mcp serve` permission gate (read/write split, the DROP DATABASE / DROP SCHEMA / TRUNCATE hard deny, the * allow-list, `--args -` folding, runner argument validation), the workspace-rules classifier and terminal - * sanitiser, the telemetry opt-out marker, identity cache, endpoint masking and string masking, sign-out + * sanitiser, the telemetry identity cache, endpoint masking and string masking, sign-out * removing the cached telemetry identity, analyst's post-config write denies, and `Filesystem.realLocation`. * * Targets the exported helpers with hostile inputs: comment-split and dollar-quoted SQL, chained @@ -30,7 +30,6 @@ import { } from "../../src/altimate/tools/sql-classify" import { classify as classifyRule, memoryScope } from "../../src/altimate/rules/classify" import { printable } from "../../src/altimate/rules/printable" -import { OptOutMarker } from "../../src/altimate/telemetry/opt-out" import { TelemetryIdentity } from "../../src/altimate/telemetry/identity" import { Telemetry } from "../../src/altimate/telemetry" import { endpointOf } from "../../src/altimate/workspace/api-client" @@ -543,10 +542,10 @@ describe("v0.12.9 adversarial: printable", () => { }) // --------------------------------------------------------------------------------------------------------- -// 3. #1501 / #1513 — telemetry: opt-out marker, identity cache, endpoint masking, maskString +// 3. #1501 / #1513 — telemetry: identity cache, endpoint masking, maskString // --------------------------------------------------------------------------------------------------------- -describe("v0.12.9 adversarial: telemetry opt-out marker and identity cache", () => { +describe("v0.12.9 adversarial: telemetry identity cache", () => { const originalHome = process.env.OPENCODE_TEST_HOME let home: string @@ -561,94 +560,9 @@ describe("v0.12.9 adversarial: telemetry opt-out marker and identity cache", () }) test("paths resolve under the redirected home", () => { - expect(OptOutMarker.markerPath()).toBe(path.join(home, ".altimate", "telemetry-opted-out")) expect(TelemetryIdentity.cachePath()).toBe(path.join(home, ".altimate", "telemetry-identity.json")) }) - test("write creates the marker once; a second write fails instead of overwriting", () => { - expect(OptOutMarker.exists()).toBe(false) - OptOutMarker.write() - expect(OptOutMarker.exists()).toBe(true) - expect(Number.isNaN(Date.parse(fs.readFileSync(OptOutMarker.markerPath(), "utf8").trim()))).toBe(false) - expect(() => OptOutMarker.write()).toThrow() - }) - - test.skipIf(process.platform === "win32")("write never follows a planted symlink", () => { - const victim = path.join(home, "victim.txt") - fs.writeFileSync(victim, "untouched") - fs.mkdirSync(path.join(home, ".altimate"), { recursive: true }) - fs.symlinkSync(victim, OptOutMarker.markerPath()) - expect(() => OptOutMarker.write()).toThrow() - expect(fs.readFileSync(victim, "utf8")).toBe("untouched") - }) - - test("claim is exclusive: a fresh claim blocks a second one until released", () => { - fs.mkdirSync(path.join(home, ".altimate"), { recursive: true }) - const token = OptOutMarker.claim() - expect(token).toBeString() - expect(OptOutMarker.holds(token!)).toBe(true) - expect(OptOutMarker.claim()).toBeUndefined() - OptOutMarker.release(token!) - expect(OptOutMarker.holds(token!)).toBe(false) - const next = OptOutMarker.claim() - expect(next).toBeString() - expect(next).not.toBe(token) - }) - - test("a stale claim is taken over", () => { - fs.mkdirSync(path.join(home, ".altimate"), { recursive: true }) - const old = OptOutMarker.claim()! - const claimFile = path.join(home, ".altimate", "telemetry-opted-out.sending") - const past = new Date(Date.now() - 60_000) - fs.utimesSync(claimFile, past, past) - const fresh = OptOutMarker.claim() - expect(fresh).toBeString() - expect(OptOutMarker.holds(old)).toBe(false) - expect(OptOutMarker.holds(fresh!)).toBe(true) - }) - - test("claim without the .altimate directory returns undefined instead of throwing", () => { - expect(OptOutMarker.claim()).toBeUndefined() - }) - - test("holds and release reject foreign, empty and prefix tokens", () => { - fs.mkdirSync(path.join(home, ".altimate"), { recursive: true }) - const token = OptOutMarker.claim()! - for (const other of ["", " ", token.slice(0, -1), `${token}x`, "__proto__"]) { - expect(OptOutMarker.holds(other), other).toBe(false) - OptOutMarker.release(other) - expect(OptOutMarker.holds(token)).toBe(true) - } - }) - - test("holds does not trim the caller's token", () => { - fs.mkdirSync(path.join(home, ".altimate"), { recursive: true }) - const token = OptOutMarker.claim()! - expect(OptOutMarker.holds(` ${token}`)).toBe(false) - }) - - test("clear removes the marker and an in-flight claim, and is a no-op when both are absent", () => { - expect(() => OptOutMarker.clear()).not.toThrow() - OptOutMarker.write() - const token = OptOutMarker.claim()! - OptOutMarker.clear() - expect(OptOutMarker.exists()).toBe(false) - expect(OptOutMarker.holds(token)).toBe(false) - }) - - test("readMachineId ignores a non-UUID or oversized file", () => { - fs.mkdirSync(path.join(home, ".altimate"), { recursive: true }) - const idFile = path.join(home, ".altimate", "machine-id") - expect(OptOutMarker.readMachineId()).toBeUndefined() - fs.writeFileSync(idFile, "not-a-uuid") - expect(OptOutMarker.readMachineId()).toBeUndefined() - fs.writeFileSync(idFile, "a".repeat(10_000)) - expect(OptOutMarker.readMachineId()).toBeUndefined() - const uuid = "123e4567-e89b-42d3-a456-426614174000" - fs.writeFileSync(idFile, `${uuid}\n`) - expect(OptOutMarker.readMachineId()).toBe(uuid) - }) - test("identity clear is a no-op when the cache is absent and removes it when present", async () => { await expect(TelemetryIdentity.clear()).resolves.toBeUndefined() fs.mkdirSync(path.dirname(TelemetryIdentity.cachePath()), { recursive: true }) diff --git a/packages/opencode/test/telemetry/telemetry-opt-out-flag.test.ts b/packages/opencode/test/telemetry/telemetry-opt-out-flag.test.ts index d985834736..58e4036b56 100644 --- a/packages/opencode/test/telemetry/telemetry-opt-out-flag.test.ts +++ b/packages/opencode/test/telemetry/telemetry-opt-out-flag.test.ts @@ -1,181 +1,51 @@ -// v0.9.5 review — Chaos gremlin P1 + End-user P2, plus bot-review follow-ups -// (codex + coderabbit + cubic all named the same gap: the initial version of -// this file only tested the shared `Flag.truthyEnv` helper against -// `ALTIMATE_TELEMETRY_DISABLED`. That meant a regression that dropped the -// `OPENCODE_DISABLE_TELEMETRY` fallback OR removed the OR-composition at either -// call site (`altimate/plugin/altimate.ts::buildCliContext`, -// `altimate/telemetry/index.ts::doInit`) would still pass every test — the -// exact failure this file exists to prevent). -// -// The file now covers three layers: -// 1. Parser semantics — `Flag.truthyEnv` accepts "true"/"TRUE"/"1", rejects -// the everything-else surface (case sensitivity, whitespace, non-1 digits). -// 2. Consumer-boundary composition — the exact `truthyEnv(A) || truthyEnv(B)` -// shape used at both gate sites returns true when EITHER env var is set. -// 3. Gate-site source anchor — both `altimate.ts` and `telemetry/index.ts` -// reference both env-var names on the same line. Brittle by design: if a -// future edit deletes the fallback branch, this test catches it without -// needing to boot the full telemetry init in a unit test. -// -// Env-mutation isolation: each test snapshots the two vars before mutating -// them and restores what it found. This keeps the suite safe against other -// tests in the same process reading `process.env.ALTIMATE_TELEMETRY_DISABLED` -// or `OPENCODE_DISABLE_TELEMETRY` and against parallel suites in other -// packages (bot-review follow-up, coderabbit). - -import { afterEach, beforeEach, describe, expect, test } from "bun:test" -import fs from "fs" +import { afterEach, beforeEach, expect, test } from "bun:test" +import { readFileSync } from "fs" import path from "path" -import { Flag } from "../../src/flag/flag" - -const ALTIMATE_VAR = "ALTIMATE_TELEMETRY_DISABLED" -const OPENCODE_VAR = "OPENCODE_DISABLE_TELEMETRY" +import { TelemetryConfig } from "../../src/altimate/telemetry/config-state" -// Snapshot both env vars and restore them after each test — protects against -// leakage into or out of this suite regardless of what set them. -let snapshot: { altimate: string | undefined; opencode: string | undefined } +const keys = ["ALTIMATE_TELEMETRY_DISABLED", "ALTIMATE_CLI_DISABLE_TELEMETRY", "OPENCODE_DISABLE_TELEMETRY"] +const saved = new Map() beforeEach(() => { - snapshot = { - altimate: process.env[ALTIMATE_VAR], - opencode: process.env[OPENCODE_VAR], + for (const key of keys) { + saved.set(key, process.env[key]) + delete process.env[key] } }) afterEach(() => { - if (snapshot.altimate === undefined) delete process.env[ALTIMATE_VAR] - else process.env[ALTIMATE_VAR] = snapshot.altimate - if (snapshot.opencode === undefined) delete process.env[OPENCODE_VAR] - else process.env[OPENCODE_VAR] = snapshot.opencode + for (const [key, value] of saved) { + if (value === undefined) delete process.env[key] + else process.env[key] = value + } }) -describe("Flag.truthyEnv — parser semantics on ALTIMATE_TELEMETRY_DISABLED", () => { - test("unset env var → false (default: telemetry enabled)", () => { - delete process.env[ALTIMATE_VAR] - expect(Flag.truthyEnv(ALTIMATE_VAR)).toBe(false) - }) - - test("empty string → false", () => { - process.env[ALTIMATE_VAR] = "" - expect(Flag.truthyEnv(ALTIMATE_VAR)).toBe(false) - }) - - test.each([ - ["true", true], - ["TRUE", true], - ["True", true], - ["1", true], - ["false", false], - ["FALSE", false], - ["0", false], - ["yes", false], - ["on", false], - [" true ", false], // no trimming — matches existing truthy() semantics - ["2", false], - ])("value %j → %s", (value, expected) => { - process.env[ALTIMATE_VAR] = value - expect(Flag.truthyEnv(ALTIMATE_VAR)).toBe(expected) - }) - - test("re-reads process.env on each call (not frozen at import)", () => { - delete process.env[ALTIMATE_VAR] - expect(Flag.truthyEnv(ALTIMATE_VAR)).toBe(false) - process.env[ALTIMATE_VAR] = "1" - expect(Flag.truthyEnv(ALTIMATE_VAR)).toBe(true) - process.env[ALTIMATE_VAR] = "false" - expect(Flag.truthyEnv(ALTIMATE_VAR)).toBe(false) - }) +test("an unset environment does not opt out", () => { + expect(TelemetryConfig.disablingEnvironmentVariable()).toBeUndefined() }) -describe("Consumer-boundary composition — both env vars route through the same OR gate", () => { - // The two production callers use `truthyEnv(A) || truthyEnv(B)`. These tests - // exercise both branches of that OR expression so a regression that hard-wires - // one branch to false would fail here (codex/coderabbit/cubic finding). - // - // We reproduce the composition inline via a local helper so the tests read - // as one unit rather than four permutations. The "gate-site anchor" tests - // below then prove the production callers actually contain this shape. - const gate = () => - Flag.truthyEnv(ALTIMATE_VAR) || Flag.truthyEnv(OPENCODE_VAR) - - test("neither set → gate is closed (telemetry enabled)", () => { - delete process.env[ALTIMATE_VAR] - delete process.env[OPENCODE_VAR] - expect(gate()).toBe(false) - }) - - test("only ALTIMATE_TELEMETRY_DISABLED set → gate is open (primary branch)", () => { - delete process.env[OPENCODE_VAR] - process.env[ALTIMATE_VAR] = "1" - expect(gate()).toBe(true) - }) - - test("only OPENCODE_DISABLE_TELEMETRY set → gate is open (fallback branch)", () => { - // v0.9.4 CHANGELOG advertised this name; before v0.9.5 it was silently - // ignored in product. Removing the second `truthyEnv(...)` from either - // call site would silently regress this — the source-anchor test below - // catches that additional shape. - delete process.env[ALTIMATE_VAR] - process.env[OPENCODE_VAR] = "1" - expect(gate()).toBe(true) - }) - - test.each([ - ["true", "true"], - ["1", "1"], - ["TRUE", "true"], - ])( - "both set (%j / %j) → gate is open", - (altimateVal, opencodeVal) => { - process.env[ALTIMATE_VAR] = altimateVal - process.env[OPENCODE_VAR] = opencodeVal - expect(gate()).toBe(true) - }, - ) - - test("both set with non-truthy values → gate is closed", () => { - process.env[ALTIMATE_VAR] = "false" - process.env[OPENCODE_VAR] = "0" - expect(gate()).toBe(false) - }) +test.each(["true", "TRUE", "True", "1"])("accepts %j", (value) => { + process.env.ALTIMATE_TELEMETRY_DISABLED = value + expect(TelemetryConfig.disablingEnvironmentVariable()).toBe("ALTIMATE_TELEMETRY_DISABLED") }) -describe("Gate-site anchor — both call sites reference both env-var names", () => { - // codex/coderabbit/cubic all pointed out that testing the shared helper in - // isolation doesn't prove the CALLER retains the fallback branch. Without an - // integration-level runner for `doInit()` / `buildCliContext` (both touch - // Config, machine-id, and a network sink), the closest deterministic proof is - // a source-shape assertion: both call sites must mention both env-var names - // within a small window. A future edit that deletes the fallback fails here - // and gives a clear name for what regressed. - // - // The window (a single non-comment line, or up to two adjacent non-comment - // lines) is deliberately narrow — enough for the OR expression to wrap to a - // second line for prettier, but not enough for the two names to sit in - // unrelated pieces of the file. - const GATE_FILES = [ - "src/altimate/plugin/altimate.ts", - "src/altimate/telemetry/index.ts", - ] +test.each(["", "false", "FALSE", "0", "yes", "on", " true ", "2"])("rejects %j", (value) => { + process.env.ALTIMATE_TELEMETRY_DISABLED = value + expect(TelemetryConfig.disablingEnvironmentVariable()).toBeUndefined() +}) - test.each(GATE_FILES)( - "%s references both %s and %s in the same gate", - (relativePath) => { - const absolute = path.resolve(__dirname, "../..", relativePath) - const source = fs.readFileSync(absolute, "utf8") - // Strip line comments so a `// mentions X and Y` comment doesn't count. - const codeLines = source - .split("\n") - .map((l) => l.replace(/\/\/.*$/, "").trim()) - const window = 2 - let matched = false - for (let i = 0; i < codeLines.length && !matched; i++) { - const slice = codeLines.slice(i, i + window).join(" ") - if (slice.includes(ALTIMATE_VAR) && slice.includes(OPENCODE_VAR)) { - matched = true - } - } - expect(matched).toBe(true) - }, - ) +test.each(keys)("%s opts out independently of the other names", (selected) => { + for (const key of keys) process.env[key] = "false" + process.env[selected] = "1" + expect(TelemetryConfig.disablingEnvironmentVariable()).toBe(selected) + process.env[selected] = "0" + expect(TelemetryConfig.disablingEnvironmentVariable()).toBeUndefined() }) + +test.each(["src/altimate/plugin/altimate.ts", "src/altimate/telemetry/index.ts"])( + "%s uses the shared opt-out", + (relativePath) => { + const source = readFileSync(path.resolve(__dirname, "../..", relativePath), "utf8") + expect(source).toMatch(/^[^/\n]*TelemetryConfig\.disablingEnvironmentVariable\(\)/m) + }, +) diff --git a/packages/opencode/test/upstream/altimate-features.test.ts b/packages/opencode/test/upstream/altimate-features.test.ts index a57a868f9d..690ede847d 100644 --- a/packages/opencode/test/upstream/altimate-features.test.ts +++ b/packages/opencode/test/upstream/altimate-features.test.ts @@ -398,13 +398,31 @@ describe("altimate features: SystemPrompt.skills sorting", () => { // 9. Telemetry stays fail-safe outside Instance context // =========================================================================== describe("altimate features: telemetry fail-safe init", () => { - test("telemetry/index.ts catches Config.get() failures so init never throws", async () => { - const src = await readSrc("altimate", "telemetry", "index.ts") - // The try/catch around Config.get() — must be present so init() can be - // called from CLI middleware before Instance.provide(). - expect(src).toMatch(/try\s*\{\s*const userConfig = \(await Config\.get\(\)\)/) - // The ALTIMATE_TELEMETRY_DISABLED env var escape hatch. - expect(src).toContain("ALTIMATE_TELEMETRY_DISABLED") + test("telemetry can resolve the opt-out before an Instance exists", async () => { + const init = await readSrc("altimate", "telemetry", "index.ts") + const config = await readSrc("altimate", "telemetry", "config-state.ts") + expect(init).toContain("TelemetryConfig.resolve(configDirectory") + expect(config).toMatch(/try\s*\{\s*return await loadedState\(\)/) + expect(config).toContain('return instanceDirectory() ? "unreadable" : disabledState(directory)') + expect(config).toContain("ALTIMATE_TELEMETRY_DISABLED") + }) + + test("both CLI project loaders settle an opt-out the startup read left open", async () => { + // Behaviour is tested in opt-out.test.ts through bootstrap(); this pins the effectCmd loader too. + const bootstrap = await readSrc("cli", "bootstrap.ts") + const effectCmd = await readSrc("cli", "effect-cmd.ts") + expect(bootstrap).toMatch(/Instance\.restore\(ctx, async \(\) => \{\s*await Telemetry\.settleInProject\(\)\s*return cb\(\)/) + expect(effectCmd).toMatch(/Instance\.restore\(ctx, async \(\) => \{\s*await Telemetry\.settleInProject\(\)\s*return AppRuntime\.runPromise\(opts\.handler/) + }) + + test("the TUI main thread adopts the worker's resolved opt-out", async () => { + const tui = await readSrc("cli", "cmd", "tui.ts") + const worker = await readSrc("cli", "tui", "worker.ts") + expect(tui).toMatch(/\.call\("telemetryOptOut", undefined\)\s*\.then\(async \(state\) => Telemetry\.adoptOptOut\(await state\)\)/) + expect(worker).toMatch(/async telemetryOptOut\(\) \{[\s\S]*?return Telemetry\.settledOptOutState\(\)/) + // A decision the worker makes later (a later init) reaches the main thread too. + expect(worker).toContain('Telemetry.onOptOutDecided((state) => Rpc.emit("telemetry.optOut", state))') + expect(tui).toMatch(/client\.on<[^>]*>\("telemetry\.optOut", \(state\) => \{\s*Telemetry\.adoptOptOut\(state\)/) }) test("telemetry/index.ts no longer reads the account; identity comes from the stored credential", async () => {