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>
48 lines
1.8 KiB
Python
48 lines
1.8 KiB
Python
"""`@command` — the whole contract in one decorator (D-263).
|
|
|
|
Cross-cutting concerns are decorators, never call-site discipline. The point of
|
|
composing them here is that a command author **cannot apply half the contract**:
|
|
there is no way to get logging without error handling, or to remember one and
|
|
forget the other on the 40th command of a long porting session. That failure
|
|
mode is the reason D-263 makes these decorators rather than conventions.
|
|
|
|
Usage, and it goes UNDER the Typer registration so it wraps the function Typer
|
|
will call:
|
|
|
|
@app.command("client-version")
|
|
@command
|
|
def client_version() -> None:
|
|
...
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Callable
|
|
from typing import Any, TypeVar
|
|
|
|
from tooling.core.errors import handle_errors
|
|
from tooling.core.logging import logged
|
|
|
|
F = TypeVar("F", bound=Callable[..., Any])
|
|
|
|
# Set by @command and asserted by the conformance test. A marker attribute is
|
|
# used rather than inspecting the composition after the fact, because unwrapping
|
|
# functools.wraps chains to prove "this was decorated" is brittle in exactly the
|
|
# way a conformance test must not be.
|
|
MARKER = "__reach_command__"
|
|
|
|
|
|
def command(func: F) -> F:
|
|
"""Compose the invocation contract onto one command function.
|
|
|
|
**Order is load-bearing.** `handle_errors` wraps `logged`, not the reverse:
|
|
the logger's `finally` then sees the ORIGINAL exception and records its type
|
|
as the outcome. Invert them and the error handler converts everything to
|
|
`SystemExit` first, so every failure is logged as "SystemExit" and the
|
|
record says nothing about what actually went wrong — while still looking
|
|
like it worked.
|
|
"""
|
|
wrapped = handle_errors(logged(func))
|
|
setattr(wrapped, MARKER, True)
|
|
return wrapped # type: ignore[return-value]
|