Skip to content

Commit 5c4b003

Browse files
committed
refactor(daemon): share command and teardown budgets
1 parent 35362fe commit 5c4b003

11 files changed

Lines changed: 391 additions & 370 deletions
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import assert from 'node:assert/strict';
2+
import { test } from 'vitest';
3+
import { resolveDaemonRequestTimeoutMs } from '../request-timeout.ts';
4+
5+
test('wait request timeout extends past the user-supplied wait budget', () => {
6+
const base = {
7+
session: 'default',
8+
positionals: [] as string[],
9+
flags: {},
10+
meta: {},
11+
};
12+
13+
// Explicit budgets beyond the default envelope extend it (budget + margin).
14+
assert.equal(
15+
resolveDaemonRequestTimeoutMs({
16+
...base,
17+
command: 'wait',
18+
positionals: ['text', 'Ready', '180000'],
19+
}),
20+
210_000,
21+
);
22+
assert.equal(
23+
resolveDaemonRequestTimeoutMs({
24+
...base,
25+
command: 'wait',
26+
positionals: ['stable', '500', '120000'],
27+
}),
28+
150_000,
29+
);
30+
// Sleep waits block for their full duration and get the same treatment.
31+
assert.equal(
32+
resolveDaemonRequestTimeoutMs({ ...base, command: 'wait', positionals: ['120000'] }),
33+
150_000,
34+
);
35+
// Small budgets never shrink the envelope below the default.
36+
assert.equal(
37+
resolveDaemonRequestTimeoutMs({
38+
...base,
39+
command: 'wait',
40+
positionals: ['text', 'Ready', '5000'],
41+
}),
42+
90_000,
43+
);
44+
// No explicit budget → default envelope.
45+
assert.equal(
46+
resolveDaemonRequestTimeoutMs({ ...base, command: 'wait', positionals: ['text', 'Ready'] }),
47+
90_000,
48+
);
49+
assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'wait' }), 90_000);
50+
});
51+
52+
test('interaction --settle budgets add post-action settle time on top of the normal envelope', () => {
53+
const base = {
54+
session: 'default',
55+
positionals: ['@e2'],
56+
meta: {},
57+
};
58+
59+
// --timeout bounds the SETTLE wait after selector resolution and the action,
60+
// so the envelope keeps the normal touch-command overhead and then adds the
61+
// settle budget plus the same safety margin used by wait.
62+
assert.equal(
63+
resolveDaemonRequestTimeoutMs({
64+
...base,
65+
command: 'press',
66+
flags: { settle: true, timeoutMs: 120_000 },
67+
}),
68+
240_000,
69+
);
70+
// A small settle deadline still needs the normal touch-command envelope plus
71+
// room for post-action observation.
72+
assert.equal(
73+
resolveDaemonRequestTimeoutMs({
74+
...base,
75+
command: 'fill',
76+
flags: { settle: true, timeoutMs: 5_000 },
77+
}),
78+
125_000,
79+
);
80+
// Longpress keeps the cold Android helper route and its 120-second maximum
81+
// hold inside the outer envelope.
82+
assert.equal(
83+
resolveDaemonRequestTimeoutMs({
84+
...base,
85+
command: 'longpress',
86+
positionals: ['300', '500', '120000'],
87+
flags: {},
88+
}),
89+
210_000,
90+
);
91+
// Bare --settle adds its default budget after the longpress-specific base,
92+
// so a maximum hold still leaves room for post-action observation.
93+
assert.equal(
94+
resolveDaemonRequestTimeoutMs({
95+
...base,
96+
command: 'longpress',
97+
flags: { settle: true },
98+
}),
99+
250_000,
100+
);
101+
// Bare timeoutMs without --settle remains wire-compatible with older touch
102+
// command clients: it is ignored instead of opting into settle semantics.
103+
assert.equal(
104+
resolveDaemonRequestTimeoutMs({
105+
...base,
106+
command: 'press',
107+
flags: { timeoutMs: 120_000 },
108+
}),
109+
90_000,
110+
);
111+
assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'press', flags: {} }), 90_000);
112+
});
113+
114+
test('snapshot uses the standard daemon request timeout with an explicit override', () => {
115+
const base = {
116+
session: 'default',
117+
positionals: [],
118+
flags: {},
119+
meta: {},
120+
};
121+
122+
assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'snapshot' }), 90_000);
123+
assert.equal(
124+
resolveDaemonRequestTimeoutMs({
125+
...base,
126+
command: 'snapshot',
127+
flags: { timeoutMs: 120_000 },
128+
}),
129+
120_000,
130+
);
131+
assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'screenshot' }), 90_000);
132+
assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'install' }), 180_000);
133+
assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'reinstall' }), 180_000);
134+
assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'install_source' }), 180_000);
135+
assert.equal(
136+
resolveDaemonRequestTimeoutMs({
137+
...base,
138+
command: 'prepare',
139+
positionals: ['ios-runner'],
140+
}),
141+
240_000,
142+
);
143+
assert.equal(
144+
resolveDaemonRequestTimeoutMs({
145+
...base,
146+
command: 'prepare',
147+
positionals: ['ios-runner'],
148+
flags: { timeoutMs: 240_000 },
149+
}),
150+
240_000,
151+
);
152+
assert.equal(resolveDaemonRequestTimeoutMs({ ...base, command: 'test' }), undefined);
153+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import path from 'node:path';
2+
import { expect, test } from 'vitest';
3+
import { AGENT_BROWSER_TIMEOUT_MS } from '@agent-device/platform-web';
4+
import { mkdtempForTestSync } from '../../__tests__/test-utils/tmp-dir.ts';
5+
import { SessionStore } from '../session-store.ts';
6+
import type { SessionState } from '../types.ts';
7+
import {
8+
resolveDaemonSessionTeardownTimeoutMs,
9+
SCREEN_RECORDING_SESSION_TEARDOWN_BUDGET_MS,
10+
WEB_BROWSER_SESSION_TEARDOWN_BUDGET_MS,
11+
} from '../session-teardown-budget.ts';
12+
import { makeRecordingSession, makeWebSession } from './session-teardown.fixtures.ts';
13+
14+
test('daemon session teardown budget extends for an active durable recording', () => {
15+
const root = mkdtempForTestSync('agent-device-shutdown-recording-budget-');
16+
const sessionStore = new SessionStore(path.join(root, 'sessions'));
17+
const session = makeRecordingSession({
18+
name: 'budget-session',
19+
sessionStore,
20+
finish: async () => ({ status: 'cleanup-pending', reason: 'transport-failed' }),
21+
});
22+
const withRecording = resolveDaemonSessionTeardownTimeoutMs(session);
23+
session.screenRecording = undefined;
24+
const withoutRecording = resolveDaemonSessionTeardownTimeoutMs(session);
25+
26+
expect(withRecording - withoutRecording).toBe(SCREEN_RECORDING_SESSION_TEARDOWN_BUDGET_MS);
27+
});
28+
29+
test('daemon session teardown budget extends for an open web session', () => {
30+
const webSession = makeWebSession('budget-web-session');
31+
const androidSession: SessionState = {
32+
name: 'budget-android-session',
33+
device: {
34+
platform: 'android',
35+
id: 'emulator-5554',
36+
name: 'Pixel',
37+
kind: 'emulator',
38+
booted: true,
39+
},
40+
createdAt: Date.now(),
41+
actions: [],
42+
};
43+
44+
expect(
45+
resolveDaemonSessionTeardownTimeoutMs(webSession) -
46+
resolveDaemonSessionTeardownTimeoutMs(androidSession),
47+
).toBe(WEB_BROWSER_SESSION_TEARDOWN_BUDGET_MS);
48+
});
49+
50+
// Pins the "mirrors AGENT_BROWSER_TIMEOUT_MS" comment on WEB_BROWSER_SESSION_TEARDOWN_BUDGET_MS
51+
// as a checked invariant rather than a claim nothing enforces: if agent-browser-provider.ts's own
52+
// per-call timeout changes, this budget must move with it in the same PR.
53+
test('the web-close teardown budget stays pinned to one agent-browser CLI call timeout', () => {
54+
expect(WEB_BROWSER_SESSION_TEARDOWN_BUDGET_MS).toBe(AGENT_BROWSER_TIMEOUT_MS);
55+
});
56+
57+
test('command admission reserves recording teardown before a session exists', () => {
58+
expect(
59+
resolveDaemonSessionTeardownTimeoutMs(undefined, true) -
60+
resolveDaemonSessionTeardownTimeoutMs(),
61+
).toBe(SCREEN_RECORDING_SESSION_TEARDOWN_BUDGET_MS);
62+
});
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import path from 'node:path';
2+
import { localRuntimeOwner } from '@agent-device/contracts/platform-runtime';
3+
import type { ScreenRecordingLiveHandle } from '@agent-device/contracts/screen-recording-runtime';
4+
import { createDurableResourceEnvelope } from '@agent-device/capture-kit';
5+
import { WEB_DESKTOP_DEVICE } from '../../__tests__/test-utils/device-fixtures.ts';
6+
import { screenRecordingResourceStore } from '../screen-recording-resource-store.ts';
7+
import type { SessionStore } from '../session-store.ts';
8+
import type { SessionState } from '../types.ts';
9+
10+
export function makeRecordingSession(params: {
11+
name: string;
12+
sessionStore: SessionStore;
13+
finish: ScreenRecordingLiveHandle['finish'];
14+
}): SessionState {
15+
const { name, sessionStore, finish } = params;
16+
const device = {
17+
platform: 'apple' as const,
18+
id: 'sim-udid-shutdown',
19+
name: 'iPhone 15',
20+
kind: 'simulator' as const,
21+
booted: true,
22+
};
23+
const outPath = path.join(sessionStore.resolveSessionDir(name), 'recording.mp4');
24+
const handle: ScreenRecordingLiveHandle = {
25+
inspect: () => ({
26+
backend: 'simctl recordVideo',
27+
outPath,
28+
startedAt: Date.now() - 5_000,
29+
scope: 'app',
30+
showTouches: false,
31+
recordOnlySession: false,
32+
gestureEvents: [],
33+
}),
34+
appendGestureEvents: () => {},
35+
setTouchReferenceFrame: () => {},
36+
setRunnerSessionId: () => {},
37+
invalidate: () => {},
38+
finish,
39+
forceCleanup: async () => ({ status: 'cleaned' }),
40+
[Symbol.asyncDispose]: async () => {},
41+
};
42+
const envelope = createDurableResourceEnvelope({
43+
resourceKind: 'screen-recording',
44+
sessionId: name,
45+
device: { id: device.id, family: 'apple', appleOs: 'ios', kind: 'simulator' },
46+
owner: localRuntimeOwner('apple'),
47+
fence: { token: `${name}-fence`, generation: 1 },
48+
lifecycle: 'open',
49+
descriptor: { version: 1, body: { recordingId: name } },
50+
metadata: { phase: 'active' },
51+
});
52+
screenRecordingResourceStore.write(
53+
screenRecordingResourceStore.resolvePath(sessionStore.resolveSessionDir(name)),
54+
envelope,
55+
);
56+
return {
57+
name,
58+
device,
59+
createdAt: Date.now(),
60+
actions: [],
61+
screenRecording: { handle, envelope },
62+
};
63+
}
64+
65+
export function makeWebSession(name: string): SessionState {
66+
return { name, device: WEB_DESKTOP_DEVICE, createdAt: Date.now(), actions: [] };
67+
}

0 commit comments

Comments
 (0)