From a1a268c22253fbb9ecf889e350c66ae0f34801cb Mon Sep 17 00:00:00 2001 From: Han Xiao Date: Thu, 29 Jan 2026 14:17:06 -0800 Subject: [PATCH] fix: compaction safeguard falls through when ctx.model is unavailable When extensionRunner.initialize() is not called (e.g. in embedded runner mode), ctx.model resolves to undefined because the default getModel returns undefined. Previously this caused the safeguard to return a fallback summary with no actual content, effectively discarding all conversation history on every compaction. Instead of returning an empty fallback, return undefined so the built-in compaction code in AgentSession handles summarization. The built-in path correctly accesses the model via this.model (AgentSession.agent.state.model) and produces a proper summary. Same fix applied to the apiKey null case -- falling through is safer than producing an empty summary that causes cascading re-compactions. --- .../pi-extensions/compaction-safeguard.ts | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/agents/pi-extensions/compaction-safeguard.ts b/src/agents/pi-extensions/compaction-safeguard.ts index b2fe39884..7a2e8e75f 100644 --- a/src/agents/pi-extensions/compaction-safeguard.ts +++ b/src/agents/pi-extensions/compaction-safeguard.ts @@ -148,26 +148,16 @@ export default function compactionSafeguardExtension(api: ExtensionAPI): void { const model = ctx.model; if (!model) { - return { - compaction: { - summary: fallbackSummary, - firstKeptEntryId: preparation.firstKeptEntryId, - tokensBefore: preparation.tokensBefore, - details: { readFiles, modifiedFiles }, - }, - }; + // ctx.model may be undefined when extensionRunner.initialize() was not called + // (e.g. embedded runner mode). Fall through to built-in compaction which has + // correct model access via AgentSession.model. + return undefined; } const apiKey = await ctx.modelRegistry.getApiKey(model); if (!apiKey) { - return { - compaction: { - summary: fallbackSummary, - firstKeptEntryId: preparation.firstKeptEntryId, - tokensBefore: preparation.tokensBefore, - details: { readFiles, modifiedFiles }, - }, - }; + // Fall through to built-in compaction rather than producing an empty summary. + return undefined; } try {