tailscale-mcp 2.0.3

MCP server for Tailscale: the local node through the CLI, the tailnet through the control-plane API
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
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
//! The one tool that runs a command this server has no tool for.
//!
//! Every other tool on the local surface is a fixed argument list with typed
//! parameters. This one takes the argument list from the caller, which is a
//! different kind of promise and so is off in every preset, `full` included: a
//! session reaches it by naming `local-passthrough`, and naming it is the
//! statement that the caller accepts commands this server has not vetted.
//!
//! ## How a command is judged
//!
//! The tool's row carries [`Tier::Read`], which is a floor and not the truth —
//! see [`ToolMeta::varying_tier`](crate::meta::ToolMeta::varying_tier). What
//! decides the tier is the command, and there are three answers:
//!
//! - [`COVERED`] names the commands a typed tool already runs, each at that
//!   tool's own tier and confirmation. `tailscale down` is destructive here
//!   because `tailscale_down` is destructive there, and needs the same
//!   `confirm: true`. A test drives every typed tool and pins this table to
//!   what they actually run, so the two cannot drift apart quietly.
//! - The two exclusion lists — this module's own and
//!   [`local_debug::EXCLUDED`], which [`excluded`] chains together — name the
//!   commands this server never runs, each with the reason the caller is
//!   shown. Closing a toolset is not a way in and neither is this.
//! - Anything else is unknown, and unknown counts as destructive. A command
//!   nobody has judged is judged at the top.
//!
//! ## Reading the command out of the arguments
//!
//! This server does not parse the client's flags and should not have to, so it
//! reads the command twice and keeps whichever reading is stricter. One reading
//! is the leading run of words that are not flags; the other is every word that
//! is not a flag. Each is matched deepest-first, up to `MAX_DEPTH` words, so
//! that `lock remove` is not read as `lock`.
//!
//! Neither reading is sound alone, and each fails in the opposite direction.
//! `tailscale serve --bg reset` runs `serve reset`, which the leading reading
//! calls `serve`: a write-tier session would wipe the serve configuration with
//! no confirmation. `tailscale funnel --set-path status 8080` runs `funnel`,
//! which the every-word reading calls `funnel status`: a destructive command
//! read as a reader. Working out which applies is the flag parsing this
//! refuses to do, and taking the stricter of the two is wrong only ever in the
//! direction of a tier or a confirmation the caller did not need. Case and
//! surrounding spaces are dropped before matching, because the client's own
//! subcommand lookup ignores case at every depth: `["DEBUG", "PREFS"]` runs
//! `debug prefs`.
//!
//! What no reading rescues is a path that stops partway into something
//! excluded. `["debug", "--file=get"]` has no word after `debug`, and letting
//! it through as unknown would run a `debug`-anything at the destructive tier,
//! so it is refused outright rather than guessed at. That is also why
//! `tailscale_debug_file_list` exists as a typed tool: its subcommand is a flag
//! on the parent, which is useful and is unreadable here.
//!
//! Two commands are deliberately absent from every table. `debug reload-config`
//! is in neither list and so is unknown-destructive, which is what DECISIONS
//! Q44 asks for: runnable, but never on this server's recommendation.
//! `completion` has no typed tool to inherit from, and inventing a tier for it
//! would be the first row nothing checks.
//!
//! ## What the caller gets
//!
//! An argument list, never a string: nothing here is parsed by a shell, so a
//! semicolon or a quote in an argument is a semicolon or a quote in an
//! argument. The command line reaches the log and any error redacted, because
//! unlike every typed tool's arguments these were not built here.

use rmcp::schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tailscale_cli::Invocation;

use crate::cli;
use crate::context::ToolContext;
use crate::error::{ErrorCode, ToolError, ToolResult};
use crate::meta::Tier;
use crate::tools::common::{Excluded, bounded_wait, printed, report};
use crate::tools::local_debug;

crate::tools! {
    /// Run a `tailscale` subcommand that no other tool covers, given as a list
    /// of arguments. Prefer a typed tool wherever one exists: they validate
    /// what they are given and answer with structured data rather than the
    /// text a person would read. What this is allowed to run depends on the
    /// command — one a typed tool covers takes that tool's tier and
    /// confirmation, an unrecognised one counts as destructive, and a command
    /// this server never runs is refused with the reason why.
    tailscale_run => RunParams, run,
        toolset: LocalPassthrough, tier: Read, varying: true;
}

