@@ -67,27 +67,37 @@ const layerServerSettingsWithSecrets = () =>
6767 ) ,
6868 ) ;
6969
70- // Runs the service's reads of settings.json through `aroundRead`, so a test can tell when the
71- // watcher has reloaded the file, or hold a reload open.
72- const layerServerSettingsAroundReads = (
73- aroundRead : (
74- read : Effect . Effect < string , PlatformError . PlatformError > ,
75- ) => Effect . Effect < string , PlatformError . PlatformError > ,
76- ) =>
70+ type SettingsFileHook = < A > (
71+ access : Effect . Effect < A , PlatformError . PlatformError > ,
72+ ) => Effect . Effect < A , PlatformError . PlatformError > ;
73+
74+ // Runs the service's reads of settings.json, and the renames that replace it, through hooks, so
75+ // a test can tell when the watcher has reloaded the file or hold a reload open.
76+ const layerServerSettingsAroundFile = ( hooks : {
77+ readonly read ?: SettingsFileHook ;
78+ readonly replace ?: SettingsFileHook ;
79+ } ) =>
7780 ServerSettingsModule . layer . pipe (
7881 Layer . provide ( ServerSecretStore . layer ) ,
7982 Layer . provide (
8083 Layer . effect (
8184 FileSystem . FileSystem ,
8285 Effect . gen ( function * ( ) {
8386 const fs = yield * FileSystem . FileSystem ;
87+ const path = yield * Path . Path ;
8488 const { settingsPath } = yield * ServerConfig . ServerConfig ;
89+ const read = hooks . read ?? ( ( access ) => access ) ;
90+ const replace = hooks . replace ?? ( ( access ) => access ) ;
8591 return FileSystem . FileSystem . of ( {
8692 ...fs ,
87- readFileString : ( path , encoding ) =>
88- path === settingsPath
89- ? aroundRead ( fs . readFileString ( path , encoding ) )
90- : fs . readFileString ( path , encoding ) ,
93+ readFileString : ( filePath , encoding ) =>
94+ filePath === settingsPath
95+ ? read ( fs . readFileString ( filePath , encoding ) )
96+ : fs . readFileString ( filePath , encoding ) ,
97+ rename : ( oldPath , newPath ) =>
98+ path . resolve ( newPath ) === path . resolve ( settingsPath )
99+ ? replace ( fs . rename ( oldPath , newPath ) )
100+ : fs . rename ( oldPath , newPath ) ,
91101 } ) ;
92102 } ) ,
93103 ) ,
@@ -103,9 +113,9 @@ const layerServerSettingsAroundReads = (
103113 ) ;
104114
105115const layerServerSettingsReportingReads = ( reads : Queue . Queue < void > ) =>
106- layerServerSettingsAroundReads ( ( read ) =>
107- read . pipe ( Effect . ensuring ( Queue . offer ( reads , undefined ) ) ) ,
108- ) ;
116+ layerServerSettingsAroundFile ( {
117+ read : ( access ) => access . pipe ( Effect . ensuring ( Queue . offer ( reads , undefined ) ) ) ,
118+ } ) ;
109119
110120const usageScript = {
111121 id : "usage" ,
@@ -512,7 +522,7 @@ it.layer(NodeServices.layer)("server settings", (it) => {
512522 } ) . pipe ( TestClock . withLive ) ,
513523 ) ;
514524
515- it . effect ( "serves the last good settings while a reload of an invalid file is in flight " , ( ) =>
525+ it . effect ( "serves the last good settings to a read made during a reload of an invalid file" , ( ) =>
516526 Effect . gen ( function * ( ) {
517527 const started = yield * Queue . unbounded < void > ( ) ;
518528 const reloads = yield * Latch . make ( true ) ;
@@ -528,21 +538,81 @@ it.layer(NodeServices.layer)("server settings", (it) => {
528538 yield * reloads . close ;
529539 yield * fs . writeFileString ( config . settingsPath , `{ "responseStreamingMode": ` ) ;
530540 yield * Queue . take ( started ) . pipe ( Effect . timeout ( "2 seconds" ) ) ;
531- const during = yield * service . getSettings . pipe ( Effect . timeout ( "2 seconds" ) ) ;
541+ const during = yield * Effect . forkChild ( service . getSettings , { startImmediately : true } ) ;
532542 yield * reloads . open ;
533543
534- assert . equal ( during . responseStreamingMode , "turn" ) ;
544+ assert . equal ( ( yield * Fiber . join ( during ) ) . responseStreamingMode , "turn" ) ;
535545 const after = yield * service . withSettingsSnapshot ( Effect . succeed ) ;
536546 assert . equal ( after . responseStreamingMode , "turn" ) ;
537547 } ) ,
538548 ) . pipe (
539549 Effect . provide (
540- layerServerSettingsAroundReads ( ( read ) =>
541- Queue . offer ( started , undefined ) . pipe (
542- Effect . andThen ( reloads . await ) ,
543- Effect . andThen ( read ) ,
544- ) ,
545- ) ,
550+ layerServerSettingsAroundFile ( {
551+ read : ( access ) =>
552+ Queue . offer ( started , undefined ) . pipe (
553+ Effect . andThen ( reloads . await ) ,
554+ Effect . andThen ( access ) ,
555+ ) ,
556+ } ) ,
557+ ) ,
558+ ) ;
559+ } ) . pipe ( TestClock . withLive ) ,
560+ ) ;
561+
562+ it . effect ( "never pairs a key from a reload with the settings it replaces" , ( ) =>
563+ Effect . gen ( function * ( ) {
564+ const reads = yield * Queue . unbounded < void > ( ) ;
565+ const replacing = yield * Queue . unbounded < void > ( ) ;
566+ const replaces = yield * Latch . make ( true ) ;
567+ yield * Effect . scoped (
568+ Effect . gen ( function * ( ) {
569+ const config = yield * ServerConfig . ServerConfig ;
570+ const fs = yield * FileSystem . FileSystem ;
571+ const service = yield * ServerSettingsModule . ServerSettingsService ;
572+ const opencode = ProviderInstanceId . make ( "opencode" ) ;
573+ const withServer = ( serverUrl : string , serverPassword : string ) =>
574+ JSON . stringify ( {
575+ providerInstances : {
576+ opencode : { driver : "opencode" , config : { serverUrl, serverPassword } } ,
577+ } ,
578+ } ) ;
579+ yield * fs . writeFileString (
580+ config . settingsPath ,
581+ withServer ( "http://127.0.0.1:4096" , "first-password" ) ,
582+ ) ;
583+ // Startup moves the password into the secret store and rewrites the file, which the
584+ // watcher then reloads.
585+ yield * service . start ;
586+ yield * Queue . take ( reads ) ;
587+ yield * Queue . take ( reads ) . pipe ( Effect . timeout ( "2 seconds" ) ) ;
588+ yield * service . withSettingsSnapshot ( ( ) => Effect . void ) ;
589+ yield * Queue . clear ( replacing ) ;
590+
591+ // The reload moves the new password into the store before it rewrites the file.
592+ yield * replaces . close ;
593+ yield * fs . writeFileString (
594+ config . settingsPath ,
595+ withServer ( "http://127.0.0.1:4097" , "second-password" ) ,
596+ ) ;
597+ yield * Queue . take ( replacing ) . pipe ( Effect . timeout ( "2 seconds" ) ) ;
598+ const during = yield * Effect . forkChild ( service . getSettings , { startImmediately : true } ) ;
599+ yield * replaces . open ;
600+
601+ assert . deepInclude ( ( yield * Fiber . join ( during ) ) . providerInstances [ opencode ] ?. config , {
602+ serverUrl : "http://127.0.0.1:4097" ,
603+ serverPassword : "second-password" ,
604+ } ) ;
605+ } ) ,
606+ ) . pipe (
607+ Effect . provide (
608+ layerServerSettingsAroundFile ( {
609+ read : ( access ) => access . pipe ( Effect . ensuring ( Queue . offer ( reads , undefined ) ) ) ,
610+ replace : ( access ) =>
611+ Queue . offer ( replacing , undefined ) . pipe (
612+ Effect . andThen ( replaces . await ) ,
613+ Effect . andThen ( access ) ,
614+ ) ,
615+ } ) ,
546616 ) ,
547617 ) ;
548618 } ) . pipe ( TestClock . withLive ) ,
0 commit comments