Skip to content

Commit f52305c

Browse files
committed
fix(lease): renew an admitted request for the window its lease carries
Admission named its own proxy default on every request, so a client that rented a device for longer than that default lost the window on the next command — the lease-side twin of the heartbeat bug in #2946, which the client fix could not reach because the shortening happened at admission. The window a lease carries is the one its client named when it allocated; only a request naming its own window changes it now, which retires DEFAULT_PROXY_LEASE_TTL_MS with no producer left.
1 parent 84abb66 commit f52305c

8 files changed

Lines changed: 106 additions & 30 deletions

File tree

‎packages/contracts/src/lease-scope.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import type { CloudProviderProfileFields } from './remote-config-fields.ts';
44
import type { CommandFlags } from './command-flags.ts';
55

66
const PROXY_LEASE_PROVIDER = 'proxy';
7-
export const DEFAULT_PROXY_LEASE_TTL_MS = 300_000;
87

98
const REQUIRED_PROXY_LEASE_FIELDS = [
109
'leaseId',

‎src/daemon/__tests__/lease-lifecycle.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ test('admitRequestLeaseForLockedScope heartbeats and stores admitted lease on th
4848

4949
expect(req.internal?.admittedLease?.leaseId).toBe(lease.leaseId);
5050
expect(req.internal?.admittedLease?.heartbeatAt).toBe(2_000);
51-
expect(sessionStore.get('default')?.lease?.expiresAt).toBe(302_000);
51+
// Renewal is for the window the lease carries — the registry default here, since this client
52+
// named none — not the proxy-specific default admission used to name on every request (#2946).
53+
expect(sessionStore.get('default')?.lease?.expiresAt).toBe(62_000);
5254
});
5355