/// The deepest command path either table names, and so the most words worth
/// matching. Five paths are that deep, all of them under `configure`;
/// `every_path_fits_the_depth_the_matcher_searches` holds the rest shallower.
const MAX_DEPTH: usize = 3;

/// How long a passthrough command may run before this server gives up, and the
/// bound it uses when the caller names none.
const DEFAULT_RUN_TIMEOUT: u64 = 30;
const MAX_RUN_TIMEOUT: u64 = 300;

/// A command a typed tool already runs, and the terms it runs on there.
///
/// Neither field is this module's opinion. Both are read off the typed tool's
/// own row, and `the_covered_table_follows_the_tools_it_claims_to_follow`
/// re-derives them from the arguments those tools actually build.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Covered {
    /// The command as it is written after `tailscale`, words separated by
    /// spaces.
    pub path: &'static str,
    /// The tier of the typed tool that runs it. Where two tools share a path,
    /// the higher of them: `switch` is both `tailscale_switch_list` and
    /// `tailscale_switch_profile`, and a caller writing `switch` unqualified
    /// may mean either.
    pub tier: Tier,
    /// Whether that tool requires a confirmation, for the same reason.
    pub confirm: bool,
}

/// Every command a typed tool covers, with the terms that tool runs it on.
///
/// Grouped as the modules are, so that a tool added there has an obvious home
/// here. Debug's twenty-nine come last, and its thirtieth tool is not among
/// them: `tailscale_debug_file_list` runs `debug --file=get`, a flag on the
/// parent command rather than a subcommand, which has no path to name.
pub const COVERED: &[Covered] = &[
    // -- local-prefs --------------------------------------------------------
    Covered {
        path: "up",
        tier: Tier::Destructive,
        confirm: true,
    },
    Covered {
        path: "down",
        tier: Tier::Destructive,
        confirm: true,
    },
    Covered {
        path: "set",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "get",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "login",
        tier: Tier::Write,
        confirm: true,
    },
    Covered {
        path: "logout",
        tier: Tier::Destructive,
        confirm: true,
    },
    Covered {
        path: "switch",
        tier: Tier::Write,
        confirm: true,
    },
    Covered {
        path: "switch remove",
        tier: Tier::Destructive,
        confirm: true,
    },
    // -- local-status -------------------------------------------------------
    Covered {
        path: "status",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "ip",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "netcheck",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "ping",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "whois",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "whoami",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "version",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "licenses",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "bugreport",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "appc-routes",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "routecheck",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "wait",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "dns status",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "dns query",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "exit-node list",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "exit-node suggest",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "metrics print",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "service list",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "syspolicy list",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "configure sysext status",
        tier: Tier::Read,
        confirm: false,
    },
    // -- local-serve --------------------------------------------------------
    Covered {
        path: "serve",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "serve status",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "serve reset",
        tier: Tier::Destructive,
        confirm: true,
    },
    Covered {
        path: "serve drain",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "serve clear",
        tier: Tier::Destructive,
        confirm: true,
    },
    Covered {
        path: "serve advertise",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "serve get-config",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "serve set-config",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "funnel",
        tier: Tier::Destructive,
        confirm: true,
    },
    Covered {
        path: "funnel status",
        tier: Tier::Read,
        confirm: false,
    },
    // -- local-files --------------------------------------------------------
    Covered {
        path: "file cp",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "file get",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "cert",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "metrics write",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "configure kubeconfig",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "syspolicy reload",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "drive list",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "drive share",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "drive rename",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "drive unshare",
        tier: Tier::Destructive,
        confirm: false,
    },
    // -- local-lock ---------------------------------------------------------
    Covered {
        path: "lock status",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "lock log",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "lock init",
        tier: Tier::Destructive,
        confirm: true,
    },
    Covered {
        path: "lock add",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "lock remove",
        tier: Tier::Destructive,
        confirm: false,
    },
    Covered {
        path: "lock sign",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "lock disable",
        tier: Tier::Destructive,
        confirm: true,
    },
    Covered {
        path: "lock disablement-kdf",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "lock local-disable",
        tier: Tier::Destructive,
        confirm: false,
    },
    Covered {
        path: "lock revoke-keys",
        tier: Tier::Destructive,
        confirm: true,
    },
    // -- local-debug --------------------------------------------------------
    Covered {
        path: "debug derp-map",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug netmap",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug hostinfo",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug control-knobs",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug daemon-goroutines",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug daemon-bus-graph",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug daemon-bus-queues",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug metrics",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug statedir",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug go-buildinfo",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug peer-relay-servers",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug peer-relay-sessions",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug stat",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug via",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug watch-ipn",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug peer-endpoint-changes",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug resolve",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug dial-types",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug derp",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug ts2021",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug portmap",
        tier: Tier::Read,
        confirm: false,
    },
    Covered {
        path: "debug component-logs",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "debug restun",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "debug rebind",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "debug rotate-disco-key",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "debug derp-unset-on-demand",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "debug pick-new-derp",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "debug force-prefer-derp",
        tier: Tier::Write,
        confirm: false,
    },
    Covered {
        path: "debug force-netmap-update",
        tier: Tier::Write,
        confirm: false,
    },
];

