@jini-ai/sidecar 0.7.3-next.2 → 0.7.4
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 +7 -0
- package/README.md +30 -0
- package/dist/daemon-connection.d.ts +31 -0
- package/dist/daemon-connection.js +24 -0
- package/dist/daemon-origin.d.ts +70 -0
- package/dist/daemon-origin.js +91 -0
- package/dist/daemon-park-recovery.d.ts +35 -0
- package/dist/daemon-park-recovery.js +31 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/json-file.d.ts +1 -1
- package/dist/json-ipc.d.ts +28 -6
- package/dist/json-ipc.js +355 -271
- package/dist/supervision/child-readiness.js +1 -2
- package/dist/supervision/parent-watchdog.d.ts +145 -0
- package/dist/supervision/parent-watchdog.js +88 -0
- package/dist/supervision/supervisor-node.d.ts +1 -1
- package/dist/supervision/supervisor.d.ts +0 -6
- package/dist/supervision/supervisor.js +9 -1
- package/package.json +23 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @jini-ai/sidecar
|
|
2
2
|
|
|
3
|
+
## 0.7.4
|
|
4
|
+
|
|
5
|
+
### Changes
|
|
6
|
+
|
|
7
|
+
- Add supervision, parent watchdog, and daemon park-recovery helpers.
|
|
8
|
+
- Require injected file writers and process-control ports and split IPC request settlement into smaller operations.
|
|
9
|
+
|
|
3
10
|
## 0.7.2
|
|
4
11
|
|
|
5
12
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -32,6 +32,14 @@ domain sockets), plus `resolveAppIpcPath` for the app-scoped socket path.
|
|
|
32
32
|
`requestJsonIpc({ socketPath, payload }, { timeoutMs })`: a newline-delimited JSON request/response
|
|
33
33
|
channel over a local socket, with a per-frame byte cap and an idle-connection timeout.
|
|
34
34
|
|
|
35
|
+
Pass `errors: { reporter, observability?, methodOf?, refusalCode: 'HANDLER_ERROR' }` (and optionally
|
|
36
|
+
`logger`) to `createJsonIpcServer` to report handler failures through your host's `ErrorReporter`: one
|
|
37
|
+
`ipc.handle` operation runs around the handler and success serialization, a failure becomes one
|
|
38
|
+
incident whose `ERR-` ID is both in the peer's words and in `requestId`, and a deliberate refusal
|
|
39
|
+
keeps `code: 'HANDLER_ERROR'` and adds its own code as `reason` (no ID). `requestJsonIpc` rejections
|
|
40
|
+
carry `reason` beside `code`. Without `errors`, the legacy reply (the connection trace ID as
|
|
41
|
+
`requestId`) and its one console write are unchanged.
|
|
42
|
+
|
|
35
43
|
**Port allocation** — `allocatePort({}, { host, label, port, reserved })`, which either honors a forced
|
|
36
44
|
port or finds a free dynamic one, returning `{ port, source: 'dynamic' | 'forced' }`.
|
|
37
45
|
|
|
@@ -130,3 +138,25 @@ process, clock, scheduler and reporting ports and starts no work on import.
|
|
|
130
138
|
|
|
131
139
|
The package TypeScript configuration includes production source and `src/**/__tests__`.
|
|
132
140
|
`npx tsc -p tsconfig.json --noEmit`, run from this package directory, checks both without running tests.
|
|
141
|
+
|
|
142
|
+
## Wiring
|
|
143
|
+
|
|
144
|
+
`@jini-ai/sidecar/daemon-origin` resolves the agent daemon's origin (`JINI_AGENT_DAEMON_URL`, then
|
|
145
|
+
`JINI_AGENT_DAEMON_PORT`, else a self-allocated free loopback port). The host owns the one production
|
|
146
|
+
resolver, calls `ensure()` before it serves, and layers any delegated-connection URL override on top.
|
|
147
|
+
|
|
148
|
+
`startOwnParentWatchdog` (root entry) self-terminates a server whose parent dies without signalling it.
|
|
149
|
+
The host injects the live process ports plus `envNames` (its disable and dev-supervisor-pid variable
|
|
150
|
+
names) and a `report` port; Jini owns no env name and no log wording. `report` receives structured
|
|
151
|
+
`ParentWatchdogEvent`s (`disabled`, `parent-gone-at-boot`, `invalid-supervisor-pid`,
|
|
152
|
+
`supervisor-gone`, `parent-reparented`); the host formats each line and picks its severity.
|
|
153
|
+
|
|
154
|
+
`@jini-ai/sidecar/daemon-park-recovery` replays a request at most once, and only after an explicit
|
|
155
|
+
503 park refusal. The host injects its wire `headers` (`parked`/`pid` names), `ensureReady` (its
|
|
156
|
+
supervisor's readiness wait), `onParked` (its parking note) and `send`; Jini owns no header name.
|
|
157
|
+
|
|
158
|
+
`@jini-ai/sidecar/daemon-connection` holds the current and previous delegated daemon endpoint, each
|
|
159
|
+
as one `{ url, token }` record so a probe of an old run keeps the credential that belongs to its
|
|
160
|
+
endpoint. `createDaemonConnectionRegistry<Connection>()` returns an instance (no module state); the
|
|
161
|
+
host builds the one production instance in its composition, owns the inherited-IPC registration that
|
|
162
|
+
calls `set`, and layers any fallback credential lookup on top of `current()`.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Instance-owned record of the agent-daemon endpoint a host currently delegates to, and the
|
|
3
|
+
* one it replaced. An endpoint and its credential are captured together (a record), so a probe of
|
|
4
|
+
* an old run can keep using the previous endpoint with the credential that belongs to it instead
|
|
5
|
+
* of mixing it with the replacement's. Memory only, never persisted. The host owns the inherited-IPC
|
|
6
|
+
* grammar, registration and readiness, and any fallback credential lookup.
|
|
7
|
+
*/
|
|
8
|
+
/** The least a connection record must carry: where the daemon is and the credential for that place. */
|
|
9
|
+
export interface DaemonConnectionRecord {
|
|
10
|
+
readonly url: string;
|
|
11
|
+
readonly token: string;
|
|
12
|
+
}
|
|
13
|
+
export interface DaemonConnectionRegistry<Connection extends DaemonConnectionRecord> {
|
|
14
|
+
/** Capture the endpoint and its credential together; old-run probes must retain both. Setting the
|
|
15
|
+
* same url and token again keeps the previous record untouched; `undefined` clears the current one.
|
|
16
|
+
* @complexity O(1), memory only. The caller owns readiness and inherited-channel authentication. */
|
|
17
|
+
set(required: {
|
|
18
|
+
connection: Connection | undefined;
|
|
19
|
+
}, optional?: {}): void;
|
|
20
|
+
/** The endpoint currently delegated to. The returned record is frozen: a late callback may hold it
|
|
21
|
+
* across a replacement and still send the credential it was issued. */
|
|
22
|
+
current(required?: {}, optional?: {}): Connection | undefined;
|
|
23
|
+
/** The endpoint the current one replaced, for recovery of runs launched against it. */
|
|
24
|
+
previous(required?: {}, optional?: {}): Connection | undefined;
|
|
25
|
+
/** Forget both records, so a test or a new process lifecycle retains no earlier capability. */
|
|
26
|
+
reset(required?: {}, optional?: {}): void;
|
|
27
|
+
}
|
|
28
|
+
/** One registry per host composition: no module state, so two instances never share a capability.
|
|
29
|
+
* @complexity O(1) to build. */
|
|
30
|
+
export declare function createDaemonConnectionRegistry<Connection extends DaemonConnectionRecord>(_required?: {}, _optional?: {}): DaemonConnectionRegistry<Connection>;
|
|
31
|
+
//# sourceMappingURL=daemon-connection.d.ts.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Instance-owned record of the agent-daemon endpoint a host currently delegates to, and the
|
|
3
|
+
* one it replaced. An endpoint and its credential are captured together (a record), so a probe of
|
|
4
|
+
* an old run can keep using the previous endpoint with the credential that belongs to it instead
|
|
5
|
+
* of mixing it with the replacement's. Memory only, never persisted. The host owns the inherited-IPC
|
|
6
|
+
* grammar, registration and readiness, and any fallback credential lookup.
|
|
7
|
+
*/
|
|
8
|
+
/** One registry per host composition: no module state, so two instances never share a capability.
|
|
9
|
+
* @complexity O(1) to build. */
|
|
10
|
+
export function createDaemonConnectionRegistry(_required = {}, _optional = {}) {
|
|
11
|
+
let current;
|
|
12
|
+
let previous;
|
|
13
|
+
return {
|
|
14
|
+
set: ({ connection }, _options = {}) => {
|
|
15
|
+
if (current && (connection?.url !== current.url || connection.token !== current.token))
|
|
16
|
+
previous = current;
|
|
17
|
+
current = connection ? Object.freeze({ ...connection }) : undefined;
|
|
18
|
+
},
|
|
19
|
+
current: (_required = {}, _options = {}) => current,
|
|
20
|
+
previous: (_required = {}, _options = {}) => previous,
|
|
21
|
+
reset: (_required = {}, _options = {}) => { current = undefined; previous = undefined; },
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=daemon-connection.js.map
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { type Server } from "node:net";
|
|
2
|
+
/**
|
|
3
|
+
* @file Self-contained per-instance agent-daemon origin resolution (2026-08-28 dispatch — daemon
|
|
4
|
+
* startup + port-scoping fix). Two env vars have always won when set — `JINI_AGENT_DAEMON_URL` (a
|
|
5
|
+
* full origin) or `JINI_AGENT_DAEMON_PORT` (a port on `127.0.0.1`) — and still do here, unchanged.
|
|
6
|
+
* What's new: when NEITHER is set, this process allocates its own free OS port instead of falling
|
|
7
|
+
* back to the fixed `4319` every other unconfigured host instance on the same box would also fall
|
|
8
|
+
* back to. That fixed default was a real, observed cross-instance hazard: two unrelated host
|
|
9
|
+
* processes on one machine, neither configuring the env var, converged on the identical daemon
|
|
10
|
+
* port — one silently proxied its assistant traffic at the OTHER instance's daemon, discovered only
|
|
11
|
+
* because their independently-minted auth tokens happened not to match.
|
|
12
|
+
*
|
|
13
|
+
* Two access patterns, matching the two things that need this value:
|
|
14
|
+
* - `ensure()` — async, called exactly once per boot (the host's boot entry points, BEFORE it
|
|
15
|
+
* builds its app) to perform the actual free-port probe when self-allocation is needed. A no-op
|
|
16
|
+
* when an explicit env var already makes the origin resolvable synchronously.
|
|
17
|
+
* - `getUrl()`/`getPortForSpawnEnv()` — synchronous, called per-request (the assistant proxy) or
|
|
18
|
+
* per-spawn (the host's daemon supervisor, building the daemon child's env). Both re-check the
|
|
19
|
+
* explicit env vars live on every call rather than trusting a value cached before `ensure()` ran,
|
|
20
|
+
* so a test harness that sets `JINI_AGENT_DAEMON_URL` after this module was first imported still
|
|
21
|
+
* wins without ever calling `ensure()`.
|
|
22
|
+
*
|
|
23
|
+
* A factory, not bare module state, so a test can hold its own isolated resolver instead of sharing
|
|
24
|
+
* one mutable cache across every test in the process. The host keeps the one production instance.
|
|
25
|
+
*/
|
|
26
|
+
export interface AgentDaemonOriginResolver {
|
|
27
|
+
/** Idempotent. Resolves the origin — synchronously from an explicit env var when present, else by
|
|
28
|
+
* probing the OS for a free port — and caches the self-allocated port for `getUrl()`/
|
|
29
|
+
* `getPortForSpawnEnv()` to read synchronously afterward. A second call returns the
|
|
30
|
+
* already-settled result without re-probing. */
|
|
31
|
+
ensure(): Promise<void>;
|
|
32
|
+
/** A deferred spawn must not reuse the boot-time released reservation. */
|
|
33
|
+
refresh(): Promise<void>;
|
|
34
|
+
/** The resolved daemon origin. An explicit `JINI_AGENT_DAEMON_URL`/`JINI_AGENT_DAEMON_PORT` is
|
|
35
|
+
* re-checked live on every call. Falls back to whatever `ensure()` already cached; throws if
|
|
36
|
+
* self-allocation was needed and `ensure()` has not yet settled — every real boot path calls
|
|
37
|
+
* `ensure()` before `createApp()` specifically so no request can ever observe that state. */
|
|
38
|
+
getUrl(): string;
|
|
39
|
+
/** The port value to inject into the daemon child's own spawn env, so the spawned process binds
|
|
40
|
+
* to the SAME port this resolver handed out — `undefined` only when `JINI_AGENT_DAEMON_URL` was
|
|
41
|
+
* set with no separate port to manage (the child then inherits whatever it inherits unchanged). */
|
|
42
|
+
getPortForSpawnEnv(): string | undefined;
|
|
43
|
+
}
|
|
44
|
+
/** The environment the resolver reads. The host passes its live `process.env` object, so a value set
|
|
45
|
+
* after construction (a test harness, a late boot step) is still seen on the next call. */
|
|
46
|
+
export type DaemonOriginEnv = Readonly<Record<string, string | undefined>>;
|
|
47
|
+
/** One real free-port probe: bind to port 0, read back whatever the OS assigned, release it
|
|
48
|
+
* immediately. Inherently a TOCTOU race against whatever binds next — the same class of race every
|
|
49
|
+
* "ask the OS for a free port" helper accepts — narrow in practice because the daemon child is
|
|
50
|
+
* spawned within milliseconds of this resolving, on the same host, with nothing else in this
|
|
51
|
+
* process's own boot sequence competing for a port.
|
|
52
|
+
* `createProbe` is the socket-factory port (real `net.createServer` by default) so the two
|
|
53
|
+
* failure arms — a close error and an address-less listener — are testable without a real OS fault.
|
|
54
|
+
* @complexity O(1) — one socket bind/close round trip. */
|
|
55
|
+
export declare function allocateFreePort({ createProbe }?: {
|
|
56
|
+
createProbe?: () => Pick<Server, "once" | "listen" | "address" | "close">;
|
|
57
|
+
}): Promise<number>;
|
|
58
|
+
/**
|
|
59
|
+
* Creates one independent resolver instance. Production uses a single host-owned instance;
|
|
60
|
+
* tests construct their own so resolving a port in one test can never leak into another's
|
|
61
|
+
* assertions.
|
|
62
|
+
*
|
|
63
|
+
* @complexity Each method O(1) beyond the one-time `allocateFreePort` probe `ensure()` may perform.
|
|
64
|
+
*/
|
|
65
|
+
export declare function createAgentDaemonOriginResolver({ env }: {
|
|
66
|
+
env: DaemonOriginEnv;
|
|
67
|
+
}, { allocatePort }?: {
|
|
68
|
+
allocatePort?: typeof allocateFreePort;
|
|
69
|
+
}): AgentDaemonOriginResolver;
|
|
70
|
+
//# sourceMappingURL=daemon-origin.d.ts.map
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { createServer } from "node:net";
|
|
2
|
+
/** Synchronous: true whenever the origin is fully determined by env vars already set, with no OS
|
|
3
|
+
* probe required. `JINI_AGENT_DAEMON_URL` wins outright over `JINI_AGENT_DAEMON_PORT`, matching the
|
|
4
|
+
* existing precedence every call site already assumed.
|
|
5
|
+
* @complexity O(1). */
|
|
6
|
+
function readExplicitOrigin({ env }, _optional = {}) {
|
|
7
|
+
const explicitUrl = env.JINI_AGENT_DAEMON_URL;
|
|
8
|
+
if (explicitUrl) {
|
|
9
|
+
const url = URL.parse(explicitUrl);
|
|
10
|
+
if (!url || !["http:", "https:"].includes(url.protocol) || [url.username, url.password, url.search, url.hash].some(Boolean)) {
|
|
11
|
+
throw new Error("JINI_AGENT_DAEMON_URL must be an HTTP(S) URL without credentials, query or fragment");
|
|
12
|
+
}
|
|
13
|
+
return { url: explicitUrl, port: undefined };
|
|
14
|
+
}
|
|
15
|
+
const explicitPort = env.JINI_AGENT_DAEMON_PORT;
|
|
16
|
+
if (explicitPort === undefined)
|
|
17
|
+
return undefined;
|
|
18
|
+
const port = Number(explicitPort);
|
|
19
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535) {
|
|
20
|
+
throw new Error("JINI_AGENT_DAEMON_PORT must be an integer from 1 to 65535");
|
|
21
|
+
}
|
|
22
|
+
return { url: `http://127.0.0.1:${port}`, port };
|
|
23
|
+
}
|
|
24
|
+
/** One real free-port probe: bind to port 0, read back whatever the OS assigned, release it
|
|
25
|
+
* immediately. Inherently a TOCTOU race against whatever binds next — the same class of race every
|
|
26
|
+
* "ask the OS for a free port" helper accepts — narrow in practice because the daemon child is
|
|
27
|
+
* spawned within milliseconds of this resolving, on the same host, with nothing else in this
|
|
28
|
+
* process's own boot sequence competing for a port.
|
|
29
|
+
* `createProbe` is the socket-factory port (real `net.createServer` by default) so the two
|
|
30
|
+
* failure arms — a close error and an address-less listener — are testable without a real OS fault.
|
|
31
|
+
* @complexity O(1) — one socket bind/close round trip. */
|
|
32
|
+
export function allocateFreePort({ createProbe = createServer } = {}) {
|
|
33
|
+
return new Promise((resolvePort, rejectPort) => {
|
|
34
|
+
const probe = createProbe();
|
|
35
|
+
probe.once("error", rejectPort);
|
|
36
|
+
probe.listen(0, "127.0.0.1", () => {
|
|
37
|
+
const address = probe.address();
|
|
38
|
+
probe.close((closeError) => {
|
|
39
|
+
if (closeError) {
|
|
40
|
+
rejectPort(closeError);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (address === null || typeof address === "string") {
|
|
44
|
+
rejectPort(new Error("agent daemon port allocation returned no usable address"));
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
resolvePort(address.port);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Creates one independent resolver instance. Production uses a single host-owned instance;
|
|
54
|
+
* tests construct their own so resolving a port in one test can never leak into another's
|
|
55
|
+
* assertions.
|
|
56
|
+
*
|
|
57
|
+
* @complexity Each method O(1) beyond the one-time `allocateFreePort` probe `ensure()` may perform.
|
|
58
|
+
*/
|
|
59
|
+
export function createAgentDaemonOriginResolver({ env }, { allocatePort = allocateFreePort } = {}) {
|
|
60
|
+
let selfAllocatedPort;
|
|
61
|
+
let ensured;
|
|
62
|
+
async function resolveAndCache() {
|
|
63
|
+
if (readExplicitOrigin({ env }) !== undefined)
|
|
64
|
+
return;
|
|
65
|
+
selfAllocatedPort = await allocatePort();
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
async refresh() {
|
|
69
|
+
await resolveAndCache();
|
|
70
|
+
},
|
|
71
|
+
ensure() {
|
|
72
|
+
ensured ??= resolveAndCache();
|
|
73
|
+
return ensured;
|
|
74
|
+
},
|
|
75
|
+
getUrl() {
|
|
76
|
+
const explicit = readExplicitOrigin({ env });
|
|
77
|
+
if (explicit)
|
|
78
|
+
return explicit.url;
|
|
79
|
+
if (selfAllocatedPort !== undefined)
|
|
80
|
+
return `http://127.0.0.1:${selfAllocatedPort}`;
|
|
81
|
+
throw new Error("agent daemon origin requested before ensure() resolved a self-allocated port — call ensure() before createApp()");
|
|
82
|
+
},
|
|
83
|
+
getPortForSpawnEnv() {
|
|
84
|
+
const explicit = readExplicitOrigin({ env });
|
|
85
|
+
if (explicit)
|
|
86
|
+
return explicit.port === undefined ? undefined : String(explicit.port);
|
|
87
|
+
return selfAllocatedPort === undefined ? undefined : String(selfAllocatedPort);
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=daemon-origin.js.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Retry one explicit, pre-dispatch park refusal from a supervised local daemon. The host owns
|
|
3
|
+
* the wire header names, the supervisor's readiness wait and its parking note; this module owns only
|
|
4
|
+
* the replay decision, so a reset or a generic 503 can never be mistaken for a safe replay.
|
|
5
|
+
*/
|
|
6
|
+
/** The two response headers that identify an explicit park refusal on the host's wire. */
|
|
7
|
+
export interface DaemonParkHeaders {
|
|
8
|
+
/** Set to `"1"` on a 503 the daemon returned because it parked before dispatching the request. */
|
|
9
|
+
readonly parked: string;
|
|
10
|
+
/** The parked child's pid (a positive safe integer). */
|
|
11
|
+
readonly pid: string;
|
|
12
|
+
}
|
|
13
|
+
export interface DaemonParkRecoveryRequired {
|
|
14
|
+
/** Sends the request. Called again for the replay, so it must resolve the current origin anew. */
|
|
15
|
+
readonly send: () => Promise<Response>;
|
|
16
|
+
readonly headers: DaemonParkHeaders;
|
|
17
|
+
/** Waits for the supervisor to bring a replacement daemon up (single-flight on the host side). */
|
|
18
|
+
readonly ensureReady: () => Promise<unknown>;
|
|
19
|
+
/** Tells the supervisor which child parked, before any replay decision. */
|
|
20
|
+
readonly onParked: (required: {
|
|
21
|
+
pid: number;
|
|
22
|
+
}) => void;
|
|
23
|
+
}
|
|
24
|
+
export interface DaemonParkRecoveryOptional {
|
|
25
|
+
/** A passive observer records the park but never wakes the daemon or replays. */
|
|
26
|
+
readonly passive?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** Retry one explicit, pre-dispatch park refusal. Never retry a reset or infer safety from a
|
|
29
|
+
* generic 503. Both legacy proxy and durable launches use this same wire contract.
|
|
30
|
+
* The send closure resolves the current origin again after the old child exits.
|
|
31
|
+
* @returns The original response for passive observers, otherwise at most one replacement send.
|
|
32
|
+
* @complexity At most two HTTP sends and one bounded, single-flight discovery wait.
|
|
33
|
+
*/
|
|
34
|
+
export declare function fetchWithDaemonParkRecovery(required: DaemonParkRecoveryRequired, { passive }?: DaemonParkRecoveryOptional): Promise<Response>;
|
|
35
|
+
//# sourceMappingURL=daemon-park-recovery.d.ts.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Retry one explicit, pre-dispatch park refusal from a supervised local daemon. The host owns
|
|
3
|
+
* the wire header names, the supervisor's readiness wait and its parking note; this module owns only
|
|
4
|
+
* the replay decision, so a reset or a generic 503 can never be mistaken for a safe replay.
|
|
5
|
+
*/
|
|
6
|
+
/** Retry one explicit, pre-dispatch park refusal. Never retry a reset or infer safety from a
|
|
7
|
+
* generic 503. Both legacy proxy and durable launches use this same wire contract.
|
|
8
|
+
* The send closure resolves the current origin again after the old child exits.
|
|
9
|
+
* @returns The original response for passive observers, otherwise at most one replacement send.
|
|
10
|
+
* @complexity At most two HTTP sends and one bounded, single-flight discovery wait.
|
|
11
|
+
*/
|
|
12
|
+
export async function fetchWithDaemonParkRecovery(required, { passive = false } = {}) {
|
|
13
|
+
const response = await required.send();
|
|
14
|
+
const pid = parkedDaemonPid({ response, headers: required.headers });
|
|
15
|
+
if (pid === undefined)
|
|
16
|
+
return response;
|
|
17
|
+
required.onParked({ pid });
|
|
18
|
+
if (passive)
|
|
19
|
+
return response;
|
|
20
|
+
await response.body?.cancel();
|
|
21
|
+
await required.ensureReady();
|
|
22
|
+
return required.send();
|
|
23
|
+
}
|
|
24
|
+
/** The parked child's pid from an explicit park refusal; undefined for any other response. */
|
|
25
|
+
function parkedDaemonPid({ response, headers }, _optional = {}) {
|
|
26
|
+
if (response.status !== 503 || response.headers.get(headers.parked) !== "1")
|
|
27
|
+
return undefined;
|
|
28
|
+
const pid = Number(response.headers.get(headers.pid));
|
|
29
|
+
return Number.isSafeInteger(pid) && pid > 0 ? pid : undefined;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=daemon-park-recovery.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -16,8 +16,10 @@ export { bootstrapSidecarRuntime, createSidecarLaunchEnv } from "./bootstrap.js"
|
|
|
16
16
|
export { allocatePort } from "./port.js";
|
|
17
17
|
export { readJsonFile, removeFile, removePointerIfCurrent, writeJsonFile, type JsonFileWriter } from "./json-file.js";
|
|
18
18
|
export { createJsonIpcServer, requestJsonIpc } from "./json-ipc.js";
|
|
19
|
+
export type { JsonIpcServerOptions } from "./json-ipc.js";
|
|
19
20
|
export type { LocalDaemonRegistryRecord } from "./daemon-registry.js";
|
|
20
21
|
export { isProcessAlive, readLiveDaemonRegistryRecord, removeDaemonRegistryRecordIfCurrent, resolveDaemonRegistryPath, writeDaemonRegistryRecord, } from "./daemon-registry.js";
|
|
21
22
|
export * from "./supervision/respawn-policy.js";
|
|
22
23
|
export * from "./supervision/supervisor.js";
|
|
24
|
+
export { startOwnParentWatchdog, type ParentWatchdogEvent, type ParentWatchdogPorts } from "./supervision/parent-watchdog.js";
|
|
23
25
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -19,4 +19,5 @@ export { isProcessAlive, readLiveDaemonRegistryRecord, removeDaemonRegistryRecor
|
|
|
19
19
|
// Preserve published entries with one implementation in supervision; Node adapters stay isolated.
|
|
20
20
|
export * from "./supervision/respawn-policy.js";
|
|
21
21
|
export * from "./supervision/supervisor.js";
|
|
22
|
+
export { startOwnParentWatchdog } from "./supervision/parent-watchdog.js";
|
|
22
23
|
//# sourceMappingURL=index.js.map
|
package/dist/json-file.d.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* writer (replacement and durability) is the host's, passed as `writer`.
|
|
9
9
|
*/
|
|
10
10
|
import type { AtomicFileWriterPort } from "@jini-ai/core/contracts/runtime";
|
|
11
|
-
/** The host's atomic JSON writer (Node hosts pass `
|
|
11
|
+
/** The host's atomic JSON writer (Node hosts pass `createAtomicWriterPort` of `@jini-ai/node-adapters/fs` over platform's functions). */
|
|
12
12
|
export type JsonFileWriter = Pick<AtomicFileWriterPort, "writeJson">;
|
|
13
13
|
/**
|
|
14
14
|
* Read and parse a JSON file, swallowing any read/parse error.
|
package/dist/json-ipc.d.ts
CHANGED
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
* `node:net`, `node:path`, `node:string_decoder`, the shared net close helper,
|
|
11
11
|
* the IPC-path recognizer, and the public IPC types.
|
|
12
12
|
*/
|
|
13
|
+
import type { ObservabilityPort } from '@jini-ai/core/contracts/observability';
|
|
14
|
+
import { type ErrorReporter } from '@jini-ai/core/errors/reporter';
|
|
15
|
+
import { type Logger } from '@jini-ai/core/primitives';
|
|
13
16
|
import type { JsonIpcHandler, JsonIpcServerHandle } from "./types.js";
|
|
14
17
|
/**
|
|
15
18
|
* Prepare a socket path for binding: ensure the parent dir exists and
|
|
@@ -26,6 +29,30 @@ import type { JsonIpcHandler, JsonIpcServerHandle } from "./types.js";
|
|
|
26
29
|
export declare function prepareIpcPath({ socketPath }: {
|
|
27
30
|
socketPath: string;
|
|
28
31
|
}, _optional?: Record<string, never>): Promise<void>;
|
|
32
|
+
/** Options of {@link createJsonIpcServer}. */
|
|
33
|
+
export interface JsonIpcServerOptions {
|
|
34
|
+
/** Reject a connection whose single frame exceeds this many bytes before a newline arrives (SEC-004). @default 1_000_000 */
|
|
35
|
+
maxFrameBytes?: number;
|
|
36
|
+
/** Drop a connection that has not delivered one complete frame within this many ms (SEC-004). @default 30_000 */
|
|
37
|
+
idleTimeoutMs?: number;
|
|
38
|
+
/**
|
|
39
|
+
* Opt in to reporter mode: a handler failure becomes one incident (or refusal) through the
|
|
40
|
+
* host's reporter, and the peer's words carry its error ID. Without it the legacy trace-ID reply
|
|
41
|
+
* and unconditional console write stay exactly as they were.
|
|
42
|
+
*/
|
|
43
|
+
errors?: {
|
|
44
|
+
reporter: ErrorReporter;
|
|
45
|
+
observability?: Pick<ObservabilityPort, "trackOperation">;
|
|
46
|
+
/** A host label for the request (`tool.run`); anything outside `/^[a-z][\w.:-]{0,63}$/i` becomes `unknown`. */
|
|
47
|
+
methodOf?: (required: {
|
|
48
|
+
message: unknown;
|
|
49
|
+
}) => string | undefined;
|
|
50
|
+
/** The wire code both incidents and refusals keep, so existing clients' `code` checks still match. */
|
|
51
|
+
refusalCode: "HANDLER_ERROR";
|
|
52
|
+
};
|
|
53
|
+
/** Where server-side warnings (and, in reporter mode, traces) go. Defaults to the console logger. */
|
|
54
|
+
logger?: Pick<Logger, "error" | "warn">;
|
|
55
|
+
}
|
|
29
56
|
/**
|
|
30
57
|
* Start a newline-delimited JSON IPC server on a unix socket / named pipe: each
|
|
31
58
|
* connection carries one JSON request, the handler's result is written back as
|
|
@@ -35,12 +62,7 @@ export declare function prepareIpcPath({ socketPath }: {
|
|
|
35
62
|
export declare function createJsonIpcServer({ handler, socketPath, }: {
|
|
36
63
|
handler: JsonIpcHandler;
|
|
37
64
|
socketPath: string;
|
|
38
|
-
},
|
|
39
|
-
/** Reject a connection whose single frame exceeds this many bytes before a newline arrives (SEC-004). @default 1_000_000 */
|
|
40
|
-
maxFrameBytes?: number;
|
|
41
|
-
/** Drop a connection that has not delivered one complete frame within this many ms (SEC-004). @default 30_000 */
|
|
42
|
-
idleTimeoutMs?: number;
|
|
43
|
-
}): Promise<JsonIpcServerHandle>;
|
|
65
|
+
}, optional?: JsonIpcServerOptions): Promise<JsonIpcServerHandle>;
|
|
44
66
|
/**
|
|
45
67
|
* Send one newline-delimited JSON request over a unix socket / named pipe and
|
|
46
68
|
* resolve with the server's `result`, rejecting on error response, malformed JSON,
|