Class: Legion::Extensions::Agentic::Executive::Planning::Helpers::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/executive/planning/helpers/plan.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(goal:, steps: [], priority: :medium, contingencies: {}, parent_plan_id: nil, description: nil) ⇒ Plan

Returns a new instance of Plan.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 16

def initialize(goal:, steps: [], priority: :medium, contingencies: {}, parent_plan_id: nil, description: nil, **)
  @id             = SecureRandom.uuid
  @goal           = goal
  @description    = description
  @priority       = priority
  @status         = :forming
  @steps          = steps.dup
  @contingencies  = contingencies.dup
  @parent_plan_id = parent_plan_id
  @created_at     = Time.now.utc
  @updated_at     = Time.now.utc
  @replan_count   = 0
end

Instance Attribute Details

#contingenciesObject (readonly)

Returns the value of attribute contingencies.



12
13
14
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 12

def contingencies
  @contingencies
end

#created_atObject (readonly)

Returns the value of attribute created_at.



12
13
14
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 12

def created_at
  @created_at
end

#descriptionObject (readonly)

Returns the value of attribute description.



12
13
14
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 12

def description
  @description
end

#goalObject (readonly)

Returns the value of attribute goal.



12
13
14
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 12

def goal
  @goal
end

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 12

def id
  @id
end

#parent_plan_idObject (readonly)

Returns the value of attribute parent_plan_id.



12
13
14
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 12

def parent_plan_id
  @parent_plan_id
end

#priorityObject (readonly)

Returns the value of attribute priority.



12
13
14
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 12

def priority
  @priority
end

#replan_countObject (readonly)

Returns the value of attribute replan_count.



12
13
14
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 12

def replan_count
  @replan_count
end

#statusObject

Returns the value of attribute status.



14
15
16
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 14

def status
  @status
end

#stepsObject (readonly)

Returns the value of attribute steps.



12
13
14
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 12

def steps
  @steps
end

#updated_atObject

Returns the value of attribute updated_at.



14
15
16
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 14

def updated_at
  @updated_at
end

Instance Method Details

#active_stepObject



37
38
39
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 37

def active_step
  @steps.find { |s| s.status == :active }
end

#advance!(step_id, result: nil) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 45

def advance!(step_id, result: nil)
  step = @steps.find { |s| s.id == step_id }
  return nil unless step

  step.complete!(result: result)
  @updated_at = Time.now.utc
  @status = :completed if progress >= Constants::COMPLETION_THRESHOLD
  step
end

#complete?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 64

def complete?
  @status == :completed || progress >= Constants::COMPLETION_THRESHOLD
end

#completed_step_idsObject



41
42
43
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 41

def completed_step_ids
  @steps.select { |s| %i[completed skipped].include?(s.status) }.map(&:id)
end

#fail_step!(step_id, reason: nil) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 55

def fail_step!(step_id, reason: nil)
  step = @steps.find { |s| s.id == step_id }
  return nil unless step

  step.fail!(reason: reason)
  @updated_at = Time.now.utc
  step
end

#failed?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 68

def failed?
  @status == :failed
end

#increment_replan!Object



76
77
78
79
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 76

def increment_replan!
  @replan_count += 1
  @updated_at = Time.now.utc
end

#progressObject



30
31
32
33
34
35
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 30

def progress
  return 0.0 if @steps.empty?

  done = @steps.count { |s| %i[completed skipped].include?(s.status) }
  done.to_f / @steps.size
end

#replace_remaining_steps!(new_steps) ⇒ Object



81
82
83
84
85
86
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 81

def replace_remaining_steps!(new_steps)
  pending = @steps.reject { |s| %i[completed skipped failed].include?(s.status) }
  pending.each { |s| @steps.delete(s) }
  new_steps.each { |s| @steps << s }
  @updated_at = Time.now.utc
end

#stale?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 72

def stale?
  (Time.now.utc - @updated_at) > Constants::STALE_PLAN_THRESHOLD
end

#to_hObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/legion/extensions/agentic/executive/planning/helpers/plan.rb', line 88

def to_h
  {
    id:             @id,
    goal:           @goal,
    description:    @description,
    priority:       @priority,
    status:         @status,
    progress:       progress.round(4),
    steps:          @steps.map(&:to_h),
    contingencies:  @contingencies,
    parent_plan_id: @parent_plan_id,
    replan_count:   @replan_count,
    created_at:     @created_at,
    updated_at:     @updated_at
  }
end