(buf: Buffer)
| 3486 | } |
| 3487 | |
| 3488 | function walkChainBeforeParse(buf: Buffer): Buffer { |
| 3489 | const NEWLINE = 0x0a |
| 3490 | const OPEN_BRACE = 0x7b |
| 3491 | const QUOTE = 0x22 |
| 3492 | const PARENT_PREFIX = Buffer.from('{"parentUuid":') |
| 3493 | const UUID_KEY = Buffer.from('"uuid":"') |
| 3494 | const SIDECHAIN_TRUE = Buffer.from('"isSidechain":true') |
| 3495 | const UUID_LEN = 36 |
| 3496 | const TS_SUFFIX = Buffer.from('","timestamp":"') |
| 3497 | const TS_SUFFIX_LEN = TS_SUFFIX.length |
| 3498 | const PREFIX_LEN = PARENT_PREFIX.length |
| 3499 | const KEY_LEN = UUID_KEY.length |
| 3500 | |
| 3501 | // Stride-3 flat index of transcript messages: [lineStart, lineEnd, parentStart]. |
| 3502 | // parentStart is the byte offset of the parent uuid's first char, or -1 for null. |
| 3503 | // Metadata lines (summary, mode, file-history-snapshot, etc.) go in metaRanges |
| 3504 | // unfiltered - they lack the parentUuid prefix and downstream needs all of them. |
| 3505 | const msgIdx: number[] = [] |
| 3506 | const metaRanges: number[] = [] |
| 3507 | const uuidToSlot = new Map<string, number>() |
| 3508 | |
| 3509 | let pos = 0 |
| 3510 | const len = buf.length |
| 3511 | while (pos < len) { |
| 3512 | const nl = buf.indexOf(NEWLINE, pos) |
| 3513 | const lineEnd = nl === -1 ? len : nl + 1 |
| 3514 | if ( |
| 3515 | lineEnd - pos > PREFIX_LEN && |
| 3516 | buf[pos] === OPEN_BRACE && |
| 3517 | buf.compare(PARENT_PREFIX, 0, PREFIX_LEN, pos, pos + PREFIX_LEN) === 0 |
| 3518 | ) { |
| 3519 | // `{"parentUuid":null,` or `{"parentUuid":"<36 chars>",` |
| 3520 | const parentStart = |
| 3521 | buf[pos + PREFIX_LEN] === QUOTE ? pos + PREFIX_LEN + 1 : -1 |
| 3522 | // The top-level uuid is immediately followed by `","timestamp":"` in |
| 3523 | // user/assistant/attachment entries (the create* helpers put them |
| 3524 | // adjacent; both always defined). But the suffix is NOT unique: |
| 3525 | // - agent_progress entries carry a nested Message in data.message, |
| 3526 | // serialized BEFORE top-level uuid — that inner Message has its |
| 3527 | // own uuid,timestamp adjacent, so its bytes also satisfy the |
| 3528 | // suffix check. |
| 3529 | // - mcpMeta/toolUseResult come AFTER top-level uuid and hold |
| 3530 | // server-controlled Record<string,unknown> — a server returning |
| 3531 | // {uuid:"<36>",timestamp:"..."} would also match. |
| 3532 | // Collect all suffix matches; a single one is unambiguous (common |
| 3533 | // case), multiple need a brace-depth check to pick the one at |
| 3534 | // JSON nesting depth 1. Entries with NO suffix match (some progress |
| 3535 | // variants put timestamp BEFORE uuid → `"uuid":"<36>"}` at EOL) |
| 3536 | // have only one `"uuid":"` and the first-match fallback is sound. |
| 3537 | let firstAny = -1 |
| 3538 | let suffix0 = -1 |
| 3539 | let suffixN: number[] | undefined |
| 3540 | let from = pos |
| 3541 | for (;;) { |
| 3542 | const next = buf.indexOf(UUID_KEY, from) |
| 3543 | if (next < 0 || next >= lineEnd) break |
| 3544 | if (firstAny < 0) firstAny = next |
| 3545 | const after = next + KEY_LEN + UUID_LEN |
no test coverage detected