Skip to content

fix(providers): paginate the Anthropic models list - #1648

Merged
clark-cant merged 3 commits into
nextlevelbuilder:devfrom
pcbeingused33:c0578-anthropic-models-pagination
Nov 5, 2026
Merged

clark-cant merged 3 commits into
nextlevelbuilder:devfrom
pcbeingused33:c0578-anthropic-models-pagination

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

Summary

fetchAnthropicModels made a single GET /v1/models with no limit or after_id. The Anthropic models API is paginated (20 per page by default, with has_more / last_id), so the provider model selector only ever showed the first page. This requests limit=1000 and follows after_id while has_more is true, with a page cap (20) so a misbehaving upstream cannot loop forever. Error handling and the empty-list result are unchanged.

Type

  • Feature
  • Bug fix
  • Hotfix (targeting main)
  • Refactor
  • Docs
  • CI/CD

Target Branch

dev

Checklist

  • go build ./... passes
  • go build -tags sqliteonly ./... passes (if Go changes)
  • go vet ./... passes (go vet ./internal/http/ passes)
  • Tests pass: go test -race ./... (go test ./internal/http/ -run FetchAnthropicModels passes)
  • Web UI builds: cd ui/web && pnpm build (if UI changes) — no UI changes
  • No hardcoded secrets or credentials
  • SQL queries use parameterized $1, $2 (no string concat) — no SQL changes
  • New user-facing strings added to all 3 locales (en/vi/zh) — no new strings
  • Migration version bumped in internal/upgrade/version.go (if new migration) — no migration

Test Plan

New TestFetchAnthropicModelsFollowsPagination serves 120 models in pages from an httptest server that mimics the API (default page size 20, limit, after_id, has_more, last_id) and expects all 120. Without the change it fails with got 20 models, want 120; with it, it passes.

This change was prepared with the help of an AI assistant (Claude Code), and the author is responsible for the change.

🤖 Generated with Claude Code

fetchAnthropicModels issued a single GET /v1/models without limit or
after_id, so only the first page (20 models by default) reached the model
selector. Request limit=1000 and follow has_more/last_id, with a page cap.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@clark-cant clark-cant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at b4f5d92 (2 files, +119 -28) against dev at 22e71c8, one commit, no prior reviews or comments.

Verdict: approve on the code. Posted as a comment rather than a formal approval because the head carries zero check runs (combined status pending with no statuses, mergeStateStatus UNSTABLE) and this is a first-time contributor, so CI is still waiting on a maintainer workflow approval.

What I verified by execution at the head:

  • go build ./internal/http clean, go vet ./internal/http clean, gofmt -l on both changed files empty.
  • go test ./internal/http -run FetchAnthropicModels -count=1 passes, and the new test does fail against the old single-request code (20 vs 120), so it is a real regression guard for the limit part of the fix.
  • The loop itself is correct. I drove fetchAnthropicModels against a throwaway upstream that ignores the requested limit and hard-caps at 20 per page: 60 models came back in exactly 3 requests, after_id was threaded from last_id each time, and no request carried an unknown cursor. So after_id / has_more / last_id handling is right, not just plausible.

Correctness notes on the change itself:

  • The query shape matches the live contract: limit is documented as 1..1000 with a default of 20, and has_more / last_id are the documented pagination fields, so limit=1000 + after_id=last_id is the right cursor and 1000 is a legal value, not an over-reach.
  • q.Encode() escapes the cursor, which matters because after_id is an opaque id.
  • The last_id == afterID check plus the 20-page cap closes both the non-advancing-upstream and the infinite-loop cases.
  • Closing the body on every path is a genuine improvement: the old code used a single defer, but moving that into a loop would have leaked a body per page.
  • The models == nil fix preserves the previous [] over null JSON shape for the empty case, so the picker contract is unchanged.

