@@ -691,6 +691,22 @@ describe("launchd install", () => {
691691 expect ( output ) . toContain ( "Stopped LaunchAgent" ) ;
692692 } ) ;
693693
694+ it ( "resolves the stop postcondition port from the stored LaunchAgent environment" , async ( ) => {
695+ const env = createDefaultLaunchdEnv ( ) ;
696+ await installLaunchAgent ( {
697+ env,
698+ stdout : new PassThrough ( ) ,
699+ programArguments : defaultProgramArguments ,
700+ environment : { OPENCLAW_GATEWAY_PORT : "19006" } ,
701+ } ) ;
702+ state . launchctlCalls . length = 0 ;
703+
704+ await stopLaunchAgent ( { env, stdout : new PassThrough ( ) } ) ;
705+
706+ expect ( cleanStaleGatewayProcessesSync ) . toHaveBeenCalledWith ( 19006 ) ;
707+ expect ( inspectPortUsage ) . toHaveBeenCalledWith ( 19006 ) ;
708+ } ) ;
709+
694710 it ( "fails stop when the verified gateway port remains busy after cleanup" , async ( ) => {
695711 const env = {
696712 ...createDefaultLaunchdEnv ( ) ,
@@ -961,6 +977,25 @@ describe("launchd install", () => {
961977 expect ( cleanStaleGatewayProcessesSync ) . toHaveBeenCalledWith ( 19001 ) ;
962978 } ) ;
963979
980+ it ( "uses the stored LaunchAgent environment port for restart stale cleanup" , async ( ) => {
981+ const env = createDefaultLaunchdEnv ( ) ;
982+ await installLaunchAgent ( {
983+ env,
984+ stdout : new PassThrough ( ) ,
985+ programArguments : defaultProgramArguments ,
986+ environment : { OPENCLAW_GATEWAY_PORT : "19007" } ,
987+ } ) ;
988+ state . launchctlCalls . length = 0 ;
989+
990+ await restartLaunchAgent ( {
991+ env,
992+ stdout : new PassThrough ( ) ,
993+ } ) ;
994+
995+ expect ( cleanStaleGatewayProcessesSync ) . toHaveBeenCalledWith ( 19007 ) ;
996+ expect ( inspectPortUsage ) . toHaveBeenCalledWith ( 19007 ) ;
997+ } ) ;
998+
964999 it ( "fails restart before kickstart when the configured gateway port remains busy" , async ( ) => {
9651000 const env = {
9661001 ...createDefaultLaunchdEnv ( ) ,
0 commit comments