From f28dacc11bce027e0e6800779fcff279c68762bd Mon Sep 17 00:00:00 2001 From: tao Date: Fri, 30 Jan 2026 00:18:23 +0800 Subject: [PATCH] fix: remove double safety margin in proactive compaction Safety margin was applied twice: 1. threshold = (context - reserve) / 1.2 2. estimated = (existing + prompt) * 1.2 This caused compaction to trigger too early (~62% instead of ~83%). Now only applied once in threshold calculation. --- src/agents/pi-embedded-runner/proactive-compaction.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/agents/pi-embedded-runner/proactive-compaction.ts b/src/agents/pi-embedded-runner/proactive-compaction.ts index 971d868f7..280895236 100644 --- a/src/agents/pi-embedded-runner/proactive-compaction.ts +++ b/src/agents/pi-embedded-runner/proactive-compaction.ts @@ -110,8 +110,8 @@ export async function checkProactiveCompaction(params: { // Add estimate for the new prompt (default ~500 tokens if not provided) const promptEstimate = params.promptTokenEstimate ?? 500; - // Apply safety margin to account for estimation inaccuracy - const estimatedTotalTokens = Math.floor((existingTokens + promptEstimate) * SAFETY_MARGIN); + // Total estimated tokens (safety margin is already applied in threshold calculation) + const estimatedTotalTokens = existingTokens + promptEstimate; const shouldCompact = estimatedTotalTokens >= threshold;