Skip to content

fix(android): stop closing foreign apps during recording dialog recovery - #5142

Open
thymikee wants to merge 4 commits into
mainfrom
t3/android-recording-dialog-guard
Open

thymikee wants to merge 4 commits into
mainfrom
t3/android-recording-dialog-guard

Conversation

@thymikee

@thymikee thymikee commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Summary

During a recording, a separate recovery tapped Close app on any Android ANR dialog, including another app's. It ran before type and every generic-route command, which contradicts #592 and #5115. The recovery is deleted. The blocking-dialog guard has no recording condition, so it now also covers recording. The guard is hardened:

  • The ANR title's process, including any :suffix, is resolved to its owning package through dumpsys activity processes. If no single package owns it, the ANR is refused.
  • Close app is tapped only next to the ANR's Wait button. The relaunch waits until a complete capture shows Wait gone.

Unanswered focus probes keep main's behavior. app-switcher, screenshot, and telephony stay unguarded.

Before / after

scroll or type while recording, with another app's ANR in focus:

  • Before: the foreign app is closed, then the command runs.
  • After: COMMAND_FAILED with the alert dismiss hint. Nothing is tapped.

ANR in com.example.app:worker:

  • Before: refused.
  • After: recovered when com.example.app owns that process.

Validation

At e9259bc, the focused vitest files pass: the platform-android suite, the daemon dialog/type/router/gesture suites, and all Android provider scenarios, with capture counts and the long-press ledger at main's values. Live API 37 recovery and refusal evidence is in a PR comment. CI is pending.

🤖 Generated with Claude Code

A recording-only recovery tapped Close app on any "isn't responding"
dialog before type and every generic-route command, including a dialog
owned by another app. Delete it. The blocking-dialog guard already runs
for type and the guarded generic commands with or without a recording:
it recovers the session app's ANR and refuses another app's ANR with the
alert dismiss hint. app-switcher now declares that guard like home and
back. screenshot and telephony stay unguarded, so a recording no longer
makes them close dialogs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.65 MB 6.65 MB -1.8 kB
Package (unpacked) 6.65 MB 6.65 MB -1.8 kB
Package (download) 2.03 MB 2.03 MB -223 B

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 21.3 ms 23.1 ms +1.8 ms
CLI --help 48.2 ms 51.4 ms +3.1 ms

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 12 files

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

At 9972959, four problems remain in Android ANR recovery. They can refuse or fail commands on the session's own app, or leave that app closed.

A second independent pass found that an Android process name is likely treated as a package name. Take com.example.demo with an application-level android:process of com.example.demo.worker. On an ANR, Android puts the process name in the window title. The parser reports it as the package, so the guard treats the session's own ANR as foreign. Recording-time type and guarded commands such as scroll and app-switcher then refuse before input. The base recording path found the Wait and Close app buttons and relaunched the session app without using the title. Android observation must resolve an ANR process to its owning package before the shared guard checks ownership. Ambiguous ownership must be refused, and Close app must require a verified session-app match.

A second independent pass found that recovery is likely lost when the window probe gets no answer. This happens if the ANR is visible in accessibility, but the first window dump times out and the focus helper has no application window to report. Observation then yields unknown, whatever the accessibility snapshot shows. The guard lets type or a guarded action reach the dialog. If the later check sees the ANR, it recovers after dispatch and returns COMMAND_FAILED. A foreign ANR also gets no refusal before input. The base recording path recovered first and then ran the action. The shared Android observer must follow an unanswered probe with a bounded second observation that can identify a visible ANR and its owner. Only a confirmed session-app ANR may be recovered. A foreign or unresolved owner must be refused.

A second independent pass found that recovery can likely launch before Android finishes closing the ANR. The tap can succeed while Android still has the close action queued. If the launch reaches the existing task and Android then kills the hung process, the focus poll cannot bring the app back. The command fails with the session app closed. The base recording path waited for the dialog to leave before launch. The new test fixture treats a tap as immediate dismissal. The shared recovery path must see the app-owned ANR clear after Close app and before relaunch.

