From 924a59e9ef4e47d1e762e240a0e5c0b84463196f Mon Sep 17 00:00:00 2001 From: Yifeng He Date: Thu, 15 Oct 2026 00:37:57 +0000 Subject: [PATCH] fix(core): orient dependency edges in cycle detection (0.1.33) detect_cycles mixed parent->child links with dependent->dependency links, so cross-branch diamonds were reported as ARA110 cycles and a dependency on one's own descendant was missed. Reverse DependsOn edges for the check only. Clears ARA110 on all 58 affected internal trees. --- CHANGELOG.md | 6 +++ Cargo.lock | 8 ++-- Cargo.toml | 2 +- crates/ara-core/src/parse.rs | 63 ++++++++++++++++++++++++++---- docs/dependency-cycle-direction.md | 26 ++++++++++++ 5 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 docs/dependency-cycle-direction.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 5af22b3..3f18047 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -258,6 +258,12 @@ All notable changes to this project are documented here. The format follows `invalid_document` errors name these roots. Old rubric history stays readable. ### Fixed +- Core validation: the dependency-cycle check (`ARA110`, 0.1.33) orients + `also_depends_on` edges from dependency to dependent, the same direction as + `children:` nesting. A cross-branch diamond, such as a node depending on a + node elsewhere that itself depends on an ancestor, is no longer reported as + a cycle and no longer makes `ls`, `find` and `status` refuse the artifact. + A node that depends on its own descendant is now reported as a cycle. - Core reads: `logic/experiments.md`, `logic/solution/heuristics.md` and `logic/problem.md` that open with a stray, unclosed `---` line before a level-one title and typed entries (`## E01: …`, `## H01: …`, `### O1: …`) diff --git a/Cargo.lock b/Cargo.lock index 0395d13..8a7281e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -112,7 +112,7 @@ checksum = "2a4385e2e34eb35d6b3efe798b9eb88096925d87726c0798709bf56d9ed84af3" [[package]] name = "ara-cli" -version = "0.1.32" +version = "0.1.33" dependencies = [ "ara-core", "arc-swap", @@ -138,7 +138,7 @@ dependencies = [ [[package]] name = "ara-core" -version = "0.1.32" +version = "0.1.33" dependencies = [ "base64", "dagre-dgl-rs", @@ -154,7 +154,7 @@ dependencies = [ [[package]] name = "ara-viewer" -version = "0.1.32" +version = "0.1.33" dependencies = [ "ara-core", "console_error_panic_hook", @@ -172,7 +172,7 @@ dependencies = [ [[package]] name = "ara-wasm" -version = "0.1.32" +version = "0.1.33" [[package]] name = "arc-swap" diff --git a/Cargo.toml b/Cargo.toml index 6f9e699..47967e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ resolver = "3" members = ["crates/*"] [workspace.package] -version = "0.1.32" +version = "0.1.33" edition = "2024" license = "MPL-2.0" repository = "https://github.com/ARA-Labs/ara-cli" diff --git a/crates/ara-core/src/parse.rs b/crates/ara-core/src/parse.rs index 0cc3c6b..1bc786f 100644 --- a/crates/ara-core/src/parse.rs +++ b/crates/ara-core/src/parse.rs @@ -229,12 +229,10 @@ pub(crate) fn parse_sources_detailed_with_claim_redirects( // dedupe links. // // A dependency whose target is an *ancestor* of the source (reachable by - // walking `children:` nesting upward) merely restates the nesting: it is - // redundant, and combined with the parent→child edge it would close a cycle - // and fail the whole artifact. Real traces do this (a child re-declaring a - // dependency on its parent), so drop the redundant edge with a warning - // instead. Genuine cross-cycles — a dependency on a sibling or descendant - // that closes a loop — are not ancestors and stay fatal in `detect_cycles`. + // walking `children:` nesting upward) merely restates the nesting. Real + // traces do this (a child re-declaring a dependency on its parent), so drop + // the redundant edge with a warning. Genuine cycles — a dependency loop, or + // a dependency on one's own descendant — stay fatal in `detect_cycles`. let parent_of: BTreeMap = norm .child_links .iter() @@ -1200,12 +1198,21 @@ fn dedupe_links(links: Vec, report: &mut ParseReport) -> Vec { /// Reports a cycle error for every back-edge in the combined /// `Child` + `DependsOn` graph (DFS three-color). +/// +/// A `Child` link points downstream (parent to child) while a `DependsOn` link +/// points upstream (dependent to dependency), so dependency edges are reversed +/// here. Mixing the two directions would report a cross-branch diamond as a +/// cycle and miss a node that depends on its own descendant. fn detect_cycles(nodes: &[Node], links: &[Link], report: &mut ParseReport) { // BTreeMap (not HashMap) keeps traversal — and thus error ordering — free of // any hash-seed influence, matching the crate's determinism guarantee. let mut adj: BTreeMap<&NodeId, Vec<&NodeId>> = BTreeMap::new(); for link in links { - adj.entry(&link.from).or_default().push(&link.to); + let (upstream, downstream) = match link.kind { + LinkKind::DependsOn => (&link.to, &link.from), + _ => (&link.from, &link.to), + }; + adj.entry(upstream).or_default().push(downstream); } let mut color: BTreeMap<&NodeId, u8> = BTreeMap::new(); // 0=white, 1=gray, 2=black let mut stack = Vec::new(); @@ -1609,6 +1616,48 @@ tree: assert!(err.errors().iter().any(|d| d.message.contains("cycle"))); } + #[test] + fn dependency_diamond_across_branches_is_not_a_cycle() { + // N01 -> N02 -> N03 by nesting, and N01 -> N04 -> N03 by dependencies: + // N04 depends on N01 and N03 depends on N04. Every edge points + // downstream, so this is a diamond, not a loop. + let yaml = "\ +tree: + - id: N01 + type: question + children: + - id: N02 + type: experiment + children: + - id: N03 + type: decision + also_depends_on: [N04] + - id: N04 + type: experiment + also_depends_on: [N01] +"; + let (m, report) = parse_sources(yaml, None).expect("a diamond is not a cycle"); + assert!(report.is_ok(), "must not error: {report}"); + assert_eq!(m.nodes.len(), 4); + } + + #[test] + fn dependency_on_own_descendant_is_a_cycle() { + // N01 nests N02 and also depends on it: N01 is upstream and downstream + // of N02 at once. + let yaml = "\ +tree: + - id: N01 + type: question + also_depends_on: [N02] + children: + - id: N02 + type: experiment +"; + let err = parse_sources(yaml, None).unwrap_err(); + assert!(err.errors().iter().any(|d| d.message.contains("cycle"))); + } + #[test] fn redundant_ancestor_depends_on_is_dropped_with_warning() { // A child that re-declares `also_depends_on` on its own parent restates diff --git a/docs/dependency-cycle-direction.md b/docs/dependency-cycle-direction.md new file mode 100644 index 0000000..8697ca3 --- /dev/null +++ b/docs/dependency-cycle-direction.md @@ -0,0 +1,26 @@ +# Dependency cycle check mixes edge directions + +**Date:** 2026-10-15. Implemented in 0.1.33. + +## Problem + +`detect_cycles` (`crates/ara-core/src/parse.rs`) runs one DFS over `Child` and `DependsOn` links. A `Child` link points from parent to child, which is downstream. A `DependsOn` link points from the dependent node to its dependency, which is upstream. Mixing the two directions gives wrong answers both ways: + +- **False positive.** A diamond is reported as a cycle. If X nests A, A nests C, B depends on X and C depends on B, the mixed graph has the loop X → A → C → B → X, although every edge runs downstream. `ara check` fails with ARA110, and `ls`, `find` and `status` refuse the artifact (`invalid_artifact`). +- **False negative.** A node that depends on its own descendant is a real cycle, but its `Child` and `DependsOn` links point the same way, so it is not reported. + +The ancestor-dependency warning (ARA208) was added for one false-positive shape, a child depending on its own ancestor. It does not cover dependencies that reach an ancestor through another branch. + +## Evidence + +The ara-eval large-artifact survey found ARA110 on 54 of 127 internal trees, and on the current `main` of Lara-dev and sierra. In every one of the 56, the graph has no cycle once edges are oriented consistently. A local build with the fix below made 24 of them readable; the rest are blocked by unrelated ARA217 diagnostics. + +## Design + +In `detect_cycles` only, orient `DependsOn` links from dependency to dependent, the same direction as `Child` links. Link data, layout and viewer edges are unchanged. The ARA208 warning and its edge drop stay as they are, because the edge is still redundant. + +## Verification + +- `dependency_diamond_across_branches_is_not_a_cycle` and `dependency_on_own_descendant_is_a_cycle` in `parse.rs` failed before the change and pass after it. The existing cycle tests (`cycle_is_detected`, `sibling_only_depends_on_cycle_is_detected`, the ARA208 ancestor test) are unchanged and pass. +- The full workspace test suite, `cargo fmt --check` and `cargo clippy -D warnings` pass. +- On the 58 internal trees that reported ARA110 under 0.1.32, the 0.1.33 build reports none.