Skip to content

Conversation

@phacops
Copy link
Contributor

@phacops phacops commented Jan 25, 2026

Summary

  • Add terminal-style shell interfaces for ClickHouse queries in snuba-admin
  • Tracing Shell: Terminal interface for tracing queries (after ClickHouse Tracing in nav)
  • System Shell: Terminal interface for system queries (after System Queries in nav)
  • Both shells share the same component with mode-specific features
  • Shell pages inherit permissions from their parent pages (tracing/system-queries)
  • GitHub-inspired dark theme with polished visual design

Screenshots

Screenshot 2026-01-25 at 12 49 56 PM

Features

Commands

Tracing Shell

  • USE <storage> - Select storage
  • SHOW STORAGES - List available storages
  • PROFILE ON/OFF - Toggle profile event collection
  • TRACE RAW/FORMATTED - Toggle trace output format
  • HELP - Show available commands
  • CLEAR - Clear terminal output

System Shell

  • USE <storage> - Select storage
  • HOST <host:port> - Select target host
  • SHOW STORAGES / SHOW HOSTS - List available options
  • SUDO ON/OFF - Toggle sudo mode (red border warning when enabled)
  • HELP - Show available commands
  • CLEAR - Clear terminal output

Extensible Command Parser

  • Registry-based command definitions in command_parser.ts
  • Each command defined with: pattern, parser function, and mode restrictions
  • Easy to add new commands by adding entries to the COMMANDS array
  • Commands automatically filtered by shell mode (tracing/system)

