BitFun
BitFun copied to clipboard
[BUG] taiji-engine: Dag::add_edge does not deduplicate, causing topological sort panic
Summary
Dag::add_edge() silently adds duplicate edges. When sort() is called, Kahn's algorithm encounters duplicate neighbors in the in-degree calculation, which can lead to incorrect topological ordering or panic in downstream consumers that assume unique edges.
Root Cause
src/crates/taiji/taiji-engine/src/dag.rs — add_edge() method pushes to into neighbors without checking if it already exists:
// Before fix:
self.edges.entry(from).or_default().push(to);
Evidence
- Duplicate edges accumulate silently over multiple
add_edgecalls sort()usesneighbors.contains(&to)in some paths but not during edge addition- No unit test covered the duplicate edge case
Fix
Added neighbors.contains(&to) guard before pushing, plus debug_assert_eq! in sort() and a new test_add_duplicate_edge_is_idempotent test.
Fixed in
- PR: https://github.com/GCWing/BitFun/pull/1674
- File:
src/crates/taiji/taiji-engine/src/dag.rs
已迁移至太极独立仓库。本 Issue 为太极项目专属 BUG,不属于 BitFun 通用范畴,故关闭。修复代码保留在 PR #1674 中供参考。