aion-context 1.0.0

Cryptographically-signed, versioned business-context file format
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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Conflict Resolution Module
//!
//! Handles divergent version histories when files are modified concurrently
//! on different machines or by different authors.
//!
//! # Conflict Scenarios
//!
//! - **Fork**: Same file modified independently from same version
//! - **Divergent**: Files diverge at some point in history
//! - **Gap**: Missing versions in the chain

use crate::operations::{FileInfo, VersionInfo};

/// Conflict type detected between two file states
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum ConflictType {
    /// Files diverged from a common ancestor
    Divergent {
        /// Common ancestor version number
        common_ancestor: u64,
        /// Version where local diverged
        local_version: u64,
        /// Version where remote diverged
        remote_version: u64,
    },
    /// Local and remote have same version but different content
    ContentMismatch {
        /// Version number with mismatch
        version: u64,
        /// Local content hash
        local_hash: String,
        /// Remote content hash
        remote_hash: String,
    },
    /// Version gap in the chain
    VersionGap {
        /// Expected version number
        expected: u64,
        /// Actual version number found
        found: u64,
    },
    /// No conflict detected
    None,
}

impl std::fmt::Display for ConflictType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Divergent {
                common_ancestor,
                local_version,
                remote_version,
            } => {
                write!(f, "Divergent histories from version {common_ancestor}: local={local_version}, remote={remote_version}")
            }
            Self::ContentMismatch {
                version,
                local_hash,
                remote_hash,
            } => {
                write!(f, "Content mismatch at version {version}: local={local_hash}, remote={remote_hash}")
            }
            Self::VersionGap { expected, found } => {
                write!(f, "Version gap: expected {expected}, found {found}")
            }
            Self::None => write!(f, "No conflict"),
        }
    }
}

/// Conflict detection result
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ConflictReport {
    /// Type of conflict detected
    pub conflict_type: ConflictType,
    /// Local file info
    pub local_version_count: u64,
    /// Remote file info
    pub remote_version_count: u64,
    /// Suggested resolution strategy
    pub suggested_strategy: MergeStrategy,
}

/// Merge strategy for resolving conflicts
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum MergeStrategy {
    /// Keep local version, discard remote
    KeepLocal,
    /// Keep remote version, discard local
    KeepRemote,
    /// Keep version with higher version number
    KeepNewest,
    /// Manual merge required
    Manual,
    /// Append remote versions after local (if linear)
    Append,
}

impl std::fmt::Display for MergeStrategy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::KeepLocal => write!(f, "Keep local"),
            Self::KeepRemote => write!(f, "Keep remote"),
            Self::KeepNewest => write!(f, "Keep newest"),
            Self::Manual => write!(f, "Manual merge required"),
            Self::Append => write!(f, "Append remote versions"),
        }
    }
}

/// Detect conflicts between local and remote file states
///
/// # Arguments
///
/// * `local` - Local file info
/// * `remote` - Remote file info
///
/// # Returns
///
/// Conflict report with detected type and suggested strategy
#[must_use]
pub fn detect_conflict(local: &FileInfo, remote: &FileInfo) -> ConflictReport {
    if local.file_id != remote.file_id {
        return file_id_mismatch_report(local, remote);
    }
    if local.version_count == remote.version_count {
        return same_length_report(local, remote);
    }
    differing_length_report(local, remote)
}

fn file_id_mismatch_report(local: &FileInfo, remote: &FileInfo) -> ConflictReport {
    ConflictReport {
        conflict_type: ConflictType::ContentMismatch {
            version: 0,
            local_hash: format!("{:016x}", local.file_id),
            remote_hash: format!("{:016x}", remote.file_id),
        },
        local_version_count: local.version_count,
        remote_version_count: remote.version_count,
        suggested_strategy: MergeStrategy::Manual,
    }
}

fn same_length_report(local: &FileInfo, remote: &FileInfo) -> ConflictReport {
    if versions_match(local, remote) {
        return ConflictReport {
            conflict_type: ConflictType::None,
            local_version_count: local.version_count,
            remote_version_count: remote.version_count,
            suggested_strategy: MergeStrategy::KeepLocal,
        };
    }
    ConflictReport {
        conflict_type: ConflictType::ContentMismatch {
            version: local.current_version,
            local_hash: format_version_hash(local),
            remote_hash: format_version_hash(remote),
        },
        local_version_count: local.version_count,
        remote_version_count: remote.version_count,
        suggested_strategy: MergeStrategy::Manual,
    }
}

fn differing_length_report(local: &FileInfo, remote: &FileInfo) -> ConflictReport {
    let (shorter, longer) = if local.version_count < remote.version_count {
        (local, remote)
    } else {
        (remote, local)
    };
    if is_linear_extension(shorter, longer) {
        let strategy = if local.version_count < remote.version_count {
            MergeStrategy::KeepRemote
        } else {
            MergeStrategy::KeepLocal
        };
        return ConflictReport {
            conflict_type: ConflictType::None,
            local_version_count: local.version_count,
            remote_version_count: remote.version_count,
            suggested_strategy: strategy,
        };
    }
    ConflictReport {
        conflict_type: ConflictType::Divergent {
            common_ancestor: find_common_ancestor(local, remote),
            local_version: local.current_version,
            remote_version: remote.current_version,
        },
        local_version_count: local.version_count,
        remote_version_count: remote.version_count,
        suggested_strategy: MergeStrategy::Manual,
    }
}

/// Check if two file states have matching latest versions
fn versions_match(local: &FileInfo, remote: &FileInfo) -> bool {
    match (local.versions.last(), remote.versions.last()) {
        (Some(l), Some(r)) => l.rules_hash == r.rules_hash,
        (None, None) => true,
        _ => false,
    }
}

