Skip to content

fix(daemon,proxy): share outer session device and proxy leases in test attempts - #5149

Closed
MilindChatrabhuji wants to merge 5 commits into
callstack:mainfrom
MilindChatrabhuji:fix/test-outer-session-device-5129
Closed

MilindChatrabhuji wants to merge 5 commits into
callstack:mainfrom
MilindChatrabhuji:fix/test-outer-session-device-5129

Conversation

@MilindChatrabhuji

@MilindChatrabhuji MilindChatrabhuji commented Nov 27, 2026 •

Copy link
Copy Markdown

Summary

Share outer session device claims and request-scoped proxy leases across nested test attempts without leaving leases or iOS simulator runners dangling after test ends, addressing #5129.

  • Skip per-attempt lease release for shared suite leases via internal suiteLeaseId while keeping leaseRetainedBySuite restricted to cases where another live session holds the lease, so unheld request-lease attempts stop their simulator runner and retain zero hosts.
  • Track suiteRetainedLease during attempt cleanup so finally only ends a lease that suite cleanup preserved, keeping unrelated caller leases intact across rejections or open-free runs.
  • Document request-lease release timing and extract request-scope helpers to satisfy Fallow complexity bounds.

Before / after

Before: Second nested open fails DEVICE_IN_USE / UNAUTHORIZED, or an unheld request lease either stays active / retains its simulator runner host or gets prematurely revoked on empty/failing test calls.
After: Multi-script outer-session and request-lease tests pass (passed: 2); only retained suite request leases end once at suite end, and zero retained simulator hosts remain.

Validation

Verified locally at HEAD 4f4253ece77b2a0ee0fe9b7413c045189242c4be (Windows 11, Node v22.14.0):

oxfmt --check (19 files): passed
tsc -b tsconfig.typecheck.json examples/sdk/tsconfig.json: passed
node --experimental-strip-types scripts/layering/check.ts: passed (2040 files)
oxlint . --deny-warnings: passed (0 warnings, 0 errors across 5429 files)
fallow audit --base 7d392e62143f932444f364681498dd7623be9004: passed (dead code 0, complexity 0, duplication 0 across 20 files)
vitest run (6 fast daemon/CLI files: session-close-lifecycle-teardown, session-replay-command, session-close-ended-lease, request-ended-leases, cli/connection/ended-lease, platform-runtime-resource-cleanup-retention): 35/35 passed
vitest run http-server-batch-maestro-trust.test.ts --testTimeout=15000: 6/6 assertions passed (exit 1 from Windows 2.5s slow-test duration check)
vitest run request-router-replay-scope.test.ts --testTimeout=25000: 11/11 assertions passed (exit 1 from Windows 2.5s slow-test duration check)
vitest run http-server-remote-output.test.ts -t "remote suite artifacts" --testTimeout=25000: first/second RPCs succeeded without LEASE_NOT_FOUND; L930 artifact download failed HTTP 500 on Windows
tsdown --config-loader native: passed (73.6s)
Not verified locally: Linux CI Coverage rerun and live macOS/iOS simulator & proxy agent-device test run (Windows host).

@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 | Re-trigger cubic

@thymikee

Copy link
Copy Markdown
Member

Thanks for this fix. At 8822191, two things need work before merge: a lease can stay rented after a test suite ends, and a live device run is missing.

isSuiteAttemptSharedLease returns true when the attempt's lease matches the test request's flags.leaseId or internal.admittedLease, even if no other live session holds it. Every cleanup close copies both, so the lease release is skipped and nothing ends the lease after the last attempt. This affects a proxy test without open, and an SDK or gateway test that passes the lease in request metadata. This is a regression: the remote device stays rented until the client releases it or the TTL expires, and endedLeases never reports it. The router test pins this behavior, and the lease contract doc becomes false. The rule: an attempt cleanup ends its lease unless another live session still holds it, and a lease from the test request that no session holds after the last attempt ends exactly once. Please keep only the other-session clause, which covers the outer-open case. Let handleReplayTestCommand end a request-only lease through the releaseSessionLease path and record it with recordEndedLease on the request ledger. Drop the two lease copies from the cleanup close request, and update the doc. A two-script router test should assert that endedLeases equals [{leaseId, outcome: 'released'}], with one entry only.

