crosslink 0.8.0

A synced issue tracker CLI for multi-agent AI development
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
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;

/// Custom serde for `HashMap`<i64, V> that serializes keys as strings for JSON
/// backward compatibility (locks.json uses string keys on disk).
mod string_key_map {
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::collections::HashMap;

    pub fn serialize<V: Serialize, S: Serializer>(
        map: &HashMap<i64, V>,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        let string_map: HashMap<String, &V> = map.iter().map(|(k, v)| (k.to_string(), v)).collect();
        string_map.serialize(serializer)
    }

    pub fn deserialize<'de, V: Deserialize<'de>, D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<HashMap<i64, V>, D::Error> {
        let string_map: HashMap<String, V> = HashMap::deserialize(deserializer)?;
        string_map
            .into_iter()
            .map(|(k, v)| {
                k.parse::<i64>()
                    .map(|id| (id, v))
                    .map_err(|_| serde::de::Error::custom(format!("invalid lock key: {k}")))
            })
            .collect()
    }
}

/// A single issue lock entry in locks.json.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Lock {
    pub agent_id: String,
    #[serde(default)]
    pub branch: Option<String>,
    pub claimed_at: DateTime<Utc>,
    pub signed_by: String,
}

/// Settings embedded in locks.json.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct LockSettings {
    #[serde(default = "default_stale_timeout")]
    pub stale_lock_timeout_minutes: u64,
}

const fn default_stale_timeout() -> u64 {
    60
}

impl Default for LockSettings {
    fn default() -> Self {
        Self {
            stale_lock_timeout_minutes: default_stale_timeout(),
        }
    }
}

/// The top-level locks.json structure.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct LocksFile {
    pub version: u32,
    /// Map from issue ID to Lock.
    #[serde(with = "string_key_map")]
    pub locks: HashMap<i64, Lock>,
    #[serde(default)]
    pub settings: LockSettings,
}

impl LocksFile {
    /// Load and parse a locks.json file.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be read or parsed as valid JSON.
    pub fn load(path: &Path) -> Result<Self> {
        let content = std::fs::read_to_string(path)
            .with_context(|| format!("Failed to read {}", path.display()))?;
        let locks: Self = serde_json::from_str(&content)
            .with_context(|| format!("Failed to parse {}", path.display()))?;
        Ok(locks)
    }

    /// Save to a file using atomic write (temp + rename) to prevent corruption.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be serialized or written atomically.
    pub fn save(&self, path: &Path) -> Result<()> {
        let json = serde_json::to_string_pretty(self)?;
        crate::utils::atomic_write(path, json.as_bytes())
    }

    /// Check if a specific issue is locked.
    #[must_use]
    pub fn is_locked(&self, issue_id: i64) -> bool {
        self.locks.contains_key(&issue_id)
    }

    /// Get the lock for a specific issue.
    #[must_use]
    pub fn get_lock(&self, issue_id: i64) -> Option<&Lock> {
        self.locks.get(&issue_id)
    }

    /// Check if an issue is locked by a specific agent.
    #[must_use]
    pub fn is_locked_by(&self, issue_id: i64, agent_id: &str) -> bool {
        self.locks
            .get(&issue_id)
            .is_some_and(|l| l.agent_id == agent_id)
    }

    /// List all issue IDs locked by a specific agent.
    #[must_use]
    pub fn agent_locks(&self, agent_id: &str) -> Vec<i64> {
        self.locks
            .iter()
            .filter(|(_, lock)| lock.agent_id == agent_id)
            .map(|(id, _)| *id)
            .collect()
    }

    /// Create an empty locks file.
    #[must_use]
    pub fn empty() -> Self {
        Self {
            version: 1,
            locks: HashMap::new(),
            settings: LockSettings::default(),
        }
    }
}

/// Heartbeat file for an agent (lives at `heartbeats/{agent_id}.json`).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Heartbeat {
    pub agent_id: String,
    pub last_heartbeat: DateTime<Utc>,
    pub active_issue_id: Option<i64>,
    pub machine_id: String,
}

/// Trust keyring — list of trusted GPG key fingerprints.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct Keyring {
    pub trusted_fingerprints: Vec<String>,
}

impl Keyring {
    /// Load and parse a keyring.json file.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be read or parsed as valid JSON.
    pub fn load(path: &Path) -> Result<Self> {
        let content = std::fs::read_to_string(path)
            .with_context(|| format!("Failed to read {}", path.display()))?;
        serde_json::from_str(&content)
            .with_context(|| format!("Failed to parse {}", path.display()))
    }

