cflx 0.6.82

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
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
use super::*;

use crate::server::api::control::{
    list_selected_change_ids_in_worktree, start_single_project_run, CONTROL_CALLS,
};
use crate::server::api::ws::build_remote_project_snapshot_async;

// ─────────────────────────────── /api/v1/version ──────────────────────────────

/// GET /api/v1/version - return backend version string
pub async fn get_version() -> Response {
    (
        StatusCode::OK,
        Json(serde_json::json!({
            "version": format!("v{} ({})", env!("CARGO_PKG_VERSION"), env!("BUILD_NUMBER"))
        })),
    )
        .into_response()
}

// ─────────────────────────────── /api/v1/projects ─────────────────────────────

/// GET /api/v1/projects - list all projects
pub async fn list_projects(State(state): State<AppState>) -> Response {
    let registry = state.registry.read().await;
    let projects: Vec<ProjectResponse> = registry.list().into_iter().map(Into::into).collect();
    (StatusCode::OK, Json(projects)).into_response()
}

/// GET /api/v1/projects/state - list projects with their OpenSpec changes
///
/// This endpoint returns a server-oriented state snapshot suitable for any
/// dashboard or client (including the TUI): projects (remote_url + branch) each
/// with the list of OpenSpec changes discovered in that project's worktree.
pub async fn projects_state(State(state): State<AppState>) -> Response {
    let (entries, data_dir, all_selections, all_errors) = {
        let registry = state.registry.read().await;
        let entries = registry.list();
        let data_dir = registry.data_dir().to_path_buf();
        let all_selections: std::collections::HashMap<
            String,
            std::collections::HashMap<String, bool>,
        > = entries
            .iter()
            .filter_map(|e| {
                registry
                    .change_selections_for_project(&e.id)
                    .map(|s| (e.id.clone(), s.clone()))
            })
            .collect();
        let all_errors: std::collections::HashMap<
            String,
            std::collections::HashMap<String, String>,
        > = entries
            .iter()
            .filter_map(|e| {
                registry
                    .error_changes_for_project(&e.id)
                    .map(|s| (e.id.clone(), s.clone()))
            })
            .collect();
        (entries, data_dir, all_selections, all_errors)
    };

    let mut projects = Vec::new();
    for entry in &entries {
        let selections = all_selections.get(&entry.id);
        let errors = all_errors.get(&entry.id);
        projects.push(
            build_remote_project_snapshot_async(
                &data_dir,
                entry,
                selections,
                errors,
                &state.shared_orchestrator_state,
            )
            .await,
        );
    }

    let sync_available = state.resolve_command.is_some();

    (
        StatusCode::OK,
        Json(ProjectsStateResponse {
            projects,
            sync_available,
        }),
    )
        .into_response()
}

