diff --git a/packages/layerfs/README.md b/packages/layerfs/README.md index 429805b..6ec2b11 100644 --- a/packages/layerfs/README.md +++ b/packages/layerfs/README.md @@ -47,8 +47,10 @@ management — if it matters, it belongs in the workspace. - **sim** — a local content-addressed store implementing the LayerFS model: immutable blobs named by SHA-256, branches as manifest pointers, checkpoints as deduplicated manifests, under - `MAGPIE_LAYERFS_HOME` (default `~/.magpie-layerfs`). It runs - anywhere, which is what the tests and the benchmark below use. + `MAGPIE_LAYERFS_HOME` (default `~/.layerfs-tools`; a + `~/.magpie-layerfs` from 0.7.2 or earlier is moved there once, with a + link left at the old path). It runs anywhere, which is what the tests + and the benchmark below use. **This package is sim-only.** The native backend — the real LayerFS through its JavaScript SDK — is developed in the standalone repo, diff --git a/packages/layerfs/layerfs.mjs b/packages/layerfs/layerfs.mjs index b7b9a12..19ffde8 100644 --- a/packages/layerfs/layerfs.mjs +++ b/packages/layerfs/layerfs.mjs @@ -20,12 +20,13 @@ // Environment: // MAGPIE_LAYERFS_BACKEND auto (default) | sim — "native" is an // explicit error in this package (above) -// MAGPIE_LAYERFS_HOME state root (default ~/.magpie-layerfs) +// MAGPIE_LAYERFS_HOME state root (default ~/.layerfs-tools; see +// defaultRoot below) import { spawn } from "node:child_process" import { createHash } from "node:crypto" import { - chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, - realpathSync, statSync, writeFileSync, + chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, + realpathSync, renameSync, statSync, symlinkSync, writeFileSync, } from "node:fs" import { homedir } from "node:os" import { dirname, join, parse, resolve, sep } from "node:path" @@ -321,13 +322,43 @@ function runShell(command, dir, timeoutMs) { }) } +// ---- the default state root ------------------------------------------------------ +// The state root when MAGPIE_LAYERFS_HOME is unset. provision, resolve +// and exec answer workspace paths under it, and a tool result goes to +// the vendor in the agent's next request, so the default names neither +// the app nor the host (#360). A root made by 0.7.2 or earlier +// (~/.magpie-layerfs) is moved once to the new name, and a link is left +// at the old path so a session still holding an old workspace path keeps +// working. A legacy root that is itself a link (the user pointed it +// somewhere) is the user's own setup and is used where it is; so is a +// legacy root the move fails on. +export const DEFAULT_ROOT = ".layerfs-tools" +export const LEGACY_ROOT = ".magpie-layerfs" +export function defaultRoot(home = homedir()) { + const root = join(home, DEFAULT_ROOT) + const legacy = join(home, LEGACY_ROOT) + if (existsSync(root)) return root + let st + try { st = lstatSync(legacy) } catch { return root } + if (st.isSymbolicLink()) return legacy + if (!st.isDirectory()) return root + try { + renameSync(legacy, root) + } catch { + // another host moved it first, or the move is refused: use what is there + return existsSync(root) ? root : legacy + } + try { symlinkSync(root, legacy, process.platform === "win32" ? "junction" : "dir") } catch { /* the new paths still work */ } + return root +} + // ---- sim backend --------------------------------------------------------------- export class SimBackend { kind = "sim" constructor(root) { - this.root = root || process.env.MAGPIE_LAYERFS_HOME || join(homedir(), ".magpie-layerfs") + this.root = root || process.env.MAGPIE_LAYERFS_HOME || defaultRoot() this.objectsDir = join(this.root, "store", "objects") this.projectsDir = join(this.root, "projects") this.workspacesDir = join(this.root, "workspaces") diff --git a/packages/layerfs/layerfs.test.mjs b/packages/layerfs/layerfs.test.mjs index b908e39..74a7428 100644 --- a/packages/layerfs/layerfs.test.mjs +++ b/packages/layerfs/layerfs.test.mjs @@ -1,14 +1,15 @@ import { afterAll, expect, test } from "bun:test" import { createHash } from "node:crypto" import { - chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, - symlinkSync, writeFileSync, + chmodSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, + rmSync, statSync, symlinkSync, writeFileSync, } from "node:fs" import { homedir, tmpdir } from "node:os" import { basename, dirname, join } from "node:path" import { LayerfsTools } from "./index.mjs" import { - IMPORT_MAX_FILES, SimBackend, checkSourceDir, createBackend, scanImportTree, + DEFAULT_ROOT, IMPORT_MAX_FILES, LEGACY_ROOT, SimBackend, checkSourceDir, createBackend, + defaultRoot, scanImportTree, } from "./layerfs.mjs" const sha = (s) => createHash("sha256").update(s).digest("hex") @@ -527,3 +528,47 @@ test("r2 item 7: storedBytes grows by the commit record's size, not just the cha // pre-fix the growth was 1 (the blob alone); the manifest record is counted now expect(after.storedBytes - before.storedBytes).toBe(recordBytes + 1) }) + +// #360: workspace paths go upstream in tool results, so the default root +// names neither the app nor the host, and an existing ~/.magpie-layerfs +// is moved once with a link left behind. +test("#360: a fresh home gets a default root that does not name magpie", () => { + const home = mkTmp("lfs-home-") + const root = defaultRoot(home) + expect(root).toBe(join(home, DEFAULT_ROOT)) + expect(/magpie/i.test(DEFAULT_ROOT)).toBe(false) + expect(existsSync(join(home, LEGACY_ROOT))).toBe(false) // nothing made at the old name +}) + +test("#360: a 0.7.2 root is moved once, its workspaces answer the new path, and the old path still resolves", async () => { + const home = mkTmp("lfs-home-") + const legacy = join(home, LEGACY_ROOT) + const old = new SimBackend(legacy) // what 0.7.2 made at the old default + const ws = await old.provision("website", "main", fixture()) + writeFileSync(join(ws.path, "kept.txt"), "work in progress\n") + + const root = defaultRoot(home) + expect(root).toBe(join(home, DEFAULT_ROOT)) + expect(lstatSync(legacy).isSymbolicLink()).toBe(true) + expect(realpathSync(legacy)).toBe(realpathSync(root)) + expect(readFileSync(join(ws.path, "kept.txt"), "utf8")).toBe("work in progress\n") // old path, via the link + + const b = new SimBackend(root) + const again = await b.provision("website", "main") + expect(again.reused).toBe(true) + expect(again.path).toBe(join(root, "workspaces", "website", "main")) + expect(readFileSync(join(again.path, "kept.txt"), "utf8")).toBe("work in progress\n") + const cp = await b.checkpoint("website", "main", "after the move") + expect(cp.added).toBe(1) + + expect(defaultRoot(home)).toBe(root) // the second call moves nothing + expect(lstatSync(legacy).isSymbolicLink()).toBe(true) +}) + +test("#360: a legacy root the user made a link is used where it is", () => { + const home = mkTmp("lfs-home-") + const elsewhere = mkTmp("lfs-elsewhere-") + symlinkSync(elsewhere, join(home, LEGACY_ROOT), "dir") + expect(defaultRoot(home)).toBe(join(home, LEGACY_ROOT)) + expect(existsSync(join(home, DEFAULT_ROOT))).toBe(false) +}) diff --git a/packages/layerfs/package.json b/packages/layerfs/package.json index 5d34b53..a7b2011 100644 --- a/packages/layerfs/package.json +++ b/packages/layerfs/package.json @@ -45,5 +45,5 @@ "directory": "packages/layerfs" }, "type": "module", - "version": "0.7.2" + "version": "0.7.3" }