package main import ( "encoding/json" "fmt" "os" "path/filepath" "strings" "github.com/steveyegge/beads/internal/templates/agents" "github.com/steveyegge/beads/internal/ui" ) // addAgentsInstructions creates or updates the agents file with embedded template content. // agentFile is the target filename (e.g. "AGENTS.md" or "BEADS.md"). // If templatePath is non-empty, the custom template file is used instead of the embedded default. // profile controls which template variant to render (full or minimal); defaults to minimal. // opts controls conditional content (e.g. omitting bd dolt push when no remote is configured). func addAgentsInstructions(agentFile string, verbose bool, templatePath string, profile agents.Profile, opts agents.RenderOpts) { if profile == "" { profile = agents.ProfileMinimal } if err := updateAgentFile(agentFile, verbose, templatePath, profile, opts); err != nil { // Non-fatal - continue with other files if verbose { fmt.Fprintf(os.Stderr, "Warning: failed to update %s: %v\n", agentFile, err) } } } // updateAgentFile creates or updates an agent instructions file with embedded template content. // When a beads section already exists (legacy or current), it is updated to the latest // versioned format so that `bd init` never silently locks in stale sections. // If the file already has a full profile and a minimal profile is requested, the full // profile is preserved to avoid information loss. func updateAgentFile(filename string, verbose bool, templatePath string, profile agents.Profile, opts agents.RenderOpts) error { // Check if file exists //nolint:gosec // G304: filename validated by config.ValidateAgentsFile or defaulted to AGENTS.md content, err := os.ReadFile(filename) if os.IsNotExist(err) { // File doesn't exist - create from template var newContent string if templatePath != "" { //nolint:gosec // G304: templatePath comes from --agents-template flag data, readErr := os.ReadFile(templatePath) if readErr != nil { return fmt.Errorf("failed to read template %s: %w", templatePath, readErr) } newContent = string(data) } else { newContent = agents.EmbeddedDefault() } // Replace the beads section with the requested profile. // EmbeddedDefault() ships with profile:full; swap to the requested profile // (which defaults to minimal). Also handles legacy markers without profile metadata. if strings.Contains(newContent, "BEGIN BEADS INTEGRATION") { if replaced, changed, err := agents.ReplaceSectionWithOpts(newContent, profile, opts); err == nil && changed { newContent = replaced } } // #nosec G306 - markdown needs to be readable if err := os.WriteFile(filename, []byte(newContent), 0644); err != nil { return fmt.Errorf("failed to create %s: %w", filename, err) } if verbose { fmt.Printf(" %s Created %s with agent instructions\n", ui.RenderPass("✓"), filename) } return nil } else if err != nil { return fmt.Errorf("failed to read %s: %w", filename, err) } // File exists - check if it already has our sections contentStr := string(content) hasBeads := strings.Contains(contentStr, "BEGIN BEADS INTEGRATION") if hasBeads { // Preserve existing full profile when minimal is requested (avoid information loss) effectiveProfile := profile existingMeta := agents.ParseMarker(contentStr[strings.Index(contentStr, "