Skip to content

Commit 8db5cc9

Browse files
thymikeeclaude
andcommitted
fix(provision-kit): look for new rentals before a teardown forgets its lease
The seeded sweep, extended to starts that publish their device early, found a teardown that awaited its stops, found none owed, and then forgot the lease even though the start had recorded a rental meanwhile: shutdown resolved with that device still rented (seed 92). The teardown now re-checks synchronously for an unnamed or unstopped rental before it forgets the lease, and loops if one appeared. The sweep now publishes some devices before their start answers, does not rent a published device twice, and accepts a shutdown report that names a still-unnamed rental by its lease. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0ae8a27 commit 8db5cc9

2 files changed

Lines changed: 24 additions & 12 deletions

File tree

‎packages/provision-kit/src/provider-lease-table-interleaving.test.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,17 @@ async function run(seed: number): Promise<void> {
5757
const table = createProviderLeaseTable<Session, Handle, unknown>({
5858
startTimeoutMs: 50,
5959
stopTimeoutMs: 20,
60-
start: async (target) =>
60+
start: async (target, _request, control) =>
6161
await new Promise((resolve, reject) => {
6262
const id = `${target.leaseId}#${(devices += 1)}`;
6363
lastDevice.set(target.leaseId, id);
6464
starts.push({ id, resolve, reject });
65+
// Some starts publish their device before they answer, as a provider that names it early does.
66+
if (next() < 0.3) {
67+
rented.add(id);
68+
owned.add(id);
69+
control.rented({ id });
70+
}
6571
}),
6672
stop: async ({ id }) => {
6773
const inFlight = (stopsInFlight.get(id) ?? 0) + 1;
@@ -86,7 +92,10 @@ async function run(seed: number): Promise<void> {
8692
releasing: () => new Error('releasing'),
8793
gaveUp: () => new Error('gave up'),
8894
cleanupPending: (unconfirmed) =>
89-
Object.assign(new Error('pending'), { ids: unconfirmed.map((stop) => stop.handle?.id) }),
95+
Object.assign(new Error('pending'), {
96+
ids: unconfirmed.map((stop) => stop.handle?.id),
97+
unnamed: unconfirmed.flatMap((stop) => (stop.handle ? [] : [stop.leaseId])),
98+
}),
9099
});
91100

92101
const allocate = (leaseId: string): void => {
@@ -106,7 +115,8 @@ async function run(seed: number): Promise<void> {
106115
if (start === undefined) return;
107116
const kind = next();
108117
const handle = { id: start.id };
109-
if (kind < 0.85) {
118+
// A device the start already published is the one the answer names; it is not rented twice.
119+
if (kind < 0.85 && !owned.has(start.id)) {
110120
rented.add(start.id);
111121
owned.add(start.id);
112122
}
@@ -128,9 +138,13 @@ async function run(seed: number): Promise<void> {
128138
() => {
129139
if (stillRented().length > 0) fail(`shutdown resolved with ${stillRented().join(', ')}`);
130140
},
131-
(error: { ids?: string[] }) => {
141+
(error: { ids?: string[]; unnamed?: string[] }) => {
142+
// A device is reported by its id, or by its lease when the report could not name it yet.
132143
for (const id of stillRented()) {
133-
if (!error.ids?.includes(id)) fail(`shutdown left ${id} rented and unreported`);
144+
const leaseId = id.split('#')[0]!;
145+
if (!error.ids?.includes(id) && !error.unnamed?.includes(leaseId)) {
146+
fail(`shutdown left ${id} rented and unreported`);
147+
}
134148
}
135149
},
136150
);

‎packages/provision-kit/src/provider-lease-table.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@ import type { DeviceLease } from '@agent-device/contracts/device';
33
/**
44
* The lease table one cloud device provider runtime serves its lease lifecycle from.
55
*
6-
* A provider rents a billed device per lease. The bookkeeping around that rental is the same for every
7-
* provider, and each rule is one a provider gets wrong on its own: a second allocation of one lease rents a
8-
* second device nobody can release; a session forgotten before its stop is confirmed stays billed; a drain
9-
* that skips a start still in flight misses the device that start rents; an allocation that wins a race
10-
* with a release is handed a device that is being stopped; and a caller that stopped waiting leaves a
11-
* device whose only handle is the answer still to come. This table owns those rules. The provider supplies
12-
* start, stop, find-by-lease, and the errors it reports in its own words.
6+
* A provider rents a billed device per lease. This table owns the rules that decide whether that device
7+
* is still rented (ADR 0007); the provider supplies start, stop, find-by-lease, and its own errors.
138
*
149
* Keep this module free of value imports: provider runtimes load it on their construction path.
1510
*/
@@ -544,6 +539,9 @@ class LeaseTable<Session, Handle, Request> {
544539
const owed = await this.stopRentals(entry, tried);
545540
if (owed === 'progress') continue;
546541
if (owed.length > 0) return { status: 'pending', owed };
542+
// The start may have recorded a rental while the stops above were awaited; look again before
543+
// forgetting the lease.
544+
if (entry.unnamed || [...entry.rentals.values()].some((rental) => !rental.stopped)) continue;
547545
return this.finishTeardown(entry);
548546
}
549547
}

0 commit comments

Comments
 (0)