mod branch;
pub(crate) mod fold;
mod nogood;
mod seed;
mod strategy;
use anyhow::{Context, Result};
use perspt_sdk::search::{BranchCandidate, ReservationTicket};
use perspt_sdk::{
evaluate_gate_with_floor, AcceptedTrajectory, BranchMeasurement, GateDecision, ModelId,
ResidualClass, ResidualEvent, SearchLimits, WitnessRef,
};
use super::node::{seed_from_attempt, CandidateSeed};
use super::{NodeAttempt, Psp9AgentRuntime, Psp9Recorder};
use crate::candidate::CodingCandidateMeasurer;
use crate::toolloop::{LoopEvent, LoopRecorder, Measured};
use branch::{consumed_usage, measure_fork_cost, BranchRecorder};
use nogood::{NoGoodComponents, NoGoodStore, NoGoodSupport};
pub(crate) use seed::SearchSeed;
use strategy::{next_strategy, BranchStrategy, BranchSummary};
#[derive(Debug, Clone)]
pub(crate) struct SearchSettings {
pub limits: SearchLimits,
pub initial_branches: u8,
pub max_branches: u8,
pub distinct_family: bool,
}
impl Default for SearchSettings {
fn default() -> Self {
Self {
limits: SearchLimits::release_default(),
initial_branches: 1,
max_branches: 3,
distinct_family: true,
}
}
}
impl SearchSettings {
pub fn from_config(config: Option<&perspt_core::ExplorationConfig>) -> Self {
let mut settings = Self::default();
if let Some(config) = config {
settings.initial_branches = config.initial_branches.unwrap_or(1);
settings.max_branches = config.max_branches.unwrap_or(3).min(3);
settings.distinct_family = config.distinct_family.unwrap_or(true);
if let Some(files) = config.max_workspace_files {
settings.limits.workspace_files = files;
}
if let Some(bytes) = config.max_workspace_bytes {
settings.limits.workspace_bytes = bytes;
}
}
settings
}
}
struct ForestRun<'a> {
recorder: &'a Psp9Recorder,
forest_id: String,
node_id: String,
generation: u32,
accepted_root: String,
baseline_energy: f64,
rho_gate: f64,
budget: perspt_sdk::search::SharedSearchBudget,
no_goods: NoGoodStore,
sensor_fingerprint: String,
started: std::time::Instant,
epoch: u64,
}
impl ForestRun<'_> {
fn emit(&self, event: LoopEvent) -> Result<()> {
self.recorder.record(&event)
}
fn open_epoch(&mut self) -> Result<()> {
self.epoch += 1;
self.budget.set_epoch(self.epoch);
self.emit(LoopEvent::FrontierEpochStarted {
forest_id: self.forest_id.clone(),
epoch: self.epoch,
forest_digest: self.forest_digest(),
})?;
self.snapshot_usage()
}
fn snapshot_usage(&self) -> Result<()> {
self.emit(LoopEvent::SearchUsageSnapshot {
forest_id: self.forest_id.clone(),
epoch: self.epoch,
usage: self.budget.snapshot(),
})
}
fn forest_digest(&self) -> String {
let mut encoder = perspt_sdk::canon::CanonicalEncoder::new(b"perspt-forest-v1");
encoder
.text(&self.node_id)
.u64(u64::from(self.generation))
.text(&self.accepted_root)
.u64(self.epoch)
.u64(u64::from(self.budget.snapshot().actions));
encoder.digest()
}
}
struct PreviousBranch {
support_key: String,
residual_count: usize,
energy: f64,
residual_summaries: Vec<String>,
obligation_refs: Vec<String>,
}
struct BranchGoal {
text: String,
base: String,
previous_obligations: Vec<String>,
}
fn goal_program(goal: &str, strategy: BranchStrategy, history: &[PreviousBranch]) -> BranchGoal {
BranchGoal {
text: branch_goal(goal, strategy, history.last()),
base: goal.to_string(),
previous_obligations: history
.last()
.map(|previous| previous.obligation_refs.clone())
.unwrap_or_default(),
}
}
struct BranchPlan {
strategy: BranchStrategy,
route: ModelId,
}
impl Psp9AgentRuntime {
#[allow(clippy::too_many_arguments)]
pub(super) async fn run_search_forest(
&self,
recorder: &Psp9Recorder,
session_id: &str,
goal: &str,
node_id: &str,
generation: u32,
model: &ModelId,
graph: &perspt_sdk::WorkGraphRevision,
seed: Option<&CandidateSeed>,
remaining_budget: u32,
baseline_energy: f64,
rho_gate: f64,
) -> Result<NodeAttempt> {
let forest_id = uuid::Uuid::new_v4().to_string();
let accepted_root = match seed {
Some(seed) => seed.expected_state_root.clone(),
None => crate::realize::snapshot_workspace(&self.working_dir, &[])?.root_hash(),
};
let (resumed_from, no_goods, budget, bypassed_forest) =
self.opening_state(node_id, generation, &accepted_root);
budget.bind_forest(&forest_id);
let mut forest = ForestRun {
recorder,
forest_id: forest_id.clone(),
node_id: node_id.to_string(),
generation,
accepted_root: accepted_root.clone(),
baseline_energy,
rho_gate,
budget,
no_goods,
sensor_fingerprint: self.live_sensor_fingerprint(),
started: std::time::Instant::now(),
epoch: 0,
};
forest.emit(LoopEvent::SearchOpened {
forest_id: forest_id.clone(),
node_id: node_id.to_string(),
generation,
accepted_root: accepted_root.clone(),
limits: forest.budget.limits().clone(),
resumed_from,
})?;
if let Some(interrupted) = bypassed_forest {
forest.emit(LoopEvent::BranchObservation {
forest_id: forest_id.clone(),
branch_id: "forest".into(),
observation: format!(
"interrupted forest {interrupted} usage unclaimed by \
{node_id}/{generation}; fresh budget under the same limits"
),
})?;
}
let attempts = self
.run_branches(
&mut forest,
session_id,
goal,
graph,
seed,
model,
remaining_budget,
)
.await?;
let chosen = self.select_and_commit(&mut forest, attempts)?;
let usage = forest.budget.close();
forest.emit(LoopEvent::SearchClosed { forest_id, usage })?;
Ok(chosen)
}
fn opening_state(
&self,
node_id: &str,
generation: u32,
accepted_root: &str,
) -> (
Option<String>,
NoGoodStore,
perspt_sdk::search::SharedSearchBudget,
Option<String>,
) {
let mut seed_slot = self.search_seed.lock().unwrap();
seed::claim(
&mut seed_slot,
&self.search.limits,
node_id,
generation,
accepted_root,
)
}
fn live_sensor_fingerprint(&self) -> String {
let mut encoder = perspt_sdk::canon::CanonicalEncoder::new(b"perspt-sensor-v1");
let registry = perspt_core::PluginRegistry::new();
for plugin in registry.detect_all(&self.working_dir) {
for capability in plugin.verifier_profile().capabilities {
encoder
.text(plugin.name())
.text(capability.stage.policy_name())
.text(capability.effective_command().unwrap_or_default());
}
}
encoder.text("perspt-cluster-v1:log-damped");
encoder.digest()
}
#[allow(clippy::too_many_arguments)]
fn history_entry(
support_key: String,
measured: &Measured,
candidate: &BranchCandidate,
) -> PreviousBranch {
PreviousBranch {
support_key,
residual_count: measured.residuals.len(),
energy: candidate.measurement.energy,
residual_summaries: measured
.residuals
.iter()
.take(6)
.map(|residual| residual.evidence.summary.clone())
.collect(),
obligation_refs: obligation_refs(measured),
}
}
#[allow(clippy::too_many_arguments)]
async fn run_branches(
&self,
forest: &mut ForestRun<'_>,
session_id: &str,
goal: &str,
graph: &perspt_sdk::WorkGraphRevision,
seed: Option<&CandidateSeed>,
model: &ModelId,
remaining_budget: u32,
) -> Result<Vec<(BranchCandidate, NodeAttempt, bool)>> {
let routes = self.branch_routes(model);
let mut attempts: Vec<(BranchCandidate, NodeAttempt, bool)> = Vec::new();
let mut partial: Option<(CandidateSeed, WitnessRef, String)> = None;
let mut history: Vec<PreviousBranch> = Vec::new();
for index in 0..usize::from(self.search.max_branches) {
let plan = self.plan_branch(index, &routes, &history, forest.baseline_energy);
let (branch_seed, witness, parent_branch) = Self::branch_lineage(
&partial,
plan.strategy.continues_partial(),
seed,
&forest.accepted_root,
);
let goal_program = goal_program(goal, plan.strategy, &history);
forest.open_epoch()?;
let outcome = self
.run_one_branch(
forest,
session_id,
&goal_program,
graph,
branch_seed.as_ref(),
&witness,
&plan,
index,
parent_branch,
remaining_budget,
)
.await?;
let Some((candidate, attempt, eligible, measured, support_key)) = outcome else {
break; };
let accepted = eligible && measured.hard_pass;
let over_budget = self.charge_attempt(forest, &attempt, &candidate).is_err();
forest.snapshot_usage()?;
if !accepted && !over_budget && index + 1 < usize::from(self.search.max_branches) {
partial = self
.partial_checkpoint(
forest,
&attempt,
&measured,
&witness,
&candidate.measurement.branch_id,
)
.await?;
}
history.push(Self::history_entry(support_key, &measured, &candidate));
let transport_dead = attempt.outcome.contained_by_transport;
let budget_denied =
Self::note_denied_reservation(forest, &candidate.measurement.branch_id)?;
attempts.push((candidate, attempt, eligible));
if accepted || over_budget || transport_dead || budget_denied {
break;
}
}
Ok(attempts)
}
fn note_denied_reservation(forest: &ForestRun<'_>, branch_id: &str) -> Result<bool> {
if !forest.budget.take_denied() {
return Ok(false);
}
forest.emit(LoopEvent::BranchAbandoned {
forest_id: forest.forest_id.clone(),
branch_id: branch_id.to_string(),
reason: "the search budget refused a reservation; branch aborted".into(),
})?;
Ok(true)
}
#[allow(clippy::type_complexity)]
fn branch_lineage(
partial: &Option<(CandidateSeed, WitnessRef, String)>,
continues: bool,
seed: Option<&CandidateSeed>,
accepted_root: &str,
) -> (Option<CandidateSeed>, WitnessRef, Option<String>) {
match (partial, continues) {
(Some((seed, witness, producer)), true) => {
(Some(seed.clone()), witness.clone(), Some(producer.clone()))
}
_ => (seed.cloned(), WitnessRef::root(accepted_root), None),
}
}
fn plan_branch(
&self,
index: usize,
routes: &[ModelId],
history: &[PreviousBranch],
baseline_energy: f64,
) -> BranchPlan {
let strategy = if index < usize::from(self.search.initial_branches.max(1)) {
BranchStrategy::LocalRepair
} else {
next_strategy(summarize(history, baseline_energy))
};
let route_index = if strategy == BranchStrategy::DistinctFamily {
index.min(routes.len() - 1)
} else {
0
};
BranchPlan {
strategy,
route: routes[route_index].clone(),
}
}
fn charge_attempt(
&self,
forest: &mut ForestRun<'_>,
attempt: &NodeAttempt,
candidate: &BranchCandidate,
) -> Result<()> {
if cfg!(debug_assertions) {
let observed = consumed_usage(&attempt.outcome);
let settled = forest.budget.snapshot();
debug_assert!(
settled.model_turns >= observed.model_turns
&& settled.tool_calls >= observed.tool_calls,
"in-loop reservations must cover the observed outcome"
);
}
let elapsed = forest.started.elapsed().as_secs();
let interval = elapsed.saturating_sub(forest.budget.snapshot().elapsed_secs);
forest.budget.charge_elapsed(interval).map_err(|error| {
let _ = forest.emit(LoopEvent::BranchObservation {
forest_id: forest.forest_id.clone(),
branch_id: candidate.measurement.branch_id.clone(),
observation: format!("budget exhausted after branch: {error}"),
});
anyhow::anyhow!("{error}")
})
}
fn branch_routes(&self, primary: &ModelId) -> Vec<ModelId> {
let mut routes = vec![primary.clone()];
for route in self
.fallback_models
.iter()
.chain(self.handoff_model.as_ref())
{
if routes.contains(route) {
continue;
}
if self.search.distinct_family {
let family = self.transport.family_of(route);
let seen = routes
.iter()
.any(|existing| self.transport.family_of(existing) == family);
if seen && routes.len() > 1 {
continue;
}
}
routes.push(route.clone());
}
routes
}
#[allow(clippy::too_many_arguments)]
async fn run_one_branch(
&self,
forest: &mut ForestRun<'_>,
session_id: &str,
goal: &BranchGoal,
graph: &perspt_sdk::WorkGraphRevision,
seed: Option<&CandidateSeed>,
witness: &WitnessRef,
plan: &BranchPlan,
index: usize,
parent_branch: Option<String>,
remaining_budget: u32,
) -> Result<Option<(BranchCandidate, NodeAttempt, bool, Measured, String)>> {
let branch_id = format!("{}/b{}", forest.forest_id, index + 1);
let prepared = self
.prepare_attempt(
forest.recorder,
session_id,
&forest.node_id,
forest.generation,
&plan.route,
graph,
)
.await?;
let no_good = self.no_good_components(
forest,
&goal.base,
plan.strategy.id(),
&prepared,
&goal.previous_obligations,
);
if !self.admit_fork(forest, &branch_id, remaining_budget, &no_good)? {
return Ok(None);
}
emit_branch_opened(forest, &branch_id, parent_branch, witness, plan)?;
let branch_recorder = BranchRecorder {
inner: forest.recorder,
forest_id: forest.forest_id.clone(),
branch_id: branch_id.clone(),
};
let partial_root = (witness.chain.len() > 1)
.then(|| seed.map(|s| s.expected_state_root.clone()))
.flatten();
let attempt = self
.attempt_prepared(
prepared,
&goal.text,
&forest.node_id,
forest.generation,
&plan.route,
graph,
seed,
remaining_budget,
&branch_recorder,
partial_root,
Some(forest.budget.clone()),
)
.await?;
let previewed = self
.preview_branch(forest, &branch_id, &attempt, &no_good)
.await?;
let (candidate, eligible, measured, support_key) = previewed;
Ok(Some((candidate, attempt, eligible, measured, support_key)))
}
fn admit_fork(
&self,
forest: &mut ForestRun<'_>,
branch_id: &str,
remaining_budget: u32,
no_good: &NoGoodComponents,
) -> Result<bool> {
if forest.no_goods.suppresses(no_good) {
forest.emit(LoopEvent::BranchObservation {
forest_id: forest.forest_id.clone(),
branch_id: branch_id.to_string(),
observation: "exact no-good suppressed a duplicate attempt".into(),
})?;
return Ok(false);
}
let turn_probe = perspt_sdk::search::ReservationRequest {
model_turns: remaining_budget.min(self.config.max_turns).max(1),
..Default::default()
};
if !forest.budget.headroom(&turn_probe) {
let _ = forest.budget.take_denied();
forest.emit(LoopEvent::BranchObservation {
forest_id: forest.forest_id.clone(),
branch_id: branch_id.to_string(),
observation: "fork refused: no model-turn headroom remains".into(),
})?;
return Ok(false);
}
let request = measure_fork_cost(&self.working_dir);
let _ticket: ReservationTicket = match forest.budget.reserve(request) {
Ok(ticket) => ticket,
Err(error) => {
forest.emit(LoopEvent::BranchObservation {
forest_id: forest.forest_id.clone(),
branch_id: branch_id.to_string(),
observation: format!("fork refused: {error}; no branch created"),
})?;
return Ok(false);
}
};
forest.snapshot_usage()?;
Ok(true)
}
async fn preview_branch(
&self,
forest: &mut ForestRun<'_>,
branch_id: &str,
attempt: &NodeAttempt,
no_good: &NoGoodComponents,
) -> Result<(BranchCandidate, bool, Measured, String)> {
let sweep = forest
.budget
.reserve(perspt_sdk::search::ReservationRequest {
verifier_runs: 1,
..Default::default()
});
if let Err(error) = sweep {
forest.emit(LoopEvent::BranchIneligible {
forest_id: forest.forest_id.clone(),
branch_id: branch_id.to_string(),
reason: format!("preview refused: {error}"),
})?;
anyhow::bail!("the budget refused the preview sweep: {error}");
}
forest.snapshot_usage()?;
let measurer =
CodingCandidateMeasurer::new(&attempt.candidate, &forest.node_id, forest.generation)
.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())
.with_correction_packets(!self.config.ablate_correction_packets);
let measured = crate::toolloop::CandidateMeasurer::measure(&measurer).await?;
let candidate_id = format!("{}/{}/{branch_id}", forest.node_id, forest.generation);
let measurement = BranchMeasurement {
branch_id: branch_id.to_string(),
candidate_id: candidate_id.clone(),
energy: measured.energy,
hard_pass: measured.hard_pass,
residuals: measured.residuals.clone(),
sensor_profile: forest.sensor_fingerprint.clone(),
cost: f64::from(attempt.outcome.turns_used),
};
forest.emit(LoopEvent::BranchCandidateMeasured {
forest_id: forest.forest_id.clone(),
branch_id: branch_id.to_string(),
candidate_id,
measurement: measurement.clone(),
})?;
let mut support_key = String::new();
if !candidate_preview_accepted(forest, &measurement) {
forest.emit(LoopEvent::BranchIneligible {
forest_id: forest.forest_id.clone(),
branch_id: branch_id.to_string(),
reason: "preview did not accept (no hard pass or measured descent)".into(),
})?;
support_key = self
.record_no_good(forest, branch_id, attempt, &measured, no_good)
.await?;
}
let eligible = candidate_eligible(forest, &measurement);
let candidate = BranchCandidate {
targeted_improvement: (forest.baseline_energy - measurement.energy).max(0.0),
measurement,
};
Ok((candidate, eligible, measured, support_key))
}
async fn record_no_good(
&self,
forest: &mut ForestRun<'_>,
branch_id: &str,
attempt: &NodeAttempt,
measured: &Measured,
no_good: &NoGoodComponents,
) -> Result<String> {
let state_root = crate::toolloop::EffectExecutor::checkpoint(&attempt.candidate, &[])
.await?
.witness
.state_root;
match no_good_support(measured, &state_root) {
Some(support) => {
let key = forest.no_goods.record(no_good, &support);
forest.emit(LoopEvent::NoGoodRecorded {
forest_id: forest.forest_id.clone(),
branch_id: branch_id.to_string(),
key,
evidence_hash: support.evidence_hash(),
support_kind: support.kind().to_string(),
})?;
Ok(support.evidence_hash())
}
None => {
forest.emit(LoopEvent::BranchObservation {
forest_id: forest.forest_id.clone(),
branch_id: branch_id.to_string(),
observation: "no deterministic support; no-good not recorded".into(),
})?;
Ok(String::new())
}
}
}
async fn partial_checkpoint(
&self,
forest: &mut ForestRun<'_>,
attempt: &NodeAttempt,
measured: &Measured,
seed_witness: &WitnessRef,
branch_id: &str,
) -> Result<Option<(CandidateSeed, WitnessRef, String)>> {
let Some(seed) = seed_from_attempt(forest.recorder, &forest.node_id, attempt).await? else {
return Ok(None);
};
let witness = seed_witness.extend(&seed.expected_state_root);
let correction = measured.packet.as_ref().map(|packet| {
perspt_sdk::CorrectionPacketRef(perspt_sdk::ledger::content_hash(
serde_json::to_string(packet).unwrap_or_default().as_bytes(),
))
});
let remaining_obligations: Vec<perspt_sdk::search::ObligationRef> =
obligation_refs(measured)
.into_iter()
.map(perspt_sdk::search::ObligationRef)
.collect();
let checkpoint = perspt_sdk::PartialCheckpointRef {
state_root: seed.expected_state_root.clone(),
accepted_ancestor: forest.accepted_root.clone(),
parent_witness: seed_witness.clone(),
correction,
remaining_obligations,
evidence_digest: seed.expected_state_root.clone(),
};
forest.emit(LoopEvent::PartialCheckpointed {
forest_id: forest.forest_id.clone(),
branch_id: branch_id.to_string(),
checkpoint,
})?;
Ok(Some((seed, witness, branch_id.to_string())))
}
fn select_and_commit(
&self,
forest: &mut ForestRun<'_>,
attempts: Vec<(BranchCandidate, NodeAttempt, bool)>,
) -> Result<NodeAttempt> {
anyhow::ensure!(!attempts.is_empty(), "the forest opened no branch");
let eligible: Vec<perspt_sdk::search::BranchCandidate> = attempts
.iter()
.filter(|(_, _, eligible)| *eligible)
.map(|(candidate, _, _)| candidate.clone())
.collect();
let selected_id = perspt_sdk::search::select_branch(&eligible)
.map(|candidate| candidate.measurement.branch_id.clone());
let mut chosen: Option<NodeAttempt> = None;
let mut fallback_best: Option<(f64, NodeAttempt)> = None;
for (candidate, attempt, _) in attempts {
if Some(&candidate.measurement.branch_id) == selected_id.as_ref() {
self.commit_selected(forest, &candidate)?;
chosen = Some(attempt);
} else {
forest.emit(LoopEvent::BranchNotSelected {
forest_id: forest.forest_id.clone(),
branch_id: candidate.measurement.branch_id.clone(),
})?;
let energy = candidate.measurement.energy;
if fallback_best
.as_ref()
.is_none_or(|(best, _)| energy < *best)
{
fallback_best = Some((energy, attempt));
}
}
}
match chosen {
Some(attempt) => Ok(attempt),
None => Ok(fallback_best
.map(|(_, attempt)| attempt)
.expect("nonempty attempts")),
}
}
fn commit_selected(
&self,
forest: &mut ForestRun<'_>,
candidate: &BranchCandidate,
) -> Result<()> {
let measurement = &candidate.measurement;
let preview = evaluate_gate_with_floor(
measurement.hard_pass,
measurement.energy,
forest.baseline_energy,
forest.rho_gate,
None,
)
.map_err(|e| anyhow::anyhow!("{e}"))?;
let mut trajectory = AcceptedTrajectory::new(
forest.node_id.clone(),
forest.generation,
forest.baseline_energy,
forest.rho_gate,
1,
)
.map_err(|e| anyhow::anyhow!("{e}"))?;
let committed = trajectory
.submit_with_floor(measurement.hard_pass, measurement.energy, None)
.map_err(|e| anyhow::anyhow!("{e}"))?;
anyhow::ensure!(
committed == preview,
"committed decision {committed:?} diverged from the preview {preview:?}; \
stale root — failing closed"
);
let recorded = trajectory.gate_decisions.last().context("one decision")?;
forest.emit(LoopEvent::CandidateMeasured {
node_id: forest.node_id.clone(),
generation: forest.generation,
candidate_id: measurement.candidate_id.clone(),
energy: measurement.energy,
hard_pass: measurement.hard_pass,
residuals: measurement.residuals.clone(),
})?;
forest.emit(LoopEvent::GateDecisionRecorded {
node_id: forest.node_id.clone(),
generation: forest.generation,
candidate_id: measurement.candidate_id.clone(),
decision: committed.clone(),
observed_energy: Some(recorded.observed_energy),
best_accepted_before: Some(recorded.best_accepted_before),
})?;
forest.emit(LoopEvent::BranchSelected {
forest_id: forest.forest_id.clone(),
branch_id: measurement.branch_id.clone(),
candidate_id: measurement.candidate_id.clone(),
})?;
forest.emit(LoopEvent::BranchCommitted {
forest_id: forest.forest_id.clone(),
branch_id: measurement.branch_id.clone(),
candidate_id: measurement.candidate_id.clone(),
decision: committed,
})?;
Ok(())
}
fn no_good_components(
&self,
forest: &ForestRun<'_>,
base_goal: &str,
strategy_id: &str,
prepared: &super::node::PreparedAttempt,
previous_obligations: &[String],
) -> NoGoodComponents {
let digest = component_digest;
let (prompt_digest, catalog_digest) = match &prepared.envelope.invocation {
Some(invocation) => (
invocation.invocation_digest.clone(),
digest("catalog", &invocation.platform.tool_spec_hash),
),
None => (
digest("prompt-text", &prepared.envelope.text),
digest("catalog", "uncompiled"),
),
};
let mut proposal = perspt_sdk::canon::CanonicalEncoder::new(b"perspt.no-good.v1");
proposal.text("proposal").text(base_goal).text(strategy_id);
for obligation in previous_obligations {
proposal.text(obligation);
}
NoGoodComponents {
accepted_root: forest.accepted_root.clone(),
domain_digest: digest("domain", &self.domain_manifest(&forest.node_id)),
strategy_digest: digest("strategy", strategy_id),
prompt_digest,
proposal_digest: proposal.digest(),
grant_digest: grant_capability_digest(&prepared.assembly),
catalog_digest,
sensor_fingerprint: forest.sensor_fingerprint.clone(),
build_digest: digest("build", &runtime_build_id()),
}
}
}
impl Psp9AgentRuntime {
fn domain_manifest(&self, node_id: &str) -> String {
let scope = perspt_sdk::DomainScope {
label: node_id.to_string(),
paths: Vec::new(),
};
serde_json::json!({
"domain_id": self.domain.domain_id().0,
"residual_schema": self.domain.residual_schema(&scope),
"energy_model": self.domain.energy_model(&scope),
"hard_gate_policy": self.domain.hard_gate_policy(&scope),
"verifier_suite": self.domain.verifier_suite(&scope),
})
.to_string()
}
}
fn emit_branch_opened(
forest: &ForestRun<'_>,
branch_id: &str,
parent_branch: Option<String>,
witness: &WitnessRef,
plan: &BranchPlan,
) -> Result<()> {
forest.emit(LoopEvent::BranchForked {
forest_id: forest.forest_id.clone(),
branch_id: branch_id.to_string(),
parent_branch,
seed_checkpoint: witness.chain.first().cloned().unwrap_or_default(),
seed_witness: witness.clone(),
})?;
forest.emit(LoopEvent::BranchStrategySelected {
forest_id: forest.forest_id.clone(),
branch_id: branch_id.to_string(),
strategy_id: plan.strategy.id().into(),
})?;
forest.emit(LoopEvent::FrontierEntryServed {
forest_id: forest.forest_id.clone(),
branch_id: branch_id.to_string(),
epoch: forest.epoch,
})
}
fn component_digest(label: &str, value: &str) -> String {
let mut encoder = perspt_sdk::canon::CanonicalEncoder::new(b"perspt.no-good.v1");
encoder.text(label).text(value);
encoder.digest()
}
fn runtime_build_id() -> String {
format!(
"{}+{}",
env!("CARGO_PKG_VERSION"),
option_env!("PERSPT_GIT_SHA").unwrap_or("dev")
)
}
fn grant_capability_digest(assembly: &super::node::NodeAssembly) -> String {
let policy = &assembly.grant_policy;
let capability = &assembly.capability;
let surface = serde_json::json!({
"effect_ceiling": policy.effect_ceiling,
"path_ceiling": policy.path_ceiling,
"command_ceiling": policy.command_ceiling,
"network_ceiling": policy.network_ceiling,
"approval_ceiling": policy.approval_ceiling,
"authority_epoch": policy.authority_epoch,
"persistent": policy.persistent,
"effects": capability.effects,
"path_scope": capability.path_scope,
"command_scope": capability.command_scope,
"network_scope": capability.network_scope,
"role": capability.role,
"approval_policy": capability.approval_policy,
"max_calls": capability.max_calls,
"write_scope": capability.write_scope,
"graph_revision": capability.graph_revision,
"node_generation": capability.node_generation,
});
component_digest("grant", &surface.to_string())
}
fn obligation_refs(measured: &Measured) -> Vec<String> {
measured
.residuals
.iter()
.map(|residual| {
format!(
"{:?}:{}",
residual.class,
perspt_sdk::ledger::content_hash(residual.evidence.summary.as_bytes())
)
})
.collect()
}
fn summarize(history: &[PreviousBranch], baseline_energy: f64) -> BranchSummary {
let Some(last) = history.last() else {
return BranchSummary::default();
};
match history.len() {
0 => BranchSummary::default(),
1 => BranchSummary {
repeated_signature: false,
obligations_decreased: false,
energy_improved: last.energy < baseline_energy,
},
_ => {
let earlier = &history[history.len() - 2];
BranchSummary {
repeated_signature: !last.support_key.is_empty()
&& last.support_key == earlier.support_key,
obligations_decreased: last.residual_count < earlier.residual_count,
energy_improved: last.energy < earlier.energy,
}
}
}
}
fn branch_goal(goal: &str, strategy: BranchStrategy, previous: Option<&PreviousBranch>) -> String {
let mut text = goal.to_string();
text.push_str("\n\n[search strategy: ");
text.push_str(strategy.id());
text.push_str("] ");
text.push_str(strategy.goal_fragment());
if let Some(previous) = previous {
if !previous.residual_summaries.is_empty() {
text.push_str("\nRemaining diagnostics from the previous attempt:\n");
for summary in &previous.residual_summaries {
text.push_str("- ");
text.push_str(summary);
text.push('\n');
}
}
}
text
}
fn candidate_eligible(forest: &ForestRun<'_>, measurement: &BranchMeasurement) -> bool {
let sensors_ok = !measurement.residuals.iter().any(|residual| {
residual.class == ResidualClass::SensorUnavailable
&& residual.evidence.summary.contains("required-stage:")
});
sensors_ok && candidate_preview_accepted(forest, measurement)
}
fn candidate_preview_accepted(forest: &ForestRun<'_>, measurement: &BranchMeasurement) -> bool {
matches!(
evaluate_gate_with_floor(
measurement.hard_pass,
measurement.energy,
forest.baseline_energy,
forest.rho_gate,
None,
),
Ok(GateDecision::HardPass | GateDecision::AcceptedByDescent { .. })
)
}
fn evidence_value(residual: &ResidualEvent) -> String {
let code = residual
.evidence
.structured
.as_ref()
.and_then(structured_codes);
match code {
Some(code) => code,
None => format!(
"{:?}:{}",
residual.class,
perspt_sdk::ledger::content_hash(residual.evidence.summary.as_bytes())
),
}
}
fn structured_codes(value: &serde_json::Value) -> Option<String> {
match value {
serde_json::Value::Object(map) => map
.get("code")
.and_then(|code| code.as_str())
.map(str::to_string),
serde_json::Value::Array(items) => {
let mut codes: Vec<&str> = items
.iter()
.filter_map(|item| item.get("code").and_then(|code| code.as_str()))
.filter(|code| !code.is_empty())
.collect();
if codes.is_empty() {
return None;
}
codes.sort_unstable();
codes.dedup();
codes.truncate(8);
Some(codes.join(","))
}
_ => None,
}
}
fn no_good_support(measured: &Measured, state_root: &str) -> Option<NoGoodSupport> {
use ResidualClass as C;
if measured.residuals.is_empty() {
return Some(NoGoodSupport::UnchangedStateHash(state_root.to_string()));
}
let mut best: Option<(u8, NoGoodSupport)> = None;
for residual in &measured.residuals {
let ranked = match residual.class {
C::TestFailure | C::Regression | C::Runtime => {
Some((0, NoGoodSupport::FailedTest(evidence_value(residual))))
}
C::Syntax
| C::Type
| C::Build
| C::Lint
| C::Format
| C::ImportGraph
| C::SymbolMismatch
| C::Manifest
| C::Dependency => Some((1, NoGoodSupport::CompilerCode(evidence_value(residual)))),
C::InterfaceMismatch
| C::OwnershipViolation
| C::SheafInconsistency
| C::ContextDrift
| C::WitnessStale
| C::ToolArgumentInvalid => Some((
2,
NoGoodSupport::ContractDiagnostic(evidence_value(residual)),
)),
C::Policy | C::CapabilityDenied => {
Some((3, NoGoodSupport::DeniedCapability(evidence_value(residual))))
}
_ => None,
};
if let Some((rank, support)) = ranked {
if best.as_ref().is_none_or(|(current, _)| rank < *current) {
best = Some((rank, support));
}
}
}
best.map(|(_, support)| support)
}
#[cfg(test)]
mod tests {
use super::*;
fn residual(class: ResidualClass) -> ResidualEvent {
let mut residual = ResidualEvent::new(
"n1",
0,
class,
perspt_sdk::ResidualSeverity::Error,
1.0,
perspt_sdk::SensorRef::new(
"governed-verifier",
perspt_sdk::IndependenceRoute::DeterministicTool,
),
)
.unwrap();
residual.evidence.summary = format!("{class:?} happened");
residual
}
fn measured(residuals: Vec<ResidualEvent>) -> Measured {
Measured {
hard_pass: false,
energy: 4.0,
residuals,
correction: None,
packet: None,
}
}
#[test]
fn support_classification_follows_gate_ab() {
use ResidualClass as C;
let cases = [
(C::TestFailure, Some("failed-test")),
(C::Runtime, Some("failed-test")),
(C::Build, Some("compiler-code")),
(C::Type, Some("compiler-code")),
(C::Lint, Some("compiler-code")),
(C::WitnessStale, Some("contract-diagnostic")),
(C::ToolArgumentInvalid, Some("contract-diagnostic")),
(C::Policy, Some("denied-capability")),
(C::CapabilityDenied, Some("denied-capability")),
(C::SensorUnavailable, None),
(C::ToolFailure, None),
(C::ProviderUnavailable, None),
(C::BudgetExhausted, None),
(C::AdjudicatorObjection, None),
];
for (class, expected) in cases {
let support = no_good_support(&measured(vec![residual(class)]), "root-1");
assert_eq!(
support.as_ref().map(NoGoodSupport::kind),
expected,
"class {class:?}"
);
}
}
#[test]
fn support_precedence_and_state_root_fallback() {
let mixed = measured(vec![
residual(ResidualClass::Build),
residual(ResidualClass::TestFailure),
]);
assert_eq!(
no_good_support(&mixed, "root-1").unwrap().kind(),
"failed-test"
);
let empty = no_good_support(&measured(vec![]), "root-xyz").unwrap();
assert!(matches!(
empty,
NoGoodSupport::UnchangedStateHash(ref root) if root == "root-xyz"
));
}
#[test]
fn evidence_value_prefers_structured_codes() {
let mut with_code = residual(ResidualClass::Build);
with_code.evidence.structured = Some(serde_json::json!({"code": "E0432"}));
assert_eq!(evidence_value(&with_code), "E0432");
let without = residual(ResidualClass::Build);
let value = evidence_value(&without);
assert!(value.starts_with("Build:"), "{value}");
assert!(!value.contains("happened"), "prose must not enter");
}
#[test]
fn evidence_value_reads_clustered_diagnostic_arrays() {
let mut clustered = residual(ResidualClass::Build);
clustered.evidence.structured = Some(serde_json::json!([
{"code": "E0433", "message": "failed to resolve", "line": 3},
{"code": "E0432", "message": "unresolved import", "line": 1},
{"code": "E0432", "message": "unresolved import", "line": 2},
]));
assert_eq!(evidence_value(&clustered), "E0432,E0433");
let mut empty = residual(ResidualClass::Build);
empty.evidence.structured = Some(serde_json::json!([]));
assert!(
evidence_value(&empty).starts_with("Build:"),
"an empty cluster falls back to the class-tagged hash"
);
}
#[test]
fn build_id_carries_version() {
assert!(runtime_build_id().starts_with(env!("CARGO_PKG_VERSION")));
}
}