Skip to content

Commit eba225a

Browse files
committed
fix(agents): preserve prior summaries during compaction fallback
1 parent b7927bc commit eba225a

3 files changed

Lines changed: 64 additions & 8 deletions

File tree

‎src/agents/agent-hooks/compaction-safeguard.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,54 @@ describe("compaction-safeguard recent-turn preservation", () => {
22722272
expect(result.compaction?.summary).not.toContain("Old duplicated section");
22732273
});
22742274

2275+
it("preserves the prior summary when staged summarization returns a generic fallback", async () => {
2276+
mockSummarizeInStages.mockReset();
2277+
mockSummarizeInStages.mockResolvedValue({
2278+
kind: "generic-fallback",
2279+
text: "Context contained 4 messages. Summary unavailable due to size limits.",
2280+
});
2281+
2282+
const sessionManager = stubSessionManager();
2283+
const model = createAnthropicModelFixture();
2284+
setCompactionSafeguardRuntime(sessionManager, {
2285+
model,
2286+
recentTurnsPreserve: 0,
2287+
});
2288+
2289+
const compactionHandler = createCompactionHandler();
2290+
const mockContext = createCompactionContext({
2291+
sessionManager,
2292+
getApiKeyMock: vi.fn().mockResolvedValue("test-key"),
2293+
});
2294+
const event = {
2295+
preparation: {
2296+
messagesToSummarize: [{ role: "user", content: "latest ask status", timestamp: 1 }],
2297+
turnPrefixMessages: [],
2298+
firstKeptEntryId: "entry-1",
2299+
tokensBefore: 1_500,
2300+
fileOps: {
2301+
read: [],
2302+
edited: [],
2303+
written: [],
2304+
},
2305+
settings: { reserveTokens: 4_000 },
2306+
previousSummary: "## Goal\nKnown context that must survive the outage.",
2307+
isSplitTurn: false,
2308+
},
2309+
customInstructions: "",
2310+
signal: new AbortController().signal,
2311+
};
2312+
2313+
const result = (await compactionHandler(event, mockContext)) as {
2314+
cancel?: boolean;
2315+
compaction?: { summary?: string };
2316+
};
2317+
2318+
expect(result.cancel).not.toBe(true);
2319+
expect(result.compaction?.summary).toContain("Known context that must survive the outage.");
2320+
expect(result.compaction?.summary).toContain("Summary unavailable due to size limits.");
2321+
});
2322+
22752323
it("falls back to LLM when provider throws a provider-side AbortError with signal not aborted", async () => {
22762324
// Reproduce the undici AbortError("This operation was aborted") shape that
22772325
// arrives when the compaction provider's HTTP connection drops mid-stream while

‎src/agents/agent-hooks/compaction-safeguard.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,19 @@ function containsRealConversation(messages: AgentMessage[]): boolean {
286286
* Only called when no compaction provider is available or the provider failed.
287287
*/
288288
async function summarizeViaLLM(params: Parameters<typeof summarizeInStages>[0]): Promise<string> {
289-
return (
290-
await compactionSafeguardDeps.summarizeInStages({
291-
...params,
292-
messages: prependPreviousSummaryForRedistill(params),
293-
previousSummary: undefined,
294-
})
295-
).text;
289+
const result = await compactionSafeguardDeps.summarizeInStages({
290+
...params,
291+
messages: prependPreviousSummaryForRedistill(params),
292+
previousSummary: undefined,
293+
});
294+
if (result.kind === "summary") {
295+
return result.text;
296+
}
297+
298+
// A generic fallback means redistillation never happened. Preserve the
299+
// known summary verbatim so a temporary model outage cannot erase it.
300+
const previousSummary = params.previousSummary?.trim();
301+
return previousSummary ? `${previousSummary}\n\n${result.text}` : result.text;
296302
}
297303

298304
/**

‎src/agents/compaction.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ const log = createSubsystemLogger("compaction");
4242

4343
type PartialSummaryError = Error & { partialSummary?: string };
4444

45-
type CompactionSummaryResult = { kind: "summary"; text: string };
45+
type CompactionSummaryResult =
46+
| { kind: "summary"; text: string }
47+
| { kind: "generic-fallback"; text: string };
4648

4749
const DEFAULT_SUMMARY_FALLBACK = "No prior history.";
4850
const MERGE_SUMMARIES_INSTRUCTIONS = [

0 commit comments

Comments
 (0)