Skip to content

fix(open): stop the emulator an open booted when the open fails - #4842

Merged
thymikee merged 7 commits into
0.22from
fix/0.22-stop-emulator-after-failed-open
Nov 20, 2026
Merged

thymikee merged 7 commits into
0.22from
fix/0.22-stop-emulator-after-failed-open

Conversation

@thymikee

@thymikee thymikee commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Summary

An open that boots a stopped Android emulator and then ends without publishing a session left that emulator running with nothing to own it: no session recorded the boot, so no later close ever learned to stop it. The open now stops the launch it caused when its own remaining steps end with no published session, whether they return a failure, throw, or report the request canceled. A host that refuses the stop is treated as an unconfirmed stop; the open's own error stays the reported error either way. Fixes #4764.

The stop uses the launched-process owner the root composes — the same path close --shutdown confirms through (#4815) — so open orchestration names no platform. Before stopping, the cleanup asks the same ownership authority acquisition uses: another session of this daemon holding the booted device, or a claim conflicting with this open's session and workspace, keeps the launch. The final ownership answer and the bounded stop run as one exclusion under the per-device claim lock that acquisition writes a claim under, so no owner can be acquired for the device in between; a lock this process cannot take keeps the launch too. ADR 0029 is amended for the denied-policy outcome.

Before / after

Same cold open, failing after the boot:

  • Before: COMMAND_FAILED: pm list features timed out, emulator left running, no session.
  • Head: same code and message, plus error.details.failedOpenEmulatorCleanup: stopped, kept_running with failedOpenEmulatorCleanupReason (device-in-use, daemon-policy-denied, no-process-identity), or not_confirmed.

An emulator that was already running reports nothing and is never stopped, because readiness reports a launch only for a boot it started. A launch whose start time could not be read is left running with one diagnostic — no serial-based process probe. An open that lost the booted device to another session or claim, or found the device being claimed at that moment, keeps that owner's emulator running and says device-in-use. The rule covers failures after the boot completes; a boot that timed out or failed before ready leaves the emulator as it is (#4847).

Validation

Head f988585abc, base origin/0.22 = db5f1225e3. Review rounds one to three folded in: cleanup stop is try/caught, ownership compares the full pid+start-time pair, a session/claim conflict keeps the launch and reports device-in-use from the typed contracts table, the claim re-check and the stop share one acquisition of the per-device claim lock through withDeviceClaimLockHeld (daemon-contracts, no wait, typed ran/conflict), findSessionHoldingDevice is reused rather than repeated, open CLI help states the stop and its boot carve-out, ADR 0029 amended, docs link fixed, stop escalation shared and its budget comments reduced, routed cases for both conflict routes and the lock. Local: pnpm typecheck, pnpm check:layering (R89 quiet), pnpm format, fallow audit --base origin/0.22 (no issues on 25 changed files), oxlint, check:production-exports, check:daemon-wire-compat (protocol unchanged, ledger updated by the tool), check:doc-links, check:doc-prose, check:mcp-metadata, check:command-docs+MCP surface (always-loaded 22519 / 22528 B), and the full unit-core lane: 2159 files / 20823 tests. No local check:affected under host load — hosted CI validates the head.

Live runs on a dedicated AVD: a cold open failing after the boot (failedOpenEmulatorCleanup: "stopped", emulator process gone), a cold open interrupted during the boot, and a control are in #4842 (comment) (head 17eafaaaba); the failing-after-boot run was repeated at 4e9d7e0cb7 in #4842 (comment). The device-in-use route and the claim-lock exclusion are covered by routed and store-level tests only, not run as a two-daemon race.

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Size Report

Metric Base Current Diff
Installed (including dependencies) 6.19 MB 6.19 MB +3.8 kB
Package (unpacked) 6.19 MB 6.19 MB +3.8 kB
Package (download) 1.89 MB 1.89 MB +1.2 kB

Startup median (7 runs, lower is better):

Scenario Base Current Diff
CLI --version 26.5 ms 26.2 ms -0.3 ms
CLI --help 49.4 ms 49.6 ms +0.2 ms

@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 72f20d3. The verdict and the full list are in the summary comment.

An open that boots a stopped Android emulator and then ends without publishing a session left
that emulator running with nothing to own it: no session recorded the boot, so no later close
ever learned to stop it (#4764). The open now stops the launch it caused when its own steps end
without a session, whether they return a failure, throw, or report the request canceled, and
reports the result on that open's own error details as failedOpenEmulatorCleanup. A host that
refuses the stop itself is treated as an unconfirmed stop: the open's own error stays the
reported error either way.

Only a launch this open caused is stopped. The readiness runtime reports the process it started,
and reports nothing at all when the device was already running, so a prebooted emulator is never
mistaken for this open's launch and no serial-based process probe is added. A launch whose start
time could not be read keeps running with one diagnostic naming the step. When the daemon's policy
denies device-shutdown nothing is signaled and the reason rides the error (ADR 0029 amended).
The stop goes through the launched-process owner the root already composes, on the same path
close --shutdown uses, so open orchestration names no platform.
@thymikee
thymikee force-pushed the fix/0.22-stop-emulator-after-failed-open branch from 72f20d3 to 17eafaa Compare November 20, 2026 17:40
@thymikee

Copy link
Copy Markdown
Member Author

Live run: failed and interrupted cold opens on a dedicated AVD

Head 17eafaaaba, built in the PR worktree, branch CLI with an isolated --state-dir. AVD agent-device-4539-live (dedicated, not running before each run; it took serial emulator-5554, which was free). No other device was touched.

Run Command Result
A. Open fails after the boot open com.example.not.installed.app --platform android --device agent-device-4539-live --json (cold) exit 1 in 8.7 s, APP_NOT_INSTALLED, details.failedOpenEmulatorCleanup: "stopped". No qemu ... -avd agent-device-4539-live process and no adb device right after the command returned. Run twice, same result.
B. Open interrupted during the boot same cold open com.android.settings, CLI sent SIGINT after 2 s (timeout -s INT 2) CLI exit 124. The emulator process (pid 88875) was alive at the interrupt and gone about 2 s later. session list is empty; no adb device.
C. Control: open succeeds cold open com.android.settings success, selection.bootOccurred: true; the emulator stays up. Plain close then reports shutdown.success: true and the emulator exits.
{
  "success": false,
  "error": {
    "code": "APP_NOT_INSTALLED",
    "message": "No package found matching \"com.example.not.installed.app\"",
    "details": {
      "package": "com.example.not.installed.app",
      "failedOpenEmulatorCleanup": "stopped",
      "dispatched": "unknown"
    }
  }
}

Limits: for run B the evidence is the observed process exit and the empty session list; I found no daemon diagnostic line for the canceled case in the state dir, so the outcome label for that path is not shown here. The AVD boots from a Quick Boot snapshot (about 5 s), so the interrupt window is short. The isolated daemon was stopped after the runs.

@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: 1 finding moved to a new line on 17eafaa. The verdict and the full list are in the summary comment.

@thymikee

Copy link
Copy Markdown
Member Author

Reviewed 6cd7300: the change is close, but one regression and one help-text gap need fixing before merge.

The cleanup in session-open-launch-cleanup.ts:107 asks only whether a session under this open's name owns the boot. The post-boot steps can also fail because another owner holds the new serial: a session in this daemon (session-open-execution.ts:550) or a device claim from another daemon or workspace (session-open-execution.ts:563-566, DEVICE_IN_USE). Both return a response, so the open stops the launch by pid. Open A boots the AVD under the AVD-name lock. Open B targets emulator-5554 once adb lists it, under a different lock key, and publishes a session or claim. A then hits the conflict and kills the emulator B is using. Before this PR, A failed with DEVICE_IN_USE and B kept its device, so this breaks the PR's own rule and is a regression. The window is short, but the route is real and has no test. Could the rule be: the open stops its launch only when no session or claim other than its own holds the device? stopUnownedLaunch could enforce it, report kept_running with a typed reason such as device-in-use, and branch on the typed reason or code, not on message text. One routed test where a session already holds BOOTED_EMULATOR.id and the conflict response leaves owner.stops empty would prove it.

The new help sentence in app.ts:222 says any open that fails after launching a stopped emulator stops it. Head 6cd7300 narrowed commands.md to failures after the boot completes, so a boot that times out or fails before ready leaves the emulator running (tracked in #4847). The CLI help has no such carve-out, so an agent reading help open after a boot failure will assume the emulator was stopped. Please say "after a stopped Android emulator it launched finishes booting", or add the same carve-out commands.md states.

Not blocking, take or leave: stopAndroidEmulatorLaunchProcess in device-boot.ts:118 repeats the tail of confirmAndroidEmulatorLaunchProcessStopped (same stop call, identity mapping, budget and result mapping), so the latter could call the former after its wait, and the long comments at device-boot.ts:140 and on withFailedOpenLaunchCleanup could shrink to the constraint (3 s bound, TERM then KILL).

The inline thread on the pid and start-time comparison is fixed at this head (#4842 (comment)). The thread on a boot that throws after launch no longer applies, because commands.md now states that carve-out and #4847 tracks it; the help gap above is the remaining part (#4842 (comment)).

The required live evidence is in the author's comment at 17eafaa; this head differs only by one docs line. Not verified here: the live runs, and the race above, which comes from reading the code. The SIGINT-during-boot run exercises the existing abort rollback, not the new post-boot cancel branch, which only the routed unit test at session-open-failed-boot.test.ts:293 covers. Not verified here: the 3-second stop bound. Checks are still running on this head, and there are no conflicts. Before merge, the device-in-use case needs a fix with a routed test, the help text needs the carve-out, and the checks need to finish green.

@apex-sentinel-staging

This comment has been minimized.

A failed cold open stopped its launch whenever no session under its own name owned the boot,
so an open refused with DEVICE_IN_USE — a session in this daemon holding the booted serial, or
a claim from another workspace or daemon — killed the emulator that other owner had just taken
(#4764 review). The cleanup now asks the same authority acquisition uses: the device's session,
plus inspectDeviceClaimAcquisition for this open's own session and workspace. Only a conflict
keeps the launch; this open's own claim answers held and settles nothing. The kept launch is
reported as kept_running with the typed failedOpenEmulatorCleanupReason device-in-use from the
contracts table.

The open help states the boot carve-out commands.md already carries: the stop applies after the
emulator finished booting, and a boot that timed out or failed before ready leaves it running.
The Android stop keeps one escalation path, and the comment budgets state their constraint.
@thymikee

Copy link
Copy Markdown
Member Author

Round 2 folded in — commit 4e9d7e0cb7.

1. Session / claim conflict must keep the launch (blocking). stopUnownedLaunch now checks ownership before anything else can stop the process: another session in this daemon holding the device (the same findByDevice address lookup the rest of the open path uses), or the claim saying so via inspectDeviceClaimAcquisition — the exact decision acquisition makes for this open's session + workspace, asked without taking or settling anything, so this open's own claim answers held and an abandoned one answers reconcile. Only conflict counts as somebody else. When it does, the open keeps reporting its DEVICE_IN_USE failure and now also carries failedOpenEmulatorCleanup: "kept_running" + failedOpenEmulatorCleanupReason: "device-in-use" with owner.stops empty. The reason is the new FAILED_OPEN_KEPT_RUNNING_REASONS table in @agent-device/contracts/device-boot (daemon-policy-denied / no-process-identity / device-in-use), and the owner reads its members rather than literals. The check precedes the identity one, so a kept launch reports the reason that kept it. Two routed cases in session-open-failed-boot.test.ts: another session publishes the booted serial, and a live foreign claim in another workspace (registered owner, so it is not owner-daemon-superseded). commands.md lists device-in-use; ADR 0029 does not enumerate reasons, so it stands.

2. open help matches the boot carve-out. cliDetail now reads: stops the emulator "after a stopped Android emulator it launched finishes booting"; an emulator already running, one another session or claim then took (device-in-use), or a boot that timed out or failed before ready keeps it running. The always-loaded MCP surface is untouched (measured 22519 / 22528 B).

3. Stop path deduped. confirmAndroidEmulatorLaunchProcessStopped waits for the graceful exit and then delegates to stopAndroidEmulatorLaunchProcess; the escalation lives once. Comments reduced to the constraint: TERM then KILL inside 3 s, and why the close path confirms only briefly.

Validation this round: typecheck, layering guard (R89 quiet), oxlint, fallow audit --base origin/0.22, doc links and prose, MCP metadata + command docs, and the focused suites (480 files / 4214 tests) all green. Hosted CI covering the rest.

@thymikee

Copy link
Copy Markdown
Member Author

Live run repeated at 4e9d7e0cb7

Same setup as #4842 (comment) (dedicated AVD agent-device-4539-live, branch CLI built at this head, isolated --state-dir).

Run Result
Cold open com.example.not.installed.app --platform android --device agent-device-4539-live --json exit 1 in 8.3 s, APP_NOT_INSTALLED, details.failedOpenEmulatorCleanup: "stopped"; no -avd agent-device-4539-live process right after the command returned

So the ownership check added in this head (no other session or claim holds the device) still lets the open stop its own launch. The device-in-use route is covered by the two routed tests only; I did not stage two racing opens on a live device. The PR body line that says the live runs are still owed is out of date: they are in the linked comment and here.

@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: 1 new finding on 4e9d7e0. The verdict and the full list are in the summary comment.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

One high-priority defect remains at 8146622: a failed cold open can still stop an emulator that a second daemon already owns. The three earlier findings from #4842 (comment) are fixed at this commit.

A cold open can clear its claim on a preparation failure (session-open-execution.ts:591-593, :648-650). Cleanup then inspects the booted serial and finds no claim. A second daemon can acquire that serial (device-claims.ts:224-228) and publish its claim (:432) before the first daemon signals the process. The inspection at session-open-launch-cleanup.ts:154 takes no host-global claim lock. The signal path also awaits a PID identity read before SIGTERM (host-process.ts:807-812). The emulator is still the same process, so the identity check permits the stop. The failed open can then terminate an emulator that another daemon owns and is opening. The merge base left that emulator running. The final claim check and the bounded process stop should both run under the same per-device claim lock that acquisition uses. A foreign claim must be preserved, and so must the original open error.

Could daemon-contracts own a callback that runs only while the per-device claim lock proves the device has no conflicting owner, with session-open-launch-cleanup owning the stop and its outcome? The largest helper, stopUnownedLaunch, has no full duplicate. It reuses the existing claim inspection and process stop. Its in-daemon holder check repeats findSessionHoldingDevice in open-device-contention-wait.ts:129-136.

The three earlier inline threads are fixed at this commit and can be resolved: the pid and start-time comparison (#4842 (comment)), the help text carve-out (#4842 (comment)), and the reassigned-pid doc comments (#4842 (comment)).

CI is green: 15 checks, none failing, on 8146622. There are no conflicts. The device-in-use branch is a race between two opens and was not run live. Not verified here: the tests were not run locally, and the earlier live evidence at 17eafaa covers the stop route, which this change leaves unchanged. Not verified here: what inspectDeviceClaimAcquisition does on every I/O error. Once the claim-lock fix lands and CI stays green, nothing else blocks merge.

A failed open read the device's owner and then signalled the process it had booted, so an
acquisition that took the device in between had its emulator stopped (#4764 review). The final
ownership answer and the bounded stop now run as one exclusion under the per-device claim lock
that `acquireDeviceClaim` writes a claim under. The lock is taken without waiting: a lock this
process cannot take says somebody is claiming the device right now, which is the same answer a
claim that refuses this open gives, so the launch is kept and reported as device-in-use.
`withDeviceClaimLockHeld` owns that take-without-wait in the claim store, beside the lock
acquisition already uses, and reports ran or conflict.
@thymikee

Copy link
Copy Markdown
Member Author

Fixed at f988585abc. The final ownership answer and the bounded stop are now one exclusion under the per-device claim lock that acquisition writes a claim under, so no owner can be acquired for the device between "nothing conflicts" and the signal.

The helper (daemon-contracts owns it). withDeviceClaimLockHeld(deviceKey, env, task) lives in packages/daemon-contracts/src/device-claim-store.ts, beside withDeviceClaimLock — the same <claim>.json.lock key, the same owner identity, the same deviceClaimLockPath the acquisition path already derives. It re-exports from @agent-device/daemon-contracts/device-claims, the only claim subpath the daemon may import, and answers a typed union: { status: 'ran', result } or { status: 'conflict' }. session-open-launch-cleanup still owns the stop and every outcome. It takes the lock with tryAcquireProcessLock, i.e. one attempt, no wait: acquisition holds this lock for the one short write-or-clear transaction, so a lock this process cannot take says somebody is claiming the device right now, and that device is left alone rather than waited for. busy and unproven both answer conflict, so an unprovable lock keeps the launch too — never the reverse.

The stop route. After the identity, policy and published-session answers, the cleanup re-asks the claim question under the lock and only its answer permits the stop. The lock is held for exactly the re-check plus the bounded stop (TERM, then KILL, ≤3 s), nothing else. claimHeldByAnotherOwner is the same inspectDeviceClaimAcquisition call for this open's own session and workspace: conflict keeps, held/reconcile permit, as before. A lock conflict reports kept_running / device-in-use with one new diagnostic, open-kept-claim-in-flight — a foreign claim is preserved, and the original open error stays the reported error in every branch. The pre-lock session and claim checks stay so a claim conflict still outranks no-process-identity and daemon-policy-denied as the reason. commands.md and the open help now say device-in-use also covers a device "in the middle of" being taken.

findSessionHoldingDevice is exported from open-device-contention-wait.ts:129 and reused (open-device-contention-wait.ts:129-136 unchanged), so the in-daemon holder answer has one owner.

On your open question — what inspectDeviceClaimAcquisition does on every I/O error. It keeps the launch, which is the conservative direction: readDeviceClaimEntry maps any read failure other than ENOENT/ENOTDIR to classification: 'unknown' with the error, and a bad payload to inconsistent; both carry no claim, and inspectExistingClaimSettlement returns { status: 'conflict' } for an entry without a claim (device-claim-settlement.ts:129), as does an unreadable overlapping host-scoped entry. So every I/O error answers conflict, and the cleanup stops nothing.

Tests. packages/daemon-contracts/src/device-claim-store.test.ts (2 cases) proves the exclusion itself: a contending acquireDeviceClaim started inside the callback is still waiting when the callback ends and has written no claim file, then goes through once the lock is released; and a live foreign holder of the lock dir yields { status: 'conflict' } with the callback never run, after which the same call runs. The routed suite gains: a failed open reads its own stop and finds the lock held by this process (read from inside the stop, held open on a signal — no timing assumption), released once the stop is over; and the cleanup, asked directly with the lock held by a live foreign owner, attempts no stop and reports kept_running / device-in-use with open-kept-claim-in-flight. Dropping the lock from the cleanup, or from the store helper, fails these for the intended reason (checked by mutation). The earlier 13 cases, including both conflict routes and the stop route, stay green.

Validation at this head: pnpm typecheck, check:layering (R89 quiet), format, oxlint, fallow audit --base origin/0.22 (clean on 25 changed files), check:production-exports, check:daemon-wire-compat (protocol unchanged, 8 added / 0 removed — the new declaration is a contracts entry, ledger updated by the tool), check:doc-prose, check:mcp-metadata, command-doc guards, and the full unit-core lane locally: 2159 files / 20823 tests green. No local check:affected under host load — hosted CI validates the head. The two-daemon race is still routed tests only, not run live.

@thymikee

Copy link
Copy Markdown
Member Author

Live run repeated at f988585abc (stop under the claim lock)

Same setup as before (dedicated AVD agent-device-4539-live, branch CLI built at this head, isolated --state-dir).

Run Result
Cold open com.example.not.installed.app --platform android --device agent-device-4539-live --json exit 1 in 8.2 s, APP_NOT_INSTALLED, details.failedOpenEmulatorCleanup: "stopped"; no -avd agent-device-4539-live process right after the command returned

So with the final check and the stop now under the per-device claim lock, the open still takes the lock and stops its own launch. The conflict branches (device-in-use, lock held by another owner) are covered by the routed and claim-store tests only.

@apex-sentinel-staging

This comment has been minimized.

@thymikee

Copy link
Copy Markdown
Member Author

Most of the earlier review (#4842 (comment)) is fixed at f988585: a failed open now stops the emulator it booted, and the live run shows it. One docs finding remains.

The new open help at https://github.com/callstack/agent-device/blob/f988585/packages/command-surface/src/management/app.ts#L222 says an open that fails after its own emulator finishes booting stops that emulator within 3 seconds. When the daemon policy denies device-shutdown, the router sets deviceShutdownDenied (request-router.ts:200-201) and session-open-launch-cleanup.ts:108-113 leaves the emulator running and reports kept_running with daemon-policy-denied. The routed test at session-open-failed-boot.test.ts:239-259 expects zero stops. A caller who reads the help can believe the emulator is stopped when the daemon kept it on purpose. Please word the help as an attempt to stop an unowned launch, and point callers to failedOpenEmulatorCleanup for the confirmed outcome and the kept-running reason.

Could device-claims.ts accept the booted device for the no-wait claim-lock operation and derive the canonical key there? Then the claim owner would control the mapping that must agree with acquisition. The store would still own the lock mechanics, and session-open-launch-cleanup.ts would still own the stop and its reported outcome.

The three earlier inline threads no longer apply at this commit, so they can be resolved: pid and start-time comparison is unchanged by this delta (#4842 (comment)), the help-text carve-out was fixed in 6cd7300 (#4842 (comment)), and the reassigned-pid comments were fixed in 8146622 (#4842 (comment)).

Not verified here: tests were not run locally, and the held-lock branch (a two-daemon race) was not run live. The unit test with a lock held by another live pid covers that branch, and the live run at f988585 is the author report.

Smoke Tests (macos) and one other check are still running on f988585, and all other checks are green. The change only touches the failure branch of an Android cold open that launched an emulator, so a macOS smoke run that never fails such an open does not reach it. No conflicts are known. After the help fix, the remaining step before merge is for the running checks to finish green.

@thymikee

Copy link
Copy Markdown
Member Author

Reply to the review at f988585 (#4842 (comment)). Head is now e8df336da8; it changes only the open help sentence.

Help text. open help now says the open tries to stop the emulator within 3 s, tells the caller to read error.details.failedOpenEmulatorCleanup for the outcome (stopped, kept_running, not_confirmed) and failedOpenEmulatorCleanupReason for why it was kept, and lists the kept-running cases with daemon policy first (daemon-policy-denied), then device-in-use, an emulator that was already running, and a boot that timed out or failed before ready. check:command-docs, check:mcp-metadata, check:doc-prose and the help tests pass.

Could device-claims.ts take the booted device and derive the canonical key itself? Yes, and it would be the better boundary. Today the cleanup calls withDeviceClaimLockHeld(canonicalLocalDeviceKey(deviceClaimIdentity(device)), …). That is the same expression the claim owner uses at acquisition (device-claims.ts 181, 224, 327, 517), so the two agree today, but the agreement is by convention in a second module. A device-taking entry in device-claims.ts would make the claim owner the only place that maps a device to its lock, with the store keeping the lock mechanics and the cleanup keeping the stop and its outcome, as you describe. It is not in this head: this was the third review round, so the change here is limited to the help sentence. It is a small follow-up (one wrapper, one call site, the existing tests re-pointed) if the maintainer wants it in this PR or after it.

The three inline threads named in the review are resolved.

@apex-sentinel-staging

Copy link
Copy Markdown

Apex Sentinel · ✅ Approve · review of e8d8095

Since the review of f988585: 0 new · 0 still open · 0 no longer raised

No open findings.

1 new finding away from what changed since f988585 not shown. Comment @apex-sentinel-staging review for a full review.

Reviewed with this repository's own Sentinel configuration (0922cf287450).

💬 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.

@thymikee

Copy link
Copy Markdown
Member Author

The PR is ready at e8d8095. The help text now describes the stop as an attempt and points to failedOpenEmulatorCleanup for the outcome, and every kept-running case it lists matches the code at that commit. CI is green (15 checks, none failing), and there are no conflicts. Not blocking, and fine to take or leave: the help sentence at https://github.com/callstack/agent-device/blob/e8d8095/packages/command-surface/src/management/app.ts#L222 lists the cases where the emulator keeps running, but a fifth case exists (the launch start time could not be read, reason no-process-identity); adding it would match commands.md, and the help already points to failedOpenEmulatorCleanupReason for the reason. Not verified here: a live device run for this change, which only edits help text; the live cleanup evidence sits in earlier PR comments, and the device-in-use route is covered by routed and store tests only, as the PR body states.

@thymikee thymikee added the ready-for-human Valid work that needs human implementation, judgment, or maintainer merge label Nov 20, 2026
@thymikee
thymikee merged commit e937e57 into 0.22 Nov 20, 2026
15 checks passed
@thymikee
thymikee deleted the fix/0.22-stop-emulator-after-failed-open branch November 20, 2026 21:00
thymikee added a commit that referenced this pull request Nov 21, 2026
* fix(open): stop the emulator an open booted when the open fails

An open that boots a stopped Android emulator and then ends without publishing a session left
that emulator running with nothing to own it: no session recorded the boot, so no later close
ever learned to stop it (#4764). The open now stops the launch it caused when its own steps end
without a session, whether they return a failure, throw, or report the request canceled, and
reports the result on that open's own error details as failedOpenEmulatorCleanup. A host that
refuses the stop itself is treated as an unconfirmed stop: the open's own error stays the
reported error either way.

Only a launch this open caused is stopped. The readiness runtime reports the process it started,
and reports nothing at all when the device was already running, so a prebooted emulator is never
mistaken for this open's launch and no serial-based process probe is added. A launch whose start
time could not be read keeps running with one diagnostic naming the step. When the daemon's policy
denies device-shutdown nothing is signaled and the reason rides the error (ADR 0029 amended).
The stop goes through the launched-process owner the root already composes, on the same path
close --shutdown uses, so open orchestration names no platform.

* docs(commands): state that a failed boot is outside the failed-open stop

* fix(open): keep the launch when another owner holds the booted device

A failed cold open stopped its launch whenever no session under its own name owned the boot,
so an open refused with DEVICE_IN_USE — a session in this daemon holding the booted serial, or
a claim from another workspace or daemon — killed the emulator that other owner had just taken
(#4764 review). The cleanup now asks the same authority acquisition uses: the device's session,
plus inspectDeviceClaimAcquisition for this open's own session and workspace. Only a conflict
keeps the launch; this open's own claim answers held and settles nothing. The kept launch is
reported as kept_running with the typed failedOpenEmulatorCleanupReason device-in-use from the
contracts table.

The open help states the boot carve-out commands.md already carries: the stop applies after the
emulator finished booting, and a boot that timed out or failed before ready leaves it running.
The Android stop keeps one escalation path, and the comment budgets state their constraint.

* docs(device-boot): say a replaced launch answers stopped, as host-kit reports it

* fix(open): decide the failed-open stop under the device claim lock

A failed open read the device's owner and then signalled the process it had booted, so an
acquisition that took the device in between had its emulator stopped (#4764 review). The final
ownership answer and the bounded stop now run as one exclusion under the per-device claim lock
that `acquireDeviceClaim` writes a claim under. The lock is taken without waiting: a lock this
process cannot take says somebody is claiming the device right now, which is the same answer a
claim that refuses this open gives, so the launch is kept and reported as device-in-use.
`withDeviceClaimLockHeld` owns that take-without-wait in the claim store, beside the lock
acquisition already uses, and reports ran or conflict.

* docs(open): word the failed-open stop as an attempt and point to its reported outcome
thymikee added a commit that referenced this pull request Nov 21, 2026
* fix(open): stop the emulator an open booted when the open fails

An open that boots a stopped Android emulator and then ends without publishing a session left
that emulator running with nothing to own it: no session recorded the boot, so no later close
ever learned to stop it (#4764). The open now stops the launch it caused when its own steps end
without a session, whether they return a failure, throw, or report the request canceled, and
reports the result on that open's own error details as failedOpenEmulatorCleanup. A host that
refuses the stop itself is treated as an unconfirmed stop: the open's own error stays the
reported error either way.

Only a launch this open caused is stopped. The readiness runtime reports the process it started,
and reports nothing at all when the device was already running, so a prebooted emulator is never
mistaken for this open's launch and no serial-based process probe is added. A launch whose start
time could not be read keeps running with one diagnostic naming the step. When the daemon's policy
denies device-shutdown nothing is signaled and the reason rides the error (ADR 0029 amended).
The stop goes through the launched-process owner the root already composes, on the same path
close --shutdown uses, so open orchestration names no platform.

* docs(commands): state that a failed boot is outside the failed-open stop

* fix(open): keep the launch when another owner holds the booted device

A failed cold open stopped its launch whenever no session under its own name owned the boot,
so an open refused with DEVICE_IN_USE — a session in this daemon holding the booted serial, or
a claim from another workspace or daemon — killed the emulator that other owner had just taken
(#4764 review). The cleanup now asks the same authority acquisition uses: the device's session,
plus inspectDeviceClaimAcquisition for this open's own session and workspace. Only a conflict
keeps the launch; this open's own claim answers held and settles nothing. The kept launch is
reported as kept_running with the typed failedOpenEmulatorCleanupReason device-in-use from the
contracts table.

The open help states the boot carve-out commands.md already carries: the stop applies after the
emulator finished booting, and a boot that timed out or failed before ready leaves it running.
The Android stop keeps one escalation path, and the comment budgets state their constraint.

* docs(device-boot): say a replaced launch answers stopped, as host-kit reports it

* fix(open): decide the failed-open stop under the device claim lock

A failed open read the device's owner and then signalled the process it had booted, so an
acquisition that took the device in between had its emulator stopped (#4764 review). The final
ownership answer and the bounded stop now run as one exclusion under the per-device claim lock
that `acquireDeviceClaim` writes a claim under. The lock is taken without waiting: a lock this
process cannot take says somebody is claiming the device right now, which is the same answer a
claim that refuses this open gives, so the launch is kept and reported as device-in-use.
`withDeviceClaimLockHeld` owns that take-without-wait in the claim store, beside the lock
acquisition already uses, and reports ran or conflict.

* docs(open): word the failed-open stop as an attempt and point to its reported outcome
thymikee added a commit that referenced this pull request Nov 23, 2026
…#4991)

* fix(android): check ownership before a failed boot stops its emulator

#4863 stops the emulator a cold boot launched when that boot fails, and the
stop is gated only by the `device-shutdown` policy. A second `open` that binds
to the booting `emulator-NNNN` becomes that emulator's owner, and the first
open's later failure stopped it out from under that owner. The daemon's
failed-open cleanup (#4842) already refuses to stop a launch somebody else
holds; the readiness stop had no such answer.

Readiness now asks whoever owns the boot before its stop signals the launched
pid. The daemon answers from the same session and claim state its cleanup
reads, under that device's claim lock, so a claim being written right now
answers as ownership too. A held device is never signaled, whichever reason
failed the boot, and an answer that cannot be reached keeps the launch. A route
that opens no session, such as `boot`, has no ownership to ask about and keeps
following the boot's own rule.

Keeping a launch after its own failure is now the boot wait's own knowledge
rather than an undocumented `bootBudgetSpent` field on the error's details.

Taking the optional notes from #4863's final review: ADR 0029's amendment now
says the emulator host gates its own `terminate` and the launched-process stop
uses the request-level flag, and a composition test pins the shutdown gate into
the composed emulator host's `terminate`.

* fix(android): record an unanswered ownership question

The readiness stop already keeps a launch when nobody can say who owns the
emulator, but it swallows the reason. It now emits
android_emulator_launch_ownership_unanswered, in the same style as the
terminate refusal beside it, and the open's help, the command docs and ADR 0029
say that an unreachable answer stops nothing.

The failed-open cleanup asks the session half of the same question through
holdsDeviceInAnotherSession, so both stops read ownership one way.

* fix(android): keep a launch whose boot never resolved a serial

Sessions and device claims are keyed under the emulator's serial, so an AVD
name is not an address anything can be held under. Asking ownership about a
boot that never resolved a serial could therefore only answer "not held", which
is a guess at somebody else's emulator: the guard was vacuous exactly when the
boot failed before discovery named the serial, and the stop killed the
emulator a concurrent open had already taken.

Readiness now treats that answer as unknown and keeps the launch, the same way
it treats an unreachable one, and records
android_emulator_launch_ownership_unanswered with the reason. No re-resolution
is added: whoever resolves the serial is the open that binds to it.

The daemon test pinned the vacuous false answer for an AVD; it asks about a
serial nobody holds instead, which is the other real answer.

* docs(4867): scope the ownership answer to the open that launched the boot

Only `open` reads the session and claim state that answers ownership, and only
an `open` publishes the session a held device would belong to. `boot`, app
deployment, `apps`, `appstate`, `push` and `doctor` keep the boot's own rule and
stop their launch; covering every route from the daemon's readiness composition
is filed as a follow-up (#4867 comment r4660185881 finding).

The commands doc sentence and ADR 0029 said the whole boot-time rule, including
`boot` and deployment, stops nothing when the device is held. They now state the
scope: only `open` asks who holds the emulator.

* fix(android): stop a launch whose boot failed before a serial exists

Ownership is recorded under the emulator's serial, so a boot that failed
before discovery named one has nothing to ask about. Settling that address
would take one more inventory pass, and every inventory probe runs under the
request's own signal, which a canceled request has already aborted: the pass
could only fail. Ask ownership only of a boot that resolved a serial, and let
the earlier window stop its launch as base did, so no emulator is left running
with no session to own it (#4863).

Issue #4993 tracks giving that window, and every other daemon route that can
stop a failed boot's launch, an ownership answer from state that needs no
inventory.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>

* fix(android): keep the ownership diagnostic's reason a code

android_emulator_launch_ownership_unanswered recorded the ownership caller's
error message as its `reason`. Report the typed reason code
`ownership-query-failed` there and carry the message as `error`, so a reader of
the diagnostic log groups unanswered ownership questions by why, not by the
text each caller happened to throw.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>

---------

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>
thymikee added a commit that referenced this pull request Nov 23, 2026
* fix(open): stop the emulator an open booted when the open fails

An open that boots a stopped Android emulator and then ends without publishing a session left
that emulator running with nothing to own it: no session recorded the boot, so no later close
ever learned to stop it (#4764). The open now stops the launch it caused when its own steps end
without a session, whether they return a failure, throw, or report the request canceled, and
reports the result on that open's own error details as failedOpenEmulatorCleanup. A host that
refuses the stop itself is treated as an unconfirmed stop: the open's own error stays the
reported error either way.

Only a launch this open caused is stopped. The readiness runtime reports the process it started,
and reports nothing at all when the device was already running, so a prebooted emulator is never
mistaken for this open's launch and no serial-based process probe is added. A launch whose start
time could not be read keeps running with one diagnostic naming the step. When the daemon's policy
denies device-shutdown nothing is signaled and the reason rides the error (ADR 0029 amended).
The stop goes through the launched-process owner the root already composes, on the same path
close --shutdown uses, so open orchestration names no platform.

* docs(commands): state that a failed boot is outside the failed-open stop

* fix(open): keep the launch when another owner holds the booted device

A failed cold open stopped its launch whenever no session under its own name owned the boot,
so an open refused with DEVICE_IN_USE — a session in this daemon holding the booted serial, or
a claim from another workspace or daemon — killed the emulator that other owner had just taken
(#4764 review). The cleanup now asks the same authority acquisition uses: the device's session,
plus inspectDeviceClaimAcquisition for this open's own session and workspace. Only a conflict
keeps the launch; this open's own claim answers held and settles nothing. The kept launch is
reported as kept_running with the typed failedOpenEmulatorCleanupReason device-in-use from the
contracts table.

The open help states the boot carve-out commands.md already carries: the stop applies after the
emulator finished booting, and a boot that timed out or failed before ready leaves it running.
The Android stop keeps one escalation path, and the comment budgets state their constraint.

* docs(device-boot): say a replaced launch answers stopped, as host-kit reports it

* fix(open): decide the failed-open stop under the device claim lock

A failed open read the device's owner and then signalled the process it had booted, so an
acquisition that took the device in between had its emulator stopped (#4764 review). The final
ownership answer and the bounded stop now run as one exclusion under the per-device claim lock
that `acquireDeviceClaim` writes a claim under. The lock is taken without waiting: a lock this
process cannot take says somebody is claiming the device right now, which is the same answer a
claim that refuses this open gives, so the launch is kept and reported as device-in-use.
`withDeviceClaimLockHeld` owns that take-without-wait in the claim store, beside the lock
acquisition already uses, and reports ran or conflict.

* docs(open): word the failed-open stop as an attempt and point to its reported outcome
thymikee added a commit that referenced this pull request Nov 23, 2026
…#4991)

* fix(android): check ownership before a failed boot stops its emulator

stop is gated only by the `device-shutdown` policy. A second `open` that binds
to the booting `emulator-NNNN` becomes that emulator's owner, and the first
open's later failure stopped it out from under that owner. The daemon's
failed-open cleanup (#4842) already refuses to stop a launch somebody else
holds; the readiness stop had no such answer.

Readiness now asks whoever owns the boot before its stop signals the launched
pid. The daemon answers from the same session and claim state its cleanup
reads, under that device's claim lock, so a claim being written right now
answers as ownership too. A held device is never signaled, whichever reason
failed the boot, and an answer that cannot be reached keeps the launch. A route
that opens no session, such as `boot`, has no ownership to ask about and keeps
following the boot's own rule.

Keeping a launch after its own failure is now the boot wait's own knowledge
rather than an undocumented `bootBudgetSpent` field on the error's details.

Taking the optional notes from #4863's final review: ADR 0029's amendment now
says the emulator host gates its own `terminate` and the launched-process stop
uses the request-level flag, and a composition test pins the shutdown gate into
the composed emulator host's `terminate`.

* fix(android): record an unanswered ownership question

The readiness stop already keeps a launch when nobody can say who owns the
emulator, but it swallows the reason. It now emits
android_emulator_launch_ownership_unanswered, in the same style as the
terminate refusal beside it, and the open's help, the command docs and ADR 0029
say that an unreachable answer stops nothing.

The failed-open cleanup asks the session half of the same question through
holdsDeviceInAnotherSession, so both stops read ownership one way.

* fix(android): keep a launch whose boot never resolved a serial

Sessions and device claims are keyed under the emulator's serial, so an AVD
name is not an address anything can be held under. Asking ownership about a
boot that never resolved a serial could therefore only answer "not held", which
is a guess at somebody else's emulator: the guard was vacuous exactly when the
boot failed before discovery named the serial, and the stop killed the
emulator a concurrent open had already taken.

Readiness now treats that answer as unknown and keeps the launch, the same way
it treats an unreachable one, and records
android_emulator_launch_ownership_unanswered with the reason. No re-resolution
is added: whoever resolves the serial is the open that binds to it.

The daemon test pinned the vacuous false answer for an AVD; it asks about a
serial nobody holds instead, which is the other real answer.

* docs(4867): scope the ownership answer to the open that launched the boot

Only `open` reads the session and claim state that answers ownership, and only
an `open` publishes the session a held device would belong to. `boot`, app
deployment, `apps`, `appstate`, `push` and `doctor` keep the boot's own rule and
stop their launch; covering every route from the daemon's readiness composition
is filed as a follow-up (#4867 comment r4660185881 finding).

The commands doc sentence and ADR 0029 said the whole boot-time rule, including
`boot` and deployment, stops nothing when the device is held. They now state the
scope: only `open` asks who holds the emulator.

* fix(android): stop a launch whose boot failed before a serial exists

Ownership is recorded under the emulator's serial, so a boot that failed
before discovery named one has nothing to ask about. Settling that address
would take one more inventory pass, and every inventory probe runs under the
request's own signal, which a canceled request has already aborted: the pass
could only fail. Ask ownership only of a boot that resolved a serial, and let
the earlier window stop its launch as base did, so no emulator is left running
with no session to own it (#4863).

Issue #4993 tracks giving that window, and every other daemon route that can
stop a failed boot's launch, an ownership answer from state that needs no
inventory.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>

* fix(android): keep the ownership diagnostic's reason a code

android_emulator_launch_ownership_unanswered recorded the ownership caller's
error message as its `reason`. Report the typed reason code
`ownership-query-failed` there and carry the message as `error`, so a reader of
the diagnostic log groups unanswered ownership questions by why, not by the
text each caller happened to throw.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>

---------

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>
hannojg pushed a commit to hannojg/agent-device that referenced this pull request Nov 23, 2026
…stack#4842)

* fix(open): stop the emulator an open booted when the open fails

An open that boots a stopped Android emulator and then ends without publishing a session left
that emulator running with nothing to own it: no session recorded the boot, so no later close
ever learned to stop it (callstack#4764). The open now stops the launch it caused when its own steps end
without a session, whether they return a failure, throw, or report the request canceled, and
reports the result on that open's own error details as failedOpenEmulatorCleanup. A host that
refuses the stop itself is treated as an unconfirmed stop: the open's own error stays the
reported error either way.

Only a launch this open caused is stopped. The readiness runtime reports the process it started,
and reports nothing at all when the device was already running, so a prebooted emulator is never
mistaken for this open's launch and no serial-based process probe is added. A launch whose start
time could not be read keeps running with one diagnostic naming the step. When the daemon's policy
denies device-shutdown nothing is signaled and the reason rides the error (ADR 0029 amended).
The stop goes through the launched-process owner the root already composes, on the same path
close --shutdown uses, so open orchestration names no platform.

* docs(commands): state that a failed boot is outside the failed-open stop

* fix(open): keep the launch when another owner holds the booted device

A failed cold open stopped its launch whenever no session under its own name owned the boot,
so an open refused with DEVICE_IN_USE — a session in this daemon holding the booted serial, or
a claim from another workspace or daemon — killed the emulator that other owner had just taken
(callstack#4764 review). The cleanup now asks the same authority acquisition uses: the device's session,
plus inspectDeviceClaimAcquisition for this open's own session and workspace. Only a conflict
keeps the launch; this open's own claim answers held and settles nothing. The kept launch is
reported as kept_running with the typed failedOpenEmulatorCleanupReason device-in-use from the
contracts table.

The open help states the boot carve-out commands.md already carries: the stop applies after the
emulator finished booting, and a boot that timed out or failed before ready leaves it running.
The Android stop keeps one escalation path, and the comment budgets state their constraint.

* docs(device-boot): say a replaced launch answers stopped, as host-kit reports it

* fix(open): decide the failed-open stop under the device claim lock

A failed open read the device's owner and then signalled the process it had booted, so an
acquisition that took the device in between had its emulator stopped (callstack#4764 review). The final
ownership answer and the bounded stop now run as one exclusion under the per-device claim lock
that `acquireDeviceClaim` writes a claim under. The lock is taken without waiting: a lock this
process cannot take says somebody is claiming the device right now, which is the same answer a
claim that refuses this open gives, so the launch is kept and reported as device-in-use.
`withDeviceClaimLockHeld` owns that take-without-wait in the claim store, beside the lock
acquisition already uses, and reports ran or conflict.

* docs(open): word the failed-open stop as an attempt and point to its reported outcome
hannojg pushed a commit to hannojg/agent-device that referenced this pull request Nov 23, 2026
…callstack#4991)

* fix(android): check ownership before a failed boot stops its emulator

stop is gated only by the `device-shutdown` policy. A second `open` that binds
to the booting `emulator-NNNN` becomes that emulator's owner, and the first
open's later failure stopped it out from under that owner. The daemon's
failed-open cleanup (callstack#4842) already refuses to stop a launch somebody else
holds; the readiness stop had no such answer.

Readiness now asks whoever owns the boot before its stop signals the launched
pid. The daemon answers from the same session and claim state its cleanup
reads, under that device's claim lock, so a claim being written right now
answers as ownership too. A held device is never signaled, whichever reason
failed the boot, and an answer that cannot be reached keeps the launch. A route
that opens no session, such as `boot`, has no ownership to ask about and keeps
following the boot's own rule.

Keeping a launch after its own failure is now the boot wait's own knowledge
rather than an undocumented `bootBudgetSpent` field on the error's details.

Taking the optional notes from callstack#4863's final review: ADR 0029's amendment now
says the emulator host gates its own `terminate` and the launched-process stop
uses the request-level flag, and a composition test pins the shutdown gate into
the composed emulator host's `terminate`.

* fix(android): record an unanswered ownership question

The readiness stop already keeps a launch when nobody can say who owns the
emulator, but it swallows the reason. It now emits
android_emulator_launch_ownership_unanswered, in the same style as the
terminate refusal beside it, and the open's help, the command docs and ADR 0029
say that an unreachable answer stops nothing.

The failed-open cleanup asks the session half of the same question through
holdsDeviceInAnotherSession, so both stops read ownership one way.

* fix(android): keep a launch whose boot never resolved a serial

Sessions and device claims are keyed under the emulator's serial, so an AVD
name is not an address anything can be held under. Asking ownership about a
boot that never resolved a serial could therefore only answer "not held", which
is a guess at somebody else's emulator: the guard was vacuous exactly when the
boot failed before discovery named the serial, and the stop killed the
emulator a concurrent open had already taken.

Readiness now treats that answer as unknown and keeps the launch, the same way
it treats an unreachable one, and records
android_emulator_launch_ownership_unanswered with the reason. No re-resolution
is added: whoever resolves the serial is the open that binds to it.

The daemon test pinned the vacuous false answer for an AVD; it asks about a
serial nobody holds instead, which is the other real answer.

* docs(4867): scope the ownership answer to the open that launched the boot

Only `open` reads the session and claim state that answers ownership, and only
an `open` publishes the session a held device would belong to. `boot`, app
deployment, `apps`, `appstate`, `push` and `doctor` keep the boot's own rule and
stop their launch; covering every route from the daemon's readiness composition
is filed as a follow-up (callstack#4867 comment r4660185881 finding).

The commands doc sentence and ADR 0029 said the whole boot-time rule, including
`boot` and deployment, stops nothing when the device is held. They now state the
scope: only `open` asks who holds the emulator.

* fix(android): stop a launch whose boot failed before a serial exists

Ownership is recorded under the emulator's serial, so a boot that failed
before discovery named one has nothing to ask about. Settling that address
would take one more inventory pass, and every inventory probe runs under the
request's own signal, which a canceled request has already aborted: the pass
could only fail. Ask ownership only of a boot that resolved a serial, and let
the earlier window stop its launch as base did, so no emulator is left running
with no session to own it (callstack#4863).

Issue callstack#4993 tracks giving that window, and every other daemon route that can
stop a failed boot's launch, an ownership answer from state that needs no
inventory.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>

* fix(android): keep the ownership diagnostic's reason a code

android_emulator_launch_ownership_unanswered recorded the ownership caller's
error message as its `reason`. Report the typed reason code
`ownership-query-failed` there and carry the message as `error`, so a reader of
the diagnostic log groups unanswered ownership questions by why, not by the
text each caller happened to throw.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>

---------

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>
thymikee added a commit that referenced this pull request Nov 24, 2026
* fix(open): stop the emulator an open booted when the open fails

An open that boots a stopped Android emulator and then ends without publishing a session left
that emulator running with nothing to own it: no session recorded the boot, so no later close
ever learned to stop it (#4764). The open now stops the launch it caused when its own steps end
without a session, whether they return a failure, throw, or report the request canceled, and
reports the result on that open's own error details as failedOpenEmulatorCleanup. A host that
refuses the stop itself is treated as an unconfirmed stop: the open's own error stays the
reported error either way.

Only a launch this open caused is stopped. The readiness runtime reports the process it started,
and reports nothing at all when the device was already running, so a prebooted emulator is never
mistaken for this open's launch and no serial-based process probe is added. A launch whose start
time could not be read keeps running with one diagnostic naming the step. When the daemon's policy
denies device-shutdown nothing is signaled and the reason rides the error (ADR 0029 amended).
The stop goes through the launched-process owner the root already composes, on the same path
close --shutdown uses, so open orchestration names no platform.

* docs(commands): state that a failed boot is outside the failed-open stop

* fix(open): keep the launch when another owner holds the booted device

A failed cold open stopped its launch whenever no session under its own name owned the boot,
so an open refused with DEVICE_IN_USE — a session in this daemon holding the booted serial, or
a claim from another workspace or daemon — killed the emulator that other owner had just taken
(#4764 review). The cleanup now asks the same authority acquisition uses: the device's session,
plus inspectDeviceClaimAcquisition for this open's own session and workspace. Only a conflict
keeps the launch; this open's own claim answers held and settles nothing. The kept launch is
reported as kept_running with the typed failedOpenEmulatorCleanupReason device-in-use from the
contracts table.

The open help states the boot carve-out commands.md already carries: the stop applies after the
emulator finished booting, and a boot that timed out or failed before ready leaves it running.
The Android stop keeps one escalation path, and the comment budgets state their constraint.

* docs(device-boot): say a replaced launch answers stopped, as host-kit reports it

* fix(open): decide the failed-open stop under the device claim lock

A failed open read the device's owner and then signalled the process it had booted, so an
acquisition that took the device in between had its emulator stopped (#4764 review). The final
ownership answer and the bounded stop now run as one exclusion under the per-device claim lock
that `acquireDeviceClaim` writes a claim under. The lock is taken without waiting: a lock this
process cannot take says somebody is claiming the device right now, which is the same answer a
claim that refuses this open gives, so the launch is kept and reported as device-in-use.
`withDeviceClaimLockHeld` owns that take-without-wait in the claim store, beside the lock
acquisition already uses, and reports ran or conflict.

* docs(open): word the failed-open stop as an attempt and point to its reported outcome
thymikee added a commit that referenced this pull request Nov 24, 2026
…#4991)

* fix(android): check ownership before a failed boot stops its emulator

stop is gated only by the `device-shutdown` policy. A second `open` that binds
to the booting `emulator-NNNN` becomes that emulator's owner, and the first
open's later failure stopped it out from under that owner. The daemon's
failed-open cleanup (#4842) already refuses to stop a launch somebody else
holds; the readiness stop had no such answer.

Readiness now asks whoever owns the boot before its stop signals the launched
pid. The daemon answers from the same session and claim state its cleanup
reads, under that device's claim lock, so a claim being written right now
answers as ownership too. A held device is never signaled, whichever reason
failed the boot, and an answer that cannot be reached keeps the launch. A route
that opens no session, such as `boot`, has no ownership to ask about and keeps
following the boot's own rule.

Keeping a launch after its own failure is now the boot wait's own knowledge
rather than an undocumented `bootBudgetSpent` field on the error's details.

Taking the optional notes from #4863's final review: ADR 0029's amendment now
says the emulator host gates its own `terminate` and the launched-process stop
uses the request-level flag, and a composition test pins the shutdown gate into
the composed emulator host's `terminate`.

* fix(android): record an unanswered ownership question

The readiness stop already keeps a launch when nobody can say who owns the
emulator, but it swallows the reason. It now emits
android_emulator_launch_ownership_unanswered, in the same style as the
terminate refusal beside it, and the open's help, the command docs and ADR 0029
say that an unreachable answer stops nothing.

The failed-open cleanup asks the session half of the same question through
holdsDeviceInAnotherSession, so both stops read ownership one way.

* fix(android): keep a launch whose boot never resolved a serial

Sessions and device claims are keyed under the emulator's serial, so an AVD
name is not an address anything can be held under. Asking ownership about a
boot that never resolved a serial could therefore only answer "not held", which
is a guess at somebody else's emulator: the guard was vacuous exactly when the
boot failed before discovery named the serial, and the stop killed the
emulator a concurrent open had already taken.

Readiness now treats that answer as unknown and keeps the launch, the same way
it treats an unreachable one, and records
android_emulator_launch_ownership_unanswered with the reason. No re-resolution
is added: whoever resolves the serial is the open that binds to it.

The daemon test pinned the vacuous false answer for an AVD; it asks about a
serial nobody holds instead, which is the other real answer.

* docs(4867): scope the ownership answer to the open that launched the boot

Only `open` reads the session and claim state that answers ownership, and only
an `open` publishes the session a held device would belong to. `boot`, app
deployment, `apps`, `appstate`, `push` and `doctor` keep the boot's own rule and
stop their launch; covering every route from the daemon's readiness composition
is filed as a follow-up (#4867 comment r4660185881 finding).

The commands doc sentence and ADR 0029 said the whole boot-time rule, including
`boot` and deployment, stops nothing when the device is held. They now state the
scope: only `open` asks who holds the emulator.

* fix(android): stop a launch whose boot failed before a serial exists

Ownership is recorded under the emulator's serial, so a boot that failed
before discovery named one has nothing to ask about. Settling that address
would take one more inventory pass, and every inventory probe runs under the
request's own signal, which a canceled request has already aborted: the pass
could only fail. Ask ownership only of a boot that resolved a serial, and let
the earlier window stop its launch as base did, so no emulator is left running
with no session to own it (#4863).

Issue #4993 tracks giving that window, and every other daemon route that can
stop a failed boot's launch, an ownership answer from state that needs no
inventory.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>

* fix(android): keep the ownership diagnostic's reason a code

android_emulator_launch_ownership_unanswered recorded the ownership caller's
error message as its `reason`. Report the typed reason code
`ownership-query-failed` there and carry the message as `error`, so a reader of
the diagnostic log groups unanswered ownership questions by why, not by the
text each caller happened to throw.

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>

---------

Co-authored-by: Apex powered by GPT-5.6 <noreply@callstack.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-human Valid work that needs human implementation, judgment, or maintainer merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant