Skip to content

Commit 66c30e7

Browse files
fix(openrouter): publish endpoints on their own lookup sequence
The endpoint records (provider pricing and limits) were published only inside the branch that publishes a non-empty model catalog, so a catalog miss dropped a successful endpoint answer and getModel() fell back to the default info for that request. Endpoints now carry their own publication sequence, advanced only when a non-empty endpoint answer publishes: a catalog miss no longer discards them, and an older lookup can no longer overwrite endpoint records a newer one installed while both lookups returned an empty catalog. Tests (openrouter.spec.ts, fetchModel): - publishes endpoint records even when the model catalog came back empty - does not let an older endpoint answer replace a newer one when both catalogs are empty - keeps an older endpoint answer when a newer lookup publishes models without endpoints Negative controls (each restored byte-identically afterwards): - reverting to the nested publication fails all three new tests: "expected 8192 to be 444", "expected 8192 to be 222", "expected 555 to be 111". - dropping only the endpoint-sequence guard fails exactly the ordering test: "expected 111 to be 222". - publishing empty endpoint answers fails the pre-existing "keeps the previous endpoint records when a newer endpoint lookup returns none" ("expected 8192 to be 333") plus the third new test. - openrouter.spec.ts 109/109 green; src tsc --noEmit, eslint --max-warnings=0 and prettier --check clean; no new suppressions and no new "as any".
1 parent 31907b8 commit 66c30e7

2 files changed

Lines changed: 133 additions & 6 deletions

File tree

‎src/api/providers/__tests__/openrouter.spec.ts‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,124 @@ describe("OpenRouterHandler", () => {
415415
await handler.fetchModel()
416416
expect(handler.getModel().info.maxTokens).toBe(333)
417417
})
418+
419+
it("publishes endpoint records even when the model catalog came back empty", async () => {
420+
// A catalog miss must not discard endpoint data: the endpoint lookup carries the
421+
// pricing and limits for the selected provider, and getModel() prefers the endpoint
422+
// record over the (now missing) catalog entry.
423+
const { getModels } = await import("../fetchers/modelCache")
424+
vitest.mocked(getModels).mockResolvedValueOnce({})
425+
mockGetModelEndpoints.mockResolvedValueOnce({
426+
"provider-x": { ...openRouterDefaultModelInfo, maxTokens: 444 },
427+
})
428+
429+
const handler = new OpenRouterHandler(
430+
makeApiHandlerOptions({
431+
openRouterApiKey: "test-key",
432+
openRouterModelId: "anthropic/claude-sonnet-4",
433+
openRouterSpecificProvider: "provider-x",
434+
}),
435+
)
436+
437+
await handler.fetchModel()
438+
439+
expect(handler.getModel().info.maxTokens).toBe(444)
440+
})
441+
442+
it("does not let an older endpoint answer replace a newer one when both catalogs are empty", async () => {
443+
// With no models to publish, the model sequence never advances, so endpoint ordering
444+
// needs its own sequence: without it the slower, older lookup would overwrite the
445+
// endpoint records the newer lookup already installed.
446+
const { getModels } = await import("../fetchers/modelCache")
447+
const mockedGetModels = vitest.mocked(getModels)
448+
mockedGetModels.mockResolvedValueOnce({}).mockResolvedValueOnce({})
449+
450+
let releaseFirst!: () => void
451+
let releaseSecond!: () => void
452+
const firstGate = new Promise<void>((resolve) => {
453+
releaseFirst = resolve
454+
})
455+
const secondGate = new Promise<void>((resolve) => {
456+
releaseSecond = resolve
457+
})
458+
mockGetModelEndpoints
459+
.mockImplementationOnce(async () => {
460+
await firstGate
461+
return { "provider-x": { ...openRouterDefaultModelInfo, maxTokens: 111 } }
462+
})
463+
.mockImplementationOnce(async () => {
464+
await secondGate
465+
return { "provider-x": { ...openRouterDefaultModelInfo, maxTokens: 222 } }
466+
})
467+
468+
const handler = new OpenRouterHandler(
469+
makeApiHandlerOptions({
470+
openRouterApiKey: "test-key",
471+
openRouterModelId: "anthropic/claude-sonnet-4",
472+
openRouterSpecificProvider: "provider-x",
473+
}),
474+
)
475+
476+
const first = handler.fetchModel()
477+
const second = handler.fetchModel()
478+
479+
releaseSecond()
480+
await second
481+
releaseFirst()
482+
await first
483+
484+
expect(handler.getModel().info.maxTokens).toBe(222)
485+
})
486+
487+
it("keeps an older endpoint answer when a newer lookup publishes models without endpoints", async () => {
488+
// The endpoint sequence advances only when endpoints publish, so a newer lookup that
489+
// failed its endpoint fetch must not freeze the ordering: the older lookup's successful
490+
// endpoint records may still arrive afterwards and must be published.
491+
const { getModels } = await import("../fetchers/modelCache")
492+
const mockedGetModels = vitest.mocked(getModels)
493+
494+
let releaseFirst!: () => void
495+
let releaseSecond!: () => void
496+
const firstGate = new Promise<void>((resolve) => {
497+
releaseFirst = resolve
498+
})
499+
const secondGate = new Promise<void>((resolve) => {
500+
releaseSecond = resolve
501+
})
502+
mockedGetModels
503+
.mockImplementationOnce(async () => {
504+
await firstGate
505+
return {}
506+
})
507+
.mockImplementationOnce(async () => {
508+
await secondGate
509+
return { "anthropic/claude-sonnet-4": { ...openRouterDefaultModelInfo, maxTokens: 555 } }
510+
})
511+
mockGetModelEndpoints
512+
.mockImplementationOnce(async () => {
513+
await firstGate
514+
return { "provider-x": { ...openRouterDefaultModelInfo, maxTokens: 111 } }
515+
})
516+
.mockResolvedValueOnce({})
517+
518+
const handler = new OpenRouterHandler(
519+
makeApiHandlerOptions({
520+
openRouterApiKey: "test-key",
521+
openRouterModelId: "anthropic/claude-sonnet-4",
522+
openRouterSpecificProvider: "provider-x",
523+
}),
524+
)
525+
526+
const first = handler.fetchModel()
527+
const second = handler.fetchModel()
528+
529+
releaseSecond()
530+
await second
531+
releaseFirst()
532+
await first
533+
534+
expect(handler.getModel().info.maxTokens).toBe(111)
535+
})
418536
})
419537

420538
describe("applyOpenRouterMoonshotK3Profile", () => {

‎src/api/providers/openrouter.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
150150
// publishes when it is newer than what was published, so an older lookup that settles first is
151151
// not dropped just because a newer one has started.
152152
private publishedModelSequence = 0
153+
// Sequence of the newest lookup whose endpoint records were published. Endpoints keep their own
154+
// ordering because they publish even when the model catalog came back empty; sharing the model
155+
// sequence would let an older lookup overwrite newer endpoint records whenever both lookups
156+
// returned an empty catalog, since the model sequence only advances when models publish.
157+
private publishedEndpointSequence = 0
153158

154159
constructor(options: ApiHandlerOptions) {
155160
super()
@@ -675,12 +680,16 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH
675680
if (sequence > this.publishedModelSequence && Object.keys(models).length > 0) {
676681
this.publishedModelSequence = sequence
677682
this.models = models
678-
// The endpoint lookup answers an empty map when the request failed. Publishing that would
679-
// drop the endpoint records (pricing, limits) a previous successful lookup installed, so
680-
// only a non-empty answer replaces them.
681-
if (Object.keys(endpoints).length > 0) {
682-
this.endpoints = endpoints
683-
}
683+
}
684+
685+
// Endpoints are ordered by their own sequence for the same reason, and they publish even when
686+
// the model catalog came back empty: a catalog miss must not discard a successful endpoint
687+
// answer, whose records carry the pricing and limits for the selected provider. The endpoint
688+
// lookup answers an empty map when its request failed, and publishing that would drop the
689+
// records a previous successful lookup installed, so only a non-empty answer replaces them.
690+
if (sequence > this.publishedEndpointSequence && Object.keys(endpoints).length > 0) {
691+
this.publishedEndpointSequence = sequence
692+
this.endpoints = endpoints
684693
}
685694

686695
return this.getModel()

0 commit comments

Comments
 (0)