mirror of
https://github.com/morluto/rea.git
synced 2026-11-08 07:46:28 +00:00
Keep a run-scoped evidence owner from acquisition through settlement and cleanup retry. Bind token observations to native generations and retain related generations and unresolved identity lookups across table omissions, reparenting and process-group changes without treating PID hints as authority. Use native effective UID facts for withholding decisions, corroborate Linux procfs absence, and share the Linux stat parser with sampling and debugger identity checks. Signal only after current token and group revalidation. Complete swept cleanup across all groups with one bounded final family check; retain unresolved owners and successful-peer completion across retries. Validation: npm run check, docs:check and test discovery passed. Focused ownership/consumer coverage totals 513 passed and 1 skipped across 35 files, including the corrected Electron cancellation test rerun independently. Darwin helper compilation, real owned Node child cleanup, CLI/MCP capture and retained cleanup retries ran. Real Electron applications, native Linux races/PID reuse and Windows cleanup were not qualified. The original Linux reports remain open for that work. Fixes #5238 Refs #4322, #5206
120 lines
4.3 KiB
TypeScript
120 lines
4.3 KiB
TypeScript
import type { _electron as electron } from "playwright-core";
|
|
import {
|
|
AnalysisCleanupLease,
|
|
AnalysisCleanupLeaseGroup,
|
|
} from "../artifacts/AnalysisCleanupLease.js";
|
|
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
|
|
import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
|
|
import type { AnalysisError } from "../domain/analysisErrorBase.js";
|
|
import { observeOwnedProcessLineage } from "../process/ProcessOwnershipObservation.js";
|
|
import type {
|
|
PersistentOwnedProcessGroup,
|
|
ProcessCleanupResult,
|
|
ProcessLineageObservation,
|
|
} from "../process/ProcessOwnership.js";
|
|
import { runWithExecutionLimits } from "./PlaywrightElectronActiveActions.js";
|
|
import { cleanupElectronProcesses } from "./ElectronProcessCleanup.js";
|
|
|
|
type ElectronApplication = Awaited<ReturnType<typeof electron.launch>>;
|
|
|
|
/** Transport acknowledgement and process-family release are independent facts. */
|
|
export class ElectronCaptureCleanup {
|
|
readonly #transport: AnalysisCleanupLease;
|
|
readonly #processes: AnalysisCleanupLease;
|
|
readonly #ownership: PersistentOwnedProcessGroup | undefined;
|
|
#closeOperation: Promise<void> | undefined;
|
|
#lineage: ProcessLineageObservation | undefined;
|
|
#processResult: ProcessCleanupResult | undefined;
|
|
|
|
constructor(
|
|
application: Pick<ElectronApplication, "close">,
|
|
ownership: PersistentOwnedProcessGroup | undefined,
|
|
lineage: ProcessLineageObservation | undefined,
|
|
) {
|
|
this.#ownership = ownership;
|
|
this.#lineage = lineage;
|
|
this.#transport = new AnalysisCleanupLease(async () => {
|
|
// A deadline stops this wait, not the underlying Playwright close. Reuse
|
|
// a still-running/fulfilled close; only a physical rejection permits retry.
|
|
if (this.#closeOperation === undefined) {
|
|
const closing = Promise.resolve().then(() => application.close());
|
|
this.#closeOperation = closing;
|
|
void closing.catch(() => {
|
|
if (this.#closeOperation === closing)
|
|
this.#closeOperation = undefined;
|
|
});
|
|
}
|
|
await runWithExecutionLimits(
|
|
this.#closeOperation,
|
|
undefined,
|
|
performance.now() + 5_000,
|
|
);
|
|
}, ["browser_transport"]);
|
|
const resources =
|
|
this.#ownership === undefined
|
|
? ["owned_process_group"]
|
|
: [
|
|
"owned_process_group",
|
|
`run:${this.#ownership.runId}`,
|
|
`process-group:${String(this.#ownership.processGroupId)}`,
|
|
];
|
|
this.#processes = new AnalysisCleanupLease(async () => {
|
|
const cleanup =
|
|
this.#ownership === undefined
|
|
? {
|
|
cleaned: false as const,
|
|
reason: "owned Electron process identity was unavailable",
|
|
}
|
|
: await this.#cleanupProcesses(this.#ownership);
|
|
this.#processResult = cleanup;
|
|
if (!cleanup.cleaned)
|
|
throw new ArtifactReaderFailure("unavailable", cleanup.reason, {
|
|
cleanup: { reason: cleanup.reason, resources },
|
|
});
|
|
}, resources);
|
|
}
|
|
|
|
get processResult(): ProcessCleanupResult | undefined {
|
|
return this.#processResult;
|
|
}
|
|
get pendingResources(): readonly string[] {
|
|
return this.#pending.flatMap((lease) => lease.cleanupResources ?? []);
|
|
}
|
|
get #pending(): AnalysisCleanupLease[] {
|
|
return [this.#transport, this.#processes].filter(
|
|
(lease) => !lease.released,
|
|
);
|
|
}
|
|
|
|
async release(scope: ArtifactResourceScope) {
|
|
const transport = await scope.release({
|
|
kind: "lease",
|
|
resource: "browser_transport",
|
|
lease: this.#transport,
|
|
});
|
|
const processes = await scope.release({
|
|
kind: "lease",
|
|
resource: "owned_process_group",
|
|
lease: this.#processes,
|
|
});
|
|
return { transport, processes };
|
|
}
|
|
|
|
bind(error: AnalysisError): void {
|
|
new AnalysisCleanupLeaseGroup(error, this.#pending);
|
|
}
|
|
|
|
async #cleanupProcesses(
|
|
ownership: PersistentOwnedProcessGroup,
|
|
): Promise<ProcessCleanupResult> {
|
|
const current = await observeOwnedProcessLineage(ownership);
|
|
if (current.status === "verified") this.#lineage = current;
|
|
// Cached POSIX group IDs are candidates only; the shared primitive checks
|
|
// current tokens/start identities. Windows requires this attempt's lineage.
|
|
return cleanupElectronProcesses(
|
|
ownership,
|
|
process.platform === "win32" ? current : (this.#lineage ?? current),
|
|
);
|
|
}
|
|
}
|