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
// Basic parallel processing tests for transitive dependency resolution
//
// Tests core parallel processing features:
// - DashMap-based concurrent data structures
// - Basic concurrent transitive resolution
// - Concurrent access to shared dependencies
// - Pattern expansion under concurrent load

use anyhow::Result;
use std::time::Instant;

use crate::common::{ManifestBuilder, TestProject};

/// Test basic parallel transitive resolution with 20+ dependencies
#[tokio::test]
async fn test_parallel_transitive_resolution() -> Result<()> {
    agpm_cli::test_utils::init_test_logging(None);

    let project = TestProject::new().await?;

    // Create source repo with multiple agent chains
    let community_repo = project.create_source_repo("community").await?;

    // Add base helper agents (no dependencies)
    for i in 0..10 {
        community_repo
            .add_resource(
                "agents",
                &format!("helper-{:02}", i),
                format!(
                    r#"---
# Helper Agent {:02}
This is helper agent {} with no dependencies.
---
"#,
                    i, i
                )
                .as_str(),
            )
            .await?;
    }

    // Add main agents that depend on multiple helpers
    for i in 0..15 {
        let mut dependencies = String::from("dependencies:\n  agents:\n");
        // Each main agent depends on 2-3 helper agents
        let dep_count = 2 + (i % 2);
        for j in 0..dep_count {
            let helper_idx = (i * 2 + j) % 10;
            dependencies.push_str(&format!(
                "    - path: ./helper-{:02}.md\n      version: v1.0.0\n",
                helper_idx
            ));
        }

        community_repo
            .add_resource(
                "agents",
                &format!("main-{:02}", i),
                format!(
                    r#"---
{}
---

# Main Agent {:02}
This agent depends on {} helper agents.
"#,
                    dependencies, i, dep_count
                )
                .as_str(),
            )
            .await?;
    }

    community_repo.commit_all("Initial commit")?;
    community_repo.tag_version("v1.0.0")?;

    // Create manifest with all main agents (should pull in transitive helpers)
    let source_url = community_repo.bare_file_url(project.sources_path()).await?;
    let mut builder = ManifestBuilder::new().add_source("community", &source_url);

    // Add all 15 main agents to the manifest
    for i in 0..15 {
        builder = builder.add_standard_agent(
            &format!("main-{:02}", i),
            "community",
            &format!("agents/main-{:02}.md", i),
        );
    }

    let manifest = builder.build();
    project.write_manifest(&manifest).await?;

    // Measure install time for performance verification
    let start_time = Instant::now();

    // Run install
    let output = project.run_agpm(&["install"])?;
    assert!(output.success, "Install should succeed. Stderr: {}", output.stderr);

    let install_duration = start_time.elapsed();

    // Verify all agents were installed (15 main + at least some helpers)
    let lockfile_content = project.read_lockfile().await?;

    // All main agents should be in lockfile
    for i in 0..15 {
        assert!(
            lockfile_content.contains(&format!("main-{:02}", i)),
            "Main agent {:02} should be in lockfile",
            i
        );
    }

    // At least some helper agents should be installed as transitive deps
    let mut helper_count = 0;
    for i in 0..10 {
        if lockfile_content.contains(&format!("helper-{:02}", i)) {
            helper_count += 1;
        }
    }
    assert!(
        helper_count > 0,
        "At least some helper agents should be in lockfile as transitive dependencies"
    );

    // Verify files were actually installed
    let agents_dir = project.project_path().join(".claude/agents/agpm");
    let mut agent_count = 0;
    let mut entries = tokio::fs::read_dir(&agents_dir).await?;
    while let Some(_entry) = entries.next_entry().await? {
        agent_count += 1;
    }

    assert!(agent_count >= 15, "At least 15 agents should be installed (got {})", agent_count);

    // Log performance metrics for verification
    println!(
        "Parallel install of {} agents completed in {:?} ({:.2} agents/sec)",
        agent_count,
        install_duration,
        agent_count as f64 / install_duration.as_secs_f64()
    );

    Ok(())
}

