perf(ios): halve hostile-screen capture cost under the XCTest-channel penalty

Merged
TH
4mo ago
+288-15

What

Two changes to how iOS private-AX captures behave once the XCTest-channel penalty (#1105/#1156) has marked a screen class hostile, plus a verdict-semantics fix the second change exposed:

  1. Skip the viewport read while the channel is penalized (RunnerTests+AXSnapshotFallback.swift). The viewport read is XCTest main-thread work — the exact channel the penalty marks as grinding. Live measurement on the AppControlBench Bluesky feed showed it burning its full 1s runMainThreadWork timeout and falling back to the root frame on every capture. Under penalty it now goes straight to the fallback, same policy as the existing abandoned-tree-capture guard.

  2. Remember the accepted private-AX ladder depth per bundle. Deep RN screens reject the default depth-64 request with kAXErrorIllegalArgument on every capture (~310ms each) before succeeding at 56. The accepted rung is now remembered per bundle with the penalty's lifetime model (120s TTL, cleared on target process change). A first-rung success on remembered depth deliberately does not refresh the TTL, so expiry re-probes the full requested depth — screens that regain deep-capture ability are never capped forever. Explicit --depth requests bypass the memory in both directions.

  3. New deferred reasonCode for penalty-preselected captures. The plan runner stamped penalty-preselected captures with the same recovered verdict as a capture that ground through a live failure. On hostile screens that meant the full "Detected an overly complex or slow accessibility tree…" warning repeated on every capture (130× in one 30-task bench run — pure context noise for the driving agent), and settle's one-shot private-AX budget reset fired on every loop even though the capture paid no grind. The deferred case now carries reasonCode: 'deferred'; the daemon keeps the verdict recovered but suppresses the repeated warning (depth-cap line stays) and skips the settle budget reset. Older daemons drop unknown reason codes and keep today's behavior, so the runner/daemon pairing is forward/backward safe.

Why

Software Mansion's AppControlBench re-run at 0.20.5 showed Bluesky tasks ~2× slower than the published 0.17.6 numbers. Decomposing the transcripts: ~half the delta was environmental (model transport startup, Expo dev-client popup absent from SWM's golden), but the largest tool-side cost was private-AX capture overhead multiplied through --settle (2 captures per settle, 227 settle requests per 30-task run) and every press target resolution.

Runner-side timeline per hostile capture before this change: ~310ms failed depth-64 rung + ~350ms real work + ~1000ms doomed viewport read ≈ 1.65s, ×2 per settle.

Measured (live, seeded bench Bluesky feed, 139–171-node tree)

operationbeforeafter
snapshot -i steady-state (CLI wall)2.65s~0.55s
press --settle round-trip~5.4s+~2.0s (settled after ~1.0s)
runner-side capture~1.65s~0.35s
bench task bsky-04 end-to-end271s / 26 tool calls130s / 11 tool calls

Healthy-screen behavior is unchanged (verified on Settings: healthy verdict, no memory involvement, penalty+memory cleared on app switch).

Reviewer notes

  • The budget-reset skip for deferred is intentionally conservative: genuine recoveries (a capture that ground through XCTest and then fell back mid-plan) keep the reset — that path still needs its budget back.
  • xcTestChannelStateFirstFailure was extracted pure so the deferred-vs-budget stamping is pinned by an in-bundle test; the bounded-XCTest-probe case (physical devices) keeps budget since its short probe slice genuinely constrains the capture.
  • Tests: 2 new in-bundle Swift tests (attempt-depth ladder math, memory match/expiry) + 1 stamping test, new TS tests for warning suppression and the no-reset settle path. Full unit suite green (5291 tests).

Review round 1 (3696f338b)

  • Depth memory is now PID-bound (P1): it stores the process identifier it was learned under and matches only the same live process; recording without a PID is refused. All invalidation paths (invalidateCachedTarget, external relaunch, A→B→A with restart while inactive) are covered automatically because they all drop currentAppProcessIdentifier.
  • deferred is pinned through the raw wire parser: the settle no-reset test and warning-suppression test now build their verdict via readSnapshotQualityVerdict on a raw runner payload, plus a direct membership test.
  • Viewport skip has a structural regression test: shouldReadPrivateAXViewportViaXCTest() extracted and pinned (penalized → skip, cleared → read, abandoned-capture → skip).

Pre-fix red evidence (new tests on base 80feff42d)

FAIL  the runner-wire 'deferred' reasonCode survives parsing into warning suppression
      + undefined  - 'deferred'                       (parser strips the unknown code)
FAIL  penalty-deferred recovered captures suppress the fallback warning but keep depth copy
      + 'Detected an overly complex or slow accessibility tree…'   (warning re-appears)
FAIL  penalty-deferred private-ax captures do not reset the settle budget
      settled: true instead of false                  (budget reset fires again)

The Swift-side tests reference symbols introduced by this PR (privateAXAttemptDepths, shouldReadPrivateAXViewportViaXCTest, PID-bound memory), so their base state is "does not compile" rather than red.