Class: SwarmSDK::V3::Memory::ExposureTracker
- Inherits:
-
Object
- Object
- SwarmSDK::V3::Memory::ExposureTracker
- Defined in:
- lib/swarm_sdk/v3/memory/exposure_tracker.rb
Overview
Calculates exposure scores for memory cards
Exposure score = α·frequency + β·recency + γ·dwell (weighted sum)
Uses an additive formula so each signal contributes independently. A multiplicative formula would zero out the score whenever any single factor is zero (e.g., a fresh card with access_count=0), which would make every new card an immediate compression candidate.
Used by the compressor to decide which cards get lossy compression first. Low-exposure cards are candidates for compression or eviction.
Constant Summary collapse
- DEFAULT_FREQUENCY_WEIGHT =
Default weights for the additive exposure formula
0.4- DEFAULT_RECENCY_WEIGHT =
0.4- DEFAULT_DWELL_WEIGHT =
0.2- DEFAULT_RECENCY_HALF_LIFE =
Default half-life for recency decay in seconds (7 days)
7 * 24 * 3600
Instance Method Summary collapse
-
#exposure_score(card) ⇒ Float
Calculate exposure score for a card.
-
#initialize(adapter, frequency_weight: nil, recency_weight: nil, dwell_weight: nil, recency_half_life: nil) ⇒ ExposureTracker
constructor
A new instance of ExposureTracker.
-
#low_exposure_cards(threshold: 1.0) ⇒ Array<Card>
Find cards with low exposure (candidates for compression).
-
#rank_by_exposure ⇒ Array<Hash>
Rank all cards by exposure score (ascending = least exposed first).
Constructor Details
#initialize(adapter, frequency_weight: nil, recency_weight: nil, dwell_weight: nil, recency_half_life: nil) ⇒ ExposureTracker
Returns a new instance of ExposureTracker.
35 36 37 38 39 40 41 42 |
# File 'lib/swarm_sdk/v3/memory/exposure_tracker.rb', line 35 def initialize(adapter, frequency_weight: nil, recency_weight: nil, dwell_weight: nil, recency_half_life: nil) @adapter = adapter config = Configuration.instance @frequency_weight = frequency_weight || config.exposure_frequency_weight @recency_weight = recency_weight || config.exposure_recency_weight @dwell_weight = dwell_weight || config.exposure_dwell_weight @recency_half_life = recency_half_life || config.exposure_recency_half_life end |
Instance Method Details
#exposure_score(card) ⇒ Float
Calculate exposure score for a card
Combines three signals additively:
-
Frequency: log(1 + access_count) — dampens high-access cards
-
Recency: exponential decay from last access time
-
Dwell: accumulated time in working context
E = α·log(1 + access_count) + β·recency(last_accessed) + γ·dwell
60 61 62 63 64 65 66 67 68 |
# File 'lib/swarm_sdk/v3/memory/exposure_tracker.rb', line 60 def exposure_score(card) frequency = Math.log(1 + card.access_count) recency = recency_factor(card.last_accessed) dwell = card.dwell @frequency_weight * frequency + @recency_weight * recency + @dwell_weight * dwell end |
#low_exposure_cards(threshold: 1.0) ⇒ Array<Card>
Find cards with low exposure (candidates for compression)
83 84 85 86 87 |
# File 'lib/swarm_sdk/v3/memory/exposure_tracker.rb', line 83 def low_exposure_cards(threshold: 1.0) rank_by_exposure .select { |entry| entry[:score] < threshold } .map { |entry| entry[:card] } end |
#rank_by_exposure ⇒ Array<Hash>
Rank all cards by exposure score (ascending = least exposed first)
73 74 75 76 77 |
# File 'lib/swarm_sdk/v3/memory/exposure_tracker.rb', line 73 def rank_by_exposure @adapter.list_cards.map do |card| { card: card, score: exposure_score(card) } end.sort_by { |entry| entry[:score] } end |