Docs menu

SDK reference

JavaScript SDK

LayerFS ships three JavaScript packages, split by who calls them, on one native addon. Agents need only@layerfs/agent. A tool that runs agents, such as DSH, also uses @layerfs/runtime and@layerfs/backend.

Which package

PackageForWhat it does
@layerfs/agentAgents and their scriptsEverything an agent does on one project: Branches, files, Workspaces and Sandboxes.
@layerfs/runtimeIntegration hostsOne LayerFS installation: which projects exist, starting Init, the worker on macOS and Windows, cleanup.
@layerfs/backendIntegration hostsWhat a host shows in its own views: history, storage numbers, live Workspace state, background processes and daemon logs.

All three share one version and load the same Rust addon, layerfs.node, which opens the project's Store and talks to Sandbox daemons. LayerFS ships no UI and serves no HTTP: an integration draws its own views from these calls. For coding agents, the layerfs-js-sdk skill is the guide to @layerfs/agent.

import { LayerFS } from "@layerfs/agent";

const layerfs = await LayerFS.open("shop");   // settings come from the environment
try {
  await layerfs.branch.fork("cart");
  await layerfs.fs.write("cart", "/README.md", "# Shop\n", "readme");
  const own = await layerfs.sandbox.create();           // { sandbox: "1", image }
  try {
    const ws = await layerfs.workspace.mount(own.sandbox, "cart");
    try {
      const run = await layerfs.workspace.exec(ws, "timeout 110 npm test 2>&1", "step 1");
      await layerfs.workspace.commit(ws, `step 1: npm test (exit ${run.exitCode})`);
    } finally {
      await layerfs.workspace.unmount(ws);
    }
  } finally {
    await layerfs.sandbox.close(own.sandbox);
  }
} finally {
  await layerfs.close();
}

Two routes

  • Store calls (project, branch, fs, and the backend's store.*) open the project's Store file in your process and read, save and publish there. The file is global/store.sqlite in the project's Docker volume, named by LAYERFS_STORE_PATH.
  • Workspace calls (workspace.*, sandbox.*, and the backend's daemon.*) go to the daemon in a Sandbox container, through Docker. The daemon opens the same Store.
The SDK's two routesIn your process, fs, branch and project calls open the Store file directly and read, save and publish there; this works on Linux with the Store on a local ext4, xfs or btrfs filesystem, and fails Unsupported anywhere else. workspace calls go through Docker to the LayerFS daemon in a Sandbox, which mounts Workspaces and opens the same Store directly.your process@layerfs/agentLayerFS.open("shop")LAYERFS_STORE_PATHfs · branch · projectStore callsopened in this processLinux · local ext4/xfs/btrfsworkspace.*via DockerSANDBOXLayerFS daemon/workspaces/1, /2 …mount · exec · commit · unmountthe daemon opensthe same StoreStoreone SQLite fileshared volume!macOS, a network filesystem, another machine: Store calls failUnsupported. There is no fallback route.
Store calls run in your process; workspace calls go through Docker to a daemon that opens the same Store.

Where calls run

Every process that opens a Store must share one Linux kernel, with the Store on a local ext4, xfs or btrfs filesystem. Anywhere else, Store calls fail Unsupported, with no fallback.

PlatformStore callsWorkspace calls
LinuxIn your own processThe Sandbox daemon, through Docker Engine
macOS, through the workerThe LayerFS home's worker container, layerfs-worker, that runs the real SDK in LinuxThe Sandbox daemon, through Docker Desktop
Windows, through the workerThe LayerFS home's worker, a Node process in a WSL2 Linux distributionThe Sandbox daemon, through that distribution's Docker Engine

The worker route is open to any integration. @layerfs/runtimestarts the worker and hands it to @layerfs/agent in its own process; any other process, such as an agent's command, uses it when its LAYERFS_WORKER_ENDPOINT names the worker's endpoint file. An agent's script is the same on all three. See Platform and limits.

Install

The packages are folders of the LayerFS checkout's api/ folder (api/agent/sdk,api/backend, api/runtime); they aren't published to npm. They need Node 20 or newer. Set them up once in the checkout:

npm --prefix "$LAYERFS_REPO/api" install              # links the three packages to each other; downloads nothing
npm --prefix "$LAYERFS_REPO/api" run build:native     # the addon, api/binding/layerfs.node, on the host that loads it

Then link @layerfs/agent into the folder your scripts run in:

npm init -y >/dev/null && npm pkg set type=module
npm install --no-audit --no-fund "file:$LAYERFS_REPO/api/agent/sdk"   # the LayerFS checkout

An integration host links api/runtime and api/backend the same way. Thelayerfs command, one agent call per shell command, is the checkout's api/agent/cli/ folder: npm --prefix "$LAYERFS_REPO/api/agent/cli" install links it to @layerfs/agent. The repository'sapi/README.md covers builds and tests.

Coming from v1.0.0

v1.0.0 shipped one package, @layerfs/sdk, described in itsREADME. The groupsfs and branch, project.list_branches, and the Workspace calls other than mount and status take the same arguments and answer the same results. What changed:

v1.0.0Now
import … from "@layerfs/sdk"import … from "@layerfs/agent"
the sdk/js folderthe api/ folder: api/agent/sdk, api/agent/cli, api/backend, api/runtime, and the addon at api/binding/layerfs.node
new LayerFS(name, { storePath, storePolicy, … })await LayerFS.open(name), ready when it returns; new LayerFS() is refused
options on the clientthe environment only: LAYERFS_STORE_PATH, LAYERFS_STORE_POLICY, LAYERFS_DAEMON_HOST, LAYERFS_TRACE; an options object is refused InvalidInput
DSH_LAYERFS_* names as fallbacksnot read any more: LayerFS reads only LAYERFS_*, and an integration that can set only its own names maps them itself
layerfs.project.init(folder)await LayerFS.init(name, folder, timeout?), 300 s by default, answering an open client
workspace.mount(branch, { image }?), placed in a Sandbox for yousandbox.create({ image }?), then workspace.mount(sandbox, branch): the mount names its Sandbox, and there is no placement
operator.sandbox.list(); no way to stop a Sandboxproject.list_sandboxes, sandbox.list_workspaces, sandbox.close
workspace.status(ws)backend.daemon.workspace.status(ws) in @layerfs/backend
operator.* reads and processes (@layerfs/sdk/operator)@layerfs/backend (LayerFSBackend.of(layerfs)) and @layerfs/runtime; old trace names map through RENAMED_METHODS
Store calls on Linux onlyon macOS and Windows too, through the LayerFS home's worker (LAYERFS_WORKER_ENDPOINT), which @layerfs/runtime starts
a Node script for every callor the layerfs command, one call per command

Old trace files keep the names calls had when they ran; RENAMED_METHODS in @layerfs/agentmaps each old name to its new one.