@linnlabs/linnkit 0.32.3 → 0.34.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.
Files changed (83) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/dist/cli.cjs +458 -70
  3. package/dist/cli.cjs.map +1 -1
  4. package/dist/cli.js +458 -70
  5. package/dist/cli.js.map +1 -1
  6. package/dist/context-manager.cjs +55 -0
  7. package/dist/context-manager.cjs.map +1 -1
  8. package/dist/context-manager.js +55 -0
  9. package/dist/context-manager.js.map +1 -1
  10. package/dist/contracts/agentSpec.d.cts +30 -30
  11. package/dist/contracts/agentSpec.d.ts +30 -30
  12. package/dist/contracts/audit.d.cts +24 -24
  13. package/dist/contracts/audit.d.ts +24 -24
  14. package/dist/contracts/contextPolicy.d.cts +36 -36
  15. package/dist/contracts/contextPolicy.d.ts +36 -36
  16. package/dist/contracts/events.d.cts +1083 -1083
  17. package/dist/contracts/events.d.ts +1083 -1083
  18. package/dist/contracts/execution.d.cts +8 -8
  19. package/dist/contracts/execution.d.ts +8 -8
  20. package/dist/contracts/messages.d.cts +1178 -1178
  21. package/dist/contracts/messages.d.ts +1178 -1178
  22. package/dist/contracts/provider-continuation.d.cts +28 -28
  23. package/dist/contracts/provider-continuation.d.ts +28 -28
  24. package/dist/contracts/sse.d.cts +899 -899
  25. package/dist/contracts/sse.d.ts +899 -899
  26. package/dist/contracts/sub-run-trace-payload.d.cts +26 -26
  27. package/dist/contracts/sub-run-trace-payload.d.ts +26 -26
  28. package/dist/contracts/token-accounting.d.cts +30 -30
  29. package/dist/contracts/token-accounting.d.ts +30 -30
  30. package/dist/index.cjs +538 -94
  31. package/dist/index.cjs.map +1 -1
  32. package/dist/index.js +538 -94
  33. package/dist/index.js.map +1 -1
  34. package/dist/quickstart/toolRuntime.d.cts +1 -1
  35. package/dist/quickstart/toolRuntime.d.ts +1 -1
  36. package/dist/quickstart/types.d.cts +1 -1
  37. package/dist/quickstart/types.d.ts +1 -1
  38. package/dist/quickstart.cjs +458 -70
  39. package/dist/quickstart.cjs.map +1 -1
  40. package/dist/quickstart.js +458 -70
  41. package/dist/quickstart.js.map +1 -1
  42. package/dist/runtime-kernel/child-runs/childRunInvoker.d.cts +11 -0
  43. package/dist/runtime-kernel/child-runs/childRunInvoker.d.ts +11 -0
  44. package/dist/runtime-kernel/events/agentEvents.d.cts +172 -172
  45. package/dist/runtime-kernel/events/agentEvents.d.ts +172 -172
  46. package/dist/runtime-kernel/execution/eventBusEventPersistence.d.cts +15 -1
  47. package/dist/runtime-kernel/execution/eventBusEventPersistence.d.ts +15 -1
  48. package/dist/runtime-kernel/graph-engine/definitions/runContinuation.d.cts +34 -0
  49. package/dist/runtime-kernel/graph-engine/definitions/runContinuation.d.ts +34 -0
  50. package/dist/runtime-kernel/graph-engine/engine.d.cts +13 -2
  51. package/dist/runtime-kernel/graph-engine/engine.d.ts +13 -2
  52. package/dist/runtime-kernel/graph-engine/functions/parseEngineCheckpoint.d.cts +6 -0
  53. package/dist/runtime-kernel/graph-engine/functions/parseEngineCheckpoint.d.ts +6 -0
  54. package/dist/runtime-kernel/graph-engine/index.d.cts +3 -1
  55. package/dist/runtime-kernel/graph-engine/index.d.ts +3 -1
  56. package/dist/runtime-kernel/graph-engine/types.d.cts +8 -0
  57. package/dist/runtime-kernel/graph-engine/types.d.ts +8 -0
  58. package/dist/runtime-kernel/run-supervisor/definitions/runSupervisorContracts.d.cts +35 -6
  59. package/dist/runtime-kernel/run-supervisor/definitions/runSupervisorContracts.d.ts +35 -6
  60. package/dist/runtime-kernel/run-supervisor/index.d.cts +1 -1
  61. package/dist/runtime-kernel/run-supervisor/index.d.ts +1 -1
  62. package/dist/runtime-kernel/run-supervisor/memoryRunRegistryStore.d.cts +1 -0
  63. package/dist/runtime-kernel/run-supervisor/memoryRunRegistryStore.d.ts +1 -0
  64. package/dist/runtime-kernel/run-supervisor/runHandle.d.cts +9 -5
  65. package/dist/runtime-kernel/run-supervisor/runHandle.d.ts +9 -5
  66. package/dist/runtime-kernel/run-supervisor/runRegistryStorePort.d.cts +2 -0
  67. package/dist/runtime-kernel/run-supervisor/runRegistryStorePort.d.ts +2 -0
  68. package/dist/runtime-kernel/run-supervisor/runSupervisor.d.cts +9 -2
  69. package/dist/runtime-kernel/run-supervisor/runSupervisor.d.ts +9 -2
  70. package/dist/runtime-kernel.cjs +535 -91
  71. package/dist/runtime-kernel.cjs.map +1 -1
  72. package/dist/runtime-kernel.d.cts +3 -1
  73. package/dist/runtime-kernel.d.ts +3 -1
  74. package/dist/runtime-kernel.js +532 -92
  75. package/dist/runtime-kernel.js.map +1 -1
  76. package/dist/testkit.cjs +535 -91
  77. package/dist/testkit.cjs.map +1 -1
  78. package/dist/testkit.js +535 -91
  79. package/dist/testkit.js.map +1 -1
  80. package/docs/integration/child-runs.md +12 -0
  81. package/docs/integration/persistence.md +24 -0
  82. package/docs/integration/run-supervisor.md +15 -2
  83. package/package.json +2 -2
