linshenkx/prompt-optimizer · error · FavoriteValidationError

favorites payload exceeds hard limit of

Error message

favorites payload exceeds hard limit of ${FAVORITES_HARD_LIMIT_BYTES} bytes

What it means

Collection-wide guard in assertFavoritesPayloadWithinBudget: after each favorite passes the metadata and per-item checks, the total serialized size of the whole favorites array must stay under FAVORITES_HARD_LIMIT_BYTES. A soft-limit warning is computed afterwards but the hard throw happens first.

Solutions

  1. Delete old or unused favorites to bring total serialized size back under the limit
  2. Reduce oversized individual entries (see the per-item limit error)
  3. Track total size (TextEncoder on the serialized array) and prune or archive before saving new entries

Example fix

// before
await manager.addFavorite(newFav); // store already near cap

// after
const all = await manager.getFavorites();
const total = new TextEncoder().encode(JSON.stringify(all)).length;
if (total + new TextEncoder().encode(JSON.stringify(newFav)).length > LIMIT) {
  await manager.deleteFavorite(oldestId);
}
await manager.addFavorite(newFav);
Defensive patterns

Strategy: fallback

Validate before calling

const all = await manager.getFavorites();
const totalBytes = new TextEncoder().encode(JSON.stringify(all)).length;
const incomingBytes = new TextEncoder().encode(JSON.stringify(newFavorite)).length;
if (totalBytes + incomingBytes > FAVORITES_HARD_LIMIT_BYTES) {
  await manager.deleteFavorite(oldestFavoriteId); // prune before saving
}

Try / catch

try {
  await manager.addFavorite(newFav);
} catch (e) {
  if (e instanceof FavoriteValidationError && /payload exceeds hard limit/.test(e.message)) {
    await pruneOldestFavorites();
    await manager.addFavorite(newFav);
  }
}

Prevention

When it happens

Trigger: Saving a favorite when JSON.stringify(favorites) across the entire collection exceeds FAVORITES_HARD_LIMIT_BYTES — the accumulation of all entries, not any single one, trips it.

Common situations: Long-lived installs accumulating thousands of favorites, importing a large batch at once, or many entries each near the per-item limit.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/ee963d5f70b7e07b. Report an issue: GitHub.

Appendix: source

Thrown at packages/core/src/services/favorite/storage-guards.ts:138

export const assertFavoritesPayloadWithinBudget = (
  favorites: FavoritePrompt[],
  options?: {
    warnOnSoftLimit?: boolean
    logWarning?: (message: string) => void
  },
): {
  totalBytes: number
  softLimitExceeded: boolean
} => {
  favorites.forEach((favorite) => {
    assertFavoriteMetadataHasNoInlineImages(favorite.metadata)
    assertFavoriteFitsItemBudget(favorite)
  })

  const totalBytes = getSerializedByteLength(JSON.stringify(favorites))
  if (totalBytes > FAVORITES_HARD_LIMIT_BYTES) {
    throw new FavoriteValidationError(
      `favorites payload exceeds hard limit of ${FAVORITES_HARD_LIMIT_BYTES} bytes`,
    )
  }

  const softLimitExceeded =
    totalBytes > FAVORITES_SOFT_LIMIT_BYTES - FAVORITES_SOFT_WARNING_HEADROOM_BYTES
  if (softLimitExceeded && options?.warnOnSoftLimit) {
    const logWarning = options.logWarning ?? console.warn
    logWarning(
      `favorites payload exceeds soft limit (${totalBytes} bytes > ${FAVORITES_SOFT_LIMIT_BYTES} bytes)`,
    )
  }

  return {
    totalBytes,
    softLimitExceeded,
  }
}

View on GitHub (pinned to 3e677b1d9f)