TypeScript
fix(server): t3 run from a T3 terminal no longer trips on the service's launcher context (#14263)
Shawn1b5c2d1d300a
diff --git a/apps/server/src/cloud/serviceLauncherClient.test.ts b/apps/server/src/cloud/serviceLauncherClient.test.tsindex dee00b0b203..f60b8bb9cf8 100644--- a/apps/server/src/cloud/serviceLauncherClient.test.ts+++ b/apps/server/src/cloud/serviceLauncherClient.test.ts@@ -12,12 +12,13 @@ import {import * as ServiceLauncherClient from "./serviceLauncherClient.ts";class FakeLauncherProcess {- readonly connected = true;+ readonly connected: boolean;readonly env: Record<string, string | undefined>;readonly sent: ServiceLauncherChildMessage[] = [];readonly #listeners = new Map<string, Set<(...args: ReadonlyArray<unknown>) => void>>();- constructor(context: unknown) {+ constructor(context: unknown, connected = true) {+ this.connected = connected;this.env = { [SERVICE_LAUNCHER_CONTEXT_ENV]: JSON.stringify(context) };}@@ -136,3 +137,18 @@ it.effect("rejects contradictory trial context instead of leaving activation cloexpect(error.message).toBe("The service launcher supplied invalid startup context.");}),);++it.effect("ignores launcher context inherited without an IPC channel", () =>+ Effect.gen(function* () {+ const host = new FakeLauncherProcess(+ { protocol: SERVICE_LAUNCHER_PROTOCOL, childVersion: "1.0.0" },+ false,+ );+ const client = yield* makeClient(host, "1.1.0");+ const error = yield* client+ .requestUpdate({ targetVersion: "1.2.0", dbPath: "/tmp/state.sqlite" })+ .pipe(Effect.flip);+ expect(error).toMatchObject({ _tag: "ServiceLauncherClientError", operation: "unmanaged" });+ expect(host.sent).toEqual([]);+ }),+);diff --git a/apps/server/src/cloud/serviceLauncherClient.ts b/apps/server/src/cloud/serviceLauncherClient.tsindex b58d133494c..86d3f2022ae 100644--- a/apps/server/src/cloud/serviceLauncherClient.ts+++ b/apps/server/src/cloud/serviceLauncherClient.ts@@ -20,7 +20,6 @@ export class ServiceLauncherClientError extends Schema.TaggedError<ServiceLaunchoperation: Schema.Literals(["decode-context","version-mismatch",- "ipc-unavailable","unmanaged","send","disconnect",@@ -35,8 +34,6 @@ export class ServiceLauncherClientError extends Schema.TaggedError<ServiceLaunchreturn "The service launcher supplied invalid startup context.";case "version-mismatch":return "The service launcher started a different t3 version.";- case "ipc-unavailable":- return "The service launcher IPC channel is unavailable.";case "unmanaged":return "This server is not managed by the launcher.";case "send":@@ -116,7 +113,10 @@ const resolveStartup = Effect.fn("cloud.service_launcher_client.resolve_startup"const host = yield* ServiceLauncherHostProcess;const environment = yield* HostProcess.Environment;const currentVersion = options?.currentVersion ?? packageJson.version;- const rawContext = environment[SERVICE_LAUNCHER_CONTEXT_ENV];+ // The launcher always spawns its child with an IPC channel, and Node does+ // not pass that channel on. Without one, the context was inherited from an+ // ancestor (e.g. `t3 serve` run in a T3 terminal), so it describes someone else.+ const rawContext = host.connected ? environment[SERVICE_LAUNCHER_CONTEXT_ENV] : undefined;const context = rawContext === undefined ? undefined : decodeServiceLauncherContext(rawContext);if (rawContext !== undefined && context === undefined) {@@ -126,10 +126,7 @@ const resolveStartup = Effect.fn("cloud.service_launcher_client.resolve_startup"return yield* new ServiceLauncherClientError({ operation: "version-mismatch" });}- const managed = context !== undefined && host.connected;- if (context !== undefined && !managed) {- return yield* new ServiceLauncherClientError({ operation: "ipc-unavailable" });- }+ const managed = context !== undefined;return { host, context, managed };},