| author | |
| committer | |
| log | fe417dff4cdc149d6873bb4fbaaa388731628d0b |
| tree | e33b63ac38c6680636b3543e71a782f36f1f1630 |
| parent | efecd3cf8bcec3d1891b5f5a27dc2f6d797c6448 |
| signature | Signed by PGP key B5690EEEBB952194 |
6 files changed, 198 insertions(+), 6 deletions(-)
apps/server/src/binCli.ts+8-2| ... | ... | @@ -15,7 +15,7 @@ import { pairCommand } from "./cli/pair.ts"; |
| 15 | 15 | import { hasCloudPublicConfig } from "./cloud/publicConfig.ts"; |
| 16 | 16 | import { sharedServerCommandFlags } from "./cli/config.ts"; |
| 17 | 17 | import { projectCommand } from "./cli/project.ts"; |
| 18 | import { runServerCommand, serveCommand, startCommand } from "./cli/server.ts"; | |
| 18 | import { runDefaultServerCommand, serveCommand, startCommand } from "./cli/server.ts"; | |
| 19 | 19 | import { updateCommand } from "./cli/update.ts"; |
| 20 | 20 | import { uninstallCommand } from "./cli/uninstall.ts"; |
| 21 | 21 | import { serviceLauncherCommand } from "./cli/serviceLauncher.ts"; |
| ... | ... | @@ -56,8 +56,14 @@ const connectUnavailableCommand = Command.make("connect", { |
| 56 | 56 | export const makeCli = ({ cloudEnabled = hasCloudPublicConfig } = {}) => |
| 57 | 57 | Command.make("t3", { ...sharedServerCommandFlags }).pipe( |
| 58 | 58 | Command.withDescription("Run the T3 Code server."), |
| 59 | Command.withHandler((flags) => runServerCommand(flags)), | |
| 59 | Command.withHandler(runDefaultServerCommand), | |
| 60 | 60 | Command.withSubcommands([ |
| 61 | Command.make("help").pipe( | |
| 62 | Command.withDescription("Show command help."), | |
| 63 | Command.withHandler(() => | |
| 64 | Effect.fail(new CliError.ShowHelp({ commandPath: ["t3"], errors: [] })), | |
| 65 | ), | |
| 66 | ), | |
| 61 | 67 | acpMcpBridgeCommand, |
| 62 | 68 | acpMcpCallCommand, |
| 63 | 69 | startCommand, |
apps/server/src/cli/app.test.ts+86| ... | ... | @@ -17,10 +17,12 @@ import * as NetService from "@t3tools/shared/Net"; |
| 17 | 17 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 18 | 18 | import * as Effect from "effect/Effect"; |
| 19 | 19 | import * as Layer from "effect/Layer"; |
| 20 | import * as Schema from "effect/Schema"; | |
| 20 | 21 | import { Command } from "effect/unstable/cli"; |
| 21 | 22 | import { afterEach, describe, expect, vi } from "vite-plus/test"; |
| 22 | 23 | |
| 23 | 24 | import { makeCli } from "../binCli.ts"; |
| 25 | import { PersistedServerRuntimeState } from "../serverRuntimeState.ts"; | |
| 24 | 26 | |
| 25 | 27 | vi.mock("node:os", async (importOriginal) => { |
| 26 | 28 | const os = await importOriginal<typeof import("node:os")>(); |
| ... | ... | @@ -29,6 +31,8 @@ vi.mock("node:os", async (importOriginal) => { |
| 29 | 31 | |
| 30 | 32 | afterEach(() => vi.mocked(NodeOS.homedir).mockReset()); |
| 31 | 33 | |
| 34 | const encodeRuntimeState = Schema.encodeEffect(Schema.fromJsonString(PersistedServerRuntimeState)); | |
| 35 | ||
| 32 | 36 | const runCli = (args: ReadonlyArray<string>, env: Record<string, string> = {}) => |
| 33 | 37 | Command.runWith(makeCli(), { version: "0.0.0" })(args).pipe( |
| 34 | 38 | Effect.provide( |
| ... | ... | @@ -130,6 +134,88 @@ const withTempDirectory = <A, E, R>( |
| 130 | 134 | (root) => Effect.promise(() => NodeFSP.rm(root, { recursive: true, force: true })), |
| 131 | 135 | ); |
| 132 | 136 | |
| 137 | describe("t3 server command safety", () => { | |
| 138 | it.effect("rejects unknown command words without creating a home or project", () => | |
| 139 | withTempDirectory("t3-cli-unknown-", (root) => | |
| 140 | Effect.gen(function* () { | |
| 141 | const baseDir = NodePath.join(root, "home"); | |
| 142 | for (const word of [ | |
| 143 | "account", | |
| 144 | "login", | |
| 145 | "clients", | |
| 146 | "conenct", | |
| 147 | "package.json", | |
| 148 | "C:new-project", | |
| 149 | ]) { | |
| 150 | const error = yield* runCli([word, "--base-dir", baseDir]).pipe( | |
| 151 | Effect.provideService(HostProcessPlatform, "linux"), | |
| 152 | Effect.flip, | |
| 153 | ); | |
| 154 | expect(String(error)).toContain(`Unknown command "${word}"`); | |
| 155 | expect(yield* pathExists(word)).toBe(word === "package.json"); | |
| 156 | expect(yield* pathExists(baseDir)).toBe(false); | |
| 157 | } | |
| 158 | }), | |
| 159 | ), | |
| 160 | ); | |
| 161 | ||
| 162 | it.effect("shows help without creating state", () => | |
| 163 | withTempDirectory("t3-cli-help-", (root) => | |
| 164 | Effect.gen(function* () { | |
| 165 | const baseDir = NodePath.join(root, "home"); | |
| 166 | const help = yield* runCli(["help"], { T3CODE_HOME: baseDir }).pipe(Effect.flip); | |
| 167 | expect(help).toMatchObject({ _tag: "ShowHelp", commandPath: ["t3"], errors: [] }); | |
| 168 | expect(yield* pathExists(baseDir)).toBe(false); | |
| 169 | }), | |
| 170 | ), | |
| 171 | ); | |
| 172 | ||
| 173 | it.effect("refuses manual startup over a live server before creating directories", () => | |
| 174 | withTempDirectory("t3-cli-running-", (root) => | |
| 175 | Effect.gen(function* () { | |
| 176 | const baseDir = NodePath.join(root, "home"); | |
| 177 | const stateDir = NodePath.join(baseDir, "userdata"); | |
| 178 | const statePath = NodePath.join(stateDir, "server-runtime.json"); | |
| 179 | const record = yield* encodeRuntimeState({ | |
| 180 | version: 1, | |
| 181 | pid: process.pid, | |
| 182 | port: 3773, | |
| 183 | origin: "http://127.0.0.1:3773", | |
| 184 | startedAt: "2026-10-01T00:00:00.000Z", | |
| 185 | serviceManaged: true, | |
| 186 | }); | |
| 187 | yield* Effect.promise(() => NodeFSP.mkdir(stateDir, { recursive: true })); | |
| 188 | yield* Effect.promise(() => NodeFSP.writeFile(statePath, record)); | |
| 189 | const newDirectory = NodePath.join(root, "new-project"); | |
| 190 | const platform = yield* HostProcessPlatform; | |
| 191 | for (const args of [ | |
| 192 | [], | |
| 193 | ["start"], | |
| 194 | ["."], | |
| 195 | ["node_modules"], | |
| 196 | ["C:new-project"], | |
| 197 | [newDirectory], | |
| 198 | ["start", newDirectory], | |
| 199 | ]) { | |
| 200 | const error = yield* runCli(args, { T3CODE_HOME: baseDir }).pipe( | |
| 201 | Effect.provideService( | |
| 202 | HostProcessPlatform, | |
| 203 | args[0] === "C:new-project" ? "win32" : platform, | |
| 204 | ), | |
| 205 | Effect.flip, | |
| 206 | ); | |
| 207 | expect(String(error)).toContain("A T3 Code server is already running"); | |
| 208 | expect(yield* Effect.promise(() => NodeFSP.readFile(statePath, "utf8"))).toBe(record); | |
| 209 | expect(yield* pathExists(newDirectory)).toBe(false); | |
| 210 | expect(yield* Effect.promise(() => NodeFSP.readdir(stateDir))).toEqual([ | |
| 211 | "server-runtime.json", | |
| 212 | ]); | |
| 213 | } | |
| 214 | }), | |
| 215 | ), | |
| 216 | ); | |
| 217 | }); | |
| 218 | ||
| 133 | 219 | describe("t3 app", () => { |
| 134 | 220 | it.effect("rejects SSH before it tries to reach a desktop app", () => |
| 135 | 221 | withTempDirectory("t3-app-ssh-test-", (root) => |
apps/server/src/cli/config.test.ts+47| ... | ... | @@ -80,6 +80,53 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => { |
| 80 | 80 | ); |
| 81 | 81 | }); |
| 82 | 82 | |
| 83 | it.effect("keeps stale records and supervised startup out of the manual launch preflight", () => | |
| 84 | Effect.gen(function* () { | |
| 85 | const fs = yield* FileSystem.FileSystem; | |
| 86 | const path = yield* Path.Path; | |
| 87 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-preflight-" }); | |
| 88 | for (const [name, pid, mode, rejectRunningServer] of [ | |
| 89 | ["stale", 2_147_483_647, "web", true], | |
| 90 | ["desktop", process.pid, "desktop", true], | |
| 91 | ["serve", process.pid, "web", false], | |
| 92 | ] as const) { | |
| 93 | const baseDir = path.join(root, name); | |
| 94 | const stateDir = path.join(baseDir, "userdata"); | |
| 95 | yield* fs.makeDirectory(stateDir, { recursive: true }); | |
| 96 | yield* fs.writeFileString( | |
| 97 | path.join(stateDir, "server-runtime.json"), | |
| 98 | yield* encodeUnknownJson({ | |
| 99 | version: 1, | |
| 100 | pid, | |
| 101 | port: 3773, | |
| 102 | origin: "http://127.0.0.1:3773", | |
| 103 | startedAt: "2026-10-01T00:00:00.000Z", | |
| 104 | }), | |
| 105 | ); | |
| 106 | const cwd = path.join(root, `${name}-project`); | |
| 107 | const config = yield* resolveServerConfig( | |
| 108 | { | |
| 109 | ...minimalWebFlags(baseDir), | |
| 110 | mode: Option.some(mode), | |
| 111 | port: Option.some(8788), | |
| 112 | cwd: Option.some(cwd), | |
| 113 | }, | |
| 114 | Option.none(), | |
| 115 | { rejectRunningServer }, | |
| 116 | ).pipe( | |
| 117 | Effect.provide( | |
| 118 | Layer.merge( | |
| 119 | NetService.layer, | |
| 120 | ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })), | |
| 121 | ), | |
| 122 | ), | |
| 123 | ); | |
| 124 | expect(config.cwd).toBe(cwd); | |
| 125 | expect(yield* fs.exists(cwd)).toBe(true); | |
| 126 | } | |
| 127 | }), | |
| 128 | ); | |
| 129 | ||
| 83 | 130 | it.effect("enables a trimmed reusable auth token only for web dev mode", () => |
| 84 | 131 | Effect.gen(function* () { |
| 85 | 132 | const baseDir = yield* FileSystem.FileSystem.pipe( |
apps/server/src/cli/config.ts+14-1| ... | ... | @@ -19,10 +19,12 @@ import * as Schema from "effect/Schema"; |
| 19 | 19 | import * as SchemaIssue from "effect/SchemaIssue"; |
| 20 | 20 | import * as SchemaTransformation from "effect/SchemaTransformation"; |
| 21 | 21 | import { Argument, Flag } from "effect/unstable/cli"; |
| 22 | import * as CliError from "effect/unstable/cli/CliError"; | |
| 22 | 23 | |
| 23 | 24 | import { readBootstrapEnvelope } from "../bootstrap.ts"; |
| 24 | 25 | import * as ServerConfig from "../config.ts"; |
| 25 | 26 | import { expandHomePath, resolveBaseDir } from "../os-jank.ts"; |
| 27 | import { isProcessAlive, readPersistedServerRuntimeState } from "../serverRuntimeState.ts"; | |
| 26 | 28 | |
| 27 | 29 | const modeFlag = Flag.Literals("mode", ServerConfig.RuntimeMode.literals).pipe( |
| 28 | 30 | Flag.withDescription("Runtime mode. `desktop` keeps loopback defaults unless overridden."), |
| ... | ... | @@ -254,6 +256,7 @@ export const resolveServerConfig = ( |
| 254 | 256 | options?: { |
| 255 | 257 | readonly startupPresentation?: ServerConfig.StartupPresentation; |
| 256 | 258 | readonly forceAutoBootstrapProjectFromCwd?: boolean; |
| 259 | readonly rejectRunningServer?: boolean; | |
| 257 | 260 | }, |
| 258 | 261 | ) => |
| 259 | 262 | Effect.gen(function* () { |
| ... | ... | @@ -324,10 +327,20 @@ export const resolveServerConfig = ( |
| 324 | 327 | ); |
| 325 | 328 | const rawCwd = Option.getOrElse(normalizedFlags.cwd, () => process.cwd()); |
| 326 | 329 | const cwd = path.resolve(yield* expandHomePath(rawCwd.trim())); |
| 327 | yield* fs.makeDirectory(cwd, { recursive: true }); | |
| 328 | 330 | const derivedPaths = yield* ServerConfig.deriveServerPaths(baseDir, devUrl, { |
| 329 | 331 | baseDirIsExplicit: Option.isSome(explicitBaseDir), |
| 330 | 332 | }); |
| 333 | // An interactive CLI must not start over a discovered server. Lifetime locking | |
| 334 | // and supervisor handoff are separate; this preflight cannot arbitrate two starts. | |
| 335 | if (options?.rejectRunningServer && mode === "web") { | |
| 336 | const runtime = yield* readPersistedServerRuntimeState(derivedPaths.serverRuntimeStatePath); | |
| 337 | if (Option.isSome(runtime) && runtime.value.pid > 0 && isProcessAlive(runtime.value.pid)) { | |
| 338 | return yield* new CliError.UserError({ | |
| 339 | cause: `A T3 Code server is already running for ${baseDir} (pid ${runtime.value.pid}, ${runtime.value.origin}). Connect to that server, stop it before starting another, or use a different --base-dir.`, | |
| 340 | }); | |
| 341 | } | |
| 342 | } | |
| 343 | yield* fs.makeDirectory(cwd, { recursive: true }); | |
| 331 | 344 | yield* ServerConfig.ensureServerDirectories(derivedPaths); |
| 332 | 345 | const persistedObservabilitySettings = yield* loadPersistedObservabilitySettings( |
| 333 | 346 | derivedPaths.settingsPath, |
apps/server/src/cli/server.ts+36-2| ... | ... | @@ -1,15 +1,23 @@ |
| 1 | import { HostProcessPlatform } from "@t3tools/shared/hostProcess"; | |
| 1 | 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FileSystem from "effect/FileSystem"; | |
| 4 | import * as Option from "effect/Option"; | |
| 5 | import * as Schema from "effect/Schema"; | |
| 2 | 6 | import { Command, GlobalFlag } from "effect/unstable/cli"; |
| 7 | import * as CliError from "effect/unstable/cli/CliError"; | |
| 3 | 8 | |
| 4 | 9 | import * as ServerConfig from "../config.ts"; |
| 5 | 10 | import { runServer } from "../server.ts"; |
| 6 | 11 | import { type CliServerFlags, resolveServerConfig, sharedServerCommandFlags } from "./config.ts"; |
| 7 | 12 | |
| 8 | export const runServerCommand = ( | |
| 13 | const encodeCommand = Schema.encodeEffect(Schema.fromJsonString(Schema.String)); | |
| 14 | ||
| 15 | const runServerCommand = ( | |
| 9 | 16 | flags: CliServerFlags, |
| 10 | 17 | options?: { |
| 11 | 18 | readonly startupPresentation?: ServerConfig.StartupPresentation; |
| 12 | 19 | readonly forceAutoBootstrapProjectFromCwd?: boolean; |
| 20 | readonly rejectRunningServer?: boolean; | |
| 13 | 21 | }, |
| 14 | 22 | ) => |
| 15 | 23 | Effect.gen(function* () { |
| ... | ... | @@ -18,9 +26,35 @@ export const runServerCommand = ( |
| 18 | 26 | return yield* runServer.pipe(Effect.provideService(ServerConfig.ServerConfig, config)); |
| 19 | 27 | }); |
| 20 | 28 | |
| 29 | /** Bare words can name existing directories, but must not create typo projects. */ | |
| 30 | export const runDefaultServerCommand = (flags: CliServerFlags) => | |
| 31 | Effect.gen(function* () { | |
| 32 | if (Option.isSome(flags.cwd)) { | |
| 33 | const cwd = flags.cwd.value.trim(); | |
| 34 | const fs = yield* FileSystem.FileSystem; | |
| 35 | const platform = yield* HostProcessPlatform; | |
| 36 | const explicitPath = | |
| 37 | cwd === "." || | |
| 38 | cwd === ".." || | |
| 39 | cwd === "~" || | |
| 40 | /[/\\]/.test(cwd) || | |
| 41 | (platform === "win32" && /^[a-z]:/i.test(cwd)); | |
| 42 | if ( | |
| 43 | !explicitPath && | |
| 44 | (!(yield* fs.exists(cwd)) || (yield* fs.stat(cwd)).type !== "Directory") | |
| 45 | ) { | |
| 46 | return yield* new CliError.UserError({ | |
| 47 | cause: cwd, | |
| 48 | userMessage: `Unknown command ${yield* encodeCommand(cwd)}. Use "t3 --help" for commands or an explicit path such as "t3 ./my-project" for a new directory.`, | |
| 49 | }); | |
| 50 | } | |
| 51 | } | |
| 52 | return yield* runServerCommand(flags, { rejectRunningServer: true }); | |
| 53 | }); | |
| 54 | ||
| 21 | 55 | export const startCommand = Command.make("start", { ...sharedServerCommandFlags }).pipe( |
| 22 | 56 | Command.withDescription("Run the T3 Code server."), |
| 23 | Command.withHandler((flags) => runServerCommand(flags)), | |
| 57 | Command.withHandler((flags) => runServerCommand(flags, { rejectRunningServer: true })), | |
| 24 | 58 | ); |
| 25 | 59 | |
| 26 | 60 | export const serveCommand = Command.make("serve", { ...sharedServerCommandFlags }).pipe( |
docs/user/install.md+7-1| ... | ... | @@ -33,7 +33,13 @@ line to add. Set `T3CODE_CHANNEL=nightly` to install the nightly train, or |
| 33 | 33 | | Move to the newest release | `t3 update` | |
| 34 | 34 | | Remove it again | `t3 uninstall` | |
| 35 | 35 | |
| 36 | Run `t3 --help` for the full reference. | |
| 36 | Run `t3 help` or `t3 --help` for the full reference. To start in a new working | |
| 37 | directory, use an explicit path such as `t3 ./my-project`. A bare directory name | |
| 38 | is accepted only if it already exists. | |
| 39 | ||
| 40 | If `t3` or `t3 start` reports an already running server, connect to that server | |
| 41 | instead. Stop it before starting a replacement, or use a different `--base-dir` | |
| 42 | for an independent server. | |
| 37 | 43 | |
| 38 | 44 | To try T3 Code once without installing it, run `npx t3@latest` instead (needs |
| 39 | 45 | Node.js for `npx`). |