charmbracelet/crush · error

failed to update preferred model: %w

Error message

failed to update preferred model: %w

What it means

UpdatePreferredModel wraps a transport-level failure of the POST to /workspaces/{id}/config/model. The model selection was not delivered to the server.

Source

Thrown at internal/client/config.go:55

	if err != nil {
		return fmt.Errorf("failed to remove config field: %w", err)
	}
	defer rsp.Body.Close()
	if rsp.StatusCode != http.StatusOK {
		return fmt.Errorf("failed to remove config field: status code %d", rsp.StatusCode)
	}
	return nil
}

// UpdatePreferredModel updates the preferred model on the server.
func (c *Client) UpdatePreferredModel(ctx context.Context, id string, scope config.Scope, modelType config.SelectedModelType, model config.SelectedModel) error {
	rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/config/model", id), nil, jsonBody(struct {
		Scope     config.Scope             `json:"scope"`
		ModelType config.SelectedModelType `json:"model_type"`
		Model     config.SelectedModel     `json:"model"`
	}{Scope: scope, ModelType: modelType, Model: model}), http.Header{"Content-Type": []string{"application/json"}})
	if err != nil {
		return fmt.Errorf("failed to update preferred model: %w", err)
	}
	defer rsp.Body.Close()
	if rsp.StatusCode != http.StatusOK {
		return fmt.Errorf("failed to update preferred model: status code %d", rsp.StatusCode)
	}
	return nil
}

// SetCompactMode sets compact mode on the server.
func (c *Client) SetCompactMode(ctx context.Context, id string, scope config.Scope, enabled bool) error {
	rsp, err := c.post(ctx, fmt.Sprintf("/workspaces/%s/config/compact", id), nil, jsonBody(struct {
		Scope   config.Scope `json:"scope"`
		Enabled bool         `json:"enabled"`
	}{Scope: scope, Enabled: enabled}), http.Header{"Content-Type": []string{"application/json"}})
	if err != nil {
		return fmt.Errorf("failed to set compact mode: %w", err)
	}
	defer rsp.Body.Close()

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Verify the server is reachable and retry the update
  2. Check the wrapped cause; if the context expired, increase the deadline
  3. Re-run restoreModelFromSession after reconnection
Defensive patterns

Strategy: retry

Validate before calling

// Validate model before sending
if model.ID == "" {
    return fmt.Errorf("model id is required")
}
if err := ctx.Err(); err != nil {
    return err
}

Try / catch

if err := c.UpdatePreferredModel(ctx, id, scope, modelType, model); err != nil {
    if isTransient(err) {
        // retry with backoff
    }
    return err
}

Prevention

When it happens

Trigger: c.post fails (connection refused, context canceled) while updating the preferred model; called by overrideModels and restoreModelFromSession.

Common situations: Restoring a model from a session after the server restarted; transient socket drop during model override.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29). Data as JSON: /api/errors/853e6c947421cea5. Report an issue: GitHub.