Skip to content

Remembered contacts: resolve people by a saved alias, never by a guess - #68

Open
erfnzdeh wants to merge 4 commits into
mainfrom
feat/contact-aliases
Open

erfnzdeh wants to merge 4 commits into
mainfrom
feat/contact-aliases

Conversation

@erfnzdeh

Copy link
Copy Markdown
Collaborator

Closes #64

What was wrong

  • A bare ASCII word is a username (tlgr/models/peer.py:348 at 2f303da), so tlgr message send sara "hi" goes to the public @sara, who may be a stranger, when the agent meant the contact the user calls Sara.
  • Any other free text, including every Persian name, is a parse error (peer.py:351). There was no way to say "the person the user calls X", so agents ran contact search, picked a row and sent: the wrong-person failure mode.
  • TlgrError had no structured payload (tlgr/core/errors.py:111), so an error could not hand an agent machine-readable next steps.
  • plugin/skills/tlgr/SKILL.md:39 and AGENT.md:1449 said phone numbers are not accepted as chats; parse_peer_ref has accepted +phone for a while.

Changes

  • Name references. PeerRef gets a name kind (tlgr/models/peer.py): name:<text>, or any text no other form claims (non-ASCII, several words, ASCII that is not a valid username). A bare ASCII username-form word still means the username. Malformed handles (@ab, +abc, a non-Telegram URL) stay parse errors. normalize_name() builds the lookup key: NFKC, casefold, ZWNJ/ZWJ and direction marks removed, Arabic ye/kaf to Persian, Arabic diacritics and tatweel dropped, whitespace collapsed. No stemming.
  • Per-account alias store (tlgr/core/aliases.py): accounts/<alias>/aliases.json next to peers.json, written with write_private (atomic, 0600) under an advisory lock. Each record keeps the wording, marked id, cache kind, access hash, username and the display name at save time. A corrupt file is never overwritten and makes name resolution refuse, naming the file.
  • One choke point. PeerResolver.resolve (tlgr/core/peers.py) handles kind == "name": exact key lookup, then the stored hash, then the stored id through the existing id strategy. CLI params and /v1/op payloads already parse through parse_peer_ref, so every chat/user argument, batch line and gateway job filter accepts a name with no per-op change.
  • Never a guess. A miss raises the new PEER_NEEDS_USER (exit 2, PeerNeedsUserError), with error.details: reason (unknown, stale, ambiguous), reference, key, account, nothing_sent: true, candidates (near aliases, plus contacts whose name resembles the wording from one contacts.getContacts on the failure path; an exact contact-name match is still only a candidate), save_with, retry: once. The hint tells the agent to ask the user, save with contact alias set, and retry once. A stored peer that is no longer reachable (NOT_FOUND) is stale; INDETERMINATE passes through.
  • The overlap case. A bare word that is both a valid username form and a saved alias (sara) is refused as ambiguous, naming name:sara and @sara. This only triggers once the user saved such an alias. An unreadable alias file does not block bare-username sends.
  • error.details (tlgr/core/errors.py, tlgr/models/error.py, tlgr/transport/client.py): optional, emitted only when set, and carried across the socket, so the existing error JSON is unchanged.
  • Ops (tlgr/ops/contact.py): contact alias set <alias> <peer> [--replace] (the peer must be an id, @username, +phone, link or an existing alias; an alias that looks like an id, phone, @handle, link, me or saved is refused; repointing needs --replace and the reply reports replaced), contact alias list (one row per peer with all its aliases), contact alias remove <alias> (exact only; a miss exits 5 with lookalikes in the hint). List/remove are local and need no Telegram connection. Tagged infrastructure (no catalog id).
  • user dialog-status lets PEER_NEEDS_USER through instead of folding it into an unknown answer. resolve peer reports source: "alias".
  • Docs: AGENT.md ("Chat Resolution" and new "People by name" section, error shape), plugin/skills/tlgr/SKILL.md (rule 5, exit 2 row), docs/design/DECISIONS.md (syntax decision), CHANGELOG.md, regenerated docs/reference.

Tests

