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
//! Handlers for hub synchronization endpoints.
//!
//! Implements:
//! - `GET /api/v1/sync/status` — hub init state, remote, lock counts
//! - `POST /api/v1/sync/fetch` — fetch latest state from remote
//! - `POST /api/v1/sync/push` — push local hub state to remote

use axum::{extract::State, http::StatusCode, response::Json};

use crate::server::{
    errors::internal_error,
    state::AppState,
    types::{ApiError, SyncActionResponse, SyncStatusResponse},
};
use crate::sync::SyncManager;

/// Try to construct a `SyncManager` from the shared crosslink dir.
fn sync_manager(state: &AppState) -> Result<SyncManager, (StatusCode, Json<ApiError>)> {
    SyncManager::new(&state.crosslink_dir)
        .map_err(|e| internal_error("Failed to initialize SyncManager", e))
}

// ---------------------------------------------------------------------------
// Handlers
// ---------------------------------------------------------------------------

/// `GET /api/v1/sync/status` — return hub initialization state, remote, and lock counts.
///
/// # Errors
///
/// Returns an error if the sync manager cannot be initialized or lock
/// data cannot be read.
pub async fn sync_status(
    State(state): State<AppState>,
) -> Result<Json<SyncStatusResponse>, (StatusCode, Json<ApiError>)> {
    let sm = sync_manager(&state)?;

    let hub_initialized = sm.is_initialized();
    let remote = sm.remote().to_string();

    // Lock and stale-lock counts (default to 0 if hub is not initialized)
    let (active_lock_count, stale_lock_count) = if hub_initialized {
        let locks = sm
            .read_locks_auto()
            .map_err(|e| internal_error("Failed to read locks", e))?;
        let active = locks.locks.len();

        let stale_count = sm.find_stale_locks_with_age().map_or(0, |v| v.len());

        (active, stale_count)
    } else {
        (0, 0)
    };

    // Derive last_fetch_at from the hub cache directory's modification time.
    let last_fetch_at = if hub_initialized {
        std::fs::metadata(sm.cache_path())
            .ok()
            .and_then(|m| m.modified().ok())
            .map(chrono::DateTime::<chrono::Utc>::from)
    } else {
        None
    };

    Ok(Json(SyncStatusResponse {
        hub_initialized,
        hub_branch: "crosslink/hub".to_string(),
        remote,
        last_fetch_at,
        active_lock_count,
        stale_lock_count,
    }))
}

/// `POST /api/v1/sync/fetch` — fetch the latest hub state from remote.
///
/// # Errors
///
/// Returns an error if the hub is not initialized or the fetch operation fails.
pub async fn sync_fetch(
    State(state): State<AppState>,
) -> Result<Json<SyncActionResponse>, (StatusCode, Json<ApiError>)> {
    let sm = sync_manager(&state)?;

    if !sm.is_initialized() {
        return Err((
            StatusCode::CONFLICT,
            Json(ApiError {
                error: "Hub not initialized".to_string(),
                detail: Some(
                    "Run `crosslink sync` from the CLI to initialize the hub cache first."
                        .to_string(),
                ),
            }),
        ));
    }

    sm.fetch().map_err(|e| internal_error("Fetch failed", e))?;

    Ok(Json(SyncActionResponse {
        success: true,
        message: "Fetched latest hub state from remote".to_string(),
    }))
}

/// `POST /api/v1/sync/push` — push local hub state to remote.
///
/// Commits any uncommitted changes in the hub cache and pushes to the remote.
///
/// # Errors
///
/// Returns an error if the hub is not initialized or the push operation fails.
pub async fn sync_push(
    State(state): State<AppState>,
) -> Result<Json<SyncActionResponse>, (StatusCode, Json<ApiError>)> {
    let sm = sync_manager(&state)?;

    if !sm.is_initialized() {
        return Err((
            StatusCode::CONFLICT,
            Json(ApiError {
                error: "Hub not initialized".to_string(),
                detail: Some(
                    "Run `crosslink sync` from the CLI to initialize the hub cache first."
                        .to_string(),
                ),
            }),
        ));
    }

    // Clean any dirty state (stage + commit recovery), then push.
    let had_dirty = sm
        .clean_dirty_state()
        .map_err(|e| internal_error("Failed to clean dirty state", e))?;

    // Fetch first to rebase any local changes on top of remote.
    sm.fetch()
        .map_err(|e| internal_error("Pre-push fetch failed", e))?;

    // Push the hub cache to remote using git push.
    push_hub_cache(&sm).map_err(|e| internal_error("Push failed", e))?;

    let message = if had_dirty {
        "Committed local changes and pushed hub state to remote".to_string()
    } else {
        "Pushed hub state to remote".to_string()
    };

    Ok(Json(SyncActionResponse {
        success: true,
        message,
    }))
}

