gaze-cli 0.11.3

Gaze command-line interface
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
mod audit;
mod clean;
mod daemon;
#[cfg(feature = "document")]
mod document;
#[cfg(feature = "index")]
mod index;
#[cfg(feature = "mcp")]
mod mcp;
#[cfg(feature = "proxy")]
mod proxy;
mod restore;
#[cfg(feature = "setup")]
mod setup;

use std::path::PathBuf;

use clap::{Parser, Subcommand, ValueEnum};

use crate::error::{CliError, RestoreMode};
use crate::io::DEFAULT_MAX_BYTES;

pub(crate) const DEFAULT_SAFETY_NET_TIMEOUT_MS: u64 = 5_000;
pub(crate) const DEFAULT_SAFETY_NET_INPUT_LIMIT_BYTES: usize = 1_048_576;

#[derive(Parser, Debug)]
#[command(
    name = "gaze",
    version,
    about = "Channel-agnostic PII redaction for LLM pipes"
)]
pub(crate) struct Cli {
    #[command(subcommand)]
    cmd: Cmd,
}

#[derive(Subcommand, Debug)]
#[allow(clippy::large_enum_variant)]
enum Cmd {
    /// Read raw text from stdin; emit `{clean_text, session_blob, stats}` JSON to stdout.
    Clean {
        /// Path to policy.toml. Required once the policy loader lands (issue #3).
        #[arg(long)]
        policy: Option<PathBuf>,
        /// Output format. Only `json` is supported today.
        #[arg(long, default_value = "json")]
        format: String,
        /// Override the persistent session TTL in seconds.
        #[arg(long)]
        session_ttl: Option<u64>,
        /// Override policy \[session].scope.
        #[arg(long)]
        session_scope: Option<String>,
        /// Active locale fallback chain, comma separated and priority ordered.
        #[arg(long, value_delimiter = ',')]
        locale: Vec<String>,
        /// Override policy \[ner] threshold. Must be between 0.0 and 1.0 inclusive.
        #[arg(long)]
        ner_threshold: Option<f32>,
        /// Override policy \[ner].model_dir.
        #[arg(long)]
        ner_model_dir: Option<PathBuf>,
        /// Override policy \[ner].locale.
        #[arg(long)]
        ner_locale: Option<String>,
        /// Override policy.rulepacks.bundled. Comma-separated and repeatable.
        #[arg(long, value_delimiter = ',')]
        rulepack_bundled: Vec<String>,
        /// Override policy.rulepacks.paths. Repeatable.
        #[arg(long = "rulepack-path")]
        rulepack_paths: Vec<PathBuf>,
        /// Max stdin size in bytes. stdin longer than this exits 1 InputTooLarge.
        #[arg(long, default_value_t = DEFAULT_MAX_BYTES)]
        max_bytes: u64,
        /// Path to a typed Context JSON envelope. stdin remains raw text.
        #[arg(long)]
        context_json: Option<PathBuf>,
        /// Optional SQLite redaction-log database path.
        #[arg(long)]
        audit_db: Option<PathBuf>,
        /// Optional observer-only privacy safety net.
        #[arg(long, value_enum)]
        safety_net: Option<SafetyNetKind>,
        /// v0.8 backend selector. When set with
        /// `--safety-net=<kind>`, this flag wins. Lets adopters swap the
        /// Pass-3 backend without re-typing the legacy `--safety-net` value.
        #[arg(long, value_enum)]
        safety_net_backend: Option<SafetyNetBackend>,
        /// Enable locale-aware Pass-3 safety-net registry dispatch.
        #[arg(long)]
        safety_net_registry: bool,
        /// Add one backend to the locale-aware safety-net registry. Repeatable.
        #[arg(long, value_enum)]
        safety_net_add: Vec<SafetyNetBackend>,
        /// Path to the local OpenAI Privacy Filter `opf` command.
        #[arg(long)]
        openai_filter_command: Option<PathBuf>,
        /// Path to the local OpenAI Privacy Filter checkpoint or model directory.
        #[arg(long)]
        openai_filter_checkpoint: Option<PathBuf>,
        /// OpenAI Privacy Filter operating point, when supported by the command.
        #[arg(long, value_enum)]
        openai_filter_operating_point: Option<OpenAiFilterOperatingPoint>,
        /// Device selection for the OpenAI safety-net subprocess (auto|cpu|cuda|mps). Default: auto (let opf decide).
        #[arg(long, value_enum, default_value_t = OpenAiFilterDevice::Auto)]
        openai_filter_device: OpenAiFilterDevice,
        /// Kiji DistilBERT runtime backend. Default: subprocess for compatibility.
        #[arg(long, value_enum, default_value_t = KijiBackend::Subprocess)]
        kiji_backend: KijiBackend,
        /// Kiji DistilBERT ONNX precision. Default: fp32.
        #[arg(long, value_enum, default_value_t = KijiDistilbertPrecision::Fp32)]
        kiji_distilbert_precision: KijiDistilbertPrecision,
        /// Locale list for the OpenAI Privacy Filter registry entry.
        #[arg(long, value_delimiter = ',')]
        opf_locales: Vec<String>,
        /// Alias for --openai-filter-command in registry examples.
        #[arg(long)]
        opf_command: Option<PathBuf>,
        /// Alias for --openai-filter-checkpoint in registry examples.
        #[arg(long)]
        opf_checkpoint: Option<PathBuf>,
        /// Path to the local Kiji DistilBERT subprocess command.
        #[arg(long)]
        kiji_distilbert_command: Option<PathBuf>,
        /// Path to the pinned Kiji DistilBERT model directory (must contain
        /// SHA256SUMS, labels.json, model.onnx, tokenizer.json).
        #[arg(long)]
        kiji_distilbert_model_dir: Option<PathBuf>,
        /// Locale list for the Kiji DistilBERT registry entry.
        #[arg(long, value_delimiter = ',')]
        kiji_distilbert_locales: Vec<String>,
        /// Safety-net subprocess timeout in milliseconds.
        #[arg(long, default_value_t = DEFAULT_SAFETY_NET_TIMEOUT_MS)]
        safety_net_timeout_ms: u64,
        /// Maximum clean-text bytes submitted to the safety net.
        #[arg(long, default_value_t = DEFAULT_SAFETY_NET_INPUT_LIMIT_BYTES)]
        safety_net_input_limit_bytes: usize,
        /// Safety-net handling mode for suspected leaks.
        #[arg(long, value_enum, default_value_t = SafetyNetMode::Resolve)]
        safety_net_mode: SafetyNetMode,
        /// Fallback when safety-net resolve or redact cannot complete.
        #[arg(long, value_enum, default_value_t = SafetyNetFallback::Redact)]
        safety_net_fallback: SafetyNetFallback,
    },
    /// Run a long-lived JSON-lines stdio cleaning daemon.
    Daemon {
        /// Path to policy.toml.
        #[arg(long)]
        policy: PathBuf,
        /// Optional observer-only privacy safety net.
        #[arg(long, value_enum)]
        safety_net: Option<SafetyNetKind>,
        /// v0.8 backend selector. When set with `--safety-net=<kind>`, this flag wins.
        #[arg(long, value_enum)]
        safety_net_backend: Option<SafetyNetBackend>,
        /// Exit when stdin is idle for this many seconds.
        #[arg(long, default_value_t = daemon::default_process_idle_timeout_secs())]
        idle_timeout: u64,
        /// Evict sessions idle for this many seconds.
        #[arg(long, default_value_t = daemon::default_session_idle_timeout_secs())]
        session_idle_timeout: u64,
        /// Maximum live sessions before LRU eviction.
        #[arg(long, default_value_t = daemon::default_session_cap())]
        session_cap: usize,
        /// Optional SQLite redaction-log database path.
        #[arg(long)]
        audit_db: Option<PathBuf>,
        /// Active locale fallback chain, comma separated and priority ordered.
        #[arg(long, value_delimiter = ',')]
        locale: Vec<String>,
        /// Override policy \[ner\] threshold. Must be between 0.0 and 1.0 inclusive.
        #[arg(long)]
        ner_threshold: Option<f32>,
        /// Override policy \[ner\].model_dir.
        #[arg(long)]
        ner_model_dir: Option<PathBuf>,
        /// Override policy \[ner\].locale.
        #[arg(long)]
        ner_locale: Option<String>,
        /// Path to the local OpenAI Privacy Filter `opf` command.
        #[arg(long)]
        openai_filter_command: Option<PathBuf>,
        /// Path to the local OpenAI Privacy Filter checkpoint or model directory.
        #[arg(long)]
        openai_filter_checkpoint: Option<PathBuf>,
        /// OpenAI Privacy Filter operating point, when supported by the command.
        #[arg(long, value_enum)]
        openai_filter_operating_point: Option<OpenAiFilterOperatingPoint>,
        /// Device selection for the OpenAI safety-net subprocess.
        #[arg(long, value_enum, default_value_t = OpenAiFilterDevice::Auto)]
        openai_filter_device: OpenAiFilterDevice,
        /// Kiji DistilBERT runtime backend.
        #[arg(long, value_enum, default_value_t = KijiBackend::Subprocess)]
        kiji_backend: KijiBackend,
        /// Path to the local Kiji DistilBERT subprocess command.
        #[arg(long)]
        kiji_distilbert_command: Option<PathBuf>,
        /// Path to the pinned Kiji DistilBERT model directory.
        #[arg(long)]
        kiji_distilbert_model_dir: Option<PathBuf>,
        /// Locale list for the Kiji DistilBERT backend.
        #[arg(long, value_delimiter = ',')]
        kiji_distilbert_locales: Vec<String>,
        /// Safety-net subprocess timeout in milliseconds.
        #[arg(long, default_value_t = DEFAULT_SAFETY_NET_TIMEOUT_MS)]
        safety_net_timeout_ms: u64,
        /// Maximum clean-text bytes submitted to the safety net.
        #[arg(long, default_value_t = DEFAULT_SAFETY_NET_INPUT_LIMIT_BYTES)]
        safety_net_input_limit_bytes: usize,
        /// Safety-net handling mode for suspected leaks.
        #[arg(long, value_enum, default_value_t = SafetyNetMode::Resolve)]
        safety_net_mode: SafetyNetMode,
        /// Fallback when safety-net resolve or redact cannot complete.
        #[arg(long, value_enum, default_value_t = SafetyNetFallback::Redact)]
        safety_net_fallback: SafetyNetFallback,
    },
    /// Read `{session_blob, text}` JSON from stdin; emit `{text}` JSON to stdout.
    Restore {
        /// Output format. Only `json` is supported today.
        #[arg(long, default_value = "json")]
        format: String,
        /// Unknown-token handling during restore.
        #[arg(long, value_enum, default_value_t = RestoreMode::Strict)]
        restore_mode: RestoreMode,
        /// Alias for `--restore-mode` used by restore-boundary telemetry.
        #[arg(long = "policy", value_enum)]
        restore_policy: Option<RestoreMode>,
        /// Emit restore telemetry in the JSON response.
        #[arg(long)]
        telemetry: bool,
        /// Optional SQLite redaction-log database path for restore telemetry rows.
        #[arg(long)]
        audit_db: Option<PathBuf>,
        /// Max stdin size in bytes. stdin longer than this exits 1 InputTooLarge.
        #[arg(long, default_value_t = DEFAULT_MAX_BYTES)]
        max_bytes: u64,
    },
    /// Query, export, or maintain redaction-log metadata without reading raw PII payloads.
    Audit {
        #[command(subcommand)]
        command: AuditCmd,
    },
    /// Install the pinned local model, write a working policy, and run a doctor check.
    ///
    /// Requires the binary to be built with `--features setup`.
    #[cfg(feature = "setup")]
    Setup {
        /// Safety-net setup path. Defaults to NER; OPF verifies an existing `opf download` checkpoint when available.
        #[arg(long, value_enum)]
        safety_net: Option<setup::SetupSafetyNet>,
        /// Policy TOML output path. Defaults to ./gaze.toml.
        #[arg(long)]
        policy_out: Option<PathBuf>,
        /// Model install directory. Defaults to $XDG_DATA_HOME/gaze/models/kiji-distilbert.
        #[arg(long)]
        model_dir: Option<PathBuf>,
        /// Use defaults without prompts.
        #[arg(long)]
        non_interactive: bool,
        /// Overwrite an existing policy file.
        #[arg(long)]
        force: bool,
    },
    /// Ingest a document (PNG/JPG/PDF) into a split SafeBundle.
    ///
    /// Requires the binary to be built with `--features document`.
    #[cfg(feature = "document")]
    Document {
        #[command(subcommand)]
        command: DocumentCmd,
    },
    /// Owner-side local text/Markdown index ingest and search.
    ///
    /// Requires the binary to be built with `--features index`.
    #[cfg(feature = "index")]
    Index {
        #[command(subcommand)]
        command: IndexCmd,
    },
    /// Install, diagnose, or run the Gaze MCP stdio server.
    ///
    /// Requires the binary to be built with `--features mcp`.
    #[cfg(feature = "mcp")]
    Mcp {
        #[command(subcommand)]
        command: McpCmd,
    },
    /// Run or manage the Gaze LLM proxy.
    ///
    /// Requires the binary to be built with `--features proxy`.
    #[cfg(feature = "proxy")]
    Proxy {
        #[command(subcommand)]
        command: ProxyCmd,
    },
}

