lex-flow 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ad617eaa39c186549a7304251ce00054370ba03c6fbc36916527c6e6f43b0194
4
+ data.tar.gz: e923b25557ef4bc122d66226759a0f0a669570668e0a39fd77f04fa4bdadf13e
5
+ SHA512:
6
+ metadata.gz: 4404b6486afb71a5b14aae7a3bc971483fdc1e2cff8d50bd466bdd639ab7f7dca2b5e92362c654e93ed515da20368206af49ce082e6058cb735928fa708d0a82
7
+ data.tar.gz: 3443d0f74f6be25660aa93b62633ea49977fce4b8821c269fdefaa475002f3087b6dfabb2f3670420d58e5a5c85f8432d5f89e72f1043ac3c2ccb3396af9d403
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'legion/extensions/flow/helpers/constants'
4
+ require 'legion/extensions/flow/helpers/flow_detector'
5
+ require 'legion/extensions/flow/runners/flow'
6
+
7
+ module Legion
8
+ module Extensions
9
+ module Flow
10
+ class Client
11
+ include Runners::Flow
12
+
13
+ attr_reader :flow_detector
14
+
15
+ def initialize(flow_detector: nil, **)
16
+ @flow_detector = flow_detector || Helpers::FlowDetector.new
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module Flow
6
+ module Helpers
7
+ module Constants
8
+ # Flow states (Csikszentmihalyi's model)
9
+ FLOW_STATES = %i[
10
+ flow
11
+ arousal
12
+ control
13
+ relaxation
14
+ boredom
15
+ apathy
16
+ worry
17
+ anxiety
18
+ ].freeze
19
+
20
+ # Input dimensions for flow detection
21
+ DIMENSIONS = %i[challenge skill].freeze
22
+
23
+ # Flow zone boundaries (challenge-skill space)
24
+ # Flow occurs when challenge ~= skill and both are moderate-to-high
25
+ FLOW_ZONE = {
26
+ challenge_min: 0.4,
27
+ challenge_max: 0.8,
28
+ skill_min: 0.4,
29
+ skill_max: 0.8,
30
+ balance_tolerance: 0.15
31
+ }.freeze
32
+
33
+ # EMA alpha for challenge/skill tracking
34
+ FLOW_ALPHA = 0.15
35
+
36
+ # Minimum ticks in flow before "deep flow" bonus
37
+ DEEP_FLOW_THRESHOLD = 20
38
+
39
+ # Flow score bonuses
40
+ DEEP_FLOW_BONUS = 0.1
41
+ CURIOSITY_BONUS = 0.05
42
+ LOW_ERROR_BONUS = 0.05
43
+
44
+ # Flow effects on other subsystems
45
+ FLOW_EFFECTS = {
46
+ fatigue_reduction: 0.5, # fatigue drains 50% slower in flow
47
+ time_dilation: 0.7, # subjective time flies (rate < 1.0)
48
+ performance_boost: 1.15, # 15% performance improvement
49
+ attention_broadening: 0.8, # attention threshold lowered (more open)
50
+ creativity_boost: 1.2 # curiosity/imagination amplified
51
+ }.freeze
52
+
53
+ # Anti-flow indicators (break flow state)
54
+ FLOW_BREAKERS = %i[
55
+ high_anxiety
56
+ trust_violation
57
+ critical_error
58
+ burnout
59
+ conflict_escalation
60
+ ].freeze
61
+
62
+ # History cap
63
+ MAX_FLOW_HISTORY = 100
64
+
65
+ # State classification regions in challenge-skill space
66
+ STATE_REGIONS = {
67
+ flow: { challenge: (0.4..0.8), skill: (0.4..0.8), balanced: true },
68
+ arousal: { challenge: (0.6..1.0), skill: (0.3..0.6), balanced: false },
69
+ control: { challenge: (0.2..0.5), skill: (0.5..0.8), balanced: false },
70
+ relaxation: { challenge: (0.1..0.4), skill: (0.4..0.7), balanced: false },
71
+ boredom: { challenge: (0.0..0.3), skill: (0.5..1.0), balanced: false },
72
+ apathy: { challenge: (0.0..0.3), skill: (0.0..0.3), balanced: false },
73
+ worry: { challenge: (0.5..0.8), skill: (0.1..0.4), balanced: false },
74
+ anxiety: { challenge: (0.7..1.0), skill: (0.0..0.4), balanced: false }
75
+ }.freeze
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,162 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module Flow
6
+ module Helpers
7
+ class FlowDetector
8
+ attr_reader :challenge, :skill, :flow_state, :flow_score,
9
+ :consecutive_flow_ticks, :total_flow_ticks, :history
10
+
11
+ def initialize
12
+ @challenge = 0.5
13
+ @skill = 0.5
14
+ @flow_state = :relaxation
15
+ @flow_score = 0.0
16
+ @consecutive_flow_ticks = 0
17
+ @total_flow_ticks = 0
18
+ @history = []
19
+ end
20
+
21
+ def update(challenge_input:, skill_input:, modifiers: {})
22
+ @challenge = ema(@challenge, challenge_input.clamp(0.0, 1.0), Constants::FLOW_ALPHA)
23
+ @skill = ema(@skill, skill_input.clamp(0.0, 1.0), Constants::FLOW_ALPHA)
24
+
25
+ @flow_state = classify_state
26
+ @flow_score = compute_flow_score(modifiers)
27
+
28
+ if @flow_state == :flow
29
+ @consecutive_flow_ticks += 1
30
+ @total_flow_ticks += 1
31
+ else
32
+ @consecutive_flow_ticks = 0
33
+ end
34
+
35
+ record_snapshot
36
+ end
37
+
38
+ def in_flow?
39
+ @flow_state == :flow
40
+ end
41
+
42
+ def deep_flow?
43
+ in_flow? && @consecutive_flow_ticks >= Constants::DEEP_FLOW_THRESHOLD
44
+ end
45
+
46
+ def flow_effects
47
+ if in_flow?
48
+ effects = Constants::FLOW_EFFECTS.dup
49
+ if deep_flow?
50
+ effects[:performance_boost] += 0.05
51
+ effects[:creativity_boost] += 0.1
52
+ end
53
+ effects
54
+ else
55
+ { fatigue_reduction: 1.0, time_dilation: 1.0, performance_boost: 1.0,
56
+ attention_broadening: 1.0, creativity_boost: 1.0 }
57
+ end
58
+ end
59
+
60
+ def challenge_skill_balance
61
+ (@challenge - @skill).abs
62
+ end
63
+
64
+ def flow_trend
65
+ return :insufficient_data if @history.size < 5
66
+
67
+ recent = @history.last(10)
68
+ scores = recent.map { |h| h[:flow_score] }
69
+ first_half = scores[0...(scores.size / 2)]
70
+ second_half = scores[(scores.size / 2)..]
71
+ diff = (second_half.sum / second_half.size.to_f) - (first_half.sum / first_half.size.to_f)
72
+
73
+ if diff > 0.05
74
+ :entering_flow
75
+ elsif diff < -0.05
76
+ :leaving_flow
77
+ else
78
+ :stable
79
+ end
80
+ end
81
+
82
+ def flow_percentage
83
+ return 0.0 if @history.empty?
84
+
85
+ flow_count = @history.count { |h| h[:state] == :flow }
86
+ (flow_count.to_f / @history.size * 100).round(1)
87
+ end
88
+
89
+ def to_h
90
+ {
91
+ state: @flow_state,
92
+ score: @flow_score.round(3),
93
+ challenge: @challenge.round(3),
94
+ skill: @skill.round(3),
95
+ balance: challenge_skill_balance.round(3),
96
+ in_flow: in_flow?,
97
+ deep_flow: deep_flow?,
98
+ consecutive_flow_ticks: @consecutive_flow_ticks,
99
+ total_flow_ticks: @total_flow_ticks,
100
+ flow_percentage: flow_percentage,
101
+ trend: flow_trend,
102
+ effects: flow_effects
103
+ }
104
+ end
105
+
106
+ private
107
+
108
+ def classify_state
109
+ best_state = :apathy
110
+ best_score = -1.0
111
+
112
+ Constants::STATE_REGIONS.each do |state, region|
113
+ next unless region[:challenge].cover?(@challenge) && region[:skill].cover?(@skill)
114
+
115
+ score = region[:balanced] ? balance_bonus : 0.5
116
+ if score > best_score
117
+ best_score = score
118
+ best_state = state
119
+ end
120
+ end
121
+
122
+ best_state
123
+ end
124
+
125
+ def balance_bonus
126
+ balance = challenge_skill_balance
127
+ balance <= Constants::FLOW_ZONE[:balance_tolerance] ? 1.0 : 0.7
128
+ end
129
+
130
+ def compute_flow_score(modifiers)
131
+ base = if in_flow?
132
+ 0.7 + ((1.0 - challenge_skill_balance) * 0.3)
133
+ else
134
+ [0.0, 0.5 - (challenge_skill_balance * 0.5)].max
135
+ end
136
+
137
+ base += Constants::DEEP_FLOW_BONUS if deep_flow?
138
+ base += Constants::CURIOSITY_BONUS if modifiers[:curiosity_active]
139
+ base += Constants::LOW_ERROR_BONUS if modifiers[:low_errors]
140
+
141
+ base.clamp(0.0, 1.0)
142
+ end
143
+
144
+ def ema(current, observed, alpha)
145
+ (current * (1.0 - alpha)) + (observed * alpha)
146
+ end
147
+
148
+ def record_snapshot
149
+ @history << {
150
+ state: @flow_state,
151
+ flow_score: @flow_score,
152
+ challenge: @challenge,
153
+ skill: @skill,
154
+ at: Time.now.utc
155
+ }
156
+ @history.shift while @history.size > Constants::MAX_FLOW_HISTORY
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module Flow
6
+ module Runners
7
+ module Flow
8
+ include Legion::Extensions::Helpers::Lex if Legion::Extensions.const_defined?(:Helpers) &&
9
+ Legion::Extensions::Helpers.const_defined?(:Lex)
10
+
11
+ def update_flow(tick_results: {}, **)
12
+ challenge_input = extract_challenge(tick_results)
13
+ skill_input = extract_skill(tick_results)
14
+ modifiers = extract_modifiers(tick_results)
15
+
16
+ flow_detector.update(challenge_input: challenge_input, skill_input: skill_input, modifiers: modifiers)
17
+
18
+ breakers = detect_flow_breakers(tick_results)
19
+ if breakers.any? && flow_detector.in_flow?
20
+ flow_detector.instance_variable_set(:@flow_state, :disrupted)
21
+ flow_detector.instance_variable_set(:@consecutive_flow_ticks, 0)
22
+ end
23
+
24
+ Legion::Logging.debug "[flow] state=#{flow_detector.flow_state} score=#{flow_detector.flow_score.round(3)} " \
25
+ "deep=#{flow_detector.deep_flow?} breakers=#{breakers}"
26
+
27
+ {
28
+ state: flow_detector.flow_state,
29
+ score: flow_detector.flow_score.round(3),
30
+ in_flow: flow_detector.in_flow?,
31
+ deep_flow: flow_detector.deep_flow?,
32
+ effects: flow_detector.flow_effects,
33
+ breakers: breakers,
34
+ challenge: flow_detector.challenge.round(3),
35
+ skill: flow_detector.skill.round(3)
36
+ }
37
+ end
38
+
39
+ def flow_status(**)
40
+ Legion::Logging.debug "[flow] status: state=#{flow_detector.flow_state} score=#{flow_detector.flow_score.round(3)}"
41
+ flow_detector.to_h
42
+ end
43
+
44
+ def flow_effects(**)
45
+ effects = flow_detector.flow_effects
46
+ Legion::Logging.debug "[flow] effects: #{effects}"
47
+ { effects: effects, in_flow: flow_detector.in_flow?, deep_flow: flow_detector.deep_flow? }
48
+ end
49
+
50
+ def flow_history(limit: 20, **)
51
+ recent = flow_detector.history.last(limit)
52
+ Legion::Logging.debug "[flow] history: #{recent.size} entries"
53
+ { history: recent, total: flow_detector.history.size }
54
+ end
55
+
56
+ def flow_stats(**)
57
+ Legion::Logging.debug '[flow] stats'
58
+ {
59
+ state: flow_detector.flow_state,
60
+ score: flow_detector.flow_score.round(3),
61
+ consecutive_flow_ticks: flow_detector.consecutive_flow_ticks,
62
+ total_flow_ticks: flow_detector.total_flow_ticks,
63
+ flow_percentage: flow_detector.flow_percentage,
64
+ trend: flow_detector.flow_trend,
65
+ balance: flow_detector.challenge_skill_balance.round(3)
66
+ }
67
+ end
68
+
69
+ private
70
+
71
+ def flow_detector
72
+ @flow_detector ||= Helpers::FlowDetector.new
73
+ end
74
+
75
+ def extract_challenge(tick_results)
76
+ prediction_accuracy = tick_results.dig(:prediction_engine, :rolling_accuracy) || 0.5
77
+ error_rate = tick_results.dig(:prediction_engine, :error_rate) || 0.5
78
+ task_complexity = tick_results.dig(:action_selection, :complexity) || 0.5
79
+
80
+ ((1.0 - prediction_accuracy) * 0.4) + (error_rate * 0.3) + (task_complexity * 0.3)
81
+ end
82
+
83
+ def extract_skill(tick_results)
84
+ prediction_accuracy = tick_results.dig(:prediction_engine, :rolling_accuracy) || 0.5
85
+ memory_strength = tick_results.dig(:memory_retrieval, :avg_strength) || 0.5
86
+ habit_automation = tick_results.dig(:habit, :automation_level) || 0.5
87
+
88
+ (prediction_accuracy * 0.4) + (memory_strength * 0.3) + (habit_automation * 0.3)
89
+ end
90
+
91
+ def extract_modifiers(tick_results)
92
+ {
93
+ curiosity_active: (tick_results.dig(:curiosity, :intensity) || 0.0) > 0.5,
94
+ low_errors: (tick_results.dig(:prediction_engine, :error_rate) || 1.0) < 0.2
95
+ }
96
+ end
97
+
98
+ def detect_flow_breakers(tick_results)
99
+ breakers = []
100
+ check_anxiety_breaker(tick_results, breakers)
101
+ check_simple_breakers(tick_results, breakers)
102
+ check_conflict_breaker(tick_results, breakers)
103
+ breakers
104
+ end
105
+
106
+ def check_anxiety_breaker(tick_results, breakers)
107
+ anxiety = tick_results.dig(:emotional_evaluation, :anxiety) || 0.0
108
+ breakers << :high_anxiety if anxiety > 0.8
109
+ end
110
+
111
+ def check_simple_breakers(tick_results, breakers)
112
+ breakers << :trust_violation if tick_results.dig(:trust, :violation)
113
+ breakers << :critical_error if tick_results.dig(:error, :critical)
114
+ breakers << :burnout if tick_results.dig(:fatigue, :burnout)
115
+ end
116
+
117
+ def check_conflict_breaker(tick_results, breakers)
118
+ conflict = tick_results.dig(:conflict, :severity)
119
+ breakers << :conflict_escalation if conflict && conflict >= 4
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Extensions
5
+ module Flow
6
+ VERSION = '0.1.0'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'legion/extensions/flow/version'
4
+ require 'legion/extensions/flow/helpers/constants'
5
+ require 'legion/extensions/flow/helpers/flow_detector'
6
+ require 'legion/extensions/flow/runners/flow'
7
+ require 'legion/extensions/flow/client'
8
+
9
+ module Legion
10
+ module Extensions
11
+ module Flow
12
+ extend Legion::Extensions::Core if Legion::Extensions.const_defined?(:Core)
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lex-flow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Iverson
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: legion-gaia
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ description: Detects and manages psychological flow states based on challenge-skill
27
+ balance
28
+ email:
29
+ - matt@legionIO.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/legion/extensions/flow.rb
35
+ - lib/legion/extensions/flow/client.rb
36
+ - lib/legion/extensions/flow/helpers/constants.rb
37
+ - lib/legion/extensions/flow/helpers/flow_detector.rb
38
+ - lib/legion/extensions/flow/runners/flow.rb
39
+ - lib/legion/extensions/flow/version.rb
40
+ homepage: https://github.com/LegionIO/lex-flow
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ rubygems_mfa_required: 'true'
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '3.4'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.6.9
60
+ specification_version: 4
61
+ summary: Flow state detection and management for LegionIO cognitive agents
62
+ test_files: []