@@ -7,7 +7,6 @@ import { IntegrationConnection } from "../../integration/connection.js"
77import { Credential } from "../../credential.js"
88import { CopilotModels } from "../../github-copilot/models.js"
99import { App } from "../../app.js"
10- import { Flag } from "../../flag.js"
1110import { Integration } from "../../integration.js"
1211import { Model } from "../../model.js"
1312import { Agent } from "../../agent.js"
@@ -171,10 +170,13 @@ export const GithubCopilotPlugin = define({
171170 const cache = yield * SharedCache . Service
172171 const http = yield * HttpClient . HttpClient
173172 const outbound = yield * FetchHttpClient . Fetch
174- const auto = yield * Flag . enabled ( "copilot_auto" )
175- // Each Session keeps the model Copilot's Auto router picked until the pick's token nears expiry or the Session is
176- // compacted. The token unlocks the picked model and its Auto discount, and Copilot rejects it for any other model.
177- const routes = new Map < Session . ID , { model : string ; token ?: string ; until : number ; stale ?: boolean } > ( )
173+ // Each Session keeps the model Copilot's Auto router picked until its token nears expiry, the Session is compacted,
174+ // or the account switches. The token unlocks the pick and its Auto discount; Copilot rejects it for any other model.
175+ const routes = new Map < Session . ID , { model : string ; token ?: string ; until : number } > ( )
176+ yield * ProviderRefresh . onSwitch (
177+ "github-copilot" ,
178+ Effect . sync ( ( ) => routes . clear ( ) ) ,
179+ )
178180 const discovered = yield * ProviderRefresh . start ( {
179181 integrationID : "github-copilot" ,
180182 initial : { } as {
@@ -258,7 +260,7 @@ export const GithubCopilotPlugin = define({
258260 const loaded = discovered . current
259261 if ( loaded . models ) {
260262 const url = loaded . baseURL ?? baseURL ( )
261- const router = auto ? CopilotModels . autoModel ( url , loaded . models ) : undefined
263+ const router = CopilotModels . autoModel ( url , loaded . models )
262264 evt . add ( {
263265 info : item . provider ,
264266 models : [
@@ -362,28 +364,21 @@ export const GithubCopilotPlugin = define({
362364 if ( evt . model . id !== CopilotModels . auto || evt . kind === "title" ) return
363365 const now = yield * Clock . currentTimeMillis
364366 const cached = routes . get ( evt . sessionID )
365- const current =
366- cached && cached . until > now && ! ( cached . stale && evt . kind === "primary" )
367- ? cached
368- : yield * route ( evt . sessionID )
367+ const current = cached && cached . until > now ? cached : yield * route ( evt . sessionID )
369368 if ( ! current ) return
370- if ( current !== cached ) {
371- routes . delete ( evt . sessionID )
372- routes . set ( evt . sessionID , current )
373- const oldest = routes . keys ( ) . next ( ) . value
374- if ( routes . size > 500 && oldest ) routes . delete ( oldest )
375- }
369+ routes . set ( evt . sessionID , current )
376370 evt . target = Model . Ref . make ( { providerID : Provider . ID . githubCopilot , id : Model . ID . make ( current . model ) } )
377371 } ) ,
378372 { providerID : Provider . ID . githubCopilot } ,
379373 )
380- // Compaction rewrites the prompt, so the next request routes again, like Copilot's own clients.
374+ // Compaction rewrites the prompt, so the request after it routes again, like Copilot's own clients. The compaction
375+ // request itself still carries the current pick's token.
381376 yield * ctx . session . hook (
382377 "compaction" ,
383378 ( evt ) =>
384379 Effect . sync ( ( ) => {
385380 const route = routes . get ( evt . sessionID )
386- if ( route ) route . stale = true
381+ if ( route ) route . until = 0
387382 } ) ,
388383 { providerID : Provider . ID . githubCopilot } ,
389384 )
0 commit comments