swarm: set the fresh-session threshold per agent
This commit is contained in:
parent
49ee678117
commit
5175d3663e
3 changed files with 154 additions and 32 deletions
|
|
@ -218,6 +218,11 @@ pub struct Bus {
|
|||
/// compaction policy compacts at it instead of the harness's own
|
||||
/// watermark. `None` until the swarm sets one.
|
||||
soft_context_percent: Arc<Mutex<Option<u8>>>,
|
||||
/// The swarm's fresh-session threshold, as a percent of the context window
|
||||
/// (`fresh_session_percent` of the agent's `agent-inference` key): above it, a
|
||||
/// turn that finds the prompt cache cold starts a fresh session. `None`
|
||||
/// until the swarm sets one, which keeps the harness's own half.
|
||||
fresh_session_percent: Arc<Mutex<Option<u8>>>,
|
||||
/// Resolved model id from the most recent turn's `assistant` events
|
||||
/// (the API-echoed `message.model`, e.g. `claude-opus-4-8`), as
|
||||
/// opposed to the requested `--model` name which may be a short
|
||||
|
|
@ -261,6 +266,7 @@ impl Bus {
|
|||
last_turn_ended_unix: Arc::new(AtomicI64::new(0)),
|
||||
api_context_window: Arc::new(Mutex::new(None)),
|
||||
soft_context_percent: Arc::new(Mutex::new(None)),
|
||||
fresh_session_percent: Arc::new(Mutex::new(None)),
|
||||
last_resolved_model: Arc::new(Mutex::new(None)),
|
||||
}
|
||||
}
|
||||
|
|
@ -530,6 +536,17 @@ impl Bus {
|
|||
*self.soft_context_percent.lock().unwrap()
|
||||
}
|
||||
|
||||
/// Set (or clear, with `None`) the swarm's fresh-session threshold.
|
||||
pub fn set_fresh_session_percent(&self, percent: Option<u8>) {
|
||||
*self.fresh_session_percent.lock().unwrap() = percent;
|
||||
}
|
||||
|
||||
/// The swarm's fresh-session threshold, as a percent of the window.
|
||||
#[must_use]
|
||||
pub fn fresh_session_percent(&self) -> Option<u8> {
|
||||
*self.fresh_session_percent.lock().unwrap()
|
||||
}
|
||||
|
||||
/// Walk a stream-json value for `tool_use` blocks and bump the
|
||||
/// per-turn counter for each one we find. Called by the stdout
|
||||
/// pump on every parsed line. Cheap when the line isn't an
|
||||
|
|
|
|||
|
|
@ -21,7 +21,8 @@
|
|||
//! start, the agent runs on its local values.
|
||||
//!
|
||||
//! `subagent_models` and `max_subagents` are not read here. `soft_context_percent`
|
||||
//! is handed to the Bus, which the compaction policy reads at turn end.
|
||||
//! is handed to the Bus, which the compaction policy reads at turn end, and so is
|
||||
//! `fresh_session_percent`, which the pre-turn auto-reset reads.
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
|
|
@ -147,6 +148,9 @@ pub async fn run(bus: Bus) {
|
|||
target
|
||||
.bus
|
||||
.set_soft_context_percent(read.soft_context_percent);
|
||||
target
|
||||
.bus
|
||||
.set_fresh_session_percent(read.fresh_session_percent);
|
||||
}
|
||||
tokio::time::sleep(POLL_INTERVAL).await;
|
||||
}
|
||||
|
|
@ -234,6 +238,7 @@ mod tests {
|
|||
subagent_models: None,
|
||||
max_subagents: None,
|
||||
soft_context_percent: None,
|
||||
fresh_session_percent: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -264,12 +264,16 @@ pub fn stall_sleep_secs() -> u64 {
|
|||
|
||||
/// Resolve the auto-reset watermark. Priority order:
|
||||
/// 1. `HIVE_AUTO_RESET_WATERMARK_TOKENS` env var (explicit override).
|
||||
/// 2. 50% of the window the API last reported, or `0` before one has been.
|
||||
/// 2. The swarm's `fresh_session_percent` of the window the API last reported,
|
||||
/// or 50% when the swarm sets none; `0` before a window has been reported.
|
||||
///
|
||||
/// `0` disables auto-reset entirely.
|
||||
fn auto_reset_watermark_tokens(bus: &Bus) -> u64 {
|
||||
env_u64("HIVE_AUTO_RESET_WATERMARK_TOKENS")
|
||||
.unwrap_or_else(|| bus.api_context_window().map_or(0, |window| window / 2))
|
||||
env_u64("HIVE_AUTO_RESET_WATERMARK_TOKENS").unwrap_or_else(|| {
|
||||
let percent = u64::from(bus.fresh_session_percent().unwrap_or(50));
|
||||
bus.api_context_window()
|
||||
.map_or(0, |window| window * percent / 100)
|
||||
})
|
||||
}
|
||||
|
||||
/// Resolve the assumed cache TTL: `HIVE_CACHE_TTL_SECS` if set (`0` disables
|
||||
|
|
@ -958,6 +962,11 @@ mod tests {
|
|||
assert_eq!(auto_reset_watermark_tokens(&bus), 0);
|
||||
bus.set_api_context_window(1_000_000);
|
||||
assert_eq!(auto_reset_watermark_tokens(&bus), 500_000);
|
||||
// The swarm sets the threshold: a percent of the same window.
|
||||
bus.set_fresh_session_percent(Some(20));
|
||||
assert_eq!(auto_reset_watermark_tokens(&bus), 200_000);
|
||||
bus.set_fresh_session_percent(None);
|
||||
assert_eq!(auto_reset_watermark_tokens(&bus), 500_000);
|
||||
}
|
||||
|
||||
/// A turn that reports no window never triggers the proactive compaction,
|
||||
|
|
|
|||
Loading…
Reference in a new issue