@b4run/workspace

中文文档
0.15.8发布于 2026/10/31MITTypeScriptES Module构建来源已验证近 30 天下载 5067

Filesystem and shell workspace contracts and tools for B4.run agent applications.

原始使用文档
文档目录10 节
  1. Install
  2. Example
  3. Runtime and stability
  4. Capturing initial source
  5. Inspecting a text workspace
  6. Reading another thread's workspace
  7. Supported surfaces
  8. Related
  9. Maturity and support
  10. License

@b4run/workspace

Filesystem and shell backend contracts for B4.run agent workspaces.

Use this when: You are supplying filesystem or shell backends, middleware, or workspace tools to a B4.run application.

Install

pnpm add @b4run/workspace

Example

import { compose, type FilesystemBackend, withFilesystemLogging } from "@b4run/workspace"
import { localFilesystem } from "@b4run/workspace/node"

const base: FilesystemBackend = localFilesystem({ maxFileBytes: 512 * 1024 })
const filesystem = compose(withFilesystemLogging())(base)

Runtime and stability

Capturing initial source

The Node entry point provides captureWorkspaceSource, createSourceBundle, verifySourceBundle, and readSourceFile for immutable source bundles.

import { captureWorkspaceSource, readSourceFile } from "@b4run/workspace/node"

const source = await captureWorkspaceSource(appRoot, {
  directory: "fixtures/demo",
  include: ["index.ts", "package.json"],
  files: [{ path: "TASK.md", text: "Repair the failing example.\n" }],
})
const original = readSourceFile(source, "index.ts")

Paths resolve relative to appRoot. include is the exact file inventory; unexpected files cause capture to fail. excludeDirectories can explicitly omit existing directory subtrees, such as installed dependencies. Additional entries can declare file paths relative to appRoot instead of inline text.

Capture preserves binary bytes and executable flags, rejects symlinks and ambiguous paths, and returns a verified digest with immutable content. Limits are 10,000 distinct inspected filesystem paths (including the app root and ancestor directories), 16 MiB per file, and 64 MiB total file content. Source paths use a portable ASCII subset with consistent directory casing.

Capture reads a trusted application tree and rejects detected changes during reading; it is not an atomic filesystem snapshot. It does not create a sandbox, register runtime configuration, or provide workspace lifecycle recovery.

Inspecting a text workspace

inspectWorkspace accepts the permission-bound ctx.fs author handle, a SandboxHandle, or any WorkspaceReadSource — including the SandboxWorkspaceReader a provider hands back from openWorkspaceReader. All use the same bounded traversal and require leaf metadata (stat / lstat) and raw binary reads; missing support fails closed.

import { inspectWorkspace } from "@b4run/workspace"

const inventory = await inspectWorkspace(ctx.fs, {
  signal: ctx.signal,
  excludeRootDirectories: [".git"],
  expectedRootSymlinks: { node_modules: "/opt/project/node_modules" },
})
// inventory.files: relative path → exact UTF-8 text (including a leading BOM)
// inventory.symlinks: validated root name → exact readlink target

Defaults are 10,000 entries, 2 MiB per file, and 16 MiB total file bytes; maxEntries, maxFileBytes, and maxTotalBytes accept nonnegative safe integers. Directories and omitted root entries count toward the entry limit; the workspace root itself does not. Limits check both reported sizes and actual returned bytes. The result also includes entries and totalBytes. File and link records have null prototypes, so filenames such as __proto__ remain ordinary keys.

Excluded root names may be absent, but must be directories when present. Expected root links are required and must match their exact, unnormalized targets. Their targets are never traversed. Nested entries receive no root exclusions. All other symlinks, non-file/non-directory entries, executable files, invalid UTF-8 and NUL-containing binary data are rejected. Leaf names cannot traverse directories. UTF-8 text without NUL is accepted; this is not a file-format classifier.

Reading another thread's workspace

withWorkspaceReader(provider, { threadId, signal }, operation) opens a provider's optional read-only view of one thread's workspace storage, hands it to operation, and closes it on both the success and failure paths. The reader carries no write method and no command backend, and reading does not disturb that thread's live sandbox. openWorkspaceReader is optional on SandboxProvider; a provider that omits it makes withWorkspaceReader raise rather than silently return nothing.

This is a host-side API for an already-trusted caller. It is not an authorization boundary, not a tool an agent can call, and not an HTTP surface.

Pass a signal to check cancellation around every filesystem call; sandbox backends also receive it in their BackendContext. Author handles retain their existing permission policy and cancellation behavior. The helper cannot interrupt an author operation already in flight. It uses no shell and no host filesystem APIs.

Inspection is not an atomic snapshot or a new isolation boundary. Metadata, listing, and reads use separate backend calls and can race with concurrent writers; quiesce writers or revalidate before acting. Exact links validate link identity, not the contents of their external targets. Backend reads must honor the supplied byte cap to bound allocation before returning data.

Supported surfaces

  • @b4run/workspace is an edge-safe, supported application surface.
  • @b4run/workspace/node is a node-only, supported application surface.

Workspace backends define where operations run; they are not an isolation boundary by themselves. Use a sandbox provider when untrusted execution must leave the host process.

Maturity and support

B4.run is pre-1.0, and its public surface can change. All publishable B4.run packages release together as a fixed group; review the @b4run/workspace changelog and upgrading guide before upgrading. For support, use GitHub Discussions; report defects in GitHub Issues.

License

MIT. See the repository license.

版本与发布记录

@b4run/workspace 共发布 23 个版本,首次发布于 2026/09/09;过去 12 个月发布了 23 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.15.8latest2026/10/317 天前
0.15.72026/10/2810 天前
0.15.52026/10/2712 天前
0.15.42026/10/2612 天前
0.15.22026/10/2514 天前
0.15.12026/10/2414 天前

关于 @b4run/workspace 的常见问题

@b4run/workspace 是什么?

Filesystem and shell workspace contracts and tools for B4.run agent applications.

如何安装 @b4run/workspace?

在项目目录运行 npm install @b4run/workspace,或使用 pnpm add / yarn add / bun add。

@b4run/workspace 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@b4run/workspace 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@b4run/workspace 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@b4run/workspace 可以商用吗?

@b4run/workspace 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@b4run/workspace 的下载量有多少?

根据 npm 官方下载统计,@b4run/workspace 在 2026/10/08 – 2026/11/06 这 30 天内被下载 5,067 次,日均约 169 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@b4run/workspace 还在维护吗?

最新版本 0.15.8 发布于 2026/10/31(7 天前);至今共发布 23 个版本,其中过去 12 个月发布了 23 个;首次发布于 2026/09/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。