fix: add safety timeout to session.compact() to prevent lane deadlock

When embedded compaction runs hang indefinitely (e.g., provider timeout
without rejection), the session lane remains permanently stuck because
the lane task never completes. Add a 5-minute safety timeout around
session.compact() so the Promise always settles, allowing the lane to
drain and process subsequent messages.

Closes #16331

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Bin Deng
2026-02-15 04:53:12 +08:00
committed by Gustavo Madeira Santana
parent 542271e305
commit b8825097c7

View File

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