MCPcopy Index your code
hub / github.com/codeaashu/claude-code / walkChainBeforeParse

Function walkChainBeforeParse

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

Source from the content-addressed store, hash-verified

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

Callers 1

loadTranscriptFileFunction · 0.85

Calls 7

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

Tested by

no test coverage detected