wsb 0.186.4098

Workspace - A tool suite for file operations, version management, and development workflow automation
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
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
use crate::prelude::*;
use axum::{
    extract::{Query, State},
    http::StatusCode,
    Json,
};
use std::{
    collections::HashMap,
    fs,
    path::{Component, Path, PathBuf},
};

use super::{fleet::workspace_root, sessions::ErrorResponse};
use crate::fleet::{config::GitModules, error::SuperGitError};
use crate::git_backend::FileStatusKind;
use crate::web::github;
use crate::web::handlers::fleet::RepoDetailedStatus;
use crate::web::state::WebState;
use crate::workspace_state::WorkspaceState;
use axum::extract::State as AxumState;
use axum::response::sse::{Event, KeepAlive, Sse};
use futures::stream::Stream;
use std::convert::Infallible;

#[derive(Debug, Deserialize)]
pub struct WorkspacePathQuery {
    #[serde(default)]
    path: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct WorkspaceUpdateRequest {
    #[serde(default)]
    pub path: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct WorkspaceUpdateResponse {
    pub path: String,
    pub version: String,
}

#[derive(Debug, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum WorkspaceEntryKind {
    File,
    Directory,
    #[serde(rename = "error")]
    Error,
}

#[derive(Debug, Serialize, Clone)]
pub struct WorkspaceSubmoduleInfo {
    pub name: String,
    pub version: Option<String>,
    pub github_repo: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct WorkspaceEntry {
    pub path: String,
    pub name: String,
    pub kind: WorkspaceEntryKind,
    pub hidden: bool,
    pub submodule: Option<WorkspaceSubmoduleInfo>,
    /// Git status for this entry, pre-computed by the backend.
    /// For files: the file's own status. For plain directories: the worst
    /// status among all files within. For submodule directories: null
    /// (use repo_status instead).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub git_status: Option<FileStatusKind>,
    /// Full repo status for submodule directory entries. Null for files and
    /// plain directories.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repo_status: Option<RepoDetailedStatus>,
    /// For files inside a submodule, the workspace-relative path of the owning
    /// submodule (e.g. "libs/core"). Null for files in the superproject and for
    /// directory entries. Used by the diff view to scope git operations.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repo_path: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct WorkspaceTreeResponse {
    pub root: String,
    pub path: String,
    pub entries: Vec<WorkspaceEntry>,
}

#[derive(Debug, Serialize)]
pub struct WorkspaceFileResponse {
    pub root: String,
    pub path: String,
    pub content: String,
}

pub async fn list_tree(
    State(state): State<WebState>,
    Query(query): Query<WorkspacePathQuery>,
) -> Result<Json<WorkspaceTreeResponse>, (StatusCode, Json<ErrorResponse>)> {
    let root = workspace_root()?;
    let relative = sanitize_relative_path(query.path.as_deref().unwrap_or(""))?;
    let target = resolve_workspace_path(&root, &relative)?;

    // Snapshot git status before entering the blocking task so we don't hold
    // the async read lock across a blocking boundary.
    let git_snapshot: HashMap<String, RepoDetailedStatus> =
        state.git_status_snapshot.read().await.clone();

    // If the path doesn't exist, return an error entry instead of 500
    if !target.exists() {
        return Ok(Json(WorkspaceTreeResponse {
            root: root.display().to_string(),
            path: display_relative_path(&relative),
            entries: vec![WorkspaceEntry {
                path: display_relative_path(&relative),
                name: relative
                    .file_name()
                    .unwrap_or_default()
                    .to_string_lossy()
                    .into_owned(),
                kind: WorkspaceEntryKind::Error,
                hidden: false,
                submodule: None,
                git_status: None,
                repo_status: None,
                repo_path: None,
            }],
        }));
    }

    let (root, relative, entries) = tokio::task::spawn_blocking(move || {
        let metadata = fs::metadata(&target).map_err(internal_error)?;
        if !metadata.is_dir() {
            return Err(bad_request(format!(
                "workspace path '{}' is not a directory",
                display_relative_path(&relative)
            )));
        }

        let mut raw_entries = fs::read_dir(&target)
            .map_err(internal_error)?
            .collect::<Result<Vec<_>, _>>()
            .map_err(internal_error)?;

        raw_entries.sort_by(|left, right| {
            let left_kind = left.file_type().map(|ft| ft.is_dir()).unwrap_or(false);
            let right_kind = right.file_type().map(|ft| ft.is_dir()).unwrap_or(false);
            match right_kind.cmp(&left_kind) {
                std::cmp::Ordering::Equal => left.file_name().cmp(&right.file_name()),
                ordering => ordering,
            }
        });

        let visible_directory_paths = raw_entries
            .iter()
            .filter_map(|entry| {
                let file_type = entry.file_type().ok()?;
                if !file_type.is_dir() {
                    return None;
                }
                let name = entry.file_name().to_string_lossy().into_owned();
                Some(join_relative_path(&relative, &name))
            })
            .collect::<Vec<_>>();
        // Submodules only exist at the workspace root level. Never parse
        // .gitmodules when listing a subdirectory — submodules are flat, not nested.
        let is_root = relative.as_os_str().is_empty();
        let submodules = if is_root {
            load_workspace_submodules(&root, &visible_directory_paths)
        } else {
            HashMap::new()
        };
        let uninitialized_submodules = if is_root {
            load_uninitialized_submodules(&root, &visible_directory_paths)
        } else {
            vec![]
        };

        // Determine which repo owns the directory being listed:
        // - Root ("") is always the superproject.
        // - Any path matching a known submodule path belongs to that submodule.
        // - Paths inside a submodule (e.g. "libs/core/src") also belong to it.
        let relative_str = display_relative_path(&relative);
        let owning_repo: Option<&RepoDetailedStatus> = git_snapshot
            .values()
            .find(|r| {
                if r.is_superproject {
                    relative_str.is_empty()
                } else {
                    relative_str == r.path || relative_str.starts_with(&format!("{}/", r.path))
                }
            });

        // Build file→status map for files in this directory's owning repo.
        // Keys are repo-relative paths; we need workspace-relative paths for lookup.
        let (file_status_map, repo_path_prefix): (HashMap<String, FileStatusKind>, &str) =
            if let Some(repo) = owning_repo {
                let prefix = if repo.is_superproject { "" } else { &repo.path };
                let map = repo
                    .files
                    .iter()
                    .map(|f| {
                        let ws_path = if prefix.is_empty() {
                            f.path.clone()
                        } else {
                            format!("{prefix}/{}", f.path)
                        };
                        (ws_path, f.status)
                    })
                    .collect();
                (map, prefix)
            } else {
                (HashMap::new(), "")
            };

        let mut entries = raw_entries
            .into_iter()
            .map(|entry| {
                let file_type = entry.file_type().map_err(internal_error)?;
                let file_name = entry.file_name().to_string_lossy().into_owned();
                let entry_relative = join_relative_path(&relative, &file_name);
                let submodule = if file_type.is_dir() {
                    submodules.get(&entry_relative).cloned()
                } else {
                    None
                };

                let (git_status, repo_status, repo_path) = if file_type.is_dir() {
                    if submodule.is_some() {
                        // Submodule directory: attach full repo status, no dir aggregation
                        let rs = git_snapshot.get(&entry_relative).cloned();
                        (None, rs, None)
                    } else {
                        // Plain directory: aggregate worst file status under this prefix
                        let dir_status =
                            aggregate_dir_status(&entry_relative, &file_status_map);
                        (dir_status, None, None)
                    }
                } else {
                    // File: direct status lookup and owning repo path
                    let fs = file_status_map.get(&entry_relative).copied();
                    let rp = if repo_path_prefix.is_empty() {
                        None
                    } else {
                        Some(repo_path_prefix.to_string())
                    };
                    (fs, None, rp)
                };

                Ok(WorkspaceEntry {
                    path: entry_relative.clone(),
                    name: submodule
                        .as_ref()
                        .map(|s| s.name.clone())
                        .unwrap_or(file_name),
                    kind: if file_type.is_dir() {
                        WorkspaceEntryKind::Directory
                    } else {
                        WorkspaceEntryKind::File
                    },
                    hidden: is_hidden_name(&entry_relative),
                    submodule,
                    git_status,
                    repo_status,
                    repo_path,
                })
            })
            .collect::<Result<Vec<_>, _>>()?;

        for sub in &uninitialized_submodules {
            let rs = git_snapshot.get(&sub.path).cloned();
            entries.push(WorkspaceEntry {
                path: sub.path.clone(),
                name: sub.name.clone(),
                kind: WorkspaceEntryKind::Directory,
                hidden: is_hidden_name(&sub.path),
                submodule: Some(WorkspaceSubmoduleInfo {
                    name: sub.name.clone(),
                    version: None,
                    github_repo: sub.github_repo.clone(),
                    error: Some("submodule not initialized".to_string()),
                }),
                git_status: None,
                repo_status: rs,
                repo_path: None,
            });
        }

        entries.sort_by(compare_workspace_entries);

        Ok((root, relative, entries))
    })
    .await
    .map_err(|e| internal_error(format!("list_tree task panicked: {e}")))??;

    Ok(Json(WorkspaceTreeResponse {
        root: root.display().to_string(),
        path: display_relative_path(&relative),
        entries,
    }))
}

/// Aggregate the worst git status for all files under `dir_path/` prefix.
/// Ignored files do not contribute — a directory containing only ignored
/// files returns None so it appears uncoloured.
fn aggregate_dir_status(
    dir_path: &str,
    file_status_map: &HashMap<String, FileStatusKind>,
) -> Option<FileStatusKind> {
    let prefix = format!("{dir_path}/");
    let mut worst: Option<FileStatusKind> = None;
    for (path, &status) in file_status_map {
        if !path.starts_with(&prefix) {
            continue;
        }
        if status == FileStatusKind::Ignored {
            continue;
        }
        worst = Some(match worst {
            None => status,
            Some(current) => higher_priority_status(current, status),
        });
        if worst == Some(FileStatusKind::Conflict) {
            return worst;
        }
    }
    worst
}

fn higher_priority_status(a: FileStatusKind, b: FileStatusKind) -> FileStatusKind {
    if status_priority(b) > status_priority(a) { b } else { a }
}

fn status_priority(s: FileStatusKind) -> u8 {
    match s {
        FileStatusKind::Conflict  => 6,
        FileStatusKind::Deleted   => 5,
        FileStatusKind::Modified  => 4,
        FileStatusKind::Staged    => 3,
        FileStatusKind::Renamed   => 2,
        FileStatusKind::Untracked => 1,
        FileStatusKind::Ignored   => 0,
    }
}

/// GET /api/v1/workspace/watch — SSE stream that signals when the git status
/// snapshot changes so clients know which directory trees to refetch.
///
/// Each event is `event: changed` with a JSON array of affected directory
/// paths (workspace-relative). Clients should refetch `/workspace/tree` for
/// each path in the array to get fresh git-annotated entries.
pub async fn workspace_watch(
    State(state): State<WebState>,
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
    let mut rx = state.git_status_tx.subscribe();

    let stream = async_stream::stream! {
        // Send an initial "ping" so the client knows the connection is live.
        yield Ok(Event::default().event("connected").data("{}"));

        loop {
            match rx.recv().await {
                Ok(repo) => {
                    // The repo path is the directory that changed. Clients
                    // holding an open tree for that path (or its parent "")
                    // need to refetch.
                    let paths = affected_tree_paths(&repo.path);
                    if let Ok(json) = serde_json::to_string(&paths) {
                        yield Ok(Event::default().event("changed").data(json));
                    }
                }
                Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => {
                    // We fell behind. Signal the client to refetch everything.
                    let paths: Vec<String> = vec!["".to_string()];
                    if let Ok(json) = serde_json::to_string(&paths) {
                        yield Ok(Event::default().event("changed").data(json));
                    }
                }
                Err(tokio::sync::broadcast::error::RecvError::Closed) => break,
            }
        }
    };

    Sse::new(stream).keep_alive(KeepAlive::default())
}

/// Given a repo's workspace-relative path, return the set of directory tree
/// paths that need to be refetched when that repo's status changes.
///
/// The root superproject (".") affects the root tree path ("").
/// A submodule at "libs/core" affects its parent dir "libs" and the root "".
fn affected_tree_paths(repo_path: &str) -> Vec<String> {
    if repo_path == "." {
        return vec!["".to_string()];
    }
    let mut paths = vec!["".to_string()];
    // Add the immediate parent directory of the submodule
    if let Some(parent) = std::path::Path::new(repo_path).parent() {
        let p = parent.to_string_lossy();
        if !p.is_empty() && p != "." {
            paths.push(p.into_owned());
        }
    }
    paths.dedup();
    paths
}

fn load_workspace_submodules(
    root: &Path,
    visible_directory_paths: &[String],
) -> HashMap<String, WorkspaceSubmoduleInfo> {
    let gitmodules = match GitModules::parse(root) {
        Ok(gm) => gm,
        Err(SuperGitError::NoSubmodules) => return HashMap::new(),
        Err(e) => {
            tracing::warn!("Failed to parse .gitmodules: {e}");
            return HashMap::new();
        }
    };

    let visible_directory_paths = visible_directory_paths
        .iter()
        .cloned()
        .collect::<std::collections::HashSet<_>>();

    let mut submodules = HashMap::with_capacity(gitmodules.submodules.len());
    for submodule in gitmodules.submodules {
        let relative_path = submodule.path.to_string_lossy().into_owned();
        if !visible_directory_paths.contains(&relative_path) {
            continue;
        }
        let submodule_root = root.join(&submodule.path);
        let version = crate::state::read_version_from_path(&submodule_root)
            .map(|version| version.full_version)
            .ok();
        let github_repo = github::parse_github_url(&submodule.url)
            .map(|(owner, repo)| format!("{owner}/{repo}"));
        let (name, error) = match read_submodule_project_name(&submodule_root) {
            Some(name) => (name, None),
            None => (submodule.name, None),
        };
        submodules.insert(
            relative_path,
            WorkspaceSubmoduleInfo {
                name,
                version,
                github_repo,
                error,
            },
        );
    }

    submodules
}

struct UninitializedSubmodule {
    path: String,
    name: String,
    github_repo: Option<String>,
}

fn load_uninitialized_submodules(
    root: &Path,
    existing_directory_paths: &[String],
) -> Vec<UninitializedSubmodule> {
    let gitmodules = match GitModules::parse(root) {
        Ok(gm) => gm,
        Err(SuperGitError::NoSubmodules) => return Vec::new(),
        Err(_) => return Vec::new(),
    };

    let existing: std::collections::HashSet<_> = existing_directory_paths.iter().collect();

    let mut result = Vec::new();
    for submodule in gitmodules.submodules {
        let relative_path = submodule.path.to_string_lossy().into_owned();
        if existing.contains(&relative_path) {
            continue;
        }
        let github_repo = github::parse_github_url(&submodule.url)
            .map(|(owner, repo)| format!("{owner}/{repo}"));
        result.push(UninitializedSubmodule {
            path: relative_path,
            name: submodule.name,
            github_repo,
        });
    }
    result
}

fn read_submodule_project_name(submodule_root: &Path) -> Option<String> {
    let state_file = submodule_root.join(".wsb").join("state.json");
    if !state_file.exists() {
        return None;
    }

    let content = fs::read_to_string(&state_file).ok()?;
    let state = serde_json::from_str::<WorkspaceState>(&content).ok()?;
    state.project_name.and_then(|name| {
        let trimmed = name.trim();
        if trimmed.is_empty() {
            None
        } else {
            Some(trimmed.to_string())
        }
    })
}

pub async fn read_file(
    State(_state): State<WebState>,
    Query(query): Query<WorkspacePathQuery>,
) -> Result<Json<WorkspaceFileResponse>, (StatusCode, Json<ErrorResponse>)> {
    let root = workspace_root()?;
    let requested = query.path.as_deref().unwrap_or("").trim();
    if requested.is_empty() {
        return Err(bad_request("workspace file path is required".to_string()));
    }

    let relative = sanitize_relative_path(requested)?;
    let target = resolve_workspace_path(&root, &relative)?;

    let (root, relative, content) = tokio::task::spawn_blocking(move || {
        let metadata = fs::metadata(&target).map_err(internal_error)?;
        if !metadata.is_file() {
            return Err(bad_request(format!(
                "workspace path '{}' is not a file",
                display_relative_path(&relative)
            )));
        }

        let bytes = fs::read(&target).map_err(internal_error)?;
        let content = String::from_utf8(bytes).map_err(|_| {
            bad_request(format!(
                "workspace file '{}' is not valid UTF-8 text",
                display_relative_path(&relative)
            ))
        })?;

        Ok((root, relative, content))
    })
    .await
    .map_err(|e| internal_error(format!("read_file task panicked: {e}")))??;

    Ok(Json(WorkspaceFileResponse {
        root: root.display().to_string(),
        path: display_relative_path(&relative),
        content,
    }))
}

pub async fn run_update(
    AxumState(_state): AxumState<WebState>,
    Json(body): Json<WorkspaceUpdateRequest>,
) -> Result<Json<WorkspaceUpdateResponse>, (StatusCode, Json<ErrorResponse>)> {
    let root = workspace_root()?;
    let relative = sanitize_relative_path(body.path.as_deref().unwrap_or(""))?;
    validate_update_target(&root, &relative)?;
    let target = resolve_workspace_path(&root, &relative)?;

    let metadata = fs::metadata(&target).map_err(internal_error)?;
    if !metadata.is_dir() {
        return Err(bad_request(format!(
            "workspace path '{}' is not a directory",
            display_relative_path(&relative)
        )));
    }

    let display_path = display_relative_path(&relative);
    let version = crate::workspace_update::run_workspace_update_async(&target)
        .await
        .map_err(internal_error)?
        .version;

    Ok(Json(WorkspaceUpdateResponse {
        path: display_path,
        version,
    }))
}

fn sanitize_relative_path(raw: &str) -> Result<PathBuf, (StatusCode, Json<ErrorResponse>)> {
    let path = Path::new(raw);
    if path.is_absolute() {
        return Err(bad_request(format!(
            "workspace path '{}' must be relative to the workspace root",
            raw
        )));
    }

    let mut relative = PathBuf::new();
    for component in path.components() {
        match component {
            Component::CurDir => {}
            Component::Normal(part) => relative.push(part),
            Component::ParentDir => {
                return Err(bad_request(format!(
                    "workspace path '{}' must not traverse outside the workspace root",
                    raw
                )))
            }
            Component::RootDir | Component::Prefix(_) => {
                return Err(bad_request(format!(
                    "workspace path '{}' must be relative to the workspace root",
                    raw
                )))
            }
        }
    }

    Ok(relative)
}

fn validate_update_target(
    root: &Path,
    relative: &Path,
) -> Result<(), (StatusCode, Json<ErrorResponse>)> {
    if relative.as_os_str().is_empty() {
        return Ok(());
    }

    let gitmodules = match GitModules::parse(root) {
        Ok(gitmodules) => gitmodules,
        Err(SuperGitError::NoSubmodules) => {
            return Err(bad_request(format!(
                "workspace path '{}' is not a submodule root",
                relative.display()
            )))
        }
        Err(error) => return Err(internal_error(error)),
    };

    let relative_str = relative.to_string_lossy();
    if gitmodules
        .submodules
        .iter()
        .any(|submodule| submodule.path == relative)
    {
        return Ok(());
    }

    Err(bad_request(format!(
        "workspace path '{}' is not a submodule root",
        relative_str
    )))
}

fn resolve_workspace_path(
    root: &Path,
    relative: &Path,
) -> Result<PathBuf, (StatusCode, Json<ErrorResponse>)> {
    let target = root.join(relative);
    if !target.starts_with(root) {
        return Err(bad_request(format!(
            "workspace path '{}' resolved outside the workspace root",
            display_relative_path(relative)
        )));
    }
    Ok(target)
}

fn join_relative_path(base: &Path, name: &str) -> String {
    if base.as_os_str().is_empty() {
        name.to_string()
    } else {
        base.join(name).to_string_lossy().into_owned()
    }
}

fn display_relative_path(path: &Path) -> String {
    if path.as_os_str().is_empty() {
        String::new()
    } else {
        path.to_string_lossy().into_owned()
    }
}

fn compare_workspace_entries(left: &WorkspaceEntry, right: &WorkspaceEntry) -> std::cmp::Ordering {
    let left_is_directory = left.kind == WorkspaceEntryKind::Directory;
    let right_is_directory = right.kind == WorkspaceEntryKind::Directory;
    match right_is_directory.cmp(&left_is_directory) {
        std::cmp::Ordering::Equal => {
            let left_is_submodule = left.submodule.is_some();
            let right_is_submodule = right.submodule.is_some();
            match right_is_submodule.cmp(&left_is_submodule) {
                std::cmp::Ordering::Equal => left.name.cmp(&right.name),
                ordering => ordering,
            }
        }
        ordering => ordering,
    }
}

fn is_hidden_name(path: &str) -> bool {
    Path::new(path)
        .file_name()
        .and_then(|name| name.to_str())
        .is_some_and(|name| name.starts_with('.'))
}

fn bad_request(error: String) -> (StatusCode, Json<ErrorResponse>) {
    (StatusCode::BAD_REQUEST, Json(ErrorResponse { error }))
}

fn internal_error(error: impl ToString) -> (StatusCode, Json<ErrorResponse>) {
    (
        StatusCode::INTERNAL_SERVER_ERROR,
        Json(ErrorResponse {
            error: error.to_string(),
        }),
    )
}

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