/// Every documented command this server will not run at all.
///
/// Three of the four grounds `CONTEXT.md` names are here: it runs in the
/// foreground with no end, it wires a stream to a terminal this server does not
/// have, or it changes the host outside Tailscale. The fourth — printing a
/// secret — belongs to `local_debug::EXCLUDED`, which holds the hidden half of
/// the same rule. [`excluded`] is the two of them together, and is how anything
/// outside this module reads either.
const EXCLUDED: &[Excluded] = &[
    Excluded {
        path: "ssh",
        reason: "it opens an interactive session on a peer, which needs a terminal \
                 this server does not have",
    },
    Excluded {
        path: "nc",
        reason: "it wires a socket to standard input and output, which a tool call \
                 has nothing to connect",
    },
    Excluded {
        path: "web",
        reason: "it runs a web server in the foreground until interrupted",
    },
    Excluded {
        path: "systray",
        reason: "it runs a desktop application in the foreground until quit",
    },
    Excluded {
        path: "update",
        reason: "it replaces the `tailscale` binary this server is talking to; \
                 update the host the way the host is normally updated",
    },
    Excluded {
        path: "configure sysext activate",
        reason: "it installs a system extension and reboots into it, which is a \
                 change to the host rather than to the tailnet",
    },
    Excluded {
        path: "configure sysext deactivate",
        reason: "it removes a system extension the node may be running on, which is \
                 a change to the host rather than to the tailnet",
    },
    Excluded {
        path: "configure mac-vpn install",
        reason: "it installs a system VPN profile, which is a change to the host \
                 rather than to the tailnet",
    },
    Excluded {
        path: "configure mac-vpn uninstall",
        reason: "it removes a system VPN profile, which is a change to the host \
                 rather than to the tailnet",
    },
];

/// Every command this server refuses, documented and hidden alike.
///
/// The two halves are separate constants because they are each other's
/// context: one is read beside the debug tools, the other beside this one. A
/// caller meets them as a single list, which is what this is for.
pub fn excluded() -> impl Iterator<Item = &'static Excluded> {
    EXCLUDED.iter().chain(local_debug::EXCLUDED)
}

