MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / readLiteMetadata

Function readLiteMetadata

source code/utils/sessionStorage.ts:4741–4815  ·  view source on GitHub ↗

* Reads the first and last ~64KB of a JSONL file and extracts lite metadata. * * Head (first 64KB): isSidechain, projectPath, teamName, firstPrompt. * Tail (last 64KB): customTitle, tag, PR link, latest gitBranch. * * Accepts a shared buffer to avoid per-file allocation overhead.

(
  filePath: string,
  fileSize: number,
  buf: Buffer,
)

Source from the content-addressed store, hash-verified

4739 * Accepts a shared buffer to avoid per-file allocation overhead.
4740 */
4741async function readLiteMetadata(
4742 filePath: string,
4743 fileSize: number,
4744 buf: Buffer,
4745): Promise<LiteMetadata> {
4746 const { head, tail } = await readHeadAndTail(filePath, fileSize, buf)
4747 if (!head) return { firstPrompt: '', isSidechain: false }
4748
4749 // Extract stable metadata from the first line via string search.
4750 // Works even when the first line is truncated (>64KB message).
4751 const isSidechain =
4752 head.includes('"isSidechain":true') || head.includes('"isSidechain": true')
4753 const projectPath = extractJsonStringField(head, 'cwd')
4754 const teamName = extractJsonStringField(head, 'teamName')
4755 const agentSetting = extractJsonStringField(head, 'agentSetting')
4756
4757 // Prefer the last-prompt tail entry — captured by extractFirstPrompt at
4758 // write time (filtered, authoritative) and shows what the user was most
4759 // recently doing. Head scan is the fallback for sessions written before
4760 // last-prompt entries existed. Raw string scrapes of head are last resort
4761 // and catch array-format content blocks (VS Code <ide_selection> metadata).
4762 const firstPrompt =
4763 extractLastJsonStringField(tail, 'lastPrompt') ||
4764 extractFirstPromptFromChunk(head) ||
4765 extractJsonStringFieldPrefix(head, 'content', 200) ||
4766 extractJsonStringFieldPrefix(head, 'text', 200) ||
4767 ''
4768
4769 // Extract tail metadata via string search (last occurrence wins).
4770 // User titles (customTitle field, from custom-title entries) win over
4771 // AI titles (aiTitle field, from ai-title entries). The distinct field
4772 // names mean extractLastJsonStringField naturally disambiguates.
4773 const customTitle =
4774 extractLastJsonStringField(tail, 'customTitle') ??
4775 extractLastJsonStringField(head, 'customTitle') ??
4776 extractLastJsonStringField(tail, 'aiTitle') ??
4777 extractLastJsonStringField(head, 'aiTitle')
4778 const summary = extractLastJsonStringField(tail, 'summary')
4779 const tag = extractLastJsonStringField(tail, 'tag')
4780 const gitBranch =
4781 extractLastJsonStringField(tail, 'gitBranch') ??
4782 extractJsonStringField(head, 'gitBranch')
4783
4784 // PR link fields — prNumber is a number not a string, so try both
4785 const prUrl = extractLastJsonStringField(tail, 'prUrl')
4786 const prRepository = extractLastJsonStringField(tail, 'prRepository')
4787 let prNumber: number | undefined
4788 const prNumStr = extractLastJsonStringField(tail, 'prNumber')
4789 if (prNumStr) {
4790 prNumber = parseInt(prNumStr, 10) || undefined
4791 }
4792 if (!prNumber) {
4793 const prNumMatch = tail.lastIndexOf('"prNumber":')
4794 if (prNumMatch >= 0) {
4795 const afterColon = tail.slice(prNumMatch + 11, prNumMatch + 25)
4796 const num = parseInt(afterColon.trim(), 10)
4797 if (num > 0) prNumber = num
4798 }

Callers 1

enrichLogFunction · 0.85

Calls 5

readHeadAndTailFunction · 0.85
extractJsonStringFieldFunction · 0.85

Tested by

no test coverage detected