diff --git a/src/cli/commands/lease.ts b/src/cli/commands/lease.ts index 30edba4f6..15c47d22b 100644 --- a/src/cli/commands/lease.ts +++ b/src/cli/commands/lease.ts @@ -50,6 +50,7 @@ async function runLease( "export-env": { type: "boolean" }, help: { type: "boolean", short: "h" }, "image-tag": { type: "string" }, + label: { type: "string" }, "lease-id": { type: "string" }, mode: { type: "string" }, "no-wait": { type: "boolean" }, @@ -63,7 +64,7 @@ async function runLease( environment.stdout.write( "Usage: simlock lease --platform [--device | --class ]\n" + " [--os ] [--mode ] [--image-tag ] [--agent-id ]\n" + - " [--timeout ] [--lease-id ] [--physical]\n" + + " [--timeout ] [--lease-id ] [--label ] [--physical]\n" + " [--no-wait] [--detach] [--ttl ] [--allow-download]\n" + " [--export-env] [--bind-pid ]\n", ); @@ -184,6 +185,7 @@ async function runLease( ...(typeof values.mode === "string" ? { mode: values.mode as "slim" | "full" } : {}), ...(typeof values["image-tag"] === "string" ? { imageTag: values["image-tag"] } : {}), ...(typeof values["lease-id"] === "string" ? { leaseId: values["lease-id"] } : {}), + ...(typeof values.label === "string" ? { label: values.label } : {}), ...(timeoutMs === undefined ? {} : { timeoutMs }), ...(ttlMs === undefined ? {} : { ttlMs }), }, diff --git a/src/cli/index.test.ts b/src/cli/index.test.ts index 50a8e348b..6dfe23319 100644 --- a/src/cli/index.test.ts +++ b/src/cli/index.test.ts @@ -6741,6 +6741,27 @@ describe("CLI: holder renew and release (ADR 0004 §2)", () => { expect(output.stderr).toContain('"code":"LEASE_ID_TAKEN"'); }); + it("the CLI --label flag sends label, and an omitted flag sends none", async () => { + const output = outputCapture(); + const requested: unknown[] = []; + const environment = output.environmentWith({ + clock: new FakeClock(0), + connectAdmin: async () => + fakeClient({ + requestLease: (input) => { + requested.push("label" in input ? input.label : "absent"); + return Promise.resolve(detachedGrant); + }, + }), + }); + const lease = ["lease", "--platform", "android", "--detach"]; + + await runCli([...lease, "--label", "feature-x"], environment); + await runCli(lease, environment); + + expect(requested).toEqual(["feature-x", "absent"]); + }); + it.each(["SIGINT", "SIGTERM"] as const)( "releases explicitly on %s (ADR 0004 §2's catchable signals)", async (signal) => { @@ -9353,7 +9374,7 @@ describe("simlock lease: a request names a model, a class, or nothing", () => { expect(output.stdout).toBe( "Usage: simlock lease --platform [--device | --class ]\n" + " [--os ] [--mode ] [--image-tag ] [--agent-id ]\n" + - " [--timeout ] [--lease-id ] [--physical]\n" + + " [--timeout ] [--lease-id ] [--label ] [--physical]\n" + " [--no-wait] [--detach] [--ttl ] [--allow-download]\n" + " [--export-env] [--bind-pid ]\n", ); diff --git a/src/contract/operations.test.ts b/src/contract/operations.test.ts index a23cba0e2..734dca52e 100644 --- a/src/contract/operations.test.ts +++ b/src/contract/operations.test.ts @@ -328,6 +328,30 @@ describe("lease.request osVersion", () => { ); }); +describe("lease.request label", () => { + const parse = (label: unknown) => + OPERATIONS["lease.request"].input.safeParse({ label, platform: "android" }); + + it.each([ + ["a branch name", "feature/login"], + ["spaces and characters outside ASCII", "Zé's phone"], + ["64 characters", "a".repeat(64)], + ])("accepts %s", (_label, label) => { + expect(parse(label).success).toBe(true); + }); + + it.each([ + ["an empty string", ""], + ["65 characters", "a".repeat(65)], + ["a line break", "feature-x\nhw.keyboard=yes"], + ["a NUL", "feature\u0000x"], + ["a line separator", "feature\u2028x"], + ["a number", 7], + ])("refuses %s", (_label, label) => { + expect(parse(label).success).toBe(false); + }); +}); + describe("lease.request leaseId", () => { const parse = (leaseId: unknown) => OPERATIONS["lease.request"].input.safeParse({ leaseId, model: "iPhone 17", platform: "ios" }); diff --git a/src/contract/operations.ts b/src/contract/operations.ts index 549bb963b..febb12251 100644 --- a/src/contract/operations.ts +++ b/src/contract/operations.ts @@ -31,6 +31,7 @@ import { hostLoadSchema, imageTagSchema, leaseGrantSchema, + leaseLabelSchema, leaseRecordSchema, nukeReportSchema, passthroughCommandSchema, @@ -166,6 +167,7 @@ const leaseRequestBaseSchema = z owner: z.string().optional(), idempotencyKey: z.string().min(1).max(200).optional(), leaseId: leaseIdSchema.optional(), + label: leaseLabelSchema.optional(), fleetRequestId: z.string().min(1).max(200).optional(), requires: z .array(capabilityNameSchema) diff --git a/src/contract/protocol.ts b/src/contract/protocol.ts index 778f455f6..e906cba17 100644 --- a/src/contract/protocol.ts +++ b/src/contract/protocol.ts @@ -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: 69, max: 69 }; +export const PROTOCOL_VERSION_RANGE: ProtocolRange = { min: 70, max: 70 }; const LEGACY_DAEMON_PROTOCOL_VERSION = 2; diff --git a/src/contract/schemas.ts b/src/contract/schemas.ts index 96649398d..33b6aad08 100644 --- a/src/contract/schemas.ts +++ b/src/contract/schemas.ts @@ -89,6 +89,19 @@ export const imageTagSchema = z .max(IMAGE_TAG_MAX) .regex(/^[A-Za-z0-9_.-]+$/, "imageTag may contain only letters, digits, '_', '.' and '-'"); +/** + * A lease's label, the name of the simulator it boots. + * why: a driver passes it to `simctl rename`, so a line break or NUL never reaches one (safety rule 10). + */ +export const leaseLabelSchema = z + .string() + .min(1) + .max(64) + .refine( + (label) => !/[\p{Cc}\u2028\u2029]/u.test(label), + "label must not contain control characters", + ); + const REQUESTER_ID_MAX = 256; const GATEWAY_PREFIX = /^gw:[^:]{0,100}:/; diff --git a/src/core/device-provisioner.ts b/src/core/device-provisioner.ts index b90baac56..d7e767a25 100644 --- a/src/core/device-provisioner.ts +++ b/src/core/device-provisioner.ts @@ -44,6 +44,8 @@ export interface ProvisionDeviceOptions { * handoff, so a request planning in between sees a device that is on its way, and whose. */ readonly claim?: { readonly kind: "boot"; readonly owner?: string }; + /** why: the boot titles the new device with it, so it reaches `makeReady` with the claim. */ + readonly label?: string; readonly onProgress?: (progress: ProvisionProgress) => void; readonly reservation: CapacityReservation; } @@ -118,7 +120,11 @@ export class DeviceProvisioner { stage: "booting", etaMs: driver.estimate({ operation: "boot" }, spec), }); - const ready = await this.options.lifecycle.readyProvisionedForLease(device, bootClaim); + const ready = await this.options.lifecycle.readyProvisionedForLease( + device, + bootClaim, + options.label, + ); if (ready === undefined) throw new Error(`Registered device could not be made ready: ${device.id}`); return ready; diff --git a/src/core/driver.ts b/src/core/driver.ts index ad332b96d..025955c5e 100644 --- a/src/core/driver.ts +++ b/src/core/driver.ts @@ -272,6 +272,8 @@ export interface Driver { options: { readonly purpose: "prepare" | "recover"; readonly mode: DeviceMode; + /** why: a `prepare` boot names the device with the lease's label where its platform can (iOS); a `recover` boot changes nothing (safety rule 2). */ + readonly label?: string; }, ): Promise; reclaim( @@ -320,6 +322,8 @@ export interface Driver { destroyLegacy?(device: VirtualDriverDevice): Promise; advisories?(): Promise; toolVersions?(): Promise; + /** why: names a booted device for its lease, or back to its own name without a label; only a platform that can rename one has it (iOS). */ + applyLabel?(device: DriverDevice, label: string | undefined): Promise; } export interface LegacyDevice { diff --git a/src/core/fake-driver.ts b/src/core/fake-driver.ts index 39692d669..0ad7b4501 100644 --- a/src/core/fake-driver.ts +++ b/src/core/fake-driver.ts @@ -60,7 +60,8 @@ export type FakeDriverOperation = | "removeComponent" | "revalidateRoot" | "findLegacy" - | "destroyLegacy"; + | "destroyLegacy" + | "applyLabel"; export type DriverEstimateOperation = "provision" | "boot" | "reclaim"; @@ -418,7 +419,11 @@ export class FakeDriver implements Driver { async makeReady( device: DriverDevice, - options: { readonly purpose: "prepare" | "recover"; readonly mode: DeviceMode }, + options: { + readonly purpose: "prepare" | "recover"; + readonly mode: DeviceMode; + readonly label?: string; + }, ): Promise { await this.#beforeCall("makeReady", device, options); this.#requireDevice(device); @@ -603,6 +608,10 @@ export class FakeDriver implements Driver { return this.#leaseEnvironment; } + async applyLabel(device: DriverDevice, label: string | undefined): Promise { + await this.#beforeCall("applyLabel", device, label); + } + async passthrough( tool: string, device: VirtualDriverDevice | PhysicalDriverDevice | undefined, diff --git a/src/core/managed-device-lifecycle.ts b/src/core/managed-device-lifecycle.ts index a6e8460b8..b083f30c7 100644 --- a/src/core/managed-device-lifecycle.ts +++ b/src/core/managed-device-lifecycle.ts @@ -71,8 +71,9 @@ export class ManagedDeviceLifecycle { async bootForLease( target: VirtualDevice, claim: DeviceOperationClaim, + label?: string, ): Promise { - return this.#makeReadyForLease(target, "shutdown", claim); + return this.#makeReadyForLease(target, "shutdown", claim, label); } /** @@ -95,8 +96,9 @@ export class ManagedDeviceLifecycle { async readyProvisionedForLease( target: VirtualDevice, claim?: DeviceOperationClaim, + label?: string, ): Promise { - return this.#makeReadyForLease(target, "provisioning", claim); + return this.#makeReadyForLease(target, "provisioning", claim, label); } async shutdown( @@ -231,6 +233,7 @@ export class ManagedDeviceLifecycle { target: VirtualDevice, expectedState: "provisioning" | "shutdown", existingClaim?: DeviceOperationClaim, + label?: string, ): Promise { const claimed = await this.#claim(target, [expectedState], "boot", existingClaim); if (claimed === undefined) return undefined; @@ -242,6 +245,7 @@ export class ManagedDeviceLifecycle { .makeReady(toDriverDevice(claimed.device), { mode: specMode(claimed.device.spec), purpose: "prepare", + ...(label === undefined ? {} : { label }), }); } catch (error: unknown) { await this.#release(claimed); diff --git a/src/daemon-protocol/index.test.ts b/src/daemon-protocol/index.test.ts index d46f88d04..fe5e0fc04 100644 --- a/src/daemon-protocol/index.test.ts +++ b/src/daemon-protocol/index.test.ts @@ -9,7 +9,7 @@ import { describe("daemon protocol", () => { it("keeps the current protocol version and newline framing", () => { - expect(DAEMON_PROTOCOL_VERSION).toBe(69); + expect(DAEMON_PROTOCOL_VERSION).toBe(70); expect(serializeFrame({ id: 1, type: "hello" })).toBe('{"id":1,"type":"hello"}\n'); }); diff --git a/src/daemon/dispatcher.test.ts b/src/daemon/dispatcher.test.ts index c984e6bee..bbc4b1c63 100644 --- a/src/daemon/dispatcher.test.ts +++ b/src/daemon/dispatcher.test.ts @@ -555,6 +555,23 @@ describe("Dispatcher: parsing", () => { ]); }); + it("boots the device of a lease.request with its label", async () => { + const { dispatcher, driver } = await buildDispatcher(); + + await dispatcher.dispatch( + "lease.request", + { label: "feature-x", model: "iPhone 17 Pro", platform: "ios" }, + session(), + ); + + expect(driver.calls.find((call) => call.operation === "makeReady")?.arguments[1]).toMatchObject( + { + label: "feature-x", + purpose: "prepare", + }, + ); + }); + it("grants lease.request's leaseId as the lease ID, and answers an ID that is in use with LEASE_ID_TAKEN carrying it in details", async () => { const { dispatcher } = await buildDispatcher(); const input = { leaseId: "ad-7f3a", model: "iPhone 17 Pro", platform: "ios" } as const; diff --git a/src/daemon/dispatcher.ts b/src/daemon/dispatcher.ts index d43b43a83..c8124b9b3 100644 --- a/src/daemon/dispatcher.ts +++ b/src/daemon/dispatcher.ts @@ -506,6 +506,7 @@ export class Dispatcher { : { onAdmitted: session.onRequestAdmitted }), ...(input.idempotencyKey === undefined ? {} : { idempotencyKey: input.idempotencyKey }), ...(input.leaseId === undefined ? {} : { leaseId: input.leaseId }), + ...(input.label === undefined ? {} : { label: input.label }), ...(input.fleetRequestId === undefined ? {} : { fleetRequestId: input.fleetRequestId }), ...(input.timeoutMs === undefined ? {} : { timeoutMs: input.timeoutMs }), ...(input.ttlMs === undefined ? {} : { ttlMs: input.ttlMs }), diff --git a/src/drivers/ios/AGENTS.md b/src/drivers/ios/AGENTS.md index 342facbe5..4cb26646d 100644 --- a/src/drivers/ios/AGENTS.md +++ b/src/drivers/ios/AGENTS.md @@ -54,10 +54,18 @@ Boot — `makeReady`: what was written. A label not as written → `full`; a readback that fails → `slim`; both → `onSlimSkipped` (logged only). Every label as written, and the write ok → `slim`, `onSlimmed` → `device.slimmed`. -4. `purpose: "recover"`: boot only, no write, no readback. +4. `prepare` with a `label`: `simctl rename