pub async fn add_project(
    State(state): State<AppState>,
    Json(req): Json<AddProjectRequest>,
) -> Response {
    if req.remote_url.trim().is_empty() || req.branch.trim().is_empty() {
        return error_response(
            StatusCode::BAD_REQUEST,
            "remote_url and branch are required",
        );
    }

    let remote_url = req.remote_url.clone();
    let branch = req.branch.clone();

    // Step 1: Register the project in the registry first so we can obtain the lock.
    let entry = {
        let mut registry = state.registry.write().await;
        match registry.add(remote_url.clone(), branch.clone()) {
            Ok(e) => e,
            Err(e) => {
                let msg = e.to_string();
                return if msg.contains("already exists") {
                    error_response(StatusCode::CONFLICT, msg)
                } else {
                    error_response(StatusCode::INTERNAL_SERVER_ERROR, msg)
                };
            }
        }
    };

    let project_id = entry.id.clone();

    // Helper: roll back registry insertion on failure.
    let rollback = |state: &AppState, project_id: String| {
        let registry = state.registry.clone();
        async move {
            let mut reg = registry.write().await;
            if let Err(e) = reg.remove(&project_id) {
                error!("Rollback failed for project_id={}: {}", project_id, e);
            } else {
                info!("Rolled back registry entry for project_id={}", project_id);
            }
        }
    };

    // Step 2: Acquire global semaphore and per-project lock.
    let (lock, semaphore) = {
        let registry = state.registry.read().await;
        let lock = match registry.project_lock(&project_id) {
            Some(l) => l,
            None => {
                rollback(&state, project_id).await;
                return error_response(StatusCode::INTERNAL_SERVER_ERROR, "Missing project lock");
            }
        };
        let semaphore = registry.global_semaphore();
        (lock, semaphore)
    };

    let _sem_permit = match semaphore.acquire().await {
        Ok(p) => p,
        Err(_) => {
            rollback(&state, project_id).await;
            return error_response(
                StatusCode::SERVICE_UNAVAILABLE,
                "Server is at maximum concurrent capacity",
            );
        }
    };

    let _guard = lock.lock().await;

    info!(
        "add_project: project_id={} remote_url={} branch={}",
        project_id, remote_url, branch
    );

    // Determine local paths.
    // The worktree uses a server-specific branch name to avoid checking out the base branch.
    // This is required so that git pull/push on the bare clone can update refs/heads/<branch>
    // without being blocked by an active checkout of that branch.
    let server_branch = server_worktree_branch(&project_id, &branch);
    let (data_dir, local_repo_path, worktree_path) = {
        let registry = state.registry.read().await;
        let data_dir = registry.data_dir().to_path_buf();
        let repo = data_dir.join(&project_id);
        let wt = data_dir.join("worktrees").join(&project_id).join(&branch);
        (data_dir, repo, wt)
    };

    // Step 3: Verify the branch exists on the remote.
    let ls_remote = tokio::process::Command::new("git")
        .args(["ls-remote", "--heads", &remote_url, &branch])
        .current_dir(&data_dir)
        .output()
        .await;

    match ls_remote {
        Ok(out) if out.status.success() => {
            let stdout = String::from_utf8_lossy(&out.stdout);
            if stdout.trim().is_empty() {
                rollback(&state, project_id).await;
                return error_response(
                    StatusCode::UNPROCESSABLE_ENTITY,
                    format!("Branch '{}' not found on remote '{}'", branch, remote_url),
                );
            }
        }
        Ok(out) => {
            let stderr = String::from_utf8_lossy(&out.stderr).to_string();
            error!("git ls-remote failed: {}", stderr);
            rollback(&state, project_id).await;
            return error_response(
                StatusCode::UNPROCESSABLE_ENTITY,
                format!("git operation failed: {}", stderr),
            );
        }
        Err(e) => {
            error!("Failed to run git: {}", e);
            rollback(&state, project_id).await;
            return error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Failed to run git: {}", e),
            );
        }
    }

    // Step 4: Clone as a bare repository if not already present.
    if !local_repo_path.exists() {
        let clone_output = tokio::process::Command::new("git")
            .args([
                "clone",
                "--bare",
                "--branch",
                &branch,
                "--single-branch",
                &remote_url,
                local_repo_path.to_str().unwrap_or(""),
            ])
            .output()
            .await;

        match clone_output {
            Ok(out) if out.status.success() => {
                info!("git clone (bare) succeeded: project_id={}", project_id);
            }
            Ok(out) => {
                let stderr = String::from_utf8_lossy(&out.stderr).to_string();
                error!(
                    "git clone failed: project_id={} stderr={}",
                    project_id, stderr
                );
                rollback(&state, project_id).await;
                return error_response(
                    StatusCode::UNPROCESSABLE_ENTITY,
                    format!("git clone failed: {}", stderr),
                );
            }
            Err(e) => {
                rollback(&state, project_id).await;
                return error_response(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    format!("Failed to run git clone: {}", e),
                );
            }
        }
    } else {
        info!(
            "Bare clone already exists, reusing: project_id={}",
            project_id
        );
    }

    // Step 5: Create a worktree at data_dir/worktrees/<project_id>/<branch>.
    // The worktree is created on a server-specific branch (`server-wt/<project_id>/<base_branch>`)
    // so that the bare clone's refs/heads/<base_branch> can be updated by git pull/push
    // without being blocked by an active checkout.
    if !worktree_path.exists() {
        if let Err(e) = std::fs::create_dir_all(&worktree_path) {
            error!("Failed to create worktree parent dirs: {}", e);
            rollback(&state, project_id).await;
            return error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Failed to create worktree directory: {}", e),
            );
        }
        // Remove the pre-created dir so git worktree add can create it.
        if let Err(e) = std::fs::remove_dir(&worktree_path) {
            error!("Failed to remove pre-created worktree dir: {}", e);
            rollback(&state, project_id).await;
            return error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Failed to prepare worktree directory: {}", e),
            );
        }

        // Use `-b <server_branch>` to create a new branch for the worktree.
        // This avoids checking out the base branch directly in the worktree,
        // which would prevent the bare clone from updating refs/heads/<base_branch>.
        let worktree_output = tokio::process::Command::new("git")
            .args([
                "worktree",
                "add",
                "-b",
                &server_branch,
                worktree_path.to_str().unwrap_or(""),
                &branch,
            ])
            .current_dir(&local_repo_path)
            .output()
            .await;

        match worktree_output {
            Ok(out) if out.status.success() => {
                info!(
                    "git worktree add succeeded: project_id={} path={:?} server_branch={}",
                    project_id, worktree_path, server_branch
                );
            }
            Ok(out) => {
                let stderr = String::from_utf8_lossy(&out.stderr).to_string();
                error!(
                    "git worktree add failed: project_id={} stderr={}",
                    project_id, stderr
                );
                // Clean up bare clone on worktree failure.
                let _ = std::fs::remove_dir_all(&local_repo_path);
                rollback(&state, project_id).await;
                return error_response(
                    StatusCode::UNPROCESSABLE_ENTITY,
                    format!("git worktree add failed: {}", stderr),
                );
            }
            Err(e) => {
                let _ = std::fs::remove_dir_all(&local_repo_path);
                rollback(&state, project_id).await;
                return error_response(
                    StatusCode::INTERNAL_SERVER_ERROR,
                    format!("Failed to run git worktree add: {}", e),
                );
            }
        }
    } else {
        // Worktree already exists. Check if it is checking out the base branch directly,
        // which would block git pull/push on the bare clone.
        // If so, return an error with instructions to recreate the worktree.
        let head_output = tokio::process::Command::new("git")
            .args(["worktree", "list", "--porcelain"])
            .current_dir(&local_repo_path)
            .output()
            .await;

        if let Ok(out) = head_output {
            let stdout = String::from_utf8_lossy(&out.stdout).to_string();
            // Parse worktree list output to find the worktree at our path and its branch.
            // Format:
            //   worktree /path/to/wt
            //   HEAD <sha>
            //   branch refs/heads/<branch>
            let wt_path_str = worktree_path.to_str().unwrap_or("");
            let mut in_our_worktree = false;
            let mut checked_out_branch: Option<String> = None;
            for line in stdout.lines() {
                if line.starts_with("worktree ") {
                    let wt_path = line.trim_start_matches("worktree ");
                    in_our_worktree = wt_path == wt_path_str;
                    if !in_our_worktree {
                        checked_out_branch = None;
                    }
                } else if in_our_worktree && line.starts_with("branch refs/heads/") {
                    checked_out_branch =
                        Some(line.trim_start_matches("branch refs/heads/").to_string());
                }
            }

            if checked_out_branch.as_deref() == Some(branch.as_str()) {
                // The existing worktree is checking out the base branch directly.
                // This blocks bare clone pull/push. The user must recreate it.
                error!(
                    "Existing worktree checks out base branch '{}' directly: project_id={} path={:?}. \
                    This blocks git pull/push on the bare clone.",
                    branch, project_id, worktree_path
                );
                let err_msg = format!(
                    "Existing worktree at {:?} checks out the base branch '{}' directly. \
                    This prevents git pull/push on the bare clone. \
                    To fix: remove the project (DELETE /api/v1/projects/{}), then re-add it \
                    so the worktree is recreated on a server-specific branch ({}). \
                    Alternatively, run: git worktree remove {:?} && re-add via API.",
                    worktree_path, branch, project_id, server_branch, worktree_path
                );
                rollback(&state, project_id).await;
                return error_response(StatusCode::CONFLICT, err_msg);
            }
        }

        info!(
            "Worktree already exists, reusing: project_id={} path={:?}",
            project_id, worktree_path
        );
    }

    // Step 6: Execute repo-root .wt/setup in the new worktree (if present).
    // Failure is treated as add_project failure and triggers rollback.
    if let Err(e) =
        crate::vcs::git::commands::run_worktree_setup(&worktree_path, &worktree_path).await
    {
        error!(
            "worktree setup failed: project_id={} worktree={:?} error={}",
            project_id, worktree_path, e
        );

        // Best-effort cleanup for partially provisioned project files.
        if let Err(cleanup_err) = std::fs::remove_dir_all(&worktree_path) {
            error!(
                "Failed to cleanup worktree after setup failure: project_id={} path={:?} error={}",
                project_id, worktree_path, cleanup_err
            );
        }
        if let Err(cleanup_err) = std::fs::remove_dir_all(&local_repo_path) {
            error!(
                "Failed to cleanup bare clone after setup failure: project_id={} path={:?} error={}",
                project_id, local_repo_path, cleanup_err
            );
        }

        rollback(&state, project_id).await;
        return error_response(
            StatusCode::UNPROCESSABLE_ENTITY,
            format!("worktree setup failed: {}", e),
        );
    }

    info!("Project added with clone and worktree: id={}", project_id);

    // Task 8: Auto-enqueue if orchestration is currently running.
    // When a new project is added during Running state, automatically spawn a runner for it.
    {
        let orch_status = state.orchestration_status.read().await;
        if *orch_status == OrchestrationStatus::Running {
            let changes = list_selected_change_ids_in_worktree(
                &worktree_path,
                None,
                &state.shared_orchestrator_state,
            )
            .await;
            if !changes.is_empty() {
                info!(
                    "Auto-enqueuing new project during Running state: project_id={} changes={:?}",
                    project_id, changes
                );
                if CONTROL_CALLS.get().is_none() {
                    if let Err(e) =
                        start_single_project_run(&state, &project_id, worktree_path, changes).await
                    {
                        error!(
                            "Failed to auto-enqueue new project: project_id={} err={}",
                            project_id, e
                        );
                    }
                }
            }
        }
    }

    (StatusCode::CREATED, Json(ProjectResponse::from(entry))).into_response()
}

