mastra-ai/mastra · error · Error

Harness session "${sessionId}" was not found

Error message

Harness session "${sessionId}" was not found

What it means

The in-memory harness storage overrides `updateSession` and looks the session up directly in its `#sessions` map; it throws this error when the id is absent. Because this override does not route through `loadSession` like the base class, the same missing-session condition raises here for in-memory stores.

Source

Thrown at packages/core/src/storage/domains/harness/inmemory.ts:53

  }

  async loadSession(sessionId: string): Promise<SessionRecord | null> {
    const record = this.#sessions.get(sessionId);
    return record ? cloneSessionRecord(record) : null;
  }

  async saveSession(record: SessionRecord): Promise<void> {
    this.#sessions.set(record.id, cloneSessionRecord(record));
  }

  async listSessions(): Promise<SessionRecord[]> {
    return [...this.#sessions.values()].map(cloneSessionRecord);
  }

  override async updateSession(sessionId: string, updates: SessionRecordUpdate): Promise<SessionRecord> {
    const record = this.#sessions.get(sessionId);
    if (!record) {
      throw new Error(`Harness session "${sessionId}" was not found`);
    }

    const next = cloneSessionRecord({
      ...record,
      ...updates,
      id: record.id,
      createdAt: record.createdAt,
      lastActivityAt: updates.lastActivityAt ?? new Date(),
    });
    this.#sessions.set(sessionId, next);
    return cloneSessionRecord(next);
  }
}

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Seed the session on the same in-memory instance via `createSession` before updating.
  2. Assert in tests that setup created the session on the exact instance under test.
  3. Centralize storage instance creation (DI/container) so all code shares one store.
  4. Check `listSessions()` output on that instance to confirm what actually exists.

Example fix

// before
const store = new InMemoryHarnessStorage();
store.updateSession('sess-1', updates); // throws: map empty
// after
const store = new InMemoryHarnessStorage();
await store.createSession({ id: 'sess-1', /* ... */ });
await store.updateSession('sess-1', updates);
Defensive patterns

Strategy: validation

Validate before calling

const store = getSharedHarnessStorage(); // one instance, not ad-hoc
const sessions = await store.listSessions();
if (!sessions.some(s => s.id === sessionId)) {
  await store.createSession({ id: sessionId });
}
await store.updateSession(sessionId, updates);

Type guard

function isTrackedSession(sessions: SessionRecord[], id: string): boolean {
  return sessions.some(s => s.id === id);
}

Try / catch

try {
  await store.updateSession(sessionId, updates);
} catch (e) {
  if (e instanceof Error && e.message.includes('was not found')) {
    throw new Error(`Session ${sessionId} not in in-memory store; check instance reuse`);
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling `updateSession` on an `InMemoryHarnessStorage` instance with an id not in its map: never created, already deleted, or created on a different instance.

Common situations: Multiple in-memory instances (per-test or per-request scoping) with ids shared across them; test setup that forgot to seed the session; hot-reload replacing the store instance between calls.

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/f2196e3e300f03ed. Report an issue: GitHub.