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
//! Automatic version backtracking for SHA conflict resolution.
//!
//! This module implements automatic resolution of version conflicts by finding
//! alternative versions that satisfy all constraints and resolve to the same commit SHA.
//!
//! # Algorithm
//!
//! When SHA conflicts are detected (multiple requirements for the same resource resolving
//! to different commits), the backtracking resolver attempts to find compatible versions:
//!
//! 1. **Query available versions**: Fetch all tags from the Git repository
//! 2. **Filter by constraints**: Find versions satisfying all requirements
//! 3. **Try alternatives**: Test versions in preference order (latest first)
//! 4. **Verify SHA match**: Check if alternative version resolves to same SHA as other requirements
//! 5. **Handle transitive deps**: Re-resolve transitive dependencies after version changes
//! 6. **Iterate if needed**: Continue until all conflicts resolved or limits reached
//!
//! # Performance Limits
//!
//! To prevent excessive computation:
//! - Maximum 100 version resolution attempts per conflict
//! - 10-second timeout for entire backtracking process
//! - Early termination if no progress made

mod algorithm;
mod registry;
mod transitive;
mod types;

use anyhow::Result;
use std::collections::HashMap;
use std::time::{Duration, Instant};

use crate::resolver::ResolutionCore;
use crate::resolver::version_resolver::VersionResolutionService;
use crate::version::conflict::{ConflictDetector, VersionConflict};

pub use registry::{ResourceParams, parse_resource_id_string, resource_id_to_string};
pub use types::{BacktrackingIteration, BacktrackingResult, TerminationReason, VersionUpdate};

use registry::{ResourceRegistry, TransitiveChangeTracker};

/// Maximum number of version resolution attempts before giving up
const MAX_ATTEMPTS: usize = 100;

/// Maximum duration for backtracking before timeout (increased for transitive resolution)
const MAX_DURATION: Duration = Duration::from_secs(10);

/// Maximum number of backtracking iterations before giving up
const MAX_ITERATIONS: usize = 10;

/// Automatic version backtracking resolver.
///
/// Attempts to resolve SHA conflicts by finding alternative versions
/// that satisfy all constraints and resolve to the same commit.
pub struct BacktrackingResolver<'a> {
    /// Core resolution context with manifest, cache, and source manager
    core: &'a ResolutionCore,

    /// Version resolution service for Git operations
    version_service: &'a mut VersionResolutionService,

    /// Maximum version resolution attempts
    max_attempts: usize,

    /// Maximum duration before timeout
    timeout: Duration,

    /// Start time for timeout tracking
    start_time: Instant,

    /// Number of attempts made so far
    attempts: usize,

    /// Tracks resources whose versions changed (need transitive re-resolution)
    change_tracker: TransitiveChangeTracker,

    /// Iteration history for debugging and oscillation detection
    iteration_history: Vec<BacktrackingIteration>,

    /// Maximum iterations before giving up
    max_iterations: usize,

    /// Registry of all resources for conflict detection after version changes
    resource_registry: ResourceRegistry,
}

impl<'a> BacktrackingResolver<'a> {
    /// Create a new backtracking resolver with default limits.
    pub fn new(
        core: &'a ResolutionCore,
        version_service: &'a mut VersionResolutionService,
    ) -> Self {
        Self {
            core,
            version_service,
            max_attempts: MAX_ATTEMPTS,
            timeout: MAX_DURATION,
            start_time: Instant::now(),
            attempts: 0,
            change_tracker: TransitiveChangeTracker::new(),
            iteration_history: Vec::new(),
            max_iterations: MAX_ITERATIONS,
            resource_registry: ResourceRegistry::new(),
        }
    }

    /// Populate the resource registry from a ConflictDetector.
    ///
    /// This extracts all requirements from the conflict detector and builds
    /// a complete resource registry for conflict detection during backtracking.
    pub fn populate_from_conflict_detector(&mut self, conflict_detector: &ConflictDetector) {
        let requirements = conflict_detector.requirements();

        let mut skipped_count = 0;
        let mut processed_count = 0;

        for (resource_id, reqs) in requirements {
            if resource_id_to_string(resource_id).is_err() {
                let tool_info =
                    resource_id.tool().map(|t| format!("tool: {}", t)).unwrap_or_default();
                let type_info = format!("type: {}", resource_id.resource_type());

                tracing::warn!(
                    "Skipping resource without source: {} (name: {}, {}, {} requirements: {})",
                    resource_id,
                    resource_id.name(),
                    type_info,
                    tool_info,
                    reqs.len()
                );

                skipped_count += 1;
                continue;
            }

            processed_count += 1;

            for req in reqs {
                self.resource_registry.add_or_update_resource(ResourceParams {
                    resource_id: resource_id.clone(),
                    version: req.requirement.clone(),
                    sha: req.resolved_sha.clone(),
                    version_constraint: req.requirement.clone(),
                    required_by: req.required_by.clone(),
                });
            }
        }

        if skipped_count > 0 {
            tracing::info!(
                "Population complete: processed {} resources, skipped {} without source",
                processed_count,
                skipped_count
            );
        } else {
            tracing::debug!(
                "Population complete: processed {} resources, no local resources skipped",
                processed_count
            );
        }
    }

    /// Create a backtracking resolver with custom limits (for testing).
    #[allow(dead_code)]
    pub fn with_limits(
        core: &'a ResolutionCore,
        version_service: &'a mut VersionResolutionService,
        max_attempts: usize,
        timeout: Duration,
    ) -> Self {
        Self {
            core,
            version_service,
            max_attempts,
            timeout,
            start_time: Instant::now(),
            attempts: 0,
            change_tracker: TransitiveChangeTracker::new(),
            iteration_history: Vec::new(),
            max_iterations: MAX_ITERATIONS,
            resource_registry: ResourceRegistry::new(),
        }
    }

    /// Attempt to resolve conflicts by finding compatible versions.
    pub async fn resolve_conflicts(
        &mut self,
        initial_conflicts: &[VersionConflict],
    ) -> Result<BacktrackingResult> {
        tracing::debug!(
            "Starting iterative backtracking for {} conflict(s), limits: {} iterations, {} attempts, {}s timeout",
            initial_conflicts.len(),
            self.max_iterations,
            self.max_attempts,
            self.timeout.as_secs()
        );

        let mut current_conflicts = initial_conflicts.to_vec();
        let mut all_updates = Vec::new();
        let mut total_transitive = 0;

        for iteration_num in 1..=self.max_iterations {
            tracing::debug!("=== Backtracking iteration {} ===", iteration_num);
            tracing::debug!("Processing {} conflict(s)", current_conflicts.len());

            if self.start_time.elapsed() > self.timeout {
                tracing::warn!("Backtracking timeout after {:?}", self.start_time.elapsed());
                return Ok(self.build_result(
                    false,
                    all_updates,
                    total_transitive,
                    TerminationReason::Timeout,
                ));
            }

            let mut iteration_updates = Vec::new();
            for conflict in &current_conflicts {
                match self.resolve_single_conflict(conflict).await? {
                    Some(update) => {
                        tracing::debug!(
                            "Resolved conflict for {}: {} → {}",
                            conflict.resource,
                            update.old_version,
                            update.new_version
                        );
                        iteration_updates.push(update);
                    }
                    None => {
                        tracing::debug!("Could not resolve conflict for {}", conflict.resource);
                        return Ok(self.build_result(
                            false,
                            all_updates,
                            total_transitive,
                            TerminationReason::NoCompatibleVersion,
                        ));
                    }
                }
            }

            if iteration_updates.is_empty() {
                tracing::debug!("No updates found in iteration {}", iteration_num);
                return Ok(self.build_result(
                    false,
                    all_updates,
                    total_transitive,
                    TerminationReason::NoCompatibleVersion,
                ));
            }

            for update in &iteration_updates {
                self.change_tracker.record_change(
                    &update.resource_id,
                    &update.old_version,
                    &update.new_version,
                    &update.new_sha,
                    update.variant_inputs.clone(),
                );

                self.resource_registry.update_version_and_sha(
                    &update.resource_id,
                    update.new_version.clone(),
                    update.new_sha.clone(),
                );
            }

            all_updates.extend(iteration_updates.clone());

            tracing::debug!(
                "Re-extracting transitive deps for {} changed resource(s)",
                self.change_tracker.get_changed_resources().len()
            );
            let transitive_count = transitive::reextract_transitive_deps(
                self.core,
                self.version_service,
                &mut self.change_tracker,
            )
            .await?;
            total_transitive += transitive_count;

            if transitive_count > 0 {
                tracing::debug!("Re-resolved {} transitive dependency(ies)", transitive_count);
            }

            let new_conflicts = transitive::detect_conflicts_after_changes(&self.resource_registry);
            tracing::debug!(
                "After iteration {}: {} conflict(s) remaining",
                iteration_num,
                new_conflicts.len()
            );

            self.iteration_history.push(BacktrackingIteration {
                iteration: iteration_num,
                conflicts: current_conflicts.clone(),
                updates: iteration_updates,
                transitive_reresolutions: transitive_count,
                made_progress: !new_conflicts.is_empty() || transitive_count > 0,
            });

            if new_conflicts.is_empty() {
                tracing::info!(
                    "✓ Resolved all conflicts after {} iteration(s), {} version update(s), {} transitive re-resolution(s)",
                    iteration_num,
                    all_updates.len(),
                    total_transitive
                );
                return Ok(self.build_result(
                    true,
                    all_updates,
                    total_transitive,
                    TerminationReason::Success,
                ));
            }

            if algorithm::conflicts_equal(&current_conflicts, &new_conflicts) {
                tracing::warn!(
                    "No progress made in iteration {}: same conflicts remain",
                    iteration_num
                );
                return Ok(self.build_result(
                    false,
                    all_updates,
                    total_transitive,
                    TerminationReason::NoProgress,
                ));
            }

            if self.detect_oscillation(&new_conflicts) {
                tracing::warn!("Oscillation detected in iteration {}", iteration_num);
                return Ok(self.build_result(
                    false,
                    all_updates,
                    total_transitive,
                    TerminationReason::Oscillation,
                ));
            }

            current_conflicts = new_conflicts;
        }

        tracing::warn!(
            "Reached max iterations ({}) without resolving all conflicts. {} conflict(s) remaining",
            self.max_iterations,
            current_conflicts.len()
        );
        Ok(self.build_result(
            false,
            all_updates,
            total_transitive,
            TerminationReason::MaxIterations,
        ))
    }

