BitFun icon indicating copy to clipboard operation
BitFun copied to clipboard

[BUG] taiji-engine: Dag::add_edge does not deduplicate, causing topological sort panic

Open 1688mengdie opened this issue 4 months ago • 1 comments

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_edge calls
  • sort() uses neighbors.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

1688mengdie avatar Jul 22 '26 07:07 1688mengdie

已迁移至太极独立仓库。本 Issue 为太极项目专属 BUG,不属于 BitFun 通用范畴,故关闭。修复代码保留在 PR #1674 中供参考。

1688mengdie avatar Jul 22 '26 20:07 1688mengdie