cflx 0.6.11

Conflux – a spec-driven parallel coding orchestrator that runs AI agents on git worktrees
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
use super::*;

// ─────────────────────────── /api/v1/projects/:id/worktrees ───────────────────

/// Request body for creating a worktree in server mode.
#[derive(Debug, Deserialize)]
pub struct ServerCreateWorktreeRequest {
    /// Change ID to create the worktree for
    pub change_id: String,
    /// Optional base commit (defaults to HEAD of project branch)
    #[serde(default)]
    pub base_commit: Option<String>,
}

/// Response for worktree operations that return a success message.
#[derive(Debug, Serialize)]
pub(super) struct WorktreeOpResponse {
    success: bool,
    message: String,
}

/// Helper: resolve the project's main worktree path from registry.
pub(super) async fn resolve_project_worktree_path(
    state: &AppState,
    project_id: &str,
) -> Result<(std::path::PathBuf, crate::server::registry::ProjectEntry), Response> {
    let registry = state.registry.read().await;
    let entry = registry.get(project_id).cloned().ok_or_else(|| {
        error_response(
            StatusCode::NOT_FOUND,
            format!("Project not found: {}", project_id),
        )
    })?;
    let data_dir = registry.data_dir().to_path_buf();
    let worktree_path = data_dir
        .join("worktrees")
        .join(project_id)
        .join(&entry.branch);
    Ok((worktree_path, entry))
}

/// GET /api/v1/projects/:id/worktrees - list all worktrees for a project
pub async fn server_list_worktrees(
    State(state): State<AppState>,
    Path(project_id): Path<String>,
) -> Response {
    let (worktree_path, _entry) = match resolve_project_worktree_path(&state, &project_id).await {
        Ok(v) => v,
        Err(resp) => return resp,
    };

    if !worktree_path.exists() {
        return (
            StatusCode::OK,
            Json(Vec::<crate::remote::types::RemoteWorktreeInfo>::new()),
        )
            .into_response();
    }

    match crate::worktree_ops::get_worktrees(&worktree_path).await {
        Ok(worktrees) => {
            let remote_worktrees: Vec<crate::remote::types::RemoteWorktreeInfo> =
                worktrees.into_iter().map(Into::into).collect();
            (StatusCode::OK, Json(remote_worktrees)).into_response()
        }
        Err(e) => {
            error!(
                project_id = %project_id,
                error = %e,
                "Failed to list worktrees"
            );
            error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Failed to list worktrees: {}", e),
            )
        }
    }
}

/// POST /api/v1/projects/:id/worktrees - create a new worktree
pub async fn server_create_worktree(
    State(state): State<AppState>,
    Path(project_id): Path<String>,
    Json(req): Json<ServerCreateWorktreeRequest>,
) -> Response {
    let (worktree_path, _entry) = match resolve_project_worktree_path(&state, &project_id).await {
        Ok(v) => v,
        Err(resp) => return resp,
    };

    if !worktree_path.exists() {
        return error_response(
            StatusCode::NOT_FOUND,
            format!(
                "Project worktree not found at {:?}. Add the project first.",
                worktree_path
            ),
        );
    }

    // Check if worktree already exists for this change_id
    match crate::worktree_ops::worktree_exists(&worktree_path, &req.change_id).await {
        Ok(true) => {
            return error_response(
                StatusCode::CONFLICT,
                format!("Worktree for '{}' already exists", req.change_id),
            );
        }
        Ok(false) => {}
        Err(e) => {
            return error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Failed to check worktree existence: {}", e),
            );
        }
    }

    // Determine workspace base directory for new worktrees under this project
    let workspace_base = {
        let registry = state.registry.read().await;
        registry.data_dir().join("worktrees").join(&project_id)
    };

    // Sanitize change_id for branch name
    let branch_name = req.change_id.replace(['/', '\\', ' '], "-");
    let new_worktree_path = workspace_base.join(&branch_name);

    // Ensure base directory exists
    if let Err(e) = std::fs::create_dir_all(&workspace_base) {
        return error_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("Failed to create workspace directory: {}", e),
        );
    }

    // Get base commit
    let base_commit = match req.base_commit {
        Some(commit) => commit,
        None => match crate::vcs::git::commands::get_current_commit(&worktree_path).await {
            Ok(commit) => commit,
            Err(e) => {
                return error_response(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    format!("Failed to get current commit: {}", e),
                );
            }
        },
    };

    // Create worktree
    if let Err(e) = crate::vcs::git::commands::worktree_add(
        &worktree_path,
        new_worktree_path.to_str().unwrap_or(""),
        &branch_name,
        &base_commit,
    )
    .await
    {
        return error_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("Failed to create worktree: {}", e),
        );
    }

    // Execute setup script if it exists
    let _ = crate::vcs::git::commands::run_worktree_setup(&worktree_path, &new_worktree_path).await;

    info!(
        project_id = %project_id,
        change_id = %req.change_id,
        "Worktree created successfully"
    );

    let worktree_info = crate::remote::types::RemoteWorktreeInfo {
        path: new_worktree_path.to_string_lossy().to_string(),
        label: branch_name.clone(),
        head: base_commit[..8.min(base_commit.len())].to_string(),
        branch: branch_name,
        is_detached: false,
        is_main: false,
        is_merging: false,
        has_commits_ahead: false,
        merge_conflict: None,
    };

    (StatusCode::CREATED, Json(worktree_info)).into_response()
}

