Skip to content

Commit bf62a07

Browse files
feat: send one final telemetry_opted_out event when telemetry is disabled
A machine that has sent telemetry before sends the opt-out once (machine id, version, how it was set, cached user id and tenant) and nothing after; a marker next to the machine id keeps it to once and is cleared when telemetry is re-enabled. Closes #1499 Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent d275dec commit bf62a07

4 files changed

Lines changed: 301 additions & 1 deletion

File tree

‎packages/opencode/src/altimate/telemetry/index.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { TraceContext } from "@/altimate/observability/trace-context"
66
// altimate_change — shared machine-id helper (race-safe, UUID-validated, size-capped)
77
import { getOrCreateMachineId } from "@/altimate/util/machine-id"
88
import { TelemetryIdentity } from "./identity"
9+
import { OptOutMarker } from "./opt-out"
910
import { createHash, randomUUID } from "crypto"
1011
// altimate_change — first-run health: the event-loop stall monitor labels which thread stalled
1112
import { isMainThread } from "node:worker_threads"
@@ -65,9 +66,17 @@ const log = Log.create({ service: "telemetry" })
6566
*/
6667
function isAutomatedRun(): boolean {
6768
if (process.env.ALTIMATE_TELEMETRY_FORCE === "true") return false
69+
return underTestRunner()
70+
}
71+
72+
// altimate_change start — the raw test-runner check, without the force flag. The opt-out marker
73+
// reads and writes files in the real home directory, so under a test runner it stays inert unless
74+
// a suite opts in through Telemetry.allowOptOutUnderTestRunner(); the force flag must not reach it.
75+
function underTestRunner(): boolean {
6876
if (process.env.NODE_ENV === "test") return true
6977
return Boolean(process.env.BUN_TEST || process.env.VITEST || process.env.JEST_WORKER_ID)
7078
}
79+
// altimate_change end
7180