/// Test concurrent access to the same dependency keys
#[tokio::test]
async fn test_concurrent_dependency_access() -> Result<()> {
    agpm_cli::test_utils::init_test_logging(None);

    let project = TestProject::new().await?;

    // Create source repo with a shared dependency
    let community_repo = project.create_source_repo("community").await?;

    // Add a single shared helper that many agents will depend on
    community_repo
        .add_resource(
            "agents",
            "shared-helper",
            r#"---
# Shared Helper Agent
This helper is depended upon by many agents.
---
"#,
        )
        .await?;

    // Add multiple agents that all depend on the same helper
    for i in 0..20 {
        community_repo
            .add_resource(
                "agents",
                &format!("agent-{:02}", i),
                format!(
                    r#"---
dependencies:
  agents:
    - path: ./shared-helper.md
      version: v1.0.0
---

# Agent {:02}
This agent depends on the shared helper.
"#,
                    i
                )
                .as_str(),
            )
            .await?;
    }

    community_repo.commit_all("Initial commit")?;
    community_repo.tag_version("v1.0.0")?;

    // Create manifest with all agents
    let source_url = community_repo.bare_file_url(project.sources_path()).await?;
    let mut builder = ManifestBuilder::new().add_source("community", &source_url);

    for i in 0..20 {
        builder = builder.add_standard_agent(
            &format!("agent-{:02}", i),
            "community",
            &format!("agents/agent-{:02}.md", i),
        );
    }

    let manifest = builder.build();
    project.write_manifest(&manifest).await?;

    // Run install (this will test concurrent access to shared-helper)
    let output = project.run_agpm(&["install"])?;
    assert!(output.success, "Install should succeed. Stderr: {}", output.stderr);

    // Verify shared helper was only installed once (deduplication)
    // For transitive dependencies, let's verify that the helper file was installed only once
    let agents_dir = project.project_path().join(".claude/agents/agpm");
    let shared_helper_path = agents_dir.join("shared-helper.md");
    assert!(
        tokio::fs::metadata(&shared_helper_path).await.is_ok(),
        "Shared helper should be installed"
    );

    // Also verify that agents referencing it were installed
    let mut agent_count = 0;
    let mut entries = tokio::fs::read_dir(&agents_dir).await?;
    while let Some(entry) = entries.next_entry().await? {
        let file_name = entry.file_name();
        let name = file_name.to_string_lossy();
        if name.starts_with("agent-") {
            agent_count += 1;
        }
    }
    assert_eq!(agent_count, 20, "All 20 agents should be installed");

    // Check all agents exist
    for i in 0..20 {
        let agent_path = agents_dir.join(format!("agent-{:02}.md", i));
        assert!(
            tokio::fs::metadata(&agent_path).await.is_ok(),
            "Agent {:02} should be installed",
            i
        );
    }

    Ok(())
}

/// Test concurrent pattern expansion and alias mapping
#[tokio::test]
async fn test_concurrent_pattern_expansion() -> Result<()> {
    agpm_cli::test_utils::init_test_logging(None);

    let project = TestProject::new().await?;

    // Create source repo with pattern-based resources
    let community_repo = project.create_source_repo("community").await?;

    // Add a set of utility agents as a pattern
    for i in 0..12 {
        community_repo
            .add_resource(
                "agents",
                &format!("utils/utility-{:02}", i),
                format!(
                    r#"---
# Utility Agent {:02}
This is a utility agent in the utils directory.
---
"#,
                    i
                )
                .as_str(),
            )
            .await?;
    }

    // Add some main agents that depend on patterns
    for i in 0..5 {
        community_repo
            .add_resource(
                "agents",
                &format!("pattern-agent-{:02}", i),
                r#"---
dependencies:
  agents:
    - path: ./utils/utility-*.md
      version: v1.0.0
---

# Pattern Agent
This agent depends on all utility agents via pattern.
"#,
            )
            .await?;
    }

    community_repo.commit_all("Initial commit")?;
    community_repo.tag_version("v1.0.0")?;

    // Create manifest with pattern dependencies
    let source_url = community_repo.bare_file_url(project.sources_path()).await?;
    let manifest = ManifestBuilder::new()
        .add_source("community", &source_url)
        .add_agent_pattern("all-utilities", "community", "agents/utils/utility-*.md", "v1.0.0")
        .add_agent_pattern("pattern-agents", "community", "agents/pattern-agent-*.md", "v1.0.0")
        .build();

    project.write_manifest(&manifest).await?;

    // Run install
    let output = project.run_agpm(&["install"])?;
    assert!(output.success, "Install should succeed. Stderr: {}", output.stderr);

    // Verify all utility agents were installed via pattern
    let agents_dir = project.project_path().join(".claude/agents/agpm");

    // Check utility agents
    for i in 0..12 {
        let utility_path = agents_dir.join(format!("utility-{:02}.md", i));
        assert!(
            tokio::fs::metadata(&utility_path).await.is_ok(),
            "Utility agent {:02} should be installed via pattern",
            i
        );
    }

    // Check pattern agents
    for i in 0..5 {
        let agent_path = agents_dir.join(format!("pattern-agent-{:02}.md", i));
        assert!(
            tokio::fs::metadata(&agent_path).await.is_ok(),
            "Pattern agent {:02} should be installed",
            i
        );
    }

    Ok(())
}

