mfs-cli 0.4.0-beta.1

MFS CLI — shell-native client for Multi-source File-like Search
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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
//! MFS CLI — thin HTTP client over the server's /v1 control plane.
//! Cold-start-fast single binary; all heavy work is server-side. Endpoint resolves
//! from $MFS_API_URL, else the active profile in ~/.mfs/client.toml, else default.

use clap::{Parser, Subcommand};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::BTreeMap;
use std::path::PathBuf;

#[derive(Parser)]
#[command(name = "mfs", version, about = "Multi-source File-like Search")]
struct Cli {
    #[command(subcommand)]
    cmd: Cmd,
    /// Output raw JSON envelope
    #[arg(long, global = true)]
    json: bool,
}

#[derive(Subcommand)]
enum Cmd {
    /// Register + sync a path or connector URI
    Add {
        target: String,
        /// Connector config TOML (schemas, [[objects]], _credential_ref, ...)
        #[arg(long)]
        config: Option<String>,
        /// Only index changes since this cursor/date (connectors with a time cursor)
        #[arg(long)]
        since: Option<String>,
        /// Force full re-index (ignore caches/fingerprints)
        #[arg(long, visible_alias = "full")]
        force_index: bool,
        /// Block until indexing finishes (poll the job); default returns immediately
        #[arg(long)]
        wait: bool,
        /// Bundle + upload the tree to the server even on the same host (no shared fs)
        #[arg(long)]
        upload: bool,
        /// Re-upload every file (skip the manifest diff) and force a full re-index
        #[arg(long)]
        force_upload: bool,
        /// Never upload; have the server read the path itself (shared fs)
        #[arg(long)]
        no_upload: bool,
        /// Skip the pre-flight estimate/confirm prompt for external connectors
        #[arg(long, short = 'y')]
        yes: bool,
    },
    /// Semantic + keyword search
    Search {
        query: String,
        /// Path/URI to scope the search to (required unless --all)
        path: Option<String>,
        /// Search the whole namespace instead of a scoped path
        #[arg(long)]
        all: bool,
        #[arg(long, default_value = "hybrid")]
        mode: String,
        #[arg(long, default_value_t = 10)]
        top_k: u32,
        /// Restrict to chunk kinds, comma-separated (e.g. body,directory_summary)
        #[arg(long)]
        kind: Option<String>,
        /// Collapse multiple hits from the same object into one
        #[arg(long)]
        collapse: bool,
    },
    /// Keyword / full-text search (pushdown -> BM25 -> linear)
    Grep { pattern: String, path: String },
    /// List children
    Ls { path: String },
    /// Recursively list a subtree
    Tree {
        path: String,
        #[arg(short = 'L', long, default_value_t = 2)]
        depth: u32,
    },
    /// Read an object
    Cat {
        path: String,
        #[arg(long)]
        range: Option<String>,
        #[arg(long)]
        meta: bool,
        /// Reopen a single structured record by its locator JSON, e.g. '{"pk":{"id":12}}'
        #[arg(long)]
        locator: Option<String>,
        /// Skeleton view: headings/symbols only
        #[arg(long)]
        peek: bool,
        /// peek + one-line summaries
        #[arg(long)]
        skim: bool,
    },
    /// First N lines of an object
    Head {
        path: String,
        #[arg(short = 'n', long, default_value_t = 20)]
        lines: usize,
    },
    /// Last N lines of an object
    Tail {
        path: String,
        #[arg(short = 'n', long, default_value_t = 20)]
        lines: usize,
    },
    /// Export an object's full content to a file
    Export { path: String, out: String },
    /// Server / connector / job status
    Status,
    /// Job inspection / control
    Job {
        #[command(subcommand)]
        action: JobAction,
    },
    /// Connector management
    Connector {
        #[command(subcommand)]
        action: ConnectorAction,
    },
    /// Remove a connector + everything it owns (alias for `connector remove`)
    Remove {
        target: String,
        /// Skip the confirmation prompt
        #[arg(long, short = 'y')]
        yes: bool,
    },
    /// Client profile (endpoint) management — ~/.mfs/client.toml
    Profile {
        #[command(subcommand)]
        action: ProfileAction,
    },
    /// Show client/server config
    Config {
        #[command(subcommand)]
        action: ConfigAction,
    },
    /// Manage a local mfs-server process
    Serve {
        #[command(subcommand)]
        action: ServeAction,
    },
}

#[derive(Subcommand)]
enum JobAction {
    /// List recent jobs
    List,
    /// Show a job by id
    Show { job_id: String },
    /// Cancel a running/queued job
    Cancel { job_id: String },
}

#[derive(Subcommand)]
enum ConfigAction {
    /// Show resolved endpoint + server info
    Show,
}

