use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use gadk_agent::{ContextBuilder, InvocationContext};
use gadk_session::SessionService;
use gadk_session::compaction::Config;
use super::{ProgressGate, has_tail_retention};
pub(crate) const MAX_FAILURES_PER_INVOCATION: u32 = 3;
#[derive(Debug, Default)]
struct GateState {
last_compaction_tokens: i64,
failures: u32,
}
pub(crate) struct CompactionRuntime {
config: Arc<Config>,
session_service: Option<Arc<dyn SessionService>>,
compacted: AtomicBool,
gates: parking_lot::Mutex<HashMap<String, GateState>>,
}
impl CompactionRuntime {
pub(crate) fn new(
cfg: Arc<Config>,
svc: Option<Arc<dyn SessionService>>,
) -> Arc<CompactionRuntime> {
Arc::new(CompactionRuntime {
config: cfg,
session_service: svc,
compacted: AtomicBool::new(false),
gates: parking_lot::Mutex::new(HashMap::new()),
})
}
pub(crate) fn config(&self) -> &Arc<Config> {
&self.config
}
pub(crate) fn session_service(&self) -> Option<&Arc<dyn SessionService>> {
self.session_service.as_ref()
}
pub(crate) fn mark_compacted(&self) {
self.compacted.store(true, Ordering::SeqCst);
}
pub(crate) fn already_compacted(&self) -> bool {
self.compacted.load(Ordering::SeqCst)
}
pub(crate) fn configured(&self) -> bool {
true
}
pub(crate) fn enabled(&self) -> bool {
self.session_service.is_some() && has_tail_retention(Some(&self.config))
}
pub(crate) fn gate_for(
self: &Arc<Self>,
agent_name: &str,
branch: &str,
isolation_scope: &str,
) -> Arc<Gate> {
Arc::new(Gate {
rt: self.clone(),
scope: format!("{agent_name}\x00{branch}\x00{isolation_scope}"),
})
}
fn with_state<R>(&self, scope: &str, f: impl FnOnce(&mut GateState) -> R) -> R {
let mut gates = self.gates.lock();
f(gates.entry(scope.to_owned()).or_default())
}
}
pub(crate) struct Gate {
rt: Arc<CompactionRuntime>,
scope: String,
}
impl ProgressGate for Gate {
fn allow_at(&self) -> bool {
self.rt.with_state(&self.scope, |st| {
st.last_compaction_tokens == 0 && st.failures < MAX_FAILURES_PER_INVOCATION
})
}
fn record_at(&self, tokens: i64) {
self.rt
.with_state(&self.scope, |st| st.last_compaction_tokens = tokens.max(1));
}
fn recovered(&self) {
self.rt
.with_state(&self.scope, |st| st.last_compaction_tokens = 0);
}
fn failed(&self) {
self.rt.with_state(&self.scope, |st| st.failures += 1);
}
}
pub(crate) fn from_ctx(ctx: &InvocationContext) -> Option<Arc<CompactionRuntime>> {
ctx.compaction_runtime()?
.downcast::<CompactionRuntime>()
.ok()
}
pub(crate) fn attach(b: ContextBuilder, rt: Arc<CompactionRuntime>) -> ContextBuilder {
b.compaction_runtime(rt)
}
pub(crate) fn configured(ctx: &InvocationContext) -> bool {
from_ctx(ctx).is_some_and(|rt| rt.configured())
}
#[cfg(test)]
pub(crate) fn enabled(ctx: &InvocationContext) -> bool {
from_ctx(ctx).is_some_and(|rt| rt.enabled())
}
pub(crate) fn already_compacted(ctx: &InvocationContext) -> bool {
from_ctx(ctx).is_some_and(|rt| rt.already_compacted())
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
use std::sync::Arc;
use futures::StreamExt;
use gadk_agent::{AgentConfig, ContextDelta, EventStream, InvocationContext, new_agent};
use gadk_session::InMemorySessionService;
use parking_lot::Mutex;
use super::*;
use crate::agents::parallel_agent::{self, ParallelAgentConfig};
fn runtime() -> Arc<CompactionRuntime> {
CompactionRuntime::new(
Arc::new(Config {
token_threshold: 10,
event_retention_size: 1,
..Default::default()
}),
Some(Arc::new(InMemorySessionService::new())),
)
}
#[test]
fn slot_round_trips_and_absent_slot_is_not_configured() {
let rt = runtime();
let ctx = attach(InvocationContext::builder(), rt.clone()).build_invocation();
assert!(Arc::ptr_eq(&from_ctx(&ctx).unwrap(), &rt));
assert!(configured(&ctx) && enabled(&ctx) && !already_compacted(&ctx));
rt.mark_compacted();
assert!(already_compacted(&ctx));
let bare = InvocationContext::builder().build_invocation();
assert!(from_ctx(&bare).is_none());
assert!(!configured(&bare) && !enabled(&bare) && !already_compacted(&bare));
let other = InvocationContext::builder()
.compaction_runtime(Arc::new(5u8))
.build_invocation();
assert!(from_ctx(&other).is_none() && !configured(&other));
}
#[test]
fn enabled_needs_a_session_service_and_a_threshold() {
let no_svc = CompactionRuntime::new(
Arc::new(Config {
token_threshold: 10,
event_retention_size: 1,
..Default::default()
}),
None,
);
assert!(no_svc.configured() && !no_svc.enabled());
let sliding_only = CompactionRuntime::new(
Arc::new(Config {
compaction_interval: 2,
..Default::default()
}),
Some(Arc::new(InMemorySessionService::new())),
);
assert!(!sliding_only.enabled());
assert!(runtime().enabled());
}
#[test]
fn derived_contexts_share_the_root_runtime() {
let rt = runtime();
let ctx = attach(InvocationContext::builder(), rt.clone()).build_invocation();
let derived = [
ctx.with_delta(ContextDelta {
branch: Some("root.child".into()),
..Default::default()
}),
ctx.with_delta(ContextDelta {
fresh_invocation: true,
..Default::default()
}),
ctx.clone()
.into_context()
.with_delta(ContextDelta {
function_call_id: Some("fc-1".into()),
..Default::default()
})
.as_invocation()
.clone(),
ctx.clone()
.into_context()
.with_agent_cancel()
.0
.as_invocation()
.clone(),
];
for d in &derived {
assert!(
Arc::ptr_eq(&from_ctx(d).unwrap(), &rt),
"a derived context lost the runtime"
);
}
}
#[tokio::test]
async fn parallel_sub_agents_share_the_root_runtime() {
let seen: Arc<Mutex<Vec<Option<Arc<CompactionRuntime>>>>> = Arc::default();
let probe = |name: &str| {
let seen = seen.clone();
new_agent(AgentConfig {
name: name.into(),
run: Some(Arc::new(move |ctx: InvocationContext| -> EventStream {
seen.lock().push(from_ctx(&ctx));
Box::pin(futures::stream::empty())
})),
..Default::default()
})
.unwrap()
};
let par = parallel_agent::new(ParallelAgentConfig {
agent: AgentConfig {
name: "par".into(),
sub_agents: vec![probe("left"), probe("right")],
..Default::default()
},
})
.unwrap();
let rt = runtime();
let ctx = attach(InvocationContext::builder(), rt.clone())
.agent(par.clone())
.build_invocation();
let mut events = par.run(ctx);
while let Some(item) = events.next().await {
item.unwrap();
}
let seen = seen.lock();
assert_eq!(seen.len(), 2, "both sub-agents should have run");
for got in seen.iter() {
assert!(Arc::ptr_eq(
got.as_ref().expect("sub-agent saw no runtime"),
&rt
));
}
}
}