( sessionId: string, )
| 1630 | * from the latest compaction boundary. |
| 1631 | */ |
| 1632 | export async function hydrateFromCCRv2InternalEvents( |
| 1633 | sessionId: string, |
| 1634 | ): Promise<boolean> { |
| 1635 | const startMs = Date.now() |
| 1636 | switchSession(asSessionId(sessionId)) |
| 1637 | |
| 1638 | const project = getProject() |
| 1639 | const reader = project.getInternalEventReader() |
| 1640 | if (!reader) { |
| 1641 | logForDebugging('No internal event reader registered for CCR v2 resume') |
| 1642 | return false |
| 1643 | } |
| 1644 | |
| 1645 | try { |
| 1646 | // Fetch foreground events |
| 1647 | const events = await reader() |
| 1648 | if (!events) { |
| 1649 | logForDebugging('Failed to read internal events for resume') |
| 1650 | logForDiagnosticsNoPII('error', 'hydrate_ccr_v2_read_fail') |
| 1651 | return false |
| 1652 | } |
| 1653 | |
| 1654 | const projectDir = getProjectDir(getOriginalCwd()) |
| 1655 | await mkdir(projectDir, { recursive: true, mode: 0o700 }) |
| 1656 | |
| 1657 | // Write foreground transcript |
| 1658 | const sessionFile = getTranscriptPathForSession(sessionId) |
| 1659 | const fgContent = events.map(e => jsonStringify(e.payload) + '\n').join('') |
| 1660 | await writeFile(sessionFile, fgContent, { encoding: 'utf8', mode: 0o600 }) |
| 1661 | |
| 1662 | logForDebugging( |
| 1663 | `Hydrated ${events.length} foreground entries from CCR v2 internal events`, |
| 1664 | ) |
| 1665 | |
| 1666 | // Fetch and write subagent events |
| 1667 | let subagentEventCount = 0 |
| 1668 | const subagentReader = project.getInternalSubagentEventReader() |
| 1669 | if (subagentReader) { |
| 1670 | const subagentEvents = await subagentReader() |
| 1671 | if (subagentEvents && subagentEvents.length > 0) { |
| 1672 | subagentEventCount = subagentEvents.length |
| 1673 | // Group by agent_id |
| 1674 | const byAgent = new Map<string, Record<string, unknown>[]>() |
| 1675 | for (const e of subagentEvents) { |
| 1676 | const agentId = e.agent_id || '' |
| 1677 | if (!agentId) continue |
| 1678 | let list = byAgent.get(agentId) |
| 1679 | if (!list) { |
| 1680 | list = [] |
| 1681 | byAgent.set(agentId, list) |
| 1682 | } |
| 1683 | list.push(e.payload) |
| 1684 | } |
| 1685 | |
| 1686 | // Write each agent's transcript to its own file |
| 1687 | for (const [agentId, entries] of byAgent) { |
| 1688 | const agentFile = getAgentTranscriptPath(asAgentId(agentId)) |
| 1689 | await mkdir(dirname(agentFile), { recursive: true, mode: 0o700 }) |
no test coverage detected