# Mobile Simulator Debugging This guide is the fixed local loop for testing `apps/mobile` in an iOS Simulator or Windows Android Emulator. It exists because Expo Go, a development client, and an installed distribution build look similar during manual testing but prove different things. On Windows, the explicit Mainland China one-command entry starts or reuses the `cindy-api36` AVD, waits for Android to finish booting, configures `adb reverse`, and then keeps the current worktree's Metro in the foreground: ```bash pnpm mobile:sim:start:cn ``` `mobile:sim:start` keeps the repository-wide Global default. Use `-- --avd ` for another AVD or `-- --no-emulator` for Metro only. Android SDK tools are resolved from `ANDROID_SDK_ROOT`, `ANDROID_HOME`, or the standard Windows Android Studio SDK location; they do not need to be on `PATH`. The command does not rebuild the native app, so install the matching development package first when switching build identity. The three commands share Metro worktree, source, region and environment checks. `whoami` success proves identity consistency, not that the app has loaded a bundle or displayed a page (`pageVerified` remains false). `rebuild` reports installation separately from a launch request, and exits nonzero if launch is blocked, including when Metro is absent. `--build-only` does not require Metro. A successful launch request still requires fresh bundle logs and visible page verification. For the macOS external viewer, use `pnpm mobile:sim:open -- --udid `. It resolves the app inside the selected Xcode toolchain (including `DEVELOPER_DIR`), opens Device Hub or Simulator by its absolute path, and exits nonzero on failure. Do not use AppleScript app-name lookup or `open -a Simulator`: Xcode 27 has Device Hub instead, and probing a missing name can display an application chooser. The pinned Expo CLI patch shares discovery and opening with this entrypoint. Use `mobile:sim:whoami -- --viewer --json` when handing off an external window. `viewer.running` checks the selected app process; `viewer.windowVerified: false` explicitly leaves visible-window acceptance to a screenshot/manual check. Without `--viewer`, a missing external viewer does not fail embedded/headless identity checks. ## Current Source Verification Contract Before anyone claims "the simulator is already showing the new version", they must produce evidence. The #1 time sink in mobile debugging is staring at a **stale bundle from another worktree's Metro** and concluding "my change didn't apply". Treat the following as a contract, not optional steps: - **The native build number does NOT prove JS freshness.** `CFBundleVersion` / the version label only identify which native *development client* is installed. A pure JS/UI change (most edits) never bumps it. So a matching build number is necessary-but-not-sufficient. - **JS freshness = which Metro the app is connected to.** With multiple mobile worktrees open, several Metro servers run at once and the app silently connects to whichever it last used (often `8081`, which is frequently a *different* branch). "The app opened" ≠ "it loaded my bundle". - **Evidence required before trusting the simulator:** 1. The `__DEV__` build label at the top of the new-session screen shows `branch@commit+dirty-fingerprint · vX (build) · `, and the fingerprint is **your current worktree**. (Injected by `mobile:sim:start`; see below.) 2. `pnpm mobile:sim:whoami` exits nonzero unless a booted development client, the current worktree's `8081` Metro, and the exact source fingerprint all agree; the label's port must map to your worktree. 3. The Metro terminal printed a fresh `iOS Bundled …` after your edit. ### Tools (use these instead of ad-hoc `lsof`/`PlistBuddy`/deep-link dances) ```bash pnpm mobile:sim:start # start THIS worktree's Global dev-client Metro; injects git # branch/commit into the __DEV__ build label (EXPO_PUBLIC_*) pnpm mobile:sim:start:cn # Mainland China; on Windows also starts cindy-api36 pnpm mobile:sim:start -- --region=cn # 中国大陆版 JS region;先 rebuild 对应 native app pnpm mobile:sim:whoami # doctor: booted install + which port = which worktree pnpm mobile:sim:whoami -- --region=cn # inspect the cn native app + Metro ownership pnpm mobile:sim:rebuild # rebuild + reinstall the Global native dev app (native changes only) ``` `mobile:sim:start` and `mobile:sim:rebuild` default to `global`; the China Mainland build requires explicit `--region=cn`. Before touching Expo, all `mobile:sim:*` commands check dependencies without installing or deleting anything. If the check fails, preserve the environment and explicitly run `pnpm install --frozen-lockfile` in the current worktree after diagnosing the failure. The start/rebuild scripts also synchronize the selected build region and the matching `config/endpoint*.json` bootstrap base into `apps/mobile/.env`. Local Xcode / Simulator builds also read the selected block from the gitignored `apps/mobile/scripts/self-host-regions.json` for bundle identity, TapDB, and global Google client configuration. If this gitignored file is absent, the scripts automatically reuse and validate it from a registered personal-client or main worktree without printing its values; if no valid copy exists, the file is created automatically from the blank `self-host-regions.json.example` template with a warning (built-in app identity, analytics and Google sign-in disabled). You do not need to inject those public values or endpoint variables into `.env` by hand. For local builds only the bundle identity fields of the selected region (`iosBundleId` / `androidPackage`) are required. `tapdb` and `global.google` may be left empty: analytics then no-ops at runtime and Google sign-in is skipped, so external contributors can build without TapDB or Google OAuth accounts. The self-host release scripts still require these fields. The new-session build label reads the exact source fingerprint plus branch/commit from `EXPO_PUBLIC_XDT_GIT_*` (set by `mobile:sim:start`) and the Metro host from `Constants.expoConfig.hostUri`. The fingerprint is `branch@commit` for a clean worktree and adds a hash of tracked/known untracked changes for a dirty one, so an amend/rebase/reset or later edit cannot masquerade as the old Metro. It is `__DEV__`-only and compiled out of release builds. branch/commit/fingerprint are intentionally NOT injected via `app.config.js`/`extra`: that would change the `@expo/fingerprint` runtime version on every commit and break OTA. `EXPO_PUBLIC_*` lives in the JS bundle and does not affect the fingerprint. The sim scripts also live in the **root** `package.json` (`mobile:sim:*`), not `apps/mobile/package.json`, because the latter's `scripts` field IS a fingerprint input — adding a script there would bump the mobile runtime version. ### Multi-worktree Metro pitfall - The debug app has **no `expo-dev-client` dependency**, so it only ever loads its compiled default packager port (`8081`) — a `xdmaker://expo-development-client/?url=…` deep link or a `--dev-client` "No apps connected" state will NOT switch it. Starting Metro on any other port leaves the app loading a stale `8081` bundle. - Therefore `mobile:sim:start` **insists on `8081`** (never auto-bumps): if `8081` is held by *another* worktree it refuses and tells you to stop that Metro first (you can only run one mobile dev session at a time); if it's this worktree's own Metro it just says "already running". Use `mobile:sim:whoami` to see who owns each port. Override with `-- --port

