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.
This commit is contained in:
liri-ha
2026-03-04 22:29:25 +03:00
committed by Josh Lehman
parent 424ca99305
commit 8804983220

View File

@@ -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),