use futures::stream::{FuturesUnordered, StreamExt};
use tokio_util::sync::CancellationToken;
use super::*;
pub(super) struct SharedRecoveryPool {
remaining: std::sync::Mutex<u32>,
}
impl SharedRecoveryPool {
fn new(budget: u32) -> Self {
Self {
remaining: std::sync::Mutex::new(budget),
}
}
fn claim(&self, want: u32) -> u32 {
let mut remaining = self.remaining.lock().unwrap();
let granted = (*remaining).min(want);
*remaining -= granted;
granted
}
fn refund(&self, unspent: u32) {
*self.remaining.lock().unwrap() += unspent;
}
}
struct NodeDone {
node_id: String,
generation: u32,
claimed: u32,
outcome: Option<Result<NodeAttempt>>,
}
pub(super) struct DispatchOutcome {
pub outcome: NodeTerminalOutcome,
pub status: &'static str,
pub promoted_paths: Vec<String>,
pub turns_used: u32,
}
impl Psp9AgentRuntime {
pub(super) async fn run_dispatched(
&self,
recorder: &Psp9Recorder,
session_id: &str,
graph: WorkGraphRevision,
) -> Result<DispatchOutcome> {
self.run_dispatched_with(
recorder,
session_id,
graph,
super::integrate::StagingRoot::default(),
std::collections::BTreeMap::new(),
)
.await
}
pub(super) async fn run_dispatched_with(
&self,
recorder: &Psp9Recorder,
session_id: &str,
mut graph: WorkGraphRevision,
staging: super::integrate::StagingRoot,
resume_seeds: std::collections::BTreeMap<String, super::node::CandidateSeed>,
) -> Result<DispatchOutcome> {
let scheduler = tokio::sync::Mutex::new(Scheduler::new(self.config.max_parallel_nodes));
let mut leases = perspt_sdk::LeaseTable::default();
let pool = SharedRecoveryPool::new(self.config.rejection_budget);
let mut staging = staging;
let mut running: FuturesUnordered<futures::future::BoxFuture<'_, NodeDone>> =
FuturesUnordered::new();
let mut tokens: std::collections::BTreeMap<String, CancellationToken> = Default::default();
let mut aggregate = DispatchOutcome {
outcome: NodeTerminalOutcome::HardPass,
status: "COMPLETED_PSP9",
promoted_paths: Vec::new(),
turns_used: 0,
};
loop {
graph = self
.dispatch_ready(
recorder,
session_id,
&graph,
&scheduler,
&pool,
&mut running,
&mut tokens,
&staging,
&resume_seeds,
)
.await?;
let Some(done) = running.next().await else {
break;
};
let done: NodeDone = done;
scheduler
.lock()
.await
.finish(&done.node_id, done.generation);
tokens.remove(&done.node_id);
let node_id = done.node_id.clone();
let Some(attempt) = settle_done(recorder, &pool, done)? else {
continue;
};
graph = self
.fold_completion(
recorder,
session_id,
&scheduler,
&mut leases,
&pool,
graph,
node_id,
attempt,
&mut aggregate,
&mut staging,
)
.await?;
cancel_stale(&graph, &tokens);
}
self.integrate_staged(recorder, session_id, &graph, &staging, &mut aggregate)
.await?;
Ok(aggregate)
}
async fn integrate_staged(
&self,
recorder: &Psp9Recorder,
session_id: &str,
graph: &WorkGraphRevision,
staging: &super::integrate::StagingRoot,
aggregate: &mut DispatchOutcome,
) -> Result<()> {
if staging.is_empty() {
return Ok(());
}
if matches!(aggregate.outcome, NodeTerminalOutcome::HardPass) {
match self
.run_integration_gate(recorder, session_id, graph, staging)
.await?
{
Some(paths) => aggregate.promoted_paths = paths,
None => {
aggregate.outcome = NodeTerminalOutcome::Escalated {
certificate_id: uuid::Uuid::new_v4().to_string(),
};
aggregate.status = "ESCALATED_PSP9";
}
}
} else {
recorder.record_custom(
"integration_failed",
serde_json::json!({
"staging_root": staging.digest(),
"reason": "a sibling node failed; staged winners were discarded",
}),
)?;
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
async fn dispatch_ready<'a>(
&'a self,
recorder: &'a Psp9Recorder,
session_id: &'a str,
graph: &WorkGraphRevision,
scheduler: &tokio::sync::Mutex<Scheduler>,
pool: &SharedRecoveryPool,
running: &mut FuturesUnordered<futures::future::BoxFuture<'a, NodeDone>>,
tokens: &mut std::collections::BTreeMap<String, CancellationToken>,
staging: &super::integrate::StagingRoot,
resume_seeds: &std::collections::BTreeMap<String, super::node::CandidateSeed>,
) -> Result<WorkGraphRevision> {
let mut updated = graph.clone();
loop {
let Some(selected) = take_ready_slot(recorder, scheduler, &updated).await? else {
break;
};
let claimed = self.claim_fair_share(recorder, pool, &selected)?;
let token = CancellationToken::new();
tokens.insert(selected.node_id.clone(), token.clone());
updated = execution_revision(&updated, &selected.node_id, WorkNodeState::Running)?;
recorder.record_custom("graph_revision", serde_json::to_value(&updated)?)?;
let graph_snapshot = updated.clone();
let seed = match self
.staging_seed(&updated, staging, &selected.node_id)
.await?
{
Some(seed) => Some(seed),
None => resume_seeds.get(&selected.node_id).cloned(),
};
running.push(Box::pin(async move {
let node_id = selected.node_id.clone();
let generation = selected.generation;
tokio::select! {
_ = token.cancelled() => NodeDone {
node_id,
generation,
claimed,
outcome: None,
},
attempt = self.attempt_node(
recorder,
session_id,
&selected.goal,
&selected.node_id,
selected.generation,
&self.model,
&graph_snapshot,
seed.as_ref(),
claimed,
) => NodeDone {
node_id,
generation,
claimed,
outcome: Some(attempt),
},
}
}));
}
Ok(updated)
}
#[allow(clippy::too_many_arguments)]
#[allow(clippy::too_many_arguments)]
async fn fold_completion(
&self,
recorder: &Psp9Recorder,
session_id: &str,
scheduler: &tokio::sync::Mutex<Scheduler>,
leases: &mut perspt_sdk::LeaseTable,
pool: &SharedRecoveryPool,
graph: WorkGraphRevision,
node_id: String,
attempt: NodeAttempt,
aggregate: &mut DispatchOutcome,
staging: &mut super::integrate::StagingRoot,
) -> Result<WorkGraphRevision> {
let (attempt, mut graph, _generation) = self
.recover_if_escalated(
recorder, session_id, scheduler, pool, graph, &node_id, attempt,
)
.await?;
let task = graph
.node(&node_id)
.map(|node| node.goal.clone())
.unwrap_or_default();
let (final_outcome, status, promoted) = self
.stage_or_conclude(recorder, &attempt, &node_id, &task, staging)
.await?;
let lease = leases
.acquire(
"dispatcher",
perspt_sdk::LeaseKind::GraphWrite,
Resource::WorkGraph,
)
.context("graph-write lease unavailable")?;
let terminal_state = if matches!(final_outcome, NodeTerminalOutcome::HardPass) {
WorkNodeState::Stable
} else {
WorkNodeState::Stopped {
certificate_id: uuid::Uuid::new_v4().to_string(),
}
};
graph = execution_revision(&graph, &node_id, terminal_state)?;
recorder.record_custom("graph_revision", serde_json::to_value(&graph)?)?;
recorder.record_custom(
"node_terminal",
serde_json::json!({"node_id": node_id, "outcome": final_outcome}),
)?;
leases.release(&lease);
aggregate.turns_used += attempt.outcome.turns_used;
aggregate.promoted_paths.extend(promoted);
if !matches!(final_outcome, NodeTerminalOutcome::HardPass) {
aggregate.outcome = final_outcome;
aggregate.status = status;
}
Ok(graph)
}
}
impl Psp9AgentRuntime {
fn claim_fair_share(
&self,
recorder: &Psp9Recorder,
pool: &SharedRecoveryPool,
selected: &WorkNode,
) -> Result<u32> {
let fair_share =
(self.config.rejection_budget / self.config.max_parallel_nodes.max(1) as u32).max(1);
let claimed = pool.claim(fair_share);
recorder.record_custom(
"recovery_pool_claim",
serde_json::json!({
"node_id": selected.node_id,
"claimed": claimed,
}),
)?;
Ok(claimed)
}
#[allow(clippy::too_many_arguments)]
async fn recover_if_escalated(
&self,
recorder: &Psp9Recorder,
session_id: &str,
scheduler: &tokio::sync::Mutex<Scheduler>,
pool: &SharedRecoveryPool,
graph: WorkGraphRevision,
node_id: &str,
attempt: NodeAttempt,
) -> Result<(NodeAttempt, WorkGraphRevision, u32)> {
if !matches!(
attempt.outcome.outcome,
NodeTerminalOutcome::Escalated { .. }
) {
return Ok((attempt, graph, 0));
}
let claimed = pool.claim(self.config.rejection_budget);
let mut guard = scheduler.lock().await;
let session = LadderSession {
recorder,
session_id,
node_id,
scheduler: &mut guard,
};
let goal = graph
.node(node_id)
.map(|node| node.goal.clone())
.unwrap_or_default();
let result = self
.recovery_ladder(session, claimed, graph, goal, attempt)
.await?;
pool.refund(claimed.saturating_sub(spent_of(&result.0)));
Ok(result)
}
}
impl Psp9AgentRuntime {
async fn stage_or_conclude(
&self,
recorder: &Psp9Recorder,
attempt: &NodeAttempt,
node_id: &str,
task: &str,
staging: &mut super::integrate::StagingRoot,
) -> Result<(NodeTerminalOutcome, &'static str, Vec<String>)> {
let hard = matches!(attempt.outcome.outcome, NodeTerminalOutcome::HardPass);
if !hard {
return self
.conclude_attempt(recorder, attempt, node_id, task)
.await;
}
let approved = self
.validate_and_approve_attempt(recorder, attempt, node_id, task)
.await?;
if !approved {
return Ok((
NodeTerminalOutcome::Escalated {
certificate_id: uuid::Uuid::new_v4().to_string(),
},
"ESCALATED_PSP9",
Vec::new(),
));
}
let files = attempt.candidate.export_accepted().await?;
let state_root = attempt.candidate.checkpoint(&[]).await?.witness.state_root;
let paths: Vec<String> = files.iter().map(|file| file.path.clone()).collect();
let durable = durable_staged_files(recorder, &files)?;
staging.contributions.insert(
node_id.to_string(),
super::integrate::StagedWinner {
state_root: state_root.clone(),
files,
},
);
recorder.record_custom(
"staging_root_updated",
serde_json::json!({
"node_id": node_id,
"generation": attempt.candidate.node_generation(),
"staging_root": staging.digest(),
"contributions": staging.contributions.len(),
"paths": paths,
"state_root": state_root,
"files": durable,
}),
)?;
Ok((NodeTerminalOutcome::HardPass, "COMPLETED_PSP9", Vec::new()))
}
}
fn durable_staged_files(
recorder: &Psp9Recorder,
files: &[crate::toolloop::SeedFile],
) -> Result<Vec<crate::toolloop::DurableSeedFile>> {
let put = |bytes: Option<&[u8]>| -> Result<Option<String>> {
let Some(bytes) = bytes else { return Ok(None) };
let handle = perspt_sdk::ledger::content_hash(bytes);
recorder
.store
.put_psp9_artifact(&handle, bytes, "application/octet-stream")?;
Ok(Some(handle))
};
files
.iter()
.map(|file| {
Ok(crate::toolloop::DurableSeedFile {
path: file.path.clone(),
content_artifact: put(file.content.as_deref())?,
source_preimage_artifact: put(file.source_preimage.as_deref())?,
})
})
.collect()
}
fn settle_done(
recorder: &Psp9Recorder,
pool: &SharedRecoveryPool,
done: NodeDone,
) -> Result<Option<NodeAttempt>> {
let Some(outcome) = done.outcome else {
recorder.record_custom(
"node_revalidation_cancelled",
serde_json::json!({
"node_id": done.node_id,
"generation": done.generation,
}),
)?;
pool.refund(done.claimed);
return Ok(None);
};
let attempt = outcome?;
pool.refund(done.claimed.saturating_sub(spent_of(&attempt)));
Ok(Some(attempt))
}
async fn take_ready_slot(
recorder: &Psp9Recorder,
scheduler: &tokio::sync::Mutex<Scheduler>,
graph: &WorkGraphRevision,
) -> Result<Option<WorkNode>> {
let mut guard = scheduler.lock().await;
let next = guard
.ready_nodes(graph, node_footprint)
.first()
.map(|node| (*node).clone());
let Some(node) = next else {
return Ok(None);
};
guard.start(&node, node_footprint(&node));
let slot = guard.running_count().saturating_sub(1);
recorder.record_custom(
"scheduler_dispatch",
serde_json::json!({
"node_id": node.node_id,
"generation": node.generation,
"parallel_slot": slot,
}),
)?;
Ok(Some(node))
}
fn cancel_stale(
graph: &WorkGraphRevision,
tokens: &std::collections::BTreeMap<String, CancellationToken>,
) {
for (node_id, token) in tokens {
let current = graph.node(node_id);
let stale = current.is_none()
|| current.is_some_and(|node| !matches!(node.state, WorkNodeState::Running));
if stale {
token.cancel();
}
}
}