Module: Legion::MCP::Patterns::Store
- Extended by:
- Logging::Helper
- Defined in:
- lib/legion/mcp/patterns/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
- .evict_l1(intent_hash) ⇒ 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
- .mcp_log(level, event, **fields) ⇒ 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, candidate_key: nil, request_id: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists.
-
.record_candidate(intent_hash:, tool_chain:, intent_text:, candidate_key: nil, threshold: 3, request_id: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists.
- .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
370 371 372 373 374 375 376 377 378 |
# File 'lib/legion/mcp/patterns/store.rb', line 370 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') nil end |
.candidates ⇒ Object
184 185 186 |
# File 'lib/legion/mcp/patterns/store.rb', line 184 def candidates candidates_mutex.synchronize { candidates_buffer.dup } end |
.candidates_buffer ⇒ Object
451 452 453 |
# File 'lib/legion/mcp/patterns/store.rb', line 451 def candidates_buffer @candidates_buffer ||= {} end |
.candidates_mutex ⇒ Object
455 456 457 |
# File 'lib/legion/mcp/patterns/store.rb', line 455 def candidates_mutex @candidates_mutex ||= Mutex.new end |
.cosine_similarity(vec_a, vec_b) ⇒ Object
293 294 295 296 297 298 299 300 301 302 |
# File 'lib/legion/mcp/patterns/store.rb', line 293 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
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/legion/mcp/patterns/store.rb', line 245 def decay_all(factor: 0.998) log.debug("[mcp][pattern_store] action=decay_all factor=#{factor} patterns=#{size}") 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 do |hash| archive_l2(hash) evict_l1(hash) end sync_all_to_persistence log.debug("[mcp][pattern_store] action=decay_all.complete archived=#{archived.size} remaining=#{size}") end |
.deserialize_pattern(row) ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/legion/mcp/patterns/store.rb', line 427 def deserialize_pattern(row) { intent_hash: row[:intent_hash], intent_text: row[:intent_text], intent_vector: row[:intent_vector] ? Legion::JSON.load(row[:intent_vector]) : nil, tool_chain: Legion::JSON.load(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] ? Legion::JSON.load(row[:context_requirements]) : nil } end |
.empty? ⇒ Boolean
196 197 198 |
# File 'lib/legion/mcp/patterns/store.rb', line 196 def empty? size.zero? end |
.ensure_local_table ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/legion/mcp/patterns/store.rb', line 390 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 |
.evict_l1(intent_hash) ⇒ Object
315 316 317 318 319 320 321 322 |
# File 'lib/legion/mcp/patterns/store.rb', line 315 def evict_l1(intent_hash) return unless defined?(Legion::Cache) && Legion::Cache.respond_to?(:connected?) && Legion::Cache.connected? Legion::Cache.delete("tbi:pattern:#{intent_hash}") rescue StandardError => e handle_exception(e, level: :warn, operation: 'legion.mcp.pattern_store.evict_l1') nil end |
.hydrate_from_l2 ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/legion/mcp/patterns/store.rb', line 264 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 mcp_log :info, 'pattern.hydrate.complete', source: :l2, loaded: loaded rescue StandardError => e handle_exception(e, level: :warn, operation: 'legion.mcp.pattern_store.hydrate_from_l2') nil end |
.learn_response_template(intent_hash, result_data, threshold: 3, request_id: nil) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/legion/mcp/patterns/store.rb', line 220 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) mcp_log :info, 'pattern.template.learned', request_id: request_id, intent_hash: intent_hash&.[](0, 12), template: Utils.summarize_text(template) end end end |
.local_db_available? ⇒ Boolean
384 385 386 387 388 |
# File 'lib/legion/mcp/patterns/store.rb', line 384 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
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 |
# File 'lib/legion/mcp/patterns/store.rb', line 35 def lookup(intent_hash, request_id: nil) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength result = mutex.synchronize { patterns_l0[intent_hash]&.dup } if result mcp_log :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 } mcp_log :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) mcp_log :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 mcp_log :info, 'pattern.lookup', request_id: request_id, source: :miss, intent_hash: intent_hash&.[](0, 12) nil end |
.lookup_l1(intent_hash) ⇒ Object
324 325 326 327 328 329 330 331 332 |
# File 'lib/legion/mcp/patterns/store.rb', line 324 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') nil end |
.lookup_l2(intent_hash) ⇒ Object
351 352 353 354 355 356 357 358 359 360 |
# File 'lib/legion/mcp/patterns/store.rb', line 351 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') nil end |
.lookup_semantic(intent_vector, threshold: 0.85, request_id: nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/legion/mcp/patterns/store.rb', line 75 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 mcp_log :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 |
.mcp_log(level, event, **fields) ⇒ Object
18 19 20 |
# File 'lib/legion/mcp/patterns/store.rb', line 18 def mcp_log(level, event, **fields) log.public_send(level, "[mcp] #{event} #{Utils.format_fields(fields)}") end |
.mutex ⇒ Object
447 448 449 |
# File 'lib/legion/mcp/patterns/store.rb', line 447 def mutex @mutex ||= Mutex.new end |
.pattern_exists?(intent_hash) ⇒ Boolean
— Private helpers —
289 290 291 |
# File 'lib/legion/mcp/patterns/store.rb', line 289 def pattern_exists?(intent_hash) mutex.synchronize { patterns_l0.key?(intent_hash) } end |
.patterns ⇒ Object
188 189 190 |
# File 'lib/legion/mcp/patterns/store.rb', line 188 def patterns mutex.synchronize { patterns_l0.dup } end |
.patterns_l0 ⇒ Object
443 444 445 |
# File 'lib/legion/mcp/patterns/store.rb', line 443 def patterns_l0 @patterns_l0 ||= {} end |
.persist_l1(intent_hash, pattern) ⇒ Object
— L1: Cache (optional) —
306 307 308 309 310 311 312 313 |
# File 'lib/legion/mcp/patterns/store.rb', line 306 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') nil end |
.persist_l2(intent_hash, pattern) ⇒ Object
— L2: Data::Local SQLite (optional) —
336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/legion/mcp/patterns/store.rb', line 336 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') nil end |
.promote_candidate(intent_hash:, tool_chain:, intent_text:, intent_vector: nil, candidate_key: nil, request_id: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/legion/mcp/patterns/store.rb', line 134 def promote_candidate(intent_hash:, tool_chain:, intent_text:, intent_vector: nil, candidate_key: nil, request_id: nil) # rubocop:disable Metrics/ParameterLists 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) buf_key = candidate_key || intent_hash candidates_mutex.synchronize { candidates_buffer.delete(buf_key) } mcp_log :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:, candidate_key: nil, threshold: 3, request_id: nil) ⇒ Object
rubocop:disable Metrics/ParameterLists
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/legion/mcp/patterns/store.rb', line 157 def record_candidate(intent_hash:, tool_chain:, intent_text:, candidate_key: nil, threshold: 3, request_id: nil) # rubocop:disable Metrics/ParameterLists buf_key = candidate_key || intent_hash candidates_mutex.synchronize do entry = candidates_buffer[buf_key] ||= { intent_text: intent_text, tool_chain: tool_chain, count: 0 } entry[:count] += 1 if entry[:count] == 1 mcp_log :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(buf_key) mcp_log :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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/legion/mcp/patterns/store.rb', line 100 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 } mcp_log :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
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/legion/mcp/patterns/store.rb', line 118 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 } mcp_log :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
281 282 283 284 285 |
# File 'lib/legion/mcp/patterns/store.rb', line 281 def reset! mutex.synchronize { patterns_l0.clear } candidates_mutex.synchronize { candidates_buffer.clear } template_mutex.synchronize { template_observations.clear } end |
.serialize_pattern(pattern) ⇒ Object
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/legion/mcp/patterns/store.rb', line 411 def serialize_pattern(pattern) { intent_hash: pattern[:intent_hash], intent_text: pattern[:intent_text], intent_vector: pattern[:intent_vector] ? Legion::JSON.dump(pattern[:intent_vector]) : nil, tool_chain: Legion::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| Legion::JSON.dump(c) } } end |
.size ⇒ Object
192 193 194 |
# File 'lib/legion/mcp/patterns/store.rb', line 192 def size mutex.synchronize { patterns_l0.size } end |
.stats ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/legion/mcp/patterns/store.rb', line 200 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
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/legion/mcp/patterns/store.rb', line 22 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) mcp_log :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
380 381 382 |
# File 'lib/legion/mcp/patterns/store.rb', line 380 def sync_all_to_persistence mutex.synchronize { patterns_l0.keys.dup }.each { |h| sync_to_persistence(h) } end |
.sync_to_persistence(intent_hash) ⇒ Object
362 363 364 365 366 367 368 |
# File 'lib/legion/mcp/patterns/store.rb', line 362 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
463 464 465 |
# File 'lib/legion/mcp/patterns/store.rb', line 463 def template_mutex @template_mutex ||= Mutex.new end |
.template_observations ⇒ Object
459 460 461 |
# File 'lib/legion/mcp/patterns/store.rb', line 459 def template_observations @template_observations ||= {} end |