` only if you'll point the app there yourself. - If `8081` belongs to this worktree but its injected fingerprint no longer equals the current worktree, `mobile:sim:start` refuses instead of claiming Metro is reusable. Stop it and start Metro again after the amend/rebase/reset or edit. ### Native build gotcha - Do **not** build with `CODE_SIGNING_ALLOWED=NO` for runtime verification: the resulting app lacks the keychain entitlement, so `expo-secure-store` fails and login/storage break. Use `mobile:sim:rebuild` (signed debug build) instead. ## Runtime Choice Use these runtimes for different jobs: - Current source debugging: iOS development client, bundle id resolved from the selected local region config (`com.xd.cindycn` for the current cn config, `com.xd.cindy` for global), attached to Metro. - An installed distribution build does not consume local Metro changes. - Expo Go: only for explicit Expo Go compatibility checks. It is not the normal regression target because this app depends on native config, secure storage, Feishu/Lark app handoff, audio, image picker, app scheme, and build-time iOS metadata. When testing a code change, state explicitly whether it is the development client or an installed distribution build. Do not just say "the app". ## Clean Simulator Loop From the mobile worktree: ```bash cd pnpm mobile:sim:start ``` Keep that Metro terminal open. In another terminal, install or run the native development client: ```bash pnpm --filter mobile ios -- --device "iPhone 17 Pro" ``` If the app is already installed and only JavaScript changed, reload instead of reinstalling: ```bash xcrun simctl terminate booted com.xd.cindycn || true xcrun simctl launch booted com.xd.cindycn ``` Then press `r` in the Metro terminal, or open the Expo dev menu in the simulator and choose Reload. Reinstall only when native state or native config changed: ```bash xcrun simctl uninstall booted com.xd.cindycn || true pnpm --filter mobile ios -- --device "iPhone 17 Pro" ``` Native rebuilds are required after changes to `app.json`, app schemes, iOS permissions, plugins, native modules, or `expo prebuild` output. > Note (Xcode 26.5+): `pnpm --filter mobile ios` (`expo run:ios`) can fail device > resolution with `xcodebuild: error: Unable to find a destination matching the > provided destination specifier: { id: }`. On this Xcode, `expo`'s > devicectl parsing breaks and xcodebuild only enumerates placeholder > destinations, so a concrete simulator UDID never matches. Use the rebuild > script instead — it builds against a generic simulator destination and installs > via `simctl`, sidestepping device resolution: > > ```bash > pnpm mobile:sim:rebuild # rebuild + reinstall onto the booted simulator > pnpm mobile:sim:rebuild -- --clean # uninstall first (clean login-state test) > ``` > > It passes `EXCLUDED_ARCHS=''` plus `ARCHS=` (arm64 on Apple > Silicon, x86_64 on Intel — derived from `process.arch`) to override the LarkSSO > pod's simulator arm64 exclusion and build a binary the host's simulator can run > (otherwise the app won't install / runs under Rosetta). Pure JS/TS changes never > need this — Metro Fast Refresh covers them. ## Confirm The Installed Build Before asking someone to retest, confirm which native app is installed: ```bash xcrun simctl list devices booted pnpm mobile:sim:whoami # Global(default) pnpm mobile:sim:whoami -- --region=cn # China Mainland ``` `mobile:sim:whoami` resolves the selected identity from `app.config.js` plus the gitignored `scripts/self-host-regions.json`, then prints the installed version and build number from that app's `Info.plist`. It is a doctor command: no booted device, no selected native app, no current-worktree `8081` Metro, or a source fingerprint mismatch makes it exit nonzero. Expected version values come from `apps/mobile/app.json`: - `ios.bundleIdentifier` - `version` - `ios.buildNumber` The native build number only proves native installation. `whoami` also compares the installed `EXUpdates.bundle/fingerprint` with the current worktree's Expo Updates fingerprint, using the same development environment and workflow as the native build. A missing/unreadable fingerprint (`native-unknown`) or difference (`native-mismatch`) fails the check even when Metro is correct. Run ordinary `mobile:sim:rebuild` without `--clean`; it rejects incompatible cached artifacts and checks the built and installed app before handing it off. For example, switching worktrees with different `@expo/ui` versions can otherwise silently break native gradient parameters while the app still opens. For JavaScript freshness, the Metro terminal must show a new bundle/reload after the source change. Matching native fingerprints does not prove page rendering. If a freshly built app exits before loading JavaScript with `UIScene life cycle is required`, check the SDK 57 scene backport: `expo >= 57.0.23` and `expo-build-properties` with `ios.enableSceneSupport: true`. The repository enables this official path for Xcode 27 / iOS 27. Prebuild should generate an `EXExpoAppSceneDelegate` manifest for the main app and make AppDelegate conform to `ExpoReactNativeFactoryProvider`; it must no longer create the legacy window. This native migration changes the runtime fingerprint and requires a new native build. Verify cold/warm links and background/foreground behavior as well as startup; do not mask it with a JavaScript workaround. ## Environment And Login `EXPO_PUBLIC_*` values are read by Metro at bundle time. After changing `apps/mobile/.env`, restart Metro and reload the app. `mobile:sim:start` / `mobile:sim:rebuild` create or repair the required functional values automatically from `eas.json`, without enabling dev-only flags. Online login no longer requires a Feishu App ID. Business endpoints come from the repo's `config/endpoint.json` manifest in dev; no API base env is needed. If the simulator starts already logged in, app data is still present. Use the app logout path for normal testing, or uninstall the app for a clean login-state test: ```bash xcrun simctl uninstall booted com.xd.cindycn pnpm --filter mobile ios -- --device "iPhone 17 Pro" ``` If Feishu login opens Safari and lands on `Cannot GET /api/auth/callback`, do not assume the backend auth exchange failed yet. First check: - The app is the development client, not Expo Go or an installed distribution build. - The installed build contains the selected region scheme (`cindycn` or `cindy`). - Metro was restarted after env changes. - Metro logs show whether `WebBrowser.openAuthSessionAsync` returned success, cancel, or dismiss. ## Logs Use Metro first for JavaScript warnings, React Navigation warnings, fetch errors, auth errors, and Expo runtime warnings: ```bash pnpm --filter mobile start -- --dev-client --host lan ``` Use the simulator system log for native crashes or native auth/session issues: ```bash xcrun simctl spawn booted log stream --style compact --predicate 'process == "Cindy"' ``` The in-app "Open debugger to view warnings" banner means a JavaScript warning is active. Read Metro before changing UI code. For device-link network symptoms, collect: - Metro log around the symptom. - The selected device id/name and connection state shown in the app. - Desktop app logs if the controlled computer is involved. - Server/device-link relay logs only when both mobile and desktop show relay symptoms. ## Render Storm Forensics And Regression Measurement Release 也可在设置 → 调试 / 开发者打开 Debug 日志,复现后导出。默认关闭;开启后记录 手机连接恢复、网络变化、错误与堆栈、滚动与键盘几何、渲染计数/解析耗时、JS 停摆。 不记录聊天正文、完整请求或凭证,不复制被控端任务日志,不捕捉任意 console 或原生日志。 详细日志最多保留 7 天、8 MiB;每 2 秒及前后台切换时落盘,突然杀进程可能丢失最后一批。 配置日志服务的构建还可手动上传筛选后的运行摘要(最近 7 天最多 500 条),成功自动复制编号。 上传摘要不包含详细 Debug 文件,需要详细现场时用导出或开发设备容器读取。 ### 从开发版 iPhone 读取历史 Debug 文件 手机与 Mac 配对、开启 Developer Mode 后,可通过 Xcode 设备管理窗口 (Devices and Simulators)选择对应开发版 App,使用 Download Container。 在下载的 `.xcappdata` 中查看 `AppData/Documents/cindy-debug/mobile-debug-*.ndjson`。 记录时不需要连着 Xcode;配对设备通过同网 Wi-Fi 可用时,也可以无线读取。 这不是任意互联网远程访问,实际权限取决于设备连接、开发签名与系统授权。 也可只拷出日志目录(bundle ID 必须取当前安装的开发版,不猜区域或包名): ```sh xcrun devicectl device copy from \ --device '' \ --domain-type appDataContainer \ --domain-identifier '' \ --source Documents/cindy-debug \ --destination /tmp/cindy-mobile-debug ``` 历史文件不会自动出现在 Xcode 实时 Console;该文件日志不是 Apple Unified Logging。 日志随 App 容器保存(可能进入系统备份),卸载会删除。尚未开启记录或尚未落盘时目录可能不存在。 Apple 参考:[配对设备](https://developer.apple.com/documentation/xcode/pairing-your-devices-with-your-mac)、 [下载容器](https://developer.apple.com/documentation/metal/creating-binary-archives-from-device-built-pipeline-state-objects)。 ### 进一步测量 背景:2026-07 会话白屏/卡死排查确立的取证与回归测量体系。 触碰 `remoteSessionStore` 订阅链、首页/详情页列表派生 (索引 useMemo、sections、行 memo)、或做相关重构时,改动前后各测一轮对比。 三层信号,从粗到细: 1. **`[js-stall]` 停摆探测器**(dev 常驻,`src/debug/jsStallWatchdog.ts`):JS 线程 停摆 ≥2s 即在 Metro 日志打带时长的 WARN(≥120s 判 suspend-suspect,是整机睡眠 假象)。日常开发的金丝雀——正常应为零或零星 2-3s(dev 调试开销);出现 ≥10s 或连发即回归。 2. **渲染 trace 采集 + 组件归因**(按需): ```bash # App 连着 Metro 时录制;期间复现目标场景(典型:桌面端流式输出 + 手机首页/会话来回) node apps/mobile/scripts/render-trace.mjs --seconds 120 node --max-old-space-size=8192 apps/mobile/scripts/render-trace-analyze.mjs /tmp/render-trace/trace.json ``` 判定基准(~2 分钟采样、桌面端流式场景实测):病态 = HomeSessionRow 数千次、 SectionList 全列表 pass 数十次 × 秒级;健康 = HomeSessionRow 两位数、壳层 pass 收敛到变化行。「Changed Props 供词」里的 *referentially unequal but deeply equal* 就是引用不稳定处,优先修。 3. **CI 不变量**(自动):`remoteSessionStore.test.ts` 的「引用调和」组(含 风暴不变量:消息/运行态高频 churn + 内容等价重算下 `getSessions()` 引用零漂移)、 `homeDesktopFirst.test.ts` 的保鲜契约断言(storeVersion 裸订阅 / 分钟心跳 / SessionRelativeTime)。重构必须保持这些绿灯——它们钉住的是「无关更新不惊动 列表」与「memo 化后该刷新的仍会刷新」两个方向的语义。 ## Keyboard, Rotation, And iPad Virtual keyboard: - Simulator menu: `I/O > Keyboard > Toggle Software Keyboard` - Shortcut: `Cmd+K` in most Xcode Simulator versions Rotation: - Simulator toolbar rotate buttons - Or `Device > Rotate Left` / `Device > Rotate Right` iPad: ```bash xcrun simctl list devices available | rg "iPad" pnpm --filter mobile ios -- --device "" ``` `apps/mobile/app.json` has `supportsTablet: true` and `orientation: default`, so phone portrait, phone landscape, and iPad should all be treated as layout regression targets. ## Common Failure Map `Project is incompatible with this version of Expo Go` : Wrong runtime. Use the development client for current source testing. `The action 'GO_BACK' was not handled` : The screen called stack back without a previous route. Top-level and modal-like screens need an explicit destination such as `/devices`. Local source change is not visible : Usually one of: wrong runtime, stale Metro bundle, an installed distribution build instead of development client, or native rebuild required. No controllable devices : Wait for the initial device-list read to finish. If it remains empty, verify same account, desktop online state, remote control enabled, and device-link WebSocket connectivity. Global "connecting" UI when only one device is slow : Treat connection state as per-device UI. The device chip should show its own connecting/offline/online state; the whole home screen should not block if other devices are usable. Keyboard covers the composer : Reproduce with the software keyboard enabled, then inspect `KeyboardAvoidingView` behavior, safe-area insets, and composer bottom spacing together. Do not validate keyboard layout with only the hardware keyboard. ## Before Asking For Manual Retest Run this checklist: - Confirm the exact runtime: development client, Expo Go, or an installed distribution build. - Confirm `CFBundleIdentifier`, `CFBundleShortVersionString`, and `CFBundleVersion`. - Restart Metro after env changes. - Reload the development client and watch Metro print a new bundle. - Reinstall the development client after native config changes. - Run `pnpm --filter mobile typecheck` when TypeScript changed. - Run the narrow relevant test when there is one. - Manually touch the exact path that regressed in the simulator. - Tell the tester which simulator/device, runtime, and build were used.