@@ -78,6 +78,64 @@ describe("serverRuntimeState", () => {
7878 } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
7979 ) ;
8080
81+ it . effect ( "clears runtime state owned by the stopping server" , ( ) =>
82+ Effect . gen ( function * ( ) {
83+ const fileSystem = yield * FileSystem . FileSystem ;
84+ const path = yield * Path . Path ;
85+ const root = yield * fileSystem . makeTempDirectoryScoped ( {
86+ prefix : "t3-server-runtime-state-test-" ,
87+ } ) ;
88+ const statePath = path . join ( root , "server.json" ) ;
89+ const state : ServerRuntimeState . PersistedServerRuntimeState = {
90+ version : 1 ,
91+ pid : 123 ,
92+ port : 4_971 ,
93+ origin : "http://127.0.0.1:4971" ,
94+ startedAt : "2026-06-20T00:00:00.000Z" ,
95+ } ;
96+ yield * ServerRuntimeState . persistServerRuntimeState ( { path : statePath , state } ) ;
97+
98+ yield * ServerRuntimeState . clearPersistedServerRuntimeState ( statePath , state ) ;
99+
100+ const restored = yield * ServerRuntimeState . readPersistedServerRuntimeState ( statePath ) ;
101+ assert . isTrue ( Option . isNone ( restored ) ) ;
102+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
103+ ) ;
104+
105+ it . effect ( "does not clear runtime state replaced by a newer server" , ( ) =>
106+ Effect . gen ( function * ( ) {
107+ const fileSystem = yield * FileSystem . FileSystem ;
108+ const path = yield * Path . Path ;
109+ const root = yield * fileSystem . makeTempDirectoryScoped ( {
110+ prefix : "t3-server-runtime-state-test-" ,
111+ } ) ;
112+ const statePath = path . join ( root , "server.json" ) ;
113+ const oldState : ServerRuntimeState . PersistedServerRuntimeState = {
114+ version : 1 ,
115+ pid : 123 ,
116+ port : 4_971 ,
117+ origin : "http://127.0.0.1:4971" ,
118+ startedAt : "2026-06-20T00:00:00.000Z" ,
119+ } ;
120+ const currentState : ServerRuntimeState . PersistedServerRuntimeState = {
121+ ...oldState ,
122+ pid : 456 ,
123+ port : 4_972 ,
124+ origin : "http://127.0.0.1:4972" ,
125+ startedAt : "2026-06-20T00:01:00.000Z" ,
126+ } ;
127+ yield * ServerRuntimeState . persistServerRuntimeState ( {
128+ path : statePath ,
129+ state : currentState ,
130+ } ) ;
131+
132+ yield * ServerRuntimeState . clearPersistedServerRuntimeState ( statePath , oldState ) ;
133+
134+ const restored = yield * ServerRuntimeState . readPersistedServerRuntimeState ( statePath ) ;
135+ assert . deepEqual ( Option . getOrThrow ( restored ) , currentState ) ;
136+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
137+ ) ;
138+
81139 it . effect ( "preserves malformed state decode failures" , ( ) => {
82140 const logs : CapturedLog [ ] = [ ] ;
83141 const logger = Logger . make ( ( { fiber, message } ) => {
0 commit comments