-
Notifications
You must be signed in to change notification settings - Fork 0
feat(dag): YAML/JSON serde round-trip serialization (F4) #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KooshaPari
wants to merge
1
commit into
main
Choose a base branch
from
epic/F4-dag-serialization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,5 +2,6 @@ | |
| resolver = "3" | ||
| members = [ | ||
| "crates/byteport-transport", | ||
| "crates/byteport-dag", | ||
| "frontend/web/src-tauri", | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [package] | ||
| name = "byteport-dag" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| license = "Apache-2.0" | ||
| description = "DAG foundation: topological sort and parallel-bucket scheduler for Phenotype compute/infra" | ||
| publish = false | ||
|
|
||
| [dependencies] | ||
| serde = { version = "1", features = ["derive"] } | ||
| serde_json = "1" | ||
| serde_yaml = "0.9" | ||
| thiserror = "2.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| //! Core DAG data structure. | ||
| //! | ||
| //! A generic directed acyclic graph backed by an adjacency list. | ||
|
|
||
| use std::collections::{HashMap, HashSet}; | ||
| use std::hash::Hash; | ||
|
|
||
| /// Error types for DAG operations. | ||
| #[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] | ||
| pub enum DagError { | ||
| #[error("node `{0:?}` already exists")] | ||
| NodeAlreadyExists(String), | ||
|
|
||
| #[error("node `{0:?}` not found")] | ||
| NodeNotFound(String), | ||
|
|
||
| #[error("edge from `{0:?}` to `{1:?}` would create a cycle")] | ||
| CycleDetected(String, String), | ||
| } | ||
|
|
||
| /// A generic directed acyclic graph over node identifiers of type `K`. | ||
| #[derive(Debug, Clone)] | ||
| pub struct Dag<K> { | ||
| /// All known nodes. | ||
| nodes: HashSet<K>, | ||
| /// Adjacency list: parent -> children (forward edges). | ||
| children: HashMap<K, Vec<K>>, | ||
| /// Reverse adjacency list: child -> parents (back edges). | ||
| parents: HashMap<K, Vec<K>>, | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Construction | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| impl<K> Default for Dag<K> { | ||
| fn default() -> Self { | ||
| Self { | ||
| nodes: HashSet::new(), | ||
| children: HashMap::new(), | ||
| parents: HashMap::new(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<K> Dag<K> | ||
| where | ||
| K: Eq + Hash + Clone + std::fmt::Debug, | ||
| { | ||
| /// Create an empty DAG. | ||
| pub fn new() -> Self { | ||
| Self::default() | ||
| } | ||
|
|
||
| /// Insert a new node. Returns an error if the node already exists. | ||
| pub fn add_node(&mut self, node: K) -> Result<(), DagError> { | ||
| if self.nodes.contains(&node) { | ||
| return Err(DagError::NodeAlreadyExists(format!("{:?}", node))); | ||
| } | ||
| self.nodes.insert(node.clone()); | ||
| self.children.entry(node.clone()).or_default(); | ||
| self.parents.entry(node).or_default(); | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Insert a directed edge `from -> to`. | ||
| /// | ||
| /// Both endpoints must already exist. This method does **not** check | ||
| /// for cycles — call [`check_cycle`] or [`topological_sort`] separately. | ||
| pub fn add_edge(&mut self, from: K, to: K) -> Result<(), DagError> { | ||
| if !self.nodes.contains(&from) { | ||
| return Err(DagError::NodeNotFound(format!("{:?}", from))); | ||
| } | ||
| if !self.nodes.contains(&to) { | ||
| return Err(DagError::NodeNotFound(format!("{:?}", to))); | ||
| } | ||
| self.children.entry(from.clone()).or_default().push(to.clone()); | ||
| self.parents.entry(to).or_default().push(from); | ||
| Ok(()) | ||
| } | ||
|
|
||
| // ----------------------------------------------------------------------- | ||
| // Accessors | ||
| // ----------------------------------------------------------------------- | ||
|
|
||
| /// Return the set of all node identifiers. | ||
| pub fn nodes(&self) -> &HashSet<K> { | ||
| &self.nodes | ||
| } | ||
|
|
||
| /// Return the number of nodes. | ||
| pub fn node_count(&self) -> usize { | ||
| self.nodes.len() | ||
| } | ||
|
|
||
| /// Return the number of edges. | ||
| pub fn edge_count(&self) -> usize { | ||
| self.children.values().map(|v| v.len()).sum() | ||
| } | ||
|
|
||
| /// Return the children (direct successors) of `node`. | ||
| pub fn children_of(&self, node: &K) -> Option<&[K]> { | ||
| self.children.get(node).map(|v| v.as_slice()) | ||
| } | ||
|
|
||
| /// Return the parents (direct predecessors) of `node`. | ||
| pub fn parents_of(&self, node: &K) -> Option<&[K]> { | ||
| self.parents.get(node).map(|v| v.as_slice()) | ||
| } | ||
|
|
||
| /// Return `true` if the graph contains `node`. | ||
| pub fn contains(&self, node: &K) -> bool { | ||
| self.nodes.contains(node) | ||
| } | ||
|
|
||
| /// Iterate over all nodes. | ||
| pub fn iter_nodes(&self) -> impl Iterator<Item = &K> { | ||
| self.nodes.iter() | ||
| } | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Tests | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn empty_dag() { | ||
| let dag: Dag<u32> = Dag::new(); | ||
| assert_eq!(dag.node_count(), 0); | ||
| assert_eq!(dag.edge_count(), 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn add_and_query_nodes() { | ||
| let mut dag = Dag::new(); | ||
| dag.add_node(1).unwrap(); | ||
| dag.add_node(2).unwrap(); | ||
| assert_eq!(dag.node_count(), 2); | ||
| assert!(dag.contains(&1)); | ||
| assert!(dag.contains(&2)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn duplicate_node_error() { | ||
| let mut dag = Dag::new(); | ||
| dag.add_node("a").unwrap(); | ||
| assert!(dag.add_node("a").is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn add_edge_missing_source() { | ||
| let mut dag = Dag::<&str>::new(); | ||
| dag.add_node("b").unwrap(); | ||
| assert!(dag.add_edge("a", "b").is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn add_edge_missing_target() { | ||
| let mut dag = Dag::<&str>::new(); | ||
| dag.add_node("a").unwrap(); | ||
| assert!(dag.add_edge("a", "b").is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn simple_edge() { | ||
| let mut dag = Dag::new(); | ||
| dag.add_node("a").unwrap(); | ||
| dag.add_node("b").unwrap(); | ||
| dag.add_edge("a", "b").unwrap(); | ||
| assert_eq!(dag.edge_count(), 1); | ||
| assert_eq!(dag.children_of(&"a"), Some(&["b"][..])); | ||
| assert_eq!(dag.parents_of(&"b"), Some(&["a"][..])); | ||
| } | ||
|
|
||
| #[test] | ||
| fn diamond_graph() { | ||
| let mut dag = Dag::new(); | ||
| for n in ["a", "b", "c", "d"] { | ||
| dag.add_node(n).unwrap(); | ||
| } | ||
| dag.add_edge("a", "b").unwrap(); | ||
| dag.add_edge("a", "c").unwrap(); | ||
| dag.add_edge("b", "d").unwrap(); | ||
| dag.add_edge("c", "d").unwrap(); | ||
| assert_eq!(dag.node_count(), 4); | ||
| assert_eq!(dag.edge_count(), 4); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| //! # byteport-dag | ||
| //! | ||
| //! DAG foundation for Phenotype compute/infra automation (epic F). | ||
| //! | ||
| //! ## Modules | ||
| //! | ||
| //! | Module | Description | | ||
| //! |---------------|-----------------------------------------------------| | ||
| //! | [`dag`] | Generic directed-acyclic-graph data structure | | ||
| //! | [`topo`] | Topological sort (Kahn's algorithm + DFS variant) | | ||
| //! | [`scheduler`] | Parallel-bucket scheduler built on topological order | | ||
| //! | [`serialize`] | YAML/JSON round-trip serialization (DagSchema) | | ||
| //! | ||
| //! ## Example | ||
| //! | ||
| //! ```rust | ||
| //! use byteport_dag::dag::Dag; | ||
| //! use byteport_dag::scheduler; | ||
| //! | ||
| //! let mut dag = Dag::new(); | ||
| //! dag.add_node("build").unwrap(); | ||
| //! dag.add_node("test").unwrap(); | ||
| //! dag.add_node("deploy").unwrap(); | ||
| //! dag.add_edge("build", "test").unwrap(); | ||
| //! dag.add_edge("test", "deploy").unwrap(); | ||
| //! | ||
| //! let sched = scheduler::schedule(&dag).unwrap(); | ||
| //! assert_eq!(sched.buckets.len(), 3); | ||
| //! ``` | ||
|
|
||
| pub mod dag; | ||
| pub mod topo; | ||
| pub mod scheduler; | ||
| pub mod serialize; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Duplicate edges are inserted without checks, which duplicates entries in
children/parentsand inflatesedge_count. This also breaks serialization round-trip fidelity becauseDagSchema::from_dagdeduplicates edges viaBTreeSet, so repeated edges are silently lost; reject duplicates on insert (or switch adjacency storage to sets). [logic error]Severity Level: Major⚠️
Steps of Reproduction ✅
(Use Cmd/Ctrl + Click for best experience)
Prompt for AI Agent 🤖