#[derive(Subcommand)]
enum ConnectorAction {
    /// Register + sync a connector (alias: `mfs add`)
    Add {
        target: String,
        #[arg(long)]
        config: Option<String>,
    },
    /// Try-connect a connector without registering
    Probe {
        target: String,
        #[arg(long)]
        config: Option<String>,
    },
    /// List registered connectors
    List,
    /// Show a connector's objects/jobs summary
    Inspect { target: String },
    /// Re-sync a connector (alias: `mfs add <uri>`)
    Update {
        target: String,
        #[arg(long)]
        config: Option<String>,
    },
    /// Remove a connector and everything it owns
    Remove {
        target: String,
        /// Skip the confirmation prompt
        #[arg(long, short = 'y')]
        yes: bool,
    },
}

#[derive(Subcommand)]
enum ProfileAction {
    /// List profiles
    List,
    /// Add (or update) a profile
    Add {
        name: String,
        url: String,
        /// Bearer token for remote auth (literal or env:VAR)
        #[arg(long)]
        token: Option<String>,
    },
    /// Set the active profile
    Use { name: String },
}

#[derive(Subcommand)]
enum ServeAction {
    /// Start a local mfs-server (detached)
    Start {
        #[arg(long, default_value = "127.0.0.1:8765")]
        bind: String,
    },
    /// Stop the local mfs-server
    Stop,
    /// Restart the local mfs-server
    Restart {
        #[arg(long, default_value = "127.0.0.1:8765")]
        bind: String,
    },
    /// Is the local mfs-server running?
    Status,
    /// Tail the local server log
    Logs,
}

// ---------- profile (client.toml) ----------
#[derive(Serialize, Deserialize, Default)]
struct ClientConfig {
    active: Option<String>,
    /// Stable client identity (UUID), generated once. Survives hostname/container churn
    /// — machine-id (hostname) is only used to decide local-vs-remote.
    #[serde(default)]
    client_id: Option<String>,
    #[serde(default)]
    profiles: BTreeMap<String, Profile>,
}

/// Load (or generate + persist) the stable client_id from client.toml.
fn client_id() -> String {
    let mut cfg = load_client_cfg();
    if let Some(id) = &cfg.client_id {
        if !id.is_empty() {
            return id.clone();
        }
    }
    let id = std::fs::read_to_string("/proc/sys/kernel/random/uuid")
        .ok()
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .unwrap_or_else(|| format!("cid-{}", std::process::id()));
    cfg.client_id = Some(id.clone());
    let _ = save_client_cfg(&cfg);
    id
}

#[derive(Serialize, Deserialize, Clone)]
struct Profile {
    url: String,
    /// Bearer token for remote auth; literal or `env:VAR`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    token: Option<String>,
}

/// Active Bearer token: $MFS_API_TOKEN, else the active profile's token (env: resolved).
fn auth_token() -> Option<String> {
    if let Ok(t) = std::env::var("MFS_API_TOKEN") {
        if !t.is_empty() {
            return Some(t);
        }
    }
    let cfg = load_client_cfg();
    if let Some(raw) = cfg
        .active
        .as_ref()
        .and_then(|a| cfg.profiles.get(a))
        .and_then(|p| p.token.clone())
    {
        return Some(match raw.strip_prefix("env:") {
            Some(var) => std::env::var(var).unwrap_or_default(),
            None => raw,
        });
    }
    // Fall back to the local server's auto-generated token: `mfs-server
    // run` writes ~/.mfs/server.token, so a CLI on the same host authenticates to its
    // loopback server with zero configuration.
    std::fs::read_to_string(mfs_home().join("server.token"))
        .ok()
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
}

/// A remote endpoint (non-loopback) means the server can't see local paths. Rewrite an
/// existing local path to its stable upload identity file://<client_id><abs> so browse/
/// search/remove hit the connector created by `mfs add --upload`.
fn is_remote(base: &str) -> bool {
    !(base.contains("127.0.0.1") || base.contains("localhost") || base.contains("[::1]"))
}

fn remote_path(base: &str, path: &str) -> String {
    if is_remote(base) {
        if let Ok(abs) = std::fs::canonicalize(path) {
            return format!("file://{}{}", client_id(), abs.to_string_lossy());
        }
    }
    path.to_string()
}

fn with_auth(rb: reqwest::blocking::RequestBuilder) -> reqwest::blocking::RequestBuilder {
    match auth_token() {
        Some(t) if !t.is_empty() => rb.bearer_auth(t),
        _ => rb,
    }
}

fn mfs_home() -> PathBuf {
    let home = std::env::var("MFS_HOME")
        .or_else(|_| std::env::var("HOME").map(|h| format!("{h}/.mfs")))
        .unwrap_or_else(|_| ".mfs".to_string());
    PathBuf::from(home)
}

fn client_cfg_path() -> PathBuf {
    mfs_home().join("client.toml")
}

fn load_client_cfg() -> ClientConfig {
    let p = client_cfg_path();
    std::fs::read_to_string(p)
        .ok()
        .and_then(|s| toml::from_str(&s).ok())
        .unwrap_or_default()
}

fn save_client_cfg(cfg: &ClientConfig) -> Result<(), String> {
    let dir = mfs_home();
    std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?;
    let s = toml::to_string_pretty(cfg).map_err(|e| e.to_string())?;
    std::fs::write(client_cfg_path(), s).map_err(|e| e.to_string())
}

