Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
5.3 kB · 142 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143import { describe, expect, it } from "vitest";import { type CompactionSettings, parseCompactCommandArgs, shouldCompact } from "../src/core/compaction/index.js";import { SettingsManager } from "../src/core/settings-manager.js";
describe("compaction threshold command", () => { describe("SettingsManager compaction settings", () => { it("supports getting and setting maxContextTokens", () => { const sm = SettingsManager.inMemory(); expect(sm.getCompactionMaxContextTokens()).toBeUndefined(); expect(sm.getCompactionSettings().maxContextTokens).toBeUndefined();
sm.setCompactionMaxContextTokens(600000); expect(sm.getCompactionMaxContextTokens()).toBe(600000); expect(sm.getCompactionSettings().maxContextTokens).toBe(600000);
sm.setCompactionMaxContextTokens(undefined); expect(sm.getCompactionMaxContextTokens()).toBeUndefined(); expect(sm.getCompactionSettings().maxContextTokens).toBeUndefined(); }); });
describe("parseCompactCommandArgs", () => { it("parses immediate compaction with and without instructions", () => { expect(parseCompactCommandArgs("")).toEqual({ kind: "immediate" }); expect(parseCompactCommandArgs("focus on bug")).toEqual({ kind: "immediate", instructions: "focus on bug", }); });
it("parses threshold specification in tokens", () => { expect(parseCompactCommandArgs("at 600k")).toEqual({ kind: "set_threshold", threshold: 600000, rawValue: "600k", isPercent: false, global: false, instructions: undefined, });
expect(parseCompactCommandArgs("at 600K --global")).toEqual({ kind: "set_threshold", threshold: 600000, rawValue: "600K", isPercent: false, global: true, instructions: undefined, });
expect(parseCompactCommandArgs("at 1m")).toEqual({ kind: "set_threshold", threshold: 1000000, rawValue: "1m", isPercent: false, global: false, instructions: undefined, });
expect(parseCompactCommandArgs("at 200,000")).toEqual({ kind: "set_threshold", threshold: 200000, rawValue: "200,000", isPercent: false, global: false, instructions: undefined, }); });
it("parses threshold specification in percentage", () => { expect(parseCompactCommandArgs("at 60%", 1000000)).toEqual({ kind: "set_threshold", threshold: 600000, rawValue: "60%", isPercent: true, global: false, instructions: undefined, }); });
it("parses threshold with trailing instructions", () => { expect(parseCompactCommandArgs("at 600k keep architectural decisions")).toEqual({ kind: "set_threshold", threshold: 600000, rawValue: "600k", isPercent: false, global: false, instructions: "keep architectural decisions", }); });
it("parses reset, off, clear, and default", () => { expect(parseCompactCommandArgs("at reset")).toEqual({ kind: "reset", global: false }); expect(parseCompactCommandArgs("at off")).toEqual({ kind: "reset", global: false }); expect(parseCompactCommandArgs("at clear")).toEqual({ kind: "reset", global: false }); expect(parseCompactCommandArgs("at default")).toEqual({ kind: "reset", global: false }); expect(parseCompactCommandArgs("at reset --global")).toEqual({ kind: "reset", global: true }); });
it("parses status queries", () => { expect(parseCompactCommandArgs("at")).toEqual({ kind: "status" }); expect(parseCompactCommandArgs("at status")).toEqual({ kind: "status" }); });
it("falls back to immediate compaction when 'at' is part of normal sentence", () => { expect(parseCompactCommandArgs("at the conclusion summarize all changes")).toEqual({ kind: "immediate", instructions: "at the conclusion summarize all changes", }); }); });
describe("auto-compaction thresholding logic", () => { const settings: CompactionSettings = { enabled: true, reserveTokens: 16384, keepRecentTokens: 20000, maxContextTokens: 600000, };
it("triggers compaction at maxContextTokens minus reserveTokens instead of 1M limit", () => { const contextWindow = 1000000; // Without override: threshold is 1,000,000 - 16,384 = 983,616 expect(shouldCompact(590000, contextWindow, { ...settings, maxContextTokens: undefined })).toBe(false); expect(shouldCompact(980000, contextWindow, { ...settings, maxContextTokens: undefined })).toBe(false); expect(shouldCompact(990000, contextWindow, { ...settings, maxContextTokens: undefined })).toBe(true);
// With override: threshold is 600,000 - 16,384 = 583,616 expect(shouldCompact(500000, contextWindow, settings)).toBe(false); expect(shouldCompact(580000, contextWindow, settings)).toBe(false); expect(shouldCompact(585000, contextWindow, settings)).toBe(true); expect(shouldCompact(600000, contextWindow, settings)).toBe(true); expect(shouldCompact(700000, contextWindow, settings)).toBe(true); });
it("clamps to contextWindow when model contextWindow is smaller than threshold", () => { // Model with 128k context window: effective window is 128k, not 600k (threshold: 128,000 - 16,384 = 111,616) const contextWindow = 128000; expect(shouldCompact(110000, contextWindow, settings)).toBe(false); expect(shouldCompact(115000, contextWindow, settings)).toBe(true); }); });});