@@ -1929,6 +1929,156 @@ it.layer(
19291929 } ) ,
19301930 ) ;
19311931
1932+ it . effect ( "drops retained models when a rebuild points at a different model source" , ( ) =>
1933+ Effect . gen ( function * ( ) {
1934+ const driver = ProviderDriverKind . make ( "opencode" ) ;
1935+ const instanceId = ProviderInstanceId . make ( "opencode" ) ;
1936+ const readyProvider = {
1937+ instanceId,
1938+ driver,
1939+ status : "ready" ,
1940+ enabled : true ,
1941+ installed : true ,
1942+ auth : { status : "authenticated" } ,
1943+ checkedAt : "2026-06-10T00:00:00.000Z" ,
1944+ version : "2.0.0" ,
1945+ models : [
1946+ {
1947+ slug : "opencode/big-pickle" ,
1948+ name : "Big Pickle" ,
1949+ subProvider : "OpenCode" ,
1950+ isCustom : false ,
1951+ capabilities : null ,
1952+ } ,
1953+ ] ,
1954+ slashCommands : [ ] ,
1955+ skills : [ ] ,
1956+ } as const satisfies ServerProvider ;
1957+ // A catalog still loading reports a warning and no models. Retention keeps
1958+ // the previous models while it is pending, which is only correct when the
1959+ // pending catalog comes from the same server.
1960+ const loadingProvider = {
1961+ ...readyProvider ,
1962+ status : "warning" ,
1963+ auth : { status : "unknown" } ,
1964+ checkedAt : "2026-06-10T00:01:00.000Z" ,
1965+ models : [ ] ,
1966+ message : "OpenCode 2 is still loading its model catalog." ,
1967+ } satisfies ServerProvider ;
1968+ const registryChanges = yield * PubSub . unbounded < void > ( ) ;
1969+ const makeInstance = (
1970+ provider : ServerProvider ,
1971+ modelSourceIdentity : string ,
1972+ ) : ProviderInstance => ( {
1973+ instanceId,
1974+ driverKind : driver ,
1975+ continuationIdentity : {
1976+ driverKind : driver ,
1977+ continuationKey : "opencode:instance:opencode" ,
1978+ } ,
1979+ modelSourceIdentity,
1980+ displayName : undefined ,
1981+ enabled : true ,
1982+ snapshot : {
1983+ resolveMaintenance : ( ) =>
1984+ Effect . succeed (
1985+ makeManualOnlyProviderMaintenanceCapabilities ( {
1986+ provider : driver ,
1987+ packageName : null ,
1988+ } ) ,
1989+ ) ,
1990+ getSnapshot : Effect . succeed ( provider ) ,
1991+ refresh : Effect . succeed ( provider ) ,
1992+ streamChanges : Stream . empty ,
1993+ applyUsageLimits : ( ) => Effect . void ,
1994+ } ,
1995+ orchestrationAdapter : { } as ProviderInstance [ "orchestrationAdapter" ] ,
1996+ textGeneration : { } as ProviderInstance [ "textGeneration" ] ,
1997+ } ) ;
1998+ const instancesRef = yield * Ref . make < ReadonlyArray < ProviderInstance > > ( [
1999+ makeInstance ( readyProvider , "http://127.0.0.1:4096" ) ,
2000+ ] ) ;
2001+ const layerInstanceRegistry = Layer . succeed (
2002+ ProviderInstanceRegistry . ProviderInstanceRegistry ,
2003+ {
2004+ getInstance : ( requestedId ) =>
2005+ Ref . get ( instancesRef ) . pipe (
2006+ Effect . map ( ( instances ) =>
2007+ instances . find ( ( instance ) => instance . instanceId === requestedId ) ,
2008+ ) ,
2009+ ) ,
2010+ listInstances : Ref . get ( instancesRef ) ,
2011+ listUnavailable : Effect . succeed ( [ ] ) ,
2012+ streamChanges : Stream . fromPubSub ( registryChanges ) ,
2013+ subscribeChanges : PubSub . subscribe ( registryChanges ) ,
2014+ } ,
2015+ ) ;
2016+ const scope = yield * Scope . make ( ) ;
2017+ yield * Effect . addFinalizer ( ( ) => Scope . close ( scope , Exit . void ) ) ;
2018+ const runtimeServices = yield * Layer . build (
2019+ ProviderRegistry . layer . pipe (
2020+ Layer . provideMerge ( layerInstanceRegistry ) ,
2021+ Layer . provideMerge (
2022+ ServerConfig . layerTest ( process . cwd ( ) , {
2023+ prefix : "t3-provider-registry-model-source-" ,
2024+ } ) ,
2025+ ) ,
2026+ Layer . provideMerge ( NodeServices . layer ) ,
2027+ ) ,
2028+ ) . pipe ( Scope . provide ( scope ) ) ;
2029+
2030+ yield * Effect . gen ( function * ( ) {
2031+ const registry = yield * ProviderRegistry . ProviderRegistry ;
2032+ const settleOn = ( expectedCheckedAt : string ) =>
2033+ Effect . gen ( function * ( ) {
2034+ let current = yield * registry . getProviders ;
2035+ for (
2036+ let attempt = 0 ;
2037+ attempt < 5000 && current [ 0 ] ?. checkedAt !== expectedCheckedAt ;
2038+ attempt += 1
2039+ ) {
2040+ // `it.effect` runs on a test clock, so this must not sleep:
2041+ // real-time sleeps never advance here.
2042+ yield * Effect . yieldNow ;
2043+ current = yield * registry . getProviders ;
2044+ }
2045+ assert . strictEqual (
2046+ current [ 0 ] ?. checkedAt ,
2047+ expectedCheckedAt ,
2048+ "the rebuilt instance was never published" ,
2049+ ) ;
2050+ return current ;
2051+ } ) ;
2052+
2053+ const publishRebuild = ( instance : ProviderInstance ) =>
2054+ Effect . gen ( function * ( ) {
2055+ yield * Ref . set ( instancesRef , [ instance ] ) ;
2056+ yield * PubSub . publish ( registryChanges , undefined ) ;
2057+ } ) ;
2058+
2059+ yield * publishRebuild ( makeInstance ( readyProvider , "http://127.0.0.1:4096" ) ) ;
2060+ const published = yield * settleOn ( readyProvider . checkedAt ) ;
2061+ assert . deepStrictEqual ( published [ 0 ] ?. models , readyProvider . models ) ;
2062+
2063+ // Same server, still loading: the pending catalog is a continuation of
2064+ // the one already listed, so its models stay visible.
2065+ yield * publishRebuild ( makeInstance ( loadingProvider , "http://127.0.0.1:4096" ) ) ;
2066+ const sameSource = yield * settleOn ( loadingProvider . checkedAt ) ;
2067+ assert . deepStrictEqual ( sameSource [ 0 ] ?. models , readyProvider . models ) ;
2068+
2069+ // Different server, still loading: the pending catalog belongs to a
2070+ // server whose models were never listed, so the old ones must go.
2071+ const otherSourceProvider = {
2072+ ...loadingProvider ,
2073+ checkedAt : "2026-06-10T00:02:00.000Z" ,
2074+ } satisfies ServerProvider ;
2075+ yield * publishRebuild ( makeInstance ( otherSourceProvider , "http://127.0.0.1:5099" ) ) ;
2076+ const otherSource = yield * settleOn ( otherSourceProvider . checkedAt ) ;
2077+ assert . deepStrictEqual ( otherSource [ 0 ] ?. models , [ ] ) ;
2078+ } ) . pipe ( Effect . provide ( runtimeServices ) ) ;
2079+ } ) ,
2080+ ) ;
2081+
19322082 it . effect ( "refreshes OpenCode catalogs and preserves other providers" , ( ) =>
19332083 Effect . gen ( function * ( ) {
19342084 const codexDriver = ProviderDriverKind . make ( "codex" ) ;
0 commit comments