feat(cli): t3 up asks how your other devices reach this machine #27996
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 18 commits into
main fromrefs/pull/27996/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/cli/up.ts line 215OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +195,21 @@195+ const linked = yield* linkEnvironmentForConnect({196+ headless: flags.headless,197+ installRelayClient: true,198+ });199+ if (linked === null) return;200+ yield* Console.log(`✓ Signed in${linked.identity ? ` as ${linked.identity}` : ""}\n`);201+ }202+ if (!routes.connect && current.connect) {203+ yield* disconnectCloud({ clearAuthorization: false });204+ }205+ yield* CliService.writeServiceEnv(baseDir, { T3CODE_LAN: String(routes.lan) });206+ if (before.host !== undefined) {207+ yield* Console.warn(208+ `service.env sets T3CODE_HOST=${before.host}, which decides where T3 Code listens. Remove it to use the local network choice.`,209+ );210+ }211+212+ const printServeCommand = Effect.fn(function* (reason: string) {213+ const serve = yield* resolveCliCommand("serve");214+ yield* Console.log(215+ `${reason} Run this and keep the terminal open:\n ${serve}${routes.lan ? " --lan" : ""}\n`,apps/server/src/cli/up.ts lines 74 to 74OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +54,21 @@54+ : null;55+ return {56+ service,57+ connect,58+ lan: Option.getOrElse(lan, () => service.installed),59+ lanFromOldDefault: Option.isNone(lan) && service.installed,60+ host: entries.T3CODE_HOST,61+ } satisfies Setup;62+});63+64+/**65+ * The routes `t3 up` starts from. With no route flag it asks about every66+ * route; any route flag means it asks nothing and keeps the routes not named.67+ * A machine with no service yet starts from T3 Connect.68+ */69+export const planRoutes = (70+ setup: { readonly installed: boolean; readonly connect: boolean | null; readonly lan: boolean },71+ flags: { readonly connect: Option.Option<boolean>; readonly lan: Option.Option<boolean> },72+) => {73+ const keep = {74+ connect: setup.connect === true || (!setup.installed && setup.connect !== null),apps/server/src/cli/up.ts line 253OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +233,21 @@233+ : yield* CliService.reconcileService().pipe(234+ Effect.catchTags({235+ BootServicePrerequisiteError: (error) => Effect.succeed({ failed: error.message }),236+ BootServiceUnsupportedError: (error) => Effect.succeed({ failed: error.message }),237+ }),238+ );239+ if ("failed" in result) {240+ yield* printServeCommand(`Background setup did not finish: ${result.failed}`);241+ } else if (result.changed) {242+ yield* Console.log(CliService.formatServiceChange(result));243+ started = true;244+ } else if (needsRestart(current, routes)) {245+ const restart =246+ !interactive ||247+ (yield* Prompt.run(248+ Prompt.Confirm({249+ message: "Restart T3 Code to apply this? Running agent turns stop.",250+ initial: true,251+ }),252+ ).pipe(Effect.catchTags({ QuitError: () => Effect.succeed(false) })));253+ if (!restart) {
- apps/server/src/cli/up.ts lines 114 to 117Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +72,21 @@72+) => {73+ const keep = {74+ connect: setup.connect === true || (!setup.installed && setup.connect !== null),75+ lan: setup.lan,76+ };77+ const ask = Option.isNone(flags.connect) && Option.isNone(flags.lan);78+ return {79+ routes: {80+ connect: Option.getOrElse(flags.connect, () => keep.connect),81+ lan: Option.getOrElse(flags.lan, () => keep.lan),82+ } satisfies Routes,83+ ask: ask84+ ? (["connect", "lan"] as const).filter((r) => r !== "connect" || setup.connect !== null)85+ : [],86+ };87+};88+89+/** A running service reads T3CODE_LAN and its T3 Connect link only when it starts. */90+const needsRestart = (before: Routes, after: Routes) =>91+ after.lan !== before.lan || (after.connect && !before.connect);92+
- @@ -0,0 +170,21 @@170+ const picked = yield* Prompt.run(171+ Prompt.MultiSelect({172+ message: "How can your other devices reach this machine?",173+ choices: plan.ask.map((route) => ({174+ ...CHOICES[route],175+ value: route,176+ selected: plan.routes[route],177+ })),178+ }),179+ ).pipe(Effect.catchTags({ QuitError: () => Effect.succeed(null) }));180+ if (picked === null) return yield* Console.log("Nothing changed.");181+ routes = { connect: picked.includes("connect"), lan: picked.includes("lan") };182+ } else if (plan.ask.length > 0 && !flags.yes && !before.service.installed) {183+ return yield* new CliError.UserError({184+ cause: "routes",185+ userMessage: `Choose how your other devices reach this machine: ${186+ before.connect === null ? "t3 up --lan" : "t3 up --connect, t3 up --lan, or both"187+ }. --yes takes the default.`,188+ });189+ }190+ if (!before.service.installed && !routes.connect && !routes.lan) {
- apps/server/src/cli/up.ts lines 203 to 203OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +183,21 @@183+ return yield* new CliError.UserError({184+ cause: "routes",185+ userMessage: `Choose how your other devices reach this machine: ${186+ before.connect === null ? "t3 up --lan" : "t3 up --connect, t3 up --lan, or both"187+ }. --yes takes the default.`,188+ });189+ }190+ // Nothing to run in the background, but a route left on still gets turned off.191+ if (!before.service.installed && !routes.connect && !routes.lan) {192+ if (current.connect) yield* disconnectCloud({ clearAuthorization: false });193+ if (current.lan) yield* CliService.writeServiceEnv(baseDir, { T3CODE_LAN: "false" });194+ return yield* Console.log(formatNextSteps(routes));195+ }196+ const { installedVersion } = before.service;197+ const newer =198+ installedVersion !== undefined &&199+ compareExactServiceVersions(installedVersion, packageJson.version) > 0;200+ // Asked before anything changes, so a declined restart leaves service.env201+ // matching what the running service read when it started.202+ const restart =203+ before.service.installed && (before.service.current || newer) && needsRestart(current, routes);
- @@ -0,0 +199,21 @@199+ compareExactServiceVersions(installedVersion, packageJson.version) > 0;200+ // Asked before anything changes, so a declined restart leaves service.env201+ // matching what the running service read when it started.202+ // A stopped service of this version is not current, so reconcile starts it;203+ // a newer one is left alone and needs this restart to start.204+ const stopped = before.service.problems?.includes("service-stopped") === true;205+ const restart =206+ before.service.installed &&207+ (before.service.current || newer) &&208+ (needsRestart(current, routes) || stopped);209+ if (restart && interactive && !stopped) {210+ const confirmed = yield* Prompt.run(211+ Prompt.Confirm({212+ message: "Restart T3 Code to apply this? Running agent turns stop.",213+ initial: true,214+ }),215+ ).pipe(Effect.catchTags({ QuitError: () => Effect.succeed(false) }));216+ if (!confirmed) return yield* Console.log("Nothing changed.");217+ }218+219+ if (routes.connect && !current.connect) {
- apps/server/src/cli/up.ts lines 263 to 263OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +243,21 @@243+ headless: flags.headless,244+ installRelayClient: true,245+ });246+ if (linked === null) return;247+ yield* Console.log(`✓ Signed in${linked.identity ? ` as ${linked.identity}` : ""}\n`);248+ }249+ if (!routes.connect && (current.connect || before.connectRevoked)) {250+ yield* disconnectCloud({ clearAuthorization: false });251+ }252+ const previousLan = (yield* CliService.readServiceEnv(baseDir)).entries.T3CODE_LAN;253+ yield* CliService.writeServiceEnv(baseDir, { T3CODE_LAN: String(routes.lan) });254+ if (before.host !== undefined) {255+ yield* Console.warn(256+ `service.env sets T3CODE_HOST=${before.host}, which decides where T3 Code listens. Remove it to use the local network choice.`,257+ );258+ }259+260+ const printServeCommand = Effect.fn(function* (reason: string) {261+ const { command } = yield* resolveHostCliCommand("serve", [262+ ...Option.match(flags.baseDir, { onNone: () => [], onSome: (dir) => ["--base-dir", dir] }),263+ ...(routes.lan ? ["--lan"] : []),
- apps/server/src/cli/up.ts lines 265 to 266OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +246,21 @@246+ let started = false;247+ if (!before.service.supported) {248+ yield* printServeCommand("This machine cannot run T3 Code in the background.");249+ } else {250+ if (newer) {251+ yield* Console.log(252+ `A newer t3@${installedVersion} background service is installed, so it keeps that version.`,253+ );254+ }255+ const result = newer256+ ? ({ changed: false } as const)257+ : yield* CliService.reconcileService().pipe(258+ Effect.catchTags({259+ BootServicePrerequisiteError: (error) => Effect.succeed({ failed: error.message }),260+ BootServiceUnsupportedError: (error) => Effect.succeed({ failed: error.message }),261+ }),262+ );263+ if ("failed" in result) {264+ // An installed service may still run with the old value, so keep the file matching it.265+ if (before.service.installed) {266+ yield* CliService.writeServiceEnv(baseDir, { T3CODE_LAN: previousLan });
- @@ -0,0 +249,21 @@249+ if (!routes.connect && (current.connect || before.connectRevoked)) {250+ yield* disconnectCloud({ clearAuthorization: false });251+ }252+ const previousLan = (yield* CliService.readServiceEnv(baseDir)).entries.T3CODE_LAN;253+ yield* CliService.writeServiceEnv(baseDir, { T3CODE_LAN: String(routes.lan) });254+ // When the service step fails, an installed service still runs with the old255+ // value, so the file goes back to match it.256+ const restoreServiceEnv = before.service.installed257+ ? CliService.writeServiceEnv(baseDir, { T3CODE_LAN: previousLan })258+ : Effect.void;259+ if (before.host !== undefined) {260+ yield* Console.warn(261+ `service.env sets T3CODE_HOST=${before.host}, which decides where T3 Code listens. Remove it to use the local network choice.`,262+ );263+ }264+265+ const printServeCommand = Effect.fn(function* (reason: string) {266+ const { command } = yield* resolveHostCliCommand("serve", [267+ ...Option.match(flags.baseDir, { onNone: () => [], onSome: (dir) => ["--base-dir", dir] }),268+ // Explicit either way, so a T3CODE_LAN in the shell cannot override the choice.269+ routes.lan ? "--lan" : "--no-lan",
- apps/server/src/cli/up.ts line 304OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +284,21 @@284+ }285+ const result = newer286+ ? ({ changed: false } as const)287+ : yield* CliService.reconcileService().pipe(288+ Effect.catchTags({289+ BootServicePrerequisiteError: (error) => Effect.succeed({ failed: error.message }),290+ BootServiceUnsupportedError: (error) => Effect.succeed({ failed: error.message }),291+ }),292+ Effect.tapError(() => restoreServiceEnv),293+ );294+ if ("failed" in result) {295+ yield* restoreServiceEnv;296+ yield* printServeCommand(`Service setup failed: ${result.failed}`);297+ } else if (result.changed) {298+ yield* Console.log(CliService.formatServiceChange(result));299+ started = true;300+ } else if (restart) {301+ if (!interactive && !stopped) {302+ yield* Console.log("Restarting T3 Code. Running agent turns will stop.");303+ }304+ const outcome = yield* service.restart.pipe(Effect.tapError(() => restoreServiceEnv));
- apps/server/src/cli/up.ts lines 234 to 255OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +235,21 @@235+ const confirmed = yield* Prompt.run(236+ Prompt.Confirm({237+ message: "Apply changes and restart? Running agent turns will stop.",238+ initial: true,239+ }),240+ ).pipe(Effect.catchTags({ QuitError: () => Effect.succeed(false) }));241+ if (!confirmed) return yield* Console.log("Nothing changed.");242+ }243+244+ if (routes.connect && !current.connect) {245+ const linked = yield* linkEnvironmentForConnect({246+ headless: flags.headless,247+ installRelayClient: true,248+ });249+ if (linked === null) return;250+ yield* Console.log(251+ `✓ Signed in to T3 Connect${linked.identity ? ` as ${linked.identity}` : ""}`,252+ );253+ }254+ if (!routes.connect && (current.connect || before.connectRevoked)) {255+ yield* disconnectCloud({ clearAuthorization: false });apps/server/src/cli/up.ts lines 306 to 317Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +249,21 @@249+ if (linked === null) return;250+ yield* Console.log(251+ `✓ Signed in to T3 Connect${linked.identity ? ` as ${linked.identity}` : ""}`,252+ );253+ }254+ if (!routes.connect && (current.connect || before.connectRevoked)) {255+ yield* disconnectCloud({ clearAuthorization: false });256+ }257+ const previousLan = (yield* CliService.readServiceEnv(baseDir)).entries.T3CODE_LAN;258+ yield* CliService.writeServiceEnv(baseDir, { T3CODE_LAN: String(routes.lan) });259+ // When the service step fails, an installed service still runs with the old260+ // value, so the file goes back to match it.261+ const restoreServiceEnv = before.service.installed262+ ? CliService.writeServiceEnv(baseDir, { T3CODE_LAN: previousLan })263+ : Effect.void;264+ if (before.host !== undefined) {265+ yield* Console.warn(266+ `T3CODE_HOST=${before.host} in service.env overrides the network choice. Remove it to apply it.`,267+ );268+ }269+
- @@ -0,0 +326,21 @@326+ let started = false;327+ if (!before.service.supported) {328+ yield* printServeCommand("Background service unavailable.");329+ } else {330+ if (newer) {331+ yield* Console.log(`Keeping installed service version t3@${installedVersion}.`);332+ }333+ if (update && !interactive) {334+ yield* Console.log("Updating T3 Code. Running agent turns will stop.");335+ }336+ const result = newer337+ ? ({ changed: false } as const)338+ : yield* CliService.reconcileService().pipe(339+ Effect.catchTags({340+ BootServicePrerequisiteError: (error) => Effect.succeed({ failed: error.message }),341+ BootServiceUnsupportedError: (error) => Effect.succeed({ failed: error.message }),342+ }),343+ Effect.tapError(() => rollBack),344+ );345+ if ("failed" in result) {346+ yield* restoreServiceEnv;apps/server/src/cli/up.ts line 294OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +274,21 @@274+ if (linked === null) return;275+ yield* Console.log(276+ `✓ Signed in to T3 Connect${linked.identity ? ` as ${linked.identity}` : ""}`,277+ );278+ }279+ // A running server unlinks live; when that fails, only a restart stops its tunnel.280+ const unlinkFailed =281+ !routes.connect && (current.connect || before.connectRevoked)282+ ? (yield* disconnectCloud({ clearAuthorization: false })).status === "failed"283+ : false;284+ const previousLan = (yield* CliService.readServiceEnv(baseDir)).entries.T3CODE_LAN;285+ // Also with no service manager: the file records the choice that `t3 status` shows.286+ yield* CliService.writeServiceEnv(baseDir, { T3CODE_LAN: String(routes.lan) });287+ // When the service step fails, an installed service still runs with the old288+ // value, so the file goes back to match it.289+ // A new T3 Connect link is undone too, with any tunnel the failed start saved, so a290+ // rerun sees it as a change and restarts. The sign-in stays.291+ const restoreServiceEnv = before.service.installed292+ ? CliService.writeServiceEnv(baseDir, { T3CODE_LAN: previousLan }).pipe(293+ Effect.andThen(294+ routes.connect && !current.connect
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed