Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions COMPATIBILITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 13 additions & 2 deletions src/E2E.Limrun/LimrunDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,7 +30,8 @@ internal static class LimrunDriver
public static async Task<LimrunBinding> 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(
Expand Down Expand Up @@ -69,7 +72,7 @@ public static async Task<LimrunBinding> 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(
Expand All @@ -83,6 +86,14 @@ public static async Task<LimrunBinding> AttachAsync(LimrunDriverRequest request,
return binding with { InstalledApp = id };
}

/// <summary>
/// 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 <c>client</c>. The port cannot, because <c>session</c> is reserved there for
/// the engine (see <c>DeviceBindings</c>), so the heartbeat derives it from the same state directory.
/// </summary>
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}");

Expand Down
5 changes: 4 additions & 1 deletion src/E2E.Limrun/LimrunHeartbeat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public static async Task<Func<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<string>(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)
Expand Down
2 changes: 1 addition & 1 deletion src/E2E.Mobile/AgentDeviceRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public AgentDeviceRpcClient(string session, JsonObject? settings, Func<Cancellat
JsonObject parameters;
if (lease)
{
parameters = DeviceCommands.LeaseParameters(command, _settings);
parameters = DeviceCommands.LeaseParameters(command, _settings, _session);
}
else
{
Expand Down
7 changes: 4 additions & 3 deletions src/E2E.Mobile/DeviceCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ public static DeviceCommand Translate(string method, JsonObject? options)
/// The params of a lease request, as the client's lease writer sends them. Each field is the call's option, else the
/// client configuration's: <c>tenant</c> is <c>tenantId</c> on the wire and <c>leaseBackend</c> is <c>backend</c>.
/// Allocate carries the lease's life (<c>ttlMs</c>, <c>retainOnClose</c>) and its backend, heartbeat and release the
/// lease id, heartbeat also <c>ttlMs</c>. No session is named, so the daemon takes <c>default</c>, as it does for the
/// upstream client the engine hands no session to.
/// lease id, heartbeat also <c>ttlMs</c>. Each names the client's <paramref name="session"/>, which agent-device
/// requires of a provider lease.
/// </summary>
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];

Expand All @@ -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"));
Expand Down
2 changes: 1 addition & 1 deletion src/E2E.Mobile/Lease/LeaseDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static class LeaseDriver
{
public static async Task<DriverGrant> 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(
Expand Down
6 changes: 5 additions & 1 deletion src/E2E.Mobile/Lease/LeaseHeartbeat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ public static async Task<Func<Task>> StartAsync(
? id
: throw new InvalidOperationException("The lease scope has no lease id");

var stateDirectory = scope["stateDir"] is JsonValue directory && directory.TryGetValue<string>(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)
Expand Down
19 changes: 19 additions & 0 deletions src/E2E.Mobile/Lease/LeaseSessions.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>The session a lease's daemon names (upstream's <c>session.ts</c>).</summary>
internal static class LeaseSessions
{
/// <summary>
/// The session a lease's daemon allocates and renews the lease under (upstream's <c>leaseSession</c>). agent-device refuses a
/// provider lease to a client that names no session, and the daemon in <paramref name="stateDirectory"/> holds one lease.
/// </summary>
public static string LeaseSession(string stateDirectory) =>
$"e2e-lease-{Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(stateDirectory)))[..8]}";
}
18 changes: 15 additions & 3 deletions tests/E2E.Limrun.Tests/Driver/LimrunDriverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

namespace E2E.Limrun.Tests.Driver;

/// <summary>The driver against a scripted agent-device client. Ports <c>packages/limrun/tests/unit/driver.test.ts</c>.</summary>
/// <summary>
/// The driver against a scripted agent-device client. Ports <c>packages/limrun/tests/unit/driver.test.ts</c>. The first test is partial:
/// upstream asserts that the binding's <c>client</c> carries the lease session, and the port keeps <c>session</c> out of a binding's client
/// (the engine reserves it), so the test asserts the session the driver's clients are created with.
/// </summary>
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
Expand All @@ -24,15 +28,23 @@ public async Task Uses_the_requested_app_when_installation_returns_no_identifier
["leaseId"] = "lease",
["backend"] = "ios-instance",
});
var sessions = new List<string>();
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.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}""",
client.LastArgs(DeviceMethods.LeasesAllocate));
Expand Down
5 changes: 3 additions & 2 deletions tests/E2E.Limrun.Tests/Heartbeat/LimrunHeartbeatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>()));
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);
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion tests/E2E.Mobile.Tests/LeaseDriver/LeaseDriverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@ 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;
},
CancellationToken.None);

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));
}

Expand Down
3 changes: 2 additions & 1 deletion tests/E2E.Mobile.Tests/LeaseHeartbeat/LeaseHeartbeatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>()));
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);
Expand Down
Loading