Skip to content

fix(android): diagnose emulator starts distinctly and prove a real Flutter APK - #101

Merged
ElbertePlinio merged 7 commits into
mainfrom
fix/android-release-proof
Sep 5, 2026
Merged

ElbertePlinio merged 7 commits into
mainfrom
fix/android-release-proof

Conversation

@ElbertePlinio

@ElbertePlinio ElbertePlinio commented Sep 5, 2026 •

Copy link
Copy Markdown
Member

Summary

Release-grade Android proof for #93: root-causes the "intermittent" live-test start failure, makes emulator start failures distinct and durable, verifies app launches, and proves a real Flutter APK end to end through the Pickforge Android lab with independent visual review.

Full report and raw evidence: ~/Projects/Boards/pickforge/0.4-release/android/report.md. Round 3 (2c341c8, fully isolated, disposable AVD clone; the current head b2c8066 merges current origin/main through #100; production Android, CLI Android, and core lifecycle sources are byte-identical to the round-3 device-pass head, so the pass was not redone): raw-isolated/, raw-isolated-concurrency/, real-trees-diff.txt, investigation-first-launch/, vision-verdict-round3.md, run-isolated-pass.sh. Rounds 1-2 are preserved under raw/, raw-concurrency/, raw-attempt*/, live-test/.

Root cause of the baseline failure

avdmanager honours XDG_CONFIG_HOME and wrote the test's freshly created pickforge-avd to ~/.config/.android/avd; the emulator resolves $ANDROID_AVD_HOME, then $ANDROID_USER_HOME/avd, $ANDROID_EMULATOR_HOME/avd, $ANDROID_PREFS_ROOT/.android/avd, $ANDROID_SDK_HOME/.android/avd and $HOME/.android/avd, so it exited in 2 s with Unknown AVD name, and the test's afterAll deleted the log named by the error. Reproduced deterministically in an isolated HOME. Not intermittent: it depends on whether the AVD had to be created on an XDG host. (The stray ~/.config/.android/avd/pickforge-avd.* from the 2026-09-04 baseline is still on the machine; not deleted by this work.)

Changes

  • One AVD directory resolved by Pickforge in the emulator's own order (ANDROID_AVD_HOME → ANDROID_USER_HOME/avd → ANDROID_EMULATOR_HOME/avd → ANDROID_PREFS_ROOT/.android/avd → ANDROID_SDK_HOME/.android/avd → ~/.android/avd) and passed as ANDROID_AVD_HOME to avdmanager, emulator -list-avds and the emulator; createAvd verifies the ini landed there. The avd-missing hint and the doc comment name the same directories the code resolves.
  • Pre-flight before spawning: AVD present (lists available AVDs); AVD lock held by a live emulator → avd-in-use; explicit port bound on loopback → port-collision.
  • AVD sharing is a Pickforge policy, stated as such (AVD_SHARING_POLICY): Pickforge shares an AVD only among read-only sessions and refuses to start any session, writable or read-only, while a writable emulator holds the AVD's lock, because a writable instance keeps rewriting the disk overlays and quickboot snapshot a read-only one maps. What the emulator itself admits is recorded as evidence (raw-isolated/41-*, 42-*) and not relied on.
  • Auto port allocation adds a loopback bind probe on the console and adb ports (covers other Pickforge homes and foreign processes); an emulator that still reports a collision is retried on a fresh pair, at most twice, each attempt reported as progress.
  • EmulatorStartError with kinds avd-missing | avd-in-use | port-collision | snapshot | kvm | process-exit | boot-timeout | aborted, the log path, the redacted last 25 log lines, and the adb device state on timeout (missing/offline/unauthorized/device) with a hint each. A sys.boot_completed probe that cannot run at all (adb missing or not executable mid-boot) no longer escapes as a bare spawn error: the wait ends in the typed boot-timeout diagnosis with the probe's own error. The failed session record keeps the diagnosis in meta.androidStartFailure.
  • --cold-boot / --read-only on session create and android start, coldBoot/readOnly on the MCP tools; sessions report bootMode and readOnly. Graceful stop waits up to 30 s after adb emu kill before signalling.
  • launchApp resolves the launcher activity (cmd package resolve-activity), starts it with am start -W, and confirms the process with pidof, returning component, pid and launch state. The real pass showed the old monkey launch returning success while the launcher stayed on screen.
  • Live integration test: uses an existing AVD (PICKFORGE_LIVE_ANDROID_AVD) or creates a throwaway one under its temp root, optional real APK (PICKFORGE_LIVE_ANDROID_APK/_PACKAGE), preserves its temp root on failure, copies logs and records to PICKFORGE_LIVE_ANDROID_ARTIFACTS, adds a cold-boot read-only + concurrent read-only second-home case, and gates each launch attempt on lowmemorykiller having been quiet for 30 s (bounded, every probe logged) instead of on MemAvailable/load, which the investigation showed do not predict the first-launch kill.
  • MCP registerSessionTools split per tool (complexity gate on the touched function).
  • Unit tests: AVD home precedence and the avdmanager/emulator mismatch, every failure kind, port probe/skip/retry, lock pre-flight, boot mode, launch verification, CLI/MCP flag plumbing; plus, for the review residuals, the hint directories against avdHomeDir, the policy wording, and the probe-cannot-run path (old-head-replay-bc3302b.txt: exactly these 5 fail on the reviewed head, 27 pass).

Evidence, round 3 (this head; real SDK, KVM, Flutter 3.41.6 release APK)

Isolation: HOME, XDG_CONFIG_HOME, ANDROID_USER_HOME, ANDROID_EMULATOR_HOME, PICKFORGE_HOME (three homes), PICKLAB_HOME, the adb server (ANDROID_ADB_SERVER_PORT=5137 with its own copy of the adb key; the stray default-port server was stopped first) and ANDROID_AVD_HOME, which points at a btrfs reflink clone of picklab-avd under /var/tmp, all set by the harness (run-isolated-pass.sh), not by the product. Nothing under the real ~/.android or ~/.config was opened for writing; snapshot-real-trees.sh hashed every file under ~/.android (20 GB, all AVDs and quickboot snapshots) and ~/.config/.android before and after: paths and sha256 identical for ~/.android (152 files) and ~/.config/.android, sentinels identical, and the only ~/.config changes are Codex/Slack/zen browser state written by those applications (real-trees-diff.txt).

  • Session A (writable, warm boot from the clone in 10 s): install; verified launch on the first attempt (pid, COLD); UI tree content-desc 0 → 2 after two taps with screenshots; home + relaunch keeps 2 (launchState: HOT, same pid); 26 s screen recording; logcat; pickforge evidence record → outcome passed with the planted secret redacted; run artifact recorded; destroy leaves no process, adb device, record or port lock.
  • AVD-sharing policy evidence while A ran: a Pickforge --read-only start from another home refused before spawning with avd-in-use and the policy text, its failed session record keeping the diagnosis; a raw emulator -read-only next to A refused by the emulator itself (Another emulator instance is running…, exit 1). A untouched afterwards.
  • Two read-only sessions from two isolated homes/projects concurrently on ports 5556/5558 (both device in one adb devices), the same APK installed and launched in both, tapped to 1 and 3, cross-home targeting fails closed, B destroyed, C tapped to 4, both gone, no lock left by the read-only instances.
  • Independent vision review round 3 (claude -p --model claude-opus-5, Read only, different model from the driver): PASS, A: 0, 1, 2, 2, 2, B: 0, 1, C: 0, 3, 4; its remaining notes are what pixels cannot show (byte-identical kept-state frames, disclosed; serial/port identity and B's teardown, which the adb listings and session records carry).
  • First-launch kill investigated on six fresh clones (investigation-first-launch/): a fresh sideload on the 2 GB Play image right after a quickboot restore can trip lowmemorykiller into a storm that kills every oom_score_adj 0 process including the app just launched, while MemAvailable stays around 1 GB; a reinstall or a read-only boot did not trip it; the storm is sometimes transient (a launch after 30 s of lmkd quiet survives) and sometimes an eight-minute thrash no bounded wait survives. The product keeps failing loudly; the harness and the live test wait for lmkd quiet; the product-side fix (opt-in bounded readiness, AVD provisioning with more RAM) is filed as Android: first launch of a fresh sideload after a quickboot restore is killed by lowmemorykiller on the 2 GB Play-image AVD #105.

Rounds 1-2 (previous head bc3302b, run against the real picklab-avd with the real AVD home): warm boot, install, verified launch, counter 0 → 1 → 2, home + relaunch keeps 2, video, logcat, evidence record with the planted secret redacted, clean destroy; two read-only sessions from two homes tapped to 1 and 3 / 4; vision round 1 FAIL on mid-restore frames, round 2 PASS. Those runs rewrote the real AVD's snapshot.pb, which is why round 3 was redone on a clone.

Findings for follow-up (not fixed here)

Gates

bun run typecheck, bun run lint, complexity-gate check --changed, bun run test, bun run test:coverage (thresholds unchanged), bun run build, cargo fmt --check, cargo clippy --workspace --all-targets --locked -- -D warnings, cargo test --workspace --locked, cargo build --release -p pickforge-cli. Local results at 2c341c8 (the round-3 head; f82e45c adds release-notes text only): typecheck and lint pass; complexity-gate check --changed reports 0 violations; bun run test 106 files, 1353 passed, 4 skipped; bun run test:coverage all files statements 84.77 %, branches 86.92 %, functions 91.05 %, lines 84.77 % (thresholds 73/86/79/82 unchanged); bun run build ok; cargo fmt --check, cargo clippy -D warnings, cargo test --workspace --locked (all suites ok) and the release build ok. CI for f82e45c: run 33938643555, all jobs pass.

Head 88fb607 merges origin/main a2ecc44 (#103, Xvfb display allocation and teardown): no file in common with this branch, clean merge, and the Android package, the CLI/MCP Android paths and session-lifecycle.ts do not use the display helpers #103 changed, so production Android behaviour is unchanged and the device pass was not repeated. Local results at 88fb607: typecheck and lint pass; complexity-gate check --changed reports 0 violations; bun run test 107 files, 1358 passed, 4 skipped; bun run test:coverage all files statements 84.79 %, branches 86.79 %, functions 91.15 %, lines 84.79 % (thresholds unchanged); bun run build ok; cargo fmt --check, cargo clippy -D warnings, cargo test --workspace --locked (all suites ok) and the release build ok. CI for 88fb607: run 33940311985 on the exact head, all jobs pass.

Refs #93
Refs #89

Exact combined-head CI: https://github.com/pickforge/pickforge/actions/runs/33944473917 (attempt 1, all green). Candidate smokes: https://github.com/pickforge/pickforge/actions/runs/33944473928 (attempt 1, all green). Independent closure review: CLEAN.

…utter APK

The live Android integration test failed in two seconds with "exited before
finishing boot" and then deleted the emulator log it named. Root cause:
avdmanager honours XDG_CONFIG_HOME and wrote the freshly created AVD to
~/.config/.android/avd, while the emulator only searches $ANDROID_AVD_HOME,
$ANDROID_SDK_HOME/avd and $HOME/.android/avd. Pickforge now resolves one AVD
directory, passes it as ANDROID_AVD_HOME to avdmanager, -list-avds and the
emulator, verifies the ini landed there, and fails before spawning when the
AVD is not where the emulator will look.

Start failures carry one distinct kind (avd-missing, avd-in-use,
port-collision, snapshot, kvm, process-exit, boot-timeout with the adb device
state, aborted), the log path and its last lines, and the failed session
record keeps the same diagnosis under meta.androidStartFailure. Console ports
are probed with a loopback bind before launch on top of adb and the per-home
registry; a collision the emulator still reports is retried on a fresh pair.
New --cold-boot and --read-only options (CLI and MCP); sessions report
bootMode and readOnly; a writable instance is reported as avd-in-use for any
further start because the emulator shares an AVD only among read-only
instances. Graceful stop waits 30 s for the snapshot save.

android launch-app resolves the launcher activity, starts it with
am start -W and confirms the process, returning component, pid and launch
state; the real APK pass showed the old monkey launch reporting success while
the launcher stayed on screen.

The live test preserves diagnostics on failure, copies them to
PICKFORGE_LIVE_ANDROID_ARTIFACTS, boots PICKFORGE_LIVE_ANDROID_AVD or a
throwaway AVD under its temp root, optionally installs and launches a real
APK, and proves a cold-boot read-only session with a concurrent read-only
session from a second home.

Closes #93
Refs #89
…ts, harden the boot probe

Review residuals on #101:

- The refusal to start any session while a writable emulator holds the AVD
  is Pickforge's own fail-closed policy (AVD_SHARING_POLICY), stated as such
  in the pre-flight error, the log-classification hint, the release notes
  and the live test, instead of being presented as emulator behaviour.
- The avd-missing hint and the avdHomeDir doc comment named
  $ANDROID_SDK_HOME/avd while the code (and the emulator) resolve
  $ANDROID_SDK_HOME/.android/avd; both now list the real search order.
- A sys.boot_completed probe that cannot run (adb missing or not executable
  mid-boot) no longer escapes waitForBoot as a bare spawn error: the wait
  keeps polling and ends in the typed boot-timeout EmulatorStartError with
  the adb state, the log path, the redacted tail and the probe's own error.

Tests fail on the previous head for each of the three points.
…mory counters

The #93 first-launch deaths were reproduced on a disposable clone of the lab
AVD: a fresh sideload on the 2 GB Play image, right after a quickboot
restore, trips lowmemorykiller into a storm that kills every
oom_score_adj 0 process including the app just launched, while MemAvailable
stays around 1 GB and load and io pressure look the same on restores whose
first launch survives. The live test now waits (bounded, every probe
logged) until lowmemorykiller has been quiet for 30 s before each launch
attempt instead of reading MemAvailable and load1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant