mirror of
https://github.com/morluto/rea.git
synced 2026-11-18 20:53:00 +00:00
* refactor(kernel): give error-to-message text one owner in a new src/kernel layer * fix(kernel): keep the regex worker's own error text inside its isolated source * refactor(kernel): exclude copies that run in another realm from the error-message guard by name The regex worker's source string and the page.evaluate fetch in PlaywrightScenarioRequest cannot import errorMessage. The guard now names them, so the counted baseline does not invite a later slice to rewrite them. * refactor(kernel): migrate the long tail of inline error-message copies to errorMessage (#11552) Every provider and adapter folder with six or fewer copies now calls errorMessage from src/kernel. Local helpers whose whole body was the copy (failureMessage, gdbErrorMessage, message, describe, reasonOf, recoveryFailureMessage and four local errorMessage definitions) are removed and their call sites call the owner. Refs #11548 * refactor(kernel): give byte-to-hex formatting one owner in src/kernel (#11560) byteToHex returns the two lowercase hexadecimal digits of one byte. All eight inline copies (native, contracts, domain firmware and desktop, pcap) call it, keeping their own 0x, % or \x notation, and a counted guard with an empty baseline keeps new copies out. Refs #11548 * refactor(kernel): give one-shot SHA-256 hex digests one owner in src/kernel (#11562) sha256Hex digests one in-memory value. A counted guard records the one-shot createHash("sha256").update(x).digest("hex") chains (115 in 101 files) and the first slice migrates domain, contracts and process (35 copies), removing local wrappers (sha256, sha256Text, hash) and the exported sha256Text. Incremental and streamed hashing is not counted. Refs #11548 * refactor(kernel): migrate the application layer's inline error-message copies to errorMessage (#11563) All 69 copies in src/application call errorMessage from src/kernel. Local helpers whose whole body was the copy (errorDetail, causeReason and five local errorMessage definitions) are removed and their call sites call the owner. Refs #11548 * refactor(kernel): migrate the native provider's inline error-message copies to errorMessage (#11567) All 49 copies in src/native call errorMessage from src/kernel. The local errorReason helpers in LldbCallTracer and LldbRetainedObservations, whose whole body was the copy, are removed and their call sites call the owner. Refs #11548 * refactor(kernel): migrate the remaining inline error-message copies to errorMessage (#11569)
143 lines
4.7 KiB
TypeScript
143 lines
4.7 KiB
TypeScript
import { DarwinProcessOwnershipInspectionError } from "./DarwinProcessRunTokenReader.js";
|
|
import {
|
|
assertProcessOwnershipInspectionActive,
|
|
rethrowProcessOwnershipInspectionFailure,
|
|
} from "./ProcessInspectionCancellation.js";
|
|
import type {
|
|
ProcessOwnershipHost,
|
|
ProcessRunTokenObservation,
|
|
ProcessTableEntry,
|
|
} from "./ProcessOwnership.js";
|
|
import { liveProcesses } from "./ProcessOwnershipProcessTree.js";
|
|
import { errorMessage } from "../kernel/errorMessage.js";
|
|
|
|
/** Structured native token availability, distinct from failed reads and caller cancellation. */
|
|
export class ProcessRunTokenReadError extends Error {
|
|
readonly _tag = "ProcessRunTokenReadError";
|
|
constructor(
|
|
readonly pid: number,
|
|
readonly availability: "absent" | "unavailable",
|
|
readonly reason: string,
|
|
) {
|
|
super(`Run token for PID ${String(pid)}: ${reason}`);
|
|
this.name = "ProcessRunTokenReadError";
|
|
}
|
|
}
|
|
|
|
/** Only a variable observed on this process supplies its run token. */
|
|
export const processRunIdFromEnvironment = (
|
|
environment: Readonly<Record<string, string>>,
|
|
): string | undefined =>
|
|
Object.hasOwn(environment, "REA_PROCESS_RUN_ID")
|
|
? environment.REA_PROCESS_RUN_ID
|
|
: undefined;
|
|
|
|
/** Read a selected PID's environment token without requiring process-table relations. */
|
|
export const readProcessRunToken = async (
|
|
host: ProcessOwnershipHost,
|
|
pid: number,
|
|
signal?: AbortSignal,
|
|
): Promise<ProcessRunTokenObservation> => {
|
|
assertProcessOwnershipInspectionActive(signal);
|
|
try {
|
|
const environment =
|
|
signal === undefined
|
|
? await host.environment(pid)
|
|
: await host.environment(pid, signal);
|
|
assertProcessOwnershipInspectionActive(signal);
|
|
return {
|
|
state: "readable",
|
|
runId: processRunIdFromEnvironment(environment),
|
|
};
|
|
} catch (cause: unknown) {
|
|
if (
|
|
cause instanceof DarwinProcessOwnershipInspectionError &&
|
|
(cause.cleanupOwner !== undefined || cause.cleanupFailure !== undefined)
|
|
)
|
|
throw cause;
|
|
rethrowProcessOwnershipInspectionFailure(cause);
|
|
const code =
|
|
cause instanceof Error &&
|
|
"code" in cause &&
|
|
typeof cause.code === "string"
|
|
? cause.code
|
|
: undefined;
|
|
return {
|
|
state: "unavailable",
|
|
...(code === undefined ? {} : { code }),
|
|
reason: errorMessage(cause),
|
|
};
|
|
}
|
|
};
|
|
|
|
/** Read only ownership tokens, using per-process fallback for missing batch rows. */
|
|
export async function* readProcessRunTokens(
|
|
host: ProcessOwnershipHost,
|
|
processes: readonly ProcessTableEntry[],
|
|
signal?: AbortSignal,
|
|
): AsyncGenerator<{
|
|
readonly process: ProcessTableEntry;
|
|
readonly observation: ProcessRunTokenObservation;
|
|
}> {
|
|
assertProcessOwnershipInspectionActive(signal);
|
|
if (processes.length === 0) return;
|
|
let batch: ReadonlyMap<number, ProcessRunTokenObservation> | undefined;
|
|
let batchFailure: string | undefined;
|
|
if (host.runTokens !== undefined) {
|
|
try {
|
|
batch =
|
|
signal === undefined
|
|
? await host.runTokens(processes)
|
|
: await host.runTokens(processes, signal);
|
|
} catch (cause: unknown) {
|
|
if (
|
|
cause instanceof DarwinProcessOwnershipInspectionError &&
|
|
(cause.cleanupOwner !== undefined || cause.cleanupFailure !== undefined)
|
|
)
|
|
throw cause;
|
|
rethrowProcessOwnershipInspectionFailure(cause);
|
|
batchFailure = errorMessage(cause);
|
|
}
|
|
}
|
|
for (const process of processes) {
|
|
assertProcessOwnershipInspectionActive(signal);
|
|
let observation = batch?.get(process.pid);
|
|
if (observation === undefined) {
|
|
observation = await readProcessRunToken(host, process.pid, signal);
|
|
if (observation.state === "unavailable" && batchFailure !== undefined)
|
|
observation = {
|
|
...observation,
|
|
reason: `${observation.reason}; run-token batch failed: ${batchFailure}`,
|
|
};
|
|
}
|
|
assertProcessOwnershipInspectionActive(signal);
|
|
yield { process, observation };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Live processes in the current process table that expose this run token.
|
|
* A process whose token cannot be read is left out, so the list proves
|
|
* presence only; it does not prove that no other carrier exists.
|
|
*/
|
|
export const readRunTokenCarriers = async (
|
|
host: ProcessOwnershipHost,
|
|
runId: string,
|
|
signal?: AbortSignal,
|
|
): Promise<readonly ProcessTableEntry[]> => {
|
|
const carriers: ProcessTableEntry[] = [];
|
|
const processes = liveProcesses(
|
|
signal === undefined
|
|
? await host.listProcesses()
|
|
: await host.listProcesses(signal),
|
|
);
|
|
for await (const { process, observation } of readProcessRunTokens(
|
|
host,
|
|
processes,
|
|
signal,
|
|
))
|
|
if (observation.state === "readable" && observation.runId === runId)
|
|
carriers.push(process);
|
|
return carriers;
|
|
};
|