Skip to content

fix(chat): update the context ring as soon as a compaction lands - #2018

Merged
vastsa merged 1 commit into
vastsa:mainfrom
zszz3:fix/compaction-context-usage
Oct 31, 2026
Merged

vastsa merged 1 commit into
vastsa:mainfrom
zszz3:fix/compaction-context-usage

Conversation

@zszz3

@zszz3 zszz3 commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

fixes #2015

问题

输入框右侧的上下文圆环读取的是最新一条助手消息里的供应商用量。压缩本身不会产生助手消息,所以 /compact 成功后,圆环还停在压缩前的数字,要等下一轮请求报告用量才会变。看起来像压缩没生效。

修改

  • runtime:persistCheckpoint 在安装检查点前本来就会估算压缩后的下一次请求大小(compactedBudget.tokens,用来判断是否超出安全预算)。现在把这个值写进检查点的 details.contextTokens。details 由 host 原样持久化,和 generation 一样,重开会话后也在。
  • shared:ContextCompactionStatus 增加可选的 contextTokens,contextCompactionMark() 从 details 读出。实时的 compaction_end.mark 和打开会话时重建的 mark 都会带上它。
  • renderer:latestTurnContextInspector() 从末尾往前找时,如果先遇到最新检查点的 throughMessageId,再遇到带用量的消息,说明这份用量是压缩前的。这时返回 compactedContextTokens,圆环和百分比用它计算。面板里的输入、输出、缓存这些供应商用量仍显示最后一次真实请求的数据。下一次请求报告用量后,自动换回真实数据。

兼容性

  • 只新增可选字段,没有改数据库结构,也不需要迁移。
  • 之前写入的检查点没有 contextTokens,圆环和现在的表现一样。

验证

  • 新增 apps/desktop/test/compaction-context-usage.test.mjs:
    • 压缩后圆环从 82% 变为 12%
    • 之后的请求有用量时换回真实数据
    • 旧检查点不变
    • 锚点不在消息里时忽略
  • 变异检查:把判断改为恒为 false 后,第一个用例失败。
  • runtime.test.ts 的阈值压缩用例:要求持久化的 details 和 compaction_end.mark 都带 contextTokens。改代码前这个用例失败,改后通过。
  • shared 测试:checkpointContextTokens 和 mark 都有覆盖。
  • 本地全部通过:pnpm build:js、desktop 和 agent-runtime 的 typecheck、pnpm lint、check-architecture、git diff --check。
  • 测试套件:desktop 4603 个,0 失败;shared 1346 个通过;agent-runtime 1632 个通过。
  • 文档:已更新 IPC 协议 spec(中英文)、E2E 计划的压缩场景(中英文)和 docs/project/unreleased.md。
  • 没有跑 Electron E2E,也没有用真实供应商手动验证。

The composer ring reads the newest assistant message's provider usage,
and a compaction produces no such message. After /compact the ring kept
the pre-compaction figure until another turn ran, which made a
successful compaction look like it had failed.

The runtime already estimates the compacted request before installing
a checkpoint. It now stamps that estimate into the checkpoint's opaque
details, so the mark on compaction_end and the marks rebuilt on session
open both carry it. The ring shows it while no request after the
checkpoint has reported usage; older checkpoints without the field keep
the previous behavior.

fixes vastsa#2015

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

已验证原问题真实存在,并完成候选验证,可以合入。

独立使用 main 和候选的生产 inspector 同一夹具比较:82% 的压缩前用量,在带 12000-token 检查点后,main 仍为 82%,候选为 12%。新增四个 renderer 用例及 shared 14 个用例通过;runtime.test.ts + context-usage-compaction.test.ts 共 398/398 通过,runtime build/typecheck 通过。

生产 sidecar + localhost Responses 网关三种离线场景通过(缓存输入、不完整计数、普通用量),额外断言持久化 checkpoint.details.contextTokens 为正且与 compaction_end.mark.contextTokens 一致。原 E2E fake-host 未适配新合入的 tools.fileBatchCapabilities,诊断中补了 supported:false 后通过;未改贡献者提交。没有真实供应商调用或本地 Electron UI E2E;远端六项检查全部通过,包含 Electron 和 Windows。

可选 metadata 保留旧检查点兼容性,live/reload 共用 mark,下一次真实用量恢复供应商值;改动范围与问题一致。最新 main 为 head 祖先,PR integration tree 与测试 head 相同 e46b12202da5eb85306488ce70c51dc874dd508c。本机旧依赖不兼容,已仅在隔离工作区安装锁定依赖;原工作区未修改。

@vastsa
vastsa merged commit c85251b into vastsa:main Oct 31, 2026
6 checks passed
@zszz3
zszz3 deleted the fix/compaction-context-usage branch October 31, 2026 06:11
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.

[Bug] 手动压缩上下文后,界面没有及时更新

2 participants