@jini-ai/sidecar 0.7.1 → 0.7.3-next.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/CHANGELOG.md +1 -1
- package/dist/daemon-registry.d.ts +7 -5
- package/dist/daemon-registry.js +5 -5
- package/dist/index.d.ts +1 -1
- package/dist/ipc-path.d.ts +2 -2
- package/dist/ipc-path.js +2 -2
- package/dist/json-file.d.ts +11 -7
- package/dist/json-file.js +7 -8
- package/dist/json-ipc.d.ts +1 -18
- package/dist/json-ipc.js +11 -35
- package/dist/net.d.ts +7 -2
- package/dist/net.js +2 -2
- package/dist/paths.d.ts +11 -11
- package/dist/paths.js +11 -11
- package/dist/port.js +4 -4
- package/dist/supervision/child-entries.d.ts +85 -0
- package/dist/supervision/child-entries.js +139 -0
- package/dist/supervision/child-exit-signal.d.ts +21 -0
- package/dist/supervision/child-exit-signal.js +24 -0
- package/dist/supervision/child-orphans.d.ts +37 -0
- package/dist/supervision/child-orphans.js +48 -0
- package/dist/supervision/child-process-inspection.d.ts +29 -0
- package/dist/supervision/child-process-inspection.js +32 -0
- package/dist/supervision/child-readiness.d.ts +57 -0
- package/dist/supervision/child-readiness.js +83 -0
- package/dist/supervision/child-registry.d.ts +98 -0
- package/dist/supervision/child-registry.js +118 -0
- package/dist/supervision/child-stop.d.ts +39 -0
- package/dist/supervision/child-stop.js +81 -0
- package/dist/supervision/child-transitions.d.ts +54 -0
- package/dist/supervision/child-transitions.js +62 -0
- package/dist/supervision/keyed-serializer.d.ts +29 -0
- package/dist/supervision/keyed-serializer.js +50 -0
- package/dist/supervision/supervisor-node.d.ts +29 -11
- package/dist/supervision/supervisor-node.js +40 -29
- package/dist/supervision/supervisor.d.ts +3 -0
- package/dist/supervision/supervisor.js +312 -276
- package/package.json +4 -5
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The one holder of "which site dirs have a LIVE child server right now" — the host's
|
|
3
|
+
* `openSites` map, grown the one property it never had: it observes the child dying.
|
|
4
|
+
*
|
|
5
|
+
* **The defect this exists for.** `openSites` was a bare `Map`, and the audit's phrasing is
|
|
6
|
+
* exact: it was "a registry of what was STARTED and not yet deliberately stopped", read everywhere
|
|
7
|
+
* as "what is running". the host's server's handle was built for boot-or-fail and discarded the
|
|
8
|
+
* child's exit once ready, so after a crash:
|
|
9
|
+
*
|
|
10
|
+
* - the host snapshot builder (the host lifecycle handler) kept reporting `status: "running"` and the old port;
|
|
11
|
+
* - the host poller's 4 s re-poll — whose own comment claims it is there to catch a crash —
|
|
12
|
+
* re-read a map that never changed, so the comment was false;
|
|
13
|
+
* - the host open operation (the host) returned `already.server`, so "Start site" handed back the corpse
|
|
14
|
+
* and spawned nothing. Wedged for the rest of the session.
|
|
15
|
+
*
|
|
16
|
+
* Nothing was wrong with any individual reader. There was simply no component that owned the
|
|
17
|
+
* `running → exited` transition, so every reader of "running" was reading "was started". This is
|
|
18
|
+
* that component.
|
|
19
|
+
*
|
|
20
|
+
* **Deliberately Map-compatible.** `get`/`set`/`has`/`delete`/`values`/`size` behave exactly as the
|
|
21
|
+
* `Map` they replace, so every existing call site and every test that injects a plain `Map` keeps
|
|
22
|
+
* working unchanged. What is added is invisible to those callers: `set` also attaches the exit
|
|
23
|
+
* observation, `delete` also withdraws it, and an exit that arrives while an entry is still current
|
|
24
|
+
* removes it and reports through `onUnexpectedExit`.
|
|
25
|
+
*
|
|
26
|
+
* **Why DELIBERATE closes are not this module's business.** `handleDelete` and the window `closed`
|
|
27
|
+
* listener already drop the entry and the crash-safety row themselves, in that order, for reasons
|
|
28
|
+
* their own comments give. Taking that over would rewrite two working paths to fix a third. This
|
|
29
|
+
* module owns exactly the transition that had no owner: the one nobody asked for.
|
|
30
|
+
*
|
|
31
|
+
* No `electron` import, so all of it is testable under plain `node --test` — same convention as
|
|
32
|
+
* the other host-independent lifecycle modules.
|
|
33
|
+
*/
|
|
34
|
+
/** How a site's child exited: the `code` and `signal` of Node's own child `exit` event. */
|
|
35
|
+
interface ChildExit {
|
|
36
|
+
code: number | null;
|
|
37
|
+
signal: string | null;
|
|
38
|
+
}
|
|
39
|
+
/** The slice of a the host's server handle this module reads. `onExit` is optional: see `set`. */
|
|
40
|
+
interface ObservedServer {
|
|
41
|
+
onExit?: (required: {
|
|
42
|
+
listener: (exit: ChildExit) => void;
|
|
43
|
+
}, optional?: Record<string, never>) => void;
|
|
44
|
+
}
|
|
45
|
+
/** The least a stored entry carries. The caller's real entry type (the server handle's `port` and
|
|
46
|
+
* `pid`, the site's window) passes through as `E`. */
|
|
47
|
+
interface ChildEntry {
|
|
48
|
+
server: ObservedServer;
|
|
49
|
+
}
|
|
50
|
+
/** {@link createChildEntries}'s dependencies. */
|
|
51
|
+
interface ChildEntriesRequired<E extends ChildEntry> {
|
|
52
|
+
onUnexpectedExit: (input: {
|
|
53
|
+
key: string;
|
|
54
|
+
exit: ChildExit;
|
|
55
|
+
entry: E;
|
|
56
|
+
}) => void;
|
|
57
|
+
}
|
|
58
|
+
/** What {@link createChildEntries} returns: `Map<key, E>`'s surface plus {@link lastExitOf}. */
|
|
59
|
+
interface ChildEntries<E extends ChildEntry> {
|
|
60
|
+
set(key: string, entry: E): ChildEntries<E>;
|
|
61
|
+
delete(key: string): boolean;
|
|
62
|
+
get(key: string): E | undefined;
|
|
63
|
+
has(key: string): boolean;
|
|
64
|
+
values(): MapIterator<E>;
|
|
65
|
+
readonly size: number;
|
|
66
|
+
lastExitOf({ key }: {
|
|
67
|
+
key: string;
|
|
68
|
+
}, optional?: Record<string, never>): ChildExit | null;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* @param deps
|
|
72
|
+
* @param deps.onUnexpectedExit called once per site whose child dies without a {@link delete} first.
|
|
73
|
+
* Receives the entry that was holding it, because the caller needs its `server.pid` to drop the
|
|
74
|
+
* right crash-safety row — this instance's own registry file can also hold a still-draining earlier
|
|
75
|
+
* child's row for the same site dir, and closing by site dir alone would take that one too.
|
|
76
|
+
* Injected rather than reaching for the host registry directly so this module needs no
|
|
77
|
+
* `registryPath`, no filesystem, and no knowledge of what the caller does about it — the host
|
|
78
|
+
* drops the crash-safety row and logs; a test just records the call.
|
|
79
|
+
* @returns a `Map`-compatible store with {@link lastExitOf} added.
|
|
80
|
+
* @complexity O(1) to construct.
|
|
81
|
+
*/
|
|
82
|
+
declare function createChildEntries<E extends ChildEntry>(deps: ChildEntriesRequired<E>, _optional?: Record<string, never>): ChildEntries<E>;
|
|
83
|
+
export { createChildEntries };
|
|
84
|
+
export type { ChildExit, ChildEntry, ChildEntries, ChildEntriesRequired, ObservedServer };
|
|
85
|
+
//# sourceMappingURL=child-entries.d.ts.map
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The one holder of "which site dirs have a LIVE child server right now" — the host's
|
|
3
|
+
* `openSites` map, grown the one property it never had: it observes the child dying.
|
|
4
|
+
*
|
|
5
|
+
* **The defect this exists for.** `openSites` was a bare `Map`, and the audit's phrasing is
|
|
6
|
+
* exact: it was "a registry of what was STARTED and not yet deliberately stopped", read everywhere
|
|
7
|
+
* as "what is running". the host's server's handle was built for boot-or-fail and discarded the
|
|
8
|
+
* child's exit once ready, so after a crash:
|
|
9
|
+
*
|
|
10
|
+
* - the host snapshot builder (the host lifecycle handler) kept reporting `status: "running"` and the old port;
|
|
11
|
+
* - the host poller's 4 s re-poll — whose own comment claims it is there to catch a crash —
|
|
12
|
+
* re-read a map that never changed, so the comment was false;
|
|
13
|
+
* - the host open operation (the host) returned `already.server`, so "Start site" handed back the corpse
|
|
14
|
+
* and spawned nothing. Wedged for the rest of the session.
|
|
15
|
+
*
|
|
16
|
+
* Nothing was wrong with any individual reader. There was simply no component that owned the
|
|
17
|
+
* `running → exited` transition, so every reader of "running" was reading "was started". This is
|
|
18
|
+
* that component.
|
|
19
|
+
*
|
|
20
|
+
* **Deliberately Map-compatible.** `get`/`set`/`has`/`delete`/`values`/`size` behave exactly as the
|
|
21
|
+
* `Map` they replace, so every existing call site and every test that injects a plain `Map` keeps
|
|
22
|
+
* working unchanged. What is added is invisible to those callers: `set` also attaches the exit
|
|
23
|
+
* observation, `delete` also withdraws it, and an exit that arrives while an entry is still current
|
|
24
|
+
* removes it and reports through `onUnexpectedExit`.
|
|
25
|
+
*
|
|
26
|
+
* **Why DELIBERATE closes are not this module's business.** `handleDelete` and the window `closed`
|
|
27
|
+
* listener already drop the entry and the crash-safety row themselves, in that order, for reasons
|
|
28
|
+
* their own comments give. Taking that over would rewrite two working paths to fix a third. This
|
|
29
|
+
* module owns exactly the transition that had no owner: the one nobody asked for.
|
|
30
|
+
*
|
|
31
|
+
* No `electron` import, so all of it is testable under plain `node --test` — same convention as
|
|
32
|
+
* the other host-independent lifecycle modules.
|
|
33
|
+
*/
|
|
34
|
+
/**
|
|
35
|
+
* @param deps
|
|
36
|
+
* @param deps.onUnexpectedExit called once per site whose child dies without a {@link delete} first.
|
|
37
|
+
* Receives the entry that was holding it, because the caller needs its `server.pid` to drop the
|
|
38
|
+
* right crash-safety row — this instance's own registry file can also hold a still-draining earlier
|
|
39
|
+
* child's row for the same site dir, and closing by site dir alone would take that one too.
|
|
40
|
+
* Injected rather than reaching for the host registry directly so this module needs no
|
|
41
|
+
* `registryPath`, no filesystem, and no knowledge of what the caller does about it — the host
|
|
42
|
+
* drops the crash-safety row and logs; a test just records the call.
|
|
43
|
+
* @returns a `Map`-compatible store with {@link lastExitOf} added.
|
|
44
|
+
* @complexity O(1) to construct.
|
|
45
|
+
*/
|
|
46
|
+
function createChildEntries(deps, _optional = {}) {
|
|
47
|
+
return new ChildEntriesStore(deps);
|
|
48
|
+
}
|
|
49
|
+
class ChildEntriesStore {
|
|
50
|
+
deps;
|
|
51
|
+
/** Live entries only. */
|
|
52
|
+
live = new Map();
|
|
53
|
+
/** Why the last child died. */
|
|
54
|
+
exits = new Map();
|
|
55
|
+
constructor(deps) {
|
|
56
|
+
this.deps = deps;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Handle one child's exit.
|
|
60
|
+
*
|
|
61
|
+
* The identity check is the whole correctness of this function. A listener fires against the
|
|
62
|
+
* entry it was attached for, and by the time it fires that entry may already have been deleted
|
|
63
|
+
* (a deliberate stop — whose `server.stop()` is itself what triggered the exit) or REPLACED by a
|
|
64
|
+
* later `set` for the same site dir. Acting on either would drop a healthy entry that has nothing
|
|
65
|
+
* to do with the process that just died. Comparing the entry object identity, not the site dir,
|
|
66
|
+
* is what makes "is this exit still about the thing I am holding" answerable.
|
|
67
|
+
*
|
|
68
|
+
* @complexity O(1).
|
|
69
|
+
*/
|
|
70
|
+
handleExit({ key, entry, exit }, _optional = {}) {
|
|
71
|
+
if (this.live.get(key) !== entry)
|
|
72
|
+
return;
|
|
73
|
+
this.live.delete(key);
|
|
74
|
+
this.exits.set(key, exit);
|
|
75
|
+
this.deps.onUnexpectedExit({ key, exit, entry });
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Record a site's live server and begin watching it. `Map.set`'s contract plus the observation.
|
|
79
|
+
*
|
|
80
|
+
* A handle with no `onExit` is stored and simply not watched, rather than throwing: the sites-home
|
|
81
|
+
* and own-server paths both build real handles from the host's server, but tests inject minimal
|
|
82
|
+
* `{server: {port}}` stand-ins, and a store that refused those would force every unrelated test
|
|
83
|
+
* to fake a lifecycle it is not exercising.
|
|
84
|
+
*
|
|
85
|
+
* @complexity O(1).
|
|
86
|
+
*/
|
|
87
|
+
set(key, entry) {
|
|
88
|
+
this.live.set(key, entry);
|
|
89
|
+
// A previous crash at this path is no longer the current answer about it.
|
|
90
|
+
this.exits.delete(key);
|
|
91
|
+
if (typeof entry?.server?.onExit === "function") {
|
|
92
|
+
entry.server.onExit({ listener: (exit) => this.handleExit({ key, entry, exit }) });
|
|
93
|
+
}
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Forget a site — the deliberate close. Withdraws the exit observation by construction: the
|
|
98
|
+
* listener still fires when `server.stop()` lands, sees this entry is no longer current, and
|
|
99
|
+
* does nothing.
|
|
100
|
+
*
|
|
101
|
+
* @complexity O(1).
|
|
102
|
+
*/
|
|
103
|
+
delete(key) {
|
|
104
|
+
this.exits.delete(key);
|
|
105
|
+
return this.live.delete(key);
|
|
106
|
+
}
|
|
107
|
+
/** @complexity O(1). */
|
|
108
|
+
get(key) {
|
|
109
|
+
return this.live.get(key);
|
|
110
|
+
}
|
|
111
|
+
/** @complexity O(1). */
|
|
112
|
+
has(key) {
|
|
113
|
+
return this.live.has(key);
|
|
114
|
+
}
|
|
115
|
+
/** @complexity O(1). */
|
|
116
|
+
values() {
|
|
117
|
+
return this.live.values();
|
|
118
|
+
}
|
|
119
|
+
/** @complexity O(1). */
|
|
120
|
+
get size() {
|
|
121
|
+
return this.live.size;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* How the site at `key` last died, when it died on its own — the fact `SiteRecord`'s
|
|
125
|
+
* `statusDetail` reports so a stopped tab can say "the server exited" rather than presenting a
|
|
126
|
+
* crashed site as one the operator simply never started.
|
|
127
|
+
*
|
|
128
|
+
* Cleared by both {@link set} and {@link delete}: a restarted or forgotten site's old crash is
|
|
129
|
+
* not a current fact about it.
|
|
130
|
+
*
|
|
131
|
+
* @returns `{code, signal}`, or `null` when this site has not died unattended.
|
|
132
|
+
* @complexity O(1).
|
|
133
|
+
*/
|
|
134
|
+
lastExitOf({ key }, _optional = {}) {
|
|
135
|
+
return this.exits.get(key) ?? null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export { createChildEntries };
|
|
139
|
+
//# sourceMappingURL=child-entries.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface ChildExitOutcome {
|
|
2
|
+
code: number | null;
|
|
3
|
+
signal: NodeJS.Signals | null;
|
|
4
|
+
}
|
|
5
|
+
export interface ChildExitSignal {
|
|
6
|
+
onExit(required: {
|
|
7
|
+
listener: (exit: ChildExitOutcome) => void;
|
|
8
|
+
}, optional?: Record<string, never>): void;
|
|
9
|
+
}
|
|
10
|
+
export interface ExitSignalChild {
|
|
11
|
+
once(event: "exit", listener: (code: number | null, signal: NodeJS.Signals | null) => void): unknown;
|
|
12
|
+
}
|
|
13
|
+
/** Capture immediately after spawning, before any await. A child can die between readiness and a
|
|
14
|
+
* supervisor's subscription: forwarding only future events would leave its entry running forever.
|
|
15
|
+
* Late subscriptions replay synchronously, so no instant exists where an exit goes unrecorded.
|
|
16
|
+
* @returns A one-shot object-shaped subscription; listeners are dropped once delivered.
|
|
17
|
+
* @complexity O(1) registration; O(n) delivery to waiting listeners. */
|
|
18
|
+
export declare function createChildExitSignal({ child }: {
|
|
19
|
+
child: ExitSignalChild;
|
|
20
|
+
}, _optional?: {}): ChildExitSignal;
|
|
21
|
+
//# sourceMappingURL=child-exit-signal.d.ts.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Capture immediately after spawning, before any await. A child can die between readiness and a
|
|
2
|
+
* supervisor's subscription: forwarding only future events would leave its entry running forever.
|
|
3
|
+
* Late subscriptions replay synchronously, so no instant exists where an exit goes unrecorded.
|
|
4
|
+
* @returns A one-shot object-shaped subscription; listeners are dropped once delivered.
|
|
5
|
+
* @complexity O(1) registration; O(n) delivery to waiting listeners. */
|
|
6
|
+
export function createChildExitSignal({ child }, _optional = {}) {
|
|
7
|
+
let exit = null;
|
|
8
|
+
const waiting = [];
|
|
9
|
+
child.once("exit", (code, signal) => {
|
|
10
|
+
exit = { code, signal };
|
|
11
|
+
for (const listener of waiting.splice(0))
|
|
12
|
+
listener(exit);
|
|
13
|
+
});
|
|
14
|
+
return {
|
|
15
|
+
onExit({ listener }, _options = {}) {
|
|
16
|
+
if (exit !== null) {
|
|
17
|
+
listener(exit);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
waiting.push(listener);
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=child-exit-signal.js.map
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Identity and parentage are host policy; a persisted pid alone never authorizes a signal. */
|
|
2
|
+
export interface ChildRegistryProcesses<Row extends {
|
|
3
|
+
pid: number;
|
|
4
|
+
}> {
|
|
5
|
+
isAlive(required: {
|
|
6
|
+
pid: number;
|
|
7
|
+
}): boolean;
|
|
8
|
+
isOrphan(required: {
|
|
9
|
+
pid: number;
|
|
10
|
+
}): boolean;
|
|
11
|
+
isLiveRow(required: {
|
|
12
|
+
row: Row;
|
|
13
|
+
}): boolean;
|
|
14
|
+
signal(required: {
|
|
15
|
+
pid: number;
|
|
16
|
+
signal: NodeJS.Signals;
|
|
17
|
+
}): void;
|
|
18
|
+
}
|
|
19
|
+
/** Package-internal termination of an already-proven orphan; reconciliation owns the first proof. */
|
|
20
|
+
export declare function terminateConfirmedOrphan<Row extends {
|
|
21
|
+
pid: number;
|
|
22
|
+
}>({ row, processes }: {
|
|
23
|
+
row: Row;
|
|
24
|
+
processes: ChildRegistryProcesses<Row>;
|
|
25
|
+
}, { graceMs }?: {
|
|
26
|
+
graceMs?: number;
|
|
27
|
+
}): Promise<void>;
|
|
28
|
+
/** Prove liveness, command identity and parentage before any signal.
|
|
29
|
+
* Live siblings match their own rows exactly; only parentage distinguishes them from orphans.
|
|
30
|
+
* Reports retain rows selected for termination even if the process exits in a signal race. */
|
|
31
|
+
export declare function reconcileRows<Row extends {
|
|
32
|
+
pid: number;
|
|
33
|
+
}>({ rows, processes }: {
|
|
34
|
+
rows: Row[];
|
|
35
|
+
processes: ChildRegistryProcesses<Row>;
|
|
36
|
+
}, _optional?: {}): Promise<Row[]>;
|
|
37
|
+
//# sourceMappingURL=child-orphans.d.ts.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { orFallbackSync } from "@jini-ai/core/errors/idioms";
|
|
2
|
+
// Match the grace given to a child started by the current host, including its graceful drain.
|
|
3
|
+
const DEFAULT_TERMINATE_GRACE_MS = 5_000;
|
|
4
|
+
const TERMINATE_POLL_MS = 200;
|
|
5
|
+
/** The caller bounds the number of polls. */
|
|
6
|
+
function sleep({ ms }, _optional = {}) {
|
|
7
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
8
|
+
}
|
|
9
|
+
/** Re-prove identity before force: the OS can recycle a pid during the grace window.
|
|
10
|
+
* A successful force request is not observed exit; the final bounded poll lets the OS reap it. */
|
|
11
|
+
async function finishOrphanTermination({ row, processes }, _optional = {}) {
|
|
12
|
+
if (processes.isAlive({ pid: row.pid }) && processes.isLiveRow({ row })) {
|
|
13
|
+
const signalled = orFallbackSync({ work: () => { processes.signal({ pid: row.pid, signal: "SIGKILL" }); return true; }, fallback: false, step: "force-orphan" });
|
|
14
|
+
if (!signalled)
|
|
15
|
+
return; // Already gone in the signal race.
|
|
16
|
+
const killDeadline = Date.now() + DEFAULT_TERMINATE_GRACE_MS;
|
|
17
|
+
while (processes.isAlive({ pid: row.pid }) && Date.now() < killDeadline) {
|
|
18
|
+
await sleep({ ms: TERMINATE_POLL_MS });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Package-internal termination of an already-proven orphan; reconciliation owns the first proof. */
|
|
23
|
+
export async function terminateConfirmedOrphan({ row, processes }, { graceMs = DEFAULT_TERMINATE_GRACE_MS } = {}) {
|
|
24
|
+
const signalled = orFallbackSync({ work: () => { processes.signal({ pid: row.pid, signal: "SIGTERM" }); return true; }, fallback: false, step: "terminate-orphan" });
|
|
25
|
+
if (!signalled)
|
|
26
|
+
return; // Already gone in the signal race.
|
|
27
|
+
const deadline = Date.now() + graceMs;
|
|
28
|
+
while (Date.now() < deadline) {
|
|
29
|
+
if (!processes.isAlive({ pid: row.pid }))
|
|
30
|
+
return;
|
|
31
|
+
await sleep({ ms: TERMINATE_POLL_MS });
|
|
32
|
+
}
|
|
33
|
+
await finishOrphanTermination({ row, processes });
|
|
34
|
+
}
|
|
35
|
+
/** Prove liveness, command identity and parentage before any signal.
|
|
36
|
+
* Live siblings match their own rows exactly; only parentage distinguishes them from orphans.
|
|
37
|
+
* Reports retain rows selected for termination even if the process exits in a signal race. */
|
|
38
|
+
export async function reconcileRows({ rows, processes }, _optional = {}) {
|
|
39
|
+
const reconciled = [];
|
|
40
|
+
for (const row of rows) {
|
|
41
|
+
if (!processes.isAlive({ pid: row.pid }) || !processes.isLiveRow({ row }) || !processes.isOrphan({ pid: row.pid }))
|
|
42
|
+
continue;
|
|
43
|
+
reconciled.push(row);
|
|
44
|
+
await terminateConfirmedOrphan({ row, processes });
|
|
45
|
+
}
|
|
46
|
+
return reconciled;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=child-orphans.js.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface ChildProcessInspectionRequired {
|
|
2
|
+
runPs(required: {
|
|
3
|
+
args: string[];
|
|
4
|
+
}): string;
|
|
5
|
+
}
|
|
6
|
+
export interface ChildProcessInspection {
|
|
7
|
+
isProcessAlive(required: {
|
|
8
|
+
pid: number;
|
|
9
|
+
}, optional?: Record<string, never>): boolean;
|
|
10
|
+
readProcessCommand(required: {
|
|
11
|
+
pid: number;
|
|
12
|
+
}, optional?: Record<string, never>): string | null;
|
|
13
|
+
readProcessParentPid(required: {
|
|
14
|
+
pid: number;
|
|
15
|
+
}, optional?: Record<string, never>): number | null;
|
|
16
|
+
isOrphanedProcess(required: {
|
|
17
|
+
pid: number;
|
|
18
|
+
}, optional?: Record<string, never>): boolean;
|
|
19
|
+
}
|
|
20
|
+
/** Argv-only probes never request environment columns, which may contain live credentials.
|
|
21
|
+
* Windows has no ps: return before invoking the port. Unknown identity means no reaping,
|
|
22
|
+
* preferring a leaked orphan over killing an unrelated or live sibling process.
|
|
23
|
+
* Parentage is read from the running process, so rows of every vintage are protected immediately;
|
|
24
|
+
* a persisted owner field would not protect children recorded by an older host.
|
|
25
|
+
* A detached process leads its process group but retains its parent until that parent exits. */
|
|
26
|
+
export declare function createChildProcessInspection({ runPs }: ChildProcessInspectionRequired, { platform }?: {
|
|
27
|
+
platform?: NodeJS.Platform;
|
|
28
|
+
}): ChildProcessInspection;
|
|
29
|
+
//# sourceMappingURL=child-process-inspection.d.ts.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { orFallbackSync } from "@jini-ai/core/errors/idioms";
|
|
2
|
+
/** Signal zero asks only whether the kernel knows a signalable process, without reading secrets. */
|
|
3
|
+
function isProcessAlive({ pid }, _optional = {}) {
|
|
4
|
+
return orFallbackSync({ work: () => { process.kill(pid, 0); return true; }, fallback: false, step: "child-liveness" });
|
|
5
|
+
}
|
|
6
|
+
/** Argv-only probes never request environment columns, which may contain live credentials.
|
|
7
|
+
* Windows has no ps: return before invoking the port. Unknown identity means no reaping,
|
|
8
|
+
* preferring a leaked orphan over killing an unrelated or live sibling process.
|
|
9
|
+
* Parentage is read from the running process, so rows of every vintage are protected immediately;
|
|
10
|
+
* a persisted owner field would not protect children recorded by an older host.
|
|
11
|
+
* A detached process leads its process group but retains its parent until that parent exits. */
|
|
12
|
+
export function createChildProcessInspection({ runPs }, { platform = process.platform } = {}) {
|
|
13
|
+
function readProcessCommand({ pid }, _optional = {}) {
|
|
14
|
+
if (platform === "win32")
|
|
15
|
+
return null;
|
|
16
|
+
return orFallbackSync({ work: () => runPs({ args: ["-o", "command=", "-p", String(pid)] }).trim() || null, fallback: null, step: "child-command" });
|
|
17
|
+
}
|
|
18
|
+
function readProcessParentPid({ pid }, _optional = {}) {
|
|
19
|
+
if (platform === "win32")
|
|
20
|
+
return null;
|
|
21
|
+
return orFallbackSync({ work: () => {
|
|
22
|
+
const parsed = Number.parseInt(runPs({ args: ["-o", "ppid=", "-p", String(pid)] }).trim(), 10);
|
|
23
|
+
return Number.isNaN(parsed) ? null : parsed;
|
|
24
|
+
}, fallback: null, step: "child-parent" });
|
|
25
|
+
}
|
|
26
|
+
function isOrphanedProcess({ pid }, _optional = {}) {
|
|
27
|
+
// Fail closed: only a confirmed reparent to pid 1 establishes orphanhood.
|
|
28
|
+
return readProcessParentPid({ pid }) === 1;
|
|
29
|
+
}
|
|
30
|
+
return { isProcessAlive, readProcessCommand, readProcessParentPid, isOrphanedProcess };
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=child-process-inspection.js.map
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Readable } from "node:stream";
|
|
2
|
+
export interface ReadinessChild {
|
|
3
|
+
pid?: number | undefined;
|
|
4
|
+
stdout: Readable | null;
|
|
5
|
+
stderr: Readable | null;
|
|
6
|
+
once(event: "exit", listener: (code: number | null, signal: NodeJS.Signals | null) => void): unknown;
|
|
7
|
+
once(event: "error", listener: (error: Error) => void): unknown;
|
|
8
|
+
}
|
|
9
|
+
export interface ChildReadinessOutputSink {
|
|
10
|
+
write(chunk: string): unknown;
|
|
11
|
+
end(): unknown;
|
|
12
|
+
}
|
|
13
|
+
export type ChildReadinessFailure = {
|
|
14
|
+
type: "timeout";
|
|
15
|
+
text: string;
|
|
16
|
+
readyTimeoutMs: number;
|
|
17
|
+
} | {
|
|
18
|
+
type: "exit";
|
|
19
|
+
text: string;
|
|
20
|
+
code: number | null;
|
|
21
|
+
signal: NodeJS.Signals | null;
|
|
22
|
+
} | {
|
|
23
|
+
type: "missing-pid";
|
|
24
|
+
text: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: "missing-stream";
|
|
27
|
+
text: string;
|
|
28
|
+
} | {
|
|
29
|
+
type: "output-limit";
|
|
30
|
+
text: string;
|
|
31
|
+
maxOutputChars: number;
|
|
32
|
+
};
|
|
33
|
+
export interface ChildReadinessRequired<T> {
|
|
34
|
+
child: ReadinessChild;
|
|
35
|
+
readyTimeoutMs: number;
|
|
36
|
+
parseReady(required: {
|
|
37
|
+
text: string;
|
|
38
|
+
}): T | null;
|
|
39
|
+
onReady(required: {
|
|
40
|
+
ready: T;
|
|
41
|
+
pid: number;
|
|
42
|
+
text: string;
|
|
43
|
+
}): void;
|
|
44
|
+
createOutputSink(required: {
|
|
45
|
+
stream: "stdout" | "stderr";
|
|
46
|
+
}): ChildReadinessOutputSink;
|
|
47
|
+
describeFailure(required: ChildReadinessFailure): string;
|
|
48
|
+
}
|
|
49
|
+
/** Wait for parsed readiness with one settlement and a bounded raw startup buffer.
|
|
50
|
+
* The callback receives all accumulated text so identity and startup credentials are read in order.
|
|
51
|
+
* @returns Parsed readiness and the native child pid.
|
|
52
|
+
* @complexity O(n) in startup output, bounded by one million characters. */
|
|
53
|
+
export declare function waitForChildReady<T>(ports: ChildReadinessRequired<T>, _optional?: {}): Promise<{
|
|
54
|
+
ready: T;
|
|
55
|
+
pid: number;
|
|
56
|
+
}>;
|
|
57
|
+
//# sourceMappingURL=child-readiness.d.ts.map
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** Startup output beyond this is a runaway child, not a readiness line. */
|
|
2
|
+
const MAX_BOOT_OUTPUT_CHARS = 1_000_000;
|
|
3
|
+
/** Whichever of readiness, timeout, or exit arrives first owns the outcome. */
|
|
4
|
+
function finish({ run }, _optional = {}) {
|
|
5
|
+
if (run.settled)
|
|
6
|
+
return false;
|
|
7
|
+
run.settled = true;
|
|
8
|
+
clearTimeout(run.timer);
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
function fail({ wait, error }, _optional = {}) {
|
|
12
|
+
if (finish({ run: wait.run }))
|
|
13
|
+
wait.reject(error);
|
|
14
|
+
}
|
|
15
|
+
function settleOnReady({ wait }, _optional = {}) {
|
|
16
|
+
const { ports, run } = wait;
|
|
17
|
+
const ready = ports.parseReady({ text: run.output });
|
|
18
|
+
if (ready === null)
|
|
19
|
+
return;
|
|
20
|
+
// A readiness line normally proves this child spawned and has a process id.
|
|
21
|
+
const pid = ports.child.pid;
|
|
22
|
+
if (pid == null)
|
|
23
|
+
return fail({ wait, error: new Error(ports.describeFailure({ type: "missing-pid", text: run.output })) });
|
|
24
|
+
if (!finish({ run }))
|
|
25
|
+
return;
|
|
26
|
+
ports.onReady({ ready, pid, text: run.output });
|
|
27
|
+
wait.resolve({ ready, pid });
|
|
28
|
+
run.output = ""; // Parsing is finished; the host sink alone retains its bounded diagnostic tail.
|
|
29
|
+
}
|
|
30
|
+
function acceptChunk({ wait, chunk }, _optional = {}) {
|
|
31
|
+
const { run, ports } = wait;
|
|
32
|
+
if (run.settled)
|
|
33
|
+
return;
|
|
34
|
+
if (run.output.length + chunk.length > MAX_BOOT_OUTPUT_CHARS) {
|
|
35
|
+
return fail({ wait, error: new Error(ports.describeFailure({ type: "output-limit", text: run.output, maxOutputChars: MAX_BOOT_OUTPUT_CHARS })) });
|
|
36
|
+
}
|
|
37
|
+
run.output += chunk;
|
|
38
|
+
settleOnReady({ wait });
|
|
39
|
+
}
|
|
40
|
+
/** Feed raw chunks to parsing, and keep draining through the host sink after settlement.
|
|
41
|
+
* Framing, redaction before retention/mirroring, and diagnostic bounds belong to that sink. */
|
|
42
|
+
function readStream({ wait, stream, name }, _optional = {}) {
|
|
43
|
+
stream.setEncoding("utf8");
|
|
44
|
+
const sink = wait.ports.createOutputSink({ stream: name });
|
|
45
|
+
stream.once("end", () => sink.end());
|
|
46
|
+
stream.on("data", (chunk) => {
|
|
47
|
+
sink.write(chunk);
|
|
48
|
+
acceptChunk({ wait, chunk });
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function watchChildEnd({ wait }, _optional = {}) {
|
|
52
|
+
const { ports, run } = wait;
|
|
53
|
+
// Preserve the native spawn error, including its identity and cause.
|
|
54
|
+
ports.child.once("error", error => fail({ wait, error }));
|
|
55
|
+
ports.child.once("exit", (code, signal) => {
|
|
56
|
+
if (!finish({ run }))
|
|
57
|
+
return;
|
|
58
|
+
wait.reject(new Error(ports.describeFailure({ type: "exit", text: run.output, code, signal })));
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/** Wait for parsed readiness with one settlement and a bounded raw startup buffer.
|
|
62
|
+
* The callback receives all accumulated text so identity and startup credentials are read in order.
|
|
63
|
+
* @returns Parsed readiness and the native child pid.
|
|
64
|
+
* @complexity O(n) in startup output, bounded by one million characters. */
|
|
65
|
+
export function waitForChildReady(ports, _optional = {}) {
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
const run = { output: "", settled: false, timer: undefined };
|
|
68
|
+
const wait = { ports, run, resolve, reject };
|
|
69
|
+
run.timer = setTimeout(() => {
|
|
70
|
+
if (!finish({ run }))
|
|
71
|
+
return;
|
|
72
|
+
reject(new Error(ports.describeFailure({ type: "timeout", text: run.output, readyTimeoutMs: ports.readyTimeoutMs })));
|
|
73
|
+
}, ports.readyTimeoutMs);
|
|
74
|
+
if (!ports.child.stdout || !ports.child.stderr) {
|
|
75
|
+
fail({ wait, error: new Error(ports.describeFailure({ type: "missing-stream", text: run.output })) });
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
readStream({ wait, stream: ports.child.stdout, name: "stdout" });
|
|
79
|
+
readStream({ wait, stream: ports.child.stderr, name: "stderr" });
|
|
80
|
+
watchChildEnd({ wait });
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=child-readiness.js.map
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { ChildRegistryProcesses } from "./child-orphans.js";
|
|
2
|
+
export type ChildRegistryRead<Row> = {
|
|
3
|
+
state: "ok";
|
|
4
|
+
rows: Row[];
|
|
5
|
+
} | {
|
|
6
|
+
state: "missing";
|
|
7
|
+
} | {
|
|
8
|
+
state: "unreadable";
|
|
9
|
+
};
|
|
10
|
+
export interface ChildRegistryStorage<Row> {
|
|
11
|
+
instancePath(required: {
|
|
12
|
+
registryDir: string;
|
|
13
|
+
instanceId: string;
|
|
14
|
+
}): string;
|
|
15
|
+
read(required: {
|
|
16
|
+
filePath: string;
|
|
17
|
+
}): ChildRegistryRead<Row>;
|
|
18
|
+
write(required: {
|
|
19
|
+
filePath: string;
|
|
20
|
+
rows: Row[];
|
|
21
|
+
}): void;
|
|
22
|
+
quarantine(required: {
|
|
23
|
+
filePath: string;
|
|
24
|
+
}): boolean;
|
|
25
|
+
removeDeadFile(required: {
|
|
26
|
+
filePath: string;
|
|
27
|
+
ownerPid: number;
|
|
28
|
+
}): void;
|
|
29
|
+
}
|
|
30
|
+
export type ChildRegistryReport = {
|
|
31
|
+
type: "list-failed";
|
|
32
|
+
registryDir: string;
|
|
33
|
+
} | {
|
|
34
|
+
type: "live-owner-unreadable";
|
|
35
|
+
filePath: string;
|
|
36
|
+
ownerPid: number;
|
|
37
|
+
};
|
|
38
|
+
export interface ChildRegistryRequired<Row extends {
|
|
39
|
+
pid: number;
|
|
40
|
+
}> {
|
|
41
|
+
instanceId: string;
|
|
42
|
+
legacyPath(required: {
|
|
43
|
+
registryDir: string;
|
|
44
|
+
}): string;
|
|
45
|
+
parseInstanceName(required: {
|
|
46
|
+
name: string;
|
|
47
|
+
}): {
|
|
48
|
+
instanceId: string;
|
|
49
|
+
ownerPid: number;
|
|
50
|
+
} | null;
|
|
51
|
+
keyOf(required: {
|
|
52
|
+
row: Row;
|
|
53
|
+
}): string;
|
|
54
|
+
storage: ChildRegistryStorage<Row>;
|
|
55
|
+
processes: ChildRegistryProcesses<Row>;
|
|
56
|
+
report(event: ChildRegistryReport): void;
|
|
57
|
+
}
|
|
58
|
+
export interface ChildRegistrySnapshot<Row> {
|
|
59
|
+
rows: Row[];
|
|
60
|
+
unreadable: string[];
|
|
61
|
+
}
|
|
62
|
+
export interface ChildRegistry<Row extends {
|
|
63
|
+
pid: number;
|
|
64
|
+
}> {
|
|
65
|
+
read(required: {
|
|
66
|
+
registryDir: string;
|
|
67
|
+
}, optional?: Record<string, never>): ChildRegistrySnapshot<Row>;
|
|
68
|
+
recordOpened(required: {
|
|
69
|
+
registryDir: string;
|
|
70
|
+
row: Row;
|
|
71
|
+
}, optional?: {
|
|
72
|
+
instanceId?: string;
|
|
73
|
+
isLiveRow?: (required: {
|
|
74
|
+
row: Row;
|
|
75
|
+
}) => boolean;
|
|
76
|
+
}): void;
|
|
77
|
+
recordClosed(required: {
|
|
78
|
+
registryDir: string;
|
|
79
|
+
key: string;
|
|
80
|
+
}, optional?: {
|
|
81
|
+
pid?: number;
|
|
82
|
+
instanceId?: string;
|
|
83
|
+
quarantine?: ChildRegistryStorage<Row>["quarantine"];
|
|
84
|
+
}): void;
|
|
85
|
+
reconcileOrphans(required: {
|
|
86
|
+
registryDir: string;
|
|
87
|
+
}, optional?: {
|
|
88
|
+
instanceId?: string;
|
|
89
|
+
}): Promise<Row[]>;
|
|
90
|
+
}
|
|
91
|
+
/** One file per instance has one writer. The host supplies filename and codec policy.
|
|
92
|
+
* Reconciliation never rewrites a sibling's file: a row recorded during a grace window must survive.
|
|
93
|
+
* A recycled owner pid conservatively keeps the file, while per-row parentage still permits reaping.
|
|
94
|
+
* The registry does not fabricate handles for recovered children: no pipe exists to supervise them. */
|
|
95
|
+
export declare function createChildRegistry<Row extends {
|
|
96
|
+
pid: number;
|
|
97
|
+
}>(required: ChildRegistryRequired<Row>, _optional?: {}): ChildRegistry<Row>;
|
|
98
|
+
//# sourceMappingURL=child-registry.d.ts.map
|