decision_agent 0.2.0 → 1.0.1
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 +4 -4
- data/README.md +313 -8
- data/bin/decision_agent +104 -0
- data/lib/decision_agent/agent.rb +72 -1
- data/lib/decision_agent/context.rb +1 -0
- data/lib/decision_agent/data_enrichment/cache/memory_adapter.rb +86 -0
- data/lib/decision_agent/data_enrichment/cache_adapter.rb +49 -0
- data/lib/decision_agent/data_enrichment/circuit_breaker.rb +135 -0
- data/lib/decision_agent/data_enrichment/client.rb +220 -0
- data/lib/decision_agent/data_enrichment/config.rb +78 -0
- data/lib/decision_agent/data_enrichment/errors.rb +36 -0
- data/lib/decision_agent/decision.rb +102 -2
- data/lib/decision_agent/dmn/adapter.rb +135 -0
- data/lib/decision_agent/dmn/cache.rb +306 -0
- data/lib/decision_agent/dmn/decision_graph.rb +327 -0
- data/lib/decision_agent/dmn/decision_tree.rb +192 -0
- data/lib/decision_agent/dmn/errors.rb +30 -0
- data/lib/decision_agent/dmn/exporter.rb +217 -0
- data/lib/decision_agent/dmn/feel/evaluator.rb +819 -0
- data/lib/decision_agent/dmn/feel/functions.rb +420 -0
- data/lib/decision_agent/dmn/feel/parser.rb +349 -0
- data/lib/decision_agent/dmn/feel/simple_parser.rb +276 -0
- data/lib/decision_agent/dmn/feel/transformer.rb +372 -0
- data/lib/decision_agent/dmn/feel/types.rb +276 -0
- data/lib/decision_agent/dmn/importer.rb +77 -0
- data/lib/decision_agent/dmn/model.rb +197 -0
- data/lib/decision_agent/dmn/parser.rb +191 -0
- data/lib/decision_agent/dmn/testing.rb +333 -0
- data/lib/decision_agent/dmn/validator.rb +315 -0
- data/lib/decision_agent/dmn/versioning.rb +229 -0
- data/lib/decision_agent/dmn/visualizer.rb +513 -0
- data/lib/decision_agent/dsl/condition_evaluator.rb +984 -838
- data/lib/decision_agent/dsl/schema_validator.rb +53 -14
- data/lib/decision_agent/evaluators/dmn_evaluator.rb +308 -0
- data/lib/decision_agent/evaluators/json_rule_evaluator.rb +69 -9
- data/lib/decision_agent/explainability/condition_trace.rb +83 -0
- data/lib/decision_agent/explainability/explainability_result.rb +52 -0
- data/lib/decision_agent/explainability/rule_trace.rb +39 -0
- data/lib/decision_agent/explainability/trace_collector.rb +24 -0
- data/lib/decision_agent/monitoring/alert_manager.rb +5 -1
- data/lib/decision_agent/simulation/errors.rb +18 -0
- data/lib/decision_agent/simulation/impact_analyzer.rb +498 -0
- data/lib/decision_agent/simulation/monte_carlo_simulator.rb +635 -0
- data/lib/decision_agent/simulation/replay_engine.rb +486 -0
- data/lib/decision_agent/simulation/scenario_engine.rb +318 -0
- data/lib/decision_agent/simulation/scenario_library.rb +163 -0
- data/lib/decision_agent/simulation/shadow_test_engine.rb +287 -0
- data/lib/decision_agent/simulation/what_if_analyzer.rb +1002 -0
- data/lib/decision_agent/simulation.rb +17 -0
- data/lib/decision_agent/version.rb +1 -1
- data/lib/decision_agent/versioning/activerecord_adapter.rb +23 -8
- data/lib/decision_agent/web/dmn_editor.rb +426 -0
- data/lib/decision_agent/web/public/app.js +119 -0
- data/lib/decision_agent/web/public/dmn-editor.css +596 -0
- data/lib/decision_agent/web/public/dmn-editor.html +250 -0
- data/lib/decision_agent/web/public/dmn-editor.js +553 -0
- data/lib/decision_agent/web/public/index.html +52 -0
- data/lib/decision_agent/web/public/simulation.html +130 -0
- data/lib/decision_agent/web/public/simulation_impact.html +478 -0
- data/lib/decision_agent/web/public/simulation_replay.html +551 -0
- data/lib/decision_agent/web/public/simulation_shadow.html +546 -0
- data/lib/decision_agent/web/public/simulation_whatif.html +532 -0
- data/lib/decision_agent/web/public/styles.css +86 -0
- data/lib/decision_agent/web/server.rb +1059 -23
- data/lib/decision_agent.rb +60 -2
- metadata +105 -61
- data/spec/ab_testing/ab_test_assignment_spec.rb +0 -253
- data/spec/ab_testing/ab_test_manager_spec.rb +0 -612
- data/spec/ab_testing/ab_test_spec.rb +0 -270
- data/spec/ab_testing/ab_testing_agent_spec.rb +0 -481
- data/spec/ab_testing/storage/adapter_spec.rb +0 -64
- data/spec/ab_testing/storage/memory_adapter_spec.rb +0 -485
- data/spec/activerecord_thread_safety_spec.rb +0 -553
- data/spec/advanced_operators_spec.rb +0 -3150
- data/spec/agent_spec.rb +0 -289
- data/spec/api_contract_spec.rb +0 -430
- data/spec/audit_adapters_spec.rb +0 -92
- data/spec/auth/access_audit_logger_spec.rb +0 -394
- data/spec/auth/authenticator_spec.rb +0 -112
- data/spec/auth/password_reset_spec.rb +0 -294
- data/spec/auth/permission_checker_spec.rb +0 -207
- data/spec/auth/permission_spec.rb +0 -73
- data/spec/auth/rbac_adapter_spec.rb +0 -550
- data/spec/auth/rbac_config_spec.rb +0 -82
- data/spec/auth/role_spec.rb +0 -51
- data/spec/auth/session_manager_spec.rb +0 -172
- data/spec/auth/session_spec.rb +0 -112
- data/spec/auth/user_spec.rb +0 -130
- data/spec/comprehensive_edge_cases_spec.rb +0 -1777
- data/spec/context_spec.rb +0 -127
- data/spec/decision_agent_spec.rb +0 -96
- data/spec/decision_spec.rb +0 -423
- data/spec/dsl/condition_evaluator_spec.rb +0 -774
- data/spec/dsl_validation_spec.rb +0 -648
- data/spec/edge_cases_spec.rb +0 -353
- data/spec/evaluation_spec.rb +0 -364
- data/spec/evaluation_validator_spec.rb +0 -165
- data/spec/examples/feedback_aware_evaluator_spec.rb +0 -460
- data/spec/examples.txt +0 -1633
- data/spec/issue_verification_spec.rb +0 -759
- data/spec/json_rule_evaluator_spec.rb +0 -587
- data/spec/monitoring/alert_manager_spec.rb +0 -378
- data/spec/monitoring/metrics_collector_spec.rb +0 -499
- data/spec/monitoring/monitored_agent_spec.rb +0 -222
- data/spec/monitoring/prometheus_exporter_spec.rb +0 -242
- data/spec/monitoring/storage/activerecord_adapter_spec.rb +0 -498
- data/spec/monitoring/storage/base_adapter_spec.rb +0 -61
- data/spec/monitoring/storage/memory_adapter_spec.rb +0 -247
- data/spec/performance_optimizations_spec.rb +0 -486
- data/spec/replay_edge_cases_spec.rb +0 -699
- data/spec/replay_spec.rb +0 -210
- data/spec/rfc8785_canonicalization_spec.rb +0 -215
- data/spec/scoring_spec.rb +0 -225
- data/spec/spec_helper.rb +0 -60
- data/spec/testing/batch_test_importer_spec.rb +0 -693
- data/spec/testing/batch_test_runner_spec.rb +0 -307
- data/spec/testing/test_coverage_analyzer_spec.rb +0 -292
- data/spec/testing/test_result_comparator_spec.rb +0 -392
- data/spec/testing/test_scenario_spec.rb +0 -113
- data/spec/thread_safety_spec.rb +0 -482
- data/spec/thread_safety_spec.rb.broken +0 -878
- data/spec/versioning/adapter_spec.rb +0 -156
- data/spec/versioning_spec.rb +0 -1030
- data/spec/web/middleware/auth_middleware_spec.rb +0 -133
- data/spec/web/middleware/permission_middleware_spec.rb +0 -247
- data/spec/web_ui_rack_spec.rb +0 -1840
data/spec/agent_spec.rb
DELETED
|
@@ -1,289 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
|
|
3
|
-
RSpec.describe DecisionAgent::Agent do
|
|
4
|
-
describe "#initialize" do
|
|
5
|
-
it "requires at least one evaluator" do
|
|
6
|
-
expect do
|
|
7
|
-
DecisionAgent::Agent.new(evaluators: [])
|
|
8
|
-
end.to raise_error(DecisionAgent::InvalidConfigurationError, /at least one evaluator/i)
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
it "validates evaluators respond to #evaluate" do
|
|
12
|
-
invalid_evaluator = Object.new
|
|
13
|
-
|
|
14
|
-
expect do
|
|
15
|
-
DecisionAgent::Agent.new(evaluators: [invalid_evaluator])
|
|
16
|
-
end.to raise_error(DecisionAgent::InvalidEvaluatorError)
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
it "validates scoring strategy responds to #score" do
|
|
20
|
-
evaluator = DecisionAgent::Evaluators::StaticEvaluator.new(decision: "approve")
|
|
21
|
-
invalid_strategy = Object.new
|
|
22
|
-
|
|
23
|
-
expect do
|
|
24
|
-
DecisionAgent::Agent.new(
|
|
25
|
-
evaluators: [evaluator],
|
|
26
|
-
scoring_strategy: invalid_strategy
|
|
27
|
-
)
|
|
28
|
-
end.to raise_error(DecisionAgent::InvalidScoringStrategyError)
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
it "validates audit adapter responds to #record" do
|
|
32
|
-
evaluator = DecisionAgent::Evaluators::StaticEvaluator.new(decision: "approve")
|
|
33
|
-
invalid_adapter = Object.new
|
|
34
|
-
|
|
35
|
-
expect do
|
|
36
|
-
DecisionAgent::Agent.new(
|
|
37
|
-
evaluators: [evaluator],
|
|
38
|
-
audit_adapter: invalid_adapter
|
|
39
|
-
)
|
|
40
|
-
end.to raise_error(DecisionAgent::InvalidAuditAdapterError)
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
it "uses defaults when optional parameters are omitted" do
|
|
44
|
-
evaluator = DecisionAgent::Evaluators::StaticEvaluator.new(decision: "approve")
|
|
45
|
-
|
|
46
|
-
agent = DecisionAgent::Agent.new(evaluators: [evaluator])
|
|
47
|
-
|
|
48
|
-
expect(agent.scoring_strategy).to be_a(DecisionAgent::Scoring::WeightedAverage)
|
|
49
|
-
expect(agent.audit_adapter).to be_a(DecisionAgent::Audit::NullAdapter)
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
it "enables validation by default in non-production environments" do
|
|
53
|
-
evaluator = DecisionAgent::Evaluators::StaticEvaluator.new(decision: "approve")
|
|
54
|
-
original_env = ENV.fetch("RAILS_ENV", nil)
|
|
55
|
-
ENV["RAILS_ENV"] = "development"
|
|
56
|
-
|
|
57
|
-
agent = DecisionAgent::Agent.new(evaluators: [evaluator])
|
|
58
|
-
# Validation should be enabled (we can't directly test this, but we can test behavior)
|
|
59
|
-
# If validation is enabled, invalid evaluations would raise errors
|
|
60
|
-
expect(agent).to be_a(DecisionAgent::Agent)
|
|
61
|
-
|
|
62
|
-
ENV["RAILS_ENV"] = original_env
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
it "disables validation in production by default" do
|
|
66
|
-
evaluator = DecisionAgent::Evaluators::StaticEvaluator.new(decision: "approve")
|
|
67
|
-
original_env = ENV.fetch("RAILS_ENV", nil)
|
|
68
|
-
ENV["RAILS_ENV"] = "production"
|
|
69
|
-
|
|
70
|
-
agent = DecisionAgent::Agent.new(evaluators: [evaluator])
|
|
71
|
-
expect(agent).to be_a(DecisionAgent::Agent)
|
|
72
|
-
|
|
73
|
-
ENV["RAILS_ENV"] = original_env
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
it "allows explicit validation control" do
|
|
77
|
-
evaluator = DecisionAgent::Evaluators::StaticEvaluator.new(decision: "approve")
|
|
78
|
-
|
|
79
|
-
agent_with_validation = DecisionAgent::Agent.new(
|
|
80
|
-
evaluators: [evaluator],
|
|
81
|
-
validate_evaluations: true
|
|
82
|
-
)
|
|
83
|
-
expect(agent_with_validation).to be_a(DecisionAgent::Agent)
|
|
84
|
-
|
|
85
|
-
agent_without_validation = DecisionAgent::Agent.new(
|
|
86
|
-
evaluators: [evaluator],
|
|
87
|
-
validate_evaluations: false
|
|
88
|
-
)
|
|
89
|
-
expect(agent_without_validation).to be_a(DecisionAgent::Agent)
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
describe "#decide" do
|
|
94
|
-
it "returns a Decision object with all required fields" do
|
|
95
|
-
evaluator = DecisionAgent::Evaluators::StaticEvaluator.new(
|
|
96
|
-
decision: "approve",
|
|
97
|
-
weight: 0.8,
|
|
98
|
-
reason: "Test approval"
|
|
99
|
-
)
|
|
100
|
-
|
|
101
|
-
agent = DecisionAgent::Agent.new(evaluators: [evaluator])
|
|
102
|
-
|
|
103
|
-
result = agent.decide(context: { user: "test" })
|
|
104
|
-
|
|
105
|
-
expect(result).to be_a(DecisionAgent::Decision)
|
|
106
|
-
expect(result.decision).to eq("approve")
|
|
107
|
-
expect(result.confidence).to be_between(0.0, 1.0)
|
|
108
|
-
expect(result.explanations).to be_an(Array)
|
|
109
|
-
expect(result.evaluations).to be_an(Array)
|
|
110
|
-
expect(result.audit_payload).to be_a(Hash)
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
it "accepts Context object or Hash for context parameter" do
|
|
114
|
-
evaluator = DecisionAgent::Evaluators::StaticEvaluator.new(decision: "approve")
|
|
115
|
-
agent = DecisionAgent::Agent.new(evaluators: [evaluator])
|
|
116
|
-
|
|
117
|
-
result1 = agent.decide(context: { user: "test" })
|
|
118
|
-
result2 = agent.decide(context: DecisionAgent::Context.new({ user: "test" }))
|
|
119
|
-
|
|
120
|
-
expect(result1.decision).to eq(result2.decision)
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
it "raises NoEvaluationsError when no evaluators return decisions" do
|
|
124
|
-
failing_evaluator = Class.new(DecisionAgent::Evaluators::Base) do
|
|
125
|
-
def evaluate(_context, feedback: {})
|
|
126
|
-
nil
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
agent = DecisionAgent::Agent.new(evaluators: [failing_evaluator.new])
|
|
131
|
-
|
|
132
|
-
expect do
|
|
133
|
-
agent.decide(context: {})
|
|
134
|
-
end.to raise_error(DecisionAgent::NoEvaluationsError)
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
it "includes feedback in evaluation" do
|
|
138
|
-
feedback_evaluator = Class.new(DecisionAgent::Evaluators::Base) do
|
|
139
|
-
def evaluate(_context, feedback: {})
|
|
140
|
-
decision = feedback[:override] ? "reject" : "approve"
|
|
141
|
-
DecisionAgent::Evaluation.new(
|
|
142
|
-
decision: decision,
|
|
143
|
-
weight: 1.0,
|
|
144
|
-
reason: "Feedback-based",
|
|
145
|
-
evaluator_name: "FeedbackEvaluator"
|
|
146
|
-
)
|
|
147
|
-
end
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
agent = DecisionAgent::Agent.new(evaluators: [feedback_evaluator.new])
|
|
151
|
-
|
|
152
|
-
result1 = agent.decide(context: {}, feedback: {})
|
|
153
|
-
result2 = agent.decide(context: {}, feedback: { override: true })
|
|
154
|
-
|
|
155
|
-
expect(result1.decision).to eq("approve")
|
|
156
|
-
expect(result2.decision).to eq("reject")
|
|
157
|
-
end
|
|
158
|
-
|
|
159
|
-
it "records decision via audit adapter" do
|
|
160
|
-
evaluator = DecisionAgent::Evaluators::StaticEvaluator.new(decision: "approve")
|
|
161
|
-
|
|
162
|
-
audit_adapter = Class.new(DecisionAgent::Audit::Adapter) do
|
|
163
|
-
attr_reader :recorded_decision, :recorded_context
|
|
164
|
-
|
|
165
|
-
def record(decision, context)
|
|
166
|
-
@recorded_decision = decision
|
|
167
|
-
@recorded_context = context
|
|
168
|
-
end
|
|
169
|
-
end.new
|
|
170
|
-
|
|
171
|
-
agent = DecisionAgent::Agent.new(
|
|
172
|
-
evaluators: [evaluator],
|
|
173
|
-
audit_adapter: audit_adapter
|
|
174
|
-
)
|
|
175
|
-
|
|
176
|
-
result = agent.decide(context: { user: "test" })
|
|
177
|
-
|
|
178
|
-
expect(audit_adapter.recorded_decision).to eq(result)
|
|
179
|
-
expect(audit_adapter.recorded_context.to_h).to eq({ user: "test" })
|
|
180
|
-
end
|
|
181
|
-
|
|
182
|
-
it "includes deterministic hash in audit payload" do
|
|
183
|
-
evaluator = DecisionAgent::Evaluators::StaticEvaluator.new(decision: "approve", weight: 0.8)
|
|
184
|
-
agent = DecisionAgent::Agent.new(evaluators: [evaluator])
|
|
185
|
-
|
|
186
|
-
result1 = agent.decide(context: { user: "test" })
|
|
187
|
-
result2 = agent.decide(context: { user: "test" })
|
|
188
|
-
|
|
189
|
-
expect(result1.audit_payload[:deterministic_hash]).to be_a(String)
|
|
190
|
-
expect(result1.audit_payload[:deterministic_hash]).to eq(result2.audit_payload[:deterministic_hash])
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
it "produces different hashes for different contexts" do
|
|
194
|
-
evaluator = DecisionAgent::Evaluators::StaticEvaluator.new(decision: "approve")
|
|
195
|
-
agent = DecisionAgent::Agent.new(evaluators: [evaluator])
|
|
196
|
-
|
|
197
|
-
result1 = agent.decide(context: { user: "alice" })
|
|
198
|
-
result2 = agent.decide(context: { user: "bob" })
|
|
199
|
-
|
|
200
|
-
expect(result1.audit_payload[:deterministic_hash]).not_to eq(result2.audit_payload[:deterministic_hash])
|
|
201
|
-
end
|
|
202
|
-
end
|
|
203
|
-
|
|
204
|
-
describe "conflict resolution" do
|
|
205
|
-
it "resolves conflicting evaluations using scoring strategy" do
|
|
206
|
-
evaluator1 = DecisionAgent::Evaluators::StaticEvaluator.new(
|
|
207
|
-
decision: "approve",
|
|
208
|
-
weight: 0.6,
|
|
209
|
-
name: "Evaluator1"
|
|
210
|
-
)
|
|
211
|
-
evaluator2 = DecisionAgent::Evaluators::StaticEvaluator.new(
|
|
212
|
-
decision: "reject",
|
|
213
|
-
weight: 0.9,
|
|
214
|
-
name: "Evaluator2"
|
|
215
|
-
)
|
|
216
|
-
|
|
217
|
-
agent = DecisionAgent::Agent.new(
|
|
218
|
-
evaluators: [evaluator1, evaluator2],
|
|
219
|
-
scoring_strategy: DecisionAgent::Scoring::MaxWeight.new
|
|
220
|
-
)
|
|
221
|
-
|
|
222
|
-
result = agent.decide(context: {})
|
|
223
|
-
|
|
224
|
-
expect(result.decision).to eq("reject")
|
|
225
|
-
expect(result.explanations.join(" ")).to include("Conflicting evaluations")
|
|
226
|
-
end
|
|
227
|
-
|
|
228
|
-
it "includes conflicting evaluations in explanations" do
|
|
229
|
-
evaluator1 = DecisionAgent::Evaluators::StaticEvaluator.new(
|
|
230
|
-
decision: "approve",
|
|
231
|
-
weight: 0.4,
|
|
232
|
-
name: "Evaluator1"
|
|
233
|
-
)
|
|
234
|
-
evaluator2 = DecisionAgent::Evaluators::StaticEvaluator.new(
|
|
235
|
-
decision: "reject",
|
|
236
|
-
weight: 0.7,
|
|
237
|
-
name: "Evaluator2"
|
|
238
|
-
)
|
|
239
|
-
|
|
240
|
-
agent = DecisionAgent::Agent.new(evaluators: [evaluator1, evaluator2])
|
|
241
|
-
|
|
242
|
-
result = agent.decide(context: {})
|
|
243
|
-
|
|
244
|
-
explanations_text = result.explanations.join(" ")
|
|
245
|
-
expect(explanations_text).to include("Evaluator1")
|
|
246
|
-
expect(explanations_text).to include("Evaluator2")
|
|
247
|
-
end
|
|
248
|
-
end
|
|
249
|
-
|
|
250
|
-
describe "multiple evaluators agreeing" do
|
|
251
|
-
it "combines evaluations when all agree" do
|
|
252
|
-
evaluator1 = DecisionAgent::Evaluators::StaticEvaluator.new(
|
|
253
|
-
decision: "approve",
|
|
254
|
-
weight: 0.6,
|
|
255
|
-
name: "Evaluator1"
|
|
256
|
-
)
|
|
257
|
-
evaluator2 = DecisionAgent::Evaluators::StaticEvaluator.new(
|
|
258
|
-
decision: "approve",
|
|
259
|
-
weight: 0.8,
|
|
260
|
-
name: "Evaluator2"
|
|
261
|
-
)
|
|
262
|
-
|
|
263
|
-
agent = DecisionAgent::Agent.new(evaluators: [evaluator1, evaluator2])
|
|
264
|
-
|
|
265
|
-
result = agent.decide(context: {})
|
|
266
|
-
|
|
267
|
-
expect(result.decision).to eq("approve")
|
|
268
|
-
expect(result.confidence).to be > 0.5
|
|
269
|
-
end
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
describe "graceful error handling" do
|
|
273
|
-
it "ignores evaluators that raise errors" do
|
|
274
|
-
good_evaluator = DecisionAgent::Evaluators::StaticEvaluator.new(decision: "approve")
|
|
275
|
-
|
|
276
|
-
bad_evaluator = Class.new(DecisionAgent::Evaluators::Base) do
|
|
277
|
-
def evaluate(_context, feedback: {})
|
|
278
|
-
raise StandardError, "Intentional error"
|
|
279
|
-
end
|
|
280
|
-
end
|
|
281
|
-
|
|
282
|
-
agent = DecisionAgent::Agent.new(evaluators: [bad_evaluator.new, good_evaluator])
|
|
283
|
-
|
|
284
|
-
result = agent.decide(context: {})
|
|
285
|
-
|
|
286
|
-
expect(result.decision).to eq("approve")
|
|
287
|
-
end
|
|
288
|
-
end
|
|
289
|
-
end
|