babalae/better-genshin-impact · error · InvalidOperationException

状态机达到最大迭代次数 {maxIterations},未到达目标状态:{string.Join(", ", targe

Error message

状态机达到最大迭代次数 {maxIterations},未到达目标状态:{string.Join(", ", targetStates)}

What it means

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.

Source

Thrown at BetterGenshinImpact/GameTask/Common/StateMachine/StateMachineBase.cs:784

            {
                Logger.LogWarning("状态 {State} 无注册处理器,使用未知状态处理器", CurrentState);
                await _unknownStateHandler(context);
            }
            else if (_unknownStateHandler != null)
            {
                await _unknownStateHandler(context);
            }
            else
            {
                Logger.LogWarning("状态 {State} 无注册处理器,跳过", CurrentState);
            }

            await Delay(nextLoopInterval, _ct);
        }

        Logger.LogError("状态机达到最大迭代次数 {Max},目标状态:{Targets},当前状态:{State}",
            maxIterations, string.Join(", ", targetStates), CurrentState);
        throw new InvalidOperationException($"状态机达到最大迭代次数 {maxIterations},未到达目标状态:{string.Join(", ", targetStates)}");
    }

    /// <summary>
    /// 运行状态机直到达到单个目标状态
    /// </summary>
    protected Task RunStateMachineUntil(TContext context, TState targetState, int maxIterations = 1000)
    {
        return RunStateMachineUntil(context, new[] { targetState }, maxIterations);
    }

    #endregion

    #region 状态检测器注册

    /// <summary>
    /// 注册状态检测器
    /// 检测器接收截图区域,返回 true 表示当前处于该状态
    /// </summary>

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Raise maxIterations if the flow legitimately needs more steps.
  2. Verify the target states are actually reachable via registered transitions/handlers.
  3. Check that the target-state detector would return true when the goal UI is shown (re-capture templates).
  4. Inspect iteration logs to see which states were being detected repeatedly.
Defensive patterns

Strategy: validation

Validate before calling

// Confirm target states are reachable from the current state graph before running
foreach (var t in targetStates)
    if (!_stateDetectors.ContainsKey(t)) Logger.LogWarning("目标状态 {State} 无检测器,可能无法到达", t);

Try / catch

try { await RunStateMachineUntil(context, targets, maxIterations: 2000); }
catch (InvalidOperationException ex) when (ex.Message.Contains("最大迭代次数"))
{ Logger.LogError(ex, "状态机未到达目标"); throw; }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/2f07ff61e48568cd. Report an issue: GitHub.