diff --git a/apps/server/src/cloud/bootService.test.ts b/apps/server/src/cloud/bootService.test.ts index 814d470cad410..86f855b1e2ab2 100644 --- a/apps/server/src/cloud/bootService.test.ts +++ b/apps/server/src/cloud/bootService.test.ts @@ -108,7 +108,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* ( const statePath = path.join(baseDir, "runtime", "service-state.json"); const runtime = pinnedRuntimePaths(path, baseDir, "1.2.3", platform); yield* fs.makeDirectory(path.dirname(runtime.entryPath), { recursive: true }); - yield* fs.writeFileString(runtime.entryPath, "#!/bin/sh\n"); + yield* fs.writeFileString(runtime.entryPath, "#!/bin/sh\n", { mode: 0o755 }); yield* fs.writeFileString(runtime.sentinelPath, "1.2.3\n"); const commands: string[] = []; @@ -135,6 +135,9 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* ( invocations.push(input); timeouts.set(command, input.timeout); const failed = command === control.failCommand; + // GNU cp has no -c; leave a partial copy so the fallback has to clear it. + const copyRejected = input.command === "cp" && input.args[0] === "-c"; + if (copyRejected) yield* fs.writeFileString(input.args.at(-1) ?? "", "partial"); if (!failed && command === "loginctl enable-linger --no-ask-password 501") control.linger = "yes"; if (!failed && command === "systemctl --user enable t3code.service") control.enabled = true; @@ -146,7 +149,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* ( (command === "systemctl --user stop t3code.service" || command.startsWith("launchctl bootout --wait ")) ) { - yield* fs.writeFileString(statePath, control.stateAfterStop).pipe(Effect.orDie); + yield* fs.writeFileString(statePath, control.stateAfterStop); } return { stdout: @@ -173,6 +176,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* ( stderr: "", code: ChildProcessSpawner.ExitCode( failed || + copyRejected || (input.args[1] === "is-active" && !control.active) || (input.args[1] === "is-enabled" && !control.enabled) ? 1 @@ -184,7 +188,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* ( stdoutInvalidUtf8: false, stderrInvalidUtf8: false, }; - }), + }, Effect.orDie), }); const makeService = ( environmentPath: string | undefined = installerPath, @@ -196,7 +200,7 @@ const makeHarness = Effect.fn("test.make_boot_service_harness")(function* ( Effect.gen(function* () { const paths = pinnedRuntimePaths(path, serviceBaseDir, cliVersion, platform); yield* fs.makeDirectory(path.dirname(paths.entryPath), { recursive: true }); - yield* fs.writeFileString(paths.entryPath, "#!/bin/sh\n"); + yield* fs.writeFileString(paths.entryPath, "#!/bin/sh\n", { mode: 0o755 }); yield* fs.writeFileString(paths.sentinelPath, `${cliVersion}\n`); return yield* BootService.make({ baseDir: serviceBaseDir, @@ -560,7 +564,9 @@ it.layer(NodeServices.layer)("boot service install", (it) => { protocol: SERVICE_LAUNCHER_PROTOCOL, activeVersion: "1.2.4", }); - expect(yield* fs.readFileString(plan.unitPath)).toContain("versions/1.2.4/t3"); + expect(yield* fs.readFileString(plan.unitPath)).toContain( + platform === "darwin" ? "runtime/launcher/t3" : "versions/1.2.4/t3", + ); expect( commands.filter( (command) => @@ -839,8 +845,9 @@ it.layer(NodeServices.layer)("boot service install", (it) => { it.effect("installs, reports current state, and uninstalls on macOS", () => Effect.gen(function* () { - const { service, fs, statePath, commands, timeouts, runtime } = yield* makeHarness("darwin"); + const { service, fs, baseDir, statePath, commands, timeouts } = yield* makeHarness("darwin"); const path = yield* Path.Path; + const launcherDir = path.join(baseDir, "runtime", "launcher"); const plan = yield* service.install(); expect( @@ -856,13 +863,18 @@ it.layer(NodeServices.layer)("boot service install", (it) => { activeVersion: "1.2.3", }); expect(yield* fs.readFileString(plan.unitPath)).toContain( - ` ${runtime.entryPath}\n __service-launcher`, + ` ${path.join(launcherDir, "t3")}\n __service-launcher`, ); expect(yield* service.status).toMatchObject({ current: true, installedVersion: "1.2.3", }); + yield* fs.writeFileString(path.join(launcherDir, ".install-complete"), "1.2.2\n"); + expect((yield* service.status).current).toBe(false); + yield* service.install(); + expect((yield* service.status).current).toBe(true); expect(yield* service.uninstall).toBe(true); + expect(yield* fs.exists(launcherDir)).toBe(false); expect((yield* service.status).installed).toBe(false); expect(commands.some((command) => command.startsWith("systemctl "))).toBe(false); // launchd can take 90s to bootout; the runner's 60s default could let bootstrap race it. @@ -872,6 +884,111 @@ it.layer(NodeServices.layer)("boot service install", (it) => { }), ); + // regression: #17225 macOS privacy grants are keyed to the launch agent's program path + it.effect("keeps the launch agent program at one path across version updates", () => + Effect.gen(function* () { + const { service, makeService, fs } = yield* makeHarness("darwin"); + const plan = yield* service.install(); + const program = BootService.bootServiceProgramOf(yield* fs.readFileString(plan.unitPath)); + + yield* (yield* makeService(undefined, "1.2.4")).install(); + const updatedProgram = BootService.bootServiceProgramOf( + yield* fs.readFileString(plan.unitPath), + ); + + expect(updatedProgram).toBe(program); + expect(updatedProgram).not.toContain("/runtime/versions/"); + expect(yield* fs.exists(updatedProgram ?? "")).toBe(true); + }), + ); + + it.effect("swaps in a new launch agent runtime without writing into the running one", () => + Effect.gen(function* () { + const { service, makeService, fs, baseDir } = yield* makeHarness("darwin"); + const path = yield* Path.Path; + const launcherDir = path.join(baseDir, "runtime", "launcher"); + const versionEntry = (version: string) => + path.join(baseDir, "runtime", "versions", version, "t3"); + yield* service.install(); + const running = yield* fs.open(path.join(launcherDir, "t3"), { flag: "r" }); + + const newer = yield* makeService(undefined, "1.2.4"); + yield* fs.writeFileString(versionEntry("1.2.4"), "#!/bin/sh\n# 1.2.4\n"); + yield* newer.install({ start: false }); + + const buffer = new Uint8Array(64); + const read = yield* running.read(buffer); + expect(new TextDecoder().decode(buffer.subarray(0, Number(read)))).toBe("#!/bin/sh\n"); + expect(yield* fs.readFileString(path.join(launcherDir, "t3"))).toBe("#!/bin/sh\n# 1.2.4\n"); + expect((yield* fs.stat(path.join(launcherDir, "t3"))).mode & 0o111).toBe( + (yield* fs.stat(versionEntry("1.2.4"))).mode & 0o111, + ); + + const failingSwap = yield* makeService(undefined, "1.2.5").pipe( + Effect.provideService(FileSystem.FileSystem, { + ...fs, + rename: (from, to) => + to === path.join(launcherDir, "t3") + ? fs.rename(path.join(from, "missing"), to) + : fs.rename(from, to), + }), + ); + expect((yield* Effect.flip(failingSwap.install({ start: false })))._tag).toBe( + "BootServiceInstallError", + ); + expect(yield* fs.readFileString(path.join(launcherDir, ".install-complete"))).toBe("1.2.4\n"); + expect(yield* fs.readFileString(path.join(launcherDir, "t3"))).toBe("#!/bin/sh\n# 1.2.4\n"); + + const failingCopy = yield* makeService(undefined, "1.2.6").pipe( + Effect.provideService(FileSystem.FileSystem, { + ...fs, + copyFile: (from, to) => fs.copyFile(path.join(from, "missing"), to), + }), + ); + expect((yield* Effect.flip(failingCopy.install({ start: false })))._tag).toBe( + "BootServiceInstallError", + ); + expect(yield* fs.readDirectory(launcherDir)).toEqual( + expect.arrayContaining(["t3", ".install-complete"]), + ); + expect(yield* fs.readDirectory(launcherDir)).toHaveLength(2); + expect(yield* fs.readFileString(path.join(launcherDir, "t3"))).toBe("#!/bin/sh\n# 1.2.4\n"); + }), + ); + + it.effect("keeps a launch agent program in place when an update dies mid-publish", () => + Effect.gen(function* () { + const { service, makeService, fs, baseDir } = yield* makeHarness("darwin"); + const path = yield* Path.Path; + const launcherDir = path.join(baseDir, "runtime", "launcher"); + const plan = yield* service.install(); + const newer = yield* makeService(undefined, "1.2.4").pipe( + Effect.provideService(FileSystem.FileSystem, { + ...fs, + rename: (from, to) => + fs + .rename(from, to) + .pipe( + Effect.andThen( + from === launcherDir || to === path.join(launcherDir, "t3") + ? Effect.die("process died") + : Effect.void, + ), + ), + }), + ); + yield* Effect.exit(newer.install({ start: false })); + + const program = BootService.bootServiceProgramOf(yield* fs.readFileString(plan.unitPath)); + expect(program).toBe(path.join(launcherDir, "t3")); + expect((yield* fs.stat(program ?? "")).mode & 0o111).toBe(0o111); + const current = yield* makeService(undefined, "1.2.4"); + expect((yield* current.status).current).toBe(false); + yield* current.install(); + expect((yield* current.status).current).toBe(true); + }), + ); + // regression: #8173 keep provider CLIs available in the macOS service it.effect("reconstructs a launch agent search path when the installer has no PATH", () => Effect.gen(function* () { diff --git a/apps/server/src/cloud/bootService.ts b/apps/server/src/cloud/bootService.ts index 88d477974ae0a..0c59abcef37d8 100644 --- a/apps/server/src/cloud/bootService.ts +++ b/apps/server/src/cloud/bootService.ts @@ -23,6 +23,7 @@ import { pinnedRuntimePaths, pinnedRuntimeVersionsDir, PinnedRuntimeInstallError, + type PinnedRuntimePaths, } from "./pinnedRuntime.ts"; import { SERVICE_LAUNCHER_PROTOCOL, @@ -210,6 +211,8 @@ export interface BootServiceManager { readonly handoff?: ReadonlyArray; readonly deactivate: ReadonlyArray; readonly finalize: ReadonlyArray; + /** The unit starts a copy of the runtime kept here instead of its versioned directory. */ + readonly launcherDir?: string; } function systemdManager(input: { @@ -285,6 +288,7 @@ function systemdManager(input: { function launchdManager(input: { readonly path: Path.Path; readonly homeDir: string; + readonly baseDir: string; readonly uid: number; readonly environmentPath: string; }): BootServiceManager { @@ -347,12 +351,15 @@ function launchdManager(input: { }, ], finalize: [], + // TCC keys the launch agent's privacy grants to its program path, so it must not change. + launcherDir: input.path.join(input.baseDir, "runtime", "launcher"), }; } function selectBootServiceManager(input: { readonly platform: NodeJS.Platform; readonly homeDir: string; + readonly baseDir: string; readonly xdgConfigHome: string; readonly systemdUnitDir: string | undefined; readonly uid: number | undefined; @@ -380,6 +387,7 @@ function selectBootServiceManager(input: { return launchdManager({ path: input.path, homeDir: input.homeDir, + baseDir: input.baseDir, uid: input.uid, environmentPath: input.environmentPath, }); @@ -645,6 +653,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { const detectedManager = selectBootServiceManager({ platform, homeDir, + baseDir: input.baseDir, xdgConfigHome, systemdUnitDir, uid, @@ -655,6 +664,15 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { const logPath = path.join(input.logsDir, BOOT_SERVICE_LOG_FILE); const statePath = path.join(input.baseDir, "runtime", SERVICE_STATE_FILE); const restartPendingPath = path.join(input.baseDir, "runtime", SERVICE_RESTART_PENDING_FILE); + const launcherDir = detectedManager?.launcherDir; + const launcherPaths: PinnedRuntimePaths = + launcherDir === undefined + ? runtimePaths + : { + versionDir: launcherDir, + entryPath: path.join(launcherDir, path.basename(runtimePaths.entryPath)), + sentinelPath: path.join(launcherDir, path.basename(runtimePaths.sentinelPath)), + }; const otherBaseDirOf = (unit: string) => { const installedBaseDir = bootServiceBaseDirOf(unit); return installedBaseDir !== undefined && @@ -682,12 +700,41 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { }), ).pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause }))); const plan: BootServicePlan = { - program: [runtimePaths.entryPath, "__service-launcher"], + program: [launcherPaths.entryPath, "__service-launcher"], baseDir: input.baseDir, logPath, unitPath, }; + // cp -c clones on APFS and copies all bytes where cloning is unsupported; copyFile never clones. + const copyLauncher = (source: string, destination: string) => + runner + .run({ command: "cp", args: ["-c", "-p", source, destination], timeout: Duration.minutes(1) }) + .pipe( + Effect.filterOrFail((result) => result.code === 0), + Effect.catch(() => fs.copyFile(source, destination)), + ); + // `__service-launcher` loads only Node built-ins, so the executable alone is the launcher. + // Each file is renamed over its predecessor: the program path never goes missing, even if the + // process dies mid-publish, and a running launcher keeps its old inode instead of being written into. + const publishLauncher = Effect.fn("cloud.boot_service.publish_launcher")(function* () { + yield* fs.makeDirectory(launcherPaths.versionDir, { recursive: true }); + const staging = yield* fs.makeTempDirectory({ + directory: launcherPaths.versionDir, + prefix: ".staging-", + }); + yield* Effect.gen(function* () { + const entry = path.join(staging, path.basename(launcherPaths.entryPath)); + const sentinel = path.join(staging, path.basename(launcherPaths.sentinelPath)); + yield* copyLauncher(runtimePaths.entryPath, entry); + yield* fs.writeFileString(sentinel, `${input.cliVersion}\n`); + yield* fs.rename(entry, launcherPaths.entryPath); + yield* fs.rename(sentinel, launcherPaths.sentinelPath); + }).pipe( + Effect.ensuring(fs.remove(staging, { recursive: true, force: true }).pipe(Effect.ignore)), + ); + }); + const requireManager = Effect.suspend(() => detectedManager === undefined ? new BootServiceUnsupportedError({ platform }) @@ -950,6 +997,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { return yield* new BootServiceUpdatePendingError(); } } + if (manager.launcherDir !== undefined) yield* publishLauncher(); yield* writeDurably(unitPath, manager.render(plan)); if (start) { @@ -1055,9 +1103,14 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { ) return false; yield* runSteps(manager.deactivate); - yield* fs - .remove(unitPath) - .pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause }))); + yield* fs.remove(unitPath).pipe( + Effect.andThen( + manager.launcherDir === undefined + ? Effect.void + : fs.remove(manager.launcherDir, { recursive: true, force: true }), + ), + Effect.mapError((cause) => new BootServiceInstallError({ cause })), + ); yield* runSteps(manager.finalize); return true; }).pipe(Effect.withSpan("cloud.boot_service.uninstall")); @@ -1069,10 +1122,20 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { if (!(yield* fs.exists(unitPath))) { return { supported: true, installed: false, current: false, unitPath, logPath }; } - const [unit, runtimeEntryExists, runtimeSentinel, stateText] = yield* Effect.all([ + const runtimeCurrent = (paths: PinnedRuntimePaths) => + Effect.all([ + fs.exists(paths.entryPath), + fs.readFileString(paths.sentinelPath).pipe(Effect.option), + ]).pipe( + Effect.map( + ([entryExists, sentinel]) => + entryExists && Option.isSome(sentinel) && sentinel.value.trim() === input.cliVersion, + ), + ); + const [unit, runtimeIsCurrent, launcherIsCurrent, stateText] = yield* Effect.all([ fs.readFileString(unitPath), - fs.exists(runtimePaths.entryPath), - fs.readFileString(runtimePaths.sentinelPath).pipe(Effect.option), + runtimeCurrent(runtimePaths), + runtimeCurrent(launcherPaths), fs.readFileString(statePath).pipe(Effect.option), ]); const state = Option.isSome(stateText) ? parseServiceState(stateText.value) : undefined; @@ -1098,9 +1161,8 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { current: problems.length === 0 && normalizeUnit(unit) === normalizeUnit(detectedManager.render(plan)) && - runtimeEntryExists && - Option.isSome(runtimeSentinel) && - runtimeSentinel.value.trim() === input.cliVersion && + runtimeIsCurrent && + launcherIsCurrent && state?.activeVersion === input.cliVersion && state?.update?.status !== "pending", unitPath, diff --git a/docs/user/background-service.md b/docs/user/background-service.md index 51a16878d2bbc..b85babc03ad76 100644 --- a/docs/user/background-service.md +++ b/docs/user/background-service.md @@ -154,7 +154,10 @@ On macOS, check **System Settings → General → Login Items** if the service n longer starts at login. If agent work cannot access Desktop, Documents, or Downloads, it may need Full Disk Access for the `t3` executable listed in `ProgramArguments` in -`~/Library/LaunchAgents/com.t3tools.t3code.service.plist`. +`~/Library/LaunchAgents/com.t3tools.t3code.service.plist`. That path stays the +same across updates, so the grant carries over. A service installed by an +older version moves to this path on its next update and needs the grant once +more. For failures after signing in to T3 Connect, see [connection troubleshooting](./t3-connect-troubleshooting.md).