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
+29
View File
@@ -0,0 +1,29 @@
import test from "node:test";
import assert from "node:assert/strict";
import { performance } from "node:perf_hooks";
import { DEFAULT_CONFIG } from "../src/config.ts";
import { SegmentIndex } from "../src/segment-index.ts";
function message(index: number) {
return { role: "user", content: `message-${index}`, timestamp: index + 1 };
}
test("cached projection and append-only delta stay inside the hot-path budget", () => {
const index = new SegmentIndex("perf");
index.hydrate([], DEFAULT_CONFIG);
const messages = Array.from({ length: 10_000 }, (_, value) => message(value));
const params = { environmentHash: "env", environmentTokens: 2_000, contextWindow: 300_000 };
index.project(messages, params);
const cacheStart = performance.now();
for (let iteration = 0; iteration < 1_000; iteration++) index.project(messages, params);
const cacheAverageMs = (performance.now() - cacheStart) / 1_000;
const appended = [...messages, message(10_000)];
const deltaStart = performance.now();
index.project(appended, params);
const deltaMs = performance.now() - deltaStart;
assert.ok(cacheAverageMs < 2, `cache-hit average ${cacheAverageMs.toFixed(3)}ms exceeded 2ms`);
assert.ok(deltaMs < 10, `delta projection ${deltaMs.toFixed(3)}ms exceeded 10ms`);
});