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
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ An agent-device lease and its simlock lease share one id (the CLI chooses
it; ADR 0020 lets simlock keep it) and one lifetime.

- `allocate` becomes `lease.request` as the agent's principal, with the
CLI's selectors mapped to `platform`, `model` or `class`, `osVersion` and `imageTag`. A request
CLI's selectors mapped to `platform`, `model` or `class`, `osVersion`, `imageTag` and `baseline` (its `deviceBaseline`). A request
that waits in simlock's queue holds the CLI's request open. When the CLI
hangs up, the front cancels it; a grant that lands after that is released
at once.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ mode, image tag, note, size, saved time and stale reason.

A device record carries the baseline name and version it was copied from,
or none. A request carries an optional `baseline` name beside its required
`platform` (CLI `--baseline`, `SIMLOCK_BASELINE`, HTTP, MCP, the client).
`platform` (CLI `--baseline`, `SIMLOCK_BASELINE`, HTTP, MCP, the client, agent-device's `deviceBaseline`).
`fits` and `sameSpec` compare it like the image tag: unset matches only
unset, a name matches only that name. A request naming a baseline takes the
baseline's model, OS, mode and image tag; a request whose platform, model,
Expand All @@ -218,8 +218,9 @@ OS, mode or image tag differs from the baseline's fails at once
stale. One function decides a mismatch, and the worker and the gateway both
call it. A device of an older version than the baseline's
current one is never granted. A grant and `lease.granted` carry the
baseline name and version. agent-device does not send a baseline yet; that
is a later change on both sides.
baseline name and version. The agent-device front maps `deviceBaseline` to
`baseline`, sends no class beside it, and adds the baseline to the reply's
`data.lease.device`.

A learned warm kind carries the baseline name, so the pool keeps baseline
devices warm with no configuration.
Expand Down
10 changes: 5 additions & 5 deletions src/agent-device/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ under leasing's signal (`lease.endWaitMs`).
## Flow: agent on a worker

1. CLI `open` → `POST /v1/agent-device/rpc` `agent_device.lease.allocate` with its lease id.
2. Front refuses a non-plain lease, no `--platform`, a bad `ttlMs`, no lease id, a `physical` that is not a boolean, or a `--target` it does not map.
2. Front refuses a non-plain lease, no `--platform`, a bad `ttlMs`, no lease id, a `physical` that is not a boolean, a `deviceBaseline` that is not text, or a `--target` it does not map.
3. `lease.request`: `leaseId` = `requesterId` = the CLI's lease id; `--device` → model,
`--target` → class, `--os` → `osVersion`, `--image-tag` → `imageTag`, `physical: true` → `physical: true` (Simlock picks an enrolled phone); TTL = sent, capped by `capabilities.agent-device.leaseTtlCapMs`, at least 5 s;
`--target` → class, `--os` → `osVersion`, `--image-tag` → `imageTag`, `deviceBaseline` → `baseline` (and then no class from `--target`), `physical: true` → `physical: true` (Simlock picks an enrolled phone); TTL = sent, capped by `capabilities.agent-device.leaseTtlCapMs`, at least 5 s;
queue wait 330 s. CLI hangs up → `lease.cancel`, retried every 1 s.
4. Grant → allocate forwarded with the device address (`udid`/`serial`; a phone's is its enrolled id), to the phone daemon for a phone and the simulator daemon otherwise (`RoutedHostLink`, which remembers each lease's daemon until the lease ends), no
`device`/`deviceKey`/`target`/`osVersion`/`imageTag`/`physical` (commands lose both keys too), `ttlMs` = max(24 h, 2 × `lease.maxTtlMs`).
5. agent-device error → simlock lease released. Success → front map entry; reply relayed with `data.lease.device` `{ model, osVersion, physical, imageTag? }` added from the grant.
`device`/`deviceKey`/`target`/`osVersion`/`imageTag`/`deviceBaseline`/`physical` (commands lose `osVersion`, `imageTag` and `deviceBaseline` too), `ttlMs` = max(24 h, 2 × `lease.maxTtlMs`).
5. agent-device error → simlock lease released. Success → front map entry; reply relayed with `data.lease.device` `{ model, osVersion, physical, imageTag?, baseline? { name, version } }` added from the grant.
6. Commands carry the lease id; rules check, renewals run, `devices` filtered.
7. Release → agent-device first, then `lease.release` → the device is deleted; lease-end finds no scope left.

Expand Down Expand Up @@ -140,7 +140,7 @@ Local profile: a standalone worker with HTTP and this module on keeps `<data dir
- A pid is signalled only after its role's whole marker matches (`--simlock-home`, and `--simlock-role=phones` for the phone daemon).
- The edge names no gateway type; the gateway side builds `FleetPort` from its gateway context.
- A daemon stop cancels waiting allocates (`cancelWaitingAllocates`) and stops renewals.
- Tests: `edge.test.ts`, `gateway-front.test.ts`, `front/*.test.ts`, `host/*.test.ts`.
- Tests: `edge.test.ts`, `baseline.test.ts` (both on the rig in `test-support.ts`), `gateway-front.test.ts`, `front/*.test.ts`, `host/*.test.ts`.

## Known gaps

Expand Down
180 changes: 180 additions & 0 deletions src/agent-device/baseline.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
import { describe, expect, it } from "vitest";

import {
adminSession,
allocate,
allocatesReceived,
androidDriver,
envelopeOf,
forwarded,
granted,
hex,
iosDriver,
leases,
principals,
receivedAllocate,
refusal,
refused,
requestedSpecs,
runCommand,
sessionFor,
setUp,
simlockMessage,
type Rig,
} from "./test-support.js";

// why: a baseline is saved from a leased device, so every test here starts by leasing one and saving it.
async function saveBaseline(
rig: Rig,
name: string,
spec: { readonly model: string; readonly platform: "ios" | "android" },
): Promise<void> {
const grant = await rig.daemon.dispatch("lease.request", spec, sessionFor(principals.one));
await rig.daemon.dispatch("baseline.save", { leaseId: grant.lease.id, name }, adminSession);
}

describe("the agent-device front's `deviceBaseline`", () => {
it('allocate with `deviceBaseline` "logged-in" makes a lease request with `baseline: "logged-in"`, alone on iOS and (a baseline of its own) on Android, with `target: "mobile"` (no class) and with `device` (the model too)', async () => {
const rig = await setUp({
drivers: (clock) => [iosDriver(clock), androidDriver(clock)],
maxRunning: 8,
});
await saveBaseline(rig, "logged-in", { model: "iPhone 17 Pro", platform: "ios" });
await saveBaseline(rig, "logged-in-android", { model: "Pixel 8", platform: "android" });
const cases = [
[1, { platform: "ios" }, { baseline: "logged-in", platform: "ios" }],
[
2,
{ deviceBaseline: "logged-in-android", platform: "android" },
{ baseline: "logged-in-android", platform: "android" },
],
[3, { platform: "ios", target: "mobile" }, { baseline: "logged-in", platform: "ios" }],
[
4,
{ device: "iPhone 17 Pro", platform: "ios" },
{ baseline: "logged-in", model: "iPhone 17 Pro", platform: "ios" },
],
] as const;

for (const [n, extra, spec] of cases) {
await granted(rig, n, { deviceBaseline: "logged-in", ...extra });
expect(await requestedSpecs(rig, hex(n))).toEqual([spec]);
}
});

it("the allocate the agent-device daemon receives carries no `deviceBaseline`", async () => {
const rig = await setUp({ drivers: (clock) => [androidDriver(clock)], maxRunning: 8 });
await saveBaseline(rig, "logged-in", { model: "Pixel 8", platform: "android" });

await granted(rig, 1, { deviceBaseline: "logged-in", platform: "android" });

expect(receivedAllocate(rig, 1)).not.toHaveProperty("deviceBaseline");
});

it("the relayed allocate reply carries `data.lease.device.baseline` as `{ name, version }` for a grant from a baseline, with the daemon's `deviceKey` and `data.device` unchanged", async () => {
const rig = await setUp();
await saveBaseline(rig, "logged-in", { model: "iPhone 16", platform: "ios" });
await saveBaseline(rig, "logged-in", { model: "iPhone 16", platform: "ios" });
rig.fake.nameDeviceKeyInNextReply("key-1");
rig.fake.nameDeviceInNextReply({});

const result = (await envelopeOf(await allocate(rig, 1, { deviceBaseline: "logged-in" })))
.result as { data: { device: unknown; lease: Record<string, unknown> } };

expect(result.data.lease["device"]).toEqual({
baseline: { name: "logged-in", version: 2 },
model: "iPhone 16",
osVersion: "26.5",
physical: false,
});
expect(result.data.lease["deviceKey"]).toBe("key-1");
expect(result.data.device).toMatchObject({ id: expect.any(String) });
});

it("allocate with a `deviceBaseline` Simlock refuses (unknown, model or platform mismatch, stale, an invalid name, beside `physical`) answers `refused by simlock:` with simlock's own message and leaves no lease", async () => {
const rig = await setUp({ drivers: (clock) => [iosDriver(clock), androidDriver(clock)] });
await saveBaseline(rig, "logged-in", { model: "iPhone 16", platform: "ios" });
await saveBaseline(rig, "went-stale", { model: "iPhone 16", platform: "ios" });
const sources = await rig.driver.listSources();
const [, staleSource] = sources;
rig.driver.setSources(
sources.map((source) =>
source === staleSource ? { ...source, staleReason: "runtime is not installed" } : source,
),
);
await rig.daemon.dispatch("baseline.list", {}, adminSession);
const refusals: readonly (readonly [Record<string, unknown>, Record<string, unknown>])[] = [
[{ deviceBaseline: "no-such" }, { baseline: "no-such" }],
[
{ device: "iPhone 17 Pro", deviceBaseline: "logged-in" },
{ baseline: "logged-in", model: "iPhone 17 Pro" },
],
[
{ deviceBaseline: "logged-in", platform: "android" },
{ baseline: "logged-in", platform: "android" },
],
[{ deviceBaseline: "went-stale" }, { baseline: "went-stale" }],
[{ deviceBaseline: "Logged In" }, { baseline: "Logged In" }],
[
{ deviceBaseline: "logged-in", physical: true },
{ baseline: "logged-in", physical: true },
],
];
const before = (await leases(rig)).length;

for (const [index, [extra, spec]] of refusals.entries()) {
const expected = await simlockMessage(rig, {
leaseId: hex(190 + index),
platform: "ios",
...spec,
});
const answer = await envelopeOf(await allocate(rig, 100 + index, extra));
expect(answer.error?.message).toBe(`refused by simlock: ${expected}`);
}
expect((await leases(rig)).length).toBe(before);
expect(allocatesReceived(rig)).toEqual([]);
});

it('allocate with `target: "watch"` beside a `deviceBaseline` answers `refused: unknown --target watch; use mobile, tv or desktop` and leaves no lease', async () => {
const rig = await setUp();

const answer = await envelopeOf(
await allocate(rig, 1, { deviceBaseline: "logged-in", target: "watch" }),
);

expect(answer.error).toMatchObject(
refusal("refused: unknown --target watch; use mobile, tv or desktop"),
);
expect(await leases(rig)).toEqual([]);
expect(allocatesReceived(rig)).toEqual([]);
});

it("a command with `deviceBaseline` on `flags`, on `input` or inside a batch step reaches agent-device without it", async () => {
const rig = await setUp();
await granted(rig, 1);
const sent = { deviceBaseline: "logged-in", platform: "ios" };

const onFlags = await forwarded(rig, () => runCommand(rig, "snapshot", 1, { flags: sent }));
const onInput = await forwarded(rig, () => runCommand(rig, "snapshot", 1, { input: sent }));
const inStep = await forwarded(rig, () =>
runCommand(rig, "batch", 1, {
flags: { batchSteps: [{ command: "snapshot", flags: sent }] },
}),
);

expect(onFlags["flags"]).toEqual({ platform: "ios" });
expect(onInput["input"]).toEqual({ platform: "ios" });
const steps = (inStep["flags"] as { batchSteps: { flags: unknown }[] }).batchSteps;
expect(steps[0]?.flags).toEqual({ platform: "ios" });
});

it("allocate with a `deviceBaseline` that is a number is refused with `refused: malformed deviceBaseline`", async () => {
const rig = await setUp();

await refused(
rig,
() => allocate(rig, 1, { deviceBaseline: 7 }),
"refused: malformed deviceBaseline",
);
});
});
Loading
Loading