Class: Legion::Extensions::Agentic::Integration::Labyrinth::Helpers::LabyrinthEngine
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Integration::Labyrinth::Helpers::LabyrinthEngine
- Includes:
- Logging::Helper
- Defined in:
- lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine.rb
Instance Attribute Summary collapse
-
#labyrinths ⇒ Object
readonly
Returns the value of attribute labyrinths.
Instance Method Summary collapse
- #add_node_to(labyrinth_id:, node_type:, content: nil, danger_level: nil, node_id: nil) ⇒ Object
- #backtrack(labyrinth_id:) ⇒ Object
- #check_minotaur(labyrinth_id:) ⇒ Object
- #connect_nodes(labyrinth_id:, from_id:, to_id:, bidirectional: true) ⇒ Object
- #create_labyrinth(name:, domain: nil, labyrinth_id: nil) ⇒ Object
- #delete_labyrinth(labyrinth_id:) ⇒ Object
- #follow_thread(labyrinth_id:) ⇒ Object
-
#initialize ⇒ LabyrinthEngine
constructor
A new instance of LabyrinthEngine.
- #labyrinth_report(labyrinth_id:) ⇒ Object
- #list_labyrinths ⇒ Object
- #move(labyrinth_id:, node_id:) ⇒ Object
Constructor Details
#initialize ⇒ LabyrinthEngine
Returns a new instance of LabyrinthEngine.
14 15 16 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine.rb', line 14 def initialize @labyrinths = {} end |
Instance Attribute Details
#labyrinths ⇒ Object (readonly)
Returns the value of attribute labyrinths.
12 13 14 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine.rb', line 12 def labyrinths @labyrinths end |
Instance Method Details
#add_node_to(labyrinth_id:, node_type:, content: nil, danger_level: nil, node_id: nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine.rb', line 28 def add_node_to(labyrinth_id:, node_type:, content: nil, danger_level: nil, node_id: nil) labyrinth = fetch_labyrinth!(labyrinth_id) id = node_id || SecureRandom.uuid level = (danger_level || default_danger_for(node_type)).clamp(0.0, 1.0) node = Node.new(node_id: id, node_type: node_type, content: content, danger_level: level) labyrinth.add_node(node) log.debug("[cognitive_labyrinth] added node #{id[0..7]} type=#{node_type} to labyrinth #{labyrinth_id[0..7]}") node end |
#backtrack(labyrinth_id:) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine.rb', line 68 def backtrack(labyrinth_id:, **) labyrinth = fetch_labyrinth!(labyrinth_id) node = labyrinth.backtrack! if node log.debug("[cognitive_labyrinth] backtracked to #{node.node_id[0..7]}") { success: true, node_id: node.node_id, node_type: node.node_type, path_length: labyrinth.path_length } else log.debug('[cognitive_labyrinth] backtrack failed: no breadcrumbs') { success: false, reason: :no_breadcrumbs } end end |
#check_minotaur(labyrinth_id:) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine.rb', line 103 def check_minotaur(labyrinth_id:, **) labyrinth = fetch_labyrinth!(labyrinth_id) node = labyrinth.current_node return { encountered: false } unless node if node.node_type == :minotaur_lair log.debug("[cognitive_labyrinth] MINOTAUR ENCOUNTERED at #{node.node_id[0..7]}") { encountered: true, node_id: node.node_id, danger_level: node.danger_level, danger_label: danger_label_for(node.danger_level), misconception: node.content } elsif node.dangerous? { encountered: false, warning: true, danger_level: node.danger_level, danger_label: danger_label_for(node.danger_level) } else { encountered: false } end end |
#connect_nodes(labyrinth_id:, from_id:, to_id:, bidirectional: true) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine.rb', line 39 def connect_nodes(labyrinth_id:, from_id:, to_id:, bidirectional: true) labyrinth = fetch_labyrinth!(labyrinth_id) from_node = fetch_node!(labyrinth, from_id) to_node = fetch_node!(labyrinth, to_id) from_node.connect!(to_id) to_node.connect!(from_id) if bidirectional { connected: true, from: from_id, to: to_id, bidirectional: bidirectional } end |
#create_labyrinth(name:, domain: nil, labyrinth_id: nil) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine.rb', line 18 def create_labyrinth(name:, domain: nil, labyrinth_id: nil) raise ArgumentError, "max labyrinths (#{Constants::MAX_LABYRINTHS}) reached" if @labyrinths.size >= Constants::MAX_LABYRINTHS id = labyrinth_id || SecureRandom.uuid labyrinth = Labyrinth.new(labyrinth_id: id, name: name, domain: domain) @labyrinths[id] = labyrinth log.debug("[cognitive_labyrinth] created labyrinth id=#{id[0..7]} name=#{name}") labyrinth end |
#delete_labyrinth(labyrinth_id:) ⇒ Object
145 146 147 148 149 150 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine.rb', line 145 def delete_labyrinth(labyrinth_id:, **) raise ArgumentError, "labyrinth #{labyrinth_id.inspect} not found" unless @labyrinths.key?(labyrinth_id) @labyrinths.delete(labyrinth_id) { deleted: true, labyrinth_id: labyrinth_id } end |
#follow_thread(labyrinth_id:) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine.rb', line 81 def follow_thread(labyrinth_id:, **) labyrinth = fetch_labyrinth!(labyrinth_id) node = labyrinth.follow_thread if node minotaur_result = check_minotaur(labyrinth_id: labyrinth_id) log.debug("[cognitive_labyrinth] thread followed to #{node.node_id[0..7]}") { success: true, node_id: node.node_id, node_type: node.node_type, content: node.content, danger_level: node.danger_level, solved: labyrinth.solved?, minotaur: minotaur_result } else log.debug('[cognitive_labyrinth] thread exhausted: no unvisited nodes from current') { success: false, reason: :thread_exhausted } end end |
#labyrinth_report(labyrinth_id:) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine.rb', line 129 def labyrinth_report(labyrinth_id:, **) labyrinth = fetch_labyrinth!(labyrinth_id) nodes_by_type = labyrinth.nodes.values.group_by(&:node_type).transform_values(&:count) labyrinth.to_h.merge( nodes_by_type: nodes_by_type, visited_count: labyrinth.nodes.values.count(&:visited), breadcrumb_trail: labyrinth..dup, lost: labyrinth.lost? ) end |
#list_labyrinths ⇒ Object
141 142 143 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine.rb', line 141 def list_labyrinths(**) @labyrinths.values.map(&:to_h) end |
#move(labyrinth_id:, node_id:) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine.rb', line 49 def move(labyrinth_id:, node_id:, **) labyrinth = fetch_labyrinth!(labyrinth_id) node = labyrinth.move_to!(node_id) minotaur_result = check_minotaur(labyrinth_id: labyrinth_id) log.debug("[cognitive_labyrinth] moved to #{node_id[0..7]} type=#{node.node_type}") { success: true, node_id: node.node_id, node_type: node.node_type, content: node.content, danger_level: node.danger_level, solved: labyrinth.solved?, minotaur: minotaur_result, path_length: labyrinth.path_length } end |