#[cfg(feature = "document")]
#[derive(Subcommand, Debug)]
enum DocumentCmd {
    /// Run OCR + Gaze redact on `<input>` and split agent-safe files from owner restore material.
    #[command(group(
        clap::ArgGroup::new("document_output")
            .required(true)
            .args(["out", "agent_out"])
    ))]
    Clean {
        /// Source file. Must be `.png`, `.jpg`, `.jpeg`, or `.pdf` (single-page).
        input: PathBuf,
        /// Convenience output root. Creates `<PATH>/agent` and `<PATH>/owner`.
        #[arg(long, conflicts_with_all = ["agent_out", "owner_out"])]
        out: Option<PathBuf>,
        /// Agent-visible output directory for `clean.md` and `report.json`.
        #[arg(long, requires = "owner_out")]
        agent_out: Option<PathBuf>,
        /// Owner-only output directory for `manifest.json`.
        #[arg(long, requires = "agent_out")]
        owner_out: Option<PathBuf>,
    },
}

#[cfg(feature = "index")]
#[derive(Subcommand, Debug)]
enum IndexCmd {
    /// Ingest `.txt` and `.md` files into a local owner-side index.
    Ingest {
        /// Directory containing text/Markdown files.
        dir: PathBuf,
        /// Local index domain id.
        #[arg(long, default_value_t = index::default_domain())]
        domain: String,
        /// Owner-side index directory. Defaults to `GAZE_INDEX_PATH` or `./.gaze-index/`.
        #[arg(long)]
        index_path: Option<PathBuf>,
        /// Safety-net fallback for residual suspects after resolver pass.
        #[arg(long, value_enum, default_value_t = index::OnResidual::Redact)]
        on_residual: index::OnResidual,
    },
    /// Search the local owner-side index by one entity.
    Search {
        /// Entity value to search for. It is tokenized before authorization/search.
        entity: String,
        /// Local index domain id.
        #[arg(long, default_value_t = index::default_domain())]
        domain: String,
        /// Entity class: name, email, org, organization, or `custom:<name>`.
        #[arg(long = "class", value_parser = index::parse_index_class)]
        class: Option<gaze::PiiClass>,
        /// Owner-side index directory. Defaults to `GAZE_INDEX_PATH` or `./.gaze-index/`.
        #[arg(long)]
        index_path: Option<PathBuf>,
    },
}

