From 880498322022de71cb98c2bd2e5e0f71c38f40c1 Mon Sep 17 00:00:00 2001 From: liri-ha <258306986+liri-ha@users.noreply.github.com> Date: Wed, 4 Mar 2026 22:29:25 +0300 Subject: [PATCH] fix: guard pre-compaction token estimation with try/catch If estimateTokens throws on a malformed/legacy message, fall back to 0 so the sanity check becomes a no-op instead of crashing compaction. Also skip the sanity check when fullSessionTokensBefore is 0. --- src/agents/pi-embedded-runner/compact.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/agents/pi-embedded-runner/compact.ts b/src/agents/pi-embedded-runner/compact.ts index d6d00bddd8c..69f9fd0b49c 100644 --- a/src/agents/pi-embedded-runner/compact.ts +++ b/src/agents/pi-embedded-runner/compact.ts @@ -905,8 +905,8 @@ export async function compactEmbeddedPiSessionDirect( try { fullSessionTokensBefore = limited.reduce((sum, msg) => sum + estimateTokens(msg), 0); } catch { - // If token estimation throws on a malformed message, skip the sanity check - // instead of crashing compaction. + // If token estimation throws on a malformed message, fall back to 0 so + // the sanity check below becomes a no-op instead of crashing compaction. } const result = await compactWithSafetyTimeout(() => session.compact(params.customInstructions),