Add context compaction service: schema validation, token estimation, and message normalization with tests
CI / check (22) (push) Failing after 4m49s
CI / check (24) (push) Successful in 9m43s

This commit is contained in:
2026-10-16 16:00:52 +02:00
parent be4cce67f7
commit 448fe53811
2 changed files with 114 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
import test from "node:test";
import assert from "node:assert/strict";
import {buildCompactionRequest, estimateTokens, normalizeCompaction} from "../src/services/contextCompaction.js";
const valid = {
summary: "The user is implementing context compaction.",
current_goals: ["Keep recent messages verbatim"],
important_facts: [], decisions: ["Use the existing Ollama client"], constraints: [],
technical_state: ["The context model is configured separately"], open_tasks: [],
problems_and_solutions: [], user_preferences: [], important_exact_values: ["CONTEXT_MODEL"]
};
test("compaction output is strictly validated and bounded", () => {
assert.deepEqual(normalizeCompaction(JSON.stringify(valid), 5000), valid);
assert.equal(normalizeCompaction("not json"), null);
assert.equal(normalizeCompaction(JSON.stringify({...valid, summary: "", current_goals: [], important_facts: [], decisions: [], constraints: [], technical_state: [], open_tasks: [], problems_and_solutions: [], user_preferences: [], important_exact_values: []}), 5000), null);
assert.equal(normalizeCompaction(JSON.stringify(valid), 10), null);
});
test("compaction request merges existing state with only expired records", () => {
const request = buildCompactionRequest("existing fact", [{role: "user", content: "old request"}]);
assert.match(request, /existing fact/);
assert.match(request, /old request/);
assert.match(request, /Newly expired conversation records/);
assert.equal(estimateTokens("1234"), 1);
});