Repository navigation
Description
English
What happened
Manual context compaction can report already_compacted and do nothing even though the stored history-compact checkpoint is stale. The send path already rejects that same checkpoint with effective_history_changed. So the user asks for compaction, gets a "nothing to do" result, and the stale checkpoint stays in place.
Expected: when the checkpoint's covered effective history has changed since the fold, manual compaction runs a normal fold and writes a new checkpoint whose summary reads the current effective view. When the effective history is unchanged, a repeated manual compaction stays a no-op without a model call.
Root cause
A checkpoint is reusable only when two checks pass:
- Raw identity: the covered RuntimeEvent prefix is the same (
matchHistoryCompactCheckpointPrefix). - Effective currency: the effective view of that prefix, after model projection transitions, still has the digest pinned in
coverage.effectiveSourceDigest.
A projection transition (for example, archiving a large tool result) changes the effective view without changing the raw ledger. So check 1 can pass while check 2 fails.
The compactHistory fast path in packages/runtime/src/ai-sdk-compaction.ts:316-343 performs only check 1. When the raw prefix matches and there are no successor events, it returns already_compacted. It never calls checkpointEffectiveCoverageMatches (ai-sdk-compaction.ts:569-581), which prepareContextBudgetPolicy (:712-743) uses for the same checkpoint before a send.
How to reproduce
Not yet reproduced in a live session. A method-level reproduction with injected boundaries (controlled ledger, summarizer, and effective projection; no real provider):
- Create a history-compact checkpoint whose coverage includes a tool result.
- Change the effective projection of that covered tool result, as a projection transition committed after the fold would. Keep the raw events unchanged.
- Confirm the pre-send gate rejects the checkpoint (
effective_history_changed). - Call
compactHistoryagain.
Observed: compactHistory returns already_compacted, with 0 effective-history reads and 0 summarizer calls. Control: with the old checkpoint removed, the same call produces a new summary from the current effective projection.
The real-world trigger needs a transition on a covered event after the checkpoint was written, for example an archive write that failed and later succeeded. How often this happens is unknown.
Proposed fix
In the fast path, fold the covered events through the current projection transitions and check effective currency before returning already_compacted. On a mismatch, fall through to the normal planHistoryCompaction flow, whose roll-forward currency gate (history-compaction.ts:343-351) already discards a stale checkpoint and re-summarizes the span.
Before:
compactHistory (manual)
│
├─ load checkpoint
├─ canContinueHistoryCompactCheckpointForModel? ── no ──▶ normal fold
│ yes
├─ matchHistoryCompactCheckpointPrefix (raw) ── mismatch or successor events ──▶ normal fold
│ match, no successor events
▼
return already_compacted ◀── effective view never checked
After:
compactHistory (manual)
│
├─ load checkpoint
├─ canContinueHistoryCompactCheckpointForModel? ── no ──▶ normal fold
│ yes
├─ matchHistoryCompactCheckpointPrefix (raw) ── mismatch or successor events ──▶ normal fold
│ match, no successor events
├─ fold covered events through current projection transitions
├─ effective digest == coverage.effectiveSourceDigest?
│ yes ──▶ return already_compacted
│ no
▼
normal fold: planHistoryCompaction
├─ roll-forward gate discards the stale checkpoint
├─ summarize the current effective view
└─ write a new checkpoint ──▶ compacted
Acceptance:
- A repeated manual compaction with unchanged effective history still makes no model call (existing coverage in
ai-sdk-backend.test.tsstays green). - After effective drift inside the covered prefix, manual compaction returns
compacted, and the summarizer input is the current effective view.
Environment
- Maka version or commit:
apache/makamainatc7fa6bb6a(found by source analysis) - Surface: Runtime (manual context compaction from Desktop or TUI)
- Not observed in a user session; reproduced at method level only
Additional context
- The currency gate was introduced in the fix(runtime): match history-compact checkpoints against the raw ledger prefix #4845 review. Four other call sites implement it; this fast path is the one that was missed. refactor(runtime): single owner for history-compact checkpoint currency #5930 proposes a single owner for this check.
- No change to checkpoint schema, provider fit (Context budget: stop estimating fit; the provider decides, the Maka window is a user target #4559), or trigger policy.
Drafted with AI-agent assistance (Devin) from source analysis; reviewed and submitted by the reporter.
中文
发生了什么
手动上下文压缩可能报告 already_compacted 并且什么都不做,尽管已存储的 history-compact checkpoint 其实已经过时。发送路径对同一个 checkpoint 已经会以 effective_history_changed 拒绝使用。结果是用户请求压缩,得到"无需处理",过时的 checkpoint 仍然保留。
期望行为:如果 checkpoint 覆盖范围内的有效历史在压缩之后发生了变化,手动压缩应走正常流程,写入新的 checkpoint,并且摘要读取的是当前的有效视图。如果有效历史没有变化,重复的手动压缩仍应是无操作,不调用模型。
根因
checkpoint 只有在两项检查都通过时才可复用:
- 原始身份:被覆盖的 RuntimeEvent 前缀没有变化(
matchHistoryCompactCheckpointPrefix)。 - 有效时效性:该前缀经过模型投影 transition 折算后的有效视图,其摘要指纹仍等于
coverage.effectiveSourceDigest中记录的值。
投影 transition(例如归档一个大的工具结果)会改变有效视图,但不会改变原始账本。所以第 1 项可以通过,而第 2 项不通过。
packages/runtime/src/ai-sdk-compaction.ts:316-343 中 compactHistory 的快路径只做了第 1 项检查。原始前缀匹配且没有后续事件时,它直接返回 already_compacted,从不调用 checkpointEffectiveCoverageMatches(ai-sdk-compaction.ts:569-581)。而 prepareContextBudgetPolicy(:712-743)在发送前会对同一个 checkpoint 调用这项检查。
复现步骤
尚未在真实会话中复现。以下是注入边界的方法级复现(账本、摘要器和有效投影均为受控输入,没有真实供应商):
- 创建一个覆盖范围包含某个工具结果的 history-compact checkpoint。
- 改变这个被覆盖的工具结果的有效投影,模拟压缩之后提交的投影 transition。原始事件保持不变。
- 确认发送前的检查会拒绝该 checkpoint(
effective_history_changed)。 - 再次调用
compactHistory。
观察结果:compactHistory 返回 already_compacted,有效历史读取 0 次,摘要器调用 0 次。对照组:移除旧 checkpoint 后,同样的调用会基于当前有效投影生成新摘要。
真实场景的触发条件是:checkpoint 写入之后,被覆盖的事件上又发生了 transition,例如归档写入先失败、后来重试成功。实际发生频率未知。
建议修复
在快路径返回 already_compacted 之前,先用当前的投影 transition 折算被覆盖的事件,并检查有效时效性。如果不匹配,就落入正常的 planHistoryCompaction 流程。该流程的滚动续接时效性检查(history-compaction.ts:343-351)已经会丢弃过时的 checkpoint 并重新摘要整个范围。
修复前:
compactHistory(手动)
│
├─ 加载 checkpoint
├─ canContinueHistoryCompactCheckpointForModel?── 否 ──▶ 正常压缩
│ 是
├─ matchHistoryCompactCheckpointPrefix(原始)── 不匹配或有后续事件 ──▶ 正常压缩
│ 匹配,且没有后续事件
▼
返回 already_compacted ◀── 从未检查有效视图
修复后:
compactHistory(手动)
│
├─ 加载 checkpoint
├─ canContinueHistoryCompactCheckpointForModel?── 否 ──▶ 正常压缩
│ 是
├─ matchHistoryCompactCheckpointPrefix(原始)── 不匹配或有后续事件 ──▶ 正常压缩
│ 匹配,且没有后续事件
├─ 用当前的投影 transition 折算被覆盖的事件
├─ 有效指纹 == coverage.effectiveSourceDigest?
│ 是 ──▶ 返回 already_compacted
│ 否
▼
正常压缩:planHistoryCompaction
├─ 滚动续接检查丢弃过时的 checkpoint
├─ 基于当前有效视图生成摘要
└─ 写入新的 checkpoint ──▶ compacted
验收条件:
- 有效历史未变时,重复的手动压缩仍不调用模型(
ai-sdk-backend.test.ts中的现有覆盖保持通过)。 - 被覆盖前缀内的有效历史发生漂移后,手动压缩返回
compacted,并且摘要器的输入是当前的有效视图。
环境
- Maka 版本或提交:
apache/makamain分支c7fa6bb6a(通过源码分析发现) - 使用界面:Runtime(从 Desktop 或 TUI 发起的手动上下文压缩)
- 未在用户会话中观察到,仅做了方法级复现
补充信息
- 时效性检查是在 fix(runtime): match history-compact checkpoints against the raw ledger prefix #4845 的审阅中引入的。另外四个调用点都实现了这项检查,只有这个快路径漏掉了。refactor(runtime): single owner for history-compact checkpoint currency #5930 提议由单一模块负责这项检查。
- 不改变 checkpoint 结构、供应商判定是否容纳(Context budget: stop estimating fit; the provider decides, the Maka window is a user target #4559)或触发策略。
本 issue 由 AI 代理(Devin)基于源码分析协助起草,由报告者审阅并提交。
Activity
fix(runtime): verify checkpoint effective history in manual compact r…
fix(runtime): keep manual compaction checkpoints valid across tool-re…
refactor(runtime): centralize history-compact checkpoint currency val…