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
|
# File 'lib/legion/cli/chat/tools/consolidate_memory.rb', line 37
def execute(scope: 'project', dry_run: nil)
dry_run = dry_run.to_s == 'true'
scope_sym = scope.to_s == 'global' ? :global : :project
entries = MemoryStore.list(scope: scope_sym)
return "No memory entries found in #{scope} scope." if entries.empty?
return "Only #{entries.size} entries — no consolidation needed." if entries.size < 3
consolidated = consolidate_entries(entries)
return 'Consolidation failed: could not generate summary.' unless consolidated
new_entries = parse_consolidated(consolidated)
removed = entries.size - new_entries.size
if dry_run
preview = new_entries.map.with_index(1) { |e, i| "#{i}. #{e}" }.join("\n")
"Preview (#{entries.size} -> #{new_entries.size}, #{removed} removed):\n\n#{preview}"
else
write_consolidated(new_entries, scope_sym)
"Consolidated #{scope} memory: #{entries.size} -> #{new_entries.size} entries (#{removed} removed/merged)"
end
rescue StandardError => e
Legion::Logging.warn("ConsolidateMemory#execute failed: #{e.message}") if defined?(Legion::Logging)
"Error consolidating memory: #{e.message}"
end
|