Skip to content

feat(cli): Add sentry games snake - #1432

Merged
sergical merged 8 commits into
mainfrom
feat/cli-init-snake
Nov 4, 2026
Merged

sergical merged 8 commits into
mainfrom
feat/cli-init-snake

Conversation

@sergical

@sergical sergical commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Summary

Adds sentry games snake, a full-screen Snake game in the terminal. It is a standalone command. It has no link to sentry init, which will be deprecated.

  • src/lib/games/snake-game.ts is the pure engine: state transitions and a half-block renderer.
  • src/lib/games/snake-terminal.ts drives the terminal directly: alternate screen, raw-mode keys, and one redraw per state change. It loads no React, Ink, or Yoga.
  • Arrow keys, wasd, or hjkl steer. p or space pauses, r restarts, and esc, q, or ctrl+c quits. A resize pauses the run and keeps it.
  • games is an easter egg: a hidden route, so help, help --json, completions, the docs site, and the agent skill leave it out.
  • The command needs an interactive terminal. It is not available when an AI agent runs the CLI.
  • games is a lazy route in the command manifest, so other commands do not load the game.

This PR replaces the earlier version that put the game on the sentry init waiting screen. Rebased on main.

Impact on other commands

Measured on the npm bundle, as bytes of the chunks each command compiles, against main, before the route was hidden. Hiding it can only make these numbers smaller:

Command Change
--version, help, issue list, org list +110 bytes (route manifest entry and brief)
help --json +2.8 KB (full command tree)
All dist JS +22.8 KB, of which the lazy runner chunk is 6.5 KB

Breaking Changes

  • None

Created with Claude Code

@sergical
sergical marked this pull request as ready for review October 6, 2026 22:28
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Oct 6, 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.

@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.

Before merging:

  • Stabilize the pane sizing in ink-app.tsx / ink-snake.tsx. At 80×20, pressing g then ↑ changes the measured board width and pauses the game before its first tick, without a terminal resize. Keep the score/help visible and add a regression covering narrow and wide terminals.
  • Respect NO_COLOR in the renderer while keeping the cell types distinguishable.
  • Add the required JSDoc for the new engine exports and type fields.

The engine/UI split and shared shortcuts fit the repo. Property/model-based tests would suit the engine invariants, but that preference is not a separate merge blocker.

@sergical

sergical commented Oct 8, 2026

Copy link
Copy Markdown
Member Author

@betegon Thanks for the review. Fixed in 0ea3fc8:

  • Pane sizing: the left pane's width came from its content: the board plus the "Press an arrow key to start" hint. When ↑ cleared the hint, the pane width changed, resizeSnake ran, and the game paused before its first tick. While the game shows, the pane now has flexBasis={0}, so its content no longer sets its width. The status hint now has its own one-line row under the score, cut to fit, so the score and best score always stay visible. New regression tests at 80×20 and 160×50 check that the board border stays the same after the first ↑ and 400 ms later, that the score and footer stay visible, and that "Paused" never appears.
  • NO_COLOR: renderSnake takes a color flag. ink-snake.tsx passes !isPlainOutput(), which covers NO_COLOR and SENTRY_PLAIN_OUTPUT. Without color, the board uses glyphs: @ head, o body, * food, space for empty cells. Tests cover the renderer and the Ink component.
  • JSDoc: added to every exported engine symbol and type field in snake-game.ts, and to the exported types and components in ink-snake.tsx.
  • Property tests: two fast-check tests in snake-game.test.ts. One checks that the snake stays on the board and never overlaps itself. The other checks that food never lands on the snake and that the body length is always 3 + score.

@sergical
sergical requested a review from betegon October 8, 2026 18:29
@linear-code

linear-code Bot commented Oct 12, 2026

Copy link
Copy Markdown

DEVEX-752

sergical added a commit that referenced this pull request Oct 15, 2026
Adds `sentry games snake`, so the Snake game from the `sentry init`
waiting screen can be played on its own. Stacked on #1432.

- The command runs full-screen in the alternate screen. esc, q or ctrl+c
quits and restores the terminal.
- It errors when there is no TTY or when an AI agent runs the CLI.
`SENTRY_INIT_GAME` does not apply here.
- `loadInkSidecar()` is extracted from `createInkUI` with no behavior
change, so the init wizard and the game load the sidecar the same way.
- Generated docs and skill references include the new `games` route.

Breaking Changes: None

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Claude <[email protected]>
Co-authored-by: betegon <[email protected]>
@vercel

vercel Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

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

1 Skipped Deployment
Project Deployment Actions Updated
sentry-local Skipped Skipped Nov 4, 2026 9:36pm UTC

Request Review

sergical added a commit that referenced this pull request Oct 15, 2026
Adds `sentry games snake`, so the Snake game from the `sentry init`
waiting screen can be played on its own. Stacked on #1432.

- The command runs full-screen in the alternate screen. esc, q or ctrl+c
quits and restores the terminal.
- It errors when there is no TTY or when an AI agent runs the CLI.
`SENTRY_INIT_GAME` does not apply here.
- `loadInkSidecar()` is extracted from `createInkUI` with no behavior
change, so the init wizard and the game load the sidecar the same way.
- Generated docs and skill references include the new `games` route.

Breaking Changes: None

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Claude <[email protected]>
Co-authored-by: betegon <[email protected]>
@sergical
sergical force-pushed the feat/cli-init-snake branch from ba78133 to 2e16786 Compare October 15, 2026 18:18

@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.

@sergical
sergical force-pushed the feat/cli-init-snake branch from 32293fa to d1efe47 Compare November 4, 2026 03:05
@sergical sergical changed the title feat(cli): Add optional Snake game to the init waiting screen feat(cli): Add sentry games snake Nov 4, 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.

