Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/v2/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -183,6 +185,8 @@ interface PendingSummary {
customId: string;
sessionID: string;
text: string;
attempts: number;
retryAt: number;
}

interface Registration {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -1166,8 +1176,12 @@ export class V2Runtime {
allowInactive = false,
): Promise<void> {
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;

Expand Down Expand Up @@ -1211,22 +1225,36 @@ 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);
}
}
}

#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;
Expand Down