    #[test]
    fn sanitize_relative_path_rejects_parent_traversal() {
        let error = sanitize_relative_path("../secret.txt").unwrap_err();
        assert_eq!(error.0, StatusCode::BAD_REQUEST);
        assert!(error.1.error.contains("must not traverse outside"));
    }

    #[test]
    fn list_tree_sorts_directories_before_files() {
        let temp = TempDir::new().unwrap();
        fs::create_dir(temp.path().join("src")).unwrap();
        fs::write(temp.path().join("Cargo.toml"), "wsb").unwrap();
        fs::write(temp.path().join("README.md"), "docs").unwrap();

        let mut entries = fs::read_dir(temp.path())
            .unwrap()
            .collect::<Result<Vec<_>, _>>()
            .unwrap();
        entries.sort_by(|left, right| {
            let left_kind = left.file_type().map(|ft| ft.is_dir()).unwrap_or(false);
            let right_kind = right.file_type().map(|ft| ft.is_dir()).unwrap_or(false);
            match right_kind.cmp(&left_kind) {
                std::cmp::Ordering::Equal => left.file_name().cmp(&right.file_name()),
                ordering => ordering,
            }
        });

        let names = entries
            .into_iter()
            .map(|entry| entry.file_name().to_string_lossy().into_owned())
            .collect::<Vec<_>>();
        assert_eq!(names, vec!["src", "Cargo.toml", "README.md"]);
    }

