fix: clone models map before passing across goroutine boundaries

Christian Rocha created

💘 Generated with Crush

Assisted-by: GLM-5.1 via Crush <crush@charm.land>

Change summary

internal/agent/coordinator.go | 7 ++++++-
internal/agent/tools/todos.go | 8 +++++++-
2 files changed, 13 insertions(+), 2 deletions(-)

Detailed changes

internal/agent/coordinator.go 🔗

@@ -1051,7 +1051,12 @@ func (c *coordinator) updateParentSessionCost(ctx context.Context, childSessionI
 
 	parentSession.Cost += childSession.Cost
 
-	if _, err := c.sessions.SaveWithModels(ctx, parentSession, c.cfg.Config().Models); err != nil {
+	models := maps.Clone(c.cfg.Config().Models)
+	for name, model := range models {
+		model.ProviderOptions = maps.Clone(model.ProviderOptions)
+		models[name] = model
+	}
+	if _, err := c.sessions.SaveWithModels(ctx, parentSession, models); err != nil {
 		return fmt.Errorf("save parent session: %w", err)
 	}
 

internal/agent/tools/todos.go 🔗

@@ -4,6 +4,7 @@ import (
 	"context"
 	_ "embed"
 	"fmt"
+	"maps"
 
 	"charm.land/fantasy"
 	"github.com/charmbracelet/crush/internal/config"
@@ -97,7 +98,12 @@ func NewTodosTool(sessions session.Service, cfg *config.ConfigStore) fantasy.Age
 			}
 
 			currentSession.Todos = todos
-			_, err = sessions.SaveWithModels(ctx, currentSession, cfg.Config().Models)
+			models := maps.Clone(cfg.Config().Models)
+			for name, model := range models {
+				model.ProviderOptions = maps.Clone(model.ProviderOptions)
+				models[name] = model
+			}
+			_, err = sessions.SaveWithModels(ctx, currentSession, models)
 			if err != nil {
 				return fantasy.ToolResponse{}, fmt.Errorf("failed to save todos: %w", err)
 			}