Skip to main content

starweaver_context/
agent_tool_state.rs

1//! Durable agent-owned state used by tool bundles.
2
3use std::collections::{BTreeMap, BTreeSet};
4
5use serde::{Deserialize, Serialize};
6use starweaver_core::Metadata;
7
8use crate::TaskManager;
9
10/// Durable agent-owned state used by tool bundles.
11///
12/// This component keeps shell, task, deferred-call, handoff-file, and dynamic
13/// tool-search state out of the lifecycle-wide [`crate::AgentContext`] root.
14/// It is flattened during context serialization to preserve the established
15/// v0 wire shape while giving runtime code an explicit ownership boundary.
16#[derive(Clone, Default, Deserialize, Serialize)]
17pub struct AgentToolState {
18    /// Legacy execution environment accepted on restore but never serialized.
19    #[serde(default, rename = "shell_env", skip_serializing)]
20    pub shell_environment: BTreeMap<String, String>,
21    /// Metadata for deferred tool calls.
22    #[serde(
23        default,
24        rename = "deferred_tool_metadata",
25        skip_serializing_if = "BTreeMap::is_empty"
26    )]
27    pub deferred_call_metadata: BTreeMap<String, Metadata>,
28    /// Files to auto-load on the next request.
29    #[serde(default, skip_serializing_if = "Vec::is_empty")]
30    pub auto_load_files: Vec<String>,
31    /// Agent-managed task state.
32    #[serde(
33        default,
34        rename = "task_manager",
35        skip_serializing_if = "TaskManager::is_empty"
36    )]
37    pub tasks: TaskManager,
38    /// Tool names loaded through dynamic tool search.
39    #[serde(
40        default,
41        rename = "tool_search_loaded_tools",
42        skip_serializing_if = "Vec::is_empty"
43    )]
44    pub loaded_tool_names: Vec<String>,
45    /// Namespace identifiers loaded through dynamic tool search.
46    #[serde(
47        default,
48        rename = "tool_search_loaded_namespaces",
49        skip_serializing_if = "Vec::is_empty"
50    )]
51    pub loaded_tool_namespaces: Vec<String>,
52    /// Applied background-subagent context-delta operation ids.
53    #[serde(
54        default,
55        rename = "background_context_delta_ids",
56        skip_serializing_if = "BTreeSet::is_empty"
57    )]
58    pub background_context_delta_ids: BTreeSet<String>,
59}
60
61impl std::fmt::Debug for AgentToolState {
62    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        formatter
64            .debug_struct("AgentToolState")
65            .field(
66                "shell_environment_keys",
67                &self.shell_environment.keys().collect::<Vec<_>>(),
68            )
69            .field("deferred_call_metadata", &self.deferred_call_metadata)
70            .field("auto_load_files", &self.auto_load_files)
71            .field("tasks", &self.tasks)
72            .field("loaded_tool_names", &self.loaded_tool_names)
73            .field("loaded_tool_namespaces", &self.loaded_tool_namespaces)
74            .field(
75                "background_context_delta_ids",
76                &self.background_context_delta_ids,
77            )
78            .finish()
79    }
80}