MCPcopy
hub / github.com/claude-code-best/claude-code / hydrateFromCCRv2InternalEvents

Function hydrateFromCCRv2InternalEvents

src/utils/sessionStorage.ts:1669–1760  ·  view source on GitHub ↗
(
  sessionId: string,
)

Source from the content-addressed store, hash-verified

1667 * from the latest compaction boundary.
1668 */
1669export async function hydrateFromCCRv2InternalEvents(
1670 sessionId: string,
1671): Promise<boolean> {
1672 const startMs = Date.now()
1673 switchSession(asSessionId(sessionId))
1674
1675 const project = getProject()
1676 const reader = project.getInternalEventReader()
1677 if (!reader) {
1678 logForDebugging('No internal event reader registered for CCR v2 resume')
1679 return false
1680 }
1681
1682 try {
1683 // Fetch foreground events
1684 const events = await reader()
1685 if (!events) {
1686 logForDebugging('Failed to read internal events for resume')
1687 logForDiagnosticsNoPII('error', 'hydrate_ccr_v2_read_fail')
1688 return false
1689 }
1690
1691 const projectDir = getProjectDir(getOriginalCwd())
1692 await mkdir(projectDir, { recursive: true, mode: 0o700 })
1693
1694 // Write foreground transcript
1695 const sessionFile = getTranscriptPathForSession(sessionId)
1696 const fgContent = events.map(e => jsonStringify(e.payload) + '\n').join('')
1697 await writeFile(sessionFile, fgContent, { encoding: 'utf8', mode: 0o600 })
1698
1699 logForDebugging(
1700 `Hydrated ${events.length} foreground entries from CCR v2 internal events`,
1701 )
1702
1703 // Fetch and write subagent events
1704 let subagentEventCount = 0
1705 const subagentReader = project.getInternalSubagentEventReader()
1706 if (subagentReader) {
1707 const subagentEvents = await subagentReader()
1708 if (subagentEvents && subagentEvents.length > 0) {
1709 subagentEventCount = subagentEvents.length
1710 // Group by agent_id
1711 const byAgent = new Map<string, Record<string, unknown>[]>()
1712 for (const e of subagentEvents) {
1713 const agentId = e.agent_id || ''
1714 if (!agentId) continue
1715 let list = byAgent.get(agentId)
1716 if (!list) {
1717 list = []
1718 byAgent.set(agentId, list)
1719 }
1720 list.push(e.payload)
1721 }
1722
1723 // Write each agent's transcript to its own file
1724 for (const [agentId, entries] of byAgent) {
1725 const agentFile = getAgentTranscriptPath(asAgentId(agentId))
1726 await mkdir(dirname(agentFile), { recursive: true, mode: 0o700 })

Callers 1

loadInitialMessagesFunction · 0.85

Calls 15

switchSessionFunction · 0.85
asSessionIdFunction · 0.85
getProjectFunction · 0.85
logForDiagnosticsNoPIIFunction · 0.85
getOriginalCwdFunction · 0.85
mkdirFunction · 0.85
jsonStringifyFunction · 0.85
getAgentTranscriptPathFunction · 0.85
asAgentIdFunction · 0.85
nowMethod · 0.80

Tested by

no test coverage detected