mastra-ai/mastra · error · Error

Observational memory record not found: ${id}

Error message

Observational memory record not found: ${id}

What it means

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.

Source

Thrown at packages/core/src/storage/domains/memory/inmemory.ts:893

    const existing = this.db.observationalMemory.get(key) ?? [];
    // Insert in order by generationCount descending (newest first)
    let inserted = false;
    for (let i = 0; i < existing.length; i++) {
      if (record.generationCount >= existing[i]!.generationCount) {
        existing.splice(i, 0, record);
        inserted = true;
        break;
      }
    }
    if (!inserted) existing.push(record);
    this.db.observationalMemory.set(key, existing);
  }

  async updateActiveObservations(input: UpdateActiveObservationsInput): Promise<void> {
    const { id, observations, tokenCount, lastObservedAt, observedMessageIds } = input;
    const record = this.findObservationalMemoryRecordById(id);
    if (!record) {
      throw new Error(`Observational memory record not found: ${id}`);
    }

    record.activeObservations = observations;
    record.observationTokenCount = tokenCount;
    record.totalTokensObserved += tokenCount;
    // Reset pending tokens since we've now observed them
    record.pendingMessageTokens = 0;

    // Update timestamps (top-level, not in metadata)
    record.lastObservedAt = lastObservedAt;
    record.updatedAt = new Date();

    // Store observed message IDs as safeguard against re-observation
    if (observedMessageIds) {
      record.observedMessageIds = observedMessageIds;
    }
  }

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Create/obtain the record first and keep its returned id; only update ids returned by the creation API.
  2. Check the record exists before updating (look it up via the storage domain's query/get method).
  3. Catch the error and recreate the record if updates are best-effort in a background cycle.

Example fix

// before
await storage.updateActiveObservations({ id: cachedId, observations, tokenCount, lastObservedAt, observedMessageIds });
// after
try {
  await storage.updateActiveObservations({ id: cachedId, observations, tokenCount, lastObservedAt, observedMessageIds });
} catch {
  const created = await storage.createObservationalMemoryRecord(/* ... */);
}
Defensive patterns

Strategy: try-catch

Validate before calling

const record = await storage.getObservationalMemoryRecord?.(id); // or equivalent lookup
if (!record) {
  throw new Error(`Record ${id} does not exist; create it before updating`);
}

Try / catch

try {
  await storage.updateActiveObservations({ id, observations, tokenCount, lastObservedAt, observedMessageIds });
} catch (e) {
  if (e instanceof Error && e.message.startsWith('Observational memory record not found')) {
    await recreateRecordAndApply(id, { observations, tokenCount, lastObservedAt, observedMessageIds });
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/1f21d40aa55b4e72. Report an issue: GitHub.