#[cfg(feature = "mcp")]
#[derive(Subcommand, Debug)]
enum McpCmd {
    /// Install Gaze as an MCP stdio server in a supported client config.
    Install {
        /// Client config to update.
        #[arg(long, value_enum)]
        client: mcp::Client,
        /// Agent guidance file to create/update.
        #[arg(long)]
        agents_md: Option<PathBuf>,
        /// Print planned changes without writing files.
        #[arg(long)]
        dry_run: bool,
        /// Skip the marker-fenced AGENTS.md skill section.
        #[arg(long)]
        skip_agents_md: bool,
    },
    /// Diagnose MCP server dependencies and client configuration.
    Doctor {
        /// Agent guidance file to check.
        #[arg(long)]
        agents_md: Option<PathBuf>,
        /// Exit non-zero when any warning is present.
        #[arg(long)]
        strict: bool,
        /// Emit machine-readable JSON.
        #[arg(long)]
        json: bool,
    },
    /// Run the Gaze MCP stdio server.
    Serve {
        /// Directory where MCP manifest call records are written.
        #[arg(long)]
        manifest_dir: Option<PathBuf>,
        /// Maximum file size accepted by `gaze_read_file`.
        #[arg(long)]
        max_file_size: Option<u64>,
    },
    /// Run the policy-gated MCP bridge over stdio.
    Bridge {
        /// Bridge TOML configuration.
        #[arg(long)]
        config: PathBuf,
        /// Override [session].dir from the config.
        #[arg(long)]
        session_dir: Option<PathBuf>,
        /// Load config and discover downstream surface without serving.
        #[arg(long)]
        dry_run: bool,
        /// Print namespaced downstream tools and denied resource/prompt counts.
        #[arg(long)]
        print_tools: bool,
    },
}