@@ -118,6 +118,18 @@ Linnkit 只定义 child-run 原语和端口,不替 Host 选择全局实例。H
118
118
 
119
119
  ## 6. 最小验证
120
120
 
121
+ ### 同步 child 的持久继续
122
+
123
+ `ChildRunInvokeConfig.persistence` 显式注入持久 Checkpointer、ExecutionCheckpointPort
124
+ 与 ToolRecoveryPort。首次调用持久化原 child 输入;继续传入同一 runId 和 expectedRevision,
125
+ 保留原 turn、用户输入、请求与累计步数。已 yielded 的 child 只读取原结果,不再次调用模型。
126
+ 此模式的异常向 Host 传播且不清理 checkpoint,由 Host 将 attempt 结算为暂停或明确终止。
127
+ 未配置 persistence 的现有嵌入调用仍使用短生命周期内存图。
128
+
129
+ Host 必须先保存父 tool call 到稳定 child identity 的映射、冻结 child Agent 定义,并复用
130
+ 原 child lifecycle / EventStore writer。仅注入持久 checkpoint 而每次注册新 child 不算恢复。
131
+ 同步 child 的待审批限制仍然存在;需要产品交互的工作交给 foreground run。
132
+
121
133
  - 单测:父 agent 工具内 `invokeChildRun` → 父 run 的 `cost().childrenTotal.llmCost > 0`
122
134
  - 集成测:child run 内部调用工具时,`model.select` / `tool.allow` audit envelope 的 `scope.conversationId` 与注册 child run 的 conversationId 一致,`scope.runId` 是 child runId。
123
135
  - 单测:child run 的 `abortSignal` 在调用前或执行中触发时,即使 provider 抛普通 Error,`invokeChildRun` 仍 resolve 出 `cancelled:true`,而不是 reject 或写成 failed。
@@ -11,6 +11,7 @@
11
11
  ## 1. linnkit 给你的合同
12
12
 
