Compare commits

...
2 changed files with 18 additions and 5 deletions
@@ -1028,7 +1028,7 @@ describe("createContextCompactionPrepareTurn", () => {
]);
});
it("caps the default trigger at 90 percent of the context window", async () => {
it("targets automatic compaction at 50 percent of the context window", async () => {
const compact = vi.fn((_context: CoreCompactionContext) => ({
messages: [{ role: "user" as const, content: "Compacted by ratio" }],
}));
@@ -1069,8 +1069,8 @@ describe("createContextCompactionPrepareTurn", () => {
expect(createHandlerMock).not.toHaveBeenCalled();
expect(compact).toHaveBeenCalledTimes(1);
const context = compact.mock.calls[0]?.[0];
expect(context?.triggerTokens).toBe(180_000);
expect(context?.thresholdRatio).toBe(0.9);
expect(context?.triggerTokens).toBe(100_000);
expect(context?.thresholdRatio).toBe(0.5);
expect(result?.messages).toEqual([
{ role: "user", content: "Compacted by ratio" },
]);
@@ -1139,7 +1139,7 @@ describe("createContextCompactionPrepareTurn", () => {
expect(createHandlerMock).not.toHaveBeenCalled();
expect(compact).toHaveBeenCalledTimes(1);
const context = compact.mock.calls[0]?.[0];
expect(context?.triggerTokens).toBe(244_800);
expect(context?.triggerTokens).toBe(136_000);
expect(context?.utilizationRatio).toBeGreaterThan(0.9);
expect(result?.messages).toEqual([
{ role: "user", content: "Compacted provider payload" },
@@ -68,6 +68,8 @@ export interface ContextCompactionPrepareTurnOptions {
manualTargetRatio?: number;
}
const AUTO_TARGET_RATIO = 0.5;
function safeJsonSize(value: unknown): number {
try {
return JSON.stringify(value).length;
@@ -312,7 +314,18 @@ export function createContextCompactionPrepareTurn(
autoTriggerTokens: triggerState.triggerTokens,
manualTargetRatio: options.manualTargetRatio,
})
: triggerState;
: {
triggerTokens: Math.max(
0,
Math.floor(
Math.min(
triggerState.triggerTokens,
maxInputTokens * AUTO_TARGET_RATIO,
),
),
),
thresholdRatio: AUTO_TARGET_RATIO,
};
const compactionContext = {
agentId: context.agentId,