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
4 changes: 4 additions & 0 deletions packages/capture-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@
"./touch-reference-frame": {
"types": "./src/snapshot/touch-reference-frame.ts",
"default": "./src/snapshot/touch-reference-frame.ts"
},
"./durable-capture/session-binding": {
"types": "./src/durable-capture/session-binding.ts",
"default": "./src/durable-capture/session-binding.ts"
}
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { makeCaptureSessionBinding } from '../../durable-capture/session-binding.fixtures.ts';
import fs from 'node:fs/promises';
import path from 'node:path';
import { expect, test, vi } from 'vitest';
Expand Down Expand Up @@ -44,6 +45,10 @@ test('audio-probe disposes on a failed finish because terminating the helper is
);
const session: DurableCaptureSessionState = {};
sessionStore.set(sessionName, session);
const binding = makeCaptureSessionBinding(sessionStore, sessionName, {
read: (session) => session.audioProbe,
replace: (session, audioProbe) => ({ ...session, audioProbe }),
});
const statusPath = path.join(sessionStore.resolveSessionDir(sessionName), 'audio-probe.json');
const terminate = vi.fn(async () => {});
let resolveExit!: (result: HostCommandResult) => void;
Expand Down Expand Up @@ -85,9 +90,7 @@ test('audio-probe disposes on a failed finish because terminating the helper is
});
await adoptStartedAudioProbe({
admissionLedger: createAudioProbeAdmissionLedger(),
session,
sessionName,
sessionStore,
binding,
device,
owner: localRuntimeOwner('apple'),
fence,
Expand All @@ -101,9 +104,7 @@ test('audio-probe disposes on a failed finish because terminating the helper is
await expect(
finishLiveAudioProbe({
intent: 'capture',
session: sessionStore.get(sessionName) ?? session,
sessionName,
sessionStore,
binding,
}),
).rejects.toThrow('helper exited before completing the capture');

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { makeCaptureSessionBinding } from '../../durable-capture/session-binding.fixtures.ts';
import { vi } from 'vitest';
import type { AppLogCompletion, AppLogLiveHandle } from '@agent-device/contracts/app-log-runtime';
import type { CleanupOutcome, FinishOutcome } from '@agent-device/contracts/durable-resource';
Expand Down Expand Up @@ -30,19 +31,10 @@ export const testCaptureStore = createDurableCaptureResourceStore({
export function createTestCaptureResource(
store: DurableCaptureResourceStore<'app-log'> = testCaptureStore,
) {
return createDurableCaptureResource<
'app-log',
AppLogLiveHandle,
AppLogCompletion,
TestCaptureSession
>({
return createDurableCaptureResource<'app-log', AppLogLiveHandle, AppLogCompletion>({
resourceKind: 'app-log',
displayName: 'test capture',
store,
sessionSlot: {
read: (session) => session.appLog,
replace: (session, appLog) => ({ ...session, appLog, appLogFailure: undefined }),
},
completionMetadata: (completion) => ({
outputPath: completion.outputPath,
completedAt: completion.completedAt,
Expand Down Expand Up @@ -72,8 +64,11 @@ export function makeDurableCaptureContext(
const session: TestCaptureSession = {};
sessionStore.set(sessionName, session);
return {
binding: makeCaptureSessionBinding(sessionStore, sessionName, {
read: (session) => session.appLog,
replace: (session, appLog) => ({ ...session, appLog, appLogFailure: undefined }),
}),
admissionLedger: createDurableCaptureAdmissionLedger({ displayName: 'test capture' }),
session,
sessionName,
sessionStore,
device,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ test('one coordinator exposes the typed manifest and all lifecycle entrypoints',
await expect(
testCaptureResource.finishLive({
intent: 'capture',
session: active,
sessionName: context.sessionName,
sessionStore: context.sessionStore,
binding: context.binding,
}),
).resolves.toMatchObject({ outputPath: '/tmp/app.log', completedAt: 2 });
expect(context.sessionStore.get(context.sessionName)?.appLog).toBeUndefined();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { makeCaptureSessionBinding } from '../../durable-capture/session-binding.fixtures.ts';
import path from 'node:path';
import { expect, test, vi } from 'vitest';
import { createDurableResourceEnvelope } from '../../durable-resource-envelope.ts';
Expand Down Expand Up @@ -187,16 +188,11 @@ function createScreenRecordingTestResource(
return createDurableCaptureResource<
'screen-recording',
ScreenRecordingLiveHandle,
ScreenRecordingCompletion,
DurableCaptureSessionState
ScreenRecordingCompletion
>({
resourceKind: 'screen-recording',
displayName: 'screen recording',
store,
sessionSlot: {
read: (session) => session.screenRecording,
replace: (session, screenRecording) => ({ ...session, screenRecording }),
},
completionMetadata: (completion) => ({
backend: completion.backend,
outputPath: completion.outPath,
Expand All @@ -217,9 +213,14 @@ function makeContext(resource: ReturnType<typeof createScreenRecordingTestResour
const sessionName = 'recording';
const session: DurableCaptureSessionState = {};
sessionStore.set(sessionName, session);
const binding = makeCaptureSessionBinding(sessionStore, sessionName, {
read: (session) => session.screenRecording,
replace: (session, screenRecording) => ({ ...session, screenRecording }),
});
return {
admissionLedger: createDurableCaptureAdmissionLedger({ displayName: 'screen recording' }),
session,
binding,
sessionName,
sessionStore,
device,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { makeCaptureSessionBinding } from '../../durable-capture/session-binding.fixtures.ts';
import { expect, test, vi } from 'vitest';
import { PendingTransferGuard } from '@agent-device/contracts/async-lifecycle';
import { localRuntimeOwner } from '@agent-device/contracts/platform-runtime';
Expand Down Expand Up @@ -35,6 +36,10 @@ test('screen recording persists durable truth before adopting only handle and en
const sessionName = 'recording';
const session: TestRecordingSession = { name: sessionName, device };
sessionStore.set(sessionName, session);
const binding = makeCaptureSessionBinding(sessionStore, sessionName, {
read: (session) => session.screenRecording,
replace: (session, screenRecording) => ({ ...session, screenRecording }),
});
const owner = localRuntimeOwner('android');
const fence = { token: 'recording-fence', generation: 1 } as const;
const finish = vi.fn(async () => ({
Expand Down Expand Up @@ -79,9 +84,7 @@ test('screen recording persists durable truth before adopting only handle and en

await adoptStartedScreenRecording({
admissionLedger: createScreenRecordingAdmissionLedger(),
session,
sessionName,
sessionStore,
binding,
device: session.device,
owner,
fence,
Expand All @@ -101,9 +104,10 @@ test('screen recording persists durable truth before adopting only handle and en

const active = sessionStore.get(sessionName);
if (!active) throw new Error('Expected screen-recording session');
await expect(
finishLiveScreenRecording({ intent: 'capture', session: active, sessionName, sessionStore }),
).resolves.toMatchObject({ backend: 'android', outPath: '/tmp/recording.mp4' });
await expect(finishLiveScreenRecording({ intent: 'capture', binding })).resolves.toMatchObject({
backend: 'android',
outPath: '/tmp/recording.mp4',
});
expect(finish).toHaveBeenCalledOnce();
expect(sessionStore.get(sessionName)?.screenRecording).toBeUndefined();
});
Expand All @@ -115,6 +119,10 @@ test('a failed recording finish keeps the record open and never disposes the rec
const sessionName = 'recording';
const session: TestRecordingSession = { name: sessionName, device };
sessionStore.set(sessionName, session);
const binding = makeCaptureSessionBinding(sessionStore, sessionName, {
read: (session) => session.screenRecording,
replace: (session, screenRecording) => ({ ...session, screenRecording }),
});
const owner = localRuntimeOwner('android');
const fence = { token: 'recording-fence', generation: 1 } as const;
const finishError = new Error('failed to retrieve playable Android recording');
Expand Down Expand Up @@ -150,9 +158,7 @@ test('a failed recording finish keeps the record open and never disposes the rec
});
await adoptStartedScreenRecording({
admissionLedger: createScreenRecordingAdmissionLedger(),
session,
sessionName,
sessionStore,
binding,
device: session.device,
owner,
fence,
Expand All @@ -163,9 +169,7 @@ test('a failed recording finish keeps the record open and never disposes the rec

const active = sessionStore.get(sessionName);
if (!active) throw new Error('Expected screen-recording session');
await expect(
finishLiveScreenRecording({ intent: 'capture', session: active, sessionName, sessionStore }),
).rejects.toBe(finishError);
await expect(finishLiveScreenRecording({ intent: 'capture', binding })).rejects.toBe(finishError);

expect(forceCleanup).not.toHaveBeenCalled();
expect(sessionStore.get(sessionName)?.screenRecording?.handle).toBe(handle);
Expand All @@ -182,6 +186,10 @@ test('a record stop that fails after collecting resumes through the fence withou
const sessionName = 'recording';
const session: TestRecordingSession = { name: sessionName, device };
sessionStore.set(sessionName, session);
const binding = makeCaptureSessionBinding(sessionStore, sessionName, {
read: (session) => session.screenRecording,
replace: (session, screenRecording) => ({ ...session, screenRecording }),
});
const owner = localRuntimeOwner('android');
const fence = { token: 'recording-fence', generation: 1 } as const;
const signals = vi.fn(async () => ({ observation: { recorder: 'confirmed' as const } }));
Expand Down Expand Up @@ -216,9 +224,7 @@ test('a record stop that fails after collecting resumes through the fence withou
);
await adoptStartedScreenRecording({
admissionLedger: createScreenRecordingAdmissionLedger(),
session,
sessionName,
sessionStore,
binding,
device: session.device,
owner,
fence,
Expand All @@ -239,9 +245,7 @@ test('a record stop that fails after collecting resumes through the fence withou
if (!active) throw new Error('Expected screen-recording session');
return finishLiveScreenRecording({
intent: 'capture',
session: active,
sessionName,
sessionStore,
binding,
});
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
import path from 'node:path';
import { safeSessionName } from '@agent-device/host-kit/session-paths';
import type { DurableCaptureSessionStore } from '../../durable-capture/index.ts';
import { mkdtempForTestSync } from '../../tmp-dir.fixtures.ts';
import { makeCaptureFixtureStore } from '../../durable-capture/session-binding.fixtures.ts';

export type CaptureAdmissionSessionStore<S> = DurableCaptureSessionStore<S> &
Readonly<{
get(name: string): S | undefined;
sessionsDir: string;
}>;
export type CaptureAdmissionSessionStore<S> = ReturnType<
typeof makeCaptureAdmissionSessionStore<S>
>;

/**
* The whole of the daemon `SessionStore` these admission modules ever address — `set`,
* `resolveSessionDir`, and the read-back a test asserts on — over a fresh temp directory, so a
* test of this family needs no session record, store class, or daemon import.
*/
export function makeCaptureAdmissionSessionStore<S>(
prefix: string,
): CaptureAdmissionSessionStore<S> {
export function makeCaptureAdmissionSessionStore<S>(prefix: string) {
const sessionsDir = mkdtempForTestSync(prefix);
const sessions = new Map<string, S>();
return {
set: (name, session) => void sessions.set(name, session),
get: (name) => sessions.get(name),
resolveSessionDir: (name) => path.join(sessionsDir, safeSessionName(name)),
return Object.freeze({
...makeCaptureFixtureStore<S>((name) => path.join(sessionsDir, safeSessionName(name))),
sessionsDir,
};
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,20 @@ import type {
RuntimeOwnerRef,
} from '@agent-device/contracts/platform-runtime';
import type { DeviceInfo } from '@agent-device/kernel/device';
import type { DurableCaptureSessionStore } from '../durable-capture/index.ts';
import type { DurableCaptureSessionBinding } from '../durable-capture/index.ts';
import { createDurableCaptureResource } from './durable-capture-resource.ts';
import type { DurableCaptureFinishIntent } from './durable-capture-resource.ts';
import type { AudioProbeAdmissionLedger } from './audio-probe-admission-ledger.ts';
import { audioProbeResourceStore } from './audio-probe-resource-store.ts';
import type { DurableCaptureSessionState } from './session-state-slice.ts';

export const audioProbeDurableResource = createDurableCaptureResource<
'audio-probe',
AudioProbeLiveHandle,
AudioProbeCompletion,
DurableCaptureSessionState
AudioProbeCompletion
>({
resourceKind: 'audio-probe',
displayName: 'audio probe',
store: audioProbeResourceStore,
sessionSlot: {
read: (session) => session.audioProbe,
replace: (session, audioProbe) => ({ ...session, audioProbe }),
},
completionMetadata: (completion) => ({
backend: completion.backend ?? 'unknown',
source: completion.source,
Expand All @@ -48,9 +42,7 @@ export const audioProbeDurableResource = createDurableCaptureResource<

export function adoptStartedAudioProbe(params: {
admissionLedger: AudioProbeAdmissionLedger;
session: DurableCaptureSessionState;
sessionName: string;
sessionStore: DurableCaptureSessionStore<DurableCaptureSessionState>;
binding: DurableCaptureSessionBinding<'audio-probe', AudioProbeLiveHandle>;
device: DeviceInfo;
owner: RuntimeOwnerRef;
fence: ResourceOwnershipFence;
Expand All @@ -62,9 +54,7 @@ export function adoptStartedAudioProbe(params: {
}

export function finishLiveAudioProbe(params: {
session: DurableCaptureSessionState;
sessionName: string;
sessionStore: DurableCaptureSessionStore<DurableCaptureSessionState>;
binding: DurableCaptureSessionBinding<'audio-probe', AudioProbeLiveHandle>;
intent: DurableCaptureFinishIntent;
}): Promise<AudioProbeCompletion> {
return audioProbeDurableResource.finishLive(params);
Expand Down
Loading
Loading