The PR changes device claim admission, claim acquisition on open, and attempt close teardown on a device that the outer session still uses. The tests use fake runtimes only, so a live run is required before merge. Build the new head with pnpm build and use node bin/agent-device.mjs on an iOS simulator in one session S: run open --platform ios, install <app>, then test first-launch.yaml --maestro --platform ios. The output should show passed 1 and no DEVICE_IN_USE in attempt-1. Then show that device status still lists the claim with S as the same owner, snapshot in S succeeds, and close in S releases the claim. If a proxy is available, also run connect proxy, then test without open, and paste the reply's endedLeases.

Could this be simpler? The request scope could resolve the live covered host once and pass it to the existing coveredBySession rule (with its workspace check) and to claim acquisition on open, while session-holder exclusion keeps its findByDevice parameter. With the lease rule above (the existing listRefs check plus one suite-level end), the flags.leaseId and admittedLease copies and the unused suiteHostSession on close could go away.

The four earlier inline threads no longer apply and can be resolved: findByDeviceExcluding is now part of findByDevice, so no new layering entry is needed; pinOwnProcessStartTime now runs at module scope; a leaseWillEnd=false test now exists, although it relies on the admittedLease shortcut that should be removed; and both call sites now require open-session and a live claim through the shared check.

Only one check has reported, and it passes. CI is still pending: the device and e2e lanes wait for fork approval, and they exercise the changed open, claim and close routes.

Not verified here: the tests and a live device run were not executed. Runner and app retention for the outer session after an attempt closes was not checked. The value of the outer-open router test as a regression test was judged only by reading the previous open path. Client-side lease release was not checked for every gateway; the proxy connection state still stores the lease id, which limits stranded leases but does not satisfy the endedLeases contract.

Please fix the lease release first, then add the live simulator evidence.

@MilindChatrabhuji

Copy link
Copy Markdown
Author

@thymikee Thank you for catching the request-lease retention bug in 8822191 and suggesting the simplification! Pushed da7ac6be944f00aa2012caabcff6f49a40f29f4a:

  1. Suite lease release semantics (session-close.ts, session-replay-command.ts, lease-lifecycle.ts, remote-device-lease-contracts.md):
    • Restricted isSuiteAttemptSharedLease to keep an attempt lease active only when another live session in sessionStore.listRefs() still holds that leaseId, and dropped the flags.leaseId, internal.admittedLease, and suiteHostSession copies from closeReplayTestSession.
    • Added releaseUnheldSuiteRequestLease at the end of handleReplayTestCommand so any request-admitted lease not held by a live session and not already recorded in endedLeases ends once via releaseSessionLease and records { leaseId, outcome: 'released' } once via recordEndedLease (and updated remote-device-lease-contracts.md).
    • Updated the router tests in request-router-replay-scope.test.ts (including two-script no-open and mixed wait+open suites asserting endedLeases has one released entry, plus two-script outer-open lease retention) and session-close-lifecycle-teardown.test.ts.
  2. Covered host resolution simplification (request-execution-scope.ts, device-claim-admission.ts, device-claims.ts):
    • Resolved the live suiteHostSession once in createRequestExecutionScope and passed it through acquireTransientDeviceClaim's coveredBySession (keeping coveredClaimIsOwnedByRequest workspace verification) instead of the device-claim-admission.ts conflict fallback.

Validation (da7ac6be9, Windows 11 / Node v22.14.0)

  • oxfmt --check, pnpm typecheck, scripts/layering/check.ts (2040 files), oxlint . --deny-warnings (5429 files, 0 warnings/errors), and tsdown --config-loader native (711 files): passed.
  • Vitest (--project unit-core --testTimeout=30000 --maxWorkers=1, 9 suites: request-router-replay-scope.test.ts, session-replay-command.test.ts, session-close-lifecycle-teardown.test.ts, session-close-ended-lease.test.ts, request-ended-leases.test.ts, device-claim-conflict.test.ts, device-claim-admission.test.ts, device-claim-settlement.test.ts, proxy-lease-device-spec.test.ts): 126/126 assertions passed (run with --testTimeout=30000 on Windows because un-pinned process-lock.ts CIM queries hit the 2s PowerShell timeout per lock).
  • Live iOS simulator note: I am developing on Windows 11 with no Xcode or iOS Simulator host, so I could not run live open --platform ios + test first-launch.yaml --maestro --platform ios + snapshot/close or live connect proxy locally; those still require macOS CI approval or a macOS host check.

