getAncestors method
Returns the ordered ancestor chain from root to eventId (inclusive).
The first element is the oldest ancestor (root cause), the last is the event itself. Returns empty list if event not found.
Detects cycles via visited set — should never happen in a DAG but protects against data corruption.
Implementation
List<CausalEvent> getAncestors(String eventId) {
final chain = <CausalEvent>[];
String? current = eventId;
final visited = <String>{};
while (current != null && !visited.contains(current)) {
visited.add(current);
final event = _nodes[current];
if (event != null) {
chain.insert(0, event); // prepend — oldest first
}
current = _parentEdge[current];
}
return chain;
}