Skip to content

fix(send): take an unknown positive ID as a user, not a basic group - #53

Merged
neoromantic merged 4 commits into
mainfrom
fix/send-positive-id-47
Oct 21, 2026
Merged

neoromantic merged 4 commits into
mainfrom
fix/send-positive-id-47

Conversation

@neoromantic

@neoromantic neoromantic commented Oct 21, 2026 •

Copy link
Copy Markdown
Owner

What and why

tg send --to 4242 with a user ID the cache doesn't know (or a typo) built basic group 4242, so it sent to that group or failed with PEER_ID_INVALID. tg messages list and get --chat read the number the same way. Every tg command prints basic group N as -N, and a positive marked ID is a user.

Fixes #47

Fix

  • resolveUncachedId (src/commands/send/peer-resolver.ts) now takes an uncached positive number as user N with access hash 0, the way the resolver already handles a cached user that has no hash. It builds the peer with toInputPeerFromPeerId, so IDs are still converted only in telegram-peer-ids.ts. Telegram may refuse that user, but the peer never names a group. The 10+ digit phone rule and -N for basic groups stay as they were: an uncached number with 10 or more digits is still read only as a phone number, with no fallback to a user ID.
  • The issue offered two options: take the number as a user, or fail with INVALID_ARGS. I picked the user, as the issue's context suggests. A user ID is the only thing a positive marked ID can mean, and once the user is cached through @username or +phone, the same ID works, whatever its length.
  • Separate commit: a group or channel row stored under its bare ID by an older binary still running after the upgrade (until the next open re-keys it) now resolves through its type to the marked ID, with the row's access hash. Before, the "unknown = basic group" fallback covered a short bare group row by accident (the test from 40920e8 depended on it) and turned a short bare channel row into basic group N. A bare row with 10 or more digits, the usual shape of a channel ID, fell through to the phone rule and made a contacts.resolvePhone call, which could match an unrelated user with that number; it now resolves to the channel with no API call. Without this commit, the rule above would have made a short bare row user N.
  • Docs: docs/api-design.md (Peer Resolution) and docs/cli-commands.md (Send) describe the new rule, that an uncached 10+ digit number is only a phone number, and how to get a user cached so its ID works.

Callers checked: tg send and tg messages list/get (through resolveHistoryChat) are the only callers of resolvePeer. tg chats get --id and tg chats sync are cache-only (findCachedChatById) and haven't changed.

How I checked it

  • bun run check, bun run test (1560 pass) and bun run test:e2e (193 pass) all pass locally. No live Telegram run.

  • New tests, each failing before its change:

    • peer-resolver.test.ts: resolvePeer('4242') with an empty cache, and with messages cached only in group -4242, gives inputPeerUser 4242 with hash 0, not inputPeerChat; '-4242' still gives inputPeerChat 4242. A bare channel row 4242 and a bare supergroup row 1234567890 from an older binary resolve to the channel with its access hash and no API call, with a client that answers PHONE_NOT_OCCUPIED (before, the 10-digit one went to a phone lookup and failed).
    • send.test.ts: the send command with an empty cache calls messages.sendMessage with user 4242. resolvePeer('12345') with an empty cache gives a user, replacing the old "fallback to inputPeerChat" case.
    • messages-history-command.test.ts: resolveHistoryChat('4343') gives chat 4343 (the user's DM) and '-4343' gives -4343, with no API call.
  • Tests pass locally and use made-up data only

  • README, docs and affected demos follow the product content standard

  • Title follows Conventional Commits

🤖 Generated with Claude Code

neoromantic and others added 4 commits October 22, 2026 00:53
A 0.2.0 binary that is still running after the upgrade writes a group or
channel to chats_cache under its bare ID until the next open re-keys the
row. resolveCachedId skipped such a row, and a short number then reached
the fallback that builds basic group N. That gave the right peer for a
bare group row by chance, and basic group N for a bare channel row.

Now the bare row resolves through its type to the marked ID, with the
row's access hash, after users and the marked 0.2.0 lookup. This keeps
the group case working once the fallback stops building basic groups
(#47), and fixes the channel case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Every tg command prints basic group N as -N, and a positive marked ID is
a user. Yet a positive number the cache didn't know (and too short for a
phone number) became basic group N, so tg send --to 4242 with an uncached
user ID or a typo sent to basic group 4242, or failed with
PEER_ID_INVALID. tg messages list and get --chat read the same way.

Now such a number is user N with access hash 0, as for a cached user
without a hash: Telegram may refuse the user, but the peer never names a
group. The 10+ digit phone rule and -N for basic groups stay. The docs
say so and how to get the user cached.

Fixes #47

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The bare-row fix in 28f8834 also changed what happens to a supergroup or
channel row an older binary wrote under a 10+ digit bare ID, the usual
shape of a channel ID. Before, such a row fell through to the phone rule
and made a contacts.resolvePhone call, which could match an unrelated
user with that number. Now it resolves to the channel with no API call.
The test only covered the 4-digit 4242, which never reached the phone
rule, so a change that put the phone check first would still pass.

The case now runs for channel 4242 and supergroup 1234567890, with a
client that answers PHONE_NOT_OCCUPIED, and asserts no API call. Both
fail against the resolver before 28f8834.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The Send section said an uncached 10+ digit number "is tried as a phone
number first", which reads as a fallback to the user ID. There is none:
resolveByPhone fails with "Could not resolve phone number N" when no
account has the number. Current user IDs often have 10 digits, so a
reader would expect tg send --to with such an uncached ID to reach the
user. Now the section says the number is read only as a phone number,
the command fails if no account has it, and the ID works once the user
is cached, whatever its length. docs/api-design.md already said so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@neoromantic
neoromantic merged commit d7898e9 into main Oct 21, 2026
5 checks passed
@neoromantic
neoromantic deleted the fix/send-positive-id-47 branch October 21, 2026 21:37
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 send takes an unknown positive ID as a basic group

1 participant