fn base_url() -> String {
    if let Ok(u) = std::env::var("MFS_API_URL") {
        return u;
    }
    let cfg = load_client_cfg();
    if let Some(active) = &cfg.active {
        if let Some(p) = cfg.profiles.get(active) {
            return p.url.clone();
        }
    }
    "http://127.0.0.1:8765".to_string()
}

fn main() {
    let cli = Cli::parse();
    let client = reqwest::blocking::Client::new();
    let base = base_url();
    if let Err(e) = run(&cli, &client, &base) {
        eprintln!("error: {e}");
        std::process::exit(1);
    }
}

fn run(cli: &Cli, client: &reqwest::blocking::Client, base: &str) -> Result<(), String> {
    match &cli.cmd {
        Cmd::Add {
            target,
            config,
            since,
            force_index,
            wait,
            upload,
            force_upload,
            no_upload,
            yes,
        } => {
            let is_local = std::path::Path::new(target).exists();
            // zero-billing estimate + confirm before indexing an external connector
            //: the user sees physical work before any embedding spend.
            if !is_local && !yes {
                let mut eb = serde_json::json!({"target": target});
                if let Some(c) = config {
                    eb["config"] = load_config_file(c)?;
                }
                let est = post(client, &format!("{base}/v1/connectors/estimate"), &eb)?;
                println!("Connector: {target}");
                println!("Discovered: {} objects", est["objects"]);
                println!("Estimated (local chunker + tokenizer only — no embedding API calls):");
                println!("  chunks: ~{}", est["est_chunks"]);
                println!(
                    "  tokens: ~{}  (apply your provider's per-token rate to estimate $)",
                    est["est_tokens"]
                );
                if !confirm("Continue? [y/N] ")? {
                    println!("aborted.");
                    return Ok(());
                }
            }
            // local/remote decision: when the target is a real local path
            // and the server runs on a different host (no shared fs), bundle the tree and
            // upload it instead of asking the server to read a path it can't see. --upload
            // forces it on the same host; --no-upload always has the server read the path.
            let do_upload = if *no_upload {
                false
            } else if *upload || *force_upload {
                is_local
            } else if is_local {
                let server_mid = get(client, &format!("{base}/v1/server/info"), &[])
                    .ok()
                    .and_then(|v| v["machine_id"].as_str().map(String::from))
                    .unwrap_or_default();
                let client_host = client_hostname();
                !server_mid.is_empty() && !client_host.is_empty() && server_mid != client_host
            } else {
                false
            };
            // add is async: the server enqueues and returns a job_id immediately; the
            // worker (in-process for the single-binary, dedicated in CS) drains it in the
            // background. --wait polls the job to completion for scripts/CI that must block.
            let job_id = if do_upload {
                // --force-upload re-sends every file AND forces a re-index; --force-index
                // alone re-indexes the already-staged tree without re-sending bytes.
                upload_path(
                    client,
                    base,
                    target,
                    *force_index || *force_upload,
                    *force_upload,
                    cli.json,
                )?
            } else {
                let mut body =
                    serde_json::json!({"target": target, "full": force_index, "process": false});
                if let Some(c) = config {
                    body["config"] = load_config_file(c)?;
                }
                if let Some(s) = since {
                    body["since"] = Value::String(s.clone());
                }
                let v = post(client, &format!("{base}/v1/add"), &body)?;
                v["job_id"].as_str().unwrap_or("").to_string()
            };
            if *wait {
                wait_for_job(client, base, &job_id, cli.json)?;
            } else if cli.json {
                println!("{}", serde_json::json!({"job_id": job_id}));
            } else {
                println!("queued (job {job_id}). Worker running in background — run `mfs status` to check progress.");
            }
        }
        Cmd::Search {
            query,
            path,
            all,
            mode,
            top_k,
            kind,
            collapse,
        } => {
            if path.is_none() && !all {
                return Err(
                    "specify a path to scope the search, or --all for the whole namespace".into(),
                );
            }
            let mut q = vec![
                ("q", query.clone()),
                ("mode", mode.clone()),
                ("top_k", top_k.to_string()),
            ];
            if let Some(p) = path {
                q.push(("path", remote_path(base, p)));
            }
            if let Some(k) = kind {
                q.push(("kind", k.clone()));
            }
            if *collapse {
                q.push(("collapse", "true".to_string()));
            }
            let v = get(client, &format!("{base}/v1/search"), &q)?;
            if cli.json {
                println!("{v}");
                return Ok(());
            }
            for hit in v["results"].as_array().unwrap_or(&vec![]) {
                println!(
                    "{}  score={}",
                    hit["source"].as_str().unwrap_or("?"),
                    hit["score"].as_f64().unwrap_or(0.0)
                );
                if let Some(c) = hit["content"].as_str() {
                    println!(
                        "   {}",
                        c.lines()
                            .next()
                            .unwrap_or("")
                            .chars()
                            .take(100)
                            .collect::<String>()
                    );
                }
            }
        }
        Cmd::Grep { pattern, path } => {
            let v = get(
                client,
                &format!("{base}/v1/grep"),
                &[
                    ("pattern", pattern.clone()),
                    ("path", remote_path(base, path)),
                ],
            )?;
            if cli.json {
                println!("{v}");
                return Ok(());
            }
            for hit in v["results"].as_array().unwrap_or(&vec![]) {
                println!(
                    "{}: {}",
                    hit["source"].as_str().unwrap_or("?"),
                    hit["content"]
                        .as_str()
                        .unwrap_or("")
                        .chars()
                        .take(120)
                        .collect::<String>()
                );
            }
        }
        Cmd::Ls { path } => {
            let v = get(
                client,
                &format!("{base}/v1/ls"),
                &[("path", remote_path(base, path))],
            )?;
            if cli.json {
                println!("{v}");
                return Ok(());
            }
            print_entries(&v);
        }
        Cmd::Tree { path, depth } => {
            let rp = remote_path(base, path);
            let v = get(client, &format!("{base}/v1/ls"), &[("path", rp.clone())])?;
            if cli.json {
                println!("{v}");
                return Ok(());
            }
            println!("{path}");
            tree(client, base, &rp, *depth, "")?;
        }
        Cmd::Cat {
            path,
            range,
            meta,
            locator,
            peek,
            skim,
        } => {
            let mut q = vec![("path", remote_path(base, path))];
            if let Some(r) = range {
                q.push(("range", r.clone()));
            }
            if *meta {
                q.push(("meta", "true".to_string()));
            }
            if let Some(l) = locator {
                q.push(("locator", l.clone()));
            }
            if *peek {
                q.push(("density", "peek".to_string()));
            }
            if *skim {
                q.push(("density", "skim".to_string()));
            }
            let v = get(client, &format!("{base}/v1/cat"), &q)?;
            if cli.json {
                println!("{v}");
                return Ok(());
            }
            if *meta {
                println!("{v}");
            } else {
                println!("{}", v["content"].as_str().unwrap_or(""));
            }
        }
        Cmd::Head { path, lines } => {
            let v = get(
                client,
                &format!("{base}/v1/head"),
                &[("path", remote_path(base, path)), ("n", lines.to_string())],
            )?;
            if cli.json {
                println!("{v}");
            } else {
                println!("{}", v["content"].as_str().unwrap_or(""));
            }
        }
        Cmd::Tail { path, lines } => {
            let v = get(
                client,
                &format!("{base}/v1/tail"),
                &[("path", remote_path(base, path)), ("n", lines.to_string())],
            )?;
            if cli.json {
                println!("{v}");
            } else {
                println!("{}", v["content"].as_str().unwrap_or(""));
            }
        }
        Cmd::Export { path, out } => {
            // export returns the FULL object (no row cap / size guard), unlike cat
            let v = get(
                client,
                &format!("{base}/v1/export"),
                &[("path", remote_path(base, path))],
            )?;
            let text = v["content"].as_str().unwrap_or("");
            std::fs::write(out, text).map_err(|e| e.to_string())?;
            println!("exported {} bytes -> {}", text.len(), out);
        }
        Cmd::Status => {
            let v = get(client, &format!("{base}/v1/status"), &[])?;
            println!("{}", serde_json::to_string_pretty(&v).unwrap_or_default());
        }
        Cmd::Job { action } => match action {
            JobAction::List => {
                let v = get(client, &format!("{base}/v1/jobs"), &[])?;
                if cli.json {
                    println!("{v}");
                    return Ok(());
                }
                for j in v.as_array().unwrap_or(&vec![]) {
                    println!(
                        "{:8}  {:10}  {}",
                        j["status"].as_str().unwrap_or("?"),
                        j["op_kind"].as_str().unwrap_or("?"),
                        j["id"].as_str().unwrap_or("?")
                    );
                }
            }
            JobAction::Show { job_id } => {
                let v = get(client, &format!("{base}/v1/jobs/{job_id}"), &[])?;
                println!("{}", serde_json::to_string_pretty(&v).unwrap_or_default());
            }
            JobAction::Cancel { job_id } => {
                let v = post(
                    client,
                    &format!("{base}/v1/jobs/{job_id}/cancel"),
                    &serde_json::json!({}),
                )?;
                println!("cancelled: {}", v["cancelled"].as_bool().unwrap_or(false));
            }
        },
        Cmd::Connector { action } => match action {
            ConnectorAction::Add { target, config } => {
                let mut body = serde_json::json!({"target": target});
                if let Some(c) = config {
                    body["config"] = load_config_file(c)?;
                }
                let v = post(client, &format!("{base}/v1/add"), &body)?;
                println!("job: {}", v["job_id"].as_str().unwrap_or("?"));
            }
            ConnectorAction::Update { target, config } => {
                // update applies the new config to the existing connector (add ignores --config)
                let mut body = serde_json::json!({"target": target, "update": true});
                if let Some(c) = config {
                    body["config"] = load_config_file(c)?;
                }
                let v = post(client, &format!("{base}/v1/add"), &body)?;
                println!("job: {}", v["job_id"].as_str().unwrap_or("?"));
            }
            ConnectorAction::Probe { target, config } => {
                let mut body = serde_json::json!({"target": target});
                if let Some(c) = config {
                    body["config"] = load_config_file(c)?;
                }
                let v = post(client, &format!("{base}/v1/connectors/probe"), &body)?;
                println!(
                    "{}  ok={}  {}",
                    v["type"].as_str().unwrap_or("?"),
                    v["ok"].as_bool().unwrap_or(false),
                    v["detail"].as_str().unwrap_or("")
                );
            }
            ConnectorAction::List => {
                let v = get(client, &format!("{base}/v1/status"), &[])?;
                if cli.json {
                    println!("{}", v["connectors"]);
                    return Ok(());
                }
                for c in v["connectors"].as_array().unwrap_or(&vec![]) {
                    println!(
                        "{:10}  {:8}  {}",
                        c["type"].as_str().unwrap_or("?"),
                        c["status"].as_str().unwrap_or("?"),
                        c["root_uri"].as_str().unwrap_or("?")
                    );
                }
            }
            ConnectorAction::Inspect { target } => {
                let v = get(
                    client,
                    &format!("{base}/v1/connectors/inspect"),
                    &[("target", target.clone())],
                )?;
                println!("{}", serde_json::to_string_pretty(&v).unwrap_or_default());
            }
            ConnectorAction::Remove { target, yes } => {
                return remove_connector(client, base, target, *yes)
            }
        },
        Cmd::Remove { target, yes } => return remove_connector(client, base, target, *yes),
        Cmd::Profile { action } => return profile_cmd(action),
        Cmd::Config { action } => match action {
            ConfigAction::Show => {
                println!("endpoint: {base}");
                let cfg = load_client_cfg();
                println!(
                    "active profile: {}",
                    cfg.active.as_deref().unwrap_or("(none)")
                );
                println!("client_id: {}", client_id());
                match get(client, &format!("{base}/v1/server/info"), &[]) {
                    Ok(v) => println!("server: {}", serde_json::to_string(&v).unwrap_or_default()),
                    Err(e) => println!("server: unreachable ({e})"),
                }
            }
        },
        Cmd::Serve { action } => return serve_cmd(action),
    }
    Ok(())
}

