agpm-cli 0.4.14

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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! Resource management operations for lockfiles.
//!
//! This module provides methods for adding, retrieving, and managing locked
//! resources (agents, snippets, commands, scripts, hooks, MCP servers) within
//! the lockfile.

use super::{LockFile, LockedResource, LockedSource, ResourceId};

impl LockFile {
    /// Add or update source repository, setting fetched_at to current UTC time.
    ///
    /// Replaces existing source with same name.
    ///
    /// # Arguments
    ///
    /// * `name` - Unique source identifier (matches manifest `[sources]` keys)
    /// * `url` - Full Git repository URL
    /// * `commit` - Resolved 40-character commit hash
    ///
    /// # Behavior
    ///
    /// If a source with the same name already exists, it will be replaced with
    /// the new information. This ensures that each source name appears exactly
    /// once in the lockfile.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::lockfile::LockFile;
    ///
    /// let mut lockfile = LockFile::new();
    /// lockfile.add_source(
    ///     "community".to_string(),
    ///     "https://github.com/example/community.git".to_string(),
    ///     "a1b2c3d4e5f6789abcdef0123456789abcdef012".to_string()
    /// );
    ///
    /// assert_eq!(lockfile.sources.len(), 1);
    /// assert_eq!(lockfile.sources[0].name, "community");
    /// ```
    ///
    /// # Time Zone
    ///
    /// The `fetched_at` timestamp is always recorded in UTC to ensure consistency
    /// across different time zones and systems.
    pub fn add_source(&mut self, name: String, url: String, _commit: String) {
        // Remove existing entry if present
        self.sources.retain(|s| s.name != name);

        self.sources.push(LockedSource {
            name,
            url,
            fetched_at: chrono::Utc::now().to_rfc3339(),
        });
    }

    /// Find source repository by name.
    ///
    /// # Arguments
    ///
    /// * `name` - Source name to search for (matches manifest `[sources]` keys)
    ///
    /// # Returns
    ///
    /// * `Some(&LockedSource)` - Reference to the found source
    /// * `None` - No source with that name exists
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use agpm_cli::lockfile::LockFile;
    /// # let lockfile = LockFile::new();
    /// if let Some(source) = lockfile.get_source("community") {
    ///     println!("Source URL: {}", source.url);
    ///     println!("Fetched at: {}", source.fetched_at);
    /// }
    /// ```
    #[must_use]
    pub fn get_source(&self, name: &str) -> Option<&LockedSource> {
        self.sources.iter().find(|s| s.name == name)
    }

    /// Add or update resource (agents or snippets only).
    ///
    /// Replaces existing resource with same name.
    ///
    /// **Note**: Backward compatibility only. Use `add_typed_resource` for all types.
    ///
    /// # Arguments
    ///
    /// * `name` - Unique resource identifier within its type
    /// * `resource` - Complete [`LockedResource`] with all resolved information
    /// * `is_agent` - `true` for agents, `false` for snippets
    ///
    /// # Behavior
    ///
    /// If a resource with the same name already exists in the same type category,
    /// it will be replaced. Resources are categorized separately (agents vs snippets),
    /// so an agent named "helper" and a snippet named "helper" can coexist.
    ///
    /// # Examples
    ///
    /// Adding an agent:
    ///
    /// ```rust,no_run
    /// use agpm_cli::lockfile::{LockFile, LockedResourceBuilder};
    /// use agpm_cli::core::ResourceType;
    ///
    /// let mut lockfile = LockFile::new();
    /// let resource = LockedResourceBuilder::new(
    ///     "example-agent".to_string(),
    ///     "agents/example.md".to_string(),
    ///     "sha256:abcdef...".to_string(),
    ///     "agents/example-agent.md".to_string(),
    ///     ResourceType::Agent,
    /// )
    /// .source(Some("community".to_string()))
    /// .url(Some("https://github.com/example/repo.git".to_string()))
    /// .version(Some("^1.0".to_string()))
    /// .resolved_commit(Some("a1b2c3d...".to_string()))
    /// .tool(Some("claude-code".to_string()))
    /// .dependencies(Vec::new())
    /// .applied_patches(std::collections::BTreeMap::new())
    /// .build();
    ///
    /// lockfile.add_resource("example-agent".to_string(), resource, true);
    /// assert_eq!(lockfile.agents.len(), 1);
    /// ```
    ///
    /// Adding a snippet:
    ///
    /// ```rust,no_run
    /// # use agpm_cli::lockfile::{LockFile, LockedResourceBuilder};
    /// # use agpm_cli::core::ResourceType;
    /// # let mut lockfile = LockFile::new();
    /// let snippet = LockedResourceBuilder::new(
    ///     "util-snippet".to_string(),
    ///     "../local/utils.md".to_string(),
    ///     "sha256:fedcba...".to_string(),
    ///     "snippets/util-snippet.md".to_string(),
    ///     ResourceType::Snippet,
    /// )
    /// .tool(Some("claude-code".to_string()))
    /// .dependencies(Vec::new())
    /// .applied_patches(std::collections::BTreeMap::new())
    /// .build();
    ///
    /// lockfile.add_resource("util-snippet".to_string(), snippet, false);
    /// assert_eq!(lockfile.snippets.len(), 1);
    /// ```
    pub fn add_resource(&mut self, name: String, resource: LockedResource, is_agent: bool) {
        let resources = if is_agent {
            &mut self.agents
        } else {
            &mut self.snippets
        };

        // Remove existing entry if present
        resources.retain(|r| r.name != name);
        resources.push(resource);
    }

