Skip to content

fix(accounts): keep tg accounts switch when config.json sets activeAccount - #63

Merged
neoromantic merged 5 commits into
mainfrom
fix/accounts-switch-61
Oct 22, 2026
Merged

neoromantic merged 5 commits into
mainfrom
fix/accounts-switch-61

Conversation

@neoromantic

@neoromantic neoromantic commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

Rebased onto main after #59 merged.

Problem

Every command except tg config and tg skill makes the account named by activeAccount in config.json active at startup. tg accounts switch --id <id> changed only data.db, so with the key set the switch lasted one command: the next one switched back and, since #17, read the configured account's cache.

-q/--quiet also returned from the root setup before that step, unlike --format quiet, so a quiet command could use another account and skip the strict config check.

Fix

  • tg accounts switch writes the new ID to activeAccount when the key is set and differs, before it touches data.db. If that write fails, the switch fails with GENERAL_ERROR (details.path) and changes neither file. Other keys stay; without the key, or with the same value, the file keeps its exact bytes and is never created.
  • -q now runs the same startup step as the other formats.
  • tg daemon skips that step, as tg config and tg skill do. The daemon's own --quiet/-q also sets the root flag, so with the step running a quiet daemon stopped silently (exit 3 or 1, no output) on an invalid or unreadable config.json, a file it never reads. Now tg daemon start runs whatever the file holds, quiet or not; the next other command reports it.
  • The startup step finds the subcommand the way citty does, skipping the value of -f/--format. Before, tg -f pretty config set activeAccount 1 saw pretty, ran the strict check and couldn't repair an invalid config.json; tg -f json skill failed on one.
  • A config.json that can't be read (for example for lack of permission) fails at startup with GENERAL_ERROR (details.path) instead of a Bun stack trace; under -q it prints nothing. Without this, the -q change above would have exposed -q to that crash.
  • docs/configuration.md says which wins: while activeAccount is set it wins at every start, tg accounts switch updates it, and tg config set activeAccount or a hand edit takes effect from the next command. It lists the failure modes as they are: an invalid file fails every command except tg config, tg skill and tg daemon with INVALID_ARGS, an unreadable one with GENERAL_ERROR, and the switch fails only when it has to write and can't. It also notes that login doesn't write the key yet: tg auth login / login-qr don't update activeAccount in config.json, so the next command uses the configured account #62.

Tests

src/__e2e__/multi-account.e2e.test.ts, one env per test, two accounts with account 1 active:

  • no config.json: the switch to 2 lasts (chats list, every cache command, messages get) and no config.json appears
  • {activeAccount:1, cache…, note}: the switch lasts, the file becomes activeAccount: 2 with the other keys kept, tg config get activeAccount returns 2 (red before the fix: chats list returned account 1's chat)
  • a config without the key, and one that already names account 2: the switch lasts and the file is byte-identical
  • tg config set activeAccount 1 after the switch wins from the next command
  • -q chats list with activeAccount: 2 applies it
  • a read-only config.json: the switch fails with GENERAL_ERROR, and data.db still has account 1 active when read directly, before any other command could reapply activeAccount (skipped as root). Moving setActive above the config write makes this test fail.

src/__e2e__/config.e2e.test.ts:

  • tg config set repairs an invalid config.json with no flag, -q, -f pretty and --format quiet; tg -f json skill manifest runs with one (both red before the subcommand fix)
  • tg daemon start --quiet, -q and with no flag, with an invalid config.json: the daemon runs and logs that there's no account; tg daemon status reports stopped (red before: --quiet exited 3 with empty stderr)
  • an unreadable config.json: tg accounts list fails with GENERAL_ERROR and details.path, tg -q accounts list exits 1 with empty stderr (red before; skipped as root)

bun run check, bun run test and bun run test:e2e pass locally. No Telegram call changes; a live check against a test session in a temp data dir runs when the PR ships.

Fixes #61

🤖 Generated with Claude Code

@neoromantic
neoromantic force-pushed the fix/per-account-cache-17 branch from 66cb137 to 3c13b0f Compare October 22, 2026 09:48
Base automatically changed from fix/per-account-cache-17 to main October 22, 2026 09:51
@neoromantic
neoromantic changed the base branch from main to stack/pr59-base October 22, 2026 09:54
neoromantic and others added 5 commits October 22, 2026 14:57
Every command except tg config and tg skill makes the account named by
activeAccount in config.json active at startup. tg accounts switch only
changed data.db, so with the key set the switch lasted one command and
the next one read the configured account's cache again.

The switch now writes the new ID to activeAccount when the key is set
and differs, before it touches data.db, so a config.json that can't be
read or written fails the switch with nothing changed. Without the key
config.json is not created and data.db alone holds the active account.

-q/--quiet returned from the root setup before that startup step, unlike
--format quiet, so a quiet command could use another account and skip
the config check. It now runs the same step.

Fixes #61

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…json

The read-only test checked "changes nothing" with tg chats list, but that
command's start makes activeAccount 1 active again, so it passed even when
the failed switch had already written account 2 to data.db. Reading the
active account straight from data.db before any other command makes the
test fail if the switch updates data.db before config.json.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The root setup took the first argument without a leading dash as the
subcommand, so in `tg -f pretty config set activeAccount 1` it saw
"pretty", ran the strict config.json check and failed with INVALID_ARGS:
tg config could not repair an invalid file with a spaced format flag, and
tg skill failed on it. It now skips the value of -f/--format, as citty does
when it picks the subcommand.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The startup step rethrew a read error such as EACCES, so every command
except tg config and tg skill printed a Bun stack trace with no JSON error.
Since -q now runs that step too, tg -q <command> crashed the same way. The
read error is now a GENERAL_ERROR with details.path, and -q prints nothing.

The Runtime Behavior docs said tg accounts switch fails with INVALID_ARGS
or GENERAL_ERROR when config.json can't be read or written. An invalid file
fails every command at start, not the switch, and the switch fails only
when activeAccount has to change and the write fails; they now say so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…lently

The daemon's own -q/--quiet also sets the root quiet flag. Since -q no
longer skips the startup check of config.json, an invalid or unreadable
file stopped `tg daemon start --quiet` with exit 3 or 1 and no output,
so a supervised daemon died over a cache TTL typo with nothing in its
logs. The daemon reads neither activeAccount nor any other key of
config.json, so tg daemon now skips the check like tg config and tg
skill; the next other command reports the file.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic force-pushed the fix/accounts-switch-61 branch from 5fc45ef to 4cd4056 Compare October 22, 2026 10:59
@neoromantic
neoromantic changed the base branch from stack/pr59-base to main October 22, 2026 10:59
@neoromantic neoromantic reopened this Oct 22, 2026
@neoromantic
neoromantic merged commit 9cb51d3 into main Oct 22, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/accounts-switch-61 branch October 22, 2026 11:06
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.

tg accounts switch is undone by activeAccount in config.json

1 participant