13
13
  - `Checkpointer`(来自 `@linnlabs/linnkit/runtime-kernel`,在 `graph` namespace 下):`load` / `save` / `clear` 三个必需方法 + `peekMeta` / `list` 两个可选。
14
+ - JSON 持久化 adapter 使用 `graph.parseEngineCheckpoint` 解析读取结果,校验版本、执行位置、预算与事件合同并拒绝序列化能力对象;不能把 JSON 直接断言为 EngineState。Graph result 的 `stepCount` 是本 execution 增量,checkpoint `executorLocal.stepCount` 才是 durable run 的累计值;已 yielded 的继续返回零增量。
14
15
  - `Checkpointer` 的 key 参数叫 `checkpointKey`:它是 EngineState 快照索引。所有可独立运行的顶层、foreground、auxiliary、detached run 都必须使用稳定 `runId`;同步 child-run 使用自己的内部 run-scoped key。禁止用 `conversationId`,否则同会话并行 run 会互相覆盖。
15
16
  - `EventStore`(来自 `@linnlabs/linnkit/runtime-kernel`,在 `graph` namespace 下):`append` / `range` / `latestEventStoreId` 三个必需 + `truncate` 可选。配套 `createMonotonicEventStoreIdFactory()` 帮你生成单调 storage cursor。
16
17
  - `RunRegistryStore`(来自 `@linnlabs/linnkit/runtime-kernel`,在 `runSupervisor` namespace 下):run lifecycle 元数据落库。
@@ -57,4 +58,27 @@ UI 历史应由 `events` 派生为可重建 read model。read model 可以与事
57
58
 
58
59
  ## 6. 最小验证
59
60
 
61
+ ### 可恢复执行的显式接入
62
+
63
+ `GraphExecutorConfig.executionCheckpointPort` 启用可恢复执行提交:Host 必须把 publisher
64
+ 已接纳的本步骤 durable facts 与传入 checkpoint 放进同一短事务,并校验当前 activation
65
+ 的写入权。普通 EventBus consumer 在此模式下暂存事实,由该提交释放;不能先独立落盘
66
+ 再把两次写入称为原子提交。`RuntimeEventCommitPort` 的显式 durable-first 事实仍走原入口。
67
+
68
+ `EventBusEventPersistence.checkpointWriter` 在现有 consumer 内暂存本步骤事实;Graph 的
69
+ 提交端口调用同实例 `commitCheckpoint`,writer 一次收到 facts 与 checkpoint。
70
+ 离开 Graph 时调用 `finishCheckpointWrites` 作废未提交 attempt 并恢复普通 settlement
71
+ 写入。`drain` 只等待已安排的事务,不能把 staged facts 当成已持久化。客户端以持久运行
72
+ 状态确认完成,不把尚未提交的实时进度当作恢复凭据。
73
+
74
+ Graph 在节点调用前保存进度,每个工具 call 前保存执行意图、结束后保存结果与下一位置。
75
+ `continueSession` 只挂载新的临时能力,保留原请求、历史、模型和累计预算;不创建用户输入,
76
+ 也不从 `user` 重启。`yielded` checkpoint 只返回已完成状态,不能重跑最终节点。
77
+ 未决工具必须经 `ToolRecoveryPort` 查询 owner;无 owner 证明时抛 `RunRecoveryBlockedError`,
78
+ 不按相同参数猜测成功。允许安全重试是 owner 的显式决定。
79
+
80
+ `RunPauseRequested` 是临时停止的 signal reason;它不等于取消,也不结算尚未执行的
81
+ 工具为失败。Host 仍负责 RunDescriptor、权限重建、唯一 activation、生命周期和资源保留。
82
+ 仅使用原 `Checkpointer` 的 Host 不会自动获得这些恢复保证。
83
+
60
84
  linnkit 在内部对每个 port 都跑了 contract test。你的实现必须通过这些**等价的契约测试**。建议在 host 测试里 mirror linnkit 的 contract test,把 memory 实现 → 你的实现做参数化,确保行为 1:1。