#[cfg(feature = "proxy")]
#[derive(Subcommand, Debug)]
enum ProxyCmd {
    /// Run proxy in the foreground.
    Serve {
        #[arg(long, default_value = "127.0.0.1:8787")]
        bind: std::net::SocketAddr,
        #[arg(long, default_value = "https://api.openai.com")]
        upstream_openai: url::Url,
        #[arg(long, default_value = "https://api.anthropic.com")]
        upstream_anthropic: url::Url,
        #[arg(long, default_value = "https://generativelanguage.googleapis.com")]
        upstream_gemini: url::Url,
        #[arg(long)]
        policy: Option<PathBuf>,
        #[arg(long, default_value = "core")]
        rulepack: String,
        #[arg(long, default_value = "30m")]
        session_ttl: String,
        #[arg(long = "_foreground-daemon", hide = true)]
        foreground_daemon: bool,
    },
    /// Start proxy as a detached daemon.
    Start {
        #[arg(long)]
        bind: Option<std::net::SocketAddr>,
        #[arg(long)]
        upstream_openai: Option<url::Url>,
        #[arg(long)]
        upstream_anthropic: Option<url::Url>,
        #[arg(long)]
        upstream_gemini: Option<url::Url>,
        #[arg(long)]
        policy: Option<PathBuf>,
        #[arg(long)]
        rulepack: Option<String>,
        #[arg(long)]
        session_ttl: Option<String>,
    },
    /// Stop the detached proxy daemon.
    Stop {
        #[arg(long)]
        force: bool,
        #[arg(long, default_value = "10s")]
        timeout: String,
    },
    /// Show proxy daemon status.
    Status,
    /// Print proxy daemon logs.
    Logs {
        #[arg(long)]
        follow: bool,
    },
    /// Restart proxy daemon using persisted config.
    Restart {
        #[arg(long)]
        force: bool,
        #[arg(long, default_value = "10s")]
        timeout: String,
    },
    /// Install macOS launchd auto-start integration.
    InstallLaunchd,
    /// Uninstall macOS launchd auto-start integration.
    UninstallLaunchd,
    /// Install Linux systemd user auto-start integration.
    InstallSystemdUser,
    /// Uninstall Linux systemd user auto-start integration.
    UninstallSystemdUser,
}

