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
47 changes: 2 additions & 45 deletions apps/server/src/cli/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ import * as Option from "effect/Option";
import * as References from "effect/References";
import * as Schema from "effect/Schema";
import { Command, Flag, GlobalFlag } from "effect/unstable/cli";
import {
FetchHttpClient,
HttpClient,
HttpClientRequest,
HttpClientResponse,
} from "effect/unstable/http";
import { FetchHttpClient } from "effect/unstable/http";

import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
import * as ServerConfig from "../config.ts";
import { type EnvironmentProbeResult, probeEnvironmentDescriptor } from "../environmentProbe.ts";
import { resolveBaseDir } from "../os-jank.ts";
import {
type PersistedServerRuntimeState,
Expand All @@ -56,8 +52,6 @@ import {
} from "../startupAccess.ts";
import { baseDirFlag, DurationFromString } from "./config.ts";

const WELL_KNOWN_ENVIRONMENT_PATH = "/.well-known/t3/environment";
const PAIR_PROBE_TIMEOUT = Duration.millis(2_500);
// Tailscale provisions an HTTPS certificate on the first request to a fresh
// serve mapping, which can take a few seconds.
const TAILSCALE_PROBE_ATTEMPTS = 5;
Expand Down Expand Up @@ -193,43 +187,6 @@ export const formatPairOutput = (input: {
"",
].join("\n");

/**
* Three outcomes, because they drive different decisions: a T3 descriptor
* (pair with it), nothing answering (safe to configure Tailscale Serve), or
* something answering that is not a T3 server (do NOT overwrite its mapping).
*/
type EnvironmentProbeResult =
| { readonly _tag: "descriptor"; readonly descriptor: ExecutionEnvironmentDescriptor }
| { readonly _tag: "unreachable" }
| { readonly _tag: "not-a-t3-server" };

const probeEnvironmentDescriptor = (
baseUrl: string,
): Effect.Effect<EnvironmentProbeResult, never, HttpClient.HttpClient> =>
Effect.gen(function* () {
const client = yield* HttpClient.HttpClient;
const request = HttpClientRequest.get(new URL(WELL_KNOWN_ENVIRONMENT_PATH, baseUrl).toString());
const response = yield* client.execute(request).pipe(
Effect.timeout(PAIR_PROBE_TIMEOUT),
// Transport failure or timeout: nothing (reachable) is listening there.
Effect.mapError(() => ({ _tag: "unreachable" }) as const),
);
// Bad-gateway family means a proxy (Tailscale Serve) answered for a
// backend that is gone — a stale mapping, not a live occupant. Treating
// it as unreachable lets `t3 pair --tailscale` repair its own mapping
// after the server's port changed.
if (response.status === 502 || response.status === 503 || response.status === 504) {
return { _tag: "unreachable" } as const;
}
// Anything else that answered HTTP but not with a valid descriptor is
// some other service.
const descriptor = yield* HttpClientResponse.filterStatusOk(response).pipe(
Effect.flatMap(HttpClientResponse.schemaBodyJson(ExecutionEnvironmentDescriptor)),
Effect.mapError(() => ({ _tag: "not-a-t3-server" }) as const),
);
return { _tag: "descriptor", descriptor } as const;
}).pipe(Effect.catch((outcome) => Effect.succeed(outcome)));

interface DiscoveredPairTarget {
readonly baseDir: string;
readonly variant: PairStateVariant;
Expand Down
50 changes: 50 additions & 0 deletions apps/server/src/environmentProbe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* "Is a T3 Code server answering at this origin?" — shared by `t3 pair`
* discovery and by the Tailscale Serve ownership check, which both have to
* decide whether an endpoint is safe to take over.
*/
import { ExecutionEnvironmentDescriptor } from "@t3tools/contracts";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http";

const WELL_KNOWN_ENVIRONMENT_PATH = "/.well-known/t3/environment";
const ENVIRONMENT_PROBE_TIMEOUT = Duration.millis(2_500);

/**
* Three outcomes, because they drive different decisions: a T3 descriptor
* (pair with it, or leave its serve mapping alone), nothing answering (safe to
* configure Tailscale Serve), or something answering that is not a T3 server
* (do NOT overwrite its mapping).
*/
export type EnvironmentProbeResult =
| { readonly _tag: "descriptor"; readonly descriptor: ExecutionEnvironmentDescriptor }
| { readonly _tag: "unreachable" }
| { readonly _tag: "not-a-t3-server" };

export const probeEnvironmentDescriptor = (
baseUrl: string,
): Effect.Effect<EnvironmentProbeResult, never, HttpClient.HttpClient> =>
Effect.gen(function* () {
const client = yield* HttpClient.HttpClient;
const request = HttpClientRequest.get(new URL(WELL_KNOWN_ENVIRONMENT_PATH, baseUrl).toString());
const response = yield* client.execute(request).pipe(
Effect.timeout(ENVIRONMENT_PROBE_TIMEOUT),
// Transport failure or timeout: nothing (reachable) is listening there.
Effect.mapError(() => ({ _tag: "unreachable" }) as const),
);
// Bad-gateway family means a proxy (Tailscale Serve) answered for a
// backend that is gone — a stale mapping, not a live occupant. Treating
// it as unreachable lets a server repair its own mapping after the
// previous owner's port went away.
if (response.status === 502 || response.status === 503 || response.status === 504) {
return { _tag: "unreachable" } as const;
}
// Anything else that answered HTTP but not with a valid descriptor is
// some other service.
const descriptor = yield* HttpClientResponse.filterStatusOk(response).pipe(
Effect.flatMap(HttpClientResponse.schemaBodyJson(ExecutionEnvironmentDescriptor)),
Effect.mapError(() => ({ _tag: "not-a-t3-server" }) as const),
);
return { _tag: "descriptor", descriptor } as const;
}).pipe(Effect.catch((outcome) => Effect.succeed(outcome)));
42 changes: 5 additions & 37 deletions apps/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ import {
import { orchestrationHttpApiLayer } from "./orchestration/http.ts";
import * as NetService from "@t3tools/shared/Net";
import * as RelayClient from "@t3tools/shared/relayClient";
import { disableTailscaleServe, ensureTailscaleServe } from "@t3tools/tailscale";
import { forkParked, ServerActivation } from "./serverActivation.ts";
import { acquireTailscaleServe, releaseTailscaleServe } from "./tailscaleServe.ts";

// MCP handoff thread IDs include escaped provenance and can exceed find-my-way's
// 100-character default for one path segment.
Expand Down Expand Up @@ -628,44 +628,12 @@ export const makeServerLayer = Layer.unwrap(
return null;
}

const localPort = address.port;
return yield* ensureTailscaleServe({
localPort,
return yield* acquireTailscaleServe({
localPort: address.port,
servePort: config.tailscaleServePort,
localHost: "127.0.0.1",
}).pipe(
Effect.as({ localPort, servePort: config.tailscaleServePort }),
Effect.tap(() =>
Effect.logInfo("Tailscale Serve configured", {
localPort,
servePort: config.tailscaleServePort,
}),
),
Effect.catch((cause) =>
Effect.logWarning("Failed to configure Tailscale Serve", {
cause,
localPort,
servePort: config.tailscaleServePort,
}).pipe(Effect.as(null)),
),
);
});
}),
(configured) =>
configured
? disableTailscaleServe({ servePort: configured.servePort }).pipe(
Effect.tap(() =>
Effect.logInfo("Tailscale Serve disabled", {
servePort: configured.servePort,
}),
),
Effect.catch((cause) =>
Effect.logWarning("Failed to disable Tailscale Serve", {
cause,
servePort: configured.servePort,
}),
),
)
: Effect.void,
releaseTailscaleServe,
),
)
: Layer.empty;
Expand Down
177 changes: 177 additions & 0 deletions apps/server/src/tailscaleServe.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import { assert, describe, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Sink from "effect/Sink";
import * as Stream from "effect/Stream";
import { HttpClient, HttpClientError, HttpClientResponse } from "effect/unstable/http";
import { ChildProcessSpawner } from "effect/unstable/process";

import { acquireTailscaleServe, releaseTailscaleServe } from "./tailscaleServe.ts";

const encoder = new TextEncoder();

const descriptorJson = JSON.stringify({
environmentId: "environment-primary",
label: "primary",
platform: { os: "darwin", arch: "arm64" },
serverVersion: "0.0.38",
capabilities: {},
});

const serveStatusJson = (localPort: number) =>
JSON.stringify({
TCP: { "10010": { HTTPS: true } },
Web: {
"host.tail.ts.net:10010": {
Handlers: { "/": { Proxy: `http://127.0.0.1:${String(localPort)}` } },
},
},
});

function mockHandle(stdout: string) {
return ChildProcessSpawner.makeHandle({
pid: ChildProcessSpawner.ProcessId(1),
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
isRunning: Effect.succeed(false),
kill: () => Effect.void,
unref: Effect.succeed(Effect.void),
stdin: Sink.drain,
stdout: Stream.make(encoder.encode(stdout)),
stderr: Stream.empty,
all: Stream.empty,
getInputFd: () => Sink.drain,
getOutputFd: () => Stream.empty,
});
}

/** Records every tailscale invocation and answers `serve status` with `status`. */
function spawnerLayer(status: string) {
const commands: Array<ReadonlyArray<string>> = [];
const layer = Layer.succeed(
ChildProcessSpawner.ChildProcessSpawner,
ChildProcessSpawner.make((command) => {
const args = (command as unknown as { readonly args: ReadonlyArray<string> }).args;
commands.push(args);
return Effect.succeed(mockHandle(args[1] === "status" ? status : ""));
}),
);
return { commands, layer };
}

/** Answers the local descriptor probe: a live T3 server, or nothing listening. */
function httpClientLayer(alive: boolean) {
const probed: Array<string> = [];
const layer = Layer.succeed(
HttpClient.HttpClient,
HttpClient.make((request) => {
probed.push(request.url);
return alive
? Effect.succeed(
HttpClientResponse.fromWeb(
request,
new Response(descriptorJson, {
status: 200,
headers: { "content-type": "application/json" },
}),
),
)
: Effect.fail(
new HttpClientError.HttpClientError({
reason: new HttpClientError.TransportError({
request,
description: "connection refused",
cause: new Error("ECONNREFUSED"),
}),
}),
);
}),
);
return { probed, layer };
}

const serveWrites = (commands: ReadonlyArray<ReadonlyArray<string>>) =>
commands.filter((args) => args[1] === "--bg");

describe("tailscaleServe", () => {
it.effect("writes the mapping when the serve port is free", () => {
const spawner = spawnerLayer("{}");
const http = httpClientLayer(false);

return Effect.gen(function* () {
const owned = yield* acquireTailscaleServe({ localPort: 3773, servePort: 10010 });

assert.deepEqual(owned, { localPort: 3773, servePort: 10010 });
assert.deepEqual(serveWrites(spawner.commands), [
["serve", "--bg", "--https=10010", "http://127.0.0.1:3773"],
]);
assert.deepEqual(http.probed, []);
}).pipe(Effect.provide(Layer.merge(spawner.layer, http.layer)));
});

it.effect("keeps a mapping that already fronts a live server on another port", () => {
const spawner = spawnerLayer(serveStatusJson(3773));
const http = httpClientLayer(true);

return Effect.gen(function* () {
// The second desktop instance: same environment, next free backend port.
const owned = yield* acquireTailscaleServe({ localPort: 3774, servePort: 10010 });

assert.equal(owned, null);
assert.deepEqual(serveWrites(spawner.commands), []);
assert.deepEqual(http.probed, ["http://127.0.0.1:3773/.well-known/t3/environment"]);
}).pipe(Effect.provide(Layer.merge(spawner.layer, http.layer)));
});

it.effect("reclaims a mapping whose target no longer answers", () => {
const spawner = spawnerLayer(serveStatusJson(3774));
const http = httpClientLayer(false);

return Effect.gen(function* () {
const owned = yield* acquireTailscaleServe({ localPort: 3773, servePort: 10010 });

assert.deepEqual(owned, { localPort: 3773, servePort: 10010 });
assert.deepEqual(serveWrites(spawner.commands), [
["serve", "--bg", "--https=10010", "http://127.0.0.1:3773"],
]);
}).pipe(Effect.provide(Layer.merge(spawner.layer, http.layer)));
});

it.effect("adopts a mapping that already points at this server", () => {
const spawner = spawnerLayer(serveStatusJson(3773));
const http = httpClientLayer(true);

return Effect.gen(function* () {
const owned = yield* acquireTailscaleServe({ localPort: 3773, servePort: 10010 });

assert.deepEqual(owned, { localPort: 3773, servePort: 10010 });
assert.deepEqual(serveWrites(spawner.commands), []);
assert.deepEqual(http.probed, []);
}).pipe(Effect.provide(Layer.merge(spawner.layer, http.layer)));
});

it.effect("removes the mapping on shutdown while it still points here", () => {
const spawner = spawnerLayer(serveStatusJson(3773));

return Effect.gen(function* () {
yield* releaseTailscaleServe({ localPort: 3773, servePort: 10010 });

assert.deepEqual(
spawner.commands.filter((args) => args.includes("off")),
[["serve", "--https=10010", "off"]],
);
}).pipe(Effect.provide(spawner.layer));
});

it.effect("leaves a mapping another server has taken over", () => {
const spawner = spawnerLayer(serveStatusJson(3775));

return Effect.gen(function* () {
yield* releaseTailscaleServe({ localPort: 3773, servePort: 10010 });

assert.deepEqual(
spawner.commands.filter((args) => args.includes("off")),
[],
);
}).pipe(Effect.provide(spawner.layer));
});
});
Loading
Loading