From 218d0c5199667ca4b3ee73a58d2613d89edec115 Mon Sep 17 00:00:00 2001 From: Max Heimbrock <43608204+MaxHeimbrock@users.noreply.github.com> Date: Fri, 3 Jul 2026 13:40:07 +0200 Subject: [PATCH] Fix dropped first chunk in incremental stream readers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ReadIncremental() sent the FFI request before constructing the ReadIncrementalInstruction that subscribes to reader events. Chunk events are delivered directly on the FFI callback thread, and the FFI server emits already-buffered chunks as soon as it receives the request, so any chunk emitted before the subscription existed was silently lost. For delta text streams (e.g. agent transcriptions) this intermittently dropped the first word(s) of a reply. Subscribe first, then send — matching the order ReadAll() already uses. Applies to both TextStreamReader and ByteStreamReader. Co-Authored-By: Claude Fable 5 --- Runtime/Scripts/DataStreams/ByteDataStream.cs | 9 +++++++-- Runtime/Scripts/DataStreams/TextDataStream.cs | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Runtime/Scripts/DataStreams/ByteDataStream.cs b/Runtime/Scripts/DataStreams/ByteDataStream.cs index d3072700..e5185e6f 100644 --- a/Runtime/Scripts/DataStreams/ByteDataStream.cs +++ b/Runtime/Scripts/DataStreams/ByteDataStream.cs @@ -83,9 +83,14 @@ public ReadIncrementalInstruction ReadIncremental() using var request = FFIBridge.Instance.NewRequest(); var readIncReq = request.request; readIncReq.ReaderHandle = (ulong)_handle.DangerousGetHandle(); - request.Send(); - return new ReadIncrementalInstruction(_handle); + // Subscribe before sending: chunk events are delivered directly on the FFI + // callback thread, and the FFI server emits already-buffered chunks as soon + // as it receives the request. Sending first loses any chunk emitted before + // the subscription exists. + var instruction = new ReadIncrementalInstruction(_handle); + request.Send(); + return instruction; } /// diff --git a/Runtime/Scripts/DataStreams/TextDataStream.cs b/Runtime/Scripts/DataStreams/TextDataStream.cs index 4587a5c0..f6181a2c 100644 --- a/Runtime/Scripts/DataStreams/TextDataStream.cs +++ b/Runtime/Scripts/DataStreams/TextDataStream.cs @@ -122,9 +122,14 @@ public ReadIncrementalInstruction ReadIncremental() using var request = FFIBridge.Instance.NewRequest(); var readIncReq = request.request; readIncReq.ReaderHandle = (ulong)_handle.DangerousGetHandle(); - request.Send(); - return new ReadIncrementalInstruction(_handle); + // Subscribe before sending: chunk events are delivered directly on the FFI + // callback thread, and the FFI server emits already-buffered chunks as soon + // as it receives the request. Sending first loses any chunk emitted before + // the subscription exists. + var instruction = new ReadIncrementalInstruction(_handle); + request.Send(); + return instruction; } ///