    /// Resolve a single conflict by finding an alternative version.
    async fn resolve_single_conflict(
        &mut self,
        conflict: &VersionConflict,
    ) -> Result<Option<VersionUpdate>> {
        let source_name = conflict
            .resource
            .source()
            .ok_or_else(|| anyhow::anyhow!("Resource {} has no source", conflict.resource))?;

        let mut sha_groups: HashMap<&str, Vec<&crate::version::conflict::ConflictingRequirement>> =
            HashMap::new();
        for req in &conflict.conflicting_requirements {
            sha_groups.entry(req.resolved_sha.as_str()).or_default().push(req);
        }

        let target_sha = algorithm::select_target_sha(&sha_groups)?;

        tracing::debug!(
            "Target SHA for {}: {} ({} requirements)",
            conflict.resource,
            &target_sha[..8.min(target_sha.len())],
            sha_groups.get(target_sha).map_or(0, |v| v.len())
        );

        let requirements_to_update: Vec<&crate::version::conflict::ConflictingRequirement> =
            conflict
                .conflicting_requirements
                .iter()
                .filter(|req| req.resolved_sha != target_sha)
                .collect();

        if requirements_to_update.is_empty() {
            return Ok(None);
        }

        let req_to_update = requirements_to_update[0];

        if req_to_update.required_by == "manifest" {
            algorithm::find_alternative_for_direct_dependency(
                self.core,
                self.version_service,
                source_name,
                req_to_update,
                target_sha,
                &mut self.attempts,
                self.max_attempts,
                self.start_time,
                self.timeout,
            )
            .await
        } else {
            algorithm::find_alternative_for_transitive(
                self.core,
                self.version_service,
                source_name,
                req_to_update,
                target_sha,
                &mut self.attempts,
                self.max_attempts,
                self.start_time,
                self.timeout,
            )
            .await
        }
    }

    /// Detect if we're oscillating between two conflict states.
    fn detect_oscillation(&self, current_conflicts: &[VersionConflict]) -> bool {
        for iteration in &self.iteration_history {
            if algorithm::conflicts_equal(&iteration.conflicts, current_conflicts) {
                tracing::warn!(
                    "Oscillation detected: conflicts match iteration {}",
                    iteration.iteration
                );
                return true;
            }
        }
        false
    }

    fn build_result(
        &self,
        resolved: bool,
        updates: Vec<VersionUpdate>,
        total_transitive: usize,
        termination_reason: TerminationReason,
    ) -> BacktrackingResult {
        BacktrackingResult {
            resolved,
            updates,
            iterations: self.iteration_history.len(),
            attempted_versions: self.attempts,
            iteration_history: self.iteration_history.clone(),
            total_transitive_reresolutions: total_transitive,
            termination_reason,
        }
    }
}