Skip to main content

starweaver_context/
tool_runtime.rs

1//! Read-only context projection supplied to tool calls.
2
3use std::collections::BTreeMap;
4
5use crate::{ModelConfig, ToolConfig};
6
7/// Narrow, read-only configuration snapshot available during tool execution.
8///
9/// The snapshot deliberately excludes mutable conversation, task, note, message,
10/// event, usage, and lifecycle state. Tools that need coordinated mutation use
11/// dedicated handles such as [`crate::AgentContextHandle`] until narrower
12/// capability-specific handles replace them.
13#[derive(Clone, Default, Eq, PartialEq)]
14pub struct ToolRuntimeSnapshot {
15    model_config: ModelConfig,
16    tool_config: ToolConfig,
17    shell_environment: BTreeMap<String, String>,
18}
19
20impl std::fmt::Debug for ToolRuntimeSnapshot {
21    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        formatter
23            .debug_struct("ToolRuntimeSnapshot")
24            .field("model_config", &self.model_config)
25            .field("tool_config", &self.tool_config)
26            .field(
27                "shell_environment_keys",
28                &self.shell_environment.keys().collect::<Vec<_>>(),
29            )
30            .finish()
31    }
32}
33
34impl ToolRuntimeSnapshot {
35    pub(crate) const fn new(
36        model_config: ModelConfig,
37        tool_config: ToolConfig,
38        shell_environment: BTreeMap<String, String>,
39    ) -> Self {
40        Self {
41            model_config,
42            tool_config,
43            shell_environment,
44        }
45    }
46
47    pub(crate) const fn filtered(model_config: ModelConfig, tool_config: ToolConfig) -> Self {
48        Self {
49            model_config,
50            tool_config,
51            shell_environment: BTreeMap::new(),
52        }
53    }
54
55    /// Return the model limits relevant to media-producing tools.
56    #[must_use]
57    pub const fn model_config(&self) -> &ModelConfig {
58        &self.model_config
59    }
60
61    /// Return the SDK tool policy and resource limits.
62    #[must_use]
63    pub const fn tool_config(&self) -> &ToolConfig {
64        &self.tool_config
65    }
66
67    /// Return the base environment variables configured for shell tools.
68    #[must_use]
69    pub const fn shell_environment(&self) -> &BTreeMap<String, String> {
70        &self.shell_environment
71    }
72}
73
74/// Dedicated configured shell environment supplied only to authorized tool calls.
75#[derive(Clone, Default, Eq, PartialEq)]
76pub struct ShellEnvironmentSnapshot {
77    environment: BTreeMap<String, String>,
78}
79
80impl ShellEnvironmentSnapshot {
81    pub(crate) const fn new(environment: BTreeMap<String, String>) -> Self {
82        Self { environment }
83    }
84
85    /// Return configured shell environment values.
86    #[must_use]
87    pub const fn environment(&self) -> &BTreeMap<String, String> {
88        &self.environment
89    }
90}
91
92impl std::fmt::Debug for ShellEnvironmentSnapshot {
93    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94        formatter
95            .debug_struct("ShellEnvironmentSnapshot")
96            .field(
97                "environment_keys",
98                &self.environment.keys().collect::<Vec<_>>(),
99            )
100            .finish()
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use std::collections::BTreeMap;
107
108    use super::ShellEnvironmentSnapshot;
109
110    #[test]
111    fn shell_environment_snapshot_debug_omits_values() {
112        let snapshot = ShellEnvironmentSnapshot::new(BTreeMap::from([(
113            "STARWEAVER_SECRET".to_string(),
114            "STARWEAVER_SECRET_SENTINEL_7f9c".to_string(),
115        )]));
116
117        let debug = format!("{snapshot:?}");
118        assert!(debug.contains("STARWEAVER_SECRET"));
119        assert!(!debug.contains("STARWEAVER_SECRET_SENTINEL_7f9c"));
120    }
121}