Skip to content

Commit fe7db4a

Browse files
authored
feat: append TODO list as markdown to compaction summary (#319)
1 parent 0e57e73 commit fe7db4a

4 files changed

Lines changed: 54 additions & 2 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@moonshot-ai/agent-core": minor
3+
"@moonshot-ai/kimi-code": minor
4+
---
5+
6+
Append the current todo list as markdown to compaction summaries before writing them to history.

packages/agent-core/src/agent/compaction/full.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
} from '../../utils/completion-budget';
3232
import compactionInstructionTemplate from './compaction-instruction.md';
3333
import { renderMessagesToText } from './render-messages';
34+
import { renderTodoList, type TodoItem } from '../../tools/builtin/state/todo-list';
3435
import type { CompactionBeginData, CompactionResult } from './types';
3536
import {
3637
DEFAULT_COMPACTION_CONFIG,
@@ -309,6 +310,8 @@ export class FullCompaction {
309310
}
310311
}
311312

313+
summary = this.postProcessSummary(summary);
314+
312315
const recent = originalHistory.slice(compactedCount);
313316
const tokensAfter = estimateTokens(summary) + estimateTokensForMessages(recent);
314317

@@ -396,6 +399,16 @@ export class FullCompaction {
396399
},
397400
});
398401
}
402+
403+
private postProcessSummary(summary: string): string {
404+
const storeData = this.agent.tools.storeData();
405+
const todos = (storeData['todo'] as readonly TodoItem[] | undefined) ?? [];
406+
if (todos.length === 0) {
407+
return summary;
408+
}
409+
const todoMarkdown = renderTodoList(todos, '## TODO List');
410+
return `${summary.trim()}\n\n${todoMarkdown}`;
411+
}
399412
}
400413

401414
function extractCompactionSummary(response: GenerateResult): string {

packages/agent-core/src/tools/builtin/state/todo-list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ const TODO_STORE_KEY = 'todo';
6060

6161
// ── Implementation ───────────────────────────────────────────────────
6262

63-
function renderTodoList(todos: readonly TodoItem[]): string {
63+
export function renderTodoList(todos: readonly TodoItem[], title = 'Current todo list:'): string {
6464
if (todos.length === 0) {
6565
return 'Todo list is empty.';
6666
}
6767
const lines = todos.map((t) => {
6868
const marker = statusMarker(t.status);
6969
return ` ${marker} ${t.title}`;
7070
});
71-
return ['Current todo list:', ...lines].join('\n');
71+
return [title, ...lines].join('\n');
7272
}
7373

7474
function statusMarker(status: TodoStatus): string {

packages/agent-core/test/agent/compaction/full.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,39 @@ describe('FullCompaction', () => {
16681668
`);
16691669
await ctx.expectResumeMatches();
16701670
});
1671+
1672+
it('appends the todo list to the compaction summary', async () => {
1673+
const ctx = testAgent();
1674+
ctx.configure({
1675+
provider: CATALOGUED_PROVIDER,
1676+
modelCapabilities: CATALOGUED_MODEL_CAPABILITIES,
1677+
});
1678+
ctx.appendExchange(1, 'old user one', 'old assistant one', 20);
1679+
ctx.appendExchange(2, 'recent user two', 'recent assistant two', 80);
1680+
1681+
ctx.agent.tools.updateStore('todo', [
1682+
{ title: 'Fix the auth bug', status: 'in_progress' },
1683+
{ title: 'Add tests', status: 'pending' },
1684+
]);
1685+
1686+
const compacted = new Promise<void>((resolve) => {
1687+
ctx.emitter.once('context.apply_compaction', () => {
1688+
resolve();
1689+
});
1690+
});
1691+
1692+
ctx.mockNextResponse({ type: 'text', text: 'Compacted summary.' });
1693+
await ctx.rpc.beginCompaction({});
1694+
await compacted;
1695+
1696+
const history = ctx.compactHistory();
1697+
expect(history).toHaveLength(1);
1698+
expect(history[0]).toMatchObject({
1699+
role: 'assistant',
1700+
text: 'Compacted summary.\n\n## TODO List\n [in_progress] Fix the auth bug\n [pending] Add tests',
1701+
});
1702+
await ctx.expectResumeMatches();
1703+
});
16711704
});
16721705

16731706
afterEach(() => {

0 commit comments

Comments
 (0)