Pre-launch hardening: KOL module, Telegram, scanners, WS resilience

Big-picture changes since b941223:

KOL pipeline (new) — Substack/podcast/blog RSS → AI ticker extraction →
on-chain wallet diff → talks-vs-trades divergence detection. Daily polls,
19 feeds, divergence emits Post + Telegram fan-out.

Telegram push (new) — walletless free tier + wallet-linked Pro upgrade,
in-bot preference commands (/trump /btc /funding /kol /conf /quiet),
signed-envelope API for dashboard. Disconnect-wallet keeps free
subscription.

BTC funding-rate reversal scanner (new) — hourly cron, 30d cumulative
funding threshold + mean-revert + 7d price confirm, emits via
/api/signals/ingest. BTC bottom-reversal scanner promoted to System 2.

WS broadcast rewrite — per-client send timeout + parallel fan-out
(asyncio.gather). Fixes "Binance WS no close frame" reconnect storms +
APScheduler 11-min job misses, both caused by one slow client stalling
the kline loop.

Error visibility — three silent-error sites (trumpstruth/truth_social
fetchers, funding_reversal scanner) now include exception type name so
httpx ConnectError-style empty-message errors stop logging blank lines.
Telegram bot loop now classifies ReadTimeout vs network vs unknown +
logger.exception for the unknown bucket.

Security hygiene — trumpsignal.db untracked from git (held subscriber
wallets + encrypted HL keys + 22 bot trades); .gitignore now blocks
*.db/.next/backups. CORS only allows FRONTEND_URL in production.

New ops scripts —
  - scripts/preflight.py: env/DB/Telegram/AI auth verification gate
  - scripts/backup_db.sh: cron-friendly daily DB backup (SQLite + Postgres)
  - scripts/seed_kol_wallets.py: idempotent KOL on-chain wallet seeder

15 new Alembic migrations (007-021) covering convex strategy fields,
phase-1 safety, two-system frozen exits, invalidation prices, dynamic
SYS2 leverage, staged de-risk + pyramiding, peak gain tracking, risk
mode, auto-trade + grow flags, KOL module, KOL on-chain, KOL divergence,
Telegram bindings + walletless.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
k
2026-05-25 00:52:56 +08:00
parent b941223c88
commit 5fb1d52026
81 changed files with 13251 additions and 158 deletions
+12 -1
View File
@@ -53,7 +53,10 @@ async def _fetch_feed() -> Optional[str]:
resp.raise_for_status()
return resp.text
except Exception as exc:
logger.warning("Failed to fetch trumpstruth.org feed: %s", exc)
# Include type name — httpx often raises bare ConnectError/RemoteProtocolError
# with empty .args, which formats as just "Failed to fetch ..." with no body.
logger.warning("Failed to fetch trumpstruth.org feed: %s (%s)",
type(exc).__name__, exc)
return None
@@ -141,6 +144,14 @@ async def poll_trumpstruth(db_session_factory) -> None:
await manager.broadcast(_post_to_ws_payload(post))
logger.info("[trumpstruth] beat CNN — new post id=%d: %s",
post.id, post.text[:60])
# Telegram fan-out — matches truth_social.py. Without this,
# whichever poller wins the race determines whether users
# get pushed — flaky 50% delivery.
try:
from app.services.telegram import notify_signal
notify_signal(post)
except Exception as exc:
logger.warning("Telegram notify failed for post %d: %s", post.id, exc)
try:
from app.services.bot_engine import process_post
await process_post(post, db)
+40 -1
View File
@@ -63,7 +63,11 @@ async def _fetch_archive() -> Optional[list]:
resp.raise_for_status()
return resp.json()
except Exception as exc:
logger.error("Failed to fetch CNN archive: %s", exc)
# Include type name — httpx often raises bare ConnectError/TimeoutException
# with empty .args, which used to log as just "Failed to fetch CNN archive:"
# with no body, making outages impossible to diagnose.
logger.error("Failed to fetch CNN archive: %s (%s)",
type(exc).__name__, exc)
return None
@@ -80,6 +84,31 @@ async def _process_entry(entry: dict, db: AsyncSession) -> Optional[Post]:
published_at = _parse_dt(entry.get("created_at", ""))
# ── Deterministic entry pre-filter (saves AI spend + blocks 80% of noise) ──
# The 13-trade backtest showed AI confidence ≥ 85 still lets through pure
# rhetoric and second-derivative news. Hard-coded action-marker + future-
# tense + dedup check rejects those BEFORE the AI call. Failing posts are
# still saved to DB (so we have a record) but stamped as non-actionable.
from app.database import AsyncSessionLocal
from app.services.entry_filter import passes_entry_filter
filter_ok, filter_reason = await passes_entry_filter(text, AsyncSessionLocal)
if not filter_ok:
logger.info("Entry filter rejected post (id=%s): %s%s",
entry.get("id"), filter_reason, text[:80])
# Insert a stub row so we don't keep re-fetching the same post from
# the upstream archive. Signal=hold, no AI call, no analysis.
stub = Post(
external_id=external_id, text=text, source="truth",
published_at=published_at,
sentiment="neutral", ai_confidence=0,
relevant=False, signal="hold",
prefilter_reason=filter_reason[:32],
analysis_version="entry_filter_rejected",
)
db.add(stub)
await db.flush()
return None # don't broadcast, don't trade
analysis = await analyze_post(text)
asset = analysis["asset"]
@@ -154,6 +183,9 @@ def _post_to_ws_payload(post: Post) -> dict:
"ai_confidence": post.ai_confidence,
"ai_reasoning": post.ai_reasoning,
"relevant": post.relevant,
"target_asset": post.target_asset,
"category": post.category,
"expected_move_pct": post.expected_move_pct,
"price_impact": price_impact,
},
}
@@ -187,6 +219,13 @@ async def poll_truth_social(db_session_factory) -> None:
for post in new_posts:
await manager.broadcast(_post_to_ws_payload(post))
logger.info("Saved new post id=%d: %s", post.id, post.text[:60])
# Telegram fan-out (fire-and-forget). Only fires if
# signal is buy/short; noise posts are filtered inside.
try:
from app.services.telegram import notify_signal
notify_signal(post)
except Exception as exc:
logger.warning("Telegram notify failed for post %d: %s", post.id, exc)
try:
from app.services.bot_engine import process_post
await process_post(post, db)