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
+225 -61
View File
@@ -1,7 +1,8 @@
import test from "node:test";
import assert from "node:assert/strict";
import { performance } from "node:perf_hooks";
import piCompaction from "../index.ts";
import { getCompactionService } from "../src/service.ts";
import { MASK_PREFIX } from "../src/masking.ts";
function harness() {
const handlers = new Map<string, Function[]>();
@@ -24,90 +25,253 @@ function harness() {
return { pi, handlers, tools, commands, entries };
}
function context(branch: any[] = []) {
return {
function context(branch: any[] = [], tokens = 1_000) {
const compactCalls: any[] = [];
const sessionId = `session-${Math.random().toString(36).slice(2)}`;
const ctx = {
sessionManager: {
getSessionId: () => "session-test",
getSessionId: () => sessionId,
getBranch: () => branch,
getLeafId: () => "leaf-1",
},
model: { provider: "test", api: "test", id: "model", contextWindow: 100_000 },
modelRegistry: {},
getSystemPrompt: () => "system",
getContextUsage: () => ({ tokens: 1_000, contextWindow: 100_000, percent: 1 }),
getContextUsage: () => ({ tokens, contextWindow: 100_000, percent: 1 }),
compact(options: unknown) { compactCalls.push(options); },
isIdle: () => true,
hasUI: false,
abort() {},
} as any;
return { ctx, compactCalls };
}
test("one runtime projects tool evidence and recovers it through context_tree_query", async () => {
function projectedEntry(id: string, message: any) {
return { sourceEntry: { id, type: "message", message }, messages: [message] };
}
function assistantMessage(id: string, calls: Array<{ id: string; name: string; args?: unknown }>) {
return {
role: "assistant",
content: calls.map((call) => ({ type: "toolCall", id: call.id, name: call.name, arguments: call.args ?? {} })),
timestamp: id,
};
}
function toolResultMessage(id: string, toolCallId: string, text: string, toolName = "bash") {
return {
role: "toolResult",
toolCallId,
toolName,
content: [{ type: "text", text }],
timestamp: id,
};
}
// Default config: masking trigger = min(200K, 60% of window); at the harness
// 100K window that is 60K tokens and the target is 35K, so 60K used needs
// ~25K tokens of maskable results outside the 10-turn protected window.
function pressurizedContextEntries() {
const entries = [
projectedEntry("a1", assistantMessage("a1", [{ id: "c1", name: "bash", args: { command: "npm test" } }])),
projectedEntry("r1", toolResultMessage("r1", "c1", `failure evidence\n${"x".repeat(120_000)}\nstack tail`)),
];
for (let index = 0; index < 10; index++) {
entries.push(projectedEntry(`keep-${index}`, assistantMessage(`keep-${index}`, [])));
}
return entries;
}
test("turn_end masks old tool results under pressure and preserves prior drafts", async () => {
const h = harness();
piCompaction(h.pi);
const ctx = context();
const { ctx } = context([], 60_000);
await h.handlers.get("session_start")![0]!({}, ctx);
const assistant = {
role: "assistant",
content: [{ type: "toolCall", id: "call-1", name: "bash", arguments: { command: "npm test" } }],
timestamp: 10,
};
const result = {
role: "toolResult",
toolCallId: "call-1",
toolName: "bash",
content: [{ type: "text", text: `failure evidence\n${"x".repeat(12_000)}\nstack tail` }],
isError: true,
timestamp: 11,
};
await h.handlers.get("turn_end")![0]!({ message: assistant, toolResults: [result] }, ctx);
const projected = await h.handlers.get("context")![0]!({ messages: [assistant, result] }, ctx);
const prior = { type: "custom", customType: "other-extension", data: {} };
const result = await h.handlers.get("turn_end")![0]!({
entries: [prior],
message: assistantMessage("a1", []),
toolResults: [],
context: { contextEntries: pressurizedContextEntries(), contextMessages: [] },
}, ctx);
assert.match(projected.messages[1].content[0].text, /pi-compaction tool=bash/);
const cacheStart = performance.now();
for (let iteration = 0; iteration < 1_000; iteration++) {
await h.handlers.get("context")![0]!({ messages: [assistant, result] }, ctx);
}
const cacheAverageMs = (performance.now() - cacheStart) / 1_000;
assert.ok(cacheAverageMs < 2, `full context handler cache-hit average ${cacheAverageMs.toFixed(3)}ms exceeded 2ms`);
assert.equal(h.entries.length, 1);
assert.equal(h.entries[0]!.customType, "pi-compaction");
const query = h.tools.get("context_tree_query");
const recovered = await query.execute("q", { toolCallIds: ["call-1"] }, new AbortController().signal, undefined, ctx);
assert.match(recovered.content[0].text, /failure evidence/);
assert.ok(result?.entries);
assert.strictEqual(result.entries[0], prior);
const edits = result.entries.slice(1);
assert.equal(edits.length, 1);
assert.equal(edits[0]!.type, "context_edit");
assert.equal(edits[0]!.targetId, "r1");
assert.ok(edits[0]!.replacement.content.startsWith(MASK_PREFIX));
// Masking is append-only context_edit; nothing is written as custom entries.
assert.equal(h.entries.length, 0);
await h.handlers.get("session_shutdown")![0]!({}, ctx);
});
test("small tool results are passed through without a stub wrapper", async () => {
test("turn_end below the masking trigger returns undefined", async () => {
const h = harness();
piCompaction(h.pi);
const ctx = context();
const { ctx } = context([], 40_000);
await h.handlers.get("session_start")![0]!({}, ctx);
const assistant = {
role: "assistant",
content: [{ type: "toolCall", id: "call-small", name: "bash", arguments: { command: "ls" } }],
timestamp: 20,
};
const result = {
role: "toolResult",
toolCallId: "call-small",
toolName: "bash",
content: [{ type: "text", text: "ok\nfile-a.ts\nfile-b.ts" }],
timestamp: 21,
};
const result = await h.handlers.get("turn_end")![0]!({
entries: [],
message: assistantMessage("a1", []),
toolResults: [],
context: { contextEntries: pressurizedContextEntries(), contextMessages: [] },
}, ctx);
assert.equal(result, undefined);
await h.handlers.get("session_shutdown")![0]!({}, ctx);
});
await h.handlers.get("turn_end")![0]!({ message: assistant, toolResults: [result] }, ctx);
const projected = await h.handlers.get("context")![0]!({ messages: [assistant, result] }, ctx);
test("context hook returns messages untouched on the non-checkpoint path", async () => {
const h = harness();
piCompaction(h.pi);
const { ctx } = context();
await h.handlers.get("session_start")![0]!({}, ctx);
assert.strictEqual(projected.messages[1].content[0].text, "ok\nfile-a.ts\nfile-b.ts");
assert.doesNotMatch(projected.messages[1].content[0].text, /pi-compaction tool=/);
const messages = [assistantMessage("a1", []), toolResultMessage("r1", "c1", "visible\nfile-a.ts")];
const projected = await h.handlers.get("context")![0]!({ messages }, ctx);
assert.equal(projected, undefined);
await h.handlers.get("session_shutdown")![0]!({}, ctx);
});
test("agent_settled compacts once above the summary threshold and never repeats on the same leaf", async () => {
const h = harness();
piCompaction(h.pi);
const { ctx, compactCalls } = context([], 95_000);
await h.handlers.get("session_start")![0]!({}, ctx);
h.handlers.get("agent_settled")![0]!({ type: "agent_settled", aborted: false }, ctx);
assert.equal(compactCalls.length, 1);
assert.equal(typeof compactCalls[0].onError, "function");
h.handlers.get("agent_settled")![0]!({ type: "agent_settled", aborted: false }, ctx);
assert.equal(compactCalls.length, 1);
ctx.sessionManager.getLeafId = () => "leaf-2";
h.handlers.get("agent_settled")![0]!({ type: "agent_settled", aborted: false }, ctx);
assert.equal(compactCalls.length, 2);
await h.handlers.get("session_shutdown")![0]!({}, ctx);
});
test("agent_settled stays idle below the summary threshold", async () => {
const h = harness();
piCompaction(h.pi);
const { ctx, compactCalls } = context([], 50_000);
await h.handlers.get("session_start")![0]!({}, ctx);
h.handlers.get("agent_settled")![0]!({ type: "agent_settled", aborted: false }, ctx);
assert.equal(compactCalls.length, 0);
await h.handlers.get("session_shutdown")![0]!({}, ctx);
});
test("context_tree_query recovers the original tool output from the branch", async () => {
const h = harness();
piCompaction(h.pi);
const branch = [
{ type: "message", id: "a1", parentId: null, timestamp: "t", message: assistantMessage("a1", [{ id: "c1", name: "bash", args: { command: "npm test" } }]) },
{ type: "message", id: "r1", parentId: "a1", timestamp: "t", message: toolResultMessage("r1", "c1", "failure evidence\nstack tail") },
];
const { ctx } = context(branch);
await h.handlers.get("session_start")![0]!({}, ctx);
// Original evidence stays recoverable even when the message is passed through.
const query = h.tools.get("context_tree_query")!;
const recovered = await query.execute("q", { toolCallIds: ["call-small"] }, new AbortController().signal, undefined, ctx);
assert.match(recovered.content[0].text, /file-a\.ts/);
const recovered = await query.execute("q", { toolCallIds: ["c1"] }, new AbortController().signal, undefined, ctx);
assert.match(recovered.content[0].text, /failure evidence/);
assert.match(recovered.content[0].text, /stack tail/);
assert.deepEqual(recovered.details.missing, []);
const missing = await query.execute("q", { toolCallIds: ["nope"] }, new AbortController().signal, undefined, ctx);
assert.match(missing.content[0].text, /not found/);
await h.handlers.get("session_shutdown")![0]!({}, ctx);
});
test("service query returns placeholders only for masked tool call ids", async () => {
const h = harness();
piCompaction(h.pi);
const { ctx } = context([], 60_000);
await h.handlers.get("session_start")![0]!({}, ctx);
const sessionId = ctx.sessionManager.getSessionId();
await h.handlers.get("turn_end")![0]!({
entries: [],
message: assistantMessage("a1", []),
toolResults: [],
context: { contextEntries: pressurizedContextEntries(), contextMessages: [] },
}, ctx);
const records = getCompactionService()!.query(sessionId, ["c1", "never-masked"]);
assert.equal(records.length, 1);
assert.equal(records[0]!.toolCallId, "c1");
assert.ok(records[0]!.deterministicSummary.startsWith(MASK_PREFIX));
assert.equal(typeof records[0]!.resultTimestamp, "number");
await h.handlers.get("session_shutdown")![0]!({}, ctx);
});
test("hydrating a branch with this extension's context_edit rebuilds masking metrics", async () => {
const h = harness();
piCompaction(h.pi);
const placeholder = `${MASK_PREFIX}bash result cleared to save context (~1_000 tokens). Call args: {}. Re-run the call if still needed, or recover the original with context_tree_query {"toolCallIds":["c1"]}.`;
const branch = [
{ type: "message", id: "a1", parentId: null, timestamp: "t", message: assistantMessage("a1", [{ id: "c1", name: "bash", args: { command: "npm test" } }]) },
{ type: "message", id: "r1", parentId: "a1", timestamp: "t", message: toolResultMessage("r1", "c1", "x".repeat(8_000)) },
{ type: "context_edit", id: "e1", parentId: "r1", timestamp: "t", targetId: "r1", replacement: { content: placeholder } },
];
const { ctx } = context(branch);
await h.handlers.get("session_start")![0]!({}, ctx);
const sessionId = ctx.sessionManager.getSessionId();
await h.handlers.get("context")![0]!({ messages: [toolResultMessage("r1", "c1", "x".repeat(8_000))] }, ctx);
const metrics = getCompactionService()!.getMetrics(sessionId);
assert.equal(metrics!.maskedResults, 1);
assert.ok(metrics!.reclaimedTokens > 0);
assert.ok(metrics!.rawTokens > metrics!.effectiveTokens);
await h.handlers.get("session_shutdown")![0]!({}, ctx);
});
test("session_before_compact no longer cancels threshold compactions", async () => {
const h = harness();
piCompaction(h.pi);
const { ctx } = context();
await h.handlers.get("session_start")![0]!({}, ctx);
ctx.modelRegistry.getApiKeyAndHeaders = async () => ({ ok: false, error: "no credentials" });
const result = await h.handlers.get("session_before_compact")![0]!({
reason: "threshold", branchEntries: [], preparation: { firstKeptEntryId: "u1" },
signal: new AbortController().signal,
}, ctx);
assert.equal(result, undefined);
await h.handlers.get("session_shutdown")![0]!({}, ctx);
});
test("native checkpoint projection applies context edits made after the checkpoint", async () => {
const h = harness();
piCompaction(h.pi);
const details = {
kind: "pi-compaction",
version: 1,
checkpoint: "native",
modelKey: "test:test:model",
replacementHistory: [{ type: "compaction", encrypted_content: "opaque" }],
portableSummary: "checkpoint summary",
sourceFingerprint: "fp",
};
const branch = [
{ type: "compaction", id: "c1", parentId: null, timestamp: "t", summary: "marker", firstKeptEntryId: "r1", tokensBefore: 1_000, details },
{ type: "message", id: "r1", parentId: "c1", timestamp: "t", message: toolResultMessage("r1", "c1", "ORIGINAL-CONTENT-HERE") },
{ type: "context_edit", id: "e1", parentId: "r1", timestamp: "t", targetId: "r1", replacement: { content: `${MASK_PREFIX}bash result cleared to save context (~5 tokens). Call args: {}.` } },
{ type: "message", id: "u1", parentId: "e1", timestamp: "t", message: { role: "user", content: "next question", timestamp: 30 } },
] as any[];
const { ctx } = context(branch);
await h.handlers.get("session_start")![0]!({}, ctx);
const projected = await h.handlers.get("context")![0]!({ messages: [] }, ctx);
const serialized = JSON.stringify(projected.messages);
assert.doesNotMatch(serialized, /ORIGINAL-CONTENT-HERE/);
assert.match(serialized, /result cleared to save context/);
assert.match(serialized, /next question/);
await h.handlers.get("session_shutdown")![0]!({}, ctx);
});
@@ -127,7 +291,7 @@ test("model switching after a native checkpoint projects the portable checkpoint
{ type: "compaction", id: "c1", parentId: null, timestamp: new Date().toISOString(), summary: "marker", firstKeptEntryId: "m0", tokensBefore: 1_000, details },
{ type: "message", id: "m1", parentId: "c1", timestamp: new Date().toISOString(), message: { role: "user", content: "continue", timestamp: 20 } },
] as any[];
const ctx = context(branch);
const { ctx } = context(branch);
await h.handlers.get("session_start")![0]!({}, ctx);
const projected = await h.handlers.get("context")![0]!({ messages: [] }, ctx);
assert.match(projected.messages[0].content[0].text, /decisions and files/);
@@ -143,7 +307,7 @@ for (const provider of ["sub-api", "openai-codex"]) {
type: "message", id: "u1", parentId: null,
message: { role: "user", content: "Remember the test decision.", timestamp: 1 },
}];
const ctx = context(branch);
const { ctx } = context(branch);
ctx.model = {
provider, api: provider === "sub-api" ? "openai-responses" : "openai-codex-responses",
id: "selected-model", input: ["text"], contextWindow: 100_000,
@@ -231,7 +395,7 @@ test("SubAPI native failures and cancellation return control to Pi without a che
type: "message", id: "u1", parentId: null,
message: { role: "user", content: "Test only.", timestamp: 1 },
}];
const ctx = context(branch);
const { ctx } = context(branch);
ctx.model = { provider: "sub-api", api: "openai-responses", id: "test-model", input: ["text"], baseUrl: "https://gateway.example/v1" };
ctx.modelRegistry.getApiKeyAndHeaders = async () => ({ ok: true, apiKey: "test-key" });
const responses = [
@@ -266,7 +430,7 @@ test("SubAPI native failures and cancellation return control to Pi without a che
test("other providers and SubAPI Chat Completions keep their provider requests unchanged", async () => {
const h = harness();
piCompaction(h.pi);
const ctx = context();
const { ctx } = context();
try {
for (const model of [
{ provider: "other", api: "openai-responses", id: "test" },
@@ -302,7 +466,7 @@ test("native runtime delegates authentication to each provider without blocking
type: "message", id: "u1", parentId: null,
message: { role: "user", content: "Synthetic authentication test.", timestamp: 1 },
}];
const ctx = context(branch);
const { ctx } = context(branch);
const provider = scenario.provider ?? "sub-api";
ctx.model = {
provider, api: provider === "sub-api" ? "openai-responses" : "openai-codex-responses",