Keep worktree dev state off the shared home

A dev server started in a worktree resolved its data directory to the
shared `~/.t3`. Worse, an ambient `T3CODE_HOME` counts as an *explicit*
base dir, which flips the state directory from `<base>/dev` to
`<base>/userdata` — the database the user's installed T3 Code is
actively running against. Feature work in a throwaway branch was
sharing a database with the user's real app, and its
`server-runtime.json` overwrote the live server's entry.

Worktrees now default to their own gitignored `.t3`, outranking the
ambient env var; `--home-dir` still wins, and the main checkout keeps
`~/.t3/dev`. `auth pairing url` applies the same default, so `dev:pair`
from a worktree can no longer mint a credential into the real database.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
Theo BrowneandClaude Opus 5 committed 2026-07-25 18:56:45 -07:00
1 parent 2682f8aafc
commit dcae11d6fb
6 files changed
+119 -6

No files matched your search

+46
View File
@@ -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 <home>/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(
+35 -4
View File
@@ -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<string | undefined, never, Path.Path> {
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<string, never, Path.Path> {
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 `<base>/dev` to `<base>/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)."),