Skip to content
86 changes: 86 additions & 0 deletions packages/runtime/src/__tests__/ai-sdk-backend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4525,6 +4525,92 @@ describe('AiSdkBackend model history', () => {
assert.equal(result.contextBudget?.compactionDecisions?.[0]?.decision, 'replaced');
});

test('coalesces identical in-flight compactHistory requests', async () => {
const recorded: HistoryCompactCheckpoint[] = [];
let summarizeCalls = 0;
let releaseSummary!: () => void;
const summaryReady = new Promise<void>((resolve) => {
releaseSummary = resolve;
});
const backend = createTestAiSdkBackend({
sessionId: 'session-1',
header: header(),
appendMessage: async () => {},
connection: connection(),
apiKey: 'sk-test',
modelId: 'mock-model-id',
modelFactory: () => completionModel(),
tools: [],
newId: idGenerator(),
now: monotonicClock(),
contextBudget: {
name: 'in-flight-dedup-test',
charsPerToken: 1,
},
summarizeHistoryCompact: async () => {
summarizeCalls += 1;
await summaryReady;
return structuredSummary('IN_FLIGHT_DEDUP_SUMMARY');
},
recordHistoryCompactCheckpoint: (checkpoint) => {
recorded.push(checkpoint);
},
});
const runtimeContext = [
runtimeTextEvent({
id: 'dedup-old-user',
turnId: 'dedup-old-turn',
role: 'user',
author: 'user',
text: 'old context '.repeat(100),
}),
runtimeTextEvent({
id: 'dedup-old-agent',
turnId: 'dedup-old-turn',
role: 'model',
author: 'agent',
text: 'old response '.repeat(100),
}),
];
const first = backend.compactHistory({
turnId: 'dedup-compact-1',
runId: 'run-dedup-1',
runtimeContext,
});
await new Promise<void>((resolve) => setImmediate(resolve));
const second = backend.compactHistory({
turnId: 'dedup-compact-2',
runId: 'run-dedup-2',
runtimeContext: [
...runtimeContext,
runtimeTextEvent({
id: 'dedup-current-turn',
turnId: 'dedup-compact-2',
role: 'user',
author: 'user',
text: 'current turn content must not affect the fold key',
}),
],
});

releaseSummary();
const [firstResult, secondResult] = await Promise.all([first, second]);
assert.equal(summarizeCalls, 1);
assert.equal(secondResult.outcome.kind, 'compacted');
assert.equal(firstResult.outcome.kind, 'compacted');
assert.equal(recorded.length, 2);
const firstCheckpoint = recorded[0];
const secondCheckpoint = recorded[1];
assert.ok(firstCheckpoint);
assert.ok(secondCheckpoint);
assert.equal(firstCheckpoint.version, 2);
assert.equal(secondCheckpoint.version, 2);
if (firstCheckpoint.version === 2 && secondCheckpoint.version === 2) {
assert.equal(secondCheckpoint.summary, firstCheckpoint.summary);
assert.deepEqual(secondCheckpoint.coverage, firstCheckpoint.coverage);
}
});

test('manual compactHistory compacts one completed turn with multiple agent steps', async () => {
const recorded: HistoryCompactCheckpoint[] = [];
const backend = createTestAiSdkBackend({
Expand Down
Loading