Skip to content

Commit 1baadc6

Browse files
PR Batch Testerclaude
andcommitted
fix(server): t3 service install no longer rebinds another home's service
A CLI run without --base-dir saw a service installed with one as broken, and `t3 service install` then rewrote the unit for ~/.t3, so the service came back on an empty data dir. Install now refuses when the unit serves another T3 home, and `t3 service status` and onboarding name that home instead of offering a repair. Closes #23858 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2c8bdf6 commit 1baadc6

5 files changed

Lines changed: 113 additions & 2 deletions

File tree

‎apps/server/src/cli/service.test.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,20 @@ it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands
9090
);
9191
});
9292

93-
it.effect("leaves a newer service unchanged during onboarding without prompting", () =>
93+
it.effect.each([
94+
["a newer service", newerServiceStatus],
95+
[
96+
"a service for another T3 home",
97+
{
98+
...status,
99+
current: false,
100+
installedBaseDir: "/mnt/workspace/t3",
101+
otherBaseDir: "/mnt/workspace/t3",
102+
},
103+
],
104+
] as const)("leaves %s unchanged during onboarding without prompting", ([, serviceStatus]) =>
94105
Effect.gen(function* () {
95-
const { service, installOptions } = makeTestService(newerServiceStatus);
106+
const { service, installOptions } = makeTestService(serviceStatus);
96107
const terminal = Terminal.make({
97108
columns: Effect.succeed(80),
98109
rows: Effect.succeed(24),

‎apps/server/src/cli/service.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ function formatServiceStatus(status: BootService.BootServiceStatus, cliVersion:
7474
if (!status.installed) {
7575
return "T3 Code service\n Status: not installed\n Next: Run `t3 service install`.";
7676
}
77+
if (status.otherBaseDir !== undefined) {
78+
return [
79+
"T3 Code service",
80+
` Status: installed for another T3 home`,
81+
` Base dir: ${status.otherBaseDir}`,
82+
` Unit: ${status.unitPath}`,
83+
` Next: Pass \`--base-dir ${status.otherBaseDir}\` to manage it.`,
84+
].join("\n");
85+
}
7786
const installedVersion = status.installedVersion ?? cliVersion;
7887
const problems = (status.problems ?? []).map(
7988
(problem) => ` [${problem}] ${BootService.formatBootServiceProblem(problem)}`,
@@ -226,6 +235,12 @@ export const offerServiceDuringOnboarding = Effect.gen(function* () {
226235
yield* Console.log("T3 Code is already set up to run in the background on this machine.");
227236
return "ready" as const;
228237
}
238+
if (status.otherBaseDir !== undefined) {
239+
yield* Console.log(
240+
`A T3 Code service for ${status.otherBaseDir} is already installed. Leaving it unchanged.`,
241+
);
242+
return "skipped" as const;
243+
}
229244
for (const problem of status.problems ?? []) {
230245
yield* Console.warn(`[${problem}] ${BootService.formatBootServiceProblem(problem)}`);
231246
}
@@ -292,6 +307,7 @@ export const recoverServiceOnboardingOffer = <R>(
292307
BootServicePrerequisiteError: reportServiceSetupFailure,
293308
BootServiceUpdatePendingError: reportServiceSetupFailure,
294309
BootServiceDowngradeRefusedError: reportServiceSetupFailure,
310+
BootServiceOtherBaseDirError: reportServiceSetupFailure,
295311
}),
296312
);
297313

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,31 @@ it.layer(NodeServices.layer)("boot service install", (it) => {
610610
}),
611611
);
612612

613+
it.effect.each(["linux", "darwin"] as const)(
614+
"install refuses to rebind a service that serves another T3 home on %s",
615+
(platform) =>
616+
Effect.gen(function* () {
617+
const { service, fs, baseDir, commands, makeService } = yield* makeHarness(platform);
618+
const plan = yield* service.install();
619+
const unit = yield* fs.readFileString(plan.unitPath);
620+
const path = yield* Path.Path;
621+
const otherHome = yield* fs.makeTempDirectoryScoped({ prefix: "t3-other-home-" });
622+
const other = yield* makeService(undefined, "1.2.3", path.join(otherHome, ".t3"));
623+
624+
expect(yield* other.status).toMatchObject({
625+
installed: true,
626+
current: false,
627+
otherBaseDir: baseDir,
628+
});
629+
commands.length = 0;
630+
const error = yield* other.install().pipe(Effect.flip);
631+
expect(error).toMatchObject({ _tag: "BootServiceOtherBaseDirError" });
632+
expect(error.message).toContain(`--base-dir ${baseDir}`);
633+
expect(yield* fs.readFileString(plan.unitPath)).toBe(unit);
634+
expect(commands).toEqual([]);
635+
}),
636+
);
637+
613638
it.effect("hands the whole install to systemd without changing the running service", () =>
614639
Effect.gen(function* () {
615640
const { fs, commands, invocations, control, makeService } = yield* makeHarness();

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,21 @@ export class BootServiceDowngradeRefusedError extends Schema.TaggedError<BootSer
490490
}
491491
}
492492

493+
export class BootServiceOtherBaseDirError extends Schema.TaggedError<BootServiceOtherBaseDirError>()(
494+
"BootServiceOtherBaseDirError",
495+
{
496+
installedBaseDir: Schema.String,
497+
baseDir: Schema.String,
498+
},
499+
) {
500+
override get message(): string {
501+
return `The T3 Code service on this machine serves ${this.installedBaseDir}, not ${this.baseDir}. Pass --base-dir ${this.installedBaseDir} to manage it. To move the service to ${this.baseDir} instead, run \`t3 service uninstall\` first; both data directories are kept.`;
502+
}
503+
}
504+
493505
export type BootServiceError =
494506
| BootServiceUnsupportedError
507+
| BootServiceOtherBaseDirError
495508
| BootServiceCommandError
496509
| BootServiceInstallError
497510
| BootServicePrerequisiteError
@@ -504,6 +517,11 @@ export interface BootServiceStatus {
504517
readonly current: boolean;
505518
readonly installedVersion?: string;
506519
readonly installedBaseDir?: string;
520+
/**
521+
* Set when the installed unit serves a different T3 home than this CLI. The
522+
* unit name is per user, so this CLI must leave that install alone.
523+
*/
524+
readonly otherBaseDir?: string;
507525
readonly problems?: ReadonlyArray<BootServiceProblem>;
508526
readonly unitPath: string;
509527
readonly logPath: string;
@@ -598,6 +616,13 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
598616
const statePath = path.join(input.baseDir, "runtime", SERVICE_STATE_FILE);
599617
const restartPendingPath = path.join(input.baseDir, "runtime", SERVICE_RESTART_PENDING_FILE);
600618
const runtimePaths = pinnedRuntimePaths(path, input.baseDir, input.cliVersion, platform);
619+
const otherBaseDirOf = (unit: string) => {
620+
const installedBaseDir = bootServiceBaseDirOf(unit);
621+
return installedBaseDir !== undefined &&
622+
path.resolve(installedBaseDir) !== path.resolve(input.baseDir)
623+
? installedBaseDir
624+
: undefined;
625+
};
601626
const writeDurably = (filePath: string, contents: string) =>
602627
Effect.scoped(
603628
Effect.gen(function* () {
@@ -740,6 +765,21 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
740765
readonly start?: boolean;
741766
}) {
742767
const manager = yield* requireManager;
768+
// Rewriting another home's unit stops that server and restarts the service
769+
// on this home's data, which looks to the user like every thread vanished.
770+
const existingUnit = yield* fs.readFileString(unitPath).pipe(
771+
Effect.option,
772+
Effect.mapError((cause) => new BootServiceInstallError({ cause })),
773+
);
774+
const existingBaseDir = Option.isSome(existingUnit)
775+
? otherBaseDirOf(existingUnit.value)
776+
: undefined;
777+
if (existingBaseDir !== undefined) {
778+
return yield* new BootServiceOtherBaseDirError({
779+
installedBaseDir: existingBaseDir,
780+
baseDir: input.baseDir,
781+
});
782+
}
743783
yield* fs
744784
.makeDirectory(input.logsDir, { recursive: true })
745785
.pipe(Effect.mapError((cause) => new BootServiceInstallError({ cause })));
@@ -1021,6 +1061,20 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
10211061
? serviceStateActiveVersion(stateText.value)
10221062
: undefined;
10231063
const installedBaseDir = bootServiceBaseDirOf(unit);
1064+
const otherBaseDir = otherBaseDirOf(unit);
1065+
// The state, runtime and logs read above belong to this home, not to the
1066+
// one the unit serves, so they say nothing about that install.
1067+
if (otherBaseDir !== undefined) {
1068+
return {
1069+
supported: true,
1070+
installed: true,
1071+
current: false,
1072+
installedBaseDir: otherBaseDir,
1073+
otherBaseDir,
1074+
unitPath,
1075+
logPath,
1076+
};
1077+
}
10241078
const normalizeUnit = (contents: string) =>
10251079
detectedManager.kind === "launchd"
10261080
? contents.replace(/(<key>PATH<\/key>\n\s*<string>)[^<]*(<\/string>)/, "$1$2")

‎docs/user/background-service.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ Uninstalling the service leaves your projects, threads, and settings intact.
2020
Running `t3 service install` again repairs a service that `t3 service status`
2121
reports as broken.
2222

23+
There is one service per user. If you installed it with `--base-dir`, pass the
24+
same `--base-dir` to every `t3 service` command. A command run for another T3
25+
home leaves the service alone and tells you which home it serves. To move the
26+
service to a different home, run `t3 service uninstall` first.
27+
2328
`t3 update` downloads the newest release on your channel and switches `t3`
2429
and the service to it. Restarting interrupts running agent turns, terminals,
2530
and remote clients, so it asks first; answer no and the service keeps running

0 commit comments

Comments
 (0)