#[derive(Subcommand, Debug)]
enum AuditCmd {
    /// Print filtered audit metadata rows as tab-separated values.
    Query {
        /// SQLite redaction-log database path.
        #[arg(long)]
        audit_db: PathBuf,
        /// Filter by PII class, such as `email`, `name`, or `custom:term`.
        #[arg(long = "class")]
        pii_class: Option<String>,
        /// Filter by source recognizer name.
        #[arg(long)]
        source: Option<String>,
        /// Filter by action, such as `tokenize`, `redact`, or `preserve`.
        #[arg(long)]
        action: Option<String>,
        /// Filter by document kind, such as `text` or `structured`.
        #[arg(long)]
        document_kind: Option<String>,
        /// Include rows created at or after this ISO 8601 timestamp.
        #[arg(long = "from")]
        from_iso8601: Option<String>,
        /// Include rows created at or before this ISO 8601 timestamp.
        #[arg(long = "to")]
        to_iso8601: Option<String>,
        /// Filter by opaque audit session id.
        #[arg(long = "session")]
        session_id: Option<String>,
        /// Include only rows with an ambiguity side-channel record.
        #[arg(long)]
        has_ambiguity: bool,
        /// Filter by ambiguity reason, such as `no-anchor`.
        #[arg(long)]
        ambiguity_reason: Option<String>,
        /// Filter by collision family identifier.
        #[arg(long)]
        collision_family: Option<String>,
        /// Filter by collision variant identifier.
        #[arg(long)]
        collision_variant: Option<String>,
        /// Include only restore telemetry rows.
        #[arg(long)]
        restore_events: bool,
    },
    /// Export filtered audit metadata rows.
    Export {
        /// SQLite redaction-log database path.
        #[arg(long)]
        audit_db: PathBuf,
        /// Export format.
        #[arg(long, value_enum, default_value = "jsonl")]
        format: audit::ExportFormat,
        /// Optional output file. Defaults to stdout.
        #[arg(long)]
        output: Option<PathBuf>,
        /// Filter by PII class, such as `email`, `name`, or `custom:term`.
        #[arg(long = "class")]
        pii_class: Option<String>,
        /// Filter by source recognizer name.
        #[arg(long)]
        source: Option<String>,
        /// Filter by action, such as `tokenize`, `redact`, or `preserve`.
        #[arg(long)]
        action: Option<String>,
        /// Filter by document kind, such as `text` or `structured`.
        #[arg(long)]
        document_kind: Option<String>,
        /// Include rows created at or after this ISO 8601 timestamp.
        #[arg(long = "from")]
        from_iso8601: Option<String>,
        /// Include rows created at or before this ISO 8601 timestamp.
        #[arg(long = "to")]
        to_iso8601: Option<String>,
        /// Filter by opaque audit session id.
        #[arg(long = "session")]
        session_id: Option<String>,
        /// Include only rows with an ambiguity side-channel record.
        #[arg(long)]
        has_ambiguity: bool,
        /// Filter by ambiguity reason, such as `no-anchor`.
        #[arg(long)]
        ambiguity_reason: Option<String>,
        /// Filter by collision family identifier.
        #[arg(long)]
        collision_family: Option<String>,
        /// Filter by collision variant identifier.
        #[arg(long)]
        collision_variant: Option<String>,
        /// Include only restore telemetry rows.
        #[arg(long)]
        restore_events: bool,
    },
    /// Purge audit metadata rows older than an ISO 8601 UTC timestamp.
    Purge {
        /// SQLite redaction-log database path.
        #[arg(long)]
        audit_db: PathBuf,
        /// Purge rows where created_at is before this ISO 8601 UTC timestamp.
        #[arg(long)]
        before: String,
        /// Count matching rows without deleting them.
        #[arg(long, alias = "count")]
        dry_run: bool,
    },
    /// Query safety-net leak metadata without changing redaction-log output.
    SafetyNet {
        #[command(subcommand)]
        command: SafetyNetAuditCmd,
    },
}