    /// Add or update resource with explicit type support.
    ///
    /// Preferred method - supports all resource types.
    ///
    /// # Arguments
    ///
    /// * `name` - Unique resource identifier within its type
    /// * `resource` - Complete [`LockedResource`] with all resolved information
    /// * `resource_type` - The type of resource (Agent, Snippet, or Command)
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use agpm_cli::lockfile::{LockFile, LockedResourceBuilder};
    /// use agpm_cli::core::ResourceType;
    ///
    /// let mut lockfile = LockFile::new();
    /// let command = LockedResourceBuilder::new(
    ///     "build-command".to_string(),
    ///     "commands/build.md".to_string(),
    ///     "sha256:abcdef...".to_string(),
    ///     ".claude/commands/build-command.md".to_string(),
    ///     ResourceType::Command,
    /// )
    /// .source(Some("community".to_string()))
    /// .url(Some("https://github.com/example/repo.git".to_string()))
    /// .version(Some("v1.0.0".to_string()))
    /// .resolved_commit(Some("a1b2c3d...".to_string()))
    /// .tool(Some("claude-code".to_string()))
    /// .dependencies(Vec::new())
    /// .applied_patches(std::collections::BTreeMap::new())
    /// .build();
    ///
    /// lockfile.add_typed_resource("build-command".to_string(), command, ResourceType::Command);
    /// assert_eq!(lockfile.commands.len(), 1);
    /// ```
    pub fn add_typed_resource(
        &mut self,
        name: String,
        resource: LockedResource,
        resource_type: crate::core::ResourceType,
    ) {
        let resources = match resource_type {
            crate::core::ResourceType::Agent => &mut self.agents,
            crate::core::ResourceType::Snippet => &mut self.snippets,
            crate::core::ResourceType::Command => &mut self.commands,
            crate::core::ResourceType::McpServer => {
                // MCP servers are handled differently - they don't use LockedResource
                // This shouldn't be called for MCP servers
                return;
            }
            crate::core::ResourceType::Script => &mut self.scripts,
            crate::core::ResourceType::Hook => &mut self.hooks,
            crate::core::ResourceType::Skill => &mut self.skills,
        };

        // Remove existing entry if present
        resources.retain(|r| r.name != name);
        resources.push(resource);
    }

    /// Check if resource exists by name.
    ///
    /// # Arguments
    ///
    /// * `name` - Resource name to check
    ///
    /// # Returns
    ///
    /// * `true` - Resource exists in the lockfile
    /// * `false` - Resource does not exist
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use agpm_cli::lockfile::LockFile;
    /// # let lockfile = LockFile::new();
    /// if lockfile.has_resource("example-agent") {
    ///     println!("Agent is already locked");
    /// } else {
    ///     println!("Agent needs to be resolved and installed");
    /// }
    /// ```
    ///
    /// This is equivalent to calling `lockfile.get_resource(name).is_some()`.
    #[must_use]
    pub fn has_resource(&self, name: &str) -> bool {
        self.get_resource(name).is_some()
    }

    /// Internal name-based lookup across all types.
    ///
    /// Returns first match. External callers should use `find_resource_by_id` for proper lookup.
    #[must_use]
    pub(crate) fn get_resource(&self, name: &str) -> Option<&LockedResource> {
        // Simple name matching - may return first of multiple resources with same name
        // For precise matching when duplicates exist, use find_resource_by_id()
        self.agents
            .iter()
            .find(|r| r.name == name)
            .or_else(|| self.snippets.iter().find(|r| r.name == name))
            .or_else(|| self.commands.iter().find(|r| r.name == name))
            .or_else(|| self.scripts.iter().find(|r| r.name == name))
            .or_else(|| self.hooks.iter().find(|r| r.name == name))
            .or_else(|| self.mcp_servers.iter().find(|r| r.name == name))
    }

    /// Get resources by type as slice.
    pub fn get_resources(&self, resource_type: &crate::core::ResourceType) -> &[LockedResource] {
        use crate::core::ResourceType;
        match resource_type {
            ResourceType::Agent => &self.agents,
            ResourceType::Snippet => &self.snippets,
            ResourceType::Command => &self.commands,
            ResourceType::Script => &self.scripts,
            ResourceType::Hook => &self.hooks,
            ResourceType::McpServer => &self.mcp_servers,
            ResourceType::Skill => &self.skills,
        }
    }

    /// Get mutable resources by type.
    pub fn get_resources_mut(
        &mut self,
        resource_type: &crate::core::ResourceType,
    ) -> &mut Vec<LockedResource> {
        use crate::core::ResourceType;
        match resource_type {
            ResourceType::Agent => &mut self.agents,
            ResourceType::Snippet => &mut self.snippets,
            ResourceType::Command => &mut self.commands,
            ResourceType::Script => &mut self.scripts,
            ResourceType::Hook => &mut self.hooks,
            ResourceType::McpServer => &mut self.mcp_servers,
            ResourceType::Skill => &mut self.skills,
        }
    }

    /// Collect all resources across all types.
    ///
    /// Useful for operations processing resources uniformly:
    /// - Installation reports
    /// - Checksum validation
    /// - Bulk operations
    ///
    /// # Returns
    ///
    /// A vector containing references to all [`LockedResource`] entries in the lockfile.
    /// The order matches the resource type order defined in [`crate::core::ResourceType::all()`].
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use agpm_cli::lockfile::LockFile;
    /// # let lockfile = LockFile::new();
    /// let all_resources = lockfile.all_resources();
    /// println!("Total locked resources: {}", all_resources.len());
    ///
    /// for resource in all_resources {
    ///     println!("- {}: {}", resource.name, resource.installed_at);
    /// }
    /// ```
    #[must_use]
    pub fn all_resources(&self) -> Vec<&LockedResource> {
        let mut resources = Vec::new();

        // Use ResourceType::all() to iterate through all resource types
        for resource_type in crate::core::ResourceType::all() {
            resources.extend(self.get_resources(resource_type));
        }

        resources
    }

    /// Clear all entries, returning lockfile to empty state.
    ///
    /// Format version unchanged.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use agpm_cli::lockfile::LockFile;
    /// let mut lockfile = LockFile::new();
    /// // ... add sources and resources ...
    ///
    /// lockfile.clear();
    /// assert!(lockfile.sources.is_empty());
    /// assert!(lockfile.agents.is_empty());
    /// assert!(lockfile.snippets.is_empty());
    /// ```
    ///
    /// # Use Cases
    ///
    /// - Preparing for complete lockfile regeneration
    /// - Implementing `agpm clean` functionality
    /// - Resetting lockfile state during testing
    /// - Handling lockfile corruption recovery
    pub fn clear(&mut self) {
        self.sources.clear();

        // Use ResourceType::all() to clear all resource types
        for resource_type in crate::core::ResourceType::all() {
            self.get_resources_mut(resource_type).clear();
        }
    }

    /// Find resource by name within specific type.
    ///
    /// More precise than `get_resource` when type is known.
    ///
    /// # Arguments
    ///
    /// * `name` - Resource name to search for
    /// * `resource_type` - The type of resource to search within
    ///
    /// # Returns
    ///
    /// * `Some(&LockedResource)` - Reference to the found resource
    /// * `None` - No resource with that name exists in the specified type
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use agpm_cli::lockfile::LockFile;
    /// # use agpm_cli::core::ResourceType;
    /// # let lockfile = LockFile::new();
    /// // Find a specific agent
    /// if let Some(agent) = lockfile.find_resource("helper", &ResourceType::Agent) {
    ///     println!("Found agent: {}", agent.installed_at);
    /// }
    ///
    /// // Find a specific snippet
    /// if let Some(snippet) = lockfile.find_resource("utils", &ResourceType::Snippet) {
    ///     println!("Found snippet: {}", snippet.installed_at);
    /// }
    /// ```
    ///
    /// **Note**: External callers should prefer `find_resource_by_id(&ResourceId)` for ResourceId-based lookup.
    #[must_use]
    pub fn find_resource(
        &self,
        name: &str,
        resource_type: &crate::core::ResourceType,
    ) -> Option<&LockedResource> {
        self.get_resources(resource_type).iter().find(|r| r.name == name)
    }

