Module: ComposableAgents::Mixins::Resumable

Defined in:
lib/composable_agents/mixins/resumable.rb

Overview

Mixin adding resumable step capabilities to agents. An agent prepending this mixin can use the following:

  • A new constructor named parameter run_id that identifies the run that can be resumable.
  • A step re-entrant method that defines a part of the agent's processing whose input/output is persisted and that can be skipped if it was previously executed.
  • An agent_step method that calls a sub-agent with the artifacts and also tracks the state of this agent.
    • Any agent that implements the methods export_state and import_state will benefit from its state's serialization automatically.
  • An instance variable @artifacts that stores artifacts (initialized with input ones) that are JSON serialized by steps. Artifacts used with this mixin, and states returned by used agents should be JSON-serializable. This mixin uses the following methods from the agent:
  • #export_state -> Object Optional method returning the current JSON-serializable state of the agent.
  • #import_state(state) Optional method that sets the agent state from a JSON-serializable object.

Internal collapse

Public API collapse

Internal collapse

Instance Attribute Details

#steps_runArray<Array<Hash{Symbol => Object}>> (readonly)

Hierarchies of the steps executed during each run of this agent.

Returns:

  • (Array<Array<Hash{Symbol => Object}>>)

    ] List of per-run step hierarchies. Each element of this array corresponds to a run, and is the ordered list of top-level step nodes of that run. Each step node has those properties:

    • step_name [Symbol] The step name.

    • index [Array] Position of the step in the hierarchy of recursive step calls. It is the succession of indices from the root of the hierarchy to this step node, each index being the position of the node among its sibling nodes (children of the same parent). For example here are the values of the index property of the step nodes if we have this code:

      index == [0]

      step(:a) do

      index == [0, 0]

      step(:a1) do # index == [0, 0, 0] end

      index == [0, 1]

      step(:a2) do # index == [0, 1, 0] step(:a21) do # index == [0, 1, 0, 0] end # index == [0, 1, 1] step(:a22) do # index == [0, 1, 1, 0] end # index == [0, 1, 2] end

      index == [0, 2]

      end

      index == [1]

      step(:b) do

      index == [1, 0]

      end

    • status [Symbol] The status of the step:

      • `started``: The step has started its execution.
      • `cached``: The step was skipped because of a previous execution.
      • `executed``: The step has completed its execution.
      • `error``: The step has raised an exception during its execution.
    • created_at [Time] Timestamp of the creation of this step node.

    • extra_input_artifacts [Hash] Additional artifacts given as input to this step.

    • agent [Agent, nil] The agent that is invoked in this step, or nil if none.

    • error [String, nil] The message of the exception raised during the step execution. This property is only present when the status is error.

    • children [Array] Sub-steps that are invoked from this step.



90
91
92
# File 'lib/composable_agents/mixins/resumable.rb', line 90

def steps_run
  @steps_run
end

Instance Method Details

#initialize(*args, run_id: nil, **kwargs) ⇒ Object

Constructor

Parameters:

  • run_id (String, nil) (defaults to: nil)

    ID identifying this run to reuse previously executed steps, or nil if there is no resumability needed



24
25
26
27
28
# File 'lib/composable_agents/mixins/resumable.rb', line 24

def initialize(*args, run_id: nil, **kwargs)
  super(*args, **kwargs)
  @steps_run = []
  @run_id = run_id
end

#run(**input_artifacts) ⇒ Hash{Symbol => Object}

Execute the agent to generate some output artifacts based on some input artifacts.

Parameters:

  • input_artifacts (Hash{Symbol => Object})

    The input artifacts content, per artifact name

Returns:

  • (Hash{Symbol => Object})

    The output artifacts returned by the Proc



36
37
38
39
40
41
42
43
# File 'lib/composable_agents/mixins/resumable.rb', line 36

def run(**input_artifacts)
  # The artifacts store, JSON serializable
  @artifacts = input_artifacts.dup
  # List of the steps_run hierarchies, one per run. Each run appends its own new list.
  @steps_run << []
  @current_step_node = nil
  super
end