From 3ff0f03872c0a3a8cc2d521af8858bf3892f9665 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 | 25 +++++++++++++++++++++++++ internal/agent/prompt/prompt.go | 25 ++++++++++++++++++++----- internal/agent/templates/coder.md.tpl | 18 ++++++++++++++++-- internal/config/config.go | 1 + internal/config/load.go | 9 +++++++++ schema.json | 11 +++++++++++ 6 files changed, 82 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b1fcc099bf9ab4eff064927cd31ca9ef7c6975c3..03dc40b45a0ff97b568b5f7f76040ee17e7afcb3 100644 --- a/README.md +++ b/README.md @@ -315,6 +315,31 @@ using `$(echo $VAR)` syntax. } ``` +### Memory 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 tools or workflows here. + +You can customize these paths using the `memory_paths` option in your +configuration: + +```jsonc +{ + "$schema": "https://charm.land/crush.json", + "options": { + "memory_paths": [ + "~/path/to/custom/memory/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 d68c7c132116c49cd004bee52169be7487133efa..ee7f8095b45dff241636b9d66d0e759289384ee5 100644 --- a/internal/agent/prompt/prompt.go +++ b/internal/agent/prompt/prompt.go @@ -36,6 +36,7 @@ type PromptDat struct { Date string GitStatus string ContextFiles []ContextFile + MemoryFiles []ContextFile AvailSkillXML string } @@ -152,16 +153,27 @@ func (p *Prompt) promptData(ctx context.Context, provider, model string, cfg con workingDir := cmp.Or(p.workingDir, cfg.WorkingDir()) platform := cmp.Or(p.platform, runtime.GOOS) - files := map[string][]ContextFile{} + contextFiles := map[string][]ContextFile{} + memoryFiles := map[string][]ContextFile{} for _, pth := range cfg.Options.ContextPaths { expanded := expandPath(pth, cfg) pathKey := strings.ToLower(expanded) - if _, ok := files[pathKey]; ok { + if _, ok := contextFiles[pathKey]; ok { continue } content := processContextPath(expanded, cfg) - files[pathKey] = content + contextFiles[pathKey] = content + } + + for _, pth := range cfg.Options.MemoryPaths { + expanded := expandPath(pth, cfg) + pathKey := strings.ToLower(expanded) + if _, ok := memoryFiles[pathKey]; ok { + continue + } + content := processContextPath(expanded, cfg) + memoryFiles[pathKey] = content } // Discover and load skills metadata. @@ -195,8 +207,11 @@ func (p *Prompt) promptData(ctx context.Context, provider, model string, cfg con } } - for _, contextFiles := range files { - data.ContextFiles = append(data.ContextFiles, contextFiles...) + for _, files := range contextFiles { + data.ContextFiles = append(data.ContextFiles, files...) + } + for _, files := range memoryFiles { + data.MemoryFiles = append(data.MemoryFiles, files...) } return data, nil } diff --git a/internal/agent/templates/coder.md.tpl b/internal/agent/templates/coder.md.tpl index e0bbe34b92003a691a158a40d330db55b4b9c9b8..b6177ba30804463dfb5b44391ec21efb2f4b3437 100644 --- a/internal/agent/templates/coder.md.tpl +++ b/internal/agent/templates/coder.md.tpl @@ -382,11 +382,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 .MemoryFiles}} +# 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 .MemoryFiles}} + +{{.Content}} + +{{end}} + {{end}} diff --git a/internal/config/config.go b/internal/config/config.go index 604e449ee389fbae3eac7845222ac83701ca61af..ed0aa063c109eb80c0f21d4cfdfe6b670806db5a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -245,6 +245,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"` + MemoryPaths []string `json:"memory_paths,omitempty" jsonschema:"description=Paths to files containing memory 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 3ad4b909cb16cf5672dcadc9322a476854350632..e5a53fd717db0733a88ac01d4fda0284f6d79f8f 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -340,6 +340,14 @@ func (c *Config) setDefaults(workingDir, dataDir string) { if c.Options.ContextPaths == nil { c.Options.ContextPaths = []string{} } + if c.Options.MemoryPaths == nil { + crushConfigDir := filepath.Dir(GlobalConfig()) + c.Options.MemoryPaths = []string{ + filepath.Join(crushConfigDir, "CRUSH.md"), + filepath.Join(filepath.Dir(crushConfigDir), "AGENTS.md"), + } + } + c.Options.ContextPaths = append(c.Options.ContextPaths, c.Options.MemoryPaths...) if c.Options.SkillsPaths == nil { c.Options.SkillsPaths = []string{} } @@ -373,6 +381,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 150fbb2c2d698a12254891b53ed48f08ee2b1355..1a2123d71a9bd5b504f2257b2b6d32e4dd3a6aa3 100644 --- a/schema.json +++ b/schema.json @@ -351,6 +351,17 @@ "type": "array", "description": "Paths to files containing context information for the AI" }, + "memory_paths": { + "items": { + "type": "string" + }, + "type": "array", + "description": "Paths to files containing memory information for the AI", + "default": [ + "~/.config/crush/CRUSH.md", + "~/.config/AGENTS.md" + ] + }, "skills_paths": { "items": { "type": "string",