@@ -374,6 +374,27 @@ struct GatewayProcessManagerTests {
374374 }
375375 }
376376
377+ @ Test func `inactive lifecycle skips persistence ensure` ( ) async throws {
378+ let marker = FileManager . default . temporaryDirectory
379+ . appendingPathComponent ( " openclaw-launchagent-marker- \( UUID ( ) . uuidString ) " )
380+ try await self . withLocalGatewayConfig {
381+ GatewayLaunchAgentManager . setTestingDisableLaunchAgentMarkerURL ( marker )
382+ GatewayLaunchAgentManager . setTestingInterceptDaemonCommands ( true )
383+ GatewayLaunchAgentManager . clearTestingDaemonCommandCalls ( )
384+ defer {
385+ GatewayLaunchAgentManager . setTestingDisableLaunchAgentMarkerURL ( nil )
386+ GatewayLaunchAgentManager . setTestingInterceptDaemonCommands ( false )
387+ GatewayLaunchAgentManager . clearTestingDaemonCommandCalls ( )
388+ }
389+
390+ let manager = GatewayProcessManager . shared
391+ manager . setTestingDesiredActive ( false )
392+ await manager . ensureLaunchAgentEnabledIfNeeded ( )
393+
394+ #expect ( GatewayLaunchAgentManager . testingDaemonCommandCallsSnapshot ( ) . isEmpty )
395+ }
396+ }
397+
377398 @ Test func `newer inactive lifecycle retains the pending disable` ( ) async throws {
378399 let marker = FileManager . default . temporaryDirectory
379400 . appendingPathComponent ( " openclaw-launchagent-marker- \( UUID ( ) . uuidString ) " )
@@ -736,6 +757,40 @@ struct GatewayProcessManagerTests {
736757 }
737758 }
738759
760+ @ Test func `protects an unmanaged listener during persistence ensure` ( ) async throws {
761+ let port = 19100
762+ let marker = FileManager . default . temporaryDirectory
763+ . appendingPathComponent ( " openclaw-launchagent-marker- \( UUID ( ) . uuidString ) " )
764+ try await self . withLocalGatewayConfig {
765+ GatewayLaunchAgentManager . setTestingDisableLaunchAgentMarkerURL ( marker )
766+ GatewayLaunchAgentManager . setTestingInterceptDaemonCommands ( true )
767+ GatewayLaunchAgentManager . setTestingDaemonStatusPayload (
768+ #"{"ok":true,"service":{"loaded":false}}"# )
769+ GatewayLaunchAgentManager . clearTestingDaemonCommandCalls ( )
770+ defer {
771+ GatewayLaunchAgentManager . setTestingDisableLaunchAgentMarkerURL ( nil )
772+ GatewayLaunchAgentManager . setTestingInterceptDaemonCommands ( false )
773+ GatewayLaunchAgentManager . setTestingDaemonStatusPayload ( nil )
774+ GatewayLaunchAgentManager . clearTestingDaemonCommandCalls ( )
775+ }
776+
777+ let listener = PortGuardian . Descriptor (
778+ pid : 4243 ,
779+ command : " manual-gateway " ,
780+ executablePath : " /tmp/manual-gateway " )
781+ await PortGuardian . shared . setTestingDescriptor ( listener , forPort : port )
782+
783+ _ = await GatewayProcessManager . shared . _testEnableLaunchAgentIfNeeded (
784+ bundlePath : " /Applications/OpenClaw.app " ,
785+ port : port )
786+
787+ let calls = GatewayLaunchAgentManager . testingDaemonCommandCallsSnapshot ( )
788+ #expect ( calls . filter { $0 . first == " status " } . count == 2 )
789+ #expect ( calls . filter { $0 . first == " install " } . isEmpty )
790+ await PortGuardian . shared . setTestingDescriptor ( nil , forPort : port )
791+ }
792+ }
793+
739794 @ Test func `repairs loaded launch agents that are not reusable` ( ) async throws {
740795 let port = 19083
741796 let marker = FileManager . default . temporaryDirectory
@@ -811,15 +866,64 @@ struct GatewayProcessManagerTests {
811866 manager . setTestingConnection ( connection )
812867 manager . setTestingDesiredActive ( true )
813868 manager . setTestingLastFailureReason ( " health failed " )
869+ manager . _testSetLaunchAgentReadinessFailure ( port : 19101 , pid : 4242 )
814870 defer {
815871 manager . setTestingConnection ( nil )
816872 manager . setTestingDesiredActive ( false )
817873 manager . setTestingLastFailureReason ( nil )
874+ manager . _testClearLaunchAgentReadinessFailure ( )
818875 }
819876
820877 let ready = await manager . waitForGatewayReady ( timeout : 0.5 )
821878 #expect ( ready )
822879 #expect ( manager . lastFailureReason == nil )
880+ #expect ( ! manager . _testHasLaunchAgentReadinessFailure ( ) )
881+ }
882+
883+ @ Test func `stale readiness wait cannot clear a newer launch failure` ( ) async throws {
884+ let session = GatewayTestWebSocketSession (
885+ taskFactory : {
886+ GatewayTestWebSocketTask (
887+ sendHook : { task , message , sendIndex in
888+ guard sendIndex > 0 else { return }
889+ guard let id = GatewayWebSocketTestSupport . requestID ( from : message ) else { return }
890+ task . emitReceiveSuccess ( . data ( GatewayWebSocketTestSupport . okResponseData ( id : id ) ) )
891+ } ,
892+ receiveHook : { _ , receiveIndex in
893+ if receiveIndex == 0 {
894+ try await Task . sleep ( nanoseconds : 100_000_000 )
895+ }
896+ return . data ( GatewayWebSocketTestSupport . connectChallengeData ( ) )
897+ } )
898+ } )
899+ let url = try #require ( URL ( string : " ws://example.invalid " ) )
900+ let connection = GatewayConnection (
901+ configProvider : { ( url : url , token : nil , password : nil ) } ,
902+ sessionBox : WebSocketSessionBox ( session : session ) )
903+
904+ let manager = GatewayProcessManager . shared
905+ manager . setTestingConnection ( connection )
906+ manager . _testBeginGatewayStartGeneration ( )
907+ defer {
908+ manager . setTestingConnection ( nil )
909+ manager . setTestingDesiredActive ( false )
910+ manager . _testClearLaunchAgentReadinessFailure ( )
911+ }
912+
913+ let staleWait = Task { @MainActor in
914+ await manager . waitForGatewayReady ( timeout : 0.5 )
915+ }
916+ for _ in 0 ..< 100 {
917+ if session . snapshotMakeCount ( ) > 0 { break }
918+ try? await Task . sleep ( nanoseconds : 1_000_000 )
919+ }
920+ #expect ( session . snapshotMakeCount ( ) == 1 )
921+ manager . _testBeginGatewayStartGeneration ( )
922+ manager . _testSetLaunchAgentReadinessFailure ( port : 19101 , pid : 4242 )
923+
924+ #expect ( await staleWait . value == false )
925+ #expect ( manager . _testHasLaunchAgentReadinessFailure ( ) )
926+ await connection . shutdown ( )
823927 }
824928
825929 @ Test func `readiness timeout includes a stalled socket connect` ( ) async throws {
0 commit comments