Skip to content

feat: let the model read history behind a compaction checkpoint - #60

Merged
kylinmac merged 3 commits into
mainfrom
feat/compaction-history-lookup
Oct 30, 2026
Merged

kylinmac merged 3 commits into
mainfrom
feat/compaction-history-lookup

Conversation

@kylinmac

Copy link
Copy Markdown
Owner

改动

实现 R21.1:压缩后,模型可以回查压缩点之前的原文,用来找回摘要漏掉的代码、错误原文或用户原话。计划由 X-COMPACT 转为 R21,验收 V60。

  • 宿主读取:新增 session.historyRead(crates/host-core/src/transcripts/history.rs、rpc/mc_session_history_rpc.rs)。
    • 只读当前检查点 throughMessageId 及之前的消息,按页读取,不读全量 transcript。
    • 状态锁内只确认会话存在、复制数据目录;读取放到 spawn_blocking。
    • 读取位置只在同一个布局快照内使用,偏移失效时由既有 with_layout_recovery 重建一次。
    • 游标用稳定的消息 ID;历史被重写后,旧游标返回失效,不复用旧偏移。
    • 返回内容不含 thinking、页外父记录、子代理消息和附件正文。
    • 条数、单条长度、单次输出和关键词扫描都有上限;扫描没做完时会明确标出,和"确实没找到"区分开。
  • 会话归属:agent-sidecar.ts 记录嵌入宿主经 agent.prepare / prompt / steer / compact / executeApprovedPlan 交给本 sidecar 的会话,agent.disposeSession 和连接关闭时移除。session.historyRead 只对这些会话放行,其他会话返回 SESSION_NOT_BOUND。session.get 行为不变。
  • 工具:ContextHistory(mc-context-history-tool.ts)。
    • 和 new_context 一起注册,通过 ToolSearch 按需加载,所以压缩前后工具集合不变。
    • 会话由 runtime 绑定,模型只能传 query / around / cursor / limit;游标固定在它来自的检查点上。
    • 用户点停止后立即不再等待。子代理继承时拒绝(SUBAGENT_INHERIT_DENY_TOOLS)。
  • 摘要提示:工具可用时,在摘要后追加一句回查提示。只在发给模型前拼接,不写入检查点。

刻意未改

契约取舍

一个 sidecar 进程承载多个会话 runtime,线路上没有单个 runtime 的身份,可信关联只能做到"嵌入宿主交给本 sidecar 的会话"这一层。同一 sidecar 内 runtime 之间的隔离,仍依赖 runtime 自己绑定 sessionId。

验证

  • host-core:新增 8 项通过。全量 1160 通过、1 忽略、1 失败:reviews_rpc_never_follows_redirects 在全量运行时失败,单独重跑通过,与本改动无关。cargo fmt --check 通过,Clippy 未报新文件问题。
  • host-runtime:133 项通过,含新增会话归属用例。
  • agent-runtime:1534 项通过,含新增 5 项。
  • shared 1369 项通过;desktop 的 rpc-lifecycle-contract、browser-preview-tool 共 20 项通过。
  • 类型检查、pnpm lint、git diff --check、文档检查(645 页)通过。

未调用真实模型,没有动安装版。还没验证的:

  • 几百 MB 长历史下的扫描耗时,以及期间其他会话的延迟;
  • 锁外读取遇到并发写入或删除时的真实竞态(单测覆盖了重写和文件缺失,没做并发压测);
  • 真实模型在摘要缺信息时会不会主动回查。

🤖 Generated with Claude Code

macheng and others added 3 commits October 30, 2026 16:18
Preserve the proposed history lookup, file restoration, and cache reuse design before incorporating review feedback.
After a compaction the model keeps only the summary and a recent tail,
so an exact snippet the summary dropped (code, error text, the user's
wording) was gone for good. This adds R21.1, a bounded read-only lookup
of the transcript at or before the active checkpoint.

host-core serves session.historyRead from a layout snapshot outside the
state lock, on a blocking thread, with stable message-id cursors so a
rewritten transcript invalidates a cursor instead of reusing offsets. It
returns a model-facing projection without thinking, sub-agent rows or
attachment bodies, and bounds page size, output and keyword scans.

The sidecar host admits the method only for sessions the embedding host
launched into that sidecar. The runtime registers ContextHistory as an
on-demand tool next to new_context, binds the session itself, pins
cursors to their checkpoint, and adds a lookup hint to the summary only
while the tool is callable. Delegates never inherit it.

The plan is confirmed as R21 with validation V60; R21.2 and R21.3 are
not implemented here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kylinmac
kylinmac merged commit e58c6d8 into main Oct 30, 2026
9 checks passed
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.

1 participant