BlogDebuggingturno
The swapped arguments a cast hid, and a guard HOME that fails the run
In turno-cli 0.1.1, turno mcp sync passed its two arguments to busyProfiles in the wrong order behind a cast, so an open plain claude session never kept it from rewriting ~/.claude.json, and a test with a sandbox HOME pruned run records under the real one. turno-cli 0.2.0 fixed the call, and the test suite now runs under a throwaway HOME that fails the run if anything is written to it.
- Shipped in
- Author
- William Monroy
turno mcp sync writes the gateway entries into the config file of every profile. A file whose profile has an open session is skipped unless --force is given, and the command prints has open sessions; close them or use --force. In turno-cli 0.1.1 that check never counted a plain claude session, so ~/.claude.json could be rewritten while one was open.
The cause was one call with its two arguments in the wrong order, behind a cast that kept the compiler from seeing it. The same call made a test that ran in a sandbox HOME prune run records under the real one.
The call behind the cast
busyProfiles in packages/turno/src/core/sessions.ts takes the environment first and the config second. In 0.1.1, mcp sync loaded it through a dynamic import, cast the module to a function of unknown[], and passed the config first:
async function busySet(io: Io): Promise<Set<string>> {
try {
const mod = (await import("../../core/sessions.js")) as unknown as { busyProfiles?: (...a: unknown[]) => Set<string> | string[] };
const res = mod.busyProfiles?.(loadConfig(io.env), io.env) ?? [];
return new Set(res);
} catch {
return new Set();
}
}The cast erased the parameter types, so any arguments compiled. The optional call and the empty catch had the same effect at run time: a missing function or an error both read as "no open sessions".
What the swapped call did
With the config in the place of the environment, paths() found no HOME, TURNO_HUB or XDG variable on it and fell back to os.homedir(). On macOS and Linux that is the HOME of the process, as the Node.js docs say. In a normal run that is the right folder, by accident. In a test that passes a sandbox env with its own HOME, it is the real home.
With the environment in the place of the config, config.system was undefined. A session of the claude CLI with no launcher run record belongs to the system profile, the one plain claude is signed in to. With no system profile in sight, such a session got no profile, so a plain claude session never put the profile that owns ~/.claude.json in the busy set. Sessions started by a launcher still counted, through their run records.
liveSessions also ends by calling pruneRunRecords, which deletes the run records of processes that have ended. In 0.1.1, packages/turno/test/agents.test.ts ran turno mcp sync cx with a sandbox env. Through the swapped call, that cleanup ran on the run folder under the real home of whoever ran the tests.
The fix in 0.2.0
Commit 7e58586 replaced the whole helper. mcp.ts now imports liveSessions with a typed, static import, and busyState passes it the environment mcp sync was given and reports apart whether plain claude has a session:
async function busySet(io: Io): Promise<Set<string>> {
try {
const mod = (await import("../../core/sessions.js")) as unknown as { busyProfiles?: (...a: unknown[]) => Set<string> | string[] };
const res = mod.busyProfiles?.(loadConfig(io.env), io.env) ?? [];
return new Set(res);
} catch {
return new Set();
}
}
/**
* Who has a live session: profiles from launcher run records (or the system profile), and
* whether plain `claude` has one. Reads only the sandbox-able paths of `env`.
*/
function busyState(env: NodeJS.ProcessEnv, config: Config): { profiles: Set<string>; plain: boolean } {
const sessions = liveSessions(env, config);
return {
profiles: new Set(sessions.flatMap((s) => (s.profile ? [s.profile] : []))),
plain: sessions.some((s) => s.source === "system"),
};
}The turno-cli 0.2.0 changelog says mcp sync "no longer rewrites ~/.claude.json while a plain claude session is open (use --force)" and "reads open sessions only from its own HOME".
The test for it points process.env.HOME at a decoy folder that holds a dead run record. It then checks that ~/.claude.json in the sandbox is skipped, that the sandbox's dead record is pruned, and that the decoy's record is still there.
A later change, commit 55a2de1, also released in 0.2.0, removed the = process.env default from the core functions. In its words, "a missing argument is a type error instead of a silent read of the real HOME". busyProfiles now reads export function busyProfiles(env: NodeJS.ProcessEnv, config?: Config): Set<string>.
A HOME that fails the run
Each test already had its own throwaway HOME from sandbox(). That does not help when the code under test ignores the env it is given. Commit fb2fa3d, released in 0.2.0, added a Vitest global setup for the whole run, wired in packages/turno/vitest.config.ts. This is the setup as it reads in turno-cli 0.6.3. The TURNO_BROWSER_NOTIFY switch came later (commit 3a1482a, first released in turno-cli 0.6.2); the rest of what is shown came with fb2fa3d, apart from formatting.
export default function setup(): () => void {
const root = realpathSync(mkdtempSync(join(tmpdir(), "turno-guard-")));
const home = join(root, "home");
mkdirSync(home);
for (const key of Object.keys(process.env)) if (LIVE.test(key) && !KEEP.has(key)) delete process.env[key];
process.env.HOME = home;
// No notification of the system from a turno-browser host a test runs (`notify.ts`).
process.env.TURNO_BROWSER_NOTIFY = "off";
process.env.XDG_CONFIG_HOME = join(home, ".config");
process.env.XDG_STATE_HOME = join(home, ".local", "state");
process.env.XDG_DATA_HOME = join(home, ".local", "share");
process.env.XDG_CACHE_HOME = join(home, ".cache");
process.env.TURNO_NO_UPDATE_CHECK = "1";LIVE matches the variables of a live session (TURNO_, CLAUDE, CODEX_, COPILOT_, CURSOR_, ANTHROPIC_, XDG_; COPILOT_ came later, with commit a471bba), and KEEP spares the switches that select tests. Code that falls back to process.env or os.homedir() now lands in that folder, which should stay empty. When the run ends, it is checked:
// ...
return () => {
const found = guardFindings(home);
rmSync(root, { recursive: true, force: true });
if (found.length) {
const shown = found.slice(0, 20).join("\n ");
throw new Error(
`Tests wrote to HOME instead of their sandbox (test/helpers/README.md):\n ${shown}${found.length > 20 ? "\n ..." : ""}`,
);
}
};The same commit made the Claude Code, Codex and Cursor end-to-end tests build their env from the sandbox. Before, the Claude Code test ran the real claude binary with { ...process.env, CLAUDE_CONFIG_DIR: cfgDir, ... }, so it inherited the real HOME.
The same pull request fixed one such write. The message of commit fb2fa3d says installFonts looked fc-cache up on the env it was given but started it with the process env, so fontconfig wrote its cache under the real HOME, and on CI under the guard HOME. installFonts runs fc-cache only on Linux, by its platform check in packages/turno/src/ui/fonts.ts. The fix passes the same env to the child process. It shipped in 0.2.0 with no line of its own in the changelog.
What the guard does not catch
It catches writes, not reads. A call that reads the wrong HOME finds an empty folder under the guard and leaves nothing behind, so the run passes. The swapped call in mcp sync would have read nothing and pruned nothing there. In this package, only the decoy test covers reads, and only for that one command.
It looks only inside the guard HOME. A write to a path that does not come from HOME or the XDG variables is not seen.
It names paths, not tests. The check runs once, after the whole run, so a finding still has to be traced to the test that caused it.