feat(connect): keep macOS hosts reachable after reboot

This commit is contained in:
Theo Browne committed 2026-07-30 03:16:05 -07:00
1 parent 90f3913a81
commit ec3c5f68d2
12 files changed
+690 -91

No files matched your search

+142 -2
View File
@@ -12,6 +12,7 @@ import {
HostProcessArguments,
HostProcessExecutablePath,
HostProcessPlatform,
HostProcessUserId,
} from "@t3tools/shared/hostProcess";
import { reconcileService } from "../cli/service.ts";
@@ -64,6 +65,7 @@ const provideHostRefs = (home: string, platform: NodeJS.Platform = "linux") =>
Effect.provide(
Layer.mergeAll(
Layer.succeed(HostProcessPlatform, platform),
Layer.succeed(HostProcessUserId, 501),
ConfigProvider.layer(ConfigProvider.fromEnv({ env: { HOME: home } })),
),
);
@@ -143,6 +145,34 @@ it("quotes systemd values containing spaces and escapes percent specifiers", ()
assert.include(unit, "StandardError=append:/home/me/100%%logs/boot.log");
});
it("renders a restartable per-user macOS LaunchAgent", () => {
const launchAgent = BootService.renderBootServiceLaunchAgent({
nodePath: "/opt/homebrew/bin/node",
t3EntryPath: "/Users/theo/T3 & Data/runtime/service/current/node_modules/t3/dist/bin.mjs",
baseDir: "/Users/theo/T3 & Data",
logPath: "/Users/theo/T3 & Data/userdata/logs/boot-service.log",
unitPath: "/Users/theo/Library/LaunchAgents/com.t3tools.t3code.server.plist",
pathEnvironment: "/opt/homebrew/bin:/usr/bin:/bin",
});
assert.include(launchAgent, "<string>com.t3tools.t3code.server</string>");
assert.include(launchAgent, "<key>RunAtLoad</key>\n <true/>");
assert.include(launchAgent, "<key>KeepAlive</key>\n <true/>");
assert.include(launchAgent, "<string>/Users/theo/T3 &amp; Data</string>");
assert.include(
launchAgent,
"<string>/Users/theo/T3 &amp; Data/runtime/service/current/node_modules/t3/dist/bin.mjs</string>",
);
assert.include(
launchAgent,
"<key>T3_BOOT_SERVICE_UNIT</key>\n <string>com.t3tools.t3code.server</string>",
);
assert.include(
launchAgent,
"<key>PATH</key>\n <string>/opt/homebrew/bin:/usr/bin:/bin</string>",
);
});
it("flags package-manager cache entry points as ephemeral", () => {
assert.isTrue(
BootService.isEphemeralCacheEntry("/home/theo/.npm/_npx/abc123/node_modules/t3/dist/bin.mjs"),
@@ -388,7 +418,117 @@ it.layer(NodeServices.layer)("BootService", (it) => {
}),
);
it.effect("fails on non-Linux platforms without touching the filesystem", () =>
it.effect("installs and starts a pinned macOS LaunchAgent", () =>
Effect.gen(function* () {
const { dirs, fs, path } = yield* makeTestContext();
const commands: Array<RecordedCommand> = [];
const service = yield* BootService.make({
baseDir: dirs.baseDir,
logsDir: dirs.logsDir,
cliVersion: "0.0.27",
host: makeHost(dirs.stableEntry),
}).pipe(
Effect.provide(makeRecordingRunnerLayer(commands)),
provideHostRefs(dirs.home, "darwin"),
);
const plan = yield* service.install;
const runtimeDir = path.join(dirs.baseDir, "runtime", "versions", "0.0.27");
const activeRuntimeLink = path.join(dirs.baseDir, "runtime", "service", "current");
const launchAgentPath = path.join(
dirs.home,
"Library",
"LaunchAgents",
"com.t3tools.t3code.server.plist",
);
assert.equal(
plan.t3EntryPath,
path.join(activeRuntimeLink, "node_modules", "t3", "dist", "bin.mjs"),
);
assert.equal(yield* fs.readLink(activeRuntimeLink), runtimeDir);
assert.deepEqual(
commands.map((entry) => [entry.command, ...entry.args].join(" ")),
[
`npm install --prefix ${runtimeDir} --no-fund --no-audit [email protected]`,
"/bin/launchctl enable gui/501/com.t3tools.t3code.server",
"/bin/launchctl bootout gui/501/com.t3tools.t3code.server",
`/bin/launchctl bootstrap gui/501 ${launchAgentPath}`,
"/bin/launchctl print gui/501/com.t3tools.t3code.server",
],
);
const plist = yield* fs.readFileString(launchAgentPath);
assert.include(plist, `<string>${plan.t3EntryPath}</string>`);
assert.notInclude(plist, dirs.stableEntry);
// The recording npm runner does not materialize files, so complete the
// managed runtime before checking the current-state contract.
yield* fs.makeDirectory(path.dirname(path.join(runtimeDir, "node_modules/t3/dist/bin.mjs")), {
recursive: true,
});
yield* fs.writeFileString(
path.join(runtimeDir, "node_modules", "t3", "dist", "bin.mjs"),
"#!/usr/bin/env node\n",
);
const status = yield* service.status;
assert.isTrue(status.supported);
assert.isTrue(status.installed);
assert.isTrue(status.current);
assert.isTrue(yield* service.uninstall);
assert.isFalse(yield* fs.exists(launchAgentPath));
}),
);
it.effect("restores the previous macOS runtime when LaunchAgent activation fails", () =>
Effect.gen(function* () {
const { dirs, fs, path } = yield* makeTestContext();
const initialCommands: Array<RecordedCommand> = [];
const initialService = yield* BootService.make({
baseDir: dirs.baseDir,
logsDir: dirs.logsDir,
cliVersion: "0.0.27",
host: makeHost(dirs.stableEntry),
}).pipe(
Effect.provide(makeRecordingRunnerLayer(initialCommands)),
provideHostRefs(dirs.home, "darwin"),
);
yield* initialService.install;
const activeRuntimeLink = path.join(dirs.baseDir, "runtime", "service", "current");
const previousRuntime = yield* fs.readLink(activeRuntimeLink);
const launchAgentPath = path.join(
dirs.home,
"Library",
"LaunchAgents",
"com.t3tools.t3code.server.plist",
);
const previousLaunchAgent = yield* fs.readFileString(launchAgentPath);
const repairCommands: Array<RecordedCommand> = [];
const repairService = yield* BootService.make({
baseDir: dirs.baseDir,
logsDir: dirs.logsDir,
cliVersion: "0.0.28",
host: makeHost(dirs.stableEntry),
}).pipe(
Effect.provide(
makeRecordingRunnerLayer(repairCommands, {
failWhen: (command, args) => command === "/bin/launchctl" && args.includes("bootstrap"),
}),
),
provideHostRefs(dirs.home, "darwin"),
);
const error = yield* repairService.install.pipe(Effect.flip);
assert.isTrue(isCommandError(error));
assert.equal(yield* fs.readLink(activeRuntimeLink), previousRuntime);
assert.equal(yield* fs.readFileString(launchAgentPath), previousLaunchAgent);
}),
);
it.effect("fails on unsupported platforms without touching the filesystem", () =>
Effect.gen(function* () {
const { dirs, fs, path } = yield* makeTestContext();
const commands: Array<RecordedCommand> = [];
@@ -399,7 +539,7 @@ it.layer(NodeServices.layer)("BootService", (it) => {
host: makeHost("/usr/local/lib/node_modules/t3/dist/bin.mjs"),
}).pipe(
Effect.provide(makeRecordingRunnerLayer(commands)),
provideHostRefs(dirs.home, "darwin"),
provideHostRefs(dirs.home, "win32"),
);
const error = yield* service.install.pipe(Effect.flip);