@@ -690,6 +690,137 @@ describe("Config", () => {
690690 ) ,
691691 )
692692
693+ it . live ( "logged-out wellknown endpoint does not shadow still-authenticated endpoints" , ( ) =>
694+ Effect . acquireUseRelease (
695+ Effect . promise ( ( ) => tmpdir ( ) ) ,
696+ ( tmp ) => {
697+ const keys = new Map < string , string > ( )
698+ const serve = ( env : string , key : string ) =>
699+ Bun . serve ( {
700+ port : 0 ,
701+ fetch ( request ) {
702+ const url = new URL ( request . url )
703+ if ( url . pathname === "/.well-known/opencode" )
704+ return Response . json ( {
705+ auth : { command : [ "login" ] , env } ,
706+ remote_config : { url : `${ url . origin } /config` , headers : { authorization : `Bearer {env:${ env } }` } } ,
707+ } )
708+ if ( request . headers . get ( "authorization" ) !== `Bearer ${ key } ` )
709+ return new Response ( "Unauthorized" , { status : 401 } )
710+ return Response . json ( {
711+ config : {
712+ provider : {
713+ // v1 provider shape: headers live under options and migrate to
714+ // the top-level headers overlay.
715+ shared : {
716+ npm : "@ai-sdk/openai-compatible" ,
717+ options : { headers : { "x-api-key" : `{env:${ env } }` } } ,
718+ models : { } ,
719+ } ,
720+ } ,
721+ } ,
722+ } )
723+ } ,
724+ } )
725+ const serverA = serve ( "TOKEN_A" , "key-a" )
726+ const serverB = serve ( "TOKEN_B" , "key-b" )
727+ const origins = [ serverA . url . origin , serverB . url . origin ]
728+ const credentialNode = makeGlobalNode ( {
729+ service : Credential . Service ,
730+ layer : Layer . mock ( Credential . Service ) ( {
731+ list : ( integrationID : Integration . ID ) =>
732+ Effect . succeed (
733+ keys . has ( integrationID )
734+ ? [
735+ new Credential . Info ( {
736+ id : Credential . ID . create ( ) ,
737+ integrationID,
738+ label : "default" ,
739+ value : Credential . Key . make ( { type : "key" , key : keys . get ( integrationID ) ! } ) ,
740+ } ) ,
741+ ]
742+ : [ ] ,
743+ ) ,
744+ } ) ,
745+ deps : [ ] ,
746+ } )
747+ const global = path . join ( tmp . path , "global" )
748+ const project = path . join ( tmp . path , "project" )
749+ const apiKey = ( entries : readonly Entry [ ] ) =>
750+ Config . latest ( entries , "providers" ) ?. [ "shared" ] ?. headers ?. [ "x-api-key" ]
751+ return Effect . gen ( function * ( ) {
752+ yield * Effect . promise ( ( ) => fs . mkdir ( project , { recursive : true } ) )
753+ const layer = AppNodeBuilder . build ( LayerNode . group ( [ Config . node , Bus . node , WellKnown . node ] ) , [
754+ Location . node . replace (
755+ Layer . succeed (
756+ Location . Service ,
757+ Location . Service . of ( location ( { directory : AbsolutePath . make ( project ) } ) ) ,
758+ ) ,
759+ ) ,
760+ Global . node . replace ( Global . layerWith ( { config : global , home : path . join ( global , "home" ) } ) ) ,
761+ Credential . node . replace ( credentialNode ) ,
762+ Watcher . node . replace ( Watcher . testLayer ) ,
763+ ] )
764+ yield * Effect . gen ( function * ( ) {
765+ const config = yield * Config . Service
766+ const bus = yield * Bus . Service
767+ const wellknown = yield * WellKnown . Service
768+ // Runs a mutation, then waits for the config reload it triggers.
769+ const reloadAfter = < A , E > ( name : string , mutation : Effect . Effect < A , E > ) =>
770+ Effect . gen ( function * ( ) {
771+ const reloaded = yield * bus
772+ . subscribe ( Event . Updated )
773+ . pipe ( Stream . take ( 1 ) , Stream . runCollect , Effect . forkScoped ( { startImmediately : true } ) )
774+ yield * Effect . promise ( ( ) => fs . writeFile ( path . join ( project , "opencode.json" ) , JSON . stringify ( { name } ) ) )
775+ const result = yield * mutation
776+ yield * Fiber . join ( reloaded )
777+ return result
778+ } )
779+ const login = ( origin : string , key : string ) =>
780+ Effect . gen ( function * ( ) {
781+ const integrationID = Integration . ID . make ( origin )
782+ // The key must exist before wellknown.add: the add triggers the
783+ // entry load, and a logged-out endpoint contributes no documents.
784+ keys . set ( origin , key )
785+ yield * wellknown . add ( origin )
786+ yield * bus . publish (
787+ Credential . Event . Switched ,
788+ { integrationID, credentialID : Credential . ID . create ( ) } ,
789+ { global : true } ,
790+ )
791+ } )
792+ const logout = ( origin : string ) =>
793+ Effect . gen ( function * ( ) {
794+ keys . delete ( origin )
795+ yield * bus . publish (
796+ Credential . Event . Switched ,
797+ { integrationID : Integration . ID . make ( origin ) , credentialID : null } ,
798+ { global : true } ,
799+ )
800+ // Re-run the entry loads with the new credential state.
801+ yield * wellknown . refresh ( )
802+ } )
803+ yield * reloadAfter ( "login-a" , login ( origins [ 0 ] , "key-a" ) )
804+ expect ( apiKey ( yield * config . entries ( ) ) ) . toBe ( "key-a" )
805+ yield * reloadAfter ( "login-b" , login ( origins [ 1 ] , "key-b" ) )
806+ expect ( apiKey ( yield * config . entries ( ) ) ) . toBe ( "key-b" )
807+ // Logging out B must not leave its cached config resolving {env:TOKEN_B}
808+ // to "" and shadowing A's wiring — the failure mode in
809+ // anomalyco/opencode#54645. The config reload is debounced, so wait
810+ // for the update event before asserting.
811+ yield * reloadAfter ( "logout-b" , logout ( origins [ 1 ] ) )
812+ expect ( apiKey ( yield * config . entries ( ) ) ) . toBe ( "key-a" )
813+ } ) . pipe ( Effect . scoped , Effect . provide ( layer ) )
814+ } ) . pipe (
815+ Effect . ensuring (
816+ Effect . promise ( ( ) => Promise . all ( [ serverA . stop ( true ) , serverB . stop ( true ) ] ) ) . pipe ( Effect . asVoid ) ,
817+ ) ,
818+ )
819+ } ,
820+ ( tmp ) => Effect . promise ( ( ) => tmp [ Symbol . asyncDispose ] ( ) ) ,
821+ ) ,
822+ )
823+
693824 it . live ( "logs redacted source-aware diagnostics for every config source" , ( ) => {
694825 const output : Array < Record < string , unknown > > = [ ]
695826 const logger = Logger . map ( Logger . formatStructured , ( entry ) => {
0 commit comments