use std::collections::{BTreeMap, BTreeSet};
use anyhow::Result;
use perspt_sdk::canon::CanonicalEncoder;
use super::{Psp9AgentRuntime, Psp9Recorder};
use crate::candidate::{CandidateWorkspace, CodingCandidateMeasurer};
use crate::toolloop::{CandidateMeasurer, EffectExecutor, SeedFile};
pub(super) struct StagedWinner {
pub state_root: String,
pub files: Vec<SeedFile>,
}
#[derive(Default)]
pub(super) struct StagingRoot {
pub contributions: BTreeMap<String, StagedWinner>,
}
impl StagingRoot {
pub fn is_empty(&self) -> bool {
self.contributions.is_empty()
}
pub fn digest(&self) -> String {
let mut encoder = CanonicalEncoder::new(b"perspt-staging-v1");
for (node_id, winner) in &self.contributions {
encoder.text(node_id).text(&winner.state_root);
for file in &winner.files {
encoder.text(&file.path).field(
file.content
.as_deref()
.map(perspt_sdk::ledger::content_hash)
.unwrap_or_default()
.as_bytes(),
);
}
}
encoder.digest()
}
pub fn conflict(&self, graph: &perspt_sdk::WorkGraphRevision) -> Option<String> {
let ancestors: BTreeMap<&str, BTreeSet<String>> = self
.contributions
.keys()
.map(|node_id| (node_id.as_str(), transitive_predecessors(graph, node_id)))
.collect();
let comparable = |a: &str, b: &str| {
ancestors.get(a).is_some_and(|set| set.contains(b))
|| ancestors.get(b).is_some_and(|set| set.contains(a))
};
type Writers<'a> = BTreeMap<&'a str, Vec<(&'a str, Option<&'a [u8]>)>>;
let mut writers: Writers = BTreeMap::new();
for (node_id, winner) in &self.contributions {
for file in &winner.files {
writers
.entry(file.path.as_str())
.or_default()
.push((node_id, file.content.as_deref()));
}
}
for (path, entries) in &writers {
for (index, (first, first_content)) in entries.iter().enumerate() {
for (second, second_content) in &entries[index + 1..] {
if first_content != second_content && !comparable(first, second) {
return Some(format!(
"path {path} written divergently by {first} and {second}"
));
}
}
}
}
None
}
pub fn merged_files_for(
&self,
graph: &perspt_sdk::WorkGraphRevision,
contributors: &BTreeSet<String>,
) -> Vec<SeedFile> {
let topo: BTreeMap<&str, usize> = graph
.validation
.topo_order
.iter()
.enumerate()
.map(|(index, node_id)| (node_id.as_str(), index))
.collect();
let mut ordered: Vec<&String> = self
.contributions
.keys()
.filter(|node_id| contributors.contains(*node_id))
.collect();
ordered.sort_by_key(|node_id| topo.get(node_id.as_str()).copied().unwrap_or(usize::MAX));
let mut merged: BTreeMap<String, SeedFile> = BTreeMap::new();
for node_id in ordered {
for file in &self.contributions[node_id].files {
merged.insert(file.path.clone(), file.clone());
}
}
merged.into_values().collect()
}
pub fn merged_files(&self, graph: &perspt_sdk::WorkGraphRevision) -> Vec<SeedFile> {
self.merged_files_for(graph, &self.contributions.keys().cloned().collect())
}
}
pub(super) fn fold_staging(
recorder: &Psp9Recorder,
session_id: &str,
graph: &perspt_sdk::WorkGraphRevision,
) -> Result<StagingRoot> {
use anyhow::Context;
let mut staging = StagingRoot::default();
let mut pre_generation: BTreeSet<String> = BTreeSet::new();
for row in recorder.store.get_psp9_events(session_id)? {
let Ok(perspt_sdk::LedgerEvent::Custom { kind, payload }) =
serde_json::from_str::<perspt_sdk::LedgerEvent>(&row.event_json)
else {
continue;
};
if kind != "staging_root_updated" {
continue;
}
let node_id = payload
.get("node_id")
.and_then(|value| value.as_str())
.context("staged contribution names no node")?;
let Some(node) = graph.node(node_id) else {
continue;
};
let generation = match payload.get("generation").and_then(|value| value.as_u64()) {
Some(generation) => {
pre_generation.remove(node_id);
generation
}
None => {
pre_generation.insert(node_id.to_string());
staging.contributions.remove(node_id);
continue;
}
};
if generation != u64::from(node.generation) {
continue;
}
let files_value = payload.get("files").with_context(|| {
format!(
"staged contribution for {node_id} predates durable staging; \
the staging root cannot be reconstructed"
)
})?;
let handles: Vec<crate::toolloop::DurableSeedFile> =
serde_json::from_value(files_value.clone())?;
let state_root = payload
.get("state_root")
.and_then(|value| value.as_str())
.context("staged contribution carries no state root")?
.to_string();
staging.contributions.insert(
node_id.to_string(),
StagedWinner {
state_root,
files: super::node::load_seed_files(recorder, handles)?,
},
);
}
anyhow::ensure!(
pre_generation.is_empty(),
"staged contributions for {pre_generation:?} predate generation-bound staging; \
the staging root cannot be reconstructed safely"
);
Ok(staging)
}
pub(super) fn transitive_predecessors(
graph: &perspt_sdk::WorkGraphRevision,
node_id: &str,
) -> BTreeSet<String> {
let mut wanted = BTreeSet::new();
let mut frontier = vec![node_id.to_string()];
while let Some(current) = frontier.pop() {
for dependency in graph.dependencies_of(¤t) {
if wanted.insert(dependency.to_string()) {
frontier.push(dependency.to_string());
}
}
}
wanted
}
impl Psp9AgentRuntime {
pub(super) async fn run_integration_gate(
&self,
recorder: &Psp9Recorder,
session_id: &str,
graph: &perspt_sdk::WorkGraphRevision,
staging: &StagingRoot,
) -> Result<Option<Vec<String>>> {
let staging_digest = staging.digest();
if let Some(conflict) = staging.conflict(graph) {
recorder.record_custom(
"integration_failed",
serde_json::json!({
"staging_root": staging_digest,
"reason": format!("merge conflict: {conflict}"),
}),
)?;
return Ok(None);
}
let exclusions = self.candidate_exclusions();
let mut workspace = CandidateWorkspace::create_filtered(
&self.working_dir,
"integration",
0,
&graph.revision_id,
self.config.allow_unisolated_verifiers,
&exclusions,
)?;
workspace.set_tool_handlers(self.tool_handlers.clone());
workspace.set_verifier_timeouts(self.config.verifier_timeouts);
workspace.restore_exported(&staging.merged_files(graph))?;
let measured = CodingCandidateMeasurer::new(&workspace, "integration", 0)
.with_domain(self.domain.clone())
.with_max_parallel(self.config.max_parallel_verifiers)
.with_require_format(self.config.require_format)
.with_test_policy(self.config.test_policy, self.config.external_oracle.clone())
.measure()
.await?;
recorder.record_custom(
"integration_measured",
serde_json::json!({
"staging_root": staging_digest,
"energy": measured.energy,
"hard_pass": measured.hard_pass,
"residuals": measured.residuals.len(),
}),
)?;
if !measured.hard_pass {
recorder.record_custom(
"integration_failed",
serde_json::json!({
"staging_root": staging_digest,
"reason": "combined state failed the global verifier gate",
}),
)?;
return Ok(None);
}
self.promote_integration_root(recorder, session_id, graph, &workspace, &staging_digest)
.await
}
async fn promote_integration_root(
&self,
recorder: &Psp9Recorder,
session_id: &str,
graph: &perspt_sdk::WorkGraphRevision,
workspace: &CandidateWorkspace,
staging_digest: &str,
) -> Result<Option<Vec<String>>> {
let assembly = {
let external_entries = self.external_tool_entries(recorder).await?;
self.assemble_node(
recorder,
session_id,
graph,
"integration",
0,
&external_entries,
)?
};
let kernel_state =
super::node::loop_kernel_state(&assembly.grant_policy, &graph.revision_id);
let promoted = self
.promote_certified(
recorder,
workspace,
"integration",
&assembly.grant_policy,
&assembly.capability,
&assembly.contract,
&assembly.barrier,
&kernel_state,
&assembly.calibration,
)
.await?;
match promoted {
Some(paths) => {
recorder.record_custom(
"integration_promoted",
serde_json::json!({
"staging_root": staging_digest,
"paths": paths,
}),
)?;
Ok(Some(paths))
}
None => {
recorder.record_custom(
"integration_failed",
serde_json::json!({
"staging_root": staging_digest,
"reason": "promotion was not certified; prior root restored",
}),
)?;
Ok(None)
}
}
}
pub(super) async fn staging_seed(
&self,
graph: &perspt_sdk::WorkGraphRevision,
staging: &StagingRoot,
node_id: &str,
) -> Result<Option<super::node::CandidateSeed>> {
let predecessors = transitive_predecessors(graph, node_id);
let contributors: BTreeSet<String> = staging
.contributions
.keys()
.filter(|contributor| predecessors.contains(*contributor))
.cloned()
.collect();
if contributors.is_empty() {
return Ok(None);
}
let exclusions = self.candidate_exclusions();
let scratch = CandidateWorkspace::create_filtered(
&self.working_dir,
"staging-seed",
0,
&graph.revision_id,
self.config.allow_unisolated_verifiers,
&exclusions,
)?;
let files = staging.merged_files_for(graph, &contributors);
scratch.restore_seeded(&files)?;
let checkpoint = scratch.checkpoint(&[]).await?;
Ok(Some(super::node::CandidateSeed {
expected_state_root: checkpoint.witness.state_root,
canonical_scope: checkpoint.witness.canonical_scope.clone(),
files,
conversation: perspt_sdk::Conversation::default(),
birth_deps: Vec::new(),
activated_tools: Vec::new(),
inherited: true,
}))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::toolloop::SeedFile;
fn graph_a_to_b_with_sibling_z() -> perspt_sdk::WorkGraphRevision {
let node = |id: &str| {
perspt_sdk::WorkNode::new(id, format!("goal {id}"), perspt_sdk::NodeClass::Implement)
};
perspt_sdk::WorkGraphRevision::build(
1,
None,
perspt_sdk::GraphRevisionReason::InitialPlan,
vec![node("a"), node("b"), node("z")],
vec![perspt_sdk::WorkEdge::new(
"a",
"b",
perspt_sdk::EdgeKind::RequiresArtifact,
)],
)
.unwrap()
}
fn winner(content: &[u8]) -> StagedWinner {
StagedWinner {
state_root: format!("root-{}", perspt_sdk::ledger::content_hash(content)),
files: vec![SeedFile {
path: "src/shared.rs".into(),
content: Some(content.to_vec()),
source_preimage: None,
}],
}
}
#[test]
fn a_superseded_upstream_write_still_conflicts_with_a_divergent_sibling() {
let graph = graph_a_to_b_with_sibling_z();
let mut staging = StagingRoot::default();
staging.contributions.insert("a".into(), winner(b"X"));
staging.contributions.insert("b".into(), winner(b"Y"));
staging.contributions.insert("z".into(), winner(b"Y"));
let conflict = staging.conflict(&graph).expect("a and z diverge");
assert!(
conflict.contains("a") && conflict.contains("z"),
"the incomparable divergent pair is named: {conflict}"
);
}
#[test]
fn comparable_refinement_and_agreeing_siblings_do_not_conflict() {
let graph = graph_a_to_b_with_sibling_z();
let mut staging = StagingRoot::default();
staging.contributions.insert("a".into(), winner(b"X"));
staging.contributions.insert("b".into(), winner(b"Y"));
assert!(staging.conflict(&graph).is_none(), "a → b is a refinement");
staging.contributions.insert("z".into(), winner(b"X"));
let conflict = staging.conflict(&graph).expect("b and z diverge");
assert!(conflict.contains("b") && conflict.contains("z"));
}
fn staging_row(generation: Option<u32>) -> serde_json::Value {
let mut payload = serde_json::json!({
"node_id": "a",
"state_root": "root-a",
"files": [],
});
if let Some(generation) = generation {
payload["generation"] = generation.into();
}
payload
}
#[test]
fn a_superseded_pre_generation_staging_row_folds_by_the_newer_row() {
let database = tempfile::tempdir().unwrap();
let recorder = Psp9Recorder::create(
"fold-staging-test",
"task",
std::path::Path::new("."),
Some(&database.path().join("fold-staging.db")),
None,
None,
)
.unwrap();
let graph = graph_a_to_b_with_sibling_z();
recorder
.record_custom("staging_root_updated", staging_row(None))
.unwrap();
let refusal = match fold_staging(&recorder, "fold-staging-test", &graph) {
Err(error) => error.to_string(),
Ok(_) => panic!("an unsuperseded pre-generation row must refuse the fold"),
};
assert!(refusal.contains("generation-bound"), "{refusal}");
recorder
.record_custom("staging_root_updated", staging_row(Some(0)))
.unwrap();
let staging = fold_staging(&recorder, "fold-staging-test", &graph).unwrap();
assert_eq!(
staging
.contributions
.get("a")
.map(|w| w.state_root.as_str()),
Some("root-a"),
"the newer generation-bound row supersedes the legacy one"
);
}
}