lex-mental-simulation 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/Gemfile +11 -0
- data/lex-mental-simulation.gemspec +29 -0
- data/lib/legion/extensions/mental_simulation/helpers/client.rb +19 -0
- data/lib/legion/extensions/mental_simulation/helpers/constants.rb +39 -0
- data/lib/legion/extensions/mental_simulation/helpers/simulation.rb +112 -0
- data/lib/legion/extensions/mental_simulation/helpers/simulation_engine.rb +137 -0
- data/lib/legion/extensions/mental_simulation/helpers/simulation_step.rb +49 -0
- data/lib/legion/extensions/mental_simulation/runners/mental_simulation.rb +88 -0
- data/lib/legion/extensions/mental_simulation/version.rb +9 -0
- data/lib/legion/extensions/mental_simulation.rb +17 -0
- data/spec/legion/extensions/mental_simulation/helpers/constants_spec.rb +107 -0
- data/spec/legion/extensions/mental_simulation/helpers/simulation_engine_spec.rb +217 -0
- data/spec/legion/extensions/mental_simulation/helpers/simulation_spec.rb +223 -0
- data/spec/legion/extensions/mental_simulation/helpers/simulation_step_spec.rb +111 -0
- data/spec/legion/extensions/mental_simulation/runners/mental_simulation_spec.rb +184 -0
- data/spec/spec_helper.rb +20 -0
- metadata +77 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'legion/extensions/mental_simulation/helpers/client'
|
|
4
|
+
|
|
5
|
+
RSpec.describe Legion::Extensions::MentalSimulation::Runners::MentalSimulation do
|
|
6
|
+
let(:client) { Legion::Extensions::MentalSimulation::Client.new }
|
|
7
|
+
|
|
8
|
+
def create_and_populate_simulation(favorable: true)
|
|
9
|
+
result = client.create_mental_simulation(label: 'test plan', domain: :infrastructure)
|
|
10
|
+
sim_id = result[:simulation_id]
|
|
11
|
+
if favorable
|
|
12
|
+
client.add_simulation_step(simulation_id: sim_id, action: 'check health', confidence: 0.9, risk: 0.05)
|
|
13
|
+
client.add_simulation_step(simulation_id: sim_id, action: 'deploy service', confidence: 0.85, risk: 0.05)
|
|
14
|
+
else
|
|
15
|
+
client.add_simulation_step(simulation_id: sim_id, action: 'low confidence step', confidence: 0.2, risk: 0.1)
|
|
16
|
+
end
|
|
17
|
+
sim_id
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
describe '#create_mental_simulation' do
|
|
21
|
+
it 'returns a simulation_id' do
|
|
22
|
+
result = client.create_mental_simulation(label: 'my plan', domain: :networking)
|
|
23
|
+
expect(result[:simulation_id]).to match(/\A[0-9a-f-]{36}\z/)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it 'returns the label' do
|
|
27
|
+
result = client.create_mental_simulation(label: 'my plan', domain: :networking)
|
|
28
|
+
expect(result[:label]).to eq('my plan')
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'returns the domain' do
|
|
32
|
+
result = client.create_mental_simulation(label: 'my plan', domain: :networking)
|
|
33
|
+
expect(result[:domain]).to eq(:networking)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it 'starts in :pending state' do
|
|
37
|
+
result = client.create_mental_simulation(label: 'my plan', domain: :app)
|
|
38
|
+
expect(result[:state]).to eq(:pending)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
describe '#add_simulation_step' do
|
|
43
|
+
it 'returns added: true' do
|
|
44
|
+
sim_result = client.create_mental_simulation(label: 'plan', domain: :app)
|
|
45
|
+
result = client.add_simulation_step(simulation_id: sim_result[:simulation_id], action: 'step 1')
|
|
46
|
+
expect(result[:added]).to be true
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it 'returns a step_id' do
|
|
50
|
+
sim_result = client.create_mental_simulation(label: 'plan', domain: :app)
|
|
51
|
+
result = client.add_simulation_step(simulation_id: sim_result[:simulation_id], action: 'step 1')
|
|
52
|
+
expect(result[:step_id]).to match(/\A[0-9a-f-]{36}\z/)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it 'returns error for unknown simulation' do
|
|
56
|
+
result = client.add_simulation_step(simulation_id: 'bad-id', action: 'noop')
|
|
57
|
+
expect(result[:error]).to eq(:simulation_not_found)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it 'increments step_count' do
|
|
61
|
+
sim_result = client.create_mental_simulation(label: 'plan', domain: :app)
|
|
62
|
+
client.add_simulation_step(simulation_id: sim_result[:simulation_id], action: 'step 1')
|
|
63
|
+
result = client.add_simulation_step(simulation_id: sim_result[:simulation_id], action: 'step 2')
|
|
64
|
+
expect(result[:step_count]).to eq(2)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
describe '#run_mental_simulation' do
|
|
69
|
+
it 'transitions state from pending' do
|
|
70
|
+
sim_id = create_and_populate_simulation
|
|
71
|
+
result = client.run_mental_simulation(simulation_id: sim_id)
|
|
72
|
+
expect(result[:state]).not_to eq(:pending)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it 'returns favorable: true for a well-configured plan' do
|
|
76
|
+
sim_id = create_and_populate_simulation(favorable: true)
|
|
77
|
+
result = client.run_mental_simulation(simulation_id: sim_id)
|
|
78
|
+
expect(result[:favorable]).to be true
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it 'returns favorable: false for a low-confidence plan' do
|
|
82
|
+
sim_id = create_and_populate_simulation(favorable: false)
|
|
83
|
+
result = client.run_mental_simulation(simulation_id: sim_id)
|
|
84
|
+
expect(result[:favorable]).to be false
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it 'returns error for unknown simulation' do
|
|
88
|
+
result = client.run_mental_simulation(simulation_id: 'ghost')
|
|
89
|
+
expect(result[:error]).to eq(:simulation_not_found)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'includes step_count in result' do
|
|
93
|
+
sim_id = create_and_populate_simulation
|
|
94
|
+
result = client.run_mental_simulation(simulation_id: sim_id)
|
|
95
|
+
expect(result[:step_count]).to eq(2)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
describe '#abort_mental_simulation' do
|
|
100
|
+
it 'aborts a simulation' do
|
|
101
|
+
sim_result = client.create_mental_simulation(label: 'to abort', domain: :app)
|
|
102
|
+
result = client.abort_mental_simulation(simulation_id: sim_result[:simulation_id])
|
|
103
|
+
expect(result[:state]).to eq(:aborted)
|
|
104
|
+
expect(result[:aborted]).to be true
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
it 'returns error for unknown simulation' do
|
|
108
|
+
result = client.abort_mental_simulation(simulation_id: 'ghost')
|
|
109
|
+
expect(result[:error]).to eq(:simulation_not_found)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
describe '#assess_mental_simulation' do
|
|
114
|
+
it 'returns assessment without running' do
|
|
115
|
+
sim_result = client.create_mental_simulation(label: 'assess me', domain: :security)
|
|
116
|
+
client.add_simulation_step(simulation_id: sim_result[:simulation_id], action: 'read logs',
|
|
117
|
+
confidence: 0.8, risk: 0.1)
|
|
118
|
+
result = client.assess_mental_simulation(simulation_id: sim_result[:simulation_id])
|
|
119
|
+
expect(result[:state]).to eq(:pending)
|
|
120
|
+
expect(result[:steps].size).to eq(1)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it 'returns error for unknown simulation' do
|
|
124
|
+
result = client.assess_mental_simulation(simulation_id: 'ghost')
|
|
125
|
+
expect(result[:error]).to eq(:simulation_not_found)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
describe '#favorable_simulations_report' do
|
|
130
|
+
it 'returns a report with count' do
|
|
131
|
+
sim_id = create_and_populate_simulation(favorable: true)
|
|
132
|
+
client.run_mental_simulation(simulation_id: sim_id)
|
|
133
|
+
result = client.favorable_simulations_report
|
|
134
|
+
expect(result).to include(:simulations, :count)
|
|
135
|
+
expect(result[:count]).to be >= 1
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
it 'returns count: 0 when no favorable simulations exist' do
|
|
139
|
+
result = client.favorable_simulations_report
|
|
140
|
+
expect(result[:count]).to eq(0)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
describe '#failed_simulations_report' do
|
|
145
|
+
it 'returns a report with count' do
|
|
146
|
+
sim_id = create_and_populate_simulation(favorable: false)
|
|
147
|
+
client.run_mental_simulation(simulation_id: sim_id)
|
|
148
|
+
result = client.failed_simulations_report
|
|
149
|
+
expect(result).to include(:simulations, :count)
|
|
150
|
+
expect(result[:count]).to be >= 1
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
describe '#riskiest_simulations_report' do
|
|
155
|
+
it 'returns a report with simulations and count' do
|
|
156
|
+
sim_id = create_and_populate_simulation
|
|
157
|
+
client.add_simulation_step(simulation_id: sim_id, action: 'risky op', risk: 0.9)
|
|
158
|
+
result = client.riskiest_simulations_report
|
|
159
|
+
expect(result).to include(:simulations, :count)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
it 'accepts a limit parameter' do
|
|
163
|
+
3.times { client.create_mental_simulation(label: 'sim', domain: :app) }
|
|
164
|
+
result = client.riskiest_simulations_report(limit: 2)
|
|
165
|
+
expect(result[:count]).to be <= 2
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
describe '#mental_simulation_stats' do
|
|
170
|
+
it 'returns stats hash without simulations list' do
|
|
171
|
+
client.create_mental_simulation(label: 'one', domain: :app)
|
|
172
|
+
result = client.mental_simulation_stats
|
|
173
|
+
expect(result).to include(:total_simulations, :history_size, :favorable_count, :failed_count)
|
|
174
|
+
expect(result).not_to have_key(:simulations)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
it 'reflects created simulations' do
|
|
178
|
+
client.create_mental_simulation(label: 'a', domain: :app)
|
|
179
|
+
client.create_mental_simulation(label: 'b', domain: :app)
|
|
180
|
+
result = client.mental_simulation_stats
|
|
181
|
+
expect(result[:total_simulations]).to eq(2)
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'bundler/setup'
|
|
4
|
+
|
|
5
|
+
module Legion
|
|
6
|
+
module Logging
|
|
7
|
+
def self.debug(_msg); end
|
|
8
|
+
def self.info(_msg); end
|
|
9
|
+
def self.warn(_msg); end
|
|
10
|
+
def self.error(_msg); end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
require 'legion/extensions/mental_simulation'
|
|
15
|
+
|
|
16
|
+
RSpec.configure do |config|
|
|
17
|
+
config.example_status_persistence_file_path = '.rspec_status'
|
|
18
|
+
config.disable_monkey_patching!
|
|
19
|
+
config.expect_with(:rspec) { |c| c.syntax = :expect }
|
|
20
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: lex-mental-simulation
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Esity
|
|
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: Forward mental simulation of action sequences — imagine a plan, predict
|
|
27
|
+
step outcomes, evaluate before executing
|
|
28
|
+
email:
|
|
29
|
+
- matthewdiverson@gmail.com
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- Gemfile
|
|
35
|
+
- lex-mental-simulation.gemspec
|
|
36
|
+
- lib/legion/extensions/mental_simulation.rb
|
|
37
|
+
- lib/legion/extensions/mental_simulation/helpers/client.rb
|
|
38
|
+
- lib/legion/extensions/mental_simulation/helpers/constants.rb
|
|
39
|
+
- lib/legion/extensions/mental_simulation/helpers/simulation.rb
|
|
40
|
+
- lib/legion/extensions/mental_simulation/helpers/simulation_engine.rb
|
|
41
|
+
- lib/legion/extensions/mental_simulation/helpers/simulation_step.rb
|
|
42
|
+
- lib/legion/extensions/mental_simulation/runners/mental_simulation.rb
|
|
43
|
+
- lib/legion/extensions/mental_simulation/version.rb
|
|
44
|
+
- spec/legion/extensions/mental_simulation/helpers/constants_spec.rb
|
|
45
|
+
- spec/legion/extensions/mental_simulation/helpers/simulation_engine_spec.rb
|
|
46
|
+
- spec/legion/extensions/mental_simulation/helpers/simulation_spec.rb
|
|
47
|
+
- spec/legion/extensions/mental_simulation/helpers/simulation_step_spec.rb
|
|
48
|
+
- spec/legion/extensions/mental_simulation/runners/mental_simulation_spec.rb
|
|
49
|
+
- spec/spec_helper.rb
|
|
50
|
+
homepage: https://github.com/LegionIO/lex-mental-simulation
|
|
51
|
+
licenses:
|
|
52
|
+
- MIT
|
|
53
|
+
metadata:
|
|
54
|
+
homepage_uri: https://github.com/LegionIO/lex-mental-simulation
|
|
55
|
+
source_code_uri: https://github.com/LegionIO/lex-mental-simulation
|
|
56
|
+
documentation_uri: https://github.com/LegionIO/lex-mental-simulation
|
|
57
|
+
changelog_uri: https://github.com/LegionIO/lex-mental-simulation
|
|
58
|
+
bug_tracker_uri: https://github.com/LegionIO/lex-mental-simulation/issues
|
|
59
|
+
rubygems_mfa_required: 'true'
|
|
60
|
+
rdoc_options: []
|
|
61
|
+
require_paths:
|
|
62
|
+
- lib
|
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '3.4'
|
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
|
+
requirements:
|
|
70
|
+
- - ">="
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: '0'
|
|
73
|
+
requirements: []
|
|
74
|
+
rubygems_version: 3.6.9
|
|
75
|
+
specification_version: 4
|
|
76
|
+
summary: LEX Mental Simulation
|
|
77
|
+
test_files: []
|