Non-blocking notes:

  1. The new test never paginates. Its mock reads the requested limit and honours it, so the code's limit=1000 makes the server return all 120 in one page with has_more: false — I instrumented the mock and counted one upstream request. The test therefore proves that limit=1000 is sent, not that after_id / last_id / has_more are followed, and it would still pass if the loop were deleted and the change reduced to adding the query string. The comment on line 33 says the mock mimics a default page size of 20, but that branch is unreachable at limit=1000. Cheapest fix: cap the mock's page size at 20 unconditionally (or at min(20, requested)) and assert the request count, which makes the existing 120-model expectation actually exercise the cursor. Worth doing before merge: the guards in the loop are the part most likely to rot silently.

  2. A later-page failure discards everything. If page 2 returns non-200 or the transport errors, the function returns nil, err, and handleListProviderModels maps any error to respond([]ModelInfo{}), so a tenant that already received 1000 models on page 1 gets an empty picker. Returning the collected models with a warning would degrade better. Unreachable in practice while the real catalog is ~20 entries and limit=1000 covers it in one page, but it is the one new failure mode this loop introduces.

  3. net/url is now shadowed by the local url := config.DockerLocalhost(base + "/api/tags") in fetchOllamaModels, in this same file. It compiles and is legal Go, but the next person reaching for url.Values in that function gets a confusing "url is not a package" error. Aliasing the import (neturl) or renaming that local keeps the file honest.

  4. limit=1000 is a bare literal while anthropicModelsMaxPages sits above as a named constant. A anthropicModelsPageSize next to it would keep the pair readable and give the test something to assert against.

  5. No CHANGELOG entry. Unreleased → Fixed carries a per-fix line for user-visible bugs (see the #1627 entry), and "the Anthropic model picker only showed the first 20 models" is exactly that shape.

  6. The loop guard covers a stalled cursor but not a duplicated page (upstream ignoring after_id while advancing last_id). Not required by the documented API — a seen set would be the belt-and-braces version if you want it.

Duplicate check: no open PR or issue proposes the same fix, no repo issue reports the 20-model symptom, and this is the first change to provider_models_fetch.go since the Requesty provider landed. The only overlap risk in the open set is 1639/1640, which add new provider types and do not touch this function.

No merge: zero check runs, no approving review, and the workflow gate on this head is still action_required.

— clark-cant (agent), 2026-11-03

Cap the test mock at 20 per page and assert the request count so the
cursor is really exercised, keep earlier pages when a later page fails,
alias net/url, name the page-size constant and add a CHANGELOG entry.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@pcbeingused33

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review. Pushed a commit on top:

  1. The test mock now caps every page at 20 regardless of limit and the test asserts 6 upstream requests, so it fails if the loop is removed.
  2. A failure on a page after the first returns the models already collected (logged with slog.Warn); a first-page failure is still an error. New test covers both.
  3. net/url is imported as neturl.
  4. anthropicModelsPageSize constant next to anthropicModelsMaxPages.
  5. CHANGELOG entry under Unreleased → Fixed.
  6. Left the optional seen set out: the documented API does not need it and the stalled-cursor and page-cap guards bound the loop.

This comment was drafted with Claude Code.

@clark-cant clark-cant left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at 5a207e9 (merge of origin/dev into c0578-anthropic-models-pagination, dev at ed4ce88). This is the dev-sync commit the author pushed at 21:26Z; the last substantive review was at 0e901ba.

Verdict: approve. The merge adds nothing to the change and drops nothing from it.

What I checked on the merge itself:

  • Effective diff unchanged. git diff origin/dev...5a207e9c is still exactly three files, +175/−28 — CHANGELOG.md, internal/http/provider_models_fetch.go, internal/http/provider_models_fetch_test.go — byte-identical to the diff at 0e901ba. The dev sync brought in no unrelated churn and reverted none of the branch's own work.
  • The one conflict was resolved correctly. CHANGELOG.md conflicted because dev now carries the #1662 entry in the same Unreleased → Fixed block. The author kept both: the Anthropic pagination entry sits directly above the queued-run entry, each credited to its own fix, no entry lost and no duplicate section introduced.
  • Prior notes are all in. Named anthropicModelsPageSize / anthropicModelsMaxPages constants, neturl import alias so fetchOllamaModels can keep its local url, mock hard-capped at 20 per page with a requests != total/20 assertion so the cursor is genuinely exercised, partial list retained when a later page fails, and the CHANGELOG line — all present at this head.
  • Executed at the merged head: go test ./internal/http -run FetchAnthropicModels -count=1 passes, gofmt -l on both changed files is empty.

I also approved the gated CI run for this head, so the checks are now real rather than action_required: web and release-versioning are green and go is running. That gate, not the diff, was the only thing standing between this PR and a merge since 2026-11-03.

Only open note carried forward, unchanged and non-blocking: the loop guards a stalled cursor but not a duplicated page (upstream ignoring after_id while still advancing last_id). A seen set would close it; the documented API does not need it.

— clark-cant (agent)

@clark-cant clark-cant added area:providers LLM provider implementations pr:approved-by-maintain Approved by github-maintain automation pr:merge-ready Ready to merge — all gates passed pr:reviewed Reviewed by maintainer labels Nov 5, 2026
@clark-cant
clark-cant merged commit 8a5a10b into nextlevelbuilder:dev Nov 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers LLM provider implementations pr:approved-by-maintain Approved by github-maintain automation pr:merge-ready Ready to merge — all gates passed pr:reviewed Reviewed by maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants