Skip to content

Commit 8e7ef43

Browse files
t3dotggclaude
andcommitted
fix(server): name the launcher's protocol to the staged preflight
Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 91ebfca commit 8e7ef43

8 files changed

Lines changed: 53 additions & 24 deletions

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as ServerSelfUpdate from "./selfUpdate.ts";
2626
interface HarnessOptions {
2727
readonly mode?: "web" | "desktop";
2828
readonly managed?: boolean;
29+
readonly launcherProtocol?: 3 | typeof SERVICE_LAUNCHER_PROTOCOL;
2930
readonly preflight?: "ready" | "blocked";
3031
readonly preflightDuration?: Duration.Input;
3132
readonly preflightStarted?: Deferred.Deferred<void>;
@@ -65,6 +66,7 @@ const makeHarness = Effect.fn("test.make_self_update_harness")(function* (
6566
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-self-update-test-" });
6667
const order: string[] = [];
6768
const urls: string[] = [];
69+
const preflightProtocols: Array<string | undefined> = [];
6870
const runner = ProcessRunner.ProcessRunner.of({
6971
run: (input) =>
7072
Effect.gen(function* () {
@@ -113,14 +115,13 @@ const makeHarness = Effect.fn("test.make_self_update_harness")(function* (
113115
}
114116
}
115117
order.push("preflight");
118+
// Like a release, report back the launcher protocol the server named.
119+
const named = input.args[input.args.indexOf("--launcher-protocol") + 1];
120+
preflightProtocols.push(named);
116121
const result =
117122
options.preflight === "blocked"
118123
? { status: "blocked", version: "1.1.0", reason: "local update required" }
119-
: {
120-
status: "ready",
121-
version: "1.1.0",
122-
launcherProtocol: SERVICE_LAUNCHER_PROTOCOL,
123-
};
124+
: { status: "ready", version: "1.1.0", launcherProtocol: Number(named) };
124125
return {
125126
stdout: JSON.stringify(result),
126127
stderr: "",
@@ -133,8 +134,10 @@ const makeHarness = Effect.fn("test.make_self_update_harness")(function* (
133134
};
134135
}),
135136
});
137+
const managed = options.managed ?? true;
136138
const launcher = ServiceLauncherClient.ServiceLauncherClient.of({
137-
managed: options.managed ?? true,
139+
managed,
140+
protocol: managed ? (options.launcherProtocol ?? SERVICE_LAUNCHER_PROTOCOL) : undefined,
138141
requestUpdate:
139142
options.requestUpdate ??
140143
(() =>
@@ -170,7 +173,7 @@ const makeHarness = Effect.fn("test.make_self_update_harness")(function* (
170173
),
171174
),
172175
);
173-
return { selfUpdate, order, urls, baseDir };
176+
return { selfUpdate, order, urls, baseDir, preflightProtocols };
174177
});
175178

176179
it.layer(NodeServices.layer)("server self update", (it) => {
@@ -209,6 +212,15 @@ it.layer(NodeServices.layer)("server self update", (it) => {
209212
}),
210213
);
211214

215+
// regression: #34502 a remote update never replaces the launcher, so it can be older than the server
216+
it.effect("names its launcher's protocol to the staged preflight, not its own", () =>
217+
Effect.gen(function* () {
218+
const { selfUpdate, preflightProtocols } = yield* makeHarness({ launcherProtocol: 3 });
219+
expect((yield* selfUpdate.update({ targetVersion: "1.1.0" })).updateId).toBe("launcher-id");
220+
expect(preflightProtocols).toEqual(["3"]);
221+
}),
222+
);
223+
212224
// regression: #5181 make remote updates rollback-safe
213225
it.effect("rejects invalid versions and desktop-managed servers before staging", () =>
214226
Effect.gen(function* () {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
import { bootServiceProgramOf, bootServiceUnitPath } from "./bootService.ts";
4646
import { decodeServicePreflightResult } from "./servicePreflight.ts";
4747
import * as ServiceLauncherClient from "./serviceLauncherClient.ts";
48-
import { isExactServiceVersion, SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
48+
import { isExactServiceVersion } from "./serviceProtocol.ts";
4949

5050
// A staged runtime's cold start can take over a minute on a loaded, swapping host.
5151
const PREFLIGHT_TIMEOUT = Duration.minutes(3);
@@ -199,7 +199,9 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
199199
"This server is managed by the T3 Code desktop app on its machine; update the desktop app to update it.",
200200
);
201201
}
202-
if (capability === null) {
202+
// The target must accept the launcher that will run it, which can be older than this server.
203+
const launcherProtocol = launcher.protocol;
204+
if (capability === null || launcherProtocol === undefined) {
203205
return yield* failWith(
204206
"Remote updates require the T3 Code background service. Run `t3 service install` on the server machine.",
205207
);
@@ -238,7 +240,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
238240
"--database-path",
239241
serverConfig.dbPath,
240242
"--launcher-protocol",
241-
String(SERVICE_LAUNCHER_PROTOCOL),
243+
String(launcherProtocol),
242244
],
243245
timeout: PREFLIGHT_TIMEOUT,
244246
})
@@ -278,7 +280,7 @@ export const make = Effect.fn("cloud.server_self_update.make")(function* () {
278280
}),
279281
);
280282
}
281-
const preflight = decodeServicePreflightResult(parsed);
283+
const preflight = decodeServicePreflightResult(parsed, launcherProtocol);
282284
if (preflight === undefined || preflight.version !== targetVersion) {
283285
return Effect.fail(
284286
new PinnedRuntimeInstallError({

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
SERVICE_LAUNCHER_CONTEXT_ENV,
1313
type ServiceLauncherChildMessage,
1414
type ServiceLauncherParentMessage,
15+
type SupportedLauncherProtocol,
1516
} from "./serviceProtocol.ts";
1617

1718
export class ServiceLauncherClientError extends Schema.TaggedError<ServiceLauncherClientError>()(
@@ -97,6 +98,9 @@ export class ServiceLauncherClient extends Context.Service<
9798
ServiceLauncherClient,
9899
{
99100
readonly managed: boolean;
101+
/** The supervising launcher's protocol. A remote update never replaces the launcher,
102+
so it can be older than this runtime's own. */
103+
readonly protocol: SupportedLauncherProtocol | undefined;
100104
readonly requestUpdate: (input: {
101105
readonly targetVersion: string;
102106
readonly dbPath: string;
@@ -240,6 +244,7 @@ export const make = Effect.fn("cloud.service_launcher_client.make")(function* (o
240244

241245
return ServiceLauncherClient.of({
242246
managed,
247+
protocol: context?.protocol,
243248
requestUpdate,
244249
prepareTrial,
245250
});

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import packageJson from "../../package.json" with { type: "json" };
22
import { BUILD_MIGRATION_HEAD, recordedMigrations } from "../persistence/databaseMigrationHead.ts";
3-
import { isSupportedLauncherProtocol, SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
3+
import {
4+
isSupportedLauncherProtocol,
5+
SERVICE_LAUNCHER_PROTOCOL,
6+
type SupportedLauncherProtocol,
7+
} from "./serviceProtocol.ts";
48

59
export type ServicePreflightResult =
610
| {
711
readonly status: "ready";
812
readonly version: string;
9-
/** The launcher protocol the caller named: a released server accepts only its own. */
10-
readonly launcherProtocol: 3 | typeof SERVICE_LAUNCHER_PROTOCOL;
13+
/** The launcher protocol the server named; it accepts no other in reply. */
14+
readonly launcherProtocol: SupportedLauncherProtocol;
1115
}
1216
| {
1317
readonly status: "blocked";
@@ -65,21 +69,20 @@ function newerRecordedMigrations(databasePath: string): ReadonlyArray<string> |
6569
: recorded.filter(({ id }) => id > BUILD_MIGRATION_HEAD).map(({ id, name }) => `${id}_${name}`);
6670
}
6771

68-
export function decodeServicePreflightResult(value: unknown): ServicePreflightResult | undefined {
72+
export function decodeServicePreflightResult(
73+
value: unknown,
74+
launcherProtocol: SupportedLauncherProtocol,
75+
): ServicePreflightResult | undefined {
6976
if (typeof value !== "object" || value === null) {
7077
return undefined;
7178
}
7279
const record = value as Record<string, unknown>;
7380
if (
7481
record.status === "ready" &&
75-
record.launcherProtocol === SERVICE_LAUNCHER_PROTOCOL &&
82+
record.launcherProtocol === launcherProtocol &&
7683
typeof record.version === "string"
7784
) {
78-
return {
79-
status: "ready",
80-
version: record.version,
81-
launcherProtocol: SERVICE_LAUNCHER_PROTOCOL,
82-
};
85+
return { status: "ready", version: record.version, launcherProtocol };
8386
}
8487
if (
8588
record.status === "blocked" &&

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import type { ServerSelfUpdateOutcome } from "@t3tools/contracts";
66
export const SERVICE_LAUNCHER_PROTOCOL = 4 as const;
77
/** Launchers this runtime runs under. Protocol 3 has the same IPC and durable format, and
88
refusing it stops every remote update until someone opens a shell on the host. */
9+
export type SupportedLauncherProtocol = 3 | typeof SERVICE_LAUNCHER_PROTOCOL;
910
export const isSupportedLauncherProtocol = (
1011
protocol: unknown,
11-
): protocol is 3 | typeof SERVICE_LAUNCHER_PROTOCOL =>
12+
): protocol is SupportedLauncherProtocol =>
1213
protocol === 3 || protocol === SERVICE_LAUNCHER_PROTOCOL;
1314
export const SERVICE_LAUNCHER_CONTEXT_ENV = "T3_SERVICE_LAUNCHER_CONTEXT";
1415
export const SERVICE_ENV_FILE = "service.env";
@@ -64,7 +65,7 @@ export interface ServiceState {
6465
}
6566

6667
export interface ServiceLauncherContext {
67-
readonly protocol: typeof SERVICE_LAUNCHER_PROTOCOL;
68+
readonly protocol: SupportedLauncherProtocol;
6869
readonly childVersion: string;
6970
readonly update?: ServiceUpdateRecord;
7071
}
@@ -276,7 +277,7 @@ export function decodeServiceLauncherContext(value: string): ServiceLauncherCont
276277
return undefined;
277278
}
278279
return {
279-
protocol: SERVICE_LAUNCHER_PROTOCOL,
280+
protocol: parsed.protocol,
280281
childVersion: parsed.childVersion,
281282
...(update === undefined ? {} : { update }),
282283
};

‎apps/server/src/serverRuntimeStartup.autoPull.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as NetAddress from "effect/net/NetAddress";
1313

1414
import * as EnvironmentAuth from "./auth/EnvironmentAuth.ts";
1515
import * as ServiceLauncherClient from "./cloud/serviceLauncherClient.ts";
16+
import { SERVICE_LAUNCHER_PROTOCOL } from "./cloud/serviceProtocol.ts";
1617
import * as ServerConfig from "./config.ts";
1718
import * as ServerEnvironment from "./environment/ServerEnvironment.ts";
1819
import * as Keybindings from "./keybindings.ts";
@@ -146,6 +147,7 @@ it.effect("parks automatic pull until activation without delaying command readin
146147
Layer.mock(ThreadLaunch.ThreadLaunchService)({}),
147148
Layer.mock(ServiceLauncherClient.ServiceLauncherClient)({
148149
managed: true,
150+
protocol: SERVICE_LAUNCHER_PROTOCOL,
149151
prepareTrial: Deferred.succeed(prepared, undefined).pipe(
150152
Effect.andThen(Deferred.await(commitTrial)),
151153
Effect.as(undefined),

‎apps/server/src/serverRuntimeStartup.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ it.effect.each([
201201
}),
202202
Layer.mock(ServiceLauncherClient.ServiceLauncherClient)({
203203
managed: false,
204+
protocol: undefined,
204205
prepareTrial: Effect.undefined,
205206
}),
206207
Layer.mock(GitVcsDriver.GitVcsDriver)({}),

‎docs/internals/server-updates.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ takes the state lease before the unit lease; the launcher never holds both, so a
1313
unit failure cannot block a durable commit. Manager stop/start commands run outside
1414
both leases, since stopping a launcher that waits on a lease would deadlock. A
1515
running launcher refuses further updates once a local command changes its selection.
16+
A protocol-3 launcher takes no lease, so a local command can still race its updates.
1617

1718
Exact-version installs keep restarts independent of npm cache eviction or a moving
1819
release tag. Installation and preflight happen in staging before publishing an
@@ -23,6 +24,8 @@ update with a restart. Every installed launcher is therefore one protocol behind
2324
after a bump, and a target that refuses the previous protocol stops remote updates
2425
for every existing service. Refuse it only when the child cannot work under it.
2526
Protocol 4 changed the launcher alone, so its runtime still runs under protocol 3.
27+
The server names its launcher's protocol to preflight, not its own, since the two differ
28+
after the first such update.
2629

2730
After a commit or rollback the launcher points the unit at the selected runtime and
2831
reloads the manager, so the next service start runs that version's launcher; the

0 commit comments

Comments
 (0)