From 31340fe0a85c9e129758d2d436f3c87b10725b6a Mon Sep 17 00:00:00 2001 From: Jared Miller Date: Sat, 31 Jan 2026 11:06:37 -0500 Subject: [PATCH] Fix async terminal.write() causing garbled dashboard output The terminal.write() method from @xterm/headless is async - the data isn't in the buffer yet when we call serializeAsHTML() immediately after. This caused empty or partially rendered output in the browser. Now using the callback form of terminal.write(data, callback) to wait for the write to complete before serializing and broadcasting to SSE clients. This ensures the terminal buffer is fully updated before we generate HTML from it. --- src/server.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/server.ts b/src/server.ts index 9c38fad..6f6b495 100644 --- a/src/server.ts +++ b/src/server.ts @@ -520,20 +520,21 @@ const server = Bun.serve({ return; } - // Write to terminal emulator (handles all ANSI sequences) - termSession.terminal.write(msg.data); - // Store raw ANSI in database appendOutput(sessionId, msg.data); - // Serialize current terminal state as HTML - const html = serializeAsHTML(termSession); + // Write to terminal emulator (handles all ANSI sequences) + // Use callback to wait for write completion before serializing + termSession.terminal.write(msg.data, () => { + // Serialize current terminal state as HTML + const html = serializeAsHTML(termSession); - // Broadcast to dashboards - broadcastSSE({ - type: "output", - session_id: sessionId, - data: html, + // Broadcast to dashboards + broadcastSSE({ + type: "output", + session_id: sessionId, + data: html, + }); }); return;