SQL Syntax Highlighting

  • CodeMirror-based input with SQL language support
  • GitHub Dark theme colors matching the shell aesthetic:
    • Keywords (SELECT, FROM, WHERE, etc.): red/coral (#ff7b72)
    • Strings: light blue (#a5d6ff)
    • Numbers: cyan (#79c0ff)
    • Comments: gray italic (#8b949e)
    • Functions: purple (#d2a8ff)
    • Special values (NULL, TRUE, FALSE): cyan bold (#79c0ff)

Compact Collapsible Results

  • Results and trace output displayed in collapsible sections
  • Click section header to expand/collapse
  • Results section expanded by default, trace sections collapsed
  • Badge showing row count, node count, or line count
  • Compact table styling with smaller fonts and tight padding
  • Long cell values truncated with ellipsis
  • NULL values displayed in gray italic

Multi-line Input

  • Input box supports multi-line SQL queries
  • Auto-resizes based on content (up to 10 lines)
  • Queries are normalized before sending to API (newlines collapsed to spaces)

Keyboard Shortcuts (clickhouse-client style)

Shortcut Action
Enter Execute command
Shift+Enter Insert new line
Tab Autocomplete storage/host name
/ Navigate command history
Ctrl+C Clear current input
Ctrl+L Clear screen
Ctrl+U Delete from cursor to start of line
Ctrl+K Delete from cursor to end of line
Ctrl+W Delete word before cursor
Ctrl+A Move cursor to start of line
Ctrl+E Move cursor to end of line

Autocomplete

  • Press Tab after USE to autocomplete storage names
  • Press Tab after HOST to autocomplete host names (system shell)
  • Shows suggestions bar above input when multiple matches exist
  • Click suggestions to select directly

Virtualized Scrollback

  • Uses @tanstack/react-virtual for windowed virtualization of history entries
  • Only visible entries plus 5 overscan items are rendered to the DOM
  • Dynamic height measurement for accurate positioning of variable-height entries
  • Smart auto-scroll: follows new entries only when user is at bottom
  • Maximum history limit of 500 entries (~250 command/response pairs) to prevent unbounded growth

Scrollable Navigation Sidebar

  • Navigation sidebar now scrolls when there are more items than fit in the viewport
  • Prevents layout issues when many nav items are present

Visual Design

  • GitHub-inspired dark terminal aesthetic
  • Gradient backgrounds (#0d1117#161b22)
  • Custom scrollbars matching theme
  • Styled result tables with hover effects
  • Error messages with red accent border (stack traces stripped for cleaner display)
  • Animated executing indicator
  • Sudo mode: red gradient background with glowing border

Error Handling

  • Properly handles API error objects with {code, message, type} structure
  • Strips ClickHouse stack traces from error messages for cleaner display
  • Defensive checks for null/undefined results and trace data
  • Graceful handling of malformed or missing data

Other Features

  • Command history persisted to localStorage (per shell mode)
  • Full-height shell using all available screen space
  • Query results displayed in formatted tables (first 100 rows)
  • NULL values displayed explicitly
  • Trace output with formatted summaries or raw mode
  • Profile events display
  • Status bar showing current storage, host, and settings

Test plan

  • Run snuba-admin locally with snuba admin --debug
  • Verify "💻 Tracing Shell" appears after ClickHouse Tracing
  • Verify "💻 System Shell" appears after System Queries
  • Test shell commands: HELP, USE, SHOW STORAGES
  • Test Tab autocomplete for storage names
  • Test keyboard shortcuts (Ctrl+C, Ctrl+L, Ctrl+U, Ctrl+W, etc.)
  • Test SQL query execution in both shells
  • Test multi-line input with Shift+Enter
  • Verify SQL syntax highlighting (keywords, strings, numbers, comments)
  • Test collapsible results sections (click to expand/collapse)
  • Test error handling (invalid queries should show clean error messages)
  • Test command history navigation with Up/Down arrows
  • Verify shell pages respect same permissions as parent pages
  • Test sudo mode visual warning in system shell
  • Execute 20+ queries to build history, verify smooth scrolling
  • Scroll up, execute query, verify scroll position is preserved
  • Verify navigation sidebar scrolls when window is short

🤖 Generated with Claude Code

@phacops phacops requested a review from a team as a code owner January 25, 2026 20:20
@phacops phacops marked this pull request as draft January 25, 2026 20:23
Add a new SQL Shell page to snuba-admin that provides a terminal-style
interface for executing ClickHouse queries with tracing. Features include:

- Dark terminal aesthetic with monospace font
- Shell commands: USE, SHOW STORAGES, PROFILE ON/OFF, TRACE RAW/FORMATTED
- SQL query execution with full tracing support
- Command history with up/down arrow navigation (persisted to localStorage)
- Keyboard shortcuts: Enter to execute, Ctrl+L to clear
- Query results displayed in table format with trace summaries
- Prominent link in nav bar for easy access

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@phacops phacops force-pushed the feat/sql-shell-terminal branch from 345f804 to b3b15da Compare January 25, 2026 20:25
phacops and others added 12 commits January 25, 2026 12:31
Extend the SQL Shell component to support both tracing and system query modes:

- Add mode prop to SQLShell component (tracing/system)
- System mode adds: HOST command, SHOW HOSTS, SUDO ON/OFF
- System mode requires both storage and host selection
- Sudo mode shows red border warning
- Add SystemShellPage as new nav entry after System Queries
- Separate command history per mode

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Remove title display from Body component
- Add visual highlight for active nav item (blue left border + background)
- Adjust shell height to use full available space

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- tracing-shell uses same permission as tracing
- system-shell uses same permission as system-queries

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- GitHub-inspired dark theme with gradient background
- Better color palette (blues, greens, oranges)
- Improved typography with better spacing and line height
- Custom scrollbar styling
- Better result boxes with rounded corners
- Styled error messages with left border accent
- Hover effects on storage list and table rows
- Animated executing indicator with pulsing dot
- Better sudo mode warning appearance
- Overall more polished and modern look

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Decrease body margin from 20px to 10px
- Reduce shell internal padding (output, input, status areas)
- Increase shell height to use reclaimed space

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Tab completes storage names after "USE " command
- Tab completes host names after "HOST " command (system mode)
- Shows matching options when multiple completions exist
- Completes to common prefix when possible
- Updated status bar hint to show Tab shortcut

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Display matching suggestions in a bar above the input
- Clickable suggestions to select directly
- Clear suggestions when typing continues
- Styled with subtle blue highlight

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Handle error objects returned from API (not just Error instances)
- Extract error.error, error.message, or stringify the object
- Ensures error messages are displayed in the shell

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Adds keyboard shortcuts commonly found in clickhouse-client and other SQL shells:
- Ctrl+C: Clear current input
- Ctrl+L: Clear screen
- Ctrl+U: Delete from cursor to start of line
- Ctrl+K: Delete from cursor to end of line
- Ctrl+W: Delete word before cursor
- Ctrl+A: Move cursor to start of line
- Ctrl+E: Move cursor to end of line
- Tab: Autocomplete
- Up/Down: Navigate command history

Also updated HELP output to document all shortcuts.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@phacops phacops marked this pull request as ready for review January 25, 2026 21:07
phacops and others added 10 commits January 25, 2026 13:16
- Check for null/undefined result objects
- Handle null/undefined cells (display as "NULL")
- Handle undefined rows gracefully
- Handle undefined column definitions

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Check for null/undefined summary and query_summaries in FormattedTraceOutput
- Check for null node in NodeSummary
- Check for null/undefined traceOutput in RawTraceOutput
- Show "No trace data available" message instead of crashing

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The API returns errors as objects with {code, message, type} structure.
ErrorOutput was expecting a string and trying to render the object directly,
causing React to crash with "Objects are not valid as a React child".

- Update ErrorOutput to handle string, object, and any error types
- Extract message from error.message or error.error fields
- Fall back to JSON.stringify for unknown error shapes
- Update ShellHistoryEntry type to accept object errors

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Only show the error message, not the full stack trace.
ClickHouse errors contain "Stack trace:" followed by the trace -
now we strip everything after that marker for cleaner error display.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Implement windowed virtualization using @tanstack/react-virtual to
optimize rendering of shell history entries. Previously all history
was rendered directly to the DOM, which could slow down the page as
history grew large.

Changes:
- Add @tanstack/react-virtual as direct dependency
- Refactor ShellOutput to use virtualization with dynamic height
  measurement and 5-item overscan for smooth scrolling
- Add smart auto-scroll that only follows new entries when user is
  at bottom of the output
- Add MAX_HISTORY_ENTRIES limit (500) to prevent unbounded growth
- Move scroll container management into ShellOutput component

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
Add overflow-y: auto to the navigation sidebar so it can scroll when
there are more nav items than fit in the viewport. Also update the
parent flex container with minHeight: 0 and overflow: hidden to
properly enable flex child scrolling.

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
- Replace single-line input with textarea for multi-line SQL queries
- Enter executes the query, Shift+Enter adds a new line
- Textarea auto-resizes based on content (up to 10 lines)
- Normalize queries before API calls (collapse newlines to spaces)
- Update styles for proper textarea alignment and scrolling

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Create ShellInput component using CodeMirror with SQL language support
- GitHub Dark theme colors for syntax highlighting:
  - Keywords (SELECT, FROM, WHERE): red/coral
  - Strings: light blue
  - Numbers: cyan
  - Comments: gray italic
  - Functions: purple
  - Special values (NULL, TRUE, FALSE): cyan bold
- Refactor shell.tsx to use new ShellInput component
- Use Compartments for dynamic placeholder/disabled state updates

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Add CollapsibleSection component for expandable result areas
- Results section expanded by default, trace sections collapsed
- Compact table styling with smaller fonts and padding
- NULL values displayed in gray italic
- Truncated cell content with ellipsis for long values
- Scrollable raw trace output with max height
- Badge showing row/node/line counts in section headers

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Create command_parser.ts with registry-based command definitions
- Each command defined with pattern, parser function, and mode restrictions
- Easy to add new commands by adding entries to COMMANDS array
- Export getAvailableCommands() for potential autocomplete/help generation
- Remove inline parseCommand from shell.tsx

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
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