Skip to content

Improve cold start performance: cache session list for instant display #123

@lucharo

Description

@lucharo

Problem

Every time the iOS app is cold-started (process killed by OS while backgrounded), users see a loading spinner for 1-3 seconds before sessions appear. This happens on every reopen because:

  1. Zustand store initializes with isDataReady: false and empty sessions
  2. SessionsListWrapper shows a spinner when useSessionListViewData() returns null
  3. The app must fetch /v2/sessions, decrypt all session encryption keys (sequential loop), decrypt metadata + agentState per session, then call applyReady()

Sessions are not persisted to MMKV — only drafts, permission modes, and settings are cached locally.

Root Cause Analysis

In sessionSnapshot.ts, both decryption loops are fully sequential (await inside for):

  • Lines 75-90: Sequential encryption key decryption (decryptEncryptionKey per session)
  • Lines 95-149: Sequential session metadata/agentState decryption

In sync.ts:

  • applyReady() (the only place isDataReady becomes true) is called in exactly one place: #init() line 527, after sessionsSync.awaitQueue() + machinesSync.awaitQueue() complete
  • isDataReady starts as false and is never persisted

Proposed Fix

Phase 1: Parallelize decryption (low risk, high impact)

  • Use Promise.all (with concurrency limit) for the key decryption loop
  • Use Promise.all for the session metadata/agentState decryption loop

Phase 2: Cache session list to MMKV (medium risk, highest impact)

  • After applySessions(), serialize sessionListViewData to MMKV
  • On cold start, load cached data immediately before fetch completes (stale-while-revalidate)
  • Set isDataReady: true immediately if cache exists

Environment

  • iOS (Happier mobile app)
  • Affects all cold starts (any time iOS kills the app process in background)
  • More noticeable with many sessions (50+)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions