agit 1.3.0

AI-native Git wrapper for capturing context alongside code
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! Semantic conflict reconciliation for Agit.
//!
//! This module provides bidirectional reconciliation between Git and Agit:
//!
//! **Forward reconciliation (Ghost Commits)**:
//! Detects external git commits that happened while Agit had pending thoughts.
//! Checks for semantic conflicts where ghost commits modified files mentioned
//! in pending thoughts.
//!
//! **Backward reconciliation (Rewind/Dangling Head)**:
//! Detects when Git has "rewound" (e.g., `git reset --hard`) and Agit's HEAD
//! points to a neural commit attached to a git commit that no longer exists.
//! Snaps Agit back to a valid ancestor.

use crate::domain::{IndexEntry, WrappedNeuralCommit};
use crate::error::Result;
use crate::git::GitRepository;
use crate::storage::{ObjectStore, RefStore};

/// Information about ghost commits (external git commits Agit missed).
#[derive(Debug, Clone)]
pub struct GhostCommitInfo {
    /// The git commit hashes of external commits.
    pub git_hashes: Vec<String>,
    /// All files changed across all ghost commits.
    pub changed_files: Vec<String>,
}

/// Result of checking for semantic conflicts.
#[derive(Debug, Clone)]
pub struct ConflictCheckResult {
    /// Whether a semantic conflict was detected.
    pub has_conflict: bool,
    /// Files that overlap between ghost commits and pending thoughts.
    pub conflicting_files: Vec<String>,
    /// Information about the ghost commits (if any were detected).
    pub ghost_info: Option<GhostCommitInfo>,
}

impl ConflictCheckResult {
    /// Create a result indicating no conflicts.
    pub fn no_conflict() -> Self {
        Self {
            has_conflict: false,
            conflicting_files: Vec::new(),
            ghost_info: None,
        }
    }

    /// Create a result indicating a conflict.
    pub fn conflict(conflicting_files: Vec<String>, ghost_info: GhostCommitInfo) -> Self {
        Self {
            has_conflict: true,
            conflicting_files,
            ghost_info: Some(ghost_info),
        }
    }

    /// Create a result with ghost commits but no conflict.
    pub fn no_conflict_with_ghost(ghost_info: GhostCommitInfo) -> Self {
        Self {
            has_conflict: false,
            conflicting_files: Vec::new(),
            ghost_info: Some(ghost_info),
        }
    }
}

/// Detect ghost commits by comparing Git HEAD with the last known neural commit's git_hash.
///
/// Ghost commits are external git commits that happened while Agit had pending
/// thoughts. This can occur when:
/// - User ran `git commit` directly (bypassing Agit)
/// - User pulled changes from a remote
/// - A teammate pushed changes
///
/// # Arguments
///
/// * `git` - Git repository wrapper
/// * `objects` - Object store for loading neural commits
/// * `refs` - Ref store for looking up branch heads
/// * `branch` - The current branch name
///
/// # Returns
///
/// `Some(GhostCommitInfo)` if ghost commits were detected, `None` if in sync.
pub fn detect_ghost_commits<O: ObjectStore, R: RefStore>(
    git: &GitRepository,
    objects: &O,
    refs: &R,
    branch: &str,
) -> Result<Option<GhostCommitInfo>> {
    // Get current Git HEAD
    let current_git_hash = git.head_commit_hash()?;

    // Get the latest neural commit for this branch
    let neural_hash = match refs.get(branch)? {
        Some(h) => h,
        None => {
            // No neural commits yet - everything is a "ghost commit"
            // This is normal for fresh repos, so we don't treat it as ghost commits
            return Ok(None);
        },
    };

    // Load the neural commit to get its git_hash
    let data = objects.load(&neural_hash)?;
    let wrapped: WrappedNeuralCommit = serde_json::from_slice(&data)?;
    let last_known_git_hash = &wrapped.data.git_hash;

    // If the hashes match, we're in sync
    if &current_git_hash == last_known_git_hash {
        return Ok(None);
    }

    // Ghost commits exist! Get the files changed between them
    let changed_files = git.diff_commits(last_known_git_hash, &current_git_hash)?;

    // Get the commit hashes between them
    let git_hashes = git.commits_between(last_known_git_hash, &current_git_hash)?;

    Ok(Some(GhostCommitInfo {
        git_hashes,
        changed_files,
    }))
}

