fix(skills): stop the opening selection from spending the exploration budget

Two limits kept the exploration loop from running even once frontmatter made
its skills routable.

skills_ran is seeded with the agents the opening selection already ran, so it
serves two purposes at once: dedup, and the meter for max_total_skills. That
default is 20. Measured across 17 recorded engagements, 7 opened with 20 or
more agents — two with 232 — so next_round returned empty on its first call
and the loop broke before launching anything. Count the skills this loop
launched separately and budget against that; skills_ran keeps doing dedup.

The round guard was off by one. round starts at 1 and the guard read
`round >= max_rounds`, so max_rounds: 3 gave two rounds. Now `round >
max_rounds`, and 3 means 3.

Updates max_rounds_respected, which asserted at round 1 and so encoded the
off-by-one; it now asserts at round 2, keeping its intent.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Joas A SantosandClaude Opus 5 committed 2026-12-04 11:21:35 -03:00
1 parent 72f21ed13b
commit 5cea33740e
2 files changed
+102 -7

No files matched your search

+14 -3
View File
@@ -2891,6 +2891,9 @@ pub async fn run(mut cfg: RunConfig, lib: &Library, pool: &ModelPool, tx: Sender
if skill_catalog.total() > 0 {
let initial_findings: Vec<Finding> = raw.iter().flat_map(|(_, _, f)| f.clone()).collect();
let mut skills_ran: std::collections::HashSet<String> = selected.iter().map(|a| a.name.clone()).collect();
// Skills this loop launched. Separate from skills_ran, which also
// holds the opening selection and must not spend the budget.
let mut explored: usize = 0;
let exploration_budget = crate::skill::ExplorationBudget {
max_rounds: env_usize("NEUROSPLOIT_EXPLORE_ROUNDS", 3, 8) as u32,
max_skills_per_round: env_usize("NEUROSPLOIT_EXPLORE_PER_ROUND", 5, 15),
@@ -2910,7 +2913,8 @@ pub async fn run(mut cfg: RunConfig, lib: &Library, pool: &ModelPool, tx: Sender
break;
}
let next_skills = crate::skill::next_round(
&skill_catalog, &all_findings, &skills_ran, &recon, round, &exploration_budget,
&skill_catalog, &all_findings, &skills_ran, explored, &recon, round,
&exploration_budget,
);
if next_skills.is_empty() { break; }
let names: Vec<String> = next_skills.iter().map(|s| format!("{}({})", s.skill.name, s.reason)).collect();
@@ -2969,6 +2973,7 @@ pub async fn run(mut cfg: RunConfig, lib: &Library, pool: &ModelPool, tx: Sender
let round_findings: Vec<Finding> = chain_raw.iter().flat_map(|(_, _, f)| f.clone()).collect();
for (name, _, _) in &chain_raw {
skills_ran.insert(name.clone());
explored += 1;
run_snap.mark_agent_done(name, &chain_raw.iter().find(|(n, _, _)| n == name).map(|(_, _, f)| f.clone()).unwrap_or_default());
}
if round_findings.is_empty() {
@@ -3245,6 +3250,9 @@ async fn exploration_loop(
max_total_skills: env_usize("NEUROSPLOIT_EXPLORE_MAX", 20, 60),
};
let mut skills_ran: std::collections::HashSet<String> = agents_ran.iter().cloned().collect();
// Skills this loop launched. Separate from skills_ran, which also holds
// the opening selection's agents and must not spend the exploration budget.
let mut explored: usize = 0;
let mut all_findings: Vec<Finding> = initial_findings.to_vec();
let mut extra: Vec<Finding> = Vec::new();
let mut round = 1u32;
@@ -3272,7 +3280,7 @@ async fn exploration_loop(
break;
}
let next_skills = crate::skill::next_round(
&skill_catalog, &all_findings, &skills_ran, &ctx.recon_ctx, round, &budget,
&skill_catalog, &all_findings, &skills_ran, explored, &ctx.recon_ctx, round, &budget,
);
if next_skills.is_empty() { break; }
let names: Vec<String> = next_skills.iter().map(|s| format!("{}({})", s.skill.name, s.reason)).collect();
@@ -3333,7 +3341,10 @@ async fn exploration_loop(
.await;
let round_findings: Vec<Finding> = chain_raw.iter().flat_map(|(_, _, f)| f.clone()).collect();
for (name, _, _) in &chain_raw { skills_ran.insert(name.clone()); }
for (name, _, _) in &chain_raw {
skills_ran.insert(name.clone());
explored += 1;
}
if round_findings.is_empty() {
let _ = ctx.tx.send(format!("🔄 exploration round {round}: no new findings — stopping")).await;
+88 -4
View File
@@ -409,18 +409,28 @@ impl ExplorationResult {
/// Decide what to run next based on current findings.
/// Returns the skills to chain into, or empty if exploration should stop.
/// Pick the next round's skills.
///
/// `skills_ran` is the dedup set: every agent already run, including the ones
/// the main selection phase ran before exploration started. `explored` counts
/// only the skills this loop itself launched, and is what `max_total_skills`
/// budgets — measuring the budget against `skills_ran` would refuse to explore
/// at all on any engagement whose opening selection was larger than the budget,
/// which is most of them.
pub fn next_round(
catalog: &SkillCatalog,
all_findings: &[crate::types::Finding],
skills_ran: &HashSet<String>,
explored: usize,
recon: &str,
round: u32,
budget: &ExplorationBudget,
) -> Vec<ScoredSkill> {
if round >= budget.max_rounds {
// `round` is 1-based, so `max_rounds: 3` must allow rounds 1, 2 and 3.
if round > budget.max_rounds {
return Vec::new();
}
if skills_ran.len() >= budget.max_total_skills {
if explored >= budget.max_total_skills {
return Vec::new();
}
@@ -439,7 +449,7 @@ pub fn next_round(
}
// Don't exceed total budget
let remaining = budget.max_total_skills.saturating_sub(skills_ran.len());
let remaining = budget.max_total_skills.saturating_sub(explored);
next.truncate(remaining);
next
@@ -647,8 +657,9 @@ mod tests {
ran.insert("xss".to_string());
ran.insert("xss".to_string()); // duplicate won't increase count in HashSet
// `round` is 1-based: max_rounds = 1 permits round 1 and refuses round 2.
let budget = ExplorationBudget { max_rounds: 1, ..Default::default() };
let next = next_round(&catalog, &[], &ran, "html page", 1, &budget);
let next = next_round(&catalog, &[], &ran, 0, "html page", 2, &budget);
assert!(next.is_empty(), "should stop after max_rounds");
}
@@ -861,3 +872,76 @@ mod shipped_catalog_tests {
}
}
#[cfg(test)]
mod budget_tests {
use super::*;
fn triggered(name: &str) -> Skill {
let mut meta = SkillMeta::default();
meta.triggers = vec!["php".into()];
meta.priority = 20;
meta.max_rounds = 2;
Skill {
name: name.into(), title: name.into(), cwe: String::new(),
category: "vuln".into(), meta, system: String::new(), user: String::new(),
}
}
fn catalog_of(skills: Vec<Skill>) -> SkillCatalog {
let by_name = skills.iter().enumerate().map(|(i, s)| (s.name.clone(), i)).collect();
let chain_map = skills.iter()
.filter(|s| !s.meta.chains.is_empty())
.map(|s| (s.name.clone(), s.meta.chains.clone()))
.collect();
SkillCatalog { skills, by_name, chain_map }
}
/// The opening selection runs its own agents before exploration starts, and
/// they seed skills_ran for dedup. They must not spend the exploration
/// budget: 41% of recorded engagements opened with 20 or more agents, and
/// at max_total_skills = 20 that refused to explore at all.
#[test]
fn a_large_opening_selection_does_not_exhaust_the_budget() {
let catalog = catalog_of(vec![triggered("sqli_error"), triggered("lfi")]);
let budget = ExplorationBudget {
max_rounds: 3, max_skills_per_round: 5, max_total_skills: 20,
};
// 40 agents already ran in the selection phase.
let skills_ran: HashSet<String> = (0..40).map(|i| format!("opening_agent_{i}")).collect();
let picked = next_round(&catalog, &[], &skills_ran, 0, "php mysql", 1, &budget);
assert!(!picked.is_empty(), "exploration refused after a 40-agent opening selection");
}
/// Once the loop itself has spent the budget, it stops.
#[test]
fn the_budget_still_stops_the_loop() {
let catalog = catalog_of(vec![triggered("sqli_error")]);
let budget = ExplorationBudget {
max_rounds: 3, max_skills_per_round: 5, max_total_skills: 20,
};
let picked = next_round(&catalog, &[], &HashSet::new(), 20, "php", 1, &budget);
assert!(picked.is_empty(), "budget of 20 did not stop the loop at 20 explored");
}
/// `round` is 1-based, so max_rounds: 3 has to permit rounds 1, 2 and 3.
#[test]
fn max_rounds_three_allows_three_rounds() {
let catalog = catalog_of(vec![triggered("sqli_error")]);
let budget = ExplorationBudget {
max_rounds: 3, max_skills_per_round: 5, max_total_skills: 20,
};
for round in 1..=3 {
assert!(
!next_round(&catalog, &[], &HashSet::new(), 0, "php", round, &budget).is_empty(),
"round {round} refused with max_rounds = 3",
);
}
assert!(
next_round(&catalog, &[], &HashSet::new(), 0, "php", 4, &budget).is_empty(),
"round 4 ran with max_rounds = 3",
);
}
}