A second independent pass found that the app-switcher guard likely changes behavior outside recording. The base command had no dialog check and sent the system key, which Android handles in system policy. Now a focused session-app ANR causes a close and relaunch before the key, and a foreign ANR blocks the key. With or without recording, a dialog that appears after the key can turn a delivered press into a failure and relaunch the app before the action is recorded. Outside recording, the key must keep its base dispatch and succeed once delivered. If recording-time app-switcher must refuse foreign ANRs and recover session-app ANRs, the shared guard should run only before that keypress.

Repo Guards & Compatibility fails in the layering model test, likely because of this PR. RECORDED_IMPORT_TYPE_CASES still lists the AndroidObservationAdapter import type that this change removes from interaction.ts. The list must name only edges that the source still has, and the "nine" in the test title must match the new count. The two cancelled Body rules jobs come from superseded runs.

Two open threads do not apply and can be resolved. In this one, the message and hint come only from the foreign-ANR refusal, and the no-tap and no-launch assertions show that nothing was closed. In the other, the message fragment appears only in the session-app ANR failure, and the empty tap list shows that the covered button was not tapped.

Not verified here: tests were not run, and no live device run was made. The claim that the foreign-ANR router test fails on main comes from reading the base code, not from running it. The CI log was read only as a failure excerpt.

Before merge, the four ANR problems need fixes, the stale layering entry needs removal, and CI needs to pass on the new push.

An ANR window title names a process, not a package. The observer now
resolves the owning package from dumpsys activity processes, and a
process with no single owning package stays unowned, so the guard
refuses it. When no window dump answers, a visible ANR is found by its
Wait button and its not-responding process. App-ANR recovery relaunches
the app only after the dialog has left. app-switcher keeps its unguarded
dispatch, and the layering model no longer lists the removed type edge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Addressed at e09a64e:

  • Process ownership: the observer resolves the ANR process to its owning package from dumpsys activity processes. If more than one package or no package owns it, package is unset and the guard refuses. Refusal details now include focusedProcess.
  • Unanswered window probe: a visible ANR (accessibility Wait button) is identified by its single not-responding process with a fresh, bounded dump. If no single owner is found, the guard refuses.
  • Relaunch timing: app-ANR recovery polls until Close app has left before it relaunches. The test fixture now clears the dialog on the next snapshot, not at the tap, and asserts the order tap → cleared → open.
  • app-switcher: the guard is reverted, so dispatch is the same as on main with or without a recording.
  • Layering: the removed AndroidObservationAdapter edge is dropped, and the test title now says eight.
  • cubic threads: both resolved with the reasons given above.

Not verified on a device. The dumpsys activity processes format is checked against fixtures only.

@thymikee

Copy link
Copy Markdown
Member Author

Scope notes for e09a64e:

  • Process → package: the existing focus data cannot name the package that owns a process. When an ANR dialog has focus, the focus helper reports the system window (android), and the window dump names only the process. Matching the name against the session app (equal, or <package>:<suffix>) would still refuse a global android:process name. So the observer reads dumpsys activity processes and uses each record's packageList. That is one parser of about 40 lines in app-parsers.ts and one extra dump, read only when a dialog is present. When the window probe gets no answer, the fallback uses the same parser and the snapshot read that already exists.
  • Wait before relaunch: on main, the only pre-launch wait helper belonged to the deleted recording recovery. The new 10-line poll has the same shape and the same ANDROID_MODAL_POLL_* bounds. It reads the snapshot until Close app is gone.
  • app-switcher: the descriptor change is reverted, so it stays unguarded.

@apex-sentinel-staging

This comment has been minimized.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 2 new findings on e09a64e. The verdict and the full list are in the summary comment.

@thymikee

Copy link
Copy Markdown
Member Author

