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
6 changes: 5 additions & 1 deletion docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,11 @@ resolution](#admin-credential-resolution)). A single host answers as a fleet
of one: `list` shows the host itself, and `drain`, `undrain` and `remove`
answer `UNSUPPORTED_IN_WORKER_MODE` (exit 2), because there is no gateway to
take the host out of rotation or forget it. `list`
prints one line per worker, or the views as JSON with `--json`; `drain`,
prints one line per worker, or the views as JSON with `--json`. Each line
ends with one suffix per capability the worker reports, before any install
lines: ` -- agent-device available`, ` -- agent-device off` or
` -- agent-device unavailable: <reason>`. A worker that reports none, such as
one that is starting, gets no suffix; `drain`,
`undrain` and `remove` print JSON on stdout, unconditionally — `--json` is a
usage error (exit 2) for those three.

Expand Down
3 changes: 2 additions & 1 deletion docs/CONSOLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,11 @@ Each worker shows:
| Version | The worker's Simlock version |
| Devices | How many devices are running, and how many are leased |
| Capacity | Devices running out of the limit, per platform |
| Capabilities | Each optional module the worker knows of, such as agent-device: `available`, `off`, or `unavailable` with its reason |

A worker the gateway could not read from yet says "Not reported" where it
has nothing to show. A worker that is still starting shows its health as
`starting`, and leaves out its devices and capacity: they appear once it has
`starting`, and leaves out its devices, capacity and capabilities: they appear once it has
finished starting. Its page shows the status and the host, and says that devices,
leases and capacity appear once startup finishes.

Expand Down
23 changes: 22 additions & 1 deletion docs/HTTP-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ is on its entry in `workers`.

While the daemon is starting (`daemon.health` is `starting`), the answer is
`200` with the `daemon` block and `host` only: it has not yet checked what it
holds, so `devices`, `leases`, `capacity`, `queueDepth`, `physicalQueueDepth`, `installs`,
holds, so `capabilities`, `devices`, `leases`, `capacity`, `queueDepth`, `physicalQueueDepth`, `installs`,
`waiting`, `warmPool` and `workers` are absent, not empty. They are all there once `health` is
`running`, and also when it is `failed`.

Expand Down Expand Up @@ -245,6 +245,21 @@ device, oldest first, in the shape
[`GET /v1/lease-requests`](#get-v1lease-requests) answers. A worker always
sends it, empty when nothing waits; an older daemon leaves it out.

**`capabilities`** lists each optional module the daemon knows of, with its
state, in the order it sends them (at most 16, no name twice):

```json
[{ "name": "agent-device", "state": "unavailable", "reason": "found 0.20.1, need 0.21.25 or later" }]
```

`name` is lower-case letters, digits and `-`, starting with a letter, up to 32
characters. `state` is `available`, `unavailable` or `off`. `reason` is there
for `unavailable` only: 1 to 500 characters, with control characters replaced
by spaces. A worker with `agentDevice.enabled` `false` or unset sends
`agent-device` as `off`. A status read that takes over 1 s or fails shows
`unavailable` with `could not read <name>'s status`. A gateway's own answer
leaves `capabilities` out: each worker's list is on its entry in `workers`.

A device in `devices` that is `provisioning` or `reclaiming` carries
`transitionAgeMs`, how long it has been in that state. One that has been there
past its threshold with nothing working on it also carries
Expand Down Expand Up @@ -1287,6 +1302,12 @@ install queued behind another may appear only then. A worker that does not
send one shows an empty list; a disconnected or incompatible worker shows
none.

`capabilities` is the worker's own `capabilities` list from its
[`GET /v1/status`](#get-v1status): which optional modules it serves, such as
agent-device. It is re-read on every refresh, so a change shows at the next
periodic refresh at the latest. A starting or incompatible worker shows none;
a disconnected worker keeps the list it last read.

`waiting` is the worker's own `waiting` list from its
[`GET /v1/status`](#get-v1status): the requests waiting in that worker's
queue. It is re-read on every lease event on that worker. A worker that does
Expand Down
69 changes: 67 additions & 2 deletions src/agent-device/edge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ interface Envelope {
interface SetUpOptions {
readonly leaseTtlCapMs?: number;
readonly drivers?: (clock: FakeClock) => FakeDriver[];
readonly enabled?: boolean;
readonly enabled?: boolean | "unset";
readonly endWaitMs?: number;
readonly gatewayMode?: boolean;
readonly maxRunning?: number;
Expand Down Expand Up @@ -165,7 +165,7 @@ async function setUp(options: SetUpOptions = {}): Promise<Rig> {
clock,
configOverrides: {
agentDevice: {
enabled: options.enabled ?? true,
...(options.enabled === "unset" ? {} : { enabled: options.enabled ?? true }),
...(options.leaseTtlCapMs === undefined ? {} : { leaseTtlCapMs: options.leaseTtlCapMs }),
},
capacity: { config: { maxRunning: options.maxRunning ?? 4 }, strategy: "fixed" },
Expand Down Expand Up @@ -3600,3 +3600,68 @@ describe("the agent-device front's request rules", () => {
});
});
});

describe("a worker's capabilities in status.get, through the whole worker daemon", () => {
async function capabilities(rig: Rig) {
return (await rig.daemon.dispatch("status.get", {}, adminSession)).capabilities;
}

it("a worker with agentDevice.enabled unset answers status.get with capabilities [{name: agent-device, state: off}], and so does one with it false", async () => {
const unset = await setUp({ enabled: "unset" });
const off = await setUp({ enabled: false });
const running = await setUp();

expect(await capabilities(running)).toEqual([{ name: "agent-device", state: "available" }]);
expect(await capabilities(unset)).toEqual([{ name: "agent-device", state: "off" }]);
expect(await capabilities(off)).toEqual([{ name: "agent-device", state: "off" }]);
});

it("a worker with agent-device running answers [{name: agent-device, state: available}], with no reason", async () => {
const rig = await setUp();

const [entry] = (await capabilities(rig)) ?? [];

expect(entry).toEqual({ name: "agent-device", state: "available" });
expect(entry).not.toHaveProperty("reason");
});

it("a worker whose agent-device is unavailable answers state unavailable with the same reason its front's notAvailable reply names", async () => {
const rig = await setUp();
const reason = "found 0.20.1, need 0.21.25 or later";
rig.fake.setUnavailable(reason);

const health = await (await send(rig, "/health")).text();

expect(await capabilities(rig)).toEqual([
{ name: "agent-device", reason, state: "unavailable" },
]);
expect(health).toContain(`${notAvailable}${reason}`);
});

it("a worker whose agent-device is starting answers unavailable with reason starting", async () => {
const rig = await setUp();
rig.fake.setUnavailable("starting");

expect(await capabilities(rig)).toEqual([
{ name: "agent-device", reason: "starting", state: "unavailable" },
]);
});

it("a worker whose agent-device goes from unavailable to running answers available on the next status.get", async () => {
const rig = await setUp();
rig.fake.setUnavailable("starting");
expect((await capabilities(rig))?.[0]?.state).toBe("unavailable");

rig.fake.setUnavailable(undefined);

expect(await capabilities(rig)).toEqual([{ name: "agent-device", state: "available" }]);
});

it("a worker's own worker.list answer carries its capabilities", async () => {
const rig = await setUp();

const { workers } = await rig.daemon.dispatch("worker.list", {}, adminSession);

expect(workers[0]?.capabilities).toEqual([{ name: "agent-device", state: "available" }]);
});
});
2 changes: 2 additions & 0 deletions src/agent-device/host/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class AgentDeviceHost {
record: ReleaseScopeRecord = new ReleaseScopeRecord(),
) {
this.#upstreamFetch = recordingFetch(upstream, record);
// why: the gateway's first status.get reads this host's status, and must not pay for the proxy module's load.
void this.#load();
}

async status(): Promise<HostStatus> {
Expand Down
18 changes: 18 additions & 0 deletions src/agent-device/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ export function createAgentDeviceEdgeParts(
};
}

export function agentDeviceCapabilitySource(parts: AgentDeviceEdgeParts): {
readonly name: string;
readonly read: () => Promise<{
readonly state: "available" | "unavailable";
readonly reason?: string;
}>;
} {
return {
name: "agent-device",
read: async () => {
const status = await parts.link.status();
return status.state === "running"
? { state: "available" }
: { reason: status.reason, state: "unavailable" };
},
};
}

// why: a stop ends the gateway's sockets, so the calls that end then must send no renewal that races leasing teardown.
export function disposeAgentDeviceEdge(parts: AgentDeviceEdgeParts): void {
parts.renewals.dispose();
Expand Down
134 changes: 134 additions & 0 deletions src/cli/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6247,3 +6247,137 @@ describe("simlock lease: a request names a model, a class, or nothing", () => {
);
});
});

describe("CLI: a worker's capabilities", () => {
const platform = {
atRamBudget: false,
limit: 2,
maxRunning: 2,
overLimit: false,
reserved: 0,
running: 0,
used: 0,
warm: 0,
};
const worker = (overrides: Record<string, unknown> = {}) => ({
capacity: {
android: platform,
global: { maxRunning: 2, overLimit: false, reserved: 0, running: 0, warm: 0 },
ios: platform,
},
catalog: [],
connection: "connected" as const,
devices: [],
drained: false,
id: "wrk_1",
label: "mac-mini",
lastSeenAt: 1_000,
leases: [],
...overrides,
});
const listed = async (workers: unknown[]): Promise<string> => {
const output = outputCapture();
const environment = output.environmentWith({
connectAdmin: async () =>
fakeClient({ listWorkers: () => Promise.resolve({ workers } as never) }),
});
await runCli(["worker", "list"], environment);
return output.stdout;
};

it("worker list prints ` -- agent-device available`, ` -- agent-device off` and ` -- agent-device unavailable: <reason>` on the worker's line", async () => {
const shown = await listed([
worker({ capabilities: [{ name: "agent-device", state: "available" }] }),
worker({ capabilities: [{ name: "agent-device", state: "off" }], id: "wrk_2" }),
worker({
capabilities: [
{
name: "agent-device",
reason: "found 0.20.1, need 0.21.25 or later",
state: "unavailable",
},
],
id: "wrk_3",
}),
]);

expect(shown).toContain("wrk_1 (mac-mini): connected -- ios 0/2, android 0/2, 0 lease(s)");
expect(shown).toMatch(/^wrk_1 .* -- agent-device available$/m);
expect(shown).toMatch(/^wrk_2 .* -- agent-device off$/m);
expect(shown).toMatch(
/^wrk_3 .* -- agent-device unavailable: found 0\.20\.1, need 0\.21\.25 or later$/m,
);
});

it("worker list prints a starting agent-device as ` -- agent-device unavailable: starting`", async () => {
const shown = await listed([
worker({
capabilities: [{ name: "agent-device", reason: "starting", state: "unavailable" }],
}),
]);

expect(shown).toMatch(/^wrk_1 .* -- agent-device unavailable: starting$/m);
});

it("worker list prints two entries in the order the worker sent them", async () => {
const shown = await listed([
worker({
capabilities: [
{ name: "zeta", state: "off" },
{ name: "agent-device", state: "available" },
],
}),
]);

expect(shown).toMatch(/^wrk_1 .* -- zeta off -- agent-device available$/m);
});

it("worker list prints nothing for a worker whose list is absent", async () => {
const shown = await listed([worker()]);

expect(shown).toMatch(/^wrk_1 .* 0 lease\(s\)$/m);
});

it("worker list puts the suffix on the worker's line, before its install lines", async () => {
const shown = await listed([
worker({
capabilities: [{ name: "agent-device", state: "available" }],
installs: [
{ component: "26.4", platform: "ios", since: 1_000, state: "downloading", waiters: 2 },
],
}),
]);

expect(shown).toContain(
"-- agent-device available\n Install ios 26.4: downloading for 0s, 2 waiters",
);
});

it("status against a gateway prints the same suffix in its workers block", async () => {
const output = outputCapture();
const status = {
capacity: {
android: platform,
global: { maxRunning: 2, overLimit: false, reserved: 0, running: 0, warm: 0 },
ios: platform,
},
daemon: { health: "running" as const, mode: "gateway" as const },
devices: [],
host: { arch: "x64", os: "Linux", osVersion: "6.8.0", tools: [] },
leases: [],
queueDepth: 0,
workers: [
worker({
capabilities: [{ name: "agent-device", reason: "starting", state: "unavailable" }],
}),
],
};
const environment = output.environmentWith({
connectAdmin: async () => fakeClient({ getStatus: () => Promise.resolve(status as never) }),
});

await runCli(["status"], environment);

expect(output.stdout).toMatch(/^wrk_1 .* -- agent-device unavailable: starting$/m);
});
});
9 changes: 8 additions & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2221,7 +2221,14 @@ function formatWorkers(
: ` protocol ${worker.protocol.worker.min}-${worker.protocol.worker.max}` +
` vs gateway ${worker.protocol.gateway.min}-${worker.protocol.gateway.max}`;
const host = worker.host === undefined ? "" : ` -- ${formatHost(worker.host)}`;
return `${label}: ${state} -- ${capacity}, ${leaseCount(worker)}${skew}${host}${installs}`;
const capabilities = (worker.capabilities ?? [])
.map((entry) =>
entry.state === "unavailable"
? ` -- ${entry.name} unavailable: ${entry.reason ?? "unknown"}`
: ` -- ${entry.name} ${entry.state}`,
)
.join("");
return `${label}: ${state} -- ${capacity}, ${leaseCount(worker)}${skew}${host}${capabilities}${installs}`;
})
.join("\n");
}
Expand Down
3 changes: 2 additions & 1 deletion src/contract/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
tokenRecordSchema,
tokenRoleSchema,
waitingRequestSchema,
capabilityListSchema,
workerViewSchema,
} from "./schemas.js";

