Skip to content

fix(runtime): replay checkpoints before tool pruning - #3632

Merged
Astro-Han merged 1 commit into
mainfrom
fix/3631-replay-before-prune
Aug 23, 2026
Merged

Astro-Han merged 1 commit into
mainfrom
fix/3631-replay-before-prune

Conversation

@Astro-Han

Copy link
Copy Markdown
Contributor

Replay durable history checkpoints against the immutable RuntimeEvent ledger before stale Tool Result pruning shapes the uncovered remainder. This keeps checkpoint source validation stable as the recent-turn pruning window moves.

The regression test covers a checkpoint whose oversized covered Tool Result has become stale and has a valid archive ref. It fails with source_hash_mismatch before the fix and replays the checkpoint after the order change.

Fixes #3631

Verification

  • cd packages/core && npm run build
  • cd packages/runtime && npm run build
  • node --test dist/__tests__/context-budget.test.js
  • npx biome format packages/runtime/src/context-budget.ts packages/runtime/src/__tests__/context-budget.test.ts
  • git diff --check

Full repository tests were not run locally.

AI use

  • No generative tool made a substantive contribution
  • Generative tooling made a substantive contribution

Tool(s) and scope: Maka diagnosed the local failure, traced the checkpoint/pruning order, and authored the implementation and regression test under human direction.

Checklist

  • Tests cover the change and fail without it
  • Lint, format, typecheck and the affected suites pass locally

Does this PR entail a change in behavior?

  • Yes — checkpoint replay now precedes stale Tool Result pruning
  • No

@Astro-Han
Astro-Han marked this pull request as ready for review August 23, 2026 15:02

@M4n5ter M4n5ter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English

Approved at exact head 4846995c0968b0cba5fbece3af9784af5dc2e0e0. I found no P0–P3 issue in the checkpoint/pruning order change.

The new order preserves the two distinct authorities:

  • Checkpoint validation first hashes the immutable RuntimeEvent prefix from which the checkpoint was created. It then replaces that covered prefix with either the text checkpoint projection or the provider-native encrypted compaction item.
  • Stale Tool Result pruning runs only over the uncovered replay remainder. It still rewrites an eligible oversized tail result to maka.archived_tool_result; a covered result is gone with the replaced prefix rather than being revived.

This cannot re-execute a historical tool. Checkpoint replay is a pure history projection and does not call ToolRuntime. The current provider tool surface is prepared earlier from the complete durable ledger and the current catalog, independently of the budgeted replay projection. A checkpoint that mentions an old tool therefore remains historical context; it neither restores a removed tool definition nor dispatches that tool.

The 70 added test lines are one behavioral regression plus local event builders. The test does more than assert call order: with the old order, replacing the covered result with an archive placeholder changes the source digest and produces source_hash_mismatch; on this head the same immutable ledger accepts the checkpoint, reports decision: replaced, and removes the covered events. I also exercised the combined boundary with one covered oversized result and one uncovered oversized result: the covered event disappeared through checkpoint replay, while the uncovered result became an archive placeholder and prunedToolResults was exactly 1.

Validation completed: Core, Storage, and Runtime builds; 135 focused context-budget/history-compaction/tool-result-archive tests; Biome on both changed files; diff check; clean local merge-tree against current main; and the exact-head hosted test check is terminal success.

简体中文

已在 exact head 4846995c0968b0cba5fbece3af9784af5dc2e0e0 上批准。本次 checkpoint/pruning 顺序调整未发现 P0–P3 问题。

新顺序保留了两套互不混淆的 authority:

  • Checkpoint 校验先对创建该 checkpoint 时的不可变 RuntimeEvent 前缀计算 hash;通过后,再把已覆盖前缀替换为文本 checkpoint 投影或 provider-native 加密 compaction item。
  • stale Tool Result pruning 只处理未被覆盖的 replay remainder。满足条件的超大 tail result 仍会改写为 maka.archived_tool_result;已覆盖 result 随被替换的前缀一起消失,并没有被重新“复活”。

这条路径不会重新执行历史工具。Checkpoint replay 只是纯历史投影,不调用 ToolRuntime。当前 provider tool surface 更早就从完整 durable ledger 与当前 catalog 独立生成,不依赖预算后的 replay projection。因此 checkpoint 即使提到旧工具,也只属于历史上下文;它既不会恢复已移除的工具定义,也不会调度该工具。

新增 70 行由一个行为回归测试和局部 event builders 构成。测试不只钉调用顺序:旧顺序会先把 covered result 换成 archive placeholder,改变 source digest 并触发 source_hash_mismatch;当前 head 会在同一不可变 ledger 上接受 checkpoint、报告 decision: replaced,并删除已覆盖事件。我另外实跑了“一个 covered 超大 result + 一个 uncovered 超大 result”的组合边界:covered event 通过 checkpoint replay 消失,uncovered result 仍变为 archive placeholder,且 prunedToolResults 精确为 1。

已完成验证:Core、Storage、Runtime build;135 个 context-budget/history-compaction/tool-result-archive focused tests;两个变更文件的 Biome;diff check;与当前 main 的本地 merge-tree 无冲突;exact-head hosted test 终态成功。

@Astro-Han
Astro-Han merged commit 77d70c4 into main Aug 23, 2026
1 check passed
@Astro-Han
Astro-Han deleted the fix/3631-replay-before-prune branch August 23, 2026 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(runtime): replay history checkpoint before stale Tool Result pruning

2 participants