{"record":{"id":"1bad76fb16361076","repo":"mastra-ai/mastra","slug":"observational-memory-record-not-found-currentre","errorCode":null,"errorMessage":"Observational memory record not found: ${currentRecord.id}","messagePattern":"Observational memory record not found: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/memory/inmemory.ts","lineNumber":1157,"sourceCode":"    const { id, reflection, tokenCount, inputTokenCount, reflectedObservationLineCount } = input;\n    const record = this.findObservationalMemoryRecordById(id);\n    if (!record) {\n      throw new Error(`Observational memory record not found: ${id}`);\n    }\n\n    const existing = record.bufferedReflection || '';\n    record.bufferedReflection = existing ? `${existing}\\n\\n${reflection}` : reflection;\n    record.bufferedReflectionTokens = (record.bufferedReflectionTokens || 0) + tokenCount;\n    record.bufferedReflectionInputTokens = (record.bufferedReflectionInputTokens || 0) + inputTokenCount;\n    record.reflectedObservationLineCount = reflectedObservationLineCount;\n    record.updatedAt = new Date();\n  }\n\n  async swapBufferedReflectionToActive(input: SwapBufferedReflectionToActiveInput): Promise<ObservationalMemoryRecord> {\n    const { currentRecord } = input;\n    const record = this.findObservationalMemoryRecordById(currentRecord.id);\n    if (!record) {\n      throw new Error(`Observational memory record not found: ${currentRecord.id}`);\n    }\n\n    if (!record.bufferedReflection) {\n      throw new Error('No buffered reflection to swap');\n    }\n\n    const bufferedReflection = record.bufferedReflection;\n    const reflectedLineCount = record.reflectedObservationLineCount ?? 0;\n\n    // Split current activeObservations by the boundary line count.\n    // Lines 0..reflectedLineCount were reflected on → replaced by bufferedReflection.\n    // Lines after reflectedLineCount were added after reflection started → kept as-is.\n    const currentObservations = record.activeObservations ?? '';\n    const allLines = currentObservations.split('\\n');\n    const unreflectedLines = allLines.slice(reflectedLineCount);\n    const unreflectedContent = unreflectedLines.join('\\n').trim();\n\n    // New activeObservations = bufferedReflection + unreflected observations","sourceCodeStart":1139,"sourceCodeEnd":1175,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/memory/inmemory.ts#L1139-L1175","documentation":"swapBufferedReflectionToActive promotes a buffered reflection into the active observations by creating a new reflection generation. It first resolves the record by currentRecord.id and throws this error when the record no longer exists in the in-memory store. This prevents creating a bogus generation from a deleted or unknown record.","triggerScenarios":"Calling swapBufferedReflectionToActive({ currentRecord, tokenCount }) where currentRecord.id is not present in the store — typically because clearObservationalMemory ran, the process restarted, or the record id came from another storage instance/provider.","commonSituations":"Long-running reflection jobs that outlive an in-memory storage reset; passing a serialized record from a previous session; switching storage providers without migrating record ids.","solutions":["Re-fetch the latest observational memory record for the thread/resource before swapping instead of reusing a stale currentRecord object.","Avoid calling clearObservationalMemory while a reflection swap is pending.","Use a persistent storage provider if records must survive process restarts.","Wrap the call in try/catch and fall back to re-initializing observational memory for the thread."],"exampleFix":"// before\nawait memory.swapBufferedReflectionToActive({ currentRecord: staleRecord, tokenCount });\n// after\nconst current = await memory.getLatestObservationalMemoryRecord(threadId, resourceId);\nif (!current) return;\nawait memory.swapBufferedReflectionToActive({ currentRecord: current, tokenCount });","handlingStrategy":"try-catch","validationCode":"const current = await memory.getLatestObservationalMemoryRecord(threadId, resourceId);\nif (!current || current.id !== currentRecord.id) throw new Error('Stale observational memory record');","typeGuard":"function isCurrentRecord(r: ObservationalMemoryRecord | undefined, id: string): r is ObservationalMemoryRecord {\n  return r !== undefined && r.id === id;\n}","tryCatchPattern":"try {\n  const next = await memory.swapBufferedReflectionToActive({ currentRecord, tokenCount });\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Observational memory record not found')) {\n    currentRecord = await memory.getLatestObservationalMemoryRecord(threadId, resourceId);\n  } else throw e;\n}","preventionTips":["Re-fetch the record immediately before swapping.","Make reflection swap steps idempotent against retries.","Don't clear observational memory mid-reflection.","Use durable storage for long-running reflection jobs."],"tags":["memory","in-memory-storage","observational-memory"],"backgroundTag":"record-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}