{"record":{"id":"2f07ff61e48568cd","repo":"babalae/better-genshin-impact","slug":"maxiterations-string-join","errorCode":null,"errorMessage":"状态机达到最大迭代次数 {maxIterations}，未到达目标状态：{string.Join(\", \", targetStates)}","messagePattern":"状态机达到最大迭代次数 (.+?)，未到达目标状态：(.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/GameTask/Common/StateMachine/StateMachineBase.cs","lineNumber":784,"sourceCode":"            {\n                Logger.LogWarning(\"状态 {State} 无注册处理器，使用未知状态处理器\", CurrentState);\n                await _unknownStateHandler(context);\n            }\n            else if (_unknownStateHandler != null)\n            {\n                await _unknownStateHandler(context);\n            }\n            else\n            {\n                Logger.LogWarning(\"状态 {State} 无注册处理器，跳过\", CurrentState);\n            }\n\n            await Delay(nextLoopInterval, _ct);\n        }\n\n        Logger.LogError(\"状态机达到最大迭代次数 {Max}，目标状态：{Targets}，当前状态：{State}\",\n            maxIterations, string.Join(\", \", targetStates), CurrentState);\n        throw new InvalidOperationException($\"状态机达到最大迭代次数 {maxIterations}，未到达目标状态：{string.Join(\", \", targetStates)}\");\n    }\n\n    /// <summary>\n    /// 运行状态机直到达到单个目标状态\n    /// </summary>\n    protected Task RunStateMachineUntil(TContext context, TState targetState, int maxIterations = 1000)\n    {\n        return RunStateMachineUntil(context, new[] { targetState }, maxIterations);\n    }\n\n    #endregion\n\n    #region 状态检测器注册\n\n    /// <summary>\n    /// 注册状态检测器\n    /// 检测器接收截图区域，返回 true 表示当前处于该状态\n    /// </summary>","sourceCodeStart":766,"sourceCodeEnd":802,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/GameTask/Common/StateMachine/StateMachineBase.cs#L766-L802","documentation":"Thrown at the end of RunStateMachineUntil when the main loop completes all maxIterations (default 1000) iterations without ever reaching one of targetStates. This is the catch-all guard against infinite loops: the state machine kept running but never landed on a goal state. Unlike the retry/transition/Fail throws (which fire mid-loop), this fires after the for-loop exits normally.","triggerScenarios":"Over 1000 iterations the current state never matched any target state, and no other exception (Retry/Transition/Fail) fired first — meaning the machine was cycling through non-target states indefinitely (e.g. oscillating between two intermediate states, or stuck returning Wait forever).","commonSituations":"Target states are unreachable from the current state graph (misconfigured transitions); the machine keeps detecting non-target states and returning Wait; maxIterations too low for a long multi-step flow; detectors misread the UI so the real target state is never recognized as reached.","solutions":["Raise maxIterations if the flow legitimately needs more steps.","Verify the target states are actually reachable via registered transitions/handlers.","Check that the target-state detector would return true when the goal UI is shown (re-capture templates).","Inspect iteration logs to see which states were being detected repeatedly."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Confirm target states are reachable from the current state graph before running\nforeach (var t in targetStates)\n    if (!_stateDetectors.ContainsKey(t)) Logger.LogWarning(\"目标状态 {State} 无检测器，可能无法到达\", t);","typeGuard":null,"tryCatchPattern":"try { await RunStateMachineUntil(context, targets, maxIterations: 2000); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"最大迭代次数\"))\n{ Logger.LogError(ex, \"状态机未到达目标\"); throw; }","preventionTips":["Ensure the state graph can actually reach the targets.","Size maxIterations to the longest legitimate flow.","Verify target-state detectors recognize the goal UI."],"tags":["state-machine","max-iterations","infinite-loop-guard","game-automation"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}