/// Prompt on stderr/stdout and read a yes/no from stdin.
fn confirm(prompt: &str) -> Result<bool, String> {
    use std::io::Write;
    print!("{prompt}");
    std::io::stdout().flush().ok();
    let mut s = String::new();
    std::io::stdin()
        .read_line(&mut s)
        .map_err(|e| e.to_string())?;
    Ok(matches!(s.trim().to_lowercase().as_str(), "y" | "yes"))
}

/// Best-effort client machine id (matches the server's socket.gethostname()).
fn client_hostname() -> String {
    std::process::Command::new("hostname")
        .output()
        .ok()
        .and_then(|o| String::from_utf8(o.stdout).ok())
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .or_else(|| std::env::var("HOSTNAME").ok())
        .unwrap_or_default()
}

/// One file's stat from the client scan.
struct ScanEntry {
    rel: String,
    size: u64,
    mtime_ns: i64,
    inode: u64,
}

/// Walk a directory (skipping noisy dirs) collecting per-file stat (rel path, size,
/// mtime_ns, inode). Mirrors the server file connector's default ignores roughly.
fn scan_tree(root: &std::path::Path) -> Result<Vec<ScanEntry>, String> {
    use std::os::unix::fs::MetadataExt;
    const SKIP: &[&str] = &[
        ".git",
        "node_modules",
        "__pycache__",
        ".venv",
        "venv",
        ".mypy_cache",
        ".pytest_cache",
        ".ruff_cache",
        ".idea",
        ".vscode",
    ];
    let mut out = Vec::new();
    let mut stack = vec![root.to_path_buf()];
    while let Some(dir) = stack.pop() {
        let rd = std::fs::read_dir(&dir).map_err(|e| format!("scan {}: {e}", dir.display()))?;
        for ent in rd {
            let ent = ent.map_err(|e| e.to_string())?;
            let path = ent.path();
            let md = ent.metadata().map_err(|e| e.to_string())?;
            let name = ent.file_name().to_string_lossy().to_string();
            if md.is_dir() {
                if !SKIP.contains(&name.as_str()) {
                    stack.push(path);
                }
            } else if md.is_file() {
                let rel = path
                    .strip_prefix(root)
                    .map_err(|e| e.to_string())?
                    .to_string_lossy()
                    .replace('\\', "/");
                out.push(ScanEntry {
                    rel,
                    size: md.size(),
                    // ns since epoch fits i64 until year ~2262; send as a JSON number so it
                    // round-trips to the server's int field and stat-compares cleanly next sync
                    mtime_ns: md.mtime() * 1_000_000_000 + md.mtime_nsec(),
                    inode: md.ino(),
                });
            }
        }
    }
    Ok(out)
}

