|
| 1 | +// altimate_change — regression test for two fixes that together make the `telemetry.disabled` |
| 2 | +// config opt-out actually work. |
| 3 | +// |
| 4 | +// Fix 1 (src/cli/tui/worker.ts): `Telemetry.init()` used to run at module top level in the bare |
| 5 | +// TUI worker, where `Config.get()` throws (no Instance context yet), so `doInit()` proceeded as |
| 6 | +// enabled and the opt-out was silently bypassed for the whole first session. It now runs inside |
| 7 | +// `Instance.restore(ctx, ...)` in `traceReady` (worker.ts's `traceReady` IIFE), specifically so |
| 8 | +// `doInit()`'s `Config.get()` can see the merged, instance-scoped config. |
| 9 | +// |
| 10 | +// Fix 2 (packages/core/src/v1/config/config.ts): even with fix 1, `ConfigV1.Info` (the schema |
| 11 | +// every project/global/local config file is parsed against) had NO `telemetry` field at all. |
| 12 | +// `doInit()` (packages/opencode/src/altimate/telemetry/index.ts:2019-2020) reads |
| 13 | +// `(await Config.get() as any).telemetry?.disabled` — before this fix the `as any` cast was hiding |
| 14 | +// that it read a field the schema never declared. Writing `{ "telemetry": { "disabled": true } }` |
| 15 | +// to a config file made THAT FILE's `ConfigParse.schema(ConfigV1.Info, ...)` call throw |
| 16 | +// `unrecognized_keys`, which propagated out of `Config.get()` as a `ConfigInvalidError` Die defect |
| 17 | +// instead of merging past that file — `doInit()`'s own try/catch around `Config.get()` |
| 18 | +// (index.ts:2017-2023) treats ANY failure as "Config unavailable — proceed with telemetry |
| 19 | +// enabled", so the opt-out did not merely fail silently: adding it to a config file broke that |
| 20 | +// file's config loading entirely (for every other config-dependent feature in that Instance too, |
| 21 | +// not just telemetry) and telemetry stayed on regardless. `ConfigV1.Info` now declares |
| 22 | +// `telemetry: Schema.optional(Schema.Struct({ disabled: Schema.optional(Schema.Boolean) }))`, so |
| 23 | +// the field parses cleanly and `doInit()` can actually see it. |
| 24 | +// |
| 25 | +// Both fixes are necessary: fix 1 alone gets `Config.get()` a working Instance context but the |
| 26 | +// schema still rejected the field; fix 2 alone adds the field to the schema but the bare worker's |
| 27 | +// `Config.get()` call still would have thrown before ever reading it. The three tests below prove, |
| 28 | +// in order: the Instance-context mechanism resolves the right config (fix 1), the happy path with |
| 29 | +// no opt-out still enables telemetry, and the opt-out itself now disables it end to end (fix 2). |
| 30 | +import { afterEach, describe, expect, spyOn } from "bun:test" |
| 31 | +import { Effect, Layer } from "effect" |
| 32 | +import { FSUtil } from "@opencode-ai/core/fs-util" |
| 33 | +import { Config } from "@/config/config" |
| 34 | +import { Telemetry } from "@/altimate/telemetry" |
| 35 | +import { TestInstance } from "../fixture/fixture" |
| 36 | +import { testEffect } from "../lib/effect" |
| 37 | + |
| 38 | +const it = testEffect(Layer.mergeAll(Config.defaultLayer, FSUtil.defaultLayer)) |
| 39 | + |
| 40 | +// Local copy of config.test.ts's / tui.test.ts's env-restore helper — neither file exports it, and |
| 41 | +// every existing suite that needs one duplicates it rather than centralizing. |
| 42 | +function withProcessEnvs<A, E, R>(entries: Record<string, string | undefined>, effect: Effect.Effect<A, E, R>) { |
| 43 | + return Effect.acquireUseRelease( |
| 44 | + Effect.sync(() => { |
| 45 | + const originals: Record<string, string | undefined> = {} |
| 46 | + for (const [key, value] of Object.entries(entries)) { |
| 47 | + originals[key] = process.env[key] |
| 48 | + if (value === undefined) delete process.env[key] |
| 49 | + else process.env[key] = value |
| 50 | + } |
| 51 | + return originals |
| 52 | + }), |
| 53 | + () => effect, |
| 54 | + (originals) => |
| 55 | + Effect.sync(() => { |
| 56 | + for (const [key, original] of Object.entries(originals)) { |
| 57 | + if (original !== undefined) process.env[key] = original |
| 58 | + else delete process.env[key] |
| 59 | + } |
| 60 | + }), |
| 61 | + ) |
| 62 | +} |
| 63 | + |
| 64 | +// Explicit connection string bypasses isAutomatedRun()'s implicit-production-sink withholding (see |
| 65 | +// doInit()'s comment: "Telemetry's own tests set APPLICATIONINSIGHTS_CONNECTION_STRING explicitly |
| 66 | +// and are unaffected"), and both disable env vars must be cleared so only the config path is live. |
| 67 | +const TELEMETRY_ENV = { |
| 68 | + APPLICATIONINSIGHTS_CONNECTION_STRING: "InstrumentationKey=config-opt-out-test;IngestionEndpoint=https://example.com", |
| 69 | + ALTIMATE_TELEMETRY_DISABLED: undefined, |
| 70 | + OPENCODE_DISABLE_TELEMETRY: undefined, |
| 71 | +} |
| 72 | + |
| 73 | +describe("Telemetry.init() inside an Instance context — config.telemetry.disabled opt-out", () => { |
| 74 | + afterEach(async () => { |
| 75 | + await Telemetry.shutdown() |
| 76 | + }) |
| 77 | + |
| 78 | + it.instance( |
| 79 | + "Config.get(), called the same way doInit() calls it, resolves the Instance-scoped project config instead of throwing — the mechanism the worker.ts fix relies on", |
| 80 | + () => |
| 81 | + Effect.gen(function* () { |
| 82 | + const fs = yield* FSUtil.Service |
| 83 | + const test = yield* TestInstance |
| 84 | + yield* fs.writeJson(`${test.directory}/opencode.json`, { |
| 85 | + $schema: "https://opencode.ai/config.json", |
| 86 | + username: "instance-scoped-marker", |
| 87 | + }) |
| 88 | + // The plain async Config.get() facade is exactly what doInit() calls; invoking it via |
| 89 | + // Effect.promise from inside this it.instance's Instance/InstanceRef context mirrors |
| 90 | + // Instance.restore(ctx, () => Telemetry.init()) in worker.ts's traceReady. Before the fix, |
| 91 | + // calling this at bare module scope (no Instance) would throw instead of resolving. |
| 92 | + const config = yield* Effect.promise(() => Config.get()) |
| 93 | + expect((config as any).username).toBe("instance-scoped-marker") |
| 94 | + }), |
| 95 | + ) |
| 96 | + |
| 97 | + it.instance( |
| 98 | + "without a telemetry opt-out, Telemetry.init() run inside the Instance context enables telemetry and flushes anchor events", |
| 99 | + () => |
| 100 | + withProcessEnvs( |
| 101 | + TELEMETRY_ENV, |
| 102 | + Effect.gen(function* () { |
| 103 | + const flushSpy = spyOn(Telemetry, "flush") |
| 104 | + try { |
| 105 | + yield* Effect.promise(() => Telemetry.init()) |
| 106 | + expect(Telemetry.isEnabled()).toBe(true) |
| 107 | + Telemetry.track({ |
| 108 | + type: "startup_ready", |
| 109 | + timestamp: Date.now(), |
| 110 | + session_id: "config-opt-out-enabled", |
| 111 | + command: "test", |
| 112 | + duration_ms: 1, |
| 113 | + fresh_install: false, |
| 114 | + }) |
| 115 | + expect(flushSpy).toHaveBeenCalled() |
| 116 | + } finally { |
| 117 | + flushSpy.mockRestore() |
| 118 | + } |
| 119 | + }), |
| 120 | + ), |
| 121 | + ) |
| 122 | + |
| 123 | + it.instance( |
| 124 | + "telemetry.disabled: true in project config disables telemetry end to end, with no flush", |
| 125 | + () => |
| 126 | + withProcessEnvs( |
| 127 | + TELEMETRY_ENV, |
| 128 | + Effect.gen(function* () { |
| 129 | + const fs = yield* FSUtil.Service |
| 130 | + const test = yield* TestInstance |
| 131 | + const flushSpy = spyOn(Telemetry, "flush") |
| 132 | + try { |
| 133 | + yield* fs.writeJson(`${test.directory}/opencode.json`, { |
| 134 | + $schema: "https://opencode.ai/config.json", |
| 135 | + telemetry: { disabled: true }, |
| 136 | + }) |
| 137 | + yield* Effect.promise(() => Telemetry.init()) |
| 138 | + expect(Telemetry.isEnabled()).toBe(false) |
| 139 | + expect(flushSpy).not.toHaveBeenCalled() |
| 140 | + } finally { |
| 141 | + flushSpy.mockRestore() |
| 142 | + } |
| 143 | + }), |
| 144 | + ), |
| 145 | + ) |
| 146 | +}) |
0 commit comments