    /// Check if a fingerprint is trusted.
    #[must_use]
    pub fn is_trusted(&self, fingerprint: &str) -> bool {
        self.trusted_fingerprints.iter().any(|f| f == fingerprint)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;
    use tempfile::tempdir;

    fn sample_lock() -> Lock {
        Lock {
            agent_id: "worker-1".to_string(),
            branch: Some("feature/auth".to_string()),
            claimed_at: Utc::now(),
            signed_by: "ABCD1234".to_string(),
        }
    }

    fn sample_locks_file() -> LocksFile {
        let mut locks = HashMap::new();
        locks.insert(5, sample_lock());
        locks.insert(
            8,
            Lock {
                agent_id: "worker-2".to_string(),
                branch: Some("fix/api-timeout".to_string()),
                claimed_at: Utc::now(),
                signed_by: "EFGH5678".to_string(),
            },
        );
        LocksFile {
            version: 1,
            locks,
            settings: LockSettings::default(),
        }
    }

    // ==================== LocksFile Tests ====================

    #[test]
    fn test_empty_locks() {
        let locks = LocksFile::empty();
        assert_eq!(locks.version, 1);
        assert!(locks.locks.is_empty());
        assert_eq!(locks.settings.stale_lock_timeout_minutes, 60);
    }

    #[test]
    fn test_is_locked() {
        let locks = sample_locks_file();
        assert!(locks.is_locked(5));
        assert!(locks.is_locked(8));
        assert!(!locks.is_locked(1));
        assert!(!locks.is_locked(99));
    }

    #[test]
    fn test_get_lock() {
        let locks = sample_locks_file();
        let lock = locks.get_lock(5).unwrap();
        assert_eq!(lock.agent_id, "worker-1");
        assert_eq!(lock.branch, Some("feature/auth".to_string()));
        assert!(locks.get_lock(99).is_none());
    }

    #[test]
    fn test_is_locked_by() {
        let locks = sample_locks_file();
        assert!(locks.is_locked_by(5, "worker-1"));
        assert!(!locks.is_locked_by(5, "worker-2"));
        assert!(locks.is_locked_by(8, "worker-2"));
        assert!(!locks.is_locked_by(99, "worker-1"));
    }

    #[test]
    fn test_agent_locks() {
        let locks = sample_locks_file();
        let w1_locks = locks.agent_locks("worker-1");
        assert_eq!(w1_locks, vec![5]);
        let w2_locks = locks.agent_locks("worker-2");
        assert_eq!(w2_locks, vec![8]);
        let nobody_locks = locks.agent_locks("nobody");
        assert!(nobody_locks.is_empty());
    }

    #[test]
    fn test_save_and_load_roundtrip() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("locks.json");

        let original = sample_locks_file();
        original.save(&path).unwrap();

        let loaded = LocksFile::load(&path).unwrap();
        assert_eq!(loaded.version, original.version);
        assert_eq!(loaded.locks.len(), original.locks.len());
        assert_eq!(
            loaded.settings.stale_lock_timeout_minutes,
            original.settings.stale_lock_timeout_minutes
        );
    }

    #[test]
    fn test_load_missing_file() {
        let result = LocksFile::load(Path::new("/nonexistent/locks.json"));
        assert!(result.is_err());
    }

