Skip to content

Commit 88255cf

Browse files
committed
fix(compaction): preserve source message timestamps in synthetic merge messages during multi-stage summarization
In summarizeInStages(), synthetic merge messages all received timestamp: Date.now(), stripping the original chronological ordering of the source chunks. The LLM merger could not distinguish which summary chunk represents older vs newer history. Use the earliest message timestamp from each source chunk instead, falling back to the last message's timestamp, then Date.now(). Fixes: #100636
1 parent 806397f commit 88255cf

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

‎src/agents/compaction.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* Summarization and fallback helpers for transcript compaction.
33
*/
44
import type { AgentCompactionIdentifierPolicy } from "../config/types.agent-defaults.js";
5+
import { isAbortError } from "../infra/abort-signal.js";
56
import { formatErrorMessage } from "../infra/errors.js";
67
import { retryAsync } from "../infra/retry.js";
7-
import { isAbortError } from "../infra/abort-signal.js";
88
import { createSubsystemLogger } from "../logging/subsystem.js";
99
import {
1010
buildOversizedFallbackPlanWithWorker,
@@ -386,10 +386,16 @@ export async function summarizeInStages(params: {
386386
return partialSummaries[0];
387387
}
388388

389-
const summaryMessages: AgentMessage[] = partialSummaries.map((summary) => ({
389+
// Preserve the relative chronological ordering of the source chunks
390+
// in the synthetic merge messages so the LLM merger can distinguish
391+
// older history from newer history during final assembly.
392+
const summaryMessages: AgentMessage[] = partialSummaries.map((summary, idx) => ({
390393
role: "user",
391394
content: summary,
392-
timestamp: Date.now(),
395+
timestamp:
396+
plan.chunks[idx]?.[0]?.timestamp ??
397+
plan.chunks[idx]?.[plan.chunks[idx].length - 1]?.timestamp ??
398+
Date.now(),
393399
}));
394400

395401
const custom = params.customInstructions?.trim();

0 commit comments

Comments
 (0)