mastra-ai/mastra · error · Error

page value too large

Error message

page value too large

What it means

`list` guards against unreasonably large pagination by rejecting `page * perPage` offsets above `Number.MAX_SAFE_INTEGER / 2`, since such offsets cannot be represented safely and would never return meaningful data.

Source

Thrown at packages/core/src/storage/domains/agents/inmemory.ts:145

      metadata,
      status,
      entityIds,
      pinFavoritedFor,
      favoritedOnly,
    } = args || {};
    const { field, direction } = this.parseOrderBy(orderBy);

    // Normalize perPage for query (false → MAX_SAFE_INTEGER, 0 → 0, undefined → 100)
    const perPage = normalizePerPage(perPageInput, 100);

    if (page < 0) {
      throw new Error('page must be >= 0');
    }

    // Prevent unreasonably large page values
    const maxOffset = Number.MAX_SAFE_INTEGER / 2;
    if (page * perPage > maxOffset) {
      throw new Error('page value too large');
    }

    // Get all agents and apply filters
    let agents = Array.from(this.db.agents.values());

    // Restrict to a set of IDs (used by ?favoritedOnly=true).
    // An empty array means "no candidates" -> empty result.
    if (entityIds !== undefined) {
      if (entityIds.length === 0) {
        return {
          agents: [],
          total: 0,
          page,
          perPage: perPageInput === false ? false : perPage,
          hasMore: false,
        };
      }
      const idSet = new Set(entityIds);

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Keep page numbers small and paginate normally; use `perPage: false` only with page 0 for fetch-all
  2. Cap page at a sane maximum before calling `list`
  3. Fix pagination loops to stop when results are empty instead of incrementing indefinitely

Example fix

// before
await agents.list({ page: 2, perPage: false });
// after
await agents.list({ page: 0, perPage: false }); // fetch-all must use page 0
Defensive patterns

Strategy: validation

Validate before calling

const MAX_OFFSET = Number.MAX_SAFE_INTEGER / 2;
if (page * perPage > MAX_OFFSET) {
  throw new Error('page/perPage combination too large');
}
await agentsDomain.list({ page, perPage });

Type guard

null

Try / catch

try {
  return await agentsDomain.list({ page, perPage });
} catch (e) {
  if (e instanceof Error && e.message === 'page value too large') {
    return agentsDomain.list({ page: 0, perPage: 100 });
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling `list({ page: <huge number> })`, e.g. `perPage: false` (normalized to MAX_SAFE_INTEGER) combined with any page > 0, or passing attacker-controlled / erroneous page numbers.

Common situations: Using `perPage: false` (fetch-all) with a nonzero page; unbounded loop incrementing `page` on empty results; malicious query params requesting page=1e15.

Related errors


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