Skip to content

Commit fbb0e7a

Browse files
fix: an opt-out send accepted across a shutdown keeps its marker
The marker write keys on a generation that only an enabled init bumps, so a shutdown while still opted out no longer leaves the accepted event unmarked and sent again. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01YNasdD5tQMNtzXsuMEy82X
1 parent c523841 commit fbb0e7a

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,9 +1903,12 @@ export namespace Telemetry {
19031903
// once by the stamp comparison; the backoff only throttles retries of the unchanged one.
19041904
const IDENTITY_RETRY_MS = 60_000
19051905
let identityRetryAt = 0 // 0: no retry pending
1906-
// altimate_change — bumped by shutdown(); a detached opt-out send started under an earlier
1907-
// lifecycle neither posts nor writes its marker once a later init may have cleared it.
1906+
// altimate_change start — bumped by shutdown(); a detached opt-out send started under an earlier
1907+
// lifecycle does not post. Its marker write keys on markerGeneration instead, which only an
1908+
// enabled init bumps: a send accepted across a shutdown while still opted out keeps its marker.
19081909
let lifecycleGeneration = 0
1910+
let markerGeneration = 0
1911+
// altimate_change end
19091912
let machineId = ""
19101913
let sessionId = ""
19111914
let projectId = ""
@@ -2151,7 +2154,8 @@ export namespace Telemetry {
21512154
machineId = getOrCreateMachineId()
21522155
enabled = true
21532156
// altimate_change — telemetry is on: forget the opt-out marker, so turning it off again is
2154-
// recorded again.
2157+
// recorded again. The bump first, so a send still in flight never recreates it.
2158+
markerGeneration++
21552159
OptOutMarker.clear()
21562160
log.info("telemetry initialized", { mode: "appinsights" })
21572161
// altimate_change — clear any existing interval before installing a new one. doInit() can
@@ -2230,6 +2234,7 @@ export namespace Telemetry {
22302234
}
22312235
async function recordOptOut(source: "env" | "config"): Promise<void> {
22322236
const lifecycle = lifecycleGeneration
2237+
const marker = markerGeneration
22332238
try {
22342239
if (underTestRunner() && !optOutUnderTestRunner) return
22352240
// The force flag is a developer knob for running telemetry under a test runner; the opt-out
@@ -2246,8 +2251,8 @@ export namespace Telemetry {
22462251
cfg,
22472252
{ machineId: id, identity: await TelemetryIdentity.cached() },
22482253
)
2249-
// A shutdown since this task started means a later init may own the marker now: neither
2250-
// send nor write, and let the next disabled init start over.
2254+
// A shutdown since this task started means a later init may own the marker now: do not
2255+
// send, and let the next disabled init start over.
22512256
if (lifecycle !== lifecycleGeneration) return
22522257
const controller = new AbortController()
22532258
const timeout = setTimeout(() => controller.abort(), OPT_OUT_TIMEOUT_MS)
@@ -2258,7 +2263,8 @@ export namespace Telemetry {
22582263
body: JSON.stringify(envelopes),
22592264
signal: controller.signal,
22602265
})
2261-
if (res.ok && lifecycle === lifecycleGeneration) OptOutMarker.write()
2266+
// Accepted: write the marker unless telemetry was turned back on meanwhile.
2267+
if (res.ok && marker === markerGeneration) OptOutMarker.write()
22622268
} finally {
22632269
clearTimeout(timeout)
22642270
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,25 @@ describe("telemetry opt-out marker", () => {
204204
expect(await waitFor(() => OptOutMarker.exists())).toBe(true)
205205
})
206206

207+
test("a send accepted after a shutdown still writes the marker, so the next opted-out init sends nothing", async () => {
208+
writeMachineId()
209+
let release: () => void = () => {}
210+
holdTrack = new Promise<void>((r) => (release = r))
211+
process.env.ALTIMATE_TELEMETRY_DISABLED = "1"
212+
await Telemetry.shutdown()
213+
await Telemetry.init()
214+
expect(await waitFor(() => trackPosts().length === 1)).toBe(true) // the POST is in flight
215+
216+
await Telemetry.shutdown() // still opted out: nothing has cleared the marker
217+
release()
218+
expect(await waitFor(() => OptOutMarker.exists())).toBe(true)
219+
220+
holdTrack = undefined
221+
await Telemetry.init()
222+
await new Promise((r) => setTimeout(r, 100))
223+
expect(optOuts()).toHaveLength(1)
224+
})
225+
207226
test("turning telemetry back on clears the marker", async () => {
208227
writeMachineId()
209228
OptOutMarker.write()

0 commit comments

Comments
 (0)