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: wait for codex's composer before the idle paste instead of pasting into its boot

[._.]/ Adam Eivya091802732c3
diff --git a/server/lib/tuiHandshake.js b/server/lib/tuiHandshake.js
index 861971c492..694e320313 100644
--- a/server/lib/tuiHandshake.js
+++ b/server/lib/tuiHandshake.js
@@ -111,6 +111,27 @@ export const PASTE_TO_ENTER_FALLBACK_MS = 3500;
// would simply wait the full window for nothing.
export const PASTE_COMMIT_PATIENCE_MS = 45000;
+// Codex's composer placeholder — the ONLY positive evidence its input box is
+// live and will accept a paste. Codex is not on the requireInputReady path
+// (that would fail a run hard the first time a wording change outran us), so
+// this is a SOFT gate: the idle heuristic waits for it, and PASTE_DEADLINE_MS
+// still backstops delivery if it never paints.
+//
+// It is what the idle heuristic was missing. On a cold Windows start
+// (`cmd.exe /c codex.cmd` -> node -> the native binary) codex paints its
+// header, then goes quiet for seconds while the binary boots — output-idle
+// alone reads that lull as "ready" and pastes into a composer that does not
+// exist yet, which swallows the paste (2026-09-20: six codex-tui agents dead
+// in ~24s, each with an empty composer in the transcript). Waiting for the
+// placeholder orders the two correctly; PASTE_COMMIT_PATIENCE_MS above stays
+// as the rescue for a paste that still goes out early.
+//
+// Matched against ANSI-STRIPPED output, and deliberately shorter than the full
+// "Ask Codex to do anything" sentence: the stripped stream collapses the
+// cursor-positioned gaps between glyphs, so the pattern tolerates arbitrary
+// (including zero) whitespace and matches only the stable leading words.
+export const CODEX_COMPOSER_READY_PATTERN = /Ask\s*Codex/i;
+
/**
* Extract a verifiable prefix from a prompt for paste verification. The prefix
* is a unique-enough substring from the prompt's first "content" line (skipping
diff --git a/server/lib/tuiHandshake.test.js b/server/lib/tuiHandshake.test.js
index 207bd1c13e..d12334f480 100644
--- a/server/lib/tuiHandshake.test.js
+++ b/server/lib/tuiHandshake.test.js
@@ -61,6 +61,7 @@ import {
TOOL_PERMISSION_REPAINT_COOLDOWN_MS,
TOOL_PERMISSION_DECLINE_MAX,
TOOL_PERMISSION_NUDGE_TEXT,
+ CODEX_COMPOSER_READY_PATTERN,
} from './tuiHandshake.js';
import { detectImmediateFallbackSignal } from './aiToolkit/errorDetection.js';
import { CODEX_CONFIGURED_DEFAULT } from './providerModels.js';
@@ -2114,3 +2115,17 @@ describe('isPasteCommitted', () => {
expect(isPasteCommitted('> Ask Codex to do anything', { verifiablePrefix: 'audit the provider list' })).toBe(false);
});
});
+
+describe('CODEX_COMPOSER_READY_PATTERN', () => {
+ it('matches the composer placeholder, including the whitespace-collapsed stripped form', () => {
+ expect(CODEX_COMPOSER_READY_PATTERN.test('› Ask Codex to do anything')).toBe(true);
+ // ANSI stripping can drop the cursor-positioned gaps between glyphs.
+ expect(CODEX_COMPOSER_READY_PATTERN.test('AskCodextodoanything')).toBe(true);
+ });
+
+ it('does not match codex startup chrome painted before the composer exists', () => {
+ // The header/border repaint the six 2026-09-20 agents died against.
+ expect(CODEX_COMPOSER_READY_PATTERN.test('╭╭╭╭╭╭gpt-6-astra default · /repo ╭───────╮')).toBe(false);
+ expect(CODEX_COMPOSER_READY_PATTERN.test('Loading MCP servers…')).toBe(false);
+ });
+});
diff --git a/server/services/agentTuiSpawning.js b/server/services/agentTuiSpawning.js
index 17b483ee5b..62b36c4d67 100644
--- a/server/services/agentTuiSpawning.js
+++ b/server/services/agentTuiSpawning.js
@@ -64,6 +64,7 @@ import {
PASTE_TO_ENTER_MIN_DELAY_MS,
PASTE_TO_ENTER_FALLBACK_MS,
PASTE_COMMIT_PATIENCE_MS,
+ CODEX_COMPOSER_READY_PATTERN,
POST_PASTE_BUFFER_CAP,
scheduleSubmitEnters,
PASTE_DEADLINE_MS,
@@ -926,6 +927,10 @@ export async function spawnTuiAgent({
// not `spawnCommand` — a credential-bootstrap wrap makes the latter the
// bootstrap CLI.
const isCodexSession = isCodexCommand(tuiConfig.command);
+ // Latches once codex's composer placeholder paints — the positive "the input
+ // box exists" signal the idle heuristic lacks. See
+ // CODEX_COMPOSER_READY_PATTERN; consumed by the idle paste branch below.
+ let codexComposerReady = false;
const mcpBoot = createMcpBootTracker();
// Tracks claude's interactive input-readiness (footer chrome) and its first-run
// folder-trust gate. Gates the prompt paste for the claude TUI so we never
@@ -1483,6 +1488,10 @@ export async function spawnTuiAgent({
// CONFIRMED paste) means a banner that arrives AFTER an early swallowed paste
// still latches — the swallowed paste never sets promptSubmittedAt.
if (isCodexSession && !promptSubmittedAt && stripped && !mcpBoot.active) mcpBoot.observe(stripped);
+ // Same window as the MCP-boot latch, and for the same reason: only codex's
+ // own startup chrome (never the echoed prompt) may trip it.
+ if (isCodexSession && !codexComposerReady && !promptSentAt && commandInjected && stripped
+ && CODEX_COMPOSER_READY_PATTERN.test(stripped)) codexComposerReady = true;
const now = Date.now();
// Startup-idle detection (the promptTimer's non-inputReady branch below)
// reads lastOutputAt/firstOutputAt to decide the TUI has gone quiet and is
@@ -2016,6 +2025,12 @@ export async function spawnTuiAgent({
if (elapsed < tuiConfig.promptDelayMs) return;
if (firstOutputAt === null) return;
if (now - lastOutputAt < READY_IDLE_THRESHOLD_MS) return;
+ // Codex goes quiet for seconds mid-boot with no composer painted, and the
+ // idle heuristic alone reads that lull as ready — pasting into nothing.
+ // Hold the idle path until its composer placeholder has actually rendered;
+ // the PASTE_DEADLINE_MS fallback above still delivers if it never does, so
+ // this can only delay a paste, never cancel one.
+ if (isCodexSession && !codexComposerReady) return;
safeSendPrompt('ready');
clearInterval(promptTimer);
}, READY_POLL_INTERVAL_MS);
diff --git a/server/services/agentTuiSpawning.test.js b/server/services/agentTuiSpawning.test.js
index e041c890f9..0a066888a8 100644
--- a/server/services/agentTuiSpawning.test.js
+++ b/server/services/agentTuiSpawning.test.js
@@ -1166,7 +1166,7 @@ describe('spawnTuiAgent runtime', () => {
const spawnPromise = runSpawn();
await flushMicrotasks();
- await capturedOnData(Buffer.from('Codex booting...\\n'));
+ await capturedOnData(Buffer.from('Codex booting...\\n' + CODEX_COMPOSER));
await flushMicrotasks();
await vi.advanceTimersByTimeAsync(2000);
await flushMicrotasks();
@@ -1263,7 +1263,7 @@ describe('spawnTuiAgent runtime', () => {
const spawnPromise = runSpawn();
await flushMicrotasks();
- await capturedOnData(Buffer.from('booting...\n'));
+ await capturedOnData(Buffer.from('booting...\n' + CODEX_COMPOSER));
await flushMicrotasks();
// Open the ready-gate (promptDelay floor + idle threshold) so sendPrompt fires.
@@ -1291,6 +1291,10 @@ describe('spawnTuiAgent runtime', () => {
const claudeTuiConfig = { command: 'claude', args: [], spawnCommand: 'claude', spawnArgs: [], commandLine: 'claude', promptDelayMs: 100 };
// Antigravity (agy) gets the SAME positive input-ready gate as claude (#2705).
const agyTuiConfig = { command: 'agy', args: [], spawnCommand: 'agy', spawnArgs: [], commandLine: 'agy', promptDelayMs: 100 };
+ // Codex's composer placeholder — the positive he input box is live signal
+ // the idle paste path now requires (CODEX_COMPOSER_READY_PATTERN). A codex
+ // that has not painted it is still booting, and a paste into it is swallowed.
+ const CODEX_COMPOSER = '> Ask Codex to do anything\n';
const pasteCount = () => vi.mocked(shellService.writeToSession).mock.calls
.filter(([, d]) => typeof d === 'string' && d.includes('\x1b[200~')).length;
// The launch shell turns bracketed-paste OFF to run the command, then claude
@@ -1507,7 +1511,7 @@ describe('spawnTuiAgent runtime', () => {
// The selector clears and Codex repaints its composer; only then can the
// ordinary Codex readiness path paste the task.
- await capturedOnData(Buffer.from('OpenAI Codex ready\n'));
+ await capturedOnData(Buffer.from(CODEX_COMPOSER));
await vi.advanceTimersByTimeAsync(2000);
await flushMicrotasks();
expect(pasteCount()).toBe(1);
@@ -1542,7 +1546,7 @@ describe('spawnTuiAgent runtime', () => {
expect(pasteCount()).toBe(0);
// Trust accepted → codex paints its composer → the ordinary idle path pastes.
- await capturedOnData(Buffer.from('OpenAI Codex ready\n'));
+ await capturedOnData(Buffer.from(CODEX_COMPOSER));
await vi.advanceTimersByTimeAsync(2000);
await flushMicrotasks();
expect(pasteCount()).toBe(1);
@@ -2047,7 +2051,7 @@ describe('spawnTuiAgent runtime', () => {
// Banner output so firstOutputAt is set, then advance past the prompt-delay
// floor + readiness idle threshold so the ready poll fires the paste.
- await capturedOnData(Buffer.from('Codex booting...\n'));
+ await capturedOnData(Buffer.from('Codex booting...\n' + CODEX_COMPOSER));
await flushMicrotasks();
await vi.advanceTimersByTimeAsync(2000);
await flushMicrotasks();
@@ -2147,7 +2151,7 @@ describe('spawnTuiAgent runtime', () => {
// Now the real command is injected and the CLI produces its own output —
// the idle clock should start from here and the paste should proceed.
sendInitialCommand();
- await capturedOnData(Buffer.from('Codex booting...\n'));
+ await capturedOnData(Buffer.from('Codex booting...\n' + CODEX_COMPOSER));
await flushMicrotasks();
await vi.advanceTimersByTimeAsync(2000);
await flushMicrotasks();
@@ -2164,6 +2168,44 @@ describe('spawnTuiAgent runtime', () => {
// `paste-not-rendered` in ~24s with a healthy composer on screen. The paste
// must be left alone long enough for that late commit, and submitted when it
// arrives — never re-pasted over.
+ // Regression (2026-09-20, six codex-tui agents dead in ~24s): on a cold
+ // Windows start codex paints its header and then goes SILENT for seconds
+ // while the native binary boots. Output-idle alone read that lull as "ready"
+ // and pasted into a composer that did not exist yet, which swallowed the
+ // paste. The idle path must wait for codex's composer placeholder; the
+ // PASTE_DEADLINE_MS fallback still delivers if it never paints.
+ it('codex composer gate: idle silence before the composer paints does not paste', async () => {
+ runSpawn();
+ await flushMicrotasks();
+
+ // Header only — no composer. Then well past the 1200ms idle threshold.
+ await capturedOnData(Buffer.from('>_ OpenAI Codex (v0.155.1)\n'));
+ await flushMicrotasks();
+ await vi.advanceTimersByTimeAsync(3000);
+ await flushMicrotasks();
+ expect(pasteCount()).toBe(0);
+
+ // Composer paints → the ordinary idle path delivers.
+ await capturedOnData(Buffer.from(CODEX_COMPOSER));
+ await flushMicrotasks();
+ await vi.advanceTimersByTimeAsync(2000);
+ await flushMicrotasks();
+ expect(pasteCount()).toBe(1);
+ });
+
+ // The gate can only DELAY a paste: a codex whose composer wording we no
+ // longer recognize still gets the prompt at the blind-paste deadline.
+ it('codex composer gate: still blind-pastes at PASTE_DEADLINE_MS if the composer is never recognized', async () => {
+ const { PASTE_DEADLINE_MS } = await vi.importActual('../lib/tuiHandshake.js');
+
+ runSpawn();
+ await flushMicrotasks();
+ await capturedOnData(Buffer.from('>_ OpenAI Codex (v9.9.9)\n some future composer wording\n'));
+ await flushMicrotasks();
+ await vi.advanceTimersByTimeAsync(PASTE_DEADLINE_MS + 1000);
+ await flushMicrotasks();
+ expect(pasteCount()).toBe(1);
+ });
it('codex late paste commit: waits for the chip past the old window instead of re-pasting', async () => {
const pasteFailSpy = vi.fn();
vi.mocked(agentLifecycle.finalizeAgent).mockImplementation(async (args) => {
@@ -2174,7 +2216,7 @@ describe('spawnTuiAgent runtime', () => {
await flushMicrotasks();
// Ordinary banner — deliberately NOT an MCP-boot signal, so the extended
// boot budget is not what rescues this run.
- await capturedOnData(Buffer.from('>_ OpenAI Codex (v0.155.1)\n directory: loading\n'));
+ await capturedOnData(Buffer.from('>_ OpenAI Codex (v0.155.1)\n directory: loading\n' + CODEX_COMPOSER));
await flushMicrotasks();
await vi.advanceTimersByTimeAsync(2000);
await flushMicrotasks();
@@ -2210,7 +2252,7 @@ describe('spawnTuiAgent runtime', () => {
runSpawn({ prompt: 'evaluate our animation prompts and generate drafts' });
await flushMicrotasks();
- await capturedOnData(Buffer.from('>_ OpenAI Codex (v0.155.1)\n'));
+ await capturedOnData(Buffer.from('>_ OpenAI Codex (v0.155.1)\n' + CODEX_COMPOSER));
await flushMicrotasks();
await vi.advanceTimersByTimeAsync(2000);
await flushMicrotasks();
@@ -2218,7 +2260,7 @@ describe('spawnTuiAgent runtime', () => {
// Boot banner arrives only now — after the paste. The attempt must abandon
// the patient window and fall back to the short swallow-recovery ladder.
- await capturedOnData(Buffer.from('Starting MCP servers (1/2): codex_apps (0s • esc to interrupt)\n'));
+ await capturedOnData(Buffer.from('Starting MCP servers (1/2): codex_apps (0s • esc to interrupt)\n' + CODEX_COMPOSER));
await flushMicrotasks();
// Well inside the patient window, so a captured 45s wait would still be
@@ -2284,7 +2326,7 @@ describe('spawnTuiAgent runtime', () => {
runSpawn({ prompt: 'evaluate our animation prompts and generate drafts' });
await flushMicrotasks();
- await capturedOnData(Buffer.from('Starting MCP servers (1/2): codex_apps (0s • esc to interrupt)\n'));
+ await capturedOnData(Buffer.from('Starting MCP servers (1/2): codex_apps (0s • esc to interrupt)\n' + CODEX_COMPOSER));
await flushMicrotasks();
await vi.advanceTimersByTimeAsync(2000);
await flushMicrotasks();
@@ -2325,7 +2367,7 @@ describe('spawnTuiAgent runtime', () => {
runSpawn();
await flushMicrotasks();
- await capturedOnData(Buffer.from('Booting MCP server: node_repl(0s • esc to interrupt)\n'));
+ await capturedOnData(Buffer.from('Booting MCP server: node_repl(0s • esc to interrupt)\n' + CODEX_COMPOSER));
await flushMicrotasks();
await vi.advanceTimersByTimeAsync(2000);
await flushMicrotasks();
@@ -2349,7 +2391,7 @@ describe('spawnTuiAgent runtime', () => {
runSpawn();
await flushMicrotasks();
// Ordinary banner chrome — NOT an MCP-boot signal, so the budget stays fixed.
- await capturedOnData(Buffer.from('Codex booting...\n'));
+ await capturedOnData(Buffer.from('Codex booting...\n' + CODEX_COMPOSER));
await flushMicrotasks();
await vi.advanceTimersByTimeAsync(2000);
await flushMicrotasks();
@@ -2714,7 +2756,7 @@ describe('spawnTuiAgent runtime', () => {
// Drive the prompt far enough to exercise normal post-submit output, then
// use the ordinary shell-exit completion path.
- await capturedOnData(Buffer.from('Codex booting...\n'));
+ await capturedOnData(Buffer.from('Codex booting...\n' + CODEX_COMPOSER));
await flushMicrotasks();
await vi.advanceTimersByTimeAsync(2000);
await flushMicrotasks();