{"record":{"id":"c08b60f89ba4f01e","repo":"dotnet/machinelearning","slug":"can-t-add-or-update-result-that-already-save-to-cs","errorCode":null,"errorMessage":"can't add or update result that already save to csv","messagePattern":"can't add or update result that already save to csv","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.AutoML/AutoMLExperiment/ITrialResultManager.cs","lineNumber":63,"sourceCode":"            schemaBuilder.AddColumn(\"id\", NumberDataViewType.Int32);\n            schemaBuilder.AddColumn(\"loss\", NumberDataViewType.Single);\n            schemaBuilder.AddColumn(\"durationInMilliseconds\", NumberDataViewType.Single);\n            schemaBuilder.AddColumn(\"peakCpu\", NumberDataViewType.Single);\n            schemaBuilder.AddColumn(\"peakMemoryInMegaByte\", NumberDataViewType.Single);\n            schemaBuilder.AddColumn(\"parameter\", new VectorDataViewType(NumberDataViewType.Double));\n            _schema = schemaBuilder.ToSchema();\n\n            // load from csv file.\n            var trialResults = LoadFromCsvFile(filePath);\n            _trialResultsHistory = new HashSet<TrialResult>(trialResults, new TrialResult());\n            _newTrialResults = new HashSet<TrialResult>(new TrialResult());\n        }\n\n        public void AddOrUpdateTrialResult(TrialResult result)\n        {\n            if (_trialResultsHistory.Contains(result))\n            {\n                throw new ArgumentException(\"can't add or update result that already save to csv\");\n            }\n            _newTrialResults.Remove(result);\n            _newTrialResults.Add(result);\n        }\n\n        public IEnumerable<TrialResult> GetAllTrialResults()\n        {\n            return _trialResultsHistory.Concat(_newTrialResults);\n        }\n\n        /// <summary>\n        /// save trial result to csv. This will not overwrite any existing records that already written in csv.\n        /// </summary>\n        public void Save()\n        {\n            // header (type)\n            // | id (int) | loss (float) | durationInMilliseconds (float) | peakCpu (float) | peakMemoryInMegaByte (float) | parameter_i (float) |\n            using (var fileStream = new FileStream(_filePath, FileMode.Append, FileAccess.Write))","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.AutoML/AutoMLExperiment/ITrialResultManager.cs#L45-L81","documentation":"CsvTrialResultManager.AddOrUpdateTrialResult throws this ArgumentException when the TrialResult being added is already present in its _trialResultsHistory. The manager keeps a history of results already persisted to CSV and refuses duplicates to avoid double-logging a trial. It indicates the same TrialResult instance (or one comparing equal) was submitted twice.","triggerScenarios":"Calling AddOrUpdateTrialResult with a TrialResult that was previously recorded in the trial results history; a trial runner re-reporting the same completed trial, or trial IDs being reused so the same result object flows through twice.","commonSituations":"Custom ITrialResultManager implementations or experiment runners that retry trials without generating a fresh TrialResult; replaying a trial log; id collisions causing the same result to be registered repeatedly.","solutions":["Ensure each trial produces a unique TrialResult (unique TrialSettings/TrialId) and is only passed to AddOrUpdateTrialResult once","Check _trialResultsHistory (GetAllTrialResults) before calling to skip already-recorded results","If updating is intended, implement the update path in the manager instead of re-adding; remove the stale entry first","Fix trial retry logic so retried trials create new result objects rather than resubmitting the old one"],"exampleFix":"// before\nmanager.AddOrUpdateTrialResult(result);\n// after\nif (!manager.GetAllTrialResults().Any(r => r.TrialSettings.TrialId == result.TrialSettings.TrialId))\n{\n    manager.AddOrUpdateTrialResult(result);\n}","handlingStrategy":"validation","validationCode":"bool alreadyLogged = manager.GetAllTrialResults().Any(r => r == result || r.TrialSettings.TrialId == result.TrialSettings.TrialId);\nif (alreadyLogged) return;","typeGuard":null,"tryCatchPattern":"try { manager.AddOrUpdateTrialResult(result); }\ncatch (ArgumentException) { /* result already recorded — skip */ }","preventionTips":["Generate a unique TrialResult per trial run","Track already-submitted trial IDs in the runner","Never reuse TrialResult instances across retries"],"tags":["dotnet","ml-automl","duplicate-record"],"backgroundTag":"file-already-exists","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}