Files
loki/scripts/test/dispatch-launch-dir.ts
CatoandClaude 1be367df36 fix(builder): a dispatch starts in the builder's own checkout, never the laptop path (#1638)
* fix(builder): a dispatch starts in the builder's own checkout, never the laptop path

A message to a busy project gets a parallel lane, tab <project>~<run>. On the
cloud builder the checkout was looked up under that tab name: no such project,
nothing to clone, and the stored laptop directory went on to the sandbox
("Sandbox cwd does not exist: /home/g/dev/orangecat", 2026-11-12). The run
died before the prompt arrived.

- dispatchLaunchDir: the project's checkout on this builder first, by project
  name, then the run's worktree of it. A lane without its own worktree, or a
  project the builder can neither find nor clone, is refused with the reason.
- launchAgentPty no longer hands on a directory that is not on the builder,
  and asks for the project's repository and token, not the lane's.
- /api/control/workspace-source answers for a lane tab as for its project.
- scripts/test/dispatch-launch-dir.ts pins it against the real checkout and
  worktree steps.

* chore(runner): 0.8.65 with its release note

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-12 05:37:08 +01:00

154 lines
6.4 KiB
TypeScript

/**
* Where a dispatched run starts on the builder that claimed it.
*
* Production, 2026-11-12: a message to orangecat while a run was open got a
* parallel lane, tab `orangecat~ef607eb7`, on the cloud builder. The project's
* stored directory is the laptop's, `/home/g/dev/orangecat`. The runner looked
* for the box checkout under the TAB name, found no such project, and handed
* the laptop path to the sandbox: "Sandbox cwd does not exist:
* /home/g/dev/orangecat". The run died before the prompt arrived.
*
* These run the real pieces against a throwaway dev root: the box checkout
* step (box-workspace.ts) and the worktree step (worktree-workspace.ts), in the
* order dispatchLaunchDir fixes.
*
* Run: npx tsx scripts/test/dispatch-launch-dir.ts
*/
import { execFileSync } from "child_process";
import fs from "fs";
import os from "os";
import path from "path";
const SANDBOX = fs.mkdtempSync(path.join(os.tmpdir(), "loki-dispatch-dir-"));
const DEV_ROOT = path.join(SANDBOX, "dev");
// Read at module load by the pieces under test, so set before they are imported.
process.env.HOME = SANDBOX;
process.env.LOKI_BOX_DEV_ROOT = DEV_ROOT;
process.env.LOKI_WORKTREES_ROOT = path.join(SANDBOX, "worktrees");
/** The path the project was registered with, on a machine that is not this one. */
const LAPTOP_DIR = "/home/g/dev/orangecat";
const RUN = "ef607eb7-0000-4000-8000-000000000000";
let failures = 0;
async function check(name: string, fn: () => void | Promise<void>) {
try {
await fn();
console.log(` ✓ ${name}`);
} catch (err) {
failures++;
console.log(` ✗ ${name}`);
console.log(` ${err instanceof Error ? err.message : String(err)}`);
}
}
function assert(cond: boolean, msg: string) {
if (!cond) throw new Error(msg);
}
function git(dir: string, args: string[]): string {
return execFileSync("git", ["-C", dir, ...args], { encoding: "utf8" }).trim();
}
function makeCheckout(name: string): string {
const dir = path.join(DEV_ROOT, name);
fs.mkdirSync(dir, { recursive: true });
git(dir, ["init", "-q", "-b", "main"]);
git(dir, ["config", "core.hooksPath", "/dev/null"]);
git(dir, ["config", "user.email", "test@test"]);
git(dir, ["config", "user.name", "test"]);
fs.writeFileSync(path.join(dir, "app.ts"), "export const v = 1\n");
git(dir, ["add", "-A"]);
git(dir, ["commit", "-q", "-m", "init"]);
return dir;
}
async function main() {
assert(!fs.existsSync(LAPTOP_DIR), `${LAPTOP_DIR} exists here; this test needs it absent`);
const { ensureBoxWorkspace } = await import("@/lib/agent-execution/box-workspace");
const { ensureWorktreeWorkspace } = await import("@/lib/agent-execution/worktree-workspace");
const { dispatchLaunchDir } = await import("../../desktop/src/main/lane-workspace");
const seenProjects: string[] = [];
/** What the cloud builder does: its own checkout, by project name. */
const boxDeps = (worktreeEveryRun = false) => ({
baseDir: (project: string, requested: string) => {
seenProjects.push(project);
return ensureBoxWorkspace(project, requested, null);
},
worktree: ensureWorktreeWorkspace,
worktreeEveryRun,
});
const checkout = makeCheckout("orangecat");
await check("a plain dispatch runs in the builder's checkout, not the laptop path", async () => {
const got = await dispatchLaunchDir("orangecat", LAPTOP_DIR, RUN, boxDeps());
assert(got.kind === "ready", `refused: ${got.kind === "refused" ? got.error : ""}`);
if (got.kind !== "ready") return;
assert(got.dir === checkout, `ran in ${got.dir}`);
assert(!got.isolated, "a plain run without the flag has no worktree");
});
await check(
"a parallel lane resolves the PROJECT's checkout, then its own worktree",
async () => {
seenProjects.length = 0;
const got = await dispatchLaunchDir("orangecat~ef607eb7", LAPTOP_DIR, RUN, boxDeps());
assert(got.kind === "ready", `refused: ${got.kind === "refused" ? got.error : ""}`);
if (got.kind !== "ready") return;
assert(seenProjects.join() === "orangecat", `looked up ${seenProjects.join()}`);
assert(got.primaryDir === checkout, `primary was ${got.primaryDir}`);
assert(got.isolated && got.dir !== checkout, "the lane shares the checkout");
assert(fs.existsSync(path.join(got.dir, "app.ts")), `${got.dir} is not a worktree of it`);
assert(!got.dir.startsWith("/home/g/"), `the laptop path reached the launch: ${got.dir}`);
assert(fs.existsSync(got.dir), "the launch directory does not exist");
},
);
await check("no laptop path survives for any tab shape", async () => {
for (const tab of ["orangecat", "orangecat~ef607eb7", "OrangeCat~0a1b2c3d"]) {
for (const flag of [false, true]) {
const got = await dispatchLaunchDir(tab, LAPTOP_DIR, RUN, boxDeps(flag));
const dir = got.kind === "ready" ? got.dir : "";
assert(dir !== LAPTOP_DIR, `${tab} (flag ${flag}) would launch in ${LAPTOP_DIR}`);
}
}
});
await check("a project the builder cannot find or clone is refused with the reason", async () => {
const got = await dispatchLaunchDir("ghost~ef607eb7", "/home/g/dev/ghost", RUN, boxDeps());
assert(got.kind === "refused", "launched without a checkout");
if (got.kind !== "refused") return;
assert(/no checkout of "ghost" on this builder/.test(got.error), got.error);
assert(/nothing was launched/.test(got.error), got.error);
});
await check(
"a lane that cannot get a worktree is refused, never run in the shared checkout",
async () => {
const got = await dispatchLaunchDir("orangecat~ef607eb7", LAPTOP_DIR, RUN, {
baseDir: async () => checkout,
worktree: (_tab, primary) => primary, // ensureWorktreeWorkspace's failure value
worktreeEveryRun: false,
});
assert(got.kind === "refused", "two agents would share one checkout");
},
);
await check("a laptop runner keeps the directory it was given", async () => {
const got = await dispatchLaunchDir("orangecat", checkout, RUN, {
baseDir: async (_p, requested) => requested,
worktree: ensureWorktreeWorkspace,
worktreeEveryRun: false,
});
assert(got.kind === "ready" && got.dir === checkout, "identity on the laptop");
});
fs.rmSync(SANDBOX, { recursive: true, force: true });
if (failures) {
console.log(`\ndispatch-launch-dir: ${failures} failed`);
process.exit(1);
}
console.log("\ndispatch-launch-dir: all passed");
}
void main();