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
//! `tm run <target>` routing: DOC-24 alias vs. daemon-managed cold start (#4990).
//!
//! Why: `tm run` used to accept exactly one thing — a DOC-24 registry alias —
//! and launch a blocking foreground `claude` for it. Starting work on a repo
//! that is not already registered and not already on disk had no entry point
//! at all: the daemon-managed system (ADR-0030's tm checkout at
//! `~/trusty-mpm-projects/<owner>/<repo>`, a real `SessionRecord`, a tmux pane)
//! can only be entered from a checkout that already exists, because
//! `try_inproject_spawn` gates on `.git` and derives the GitHub identity from
//! `remote.origin.url`. `tm run <owner>/<repo>` is the cold start: clone or
//! verify the managed checkout, then hand off to the daemon-managed launch.
//!
//! What: `classify_run_target` sorts the positional using the SAME predicate
//! `tm register` uses ([`super::register_args::looks_like_repo`]) so the two
//! commands cannot disagree about what a string means; [`run`] dispatches an
//! alias to the unchanged standalone driver and a repo to [`run_managed`],
//! which drives [`trusty_mpm::daemon::managed_routes::inproject_cold_start`]
//! and then the existing [`super::launch::launch`] path.
//! Test: `run_target_tests.rs`.
use ;
/// What a `tm run` positional names.
///
/// Why: the positional now carries two disjoint meanings, and the disjointness
/// is real rather than assumed — a DOC-24 alias matches
/// `^[a-z0-9][a-z0-9._-]*$` (`core::standalone::registry::validate_alias`), so
/// it can never contain the `/` or `:` every repo form requires.
/// What: `Alias` is the pre-existing standalone target; `Repo` carries the
/// resolved GitHub identity and the URL to clone.
/// Test: `classify_sorts_alias_from_repo`.
pub
/// Sort a `tm run` positional into the case that decides its outcome.
///
/// Why: routing has to happen before any I/O, so an unparseable repo string is
/// rejected with a usable message rather than becoming a `git clone` that
/// fails minutes later — or, worse, a registry lookup for an alias that was
/// never meant to be one.
/// What: delegates the repo-or-not question to
/// [`super::register_args::looks_like_repo`] and the URL resolution to
/// [`super::register_args::resolved_url`], so `tm run` inherits every rejection
/// `tm register` makes (browser pastes into a repo's web UI, relative paths,
/// host-only URLs). Identity comes from
/// [`trusty_common::github_path::parse_owner_repo`] for the `owner/repo`
/// shorthand — the shared, tested primitive for a canonical identity path —
/// falling back to [`trusty_common::github_path::parse_github_path`] for a full
/// URL, which `parse_owner_repo` refuses by design.
/// Test: `classify_sorts_alias_from_repo`, `classify_resolves_shorthand`,
/// `classify_resolves_full_urls`, `classify_rejects_browser_pastes_and_paths`,
/// `shorthand_identity_agrees_with_url_identity`.
// #7166: every production caller now goes through `classify_run_target_with_account`
// (`run`'s `--account`-aware dispatch always supplies one, even when `None`);
// this account-less convenience form is kept only because the bulk of this
// module's own tests still exercise the pre-#7166 shape directly.
pub
/// `classify_run_target`, reconciling the `--account` flag (#7166).
///
/// Why: the global `--account` flag ([`crate::cli::Cli::account`]) and the
/// embedded `<login>@owner/repo` shorthand bind the SAME field, so the two
/// must be reconciled in one place rather than at each of the three callers
/// ([`run`], `classify_bare`, and `tm register`'s own resolution) —
/// see [`super::register_args::resolve_account`].
/// What: identical to `classify_run_target` otherwise; the resolved
/// account (flag wins, embedded is the fallback, a conflict between the two
/// is refused) is attached to a resulting [`RunTarget::Repo`] and ignored for
/// [`RunTarget::Alias`] — the standalone driver has no account concept.
/// Test: `classify_resolves_account_flag`, `classify_resolves_embedded_account`,
/// `classify_rejects_conflicting_account`.
pub
/// `tm run` handler.
///
/// Why: one command, two subsystems. Keeping the branch here rather than in
/// `main.rs` keeps the dispatch table flat and gives the routing decision a
/// testable home.
/// What: warns about flags that do not apply, classifies the target, and
/// dispatches — alias to `core::standalone`'s driver (unchanged), repo to
/// [`run_managed`].
///
/// Two flags reach an arm that cannot use them, and BOTH warn rather than being
/// dropped silently. `--task` is unimplemented on either arm. `--root` selects
/// the DOC-24 managed root, which the daemon-managed arm never consults: its
/// checkout location comes from `repos_root()` (`TRUSTY_MPM_REPOS_ROOT` >
/// `TRUSTY_MPM_WORKSPACE_ROOT` > config > `~/trusty-mpm-projects`), a different
/// setting entirely. A user passing `--root` there is trying to relocate the
/// checkout and would otherwise get no hint that it had no effect.
/// Test: `classify_sorts_alias_from_repo` covers the routing decision; the
/// managed arm's checkout half is covered by
/// `daemon::managed_routes::inproject_cold_start` tests.
pub async
/// Decide whether a token clap could not match names a repo to run (#6441).
///
/// Why: `tm <github-url>` has to reach the same register→load→run chain
/// `tm run <url>` already drives, and clap surfaces such a token through
/// [`crate::cli::Command::External`] — the same arm EVERY token clap cannot
/// resolve lands in: a typo (`statuss`), an ambiguous prefix (`sta`, which
/// matches `start`/`status`/`statusline`), and a retired subcommand
/// (`coordinator-tui`). So the gate is the whole design: each of those must
/// keep getting clap's usage error and the "did you mean?" hint, never a
/// managed run and never an alias lookup reporting "alias 'statuss' not found".
/// Only a token [`super::register_args::looks_like_repo`] accepts — the SAME
/// predicate `tm register` and `classify_run_target` use — is a repo.
/// What: `None` means "not a repo, hand it back to the usage-error path".
/// `Some(Ok(..))` is always [`RunTarget::Repo`]; `Some(Err(..))` is a
/// repo-shaped token that [`super::register_args::resolved_url`] refuses (a
/// host with no path, a browser paste into a repo's web UI), and that
/// descriptive error is what the user should see rather than a usage dump.
///
/// A relative path is `None` on purpose: it is neither a repo nor a
/// subcommand, and clap's usage error names the real problem more usefully
/// than a clone attempt would.
/// Test: `classify_bare_accepts_repo_shapes`,
/// `classify_bare_declines_subcommand_typos`,
/// `classify_bare_surfaces_resolved_url_errors`.
// #7166: `run_external` now always calls `classify_bare_with_account` (with
// `Some`/`None` depending on `--account`); this account-less form is kept
// only for this module's and the CLI parse layer's pre-#7166 tests.
pub
/// `classify_bare`, reconciling the `--account` flag (#7166).
///
/// Why: `tm --account bob <owner>/<repo>` and `tm bob@<owner>/<repo>` both
/// reach the `External` catch-all (the global `--account` flag parses before
/// the unrecognized token; see [`crate::cli::Cli::account`]'s doc), so
/// [`run_external`] needs the flag-aware classifier the same way [`run`] does.
/// Test: `classify_bare_accepts_account_flag_and_shorthand`,
/// `classify_bare_with_no_account_is_unchanged`.
pub
/// `tm <token>` where `<token>` matched no subcommand (#6441).
///
/// Why: keeping the fallback here rather than in `main.rs` keeps that file's
/// dispatch arm one line wide — it sits against the 500-SLOC production cap —
/// and gives the two-outcome decision a testable home next to the predicate it
/// depends on.
/// What: a repo-shaped token runs through [`run_managed`], the same cold start
/// `tm run <owner>/<repo>` uses, so an already-registered repo refreshes and
/// runs rather than erroring. Anything else goes to
/// [`reject_unknown_subcommand`], which reproduces the exact usage error the
/// invocation produced before [`crate::cli::Command::External`] existed.
/// Trailing tokens are refused rather than silently dropped: `tm <url> extra`
/// means something the CLI cannot honour.
/// Test: the repo half is `classify_bare`'s coverage plus `tm run`'s existing
/// managed-checkout tests; the usage-error half exits the process and is
/// covered at the parse layer by `cli_bare_unknown_subcommand_is_not_a_repo`,
/// `cli_bare_ambiguous_prefix_is_not_a_repo`, and
/// `cli_bare_retired_subcommand_is_not_a_repo`.
pub async
/// Print the usage error a token would have produced before #6441, and exit.
///
/// Why: `external_subcommand` makes clap ACCEPT any leading token it does not
/// recognize, so three previously-failing invocations now parse — a typo
/// (`tm statuss`), an AMBIGUOUS prefix (`tm sta`, which matches `start`,
/// `status`, and `statusline`), and a retired subcommand (`tm coordinator-tui`,
/// #1392). All three must still be refused, and with clap's own wording: its
/// "tip: some similar subcommands exist" line names the real candidates, which
/// a hand-written message cannot reproduce.
/// What: re-parses the ORIGINAL argv against a command carrying the same
/// arguments and subcommands but WITHOUT the external catch-all — which is
/// exactly the pre-#6441 definition — then prints that error, appends the
/// workspace "did you mean?" hint the way `main` does, and exits with clap's
/// own code.
///
/// 🔴 The rebuild is required. Calling `allow_external_subcommands(false)` on
/// the command `clap::CommandFactory` hands back does NOT disable the
/// catch-all: the flag reads back as `false` and `sta` still matches as the
/// external subcommand `sta`, because the derive already wired the match path
/// during augmentation. Only a command built fresh from
/// [`clap::Command::new`] honours the absence of the flag. Do not "simplify"
/// this back to a flag flip.
///
/// The re-parse cannot succeed — the token reached this function precisely
/// because nothing matched it — but an `Ok` is handled rather than unwrapped.
/// Test: `reject_unknown_subcommand_rebuild_reproduces_claps_error`,
/// `cli_bare_ambiguous_prefix_is_not_a_repo`,
/// `cli_bare_unknown_subcommand_is_not_a_repo`,
/// `cli_bare_retired_subcommand_is_not_a_repo`.
!
/// The `tm` command definition as it stood before #6441.
///
/// Why: see [`reject_unknown_subcommand`] — the pre-#6441 error can only be
/// reproduced by a command that never had the external catch-all wired in.
/// What: copies [`crate::cli::Cli`]'s own arguments and subcommands onto a
/// fresh [`clap::Command`], keeping `infer_subcommands` so an unambiguous
/// prefix still resolves and an ambiguous one still reports every candidate.
/// The name is spelled out because `clap::builder::Str` only accepts a
/// `&'static str`; `command_name_matches_the_rebuild` pins it against the
/// derive so the two cannot drift.
/// Test: `command_name_matches_the_rebuild`,
/// `reject_unknown_subcommand_rebuild_reproduces_claps_error`.
pub
/// The `#[command(name = ...)]` on [`crate::cli::Cli`].
///
/// Test: `command_name_matches_the_rebuild`.
pub const CLI_NAME: &str = "trusty-mpm";
/// Cold-start a daemon-managed session for a GitHub `owner/repo`.
///
/// Why: this is the whole point of #4990 — a bare string ends in a real
/// `SessionRecord` with a tmux pane, visible in `tm ls` and `tm sessions`, not
/// a blocking foreground `claude`.
/// What: (1) ensures the managed base clone at
/// `<repos_root>/<owner>/<repo>` via
/// [`trusty_mpm::daemon::managed_routes::inproject_cold_start::ensure_managed_checkout`],
/// which clones when absent and fails loud on a mismatched remote — a dirty
/// tree is NOT a failure there, it reports a skipped fast-forward through
/// `ManagedCheckout::refresh_skipped`, which this function prints; (2) hands
/// that directory to [`super::launch::launch`], the existing daemon-managed
/// launch path — which registers the project alias, provisions the per-session
/// worktree, prepares the session, registers the session with the daemon,
/// creates the tmux host, and attaches.
///
/// Step 2 is a HANDOFF, not a reimplementation: everything after the checkout
/// exists already and is shared with `tm launch`, so a managed session started
/// cold is indistinguishable from one started from a local checkout.
/// Test: the checkout half is covered by the `inproject_cold_start` tests and
/// `tests/inproject_cold_start.rs`; the launch half is `tm launch`'s existing
/// coverage.
async
/// Refuse a launch whose `gh` account cannot be chosen (#9091).
///
/// Why: a malformed `[accounts]` table used to surface only in the daemon log,
/// while the session's `gh` silently authenticated as nobody.
/// What: [`trusty_mpm::core::gh_org_accounts::resolve_gh_account`] — the rule
/// the clone and the spawn use — and [`preflight_verdict`] on its result.
/// Test: `preflight_refuses_a_broken_accounts_table_naming_the_file`.
pub
/// `Ok` for a chosen (or ambient) account; an operator-facing refusal naming
/// the resolver's error — for a broken table, the file and the parse error.
/// The origin is shown redacted (#9124).
/// Test: `preflight_refuses_a_broken_accounts_table_naming_the_file`,
/// `preflight_refusal_never_quotes_the_origin_token`.
pub
/// Resolve the bare form's raw tokens into a [`RunTarget`], before any I/O.
///
/// Why (#5850): clap stops applying global flags once it starts COLLECTING an
/// external subcommand's argv, so `tm <url> --user <login>` arrives as three
/// raw tokens. Keeping the lift, the account reconciliation, and the
/// extra-argument refusal in one pure function is what lets a test drive the
/// real parse output through to the target.
/// What: `Ok(None)` when the first token is not repo-shaped (the caller prints
/// clap's usage error); `Err` for a bad account flag, conflicting accounts, or
/// extra arguments; otherwise the classified target.
/// Test: `bare_form_trailing_user_reaches_the_repo_target`.
pub
/// Lift an `--account`/`--user`/`--u <login>` out of an external subcommand's
/// raw tokens (#5850, #9090).
///
/// Why: the bare form `tm <url>` reaches clap's `External` catch-all, and clap
/// applies no global flag to the argv it collects there — so the owner's
/// `tm https://github.com/duettoresearch/jev-matching --user bob-duetto` was
/// refused with "takes no further arguments" while the identical invocation
/// with the flag FIRST worked. The flag has no other meaning in this position,
/// so lifting it is a rewrite of nothing.
/// What: returns the tokens with the flag and its value removed, plus the
/// login. Every spelling is accepted in both `--user <login>` and
/// `--user=<login>` forms; a flag with no value is an error rather than a
/// silent drop, and so is a blank value (`--user=`), which `resolve_account`
/// would otherwise read as absent. Two occurrences naming DIFFERENT logins are
/// refused rather than resolved by position. The login is not validated here —
/// [`super::register_args::resolve_account`] owns that and runs on this value
/// downstream, so the two spellings cannot diverge.
/// Test: `bare_form_lifts_a_trailing_user_flag`,
/// `bare_form_lifts_a_trailing_u_flag`,
/// `bare_form_lifts_an_inline_account_value`,
/// `bare_form_rejects_a_trailing_flag_with_no_value`,
/// `bare_form_rejects_an_empty_account_value`,
/// `bare_form_leaves_unrelated_tokens_alone`.
pub
/// Is `token` the account flag, and does it carry its value inline?
///
/// Why: six spellings (`--account`, `--user`, `--u`, each with or without
/// `=value`) decided in one place, so [`split_trailing_account`] stays a loop
/// rather than a nest of string tests.
/// What: `None` when `token` is not the flag; `Some(None)` for the bare flag
/// (its value is the next token); `Some(Some(v))` for the `=` form.
/// Test: `bare_form_lifts_an_inline_account_value`.
/// Reconcile an account named BEFORE the repository with one named after it.
///
/// Why: `tm --user a <url> --user b` reaches [`run_external`] with both, and
/// picking one by position would silently clone as an account the operator did
/// not mean — the same posture
/// [`super::register_args::resolve_account`] takes for the flag versus the
/// embedded `<login>@owner/repo` selector.
/// What: agreeing values collapse; disagreeing ones are refused.
/// Test: `bare_form_refuses_two_different_accounts_around_the_repository`.