mirror of
https://github.com/backnotprop/plannotator.git
synced 2026-09-14 14:17:26 +08:00
5f33b72b2f
* feat(remote): resolve urlHost auto from Tailscale for advertised URLs PLANNOTATOR_URL_HOST=auto (or config urlHost: "auto") detects this machine's tailnet host at first use in a remote session: MagicDNS name from tailscale status --json, falling back to the single tailscale ip -4 CGNAT address. Detection is cached per process, never spawns in local sessions, warns once and falls back to localhost on failure, and stays strictly display-only: binding remains governed by PLANNOTATOR_REMOTE. Pure parsers live in the new @plannotator/shared/tailscale module, vendored to the Pi extension; both runtimes mirror the resolution. * feat(remote): render a terminal QR code for remote-ready session URLs Remote sessions print their advertised URL as the lifeline; the usual next step is opening it on another device (iPad, phone, laptop off the VPS). handleServerReady now also renders a compact unicode QR of that URL via the zero-dependency uqr package, TTY-gated so piped stderr and hook transcripts keep only the plain URL line. Pi keeps URL-only parity: its ready surface is an in-chat notification, not a TTY stream, so a QR block would not render there. * feat(cli): first-class --tailscale mode for review and annotate sessions plannotator review --tailscale (also annotate and annotate-last/last) publishes the session over the user's tailnet: the server stays loopback-bound and the CLI orchestrates tailscale serve --bg --https=<port> http://127.0.0.1:<port>, then advertises the HTTPS tailnet URL with a terminal QR code. Nothing listens beyond localhost and nothing is ever public (serve, never funnel). Guarantees: preconditions fail with actionable errors (CLI missing, daemon down or logged out); a pre-existing serve mapping on the chosen port aborts instead of being stolen and other ports are never touched; every mapping the process creates is torn down on normal completion, SIGINT/SIGTERM, and errors via the exit-routed cleanup handler. When combined with PLANNOTATOR_REMOTE or SSH detection, --tailscale wins and forces local mode with a stderr notice, which also restores the random local port so simultaneous sessions get distinct serve mappings. * fix(remote): await tailscale-ready failures, harden serve teardown and conflict detection Review fixes for #1280 (external review plus internal security review). Startup failures no longer hang the session: startReviewServer and startAnnotateServer now await async ready handlers and stop the server on rejection, and the CLI's --tailscale ready path resolves publishing failures itself with an actionable stderr message and exit 1. Under the bang-prefix skill a hanging loopback server blocked the whole Claude Code prompt. Serve teardown is checked, not assumed: a failed off retries once, then warns with the exact manual command, and a port is only forgotten after a successful off. SIGHUP (terminal close) is now routed through process.exit like SIGINT/SIGTERM so exit-time cleanup runs. Docs no longer claim guaranteed cleanup: --bg mappings survive SIGKILL and reboots, and the manual removal command is documented. Conflict detection sees foreground serve sessions (Foreground.*.TCP), which Tailscale prefers over background mappings, and fails CLOSED on unrecognizable serve status output instead of assuming the port is free. The extracted serve URL must match the requested port, so a version-dependent output shape cannot advertise another mapping's URL. The annotate agent terminal is gated off by default under --tailscale behind the existing PLANNOTATOR_AGENT_TERMINAL_REMOTE opt-in: the PTY token is not an auth boundary against network peers, and tailnet reachability implies terminal reachability. Also: --tailscale is rejected with a clear error on unsupported subcommands and documented in review/annotate/annotate-last and top-level help; the remote-ready QR renders only for URLs actually reachable off-machine (never localhost); urlHost is suppressed for --tailscale runs so the local-session warning cannot mislead; the duplicated auto-host resolution moved into the shared vendored module; tailscale-serve tests restore module and process state via a reset seam.
192 lines
5.9 KiB
TypeScript
192 lines
5.9 KiB
TypeScript
/**
|
|
* Remote session detection and port configuration
|
|
*
|
|
* Environment variables:
|
|
* PLANNOTATOR_REMOTE - Set to "1"/"true" to force remote, "0"/"false" to force local
|
|
* PLANNOTATOR_PORT - Fixed port or inclusive range (default: random locally, 19432 for remote)
|
|
*
|
|
* Legacy (still supported): SSH_TTY, SSH_CONNECTION
|
|
*/
|
|
|
|
import { parsePortSelection } from "@plannotator/shared/port-range";
|
|
import { loadConfig, resolveUrlHost } from "@plannotator/shared/config";
|
|
import { isAutoUrlHost, resolveAutoHostCached } from "@plannotator/shared/tailscale";
|
|
|
|
const DEFAULT_REMOTE_PORT = 19432;
|
|
const LOOPBACK_HOST = "127.0.0.1";
|
|
const MAX_FIXED_PORT_RETRIES = 5;
|
|
const PORT_RETRY_DELAY_MS = 500;
|
|
|
|
/** Return whether a runtime listen failure represents an occupied address. */
|
|
export function isAddressInUseError(err: unknown): boolean {
|
|
return err instanceof Error && (
|
|
(err as NodeJS.ErrnoException).code === "EADDRINUSE" ||
|
|
err.message.includes("EADDRINUSE")
|
|
);
|
|
}
|
|
|
|
function getRemoteOverride(): boolean | null {
|
|
const remote = process.env.PLANNOTATOR_REMOTE;
|
|
if (remote === undefined) {
|
|
return null;
|
|
}
|
|
|
|
if (remote === "1" || remote?.toLowerCase() === "true") {
|
|
return true;
|
|
}
|
|
|
|
if (remote === "0" || remote?.toLowerCase() === "false") {
|
|
return false;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Check if running in a remote session (SSH, devcontainer, etc.)
|
|
*/
|
|
export function isRemoteSession(): boolean {
|
|
const remoteOverride = getRemoteOverride();
|
|
if (remoteOverride !== null) {
|
|
return remoteOverride;
|
|
}
|
|
|
|
// Legacy: SSH_TTY/SSH_CONNECTION (deprecated, silent)
|
|
if (process.env.SSH_TTY || process.env.SSH_CONNECTION) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Get the server ports to try, in order.
|
|
*/
|
|
export function getServerPorts(): number[] {
|
|
return getServerPortConfiguration().ports;
|
|
}
|
|
|
|
function getServerPortConfiguration(): {
|
|
ports: number[];
|
|
isRange: boolean;
|
|
} {
|
|
const envPort = process.env.PLANNOTATOR_PORT;
|
|
if (envPort) {
|
|
const parsed = parsePortSelection(envPort);
|
|
if (parsed) {
|
|
return { ports: parsed.ports, isRange: parsed.kind === "range" };
|
|
}
|
|
console.error(
|
|
`[Plannotator] Warning: Invalid PLANNOTATOR_PORT "${envPort}", using default`
|
|
);
|
|
}
|
|
|
|
// Remote sessions use fixed port for port forwarding; local uses random
|
|
return {
|
|
ports: [isRemoteSession() ? DEFAULT_REMOTE_PORT : 0],
|
|
isRange: false,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Get the first configured server port.
|
|
*/
|
|
export function getServerPort(): number {
|
|
return getServerPorts()[0];
|
|
}
|
|
|
|
/**
|
|
* Start a Bun server on the first available configured port.
|
|
*
|
|
* Bounded ranges advance immediately after EADDRINUSE. A fixed port retains
|
|
* the existing five-attempt retry behavior for transient conflicts.
|
|
*/
|
|
export async function startBunServerOnAvailablePort<TServer>(
|
|
startServer: (port: number) => TServer,
|
|
): Promise<TServer> {
|
|
const { ports: configuredPorts, isRange } = getServerPortConfiguration();
|
|
const portsToTry = isRange
|
|
? configuredPorts
|
|
: Array(MAX_FIXED_PORT_RETRIES).fill(configuredPorts[0]);
|
|
|
|
for (const [index, port] of portsToTry.entries()) {
|
|
try {
|
|
return startServer(port);
|
|
} catch (error: unknown) {
|
|
if (!isAddressInUseError(error)) {
|
|
throw error;
|
|
}
|
|
|
|
if (index < portsToTry.length - 1) {
|
|
if (!isRange) {
|
|
await Bun.sleep(PORT_RETRY_DELAY_MS);
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (!isRange) {
|
|
const hint = isRemoteSession()
|
|
? " (set PLANNOTATOR_PORT to use different port)"
|
|
: "";
|
|
throw new Error(
|
|
`Port ${port} in use after ${MAX_FIXED_PORT_RETRIES} retries${hint}`,
|
|
);
|
|
}
|
|
|
|
const configured = `${configuredPorts[0]}-${configuredPorts.at(-1)}`;
|
|
const hint = isRemoteSession()
|
|
? " (set PLANNOTATOR_PORT to use a different port or range)"
|
|
: "";
|
|
throw new Error(`Port selection ${configured} exhausted${hint}`);
|
|
}
|
|
}
|
|
|
|
throw new Error("Failed to start server");
|
|
}
|
|
|
|
/**
|
|
* Bind local sessions to loopback, but keep remote sessions reachable via the
|
|
* container or host network interface for SSH/devcontainer/Docker forwarding.
|
|
*/
|
|
export function getServerHostname(): string {
|
|
return isRemoteSession() ? "0.0.0.0" : LOOPBACK_HOST;
|
|
}
|
|
|
|
/** True when the advertised-URL host is overridden away from localhost. */
|
|
export function isUrlHostOverridden(): boolean {
|
|
const host = resolveUrlHost(loadConfig());
|
|
if (host === undefined) return false;
|
|
if (isAutoUrlHost(host)) return isRemoteSession() && resolveAutoHostCached() !== undefined;
|
|
return true;
|
|
}
|
|
|
|
let warnedLocalUrlHost = false;
|
|
|
|
/**
|
|
* Compose the URL advertised to the user for a bound port (issue #657).
|
|
* Display-only: the PLANNOTATOR_URL_HOST / urlHost override changes what is
|
|
* printed and opened, never which interface the server listens on
|
|
* (getServerHostname). Remote sessions only: a local session binds loopback,
|
|
* so honoring the override would advertise (and auto-open) a URL nothing is
|
|
* listening on — the override is ignored with a once-per-process warning.
|
|
* The "auto" sentinel resolves the host from Tailscale (resolveAutoHost).
|
|
* Same-machine subprocesses must not use this — they get a loopback URL so a
|
|
* tailnet-only hostname can't break local agent jobs.
|
|
*/
|
|
export function buildAdvertisedUrl(port: number): string {
|
|
const host = resolveUrlHost(loadConfig());
|
|
if (host === undefined) return `http://localhost:${port}`;
|
|
if (!isRemoteSession()) {
|
|
if (!warnedLocalUrlHost) {
|
|
warnedLocalUrlHost = true;
|
|
process.stderr.write(
|
|
`[plannotator] Warning: advertised URL host ${JSON.stringify(host)} ignored — this is a local session, so the server binds loopback and only localhost is reachable. Set PLANNOTATOR_REMOTE=1 to use the override.\n`,
|
|
);
|
|
}
|
|
return `http://localhost:${port}`;
|
|
}
|
|
const resolved = isAutoUrlHost(host) ? resolveAutoHostCached() : host;
|
|
if (resolved === undefined) return `http://localhost:${port}`;
|
|
return `http://${resolved}:${port}`;
|
|
}
|