okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0b3299a01
main
1/** 2* Stream framing: partial UTF-8 and split SSE lines must not corrupt output (checklist A02). 3* 4* This is the failure mode that looks like a model problem and is actually a decoder problem. A 5* multi-byte character split across two TCP reads becomes U+FFFD if the decoder is reset per chunk, 6* and an SSE line split across two reads is dropped or merged if the buffer is not carried. Both 7* produce answers with mojibake or missing text in them, which is very hard to attribute later. 8* 9* The fixture therefore splits at the **byte** level, not at convenient character boundaries, so the 10* test cannot accidentally pass because the writer happened to split somewhere safe. 11*/ 12import test from 'node:test' ; 13import assert from 'node:assert/strict' ; 14import http from 'node:http' ; 15import { mkdtemp , rm } from 'node:fs/promises' ; 16import { tmpdir } from 'node:os' ; 17import { join } from 'node:path' ; 18 19const wait = ( ms ) => new Promise (( r ) => setTimeout ( r , ms )); 20 21async function listen ( server ) { 22await new Promise (( r ) => server . listen ( 0 , '127.0.0.1' , r )); 23return server . address (). port ; 24} 25 26/** An SSE data line carrying one delta. */ 27function line ( text ) { 28return `data: ${ JSON . stringify ({ choices : [{ delta : { content : text } }] })} \n\n` ; 29} 30 31/** Split a UTF-8 string into byte chunks of `n` bytes, so multi-byte characters straddle them. */ 32function byteChunks ( text , n ) { 33const bytes = Buffer . from ( text , 'utf8' ); 34const out = []; 35for ( let i = 0 ; i < bytes . length ; i += n ) out . push ( bytes . subarray ( i , i + n )); 36return out ; 37} 38 39async function loadCompletion () { 40const home = await mkdtemp ( join ( tmpdir (), 'strata-framing-' )); 41process . env . STRATA_HOME = home ; 42process . env . STRATA_MODELS_DIR = join ( home , 'models' ); 43const { proxyCompletion} = await import ( '../server/inference.mjs' ); 44return { proxyCompletion, home}; 45} 46 47test ( 'a multi-byte character split across reads survives intact' , async () => { 48const { proxyCompletion, home} = await loadCompletion (); 49// Every one of these is multi-byte in UTF-8, and several are outside the BMP (4 bytes), so a 50// chunk boundary can land inside any of them. 51const wanted = 'héllo — 世界 🌍 日本語 ✅' ; 52const payload = line ( wanted ) + 'data: [DONE]\n\n' ; 53const fixture = http . createServer (( req , res ) => { 54req . resume (); 55res . writeHead ( 200 , { 'content-type' :'text/event-stream' }); 56// Three bytes at a time: every emoji and every CJK character is straddled. 57for ( const chunk of byteChunks ( payload , 3 )) res . write ( chunk ); 58res . end (); 59}); 60const port = await listen ( fixture ); 61try { 62const frames = []; 63await proxyCompletion ({ 64baseUrl :`http://127.0.0.1: ${ port } ` , message :'x' , onFrame :( f ) => frames . push ( f ), 65signal :new AbortController (). signal , 66}); 67const text = frames . filter (( f ) => f . type === 'delta' ). map (( f ) => f . text ). join ( '' ); 68assert . equal ( text , wanted , 'the streamed text must be byte-identical to what was sent' ); 69assert . ok ( ! text . includes ( '\uFFFD' ), 'no replacement character may appear' ); 70} finally { 71await new Promise (( r ) => fixture . close ( r )); 72await rm ( home , { recursive :true , force :true }); 73} 74}); 75 76test ( 'an SSE line split across reads is reassembled, not dropped' , async () => { 77const { proxyCompletion, home} = await loadCompletion (); 78const fixture = http . createServer (( req , res ) => { 79req . resume (); 80res . writeHead ( 200 , { 'content-type' :'text/event-stream' }); 81// Split the JSON mid-object, mid-escape and mid-line, with pauses so the reads really separate. 82const body = line ( 'alpha' ) + line ( 'beta' ) + 'data: [DONE]\n\n' ; 83const chunks = byteChunks ( body , 7 ); 84( async () => { 85for ( const c of chunks ) { res . write ( c ); await wait ( 2 ); } 86res . end (); 87})(); 88}); 89const port = await listen ( fixture ); 90try { 91const frames = []; 92await proxyCompletion ({ 93baseUrl :`http://127.0.0.1: ${ port } ` , message :'x' , onFrame :( f ) => frames . push ( f ), 94signal :new AbortController (). signal , 95}); 96const text = frames . filter (( f ) => f . type === 'delta' ). map (( f ) => f . text ). join ( '' ); 97assert . equal ( text , 'alphabeta' , 'both deltas must survive the split' ); 98} finally { 99await new Promise (( r ) => fixture . close ( r )); 100await rm ( home , { recursive :true , force :true }); 101} 102}); 103 104test ( 'a stream that ends without a trailing newline still yields its last delta' , async () => { 105const { proxyCompletion, home} = await loadCompletion (); 106const fixture = http . createServer (( req , res ) => { 107req . resume (); 108res . writeHead ( 200 , { 'content-type' :'text/event-stream' }); 109// No trailing blank line: the reader must flush the remaining buffer when the stream ends. 110res . end ( `data: ${ JSON . stringify ({ choices : [{ delta : { content : 'tail' } }] })} ` ); 111}); 112const port = await listen ( fixture ); 113try { 114const frames = []; 115await proxyCompletion ({ 116baseUrl :`http://127.0.0.1: ${ port } ` , message :'x' , onFrame :( f ) => frames . push ( f ), 117signal :new AbortController (). signal , 118}); 119const text = frames . filter (( f ) => f . type === 'delta' ). map (( f ) => f . text ). join ( '' ); 120assert . equal ( text , 'tail' ); 121} finally { 122await new Promise (( r ) => fixture . close ( r )); 123await rm ( home , { recursive :true , force :true }); 124} 125}); 126 127test ( 'a malformed frame is skipped without losing the frames around it' , async () => { 128const { proxyCompletion, home} = await loadCompletion (); 129const fixture = http . createServer (( req , res ) => { 130req . resume (); 131res . writeHead ( 200 , { 'content-type' :'text/event-stream' }); 132// A truncated JSON object in the middle. The contract is that it is skipped, not that it 133// aborts the turn: a provider may emit a partial line and the answer is still usable. 134res . end ( line ( 'before' ) + 'data: {"choices":[{"delta":{"content":"trunc\n\n' + line ( 'after' ) + 'data: [DONE]\n\n' ); 135}); 136const port = await listen ( fixture ); 137try { 138const frames = []; 139await proxyCompletion ({ 140baseUrl :`http://127.0.0.1: ${ port } ` , message :'x' , onFrame :( f ) => frames . push ( f ), 141signal :new AbortController (). signal , 142}); 143const text = frames . filter (( f ) => f . type === 'delta' ). map (( f ) => f . text ). join ( '' ); 144assert . equal ( text , 'beforeafter' , 'the good frames on both sides must survive' ); 145} finally { 146await new Promise (( r ) => fixture . close ( r )); 147await rm ( home , { recursive :true , force :true }); 148} 149}); 150 151test ( 'an error frame is surfaced rather than silently ending the turn' , async () => { 152const { proxyCompletion, home} = await loadCompletion (); 153const fixture = http . createServer (( req , res ) => { 154req . resume (); 155res . writeHead ( 200 , { 'content-type' :'text/event-stream' }); 156res . end ( `data: ${ JSON . stringify ({ error : { message : 'the engine refused this request' } })} \n\n` ); 157}); 158const port = await listen ( fixture ); 159try { 160await assert . rejects ( 161() => proxyCompletion ({ 162baseUrl :`http://127.0.0.1: ${ port } ` , message :'x' , onFrame :() => {}, 163signal :new AbortController (). signal , 164}), 165/ refused this request / , 166'an error frame must reach the caller with the provider\'s own words' , 167); 168} finally { 169await new Promise (( r ) => fixture . close ( r )); 170await rm ( home , { recursive :true , force :true }); 171} 172});