Module: Legion::Extensions::Apollo::Runners::Maintenance
- Includes:
- Helpers::Lex
- Included in:
- Client
- Defined in:
- lib/legion/extensions/apollo/runners/maintenance.rb
Instance Method Summary collapse
- #archive_stale(days: 90) ⇒ Object
-
#check_corroboration ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity.
- #force_decay(factor: 0.5) ⇒ Object
- #resolve_dispute(entry_id:, resolution:) ⇒ Object
- #run_decay_cycle(alpha: nil, min_confidence: nil) ⇒ Object
Instance Method Details
#archive_stale(days: 90) ⇒ Object
16 17 18 |
# File 'lib/legion/extensions/apollo/runners/maintenance.rb', line 16 def archive_stale(days: 90, **) { action: :archive_stale, days: days } end |
#check_corroboration ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 |
# File 'lib/legion/extensions/apollo/runners/maintenance.rb', line 57 def check_corroboration(**) # rubocop:disable Metrics/CyclomaticComplexity return { success: false, error: 'apollo_data_not_available' } unless defined?(Legion::Data::Model::ApolloEntry) candidates = Legion::Data::Model::ApolloEntry.where(status: 'candidate').exclude(embedding: nil).all confirmed = Legion::Data::Model::ApolloEntry.where(status: 'confirmed').exclude(embedding: nil).all promoted = 0 candidates.each do |candidate| match = confirmed.find do |conf| next unless conf.content_type == candidate.content_type sim = Helpers::Similarity.cosine_similarity( vec_a: candidate., vec_b: conf. ) Helpers::Similarity.above_corroboration_threshold?(similarity: sim) end next unless match candidate_provider = candidate.respond_to?(:source_provider) ? candidate.source_provider : nil match_provider = match.respond_to?(:source_provider) ? match.source_provider : nil both_known = known_provider?(candidate_provider) && known_provider?(match_provider) next if both_known && candidate_provider == match_provider # Also reject if same source_channel (same data pipeline) candidate_channel = candidate.respond_to?(:source_channel) ? candidate.source_channel : nil match_channel = match.respond_to?(:source_channel) ? match.source_channel : nil next if candidate_channel && match_channel && candidate_channel == match_channel candidate.update( status: 'confirmed', confirmed_at: Time.now, confidence: Helpers::Confidence.apply_corroboration_boost(confidence: candidate.confidence), updated_at: Time.now ) Legion::Data::Model::ApolloRelation.create( from_entry_id: candidate.id, to_entry_id: match.id, relation_type: 'similar_to', source_agent: 'system:corroboration', weight: 1.0 ) promoted += 1 end { success: true, promoted: promoted, scanned: candidates.size } rescue Sequel::Error => e { success: false, error: e. } end |
#force_decay(factor: 0.5) ⇒ Object
12 13 14 |
# File 'lib/legion/extensions/apollo/runners/maintenance.rb', line 12 def force_decay(factor: 0.5, **) { action: :force_decay, factor: factor } end |
#resolve_dispute(entry_id:, resolution:) ⇒ Object
20 21 22 |
# File 'lib/legion/extensions/apollo/runners/maintenance.rb', line 20 def resolve_dispute(entry_id:, resolution:, **) { action: :resolve_dispute, entry_id: entry_id, resolution: resolution } end |
#run_decay_cycle(alpha: nil, min_confidence: nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/legion/extensions/apollo/runners/maintenance.rb', line 24 def run_decay_cycle(alpha: nil, min_confidence: nil, **) alpha ||= decay_alpha min_confidence ||= decay_threshold return { decayed: 0, archived: 0 } unless defined?(Legion::Data) && Legion::Data.respond_to?(:connection) && Legion::Data.connection conn = Legion::Data.connection # Power-law: per-cycle decay factor decreases as entries age # Factor = (hours_old / (hours_old + 1)) ^ alpha # Recent entries (small hours_old) get a factor closer to 0 (more decay) # Old entries (large hours_old) get a factor closer to 1 (less decay) hours_expr = Sequel.lit( 'GREATEST(EXTRACT(EPOCH FROM (NOW() - COALESCE(updated_at, created_at))) / 3600.0, 1.0)' ) decay_factor = Sequel.lit( 'POWER(CAST(? AS double precision) / (CAST(? AS double precision) + 1.0), ?)', hours_expr, hours_expr, alpha ) decayed = conn[:apollo_entries] .exclude(status: 'archived') .update(confidence: Sequel[:confidence] * decay_factor) archived = conn[:apollo_entries] .where { confidence < min_confidence } .exclude(status: 'archived') .update(status: 'archived') { decayed: decayed, archived: archived, alpha: alpha, threshold: min_confidence } rescue Sequel::Error => e { decayed: 0, archived: 0, error: e. } end |