Class: SwarmMemory::Tools::MemoryDefrag
- Inherits:
-
SwarmSDK::Tools::Base
- Object
- SwarmSDK::Tools::Base
- SwarmMemory::Tools::MemoryDefrag
- Defined in:
- lib/swarm_memory/tools/memory_defrag.rb
Overview
Tool for analyzing and optimizing memory storage
Provides defragmentation operations to maintain memory quality. Each agent has its own isolated memory storage.
Instance Method Summary collapse
-
#execute(action: "analyze", dry_run: true, similarity_threshold: 0.85, min_similarity: 0.60, max_similarity: 0.85, merge_strategy: "keep_newer", age_days: 90, max_hits: 10, min_quality_score: 20, confidence_filter: "low") ⇒ String
Execute the tool.
-
#initialize(storage:) ⇒ MemoryDefrag
constructor
Initialize with storage instance.
-
#name ⇒ Object
Override name to return simple "MemoryDefrag".
Constructor Details
#initialize(storage:) ⇒ MemoryDefrag
Initialize with storage instance
255 256 257 258 259 |
# File 'lib/swarm_memory/tools/memory_defrag.rb', line 255 def initialize(storage:) super() @storage = storage @defragmenter = nil # Lazy load end |
Instance Method Details
#execute(action: "analyze", dry_run: true, similarity_threshold: 0.85, min_similarity: 0.60, max_similarity: 0.85, merge_strategy: "keep_newer", age_days: 90, max_hits: 10, min_quality_score: 20, confidence_filter: "low") ⇒ String
Execute the tool
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/swarm_memory/tools/memory_defrag.rb', line 279 def execute( action: "analyze", dry_run: true, similarity_threshold: 0.85, min_similarity: 0.60, max_similarity: 0.85, merge_strategy: "keep_newer", age_days: 90, max_hits: 10, min_quality_score: 20, confidence_filter: "low" ) ensure_defragmenter_loaded case action.to_s.downcase # Read-only operations when "analyze" @defragmenter.health_report when "find_duplicates" @defragmenter.find_duplicates_report(threshold: similarity_threshold.to_f) when "find_low_quality" @defragmenter.find_low_quality_report(confidence_filter: confidence_filter.to_s.downcase) when "find_archival_candidates" @defragmenter.find_archival_candidates_report(age_days: age_days.to_i) when "find_related" @defragmenter.(min_threshold: min_similarity.to_f, max_threshold: max_similarity.to_f) # Active operations (modify memory) when "link_related" @defragmenter.( min_threshold: min_similarity.to_f, max_threshold: max_similarity.to_f, dry_run: dry_run, ) when "merge_duplicates" @defragmenter.merge_duplicates_active( threshold: similarity_threshold.to_f, strategy: merge_strategy.to_sym, dry_run: dry_run, ) when "cleanup_stubs" @defragmenter.cleanup_stubs_active( min_age_days: age_days.to_i, max_hits: max_hits.to_i, dry_run: dry_run, ) when "compact" @defragmenter.compact_active( min_quality_score: min_quality_score.to_i, min_age_days: age_days.to_i, max_hits: max_hits.to_i, dry_run: dry_run, ) when "full" # Full can be read-only analysis OR active optimization if dry_run # Just analysis @defragmenter.full_analysis( similarity_threshold: similarity_threshold.to_f, age_days: age_days.to_i, confidence_filter: confidence_filter.to_s.downcase, ) else # Active optimization @defragmenter.full_optimization(dry_run: dry_run) end else validation_error("Invalid action: #{action}. Must be one of: analyze, find_duplicates, find_low_quality, find_archival_candidates, find_related, link_related, merge_duplicates, cleanup_stubs, compact, full") end rescue ArgumentError => e validation_error(e.) rescue StandardError => e # Provide detailed error information error_msg = "Defrag error: #{e.class.name} - #{e.}\n\n" error_msg += "Backtrace:\n" error_msg += e.backtrace.first(10).join("\n") validation_error(error_msg) end |
#name ⇒ Object
Override name to return simple "MemoryDefrag"
262 263 264 |
# File 'lib/swarm_memory/tools/memory_defrag.rb', line 262 def name "MemoryDefrag" end |