Skip to content
Closed
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
1 change: 1 addition & 0 deletions apps/server/src/bin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const makeCliTestServerConfig = (baseDir: string) =>
mode: "web",
port: 0,
host: "127.0.0.1",
environmentLabel: undefined,
cwd: process.cwd(),
baseDir,
...derivedPaths,
Expand Down
12 changes: 12 additions & 0 deletions apps/server/src/cli/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
mode: Option.none(),
port: Option.none(),
host: Option.none(),
environmentLabel: Option.none(),
baseDir: Option.none(),
cwd: Option.none(),
devUrl: Option.none(),
Expand All @@ -98,6 +99,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
T3CODE_MODE: "desktop",
T3CODE_PORT: "4001",
T3CODE_HOST: "0.0.0.0",
T3CODE_ENVIRONMENT_LABEL: " Build Farm 03 ",
T3CODE_HOME: baseDir,
VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
T3CODE_DEV_ALLOWED_ORIGINS:
Expand All @@ -122,6 +124,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
baseDir,
...derivedPaths,
host: "0.0.0.0",
environmentLabel: "Build Farm 03",
staticDir: undefined,
devUrl: new URL("http://127.0.0.1:5173"),
devAllowedOrigins: ["https://host.example.ts.net", "https://phone.example.ts.net"],
Expand Down Expand Up @@ -150,6 +153,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
mode: Option.some("web"),
port: Option.some(8788),
host: Option.some("127.0.0.1"),
environmentLabel: Option.some("Flag Label"),
baseDir: Option.some(baseDir),
cwd: Option.none(),
devUrl: Option.some(new URL("http://127.0.0.1:4173")),
Expand All @@ -171,6 +175,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
T3CODE_MODE: "desktop",
T3CODE_PORT: "4001",
T3CODE_HOST: "0.0.0.0",
T3CODE_ENVIRONMENT_LABEL: "Env Label",
T3CODE_HOME: join(NodeOS.tmpdir(), "ignored-base"),
VITE_DEV_SERVER_URL: "http://127.0.0.1:5173",
T3CODE_NO_BROWSER: "false",
Expand All @@ -193,6 +198,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
baseDir,
...derivedPaths,
host: "127.0.0.1",
environmentLabel: "Flag Label",
staticDir: undefined,
devUrl: new URL("http://127.0.0.1:4173"),
noBrowser: true,
Expand Down Expand Up @@ -228,6 +234,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
mode: Option.some("web"),
port: Option.some(8788),
host: Option.some("127.0.0.1"),
environmentLabel: Option.none(),
baseDir: Option.some(baseDir),
cwd: Option.none(),
devUrl: Option.some(new URL("http://127.0.0.1:4173")),
Expand Down Expand Up @@ -305,6 +312,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
mode: Option.none(),
port: Option.none(),
host: Option.none(),
environmentLabel: Option.none(),
baseDir: Option.none(),
cwd: Option.none(),
devUrl: Option.none(),
Expand Down Expand Up @@ -373,6 +381,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
mode: Option.some("desktop"),
port: Option.some(4888),
host: Option.none(),
environmentLabel: Option.none(),
baseDir: Option.some(baseDir),
cwd: Option.some(customCwd),
devUrl: Option.some(new URL("http://127.0.0.1:5173")),
Expand Down Expand Up @@ -435,6 +444,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
mode: Option.none(),
port: Option.some(8788),
host: Option.some("127.0.0.1"),
environmentLabel: Option.none(),
baseDir: Option.none(),
cwd: Option.none(),
devUrl: Option.some(new URL("http://127.0.0.1:4173")),
Expand Down Expand Up @@ -511,6 +521,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
mode: Option.some("desktop"),
port: Option.some(4888),
host: Option.none(),
environmentLabel: Option.none(),
baseDir: Option.some(baseDir),
cwd: Option.none(),
devUrl: Option.none(),
Expand Down Expand Up @@ -568,6 +579,7 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => {
mode: Option.some("web"),
port: Option.some(3773),
host: Option.none(),
environmentLabel: Option.none(),
baseDir: Option.some(baseDir),
cwd: Option.none(),
devUrl: Option.none(),
Expand Down
24 changes: 24 additions & 0 deletions apps/server/src/cli/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export const logWebSocketEventsFlag = Flag.boolean("log-websocket-events").pipe(
Flag.withAlias("log-ws-events"),
Flag.optional,
);
export const environmentLabelFlag = Flag.string("environment-label").pipe(
Flag.withDescription(
"Friendly display name for this environment shown to clients, overriding the OS-detected computer name (equivalent to T3CODE_ENVIRONMENT_LABEL). Useful for telling multiple T3 Code servers on the same machine apart.",
),
Flag.optional,
);
export const tailscaleServeFlag = Flag.boolean("tailscale-serve").pipe(
Flag.withDescription(
"Configure Tailscale Serve to expose this backend over HTTPS on the Tailnet.",
Expand Down Expand Up @@ -104,6 +110,10 @@ const EnvServerConfig = Config.all({
),
port: Config.port("T3CODE_PORT").pipe(Config.option, Config.map(Option.getOrUndefined)),
host: Config.string("T3CODE_HOST").pipe(Config.option, Config.map(Option.getOrUndefined)),
environmentLabel: Config.string("T3CODE_ENVIRONMENT_LABEL").pipe(
Config.option,
Config.map(Option.getOrUndefined),
),
t3Home: Config.string("T3CODE_HOME").pipe(Config.option, Config.map(Option.getOrUndefined)),
devUrl: Config.url("VITE_DEV_SERVER_URL").pipe(Config.option, Config.map(Option.getOrUndefined)),
devAllowedOrigins: Config.string("T3CODE_DEV_ALLOWED_ORIGINS").pipe(
Expand Down Expand Up @@ -145,6 +155,7 @@ export interface CliServerFlags {
readonly mode: Option.Option<ServerConfig.RuntimeMode>;
readonly port: Option.Option<number>;
readonly host: Option.Option<string>;
readonly environmentLabel: Option.Option<string>;
readonly baseDir: Option.Option<string>;
readonly cwd: Option.Option<string>;
readonly devUrl: Option.Option<URL>;
Expand Down Expand Up @@ -174,6 +185,7 @@ export const sharedServerCommandFlags = {
mode: modeFlag,
port: portFlag,
host: hostFlag,
environmentLabel: environmentLabelFlag,
baseDir: baseDirFlag,
cwd: Argument.string("cwd").pipe(
Argument.withDescription(
Expand Down Expand Up @@ -224,6 +236,7 @@ export const resolveServerConfig = (
mode: flags.mode ?? Option.none(),
port: flags.port ?? Option.none(),
host: flags.host ?? Option.none(),
environmentLabel: flags.environmentLabel ?? Option.none(),
baseDir: flags.baseDir ?? Option.none(),
cwd: flags.cwd ?? Option.none(),
devUrl: flags.devUrl ?? Option.none(),
Expand Down Expand Up @@ -347,6 +360,15 @@ export const resolveServerConfig = (
),
() => (mode === "desktop" ? "127.0.0.1" : undefined),
);
const environmentLabel = Option.getOrUndefined(
resolveOptionPrecedence(
normalizedFlags.environmentLabel,
Option.fromUndefinedOr(env.environmentLabel),
).pipe(
Option.map((value) => value.trim()),
Option.filter((value) => value.length > 0),
),
);
const logLevel = Option.getOrElse(cliLogLevel, () => env.logLevel);

const config: ServerConfig.ServerConfig["Service"] = {
Expand All @@ -373,6 +395,7 @@ export const resolveServerConfig = (
...derivedPaths,
serverTracePath,
host,
environmentLabel,
staticDir,
devUrl,
devAllowedOrigins: env.devAllowedOrigins,
Expand Down Expand Up @@ -400,6 +423,7 @@ export const resolveCliAuthConfig = (
mode: Option.none(),
port: Option.none(),
host: Option.none(),
environmentLabel: Option.none(),
baseDir: flags.baseDir,
cwd: Option.none(),
devUrl: flags.devUrl ?? Option.none(),
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/cli/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ const makePairServerConfig = Effect.fn(function* (input: {
mode: "web",
port: state.port,
host: state.host,
environmentLabel: undefined,
cwd: process.cwd(),
baseDir,
...derivedPaths,
Expand Down
7 changes: 7 additions & 0 deletions apps/server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ export class ServerConfig extends Context.Service<
readonly mode: RuntimeMode;
readonly port: number;
readonly host: string | undefined;
/**
* Friendly display label for this environment, shown to clients in place
* of the OS-detected computer name (`T3CODE_ENVIRONMENT_LABEL` /
* `--environment-label`). Undefined falls back to OS detection.
*/
readonly environmentLabel: string | undefined;
readonly cwd: string;
readonly baseDir: string;
readonly staticDir: string | undefined;
Expand Down Expand Up @@ -188,6 +194,7 @@ const makeTest = Effect.fn("ServerConfig.makeTest")(function* (
tailscaleServePort: 443,
port: 0,
host: undefined,
environmentLabel: undefined,
desktopBootstrapToken: undefined,
desktopTelemetryFd: undefined,
desktopTelemetryControlFd: undefined,
Expand Down
28 changes: 28 additions & 0 deletions apps/server/src/environment/ServerEnvironment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const makeServerConfig = Effect.fn(function* (baseDir: string) {
tailscaleServePort: 443,
port: 0,
host: undefined,
environmentLabel: undefined,
desktopBootstrapToken: undefined,
staticDir: undefined,
devUrl: undefined,
Expand Down Expand Up @@ -96,6 +97,33 @@ it.layer(NodeServices.layer)("ServerEnvironmentLive", (it) => {
}),
);

it.effect("uses the configured environment label instead of the OS-detected name", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const baseDir = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-server-environment-label-test-",
});
const serverConfig = yield* makeServerConfig(baseDir);
yield* ServerConfig.ensureServerDirectories(serverConfig);

const descriptor = yield* Effect.gen(function* () {
const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;
return yield* serverEnvironment.getDescriptor;
}).pipe(
Effect.provide(
ServerEnvironment.layer.pipe(
Layer.provide(emptySecretStoreLayer),
Layer.provide(
ServerConfig.layer({ ...serverConfig, environmentLabel: "Mark's Mac mini (9773)" }),
),
),
),
);

expect(descriptor.label).toBe("Mark's Mac mini (9773)");
}),
);

it.effect("reports agent activity publishing from the current secret state", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
Expand Down
5 changes: 4 additions & 1 deletion apps/server/src/environment/ServerEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ export const make = Effect.gen(function* () {

const environmentId = EnvironmentId.make(environmentIdRaw);
const cwdBaseName = path.basename(serverConfig.cwd).trim();
const label = yield* resolveServerEnvironmentLabel({ cwdBaseName });
const label = yield* resolveServerEnvironmentLabel({
cwdBaseName,
overrideLabel: serverConfig.environmentLabel,
});
const launcher = yield* resolveServiceLauncherMode();
const serverSelfUpdate = resolveServerSelfUpdateCapability({
desktopManaged: serverConfig.mode === "desktop",
Expand Down
48 changes: 48 additions & 0 deletions apps/server/src/environment/ServerEnvironmentLabel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,54 @@ afterEach(() => {
});

describe("resolveServerEnvironmentLabel", () => {
it.effect("prefers a configured override label over the macOS ComputerName", () =>
Effect.gen(function* () {
const result = yield* ServerEnvironmentLabel.resolveServerEnvironmentLabel({
cwdBaseName: "t3code",
overrideLabel: "Mark's Mac mini (9773)",
}).pipe(Effect.provide(withHostPlatform(TestLayer, "darwin", "macbook-pro")));

expect(result).toBe("Mark's Mac mini (9773)");
expect(runMock).not.toHaveBeenCalled();
}),
);

it.effect("trims a configured override label", () =>
Effect.gen(function* () {
const result = yield* ServerEnvironmentLabel.resolveServerEnvironmentLabel({
cwdBaseName: "t3code",
overrideLabel: " Staging Box ",
}).pipe(Effect.provide(withHostPlatform(TestLayer, "linux", "buildbox")));

expect(result).toBe("Staging Box");
expect(runMock).not.toHaveBeenCalled();
}),
);

it.effect("falls back to OS detection when the override label is blank", () =>
Effect.gen(function* () {
runMock.mockReturnValueOnce(
Effect.succeed({
stdout: " Julius's MacBook Pro \n",
stderr: "",
code: ChildProcessSpawner.ExitCode(0),
timedOut: false,
stdoutTruncated: false,
stderrTruncated: false,
stdoutInvalidUtf8: false,
stderrInvalidUtf8: false,
}),
);

const result = yield* ServerEnvironmentLabel.resolveServerEnvironmentLabel({
cwdBaseName: "t3code",
overrideLabel: " ",
}).pipe(Effect.provide(withHostPlatform(TestLayer, "darwin", "macbook-pro")));

expect(result).toBe("Julius's MacBook Pro");
}),
);

it.effect("uses hostname fallback regardless of launch mode", () =>
Effect.gen(function* () {
const result = yield* ServerEnvironmentLabel.resolveServerEnvironmentLabel({
Expand Down
13 changes: 13 additions & 0 deletions apps/server/src/environment/ServerEnvironmentLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import * as ProcessRunner from "../processRunner.ts";

interface ResolveServerEnvironmentLabelInput {
readonly cwdBaseName: string;
/**
* Operator-configured label (from `T3CODE_ENVIRONMENT_LABEL` or
* `--environment-label`). Takes precedence over every OS-derived probe
* below: the OS computer name is shared by every T3 Code instance running
* on the same machine, so it cannot distinguish them, but an explicit
* override can.
*/
readonly overrideLabel?: string | undefined;
}

const ServerEnvironmentLabelCommandProbe = Schema.Literals([
Expand Down Expand Up @@ -182,6 +190,11 @@ const resolveFriendlyHostLabel = Effect.fn("resolveFriendlyHostLabel")(function*
export const resolveServerEnvironmentLabel = Effect.fn("resolveServerEnvironmentLabel")(function* (
input: ResolveServerEnvironmentLabelInput,
) {
const overrideLabel = normalizeLabel(input.overrideLabel);
if (overrideLabel) {
return overrideLabel;
}

const friendlyHostLabel = yield* resolveFriendlyHostLabel();
if (friendlyHostLabel) {
return friendlyHostLabel;
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ const buildAppUnderTest = (options?: {
mode: "desktop",
port: 0,
host: "127.0.0.1",
environmentLabel: undefined,
cwd: process.cwd(),
baseDir,
...derivedPaths,
Expand Down
Loading