use std::num::NonZeroU64;
use ag_protocol::ProtocolRequestProfile;
use crate::execution_graph::test_support::{graph, reader, result};
use crate::{
EnvId, ExecutionGraph, GraphCompletionGate, GraphContext, GraphContractError, GraphDependency,
GraphExecutionRef, GraphInputBinding, GraphNode, GraphPostProcessing, GraphResult,
GraphResultState, GraphWorkspaceAccess, TaskWorktreeId,
};
type InvalidGraphCase = (fn(&mut ExecutionGraph), GraphContractError);
#[test]
fn graphs_reject_empty_and_conflicting_identities() {
type Mutation = fn(&mut ExecutionGraph);
let cases: &[(Mutation, GraphContractError)] = &[
(
|graph| graph.id.clear(),
GraphContractError::EmptyField("graph_id"),
),
(
|graph| graph.session_id = " ".into(),
GraphContractError::EmptyField("session_id"),
),
(|graph| graph.nodes.clear(), GraphContractError::EmptyGraph),
(
|graph| graph.nodes[0].id.clear(),
GraphContractError::EmptyField("node_id"),
),
(
|graph| graph.nodes[0].definition.id.clear(),
GraphContractError::EmptyField("definition.id"),
),
(
|graph| graph.nodes[0].definition.prompt.clear(),
GraphContractError::EmptyField("definition.prompt"),
),
(
|graph| graph.nodes[0].objective.clear(),
GraphContractError::EmptyField("objective"),
),
(
|graph| graph.nodes[0].request.text.clear(),
GraphContractError::EmptyField("request"),
),
(
|graph| graph.nodes.push(graph.nodes[0].clone()),
duplicate("node", "first"),
),
(
|graph| graph.nodes[0].env_id = EnvId::SessionWorktree("other".into()),
GraphContractError::ForeignEnv,
),
(
|graph| {
graph.nodes[0].env_id = EnvId::TaskWorktree(TaskWorktreeId {
session_id: "other".into(),
worktree_id: "writer".into(),
});
},
GraphContractError::ForeignEnv,
),
(
|graph| {
graph.nodes[0].env_id = EnvId::TaskWorktree(TaskWorktreeId {
session_id: graph.session_id.clone(),
worktree_id: "writer".into(),
});
},
GraphContractError::UnsupportedEnv,
),
(
|graph| {
task_node(&mut graph.nodes[0], "same-task");
task_node(&mut graph.nodes[1], "same-task");
},
duplicate("task", "same-task"),
),
];
assert_invalid_graphs(cases);
}
#[test]
fn graphs_reject_invalid_output_and_input_declarations() {
type Mutation = fn(&mut ExecutionGraph);
let cases: &[(Mutation, GraphContractError)] = &[
(
|graph| graph.nodes[0].outputs[0].name.clear(),
GraphContractError::EmptyField("output_name"),
),
(
|graph| {
let output = graph.nodes[0].outputs[0].clone();
graph.nodes[0].outputs.push(output);
},
duplicate("output", "response"),
),
(
|graph| graph.nodes[0].outputs[0].schema = ProtocolRequestProfile::UtilityPrompt,
GraphContractError::InvalidProfile(
"output schema must match the producing response profile",
),
),
(
|graph| graph.nodes[1].inputs[0].name.clear(),
GraphContractError::EmptyField("input_name"),
),
(
|graph| graph.nodes[1].inputs[0].producer_node_id.clear(),
GraphContractError::EmptyField("producer_node_id"),
),
(
|graph| graph.nodes[1].inputs[0].output_name.clear(),
GraphContractError::EmptyField("output_name"),
),
(
|graph| {
let input = graph.nodes[1].inputs[0].clone();
graph.nodes[1].inputs.push(input);
},
duplicate("input", "evidence"),
),
];
assert_invalid_graphs(cases);
}
#[test]
fn graphs_reject_missing_duplicate_and_ungated_dependencies() {
type Mutation = fn(&mut ExecutionGraph);
let cases: &[(Mutation, GraphContractError)] = &[
(
|graph| graph.nodes[1].dependencies[0].node_id = "missing".into(),
GraphContractError::UnknownNode("missing".into()),
),
(
|graph| {
let dependency = graph.nodes[1].dependencies[0].clone();
graph.nodes[1].dependencies.push(dependency);
},
duplicate("dependency", "first"),
),
(
|graph| {
graph.nodes[1].dependencies[0].gate = GraphCompletionGate::ResultAccepted {
output_name: "missing".into(),
}
},
GraphContractError::UnknownOutput {
node: "first".into(),
output: "missing".into(),
},
),
(
|graph| graph.nodes[1].inputs[0].producer_node_id = "missing".into(),
GraphContractError::UnknownNode("missing".into()),
),
(
|graph| graph.nodes[1].inputs[0].output_name = "missing".into(),
GraphContractError::UnknownOutput {
node: "first".into(),
output: "missing".into(),
},
),
(
|graph| graph.nodes[1].dependencies.clear(),
GraphContractError::UngatedInput("evidence".into()),
),
(
|graph| graph.nodes[1].dependencies[0].gate = GraphCompletionGate::ExecutionSucceeded,
GraphContractError::UngatedInput("evidence".into()),
),
];
assert_invalid_graphs(cases);
}
#[test]
fn graphs_reject_invalid_execution_requirements() {
type Mutation = fn(&mut ExecutionGraph);
let cases: &[(Mutation, GraphContractError)] = &[
(
|graph| {
task_node(&mut graph.nodes[0], "task");
graph.nodes[0].execution.workspace_access = GraphWorkspaceAccess::ExclusiveWrite;
graph.nodes[0].execution.post_processing = GraphPostProcessing::SessionWorkflow;
},
GraphContractError::InvalidProfile(
"read-only definitions cannot perform host mutation effects",
),
),
(
|graph| graph.nodes[0].execution.shared_budget_id = Some(String::new()),
GraphContractError::EmptyField("shared_budget_id"),
),
];
assert_invalid_graphs(cases);
}
fn assert_invalid_graphs(cases: &[InvalidGraphCase]) {
for (mutate, expected) in cases {
let mut graph = graph();
mutate(&mut graph);
let validation = graph.validate();
assert_eq!(validation, Err(expected.clone()), "{graph:?}");
}
}
#[test]
fn graphs_reject_self_dependencies_and_cycles_even_in_disconnected_components() {
for predecessor in ["second", "first"] {
let mut graph = graph();
graph.nodes.push(reader("disconnected-root"));
graph.nodes[0].dependencies.push(GraphDependency {
gate: GraphCompletionGate::ExecutionSucceeded,
node_id: predecessor.into(),
});
let validation = graph.validate();
assert_eq!(validation, Err(GraphContractError::Cycle));
}
}
#[test]
fn graphs_allow_multiple_gates_and_join_producers_in_any_declaration_order() {
let mut graph = graph();
graph.nodes[1].dependencies.extend([
GraphDependency {
gate: GraphCompletionGate::ExecutionSucceeded,
node_id: "first".into(),
},
GraphDependency {
gate: GraphCompletionGate::ResultAccepted {
output_name: "response".into(),
},
node_id: "first".into(),
},
GraphDependency {
gate: GraphCompletionGate::ExecutionSucceeded,
node_id: "third".into(),
},
]);
graph.nodes.push(reader("third"));
graph.nodes.reverse();
let original = graph.clone();
let validation = graph.validate();
assert_eq!(validation, Ok(()));
assert_eq!(graph, original);
}
#[test]
fn graphs_validate_deep_dependency_chains_without_recursion() {
let mut graph = graph();
graph.nodes = (0..4096)
.map(|index| {
let mut node = reader(&format!("node-{index}"));
if index > 0 {
node.dependencies.push(GraphDependency {
gate: GraphCompletionGate::ExecutionSucceeded,
node_id: format!("node-{}", index - 1),
});
}
node
})
.collect();
let validation = graph.validate();
assert_eq!(validation, Ok(()));
}
#[test]
fn input_gates_require_the_state_of_the_exact_version() {
for acceptance_required in [false, true] {
for state in [
GraphResultState::Produced,
GraphResultState::Validated,
GraphResultState::Accepted,
] {
let mut graph = graph();
if acceptance_required {
graph.nodes[1].dependencies.push(GraphDependency {
gate: GraphCompletionGate::ResultAccepted {
output_name: "response".into(),
},
node_id: "first".into(),
});
}
let result = GraphResult { state, ..result() };
let binding = graph.bind_input("second", "evidence", &result);
if state == GraphResultState::Produced
|| (acceptance_required && state != GraphResultState::Accepted)
{
assert_eq!(binding, Err(GraphContractError::UnsatisfiedGate));
} else {
let binding = binding.expect("gate satisfied");
assert_eq!(binding.name, "evidence");
assert_eq!(binding.result, result.reference);
}
}
}
}
#[test]
fn input_resolution_rejects_foreign_stale_or_mistyped_result_provenance() {
type Mutation = fn(&mut GraphResult);
let cases: &[(Mutation, GraphContractError)] = &[
(
|result| result.reference.graph_id = "other".into(),
GraphContractError::ResultMismatch("graph identity or revision"),
),
(
|result| result.reference.graph_revision = NonZeroU64::new(2).expect("nonzero"),
GraphContractError::ResultMismatch("graph identity or revision"),
),
(
|result| result.reference.node_id = "second".into(),
GraphContractError::ResultMismatch("producer or output"),
),
(
|result| result.reference.output_name = "other".into(),
GraphContractError::ResultMismatch("producer or output"),
),
(
|result| result.reference.schema = ProtocolRequestProfile::UtilityPrompt,
GraphContractError::ResultMismatch("output schema"),
),
(
|result| {
result.reference.execution = GraphExecutionRef::TaskAttempt {
attempt_id: "attempt".into(),
operation_id: "operation".into(),
task_id: "task".into(),
}
},
GraphContractError::ResultMismatch("execution authority"),
),
(
|result| result.reference.id.clear(),
GraphContractError::EmptyField("result_id"),
),
];
for (mutate, expected) in cases {
let graph = graph();
let mut result = result();
mutate(&mut result);
let binding = graph.bind_input("second", "evidence", &result);
assert_eq!(binding, Err(expected.clone()));
}
}
#[test]
fn input_resolution_requires_a_valid_graph_and_declared_consumer_slot() {
let mut invalid = graph();
invalid.nodes.clear();
let invalid_graph = invalid.bind_input("second", "evidence", &result());
let unknown_consumer = graph().bind_input("missing", "evidence", &result());
let unknown_slot = graph().bind_input("second", "missing", &result());
assert_eq!(invalid_graph, Err(GraphContractError::EmptyGraph));
assert_eq!(
unknown_consumer,
Err(GraphContractError::UnknownNode("missing".into()))
);
assert_eq!(
unknown_slot,
Err(GraphContractError::ResultMismatch("declared input"))
);
}
#[test]
fn task_result_resolution_requires_the_assigned_task_authority() {
let mut graph = graph();
task_node(&mut graph.nodes[0], "task");
let mut result = result();
result.reference.schema = ProtocolRequestProfile::SessionTurn;
for execution in [
GraphExecutionRef::UtilityRun {
run_id: "run".into(),
},
GraphExecutionRef::TaskAttempt {
attempt_id: "attempt".into(),
operation_id: "operation".into(),
task_id: "other".into(),
},
GraphExecutionRef::TaskAttempt {
attempt_id: "attempt".into(),
operation_id: "operation".into(),
task_id: "task".into(),
},
] {
result.reference.execution = execution.clone();
let expected_valid = matches!(&execution, GraphExecutionRef::TaskAttempt { task_id, .. } if task_id == "task");
let binding = graph.bind_input("second", "evidence", &result);
if expected_valid {
assert_eq!(binding.expect("assigned task").result.execution, execution);
} else {
assert_eq!(
binding,
Err(GraphContractError::ResultMismatch("execution authority"))
);
}
}
}
fn duplicate(kind: &'static str, id: &str) -> GraphContractError {
GraphContractError::Duplicate {
kind,
id: id.into(),
}
}
fn task_node(node: &mut GraphNode, task_id: &str) {
node.execution.context = GraphContext::TaskConversation {
task_id: task_id.into(),
};
node.execution.response_profile = ProtocolRequestProfile::SessionTurn;
node.outputs[0].schema = ProtocolRequestProfile::SessionTurn;
}
#[test]
fn expansion_appends_nodes_as_the_next_revision_and_keeps_existing_nodes() {
let graph = graph();
let mut third = reader("third");
third.dependencies.push(GraphDependency {
gate: GraphCompletionGate::ExecutionSucceeded,
node_id: "second".into(),
});
let expanded = graph.expand(vec![third.clone()]).expect("expanded");
assert_eq!(expanded.revision.get(), 2);
assert_eq!(expanded.nodes[..2], graph.nodes[..]);
assert_eq!(expanded.nodes[2], third);
assert_eq!(
(expanded.id, expanded.session_id),
(graph.id, graph.session_id)
);
}
#[test]
fn expansion_rejects_empty_exhausted_reused_and_invalid_nodes() {
let graph = graph();
let mut exhausted = graph.clone();
exhausted.revision = NonZeroU64::MAX;
let mut missing = reader("third");
missing.dependencies.push(GraphDependency {
gate: GraphCompletionGate::ExecutionSucceeded,
node_id: "absent".into(),
});
let results = [
graph.expand(Vec::new()),
exhausted.expand(vec![reader("third")]),
graph.expand(vec![reader("first")]),
graph.expand(vec![missing]),
];
assert_eq!(
results.map(Result::err),
[
Some(GraphContractError::EmptyExpansion),
Some(GraphContractError::RevisionExhausted),
Some(duplicate("node", "first")),
Some(GraphContractError::UnknownNode("absent".into())),
]
);
}
#[test]
fn expanded_revisions_bind_results_produced_under_earlier_revisions() {
let mut third = reader("third");
third.dependencies.push(GraphDependency {
gate: GraphCompletionGate::ResultValidated {
output_name: "response".into(),
},
node_id: "first".into(),
});
third.inputs.push(GraphInputBinding {
name: "evidence".into(),
output_name: "response".into(),
producer_node_id: "first".into(),
});
let expanded = graph().expand(vec![third]).expect("expanded");
let result = result();
let binding = expanded.bind_input("third", "evidence", &result);
assert_eq!(binding.expect("earlier result").result, result.reference);
}