/// Push the hub cache to the remote with retry on conflict.
fn push_hub_cache(sm: &SyncManager) -> anyhow::Result<()> {
    use std::process::Command;

    let cache_path = sm.cache_path();
    let remote = sm.remote();

    for attempt in 0..3 {
        let output = Command::new("git")
            .args(["push", remote, "crosslink/hub"])
            .current_dir(cache_path)
            .output()?;

        if output.status.success() {
            return Ok(());
        }

        let stderr = String::from_utf8_lossy(&output.stderr);
        if stderr.contains("Could not resolve host")
            || stderr.contains("Could not read from remote")
        {
            anyhow::bail!("Remote unreachable: {}", stderr.trim());
        }

        if (stderr.contains("rejected") || stderr.contains("non-fast-forward")) && attempt < 2 {
            // INTENTIONAL: rebase failure is non-fatal — the retry loop will bail on persistent conflicts
            let _ = Command::new("git")
                .args(["pull", "--rebase", remote, "crosslink/hub"])
                .current_dir(cache_path)
                .output();
            continue;
        }

        anyhow::bail!("Push failed: {stderr}");
    }

    Ok(())
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use axum::{
        body::Body,
        http::{Method, Request, StatusCode},
        Router,
    };
    use serde_json::Value;
    use tower::util::ServiceExt;

    use crate::db::Database;
    use crate::server::{routes::build_router, state::AppState};

    fn test_app() -> (Router, tempfile::TempDir) {
        let dir = tempfile::tempdir().expect("tempdir");
        let db_path = dir.path().join("test.db");
        let db = Database::open(&db_path).expect("test db");
        let state = AppState::new(db, dir.path().join(".crosslink"));
        (build_router(state, None), dir)
    }

    async fn body_json(resp: axum::response::Response) -> Value {
        let bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
            .await
            .unwrap();
        serde_json::from_slice(&bytes).unwrap()
    }

    #[tokio::test]
    async fn test_sync_status_no_hub() {
        let (app, _dir) = test_app();
        let resp = app
            .oneshot(
                Request::builder()
                    .method(Method::GET)
                    .uri("/api/v1/sync/status")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let body = body_json(resp).await;
        assert_eq!(body["hub_initialized"], false);
        assert_eq!(body["hub_branch"], "crosslink/hub");
        assert_eq!(body["active_lock_count"], 0);
        assert_eq!(body["stale_lock_count"], 0);
    }

    #[tokio::test]
    async fn test_sync_status_has_remote_field() {
        let (app, _dir) = test_app();
        let resp = app
            .oneshot(
                Request::builder()
                    .method(Method::GET)
                    .uri("/api/v1/sync/status")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let body = body_json(resp).await;
        // When no hook-config.json exists, defaults to "origin"
        assert_eq!(body["remote"], "origin");
    }

    #[tokio::test]
    async fn test_sync_fetch_no_hub_returns_conflict() {
        let (app, _dir) = test_app();
        let resp = app
            .oneshot(
                Request::builder()
                    .method(Method::POST)
                    .uri("/api/v1/sync/fetch")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::CONFLICT);
        let body = body_json(resp).await;
        assert!(body["error"].as_str().unwrap().contains("not initialized"));
    }

    #[tokio::test]
    async fn test_sync_push_no_hub_returns_conflict() {
        let (app, _dir) = test_app();
        let resp = app
            .oneshot(
                Request::builder()
                    .method(Method::POST)
                    .uri("/api/v1/sync/push")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::CONFLICT);
        let body = body_json(resp).await;
        assert!(body["error"].as_str().unwrap().contains("not initialized"));
    }

    /// Build a test app where the hub cache directory exists (making
    /// `SyncManager::is_initialized()` return true), but contains no real
    /// git data so network-touching operations will fail gracefully.
    fn test_app_with_hub() -> (axum::Router, tempfile::TempDir) {
        let dir = tempfile::tempdir().expect("tempdir");
        let db_path = dir.path().join("test.db");
        let db = Database::open(&db_path).expect("test db");
        let crosslink_dir = dir.path().join(".crosslink");
        // Create the hub cache dir so is_initialized() returns true.
        let hub_cache = crosslink_dir.join(".hub-cache");
        std::fs::create_dir_all(&hub_cache).unwrap();
        // Write a minimal locks.json so read_locks_auto doesn't error.
        let locks_json = serde_json::json!({
            "version": 1,
            "locks": {},
            "settings": {"stale_lock_timeout_minutes": 30},
            "updated_at": chrono::Utc::now().to_rfc3339()
        });
        std::fs::write(
            hub_cache.join("locks.json"),
            serde_json::to_string(&locks_json).unwrap(),
        )
        .unwrap();
        let state = AppState::new(db, crosslink_dir);
        (build_router(state, None), dir)
    }

    #[tokio::test]
    async fn test_sync_status_with_hub_initialized() {
        let (app, _dir) = test_app_with_hub();
        let resp = app
            .oneshot(
                Request::builder()
                    .method(Method::GET)
                    .uri("/api/v1/sync/status")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let body = body_json(resp).await;
        // Hub is initialized now
        assert_eq!(body["hub_initialized"], true);
        assert_eq!(body["hub_branch"], "crosslink/hub");
        // No locks seeded
        assert_eq!(body["active_lock_count"], 0);
        assert_eq!(body["stale_lock_count"], 0);
    }

    #[tokio::test]
    async fn test_sync_fetch_with_hub_initialized_offline() {
        // When hub is initialized but remote is unreachable (offline env),
        // the fetch attempt should not panic — it either succeeds or returns
        // an internal error. We only assert that the request completes.
        let (app, _dir) = test_app_with_hub();
        let resp = app
            .oneshot(
                Request::builder()
                    .method(Method::POST)
                    .uri("/api/v1/sync/fetch")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        // Either 200 (fetch ran, possibly offline OK) or 500 (git error)
        // — we just verify the request doesn't return 409 Conflict.
        assert_ne!(resp.status(), StatusCode::CONFLICT);
    }

    #[tokio::test]
    async fn test_sync_push_with_hub_initialized_offline() {
        // Similar to fetch — verifies the push handler exercises the
        // hub-initialized branch and completes without panicking.
        let (app, _dir) = test_app_with_hub();
        let resp = app
            .oneshot(
                Request::builder()
                    .method(Method::POST)
                    .uri("/api/v1/sync/push")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        // Either 200 (push ran) or 500 (git error — no remote configured)
        assert_ne!(resp.status(), StatusCode::CONFLICT);
    }

    #[test]
    fn test_internal_error_helper() {
        let (status, json) = crate::server::errors::internal_error("sync failed", "some error");
        assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
        assert_eq!(json.error, "sync failed");
        assert_eq!(json.detail.as_deref(), Some("some error"));
    }

    #[test]
    fn test_push_hub_cache_no_git_repo_bails() {
        // Call push_hub_cache with a SyncManager pointing at a temp dir
        // that has no git repo — the git push will fail.
        let dir = tempfile::tempdir().unwrap();
        let crosslink_dir = dir.path().join(".crosslink");
        let hub_cache = crosslink_dir.join(".hub-cache");
        std::fs::create_dir_all(&hub_cache).unwrap();

        let sm = crate::sync::SyncManager::new(&crosslink_dir).unwrap();
        // push_hub_cache runs `git push` which will fail in a non-git directory.
        // It should return an error (not panic).
        let result = super::push_hub_cache(&sm);
        // Either Ok (git offline path) or Err (git command failed) — not panic.
        let _ = result;
    }
}