Jido.Agent.Strategy.FSM (Jido v2.3.0)

Copy Markdown View Source

An execution-phase finite state machine strategy.

This strategy keeps cmd/2 pure by validating execution-state transitions, emitting %Directive.RunInstruction{} directives, and handling instruction results when the runtime routes them back through cmd/2.

The FSM state is stored in agent.state.__strategy__.

Configuration

Transitions are configured via strategy options:

defmodule MyAgent do
  use Jido.Agent,
    name: "fsm_agent",
    strategy: {Jido.Agent.Strategy.FSM,
      initial_state: "idle",
      transitions: %{
        "idle" => ["processing"],
        "processing" => ["idle", "completed", "failed"],
        "completed" => ["idle"],
        "failed" => ["idle"]
      }
    }
end

Options

  • :initial_state - Initial FSM state (default: "idle")
  • :transitions - Map of valid transitions %{from_state => [to_states]}
  • :auto_transition - Whether to auto-transition back to the initial state after processing (default: true)

Custom transition maps must include the runtime "processing" state. The strategy always enters "processing" before dispatching instructions. If :auto_transition is enabled, "processing" must also be able to return to the configured initial state.

Default Transitions

If no transitions are provided, uses a simple workflow:

%{
  "idle" => ["processing"],
  "processing" => ["idle", "completed", "failed"],
  "completed" => ["idle"],
  "failed" => ["idle"]
}

States

Default execution states:

  • "idle" - Initial state, waiting for work
  • "processing" - Currently processing instructions
  • "completed" - Successfully finished
  • "failed" - Terminated with an error

Usage

agent = MyAgent.new()
{agent, directives} = MyAgent.cmd(agent, SomeAction)

cmd/2 only prepares the work. Use Jido.AgentServer (or another runtime that executes %Directive.RunInstruction{}) to run the instruction batch and feed the result back into the strategy.