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    prompts.go                     Loads Go-template system prompts
 29    templates/                     System prompt templates (coder.md.tpl, task.md.tpl, etc.)
 30    tools/                         All built-in tools (bash, edit, view, grep, glob, etc.)
 31      mcp/                         MCP client integration
 32  session/session.go               Session CRUD backed by SQLite
 33  message/                         Message model and content types
 34  db/                              SQLite via sqlc, with migrations
 35    sql/                           Raw SQL queries (consumed by sqlc)
 36    migrations/                    Schema migrations
 37  lsp/                             LSP client manager, auto-discovery, on-demand startup
 38  ui/                              Bubble Tea v2 TUI (see internal/ui/AGENTS.md)
 39  permission/                      Tool permission checking and allow-lists
 40  skills/                          Skill file discovery and loading
 41  shell/                           Bash command execution with background job support
 42  event/                           Telemetry (PostHog)
 43  pubsub/                          Internal pub/sub for cross-component messaging
 44  filetracker/                     Tracks files touched per session
 45  history/                         Prompt history
 46```
 47
 48### Key Dependency Roles
 49
 50- **`charm.land/fantasy`**: LLM provider abstraction layer. Handles protocol
 51  differences between Anthropic, OpenAI, Gemini, etc. Used in `internal/app`
 52  and `internal/agent`.
 53- **`charm.land/bubbletea/v2`**: TUI framework powering the interactive UI.
 54- **`charm.land/lipgloss/v2`**: Terminal styling.
 55- **`charm.land/glamour/v2`**: Markdown rendering in the terminal.
 56- **`charm.land/catwalk`**: Snapshot/golden-file testing for TUI components.
 57- **`sqlc`**: Generates Go code from SQL queries in `internal/db/sql/`.
 58
 59### Key Patterns
 60
 61- **Config is a Service**: accessed via `config.Service`, not global state.
 62- **Tools are self-documenting**: each tool has a `.go` implementation and a
 63  `.md` description file in `internal/agent/tools/`.
 64- **System prompts are Go templates**: `internal/agent/templates/*.md.tpl`
 65  with runtime data injected.
 66- **Context files**: Crush reads AGENTS.md, CRUSH.md, CLAUDE.md, GEMINI.md
 67  (and `.local` variants) from the working directory for project-specific
 68  instructions.
 69- **Persistence**: SQLite + sqlc. All queries live in `internal/db/sql/`,
 70  generated code in `internal/db/`. Migrations in `internal/db/migrations/`.
 71- **Pub/sub**: `internal/pubsub` for decoupled communication between agent,
 72  UI, and services.
 73- **CGO disabled**: builds with `CGO_ENABLED=0` and
 74  `GOEXPERIMENT=greenteagc`.
 75
 76## Build/Test/Lint Commands
 77
 78- **Build**: `go build .` or `go run .`
 79- **Test**: `task test` or `go test ./...` (run single test:
 80  `go test ./internal/llm/prompt -run TestGetContextFromPaths`)
 81- **Update Golden Files**: `go test ./... -update` (regenerates `.golden`
 82  files when test output changes)
 83  - Update specific package:
 84    `go test ./internal/tui/components/core -update` (in this case,
 85    we're updating "core")
 86- **Lint**: `task lint:fix`
 87- **Format**: `task fmt` (`gofumpt -w .`)
 88- **Modernize**: `task modernize` (runs `modernize` which makes code
 89  simplifications)
 90- **Dev**: `task dev` (runs with profiling enabled)
 91
 92## Code Style Guidelines
 93
 94- **Imports**: Use `goimports` formatting, group stdlib, external, internal
 95  packages.
 96- **Formatting**: Use gofumpt (stricter than gofmt), enabled in
 97  golangci-lint.
 98- **Naming**: Standard Go conventions — PascalCase for exported, camelCase
 99  for unexported.
100- **Types**: Prefer explicit types, use type aliases for clarity (e.g.,
101  `type AgentName string`).
102- **Error handling**: Return errors explicitly, use `fmt.Errorf` for
103  wrapping.
104- **Context**: Always pass `context.Context` as first parameter for
105  operations.
106- **Interfaces**: Define interfaces in consuming packages, keep them small
107  and focused.
108- **Structs**: Use struct embedding for composition, group related fields.
109- **Constants**: Use typed constants with iota for enums, group in const
110  blocks.
111- **Testing**: Use testify's `require` package, parallel tests with
112  `t.Parallel()`, `t.SetEnv()` to set environment variables. Always use
113  `t.Tempdir()` when in need of a temporary directory. This directory does
114  not need to be removed.
115- **JSON tags**: Use snake_case for JSON field names.
116- **File permissions**: Use octal notation (0o755, 0o644) for file
117  permissions.
118- **Log messages**: Log messages must start with a capital letter (e.g.,
119  "Failed to save session" not "failed to save session").
120  - This is enforced by `task lint:log` which runs as part of `task lint`.
121- **Comments**: End comments in periods unless comments are at the end of the
122  line.
123
124## Testing with Mock Providers
125
126When writing tests that involve provider configurations, use the mock
127providers to avoid API calls:
128
129```go
130func TestYourFunction(t *testing.T) {
131    // Enable mock providers for testing
132    originalUseMock := config.UseMockProviders
133    config.UseMockProviders = true
134    defer func() {
135        config.UseMockProviders = originalUseMock
136        config.ResetProviders()
137    }()
138
139    // Reset providers to ensure fresh mock data
140    config.ResetProviders()
141
142    // Your test code here - providers will now return mock data
143    providers := config.Providers()
144    // ... test logic
145}
146```
147
148## Formatting
149
150- ALWAYS format any Go code you write.
151  - First, try `gofumpt -w .`.
152  - If `gofumpt` is not available, use `goimports`.
153  - If `goimports` is not available, use `gofmt`.
154  - You can also use `task fmt` to run `gofumpt -w .` on the entire project,
155    as long as `gofumpt` is on the `PATH`.
156
157## Comments
158
159- Comments that live on their own lines should start with capital letters and
160  end with periods. Wrap comments at 78 columns.
161
162## Committing
163
164- ALWAYS use semantic commits (`fix:`, `feat:`, `chore:`, `refactor:`,
165  `docs:`, `sec:`, etc).
166- Try to keep commits to one line, not including your attribution. Only use
167  multi-line commits when additional context is truly necessary.
168
169## Working on the TUI (UI)
170
171Anytime you need to work on the TUI, read `internal/ui/AGENTS.md` before
172starting work.