Skip to main content

gadk_engine/
compactionvalidate.rs

1// Copyright 2026 Chris Olson
2// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Whether a compaction config can serve the applications behind a serving surface (the REST
17//! server, the trigger controllers), checked by a dry run of [`Runner::new`] per application
18//! rather than by re-implementing its checks.
19//!
20//! A config can pass [`Config::validate`] and still be unservable: with no summarizer, the
21//! default one needs the root agent's model, and a workflow agent has none. Without this check a
22//! server starts, reports healthy, and fails every request instead.
23
24use std::sync::Arc;
25
26use gadk_agent::{Agent, AgentError, Loader, go_quote};
27use gadk_platform::GoWrapped;
28use gadk_session::compaction::Config;
29
30use crate::runner::{Runner, RunnerConfig};
31
32/// Whether `cfg` can serve every application `loader` lists, given the services on `base`.
33///
34/// `None` config is always fine and the loader is then not touched; a `None` loader checks only
35/// the config's shape. An application the loader cannot load is skipped (it fails its own
36/// requests). Applications are checked in the loader's order.
37pub fn against_agents(
38    cfg: Option<&Config>,
39    loader: Option<&dyn Loader>,
40    base: &RunnerConfig,
41) -> Result<(), AgentError> {
42    let Some(cfg) = cfg else {
43        return Ok(());
44    };
45    let Some(loader) = loader else {
46        return check(cfg, Vec::new(), base);
47    };
48    let agents = loader
49        .list_agents()
50        .into_iter()
51        .filter_map(|name| loader.load_agent(&name).ok().map(|a| (name, a)))
52        .collect();
53    check(cfg, agents, base)
54}
55
56/// [`against_agents`] for a surface that serves only the loader's root agent.
57pub fn against_root_agent(
58    cfg: Option<&Config>,
59    root: Option<Arc<dyn Agent>>,
60    base: &RunnerConfig,
61) -> Result<(), AgentError> {
62    let Some(cfg) = cfg else {
63        return Ok(());
64    };
65    let agents = root
66        .map(|r| vec![(r.name().to_owned(), r)])
67        .unwrap_or_default();
68    check(cfg, agents, base)
69}
70
71/// Validates `cfg` and dry-runs [`Runner::new`] for each application. There is no "cannot check
72/// without a session service" case: a [`RunnerConfig`] always carries a session service.
73fn check(
74    cfg: &Config,
75    agents: Vec<(String, Arc<dyn Agent>)>,
76    base: &RunnerConfig,
77) -> Result<(), AgentError> {
78    cfg.validate()
79        .map_err(|e| AgentError::Other(Box::new(GoWrapped::new("invalid Compaction", e))))?;
80    for (name, agent) in agents {
81        // Two runs, so only a compaction problem is reported: anything else a runner needs may
82        // legitimately be missing at construction time, and the app fails its own requests.
83        let mut without = base.clone();
84        without.app_name = name.clone();
85        without.agent = agent;
86        without.compaction = None;
87        if Runner::new(without.clone()).is_err() {
88            continue;
89        }
90        let mut with = without;
91        with.compaction = Some(cfg.clone());
92        if let Err(e) = Runner::new(with) {
93            return Err(AgentError::Other(Box::new(GoWrapped::new(
94                format!("the Compaction config cannot serve app {}", go_quote(&name)),
95                e,
96            ))));
97        }
98    }
99    Ok(())
100}