layerfs 0.7.3: workspace paths sit under ~/.layerfs-tools, so a tool result no longer names magpie (#360)

provision, resolve and exec answer the workspace's path, and a tool
result goes to the vendor in the agent's next request. With the default
root ~/.magpie-layerfs every provision named magpie upstream.

The default root is now ~/.layerfs-tools. A ~/.magpie-layerfs made by
0.7.2 or earlier is renamed to it once, at the first backend made, 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's own setup) is used where it is, as is one the rename is refused
on. MAGPIE_LAYERFS_HOME set by the user is unchanged. A second host
racing the rename finds the new root and uses it.

Run, in a sandbox HOME:
- bun test packages/layerfs: 53 pass, 0 fail. bun scripts/check.mjs
  layerfs: ok.
- The three #360 tests against the old default (defaultRoot answering
  ~/.magpie-layerfs): 2 of them red, the third (a user's link) is
  unchanged behaviour.
- Through magpie's real plugin host (origin/main 4dfd88206,
  plugin.Add of this package + plugin.RunTool "provision" in a
  testenv sandbox HOME): a fresh home answers
  ~/.layerfs-tools/workspaces/demo/main and makes nothing at the old
  name; a home with a ~/.magpie-layerfs holding a marker answers the
  new path, the marker is under ~/.layerfs-tools, and ~/.magpie-layerfs
  is a symlink.

Not tried: Windows (the link is a junction there), and moving a root
while an agent's exec runs in it.
This commit is contained in:
yetone
2026-10-25 14:26:30 +08:00
parent c02957a40b
commit d43b228463
4 changed files with 88 additions and 10 deletions
+4 -2
View File
@@ -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,
+35 -4
View File
@@ -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")
+48 -3
View File
@@ -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)
})
+1 -1
View File
@@ -45,5 +45,5 @@
"directory": "packages/layerfs"
},
"type": "module",
"version": "0.7.2"
"version": "0.7.3"
}