Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/compact-overflow-retry-budget.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@moonshot-ai/agent-core": patch
"@moonshot-ai/kimi-code": patch
---

Back off failed compaction retries by a fixed slice of the model context window.
17 changes: 15 additions & 2 deletions packages/agent-core/src/agent/compaction/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface CompactionConfig {
maxRecentMessages: number;
maxRecentUserMessages: number;
maxRecentSizeRatio: number;
minOverflowReductionRatio: number;
}

export const DEFAULT_COMPACTION_CONFIG: CompactionConfig = {
Expand All @@ -20,6 +21,7 @@ export const DEFAULT_COMPACTION_CONFIG: CompactionConfig = {
maxRecentMessages: 4,
maxRecentUserMessages: Infinity,
maxRecentSizeRatio: 0.2,
minOverflowReductionRatio: 0.05,
};

export interface CompactionStrategy {
Expand Down Expand Up @@ -117,12 +119,23 @@ export class DefaultCompactionStrategy implements CompactionStrategy {
}

reduceCompactOnOverflow(messages: readonly Message[]): number {
const minReducedSize = Math.max(
1,
Math.ceil(this.maxSize * this.config.minOverflowReductionRatio),
);
let reducedSize = 0;
let bestN: number | undefined;

for (let i = messages.length - 2; i > 0; i--) {
reducedSize += estimateTokensForMessage(messages[i + 1]!);
Comment thread
kermanx marked this conversation as resolved.
if (canSplitAfter(messages, i)) {
return i + 1;
bestN = i + 1;
if (reducedSize >= minReducedSize) {
return i + 1;
}
}
}
return messages.length;
return bestN ?? messages.length;
}

get checkAfterStep(): boolean {
Expand Down
22 changes: 22 additions & 0 deletions packages/agent-core/test/agent/compaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { afterEach, describe, expect, it, vi } from 'vitest';
import type { AgentOptions } from '../../src/agent';
import { DefaultCompactionStrategy, type CompactionStrategy } from '../../src/agent/compaction';
import { HookEngine, type HookEngineTriggerArgs } from '../../src/session/hooks';
import { estimateTokensForMessages } from '../../src/utils/tokens';
import { recordingTelemetry, type TelemetryRecord } from '../fixtures/telemetry';
import type { TestAgentContext, TestAgentOptions } from './harness/agent';
import { testAgent } from './harness/agent';
Expand Down Expand Up @@ -132,6 +133,24 @@ describe('Agent compaction', () => {
expect(strategy.shouldBlock(210_000)).toBe(true);
});

it('backs off overflow compaction by at least five percent of the context window', () => {
const strategy = testCompactionStrategy(1_000);
const messages = [
textMessage('user', 'old user'),
textMessage('assistant', 'old assistant'),
...Array.from({ length: 20 }, () => [
textMessage('user', 'continue'),
textMessage('assistant', ''),
]).flat(),
];

const reduced = strategy.reduceCompactOnOverflow(messages);
const removed = messages.slice(reduced);

expect(reduced).toBeGreaterThan(0);
expect(estimateTokensForMessages(removed)).toBeGreaterThanOrEqual(50);
});

it('ignores reserved context when the reserve is not smaller than the model window', () => {
const strategy = new DefaultCompactionStrategy(() => 32_000, {
triggerRatio: 0.85,
Expand All @@ -141,6 +160,7 @@ describe('Agent compaction', () => {
maxRecentMessages: 3,
maxRecentUserMessages: Infinity,
maxRecentSizeRatio: 0.2,
minOverflowReductionRatio: 0.05,
});

expect(strategy.shouldCompact(1)).toBe(false);
Expand Down Expand Up @@ -1601,6 +1621,7 @@ function testCompactionStrategy(maxSize: number = 1_000): DefaultCompactionStrat
maxRecentMessages: 10,
maxRecentUserMessages: Infinity,
maxRecentSizeRatio: 0.2,
minOverflowReductionRatio: 0.05,
});
}

Expand All @@ -1613,6 +1634,7 @@ function overflowOnlyCompactionStrategy(maxSize: number = 14): DefaultCompaction
maxRecentMessages: 3,
maxRecentUserMessages: Infinity,
maxRecentSizeRatio: 0.2,
minOverflowReductionRatio: 0.05,
});
}

Expand Down
Loading