/// DELETE /api/v1/projects/:id - remove a project
pub async fn delete_project(
    State(state): State<AppState>,
    Path(project_id): Path<String>,
) -> Response {
    if let Some(db) = &state.db {
        if let Err(e) = db.delete_change_states_for_project(&project_id) {
            warn!(project_id = %project_id, error = %e, "Failed to clear persisted change states before deleting project");
        }
    }

    let mut registry = state.registry.write().await;
    match registry.remove(&project_id) {
        Ok(_) => {
            info!("Project deleted: id={}", project_id);
            StatusCode::NO_CONTENT.into_response()
        }
        Err(e) => {
            let msg = e.to_string();
            if msg.contains("not found") {
                error_response(StatusCode::NOT_FOUND, msg)
            } else {
                error_response(StatusCode::INTERNAL_SERVER_ERROR, msg)
            }
        }
    }
}

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

    use std::env;

    use axum::body::Body;
    use axum::http::{Method, Request, StatusCode};
    use tempfile::TempDir;
    use tower::ServiceExt;

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

    #[cfg(feature = "heavy-tests")]
    use crate::server::api::test_support::create_local_git_repo_with_setup;

    #[cfg(feature = "heavy-tests")]
    #[tokio::test]
    async fn test_add_project_without_repo_root_setup_succeeds_without_marker() {
        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 body = serde_json::json!({
            "remote_url": remote_url,
            "branch": "main"
        });

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

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

        let body_bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
            .await
            .unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body_bytes).unwrap();
        let project_id = json["id"].as_str().expect("Response must contain id");

        let marker_path = temp_dir
            .path()
            .join("worktrees")
            .join(project_id)
            .join("main")
            .join(".setup-root-path");
        assert!(
            !marker_path.exists(),
            "No setup marker should exist when repo-root .wt/setup is absent"
        );
    }

    #[cfg(feature = "heavy-tests")]
    #[tokio::test]
    async fn test_add_project_setup_failure_returns_422_and_rolls_back_registry() {
        let temp_dir = TempDir::new().unwrap();
        let origin =
            create_local_git_repo_with_setup(temp_dir.path(), Some("#!/bin/sh\nexit 42\n"));
        let remote_url = format!("file://{}", origin.to_str().unwrap());
        let expected_project_id = crate::server::registry::generate_project_id(&remote_url, "main");

        let state = make_state(&temp_dir, None);
        let router = build_router(state.clone());

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

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

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

        let body_bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
            .await
            .unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body_bytes).unwrap();
        let error_message = json["error"].as_str().unwrap_or_default();
        assert!(
            error_message.contains("worktree setup failed"),
            "error should mention setup failure, got: {}",
            json
        );

        let registry = state.registry.read().await;
        assert!(
            registry.list().is_empty(),
            "Registry should be empty after setup failure rollback"
        );

        let local_repo_path = temp_dir.path().join(&expected_project_id);
        let worktree_path = temp_dir
            .path()
            .join("worktrees")
            .join(&expected_project_id)
            .join("main");
        assert!(
            !local_repo_path.exists(),
            "Bare clone should be cleaned up after setup failure"
        );
        assert!(
            !worktree_path.exists(),
            "Worktree should be cleaned up after setup failure"
        );
    }

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

        CONTROL_CALLS.get_or_init(|| Arc::new(std::sync::Mutex::new(Vec::new())));
        CONTROL_CALLS.get().unwrap().lock().unwrap().clear();

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

        let worktree_path = temp_dir
            .path()
            .join("worktrees")
            .join(&entry.id)
            .join(&entry.branch)
            .join("openspec/changes/fix-a");
        std::fs::create_dir_all(&worktree_path).unwrap();
        std::fs::write(worktree_path.join("proposal.md"), "# proposal\n").unwrap();

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

        let req = Request::builder()
            .method(Method::POST)
            .uri("/api/v1/control/run")
            .body(Body::empty())
            .unwrap();

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

        let calls = CONTROL_CALLS.get().unwrap().lock().unwrap();
        assert!(calls
            .iter()
            .any(|(id, action)| id == "_global_" && action == "run"));
        assert!(calls
            .iter()
            .any(|(id, action)| id == &entry.id && action == "run"));
    }

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

        let state = make_state(&temp_dir, None);
        let router = build_router(state.clone());

        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);

        run_sync_monitor_once_for_tests(&state).await;

        let req = Request::builder()
            .method(Method::GET)
            .uri("/api/v1/projects/state")
            .body(Body::empty())
            .unwrap();
        let resp = router.clone().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();

        let project = &json["projects"][0];
        assert_eq!(project["sync_state"], "up_to_date");
        assert_eq!(project["ahead_count"], 0);
        assert_eq!(project["behind_count"], 0);
        assert_eq!(project["sync_required"], false);
        assert!(project["local_sha"].as_str().is_some());
        assert!(project["remote_sha"].as_str().is_some());
        assert!(project["last_remote_check_at"].as_str().is_some());
        assert!(project["remote_check_error"].is_null());
    }

    #[cfg(feature = "heavy-tests")]
    #[test]
    fn test_app_state_resolve_command_comes_from_top_level_config() {
        let top_level_resolve_cmd = Some("echo top-level-resolve".to_string());
        let app_state_resolve_command = top_level_resolve_cmd.clone();

        assert_eq!(
            app_state_resolve_command,
            Some("echo top-level-resolve".to_string()),
            "AppState resolve_command should come from top-level config resolve_command"
        );
    }

    #[tokio::test]
    async fn test_valid_auth_token_returns_200() {
        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")
            .header("Authorization", "Bearer secret-token")
            .body(Body::empty())
            .unwrap();

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

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

        let put_req = Request::builder()
            .method(Method::PUT)
            .uri("/api/v1/ui-state/selected_project_id")
            .header("Content-Type", "application/json")
            .body(Body::from(r#"{"value":"proj-1"}"#))
            .unwrap();
        let put_resp = router.clone().oneshot(put_req).await.unwrap();
        assert_eq!(put_resp.status(), StatusCode::NO_CONTENT);

        let get_req = Request::builder()
            .method(Method::GET)
            .uri("/api/v1/ui-state")
            .body(Body::empty())
            .unwrap();
        let get_resp = router.clone().oneshot(get_req).await.unwrap();
        assert_eq!(get_resp.status(), StatusCode::OK);
        let body = axum::body::to_bytes(get_resp.into_body(), usize::MAX)
            .await
            .unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(json["selected_project_id"], "proj-1");

        let delete_req = Request::builder()
            .method(Method::DELETE)
            .uri("/api/v1/ui-state/selected_project_id")
            .body(Body::empty())
            .unwrap();
        let delete_resp = router.clone().oneshot(delete_req).await.unwrap();
        assert_eq!(delete_resp.status(), StatusCode::NO_CONTENT);

        let get_req = Request::builder()
            .method(Method::GET)
            .uri("/api/v1/ui-state")
            .body(Body::empty())
            .unwrap();
        let get_resp = router.oneshot(get_req).await.unwrap();
        assert_eq!(get_resp.status(), StatusCode::OK);
        let body = axum::body::to_bytes(get_resp.into_body(), usize::MAX)
            .await
            .unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        assert!(json.get("selected_project_id").is_none());
    }

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

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

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

    #[tokio::test]
    async fn test_get_version_no_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/version")
            .body(Body::empty())
            .unwrap();

        let resp = router.oneshot(req).await.unwrap();
        assert_eq!(
            resp.status(),
            StatusCode::OK,
            "GET /api/v1/version must succeed without authentication even when auth is configured"
        );
    }

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

        let req = Request::builder()
            .method(Method::GET)
            .uri("/api/v1/version")
            .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();

        let version = json["version"]
            .as_str()
            .expect("Response must contain 'version' field as a string");
        assert!(
            !version.is_empty(),
            "version field must not be empty, got: {:?}",
            version
        );
    }

    #[test]
    fn test_server_auth_config_resolve_token_from_token_field() {
        use crate::config::ServerAuthConfig;
        let auth = ServerAuthConfig {
            mode: crate::config::ServerAuthMode::BearerToken,
            token: Some("direct-token".to_string()),
            token_env: None,
        };
        assert_eq!(auth.resolve_token(), Some("direct-token".to_string()));
    }

    #[test]
    fn test_server_auth_config_resolve_token_from_env_var() {
        use crate::config::ServerAuthConfig;

        let env_var_name = "CFLX_TEST_SERVER_TOKEN_UNIQUE_12345";
        unsafe {
            env::set_var(env_var_name, "env-token-value");
        }

        let auth = ServerAuthConfig {
            mode: crate::config::ServerAuthMode::BearerToken,
            token: Some("fallback-token".to_string()),
            token_env: Some(env_var_name.to_string()),
        };
        assert_eq!(auth.resolve_token(), Some("env-token-value".to_string()));

        unsafe {
            env::remove_var(env_var_name);
        }
    }

    #[test]
    fn test_server_auth_config_resolve_token_falls_back_when_env_unset() {
        use crate::config::ServerAuthConfig;

        let env_var_name = "CFLX_TEST_SERVER_TOKEN_UNSET_UNIQUE_99999";
        unsafe {
            env::remove_var(env_var_name);
        }

        let auth = ServerAuthConfig {
            mode: crate::config::ServerAuthMode::BearerToken,
            token: Some("fallback-token".to_string()),
            token_env: Some(env_var_name.to_string()),
        };
        assert_eq!(auth.resolve_token(), Some("fallback-token".to_string()));
    }

    #[tokio::test]
    async fn test_add_project_creates_worktree_on_server_branch() {
        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 body = serde_json::json!({
            "remote_url": remote_url,
            "branch": "main"
        });

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

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

        let body_bytes = axum::body::to_bytes(resp.into_body(), usize::MAX)
            .await
            .unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body_bytes).unwrap();
        let project_id = json["id"].as_str().expect("Response must have id field");

        let worktree_path = temp_dir
            .path()
            .join("worktrees")
            .join(project_id)
            .join("main");

        assert!(
            worktree_path.exists(),
            "Worktree directory must exist at {:?}",
            worktree_path
        );

        let head_output = std::process::Command::new("git")
            .args(["symbolic-ref", "HEAD"])
            .current_dir(&worktree_path)
            .output();

        if let Ok(out) = head_output {
            if out.status.success() {
                let head = String::from_utf8_lossy(&out.stdout).trim().to_string();
                assert_ne!(
                    head, "refs/heads/main",
                    "Worktree HEAD must NOT reference refs/heads/main (base branch). It must use a server-specific branch. Got: {}",
                    head
                );
                let expected_prefix = format!("refs/heads/server-wt/{}/", project_id);
                assert!(
                    head.starts_with(&expected_prefix),
                    "Worktree HEAD must start with '{}'. Got: {}",
                    expected_prefix,
                    head
                );
            }
        }
    }

    #[test]
    fn test_server_worktree_branch_function_produces_correct_format() {
        use crate::server::registry::server_worktree_branch;
        let project_id = "abc123def456789a";
        let base_branch = "main";
        let branch = server_worktree_branch(project_id, base_branch);
        assert_eq!(
            branch, "server-wt/abc123def456789a/main",
            "server_worktree_branch must produce 'server-wt/<project_id>/<base_branch>'"
        );
    }
}