Skip to content

Commit 67835fc

Browse files
committed
fix(server): recover macOS launcher publication and cleanup
1 parent c3d4662 commit 67835fc

3 files changed

Lines changed: 194 additions & 28 deletions

File tree

‎apps/server/src/cloud/bootService.test.ts‎

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
44
import * as ConfigProvider from "effect/ConfigProvider";
55
import * as Duration from "effect/Duration";
66
import * as Effect from "effect/Effect";
7+
import * as Exit from "effect/Exit";
8+
import * as Fiber from "effect/Fiber";
79
import * as FileSystem from "effect/FileSystem";
810
import * as Layer from "effect/Layer";
911
import * as Path from "effect/Path";
@@ -473,7 +475,14 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
473475
protocol: SERVICE_LAUNCHER_PROTOCOL,
474476
activeVersion: "1.2.4",
475477
});
476-
expect(yield* fs.readFileString(plan.unitPath)).toContain("versions/1.2.4/t3");
478+
expect(BootService.bootServiceProgramOf(yield* fs.readFileString(plan.unitPath))).toBe(
479+
path.join(
480+
baseDir,
481+
"runtime",
482+
...(platform === "darwin" ? ["launcher"] : ["versions", "1.2.4"]),
483+
"t3",
484+
),
485+
);
477486
expect(
478487
commands.filter(
479488
(command) =>
@@ -855,6 +864,103 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
855864
}),
856865
);
857866

867+
it.effect("finishes publishing the launcher before honoring interruption", () =>
868+
Effect.gen(function* () {
869+
const { service, makeService, fs, baseDir } = yield* makeHarness("darwin");
870+
const path = yield* Path.Path;
871+
const launcherDir = path.join(baseDir, "runtime", "launcher");
872+
yield* service.install();
873+
const interruptingFs = FileSystem.FileSystem.of({
874+
...fs,
875+
rename: (from, to) =>
876+
fs
877+
.rename(from, to)
878+
.pipe(
879+
Effect.andThen(
880+
to === `${launcherDir}.prev`
881+
? Effect.withFiber((fiber) => Effect.sync(() => fiber.interruptUnsafe()))
882+
: Effect.void,
883+
),
884+
),
885+
});
886+
const updated = yield* makeService(undefined, "1.2.4").pipe(
887+
Effect.provideService(FileSystem.FileSystem, interruptingFs),
888+
);
889+
const installing = yield* Effect.forkChild(updated.install({ start: false }));
890+
expect(Exit.isFailure(yield* Fiber.await(installing))).toBe(true);
891+
expect(yield* fs.readFileString(path.join(launcherDir, ".install-complete"))).toBe("1.2.4\n");
892+
expect(yield* fs.exists(path.join(launcherDir, "t3"))).toBe(true);
893+
expect(yield* fs.exists(`${launcherDir}.next`)).toBe(false);
894+
}),
895+
);
896+
897+
it.effect("recovers the previous launcher before staging after an interrupted swap", () =>
898+
Effect.gen(function* () {
899+
const { service, makeService, fs, baseDir } = yield* makeHarness("darwin");
900+
const path = yield* Path.Path;
901+
const launcherDir = path.join(baseDir, "runtime", "launcher");
902+
yield* service.install();
903+
yield* fs.rename(launcherDir, `${launcherDir}.prev`);
904+
const failingFs = FileSystem.FileSystem.of({
905+
...fs,
906+
copy: (from, to, options) =>
907+
to === `${launcherDir}.next`
908+
? Effect.fail(
909+
PlatformError.systemError({
910+
_tag: "PermissionDenied",
911+
module: "FileSystem",
912+
method: "copy",
913+
pathOrDescriptor: to,
914+
}),
915+
)
916+
: fs.copy(from, to, options),
917+
});
918+
const updated = yield* makeService(undefined, "1.2.4").pipe(
919+
Effect.provideService(FileSystem.FileSystem, failingFs),
920+
);
921+
expect((yield* updated.install().pipe(Effect.flip))._tag).toBe("BootServiceInstallError");
922+
expect(yield* fs.readFileString(path.join(launcherDir, ".install-complete"))).toBe("1.2.3\n");
923+
expect((yield* service.status).current).toBe(true);
924+
yield* (yield* makeService(undefined, "1.2.4")).install();
925+
expect(yield* fs.readFileString(path.join(launcherDir, ".install-complete"))).toBe("1.2.4\n");
926+
}),
927+
);
928+
929+
it.effect("retries launcher cleanup after uninstall removed the plist", () =>
930+
Effect.gen(function* () {
931+
const { service, makeService, fs, baseDir } = yield* makeHarness("darwin");
932+
const path = yield* Path.Path;
933+
const launcherDir = path.join(baseDir, "runtime", "launcher");
934+
yield* service.install();
935+
yield* fs.makeDirectory(`${launcherDir}.next`);
936+
yield* fs.makeDirectory(`${launcherDir}.prev`);
937+
const failingFs = FileSystem.FileSystem.of({
938+
...fs,
939+
remove: (target, options) =>
940+
target === launcherDir
941+
? Effect.fail(
942+
PlatformError.systemError({
943+
_tag: "PermissionDenied",
944+
module: "FileSystem",
945+
method: "remove",
946+
pathOrDescriptor: target,
947+
}),
948+
)
949+
: fs.remove(target, options),
950+
});
951+
const failing = yield* makeService().pipe(
952+
Effect.provideService(FileSystem.FileSystem, failingFs),
953+
);
954+
expect((yield* failing.uninstall.pipe(Effect.flip))._tag).toBe("BootServiceInstallError");
955+
expect((yield* service.status).installed).toBe(false);
956+
expect(yield* service.uninstall).toBe(true);
957+
for (const directory of [launcherDir, `${launcherDir}.next`, `${launcherDir}.prev`]) {
958+
expect(yield* fs.exists(directory)).toBe(false);
959+
}
960+
expect(yield* service.uninstall).toBe(false);
961+
}),
962+
);
963+
858964
it.effect("puts the previous launcher copy back when the swap fails on macOS", () =>
859965
Effect.gen(function* () {
860966
const { service, makeService, fs, baseDir } = yield* makeHarness("darwin");
@@ -888,6 +994,42 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
888994
}),
889995
);
890996

