@@ -4,6 +4,7 @@ import { normalizeOptionalString } from "@openclaw/normalization-core/string-coe
44import { theme } from "../../../packages/terminal-core/src/theme.js" ;
55import { isRestartEnabled } from "../../config/commands.flags.js" ;
66import { readBestEffortConfig , resolveGatewayPort } from "../../config/config.js" ;
7+ import { mergeGatewayServiceEnv } from "../../daemon/service-env-merge.js" ;
78import { resolveGatewayService } from "../../daemon/service.js" ;
89import {
910 findInstalledSystemdGatewayScope ,
@@ -61,7 +62,7 @@ function postRestartHealthAttempts(): number {
6162function formatRestartFailure ( params : {
6263 health : GatewayRestartSnapshot ;
6364 port : number ;
64- timeoutSeconds : number ;
65+ defaultTimeoutSeconds : number ;
6566} ) : { statusLine : string ; failMessage : string } {
6667 if ( params . health . waitOutcome === "stopped-free" ) {
6768 const elapsedSeconds = Math . max ( 1 , Math . round ( ( params . health . elapsedMs ?? 0 ) / 1000 ) ) ;
@@ -71,22 +72,37 @@ function formatRestartFailure(params: {
7172 } ;
7273 }
7374
75+ const timeoutSeconds = Math . max (
76+ 1 ,
77+ Math . round (
78+ params . health . elapsedMs === undefined
79+ ? params . defaultTimeoutSeconds
80+ : params . health . elapsedMs / 1000 ,
81+ ) ,
82+ ) ;
7483 return {
75- statusLine : `Timed out after ${ params . timeoutSeconds } s waiting for gateway port ${ params . port } to become healthy.` ,
76- failMessage : `Gateway restart timed out after ${ params . timeoutSeconds } s waiting for health checks.` ,
84+ statusLine : `Timed out after ${ timeoutSeconds } s waiting for gateway port ${ params . port } to become healthy.` ,
85+ failMessage : `Gateway restart timed out after ${ timeoutSeconds } s waiting for health checks.` ,
7786 } ;
7887}
7988
80- async function resolveGatewayLifecyclePort ( service = resolveGatewayService ( ) ) {
89+ async function resolveGatewayLifecycleContext ( service = resolveGatewayService ( ) ) : Promise < {
90+ port : number ;
91+ env : NodeJS . ProcessEnv ;
92+ } > {
8193 const command = await service . readCommand ( process . env ) . catch ( ( ) => null ) ;
82- const serviceEnv = command ?. environment ?? undefined ;
83- const mergedEnv = {
84- ...( process . env as Record < string , string | undefined > ) ,
85- ...( serviceEnv ?? undefined ) ,
86- } as NodeJS . ProcessEnv ;
94+ const mergedEnv = mergeGatewayServiceEnv ( process . env , command ) ;
8795
8896 const portFromArgs = parsePortFromArgs ( command ?. programArguments ) ;
89- return portFromArgs ?? resolveGatewayPort ( await readBestEffortConfig ( ) , mergedEnv ) ;
97+ const config = await readBestEffortConfig ( ) . catch ( ( ) => undefined ) ;
98+ return {
99+ port : portFromArgs ?? resolveGatewayPort ( config , mergedEnv ) ,
100+ env : mergedEnv ,
101+ } ;
102+ }
103+
104+ async function resolveGatewayLifecyclePort ( service = resolveGatewayService ( ) ) {
105+ return ( await resolveGatewayLifecycleContext ( service ) ) . port ;
90106}
91107
92108function resolveGatewayPortFallback ( ) : Promise < number > {
@@ -347,9 +363,11 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
347363 let restartedWithoutServiceManager = false ;
348364 const restartIntent = resolveGatewayRestartIntentOptions ( opts ) ;
349365 const configuredPort = await resolveExplicitGatewayConfigPort ( ) ;
350- const managedRestartPort =
351- configuredPort ??
352- ( await resolveGatewayLifecyclePort ( service ) . catch ( ( ) => resolveGatewayPortFallback ( ) ) ) ;
366+ let managedRestartContext = await resolveGatewayLifecycleContext ( service ) . catch ( async ( ) => ( {
367+ port : await resolveGatewayPortFallback ( ) ,
368+ env : process . env ,
369+ } ) ) ;
370+ let managedRestartPort = configuredPort ?? managedRestartContext . port ;
353371 // An unmanaged run loop keeps its lock port across in-process restarts, even
354372 // when config changes underneath it. Use that port for both the signal and
355373 // health proof or a valid CLI/env override looks like a failed restart.
@@ -369,8 +387,8 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
369387 } ,
370388 checkTokenDrift : true ,
371389 expectedPort : configuredPort ,
372- repairLoadedService : async ( { json, stdout, warn, state, issues } ) =>
373- await repairLoadedGatewayServiceForStart ( {
390+ repairLoadedService : async ( { json, stdout, warn, state, issues } ) => {
391+ const result = await repairLoadedGatewayServiceForStart ( {
374392 action : "restart" ,
375393 service,
376394 port : configuredPort ,
@@ -379,7 +397,13 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
379397 warn,
380398 state,
381399 issues,
382- } ) ,
400+ } ) ;
401+ // Repair rewrites the service definition, so the old command environment
402+ // no longer identifies where the restarted gateway publishes readiness.
403+ managedRestartContext = await resolveGatewayLifecycleContext ( service ) ;
404+ managedRestartPort = configuredPort ?? managedRestartContext . port ;
405+ return result ;
406+ } ,
383407 onNotLoaded : async ( ) => {
384408 if ( process . platform === "darwin" ) {
385409 const recovered = await recoverInstalledLaunchAgent ( { result : "restarted" } ) ;
@@ -430,6 +454,7 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
430454 port : managedRestartPort ,
431455 attempts : restartHealthAttempts ,
432456 delayMs : POST_RESTART_HEALTH_DELAY_MS ,
457+ env : managedRestartContext . env ,
433458 includeUnknownListenersAsStale : process . platform === "win32" ,
434459 } ) ;
435460
@@ -453,6 +478,7 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
453478 port : managedRestartPort ,
454479 attempts : restartHealthAttempts ,
455480 delayMs : POST_RESTART_HEALTH_DELAY_MS ,
481+ env : managedRestartContext . env ,
456482 includeUnknownListenersAsStale : process . platform === "win32" ,
457483 } ) ;
458484 }
@@ -465,7 +491,7 @@ export async function runDaemonRestart(opts: DaemonLifecycleOptions = {}): Promi
465491 const failure = formatRestartFailure ( {
466492 health,
467493 port : managedRestartPort ,
468- timeoutSeconds : restartWaitSeconds ,
494+ defaultTimeoutSeconds : restartWaitSeconds ,
469495 } ) ;
470496 const runningNoPortLine =
471497 health . runtime . status === "running" && health . portUsage . status === "free"
0 commit comments