@@ -40,6 +40,7 @@ const handle = await supervisor.registerRun({
40
40
  - runner 生命周期必须显式写:启动前 `markRunning()`,正常结束 `markCompleted()`,异常结束 `markFailed()`。取消请求先由 `handle.cancel({ reason })` 写 `cancelled` 并触发 abort;执行器取得真实结果后,再用第二个 lifecycle patch 补全 `currentNode / iterationsUsed`。
41
41
  - `WaitUserNode` 创建的 `requires_user_interaction` 是正式 pause 事实。它必须经 `RuntimeEventPublisher` 附着正式 `run_id / lane / visibility`,与其他事实一起发布和持久化。host runner 在 persistence drain 成功后才能调用 `markAwaitingUser()`,不得从 Graph 返回值 cherry-pick 后补发第二份事件。
42
42
  - HITL 恢复使用 `claimResume(runId, interaction, eventBus, signal)`。返回的 `RunResumeClaim` 先原子占用一次性 interaction,但保持 `awaiting_user`;host 只有在响应事实持久化成功后才能调用 `activate()`,持久化前失败调用 `release()`。这样并发/重复提交只有一个能激活,也不会在 runner 启动前留下半恢复的 running run。
43
+ - 可恢复 Host 通过 `activate({ executionId, inputEventIds, admissionCommit })` 把响应事实与激活状态放入同一事务。`metadata.resumeInputs` 保留已提交的响应引用及原 checkpoint revision;重启若 Graph 仍停在原等待边界,Host 读取这些事实完成原 response 的 resume,不能要求用户再次提交。只有瞬时能力在新 execution 中重新装配。
43
44
  - `RunResumeInteraction` 必须完整匹配 `interactionId / toolCallId / checkpointRevision / resumeToken`。host 必须让 GraphExecutor 用同一 `runId` 的 checkpoint 恢复,禁止注册替代 run 或按 conversation 查“最近 checkpoint”。
44
45
  - 多个 run 共享观察通道时,已发布事件必须带正式 `run_id`。`RunHandle.observe()` 只接收当前 run 的事实;缺少 run identity 是 publisher 边界错误,不能通过 metadata 别名或无差别透传补救。
45
46
  - `registerRun()` / `spawnDetached()` 会把 `AgentSpec` 与 request 作为注册时快照保存;`spawnDetached()` 的 executor 也读取这份快照。调用方后续修改原始对象不会改变已经注册的后台 run。
@@ -47,9 +48,12 @@ const handle = await supervisor.registerRun({
47
48
  - `spawnDetached()` 正常完成或失败后会释放 handle、abort controller 和 EventBus 监听。外部取消只先触发 abort;必须等 executor settlement 写入最终进度后,`waitForTerminal()` 才返回并释放资源。
48
49
  - `maxActiveRuns` 是拒绝式背压,不是队列。超限时 `registerRun()` / `spawnDetached()` 会抛 `RunConcurrencyLimitExceededError`,host 应在上层 orchestration 决定是提示用户、排队还是重试。
49
50
  - `concurrencyKey` 是 host 定义的进程内原子唯一键。适合表达 `conversation:<id>:foreground` 这类业务约束;允许并行的 auxiliary 不应复用 foreground key。禁止用“先 list/find、再 register”替代,因为两个请求可同时通过查询。
51
+ - 已暂停运行被新请求替代时,`registerRun` 使用 `replacesPausedRun` 的原 run / execution / updatedAt 身份,以及 `admissionCommit`。Host 必须在一个事务中核对旧记录、提交旧 cancelled、新 pending 和新请求必要输入;提交失败保留旧暂停运行及其并发名额。不能先取消旧运行再尝试保存新输入。普通注册也可通过同一 `admissionCommit` 原子保存身份与输入。
52
+ - 继续操作应同时提交 `expectedExecutionId` 与 `expectedUpdatedAt`;时间戳只表示暂停状态时间,不能独自防止同毫秒暂停、继续、再暂停后的旧命令重放。
50
53
  - transport `EventBus.close()` 不等于 run 终态。`awaiting_user` 会保留 handle/slot;恢复时 `claimResume.activate()` 把同一 RunHandle 换绑到新的 transport EventBus,并轮换 execution controller,`observeRun()` 可跨 transport 连续观察。同步 run 在 completed/failed/cancelled 后释放;detached 取消还必须等 executor settlement。
51
54
  - detached run 执行中如果 EventBus 提前 close,不会提前释放 active slot,避免后台 run 绕过并发限制;slot 会等 detached executor 终态 cleanup。
52
- - `RunHandle.pause()/resume()` 仍是冷暂停占位;不要与已实现的 HITL `RunSupervisor.claimResume()` 混淆。`runTree/handleFailure` 仍未实现。
55
+ - `pause()` 先持久化暂停意图,再以 `RunPauseRequested` 中断当前 attempt;此时 `status=paused` 且 `pausedAt` 缺失,表示仍在收口。Host 保存断点、收口当前 execution 后调用 `markPaused()`,才允许 `resumePausedRun()` 激活。它不创建用户消息,也不提交任何审批。
56
+ - `resumePausedRun({ runId, expectedUpdatedAt, executionId, eventBus })` 保留同一 run 并轮换 execution identity / signal。Host 必须先完成前一 execution 的收口,之后再启动 Graph `continueSession`。原本抛 NotImplementedError 的 `RunHandle.resume()` 已移除,避免把“改状态”误当成“调度 Graph”。`runTree/handleFailure` 仍未实现。
53
57
 
54
58
  ## 3. RunHandle 完整 API(截至 0.5.0)
55
59
 
@@ -67,7 +71,16 @@ const handle = await supervisor.registerRun({
67
71
 
68
72
  ## 4. 进程恢复(recoverOnBoot)
69
73
 
70
- 进程启动时建议调用 `recoverOnBoot()`,把上次进程遗留的 `pending/running/awaiting_user/paused` run 标记为 `RUN_ABANDONED`,避免管理面板里永远挂着"运行中"。
74
+ Host 提供 `canRestoreRun(record)` 时,`recoverOnBoot()` 把具备 durable 输入的遗留运行重建为
75
+ `paused`,保留原等待交互及其 identity,并清除旧进程的临时 response claim。它不会调用
76
+ executor。Host 校验 descriptor 后用 `restoreRun()` 重建原 handle,等待用户的正式继续操作。
77
+ 缺少恢复输入的旧运行仍以 `RUN_ABANDONED` 结算;已 cancelled/completed/failed 的运行不复活。
78
+
79
+ 恢复装配必须实现 `RunRegistryStore.compareAndSwap(previous, next)`:状态转换以完整旧记录
80
+ 为条件原子提交,冲突向调用方返回而不是覆盖。Host 同时必须持有工作区唯一 owner,并在
81
+ 事件 / checkpoint 事务校验当前 `metadata.executionId`;Supervisor 的内存锁不能替代
82
+ 存储 fence。RunHandle 拒绝与当前 execution identity 不一致的旧 owner lifecycle 写入。
83
+ `restoreRun` 不会从聊天记录猜 request:原 AgentSpec、请求和能力兼容性校验属于 Host descriptor。
71
84
 
72
85
  ## 5. Cost 统计
73
86
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@linnlabs/linnkit",
3
- "version": "0.32.3",
3
+ "version": "0.34.0",
4
4
  "type": "module",
5
5
  "description": "A fine-grained context engineering framework for Agent applications — control every token sent to the model, with clear run lifecycle, audit records, and testable protocol boundaries.",
6
6
  "license": "MIT",
@@ -109,7 +109,7 @@
109
109
  "vitest": "^4.1.11"
110
110
  },
111
111
  "linnkit": {
112
- "phase": "development (0.32.3 — Vitest 4 testkit compatibility)",
112
+ "phase": "development (0.34.0 — atomic recovery admission)",
113
113
  "sourceOfTruth": "CHANGELOG.md",
114
114
  "notes": [
115
115
  "子入口 ./runtime-kernel/events 是 browser-safe slim seam:仅暴露 events governance 纯函数,禁止扩展任何 Node-only 依赖(如 node:async_hooks / crypto / fs)",