perf(flows): wait for the launched app to draw instead of sleeping 1.5s - #776
Draft
filip131311 wants to merge 1 commit into
Draft
perf(flows): wait for the launched app to draw instead of sleeping 1.5s#776filip131311 wants to merge 1 commit into
filip131311 wants to merge 1 commit into
Conversation
filip131311
force-pushed
the
filip/flow-launch-settle
branch
2 times, most recently
from
August 12, 2026 09:49
0cc4d15 to
cd29d9e
Compare
Every `launch:` step slept a flat POST_LAUNCH_SETTLE_MS (1500ms) before the run's first step, to give a cold start a head start. `restart-app` already waits for the activity to be displayed (`am start -W`), so on any app that is already drawing, that whole window was dead time. The condition the sleep approximated is observable: the flow's own tree source either shows the app's content or it does not. Poll it instead, with the same 1500ms as an upper bound — a warm app continues after one read, a genuinely slow start still gets the full window. The wait stays deliberately forgiving, so nothing that passed before can start failing: a tree that never fills (a canvas or video first screen), or a read that throws, spends the budget and carries on exactly as the sleep did. The tree-source gate still runs first and is still what reports an unusable source; this is a head start, not a gate. Measured on an Android emulator (Pixel 9 API 35), 6-step flow against a fast app, runs interleaved with the released build to cancel out host-load drift: baseline (sleep) 6.72s 6.79s 6.68s this change 5.27s 5.39s 5.07s ~1.45s saved per launch, i.e. the sleep minus the ~50-100ms the first tree read costs. Same on iOS by construction; the win is per `launch:` step, so a flow that relaunches several times saves a multiple of it.
filip131311
force-pushed
the
filip/flow-launch-settle
branch
from
August 12, 2026 10:27
cd29d9e to
0b545c3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Every
launch:step slept a flatPOST_LAUNCH_SETTLE_MS(1500ms) before the run's first step, to give a cold start a head start.restart-appalready waits for the activity to be displayed (am start -W), so on any app that is already drawing, that whole window was dead time.The condition the sleep approximated is observable — the flow's own tree source either shows the app's content or it doesn't. This polls it instead, keeping the same 1500ms as an upper bound: a warm app continues after one read, a genuinely slow start still gets the full window.
What "has drawn" means, and why it isn't just "the tree is non-empty"
Measured on a cold start (Pixel 9 emulator, API 35): a launching Android activity reports a tree of exactly one view for the whole settle window —
— the window's own content frame, with an identifier and nothing else. A "non-empty tree" check would call that blank screen ready. Once the app draws, that shell collapses into real content (the same screen reports 24–53 nodes carrying labels).
So the predicate is addressability, not size: one leaf carrying a
labelorvalueis enough. That deliberately includes the case of an app whose entire first screen is a single labelled view — it settles as fast as a dense one, where a plain node-count threshold would have held it for the full budget. The node-count arm (>= 3, i.e. the root plus two views) remains only as a fallback for a screen that draws real content with no labels at all — icons without a content description, a canvas.Why it can't regress a passing flow
The wait is forgiving in the same places the sleep was silent:
treeSourceGatestill runs first and is still what reports an unusable tree source. This is a head start, not a gate.The gate now runs before the head start rather than after it — reading the tree needs the source to be up, and the gate never depended on the sleep. The poll interval is deliberately unhurried (250ms): a hierarchy read is served by the inspected app's own UI thread, which during a cold start is the thing being waited on.
Measurements
Android emulator (Pixel 9, API 35), 6-step flow against a fast app. Runs interleaved with the released build, alternating arm by arm, because host load on the measuring machine drifts enough to swamp a sequential A/B:
~1.45s saved per launch — the sleep minus the ~50–100ms the first tree read costs. The saving is per
launch:step, so a flow that relaunches several times saves a multiple of it. iOS gets the same by construction.Discovered while profiling why an Android flow run costs ~2.5s more than the equivalent
agent-devicereplay on the same device and app; this was the larger of the two fixed overheads. The other is #777.Testing
packages/tool-server/test/flows/flow-launch-settle.test.ts, five cases: continues after a single read once content is present; keeps waiting on an empty tree; keeps waiting on the realandroid:id/contentshell captured above; continues early for a single-labelled-view screen; survives a throwing tree read. The fast-path assertions are on poll count rather than elapsed time, so they don't move with host load.vitest run test/flows— 1047 pass. One unrelated failure,flow-idle-run > settles inside the smallest timeout the parser allows, is a wall-clock assertion that fails identically on pristineorigin/main(1052ms vs its <1000ms bound) on this loaded machine.npm run lint,npm run knip,prettier --checkclean.