    #[test]
    fn resolve_workspace_path_stays_under_root() {
        let temp = TempDir::new().unwrap();
        let relative = match sanitize_relative_path("src/lib.rs") {
            Ok(value) => value,
            Err(_) => panic!("expected relative path to sanitize"),
        };
        let resolved = match resolve_workspace_path(temp.path(), &relative) {
            Ok(value) => value,
            Err(_) => panic!("expected workspace path to resolve under root"),
        };
        assert!(resolved.starts_with(temp.path()));
        assert!(resolved.ends_with("src/lib.rs"));
    }

    #[test]
    fn hidden_entries_are_detected_from_leaf_name() {
        assert!(is_hidden_name(".git"));
        assert!(is_hidden_name("dir/.env"));
        assert!(!is_hidden_name("src/lib.rs"));
    }

    #[test]
    fn load_workspace_submodules_reads_versions_from_version_txt() {
        let temp = TempDir::new().unwrap();
        fs::write(
            temp.path().join(".gitmodules"),
            r#"[submodule "core"]
	path = libs/core
	url = https://github.com/example/core.git
"#,
        )
        .unwrap();
        fs::create_dir_all(temp.path().join("libs/core")).unwrap();
        fs::write(temp.path().join("libs/core/version.txt"), "0.414.56214\n").unwrap();

        let submodules =
            load_workspace_submodules(temp.path(), &[String::from("libs/core")]);

        assert_eq!(submodules.len(), 1);
        assert_eq!(submodules["libs/core"].name, "core");
        assert_eq!(submodules["libs/core"].version.as_deref(), Some("0.414.56214"));
    }

    #[test]
    fn load_workspace_submodules_omits_version_when_unavailable() {
        let temp = TempDir::new().unwrap();
        fs::write(
            temp.path().join(".gitmodules"),
            r#"[submodule "thesis"]
	path = thesis
	url = https://github.com/example/thesis.git
"#,
        )
        .unwrap();
        fs::create_dir_all(temp.path().join("thesis")).unwrap();

        let submodules = load_workspace_submodules(temp.path(), &[String::from("thesis")]);

        assert_eq!(submodules.len(), 1);
        assert_eq!(submodules["thesis"].name, "thesis");
        assert_eq!(submodules["thesis"].version, None);
    }

    #[test]
    fn read_submodule_project_name_uses_workspace_state_when_present() {
        let temp = TempDir::new().unwrap();
        fs::create_dir_all(temp.path().join(".wsb")).unwrap();
        fs::write(
            temp.path().join(".wsb/state.json"),
            r#"{
  "version": 1,
  "project_root": "/tmp/example",
  "project_name": "Capdag Core",
  "tools": {},
  "wstemplate_entries": [],
  "completion_hint_shown": false
}"#,
        )
        .unwrap();

        assert_eq!(
            read_submodule_project_name(temp.path()).as_deref(),
            Some("Capdag Core")
        );
    }

