* Lightweight forward scan of [0, endOffset) collecting only metadata-entry lines. * Uses raw Buffer chunks and byte-level marker matching — no readline, no per-line * string conversion for the ~99% of lines that are message content. * * Fast path: if a chunk contains zero markers (the common ca
( filePath: string, endOffset: number, )
| 3155 | * are <50 per session), the entire chunk is skipped without line splitting. |
| 3156 | */ |
| 3157 | async function scanPreBoundaryMetadata( |
| 3158 | filePath: string, |
| 3159 | endOffset: number, |
| 3160 | ): Promise<string[]> { |
| 3161 | const { createReadStream } = await import('fs') |
| 3162 | const NEWLINE = 0x0a |
| 3163 | |
| 3164 | const stream = createReadStream(filePath, { end: endOffset - 1 }) |
| 3165 | const metadataLines: string[] = [] |
| 3166 | let carry: Buffer | null = null |
| 3167 | |
| 3168 | for await (const chunk of stream) { |
| 3169 | const chunkBuf = chunk as Buffer |
| 3170 | const buf = resolveMetadataBuf(carry, chunkBuf) |
| 3171 | if (buf === null) { |
| 3172 | carry = null |
| 3173 | continue |
| 3174 | } |
| 3175 | |
| 3176 | // Fast path: most chunks contain zero metadata markers. Skip line splitting. |
| 3177 | let hasAnyMarker = false |
| 3178 | for (const m of METADATA_MARKER_BUFS) { |
| 3179 | if (buf.includes(m)) { |
| 3180 | hasAnyMarker = true |
| 3181 | break |
| 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | if (hasAnyMarker) { |
| 3186 | let lineStart = 0 |
| 3187 | let nl = buf.indexOf(NEWLINE) |
| 3188 | while (nl !== -1) { |
| 3189 | // Bounded marker check: only look within this line's byte range |
| 3190 | for (const m of METADATA_MARKER_BUFS) { |
| 3191 | const mIdx = buf.indexOf(m, lineStart) |
| 3192 | if (mIdx !== -1 && mIdx < nl) { |
| 3193 | metadataLines.push(buf.toString('utf-8', lineStart, nl)) |
| 3194 | break |
| 3195 | } |
| 3196 | } |
| 3197 | lineStart = nl + 1 |
| 3198 | nl = buf.indexOf(NEWLINE, lineStart) |
| 3199 | } |
| 3200 | carry = buf.subarray(lineStart) |
| 3201 | } else { |
| 3202 | // No markers in this chunk — just preserve the incomplete trailing line |
| 3203 | const lastNl = buf.lastIndexOf(NEWLINE) |
| 3204 | carry = lastNl >= 0 ? buf.subarray(lastNl + 1) : buf |
| 3205 | } |
| 3206 | |
| 3207 | // Guard against quadratic carry growth for pathological huge lines |
| 3208 | // (e.g., a 10 MB tool-output line with no newline). Real metadata entries |
| 3209 | // are <1 KB, so if carry exceeds this we're mid-message-content — drop it. |
| 3210 | if (carry.length > 64 * 1024) carry = null |
| 3211 | } |
| 3212 | |
| 3213 | // Final incomplete line (no trailing newline at endOffset) |
| 3214 | if (carry !== null && carry.length > 0) { |
no test coverage detected