* provider: a multi-protocol provider's catalog is the union of every endpoint's models A provider whose protocols sit at bases of their own kept only the first list that answered, so the models of every other endpoint were nowhere in the product: not in the agents' model lists, not in the catalog the routing and the per-model preferences read, and not fixable by pinning a model's protocol, which setModelAPI refuses for a model the provider does not list. nyownm reported it as #904 and worked around it with one provider per protocol. fetchOne now asks every endpoint the provider speaks and merges the lists by model id, the way fetchPerKey merges the lists of a vendor's keys: the first list's fields stand, and an image capability both lists answer is merged. The base kept is still the first protocol's, the one the drawer list is fetched beside and the models are saved with. A models URL is still asked alone, one base two protocols share is still asked once, and a provider with no endpoint answering still fails with every URL it asked named in the error. Verified with go test -tags nogui ./internal/provider ./internal/catalog and ./internal/gateway ./internal/agent, all green. TestFetchUnionsProtocolLists and TestFetchUnionsProtocolListsWithoutChat fail on the base commit with the single list the first protocol answered. * provider: an endpoint that can't be asked keeps the models it listed A provider whose protocols sit at bases of their own is asked every base and the lists are merged, so one read that failed saved the union of the rest as the whole list: the models of the failed base read as gone, dropGonePicks took them out of the user's picks and stored that, and nothing brought them back when the endpoint recovered (yetone's review of #1006, on nyownm's #904). A read that failed says nothing about what the vendor serves today, the rule fetchPerKey's keys already follow. fetchOne keeps what each base listed and saves it beside the merged list, in a new "sides" part of the catalog's live file, and a base it could not ask takes its own part back for that fetch. What is kept is that base's own models alone: a model the bases that answered have dropped is dropped here too, however many of them dropped it and however many bases are down. Keeping the whole list saved before would bring such a model back, which is the same fault the other way round. A list saved before this kept no parts, so a base whose part is missing keeps what no answering base lists, until the next fetch with every base answering says which they are. The parts are kept only where more than one base was asked: a single one that fails is the fetch that fails, and its list is kept as it is, so a provider of one protocol's cache is the file it was. fetchOne answers one fetched list now — the models, the base kept for routing, the parts — rather than three values. fetchPerKey marks the parts with the keys that saw them, and keeps a plan's own models out of them. Verified with go test ./internal/provider ./internal/catalog, all green, and ./internal/gateway ./internal/agent. TestFetchKeepsTheModelsAFailedProtocolListed, TestFetchDropsAGoneModelWhileAnotherProtocolFails and TestFetchKeepsTheModelsAFailedProtocolListedPerKey fail on 952d0b00 with the model of the failed protocol taken out of the picks. Co-authored-by: multica-agent <github@multica.ai> * provider: a base that never answered keeps nothing, and is not saved as one that listed nothing keepDeadSides read a part missing from the saved sides as the whole list saved before, and kept what no answering base lists from it. Two things were wrong with that, and both were reported on #1006 (nyownm's #904): A base in none of the saved parts is one that has never answered. The whole list is not its own, so a model its vendor had already dropped was kept for it, and the keepDeadSides result was saved as sides[base] — from then on that base had a part, and the model could never be dropped again. An Anthropic base answering 404 from the start of a provider's life was enough: one round after the Chat base dropped a model, that model stayed in the catalog, and dropGonePicks could not take it out of the user's picks. The saving is the other half of the same fault. A part is what a base listed, so one that never answered has none, and writing an empty one fixes the reading of "never answered" as "listed nothing" for good. Only a part read out of the file is written back; the list saved without any parts at all — before this kept them — is the one case where a base may stand in for a missing part, and what it stands in for is kept for that fetch alone, never saved as its own. So keepDeadSides answers two lists now: the models this fetch's list is made of, and the parts to write beside it. fetchPerKey keeps a model a key that could not be asked at all owns to the bases that listed it, read out of the parts saved beside the list: kept as a whole list with no part saying where it came from it belonged to no base, and was read as gone the first time that key answered half-way — the same permanent deletion of a picked model, one round later and needing no new base. Verified with go test ./internal/provider ./internal/catalog ./internal/gateway ./internal/agent, all green, and -race -count=3 over ./internal/provider with the four failures it already had. TestFetchDropsAModelWhileAnEndpointHasNeverAnswered and TestFetchKeepsAModelsBaseAfterItsKeyFailedWhole fail on 6621f73e. Co-authored-by: multica-agent <github@multica.ai> * provider: a base that answered without a model does not keep owning it fetchPerKey gave every model kept for a key that could not be asked at all to the bases that listed it last time, and a part there said what the bases listed when one of them had answered this round without it (yetone's review of #1006, nyownm's #904). With two keys where both were served a Claude model, and the Anthropic base stopping both of them in the round the second key was down, sides[anthropic] kept a model the base had plainly dropped, and the fetch after that read it as a base that couldn't be asked and held the model one round longer than it should have. It is self-healing — the next round with the base answering settles it — and it is the same fault c8fabd70 was written to undo, read from the other side. sides may only say what the bases listed. A model kept for a failed key belongs to a base that was not asked this round for any of the model's own keys: one that was, and answered without the model, has said what it serves today and no longer owns it. Which is per key, not per provider: fetchOne answers which of its bases replied, which a model of the second key alone never saw an answer from, so the base's saying nothing about it for the first key leaves it that base's. The keys a model is kept under are read from the list saved before, since add clears the ones of the model it is adding. TestFetchDropsAModelABaseStoppedServingWhileAKeyIsDown fails on c8fabd70 with the part holding claude-b, and TestFetchKeepsAModelsBaseAfterItsKeyFailedWhole goes on passing: the same two keys, a model only one of them sees. Co-authored-by: multica-agent <github@multica.ai> * provider, catalog: a page of a model list is not the list (#904) A /v1/models that answers with has_more is one page of the vendor's catalog. Read as the whole list, every model of a later page reads as one the vendor has dropped, and the user's pick of it goes for good, on a list that never said so. Such a reply is no answer: the base is treated as one that could not be asked, which keeps what it listed last time (#904's review of #1006). OpenRouter's Anthropic base is not asked for the list at all. With the Anthropic version header its /models answers with the catalog again, twenty newest first, every id namespaced under `anthropic/`, and the cursors of the pages after. None of those twenty is an id in OpenRouter's own catalog, so merged they are entries of the model picker that name no model: the long, cluttered list #904's reporter wrote of. Its Chat base is that catalog, whole, and the fetch is one request where it was two. The rule is OpenRouter's, by host, as the ones after it are scoped to their vendors. Co-authored-by: multica-agent <github@multica.ai> * provider: the OpenRouter rule reads the base being asked (0fb16990) It read the Chat base's host, so a provider with a Chat base of OpenRouter's and an Anthropic base of another vendor's had that other base skipped: its models were in neither the union nor the sides nor the bases that could not be asked, so no keepDeadSides path found them and the user's picks of them went for good. `magpie provider set <id> anthropic=<url>` reaches that shape from the CLI. The rule is about the vendor whose base is being asked, so that is what it now reads, together with whether the provider asks at a base of its own besides it: an OpenRouter Anthropic base alone is still asked, its reply being the only catalog there would otherwise be, and catalog.fetchOne is what keeps a page of it from being read as the whole. Five combinations of the two bases and openrouter.ai, one assertion each; three of them fail on 0fb16990. Co-authored-by: multica-agent <github@multica.ai> * catalog, provider: a model list is followed to its last page, not read as a page (#1006) A reply with has_more was refused as no answer at all, which is right for a provider with several bases and wrong for one with a single base: Anthropic's own preset has no other base to fall back on, so once its list passes twenty models — about sixteen today — the fetch answered an error and no models, where main listed the twenty of the first page, and a pick of a model on a later page went with them. catalog.fetchOne now reads a list to its end: the first page as the URL stands, and every page after it with limit=1000 and the after_id the page before ended at, up to twenty pages. Half a list is still no answer, and that is what the following ends on: a page that fails, a list that pages without naming the id to carry on from, one that answers the page just asked again under the same id, and one still paging at the cap all leave the base as one that could not be asked, which keeps what it listed last time. The rule lives in catalog.Paged, and the Kilo gateway's list — OpenRouter's shape, which pages the same way — is paged by it instead of refused by a guard of its own; the comment on that guard claimed OpenRouter's /models pages, which neither api.kilo.ai nor openrouter.ai was answering with on 2026-10-06, and now says so. One assertion of TestFetchOpenRouterRuleOverTheFourBaseCombinations follows the rule rather than the old count: OpenRouter's Anthropic base alone is asked for its first page and the page after it, which is the same page again, where before it was asked once and refused. Tests: TestFetchFollowsThePagesOfAList, TestFetchFollowsAPageAListAnswersItsOwnWay, TestFetchTakesAPagedListAsNoAnswerWhenAPageFails, TestFetchTakesAPagedListWithoutAnIdAsNoAnswer, TestFetchTakesAListThatPagesWithoutEndAsNoAnswer (catalog); TestFetchFollowsThePagesOfASingleAnthropicBase (provider, the Anthropic preset's one base, 25 models 20 to a page) and TestKiloModelsFollowThePagesOfTheList. Co-authored-by: multica-agent <github@multica.ai> * provider: OpenRouter's Anthropic base alone is asked, and refused (#1006) The fake that stands for openrouter.ai answered every cursor with the same twenty ids under the same last_id, so following the pages of a list stopped where the rule of catalog.Paged says a list stops being readable, and the single-base case looked covered: the twenty were refused and the fetch failed. The endpoint does not answer that way. Asked with the Anthropic version header on 2026-10-06, /api/v1/models gave those twenty with has_more and a cursor, and under the cursor 413 more ids with has_more false — a different set, and the end of the list. The two pages share nothing, and following them lands deeper in the `anthropic/` namespace rather than back at the catalog it was namespaced from: of the 433 ids, 4 are in the 466 OpenRouter serves without that header. So the twenty were not a page to be refused but the first half of a list that reads to the end, and the end is still not a catalog of models to serve. The base is still asked — it is the only base there is, and refusing to ask would leave the provider with nothing at all — and the list it answers with is now refused as a whole, which is what the old rule did with one page of it. It fails the fetch like any other base that could not be asked, so the provider keeps the models it listed last time (#904). The two replies are kept as they came, under testdata, and a test reads them back: twenty, then 413, the cursor the first ends at, has_more true and then false, and not one id of either page in the catalog OpenRouter serves. The fake pages with after_id, as the endpoint does. Tests: TestOpenRouterNamespacedPagesAreTheEndpointShape (new), TestFetchOpenRouterRuleOverTheFourBaseCombinations (the single-base case now asks exactly the two pages it answers with, keeps no list, and fails), TestFetchKeepsPicksWhenAModelListIsPaginated, TestFetchDoesNotMergeOpenRouter's NamespacedList, TestFetchOpenRouterAddsNoNamespacedModelAndKeepsPicks, TestFetchKeepsAnotherVendorsAnthropicList. Co-authored-by: multica-agent <github@multica.ai> * provider: the message for a namespaced list counts nothing (#1006) The reason a provider whose only base is OpenRouter's Anthropic one is refused is still the one it was, and the base is still asked and refused exactly as before: what changed is the sentence the user is left with, and the comments around it. That sentence counted the list it refused — "433 of them, of which 4 are ids OpenRouter serves" — and those counts are of whatever OpenRouter's catalog was that day, so the number a user reads is wrong tomorrow without anyone touching it. It now says why the answer is no good and what to do instead: give this provider OpenRouter's Chat base, which lists those models under the ids OpenRouter serves, or type the model ids in by hand, which is what the caller already appends to every base it could not ask. The comments said the same counts as fact, and cited a probe file that is not in the repository. The counts stay, marked as what was observed on 2026-10-06, and what does not move is now the point: following the pages of the list lands deeper in the namespace, not back at the catalog it was namespaced from. The dead file references are gone, and one test checks that the message counts nothing at all, so a count cannot creep back in. Tests: TestFetchOpenRouterRuleOverTheFourBaseCombinations (the message names the Chat base and the ids to type, and holds no number but the version in that base's path). Co-authored-by: multica-agent <github@multica.ai> * provider: a multi-protocol provider's catalog is the union of every endpoint's models A provider whose protocols sit at bases of their own kept only the first list that answered, so the models of every other endpoint were nowhere in the product: not in the agents' model lists, not in the catalog the routing and the per-model preferences read, and not fixable by pinning a model's protocol, which setModelAPI refuses for a model the provider does not list. nyownm reported it as #904 and worked around it with one provider per protocol. fetchOne now asks every endpoint the provider speaks and merges the lists by model id, the way fetchPerKey merges the lists of a vendor's keys: the first list's fields stand, and an image capability both lists answer is merged. The base kept is still the first protocol's, the one the drawer list is fetched beside and the models are saved with. A models URL is still asked alone, one base two protocols share is still asked once, and a provider with no endpoint answering still fails with every URL it asked named in the error. Verified with go test -tags nogui ./internal/provider ./internal/catalog and ./internal/gateway ./internal/agent, all green. TestFetchUnionsProtocolLists and TestFetchUnionsProtocolListsWithoutChat fail on the base commit with the single list the first protocol answered. Co-authored-by: multica-agent <github@multica.ai> * provider: an endpoint that can't be asked keeps the models it listed A provider whose protocols sit at bases of their own is asked every base and the lists are merged, so one read that failed saved the union of the rest as the whole list: the models of the failed base read as gone, dropGonePicks took them out of the user's picks and stored that, and nothing brought them back when the endpoint recovered (yetone's review of vendor serves today, the rule fetchPerKey's keys already follow. fetchOne keeps what each base listed and saves it beside the merged list, in a new "sides" part of the catalog's live file, and a base it could not ask takes its own part back for that fetch. What is kept is that base's own models alone: a model the bases that answered have dropped is dropped here too, however many of them dropped it and however many bases are down. Keeping the whole list saved before would bring such a model back, which is the same fault the other way round. A list saved before this kept no parts, so a base whose part is missing keeps what no answering base lists, until the next fetch with every base answering says which they are. The parts are kept only where more than one base was asked: a single one that fails is the fetch that fails, and its list is kept as it is, so a provider of one protocol's cache is the file it was. fetchOne answers one fetched list now — the models, the base kept for routing, the parts — rather than three values. fetchPerKey marks the parts with the keys that saw them, and keeps a plan's own models out of them. Verified with go test ./internal/provider ./internal/catalog, all green, and ./internal/gateway ./internal/agent. TestFetchKeepsTheModelsAFailedProtocolListed, TestFetchDropsAGoneModelWhileAnotherProtocolFails and TestFetchKeepsTheModelsAFailedProtocolListedPerKey fail on 952d0b00 with the model of the failed protocol taken out of the picks. Co-authored-by: multica-agent <github@multica.ai> * provider: a base that never answered keeps nothing, and is not saved as one that listed nothing keepDeadSides read a part missing from the saved sides as the whole list saved before, and kept what no answering base lists from it. Two things were wrong with that, and both were reported on #1006 (nyownm's #904): A base in none of the saved parts is one that has never answered. The whole list is not its own, so a model its vendor had already dropped was kept for it, and the keepDeadSides result was saved as sides[base] — from then on that base had a part, and the model could never be dropped again. An Anthropic base answering 404 from the start of a provider's life was enough: one round after the Chat base dropped a model, that model stayed in the catalog, and dropGonePicks could not take it out of the user's picks. The saving is the other half of the same fault. A part is what a base listed, so one that never answered has none, and writing an empty one fixes the reading of "never answered" as "listed nothing" for good. Only a part read out of the file is written back; the list saved without any parts at all — before this kept them — is the one case where a base may stand in for a missing part, and what it stands in for is kept for that fetch alone, never saved as its own. So keepDeadSides answers two lists now: the models this fetch's list is made of, and the parts to write beside it. fetchPerKey keeps a model a key that could not be asked at all owns to the bases that listed it, read out of the parts saved beside the list: kept as a whole list with no part saying where it came from it belonged to no base, and was read as gone the first time that key answered half-way — the same permanent deletion of a picked model, one round later and needing no new base. Verified with go test ./internal/provider ./internal/catalog ./internal/gateway ./internal/agent, all green, and -race -count=3 over ./internal/provider with the four failures it already had. TestFetchDropsAModelWhileAnEndpointHasNeverAnswered and TestFetchKeepsAModelsBaseAfterItsKeyFailedWhole fail on 6621f73e. Co-authored-by: multica-agent <github@multica.ai> * provider: a base that answered without a model does not keep owning it fetchPerKey gave every model kept for a key that could not be asked at all to the bases that listed it last time, and a part there said what the bases listed when one of them had answered this round without it (yetone's review of #1006, nyownm's #904). With two keys where both were served a Claude model, and the Anthropic base stopping both of them in the round the second key was down, sides[anthropic] kept a model the base had plainly dropped, and the fetch after that read it as a base that couldn't be asked and held the model one round longer than it should have. It is self-healing — the next round with the base answering settles it — and it is the same fault c8fabd70 was written to undo, read from the other side. sides may only say what the bases listed. A model kept for a failed key belongs to a base that was not asked this round for any of the model's own keys: one that was, and answered without the model, has said what it serves today and no longer owns it. Which is per key, not per provider: fetchOne answers which of its bases replied, which a model of the second key alone never saw an answer from, so the base's saying nothing about it for the first key leaves it that base's. The keys a model is kept under are read from the list saved before, since add clears the ones of the model it is adding. TestFetchDropsAModelABaseStoppedServingWhileAKeyIsDown fails on c8fabd70 with the part holding claude-b, and TestFetchKeepsAModelsBaseAfterItsKeyFailedWhole goes on passing: the same two keys, a model only one of them sees. Co-authored-by: multica-agent <github@multica.ai> * provider, catalog: a page of a model list is not the list (#904) A /v1/models that answers with has_more is one page of the vendor's catalog. Read as the whole list, every model of a later page reads as one the vendor has dropped, and the user's pick of it goes for good, on a list that never said so. Such a reply is no answer: the base is treated as one that could not be asked, which keeps what it listed last time (#904's review of #1006). OpenRouter's Anthropic base is not asked for the list at all. With the Anthropic version header its /models answers with the catalog again, twenty newest first, every id namespaced under `anthropic/`, and the cursors of the pages after. None of those twenty is an id in OpenRouter's own catalog, so merged they are entries of the model picker that name no model: the long, cluttered list #904's reporter wrote of. Its Chat base is that catalog, whole, and the fetch is one request where it was two. The rule is OpenRouter's, by host, as the ones after it are scoped to their vendors. Co-authored-by: multica-agent <github@multica.ai> * provider: the OpenRouter rule reads the base being asked (0fb16990) It read the Chat base's host, so a provider with a Chat base of OpenRouter's and an Anthropic base of another vendor's had that other base skipped: its models were in neither the union nor the sides nor the bases that could not be asked, so no keepDeadSides path found them and the user's picks of them went for good. `magpie provider set <id> anthropic=<url>` reaches that shape from the CLI. The rule is about the vendor whose base is being asked, so that is what it now reads, together with whether the provider asks at a base of its own besides it: an OpenRouter Anthropic base alone is still asked, its reply being the only catalog there would otherwise be, and catalog.fetchOne is what keeps a page of it from being read as the whole. Five combinations of the two bases and openrouter.ai, one assertion each; three of them fail on 0fb16990. Co-authored-by: multica-agent <github@multica.ai> * catalog, provider: a model list is followed to its last page, not read as a page (#1006) A reply with has_more was refused as no answer at all, which is right for a provider with several bases and wrong for one with a single base: Anthropic's own preset has no other base to fall back on, so once its list passes twenty models — about sixteen today — the fetch answered an error and no models, where main listed the twenty of the first page, and a pick of a model on a later page went with them. catalog.fetchOne now reads a list to its end: the first page as the URL stands, and every page after it with limit=1000 and the after_id the page before ended at, up to twenty pages. Half a list is still no answer, and that is what the following ends on: a page that fails, a list that pages without naming the id to carry on from, one that answers the page just asked again under the same id, and one still paging at the cap all leave the base as one that could not be asked, which keeps what it listed last time. The rule lives in catalog.Paged, and the Kilo gateway's list — OpenRouter's shape, which pages the same way — is paged by it instead of refused by a guard of its own; the comment on that guard claimed OpenRouter's /models pages, which neither api.kilo.ai nor openrouter.ai was answering with on 2026-10-06, and now says so. One assertion of TestFetchOpenRouterRuleOverTheFourBaseCombinations follows the rule rather than the old count: OpenRouter's Anthropic base alone is asked for its first page and the page after it, which is the same page again, where before it was asked once and refused. Tests: TestFetchFollowsThePagesOfAList, TestFetchFollowsAPageAListAnswersItsOwnWay, TestFetchTakesAPagedListAsNoAnswerWhenAPageFails, TestFetchTakesAPagedListWithoutAnIdAsNoAnswer, TestFetchTakesAListThatPagesWithoutEndAsNoAnswer (catalog); TestFetchFollowsThePagesOfASingleAnthropicBase (provider, the Anthropic preset's one base, 25 models 20 to a page) and TestKiloModelsFollowThePagesOfTheList. Co-authored-by: multica-agent <github@multica.ai> * provider: OpenRouter's Anthropic base alone is asked, and refused (#1006) The fake that stands for openrouter.ai answered every cursor with the same twenty ids under the same last_id, so following the pages of a list stopped where the rule of catalog.Paged says a list stops being readable, and the single-base case looked covered: the twenty were refused and the fetch failed. The endpoint does not answer that way. Asked with the Anthropic version header on 2026-10-06, /api/v1/models gave those twenty with has_more and a cursor, and under the cursor 413 more ids with has_more false — a different set, and the end of the list. The two pages share nothing, and following them lands deeper in the `anthropic/` namespace rather than back at the catalog it was namespaced from: of the 433 ids, 4 are in the 466 OpenRouter serves without that header. So the twenty were not a page to be refused but the first half of a list that reads to the end, and the end is still not a catalog of models to serve. The base is still asked — it is the only base there is, and refusing to ask would leave the provider with nothing at all — and the list it answers with is now refused as a whole, which is what the old rule did with one page of it. It fails the fetch like any other base that could not be asked, so the provider keeps the models it listed last time (#904). The two replies are kept as they came, under testdata, and a test reads them back: twenty, then 413, the cursor the first ends at, has_more true and then false, and not one id of either page in the catalog OpenRouter serves. The fake pages with after_id, as the endpoint does. Tests: TestOpenRouterNamespacedPagesAreTheEndpointShape (new), TestFetchOpenRouterRuleOverTheFourBaseCombinations (the single-base case now asks exactly the two pages it answers with, keeps no list, and fails), TestFetchKeepsPicksWhenAModelListIsPaginated, TestFetchDoesNotMergeOpenRouter's NamespacedList, TestFetchOpenRouterAddsNoNamespacedModelAndKeepsPicks, TestFetchKeepsAnotherVendorsAnthropicList. Co-authored-by: multica-agent <github@multica.ai> * provider: the message for a namespaced list counts nothing (#1006) The reason a provider whose only base is OpenRouter's Anthropic one is refused is still the one it was, and the base is still asked and refused exactly as before: what changed is the sentence the user is left with, and the comments around it. That sentence counted the list it refused — "433 of them, of which 4 are ids OpenRouter serves" — and those counts are of whatever OpenRouter's catalog was that day, so the number a user reads is wrong tomorrow without anyone touching it. It now says why the answer is no good and what to do instead: give this provider OpenRouter's Chat base, which lists those models under the ids OpenRouter serves, or type the model ids in by hand, which is what the caller already appends to every base it could not ask. The comments said the same counts as fact, and cited a probe file that is not in the repository. The counts stay, marked as what was observed on 2026-10-06, and what does not move is now the point: following the pages of the list lands deeper in the namespace, not back at the catalog it was namespaced from. The dead file references are gone, and one test checks that the message counts nothing at all, so a count cannot creep back in. Tests: TestFetchOpenRouterRuleOverTheFourBaseCombinations (the message names the Chat base and the ids to type, and holds no number but the version in that base's path). Co-authored-by: multica-agent <github@multica.ai> * provider: the list of a multi-protocol vendor is asked once (#1006) The dedup added in 952d0b00 is keyed by the base URL as it is configured, which is neither of the two things that decide whether a list is asked again: which list URL the request goes to, and what it authenticates with. Both of a vendor's ends were lost to it. A Chat base and the Anthropic base of the same remote magpie are two strings, one list — GET /v1/models — and it was fetched twice, which #1282 says a remote's test does not do. And one base string serving Chat, Anthropic and Gemini is three requests: the Chat request refused a 401 took the other two for lists already had, so a vendor whose models were all there was reported as serving none. The key is now the list URL and the authentication, in catalog.Listed, and it gives two rules. A list URL this key has listed is not asked again by any protocol — the answer is the answer, so …/v1 and the bare host beside it are one request. And a request already made is not made again, but a URL refused under one authentication is not refused under the next, so the Gemini and Anthropic lists of a base whose Chat request was refused are asked, each with its own. The base string is left out of the key on purpose: Chat and Responses at one base are one request because both send Bearer at the same URL, not because the strings match. A base whose every list was asked already and none answered says so with catalog.ErrAsked, which the caller reads as "nothing new here" rather than as a second refusal to put in the message the user reads. Tests: TestFetchAsksEachAuthenticationAtOneBase, TestFetchAsksOneListOnce ForTwoBasesOfIt, TestFetchReportsOneRefusalForOneBaseAskedTwice. Co-authored-by: multica-agent <github@multica.ai> * provider: a Gemini base that fails once no longer deletes the picked model it listed The union fetch merges a Gemini base's list like any other protocol's, but it kept none of the fetch's bookkeeping for it: what it listed was in no `side`, so the parts saved beside the catalog said no base listed it, and a base that could not be asked was in no `dead` either, so `keepDeadSides` never saw it. A picked Gemini model was therefore dropped from the user's provider for good the first time the Gemini endpoint answered 502 or timed out — the fault the Anthropic base of the same fetch was fixed for in #904. A Gemini base also went uncounted in `asked`, so a two-base provider's parts were saved on the strength of one base asked. A Gemini base now joins `asked` before it is asked, joins `sides` and `answered` with what it listed, and joins `dead` when it cannot be asked. What a failed Gemini base keeps is its own models alone, so a model its list has really dropped is still dropped from the picks. Verified at 20ee273aaedc10ab0baf06cffbd826ddc367be3c + this commit, Go 1.26.8, darwin/arm64, under a temporary HOME with GOPATH, GOMODCACHE and GOCACHE pinned. Without the change (internal/provider/models.go put back): go test ./internal/provider -run 'TestFetchKeepsTheModelsAFailedGeminiBaseListed|TestFetchDropsAGoneGeminiModelWhileAnotherProtocolFails' multiproto_models_test.go:713: picks after the Gemini base failed: [gpt-a], want [gpt-a claude-b] multiproto_models_test.go:824: picks after the Gemini list dropped a model: [gpt-a gpt-b claude-b claude-c], want [gpt-a gpt-b claude-b] FAIL (exit 1) With it: go test ./internal/provider -run 'TestFetchKeepsTheModelsAFailedGeminiBaseListed|TestFetchDropsAGoneGeminiModelWhileAnotherProtocolFails' ok (exit 0) go test -tags nogui ./internal/provider -count=1 ok github.com/yetone/magpie/internal/provider 109.187s (exit 0) gofmt -l internal/provider/models.go internal/provider/multiproto_models_test.go (prints nothing) go vet ./internal/provider (exit 0) GOOS=linux go vet -tags nogui ./... (exit 0) GOOS=windows go vet ./... (exit 0) go build ./... (exit 0) GOOS=linux go build -tags nogui ./... (exit 0) GOOS=windows go build ./... (exit 0) The maintainer's own reproduction of the first failure, run on 20ee273a before anything was changed, is the same one line and exits 1: --- FAIL: TestZZGeminiSideKept (0.04s) zz_gem_test.go:81: picks after a protocol failed: [gpt-a], want [gpt-a claude-b] The whole-repository `go test -tags nogui ./...` was not run: the change touches only internal/provider, and that package's full suite is green. Co-authored-by: multica-agent <github@multica.ai> --------- Co-authored-by: multica-agent <github@multica.ai>
1384 lines
42 KiB
Go
1384 lines
42 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/agentenv"
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// A provider whose protocols are served at bases of their own is asked
|
|
// each of them, and the lists are merged (#904): a vendor that lists its
|
|
// Claude models at its Anthropic base and its GPT models at its Chat base
|
|
// kept only the first list that answered, so the models of every other
|
|
// endpoint were invisible in the product — in the agents' model lists, in
|
|
// the saved preferences, and even behind a model's fixed protocol, which
|
|
// setModelAPI refuses for a model the catalog does not list.
|
|
func TestFetchUnionsProtocolLists(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
var chatHits, anthropicHits atomic.Int32
|
|
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
chatHits.Add(1)
|
|
w.Write([]byte(`{"data":[{"id":"gpt-a","display_name":"GPT A",` +
|
|
`"supported_reasoning_levels":[{"effort":"high"}],` +
|
|
`"modalities":{"input":["text"]}}]}`))
|
|
}))
|
|
defer chat.Close()
|
|
anthropic := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
anthropicHits.Add(1)
|
|
w.Write([]byte(`{"data":[{"id":"claude-b","display_name":"Claude B"}]}`))
|
|
}))
|
|
defer anthropic.Close()
|
|
|
|
p := Provider{
|
|
ID: "dual",
|
|
Name: "Dual",
|
|
Chat: chat.URL + "/v1",
|
|
Anthropic: anthropic.URL + "/v1",
|
|
Key: "sk-d",
|
|
}
|
|
if err := Save(p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
q, err := Find("dual")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ms, err := q.Fetch(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("fetch: %v", err)
|
|
}
|
|
got := map[string]catalog.Model{}
|
|
for _, m := range ms {
|
|
got[m.ID] = m
|
|
}
|
|
if len(got) != 2 || got["gpt-a"].ID == "" || got["claude-b"].ID == "" {
|
|
t.Fatalf("listed %v, want gpt-a and claude-b together", ms)
|
|
}
|
|
if chatHits.Load() != 1 || anthropicHits.Load() != 1 {
|
|
t.Errorf("asked chat %d times, anthropic %d", chatHits.Load(), anthropicHits.Load())
|
|
}
|
|
// the list a model came from keeps its own fields
|
|
if g := got["gpt-a"]; g.Name != "GPT A" || len(g.Efforts) != 1 || g.Efforts[0] != "high" {
|
|
t.Errorf("gpt-a: %+v", g)
|
|
}
|
|
if g := got["claude-b"]; g.Name != "Claude B" {
|
|
t.Errorf("claude-b: %+v", g)
|
|
}
|
|
// the models an agent is offered, and a model's own protocol setting
|
|
// (serves, then SetModelAPI), both read the catalog the fetch kept:
|
|
// a model only the second endpoint lists has to be in it
|
|
avail := map[string]bool{}
|
|
for _, m := range q.Available() {
|
|
avail[m.ID] = true
|
|
}
|
|
if !avail["gpt-a"] || !avail["claude-b"] {
|
|
t.Errorf("available %v, want gpt-a and claude-b", q.Available())
|
|
}
|
|
if !q.serves("claude-b") {
|
|
t.Error("claude-b is not a model of the provider")
|
|
}
|
|
// the base kept for routing is the one the first protocol answered at
|
|
if l, err := q.fetchOne(context.Background()); err != nil || l.base != chat.URL+"/v1" {
|
|
t.Errorf("base %q (%v), want the chat one", l.base, err)
|
|
}
|
|
}
|
|
|
|
// The order the protocols are asked in must not decide which models are
|
|
// kept: a provider with a Responses base and an Anthropic base and no Chat
|
|
// one is asked both.
|
|
func TestFetchUnionsProtocolListsWithoutChat(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
var responsesHits, anthropicHits atomic.Int32
|
|
responses := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
responsesHits.Add(1)
|
|
w.Write([]byte(`{"data":[{"id":"gpt-a"}]}`))
|
|
}))
|
|
defer responses.Close()
|
|
anthropic := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
anthropicHits.Add(1)
|
|
w.Write([]byte(`{"data":[{"id":"claude-b"}]}`))
|
|
}))
|
|
defer anthropic.Close()
|
|
|
|
if err := Save(Provider{
|
|
ID: "dual2",
|
|
Name: "Dual 2",
|
|
Responses: responses.URL + "/v1",
|
|
Anthropic: anthropic.URL + "/v1",
|
|
Key: "sk-d",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
q, err := Find("dual2")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ms, err := q.Fetch(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("fetch: %v", err)
|
|
}
|
|
if len(ms) != 2 || ms[0].ID != "gpt-a" || ms[1].ID != "claude-b" {
|
|
t.Fatalf("listed %v, want gpt-a then claude-b", ms)
|
|
}
|
|
if responsesHits.Load() != 1 || anthropicHits.Load() != 1 {
|
|
t.Errorf("asked responses %d times, anthropic %d", responsesHits.Load(), anthropicHits.Load())
|
|
}
|
|
}
|
|
|
|
// One model in two lists is one model: its image capability is merged the
|
|
// way the keys' lists are (an explicit text-only answer wins), not taken
|
|
// from whichever list was read last.
|
|
func TestFetchUnionsSharedModel(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(`{"data":[{"id":"gpt-a","display_name":"GPT A",` +
|
|
`"supported_reasoning_levels":[{"effort":"high"}],` +
|
|
`"modalities":{"input":["text"]}}]}`))
|
|
}))
|
|
defer chat.Close()
|
|
anthropic := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
// the same model, as the Anthropic endpoint answers for it
|
|
w.Write([]byte(`{"data":[{"id":"gpt-a","display_name":"GPT A",` +
|
|
`"supported_reasoning_levels":[{"effort":"high"}],` +
|
|
`"modalities":{"input":["text","image"]}}]}`))
|
|
}))
|
|
defer anthropic.Close()
|
|
|
|
if err := Save(Provider{
|
|
ID: "shared",
|
|
Name: "Shared",
|
|
Chat: chat.URL + "/v1",
|
|
Anthropic: anthropic.URL + "/v1",
|
|
Key: "sk-d",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
q, err := Find("shared")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ms, err := q.Fetch(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("fetch: %v", err)
|
|
}
|
|
if len(ms) != 1 {
|
|
t.Fatalf("listed %v, want one model", ms)
|
|
}
|
|
m := ms[0]
|
|
if m.ID != "gpt-a" || m.Name != "GPT A" || len(m.Efforts) != 1 || m.Efforts[0] != "high" {
|
|
t.Errorf("fields lost: %+v", m)
|
|
}
|
|
if m.ImageInput == nil || *m.ImageInput {
|
|
t.Errorf("image input %v, want the explicit text-only answer kept", m.ImageInput)
|
|
}
|
|
if m.Images {
|
|
t.Errorf("images on, want off: %+v", m)
|
|
}
|
|
}
|
|
|
|
// A provider whose every protocol answers keeps asking only the bases it
|
|
// speaks: one protocol means the one request it made before.
|
|
func TestFetchAsksOneProtocolOnce(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
var hits atomic.Int32
|
|
one := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
hits.Add(1)
|
|
w.Write([]byte(`{"data":[{"id":"gpt-a"},{"id":"gpt-b"}]}`))
|
|
}))
|
|
defer one.Close()
|
|
|
|
if err := Save(Provider{ID: "one", Name: "One", Chat: one.URL + "/v1", Key: "sk-o"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
q, err := Find("one")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ms, err := q.Fetch(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("fetch: %v", err)
|
|
}
|
|
if len(ms) != 2 || ms[0].ID != "gpt-a" || ms[1].ID != "gpt-b" {
|
|
t.Errorf("listed %v", ms)
|
|
}
|
|
if hits.Load() != 1 {
|
|
t.Errorf("asked %d times, want once", hits.Load())
|
|
}
|
|
}
|
|
|
|
// Chat and Responses at one base ask it once, as they did when the first
|
|
// answer ended the fetch.
|
|
func TestFetchAsksASharedBaseOnce(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
var hits atomic.Int32
|
|
shared := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
hits.Add(1)
|
|
w.Write([]byte(`{"data":[{"id":"gpt-a"}]}`))
|
|
}))
|
|
defer shared.Close()
|
|
|
|
if err := Save(Provider{
|
|
ID: "same",
|
|
Name: "Same",
|
|
Chat: shared.URL + "/v1",
|
|
Responses: shared.URL + "/v1",
|
|
Key: "sk-s",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
q, err := Find("same")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ms, err := q.Fetch(context.Background()); err != nil || len(ms) != 1 {
|
|
t.Fatalf("%v %v", ms, err)
|
|
}
|
|
if hits.Load() != 1 {
|
|
t.Errorf("asked %d times, want once", hits.Load())
|
|
}
|
|
}
|
|
|
|
// Where no protocol answers, the error is the one it was: every base asked
|
|
// is named in it, with what each said, and the ids can still be typed in.
|
|
func TestFetchUnionsFailsWithEveryURL(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.NotFound(w, r)
|
|
}))
|
|
defer chat.Close()
|
|
anthropic := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.NotFound(w, r)
|
|
}))
|
|
defer anthropic.Close()
|
|
|
|
if err := Save(Provider{
|
|
ID: "dead",
|
|
Name: "Dead",
|
|
Chat: chat.URL + "/v1",
|
|
Anthropic: anthropic.URL + "/v1",
|
|
Key: "sk-x",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
q, err := Find("dead")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ms, err := q.Fetch(context.Background())
|
|
if err == nil {
|
|
t.Fatalf("listed %v with no endpoint answering", ms)
|
|
}
|
|
msg := err.Error()
|
|
if !strings.Contains(msg, chat.URL+"/v1/models: 404") || !strings.Contains(msg, anthropic.URL) {
|
|
t.Errorf("error does not name every base asked: %s", msg)
|
|
}
|
|
if !strings.Contains(msg, "by hand") {
|
|
t.Errorf("error lost the way out: %s", msg)
|
|
}
|
|
if ms != nil {
|
|
t.Errorf("listed %v with no endpoint answering", ms)
|
|
}
|
|
if _, _, ok := catalog.Live("dead"); ok {
|
|
t.Error("a catalog was kept for a provider that answered nothing")
|
|
}
|
|
}
|
|
|
|
// A models URL is where the user said its list is: that URL alone is asked,
|
|
// once, and no base is.
|
|
func TestFetchModelsURLAsksNoBase(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
var baseHits atomic.Int32
|
|
base := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
baseHits.Add(1)
|
|
w.Write([]byte(`{"data":[{"id":"gpt-a"}]}`))
|
|
}))
|
|
defer base.Close()
|
|
var urlHits atomic.Int32
|
|
list := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
urlHits.Add(1)
|
|
w.Write([]byte(`{"data":[{"id":"claude-b"}]}`))
|
|
}))
|
|
defer list.Close()
|
|
|
|
if err := Save(Provider{
|
|
ID: "listed",
|
|
Name: "Listed",
|
|
Chat: base.URL + "/v1",
|
|
Anthropic: base.URL + "/v1",
|
|
ModelsURL: list.URL + "/v1/models",
|
|
Key: "sk-l",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
q, err := Find("listed")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ms, err := q.Fetch(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("fetch: %v", err)
|
|
}
|
|
if len(ms) != 1 || ms[0].ID != "claude-b" {
|
|
t.Errorf("listed %v, want the models URL's own list", ms)
|
|
}
|
|
if urlHits.Load() != 1 {
|
|
t.Errorf("asked the models URL %d times, want once", urlHits.Load())
|
|
}
|
|
if baseHits.Load() != 0 {
|
|
t.Errorf("asked a base %d times beside the models URL", baseHits.Load())
|
|
}
|
|
}
|
|
|
|
// A protocol of a multi-protocol provider that can't be asked now keeps the
|
|
// models it listed last time, as a key that can't be asked does: a failed
|
|
// read says nothing about what the vendor serves today, and without it a
|
|
// picked Claude model is gone from the user's provider for good (#904,
|
|
// yetone's review of the union fetch: an Anthropic base answering 502
|
|
// deleted its models from the picks and never brought them back).
|
|
func TestFetchKeepsTheModelsAFailedProtocolListed(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(`{"data":[{"id":"gpt-a"}]}`))
|
|
}))
|
|
defer chat.Close()
|
|
var list atomic.Value
|
|
list.Store(`{"data":[{"id":"claude-b"}]}`)
|
|
var down atomic.Bool
|
|
anthropic := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if down.Load() {
|
|
http.Error(w, "upstream is down", http.StatusBadGateway)
|
|
return
|
|
}
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(list.Load().(string)))
|
|
}))
|
|
defer anthropic.Close()
|
|
|
|
if err := Save(Provider{
|
|
ID: "failing",
|
|
Name: "Failing",
|
|
Chat: chat.URL + "/v1",
|
|
Anthropic: anthropic.URL + "/v1",
|
|
Key: "sk-f",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fetch := func() error {
|
|
p, err := Find("failing")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = p.Fetch(context.Background())
|
|
return err
|
|
}
|
|
if err := fetch(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// both models picked from the list both protocols answered with
|
|
p, _ := Find("failing")
|
|
p.Models = []string{"gpt-a", "claude-b"}
|
|
if err := Save(*p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// the Anthropic base is down: the fetch still succeeds on the Chat one,
|
|
// and nothing the failed endpoint listed goes missing
|
|
down.Store(true)
|
|
if err := fetch(); err != nil {
|
|
t.Fatalf("fetch with one protocol down: %v", err)
|
|
}
|
|
p, _ = Find("failing")
|
|
if want := []string{"gpt-a", "claude-b"}; !slices.Equal(p.Models, want) {
|
|
t.Fatalf("picks after a protocol failed: %v, want %v", p.Models, want)
|
|
}
|
|
live, _, ok := p.live()
|
|
if !ok {
|
|
t.Fatal("no list kept after a protocol failed")
|
|
}
|
|
if ids := idsOf(live); !slices.Equal(ids, []string{"gpt-a", "claude-b"}) {
|
|
t.Fatalf("list after a protocol failed: %v, want gpt-a and claude-b", ids)
|
|
}
|
|
if !p.serves("claude-b") {
|
|
t.Error("claude-b is not a model of the provider any more")
|
|
}
|
|
|
|
// and once the endpoint answers again its real list stands: a model it
|
|
// has dropped since is dropped here, so keeping is not keeping forever
|
|
down.Store(false)
|
|
list.Store(`{"data":[{"id":"claude-c"}]}`)
|
|
if err := fetch(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ = Find("failing")
|
|
if want := []string{"gpt-a"}; !slices.Equal(p.Models, want) {
|
|
t.Fatalf("picks after the endpoint answered again: %v, want %v", p.Models, want)
|
|
}
|
|
live, _, ok = p.live()
|
|
if !ok {
|
|
t.Fatal("no list kept after the endpoint answered again")
|
|
}
|
|
if ids := idsOf(live); !slices.Equal(ids, []string{"gpt-a", "claude-c"}) {
|
|
t.Fatalf("list after the endpoint answered again: %v, want gpt-a and claude-c", ids)
|
|
}
|
|
}
|
|
|
|
// The other way round, which is what stops the fix from keeping every old
|
|
// model: a model only the Chat base listed is gone from that base's list,
|
|
// and the Anthropic base failing says nothing about it. Keeping every model
|
|
// of a failed fetch would resurrect it (yetone's review).
|
|
func TestFetchDropsAGoneModelWhileAnotherProtocolFails(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
var list atomic.Value
|
|
list.Store(`{"data":[{"id":"gpt-a"},{"id":"gpt-b"}]}`)
|
|
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(list.Load().(string)))
|
|
}))
|
|
defer chat.Close()
|
|
var down atomic.Bool
|
|
anthropic := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if down.Load() {
|
|
http.Error(w, "upstream is down", http.StatusBadGateway)
|
|
return
|
|
}
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(`{"data":[{"id":"claude-b"}]}`))
|
|
}))
|
|
defer anthropic.Close()
|
|
|
|
if err := Save(Provider{
|
|
ID: "half",
|
|
Name: "Half",
|
|
Chat: chat.URL + "/v1",
|
|
Anthropic: anthropic.URL + "/v1",
|
|
Key: "sk-h",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fetch := func() error {
|
|
p, err := Find("half")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = p.Fetch(context.Background())
|
|
return err
|
|
}
|
|
if err := fetch(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ := Find("half")
|
|
p.Models = []string{"gpt-a", "gpt-b", "claude-b"}
|
|
if err := Save(*p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// the Chat base has dropped gpt-b, and the Anthropic base is down
|
|
list.Store(`{"data":[{"id":"gpt-a"}]}`)
|
|
down.Store(true)
|
|
if err := fetch(); err != nil {
|
|
t.Fatalf("fetch with one protocol down: %v", err)
|
|
}
|
|
p, _ = Find("half")
|
|
if want := []string{"gpt-a", "claude-b"}; !slices.Equal(p.Models, want) {
|
|
t.Fatalf("picks after the Chat list dropped a model: %v, want %v", p.Models, want)
|
|
}
|
|
live, _, ok := p.live()
|
|
if !ok {
|
|
t.Fatal("no list kept after a protocol failed")
|
|
}
|
|
if ids := idsOf(live); !slices.Equal(ids, []string{"gpt-a", "claude-b"}) {
|
|
t.Fatalf("list after the Chat list dropped a model: %v, want gpt-a and claude-b", ids)
|
|
}
|
|
}
|
|
|
|
// The same for a Gemini base, which the union merge had left out: a
|
|
// protocol whose list is merged but which is kept in no side is a base a
|
|
// fetch cannot find when it fails, so the picked Gemini model it listed was
|
|
// read as gone and dropped for good — the very fault the Anthropic base
|
|
// above was fixed for (yetone's review of #1006, 2026-10-13).
|
|
func TestFetchKeepsTheModelsAFailedGeminiBaseListed(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(`{"data":[{"id":"gpt-a"}]}`))
|
|
}))
|
|
defer chat.Close()
|
|
var list atomic.Value
|
|
list.Store(`{"models":[{"name":"models/claude-b","supportedGenerationMethods":["generateContent"]}]}`)
|
|
var down atomic.Bool
|
|
gemini := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if down.Load() {
|
|
http.Error(w, "upstream is down", http.StatusBadGateway)
|
|
return
|
|
}
|
|
if r.URL.Path != "/v1beta/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(list.Load().(string)))
|
|
}))
|
|
defer gemini.Close()
|
|
|
|
if err := Save(Provider{
|
|
ID: "gemfailing",
|
|
Name: "GemFailing",
|
|
Chat: chat.URL + "/v1",
|
|
Gemini: gemini.URL + "/v1beta",
|
|
Key: "sk-g",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fetch := func() error {
|
|
p, err := Find("gemfailing")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = p.Fetch(context.Background())
|
|
return err
|
|
}
|
|
if err := fetch(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// both models picked from the list both bases answered with
|
|
p, _ := Find("gemfailing")
|
|
p.Models = []string{"gpt-a", "claude-b"}
|
|
if err := Save(*p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// the Gemini base is down: the fetch still succeeds on the Chat one,
|
|
// and nothing the failed endpoint listed goes missing
|
|
down.Store(true)
|
|
if err := fetch(); err != nil {
|
|
t.Fatalf("fetch with the Gemini base down: %v", err)
|
|
}
|
|
p, _ = Find("gemfailing")
|
|
if want := []string{"gpt-a", "claude-b"}; !slices.Equal(p.Models, want) {
|
|
t.Fatalf("picks after the Gemini base failed: %v, want %v", p.Models, want)
|
|
}
|
|
live, _, ok := p.live()
|
|
if !ok {
|
|
t.Fatal("no list kept after the Gemini base failed")
|
|
}
|
|
if ids := idsOf(live); !slices.Equal(ids, []string{"gpt-a", "claude-b"}) {
|
|
t.Fatalf("list after the Gemini base failed: %v, want gpt-a and claude-b", ids)
|
|
}
|
|
if !p.serves("claude-b") {
|
|
t.Error("claude-b is not a model of the provider any more")
|
|
}
|
|
|
|
// and once the Gemini base answers again its real list stands: a model
|
|
// it has dropped since is dropped here, so keeping is not keeping
|
|
// forever
|
|
down.Store(false)
|
|
list.Store(`{"models":[{"name":"models/claude-c","supportedGenerationMethods":["generateContent"]}]}`)
|
|
if err := fetch(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ = Find("gemfailing")
|
|
if want := []string{"gpt-a"}; !slices.Equal(p.Models, want) {
|
|
t.Fatalf("picks after the Gemini base answered again: %v, want %v", p.Models, want)
|
|
}
|
|
live, _, ok = p.live()
|
|
if !ok {
|
|
t.Fatal("no list kept after the Gemini base answered again")
|
|
}
|
|
if ids := idsOf(live); !slices.Equal(ids, []string{"gpt-a", "claude-c"}) {
|
|
t.Fatalf("list after the Gemini base answered again: %v, want gpt-a and claude-c", ids)
|
|
}
|
|
}
|
|
|
|
// And the same the other way round for Gemini: a model the Gemini base has
|
|
// really dropped is dropped from the picks while the Chat base is down. It
|
|
// is what stops the fix above from keeping every old model by never
|
|
// dropping anything (yetone's review).
|
|
func TestFetchDropsAGoneGeminiModelWhileAnotherProtocolFails(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
var list atomic.Value
|
|
list.Store(`{"data":[{"id":"gpt-a"},{"id":"gpt-b"}]}`)
|
|
var down atomic.Bool
|
|
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if down.Load() {
|
|
http.Error(w, "upstream is down", http.StatusBadGateway)
|
|
return
|
|
}
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(list.Load().(string)))
|
|
}))
|
|
defer chat.Close()
|
|
var gemList atomic.Value
|
|
gemList.Store(`{"models":[{"name":"models/claude-b","supportedGenerationMethods":["generateContent"]},` +
|
|
`{"name":"models/claude-c","supportedGenerationMethods":["generateContent"]}]}`)
|
|
gemini := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1beta/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(gemList.Load().(string)))
|
|
}))
|
|
defer gemini.Close()
|
|
|
|
if err := Save(Provider{
|
|
ID: "gemhalf",
|
|
Name: "GemHalf",
|
|
Chat: chat.URL + "/v1",
|
|
Gemini: gemini.URL + "/v1beta",
|
|
Key: "sk-h",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fetch := func() error {
|
|
p, err := Find("gemhalf")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = p.Fetch(context.Background())
|
|
return err
|
|
}
|
|
if err := fetch(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ := Find("gemhalf")
|
|
p.Models = []string{"gpt-a", "gpt-b", "claude-b", "claude-c"}
|
|
if err := Save(*p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// the Gemini base has dropped claude-c, and the Chat base is down
|
|
gemList.Store(`{"models":[{"name":"models/claude-b","supportedGenerationMethods":["generateContent"]}]}`)
|
|
down.Store(true)
|
|
if err := fetch(); err != nil {
|
|
t.Fatalf("fetch with the Chat base down: %v", err)
|
|
}
|
|
p, _ = Find("gemhalf")
|
|
if want := []string{"gpt-a", "gpt-b", "claude-b"}; !slices.Equal(p.Models, want) {
|
|
t.Fatalf("picks after the Gemini list dropped a model: %v, want %v", p.Models, want)
|
|
}
|
|
live, _, ok := p.live()
|
|
if !ok {
|
|
t.Fatal("no list kept after a protocol failed")
|
|
}
|
|
// the bases are asked in order, and what a failed base keeps is added
|
|
// beside the list, not in the place it would have answered at
|
|
if ids := idsOf(live); !slices.Equal(ids, []string{"claude-b", "gpt-a", "gpt-b"}) {
|
|
t.Fatalf("list after the Gemini list dropped a model: %v, want claude-b, gpt-a and gpt-b", ids)
|
|
}
|
|
}
|
|
|
|
// idsOf are the ids of a list, in its order.
|
|
func idsOf(ms []catalog.Model) []string {
|
|
var out []string
|
|
for _, m := range ms {
|
|
out = append(out, m.ID)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// The same where each of several keys has a list of its own: a base of the
|
|
// provider's that can't be asked keeps its models, and every model's keys
|
|
// are its own still.
|
|
func TestFetchKeepsTheModelsAFailedProtocolListedPerKey(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(`{"data":[{"id":"gpt-a"}]}`))
|
|
}))
|
|
defer chat.Close()
|
|
var down atomic.Bool
|
|
anthropic := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if down.Load() {
|
|
http.Error(w, "upstream is down", http.StatusBadGateway)
|
|
return
|
|
}
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(`{"data":[{"id":"claude-b"}]}`))
|
|
}))
|
|
defer anthropic.Close()
|
|
|
|
if err := Save(Provider{
|
|
ID: "keys",
|
|
Name: "Keys",
|
|
Chat: chat.URL + "/v1",
|
|
Anthropic: anthropic.URL + "/v1",
|
|
Key: "sk-1",
|
|
Keys: []KeyAccount{{Key: "sk-2"}},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fetch := func() error {
|
|
p, err := Find("keys")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = p.Fetch(context.Background())
|
|
return err
|
|
}
|
|
if err := fetch(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ := Find("keys")
|
|
p.Models = []string{"gpt-a", "claude-b"}
|
|
if err := Save(*p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
down.Store(true)
|
|
if err := fetch(); err != nil {
|
|
t.Fatalf("fetch with one protocol down: %v", err)
|
|
}
|
|
p, _ = Find("keys")
|
|
if want := []string{"gpt-a", "claude-b"}; !slices.Equal(p.Models, want) {
|
|
t.Fatalf("picks after a protocol failed: %v, want %v", p.Models, want)
|
|
}
|
|
live, _, ok := p.live()
|
|
if !ok {
|
|
t.Fatal("no list kept after a protocol failed")
|
|
}
|
|
if ids := idsOf(live); !slices.Equal(ids, []string{"gpt-a", "claude-b"}) {
|
|
t.Fatalf("list after a protocol failed: %v, want gpt-a and claude-b", ids)
|
|
}
|
|
// both keys still see both models: a model kept for a base that failed
|
|
// is not a model of the keys that answered beside it alone
|
|
for _, m := range live {
|
|
if len(m.Keys) != 2 || !slices.Contains(m.Keys, KeyID("sk-1")) || !slices.Contains(m.Keys, KeyID("sk-2")) {
|
|
t.Errorf("%s keys %v, want both", m.ID, m.Keys)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A list saved before the parts were kept says nothing about which base
|
|
// listed what, so the models no answering base lists are kept this once, and
|
|
// the next fetch with every base answering settles it: what that base does
|
|
// not list is gone, however long it was kept for.
|
|
func TestFetchKeepsModelsOfAListSavedWithoutParts(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(`{"data":[{"id":"gpt-a"}]}`))
|
|
}))
|
|
defer chat.Close()
|
|
var down atomic.Bool
|
|
anthropic := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if down.Load() {
|
|
http.Error(w, "upstream is down", http.StatusBadGateway)
|
|
return
|
|
}
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(`{"data":[{"id":"claude-b"}]}`))
|
|
}))
|
|
defer anthropic.Close()
|
|
|
|
if err := Save(Provider{
|
|
ID: "unrecorded",
|
|
Name: "Unrecorded",
|
|
Chat: chat.URL + "/v1",
|
|
Anthropic: anthropic.URL + "/v1",
|
|
Key: "sk-u",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// the list a fetch before this change kept: the union, with no part
|
|
// saying which base listed what
|
|
if err := catalog.SaveLive("unrecorded", chat.URL+"/v1", []catalog.Model{
|
|
{ID: "gpt-a"}, {ID: "gpt-b"}, {ID: "claude-b"},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ := Find("unrecorded")
|
|
p.Models = []string{"gpt-a", "gpt-b", "claude-b"}
|
|
if err := Save(*p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
down.Store(true)
|
|
if _, err := p.Fetch(context.Background()); err != nil {
|
|
t.Fatalf("fetch with one protocol down: %v", err)
|
|
}
|
|
p, _ = Find("unrecorded")
|
|
if want := []string{"gpt-a", "gpt-b", "claude-b"}; !slices.Equal(p.Models, want) {
|
|
t.Fatalf("picks after a protocol failed: %v, want %v", p.Models, want)
|
|
}
|
|
|
|
// every base answering now says which models are its own: gpt-b, gone
|
|
// from the Chat list, is gone
|
|
down.Store(false)
|
|
if _, err := p.Fetch(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ = Find("unrecorded")
|
|
if want := []string{"gpt-a", "claude-b"}; !slices.Equal(p.Models, want) {
|
|
t.Fatalf("picks after every base answered: %v, want %v", p.Models, want)
|
|
}
|
|
}
|
|
|
|
// An endpoint that has never answered keeps nothing. The parts saved with
|
|
// the list say which endpoint listed what, and an endpoint in none of them
|
|
// is one that never answered: the whole list saved before is not its own,
|
|
// and a model the endpoint that does answer has dropped is gone, however
|
|
// many rounds the other one stays at 404. Reading an absent part as an
|
|
// empty one instead fixed that guess as what the endpoint listed, and the
|
|
// model could never be dropped again (#904, yetone's review of #1006).
|
|
func TestFetchDropsAModelWhileAnEndpointHasNeverAnswered(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
var list atomic.Value
|
|
list.Store(`{"data":[{"id":"gpt-a"},{"id":"gpt-b"}]}`)
|
|
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(list.Load().(string)))
|
|
}))
|
|
defer chat.Close()
|
|
// the Anthropic base never answers, from the first round on: no part
|
|
// of the list is ever its own
|
|
anthropic := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
http.NotFound(w, r)
|
|
}))
|
|
defer anthropic.Close()
|
|
|
|
if err := Save(Provider{
|
|
ID: "never",
|
|
Name: "Never",
|
|
Chat: chat.URL + "/v1",
|
|
Anthropic: anthropic.URL + "/v1",
|
|
Key: "sk-n",
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fetch := func() error {
|
|
p, err := Find("never")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = p.Fetch(context.Background())
|
|
return err
|
|
}
|
|
// round 0: both models listed by the one endpoint that answers, and
|
|
// both picked
|
|
if err := fetch(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ := Find("never")
|
|
p.Models = []string{"gpt-a", "gpt-b"}
|
|
if err := Save(*p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
live, _, ok := p.live()
|
|
if !ok {
|
|
t.Fatal("no list kept after the first round")
|
|
}
|
|
if ids, want := idsOf(live), []string{"gpt-a", "gpt-b"}; !slices.Equal(ids, want) {
|
|
t.Fatalf("list after the first round: %v", ids)
|
|
}
|
|
|
|
// the Chat base has dropped gpt-b. The Anthropic base, which never
|
|
// answered, has no say in it: it is gone from the second round on, and
|
|
// stays gone however long that base keeps answering 404.
|
|
list.Store(`{"data":[{"id":"gpt-a"}]}`)
|
|
for round := 1; round <= 4; round++ {
|
|
if err := fetch(); err != nil {
|
|
t.Fatalf("round %d: %v", round, err)
|
|
}
|
|
p, _ = Find("never")
|
|
if want := []string{"gpt-a"}; !slices.Equal(p.Models, want) {
|
|
t.Fatalf("round %d picks: %v, want %v", round, p.Models, want)
|
|
}
|
|
live, _, ok = p.live()
|
|
if !ok {
|
|
t.Fatalf("round %d: no list kept", round)
|
|
}
|
|
if ids, want := idsOf(live), []string{"gpt-a"}; !slices.Equal(ids, want) {
|
|
t.Fatalf("round %d list: %v, want %v", round, ids, want)
|
|
}
|
|
// an endpoint that never answered is not saved as one that listed
|
|
// nothing: it has said nothing at all, and a part here would be a
|
|
// guess fixed as what it listed
|
|
sides, _, ok := catalog.LiveSplit("never")
|
|
if !ok {
|
|
t.Fatalf("round %d: no parts kept", round)
|
|
}
|
|
// the Anthropic base is kept as the root /v1/messages is asked at
|
|
// (normalize), which is the key its part is saved under
|
|
if _, saved := sides[anthropic.URL]; saved {
|
|
t.Fatalf("round %d: a part was saved for an endpoint that never answered", round)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A key that can't be asked at any of its bases keeps the models it saw
|
|
// last time, and they keep the bases that listed them: the next time that
|
|
// key answers at its Chat base while its Anthropic base is still down, the
|
|
// Claude model only that key saw is that base's own, and is kept as a
|
|
// single key's failed base is. Kept as a whole list with no part saying
|
|
// where it came from it belongs to no base at all, and is read as gone the
|
|
// first time that key answers half-way (#904, yetone's review of #1006).
|
|
func TestFetchKeepsAModelsBaseAfterItsKeyFailedWhole(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
// each key has a Claude model of its own, so claude-b is one base's own
|
|
// and of the second key alone
|
|
var chatDown, anthropicDown, swapped atomic.Bool
|
|
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if chatDown.Load() && r.Header.Get("Authorization") == "Bearer sk-2" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(`{"data":[{"id":"gpt-a"}]}`))
|
|
}))
|
|
defer chat.Close()
|
|
anthropic := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if r.Header.Get("Authorization") != "Bearer sk-2" {
|
|
w.Write([]byte(`{"data":[{"id":"claude-1"}]}`))
|
|
return
|
|
}
|
|
if anthropicDown.Load() {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if swapped.Load() {
|
|
w.Write([]byte(`{"data":[{"id":"claude-c"}]}`))
|
|
return
|
|
}
|
|
w.Write([]byte(`{"data":[{"id":"claude-b"}]}`))
|
|
}))
|
|
defer anthropic.Close()
|
|
|
|
if err := Save(Provider{
|
|
ID: "wholekey",
|
|
Name: "Whole key",
|
|
Chat: chat.URL + "/v1",
|
|
Anthropic: anthropic.URL + "/v1",
|
|
Key: "sk-1",
|
|
Keys: []KeyAccount{{Key: "sk-2"}},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fetch := func() ([]string, []string) {
|
|
p, err := Find("wholekey")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := p.Fetch(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ = Find("wholekey")
|
|
live, _, ok := p.live()
|
|
if !ok {
|
|
t.Fatal("no list kept")
|
|
}
|
|
return idsOf(live), p.Models
|
|
}
|
|
all := []string{"gpt-a", "claude-1", "claude-b"}
|
|
|
|
// round 0: every base of every key answers
|
|
got, _ := fetch()
|
|
if !slices.Equal(got, all) {
|
|
t.Fatalf("list after the first round: %v, want %v", got, all)
|
|
}
|
|
p, _ := Find("wholekey")
|
|
p.Models = []string{"gpt-a", "claude-b"}
|
|
if err := Save(*p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// round 1: the second key can't be asked at any of its bases, so its
|
|
// Claude model is kept from the list saved before
|
|
chatDown.Store(true)
|
|
anthropicDown.Store(true)
|
|
got, _ = fetch()
|
|
if !slices.Equal(got, all) {
|
|
t.Fatalf("list after the second key failed whole: %v, want %v", got, all)
|
|
}
|
|
|
|
// round 2: that key answers at its Chat base again while its Anthropic
|
|
// base is still down
|
|
chatDown.Store(false)
|
|
got, picks := fetch()
|
|
if !slices.Equal(got, all) {
|
|
t.Fatalf("list after the second key answered half-way: %v, want %v", got, all)
|
|
}
|
|
if !slices.Equal(picks, []string{"gpt-a", "claude-b"}) {
|
|
t.Fatalf("picks after the second key answered half-way: %v, want gpt-a and claude-b", picks)
|
|
}
|
|
|
|
// round 3: that key's Anthropic base answers again and has dropped the
|
|
// model, which is then gone: keeping it is not keeping it for good
|
|
anthropicDown.Store(false)
|
|
swapped.Store(true)
|
|
got, picks = fetch()
|
|
if want := []string{"gpt-a", "claude-1", "claude-c"}; !slices.Equal(got, want) {
|
|
t.Fatalf("list after that base answered again: %v, want %v", got, want)
|
|
}
|
|
if !slices.Equal(picks, []string{"gpt-a"}) {
|
|
t.Fatalf("picks after that base answered again: %v, want gpt-a", picks)
|
|
}
|
|
}
|
|
|
|
// The other way round for the same rule, which is what keeps sides saying
|
|
// what the bases listed: a model kept for a key that could not be asked at
|
|
// all belongs to the bases that listed it, but not to one that answered
|
|
// this round for another of the model's keys and no longer serves it. A
|
|
// part here said the Anthropic base still served claude-b, and the fetch
|
|
// after that read it as a base that couldn't be asked, holding a model its
|
|
// vendor had dropped for one round longer (yetone's review of #1006, found
|
|
// on the path TestFetchKeepsAModelsBaseAfterItsKeyFailedWhole fixed).
|
|
//
|
|
// It is the same two keys as that test, and the difference is in one thing:
|
|
// there claude-b is only the second key's, so the Anthropic base never
|
|
// listed it for the first one and its answering says nothing about it. Here
|
|
// the first key saw claude-b too, and this round stopped being served it.
|
|
func TestFetchDropsAModelABaseStoppedServingWhileAKeyIsDown(t *testing.T) {
|
|
isolate(t)
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
t.Setenv("PATH", h)
|
|
for _, v := range agentenv.Vars {
|
|
t.Setenv(v, "")
|
|
}
|
|
|
|
var secondDown, dropped, anthDown atomic.Bool
|
|
chat := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if secondDown.Load() && r.Header.Get("Authorization") == "Bearer sk-2" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Write([]byte(`{"data":[{"id":"gpt-a"}]}`))
|
|
}))
|
|
defer chat.Close()
|
|
// both keys are served claude-b to begin with; the drop below is the
|
|
// vendor stopping both of them
|
|
anthropic := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if anthDown.Load() {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if r.Header.Get("Authorization") == "Bearer sk-2" && secondDown.Load() {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if dropped.Load() {
|
|
w.Write([]byte(`{"data":[{"id":"claude-1"}]}`))
|
|
return
|
|
}
|
|
w.Write([]byte(`{"data":[{"id":"claude-1"},{"id":"claude-b"}]}`))
|
|
}))
|
|
defer anthropic.Close()
|
|
|
|
if err := Save(Provider{
|
|
ID: "stopped",
|
|
Name: "Stopped",
|
|
Chat: chat.URL + "/v1",
|
|
Anthropic: anthropic.URL + "/v1",
|
|
Key: "sk-1",
|
|
Keys: []KeyAccount{{Key: "sk-2"}},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fetch := func() ([]string, []string) {
|
|
p, err := Find("stopped")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := p.Fetch(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ = Find("stopped")
|
|
live, _, ok := p.live()
|
|
if !ok {
|
|
t.Fatal("no list kept")
|
|
}
|
|
return idsOf(live), p.Models
|
|
}
|
|
|
|
// round 0: both keys see claude-b
|
|
got, _ := fetch()
|
|
if want := []string{"gpt-a", "claude-1", "claude-b"}; !slices.Equal(got, want) {
|
|
t.Fatalf("list after the first round: %v, want %v", got, want)
|
|
}
|
|
p, _ := Find("stopped")
|
|
p.Models = []string{"gpt-a", "claude-b"}
|
|
if err := Save(*p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// round 1: the second key is down at both of its bases, so claude-b is
|
|
// kept for it, and in the same round the Anthropic base answers for the
|
|
// first key without listing it. The base is not that model's any more,
|
|
// and no part says it is.
|
|
secondDown.Store(true)
|
|
dropped.Store(true)
|
|
got, _ = fetch()
|
|
if want := []string{"gpt-a", "claude-1", "claude-b"}; !slices.Equal(got, want) {
|
|
t.Fatalf("list while a key is down and the base dropped a model: %v, want %v", got, want)
|
|
}
|
|
sides, _, ok := catalog.LiveSplit("stopped")
|
|
if !ok {
|
|
t.Fatal("no parts kept")
|
|
}
|
|
if ids := idsOf(sides[anthropic.URL]); !slices.Equal(ids, []string{"claude-1"}) {
|
|
t.Fatalf("part of the Anthropic base: %v, want claude-1 alone", ids)
|
|
}
|
|
|
|
// round 2: that key is asked again and its Anthropic base is down for
|
|
// both keys, so nothing keeps claude-b and it is gone
|
|
secondDown.Store(false)
|
|
anthDown.Store(true)
|
|
got, picks := fetch()
|
|
if want := []string{"gpt-a", "claude-1"}; !slices.Equal(got, want) {
|
|
t.Fatalf("list after the base went down for every key: %v, want %v", got, want)
|
|
}
|
|
if !slices.Equal(picks, []string{"gpt-a"}) {
|
|
t.Fatalf("picks after the base went down for every key: %v, want gpt-a", picks)
|
|
}
|
|
|
|
// round 3: and it stays gone once the base answers again without it
|
|
anthDown.Store(false)
|
|
got, picks = fetch()
|
|
if want := []string{"gpt-a", "claude-1"}; !slices.Equal(got, want) {
|
|
t.Fatalf("list after the base answered again: %v, want %v", got, want)
|
|
}
|
|
if !slices.Equal(picks, []string{"gpt-a"}) {
|
|
t.Fatalf("picks after the base answered again: %v, want gpt-a", picks)
|
|
}
|
|
}
|