AGENTS.md

  1# Crush Development Guide
  2
  3## Project Overview
  4
  5Crush is a terminal-based AI coding assistant built in Go by
  6[Charm](https://charm.land). It connects to LLMs and gives them tools to read,
  7write, and execute code. It supports multiple providers (Anthropic, OpenAI,
  8Gemini, Bedrock, Copilot, Hyper, MiniMax, Vercel, and more), integrates with
  9LSPs for code intelligence, and supports extensibility via MCP servers and
 10agent skills.
 11
 12The module path is `github.com/charmbracelet/crush`.
 13
 14## Architecture
 15
 16```
 17main.go                            CLI entry point (cobra via internal/cmd)
 18internal/
 19  app/app.go                       Top-level wiring: DB, config, agents, LSP, MCP, events
 20  cmd/                             CLI commands (root, run, login, models, stats, sessions)
 21  config/
 22    config.go                      Config struct, context file paths, agent definitions
 23    load.go                        crush.json loading and validation
 24    provider.go                    Provider configuration and model resolution
 25  agent/
 26    agent.go                       SessionAgent: runs LLM conversations per session
 27    coordinator.go                 Coordinator: manages named agents ("coder", "task")
 28    hooked_tool.go                 Decorator that runs PreToolUse hooks before tool execution
 29    prompts.go                     Loads Go-template system prompts
 30    templates/                     System prompt templates (coder.md.tpl, task.md.tpl, etc.)
 31    tools/                         All built-in tools (bash, edit, view, grep, glob, etc.)
 32      mcp/                         MCP client integration
 33  hooks/                           Hook engine: runs user shell commands on hook events
 34    hooks.go                       Decision types, aggregation logic, event constants
 35    runner.go                      Parallel hook execution, timeout, dedup
 36    input.go                       Stdin payload builder, env vars, stdout parsing (Crush + Claude Code compat)
 37  session/session.go               Session CRUD backed by SQLite
 38  message/                         Message model and content types
 39  db/                              SQLite via sqlc, with migrations
 40    sql/                           Raw SQL queries (consumed by sqlc)
 41    migrations/                    Schema migrations
 42  lsp/                             LSP client manager, auto-discovery, on-demand startup
 43  ui/                              Bubble Tea v2 TUI (see internal/ui/AGENTS.md)
 44  permission/                      Tool permission checking and allow-lists
 45  skills/                          Skill file discovery and loading
 46  shell/                           Bash command execution with background job support
 47  event/                           Telemetry (PostHog)
 48  pubsub/                          Internal pub/sub for cross-component messaging
 49  filetracker/                     Tracks files touched per session
 50  history/                         Prompt history
 51```
 52
 53### Key Dependency Roles
 54
 55- **`charm.land/fantasy`**: LLM provider abstraction layer. Handles protocol
 56  differences between Anthropic, OpenAI, Gemini, etc. Used in `internal/app`
 57  and `internal/agent`.
 58- **`charm.land/bubbletea/v2`**: TUI framework powering the interactive UI.
 59- **`charm.land/lipgloss/v2`**: Terminal styling.
 60- **`charm.land/glamour/v2`**: Markdown rendering in the terminal.
 61- **`charm.land/catwalk`**: Snapshot/golden-file testing for TUI components.
 62- **`sqlc`**: Generates Go code from SQL queries in `internal/db/sql/`.
 63
 64### Key Patterns
 65
 66- **Config is a Service**: accessed via `config.Service`, not global state.
 67- **Tools are self-documenting**: each tool has a `.go` implementation and a
 68  `.md` description file in `internal/agent/tools/`.
 69- **System prompts are Go templates**: `internal/agent/templates/*.md.tpl`
 70  with runtime data injected.
 71- **Context files**: Crush reads AGENTS.md, CRUSH.md, CLAUDE.md, GEMINI.md
 72  (and `.local` variants) from the working directory for project-specific
 73  instructions.
 74- **Persistence**: SQLite + sqlc. All queries live in `internal/db/sql/`,
 75  generated code in `internal/db/`. Migrations in `internal/db/migrations/`.
 76- **Pub/sub**: `internal/pubsub` for decoupled communication between agent,
 77  UI, and services.
 78- **Hooks**: User-defined shell commands in `crush.json` that fire before
 79  tool execution. The engine (`internal/hooks/`) is independent of fantasy
 80  and agent — it takes inputs, runs commands, returns decisions. The
 81  `hookedTool` decorator in `internal/agent/hooked_tool.go` wraps tools at
 82  the coordinator level. Hooks run before permission checks. See
 83  `HOOKS.md` for the user-facing protocol.
 84- **CGO disabled**: builds with `CGO_ENABLED=0` and
 85  `GOEXPERIMENT=greenteagc`.
 86
 87## Build/Test/Lint Commands
 88
 89- **Build**: `go build .` or `go run .`
 90- **Test**: `task test` or `go test ./...` (run single test:
 91  `go test ./internal/llm/prompt -run TestGetContextFromPaths`)
 92- **Update Golden Files**: `go test ./... -update` (regenerates `.golden`
 93  files when test output changes)
 94  - Update specific package:
 95    `go test ./internal/tui/components/core -update` (in this case,
 96    we're updating "core")
 97- **Lint**: `task lint:fix`
 98- **Format**: `task fmt` (`gofumpt -w .`)
 99- **Modernize**: `task modernize` (runs `modernize` which makes code
100  simplifications)
101- **Dev**: `task dev` (runs with profiling enabled)
102- **Pre-commit flow**: `task fmt lint-fix test`
103
104## Code Style Guidelines
105
106- **Imports**: Use `goimports` formatting, group stdlib, external, internal
107  packages.
108- **Formatting**: Use gofumpt (stricter than gofmt), enabled in
109  golangci-lint.
110- **Naming**: Standard Go conventions — PascalCase for exported, camelCase
111  for unexported.
112- **Types**: Prefer explicit types, use type aliases for clarity (e.g.,
113  `type AgentName string`).
114- **Error handling**: Return errors explicitly, use `fmt.Errorf` for
115  wrapping.
116- **Context**: Always pass `context.Context` as first parameter for
117  operations.
118- **Interfaces**: Define interfaces in consuming packages, keep them small
119  and focused.
120- **Structs**: Use struct embedding for composition, group related fields.
121- **Constants**: Use typed constants with iota for enums, group in const
122  blocks.
123- **Testing**: Use testify's `require` package, parallel tests with
124  `t.Parallel()`, `t.SetEnv()` to set environment variables. Always use
125  `t.Tempdir()` when in need of a temporary directory. This directory does
126  not need to be removed.
127- **JSON tags**: Use snake_case for JSON field names.
128- **File permissions**: Use octal notation (0o755, 0o644) for file
129  permissions.
130- **Log messages**: Log messages must start with a capital letter (e.g.,
131  "Failed to save session" not "failed to save session").
132  - This is enforced by `task lint:log` which runs as part of `task lint`.
133- **Comments**: End comments in periods unless comments are at the end of the
134  line.
135
136## Testing with Mock Providers
137
138When writing tests that involve provider configurations, use the mock
139providers to avoid API calls:
140
141```go
142func TestYourFunction(t *testing.T) {
143    // Enable mock providers for testing
144    originalUseMock := config.UseMockProviders
145    config.UseMockProviders = true
146    defer func() {
147        config.UseMockProviders = originalUseMock
148        config.ResetProviders()
149    }()
150
151    // Reset providers to ensure fresh mock data
152    config.ResetProviders()
153
154    // Your test code here - providers will now return mock data
155    providers := config.Providers()
156    // ... test logic
157}
158```
159
160## Formatting
161
162- ALWAYS format any Go code you write.
163  - First, try `gofumpt -w .`.
164  - If `gofumpt` is not available, use `goimports`.
165  - If `goimports` is not available, use `gofmt`.
166  - You can also use `task fmt` to run `gofumpt -w .` on the entire project,
167    as long as `gofumpt` is on the `PATH`.
168
169## Comments
170
171- Comments that live on their own lines should start with capital letters and
172  end with periods. Wrap comments at 78 columns.
173
174## Committing
175
176- ALWAYS use semantic commits (`fix:`, `feat:`, `chore:`, `refactor:`,
177  `docs:`, `sec:`, etc).
178- Try to keep commits to one line, not including your attribution. Only use
179  multi-line commits when additional context is truly necessary.
180
181## Working on the TUI (UI)
182
183Anytime you need to work on the TUI, read `internal/ui/AGENTS.md` before
184starting work.