Skip to content

feat: 上下文蒸发(Context Evaporation)P1-P3 - #31

Merged
Jaxton07 merged 4 commits into
mainfrom
feat/context-evaporation-core
Aug 26, 2026
Merged

feat: 上下文蒸发(Context Evaporation)P1-P3#31
Jaxton07 merged 4 commits into
mainfrom
feat/context-evaporation-core

Conversation

@Jaxton07

Copy link
Copy Markdown
Owner

概要

可再生的工具输出(上下文体积 ~87%)按四级水位线蒸发为自带恢复指令的 stub,替代性方案与 ACP 互斥并存(默认行为不变)。spec:.local/agent-work/spec/context-evaporation.md(v2)。

已 review 通过(REVIEW.md:ACP 空 diff、--core 同构独立复跑逐字节一致、t6085@256K 基线复现、typecheck/test 全绿)。分四个 commit(P1 核心 / P2 接线 / P3 trace / lint 顺手修),squash 合并为一个。

分阶段

  • P1 纯函数核心tools/context-evaporation/{types,estimate,evaporate}.ts 零 SDK/零仓库 import;决策单调持久化(full→snip→stub 只升不降,KV cache 第一约束);保护区(尾部 8000 token + 最近 isError + todo + compactionProtected);hysteresis 批停机在触发线−5%;replay --core 同构验证与离线基准逐字节一致(201 会话/11706 步)
  • P2 接线:config.ts 双 key 派生 + 单一写者原子双写(与 ACP 互斥全在蒸发侧,ACP 代码零改动,双开冲突 ACP 优先);InlineExtension(session_start 全新 Map / session_compact 重置 / liveEnabled 实时读 ≤2s 生效免重开会话);设置页「上下文管理」三态(IPC + store 乐观回滚 + i18n zh/en)
  • P3 traceSessionTraces.recordCustom(trace_custom 行,reducer no-op)+ 蒸发批次双通道观测(log + trace)
  • 核心修复(P2 期间发现):offset 锚定——真实 usage 与 wire 估算差值参与批停机尺度,防 usage≫估算时批空转;修复后同构验证重新通过

配置(方案 B 定稿)

tiers {60, 85, 95} @ min(contextWindow, 256K budget);绝对触发点 154K/218K/243K。默认 mode=acp,行为与现状一致(回归底线)。

自测

typecheck/lint/481+236 tests/build 全绿;CDP 冒烟 8/8(三态渲染/双写/派生回读/UI 态);replay-trace 对含自定义行 trace 重放安全;t6085@256K corpus 基线补跑(T1/T2/T3=25/3/0,误杀 u3=15 优于 prune=80 的 33)。

- tools/context-evaporation/{types,estimate,evaporate}.ts:零 SDK/零仓库
  import(除彼此);四级水位线决策机与 wire 重建,决策单调持久化
  (full→snip→stub 只升不降),保护区/红线/hysteresis 与 replay 口径
  逐字节对齐;context 钩子侧绝不 throw,未蒸发消息保持对象身份
- test/context-evaporation.test.ts:31 例(每层每 Tier 规则、stub 快照、
  单调性、红线、保护区按 token、hysteresis、Tier 累积、冷启动超集、
  tier2Scope、trimAssistantText、usage 口径)
- scripts/replay-evaporation.mts 入库(含 160K/256K budget 网格档):
  新增 --core 开关(mono 模拟改用蒸发核心,离线/在线同构验证)与
  t6085 档线(方案 B 定稿 60/85/95);同构验证 mono-p8k-t60-w128k
  基准重跑与既有结果逐字节一致(201 会话 / 11706 步)
- context-evaporation/config.ts:readEvapConfig(对象容错+默认值+2s TTL)/
  readContextManagerMode(双 key 派生,双开冲突 ACP 优先+warn,P4 翻转只改
  缺省常量)/ writeContextManagerMode(JsonStore updateSync 原子双写,写后清
  两侧缓存即效;只动双 key,数值子键与其余键保留)
- extension.ts:InlineExtension(session_start 全新 Map / context 主流程 /
  session_compact 重置;withLock;全 try/catch 绝不 throw;liveEnabled 实时读
  派生 mode ≤2s 生效;Tier 0/无变化返回 undefined 零干扰;不注册工具不改 prompt)
- evaporate.ts 补 offset 锚定:真实 usage 与 wire 估算的差值参与批停机尺度,
  防 usage(含 system prompt 等)≫ 估算时批空转;usageTokens=null 时 offset=0,
  replay --core 逐字节一致性已重验
- pi-backend.ts:工厂链 ACP 之后插入蒸发(arch §2.1 槽位);新增
  getContextManagerConfig/setContextManagerMode 薄委托;ACP 代码零改动
- shared + desktop:ContextManagerMode/ContextManagerConfigInfo +
  contextManager:getConfig/setMode 通道 + preload + settings store
  (乐观更新失败回滚)+ GeneralPanel 三态分段按钮(替换 ACP 开关行)+ i18n zh/en
- 测试:config 19 例(默认值/非法回退/mode 全分支/双写原子性/互斥集成)、
  extension 13 例(开关矩阵/窗口分母/offset/生命周期/契约)、renderer store 4 例
- scripts/smoke-evaporation-ui.mjs:CDP 冒烟(三态渲染/双写/派生回读/UI 态),实跑通过
- SessionTraces.recordCustom(sessionId, kind, data):合成 type=trace_custom 的
  观测行,走同一批量 flush/轮转/限额管道(复用 0.4.6 加固全部防线);不经
  emitEvent → 不进 IPC/UI,只落 trace 供灰度分析脚本直读
- 蒸发扩展 reporter 携带 sessionId(session_start 会话闭包,trace 按会话落盘);
  PiBackend 注入双通道:log(reportEvapBatch,快速 grep)+ trace_custom
  (evap_batch 载荷 = tier/usagePct/snipped/pruned/savedEst/wireEst/cacheHits/mapSize)
- reducer 对未知类型 no-op(既有行为),replay-trace.mts 重放含自定义行 trace
  安全(实测 no-op 计数正确);测试:trace_custom 行可 parse + reducer no-op、
  无 recorder 会话静默、extension reporter sessionId 断言
@Jaxton07
Jaxton07 merged commit 8d2a697 into main Aug 26, 2026
1 check passed
@Jaxton07
Jaxton07 deleted the feat/context-evaporation-core branch August 26, 2026 10:38
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