/** * Loki-kept code (`loki://code/`): a new project's place for the code * with no GitHub repository behind it. Run: npx tsx scripts/test/loki-kept-code.ts * * Added 2026-11-19, when the GitHub organisation stopped accepting new * repositories (HTTP 422) and every "Make it happen" stopped at "Making a * place for the code". The contract under test: * - the URL is recognised, strictly (no path tricks), and counts as * materializable, so dispatch routes the project to a cloud runner; * - the runner makes the private bare repository on first use, clones it, * gives it one commit on main, and a second materialization (another run, * another checkout) sees the same history. */ import { execFileSync } from "child_process"; import fs from "fs"; import os from "os"; import path from "path"; for (const k of ["GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_COMMON_DIR"]) { delete process.env[k]; } const SANDBOX = fs.mkdtempSync(path.join(os.tmpdir(), "loki-kept-code-")); 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; let failed = 0; function check(name: string, fn: () => void) { try { fn(); console.log(` ✓ ${name}`); } catch (e) { failed++; console.log(` ✗ ${name}: ${e instanceof Error ? e.message : String(e)}`); } } function assert(cond: boolean, msg: string) { if (!cond) throw new Error(msg); } const git = (dir: string, ...args: string[]) => execFileSync("git", ["-C", dir, ...args], { encoding: "utf8" }).trim(); async function main() { const { lokiCodeUrl, parseLokiCodeUrl, isCloneableGitUrl, normalizeGitHubCloneUrl } = await import("@/lib/git-url"); const { coldStartWorkspaceDir } = await import("@/lib/execution-access"); const { ensureBoxWorkspace, lokiCodeRoot } = await import("@/lib/agent-execution/box-workspace"); check("the URL round-trips and is materializable", () => { const url = lokiCodeUrl("hendlar"); assert(url === "loki://code/hendlar", url); assert(parseLokiCodeUrl(url) === "hendlar", "parse"); assert(isCloneableGitUrl(url), "cloneable"); assert(normalizeGitHubCloneUrl(url) === null, "it is not a GitHub URL"); assert( coldStartWorkspaceDir("Hendlar", url) === path.join(DEV_ROOT, "hendlar"), "cold start dir", ); }); check("anything else is not Loki-kept code", () => { for (const bad of [ "loki://code/../etc", "loki://code/a/b", "loki://code/", "loki://code/Hendlar", "loki://other/hendlar", "https://github.com/bitbaum/hendlar", "", ]) { assert(parseLokiCodeUrl(bad) === null, `accepted ${JSON.stringify(bad)}`); } assert(parseLokiCodeUrl(null) === null, "null"); }); check("GitHub URLs are still cloneable", () => { assert(isCloneableGitUrl("https://github.com/bitbaum/heidi"), "https"); assert(isCloneableGitUrl("git@github.com:bitbaum/heidi.git"), "ssh"); assert(!isCloneableGitUrl("not-a-url"), "garbage"); }); const source = { gitUrl: lokiCodeUrl("hendlar"), token: null }; // First run makes the private repository; the second materializes another // checkout of it (a parallel run's tab), which must not start it again. const mainDir = await ensureBoxWorkspace("Hendlar", "", source); const dir = await ensureBoxWorkspace("Hendlar~probe", "", source); const bare = path.join(lokiCodeRoot(), "hendlar.git"); check("the repository lives next to the dev root, not on GitHub", () => { assert(lokiCodeRoot() === path.join(SANDBOX, "loki-code"), lokiCodeRoot()); assert(fs.existsSync(path.join(bare, "HEAD")), "bare repo missing"); assert(git(bare, "rev-parse", "--is-bare-repository") === "true", "not bare"); }); check("the checkout's origin is the private repository, on main, with a commit", () => { assert(mainDir === path.join(DEV_ROOT, "hendlar"), `dir ${mainDir}`); assert(fs.existsSync(path.join(mainDir, ".git")), "no checkout"); assert(git(mainDir, "remote", "get-url", "origin") === bare, "origin"); assert(git(mainDir, "rev-parse", "--abbrev-ref", "HEAD") === "main", "branch"); assert(git(bare, "rev-list", "--count", "main") === "1", "one commit in the bare repo"); assert( git(mainDir, "symbolic-ref", "--short", "refs/remotes/origin/HEAD") === "origin/main", "origin/HEAD", ); }); check("a second checkout sees the same history (no second start commit)", () => { assert(dir === path.join(DEV_ROOT, "hendlar-probe"), `dir ${dir}`); assert(git(dir, "rev-parse", "HEAD") === git(mainDir, "rev-parse", "HEAD"), "same commit"); assert(git(bare, "rev-list", "--count", "main") === "1", "still one commit"); }); check("work pushed from a checkout lands in the private repository", () => { fs.writeFileSync(path.join(mainDir, "README.md"), "# Hendlar\n"); execFileSync( "git", [ "-c", "user.name=t", "-c", "user.email=t@t", "-C", mainDir, "commit", "-qam", "x", "--allow-empty", ], { stdio: "ignore" }, ); git(mainDir, "add", "README.md"); execFileSync( "git", ["-c", "user.name=t", "-c", "user.email=t@t", "-C", mainDir, "commit", "-qm", "readme"], { stdio: "ignore" }, ); git(mainDir, "push", "-q", "origin", "main"); assert(git(bare, "rev-list", "--count", "main") === "3", "pushed"); }); fs.rmSync(SANDBOX, { recursive: true, force: true }); console.log(failed ? `\n${failed} failed` : "\nall passed"); process.exit(failed ? 1 : 0); } main().catch((e) => { console.error(e); process.exit(1); });