Skip to content

Commit c64a48c

Browse files
fix: reload once when overlapping register calls repair the same credential
Each register request notes the reload count before its pre-registration read. If a reload for the credential now on disk finished since then, it already covers that read, so overlapping repairs of a rejected, expired or logged-out credential reload once. A sequential restore still reloads. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_016x3nbZU5Vb6HnSTqE7vELm
1 parent 3a975b4 commit c64a48c

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎packages/opencode/src/server/server.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export namespace Server {
7878
let appliedBaseCredential: string | undefined
7979
// Serializes the register route's check-and-reload, so concurrent calls reload at most once.
8080
let baseReloadQueue: Promise<unknown> = Promise.resolve()
81+
// Bumped by every completed reload, so a request can tell that one ran after its own read.
82+
let baseReloadGeneration = 0
8183
// altimate_change end
8284

8385
export const Default = lazy(() => createApp({}))
@@ -785,6 +787,7 @@ export namespace Server {
785787

786788
// Read before registering: a credential that was rejected, expired or logged out and is
787789
// now reissued with identical fields still has to reload, and only this read sees that.
790+
const generationAtStart = baseReloadGeneration
788791
const before = await FreeTier.credentials().catch(() => undefined)
789792
const gate = FreeTierConsent.createRegistrationGate({
790793
register: () => FreeTier.register({ origin: "server" }),
@@ -830,8 +833,12 @@ export namespace Server {
830833
: undefined
831834
const reload = baseReloadQueue.then(async () => {
832835
const fingerprint = identity(await FreeTier.credentials().catch(() => undefined))
836+
// A reload that finished after this request's read, for the credential now on disk,
837+
// already covers whatever that read saw: overlapping repairs reload once.
838+
const coveredSinceRead = baseReloadGeneration !== generationAtStart
833839
const changed = identity(before) !== fingerprint
834-
if (!changed && fingerprint !== undefined && fingerprint === appliedBaseCredential) return true
840+
const applied = fingerprint !== undefined && fingerprint === appliedBaseCredential
841+
if (applied && (!changed || coveredSinceRead)) return true
835842
const disposed = await Promise.all([
836843
Instance.disposeAll().then(
837844
() => true,
@@ -848,7 +855,10 @@ export namespace Server {
848855
},
849856
),
850857
]).then((results) => results.every(Boolean))
851-
if (disposed) appliedBaseCredential = fingerprint
858+
if (disposed) {
859+
appliedBaseCredential = fingerprint
860+
baseReloadGeneration++
861+
}
852862
return disposed
853863
})
854864
baseReloadQueue = reload.catch(() => undefined)

‎packages/opencode/test/server/altimate-base-registration.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,37 @@ describe("Altimate Base registration route", () => {
280280
}
281281
})
282282

283+
test("two overlapping repairs of a rejected credential reload once", async () => {
284+
// Both requests read the rejected credential before either registers, then share one repair.
285+
const good = { apiKey: "sk-overlap", baseURL: "https://gateway.test", installSecret: "s" }
286+
const rejected = { ...good, rejected: true }
287+
mockCredentialsSequence(rejected, rejected, good)
288+
let entered = 0
289+
let bothEntered!: () => void
290+
const overlap = new Promise<void>((resolve) => {
291+
bothEntered = resolve
292+
})
293+
mockRegister(async () => {
294+
if (++entered === 2) bothEntered()
295+
await overlap
296+
return good
297+
})
298+
const disposeAll = spyOn(Instance, "disposeAll")
299+
const post = () =>
300+
app().request("/altimate/base/register", {
301+
method: "POST",
302+
headers: { "content-type": "application/json" },
303+
body: JSON.stringify({}),
304+
})
305+
try {
306+
const bodies = await Promise.all([post(), post()].map(async (r) => (await r).json()))
307+
for (const body of bodies) expect(body).toMatchObject({ ok: true })
308+
expect(disposeAll).toHaveBeenCalledTimes(1)
309+
} finally {
310+
disposeAll.mockRestore()
311+
}
312+
})
313+
283314
test("reloads when a rejected credential is restored with identical fields", async () => {
284315
// Directories opened while the credential was rejected cached no Base; clearing the flag can
285316
// reissue exactly the credential this process reloaded for before it was rejected.

0 commit comments

Comments
 (0)