agpm-cli 0.4.8

AGent Package Manager - A Git-based package manager for coding agents
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Private lockfile management for user-level patches.
//!
//! The private lockfile (`agpm.private.lock`) tracks patches from `agpm.private.toml`
//! separately from the project lockfile. This allows team members to have different
//! private patches without causing lockfile conflicts.
//!
//! # Structure
//!
//! The private lockfile uses the same array-based format as `agpm.lock`, storing
//! only resources that have private patches applied:
//!
//! ```toml
//! version = 1
//!
//! [[agents]]
//! name = "my-agent"
//! applied_patches = { temperature = "0.9", custom_field = "value" }
//!
//! [[commands]]
//! name = "deploy"
//! applied_patches = { timeout = "300" }
//! ```
//!
//! # Usage
//!
//! ```rust,no_run
//! use agpm_cli::lockfile::private_lock::PrivateLockFile;
//! use std::path::Path;
//!
//! let project_dir = Path::new(".");
//! let mut private_lock = PrivateLockFile::new();
//!
//! // Add private patches for a resource
//! let patches = std::collections::HashMap::from([
//!     ("temperature".to_string(), toml::Value::String("0.9".into())),
//! ]);
//! private_lock.add_private_patches("agents", "my-agent", patches);
//!
//! // Save to disk - creates an array-based lockfile matching agpm.lock format
//! private_lock.save(project_dir)?;
//! # Ok::<(), anyhow::Error>(())
//! ```

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;

const PRIVATE_LOCK_FILENAME: &str = "agpm.private.lock";
const PRIVATE_LOCK_VERSION: u32 = 1;

/// A resource entry in the private lockfile.
///
/// Contains only the essential fields needed to identify a resource and
/// track its private patches. This matches the structure used in `agpm.lock`
/// but stores only resources with private patches.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PrivateLockedResource {
    /// Resource name from the manifest
    pub name: String,

    /// Applied private patches
    ///
    /// Contains the key-value pairs from `agpm.private.toml` that override
    /// the resource's default configuration or project-level patches.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub applied_patches: HashMap<String, toml::Value>,
}

/// Private lockfile tracking user-level patches.
///
/// This file is gitignored and contains patches from `agpm.private.toml` only.
/// It works alongside `agpm.lock` to provide full reproducibility while keeping
/// team lockfiles deterministic.
///
/// Uses the same array-based format as `agpm.lock` for consistency.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PrivateLockFile {
    /// Lockfile format version
    pub version: u32,

    /// Private patches for agents
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub agents: Vec<PrivateLockedResource>,

    /// Private patches for snippets
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub snippets: Vec<PrivateLockedResource>,

    /// Private patches for commands
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub commands: Vec<PrivateLockedResource>,

    /// Private patches for scripts
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub scripts: Vec<PrivateLockedResource>,

    /// Private patches for MCP servers
    #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "mcp-servers")]
    pub mcp_servers: Vec<PrivateLockedResource>,

    /// Private patches for hooks
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub hooks: Vec<PrivateLockedResource>,
}

impl Default for PrivateLockFile {
    fn default() -> Self {
        Self::new()
    }
}

impl PrivateLockFile {
    /// Create a new empty private lockfile.
    pub fn new() -> Self {
        Self {
            version: PRIVATE_LOCK_VERSION,
            agents: Vec::new(),
            snippets: Vec::new(),
            commands: Vec::new(),
            scripts: Vec::new(),
            mcp_servers: Vec::new(),
            hooks: Vec::new(),
        }
    }

