Skip to content

feat(messages): add tg messages wait and watch - #166

Merged
neoromantic merged 1 commit into
mainfrom
feat/messages-wait
Nov 17, 2026
Merged

neoromantic merged 1 commit into
mainfrom
feat/messages-wait

Conversation

@neoromantic

Copy link
Copy Markdown
Owner

What and why

An agent in a conversation has to block until someone replies; until now it could only poll tg sql. This adds two read-only commands over what the daemon stores in the account's cache:

  • tg messages wait [--chat <peer>] [--after <id>] [--timeout 60] [--settle 0] returns the new messages oldest first, in the same item shape as tg messages list (deleted ones left out): { success: true, data: { messages, timedOut } }. On timeout it is { messages: [], timedOut: true } with exit 0.
  • tg messages watch [--chat <peer>] prints one item per line (NDJSON) as each message is stored, until SIGINT or SIGTERM, then exits 0 through the normal exit path.

How it works: the command reads PRAGMA data_version every 200 ms and, when another connection committed, reads the messages_cache rows past the last rowid it saw (an upsert keeps a row's rowid, so edits are not new). No Telegram connection, no second session; the daemon stays read-only. Only resolving a --chat the cache doesn't know asks Telegram, as tg messages list does.

Rules:

  • Without --after, only messages stored after the command started count, and only when newer than every message of their chat already in the cache, so history the daemon backfills meanwhile doesn't count.
  • --after <id> needs --chat (INVALID_ARGS otherwise) and returns the chat's cached messages newer than it at once, else waits for them.
  • --settle N: after the first new message, keep collecting until N quiet seconds, never past --timeout.
  • Without a daemon running for the account (daemon.pid names a live process and the account's daemon rows are that process's, as tg status checks), both fail at once with the existing DAEMON_NOT_RUNNING (exit 1), details: { account_id } and a suggestion to run tg daemon start.

Docs: docs/cli-commands.md (new section with the timeout result), docs/real-time.md (what the daemon stores live, so how fresh it is), docs/api-design.md (error code), README (comparison row, usage and agent line), CHANGELOG.

Before / After

Before: tg messages wait → unknown command.

After:

$ tg messages wait --chat @teamchat --after 1200 --timeout 60
{ "success": true, "data": { "messages": [ { "chatId": -1001234567890, "messageId": 1201, … } ], "timedOut": false } }
$ tg messages wait --timeout 1
{ "success": true, "data": { "messages": [], "timedOut": true } }

Testing

  • bun run check
  • src/__e2e__/messages-wait.e2e.test.ts (9 tests): the test process writes messages into the cache while the CLI runs, with a simulated running daemon (daemon.pid with a live PID and daemon rows for it). Covers the return within 1 s of the write (backfilled older message not counted), the timeout result, --after at once (deleted left out) and waiting after it, --after without --chat, --settle with another chat's message filtered out, both daemon-not-running cases, and watch printing NDJSON lines and exiting 0 on SIGTERM.
  • src/__tests__/new-messages.test.ts: the feed over two connections (backfill, a chat new to the cache, edits, deleted rows, --chat, --after).
  • Full bun run test (2246 pass) and bun run test:e2e (5 unrelated timeouts under parallel load; those files pass alone).

Fixes #24

🤖 Generated with Claude Code

wait blocks until the daemon stores a new message and returns the new
messages oldest first, or { messages: [], timedOut: true } after
--timeout; watch streams them as NDJSON until SIGINT or SIGTERM. Both
poll only the account's cache (PRAGMA data_version, then the rows past
the last rowid seen) and fail with DAEMON_NOT_RUNNING without a daemon.

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

Wait for new messages: tg messages wait

1 participant