Skip to content
Open
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
2 changes: 1 addition & 1 deletion .changeset/compaction-context-budget.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"@moonshot-ai/kimi-code": minor
---

Remind the model of its context budget before automatic compaction, and after compaction point it at the session's event log for exact details.
After compaction, point the model at the session's event log so it can recover exact details of the compacted conversation.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {
CompactionResult,
CompactionSource,
} from './types';
import type { CompactionTriggerBudget } from './strategy';
import { createDecorator } from "#/_base/di/instantiation";
import type { Event } from '#/_base/event';
import type { Hooks } from '#/hooks';
Expand All @@ -12,10 +11,6 @@ export interface FullCompactionInput {
readonly instruction?: string;
}

export interface CompactionBudget extends CompactionTriggerBudget {
readonly used: number;
}

export interface FullCompactionTask {
readonly abortController: AbortController;
readonly promise: Promise<CompactionResult>;
Expand All @@ -30,7 +25,6 @@ export interface IAgentFullCompactionService {
readonly compacting: FullCompactionTask | null;
begin(input: FullCompactionInput): boolean;
cancel(): void;
budget(): CompactionBudget;

readonly hooks: Hooks<{
onWillCompact: FullCompactionTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ import { stripDynamicToolContext } from '#/agent/toolSelect/dynamicTools';
import { IAgentToolSelectService } from '#/agent/toolSelect/toolSelect';
import { IAgentTodoService } from '#/features/todo/todoService';
import { renderTodoList } from '#/features/todo/todoItem';
import {
isContextBudgetReminder,
summarizeCompactionAheadFollowUp,
} from '#/features/contextBudget/contextBudgetReminder';
import { onUnexpectedError } from '#/_base/errors/unexpectedError';
import type { WireLineRange } from '#/wire/record';
import { IWireService } from '#/wire/wire';
Expand All @@ -52,7 +48,6 @@ import { renderCompactionInstruction } from './compactionInstruction';
import { renderContextRecoveryPointer } from './contextRecovery';
import {
IAgentFullCompactionService,
type CompactionBudget,
type FullCompactionInput,
type FullCompactionTask,
} from './fullCompaction';
Expand Down Expand Up @@ -248,10 +243,6 @@ export class AgentFullCompactionService extends Service implements IAgentFullCom
return this._compacting;
}

budget(): CompactionBudget {
return { used: this.tokenCountWithPending(), ...this.strategy.budget() };
}

cancel(): void {
const active = this._compacting;
if (active !== null) {
Expand Down Expand Up @@ -652,9 +643,7 @@ export class AgentFullCompactionService extends Service implements IAgentFullCom

const delays = retryBackoffDelays(MAX_COMPACTION_RETRY_ATTEMPTS);
let attempt: CompactionAttemptResult | undefined;
let historyForModel: readonly ContextMessage[] = stripDynamicToolContext(originalHistory).filter(
(message) => !isContextBudgetReminder(message),
);
let historyForModel: readonly ContextMessage[] = stripDynamicToolContext(originalHistory);
let droppedCount = 0;
let overflowShrinkCount = 0;
let emptyOrTruncatedShrinkCount = 0;
Expand Down Expand Up @@ -781,7 +770,6 @@ export class AgentFullCompactionService extends Service implements IAgentFullCom
thinking_effort: thinkingEffort,
trace_id: attempt.traceId,
...usageTelemetry(attempt.usage),
...aheadReminderTelemetry(originalHistory),
};
this.telemetry.track2('compaction_finished', properties);
return result;
Expand Down Expand Up @@ -845,29 +833,6 @@ export class AgentFullCompactionService extends Service implements IAgentFullCom
}
}

type CompactionAheadTelemetryProperties = Pick<
CompactionFinishedEvent,
| 'ahead_reminder_delivered'
| 'ahead_steps_count'
| 'ahead_write_calls_count'
| 'ahead_bash_calls_count'
| 'ahead_todo_calls_count'
>;

function aheadReminderTelemetry(
history: readonly ContextMessage[],
): CompactionAheadTelemetryProperties {
const followUp = summarizeCompactionAheadFollowUp(history);
if (followUp === undefined) return { ahead_reminder_delivered: false };
return {
ahead_reminder_delivered: true,
ahead_steps_count: followUp.stepCount,
ahead_write_calls_count: followUp.writeCallCount,
ahead_bash_calls_count: followUp.bashCallCount,
ahead_todo_calls_count: followUp.todoCallCount,
};
}

function findAPIStatusError(error: unknown): APIStatusError | undefined {
let current: unknown = error;
const seen = new Set<unknown>();
Expand Down
30 changes: 0 additions & 30 deletions packages/agent-core-v2/src/agent/fullCompaction/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,9 @@ export const DEFAULT_COMPACTION_CONFIG: CompactionConfig = {
minOverflowReductionRatio: 0.05,
};

export interface CompactionTriggerBudget {
readonly maxSize: number;
readonly triggerRatio: number;
readonly reservedContextSize: number;
readonly triggerTokens: number;
}

export interface CompactionStrategy {
shouldCompact(usedSize: number): boolean;
shouldBlock(usedSize: number): boolean;
budget(): CompactionTriggerBudget;
computeCompactCount(messages: readonly Message[], source: CompactionSource): number;
reduceCompactOnOverflow(messages: readonly Message[]): number;
readonly checkAfterStep: boolean;
Expand All @@ -59,10 +51,6 @@ export class RuntimeCompactionStrategy implements CompactionStrategy {
return this.delegate().shouldBlock(usedSize);
}

budget(): CompactionTriggerBudget {
return this.delegate().budget();
}

computeCompactCount(messages: readonly Message[], source: CompactionSource): number {
return this.windowDelegate().computeCompactCount(messages, source);
}
Expand Down Expand Up @@ -141,24 +129,6 @@ export class DefaultCompactionStrategy implements CompactionStrategy {
);
}

budget(): CompactionTriggerBudget {
const maxSize = this.maxSize;
const reservedContextSize = this.config.reservedContextSize;
const reservedTrigger =
reservedContextSize > 0 && reservedContextSize < maxSize
? maxSize - reservedContextSize
: Number.POSITIVE_INFINITY;
return {
maxSize,
triggerRatio: this.config.triggerRatio,
reservedContextSize,
triggerTokens:
maxSize <= 0
? Number.POSITIVE_INFINITY
: Math.min(Math.ceil(maxSize * this.config.triggerRatio), reservedTrigger),
};
}

private shouldUseReservedContext(usedSize: number): boolean {
const reservedSize = this.config.reservedContextSize;
return reservedSize > 0 && reservedSize < this.maxSize && usedSize + reservedSize >= this.maxSize;
Expand Down
43 changes: 0 additions & 43 deletions packages/agent-core-v2/src/app/telemetry/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,24 +215,6 @@ export interface CompactionFinishedEvent {
input_cache_read?: number;
input_cache_creation?: number;
trace_id?: string;
ahead_reminder_delivered: boolean;
ahead_steps_count?: number;
ahead_write_calls_count?: number;
ahead_bash_calls_count?: number;
ahead_todo_calls_count?: number;
}

export interface ContextBudgetReminderEvent {
bucket: 'half' | 'three_quarters';
used_tokens: number;
trigger_tokens: number;
max_tokens: number;
}

export interface CompactionAheadReminderEvent {
used_tokens: number;
trigger_tokens: number;
lead_tokens: number;
}

export interface CompactionFailedEvent {
Expand Down Expand Up @@ -802,31 +784,6 @@ export const telemetryEventDefinitions = {
input_cache_creation: 'Cache-creation input tokens',
trace_id:
'Trace id of the final compaction request round; absent for non-Kimi protocols',
ahead_reminder_delivered:
'Whether the compaction-ahead reminder had been delivered in the compacted window',
ahead_steps_count: 'Assistant steps taken between the compaction-ahead reminder and compaction',
ahead_write_calls_count: 'Write/Edit tool calls made after the compaction-ahead reminder',
ahead_bash_calls_count: 'Bash tool calls made after the compaction-ahead reminder',
ahead_todo_calls_count: 'Todo tool calls made after the compaction-ahead reminder',
},
}),
context_budget_reminder: defineAgentTelemetryEvent<ContextBudgetReminderEvent>({
owner: 'kimi-code',
comment: 'The model is told how much of its context budget is used, once per bucket.',
properties: {
bucket: 'Share of the compaction trigger reached: half or three_quarters',
used_tokens: 'Context tokens in use when the reminder was injected',
trigger_tokens: 'Token count at which automatic compaction triggers',
max_tokens: 'Effective context window size in tokens',
},
}),
compaction_ahead_reminder: defineAgentTelemetryEvent<CompactionAheadReminderEvent>({
owner: 'kimi-code',
comment: 'The model is warned once per window that automatic compaction is imminent.',
properties: {
used_tokens: 'Context tokens in use when the reminder was injected',
trigger_tokens: 'Token count at which automatic compaction triggers',
lead_tokens: 'Tokens between the reminder threshold and the compaction trigger',
},
}),
compaction_failed: defineAgentTelemetryEvent<CompactionFailedEvent>({
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading