Skip to content

Commit 236be86

Browse files
committed
feat(daemon): add managed allocation operation journal
1 parent 5bb3ea3 commit 236be86

41 files changed

Lines changed: 4078 additions & 12 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/capture-kit/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export { decodeDurableDescriptor } from './durable-descriptor-codec.ts';
1616
export { createScreenRecordingLiveHandle } from './screen-recording-live-handle.ts';
1717
export { createScreenRecordingCompletion } from './screen-recording-completion.ts';
1818
export { assertScreenRecordingOptionsSupported } from './screen-recording-options.ts';
19+
export { freezeJsonObject, isBoundedJsonObject } from './durable-json.ts';
1920
export {
2021
cleanupManagedAppLogProcess,
2122
reattachCleanupOnlyAppLogProcess,

‎packages/host-kit/src/file.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export {
22
isAtomicPublishTemporaryPath,
33
publishFileSync,
4+
syncDirectoryBestEffort,
45
withAtomicPublishTempPathSync,
56
} from './internal/atomic-file.ts';
67
export {

‎packages/host-kit/src/internal/atomic-file.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ export function withAtomicPublishTempPathSync<T>(
6363
}
6464
}
6565

66+
/** Syncs a containing directory when the host filesystem supports directory fsync. */
67+
export function syncDirectoryBestEffort(directory: string): void {
68+
let descriptor: number | undefined;
69+
try {
70+
descriptor = fs.openSync(directory, 'r');
71+
fs.fsyncSync(descriptor);
72+
} catch {
73+
} finally {
74+
if (descriptor !== undefined) fs.closeSync(descriptor);
75+
}
76+
}
77+
6678
/** Returns the canonical same-directory temp path used by atomic publishers. */
6779
function createAtomicPublishTempPath(destination: string): string {
6880
return path.join(
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import assert from 'node:assert/strict';
2+
import { test } from 'vitest';
3+
import { newAllocationOperation } from '../allocation-operation-record-factory.ts';
4+
import { applyAllocationTransition } from '../allocation-operation-record-transition.ts';
5+
import { decideAllocationAction } from '../allocation-operation-decision.ts';
6+
import { ALLOCATION_LEASE, ALLOCATION_REQUEST } from './allocation-operation-fixtures.ts';
7+
8+
const NOW = 1_700_000_000_000;
9+
10+
function requested() {
11+
return newAllocationOperation({
12+
...ALLOCATION_REQUEST,
13+
allocatorInstanceId: 'allocator-1',
14+
nowMs: NOW,
15+
});
16+
}
17+
18+
function granted() {
19+
const pending = applyAllocationTransition(requested(), { kind: 'request-dispatched' }, NOW + 1);
20+
assert.equal(pending.status, 'applied');
21+
const result = applyAllocationTransition(
22+
pending.record,
23+
{
24+
kind: 'allocator-outcome',
25+
outcome: {
26+
status: 'granted',
27+
lease: ALLOCATION_LEASE,
28+
identityIncarnationId: 'incarnation-1',
29+
},
30+
},
31+
NOW + 2,
32+
);
33+
assert.equal(result.status, 'applied');
34+
return result.record;
35+
}
36+
37+
test('new work may request once, while recovery of intent only looks up the allocator', () => {
38+
assert.equal(decideAllocationAction(requested(), 'new').kind, 'request');
39+
assert.equal(decideAllocationAction(requested(), 'recover').kind, 'lookup');
40+
const pending = applyAllocationTransition(requested(), { kind: 'request-dispatched' }, NOW + 1);
41+
assert.equal(pending.status, 'applied');
42+
assert.equal(decideAllocationAction(pending.record, 'recover').kind, 'lookup');
43+
});
44+
45+
test('an allocator-unknown record never gets an implicit second request', () => {
46+
const record = applyAllocationTransition(
47+
requested(),
48+
{ kind: 'allocator-unknown', message: 'response lost' },
49+
NOW + 1,
50+
);
51+
assert.equal(record.status, 'applied');
52+
const action = decideAllocationAction(record.record, 'recover');
53+
assert.deepEqual(action, {
54+
kind: 'lookup',
55+
ref: { requesterId: 'requester-a', attemptKey: 'attempt-1' },
56+
});
57+
});
58+
59+
test('grants publish first, and release cleans the binding before calling the allocator', () => {
60+
const record = granted();
61+
assert.equal(decideAllocationAction(record, 'continue').kind, 'publish');
62+
const published = applyAllocationTransition(record, { kind: 'binding-published' }, NOW + 3);
63+
assert.equal(published.status, 'applied');
64+
const cleanup = decideAllocationAction(published.record, 'release');
65+
assert.equal(cleanup.kind, 'cleanup');
66+
const cleaned = applyAllocationTransition(published.record, { kind: 'binding-cleaned' }, NOW + 4);
67+
assert.equal(cleaned.status, 'applied');
68+
assert.deepEqual(decideAllocationAction(cleaned.record, 'release'), {
69+
kind: 'release',
70+
leaseId: 'lease-1',
71+
});
72+
});
73+
74+
test('cancel and explicit supersession are allocator actions, not local terminal guesses', () => {
75+
assert.equal(decideAllocationAction(requested(), 'cancel').kind, 'cancel');
76+
assert.equal(decideAllocationAction(requested(), 'supersede').kind, 'supersede');
77+
assert.equal(decideAllocationAction(granted(), 'cancel').kind, 'blocked');
78+
});
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type {
2+
LeaseRequestInput,
3+
LeaseRequestStatus,
4+
ManagedIdentityRef,
5+
ManagedLease,
6+
ManagedShapeRequest,
7+
} from '@agent-device/contracts/managed-device-allocation';
8+
9+
const ALLOCATION_SHAPE: ManagedShapeRequest = {
10+
platform: 'ios',
11+
deviceType: 'iPhone 16',
12+
osVersion: '18.2',
13+
};
14+
15+
const ALLOCATION_IDENTITY: ManagedIdentityRef = {
16+
deviceId: 'device-1',
17+
identityIncarnationId: 'incarnation-1',
18+
};
19+
20+
export const ALLOCATION_LEASE: ManagedLease = {
21+
id: 'lease-1',
22+
ttlDeadline: 1_700_000_900_000,
23+
device: { address: ALLOCATION_IDENTITY.deviceId },
24+
environment: { SIMLOCK_IOS_DEVICE_SET: '/managed/set' },
25+
};
26+
27+
export const ALLOCATION_REQUEST: LeaseRequestInput = {
28+
requesterId: 'requester-a',
29+
requestGeneration: 1,
30+
attemptKey: 'attempt-1',
31+
shape: ALLOCATION_SHAPE,
32+
deadlineAtMs: 1_700_000_600_000,
33+
admission: 'fail-fast',
34+
activation: 'direct',
35+
attribution: { tenantId: 'tenant-a' },
36+
};
37+
38+
export const ALLOCATION_PENDING_STATUS: LeaseRequestStatus = {
39+
requesterId: ALLOCATION_REQUEST.requesterId,
40+
requestGeneration: ALLOCATION_REQUEST.requestGeneration,
41+
attemptKey: ALLOCATION_REQUEST.attemptKey,
42+
state: 'pending',
43+
};
44+
45+
export const ALLOCATION_GRANTED_STATUS: LeaseRequestStatus = {
46+
...ALLOCATION_PENDING_STATUS,
47+
identityIncarnationId: ALLOCATION_IDENTITY.identityIncarnationId,
48+
state: 'granted',
49+
lease: ALLOCATION_LEASE,
50+
};
51+
52+
export const ALLOCATION_REFUSED_STATUS: LeaseRequestStatus = {
53+
...ALLOCATION_PENDING_STATUS,
54+
state: 'refused',
55+
refusal: { reason: 'simulator-capacity', retryAfterMs: 30_000 },
56+
};

0 commit comments

Comments
 (0)