@@ -20,6 +20,12 @@ type RestartPostCheckContext = {
2020
2121type RestartParams = {
2222 opts ?: { json ?: boolean } ;
23+ repairLoadedService ?: ( ctx : {
24+ json : boolean ;
25+ stdout : NodeJS . WritableStream ;
26+ state : unknown ;
27+ issues : unknown [ ] ;
28+ } ) => Promise < unknown > ;
2329 postRestartCheck ?: ( ctx : RestartPostCheckContext ) => Promise < void > ;
2430} ;
2531
@@ -352,6 +358,51 @@ describe("runDaemonRestart health checks", () => {
352358 expect ( waitParams . env ?. OPENCLAW_SYSTEMD_UNIT ) . toBe ( "openclaw-gateway-maintenance.service" ) ;
353359 } ) ;
354360
361+ it ( "re-reads the installed service environment after restart repair" , async ( ) => {
362+ service . readCommand
363+ . mockResolvedValueOnce ( {
364+ programArguments : [ "openclaw" , "gateway" , "--port" , "18789" ] ,
365+ environment : { OPENCLAW_STATE_DIR : "/tmp/openclaw-stale-state" } ,
366+ } )
367+ . mockResolvedValue ( {
368+ programArguments : [ "openclaw" , "gateway" , "--port" , "19001" ] ,
369+ environment : { OPENCLAW_STATE_DIR : "/tmp/openclaw-repaired-state" } ,
370+ } ) ;
371+ repairLoadedGatewayServiceForStart . mockResolvedValue ( {
372+ result : "restarted" ,
373+ message : "Gateway service definition repaired and restarted." ,
374+ loaded : true ,
375+ } ) ;
376+ runServiceRestart . mockImplementation ( async ( params : RestartParams ) => {
377+ await params . repairLoadedService ?.( {
378+ json : true ,
379+ stdout : process . stdout ,
380+ state : { } ,
381+ issues : [ { code : "version-mismatch" , message : "old service" } ] ,
382+ } ) ;
383+ await params . postRestartCheck ?.( {
384+ json : true ,
385+ stdout : process . stdout ,
386+ warnings : [ ] ,
387+ fail : ( message : string ) => {
388+ throw new Error ( message ) ;
389+ } ,
390+ } ) ;
391+ return true ;
392+ } ) ;
393+
394+ await runDaemonRestart ( { json : true } ) ;
395+
396+ expect ( waitForGatewayHealthyRestart ) . toHaveBeenCalledWith (
397+ expect . objectContaining ( {
398+ port : 19_001 ,
399+ env : expect . objectContaining ( {
400+ OPENCLAW_STATE_DIR : "/tmp/openclaw-repaired-state" ,
401+ } ) ,
402+ } ) ,
403+ ) ;
404+ } ) ;
405+
355406 it ( "repairs toward an explicitly configured gateway port" , async ( ) => {
356407 loadConfig . mockReturnValue ( { gateway : { port : 19_001 } } ) ;
357408 resolveGatewayPort . mockReturnValue ( 19_001 ) ;
0 commit comments