mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 18:07:52 +01:00
* chore: add a formatter, matching the style this repo already writes singleQuote=false was chosen by counting this repo's own imports, not by fleet decree. The fleet is genuinely split and the two repos that already had a .prettierrc disagreed with each other, so there was no standard to restore. Quote style does not cross repo boundaries; having a gate does. Markdown is ignored for now — prettier rewraps prose, which would bury the real diff. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * style: format with prettier (1062 files) Mechanical. No behaviour change. This SHA is listed in .git-blame-ignore-revs so `git blame` skips it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * chore: teach git blame to skip the reformat Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * fix(lint): reattach two guards the reformat detached Both effects were one-liners, so a trailing eslint-disable-line covered the setState call: useEffect(() => { setSelected(new Set()); }, [q.length]); // eslint-disable-line ... prettier expanded the block. The comment stayed on the closing line while the call moved above it, so the guard protected nothing and the rule its author had deliberately silenced failed the build. This is the second shape of the same defect — hirnli had the eslint-disable-NEXT-line variant. Both come from the same fact: an eslint directive binds to a line number, so reformatting can silently unbind it, which makes 'mechanical, no behaviour change' false for any bulk reformat. * chore(desktop): bump the runner version the reformat requires desktop-release-drift enforces that anything built into Fleet Runner which differs from the last released tag must carry a version ahead of that tag — main read 0.8.15 and fleet-runner-v0.8.15 already shipped. A whole-repo reformat changes bytes under desktop/, so the gate fires. The alternative was adding desktop/ to .prettierignore, which would buy a quiet PR by carving a permanent hole in the formatter's coverage — the precise shape of silent drift this test was written to catch. Bumping is what the rule asks for and costs one patch release whose only content is whitespace. * chore: merge main and re-format A whole-repo reformat conflicts with any concurrent commit, and a conflicting PR runs no checks at all while reporting nothing. Conflicts resolved to main, which is correct because this branch changes formatting and nothing else. * chore: leave desktop/ out of this reformat desktop-release-drift is right to refuse this. Everything under desktop/ is built into Fleet Runner, so changing it means shipping: bump the version AND add a FLEET_RUNNER_RELEASES entry, whose stated audience is 'person who installed Fleet Runner and wants to know what changed'. For a whitespace-only build the honest answer is 'nothing', so that release should not exist — and I should not write release notes claiming otherwise. I had bumped to 0.8.16 to get the gate green. That was the wrong direction: publishing a build to three OS runners to keep a formatting PR quiet. Reverted. desktop/ is excluded in .prettierignore with the reasoning recorded there, to be formatted alongside the next real runner change, where the bump and the notes are true anyway. --------- Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
227 lines
10 KiB
TypeScript
227 lines
10 KiB
TypeScript
/**
|
|
* Regression net for worktree-per-agent isolation (worktree-workspace.ts).
|
|
*
|
|
* Locks the four behaviors the feature exists for:
|
|
* 1. A fresh dispatch gets its own worktree + branch; re-launch re-attaches.
|
|
* 2. THE INCIDENT (2026-07-17): `git add -A` + commit in the primary checkout
|
|
* can never swallow a worktree agent's files — and vice versa.
|
|
* 3. Cleanup never destroys work: uncommitted or unshared commits → kept;
|
|
* only provably-shared/empty worktrees are removed.
|
|
* 4. Any failure degrades to the primary dir (a launch is never broken by
|
|
* the isolation feature).
|
|
*
|
|
* DB-independent; uses real `git` against throwaway repos in a tmpdir.
|
|
* Run: npx tsx scripts/test/worktree-workspace.ts
|
|
*/
|
|
import fs from "fs";
|
|
import os from "os";
|
|
import path from "path";
|
|
import { execFileSync } from "child_process";
|
|
|
|
// SAFETY: when git runs hooks (husky pre-push), it exports GIT_DIR — absolute
|
|
// when pushing from a linked worktree. Our git child processes inherit it, and
|
|
// an absolute GIT_DIR overrides `git -C <sandbox>`, silently redirecting every
|
|
// "throwaway repo" operation at the REAL repository (stray test@test "init"
|
|
// commits on real branches, real worktrees pruned). Strip all repo-targeting
|
|
// git env before any git subprocess runs so the sandbox stays a sandbox.
|
|
for (const k of [
|
|
"GIT_DIR",
|
|
"GIT_WORK_TREE",
|
|
"GIT_INDEX_FILE",
|
|
"GIT_COMMON_DIR",
|
|
"GIT_OBJECT_DIRECTORY",
|
|
"GIT_PREFIX",
|
|
]) {
|
|
delete process.env[k];
|
|
}
|
|
|
|
// Point the module's worktree root into the sandbox BEFORE importing it
|
|
// (module-level const reads the env at import time → dynamic import below).
|
|
const SANDBOX = fs.mkdtempSync(path.join(os.tmpdir(), "fc-wt-test-"));
|
|
process.env.FLEETCROWN_WORKTREES_ROOT = path.join(SANDBOX, "worktrees");
|
|
|
|
// Loaded inside main() so the env override above lands first (CJS harness —
|
|
// no top-level await; a static import would hoist above the env write).
|
|
type WorktreeModule = typeof import("@/lib/agent-execution/worktree-workspace");
|
|
|
|
function assert(condition: boolean, message: string): void {
|
|
if (!condition) throw new Error(message);
|
|
}
|
|
|
|
let passed = 0;
|
|
const check = (label: string, fn: () => void) => {
|
|
fn();
|
|
passed += 1;
|
|
console.log(` ✓ ${label}`);
|
|
};
|
|
|
|
function git(dir: string, args: string[]): string {
|
|
return execFileSync("git", ["-C", dir, ...args], { encoding: "utf8" }).trim();
|
|
}
|
|
|
|
/** A throwaway "primary checkout" with one commit and a node_modules dir. */
|
|
function makePrimary(name: string): string {
|
|
const dir = path.join(SANDBOX, 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");
|
|
fs.mkdirSync(path.join(dir, "node_modules", "dep"), { recursive: true });
|
|
git(dir, ["add", "-A"]);
|
|
git(dir, ["commit", "-q", "-m", "init"]);
|
|
return dir;
|
|
}
|
|
|
|
const TAB = "TestProj";
|
|
|
|
async function main(): Promise<void> {
|
|
const {
|
|
ensureWorktreeWorkspace,
|
|
removeWorktreeIfClean,
|
|
pruneWorktrees,
|
|
worktreeBranch,
|
|
worktreePath,
|
|
worktreePromptNote,
|
|
}: WorktreeModule = await import("@/lib/agent-execution/worktree-workspace");
|
|
|
|
// ── 1. Creation, idempotency, fallback ─────────────────────────────────────
|
|
|
|
const primary = makePrimary("primary");
|
|
|
|
check("creates an isolated worktree on branch fc/<runId> from HEAD", () => {
|
|
const wt = ensureWorktreeWorkspace(TAB, primary, "run-1");
|
|
assert(wt !== primary, "should not fall back to primary");
|
|
assert(wt === worktreePath(TAB, "run-1"), `unexpected path: ${wt}`);
|
|
assert(git(wt, ["branch", "--show-current"]) === worktreeBranch("run-1"), "wrong branch");
|
|
assert(fs.existsSync(path.join(wt, "app.ts")), "checkout missing tracked file");
|
|
});
|
|
|
|
check("re-launch of the same run re-attaches to the existing worktree", () => {
|
|
const again = ensureWorktreeWorkspace(TAB, primary, "run-1");
|
|
assert(again === worktreePath(TAB, "run-1"), "should reuse, not fail or recreate");
|
|
});
|
|
|
|
check("links node_modules from the primary so the agent can build immediately", () => {
|
|
const wt = worktreePath(TAB, "run-1");
|
|
const link = path.join(wt, "node_modules");
|
|
assert(fs.lstatSync(link).isSymbolicLink(), "node_modules should be a symlink");
|
|
assert(fs.existsSync(path.join(link, "dep")), "symlink should resolve to primary's deps");
|
|
});
|
|
|
|
check("non-git primary dir degrades gracefully to the primary (launch never breaks)", () => {
|
|
const plain = path.join(SANDBOX, "not-a-repo");
|
|
fs.mkdirSync(plain, { recursive: true });
|
|
assert(ensureWorktreeWorkspace(TAB, plain, "run-x") === plain, "should fall back");
|
|
});
|
|
|
|
check("missing runId / dir degrades gracefully", () => {
|
|
assert(ensureWorktreeWorkspace(TAB, primary, "") === primary, "empty runId → primary");
|
|
const gone = path.join(SANDBOX, "gone");
|
|
assert(ensureWorktreeWorkspace(TAB, gone, "run-y") === gone, "missing dir → unchanged");
|
|
});
|
|
|
|
// ── 2. THE INCIDENT — mutual isolation under `git add -A` ──────────────────
|
|
|
|
check("`git add -A` + commit in the PRIMARY cannot swallow the worktree agent's files", () => {
|
|
const wt = worktreePath(TAB, "run-1");
|
|
// Agent writes in its worktree (uncommitted).
|
|
fs.writeFileSync(path.join(wt, "agent-work.ts"), "export const agent = true\n");
|
|
// Autopilot in the primary does the exact incident sequence.
|
|
fs.writeFileSync(path.join(primary, "autopilot.ts"), "export const auto = true\n");
|
|
git(primary, ["add", "-A"]);
|
|
git(primary, ["commit", "-q", "-m", "autopilot sweep"]);
|
|
const swallowed = git(primary, ["show", "--stat", "--name-only", "HEAD"]);
|
|
assert(!swallowed.includes("agent-work.ts"), "primary commit swallowed the agent's file");
|
|
// And the agent's file is still there, still uncommitted, in ITS tree only.
|
|
assert(fs.existsSync(path.join(wt, "agent-work.ts")), "agent file lost");
|
|
assert(
|
|
git(wt, ["status", "--porcelain"]).includes("agent-work.ts"),
|
|
"agent file not in its own status",
|
|
);
|
|
assert(!fs.existsSync(path.join(primary, "agent-work.ts")), "agent file leaked into primary");
|
|
});
|
|
|
|
check("primary HEAD stays on main — the worktree commit lands on the run branch only", () => {
|
|
const wt = worktreePath(TAB, "run-1");
|
|
git(wt, ["add", "-A"]);
|
|
git(wt, ["commit", "-q", "-m", "agent commit"]);
|
|
assert(git(primary, ["branch", "--show-current"]) === "main", "primary HEAD moved off main");
|
|
const mainFiles = git(primary, ["ls-tree", "--name-only", "main"]);
|
|
assert(!mainFiles.includes("agent-work.ts"), "agent commit reached main without a merge");
|
|
});
|
|
|
|
// ── 3. Cleanup never destroys work ─────────────────────────────────────────
|
|
|
|
check("worktree with commits unmerged anywhere is KEPT (unshared work)", () => {
|
|
assert(
|
|
removeWorktreeIfClean(TAB, primary, "run-1") === "kept-dirty",
|
|
"should keep unshared commits",
|
|
);
|
|
assert(fs.existsSync(worktreePath(TAB, "run-1")), "worktree deleted despite unshared work");
|
|
});
|
|
|
|
check("worktree with uncommitted changes is KEPT", () => {
|
|
const wt = ensureWorktreeWorkspace(TAB, primary, "run-2");
|
|
fs.writeFileSync(path.join(wt, "wip.ts"), "// wip\n");
|
|
assert(
|
|
removeWorktreeIfClean(TAB, primary, "run-2") === "kept-dirty",
|
|
"should keep uncommitted work",
|
|
);
|
|
});
|
|
|
|
check("clean worktree (no new commits) is removed, branch deleted", () => {
|
|
ensureWorktreeWorkspace(TAB, primary, "run-3");
|
|
assert(
|
|
removeWorktreeIfClean(TAB, primary, "run-3") === "removed",
|
|
"clean tree should be removed",
|
|
);
|
|
assert(!fs.existsSync(worktreePath(TAB, "run-3")), "worktree dir still present");
|
|
const branches = git(primary, ["branch", "--list", worktreeBranch("run-3")]);
|
|
assert(branches === "", "run branch should be deleted");
|
|
});
|
|
|
|
check("worktree whose commits were merged into main is removed (work is shared)", () => {
|
|
// Merge run-1's branch into main, then cleanup may remove it.
|
|
git(primary, ["merge", "-q", "--no-ff", "-m", "land agent work", worktreeBranch("run-1")]);
|
|
assert(
|
|
removeWorktreeIfClean(TAB, primary, "run-1") === "removed",
|
|
"merged work should allow removal",
|
|
);
|
|
});
|
|
|
|
check("pruneWorktrees sweeps clean ones, keeps dirty ones, reports count", () => {
|
|
ensureWorktreeWorkspace(TAB, primary, "run-4"); // clean
|
|
// run-2 is still dirty from above
|
|
const removed = pruneWorktrees(TAB, primary);
|
|
assert(removed === 1, `expected 1 removed, got ${removed}`);
|
|
assert(fs.existsSync(worktreePath(TAB, "run-2")), "dirty worktree must survive prune");
|
|
assert(!fs.existsSync(worktreePath(TAB, "run-4")), "clean worktree should be pruned");
|
|
});
|
|
|
|
check("removeWorktreeIfClean on a nonexistent worktree reports absent", () => {
|
|
assert(removeWorktreeIfClean(TAB, primary, "never-existed") === "absent", "should be absent");
|
|
});
|
|
|
|
// ── 4. The prompt note ─────────────────────────────────────────────────────
|
|
|
|
check("worktreePromptNote names the branch and the land-on-main path", () => {
|
|
const note = worktreePromptNote("run-9");
|
|
assert(note.includes(worktreeBranch("run-9")), "note must name the branch");
|
|
assert(note.includes("git push origin HEAD:main"), "note must say how to land on main");
|
|
assert(note.includes("pull --rebase"), "note must include the rebase-first step");
|
|
});
|
|
|
|
// ── Done ───────────────────────────────────────────────────────────────────
|
|
|
|
fs.rmSync(SANDBOX, { recursive: true, force: true });
|
|
console.log(`\n${passed}/${passed} passed`);
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error(e);
|
|
fs.rmSync(SANDBOX, { recursive: true, force: true });
|
|
process.exit(1);
|
|
});
|