Skip to content

feat: read-only mode and a chat allowlist for agents - #168

Merged
neoromantic merged 3 commits into
mainfrom
feat/read-only-mode
Nov 17, 2026
Merged

neoromantic merged 3 commits into
mainfrom
feat/read-only-mode

Conversation

@neoromantic

Copy link
Copy Markdown
Owner

What and why

An agent that can run tg could also send messages or make any tg api call. This adds two opt-in guards, set in config.json or, for one process, in the environment:

  • Read-only mode (readOnly, TELEGRAM_SYNC_CLI_READ_ONLY=1). Every command that changes something on Telegram fails before it connects with the new code READ_ONLY (exit 1). That covers tg send, tg api methods whose name after the namespace doesn't start with get, search, resolve or check, and tg auth login, login-qr and logout (they add or end a session). Local commands and reads still run.
  • Chat allowlist (allowedChats; TELEGRAM_SYNC_CLI_ALLOWED_CHATS replaces it when set). A command that names a chat outside the list fails with CHAT_NOT_ALLOWED (exit 1, details: { chat, id }). The target and the list entries resolve the same way, cache-first, so @name and its marked ID count as the same chat and me is the account's own ID. tg chats list, tg chats search and tg messages search without --chat show only allowed chats. tg sql and tg api can't be scoped, so they are off while a list is set. Contacts, tg user, tg me and tg status are not limited.
  • tg status shows access: { read_only, allowed_chats }, so an agent that was refused can see why.

These guard against mistakes. They are not a security boundary, and the docs say so.

Exhaustive by construction. src/commands/access.ts has one COMMAND_ACCESS table with an entry for every command path in the citty tree (now in src/cli.ts, so tests can walk it). guardCommands wraps every run, and a command without an entry throws at start and fails the unit test. Classifying a new command takes one line, for example 'messages read': { write: true, chatArgs: ['chat'] } or 'messages wait': { chatArgs: ['chat'] }.

Before / After

$ TELEGRAM_SYNC_CLI_READ_ONLY=1 tg send --to me -m hi
# before: sends
# after:  {"success":false,"error":{"code":"READ_ONLY","message":"Read-only mode is on, and tg send changes Telegram","details":{"command":"send"},...}}  exit 1

$ TELEGRAM_SYNC_CLI_ALLOWED_CHATS=@teamchat tg messages list --chat @alice
# after:  {"success":false,"error":{"code":"CHAT_NOT_ALLOWED","message":"Chat @alice is not in allowedChats","details":{"chat":"@alice","id":10},...}}  exit 1

Tests

  • src/__tests__/access.test.ts: the table and the tree match both ways, an unclassified command is refused, every command in the tree is refused in read-only mode if and only if it is a write, the tg api read/write split, and sql/api refused while an allowlist is set.
  • src/__tests__/config.test.ts: key validation, tg config set parsing, env overrides (read-only can only be turned on; the list replaces config even when empty).
  • src/__e2e__/access.e2e.test.ts, through the real CLI with no credentials: every write command from the tree refused, every chat-scoped command from the tree refusing an outside chat, @name and the ID as one chat, me as the own ID, filtered chats list / chats search / messages search (with --count), sql/api refused, the env list replacing config, and tg status output.
  • bun run check, bun run test (2258 pass), bun run test:e2e (395 pass).

Fixes #26

🤖 Generated with Claude Code

neoromantic and others added 3 commits November 18, 2026 00:43
Tests can import the tree and walk it without running the CLI.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
readOnly in config.json or TELEGRAM_SYNC_CLI_READ_ONLY refuses every
command that changes Telegram with READ_ONLY before it connects: tg send,
tg api methods not named get/search/resolve/check, and tg auth login,
login-qr and logout. allowedChats or TELEGRAM_SYNC_CLI_ALLOWED_CHATS limits
commands to those chats: a command that names another chat fails with
CHAT_NOT_ALLOWED, chats list, chats search and messages search show only
allowed chats, and tg sql and tg api are off. tg status shows both.

Every command of the tree is classified in COMMAND_ACCESS, and a command
without an entry fails at start and in the tests.

Fixes #26

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tg messages read marks a chat read on Telegram, so read-only mode refuses
it, and its --chat must be allowed. tg messages wait, watch and unread take
an allowed --chat; without one they read every chat, so with allowedChats
set the new-messages feed keeps only allowed chats in its SQL and unread
keeps only allowed chats among its targets. chats list --unread keeps the
allowlist filter.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic merged commit 50b191f into main Nov 17, 2026
5 checks passed
@neoromantic
neoromantic deleted the feat/read-only-mode branch November 17, 2026 20:53
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.

Read-only mode and a chat allowlist for agents

1 participant