@@ -45,6 +45,7 @@ const state = vi.hoisted(() => ({
4545 stopCode : 1 ,
4646 bootoutError : "" ,
4747 bootoutCode : 1 ,
48+ systemServiceLoaded : false ,
4849 serviceLoaded : true ,
4950 serviceRunning : true ,
5051 stopLeavesRunning : false ,
@@ -184,6 +185,12 @@ vi.mock("./exec-file.js", () => ({
184185 return { stdout : state . listOutput , stderr : "" , code : 0 } ;
185186 }
186187 if ( call [ 0 ] === "print" ) {
188+ if ( call [ 1 ] ?. startsWith ( "system/" ) ) {
189+ if ( state . systemServiceLoaded ) {
190+ return { stdout : [ "state = running" , "pid = 99" ] . join ( "\n" ) , stderr : "" , code : 0 } ;
191+ }
192+ return { stdout : "" , stderr : "Could not find service" , code : 113 } ;
193+ }
187194 if ( state . printNotLoadedRemaining > 0 ) {
188195 state . printNotLoadedRemaining -= 1 ;
189196 return { stdout : "" , stderr : "Could not find service" , code : 113 } ;
@@ -315,6 +322,20 @@ vi.mock("node:fs/promises", async () => {
315322 }
316323 throw new Error ( `ENOENT: no such file or directory, open '${ key } '` ) ;
317324 } ) ,
325+ readdir : vi . fn ( async ( p : string ) => {
326+ const prefix = `${ p . replace ( / \/ + $ / , "" ) } /` ;
327+ const entries = new Set < string > ( ) ;
328+ for ( const key of [ ...state . files . keys ( ) , ...state . dirs ] ) {
329+ if ( ! key . startsWith ( prefix ) ) {
330+ continue ;
331+ }
332+ const entry = key . slice ( prefix . length ) . split ( "/" ) [ 0 ] ;
333+ if ( entry ) {
334+ entries . add ( entry ) ;
335+ }
336+ }
337+ return [ ...entries ] ;
338+ } ) ,
318339 unlink : vi . fn ( async ( p : string ) => {
319340 state . files . delete ( p ) ;
320341 } ) ,
@@ -349,6 +370,7 @@ beforeEach(() => {
349370 state . stopCode = 1 ;
350371 state . bootoutError = "" ;
351372 state . bootoutCode = 1 ;
373+ state . systemServiceLoaded = false ;
352374 state . serviceLoaded = true ;
353375 state . serviceRunning = true ;
354376 state . stopLeavesRunning = false ;
@@ -803,6 +825,21 @@ describe("launchd bootstrap repair", () => {
803825 detail : "launchctl kickstart failed: permission denied" ,
804826 } ) ;
805827 } ) ;
828+
829+ it ( "does not bootstrap repair a LaunchAgent when the same label is loaded as a system LaunchDaemon" , async ( ) => {
830+ const env = createDefaultLaunchdEnv ( ) ;
831+ state . systemServiceLoaded = true ;
832+
833+ const repair = await repairLaunchAgentBootstrap ( { env } ) ;
834+
835+ expect ( repair . ok ) . toBe ( false ) ;
836+ if ( repair . ok ) {
837+ throw new Error ( "expected bootstrap repair to fail" ) ;
838+ }
839+ expect ( repair . status ) . toBe ( "system-launchdaemon-conflict" ) ;
840+ expect ( repair . detail ) . toContain ( "launchd already has system/ai.openclaw.gateway" ) ;
841+ expect ( state . launchctlCalls ) . toEqual ( [ [ "print" , "system/ai.openclaw.gateway" ] ] ) ;
842+ } ) ;
806843} ) ;
807844
808845describe ( "launchd install" , ( ) => {
@@ -1001,6 +1038,75 @@ describe("launchd install", () => {
10011038 expect ( state . dirModes . get ( tmpDir ) ) . toBe ( 0o700 ) ;
10021039 } ) ;
10031040
1041+ it ( "refuses to install a user LaunchAgent when the same label is loaded as a system LaunchDaemon" , async ( ) => {
1042+ const env = createDefaultLaunchdEnv ( ) ;
1043+ const plistPath = resolveLaunchAgentPlistPath ( env ) ;
1044+ state . systemServiceLoaded = true ;
1045+
1046+ await expect (
1047+ installLaunchAgent ( {
1048+ env,
1049+ stdout : new PassThrough ( ) ,
1050+ programArguments : defaultProgramArguments ,
1051+ } ) ,
1052+ ) . rejects . toThrow ( "launchd already has system/ai.openclaw.gateway" ) ;
1053+
1054+ expect ( state . launchctlCalls ) . toEqual ( [ [ "print" , "system/ai.openclaw.gateway" ] ] ) ;
1055+ expect ( state . files . has ( plistPath ) ) . toBe ( false ) ;
1056+ expect ( state . fileWrites . some ( ( write ) => write . path === plistPath ) ) . toBe ( false ) ;
1057+ } ) ;
1058+
1059+ it ( "refuses to install a user LaunchAgent when a system LaunchDaemon plist already exists" , async ( ) => {
1060+ const env = createDefaultLaunchdEnv ( ) ;
1061+ const plistPath = resolveLaunchAgentPlistPath ( env ) ;
1062+ state . files . set ( "/Library/LaunchDaemons/ai.openclaw.gateway.plist" , "<plist/>" ) ;
1063+
1064+ await expect (
1065+ installLaunchAgent ( {
1066+ env,
1067+ stdout : new PassThrough ( ) ,
1068+ programArguments : defaultProgramArguments ,
1069+ } ) ,
1070+ ) . rejects . toThrow (
1071+ "system-domain LaunchDaemon plist already exists at /Library/LaunchDaemons/ai.openclaw.gateway.plist" ,
1072+ ) ;
1073+
1074+ expect ( state . launchctlCalls ) . toEqual ( [ [ "print" , "system/ai.openclaw.gateway" ] ] ) ;
1075+ expect ( state . files . has ( plistPath ) ) . toBe ( false ) ;
1076+ expect ( state . fileWrites . some ( ( write ) => write . path === plistPath ) ) . toBe ( false ) ;
1077+ } ) ;
1078+
1079+ it ( "refuses to install when a differently named system LaunchDaemon plist declares the same label" , async ( ) => {
1080+ const env = createDefaultLaunchdEnv ( ) ;
1081+ const plistPath = resolveLaunchAgentPlistPath ( env ) ;
1082+ state . files . set (
1083+ "/Library/LaunchDaemons/com.example.operator-managed.plist" ,
1084+ [
1085+ '<?xml version="1.0" encoding="UTF-8"?>' ,
1086+ '<plist version="1.0">' ,
1087+ "<dict>" ,
1088+ "<key>Label</key>" ,
1089+ "<string>ai.openclaw.gateway</string>" ,
1090+ "</dict>" ,
1091+ "</plist>" ,
1092+ ] . join ( "\n" ) ,
1093+ ) ;
1094+
1095+ await expect (
1096+ installLaunchAgent ( {
1097+ env,
1098+ stdout : new PassThrough ( ) ,
1099+ programArguments : defaultProgramArguments ,
1100+ } ) ,
1101+ ) . rejects . toThrow (
1102+ "system-domain LaunchDaemon plist already exists at /Library/LaunchDaemons/com.example.operator-managed.plist" ,
1103+ ) ;
1104+
1105+ expect ( state . launchctlCalls ) . toEqual ( [ [ "print" , "system/ai.openclaw.gateway" ] ] ) ;
1106+ expect ( state . files . has ( plistPath ) ) . toBe ( false ) ;
1107+ expect ( state . fileWrites . some ( ( write ) => write . path === plistPath ) ) . toBe ( false ) ;
1108+ } ) ;
1109+
10041110 it ( "writes KeepAlive=true policy with shutdown and throttle limits" , async ( ) => {
10051111 const env = createDefaultLaunchdEnv ( ) ;
10061112 await installLaunchAgent ( {
@@ -1546,6 +1652,7 @@ describe("launchd install", () => {
15461652 expect ( result ) . toEqual ( { outcome : "completed" } ) ;
15471653 expect ( cleanStaleGatewayProcessesSync ) . toHaveBeenCalledWith ( 18789 ) ;
15481654 expect ( state . launchctlCalls ) . toEqual ( [
1655+ [ "print" , `system/${ label } ` ] ,
15491656 [ "enable" , serviceId ] ,
15501657 [ "kickstart" , "-k" , serviceId ] ,
15511658 ] ) ;
@@ -1588,7 +1695,7 @@ describe("launchd install", () => {
15881695 expect ( plist ) . toContain ( "<key>StandardInPath</key>" ) ;
15891696 expect ( plist ) . toContain ( "<string>/dev/null</string>" ) ;
15901697 expect ( plist ) . toContain ( "<string>/Users/test/Library/Logs/openclaw/gateway.log</string>" ) ;
1591- expect ( launchctlCommandNames ( ) ) . toEqual ( [ "enable" , "bootout" , "enable" , "bootstrap" ] ) ;
1698+ expect ( launchctlCommandNames ( ) ) . toEqual ( [ "print" , " enable", "bootout" , "enable" , "bootstrap" ] ) ;
15921699 expect ( launchctlCommandNames ( ) ) . not . toContain ( "kickstart" ) ;
15931700 } ) ;
15941701
@@ -1626,7 +1733,14 @@ describe("launchd install", () => {
16261733 stdout : new PassThrough ( ) ,
16271734 } ) ;
16281735
1629- expect ( launchctlCommandNames ( ) ) . toEqual ( [ "enable" , "bootout" , "enable" , "bootstrap" , "print" ] ) ;
1736+ expect ( launchctlCommandNames ( ) ) . toEqual ( [
1737+ "print" ,
1738+ "enable" ,
1739+ "bootout" ,
1740+ "enable" ,
1741+ "bootstrap" ,
1742+ "print" ,
1743+ ] ) ;
16301744 expect ( launchctlCommandNames ( ) ) . not . toContain ( "kickstart" ) ;
16311745 } ) ;
16321746
@@ -1780,6 +1894,24 @@ describe("launchd install", () => {
17801894 expect ( launchctlCommandNames ( ) ) . not . toContain ( "bootout" ) ;
17811895 } ) ;
17821896
1897+ it ( "blocks restart before normal kickstart when the same label is loaded as a system LaunchDaemon" , async ( ) => {
1898+ const env = createDefaultLaunchdEnv ( ) ;
1899+ state . systemServiceLoaded = true ;
1900+
1901+ await expect (
1902+ restartLaunchAgent ( {
1903+ env,
1904+ stdout : new PassThrough ( ) ,
1905+ } ) ,
1906+ ) . rejects . toThrow ( "launchd already has system/ai.openclaw.gateway" ) ;
1907+
1908+ expect ( launchctlCommandNames ( ) ) . not . toContain ( "enable" ) ;
1909+ expect ( launchctlCommandNames ( ) ) . not . toContain ( "kickstart" ) ;
1910+ expect ( launchctlCommandNames ( ) ) . not . toContain ( "bootstrap" ) ;
1911+ expect ( launchctlCommandNames ( ) ) . not . toContain ( "bootout" ) ;
1912+ expect ( state . launchctlCalls ) . toEqual ( [ [ "print" , "system/ai.openclaw.gateway" ] ] ) ;
1913+ } ) ;
1914+
17831915 it ( "surfaces the original kickstart failure when the service is still loaded" , async ( ) => {
17841916 const env = createDefaultLaunchdEnv ( ) ;
17851917 state . kickstartError = "Input/output error" ;
@@ -1830,7 +1962,24 @@ describe("launchd install", () => {
18301962 mode : "kickstart" ,
18311963 waitForPid : process . pid ,
18321964 } ) ;
1833- expect ( state . launchctlCalls ) . toStrictEqual ( [ ] ) ;
1965+ expect ( state . launchctlCalls ) . toStrictEqual ( [ [ "print" , "system/ai.openclaw.gateway" ] ] ) ;
1966+ } ) ;
1967+
1968+ it ( "does not schedule in-band restart handoff when the same label is loaded as a system LaunchDaemon" , async ( ) => {
1969+ const env = createDefaultLaunchdEnv ( ) ;
1970+ state . systemServiceLoaded = true ;
1971+
1972+ await expect (
1973+ withProcessEnv ( { LAUNCH_JOB_LABEL : "ai.openclaw.gateway" } , async ( ) =>
1974+ restartLaunchAgent ( {
1975+ env,
1976+ stdout : new PassThrough ( ) ,
1977+ } ) ,
1978+ ) ,
1979+ ) . rejects . toThrow ( "launchd already has system/ai.openclaw.gateway" ) ;
1980+
1981+ expect ( launchdRestartHandoffState . scheduleDetachedLaunchdRestartHandoff ) . not . toHaveBeenCalled ( ) ;
1982+ expect ( state . launchctlCalls ) . toStrictEqual ( [ [ "print" , "system/ai.openclaw.gateway" ] ] ) ;
18341983 } ) ;
18351984
18361985 it ( "hands plist reload off when current LaunchAgent needs rewritten paths" , async ( ) => {
@@ -1870,7 +2019,7 @@ describe("launchd install", () => {
18702019 waitForPid : process . pid ,
18712020 } ) ;
18722021 expect ( state . files . get ( plistPath ) ) . toContain ( "/Users/test/Library/Logs/openclaw/gateway.log" ) ;
1873- expect ( state . launchctlCalls ) . toStrictEqual ( [ ] ) ;
2022+ expect ( state . launchctlCalls ) . toStrictEqual ( [ [ "print" , "system/ai.openclaw.gateway" ] ] ) ;
18742023 } ) ;
18752024
18762025 it ( "surfaces detached handoff failures" , async ( ) => {
@@ -1915,7 +2064,7 @@ describe("launchd install", () => {
19152064 mode : "kickstart" ,
19162065 waitForPid : process . pid ,
19172066 } ) ;
1918- expect ( state . launchctlCalls ) . toStrictEqual ( [ ] ) ;
2067+ expect ( state . launchctlCalls ) . toStrictEqual ( [ [ "print" , "system/ai.openclaw.gateway" ] ] ) ;
19192068 } ) ;
19202069
19212070 it ( "does not hand restart off for unrelated inherited XPC service names" , async ( ) => {
0 commit comments