/// What this server knows about a command it was asked to run.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Known {
    /// A typed tool runs this, and these are its terms.
    Covered(&'static Covered),
    /// This server never runs it, and here is what to tell the caller.
    Excluded(&'static Excluded),
    /// Nobody has judged it, so it is judged at the top.
    Unknown,
}

/// The command a caller named, and what is known about it.
///
/// # Errors
///
/// When the arguments do not name a command this server can read: no bare word
/// at all, or bare words that stop partway into something excluded.
pub fn classify(args: &[String]) -> ToolResult<(String, Known)> {
    // Two readings, because this server does not parse the client's flags and
    // should not have to. The client does parse them: `tailscale serve --bg
    // reset` runs `serve reset`, so reading only the words before the first
    // flag would call it `serve` and let a write-tier session wipe the config
    // with no confirmation. Reading every bare word instead has the opposite
    // fault — `tailscale funnel --set-path status 8080` would read as `funnel
    // status`, a reader, when the flag's value only looks like a subcommand.
    //
    // Neither reading is right on its own, and working out which one applies is
    // the flag parsing this refuses to do. So both are taken and the stricter
    // wins. Being wrong then costs a caller a tier or a confirmation they did
    // not need, which is the direction to be wrong in.
    let leading = words(args.iter().take_while(|arg| !is_flag(arg)));
    let every = words(args.iter().filter(|arg| !is_flag(arg)));

    if every.first().is_none_or(String::is_empty) {
        return Err(ToolError::invalid_args(
            "the first argument has to be a subcommand rather than a flag or an \
             empty string, so that what runs can be read from the arguments",
        ));
    }

    let (path, known) = if leading.is_empty() {
        read(&every)
    } else {
        std::cmp::max_by_key(read(&leading), read(&every), |(_, known)| strictness(known))
    };

    // Unknown is ordinarily destructive-and-run. It is not that when a reading
    // stops partway into a command this server refuses: `["debug",
    // "--file=get"]` has no bare word after `debug`, and letting it through as
    // unknown would run a `debug`-anything on a session with the destructive
    // tier. Refusing the partial path is the only answer that does not depend
    // on parsing flags the way the client's own package would.
    if known == Known::Unknown {
        for reading in [&leading, &every] {
            if excluded().any(|e| is_proper_prefix(reading, e.path)) {
                return Err(ToolError::invalid_args(format!(
                    "`tailscale {}` names only part of a command, and this server \
                     decides what to allow from the words that are not flags. Write \
                     the whole subcommand, with its flags after it.",
                    reading.join(" ")
                )));
            }
        }
    }
    Ok((path, known))
}

/// Whether an argument is a flag rather than a word of the command.
fn is_flag(arg: &str) -> bool {
    arg.starts_with('-')
}

/// One reading's words, in the form the tables are written in.
///
/// Lowercased, because the client matches its own subcommands without regard to
/// case at every depth — `tailscale DEBUG PREFS` runs `debug prefs` — and a
/// table compared with `==` would otherwise be evaded by shouting. Trimmed for
/// the same reason in reverse: the client does not trim, so a padded word names
/// no command there, and reading it as one here only ever refuses more.
fn words<'a>(args: impl Iterator<Item = &'a String>) -> Vec<String> {
    args.map(|arg| arg.trim().to_ascii_lowercase()).collect()
}

/// The deepest command either table names at the start of `words`.
fn read(words: &[String]) -> (String, Known) {
    for depth in (1..=words.len().min(MAX_DEPTH)).rev() {
        let path = words[..depth].join(" ");
        if let Some(found) = excluded().find(|e| e.path == path) {
            return (path, Known::Excluded(found));
        }
        if let Some(found) = COVERED.iter().find(|c| c.path == path) {
            return (path, Known::Covered(found));
        }
    }
    (words.join(" "), Known::Unknown)
}

/// How strict a reading is, for choosing between two readings of one command.
///
/// Nothing is stricter than a command this server does not run at all, so that
/// ranks above every tier; below it, a higher tier is stricter than a lower one
/// and needing a confirmation is stricter than not.
fn strictness(known: &Known) -> (bool, Tier, bool) {
    match known {
        Known::Excluded(_) => (true, Tier::Destructive, true),
        Known::Covered(covered) => (false, covered.tier, covered.confirm),
        Known::Unknown => (false, Tier::Destructive, false),
    }
}

/// Whether `words` is the beginning of `path` without being all of it.
fn is_proper_prefix(words: &[String], path: &str) -> bool {
    let mut theirs = path.split(' ');
    !words.is_empty()
        && words
            .iter()
            .all(|word| theirs.next() == Some(word.as_str()))
        && theirs.next().is_some()
}

/// Run any `tailscale` subcommand.
#[derive(Debug, Deserialize, JsonSchema)]
pub struct RunParams {
    /// The arguments to `tailscale`, one per element, subcommand first: `["dns",
    /// "status", "--all"]` and never `"dns status --all"`. Nothing here is
    /// parsed by a shell, so a value containing a space, a quote or a semicolon
    /// is one argument and not several.
    pub args: Vec<String>,
    /// Set to true to confirm the command, which some of them need for the same
    /// reason the tool covering them does. A refusal names the command and says
    /// whose terms it is borrowing; what those terms are for is on that tool.
    #[serde(default)]
    pub confirm: bool,
    /// How long to let the command run, in seconds. Clamped to 1..=300, and 30
    /// when it is not given, which is what every typed tool without a reason to
    /// wait longer uses.
    #[serde(default)]
    pub timeout_seconds: Option<u64>,
}