    #[test]
    fn compare_workspace_entries_places_submodules_before_other_nodes() {
        let mut entries = vec![
            WorkspaceEntry {
                path: "README.md".to_string(),
                name: "README.md".to_string(),
                kind: WorkspaceEntryKind::File,
                hidden: false,
                submodule: None,
                git_status: None,
                repo_status: None,
                repo_path: None,
            },
            WorkspaceEntry {
                path: "src".to_string(),
                name: "src".to_string(),
                kind: WorkspaceEntryKind::Directory,
                hidden: false,
                submodule: None,
                git_status: None,
                repo_status: None,
                repo_path: None,
            },
            WorkspaceEntry {
                path: "capdag".to_string(),
                name: "capdag".to_string(),
                kind: WorkspaceEntryKind::Directory,
                hidden: false,
                submodule: Some(WorkspaceSubmoduleInfo {
                    name: "capdag".to_string(),
                    version: Some("0.414.56214".to_string()),
                    github_repo: None,
                    error: None,
                }),
                git_status: None,
                repo_status: None,
                repo_path: None,
            },
        ];

        entries.sort_by(compare_workspace_entries);

        assert_eq!(
            entries
                .iter()
                .map(|entry| entry.name.as_str())
                .collect::<Vec<_>>(),
            vec!["capdag", "src", "README.md"]
        );
    }