fn sha1_file(path: &std::path::Path) -> Result<String, String> {
    use sha1::{Digest, Sha1};
    let bytes = std::fs::read(path).map_err(|e| e.to_string())?;
    let mut h = Sha1::new();
    h.update(&bytes);
    Ok(format!("{:x}", h.finalize()))
}

/// Manifest-diff upload: scan -> POST /v1/files/manifest -> sha1 the
/// needed files + pair renames by (inode,size,sha1) -> PUT /v1/files/upload a tar.gz
/// carrying `.mfs-meta.json` + only the changed bytes. The client keeps no state.
fn upload_path(
    client: &reqwest::blocking::Client,
    base: &str,
    target: &str,
    full: bool,
    resend_all: bool,
    json: bool,
) -> Result<String, String> {
    use std::io::Write;
    let root = std::path::Path::new(target);
    let client_id = client_id(); // stable UUID identity, not the hostname
                                 // absolute path is the connector's stable identity file://<client_id><abs>
    let abs_root = std::fs::canonicalize(root)
        .map_err(|e| e.to_string())?
        .to_string_lossy()
        .to_string();

    // ① scan (stat only)
    let entries = scan_tree(root)?;
    let files: Vec<Value> = entries
        .iter()
        .map(|e| {
            serde_json::json!(
        {"path": e.rel, "size": e.size, "mtime_ns": e.mtime_ns, "inode": e.inode})
        })
        .collect();

    // ② manifest diff
    let mf = post(
        client,
        &format!("{base}/v1/files/manifest"),
        &serde_json::json!({"client_id": client_id, "root": abs_root, "files": files}),
    )?;
    // --force-upload (resend_all): ignore the server's diff and re-send every file's bytes.
    let need: std::collections::HashSet<String> = if resend_all {
        entries.iter().map(|e| e.rel.clone()).collect()
    } else {
        mf["need_sha1"]
            .as_array()
            .unwrap_or(&vec![])
            .iter()
            .filter_map(|v| v.as_str().map(String::from))
            .collect()
    };
    let del_cands = mf["deletion_candidates"]
        .as_array()
        .cloned()
        .unwrap_or_default();

    // ③ sha1 the needed files; pair renames against deletion candidates (inode+size+sha1)
    let by_rel: std::collections::HashMap<&str, &ScanEntry> =
        entries.iter().map(|e| (e.rel.as_str(), e)).collect();
    let mut hashes: Vec<Value> = Vec::new();
    let mut sha_of: std::collections::HashMap<String, String> = std::collections::HashMap::new();
    for rel in &need {
        let e = by_rel[rel.as_str()];
        let sha = sha1_file(&root.join(rel))?;
        sha_of.insert(rel.clone(), sha.clone());
        hashes.push(serde_json::json!(
            {"path": rel, "sha1": sha, "size": e.size, "mtime_ns": e.mtime_ns, "inode": e.inode}));
    }
    let mut renames: Vec<Value> = Vec::new();
    let mut consumed_old: std::collections::HashSet<String> = std::collections::HashSet::new();
    let mut renamed_new: std::collections::HashSet<String> = std::collections::HashSet::new();
    for rel in &need {
        let e = by_rel[rel.as_str()];
        let sha = &sha_of[rel];
        for dc in &del_cands {
            let old = dc["path"].as_str().unwrap_or("");
            if old.is_empty() || consumed_old.contains(old) {
                continue;
            }
            // inode+size first; fall back to sha1+size so a cross-filesystem rename (inode
            // changes) is still recognized as a rename instead of delete+add — which would
            // re-upload and re-embed identical bytes.
            let size_match = dc["size"].as_u64() == Some(e.size);
            let same = size_match
                && (dc["inode"].as_u64() == Some(e.inode)
                    || dc["sha1"].as_str() == Some(sha.as_str()));
            if same {
                renames.push(serde_json::json!({"old": old, "new": rel, "sha1": sha}));
                consumed_old.insert(old.to_string());
                renamed_new.insert(rel.clone());
                break;
            }
        }
    }
    let deletions: Vec<String> = del_cands
        .iter()
        .filter_map(|dc| dc["path"].as_str().map(String::from))
        .filter(|p| !consumed_old.contains(p))
        .collect();

    // ④ build tar.gz: .mfs-meta.json + changed bytes (renamed files send no bytes)
    let meta = serde_json::json!({"hashes": hashes, "renames": renames, "deletions": deletions});
    let buf = Vec::new();
    let enc = flate2::write::GzEncoder::new(buf, flate2::Compression::default());
    let mut tar = tar::Builder::new(enc);
    let meta_bytes = serde_json::to_vec(&meta).map_err(|e| e.to_string())?;
    let mut hdr = tar::Header::new_gnu();
    hdr.set_size(meta_bytes.len() as u64);
    hdr.set_mode(0o644);
    hdr.set_cksum();
    tar.append_data(&mut hdr, ".mfs-meta.json", &meta_bytes[..])
        .map_err(|e| e.to_string())?;
    for rel in &need {
        if !resend_all && renamed_new.contains(rel) {
            continue;
        } // moved on server, no bytes
        tar.append_path_with_name(root.join(rel), rel)
            .map_err(|e| e.to_string())?;
    }
    let gz = tar.into_inner().map_err(|e| e.to_string())?;
    let data = gz.finish().map_err(|e| e.to_string())?;
    let _ = std::io::stdout().flush();

    let resp = with_auth(
        client
            .put(format!("{base}/v1/files/upload"))
            .query(&[
                ("client_id", client_id.as_str()),
                ("root", abs_root.as_str()),
                ("process", "false"),
                ("full", &full.to_string()),
            ])
            .body(data),
    )
    .send()
    .map_err(|e| e.to_string())?;
    let v = parse(resp)?;
    if !json {
        println!(
            "uploaded {} changed, {} renamed, {} deleted",
            need.len() - renamed_new.len(),
            renames.len(),
            deletions.len()
        );
    }
    Ok(v["job_id"].as_str().unwrap_or("").to_string())
}

