Skip to content

Commit cbc3a6d

Browse files
fix: a credential change during the retry backoff is noticed at once
The backoff throttles retries of the unchanged failed credential only; the stamp comparison keeps running, so a different credential appearing in the window resolves at the next flush. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent 6be240e commit cbc3a6d

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,10 +1865,11 @@ export namespace Telemetry {
18651865
// one flush interval instead of at the next process start.
18661866
let identityStamp = ""
18671867
// altimate_change — a resolution that failed while a credential exists (backend down, timeout)
1868-
// must be tried again, but not on every 5 s flush: the stamp is reset so the next flush after
1869-
// this time re-resolves.
1868+
// is tried again at the first flush after this time, not on every 5 s flush. The stamp of the
1869+
// failed credential is kept, so a different credential appearing in the meantime is noticed at
1870+
// once by the stamp comparison; the backoff only throttles retries of the unchanged one.
18701871
const IDENTITY_RETRY_MS = 60_000
1871-
let identityRetryAt = 0
1872+
let identityRetryAt = 0 // 0: no retry pending
18721873
let machineId = ""
18731874
let sessionId = ""
18741875
let projectId = ""
@@ -2141,16 +2142,14 @@ export namespace Telemetry {
21412142
function resolveIdentity(opts: { refresh?: boolean } = {}): Promise<void> {
21422143
const generation = ++identityGeneration
21432144
identityStamp = TelemetryIdentity.credentialStamp()
2145+
identityRetryAt = 0
21442146
return TelemetryIdentity.resolve(opts)
21452147
.then(async (resolved) => {
21462148
if (generation !== identityGeneration) return
21472149
identity = resolved
21482150
if (resolved === undefined && (await TelemetryIdentity.hasCredential())) {
21492151
// Signed in but unresolved: a transient failure. Retry at a later flush.
2150-
if (generation === identityGeneration) {
2151-
identityStamp = ""
2152-
identityRetryAt = Date.now() + IDENTITY_RETRY_MS
2153-
}
2152+
if (generation === identityGeneration) identityRetryAt = Date.now() + IDENTITY_RETRY_MS
21542153
}
21552154
})
21562155
.catch(() => {})
@@ -2170,9 +2169,9 @@ export namespace Telemetry {
21702169
return identity
21712170
}
21722171

2173-
/** Test seam: end the retry backoff so the next flush re-resolves. */
2172+
/** Test seam: end a pending retry backoff so the next flush re-resolves. */
21742173
export function retryIdentityNowForTest() {
2175-
identityRetryAt = 0
2174+
if (identityRetryAt !== 0) identityRetryAt = 1
21762175
}
21772176
// altimate_change end
21782177

@@ -2372,9 +2371,10 @@ export namespace Telemetry {
23722371
// change on disk and re-resolve (cheap: two stats, and the cache answers unless the key changed).
23732372
const who = identity
23742373
const mid = machineId
2375-
if (enabled && Date.now() >= identityRetryAt) {
2374+
if (enabled) {
23762375
const stamp = TelemetryIdentity.credentialStamp()
2377-
if (stamp !== identityStamp) {
2376+
const retryDue = identityRetryAt !== 0 && Date.now() >= identityRetryAt
2377+
if (stamp !== identityStamp || retryDue) {
23782378
identity = undefined
23792379
void resolveIdentity()
23802380
}

‎packages/opencode/test/altimate/telemetry/identity.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,28 @@ describe("telemetry identity", () => {
268268
await new Promise((r) => setTimeout(r, 30))
269269
expect(meCalls().length).toBe(after) // backoff: no retry within the window
270270

271+
// A different credential appearing during the backoff is picked up at once.
272+
meStatus = 200
273+
await new Promise((r) => setTimeout(r, 15))
274+
writeCreds("key-B-longer-so-the-size-differs-too")
275+
await Telemetry.flush()
276+
const gotB = await (async () => {
277+
const until = Date.now() + 2000
278+
while (Date.now() < until) {
279+
if (Telemetry.identityForTest()?.user_id === UUID_B) return true
280+
await new Promise((r) => setTimeout(r, 20))
281+
}
282+
return false
283+
})()
284+
expect(gotB).toBe(true)
285+
286+
// And the unchanged credential is retried once the backoff has elapsed.
287+
await new Promise((r) => setTimeout(r, 15))
288+
writeCreds("key-A")
289+
meStatus = 503
290+
await Telemetry.flush()
291+
await new Promise((r) => setTimeout(r, 60))
292+
expect(Telemetry.identityForTest()).toBeUndefined()
271293
meStatus = 200
272294
Telemetry.retryIdentityNowForTest()
273295
await Telemetry.flush()

0 commit comments

Comments
 (0)