{"record":{"id":"d99dfc7a415308b6","repo":"plandex-ai/plandex","slug":"error-applying-model-settings-v","errorCode":null,"errorMessage":"error applying model settings: %v","messagePattern":"error applying model settings: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/cli/lib/model_settings.go","lineNumber":207,"sourceCode":"\n\terr = WriteModelSettingsFile(path, settings)\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"error writing model settings file: %v\", err)\n\t}\n\n\treturn true, nil\n}\n\n// save settings in file to server\nfunc SyncPlanModelSettings() error {\n\tsettings, err := api.Client.GetSettings(CurrentPlanId, CurrentBranch)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error getting settings: %v\", err)\n\t}\n\n\tupdatedSettings, apiErr := ApplyModelSettings(GetPlanModelSettingsPath(CurrentPlanId), settings)\n\tif apiErr != nil {\n\t\treturn fmt.Errorf(\"error applying model settings: %v\", err)\n\t}\n\n\tres, updateErr := api.Client.UpdateSettings(CurrentPlanId, CurrentBranch, shared.UpdateSettingsRequest{\n\t\tModelPackName: updatedSettings.ModelPackName,\n\t\tModelPack:     updatedSettings.ModelPack,\n\t})\n\n\tif updateErr != nil {\n\t\treturn fmt.Errorf(\"error updating settings: %v\", err)\n\t}\n\n\tif res == nil {\n\t\treturn nil\n\t}\n\n\tfmt.Println(res.Msg)\n\n\treturn nil","sourceCodeStart":189,"sourceCodeEnd":225,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/cli/lib/model_settings.go#L189-L225","documentation":"SyncPlanModelSettings calls ApplyModelSettings on the local plan settings file to merge local model-pack changes into the fetched server settings. If ApplyModelSettings returns an error (file read, validation, copy, or hash-save failure inside it), it is wrapped here. Caution: this line wraps `err` (the GetSettings error variable) instead of `apiErr`, so the message can misreport the underlying cause — a common source of confusing diagnostics.","triggerScenarios":"ApplyModelSettings fails: the settings file is missing or unreadable, ValidateModelPackInlineJSON rejects the JSON (which also os.Exit(1)s), DeepCopy fails, or SaveModelPackRolesHash fails writing the hash file.","commonSituations":"The plan settings file was deleted before sync; the JSON was hand-edited into an invalid model pack; the hash sidecar file's directory is read-only; running sync before any local settings were ever written.","solutions":["Inspect the inner error: if it mentions the settings file path, re-create the file (e.g. via checkout) before syncing.","Fix JSON validation errors reported above the error (ValidateModelPackInlineJSON prints details) in the settings file.","Fix the wrapping bug: change `fmt.Errorf(\"error applying model settings: %v\", err)` to wrap `apiErr` so future errors point at the real cause.","Check write permissions on the directory where SaveModelPackRolesHash stores the hash file."],"exampleFix":"// before (bug: wraps wrong variable)\nupdatedSettings, apiErr := ApplyModelSettings(GetPlanModelSettingsPath(CurrentPlanId), settings)\nif apiErr != nil {\n    return fmt.Errorf(\"error applying model settings: %v\", err)\n}\n// after\nupdatedSettings, apiErr := ApplyModelSettings(GetPlanModelSettingsPath(CurrentPlanId), settings)\nif apiErr != nil {\n    return fmt.Errorf(\"error applying model settings: %w\", apiErr)\n}","handlingStrategy":"validation","validationCode":"path := lib.GetPlanModelSettingsPath(planID)\nif _, err := os.Stat(path); os.IsNotExist(err) {\n    return fmt.Errorf(\"no local settings at %s; run checkout first before SyncPlanModelSettings\", path)\n}\ndata, _ := os.ReadFile(path)\nif jerr := json.Unmarshal(data, &json.RawMessage{}); jerr != nil {\n    return fmt.Errorf(\"local settings invalid JSON, ApplyModelSettings will fail: %w\", jerr)\n}","typeGuard":"func localSettingsReady(path string) bool {\n    data, err := os.ReadFile(path)\n    if err != nil { return false }\n    var v any\n    return json.Unmarshal(data, &v) == nil\n}","tryCatchPattern":"err := lib.SyncPlanModelSettings()\nif err != nil && strings.Contains(err.Error(), \"error applying model settings\") {\n    // NOTE: library wraps the wrong variable here; inspect the inner text,\n    // fix the settings file, and retry\n    log.Warnf(\"apply failed: %v\", err)\n    return regenerateLocalSettingsThenRetry()\n}","preventionTips":["Always run checkout/initialize before sync so the settings file exists.","Validate the settings JSON after any manual edit.","Ensure the hash sidecar directory is writable.","Patch the library to wrap apiErr instead of err for accurate messages."],"tags":["go","wrapped-wrong-error","settings-sync","validation"],"backgroundTag":"wrong-error-variable-wrapped","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}