Skip to content

Commit d40c562

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 6afa226 commit d40c562

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
@@ -1881,6 +1881,9 @@ export namespace Telemetry {
18811881
// disk by any path (manual file edit, the TUI, another thread's auth flow) is picked up within
18821882
// one flush interval instead of at the next process start.
18831883
let identityStamp = ""
1884+
// altimate_change — bumped by shutdown(); a detached opt-out send started under an earlier
1885+
// lifecycle neither posts nor writes its marker once a later init may have cleared it.
1886+
let lifecycleGeneration = 0
18841887
let machineId = ""
18851888
let sessionId = ""
18861889
let projectId = ""
@@ -2193,6 +2196,7 @@ export namespace Telemetry {
21932196
optOutUnderTestRunner = on
21942197
}
21952198
async function recordOptOut(source: "env" | "config"): Promise<void> {
2199+
const lifecycle = lifecycleGeneration
21962200
try {
21972201
if (underTestRunner() && !optOutUnderTestRunner) return
21982202
// The force flag is a developer knob for running telemetry under a test runner; the opt-out
@@ -2209,6 +2213,9 @@ export namespace Telemetry {
22092213
cfg,
22102214
{ machineId: id, identity: await TelemetryIdentity.cached() },
22112215
)
2216+
// A shutdown since this task started means a later init may own the marker now: neither
2217+
// send nor write, and let the next disabled init start over.
2218+
if (lifecycle !== lifecycleGeneration) return
22122219
const controller = new AbortController()
22132220
const timeout = setTimeout(() => controller.abort(), OPT_OUT_TIMEOUT_MS)
22142221
try {
@@ -2218,7 +2225,7 @@ export namespace Telemetry {
22182225
body: JSON.stringify(envelopes),
22192226
signal: controller.signal,
22202227
})
2221-
if (res.ok) OptOutMarker.write()
2228+
if (res.ok && lifecycle === lifecycleGeneration) OptOutMarker.write()
22222229
} finally {
22232230
clearTimeout(timeout)
22242231
}
@@ -2598,6 +2605,7 @@ export namespace Telemetry {
25982605
// previous account on events; doInit() resolves it again from whatever credential is on disk.
25992606
// The generation bump makes any resolution still in flight a no-op when it settles.
26002607
identityGeneration++
2608+
lifecycleGeneration++
26012609
identity = undefined
26022610
// NOTE: cachedLaunchId is deliberately NOT cleared here. session/prompt.ts shuts telemetry
26032611
// 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)