@thymikee

Copy link
Copy Markdown
Member

Thanks for the update.

At da7ac6b, the two lease copies are gone from the cleanup close request, so a request lease is no longer left rented by that shortcut. The code still needs one lease fix, and the live device run is still missing.

isSuiteAttemptSharedLease now keeps a lease only when another live session holds it. This likely breaks proxy test without open, and an SDK or gateway test with a lease in request metadata, when two attempts open the app (two Maestro flows with launchApp, or a retry after an attempt that opened). The first nested open stores the request lease on the attempt session. With no outer session, attempt cleanup ends that lease. The next nested open reuses the same lease id and should fail with UNAUTHORIZED "Lease is not active", so this half of #5129 still fails. The earlier advice to drop both copies led to this, because it also kept the per-attempt release. The rule: the suite ends the lease that the test request carries, once at suite end. Attempt cleanup ends only a lease that is neither the suite's admitted lease nor held by another live session. The smallest change is to pass the admitted lease id to the cleanup close in one internal field, such as suiteLeaseId, without restoring the flags.leaseId copy. Please add a router test with a request-only lease and two scripts that each open: assert passed 2, releaseCalls equal to [leaseId], and endedLeases equal to [{leaseId, outcome: 'released'}]. The lease contract sentence must be true on this route too.

A live run is still required before merge, because the PR changes claim admission, claim acquisition on open and attempt close teardown, and the tests use fake runtimes. The PR states that no macOS or iOS host was available, so a maintainer with a macOS host can supply it. After the lease fix, build with pnpm build and use node bin/agent-device.mjs on an iOS simulator in one session S: run open --platform ios, install <app>, then test <two flows that each launchApp> --maestro --platform ios. The output should show passed 2 and no DEVICE_IN_USE. Then show that device status still lists S as the claim owner, snapshot in S succeeds, and close in S releases the claim. If a proxy is available, also run connect proxy, then the same two-flow test without open, and paste passed 2 with exactly one endedLeases entry, {leaseId, outcome: 'released'}.

The four earlier inline threads are fixed and can be resolved: exclusion is now part of findByDevice; the process start time is pinned at module scope; the test now retains the lease through an outer session; and the duplicate borrow branch is gone.

CI is pending. The one reported check passed. The device and e2e lanes wait for fork approval, and they exercise the changed open, claim admission and attempt close routes.

Not verified here: no tests were executed, and the lease failure was traced in the code, not reproduced. The path from Maestro launchApp to a nested open with request metadata was inferred. It was also not checked whether the proxy client clears its stored lease id after endedLeases, or whether a thrown test command error skips the suite-end release.

Before merge, please fix the lease release with its two-script test, then add the live simulator evidence.

@MilindChatrabhuji

Copy link
Copy Markdown
Author

