fix: clear compaction timeout timer on success

Ensure the safety timeout is cleared when compact() resolves
normally, preventing a 5-minute timer leak.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Bin Deng 2026-02-15 05:25:20 +08:00 committed by Gustavo Madeira Santana
parent b8825097c7
commit eccba40ad0
1 changed files with 8 additions and 3 deletions

View File

@ -633,16 +633,21 @@ export async function compactEmbeddedPiSessionDirect(
const compactStartedAt = Date.now();
const COMPACT_TIMEOUT_MS = 300_000; // 5 minutes safety timeout
let compactTimer: ReturnType<typeof setTimeout> | undefined;
const result = await Promise.race([
session.compact(params.customInstructions),
new Promise<never>((_, reject) => {
const timer = setTimeout(
compactTimer = setTimeout(
() => reject(new Error("Compaction timed out")),
COMPACT_TIMEOUT_MS,
);
timer.unref?.();
compactTimer.unref?.();
}),
]);
]).finally(() => {
if (compactTimer) {
clearTimeout(compactTimer);
}
});
// Estimate tokens after compaction by summing token estimates for remaining messages
let tokensAfter: number | undefined;
try {