Repository navigation
Expand file tree
/
Copy pathplatform-runtime-app-log-process.ts
More file actions
438 lines (411 loc) · 13.9 KB
/
Copy pathplatform-runtime-app-log-process.ts
File metadata and controls
438 lines (411 loc) · 13.9 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import type {
AppLogBackgroundProcess,
AppLogBackgroundProcessRequest,
AppLogProcessMarker,
AppLogProcessMarkerReadOutcome,
AppLogProcessOwnership,
AppLogProcessCommand,
} from '@agent-device/contracts/app-log-runtime';
import type {
HostCommandRequest,
HostCommandResult,
} from '@agent-device/contracts/platform-runtime-host';
import { decodeAppLogProcessMarker } from '@agent-device/capture-kit';
import type { DeviceIdentity } from '@agent-device/kernel/device';
import type { EnvMap } from '@agent-device/kernel/source-value';
import fs from 'node:fs';
import path from 'node:path';
import type { Readable } from 'node:stream';
import { openVerifiedFileForRead } from '@agent-device/host-kit/file';
import { runCmdBackground } from '@agent-device/host-kit/command';
import {
isProcessAlive,
readProcessCommand,
readProcessStartTime,
waitForProcessExit,
} from '@agent-device/host-kit/process';
import { requireManagedSessionArtifactPath } from '@agent-device/daemon-contracts/session-artifact-paths';
const APP_LOG_PID_FILENAME = 'app-log.pid';
type ManagedAppLogCommandChild = Readonly<{
pid?: number;
exitCode?: number | null;
stdout?: Readable | null;
stderr?: Readable | null;
kill(signal?: NodeJS.Signals | number): boolean;
}>;
export type ManagedAppLogCommand = Readonly<{
child: ManagedAppLogCommandChild;
wait: Promise<HostCommandResult>;
}>;
export type ManagedAppLogCommandLauncher = (
command: AppLogProcessCommand,
signal?: AbortSignal,
) => ManagedAppLogCommand | Promise<ManagedAppLogCommand>;
export function createManagedAppLogProcesses(
sessionsDir: string,
env: EnvMap,
options: Readonly<{ launch?: ManagedAppLogCommandLauncher }> = {},
) {
const root = path.resolve(sessionsDir);
const launch =
options.launch ?? ((command, signal) => launchLocalAppLogCommand(command, env, signal));
return Object.freeze({
start: async (request: AppLogBackgroundProcessRequest, signal?: AbortSignal) =>
await startAppLogProcess(root, request, launch, signal),
readMarker: async (markerPath: string) => readMarker(root, markerPath),
clearMarker: async (markerPath: string) => clearMarker(root, markerPath),
inspect: async (marker: AppLogProcessMarker) => inspectOwnedProcess(marker),
terminate: async (marker: AppLogProcessMarker) => await terminateOwnedProcess(marker),
});
}
export type LegacyAppLogMarkerRecovery = Readonly<{
recovered: readonly string[];
retained: readonly Readonly<{
markerPath: string;
reason: 'invalid' | 'ownership-lost';
message?: string;
device?: DeviceIdentity;
}>[];
}>;
/** Recovers marker-only sessions after the daemon lock is held. */
export async function recoverLegacyAppLogMarkersAfterDaemonLock(
sessionsDir: string,
): Promise<LegacyAppLogMarkerRecovery> {
const recovered: string[] = [];
const retained: Array<{
markerPath: string;
reason: 'invalid' | 'ownership-lost';
message?: string;
device?: DeviceIdentity;
}> = [];
if (!fs.existsSync(sessionsDir)) return { recovered, retained };
const root = path.resolve(sessionsDir);
for (const entry of fs.readdirSync(root, { withFileTypes: true })) {
if (!entry.isDirectory()) continue;
const sessionDir = path.join(root, entry.name);
if (fs.existsSync(path.join(sessionDir, 'app-log.resource.json'))) continue;
const markerPath = path.join(sessionDir, APP_LOG_PID_FILENAME);
const read = readMarker(root, markerPath);
if (read.status === 'missing') continue;
if (read.status === 'invalid') {
retained.push({ markerPath, reason: 'invalid', message: read.message });
continue;
}
const outcome = await terminateOwnedProcess(read.marker);
if (outcome === 'ownership-lost') {
const device = legacyMarkerDeviceIdentity(read.marker.command);
retained.push({ markerPath, reason: 'ownership-lost', ...(device ? { device } : {}) });
continue;
}
clearMarker(root, markerPath);
recovered.push(markerPath);
}
return Object.freeze({ recovered: Object.freeze(recovered), retained: Object.freeze(retained) });
}
function legacyMarkerDeviceIdentity(command: string): DeviceIdentity | undefined {
const android = /(?:^|\s)(?:\S*\/)?adb\s+-s\s+([^\s]+)/.exec(command)?.[1];
if (android) {
return Object.freeze({
id: android,
family: 'android',
kind: android.startsWith('emulator-') ? 'emulator' : 'device',
target: 'mobile',
});
}
const simulator = /(?:^|\s)xcrun\s+(?:--set\s+\S+\s+)?simctl\s+spawn\s+([^\s]+)/.exec(
command,
)?.[1];
if (simulator) {
return Object.freeze({
id: simulator,
family: 'apple',
appleOs: 'ios',
kind: 'simulator',
target: 'mobile',
});
}
const device = /(?:^|\s)xcrun\s+devicectl\b[^\n]*?\s--device\s+([^\s]+)/.exec(command)?.[1];
if (!device) return undefined;
return Object.freeze({
id: device,
family: 'apple',
appleOs: 'ios',
kind: 'device',
target: 'mobile',
iosPhysicalDeviceBackend: 'coredevice',
});
}
async function startAppLogProcess(
root: string,
request: AppLogBackgroundProcessRequest,
launch: ManagedAppLogCommandLauncher,
signal?: AbortSignal,
): Promise<AppLogBackgroundProcess> {
assertMarkerAvailable(root, request.markerPath);
signal?.throwIfAborted();
const background = await launch(request.command, signal);
const child = background.child;
const waitForWrites = forwardProcessOutput(child, request.output);
const marker = await publishProcessMarkerOrRollback(root, request.markerPath, background);
const wait = background.wait
.then(async (result) => {
await waitForWrites();
return result;
})
.finally(() => {
if (request.markerPath && marker) clearPublishedMarker(root, request.markerPath, marker);
});
const terminate = createManagedProcessTermination(child, wait);
return { marker, wait, terminate, [Symbol.asyncDispose]: terminate };
}
function launchLocalAppLogCommand(
command: AppLogProcessCommand,
env: EnvMap,
signal?: AbortSignal,
): ManagedAppLogCommand {
const request = localHostCommand(command);
return runCmdBackground(request.executable, [...request.args], {
allowFailure: request.allowFailure,
cwd: request.cwd,
env: { ...env, ...request.env },
captureOutput: false,
signal,
});
}
function localHostCommand(command: AppLogProcessCommand): HostCommandRequest {
if (command.kind === 'host') return command.request;
return {
executable: 'adb',
args: ['-s', command.serial, ...command.args],
...command.options,
};
}
function assertMarkerAvailable(root: string, markerPath?: string): void {
if (!markerPath) return;
if (readMarker(root, markerPath).status === 'missing') return;
throw new Error('Existing app-log process marker evidence must be recovered before start');
}
function forwardProcessOutput(
child: ManagedAppLogCommandChild,
output: AppLogBackgroundProcessRequest['output'],
): () => Promise<void> {
let writes = Promise.resolve();
let firstWriteError: unknown;
const forward = (chunk: string | Buffer) => {
writes = writes
.then(async () => await output.write(chunk))
.catch((error: unknown) => {
firstWriteError ??= error;
if (typeof child.exitCode !== 'number') child.kill('SIGKILL');
});
};
child.stdout?.on('data', forward);
child.stderr?.on('data', forward);
return async () => {
await writes;
if (firstWriteError !== undefined) throw firstWriteError;
};
}
async function publishProcessMarkerOrRollback(
root: string,
markerPath: string | undefined,
background: ManagedAppLogCommand,
): Promise<AppLogProcessMarker | undefined> {
const marker = background.child.pid
? await resolveProcessMarker(background.child.pid)
: undefined;
try {
if (!marker) {
throw new Error('Managed app-log process did not expose a complete ownership marker');
}
if (markerPath) writeMarker(root, markerPath, marker);
return marker;
} catch (error) {
background.child.kill('SIGKILL');
await background.wait.catch(() => undefined);
throw error;
}
}
function createManagedProcessTermination(
child: ManagedAppLogCommandChild,
wait: AppLogBackgroundProcess['wait'],
): () => Promise<void> {
let termination: Promise<void> | undefined;
return async () =>
await (termination ??= (async () => {
if (typeof child.exitCode !== 'number') {
child.kill('SIGINT');
if (!(await settlesWithin(wait, 2_000))) child.kill('SIGKILL');
}
// A process outside the child's tree, such as one launched inside a simulator, can keep the
// output pipes open after the child exits. Releasing them lets the wait settle on that exit.
if (!(await settlesWithin(wait, 2_000))) {
child.stdout?.destroy();
child.stderr?.destroy();
}
await wait.catch(() => undefined);
})());
}
async function settlesWithin(promise: Promise<unknown>, timeoutMs: number): Promise<boolean> {
return await new Promise((resolve) => {
const timeout = setTimeout(() => resolve(false), timeoutMs);
const settled = () => {
clearTimeout(timeout);
resolve(true);
};
void promise.then(settled, settled);
});
}
function clearPublishedMarker(
root: string,
markerPath: string,
published: AppLogProcessMarker,
): void {
const current = readMarker(root, markerPath);
if (
current.status === 'decoded' &&
current.marker.pid === published.pid &&
current.marker.startTime === published.startTime &&
current.marker.command === published.command
) {
clearMarker(root, markerPath);
}
}
function processMarker(pid: number): AppLogProcessMarker | undefined {
const startTime = readProcessStartTime(pid);
const command = readProcessCommand(pid);
if (!startTime || !command) return undefined;
return Object.freeze({
pid,
startTime,
command,
});
}
async function resolveProcessMarker(pid: number): Promise<AppLogProcessMarker | undefined> {
for (let attempt = 0; attempt < 5; attempt += 1) {
const marker = processMarker(pid);
if (marker) return marker;
await new Promise((resolve) => setTimeout(resolve, 10));
}
return undefined;
}
function readMarker(root: string, markerPath: string): AppLogProcessMarkerReadOutcome {
const resolved = requireManagedMarkerPath(root, markerPath);
let handle: number;
try {
const opened = openVerifiedFileForRead(resolved);
if (opened === undefined) return { status: 'missing' };
handle = opened;
} catch (error) {
return {
status: 'invalid',
message: error instanceof Error ? error.message : 'App-log process marker is invalid',
};
}
try {
const value = JSON.parse(fs.readFileSync(handle, 'utf8')) as unknown;
return decodeAppLogProcessMarker(value);
} catch (error) {
return {
status: 'invalid',
message: error instanceof Error ? error.message : 'App-log process marker is invalid',
};
} finally {
fs.closeSync(handle);
}
}
function writeMarker(root: string, markerPath: string, marker: AppLogProcessMarker): void {
const resolved = requireManagedMarkerPath(root, markerPath);
if (readMarker(root, resolved).status !== 'missing') {
throw new Error(
'Existing app-log process marker evidence must be recovered before replacement',
);
}
fs.mkdirSync(path.dirname(resolved), { recursive: true });
const temporary = `${resolved}.${process.pid}.${Date.now()}.tmp`;
let handle: number | undefined;
let linked = false;
let published = false;
try {
handle = fs.openSync(temporary, 'wx', 0o600);
fs.writeFileSync(handle, `${JSON.stringify(marker)}\n`);
fs.fsyncSync(handle);
fs.closeSync(handle);
handle = undefined;
fs.linkSync(temporary, resolved);
linked = true;
fs.unlinkSync(temporary);
const directory = fs.openSync(path.dirname(resolved), 'r');
try {
fs.fsyncSync(directory);
} finally {
fs.closeSync(directory);
}
published = true;
} finally {
if (handle !== undefined) {
try {
fs.closeSync(handle);
} catch {
// Preserve the publication failure.
}
}
if (!published) {
for (const unresolvedPath of linked ? [resolved, temporary] : [temporary]) {
try {
fs.unlinkSync(unresolvedPath);
} catch {
// Keep the original publication failure.
}
}
}
}
}
function clearMarker(root: string, markerPath: string): void {
const resolved = requireManagedMarkerPath(root, markerPath);
try {
fs.unlinkSync(resolved);
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error;
}
}
function requireManagedMarkerPath(root: string, markerPath: string): string {
return requireManagedSessionArtifactPath({
sessionsDir: root,
pathname: markerPath,
basename: APP_LOG_PID_FILENAME,
label: 'App-log process marker',
});
}
function inspectOwnedProcess(marker: AppLogProcessMarker): AppLogProcessOwnership {
if (!isProcessAlive(marker.pid)) return 'missing';
return markerMatchesLiveProcess(marker) ? 'owned-alive' : 'ownership-lost';
}
async function terminateOwnedProcess(
marker: AppLogProcessMarker,
): Promise<'terminated' | 'already-missing' | 'ownership-lost'> {
if (!isProcessAlive(marker.pid)) return 'already-missing';
if (!markerMatchesLiveProcess(marker)) return 'ownership-lost';
try {
process.kill(marker.pid, 'SIGTERM');
} catch {
return 'already-missing';
}
if (!(await waitForProcessExit(marker.pid, 2_000))) {
if (!markerMatchesLiveProcess(marker)) return 'ownership-lost';
try {
process.kill(marker.pid, 'SIGKILL');
} catch {
return 'already-missing';
}
await waitForProcessExit(marker.pid, 2_000);
}
return isProcessAlive(marker.pid) ? 'ownership-lost' : 'terminated';
}
function markerMatchesLiveProcess(marker: AppLogProcessMarker): boolean {
if (!marker.startTime || !marker.command) return false;
return (
readProcessStartTime(marker.pid) === marker.startTime &&
readProcessCommand(marker.pid) === marker.command
);
}