Class: SmartBrain::Scopes::ConflictResolver
- Inherits:
-
Object
- Object
- SmartBrain::Scopes::ConflictResolver
- Defined in:
- lib/smart_brain/scopes/conflict_resolver.rb
Constant Summary collapse
- DEFAULT_PRIORITY =
%w[task project expert global session].freeze
Instance Method Summary collapse
-
#initialize(config:) ⇒ ConflictResolver
constructor
A new instance of ConflictResolver.
- #resolve(evidence) ⇒ Object
Constructor Details
#initialize(config:) ⇒ ConflictResolver
Returns a new instance of ConflictResolver.
8 9 10 11 |
# File 'lib/smart_brain/scopes/conflict_resolver.rb', line 8 def initialize(config:) @priority = Array(config.scopes[:conflict_priority] || DEFAULT_PRIORITY).map(&:to_s) @max_items_per_scope = Integer(config.scopes.fetch(:max_items_per_scope, 10)) end |
Instance Method Details
#resolve(evidence) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 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 |
# File 'lib/smart_brain/scopes/conflict_resolver.rb', line 13 def resolve(evidence) selected = [] shadowed = [] keyed, unkeyed = evidence.partition { |item| item[:source] == 'memory' && item[:memory_type] && item[:memory_key] } keyed.group_by { |item| [item[:memory_type], item[:memory_key]] }.each_value do |candidates| winner = candidates.min_by { |item| winner_rank(item) } selected << winner candidates.each do |candidate| next if candidate.equal?(winner) shadowed << candidate.merge( shadow_reason: shadow_reason(winner, candidate), shadowed_by: winner[:id] ) end end selected.concat(unkeyed) budgeted = [] counts = Hash.new(0) selected.sort_by { |item| -item.fetch(:score, 0.0) }.each do |item| scope_key = item[:scope] && "#{item.dig(:scope, :type)}:#{item.dig(:scope, :id)}" if scope_key && counts[scope_key] >= @max_items_per_scope shadowed << item.merge(shadow_reason: 'scope_budget') else counts[scope_key] += 1 if scope_key budgeted << item end end { selected: budgeted, shadowed: shadowed, scope_stats: counts, scope_budget: { max_items_per_scope: @max_items_per_scope } } end |