{"record":{"id":"9262871d0e09576b","repo":"mastra-ai/mastra","slug":"no-buffered-reflection-to-swap","errorCode":null,"errorMessage":"No buffered reflection to swap","messagePattern":"No buffered reflection to swap","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/domains/memory/inmemory.ts","lineNumber":1161,"sourceCode":"    }\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\n    const newObservations = unreflectedContent ? `${bufferedReflection}\\n\\n${unreflectedContent}` : bufferedReflection;\n\n    // Create a new generation with the merged content.\n    // tokenCount is computed by the processor using its token counter on the combined content.","sourceCodeStart":1143,"sourceCodeEnd":1179,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/memory/inmemory.ts#L1143-L1179","documentation":"swapBufferedReflectionToActive throws this when the resolved record exists but has no bufferedReflection, meaning there is nothing accumulated to promote into active observations. The library treats an empty buffer as an invalid swap request rather than a no-op, so callers don't silently lose track of reflection state.","triggerScenarios":"Calling swapBufferedReflectionToActive before updateBufferedReflection has ever stored a buffer for the record, or calling it twice — the first swap clears bufferedReflection, so a second call with the same (now stale) currentRecord fails.","commonSituations":"Double-execution of a reflection workflow step (e.g. retry after a partial failure); race between the buffering and swapping stages of observational memory; calling the internal API manually in tests or custom processors without buffering first.","solutions":["Only call swapBufferedReflectionToActive after updateBufferedReflection has stored a buffer for the record.","Guard with a check on the record's bufferedReflection before swapping.","Re-fetch the record to confirm the buffer hasn't already been consumed by a prior swap.","Make the reflection workflow step idempotent so retries don't trigger a second swap."],"exampleFix":"// before\nawait memory.swapBufferedReflectionToActive({ currentRecord, tokenCount });\n// after\nif (currentRecord.bufferedReflection) {\n  await memory.swapBufferedReflectionToActive({ currentRecord, tokenCount });\n}","handlingStrategy":"validation","validationCode":"if (!currentRecord.bufferedReflection) {\n  throw new Error('Refusing to swap: no buffered reflection accumulated');\n}","typeGuard":"function hasBufferedReflection(r: ObservationalMemoryRecord): r is ObservationalMemoryRecord & { bufferedReflection: string } {\n  return typeof r.bufferedReflection === 'string' && r.bufferedReflection.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Only call swap after a successful updateBufferedReflection.","Clear bufferedReflection bookkeeping after each swap so double-swaps are detectable.","Serialize reflection steps to avoid concurrent double execution.","Check record.reflectedObservationLineCount is set before swapping."],"tags":["memory","observational-memory","state-conflict"],"backgroundTag":"empty-buffer-swap","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}