@claudexor/core 4.1.0 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/env-scope.d.ts +7 -2
- package/dist/env-scope.d.ts.map +1 -1
- package/dist/env-scope.js +12 -3
- package/dist/env-scope.js.map +1 -1
- package/dist/host-facts.d.ts +18 -0
- package/dist/host-facts.d.ts.map +1 -0
- package/dist/host-facts.js +90 -0
- package/dist/host-facts.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/native/claudexor-process-identity +0 -0
- package/dist/proc.d.ts +23 -15
- package/dist/proc.d.ts.map +1 -1
- package/dist/proc.js +31 -37
- package/dist/proc.js.map +1 -1
- package/dist/process-drain.d.ts +31 -0
- package/dist/process-drain.d.ts.map +1 -0
- package/dist/process-drain.js +55 -0
- package/dist/process-drain.js.map +1 -0
- package/dist/process-identity.d.ts +25 -0
- package/dist/process-identity.d.ts.map +1 -1
- package/dist/process-identity.js +91 -8
- package/dist/process-identity.js.map +1 -1
- package/dist/process-lines.d.ts +1 -0
- package/dist/process-lines.d.ts.map +1 -1
- package/dist/process-lines.js +69 -13
- package/dist/process-lines.js.map +1 -1
- package/dist/process-output.d.ts +16 -0
- package/dist/process-output.d.ts.map +1 -0
- package/dist/process-output.js +28 -0
- package/dist/process-output.js.map +1 -0
- package/dist/process-registry.d.ts +15 -3
- package/dist/process-registry.d.ts.map +1 -1
- package/dist/process-registry.js +35 -3
- package/dist/process-registry.js.map +1 -1
- package/dist/process-scope-census.d.ts +196 -0
- package/dist/process-scope-census.d.ts.map +1 -0
- package/dist/process-scope-census.js +444 -0
- package/dist/process-scope-census.js.map +1 -0
- package/dist/process-scope-readers.d.ts +104 -0
- package/dist/process-scope-readers.d.ts.map +1 -0
- package/dist/process-scope-readers.js +429 -0
- package/dist/process-scope-readers.js.map +1 -0
- package/dist/process-scope.d.ts +21 -0
- package/dist/process-scope.d.ts.map +1 -0
- package/dist/process-scope.js +59 -0
- package/dist/process-scope.js.map +1 -0
- package/dist/process-table.d.ts +32 -0
- package/dist/process-table.d.ts.map +1 -0
- package/dist/process-table.js +122 -0
- package/dist/process-table.js.map +1 -0
- package/dist/process-tree.d.ts.map +1 -1
- package/dist/process-tree.js +6 -0
- package/dist/process-tree.js.map +1 -1
- package/dist/runloop.d.ts.map +1 -1
- package/dist/runloop.js +9 -0
- package/dist/runloop.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scope census and reap: find every live process that still carries a scope
|
|
3
|
+
* marker (`process-scope.ts`) in its environment and end it by measured
|
|
4
|
+
* identity. This is the owner of the "a finished run ends what it left" rule:
|
|
5
|
+
* the PPID/process-group ladder (`process-tree.ts`) stays the fast path for a
|
|
6
|
+
* cancel while the tree is intact; a descendant that `setsid`-ed and was
|
|
7
|
+
* reparented to pid 1 after its parent died (the #449 orphans) is reachable
|
|
8
|
+
* only through the marker it inherited — or, where the kernel hides that
|
|
9
|
+
* marker, through the group and birth the daemon RECORDED while the run was
|
|
10
|
+
* live (DIRECTIVE 1): macOS SIP withholds the environment of Apple platform
|
|
11
|
+
* binaries, so membership on POSIX is the UNION of (a) marker members,
|
|
12
|
+
* (b) members of recorded process groups under the pgid-reuse rule and
|
|
13
|
+
* (c) individually recorded descendants, every one signalled by pid after its
|
|
14
|
+
* birth is re-measured (`capturedCandidates` below).
|
|
15
|
+
*
|
|
16
|
+
* Contract (shared with the embedding host):
|
|
17
|
+
* - one asynchronous process-table read per census: macOS streams
|
|
18
|
+
* `ps -E -ww -A -o pid=,command=` line by line and keeps only matching pids
|
|
19
|
+
* (the environment column is never buffered whole, logged or persisted);
|
|
20
|
+
* Linux reads `/proc/<pid>/environ` with bounded concurrency and matches the
|
|
21
|
+
* exact NUL-delimited entry `NAME=1`;
|
|
22
|
+
* - affirmative membership (`confirm`) AND the measured birth identity are
|
|
23
|
+
* checked immediately before every signal; a pid is signalled on its own,
|
|
24
|
+
* never a dead leader's historical process group;
|
|
25
|
+
* - TERM, a bounded grace, then KILL, with a few bounded rescans for forks;
|
|
26
|
+
* - truncation, timeout or reader failure is `failed` — unknown, never an
|
|
27
|
+
* empty scope; a known member whose identity turns unreadable is reported
|
|
28
|
+
* separately from unrelated unreadable processes; a vanished, birth-checked
|
|
29
|
+
* member is gone;
|
|
30
|
+
* - never pid 1, never this process; nothing is ever read from a process's
|
|
31
|
+
* environment except whether the exact marker entry is present.
|
|
32
|
+
*
|
|
33
|
+
* Limit, stated plainly: on macOS a random key sits in a text column that
|
|
34
|
+
* mixes argv and environment. A stranger whose argv literally contains
|
|
35
|
+
* `NAME=1` for a 128-bit random NAME would be censused; the random id makes
|
|
36
|
+
* that practically impossible, but it is not structural proof.
|
|
37
|
+
*
|
|
38
|
+
* Windows has no environment census without native code: coverage there is
|
|
39
|
+
* `tree_only` (the existing taskkill tree kill while the parent chain exists),
|
|
40
|
+
* disclosed, never turned into a failure.
|
|
41
|
+
*/
|
|
42
|
+
import { type AsyncProcessIdentityReader, type KnownProcessIdentity } from "./process-identity.js";
|
|
43
|
+
import { type ScopeCensusReader } from "./process-scope-readers.js";
|
|
44
|
+
import { type CapturedProcess, type ProcessTableReader, type ProcessTableRow } from "./process-table.js";
|
|
45
|
+
export interface ScopeMemberRecord {
|
|
46
|
+
pid: number;
|
|
47
|
+
/** The measured birth token the member was signalled under. */
|
|
48
|
+
startToken: string;
|
|
49
|
+
}
|
|
50
|
+
export interface ScopeUnreadableRecord {
|
|
51
|
+
pid: number;
|
|
52
|
+
reason: string;
|
|
53
|
+
}
|
|
54
|
+
/** What one reap of one marker found and did. Pids and birth tokens only. */
|
|
55
|
+
export interface ScopeReapOutcome {
|
|
56
|
+
/**
|
|
57
|
+
* `marker+capture`: environment census plus the recorded descendants and
|
|
58
|
+
* process groups (POSIX with the daemon index); `marker`: environment census
|
|
59
|
+
* only; `tree_only`: no census on this host (Windows).
|
|
60
|
+
*/
|
|
61
|
+
coverage: "marker+capture" | "marker" | "tree_only";
|
|
62
|
+
census: "ok" | "failed" | "unsupported";
|
|
63
|
+
censusFailure: string | null;
|
|
64
|
+
/** Members found alive, signalled and proven gone. */
|
|
65
|
+
ended: ScopeMemberRecord[];
|
|
66
|
+
/** Members still alive (same identity) when the bounded ladder ended. */
|
|
67
|
+
survivors: ScopeMemberRecord[];
|
|
68
|
+
/** Known members whose identity or membership could not be read; never signalled unverified. */
|
|
69
|
+
unreadable: ScopeUnreadableRecord[];
|
|
70
|
+
/**
|
|
71
|
+
* Recorded groups skipped entirely because the pgid-reuse rule failed (a
|
|
72
|
+
* live process owns the group id with a different birth, or a member was
|
|
73
|
+
* born before the scope): disclosed residual, never signalled.
|
|
74
|
+
*/
|
|
75
|
+
reusedGroups: number[];
|
|
76
|
+
/** True only when every census source succeeded and nothing survived or stayed unreadable. */
|
|
77
|
+
settled: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* The single settlement deadline expired before discovery, confirmation,
|
|
80
|
+
* probing and escalation finished: scanning and signalling stopped at that
|
|
81
|
+
* moment (no late signal), and what is known is disclosed as incomplete —
|
|
82
|
+
* never a verdict.
|
|
83
|
+
*/
|
|
84
|
+
incomplete: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Whether a settled reap PROVES the scope empty. `complete` only on a host
|
|
87
|
+
* whose census reads every same-uid process environment (Linux /proc) with
|
|
88
|
+
* every source succeeding. `not_provable` on macOS even when settled: SIP
|
|
89
|
+
* hides Apple platform-binary environments, so a system binary started in
|
|
90
|
+
* the background by a shell that exited before any capture cannot be ruled
|
|
91
|
+
* out (owner-accepted, disclosed residual); also whenever a census failed or
|
|
92
|
+
* no census exists (Windows). `completenessReason` says which.
|
|
93
|
+
*/
|
|
94
|
+
completeness: "complete" | "not_provable";
|
|
95
|
+
completenessReason: string | null;
|
|
96
|
+
/**
|
|
97
|
+
* The platform hides some processes' environments from the marker census
|
|
98
|
+
* (macOS: SIP withholds KERN_PROCARGS2 for Apple *platform* binaries such as
|
|
99
|
+
* /bin/sleep, /usr/bin/find and /bin/bash). Those are reached through the
|
|
100
|
+
* recorded groups/descendants instead; a platform binary born, setsid-ed and
|
|
101
|
+
* orphaned within one capture tick is the disclosed residual.
|
|
102
|
+
*/
|
|
103
|
+
restricted: boolean;
|
|
104
|
+
durationMs: number;
|
|
105
|
+
}
|
|
106
|
+
export interface ScopeReapOptions {
|
|
107
|
+
marker: string;
|
|
108
|
+
census?: ScopeCensusReader | null;
|
|
109
|
+
identity?: AsyncProcessIdentityReader;
|
|
110
|
+
/**
|
|
111
|
+
* The scope's recorded descendants (DIRECTIVE 1 sources b and c). Present —
|
|
112
|
+
* even empty — means the daemon captured for this scope: coverage is
|
|
113
|
+
* `marker+capture`. Absent: marker census only.
|
|
114
|
+
*/
|
|
115
|
+
captured?: readonly CapturedProcess[];
|
|
116
|
+
/** Birth floor of the scope (the harness root's `lstart` at first capture). */
|
|
117
|
+
scopeStart?: string | null;
|
|
118
|
+
/** Wall-clock floor (ISO) known from the moment the scope was opened; used
|
|
119
|
+
* when no `lstart` floor exists yet (a root that exited within milliseconds). */
|
|
120
|
+
scopeStartAt?: string | null;
|
|
121
|
+
/**
|
|
122
|
+
* The harness roots' own process groups, recorded SYNCHRONOUSLY at spawn
|
|
123
|
+
* from the registry handle: pgid plus the leader's measured identity, and
|
|
124
|
+
* the leader's `lstart` once a table read confirmed it (null until then).
|
|
125
|
+
*/
|
|
126
|
+
rootGroups?: ReadonlyArray<ScopeRootGroup>;
|
|
127
|
+
table?: ProcessTableReader;
|
|
128
|
+
/** Per-pid signal delivery (default `process.kill`). */
|
|
129
|
+
signal?: (pid: number, signal: NodeJS.Signals) => void;
|
|
130
|
+
/** Cooperative grace before KILL (default 1000ms). */
|
|
131
|
+
graceMs?: number;
|
|
132
|
+
/** Overall bound (default graceMs + 4000). */
|
|
133
|
+
deadlineMs?: number;
|
|
134
|
+
/** Probe cadence (default 250ms). */
|
|
135
|
+
probeIntervalMs?: number;
|
|
136
|
+
/** The real-time deadline timer (injectable; default an unref'd setTimeout). */
|
|
137
|
+
deadlineTimer?: (ms: number) => Promise<void>;
|
|
138
|
+
/** Never signalled whatever the census says (default pid 1 and this process). */
|
|
139
|
+
excludePids?: readonly number[];
|
|
140
|
+
platform?: string;
|
|
141
|
+
sleep?: (ms: number) => Promise<void>;
|
|
142
|
+
now?: () => number;
|
|
143
|
+
}
|
|
144
|
+
export interface ScopeRootGroup {
|
|
145
|
+
pgid: number;
|
|
146
|
+
leaderPid: number;
|
|
147
|
+
/** `lstart` text of the leader when a table read confirmed it; null if the root never got one. */
|
|
148
|
+
leaderBirth: string | null;
|
|
149
|
+
/** The leader's spawn-time measured identity (proc identity token). */
|
|
150
|
+
leaderIdentity?: KnownProcessIdentity;
|
|
151
|
+
}
|
|
152
|
+
/** The epoch-seconds floor of a scope: the earlier of its `lstart` and wall-clock floors. */
|
|
153
|
+
export declare function scopeFloorSeconds(scopeStart: string | null | undefined, scopeStartAt: string | null | undefined): number | null;
|
|
154
|
+
/**
|
|
155
|
+
* The candidates of the recorded sources against one fresh table snapshot:
|
|
156
|
+
* every individually captured pid still alive with its recorded birth, plus
|
|
157
|
+
* every live member of a recorded group G under the pgid-reuse rule — POSIX
|
|
158
|
+
* does not reuse a pid as a group id while that group has members, so G is
|
|
159
|
+
* still ours when (i) every live member of G was born at or after the scope
|
|
160
|
+
* and (ii) a live process with pid == G has the birth we recorded for G's
|
|
161
|
+
* leader; otherwise G was reused and is skipped entirely. Pure.
|
|
162
|
+
*/
|
|
163
|
+
export declare function capturedCandidates(captured: readonly CapturedProcess[], rows: readonly ProcessTableRow[], scopeStart: string | null | undefined, options?: {
|
|
164
|
+
scopeStartAt?: string | null;
|
|
165
|
+
rootGroups?: ReadonlyArray<ScopeRootGroup>;
|
|
166
|
+
/** For a root group known only by its leader identity: whether a live
|
|
167
|
+
* process with pid == G is still that leader (computed by the caller). */
|
|
168
|
+
leaderVerdict?: ReadonlyMap<number, "same" | "different">;
|
|
169
|
+
}): {
|
|
170
|
+
candidates: Map<number, string>;
|
|
171
|
+
reusedGroups: number[];
|
|
172
|
+
};
|
|
173
|
+
export declare const DARWIN_COMPLETENESS_LIMIT = "macOS hides Apple platform-binary environments (SIP): a system binary started in the background by a shell that exited before any capture cannot be ruled out";
|
|
174
|
+
/** The completeness verdict of one reap (see `ScopeReapOutcome.completeness`). */
|
|
175
|
+
export declare function reapCompleteness(platform: string, censusFailure: string | null, incomplete?: boolean, censusUnreadable?: number): Pick<ScopeReapOutcome, "completeness" | "completenessReason">;
|
|
176
|
+
/** Default grace between TERM and KILL (ms). */
|
|
177
|
+
export declare const SCOPE_REAP_GRACE_MS = 1000;
|
|
178
|
+
/**
|
|
179
|
+
* The ONE settlement deadline (ms) for a scope: every read, signal and wait of
|
|
180
|
+
* a settlement — the daemon index's wait for in-flight seeds included — races
|
|
181
|
+
* it, and expiry is disclosed as `incomplete`, never extended.
|
|
182
|
+
*/
|
|
183
|
+
export declare function scopeReapDeadlineMs(graceMs?: number): number;
|
|
184
|
+
/**
|
|
185
|
+
* Scope reaper with per-marker coalescing: a second request for a marker
|
|
186
|
+
* whose reap is in flight shares that promise instead of starting a second
|
|
187
|
+
* ladder (different markers keep separate authority and deadlines).
|
|
188
|
+
*/
|
|
189
|
+
export declare class ScopeReaper {
|
|
190
|
+
private readonly inflight;
|
|
191
|
+
reap(opts: ScopeReapOptions): Promise<ScopeReapOutcome>;
|
|
192
|
+
}
|
|
193
|
+
export declare const defaultScopeReaper: ScopeReaper;
|
|
194
|
+
/** End every live process of the scope (see the module doc). */
|
|
195
|
+
export declare function reapScopeMembers(opts: ScopeReapOptions): Promise<ScopeReapOutcome>;
|
|
196
|
+
//# sourceMappingURL=process-scope-census.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-scope-census.d.ts","sourceRoot":"","sources":["../src/process-scope-census.ts"],"names":[],"mappings":"AAAA;;;;;;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EpD,gEAAgE;AAChE,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAElF"}
|
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scope census and reap: find every live process that still carries a scope
|
|
3
|
+
* marker (`process-scope.ts`) in its environment and end it by measured
|
|
4
|
+
* identity. This is the owner of the "a finished run ends what it left" rule:
|
|
5
|
+
* the PPID/process-group ladder (`process-tree.ts`) stays the fast path for a
|
|
6
|
+
* cancel while the tree is intact; a descendant that `setsid`-ed and was
|
|
7
|
+
* reparented to pid 1 after its parent died (the #449 orphans) is reachable
|
|
8
|
+
* only through the marker it inherited — or, where the kernel hides that
|
|
9
|
+
* marker, through the group and birth the daemon RECORDED while the run was
|
|
10
|
+
* live (DIRECTIVE 1): macOS SIP withholds the environment of Apple platform
|
|
11
|
+
* binaries, so membership on POSIX is the UNION of (a) marker members,
|
|
12
|
+
* (b) members of recorded process groups under the pgid-reuse rule and
|
|
13
|
+
* (c) individually recorded descendants, every one signalled by pid after its
|
|
14
|
+
* birth is re-measured (`capturedCandidates` below).
|
|
15
|
+
*
|
|
16
|
+
* Contract (shared with the embedding host):
|
|
17
|
+
* - one asynchronous process-table read per census: macOS streams
|
|
18
|
+
* `ps -E -ww -A -o pid=,command=` line by line and keeps only matching pids
|
|
19
|
+
* (the environment column is never buffered whole, logged or persisted);
|
|
20
|
+
* Linux reads `/proc/<pid>/environ` with bounded concurrency and matches the
|
|
21
|
+
* exact NUL-delimited entry `NAME=1`;
|
|
22
|
+
* - affirmative membership (`confirm`) AND the measured birth identity are
|
|
23
|
+
* checked immediately before every signal; a pid is signalled on its own,
|
|
24
|
+
* never a dead leader's historical process group;
|
|
25
|
+
* - TERM, a bounded grace, then KILL, with a few bounded rescans for forks;
|
|
26
|
+
* - truncation, timeout or reader failure is `failed` — unknown, never an
|
|
27
|
+
* empty scope; a known member whose identity turns unreadable is reported
|
|
28
|
+
* separately from unrelated unreadable processes; a vanished, birth-checked
|
|
29
|
+
* member is gone;
|
|
30
|
+
* - never pid 1, never this process; nothing is ever read from a process's
|
|
31
|
+
* environment except whether the exact marker entry is present.
|
|
32
|
+
*
|
|
33
|
+
* Limit, stated plainly: on macOS a random key sits in a text column that
|
|
34
|
+
* mixes argv and environment. A stranger whose argv literally contains
|
|
35
|
+
* `NAME=1` for a 128-bit random NAME would be censused; the random id makes
|
|
36
|
+
* that practically impossible, but it is not structural proof.
|
|
37
|
+
*
|
|
38
|
+
* Windows has no environment census without native code: coverage there is
|
|
39
|
+
* `tree_only` (the existing taskkill tree kill while the parent chain exists),
|
|
40
|
+
* disclosed, never turned into a failure.
|
|
41
|
+
*/
|
|
42
|
+
import { compareProcessBirth, compareProcessIdentity, defaultAsyncProcessIdentityReader, } from "./process-identity.js";
|
|
43
|
+
import { isScopeMarkerName } from "./process-scope.js";
|
|
44
|
+
import { createScopeCensusReader, LINUX_CONCURRENCY, mapBounded, scopeAgeSeconds, } from "./process-scope-readers.js";
|
|
45
|
+
import { birthEpochSeconds, createProcessTableReader, } from "./process-table.js";
|
|
46
|
+
/** The settlement deadline won a race against a read. */
|
|
47
|
+
const DEADLINE = Symbol("deadline");
|
|
48
|
+
/** The epoch-seconds floor of a scope: the earlier of its `lstart` and wall-clock floors. */
|
|
49
|
+
export function scopeFloorSeconds(scopeStart, scopeStartAt) {
|
|
50
|
+
const fromBirth = scopeStart ? birthEpochSeconds(scopeStart) : null;
|
|
51
|
+
const fromClock = scopeStartAt ? Math.floor(Date.parse(scopeStartAt) / 1000) : null;
|
|
52
|
+
const floors = [fromBirth, fromClock].filter((v) => v !== null && Number.isFinite(v));
|
|
53
|
+
return floors.length ? Math.min(...floors) : null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The candidates of the recorded sources against one fresh table snapshot:
|
|
57
|
+
* every individually captured pid still alive with its recorded birth, plus
|
|
58
|
+
* every live member of a recorded group G under the pgid-reuse rule — POSIX
|
|
59
|
+
* does not reuse a pid as a group id while that group has members, so G is
|
|
60
|
+
* still ours when (i) every live member of G was born at or after the scope
|
|
61
|
+
* and (ii) a live process with pid == G has the birth we recorded for G's
|
|
62
|
+
* leader; otherwise G was reused and is skipped entirely. Pure.
|
|
63
|
+
*/
|
|
64
|
+
export function capturedCandidates(captured, rows, scopeStart, options = {}) {
|
|
65
|
+
const byPid = new Map(rows.map((row) => [row.pid, row]));
|
|
66
|
+
const candidates = new Map();
|
|
67
|
+
const reusedGroups = [];
|
|
68
|
+
for (const item of captured) {
|
|
69
|
+
const row = byPid.get(item.pid);
|
|
70
|
+
if (row && row.birth === item.birth)
|
|
71
|
+
candidates.set(row.pid, row.birth);
|
|
72
|
+
}
|
|
73
|
+
const floor = scopeFloorSeconds(scopeStart, options.scopeStartAt);
|
|
74
|
+
const rootByPgid = new Map((options.rootGroups ?? []).map((g) => [g.pgid, g]));
|
|
75
|
+
const groups = new Set([...captured.map((item) => item.pgid), ...rootByPgid.keys()]);
|
|
76
|
+
for (const group of groups) {
|
|
77
|
+
const members = rows.filter((row) => row.pgid === group);
|
|
78
|
+
if (members.length === 0)
|
|
79
|
+
continue; // emptied: simply gone
|
|
80
|
+
const root = rootByPgid.get(group);
|
|
81
|
+
const recordedLeaderBirth = captured.find((item) => item.pid === group)?.birth ?? root?.leaderBirth ?? undefined;
|
|
82
|
+
const liveLeader = byPid.get(group);
|
|
83
|
+
// Rule (ii): a live process with pid == G must be the recorded leader —
|
|
84
|
+
// by its `lstart` when one was recorded, else by the caller's identity
|
|
85
|
+
// verdict against the spawn-time handle; otherwise G was reused.
|
|
86
|
+
const leaderReused = liveLeader !== undefined &&
|
|
87
|
+
(recordedLeaderBirth !== undefined
|
|
88
|
+
? liveLeader.birth !== recordedLeaderBirth
|
|
89
|
+
: options.leaderVerdict?.get(group) !== "same");
|
|
90
|
+
const bornBefore = floor === null ||
|
|
91
|
+
members.some((row) => {
|
|
92
|
+
const at = birthEpochSeconds(row.birth);
|
|
93
|
+
return at === null || at < floor;
|
|
94
|
+
});
|
|
95
|
+
if (leaderReused || bornBefore) {
|
|
96
|
+
reusedGroups.push(group);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
for (const row of members)
|
|
100
|
+
candidates.set(row.pid, row.birth);
|
|
101
|
+
}
|
|
102
|
+
return { candidates, reusedGroups };
|
|
103
|
+
}
|
|
104
|
+
function unsupportedOutcome(durationMs) {
|
|
105
|
+
return {
|
|
106
|
+
coverage: "tree_only",
|
|
107
|
+
census: "unsupported",
|
|
108
|
+
censusFailure: null,
|
|
109
|
+
ended: [],
|
|
110
|
+
survivors: [],
|
|
111
|
+
unreadable: [],
|
|
112
|
+
reusedGroups: [],
|
|
113
|
+
settled: false,
|
|
114
|
+
incomplete: false,
|
|
115
|
+
completeness: "not_provable",
|
|
116
|
+
completenessReason: "no environment census exists on this platform",
|
|
117
|
+
restricted: false,
|
|
118
|
+
durationMs,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
export const DARWIN_COMPLETENESS_LIMIT = "macOS hides Apple platform-binary environments (SIP): a system binary started in the background by a shell that exited before any capture cannot be ruled out";
|
|
122
|
+
/** The completeness verdict of one reap (see `ScopeReapOutcome.completeness`). */
|
|
123
|
+
export function reapCompleteness(platform, censusFailure, incomplete = false, censusUnreadable = 0) {
|
|
124
|
+
if (censusFailure !== null)
|
|
125
|
+
return { completeness: "not_provable", completenessReason: `census failed: ${censusFailure}` };
|
|
126
|
+
if (incomplete)
|
|
127
|
+
return {
|
|
128
|
+
completeness: "not_provable",
|
|
129
|
+
completenessReason: "settlement deadline expired before every source was read",
|
|
130
|
+
};
|
|
131
|
+
if (platform === "darwin")
|
|
132
|
+
return { completeness: "not_provable", completenessReason: DARWIN_COMPLETENESS_LIMIT };
|
|
133
|
+
if (censusUnreadable > 0)
|
|
134
|
+
return {
|
|
135
|
+
completeness: "not_provable",
|
|
136
|
+
completenessReason: `${censusUnreadable} same-uid process environment(s) were unreadable (other uids cannot inherit our environment through ordinary spawn and are not counted; setuid binaries are a disclosed residual)`,
|
|
137
|
+
};
|
|
138
|
+
return { completeness: "complete", completenessReason: null };
|
|
139
|
+
}
|
|
140
|
+
/** Default grace between TERM and KILL (ms). */
|
|
141
|
+
export const SCOPE_REAP_GRACE_MS = 1_000;
|
|
142
|
+
/**
|
|
143
|
+
* The ONE settlement deadline (ms) for a scope: every read, signal and wait of
|
|
144
|
+
* a settlement — the daemon index's wait for in-flight seeds included — races
|
|
145
|
+
* it, and expiry is disclosed as `incomplete`, never extended.
|
|
146
|
+
*/
|
|
147
|
+
export function scopeReapDeadlineMs(graceMs = SCOPE_REAP_GRACE_MS) {
|
|
148
|
+
return graceMs + 4_000;
|
|
149
|
+
}
|
|
150
|
+
async function reapOnce(opts) {
|
|
151
|
+
const now = opts.now ?? Date.now;
|
|
152
|
+
const sleep = opts.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
153
|
+
const platform = opts.platform ?? process.platform;
|
|
154
|
+
const start = now();
|
|
155
|
+
const census = opts.census === undefined ? createScopeCensusReader(platform) : opts.census;
|
|
156
|
+
if (!census)
|
|
157
|
+
return unsupportedOutcome(now() - start);
|
|
158
|
+
const identity = opts.identity ?? defaultAsyncProcessIdentityReader;
|
|
159
|
+
const captured = opts.captured;
|
|
160
|
+
const table = opts.table ?? (captured || opts.rootGroups ? createProcessTableReader() : null);
|
|
161
|
+
const deliver = opts.signal ?? ((pid, signal) => process.kill(pid, signal));
|
|
162
|
+
const graceMs = opts.graceMs ?? SCOPE_REAP_GRACE_MS;
|
|
163
|
+
const deadlineMs = opts.deadlineMs ?? scopeReapDeadlineMs(graceMs);
|
|
164
|
+
const probeIntervalMs = Math.max(1, opts.probeIntervalMs ?? 250);
|
|
165
|
+
const excluded = new Set(opts.excludePids ?? [1, process.pid]);
|
|
166
|
+
const restricted = platform === "darwin";
|
|
167
|
+
// ONE deadline for the whole settlement: every read (census, confirmation,
|
|
168
|
+
// identity, table) races it, and once it expires nothing is scanned or
|
|
169
|
+
// signalled any more — the outcome is disclosed as incomplete.
|
|
170
|
+
let expired = false;
|
|
171
|
+
const deadlineTimer = opts.deadlineTimer ??
|
|
172
|
+
((ms) => new Promise((resolve) => {
|
|
173
|
+
const timer = setTimeout(resolve, ms);
|
|
174
|
+
timer.unref?.();
|
|
175
|
+
}));
|
|
176
|
+
const expiry = deadlineTimer(deadlineMs).then(() => {
|
|
177
|
+
expired = true;
|
|
178
|
+
});
|
|
179
|
+
void expiry.catch(() => { });
|
|
180
|
+
const pastDeadline = () => expired || now() - start >= deadlineMs;
|
|
181
|
+
// Lazy: no read (on macOS a helper or `ps` process) STARTS past the deadline.
|
|
182
|
+
const bounded = (work) => pastDeadline()
|
|
183
|
+
? Promise.resolve(DEADLINE)
|
|
184
|
+
: Promise.race([work(), expiry.then(() => DEADLINE)]);
|
|
185
|
+
let censusUnreadable = 0;
|
|
186
|
+
const live = new Map();
|
|
187
|
+
const ended = [];
|
|
188
|
+
const unreadable = new Map();
|
|
189
|
+
const reusedGroups = new Set();
|
|
190
|
+
const seen = new Set();
|
|
191
|
+
const signalMember = (member, signal) => {
|
|
192
|
+
if (pastDeadline())
|
|
193
|
+
return; // never a late signal after the deadline
|
|
194
|
+
try {
|
|
195
|
+
deliver(member.pid, signal);
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
const code = error?.code;
|
|
199
|
+
live.delete(member.pid);
|
|
200
|
+
if (code === "ESRCH")
|
|
201
|
+
ended.push({ pid: member.pid, startToken: member.startToken });
|
|
202
|
+
else
|
|
203
|
+
unreadable.set(member.pid, `signal_failed:${code ?? "unknown"}`);
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
/** Admit a censused pid: confirm membership + measure identity, then signal. */
|
|
207
|
+
const admitMarker = async (pid, signal) => {
|
|
208
|
+
if (excluded.has(pid) || seen.has(pid))
|
|
209
|
+
return;
|
|
210
|
+
seen.add(pid);
|
|
211
|
+
// Bracket the membership read with two identity reads: the marker is
|
|
212
|
+
// accepted only for the ONE process that was alive under the same birth
|
|
213
|
+
// before and after `confirm`, so a pid that was freed and reused between
|
|
214
|
+
// the census and the signal can never enrol a stranger (item 7).
|
|
215
|
+
const before = await bounded(() => identity.read(pid));
|
|
216
|
+
if (before === DEADLINE || before.status === "missing")
|
|
217
|
+
return;
|
|
218
|
+
if (before.status === "unknown") {
|
|
219
|
+
unreadable.set(pid, `identity_${before.reason}`);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const member = await bounded(() => census.confirm(pid, opts.marker));
|
|
223
|
+
if (member === DEADLINE || member === false)
|
|
224
|
+
return; // vanished (or reused): not ours any more
|
|
225
|
+
if (member === null) {
|
|
226
|
+
unreadable.set(pid, "membership_unreadable");
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const observed = await bounded(() => identity.read(pid));
|
|
230
|
+
if (observed === DEADLINE || observed.status === "missing")
|
|
231
|
+
return; // gone between confirm and read
|
|
232
|
+
if (observed.status === "unknown") {
|
|
233
|
+
unreadable.set(pid, `identity_${observed.reason}`);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (compareProcessBirth(before, observed) !== "same")
|
|
237
|
+
return; // reused across the bracket
|
|
238
|
+
const entry = {
|
|
239
|
+
pid,
|
|
240
|
+
birth: { kind: "identity", identity: observed },
|
|
241
|
+
startToken: observed.startToken,
|
|
242
|
+
killed: signal === "SIGKILL",
|
|
243
|
+
};
|
|
244
|
+
live.set(pid, entry);
|
|
245
|
+
signalMember(entry, signal);
|
|
246
|
+
};
|
|
247
|
+
/** Admit a recorded candidate: re-measure its birth right before the signal. */
|
|
248
|
+
const admitCaptured = async (pid, expectedBirth, signal) => {
|
|
249
|
+
if (!table || excluded.has(pid) || seen.has(pid) || pastDeadline())
|
|
250
|
+
return;
|
|
251
|
+
seen.add(pid);
|
|
252
|
+
const row = await bounded(() => table.row(pid));
|
|
253
|
+
if (row === DEADLINE || row === null)
|
|
254
|
+
return; // gone (or out of time)
|
|
255
|
+
if (row === "unreadable") {
|
|
256
|
+
unreadable.set(pid, "birth_unreadable");
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (row.birth !== expectedBirth)
|
|
260
|
+
return; // the pid was reused since the snapshot
|
|
261
|
+
const entry = {
|
|
262
|
+
pid,
|
|
263
|
+
birth: { kind: "table", birth: row.birth },
|
|
264
|
+
startToken: `ps:${row.birth}`,
|
|
265
|
+
killed: signal === "SIGKILL",
|
|
266
|
+
};
|
|
267
|
+
live.set(pid, entry);
|
|
268
|
+
signalMember(entry, signal);
|
|
269
|
+
};
|
|
270
|
+
const scanMarker = async (signal) => {
|
|
271
|
+
const ageSeconds = scopeAgeSeconds(opts.scopeStartAt);
|
|
272
|
+
const result = await bounded(() => census.census([opts.marker], ageSeconds, pastDeadline));
|
|
273
|
+
if (result === DEADLINE || pastDeadline())
|
|
274
|
+
return null; // incomplete, never a failed census
|
|
275
|
+
if (result.status !== "ok")
|
|
276
|
+
return result.reason;
|
|
277
|
+
censusUnreadable = Math.max(censusUnreadable, result.unreadable);
|
|
278
|
+
const pids = result.members.get(opts.marker) ?? [];
|
|
279
|
+
await mapBounded(pids, LINUX_CONCURRENCY, (pid) => admitMarker(pid, signal));
|
|
280
|
+
return null;
|
|
281
|
+
};
|
|
282
|
+
const scanCaptured = async (signal) => {
|
|
283
|
+
if ((!captured && !opts.rootGroups) || !table || pastDeadline())
|
|
284
|
+
return null;
|
|
285
|
+
const rows = await bounded(() => table.snapshot());
|
|
286
|
+
if (rows === DEADLINE)
|
|
287
|
+
return null;
|
|
288
|
+
if (rows === null)
|
|
289
|
+
return "process table unreadable";
|
|
290
|
+
// A root group known only by its leader's identity (no lstart yet): if a
|
|
291
|
+
// live process owns pid == G, measure it against the spawn-time identity.
|
|
292
|
+
const leaderVerdict = new Map();
|
|
293
|
+
for (const root of opts.rootGroups ?? []) {
|
|
294
|
+
if (root.leaderBirth !== null || !root.leaderIdentity)
|
|
295
|
+
continue;
|
|
296
|
+
if (!rows.some((row) => row.pid === root.pgid))
|
|
297
|
+
continue;
|
|
298
|
+
const observed = await bounded(() => identity.read(root.pgid));
|
|
299
|
+
if (observed === DEADLINE)
|
|
300
|
+
return null;
|
|
301
|
+
leaderVerdict.set(root.pgid, compareProcessIdentity(root.leaderIdentity, observed) === "same" ? "same" : "different");
|
|
302
|
+
}
|
|
303
|
+
const found = capturedCandidates(captured ?? [], rows, opts.scopeStart, {
|
|
304
|
+
scopeStartAt: opts.scopeStartAt,
|
|
305
|
+
rootGroups: opts.rootGroups,
|
|
306
|
+
leaderVerdict,
|
|
307
|
+
});
|
|
308
|
+
for (const group of found.reusedGroups)
|
|
309
|
+
reusedGroups.add(group);
|
|
310
|
+
await mapBounded([...found.candidates], LINUX_CONCURRENCY, ([pid, birth]) => admitCaptured(pid, birth, signal));
|
|
311
|
+
return null;
|
|
312
|
+
};
|
|
313
|
+
const scan = async (signal) => {
|
|
314
|
+
const markerFailure = await scanMarker(signal);
|
|
315
|
+
const capturedFailure = await scanCaptured(signal);
|
|
316
|
+
return markerFailure ?? capturedFailure;
|
|
317
|
+
};
|
|
318
|
+
/** Is the member still the process we signalled? Its birth decides, not its current group. */
|
|
319
|
+
const stillSame = async (member) => {
|
|
320
|
+
if (member.birth.kind === "identity") {
|
|
321
|
+
const observed = await bounded(() => identity.read(member.pid));
|
|
322
|
+
if (observed === DEADLINE)
|
|
323
|
+
return "same"; // out of time: still a survivor, never a false "gone"
|
|
324
|
+
const comparison = compareProcessBirth(member.birth.identity, observed);
|
|
325
|
+
if (comparison === "same")
|
|
326
|
+
return "same";
|
|
327
|
+
if (comparison === "unknown")
|
|
328
|
+
return "unreadable";
|
|
329
|
+
return "gone"; // missing, or a different process now owns the pid
|
|
330
|
+
}
|
|
331
|
+
if (!table)
|
|
332
|
+
return "unreadable";
|
|
333
|
+
const row = await bounded(() => table.row(member.pid));
|
|
334
|
+
if (row === DEADLINE)
|
|
335
|
+
return "same";
|
|
336
|
+
if (row === null)
|
|
337
|
+
return "gone";
|
|
338
|
+
if (row === "unreadable")
|
|
339
|
+
return "unreadable";
|
|
340
|
+
return row.birth === member.birth.birth ? "same" : "gone";
|
|
341
|
+
};
|
|
342
|
+
const probe = async () => {
|
|
343
|
+
await mapBounded([...live.values()], LINUX_CONCURRENCY, async (member) => {
|
|
344
|
+
const state = await stillSame(member);
|
|
345
|
+
if (state === "same")
|
|
346
|
+
return;
|
|
347
|
+
live.delete(member.pid);
|
|
348
|
+
if (state === "unreadable")
|
|
349
|
+
unreadable.set(member.pid, "identity_unreadable");
|
|
350
|
+
else
|
|
351
|
+
ended.push({ pid: member.pid, startToken: member.startToken });
|
|
352
|
+
});
|
|
353
|
+
};
|
|
354
|
+
const outcome = (censusFailure) => ({
|
|
355
|
+
coverage: captured || opts.rootGroups ? "marker+capture" : "marker",
|
|
356
|
+
census: censusFailure === null ? "ok" : "failed",
|
|
357
|
+
censusFailure,
|
|
358
|
+
ended,
|
|
359
|
+
survivors: [...live.values()].map((m) => ({ pid: m.pid, startToken: m.startToken })),
|
|
360
|
+
unreadable: [...unreadable].map(([pid, reason]) => ({ pid, reason })),
|
|
361
|
+
reusedGroups: [...reusedGroups],
|
|
362
|
+
settled: censusFailure === null && !pastDeadline() && live.size === 0 && unreadable.size === 0,
|
|
363
|
+
incomplete: pastDeadline(),
|
|
364
|
+
...reapCompleteness(platform, censusFailure, pastDeadline(), censusUnreadable),
|
|
365
|
+
restricted,
|
|
366
|
+
durationMs: now() - start,
|
|
367
|
+
});
|
|
368
|
+
let censusFailure = await scan("SIGTERM");
|
|
369
|
+
if (live.size === 0) {
|
|
370
|
+
// Members that were already gone at the first signal (ESRCH) count as
|
|
371
|
+
// ended; a fork after that snapshot would be missed without one more look.
|
|
372
|
+
if (ended.length > 0 && !pastDeadline())
|
|
373
|
+
censusFailure ??= await scan("SIGTERM");
|
|
374
|
+
if (live.size === 0)
|
|
375
|
+
return outcome(censusFailure);
|
|
376
|
+
}
|
|
377
|
+
let escalated = false;
|
|
378
|
+
let rescans = 0;
|
|
379
|
+
for (;;) {
|
|
380
|
+
if (pastDeadline())
|
|
381
|
+
return outcome(censusFailure);
|
|
382
|
+
await sleep(probeIntervalMs);
|
|
383
|
+
if (pastDeadline())
|
|
384
|
+
return outcome(censusFailure); // members stay survivors; no probe starts
|
|
385
|
+
await probe();
|
|
386
|
+
if (pastDeadline())
|
|
387
|
+
return outcome(censusFailure);
|
|
388
|
+
const elapsed = now() - start;
|
|
389
|
+
if (elapsed >= graceMs && !escalated) {
|
|
390
|
+
escalated = true;
|
|
391
|
+
// One bounded rescan for forks before the hard signal; late arrivals get KILL.
|
|
392
|
+
rescans += 1;
|
|
393
|
+
censusFailure ??= await scan("SIGKILL");
|
|
394
|
+
for (const member of live.values()) {
|
|
395
|
+
if (member.killed)
|
|
396
|
+
continue;
|
|
397
|
+
if ((await stillSame(member)) !== "same")
|
|
398
|
+
continue;
|
|
399
|
+
member.killed = true;
|
|
400
|
+
signalMember(member, "SIGKILL");
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
if (live.size === 0) {
|
|
404
|
+
if (rescans < 2) {
|
|
405
|
+
rescans += 1;
|
|
406
|
+
censusFailure ??= await scan(escalated ? "SIGKILL" : "SIGTERM");
|
|
407
|
+
if (live.size === 0)
|
|
408
|
+
return outcome(censusFailure);
|
|
409
|
+
}
|
|
410
|
+
else
|
|
411
|
+
return outcome(censusFailure);
|
|
412
|
+
}
|
|
413
|
+
if (pastDeadline())
|
|
414
|
+
return outcome(censusFailure);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Scope reaper with per-marker coalescing: a second request for a marker
|
|
419
|
+
* whose reap is in flight shares that promise instead of starting a second
|
|
420
|
+
* ladder (different markers keep separate authority and deadlines).
|
|
421
|
+
*/
|
|
422
|
+
export class ScopeReaper {
|
|
423
|
+
inflight = new Map();
|
|
424
|
+
reap(opts) {
|
|
425
|
+
if (!isScopeMarkerName(opts.marker)) {
|
|
426
|
+
return Promise.reject(new Error("scope marker name does not match the grammar"));
|
|
427
|
+
}
|
|
428
|
+
const pending = this.inflight.get(opts.marker);
|
|
429
|
+
if (pending)
|
|
430
|
+
return pending;
|
|
431
|
+
const job = reapOnce(opts).finally(() => {
|
|
432
|
+
if (this.inflight.get(opts.marker) === job)
|
|
433
|
+
this.inflight.delete(opts.marker);
|
|
434
|
+
});
|
|
435
|
+
this.inflight.set(opts.marker, job);
|
|
436
|
+
return job;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
export const defaultScopeReaper = new ScopeReaper();
|
|
440
|
+
/** End every live process of the scope (see the module doc). */
|
|
441
|
+
export function reapScopeMembers(opts) {
|
|
442
|
+
return defaultScopeReaper.reap(opts);
|
|
443
|
+
}
|
|
444
|
+
//# sourceMappingURL=process-scope-census.js.map
|