feat: unify context compaction

This commit is contained in:
云服务部-叶林立
2026-09-01 14:52:43 +08:00
parent 44f27f7585
commit a4807dde67
114 changed files with 2681 additions and 21336 deletions
+84
View File
@@ -0,0 +1,84 @@
import test from "node:test";
import assert from "node:assert/strict";
import { DEFAULT_CONFIG } from "../src/config.ts";
import { SegmentIndex, deterministicSummary } from "../src/segment-index.ts";
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\n${"x".repeat(12_000)}\nstack tail` }],
isError: true,
timestamp: 11,
};
test("deterministic summary is bounded and preserves head/tail evidence", () => {
const summary = deterministicSummary({
toolName: "bash",
args: { command: "npm test" },
rawText: result.content[0]!.text,
isError: true,
maxStubTokens: 256,
});
assert.match(summary, /tool=bash status=error/);
assert.match(summary, /failure/);
assert.match(summary, /stack tail/);
assert.ok(summary.length <= 1_100);
});
test("projection incrementally replaces tool results and caches identical input", () => {
const index = new SegmentIndex("session-1");
index.hydrate([], DEFAULT_CONFIG);
const records = index.captureTurn(assistant, [result], DEFAULT_CONFIG);
assert.equal(records.length, 1);
const messages = [assistant, result];
const first = index.project(messages, {
environmentHash: "env",
environmentTokens: 100,
contextWindow: 300_000,
});
const second = index.project(messages, {
environmentHash: "env",
environmentTokens: 100,
contextWindow: 300_000,
});
assert.strictEqual(first, second);
assert.ok(first.effectiveTokens < first.rawTokens);
assert.match(first.messages[1]!.content[0].text, /context_tree_query/);
const record = records[0]!;
assert.ok(index.applySemantic(record.sourceKey, record.sourceHash, "- npm test failed\n- stack tail"));
const semantic = index.project(messages, {
environmentHash: "env",
environmentTokens: 100,
contextWindow: 300_000,
});
assert.notStrictEqual(semantic, first);
assert.match(semantic.messages[1]!.content[0].text, /npm test failed/);
});
test("usage-backed baseline is adjusted only by projected delta", () => {
const index = new SegmentIndex("session-2");
index.hydrate([], DEFAULT_CONFIG);
const initial = index.project([assistant], {
environmentHash: "env",
environmentTokens: 50,
contextWindow: 100_000,
});
index.recordUsage(1_000, initial.messageEstimate, "env");
const next = index.project([assistant, { role: "user", content: "next", timestamp: 12 }], {
environmentHash: "env",
environmentTokens: 50,
contextWindow: 100_000,
});
assert.equal(next.confidence, "usage-backed");
assert.ok(next.effectiveTokens >= 1_000);
assert.ok(next.effectiveTokens < 1_100);
});