Awesome Reviewers

When transforming/aggregating streamed or ranged data, explicitly define the invariants your algorithm depends on (index meaning, ownership/copy semantics, boundary semantics, and ordering), then implement collision- and edge-case-safe logic with targeted tests.

Practical rules: 1) Specify index/span semantics and keep them consistent across paths.

Collision-safe terminal-chunk handling (adapted from the discussed fix):

if isFinalChunk && chunk.StreamResponse != nil {
    if acc.TerminalResponseChunkIndex >= 0 {
        chunk.ChunkIndex = acc.TerminalResponseChunkIndex
    } else {
        if chunk.ChunkIndex <= acc.MaxResponsesChunkIndex {
            acc.MaxResponsesChunkIndex++
            chunk.ChunkIndex = acc.MaxResponsesChunkIndex
        }
        acc.TerminalResponseChunkIndex = chunk.ChunkIndex
    }
}

Test guidance: