Skip to content

feat(cli): Add sentry games leaderboard - #1437

Merged
sergical merged 5 commits into
feat/cli-init-snakefrom
feat/cli-games-leaderboard
Nov 4, 2026
Merged

sergical merged 5 commits into
feat/cli-init-snakefrom
feat/cli-games-leaderboard

Conversation

@sergical

@sergical sergical commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Adds sentry games leaderboard, which shows the top 10 Snake scores from the last 30 days. Stacked on #1432.

The leaderboard is a working example of Sentry application metrics: the CLI emits a metric and the Worker reads it back, with no database. apps/games-api/README.md describes the full data flow and lists which file to edit for each kind of change.

Scores (CLI)

  • A finished game sends one snake.score metric to the CLI telemetry project. Its only custom attribute is a random handle such as brave-otter-4242, stored in the local metadata table.
  • The metric goes out in a new trace with the user cleared on both scopes. beforeSendMetric keeps only handle, sentry.release, sentry.environment and sentry.sdk.* for this metric.
  • The handle format, metric name and score limit live in @sentry/toolkit-core/games, which the CLI and the Worker both import.
  • Nothing is sent when telemetry is off (SENTRY_CLI_NO_TELEMETRY, DO_NOT_TRACK, or the stored preference).
  • The score is flushed as soon as it is recorded. Metrics otherwise wait up to 5 seconds in the SDK buffer, and on macOS the CLI force-exits about 100 ms after a command ends.

Endpoint (new apps/games-api Worker)

  • A standalone Hono Worker, sentry-games-api, on https://games.sentry.new. The MCP server is not changed: the read token never shares a Worker with its OAuth secrets.
  • GET /v1/snake/leaderboard queries the tracemetrics dataset for max(value) per handle, with value:<=10000.
  • It rate-limits by hashed IP (30/min). The limiter fails open, like checkRateLimit in mcp-cloudflare. It caches the result with the Workers Cache API for 5 minutes.
  • It checks the upstream rows against the handle format and the score bounds. Every failure returns a generic error. The token, URL and upstream text never reach the response or Sentry.
  • Sentry.withSentry reports upstream and missing-token failures to sentry-developer-experience/games-api. The SDK does not collect the client IP, headers, cookies, bodies or query strings. A test checks the emitted envelope through the production wrapper.
  • The CLI validates each row again and drops rows that fail, so server text cannot write control sequences to the terminal. Network and HTTP failures exit with the API exit code (30). --json entries carry a you flag.

Impact on other commands: +600 bytes on the startup path of every command (the metric scrubber in telemetry.ts and the SENTRY_GAMES_API_URL registry entry). The rest loads only for sentry games.

Deployment. The Worker runs on the Sentry Internal Cloudflare account. The Deploy Games API workflow deploys it after a successful Test push run on main, the same gate as Deploy Docs MCP. Only the credential check and wrangler deploy steps receive the Cloudflare credentials. The MCP Workers use a different account, so the workflow has its own secrets. All of them are set:

  1. production environment: GAMES_API_CLOUDFLARE_API_TOKEN (Workers Scripts: Edit on Sentry Internal; Workers Routes: Edit and Zone: Read on sentry.new) and GAMES_API_CLOUDFLARE_ACCOUNT_ID. The token is a personal token for now; replace it with an account-owned token when a Sentry Internal Super Administrator can create one.
  2. On the Worker: SENTRY_GAMES_READ_TOKEN (a bot token with org:read only, on a user that is a member only of the CLI team) and SENTRY_DSN.

Known limit: the DSN is public, so anyone can submit a score. The handle format and the 10,000 cap block only impossible values.

Breaking Changes: None

Created with Claude Code

@sergical
sergical force-pushed the feat/cli-games-leaderboard branch 2 times, most recently from f775f5e to 49149a9 Compare October 7, 2026 15:25
@sergical
sergical marked this pull request as ready for review October 8, 2026 16:45
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Oct 8, 2026

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

Comment thread apps/games-api/src/leaderboard.ts Resolved
@linear-code

linear-code Bot commented Oct 12, 2026

Copy link
Copy Markdown

DEVEX-752

Base automatically changed from feat/cli-games to feat/cli-init-snake October 15, 2026 17:58
@github-actions github-actions Bot removed the risk: medium PR risk score: medium label Oct 15, 2026
@sergical
sergical force-pushed the feat/cli-init-snake branch from ba78133 to 2e16786 Compare October 15, 2026 18:18
@sergical
sergical force-pushed the feat/cli-games-leaderboard branch from dd48465 to e167a54 Compare October 15, 2026 18:22
@vercel

vercel Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
sentry-local Ready Ready Preview Nov 4, 2026 7:05pm UTC

Request Review

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Oct 15, 2026
@sergical
sergical force-pushed the feat/cli-games-leaderboard branch from e167a54 to 73367ce Compare October 15, 2026 18:37

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 73367ce. Configure here.

@sergical
sergical force-pushed the feat/cli-init-snake branch from 32293fa to d1efe47 Compare November 4, 2026 03:05
@sergical
sergical force-pushed the feat/cli-games-leaderboard branch from 038a46a to 5672812 Compare November 4, 2026 03:05
@sergical sergical changed the title feat(cli): Add an anonymous Snake leaderboard feat(cli): Add sentry games leaderboard Nov 4, 2026
@sergical
sergical force-pushed the feat/cli-games-leaderboard branch from 5672812 to 8101f2c Compare November 4, 2026 03:28
@sergical
sergical force-pushed the feat/cli-games-leaderboard branch from 8101f2c to 7b6e3a5 Compare November 4, 2026 03:45
@sergical
sergical force-pushed the feat/cli-games-leaderboard branch from 7b6e3a5 to cb0ec05 Compare November 4, 2026 04:21
@sergical
sergical force-pushed the feat/cli-games-leaderboard branch from cb0ec05 to 73fe47c Compare November 4, 2026 04:31
sergical and others added 3 commits November 4, 2026 10:24
Report each finished Snake run as an anonymous `snake.score` metric, tagged
only with a random local player handle, and show the top scores of the last
30 days with `sentry games leaderboard`.

Scores are sent only when telemetry is on, in their own trace with the user
cleared, and `beforeSendMetric` keeps only allowlisted attributes.

The new `@sentry/games-api` Worker reads the metric from Sentry with a
read-only token and serves the top 10, with a 5 minute cache and a per-IP
rate limit. Its README describes the whole data flow and where to change it.

Co-authored-by: Claude <[email protected]>
The score flush ran unawaited, so on macOS the force-exit timer could stop
the upload when the player quit right after game over. The command now
waits for pending scores, bounded by the 3 s flush timeout, after it
restores the terminal.

Co-authored-by: Claude <[email protected]>
@sergical
sergical force-pushed the feat/cli-init-snake branch from 159de2b to bec0f47 Compare November 4, 2026 15:28
@sergical
sergical force-pushed the feat/cli-games-leaderboard branch from 48c9e53 to d1a3a7f Compare November 4, 2026 15:28

@sentry-junior sentry-junior Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The privacy design (separate trace, cleared user, allowlist scrubber, re-validation on both sides) holds up. Most of what's below is about matching patterns the repo already has.

  • Shared contract is copied, not shared. The handle regex, snake.score, and MAX_SNAKE_SCORE are defined in lib/games/player.ts/score.ts, again in games-api/src/leaderboard.ts behind a "Must match the CLI" comment, and snake.score is hard-coded a third time in scrubAnonymousMetric in telemetry.ts. The README's "they must match" row admits the drift risk. The repo already handles this case: contracts shared by the CLI and a Worker live in a @sentry/toolkit-core subpath export (docs-mcp imports @sentry/toolkit-core/mcp-limits so it applies the same limits as the main MCP server). Add a small @sentry/toolkit-core/games module with those constants, import it from player.ts, score.ts, telemetry.ts, and the Worker, and drop the comment and README row. Both directions are allowed by the dependency rule in AGENTS.md, and it also avoids the telemetry.ts → score.ts import cycle that probably caused the literal.

  • games leaderboard doesn't follow packages/cli/AGENTS.md.

    • formatLeaderboardHuman calls getPlayerHandle() to add (you), so --json users can't tell which row is theirs. The "Declaring Command Output" rules say output.human must get the same data that's serialized, with no separate data path. Put the handle (or a per-entry you flag) into the yielded data.
    • The HTTP client and its schemas sit in the command file. Under "Command File Structure", that goes in src/lib, like lib/api/status-page.ts and lib/docs-service.ts do for their non-Sentry services.
    • Network and HTTP failures throw new CliError(msg), which exits 1. The error hierarchy reserves 3x exit codes for API/network failures, and docs-service.ts uses ApiError / CliError(…, EXIT.API) for the same kind of failure.
    • Building a Writer buffer around writeTable reimplements formatTable. Return formatTable(...) instead, as docs/list.ts and explore.ts do.
  • games-api diverges from the repo's two existing Workers.

    • Both docs-mcp and mcp-cloudflare export Sentry.withSentry(...). This Worker only uses console.warn, so an expired or revoked read token shows up as silent 502s that nobody gets alerted about.
    • A standalone private Worker has a home and an ownership model in this repo: apps/docs-mcp is listed in docs/README.md, has a "Deployment ownership" section, and is deployed by a gated workflow. docs/operations/github-actions.md says deployments are the case where a new workflow fits and that manual deploys of new revisions aren't the path. This package is under packages/, isn't in the root AGENTS.md structure or the docs index, hard-codes account_id (the others get CLOUDFLARE_ACCOUNT_ID from CI), and is deployed with pnpm run deploy from a laptop. At minimum, list it in the docs index and AGENTS.md and state who deploys it. Ideally, move it to apps/ and give it a deploy job like deploy-docs-mcp.yml.
    • Smaller point: the rate-limit block is copied from routes/search.ts instead of the checkRateLimit shape in mcp-cloudflare/src/server/utils/rate-limiter.ts, and it fails closed (503) where that helper fails open. If failing closed is intended, add a one-line note.