At e09a64e, session-app ANR recovery can still refuse the session's own app, tap the wrong dialog, or relaunch too early. Since the earlier review, the app-switcher guard change is reverted and the stale layering entry is gone. Process owners are now resolved, and relaunch now waits after Close app. The fallback for an unanswered window probe now exists, but the process-name, fallback and relaunch problems are only partly fixed.

Does the fallback miss the ANR flag on Android 12 and later? The flag match is case-sensitive and needs a word boundary before notResponding. These versions likely print mNotResponding=true, as a second independent pass also found. If so, a session-app ANR gets no owner when the window dumps do not answer. type or a guarded command then fails with COMMAND_FAILED instead of recovering as the base recording path did. The flag must parse in every AOSP format that dumpsys activity processes emits. Fixtures such as this one should include the prefixed form, taken from a real device dump.

Live proof is required before merge, because this change decides between closing an app and refusing a command from a new process dump that only hand-written fixtures cover. On an API 31+ device or emulator, bring up the session app's ANR dialog (for example with a button that blocks the main thread), then run scroll or type. The output must show the Recovered Android app ANR before … warning and the relaunched app. Attach the raw adb shell dumpsys activity processes excerpt for that process, with the *APP* line, packageList and the not-responding line. Then bring up a foreign app's ANR. That run must show the COMMAND_FAILED refusal with the alert dismiss hint and no tap.

A second independent pass found that private process names are likely cut before the owner lookup. With android:process=":worker", the ANR title names com.example.demo:worker, but the title pattern keeps only com.example.demo. No record matches, so recording-time scroll or type is refused, although the base recording path recovered this case. Every parsed ANR process name must reach the lookup whole, including its suffix. The focused-ANR observation fixture should cover the colon form.

A second independent pass found that the fallback can likely tap a different dialog on versions that print the accepted flag, such as Android 7. The session app can be the only flagged process while its Wait button sits in a retained window under a foreign crash dialog. The fallback assigns that owner from any Wait node, and recovery can then tap the crash dialog's Close app. A second flagged ANR would cause a refusal, but a crash dialog adds no such flag, so this thread still applies to that case. A tap must require evidence that links the visible ANR window and its Close app button to the owned process. Without that link, the ANR must stay unowned.

A second independent pass found that recovery can likely relaunch while the ANR is still visible. After the tap, Close app can have no bounds or be covered by a short overlay while Wait is still shown. The poll treats this as a cleared dialog, while the base recording path waited on the Wait button. Relaunch must wait until the ANR itself is gone, identified by its window or the Wait marker. An unreadable sample must not count as cleared. The tappable-button check should only decide where to tap.

A second independent pass found that an unanswered focus probe now runs a full capture, even on an ordinary screen, and guards can pay that cost before and after input. The Coverage job records 8 captures where 4 were expected, and 6 and 5 where 3 to 4 were allowed. This thread still applies. An explicit no-focus answer, such as mCurrentFocus=null, should not trigger the fallback. A truly unanswered probe needs a bounded, dialog-specific read that adds no full replay capture when no ANR is shown.

Two other open threads do not apply and can be resolved. In this one, the existing message, hint, no-tap and no-launch assertions are enough. In the other, the message fragment occurs only in the session-app failure, and the empty tap list shows that no covered button was tapped.

Coverage fails with four tests. Three are the capture-count failures above. The fourth is in the long-press ledger test: the worst case of 349987 ms plus the 30000 ms margin exceeds the 320000 ms envelope. It is likely caused by the added fallback reads, so the worst case must fit the envelope again. Body rules also fails because the description has 254 words and the limit is 250.

Not verified here: no device run was made, local tests were not run, and a real process dump has not confirmed the Android 12+ field spelling.

Before merge, the ANR problems need fixes, the live API 31+ evidence needs to be attached, the description needs to be under the limit, and Coverage needs to pass.

…s name