/// Extract file references from pending index entries using simple substring matching.
///
/// This is a heuristic approach that looks for file-like patterns in the content
/// of index entries. It's intentionally simple for the MVP.
///
/// # Arguments
///
/// * `entries` - The pending index entries to scan
///
/// # Returns
///
/// A vector of potential file references found in the entries.
pub fn extract_file_references(entries: &[IndexEntry]) -> Vec<String> {
    let mut references = Vec::new();

    for entry in entries {
        // Extract potential file references from the content
        // This is a simple heuristic - we look for common file patterns
        extract_file_patterns(&entry.content, &mut references);
    }

    // Deduplicate
    references.sort();
    references.dedup();

    references
}

/// Extract file-like patterns from text content.
fn extract_file_patterns(content: &str, references: &mut Vec<String>) {
    // Split on whitespace and common delimiters
    for word in content.split(|c: char| c.is_whitespace() || c == ',' || c == ';' || c == ':') {
        let word =
            word.trim_matches(|c: char| c == '\'' || c == '"' || c == '`' || c == '(' || c == ')');

        if word.is_empty() {
            continue;
        }

        // Check if this looks like a file path
        if looks_like_file_path(word) {
            // Normalize the path (remove leading ./ or /)
            let normalized = word
                .trim_start_matches("./")
                .trim_start_matches(".\\")
                .trim_start_matches('/')
                .trim_start_matches('\\');

            if !normalized.is_empty() {
                references.push(normalized.to_string());
            }
        }
    }
}

/// Check if a string looks like a file path.
fn looks_like_file_path(s: &str) -> bool {
    // Must contain a file extension or path separator
    let has_extension = s.contains('.') && !s.starts_with('.') && !s.ends_with('.');
    let has_path_sep = s.contains('/') || s.contains('\\');

    // Common file extensions to recognize
    let common_extensions = [
        ".rs", ".ts", ".js", ".tsx", ".jsx", ".py", ".go", ".java", ".c", ".cpp", ".h", ".hpp",
        ".cs", ".rb", ".php", ".swift", ".kt", ".scala", ".md", ".txt", ".json", ".yaml", ".yml",
        ".toml", ".xml", ".html", ".css", ".scss", ".less", ".sql", ".sh", ".bash", ".zsh", ".ps1",
        ".bat", ".cmd",
    ];

    if has_path_sep {
        return true;
    }

    if has_extension {
        // Check if it ends with a common extension
        let lower = s.to_lowercase();
        for ext in &common_extensions {
            if lower.ends_with(ext) {
                return true;
            }
        }
    }

    false
}

/// Check for semantic conflicts between ghost commits and pending thoughts.
///
/// A semantic conflict occurs when:
/// 1. External git commits (ghost commits) modified certain files
/// 2. Pending thoughts mention those same files
///
/// # Arguments
///
/// * `ghost_info` - Information about detected ghost commits
/// * `pending_entries` - The pending index entries
///
/// # Returns
///
/// A `ConflictCheckResult` indicating whether conflicts exist and which files.
pub fn check_semantic_conflict(
    ghost_info: &GhostCommitInfo,
    pending_entries: &[IndexEntry],
) -> ConflictCheckResult {
    if ghost_info.changed_files.is_empty() || pending_entries.is_empty() {
        return ConflictCheckResult::no_conflict_with_ghost(ghost_info.clone());
    }

    // Extract file references from pending thoughts
    let thought_files = extract_file_references(pending_entries);

    if thought_files.is_empty() {
        return ConflictCheckResult::no_conflict_with_ghost(ghost_info.clone());
    }

    // Check for intersection
    let mut conflicting_files = Vec::new();

    for changed_file in &ghost_info.changed_files {
        // Normalize the changed file path for comparison
        let changed_normalized = changed_file
            .replace('\\', "/")
            .trim_start_matches("./")
            .to_string();

        for thought_file in &thought_files {
            let thought_normalized = thought_file
                .replace('\\', "/")
                .trim_start_matches("./")
                .to_string();

            // Check for exact match or substring match (file mentioned in thought)
            let is_match = changed_normalized == thought_normalized
                || changed_normalized.ends_with(&format!("/{}", thought_normalized))
                || thought_normalized.ends_with(&format!("/{}", changed_normalized))
                || changed_normalized.contains(&thought_normalized)
                || thought_normalized.contains(&changed_normalized);

            if is_match && !conflicting_files.contains(changed_file) {
                conflicting_files.push(changed_file.clone());
            }
        }
    }

    if conflicting_files.is_empty() {
        ConflictCheckResult::no_conflict_with_ghost(ghost_info.clone())
    } else {
        ConflictCheckResult::conflict(conflicting_files, ghost_info.clone())
    }
}

/// Full conflict check combining ghost detection and semantic analysis.
///
/// This is the main entry point for conflict checking. It:
/// 1. Detects ghost commits
/// 2. If found, checks for semantic conflicts with pending thoughts
///
/// # Arguments
///
/// * `git` - Git repository wrapper
/// * `objects` - Object store for loading neural commits
/// * `refs` - Ref store for looking up branch heads
/// * `branch` - The current branch name
/// * `pending_entries` - The pending index entries
///
/// # Returns
///
/// A `ConflictCheckResult` with full conflict information.
pub fn check_for_conflicts<O: ObjectStore, R: RefStore>(
    git: &GitRepository,
    objects: &O,
    refs: &R,
    branch: &str,
    pending_entries: &[IndexEntry],
) -> Result<ConflictCheckResult> {
    // First, detect ghost commits
    let ghost_info = match detect_ghost_commits(git, objects, refs, branch)? {
        Some(info) => info,
        None => return Ok(ConflictCheckResult::no_conflict()),
    };

    // Check for semantic conflicts
    Ok(check_semantic_conflict(&ghost_info, pending_entries))
}

// =============================================================================
// Backward Reconciliation (Rewind Detection)
// =============================================================================

/// Maximum time difference (in seconds) to consider two commits as the same for amend detection.
const AMEND_TIMESTAMP_TOLERANCE_SECS: i64 = 60;

/// Detect if a divergence is due to `git commit --amend` rather than a true rewind.
///
/// Amend detection heuristic: If Git HEAD and the neural commit's linked git commit have:
/// - Same author email
/// - Same message first line
/// - Timestamp within 60 seconds
///
/// Then it's likely an amend, not a rewind.
///
/// # Arguments
///
/// * `git` - Git repository wrapper
/// * `old_git_hash` - The git hash that the neural commit is linked to
/// * `new_git_hash` - The current Git HEAD hash
///
/// # Returns
///
/// `true` if this looks like an amend, `false` otherwise.
fn is_amend_replacement(git: &GitRepository, old_git_hash: &str, new_git_hash: &str) -> bool {
    // Try to get metadata for both commits
    let old_meta = match git.get_commit_metadata(old_git_hash) {
        Ok(m) => m,
        Err(_) => return false, // Can't access old commit - likely truly deleted
    };

    let new_meta = match git.get_commit_metadata(new_git_hash) {
        Ok(m) => m,
        Err(_) => return false,
    };

    // Check all three heuristics
    let same_author = old_meta.author_email == new_meta.author_email;
    let same_message = old_meta.message_first_line == new_meta.message_first_line;
    let timestamp_close =
        (old_meta.timestamp - new_meta.timestamp).abs() <= AMEND_TIMESTAMP_TOLERANCE_SECS;

    same_author && same_message && timestamp_close
}

/// Migrate a neural commit to a new git hash (for amend support).
///
/// This creates a new neural commit object with the updated git_hash,
/// stores it, and updates the branch ref to point to it.
///
/// # Arguments
///
/// * `objects` - Object store for loading/saving neural commits
/// * `refs` - Ref store for updating branch heads
/// * `branch` - The current branch name
/// * `neural_hash` - The current neural commit hash
/// * `new_git_hash` - The new git hash to point to
///
/// # Returns
///
/// The new neural commit hash after migration.
fn migrate_neural_commit<O: ObjectStore, R: RefStore>(
    objects: &O,
    refs: &R,
    branch: &str,
    neural_hash: &str,
    new_git_hash: &str,
) -> Result<String> {
    // Load the existing neural commit
    let data = objects.load(neural_hash)?;
    let mut wrapped: WrappedNeuralCommit = serde_json::from_slice(&data)?;

    // Update the git_hash
    wrapped.data.git_hash = new_git_hash.to_string();

    // Re-serialize and save as new object
    let new_data = serde_json::to_vec(&wrapped)?;
    let new_neural_hash = objects.save(&new_data)?;

    // Update the branch ref to point to the new neural commit
    refs.update(branch, &new_neural_hash)?;

    Ok(new_neural_hash)
}

