tgcli icon indicating copy to clipboard operation
tgcli copied to clipboard

refactor: simplify the operations/command layer (#49, #43)

Open kfastov opened this issue 5 months ago • 0 comments

Cleanup of the operations/command layer the warm-backend work produced — no user-visible behavior change (same output incl --json, same errors/exit codes). Addresses #49 and #43.

Changes

  • Invoke-first (perf). runOperation now calls invoke directly (one loopback round-trip on the warm path instead of ping+invoke). Only a ServerUnavailableError (no control.json / refused loopback connection — detected via isFetchConnectionFailure) triggers ensureServer + a single retry. A 4xx/5xx op error, a relayed sendError, and a request timeout/abort are NOT connection failures and never spawn a server (preserves the send-timeout contract).
  • Source-resolution dedup. resolveFromSource({ source, fetchLive, fetchArchive }) and selectMessageResults(...) collapse the duplicated live/both/archive/live-fallback logic across messagesGet/messagesContext/messagesList/messagesSearch. Identical behavior.
  • Generic passthrough + thinner handlers (subsumes #43). A small PASSTHROUGHS table generates 16 one-line ops via buildPassthroughOps; only composite ops stay hand-written. All 44 ops and their result shapes are preserved (the MCP tools and /control/invoke depend on them). The three send* handlers share buildCommonSendArgs instead of each rebuilding the flag map (same validation order → same first-error).
  • Audits. Source consistency: every single-source result comes wholly from one source; both merges via mergeMessageSets preserving each row's source; archive→live fallback surfaces usedLiveFallback. No mixing found. TG calls: one follow-up reported (not changed, since it affects which labels appear) — resolveLiveMetadata issues an extra getPeerMetadata when username isn't cached even though the fetch already returns peerTitle; candidate to cache/skip later.

Tests

npm test → 349 passing (336 + 13 new: invoke-first vs spawn-on-connection-failure incl. op-error-does-not-spawn, the source-resolution helper across all source modes, the generic passthrough forwarding, handlers passing args through). No node_modules committed; comments behavioral.

Note

#49's "minimize TG API calls" is covered except the one reported resolveLiveMetadata round-trip (a safe-to-defer follow-up). #43 (thin handlers) is addressed via the passthrough + shared send args.

kfastov avatar Jun 04 '26 14:06 kfastov