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/append-todo-to-compaction-summary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@moonshot-ai/agent-core": minor
"@moonshot-ai/kimi-code": minor
---

Append the current todo list as markdown to compaction summaries before writing them to history.
13 changes: 13 additions & 0 deletions packages/agent-core/src/agent/compaction/full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
} from '../../utils/completion-budget';
import compactionInstructionTemplate from './compaction-instruction.md';
import { renderMessagesToText } from './render-messages';
import { renderTodoList, type TodoItem } from '../../tools/builtin/state/todo-list';
import type { CompactionBeginData, CompactionResult } from './types';
import {
DEFAULT_COMPACTION_CONFIG,
Expand Down Expand Up @@ -309,6 +310,8 @@ export class FullCompaction {
}
}

summary = this.postProcessSummary(summary);

const recent = originalHistory.slice(compactedCount);
const tokensAfter = estimateTokens(summary) + estimateTokensForMessages(recent);

Expand Down Expand Up @@ -396,6 +399,16 @@ export class FullCompaction {
},
});
}

private postProcessSummary(summary: string): string {
const storeData = this.agent.tools.storeData();
const todos = (storeData['todo'] as readonly TodoItem[] | undefined) ?? [];
if (todos.length === 0) {
return summary;
}
const todoMarkdown = renderTodoList(todos, '## TODO List');
return `${summary.trim()}\n\n${todoMarkdown}`;
Comment thread
kermanx marked this conversation as resolved.
}
}

function extractCompactionSummary(response: GenerateResult): string {
Expand Down
4 changes: 2 additions & 2 deletions packages/agent-core/src/tools/builtin/state/todo-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ const TODO_STORE_KEY = 'todo';

// ── Implementation ───────────────────────────────────────────────────

function renderTodoList(todos: readonly TodoItem[]): string {
export function renderTodoList(todos: readonly TodoItem[], title = 'Current todo list:'): string {
if (todos.length === 0) {
return 'Todo list is empty.';
}
const lines = todos.map((t) => {
const marker = statusMarker(t.status);
return ` ${marker} ${t.title}`;
});
return ['Current todo list:', ...lines].join('\n');
return [title, ...lines].join('\n');
}

function statusMarker(status: TodoStatus): string {
Expand Down
33 changes: 33 additions & 0 deletions packages/agent-core/test/agent/compaction/full.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,39 @@ describe('FullCompaction', () => {
`);
await ctx.expectResumeMatches();
});

it('appends the todo list to the compaction summary', async () => {
const ctx = testAgent();
ctx.configure({
provider: CATALOGUED_PROVIDER,
modelCapabilities: CATALOGUED_MODEL_CAPABILITIES,
});
ctx.appendExchange(1, 'old user one', 'old assistant one', 20);
ctx.appendExchange(2, 'recent user two', 'recent assistant two', 80);

ctx.agent.tools.updateStore('todo', [
{ title: 'Fix the auth bug', status: 'in_progress' },
{ title: 'Add tests', status: 'pending' },
]);

const compacted = new Promise<void>((resolve) => {
ctx.emitter.once('context.apply_compaction', () => {
resolve();
});
});

ctx.mockNextResponse({ type: 'text', text: 'Compacted summary.' });
await ctx.rpc.beginCompaction({});
await compacted;

const history = ctx.compactHistory();
expect(history).toHaveLength(1);
expect(history[0]).toMatchObject({
role: 'assistant',
text: 'Compacted summary.\n\n## TODO List\n [in_progress] Fix the auth bug\n [pending] Add tests',
});
await ctx.expectResumeMatches();
});
});

afterEach(() => {
Expand Down
Loading