/// Result of rewind reconciliation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RewindResult {
    /// Agit HEAD is valid - no rewind needed.
    NoRewindNeeded,
    /// Agit was rewound to a valid ancestor.
    Rewound {
        /// The orphaned neural commit hash (the old HEAD).
        old_hash: String,
        /// The valid ancestor we snapped to (the new HEAD).
        new_hash: String,
        /// Number of orphaned commits left behind (not deleted).
        orphaned_count: usize,
    },
    /// No valid ancestor found - all neural commits point to unreachable git commits.
    /// This is rare and likely indicates corruption or a very unusual git operation.
    NoValidAncestor {
        /// The current neural hash that has no valid git ancestor.
        neural_hash: String,
    },
    /// Detected git amend and migrated neural commit to new hash.
    ///
    /// This occurs when `git commit --amend` rewrote the commit that the neural
    /// commit was attached to. The neural commit's git_hash is updated to point
    /// to the new (amended) commit.
    MigratedAmend {
        /// The old git commit hash (before amend).
        old_git_hash: String,
        /// The new git commit hash (after amend).
        new_git_hash: String,
        /// The new neural commit hash (after migration).
        new_neural_hash: String,
    },
}

/// Detect if Agit HEAD is "dangling" and snap back to a valid ancestor.
///
/// This handles the scenario where the user runs `git reset --hard HEAD~N` and
/// Git moves backward, leaving Agit pointing to a neural commit attached to a
/// git commit that no longer exists in the history.
///
/// **Data Safety**: Orphaned neural commits are NOT deleted. They remain in storage
/// and can potentially be recovered if the user restores the git commit via `git reflog`.
///
/// # Arguments
///
/// * `git` - Git repository wrapper
/// * `objects` - Object store for loading neural commits
/// * `refs` - Ref store for reading/updating branch heads
/// * `branch` - The current branch name
///
/// # Returns
///
/// A `RewindResult` indicating what action was taken (if any).
pub fn reconcile_rewind<O: ObjectStore, R: RefStore>(
    git: &GitRepository,
    objects: &O,
    refs: &R,
    branch: &str,
) -> Result<RewindResult> {
    // 1. Get current agit HEAD neural commit
    let neural_hash = match refs.get(branch)? {
        Some(h) => h,
        None => return Ok(RewindResult::NoRewindNeeded), // No commits yet
    };

    // 2. Load neural commit and get its git_hash
    let data = objects.load(&neural_hash)?;
    let wrapped: WrappedNeuralCommit = serde_json::from_slice(&data)?;
    let agit_git_hash = &wrapped.data.git_hash;

    // 3. Get current Git HEAD
    let current_git_head = git.head_commit_hash()?;

    // 4. Special case: if hashes match exactly, we're in perfect sync
    if agit_git_hash == &current_git_head {
        return Ok(RewindResult::NoRewindNeeded);
    }

    // 5. Check if agit's git_hash is reachable from current git HEAD
    //    (i.e., is agit's git commit an ancestor of current git HEAD?)
    if git.is_ancestor(agit_git_hash, &current_git_head)? {
        return Ok(RewindResult::NoRewindNeeded); // Still valid - git moved forward
    }

    // 6. Check for amend detection before treating as rewind
    //    If the current git HEAD looks like an amended version of the old commit,
    //    migrate the neural commit to the new git hash instead of rewinding.
    if is_amend_replacement(git, agit_git_hash, &current_git_head) {
        let old_git_hash = agit_git_hash.clone();
        let new_neural_hash =
            migrate_neural_commit(objects, refs, branch, &neural_hash, &current_git_head)?;
        return Ok(RewindResult::MigratedAmend {
            old_git_hash,
            new_git_hash: current_git_head,
            new_neural_hash,
        });
    }

    // 7. Git has rewound! Walk agit history backwards to find valid ancestor
    find_valid_ancestor(git, objects, refs, branch, &neural_hash, &current_git_head)
}

