Skip to main content

agent_engine/skills/
state.rs

1//! Persisted plugin management state: ~/.synaps-cli/plugins.json.
2
3use serde::{Deserialize, Serialize};
4use std::path::Path;
5
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct PluginsState {
8    #[serde(default)]
9    pub marketplaces: Vec<Marketplace>,
10    #[serde(default)]
11    pub installed: Vec<InstalledPlugin>,
12    #[serde(default)]
13    pub trusted_hosts: Vec<String>,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct Marketplace {
18    pub name: String,
19    pub url: String,
20    #[serde(default)]
21    pub description: Option<String>,
22    #[serde(default)]
23    pub last_refreshed: Option<String>,
24    #[serde(default)]
25    pub cached_plugins: Vec<CachedPlugin>,
26    /// Git clone URL for the marketplace repo. Set when the marketplace
27    /// hosts Claude-Code-style plugins whose `source` is `./<subdir>`.
28    #[serde(default)]
29    pub repo_url: Option<String>,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct CachedPlugin {
34    pub name: String,
35    pub source: String,
36    #[serde(default)]
37    pub version: Option<String>,
38    #[serde(default)]
39    pub description: Option<String>,
40    #[serde(default)]
41    pub index: Option<CachedPluginIndexMetadata>,
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct CachedPluginIndexMetadata {
46    pub repository: String,
47    #[serde(default)]
48    pub subdir: Option<String>,
49    pub checksum_algorithm: String,
50    pub checksum_value: String,
51    #[serde(default)]
52    pub compatibility_synaps: Option<String>,
53    #[serde(default)]
54    pub compatibility_extension_protocol: Option<String>,
55    pub has_extension: bool,
56    #[serde(default)]
57    pub skills: Vec<String>,
58    #[serde(default)]
59    pub permissions: Vec<String>,
60    #[serde(default)]
61    pub hooks: Vec<String>,
62    #[serde(default)]
63    pub commands: Vec<String>,
64    #[serde(default)]
65    pub providers: Vec<crate::skills::plugin_index::PluginIndexProviderCapability>,
66    #[serde(default)]
67    pub trust_publisher: Option<String>,
68    #[serde(default)]
69    pub trust_homepage: Option<String>,
70}
71
72#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
73#[serde(tag = "state", rename_all = "snake_case")]
74pub enum SetupStatus {
75    #[default]
76    NotRequired,
77    Succeeded { log_path: Option<String> },
78    Failed { message: String, log_path: Option<String> },
79}
80
81impl SetupStatus {
82    pub fn allows_extension_load(&self) -> bool {
83        !matches!(self, SetupStatus::Failed { .. })
84    }
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize)]
88pub struct InstalledPlugin {
89    pub name: String,
90    #[serde(default)]
91    pub marketplace: Option<String>,
92    pub source_url: String,
93    pub installed_commit: String,
94    #[serde(default)]
95    pub latest_commit: Option<String>,
96    pub installed_at: String,
97    /// When the plugin was installed from a subdir of a marketplace repo
98    /// (Claude-Code-style layout), this is the subdir name. `source_url`
99    /// then refers to the marketplace repo, not a standalone plugin repo.
100    #[serde(default)]
101    pub source_subdir: Option<String>,
102    /// Optional index checksum captured at install time for index-backed plugins.
103    /// Used to verify future installs/updates before applying them.
104    #[serde(default)]
105    pub checksum_algorithm: Option<String>,
106    #[serde(default)]
107    pub checksum_value: Option<String>,
108    /// Post-install setup/prebuilt/verify status. Missing in older state files
109    /// defaults to `NotRequired` for backward compatibility.
110    #[serde(default)]
111    pub setup_status: SetupStatus,
112}
113
114impl PluginsState {
115    pub fn load_from(path: &Path) -> std::io::Result<Self> {
116        match std::fs::read_to_string(path) {
117            Ok(c) => serde_json::from_str(&c)
118                .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e)),
119            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Self::default()),
120            Err(e) => Err(e),
121        }
122    }
123
124    pub fn save_to(&self, path: &Path) -> std::io::Result<()> {
125        if let Some(p) = path.parent() {
126            std::fs::create_dir_all(p)?;
127        }
128        let json = serde_json::to_string_pretty(self)
129            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
130        // Atomic write via unique temp file + rename (avoids concurrent trampling)
131        let parent = path.parent().unwrap_or(Path::new("."));
132        let tmp = tempfile::NamedTempFile::new_in(parent)?;
133        std::fs::write(tmp.path(), json)?;
134        // fsync before rename so data is durable on power loss
135        std::fs::File::open(tmp.path()).and_then(|f| f.sync_all())?;
136        tmp.persist(path).map_err(|e| e.error).map(|_| ())
137    }
138
139    /// Resolve the on-disk path for the current profile.
140    pub fn default_path() -> std::path::PathBuf {
141        crate::config::resolve_write_path("plugins.json")
142    }
143}
144
145#[cfg(test)]
146mod tests {
147    use super::*;
148
149    #[test]
150    fn plugins_state_round_trip() {
151        let s = PluginsState {
152            marketplaces: vec![Marketplace {
153                name: "pi-skills".into(),
154                url: "https://github.com/maha-media/pi-skills".into(),
155                description: Some("…".into()),
156                last_refreshed: Some("2026-04-18T12:00:00Z".into()),
157                cached_plugins: vec![CachedPlugin {
158                    name: "web".into(),
159                    source: "https://github.com/maha-media/pi-web.git".into(),
160                    version: Some("1.0".into()),
161                    description: Some("Web tools".into()),
162                    index: None,
163                }],
164                repo_url: Some("https://github.com/maha-media/pi-skills.git".into()),
165            }],
166            installed: vec![InstalledPlugin {
167                name: "web".into(),
168                marketplace: Some("pi-skills".into()),
169                source_url: "https://github.com/maha-media/pi-web.git".into(),
170                installed_commit: "abc123".into(),
171                latest_commit: Some("abc123".into()),
172                installed_at: "2026-04-18T12:01:00Z".into(),
173                source_subdir: None,
174                checksum_algorithm: None,
175                checksum_value: None,
176                setup_status: Default::default(),
177            }],
178            trusted_hosts: vec!["github.com/maha-media".into()],
179        };
180        let json = serde_json::to_string(&s).unwrap();
181        let back: PluginsState = serde_json::from_str(&json).unwrap();
182        assert_eq!(back.marketplaces.len(), 1);
183        assert_eq!(back.installed.len(), 1);
184        assert_eq!(back.trusted_hosts, vec!["github.com/maha-media"]);
185    }
186
187    #[test]
188    fn plugins_state_defaults_to_empty() {
189        let empty: PluginsState = serde_json::from_str("{}").unwrap();
190        assert!(empty.marketplaces.is_empty());
191        assert!(empty.installed.is_empty());
192        assert!(empty.trusted_hosts.is_empty());
193    }
194
195    #[test]
196    fn plugins_state_load_missing_file_is_empty() {
197        let dir = tempfile::tempdir().unwrap();
198        let path = dir.path().join("plugins.json");
199        let loaded = PluginsState::load_from(&path).unwrap();
200        assert!(loaded.marketplaces.is_empty());
201    }
202
203    #[test]
204    fn plugins_state_save_and_load_round_trip_on_disk() {
205        let dir = tempfile::tempdir().unwrap();
206        let path = dir.path().join("plugins.json");
207        let mut s = PluginsState::default();
208        s.trusted_hosts.push("github.com/x".into());
209        s.save_to(&path).unwrap();
210        let back = PluginsState::load_from(&path).unwrap();
211        assert_eq!(back.trusted_hosts, vec!["github.com/x"]);
212    }
213
214    #[test]
215    fn plugins_state_load_malformed_is_error() {
216        let dir = tempfile::tempdir().unwrap();
217        let path = dir.path().join("plugins.json");
218        std::fs::write(&path, "not json").unwrap();
219        assert!(PluginsState::load_from(&path).is_err());
220    }
221}