Skip to content

Fix require_account fallback and send the resolved account to the daemon - #55

Merged
erfnzdeh merged 1 commit into
mainfrom
fix/require-account
Nov 21, 2026
Merged

erfnzdeh merged 1 commit into
mainfrom
fix/require-account

Conversation

@erfnzdeh

Copy link
Copy Markdown
Collaborator

Problem

README and AGENT.md say require_account = true means every command must name its account, with no default-account fallback, and that a violation exits 2. Two things in the CLI did not match:

  1. Strict mode still fell back. resolve_account (tlgr/cli/globals.py) tried [accounts] default and then the active alias, and only refused when nothing resolved at all. So the rail passed on exactly the state it is meant to distrust: a script that forgot -a ran as whichever account happened to be the default.
  2. The fallback never reached the daemon. run_op (tlgr/cli/gen.py) resolved the account into a local variable, but the daemon dispatcher reads state.account. A command without -a therefore sent account: "" and the daemon (which never picks one, COR-02) answered ACCOUNT_REQUIRED, so the documented fallback order only worked on paper. run_live already assigned to state.account; run_op did not.

Behaviour chosen (from the docs)

require_account on (TLGR_REQUIRE_ACCOUNT=1, or [defaults] require_account = true; TLGR_REQUIRE_ACCOUNT=0 still overrides the config):

  • Only explicit sources count: the positional alias, -a/--account, and TLGR_ACCOUNT.
  • [accounts] default and the active alias are never consulted.
  • A command that needs an account and has no explicit one is refused locally, before any dispatch, with ACCOUNT_REQUIRED, exit 2 (the code and exit status the daemon uses and AGENT.md documents). Previously the rare path that did refuse raised a plain Click USAGE error instead.
  • TLGR_ACCOUNT counts as explicit because AGENT.md lists it as the environment form of -a, and the root -a option already takes its default from it, so the CLI cannot tell the two apart at that layer anyway. If we want strict mode to reject the env var too, that needs the root option to report its parameter source and is a separate change.

require_account off: positional, -a, TLGR_ACCOUNT, [accounts] default, then the active alias, and the alias that wins is now the one the request carries to the daemon. When nothing resolves the CLI still sends "" and lets the daemon refuse, as before; refusing locally there would also refuse --dry-run, which run_op answers without an account (the registry contract tests pin that).

Local operations are untouched (needs_account=False for every local op, so resolve_account is never called for them).

Tests

tests/test_require_account.py (23 tests):

  • resolve_account directly: the full fallback order, strict mode refusing the active alias and the config default, the config switch alone turning it on, the env override, explicit -a / positional / TLGR_ACCOUNT passing.
  • Through a generated command with a recording dispatcher: the fallback alias and the config default reach the dispatcher; strict mode refuses with the JSON ACCOUNT_REQUIRED envelope, exit 2, and dispatches nothing; live streams are guarded too.
  • End to end against a real daemon on a temp socket: tlgr --json profile get with no -a now succeeds as the active account (it answered ACCOUNT_REQUIRED before), and with strict mode on it is refused without any Telegram call, while -a work goes through.

Against origin/main the new file has 11 failures; with this change it passes. make check (lint, typecheck, test, docs, parity) is green locally: 13569 passed.

Related

#52 (tlgr.Client) and #53 (tlgr batch) each enforce the strict rule themselves. They may touch tlgr/cli/gen.py too, so one of them may need a rebase; the diff here is limited to account resolution. With this merged, #53's own per-line check becomes redundant but stays harmless.

Not changed here: account-management ops (account info, account sync, ...) are local and resolve through ops/_auth.resolve_alias, which still falls back to the active alias even with require_account on. Whether strict mode should cover those is worth a separate look.

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.
@erfnzdeh
erfnzdeh merged commit 038f9c8 into main Nov 21, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant