From 66c0b189704c9e88bcd8a16118e56ed08b059409 Mon Sep 17 00:00:00 2001 From: Pouri <94605698+erfnzdeh@users.noreply.github.com> Date: Sat, 21 Nov 2026 17:41:38 +0330 Subject: [PATCH] Fix require_account fallback and send the resolved account to the daemon With require_account on (TLGR_REQUIRE_ACCOUNT or [defaults] require_account), resolve_account still fell back to [accounts] default and the active alias and only refused when nothing resolved. It now counts only the explicit sources (the positional, -a/--account, and TLGR_ACCOUNT, the documented environment form of -a) and refuses everything else locally with ACCOUNT_REQUIRED, exit 2, before anything is dispatched. With it off, run_op resolved the fallback account into a local variable while the daemon dispatcher read state.account, so the request carried account "" and the daemon answered ACCOUNT_REQUIRED. run_op now stores the resolved alias on state, so the documented fallback works end to end. Tests pin both modes: resolve_account directly, the CLI with a recording dispatcher (including live streams), and a real daemon on a temp socket. --- AGENT.md | 8 +- CHANGELOG.md | 11 ++ tests/test_require_account.py | 293 ++++++++++++++++++++++++++++++++++ tlgr/cli/gen.py | 5 + tlgr/cli/globals.py | 68 +++++--- 5 files changed, 356 insertions(+), 29 deletions(-) create mode 100644 tests/test_require_account.py diff --git a/AGENT.md b/AGENT.md index dcb5f5c..2886bce 100644 --- a/AGENT.md +++ b/AGENT.md @@ -1245,9 +1245,11 @@ re-drive reuses the original delivery id, so a receiver keyed on follow that are worth knowing as a caller: - **The account is always explicit.** The CLI resolves it (`-a` → - `TLGR_ACCOUNT` → `[defaults]` → active alias) and sends it; the daemon never - picks one for you and answers `ACCOUNT_REQUIRED` (exit 2) when it was not - given one. v1 used whichever alias came first out of a set, so a two-account + `TLGR_ACCOUNT` → `[accounts] default` → active alias) and sends it; the + daemon never picks one for you and answers `ACCOUNT_REQUIRED` (exit 2) when + it was not given one. With `require_account` on, only `-a` and + `TLGR_ACCOUNT` count, and the CLI refuses a command without one with the + same `ACCOUNT_REQUIRED` (exit 2) before it reaches the daemon. v1 used whichever alias came first out of a set, so a two-account user could send from the wrong identity with no signal. - **Version handshake with one restart.** A client newer than the running daemon restarts it exactly once and says so on stderr. `--no-daemon-restart` diff --git a/CHANGELOG.md b/CHANGELOG.md index 158e566..00a226a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,17 @@ codes documented in `AGENT.md` are the public API. ### Fixed +- **`require_account` refuses a command without an explicit account.** It + used to fall back to `[accounts] default` and then the active alias, and + refused only when nothing resolved at all, so the rail passed on exactly + the state it is meant to distrust. With it on, only `-a` (or its + positional form) and `TLGR_ACCOUNT` count, and a command without one is + refused locally with `ACCOUNT_REQUIRED`, exit 2, before anything reaches + the daemon. +- **The default-account fallback reaches the daemon.** `run_op` resolved + `[accounts] default` or the active alias and then sent `account: ""`, so + every command relying on that fallback was refused by the daemon with + `ACCOUNT_REQUIRED`. The resolved alias is now the one the request carries. - **Webhook filters work.** Both halves of `[webhook.filters]` were dead. `chats` was checked against a list nothing ever filled, so setting it dropped every event that had a chat; `@name` entries are now resolved diff --git a/tests/test_require_account.py b/tests/test_require_account.py new file mode 100644 index 0000000..63da0f2 --- /dev/null +++ b/tests/test_require_account.py @@ -0,0 +1,293 @@ +"""`require_account`: what it promises, and what the fallback does without it. + +README and AGENT.md say that with `require_account = true` every command must +name its account (`-a `), with no default-account fallback, and that a +violation exits 2. `resolve_account` did not do that: it fell back to +`[accounts] default` and then to the active alias, and only refused when +nothing at all resolved, so the rail passed on exactly the state it exists to +distrust. + +The other half was the opposite bug. Without `require_account`, `run_op` +resolved the fallback account and then kept it in a local variable: the +daemon dispatcher reads `state.account`, so the request went out with +`account: ""` and the daemon (which never picks one, COR-02) answered +ACCOUNT_REQUIRED. The documented fallback order worked on paper only. + +The behaviour pinned here: + +* `require_account` on: only the positional, `-a/--account` and + `TLGR_ACCOUNT` (the documented environment spelling of `-a`) count. The + configured default and the active alias are never consulted, and a command + without an explicit account is refused locally with ACCOUNT_REQUIRED, exit + 2, before anything is dispatched. +* `require_account` off: positional, `-a`, `TLGR_ACCOUNT`, `[accounts] + default`, then the active alias, and the alias that wins is the one the + daemon receives. +""" + +from __future__ import annotations + +import dataclasses +import json +from pathlib import Path +from typing import Any + +import pytest +from click.testing import CliRunner + +from tlgr.cli import cli, gen +from tlgr.cli.globals import CliState, resolve_account +from tlgr.core.accounts import AccountManager +from tlgr.core.errors import EXIT_USAGE, AccountRequiredError +from tlgr.core.paths import TlgrPaths +from tlgr.models.base import Request +from tlgr.ops._spec import OperationSpec, Surface + + +@pytest.fixture +def home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: + """A home with two accounts; `active` was added first, so it is active.""" + base = tmp_path / "home" + base.mkdir() + monkeypatch.setenv("TLGR_HOME", str(base)) + for key in ("TLGR_ACCOUNT", "TLGR_REQUIRE_ACCOUNT"): + monkeypatch.delenv(key, raising=False) + manager = AccountManager(base) + manager.add_account("active") + manager.add_account("other") + assert manager.get_active() == "active" + return base + + +def _config(base: Path, text: str) -> None: + TlgrPaths(base).config.write_text(text) + + +def _strict(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("TLGR_REQUIRE_ACCOUNT", "1") + + +# --------------------------------------------------------------------------- +# resolve_account itself +# --------------------------------------------------------------------------- + + +class TestFallbackMode: + def test_flag_wins(self, home): + assert resolve_account(CliState(account="other")) == "other" + + def test_positional_beats_flag(self, home): + assert resolve_account(CliState(account="other"), positional="third") == "third" + + def test_env_beats_config_default(self, home, monkeypatch): + _config(home, '[accounts]\ndefault = "other"\n') + monkeypatch.setenv("TLGR_ACCOUNT", "third") + assert resolve_account(CliState()) == "third" + + def test_config_default_beats_active(self, home): + _config(home, '[accounts]\ndefault = "other"\n') + assert resolve_account(CliState()) == "other" + + def test_active_alias_is_the_last_resort(self, home): + assert resolve_account(CliState()) == "active" + + def test_nothing_resolvable_is_left_to_the_daemon(self, tmp_path, monkeypatch): + """ "" goes out and the daemon refuses; a local refusal would break --dry-run.""" + empty = tmp_path / "empty" + empty.mkdir() + monkeypatch.setenv("TLGR_HOME", str(empty)) + monkeypatch.delenv("TLGR_ACCOUNT", raising=False) + monkeypatch.delenv("TLGR_REQUIRE_ACCOUNT", raising=False) + assert resolve_account(CliState()) == "" + + +class TestStrictMode: + def test_active_alias_is_refused(self, home, monkeypatch): + _strict(monkeypatch) + with pytest.raises(AccountRequiredError) as caught: + resolve_account(CliState()) + assert caught.value.code == "ACCOUNT_REQUIRED" + assert caught.value.exit_code == EXIT_USAGE + assert "require_account" in str(caught.value) + + def test_config_default_is_refused(self, home, monkeypatch): + _config(home, '[accounts]\ndefault = "other"\n') + _strict(monkeypatch) + with pytest.raises(AccountRequiredError): + resolve_account(CliState()) + + def test_config_switch_alone_turns_it_on(self, home): + _config(home, "[defaults]\nrequire_account = true\n") + with pytest.raises(AccountRequiredError): + resolve_account(CliState()) + + def test_env_off_overrides_the_config_switch(self, home, monkeypatch): + _config(home, "[defaults]\nrequire_account = true\n") + monkeypatch.setenv("TLGR_REQUIRE_ACCOUNT", "0") + assert resolve_account(CliState()) == "active" + + def test_explicit_flag_passes(self, home, monkeypatch): + _strict(monkeypatch) + assert resolve_account(CliState(account="other")) == "other" + + def test_explicit_positional_passes(self, home, monkeypatch): + _strict(monkeypatch) + assert resolve_account(CliState(), positional="other") == "other" + + def test_tlgr_account_counts_as_explicit(self, home, monkeypatch): + """AGENT.md documents `TLGR_ACCOUNT` as the environment form of `-a`.""" + _strict(monkeypatch) + monkeypatch.setenv("TLGR_ACCOUNT", "other") + assert resolve_account(CliState()) == "other" + + def test_require_argument_overrides_the_setting(self, home, monkeypatch): + with pytest.raises(AccountRequiredError): + resolve_account(CliState(), require=True) + _strict(monkeypatch) + assert resolve_account(CliState(), require=False) == "active" + + +# --------------------------------------------------------------------------- +# Through the CLI, with the dispatcher replaced +# --------------------------------------------------------------------------- + + +class _EmptyReq(Request): + pass + + +async def _never(ctx: Any, req: Any) -> dict[str, Any]: + raise AssertionError("a daemon op must not run locally") + + +SPEC = OperationSpec( + id="fixture.whoami", + request=_EmptyReq, + response=dict, + impl=_never, + summary="Report the account the request was sent for", + needs_account=True, + surface=Surface.DAEMON, + example={"ok": True}, + example_args="fixture whoami", + tags=frozenset({"infrastructure"}), +) + + +class _Recorder: + def __init__(self) -> None: + self.accounts: list[str] = [] + + def __call__(self, spec: Any, request: Any, state: CliState) -> dict[str, Any]: + self.accounts.append(state.account) + return {"ok": True, "op": spec.id, "account": state.account, "result": {}} + + +@pytest.fixture +def recorder(monkeypatch: pytest.MonkeyPatch) -> _Recorder: + sent = _Recorder() + monkeypatch.setattr(gen, "_dispatch", sent) + return sent + + +def _invoke(args: list[str]) -> Any: + return CliRunner().invoke(gen.build_command(SPEC), args, obj={}) + + +class TestThroughTheCli: + def test_the_fallback_account_reaches_the_dispatcher(self, home, recorder): + """The `run_op` bug: the resolved alias stayed in a local variable.""" + result = _invoke(["--json"]) + assert result.exit_code == 0, result.output + assert recorder.accounts == ["active"] + + def test_the_config_default_reaches_the_dispatcher(self, home, recorder): + _config(home, '[accounts]\ndefault = "other"\n') + result = _invoke(["--json"]) + assert result.exit_code == 0, result.output + assert recorder.accounts == ["other"] + + def test_an_explicit_flag_reaches_the_dispatcher(self, home, recorder): + result = _invoke(["--json", "-a", "other"]) + assert result.exit_code == 0, result.output + assert recorder.accounts == ["other"] + + def test_strict_mode_refuses_locally_with_the_documented_error( + self, home, recorder, monkeypatch + ): + _strict(monkeypatch) + result = _invoke(["--json"]) + assert result.exit_code == EXIT_USAGE + envelope = json.loads(result.stdout.splitlines()[0]) + assert envelope["ok"] is False + assert envelope["error"]["code"] == "ACCOUNT_REQUIRED" + assert envelope["error"]["exit_code"] == EXIT_USAGE + assert recorder.accounts == [], "nothing may be dispatched" + + def test_strict_mode_refuses_even_with_a_config_default(self, home, recorder, monkeypatch): + _config(home, '[accounts]\ndefault = "other"\n') + _strict(monkeypatch) + result = _invoke(["--json"]) + assert result.exit_code == EXIT_USAGE + assert recorder.accounts == [] + + def test_strict_mode_passes_an_explicit_flag(self, home, recorder, monkeypatch): + _strict(monkeypatch) + result = _invoke(["--json", "-a", "other"]) + assert result.exit_code == 0, result.output + assert recorder.accounts == ["other"] + + def test_strict_mode_also_guards_live_streams(self, home, monkeypatch): + frames: list[str] = [] + + def stream(spec: Any, request: Any, state: CliState): + frames.append(state.account) + return iter(()) + + monkeypatch.setattr(gen, "_stream_dispatch", stream) + _strict(monkeypatch) + live = dataclasses.replace( + SPEC, id="fixture.watch", tags=frozenset({"infrastructure", "live-stream"}) + ) + result = CliRunner().invoke(gen.build_command(live), ["--json"], obj={}) + assert result.exit_code == EXIT_USAGE + assert frames == [] + + +# --------------------------------------------------------------------------- +# End to end, against a real daemon on a temp socket +# --------------------------------------------------------------------------- + + +def _cli(args: list[str]) -> Any: + return CliRunner().invoke(cli, args) + + +async def test_the_active_alias_reaches_a_real_daemon( + live_daemon, stub_account, in_thread, monkeypatch +): + """Before the fix this answered ACCOUNT_REQUIRED from the daemon.""" + monkeypatch.delenv("TLGR_ACCOUNT", raising=False) + monkeypatch.delenv("TLGR_REQUIRE_ACCOUNT", raising=False) + result = await in_thread(_cli, ["--json", "profile", "get", "--no-full"]) + assert result.exit_code == 0, result.output + envelope = json.loads(result.stdout) + assert envelope["ok"] is True + assert envelope["account"] == stub_account + + +async def test_strict_mode_never_reaches_a_real_daemon( + live_daemon, stub_account, world, in_thread, monkeypatch +): + monkeypatch.delenv("TLGR_ACCOUNT", raising=False) + monkeypatch.setenv("TLGR_REQUIRE_ACCOUNT", "1") + result = await in_thread(_cli, ["--json", "profile", "get"]) + assert result.exit_code == EXIT_USAGE + envelope = json.loads(result.stdout.splitlines()[0]) + assert envelope["error"]["code"] == "ACCOUNT_REQUIRED" + assert world.called("GetFullUserRequest") == [] + + explicit = await in_thread(_cli, ["--json", "-a", stub_account, "profile", "get"]) + assert explicit.exit_code == 0, explicit.output + assert json.loads(explicit.stdout)["account"] == stub_account + assert len(world.called("GetFullUserRequest")) == 1 diff --git a/tlgr/cli/gen.py b/tlgr/cli/gen.py index f6c36ec..e11199a 100644 --- a/tlgr/cli/gen.py +++ b/tlgr/cli/gen.py @@ -152,6 +152,11 @@ def run_op(spec: OperationSpec, request: msgspec.Struct, state: CliState) -> dic # A local operation must not go looking for an account at all: reading the # active alias to attach it to `tlgr schema` would be a lie about what ran. account = resolve_account(state) if spec.needs_account else state.account + # The daemon dispatcher reads the account off `state`, so the resolved + # alias has to land there. Keeping it in a local only sent `account: ""`, + # and the daemon (which never picks one) refused every command that relied + # on `TLGR_ACCOUNT`, the configured default or the active alias. + state.account = account request_id = uuid.uuid4().hex # The dry-run short-circuit lives here, before any implementation runs, so diff --git a/tlgr/cli/globals.py b/tlgr/cli/globals.py index cd33502..fa3af86 100644 --- a/tlgr/cli/globals.py +++ b/tlgr/cli/globals.py @@ -15,6 +15,8 @@ import click +from tlgr.core.errors import AccountRequiredError + __all__ = [ "GLOBAL_FLAG_NAMES", "CliState", @@ -241,33 +243,47 @@ def resolve_account( """Resolve the account, in one place, in one order. positional → `-a/--account` → `TLGR_ACCOUNT` → `[accounts] default` → - the active alias. The daemon never picks for you (COR-02); when nothing - resolves and the operation needs one, that is a USAGE error naming the - ways to supply it. + the active alias. The daemon never picks for you (COR-02), so whatever + this returns is what the request carries. + + With `require_account` on (`TLGR_REQUIRE_ACCOUNT`, else `[defaults] + require_account`; *require* overrides both) only the explicit sources + count: the positional, `-a/--account`, and `TLGR_ACCOUNT`, which is the + documented environment spelling of `-a`. The configured default and the + active alias are never consulted, because they are exactly the state that + picks the wrong identity for a script that forgot to say which one it + meant. A command that names no account is then refused here, before + anything reaches the daemon. """ alias = (positional or state.account or _env_or("TLGR_ACCOUNT")).strip() - - if not alias: - try: - from tlgr.core.config import load_app_config - from tlgr.core.paths import default_base - - alias = (load_app_config().default_account or "").strip() - if not alias: - from tlgr.core.accounts import AccountManager - - # `default_base()` rather than the `CONFIG_DIR` constant: - # that one is captured at import, so a `TLGR_HOME` set after - # the first import was ignored here and the CLI read the - # *real* home — which is how a development run reaches a - # production tree at all. - alias = (AccountManager(default_base()).get_active() or "").strip() - except Exception: - alias = "" - - if not alias and (require or (require is None and _require_account_enabled())): - raise click.UsageError( - "No account specified. Pass -a , set TLGR_ACCOUNT, or set " - "[accounts] default in config.toml (see: tlgr account list)." + if alias: + return alias + + strict = _require_account_enabled() if require is None else require + if strict: + error = AccountRequiredError( + "require_account is on: this command needs an explicit account, and the " + "configured default and the active alias are not used" ) + error.hint = "Pass -a or set TLGR_ACCOUNT (see: tlgr account list)" + raise error + + try: + from tlgr.core.config import load_app_config + from tlgr.core.paths import default_base + + alias = (load_app_config().default_account or "").strip() + if not alias: + from tlgr.core.accounts import AccountManager + + # `default_base()` rather than the `CONFIG_DIR` constant: that one + # is captured at import, so a `TLGR_HOME` set after the first + # import was ignored here and the CLI read the *real* home, which + # is how a development run reaches a production tree at all. + alias = (AccountManager(default_base()).get_active() or "").strip() + except Exception: + alias = "" + # Nothing at all resolved: return "" and let the daemon answer + # ACCOUNT_REQUIRED. Refusing here would also refuse a `--dry-run`, which + # `run_op` answers locally without an account. return alias