> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# App startup and lifecycle

Startup is process state, owned by the clean architecture slice:

`useBootstrapEntrySDK` → `useAppLifecycleStore.start` → `AppLifecycleFacade` →
`StartAppLifecycle` → injected lifecycle source, scheduler and startup services.

The UI hook forwards its mount event only. It does not read onboarding state, subscribe to
AppState, initialize SDKs, schedule warmups, or choose startup gates. The facade starts once
per JS process, including across React remounts. A new process creates the graph again and
starts every required service; there is no persisted "services started" flag.

## Policy

Edit [`AppStartupPolicy`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/domain/policies/app-startup-policy.ts) to change **when**
a task starts. Edit [`AppStartupServices`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/infra/adapters/app-startup-services.ts)
to change **how** it reaches the device. Native collaborators are selected in
[`app-lifecycle-app-container.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/infra/container/app-lifecycle-app-container.ts).

The launch context is captured from the synchronously restored onboarding/tutorial flags.
Completing onboarding mid-session does not reinterpret that launch as a returning launch.

| Work                                  | Onboarding incomplete at launch                                                               | Onboarding completed at launch                   |
| ------------------------------------- | --------------------------------------------------------------------------------------------- | ------------------------------------------------ |
| Essential bootstrap                   | Existing splash/SDK sequence                                                                  | Existing splash/SDK sequence                     |
| Haptics, SFX, timing config           | After intro, splash hidden, and first FTUE screen mounted; two frame boundaries then idle     | After intro while active                         |
| Sampler                               | Same FTUE gate, and only once Start is pressed (its audio engine would duck the film's music) | After intro while active                         |
| Catalog sync, MIDI queue construction | Same FTUE gate                                                                                | After intro on idle                              |
| Song-version prefetch                 | Same FTUE gate, once RC has resolved                                                          | After intro on idle, once RC has resolved        |
| Tutorial cover prefetch               | Onboarding owns its tutorial preparation                                                      | Idle after intro only when resuming the tutorial |

This also covers a second cold launch when onboarding is still incomplete: the screen's
`fn_intro_1` event releases deferred work in that new process. A completed player never waits
for that event, because they no longer visit onboarding. A player resuming the tutorial gets
the gameplay services without waiting for onboarding either.

The first-screen signal is emitted before analytics delivery, so disabled analytics collection
does not block startup. Splash visibility and first-screen commit can arrive in either order;
both are required. The gate waits for a committed screen, not for the film to decode or a
network asset to load.

## Lifecycle actions

`AppLifecycleSource` bridges intro completion, splash visibility, first FTUE commit, Remote
Config changes, and native AppState events into the same use case. Events do not update React
presentation state. Each task has one startup job; repeated events cannot start it twice.
Pending scheduled work is canceled on inactive/background and scheduled again when active.
Already started services remain process-wide.

On a transition back to active, the use case requests RC refresh (unless gameplay is active)
and rechecks No Ads expiry. Consecutive active notifications do neither again. A changed RC
snapshot schedules version prefetch again without restarting sampler, haptics or other services.
The MIDI processor still enforces its existing tutorial/gameplay/audio/ad blocking and catalog
sync retains its own foreground/TTL behavior.

Essential SDK operations and their guarded boot telemetry live in
[`start-app-bootstrap.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/bootstrap/start-app-bootstrap.ts). The Remote Config fetch
starts early because it selects the monetization flow and the tutorial's songs, but only a player
past onboarding waits for it on the splash (`AppLaunchContext.waitForRemoteConfig`). A player
still before onboarding opens as soon as the rest is ready. Once its onboarding screen is seen,
the install's A/B-tested values are locked (`decideExperimentsForNewPlayer`,
[`experiment-decision.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/config/experiment-decision.ts)): the console's answer if
this launch's fetch lands within 3 s (or an earlier launch fetched it), else the in-app baseline.
Until then Start shows a loading state (the film plays meanwhile); the tutorial's songs and covers are fetched after it. Start waits for nothing else, and
never past 3 s from the screen being seen (`selectStartReady`, see [onboarding.md](/docs/features/onboarding.md)).
Every launch then settles the
lock in `publishRemoteConfigOutcome` (`settleExperimentsAtLaunch`): a player past onboarding with
no lock locks while the splash is up, a bumped `ab_lock_version` locks again while the splash is
up, and a fallback lock learns the console's round without moving. Analytics/crash,
attribution, IAP initialization and monetization binding keep their early paths. IAP and
attribution do not gate fresh-install navigation; IAP initialization must begin before callers
can request offerings. Returning launches wait for haptics; completed-tutorial launches also
register sampler/SFX splash warmups. Fresh-install `boot_timing` does not wait for deferred
gameplay warmups. Each deferred task reports failures independently without skipping other tasks.

## Validation

- Policy tests: `src/domain/__tests__/app-startup-policy.test.ts`.
- Facade integration tests with real domain classes and injected fakes:
  `src/infra/__tests__/app-lifecycle.test.ts`.
- Scheduling tests verify two frame boundaries, idle dispatch and cancellation:
  `src/infra/__tests__/app-startup-scheduler.test.ts`.
- Store and source contracts protect the thin UI and the existing native boot wiring.

Device validation remains manual/CI (`bun run e2e:test`): fresh install, relaunch before
finishing onboarding, resume tutorial, completed-player cold start, and background/foreground.
No on-device speedup is claimed by the unit tests.

***

<small>Source: [`docs/features/app-lifecycle.md`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/docs/features/app-lifecycle.md)</small>