5456
test('cleanupExpiredLeasedSession consumes expired lease and deletes the session after teardown', async () => {

‎src/daemon/__tests__/request-admission.test.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,82 @@ test('sessionless apps admits a provider declared by the runtime app catalog', (
164164
assert.equal(result, undefined);
165165
});
166166

167+
// #2946 (the other half): a proxy client that allocated a lease with a window longer than the old
168+
// synthetic admission default used to lose that window on the next admitted command — admission
169+
// re-renewed every proxy lease at its own default instead of the window the lease carries. Admission
170+
// now renews for the lease's window (ADR 0007); only a request that names a window changes it.
171+
test('admitting a command does not shorten a proxy lease allocated above the old admission default', () => {
172+
let now = 1_000;
173+
const registry = new LeaseRegistry({ now: () => now });
174+
const lease = registry.allocateLease({
175+
tenantId: 'tenant-a',
176+
runId: 'run-1',
177+
leaseProvider: 'proxy',
178+
deviceKey: 'ios:mobile:SIM-001',
179+
clientId: 'client-a',
180+
ttlMs: 600_000,
181+
});
182+
now = 2_000;
183+
184+
const result = assertRequestLeaseAdmission(
185+
makeRequest({
186+
command: 'snapshot',
187+
meta: {
188+
tenantId: 'tenant-a',
189+
runId: 'run-1',
190+
sessionIsolation: 'tenant',
191+
leaseId: lease.leaseId,
192+
leaseProvider: 'proxy',
193+
deviceKey: 'ios:mobile:SIM-001',
194+
clientId: 'client-a',
195+
},
196+
}),
197+
registry,
198+
undefined,
199+
);
200+
201+
assert.equal(result?.leaseId, lease.leaseId);
202+
assert.equal(
203+
result!.expiresAt - result!.heartbeatAt,
204+
600_000,
205+
'the window the lease carries, not a smaller default',
206+
);
207+
});
208+
209+
test('a command that names its own window renews the lease onto it', () => {
210+
let now = 1_000;
211+
const registry = new LeaseRegistry({ now: () => now });
212+
const lease = registry.allocateLease({
213+
tenantId: 'tenant-a',
214+
runId: 'run-1',
215+
leaseProvider: 'proxy',
216+
deviceKey: 'ios:mobile:SIM-001',
217+
clientId: 'client-a',
218+
ttlMs: 600_000,
219+
});
220+
now = 2_000;
221+
222+
const result = assertRequestLeaseAdmission(
223+
makeRequest({
224+
command: 'snapshot',
225+
meta: {
226+
tenantId: 'tenant-a',
227+
runId: 'run-1',
228+
sessionIsolation: 'tenant',
229+
leaseId: lease.leaseId,
230+
leaseProvider: 'proxy',
231+
deviceKey: 'ios:mobile:SIM-001',
232+
clientId: 'client-a',
233+
leaseTtlMs: 90_000,
234+
},
235+
}),
236+
registry,
237+
undefined,
238+
);
239+
240+
assert.equal(result!.expiresAt - result!.heartbeatAt, 90_000);
241+
});
242+
167243
test('close still admits and heartbeats a real active lease', () => {
168244
let now = 1_000;
169245
const registry = new LeaseRegistry({ now: () => now });

‎src/daemon/__tests__/request-execution-scope.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ test('leased session admission uses stored lease metadata and heartbeats', async
224224
expect(scope.sessionName).toBe('default');
225225
const activeLease = leaseRegistry.listActiveLeases()[0];
226226
expect(activeLease?.heartbeatAt).toBe(2_000);
227-
expect(activeLease?.expiresAt).toBe(302_000);
228-
expect(sessionStore.get('default')?.lease?.expiresAt).toBe(302_000);
227+
expect(activeLease?.expiresAt).toBe(62_000);
228+
expect(sessionStore.get('default')?.lease?.expiresAt).toBe(62_000);
229229
});
230230

231231
test('leased session heartbeat is serialized with the request execution lock', async () => {

‎src/daemon/__tests__/request-router-open.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ test('open stores admitted lease metadata on the session', async () => {
368368
leaseProvider: 'proxy',
369369
clientId: 'client-a',
370370
deviceKey: 'ios:SIM-LEASED',
371-
expiresAt: 301_000,
371+
expiresAt: 61_000,
372372
});
373373
});
374374

‎src/daemon/lease-context.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import type { DeviceLease } from '@agent-device/contracts/device';
44
import type { RunnerLogicalLeaseContext } from '@agent-device/contracts/runner-lease-context';
55
import { stripUndefined } from '@agent-device/kernel/record';
66
import {
7-
DEFAULT_PROXY_LEASE_TTL_MS,
87
findMissingProxyLeaseFields,
98
isProxyLeaseScope,
109
leaseScopeFromRequest,
1110
type LeaseScope,
1211
} from '@agent-device/contracts/lease-scope';
1312

14-
export { DEFAULT_PROXY_LEASE_TTL_MS, findMissingProxyLeaseFields, isProxyLeaseScope };
13+
export { findMissingProxyLeaseFields, isProxyLeaseScope };
1514
export type { LeaseScope };
1615

1716
export type SessionLease = {

‎src/daemon/request-admission.ts‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import {
88
} from './daemon-command-registry.ts';
99
import type { DeviceLease, ProviderAppCatalog } from '@agent-device/contracts/device';
1010
import {
11-
DEFAULT_PROXY_LEASE_TTL_MS,
1211
findMissingProxyLeaseFields,
13-
isProxyLeaseScope,
1412
resolveLeaseScope,
1513
resolveRequestOrSessionLeaseScope,
1614
} from './lease-context.ts';
@@ -94,14 +92,11 @@ export function assertRequestLeaseAdmission(
9492
}
9593
assertRequestSessionLeaseMatches(requestLeaseScope, sessionLease);
9694
const leaseScope = resolveRequestOrSessionLeaseScope(req, session);
97-
const heartbeatLeaseScope = {
98-
...leaseScope,
99-
leaseTtlMs:
100-
leaseScope.leaseTtlMs ??
101-
(isProxyLeaseScope(leaseScope) ? DEFAULT_PROXY_LEASE_TTL_MS : undefined),
102-
};
10395
leaseRegistry.assertLeaseAdmission(leaseScopeToHeartbeatRequest(leaseScope));
104-
const lease = leaseRegistry.heartbeatLease(leaseScopeToHeartbeatRequest(heartbeatLeaseScope));
96+
// Admission renews for the window the lease already carries, or the window this request named.
97+
// Naming a proxy-specific default here used to shorten every lease allocated above it — a client
98+
// that rented a device for longer than the default lost it on the next admitted command (#2946).
99+
const lease = leaseRegistry.heartbeatLease(leaseScopeToHeartbeatRequest(leaseScope));
105100
if (isHumanControlMutation(req)) leaseRegistry.assertHumanControlAdmission(lease);
106101
return lease;
107102
}

‎test/integration/provider-scenarios/remote-proxy-parity.test.ts‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import http from 'node:http';
44
import os from 'node:os';
55
import path from 'node:path';
66
import { test } from 'vitest';
7-
import { DEFAULT_PROXY_LEASE_TTL_MS } from '@agent-device/contracts/lease-scope';
87
import { sendToDaemon } from '../../../src/daemon-client/daemon-client.ts';
98
import { LeaseRegistry } from '../../../src/daemon/lease-registry.ts';
109
import { createDaemonHttpServer } from '../../../src/daemon/server/http-server.ts';
@@ -549,7 +548,7 @@ test(
549548
async ({ world, proxied }) => {
550549
const session = 'leased';
551550
const flags = { platform: 'ios', udid: SIM.id } as const;
552-
const allocate = async (): Promise<string> => {
551+
const allocate = async (): Promise<{ leaseId: string; expiresAt: number }> => {
553552
const response = await proxied({
554553
session,
555554
command: 'lease_allocate',
@@ -558,9 +557,13 @@ test(
558557
meta: LEASE_SCOPE,
559558
});
560559
assert.equal(response.ok, true, JSON.stringify(response));
561-
const leaseId = (response.ok ? response.data : {})?.lease as { leaseId?: string };
562-
assert.equal(typeof leaseId?.leaseId, 'string');
563-
return leaseId.leaseId!;
560+
const lease = (response.ok ? response.data : {})?.lease as {
561+
leaseId?: string;
562+
expiresAt?: number;
563+
};
564+
assert.equal(typeof lease?.leaseId, 'string');
565+
assert.equal(typeof lease?.expiresAt, 'number');
566+
return { leaseId: lease.leaseId!, expiresAt: lease.expiresAt! };
564567
};
565568
const run = async (
566569
command: string,
@@ -577,22 +580,24 @@ test(
577580
});
578581

579582
const firstLease = await allocate();
580-
assert.equal((await run('open', [APP], firstLease)).ok, true);
583+
assert.equal((await run('open', [APP], firstLease.leaseId)).ok, true);
581584
assert.equal(
582-
(await run('snapshot', [], firstLease, { snapshotInteractiveOnly: true })).ok,
585+
(await run('snapshot', [], firstLease.leaseId, { snapshotInteractiveOnly: true })).ok,
583586
true,
584587
);
585588
assert.equal(
586589
baselineInitialized(
587-
await run('diff', ['snapshot'], firstLease, { snapshotInteractiveOnly: true }),
590+
await run('diff', ['snapshot'], firstLease.leaseId, { snapshotInteractiveOnly: true }),
588591
),
589592
false,
590593
'the leased session holds comparison state before it expires',
591594
);
592595

593596
// The lease lapses without a heartbeat; the next request through the proxy finds it expired.
594-
now += DEFAULT_PROXY_LEASE_TTL_MS + 1;
595-
const expired = await run('diff', ['snapshot'], firstLease, {
597+
// The jump is the window the lease itself was allocated with — a client that names no ttl
598+
// gets the registry default, which is the only window the daemon promises it.
599+
now = firstLease.expiresAt + 1;
600+
const expired = await run('diff', ['snapshot'], firstLease.leaseId, {
596601
snapshotInteractiveOnly: true,
597602
});
598603
assert.equal(expired.ok, false);
@@ -603,16 +608,16 @@ test(
603608
assert.equal(world.daemon.session(session), undefined, 'expiry tears the session down');
604609

605610
const secondLease = await allocate();
606-
assert.notEqual(secondLease, firstLease);
607-
assert.equal((await run('open', [APP], secondLease)).ok, true);
611+
assert.notEqual(secondLease.leaseId, firstLease.leaseId);
612+
assert.equal((await run('open', [APP], secondLease.leaseId)).ok, true);
608613
assert.equal(
609614
baselineInitialized(
610-
await run('diff', ['snapshot'], secondLease, { snapshotInteractiveOnly: true }),
615+
await run('diff', ['snapshot'], secondLease.leaseId, { snapshotInteractiveOnly: true }),
611616
),
612617
true,
613618
'a reacquired lease must not compare against the expired session tree',
614619
);
615-
assert.equal((await run('close', [], secondLease, {})).ok, true);
620+
assert.equal((await run('close', [], secondLease.leaseId, {})).ok, true);
616621
},
617622
{ leaseRegistry },
618623
);

0 commit comments

Comments
 (0)