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

# Ad Trigger Logic — Magic Piano

> **Living spec** map các flow trigger Ads của PO → implementation. Nguồn: PO draw.io **`SimulationAdFlow`** (simulator/). Cập nhật theo từng flow khi implement.
> Trạng thái: ✅ done · 🔨 implementing · ⏳ chưa làm.

## Biến thể logic (Interstitial) — A/B qua `interlogic`

Switch ở `src/core/sdk/ad-flow.ts` (`getAdFlow` / `setAdFlow` / `isSongEndFlow`), **default = Flow A**, feed từ Remote Config `fs_logic` (1 = Flow B); sau khi slice boot thì theo `fs.flow` của arm (`setAdFlowSource`). Hai flow **loại trừ nhau**.

**Màn hình chỉ báo hành động** (`reportAdAction`, [`ad-actions.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/sdk/ad-actions.ts)): bấm bài ở Home, hết bài (Game / Learn), nút ở Result, Result → Home, hết một lượt chơi (pause), thoát Free mode. Không màn hình nào tự xét Flow A/B nữa. **Bộ quyết định** chọn: trước khi slice boot là bảng Flow A/B + các gate hiện tại (`todayPlacementFor` + `maybeShowInterstitial`); sau khi boot là policy của arm đang chạy (`createSliceFullscreenDecider` → `IFullscreenLogic` của arm). Tên placement trong analytics giữ nguyên.

- **Flow A — Baseline (USER ACTION)** ✅: interstitial bắn khi user **bấm** — `select_song` (Free→Play), `song_result` (mọi nút ở Result), `pause` (Σ play>10s), `free_home`.
- **Flow B — Variant (SONG_END)** ✅: interstitial bắn khi **bài kết thúc** — `song_end` (game-over → trước Result) + `result_home` (Result→Home, gate `song_count≥2`). Replay / Next / recommendation **không** ad; `select_song` / `song_result` / `pause` **tắt** ở Flow B.
- `free_home` (keyboard → Songs) fire ở **CẢ 2 flow** (orthogonal với A/B song-flow).
- `song_end` ở **Learn Mode** (run challenge của stage cuối xong — tức nốt cuối được chấm, không chờ nhạc tới cue marker; board đứng 3.5s: nhạc chơi tiếp 2s sau nốt cuối rồi fade 1.5s về im lặng → ad đè lên result panel đã vẽ sẵn, count-up + nút bấm chờ ad xong, 1 lần/run) chỉ ở **Flow B**, như `song_end` của Game — `useLearnResultInterstitial`. **Arm chạy Learn theo stage (`learn_fs.mode = stages`, Aggressive / Very Aggressive) không có FS lúc hết bài Learn**: FS nằm giữa các stage. **Arm tắt FS trong Learn (`learn_fs.mode = off`, Friendly / Balance / không arm — design final 10/11) không có FS nào trong Learn**, cả giữa các stage lẫn lúc hết bài. Learn result **Replay / Next / Song list** mirror `PlayResultScreen`: Flow A `song_result` + `waitForFullscreenAdDismissed` trước khi mount lại count-in / navigate; Flow B Replay/Next silent, Song list → `result_home`.
- **Learn theo stage** (`learn_fs.mode = stages`, Aggressive / Very Aggressive) — `useLearnStageInterstitial`:
  - **Khi nào:** hết mỗi stage trước stage cuối, sau nhịp 1.5s board xanh, màn Learn báo `learn_stage_complete`. Arm quyết định có FS không: lần đầu sau stage `learn_fs.first_stage` (2), rồi mỗi `learn_fs.interval` (2) stage, không bao giờ ở stage cuối (7 stage → sau stage 2, 4, 6; 5 stage → sau 2, 4).
  - **Trước thẻ hoàn thành stage:** ad hiện trước, thẻ chờ ad đóng hẳn (`waitForFullscreenAdDismissed`) rồi mới mở.
  - **Mỗi lượt vào bài:** mỗi stage chỉ báo 1 lần. Retry cùng stage không báo lại (không có FS thứ hai). Thoát bài rồi vào lại thì đếm lại từ đầu.
  - **Mọi stage trước stage cuối đều được báo**, kể cả stage không có FS, để luật `fs.skip_after_fs` thấy đủ các hành động.
  - **Vẫn theo** cooldown 15s, `fs.skip_after_fs`, VIP / No Ads, như mọi FS.
  - **Analytics:** `fullads_trigger` / `fullads_show` của ad này có thêm `location = learn_mode_stage_<n>` (`screen` = `others`). Các FS khác không gửi `location`.

**`song_count`** (Flow B): số bài chơi xong **kể từ lần cuối user ở Home**. Reset = 0 khi focus màn Songs; +1 mỗi game-over (chơi lại 1 bài cũng tính). Gate `result_home` = `≥ 2`.

## Điều kiện gating (Piano values)

| Điều kiện                       | Ý nghĩa                                                                                                                                                                                                                                                                                                                                            |
| ------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `song_end trọn đời ≥ 2`         | **(MỚI, mọi placement, cả 2 flow)** interstitial **đầu tiên** của user chỉ hiện sau `song_end` thứ 2 (tutorial = bài 1, bài user tự chọn đầu tiên = bài 2). Số đếm = `accumulated_count` của event `song_end` (`getLifetimeEventCount("song_end")`, lưu MMKV). Chặn trước cả khi khởi động SDK; ngưỡng `AD_PAYWALL.firstInterstitialSongEndCount`. |
| `Ad Interval > 15s`             | Tối thiểu 15s giữa 2 interstitial (global frequency-cap).                                                                                                                                                                                                                                                                                          |
| `Rewarded → Interstitial ≥ 30s` | **(MỚI, 1 chiều)** interstitial chỉ show khi ≥ 30s kể từ rewarded gần nhất; rewarded **KHÔNG** bị gate, chỉ ghi timestamp (`markRewardedShown`).                                                                                                                                                                                                   |
| `Σ play_time > 10s`             | **Cộng dồn** play-time qua các session; tổng > 10s mới đủ điều kiện (pause flow). Reset = 0 sau khi ad hiện.                                                                                                                                                                                                                                       |
| `song_count ≥ 2`                | (Flow B, warm-up) `result_home` chỉ show sau khi user chơi xong ≥ 2 bài kể từ lần cuối ở Home.                                                                                                                                                                                                                                                     |

***

## ADS — triggers

### 1. Select Song (user action) ✅ code (⏳ device verify)

> Impl: rewarded — `SongsScreen.onPressRewarded` (`getAdsService().showRewarded("unlock_song")` → `addUserSong`); select-song interstitial — Song List báo `reportAction({ kind: "home_song_tap" })` (`song-list-flow-store.ts`), placement `select_song`.

- **Locked Song** → Show Rewarded → **unlock FULL vĩnh viễn** (PO override "Play 1/2 section" — dev chưa tách 1/2 song nên ignore).
- **Free Song** → bấm Play → **Show Interstitial** (global `Ad Interval > 15s` only, subscriber skip) → vào gameplay.

### 2. Interstitial — màn PAUSE ✅ code (⏳ device verify accumulator)

> Impl: `src/core/sdk/ads.pure.ts` (`tickToSeconds` + `accumulatePlayTime`, unit-tested) + `ads.ts` (`reportPlaySessionEnd` / `reportPlaySessionEndTicks`: in-memory accumulator cộng dồn, rồi báo hành động `play_session_end`) + wire `PlayScreen` `handleSongs`/`handleRetry`.

```
User ở màn Pause:
 ├─ Continue        → Continue Playing (KHÔNG ad)
 ├─ Home          ─┐   bank play-time của session vừa rồi → Σ accumulated
 └─ Replay/Retry  ─┴─→ Σ play_time > 10s ?  (+ interval > 15s)
                        ├─ Yes → Show Interstitial → reset Σ = 0 → (về Home / Play lại)
                        └─ No  → (về Home / Play lại) thẳng, GIỮ Σ cho lần sau
```

- Gating: **Σ play_time (cộng dồn xuyên session) > 10s** **+** global `Ad Interval > 15s`.
- Reset accumulator = 0 **chỉ khi** ad thực sự hiện (cooldown chặn → giữ Σ, cộng tiếp lần sau).
- Continue **không** show ad. Accumulator in-memory → reset khi tắt app.
- VD: chơi 4s→Home (Σ=4, không ad) → chơi 7s→Home (Σ=11>10 → ad → Σ=0).

### 3. Interstitial — màn SONG RESULT ✅ code (⏳ device verify)

> Impl: `reportAdAction({ kind: "result_action", placement })` (gate **chỉ 15s**, KHÁC Pause) → `PlayScreenBody gateResultAd` → `PlayScreenHudOverlayGroup` (SessionResultOverlay handlers handleResultReplay/Songs/Next). Tách khỏi pause-accumulator vì 2 flow trước đây chung handler `onRetry`/`onBack`.

- Song kết thúc → **Home / Replay / Next** → (`Ad Interval > 15s`?) → Yes: Interstitial → nav; No: nav thẳng.
- "Next" hiện = về Songs (chưa có next-song thật); full Play-Next defer.

### 4. Interstitial — FREE mode Home ✅ code (⏳ device verify)

> Impl: `ads.pure.ts` (`isLeavingFreeMode`, test) + `reportAdAction({ kind: "free_mode_exit" })` (sau `canShowFreeModeExitAd`) + `app/(app)/(tabs)/_layout.tsx` (tabPress listener trên tab `songs`).

- Free mode (tab **keyboard**) → bấm tab **Songs** (= Home / Song List) → (interval > 15s) → Interstitial → vào Songs.
- Gating: CHỈ global `Ad Interval > 15s` (không play-time threshold). Chung cap 15s với Pause. Chỉ **keyboard → Songs** mới show (tab khác không).
- Cơ chế: SideTabBar emit `tabPress` (canPreventDefault) → listener `preventDefault()` → ad → `navigate("songs")`.

### 5. Banner ✅ code (⏳ device verify) — tab Keyboard (top)

> PO design: banner ở top tab keyboard, đẩy components xuống; **subscriber → ẩn**. Impl: `@core/ads` unit ids + `isPremium` thật + `src/components/BannerHost.tsx` + `BannerSlot.tsx` (một banner nạp sẵn, hiện ngay trong slot đang mở; bỏ từ lúc đếm ngược của một bài Game Mode tới khi nốt cuối kết thúc; gate qua `useShouldShowBanner()` → ẩn khi premium/no-ads **hoặc** fullscreen interstitial/rewarded đang che màn hình; result screens pass `duringFullscreenLatch` so the post-close latch does not blank the banner during score count-up) → `AdMobBanner` (`BannerAd` 320×50) + `KeyboardScreen` first child của `inner`. Tier: High trước, không fill → thay view bằng All; cả hai không fill → bỏ view, thử lại từ High sau 5 s → 60 s, không giới hạn (`banner-tiers.ts`). **JS-only → reload, không prebuild.**

### 6. App-Open ✅ code (⏳ device verify; ⏳ ID unit thật)

> PO yêu cầu 15/10 ("Open Ads after song start 4"). Impl: luật thuần [`app-open-policy.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/ads/app-open-policy.ts) (`appOpenBlock` — trả lý do chặn đầu tiên, hoặc `null`) + format `appOpen` trong [`ads-engine.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/ads/ads-engine.ts) + runtime [`app-open.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/sdk/app-open.ts) (`runLaunchAppOpen` / `runResumeAppOpen`) + [`AppOpenHost`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/app-open/components/AppOpenHost.tsx) (mount ở `app/_layout.tsx`; luật của host ở `app-open-host.pure.ts`). Placement nội bộ: `app_open` (cold launch), `foreground_return` (quay lại); event `openads_*` phía hiển thị báo `trigger` = `cold_start` / `warm_resume` (xem [analytics-events.md](/docs/analytics-events.md)).

**Gate chung (cả 2 trigger)** — thiếu một điều kiện là không hiện:

- `song_end` trọn đời **≥ 4** — 4 bài **chơi xong**, cộng dồn (bài tutorial và Learn tính; bắt đầu rồi thoát ra không tính — `getLifetimeEventCount("song_end")`, `APP_OPEN_MIN_SONG_ENDS`). PO 23/10/2026; trước đó đếm `song_start`.
- Không premium / No Ads (QA "Show Ads for Subscriber" override như interstitial).
- **≥ 120s** kể từ App Open gần nhất _được hiển thị_.
- **≥ 60s** kể từ interstitial hoặc rewarded gần nhất _được hiển thị_ (Skia video backfill **không** tính).
- Đã có unit App Open (`EXPO_PUBLIC_ADMOB_IOS_APP_OPEN`, chỉ tier **All**, không có `_HIGH`; rỗng = App Open tắt).
- Mốc thời gian lưu **in-memory theo phiên app** (`ad-lifecycle.ts` `noteFullScreenAdDisplayed` / `getAppOpenGaps`): cold launch không bao giờ bị chặn bởi ad của phiên trước.
- **1 chiều:** App Open **không** dời mốc 15s của interstitial.

**Trigger 1 — cold launch** (từ lần mở thứ 2):

- Chỉ từ launch **#2** (`useSessionStore.currentLaunch`) và khi onboarding + tutorial đã xong; launch #1, FTUE chưa xong, **hoặc gate tĩnh đã chặn** (chưa có unit = App Open tắt, premium / No Ads, `song_end` \< 4 — `appOpenStaticBlockNow`) → bước launch đánh dấu `done` ngay, không đụng SDK, nên paywall / popup không phải chờ.
- **Nạp trong lúc splash:** host quyết 1 lần khi entitlements + Remote Config đã hydrate (cùng lúc `SessionPaywallHost` ghi launch); chỉ nạp khi qua gate tĩnh.
- Splash đã tắt và `/songs` đang hiện, không bị che → **hiện nếu đã nạp xong; chưa thì chờ tối đa 5s rồi bỏ qua** (lần nạp vẫn chạy tiếp cho lần quay lại). Ad chỉ có trong tay **sau** mốc 5s (vd. app vào background giữa lúc chờ, vòng chờ thức dậy muộn) → bỏ qua, không hiện. Sau lúc chờ, gate được kiểm lại, rồi host kiểm lần cuối (vẫn `/songs`, không bị che, app `active`, **không có ad full-screen nào đang hiện / vừa đóng** — `isFullScreenAdVisible()`).
- **Paywall mở app (⑥ subscription từ session 2, ⑩ discount ở session 3), popup No Ads lúc mở app (Aggressive: `noads_popup.app_open_from_session` = 2, thay paywall mở app) và popup "Learn Mode unlocked!" chờ bước này xong** — `useAppOpenLaunchStore.phase === "done"` ([`app-open-launch-store.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/sdk/app-open-launch-store.ts), in-memory). Hiện, bỏ qua, bị chặn hay lỗi đều → `done`; nếu ad **đã hiện**, `done` chỉ đến khi ad đã trả màn hình (`waitForFullscreenAdDismissed`, \~300ms sau khi đóng). **Safety:** bước chưa bắt đầu sau 15s kể từ lúc biết launch → host tự đánh `done`, để paywall / popup không chờ vô hạn.
- **Paywall mở app còn chờ Song List:** ngoài `done` + màn hình ngang, offer chỉ mở khi đang ở `/songs`, không bị che, và không có ad full-screen nào đang hiện / đang trả màn hình (`launch-paywall.pure.ts`). Chưa đủ thì offer **vẫn nợ** và mở lần tới khi đủ (vd. quay lại Song List sau một bài). Discount ⑩ chỉ được đánh dấu "đã hiện" (`markDiscountShown`) **lúc nó thật sự mở** — người chơi thoát app trong lúc App Open thì lần mở sau vẫn được offer.

**Trigger 2 — quay lại từ background:**

- Chỉ khi `AppState` đi **background → active** thật; `inactive` (Control Center, cuộc gọi, app switcher) **không** tính là một chuyến đi.
- **Arm có `app_open` (sau khi slice boot):** `app_open.from_session` chặn cả lần quay lại, không chỉ cold launch (`currentLaunch` truyền vào `runResumeAppOpen`, lý do `firstLaunch`). Balance / Aggressive / Very Aggressive: từ launch 2 (session 1 không có App Open). **Friendly và không arm: từ launch 1** — đạt Habit (4 bài) ở session nào cũng có App Open (design final 10/11). Trước khi slice boot (không có arm), lần quay lại vẫn không xét số launch như cũ.
- **`app_open.on_resume = false` (Balance / Aggressive / Very Aggressive, design final 10/11):** chỉ có App Open lúc mở app, không có lúc quay lại từ background (lý do `resumeOff`).
- Chỉ đè lên **màn menu**: không phải `/play`, không phải `/learn` (kể cả đang pause); không trước khi onboarding + tutorial xong; **không khi có gì che màn hình** (paywall, sheet, popup, ad — `isScreenCovered()`), **và không khi một ad full-screen đang hiện hoặc vừa đóng** (`isFullScreenAdVisible()`, có latch 1.5s).
- **Không phải quay lại sau khi tap ad:** có ad full-screen đang hiện lúc app vào background (cờ visible có latch 1.5s), hoặc ad vừa đóng \< 1s trước đó (`APP_OPEN_AD_TAP_RETURN_MS`).
- **Chỉ hiện nếu đã nạp sẵn — không chờ.** Chưa nạp thì nạp cho lần sau. Bỏ qua trong lúc bước launch còn `pending`.
- **Luôn giữ sẵn 1 App Open khi người chơi được phép có:** host nạp (`preloadAppOpenIfEligible`, luật `shouldKeepAppOpenReady`) ngay khi bước launch xong (`done` — kể cả launch #1, launch bị gate tĩnh chặn, safety 15s), và lại mỗi khi premium / No Ads (vd. cờ cũ được RevenueCat xoá sau quyết định launch), `song_end` chạm 4 hoặc FTUE xong làm thay đổi — nên lần quay lại **đầu tiên** đã có ad để hiện. Chỉ đổi lúc _nạp_, không đổi lúc _hiện_; đang nạp / đã nạp thì không xin thêm; giữa bài thì engine đậu lại tới khi chơi xong.

**Nạp / hiện:** cùng engine với format khác — mỗi tier (**High**, **All**) có 1 preload pool của SDK, hiện thì lấy từ High rồi tới All, đậu khi gameplay. Khác: ad để **> 4h** do pool của SDK tự bỏ (`isStaleByPolicy`), lúc hiện sẽ không lấy phải; `start()` / `preloadAll()` **không** nạp App Open (chỉ host/runtime gọi `preloadAppOpen`); sau mỗi lần hiện (được hay lỗi) nạp ngay ad kế. Ngược lại, App Open khởi động SDK **chỉ init** (`ensureAdsSdk` → `AdsService.initSdk()`), **không** gọi `start()` → không bao giờ nạp lại interstitial / rewarded ngoài nhịp của chúng (không thêm `fullads_request` / `videoads_request`, không dựng lại pool đã lỗi ngoài nhịp của chúng).

**Analytics: không có gì** — không `openads_*`, không `fullads_*` / `videoads_*`, không `ad_impression` / AppsFlyer revenue (PO chưa chỉ định event). Xem [admob-integration.md §8](/docs/admob-integration.md#8-analytics).

### 7. Full Screen Native (FSN) ✅ code (⏳ device verify)

> PO 03/11/2026, theo magicPad (`mon_pkg`). Quảng cáo native toàn màn hình hiện **ngay sau** một FS hoặc RV **đã hiện**, theo arm A/B của install — RC `mon_pkg` (JSON, `{"variant":"balance"}`). Slice monetization quyết định khi đã boot ([`ad-policy.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/domain/policies/ad-policy.ts) `decideFsn`, xem [Monetization slice](#monetization-slice-mon_pkg)); trước đó là luật hiện tại [`fsn-policy.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/ads/fsn-policy.ts) trên [`mon-pkg.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/config/mon-pkg.ts). Hai luật cho cùng kết quả ở mọi arm mà luật hiện tại biết ([`fsn-slice-parity.test.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/ads/__tests__/fsn-slice-parity.test.ts)).

| Arm               | Sau FS | Sau RV | Bắt đầu từ                                                                                                    | Video mở khóa bài                                                                                     |
| ----------------- | ------ | ------ | ------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| ngoài test (`{}`) | —      | —      | như `friendly`                                                                                                | cả 2 mode (như `friendly`; `isRewardedUnlockPerMode`, [songs.md](/docs/features/songs.md)) |
| `friendly`        | —      | —      | —                                                                                                             | cả 2 mode                                                                                             |
| `balance`         | ✅      | —      | **D1** = 24 giờ sau lần mở app đầu tiên (`useSessionStore.firstOpenAt`; bản cài từ trước bản này = đã qua D1) | **chỉ mode đã xem video**                                                                             |
| `aggressive`      | ✅      | ✅      | **Habit** = `song_end` trọn đời ≥ 4 (tính cả tutorial)                                                        | cả 2 mode                                                                                             |
| `very_aggressive` | ✅      | ✅      | **Habit**                                                                                                     | cả 2 mode                                                                                             |

- Console có thể ghi đè slot bằng `"fsn":"off" / "after_fs" / "after_fs_and_rv"` và mốc bắt đầu bằng `"fsn_from":"now" / "d1" / "habit"`, ở mọi arm và cả khi không có arm. Tên của magicPad cũng nhận: `control` / `bal` / `aa` / `va`.
- Trước khi slice boot (Remote Config của phiên chưa về), luật hiện tại trả lời: nó không biết `very_aggressive`, `fsn_from`, và bỏ qua `fsn` khi không có arm.
- Không cho premium / No Ads (cùng cổng với FS, có QA override).
- **Mọi FS đã hiện** đều có FSN theo sau (mọi placement); **mọi RV đã hiện**, nhận thưởng hay đóng sớm.
- **Chỉ hiện nếu đã nạp sẵn — không chờ.** Chưa nạp thì bỏ qua và nạp cho lần sau. Nạp lỗi thì thử lại 5s → 60s.
- **FSN được nạp ngay khi một FS hoặc video bắt đầu.**
  - Đường của slice: `AdsGatewayAdapter.show("fs" | "rv")` ([`ads-gateway-adapter.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/infra/adapters/ads-gateway-adapter.ts)) gọi `fsn.prepare` trước `showInterstitial` / `showRewarded`. Đường cũ của app: `FsnAdBreakAdsService`.
  - Slot mở giữa session (đạt Habit, qua D1) chưa có ad nào, nên FS hay video đầu tiên sau đó là cơ hội nạp duy nhất: FSN nạp trong lúc ad đó đang hiện.
  - Trước đây đường của slice không nạp trước. Vì vậy FS ở lần hết bài đạt Habit luôn thiếu FSN (`nativeads_show_notready`), build 23 và 25.
- **Lần nạp bị đậu (giữa bài) thì vào hàng chờ, không bị bỏ** (`deferLoad` → `scheduleDeferredAdPreload`).
- Nút đóng đếm ngược **3 giây**; bấm vào quảng cáo thì FSN tự đóng.
- Lệnh hiện FS / RV chỉ trả về **sau khi FSN đóng**: phần thưởng RV được trao sau FSN (thứ tự của magicPad), và mốc 15s / 30s tính từ lúc hết lượt quảng cáo.
- User property `mon_pkg_variant` = `friendly` / `balance` / `aggressive` / `very_aggressive` / `default`. Event: `nativeads_show`, `nativeads_click`, `ad_impression` với `ad_format: native` (trước 19/11/2026 là `fullscreen_native`) ([analytics-events.md](/docs/analytics-events.md)).
- Dev build: `EXPO_PUBLIC_RC_MON_PKG={"variant":"aggressive"}` trong `.env.local` để ép arm trên máy đang chạy Metro.
- **Arm bị khoá theo từng bản cài** ([`experiment-lock.pure.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/config/experiment-lock.pure.ts)): `mon_pkg` được chốt một lần (cùng `tutorial_song`), lưu MMKV. Slice đọc `mon_pkg` theo thứ tự: luật DevTool → `EXPO_PUBLIC_RC_MON_PKG` (dev) → **giá trị đã khoá** → console (`readEffectiveMonetizationValue`). Lần fetch về muộn (Firebase gán người chơi fallback vào một arm) không đổi arm nữa. Người chốt ở fallback chạy `{}` = `default` (luật Friendly). Chỉ khi console có `ab_lock_version` lớn hơn số đã khoá, app mới khoá lại theo cấu hình mới: tăng số khi bắt đầu hoặc kết thúc một đợt test, không tăng khi test đang chạy.

***

## Monetization slice (`mon_pkg`)

Kiến trúc cho ads A/B (`mon_pkg` v26), theo lớp clean-architecture như song catalog ([architecture.md](/docs/architecture.md)):

- **Domain** (`src/domain`): mỗi arm một file trong `policies/mon-arms/` + [`MonArmRegistry`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/domain/policies/mon-arm-registry.ts); luật thuần [`ad-policy.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/domain/policies/ad-policy.ts) (FS, Learn theo stage, FSN, App Open, popup No Ads, banner) và [`fs-spacing.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/domain/policies/fs-spacing.ts); use case trong `use-cases/monetization/`; API duy nhất là [`MonetizationFacade`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/domain/facades/monetization-facade.ts).
- **Data** (`src/data`): đọc `mon_pkg` / `fs_logic` / `song_pools` ([`mon-pkg-mapper.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/data/mappers/mon-pkg-mapper.ts)): field có mặt đè mặc định của arm, field sai giữ mặc định, field lạ bị bỏ qua (có ghi chú QA); JSON không phải object giữ giá trị tốt gần nhất. Repository đọc bộ đếm của app (lần mở app, lần mở đầu, `song_end`) và thư viện bài (user-library / Firestore). Không arm nào đặt sẵn `song_pool`: Song List giữ thứ tự catalog, trừ khi `mon_pkg.song_pool` nêu tên một pool trong `song_pools`.
- **Infra** (`src/infra`): adapter bọc ads layer, RevenueCat, Remote Config; [`monetization-app-container.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/infra/container/monetization-app-container.ts).
- **Presentation**: [`useMonetizationStore`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/monetization/store.ts) cho màn hình mới (paywall No Ads / Shop).

Không có arm (`{}`, thiếu, tên lạ) = chạy luật của `friendly` (Baseline của design final 10/11); `mon_pkg_variant = default` để analytics vẫn phân biệt được.

### Hành vi riêng của từng arm

Arm không chỉ là dữ liệu: mỗi arm có thể khai báo `behavior` ([`MonArm.behavior`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/domain/policies/mon-arms/mon-arm.ts)), tức bộ quyết định của riêng nó theo các contract nhỏ trong [`mon-behaviors/contracts.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/domain/policies/mon-behaviors/contracts.ts): `IFullscreenLogic`, `IFsnLogic`, `IAppOpenLogic`, `INoAdsPopupLogic`, `IBannerLogic`, `IArmRules`. `AdPolicy` chỉ chuyển câu hỏi cho logic của arm đang chạy (`MonArmRegistry.behaviorFor(config)`), use case không biết đang là arm nào. Config chỉ là thứ logic _có thể_ đọc.

- Mặc định mọi quyết định đọc từ config (`ConfigFullscreenLogic`, `configBehavior` trong `mon-behaviors/`), nên một arm không khai báo `behavior` chạy y như trước.
- Mỗi arm hiện có lớp interstitial riêng (`FriendlyFullscreenLogic`, `BalanceFullscreenLogic`, `AggressiveFullscreenLogic`, `VeryAggressiveFullscreenLogic`, `DefaultFullscreenLogic`) kế thừa lớp config. Sửa lớp của arm nào chỉ đổi arm đó; muốn thay quyết định khác thì thêm vào `behavior`: `{ ...configBehavior(config), fsn: new MyFsn(config) }`.
- Logic không giữ state: thứ nó cần nhớ nằm ở repository và đến logic qua `StatsSnapshot` (cần cổng riêng thì truyền qua constructor của logic, domain không import MMKV).
- [`arm-behaviors.test.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/domain/__tests__/arm-behaviors.test.ts) chạy một bộ test hợp đồng trên mọi arm (không ad cho VIP/No Ads, không hai interstitial liên tiếp, luôn có lý do khi không hiện, cùng đầu vào cùng kết quả): arm mới tự vào bộ này.

### Khởi động

- `startMonetization()`: lúc mở app (trước khi fetch Remote Config), dựng slice trên giá trị Remote Config đang active và entitlement của app, rồi giao cho slice các quyết định đã nối (`installDecisions`).
- `bootMonetization()`: khi Remote Config của phiên đã biết (`publishRemoteConfigOutcome`): lấy arm của phiên, gắn user property `mon_pkg_variant` / `mon_pkg_rev`, theo dõi thay đổi.

### Đã nối vào app

| Quyết định                                                                                                                                                                         | Hook trong feature                                                                                                                                                                                                                                                                 | Slice trả lời                                                                                                                                                                                                                                                                                                      |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Video mở 1 mode hay cả 2 (`rv.per_mode`)                                                                                                                                           | `setRewardedPerModeSource` trong [`rewarded-unlock-mode.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/songs/rewarded-unlock-mode.ts)                                                                                  | `facade.rvPerMode()`                                                                                                                                                                                                                                                                                               |
| Bài cần 2 video RV (`rv.two_video_songs`, chỉ var 2 aggressive / var 3 very_aggressive đọc; mặc định `[]`)                                                                         | `countVideo` trong [`rewarded-song-unlock.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/songs/rewarded-song-unlock.ts) → `useMonetizationStore.actions.countSongVideo`; counter `rvVideoProgress` / `selectRvCounter` | `facade.countSongVideo()` / `facade.songVideoProgress()`; tiến độ lưu MMKV `monetization.rv2.<id>.<scope>` (giữ qua session), trước khi boot mọi bài cần 1 video                                                                                                                                                   |
| Số bài miễn phí trong 3 bài tutorial (`tutorial.free_songs`, cố định theo arm, console không chỉnh: friendly 3 = mở cả 3, các arm khác 2 = bài đã chơi + 1 bài Claim ở màn reward) | `setTutorialFreeSongCountSource` trong [`tutorial-catalog-source.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/tutorial/tutorial-catalog-source.ts)                                                                   | `facade.tutorialSongCount()`, chỉ sau khi boot (trước đó 3)                                                                                                                                                                                                                                                        |
| FSN sau FS / RV (`fsn`, `fsn_from`)                                                                                                                                                | `setFsnSlotSource` trong [`fsn-slot-source.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/ads/fsn/fsn-slot-source.ts)                                                                                                      | `facade.isFsnOpen("fs" \| "rv")`, chỉ sau khi boot (`isReady()`)                                                                                                                                                                                                                                                   |
| Flow A / B (`fs.flow`)                                                                                                                                                             | `setAdFlowSource` trong [`ad-flow.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/sdk/ad-flow.ts)                                                                                                                           | `facade.adFlow()`, chỉ sau khi boot                                                                                                                                                                                                                                                                                |
| App Open (`app_open.*`)                                                                                                                                                            | `setAppOpenThresholdsSource` trong [`app-open.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/sdk/app-open.ts)                                                                                                              | `facade.appOpenThresholds()`, chỉ sau khi boot                                                                                                                                                                                                                                                                     |
| Interstitial ở mọi hành động (`fs.*`)                                                                                                                                              | `setFullscreenAdDecider` trong [`ad-actions.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/sdk/ad-actions.ts); màn hình gọi `reportAdAction`                                                                               | [`createSliceFullscreenDecider`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/infra/adapters/slice-fullscreen-decider.ts) → `onHomeSongClick`, `onSongEnd`, `onLearnSongEnd`, `onResultAction`, `onResultHome`, `onPlayTime`, `onFreeModeExit`, `onLearnStageDone` |
| RV của app tính vào khoảng cách RV → FS                                                                                                                                            | `setRewardedShownListener` (gọi trong `markRewardedShown`)                                                                                                                                                                                                                         | `facade.noteRewardedEarned()`                                                                                                                                                                                                                                                                                      |

**Field `mon_pkg` cho interstitial** (PO bật/tắt theo variant trên Firebase hoặc DevTool):

| Field                     | Ý nghĩa                                                                                                                                | Mặc định                                                                                                    |
| ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| `fs.cooldown_s`           | khoảng cách tối thiểu giữa 2 interstitial                                                                                              | Friendly / Balance / không arm 30, Aggressive / Very Aggressive 15                                          |
| `fs.rv_to_fs_s`           | khoảng cách từ video thưởng tới interstitial                                                                                           | 30                                                                                                          |
| `fs.first_after_song_end` | interstitial đầu tiên sau N bài chơi xong (trọn đời, **bài tutorial tính là bài thứ 1**)                                               | Friendly / Balance / không arm 3 (= bài thứ 2 sau tutorial); Aggressive / Very Aggressive 2                 |
| `fs.flow`                 | `user_action` (Flow A) / `song_end` (Flow B); `mon_pkg` đặt thì thắng                                                                  | mọi arm và không arm `song_end` (bất kể `fs_logic`); Flow A chỉ còn khi console đặt `fs.flow = user_action` |
| `fs.skip_after_fs`        | không show FS ở 2 hành động liên tiếp. **Không áp dụng cho `song_end`** (Game): FS hết bài chỉ theo cooldown / RV → FS (PM 2026-11-11) | `true` mọi arm                                                                                              |
| `fs.home_song_click`      | bấm bài ở Home cũng là thời điểm FS trong Flow B                                                                                       | Aggressive / Very Aggressive `true`                                                                         |
| `fs.skip_first_home_tap`  | không show FS ở lần bấm bài đầu tiên của phiên                                                                                         | Aggressive / Very Aggressive `true`                                                                         |
| `learn_fs.mode`           | `song_end` (FS lúc hết bài Learn) / `stages` (FS giữa các stage, không có FS lúc hết bài Learn) / `off` (không có FS nào trong Learn)  | Aggressive / Very Aggressive `stages`; Friendly / Balance / không arm `off`                                 |
| `learn_fs.first_stage`    | stage đầu tiên có FS (chế độ `stages`)                                                                                                 | 2                                                                                                           |
| `learn_fs.interval`       | sau đó cứ mỗi N stage một FS, không bao giờ ở stage cuối                                                                               | 2                                                                                                           |

**Bài bỏ dở** (cố định trong code, không có field `mon_pkg`; chỉ arm Flow B có `fs.home_song_click`, tức Aggressive / Very Aggressive; không áp dụng Learn), PM 2026-11-11:

- Bấm bài ở Home **có FS** → đánh dấu bài đó (`tapFsUnfinished`, chỉ trong phiên).
- Hết bài (`song_end` / `learn_song_end`) → xoá dấu. Pause → Back / Replay **giữ** dấu.
- Lần bấm bài ở Home kế tiếp còn dấu → không có FS (`reason = abandoned_song`), rồi xoá dấu: chỉ miễn **1** lần bấm.
- Mục đích: user xem FS lúc bấm bài, không thích / bấm nhầm, thoát giữa chừng, chọn bài khác thì không bị FS lần nữa. Trước đây `fs.skip_after_fs` vừa chặn nhầm FS hết bài ngay sau FS lúc bấm bài, vừa không che được ca này (`play_session_end` lúc thoát xoá cờ).

Các quyết định khác (banner, paywall No Ads / Shop) vẫn do code hiện tại của từng feature.

### Cách nối một quyết định vào slice

Theo đúng mẫu `rv.per_mode`:

1. **Slice đã có câu trả lời** trên facade:
   - banner: `banner()`;
   - paywall No Ads / Shop: `useMonetizationStore`.
2. **Trong feature**, thêm một hook `setXSource(source | null)`. Feature hỏi source trước; source trả `null` (slice chưa sẵn sàng) hoặc chưa cài thì feature giữ câu trả lời của chính nó.
3. **Cài hook trong `installDecisions`** ([`monetization-app-container.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/infra/container/monetization-app-container.ts)). Feature không import container, nên không có vòng import.
4. **Thêm test:**
   - unit test cho hook trong feature;
   - test hợp đồng nguồn như [`rewarded-per-mode-slice-wiring.test.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/songs/__tests__/rewarded-per-mode-slice-wiring.test.ts) hoặc [`fsn-slice-wiring.test.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/ads/__tests__/fsn-slice-wiring.test.ts) (container, bootstrap và composition root không chạy được dưới `bun:test`).

### Arm là dữ liệu — thêm hoặc sửa một arm

Mỗi arm là **một file** trong `policies/mon-arms/` chỉ khai báo `overrides` (phần nó đổi so với baseline `baseMonConfig`), không có code chạy. Thứ tự áp: baseline → `overrides` của arm → field có mặt trong `mon_pkg` (data layer). Hàm thuần ở [`mon-config.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/domain/policies/mon-config.ts): `applyOverrides`, `extendOverrides` (arm = arm khác + thay đổi, như `very_aggressive`), `diffConfigs`, `deepEqual`.

Thêm arm mới:

1. Thêm id vào `MON_ARM_IDS` ([`entities/monetization.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/domain/entities/monetization.ts)).
2. Thêm file `mon-arms/<ten>.ts` (`id`, `label`, `aliases`, `summary`, `overrides`).
3. Thêm vào `MON_ARM_TABLE` ([`mon-arm-registry.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/domain/policies/mon-arm-registry.ts)). Bảng khai báo `satisfies Record<MonArmId, MonArm>`: thiếu arm cho một id, hoặc arm nằm dưới id lạ, **không compile**. `MonArmRegistry` ném lỗi khi hai arm trùng tên / alias hoặc một arm lấy id `default`.
4. Cập nhật `mon-arms.test.ts` (giá trị mặc định của arm). `registry.diff(a, b, flow)` cho biết hai arm khác nhau đúng những field nào (dùng cho QA / DevTool).

Một chiều biến thể **mới** (field mới trong `MonConfig`) cần thêm: field trong `MonConfig`, một dòng trong `FIELDS` của `mon-pkg-mapper.ts`, nhánh trong `AdPolicy` nếu nó đổi quyết định. Khi một arm cần _hành vi_ không biểu diễn được bằng con số, đó là lúc đổi phần đó sang strategy riêng của arm — chưa cần tới.

`createMonetizationModule({ arms })` nhận danh sách arm (mặc định `MON_ARMS`), nên test có thể chạy cả slice trên một arm tự dựng.

### Facade chỉ trả lời, không lộ config

`MonetizationFacade` không có `config()`. Ngoài slice chỉ hỏi bằng câu hỏi có ý nghĩa: `arm()`, `adFlow()`, `appOpenThresholds()`, `rvPerMode()`, `isFsnOpen()`, `canShowInterstitial()`… (cấu trúc `MonConfig` đổi thì app không biết). Mỗi bên chỉ thấy phần mình cần: `MonetizationScreenApi` (store màn hình), `MonetizationDecisionsApi` (ads layer).

**Trước khi boot, mọi câu trả lời là "chưa" và không bao giờ ném lỗi:** `null` (arm, flow, thresholds, `paywall`, `banner`), `[]` (`songList`), `reason: "not_ready"` (interstitial, App Open), `gate: "not_ready"` (`openSong`), `outcome: "not_ready"` (`watchRewarded`). Caller giữ mặc định của mình.

`refreshVariant` / `watchVariant` so cả snapshot (config + `song_pools` + catalog + ghi chú), không chỉ config: kết quả `refreshed` nghĩa là cùng config nhưng pool / catalog đổi (Song List được dựng lại, không log `variant_switched`). Container nối `watch` vào cả Remote Config store lẫn catalog store.

State chạy của slice (`ActiveConfig`, `EntitlementState`, `SwitchSettings`, `PlayerStats`) nằm ở `src/domain/state/`, không lẫn với use case hay policy.

Quy ước tên: **arm** là khái niệm trong code (`MonArm`, `MonArmRegistry`); **variant** là tên trên dây và trong analytics (`mon_pkg.variant`, user property `mon_pkg_variant`, event `variant_switched`, `IVariantProvider`, `VariantSwitcher`). Không đổi tên phía dây vì dashboard analytics đã dựa vào chúng.

## Implementation notes

- Ad show qua **`getAdsService()`** → `showInterstitial(placement)` / `showRewarded(placement)` — `ads.ts` `createFrequencyCap` lo `Ad Interval`. SDK = **Google AdMob** (`react-native-google-mobile-ads`); init is lazy (first `start()`), not at app entry — see [admob-integration.md](/docs/admob-integration.md).
- **Tier High → All** (mọi format): mỗi tier giữ 1 **preload pool** của SDK (xem [admob-integration.md §3](/docs/admob-integration.md#3-ad-units--tiers-high--all)); `preload()` dựng pool High rồi pool All sau 200–500 ms, sau đó không làm gì thêm. Lúc hiện, engine **lấy** (`take`) ad từ pool High nếu sẵn sàng, không thì All, không thì `not-ready` ngay. Engine không còn retry ladder / load timeout / eviction — SDK tự lo; pool lỗi được dựng lại tối đa 1 lần/phút. `*_request` = pool được tạo hoặc nạp lại sau khi lấy ad, `*_request_success` = pool báo có ad, `*_request_failed` = pool lỗi.
- **Preload/refill (interstitial + rewarded):** bài ads kế được **request ngay khi ad hiện** (`onAdDisplayed` hoặc ILRD `ad_impression` confirm) — `videoads_request` / `fullads_request` fire **đồng bộ** với `*_show`; prefetch load song song; `fullads_request_success` khi prefetch interstitial load xong; `onAdClosed` **adopt** prefetch vào slot live (fallback tracked request on close nếu display miss prefetch); ad đã adopt dùng chung show events của slot (`showEvents`), nên display / reward / close của nó trả kết quả cho lần show kế — trước đây listener của prefetch là no-op, lần show thứ hai chỉ được watchdog 8s trả `show-failed` giữa video. **Skia video backfill** (`videoads_backfill`) chỉ khi rewarded thật sự không show — `runAdsBackfill` **không** stack lên native fullscreen (`isFullScreenAdVisible` guard). Nếu callback hiển thị (`OPENED`) đến trễ / không đến mà paid event của ad đã về → engine confirm hiển thị từ impression đó để watchdog 8s không false-fail. Nếu `CLOSED` cũng không bao giờ đến, chạm đầu tiên vào màn hình app (sau 1s kể từ lúc ad hiện) sẽ đóng show như `CLOSED` — ad đang hiện thì giữ mọi cú chạm, nên chạm tới được app nghĩa là ad đã tắt (xem [admob-integration.md §6](/docs/admob-integration.md#6-engine-protections)). Result / Learn / quit CTAs → gameplay **chờ `waitForFullscreenAdDismissed()`** sau interstitial; **về Home cũng vậy** (Song list ở Result Game/Learn, cả 2 flow): chỉ `router.back()` khi ad đã đóng hẳn — pop trong lúc view của ad còn đang gỡ có thể bị bỏ, user phải bấm Home lần nữa. Pause → Song list về Home **ngay** sau `gatePauseAd()` (không qua `requestIdleCallback`). Learn: Leave / back giữa chặng và Result → Song list đi qua `runLearnExit` ([`learn-exit.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/learn/logic/learn-exit.ts), DEV 2026-12-02): dừng nhạc trước, bước quảng cáo có **3 s** để đưa ad lên màn hình (không có ad thì thoát luôn; ad đang hiện thì chờ người chơi đóng), lỗi ở bất kỳ bước nào vẫn thoát; Leave giữa chặng **không** chờ popup No Ads, Result → Song list vẫn chờ (giới hạn 3 s). `SongsScreen` `select_song` navigates as soon as `runInterstitial` resolves (no wait). `waitForFullscreenAdDismissed()` chỉ chờ ad đóng (raw flag) + settle **300ms** (`FULLSCREEN_AD_NAV_SETTLE_MS`), **không** chờ hết latch 1.5s — latch chỉ dành cho memory layer / preload, trước đây mỗi Replay / Next / Home sau ad bị đứng \~1.5s. (Tests: `ads-engine.test.ts`, `ad-lifecycle.test.ts`.)
- **Game Mode — chỉ request ads ở màn loading của bài** (02/10, [`usePlayPrepareAdWait`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/piano/screens/PlayScreen/usePlayPrepareAdWait.ts)):
  - **Màn loading đầu bài mở "cửa sổ loading"** (`setAdLoadingWindowOpen`). Khi cửa sổ mở, các lần nạp không bị đậu, hàng chờ được chạy, và interstitial lẫn rewarded được request.
  - **Đếm ngược 3-2-1 chờ interstitial,** tối đa **5s** tính từ lúc mở màn loading (`SONG_START_AD_WAIT_CAP_MS`), và không bao giờ chờ rewarded. Đi tiếp ngay khi interstitial đã sẵn sàng (High hoặc All) hoặc khi bài này không cần: user không thấy ads, hoặc bài này chưa đủ tới `song_end` thứ 2 trọn đời.
  - **Từ lúc đếm ngược tới khi rời bài thì không request mới.** Retry, nạp lại và refill đều xếp hàng, rời bài mới chạy. Lần nạp đang chạy dở thì cứ hoàn tất.
  - **Luật đậu là một hàm thuần duy nhất:** `shouldParkAdLoad` ([`ad-lifecycle.pure.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/sdk/ad-lifecycle.pure.ts)).
  - **Đổi màn chạy lại hàng chờ** (`resumeParkedAdLoads`, gọi trong [`screen-view-tracking.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/core/navigation/screen-view-tracking.ts) ngay sau khi vị trí ad đổi). Lần nạp bị đậu khi vị trí ad còn là gameplay sẽ chạy ngay khi vị trí đổi, không phải chờ lần refill kế.
  - **Bài tutorial cũng đậu như vậy** (17/11, [`tutorial-ad-park.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/tutorial/tutorial-ad-park.ts), giữ trong `useTutorialSongSession`): mở cửa sổ loading khi bài mount, đóng khi bài sẵn sàng đếm ngược, nhả cờ khi rời bài. Khác Game Mode: không tự request ads (người chưa trả lời ATT sẽ bị khởi động SDK trước prompt) và không chờ interstitial.
  - **Giữ nguyên:** Learn, practice của tutorial, Free mode và banner (kể cả ở menu tạm dừng).
- IAP song prices: one **`useIapSongOfferingsQuery()`** on `SongsScreen`; rows look up packages by product id (no per-row RC fetch).

***

## Subscription paywall theo arm (`mon_pkg.paywall.*`)

Arm quyết định qua `MonetizationFacade.subscriptionPaywall()` → `features/paywall/paywall-rules-source.ts`. Giá và product id **không** viết trong app: mọi paywall đọc package (`$rc_weekly` / `$rc_annual`) từ offering RC (`default`, `notrial`, `discount`), giá lấy từ store.

| Key RC (`mon_pkg.paywall.*`)                                                                                              | Friendly (Baseline)          | Balance       | Aggressive A / B                                                       |
| ------------------------------------------------------------------------------------------------------------------------- | ---------------------------- | ------------- | ---------------------------------------------------------------------- |
| `trial_intro` (paywall sau intro = sau onboarding hoặc tutorial)                                                          | true                         | true          | false                                                                  |
| `trial_user_opened` (bấm bài VIP, Shop, See All Plans, Settings…)                                                         | true                         | true          | false                                                                  |
| `launch_paywall` / `launch_from_session`                                                                                  | true / 2                     | true / 2      | false (popup No Ads thay vào: `noads_popup.app_open_from_session` = 2) |
| `discount_50` / `discount_50_from_session` / `discount_50_after_dismissals`                                               | true / 3 / 15                | true / 3 / 15 | false                                                                  |
| `vip_offer` / `vip_offer_after_taps` / `vip_offer_per_session` (offer 50% khi bấm bài VIP, đếm reset mỗi session)         | true / 2 / 1                 | true / 2 / 1  | false                                                                  |
| `default_paywall` / `default_paywall_song_start` (đếm `song_start` cộng dồn, tutorial + Learn tính; hiện ở result, 1 lần) | true / 6                     | true / 6      | false (popup No Ads thay vào: `noads_popup.song_start` = 6)            |
| `home_return` / `home_return_songs`                                                                                       | true / 3                     | true / 3      | false                                                                  |
| `replay` (ngưỡng vẫn `replay_paywall_threshold`)                                                                          | true                         | true          | false                                                                  |
| `first_song` (paywall ở màn kết quả Game Mode đầu tiên sau tutorial, 1 lần; là paywall "intro")                           | true                         | true          | true, không trial (PO 20/11)                                           |
| `offering_trial` / `offering_no_trial` / `offering_discount` (RC offering ids; each holds `$rc_weekly` + `$rc_annual`)    | default / notrial / discount | same          | same                                                                   |
| `skip_app_open_on_paywall` (launch nợ paywall thì không App Open; mọi arm đều tắt, App Open và paywall độc lập)           | false                        | false         | false                                                                  |

`paywall.trial` đặt cả hai cờ trial. Không-trial = offering `notrial` (VIP weekly/yearly no trial); màn hình chỉ hứa trial khi StoreKit báo gói có `freeTrial`. Location (`screen`) của offer sau bấm bài VIP: `vip_offer`. Không hai paywall chồng nhau: `openDiscountPaywall` và các trigger tự động bỏ qua khi một paywall đang hiện. Trước khi slice boot: mọi thứ như cũ.

## Ngoài scope feature này — Subscription Paywall triggers (feature 4)

_(PO diagram cũng gộp paywall; để dành feature 4)_ — click Sub icon / VIP song / "Unlock all songs"; popup theo session/onboarding/replay-count/song-count/after-N-ads; No-Ads / Discount paywall variants.

***

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