@soiljs/storage-local 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +87 -2
- package/dist/index.d.ts +14 -0
- package/dist/index.js +1340 -0
- package/dist/index.js.map +1 -0
- package/package.json +40 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ximing
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,88 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @soiljs/storage-local
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
让 Agent 的执行进度在单机重启后保留:SQLite WAL 保存结构化账本与 Outbox,本地文件保存不可变 SHA-256 内容寻址产物。它是仅依赖 `@soiljs/spi` 的 Layer 1 存储包,无第三方数据库依赖,不执行模型或工具;适合本地应用与单机服务,要求 Node.js ≥22.19。
|
|
4
|
+
|
|
5
|
+
## 安装与入口
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @soiljs/storage-local
|
|
9
|
+
# 与 Runtime 组合时安装
|
|
10
|
+
pnpm add @soiljs/agent @soiljs/models
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
| 导出入口 | 用途 |
|
|
14
|
+
| --- | --- |
|
|
15
|
+
| `@soiljs/storage-local` | `localStorage` 与 `LocalStorageConfig` |
|
|
16
|
+
|
|
17
|
+
## 最小可运行示例
|
|
18
|
+
|
|
19
|
+
此例完全离线,使用模型夹具;同一目录再次打开时保留账本与产物。夹具本身的脚本游标不承担跨进程恢复,恢复业务应重新注册兼容的模型/Agent/工具定义。
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { defineAgent, openRuntime } from "@soiljs/agent";
|
|
23
|
+
import { createFixtureModel } from "@soiljs/models/recording";
|
|
24
|
+
import { localStorage } from "@soiljs/storage-local";
|
|
25
|
+
|
|
26
|
+
const assistant = defineAgent({
|
|
27
|
+
id: "order-assistant",
|
|
28
|
+
version: "1",
|
|
29
|
+
model: createFixtureModel({
|
|
30
|
+
protocolId: "openai/chat",
|
|
31
|
+
model: "offline",
|
|
32
|
+
responses: ["订单处理完成"],
|
|
33
|
+
}),
|
|
34
|
+
instructions: "简洁回答。",
|
|
35
|
+
});
|
|
36
|
+
await using runtime = await openRuntime({
|
|
37
|
+
storage: localStorage({ dir: "./.soil" }),
|
|
38
|
+
agents: [assistant],
|
|
39
|
+
});
|
|
40
|
+
const session = await runtime.openSession(assistant);
|
|
41
|
+
console.log(session.id);
|
|
42
|
+
console.log(await (await session.send("处理订单" )).wait());
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## 配置、账本与迁移
|
|
46
|
+
|
|
47
|
+
| 配置 / API | 默认值与行为 |
|
|
48
|
+
| --- | --- |
|
|
49
|
+
| `dir` | 必填,保存 `ledger.sqlite`、WAL/SHM 与 `artifacts/sha256/` |
|
|
50
|
+
| `busyTimeoutMs` | 默认 5000 ms,非负安全整数 |
|
|
51
|
+
| `outbox.maxRetentionDays` | 默认 7 天 |
|
|
52
|
+
| `outbox.maxPendingEvents` | 默认 100000 条事件 |
|
|
53
|
+
| `storage.kind` | `"sqlite"` |
|
|
54
|
+
| `open()` / `close()` | 幂等;Runtime 自动管理,直接使用 SPI 时自行调用 |
|
|
55
|
+
|
|
56
|
+
本包不读取环境变量。`open()` 加载/初始化持久 schema,拒绝未知未来版本。每次领域修改、租约/CAS 检查与持久事件在短 `BEGIN IMMEDIATE` 事务内提交;使用 `journal_mode=WAL`、`synchronous=FULL`、外键与 busy timeout。事务中不会等待模型、工具或用户回调,SQLite 跨进程串行化写入。
|
|
57
|
+
|
|
58
|
+
数据库唯一索引维护 Session 单活跃 Turn(含等待态),每次取得租约递增持久 epoch。holder、epoch、过期时间先于 Step CAS 检查;旧持有者不能写账本。终态 Attempt 不覆盖,兄弟 Step 独立提交,消息历史采用内容寻址前缀链避免每个模型 Step 重复全量对话。公开快照脱离存储,可用独占的 `afterStepId` 分页;会话历史直接读账本,不依赖 Outbox。
|
|
59
|
+
|
|
60
|
+
准入/命令键按 scope 与规范载荷去重,冲突抛 `IdempotencyConflictError`。命令授权归 Runtime,存储仍独立检查 scope 与状态并记录回执。
|
|
61
|
+
|
|
62
|
+
## Outbox 与产物
|
|
63
|
+
|
|
64
|
+
Outbox 序列与消费者确认跨重启保存;账本提交时执行配置留存,也可空闲时显式 `outbox.prune(...)`。读游标落后于已裁剪前缀时返回 `gap`,即使重开后 Outbox 已空也保留缺口。`oldestAvailableSeq` 在空时为下一序列;确认不能倒退或超出最新事件。裁剪只影响观测历史,不删除执行恢复事实。
|
|
65
|
+
|
|
66
|
+
产物路径为 `artifacts/sha256/<前两位>/<sha256>`,写临时文件、fsync、原子重命名并同步目录后才返回引用;读取验证摘要与字节数。先写产物再提交账本引用,没有跨文件/账本两阶段提交。
|
|
67
|
+
|
|
68
|
+
`pruneUnreferenced({ olderThan, isReferenced, limit })` 同时检查调用者与账本引用,最终检查与删除共享 SQLite 写锁。建议至少 24 小时保护期,避免尚未首次提交引用的产物被回收。进程中断可能遗留临时文件,读取/回收会忽略。`scope` 是账本隔离元数据;本地产物假设可信宿主并跨 scope 按内容去重。
|
|
69
|
+
|
|
70
|
+
## 恢复与关键边界
|
|
71
|
+
|
|
72
|
+
重启后用相同目录与兼容注册定义打开 Runtime,回收过期租约、复用已提交步骤。外部操作中断需 `reconcile` 证据或授权 `resolveUnknown`,否则保持 `unknown`,不能盲重试。幂等键、取消与崩溃都不能证明远端没执行,没有 exactly-once。
|
|
73
|
+
|
|
74
|
+
SQLite 持久性依赖本地文件系统兑现同步语义,本包不提供分布式锁或调度,也不自行执行恢复任务。生产环境应按业务数据要求管理目录权限与备份。
|
|
75
|
+
|
|
76
|
+
## 深入阅读与本地校验
|
|
77
|
+
|
|
78
|
+
[在线 API](https://ximing.github.io/agent-foundation/#/api/storage) · [本地恢复示例](../../examples/02-local-durable-recovery.ts) · [SPI 存储契约](../../docs/03-spi-storage-contracts.md)
|
|
79
|
+
|
|
80
|
+
```sh
|
|
81
|
+
pnpm build
|
|
82
|
+
pnpm --filter @soiljs/storage-local typecheck
|
|
83
|
+
pnpm --filter @soiljs/storage-local exec tsup
|
|
84
|
+
pnpm exec vitest run --maxWorkers=1 packages/storage-local/src/__tests__
|
|
85
|
+
pnpm exec vitest run --maxWorkers=1 packages/storage-local/src/__tests__/crash-recovery.test.ts
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
测试覆盖九组 SPI 契约、独立连接、事务回滚、重开后的游标/产物保护,并对真实子 Node 进程发送 SIGKILL,验证自动对账与人工裁决。示例的关闭重开与 SIGKILL 证据分别提供;模型仍为离线夹具,不代表供应商或真实 MCP 覆盖。
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { StorageAdapter } from '@soiljs/spi';
|
|
2
|
+
|
|
3
|
+
interface LocalStorageConfig {
|
|
4
|
+
dir: string;
|
|
5
|
+
busyTimeoutMs?: number;
|
|
6
|
+
outbox?: {
|
|
7
|
+
maxRetentionDays?: number;
|
|
8
|
+
maxPendingEvents?: number;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
/** SQLite WAL ledger: every domain mutation and its Outbox facts commit together. */
|
|
12
|
+
declare function localStorage(config: LocalStorageConfig): StorageAdapter;
|
|
13
|
+
|
|
14
|
+
export { type LocalStorageConfig, localStorage };
|