From 41caa677d8d7355b28f2c7bdbfea4ee5384a899f Mon Sep 17 00:00:00 2001 From: Ishaan Gupta Date: Tue, 17 Nov 2026 21:59:44 +0530 Subject: [PATCH] fix: back off failed compaction summary saves --- src/v2/runtime.ts | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/v2/runtime.ts b/src/v2/runtime.ts index f422023..d454709 100644 --- a/src/v2/runtime.ts +++ b/src/v2/runtime.ts @@ -62,6 +62,8 @@ const SYNTHETIC_METADATA_KEY = "supermemory"; const UPDATE_COMMAND = "bunx opencode-supermemory@latest install"; const MAX_DISPATCHES_PER_SESSION = 8; const MIN_SUMMARY_CHARS = 100; +const MAX_SUMMARY_ATTEMPTS = 5; +const SUMMARY_RETRY_BASE_MS = 15_000; export const SUPERMEMORY_TOOL_NAME = "supermemory"; export const SUPERMEMORY_RECALL_TOOL_NAME = "supermemory_recall"; @@ -183,6 +185,8 @@ interface PendingSummary { customId: string; sessionID: string; text: string; + attempts: number; + retryAt: number; } interface Registration { @@ -754,7 +758,13 @@ export class V2Runtime { } const eventId = event.id ?? sha256(`${sessionID}:${text}`); const customId = `opencode:compaction:${sha256(`${sessionID}:${eventId}`)}`; - this.#pendingSummaries.set(customId, { customId, sessionID, text }); + this.#pendingSummaries.set(customId, { + customId, + sessionID, + text, + attempts: 0, + retryAt: 0, + }); await this.#retryPendingSummaries(sessionID); return; } @@ -1166,8 +1176,12 @@ export class V2Runtime { allowInactive = false, ): Promise { if (!allowInactive && !this.active) return; + const now = Date.now(); const pending = [...this.#pendingSummaries.values()].filter( - (item) => item.sessionID === sessionID, + (item) => + item.sessionID === sessionID && + item.retryAt <= now && + !this.#summaryInFlight.has(item.customId), ); if (pending.length === 0) return; @@ -1211,15 +1225,19 @@ export class V2Runtime { memoryId: result.id, }); } else { + this.#deferSummary(summary); this.#deps.logger("[compaction] failed to save summary", { sessionID, error: result.error, + attempts: summary.attempts, }); } } catch (error) { + this.#deferSummary(summary); this.#deps.logger("[compaction] failed to save summary", { sessionID, error: String(error), + attempts: summary.attempts, }); } finally { this.#summaryInFlight.delete(summary.customId); @@ -1227,6 +1245,16 @@ export class V2Runtime { } } + #deferSummary(summary: PendingSummary): void { + summary.attempts += 1; + if (summary.attempts >= MAX_SUMMARY_ATTEMPTS) { + this.#pendingSummaries.delete(summary.customId); + return; + } + const backoff = SUMMARY_RETRY_BASE_MS * 2 ** (summary.attempts - 1); + summary.retryAt = Date.now() + backoff / 2 + Math.random() * (backoff / 2); + } + #eventSessionID(event: V2Event): string | undefined { const sessionID = event.data?.sessionID; return typeof sessionID === "string" && sessionID ? sessionID : undefined;