|
| 1 | +import { |
| 2 | + Memory_SessionService, |
| 3 | + Memory_ChromaManager, |
| 4 | + Memory_Config |
| 5 | +} from '../services.mjs'; |
| 6 | + |
| 7 | +async function debugSessionState() { |
| 8 | + console.log('🔍 Starting Session State Debugger...'); |
| 9 | + |
| 10 | + // 1. Initialize |
| 11 | + console.log('⏳ Waiting for Memory Core readiness...'); |
| 12 | + try { |
| 13 | + await Memory_ChromaManager.ready(); |
| 14 | + await Memory_SessionService.ready(); |
| 15 | + console.log('✅ Memory Core Services Ready.'); |
| 16 | + } catch (e) { |
| 17 | + console.error('❌ Failed to initialize services:', e); |
| 18 | + process.exit(1); |
| 19 | + } |
| 20 | + |
| 21 | + // 2. Access Collections directly |
| 22 | + // The service exposes these as properties (getters) |
| 23 | + const memCol = Memory_SessionService.memoryCollection; |
| 24 | + const sumCol = Memory_SessionService.sessionsCollection; |
| 25 | + |
| 26 | + if (!memCol || !sumCol) { |
| 27 | + console.error('❌ Collections not initialized in SessionService.'); |
| 28 | + process.exit(1); |
| 29 | + } |
| 30 | + |
| 31 | + console.log(`📂 Connected to collections: ${memCol.name}, ${sumCol.name}`); |
| 32 | + |
| 33 | + // 3. Scan Data (Replicating logic with logging) |
| 34 | + const includeAll = true; // Scan everything to be safe |
| 35 | + const ONE_MONTH_MS = 30 * 24 * 60 * 60 * 1000; |
| 36 | + const minTimestamp = Date.now() - ONE_MONTH_MS; |
| 37 | + |
| 38 | + console.log(` |
| 39 | +📊 Scanning Memories (Include All: ${includeAll})...`); |
| 40 | + |
| 41 | + const limit = 2000; |
| 42 | + let offset = 0; |
| 43 | + let allMemories = []; |
| 44 | + let hasMore = true; |
| 45 | + |
| 46 | + const memQuery = { |
| 47 | + include: ['metadatas'], |
| 48 | + limit |
| 49 | + }; |
| 50 | + |
| 51 | + if (!includeAll) { |
| 52 | + memQuery.where = { timestamp: { '$gt': minTimestamp } }; |
| 53 | + } |
| 54 | + |
| 55 | + while (hasMore) { |
| 56 | + memQuery.offset = offset; |
| 57 | + const batch = await memCol.get(memQuery); |
| 58 | + |
| 59 | + if (batch.ids.length === 0) { |
| 60 | + hasMore = false; |
| 61 | + } else { |
| 62 | + allMemories = allMemories.concat(batch.metadatas); |
| 63 | + offset += limit; |
| 64 | + process.stdout.write(`\r Fetched ${allMemories.length} records...`); |
| 65 | + if (batch.ids.length < limit) hasMore = false; |
| 66 | + } |
| 67 | + } |
| 68 | + console.log(`\n ✅ Total Memories Found: ${allMemories.length}`); |
| 69 | + |
| 70 | + // 4. Group Memories |
| 71 | + const sessions = {}; |
| 72 | + allMemories.forEach(m => { |
| 73 | + if (!m.sessionId) return; |
| 74 | + if (!sessions[m.sessionId]) sessions[m.sessionId] = { count: 0, lastActive: m.timestamp }; |
| 75 | + sessions[m.sessionId].count++; |
| 76 | + if (m.timestamp > sessions[m.sessionId].lastActive) sessions[m.sessionId].lastActive = m.timestamp; |
| 77 | + }); |
| 78 | + |
| 79 | + const sessionIds = Object.keys(sessions); |
| 80 | + console.log(` found ${sessionIds.length} unique sessions.`); |
| 81 | + |
| 82 | + // 5. Scan Summaries |
| 83 | + console.log(` |
| 84 | +📊 Scanning Summaries... |
| 85 | +`); |
| 86 | + |
| 87 | + offset = 0; |
| 88 | + hasMore = true; |
| 89 | + let allSummaries = []; |
| 90 | + const sumQuery = { |
| 91 | + include: ['metadatas'], |
| 92 | + limit |
| 93 | + }; |
| 94 | + |
| 95 | + if (!includeAll) { |
| 96 | + sumQuery.where = { timestamp: { '$gt': minTimestamp } }; |
| 97 | + } |
| 98 | + |
| 99 | + while (hasMore) { |
| 100 | + sumQuery.offset = offset; |
| 101 | + const batch = await sumCol.get(sumQuery); |
| 102 | + |
| 103 | + if (batch.ids.length === 0) { |
| 104 | + hasMore = false; |
| 105 | + } else { |
| 106 | + allSummaries = allSummaries.concat(batch.metadatas); |
| 107 | + offset += limit; |
| 108 | + process.stdout.write(`\r Fetched ${allSummaries.length} records...`); |
| 109 | + if (batch.ids.length < limit) hasMore = false; |
| 110 | + } |
| 111 | + } |
| 112 | + console.log(`\n ✅ Total Summaries Found: ${allSummaries.length}`); |
| 113 | + |
| 114 | + const summaryMap = {}; |
| 115 | + allSummaries.forEach(m => { |
| 116 | + if (m.sessionId) summaryMap[m.sessionId] = m; |
| 117 | + }); |
| 118 | + |
| 119 | + // 6. Compare and Diagnose |
| 120 | + console.log(` |
| 121 | +🕵️ Diagnosing Session Status:`); |
| 122 | + console.log('------------------------------------------------------------------------------------------------------------------'); |
| 123 | + console.log('| Session ID | Mem (DB) | Sum (DB) | Drift? | Status | Last Active |'); |
| 124 | + console.log('------------------------------------------------------------------------------------------------------------------'); |
| 125 | + |
| 126 | + let candidates = 0; |
| 127 | + |
| 128 | + // Sort sessions by last active (newest first) |
| 129 | + sessionIds.sort((a, b) => new Date(sessions[b].lastActive) - new Date(sessions[a].lastActive)); |
| 130 | + |
| 131 | + sessionIds.forEach(id => { |
| 132 | + const memCount = sessions[id].count; |
| 133 | + const sumData = summaryMap[id]; |
| 134 | + const sumCount = sumData ? (sumData.memoryCount || 0) : 'N/A'; |
| 135 | + const lastActive = sessions[id].lastActive; |
| 136 | + |
| 137 | + let status = ''; |
| 138 | + let drift = false; |
| 139 | + |
| 140 | + if (sumCount === 'N/A') { |
| 141 | + status = 'MISSING SUMMARY'; |
| 142 | + drift = true; |
| 143 | + } else if (memCount !== sumCount) { |
| 144 | + status = 'COUNT MISMATCH'; |
| 145 | + drift = true; |
| 146 | + } else { |
| 147 | + status = 'SYNCED'; |
| 148 | + } |
| 149 | + |
| 150 | + if (drift) candidates++; |
| 151 | + |
| 152 | + // Truncate ID for display if needed, or keep full |
| 153 | + const dispId = id.length > 36 ? id.substring(0, 33) + '...' : id.padEnd(36); |
| 154 | + |
| 155 | + // Safely handle lastActive display |
| 156 | + let activeDisplay = String(lastActive); |
| 157 | + if (typeof lastActive === 'number') { |
| 158 | + activeDisplay = new Date(lastActive).toISOString(); |
| 159 | + } |
| 160 | + |
| 161 | + console.log(`| ${dispId} | ${String(memCount).padStart(8)} | ${String(sumCount).padStart(8)} | ${drift ? 'YES' : 'NO '} | ${status.padEnd(15)} | ${activeDisplay.padEnd(27)} |`); |
| 162 | + }); |
| 163 | + console.log('------------------------------------------------------------------------------------------------------------------'); |
| 164 | + console.log(`\nDiagnosis complete. Found ${candidates} sessions needing summarization.`); |
| 165 | + |
| 166 | + console.log(`\n🧪 Verifying Service Logic (Memory_SessionService.findSessionsToSummarize(false))...`); |
| 167 | + let serviceCandidates = []; |
| 168 | + try { |
| 169 | + serviceCandidates = await Memory_SessionService.findSessionsToSummarize(false); |
| 170 | + console.log(` Service returned ${serviceCandidates.length} candidates:`, serviceCandidates); |
| 171 | + |
| 172 | + const missing = sessionIds.filter(id => { |
| 173 | + // Logic: if my diagnosis said it needs update (drift=true) but service didn't find it |
| 174 | + const memCount = sessions[id].count; |
| 175 | + const sumData = summaryMap[id]; |
| 176 | + const sumCount = sumData ? (sumData.memoryCount || 0) : undefined; |
| 177 | + const needsUpdate = (sumCount === undefined || memCount !== sumCount); |
| 178 | + return needsUpdate && !serviceCandidates.includes(id); |
| 179 | + }); |
| 180 | + |
| 181 | + if (missing.length > 0) { |
| 182 | + console.warn(` ⚠️ Service MISSED these candidates:`, missing); |
| 183 | + } else { |
| 184 | + console.log(` ✅ Service logic matches diagnosis.`); |
| 185 | + } |
| 186 | + |
| 187 | + } catch (e) { |
| 188 | + console.error(' ❌ Service call failed:', e); |
| 189 | + } |
| 190 | + |
| 191 | + if (serviceCandidates.length > 0) { |
| 192 | + console.log('\n🚀 Executing Summarization for Candidates...'); |
| 193 | + for (const sessionId of serviceCandidates) { |
| 194 | + process.stdout.write(` Summarizing ${sessionId}... `); |
| 195 | + try { |
| 196 | + await Memory_SessionService.summarizeSession(sessionId); |
| 197 | + console.log('✅ Done'); |
| 198 | + } catch(err) { |
| 199 | + console.log('❌ Failed', err.message); |
| 200 | + } |
| 201 | + } |
| 202 | + console.log('\n✨ Batch Summarization Complete.'); |
| 203 | + } |
| 204 | + |
| 205 | + process.exit(0); |
| 206 | +} |
| 207 | + |
| 208 | +debugSessionState(); |
0 commit comments