From 36c432e61b00b247ba514b21acb0d54d6c51c0cd Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 20:48:03 +0000 Subject: [PATCH 1/4] test(server): t3 pair treats a server on another machine sharing $HOME as gone Failing repro for #34054. AI-assisted; verified with the tests and tools listed above. --- apps/server/src/cli/pair.test.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/apps/server/src/cli/pair.test.ts b/apps/server/src/cli/pair.test.ts index e626461914a06..f3f6aee2d1169 100644 --- a/apps/server/src/cli/pair.test.ts +++ b/apps/server/src/cli/pair.test.ts @@ -343,6 +343,32 @@ describe("t3 pair", () => { ).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), ); + // regression: #34054 on a shared $HOME the recorded pid belongs to another machine. + it.effect("names the other machine whose server owns a shared T3 home", () => + Effect.gen(function* () { + const baseDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3 pair host test-")); + const state = yield* makePersistedServerRuntimeState({ + config: { host: "127.0.0.1", devUrl: undefined }, + port: 1, + }); + NodeFS.mkdirSync(NodePath.join(baseDir, "userdata"), { recursive: true }); + NodeFS.writeFileSync( + NodePath.join(baseDir, "userdata", "server-runtime.json"), + JSON.stringify({ ...state, pid: 4_194_305, hostname: "host-a" }), + ); + + const error = yield* provideCliTestLayers( + runCli(["pair", "--base-dir", baseDir]).pipe(Effect.flip), + ); + + const rendered = String( + typeof error === "object" && error !== null && "cause" in error ? error.cause : error, + ); + assert.notInclude(rendered, "No running T3 Code server found."); + assert.include(rendered, "host-a"); + }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), + ); + // regression: #10516 minting opens the target database with this checkout's // migrations, so a worktree must never fall back to the live install. it.effect("does not fall back to the shared home from a linked worktree", () => From 62f855d0daa485b0ca16fb3ae75e54c978846a26 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 21:05:53 +0000 Subject: [PATCH 2/4] fix(server): name the other machine that owns a shared T3 home On a shared $HOME (NFS), server-runtime.json did not say which machine the server runs on, so the local PID check reported a live server elsewhere as gone, and stop/uninstall advice would act on that machine's service. Servers now record their hostname. `t3 pair`, `t3 serve`'s ownership refusal, the service launcher and the boot-service "other T3 home" refusal name the other machine and stop advising stop or uninstall from here. Fixes pingdotgg/t3code#34054 AI-assisted; verified with the tests and tools listed above. --- apps/server/src/cli/pair.ts | 23 +++++++++++++++++++ apps/server/src/cli/service.ts | 3 +++ apps/server/src/cli/up.ts | 3 +++ apps/server/src/cloud/bootService.test.ts | 23 +++++++++++++++++++ apps/server/src/cloud/bootService.ts | 26 +++++++++++++++++++++- apps/server/src/serverOwnership.ts | 21 +++++++++++++++-- apps/server/src/serverRuntimeState.test.ts | 6 +++++ apps/server/src/serverRuntimeState.ts | 13 ++++++++++- apps/server/src/serviceLauncher.test.ts | 8 ++++++- apps/server/src/serviceLauncher.ts | 20 ++++++++++++++++- 10 files changed, 140 insertions(+), 6 deletions(-) diff --git a/apps/server/src/cli/pair.ts b/apps/server/src/cli/pair.ts index 904182ad29c6b..72d28fb1f1dcc 100644 --- a/apps/server/src/cli/pair.ts +++ b/apps/server/src/cli/pair.ts @@ -36,6 +36,7 @@ import { resolveBaseDir } from "../os-jank.ts"; import { type PersistedServerRuntimeState, isProcessAlive, + otherMachineOf, readPersistedServerRuntimeState, } from "../serverRuntimeState.ts"; import { @@ -82,6 +83,18 @@ export class NoRunningServerError extends Schema.TaggedError()( + "ServerOnOtherMachineError", + { hostname: Schema.String, statePath: Schema.String }, +) { + override get message(): string { + return [ + `The T3 Code server recorded in ${this.statePath} runs on ${this.hostname}, which shares this T3 home.`, + `Run \`t3 pair\` on ${this.hostname}, or start a server on this machine with a separate T3 home (--base-dir).`, + ].join("\n"); + } +} + export class IncompatibleServerProtocolError extends Schema.TaggedError()( "IncompatibleServerProtocolError", { serverVersion: Schema.String, serverProtocolVersion: Schema.Number }, @@ -267,6 +280,8 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* ( } const checkedStatePaths: Array = []; + const localHostname = yield* HostProcess.Hostname; + let otherMachine: ServerOnOtherMachineError | undefined; for (const baseDir of new Set(bases)) { for (const variant of ["userdata", "dev"] as const) { const derivedPaths = yield* ServerConfig.deriveServerPaths( @@ -280,6 +295,11 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* ( if (Option.isNone(state)) { continue; } + const hostname = otherMachineOf(state.value, localHostname); + if (hostname !== undefined) { + otherMachine ??= new ServerOnOtherMachineError({ hostname, statePath }); + continue; + } // A dead server's port may be reused; do not mint tokens in its old database for another server. if (!isProcessAlive(state.value.pid)) { continue; @@ -296,6 +316,9 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* ( } satisfies DiscoveredPairTarget; } } + if (otherMachine !== undefined) { + return yield* otherMachine; + } const baseDirArgs = hasExplicitBaseDir ? ["--base-dir", explicitBaseDir] : []; return yield* new NoRunningServerError({ checkedStatePaths, diff --git a/apps/server/src/cli/service.ts b/apps/server/src/cli/service.ts index ce43154116b8e..4a7f4ddb477e1 100644 --- a/apps/server/src/cli/service.ts +++ b/apps/server/src/cli/service.ts @@ -97,6 +97,9 @@ export const reconcileService = Effect.fn("cli.service.reconcile")(function* (op return yield* new BootService.BootServiceOtherBaseDirError({ installedBaseDir: status.otherBaseDir, baseDir, + ...(status.otherBaseDirHostname === undefined + ? {} + : { ownerHostname: status.otherBaseDirHostname }), }); } if (status.installed && status.current) { diff --git a/apps/server/src/cli/up.ts b/apps/server/src/cli/up.ts index 9bd0409f6ef9f..5dbf36233f375 100644 --- a/apps/server/src/cli/up.ts +++ b/apps/server/src/cli/up.ts @@ -218,6 +218,9 @@ const setUp = Effect.fn("cli.up")(function* (flags: { return yield* new BootService.BootServiceOtherBaseDirError({ installedBaseDir: before.service.otherBaseDir, baseDir, + ...(before.service.otherBaseDirHostname === undefined + ? {} + : { ownerHostname: before.service.otherBaseDirHostname }), }); } if (before.connect === null && Option.getOrElse(flags.connect, () => false)) { diff --git a/apps/server/src/cloud/bootService.test.ts b/apps/server/src/cloud/bootService.test.ts index 814d470cad410..9cd111fb7e2f4 100644 --- a/apps/server/src/cloud/bootService.test.ts +++ b/apps/server/src/cloud/bootService.test.ts @@ -698,6 +698,29 @@ it.layer(NodeServices.layer)("boot service install", (it) => { expect(yield* fs.readFileString(plan.unitPath)).toBe(unit); expect(commands).toEqual([]); expect((yield* other.status).otherBaseDir).toBe(baseDir); + expect(refused.message).toContain("t3 service uninstall"); + + // A shared $HOME: the served home's server recorded another machine. + yield* fs.makeDirectory(path.join(baseDir, "userdata"), { recursive: true }); + yield* fs.writeFileString( + path.join(baseDir, "userdata", "server-runtime.json"), + JSON.stringify({ + version: 1, + pid: 1, + hostname: "host-a", + port: 3773, + origin: "http://127.0.0.1:3773", + startedAt: "2026-01-01T00:00:00.000Z", + }), + ); + const shared = yield* other.install().pipe(Effect.flip); + expect(shared).toMatchObject({ + _tag: "BootServiceOtherBaseDirError", + ownerHostname: "host-a", + }); + expect(shared.message).toContain("runs on host-a"); + expect(shared.message).not.toContain("t3 service uninstall"); + expect((yield* other.status).otherBaseDirHostname).toBe("host-a"); }), ); diff --git a/apps/server/src/cloud/bootService.ts b/apps/server/src/cloud/bootService.ts index 88d477974ae0a..837360529b8b8 100644 --- a/apps/server/src/cloud/bootService.ts +++ b/apps/server/src/cloud/bootService.ts @@ -16,6 +16,7 @@ import * as Schema from "effect/Schema"; import { CLI_RELEASE_BASE_URL_ENV } from "@t3tools/shared/cliRelease"; import * as ProcessRunner from "../processRunner.ts"; +import { otherMachineOf, readPersistedServerRuntimeState } from "../serverRuntimeState.ts"; import { ensurePinnedRuntimeInstalled, launcherOwnsVersionsDir, @@ -523,9 +524,14 @@ export class BootServiceOtherBaseDirError extends Schema.TaggedError; readonly unitPath: string; readonly logPath: string; @@ -655,6 +663,14 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { const logPath = path.join(input.logsDir, BOOT_SERVICE_LOG_FILE); const statePath = path.join(input.baseDir, "runtime", SERVICE_STATE_FILE); const restartPendingPath = path.join(input.baseDir, "runtime", SERVICE_RESTART_PENDING_FILE); + const localHostname = yield* HostProcess.Hostname; + const hostnameOfOtherMachineServing = (baseDir: string) => + readPersistedServerRuntimeState(path.join(baseDir, "userdata", "server-runtime.json")).pipe( + Effect.map((state) => + Option.isSome(state) ? otherMachineOf(state.value, localHostname) : undefined, + ), + Effect.provideService(FileSystem.FileSystem, fs), + ); const otherBaseDirOf = (unit: string) => { const installedBaseDir = bootServiceBaseDirOf(unit); return installedBaseDir !== undefined && @@ -811,7 +827,12 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { ? otherBaseDirOf(existingUnit.value) : undefined; if (installedBaseDir !== undefined) { - return yield* new BootServiceOtherBaseDirError({ installedBaseDir, baseDir: input.baseDir }); + const ownerHostname = yield* hostnameOfOtherMachineServing(installedBaseDir); + return yield* new BootServiceOtherBaseDirError({ + installedBaseDir, + baseDir: input.baseDir, + ...(ownerHostname === undefined ? {} : { ownerHostname }), + }); } yield* fs .makeDirectory(input.logsDir, { recursive: true }) @@ -1081,6 +1102,8 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { : undefined; const installedBaseDir = bootServiceBaseDirOf(unit); const otherBaseDir = otherBaseDirOf(unit); + const otherBaseDirHostname = + otherBaseDir === undefined ? undefined : yield* hostnameOfOtherMachineServing(otherBaseDir); const normalizeUnit = (contents: string) => detectedManager.kind === "launchd" ? contents.replace(/(PATH<\/key>\n\s*)[^<]*(<\/string>)/, "$1$2") @@ -1094,6 +1117,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: { ...(installedVersion === undefined ? {} : { installedVersion }), ...(installedBaseDir === undefined ? {} : { installedBaseDir }), ...(otherBaseDir === undefined ? {} : { otherBaseDir }), + ...(otherBaseDirHostname === undefined ? {} : { otherBaseDirHostname }), problems, current: problems.length === 0 && diff --git a/apps/server/src/serverOwnership.ts b/apps/server/src/serverOwnership.ts index 565f8111849e0..0139771acffec 100644 --- a/apps/server/src/serverOwnership.ts +++ b/apps/server/src/serverOwnership.ts @@ -31,6 +31,7 @@ import * as ProcessRunner from "./processRunner.ts"; import { clearPersistedServerRuntimeState, isProcessAlive, + otherMachineOf, persistServerRuntimeState, readPersistedServerRuntimeState, type PersistedServerRuntimeState, @@ -38,13 +39,17 @@ import { export class ServerAlreadyRunningError extends Schema.TaggedError()( "ServerAlreadyRunningError", - { stateDir: Schema.String }, + { stateDir: Schema.String, hostname: Schema.optional(Schema.String) }, ) { // Distinct exit codes let supervisors stop restarting an already-owned server. override readonly [Runtime.errorExitCode] = SERVER_EXIT_CODE_STATE_DIR_OWNED; override get message(): string { - return `A T3 Code server already owns ${this.stateDir}. Stop that server, or use a separate T3 home and pair with it, then retry. No server was stopped. server-owner.sqlite is the ownership lock; do not delete it while a server is running.`; + const owner = + this.hostname === undefined + ? `A T3 Code server already owns ${this.stateDir}. Stop that server` + : `A T3 Code server on ${this.hostname} already owns ${this.stateDir}, which this machine shares. Stop it on ${this.hostname}`; + return `${owner}, or use a separate T3 home and pair with it, then retry. No server was stopped. server-owner.sqlite is the ownership lock; do not delete it while a server is running.`; } } @@ -227,6 +232,18 @@ export const acquireServerOwnership = Effect.fn("acquireServerOwnership")(functi Effect.ignore({ log: true }), Effect.ensuring(Effect.sync(() => db.close())), ), + ).pipe( + Effect.catchTags({ + ServerAlreadyRunningError: Effect.fn("acquireServerOwnership.nameOwner")(function* (error) { + const owner = yield* readPersistedServerRuntimeState(statePath); + const hostname = Option.isSome(owner) + ? otherMachineOf(owner.value, yield* HostProcess.Hostname) + : undefined; + return yield* hostname === undefined + ? error + : new ServerAlreadyRunningError({ stateDir, hostname }); + }), + }), ); const previous = yield* readPersistedServerRuntimeState(statePath).pipe( diff --git a/apps/server/src/serverRuntimeState.test.ts b/apps/server/src/serverRuntimeState.test.ts index 5a14cf139577e..cc366bfd0dbd7 100644 --- a/apps/server/src/serverRuntimeState.test.ts +++ b/apps/server/src/serverRuntimeState.test.ts @@ -188,7 +188,13 @@ describe("server ownership", () => { ); assert.equal(refused._tag, "ServerAlreadyRunningError"); assert.include(refused.message, "do not delete it while a server is running"); + assert.notInclude(refused.message, "which this machine shares"); assert.equal(yield* fs.readFileString(statePath), before); + // A shared $HOME: the owner recorded another machine. + yield* owner.publish({ ...state, hostname: "host-a" }); + const remote = yield* ServerOwnership.acquireServerOwnership(statePath).pipe(Effect.flip); + assert.include(remote.message, "server on host-a already owns"); + assert.include(remote.message, "Stop it on host-a"); }), ); assert.isFalse(yield* fs.exists(statePath)); diff --git a/apps/server/src/serverRuntimeState.ts b/apps/server/src/serverRuntimeState.ts index 49d9a48f991bc..c80352292328f 100644 --- a/apps/server/src/serverRuntimeState.ts +++ b/apps/server/src/serverRuntimeState.ts @@ -1,3 +1,4 @@ +import * as HostProcess from "@t3tools/shared/HostProcess"; import * as DateTime from "effect/DateTime"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; @@ -12,6 +13,8 @@ import { formatHostForUrl, isWildcardHost } from "./startupAccess.ts"; export const PersistedServerRuntimeState = Schema.Struct({ version: Schema.Literal(1), pid: Schema.Int, + // A shared $HOME (NFS) can hold a server from another machine, where `pid` means nothing here. + hostname: Schema.optional(Schema.String), ownerId: Schema.optional(Schema.String), host: Schema.optional(Schema.String), privateNetworkOnly: Schema.optional(Schema.Boolean), @@ -70,9 +73,10 @@ export const makePersistedServerRuntimeState = (input: { readonly serviceManaged?: boolean; readonly managedPolicy?: PersistedServerRuntimeState["managedPolicy"]; }): Effect.Effect => - Effect.map(DateTime.now, (now) => ({ + Effect.map(Effect.all([DateTime.now, HostProcess.Hostname]), ([now, hostname]) => ({ version: 1, pid: process.pid, + hostname, ...(input.config.host ? { host: input.config.host } : {}), ...(input.config.privateNetworkOnly ? { privateNetworkOnly: true } : {}), port: input.port, @@ -128,6 +132,13 @@ export const clearPersistedServerRuntimeState = Effect.fn( ); }); +/** The machine a recorded server runs on, when that is not this one. */ +export const otherMachineOf = ( + state: Pick, + localHostname: string, +): string | undefined => + state.hostname !== undefined && state.hostname !== localHostname ? state.hostname : undefined; + /** Signal 0 only probes existence; EPERM still means alive. */ export const isProcessAlive = (pid: number): boolean => { if (!Number.isInteger(pid) || pid <= 0) return false; diff --git a/apps/server/src/serviceLauncher.test.ts b/apps/server/src/serviceLauncher.test.ts index 274b86573ed3c..3d427f59d74bc 100644 --- a/apps/server/src/serviceLauncher.test.ts +++ b/apps/server/src/serviceLauncher.test.ts @@ -234,11 +234,17 @@ it.layer(NodeServices.layer)("service state persistence", (it) => { activeVersion: "1.0.0", }), ); + // A shared $HOME: the owner recorded another machine. + yield* fs.makeDirectory(path.join(root, "userdata"), { recursive: true }); + yield* fs.writeFileString( + path.join(root, "userdata", "server-runtime.json"), + JSON.stringify({ hostname: "host-a" }), + ); const refused = Promise.withResolvers(); yield* Effect.acquireRelease( Effect.sync(() => vi.spyOn(process.stderr, "write").mockImplementation((chunk) => { - if (String(chunk).includes("owns this T3 home")) refused.resolve(); + if (String(chunk).includes("server on host-a owns this T3 home")) refused.resolve(); return true; }), ), diff --git a/apps/server/src/serviceLauncher.ts b/apps/server/src/serviceLauncher.ts index ee05c63297843..c0896077129a3 100644 --- a/apps/server/src/serviceLauncher.ts +++ b/apps/server/src/serviceLauncher.ts @@ -5,6 +5,7 @@ import * as NodeChildProcess from "node:child_process"; import * as NodeCrypto from "node:crypto"; import * as NodeFS from "node:fs"; import * as NodeFSP from "node:fs/promises"; +import * as NodeOS from "node:os"; import * as NodePath from "node:path"; import * as NodeUtil from "node:util"; @@ -48,6 +49,20 @@ interface ManagedChild { readonly process: NodeChildProcess.ChildProcess; } +// Mirrors serverRuntimeState's otherMachineOf: on a shared $HOME the owner may run elsewhere. +const ownerOnOtherMachine = async (baseDir: string): Promise => { + try { + const record = JSON.parse( + await NodeFSP.readFile(NodePath.join(baseDir, "userdata", "server-runtime.json"), "utf8"), + ) as { readonly hostname?: unknown }; + return typeof record.hostname === "string" && record.hostname !== NodeOS.hostname() + ? record.hostname + : undefined; + } catch { + return undefined; + } +}; + const runtimePaths = (baseDir: string, version: string) => { const versionDir = NodePath.join(baseDir, "runtime", "versions", version); // oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone launcher has no Effect runtime. @@ -800,8 +815,11 @@ export class Launcher { } // Signal listeners alone do not keep Node alive. this.#timer = setInterval(() => {}, 2_147_483_647); + const hostname = await ownerOnOtherMachine(this.#baseDir); process.stderr.write( - "[service-launcher] Another T3 Code server owns this T3 home. Stop it, then restart this service.\n", + hostname === undefined + ? "[service-launcher] Another T3 Code server owns this T3 home. Stop it, then restart this service.\n" + : `[service-launcher] A T3 Code server on ${hostname} owns this T3 home, which this machine shares. Stop it on ${hostname}, then restart this service.\n`, ); } From def4ac6e6e9b099679d47caf9af1c44cc170ca54 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 21:25:29 +0000 Subject: [PATCH 3/4] fix(server): a live local server stays local when its hostname changes The hostname is recorded once at server start, and on macOS it follows the network, so a running local server could be reported as running on another machine. A recorded server now counts as another machine's only when its PID is not alive here, so pairing keeps main's path for a live local PID. AI-assisted; verified with the tests and tools listed above. --- apps/server/src/cloud/bootService.test.ts | 2 +- apps/server/src/serverRuntimeState.test.ts | 8 +++++++- apps/server/src/serverRuntimeState.ts | 11 ++++++++--- apps/server/src/serviceLauncher.ts | 17 +++++++++++++---- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/apps/server/src/cloud/bootService.test.ts b/apps/server/src/cloud/bootService.test.ts index 9cd111fb7e2f4..ed8744a5295d3 100644 --- a/apps/server/src/cloud/bootService.test.ts +++ b/apps/server/src/cloud/bootService.test.ts @@ -706,7 +706,7 @@ it.layer(NodeServices.layer)("boot service install", (it) => { path.join(baseDir, "userdata", "server-runtime.json"), JSON.stringify({ version: 1, - pid: 1, + pid: 4_194_305, hostname: "host-a", port: 3773, origin: "http://127.0.0.1:3773", diff --git a/apps/server/src/serverRuntimeState.test.ts b/apps/server/src/serverRuntimeState.test.ts index cc366bfd0dbd7..11ce8f4267f7e 100644 --- a/apps/server/src/serverRuntimeState.test.ts +++ b/apps/server/src/serverRuntimeState.test.ts @@ -190,8 +190,14 @@ describe("server ownership", () => { assert.include(refused.message, "do not delete it while a server is running"); assert.notInclude(refused.message, "which this machine shares"); assert.equal(yield* fs.readFileString(statePath), before); - // A shared $HOME: the owner recorded another machine. + // A renamed host keeps a live local server local. yield* owner.publish({ ...state, hostname: "host-a" }); + const renamed = yield* ServerOwnership.acquireServerOwnership(statePath).pipe( + Effect.flip, + ); + assert.notInclude(renamed.message, "host-a"); + // A shared $HOME: the owner recorded another machine, whose PID is not alive here. + yield* owner.publish({ ...state, pid: 4_194_305, hostname: "host-a" }); const remote = yield* ServerOwnership.acquireServerOwnership(statePath).pipe(Effect.flip); assert.include(remote.message, "server on host-a already owns"); assert.include(remote.message, "Stop it on host-a"); diff --git a/apps/server/src/serverRuntimeState.ts b/apps/server/src/serverRuntimeState.ts index c80352292328f..e4fa8b30885bf 100644 --- a/apps/server/src/serverRuntimeState.ts +++ b/apps/server/src/serverRuntimeState.ts @@ -132,12 +132,17 @@ export const clearPersistedServerRuntimeState = Effect.fn( ); }); -/** The machine a recorded server runs on, when that is not this one. */ +/** + * The machine a recorded server runs on, when that is not this one. A live local PID wins: + * hostnames can change under a running server (macOS takes them from the network). + */ export const otherMachineOf = ( - state: Pick, + state: Pick, localHostname: string, ): string | undefined => - state.hostname !== undefined && state.hostname !== localHostname ? state.hostname : undefined; + state.hostname !== undefined && state.hostname !== localHostname && !isProcessAlive(state.pid) + ? state.hostname + : undefined; /** Signal 0 only probes existence; EPERM still means alive. */ export const isProcessAlive = (pid: number): boolean => { diff --git a/apps/server/src/serviceLauncher.ts b/apps/server/src/serviceLauncher.ts index c0896077129a3..a55403dfec336 100644 --- a/apps/server/src/serviceLauncher.ts +++ b/apps/server/src/serviceLauncher.ts @@ -54,10 +54,19 @@ const ownerOnOtherMachine = async (baseDir: string): Promise try { const record = JSON.parse( await NodeFSP.readFile(NodePath.join(baseDir, "userdata", "server-runtime.json"), "utf8"), - ) as { readonly hostname?: unknown }; - return typeof record.hostname === "string" && record.hostname !== NodeOS.hostname() - ? record.hostname - : undefined; + ) as { readonly hostname?: unknown; readonly pid?: unknown }; + if (typeof record.hostname !== "string" || record.hostname === NodeOS.hostname()) { + return undefined; + } + if (typeof record.pid === "number" && record.pid > 0) { + try { + process.kill(record.pid, 0); + return undefined; + } catch (error) { + if (error instanceof Error && "code" in error && error.code === "EPERM") return undefined; + } + } + return record.hostname; } catch { return undefined; } From acadf3730aa31e160304bd0890c222eb636ccc65 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 10 Nov 2026 21:41:24 +0000 Subject: [PATCH 4/4] fix(server): a colliding local PID no longer hides the other machine A live local PID alone cannot tell a renamed machine from an unrelated local process that happens to have the other machine's server PID. The hostname check is hostname-only again, and `t3 pair` treats a record as local only when its PID is alive and the recorded origin answers as a T3 server; otherwise a recorded hostname that is not this machine's is reported as the other machine. AI-assisted; verified with the tests and tools listed above. --- apps/server/src/cli/pair.test.ts | 39 +++++++++++++--------- apps/server/src/cli/pair.ts | 19 +++++------ apps/server/src/serverRuntimeState.test.ts | 8 +---- apps/server/src/serverRuntimeState.ts | 11 ++---- apps/server/src/serviceLauncher.ts | 17 +++------- 5 files changed, 40 insertions(+), 54 deletions(-) diff --git a/apps/server/src/cli/pair.test.ts b/apps/server/src/cli/pair.test.ts index f3f6aee2d1169..36b0b4fb1e17d 100644 --- a/apps/server/src/cli/pair.test.ts +++ b/apps/server/src/cli/pair.test.ts @@ -202,10 +202,14 @@ describe("t3 pair", () => { const statePath = NodePath.join(baseDir, "userdata", "server-runtime.json"); yield* persistServerRuntimeState({ path: statePath, - state: yield* makePersistedServerRuntimeState({ - config: { host: "127.0.0.1", devUrl: undefined }, - port, - }), + state: { + ...(yield* makePersistedServerRuntimeState({ + config: { host: "127.0.0.1", devUrl: undefined }, + port, + })), + // The hostname can change under a running server; a live PID that answers is local. + hostname: "renamed-host", + }, }); const output = yield* captureStdout(runCli(["pair", "--base-dir", baseDir])); @@ -352,20 +356,23 @@ describe("t3 pair", () => { port: 1, }); NodeFS.mkdirSync(NodePath.join(baseDir, "userdata"), { recursive: true }); - NodeFS.writeFileSync( - NodePath.join(baseDir, "userdata", "server-runtime.json"), - JSON.stringify({ ...state, pid: 4_194_305, hostname: "host-a" }), - ); + // The recorded PID may be dead here, or belong to an unrelated local process. + for (const pid of [4_194_305, process.pid]) { + NodeFS.writeFileSync( + NodePath.join(baseDir, "userdata", "server-runtime.json"), + JSON.stringify({ ...state, pid, hostname: "host-a" }), + ); - const error = yield* provideCliTestLayers( - runCli(["pair", "--base-dir", baseDir]).pipe(Effect.flip), - ); + const error = yield* provideCliTestLayers( + runCli(["pair", "--base-dir", baseDir]).pipe(Effect.flip), + ); - const rendered = String( - typeof error === "object" && error !== null && "cause" in error ? error.cause : error, - ); - assert.notInclude(rendered, "No running T3 Code server found."); - assert.include(rendered, "host-a"); + const rendered = String( + typeof error === "object" && error !== null && "cause" in error ? error.cause : error, + ); + assert.notInclude(rendered, "No running T3 Code server found."); + assert.include(rendered, "host-a"); + } }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))), ); diff --git a/apps/server/src/cli/pair.ts b/apps/server/src/cli/pair.ts index 72d28fb1f1dcc..075fb959f18db 100644 --- a/apps/server/src/cli/pair.ts +++ b/apps/server/src/cli/pair.ts @@ -295,17 +295,16 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* ( if (Option.isNone(state)) { continue; } - const hostname = otherMachineOf(state.value, localHostname); - if (hostname !== undefined) { - otherMachine ??= new ServerOnOtherMachineError({ hostname, statePath }); - continue; - } // A dead server's port may be reused; do not mint tokens in its old database for another server. - if (!isProcessAlive(state.value.pid)) { - continue; - } - const probed = yield* probeEnvironmentDescriptor(state.value.origin); - if (probed._tag !== "descriptor") { + // A live PID that answers is local even under a changed hostname (macOS follows the network). + const probed = isProcessAlive(state.value.pid) + ? yield* probeEnvironmentDescriptor(state.value.origin) + : undefined; + if (probed?._tag !== "descriptor") { + const hostname = otherMachineOf(state.value, localHostname); + if (hostname !== undefined) { + otherMachine ??= new ServerOnOtherMachineError({ hostname, statePath }); + } continue; } return { diff --git a/apps/server/src/serverRuntimeState.test.ts b/apps/server/src/serverRuntimeState.test.ts index 11ce8f4267f7e..bdd4a395ff0fc 100644 --- a/apps/server/src/serverRuntimeState.test.ts +++ b/apps/server/src/serverRuntimeState.test.ts @@ -190,14 +190,8 @@ describe("server ownership", () => { assert.include(refused.message, "do not delete it while a server is running"); assert.notInclude(refused.message, "which this machine shares"); assert.equal(yield* fs.readFileString(statePath), before); - // A renamed host keeps a live local server local. + // A shared $HOME: the owner recorded another machine; its PID may exist here too. yield* owner.publish({ ...state, hostname: "host-a" }); - const renamed = yield* ServerOwnership.acquireServerOwnership(statePath).pipe( - Effect.flip, - ); - assert.notInclude(renamed.message, "host-a"); - // A shared $HOME: the owner recorded another machine, whose PID is not alive here. - yield* owner.publish({ ...state, pid: 4_194_305, hostname: "host-a" }); const remote = yield* ServerOwnership.acquireServerOwnership(statePath).pipe(Effect.flip); assert.include(remote.message, "server on host-a already owns"); assert.include(remote.message, "Stop it on host-a"); diff --git a/apps/server/src/serverRuntimeState.ts b/apps/server/src/serverRuntimeState.ts index e4fa8b30885bf..c80352292328f 100644 --- a/apps/server/src/serverRuntimeState.ts +++ b/apps/server/src/serverRuntimeState.ts @@ -132,17 +132,12 @@ export const clearPersistedServerRuntimeState = Effect.fn( ); }); -/** - * The machine a recorded server runs on, when that is not this one. A live local PID wins: - * hostnames can change under a running server (macOS takes them from the network). - */ +/** The machine a recorded server runs on, when that is not this one. */ export const otherMachineOf = ( - state: Pick, + state: Pick, localHostname: string, ): string | undefined => - state.hostname !== undefined && state.hostname !== localHostname && !isProcessAlive(state.pid) - ? state.hostname - : undefined; + state.hostname !== undefined && state.hostname !== localHostname ? state.hostname : undefined; /** Signal 0 only probes existence; EPERM still means alive. */ export const isProcessAlive = (pid: number): boolean => { diff --git a/apps/server/src/serviceLauncher.ts b/apps/server/src/serviceLauncher.ts index a55403dfec336..c0896077129a3 100644 --- a/apps/server/src/serviceLauncher.ts +++ b/apps/server/src/serviceLauncher.ts @@ -54,19 +54,10 @@ const ownerOnOtherMachine = async (baseDir: string): Promise try { const record = JSON.parse( await NodeFSP.readFile(NodePath.join(baseDir, "userdata", "server-runtime.json"), "utf8"), - ) as { readonly hostname?: unknown; readonly pid?: unknown }; - if (typeof record.hostname !== "string" || record.hostname === NodeOS.hostname()) { - return undefined; - } - if (typeof record.pid === "number" && record.pid > 0) { - try { - process.kill(record.pid, 0); - return undefined; - } catch (error) { - if (error instanceof Error && "code" in error && error.code === "EPERM") return undefined; - } - } - return record.hostname; + ) as { readonly hostname?: unknown }; + return typeof record.hostname === "string" && record.hostname !== NodeOS.hostname() + ? record.hostname + : undefined; } catch { return undefined; }