ANR process names keep their :suffix through the owner lookup, and the
process dump flag parses in both the notResponding and mNotResponding
spellings (the latter from an API 37 dump). An unanswered window probe
no longer captures a snapshot on an ordinary screen: it is skipped on
mCurrentFocus=null, reads the process dump within the same observation
budget, and captures only when a process is not responding. An ANR seen
that way has no owner, so it is refused and never tapped. Recovery taps
Close app only next to the ANR's Wait button and relaunches only after a
readable snapshot shows the Wait button gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Live evidence, using the branch build at 656fc13's parent code (the behavior is unchanged in 656fc13), on an Android 17 (API 37) emulator. Setup: the session app is com.callstack.agentdevicelab. A process is frozen with kill -STOP (via adb root), and one tap raises its input ANR.

Session-app ANR (dumpsys window focus, then the process record):

mCurrentFocus=Window{4858961 u0 Application Not Responding: com.callstack.agentdevicelab}
  *APP* UID 10233 ProcessRecord{8851e5b 29454:com.callstack.agentdevicelab/u0a233}
    packageList={com.callstack.agentdevicelab}
     mCrashing=false null mNotResponding=true [com.android.server.am.AppNotRespondingDialog@ec3b7c1] bad=false

scroll down:

Scrolled down
Warning: Recovered Android app ANR before scroll: closed and relaunched com.callstack.agentdevicelab.

Afterwards: mCurrentFocus=Window{f0368d9 u0 com.callstack.agentdevicelab/com.callstack.agentdevicelab.MainActivity}, pid 29454 → 30017.

Foreign ANR (Settings is brought to the front with am start and frozen; the session app stays the same):

mCurrentFocus=Window{3a5ac8e u0 Application Not Responding: com.android.settings}
  *APP* UID 1000 ProcessRecord{4bf0afa 19226:com.android.settings/1000}
    packageList={com.android.settings}
     mCrashing=false null mNotResponding=true [com.android.server.am.AppNotRespondingDialog@e23d462] bad=false

scroll down:

Error (COMMAND_FAILED): Android system dialog is blocking scroll: Application Not Responding: com.android.settings (package com.android.settings). May have reached the device. Next: Run snapshot to check the screen before you retry. Run alert dismiss to close the app that is not responding, or alert accept to wait for it, then retry.

Afterwards: the ANR is still focused, the Settings pid is unchanged (19226), and the request log has no input tap. The mNotResponding line above is now the fixture in the observation and parser tests.

On this image, dumpsys window windows printed no focus line, so the observer answered from dumpsys window.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of 656fc13

Since the review of e09a64e: 1 new · 0 still open · 2 no longer raised (2 marked fixed)

1 finding(s): 1 minor.

🟡 Minor (1)

Where Finding
packages/platform-android/src/observation.ts:180 🆕 Session-app ANR recovery now needs a dumpsys activity processes record line to parse
🔧 2 findings marked fixed in their threads
Finding Fixed by Where
Every unobserved blocking-dialog read now costs a full accessibility snapshot, twice per command thymikee 656fc13 · reply
A visible ANR is attributed to whichever process is flagged notResponding, not the dialog's thymikee 656fc13 · reply

1 finding below minor hidden (user_settings.min_severity).

Reviewed with this repository's own Sentinel configuration (7d392e62143f).

💬 Talking to Sentinel

In a finding's thread, reply with:

Command What it does
@apex-sentinel-staging note <context> Adds context; Sentinel re-checks the cited code
@apex-sentinel-staging pivot <why> Says the finding is wrong; Sentinel re-checks the cited code
@apex-sentinel-staging like Marks the finding as useful
@apex-sentinel-staging dislike <why> Marks it as not useful, so it is not raised again

On this conversation, comment @apex-sentinel-staging review to run a full review once again.

@apex-sentinel-staging apex-sentinel-staging Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apex Sentinel: 1 new finding on 656fc13. The verdict and the full list are in the summary comment.

@thymikee

Copy link
Copy Markdown
Member Author

