From 39a130a92e49dd23e7ad255909c9e99b2937e8c3 Mon Sep 17 00:00:00 2001 From: Amolith Date: Mon, 15 Sep 2025 18:00:18 -0600 Subject: [PATCH] feat: load user CRUSH.md and AGENTS.md from config Issue: charmbracelet/crush#1050 --- README.md | 27 +++++++++++++++ internal/agent/prompt/prompt.go | 48 ++++++++++++++++++--------- internal/agent/templates/coder.md.tpl | 18 ++++++++-- internal/config/config.go | 1 + internal/config/load.go | 12 +++++++ schema.json | 11 ++++++ 6 files changed, 100 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 589acf535f7b7acc324272f9c4315da090d00494..95ffa08c4d14a589da24450dccca8be33e31e00b 100644 --- a/README.md +++ b/README.md @@ -335,6 +335,33 @@ using `$(echo $VAR)` syntax. } ``` +### Global context files + +Crush automatically includes two files for cross-project instructions. + +- `~/.config/crush/CRUSH.md`: Crush-specific rules that would confuse other + agentic coding tools. If you only use Crush, this is the only one you need to + edit. +- `~/.config/AGENTS.md`: generic instructions that other coding tools might + read. Avoid referring to Crush-specific features or workflows here. You + probably only care about this if you use multiple agentic coding tools and + want to share instructions between them. + +You can customize these paths using the `global_context_paths` option in your +configuration: + +```jsonc +{ + "$schema": "https://charm.land/crush.json", + "options": { + "global_context_paths": [ + "~/path/to/custom/context/file.md", + "/full/path/to/folder/of/files/" // recursively load all .md files in folder + ] + } +} +``` + ### Ignoring Files Crush respects `.gitignore` files by default, but you can also create a diff --git a/internal/agent/prompt/prompt.go b/internal/agent/prompt/prompt.go index 070f29c18d02f220d309d5827f8d0c8879e0e5a2..108873b4b144e55c5dc172a9b9bc4601e10f5dab 100644 --- a/internal/agent/prompt/prompt.go +++ b/internal/agent/prompt/prompt.go @@ -9,6 +9,7 @@ import ( "path/filepath" "runtime" "strings" + "testing" "text/template" "time" @@ -28,16 +29,17 @@ type Prompt struct { } type PromptDat struct { - Provider string - Model string - Config config.Config - WorkingDir string - IsGitRepo bool - Platform string - Date string - GitStatus string - ContextFiles []ContextFile - AvailSkillXML string + Provider string + Model string + Config config.Config + WorkingDir string + IsGitRepo bool + Platform string + Date string + GitStatus string + ContextFiles []ContextFile + GlobalContextFiles []ContextFile + AvailSkillXML string } type ContextFile struct { @@ -153,17 +155,28 @@ func (p *Prompt) promptData(ctx context.Context, provider, model string, store * workingDir := cmp.Or(p.workingDir, store.WorkingDir()) platform := cmp.Or(p.platform, runtime.GOOS) - files := map[string][]ContextFile{} + contextFiles := map[string][]ContextFile{} + globalContextFiles := map[string][]ContextFile{} cfg := store.Config() for _, pth := range cfg.Options.ContextPaths { expanded := expandPath(pth, store) pathKey := strings.ToLower(expanded) - if _, ok := files[pathKey]; ok { + if _, ok := contextFiles[pathKey]; ok { continue } content := processContextPath(expanded, store) - files[pathKey] = content + contextFiles[pathKey] = content + } + + for _, pth := range cfg.Options.GlobalContextPaths { + expanded := expandPath(pth, store) + pathKey := strings.ToLower(expanded) + if _, ok := globalContextFiles[pathKey]; ok { + continue + } + content := processContextPath(expanded, store) + globalContextFiles[pathKey] = content } // Discover and load skills metadata. @@ -219,8 +232,13 @@ func (p *Prompt) promptData(ctx context.Context, provider, model string, store * } } - for _, contextFiles := range files { - data.ContextFiles = append(data.ContextFiles, contextFiles...) + for _, files := range contextFiles { + data.ContextFiles = append(data.ContextFiles, files...) + } + if !testing.Testing() { + for _, files := range globalContextFiles { + data.GlobalContextFiles = append(data.GlobalContextFiles, files...) + } } return data, nil } diff --git a/internal/agent/templates/coder.md.tpl b/internal/agent/templates/coder.md.tpl index c5f28f5590678360a7185e6f9f348382ffde4eb4..1d4d71af9ff4619d2c00f712f9ff63cc2b1b73f3 100644 --- a/internal/agent/templates/coder.md.tpl +++ b/internal/agent/templates/coder.md.tpl @@ -386,11 +386,25 @@ If a skill mentions scripts, references, or assets, they are placed in the same {{end}} {{if .ContextFiles}} - +# Project-Specific Context +Make sure to follow the instructions in the context below. + {{range .ContextFiles}} {{.Content}} {{end}} - + +{{end}} + +{{if .GlobalContextFiles}} +# User context +The following is personal content added by the user that they'd like you to follow no matter what project you're working in. + +{{range .GlobalContextFiles}} + +{{.Content}} + +{{end}} + {{end}} diff --git a/internal/config/config.go b/internal/config/config.go index cad914461606ec6e7b55f26048709ade2bbb2cf2..287eb796775f5c71cdc013463ad88625182b24dc 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -241,6 +241,7 @@ func (Attribution) JSONSchemaExtend(schema *jsonschema.Schema) { type Options struct { ContextPaths []string `json:"context_paths,omitempty" jsonschema:"description=Paths to files containing context information for the AI,example=.cursorrules,example=CRUSH.md"` + GlobalContextPaths []string `json:"global_context_paths,omitempty" jsonschema:"description=Paths to files containing global context information for the AI,default=~/.config/crush/CRUSH.md,default=~/.config/AGENTS.md"` SkillsPaths []string `json:"skills_paths,omitempty" jsonschema:"description=Paths to directories containing Agent Skills (folders with SKILL.md files),example=~/.config/crush/skills,example=./skills"` TUI *TUIOptions `json:"tui,omitempty" jsonschema:"description=Terminal user interface options"` Debug bool `json:"debug,omitempty" jsonschema:"description=Enable debug logging,default=false"` diff --git a/internal/config/load.go b/internal/config/load.go index b6a967ca0f48ad01492052cb5d7db92f3fd8aa47..b8dbb329f7eff635b0c0989d6defbce032307d5e 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -395,6 +395,17 @@ func (c *Config) setDefaults(workingDir, dataDir string) { if c.Options.TUI == nil { c.Options.TUI = &TUIOptions{} } + if len(c.Options.GlobalContextPaths) == 0 { + crushConfigDir := filepath.Dir(GlobalConfig()) + c.Options.GlobalContextPaths = []string{ + filepath.Join(crushConfigDir, "CRUSH.md"), + filepath.Join(crushConfigDir, "AGENTS.md"), + filepath.Join(filepath.Dir(crushConfigDir), "AGENTS.md"), + } + } + slices.Sort(c.Options.GlobalContextPaths) + c.Options.GlobalContextPaths = slices.Compact(c.Options.GlobalContextPaths) + if dataDir != "" { c.Options.DataDirectory = dataDir } else if c.Options.DataDirectory == "" { @@ -425,6 +436,7 @@ func (c *Config) setDefaults(workingDir, dataDir string) { // Add the default context paths if they are not already present c.Options.ContextPaths = append(defaultContextPaths, c.Options.ContextPaths...) + slices.Sort(c.Options.ContextPaths) c.Options.ContextPaths = slices.Compact(c.Options.ContextPaths) diff --git a/schema.json b/schema.json index 750e44d7674b46c3afb3874100de2b4545273bf5..8066b0625b74f448f3162b1a8940b199c3a7ef22 100644 --- a/schema.json +++ b/schema.json @@ -363,6 +363,17 @@ "type": "array", "description": "Paths to files containing context information for the AI" }, + "global_context_paths": { + "items": { + "type": "string" + }, + "type": "array", + "description": "Paths to files containing global context information for the AI", + "default": [ + "~/.config/crush/CRUSH.md", + "~/.config/AGENTS.md" + ] + }, "skills_paths": { "items": { "type": "string",