    /// Load private lockfile from disk.
    ///
    /// Returns `Ok(None)` if the file doesn't exist (no private patches).
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use agpm_cli::lockfile::private_lock::PrivateLockFile;
    /// use std::path::Path;
    ///
    /// let project_dir = Path::new(".");
    /// match PrivateLockFile::load(project_dir)? {
    ///     Some(lock) => println!("Loaded {} private patches", lock.total_patches()),
    ///     None => println!("No private lockfile found"),
    /// }
    /// # Ok::<(), anyhow::Error>(())
    /// ```
    pub fn load(project_dir: &Path) -> Result<Option<Self>> {
        let path = project_dir.join(PRIVATE_LOCK_FILENAME);
        if !path.exists() {
            return Ok(None);
        }

        let content = std::fs::read_to_string(&path)
            .with_context(|| format!("Failed to read {}", path.display()))?;

        let lock: Self = toml::from_str(&content)
            .with_context(|| format!("Failed to parse {}", path.display()))?;

        // Validate version
        if lock.version > PRIVATE_LOCK_VERSION {
            anyhow::bail!(
                "Private lockfile version {} is newer than supported version {}. \
                 Please upgrade AGPM.",
                lock.version,
                PRIVATE_LOCK_VERSION
            );
        }

        Ok(Some(lock))
    }

    /// Save private lockfile to disk.
    ///
    /// Deletes the file if the lockfile is empty (no private patches).
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use agpm_cli::lockfile::private_lock::PrivateLockFile;
    /// use std::path::Path;
    ///
    /// let mut lock = PrivateLockFile::new();
    /// // ... add patches ...
    /// lock.save(Path::new("."))?;
    /// # Ok::<(), anyhow::Error>(())
    /// ```
    pub fn save(&self, project_dir: &Path) -> Result<()> {
        let path = project_dir.join(PRIVATE_LOCK_FILENAME);

        // Don't create empty lockfiles; delete if exists
        if self.is_empty() {
            if path.exists() {
                std::fs::remove_file(&path)
                    .with_context(|| format!("Failed to remove {}", path.display()))?;
            }
            return Ok(());
        }

        let content = serialize_private_lockfile_with_inline_patches(self)?;

        std::fs::write(&path, content)
            .with_context(|| format!("Failed to write {}", path.display()))?;

        Ok(())
    }

    /// Check if the lockfile has any patches.
    pub fn is_empty(&self) -> bool {
        self.agents.is_empty()
            && self.snippets.is_empty()
            && self.commands.is_empty()
            && self.scripts.is_empty()
            && self.mcp_servers.is_empty()
            && self.hooks.is_empty()
    }

    /// Count total number of resources with private patches.
    pub fn total_patches(&self) -> usize {
        self.agents.len()
            + self.snippets.len()
            + self.commands.len()
            + self.scripts.len()
            + self.mcp_servers.len()
            + self.hooks.len()
    }

    /// Add private patches for a resource.
    ///
    /// If patches is empty, this is a no-op. If a resource with the same name
    /// already exists, its patches are replaced.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use agpm_cli::lockfile::private_lock::PrivateLockFile;
    /// use std::collections::HashMap;
    ///
    /// let mut lock = PrivateLockFile::new();
    /// let patches = HashMap::from([
    ///     ("model".to_string(), toml::Value::String("haiku".into())),
    /// ]);
    /// lock.add_private_patches("agents", "my-agent", patches);
    /// ```
    pub fn add_private_patches(
        &mut self,
        resource_type: &str,
        name: &str,
        patches: HashMap<String, toml::Value>,
    ) {
        if patches.is_empty() {
            return;
        }

        let vec = match resource_type {
            "agents" => &mut self.agents,
            "snippets" => &mut self.snippets,
            "commands" => &mut self.commands,
            "scripts" => &mut self.scripts,
            "mcp-servers" => &mut self.mcp_servers,
            "hooks" => &mut self.hooks,
            _ => return,
        };

        // Remove existing entry if present
        vec.retain(|r| r.name != name);

        // Add new entry
        vec.push(PrivateLockedResource {
            name: name.to_string(),
            applied_patches: patches,
        });
    }

    /// Get private patches for a specific resource.
    pub fn get_patches(
        &self,
        resource_type: &str,
        name: &str,
    ) -> Option<&HashMap<String, toml::Value>> {
        let vec = match resource_type {
            "agents" => &self.agents,
            "snippets" => &self.snippets,
            "commands" => &self.commands,
            "scripts" => &self.scripts,
            "mcp-servers" => &self.mcp_servers,
            "hooks" => &self.hooks,
            _ => return None,
        };

        vec.iter().find(|r| r.name == name).map(|r| &r.applied_patches)
    }
}

