export interface ParentNode { id: string; parentId: string | null; } export interface Hierarchy { nodes: Map; children: Map; roots: T[]; } export interface HierarchyRow { node: T; depth: number; childCount: number; } export function hierarchy(items: T[]): Hierarchy { const nodes = new Map(items.map((node) => [node.id, node])); const children = new Map(); const roots: T[] = []; for (const node of items) { if (!node.parentId || !nodes.has(node.parentId)) roots.push(node); else { const siblings = children.get(node.parentId) ?? []; siblings.push(node); children.set(node.parentId, siblings); } } // Malformed rootless cycles remain inspectable; original parent IDs are never rewritten. const reached = new Set(); const stack = roots.slice(); const visit = () => { while (stack.length) { const node = stack.pop(); if (!node || reached.has(node.id)) continue; reached.add(node.id); stack.push(...(children.get(node.id) ?? [])); } }; visit(); for (const node of items) if (!reached.has(node.id)) { roots.push(node); stack.push(node); visit(); } return { nodes, children, roots }; } export function ancestors(tree: Hierarchy, id: string): string[] { const result: string[] = []; const seen = new Set([id]); let parent = tree.nodes.get(id)?.parentId; while (parent && tree.nodes.has(parent) && !seen.has(parent)) { seen.add(parent); result.push(parent); parent = tree.nodes.get(parent)?.parentId; } return result; } export function withAncestors(tree: Hierarchy, matches: Set): Set { const keep = new Set(matches); // Each parent edge is visited once even for very long linear histories. for (const id of matches) { let current = tree.nodes.get(id)?.parentId; const seen = new Set([id]); while (current && tree.nodes.has(current) && !seen.has(current)) { seen.add(current); if (keep.has(current)) break; keep.add(current); current = tree.nodes.get(current)?.parentId; } } return keep; } export function flatten( tree: Hierarchy, expanded: Set, keep?: Set, ): HierarchyRow[] { const rows: HierarchyRow[] = []; const seen = new Set(); const stack = tree.roots .slice() .reverse() .map((node) => ({ node, depth: 0 })); while (stack.length) { const row = stack.pop(); if (!row || seen.has(row.node.id) || (keep && !keep.has(row.node.id))) continue; seen.add(row.node.id); const children = (tree.children.get(row.node.id) ?? []).filter((node) => !keep || keep.has(node.id)); rows.push({ ...row, childCount: children.length }); if (expanded.has(row.node.id)) for (const node of children.slice().reverse()) stack.push({ node, depth: row.depth + 1 }); } return rows; } export function reveal(tree: Hierarchy, expanded: Set, id: string): Set { return new Set([...expanded, ...ancestors(tree, id)]); } export function label(node: { kind: string; name?: string; label: string }): string { if (node.name) return node.name; if (node.label && !/^[[{]/.test(node.label.trim())) return node.label; switch (node.kind) { case "context_edit": return "Context contribution updated"; case "compaction": return "Compaction checkpoint"; case "branch_summary": return "Branch summary"; case "custom": return "Custom session entry"; case "label": return "Entry label updated"; case "session_info": return "Session metadata"; default: return "Recorded session entry"; } }