Files
magpie/internal/provider/copilot_long_test.go
T
yetone 7670295019 copilot: list each model's long-context tier, as Copilot Chat asks /models (gtliuu on X)
gtliuu on X: Copilot's Claude Opus 5.5 has a 200K and a long tier, and magpie
offered only 200K. magpie asked /models at X-GitHub-Api-Version 2026-01-09,
and at that version Copilot lists only the default tier's
max_prompt_tokens. Copilot Chat 0.68 never sends 2026-01-09:
@vscode/copilot-api 0.5.2's _mixinHeaders sets 2026-08-01 on every Copilot
API request, Models included, over networking.ts's 2026-01-09. At
2026-06-01 and later, /models gives each model its whole window (Opus 5.5
1M) and billing.token_prices with a default tier and a long_context tier.

- The editors' sign-in asks GET /models at 2026-08-01
  (copilotModelsVersion). The Copilot CLI's sign-in keeps its own
  2026-07-01, which already lists the tiers. Chat requests are unchanged:
  Copilot served prompts past the default tier at 2026-01-09. The other
  requests still on 2026-01-09 are #6571.
- The window follows Copilot Chat (languageModelAccess.ts and
  agentIntent.ts). A long tier at the default's prices gives the whole
  window: Claude Opus 5.5 1M, and the Sonnet, Fable and Gemini models
  936K or 983K. A long tier with a surcharge keeps the default tier's
  prompt as the window agents are told. The whole window becomes
  MaxContext, which the user can raise the window to. GPT-6 Luna gets
  272K and 872K, and Claude Haiku 5.5 100K and 936K. The whole window is
  the limits' max_prompt_tokens, not the long tier's prompt: Grok 4.7's
  long tier says 500000, but Copilot takes 372000.
- Price: Copilot's own list (AIU per batch_size tokens / 100 = USD per M)
  is the list price, ahead of models.dev. A long tier with a surcharge
  becomes a Tier above the default's prompt. Both 2026-06-01's field
  names (cache_price, context_max) and 2026-08-01's are read. GPT-4.1's
  all-zero list is no price. A flat, older list changes nothing.

Run (base origin/main 7b50e1f2c):
- go test -tags nogui -count=1 -timeout 45m ./internal/provider/...
  ./internal/catalog/... ./internal/gateway/... ./internal/usage/... on
  mote, in a sandbox HOME: all ok (provider 139s, catalog 3s, gateway
  918s, usage 42s). Not the whole ./...: mote's suite lock had six suites
  queued, and mica's disk is full.
- GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., and the
  linux and windows builds: ok.
- gofmt -l: clean.
- Fixtures are the owner's account's real /models replies at 2026-01-09,
  2026-06-01 and 2026-08-01, trimmed to four models.