/// Poll a sync job to a terminal state (for `mfs add --wait`). The HTTP request itself is
/// short — we never hold a long connection open, so a slow index can't time the client out.
fn wait_for_job(
    client: &reqwest::blocking::Client,
    base: &str,
    job_id: &str,
    json: bool,
) -> Result<(), String> {
    loop {
        let v = get(client, &format!("{base}/v1/jobs/{job_id}"), &[])?;
        match v["status"].as_str().unwrap_or("") {
            "succeeded" => {
                if json {
                    println!("{v}");
                } else {
                    println!(
                        "done: {} of {} objects indexed, {} failed",
                        v["succeeded_objects"].as_i64().unwrap_or(0),
                        v["total_objects"].as_i64().unwrap_or(0),
                        v["failed_objects"].as_i64().unwrap_or(0)
                    );
                }
                return Ok(());
            }
            "failed" | "cancelled" => {
                return Err(format!(
                    "job {}: {}",
                    v["status"].as_str().unwrap_or("?"),
                    v["error"].as_str().unwrap_or("")
                ));
            }
            _ => std::thread::sleep(std::time::Duration::from_millis(1000)),
        }
    }
}

/// Load a connector config TOML file and convert it to a JSON value for the /v1/add body.
fn load_config_file(path: &str) -> Result<Value, String> {
    let text = std::fs::read_to_string(path).map_err(|e| format!("read {path}: {e}"))?;
    let toml_val: toml::Value = toml::from_str(&text).map_err(|e| format!("parse {path}: {e}"))?;
    serde_json::to_value(toml_val).map_err(|e| e.to_string())
}