#[derive(Subcommand, Debug)]
enum SafetyNetAuditCmd {
    /// Print filtered safety-net metadata rows as tab-separated values.
    Query {
        /// SQLite redaction-log database path.
        #[arg(long)]
        audit_db: PathBuf,
        /// Filter by safety-net leak kind.
        #[arg(long)]
        leak_kind: Option<String>,
        /// Filter by raw backend label.
        #[arg(long)]
        raw_label: Option<String>,
        /// Filter by mapped Gaze class.
        #[arg(long)]
        mapped_class: Option<String>,
        /// Filter by structured field path.
        #[arg(long)]
        field_path: Option<String>,
        /// Include rows created at or after this ISO 8601 timestamp.
        #[arg(long = "from")]
        from_iso8601: Option<String>,
        /// Include rows created at or before this ISO 8601 timestamp.
        #[arg(long = "to")]
        to_iso8601: Option<String>,
    },
}

#[derive(ValueEnum, Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SafetyNetKind {
    OpenaiFilter,
    KijiDistilbert,
}

/// v0.8 forward-compatible backend selector.
///
/// `--safety-net-backend` lets adopters pick which observer-only backend runs
/// at Pass-3 when more than one is wired in. Defaults to `openai-filter`,
/// which preserves the v0.6/v0.7 single-backend behavior.
#[derive(ValueEnum, Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SafetyNetBackend {
    OpenaiFilter,
    KijiDistilbert,
}

impl From<SafetyNetKind> for SafetyNetBackend {
    fn from(kind: SafetyNetKind) -> Self {
        match kind {
            SafetyNetKind::OpenaiFilter => Self::OpenaiFilter,
            SafetyNetKind::KijiDistilbert => Self::KijiDistilbert,
        }
    }
}

#[derive(ValueEnum, Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum OpenAiFilterOperatingPoint {
    HighRecall,
    Balanced,
    HighPrecision,
}

impl OpenAiFilterOperatingPoint {
    #[cfg(feature = "safety-net-openai")]
    pub(crate) fn as_opf_value(self) -> &'static str {
        match self {
            Self::HighRecall => "high-recall",
            Self::Balanced => "balanced",
            Self::HighPrecision => "high-precision",
        }
    }
}

#[derive(ValueEnum, Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum OpenAiFilterDevice {
    Auto,
    Cpu,
    Cuda,
    Mps,
}

impl OpenAiFilterDevice {
    #[cfg(feature = "safety-net-openai")]
    pub(crate) fn as_opf_value(self) -> Option<&'static str> {
        match self {
            Self::Auto => None,
            Self::Cpu => Some("cpu"),
            Self::Cuda => Some("cuda"),
            Self::Mps => Some("mps"),
        }
    }
}

#[derive(ValueEnum, Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum KijiBackend {
    Subprocess,
    Ort,
    #[cfg(feature = "runtime-tract")]
    Tract,
    #[cfg(feature = "runtime-candle")]
    Candle,
}

#[derive(ValueEnum, Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum KijiDistilbertPrecision {
    Fp32,
    Int8,
}

#[derive(ValueEnum, Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SafetyNetMode {
    Strict,
    Tolerant,
    Redact,
    Resolve,
}

#[derive(ValueEnum, Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum SafetyNetFallback {
    Strict,
    Tolerant,
    Redact,
}

