Class: Legion::Extensions::Agentic::Integration::Labyrinth::Helpers::Labyrinth

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(labyrinth_id:, name:, domain: nil) ⇒ Labyrinth

Returns a new instance of Labyrinth.



13
14
15
16
17
18
19
20
21
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 13

def initialize(labyrinth_id:, name:, domain: nil)
  @labyrinth_id   = labyrinth_id
  @name           = name
  @domain         = domain
  @nodes          = {}
  @breadcrumbs    = []
  @current_node_id = nil
  @solved          = false
end

Instance Attribute Details

Returns the value of attribute breadcrumbs.



10
11
12
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 10

def breadcrumbs
  @breadcrumbs
end

#current_node_idObject (readonly)

Returns the value of attribute current_node_id.



10
11
12
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 10

def current_node_id
  @current_node_id
end

#domainObject (readonly)

Returns the value of attribute domain.



10
11
12
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 10

def domain
  @domain
end

#labyrinth_idObject (readonly)

Returns the value of attribute labyrinth_id.



10
11
12
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 10

def labyrinth_id
  @labyrinth_id
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 10

def name
  @name
end

#nodesObject (readonly)

Returns the value of attribute nodes.



10
11
12
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 10

def nodes
  @nodes
end

#solvedObject

Returns the value of attribute solved.



11
12
13
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 11

def solved
  @solved
end

Instance Method Details

#add_node(node) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 23

def add_node(node)
  raise ArgumentError, "nodes must be a #{Node}" unless node.is_a?(Node)
  raise ArgumentError, "max nodes (#{Constants::MAX_NODES}) reached" if @nodes.size >= Constants::MAX_NODES

  @nodes[node.node_id] = node
  @current_node_id ||= node.node_id if node.node_type == :entrance
  node
end

#backtrack!Object



47
48
49
50
51
52
53
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 47

def backtrack!
  return nil if @breadcrumbs.empty?

  target_id = @breadcrumbs.pop
  @current_node_id = target_id
  current_node
end

#complexityObject



103
104
105
106
107
108
109
110
111
112
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 103

def complexity
  return 0.0 if @nodes.empty?

  dead_end_count  = @nodes.values.count(&:dead_end?)
  junction_count  = @nodes.values.count(&:junction?)
  minotaur_count  = @nodes.values.count { |n| n.node_type == :minotaur_lair }

  raw = ((dead_end_count * 0.3) + (junction_count * 0.2) + (minotaur_count * 0.5)) / @nodes.size.to_f
  raw.clamp(0.0, 1.0).round(10)
end

#complexity_labelObject



114
115
116
117
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 114

def complexity_label
  score = complexity
  Constants::COMPLEXITY_LABELS.find { |range, _| range.cover?(score) }&.last || :labyrinthine
end

#current_nodeObject



99
100
101
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 99

def current_node
  @nodes[@current_node_id]
end

#drop_breadcrumbObject



55
56
57
58
59
60
61
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 55

def drop_breadcrumb
  return unless @current_node_id

  current = @nodes[@current_node_id]
  current.visited = true if current
  @breadcrumbs << @current_node_id if @breadcrumbs.last != @current_node_id
end

#follow_threadObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 63

def follow_thread
  return nil if current_node.nil?

  unvisited = current_node.connections.find do |conn_id|
    node = @nodes[conn_id]
    node && !node.visited
  end

  return nil unless unvisited

  move_to!(unvisited)
end

#lost?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 80

def lost?
  return false if @current_node_id.nil?
  return false if solved?

  current = current_node
  return true if current.nil?

  unvisited_exits = current.connections.any? do |conn_id|
    node = @nodes[conn_id]
    node && !node.visited
  end

  !unvisited_exits && !current.dead_end? && @breadcrumbs.empty?
end

#move_to!(node_id) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 32

def move_to!(node_id)
  node = @nodes.fetch(node_id) { raise ArgumentError, "node #{node_id.inspect} not found in labyrinth" }

  current = current_node
  unless current.nil? || current.connections.include?(node_id)
    raise ArgumentError, "cannot move to #{node_id.inspect}: not connected from current node #{@current_node_id.inspect}"
  end

  drop_breadcrumb
  node.visited = true
  @current_node_id = node_id
  @solved = true if node.node_type == :exit
  node
end

#path_lengthObject



95
96
97
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 95

def path_length
  @breadcrumbs.size
end

#solved?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 76

def solved?
  @solved
end

#to_hObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb', line 119

def to_h
  {
    labyrinth_id:     @labyrinth_id,
    name:             @name,
    domain:           @domain,
    node_count:       @nodes.size,
    current_node_id:  @current_node_id,
    path_length:      path_length,
    solved:           @solved,
    complexity:       complexity,
    complexity_label: complexity_label
  }
end