Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 5 additions & 25 deletions kernel/relayflowd-core/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ use serde::{Deserialize, Serialize};
use serde_json::Value;
use thiserror::Error;

mod dependencies;

use dependencies::validate_dependency_cycles;

/// The spec schema version this kernel reads and writes (semver, RFC §7).
pub const SPEC_VERSION: &str = "0.1.0";

Expand Down Expand Up @@ -156,11 +160,7 @@ impl RunSpec {
.iter()
.map(|step| (step.id.as_str(), step.depends_on.as_slice()))
.collect::<BTreeMap<_, _>>();
let mut visiting = BTreeSet::new();
let mut visited = BTreeSet::new();
for id in &ids {
visit(id, &dependencies, &mut visiting, &mut visited)?;
}
validate_dependency_cycles(&ids, &dependencies)?;
Ok(())
}

Expand Down Expand Up @@ -215,26 +215,6 @@ fn reject_unknown_step_fields(value: &Value) -> Result<(), SpecError> {
Ok(())
}

fn visit<'a>(
id: &'a str,
dependencies: &BTreeMap<&'a str, &'a [String]>,
visiting: &mut BTreeSet<&'a str>,
visited: &mut BTreeSet<&'a str>,
) -> Result<(), SpecError> {
if visited.contains(id) {
return Ok(());
}
if !visiting.insert(id) {
return Err(SpecError::DependencyCycle(id.to_owned()));
}
for dependency in dependencies.get(id).copied().unwrap_or_default() {
visit(dependency, dependencies, visiting, visited)?;
}
visiting.remove(id);
visited.insert(id);
Ok(())
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct StepSpec {
pub id: String,
Expand Down
54 changes: 54 additions & 0 deletions kernel/relayflowd-core/src/spec/dependencies.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::collections::{BTreeMap, BTreeSet};

use super::SpecError;

/// Reject dependency cycles without consuming call-stack depth from the spec.
pub(super) fn validate_dependency_cycles<'a>(
ids: &'a BTreeSet<String>,
dependencies: &BTreeMap<&'a str, &'a [String]>,
) -> Result<(), SpecError> {
struct Frame<'a> {
id: &'a str,
next_dependency: usize,
}

let mut visiting = BTreeSet::new();
let mut visited = BTreeSet::new();

for id in ids {
let id = id.as_str();
if visited.contains(id) {
continue;
}

visiting.insert(id);
let mut frames = vec![Frame {
id,
next_dependency: 0,
}];

while let Some(frame) = frames.last_mut() {
let step_dependencies = dependencies.get(frame.id).copied().unwrap_or_default();
let Some(dependency) = step_dependencies.get(frame.next_dependency) else {
let completed = frames.pop().expect("the active frame exists");
visiting.remove(completed.id);
visited.insert(completed.id);
continue;
};
frame.next_dependency += 1;
let dependency = dependency.as_str();

if visited.contains(dependency) {
continue;
}
if !visiting.insert(dependency) {
return Err(SpecError::DependencyCycle(dependency.to_owned()));
}
frames.push(Frame {
id: dependency,
next_dependency: 0,
});
}
}
Ok(())
}
41 changes: 40 additions & 1 deletion kernel/relayflowd-core/src/spec/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde_json::json;
use serde_json::{Value, json};

use super::*;

Expand Down Expand Up @@ -27,6 +27,45 @@ fn cycles_are_rejected() {
));
}

const DEEP_DEPENDENCY_GRAPH_LENGTH: usize = 10_000;

fn sdk_boundary_dependency_graph(cyclic: bool) -> RunSpec {
let steps = (0..DEEP_DEPENDENCY_GRAPH_LENGTH)
.map(|index| {
let mut step = json!({
"id": format!("s{index}"),
"type": "deterministic",
"command": "true",
});
if index + 1 < DEEP_DEPENDENCY_GRAPH_LENGTH {
step["depends_on"] = json!([format!("s{}", index + 1)]);
} else if cyclic {
step["depends_on"] = json!(["s0"]);
}
step
})
.collect::<Vec<Value>>();

RunSpec::parse(&json!({
"version": "0.1.0",
"steps": steps,
}))
.expect("the SDK-to-kernel boundary shape must parse")
}

#[test]
fn sdk_boundary_accepts_a_valid_10_000_step_reverse_chain() {
assert_eq!(sdk_boundary_dependency_graph(false).validate(), Ok(()));
}

#[test]
fn sdk_boundary_rejects_a_10_000_step_cycle_with_a_typed_error() {
assert_eq!(
sdk_boundary_dependency_graph(true).validate(),
Err(SpecError::DependencyCycle("s0".to_owned()))
);
}

#[test]
fn a_misspelled_verification_gate_key_is_a_parse_error_not_a_dropped_gate() {
// The fail-open refutation case: "output_contain" (typo) must never
Expand Down
Loading
Loading