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
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { test } from 'vitest';
import assert from 'node:assert/strict';
import { createRequestCanceledError } from '@agent-device/kernel/errors';
import { LeaseRegistry } from '../lease-registry.ts';
import {
HUMAN_CONTROL_LEASE_REQUEST,
isHumanControlError,
} from '../../__tests__/support/human-control-fixtures.ts';

test('canceling an active hold re-upsert preserves the hold and blocks device admission', async () => {
let now = 0;
const registry = new LeaseRegistry({ now: () => now });
const lease = registry.allocateLease(HUMAN_CONTROL_LEASE_REQUEST);
const authority = { kind: 'lease', leaseId: lease.leaseId } as const;
const original = await registry.putHumanControlHold(authority, 'console', {
reason: 'Human is using the device',
ttlMs: 10_000,
});
assert.equal(original.state, 'active');
now = 1_000;
const controller = new AbortController();
const renewal = registry.putHumanControlHold(
authority,
'console',
{ reason: 'Renewed hold', ttlMs: 20_000 },
controller.signal,
);
const canceled = createRequestCanceledError();
const rejected = assert.rejects(renewal, (error) => error === canceled);
controller.abort(canceled);
await rejected;

assert.deepEqual(registry.listHumanControlHolds(authority), [original]);
assert.throws(() => registry.assertHumanControlAdmission(lease), isHumanControlError);
await assert.rejects(
registry.runDeviceMutation(lease, async () => 'unwanted mutation'),
isHumanControlError,
);
now = 10_000;
assert.deepEqual(registry.listHumanControlHolds(authority), []);
assert.doesNotThrow(() => registry.assertHumanControlAdmission(lease));
});
9 changes: 6 additions & 3 deletions packages/daemon/src/resources/lease-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,16 @@ export class LeaseRegistry {
},
};
const holds = this.holdsByDevice.get(key) ?? new Map<string, OwnedHumanControlHold>();
const current = existing?.hold.state === 'active' ? existing : pending;
this.holdsByDevice.set(key, holds);
holds.set(id, pending);
return await this.activateHumanControlHold(key, pending, input.ttlMs, signal);
holds.set(id, current);
return await this.activateHumanControlHold(key, pending, current, input.ttlMs, signal);
}

private async activateHumanControlHold(
key: string,
pending: OwnedHumanControlHold,
current: OwnedHumanControlHold,
ttlMs: number | undefined,
signal?: AbortSignal,
): Promise<HumanControlHold> {
Expand All @@ -408,7 +410,7 @@ export class LeaseRegistry {
try {
await this.mutations.wait(key, signal);
signal?.throwIfAborted();
if (holds.get(id) !== pending) {
if (holds.get(id) !== current) {
throw new AppError(
'COMMAND_FAILED',
'Human-control hold changed before activation completed.',
Expand All @@ -422,6 +424,7 @@ export class LeaseRegistry {
updatedAt: activatedAt,
...(ttlMs === undefined ? {} : { expiresAt: activatedAt + ttlMs }),
};
holds.set(id, pending);
this.refreshHeldLease(key, activatedAt);
return cloneHumanControlHold(pending.hold);
} catch (error) {
Expand Down
Loading