pub(crate) fn dispatch(cli: Cli) -> std::result::Result<(), CliError> {
    match cli.cmd {
        Cmd::Clean {
            policy,
            format,
            session_ttl,
            session_scope,
            locale,
            ner_threshold,
            ner_model_dir,
            ner_locale,
            rulepack_bundled,
            rulepack_paths,
            max_bytes,
            context_json,
            audit_db,
            safety_net,
            safety_net_backend,
            safety_net_registry,
            safety_net_add,
            openai_filter_command,
            openai_filter_checkpoint,
            openai_filter_operating_point,
            openai_filter_device,
            kiji_backend,
            kiji_distilbert_precision,
            opf_locales,
            opf_command,
            opf_checkpoint,
            kiji_distilbert_command,
            kiji_distilbert_model_dir,
            kiji_distilbert_locales,
            safety_net_timeout_ms,
            safety_net_input_limit_bytes,
            safety_net_mode,
            safety_net_fallback,
        } => clean::run(clean::Args {
            policy,
            format,
            session_ttl,
            session_scope,
            locale,
            ner_threshold,
            ner_model_dir,
            ner_locale,
            rulepack_bundled,
            rulepack_paths,
            max_bytes,
            context_json,
            audit_db,
            safety_net,
            safety_net_backend,
            safety_net_registry,
            safety_net_add,
            openai_filter_command,
            openai_filter_checkpoint,
            openai_filter_operating_point,
            openai_filter_device,
            kiji_backend,
            kiji_distilbert_precision,
            opf_locales,
            opf_command,
            opf_checkpoint,
            kiji_distilbert_command,
            kiji_distilbert_model_dir,
            kiji_distilbert_locales,
            safety_net_timeout_ms,
            safety_net_input_limit_bytes,
            safety_net_mode,
            safety_net_fallback,
        }),
        Cmd::Restore {
            format,
            restore_mode,
            restore_policy,
            telemetry,
            audit_db,
            max_bytes,
        } => restore::run(restore::Args {
            format,
            restore_mode: restore_policy.unwrap_or(restore_mode),
            telemetry,
            audit_db,
            max_bytes,
        }),
        Cmd::Daemon {
            policy,
            safety_net,
            safety_net_backend,
            idle_timeout,
            session_idle_timeout,
            session_cap,
            audit_db,
            locale,
            ner_threshold,
            ner_model_dir,
            ner_locale,
            openai_filter_command,
            openai_filter_checkpoint,
            openai_filter_operating_point,
            openai_filter_device,
            kiji_backend,
            kiji_distilbert_command,
            kiji_distilbert_model_dir,
            kiji_distilbert_locales,
            safety_net_timeout_ms,
            safety_net_input_limit_bytes,
            safety_net_mode,
            safety_net_fallback,
        } => daemon::run(daemon::Args {
            policy,
            safety_net,
            safety_net_backend,
            idle_timeout_secs: idle_timeout,
            session_idle_timeout_secs: session_idle_timeout,
            session_cap,
            audit_db,
            locale,
            ner_threshold,
            ner_model_dir,
            ner_locale,
            openai_filter_command,
            openai_filter_checkpoint,
            openai_filter_operating_point,
            openai_filter_device,
            kiji_backend,
            kiji_distilbert_command,
            kiji_distilbert_model_dir,
            kiji_distilbert_locales,
            safety_net_timeout_ms,
            safety_net_input_limit_bytes,
            safety_net_mode,
            safety_net_fallback,
        }),
        Cmd::Audit { command } => match command {
            AuditCmd::Query {
                audit_db,
                pii_class,
                source,
                action,
                document_kind,
                from_iso8601,
                to_iso8601,
                session_id,
                has_ambiguity,
                ambiguity_reason,
                collision_family,
                collision_variant,
                restore_events,
            } => audit::query(audit::Args {
                audit_db,
                class: pii_class,
                source,
                action,
                document_kind,
                from_iso8601,
                to_iso8601,
                session_id,
                has_ambiguity,
                ambiguity_reason,
                collision_family,
                collision_variant,
                restore_events_only: restore_events,
            }),
            AuditCmd::Export {
                audit_db,
                format,
                output,
                pii_class,
                source,
                action,
                document_kind,
                from_iso8601,
                to_iso8601,
                session_id,
                has_ambiguity,
                ambiguity_reason,
                collision_family,
                collision_variant,
                restore_events,
            } => audit::export(
                audit::Args {
                    audit_db,
                    class: pii_class,
                    source,
                    action,
                    document_kind,
                    from_iso8601,
                    to_iso8601,
                    session_id,
                    has_ambiguity,
                    ambiguity_reason,
                    collision_family,
                    collision_variant,
                    restore_events_only: restore_events,
                },
                format,
                output,
            ),
            AuditCmd::Purge {
                audit_db,
                before,
                dry_run,
            } => audit::purge(audit::PurgeArgs {
                audit_db,
                before,
                dry_run,
            }),
            AuditCmd::SafetyNet { command } => match command {
                SafetyNetAuditCmd::Query {
                    audit_db,
                    leak_kind,
                    raw_label,
                    mapped_class,
                    field_path,
                    from_iso8601,
                    to_iso8601,
                } => audit::query_safety_net(audit::SafetyNetArgs {
                    audit_db,
                    leak_kind,
                    raw_label,
                    mapped_class,
                    field_path,
                    from_iso8601,
                    to_iso8601,
                }),
            },
        },
        #[cfg(feature = "setup")]
        Cmd::Setup {
            safety_net,
            policy_out,
            model_dir,
            non_interactive,
            force,
        } => setup::run(setup::Args {
            safety_net,
            policy_out,
            model_dir,
            non_interactive,
            force,
        }),
        #[cfg(feature = "document")]
        Cmd::Document { command } => match command {
            DocumentCmd::Clean {
                input,
                out,
                agent_out,
                owner_out,
            } => document::run_clean(document::CleanArgs {
                input,
                out,
                agent_out,
                owner_out,
            }),
        },
        #[cfg(feature = "index")]
        Cmd::Index { command } => match command {
            IndexCmd::Ingest {
                dir,
                domain,
                index_path,
                on_residual,
            } => index::ingest(index::IngestArgs {
                dir,
                domain,
                index_path,
                on_residual,
            }),
            IndexCmd::Search {
                entity,
                domain,
                class,
                index_path,
            } => index::search(index::SearchArgs {
                entity,
                domain,
                class,
                index_path,
            }),
        },
        #[cfg(feature = "mcp")]
        Cmd::Mcp { command } => match command {
            McpCmd::Install {
                client,
                agents_md,
                dry_run,
                skip_agents_md,
            } => mcp::install(mcp::InstallArgs {
                client,
                agents_md,
                dry_run,
                skip_agents_md,
            }),
            McpCmd::Doctor {
                agents_md,
                strict,
                json,
            } => mcp::doctor(mcp::DoctorArgs {
                agents_md,
                strict,
                json,
            }),
            McpCmd::Serve {
                manifest_dir,
                max_file_size,
            } => mcp::serve(mcp::ServeArgs {
                manifest_dir,
                max_file_size,
            }),
            McpCmd::Bridge {
                config,
                session_dir,
                dry_run,
                print_tools,
            } => mcp::bridge(mcp::BridgeArgs {
                config,
                session_dir,
                dry_run,
                print_tools,
            }),
        },
        #[cfg(feature = "proxy")]
        Cmd::Proxy { command } => match command {
            ProxyCmd::Serve {
                bind,
                upstream_openai,
                upstream_anthropic,
                upstream_gemini,
                policy,
                rulepack,
                session_ttl,
                foreground_daemon,
            } => proxy::serve(proxy::ServeArgs {
                bind,
                upstream_openai,
                upstream_anthropic,
                upstream_gemini,
                policy,
                rulepack,
                session_ttl,
                foreground_daemon,
            }),
            ProxyCmd::Start {
                bind,
                upstream_openai,
                upstream_anthropic,
                upstream_gemini,
                policy,
                rulepack,
                session_ttl,
            } => proxy::start(proxy::StartArgs {
                bind,
                upstream_openai,
                upstream_anthropic,
                upstream_gemini,
                policy,
                rulepack,
                session_ttl,
            }),
            ProxyCmd::Stop { force, timeout } => proxy::stop(proxy::StopArgs { force, timeout }),
            ProxyCmd::Status => proxy::status(),
            ProxyCmd::Logs { follow } => proxy::logs(follow),
            ProxyCmd::Restart { force, timeout } => {
                proxy::restart(proxy::RestartArgs { force, timeout })
            }
            ProxyCmd::InstallLaunchd => proxy::install_launchd(),
            ProxyCmd::UninstallLaunchd => proxy::uninstall_launchd(),
            ProxyCmd::InstallSystemdUser => proxy::install_systemd_user(),
            ProxyCmd::UninstallSystemdUser => proxy::uninstall_systemd_user(),
        },
    }
}

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

    #[test]
    fn openai_filter_device_defaults_to_auto() {
        let cli = Cli::parse_from(["gaze", "clean"]);

        let Cmd::Clean {
            openai_filter_device,
            ..
        } = cli.cmd
        else {
            unreachable!("expected clean command");
        };

        assert_eq!(openai_filter_device, OpenAiFilterDevice::Auto);
    }
}