@@ -572,16 +572,13 @@ const make = Effect.gen(function* () {
572572 const startedDeferred = yield * Deferred . make < void , ServerSettingsError > ( ) ;
573573 const watcherScope = yield * Scope . make ( "sequential" ) ;
574574 yield * Effect . addFinalizer ( ( ) => Scope . close ( watcherScope , Exit . void ) ) ;
575- // What subscribers last received. A reload that finds the same settings, such as our own
576- // write coming back through the watcher, publishes nothing .
577- const publishedRef = yield * Ref . make ( Option . none < ServerSettings > ( ) ) ;
575+ // The file contents of our last save, set only once the save is published. The watcher skips
576+ // a reload that reads exactly these bytes; any other contents are published as before .
577+ const publishedSaveRef = yield * Ref . make ( Option . none < string > ( ) ) ;
578578 const parseFailureLoggedRef = yield * Ref . make ( false ) ;
579579
580580 const emitChange = ( settings : ServerSettings ) =>
581- Ref . set ( publishedRef , Option . some ( settings ) ) . pipe (
582- Effect . andThen ( PubSub . publish ( changesPubSub , settings ) ) ,
583- Effect . asVoid ,
584- ) ;
581+ PubSub . publish ( changesPubSub , settings ) . pipe ( Effect . asVoid ) ;
585582
586583 const readConfigExists = fs . exists ( settingsPath ) . pipe (
587584 Effect . mapError (
@@ -610,13 +607,12 @@ const make = Effect.gen(function* () {
610607 const sparseSettingsJson = yield * encodeServerSettingsJson (
611608 stripDefaultServerSettings ( settings , DEFAULT_SERVER_SETTINGS ) ?? { } ,
612609 ) ;
610+ const contents = `${ sparseSettingsJson } \n` ;
613611
614- return yield * writeFileStringAtomically ( {
615- filePath : settingsPath ,
616- contents : `${ sparseSettingsJson } \n` ,
617- } ) . pipe (
612+ return yield * writeFileStringAtomically ( { filePath : settingsPath , contents } ) . pipe (
618613 Effect . provideService ( FileSystem . FileSystem , fs ) ,
619614 Effect . provideService ( Path . Path , pathService ) ,
615+ Effect . as ( contents ) ,
620616 ) ;
621617 } ,
622618 Effect . mapError (
@@ -664,9 +660,11 @@ const make = Effect.gen(function* () {
664660 let legacyProviders : Readonly < Record < string , unknown > > | undefined ;
665661 let legacySourceControl : Parameters < typeof migrateLegacySourceControlSettings > [ 1 ] = { } ;
666662 let parseFailure = Option . none < unknown > ( ) ;
663+ let isPublishedSave = false ;
667664
668665 if ( yield * readConfigExists ) {
669666 const raw = yield * readRawConfig ;
667+ isPublishedSave = Option . contains ( yield * Ref . get ( publishedSaveRef ) , raw ) ;
670668 const decoded = decodeServerSettingsJsonExit ( raw ) ;
671669 const legacySettings = decodeLegacyProviderSettingsJsonExit ( raw ) ;
672670 if ( legacySettings . _tag === "Success" ) {
@@ -801,27 +799,35 @@ const make = Effect.gen(function* () {
801799 yield * secretStore . remove ( move . from ) . pipe ( Effect . ignore ) ;
802800 }
803801 }
804- return { settings : migrated , parseFailure } ;
802+ return { settings : migrated , parseFailure, isPublishedSave } ;
805803 } ) ;
806804
807- const settingsCache = yield * Cache . makeWith ( ( _key : typeof cacheKey ) => loadSettingsFromDisk , {
808- capacity : 1 ,
809- timeToLive : ( exit ) => ( Exit . isSuccess ( exit ) ? Duration . infinity : Duration . zero ) ,
810- } ) ;
805+ const settingsCache = yield * Cache . makeWith (
806+ ( _key : typeof cacheKey ) =>
807+ loadSettingsFromDisk . pipe (
808+ Effect . map ( ( { settings, parseFailure } ) => ( { settings, parseFailure } ) ) ,
809+ ) ,
810+ {
811+ capacity : 1 ,
812+ timeToLive : ( exit ) => ( Exit . isSuccess ( exit ) ? Duration . infinity : Duration . zero ) ,
813+ } ,
814+ ) ;
811815
812816 const getSettingsFromCache = Cache . get ( settingsCache , cacheKey ) . pipe (
813817 Effect . map ( ( loaded ) => loaded . settings ) ,
814818 ) ;
815- // A file caught mid-write or left invalid keeps the settings that last loaded instead of
816- // resetting them to defaults. Its parse failure still blocks saves.
819+ // Loads beside the cached entry and then replaces it, so readers never wait on a reload or
820+ // see a half-finished one. A file caught mid-write or left invalid keeps the settings that
821+ // last loaded instead of resetting them to defaults; its parse failure still blocks saves.
817822 const reloadSettings = Effect . gen ( function * ( ) {
823+ const { isPublishedSave, ...loaded } = yield * loadSettingsFromDisk ;
818824 const previous = yield * Cache . getSuccess ( settingsCache , cacheKey ) ;
819- yield * Cache . invalidate ( settingsCache , cacheKey ) ;
820- const loaded = yield * Cache . get ( settingsCache , cacheKey ) ;
821- if ( Option . isNone ( loaded . parseFailure ) || Option . isNone ( previous ) ) return loaded ;
822- const kept = { settings : previous . value . settings , parseFailure : loaded . parseFailure } ;
823- yield * Cache . set ( settingsCache , cacheKey , kept ) ;
824- return kept ;
825+ const next =
826+ Option . isSome ( loaded . parseFailure ) && Option . isSome ( previous )
827+ ? { settings : previous . value . settings , parseFailure : loaded . parseFailure }
828+ : loaded ;
829+ yield * Cache . set ( settingsCache , cacheKey , next ) ;
830+ return { ... next , isPublishedSave } ;
825831 } ) ;
826832
827833 const materializeSettingsSecrets = Effect . fn ( "serverSettings.materializeSettingsSecrets" ) (
@@ -1035,7 +1041,7 @@ const make = Effect.gen(function* () {
10351041 const updated = yield * update ( current ) ;
10361042 const persisted = yield * persistSettingsSecrets ( current , updated ) ;
10371043 const next = yield * normalizeServerSettings ( persisted . settings ) ;
1038- const materialized = yield * Effect . uninterruptibleMask ( ( ) =>
1044+ const { materialized, written } = yield * Effect . uninterruptibleMask ( ( ) =>
10391045 Effect . gen ( function * ( ) {
10401046 const rollbackSecretChanges = yield * applySettingsSecretChanges ( persisted . changes ) ;
10411047 const materializedExit = yield * Effect . exit ( materializeSettingsSecrets ( next ) ) ;
@@ -1048,11 +1054,12 @@ const make = Effect.gen(function* () {
10481054 yield * rollbackSecretChanges ;
10491055 return yield * Effect . failCause ( writeExit . cause ) ;
10501056 }
1051- return materializedExit . value ;
1057+ return { materialized : materializedExit . value , written : writeExit . value } ;
10521058 } ) ,
10531059 ) ;
10541060 yield * Cache . set ( settingsCache , cacheKey , { settings : next , parseFailure : Option . none ( ) } ) ;
10551061 yield * emitChange ( next ) ;
1062+ yield * Ref . set ( publishedSaveRef , Option . some ( written ) ) ;
10561063 return resolveTextGenerationProvider ( materialized ) ;
10571064 } ) ,
10581065 ) ;
@@ -1068,10 +1075,11 @@ const make = Effect.gen(function* () {
10681075
10691076 const revalidateAndEmit = writeSemaphore . withPermits ( 1 ) (
10701077 Effect . gen ( function * ( ) {
1071- const { settings } = yield * reloadSettings ;
1072- const published = yield * Ref . get ( publishedRef ) ;
1073- if ( Option . isSome ( published ) && Equal . equals ( published . value , settings ) ) return ;
1078+ const { settings, parseFailure , isPublishedSave } = yield * reloadSettings ;
1079+ // Subscribers already have our own save, and keep what they have while the file is invalid.
1080+ if ( isPublishedSave || Option . isSome ( parseFailure ) ) return ;
10741081 yield * emitChange ( settings ) ;
1082+ yield * Ref . set ( publishedSaveRef , Option . none ( ) ) ;
10751083 } ) ,
10761084 ) ;
10771085
@@ -1159,12 +1167,7 @@ const make = Effect.gen(function* () {
11591167
11601168 const startup = Effect . gen ( function * ( ) {
11611169 yield * startWatcher ;
1162- const { settings } = yield * reloadSettings ;
1163- // Clients start from a snapshot of these, so they count as published.
1164- yield * Ref . update (
1165- publishedRef ,
1166- Option . orElseSome ( ( ) => settings ) ,
1167- ) ;
1170+ yield * reloadSettings ;
11681171 } ) ;
11691172
11701173 const startupExit = yield * Effect . exit ( startup ) ;
0 commit comments