Skip to content

Commit f4e9d6c

Browse files
rookmateclaude
andcommitted
fix(server): report handed-off installs and queued restarts as pending
An in-service install hands its work to a transient unit and an in-service restart only queues one, so neither is done when the command returns. The transient install now appends its output to boot-service.log, restart resolves "queued" instead of true, and onboarding resolves "pending", so `t3 update`, `t3 service install/restart` and `t3 connect` say the switch is under way instead of reporting it finished. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 5589ddf commit f4e9d6c

7 files changed

Lines changed: 224 additions & 67 deletions

File tree

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
acquireRelayClientForLink,
1616
headlessSessionConfig,
1717
reportCloudDisconnectResults,
18+
reportServiceOnboarding,
1819
} from "./connect.ts";
1920
import { recoverServiceOnboardingOffer } from "./service.ts";
2021

@@ -40,7 +41,7 @@ it.effect("detects headless operation from individual SSH config values", () =>
4041
it.effect("treats cancelling optional background setup as a successful skip", () =>
4142
Effect.gen(function* () {
4243
const result = yield* recoverServiceOnboardingOffer(Effect.fail(new Terminal.QuitError({})));
43-
assert.isFalse(result);
44+
assert.equal(result, "skipped");
4445
}),
4546
);
4647

@@ -49,10 +50,32 @@ it.effect("keeps a successful connection when a remote service update is pending
4950
const result = yield* recoverServiceOnboardingOffer(
5051
Effect.fail(new BootService.BootServiceUpdatePendingError()),
5152
);
52-
assert.isFalse(result);
53+
assert.equal(result, "skipped");
5354
}),
5455
);
5556

