Skip to content

[musashi-api] Archive analyzed tweets to Supabase#13

Open
TianyiRnj wants to merge 2 commits into
feat/archive-analyzed-tweets-to-supabasefrom
main
Open

[musashi-api] Archive analyzed tweets to Supabase#13
TianyiRnj wants to merge 2 commits into
feat/archive-analyzed-tweets-to-supabasefrom
main

Conversation

@TianyiRnj
Copy link
Copy Markdown
Contributor

Summary

  • Adds src/api/supabase-server-client.ts — singleton Supabase client (server-side, service role) with typed schema for the analyzed_tweets table
  • Adds src/api/analyzed-tweet-archive.ts — batched upsert of AnalyzedTweet records after each cron run, with per-batch error capture and an ArchiveResult summary
  • Updates api/cron/collect-tweets.ts to:
    • Buffer accepted tweets, write KV (hot path) first, then archive to Supabase
    • Delay advancing the account-rotation key until the archive result is known
    • Return 500 and skip rotation if SUPABASE_ARCHIVE_REQUIRED=true and any rows fail
    • Include archive stats in cron metadata response
  • Adds Supabase migration supabase/migrations/20260512000000_analyzed_tweets.sql with tweet_id as the upsert conflict key

New env vars

Variable Required Notes
SUPABASE_URL Yes Supabase project URL
SUPABASE_SERVICE_ROLE_KEY Yes Service role key — never expose client-side
SUPABASE_ARCHIVE_REQUIRED No Set true to make archive failure block cron rotation

Migration

Run supabase/migrations/20260512000000_analyzed_tweets.sql against the target project before deploying.

Test plan

  • pnpm typecheck passes
  • pnpm test passes (analyzed-tweet-archive, supabase-server-client, collect-tweets)
  • Deploy to preview → trigger cron → confirm rows in analyzed_tweets table
  • Cron metadata response includes archive field
  • With SUPABASE_ARCHIVE_REQUIRED=true: simulate upsert failure → cron returns 500, rotation does not advance

chechangyuan and others added 2 commits May 12, 2026 21:53
Production movers endpoint timed out 100% of the time for 8 days starting
5/4 16:00 UTC. Root cause: handler did 1,600 sequential kv.get() calls
inside detectMovers() (one per tracked market), plus 1,600 KV writes via
recordPriceSnapshots(), all in the request path. Once @vercel/kv's
Upstash migration added a small amount of per-request latency, total
runtime crossed the 25s function timeout and never recovered.

Changes:
- Extract snapshot/movers logic to api/lib/price-snapshots.ts.
  detectMoversBatch() uses kv.mget() in chunks of 100, replacing 1,600
  individual gets with ~16 batched calls. recordPriceSnapshots() now
  also reads via mget and caps each market's snapshot array at 300
  entries so values stop bloating until 7-day TTL.
- Add api/cron/refresh-markets.ts. Runs every 2 minutes, fetches markets,
  records snapshots, precomputes movers for buckets 0.02/0.05/0.1/0.2,
  and writes them to movers:precomputed:<bucket> with a 5-minute TTL.
  Writes freshness timestamps to meta:last_snapshot_run and
  meta:last_movers_run for /api/health to consume.
- Rewrite api/markets/movers.ts as a read-only endpoint: single kv.get()
  of the precomputed bucket, in-memory filter for limit/category, plus
  a 20s in-process response cache. Returns 503 if the cron hasn't run.
- Rewrite api/health.ts as a 4-check honest health probe: real market
  counts (Poly >= 800, Kalshi >= 200), KV read+write probe, snapshot
  freshness (< 5min), movers freshness (< 5min). Returns 503 on any
  degraded check so external monitors actually fire.
- Update vercel.json with the new cron entry and rewrite.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…health

Fix /api/markets/movers timeout and make /api/health honest
@vercel
Copy link
Copy Markdown

vercel Bot commented May 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
musashi Ready Ready Preview, Comment May 15, 2026 5:59pm

Request Review

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.

2 participants