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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
imageGen/codex: surface decline reason, harvest JSONL base64, fix -i variadic swallowing the prompt
[._.]/ Adam Eivyaa67b7f73994
diff --git a/server/services/imageGen/codex.js b/server/services/imageGen/codex.jsindex fecbd1db84..0da735332c 100644--- a/server/services/imageGen/codex.js+++ b/server/services/imageGen/codex.js@@ -8,9 +8,11 @@** Wire format: `codex exec --skip-git-repo-check --sandbox workspace-write* '$imagegen <prompt>'`. Codex prints a `session id: <uuid>` banner on stderr- * and writes the final PNG to `~/.codex/generated_images/<session-id>/ig_*.png`- * — there's no machine-readable path on stdout, so we parse the banner and- * harvest the dir after the child exits.+ * and usually writes the final PNG to+ * `~/.codex/generated_images/<session-id>/ig_*.png`. Newer Codex builds can+ * keep the image bytes only in the session JSONL's `image_generation_end`+ * event, so we parse the banner and harvest both locations after the child+ * exits.** The user must explicitly enable this provider in Settings → Image Gen* because not every Codex account has access to the `image_gen` tool. When@@ -19,7 +21,7 @@*/import { spawn } from 'child_process';-import { copyFile, readdir, stat, writeFile } from 'fs/promises';+import { copyFile, readFile, readdir, stat, writeFile } from 'fs/promises';import { existsSync } from 'fs';import { join } from 'path';import { homedir } from 'os';@@ -48,6 +50,7 @@ const DEFAULT_BIN = 'codex';const codexImagesDir = (sessionId) =>join(homedir(), '.codex', 'generated_images', sessionId);+const codexSessionsDir = () => join(homedir(), '.codex', 'sessions');// Per-job state — keyed by jobId so multiple codex renders can run in// parallel under the mediaJobQueue's configurable lane limit. Same client@@ -181,12 +184,18 @@ export async function generateImage({const fullPrompt = `$imagegen ${editPrefix}${prompt.trim()}${sizeHint}${qualityHint}${avoidHint}`;const bin = codexPath || DEFAULT_BIN;+ // `codex exec` treats `-i, --image <FILE>...` as VARIADIC, so it greedily+ // swallows every following positional — including the prompt — when no other+ // flag interrupts it. That left codex with no positional prompt; it then read+ // stdin (ignored here) and died with "No prompt provided". A `--` terminator+ // after the options bounds the variadic so the prompt lands as the positional.const args = ['exec','--skip-git-repo-check','--sandbox', 'workspace-write',...(validInitImagePath ? ['-i', validInitImagePath] : []),...(model ? ['-m', String(model)] : []),+ ...(validInitImagePath ? ['--'] : []),fullPrompt,];@@ -220,6 +229,33 @@ export async function generateImage({};}+// Codex exited 0 with a session id but wrote no PNG. Turn its stdout narration+// into the most useful error we can — surfacing the model's own words (#imagegen+// declines, content refusals, "generated" claims with no file) instead of a+// fixed guess. The legacy account/enablement hint is kept as a fallback when+// codex said nothing usable.+// eslint-disable-next-line no-control-regex+const ANSI_RE = /\u001b\[[0-9;]*m/g;+const CODEX_NO_IMAGE_HINT =+ 'Codex returned no image — your Codex account may not allow image_gen, or the model declined. Check Settings → Image Gen → Enable Codex Imagegen.';+export function noImageReason(stdoutTail = '') {+ const clean = String(stdoutTail).replace(ANSI_RE, '').trim();+ // Keep the model's last few non-empty narration lines, dropping codex's own+ // structural labels (`codex`, `user`, `tokens used`, dashed rules).+ const lines = clean.split('\n')+ .map((l) => l.trim())+ .filter((l) => l && !/^(codex|user|-{2,})$/i.test(l) && !/^tokens used\b/i.test(l) && !/^[\d,]+$/.test(l));+ const said = lines.slice(-4).join(' ').slice(-600);+ if (!said) return CODEX_NO_IMAGE_HINT;+ // Codex claims success but no file landed → the image tool wasn't actually+ // run (image-gen unavailable / rate-limited on the account), even though the+ // turn narrated a generation. Call that out specifically.+ if (/\b(generated|created|here(?:'|’)s|i(?:'|’)ve (?:made|generated))\b/i.test(said)) {+ return `Codex reported success but wrote no image file — the built-in image tool likely didn't run (image generation may be unavailable or rate-limited on your account, even though the feature is enabled). Codex said: "${said}"`;+ }+ return `Codex did not produce an image. Codex said: "${said}"`;+}+async function runCodex(job, jobId, bin, args, outputPath, filename, meta, { cleanC2PA = false, denoise = false } = {}) {const proc = spawn(bin, args, { shell: false, stdio: ['ignore', 'pipe', 'pipe'] });activeProcs.set(jobId, proc);@@ -227,6 +263,11 @@ async function runCodex(job, jobId, bin, args, outputPath, filename, meta, { clelet sessionId = null;let stderrTail = '';const STDERR_TAIL_BYTES = 32 * 1024;+ // Codex narrates its turn (incl. "I can't create that…" content declines and+ // any tool-failure notes) on STDOUT. Keep a rolling tail so a no-image finish+ // can surface the model's actual words instead of a generic "no image" guess.+ let stdoutTail = '';+ const STDOUT_TAIL_BYTES = 8 * 1024;let timeoutTimer = setTimeout(() => {if (activeProcs.get(jobId) === proc) {console.log(`⏱️ codex timed out after ${CODEX_TIMEOUT_MS}ms [${jobId.slice(0, 8)}]`);@@ -266,11 +307,16 @@ async function runCodex(job, jobId, bin, args, outputPath, filename, meta, { clefinalizeError(job, jobId, proc, `Failed to spawn ${bin}: ${err.message}`);});- proc.stdout.on('data', () => {+ proc.stdout.on('data', (chunk) => {// Codex prints the `session id:` banner on stderr only — don't feed// stdout into bannerBuf. A stdout chunk arriving between two stderr// chunks of the banner can split the session-id line with unrelated// text and break the regex match.+ // We DO retain stdout here (the model's turn narration) so a no-image+ // finish can report why — a content decline, or a "generated" claim with+ // no file (tool unavailable / image-gen rate-limited on the account).+ stdoutTail += chunk.toString();+ if (stdoutTail.length > STDOUT_TAIL_BYTES) stdoutTail = stdoutTail.slice(-STDOUT_TAIL_BYTES);broadcastSse(job, { type: 'status', message: 'Running…' });});@@ -304,14 +350,15 @@ async function runCodex(job, jobId, bin, args, outputPath, filename, meta, { cle// Codex writes the PNG asynchronously while it's wrapping up the turn.// Empirically the file is on disk by the time `codex exec` exits, but// poll for a few seconds in case there's a flush lag on slow disks.- const harvested = await harvestLatestImage(sessionId, 5000);+ const harvested = await harvestGeneratedImage(sessionId, 5000);if (!harvested) {- return finalizeError(- job, jobId, proc,- 'Codex returned no image — your Codex account may not allow image_gen, or the model declined. Check Settings → Image Gen → Enable Codex Imagegen.',- );+ return finalizeError(job, jobId, proc, noImageReason(stdoutTail));+ }+ if (harvested.path) {+ await copyFile(harvested.path, outputPath);+ } else {+ await writeFile(outputPath, harvested.buffer);}- await copyFile(harvested, outputPath);// Sidecar metadata so the gallery can recover prompt/seed/etc. The// codex sessionId is the closest analogue to a seed for gpt-image-2// (which doesn't expose one) — uniquely identifies the run and is@@ -358,22 +405,91 @@ const finalizeError = (job, jobId, proc, reason) => {// Returns the absolute path to the newest ig_*.png in the session dir, or// null if none appears within `timeoutMs`. Polls every 250ms — the file// usually lands in <1s but the rare slow case is fine.-async function harvestLatestImage(sessionId, timeoutMs) {+async function latestGeneratedImageFile(sessionId) {const dir = codexImagesDir(sessionId);+ if (!existsSync(dir)) return null;+ const names = await readdir(dir).catch(() => []);+ const pngs = names.filter((f) => f.startsWith('ig_') && f.endsWith('.png'));+ if (pngs.length) {+ const stats = await Promise.all(pngs.map(async (n) => {+ const s = await stat(join(dir, n)).catch(() => null);+ return s ? { n, mtimeMs: s.mtimeMs } : null;+ }));+ const latest = stats.filter(Boolean).sort((a, b) => b.mtimeMs - a.mtimeMs)[0];+ if (latest) return join(dir, latest.n);+ }+ return null;+}++async function harvestLatestImage(sessionId, timeoutMs) {const deadline = Date.now() + timeoutMs;while (Date.now() < deadline) {- if (existsSync(dir)) {- const names = await readdir(dir).catch(() => []);- const pngs = names.filter((f) => f.startsWith('ig_') && f.endsWith('.png'));- if (pngs.length) {- const stats = await Promise.all(pngs.map(async (n) => {- const s = await stat(join(dir, n)).catch(() => null);- return s ? { n, mtimeMs: s.mtimeMs } : null;- }));- const latest = stats.filter(Boolean).sort((a, b) => b.mtimeMs - a.mtimeMs)[0];- if (latest) return join(dir, latest.n);+ const latest = await latestGeneratedImageFile(sessionId);+ if (latest) return latest;+ await new Promise((r) => setTimeout(r, 250));+ }+ return null;+}++const isPngBuffer = (buf) => {+ if (!Buffer.isBuffer(buf) || buf.length < 12) return false;+ return buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47+ && buf[4] === 0x0d && buf[5] === 0x0a && buf[6] === 0x1a && buf[7] === 0x0a;+};++async function findSessionLogFile(sessionId) {+ const root = codexSessionsDir();+ if (!existsSync(root)) return null;+ const stack = [root];+ while (stack.length) {+ const dir = stack.pop();+ const entries = await readdir(dir, { withFileTypes: true }).catch(() => []);+ for (const entry of entries) {+ const full = join(dir, entry.name);+ if (entry.isFile() && entry.name.endsWith('.jsonl') && entry.name.includes(sessionId)) {+ return full;}+ if (entry.isDirectory()) stack.push(full);+ }+ }+ return null;+}++async function harvestSessionLogImage(sessionId) {+ const logPath = await findSessionLogFile(sessionId);+ if (!logPath) return null;+ const text = await readFile(logPath, 'utf8').catch(() => '');+ let latest = null;+ for (const line of text.split('\n')) {+ if (!line.trim()) continue;+ try {+ const record = JSON.parse(line);+ const payload = record?.payload;+ if (payload?.type !== 'image_generation_end' || typeof payload.result !== 'string') continue;+ const raw = payload.result.replace(/^data:image\/[a-z0-9.+-]+;base64,/i, '');+ const buffer = Buffer.from(raw, 'base64');+ if (isPngBuffer(buffer)) latest = { buffer, path: logPath, callId: payload.call_id || null };+ } catch {+ // Session JSONL can contain arbitrarily large model payloads; ignore+ // malformed/truncated lines and keep scanning for a valid image event.}+ }+ return latest;+}++async function harvestGeneratedImage(sessionId, timeoutMs) {+ const deadline = Date.now() + timeoutMs;+ while (Date.now() < deadline) {+ const path = await latestGeneratedImageFile(sessionId);+ if (path) return { path };++ // Newer Codex builds may persist image bytes only in the session JSONL as+ // image_generation_end.result (base64), without materializing+ // ~/.codex/generated_images/<session-id>/ig_*.png. Decode that fallback so+ // a real generation does not get reported as "success but no file".+ const sessionImage = await harvestSessionLogImage(sessionId);+ if (sessionImage) return { buffer: sessionImage.buffer, sessionLogPath: sessionImage.path };+await new Promise((r) => setTimeout(r, 250));}return null;@@ -385,6 +501,9 @@ async function harvestLatestImage(sessionId, timeoutMs) {// module because the test path is an explicit ergonomic carve-out.export const _internals = {codexImagesDir,+ codexSessionsDir,SESSION_ID_RE,harvestLatestImage,+ harvestSessionLogImage,+ harvestGeneratedImage,};diff --git a/server/services/imageGen/codex.test.js b/server/services/imageGen/codex.test.jsindex 7654d9b9f9..81b85a2b0a 100644--- a/server/services/imageGen/codex.test.js+++ b/server/services/imageGen/codex.test.js@@ -164,6 +164,12 @@ describe('codex provider — generateImage', () => {const iIdx = args.indexOf('-i');expect(iIdx).toBeGreaterThan(-1);expect(args[iIdx + 1]).toBe(join(FAKE_IMAGES_DIR, 'proof.png'));+ // `-i` is VARIADIC in codex exec — without a `--` terminator it swallows the+ // prompt as a second image file ("No prompt provided"). Assert `--` follows+ // the options and the prompt is the positional AFTER it.+ const dashIdx = args.indexOf('--');+ expect(dashIdx).toBeGreaterThan(iIdx);+ expect(dashIdx).toBe(args.length - 2);const promptArg = args[args.length - 1];// The `$imagegen` prefix is preserved so the bundled skill still triggers.expect(promptArg.startsWith('$imagegen ')).toBe(true);@@ -186,6 +192,8 @@ describe('codex provider — generateImage', () => {});const { args } = spawnCalls[0];expect(args).not.toContain('-i');+ // No init image → no variadic to terminate, so no `--` is added.+ expect(args).not.toContain('--');const promptArg = args[args.length - 1];// No edit prefix when the init image is dropped.expect(promptArg).not.toMatch(/edit the attached reference image/i);@@ -255,6 +263,49 @@ describe('codex provider — image harvest', () => {expect(existsSync(sidecar)).toBe(true);});+ it('decodes image bytes from the Codex session JSONL when no generated_images file exists', async () => {+ const sessionId = '22222222-2222-4222-8222-222222222222';+ const sessionsDir = join(TEST_HOME, '.codex', 'sessions', '2026', '06', '22');+ await mkdir(sessionsDir, { recursive: true });+ const fakePngBytes = Buffer.from([+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,+ 0x00, 0x00, 0x00, 0x0d,+ ]);+ await writeFile(+ join(sessionsDir, `rollout-2026-06-22T00-19-45-${sessionId}.jsonl`),+ `${JSON.stringify({+ timestamp: '2026-06-22T00:22:57.040Z',+ type: 'event_msg',+ payload: {+ type: 'image_generation_end',+ call_id: 'ig_test',+ status: 'generating',+ result: fakePngBytes.toString('base64'),+ },+ })}\n`,+ );++ const completedListener = vi.fn();+ imageGenEvents.on('completed', completedListener);++ const job = await codex.generateImage({ prompt: 'a fox' });+ const child = spawnCalls[0].child;+ child.stderr.emit('data', Buffer.from(`session id: ${sessionId}\n`));+ child.exitCode = 0;+ child.emit('close', 0, null);++ const deadline = Date.now() + 3000;+ while (Date.now() < deadline && completedListener.mock.calls.length === 0) {+ await new Promise((r) => setTimeout(r, 50));+ }++ expect(completedListener).toHaveBeenCalledTimes(1);+ const finalPath = join(FAKE_IMAGES_DIR, job.filename);+ expect(existsSync(finalPath)).toBe(true);+ const written = await readFile(finalPath);+ expect(Buffer.compare(written, fakePngBytes)).toBe(0);+ });+it('emits a failed event when codex exits 0 but writes no image (likely auth/quota issue)', async () => {const sessionId = '00000000-0000-4000-8000-000000000000';// Create the session dir so the harvest path exists, but leave it@@ -481,3 +532,35 @@ something else`;});});+describe('codex provider — noImageReason (no-image diagnostics)', () => {+ it('flags a "generated" claim with no file as tool-unavailable / rate-limited', () => {+ const msg = codex.noImageReason('codex\nGenerated with the built-in image tool using the imagegen skill.\ntokens used\n11484');+ expect(msg).toMatch(/wrote no image file/i);+ expect(msg).toMatch(/unavailable or rate-limited/i);+ // the model's own words are surfaced…+ expect(msg).toContain('Generated with the built-in image tool');+ // …but codex's structural labels + token count are stripped out+ expect(msg).not.toMatch(/\b11484\b/);+ expect(msg).not.toMatch(/tokens used/i);+ });++ it('surfaces a content-decline message verbatim', () => {+ const msg = codex.noImageReason('codex\nI can’t create that image because it depicts explicit content.\n');+ expect(msg).toMatch(/did not produce an image/i);+ expect(msg).toContain('depicts explicit content');+ expect(msg).not.toMatch(/rate-limited/i);+ });++ it('falls back to the account/enablement hint when codex said nothing usable', () => {+ expect(codex.noImageReason('')).toMatch(/may not allow image_gen/i);+ expect(codex.noImageReason('codex\nuser\n----\ntokens used\n42')).toMatch(/may not allow image_gen/i);+ });++ it('strips ANSI escape codes from the surfaced narration', () => {+ const ESC = String.fromCharCode(27);+ const msg = codex.noImageReason(`${ESC}[1mI can’t make that.${ESC}[0m`);+ expect(msg).toContain('I can’t make that.');+ expect(msg).not.toContain(ESC);+ expect(msg).not.toMatch(/\[\d+m/);+ });+});