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
44 changes: 25 additions & 19 deletions e2e/capacity-cleanup-nuke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ async function deviceRows(
return result.json as { id: string; driverDeviceId: string; state: string }[];
}

function leaseHeld(
/** Starts a `simlock lease` that stays alive, renewing its own lease (ADR 0004 §2). */
function leaseHolder(
env: Awaited<ReturnType<typeof withDaemon>>,
agentId: string,
): CliBackgroundHandle {
Expand All @@ -57,11 +58,13 @@ async function releaseAndForget(
env: Awaited<ReturnType<typeof withDaemon>>,
agentId: string,
): Promise<void> {
const held = leaseHeld(env, agentId);
const grant = JSON.parse(await held.firstStdoutLine()) as { lease: { id: string } };
const holder = leaseHolder(env, agentId);
const grant = JSON.parse(await holder.firstStdoutLine()) as { lease: { id: string } };
// Released explicitly, before the process is killed: a SIGKILLed holder frees nothing
// (ADR 0004 §3), so the release has to be its own step for the device to come back.
await env.cli(["release", grant.lease.id]);
held.kill("SIGKILL");
await held.waitForExit(15_000).catch(() => undefined);
holder.kill("SIGKILL");
await holder.waitForExit(15_000).catch(() => undefined);
}

/**
Expand Down Expand Up @@ -96,15 +99,15 @@ describe("capacity, warm pool, cleanup, and nuke", () => {
ios: { knownModels: ["iPhone 16"], availableOsVersions: ["18.4"] },
});

const first = leaseHeld(env, "cap-a");
const first = leaseHolder(env, "cap-a");
const grantA = JSON.parse(await first.firstStdoutLine()) as {
lease: { id: string };
device: { driverDeviceId: string };
};

// maxRunning (2) has room, but maxDevices (1) does not: a second requester must
// still queue, because there is no second device to provision.
const second = leaseHeld(env, "cap-b");
const second = leaseHolder(env, "cap-b");
await waitFor(() => second.progressEvents().some((event) => isQueuedAt(event, 1)), {
label: "second requester queues on the managed-device limit, not the running limit",
});
Expand Down Expand Up @@ -142,12 +145,16 @@ describe("capacity, warm pool, cleanup, and nuke", () => {
};
expect(grantB.device.driverDeviceId).toBe(grantA.device.driverDeviceId);

// `second` still holds `grantB`, and a SIGKILL would leave it standing for the whole
// TTL (ADR 0004 §3), so it is released explicitly before the processes are torn down.
await env.cli(["release", grantB.lease.id]);
first.kill("SIGKILL");
second.kill("SIGKILL");
await Promise.all([
first.waitForExit(15_000).catch(() => undefined),
second.waitForExit(15_000).catch(() => undefined),
]);
await waitForLeaseCount(env, 0);
});

it("reports overLimit when a lowered running limit can't yet be met, without evicting existing leases", async () => {
Expand All @@ -158,11 +165,10 @@ describe("capacity, warm pool, cleanup, and nuke", () => {
ios: { knownModels: ["iPhone 16"], availableOsVersions: ["18.4"] },
});

// Detached, not held: a graceful `daemon stop` (which `withConfig` triggers to
// pick up the new limit) releases every held-mode lease as part of closing its
// owning connection -- exactly like a normal held-mode client disconnecting. A
// detached lease has no owning connection, so it survives the restart, which is
// what actually exercises "a lowered limit doesn't evict an existing lease".
// `--detach`, so no holder process has to stay alive across the `daemon stop` that
// `withConfig` triggers to pick up the new limit. The leases themselves survive that
// stop either way now (ADR 0004 §3), which is what lets this exercise "a lowered limit
// doesn't evict an existing lease" against a genuinely still-leased device.
const leaseOne = await env.cli([
"lease",
"--platform",
Expand Down Expand Up @@ -219,14 +225,14 @@ describe("capacity, warm pool, cleanup, and nuke", () => {
ios: { knownModels: ["iPhone 16"], availableOsVersions: ["18.4"] },
});

const heldX = leaseHeld(env, "idle-x");
const grantX = JSON.parse(await heldX.firstStdoutLine()) as {
const holderX = leaseHolder(env, "idle-x");
const grantX = JSON.parse(await holderX.firstStdoutLine()) as {
lease: { id: string };
device: { driverDeviceId: string };
};
await env.cli(["release", grantX.lease.id]);
heldX.kill("SIGKILL");
await heldX.waitForExit(15_000).catch(() => undefined);
holderX.kill("SIGKILL");
await holderX.waitForExit(15_000).catch(() => undefined);
await waitForDeviceState(env, grantX.device.driverDeviceId, "ready");

await waitForCleanupToReach(env, grantX.device.driverDeviceId, "shutdown");
Expand Down Expand Up @@ -259,14 +265,14 @@ describe("capacity, warm pool, cleanup, and nuke", () => {
"--dry-run must never call a destructive/state-changing driver verb",
).toEqual([]);

// nuke: force-releases held leases, clears the queue, destroys registry devices,
// nuke: force-releases every lease, clears the queue, destroys registry devices,
// and must never touch a device the driver reports but the registry doesn't know
// about (safety.md: registry-only destruction).
const heldForNuke = leaseHeld(env, "nuke-held");
const heldForNuke = leaseHolder(env, "nuke-held");
await heldForNuke.firstStdoutLine();
// Managed devices are already at 2/2 and running at 1/1 from the idle/dry-run
// phase above, so this requester has no device to reuse or provision and queues.
const queuedForNuke = leaseHeld(env, "nuke-queued");
const queuedForNuke = leaseHolder(env, "nuke-queued");
await waitFor(() => queuedForNuke.progressEvents().some((event) => isQueuedAt(event, 1)), {
label: "a waiter is queued before nuke",
});
Expand Down
2 changes: 1 addition & 1 deletion e2e/doctor-drift.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function deviceRows(env: Awaited<ReturnType<typeof withDaemon>>): Promise<
return result.json as DeviceRow[];
}

/** Launches a held-mode lease for `agentId`; caller launches every agent it needs
/** Launches a `simlock lease` holder for `agentId`; caller launches every agent it needs
* before awaiting any of their grants, so none is warm-pool reused for the next
* (a sequential lease/release/lease would just reuse the same warm device). */
function leaseHeld(
Expand Down
22 changes: 12 additions & 10 deletions e2e/error-exit-codes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ describe("error and exit-code table", () => {
expectStructuredFailure(result, 1, "UNKNOWN_LEASE");
});

it("lease renew on a held-mode lease -> exit 0 with a later deadline", async () => {
it("lease renew on a running holder's lease -> exit 0 with a later deadline", async () => {
const env = await withDaemon();
await env.driverScript.set({
ios: { knownModels: ["iPhone 16"], availableOsVersions: ["18.4"] },
});

const held = env.cliBackground([
const holder = env.cliBackground([
"lease",
"--platform",
"ios",
Expand All @@ -99,9 +99,11 @@ describe("error and exit-code table", () => {
"--os",
"18.4",
"--agent-id",
"flow4-held",
"flow4-holder",
]);
const grant = JSON.parse(await held.firstStdoutLine()) as { lease: { id: string } };
const grant = JSON.parse(await holder.firstStdoutLine()) as {
lease: { id: string; ttlMs: number };
};

try {
const rows = await waitForLeaseCount(env, 1);
Expand All @@ -114,15 +116,15 @@ describe("error and exit-code table", () => {
const result = await env.cli(["lease", "renew", grant.lease.id]);
expect(result.code).toBe(0);
expect(result.stderr).toBe("");
const renewed = result.json as { id: string; mode: string; ttlDeadline: number };
const renewed = result.json as { id: string; ttlMs: number; ttlDeadline: number };
expect(renewed.id).toBe(grant.lease.id);
expect(renewed.mode).toBe("held");
// A held-mode lease used to have no escape hatch: renewal now resets its
// backstop deadline exactly like a detached lease's, instead of rejecting.
// ADR 0004: one kind of lease, and a body-less renew re-applies its own stored width
// rather than any mode-shaped default.
expect(renewed.ttlMs).toBe(grant.lease.ttlMs);
expect(renewed.ttlDeadline).toBeGreaterThan(beforeDeadline);
} finally {
held.kill("SIGTERM");
await held.waitForExit(15_000);
holder.kill("SIGTERM");
await holder.waitForExit(15_000);
}
});
});
169 changes: 0 additions & 169 deletions e2e/heartbeat-ttl.test.ts

This file was deleted.

Loading
Loading