From 85188f411d1fdc502441ec1da5e1546a5bc319fc Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Thu, 30 Jul 2026 03:34:45 -0700 Subject: [PATCH] fix(connect): keep macOS service state accurate --- apps/server/src/cloud/bootService.test.ts | 56 +++++++++++++++++++- apps/server/src/cloud/bootService.ts | 62 +++++++++++++++++++++-- 2 files changed, 112 insertions(+), 6 deletions(-) diff --git a/apps/server/src/cloud/bootService.test.ts b/apps/server/src/cloud/bootService.test.ts index 3f1b218371..4711c95ec9 100644 --- a/apps/server/src/cloud/bootService.test.ts +++ b/apps/server/src/cloud/bootService.test.ts @@ -10,6 +10,7 @@ import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawne import { HostProcessArguments, + HostProcessEnvironment, HostProcessExecutablePath, HostProcessPlatform, HostProcessUserId, @@ -62,10 +63,15 @@ const makeHost = (entry: string): BootService.BootServiceHost => ({ cliEntryPath: entry, }); -const provideHostRefs = (home: string, platform: NodeJS.Platform = "linux") => +const provideHostRefs = ( + home: string, + platform: NodeJS.Platform = "linux", + pathEnvironment = "/test/bin:/usr/bin:/bin", +) => Effect.provide( Layer.mergeAll( Layer.succeed(HostProcessPlatform, platform), + Layer.succeed(HostProcessEnvironment, { HOME: home, PATH: pathEnvironment }), Layer.succeed(HostProcessUserId, 501), ConfigProvider.layer(ConfigProvider.fromEnv({ env: { HOME: home } })), ), @@ -494,6 +500,17 @@ it.layer(NodeServices.layer)("BootService", (it) => { assert.isTrue(status.installed); assert.isTrue(status.current); + const differentPathService = yield* BootService.make({ + baseDir: dirs.baseDir, + logsDir: dirs.logsDir, + cliVersion: "0.0.27", + host: makeHost(dirs.stableEntry), + }).pipe( + Effect.provide(makeRecordingRunnerLayer([])), + provideHostRefs(dirs.home, "darwin", "/different/setup/path"), + ); + assert.isTrue((yield* differentPathService.status).current); + assert.isTrue(yield* service.uninstall); assert.isFalse(yield* fs.exists(launchAgentPath)); }), @@ -573,6 +590,10 @@ it.layer(NodeServices.layer)("BootService", (it) => { assert.isTrue(yield* service.uninstall); assert.deepEqual(commands, [ + { + command: "/bin/launchctl", + args: ["print", "gui/501/com.t3tools.t3code.server"], + }, { command: "/bin/launchctl", args: ["bootout", "gui/501/com.t3tools.t3code.server"], @@ -581,6 +602,39 @@ it.layer(NodeServices.layer)("BootService", (it) => { }), ); + it.effect("keeps the macOS plist when launchd cannot stop a loaded agent", () => + Effect.gen(function* () { + const { dirs, fs, path } = yield* makeTestContext(); + const launchAgentPath = path.join( + dirs.home, + "Library", + "LaunchAgents", + "com.t3tools.t3code.server.plist", + ); + yield* fs.makeDirectory(path.dirname(launchAgentPath), { recursive: true }); + yield* fs.writeFileString(launchAgentPath, "existing launch agent"); + + const service = yield* BootService.make({ + baseDir: dirs.baseDir, + logsDir: dirs.logsDir, + cliVersion: "0.0.27", + host: makeHost(dirs.stableEntry), + }).pipe( + Effect.provide( + makeRecordingRunnerLayer([], { + failWhen: (command, args) => command === "/bin/launchctl" && args.includes("bootout"), + }), + ), + provideHostRefs(dirs.home, "darwin"), + ); + + const error = yield* service.uninstall.pipe(Effect.flip); + + assert.isTrue(isCommandError(error)); + assert.isTrue(yield* fs.exists(launchAgentPath)); + }), + ); + it.effect("fails on unsupported platforms without touching the filesystem", () => Effect.gen(function* () { const { dirs, fs, path } = yield* makeTestContext(); diff --git a/apps/server/src/cloud/bootService.ts b/apps/server/src/cloud/bootService.ts index c59d93808c..d6e9ddf5d5 100644 --- a/apps/server/src/cloud/bootService.ts +++ b/apps/server/src/cloud/bootService.ts @@ -201,6 +201,24 @@ export function renderBootServiceLaunchAgent(plan: BootServicePlan): string { ].join("\n"); } +const LAUNCH_AGENT_PATH_BLOCK = / PATH<\/key>\n [^\n]*<\/string>\n/; + +function isCurrentLaunchAgentDefinition(plist: string, plan: BootServicePlan): boolean { + if (!LAUNCH_AGENT_PATH_BLOCK.test(plist)) { + return false; + } + const planWithoutPath: BootServicePlan = { + nodePath: plan.nodePath, + t3EntryPath: plan.t3EntryPath, + baseDir: plan.baseDir, + logPath: plan.logPath, + unitPath: plan.unitPath, + }; + return ( + plist.replace(LAUNCH_AGENT_PATH_BLOCK, "") === renderBootServiceLaunchAgent(planWithoutPath) + ); +} + export class BootServiceUnsupportedError extends Schema.TaggedErrorClass()( "BootServiceUnsupportedError", { platform: Schema.String }, @@ -370,6 +388,27 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { Effect.orElseSucceed(() => false), ); }); + const isLaunchAgentLoaded = Effect.fn("cloud.boot_service.is_launch_agent_loaded")(function* () { + if (launchAgent === null) { + return false; + } + return yield* runner + .run({ + command: "/bin/launchctl", + args: ["print", launchAgent.serviceTarget], + }) + .pipe( + Effect.map((result) => result.code === 0), + Effect.mapError( + (cause) => + new BootServiceCommandError({ + step: "checking the LaunchAgent", + supervisor: "launchd", + cause, + }), + ), + ); + }); const readLaunchAgentDisabled = Effect.fn("cloud.boot_service.read_launch_agent_disabled")( function* () { if (launchAgent === null || userId === null) { @@ -633,10 +672,23 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { const uninstall: BootService["Service"]["uninstall"] = Effect.gen(function* () { yield* requireSupportedPlatform; - const stoppedLaunchAgent = - platform === "darwin" && launchAgent !== null - ? yield* runOptionalStep("/bin/launchctl", ["bootout", launchAgent.serviceTarget]) - : false; + let stoppedLaunchAgent = false; + if (platform === "darwin" && launchAgent !== null) { + if (yield* isLaunchAgentLoaded()) { + yield* runLaunchAgentStep("stopping the LaunchAgent", [ + "bootout", + launchAgent.serviceTarget, + ]); + stoppedLaunchAgent = true; + } else { + // Catch a job loaded between the print and bootout without treating + // the expected "not loaded" result as an uninstall failure. + stoppedLaunchAgent = yield* runOptionalStep("/bin/launchctl", [ + "bootout", + launchAgent.serviceTarget, + ]); + } + } const exists = yield* fs .exists(unitPath) .pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause }))); @@ -684,7 +736,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { const loaded = yield* runOptionalStep("/bin/launchctl", ["print", launchAgent.serviceTarget]); const disabled = yield* readLaunchAgentDisabled(); const current = - unit === renderBootServiceLaunchAgent(plan) && + isCurrentLaunchAgentDefinition(unit, plan) && entryExists && activeRuntimeIsCurrent && loaded &&