lex-agentic-integration 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +12 -0
- data/Gemfile +5 -0
- data/LICENSE +21 -0
- data/README.md +13 -0
- data/lex-agentic-integration.gemspec +30 -0
- data/lib/legion/extensions/agentic/integration/boundary/client.rb +15 -0
- data/lib/legion/extensions/agentic/integration/boundary/helpers/boundary.rb +90 -0
- data/lib/legion/extensions/agentic/integration/boundary/helpers/boundary_engine.rb +123 -0
- data/lib/legion/extensions/agentic/integration/boundary/helpers/constants.rb +44 -0
- data/lib/legion/extensions/agentic/integration/boundary/runners/cognitive_boundary.rb +100 -0
- data/lib/legion/extensions/agentic/integration/boundary/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/boundary.rb +19 -0
- data/lib/legion/extensions/agentic/integration/context/client.rb +26 -0
- data/lib/legion/extensions/agentic/integration/context/helpers/constants.rb +34 -0
- data/lib/legion/extensions/agentic/integration/context/helpers/context_manager.rb +150 -0
- data/lib/legion/extensions/agentic/integration/context/helpers/frame.rb +99 -0
- data/lib/legion/extensions/agentic/integration/context/runners/context.rb +94 -0
- data/lib/legion/extensions/agentic/integration/context/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/context.rb +19 -0
- data/lib/legion/extensions/agentic/integration/distributed_cognition/client.rb +19 -0
- data/lib/legion/extensions/agentic/integration/distributed_cognition/helpers/constants.rb +51 -0
- data/lib/legion/extensions/agentic/integration/distributed_cognition/helpers/distribution_engine.rb +159 -0
- data/lib/legion/extensions/agentic/integration/distributed_cognition/helpers/participant.rb +100 -0
- data/lib/legion/extensions/agentic/integration/distributed_cognition/runners/distributed_cognition.rb +107 -0
- data/lib/legion/extensions/agentic/integration/distributed_cognition/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/distributed_cognition.rb +19 -0
- data/lib/legion/extensions/agentic/integration/gestalt/client.rb +21 -0
- data/lib/legion/extensions/agentic/integration/gestalt/helpers/constants.rb +51 -0
- data/lib/legion/extensions/agentic/integration/gestalt/helpers/pattern.rb +90 -0
- data/lib/legion/extensions/agentic/integration/gestalt/helpers/pattern_store.rb +123 -0
- data/lib/legion/extensions/agentic/integration/gestalt/runners/gestalt.rb +82 -0
- data/lib/legion/extensions/agentic/integration/gestalt/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/gestalt.rb +19 -0
- data/lib/legion/extensions/agentic/integration/global_workspace/actors/competition.rb +45 -0
- data/lib/legion/extensions/agentic/integration/global_workspace/client.rb +29 -0
- data/lib/legion/extensions/agentic/integration/global_workspace/helpers/broadcast.rb +62 -0
- data/lib/legion/extensions/agentic/integration/global_workspace/helpers/competitor.rb +59 -0
- data/lib/legion/extensions/agentic/integration/global_workspace/helpers/constants.rb +65 -0
- data/lib/legion/extensions/agentic/integration/global_workspace/helpers/workspace.rb +188 -0
- data/lib/legion/extensions/agentic/integration/global_workspace/runners/global_workspace.rb +104 -0
- data/lib/legion/extensions/agentic/integration/global_workspace/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/global_workspace.rb +20 -0
- data/lib/legion/extensions/agentic/integration/integration/client.rb +19 -0
- data/lib/legion/extensions/agentic/integration/integration/helpers/constants.rb +62 -0
- data/lib/legion/extensions/agentic/integration/integration/helpers/integrated_representation.rb +106 -0
- data/lib/legion/extensions/agentic/integration/integration/helpers/integration_engine.rb +163 -0
- data/lib/legion/extensions/agentic/integration/integration/helpers/modal_signal.rb +62 -0
- data/lib/legion/extensions/agentic/integration/integration/runners/cognitive_integration.rb +105 -0
- data/lib/legion/extensions/agentic/integration/integration/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/integration.rb +20 -0
- data/lib/legion/extensions/agentic/integration/labyrinth/actors/thread_walker.rb +45 -0
- data/lib/legion/extensions/agentic/integration/labyrinth/client.rb +23 -0
- data/lib/legion/extensions/agentic/integration/labyrinth/helpers/constants.rb +39 -0
- data/lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth.rb +138 -0
- data/lib/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine.rb +177 -0
- data/lib/legion/extensions/agentic/integration/labyrinth/helpers/node.rb +64 -0
- data/lib/legion/extensions/agentic/integration/labyrinth/runners/cognitive_labyrinth.rb +128 -0
- data/lib/legion/extensions/agentic/integration/labyrinth/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/labyrinth.rb +22 -0
- data/lib/legion/extensions/agentic/integration/map/actors/decay.rb +45 -0
- data/lib/legion/extensions/agentic/integration/map/client.rb +29 -0
- data/lib/legion/extensions/agentic/integration/map/helpers/cognitive_map_store.rb +179 -0
- data/lib/legion/extensions/agentic/integration/map/helpers/constants.rb +71 -0
- data/lib/legion/extensions/agentic/integration/map/helpers/graph_traversal.rb +124 -0
- data/lib/legion/extensions/agentic/integration/map/helpers/location.rb +71 -0
- data/lib/legion/extensions/agentic/integration/map/runners/cognitive_map.rb +95 -0
- data/lib/legion/extensions/agentic/integration/map/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/map.rb +20 -0
- data/lib/legion/extensions/agentic/integration/mosaic/client.rb +15 -0
- data/lib/legion/extensions/agentic/integration/mosaic/helpers/constants.rb +55 -0
- data/lib/legion/extensions/agentic/integration/mosaic/helpers/mosaic.rb +111 -0
- data/lib/legion/extensions/agentic/integration/mosaic/helpers/mosaic_engine.rb +124 -0
- data/lib/legion/extensions/agentic/integration/mosaic/helpers/tessera.rb +86 -0
- data/lib/legion/extensions/agentic/integration/mosaic/runners/cognitive_mosaic.rb +76 -0
- data/lib/legion/extensions/agentic/integration/mosaic/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/mosaic.rb +22 -0
- data/lib/legion/extensions/agentic/integration/mycelium/client.rb +19 -0
- data/lib/legion/extensions/agentic/integration/mycelium/helpers/constants.rb +52 -0
- data/lib/legion/extensions/agentic/integration/mycelium/helpers/fruiting_body.rb +50 -0
- data/lib/legion/extensions/agentic/integration/mycelium/helpers/hypha.rb +76 -0
- data/lib/legion/extensions/agentic/integration/mycelium/helpers/mycelial_node.rb +72 -0
- data/lib/legion/extensions/agentic/integration/mycelium/helpers/mycelium_engine.rb +151 -0
- data/lib/legion/extensions/agentic/integration/mycelium/runners/cognitive_mycelium.rb +73 -0
- data/lib/legion/extensions/agentic/integration/mycelium/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/mycelium.rb +23 -0
- data/lib/legion/extensions/agentic/integration/phenomenal_binding/client.rb +25 -0
- data/lib/legion/extensions/agentic/integration/phenomenal_binding/helpers/binding_engine.rb +156 -0
- data/lib/legion/extensions/agentic/integration/phenomenal_binding/helpers/binding_unit.rb +72 -0
- data/lib/legion/extensions/agentic/integration/phenomenal_binding/helpers/constants.rb +35 -0
- data/lib/legion/extensions/agentic/integration/phenomenal_binding/helpers/stream.rb +51 -0
- data/lib/legion/extensions/agentic/integration/phenomenal_binding/runners/phenomenal_binding.rb +110 -0
- data/lib/legion/extensions/agentic/integration/phenomenal_binding/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/phenomenal_binding.rb +20 -0
- data/lib/legion/extensions/agentic/integration/qualia/client.rb +19 -0
- data/lib/legion/extensions/agentic/integration/qualia/helpers/constants.rb +74 -0
- data/lib/legion/extensions/agentic/integration/qualia/helpers/quale.rb +103 -0
- data/lib/legion/extensions/agentic/integration/qualia/helpers/qualia_engine.rb +142 -0
- data/lib/legion/extensions/agentic/integration/qualia/runners/qualia.rb +67 -0
- data/lib/legion/extensions/agentic/integration/qualia/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/qualia.rb +19 -0
- data/lib/legion/extensions/agentic/integration/situation_model/client.rb +28 -0
- data/lib/legion/extensions/agentic/integration/situation_model/helpers/client.rb +23 -0
- data/lib/legion/extensions/agentic/integration/situation_model/helpers/constants.rb +40 -0
- data/lib/legion/extensions/agentic/integration/situation_model/helpers/situation_engine.rb +73 -0
- data/lib/legion/extensions/agentic/integration/situation_model/helpers/situation_event.rb +56 -0
- data/lib/legion/extensions/agentic/integration/situation_model/helpers/situation_model.rb +90 -0
- data/lib/legion/extensions/agentic/integration/situation_model/runners/situation_model.rb +99 -0
- data/lib/legion/extensions/agentic/integration/situation_model/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/situation_model.rb +21 -0
- data/lib/legion/extensions/agentic/integration/synthesis/client.rb +29 -0
- data/lib/legion/extensions/agentic/integration/synthesis/helpers/constants.rb +41 -0
- data/lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis.rb +63 -0
- data/lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine.rb +213 -0
- data/lib/legion/extensions/agentic/integration/synthesis/helpers/synthesis_stream.rb +67 -0
- data/lib/legion/extensions/agentic/integration/synthesis/runners/cognitive_synthesis.rb +82 -0
- data/lib/legion/extensions/agentic/integration/synthesis/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/synthesis.rb +20 -0
- data/lib/legion/extensions/agentic/integration/tapestry/client.rb +15 -0
- data/lib/legion/extensions/agentic/integration/tapestry/helpers/constants.rb +46 -0
- data/lib/legion/extensions/agentic/integration/tapestry/helpers/loom_engine.rb +125 -0
- data/lib/legion/extensions/agentic/integration/tapestry/helpers/tapestry.rb +144 -0
- data/lib/legion/extensions/agentic/integration/tapestry/helpers/thread.rb +112 -0
- data/lib/legion/extensions/agentic/integration/tapestry/runners/cognitive_tapestry.rb +81 -0
- data/lib/legion/extensions/agentic/integration/tapestry/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/tapestry.rb +22 -0
- data/lib/legion/extensions/agentic/integration/tessellation/client.rb +19 -0
- data/lib/legion/extensions/agentic/integration/tessellation/helpers/constants.rb +72 -0
- data/lib/legion/extensions/agentic/integration/tessellation/helpers/mosaic.rb +99 -0
- data/lib/legion/extensions/agentic/integration/tessellation/helpers/tessellation_engine.rb +141 -0
- data/lib/legion/extensions/agentic/integration/tessellation/helpers/tile.rb +76 -0
- data/lib/legion/extensions/agentic/integration/tessellation/runners/cognitive_tessellation.rb +58 -0
- data/lib/legion/extensions/agentic/integration/tessellation/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/tessellation.rb +22 -0
- data/lib/legion/extensions/agentic/integration/version.rb +11 -0
- data/lib/legion/extensions/agentic/integration/zeitgeist/helpers/client.rb +17 -0
- data/lib/legion/extensions/agentic/integration/zeitgeist/helpers/cognitive_signal.rb +38 -0
- data/lib/legion/extensions/agentic/integration/zeitgeist/helpers/constants.rb +64 -0
- data/lib/legion/extensions/agentic/integration/zeitgeist/helpers/trend_window.rb +80 -0
- data/lib/legion/extensions/agentic/integration/zeitgeist/helpers/zeitgeist_engine.rb +157 -0
- data/lib/legion/extensions/agentic/integration/zeitgeist/runners/cognitive_zeitgeist.rb +99 -0
- data/lib/legion/extensions/agentic/integration/zeitgeist/version.rb +13 -0
- data/lib/legion/extensions/agentic/integration/zeitgeist.rb +22 -0
- data/lib/legion/extensions/agentic/integration.rb +34 -0
- data/spec/legion/extensions/agentic/integration/boundary/helpers/boundary_engine_spec.rb +165 -0
- data/spec/legion/extensions/agentic/integration/boundary/helpers/boundary_spec.rb +168 -0
- data/spec/legion/extensions/agentic/integration/boundary/helpers/constants_spec.rb +65 -0
- data/spec/legion/extensions/agentic/integration/boundary/runners/cognitive_boundary_spec.rb +111 -0
- data/spec/legion/extensions/agentic/integration/context/client_spec.rb +58 -0
- data/spec/legion/extensions/agentic/integration/context/helpers/constants_spec.rb +32 -0
- data/spec/legion/extensions/agentic/integration/context/helpers/context_manager_spec.rb +190 -0
- data/spec/legion/extensions/agentic/integration/context/helpers/frame_spec.rb +164 -0
- data/spec/legion/extensions/agentic/integration/context/runners/context_spec.rb +142 -0
- data/spec/legion/extensions/agentic/integration/distributed_cognition/client_spec.rb +21 -0
- data/spec/legion/extensions/agentic/integration/distributed_cognition/helpers/distribution_engine_spec.rb +149 -0
- data/spec/legion/extensions/agentic/integration/distributed_cognition/helpers/participant_spec.rb +116 -0
- data/spec/legion/extensions/agentic/integration/distributed_cognition/runners/distributed_cognition_spec.rb +102 -0
- data/spec/legion/extensions/agentic/integration/gestalt/client_spec.rb +53 -0
- data/spec/legion/extensions/agentic/integration/gestalt/helpers/constants_spec.rb +27 -0
- data/spec/legion/extensions/agentic/integration/gestalt/helpers/pattern_spec.rb +128 -0
- data/spec/legion/extensions/agentic/integration/gestalt/helpers/pattern_store_spec.rb +155 -0
- data/spec/legion/extensions/agentic/integration/gestalt/runners/gestalt_spec.rb +114 -0
- data/spec/legion/extensions/agentic/integration/global_workspace/client_spec.rb +49 -0
- data/spec/legion/extensions/agentic/integration/global_workspace/helpers/broadcast_spec.rb +83 -0
- data/spec/legion/extensions/agentic/integration/global_workspace/helpers/competitor_spec.rb +84 -0
- data/spec/legion/extensions/agentic/integration/global_workspace/helpers/workspace_spec.rb +174 -0
- data/spec/legion/extensions/agentic/integration/global_workspace/runners/global_workspace_spec.rb +118 -0
- data/spec/legion/extensions/agentic/integration/integration/cognitive_integration_spec.rb +7 -0
- data/spec/legion/extensions/agentic/integration/integration/helpers/integrated_representation_spec.rb +149 -0
- data/spec/legion/extensions/agentic/integration/integration/helpers/integration_engine_spec.rb +191 -0
- data/spec/legion/extensions/agentic/integration/integration/helpers/modal_signal_spec.rb +81 -0
- data/spec/legion/extensions/agentic/integration/labyrinth/client_spec.rb +122 -0
- data/spec/legion/extensions/agentic/integration/labyrinth/cognitive_labyrinth_spec.rb +19 -0
- data/spec/legion/extensions/agentic/integration/labyrinth/helpers/constants_spec.rb +72 -0
- data/spec/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_engine_spec.rb +222 -0
- data/spec/legion/extensions/agentic/integration/labyrinth/helpers/labyrinth_spec.rb +235 -0
- data/spec/legion/extensions/agentic/integration/labyrinth/helpers/node_spec.rb +157 -0
- data/spec/legion/extensions/agentic/integration/labyrinth/runners/cognitive_labyrinth_spec.rb +209 -0
- data/spec/legion/extensions/agentic/integration/map/client_spec.rb +89 -0
- data/spec/legion/extensions/agentic/integration/map/helpers/cognitive_map_store_spec.rb +321 -0
- data/spec/legion/extensions/agentic/integration/map/helpers/location_spec.rb +165 -0
- data/spec/legion/extensions/agentic/integration/map/runners/cognitive_map_spec.rb +190 -0
- data/spec/legion/extensions/agentic/integration/mosaic/client_spec.rb +16 -0
- data/spec/legion/extensions/agentic/integration/mosaic/helpers/constants_spec.rb +37 -0
- data/spec/legion/extensions/agentic/integration/mosaic/helpers/mosaic_engine_spec.rb +108 -0
- data/spec/legion/extensions/agentic/integration/mosaic/helpers/mosaic_spec.rb +120 -0
- data/spec/legion/extensions/agentic/integration/mosaic/helpers/tessera_spec.rb +92 -0
- data/spec/legion/extensions/agentic/integration/mosaic/runners/cognitive_mosaic_spec.rb +88 -0
- data/spec/legion/extensions/agentic/integration/mycelium/client_spec.rb +25 -0
- data/spec/legion/extensions/agentic/integration/mycelium/cognitive_mycelium_spec.rb +7 -0
- data/spec/legion/extensions/agentic/integration/mycelium/helpers/constants_spec.rb +28 -0
- data/spec/legion/extensions/agentic/integration/mycelium/helpers/fruiting_body_spec.rb +33 -0
- data/spec/legion/extensions/agentic/integration/mycelium/helpers/hypha_spec.rb +69 -0
- data/spec/legion/extensions/agentic/integration/mycelium/helpers/mycelial_node_spec.rb +79 -0
- data/spec/legion/extensions/agentic/integration/mycelium/helpers/mycelium_engine_spec.rb +136 -0
- data/spec/legion/extensions/agentic/integration/mycelium/runners/cognitive_mycelium_spec.rb +61 -0
- data/spec/legion/extensions/agentic/integration/phenomenal_binding/client_spec.rb +52 -0
- data/spec/legion/extensions/agentic/integration/phenomenal_binding/helpers/binding_engine_spec.rb +290 -0
- data/spec/legion/extensions/agentic/integration/phenomenal_binding/helpers/binding_unit_spec.rb +195 -0
- data/spec/legion/extensions/agentic/integration/phenomenal_binding/helpers/constants_spec.rb +92 -0
- data/spec/legion/extensions/agentic/integration/phenomenal_binding/helpers/stream_spec.rb +128 -0
- data/spec/legion/extensions/agentic/integration/phenomenal_binding/runners/phenomenal_binding_spec.rb +174 -0
- data/spec/legion/extensions/agentic/integration/qualia/client_spec.rb +21 -0
- data/spec/legion/extensions/agentic/integration/qualia/helpers/quale_spec.rb +168 -0
- data/spec/legion/extensions/agentic/integration/qualia/helpers/qualia_engine_spec.rb +174 -0
- data/spec/legion/extensions/agentic/integration/qualia/qualia_spec.rb +7 -0
- data/spec/legion/extensions/agentic/integration/qualia/runners_spec.rb +70 -0
- data/spec/legion/extensions/agentic/integration/situation_model/client_spec.rb +51 -0
- data/spec/legion/extensions/agentic/integration/situation_model/helpers/constants_spec.rb +56 -0
- data/spec/legion/extensions/agentic/integration/situation_model/helpers/situation_engine_spec.rb +203 -0
- data/spec/legion/extensions/agentic/integration/situation_model/helpers/situation_event_spec.rb +94 -0
- data/spec/legion/extensions/agentic/integration/situation_model/helpers/situation_model_spec.rb +235 -0
- data/spec/legion/extensions/agentic/integration/situation_model/runners/situation_model_spec.rb +204 -0
- data/spec/legion/extensions/agentic/integration/synthesis/client_spec.rb +26 -0
- data/spec/legion/extensions/agentic/integration/synthesis/helpers/constants_spec.rb +87 -0
- data/spec/legion/extensions/agentic/integration/synthesis/helpers/synthesis_engine_spec.rb +233 -0
- data/spec/legion/extensions/agentic/integration/synthesis/helpers/synthesis_spec.rb +101 -0
- data/spec/legion/extensions/agentic/integration/synthesis/helpers/synthesis_stream_spec.rb +116 -0
- data/spec/legion/extensions/agentic/integration/synthesis/runners/cognitive_synthesis_spec.rb +181 -0
- data/spec/legion/extensions/agentic/integration/tapestry/client_spec.rb +55 -0
- data/spec/legion/extensions/agentic/integration/tapestry/helpers/constants_spec.rb +88 -0
- data/spec/legion/extensions/agentic/integration/tapestry/helpers/loom_engine_spec.rb +225 -0
- data/spec/legion/extensions/agentic/integration/tapestry/helpers/tapestry_spec.rb +229 -0
- data/spec/legion/extensions/agentic/integration/tapestry/helpers/thread_spec.rb +211 -0
- data/spec/legion/extensions/agentic/integration/tapestry/runners/cognitive_tapestry_spec.rb +171 -0
- data/spec/legion/extensions/agentic/integration/tessellation/client_spec.rb +40 -0
- data/spec/legion/extensions/agentic/integration/tessellation/helpers/mosaic_spec.rb +99 -0
- data/spec/legion/extensions/agentic/integration/tessellation/helpers/tessellation_engine_spec.rb +145 -0
- data/spec/legion/extensions/agentic/integration/tessellation/helpers/tile_spec.rb +138 -0
- data/spec/legion/extensions/agentic/integration/zeitgeist/helpers/cognitive_signal_spec.rb +62 -0
- data/spec/legion/extensions/agentic/integration/zeitgeist/helpers/constants_spec.rb +73 -0
- data/spec/legion/extensions/agentic/integration/zeitgeist/helpers/trend_window_spec.rb +100 -0
- data/spec/legion/extensions/agentic/integration/zeitgeist/helpers/zeitgeist_engine_spec.rb +163 -0
- data/spec/legion/extensions/agentic/integration/zeitgeist/runners/cognitive_zeitgeist_spec.rb +165 -0
- data/spec/spec_helper.rb +46 -0
- metadata +320 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Integration
|
|
7
|
+
module Context
|
|
8
|
+
module Helpers
|
|
9
|
+
class ContextManager
|
|
10
|
+
include Constants
|
|
11
|
+
|
|
12
|
+
attr_reader :frames, :active_stack, :switch_history
|
|
13
|
+
|
|
14
|
+
def initialize
|
|
15
|
+
@frames = []
|
|
16
|
+
@active_stack = []
|
|
17
|
+
@switch_history = []
|
|
18
|
+
@last_switch_at = nil
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def create_frame(name:, domain: :general, cues: [])
|
|
22
|
+
frame = Frame.new(name: name, domain: domain, cues: cues)
|
|
23
|
+
@frames << frame
|
|
24
|
+
trim_frames
|
|
25
|
+
frame
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def activate(frame_id)
|
|
29
|
+
frame = find(frame_id)
|
|
30
|
+
return nil unless frame
|
|
31
|
+
|
|
32
|
+
previous = current_frame
|
|
33
|
+
cost = 0.0
|
|
34
|
+
|
|
35
|
+
if previous && previous.id != frame_id
|
|
36
|
+
cost = compute_switch_cost(previous, frame)
|
|
37
|
+
previous.deactivate
|
|
38
|
+
record_switch(from: previous, to: frame, cost: cost)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
@active_stack.reject! { |f| f.id == frame_id }
|
|
42
|
+
@active_stack.push(frame)
|
|
43
|
+
@active_stack.shift while @active_stack.size > MAX_FRAME_STACK
|
|
44
|
+
frame.activate
|
|
45
|
+
{ frame: frame, switch_cost: cost }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def current_frame
|
|
49
|
+
@active_stack.last
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def detect_context(input_cues)
|
|
53
|
+
scored = @frames.map { |f| [f, f.match_score(input_cues)] }
|
|
54
|
+
scored.select! { |_, score| score >= RELEVANCE_THRESHOLD }
|
|
55
|
+
scored.sort_by! { |_, score| -score }
|
|
56
|
+
scored.map { |f, score| { frame: f, relevance: score } }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def auto_switch(input_cues)
|
|
60
|
+
candidates = detect_context(input_cues)
|
|
61
|
+
return nil if candidates.empty?
|
|
62
|
+
|
|
63
|
+
best = candidates.first
|
|
64
|
+
current = current_frame
|
|
65
|
+
|
|
66
|
+
return nil if current && current.id == best[:frame].id
|
|
67
|
+
|
|
68
|
+
activate(best[:frame].id)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def find(frame_id)
|
|
72
|
+
@frames.find { |f| f.id == frame_id }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def find_by_name(name)
|
|
76
|
+
@frames.select { |f| f.name == name }
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def in_domain(domain)
|
|
80
|
+
@frames.select { |f| f.domain == domain }
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def decay_all
|
|
84
|
+
@frames.each(&:decay)
|
|
85
|
+
@frames.reject!(&:stale?)
|
|
86
|
+
@active_stack.reject!(&:stale?)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def remove(frame_id)
|
|
90
|
+
@frames.reject! { |f| f.id == frame_id }
|
|
91
|
+
@active_stack.reject! { |f| f.id == frame_id }
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def switch_cost_average
|
|
95
|
+
return 0.0 if @switch_history.empty?
|
|
96
|
+
|
|
97
|
+
@switch_history.sum { |s| s[:cost] }.to_f / @switch_history.size
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def to_h
|
|
101
|
+
{
|
|
102
|
+
frame_count: @frames.size,
|
|
103
|
+
active_frame: current_frame&.name,
|
|
104
|
+
stack_depth: @active_stack.size,
|
|
105
|
+
switch_count: @switch_history.size,
|
|
106
|
+
avg_switch_cost: switch_cost_average.round(4),
|
|
107
|
+
by_domain: @frames.group_by(&:domain).transform_values(&:size)
|
|
108
|
+
}
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
private
|
|
112
|
+
|
|
113
|
+
def compute_switch_cost(from_frame, to_frame)
|
|
114
|
+
base = SWITCH_COST
|
|
115
|
+
familiarity_discount = to_frame.familiarity * 0.2
|
|
116
|
+
domain_bonus = from_frame.domain == to_frame.domain ? 0.02 : 0.0
|
|
117
|
+
cooldown_penalty = in_cooldown? ? 0.1 : 0.0
|
|
118
|
+
(base - familiarity_discount - domain_bonus + cooldown_penalty).clamp(0.0, 1.0)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def in_cooldown?
|
|
122
|
+
return false unless @last_switch_at
|
|
123
|
+
|
|
124
|
+
(Time.now.utc - @last_switch_at) < SWITCH_COOLDOWN
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def record_switch(from:, to:, cost:)
|
|
128
|
+
@last_switch_at = Time.now.utc
|
|
129
|
+
@switch_history << {
|
|
130
|
+
from: from.name,
|
|
131
|
+
to: to.name,
|
|
132
|
+
cost: cost,
|
|
133
|
+
at: @last_switch_at
|
|
134
|
+
}
|
|
135
|
+
@switch_history.shift while @switch_history.size > MAX_HISTORY
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def trim_frames
|
|
139
|
+
return unless @frames.size > MAX_FRAMES
|
|
140
|
+
|
|
141
|
+
@frames.sort_by!(&:strength)
|
|
142
|
+
@frames.shift(@frames.size - MAX_FRAMES)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'securerandom'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Extensions
|
|
7
|
+
module Agentic
|
|
8
|
+
module Integration
|
|
9
|
+
module Context
|
|
10
|
+
module Helpers
|
|
11
|
+
class Frame
|
|
12
|
+
include Constants
|
|
13
|
+
|
|
14
|
+
attr_reader :id, :name, :domain, :cues, :strength, :familiarity,
|
|
15
|
+
:created_at, :last_activated, :activation_count
|
|
16
|
+
|
|
17
|
+
def initialize(name:, domain: :general, cues: [])
|
|
18
|
+
@id = SecureRandom.uuid
|
|
19
|
+
@name = name
|
|
20
|
+
@domain = domain
|
|
21
|
+
@cues = cues.first(MAX_CUES_PER_FRAME)
|
|
22
|
+
@strength = 1.0
|
|
23
|
+
@familiarity = DEFAULT_FAMILIARITY
|
|
24
|
+
@created_at = Time.now.utc
|
|
25
|
+
@last_activated = @created_at
|
|
26
|
+
@activation_count = 0
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def activate
|
|
30
|
+
@last_activated = Time.now.utc
|
|
31
|
+
@activation_count += 1
|
|
32
|
+
@strength = [@strength + 0.1, 1.0].min
|
|
33
|
+
update_familiarity(1.0)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def deactivate
|
|
37
|
+
update_familiarity(0.0)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def decay
|
|
41
|
+
@strength = [(@strength - FRAME_DECAY), 0.0].max
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def match_score(input_cues)
|
|
45
|
+
return 0.0 if @cues.empty? || input_cues.empty?
|
|
46
|
+
|
|
47
|
+
overlap = (@cues & input_cues).size
|
|
48
|
+
overlap.to_f / @cues.size
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def add_cue(cue)
|
|
52
|
+
return if @cues.include?(cue)
|
|
53
|
+
|
|
54
|
+
@cues << cue
|
|
55
|
+
@cues.shift if @cues.size > MAX_CUES_PER_FRAME
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def remove_cue(cue)
|
|
59
|
+
@cues.delete(cue)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def label
|
|
63
|
+
FRAME_LABELS.each do |range, lbl|
|
|
64
|
+
return lbl if range.cover?(@strength)
|
|
65
|
+
end
|
|
66
|
+
:fading
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def stale?
|
|
70
|
+
@strength < FRAME_STRENGTH_FLOOR
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def to_h
|
|
74
|
+
{
|
|
75
|
+
id: @id,
|
|
76
|
+
name: @name,
|
|
77
|
+
domain: @domain,
|
|
78
|
+
strength: @strength,
|
|
79
|
+
familiarity: @familiarity,
|
|
80
|
+
label: label,
|
|
81
|
+
cues: @cues.dup,
|
|
82
|
+
activation_count: @activation_count,
|
|
83
|
+
created_at: @created_at,
|
|
84
|
+
last_activated: @last_activated
|
|
85
|
+
}
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
private
|
|
89
|
+
|
|
90
|
+
def update_familiarity(signal)
|
|
91
|
+
@familiarity = ((FAMILIARITY_ALPHA * signal) + ((1.0 - FAMILIARITY_ALPHA) * @familiarity)).clamp(0.0, 1.0)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Integration
|
|
7
|
+
module Context
|
|
8
|
+
module Runners
|
|
9
|
+
module Context
|
|
10
|
+
include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
|
|
11
|
+
Legion::Extensions::Helpers.const_defined?(:Lex)
|
|
12
|
+
|
|
13
|
+
def create_context(name:, domain: :general, cues: [], **)
|
|
14
|
+
frame = context_manager.create_frame(name: name, domain: domain, cues: cues)
|
|
15
|
+
Legion::Logging.debug "[context] created frame=#{name} domain=#{domain} cues=#{cues.size}"
|
|
16
|
+
{ success: true, frame: frame.to_h }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def activate_context(frame_id:, **)
|
|
20
|
+
result = context_manager.activate(frame_id)
|
|
21
|
+
return { success: false, reason: :not_found } unless result
|
|
22
|
+
|
|
23
|
+
frame = result[:frame]
|
|
24
|
+
Legion::Logging.debug "[context] activated frame=#{frame.name} cost=#{result[:switch_cost].round(3)}"
|
|
25
|
+
{ success: true, frame: frame.to_h, switch_cost: result[:switch_cost] }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def detect_context(input_cues:, **)
|
|
29
|
+
matches = context_manager.detect_context(input_cues)
|
|
30
|
+
Legion::Logging.debug "[context] detect: #{matches.size} candidates for #{input_cues.size} cues"
|
|
31
|
+
{
|
|
32
|
+
success: true,
|
|
33
|
+
candidates: matches.map { |m| { frame: m[:frame].to_h, relevance: m[:relevance] } },
|
|
34
|
+
count: matches.size,
|
|
35
|
+
best: matches.first && { name: matches.first[:frame].name, relevance: matches.first[:relevance] }
|
|
36
|
+
}
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def auto_switch(input_cues:, **)
|
|
40
|
+
result = context_manager.auto_switch(input_cues)
|
|
41
|
+
return { success: true, switched: false, reason: :no_better_match } unless result
|
|
42
|
+
|
|
43
|
+
frame = result[:frame]
|
|
44
|
+
Legion::Logging.debug "[context] auto-switched to frame=#{frame.name} cost=#{result[:switch_cost].round(3)}"
|
|
45
|
+
{ success: true, switched: true, frame: frame.to_h, switch_cost: result[:switch_cost] }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def current_context(**)
|
|
49
|
+
frame = context_manager.current_frame
|
|
50
|
+
return { success: true, frame: nil } unless frame
|
|
51
|
+
|
|
52
|
+
{ success: true, frame: frame.to_h }
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def update_context(**)
|
|
56
|
+
context_manager.decay_all
|
|
57
|
+
Legion::Logging.debug "[context] tick: frames=#{context_manager.frames.size} active=#{context_manager.current_frame&.name}"
|
|
58
|
+
{ success: true, frame_count: context_manager.frames.size, active: context_manager.current_frame&.name }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def add_cue(frame_id:, cue:, **)
|
|
62
|
+
frame = context_manager.find(frame_id)
|
|
63
|
+
return { success: false, reason: :not_found } unless frame
|
|
64
|
+
|
|
65
|
+
frame.add_cue(cue)
|
|
66
|
+
{ success: true, frame: frame.to_h }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def frames_in_domain(domain:, **)
|
|
70
|
+
frames = context_manager.in_domain(domain)
|
|
71
|
+
{ success: true, frames: frames.map(&:to_h), count: frames.size }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def remove_context(frame_id:, **)
|
|
75
|
+
context_manager.remove(frame_id)
|
|
76
|
+
{ success: true }
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def context_stats(**)
|
|
80
|
+
{ success: true, stats: context_manager.to_h }
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
def context_manager
|
|
86
|
+
@context_manager ||= Helpers::ContextManager.new
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/agentic/integration/context/version'
|
|
4
|
+
require 'legion/extensions/agentic/integration/context/helpers/constants'
|
|
5
|
+
require 'legion/extensions/agentic/integration/context/helpers/frame'
|
|
6
|
+
require 'legion/extensions/agentic/integration/context/helpers/context_manager'
|
|
7
|
+
require 'legion/extensions/agentic/integration/context/runners/context'
|
|
8
|
+
require 'legion/extensions/agentic/integration/context/client'
|
|
9
|
+
|
|
10
|
+
module Legion
|
|
11
|
+
module Extensions
|
|
12
|
+
module Agentic
|
|
13
|
+
module Integration
|
|
14
|
+
module Context
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Integration
|
|
7
|
+
module DistributedCognition
|
|
8
|
+
class Client
|
|
9
|
+
include Runners::DistributedCognition
|
|
10
|
+
|
|
11
|
+
def initialize(engine: nil)
|
|
12
|
+
@engine = engine || Helpers::DistributionEngine.new
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Integration
|
|
7
|
+
module DistributedCognition
|
|
8
|
+
module Helpers
|
|
9
|
+
module Constants
|
|
10
|
+
MAX_PARTICIPANTS = 100
|
|
11
|
+
MAX_ARTIFACTS = 200
|
|
12
|
+
MAX_CONTRIBUTIONS = 1000
|
|
13
|
+
MAX_HISTORY = 300
|
|
14
|
+
|
|
15
|
+
DEFAULT_RELIABILITY = 0.5
|
|
16
|
+
RELIABILITY_FLOOR = 0.0
|
|
17
|
+
RELIABILITY_CEILING = 1.0
|
|
18
|
+
|
|
19
|
+
REINFORCEMENT_RATE = 0.1
|
|
20
|
+
PENALTY_RATE = 0.15
|
|
21
|
+
DECAY_RATE = 0.02
|
|
22
|
+
STALE_THRESHOLD = 120
|
|
23
|
+
|
|
24
|
+
PARTICIPANT_TYPES = %i[agent artifact environment].freeze
|
|
25
|
+
|
|
26
|
+
CONTRIBUTION_TYPES = %i[
|
|
27
|
+
computation storage retrieval transformation communication
|
|
28
|
+
].freeze
|
|
29
|
+
|
|
30
|
+
RELIABILITY_LABELS = {
|
|
31
|
+
(0.8..) => :highly_reliable,
|
|
32
|
+
(0.6...0.8) => :reliable,
|
|
33
|
+
(0.4...0.6) => :moderate,
|
|
34
|
+
(0.2...0.4) => :unreliable,
|
|
35
|
+
(..0.2) => :failing
|
|
36
|
+
}.freeze
|
|
37
|
+
|
|
38
|
+
DISTRIBUTION_LABELS = {
|
|
39
|
+
(0.8..) => :fully_distributed,
|
|
40
|
+
(0.6...0.8) => :well_distributed,
|
|
41
|
+
(0.4...0.6) => :partially_distributed,
|
|
42
|
+
(0.2...0.4) => :concentrated,
|
|
43
|
+
(..0.2) => :centralized
|
|
44
|
+
}.freeze
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
data/lib/legion/extensions/agentic/integration/distributed_cognition/helpers/distribution_engine.rb
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module Extensions
|
|
5
|
+
module Agentic
|
|
6
|
+
module Integration
|
|
7
|
+
module DistributedCognition
|
|
8
|
+
module Helpers
|
|
9
|
+
class DistributionEngine
|
|
10
|
+
include Constants
|
|
11
|
+
|
|
12
|
+
attr_reader :history
|
|
13
|
+
|
|
14
|
+
def initialize
|
|
15
|
+
@participants = {}
|
|
16
|
+
@contributions = []
|
|
17
|
+
@history = []
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def register_participant(name:, participant_type:, domain:, capabilities: [])
|
|
21
|
+
return { success: false, reason: :invalid_participant_type } unless PARTICIPANT_TYPES.include?(participant_type)
|
|
22
|
+
|
|
23
|
+
evict_oldest if @participants.size >= MAX_PARTICIPANTS
|
|
24
|
+
|
|
25
|
+
participant = Participant.new(
|
|
26
|
+
name: name,
|
|
27
|
+
participant_type: participant_type,
|
|
28
|
+
domain: domain,
|
|
29
|
+
capabilities: capabilities
|
|
30
|
+
)
|
|
31
|
+
@participants[participant.id] = participant
|
|
32
|
+
record_history(:registered, participant.id)
|
|
33
|
+
participant
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def record_contribution(participant_id:, contribution_type:, success:, context: {})
|
|
37
|
+
participant = @participants[participant_id]
|
|
38
|
+
return { success: false, reason: :not_found } unless participant
|
|
39
|
+
|
|
40
|
+
return { success: false, reason: :invalid_contribution_type } unless CONTRIBUTION_TYPES.include?(contribution_type)
|
|
41
|
+
|
|
42
|
+
participant.contribute!(success: success)
|
|
43
|
+
store_contribution(participant_id, contribution_type, success, context)
|
|
44
|
+
record_history(:contributed, participant_id)
|
|
45
|
+
build_contribution_result(participant, contribution_type)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def find_capable(capability:)
|
|
49
|
+
@participants.values.select { |p| p.capable_of?(capability) }
|
|
50
|
+
.sort_by { |p| -p.reliability }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def by_type(participant_type:)
|
|
54
|
+
@participants.values.select { |p| p.participant_type == participant_type }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def by_domain(domain:)
|
|
58
|
+
@participants.values.select { |p| p.domain == domain }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def most_reliable(limit: 5)
|
|
62
|
+
@participants.values.sort_by { |p| -p.reliability }.first(limit)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def distribution_score
|
|
66
|
+
return 0.0 if @contributions.empty? || @participants.size <= 1
|
|
67
|
+
|
|
68
|
+
counts = @contributions.each_with_object(Hash.new(0)) do |contrib, hash|
|
|
69
|
+
hash[contrib[:participant_id]] += 1
|
|
70
|
+
end
|
|
71
|
+
compute_evenness(counts.values)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def distribution_label
|
|
75
|
+
score = distribution_score
|
|
76
|
+
DISTRIBUTION_LABELS.find { |range, _| range.cover?(score) }&.last || :unknown
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def cognitive_load_balance
|
|
80
|
+
return {} if @participants.empty?
|
|
81
|
+
|
|
82
|
+
@participants.transform_values(&:contribution_count)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def decay_all
|
|
86
|
+
@participants.each_value(&:decay!)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def prune_failing
|
|
90
|
+
failing_ids = @participants.select { |_id, p| p.reliability <= 0.05 }.keys
|
|
91
|
+
failing_ids.each { |id| @participants.delete(id) }
|
|
92
|
+
failing_ids.size
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def to_h
|
|
96
|
+
{
|
|
97
|
+
total_participants: @participants.size,
|
|
98
|
+
agent_count: by_type(participant_type: :agent).size,
|
|
99
|
+
artifact_count: by_type(participant_type: :artifact).size,
|
|
100
|
+
environment_count: by_type(participant_type: :environment).size,
|
|
101
|
+
total_contributions: @contributions.size,
|
|
102
|
+
distribution_score: distribution_score,
|
|
103
|
+
distribution_label: distribution_label,
|
|
104
|
+
history_count: @history.size
|
|
105
|
+
}
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
private
|
|
109
|
+
|
|
110
|
+
def store_contribution(participant_id, contribution_type, success, context)
|
|
111
|
+
entry = {
|
|
112
|
+
participant_id: participant_id,
|
|
113
|
+
contribution_type: contribution_type,
|
|
114
|
+
success: success,
|
|
115
|
+
context: context,
|
|
116
|
+
at: Time.now.utc
|
|
117
|
+
}
|
|
118
|
+
@contributions << entry
|
|
119
|
+
@contributions.shift while @contributions.size > MAX_CONTRIBUTIONS
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def build_contribution_result(participant, contribution_type)
|
|
123
|
+
{
|
|
124
|
+
success: true,
|
|
125
|
+
participant_id: participant.id,
|
|
126
|
+
contribution_type: contribution_type,
|
|
127
|
+
reliability: participant.reliability,
|
|
128
|
+
success_rate: participant.success_rate
|
|
129
|
+
}
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def compute_evenness(counts)
|
|
133
|
+
total = counts.sum.to_f
|
|
134
|
+
return 0.0 if total.zero?
|
|
135
|
+
|
|
136
|
+
proportions = counts.map { |c| c / total }
|
|
137
|
+
max_entropy = Math.log2(counts.size)
|
|
138
|
+
return 1.0 if max_entropy.zero?
|
|
139
|
+
|
|
140
|
+
entropy = -proportions.sum { |p| p.positive? ? p * Math.log2(p) : 0.0 }
|
|
141
|
+
(entropy / max_entropy).clamp(0.0, 1.0)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def evict_oldest
|
|
145
|
+
oldest_id = @participants.min_by { |_id, p| p.last_active_at }&.first
|
|
146
|
+
@participants.delete(oldest_id) if oldest_id
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def record_history(event, subject_id)
|
|
150
|
+
@history << { event: event, subject_id: subject_id, at: Time.now.utc }
|
|
151
|
+
@history.shift while @history.size > MAX_HISTORY
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|