import { SamplingReadBatchFailure, samplingInspectionFailures, } from "../process/capture/ProcessSamplingObservation.js"; import { ArtifactReaderFailure, AnalysisCleanupLease, } from "../resources/index.js"; import { AnalysisCancelledError } from "../domain/analysisErrorCore.js"; import { assertProcessOwnershipInspectionActive, isProcessOwnershipInspectionCancellation, waitForProcessOwnershipInspection, } from "../process/ProcessInspectionCancellation.js"; import { randomUUID } from "node:crypto"; import { checkpointNameRefusal, type DesktopSession, } from "../domain/desktop/desktopCapture.js"; import { ownedCommandResources } from "../process/OwnedCommand.js"; import { cleanupOwnedProcessGroup } from "../process/ProcessOwnership.js"; import { ProviderProcessSupervisor, spawnOwnedProviderProcess, type SpawnedOwnedProviderProcess, type ProviderProcessInspectionCleanup, } from "../process/ProviderProcess.js"; import { terminateTokenOwnedProcesses } from "../process/TokenOwnedTermination.js"; import { SessionObservations, matchesWindowNames, observedSession, recordWindows, sampleOpenFiles, sampleProcesses, sampleTokenCarriers, trackProcess, } from "./DesktopSessionObservations.js"; import { DesktopSessionCleanupError, type CheckpointTrigger, type DesktopSessionOutcome, type DesktopSessionPlan, type DesktopWindowObserver, type UnresolvedCleanup, type WindowSample, } from "../ports/DesktopApplicationSession.js"; /** The most of the launched process's stderr a session keeps: its latest bytes, where a failing app's last words are. */ const STDERR_BYTES = 8 * 1024; const TERMINATION_GRACE_MS = 5_000; /** * Windows that already match the application by name before it is launched, * or why the window list cannot be read. A capture refuses to start beside * such windows: the launch could hand off to that instance, and its unsaved * state would sit outside the baseline. */ export const runningApplicationWindows = async ( windows: DesktopWindowObserver, names: ReadonlySet, signal?: AbortSignal, ): Promise => { const sample = await windows.sample(() => false, signal); return sample.state === "listed" ? { state: "listed", windows: sample.windows.filter((window) => matchesWindowNames(window, names), ), } : sample; }; /** * Sample processes, windows and open files. Returns the application's window * count, or undefined when the window list could not be read. */ const sampleSession = async ( plan: DesktopSessionPlan, observations: SessionObservations, signal: AbortSignal, ): Promise => { await sampleProcesses(observations, signal); const sample = await plan.windows.sample( (window) => observations.belongs(window), signal, ); let applicationWindows: number | undefined; if (sample.state === "listed") applicationWindows = await recordWindows( observations, sample.windows, signal, ); else observations.unreadableWindowSamples += 1; await sampleOpenFiles(observations, plan.roots, signal); if (!observations.announced && observations.firstWindowMs !== null) { observations.announced = true; await plan.report( "The application window is open. Send an empty line or `checkpoint ` to record its state, or `stop` to finish.", { kind: "window_open" }, ); } return applicationWindows; }; const checkpointNow = ( plan: DesktopSessionPlan, observations: SessionObservations, name: string, trigger: CheckpointTrigger, atMs = observations.now(), ): Promise => plan.checkpoint(name, trigger, atMs, { openedFiles: new Set(observations.openedFiles.keys()), titles: [...observations.windows.values()] .filter((window) => window.application) .flatMap((window) => window.titles), }); /** Record a requested checkpoint, or report why its name was refused and carry on. */ const requestCheckpoint = async ( plan: DesktopSessionPlan, observations: SessionObservations, name: string | undefined, taken: Set, ): Promise => { const refusal = checkpointNameRefusal(name, taken); if (refusal !== undefined) { await plan.report(`${refusal} No checkpoint was recorded.`, { kind: "checkpoint_refused", name: name ?? "", reason: refusal, }); return; } const atMs = observations.now(); let chosen = name; if (chosen === undefined) { const base = `checkpoint-${String(atMs)}`; chosen = base; for (let suffix = 2; taken.has(chosen); suffix += 1) chosen = `${base}-${String(suffix)}`; } taken.add(chosen); await checkpointNow(plan, observations, chosen, "request", atMs); }; /** * Whether no process the capture started is left. Run-token carriers no * parent link reached are looked for first, since a launcher can exit before * its worker is sampled; an unreadable process table leaves this unknown. */ const noStartedProcessLeft = async ( observations: SessionObservations, signal: AbortSignal, ): Promise => observations.liveProcessIds(true).length === 0 && (await sampleTokenCarriers(observations, signal)) && observations.liveProcessIds(true).length === 0; /** * Watch until the application exits, a stop is requested or time runs out. * The application has exited after two consecutive samples in which the * launched process is gone, no started process is left and the window list * was read and holds none of its windows; an unread list counts as unknown. */ const watch = async ( plan: DesktopSessionPlan, observations: SessionObservations, supervisor: ProviderProcessSupervisor, signal: AbortSignal, ): Promise => { let quietSamples = 0; const taken = new Set(); for (;;) { assertProcessOwnershipInspectionActive(signal); if (observations.now() >= plan.maxDurationMs) return "time_limit"; // One request per sample, so queued lines cannot starve observation. const request = plan.nextRequest(); if (request?.kind === "stop") return "stop_requested"; if (request !== undefined) await requestCheckpoint(plan, observations, request.name, taken); const applicationWindows = await sampleSession(plan, observations, signal); const quiet = supervisor.observedExit() !== undefined && applicationWindows === 0 && (await noStartedProcessLeft(observations, signal)); quietSamples = quiet ? quietSamples + 1 : 0; if (quietSamples >= 2) return "application_exited"; await waitForProcessOwnershipInspection(plan.pollIntervalMs, signal); } }; interface SessionRelease { readonly cleanup: DesktopSession["cleanup"]; readonly inspectionCleanup?: ProviderProcessInspectionCleanup; } /** * Ask the launched process, its process group and every process carrying the * run token to exit, wait up to the grace period, then remove whatever still * carries the token. Processes that only own a matching window, such as an * instance started outside the capture, are never signalled. */ const release = async ( spawned: SpawnedOwnedProviderProcess, supervisor: ProviderProcessSupervisor, ): Promise => { const inspected = await supervisor.retryInspectionCleanup(); // One SIGTERM each, since some applications treat a second one as a forced exit. let launchGroupSignalled = false; if (inspected.ok) { const termination = await terminateTokenOwnedProcesses( spawned.ownership, TERMINATION_GRACE_MS, ); launchGroupSignalled = termination.ok ? termination.value.launchGroupSignalled : termination.error.launchGroupSignalled; if (!termination.ok) supervisor.retainInspectionFailure(termination.error.cause); } if (!launchGroupSignalled && supervisor.observedExit() === undefined) { spawned.process.kill("SIGTERM"); await supervisor.waitForExit(TERMINATION_GRACE_MS); } let stopped = await supervisor.stop(); let inspectionCleanup = inspected.ok ? stopped.status === "incomplete" ? stopped.inspectionCleanup : undefined : inspected.error; // An incomplete stop leaves the supervisor owning the processes; try once more. if (stopped.status === "incomplete") { stopped = await supervisor.stop(); if (inspectionCleanup === undefined && stopped.status === "incomplete") inspectionCleanup = stopped.inspectionCleanup; } return { cleanup: stopped.status === "incomplete" ? { verified: false, reason: stopped.reason } : { verified: true, reason: null }, ...(inspectionCleanup === undefined ? {} : { inspectionCleanup }), }; }; /** * Keep the supervisor owning processes whose release was not verified; its * lease stops them again and is labelled with the run token, pid and group. */ const unreleased = ( spawned: SpawnedOwnedProviderProcess, supervisor: ProviderProcessSupervisor, result: SessionRelease, ): UnresolvedCleanup | undefined => result.cleanup.verified ? undefined : { reason: result.cleanup.reason ?? "cleanup was not verified", ...(result.inspectionCleanup === undefined ? {} : { inspectionCleanup: result.inspectionCleanup }), leases: [ new AnalysisCleanupLease(async () => { const stopped = await supervisor.stop(); if (stopped.status === "incomplete") throw new Error(stopped.reason); }, ownedCommandResources(spawned.ownership)), ], }; /** * The caller's environment without the agent environment's own variables, * such as its control token: agentenv launches the exported app without * them, and the app must not hold a credential to the computer it runs on. * glibc writes a fatal error, such as an abort's reason, to stderr rather * than the terminal, so a failed launch reports it. */ const applicationEnvironment = ( environment: NodeJS.ProcessEnv, ): NodeJS.ProcessEnv => ({ ...Object.fromEntries( Object.entries(environment).filter( ([name]) => !name.startsWith("AGENTENV_"), ), ), LIBC_FATAL_STDERR_: "1", }); const launch = async ( plan: DesktopSessionPlan, runId: string, signal: AbortSignal, ) => { // An env wrapper can clear or overwrite the token supplied to its own // process. Give the application the supervisor's token as the last // assignment, without changing the recorded argv or the wrapper's options. const argv = plan.envProgramIndex === undefined ? plan.argv : plan.argv.toSpliced( plan.envProgramIndex, 0, `REA_PROCESS_RUN_ID=${runId}`, ); const [command, ...arguments_] = argv; const spawned = await spawnOwnedProviderProcess({ command: command ?? "", arguments: arguments_, runId, // Desktop launchers often exec through env, a shell or a wrapper script. expectedCommand: null, cwd: plan.workingDirectory, hostEnvironment: applicationEnvironment(plan.environment), sweepTokenOwnedProcesses: true, signal, }); const supervisor = new ProviderProcessSupervisor( { ...spawned, ownsProcessLifetime: true, cleanup: spawned.cleanup ?? (() => cleanupOwnedProcessGroup(spawned.ownership)), }, { maxDiagnosticBytes: { stdout: 0, stderr: STDERR_BYTES }, diagnosticRetention: "tail", }, ); return { spawned, supervisor }; }; /** * Launch the application with a run token in its own process group and watch * it. Checkpoints run between samples. Cancellation ends the session without * further checkpoints and still releases the application; any other failure * releases it the same way before it is rethrown. */ export const runDesktopSession = async ( plan: DesktopSessionPlan, ): Promise => { const signal = plan.signal ?? new AbortController().signal; const runId = randomUUID(); const startedAt = new Date(); const startedMs = performance.now(); const { spawned, supervisor } = await launch(plan, runId, signal); const observations = new SessionObservations( startedMs, { runId, launchPid: spawned.ownership.leaderPid }, plan.windowMatching, ); let endReason: DesktopSession["end_reason"]; let releaseResult: SessionRelease; // Released at most once: a second release would signal the processes again. let released: Promise | undefined; const releaseOnce = () => (released ??= release(spawned, supervisor)); try { await trackProcess(observations, spawned.ownership.leaderPid, signal); endReason = await watch(plan, observations, supervisor, signal); if (endReason !== "application_exited") await checkpointNow( plan, observations, endReason === "stop_requested" ? "stop" : "time-limit", "stop", ); releaseResult = await releaseOnce(); assertProcessOwnershipInspectionActive(signal); await checkpointNow(plan, observations, "exit", "exit"); assertProcessOwnershipInspectionActive(signal); } catch (cause: unknown) { const inspections = samplingInspectionFailures(cause); for (const inspection of inspections) supervisor.retainInspectionFailure(inspection); const cancelled = cause instanceof AnalysisCancelledError || (cause instanceof ArtifactReaderFailure && cause.reason === "cancelled") || isProcessOwnershipInspectionCancellation(cause) || (cause instanceof SamplingReadBatchFailure && cause.otherFailures.length === 0 && inspections.length > 0 && inspections.every(isProcessOwnershipInspectionCancellation)); releaseResult = await releaseOnce(); if (!cancelled) { const left = unreleased(spawned, supervisor, releaseResult); if (left === undefined) throw cause; throw new DesktopSessionCleanupError(cause, left); } endReason = "cancelled"; } const snapshot = supervisor.snapshot(); const exit = supervisor.observedExit(); const session: DesktopSession = { started_at: startedAt.toISOString(), duration_ms: observations.now(), end_reason: endReason, launch_pid: spawned.ownership.leaderPid, exit: exit === undefined ? null : { code: exit.code, signal: exit.signal }, ...observedSession(observations), stderr: snapshot.stderr.text, stderr_truncated: snapshot.stderr.bytes < snapshot.stderr.observedBytes, cleanup: releaseResult.cleanup, }; return { session, unreleased: unreleased(spawned, supervisor, releaseResult), unreadableWindowSamples: observations.unreadableWindowSamples, }; };