/// Walk the agit commit chain backwards to find a neural commit whose git_hash
/// is reachable from the current git HEAD.
fn find_valid_ancestor<O: ObjectStore, R: RefStore>(
    git: &GitRepository,
    objects: &O,
    refs: &R,
    branch: &str,
    start_hash: &str,
    git_head: &str,
) -> Result<RewindResult> {
    let mut current = start_hash.to_string();
    let mut orphaned_count = 0;

    loop {
        // Load the current neural commit
        let data = objects.load(&current)?;
        let wrapped: WrappedNeuralCommit = serde_json::from_slice(&data)?;
        let commit = &wrapped.data;

        // Check if this commit's git_hash is reachable from git HEAD
        // (handles both exact match and ancestor relationship)
        let is_valid =
            commit.git_hash == git_head || git.is_ancestor(&commit.git_hash, git_head)?;

        if is_valid {
            // Found valid ancestor - update ref to point here
            refs.update(branch, &current)?;
            return Ok(RewindResult::Rewound {
                old_hash: start_hash.to_string(),
                new_hash: current,
                orphaned_count,
            });
        }

        orphaned_count += 1;

        // Move to parent neural commit
        match commit.first_parent() {
            Some(parent) => current = parent.to_string(),
            None => {
                // Reached root with no valid ancestor - very unusual
                return Ok(RewindResult::NoValidAncestor {
                    neural_hash: start_hash.to_string(),
                });
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::{Category, Role};

    fn make_entry(content: &str) -> IndexEntry {
        IndexEntry {
            role: Role::Ai,
            category: Category::Reasoning,
            content: content.to_string(),
            timestamp: chrono::Utc::now(),
            locations: None,
            file_path: None,
            line_number: None,
        }
    }

    #[test]
    fn test_extract_file_references_basic() {
        let entries = vec![make_entry("I will modify src/main.rs to fix the bug")];
        let refs = extract_file_references(&entries);
        assert!(refs.contains(&"src/main.rs".to_string()));
    }

    #[test]
    fn test_extract_file_references_multiple() {
        let entries = vec![
            make_entry("Update auth.rs and config.toml"),
            make_entry("Also check test/unit.rs"),
        ];
        let refs = extract_file_references(&entries);
        assert!(refs.contains(&"auth.rs".to_string()));
        assert!(refs.contains(&"config.toml".to_string()));
        assert!(refs.contains(&"test/unit.rs".to_string()));
    }

    #[test]
    fn test_extract_file_references_empty() {
        let entries = vec![make_entry("I will fix the authentication logic")];
        let refs = extract_file_references(&entries);
        // No file-like patterns in this content
        assert!(refs.is_empty());
    }

    #[test]
    fn test_looks_like_file_path() {
        assert!(looks_like_file_path("main.rs"));
        assert!(looks_like_file_path("src/lib.rs"));
        assert!(looks_like_file_path("test/unit.py"));
        assert!(!looks_like_file_path("hello"));
        assert!(!looks_like_file_path("README")); // No extension
        assert!(!looks_like_file_path(".gitignore")); // Hidden file without ext
    }

    #[test]
    fn test_check_semantic_conflict_no_overlap() {
        let ghost = GhostCommitInfo {
            git_hashes: vec!["abc123".to_string()],
            changed_files: vec!["README.md".to_string()],
        };
        let entries = vec![make_entry("I will modify src/main.rs")];

        let result = check_semantic_conflict(&ghost, &entries);
        assert!(!result.has_conflict);
        assert!(result.conflicting_files.is_empty());
    }

    #[test]
    fn test_check_semantic_conflict_with_overlap() {
        let ghost = GhostCommitInfo {
            git_hashes: vec!["abc123".to_string()],
            changed_files: vec!["src/main.rs".to_string()],
        };
        let entries = vec![make_entry("I will modify src/main.rs to fix the bug")];

        let result = check_semantic_conflict(&ghost, &entries);
        assert!(result.has_conflict);
        assert!(result
            .conflicting_files
            .contains(&"src/main.rs".to_string()));
    }

    #[test]
    fn test_check_semantic_conflict_partial_match() {
        let ghost = GhostCommitInfo {
            git_hashes: vec!["abc123".to_string()],
            changed_files: vec!["src/auth/login.rs".to_string()],
        };
        let entries = vec![make_entry("Update login.rs with new validation")];

        let result = check_semantic_conflict(&ghost, &entries);
        assert!(result.has_conflict);
    }
}