Share the handle format, metric name, and score limit through
@sentry/toolkit-core/games, so the CLI and the Worker cannot drift.

In the CLI, move the leaderboard client to lib/games, exit with the API
code on network and HTTP failures, render with formatTable, and put a
`you` flag on each entry so JSON and human output carry the same data.

Move the Worker to apps/games-api. Wrap it in Sentry.withSentry so
upstream and token failures are reported, make the rate limiter fail
open like mcp-cloudflare, read the account from CLOUDFLARE_ACCOUNT_ID,
and deploy it from a gated workflow after the main Test run. Document
its ownership in its README, the docs index, and AGENTS.md.

Co-authored-by: Claude <[email protected]>
@sergical

sergical commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

Addressed in 611f348:

  • Shared contract. New @sentry/toolkit-core/games export holds the handle regex, snake.score, the handle attribute name and MAX_SNAKE_SCORE. player.ts, score.ts, telemetry.ts and the Worker import it. The "Must match the CLI" comment and the README row are gone.
  • CLI rules.
    • The HTTP client and schemas moved to lib/games/leaderboard-api.ts. It stays in lib/games so it loads only with the games chunk.
    • Network failures throw CliError(…, EXIT.API). HTTP and invalid-body failures throw ApiError.
    • Each entry in the yielded data has a you flag. formatLeaderboardHuman reads only that flag and no longer calls getPlayerHandle().
    • The table uses formatTable.
  • withSentry. The Worker is wrapped with Sentry.withSentry, with the DSN from env.SENTRY_DSN. Upstream failures and a missing token are reported. The upstream error carries only the status, and request headers are not sent because they include CF-Connecting-IP.
  • Ownership.
    • The Worker moved to apps/games-api. It is in the root AGENTS.md structure and in docs/README.md, and it has a "Deployment ownership" section (owner: the CLI team).
    • account_id is gone from wrangler.jsonc.
    • deploy-games-api.yml deploys it after a successful Test push on main, with the same revision checks as deploy-docs-mcp.yml. It also smoke-tests the endpoint.
    • The Worker runs on a different Cloudflare account from the MCP Workers, so the workflow reads GAMES_API_CLOUDFLARE_API_TOKEN and GAMES_API_CLOUDFLARE_ACCOUNT_ID.
  • Rate limiter. Same shape as checkRateLimit: a hashed key, and it fails open. A limiter error is reported to Sentry. A test covers it.

Created with Claude Code

Turn off collection of the client IP, headers, cookies, bodies and query
strings through the SDK's dataCollection option, so spans and events
both omit them. A new test sends a request through the production
wrapper and checks the emitted envelope.

Give the Cloudflare credentials only to the deploy workflow steps that
use them, so dependency install scripts cannot read them.

Co-authored-by: Claude <[email protected]>

@betegon betegon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 👌 🙆‍♂️

@sergical
sergical merged commit 42831ed into feat/cli-init-snake Nov 4, 2026
33 checks passed
@sergical
sergical deleted the feat/cli-games-leaderboard branch November 4, 2026 21:24

This branch was successfully deployed

1 active deployment
Preview — 2821dd92 Deployed Nov 4, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants