From a2bf58ebc7d020d77915c7b3f84207a243bf61bc Mon Sep 17 00:00:00 2001 From: xm0onh Date: Mon, 23 Dec 2024 18:00:55 -0800 Subject: [PATCH 1/2] Remove waiting for promise - avoid of breaking other processes - increase retry --- .../agent-memory-viewer/backend/src/index.ts | 34 +++++++-------- .../backend/src/utils/dsn.ts | 2 +- .../backend/src/utils/resurrection.ts | 41 +++++++++++++++---- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/auto-kol/agent-memory-viewer/backend/src/index.ts b/auto-kol/agent-memory-viewer/backend/src/index.ts index 015ca70..738e509 100644 --- a/auto-kol/agent-memory-viewer/backend/src/index.ts +++ b/auto-kol/agent-memory-viewer/backend/src/index.ts @@ -25,27 +25,29 @@ process.on('unhandledRejection', async (reason, promise) => { async function main() { try { await initialize(); - await resurrection(); + resurrection(); const memoryWatcher = watchMemoryHashUpdates(async (agent, cid) => { - try { - logger.info('New memory hash detected', { agent, cid }); - const memory = await downloadMemory(cid); - if (memory) { - const savedMemory = await saveMemoryRecord(cid, memory, memory?.previousCid); - logger.info('Memory processed successfully', { + logger.info('New memory hash detected', { agent, cid }); + + downloadMemory(cid) + .then(async memory => { + if (memory) { + const savedMemory = await saveMemoryRecord(cid, memory, memory?.previousCid); + logger.info('Memory processed successfully', { + cid, + isNew: savedMemory.created_at === savedMemory.created_at + }); + } + }) + .catch(error => { + logger.error('Error processing memory update', { + error, + agent, cid, - isNew: savedMemory.created_at === savedMemory.created_at + errorType: error instanceof Error ? error.constructor.name : typeof error }); - } - } catch (error) { - logger.error('Error processing memory update', { - error, - agent, - cid, - errorType: error instanceof Error ? error.constructor.name : typeof error }); - } }); createWebSocketServer(); diff --git a/auto-kol/agent-memory-viewer/backend/src/utils/dsn.ts b/auto-kol/agent-memory-viewer/backend/src/utils/dsn.ts index 2da1bda..f1f96fc 100644 --- a/auto-kol/agent-memory-viewer/backend/src/utils/dsn.ts +++ b/auto-kol/agent-memory-viewer/backend/src/utils/dsn.ts @@ -5,7 +5,7 @@ import { createLogger } from './logger.js'; const logger = createLogger('dsn'); -const MAX_RETRIES = 10; +const MAX_RETRIES = 100; const INITIAL_RETRY_DELAY = 10000; async function delay(ms: number) { diff --git a/auto-kol/agent-memory-viewer/backend/src/utils/resurrection.ts b/auto-kol/agent-memory-viewer/backend/src/utils/resurrection.ts index cfeb451..3471b22 100644 --- a/auto-kol/agent-memory-viewer/backend/src/utils/resurrection.ts +++ b/auto-kol/agent-memory-viewer/backend/src/utils/resurrection.ts @@ -8,7 +8,15 @@ const logger = createLogger('resurrection'); export async function resurrection() { logger.info('Starting resurrection'); let hash = await getLastMemoryHash(); + + processResurrection(hash).catch(error => { + logger.error('Error during resurrection:', error); + }); +} + +async function processResurrection(startHash: string) { const memories: { hash: string; data: any }[] = []; + let hash = startHash; while (true) { const existingMemory = await getMemoryByCid(hash); @@ -17,19 +25,34 @@ export async function resurrection() { break; } - const memory = await downloadMemory(hash); - if (!memory) break; - - memories.push({ hash, data: memory }); - hash = memory?.previousCid; - - if (!hash) break; + try { + const memory = await downloadMemory(hash); + if (!memory) break; + + memories.push({ hash, data: memory }); + hash = memory?.previousCid; + + if (!hash) break; + } catch (error) { + logger.error('Failed to download memory during resurrection', { + cid: hash, + error + }); + break; + } } for (let i = memories.length - 1; i >= 0; i--) { const { hash, data } = memories[i]; - await saveMemoryRecord(hash, data, data?.previousCid); - logger.info('Saved memory during resurrection', { cid: hash }); + try { + await saveMemoryRecord(hash, data, data?.previousCid); + logger.info('Saved memory during resurrection', { cid: hash }); + } catch (error) { + logger.error('Failed to save memory during resurrection', { + cid: hash, + error + }); + } } logger.info('Resurrection complete', { memoriesProcessed: memories.length }); From 3de07aa866743638087e57e051bdb163be3c0d48 Mon Sep 17 00:00:00 2001 From: Xm0onh <89309559+Xm0onh@users.noreply.github.com> Date: Tue, 24 Dec 2024 10:04:25 -0800 Subject: [PATCH 2/2] Update retry param in dsn --- auto-kol/agent-memory-viewer/backend/src/utils/dsn.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/auto-kol/agent-memory-viewer/backend/src/utils/dsn.ts b/auto-kol/agent-memory-viewer/backend/src/utils/dsn.ts index f1f96fc..d4455a9 100644 --- a/auto-kol/agent-memory-viewer/backend/src/utils/dsn.ts +++ b/auto-kol/agent-memory-viewer/backend/src/utils/dsn.ts @@ -5,7 +5,7 @@ import { createLogger } from './logger.js'; const logger = createLogger('dsn'); -const MAX_RETRIES = 100; +const MAX_RETRIES = 15; const INITIAL_RETRY_DELAY = 10000; async function delay(ms: number) { @@ -70,4 +70,4 @@ export async function downloadMemory(cid: string, retryCount = 0): Promise }); throw error; } -} \ No newline at end of file +}