mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 04:16:59 +01:00
* docs(handoff): checkpoint for building with Loki and with a Cat bot The objective, the two journeys that count as done, how production can be reached from an agent session, and the state when the work began. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi * docs(handoff): what the investigation found, and what it did not reach Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi * fix(builder): a task held for the runner's update waits minutes, not for ever, and says so While the cloud runner is behind the deploy the commands route hands it no work-starting command, so the idle moment its restart waits for comes. The hold had no end, and on 2026-11-12 the restart did not come: two runner releases landed 35 minutes apart, the drain read "5 agent(s) working" on every look for an hour, and the builder claimed screen peeks and nothing else (Sealed builds run 43165605499). Every task a person sent sat queued while the chat said a runner would claim it shortly. - runner-update-hold: a held command is handed over once it has waited five minutes, and the hold applies only within two hours of the newest runner release. Running on last week's runner is recoverable; a task that never starts is not. - claimNextPendingCommand takes the overdue rule. While holding, an empty at-once list claims nothing (it used to claim everything). - The person is told why and for how long: in the chat reply, in the /api/inject answer (builderUpdating, message) and as the run track's headline while the job is unclaimed. - loki-loop-check.yml: the end-to-end check of the live site on demand, with an optional real no-change task that must come back into its thread. loki-e2e-check.sh --report takes the project, the wait and the dispatch choice from its caller. - sealed-builds.yml mode=activity: read-only, what each agent process of the runner read and wrote over a minute, so the drain's "working" can be measured for sessions it cannot read a status for. - pre-push: the watchdog's sleep no longer holds the caller's output open for fifteen minutes after the push has landed. Server and box scripts only: no runner code, no runner version. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi * fix(runs): a build tells the truth after the agent finishes, and says when it is live A task sent from the chat ended at "done, pull request opened". What it said next was wrong in several ways, and the one thing a person waits for, that it went live, was said to nobody. - The "done" message reads the pull request the runner itself reported (payload.prUrl from closeRunShipped, read by nothing until now) and asks GitHub with the token that can read the repository. The person's own OAuth token is refused on the organisation's repositories, so a run that shipped could be posted as "No pull request or push was found". The reaper's evidence lookup asks the same way. - "Live" is read from the site: a merged change is live when the commit in the site's /.well-known/fleet-release.json is the merge or comes after it (feedback/served-release). Workflow-run labels are wrong on this fleet: a Deploy run is labelled with main's tip at its start, a batched merge has no Deploy run of its own, and OrangeCat's deploy is named CD. It only upgrades; with no receipt the workflow-run verdict stands. - A chat build is followed to live on the fifteen-minute cron that already follows feedback fixes (run-follow-notify): on the change to live, or to a failed deploy, one line in the thread the task came from and one push. GitHub and the release receipt only, no model. Old news is not announced. - A task that waits behind the project's earlier run says so in the chat, the /api/inject answer (queuedBehind, message) and the track, instead of "will claim it shortly". - Isolated runs ship through a pull request: worktreePromptNote told every lane and one-off session to push straight to main. - The close message no longer prints "Live: <address>" beside a pull request that has not merged. Server only, plus one prompt line the runner reads at its next start. No file under desktop/, no runner version. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi --------- Co-authored-by: Claude <noreply@anthropic.com>
253 lines
12 KiB
TypeScript
253 lines
12 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.LOKI_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,
|
|
isProjectWorktreeDir,
|
|
}: 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 ships through a pull request", () => {
|
|
const note = worktreePromptNote("run-9");
|
|
assert(note.includes(worktreeBranch("run-9")), "note must name the branch");
|
|
assert(note.includes("git push -u origin HEAD"), "note must say how to push the branch");
|
|
assert(note.includes("open a pull request"), "note must say the pull request is the way");
|
|
// 2026-11-12: it told every isolated run to push straight to main.
|
|
assert(!/HEAD:main|push origin main/.test(note), "note must never send work to main");
|
|
assert(note.includes("Never push to main"), "and says so");
|
|
});
|
|
|
|
// ── 5. Whose agent is it? ──────────────────────────────────────────────────
|
|
// 2026-10-02: an agent in ~/.loki/worktrees/<project>-2ba3ac62/<runId> read as
|
|
// "no agent running" everywhere, because only the primary checkout counted.
|
|
|
|
const root = path.join(SANDBOX, "worktrees");
|
|
check("an agent in the project's own dispatch worktree is the project's", () => {
|
|
assert(isProjectWorktreeDir(path.join(root, "testproj", "run-1"), TAB), "own tab folder");
|
|
assert(isProjectWorktreeDir(path.join(root, "testproj", "run-1", "src"), TAB), "subdirectory");
|
|
});
|
|
check("an agent in a derived run-tab worktree (<project>~<runId8>) is the project's", () => {
|
|
assert(
|
|
isProjectWorktreeDir(path.join(root, "testproj-2ba3ac62", "2ba3ac62-0df4-4acc"), TAB),
|
|
"derived run-tab folder",
|
|
);
|
|
});
|
|
check("a different project whose name starts the same is NOT this project's", () => {
|
|
assert(!isProjectWorktreeDir(path.join(root, "testproj-cli", "run-1"), TAB), "prefix sibling");
|
|
assert(!isProjectWorktreeDir(path.join(root, "testproj-2ba3ac6", "x"), TAB), "7 hex chars");
|
|
assert(!isProjectWorktreeDir(path.join(SANDBOX, "elsewhere", "testproj"), TAB), "outside root");
|
|
assert(!isProjectWorktreeDir(root, TAB), "the root itself");
|
|
});
|
|
|
|
// ── 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);
|
|
});
|