Class: Legion::Extensions::Agentic::Inference::Horizon::Helpers::HorizonEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_horizon: Constants::DEFAULT_HORIZON) ⇒ HorizonEngine

Returns a new instance of HorizonEngine.



12
13
14
15
16
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 12

def initialize(initial_horizon: Constants::DEFAULT_HORIZON)
  @projections     = []
  @current_horizon = initial_horizon.clamp(Constants::MIN_HORIZON, Constants::MAX_HORIZON)
  @stress_level    = 0.0
end

Instance Attribute Details

#current_horizonObject (readonly)

Returns the value of attribute current_horizon.



10
11
12
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 10

def current_horizon
  @current_horizon
end

#projectionsObject (readonly)

Returns the value of attribute projections.



10
11
12
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 10

def projections
  @projections
end

#stress_levelObject (readonly)

Returns the value of attribute stress_level.



10
11
12
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 10

def stress_level
  @stress_level
end

Instance Method Details

#add_projection(description:, domain: :general, horizon_distance: nil, confidence: 1.0) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 18

def add_projection(description:, domain: :general, horizon_distance: nil, confidence: 1.0)
  dist = horizon_distance || @current_horizon
  proj = Projection.new(
    description:      description,
    domain:           domain,
    horizon_distance: dist,
    confidence:       confidence
  )
  @projections << proj
  @projections = @projections.last(Constants::MAX_PROJECTIONS) if @projections.size > Constants::MAX_PROJECTIONS
  proj
end

#apply_stress!(level) ⇒ Object



41
42
43
44
45
46
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 41

def apply_stress!(level)
  @stress_level = level.clamp(0.0, 1.0)
  @current_horizon = (@current_horizon - (@stress_level * Constants::STRESS_CONTRACTION))
                     .clamp(Constants::MIN_HORIZON, Constants::MAX_HORIZON)
  @current_horizon
end

#beyond_horizon_projectionsObject



62
63
64
65
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 62

def beyond_horizon_projections
  eh = effective_horizon
  @projections.select { |p| p.horizon_distance > eh }
end

#construal_labelObject



75
76
77
78
79
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 75

def construal_label
  dist = effective_horizon
  entry = Constants::CONSTRUAL_LABELS.find { |e| e[:range].cover?(dist) }
  entry ? entry[:label] : :balanced
end

#contract_horizon!(amount: Constants::HORIZON_CONTRACT) ⇒ Object



36
37
38
39
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 36

def contract_horizon!(amount: Constants::HORIZON_CONTRACT)
  @current_horizon = (@current_horizon - amount).clamp(Constants::MIN_HORIZON, Constants::MAX_HORIZON)
  @current_horizon
end

#effective_horizonObject



52
53
54
55
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 52

def effective_horizon
  (@current_horizon - (@stress_level * Constants::STRESS_CONTRACTION))
    .clamp(Constants::MIN_HORIZON, Constants::MAX_HORIZON)
end

#expand_horizon!(amount: Constants::HORIZON_EXPAND) ⇒ Object



31
32
33
34
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 31

def expand_horizon!(amount: Constants::HORIZON_EXPAND)
  @current_horizon = (@current_horizon + amount).clamp(Constants::MIN_HORIZON, Constants::MAX_HORIZON)
  @current_horizon
end

#farthest_projections(num: 5) ⇒ Object



71
72
73
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 71

def farthest_projections(num: 5)
  @projections.sort_by(&:horizon_distance).last(num).reverse
end

#horizon_reportObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 81

def horizon_report
  {
    current_horizon:   @current_horizon.round(10),
    stress_level:      @stress_level.round(10),
    effective_horizon: effective_horizon.round(10),
    construal_label:   construal_label,
    horizon_level:     horizon_level,
    total_projections: @projections.size,
    within_horizon:    projections_within_horizon.size,
    beyond_horizon:    beyond_horizon_projections.size
  }
end

#nearest_projections(num: 5) ⇒ Object



67
68
69
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 67

def nearest_projections(num: 5)
  @projections.sort_by(&:horizon_distance).first(num)
end

#projections_within_horizonObject



57
58
59
60
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 57

def projections_within_horizon
  eh = effective_horizon
  @projections.select { |p| p.horizon_distance <= eh }
end

#relieve_stress!(amount: 0.1) ⇒ Object



48
49
50
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 48

def relieve_stress!(amount: 0.1)
  @stress_level = (@stress_level - amount).clamp(0.0, 1.0)
end

#to_hObject



94
95
96
# File 'lib/legion/extensions/agentic/inference/horizon/helpers/horizon_engine.rb', line 94

def to_h
  horizon_report.merge(projections: @projections.map(&:to_h))
end