At 656fc13, three ANR cases likely still need fixes: a visible ANR can pass without a refusal, an ordinary screen can pay for an extra capture, and relaunch can start too early. Since the earlier review, both flag spellings parse, :suffix process names stay whole, the fallback no longer assigns an owner, and a tap requires the Wait button. Explicit no-focus answers, and process dumps with no flagged process, now add no capture. Coverage now passes. The relaunch and capture-cost problems are only partly fixed.

A second independent pass found that an exhausted focus probe likely skips the refusal of a visible ANR. If a foreign ANR has focus during a recording and the window dump spends the 10-second budget, the process dump reads as empty. The empty process set then skips the only Wait check. The daemon treats unknown as clear, so scroll or type reaches the app instead of being refused. When the focus probe is unanswered, a visible Wait button must always produce an unowned dialog, including on every path where the process dump gives no usable result. That check needs its own reserved, bounded time, and a test with a spent budget should show the refusal.

A second independent pass found that the fallback likely still adds a full capture when no ANR dialog is visible. Android sets mNotResponding=true before tracing and can kill a silent background ANR without showing a dialog. An unanswered focus probe then runs the snapshot read, and the command runs because there is no Wait button. The check after input can pay the same cost. A flagged process without a visible dialog must never add a full snapshot capture, for example by using a bounded lookup for the Wait button only. The no-Wait test should assert this.

A second independent pass found that a failed snapshot can likely trigger an early relaunch. Android may still be clearing the ANR after Close app when the next snapshot hits the presentation deadline. That capture resolves as empty, truncated and sparse instead of throwing, as this test shows. Only the node array reaches the leave poll, which reads it as Wait being gone and relaunches. Only a complete sample may confirm that Wait is absent. Every degraded capture result must keep the poll waiting, and a test with a sparse sample should show no early relaunch.

These threads no longer apply and can be resolved if still open: process record lookup (a live API 37 record has the parsed format, and an unanswered dump leads to a safe refusal), capture cost (mCurrentFocus=null now skips the fallback, and the snapshot is read only for a flagged process), and fallback owner (the fallback ANR is now refused without a tap). The refusal assertions and the occlusion test are already resolved and need no change.

The required live evidence is attached, from a run at e09a64e. Not verified here: the new Wait requirement for the tap and the Wait-based leave poll were not exercised on a device, although the standard Android ANR layout includes that button. Not verified here: no device run was made and no local tests were run. A test showing that a crash dialog's Close app is not tapped without Wait would add coverage, but is not required for merge.

The earlier Body rules failure concerned only the description length, and a later run passed. There are no conflicts. Before merge, the three ANR cases above need fixes.

…lete captures

An unanswered focus probe again leaves the observation unknown, as the
guard does on every path on main; a stronger check belongs to the guard
for all commands. The process-owner lookup stays for answered probes,
without the not-responding flag that only the fallback read. The ANR
recovery snapshot host now rejects a truncated or sparse capture, so a
failed presentation after Close app keeps the relaunch waiting instead
of reading as the Wait button gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thymikee

Copy link
Copy Markdown
Member Author

Scope decision at e9259bc (third review round, delegated by the maintainer):

  • Unanswered focus probes keep main's behavior on every path. The fallback (a second process dump plus a snapshot read) is removed, which resolves the first two findings by deletion. A stronger check for unanswered probes, if wanted, belongs to the guard for all commands in a separate change, not to recording-time recovery.
  • The owner lookup stays for answered probes. It covers :suffix names and uses the record format verified on API 37. The notResponding flag parsing is removed, because only the fallback read it. Unproven ownership is refused, never recovered.
  • Leave poll: the recovery snapshot host now rejects truncated or sparse captures, so a failed presentation after Close app keeps the poll waiting within its existing limits. A new test shows that the relaunch waits past a sparse sample.
  • Capture counts and the long-press ledger match main. The owned-path logic is unchanged, so no new live run was made.

This branch has not been deployed

No deployments
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