{"record":{"id":"a90d695670825893","repo":"microsoft/autogen","slug":"invalid-mode","errorCode":null,"errorMessage":"Invalid mode","messagePattern":"Invalid mode","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen/Middleware/HumanInputMiddleware.cs","lineNumber":84,"sourceCode":"        {\n            if (await isTermination(context.Messages, cancellationToken) is false)\n            {\n                return await agent.GenerateReplyAsync(context.Messages, context.Options, cancellationToken);\n            }\n\n            this.writeLine(prompt);\n            var input = getInput();\n            if (input == exitKeyword)\n            {\n                return new TextMessage(Role.Assistant, GroupChatExtension.TERMINATE, agent.Name);\n            }\n\n            input ??= string.Empty;\n\n            return new TextMessage(Role.Assistant, input, agent.Name);\n        }\n\n        throw new InvalidOperationException(\"Invalid mode\");\n    }\n\n    private async Task<bool> DefaultIsTermination(IEnumerable<IMessage> messages, CancellationToken _)\n    {\n        return messages?.Last().IsGroupChatTerminateMessage() is true;\n    }\n\n    private string? GetInput()\n    {\n        return Console.ReadLine();\n    }\n\n    private void WriteLine(string message)\n    {\n        Console.WriteLine(message);\n    }\n}\n","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen/Middleware/HumanInputMiddleware.cs#L66-L102","documentation":"Unreachable-in-practice defensive throw at the end of HumanInputMiddleware.InvokeAsync. The method handles HumanInputMode.NEVER, ALWAYS, and AUTO explicitly; the InvalidOperationException only fires if 'mode' holds an enum value outside those three (e.g. (HumanInputMode)99 cast from an int or from a bad config parse).","triggerScenarios":"Constructing HumanInputMiddleware with a mode produced by an unchecked cast: (HumanInputMode)42, or parsing a numeric/string config value into the enum without Enum.IsDefined validation.","commonSituations":"Driving HumanInputMode from appsettings or CLI args where an out-of-range integer is accepted by the compiler/parser; interop code that passes raw ints across a boundary; refactoring that adds a new enum member not yet handled here.","solutions":["Use only the defined HumanInputMode values: NEVER, ALWAYS, AUTO (AUTO is the default)","Validate external input with Enum.IsDefined<HumanInputMode>(value) before constructing the middleware","If a new enum member was added upstream, upgrade AutoGen so the middleware handles it"],"exampleFix":"// before\nvar mode = (HumanInputMode)int.Parse(args[0]); // 7 -> invalid\nvar mw = new HumanInputMiddleware(mode: mode);\n// after\nvar raw = int.Parse(args[0]);\nvar mode = Enum.IsDefined<HumanInputMode>(raw) ? (HumanInputMode)raw : HumanInputMode.AUTO;\nvar mw = new HumanInputMiddleware(mode: mode);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined<HumanInputMode>(mode)) throw new ArgumentOutOfRangeException(nameof(mode));\nvar middleware = new HumanInputMiddleware(mode: mode);","typeGuard":"static bool IsValidMode(HumanInputMode mode) => mode is HumanInputMode.NEVER or HumanInputMode.ALWAYS or HumanInputMode.AUTO;","tryCatchPattern":null,"preventionTips":["Never construct enum values via unchecked casts from external input; validate with Enum.IsDefined","Parse mode strings with Enum.TryParse<HumanInputMode>(s, ignoreCase, out _) plus IsDefined","Default to AUTO when input is unparsable instead of propagating a bad value"],"tags":["autogen","human-in-the-loop","enum","validation","csharp"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}