Class: AgentRuntime::State

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_runtime/state.rb

Overview

Explicit, serializable state management with deep merge support.

This class manages the agent’s state throughout execution. State is stored as a hash and can be snapshotted for read-only access. Updates are applied using deep merge to preserve nested structures.

Examples:

Initialize with initial data

state = State.new({ step: 1, context: { user: "Alice" } })

Take a snapshot

snapshot = state.snapshot
# => { step: 1, context: { user: "Alice" } }

Apply updates

state.apply!({ step: 2, context: { task: "search" } })
state.snapshot
# => { step: 2, context: { user: "Alice", task: "search" } }

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ State

Initialize a new State instance.

Parameters:

  • data (Hash) (defaults to: {})

    Initial state data (default: {})



25
26
27
# File 'lib/agent_runtime/state.rb', line 25

def initialize(data = {})
  @data = data
end

Instance Method Details

#apply!(result) ⇒ void

This method returns an undefined value.

Apply a result hash to the state using deep merge.

Merges the result hash into the current state, preserving nested structures. If result is not a hash, this method does nothing.

Examples:

state = State.new({ a: 1, nested: { x: 10 } })
state.apply!({ b: 2, nested: { y: 20 } })
state.snapshot
# => { a: 1, b: 2, nested: { x: 10, y: 20 } }

Parameters:

  • result (Hash, Object)

    The result to merge into state (must be a Hash to apply)



56
57
58
59
60
# File 'lib/agent_runtime/state.rb', line 56

def apply!(result)
  return unless result.is_a?(Hash)

  deep_merge!(@data, result)
end

#snapshotHash

Create a snapshot of the current state.

Returns a shallow copy of the state data. Modifications to the snapshot will not affect the original state.

Examples:

snapshot = state.snapshot
snapshot[:new_key] = "value"  # Does not modify state

Returns:

  • (Hash)

    A copy of the current state data



39
40
41
# File 'lib/agent_runtime/state.rb', line 39

def snapshot
  @data.dup
end