Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: prevent canceled JEV startups from becoming ready (#8677)

[._.]/ Adam Eivyc4856b382f28
diff --git a/server/services/jev.js b/server/services/jev.js
index aee4d79ff0..ffb489a7d6 100644
--- a/server/services/jev.js
+++ b/server/services/jev.js
@@ -85,6 +85,8 @@ let lastInstallFailure = null;
// first-callers must produce exactly ONE process, not two competing 9 GB loads.
let sidecar = null;
let startInFlight = null;
+// Stop invalidates startup even before it has a child handle.
+let startGeneration = 0;
let idleTimer = null;
// The child between spawn and its first healthy /health. `sidecar` is not set
// yet, so without this a stop during a cold start (an operator unload, a test
@@ -345,6 +347,7 @@ function clearIdleTimer() {
* with no way to end it is a resource leak with a UI.
*/
export function stopJevSidecar() {
+ startGeneration += 1;
clearIdleTimer();
const running = sidecar;
const starting = startingProc;
@@ -392,10 +395,11 @@ const delay = (ms) => new Promise((resolve) => { setTimeout(resolve, ms).unref?.
* concurrently — two 9 GB loads racing for the same port would leave one of
* them bound to nothing and the other reaped by a caller that never saw it.
*/
-async function startSidecar() {
+async function startSidecar(generation) {
const target = jevVenvSpawnTarget({ stdio: ['ignore', 'pipe', 'pipe'] });
if (!target) return failure('jev-not-installed');
const files = await findCachedRepoFiles(JEV_MODEL.repository, JEV_REQUIRED_FILES, { revision: JEV_MODEL.revision });
+ if (generation !== startGeneration) return failure('jev-start-failed');
if (!files?.[0]) return failure('jev-not-installed');
// Every required file sits in the pinned subfolder, so its parent IS the
// directory `from_pretrained` loads.
@@ -442,8 +446,10 @@ async function startSidecar() {
const deadline = Date.now() + JEV_START_TIMEOUT_MS;
while (Date.now() < deadline) {
// `startingProc` moving off this child means a stop landed mid-start.
- if (exited || startingProc !== proc) return failure('jev-start-failed');
+ if (exited || generation !== startGeneration || startingProc !== proc) return failure('jev-start-failed');
const health = await probeHealth();
+ // Stop, exit, or a replacement start may have won while health was pending.
+ if (exited || generation !== startGeneration || startingProc !== proc) return failure('jev-start-failed');
if (health) {
startingProc = null;
sidecar = { proc, device: typeof health.device === 'string' ? health.device : null };
@@ -464,7 +470,10 @@ function ensureSidecar() {
// Single in-flight start. Cleared in `finally` so a failed start does not
// pin every later caller to the same rejection.
if (!startInFlight) {
- startInFlight = startSidecar().finally(() => { startInFlight = null; });
+ const pending = startSidecar(startGeneration).finally(() => {
+ if (startInFlight === pending) startInFlight = null;
+ });
+ startInFlight = pending;
}
return startInFlight;
}
diff --git a/server/services/jev.test.js b/server/services/jev.test.js
index 6d887ccb11..6fba922a17 100644
--- a/server/services/jev.test.js
+++ b/server/services/jev.test.js
@@ -152,6 +152,84 @@ describe('scoreHypotheses', () => {
expect(spawn).toHaveBeenCalledTimes(1);
});
+ it('does not spawn after stop cancels a pending cache lookup', async () => {
+ makeInstalled();
+ let resolveFiles;
+ findCachedRepoFiles.mockReturnValueOnce(new Promise((resolve) => { resolveFiles = resolve; }));
+ const pending = jev.scoreHypotheses({ premise: 'p', hypotheses: ['a', 'b'] });
+ jev.stopJevSidecar();
+ resolveFiles([MODEL_FILE]);
+ expect(await pending).toEqual({ ok: false, code: 'jev-start-failed' });
+ expect(spawn).not.toHaveBeenCalled();
+ });
+
+ it.each(['stop', 'exit'])('rejects successful health received after child %s', async (action) => {
+ makeInstalled();
+ const child = fakeChild();
+ spawn.mockReturnValue(child);
+ const { jevEvents } = await import('./jevEvents.js');
+ const states = [];
+ jevEvents.on('status', () => states.push(jev.isJevSidecarRunning()));
+ let resolveHealth;
+ const health = new Promise((resolve) => { resolveHealth = resolve; });
+ vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: true, json: () => health }));
+ const pending = jev.scoreHypotheses({ premise: 'p', hypotheses: ['a', 'b'] });
+ await vi.advanceTimersByTimeAsync(0);
+ expect(fetch).toHaveBeenCalledTimes(1);
+ if (action === 'stop') jev.stopJevSidecar();
+ else child.emitClose(0);
+ resolveHealth({ ready: true });
+ expect(await pending).toEqual({ ok: false, code: 'jev-start-failed' });
+ expect(jev.isJevSidecarRunning()).toBe(false);
+ expect(states).not.toContain(true);
+ expect(vi.getTimerCount()).toBe(0);
+ expect(fetch).toHaveBeenCalledTimes(1);
+ });
+
+ it.each(['starting', 'resident'])('keeps a newer %s child owned when the canceled start settles', async (state) => {
+ makeInstalled();
+ const oldChild = fakeChild();
+ const newChild = fakeChild();
+ spawn.mockReturnValueOnce(oldChild).mockReturnValue(newChild);
+ let resolveOldHealth;
+ let resolveNewHealth;
+ const oldHealth = new Promise((resolve) => { resolveOldHealth = resolve; });
+ const newHealth = new Promise((resolve) => { resolveNewHealth = resolve; });
+ const scores = [
+ { hypothesis: 'a', entailment: 0.9, contradiction: 0, neutral: 0.1 },
+ { hypothesis: 'b', entailment: 0.1, contradiction: 0, neutral: 0.9 },
+ ];
+ vi.stubGlobal('fetch', vi.fn()
+ .mockResolvedValueOnce({ ok: true, json: () => oldHealth })
+ .mockResolvedValueOnce({ ok: true, json: () => newHealth })
+ .mockImplementation(async () => scoreOk(scores)));
+ const { jevEvents } = await import('./jevEvents.js');
+ const states = [];
+ jevEvents.on('status', () => states.push(jev.isJevSidecarRunning()));
+ const request = { premise: 'p', hypotheses: ['a', 'b'] };
+ const oldRequest = jev.scoreHypotheses(request);
+ await vi.advanceTimersByTimeAsync(0);
+ jev.stopJevSidecar();
+ const newRequest = jev.scoreHypotheses(request);
+ await vi.advanceTimersByTimeAsync(0);
+ if (state === 'resident') {
+ resolveNewHealth({ ready: true });
+ expect((await newRequest).ok).toBe(true);
+ }
+ resolveOldHealth({ ready: true });
+ expect(await oldRequest).toEqual({ ok: false, code: 'jev-start-failed' });
+ const joinedRequest = jev.scoreHypotheses(request);
+ await vi.advanceTimersByTimeAsync(0);
+ expect(spawn).toHaveBeenCalledTimes(2);
+ resolveNewHealth({ ready: true });
+ expect(await newRequest).toEqual({ ok: true, scores });
+ expect(await joinedRequest).toEqual({ ok: true, scores });
+ expect(states).toEqual([false, true]);
+ jev.stopJevSidecar();
+ expect(newChild.killed).toBe(true);
+ expect(states).toEqual([false, true, false]);
+ });
+
it('reports jev-start-failed when the sidecar dies before reporting ready', async () => {
makeInstalled();
const child = fakeChild();