mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 05:02:40 +00:00
* fix(process): preserve producer-owned inspection cancellation Prototype the explicit inspection origin and retained file/batch owner protocol. Downstream adapter migration and lease binding remain required before integration. * fix(process): type inspection ownership handoffs precisely * fix(process): retain plain inspection preparation cancellation * fix(process): retain explicit inspection cleanup and cancellation facts * fix(process): require explicit inspection facts and cleanup scope * refactor(process): remove redundant cleanup forwarding wrapper * fix(firmware): retain inspection cancellation and cleanup ownership * fix(wakaru): retain typed inspection outcomes and caller scope * fix(ghidra): preserve inspection facts and launch cleanup scope * fix(providers): preserve typed process inspection outcomes * refactor(process): remove obsolete inspection cancellation re-export * fix(native): preserve inspection preparation cleanup and cancellation * fix(registration): retain typed inspection cleanup during Git discovery * fix(process): preserve inspection deadline origin in adapter handoff * fix(process): consume the canonical inspection selector in binding * fix(native): preserve inspection deadlines and cleanup retry completion * test(process): narrow the retained Darwin preparation owner * fix(providers): preserve preparation ownership and timeout policy Narrow preparation facts through their canonical owner, preserve actual cleanup capabilities and recorded cancellation, and keep enclosing command deadlines authoritative. Retain raw inspection records through Java refusal and configuration projections without changing availability fallback policy. * refactor(process): use one inspection failure type owner * fix(providers): retain recorded secondary failures when rebinding * fix(ghidra): preserve normalized inspection facts through public projection * fix(process): normalize native process listing cancellation * docs(development): clarify failure provenance and cleanup verification * fix(process): preserve direct inspection cancellation facts * fix(process): retain inspection owners through termination * fix(process): preserve command inspection cleanup provenance * fix(desktop): preserve inspection facts after cleanup retry * refactor(process)!: migrate desktop capture with inspection producers * fix(desktop)!: preserve capture failure origins and cleanup owners (#10115) * fix(desktop): preserve capture failure origins and cleanup * fix(desktop): type keyring cleanup cancellation * fix(kuna)!: retain copy cleanup before retiring workspaces (#10114) * fix(kuna): own copy cancellation and descriptor cleanup * fix(kuna): close copy descriptors before workspace removal * test(kuna): type copy lifecycle gates for project library * docs(kuna): explain retained copy-file cleanup and staging ownership * docs(kuna): separate cache publication, integrity and cleanup guidance * fix(desktop): retain sampling inspection owners before release * fix(process): preserve capture file read cancellation origin * fix(process): retain capture inspection cancellation and cleanup owners * fix(process): retain sampling inspection failures in capture scope * fix(process): retain inspection failures through mandatory cleanup * fix(process): distinguish sampler shutdown from capture cancellation * fix(browser): retain startup EOF before later cancellation * fix(process): bind capture inspection cleanup to the owning run * test(process): migrate cancellation fixtures to typed owners Record explicit preparation and command cancellation facts, retain exact opaque reasons and physical causes, and guard the mocked version probe from native preparation. Keep the live Wakaru command control while adding a recording equivalent for bounded source verification. * fix(process): own sampling reads and settle inspection peers * fix(errors): retain authored recovery through cleanup composition * fix(process): retain all sampling inspection leaves for final cleanup * test(browser): report recording cleanup signal outcome * fix(process): keep inspection error context out of implicit JSON * fix(desktop): retain normalized sampling cleanup ownership * test(process): assert tagged startup cancellation
153 lines
5.0 KiB
TypeScript
153 lines
5.0 KiB
TypeScript
import { err, ok, type Result } from "../domain/result.js";
|
|
import { performance } from "node:perf_hooks";
|
|
import { setTimeout as delay } from "node:timers/promises";
|
|
|
|
import { processIdentityHasExited } from "./ProcessLifecycle.js";
|
|
import type {
|
|
OwnedProcessGroup,
|
|
ProcessOwnershipFact,
|
|
ProcessOwnershipHost,
|
|
} from "./ProcessOwnership.js";
|
|
import {
|
|
readNativeProcessIdentityMap,
|
|
readProcessOwnershipFacts,
|
|
systemProcessOwnershipHost,
|
|
} from "./ProcessOwnershipObservation.js";
|
|
import { readRunTokenCarriers } from "./ProcessRunTokenObservations.js";
|
|
|
|
const EXIT_POLL_INTERVAL_MS = 100;
|
|
|
|
/** Operating-system seams for a graceful stop; replaceable in tests. */
|
|
export interface TokenOwnedTerminationHost {
|
|
readonly ownership: ProcessOwnershipHost;
|
|
readonly signalProcess: (pid: number, signal: NodeJS.Signals) => void;
|
|
}
|
|
|
|
const systemTerminationHost: TokenOwnedTerminationHost = {
|
|
ownership: systemProcessOwnershipHost,
|
|
signalProcess: (pid, signal) => {
|
|
process.kill(pid, signal);
|
|
},
|
|
};
|
|
|
|
/** Live processes whose run token was read between two matching identity reads. */
|
|
const tokenOwnedFacts = async (
|
|
runId: string,
|
|
host: ProcessOwnershipHost,
|
|
): Promise<readonly ProcessOwnershipFact[]> =>
|
|
(
|
|
await readProcessOwnershipFacts(
|
|
await readRunTokenCarriers(host, runId),
|
|
host,
|
|
)
|
|
).filter(
|
|
(fact) =>
|
|
fact.state === "same-generation" &&
|
|
fact.token.state === "readable" &&
|
|
fact.token.runId === runId &&
|
|
!processIdentityHasExited(fact.identityAfter),
|
|
);
|
|
|
|
/** The group this read observed, or undefined when membership is unknown; an older table row never grants group authority. */
|
|
const processGroupOf = ({ identityAfter }: ProcessOwnershipFact) =>
|
|
identityAfter.state === "readable" &&
|
|
identityAfter.relation?.state === "observed"
|
|
? identityAfter.relation.processGroupId
|
|
: undefined;
|
|
|
|
const identityOf = ({ identityAfter }: ProcessOwnershipFact) =>
|
|
identityAfter.state === "readable" ? identityAfter.identity : undefined;
|
|
|
|
/** Poll until every signalled process has exited or been replaced, or the deadline passes. */
|
|
const awaitExit = async (
|
|
signalled: readonly ProcessOwnershipFact[],
|
|
deadline: number,
|
|
host: ProcessOwnershipHost,
|
|
): Promise<void> => {
|
|
let pending = signalled;
|
|
while (pending.length > 0) {
|
|
const remaining = deadline - performance.now();
|
|
if (remaining <= 0) return;
|
|
await delay(Math.min(EXIT_POLL_INTERVAL_MS, remaining));
|
|
const current = await readNativeProcessIdentityMap(
|
|
pending.map(({ process }) => process),
|
|
host,
|
|
);
|
|
pending = pending.filter((fact) => {
|
|
const now = current.get(fact.process.pid);
|
|
return (
|
|
!processIdentityHasExited(now) &&
|
|
!(now?.state === "readable" && now.identity !== identityOf(fact))
|
|
);
|
|
});
|
|
}
|
|
};
|
|
|
|
const send = (deliver: () => void): boolean => {
|
|
try {
|
|
deliver();
|
|
return true;
|
|
} catch {
|
|
// Exit or a failed delivery does not establish that SIGTERM was sent.
|
|
return false;
|
|
}
|
|
};
|
|
|
|
export class TokenOwnedTerminationFailure extends Error {
|
|
readonly _tag = "TokenOwnedTerminationFailure";
|
|
constructor(
|
|
cause: unknown,
|
|
readonly launchGroupSignalled: boolean,
|
|
) {
|
|
super("Token-owned process termination inspection failed", { cause });
|
|
this.name = this._tag;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ask an owned run to exit before it is killed: send one SIGTERM to its
|
|
* launch process group, while a member proven to carry the run token keeps
|
|
* that group id from being reused, and one to every other process proven to
|
|
* carry the token, then wait up to `graceMs` for them to exit. Anything still
|
|
* running is left to the caller's verified cleanup. Return the delivered
|
|
* launch-group flag; failed inspection retains its exact cause and any group
|
|
* signal already delivered. When the group was not signalled, the caller
|
|
* still owns asking its launcher to exit.
|
|
*/
|
|
export const terminateTokenOwnedProcesses = async (
|
|
ownership: Pick<OwnedProcessGroup, "runId" | "processGroupId">,
|
|
graceMs: number,
|
|
host: TokenOwnedTerminationHost = systemTerminationHost,
|
|
): Promise<
|
|
Result<
|
|
{ readonly launchGroupSignalled: boolean },
|
|
TokenOwnedTerminationFailure
|
|
>
|
|
> => {
|
|
const deadline = performance.now() + graceMs;
|
|
let launchGroupSignalled = false;
|
|
try {
|
|
const owned = await tokenOwnedFacts(ownership.runId, host.ownership);
|
|
const inGroup = (fact: ProcessOwnershipFact) =>
|
|
processGroupOf(fact) === ownership.processGroupId;
|
|
launchGroupSignalled =
|
|
owned.some(inGroup) &&
|
|
send(() => {
|
|
host.ownership.signalGroup(ownership.processGroupId, "SIGTERM");
|
|
});
|
|
const signalled = launchGroupSignalled ? owned.filter(inGroup) : [];
|
|
for (const fact of owned)
|
|
if (
|
|
!inGroup(fact) &&
|
|
send(() => {
|
|
host.signalProcess(fact.process.pid, "SIGTERM");
|
|
})
|
|
)
|
|
signalled.push(fact);
|
|
await awaitExit(signalled, deadline, host.ownership);
|
|
return ok({ launchGroupSignalled });
|
|
} catch (cause: unknown) {
|
|
return err(new TokenOwnedTerminationFailure(cause, launchGroupSignalled));
|
|
}
|
|
};
|