{"record":{"id":"853e6c947421cea5","repo":"charmbracelet/crush","slug":"failed-to-update-preferred-model-w","errorCode":null,"errorMessage":"failed to update preferred model: %w","messagePattern":"failed to update preferred model: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/client/config.go","lineNumber":55,"sourceCode":"\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to remove config field: %w\", err)\n\t}\n\tdefer rsp.Body.Close()\n\tif rsp.StatusCode != http.StatusOK {\n\t\treturn fmt.Errorf(\"failed to remove config field: status code %d\", rsp.StatusCode)\n\t}\n\treturn nil\n}\n\n// UpdatePreferredModel updates the preferred model on the server.\nfunc (c *Client) UpdatePreferredModel(ctx context.Context, id string, scope config.Scope, modelType config.SelectedModelType, model config.SelectedModel) error {\n\trsp, err := c.post(ctx, fmt.Sprintf(\"/workspaces/%s/config/model\", id), nil, jsonBody(struct {\n\t\tScope     config.Scope             `json:\"scope\"`\n\t\tModelType config.SelectedModelType `json:\"model_type\"`\n\t\tModel     config.SelectedModel     `json:\"model\"`\n\t}{Scope: scope, ModelType: modelType, Model: model}), http.Header{\"Content-Type\": []string{\"application/json\"}})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to update preferred model: %w\", err)\n\t}\n\tdefer rsp.Body.Close()\n\tif rsp.StatusCode != http.StatusOK {\n\t\treturn fmt.Errorf(\"failed to update preferred model: status code %d\", rsp.StatusCode)\n\t}\n\treturn nil\n}\n\n// SetCompactMode sets compact mode on the server.\nfunc (c *Client) SetCompactMode(ctx context.Context, id string, scope config.Scope, enabled bool) error {\n\trsp, err := c.post(ctx, fmt.Sprintf(\"/workspaces/%s/config/compact\", id), nil, jsonBody(struct {\n\t\tScope   config.Scope `json:\"scope\"`\n\t\tEnabled bool         `json:\"enabled\"`\n\t}{Scope: scope, Enabled: enabled}), http.Header{\"Content-Type\": []string{\"application/json\"}})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to set compact mode: %w\", err)\n\t}\n\tdefer rsp.Body.Close()","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/client/config.go#L37-L73","documentation":"UpdatePreferredModel wraps a transport-level failure of the POST to /workspaces/{id}/config/model. The model selection was not delivered to the server.","triggerScenarios":"c.post fails (connection refused, context canceled) while updating the preferred model; called by overrideModels and restoreModelFromSession.","commonSituations":"Restoring a model from a session after the server restarted; transient socket drop during model override.","solutions":["Verify the server is reachable and retry the update","Check the wrapped cause; if the context expired, increase the deadline","Re-run restoreModelFromSession after reconnection"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Validate model before sending\nif model.ID == \"\" {\n    return fmt.Errorf(\"model id is required\")\n}\nif err := ctx.Err(); err != nil {\n    return err\n}","typeGuard":null,"tryCatchPattern":"if err := c.UpdatePreferredModel(ctx, id, scope, modelType, model); err != nil {\n    if isTransient(err) {\n        // retry with backoff\n    }\n    return err\n}","preventionTips":["Retry model-restore flows after reconnection","Use contexts with adequate deadlines","Verify the server is healthy before mutating model state"],"tags":["http","network","model-selection"],"backgroundTag":"http-request-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}