Class: KairosMcp::DslAst::AstEngine
- Inherits:
-
Object
- Object
- KairosMcp::DslAst::AstEngine
- Defined in:
- lib/kairos_mcp/dsl_ast/ast_engine.rb
Overview
AST node evaluation engine Verifies definition nodes against a binding context without using eval. Design: verification-only — does not replace behavior block execution. NOTE: The engine itself is infrastructure in Phase 2. Policy aspects (drift thresholds, etc.) may become a SkillSet in Phase 3.
Constant Summary collapse
- ALLOWED_METHODS =
Allowed methods for condition evaluation via send(). Only query methods (no side effects) are permitted.
%i[ can_evolve? has_tool? include? key? empty? nil? is_a? respond_to? size length count ].freeze
Class Method Summary collapse
-
.evaluate_check(node, binding_context) ⇒ Object
Evaluate Check node.
-
.evaluate_constraint(node, binding_context) ⇒ Object
Evaluate Constraint node via pattern matching (no eval).
-
.evaluate_node(node, binding_context: {}) ⇒ NodeResult
Evaluate a single AST node.
-
.evaluate_plan(node) ⇒ Object
Evaluate Plan node — structural validity (steps exist and are named).
-
.evaluate_semantic_reasoning(node) ⇒ Object
SemanticReasoning — explicitly non-evaluable (requires human judgment).
-
.evaluate_tool_call(node) ⇒ Object
Evaluate ToolCall node — command recognition (does not execute).
-
.match_condition(condition, binding_context) ⇒ Object
Pattern-match a condition string against binding_context Supported patterns: “X == true/false” — boolean comparison “X == VALUE” — equality comparison “X < Y” / “X > Y” / “X >= Y” / “X <= Y” — numeric comparison “X.method?(arg)” — method call on context object “X not in Y” — exclusion check Unsupported patterns return evaluable: false.
-
.verify(skill, binding_context: {}) ⇒ VerificationReport
Verify all definition nodes for a skill.
Class Method Details
.evaluate_check(node, binding_context) ⇒ Object
Evaluate Check node
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/kairos_mcp/dsl_ast/ast_engine.rb', line 132 def self.evaluate_check(node, binding_context) opts = node. || {} condition = opts[:condition] unless condition return NodeResult.new( node_name: node.name, node_type: :Check, satisfied: :unknown, detail: "No condition specified", evaluable: false ) end result = match_condition(condition, binding_context) NodeResult.new( node_name: node.name, node_type: :Check, satisfied: result[:satisfied], detail: result[:detail], evaluable: result[:evaluable] ) end |
.evaluate_constraint(node, binding_context) ⇒ Object
Evaluate Constraint node via pattern matching (no eval)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/kairos_mcp/dsl_ast/ast_engine.rb', line 94 def self.evaluate_constraint(node, binding_context) opts = node. || {} condition = opts[:condition] if condition.nil? # No condition string — check if required key exists in context if opts[:required] == true # Constraint is a declaration; structurally valid return NodeResult.new( node_name: node.name, node_type: :Constraint, satisfied: true, detail: "Required constraint declared (structural)", evaluable: true ) end return NodeResult.new( node_name: node.name, node_type: :Constraint, satisfied: true, detail: "Constraint declared (no condition to evaluate)", evaluable: true ) end # Pattern match the condition string result = match_condition(condition, binding_context) NodeResult.new( node_name: node.name, node_type: :Constraint, satisfied: result[:satisfied], detail: result[:detail], evaluable: result[:evaluable] ) end |
.evaluate_node(node, binding_context: {}) ⇒ NodeResult
Evaluate a single AST node
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/kairos_mcp/dsl_ast/ast_engine.rb', line 68 def self.evaluate_node(node, binding_context: {}) case node.type when :Constraint evaluate_constraint(node, binding_context) when :Check evaluate_check(node, binding_context) when :Plan evaluate_plan(node) when :ToolCall evaluate_tool_call(node) when :SemanticReasoning evaluate_semantic_reasoning(node) else NodeResult.new( node_name: node.name, node_type: node.type, satisfied: :unknown, detail: "Unknown node type: #{node.type}", evaluable: false ) end end |
.evaluate_plan(node) ⇒ Object
Evaluate Plan node — structural validity (steps exist and are named)
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/kairos_mcp/dsl_ast/ast_engine.rb', line 157 def self.evaluate_plan(node) opts = node. || {} steps = opts[:steps] unless steps.is_a?(Array) && !steps.empty? return NodeResult.new( node_name: node.name, node_type: :Plan, satisfied: false, detail: "Plan has no steps defined", evaluable: true ) end NodeResult.new( node_name: node.name, node_type: :Plan, satisfied: true, detail: "Plan has #{steps.size} steps: #{steps.map(&:to_s).join(' -> ')}", evaluable: true ) end |
.evaluate_semantic_reasoning(node) ⇒ Object
SemanticReasoning — explicitly non-evaluable (requires human judgment)
205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/kairos_mcp/dsl_ast/ast_engine.rb', line 205 def self.evaluate_semantic_reasoning(node) opts = node. || {} prompt = opts[:prompt] || "(no prompt)" NodeResult.new( node_name: node.name, node_type: :SemanticReasoning, satisfied: :unknown, detail: "Requires human judgment: #{prompt}", evaluable: false ) end |
.evaluate_tool_call(node) ⇒ Object
Evaluate ToolCall node — command recognition (does not execute)
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/kairos_mcp/dsl_ast/ast_engine.rb', line 181 def self.evaluate_tool_call(node) opts = node. || {} command = opts[:command] unless command && !command.to_s.strip.empty? return NodeResult.new( node_name: node.name, node_type: :ToolCall, satisfied: false, detail: "No command specified", evaluable: true ) end NodeResult.new( node_name: node.name, node_type: :ToolCall, satisfied: true, detail: "Command recognized: #{command}", evaluable: true ) end |
.match_condition(condition, binding_context) ⇒ Object
Pattern-match a condition string against binding_context Supported patterns:
"X == true/false" — boolean comparison
"X == VALUE" — equality comparison
"X < Y" / "X > Y" / "X >= Y" / "X <= Y" — numeric comparison
"X.method?(arg)" — method call on context object
"X not in Y" — exclusion check
Unsupported patterns return evaluable: false
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/kairos_mcp/dsl_ast/ast_engine.rb', line 226 def self.match_condition(condition, binding_context) # Pattern: "X == true" or "X == false" if condition =~ /\A(\w+)\s*==\s*(true|false)\z/ var_name = $1.to_sym expected = $2 == 'true' if binding_context.key?(var_name) actual = binding_context[var_name] return { satisfied: actual == expected, detail: "#{var_name}: expected #{expected}, got #{actual}", evaluable: true } else return { satisfied: :unknown, detail: "Variable '#{var_name}' not in binding context", evaluable: false } end end # Pattern: "X < Y" or "X > Y" or "X >= Y" or "X <= Y" if condition =~ /\A(\w+)\s*(<|>|<=|>=)\s*(\w+)\z/ left_name = $1.to_sym op = $2 right_name = $3.to_sym left_val = binding_context.key?(left_name) ? binding_context[left_name] : nil right_val = binding_context.key?(right_name) ? binding_context[right_name] : nil if left_val.nil? || right_val.nil? missing = [] missing << left_name unless binding_context.key?(left_name) missing << right_name unless binding_context.key?(right_name) return { satisfied: :unknown, detail: "Missing variables: #{missing.join(', ')}", evaluable: false } end begin result = case op when '<' then left_val < right_val when '>' then left_val > right_val when '<=' then left_val <= right_val when '>=' then left_val >= right_val end return { satisfied: result, detail: "#{left_name}(#{left_val}) #{op} #{right_name}(#{right_val}) = #{result}", evaluable: true } rescue TypeError, ArgumentError => e return { satisfied: :unknown, detail: "Type error in comparison: #{e.}", evaluable: false } end end # Pattern: "X.method?(arg)" if condition =~ /\A(\w+)\.(\w+\??)\(([^)]*)\)\z/ obj_name = $1.to_sym method_name = $2.to_sym arg_str = $3.strip if binding_context.key?(obj_name) obj = binding_context[obj_name] if obj.respond_to?(method_name) # Security: only allow whitelisted query methods (no side effects) unless ALLOWED_METHODS.include?(method_name) return { satisfied: :unknown, detail: "Method '#{method_name}' not in allowed list", evaluable: false } end # Parse argument: try symbol, then string arg = arg_str.start_with?(':') ? arg_str[1..-1].to_sym : arg_str begin result = obj.send(method_name, arg) return { satisfied: !!result, detail: "#{obj_name}.#{method_name}(#{arg_str}) = #{result}", evaluable: true } rescue StandardError => e return { satisfied: :unknown, detail: "Error calling #{method_name}: #{e.}", evaluable: false } end end end return { satisfied: :unknown, detail: "Cannot evaluate: #{condition}", evaluable: false } end # Pattern: "X not in Y" if condition =~ /\A(\w+)\s+not\s+in\s+(\w+)\z/ item_name = $1.to_sym collection_name = $2.to_sym if binding_context.key?(item_name) && binding_context.key?(collection_name) item = binding_context[item_name] collection = binding_context[collection_name] if collection.respond_to?(:include?) result = !collection.include?(item) return { satisfied: result, detail: "#{item_name} not in #{collection_name}: #{result}", evaluable: true } end end return { satisfied: :unknown, detail: "Cannot evaluate exclusion: #{condition}", evaluable: false } end # Unrecognized pattern { satisfied: :unknown, detail: "Unrecognized condition pattern: #{condition}", evaluable: false } end |
.verify(skill, binding_context: {}) ⇒ VerificationReport
Verify all definition nodes for a skill
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/kairos_mcp/dsl_ast/ast_engine.rb', line 50 def self.verify(skill, binding_context: {}) return nil unless skill.definition results = skill.definition.nodes.map do |node| evaluate_node(node, binding_context: binding_context) end VerificationReport.new( skill_id: skill.id, results: results, timestamp: Time.now.iso8601 ) end |