@jini-ai/sidecar 0.6.15 → 0.6.16

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.
@@ -75,6 +75,12 @@ export interface DaemonSupervisorRequired {
75
75
  logger: {
76
76
  emit(event: SupervisorEvent): void;
77
77
  };
78
+ /** Discovery is host-owned; true only for this live child identity. */
79
+ readiness?: {
80
+ isReady(input: {
81
+ pid: number;
82
+ }): Promise<boolean>;
83
+ };
78
84
  }
79
85
  export interface DaemonSupervisorOptions {
80
86
  onDemandCooldownMs?: number;
@@ -95,6 +101,10 @@ export interface DaemonSupervisor {
95
101
  restart(): DaemonSupervisorActionResult;
96
102
  ensureStarted(): DaemonSupervisorActionResult;
97
103
  shutdown(): void;
104
+ isReady(): boolean;
105
+ ensureReady(requiredArgs: Record<string, never>, optionalArgs?: {
106
+ signal?: AbortSignal;
107
+ }): Promise<void>;
98
108
  }
99
109
  /**
100
110
  * Wire a caller-owned process lifecycle to retry decisions without global handlers or host defaults.
@@ -23,6 +23,77 @@ export function createDaemonSupervisor(deps, options = {}) {
23
23
  let terminating = false;
24
24
  let spawnAttempts = 0;
25
25
  let lastOnDemandAttemptAt;
26
+ let readyChild;
27
+ let readyWork;
28
+ let discoveryWork;
29
+ let cancelReadiness;
30
+ function isCurrentLive({ candidate }) {
31
+ return candidate !== undefined && current === candidate && !terminating && !candidate.exited && !candidate.stopping;
32
+ }
33
+ function isReady() {
34
+ return readyChild !== undefined && isCurrentLive({ candidate: readyChild });
35
+ }
36
+ /** Discovery outlives individual request deadlines; never re-enter the demand cooldown.
37
+ * O(1) state and at most one discovery read per 250ms, excluding the injected read itself. */
38
+ function waitForReady() {
39
+ return new Promise((resolve, reject) => {
40
+ let settled = false;
41
+ let cancelPoll;
42
+ cancelReadiness = () => finish(new Error("shutting down"));
43
+ function finish(error) {
44
+ if (settled)
45
+ return;
46
+ settled = true;
47
+ cancelPoll?.();
48
+ cancelReadiness = undefined;
49
+ if (error)
50
+ reject(error);
51
+ else
52
+ resolve();
53
+ }
54
+ async function poll() {
55
+ if (settled)
56
+ return;
57
+ const candidate = current;
58
+ try {
59
+ if (candidate?.child.pid !== undefined && isCurrentLive({ candidate })) {
60
+ const discovered = await deps.readiness.isReady({ pid: candidate.child.pid });
61
+ // The read may have crossed an exit/replacement or terminal shutdown.
62
+ if (discovered && !settled && isCurrentLive({ candidate })) {
63
+ readyChild = candidate;
64
+ deps.failureReporter.clear();
65
+ finish();
66
+ return;
67
+ }
68
+ }
69
+ }
70
+ catch { /* Missing or partly-published discovery is not readiness; keep waiting. */ }
71
+ if (!settled)
72
+ cancelPoll = deps.scheduler.schedule({ delayMs: 250, run: () => { void poll(); } });
73
+ }
74
+ void poll();
75
+ });
76
+ }
77
+ function ensureReady(_required, { signal } = {}) {
78
+ if (signal?.aborted)
79
+ return Promise.reject(signal.reason);
80
+ if (terminating)
81
+ return Promise.reject(new Error("shutting down"));
82
+ if (isReady())
83
+ return Promise.resolve();
84
+ if (!deps.readiness)
85
+ return Promise.reject(new Error("readiness port is not configured"));
86
+ if (!readyWork) {
87
+ const start = supervisor.ensureStarted();
88
+ if (!start.ok)
89
+ return Promise.reject(new Error(start.reason));
90
+ // A slow healthy child must keep being discovered after an HTTP caller gives up.
91
+ // Share the monitor across subsequent demands; only shutdown cancels background polling.
92
+ discoveryWork ??= waitForReady().finally(() => { discoveryWork = undefined; });
93
+ readyWork = readinessDeadline({ work: discoveryWork, scheduler: deps.scheduler }).finally(() => { readyWork = undefined; });
94
+ }
95
+ return signal ? awaitForCaller({ work: readyWork, signal }) : readyWork;
96
+ }
26
97
  function reportFailure(reason, decision) {
27
98
  deps.failureReporter.record({ reason });
28
99
  deps.logger.emit({ type: "failure", at: deps.now(), reason, ...(decision === undefined ? {} : { decision }) });
@@ -135,7 +206,8 @@ export function createDaemonSupervisor(deps, options = {}) {
135
206
  }
136
207
  attemptSpawn();
137
208
  }
138
- return {
209
+ const supervisor = {
210
+ isReady, ensureReady,
139
211
  start() { if (cancelRetry === undefined)
140
212
  attemptSpawn(); },
141
213
  restart() {
@@ -168,6 +240,7 @@ export function createDaemonSupervisor(deps, options = {}) {
168
240
  return;
169
241
  terminating = true;
170
242
  cancelPendingRetry();
243
+ cancelReadiness?.();
171
244
  if (current === undefined)
172
245
  return;
173
246
  current.deliberate = true;
@@ -176,5 +249,21 @@ export function createDaemonSupervisor(deps, options = {}) {
176
249
  terminate(current);
177
250
  },
178
251
  };
252
+ return supervisor;
253
+ }
254
+ /** Bound demand waits without cancelling shared discovery. O(1) timers and handlers per wait. */
255
+ function readinessDeadline({ work, scheduler }, _options = {}) {
256
+ return new Promise((resolve, reject) => {
257
+ const cancel = scheduler.schedule({ delayMs: 30_000, run: () => reject(new Error("daemon readiness timed out")) });
258
+ work.then(() => { cancel(); resolve(); }, error => { cancel(); reject(error); });
259
+ });
260
+ }
261
+ /** Cancellation belongs to one caller; it cannot cancel another caller's startup. */
262
+ function awaitForCaller({ work, signal }) {
263
+ return new Promise((resolve, reject) => {
264
+ const abort = () => reject(signal.reason);
265
+ signal.addEventListener("abort", abort, { once: true });
266
+ work.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
267
+ });
179
268
  }
180
269
  //# sourceMappingURL=supervisor.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jini-ai/sidecar",
3
- "version": "0.6.15",
3
+ "version": "0.6.16",
4
4
  "description": "Sidecar process runtime: JSON-IPC server and client, IPC path validation, namespace/runtime path resolution, port allocation, launch bootstrap, and a local daemon registry.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -63,19 +63,12 @@
63
63
  },
64
64
  "devDependencies": {
65
65
  "@vitest/coverage-v8": "^2.1.9",
66
- "@jini-ai/platform": "0.6.15",
67
- "@jini-ai/core": "0.6.15"
66
+ "@jini-ai/platform": "0.6.16",
67
+ "@jini-ai/core": "0.6.16"
68
68
  },
69
69
  "dependencies": {
70
- "@jini-ai/core": "^0.6.15"
71
- },
72
- "peerDependencies": {
73
- "@jini-ai/platform": "^0.6.10"
74
- },
75
- "peerDependenciesMeta": {
76
- "@jini-ai/platform": {
77
- "optional": true
78
- }
70
+ "@jini-ai/platform": "^0.6.16",
71
+ "@jini-ai/core": "^0.6.16"
79
72
  },
80
73
  "scripts": {
81
74
  "build": "tsc -p tsconfig.json",