refactor(pi-compaction): 改为压力触发的观察遮蔽与空闲阈值整段摘要

原实现对所有工具结果(含 read)在下一次请求即替换为约 4K 的首尾存根,
不看上下文压力,导致模型看不到大文件中段;后台逐条语义摘要又反复改写
历史、打碎 prompt 缓存。参照 Anthropic 上下文编辑、Manus 与 JetBrains
观察遮蔽研究重构为:
- L2:上下文达到阈值时在 turn_end 批量提交 Pi 原生 context_edit,把最近
  keepRecentTurns 轮之外的旧 toolResult 换成确定性占位符;trigger/target
  回差与 minClearTokens 保证少触发、一次清够;原始条目保留,可经
  context_tree_query 取回
- L3:空闲 agent_settled 且超过绝对阈值时调用一次 ctx.compact
- 阈值按绝对 token 计并受窗口比例约束,默认针对 1M 窗口(DeepSeek V4.1
  Flash):遮蔽 200K→100K、整段摘要 400K
- 删除逐条存根、语义 worker、soft/hard/target 比例与自行取消 Pi 压缩的门控;
  Pi 原生阈值压缩保留为最终安全网

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
云服务部-叶林立
2026-10-21 19:51:36 -07:00
co-authored by Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
parent a254d4fd68
commit a6d8416c76
21 changed files with 1174 additions and 804 deletions
+11 -13
View File
@@ -1,19 +1,17 @@
import test from "node:test";
import assert from "node:assert/strict";
import { shouldCompact } from "../src/token-accounting.ts";
import { effectiveThreshold } from "../src/token-accounting.ts";
test("raw pressure cannot trigger compaction while effective lower-bound stays below 90%", () => {
const config = { hardRatio: 0.9 };
const snapshot = { lowerBound: 231_000, contextWindow: 300_000 };
assert.equal(shouldCompact("threshold", snapshot, config), false);
assert.equal(shouldCompact("manual", snapshot, config), true);
assert.equal(shouldCompact("overflow", snapshot, config), true);
test("absolute token threshold wins on large windows", () => {
assert.equal(effectiveThreshold(200_000, 0.6, 1_000_000), 200_000);
});
test("effective lower-bound at the configured threshold compacts", () => {
assert.equal(shouldCompact(
"threshold",
{ lowerBound: 270_000, contextWindow: 300_000 },
{ hardRatio: 0.9 },
), true);
test("window ratio caps the threshold on small windows", () => {
assert.equal(effectiveThreshold(200_000, 0.6, 272_000), 163_200);
assert.equal(effectiveThreshold(100_000, 0.35, 272_000), 95_200);
});
test("a missing context window falls back to the absolute threshold", () => {
assert.equal(effectiveThreshold(200_000, 0.6, 0), 200_000);
assert.equal(effectiveThreshold(200_000, 0.6, -1), 200_000);
});