Skip to content

Commit bace781

Browse files
authored
fix(macos): attach to a running app instead of launching a duplicate on open (#3707)
* fix(macos): attach to a running app instead of launching a duplicate on open A macOS app started directly — by a supervisor or a dev script — is not listed by LaunchServices until half a second or so after it spawns. During that window `open -b` sees no instance and starts a second copy with no arguments, which the session then drives instead of the copy the caller owns (#3406). The bundle open now reads the same view `open -b` consults and, when it lists nothing while a process of that bundle is running, waits until LaunchServices names the running copy before running `open` unchanged. An already-listed bundle and a bundle with nothing running reach `open` on the first read; the wait, every read, and the process scan share one budget, and anything the probe cannot answer fails open so an open is never withheld. * fix(macos): size the registration probe to finish the race it waits out The probe's 5 s ceiling exceeded the healthy open it precedes by an order of magnitude while the case it waits out registers within ~0.75 s. The ceiling now covers the full poll plus the reads ahead of it with measured room (reads cost well under 300 ms together on a healthy host), so a host too loaded to answer within the wait is dropped at the remaining-budget floor instead of holding the open for seconds the race never needs. The batched bundle-identifier read gains a hermetic invariant against the real plutil: one -extract answers for every plist it is named, in argument order, so one call can pair every candidate bundle; the unit fakes cannot carry that proof because they script the rows. * test(macos): pin the real-plutil batch to the target's own pid The registration stub answered any pid-pinned read, so a batch that paired an identifier with the wrong bundle's process could still end the wait green. Only pid 4242's query now reports an ASN and the test asserts the exact read transcript, so a rotation of the row-to-path pairing (verified by a disposable mutation: 3 failed, 17 passed) fails this test on the target's pid rather than passing on another bundle's. * test(macos): point the probe routing comment at its owning test The final review caught that the comment in the routing test named host-provider-open.test.ts, which does not exist. The probe assertions live in launch-services-registration.test.ts, the file the comment should route a reader to. * fix(macos): read the process table through the provider's own executor The probe ran lsappinfo, plutil and the open through the injected Apple tool executor but read ps on the daemon's own host through the host-kit default. A scoped Apple provider could then pair pids found on one host with its lsappinfo and plutil on another. listHostProcesses({runCommand, timeoutMs}) routes the scan through the same seam and deadline the other reads already share, so the probe answers about one host end to end. The owning tests now script ps through fakeMacHost instead of mocking the process module. * test(macos): let the fake host answer the process-table read like any read fakeMacHost recorded the ps call and returned the table before consulting the responder, so the scan could not be made to throw, fail, or time out even though the probe now runs it through the same executor seam the Error-answering stub exists for. The responder is consulted first and the table is only the default answer. Three owning scenarios pin what the probe does when the scan itself fails: a tool that cannot spawn and a read killed at its budget end the probe with a typed diagnostic and the original open, and a non-zero ps exit is the unreadable table the empty answer already was — never 'nothing runs'.
1 parent 5b97e14 commit bace781

6 files changed

Lines changed: 1006 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Add `uninstall <app>` and `client.apps.uninstall({ app })` to remove an installed app on adb-backed Android targets, iOS simulators, and CoreDevice iOS devices. Both report `wasInstalled`. A package name or bundle id that is not installed succeeds with `wasInstalled: false`. Other targets, Appium and Limrun sessions included, refuse it before anything runs.
66
- Add `settings mobile-data on|off` for adb-backed Android targets. It switches mobile data through `svc data` and reports the `mobileData` state stored in the global `mobile_data` row; multi-SIM read-back is not covered yet. Every other target refuses it before anything runs, and `--app` is refused because the switch is device-wide.
77
- On iOS, a `fill` that runs under a degraded XCTest snapshot channel now aims at a point, including ref, selector and replay fills. When the runner identifies no text input under that point, it refuses with `NO_TEXT_INPUT_AT_POINT` (`error.details.reason: no_text_input_at_point`, `dispatched: no`) before dispatching its focus tap, and no field changes; when the input lookup itself cannot answer, it refuses with `TEXT_INPUT_PROBE_UNAVAILABLE` (`error.details.reason: text_input_probe_unavailable`). Previously such a fill cleared and typed through the app-level first responder, so it could rewrite a field the caller never named, or report a clear failure after emptying it. A ref or selector fill whose rect centre misses the real field (an outer container, a non-input node) now needs a point inside the input's own rect.
8+
- Opening a macOS app by bundle identifier now waits briefly when the app is already running but not yet registered with LaunchServices — the case when a supervisor or dev script started it directly — and if registration arrives within that bounded window the open attaches to the running copy, so the session drives the app the caller started, with its arguments. A copy still unlisted when the window closes gets the plain `open` as before, which can still duplicate; the wait only bounds the race, it does not close it. Launching an app that is not running costs one extra LaunchServices lookup and one process-table read, plus one bundle-identifier read when app-shaped processes exist.
89
- Limrun iOS snapshots no longer fail when the instance reports a numeric or boolean accessibility value (the system photo picker's tabs report `1`/`0`): the provider seam normalizes variant scalars to text, the way the Apple tree does. Limrun nodes now also publish `selected: true` for nodes carrying the `Selected` trait, so snapshot output and `selected:` selectors can tell a picked photo from an unpicked one.
910
- `settings` refuses a setting the target does not serve before anything changes, with `UNSUPPORTED_OPERATION`, `settings <setting> is not supported on this device`, and the target's `error.details.reason` (such as `unsupported-platform-leaf`). This replaces the macOS `INVALID_ARGS` refusal and the Apple `setting-unsupported-on-leaf` reason, and now also covers settings that used to reach the device first, such as Android `reset-keychain` or Apple `animations`.
1011
- `settings` refuses, before touching the device, a state its setting does not accept (`Invalid <setting> state: …`), a permission action or mode its target does not take (such as `permission deny accessibility`), and a stray extra argument (such as `clear-app-state <app> <extra>`). The CLI and the daemon accept the same state spellings, in any casing or surrounding whitespace.

‎packages/platform-apple/src/core/__tests__/tool-provider.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ test('local Apple tool provider exposes macOS host operations as semantic method
177177
const calls: Array<[string, string[], string | undefined]> = [];
178178
const provider = createLocalAppleToolProvider({
179179
runCommand: async (cmd, args, options) => {
180+
// The bundle open's LaunchServices registration probe is the seam's own read, asserted in
181+
// launch-services-registration.test.ts; this routing test only reports it as listing the bundle.
182+
if (cmd === '/usr/bin/lsappinfo') {
183+
return { exitCode: 0, stdout: 'ASN:0x0-0x1234-"Demo":\n', stderr: '' };
184+
}
180185
calls.push([cmd, args, typeof options?.stdin === 'string' ? options.stdin : undefined]);
181186
if (cmd === 'pbpaste') {
182187
return { exitCode: 0, stdout: 'copied\r\n', stderr: '' };

‎packages/platform-apple/src/os/macos/host-provider.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export function createLocalAppleMacOsHostProvider(
1818
): AppleMacOsHostProvider {
1919
return {
2020
openBundle: async (bundleId, url) => {
21+
// Loaded on the first bundle open, not when this provider is constructed: the probe's host-kit
22+
// reads stay off the Apple façades' eager closure (ADR 0019), and an open of an already-listed
23+
// app is the common path that never needs them until `open` actually runs.
24+
const { waitForLaunchServicesRegistration } =
25+
await import('./launch-services-registration.ts');
26+
await waitForLaunchServicesRegistration(runCommand, bundleId);
2127
await runCommand('open', buildMacOpenArgs(bundleId, url));
2228
},
2329
openTarget: async (target) => {

0 commit comments

Comments
 (0)