Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions apps/server/src/cli/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,13 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-preflight-" });
const child = yield* liveProcess;
assert(child.pid !== undefined);
for (const [name, pid, mode, rejectRunningServer] of [
["stale", 2_147_483_647, "web", true],
["own-pid", process.pid, "web", true],
["desktop", child.pid, "desktop", true],
["serve", child.pid, "web", false],
for (const [name, pid, mode, rejectRunningServer, hostname] of [
["stale", 2_147_483_647, "web", true, undefined],
["own-pid", process.pid, "web", true, undefined],
// A recreated container or a machine sharing the home: the ownership lock decides.
["foreign-host", child.pid, "web", true, `${NodeOS.hostname()}-peer`],
["desktop", child.pid, "desktop", true, undefined],
["serve", child.pid, "web", false, undefined],
] as const) {
const baseDir = path.join(root, name);
const stateDir = path.join(baseDir, "userdata");
Expand All @@ -142,6 +144,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
yield* encodeUnknownJson({
version: 1,
pid,
hostname,
port: 3773,
origin: "http://127.0.0.1:3773",
startedAt: "2026-10-01T00:00:00.000Z",
Expand Down
10 changes: 8 additions & 2 deletions apps/server/src/cli/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ import { readBootstrapEnvelope } from "../bootstrap.ts";
import { resolveServiceLauncherMode } from "../cloud/serviceLauncherClient.ts";
import * as ServerConfig from "../config.ts";
import { expandHomePath, resolveBaseDir } from "../os-jank.ts";
import { isProcessAlive, readPersistedServerRuntimeState } from "../serverRuntimeState.ts";
import {
isProcessAlive,
isRuntimeStateFromAnotherMachine,
readPersistedServerRuntimeState,
} from "../serverRuntimeState.ts";
import { resolveHostCliCommand } from "./invocation.ts";

const modeFlag = Flag.Literals("mode", ServerConfig.RuntimeMode.literals).pipe(
Expand Down Expand Up @@ -349,9 +353,11 @@ export const resolveServerConfig = Effect.fn("config.resolveServerConfig")(funct
// and supervisor handoff are separate; this preflight cannot arbitrate two starts.
if (options?.rejectRunningServer && mode === "web") {
const runtime = yield* readPersistedServerRuntimeState(derivedPaths.serverRuntimeStatePath);
// A killed container can leave a record with the new server's own PID.
// A killed container can leave a record with the new server's own PID. Another
// host's PID means nothing here, so its record is left to the ownership lock.
if (
Option.isSome(runtime) &&
!isRuntimeStateFromAnotherMachine(runtime.value) &&
runtime.value.pid > 0 &&
runtime.value.pid !== process.pid &&
isProcessAlive(runtime.value.pid)
Expand Down
33 changes: 33 additions & 0 deletions apps/server/src/cli/pair.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,39 @@ describe("t3 pair", () => {
).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);

// A shared $HOME makes another machine's record visible here. The server that
// owns the home still pairs, since a hostname can change while it runs.
it.effect("names the other machine unless the server that owns the home answers", () =>
withDescriptorServer((origin) =>
Effect.gen(function* () {
const baseDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-pair-foreign-test-"));
const statePath = NodePath.join(baseDir, "userdata", "server-runtime.json");
const state = yield* makePersistedServerRuntimeState({
config: { host: "127.0.0.1", devUrl: undefined },
port: Number(new URL(origin).port),
});
const hostname = `${NodeOS.hostname()}-peer`;

yield* persistServerRuntimeState({ path: statePath, state: { ...state, hostname } });
const environmentIdPath = NodePath.join(baseDir, "userdata", "environment-id");
NodeFS.writeFileSync(environmentIdPath, `${testDescriptor.environmentId}\n`);
const output = yield* captureStdout(runCli(["pair", "--base-dir", baseDir]));
assert.include(output, `Pairing URL: ${origin}/pair#token=`);

// The recorded PID and port answer here, but for a server of another home.
NodeFS.writeFileSync(environmentIdPath, "another-home-environment\n");
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.include(rendered, "running on another machine");
assert.include(rendered, hostname);
}),
).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", () =>
Expand Down
49 changes: 43 additions & 6 deletions apps/server/src/cli/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as Console from "effect/Console";
import * as DateTime from "effect/DateTime";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as References from "effect/References";
Expand All @@ -38,6 +39,7 @@ import { resolveBaseDir } from "../os-jank.ts";
import {
type PersistedServerRuntimeState,
isProcessAlive,
isRuntimeStateFromAnotherMachine,
readPersistedServerRuntimeState,
} from "../serverRuntimeState.ts";
import {
Expand Down Expand Up @@ -82,6 +84,22 @@ export class NoRunningServerError extends Schema.TaggedError<NoRunningServerErro
}
}

export class ServerRunningOnAnotherMachineError extends Schema.TaggedError<ServerRunningOnAnotherMachineError>()(
"ServerRunningOnAnotherMachineError",
{
hostname: Schema.String,
statePath: Schema.String,
},
) {
override get message(): string {
return [
`The T3 Code server for this T3 home is running on another machine (\`${this.hostname}\`).`,
`Its record is ${this.statePath}, visible here because this machine shares that T3 home.`,
`Run \`t3 pair\` on \`${this.hostname}\` to pair with it. To run a separate server on this machine, give it its own --base-dir.`,
].join("\n");
}
}

export class IncompatibleServerProtocolError extends Schema.TaggedError<IncompatibleServerProtocolError>()(
"IncompatibleServerProtocolError",
{
Expand Down Expand Up @@ -292,7 +310,9 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* (
}
}

const fs = yield* FileSystem.FileSystem;
const checkedStatePaths: Array<string> = [];
let foreignServer: { readonly hostname: string; readonly statePath: string } | undefined;
for (const baseDir of new Set(bases)) {
for (const variant of ["userdata", "dev"] as const) {
const derivedPaths = yield* ServerConfig.deriveServerPaths(
Expand All @@ -307,21 +327,38 @@ const discoverPairTarget = Effect.fn("pair.discoverPairTarget")(function* (
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") {
const probed = isProcessAlive(state.value.pid)
? yield* probeEnvironmentDescriptor(state.value.origin)
: undefined;
const foreign = isRuntimeStateFromAnotherMachine(state.value);
// Hostnames change under running servers (DHCP), so a foreign record still pairs, but only
// with the server that owns this home: its recorded PID and port can be reused here.
const descriptor =
probed?._tag === "descriptor" &&
(!foreign ||
(yield* fs.readFileString(derivedPaths.environmentIdPath).pipe(
Effect.map((id) => id.trim()),
Effect.orElseSucceed(() => undefined),
)) === probed.descriptor.environmentId)
? probed.descriptor
: undefined;
if (descriptor === undefined) {
if (foreign) {
foreignServer ??= { hostname: state.value.hostname ?? "unknown", statePath };
}
continue;
}
return {
baseDir,
variant,
state: state.value,
descriptor: probed.descriptor,
descriptor,
} satisfies DiscoveredPairTarget;
}
}
if (foreignServer !== undefined) {
return yield* new ServerRunningOnAnotherMachineError(foreignServer);
}
const baseDirArgs = hasExplicitBaseDir ? ["--base-dir", explicitBaseDir] : [];
return yield* new NoRunningServerError({
checkedStatePaths,
Expand Down
8 changes: 6 additions & 2 deletions apps/server/src/cli/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands
{
command,
label: "another home's service",
status: { ...newerServiceStatus, otherBaseDir: "/srv/t3" },
error: { _tag: "BootServiceOtherBaseDirError", installedBaseDir: "/srv/t3" },
status: { ...newerServiceStatus, otherBaseDir: "/srv/t3", otherBaseDirHostname: "peer" },
error: {
_tag: "BootServiceOtherBaseDirError",
installedBaseDir: "/srv/t3",
hostname: "peer",
},
},
]),
)(
Expand Down
9 changes: 8 additions & 1 deletion apps/server/src/cli/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ export const reconcileService = Effect.fn("cli.service.reconcile")(function* (op
return yield* new BootService.BootServiceOtherBaseDirError({
installedBaseDir: status.otherBaseDir,
baseDir,
...(status.otherBaseDirHostname === undefined
? {}
: { hostname: status.otherBaseDirHostname }),
});
}
if (status.installed && status.current) {
Expand Down Expand Up @@ -189,11 +192,15 @@ function formatServiceStatus(
return `T3 Code service\n Status: not installed\n Next: Run \`${commands.install}\`.`;
}
if (status.otherBaseDir !== undefined) {
const manage =
status.otherBaseDirHostname === undefined
? `Pass \`${BootService.formatBootServiceBaseDirOption(status.otherBaseDir)}\` to manage it.`
: `Manage it on \`${status.otherBaseDirHostname}\` with \`${BootService.formatBootServiceBaseDirOption(status.otherBaseDir)}\`.`;
return [
"T3 Code service",
" Status: installed for another T3 home",
` Base dir: ${status.otherBaseDir}`,
` Next: Pass \`${BootService.formatBootServiceBaseDirOption(status.otherBaseDir)}\` to manage it.`,
` Next: ${manage}`,
].join("\n");
}
const installedVersion = status.installedVersion ?? cliVersion;
Expand Down
21 changes: 18 additions & 3 deletions apps/server/src/cli/up.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer))
{
label: "a service for another home is refused without a restart",
installed: true,
status: { current: false, otherBaseDir: "/srv/t3 home" },
status: { current: false, otherBaseDir: "/srv/t3 home", otherBaseDirHostname: "peer" },
fails: true,
answer: "y",
env: "T3CODE_LAN=false\n",
Expand All @@ -273,10 +273,10 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer))
{
label: "another home's service supplies no local network default",
installed: true,
status: { current: false, otherBaseDir: "/srv/t3 home" },
status: { current: false, otherBaseDir: "/srv/t3 home", otherBaseDirHostname: "peer" },
reportStatus: true,
json: {
service: { otherBaseDir: "/srv/t3 home" },
service: { otherBaseDir: "/srv/t3 home", otherBaseDirHostname: "peer" },
lan: { enabled: false, inheritedDefault: false },
},
env: "",
Expand All @@ -285,6 +285,18 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer))
restarts: 0,
envAfter: "",
},
{
label: "status names the machine running another home's service",
installed: true,
status: { current: false, otherBaseDir: "/srv/t3 home", otherBaseDirHostname: "peer" },
reportStatus: true,
prints: ["installed for another T3 home · /srv/t3 home on `peer`", "Manage it on `peer`"],
env: "",
args: [],
installs: 0,
restarts: 0,
envAfter: "",
},
{
label: "status hints keep the caller's --base-dir",
installed: true,
Expand Down Expand Up @@ -362,6 +374,9 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer))
new BootService.BootServiceOtherBaseDirError({
installedBaseDir: status.otherBaseDir,
baseDir,
...(status.otherBaseDirHostname === undefined
? {}
: { hostname: status.otherBaseDirHostname }),
}),
)
: input.installFails === true
Expand Down
16 changes: 14 additions & 2 deletions apps/server/src/cli/up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,16 @@ const formatSetup = (setup: Setup, hints: Hints) => {
const { service } = setup;
// This home's access settings reach no service, so only the service is shown.
if (service.otherBaseDir !== undefined) {
const machine =
service.otherBaseDirHostname === undefined ? "" : ` on \`${service.otherBaseDirHostname}\``;
const manage =
service.otherBaseDirHostname === undefined
? `Pass \`${BootService.formatBootServiceBaseDirOption(service.otherBaseDir)}\` to manage it.`
: `Manage it on \`${service.otherBaseDirHostname}\` with \`${BootService.formatBootServiceBaseDirOption(service.otherBaseDir)}\`.`;
return [
` ${"Service".padEnd(15)}installed for another T3 home · ${service.otherBaseDir}`,
` ${"Service".padEnd(15)}installed for another T3 home · ${service.otherBaseDir}${machine}`,
"",
`Pass \`${BootService.formatBootServiceBaseDirOption(service.otherBaseDir)}\` to manage it.`,
manage,
].join("\n");
}
const rows: ReadonlyArray<readonly [string, string]> = [
Expand Down Expand Up @@ -236,6 +242,9 @@ const setUp = Effect.fn("cli.up")(function* (flags: {
return yield* new BootService.BootServiceOtherBaseDirError({
installedBaseDir: before.service.otherBaseDir,
baseDir,
...(before.service.otherBaseDirHostname === undefined
? {}
: { hostname: before.service.otherBaseDirHostname }),
});
}
if (before.connect === null && Option.getOrElse(flags.connect, () => false)) {
Expand Down Expand Up @@ -516,6 +525,9 @@ const setupJson = (setup: Setup) =>
installed: setup.service.installed,
installedVersion: setup.service.installedVersion ?? null,
otherBaseDir: setup.service.otherBaseDir ?? null,
...(setup.service.otherBaseDirHostname === undefined
? {}
: { otherBaseDirHostname: setup.service.otherBaseDirHostname }),
},
connect:
setup.connect === null ? null : { enabled: setup.connect, revoked: setup.connectRevoked },
Expand Down
17 changes: 17 additions & 0 deletions apps/server/src/cloud/bootService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1226,15 +1226,32 @@ process.disconnect();
const otherHome = yield* fs.makeTempDirectoryScoped({ prefix: "t3-other-home-" });

const other = yield* makeService(undefined, "1.2.3", path.join(otherHome, ".t3"));
// A record from another machine sharing this home names that 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: 1234,
hostname: "another-machine",
port: 3773,
origin: "http://127.0.0.1:3773",
startedAt: "2026-10-01T00:00:00.000Z",
}),
);
commands.length = 0;
expect(yield* other.restart).toBe("skipped");
const refused = yield* other.install().pipe(Effect.flip);
expect(refused._tag).toBe("BootServiceOtherBaseDirError");
expect(refused).toMatchObject({ hostname: "another-machine" });
expect(refused.message).toContain("`another-machine`");
expect(refused.message).not.toContain("uninstall");
// The CLI prints a user message without a stack trace.
expect(CliError.getUserMessage(refused)).toBe(refused.message);
expect(yield* fs.readFileString(plan.unitPath)).toBe(unit);
expect(commands).toEqual([]);
expect((yield* other.status).otherBaseDir).toBe(baseDir);
expect((yield* other.status).otherBaseDirHostname).toBe("another-machine");

const quoted = yield* makeService(undefined, "1.2.3", path.join(otherHome, 'T3 "Data"'));
commands.length = 0;
Expand Down
Loading
Loading