Skip to content

Commit 6e425e4

Browse files
fix: a stale opt-out send never posts or writes the marker after a shutdown
The send carries the lifecycle it started in; shutdown() bumps it, so a task still pending when telemetry is re-enabled neither posts nor recreates the marker. Marker removal is best effort, and the suite drops the force flag. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent 3032626 commit 6e425e4

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,9 @@ export namespace Telemetry {
18761876
// resolution started under credential A must not land after a sign-in installed B, nor after
18771877
// shutdown() cleared the state. shutdown() and every new resolution bump it.
18781878
let identityGeneration = 0
1879+
// altimate_change — bumped by shutdown(); a detached opt-out send started under an earlier
1880+
// lifecycle neither posts nor writes its marker once a later init may have cleared it.
1881+
let lifecycleGeneration = 0
18791882
let machineId = ""
18801883
let sessionId = ""
18811884
let projectId = ""
@@ -2183,6 +2186,7 @@ export namespace Telemetry {
21832186
optOutUnderTestRunner = on
21842187
}
21852188
async function recordOptOut(source: "env" | "config"): Promise<void> {
2189+
const lifecycle = lifecycleGeneration
21862190
try {
21872191
if (underTestRunner() && !optOutUnderTestRunner) return
21882192
// The force flag is a developer knob for running telemetry under a test runner; the opt-out
@@ -2199,6 +2203,9 @@ export namespace Telemetry {
21992203
cfg,
22002204
{ machineId: id, identity: await TelemetryIdentity.cached() },
22012205
)
2206+
// A shutdown since this task started means a later init may own the marker now: neither
2207+
// send nor write, and let the next disabled init start over.
2208+
if (lifecycle !== lifecycleGeneration) return
22022209
const controller = new AbortController()
22032210
const timeout = setTimeout(() => controller.abort(), OPT_OUT_TIMEOUT_MS)
22042211
try {
@@ -2208,7 +2215,7 @@ export namespace Telemetry {
22082215
body: JSON.stringify(envelopes),
22092216
signal: controller.signal,
22102217
})
2211-
if (res.ok) OptOutMarker.write()
2218+
if (res.ok && lifecycle === lifecycleGeneration) OptOutMarker.write()
22122219
} finally {
22132220
clearTimeout(timeout)
22142221
}
@@ -2575,6 +2582,7 @@ export namespace Telemetry {
25752582
// previous account on events; doInit() resolves it again from whatever credential is on disk.
25762583
// The generation bump makes any resolution still in flight a no-op when it settles.
25772584
identityGeneration++
2585+
lifecycleGeneration++
25782586
identity = undefined
25792587
// NOTE: cachedLaunchId is deliberately NOT cleared here. session/prompt.ts shuts telemetry
25802588
// down at the end of every session, so clearing it would mint a fresh launch_id per prompt in

‎packages/opencode/src/altimate/telemetry/opt-out.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ export namespace OptOutMarker {
4040
fs.writeFileSync(markerPath(), `${new Date().toISOString()}\n`, { mode: 0o600 })
4141
}
4242

43+
/** Best effort: an unremovable marker (read-only directory) must never fail an init. */
4344
export function clear(): void {
44-
fs.rmSync(markerPath(), { force: true })
45+
try {
46+
fs.rmSync(markerPath(), { force: true })
47+
} catch {
48+
// leave it; the next opt-out is then not recorded again, which is the lesser failure
49+
}
4550
}
4651
}

‎packages/opencode/test/altimate/telemetry/opt-out.test.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ describe("telemetry opt-out marker", () => {
2323
let home: string
2424
let calls: Call[]
2525
let trackStatus = 200
26+
/** When set, the next /v2/track POST waits on it before answering. */
27+
let holdTrack: Promise<void> | undefined
2628
let fetchSpy: ReturnType<typeof spyOn>
2729
let homedirSpy: ReturnType<typeof spyOn>
2830
const saved: Record<string, string | undefined> = {}
@@ -31,6 +33,7 @@ describe("telemetry opt-out marker", () => {
3133
"APPLICATIONINSIGHTS_CONNECTION_STRING",
3234
"ALTIMATE_TELEMETRY_DISABLED",
3335
"OPENCODE_DISABLE_TELEMETRY",
36+
"ALTIMATE_TELEMETRY_FORCE",
3437
]
3538

3639
const optOuts = () =>
@@ -59,10 +62,12 @@ describe("telemetry opt-out marker", () => {
5962
process.env.APPLICATIONINSIGHTS_CONNECTION_STRING = CS
6063
delete process.env.ALTIMATE_TELEMETRY_DISABLED
6164
delete process.env.OPENCODE_DISABLE_TELEMETRY
65+
delete process.env.ALTIMATE_TELEMETRY_FORCE
6266
homedirSpy = spyOn(os, "homedir").mockImplementation(() => home)
6367
Telemetry.allowOptOutUnderTestRunner(true)
6468
calls = []
6569
trackStatus = 200
70+
holdTrack = undefined
6671
fetchSpy = spyOn(globalThis, "fetch").mockImplementation((async (input: any, init?: RequestInit) => {
6772
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url
6873
calls.push({ url, body: String(init?.body ?? "") })
@@ -72,7 +77,10 @@ describe("telemetry opt-out marker", () => {
7277
headers: { "content-type": "application/json" },
7378
})
7479
}
75-
if (url.includes("/v2/track")) return new Response("", { status: trackStatus })
80+
if (url.includes("/v2/track")) {
81+
if (holdTrack) await holdTrack
82+
return new Response("", { status: trackStatus })
83+
}
7684
return new Response("", { status: 200 })
7785
}) as any)
7886
})
@@ -169,6 +177,32 @@ describe("telemetry opt-out marker", () => {
169177
expect(optOuts()[1].tags["ai.user.id"]).toBe(MACHINE)
170178
})
171179

180+
test("a send still pending when telemetry is re-enabled never recreates the marker", async () => {
181+
writeMachineId()
182+
let release: () => void = () => {}
183+
holdTrack = new Promise<void>((r) => (release = r))
184+
process.env.ALTIMATE_TELEMETRY_DISABLED = "1"
185+
await Telemetry.shutdown()
186+
await Telemetry.init()
187+
expect(await waitFor(() => trackPosts().length === 1)).toBe(true) // the POST is in flight
188+
189+
delete process.env.ALTIMATE_TELEMETRY_DISABLED
190+
await Telemetry.shutdown()
191+
await Telemetry.init() // telemetry on again; this lifecycle owns the marker now
192+
expect(Telemetry.isEnabled()).toBe(true)
193+
release()
194+
await new Promise((r) => setTimeout(r, 100))
195+
expect(OptOutMarker.exists()).toBe(false)
196+
197+
// Opting out again is recorded afresh.
198+
holdTrack = undefined
199+
process.env.ALTIMATE_TELEMETRY_DISABLED = "1"
200+
await Telemetry.shutdown()
201+
await Telemetry.init()
202+
expect(await waitFor(() => optOuts().length === 2)).toBe(true)
203+
expect(await waitFor(() => OptOutMarker.exists())).toBe(true)
204+
})
205+
172206
test("turning telemetry back on clears the marker", async () => {
173207
writeMachineId()
174208
OptOutMarker.write()

0 commit comments

Comments
 (0)