MCPcopy Create free account
hub / github.com/WJX20/claude-code / walkChainBeforeParse

Function walkChainBeforeParse

src/utils/sessionStorage.ts:3307–3467  ·  view source on GitHub ↗
(buf: Buffer)

Source from the content-addressed store, hash-verified

3305}
3306
3307function walkChainBeforeParse(buf: Buffer): Buffer {
3308 const NEWLINE = 0x0a
3309 const OPEN_BRACE = 0x7b
3310 const QUOTE = 0x22
3311 const PARENT_PREFIX = Buffer.from('{"parentUuid":')
3312 const UUID_KEY = Buffer.from('"uuid":"')
3313 const SIDECHAIN_TRUE = Buffer.from('"isSidechain":true')
3314 const UUID_LEN = 36
3315 const TS_SUFFIX = Buffer.from('","timestamp":"')
3316 const TS_SUFFIX_LEN = TS_SUFFIX.length
3317 const PREFIX_LEN = PARENT_PREFIX.length
3318 const KEY_LEN = UUID_KEY.length
3319
3320 // Stride-3 flat index of transcript messages: [lineStart, lineEnd, parentStart].
3321 // parentStart is the byte offset of the parent uuid's first char, or -1 for null.
3322 // Metadata lines (summary, mode, file-history-snapshot, etc.) go in metaRanges
3323 // unfiltered - they lack the parentUuid prefix and downstream needs all of them.
3324 const msgIdx: number[] = []
3325 const metaRanges: number[] = []
3326 const uuidToSlot = new Map<string, number>()
3327
3328 let pos = 0
3329 const len = buf.length
3330 while (pos < len) {
3331 const nl = buf.indexOf(NEWLINE, pos)
3332 const lineEnd = nl === -1 ? len : nl + 1
3333 if (
3334 lineEnd - pos > PREFIX_LEN &&
3335 buf[pos] === OPEN_BRACE &&
3336 buf.compare(PARENT_PREFIX, 0, PREFIX_LEN, pos, pos + PREFIX_LEN) === 0
3337 ) {
3338 // `{"parentUuid":null,` or `{"parentUuid":"<36 chars>",`
3339 const parentStart =
3340 buf[pos + PREFIX_LEN] === QUOTE ? pos + PREFIX_LEN + 1 : -1
3341 // The top-level uuid is immediately followed by `","timestamp":"` in
3342 // user/assistant/attachment entries (the create* helpers put them
3343 // adjacent; both always defined). But the suffix is NOT unique:
3344 // - agent_progress entries carry a nested Message in data.message,
3345 // serialized BEFORE top-level uuid — that inner Message has its
3346 // own uuid,timestamp adjacent, so its bytes also satisfy the
3347 // suffix check.
3348 // - mcpMeta/toolUseResult come AFTER top-level uuid and hold
3349 // server-controlled Record<string,unknown> — a server returning
3350 // {uuid:"<36>",timestamp:"..."} would also match.
3351 // Collect all suffix matches; a single one is unambiguous (common
3352 // case), multiple need a brace-depth check to pick the one at
3353 // JSON nesting depth 1. Entries with NO suffix match (some progress
3354 // variants put timestamp BEFORE uuid → `"uuid":"<36>"}` at EOL)
3355 // have only one `"uuid":"` and the first-match fallback is sound.
3356 let firstAny = -1
3357 let suffix0 = -1
3358 let suffixN: number[] | undefined
3359 let from = pos
3360 for (;;) {
3361 const next = buf.indexOf(UUID_KEY, from)
3362 if (next < 0 || next >= lineEnd) break
3363 if (firstAny < 0) firstAny = next
3364 const after = next + KEY_LEN + UUID_LEN

Callers 1

loadTranscriptFileFunction · 0.85

Calls 6

setMethod · 0.80
toStringMethod · 0.65
hasMethod · 0.45
addMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected