From a0e3adcb92a82c4e43de7ed74e0b6f4390c7a061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Tue, 3 Nov 2026 10:27:07 -0300 Subject: [PATCH 1/3] Name the agent-device session of a device-cloud lease (upstream tester-army/e2e#1513) Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Ls2e1yaZFgZ3FEJkYScfkK --- COMPATIBILITY.md | 1 + src/E2E.Limrun/LimrunDriver.cs | 15 +++++++++++++-- src/E2E.Limrun/LimrunHeartbeat.cs | 5 ++++- src/E2E.Mobile/AgentDeviceRpcClient.cs | 2 +- src/E2E.Mobile/DeviceCommands.cs | 7 ++++--- src/E2E.Mobile/Lease/LeaseDriver.cs | 2 +- src/E2E.Mobile/Lease/LeaseHeartbeat.cs | 6 +++++- src/E2E.Mobile/Lease/LeaseSessions.cs | 19 +++++++++++++++++++ .../Driver/LimrunDriverTests.cs | 17 ++++++++++++++--- .../Heartbeat/LimrunHeartbeatTests.cs | 5 +++-- .../LeaseDriver/LeaseDriverTests.cs | 5 ++++- .../LeaseHeartbeat/LeaseHeartbeatTests.cs | 3 ++- 12 files changed, 71 insertions(+), 16 deletions(-) create mode 100644 src/E2E.Mobile/Lease/LeaseSessions.cs diff --git a/COMPATIBILITY.md b/COMPATIBILITY.md index fddc67dda..c0cb02c0a 100644 --- a/COMPATIBILITY.md +++ b/COMPATIBILITY.md @@ -397,6 +397,7 @@ The port installs the skill with `npx skills add hardkoded/e2e-dotnet` and regis - Upstream runs the allocation in a child process (`driver.ts`) so each daemon gets its own environment. The port starts the daemon itself with that environment (`AgentDeviceDaemon.LocalAsync`, which already runs agent-device as its own process) and calls `leases.allocate` from the runner, so `LeaseDriver` has no child process. The allocation gets a 15-minute bound the daemon's own deadline also enforces. The shutdown report of `daemon stop --json` comes from `AgentDeviceDaemon.StopReportAsync` (internal; `StopAsync` is unchanged) and goes through `AssertSessionsEnded` as upstream's does: a release fails unless agent-device lists the granted lease as released, and a retried release only cleans up once the stop confirmed. - The allocate request of the port's client now carries the device selectors (`platform`, `target`, `device`, the `provider*` capabilities, `osVersion`, `imageTag`, `physical`) beside the lease fields, as agent-device's client spreads them into the params (read from agent-device 0.21.35). Limrun sends none, so its requests are unchanged. - The helper checks that the engine asked for the pinned agent-device (`AgentDeviceVersion.Pinned`), as `LimrunSimulators` does, because the daemon it starts is that release. Upstream's testmu provider checks its plugin against the version instead, and upstream's BrowserStack provider checks nothing. +- A lease is allocated and renewed under a named session (upstream's `leaseSession`, `e2e-lease-` and the first 8 hex digits of the SHA-256 of the state directory), because agent-device refuses a provider lease to a client that names none. The lease requests of the port's client now carry the client's session as `session` (read from agent-device 0.21.36's lease writer). Upstream sets it on the client configuration and, for Limrun, also on the binding's `client`. The port passes it as the client's session instead, and keeps it out of a binding's `client`, where `session` is reserved for the engine (`DeviceBindings`). `LimrunHeartbeat` derives it again from the binding's `stateDir`. The named session is not checked against a live provider. - Options are the `BrowserStackDevicesOptions` record, with an enum for `Orientation` (`BrowserStackOrientation`), so an unknown option and a wrong type are compile errors and upstream's `rejectUnknownKeys` has no counterpart. `BrowserStackDevices` is the name of upstream's `browserstack()` (the port names a provider class, as `LimrunSimulators` is `limrun()`), and `BrowserStackDevicesOptions` is `BrowserStackOptions`. - The request's cancellation is a `CancellationToken`. A cancel stays an `OperationCanceledException`, also inside the API lookups, where upstream throws an `Error` saying "cancelled". A request that gets no answer within its bound is a `TimeoutException`. Errors name the build and session as upstream's do. - The credentials reach the daemon's environment and nothing else: the lease's `Client`, the log lines, the errors, and `BrowserStackCredentials.ToString()` leave out the access key (a test checks the first three; the last is .NET-only, because a record prints its members). `E2E.Cli` does not reference `E2E.BrowserStack`, so `e2e explore` and `e2e mcp` cannot lease a BrowserStack device, and nothing registers the key as a `Secret`: upstream's CLI loads the provider from the config, and the port's tool ships only Limrun and DevFlow (see [Mobile engine](#mobile-engine)). `E2E_MOBILE_PROVIDER` does not name `browserstack` either; set the provider in code, as for EAS. diff --git a/src/E2E.Limrun/LimrunDriver.cs b/src/E2E.Limrun/LimrunDriver.cs index 132621887..e177874da 100644 --- a/src/E2E.Limrun/LimrunDriver.cs +++ b/src/E2E.Limrun/LimrunDriver.cs @@ -2,6 +2,8 @@ // Modified by Dario Kondratiuk. // SPDX-License-Identifier: Apache-2.0 +using System.Security.Cryptography; +using System.Text; using System.Text.Json.Nodes; using E2E.Mobile; @@ -28,7 +30,8 @@ internal static class LimrunDriver public static async Task AttachAsync(LimrunDriverRequest request, DeviceClientFactory connect, CancellationToken cancellationToken) { var platform = request.Platform.WireName(); - var allocator = connect("default", new DeviceConnection { Client = new JsonObject { ["stateDir"] = request.StateDirectory } }); + var session = LeaseSession(request.StateDirectory); + var allocator = connect(session, new DeviceConnection { Client = new JsonObject { ["stateDir"] = request.StateDirectory } }); var lease = await allocator.CallAsync( DeviceMethods.LeasesAllocate, DeviceArgs.Of( @@ -69,7 +72,7 @@ public static async Task AttachAsync(LimrunDriverRequest request, return binding; } - var device = connect("default", new DeviceConnection { Client = client }); + var device = connect(session, new DeviceConnection { Client = client }); var installed = await device.CallAsync( DeviceMethods.AppsInstall, DeviceArgs.Of( @@ -83,6 +86,14 @@ public static async Task AttachAsync(LimrunDriverRequest request, return binding with { InstalledApp = id }; } + /// + /// The session the driver allocates and renews the lease under: agent-device refuses a provider lease to a client that names no + /// session. Upstream carries it in the binding's client. The port cannot, because session is reserved there for + /// the engine (see DeviceBindings), so the heartbeat derives it from the same state directory. + /// + internal static string LeaseSession(string stateDirectory) => + $"e2e-lease-{Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(stateDirectory)))[..8]}"; + private static string Required(JsonObject lease, string name) => Text(lease[name]) ?? throw new InvalidOperationException($"Invalid lease response from daemon: no {name}"); diff --git a/src/E2E.Limrun/LimrunHeartbeat.cs b/src/E2E.Limrun/LimrunHeartbeat.cs index 6154e4d65..3b40f01e4 100644 --- a/src/E2E.Limrun/LimrunHeartbeat.cs +++ b/src/E2E.Limrun/LimrunHeartbeat.cs @@ -31,7 +31,10 @@ public static async Task> StartAsync( : throw new InvalidOperationException("Limrun driver returned no agent-device lease id"); // The lease's own stateDir selects the local daemon, whatever remote daemon the environment names. - var client = connect("default", new DeviceConnection { Client = binding.Client }); + var stateDirectory = binding.Client["stateDir"] is JsonValue directory && directory.TryGetValue(out var path) && path.Length > 0 + ? path + : throw new InvalidOperationException("Limrun driver returned no state directory"); + var client = connect(LimrunDriver.LeaseSession(stateDirectory), new DeviceConnection { Client = binding.Client }); var stopping = new CancellationTokenSource(); async Task BeatAsync(CancellationToken outer) diff --git a/src/E2E.Mobile/AgentDeviceRpcClient.cs b/src/E2E.Mobile/AgentDeviceRpcClient.cs index d977e13d4..bc95edbac 100644 --- a/src/E2E.Mobile/AgentDeviceRpcClient.cs +++ b/src/E2E.Mobile/AgentDeviceRpcClient.cs @@ -71,7 +71,7 @@ public AgentDeviceRpcClient(string session, JsonObject? settings, Functenant is tenantId on the wire and leaseBackend is backend. /// Allocate carries the lease's life (ttlMs, retainOnClose) and its backend, heartbeat and release the - /// lease id, heartbeat also ttlMs. No session is named, so the daemon takes default, as it does for the - /// upstream client the engine hands no session to. + /// lease id, heartbeat also ttlMs. Each names the client's , which agent-device + /// requires of a provider lease. /// - public static JsonObject LeaseParameters(DeviceCommand command, JsonObject settings) + public static JsonObject LeaseParameters(DeviceCommand command, JsonObject settings, string session) { JsonNode? Field(string name) => command.Flags[name] ?? settings[name]; @@ -131,6 +131,7 @@ void Put(string key, JsonNode? value) } } + parameters["session"] = session; Put("tenantId", Field("tenant")); Put("runId", Field("runId")); Put("leaseProvider", Field("leaseProvider")); diff --git a/src/E2E.Mobile/Lease/LeaseDriver.cs b/src/E2E.Mobile/Lease/LeaseDriver.cs index 081c2c98d..ae05f05ea 100644 --- a/src/E2E.Mobile/Lease/LeaseDriver.cs +++ b/src/E2E.Mobile/Lease/LeaseDriver.cs @@ -22,7 +22,7 @@ internal static class LeaseDriver { public static async Task AllocateAsync(DriverRequest request, DeviceClientFactory connect, CancellationToken cancellationToken) { - var client = connect("default", new DeviceConnection { Client = new JsonObject { ["stateDir"] = request.StateDirectory } }); + var client = connect(LeaseSessions.LeaseSession(request.StateDirectory), new DeviceConnection { Client = new JsonObject { ["stateDir"] = request.StateDirectory } }); var lease = await client.CallAsync(DeviceMethods.LeasesAllocate, (JsonObject)request.Allocate.DeepClone(), cancellationToken).ConfigureAwait(false) as JsonObject ?? throw new InvalidOperationException("agent-device returned no lease"); return new DriverGrant( diff --git a/src/E2E.Mobile/Lease/LeaseHeartbeat.cs b/src/E2E.Mobile/Lease/LeaseHeartbeat.cs index 9fb50fc9a..23041014d 100644 --- a/src/E2E.Mobile/Lease/LeaseHeartbeat.cs +++ b/src/E2E.Mobile/Lease/LeaseHeartbeat.cs @@ -33,8 +33,12 @@ public static async Task> StartAsync( ? id : throw new InvalidOperationException("The lease scope has no lease id"); + var stateDirectory = scope["stateDir"] is JsonValue directory && directory.TryGetValue(out var path) && path.Length > 0 + ? path + : throw new InvalidOperationException("The lease scope has no state directory"); + // The lease's own stateDir selects the local daemon, whatever remote daemon the environment names. - var client = connect("default", new DeviceConnection { Client = scope }); + var client = connect(LeaseSessions.LeaseSession(stateDirectory), new DeviceConnection { Client = scope }); var stopping = new CancellationTokenSource(); async Task BeatAsync(CancellationToken outer) diff --git a/src/E2E.Mobile/Lease/LeaseSessions.cs b/src/E2E.Mobile/Lease/LeaseSessions.cs new file mode 100644 index 000000000..ef7408939 --- /dev/null +++ b/src/E2E.Mobile/Lease/LeaseSessions.cs @@ -0,0 +1,19 @@ +// Copyright 2026 TesterArmy. +// Modified by Dario Kondratiuk. +// SPDX-License-Identifier: Apache-2.0 + +using System.Security.Cryptography; +using System.Text; + +namespace E2E.Mobile.Lease; + +/// The session a lease's daemon names (upstream's session.ts). +internal static class LeaseSessions +{ + /// + /// The session a lease's daemon allocates and renews the lease under (upstream's leaseSession). agent-device refuses a + /// provider lease to a client that names no session, and the daemon in holds one lease. + /// + public static string LeaseSession(string stateDirectory) => + $"e2e-lease-{Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(stateDirectory)))[..8]}"; +} diff --git a/tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs b/tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs index 6b208ccd1..3cfedf8c3 100644 --- a/tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs +++ b/tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs @@ -7,13 +7,17 @@ namespace E2E.Limrun.Tests.Driver; -/// The driver against a scripted agent-device client. Ports packages/limrun/tests/unit/driver.test.ts. +/// +/// The driver against a scripted agent-device client. Ports packages/limrun/tests/unit/driver.test.ts. The first test is partial: +/// upstream asserts that the binding's client carries the lease session, and the port keeps session out of a binding's client +/// (the engine reserves it), so the test asserts the session the driver's clients are created with. +/// public sealed class LimrunDriverTests { [Theory] [InlineData(MobilePlatform.Ios)] [InlineData(MobilePlatform.Android)] - public async Task Uses_the_requested_app_when_installation_returns_no_identifier(MobilePlatform platform) + public async Task Partial_Uses_the_requested_app_when_installation_returns_no_identifier(MobilePlatform platform) { var client = new FakeDeviceClient(); client.Respond(DeviceMethods.LeasesAllocate, _ => new JsonObject @@ -24,15 +28,22 @@ public async Task Uses_the_requested_app_when_installation_returns_no_identifier ["leaseId"] = "lease", ["backend"] = "ios-instance", }); + var sessions = new List(); client.Respond(DeviceMethods.AppsInstall, _ => new JsonObject()); var binding = await LimrunDriver.AttachAsync( new LimrunDriverRequest("/daemon", "run", 0, platform, "com.example.app", "/build"), - (_, _) => client, + (named, _) => + { + sessions.Add(named); + return client; + }, CancellationToken.None); Assert.Equal("com.example.app", binding.InstalledApp); Assert.Equal($"limrun:{platform.WireName()}:lease", binding.DeviceId); + Assert.All(sessions, named => Assert.Matches("^e2e-lease-[0-9a-f]{8}$", named)); + Assert.Single(sessions.Distinct()); JsonAssert.Args( $$"""{"tenant":"e2e","runId":"run","clientId":"worker-0","leaseBackend":"{{platform.WireName()}}-instance","leaseProvider":"limrun","retainOnClose":true}""", client.LastArgs(DeviceMethods.LeasesAllocate)); diff --git a/tests/E2E.Limrun.Tests/Heartbeat/LimrunHeartbeatTests.cs b/tests/E2E.Limrun.Tests/Heartbeat/LimrunHeartbeatTests.cs index 24d3a52c7..076e3f40c 100644 --- a/tests/E2E.Limrun.Tests/Heartbeat/LimrunHeartbeatTests.cs +++ b/tests/E2E.Limrun.Tests/Heartbeat/LimrunHeartbeatTests.cs @@ -72,7 +72,8 @@ public async Task Renews_an_idle_scoped_lease_beyond_its_expiry_window_and_stops await clock.AdvanceAsync(TimeSpan.FromSeconds(140)); Assert.Equal(8, daemon.Requests.Count); Assert.All(daemon.Requests, request => Assert.Equal("agent_device.lease.heartbeat", request["method"]!.GetValue())); - JsonAssert.Args("""{"tenantId":"e2e","runId":"run","leaseId":"lease-2","ttlMs":60000}""", daemon.Requests[^1]["params"]); + Assert.Matches("^e2e-lease-[0-9a-f]{8}$", config.Session); + JsonAssert.Args($$"""{"session":"{{config.Session}}","tenantId":"e2e","runId":"run","leaseId":"lease-2","ttlMs":60000}""", daemon.Requests[^1]["params"]); await stop(); await clock.AdvanceAsync(TimeSpan.FromSeconds(140)); Assert.Equal(8, daemon.Requests.Count); @@ -109,7 +110,7 @@ public async Task Reports_transient_renewal_failures_retries_and_cancels_an_in_f } }); var stop = await LimrunHeartbeat.StartAsync( - new LimrunBinding(new JsonObject { ["leaseId"] = "lease" }, "device"), + new LimrunBinding(new JsonObject { ["stateDir"] = "/daemon", ["leaseId"] = "lease" }, "device"), (_, _) => client, log.Add, clock.Delay, diff --git a/tests/E2E.Mobile.Tests/LeaseDriver/LeaseDriverTests.cs b/tests/E2E.Mobile.Tests/LeaseDriver/LeaseDriverTests.cs index a05d27855..2e8ccfbd9 100644 --- a/tests/E2E.Mobile.Tests/LeaseDriver/LeaseDriverTests.cs +++ b/tests/E2E.Mobile.Tests/LeaseDriver/LeaseDriverTests.cs @@ -24,11 +24,13 @@ public async Task Allocates_from_the_daemon_in_its_state_directory_and_reports_t var client = new FakeDeviceClient(); client.Respond(DeviceMethods.LeasesAllocate, _ => new JsonObject { ["leaseId"] = "lease", ["tenantId"] = "browserstack", ["runId"] = "run", ["backend"] = "ios-instance", ["retainOnClose"] = true }); DeviceConnection? connection = null; + string? session = null; var grant = await Mobile.Lease.LeaseDriver.AllocateAsync( new DriverRequest("/daemon", Allocate), - (_, config) => + (named, config) => { + session = named; connection = config; return client; }, @@ -36,6 +38,7 @@ public async Task Allocates_from_the_daemon_in_its_state_directory_and_reports_t Assert.Equal(new DriverGrant("lease", "browserstack", "run", true), grant); JsonAssert.Args("""{"stateDir":"/daemon"}""", connection!.Client); + Assert.Matches("^e2e-lease-[0-9a-f]{8}$", session); JsonAssert.Args("""{"tenant":"browserstack","runId":"run","leaseProvider":"browserstack","retainOnClose":true}""", client.LastArgs(DeviceMethods.LeasesAllocate)); } diff --git a/tests/E2E.Mobile.Tests/LeaseHeartbeat/LeaseHeartbeatTests.cs b/tests/E2E.Mobile.Tests/LeaseHeartbeat/LeaseHeartbeatTests.cs index 75292dc6d..76dfd339f 100644 --- a/tests/E2E.Mobile.Tests/LeaseHeartbeat/LeaseHeartbeatTests.cs +++ b/tests/E2E.Mobile.Tests/LeaseHeartbeat/LeaseHeartbeatTests.cs @@ -62,13 +62,14 @@ public async Task Renews_the_lease_on_the_leases_own_daemon_until_stopped() CancellationToken.None); var config = Assert.Single(created); + Assert.Matches("^e2e-lease-[0-9a-f]{8}$", config.Session); Assert.Equal("", config.DaemonBaseUrl); Assert.Equal("", config.DaemonAuthToken); Assert.Equal("auto", config.DaemonTransport); await clock.AdvanceAsync(TimeSpan.FromSeconds(140)); Assert.Equal(8, daemon.Requests.Count); Assert.All(daemon.Requests, request => Assert.Equal("agent_device.lease.heartbeat", request["method"]!.GetValue())); - JsonAssert.Args("""{"tenantId":"browserstack","runId":"run","leaseProvider":"browserstack","leaseId":"lease-2","ttlMs":60000}""", daemon.Requests[^1]["params"]); + JsonAssert.Args($$"""{"session":"{{config.Session}}","tenantId":"browserstack","runId":"run","leaseProvider":"browserstack","leaseId":"lease-2","ttlMs":60000}""", daemon.Requests[^1]["params"]); await stop(); await clock.AdvanceAsync(TimeSpan.FromSeconds(140)); Assert.Equal(8, daemon.Requests.Count); From 0b4eaddd2bb98ebfe95933615fad15cecd6e0cc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Tue, 3 Nov 2026 10:29:17 -0300 Subject: [PATCH 2/3] Check the Limrun driver created at least one client Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Ls2e1yaZFgZ3FEJkYScfkK --- tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs b/tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs index 3cfedf8c3..9c3be0871 100644 --- a/tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs +++ b/tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs @@ -42,7 +42,8 @@ public async Task Partial_Uses_the_requested_app_when_installation_returns_no_id Assert.Equal("com.example.app", binding.InstalledApp); Assert.Equal($"limrun:{platform.WireName()}:lease", binding.DeviceId); - Assert.All(sessions, named => Assert.Matches("^e2e-lease-[0-9a-f]{8}$", named)); + Assert.NotEmpty(sessions); + Assert.All(sessions,named => Assert.Matches("^e2e-lease-[0-9a-f]{8}$", named)); Assert.Single(sessions.Distinct()); JsonAssert.Args( $$"""{"tenant":"e2e","runId":"run","clientId":"worker-0","leaseBackend":"{{platform.WireName()}}-instance","leaseProvider":"limrun","retainOnClose":true}""", From 5fd3ae920a04b1ac7ff25b01fb571c4f1b577060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Kondratiuk?= Date: Tue, 3 Nov 2026 10:30:50 -0300 Subject: [PATCH 3/3] Fix whitespace Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01Ls2e1yaZFgZ3FEJkYScfkK --- tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs b/tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs index 9c3be0871..8b973d61b 100644 --- a/tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs +++ b/tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs @@ -43,7 +43,7 @@ public async Task Partial_Uses_the_requested_app_when_installation_returns_no_id Assert.Equal("com.example.app", binding.InstalledApp); Assert.Equal($"limrun:{platform.WireName()}:lease", binding.DeviceId); Assert.NotEmpty(sessions); - Assert.All(sessions,named => Assert.Matches("^e2e-lease-[0-9a-f]{8}$", named)); + Assert.All(sessions, named => Assert.Matches("^e2e-lease-[0-9a-f]{8}$", named)); Assert.Single(sessions.Distinct()); JsonAssert.Args( $$"""{"tenant":"e2e","runId":"run","clientId":"worker-0","leaseBackend":"{{platform.WireName()}}-instance","leaseProvider":"limrun","retainOnClose":true}""",