Skip to content

Commit e07df1d

Browse files
committed
fix: preserve global compaction offsets
1 parent 2404e3a commit e07df1d

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ export class FullCompaction {
342342
startIndex + compactedCount,
343343
);
344344
const messages = [
345-
...this.agent.context.project(messagesToCompact),
345+
...this.agent.context.project(messagesToCompact, startIndex),
346346
createUserMessage(renderPrompt(compactionInstructionTemplate, { customInstruction: data.instruction ?? '' })),
347347
];
348348
try {

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,35 @@ describe('FullCompaction', () => {
393393
expect(messageText(compactionCall?.history[2])).toBe('[Old tool result content cleared]');
394394
expect(messageText(compactionCall?.history[5])).toBe('lookup result');
395395
});
396+
it('uses global history offsets when projecting a from compaction range', async () => {
397+
enableMicroCompactionFlag();
398+
const ctx = testAgent({
399+
microCompaction: {
400+
keepRecentMessages: 0,
401+
minContentTokens: 1,
402+
},
403+
});
404+
ctx.configure({
405+
provider: CATALOGUED_PROVIDER,
406+
modelCapabilities: CATALOGUED_MODEL_CAPABILITIES,
407+
});
408+
ctx.appendExchange(1, 'old user one', 'old assistant one', 20);
409+
ctx.appendToolExchange();
410+
const longToolResult = 'lookup result '.repeat(100);
411+
const toolResult = ctx.agent.context.history.at(-1);
412+
if (toolResult?.role !== 'tool') throw new Error('Expected a tool result.');
413+
toolResult.content[0] = { type: 'text', text: longToolResult };
414+
ctx.appendExchange(3, 'recent user two', 'recent assistant two', 40);
415+
ctx.agent.microCompaction.apply(3);
416+
const compacted = ctx.once('context.apply_compaction');
417+
418+
ctx.mockNextResponse({ type: 'text', text: 'Compacted summary.' });
419+
await ctx.rpc.beginCompaction({ promptFromEnd: 2, direction: 'from' } as never);
420+
await compacted;
421+
422+
const [compactionCall] = ctx.llmCalls;
423+
expect(messageText(compactionCall?.history[2])).toBe(longToolResult);
424+
});
396425

397426
it('fires PreCompact and PostCompact hooks from the compaction module', async () => {
398427
const dir = mkdtempSync(join(tmpdir(), 'pythinker-compact-hooks-'));

0 commit comments

Comments
 (0)