( sessionId: string, )
| 1788 | * from the latest compaction boundary. |
| 1789 | */ |
| 1790 | export async function hydrateFromCCRv2InternalEvents( |
| 1791 | sessionId: string, |
| 1792 | ): Promise<boolean> { |
| 1793 | const startMs = Date.now() |
| 1794 | switchSession(asSessionId(sessionId)) |
| 1795 | |
| 1796 | const project = getProject() |
| 1797 | const reader = project.getInternalEventReader() |
| 1798 | if (!reader) { |
| 1799 | logForDebugging('No internal event reader registered for CCR v2 resume') |
| 1800 | return false |
| 1801 | } |
| 1802 | |
| 1803 | try { |
| 1804 | // Fetch foreground events |
| 1805 | const events = await reader() |
| 1806 | if (!events) { |
| 1807 | logForDebugging('Failed to read internal events for resume') |
| 1808 | logForDiagnosticsNoPII('error', 'hydrate_ccr_v2_read_fail') |
| 1809 | return false |
| 1810 | } |
| 1811 | |
| 1812 | const projectDir = getProjectDir(getOriginalCwd()) |
| 1813 | await mkdir(projectDir, { recursive: true, mode: 0o700 }) |
| 1814 | |
| 1815 | // Write foreground transcript |
| 1816 | const sessionFile = getTranscriptPathForSession(sessionId) |
| 1817 | const fgContent = events.map(e => jsonStringify(e.payload) + '\n').join('') |
| 1818 | await writeFile(sessionFile, fgContent, { encoding: 'utf8', mode: 0o600 }) |
| 1819 | |
| 1820 | logForDebugging( |
| 1821 | `Hydrated ${events.length} foreground entries from CCR v2 internal events`, |
| 1822 | ) |
| 1823 | |
| 1824 | // Fetch and write subagent events |
| 1825 | let subagentEventCount = 0 |
| 1826 | const subagentReader = project.getInternalSubagentEventReader() |
| 1827 | if (subagentReader) { |
| 1828 | const subagentEvents = await subagentReader() |
| 1829 | if (subagentEvents && subagentEvents.length > 0) { |
| 1830 | subagentEventCount = subagentEvents.length |
| 1831 | // Group by agent_id |
| 1832 | const byAgent = new Map<string, Record<string, unknown>[]>() |
| 1833 | for (const e of subagentEvents) { |
| 1834 | const agentId = e.agent_id || '' |
| 1835 | if (!agentId) continue |
| 1836 | let list = byAgent.get(agentId) |
| 1837 | if (!list) { |
| 1838 | list = [] |
| 1839 | byAgent.set(agentId, list) |
| 1840 | } |
| 1841 | list.push(e.payload) |
| 1842 | } |
| 1843 | |
| 1844 | // Write each agent's transcript to its own file |
| 1845 | for (const [agentId, entries] of byAgent) { |
| 1846 | const agentFile = getAgentTranscriptPath(asAgentId(agentId)) |
| 1847 | await mkdir(dirname(agentFile), { recursive: true, mode: 0o700 }) |
no test coverage detected