Test What it reproduces Fails on main?
test_aliases.py::TestNormalization::test_spellings_of_one_wording_share_a_key (13 cases) case, NFKC width, casefold, ZWNJ, ZWJ, Arabic ye/alef maksura/kaf, harakat, tatweel, direction mark yes (no normalizer)
TestNormalization::test_no_stemming sara/saras, ali/alireza, مامان/ماما stay distinct yes (import)
TestParsing::test_free_text_is_a_name Persian, multi-word, short ASCII, name: forms parse as names yes (parse error)
TestParsing::test_a_bare_username_still_means_the_username sara is still a username no (guard)
TestParsing::test_malformed_handles_are_still_parse_errors !!!, @ab, +abc, URL, empty name: partly (name: forms)
TestStore::* 0600 file, replace returns previous, corrupt file never overwritten, alias cannot shadow id/phone/@handle/link/me/saved, lookalikes exclude exact yes
TestResolve::test_an_exact_alias_resolves_from_the_store_without_network stored hash, zero RPCs yes
TestResolve::test_persian_spelling_variants_resolve_to_the_same_alias ZWNJ/Arabic letters/tatweel variants hit one alias yes
TestResolve::test_an_unknown_name_sends_nothing_and_asks_the_user PEER_NEEDS_USER, details, alias + contact candidates, public @sara not suggested, no resolveUsername yes
TestResolve::test_a_unique_exact_contact_name_is_still_only_a_candidate مامان contact is exact_name candidate, not resolved yes
TestResolve::test_a_stale_alias_asks_again unreachable stored id is stale, --replace in save_with yes
TestResolve::test_a_bare_word_that_is_also_an_alias_is_refused sara refused as ambiguous; name:sara and @sara each work yes
TestResolve::test_a_bare_word_without_an_alias_is_the_username_as_before no alias: unchanged no (guard)
TestResolve::test_aliases_are_per_account alias on work does not resolve on home yes
TestResolve::test_an_unreadable_store_refuses_rather_than_guessing CONFIG_ERROR for names; bare username still works yes
test_the_session_resolver_keeps_aliases_next_to_the_peer_cache daemon wiring yes
TestThroughTheDaemon::test_an_unknown_name_sends_nothing message.send to an unsaved name: no SendMessageRequest, details over the socket yes (was USAGE parse error, no details)
TestThroughTheDaemon::test_a_gateway_job_resolves_an_alias job_client.resolve_chat("name:sara") yes
TestThroughTheDaemon::test_dialog_status_asks_rather_than_answering_unknown dialog-status raises PEER_NEEDS_USER yes
TestOps::* (8 tests) set then send by name, Persian alias through any spelling, --replace required, already, exact peer required, alias as peer, list grouped per person, remove exact only yes (ops absent)
test_errors_map.py::TestErrorDetails::* details only when set, v1 error key kept, survives error_from_body yes

Not changed, and why

  • Job action queue items (forward to:) store the destination string and resolve it when the item runs, so an alias repointed with --replace while items are queued sends them to the new peer. Gateway filters resolve once at start/reload. Storing the resolved id at enqueue time is a job-actions change, not part of this issue.
  • Fuzzy or inflection-aware resolution (telegram-mcp's Russian heuristics): suggestions only, by design. No stemming was asked for.
  • Dialog titles as suggestions: only the contact list is checked (one cheap call). Scanning dialogs on every miss is expensive and would suggest group titles for a person's name.
  • new_peers_per_day (dead config noted in the report) and echoing the resolved name in every mutating result: out of scope.
  • format_error_json (the legacy flat v1 formatter) does not carry details; it is a frozen compatibility shape. The envelope and --results-only inner object both do.

Not verified

  • Against live Telegram: contacts.getContacts for suggestions and stored access hashes are exercised only through the fake client.
  • zsh/bash quoting of Persian aliases was checked by reading, not in a real shell session.

make check: pass (lint, mypy, 13756 tests, docs and parity regenerated with no diff).

details is emitted as error.details only when an error sets it, so the
existing error JSON is unchanged, and error_from_body carries it across the
socket. PEER_NEEDS_USER (exit 2) is the error a contact alias miss raises.
A new PeerRef kind, name, covers name:<text> and any text no other form
claims (non-ASCII, several words, ASCII that is not a username). A bare
ASCII username-form word still means the username. PeerResolver.resolve
looks the normalized wording up in a per-account aliases.json next to the
peer cache; only an exact key resolves. A miss raises PEER_NEEDS_USER with
lookalike aliases and contacts as candidates, and a bare word that is both
a username and a saved alias is refused as ambiguous.
set takes an exact peer (id, @username, phone, link or an existing alias),
refuses a wording that would shadow a real reference, and needs --replace
to repoint. list is one row per peer with all its aliases; remove is exact
only and suggests lookalikes on a miss. list and remove are local.
AGENT.md and the skill tell agents to pass the name instead of picking a
contact search row, and what to do on PEER_NEEDS_USER. The chat reference
list now includes +phone, links and aliases. DECISIONS records why name: and
why a bare username keeps its meaning.

This branch has not been deployed

No deployments
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.

Remembered contacts: resolve people by a saved alias, never by a guess

1 participant