    #[test]
    fn test_json_roundtrip() {
        let locks = sample_locks_file();
        let json = serde_json::to_string_pretty(&locks).unwrap();
        let parsed: LocksFile = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.version, locks.version);
        assert_eq!(parsed.locks.len(), locks.locks.len());
    }

    #[test]
    fn test_missing_settings_defaults() {
        let json = r#"{"version": 1, "locks": {}}"#;
        let locks: LocksFile = serde_json::from_str(json).unwrap();
        assert_eq!(locks.settings.stale_lock_timeout_minutes, 60);
    }

    #[test]
    fn test_custom_stale_timeout() {
        let json =
            r#"{"version": 1, "locks": {}, "settings": {"stale_lock_timeout_minutes": 120}}"#;
        let locks: LocksFile = serde_json::from_str(json).unwrap();
        assert_eq!(locks.settings.stale_lock_timeout_minutes, 120);
    }

    // ==================== Lock Tests ====================

    #[test]
    fn test_lock_json_roundtrip() {
        let lock = sample_lock();
        let json = serde_json::to_string(&lock).unwrap();
        let parsed: Lock = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.agent_id, lock.agent_id);
        assert_eq!(parsed.branch, lock.branch);
        assert_eq!(parsed.signed_by, lock.signed_by);
    }

    #[test]
    fn test_lock_no_branch() {
        let lock = Lock {
            agent_id: "worker-1".to_string(),
            branch: None,
            claimed_at: Utc::now(),
            signed_by: "ABC".to_string(),
        };
        let json = serde_json::to_string(&lock).unwrap();
        let parsed: Lock = serde_json::from_str(&json).unwrap();
        assert!(parsed.branch.is_none());
    }

    // ==================== Heartbeat Tests ====================

    #[test]
    fn test_heartbeat_json_roundtrip() {
        let hb = Heartbeat {
            agent_id: "worker-1".to_string(),
            last_heartbeat: Utc::now(),
            active_issue_id: Some(5),
            machine_id: "my-host".to_string(),
        };
        let json = serde_json::to_string(&hb).unwrap();
        let parsed: Heartbeat = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.agent_id, hb.agent_id);
        assert_eq!(parsed.active_issue_id, Some(5));
        assert_eq!(parsed.machine_id, hb.machine_id);
    }

    #[test]
    fn test_heartbeat_no_active_issue() {
        let hb = Heartbeat {
            agent_id: "worker-1".to_string(),
            last_heartbeat: Utc::now(),
            active_issue_id: None,
            machine_id: "my-host".to_string(),
        };
        let json = serde_json::to_string(&hb).unwrap();
        let parsed: Heartbeat = serde_json::from_str(&json).unwrap();
        assert!(parsed.active_issue_id.is_none());
    }

    // ==================== Keyring Tests ====================

    #[test]
    fn test_keyring_is_trusted() {
        let keyring = Keyring {
            trusted_fingerprints: vec!["ABC123".to_string(), "DEF456".to_string()],
        };
        assert!(keyring.is_trusted("ABC123"));
        assert!(keyring.is_trusted("DEF456"));
        assert!(!keyring.is_trusted("XYZ999"));
        assert!(!keyring.is_trusted(""));
    }

    #[test]
    fn test_keyring_empty() {
        let keyring = Keyring {
            trusted_fingerprints: vec![],
        };
        assert!(!keyring.is_trusted("anything"));
    }

    #[test]
    fn test_keyring_save_and_load() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("keyring.json");

        let keyring = Keyring {
            trusted_fingerprints: vec!["ABC".to_string(), "DEF".to_string()],
        };
        let json = serde_json::to_string_pretty(&keyring).unwrap();
        std::fs::write(&path, json).unwrap();

        let loaded = Keyring::load(&path).unwrap();
        assert_eq!(loaded, keyring);
    }

    // ==================== Property-Based Tests ====================

    proptest! {
        #[test]
        fn prop_locks_file_roundtrip(
            id1 in 1i64..1000,
            id2 in 1001i64..2000,
            agent1 in "[a-z]{3,10}",
            agent2 in "[a-z]{3,10}",
        ) {
            let mut locks = HashMap::new();
            locks.insert(
                id1,
                Lock {
                    agent_id: agent1.clone(),
                    branch: None,
                    claimed_at: Utc::now(),
                    signed_by: "ABC".to_string(),
                },
            );
            locks.insert(
                id2,
                Lock {
                    agent_id: agent2.clone(),
                    branch: Some("branch".to_string()),
                    claimed_at: Utc::now(),
                    signed_by: "DEF".to_string(),
                },
            );

            let file = LocksFile {
                version: 1,
                locks,
                settings: LockSettings::default(),
            };

            let json = serde_json::to_string(&file).unwrap();
            let parsed: LocksFile = serde_json::from_str(&json).unwrap();

            prop_assert!(parsed.is_locked(id1));
            prop_assert!(parsed.is_locked(id2));
            prop_assert!(parsed.is_locked_by(id1, &agent1));
            prop_assert!(parsed.is_locked_by(id2, &agent2));
            prop_assert!(!parsed.is_locked_by(id1, &agent2));
        }

        #[test]
        fn prop_empty_locks_nothing_locked(id in 1i64..10000) {
            let locks = LocksFile::empty();
            prop_assert!(!locks.is_locked(id));
            prop_assert!(locks.get_lock(id).is_none());
        }
    }
}