{"record":{"id":"1f21d40aa55b4e72","repo":"mastra-ai/mastra","slug":"observational-memory-record-not-found-id","errorCode":null,"errorMessage":"Observational memory record not found: ${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":893,"sourceCode":"    const existing = this.db.observationalMemory.get(key) ?? [];\n    // Insert in order by generationCount descending (newest first)\n    let inserted = false;\n    for (let i = 0; i < existing.length; i++) {\n      if (record.generationCount >= existing[i]!.generationCount) {\n        existing.splice(i, 0, record);\n        inserted = true;\n        break;\n      }\n    }\n    if (!inserted) existing.push(record);\n    this.db.observationalMemory.set(key, existing);\n  }\n\n  async updateActiveObservations(input: UpdateActiveObservationsInput): Promise<void> {\n    const { id, observations, tokenCount, lastObservedAt, observedMessageIds } = input;\n    const record = this.findObservationalMemoryRecordById(id);\n    if (!record) {\n      throw new Error(`Observational memory record not found: ${id}`);\n    }\n\n    record.activeObservations = observations;\n    record.observationTokenCount = tokenCount;\n    record.totalTokensObserved += tokenCount;\n    // Reset pending tokens since we've now observed them\n    record.pendingMessageTokens = 0;\n\n    // Update timestamps (top-level, not in metadata)\n    record.lastObservedAt = lastObservedAt;\n    record.updatedAt = new Date();\n\n    // Store observed message IDs as safeguard against re-observation\n    if (observedMessageIds) {\n      record.observedMessageIds = observedMessageIds;\n    }\n  }\n","sourceCodeStart":875,"sourceCodeEnd":911,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/domains/memory/inmemory.ts#L875-L911","documentation":"updateActiveObservations resolves the observational-memory record by id via findObservationalMemoryRecordById and throws if no record with that id exists. The update (active observations, token counts, lastObservedAt) cannot be applied to a nonexistent record. This is a lookup/state error in the observational memory feature of the in-memory storage domain.","triggerScenarios":"Calling updateActiveObservations({ id: 'unknown', observations, tokenCount, lastObservedAt, observedMessageIds }) with an id that was never created via the observational memory API, was already deleted, or belongs to a different storage instance/restarted process.","commonSituations":"Background observation cycles outliving the record lifecycle; holding a record id across a storage reset (in-memory data is not durable); stale ids cached in memory/workflow state; passing a message id instead of an observational-record id.","solutions":["Create/obtain the record first and keep its returned id; only update ids returned by the creation API.","Check the record exists before updating (look it up via the storage domain's query/get method).","Catch the error and recreate the record if updates are best-effort in a background cycle."],"exampleFix":"// before\nawait storage.updateActiveObservations({ id: cachedId, observations, tokenCount, lastObservedAt, observedMessageIds });\n// after\ntry {\n  await storage.updateActiveObservations({ id: cachedId, observations, tokenCount, lastObservedAt, observedMessageIds });\n} catch {\n  const created = await storage.createObservationalMemoryRecord(/* ... */);\n}","handlingStrategy":"try-catch","validationCode":"const record = await storage.getObservationalMemoryRecord?.(id); // or equivalent lookup\nif (!record) {\n  throw new Error(`Record ${id} does not exist; create it before updating`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await storage.updateActiveObservations({ id, observations, tokenCount, lastObservedAt, observedMessageIds });\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Observational memory record not found')) {\n    await recreateRecordAndApply(id, { observations, tokenCount, lastObservedAt, observedMessageIds });\n    return;\n  }\n  throw e;\n}","preventionTips":["Only update ids returned by the record-creation API; never fabricate or reuse other id kinds.","Re-create the record if the in-memory store was restarted (records are not durable).","Track record lifecycle so updates never run after the record was deleted."],"tags":["storage","not-found","observational-memory","state"],"backgroundTag":"entity-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}