Skip to main content

agy_bridge/config/
agent.rs

1//! Agent configuration, system instructions, and local agent config.
2
3use std::path::PathBuf;
4
5use serde::{Deserialize, Serialize};
6use typed_builder::TypedBuilder;
7
8use super::{
9    DEFAULT_MODEL, capabilities::CapabilitiesConfig, mcp::McpServer, models::GeminiConfig,
10};
11use crate::{
12    hooks::HookEntry, policies::PolicyRule, tools::ToolDefinition, triggers::TriggerEntry,
13};
14
15/// A section within a system instruction, with a label and body text.
16#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
17pub struct SystemInstructionSection {
18    /// The content of the section.
19    pub content: String,
20    /// The title/label for this section.
21    #[serde(default = "default_section_title")]
22    pub title: String,
23}
24
25fn default_section_title() -> String {
26    "user_system_instructions".to_owned()
27}
28
29fn default_model_name() -> String {
30    DEFAULT_MODEL.to_owned()
31}
32
33/// System instruction configuration, mirroring the Python SDK's union type.
34///
35/// Uses internal tagging via `#[serde(untagged)]` so each variant is
36/// distinguishable by its `"mode"` field in JSON (e.g. `{"mode": "Custom", "text": "..."}`).
37#[non_exhaustive]
38#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(untagged)]
40pub enum SystemInstructions {
41    /// Completely replace the default system instructions (advanced usage).
42    Custom(String),
43    /// Override identity and/or append sections to the defaults (recommended).
44    Templated {
45        /// Optional identity string that replaces the agent's default persona.
46        #[serde(default)]
47        identity: Option<String>,
48        /// Sections appended to the default system instructions.
49        #[serde(default)]
50        sections: Vec<SystemInstructionSection>,
51    },
52}
53
54impl SystemInstructions {
55    /// Create custom system instructions from a plain text string.
56    #[must_use]
57    pub fn custom(text: impl Into<String>) -> Self {
58        Self::Custom(text.into())
59    }
60}
61
62impl From<&str> for SystemInstructions {
63    fn from(s: &str) -> Self {
64        Self::custom(s)
65    }
66}
67
68impl From<String> for SystemInstructions {
69    fn from(s: String) -> Self {
70        Self::custom(s)
71    }
72}
73
74// ─── JSON Schema newtype ──────────────────────────────────────────────────────────────────
75
76/// A JSON Schema definition for structured output.
77#[derive(Debug, Clone, Serialize, Deserialize)]
78#[serde(transparent)]
79pub struct JsonSchema(serde_json::Value);
80
81impl JsonSchema {
82    #[must_use]
83    /// Wrap a raw `serde_json::Value` as a `JsonSchema`.
84    pub const fn new(value: serde_json::Value) -> Self {
85        Self(value)
86    }
87
88    #[must_use]
89    /// Return a reference to the inner JSON value.
90    pub const fn as_value(&self) -> &serde_json::Value {
91        &self.0
92    }
93
94    /// Validate that the schema is structurally sound.
95    ///
96    /// Currently checks that the top-level value is a JSON object (i.e.
97    /// `serde_json::Value::Object`), which is the minimum requirement for a
98    /// valid JSON Schema.
99    ///
100    /// # Errors
101    ///
102    /// Returns a static error message if the schema is not an object.
103    pub fn validate(&self) -> Result<(), &'static str> {
104        if self.0.is_object() {
105            Ok(())
106        } else {
107            Err("JSON Schema must be a JSON object at the top level")
108        }
109    }
110}
111
112// ─── AgentConfig ─────────────────────────────────────────────────────────────
113
114/// Full configuration for creating an agent.
115///
116/// Covers model selection, system instructions, capabilities, tools,
117/// policies, hooks, MCP servers, structured output, and Gemini backend
118/// settings. All fields have sensible defaults.
119///
120/// # Construction patterns
121///
122/// `AgentConfig` deliberately supports **two** construction paths:
123///
124/// 1. **[`TypedBuilder`]** — ergonomic chained construction with `impl
125///    IntoIterator` setters for collection fields. Preferred for
126///    programmatic use:
127///    ```
128///    # use agy_bridge::config::AgentConfig;
129///    let config = AgentConfig::builder()
130///        .model("gemini-3.5-flash")
131///        .build();
132///    ```
133///
134/// 2. **Struct literal with `..Default::default()`** — convenient for
135///    deserialization (`serde`), config files, and framework code that
136///    already has fully-formed values:
137///    ```
138///    # use agy_bridge::config::AgentConfig;
139///    let config = AgentConfig {
140///        model: "gemini-3.5-flash".into(),
141///        ..AgentConfig::default()
142///    };
143///    ```
144///
145/// Both paths are supported intentionally. The builder provides ergonomic
146/// setters (e.g. accepting `impl IntoIterator` for collection fields),
147/// while struct literals enable direct field access for serialization
148/// roundtrips and downstream framework integration.
149#[derive(Debug, Clone, Serialize, Deserialize, TypedBuilder)]
150#[builder(field_defaults(default))]
151pub struct AgentConfig {
152    /// The model name (e.g. `"gemini-3.5-flash"`).
153    #[serde(default = "default_model_name")]
154    #[builder(default = DEFAULT_MODEL.to_owned(), setter(into))]
155    pub model: String,
156    /// API key. Falls back to `GEMINI_API_KEY` env var if `None`.
157    #[serde(default)]
158    #[builder(setter(into, strip_option))]
159    pub api_key: Option<String>,
160    /// Optional system instructions (custom text or templated sections).
161    #[builder(setter(into, strip_option))]
162    pub system_instructions: Option<SystemInstructions>,
163    #[serde(default)]
164    /// Agent capability toggles (tool lists, subagents, compaction).
165    #[builder(setter(strip_option))]
166    pub capabilities: Option<CapabilitiesConfig>,
167    #[serde(default, skip_serializing_if = "Vec::is_empty")]
168    /// Workspace directories the agent can access.
169    ///
170    /// When empty (the default), the Python SDK's own default of
171    /// `[os.getcwd()]` applies. Set explicitly to override.
172    #[builder(setter(transform = |v: impl IntoIterator<Item = impl Into<PathBuf>>| v.into_iter().map(Into::into).collect()))]
173    pub workspaces: Vec<PathBuf>,
174    #[serde(default, skip_serializing_if = "Vec::is_empty")]
175    /// Custom tool definitions exposed to the agent.
176    #[builder(setter(transform = |v: impl IntoIterator<Item = impl Into<ToolDefinition>>| v.into_iter().map(Into::into).collect()))]
177    pub tools: Vec<ToolDefinition>,
178    #[serde(default = "default_policies")]
179    /// Policy rules evaluated before each tool call.
180    #[builder(default = default_policies(), setter(transform = |v: impl IntoIterator<Item = impl Into<PolicyRule>>| v.into_iter().map(Into::into).collect()))]
181    pub policies: Vec<PolicyRule>,
182    #[serde(default, skip_serializing_if = "Vec::is_empty")]
183    /// Event-driven triggers attached to this agent.
184    #[builder(setter(transform = |v: impl IntoIterator<Item = impl Into<TriggerEntry>>| v.into_iter().map(Into::into).collect()))]
185    pub triggers: Vec<TriggerEntry>,
186    #[serde(default, skip_serializing_if = "Vec::is_empty")]
187    /// Lifecycle hooks (pre-turn, post-turn, pre-tool, etc.).
188    #[builder(setter(transform = |v: impl IntoIterator<Item = impl Into<HookEntry>>| v.into_iter().map(Into::into).collect()))]
189    pub hooks: Vec<HookEntry>,
190    #[serde(
191        default,
192        skip_serializing_if = "Vec::is_empty",
193        rename = "skills_paths"
194    )]
195    /// Paths to skill instruction files loaded into the agent.
196    ///
197    /// Serializes as `"skills_paths"` to match the Python SDK field name.
198    #[builder(setter(transform = |v: impl IntoIterator<Item = impl Into<PathBuf>>| v.into_iter().map(Into::into).collect()))]
199    pub skills: Vec<PathBuf>,
200
201    /// MCP server configurations.
202    #[serde(default, skip_serializing_if = "Vec::is_empty")]
203    #[builder(setter(transform = |v: impl IntoIterator<Item = impl Into<McpServer>>| v.into_iter().map(Into::into).collect()))]
204    pub mcp_servers: Vec<McpServer>,
205    /// Pre-existing conversation ID to resume.
206    #[serde(default)]
207    #[builder(setter(into, strip_option))]
208    pub conversation_id: Option<String>,
209    /// Directory where conversation state is saved.
210    #[serde(default)]
211    #[builder(setter(into, strip_option))]
212    pub save_dir: Option<PathBuf>,
213    /// Application data directory.
214    #[serde(default)]
215    #[builder(setter(into, strip_option))]
216    pub app_data_dir: Option<PathBuf>,
217    /// Optional JSON schema for structured responses.
218    #[serde(default)]
219    #[builder(setter(strip_option))]
220    pub response_schema: Option<JsonSchema>,
221    /// Gemini model backend configuration.
222    ///
223    /// Controls per-model API keys, model selection per capability,
224    /// and generation parameters such as `thinking_level`.
225    ///
226    /// Serializes as `"gemini_config"` to match the Python SDK field name.
227    #[serde(default, rename = "gemini_config")]
228    #[builder(setter(strip_option))]
229    pub gemini: Option<GeminiConfig>,
230    /// Maximum number of quota retry attempts before giving up.
231    ///
232    /// If `None`, defaults to 0 (no retries).
233    #[serde(default)]
234    #[builder(setter(into, strip_option))]
235    pub max_quota_retries: Option<u32>,
236}
237
238impl Default for AgentConfig {
239    fn default() -> Self {
240        Self::builder().build()
241    }
242}
243
244impl AgentConfig {
245    /// Resolve the effective API key using the same priority chain as the
246    /// Python SDK's `LocalAgentConfig` → `_build_harness_config`:
247    ///
248    /// 1. Per-model key (`gemini.models.default.api_key`)
249    /// 2. Shared `GeminiConfig` key (`gemini.api_key`)
250    /// 3. Top-level shorthand (`api_key`)
251    /// 4. `$GEMINI_API_KEY` environment variable
252    #[must_use]
253    pub fn effective_api_key(&self) -> Option<String> {
254        self.gemini
255            .as_ref()
256            .and_then(|g| g.models.default.api_key.clone())
257            .or_else(|| self.gemini.as_ref().and_then(|g| g.api_key.clone()))
258            .or_else(|| self.api_key.clone())
259            .or_else(|| std::env::var("GEMINI_API_KEY").ok())
260    }
261
262    /// Returns the names of all explicitly registered custom tools.
263    /// To get the full list of tools including built-ins, combine this
264    /// with `capabilities.enabled_tools` or examine `tools` + default semantics.
265    #[must_use]
266    pub fn custom_tool_names(&self) -> Vec<String> {
267        self.tools.iter().map(|t| t.name.clone()).collect()
268    }
269}
270
271// ─── LocalAgentConfig ────────────────────────────────────────────────────────
272
273/// Configuration for a local (on-device) agent, mirroring the Python SDK's
274/// `LocalAgentConfig`.
275///
276/// Wraps the standard [`AgentConfig`] via `#[serde(flatten)]`. The Python SDK's
277/// `LocalAgentConfig` extends the base `AgentConfig` with override defaults
278/// (e.g. `policies = confirm_run_command()`, `workspaces = [cwd]`), which
279/// our `AgentConfig` already matches.
280#[derive(Debug, Clone, Serialize, Deserialize, Default)]
281pub struct LocalAgentConfig {
282    /// The base agent configuration.
283    #[serde(flatten)]
284    pub agent: AgentConfig,
285}
286
287impl LocalAgentConfig {
288    /// Create a new `LocalAgentConfig` wrapping the given agent configuration.
289    #[must_use]
290    pub const fn new(agent: AgentConfig) -> Self {
291        Self { agent }
292    }
293}
294
295impl From<AgentConfig> for LocalAgentConfig {
296    fn from(agent: AgentConfig) -> Self {
297        Self::new(agent)
298    }
299}
300
301/// Matches the Python SDK's `LocalAgentConfig` default: block `run_command`,
302/// allow all other tools.
303fn default_policies() -> Vec<PolicyRule> {
304    vec![
305        PolicyRule::Deny("run_command".to_string()),
306        PolicyRule::AllowAll,
307    ]
308}
309
310#[cfg(test)]
311mod tests {
312    use pyo3::types::PyAnyMethods;
313
314    use super::{
315        super::{
316            DEFAULT_IMAGE_GENERATION_MODEL,
317            capabilities::BuiltinTools,
318            models::{
319                GenerationConfig, ModelConfig, ModelEntry, ThinkingLevel, default_image_model_entry,
320            },
321        },
322        *,
323    };
324
325    #[derive(schemars::JsonSchema)]
326    struct CustomToolParams {}
327
328    #[test]
329    fn test_roundtrip_serialization() {
330        let config = AgentConfig {
331            system_instructions: Some(SystemInstructions::Custom("Be helpful".to_string())),
332            capabilities: Some(CapabilitiesConfig {
333                enable_subagents: true,
334                enabled_tools: Some(vec![BuiltinTools::ListDir]),
335                compaction_threshold: Some(4000),
336                ..CapabilitiesConfig::default()
337            }),
338            workspaces: vec![PathBuf::from("/tmp")],
339            ..AgentConfig::default()
340        };
341
342        let json = serde_json::to_string(&config).unwrap();
343        let parsed: AgentConfig = serde_json::from_str(&json).unwrap();
344        assert_eq!(parsed.workspaces.len(), 1);
345        assert_eq!(
346            parsed.capabilities.unwrap().enabled_tools.unwrap()[0],
347            BuiltinTools::ListDir
348        );
349    }
350
351    #[test]
352    fn agent_config_builder_with_gemini() {
353        let gemini = GeminiConfig {
354            api_key: Some("test-key".to_string()),
355            base_url: None,
356            models: ModelConfig::default(),
357        };
358        let config = AgentConfig::builder().gemini(gemini).build();
359        let gemini_cfg = config.gemini.expect("gemini should be Some");
360        assert_eq!(gemini_cfg.api_key.as_deref(), Some("test-key"));
361        assert_eq!(gemini_cfg.models.default.name, DEFAULT_MODEL);
362    }
363
364    #[test]
365    fn agent_config_builder_gemini_with_thinking_level() {
366        let gemini = GeminiConfig {
367            api_key: None,
368            base_url: None,
369            models: ModelConfig {
370                default: ModelEntry {
371                    name: "gemini-3.5-flash".to_string(),
372                    api_key: None,
373                    generation: GenerationConfig {
374                        thinking_level: Some(ThinkingLevel::High),
375                    },
376                },
377                image_generation: default_image_model_entry(),
378            },
379        };
380        let config = AgentConfig::builder().gemini(gemini).build();
381        let gemini_cfg = config.gemini.expect("gemini should be Some");
382        assert_eq!(
383            gemini_cfg.models.default.generation.thinking_level,
384            Some(ThinkingLevel::High)
385        );
386        assert_eq!(gemini_cfg.models.default.name, "gemini-3.5-flash");
387    }
388
389    #[test]
390    fn agent_config_gemini_none_by_default() {
391        let config = AgentConfig::default();
392        assert!(config.gemini.is_none());
393    }
394
395    #[test]
396    fn agent_config_gemini_serde_roundtrip() {
397        let config = AgentConfig {
398            gemini: Some(GeminiConfig {
399                api_key: Some("roundtrip-key".to_string()),
400                base_url: None,
401                models: ModelConfig {
402                    default: ModelEntry {
403                        name: "gemini-3.5-flash".to_string(),
404                        api_key: Some("model-key".to_string()),
405                        generation: GenerationConfig {
406                            thinking_level: Some(ThinkingLevel::Medium),
407                        },
408                    },
409                    image_generation: default_image_model_entry(),
410                },
411            }),
412            ..AgentConfig::default()
413        };
414        let json = serde_json::to_string(&config).unwrap();
415        let parsed: AgentConfig = serde_json::from_str(&json).unwrap();
416        let gemini_cfg = parsed.gemini.expect("gemini should survive roundtrip");
417        assert_eq!(gemini_cfg.api_key.as_deref(), Some("roundtrip-key"));
418        assert_eq!(gemini_cfg.models.default.name, "gemini-3.5-flash");
419        assert_eq!(
420            gemini_cfg.models.default.api_key.as_deref(),
421            Some("model-key")
422        );
423        assert_eq!(
424            gemini_cfg.models.default.generation.thinking_level,
425            Some(ThinkingLevel::Medium)
426        );
427    }
428
429    #[test]
430    fn system_instructions_custom_serde() {
431        let instr = SystemInstructions::Custom("Be a helpful assistant".to_string());
432        let json = serde_json::to_string(&instr).unwrap();
433        let parsed: SystemInstructions = serde_json::from_str(&json).unwrap();
434        match parsed {
435            SystemInstructions::Custom(text) => assert_eq!(text, "Be a helpful assistant"),
436            SystemInstructions::Templated { .. } => {
437                panic!("Expected Custom, got Templated")
438            }
439        }
440    }
441
442    #[test]
443    fn system_instructions_templated_serde() {
444        let instr = SystemInstructions::Templated {
445            identity: Some("a security analyst".to_string()),
446            sections: vec![SystemInstructionSection {
447                content: "Always check permissions".to_string(),
448                title: "security".to_string(),
449            }],
450        };
451        let json = serde_json::to_string(&instr).unwrap();
452        let parsed: SystemInstructions = serde_json::from_str(&json).unwrap();
453        match parsed {
454            SystemInstructions::Templated { identity, sections } => {
455                assert_eq!(identity.as_deref(), Some("a security analyst"));
456                assert_eq!(sections.len(), 1);
457                assert_eq!(sections[0].content, "Always check permissions");
458            }
459            SystemInstructions::Custom(_) => {
460                panic!("Expected Templated, got Custom")
461            }
462        }
463    }
464
465    #[test]
466    fn agent_config_fully_populated_serde() {
467        let config = AgentConfig {
468            system_instructions: Some(SystemInstructions::Templated {
469                identity: Some("test-identity".to_string()),
470                sections: vec![],
471            }),
472            capabilities: Some(CapabilitiesConfig {
473                enable_subagents: true,
474                disabled_tools: Some(vec![BuiltinTools::RunCommand]),
475                compaction_threshold: Some(1000),
476                ..CapabilitiesConfig::default()
477            }),
478            workspaces: vec![PathBuf::from("/a"), PathBuf::from("/b")],
479            tools: vec![crate::tools::ToolDefinition {
480                name: "custom_tool".to_owned(),
481                description: "A custom tool".to_owned(),
482                parameter_schema: serde_json::to_value(schemars::schema_for!(CustomToolParams))
483                    .unwrap(),
484            }],
485            policies: vec![PolicyRule::DenyAll],
486            triggers: vec![TriggerEntry {
487                name: "poll".to_owned(),
488                config: crate::triggers::TriggerConfig::every_secs(30),
489                message_template: "time to poll".to_owned(),
490            }],
491            hooks: vec![HookEntry {
492                name: "pre_gate".to_owned(),
493                point: crate::hooks::HookPoint::PreTurn,
494                callback_id: "cb_pre".to_owned(),
495            }],
496            skills: vec![PathBuf::from("/skills/foo")],
497            ..AgentConfig::default()
498        };
499        let json = serde_json::to_string(&config).unwrap();
500        let parsed: AgentConfig = serde_json::from_str(&json).unwrap();
501        assert_eq!(parsed.workspaces.len(), 2);
502        assert_eq!(parsed.tools.len(), 1);
503        assert_eq!(parsed.policies.len(), 1);
504        assert_eq!(parsed.triggers.len(), 1);
505        assert_eq!(parsed.hooks.len(), 1);
506        assert_eq!(parsed.skills.len(), 1);
507    }
508
509    #[test]
510    fn agent_config_empty_defaults_serde() {
511        let json = r#"{"system_instructions":null}"#;
512        let parsed: AgentConfig = serde_json::from_str(json).unwrap();
513        assert!(parsed.system_instructions.is_none());
514        assert!(parsed.capabilities.is_none());
515        assert!(parsed.workspaces.is_empty());
516        assert!(parsed.tools.is_empty());
517        assert_eq!(
518            parsed.policies,
519            vec![
520                PolicyRule::Deny("run_command".to_string()),
521                PolicyRule::AllowAll,
522            ]
523        );
524        assert!(parsed.triggers.is_empty());
525        assert!(parsed.hooks.is_empty());
526        assert!(parsed.skills.is_empty());
527
528        assert!(parsed.gemini.is_none());
529    }
530
531    #[test]
532    fn agent_config_all_optional_fields_roundtrip() {
533        let config = AgentConfig {
534            workspaces: vec![PathBuf::from("/ws")],
535            skills: vec![PathBuf::from("/skills/test")],
536
537            conversation_id: Some("conv-123".to_string()),
538            save_dir: Some(PathBuf::from("/save")),
539            app_data_dir: Some(PathBuf::from("/app")),
540            response_schema: Some(JsonSchema::new(serde_json::json!({"type": "object"}))),
541            ..AgentConfig::default()
542        };
543        let json = serde_json::to_string(&config).unwrap();
544        let parsed: AgentConfig = serde_json::from_str(&json).unwrap();
545        assert_eq!(parsed.workspaces.len(), 1);
546        assert_eq!(parsed.conversation_id.as_deref(), Some("conv-123"));
547        assert_eq!(parsed.save_dir.as_ref().unwrap(), &PathBuf::from("/save"));
548        assert!(parsed.response_schema.is_some());
549    }
550
551    #[test]
552    fn agent_config_custom_tools_and_builtin_tools_coexist() {
553        // Audit 3: Verify that an AgentConfig can carry both custom tool
554        // definitions (for the ToolRegistry) AND SDK built-in tools
555        // (via CapabilitiesConfig.enabled_tools) at the same time.
556        let custom_tool = crate::tools::ToolDefinition {
557            name: "my_custom_tool".to_owned(),
558            description: "Does something custom".to_owned(),
559            parameter_schema: serde_json::json!({"type": "object", "properties": {}}),
560        };
561        let config = AgentConfig {
562            tools: vec![custom_tool],
563            capabilities: Some(CapabilitiesConfig {
564                enabled_tools: Some(vec![BuiltinTools::ViewFile, BuiltinTools::RunCommand]),
565                ..CapabilitiesConfig::default()
566            }),
567            ..AgentConfig::default()
568        };
569
570        // Serialize and deserialize to prove the combined config survives a roundtrip.
571        let json = serde_json::to_string(&config).unwrap();
572        let parsed: AgentConfig = serde_json::from_str(&json).unwrap();
573
574        // Custom tools are preserved.
575        assert_eq!(parsed.tools.len(), 1);
576        assert_eq!(parsed.tools[0].name, "my_custom_tool");
577
578        // Built-in tool selection is preserved.
579        let caps = parsed.capabilities.as_ref().unwrap();
580        let enabled = caps.enabled_tools.as_ref().unwrap();
581        assert_eq!(enabled.len(), 2);
582        assert!(enabled.contains(&BuiltinTools::ViewFile));
583        assert!(enabled.contains(&BuiltinTools::RunCommand));
584
585        // Validate the config is internally consistent.
586        assert!(caps.validate().is_ok());
587    }
588
589    #[test]
590    fn agent_config_custom_tools_only_no_builtins() {
591        // Verify custom_tools_only() + custom tools is valid.
592        let config = AgentConfig {
593            tools: vec![crate::tools::ToolDefinition {
594                name: "fetch_data".to_owned(),
595                description: "Fetches data".to_owned(),
596                parameter_schema: serde_json::json!({"type": "object"}),
597            }],
598            capabilities: Some(CapabilitiesConfig::custom_tools_only()),
599            ..AgentConfig::default()
600        };
601
602        let caps = config.capabilities.as_ref().unwrap();
603        assert!(caps.enabled_tools.as_ref().unwrap().is_empty());
604        assert!(caps.validate().is_ok());
605        assert_eq!(config.tools.len(), 1);
606    }
607
608    // ── LocalAgentConfig tests ───────────────────────────────────────
609
610    #[test]
611    fn local_agent_config_default() {
612        let config = LocalAgentConfig::default();
613        assert_eq!(config.agent.model, DEFAULT_MODEL);
614    }
615
616    #[test]
617    fn local_agent_config_from_agent_config() {
618        let agent_cfg = AgentConfig {
619            model: "gemini-3.5-flash".to_string(),
620            ..AgentConfig::default()
621        };
622        let local: LocalAgentConfig = agent_cfg.into();
623        assert_eq!(local.agent.model, "gemini-3.5-flash");
624    }
625
626    #[test]
627    fn local_agent_config_serde_roundtrip() {
628        let config = LocalAgentConfig::new(AgentConfig::default());
629        let json = serde_json::to_string(&config).unwrap();
630        let parsed: LocalAgentConfig = serde_json::from_str(&json).unwrap();
631        assert_eq!(parsed.agent.model, DEFAULT_MODEL);
632    }
633
634    // ── SDK field name alignment tests ────────────────────────────────
635    //
636    // Verify that serde serializes field names to match the Python SDK's
637    // expected JSON keys.
638
639    #[test]
640    fn skills_serializes_as_skills_paths() {
641        let config = AgentConfig::builder()
642            .skills(vec![PathBuf::from("/skill/a.md")])
643            .build();
644        let json = serde_json::to_string(&config).unwrap();
645        let v: serde_json::Value = serde_json::from_str(&json).unwrap();
646        assert!(
647            v.get("skills_paths").is_some(),
648            "Expected JSON key 'skills_paths', got: {json}"
649        );
650        assert!(
651            v.get("skills").is_none(),
652            "Should not have 'skills' key in JSON"
653        );
654    }
655
656    #[test]
657    fn skills_paths_deserializes_to_skills_field() {
658        let json = r#"{"skills_paths": ["/skill/a.md"]}"#;
659        let config: AgentConfig = serde_json::from_str(json).unwrap();
660        assert_eq!(config.skills.len(), 1);
661        assert_eq!(config.skills[0], PathBuf::from("/skill/a.md"));
662    }
663
664    #[test]
665    fn gemini_serializes_as_gemini_config() {
666        let config = AgentConfig::builder()
667            .gemini(super::super::GeminiConfig::default())
668            .build();
669        let json = serde_json::to_string(&config).unwrap();
670        let v: serde_json::Value = serde_json::from_str(&json).unwrap();
671        assert!(
672            v.get("gemini_config").is_some(),
673            "Expected JSON key 'gemini_config', got: {json}"
674        );
675        assert!(
676            v.get("gemini").is_none(),
677            "Should not have 'gemini' key in JSON"
678        );
679    }
680
681    #[test]
682    fn gemini_config_deserializes_to_gemini_field() {
683        let json = r#"{"gemini_config": {"api_key": "test-key"}}"#;
684        let config: AgentConfig = serde_json::from_str(json).unwrap();
685        assert_eq!(
686            config.gemini.as_ref().unwrap().api_key.as_deref(),
687            Some("test-key")
688        );
689    }
690
691    // ── skip_serializing_if tests ─────────────────────────────────────
692
693    #[test]
694    fn empty_vecs_omitted_from_json() {
695        let config = AgentConfig::default();
696        let json = serde_json::to_string(&config).unwrap();
697        let v: serde_json::Value = serde_json::from_str(&json).unwrap();
698        // These should all be absent when empty.
699        for key in &[
700            "workspaces",
701            "tools",
702            "triggers",
703            "hooks",
704            "skills_paths",
705            "mcp_servers",
706        ] {
707            assert!(
708                v.get(key).is_none(),
709                "Empty vec field '{key}' should be omitted from JSON, got: {json}"
710            );
711        }
712        // policies should always be present (non-empty default)
713        assert!(
714            v.get("policies").is_some(),
715            "policies should always be serialized"
716        );
717    }
718
719    #[test]
720    fn populated_vecs_included_in_json() {
721        let config = AgentConfig::builder()
722            .skills(vec![PathBuf::from("/skill.md")])
723            .workspaces(vec![PathBuf::from("/ws")])
724            .build();
725        let json = serde_json::to_string(&config).unwrap();
726        let v: serde_json::Value = serde_json::from_str(&json).unwrap();
727        assert!(
728            v.get("skills_paths").is_some(),
729            "Non-empty skills should be present"
730        );
731        assert!(
732            v.get("workspaces").is_some(),
733            "Non-empty workspaces should be present"
734        );
735    }
736
737    // ── Default policy tests ──────────────────────────────────────────
738
739    #[test]
740    fn default_policies_deny_run_command_allow_rest() {
741        let config = AgentConfig::default();
742        assert_eq!(config.policies.len(), 2);
743        assert_eq!(
744            config.policies[0],
745            PolicyRule::Deny("run_command".to_string())
746        );
747        assert_eq!(config.policies[1], PolicyRule::AllowAll);
748    }
749
750    // ── Python SDK mirror tests ────────────────────────────────────────
751    //
752    // These tests import the live Python SDK and verify that our Rust
753    // constants haven't drifted from the canonical Python values.  They
754    // require `pyo3::Python::initialize()` and a venv with the
755    // SDK installed.
756
757    /// Helper: extract a Python module-level attribute as a `String`.
758    fn py_str_attr(module: &str, attr: &str) -> String {
759        pyo3::Python::initialize();
760        pyo3::Python::attach(|py| {
761            crate::runtime::venv::configure_python_sys_path(py)
762                .unwrap_or_else(|e| panic!("Failed to configure python sys.path: {e}"));
763            let m = py
764                .import(module)
765                .unwrap_or_else(|e| panic!("Failed to import {module}: {e}"));
766            m.getattr(attr)
767                .unwrap_or_else(|e| panic!("Failed to get {module}.{attr}: {e}"))
768                .extract::<String>()
769                .unwrap_or_else(|e| panic!("Failed to extract {module}.{attr} as String: {e}"))
770        })
771    }
772
773    #[test]
774    fn default_model_matches_python_sdk() {
775        let py_val = py_str_attr("google.antigravity.types", "DEFAULT_MODEL");
776        assert_eq!(
777            DEFAULT_MODEL, py_val,
778            "Rust DEFAULT_MODEL ({DEFAULT_MODEL}) != Python SDK ({py_val})"
779        );
780    }
781
782    #[test]
783    fn default_image_model_matches_python_sdk() {
784        let py_val = py_str_attr("google.antigravity.types", "DEFAULT_IMAGE_GENERATION_MODEL");
785        assert_eq!(
786            DEFAULT_IMAGE_GENERATION_MODEL, py_val,
787            "Rust DEFAULT_IMAGE_GENERATION_MODEL ({DEFAULT_IMAGE_GENERATION_MODEL}) != Python SDK ({py_val})"
788        );
789    }
790
791    // ── effective_api_key tests ──────────────────────────────────────
792
793    #[test]
794    fn effective_api_key_prefers_per_model_key() {
795        let config = AgentConfig::builder()
796            .api_key("top-level-key")
797            .gemini(super::super::GeminiConfig {
798                api_key: Some("shared-key".into()),
799                base_url: None,
800                models: super::super::ModelConfig {
801                    default: super::super::ModelEntry {
802                        name: "gemini-3.5-flash".into(),
803                        api_key: Some("per-model-key".into()),
804                        generation: super::super::GenerationConfig::default(),
805                    },
806                    image_generation: super::super::ModelEntry {
807                        name: "imagen-4.0-generate-preview-06-03".into(),
808                        api_key: None,
809                        generation: super::super::GenerationConfig::default(),
810                    },
811                },
812            })
813            .build();
814        assert_eq!(config.effective_api_key().as_deref(), Some("per-model-key"));
815    }
816
817    #[test]
818    fn effective_api_key_falls_back_to_gemini_shared_key() {
819        let config = AgentConfig::builder()
820            .gemini(super::super::GeminiConfig {
821                api_key: Some("shared-key".into()),
822                ..Default::default()
823            })
824            .build();
825        assert_eq!(config.effective_api_key().as_deref(), Some("shared-key"));
826    }
827
828    #[test]
829    fn effective_api_key_falls_back_to_top_level() {
830        let config = AgentConfig::builder().api_key("top-level-key").build();
831        assert_eq!(config.effective_api_key().as_deref(), Some("top-level-key"));
832    }
833
834    #[test]
835    fn effective_api_key_none_without_any_key() {
836        // Build a config with no API key set at any level.
837        // We can't safely manipulate env vars in multi-threaded tests,
838        // so we test the chain up to the env-var fallback: if all config
839        // keys are None and the env var isn't set, the result is None.
840        // If GEMINI_API_KEY happens to be set, we verify it's returned.
841        let config = AgentConfig::builder().build();
842        let result = config.effective_api_key();
843        match std::env::var("GEMINI_API_KEY").ok() {
844            Some(env_key) => assert_eq!(result.as_deref(), Some(env_key.as_str())),
845            None => assert!(result.is_none()),
846        }
847    }
848}