mastra-ai/mastra · error

@mastra/opencode: failed to initialize memory storage from $

Error message

@mastra/opencode: failed to initialize memory storage from ${storagePath}

What it means

The @mastra/opencode plugin creates a LibSQL store at a local file path to back Observational Memory. After initializing the store it calls store.getStore('memory'); if that returns null, the underlying memory storage could not be created, so the plugin aborts with this error including the storage URL.

Source

Thrown at integrations/opencode/src/index.ts:199

          }
        }
      }
    } catch {
      // Credentials not available from opencode — rely on .env
    }
    credentialsReady = true;
  };

  // Storage: SQLite via Mastra's LibSQLStore
  const dbRelativePath = config.storagePath ?? DEFAULT_STORAGE_PATH;
  const dbAbsolutePath = join(ctx.directory, dbRelativePath);
  await mkdir(dirname(dbAbsolutePath), { recursive: true });
  const storagePath = `file:${dbAbsolutePath}`;
  const store = new LibSQLStore({ id: 'mastra-om', url: storagePath });
  await store.init();
  const storage = await store.getStore('memory');
  if (!storage) {
    throw new Error(`@mastra/opencode: failed to initialize memory storage from ${storagePath}`);
  }

  // Observational Memory: uses Mastra's full OM class
  // Model string IDs (e.g., 'google/gemini-2.5-flash') are resolved by Mastra's provider registry.
  const om = new ObservationalMemory({
    storage,
    model: config.model,
    observation: config.observation,
    reflection: config.reflection,
    scope: config.scope,
    shareTokenBudget: config.shareTokenBudget,
  });

  // Notify user that OM is active (delayed to let TUI initialize)
  setTimeout(() => {
    void ctx.client.tui.showToast({
      body: {
        title: 'Mastra',

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Delete the existing DB file at the storagePath reported in the message (or its directory) and let the plugin recreate it, then retry.
  2. Update @mastra/core (LibSQLStore) and @mastra/opencode to matching versions so getStore('memory') is supported.
  3. Verify the dbAbsolutePath directory is writable and `await store.init()` completed without warnings before getStore.

Example fix

// before (corrupt db file from old version)
rm ~/.opencode/mastra-om.db
// after — restart the opencode session; plugin re-initializes storage cleanly
Defensive patterns

Strategy: try-catch

Validate before calling

const storage = await store.getStore('memory');
if (!storage) throw new Error(`memory storage unavailable at ${storagePath}; recreate the DB file`);

Type guard

function isMemoryStorage(s: unknown): boolean {
  return s != null && typeof (s as { getMessages?: unknown }).getMessages === 'function';
}

Try / catch

try {
  const plugin = await MastraPlugin(/* ... */);
} catch (e) {
  if ((e as Error).message.includes('failed to initialize memory storage')) {
    fs.rmSync(dbAbsolutePath, { force: true });
    // retry plugin init once
  } else throw e;
}

Prevention

When it happens

Trigger: Calling the MastraPlugin (or loading the @mastra/opencode plugin) where `new LibSQLStore({ url: 'file:...' }).init()` succeeds but getStore('memory') returns null — e.g. corrupted or unreadable DB file, incompatible store version, or init not persisting the memory domain.

Common situations: A stale/corrupt SQLite file left over from a previous version of the plugin; the DB path being created in a directory with odd permissions; running with an older @mastra/core whose LibSQLStore doesn't expose the memory domain.

Related errors


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