diff --git a/.agents/skills/test-t3-app/SKILL.md b/.agents/skills/test-t3-app/SKILL.md
index 8c9ffa2296..9b68e07b27 100644
--- a/.agents/skills/test-t3-app/SKILL.md
+++ b/.agents/skills/test-t3-app/SKILL.md
@@ -25,6 +25,8 @@ 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.
+Inside a worktree, the dev runner defaults to that worktree's own gitignored `.t3` rather than the shared `~/.t3` — including when `T3CODE_HOME` is exported in the environment, which would otherwise resolve to `~/.t3/userdata`: the live database the user's installed T3 Code is running against. Do not override that default to the shared home.
+
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.
The dev runner disables browser auto-open by default. Do not pass `--browser` during automated testing: an automatically opened page can consume the one-time bootstrap token before the controlled browser uses it.
diff --git a/AGENTS.md b/AGENTS.md
index bf499f6a4a..2d2beeb969 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -20,6 +20,7 @@
- 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.
+- In a worktree, dev state goes to that worktree's own gitignored `.t3` — never the shared `~/.t3` the user's installed app runs against. Do not point a dev server at `~/.t3`, and do not "fix" an ambient `T3CODE_HOME` by passing it through.
## Package Roles
diff --git a/apps/server/src/cli/auth.ts b/apps/server/src/cli/auth.ts
index 1096ce61e4..c43d4b495a 100644
--- a/apps/server/src/cli/auth.ts
+++ b/apps/server/src/cli/auth.ts
@@ -5,6 +5,7 @@ import {
} from "@t3tools/contracts";
import * as Console from "effect/Console";
import * as Effect from "effect/Effect";
+import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Path from "effect/Path";
@@ -132,6 +133,28 @@ const pairingCreateCommand = Command.make("create", {
),
);
+/**
+ * A git worktree's own `.t3`, or undefined outside one. Git marks a linked
+ * worktree by making `.git` a file (`gitdir: …`) rather than a directory —
+ * mirrors `resolveWorktreePath` in scripts/dev-runner.ts, which is what puts
+ * dev state there in the first place.
+ */
+export const resolveWorktreeBaseDir = Effect.fn("auth.resolveWorktreeBaseDir")(function* (
+ cwd: string,
+) {
+ const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const info = yield* fileSystem.stat(path.join(cwd, ".git")).pipe(Effect.option);
+ if (Option.isNone(info) || info.value.type !== "File") {
+ return undefined;
+ }
+ const baseDir = path.join(cwd, ".t3");
+ // Only claim it when the dev runner has actually created it; otherwise let
+ // the normal resolution report "no running server" against the real home.
+ const exists = yield* fileSystem.exists(baseDir).pipe(Effect.orElseSucceed(() => false));
+ return exists ? baseDir : undefined;
+});
+
/**
* The state directory is `/dev` for an implicit dev home and
* `/userdata` otherwise — a split that depends on flags the caller of
@@ -199,7 +222,16 @@ const pairingUrlCommand = Command.make("url", {
Command.withHandler((flags) =>
Effect.gen(function* () {
const logLevel = yield* GlobalFlag.LogLevel;
- const config = yield* resolveCliAuthConfig(flags, logLevel);
+ // Run from a worktree, this command means "the dev server I just started
+ // here" — which the dev runner puts in the worktree's own `.t3`. Falling
+ // through to the shared home would mint a credential into the database
+ // the user's installed T3 Code is running against.
+ const worktreeBaseDir = yield* resolveWorktreeBaseDir(process.cwd());
+ const resolvedFlags =
+ Option.isSome(flags.baseDir) || worktreeBaseDir === undefined
+ ? flags
+ : { ...flags, baseDir: Option.some(worktreeBaseDir) };
+ const config = yield* resolveCliAuthConfig(resolvedFlags, logLevel);
const live = yield* findLiveServerRuntimeState(config);
if (Option.isNone(live)) {
diff --git a/docs/reference/scripts.md b/docs/reference/scripts.md
index 6756659264..39ce240621 100644
--- a/docs/reference/scripts.md
+++ b/docs/reference/scripts.md
@@ -5,7 +5,8 @@
- `vp run dev:pair` — Prints a fresh pairing URL for the dev server already running against this data directory, resolving its port and web origin from `server-runtime.json`. Searches both the `dev` and `userdata` state directories and requires the recorded process to still be alive, so a crashed server's leftover state file is not mistaken for a running one. Add `--base-dir ` only when the server was started with `--home-dir`.
- `vp run dev:server` — Starts just the WebSocket server. The server process runs on Bun (`@effect/platform-bun` + `BunPtyAdapter`), but task running uses `vp run`.
- `vp run dev:web` — Starts just the Vite dev server for the web app.
-- Dev commands implicitly use `~/.t3/dev`, keeping development state separate from `~/.t3/userdata`. An explicit `--home-dir ` stores state under `/userdata`; the base directory remains available for caches, worktrees, and other shared data.
+- Dev commands run from a **git worktree** default to that worktree's own gitignored `.t3`, so feature work never writes to the data directory the installed app uses. This deliberately outranks an ambient `T3CODE_HOME`; pass `--home-dir ` to choose somewhere else.
+- From the **main checkout**, dev commands implicitly use `~/.t3/dev`, keeping development state separate from `~/.t3/userdata`. An explicit `--home-dir ` stores state under `/userdata`; the base directory remains available for caches, worktrees, and other shared data.
- Web dev commands do not auto-open a browser. Open the one-time pairing URL printed by the server so the first browser navigation is authenticated. Set `T3CODE_NO_BROWSER=0` only when interactive auto-open is intentional.
- Pass dev-runner flags directly after the root task name, for example:
`vp run dev --home-dir /tmp/t3code-dev`
diff --git a/scripts/dev-runner.test.ts b/scripts/dev-runner.test.ts
index 197a1e4524..b0f456fff3 100644
--- a/scripts/dev-runner.test.ts
+++ b/scripts/dev-runner.test.ts
@@ -18,6 +18,7 @@ import {
getDevRunnerModeArgs,
resolveModePortOffsets,
resolveOffset,
+ resolveWorktreeHome,
runDevRunnerWithInput,
} from "./dev-runner.ts";
@@ -562,6 +563,25 @@ it.layer(NodeServices.layer)("dev-runner", (it) => {
);
});
+ // A worktree dev server writing to the shared ~/.t3 lands on the *real*
+ // database the installed app uses, because an ambient T3CODE_HOME counts as
+ // an explicit base dir and flips the state dir from `dev` to `userdata`.
+ describe("resolveWorktreeHome", () => {
+ it.effect("keeps a worktree's data directory inside the worktree", () =>
+ Effect.gen(function* () {
+ const path = yield* Path.Path;
+ const home = yield* resolveWorktreeHome("/repos/app-worktree");
+ assert.equal(home, path.join("/repos/app-worktree", ".t3"));
+ }),
+ );
+
+ it.effect("leaves the main checkout on the shared home", () =>
+ Effect.gen(function* () {
+ assert.equal(yield* resolveWorktreeHome(undefined), undefined);
+ }),
+ );
+ });
+
describe("runDevRunnerWithInput", () => {
it.effect("preserves invalid configuration as the exact cause", () =>
Effect.gen(function* () {
@@ -622,6 +642,32 @@ it.layer(NodeServices.layer)("dev-runner", (it) => {
// `tailscale serve` config outlives the process, so a dry run that shared
// would replace and then tear down whatever mapping the port already had.
+ // An ambient T3CODE_HOME must not drag a worktree's dev state onto the
+ // shared home: that resolves to /userdata, the database the user's
+ // installed T3 Code is actively running against.
+ it.effect("keeps an ambient T3CODE_HOME from overriding the worktree home", () =>
+ Effect.gen(function* () {
+ const path = yield* Path.Path;
+ const worktreeHome = yield* resolveWorktreeHome("/repos/app-worktree");
+ const env = yield* createDevRunnerEnv({
+ mode: "dev",
+ baseEnv: { T3CODE_HOME: "/home/user/.t3" },
+ serverOffset: 0,
+ webOffset: 0,
+ // Mirrors the precedence runDevRunnerWithInput applies.
+ t3Home: worktreeHome,
+ browser: undefined,
+ autoBootstrapProjectFromCwd: undefined,
+ logWebSocketEvents: undefined,
+ host: undefined,
+ port: undefined,
+ devUrl: undefined,
+ });
+
+ assert.equal(env.T3CODE_HOME, path.resolve("/repos/app-worktree/.t3"));
+ }),
+ );
+
it.effect("spawns nothing when --dry-run is combined with --share", () => {
let spawnCount = 0;
const spawnerLayer = Layer.succeed(
diff --git a/scripts/dev-runner.ts b/scripts/dev-runner.ts
index 78f61e7027..59e504e397 100644
--- a/scripts/dev-runner.ts
+++ b/scripts/dev-runner.ts
@@ -237,6 +237,24 @@ export function resolveWorktreePath(
});
}
+/**
+ * The data directory a worktree's dev server should use: its own gitignored
+ * `.t3`, keeping feature work off the shared `~/.t3` that the installed app
+ * runs against. `undefined` for the main checkout, which keeps the documented
+ * `~/.t3/dev` behaviour.
+ */
+export function resolveWorktreeHome(
+ worktreePath: string | undefined,
+): Effect.Effect {
+ return Effect.gen(function* () {
+ if (worktreePath === undefined) {
+ return undefined;
+ }
+ const path = yield* Path.Path;
+ return path.join(worktreePath, ".t3");
+ });
+}
+
function resolveBaseDir(baseDir: string | undefined): Effect.Effect {
return Effect.gen(function* () {
const path = yield* Path.Path;
@@ -543,10 +561,12 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) {
),
);
+ const worktreePath = yield* resolveWorktreePath(process.cwd());
+
const { offset, source } = yield* resolveOffset({
portOffset,
devInstance,
- worktreePath: yield* resolveWorktreePath(process.cwd()),
+ worktreePath,
});
const { serverOffset, webOffset } = yield* resolveModePortOffsets({
@@ -557,12 +577,22 @@ export function runDevRunnerWithInput(input: DevRunnerCliInput) {
});
const hostEnvironment = yield* HostProcessEnvironment;
+ // A dev server started inside a worktree defaults to that worktree's own
+ // (gitignored) `.t3`. Otherwise it lands on the shared `~/.t3`, and because
+ // an ambient `T3CODE_HOME` counts as an *explicit* base dir, the state
+ // directory flips from `/dev` to `/userdata` — the real
+ // database the user's installed T3 Code is running against. Feature work in
+ // a throwaway branch has no business writing there, so the worktree default
+ // deliberately outranks the ambient env var. `--home-dir` still wins.
+ const worktreeHome = yield* resolveWorktreeHome(worktreePath);
+ const resolvedT3Home =
+ input.t3Home ?? worktreeHome ?? hostEnvironment.T3CODE_HOME?.trim() ?? undefined;
const env = yield* createDevRunnerEnv({
mode: input.mode,
baseEnv: hostEnvironment,
serverOffset,
webOffset,
- t3Home: input.t3Home,
+ t3Home: resolvedT3Home,
browser: input.browser,
autoBootstrapProjectFromCwd: input.autoBootstrapProjectFromCwd,
logWebSocketEvents: input.logWebSocketEvents,
@@ -709,9 +739,10 @@ const devRunnerCli = Command.make("dev-runner", {
),
t3Home: Flag.string("home-dir").pipe(
Flag.withDescription(
- "Explicit T3 Code data directory; runtime state is stored under userdata (equivalent to T3CODE_HOME).",
+ "Explicit T3 Code data directory; runtime state is stored under userdata (equivalent to T3CODE_HOME). Inside a git worktree this defaults to that worktree's own .t3 so dev state stays off the shared home.",
),
- Flag.withFallbackConfig(optionalStringConfig("T3CODE_HOME")),
+ Flag.optional,
+ Flag.map(Option.getOrUndefined),
),
browser: Flag.boolean("browser").pipe(
Flag.withDescription("Open a browser automatically (disabled by default for web dev)."),