    /// Find resource by complete ResourceId (canonical lookup method).
    ///
    /// Checks all identity fields: name, source, tool, template_vars.
    ///
    /// # Arguments
    ///
    /// * `id` - The complete ResourceId to search for
    ///
    /// # Returns
    ///
    /// * `Some(&LockedResource)` - Reference to the matching resource
    /// * `None` - No resource with that exact ID exists
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use agpm_cli::lockfile::{LockFile, ResourceId};
    /// # use agpm_cli::core::ResourceType;
    /// # use agpm_cli::utils::compute_variant_inputs_hash;
    /// # use serde_json::json;
    /// # let lockfile = LockFile::new();
    /// // Find resource with specific template_vars
    /// let template_vars = json!({"project": {"language": "python"}});
    /// let variant_hash = compute_variant_inputs_hash(&template_vars).unwrap_or_default();
    /// let id = ResourceId::new(
    ///     "backend-engineer",
    ///     Some("community"),
    ///     Some("claude-code"),
    ///     ResourceType::Agent,
    ///     variant_hash
    /// );
    ///
    /// if let Some(resource) = lockfile.find_resource_by_id(&id) {
    ///     println!("Found: {}", resource.installed_at);
    /// }
    /// ```
    #[must_use]
    pub fn find_resource_by_id(&self, id: &ResourceId) -> Option<&LockedResource> {
        // Search all resource types for exact ResourceId match
        self.agents
            .iter()
            .find(|r| r.matches_id(id))
            .or_else(|| self.snippets.iter().find(|r| r.matches_id(id)))
            .or_else(|| self.commands.iter().find(|r| r.matches_id(id)))
            .or_else(|| self.scripts.iter().find(|r| r.matches_id(id)))
            .or_else(|| self.hooks.iter().find(|r| r.matches_id(id)))
            .or_else(|| self.mcp_servers.iter().find(|r| r.matches_id(id)))
    }

    /// Find mutable resource by ResourceId.
    ///
    /// Use for modifications (checksums, patches, etc.).
    ///
    /// # Arguments
    ///
    /// * `id` - The complete ResourceId to search for
    ///
    /// # Returns
    ///
    /// * `Some(&mut LockedResource)` - Mutable reference to the matching resource
    /// * `None` - No resource with that exact ID exists
    #[must_use]
    pub fn find_resource_by_id_mut(&mut self, id: &ResourceId) -> Option<&mut LockedResource> {
        // Search all resource types for exact ResourceId match (mutable)
        if let Some(r) = self.agents.iter_mut().find(|r| r.matches_id(id)) {
            return Some(r);
        }
        if let Some(r) = self.snippets.iter_mut().find(|r| r.matches_id(id)) {
            return Some(r);
        }
        if let Some(r) = self.commands.iter_mut().find(|r| r.matches_id(id)) {
            return Some(r);
        }
        if let Some(r) = self.scripts.iter_mut().find(|r| r.matches_id(id)) {
            return Some(r);
        }
        if let Some(r) = self.hooks.iter_mut().find(|r| r.matches_id(id)) {
            return Some(r);
        }
        self.mcp_servers.iter_mut().find(|r| r.matches_id(id))
    }

    /// Get all resources by type for templating.
    ///
    /// # Arguments
    ///
    /// * `resource_type` - The type of resources to retrieve
    ///
    /// # Returns
    ///
    /// A slice of all resources of the specified type.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use agpm_cli::lockfile::LockFile;
    /// # use agpm_cli::core::ResourceType;
    /// # let lockfile = LockFile::new();
    /// // Get all agents for templating
    /// let agents = lockfile.get_resources_by_type(&ResourceType::Agent);
    /// for agent in agents {
    ///     println!("Agent: {} -> {}", agent.name, agent.installed_at);
    /// }
    ///
    /// // Get all snippets for templating
    /// let snippets = lockfile.get_resources_by_type(&ResourceType::Snippet);
    /// println!("Found {} snippets", snippets.len());
    /// ```
    ///
    /// # See Also
    ///
    /// * [`get_resources`](Self::get_resources) - Get resources by type (same method)
    /// * [`all_resources`](Self::all_resources) - Get all resources across all types
    #[must_use]
    pub fn get_resources_by_type(
        &self,
        resource_type: &crate::core::ResourceType,
    ) -> &[LockedResource] {
        self.get_resources(resource_type)
    }
}