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: clear activeProcess on codex spawn error + doc cleanups

Adam Eivy13636f1ebaeb
diff --git a/client/src/components/settings/ImageGenTab.jsx b/client/src/components/settings/ImageGenTab.jsx
index 7ebdcc160d..8a73732a4b 100644
--- a/client/src/components/settings/ImageGenTab.jsx
+++ b/client/src/components/settings/ImageGenTab.jsx
@@ -1,7 +1,9 @@
/**
- * Image Gen Settings — mode picker (External SD API vs local mflux), per-mode
- * configuration, and the "expose A1111 API on the tailnet" toggle so other
- * machines can use this PortOS as their image/video backend.
+ * Image Gen Settings — backend picker (External SD API / local mflux / Codex
+ * CLI), per-mode configuration, and the "expose A1111 API on the tailnet"
+ * toggle so other machines can use this PortOS as their image/video backend.
+ * Codex appears as a backend tile only after the user enables it; the toggle
+ * lives in the always-visible Codex CLI Imagegen section.
*/
import { useState, useEffect, useCallback, useRef } from 'react';
diff --git a/client/src/pages/ImageGen.jsx b/client/src/pages/ImageGen.jsx
index a7b2b200a8..fed399b8e1 100644
--- a/client/src/pages/ImageGen.jsx
+++ b/client/src/pages/ImageGen.jsx
@@ -1,11 +1,11 @@
/**
* Image Generation page.
*
- * Works against either an external SD API (default) or PortOS's local mflux
- * backend, picked in Settings → Image Gen. The external mode uses the
- * /api/image-gen/generate JSON endpoint; the local mode kicks off a job and
- * subscribes to /api/image-gen/:jobId/events SSE for streaming progress and
- * additional knobs (model picker, LoRAs, quantization).
+ * Backend is picked per-render via the chip strip (Local / External / Codex);
+ * default comes from Settings → Image Gen. External mode is synchronous over
+ * /api/image-gen/generate. Local + Codex are async — both kick off a job and
+ * stream progress over /api/image-gen/:jobId/events SSE. Codex requires the
+ * "Enable Codex Imagegen" toggle in Settings; otherwise its chip is hidden.
*/
import { useState, useEffect, useCallback, useRef } from 'react';
diff --git a/server/routes/imageGen.js b/server/routes/imageGen.js
index 2a09523b8c..3f4dd40af6 100644
--- a/server/routes/imageGen.js
+++ b/server/routes/imageGen.js
@@ -1,10 +1,12 @@
/**
- * Image Generation Routes — works against either the external SD API or the
- * local mflux backend, depending on settings.imageGen.mode.
+ * Image Generation Routes — works against the external SD API, local mflux,
+ * or the Codex CLI built-in image_gen tool, depending on settings.imageGen.mode
+ * (or the per-request `mode` override).
*
* Generic endpoints (status, generate, avatar) go through the dispatcher.
- * Local-mode endpoints (events SSE, gallery, loras, cancel, delete) target
- * the local module directly because their shape doesn't apply to external.
+ * Async-mode endpoints (events SSE, cancel) also go through the dispatcher
+ * which routes the jobId to whichever provider owns it. Local-only endpoints
+ * (gallery, loras, models, delete) target the local module directly.
*/
import { Router } from 'express';
diff --git a/server/services/imageGen/codex.js b/server/services/imageGen/codex.js
index 59ac918933..468708a936 100644
--- a/server/services/imageGen/codex.js
+++ b/server/services/imageGen/codex.js
@@ -218,6 +218,15 @@ async function runCodex(job, jobId, bin, args, outputPath, filename, meta) {
}
const finalizeError = (job, jobId, reason) => {
+ // Idempotent — spawn failures fire 'error' AND a follow-up 'close', so
+ // both paths reach finalizeError. Without this guard, listeners would
+ // see duplicate 'failed' events.
+ if (job.status === 'error' || job.status === 'complete') return;
+ // Clear activeProcess too. The 'close' handler does this for normal exits,
+ // but a spawn failure (ENOENT, EACCES) routes through 'error' before any
+ // 'close' may fire — without this, a single bad codexPath would
+ // permanently lock the provider into 409 BUSY for every later request.
+ activeProcess = null;
job.status = 'error';
activeJob = null;
console.log(`❌ codex image generation failed [${jobId.slice(0, 8)}]: ${reason.split('\n')[0]}`);
diff --git a/server/services/imageGen/codex.test.js b/server/services/imageGen/codex.test.js
index 77471e7e9d..a73aa3dfa4 100644
--- a/server/services/imageGen/codex.test.js
+++ b/server/services/imageGen/codex.test.js
@@ -197,6 +197,34 @@ describe('codex provider — image harvest', () => {
expect(failedListener.mock.calls[0][0].error).toMatch(/no session id/i);
});
+ it('recovers from a spawn error so subsequent generations can run', async () => {
+ // Simulate ENOENT (codex binary not found) by emitting 'error' on the
+ // child. Without the activeProcess-clear fix, the next generateImage
+ // call would forever throw 409 IMAGE_GEN_BUSY because activeProcess
+ // would still be set to the dead child.
+ const failedListener = vi.fn();
+ imageGenEvents.on('failed', failedListener);
+
+ await codex.generateImage({ prompt: 'first', codexPath: '/nonexistent/codex' });
+ const child = spawnCalls[0].child;
+ child.emit('error', Object.assign(new Error('spawn /nonexistent/codex ENOENT'), { code: 'ENOENT' }));
+ // The 'close' event also fires for spawn errors; emit it so the
+ // idempotence guard is exercised.
+ child.exitCode = 1;
+ child.emit('close', 1, null);
+ await flush();
+ await new Promise((r) => setTimeout(r, 50));
+
+ expect(failedListener).toHaveBeenCalled();
+ // Critical: activeProcess must be cleared so a follow-up call works.
+ const second = await codex.generateImage({ prompt: 'second' });
+ expect(second.mode).toBe('codex');
+ expect(spawnCalls.length).toBe(2);
+ spawnCalls[1].child.exitCode = 1;
+ spawnCalls[1].child.emit('close', 1, null);
+ await flush();
+ });
+
it('emits a failed event when codex exits non-zero', async () => {
const failedListener = vi.fn();
imageGenEvents.on('failed', failedListener);
diff --git a/server/services/voice/tools.js b/server/services/voice/tools.js
index fee712dc1e..e1afa5372d 100644
--- a/server/services/voice/tools.js
+++ b/server/services/voice/tools.js
@@ -14,7 +14,6 @@ import * as journal from '../brainJournal.js';
import { resolveNavCommand, normalizeLabel } from '../../lib/navManifest.js';
import { runAsk, VALID_MODES as ASK_VALID_MODES } from '../askService.js';
import * as imageGen from '../imageGen/index.js';
-import { IMAGE_GEN_MODES } from '../imageGen/index.js';
import { getSettings } from '../settings.js';
const DAILY_LOG_PATH = '/brain/daily-log';
@@ -1075,7 +1074,7 @@ const TOOLS = [
},
provider: {
type: 'string',
- enum: ['auto', ...IMAGE_GEN_MODES],
+ enum: ['auto', ...imageGen.IMAGE_GEN_MODES],
description: '"auto" (default) uses the user\'s saved backend. Override only when the user explicitly asks for a specific one or the task strongly favors it.',
},
negativePrompt: {