fn remove_connector(
    client: &reqwest::blocking::Client,
    base: &str,
    target: &str,
    yes: bool,
) -> Result<(), String> {
    // remove is destructive (drops the index, artifacts, and all metadata); confirm unless -y.
    if !yes
        && !confirm(&format!(
            "Remove connector '{target}' and everything it owns? [y/N] "
        ))?
    {
        println!("aborted.");
        return Ok(());
    }
    let target = remote_path(base, target); // local path -> upload identity when remote
    let resp = with_auth(
        client
            .delete(format!("{base}/v1/connectors"))
            .query(&[("target", target.as_str())]),
    )
    .send()
    .map_err(|e| e.to_string())?;
    let v = parse(resp)?;
    println!("removed: {}", v["removed"].as_bool().unwrap_or(false));
    Ok(())
}

fn print_entries(v: &Value) {
    for e in v["entries"].as_array().unwrap_or(&vec![]) {
        println!(
            "{:4}  {}",
            e["type"].as_str().unwrap_or(""),
            e["name"].as_str().unwrap_or("")
        );
    }
}

fn tree(
    client: &reqwest::blocking::Client,
    base: &str,
    path: &str,
    depth: u32,
    prefix: &str,
) -> Result<(), String> {
    if depth == 0 {
        return Ok(());
    }
    let v = get(
        client,
        &format!("{base}/v1/ls"),
        &[("path", path.to_string())],
    )?;
    let entries = v["entries"].as_array().cloned().unwrap_or_default();
    let n = entries.len();
    for (i, e) in entries.iter().enumerate() {
        let name = e["name"].as_str().unwrap_or("");
        let is_dir = e["type"].as_str() == Some("dir");
        let last = i + 1 == n;
        let branch = if last { "└── " } else { "├── " };
        println!("{prefix}{branch}{name}{}", if is_dir { "/" } else { "" });
        if is_dir {
            let child = format!("{}/{}", path.trim_end_matches('/'), name);
            let next_prefix = format!("{prefix}{}", if last { "    " } else { "" });
            tree(client, base, &child, depth - 1, &next_prefix)?;
        }
    }
    Ok(())
}

