feat(config): T-1249 — the contract is a decorator, and now a test
Every non-zero exit names the command that would fix it, and still exits non-zero. Both halves matter; the second is the one that gets lost, because a tool that explains itself beautifully and exits 0 looks MORE correct while having silently disabled its own gate. core/errors.py holds ReachError(message, fix=) and @handle_errors. core/logging.py holds @logged, emitting through console rather than a second sink — one output path, so there is nothing to drift. core/command.py composes them, and the order is load-bearing: handle_errors wraps logged, so the logger sees the original exception. Inverted, every failure would be recorded as "SystemExit" and the log would say nothing about what went wrong while looking like it worked. core/ raises SystemExit, not typer.Exit. A service must be callable from a test, another service, or a future second front end, and an exception type that only makes sense inside a CLI leaks the transport into every layer. The check router is retrofitted off its hand-rolled verdict-and-exit pattern — exactly the boilerplate this removes — and test_check_parity.py passes unchanged across the retrofit. That test predates the decorators and pins exit codes against the old script, so it is independent evidence, not a test tuned to match new behaviour. Unknown domains and unknown verbs now enumerate what exists instead of only saying no. That needed a shared group class, which collided with "no typer outside main.py and router.py" — resolved by sharpening the invariant rather than breaking it, since its purpose is that a SERVICE never knows it was called from a CLI. Transport now lives in main.py, router.py and core/cli.py; never in service.py, schemas.py or helpers.py. The upside is that cli.domain() carries the settings that were previously per-router decisions, including the load-bearing rich_markup_mode=None that one forgetful domain could have undone. test_conformance.py makes five invariants executable, AST-based rather than grep. Scoped to the package, not the 123 legacy scripts — and deliberately so: as T-1250 moves each script into domains/, it lands inside the scope and the rules start applying automatically, so the test's reach grows with the migration. Proven to fail before being trusted: removing @command and removing a fix= each produced a failure naming the file, the line and the reason. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+34
-2
@@ -76,6 +76,16 @@ class LazyDomainGroup(TyperGroup):
|
||||
return _load_domain(cmd_name)
|
||||
return super().get_command(ctx, cmd_name)
|
||||
|
||||
def resolve_command(self, ctx: typer.Context, args: list[str]):
|
||||
# Closed-set enumeration (D-263). Click's default is "No such command
|
||||
# 'x'" — which tells you that you are wrong without telling you what
|
||||
# would be right, the exact gap measured in pql. The accepted set is
|
||||
# sitting in DOMAINS, already loaded for --help, so naming it is free.
|
||||
if args and args[0] not in DOMAINS and super().get_command(ctx, args[0]) is None:
|
||||
listed = ", ".join(sorted({*DOMAINS, *super().list_commands(ctx)}))
|
||||
ctx.fail(f"unknown domain {args[0]!r}\n\nChoose one of: {listed}")
|
||||
return super().resolve_command(ctx, args)
|
||||
|
||||
def format_commands(self, ctx: typer.Context, formatter) -> None:
|
||||
# Deliberately does NOT call get_command. See the class docstring.
|
||||
rows = [(name, short) for name, (_target, short) in sorted(DOMAINS.items())]
|
||||
@@ -108,5 +118,27 @@ cli = typer.Typer(
|
||||
|
||||
|
||||
@cli.callback()
|
||||
def root() -> None:
|
||||
"""Present so an empty root is legal — see the module docstring."""
|
||||
def root(
|
||||
verbose: bool = typer.Option(
|
||||
False, "--verbose", "-v", help="Show progress events and full tracebacks."
|
||||
),
|
||||
no_input: bool = typer.Option(
|
||||
False, "--no-input", help="Never prompt. Hooks and agents should always pass this."
|
||||
),
|
||||
) -> None:
|
||||
"""Global options, declared once here so every domain inherits them.
|
||||
|
||||
A per-domain copy of these is how the two would drift apart — one router
|
||||
growing a `--verbose` that sets a different level, or forgetting `--no-input`
|
||||
entirely.
|
||||
|
||||
Note `--no-input` is about PROMPTING, not output format. Console already
|
||||
chooses JSONL versus rendered text from `isatty` with an `SR_OUTPUT_FORMAT`
|
||||
override; making this flag a second, conflicting way to say the same thing
|
||||
would leave nobody sure which one wins.
|
||||
"""
|
||||
from tooling.core import console, runtime
|
||||
|
||||
if verbose:
|
||||
console.set_level("debug")
|
||||
runtime.set_no_input(no_input)
|
||||
|
||||
Reference in New Issue
Block a user