/// Convert private lockfile to TOML string with inline tables for `applied_patches`.
///
/// Uses `toml_edit` to ensure `applied_patches` fields are serialized as inline tables:
/// ```toml
/// [[agents]]
/// name = "my-agent"
/// applied_patches = { model = "haiku", temperature = "0.9" }
/// ```
///
/// Instead of the confusing separate table format:
/// ```toml
/// [[agents]]
/// name = "my-agent"
///
/// [agents.applied_patches]
/// model = "haiku"
/// ```
fn serialize_private_lockfile_with_inline_patches(lockfile: &PrivateLockFile) -> Result<String> {
    use toml_edit::{DocumentMut, Item};

    // First serialize to a toml_edit document
    let toml_str =
        toml::to_string_pretty(lockfile).context("Failed to serialize private lockfile to TOML")?;
    let mut doc: DocumentMut = toml_str.parse().context("Failed to parse TOML document")?;

    // Convert all `applied_patches` tables to inline tables
    let resource_types = ["agents", "snippets", "commands", "scripts", "hooks", "mcp-servers"];

    for resource_type in &resource_types {
        if let Some(Item::ArrayOfTables(array)) = doc.get_mut(resource_type) {
            for table in array.iter_mut() {
                if let Some(Item::Table(patches_table)) = table.get_mut("applied_patches") {
                    // Convert to inline table
                    let mut inline = toml_edit::InlineTable::new();
                    for (key, val) in patches_table.iter() {
                        if let Some(v) = val.as_value() {
                            inline.insert(key, v.clone());
                        }
                    }
                    table.insert("applied_patches", toml_edit::value(inline));
                }
            }
        }
    }

    Ok(doc.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_new_lockfile_is_empty() {
        let lock = PrivateLockFile::new();
        assert!(lock.is_empty());
        assert_eq!(lock.total_patches(), 0);
    }

    #[test]
    fn test_add_private_patches() {
        let mut lock = PrivateLockFile::new();
        let patches = HashMap::from([
            ("model".to_string(), toml::Value::String("haiku".into())),
            ("temp".to_string(), toml::Value::String("0.9".into())),
        ]);

        lock.add_private_patches("agents", "my-agent", patches);

        assert!(!lock.is_empty());
        assert_eq!(lock.total_patches(), 1);
        assert!(lock.agents.iter().any(|r| r.name == "my-agent"));
    }

    #[test]
    fn test_empty_patches_not_added() {
        let mut lock = PrivateLockFile::new();
        lock.add_private_patches("agents", "my-agent", HashMap::new());
        assert!(lock.is_empty());
    }

    #[test]
    fn test_save_and_load() {
        let temp_dir = TempDir::new().unwrap();
        let mut lock = PrivateLockFile::new();

        let patches = HashMap::from([("model".to_string(), toml::Value::String("haiku".into()))]);
        lock.add_private_patches("agents", "test", patches);

        // Save
        lock.save(temp_dir.path()).unwrap();

        // Load
        let loaded = PrivateLockFile::load(temp_dir.path()).unwrap();
        assert!(loaded.is_some());
        assert_eq!(loaded.unwrap(), lock);
    }

    #[test]
    fn test_empty_lockfile_deletes_file() {
        let temp_dir = TempDir::new().unwrap();
        let lock_path = temp_dir.path().join(PRIVATE_LOCK_FILENAME);

        // Create file
        std::fs::write(&lock_path, "test").unwrap();
        assert!(lock_path.exists());

        // Save empty lockfile should delete
        let lock = PrivateLockFile::new();
        lock.save(temp_dir.path()).unwrap();
        assert!(!lock_path.exists());
    }

    #[test]
    fn test_load_nonexistent_returns_none() {
        let temp_dir = TempDir::new().unwrap();
        let result = PrivateLockFile::load(temp_dir.path()).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_get_patches() {
        let mut lock = PrivateLockFile::new();
        let patches = HashMap::from([("model".to_string(), toml::Value::String("haiku".into()))]);
        lock.add_private_patches("agents", "test", patches.clone());

        let retrieved = lock.get_patches("agents", "test");
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap(), &patches);

        let missing = lock.get_patches("agents", "nonexistent");
        assert!(missing.is_none());
    }
}