    #[test]
    fn load_workspace_submodules_returns_empty_for_non_root_path() {
        // Submodules are defined only at the workspace root. When listing a
        // subdirectory (e.g. a submodule's own contents), the root .gitmodules
        // must not bleed in — that caused infinite recursive tree rendering.
        let temp = TempDir::new().unwrap();
        fs::write(
            temp.path().join(".gitmodules"),
            r#"[submodule "child"]
	path = child
	url = https://github.com/example/child.git
"#,
        )
        .unwrap();
        // Create the submodule directory so it appears in visible_directory_paths
        fs::create_dir_all(temp.path().join("child")).unwrap();

        // Listing "child/" — its visible directories are workspace-relative like "child/foo"
        // Root submodules (path = "child") must NOT appear here.
        let submodule_visible_dirs = vec!["child/foo".to_string(), "child/src".to_string()];
        let result = load_workspace_submodules(temp.path(), &submodule_visible_dirs);
        assert!(
            result.is_empty(),
            "root .gitmodules must not inject submodules into subdirectory listings, got: {result:?}"
        );
    }

    #[test]
    fn load_uninitialized_submodules_only_reports_missing_root_children() {
        // "sub" is in .gitmodules but not on disk — it is uninitialized.
        // "initialized" is on disk and in .gitmodules — it is initialized.
        // The function returns only the missing ones when given the on-disk set.
        let temp = TempDir::new().unwrap();
        fs::write(
            temp.path().join(".gitmodules"),
            r#"[submodule "sub"]
	path = sub
	url = https://github.com/example/sub.git
[submodule "initialized"]
	path = initialized
	url = https://github.com/example/initialized.git
"#,
        )
        .unwrap();
        fs::create_dir_all(temp.path().join("initialized")).unwrap();

        // Root listing — visible dirs are direct children (single segment).
        let root_visible_dirs = vec!["initialized".to_string()];
        let result = load_uninitialized_submodules(temp.path(), &root_visible_dirs);
        assert_eq!(result.len(), 1, "only 'sub' is uninitialized");
        assert_eq!(result[0].path, "sub");
    }
}