Skip to content
28 changes: 15 additions & 13 deletions e2e/heartbeat-ttl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ interface LeaseRow {
readonly id: string;
readonly requesterId: string;
readonly ttlDeadline: number;
readonly lastHeartbeatAt?: number;
}

async function leaseRows(env: Awaited<ReturnType<typeof withDaemon>>): Promise<LeaseRow[]> {
Expand All @@ -16,7 +15,7 @@ async function leaseRows(env: Awaited<ReturnType<typeof withDaemon>>): Promise<L
return result.json as LeaseRow[];
}

describe("sliding TTL and heartbeat", () => {
describe("sliding TTL and renewal", () => {
it("refuses to start with a heartbeat interval that isn't at most backstop / 4, naming the key", async () => {
const env = await withDaemon({
mode: "auto",
Expand Down Expand Up @@ -44,12 +43,16 @@ describe("sliding TTL and heartbeat", () => {
);
});

it("slides every heartbeat-capable held lease (MCP and CLI) past the backstop while a detached lease expires at it", async () => {
it("slides every held lease (MCP and CLI) past the backstop while a detached lease expires at it", async () => {
const env = await withDaemon({
// detachedTtlMs is pinned to the same value as heldTtlBackstopMs purely so the
// detached lease's own (unrelated) TTL knob expires it on the timeline this
// test already waits on -- detached mode never heartbeats regardless of TTL,
// it just isn't naturally this short by default.
// test already waits on -- a detached lease has no holder renewing it regardless
// of TTL, it just isn't naturally this short by default.
//
// heartbeatIntervalMs is no longer what keeps the held leases alive (their holders
// renew on their own timer, ADR 0004 §2); it stays here because the config validator
// still requires it to be at most heldTtlBackstopMs / 4, and 4_000ms is short.
configOverrides: {
lease: { detachedTtlMs: 4_000, heldTtlBackstopMs: 4_000, heartbeatIntervalMs: 800 },
},
Expand Down Expand Up @@ -98,16 +101,17 @@ describe("sliding TTL and heartbeat", () => {
]);
const detachedGrant = detachedResult.json as { lease: { id: string } };

// Both held leases -- MCP and CLI -- declare the heartbeat capability and
// slide their deadline on every ping; the detached lease holds no connection
// at all, never heartbeats, and expires exactly at its grant-time TTL.
// Both held holders -- MCP and CLI -- renew on their own timer at a third of the
// TTL (ADR 0004 §2), sliding their deadline well past the backstop; the detached
// lease has no holder process at all, renews never, and expires exactly at its
// grant-time TTL.
await waitFor(
async () => {
const rows = await leaseRows(env);
const detachedRow = rows.find((row) => row.id === detachedGrant.lease.id);
return detachedRow === undefined;
},
{ timeout: 15_000, label: "detached (non-heartbeating) lease expires at its TTL" },
{ timeout: 15_000, label: "detached (unrenewed) lease expires at its TTL" },
);

const rowsAfterExpiry = await leaseRows(env);
Expand All @@ -122,9 +126,7 @@ describe("sliding TTL and heartbeat", () => {
"CLI held lease should have survived past the detached lease's expiry",
).toBeDefined();
expect(mcpRow?.ttlDeadline).toBeGreaterThan(mcpLease.lease.ttlDeadline);
expect(mcpRow?.lastHeartbeatAt).toBeGreaterThan(0);
expect(cliRow?.ttlDeadline).toBeGreaterThan(cliGrant.lease.ttlDeadline);
expect(cliRow?.lastHeartbeatAt).toBeGreaterThan(0);

const statusResult = await mcp.client.callTool({ name: "lease_status", arguments: {} });
const statusExpiry = (statusResult.structuredContent as { ttlDeadline: number }).ttlDeadline;
Expand All @@ -137,15 +139,15 @@ describe("sliding TTL and heartbeat", () => {
entry.event === "lease.renewed" &&
(entry.payload as { leaseId?: string }).leaseId === mcpLease.lease.id,
).length,
"expected repeated lease.renewed events for the heartbeating MCP lease",
"expected repeated lease.renewed events for the renewing MCP lease",
).toBeGreaterThan(1);
expect(
recorded.filter(
(entry) =>
entry.event === "lease.renewed" &&
(entry.payload as { leaseId?: string }).leaseId === cliGrant.lease.id,
).length,
"expected repeated lease.renewed events for the heartbeating CLI lease",
"expected repeated lease.renewed events for the renewing CLI lease",
).toBeGreaterThan(1);
expect(recorded).toContainEqual(
expect.objectContaining({
Expand Down
Loading