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
fix: decode createLineReader chunks through StringDecoder for multibyte-safe line splitting (#2392)
Adam Eivy4764da2b1f97
diff --git a/server/lib/streamLines.js b/server/lib/streamLines.jsindex ba638119a3..0e68fda3a9 100644--- a/server/lib/streamLines.js+++ b/server/lib/streamLines.js@@ -10,12 +10,24 @@// helper ends — hand-copied splitters (ytdlpAudioImport, videoDownload) were// dropping that unterminated last line while hfDownload's copy alone flushed.//+// Chunks are decoded through a per-reader `StringDecoder`, so a multibyte+// UTF-8 codepoint split across a chunk boundary (a non-ASCII yt-dlp title,+// say) is stitched back together instead of decoded as two replacement+// characters. Callers may also `push()` plain strings (tests, already-decoded+// input) — those pass straight through.+//// Each stream needs its OWN reader: a shared carry buffer would splice a// partial line from stdout onto a chunk from stderr and corrupt marker lines.//-// `splitRe` defaults to `/\r?\n/` (newline-terminated lines). Pass-// `splitRe: /[\r\n]+/` for torch/tqdm-style progress bars that redraw the same-// line with a bare `\r` and no `\n`, so each redraw surfaces as its own line.+// `splitRe` defaults to `/\r?\n/` (newline-terminated lines; blank lines are+// preserved as empty strings). Pass `splitRe: /[\r\n]+/` for torch/tqdm-style+// progress bars that redraw the same line with a bare `\r` and no `\n`, so each+// redraw surfaces as its own line. A collapsing regex like `/[\r\n]+/` collapses+// separator runs WITHIN a chunk, but a run split across a chunk boundary can+// still yield one empty-string segment — every caller of that option filters+// empty lines, which is the intended usage.++import { StringDecoder } from 'string_decoder';const DEFAULT_SPLIT_RE = /\r?\n/;// Clamp so a stream that never emits a separator can't grow the carry buffer@@ -24,15 +36,16 @@ const DEFAULT_SPLIT_RE = /\r?\n/;const DEFAULT_MAX_CARRY = 1 << 20; // 1 MiBexport function createLineReader(onLine, { splitRe = DEFAULT_SPLIT_RE, maxCarry = DEFAULT_MAX_CARRY } = {}) {+ const decoder = new StringDecoder('utf8');let carry = '';const push = (chunk) => {- carry += chunk.toString();+ carry += typeof chunk === 'string' ? chunk : decoder.write(chunk);const lines = carry.split(splitRe);carry = lines.pop() ?? '';for (const line of lines) onLine(line);if (carry.length > maxCarry) {- // Safety valve: no separator seen within maxCarry bytes. Emit what we+ // Safety valve: no separator seen within maxCarry chars. Emit what we// have as a line rather than buffer unbounded, then reset.const oversized = carry;carry = '';@@ -41,6 +54,9 @@ export function createLineReader(onLine, { splitRe = DEFAULT_SPLIT_RE, maxCarry};const flush = () => {+ // Drain any bytes the decoder is still holding for an incomplete multibyte+ // sequence (a no-op when only strings were pushed).+ carry += decoder.end();if (carry.length === 0) return;const line = carry;carry = '';diff --git a/server/lib/streamLines.test.js b/server/lib/streamLines.test.jsindex 473dde5346..7a98235834 100644--- a/server/lib/streamLines.test.js+++ b/server/lib/streamLines.test.js@@ -62,12 +62,43 @@ describe('createLineReader', () => {expect(lines).toEqual(['10%', '20%', '30%']);});- it('collapses runs of separators with /[\\r\\n]+/ (no empty lines between redraws)', () => {+ it('collapses runs of separators with /[\\r\\n]+/ WITHIN a chunk (no empty lines between redraws)', () => {const { lines, reader } = collect({ splitRe: /[\r\n]+/ });reader.push('a\r\n\r\nb\n');expect(lines).toEqual(['a', 'b']);});+ it('a separator run split across a chunk boundary can yield one empty segment (callers filter empties)', () => {+ // Documents the collapsing-regex boundary behavior codex flagged: because+ // splitting is incremental, a `\r` in one chunk and `\n` in the next are+ // not collapsed into one separator, so an empty segment appears. Every+ // caller that passes /[\r\n]+/ discards empty lines, which is the contract.+ const { lines, reader } = collect({ splitRe: /[\r\n]+/ });+ reader.push('a\r');+ reader.push('\nb\n');+ expect(lines.filter((l) => l !== '')).toEqual(['a', 'b']);+ });++ it('stitches a multibyte UTF-8 codepoint split across chunk boundaries (Buffer input)', () => {+ const { lines, reader } = collect();+ // '🎬' (U+1F3AC) is 4 UTF-8 bytes at indices 6-9 of this string; cut at+ // byte 8 so the emoji is split across the two chunks.+ const full = Buffer.from('TITLE:🎬 clip\n', 'utf8');+ reader.push(full.subarray(0, 8)); // 'TITLE:' + first 2 emoji bytes+ reader.push(full.subarray(8)); // last 2 emoji bytes + ' clip\n'+ expect(lines).toEqual(['TITLE:🎬 clip']);+ expect(lines[0]).not.toContain('�'); // no replacement chars+ });++ it('flush() drains a trailing incomplete multibyte sequence without a newline', () => {+ const { lines, reader } = collect();+ const buf = Buffer.from('café', 'utf8'); // 'é' is 2 bytes+ reader.push(buf.subarray(0, buf.length - 1)); // drop the last byte of 'é'+ reader.push(buf.subarray(buf.length - 1)); // deliver it+ reader.flush();+ expect(lines).toEqual(['café']);+ });+it('clamps the carry buffer when no separator arrives within maxCarry', () => {const { lines, reader } = collect({ maxCarry: 8 });reader.push('123456789'); // 9 chars, exceeds maxCarry of 8, no newline