From 827469be4dec3ebda50cd3e21f1ffbaf916f65d1 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Fri, 24 Jul 2026 17:38:23 -0700 Subject: [PATCH] Address CodeRabbit review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - The skill told agents both to never put a pairing URL in a response and to hand the shared one to the user. State the rule and its single exception explicitly: give the URL to the person who asked for access, never put it anywhere durable. - A failed `tailscale serve` left the port unshared, because the stale- mapping clear had already run. The clear is still required (old mappings carry path routes that serving "/" would not replace), so say so in the error instead of letting an operator assume the previous mapping survived. - Worktree-derived ports were described as collision-free. They are a preferred offset, not a guarantee — hashes can collide and occupied ports shift both values. Qualified in AGENTS.md, the skill, and docs/reference/scripts.md. CodeRabbit also asked for tests around the pairing-url command; those landed in 0555604de, which it had not yet seen. Co-Authored-By: Claude Opus 5 (1M context) --- .agents/skills/test-t3-app/SKILL.md | 16 +++++-- AGENTS.md | 2 +- docs/reference/scripts.md | 6 ++- scripts/lib/dev-share.test.ts | 71 +++++++++++++++++++++++++++++ scripts/lib/dev-share.ts | 11 +++-- 5 files changed, 95 insertions(+), 11 deletions(-) create mode 100644 scripts/lib/dev-share.test.ts diff --git a/.agents/skills/test-t3-app/SKILL.md b/.agents/skills/test-t3-app/SKILL.md index 6db626a30b..8c9ffa2296 100644 --- a/.agents/skills/test-t3-app/SKILL.md +++ b/.agents/skills/test-t3-app/SKILL.md @@ -18,10 +18,12 @@ Use this skill for the web client. For iOS Simulator, Android Emulator, or physi (see "Share a dev server with the user" below). 4. Keep the terminal session alive and read the selected server port, web port, base directory, and pairing URL from its output. -Ports are derived from the worktree path, so each worktree gets its own stable -pair that survives restarts. Read them from the dev-runner line rather than -assuming `5733`/`13773`; `node scripts/dev-runner.ts dev --dry-run` prints them -without starting anything. +Ports are derived from the worktree path, so a worktree prefers the same pair on +every run rather than racing others for the default. That is a preference, not a +guarantee — the runner shifts both ports when either is already taken. Read the +actual values from the dev-runner line rather than assuming `5733`/`13773`, and +re-read them after a restart; `node scripts/dev-runner.ts dev --dry-run` prints +them without starting anything. Treat a base directory as disposable only when it was created or deliberately selected for the current test. Never delete or directly seed the shared `~/.t3` directory. Prefer starting with a new temporary base directory over clearing state of uncertain ownership. @@ -45,7 +47,9 @@ Treat the overall testing or implementation loop—not an assistant turn or one 4. Wait for the pairing exchange and redirect to finish before navigating elsewhere. 5. Continue in the same browser context so its stored bearer session remains available. -Treat pairing URLs as secrets. Do not copy them into final responses, screenshots, committed files, or durable logs. A pairing token is short-lived and single-use; opening the URL in another browser or opening it twice can consume it. +Treat pairing URLs as secrets: they grant access to the environment. Never put one anywhere it outlives the moment — screenshots, committed files, durable logs, or a PR description. + +The one exception is handing access to the user who asked for it: when they want to open the app themselves (see "Share a dev server with the user"), the URL is the deliverable, so give it to them directly in the response. Do not volunteer one otherwise. A pairing token is short-lived and single-use; opening the URL in another browser or opening it twice can consume it. ## Recover a consumed or expired pairing token @@ -83,6 +87,8 @@ This publishes the web port over HTTPS on the machine's tailnet and prints `[dev-runner] shared on tailnet: https://:/`. The pairing URL logged right after is already built against that origin, so give the user that URL verbatim — it is the deliverable, and it works unchanged on any of their devices. +This is the one case where a pairing URL belongs in a response: it is going to +the person who asked for it. It still must not go anywhere durable. Dev is single-origin: Vite proxies `/api`, `/ws`, `/oauth`, and `/.well-known` to the backend, so one shared port covers the whole app. Do not map backend paths by diff --git a/AGENTS.md b/AGENTS.md index a08ffc25ad..bf499f6a4a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -16,7 +16,7 @@ ## Dev Servers - Start the web stack with `bun run dev` (equivalently `vp run dev`). Never run `vp dev` from the repo root — that starts a bare Vite with no backend. -- Ports are derived from the worktree path, so each worktree has its own stable pair. Read them from the `[dev-runner] …` line; `--dry-run` prints them without starting anything. +- Ports are derived from the worktree path, so a worktree prefers the same pair every run instead of everyone racing for the default. They still shift when something already holds them, so read the actual values from the `[dev-runner] …` line rather than assuming them. `--dry-run` prints them without starting anything. - To let the user try a change on their own device, use `bun run dev:share`. It publishes the web port on the tailnet and logs a pairing URL already built against that origin — hand them that URL as-is. Details and troubleshooting live in the `test-t3-app` skill. - Dev is single-origin: Vite proxies `/api`, `/ws`, `/oauth`, and `/.well-known` to the backend. Do not set `VITE_HTTP_URL`/`VITE_WS_URL` for `dev`/`dev:web` — they compile absolute localhost URLs into the bundle and break every non-localhost origin. - Need a pairing URL for a server that is already running? `bun run dev:pair`. It finds the server itself; no ports or flags to get wrong. diff --git a/docs/reference/scripts.md b/docs/reference/scripts.md index 8409c442ba..6756659264 100644 --- a/docs/reference/scripts.md +++ b/docs/reference/scripts.md @@ -44,10 +44,12 @@ Ports resolve in this order, first match winning: 1. `T3CODE_PORT_OFFSET=` — exact numeric offset, full control. 2. `T3CODE_DEV_INSTANCE=` — numeric offset, or a hashed one for non-numeric values. Example: `T3CODE_DEV_INSTANCE=branch-a vp run dev:desktop` -3. **Git worktree** — the offset is hashed from the worktree path, so every worktree gets its own stable pair that survives restarts and doesn't collide with its siblings. +3. **Git worktree** — the offset is hashed from the worktree path, so a worktree gets the same preferred pair every time instead of everyone starting at the default and racing for it. 4. Otherwise the defaults: server `13773`, web `5733`. -Whatever the source, both ports are then checked on loopback and shifted together if either is taken. The resolved values are printed on the `[dev-runner] …` line; `--dry-run` prints them without starting anything. Read them from there rather than assuming the defaults. +Whatever the source, this only picks a _preferred_ offset. Both ports are then checked on loopback and shifted together if either is taken, so two worktrees whose hashes collide — or one whose ports something else already holds — still start, just not on the offset they asked for. Ports are stable across restarts in practice, not guaranteed. + +Which means: read the resolved values from the `[dev-runner] …` line rather than assuming them, and re-read them after a restart. `--dry-run` prints them without starting anything (it resolves only — it will not touch a `--share` mapping). ## Browser dev is single-origin diff --git a/scripts/lib/dev-share.test.ts b/scripts/lib/dev-share.test.ts new file mode 100644 index 0000000000..4388252ff4 --- /dev/null +++ b/scripts/lib/dev-share.test.ts @@ -0,0 +1,71 @@ +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 { ChildProcessSpawner } from "effect/unstable/process"; + +import { DevShareError, shareDevServer } from "./dev-share.ts"; + +const TAILNET_STATUS = JSON.stringify({ Self: { DNSName: "host.example.ts.net." } }); + +/** + * Answers `tailscale status --json` with a valid tailnet name, and lets each + * test decide how the `serve` call behaves. + */ +const spawnerLayer = (serve: { readonly exitCode: number; readonly stderr?: string }) => + Layer.succeed( + ChildProcessSpawner.ChildProcessSpawner, + ChildProcessSpawner.make((command) => { + const args = "args" in command ? (command.args as ReadonlyArray) : []; + const isStatus = args.includes("status"); + return Effect.succeed( + ChildProcessSpawner.makeHandle({ + pid: ChildProcessSpawner.ProcessId(1), + exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(isStatus ? 0 : serve.exitCode)), + isRunning: Effect.succeed(false), + kill: () => Effect.void, + unref: Effect.succeed(Effect.void), + stdin: Sink.drain, + stdout: isStatus ? Stream.make(new TextEncoder().encode(TAILNET_STATUS)) : Stream.empty, + stderr: + !isStatus && serve.stderr + ? Stream.make(new TextEncoder().encode(serve.stderr)) + : Stream.empty, + all: Stream.empty, + getInputFd: () => Sink.drain, + getOutputFd: () => Stream.empty, + }), + ); + }), + ); + +describe("shareDevServer", () => { + it.effect("returns the tailnet URL for the same port", () => + Effect.gen(function* () { + const shared = yield* shareDevServer({ webPort: 5788 }).pipe( + Effect.provide(spawnerLayer({ exitCode: 0 })), + ); + + assert.equal(shared.host, "host.example.ts.net"); + assert.equal(shared.url, "https://host.example.ts.net:5788/"); + }), + ); + + // The stale-mapping clear runs before serve, so a failure here leaves the + // port serving nothing. Saying only "serve failed" would let an operator + // assume their previous mapping survived. + it.effect("reports that the prior mapping was cleared when serve fails", () => + Effect.gen(function* () { + const error: DevShareError = yield* shareDevServer({ webPort: 5788 }).pipe( + Effect.provide(spawnerLayer({ exitCode: 1, stderr: "port already in use" })), + Effect.flip, + ); + + assert.equal(error.reason, "serve-failed"); + assert.include(error.message, "port already in use"); + assert.include(error.message, "no longer served"); + assert.include(error.message, "5788"); + }), + ); +}); diff --git a/scripts/lib/dev-share.ts b/scripts/lib/dev-share.ts index e4ea6617c5..51e9bcad00 100644 --- a/scripts/lib/dev-share.ts +++ b/scripts/lib/dev-share.ts @@ -153,8 +153,9 @@ export const shareDevServer = Effect.fn("devShare.shareDevServer")(function* (in // Clear any mapping left behind by a run that was killed before its finalizer // could fire. Serve config survives both the process and a reboot, and a - // stale entry may carry path routes we no longer want (older versions mapped - // /ws, /api and friends to a separate backend port). + // stale entry may carry path routes we no longer want — older versions mapped + // /ws, /api and friends to a separate backend port, and serving "/" alone + // would leave those pointing at a port nothing is listening on. yield* unshareDevServer(input.webPort); const serve = yield* runTailscale( @@ -163,9 +164,13 @@ export const shareDevServer = Effect.fn("devShare.shareDevServer")(function* (in ); if (serve.exitCode !== 0) { + // The clear above already happened, so say so: on a re-share this port is + // now serving nothing, and an operator who only saw "serve failed" would + // reasonably assume the previous mapping survived. + const cause = serve.stderr.trim() || `exit code ${String(serve.exitCode)}`; return yield* new DevShareError({ reason: "serve-failed", - detail: serve.stderr.trim() || `exit code ${String(serve.exitCode)} for port ${port}`, + detail: `${cause} (port ${port} is no longer served; any previous mapping for it was cleared before this attempt)`, }); }