997+
it.effect("retains recovery when publication and rollback both fail", () =>
998+
Effect.gen(function* () {
999+
const { service, makeService, fs, baseDir } = yield* makeHarness("darwin");
1000+
const path = yield* Path.Path;
1001+
const launcherDir = path.join(baseDir, "runtime", "launcher");
1002+
yield* service.install();
1003+
const failingFs = FileSystem.FileSystem.of({
1004+
...fs,
1005+
rename: (from, to) =>
1006+
to === launcherDir
1007+
? Effect.fail(
1008+
PlatformError.systemError({
1009+
_tag: "PermissionDenied",
1010+
module: "FileSystem",
1011+
method: "rename",
1012+
pathOrDescriptor: from,
1013+
}),
1014+
)
1015+
: fs.rename(from, to),
1016+
});
1017+
const updated = yield* makeService(undefined, "1.2.4").pipe(
1018+
Effect.provideService(FileSystem.FileSystem, failingFs),
1019+
);
1020+
expect((yield* updated.install().pipe(Effect.flip))._tag).toBe("BootServiceInstallError");
1021+
expect(yield* fs.exists(launcherDir)).toBe(false);
1022+
expect(yield* fs.readFileString(path.join(`${launcherDir}.prev`, ".install-complete"))).toBe(
1023+
"1.2.3\n",
1024+
);
1025+
expect(yield* fs.exists(`${launcherDir}.next`)).toBe(false);
1026+
const retry = yield* makeService(undefined, "1.2.4");
1027+
yield* retry.install();
1028+
expect((yield* retry.status).current).toBe(true);
1029+
expect(yield* fs.exists(`${launcherDir}.prev`)).toBe(false);
1030+
}),
1031+
);
1032+
8911033
it.effect("finishes the install when the retired launcher copy cannot be removed", () =>
8921034
Effect.gen(function* () {
8931035
const { service, makeService, fs, baseDir } = yield* makeHarness("darwin");

‎apps/server/src/cloud/bootService.ts‎

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -760,9 +760,15 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
760760
const stageLauncher = Effect.suspend(() =>
761761
launcher === undefined
762762
? Effect.void
763-
: fs
764-
.remove(launcher.stagingDir, { recursive: true, force: true })
765-
.pipe(Effect.andThen(fs.copy(runtimePaths.versionDir, launcher.stagingDir))),
763+
: Effect.gen(function* () {
764+
// A process exit between the two publication renames can leave only
765+
// the retired copy. Restore it before any fallible staging work.
766+
if (!(yield* fs.exists(launcher.dir)) && (yield* fs.exists(launcher.retiredDir))) {
767+
yield* fs.rename(launcher.retiredDir, launcher.dir);
768+
}
769+
yield* fs.remove(launcher.stagingDir, { recursive: true, force: true });
770+
yield* fs.copy(runtimePaths.versionDir, launcher.stagingDir);
771+
}),
766772
).pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause })));
767773
// Renames, never overwrites: a launcher that is still running keeps its
768774
// open executable. The fixed path holds a complete copy except for the
@@ -774,17 +780,22 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
774780
? Effect.void
775781
: Effect.gen(function* () {
776782
yield* fs.remove(launcher.retiredDir, { recursive: true, force: true });
777-
const retired = yield* fs.exists(launcher.dir);
778-
if (retired) yield* fs.rename(launcher.dir, launcher.retiredDir);
779-
yield* fs
780-
.rename(launcher.stagingDir, launcher.dir)
781-
.pipe(
782-
Effect.tapError(() =>
783-
retired
784-
? fs.rename(launcher.retiredDir, launcher.dir).pipe(Effect.ignore)
785-
: Effect.void,
786-
),
787-
);
783+
// Honor cancellation only once the fixed path is populated again.
784+
yield* Effect.uninterruptible(
785+
Effect.gen(function* () {
786+
const retired = yield* fs.exists(launcher.dir);
787+
if (retired) yield* fs.rename(launcher.dir, launcher.retiredDir);
788+
yield* fs
789+
.rename(launcher.stagingDir, launcher.dir)
790+
.pipe(
791+
Effect.tapError(() =>
792+
retired
793+
? fs.rename(launcher.retiredDir, launcher.dir).pipe(Effect.ignore)
794+
: Effect.void,
795+
),
796+
);
797+
}),
798+
);
788799
yield* fs
789800
.remove(launcher.retiredDir, { recursive: true, force: true })
790801
.pipe(Effect.ignore);
@@ -1052,23 +1063,33 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
10521063

10531064
const uninstall: BootService["Service"]["uninstall"] = Effect.gen(function* () {
10541065
const manager = yield* requireManager;
1055-
if (
1056-
!(yield* fs
1057-
.exists(unitPath)
1058-
.pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause }))))
1059-
)
1060-
return false;
1061-
yield* runSteps(manager.deactivate);
1062-
yield* fs
1063-
.remove(unitPath)
1066+
const installed = yield* fs
1067+
.exists(unitPath)
10641068
.pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause })));
1065-
if (launcher !== undefined) {
1069+
if (installed) {
1070+
yield* runSteps(manager.deactivate);
10661071
yield* fs
1067-
.remove(launcher.dir, { recursive: true, force: true })
1072+
.remove(unitPath)
10681073
.pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause })));
10691074
}
1070-
yield* runSteps(manager.finalize);
1071-
return true;
1075+
// A prior uninstall may have removed the plist before copy cleanup failed.
1076+
let removedLauncher = false;
1077+
if (launcher !== undefined) {
1078+
for (const directory of [launcher.dir, launcher.stagingDir, launcher.retiredDir]) {
1079+
if (
1080+
yield* fs
1081+
.exists(directory)
1082+
.pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause })))
1083+
) {
1084+
yield* fs
1085+
.remove(directory, { recursive: true, force: true })
1086+
.pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause })));
1087+
removedLauncher = true;
1088+
}
1089+
}
1090+
}
1091+
if (installed) yield* runSteps(manager.finalize);
1092+
return installed || removedLauncher;
10721093
}).pipe(Effect.withSpan("cloud.boot_service.uninstall"));
10731094

10741095
const status: BootService["Service"]["status"] = Effect.gen(function* () {

‎docs/user/background-service.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ longer starts at login. If agent work cannot access Desktop, Documents, or
116116
Downloads, it may need Full Disk Access for the `t3` executable listed in
117117
`ProgramArguments` in
118118
`~/Library/LaunchAgents/com.t3tools.t3code.service.plist`.
119+
The service uses `<T3 home>/runtime/launcher/t3` at a fixed path across updates.
120+
When upgrading an older service, approve this executable if macOS asks; an
121+
existing grant for a versioned executable does not move to the new path.
119122

120123
For failures after signing in to T3 Connect, see
121124
[connection troubleshooting](./t3-connect-troubleshooting.md).

0 commit comments

Comments
 (0)