@thymikee Thank you for the review! Addressed every point in 64134f4e1e1e9421f8f2e31dc46032a8ced7bc1d:

  1. suiteLeaseId without restoring flags.leaseId (daemon-request.ts, session-close.ts, session-replay-command.ts): Passed suiteLeaseId internally from closeReplayTestSession into handleSessionCloseCommands (isSuiteAttemptSharedLease), so attempt 1's close cleanup preserves the request-scoped lease across two open scripts without mutating flags.leaseId.
  2. try / finally error-path release & proxy client endedLeases clearing:
    • Wrapped runReplayTestCommand in try { ... } finally { await releaseUnheldSuiteRequestLease(...); } and added unit tests in session-replay-command.test.ts verifying the unheld suite lease is still released cleanly into endedLeases both on cleanup failure and when setup throws inside try.
    • Verified in code: request-router.ts wraps suite dispatch in try { ... } finally { ... } inside reportEndedLeases, attaching endedLeases onto thrown error responses (error.details.endedLeases), and src/cli.ts calls forgetEndedConnectionLease (src/cli/connection/ended-lease.ts) for both data and error.details.
  3. Two-open-script router test (request-router-replay-scope.test.ts): Added two-script test with a request-only lease and two open scripts ends the lease once at suite end (#5129).
  4. Verification (64134f4e1e1e9421f8f2e31dc46032a8ced7bc1d, Windows 11 / Node v22.14.0):
    • oxfmt --check, tsc -b tsconfig.typecheck.json examples/sdk/tsconfig.json, scripts/layering/check.ts, oxlint . --deny-warnings (0 warnings/errors, 5,429 files), tsdown --config-loader native: all passed.
    • Vitest: ended-lease.test.ts (3/3), session-replay-command.test.ts (4/4), session-close-lifecycle-teardown.test.ts, session-close-ended-lease.test.ts, request-ended-leases.test.ts passed (17/17 across the 4 daemon files + 3/3 CLI); request-router-replay-scope.test.ts passed 11/11 assertions (all 3 #5129 two-script tests within the 2x slow-test gate; 3 pre-existing open tests exceeded the local 2.5s budget at 8.25s–12.33s).
    • Not verified locally: live iOS Simulator agent-device connect && agent-device test ... (Windows host; requires macOS CI/simulator).

@thymikee

Copy link
Copy Markdown
Member

Thanks for the update.

At 64134f4, the lease problem from the earlier review is addressed in the code: attempt cleanup now keeps the suite's request lease, and the suite releases it at the end. One new teardown problem on this route and the live device run remain.

The new suiteLeaseId exemption likely leaves an XCTest runner running after a proxy test without open, or an SDK or gateway test with a lease in request metadata. Attempt teardown now treats the lease as continuing, so on an iOS simulator the runner is retained and recorded under the request lease id. The suite-end release then ends the lease directly and skips lease-end session teardown, which cleans up retained hosts. A one-script test stopped the runner before this change. Now the runner can stay on a simulator the tenant no longer holds, until another session publishes on that device or the daemon exits. The rule: when a lease ends, its runner host and retained-host record are torn down too. Either send the suite-end release through the owner of lease-end teardown (endSessionsForLease), or use suiteLeaseId only to skip the provider release, so each attempt still stops the runner and only an outer session retains it. Please add a router or teardown test with an iOS simulator and the real retainExecutionHostAfterClose predicate, since the current router fixture always returns false. It should show that no retained host remains for the lease after the suite.

A live run is required before merge, because claim acquisition on open, claim admission, attempt close teardown and runner retention changed, the tests use fake runtimes, and no live run has been posted. A maintainer with a macOS host can build this head with pnpm build and use node bin/agent-device.mjs on an iOS simulator in one session S: open --platform ios, install <app>, then test <two flows that each launchApp> --maestro --platform ios. The output should show passed 2 and no DEVICE_IN_USE. After that, device status should still list S as the claim owner, snapshot in S should succeed, and close in S should release the claim. Over a proxy, run connect proxy, then the same two-flow test without open. It should show passed 2 and exactly one endedLeases entry, {leaseId, outcome: 'released'}, and the provider-side device should show no running agent-device runner afterwards.

The four earlier inline threads no longer apply and can be resolved: the exclusion is now part of findByDevice; the process start time is pinned at module scope; a parameterized retained-lease teardown test exists; and one shared claim-conflict check replaces the duplicate.

The Repo Guards & Compatibility check fails the Fallow complexity gate on createRequestExecutionScope. This is likely related, because this PR edits that function, but its complexity on the base branch was not checked. The cause of the Coverage failure is unclear, because the saved log ends before any failure line; this PR adds production branches that the job measures. The Body rules check was cancelled.

Not verified here: no tests were run, and the runner leak was traced in the code, not reproduced on a simulator. It was not checked whether a proxy provider recycles the simulator in a way that stops the runner anyway. It was also not confirmed that the new two-open router test fails without the new exemption, that a rejected test command always cleans up its attempt session before the suite-end release, or that the CLI clears its stored lease as the PR describes.

Before merge, please fix the lease-end teardown with its test, add the live simulator and proxy evidence, and get the complexity gate to pass.

@MilindChatrabhuji

Copy link
Copy Markdown
Author

@thymikee Thank you for catching the runner retention issue at 64134f4 and pointing out the Attempt #2 CI failures! Pushed 4f4253ece77b2a0ee0fe9b7413c045189242c4be to address everything:

  1. Simulator runner teardown without outer session (session-close.ts): Split isSuiteAttemptSharedLease into resolveSuiteAttemptLeaseRetention so suiteLeaseId skips provider lease release across suite attempts (skipProviderLeaseRelease) while leaseRetainedBySuite stays true only when another live session holds the lease (leaseRetainedByOtherSession). Without an outer live session and with !retainOnClose, leaseWillEnd is true on attempt close, retainExecutionHostAfterClose returns false, finalizeApplicationClose receives retainRunner: false, and no retained host entry is recorded in sessionStore.
  2. Real-predicate simulator tests (session-close-lifecycle-teardown.test.ts & request-router-replay-scope.test.ts):
    • Added suite-owned attempt close with suiteLeaseId and no outer session stops simulator runner while preserving lease (#5129) in session-close-lifecycle-teardown.test.ts using the iOS simulator predicate (asserts leaseWillEnd: true, retainExecutionHostAfterClose returns false, retainRunner: false, listRetainedExecutionHosts === [], and lease remains active).
    • Updated the request-router-replay-scope.test.ts router tests to inject the iOS simulator retainExecutionHostAfterClose predicate and assert leaseWillEnd: true, false returns x2, and sessionStore.listRetainedExecutionHosts(lease.leaseId) === [] on the request-lease suite (and true x2 while an outer session holds the device).
  3. CI Attempt Update README with correct GitHub link #2 fixes (Coverage & Fallow complexity):
    • Conditional suite-end release (session-replay-command.ts, session-replay-command.test.ts, request-router-replay-scope.test.ts, remote-device-lease-contracts.md): Tracked suiteRetainedLease via onRetainedSuiteLease inside closeReplayTestSession so releaseUnheldSuiteRequestLease in finally does not revoke unrelated caller leases when test rejects early or executes scripts that never bound a session to suiteLeaseId (fixing the LEASE_NOT_FOUND regression seen in http-server-batch-maestro-trust.test.ts and http-server-remote-output.test.ts).
    • Fallow complexity (request-execution-scope.ts): Extracted throwDaemonErrorResponse and resolveScopedExecutionRequest; Fallow audit now reports dead code 0 · complexity 0 · duplication 0 across all 20 changed files.
  4. Verification notes (Windows 11 / Node v22.14.0 at 4f4253ece77b2a0ee0fe9b7413c045189242c4be):
    • oxfmt --check (19 files), tsc -b tsconfig.typecheck.json examples/sdk/tsconfig.json, scripts/layering/check.ts (2040 files), oxlint . --deny-warnings (0 warnings, 0 errors across 5429 files), fallow audit --base 7d392e62143f932444f364681498dd7623be9004 (0 issues across 20 files), and tsdown --config-loader native (73.6s): all passed.
    • Vitest: 35/35 passed across the 6 fast daemon/CLI files (session-close-lifecycle-teardown, session-replay-command, session-close-ended-lease, request-ended-leases, cli/connection/ended-lease, platform-runtime-resource-cleanup-retention); http-server-batch-maestro-trust.test.ts passed 6/6 assertions under --testTimeout=15000 and request-router-replay-scope.test.ts passed 11/11 assertions under --testTimeout=25000 (both exited 1 on Windows slow-test duration checks); in http-server-remote-output.test.ts -t "remote suite artifacts" under --testTimeout=25000, the first and second RPC calls succeeded without LEASE_NOT_FOUND while the /artifacts/ download assertion at L930 hit an HTTP 500 on Windows.
    • Also verified in code: packages/replay-test/src/internal/session-test-runtime.ts catches runReplay rejections via .catch(...) and awaits cleanupSession(sessionName) in finally before returning, and src/cli.ts invokes forgetEndedConnectionLease on every remote daemon reply.
    • Not verified locally: Linux CI rerun (Coverage / http-server-remote-output.test.ts / Body rules) and live macOS/iOS simulator & proxy runs.

@thymikee

Copy link
Copy Markdown
Member

Thanks for the update.

At 4f4253e, the runner leak from the earlier review is fixed in the code: attempt close skips only the provider release for the suite lease, so the runner is retained only when another session holds that lease. The live device run is still missing.

A live run is required before merge. This PR changes claim admission, claim acquisition on open, attempt close teardown, runner retention and the suite-end lease release. All tests use fake runtimes, and no live run has been posted at any commit, so runner stop and restart between attempts and the claim handoff to the outer session are unproven on a real simulator and proxy. A maintainer with a macOS host can build this commit with pnpm build and use node bin/agent-device.mjs on an iOS simulator in one session S: open --platform ios, install <app>, then test <two flows that each launchApp> --maestro --platform ios. The output should show passed 2 and no DEVICE_IN_USE. After that, device status should still list S as the claim owner, snapshot in S should succeed, and close in S should release the claim. Over a proxy, run connect proxy, then the same two-flow test without open. It should show passed 2 and exactly one endedLeases entry, {leaseId, outcome: 'released'}, with no agent-device XCTest runner process left on the provider simulator afterwards.

The four earlier inline threads no longer apply and can be resolved: the exclusion is now a findByDevice parameter; the process start time is pinned at module scope; retained-lease teardown tests now exist; and the duplicate borrow branch is gone.

The one check reported on this commit passes. The earlier Coverage and Repo Guards (Fallow) failures were on 64134f4, and this update targets both. The device, e2e, Coverage and Repo Guards jobs are waiting for fork approval, and they exercise the changed open, claim and close routes.

Not verified here: no tests were run, no device was available, and the runner stop on attempt close was traced in the code, not observed. It is also unconfirmed that the http-server-batch-maestro-trust and http-server-remote-output LEASE_NOT_FOUND failures and the Fallow complexity gate now pass. A timed-out attempt that publishes a session with the suite lease after both the grace period and synchronous cleanup was not proven impossible; it was judged unlikely. The router tests use a copy of the iOS simulator retention predicate, so drift from the real one would not be caught.

Before merge, the live simulator and proxy evidence is needed, followed by passing results from the fork-approved jobs on this commit.

@thymikee

Copy link
Copy Markdown
Member

This branch now conflicts with main. Please merge or rebase onto the current main and push; the latest review still applies to the updated head, including the live device run it asks for.

@MilindChatrabhuji
MilindChatrabhuji force-pushed the fix/test-outer-session-device-5129 branch from 4f4253e to 6424574 Compare November 28, 2026 10:25
@MilindChatrabhuji

Copy link
Copy Markdown
Author

Rebased cleanly onto current main (e52cd29) at head 6424574 (packages/daemon/src/server/request-execution-scope.ts updated to use �pplyRequestCommandDefaults and keep queueLeaseEnd).

Local checks on Windows 11 (Node v22.14.0, head 6424574):

  • oxfmt --check (19 changed files), sc -b tsconfig.typecheck.json examples/sdk/tsconfig.json,
    ode --experimental-strip-types scripts/layering/check.ts (2048 files), oxlint . --deny-warnings (0 warnings, 0 errors across 5448 files), allow audit --base e52cd29 (dead code 0 · complexity 0 · duplication 0), sdown --config-loader native: all passed.
  • Vitest (--testTimeout=25000): 64/64 passed across the 6 targeted daemon/cli test files and 6/6 passed in http-server-batch-maestro-trust.test.ts (exiting non-zero only on the slow-test wall-clock gate). In http-server-remote-output.test.ts: 51 passed, 24 failed on Windows-specific EPERM symlink / file-identity / /artifacts/ checks (no LEASE_NOT_FOUND).
  • Live macOS / physical iOS device testing has not been run yet (testing from a Windows host). Could a maintainer approve CI workflows for 6424574 / validate on macOS?

@thymikee

Copy link
Copy Markdown
Member

Thanks for the update. At 6424574, the four earlier inline points are fixed, but the live run requested in the earlier review is still missing.

This PR changes claim admission, claim acquisition on open, attempt close teardown, provider release skipping (code), runner retention and the suite-end lease release. All tests use fake runtimes, and you confirmed that no live run was done at this commit. So the runner stop and restart between attempts, the claim handoff back to the outer session, and the absence of leaked leases or XCTest runners after tests end have not been seen on a real simulator or proxy. Before merge, a maintainer with a macOS host needs to build this commit (pnpm build) and use node bin/agent-device.mjs in one session S on an iOS simulator: open --platform ios, install <app>, then test <two flows that each launchApp> --maestro --platform ios. The run must show 2 passed and no DEVICE_IN_USE. After that, device status must still list S as the claim owner, snapshot in S must succeed, and close in S must release the claim. Over a proxy, run connect proxy, then the same two-flow test without open. It must show 2 passed, exactly one endedLeases entry {leaseId, outcome: 'released'}, and no agent-device XCTest runner process left on the provider simulator.

These threads no longer apply and can be resolved: this one, because the exclusion is now a findByDevice parameter and findByDeviceExcluding is gone; this one, because the process start time is pinned at module scope in request-router-replay-scope.test.ts; this one, because retained-lease teardown tests now exist, including the iOS simulator suite-lease case; and this one, because the duplicate borrow check is gone and isBorrowedSuiteHostClaimConflict is the only one.

The 3 reported checks pass at this commit.

Not verified here: tests were not run, and no device or proxy was available. Runner stop on attempt close and the suite-end release were only traced in the code. Results of the fork-gated device, e2e, Coverage and Repo Guards jobs, which exercise the changed open, claim and close routes, were not available. Coverage and Fallow results are unconfirmed beyond your local Windows report. It was not checked whether dropping the no-open proxy route breaks any test or caller outside this PR; PUBLIC_COMMANDS.test in proxyLeaseAllocatingCommands is the only CLI-side change. A timed-out attempt that publishes a session with the suite lease after cleanup was earlier judged unlikely, but not proven impossible.

The next step before merge is the live iOS simulator and proxy evidence above. Separately, should the no-open proxy route and its suiteLeaseId/suiteRetainedLease state stay, or should the PR narrow to claim borrowing plus leaseRetainedByOtherSession? That is the maintainer's decision.

@thymikee

Copy link
Copy Markdown
Member

Thanks for sticking with this through several rounds. #5129 is a real bug: open, then install, then test --maestro fails with DEVICE_IN_USE, and it should be fixed. The current change is several times larger than that bug needs, though, and the maintainers want to keep daemon lease and session state from growing for it. Please narrow the PR to the following scope. That is the bar for merge.

In scope (the whole fix):

  • A Maestro test attempt session (<session>:test:<id>:<n>-<flow>:attempt-<k>) may use the device claim held by its own parent session <session>. Keep this as a narrow check in claim admission and conflict handling (device-claim-admission.ts, device-claim-conflict.ts), based on the parent relation the attempt session already has.
  • When an attempt session closes, it releases only what it acquired. It must never release or end the parent session's claim.
  • Tests for these cases: an attempt borrows its parent's claim; an attempt is still refused a device held by an unrelated session; the parent keeps its claim after the attempts close.

Out of scope (please remove):

  • suiteLeaseId, suiteRetainedLease and any other new lease-retention state.
  • The proxy route that runs test without a prior open, along with its proxy lease changes. If that route is needed, it belongs in its own issue.
  • Refactors and helper extractions that the fix above does not need.

Expectations:

  • Production changes should land at roughly 30–60 lines, with no new daemon concepts.
  • One live run on an iOS simulator at the head commit, built with pnpm build and run with node bin/agent-device.mjs, in one session: open --platform ios, install <app>, then test <a flow that runs launchApp> --maestro --platform ios. It must show the flow passing with no DEVICE_IN_USE. After that, snapshot in the same session succeeds and close releases the claim. Please paste the commands, device and outputs, with the head SHA, into the PR description.
  • Rebasing onto main and force-pushing the narrowed version is fine.

The next review will check only these points.

@thymikee

Copy link
Copy Markdown
Member

@MilindChatrabhuji, this branch now conflicts with main at 6424574. Please rebase it onto current main. The open items in the last review still apply, and the next review will check the rebased head against them.

@thymikee

Copy link
Copy Markdown
Member

@MilindChatrabhuji, thanks for the work here. Closing this version: it still adds suite-level lease state and a proxy route for test without open, and Maestro support is a low priority for the maintainers, so daemon lease and session growth for it is out of scope. A replacement is welcome if it stays narrow and covers only the reported conflict: a test attempt borrows its own parent session's device claim, rejects an unrelated owner, and leaves the parent able to snapshot and close afterwards, with no new daemon lease or session concepts.

@thymikee thymikee closed this Nov 30, 2026
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.

2 participants