7281
// altimate_change start — composed path-masking rules (shared fragments)
7382
// R: one path-run character — anything but whitespace/separators/quotes,
@@ -668,6 +677,14 @@ export namespace Telemetry {
668677
// The 0.11.0 first-run freeze (an in-process @npmcli/arborist install blocking the loop for minutes)
669678
// was invisible for two months: nothing timed startup or registration, and a blocked loop cannot
670679
// flush, so a killed process left only `session_start`. These events close that gap.
680+
| {
681+
/** The last event an opted-out machine sends; nothing follows it until telemetry is
682+
* turned back on. Built directly in recordOptOut(), never through track(). */
683+
type: "telemetry_opted_out"
684+
timestamp: number
685+
/** Where the opt-out was set: an environment variable or the user config. */
686+
opt_out_source: "env" | "config"
687+
}
671688
| {
672689
type: "startup_ready"
673690
timestamp: number
@@ -2062,6 +2079,7 @@ export namespace Telemetry {
20622079
// (previously only wired in test fixtures, silent no-op in product).
20632080
if (Flag.truthyEnv("ALTIMATE_TELEMETRY_DISABLED") || Flag.truthyEnv("OPENCODE_DISABLE_TELEMETRY")) {
20642081
buffer = []
2082+
void recordOptOut("env")
20652083
return
20662084
}
20672085
// Config.get() may throw outside Instance context (e.g. CLI middleware
@@ -2071,6 +2089,7 @@ export namespace Telemetry {
20712089
const userConfig = (await Config.get()) as any
20722090
if (userConfig.telemetry?.disabled) {
20732091
buffer = []
2092+
void recordOptOut("config")
20742093
return
20752094
}
20762095
} catch {
@@ -2106,6 +2125,9 @@ export namespace Telemetry {
21062125
// logs + returns ""). No try/catch needed — all paths are handled inside.
21072126
machineId = getOrCreateMachineId()
21082127
enabled = true
2128+
// altimate_change — telemetry is on: forget the opt-out marker, so turning it off again is
2129+
// recorded again.
2130+
OptOutMarker.clear()
21092131
log.info("telemetry initialized", { mode: "appinsights" })
21102132
// altimate_change — clear any existing interval before installing a new one. doInit() can
21112133
// run more than once per process (init/shutdown cycles per session in prompt.ts), and
@@ -2158,6 +2180,59 @@ export namespace Telemetry {
21582180
}
21592181
// altimate_change end
21602182

2183+
// altimate_change start — the final event of an opted-out machine. Sent once, only from a
2184+
// machine that has sent telemetry before (a machine id exists; none is minted here), under the
2185+
// same sink rules as doInit(), with the identity cached by an earlier signed-in run (no request
2186+
// to resolve it). The marker is written only once the ingest endpoint accepted the event, so an
2187+
// offline launch tries again next time; after the marker, nothing is sent until telemetry is
2188+
// re-enabled. Not awaited: an opted-out launch must not wait on the network.
2189+
const OPT_OUT_TIMEOUT_MS = 3_000
2190+
let optOutUnderTestRunner = false
2191+
/** Test seam: let a suite that redirects the home directory exercise recordOptOut(). */
2192+
export function allowOptOutUnderTestRunner(on: boolean) {
2193+
optOutUnderTestRunner = on
2194+
}
2195+
async function recordOptOut(source: "env" | "config"): Promise<void> {
2196+
try {
2197+
if (underTestRunner() && !optOutUnderTestRunner) return
2198+
// The force flag is a developer knob for running telemetry under a test runner; the opt-out
2199+
// marker is about real users, and "disabled" must keep winning over "force" to the letter.
2200+
if (process.env.ALTIMATE_TELEMETRY_FORCE === "true") return
2201+
if (OptOutMarker.exists()) return
2202+
const id = OptOutMarker.readMachineId()
2203+
if (!id) return
2204+
const explicit = process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
2205+
const cfg = parseConnectionString(explicit ?? DEFAULT_CONNECTION_STRING)
2206+
if (!cfg) return
2207+
machineId = id
2208+
identity = await TelemetryIdentity.cached()
2209+
const envelopes = toAppInsightsEnvelopes(
2210+
[{ type: "telemetry_opted_out", timestamp: Date.now(), opt_out_source: source }],
2211+
cfg,
2212+
)
2213+
machineId = ""
2214+
identity = undefined
2215+
const controller = new AbortController()
2216+
const timeout = setTimeout(() => controller.abort(), OPT_OUT_TIMEOUT_MS)
2217+
try {
2218+
const res = await fetch(cfg.endpoint, {
2219+
method: "POST",
2220+
headers: { "Content-Type": "application/json" },
2221+
body: JSON.stringify(envelopes),
2222+
signal: controller.signal,
2223+
})
2224+
if (res.ok) OptOutMarker.write()
2225+
} finally {
2226+
clearTimeout(timeout)
2227+
}
2228+
} catch {
2229+
// The opt-out path never surfaces: a missed marker only means one more attempt next launch.
2230+
machineId = ""
2231+
identity = undefined
2232+
}
2233+
}
2234+
// altimate_change end
2235+
21612236
export function setContext(opts: { sessionId: string; projectId: string }) {
21622237
sessionId = opts.sessionId
21632238
projectId = opts.projectId
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import fs from "fs"
2+
import path from "path"
3+
import { Global } from "../../global"
4+
5+
/**
6+
* The marker that says this machine already sent its final ``telemetry_opted_out`` event.
7+
*
8+
* A machine that turns telemetry off goes silent, which on the dashboards looks exactly like a
9+
* client that stopped emitting. So the last event an opted-out machine sends is the opt-out
10+
* itself, once; the marker is what makes it once. It lives next to the machine id because both
11+
* describe the machine, not the user, and it is removed when telemetry is turned back on so a
12+
* later opt-out is recorded again. See ``recordOptOut`` in index.ts.
13+
*
14+
* Paths go through ``Global.Path.home`` so a test that sets ``OPENCODE_TEST_HOME`` never reads
15+
* or writes the real home directory; in a real session that is the same directory the machine
16+
* id lives in.
17+
*/
18+
export namespace OptOutMarker {
19+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
20+
21+
function dir(): string {
22+
return path.join(Global.Path.home, ".altimate")
23+
}
24+
25+
export function markerPath(): string {
26+
return path.join(dir(), "telemetry-opted-out")
27+
}
28+
29+
/** The machine id minted by an earlier telemetry-enabled run, if any. Never mints one: a machine
30+
* that never sent telemetry has nothing to mark. */
31+
export function readMachineId(): string | undefined {
32+
try {
33+
const raw = fs.readFileSync(path.join(dir(), "machine-id"), "utf8").trim()
34+
return UUID.test(raw) ? raw : undefined
35+
} catch {
36+
return undefined
37+
}
38+
}
39+
40+
export function exists(): boolean {
41+
return fs.existsSync(markerPath())
42+
}
43+
44+
export function write(): void {
45+
fs.mkdirSync(dir(), { recursive: true })
46+
fs.writeFileSync(markerPath(), `${new Date().toISOString()}\n`, { mode: 0o600 })
47+
}
48+
49+
export function clear(): void {
50+
fs.rmSync(markerPath(), { force: true })
51+
}
52+
}
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"
2+
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "fs"
3+
import os from "os"
4+
import path from "path"
5+
import { Telemetry } from "../../../src/altimate/telemetry"
6+
import { TelemetryIdentity } from "../../../src/altimate/telemetry/identity"
7+
import { OptOutMarker } from "../../../src/altimate/telemetry/opt-out"
8+
9+
const MACHINE = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d"
10+
const UUID = "0f3c2a1e-6b7d-4c8e-9f10-1a2b3c4d5e6f"
11+
const API = "https://api.example.invalid"
12+
const CS = "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://ingest.example.invalid/"
13+
14+
type Call = { url: string; body: string }
15+
16+
async function waitFor(pred: () => boolean, ms = 2_000) {
17+
const until = Date.now() + ms
18+
while (!pred() && Date.now() < until) await new Promise((r) => setTimeout(r, 20))
19+
return pred()
20+
}
21+
22+
describe("telemetry opt-out marker", () => {
23+
let home: string
24+
let calls: Call[]
25+
let trackStatus = 200
26+
let fetchSpy: ReturnType<typeof spyOn>
27+
let homedirSpy: ReturnType<typeof spyOn>
28+
const saved: Record<string, string | undefined> = {}
29+
const ENV = [
30+
"OPENCODE_TEST_HOME",
31+
"APPLICATIONINSIGHTS_CONNECTION_STRING",
32+
"ALTIMATE_TELEMETRY_DISABLED",
33+
"OPENCODE_DISABLE_TELEMETRY",
34+
]
35+
36+
const optOuts = () =>
37+
calls
38+
.filter((c) => c.url.includes("/v2/track"))
39+
.flatMap((c) => JSON.parse(c.body) as any[])
40+
.filter((e) => e.data?.baseData?.name === "telemetry_opted_out")
41+
const trackPosts = () => calls.filter((c) => c.url.includes("/v2/track"))
42+
43+
function writeMachineId() {
44+
mkdirSync(path.join(home, ".altimate"), { recursive: true })
45+
writeFileSync(path.join(home, ".altimate", "machine-id"), MACHINE)
46+
}
47+
function writeCreds() {
48+
mkdirSync(path.join(home, ".altimate"), { recursive: true })
49+
writeFileSync(
50+
path.join(home, ".altimate", "altimate.json"),
51+
JSON.stringify({ altimateUrl: API, altimateInstanceName: "acme", altimateApiKey: "key-1" }),
52+
)
53+
}
54+
55+
beforeEach(() => {
56+
home = mkdtempSync(path.join(os.tmpdir(), "telemetry-opt-out-"))
57+
for (const k of ENV) saved[k] = process.env[k]
58+
process.env.OPENCODE_TEST_HOME = home
59+
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING = CS
60+
delete process.env.ALTIMATE_TELEMETRY_DISABLED
61+
delete process.env.OPENCODE_DISABLE_TELEMETRY
62+
homedirSpy = spyOn(os, "homedir").mockImplementation(() => home)
63+
Telemetry.allowOptOutUnderTestRunner(true)
64+
calls = []
65+
trackStatus = 200
66+
fetchSpy = spyOn(globalThis, "fetch").mockImplementation((async (input: any, init?: RequestInit) => {
67+
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url
68+
calls.push({ url, body: String(init?.body ?? "") })
69+
if (url === `${API}/users/me`) {
70+
return new Response(JSON.stringify({ id: 7, uuid: UUID }), {
71+
status: 200,
72+
headers: { "content-type": "application/json" },
73+
})
74+
}
75+
if (url.includes("/v2/track")) return new Response("", { status: trackStatus })
76+
return new Response("", { status: 200 })
77+
}) as any)
78+
})
79+
80+
afterEach(async () => {
81+
await Telemetry.shutdown()
82+
Telemetry.allowOptOutUnderTestRunner(false)
83+
fetchSpy.mockRestore()
84+
homedirSpy.mockRestore()
85+
for (const k of ENV) {
86+
if (saved[k] === undefined) delete process.env[k]
87+
else process.env[k] = saved[k]
88+
}
89+
rmSync(home, { recursive: true, force: true })
90+
})
91+
92+
test("an opted-out machine sends one final event with its identity, then nothing", async () => {
93+
writeMachineId()
94+
writeCreds()
95+
expect(await TelemetryIdentity.resolve()).toEqual({ user_id: UUID, tenant: "acme" })
96+
const meCallsBefore = calls.filter((c) => c.url.endsWith("/users/me")).length
97+
98+
process.env.ALTIMATE_TELEMETRY_DISABLED = "1"
99+
await Telemetry.shutdown()
100+
await Telemetry.init()
101+
expect(await waitFor(() => optOuts().length > 0)).toBe(true)
102+
const [event] = optOuts()
103+
expect(event.data.baseData.properties.machine_id).toBe(MACHINE)
104+
expect(event.data.baseData.properties.opt_out_source).toBe("env")
105+
expect(event.data.baseData.properties.user_id).toBe(UUID)
106+
expect(event.data.baseData.properties.tenant).toBe("acme")
107+
expect(typeof event.data.baseData.properties.cli_version).toBe("string")
108+
expect(event.tags["ai.user.id"]).toBe(UUID)
109+
// Identity came from the cache: no request to resolve it while opted out.
110+
expect(calls.filter((c) => c.url.endsWith("/users/me")).length).toBe(meCallsBefore)
111+
expect(await waitFor(() => OptOutMarker.exists())).toBe(true)
112+
113+
// Nothing after it: tracked events are dropped, and a new init does not send again.
114+
const posts = trackPosts().length
115+
Telemetry.track({
116+
type: "startup_ready",
117+
session_id: "",
118+
command: "test",
119+
duration_ms: 1,
120+
fresh_install: false,
121+
timestamp: Date.now(),
122+
})
123+
await Telemetry.flush()
124+
await Telemetry.shutdown()
125+
await Telemetry.init()
126+
await new Promise((r) => setTimeout(r, 150))
127+
expect(trackPosts().length).toBe(posts)
128+
expect(optOuts()).toHaveLength(1)
129+
})
130+
131+
test("a machine that never sent telemetry sends nothing and gets no marker", async () => {
132+
process.env.ALTIMATE_TELEMETRY_DISABLED = "1"
133+
await Telemetry.shutdown()
134+
await Telemetry.init()
135+
await new Promise((r) => setTimeout(r, 150))
136+
expect(trackPosts()).toHaveLength(0)
137+
expect(OptOutMarker.exists()).toBe(false)
138+
expect(existsSync(path.join(home, ".altimate", "machine-id"))).toBe(false)
139+
})
140+
141+
test("the marker is written only once the sink accepted the event", async () => {
142+
writeMachineId()
143+
process.env.OPENCODE_DISABLE_TELEMETRY = "true"
144+
trackStatus = 500
145+
await Telemetry.shutdown()
146+
await Telemetry.init()
147+
expect(await waitFor(() => trackPosts().length === 1)).toBe(true)
148+
await new Promise((r) => setTimeout(r, 50))
149+
expect(OptOutMarker.exists()).toBe(false)
150+
151+
trackStatus = 200
152+
await Telemetry.shutdown()
153+
await Telemetry.init()
154+
expect(await waitFor(() => trackPosts().length === 2)).toBe(true)
155+
expect(await waitFor(() => OptOutMarker.exists())).toBe(true)
156+
expect(optOuts()).toHaveLength(2)
157+
expect(optOuts()[1].data.baseData.properties.user_id).toBeUndefined()
158+
expect(optOuts()[1].tags["ai.user.id"]).toBe(MACHINE)
159+
})
160+
161+
test("turning telemetry back on clears the marker", async () => {
162+
writeMachineId()
163+
OptOutMarker.write()
164+
expect(OptOutMarker.exists()).toBe(true)
165+
await Telemetry.shutdown()
166+
await Telemetry.init()
167+
expect(Telemetry.isEnabled()).toBe(true)
168+
expect(OptOutMarker.exists()).toBe(false)
169+
expect(optOuts()).toHaveLength(0)
170+
})
171+
})

‎packages/opencode/test/telemetry/automated-run.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ async function withEnv(env: Partial<Record<(typeof ENV_KEYS)[number], string>>,
3737
saved.set(key, process.env[key])
3838
delete process.env[key]
3939
}
40-
const homeKeys = ["HOME", "USERPROFILE"] as const
40+
// OPENCODE_TEST_HOME: Global.Path.home honours it, and Bun's os.homedir() does not follow a
41+
// changed HOME, so without it code keyed on Global.Path.home would read the real home.
42+
const homeKeys = ["HOME", "USERPROFILE", "OPENCODE_TEST_HOME"] as const
4143
for (const key of homeKeys) saved.set(key, process.env[key])
4244
const home = await mkdtemp(path.join(tmpdir(), "telemetry-home-"))
4345
for (const key of homeKeys) process.env[key] = home

0 commit comments

Comments
 (0)