Two streaming generation paths do super-linear work on the model output and can
block the Node event loop (and balloon the heap) on large or pathological
responses:
1. app/api/generate/scene-outlines-stream re-parses the ENTIRE accumulated
buffer on every streamed chunk — extractLanguageDirective (full-buffer regex)
and extractNewOutlines (char-walk from the array start). That is O(n²) over
the stream; a ~300 KB response blocks the event loop for ~20 s.
2. lib/generation/interactive-post-processor convertLatexDelimiters restores
<script> placeholders by rebuilding the whole HTML string once per block —
O(blocks × length). A widget with many <script> tags blocks for tens of
seconds and allocates GBs.
Fixes:
- extractNewOutlines: parse incrementally, resuming from a cursor so the buffer
is scanned once across the stream (O(n)). Behaviour-equivalent — verified
across chunk sizes, markdown fences, flat arrays, prose preambles, and
braces/escapes inside string values.
- extractLanguageDirective: bound the regex to the buffer head (the directive is
the first wrapper key), O(1) per chunk.
- Restore <script> blocks in a single pass with a function replacer ($-safe).
- Stream-loop hardening: honor request abort and propagate the AbortSignal to
the LLM call so upstream generation stops on disconnect; cap the accumulated
buffer; guard controller.close() against double-close.
Standalone repro (copied functions): 300 KB outline stream 20.8 s -> 1.3 s;
45 MB widget HTML >30 s + OOM -> 45 ms.
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>