/// DELETE /api/v1/projects/:id/worktrees/:branch - delete a worktree
pub async fn server_delete_worktree(
    State(state): State<AppState>,
    Path((project_id, branch)): Path<(String, String)>,
) -> Response {
    // Acquire active command slot for this worktree root
    let _active_guard = match try_acquire_active_command(
        &state.active_commands,
        &project_id,
        RootKind::Worktree(branch.clone()),
        "delete",
    )
    .await
    {
        Ok(guard) => guard,
        Err(resp) => return resp,
    };

    let (worktree_path, _entry) = match resolve_project_worktree_path(&state, &project_id).await {
        Ok(v) => v,
        Err(resp) => return resp,
    };

    if !worktree_path.exists() {
        return error_response(StatusCode::NOT_FOUND, "Project worktree not found");
    }

    // Get worktree list to find the target
    let worktrees = match crate::worktree_ops::get_worktrees(&worktree_path).await {
        Ok(wts) => wts,
        Err(e) => {
            return error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Failed to list worktrees: {}", e),
            );
        }
    };

    let worktree = match worktrees.iter().find(|wt| wt.branch == branch) {
        Some(wt) => wt,
        None => {
            return error_response(
                StatusCode::NOT_FOUND,
                format!("Worktree '{}' not found", branch),
            );
        }
    };

    // Validate deletion
    let (can_delete, reason) = crate::worktree_ops::can_delete_worktree(worktree).await;
    if !can_delete {
        let error_msg = reason.unwrap_or_else(|| "Cannot delete worktree".to_string());
        return error_response(StatusCode::CONFLICT, error_msg);
    }

    // Delete worktree
    if let Err(e) =
        crate::vcs::git::commands::worktree_remove(&worktree_path, worktree.path.to_str().unwrap())
            .await
    {
        return error_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("Failed to remove worktree: {}", e),
        );
    }

    // Delete branch
    let _ = crate::vcs::git::commands::branch_delete(&worktree_path, &branch).await;

    info!(
        project_id = %project_id,
        branch = %branch,
        "Worktree deleted successfully"
    );

    (
        StatusCode::OK,
        Json(WorktreeOpResponse {
            success: true,
            message: format!("Worktree '{}' deleted successfully", branch),
        }),
    )
        .into_response()
}