57+
it.effect("does not announce a background service that is still switching over", () => {
58+
const lines: Array<string> = [];
59+
const testConsole = {
60+
...globalThis.console,
61+
log: (...args: ReadonlyArray<unknown>) => {
62+
lines.push(args.map(String).join(" "));
63+
},
64+
} satisfies Console.Console;
65+
66+
return reportServiceOnboarding("pending").pipe(
67+
Effect.provideService(Console.Console, testConsole),
68+
Effect.tap(() =>
69+
Effect.sync(() => {
70+
const output = lines.join("\n");
71+
assert.notInclude(output, "ready");
72+
assert.notInclude(output, "Start the server");
73+
assert.include(output, "Background service restarting");
74+
}),
75+
),
76+
);
77+
});
78+
5679
it.effect("does not install the relay client when the user declines the managed download", () =>
5780
Effect.gen(function* () {
5881
let installCalls = 0;

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
bootServiceLayer,
5454
offerServiceDuringOnboarding,
5555
recoverServiceOnboardingOffer,
56+
type ServiceOnboardingOutcome,
5657
} from "./service.ts";
5758

5859
const jsonFlag = Flag.Boolean("json").pipe(
@@ -670,6 +671,31 @@ const connectLogoutCommand = Command.make("logout", {
670671
),
671672
);
672673

674+
/** What `t3 connect` tells the user once background setup has settled. */
675+
export const reportServiceOnboarding = Effect.fn("cli.connect.report_service_onboarding")(
676+
function* (outcome: ServiceOnboardingOutcome) {
677+
if (outcome === "pending") {
678+
yield* Console.log(
679+
"\nBackground service restarting\n\nThe server establishes the T3 Connect link once it is back. Check the log above if it does not come back.",
680+
);
681+
return;
682+
}
683+
if (outcome === "ready") {
684+
const platform = yield* HostProcessPlatform;
685+
yield* Console.log(
686+
platform === "darwin"
687+
? "\n✓ Background service ready\n\nT3 Code is set to run while you are logged in to this Mac. The server establishes the T3 Connect link on startup."
688+
: "\n✓ Background service ready\n\nT3 Code is set to keep running after you log out. The server establishes the T3 Connect link on startup.",
689+
);
690+
return;
691+
}
692+
const serveCommand = yield* resolveCliCommand("serve");
693+
yield* Console.log(
694+
`\nNext\n Start the server with \`${serveCommand}\` to make this machine reachable.`,
695+
);
696+
},
697+
);
698+
673699
export const connectCommand = Command.make("connect", {
674700
...projectLocationFlags,
675701
headless: headlessFlag,
@@ -691,19 +717,8 @@ export const connectCommand = Command.make("connect", {
691717

692718
// Authorization is stored. If service setup fails, preserve it and
693719
// show how to run the server manually.
694-
const background = yield* recoverServiceOnboardingOffer(offerServiceDuringOnboarding);
695-
if (background) {
696-
const platform = yield* HostProcessPlatform;
697-
yield* Console.log(
698-
platform === "darwin"
699-
? "\n✓ Background service ready\n\nT3 Code is set to run while you are logged in to this Mac. The server establishes the T3 Connect link on startup."
700-
: "\n✓ Background service ready\n\nT3 Code is set to keep running after you log out. The server establishes the T3 Connect link on startup.",
701-
);
702-
return;
703-
}
704-
const serveCommand = yield* resolveCliCommand("serve");
705-
yield* Console.log(
706-
`\nNext\n Start the server with \`${serveCommand}\` to make this machine reachable.`,
720+
yield* reportServiceOnboarding(
721+
yield* recoverServiceOnboardingOffer(offerServiceDuringOnboarding),
707722
);
708723
}),
709724
),

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

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { assert, expect, it } from "@effect/vitest";
33
import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";
44
import * as NetService from "@t3tools/shared/Net";
55
import * as ConfigProvider from "effect/ConfigProvider";
6+
import * as Console from "effect/Console";
67
import * as Effect from "effect/Effect";
78
import * as FileSystem from "effect/FileSystem";
89
import * as Layer from "effect/Layer";
@@ -13,6 +14,7 @@ import { afterEach, vi } from "vite-plus/test";
1314
import packageJson from "../../package.json" with { type: "json" };
1415
import * as BootService from "../cloud/bootService.ts";
1516
import {
17+
formatServiceChange,
1618
formatServiceStatus,
1719
offerServiceDuringOnboarding,
1820
reconcileService,
@@ -42,6 +44,24 @@ it("reports the installed service version and host paths", () => {
4244
);
4345
});
4446

47+
it("does not claim a handed-off install has finished", () => {
48+
const plan = {
49+
program: ["/home/me/.t3/runtime/versions/0.0.30/t3", "__service-launcher"],
50+
baseDir: "/home/me/.t3",
51+
logPath: status.logPath,
52+
unitPath: status.unitPath,
53+
};
54+
expect(formatServiceChange({ changed: true, previouslyInstalled: true, plan }, "0.0.30")).toBe(
55+
`Updated T3 Code service with [email protected].\nLogs: ${status.logPath}`,
56+
);
57+
const handedOff = formatServiceChange(
58+
{ changed: true, previouslyInstalled: true, plan: { ...plan, handedOff: true } },
59+
"0.0.30",
60+
);
61+
expect(handedOff).not.toContain("Updated");
62+
expect(handedOff).toContain(`Result: ${status.logPath}`);
63+
});
64+
4565
it("gives a direct repair command for a stale service", () => {
4666
assert.include(
4767
formatServiceStatus({ ...status, current: false }, "0.0.29"),
@@ -97,14 +117,17 @@ it("reports a newer installed service and tells the CLI to catch up to it", () =
97117

98118
const newerServiceStatus = { ...status, current: false, installedVersion: "999.0.0" };
99119

100-
function makeTestService(serviceStatus: BootService.BootServiceStatus) {
120+
function makeTestService(
121+
serviceStatus: BootService.BootServiceStatus,
122+
restartOutcome: "restarted" | "queued" = "restarted",
123+
) {
101124
const installOptions: Array<Parameters<BootService.BootService["Service"]["install"]>[0]> = [];
102125
const restarts: Array<true> = [];
103126
const service = BootService.BootService.of({
104127
status: Effect.succeed(serviceStatus),
105128
restart: Effect.sync(() => {
106129
restarts.push(true);
107-
return serviceStatus.installed;
130+
return serviceStatus.installed ? restartOutcome : "skipped";
108131
}),
109132
install: (options) =>
110133
Effect.sync(() => {
@@ -145,6 +168,39 @@ it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands
145168
}),
146169
);
147170

171+
it.effect("restart from inside the service says the restart is only queued", () =>
172+
Effect.gen(function* () {
173+
const fs = yield* FileSystem.FileSystem;
174+
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" });
175+
const { service } = makeTestService({ ...status, installedVersion: "0.0.30" }, "queued");
176+
vi.spyOn(BootService, "layer").mockReturnValue(
177+
Layer.succeed(BootService.BootService, service),
178+
);
179+
const lines: Array<string> = [];
180+
const testConsole = {
181+
...globalThis.console,
182+
log: (...args: ReadonlyArray<unknown>) => {
183+
lines.push(args.map(String).join(" "));
184+
},
185+
} satisfies Console.Console;
186+
187+
yield* Command.runWith(serviceCommand, { version: packageJson.version })([
188+
"restart",
189+
"--base-dir",
190+
baseDir,
191+
]).pipe(
192+
Effect.provideService(HostProcessEnvironment, {}),
193+
Effect.provideService(Console.Console, testConsole),
194+
Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))),
195+
);
196+
197+
const output = lines.join("\n");
198+
expect(output).not.toContain("Restarted");
199+
expect(output).toContain("Queued a restart of the T3 Code service on [email protected]");
200+
expect(output).toContain(`Logs: ${status.logPath}`);
201+
}),
202+
);
203+
148204
it.effect.each(["install", "update"] as const)(
149205
"%s refuses a downgrade before changing the service",
150206
(command) =>
@@ -246,7 +302,7 @@ it.effect("leaves a newer service unchanged during onboarding without prompting"
246302
Effect.provide(NodeServices.layer),
247303
);
248304

249-
expect(ready).toBe(false);
305+
expect(ready).toBe("skipped");
250306
expect(installOptions).toEqual([]);
251307
}),
252308
);
@@ -262,7 +318,7 @@ it.effect("keeps onboarding successful when a newer version appears before insta
262318
),
263319
);
264320

265-
expect(ready).toBe(false);
321+
expect(ready).toBe("skipped");
266322
}),
267323
);
268324

@@ -271,6 +327,6 @@ it.effect("keeps the manual-server fallback when background prerequisites fail",
271327
const ready = yield* recoverServiceOnboardingOffer(
272328
Effect.fail(new BootService.BootServicePrerequisiteError({ problem: "linger-disabled" })),
273329
);
274-
expect(ready).toBe(false);
330+
expect(ready).toBe("skipped");
275331
}),
276332
);

0 commit comments

Comments
 (0)