Module: Legion::MCP::PatternStore
- Extended by:
- Logging::Helper
- Defined in:
- lib/legion/mcp/pattern_store.rb
Overview
rubocop:disable Metrics/ModuleLength
Constant Summary collapse
- CONFIDENCE_SUCCESS_DELTA =
0.02- CONFIDENCE_FAILURE_DELTA =
-0.05
- SEEDED_CONFIDENCE =
0.5- DECAY_ARCHIVE_THRESHOLD =
0.1
Class Method Summary collapse
- .archive_l2(intent_hash) ⇒ Object
- .candidates ⇒ Object
- .candidates_buffer ⇒ Object
- .candidates_mutex ⇒ Object
- .cosine_similarity(vec_a, vec_b) ⇒ Object
- .decay_all(factor: 0.998) ⇒ Object
- .deserialize_pattern(row) ⇒ Object
- .empty? ⇒ Boolean
- .ensure_local_table ⇒ Object
- .hydrate_from_l2 ⇒ Object
- .learn_response_template(intent_hash, result_data, threshold: 3, request_id: nil) ⇒ Object
- .local_db_available? ⇒ Boolean
-
.lookup(intent_hash, request_id: nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
- .lookup_l1(intent_hash) ⇒ Object
- .lookup_l2(intent_hash) ⇒ Object
- .lookup_semantic(intent_vector, threshold: 0.85, request_id: nil) ⇒ Object
- .mutex ⇒ Object
-
.pattern_exists?(intent_hash) ⇒ Boolean
— Private helpers —.
- .patterns ⇒ Object
- .patterns_l0 ⇒ Object
-
.persist_l1(intent_hash, pattern) ⇒ Object
— L1: Cache (optional) —.
-
.persist_l2(intent_hash, pattern) ⇒ Object
— L2: Data::Local SQLite (optional) —.
- .promote_candidate(intent_hash:, tool_chain:, intent_text:, intent_vector: nil, request_id: nil) ⇒ Object
-
.record_candidate(intent_hash:, tool_chain:, intent_text:, threshold: 3, request_id: nil) ⇒ Object
rubocop:disable Metrics/MethodLength.
- .record_hit(intent_hash, request_id: nil) ⇒ Object
- .record_miss(intent_hash, request_id: nil) ⇒ Object
- .reset! ⇒ Object
- .serialize_pattern(pattern) ⇒ Object
- .size ⇒ Object
- .stats ⇒ Object
- .store(pattern = nil, request_id: nil, **attrs) ⇒ Object
- .sync_all_to_persistence ⇒ Object
- .sync_to_persistence(intent_hash) ⇒ Object
- .template_mutex ⇒ Object
- .template_observations ⇒ Object
Class Method Details
.archive_l2(intent_hash) ⇒ Object
393 394 395 396 397 398 399 400 401 402 |
# File 'lib/legion/mcp/pattern_store.rb', line 393 def archive_l2(intent_hash) return unless local_db_available? table = ensure_local_table table.where(intent_hash: intent_hash).delete rescue StandardError => e handle_exception(e, level: :warn, operation: 'legion.mcp.pattern_store.archive_l2') log.warn("PatternStore#archive_l2 failed: #{e.}") nil end |
.candidates ⇒ Object
213 214 215 |
# File 'lib/legion/mcp/pattern_store.rb', line 213 def candidates candidates_mutex.synchronize { candidates_buffer.dup } end |
.candidates_buffer ⇒ Object
475 476 477 |
# File 'lib/legion/mcp/pattern_store.rb', line 475 def candidates_buffer @candidates_buffer ||= {} end |
.candidates_mutex ⇒ Object
479 480 481 |
# File 'lib/legion/mcp/pattern_store.rb', line 479 def candidates_mutex @candidates_mutex ||= Mutex.new end |
.cosine_similarity(vec_a, vec_b) ⇒ Object
321 322 323 324 325 326 327 328 329 330 |
# File 'lib/legion/mcp/pattern_store.rb', line 321 def cosine_similarity(vec_a, vec_b) return 0.0 if vec_a.nil? || vec_b.nil? || vec_a.empty? || vec_b.empty? dot = vec_a.zip(vec_b).sum { |a, b| a * b } mag_a = Math.sqrt(vec_a.sum { |x| x**2 }) mag_b = Math.sqrt(vec_b.sum { |x| x**2 }) return 0.0 if mag_a.zero? || mag_b.zero? dot / (mag_a * mag_b) end |
.decay_all(factor: 0.998) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/legion/mcp/pattern_store.rb', line 277 def decay_all(factor: 0.998) archived = [] mutex.synchronize do patterns_l0.each do |hash, pattern| pattern[:confidence] = (pattern[:confidence] * factor).clamp(0.0, 1.0) archived << hash if pattern[:confidence] < DECAY_ARCHIVE_THRESHOLD end archived.each { |hash| patterns_l0.delete(hash) } end archived.each { |hash| archive_l2(hash) } sync_all_to_persistence unless archived.empty? end |
.deserialize_pattern(row) ⇒ Object
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/legion/mcp/pattern_store.rb', line 451 def deserialize_pattern(row) { intent_hash: row[:intent_hash], intent_text: row[:intent_text], intent_vector: row[:intent_vector] ? ::JSON.parse(row[:intent_vector]) : nil, tool_chain: ::JSON.parse(row[:tool_chain]), response_template: row[:response_template], confidence: row[:confidence], hit_count: row[:hit_count], miss_count: row[:miss_count], last_hit_at: row[:last_hit_at], created_at: row[:created_at], context_requirements: row[:context_requirements] ? ::JSON.parse(row[:context_requirements]) : nil } end |
.empty? ⇒ Boolean
225 226 227 |
# File 'lib/legion/mcp/pattern_store.rb', line 225 def empty? size.zero? end |
.ensure_local_table ⇒ Object
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/legion/mcp/pattern_store.rb', line 414 def ensure_local_table db = Legion::Data::Local.connection unless db.table_exists?(:tbi_patterns) db.create_table(:tbi_patterns) do primary_key :id String :intent_hash, null: false, unique: true String :intent_text, text: true String :intent_vector, text: true String :tool_chain, text: true, null: false String :response_template, text: true Float :confidence, default: 0.5 Integer :hit_count, default: 0 Integer :miss_count, default: 0 DateTime :last_hit_at DateTime :created_at String :context_requirements, text: true end end db[:tbi_patterns] end |
.hydrate_from_l2 ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/legion/mcp/pattern_store.rb', line 291 def hydrate_from_l2 return unless local_db_available? table = ensure_local_table loaded = 0 table.each do |row| pattern = deserialize_pattern(row) mutex.synchronize { patterns_l0[pattern[:intent_hash]] = pattern } persist_l1(pattern[:intent_hash], pattern) loaded += 1 end LoggingSupport.info('pattern.hydrate.complete', source: :l2, loaded: loaded) rescue StandardError => e handle_exception(e, level: :warn, operation: 'legion.mcp.pattern_store.hydrate_from_l2') LoggingSupport.warn('pattern.hydrate.failed', source: :l2, error: e.) nil end |
.learn_response_template(intent_hash, result_data, threshold: 3, request_id: nil) ⇒ Object
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 |
# File 'lib/legion/mcp/pattern_store.rb', line 249 def learn_response_template(intent_hash, result_data, threshold: 3, request_id: nil) return unless result_data.is_a?(Hash) template_mutex.synchronize do buffer = template_observations[intent_hash] ||= [] buffer << result_data.keys.sort buffer.shift if buffer.size > 10 return unless buffer.size >= threshold if buffer.last(threshold).uniq.size == 1 keys = buffer.last.sort template = keys.map { |k| "#{k}: {{#{k}}}" }.join(', ') mutex.synchronize do pattern = patterns_l0[intent_hash] pattern[:response_template] = template if pattern end sync_to_persistence(intent_hash) LoggingSupport.info( 'pattern.template.learned', request_id: request_id, intent_hash: intent_hash&.[](0, 12), template: LoggingSupport.summarize_text(template) ) end end end |
.local_db_available? ⇒ Boolean
408 409 410 411 412 |
# File 'lib/legion/mcp/pattern_store.rb', line 408 def local_db_available? defined?(Legion::Data::Local) && Legion::Data::Local.respond_to?(:connected?) && Legion::Data::Local.connected? end |
.lookup(intent_hash, request_id: nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/legion/mcp/pattern_store.rb', line 34 def lookup(intent_hash, request_id: nil) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength result = mutex.synchronize { patterns_l0[intent_hash]&.dup } if result LoggingSupport.info( 'pattern.lookup', request_id: request_id, source: :l0, intent_hash: intent_hash&.[](0, 12), confidence: result[:confidence]&.round(3), tool_chain: Array(result[:tool_chain]) ) return result end result = lookup_l1(intent_hash) if result mutex.synchronize { patterns_l0[intent_hash] = result } LoggingSupport.info( 'pattern.lookup', request_id: request_id, source: :l1, intent_hash: intent_hash&.[](0, 12), confidence: result[:confidence]&.round(3), tool_chain: Array(result[:tool_chain]) ) return result.dup end result = lookup_l2(intent_hash) if result mutex.synchronize { patterns_l0[intent_hash] = result } persist_l1(intent_hash, result) LoggingSupport.info( 'pattern.lookup', request_id: request_id, source: :l2, intent_hash: intent_hash&.[](0, 12), confidence: result[:confidence]&.round(3), tool_chain: Array(result[:tool_chain]) ) return result.dup end LoggingSupport.info('pattern.lookup', request_id: request_id, source: :miss, intent_hash: intent_hash&.[](0, 12)) nil end |
.lookup_l1(intent_hash) ⇒ Object
344 345 346 347 348 349 350 351 352 353 |
# File 'lib/legion/mcp/pattern_store.rb', line 344 def lookup_l1(intent_hash) return nil unless defined?(Legion::Cache) && Legion::Cache.respond_to?(:connected?) && Legion::Cache.connected? raw = Legion::Cache.get("tbi:pattern:#{intent_hash}") raw ? Legion::JSON.load(raw) : nil rescue StandardError => e handle_exception(e, level: :warn, operation: 'legion.mcp.pattern_store.lookup_l1') log.warn("PatternStore#lookup_l1 failed: #{e.}") nil end |
.lookup_l2(intent_hash) ⇒ Object
373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/legion/mcp/pattern_store.rb', line 373 def lookup_l2(intent_hash) return nil unless local_db_available? table = ensure_local_table row = table.where(intent_hash: intent_hash).first row ? deserialize_pattern(row) : nil rescue StandardError => e handle_exception(e, level: :warn, operation: 'legion.mcp.pattern_store.lookup_l2') log.warn("PatternStore#lookup_l2 failed: #{e.}") nil end |
.lookup_semantic(intent_vector, threshold: 0.85, request_id: nil) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/legion/mcp/pattern_store.rb', line 82 def lookup_semantic(intent_vector, threshold: 0.85, request_id: nil) return nil unless intent_vector && !patterns_l0.empty? best_hash = nil best_score = 0.0 mutex.synchronize do patterns_l0.each do |hash, pattern| next unless pattern[:intent_vector] score = cosine_similarity(intent_vector, pattern[:intent_vector]) if score > best_score && score >= threshold best_score = score best_hash = hash end end end LoggingSupport.info( 'pattern.semantic_lookup', request_id: request_id, matched: !best_hash.nil?, best_score: best_score.round(4), threshold: threshold, intent_hash: best_hash&.[](0, 12) ) best_hash ? lookup(best_hash, request_id: request_id) : nil end |
.mutex ⇒ Object
471 472 473 |
# File 'lib/legion/mcp/pattern_store.rb', line 471 def mutex @mutex ||= Mutex.new end |
.pattern_exists?(intent_hash) ⇒ Boolean
— Private helpers —
317 318 319 |
# File 'lib/legion/mcp/pattern_store.rb', line 317 def pattern_exists?(intent_hash) mutex.synchronize { patterns_l0.key?(intent_hash) } end |
.patterns ⇒ Object
217 218 219 |
# File 'lib/legion/mcp/pattern_store.rb', line 217 def patterns mutex.synchronize { patterns_l0.dup } end |
.patterns_l0 ⇒ Object
467 468 469 |
# File 'lib/legion/mcp/pattern_store.rb', line 467 def patterns_l0 @patterns_l0 ||= {} end |
.persist_l1(intent_hash, pattern) ⇒ Object
— L1: Cache (optional) —
334 335 336 337 338 339 340 341 342 |
# File 'lib/legion/mcp/pattern_store.rb', line 334 def persist_l1(intent_hash, pattern) return unless defined?(Legion::Cache) && Legion::Cache.respond_to?(:connected?) && Legion::Cache.connected? Legion::Cache.set("tbi:pattern:#{intent_hash}", Legion::JSON.dump(pattern), 3600) rescue StandardError => e handle_exception(e, level: :warn, operation: 'legion.mcp.pattern_store.persist_l1') log.warn("PatternStore#persist_l1 failed: #{e.}") nil end |
.persist_l2(intent_hash, pattern) ⇒ Object
— L2: Data::Local SQLite (optional) —
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/legion/mcp/pattern_store.rb', line 357 def persist_l2(intent_hash, pattern) return unless local_db_available? table = ensure_local_table data = serialize_pattern(pattern) if table.where(intent_hash: intent_hash).first table.where(intent_hash: intent_hash).update(data) else table.insert(data) end rescue StandardError => e handle_exception(e, level: :warn, operation: 'legion.mcp.pattern_store.persist_l2') log.warn("PatternStore#persist_l2 failed: #{e.}") nil end |
.promote_candidate(intent_hash:, tool_chain:, intent_text:, intent_vector: nil, request_id: nil) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/legion/mcp/pattern_store.rb', line 151 def promote_candidate(intent_hash:, tool_chain:, intent_text:, intent_vector: nil, request_id: nil) pattern = { intent_hash: intent_hash, intent_text: intent_text, intent_vector: intent_vector, tool_chain: tool_chain, response_template: nil, confidence: SEEDED_CONFIDENCE, hit_count: 0, miss_count: 0, last_hit_at: nil, created_at: Time.now, context_requirements: nil } store(pattern, request_id: request_id) candidates_mutex.synchronize { candidates_buffer.delete(intent_hash) } LoggingSupport.info( 'pattern.promoted', request_id: request_id, intent_hash: intent_hash&.[](0, 12), intent: intent_text, tool_chain: Array(tool_chain) ) pattern end |
.record_candidate(intent_hash:, tool_chain:, intent_text:, threshold: 3, request_id: nil) ⇒ Object
rubocop:disable Metrics/MethodLength
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/legion/mcp/pattern_store.rb', line 177 def record_candidate(intent_hash:, tool_chain:, intent_text:, threshold: 3, request_id: nil) # rubocop:disable Metrics/MethodLength candidates_mutex.synchronize do # rubocop:disable Metrics/BlockLength entry = candidates_buffer[intent_hash] ||= { intent_text: intent_text, tool_chain: tool_chain, count: 0 } entry[:count] += 1 if entry[:count] == 1 LoggingSupport.info( 'pattern.candidate.recorded', request_id: request_id, intent_hash: intent_hash&.[](0, 12), intent: intent_text, tool_chain: Array(tool_chain), count: entry[:count], threshold: threshold ) end if entry[:count] >= threshold && !pattern_exists?(intent_hash) candidates_buffer.delete(intent_hash) LoggingSupport.info( 'pattern.candidate.threshold_met', request_id: request_id, intent_hash: intent_hash&.[](0, 12), intent: intent_text, tool_chain: Array(tool_chain), count: entry[:count], threshold: threshold ) return { promote: true, intent_hash: intent_hash, tool_chain: tool_chain, intent_text: intent_text } end end nil end |
.record_hit(intent_hash, request_id: nil) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/legion/mcp/pattern_store.rb', line 111 def record_hit(intent_hash, request_id: nil) mutex.synchronize do pattern = patterns_l0[intent_hash] return unless pattern pattern[:hit_count] = (pattern[:hit_count] || 0) + 1 pattern[:miss_count] = 0 pattern[:last_hit_at] = Time.now pattern[:confidence] = (pattern[:confidence] + CONFIDENCE_SUCCESS_DELTA).clamp(0.0, 1.0) end sync_to_persistence(intent_hash) pattern = mutex.synchronize { patterns_l0[intent_hash]&.dup } LoggingSupport.info( 'pattern.hit', request_id: request_id, intent_hash: intent_hash&.[](0, 12), confidence: pattern&.dig(:confidence)&.round(3), hit_count: pattern&.dig(:hit_count) ) end |
.record_miss(intent_hash, request_id: nil) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/legion/mcp/pattern_store.rb', line 132 def record_miss(intent_hash, request_id: nil) mutex.synchronize do pattern = patterns_l0[intent_hash] return unless pattern pattern[:miss_count] = (pattern[:miss_count] || 0) + 1 pattern[:confidence] = (pattern[:confidence] + CONFIDENCE_FAILURE_DELTA).clamp(0.0, 1.0) end sync_to_persistence(intent_hash) pattern = mutex.synchronize { patterns_l0[intent_hash]&.dup } LoggingSupport.info( 'pattern.miss', request_id: request_id, intent_hash: intent_hash&.[](0, 12), confidence: pattern&.dig(:confidence)&.round(3), miss_count: pattern&.dig(:miss_count) ) end |
.reset! ⇒ Object
309 310 311 312 313 |
# File 'lib/legion/mcp/pattern_store.rb', line 309 def reset! mutex.synchronize { patterns_l0.clear } candidates_mutex.synchronize { candidates_buffer.clear } template_mutex.synchronize { template_observations.clear } end |
.serialize_pattern(pattern) ⇒ Object
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/legion/mcp/pattern_store.rb', line 435 def serialize_pattern(pattern) { intent_hash: pattern[:intent_hash], intent_text: pattern[:intent_text], intent_vector: pattern[:intent_vector] ? ::JSON.dump(pattern[:intent_vector]) : nil, tool_chain: ::JSON.dump(pattern[:tool_chain]), response_template: pattern[:response_template], confidence: pattern[:confidence], hit_count: pattern[:hit_count], miss_count: pattern[:miss_count], last_hit_at: pattern[:last_hit_at], created_at: pattern[:created_at], context_requirements: pattern[:context_requirements]&.then { |c| ::JSON.dump(c) } } end |
.size ⇒ Object
221 222 223 |
# File 'lib/legion/mcp/pattern_store.rb', line 221 def size mutex.synchronize { patterns_l0.size } end |
.stats ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/legion/mcp/pattern_store.rb', line 229 def stats total_hits = 0 total_conf = 0.0 count = 0 mutex.synchronize do patterns_l0.each_value do |p| total_hits += p[:hit_count] || 0 total_conf += p[:confidence] || 0.0 count += 1 end end { size: count, hit_rate: count.positive? ? (total_hits.to_f / [count, 1].max).round(2) : 0.0, avg_confidence: count.positive? ? (total_conf / count).round(4) : 0.0 } end |
.store(pattern = nil, request_id: nil, **attrs) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/legion/mcp/pattern_store.rb', line 18 def store(pattern = nil, request_id: nil, **attrs) pattern = (pattern || {}).merge(attrs) hash = pattern[:intent_hash] mutex.synchronize { patterns_l0[hash] = pattern.dup } persist_l1(hash, pattern) persist_l2(hash, pattern) LoggingSupport.info( 'pattern.store', request_id: request_id, intent_hash: hash&.[](0, 12), intent: pattern[:intent_text], confidence: pattern[:confidence]&.round(3), tool_chain: Array(pattern[:tool_chain]) ) end |
.sync_all_to_persistence ⇒ Object
404 405 406 |
# File 'lib/legion/mcp/pattern_store.rb', line 404 def sync_all_to_persistence mutex.synchronize { patterns_l0.keys.dup }.each { |h| sync_to_persistence(h) } end |
.sync_to_persistence(intent_hash) ⇒ Object
385 386 387 388 389 390 391 |
# File 'lib/legion/mcp/pattern_store.rb', line 385 def sync_to_persistence(intent_hash) pattern = mutex.synchronize { patterns_l0[intent_hash]&.dup } return unless pattern persist_l1(intent_hash, pattern) persist_l2(intent_hash, pattern) end |
.template_mutex ⇒ Object
487 488 489 |
# File 'lib/legion/mcp/pattern_store.rb', line 487 def template_mutex @template_mutex ||= Mutex.new end |
.template_observations ⇒ Object
483 484 485 |
# File 'lib/legion/mcp/pattern_store.rb', line 483 def template_observations @template_observations ||= {} end |