Expand Down Expand Up @@ -86,7 +87,6 @@ export const catalogGet = defineOperation({

// ---- status.get ---------------------------------------------------------------------------

// fallow-ignore-next-line unused-export -- consumed only through the OPERATIONS registry, not by name; still public contract surface.
export const statusGet = defineOperation({
name: "status.get",
role: "agent",
Expand All @@ -105,6 +105,7 @@ export const statusGet = defineOperation({
* is `starting`, like the fields above: a started worker and a gateway always send it.
*/
warmPool: statusWarmPoolSchema.optional(),
capabilities: capabilityListSchema.optional(),
daemon: z.object({
health: daemonHealthSchema,
mode: daemonModeSchema,
Expand Down
2 changes: 1 addition & 1 deletion src/contract/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface ProtocolRange {
const protocolRangeSchema = z.object({ min: z.number().int(), max: z.number().int() });

/** Widen only once a second version is kept alive side by side with the first; a bump that leaves no shim behind moves both ends. */
export const PROTOCOL_VERSION_RANGE: ProtocolRange = { min: 32, max: 32 };
export const PROTOCOL_VERSION_RANGE: ProtocolRange = { min: 33, max: 33 };

const LEGACY_DAEMON_PROTOCOL_VERSION = 2;

Expand Down
Loading
Loading