swarm: a soft context limit per agent, as a percent of the window, applied at turn end
This commit is contained in:
parent
c56faf9dff
commit
1e03979836
1 changed files with 214 additions and 35 deletions
|
|
@ -12,6 +12,7 @@ use hive_runtime::{
|
|||
AcpRuntime, AgentRuntime, ClaudeRuntime, Config, PercentPolicy, PermissionAsk,
|
||||
PermissionPolicy, Runtime, RuntimeSpec, Sink,
|
||||
};
|
||||
use hive_claude::{CompactionPolicy, Usage};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::events::{Bus, LiveEvent};
|
||||
|
|
@ -316,23 +317,58 @@ const ACP_SESSION_FILE: &str = "hyperhive-acp-session";
|
|||
/// [`hive_claude::InfiniteSession`]. Built once by the serve loop (see
|
||||
/// [`make_session`]) and threaded through the turns, rather than rebuilt each
|
||||
/// time.
|
||||
pub type AgentSession = AgentRuntime<PercentPolicy>;
|
||||
pub type AgentSession = AgentRuntime<SoftLimitPolicy>;
|
||||
|
||||
/// [`PercentPolicy`] with the watermark the swarm sets, when it sets one.
|
||||
///
|
||||
/// The swarm's `soft_context_percent` replaces the harness's own percent
|
||||
/// (`compact_percent`) for the compaction decision at turn end, as a share of
|
||||
/// the window the model reports; a turn can still run past it. Read per
|
||||
/// decision, because the swarm changes it while the session lives. A harness
|
||||
/// percent of `0` (proactive compaction off) stays off.
|
||||
#[derive(Clone)]
|
||||
pub struct SoftLimitPolicy {
|
||||
percent: PercentPolicy,
|
||||
bus: Bus,
|
||||
}
|
||||
|
||||
impl CompactionPolicy for SoftLimitPolicy {
|
||||
fn should_compact(&self, usage: Usage) -> bool {
|
||||
let percent = match self.bus.soft_context_percent() {
|
||||
Some(soft) if self.percent.percent != 0 => soft,
|
||||
_ => self.percent.percent,
|
||||
};
|
||||
PercentPolicy {
|
||||
percent,
|
||||
..self.percent.clone()
|
||||
}
|
||||
.should_compact(usage)
|
||||
}
|
||||
|
||||
fn checkpoint_prompt(&self) -> Option<&str> {
|
||||
self.percent.checkpoint_prompt()
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct the agent's durable session, with a percent-of-window compaction
|
||||
/// policy that checkpoints (`CHECKPOINT_PROMPT`) before compacting; on claude
|
||||
/// also a constant title and the on-disk store. Called once at serve-loop start.
|
||||
/// `percent` comes from a boot-time env var and `default_window` is only a
|
||||
/// fallback for turns where the model didn't report a window, so a single
|
||||
/// build at startup is fine.
|
||||
/// build at startup is fine; the swarm's soft limit is read from the bus per
|
||||
/// decision.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if the runtime selection in the environment is invalid.
|
||||
pub fn make_session(bus: &Bus) -> Result<AgentSession> {
|
||||
let policy = PercentPolicy {
|
||||
percent: compact_percent(),
|
||||
default_window: Some(effective_context_window(bus)),
|
||||
checkpoint_prompt: Some(CHECKPOINT_PROMPT.to_string()),
|
||||
let policy = SoftLimitPolicy {
|
||||
percent: PercentPolicy {
|
||||
percent: compact_percent(),
|
||||
default_window: Some(effective_context_window(bus)),
|
||||
checkpoint_prompt: Some(CHECKPOINT_PROMPT.to_string()),
|
||||
},
|
||||
bus: bus.clone(),
|
||||
};
|
||||
Ok(match RuntimeSpec::from_env()? {
|
||||
RuntimeSpec::Claude => {
|
||||
|
|
@ -860,9 +896,48 @@ mod tests {
|
|||
error_to_turn, nonzero_or, parse_u64, report_compact, runtime_error_to_turn,
|
||||
with_turn_end_compact,
|
||||
};
|
||||
use super::{PercentPolicy, SoftLimitPolicy};
|
||||
use crate::events::{Bus, LiveEvent};
|
||||
use hive_claude::{CompactionPolicy, Usage};
|
||||
use hive_runtime::{AcpError, Error};
|
||||
|
||||
/// The swarm's soft limit replaces the harness's percent: 20% of a
|
||||
/// reported 1M window is 200k, below the 75% default, and clearing it
|
||||
/// restores the default. A harness percent of 0 stays off whatever the
|
||||
/// swarm sets.
|
||||
#[test]
|
||||
fn the_swarm_soft_limit_replaces_the_compaction_percent() {
|
||||
let bus = Bus::new();
|
||||
let policy = |percent| SoftLimitPolicy {
|
||||
percent: PercentPolicy {
|
||||
percent,
|
||||
default_window: None,
|
||||
checkpoint_prompt: None,
|
||||
},
|
||||
bus: bus.clone(),
|
||||
};
|
||||
let usage = Usage {
|
||||
context_tokens: 250_000,
|
||||
context_window: Some(1_000_000),
|
||||
};
|
||||
assert!(!policy(75).should_compact(usage));
|
||||
|
||||
bus.set_soft_context_percent(Some(20));
|
||||
assert!(policy(75).should_compact(usage));
|
||||
assert!(!policy(0).should_compact(usage));
|
||||
|
||||
// A soft limit above the default is honoured too.
|
||||
bus.set_soft_context_percent(Some(90));
|
||||
let high = Usage {
|
||||
context_tokens: 800_000,
|
||||
..usage
|
||||
};
|
||||
assert!(!policy(75).should_compact(high));
|
||||
|
||||
bus.set_soft_context_percent(None);
|
||||
assert!(policy(75).should_compact(high));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_turn_that_parsed_nothing_does_not_keep_the_previous_resolved_model() {
|
||||
let resolved = crate::paths::with_harness_dir(|| {
|
||||
|
|
|
|||
Loading…
Reference in a new issue