CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project Overview
Scarab is a high-performance, split-process terminal emulator built in Rust. It features:
- Split Architecture: Daemon (headless server) + Client (Bevy-based GUI)
- Zero-Copy IPC: Shared memory for bulk data transfer between processes
- Hybrid Plugin System: Fusabi-lang (F# dialect) with dual runtimes
- GPU-Accelerated Rendering: Bevy game engine with cosmic-text
Workspace Structure
This is a Cargo workspace with 5 crates:
scarab/
├── crates/
│ ├── scarab-daemon/ # Headless server, owns PTY processes
│ ├── scarab-client/ # Bevy GUI, renders via shared memory
│ ├── scarab-protocol/ # IPC definitions, shared memory layout (#[repr(C)])
│ ├── scarab-plugin-api/ # Shared plugin traits
│ └── scarab-config/ # Configuration management
External Dependencies:
fusabi-vm- Official Fusabi VM runtime for .fzb bytecode (from https://github.com/fusabi-lang/fusabi)fusabi-frontend- Official Fusabi compiler/parser for .fsx scripts (from https://github.com/fusabi-lang/fusabi)
Build Commands
Build the entire workspace:
cargo buildBuild with optimizations:
cargo build --releaseBuild specific crate:
cargo build -p scarab-daemon
cargo build -p scarab-clientRun daemon (headless server):
cargo run -p scarab-daemonRun client (in separate terminal):
cargo run -p scarab-clientCheck all crates:
cargo check --workspaceRun tests:
cargo test --workspaceArchitecture Constraints
scarab-protocol (IPC Layer)
- CRITICAL: All shared memory structs MUST be
#[repr(C)] - CRITICAL: Must be
#![no_std]compatible for memory layout guarantees - Uses
bytemuck::{Pod, Zeroable}for safe zero-copy transmutation - Shared memory path:
/scarab_shm_v1 - Default grid: 200x100 cells
scarab-daemon (Server)
- Owns PTY processes via
portable-pty - Maintains terminal grid state (source of truth)
- Writes to shared memory ring buffer
- Uses
alacritty_terminalfor VTE parsing - Runs compiled
.fzbplugins (Fusabi VM) - Persistence: Survives client crashes/disconnects
scarab-client (Bevy GUI)
- Reads from shared memory (zero-copy)
- Renders text using
cosmic-textwith texture atlas caching - Uses
AtomicU64sequence numbers for lock-free synchronization - Runs interpreted
.fsxscripts (hot-reloadable UI) - Sends input to daemon via Unix Domain Sockets
Synchronization Strategy
- Lock-free: Use
AtomicU64sequence numbers in SharedState - Never block the Bevy render thread - client reads are non-blocking
- Daemon increments sequence_number after each grid update
- Client polls sequence_number to detect changes
Plugin System (Fusabi)
Scarab uses the official Fusabi scripting language (https://github.com/fusabi-lang/fusabi) - a high-performance F# dialect for Rust.
Two runtimes for different use cases:
| Runtime | File Type | Location | Crate | Purpose |
|---|---|---|---|---|
| Fusabi VM | .fzb | Daemon | fusabi-vm | Compiled bytecode for high-perf hooks, output scanning, mux logic |
| Fusabi Frontend | .fsx | Client | fusabi-frontend | F# source parser/compiler for hot-reloadable UI scripts |
Hot Reloading: .fsx files can be recompiled on-the-fly without Rust recompilation
Key Dependencies
- Fusabi - Official F# scripting language for Rust
fusabi-vm- Bytecode VM runtimefusabi-frontend- Parser, type checker, compiler
bevy0.15 - Game engine (minimal features: winit, ui, render, x11, wayland)portable-pty0.8 - Cross-platform PTY handlingalacritty_terminal0.24 - VTE parsercosmic-text0.11 - Text renderingshared_memory0.12 - IPC shared memorybytemuck1.14 - Safe zero-copy castingtokio1.36 - Async runtime
Development Roadmap
Current phase: Scaffolding (IPC and PTY bridging)
Next phases:
- Implement Shared Memory IPC Bridge
- Integrate
cosmic-textrendering in Bevy - Integrate Fusabi VM for daemon plugins
- Integrate Fusabi Frontend for client UI scripts
Critical TODOs in Code
scarab-daemon/src/main.rs:
- Initialize Shared Memory writer
- Feed PTY output to Alacritty VTE parser
- Update SharedState grid from parsed terminal commands
- Increment sequence_number atomically
scarab-client/src/main.rs:
- Initialize Shared Memory reader
- Check SharedState sequence_number in sync_grid()
- Update texture/mesh when sequence changes
- Send input to daemon via socket
scarab-protocol/src/lib.rs:
- Fix incomplete
cellsfield definition in SharedState (line 33) - Fix incomplete ControlMessage enum attribute (line 37)
Release Profile
Optimized for performance:
- LTO: thin
- Codegen units: 1
- Opt level: 3