@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 packages/cli/script/generate-skill.ts Outdated
Comment on lines +96 to +97
/** Routes that stay in `sentry help` but not in the skill, because agents cannot use them. */
const HUMAN_ONLY_ROUTES = new Set(["games"]);

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.

Shall we make this an easter egg, hidden from the help too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

great idea

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in bec0f47. games is now a hidden route, so help, help --json, completions, the docs site, and the skill leave it out. The skill filter is gone.

@sergical
sergical changed the base branch from main to perf/cli-load-budget November 4, 2026 15:07
@sergical

sergical commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

This PR now sits on #1945, which sets a CI budget for the JavaScript each command reads. Size cost of games (#1432 and #1437 together), against that baseline:

Before With games
sentry --version 1,568 KB 1,568 KB
sentry auth status 1,661 KB 1,662 KB
sentry help --json 4,021 KB 4,028 KB
All package JavaScript 17,768 KB 17,805 KB

The games code loads only for sentry games. All budgets pass.

@BYK

BYK commented Nov 4, 2026

Copy link
Copy Markdown
Member

This PR now sits on #1945, which sets a CI budget for the JavaScript each command reads.

Please no, not yet.

sergical and others added 6 commits November 4, 2026 10:20
Play Snake full-screen in the terminal with `sentry games snake`.

The game is standalone: it no longer runs inside the `sentry init` waiting
screen and does not load Ink or the init sidecar. A small runner
(`src/lib/games/snake-terminal.ts`) draws straight to the terminal with
the alternate screen and raw-mode keys, on top of the pure engine in
`src/lib/games/snake-game.ts`. The command imports the runner with
`import()`, so other commands and the full route tree load only the
route stub.

`completions.property.test.ts` now accepts a command without visible
flags: the completion targets leave `flagNames` out for those, and
`games snake` is the first one.

Co-authored-by: Claude <[email protected]>
Agents cannot play, so the skill does not list the games route. It stays
in sentry help.

Co-authored-by: Claude <[email protected]>
A terminal can deliver one arrow key over two reads, and Alt plus a key
arrives as escape and the key. Both quit the game. Keep an unfinished
sequence until the next read, drop Alt plus a key, and read a lone escape
as Esc only when nothing follows within 100 ms.

Co-authored-by: Claude <[email protected]>
A lone Esc waits for the rest of a split key sequence. When the next read
did not continue it, the Esc and the next key were dropped as Alt+key, and
leftover bytes of an arrow key could turn the snake. Now the pending Esc
quits and the next read is parsed on its own. An Esc followed by another
escape in one read is also the Esc key.

Co-authored-by: Claude <[email protected]>
Make `sentry games` an easter egg. The route is now hidden, so help,
help --json, completions, the docs site, and the agent skill leave it
out, and the skill no longer needs its own filter for it.

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 changed the base branch from perf/cli-load-budget to main November 4, 2026 15:28
@sergical

sergical commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

Unstacked: this PR is on main again and does not include #1945.

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

---------

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

@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.

const SENTRY_HOST = "https://us.sentry.io";
const SENTRY_ORG_SLUG = "sentry";
const SNAKE_PROJECT_ID = "4510776311808000";
const SCORE_FIELD = `max(value,${SNAKE_SCORE_METRIC},distribution,-)`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Leaderboard aggregate uses a unit that does not match emitted scores

Snake scores are emitted without a unit, whose documented tracemetrics unit is none, but the leaderboard requests distribution,-. The query therefore does not read the emitted score series, leaving the leaderboard empty or unavailable.

Evidence
  • reportSnakeScore() emits snake.score without a unit; the Explore/dashboard docs say an unspecified metric unit is none and that the query unit must match SDK emission.
  • buildUpstreamUrl() uses distribution,- for both the score field and sort, and fetchLeaderboard() reads scores using that same field key.
  • The leaderboard test fabricates upstream rows keyed with - and checks the generated URL, so it does not verify compatibility with emitted metrics; the deploy smoke test checks only HTTP 200.

Identified by Warden · code-review · EK9-T42

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No change needed. In a trace metric aggregate, - means "any unit" and adds no unit filter. It is not a different unit:

  • extract_trace_metric_aggregate_arguments in src/sentry/search/eap/columns.py turns a - unit argument into metric_unit=None.
  • TraceMetric.get_filter in src/sentry/search/eap/trace_metrics/types.py adds a unit filter only when the unit is not None and not ANY_UNIT ("-").

So max(value,snake.score,distribution,-) matches the scores that the CLI sends without a unit. none would also match them, but - keeps the query correct if the CLI adds a unit later.

When the terminal is smaller than the smallest board, show a short
message instead of the board and ignore keys other than quit. Clip the
score line to the board width so it does not wrap.

Co-authored-by: Claude <[email protected]>
@sergical
sergical enabled auto-merge (squash) November 4, 2026 21:40

@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.

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 a8a7ca5. Configure here.

fits()
? renderSnakeFrame(state, best, color)
: `\x1b[H${TOO_SMALL_MESSAGE.slice(0, columns())}\x1b[K\x1b[J`,
);

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.

Game runs while terminal is too small

Low Severity

Shrinking below the smallest frame does not stop a playing run when snakeBoardSize still returns the clamped 10×6 board. resizeSnake then leaves status as playing, so schedule keeps stepping while draw hides the board and keys are ignored. The snake can hit a wall and fire onGameOver off-screen.

Additional Locations (2)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit a8a7ca5. Configure here.

@sergical
sergical merged commit 0aa23db into main Nov 4, 2026
43 checks passed
@sergical
sergical deleted the feat/cli-init-snake branch November 4, 2026 21:49

This branch was previously deployed

1 inactive deployment
Preview — a8a7ca50 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.

3 participants