fix(registry): support gemini handler and strict weak ordering in GetFirstAvailableModel

Open Beginner friendly
#8,147 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
backend

Research direction

Start in internal/registry/model_registry.go at GetFirstAvailableModel, where convertModelToMap output is read with model["id"] and the sort.Slice comparator handles the created timestamp. Check the gemini branch in convertModelToMap to confirm the ID lands in "name". Then run go test -race -count=1 -run TestGetFirstAvailableModel ./internal/registry against the tests in model_registry_first_available_test.go. Done means the gemini lookup succeeds and the mixed-created ordering is deterministic.

Written by the indexing model from the issue text.

Description

Reference PR: #8070 (verified against origin/dev, go test -race + Solaris cross-build passing, all Codex review findings resolved at commit 2aa9b96a).


fix(registry): support gemini handler and strict weak ordering in GetFirstAvailableModel

Summary

Fixes two bugs in ModelRegistry.GetFirstAvailableModel (internal/registry/model_registry.go):

  1. Gemini handler always failed: convertModelToMap(..., "gemini") populates "name" (either model.Name such as "models/gemini-2.5-pro" or model.ID) instead of "id". GetFirstAvailableModel only read model["id"].(string), causing GetFirstAvailableModel("gemini") to always return "no available clients for any model in handler type: gemini".
  2. Broken strict weak ordering in sort.Slice: The comparator returned false whenever !okI || !okJ on model["created"].(int64). When a slice mixes models with and without "created" timestamps, sort.Slice's comparator violates strict weak ordering (incomparability is not transitive), which can prevent the newest model from sorting to the front or cause nondeterministic ordering.

Changes

  • internal/registry/model_registry.go:
    • Added an extractModelID helper inside GetFirstAvailableModel that reads "id" first and falls back to "name" with any "models/" prefix stripped.
    • Updated the sort.Slice comparator to enforce a total/strict weak order: models with a "created" timestamp sort before those without, then by "created" descending (newest first), with lexicographical tie-breaking on the extracted model ID.
  • internal/registry/model_registry_first_available_test.go:
    • Added TestGetFirstAvailableModel_GeminiHandler covering Gemini models with and without Name ("models/..." prefix stripping).
    • Added TestGetFirstAvailableModel_MixedCreatedStrictWeakOrdering covering mixed present/absent created timestamps and deterministic tie-breaking.

Verification

  • gofmt -w internal/registry/model_registry.go internal/registry/model_registry_first_available_test.go
  • go vet ./internal/registry/... — PASS
  • go test -v -race -count=1 -run TestGetFirstAvailableModel ./internal/registry — PASS (TestGetFirstAvailableModel_GeminiHandler, TestGetFirstAvailableModel_MixedCreatedStrictWeakOrdering)
  • go build -o test-output ./cmd/server — PASS
  • GOOS=solaris GOARCH=amd64 CGO_ENABLED=0 go build -o test-output-solaris ./cmd/server — PASS
Changed Files in Reference PR #8070
  • internal/registry/model_registry.go (+19/-14)
  • internal/registry/model_registry_first_available_test.go (+148/-0)
Dominant language
Go
Stars
59.4k
Forks
9.1k
Avg merge
11h 50m
Merged PRs (30d)
67

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from router-for-me/CLIProxyAPI

All issues in router-for-me/CLIProxyAPI

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.