From b8825097c74863a17bdac7487cc675ef17ff8292 Mon Sep 17 00:00:00 2001 From: Bin Deng Date: Sun, 15 Feb 2026 04:53:12 +0800 Subject: [PATCH] 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 --- src/agents/pi-embedded-runner/compact.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/agents/pi-embedded-runner/compact.ts b/src/agents/pi-embedded-runner/compact.ts index 1ada5c626a53..43738d0495e6 100644 --- a/src/agents/pi-embedded-runner/compact.ts +++ b/src/agents/pi-embedded-runner/compact.ts @@ -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((_, 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 {