/// POST /api/v1/projects/:id/worktrees/:branch/merge - merge a worktree branch
pub async fn server_merge_worktree(
    State(state): State<AppState>,
    Path((project_id, branch)): Path<(String, String)>,
) -> Response {
    // Merge operates on the base worktree (merging branch into base), so guard the base root.
    // Also guard the worktree root to prevent concurrent operations on the branch being merged.
    let _active_guard_base = match try_acquire_active_command(
        &state.active_commands,
        &project_id,
        RootKind::Base,
        "merge",
    )
    .await
    {
        Ok(guard) => guard,
        Err(resp) => return resp,
    };
    let _active_guard_wt = match try_acquire_active_command(
        &state.active_commands,
        &project_id,
        RootKind::Worktree(branch.clone()),
        "merge",
    )
    .await
    {
        Ok(guard) => guard,
        Err(resp) => return resp,
    };

    let (worktree_path, _entry) = match resolve_project_worktree_path(&state, &project_id).await {
        Ok(v) => v,
        Err(resp) => return resp,
    };

    if !worktree_path.exists() {
        return error_response(StatusCode::NOT_FOUND, "Project worktree not found");
    }

    // Get worktree list to find the target
    let worktrees = match crate::worktree_ops::get_worktrees(&worktree_path).await {
        Ok(wts) => wts,
        Err(e) => {
            return error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Failed to list worktrees: {}", e),
            );
        }
    };

    let worktree = match worktrees.iter().find(|wt| wt.branch == branch) {
        Some(wt) => wt,
        None => {
            return error_response(
                StatusCode::NOT_FOUND,
                format!("Worktree '{}' not found", branch),
            );
        }
    };

    // Validate merge
    let (can_merge, reason) = crate::worktree_ops::can_merge_worktree(worktree);
    if !can_merge {
        let error_msg = reason.unwrap_or_else(|| "Cannot merge worktree".to_string());
        return error_response(StatusCode::CONFLICT, error_msg);
    }

    // Get base branch name
    let base_branch = match worktrees.iter().find(|wt| wt.is_main) {
        Some(wt) => wt.branch.clone(),
        None => {
            return error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                "Failed to determine base branch",
            );
        }
    };

    // Checkout base branch
    if let Err(e) = crate::vcs::git::commands::checkout(&worktree_path, &base_branch).await {
        return error_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("Failed to checkout base branch: {}", e),
        );
    }

    // Merge branch
    if let Err(e) = crate::vcs::git::commands::merge(&worktree_path, &branch).await {
        return error_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            format!("Failed to merge branch: {}", e),
        );
    }

    info!(
        project_id = %project_id,
        branch = %branch,
        base_branch = %base_branch,
        "Worktree merged successfully"
    );

    (
        StatusCode::OK,
        Json(WorktreeOpResponse {
            success: true,
            message: format!(
                "Branch '{}' merged into '{}' successfully",
                branch, base_branch
            ),
        }),
    )
        .into_response()
}

