Class: Legion::Extensions::Agentic::Integration::GlobalWorkspace::Helpers::Workspace

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb

Constant Summary

Constants included from Constants

Constants::BROADCAST_TTL, Constants::COMPETITION_THRESHOLD, Constants::DOMINANCE_MARGIN, Constants::MAX_BROADCAST_HISTORY, Constants::MAX_COALITION_SIZE, Constants::MAX_COMPETITORS, Constants::MAX_SUBSCRIBERS, Constants::MAX_URGENCY, Constants::SALIENCE_DECAY, Constants::SALIENCE_LABELS, Constants::URGENCY_BOOST, Constants::UTILIZATION_ALPHA, Constants::WORKSPACE_STATE_LABELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWorkspace

Returns a new instance of Workspace.



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

def initialize
  @competitors       = []
  @broadcast_history = []
  @subscribers       = {}   # id => { name:, domains: [] }
  @current_broadcast = nil
  @utilization       = 0.0  # EMA of workspace usage
  @competition_count = 0
end

Instance Attribute Details

#broadcast_historyObject (readonly)

Returns the value of attribute broadcast_history.



12
13
14
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 12

def broadcast_history
  @broadcast_history
end

#competitorsObject (readonly)

Returns the value of attribute competitors.



12
13
14
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 12

def competitors
  @competitors
end

#current_broadcastObject (readonly)

Returns the value of attribute current_broadcast.



12
13
14
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 12

def current_broadcast
  @current_broadcast
end

#subscribersObject (readonly)

Returns the value of attribute subscribers.



12
13
14
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 12

def subscribers
  @subscribers
end

#utilizationObject (readonly)

Returns the value of attribute utilization.



12
13
14
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 12

def utilization
  @utilization
end

Instance Method Details

#acknowledge(subscriber_id:) ⇒ Object



96
97
98
99
100
101
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 96

def acknowledge(subscriber_id:)
  return false unless @current_broadcast && !@current_broadcast.expired?

  @current_broadcast.acknowledge(subscriber_id)
  true
end

#competeObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 53

def compete
  @competition_count += 1
  expire_current_broadcast
  prune_weak_competitors

  return nil if @competitors.empty?

  sorted = @competitors.sort_by { |c| -c.effective_salience }
  winner = sorted.first

  if sorted.size > 1
    runner_up = sorted[1]
    unless (winner.effective_salience - runner_up.effective_salience) >= DOMINANCE_MARGIN
      boost_losers
      update_utilization(busy: true)
      return nil
    end
  end

  @competitors.delete(winner)
  broadcast = create_broadcast(winner)
  @current_broadcast = broadcast

  boost_losers
  update_utilization(busy: true)

  broadcast
end

#competitor_countObject



121
122
123
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 121

def competitor_count
  @competitors.size
end

#conscious?(content) ⇒ Boolean

— Query —

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 84

def conscious?(content)
  return false unless @current_broadcast && !@current_broadcast.expired?

  @current_broadcast.content == content
end

#current_contentObject



90
91
92
93
94
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 90

def current_content
  return nil unless @current_broadcast && !@current_broadcast.expired?

  @current_broadcast.to_h
end

#register_subscriber(id:, name:, domains: []) ⇒ Object

— Subscriber Management —



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

def register_subscriber(id:, name:, domains: [])
  return false if @subscribers.size >= MAX_SUBSCRIBERS && !@subscribers.key?(id)

  @subscribers[id] = { name: name, domains: Array(domains), registered_at: Time.now.utc }
  true
end

#stateObject

— Workspace State —



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 105

def state
  if @current_broadcast && !@current_broadcast.expired?
    :broadcasting
  elsif @competitors.size > 1
    :contention
  elsif @utilization > 0.7
    :saturated
  else
    :idle
  end
end

#submit(content:, source:, domain:, salience:, coalition: []) ⇒ Object

— Competition —



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 38

def submit(content:, source:, domain:, salience:, coalition: [])
  return nil if salience.to_f < COMPETITION_THRESHOLD

  @competitors.shift while @competitors.size >= MAX_COMPETITORS
  competitor = Competitor.new(
    content:   content,
    source:    source,
    domain:    domain,
    salience:  salience,
    coalition: coalition
  )
  @competitors << competitor
  competitor
end

#subscriber_countObject



117
118
119
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 117

def subscriber_count
  @subscribers.size
end

#tickObject

— Tick / Maintenance —



127
128
129
130
131
132
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 127

def tick
  expire_current_broadcast
  decay_competitors
  prune_weak_competitors
  update_utilization(busy: !@competitors.empty? || (@current_broadcast && !@current_broadcast.expired?))
end

#to_hObject



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 134

def to_h
  {
    state:             state,
    state_label:       WORKSPACE_STATE_LABELS[state],
    subscribers:       @subscribers.size,
    competitors:       @competitors.size,
    broadcast_history: @broadcast_history.size,
    utilization:       @utilization.round(4),
    competitions:      @competition_count,
    current_broadcast: @current_broadcast&.expired? == false ? @current_broadcast.to_h : nil
  }
end

#unregister_subscriber(id:) ⇒ Object



32
33
34
# File 'lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb', line 32

def unregister_subscriber(id:)
  !@subscribers.delete(id).nil?
end