/// Format version hash for display
fn format_version_hash(info: &FileInfo) -> String {
    info.versions
        .last()
        .map_or_else(|| "empty".to_string(), |v| hex::encode(&v.rules_hash[..8]))
}

/// Check if shorter history is a linear prefix of longer
fn is_linear_extension(shorter: &FileInfo, longer: &FileInfo) -> bool {
    if shorter.versions.len() > longer.versions.len() {
        return false;
    }

    for (i, short_ver) in shorter.versions.iter().enumerate() {
        let Some(long_ver) = longer.versions.get(i) else {
            return false;
        };
        if short_ver.rules_hash != long_ver.rules_hash {
            return false;
        }
    }

    true
}

/// Find the common ancestor version between two divergent histories
fn find_common_ancestor(local: &FileInfo, remote: &FileInfo) -> u64 {
    let min_len = std::cmp::min(local.versions.len(), remote.versions.len());
    let mut last_matching: Option<&VersionInfo> = None;

    for i in 0..min_len {
        let (Some(l), Some(r)) = (local.versions.get(i), remote.versions.get(i)) else {
            break;
        };
        if l.rules_hash != r.rules_hash {
            return last_matching.map_or(0, |v| v.version_number);
        }
        last_matching = Some(l);
    }

    last_matching.map_or(0, |v| v.version_number)
}

/// Conflict marker for manual resolution
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ConflictMarker {
    /// Marker type
    pub marker_type: MarkerType,
    /// Start position in content
    pub start: usize,
    /// End position in content
    pub end: usize,
    /// Source label
    pub source: String,
}

/// Type of conflict marker
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum MarkerType {
    /// Start of conflicting section
    ConflictStart,
    /// Separator between local and remote
    Separator,
    /// End of conflicting section
    ConflictEnd,
}

/// Create conflict markers for manual merge
///
/// Format similar to git merge conflicts:
/// ```text
/// <<<<<<< LOCAL
/// local content
/// =======
/// remote content
/// >>>>>>> REMOTE
/// ```
#[must_use]
pub fn create_conflict_markers(
    local_content: &[u8],
    remote_content: &[u8],
    local_label: &str,
    remote_label: &str,
) -> Vec<u8> {
    let mut result = Vec::new();

    // Start marker
    result.extend_from_slice(b"<<<<<<< ");
    result.extend_from_slice(local_label.as_bytes());
    result.push(b'\n');

    // Local content
    result.extend_from_slice(local_content);
    if !local_content.ends_with(b"\n") {
        result.push(b'\n');
    }

    // Separator
    result.extend_from_slice(b"=======\n");

    // Remote content
    result.extend_from_slice(remote_content);
    if !remote_content.ends_with(b"\n") {
        result.push(b'\n');
    }

    // End marker
    result.extend_from_slice(b">>>>>>> ");
    result.extend_from_slice(remote_label.as_bytes());
    result.push(b'\n');

    result
}

/// Check if content contains conflict markers
#[must_use]
pub fn has_conflict_markers(content: &[u8]) -> bool {
    let content_str = String::from_utf8_lossy(content);
    content_str.contains("<<<<<<<") && content_str.contains(">>>>>>>")
}

/// Parse conflict markers from content
///
/// Returns (`local_content`, `remote_content`) if markers found
#[must_use]
pub fn parse_conflict_markers(content: &[u8]) -> Option<(Vec<u8>, Vec<u8>)> {
    let content_str = String::from_utf8_lossy(content);

    let start_idx = content_str.find("<<<<<<< ")?;
    let separator_idx = content_str.find("=======")?;
    let end_idx = content_str.find(">>>>>>> ")?;

    if start_idx >= separator_idx || separator_idx >= end_idx {
        return None;
    }

    // Extract local content (after first newline, before separator)
    let after_start = content_str
        .get(start_idx..)?
        .find('\n')?
        .checked_add(start_idx)?
        .checked_add(1)?;
    let local = content_str.get(after_start..separator_idx)?;

    // Extract remote content (after separator newline, before end)
    let after_sep = separator_idx.checked_add(8)?; // "=======\n"
    let remote = content_str.get(after_sep..end_idx)?;

    Some((
        local.trim_end().as_bytes().to_vec(),
        remote.trim_end().as_bytes().to_vec(),
    ))
}

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

    #[test]
    fn test_conflict_markers() {
        let local = b"local version";
        let remote = b"remote version";

        let merged = create_conflict_markers(local, remote, "LOCAL", "REMOTE");

        assert!(has_conflict_markers(&merged));

        let (parsed_local, parsed_remote) =
            parse_conflict_markers(&merged).unwrap_or_else(|| std::process::abort());
        assert_eq!(parsed_local, local.to_vec());
        assert_eq!(parsed_remote, remote.to_vec());
    }

    #[test]
    fn test_no_conflict_markers() {
        let content = b"normal content without markers";
        assert!(!has_conflict_markers(content));
    }

    #[test]
    fn test_merge_strategy_display() {
        assert_eq!(MergeStrategy::KeepLocal.to_string(), "Keep local");
        assert_eq!(MergeStrategy::Manual.to_string(), "Manual merge required");
    }

    #[test]
    fn test_conflict_type_display() {
        let conflict = ConflictType::Divergent {
            common_ancestor: 5,
            local_version: 7,
            remote_version: 8,
        };
        let display = conflict.to_string();
        assert!(display.contains("Divergent"));
        assert!(display.contains('5'));
    }
}