Repository navigation
Expand file tree
/
Copy pathsession-teardown.ts
More file actions
212 lines (201 loc) · 7.78 KB
/
Copy pathsession-teardown.ts
File metadata and controls
212 lines (201 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { AppError } from '@agent-device/kernel/errors';
import { emitDiagnostic } from '@agent-device/host-kit/diagnostics';
import { cleanupRetainedMaterializedPathsForSession } from './materialized-path-registry.ts';
import type { SessionRef, SessionState } from './session-state.ts';
import { bindSessionAudioProbe } from './audio-probe-session-binding.ts';
import { bindSessionPerfCapture } from './perf-capture-session-binding.ts';
import { bindSessionScreenRecording } from './screen-recording-session-binding.ts';
import type { SessionStore } from './session-store.ts';
import { forceCleanupSessionAppLog } from './app-log-session-resource.ts';
import { finishLiveAudioProbe } from '@agent-device/capture-kit/audio-probe-session-resource';
import { finishLivePerfCapture } from '@agent-device/capture-kit/perf-capture-session-resource';
import { finishLiveScreenRecording } from '@agent-device/capture-kit/screen-recording-session-resource';
import { openWebSessionNames } from './web-session-names.ts';
import type { PlatformResourceCleanup } from './platform-resource-cleanup.ts';
export async function stopSessionAppLog(params: {
ref: SessionRef;
sessionStore: SessionStore;
}): Promise<void> {
await forceCleanupSessionAppLog(params);
}
export async function stopSessionPerfCapture(params: {
ref: SessionRef;
sessionStore: SessionStore;
}): Promise<void> {
const binding = bindSessionPerfCapture(params.sessionStore, params.ref);
if (binding.read()) await finishLivePerfCapture({ binding, intent: 'disposal' });
}
export async function stopSessionSnapshotHelper(
session: SessionState,
platformCleanup: PlatformResourceCleanup,
): Promise<void> {
await platformCleanup.stopSnapshotHelper(session.device);
}
// Best-effort mirror of the platform close `session close` dispatches for a web session
// (`shouldDispatchPlatformClose` in
// `daemon/session-lifecycle/internal/session-close.ts`) so a daemon shutdown or expired-session
// reap tells agent-browser to close its fleet immediately instead of leaving it
// for agent-browser's own idle timer (`DEFAULT_AGENT_BROWSER_IDLE_TIMEOUT_MS`). Unlike its
// siblings above, this has no second caller in the ordinary-close path (that path already
// reaches the browser through `dispatchTargetedPlatformClose`), so it stays module-private.
async function stopSessionWebBrowser(params: {
ref: SessionRef;
sessionStore: SessionStore;
platformCleanup: PlatformResourceCleanup;
}): Promise<void> {
const { ref, sessionStore, platformCleanup } = params;
const session = sessionStore.resolveCurrent(ref) ?? ref.session;
const sessionName = ref.address;
await platformCleanup.closeManagedBrowser({
device: session.device,
sessionName,
stateDir: sessionStore.resolveDaemonStateDir(),
openSessionNames: () => openWebSessionNames(sessionStore),
});
}
type SessionCleanupStep = { step: string; run: () => Promise<void> };
export type SessionCleanupFailure = { step: string; error: unknown };
// Run every cleanup step, isolating failures so one rejected resource never
// skips the resources scheduled after it. Callers own lease/session deletion and
// decide how to surface the returned failures.
async function runIsolatedSessionCleanup(
steps: readonly SessionCleanupStep[],
): Promise<SessionCleanupFailure[]> {
const failures: SessionCleanupFailure[] = [];
for (const { step, run } of steps) {
try {
await run();
} catch (error) {
failures.push({ step, error });
}
}
return failures;
}
// Emit an aggregate diagnostic for failed cleanup steps and build a single
// actionable error. Returns undefined when nothing failed so the happy path is
// untouched.
export function reportSessionCleanupFailures(params: {
sessionName: string;
phase: string;
failures: readonly SessionCleanupFailure[];
}): AppError | undefined {
if (params.failures.length === 0) return undefined;
const failedSteps = params.failures.map(({ step }) => step);
const stepMessages = params.failures.map(
({ step, error }) => `${step}: ${error instanceof Error ? error.message : String(error)}`,
);
emitDiagnostic({
level: 'error',
phase: params.phase,
data: {
session: params.sessionName,
failedSteps,
errors: stepMessages,
},
});
return new AppError(
'COMMAND_FAILED',
`Session cleanup left ${params.failures.length} resource(s) unreleased: ${stepMessages.join('; ')}`,
{
reason: 'session_cleanup_incomplete',
session: params.sessionName,
failedSteps,
hint: 'Some session resources failed to release; inspect the session log for per-resource diagnostics. The session was still deleted, so retrying is safe.',
},
);
}
type SessionResourceTeardownRequest = {
ref: SessionRef;
sessionStore: SessionStore;
stateDir?: string;
appLog: 'run' | 'already-settled';
platformCleanup?: PlatformResourceCleanup;
};
export async function teardownSessionResources(
request: SessionResourceTeardownRequest,
): Promise<void> {
const { ref, sessionStore } = request;
const session = sessionStore.resolveCurrent(ref) ?? ref.session;
const sessionName = ref.address;
if (!request.platformCleanup) {
throw new AppError(
'INTERNAL_ERROR',
'Platform resource cleanup was not supplied by root runtime composition',
);
}
const platformCleanup = request.platformCleanup;
const appLogSteps: SessionCleanupStep[] =
request.appLog === 'run'
? [
{
step: 'app_log',
run: () => stopSessionAppLog({ ref, sessionStore }),
},
]
: [];
const steps: SessionCleanupStep[] = [
// Finalize any still-active recording BEFORE the Apple runner is stopped
// below: the runner supplies gesture-telemetry for overlay finalization, and
// signalling the recorder first prevents a leaked `simctl recordVideo` child
// (and its 0-byte, slot-holding mp4) when a session is torn down — including
// on daemon shutdown — without an explicit `record stop`.
{
step: 'recording',
run: () =>
finishSessionScreenRecording({
ref,
sessionStore,
}),
},
...appLogSteps,
{
step: 'audio_probe',
run: () => finishSessionAudioProbe({ ref, sessionStore }),
},
{
step: 'perf_capture',
run: () => stopSessionPerfCapture({ ref, sessionStore }),
},
{
step: 'platform_snapshot_helper',
run: () => stopSessionSnapshotHelper(session, platformCleanup),
},
// Runs after the resource steps above (recording, app-log, audio, perf) so nothing is still
// reading through the browser when it closes, mirroring the ordering `runSessionCloseTeardown`
// uses for an ordinary `session close`: best-effort resources first, platform close after.
{
step: 'web_browser',
run: () =>
stopSessionWebBrowser({
ref,
sessionStore,
platformCleanup,
}),
},
];
steps.push({
step: 'materialized_paths',
run: () => cleanupRetainedMaterializedPathsForSession(sessionName),
});
const failures = await runIsolatedSessionCleanup(steps);
const aggregate = reportSessionCleanupFailures({
sessionName,
phase: 'session_teardown_cleanup_failed',
failures,
});
if (aggregate) throw aggregate;
}
export async function finishSessionScreenRecording(params: {
ref: SessionRef;
sessionStore: SessionStore;
}): Promise<void> {
const binding = bindSessionScreenRecording(params.sessionStore, params.ref);
if (binding.read()) await finishLiveScreenRecording({ binding, intent: 'disposal' });
}
export async function finishSessionAudioProbe(params: {
ref: SessionRef;
sessionStore: SessionStore;
}): Promise<void> {
const binding = bindSessionAudioProbe(params.sessionStore, params.ref);
if (binding.read()) await finishLiveAudioProbe({ binding, intent: 'disposal' });
}