feat(condense): tune default pruning policy

This commit is contained in:
云服务部-叶林立
2026-08-28 12:14:19 +08:00
parent 2dcec0207c
commit 0c80a8b537
5 changed files with 81 additions and 21 deletions
+55 -13
View File
@@ -4,7 +4,20 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import test from "node:test";
import { ensureCondenseEnabledDefault } from "../extensions/condense-config.ts";
import { ensureCondenseDefaults } from "../extensions/condense-config.ts";
const EXPECTED_DEFAULTS = {
enabled: true,
pruneOn: "agent-message",
batchingMode: "turn",
autoBudgetThreshold: 0.735,
budgetTurnDelta: 0.04,
summarizerModel: "default",
summarizerThinking: "default",
summarizerIdleTimeoutMs: 90_000,
summarizerMaxTimeoutMs: 300_000,
quietOversizedSkips: false,
};
async function withAgentDir(run: (agentDir: string) => Promise<void>): Promise<void> {
const agentDir = await mkdtemp(join(tmpdir(), "my-pi-condense-"));
@@ -15,42 +28,71 @@ async function withAgentDir(run: (agentDir: string) => Promise<void>): Promise<v
}
}
test("pi-condense defaults to enabled when contextPrune is absent", async () => {
test("pi-condense writes the complete bundle defaults when contextPrune is absent", async () => {
await withAgentDir(async (agentDir) => {
assert.equal(ensureCondenseEnabledDefault(agentDir), "updated");
assert.equal(ensureCondenseDefaults(agentDir), "updated");
assert.deepEqual(JSON.parse(await readFile(join(agentDir, "settings.json"), "utf8")), {
contextPrune: { enabled: true },
contextPrune: EXPECTED_DEFAULTS,
});
});
});
test("pi-condense default preserves existing settings and contextPrune fields", async () => {
test("pi-condense fills only missing defaults and preserves unrelated settings", async () => {
await withAgentDir(async (agentDir) => {
const path = join(agentDir, "settings.json");
await writeFile(path, JSON.stringify({ theme: "dark", contextPrune: { minBatchChars: 4000 } }), "utf8");
assert.equal(ensureCondenseEnabledDefault(agentDir), "updated");
await writeFile(
path,
JSON.stringify({ theme: "dark", contextPrune: { minBatchChars: 4000, budgetTurnDelta: 0.03 } }),
"utf8",
);
assert.equal(ensureCondenseDefaults(agentDir), "updated");
assert.deepEqual(JSON.parse(await readFile(path, "utf8")), {
theme: "dark",
contextPrune: { minBatchChars: 4000, enabled: true },
contextPrune: { ...EXPECTED_DEFAULTS, minBatchChars: 4000, budgetTurnDelta: 0.03 },
});
});
});
test("pi-condense default respects an explicit enabled choice", async () => {
test("pi-condense preserves an explicit disabled choice while filling other defaults", async () => {
await withAgentDir(async (agentDir) => {
const path = join(agentDir, "settings.json");
const original = `${JSON.stringify({ contextPrune: { enabled: false } }, null, 2)}\n`;
await writeFile(path, JSON.stringify({ contextPrune: { enabled: false } }), "utf8");
assert.equal(ensureCondenseDefaults(agentDir), "updated");
assert.deepEqual(JSON.parse(await readFile(path, "utf8")), {
contextPrune: { ...EXPECTED_DEFAULTS, enabled: false },
});
});
});
test("pi-condense does not rewrite a complete explicit configuration", async () => {
await withAgentDir(async (agentDir) => {
const path = join(agentDir, "settings.json");
const original = `${JSON.stringify(
{ contextPrune: { ...EXPECTED_DEFAULTS, enabled: false, quietOversizedSkips: true } },
null,
2,
)}\n`;
await writeFile(path, original, "utf8");
assert.equal(ensureCondenseEnabledDefault(agentDir), "unchanged");
assert.equal(ensureCondenseDefaults(agentDir), "unchanged");
assert.equal(await readFile(path, "utf8"), original);
});
});
test("pi-condense default does not overwrite malformed settings", async () => {
test("pi-condense defaults do not overwrite malformed settings", async () => {
await withAgentDir(async (agentDir) => {
const path = join(agentDir, "settings.json");
await writeFile(path, "{not-json", "utf8");
assert.equal(ensureCondenseEnabledDefault(agentDir), "skipped-invalid");
assert.equal(ensureCondenseDefaults(agentDir), "skipped-invalid");
assert.equal(await readFile(path, "utf8"), "{not-json");
});
});
test("pi-condense defaults reject a non-object contextPrune value", async () => {
await withAgentDir(async (agentDir) => {
const path = join(agentDir, "settings.json");
const original = `${JSON.stringify({ contextPrune: false }, null, 2)}\n`;
await writeFile(path, original, "utf8");
assert.equal(ensureCondenseDefaults(agentDir), "skipped-invalid");
assert.equal(await readFile(path, "utf8"), original);
});
});