- Real use: a -tags dev build in a sandbox HOME with only
  github-copilot/apps.json copied, on 127.0.0.1:3476. Its /v1/models
  lists copilot/claude-opus-5.5 at 1000000 (200000 before) and
  gpt-6-luna at 272000, both priced from Copilot's list. A
  584,849-token prompt to copilot/claude-opus-5.5 on /v1/messages got
  200 end_turn, billed by Copilot at 233.9 AIU (400 an M, the default
  tier's price).

Broken by hand, each red:
- /models asked without copilotModelsVersion: TestCopilotListsTheLongContextTier.
- tiers not applied: that test and TestCopilotTiersAtTheFirstTieredVersion.
- the long tier's prompt over the limits': red on Grok 4.7's 500000.
- context_max not read: TestCopilotTiersAtTheFirstTieredVersion.
- copilotListPrice off: TestCopilotListPriceIsCopilots.

Not tried: a request on a surcharged model raised past its default
tier (GPT-6 Luna over 272K), or a Business or Enterprise account's list.
2026-11-14 03:37:38 +08:00

173 lines
6.4 KiB
Go

package provider
import (
"context"
"net/http"
"net/http/httptest"
"os"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
)
// copilotVersionedAPI answers /models with the real reply Copilot gave
// at the version asked (testdata/copilot-models-<version>.json: the
// owner's account, four of its models), and keeps the versions asked.
func copilotVersionedAPI(t *testing.T) (*httptest.Server, func() []string) {
t.Helper()
var mu sync.Mutex
var asked []string
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v := r.Header.Get("X-GitHub-Api-Version")
mu.Lock()
asked = append(asked, v)
mu.Unlock()
b, err := os.ReadFile("testdata/copilot-models-" + v + ".json")
if err != nil {
// a version these fixtures don't have: the oldest's reply
b, _ = os.ReadFile("testdata/copilot-models-2026-01-09.json")
}
w.Write(b)
}))
t.Cleanup(api.Close)
return api, func() []string {
mu.Lock()
defer mu.Unlock()
return append([]string(nil), asked...)
}
}
func copilotTestModels(t *testing.T, s copilotSession) map[string]catalog.Model {
t.Helper()
copilotSessions = map[string]copilotSession{"gho_long": s}
ms, err := copilotModels(context.Background(), copilotApp{User: "octocat", Token: "gho_long"})
if err != nil {
t.Fatal(err)
}
got := map[string]catalog.Model{}
for _, m := range ms {
got[m.ID] = m
}
return got
}
func copilotTestSession(api string, direct bool) copilotSession {
s := copilotSession{Token: "sess", ExpiresAt: time.Now().Add(time.Hour).Unix(), direct: direct}
s.Endpoints.API = api
return s
}
// Copilot Chat asks /models at the version its @vscode/copilot-api sets
// on every request (2026-08-01), where a model's long-context tier is
// listed; at 2026-01-09 Claude Opus 5.5 was 200K alone (gtliuu on X). A
// long tier at the default's prices is the window agents are told
// (Opus 5.5's 1M). One that costs more, as Copilot Chat does, keeps the
// default tier's prompt as the window and the long one as the most it
// may be raised to (GPT-6 Luna's 272K and 872K), priced above 272K at
// the long tier's prices.
func TestCopilotListsTheLongContextTier(t *testing.T) {
signIn(t)
api, asked := copilotVersionedAPI(t)
got := copilotTestModels(t, copilotTestSession(api.URL, false))
if v := asked(); len(v) != 1 || v[0] != "2026-08-01" {
t.Fatalf("/models asked at %q; want 2026-08-01", v)
}
p := Provider{ID: "copilot"}
opus := got["claude-opus-5.5"]
if w := p.WindowOf(opus); w != 1000000 {
t.Errorf("claude-opus-5.5 window %d; want 1000000", w)
}
if opus.Price == nil || opus.Price.Input != 4 || opus.Price.Output != 20 || opus.Price.CacheRead != 0.2 ||
opus.Price.CacheWrite != 5 || opus.Price.CacheWrite1h != 8 || len(opus.Price.Tiers) != 0 {
t.Errorf("claude-opus-5.5 price %+v; want 4/20, cache 0.2/5/8, no tier", opus.Price)
}
luna := got["gpt-6-luna"]
if w := p.WindowOf(luna); w != 272000 || luna.MaxContext != 872000 {
t.Errorf("gpt-6-luna window %d most %d; want 272000 and 872000", w, luna.MaxContext)
}
want := catalog.Price{Input: 0.1, Output: 0.5, CacheRead: 0.01, CacheWrite: 0.125,
Tiers: []catalog.Tier{{Above: 272000, Input: 0.2, Output: 0.75, CacheRead: 0.02, CacheWrite: 0.25}}}
if luna.Price == nil || !samePrice(*luna.Price, want) {
t.Errorf("gpt-6-luna price %+v; want %+v", luna.Price, want)
}
// raised to its long tier by the user, it is told that
p.Contexts = map[string]int{"gpt-6-luna": 872000}
if w := p.WindowOf(luna); w != 872000 {
t.Errorf("gpt-6-luna set to 872000: window %d", w)
}
// Grok 4.7's long tier says 500000, its limits 372000: Copilot takes
// the limits' prompt
if m := got["grok-4.7"]; m.Context != 200000 || m.MaxContext != 372000 {
t.Errorf("grok-4.7 window %d most %d; want 200000 and 372000", m.Context, m.MaxContext)
}
// GPT-4.1 lists every price 0 (batch_size 0): not a price at all
if m := got["gpt-4.1"]; m.Price != nil || m.Context != 128000 {
t.Errorf("gpt-4.1 price %+v window %d; want none and 128000", m.Price, m.Context)
}
}
// 2026-06-01 names a tier's prompt context_max and its cache read
// cache_price; read the same.
func TestCopilotTiersAtTheFirstTieredVersion(t *testing.T) {
b, err := os.ReadFile("testdata/copilot-models-2026-06-01.json")
if err != nil {
t.Fatal(err)
}
signIn(t)
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write(b) }))
defer api.Close()
luna := copilotTestModels(t, copilotTestSession(api.URL, false))["gpt-6-luna"]
if luna.Context != 272000 || luna.MaxContext != 872000 || luna.Price == nil ||
luna.Price.CacheRead != 0.01 || len(luna.Price.Tiers) != 1 || luna.Price.Tiers[0].CacheRead != 0.02 {
t.Errorf("gpt-6-luna at 2026-06-01: window %d most %d price %+v", luna.Context, luna.MaxContext, luna.Price)
}
}
// The Copilot CLI's sign-in asks /models at the CLI's own version, as
// the CLI does; and a reply in the older flat shape changes nothing.
func TestCopilotCLIKeepsItsModelsVersion(t *testing.T) {
signIn(t)
api, asked := copilotVersionedAPI(t)
got := copilotTestModels(t, copilotTestSession(api.URL, true))
if v := asked(); len(v) != 1 || v[0] != copilotCLIHeaders["X-GitHub-Api-Version"] {
t.Fatalf("CLI /models asked at %q; want %q", v, copilotCLIHeaders["X-GitHub-Api-Version"])
}
// served the 2026-01-09 reply: flat nano-AIU prices, no tiers
if m := got["claude-opus-5.5"]; m.Context != 200000 || m.MaxContext != 0 || m.Price != nil {
t.Errorf("flat reply: claude-opus-5.5 window %d most %d price %+v; want 200000, 0, none", m.Context, m.MaxContext, m.Price)
}
}
// What Copilot listed the model at is its list price, over models.dev's.
func TestCopilotListPriceIsCopilots(t *testing.T) {
signIn(t)
luna := catalog.Model{ID: "gpt-6-luna", Context: 272000, MaxContext: 872000,
Price: &catalog.Price{Input: 0.1, Output: 0.5, Tiers: []catalog.Tier{{Above: 272000, Input: 0.2, Output: 0.75}}}}
if err := catalog.SaveLive("copilot", copilotBase, []catalog.Model{luna}); err != nil {
t.Fatal(err)
}
pr, ok := Provider{ID: "copilot"}.ListPrice("gpt-6-luna")
if !ok || !samePrice(pr, *luna.Price) {
t.Errorf("ListPrice %+v %v; want Copilot's %+v", pr, ok, *luna.Price)
}
}
func samePrice(a, b catalog.Price) bool {
if a.Input != b.Input || a.Output != b.Output || a.CacheRead != b.CacheRead || a.CacheWrite != b.CacheWrite ||
a.CacheWrite1h != b.CacheWrite1h || len(a.Tiers) != len(b.Tiers) {
return false
}
for i := range a.Tiers {
if a.Tiers[i] != b.Tiers[i] {
return false
}
}
return true
}