/// Test parallel batch size calculation (max(10, 2×CPU cores))
#[tokio::test]
async fn test_parallel_batch_calculation() -> Result<()> {
    agpm_cli::test_utils::init_test_logging(None);

    let project = TestProject::new().await?;

    // Create source repo
    let community_repo = project.create_source_repo("community").await?;

    // Add a modest number of dependencies to test batch processing
    for i in 0..25 {
        community_repo
            .add_resource(
                "agents",
                &format!("batch-test-{:02}", i),
                r#"---
name: "Batch Test Agent"
---
# Batch Test Agent

This agent is part of batch processing tests.
"#,
            )
            .await?;
    }

    community_repo.commit_all("Initial commit")?;
    community_repo.tag_version("v1.0.0")?;

    // Create manifest with all agents
    let source_url = community_repo.bare_file_url(project.sources_path()).await?;
    let mut builder = ManifestBuilder::new().add_source("community", &source_url);

    for i in 0..25 {
        builder = builder.add_standard_agent(
            &format!("batch-test-{:02}", i),
            "community",
            &format!("agents/batch-test-{:02}.md", i),
        );
    }

    let manifest = builder.build();
    project.write_manifest(&manifest).await?;

    // Run install with verbose output to capture batch processing info
    let output = project.run_agpm(&["install", "--verbose"])?;
    assert!(output.success, "Install should succeed. Stderr: {}", output.stderr);

    // Verify all agents were installed
    let lockfile_content = project.read_lockfile().await?;
    for i in 0..25 {
        assert!(
            lockfile_content.contains(&format!("batch-test-{:02}", i)),
            "Batch test agent {:02} should be in lockfile",
            i
        );
    }

    // For batch calculation test, we just need to verify all agents were installed
    // The verbose output may vary depending on the environment and logging configuration
    // The fact that installation succeeded and all agents are in lockfile is sufficient

    Ok(())
}

/// Test concurrent shared dependencies with deduplication
#[tokio::test]
async fn test_concurrent_shared_dependencies() -> Result<()> {
    agpm_cli::test_utils::init_test_logging(None);

    let project = TestProject::new().await?;

    // Create source repo with shared dependencies
    let community_repo = project.create_source_repo("community").await?;

    // Add shared utilities
    for i in 0..5 {
        community_repo
            .add_resource(
                "agents",
                &format!("shared-util-{:02}", i),
                format!(
                    r#"---
# Shared Utility {:02}
This is a shared utility used by many agents.
---
"#,
                    i
                )
                .as_str(),
            )
            .await?;
    }

    // Add agents that share utilities
    for i in 0..15 {
        community_repo
            .add_resource(
                "agents",
                &format!("shared-agent-{:02}", i),
                format!(
                    r#"---
dependencies:
  agents:
    - path: shared-util-{:02}.md
      version: v1.0.0
    - path: shared-util-{:02}.md
      version: v1.0.0
---

# Shared Agent {:02}
This agent uses shared utilities.
"#,
                    i % 5,
                    (i + 1) % 5,
                    i
                )
                .as_str(),
            )
            .await?;
    }

    community_repo.commit_all("Initial commit")?;
    community_repo.tag_version("v1.0.0")?;

    // Create manifest
    let source_url = community_repo.bare_file_url(project.sources_path()).await?;
    let mut builder = ManifestBuilder::new().add_source("community", &source_url);

    for i in 0..15 {
        builder = builder.add_standard_agent(
            &format!("shared-agent-{:02}", i),
            "community",
            &format!("agents/shared-agent-{:02}.md", i),
        );
    }

    let manifest = builder.build();
    project.write_manifest(&manifest).await?;

    // Run install
    let output = project.run_agpm(&["install"])?;
    assert!(output.success, "Install should succeed. Stderr: {}", output.stderr);

    // Verify all utilities and agents were installed
    let agents_dir = project.project_path().join(".claude/agents/agpm");

    // Check shared utilities
    for i in 0..5 {
        let util_path = agents_dir.join(format!("shared-util-{:02}.md", i));
        assert!(
            tokio::fs::metadata(&util_path).await.is_ok(),
            "Shared utility {:02} should be installed",
            i
        );
    }

    // Check agents
    for i in 0..15 {
        let agent_path = agents_dir.join(format!("shared-agent-{:02}.md", i));
        assert!(
            tokio::fs::metadata(&agent_path).await.is_ok(),
            "Shared agent {:02} should be installed",
            i
        );
    }

    Ok(())
}