@soiljs/storage-local
v0.5.0
Published
SQLite WAL runtime ledger with durable local content-addressed artifacts.
Readme
@soiljs/storage-local
Foundation controls persist in the existing JSON entities without a schema migration: sequential policy, prepared/validated input, tool model projection, model-call reservations, stop diagnostics, and terminating-tool completion intent. Missing fields in older records preserve parallel execution and original output projection. T4 atomically records a terminating result and skips unstarted later siblings; the SIGKILL suite includes a real crash after T4 and before T7 and verifies no model/tool re-execution. Cancellation committed before T4 wins; cancellation after the completion intent does not replace the successful result. Sequential waits block later preparation and dispatch under the same fenced SQLite transactions, including after reopening the database.
让 Agent 的执行进度在单机重启后保留:SQLite WAL 保存结构化账本与 Outbox,本地文件保存不可变 SHA-256 内容寻址产物。它是仅依赖 @soiljs/spi 的 Layer 1 存储包,无第三方数据库依赖,不执行模型或工具;适合本地应用与单机服务,要求 Node.js ≥22.19。
安装与入口
pnpm add @soiljs/storage-local
# 与 Runtime 组合时安装
pnpm add @soiljs/agent @soiljs/models| 导出入口 | 用途 |
| --- | --- |
| @soiljs/storage-local | localStorage 与 LocalStorageConfig |
最小可运行示例
此例完全离线,使用模型夹具;同一目录再次打开时保留账本与产物。夹具本身的脚本游标不承担跨进程恢复,恢复业务应重新注册兼容的模型/Agent/工具定义。
import { defineAgent, openRuntime } from "@soiljs/agent";
import { createFixtureModel } from "@soiljs/models/recording";
import { localStorage } from "@soiljs/storage-local";
const assistant = defineAgent({
id: "order-assistant",
version: "1",
model: createFixtureModel({
protocolId: "openai/chat",
model: "offline",
responses: ["订单处理完成"],
}),
instructions: "简洁回答。",
});
await using runtime = await openRuntime({
storage: localStorage({ dir: "./.soil" }),
agents: [assistant],
});
const session = await runtime.openSession(assistant);
console.log(session.id);
console.log(await (await session.send("处理订单" )).wait());配置、账本与迁移
| 配置 / API | 默认值与行为 |
| --- | --- |
| dir | 必填,保存 ledger.sqlite、WAL/SHM 与 artifacts/sha256/ |
| busyTimeoutMs | 默认 5000 ms,非负安全整数 |
| outbox.maxRetentionDays | 默认 7 天 |
| outbox.maxPendingEvents | 默认 100000 条事件 |
| storage.kind | "sqlite" |
| open() / close() | 幂等;Runtime 自动管理,直接使用 SPI 时自行调用 |
本包不读取环境变量。open() 加载/初始化持久 schema,拒绝未知未来版本。每次领域修改、租约/CAS 检查与持久事件在短 BEGIN IMMEDIATE 事务内提交;使用 journal_mode=WAL、synchronous=FULL、外键与 busy timeout。事务中不会等待模型、工具或用户回调,SQLite 跨进程串行化写入。
数据库唯一索引维护 Session 单活跃 Turn(含等待态),每次取得租约递增持久 epoch。holder、epoch、过期时间先于 Step CAS 检查;旧持有者不能写账本。终态 Attempt 不覆盖,兄弟 Step 独立提交,消息历史采用内容寻址前缀链避免每个模型 Step 重复全量对话。公开快照脱离存储,可用独占的 afterStepId 分页;会话历史直接读账本,不依赖 Outbox。
准入/命令键按 scope 与规范载荷去重,冲突抛 IdempotencyConflictError。命令授权归 Runtime,存储仍独立检查 scope 与状态并记录回执。
Outbox 与产物
Outbox 序列与消费者确认跨重启保存;账本提交时执行配置留存,也可空闲时显式 outbox.prune(...)。读游标落后于已裁剪前缀时返回 gap,即使重开后 Outbox 已空也保留缺口。oldestAvailableSeq 在空时为下一序列;确认不能倒退或超出最新事件。裁剪只影响观测历史,不删除执行恢复事实。
产物路径为 artifacts/sha256/<前两位>/<sha256>,写临时文件、fsync、原子重命名并同步目录后才返回引用;读取验证摘要与字节数。先写产物再提交账本引用,没有跨文件/账本两阶段提交。
pruneUnreferenced({ olderThan, isReferenced, limit }) 同时检查调用者与账本引用,最终检查与删除共享 SQLite 写锁。建议至少 24 小时保护期,避免尚未首次提交引用的产物被回收。进程中断可能遗留临时文件,读取/回收会忽略。scope 是账本隔离元数据;本地产物假设可信宿主并跨 scope 按内容去重。
恢复与关键边界
重启后用相同目录与兼容注册定义打开 Runtime,回收过期租约、复用已提交步骤。外部操作中断需 reconcile 证据或授权 resolveUnknown,否则保持 unknown,不能盲重试。幂等键、取消与崩溃都不能证明远端没执行,没有 exactly-once。
SQLite 持久性依赖本地文件系统兑现同步语义,本包不提供分布式锁或调度,也不自行执行恢复任务。生产环境应按业务数据要求管理目录权限与备份。
深入阅读与本地校验
pnpm build
pnpm --filter @soiljs/storage-local typecheck
pnpm --filter @soiljs/storage-local exec tsup
pnpm exec vitest run --maxWorkers=1 packages/storage-local/src/__tests__
pnpm exec vitest run --maxWorkers=1 packages/storage-local/src/__tests__/crash-recovery.test.ts测试覆盖九组 SPI 契约、独立连接、事务回滚、重开后的游标/产物保护,并对真实子 Node 进程发送 SIGKILL,验证自动对账与人工裁决。示例的关闭重开与 SIGKILL 证据分别提供;模型仍为离线夹具,不代表供应商或真实 MCP 覆盖。
