AI engineering traps (TypeScript) · Chapter 14 of 16
Streaming bytes cut in half
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Tools
- 3 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of AI engineering traps (TypeScript) are free. This one comes with the all labs plan.
Why you need this
The network decides where one piece of the stream ends and the next begins. On a fast connection, each event of the model's reply tends to arrive whole, and the bug never shows. On a slow one, pieces are small and cut anywhere. An English letter cannot be cut, it is one byte; a Hindi one can. So the bug hides in every test written in English on a laptop.
Deleting the � afterwards does not help: the bytes of those letters are already thrown away, so the words come out with letters missing.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.ts: stream the Hindi notice over a slow network
Write reproduce() in repro.ts. Ask streamNotice from net.ts for the ticket's notice, with these four lines as the facts, one string, a line each: सोसाइटी: गोमती हाइट्स, then - शनिवार 8 नवंबर को सुबह 10 बजे से शाम 4 बजे तक पानी की आपूर्ति बंद रहेगी।, then - कृपया शुक्रवार रात को पानी भर कर रखें।, then - लिफ्ट B सुबह 11 बजे से दोपहर 1 बजे तक बंद रहेगी। Pass the stream through smallChunks(stream, 5), then read it with readNotice from stream.ts, and resolve to the notice's last line.
The check runs your reproduce() twice: beside the original stream.ts the line must have the replacement character � in it (the broken sign-off in the ticket), and beside a fixed stream.ts it must be 'प्रबंध समिति, गोमती हाइट्स'. reproduce() must finish within 10 seconds.
- Task 2
stream.ts: decode the stream, not each chunk
Fix readNotice in stream.ts. Turn the bytes into text with one TextDecoder for the whole stream, decoding each chunk with { stream: true }, so a character cut between two chunks is kept until the rest of it arrives. The notice must come out exactly as the model wrote it, however the bytes are cut.
Keep the line buffer, the events and the log line stream: <n> characters as they are. The check streams its own notices, in Hindi and in English, in pieces from 1 to 64 bytes, and compares every character. It also type-checks stream.ts. Deleting the � from the notice is not a fix. net.ts is not to be changed.
- Task 3
stream.test.ts: a test that would have caught it
Write a vitest test file, stream.test.ts, that would have caught the broken Hindi notices. The check runs your file three times: beside the original stream.ts, where at least one test must fail; beside the fixed stream.ts, where every test must pass; and beside a wrong fix that deletes the � from the notice, where at least one test must fail.
So stream a Hindi notice of your own through smallChunks with small pieces and expect its last line to be the sign-off word for word: प्रबंध समिति, then your society's name in Hindi. Each test must finish within 10 seconds.
What you will learn
- repro.ts: stream the Hindi notice over a slow network
- stream.ts: decode the stream, not each chunk
- stream.test.ts: a test that would have caught it
More in Tools
Build "Streaming bytes cut in half" yourself
Sunwai, a Lucknow app for housing societies, runs on a model, and Farah, its CTO, keeps assigning you tickets. Each is a trap AI engineering interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of AI engineering traps (TypeScript) are free. This one comes with the all labs plan.