/// POST /api/v1/projects/:id/worktrees/refresh - refresh worktree conflict detection
pub async fn server_refresh_worktrees(
    State(state): State<AppState>,
    Path(project_id): Path<String>,
) -> Response {
    let (worktree_path, _entry) = match resolve_project_worktree_path(&state, &project_id).await {
        Ok(v) => v,
        Err(resp) => return resp,
    };

    if !worktree_path.exists() {
        return error_response(StatusCode::NOT_FOUND, "Project worktree not found");
    }

    match crate::worktree_ops::get_worktrees(&worktree_path).await {
        Ok(_) => (
            StatusCode::OK,
            Json(WorktreeOpResponse {
                success: true,
                message: "Worktree snapshot refreshed".to_string(),
            }),
        )
            .into_response(),
        Err(e) => {
            error!(
                project_id = %project_id,
                error = %e,
                "Failed to refresh worktree snapshot"
            );
            error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Failed to refresh worktree snapshot: {}", e),
            )
        }
    }
}

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

    use crate::server::api::build_router;
    use crate::server::api::test_support::{create_local_git_repo, make_router, make_state};

    #[tokio::test]
    async fn test_list_worktrees_with_real_project() {
        let temp_dir = TempDir::new().unwrap();
        let origin = create_local_git_repo(temp_dir.path());
        let remote_url = format!("file://{}", origin.to_str().unwrap());

        let router = make_router(&temp_dir, None);

        let add_body = serde_json::json!({
            "remote_url": remote_url,
            "branch": "main"
        });

        let add_req = Request::builder()
            .method(Method::POST)
            .uri("/api/v1/projects")
            .header("Content-Type", "application/json")
            .body(Body::from(add_body.to_string()))
            .unwrap();

        let add_resp = router.clone().oneshot(add_req).await.unwrap();
        assert_eq!(add_resp.status(), StatusCode::CREATED);

        let body_bytes = axum::body::to_bytes(add_resp.into_body(), usize::MAX)
            .await
            .unwrap();
        let project_json: serde_json::Value = serde_json::from_slice(&body_bytes).unwrap();
        let project_id = project_json["id"].as_str().unwrap();

        let list_req = Request::builder()
            .method(Method::GET)
            .uri(format!("/api/v1/projects/{}/worktrees", project_id))
            .body(Body::empty())
            .unwrap();

        let list_resp = router.oneshot(list_req).await.unwrap();
        assert_eq!(list_resp.status(), StatusCode::OK);

        let body = axum::body::to_bytes(list_resp.into_body(), usize::MAX)
            .await
            .unwrap();
        let worktrees: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
        assert!(
            !worktrees.is_empty(),
            "Should have at least one worktree after project add"
        );
    }

    #[tokio::test]
    async fn test_list_worktrees_project_not_found_returns_404() {
        let temp_dir = TempDir::new().unwrap();
        let router = make_router(&temp_dir, None);

        let req = Request::builder()
            .method(Method::GET)
            .uri("/api/v1/projects/nonexistent/worktrees")
            .body(Body::empty())
            .unwrap();

        let resp = router.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn test_list_worktrees_empty_for_registered_project() {
        let temp_dir = TempDir::new().unwrap();
        let state = make_state(&temp_dir, None);

        let entry = state
            .registry
            .write()
            .await
            .add("https://github.com/foo/bar".to_string(), "main".to_string())
            .unwrap();

        let router = build_router(state.clone());

        let req = Request::builder()
            .method(Method::GET)
            .uri(format!("/api/v1/projects/{}/worktrees", entry.id))
            .body(Body::empty())
            .unwrap();

        let resp = router.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);

        let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
            .await
            .unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(json, serde_json::json!([]));
    }

    #[tokio::test]
    async fn test_delete_worktree_project_not_found_returns_404() {
        let temp_dir = TempDir::new().unwrap();
        let router = make_router(&temp_dir, None);

        let req = Request::builder()
            .method(Method::DELETE)
            .uri("/api/v1/projects/nonexistent/worktrees/some-branch")
            .body(Body::empty())
            .unwrap();

        let resp = router.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn test_merge_worktree_project_not_found_returns_404() {
        let temp_dir = TempDir::new().unwrap();
        let router = make_router(&temp_dir, None);

        let req = Request::builder()
            .method(Method::POST)
            .uri("/api/v1/projects/nonexistent/worktrees/some-branch/merge")
            .body(Body::empty())
            .unwrap();

        let resp = router.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn test_refresh_worktrees_project_not_found_returns_404() {
        let temp_dir = TempDir::new().unwrap();
        let router = make_router(&temp_dir, None);

        let req = Request::builder()
            .method(Method::POST)
            .uri("/api/v1/projects/nonexistent/worktrees/refresh")
            .body(Body::empty())
            .unwrap();

        let resp = router.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn test_create_worktree_project_not_found_returns_404() {
        let temp_dir = TempDir::new().unwrap();
        let router = make_router(&temp_dir, None);

        let body = serde_json::json!({
            "change_id": "test-change"
        });

        let req = Request::builder()
            .method(Method::POST)
            .uri("/api/v1/projects/nonexistent/worktrees")
            .header("Content-Type", "application/json")
            .body(Body::from(body.to_string()))
            .unwrap();

        let resp = router.oneshot(req).await.unwrap();
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[tokio::test]
    async fn test_worktree_auth_required() {
        let temp_dir = TempDir::new().unwrap();
        let router = make_router(&temp_dir, Some("secret-token"));

        let req = Request::builder()
            .method(Method::GET)
            .uri("/api/v1/projects/some-id/worktrees")
            .body(Body::empty())
            .unwrap();

        let resp = router.oneshot(req).await.unwrap();
        assert_eq!(
            resp.status(),
            StatusCode::UNAUTHORIZED,
            "Worktree endpoints should require authentication"
        );
    }
}