From be44a3bb0da51717fe996689cbc85673bcd6f50f Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Fri, 10 Apr 2026 03:20:20 -0400 Subject: [PATCH] Schedule unconscious agents by oldest last_run Pick the agent that ran longest ago (or never) instead of scanning alphabetically. Fairness via min_by_key(last_run). Co-Authored-By: ProofOfConcept --- src/mind/unconscious.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/mind/unconscious.rs b/src/mind/unconscious.rs index fdcfaed..009e80f 100644 --- a/src/mind/unconscious.rs +++ b/src/mind/unconscious.rs @@ -187,17 +187,14 @@ impl Unconscious { } let running = self.agents.iter().filter(|a| a.is_running()).count(); - if running >= self.max_concurrent { return; } - let slots = self.max_concurrent - running; - - let ready: Vec = self.agents.iter().enumerate() - .filter(|(_, a)| a.should_run()) - .map(|(i, _)| i) - .take(slots) - .collect(); - - for idx in ready { - self.spawn_agent(idx).await; + for _ in running..self.max_concurrent { + let next = self.agents.iter().enumerate() + .filter(|(_, a)| a.should_run()) + .min_by_key(|(_, a)| a.last_run); + match next { + Some((idx, _)) => self.spawn_agent(idx).await, + None => break, + } } }