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(tui): wait for TUI ready before pasting prompt + filter output noise

[._.]/ Adam Eivy8dfee0bb1538
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index faf9b04a18..67bf7736fb 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -1104,6 +1104,24 @@
## Fixed
+- **TUI agent prompt now actually lands in Claude Code's input.** The fixed
+ 2500 ms paste delay raced Claude Code's banner/loading render, so bracketed
+ paste arrived before the input field was ready and the entire prompt was
+ discarded — the agent then sat at an empty `>` prompt forever. The paste
+ is now gated on output-idle (TUI has stopped repainting for 1.2 s) with a
+ minimum boot delay floor and a 10 s deadline fallback, and the `\r`
+ submission is split out from the paste write by 400 ms so Claude Code can
+ commit the paste buffer before Enter fires. The deferred Enter is tracked
+ and cancelled in `finish()` if the agent ends inside the 400 ms window.
+ Also cleaned up the agent "Show output" panel: a streaming ANSI stripper
+ buffers unterminated CSI/OSC sequences across PTY chunks (was leaking
+ `0;Claude Code…` title-set fragments as visible text), and an `isTuiNoise`
+ filter drops the welcome banner, status footer, box-drawing lines, and
+ letter-spaced animation frames so what's left is closer to actual agent
+ output. The raw PTY stream is unaffected and still flows to the attached
+ shell session for live inspection. Touches `server/services/agentTuiSpawning.js`
+ + `agentTuiSpawning.test.js`.
+
- **`index.html` now revalidates on every navigation, so rebuilds are picked
up without a hard refresh.** The SPA fallback in `server/index.js` was
serving `index.html` with no `Cache-Control` header, so browsers fell back
diff --git a/server/services/agentTuiSpawning.js b/server/services/agentTuiSpawning.js
index a9e02d60f3..baeb451c9f 100644
--- a/server/services/agentTuiSpawning.js
+++ b/server/services/agentTuiSpawning.js
@@ -38,13 +38,85 @@ const OUTPUT_BUFFER_HEADROOM = 1280 * 1024;
// live tail but cuts I/O by 1-2 orders of magnitude.
const OUTPUT_FLUSH_INTERVAL_MS = 250;
+// Paste readiness gating. The TUI process needs time to render its welcome
+// banner and become input-ready before bracketed paste lands; sending the paste
+// during boot loses the entire prompt. We poll for output-idle (TUI has stopped
+// repainting) instead of guessing a fixed delay, with a hard upper bound so a
+// silent provider still gets the prompt eventually.
+const READY_POLL_INTERVAL_MS = 300;
+const READY_IDLE_THRESHOLD_MS = 1200;
+const PASTE_TO_ENTER_DELAY_MS = 400;
+const PASTE_DEADLINE_MS = 10000;
+
const ANSI_PATTERN = /\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~]|\][^\x07]*(?:\x07|\x1B\\))/g;
-function stripAnsi(text) {
- return text
- .replace(ANSI_PATTERN, '')
- .replace(/\x00/g, '')
- .replace(/\u001b/g, '');
+// Heuristics for a trailing chunk that *might* be an unterminated escape
+// sequence. We hold the tail back from the strip pass and prepend it to the
+// next chunk, so a CSI/OSC split across two PTY reads still strips cleanly
+// instead of leaking the body (e.g. `0;Claude Code…`) into displayed output.
+const INCOMPLETE_CSI = /^\x1B\[[0-?]*[ -/]*$/;
+const INCOMPLETE_OSC = /^\x1B\][^\x07\x1B]*$/;
+const INCOMPLETE_ESC_2BYTE = /^\x1B$/;
+
+function createStreamingAnsiStripper() {
+ let tail = '';
+ const strip = (s) => s.replace(ANSI_PATTERN, '').replace(/\x00/g, '');
+ return (text) => {
+ const combined = tail + text;
+ tail = '';
+ const lastEsc = combined.lastIndexOf('\x1B');
+ // Only consider the trailing fragment if it lives near the end — older
+ // unterminated bytes belong to a previous repaint and would never resolve.
+ // Bodies longer than 4096 bytes are treated as terminated; an unbounded
+ // OSC (e.g. very long hyperlink) would leak its body to display rather
+ // than buffer forever.
+ if (lastEsc !== -1 && combined.length - lastEsc <= 4096) {
+ const candidate = combined.slice(lastEsc);
+ if (INCOMPLETE_ESC_2BYTE.test(candidate)
+ || INCOMPLETE_CSI.test(candidate)
+ || INCOMPLETE_OSC.test(candidate)) {
+ tail = candidate;
+ return strip(combined.slice(0, lastEsc));
+ }
+ }
+ return strip(combined);
+ };
+}
+
+// TUI repaint artifacts that aren't useful to display in the agent output
+// panel. The raw PTY stream still goes to the attached shell session (and to
+// rawBuffer for error analysis) — this only suppresses display noise.
+const BOX_DRAW_CHARS = /[─│┌┐└┘├┤┬┴┼━┃╮╯╰╭╱╲╳▶◀▼▲╴╵╶╷▌▐▖▗▘▝▙▚▛▜▟►◄·•]/g;
+const STATUS_FOOTER_PATTERNS = [
+ /bypass permissions (on|off)/i,
+ /shift\s*\+\s*tab to cycle/i
+];
+
+export function isTuiNoise(line) {
+ if (!line) return true;
+ const stripped = line.replace(BOX_DRAW_CHARS, '').trim();
+ if (!stripped) return true;
+ // Single bullet/prompt marker with nothing else
+ if (/^[>?]\s*$/.test(stripped)) return true;
+ // "Try 'how does X work?'" placeholder
+ if (/^try ["'].*["']\??$/i.test(stripped)) return true;
+ // Welcome banner lines (Claude Code v…, Opus 4.x, etc.)
+ if (/^claude code\b/i.test(stripped) && stripped.length < 64) return true;
+ if (/^opus \d/i.test(stripped) && stripped.length < 64) return true;
+ // Status footer / help bar
+ if (STATUS_FOOTER_PATTERNS.some(re => re.test(stripped))) return true;
+ // Letter-spaced gibberish from TUI animation: e.g. "c l a u d e - c o d e".
+ // Match strings where the majority of whitespace-separated tokens are a
+ // single character — the TUI sometimes renders typed input one glyph at a
+ // time with separating spaces during paste/erase animation.
+ if (stripped.length > 8) {
+ const tokens = stripped.split(/\s+/);
+ if (tokens.length >= 6) {
+ const singleCharTokens = tokens.filter(t => t.length === 1).length;
+ if (singleCharTokens >= tokens.length * 0.6) return true;
+ }
+ }
+ return false;
}
function shellQuote(value) {
@@ -90,6 +162,7 @@ export async function spawnTuiAgent(agentId, task, prompt, workspacePath, model,
let finalized = false;
let hasStartedWorking = false;
let promptSentAt = null;
+ let firstOutputAt = null;
let lastOutputAt = Date.now();
let meaningfulLinesAfterPrompt = 0;
let lastLine = '';
@@ -98,6 +171,9 @@ export async function spawnTuiAgent(agentId, task, prompt, workspacePath, model,
let pendingLines = [];
let flushTimer = null;
let flushing = null;
+ let pasteEnterTimer = null;
+
+ const streamingStrip = createStreamingAnsiStripper();
const flushPendingLines = async () => {
if (flushTimer) { clearTimeout(flushTimer); flushTimer = null; }
@@ -118,10 +194,14 @@ export async function spawnTuiAgent(agentId, task, prompt, workspacePath, model,
}, OUTPUT_FLUSH_INTERVAL_MS);
};
- const appendLine = (line) => {
+ const appendLine = (line, { force = false } = {}) => {
const cleanLine = line.trim();
if (!cleanLine || cleanLine === lastLine) return;
if (promptPreview && cleanLine.replace(/\s+/g, ' ').includes(promptPreview)) return;
+ // Filter TUI repaint artifacts (banners, status footer, box drawing,
+ // letter-spaced animation frames). `force: true` lets internal messages
+ // ("TUI session started", etc.) bypass the filter.
+ if (!force && isTuiNoise(cleanLine)) return;
lastLine = cleanLine;
outputBuffer += `${cleanLine}\n`;
@@ -139,7 +219,8 @@ export async function spawnTuiAgent(agentId, task, prompt, workspacePath, model,
const agentData = activeAgents.get(agentId);
if (agentData?.idleTimer) clearInterval(agentData.idleTimer);
- if (agentData?.promptTimer) clearTimeout(agentData.promptTimer);
+ if (agentData?.promptTimer) clearInterval(agentData.promptTimer);
+ if (pasteEnterTimer) { clearTimeout(pasteEnterTimer); pasteEnterTimer = null; }
// Drain pending parsed lines before the final state writes so completion
// events don't beat the last output batch to disk.
@@ -241,6 +322,7 @@ export async function spawnTuiAgent(agentId, task, prompt, workspacePath, model,
rawBuffer += text;
if (rawBuffer.length > RAW_BUFFER_HEADROOM) rawBuffer = rawBuffer.slice(-RAW_BUFFER_CAP);
lastOutputAt = Date.now();
+ if (firstOutputAt === null) firstOutputAt = lastOutputAt;
if (!hasStartedWorking) {
hasStartedWorking = true;
@@ -248,7 +330,7 @@ export async function spawnTuiAgent(agentId, task, prompt, workspacePath, model,
emitLog('info', `TUI agent ${agentId} working...`, { agentId, phase: 'working' });
}
- const clean = stripAnsi(text).replace(/\r/g, '\n');
+ const clean = streamingStrip(text).replace(/\r/g, '\n');
const lowerClean = clean.toLowerCase();
if (!promptSentAt && lowerClean.includes('command not found') && lowerClean.includes(commandName.toLowerCase())) {
await finish({
@@ -307,12 +389,43 @@ export async function spawnTuiAgent(agentId, task, prompt, workspacePath, model,
});
}
- const promptTimer = setTimeout(() => {
- if (finalized) return;
+ // Send the bracketed-paste prompt only after the TUI has finished its initial
+ // repaint and gone quiet — pasting during the banner/loading screen is the
+ // failure mode that left the input empty. The `\r` is split from the paste
+ // write so Claude Code has time to commit the paste buffer before Enter
+ // fires; its handle is tracked so finish() can cancel a still-pending Enter
+ // if the agent ends in that window.
+ const startedAt = Date.now();
+ const sendPrompt = (reason) => {
+ if (finalized || promptSentAt) return;
promptSentAt = Date.now();
- shellService.writeToSession(sessionId, `\x1b[200~${prompt}\x1b[201~\r`);
- appendLine(`📟 Prompt pasted into TUI session ${sessionId.slice(0, 8)}`);
- }, tuiConfig.promptDelayMs);
+ shellService.writeToSession(sessionId, `\x1b[200~${prompt}\x1b[201~`);
+ pasteEnterTimer = setTimeout(() => {
+ pasteEnterTimer = null;
+ if (finalized) return;
+ shellService.writeToSession(sessionId, '\r');
+ }, PASTE_TO_ENTER_DELAY_MS);
+ appendLine(`📟 Prompt pasted into TUI session ${sessionId.slice(0, 8)} (${reason})`, { force: true });
+ };
+
+ const promptTimer = setInterval(() => {
+ if (finalized || promptSentAt) {
+ clearInterval(promptTimer);
+ return;
+ }
+ const now = Date.now();
+ const elapsed = now - startedAt;
+ if (elapsed >= PASTE_DEADLINE_MS) {
+ sendPrompt('fallback');
+ clearInterval(promptTimer);
+ return;
+ }
+ if (elapsed < tuiConfig.promptDelayMs) return;
+ if (firstOutputAt === null) return;
+ if (now - lastOutputAt < READY_IDLE_THRESHOLD_MS) return;
+ sendPrompt('ready');
+ clearInterval(promptTimer);
+ }, READY_POLL_INTERVAL_MS);
const idleTimer = setInterval(() => {
if (!promptSentAt || finalized) return;
@@ -352,6 +465,6 @@ export async function spawnTuiAgent(agentId, task, prompt, workspacePath, model,
}
});
- appendLine(`📟 TUI session started: ${sessionId.slice(0, 8)} (${tuiConfig.commandLine})`);
+ appendLine(`📟 TUI session started: ${sessionId.slice(0, 8)} (${tuiConfig.commandLine})`, { force: true });
return agentId;
}
diff --git a/server/services/agentTuiSpawning.test.js b/server/services/agentTuiSpawning.test.js
index ba117dbfb8..d6213f82e6 100644
--- a/server/services/agentTuiSpawning.test.js
+++ b/server/services/agentTuiSpawning.test.js
@@ -82,7 +82,7 @@ vi.mock('../lib/providerModels.js', () => ({
resolveCliModel: vi.fn((m) => (m === 'codex-configured-default' || !m) ? null : m)
}));
-import { buildTuiSpawnConfig, spawnTuiAgent } from './agentTuiSpawning.js';
+import { buildTuiSpawnConfig, isTuiNoise, spawnTuiAgent } from './agentTuiSpawning.js';
import * as shellService from './shell.js';
import * as cosAgents from './cosAgents.js';
import * as cos from './cos.js';
@@ -148,6 +148,30 @@ describe('agent TUI spawning', () => {
});
});
+describe('isTuiNoise', () => {
+ it('treats banner/status/box-drawing artifacts as noise', () => {
+ expect(isTuiNoise('')).toBe(true);
+ expect(isTuiNoise(' ')).toBe(true);
+ expect(isTuiNoise('─────────────')).toBe(true);
+ expect(isTuiNoise('>')).toBe(true);
+ expect(isTuiNoise('?')).toBe(true);
+ expect(isTuiNoise('Try "how does Layout.jsx work?"')).toBe(true);
+ expect(isTuiNoise('Claude Code v2.1.133')).toBe(true);
+ expect(isTuiNoise('Opus 4.7 with xhigh effort · Claude Max')).toBe(true);
+ expect(isTuiNoise('►► bypass permissions on (shift+tab to cycle)')).toBe(true);
+ // Letter-spaced animation frame from paste rendering
+ expect(isTuiNoise('c l a u d e - d a n g e r o u s l y - s k i p')).toBe(true);
+ });
+
+ it('keeps real agent output', () => {
+ expect(isTuiNoise('Reading file src/App.jsx...')).toBe(false);
+ expect(isTuiNoise('🔧 ReadFile')).toBe(false);
+ expect(isTuiNoise('Done! Refactored the auth middleware.')).toBe(false);
+ // Short prompt-style line — only filtered if it's a *bare* > or ?
+ expect(isTuiNoise('> hello')).toBe(false);
+ });
+});
+
// ─── spawnTuiAgent runtime tests ─────────────────────────────────────────────
// Flush the microtask queue (pending Promise continuations). vi.runAllMicrotasksAsync
@@ -228,11 +252,19 @@ describe('spawnTuiAgent runtime', () => {
// Flush initial async setup (updateAgent calls etc.)
await flushMicrotasks();
- // Fire the prompt timer so promptSentAt is set (promptDelayMs = 100ms)
- await vi.advanceTimersByTimeAsync(110);
+ // Feed a banner-style line so firstOutputAt is set — the paste timer
+ // gates on "we've seen at least one chunk of output" plus an idle window
+ // before sending the prompt (ready-signal detection).
+ await capturedOnData(Buffer.from('Codex booting...\n'));
+ await flushMicrotasks();
+
+ // Advance past the prompt-delay floor (100ms) AND the readiness idle
+ // threshold (1200ms). The poll interval (300ms) ticks during this window
+ // and fires the paste once both gates open, setting promptSentAt.
+ await vi.advanceTimersByTimeAsync(2000);
await flushMicrotasks();
- // Feed 2 meaningful lines AFTER the prompt so meaningfulLinesAfterPrompt >= 2.
+ // Feed 2 meaningful lines AFTER the paste so meaningfulLinesAfterPrompt >= 2.
// These must not match the promptPreview ("do the thing").
await capturedOnData(Buffer.from('Agent output line one\n'));
await capturedOnData(Buffer.from('Agent output line two\n'));