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: codex image gen sliced session-id banner off long stderr chunks

[._.]/ Adam Eivyfe5ed6bbe07e
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 170a35024d..da492ad18c 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -147,4 +147,13 @@
## Fixed
+- **Codex image gen — "Codex returned no session id" false negative.** With
+ long pipeline prompts (multi-KB comic-script payloads), codex emits the
+ banner + echoed prompt in a single stderr chunk that exceeds the
+ banner-scan buffer. The old `captureSession()` sliced the buffer's tail
+ before running the regex, chopping the `session id:` line off the front
+ and failing every render. Now matches before slicing, and stops feeding
+ stdout into the banner buffer so an interleaved stdout chunk can't split
+ the session-id line either. Adds regression tests for both paths.
+
## Removed
diff --git a/server/services/imageGen/codex.js b/server/services/imageGen/codex.js
index 0645613750..07a17be140 100644
--- a/server/services/imageGen/codex.js
+++ b/server/services/imageGen/codex.js
@@ -197,18 +197,26 @@ async function runCodex(job, jobId, bin, args, outputPath, filename, meta) {
// buffer so a session-id line that gets split across chunk boundaries
// (Node streams can land each pipe write as its own 'data' event)
// still matches. Trim aggressively after a match to keep this tiny.
+ //
+ // Why: match BEFORE slicing. With long pipeline prompts (multi-KB
+ // comic-script payloads), codex emits the banner + the echoed prompt
+ // in a single stderr chunk that can exceed BANNER_BUF_MAX. If we
+ // sliced first, the `session id:` line at the FRONT would get chopped
+ // off before we ever ran the regex, producing the
+ // "Codex returned no session id" false negative.
let bannerBuf = '';
const BANNER_BUF_MAX = 4 * 1024;
const captureSession = (text) => {
if (sessionId) return;
bannerBuf += text;
- if (bannerBuf.length > BANNER_BUF_MAX) bannerBuf = bannerBuf.slice(-BANNER_BUF_MAX);
const m = bannerBuf.match(SESSION_ID_RE);
if (m) {
sessionId = m[1];
bannerBuf = '';
broadcastSse(job, { type: 'status', message: `Codex session ${sessionId.slice(0, 8)}…` });
+ return;
}
+ if (bannerBuf.length > BANNER_BUF_MAX) bannerBuf = bannerBuf.slice(-BANNER_BUF_MAX);
};
proc.on('error', (err) => {
@@ -216,9 +224,11 @@ async function runCodex(job, jobId, bin, args, outputPath, filename, meta) {
finalizeError(job, jobId, proc, `Failed to spawn ${bin}: ${err.message}`);
});
- proc.stdout.on('data', (chunk) => {
- const text = chunk.toString();
- captureSession(text);
+ proc.stdout.on('data', () => {
+ // 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.
broadcastSse(job, { type: 'status', message: 'Running…' });
});
diff --git a/server/services/imageGen/codex.test.js b/server/services/imageGen/codex.test.js
index ba624620cc..7c0d74ac76 100644
--- a/server/services/imageGen/codex.test.js
+++ b/server/services/imageGen/codex.test.js
@@ -206,6 +206,81 @@ describe('codex provider — image harvest', () => {
expect(failedListener.mock.calls[0][0].error).toMatch(/no image|account may not allow/i);
}, 10000);
+ it('captures the session id even when banner + long prompt echo arrive in a single >4KB stderr chunk', async () => {
+ // Regression: with multi-KB comic-pipeline prompts, codex flushes the
+ // entire banner *and* the echoed prompt in one stderr write. The old
+ // captureSession() sliced the buffer to its tail before running the
+ // regex, chopping the `session id:` line off the front and producing
+ // a spurious "Codex returned no session id" failure even though the
+ // banner was emitted correctly.
+ const sessionId = '019e2513-c506-7de1-815e-c1f589229242';
+ const codexDir = join(TEST_HOME, '.codex', 'generated_images', sessionId);
+ await mkdir(codexDir, { recursive: true });
+ await writeFile(join(codexDir, 'ig_x.png'), Buffer.from('png'));
+
+ const completedListener = vi.fn();
+ imageGenEvents.on('completed', completedListener);
+
+ await codex.generateImage({ prompt: 'a fox' });
+ const child = spawnCalls[0].child;
+
+ const longPromptEcho = 'panel description '.repeat(400); // ~7KB
+ const banner =
+ 'Reading additional input from stdin...\n' +
+ 'OpenAI Codex v0.130.0\n' +
+ '--------\n' +
+ 'workdir: /tmp\n' +
+ 'model: gpt-5.5\n' +
+ 'provider: openai\n' +
+ 'approval: never\n' +
+ 'sandbox: workspace-write\n' +
+ 'reasoning effort: high\n' +
+ 'reasoning summaries: none\n' +
+ `session id: ${sessionId}\n` +
+ '--------\n' +
+ 'user\n' +
+ `$imagegen ${longPromptEcho}\n`;
+ expect(banner.length).toBeGreaterThan(4 * 1024);
+ child.stderr.emit('data', Buffer.from(banner));
+ 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);
+ });
+
+ it('captures the session id when a stdout chunk interleaves the stderr banner', async () => {
+ // Regression: the old code fed both stdout and stderr into the same
+ // bannerBuf. A stdout chunk arriving between two stderr chunks of the
+ // banner would split the `session id:` line with unrelated text and
+ // break the regex match.
+ const sessionId = '019e2513-aaaa-7bcd-bb00-c1f589229242';
+ const codexDir = join(TEST_HOME, '.codex', 'generated_images', sessionId);
+ await mkdir(codexDir, { recursive: true });
+ await writeFile(join(codexDir, 'ig_y.png'), Buffer.from('png'));
+
+ const completedListener = vi.fn();
+ imageGenEvents.on('completed', completedListener);
+
+ await codex.generateImage({ prompt: 'a fox' });
+ const child = spawnCalls[0].child;
+
+ child.stderr.emit('data', Buffer.from('session id: 019e2513-aaaa-7bcd-'));
+ child.stdout.emit('data', Buffer.from('progress noise\n'));
+ child.stderr.emit('data', Buffer.from('bb00-c1f589229242\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);
+ });
+
it('emits a failed event when no session id banner is parsed', async () => {
const failedListener = vi.fn();
imageGenEvents.on('failed', failedListener);