fn profile_cmd(action: &ProfileAction) -> Result<(), String> {
    let mut cfg = load_client_cfg();
    match action {
        ProfileAction::List => {
            for (name, p) in &cfg.profiles {
                let marker = if cfg.active.as_deref() == Some(name) {
                    "*"
                } else {
                    " "
                };
                println!("{marker} {name:12} {}", p.url);
            }
            if cfg.profiles.is_empty() {
                println!("(no profiles; using {})", base_url());
            }
        }
        ProfileAction::Add { name, url, token } => {
            cfg.profiles.insert(
                name.clone(),
                Profile {
                    url: url.clone(),
                    token: token.clone(),
                },
            );
            if cfg.active.is_none() {
                cfg.active = Some(name.clone());
            }
            save_client_cfg(&cfg)?;
            println!(
                "profile '{name}' -> {url}{}",
                if token.is_some() { " (token set)" } else { "" }
            );
        }
        ProfileAction::Use { name } => {
            if !cfg.profiles.contains_key(name) {
                return Err(format!("no such profile: {name}"));
            }
            cfg.active = Some(name.clone());
            save_client_cfg(&cfg)?;
            println!("active profile: {name}");
        }
    }
    Ok(())
}

fn serve_cmd(action: &ServeAction) -> Result<(), String> {
    let pid_file = mfs_home().join("server.pid");
    let log_file = mfs_home().join("server.log");
    match action {
        ServeAction::Start { bind } => {
            if let Some(pid) = read_pid(&pid_file) {
                if pid_alive(pid) {
                    println!("already running (pid {pid})");
                    return Ok(());
                }
            }
            std::fs::create_dir_all(mfs_home()).map_err(|e| e.to_string())?;
            let log = std::fs::File::create(&log_file).map_err(|e| e.to_string())?;
            let log_err = log.try_clone().map_err(|e| e.to_string())?;
            let child = std::process::Command::new("mfs-server")
                .args(["run", "--bind", bind])
                .stdout(std::process::Stdio::from(log))
                .stderr(std::process::Stdio::from(log_err))
                .spawn()
                .map_err(|e| format!("failed to spawn mfs-server: {e}"))?;
            std::fs::write(&pid_file, child.id().to_string()).map_err(|e| e.to_string())?;
            println!(
                "started mfs-server (pid {}) on {bind}; logs: {}",
                child.id(),
                log_file.display()
            );
        }
        ServeAction::Stop => match read_pid(&pid_file) {
            Some(pid) => {
                let _ = std::process::Command::new("kill")
                    .arg(pid.to_string())
                    .status();
                let _ = std::fs::remove_file(&pid_file);
                println!("stopped (pid {pid})");
            }
            None => println!("not running"),
        },
        ServeAction::Restart { bind } => {
            if let Some(pid) = read_pid(&pid_file) {
                let _ = std::process::Command::new("kill")
                    .arg(pid.to_string())
                    .status();
                let _ = std::fs::remove_file(&pid_file);
            }
            return serve_cmd(&ServeAction::Start { bind: bind.clone() });
        }
        ServeAction::Status => match read_pid(&pid_file) {
            Some(pid) if pid_alive(pid) => println!("running (pid {pid})"),
            _ => println!("not running"),
        },
        ServeAction::Logs => {
            let s = std::fs::read_to_string(&log_file).unwrap_or_default();
            for l in s
                .lines()
                .rev()
                .take(40)
                .collect::<Vec<_>>()
                .into_iter()
                .rev()
            {
                println!("{l}");
            }
        }
    }
    Ok(())
}

fn read_pid(p: &PathBuf) -> Option<u32> {
    std::fs::read_to_string(p)
        .ok()
        .and_then(|s| s.trim().parse().ok())
}

fn pid_alive(pid: u32) -> bool {
    std::process::Command::new("kill")
        .args(["-0", &pid.to_string()])
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

fn get(
    client: &reqwest::blocking::Client,
    url: &str,
    q: &[(&str, String)],
) -> Result<Value, String> {
    let resp = with_auth(client.get(url).query(q))
        .send()
        .map_err(|e| e.to_string())?;
    parse(resp)
}

fn post(client: &reqwest::blocking::Client, url: &str, body: &Value) -> Result<Value, String> {
    let resp = with_auth(client.post(url).json(body))
        .send()
        .map_err(|e| e.to_string())?;
    parse(resp)
}

fn parse(resp: reqwest::blocking::Response) -> Result<Value, String> {
    let status = resp.status();
    let v: Value = resp.json().map_err(|e| e.to_string())?;
    if !status.is_success() {
        // surface the stable error `code` alongside the human detail
        let code = v.get("code").and_then(|c| c.as_str()).unwrap_or("");
        let detail = v
            .get("detail")
            .and_then(|d| d.as_str())
            .unwrap_or("request failed");
        return Err(if code.is_empty() {
            format!("{status}: {detail}")
        } else {
            format!("{status} [{code}]: {detail}")
        });
    }
    Ok(v)
}