/// What a passthrough command did.
#[derive(Debug, Serialize, JsonSchema)]
pub struct RunReport {
    /// The command as it ran, with anything secret-shaped removed.
    pub command: String,
    /// The tier it was judged at.
    pub tier: &'static str,
    /// Whether a typed tool covers it. When this is false the tier above is
    /// this server declining to guess rather than something it knows, and a
    /// typed tool is worth looking for.
    pub covered: bool,
    /// Everything the client printed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub printed: Option<String>,
}

async fn run(ctx: &ToolContext, params: RunParams) -> ToolResult<Value> {
    let meta = &metas::tailscale_run;
    let (path, known) = classify(&params.args)?;

    let (tier, confirm) = match known {
        Known::Excluded(excluded) => {
            // No hint: there is no switch that turns this on, which is the
            // whole difference between this refusal and the gate's.
            return Err(ToolError::new(
                ErrorCode::NotPermitted,
                format!(
                    "`tailscale {}` is never run by this server: {}",
                    excluded.path, excluded.reason
                ),
            ));
        }
        Known::Covered(covered) => (covered.tier, covered.confirm),
        Known::Unknown => (Tier::Destructive, false),
    };

    // `Tier::Read` outranks nothing and has no flag, so the two halves are one
    // condition: a tier above the session's is a tier with a switch to name.
    if tier > ctx.max_tier
        && let Some(flag) = tier.flag()
    {
        return Err(ToolError::not_permitted(&format!("tailscale {path}"), flag));
    }
    // Only a covered row asks for a confirmation; an unknown command is
    // destructive without one, because there is no tool whose terms to borrow.
    if confirm && !params.confirm {
        return Err(ToolError::confirmation_required(
            &format!("tailscale {path}"),
            "runs at the same terms as the tool that covers it",
        ));
    }

    let (_, bound) = bounded_wait(params.timeout_seconds, DEFAULT_RUN_TIMEOUT, MAX_RUN_TIMEOUT);
    // Anything that is not a read takes the exclusive lane. A typed tool knows
    // whether its own write can share; this one does not, and queueing behind
    // another mutation costs a wait where guessing wrong costs a race.
    let invocation = if tier == Tier::Read {
        Invocation::read(params.args.clone())
    } else {
        Invocation::mutate(params.args.clone())
    }
    .with_timeout(bound);

    let command = cli::displayed(ctx, &invocation);
    tracing::info!(command = %command, tier = tier.as_str(), "passthrough");
    let output = cli::run(ctx, meta, invocation).await?;
    report(RunReport {
        command,
        tier: tier.as_str(),
        covered: matches!(known, Known::Covered(_)),
        printed: printed(ctx, &output),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testing::{Reply, StubBackend, context};
    use std::sync::Arc;

    fn words(args: &[&str]) -> Vec<String> {
        args.iter().map(|a| (*a).to_owned()).collect()
    }

    fn class(args: &[&str]) -> Known {
        classify(&words(args))
            .expect("the arguments name a command")
            .1
    }

    #[test]
    fn every_path_fits_the_depth_the_matcher_searches() {
        for path in COVERED
            .iter()
            .map(|c| c.path)
            .chain(excluded().map(|e| e.path))
        {
            assert!(
                path.split(' ').count() <= MAX_DEPTH,
                "`{path}` is deeper than the matcher looks"
            );
        }
    }

    #[test]
    fn no_path_is_named_twice() {
        let mut seen: Vec<&str> = COVERED
            .iter()
            .map(|c| c.path)
            .chain(excluded().map(|e| e.path))
            .collect();
        let before = seen.len();
        seen.sort_unstable();
        seen.dedup();
        assert_eq!(before, seen.len(), "a command is judged in two places");
    }

    #[test]
    fn every_path_is_written_the_way_the_matcher_compares_it() {
        // The matcher lowercases what it is given and compares with `==`, so a
        // row written in mixed case would sit in the table matching nothing.
        for path in COVERED
            .iter()
            .map(|c| c.path)
            .chain(excluded().map(|e| e.path))
        {
            assert_eq!(path, path.to_ascii_lowercase(), "`{path}` cannot match");
            assert_eq!(path.trim(), path, "`{path}` cannot match");
        }
    }

    #[test]
    fn a_partial_path_into_an_excluded_command_is_refused_rather_than_guessed() {
        // Neither reading gets past `debug`, so there is nothing to judge and
        // letting it through as unknown would run it at the destructive tier.
        let error = classify(&words(&["debug", "--file=get"]))
            .expect_err("`debug` on its own cannot be judged");
        assert_eq!(error.code, ErrorCode::InvalidArgs);
        assert!(
            error.message.contains("only part of a command"),
            "{error:?}"
        );
    }

    #[test]
    fn a_flag_value_that_only_looks_like_a_subcommand_keeps_the_stricter_terms() {
        // The other direction: `status` here is the value of `--set-path`, and
        // reading it as `funnel status` would drop a destructive command to a
        // reader. Neither reading can be trusted alone, so the stricter wins.
        let (path, known) = classify(&words(&["funnel", "--set-path", "status", "8080"]))
            .expect("`funnel` is readable");
        assert_eq!(path, "funnel");
        let Known::Covered(covered) = known else {
            panic!("`funnel` has a row");
        };
        assert_eq!((covered.tier, covered.confirm), (Tier::Destructive, true));
    }

    #[test]
    fn a_blank_first_argument_is_refused_rather_than_run_as_a_nameless_command() {
        // `["", "down"]` would otherwise read as the unknown command `" down"`
        // and run at the destructive tier as `tailscale "" down`.
        for args in [vec![""], vec!["", "down"], vec!["   "]] {
            let args: Vec<String> = args.into_iter().map(str::to_owned).collect();
            let error = classify(&args).expect_err("a blank subcommand cannot be read");
            assert_eq!(error.code, ErrorCode::InvalidArgs);
        }
        // A blank word after the subcommand is one of its arguments, and the
        // subcommand is still read.
        let args: Vec<String> = ["ping", ""].into_iter().map(str::to_owned).collect();
        let (path, _) = classify(&args).expect("`ping` is still readable");
        assert_eq!(path, "ping");
    }

    #[test]
    fn a_leading_flag_does_not_hide_the_subcommand_behind_it() {
        // The leading-words reading is empty here, so only the other one has
        // anything to say, and it says `status`.
        let (path, known) =
            classify(&words(&["--socket=/tmp/x", "status"])).expect("`status` is still named");
        assert_eq!(path, "status");
        assert!(matches!(known, Known::Covered(_)));
    }

    #[test]
    fn reload_config_stays_runnable() {
        // DECISIONS Q44: in neither table, so unknown and destructive rather
        // than refused.
        assert!(matches!(class(&["debug", "reload-config"]), Known::Unknown));
    }

    /// Run the handler against a scripted client and report what it ran.
    async fn against(reply: Reply, params: RunParams) -> (ToolResult<Value>, Vec<Vec<String>>) {
        let backend = Arc::new(StubBackend::always(reply));
        let ctx = context(backend.clone());
        let answer = run(&ctx, params).await;
        (answer, backend.argv())
    }

    fn params(args: &[&str]) -> RunParams {
        RunParams {
            args: words(args),
            confirm: false,
            timeout_seconds: None,
        }
    }

    #[tokio::test]
    async fn the_report_says_how_the_command_was_judged() {
        let (answer, _) = against(Reply::ok("100.64.0.1\n"), params(&["ip"])).await;
        let value = answer.expect("a read runs");
        assert_eq!(value["tier"], "read");
        assert_eq!(value["covered"], true);
        assert_eq!(value["command"], "tailscale ip");
        assert_eq!(value["printed"], "100.64.0.1");
    }

    #[tokio::test]
    async fn a_secret_on_the_argument_list_is_kept_out_of_the_report() {
        // The one place a caller's own arguments become text this server
        // shows: every typed tool builds its own, and none of them can carry
        // a key the way this can.
        let key = "tskey-auth-kFakeExample-0123456789";
        let (answer, argv) = against(
            Reply::ok("done\n"),
            RunParams {
                args: words(&["up", &format!("--auth-key={key}")]),
                confirm: true,
                timeout_seconds: None,
            },
        )
        .await;
        let value = answer.expect("`up` runs once confirmed");
        let command = value["command"].as_str().expect("a command line");
        assert!(
            !command.contains(key),
            "the key reached the report: {command}"
        );

        // Redaction is what is shown and never what is run: the client still
        // has to receive the real key.
        assert_eq!(argv, vec![words(&["up", &format!("--auth-key={key}")])]);
    }
}