-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_consolidate.go
More file actions
259 lines (234 loc) · 7.17 KB
/
memory_consolidate.go
File metadata and controls
259 lines (234 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
51
52
53
54
55
56
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package main
import (
"log"
"sort"
"strings"
)
// ConsolidationPair represents a candidate pair of memories that are similar enough
// to consider consolidating them into a single chunk.
type ConsolidationPair struct {
AID string `json:"a_id"`
BID string `json:"b_id"`
Similarity float64 `json:"similarity"`
Type string `json:"type,omitempty"`
}
// ConsolidationParams controls how candidate pairs are discovered.
type ConsolidationParams struct {
// MinSimilarity is the minimum edge similarity required to include a pair.
// If zero or negative, a conservative default (0.9) is used.
MinSimilarity float64
// TypeFilter, when non-empty, restricts pairs to memories where both sides
// have this type.
TypeFilter string
// Limit caps the number of pairs returned. Zero means no limit.
Limit int
}
// ConsolidateOptions controls how multiple memories are merged into one.
type ConsolidateOptions struct {
// NewLabel, when non-empty, overrides the label for the merged chunk.
// If empty, the label is derived from the merged text (same as Add()).
NewLabel string
// NewType, when non-empty, sets the type for the merged chunk.
// If empty, the type is inferred from the source chunks (they must be compatible).
NewType string
// DeleteSources controls whether the source chunks are deleted after a successful merge.
// If nil, the default is true.
DeleteSources *bool
// TextJoiner is inserted between source texts when building the merged text.
// If empty, a clear separator is used.
TextJoiner string
}
// defaultMinConsolidationSimilarity is intentionally high; callers can lower it explicitly.
const defaultMinConsolidationSimilarity = 0.9
// FindConsolidationPairs scans existing similarity edges and returns candidate pairs
// whose similarity exceeds the configured threshold. It is read-only; no mutations.
func (s *MemoryStore) FindConsolidationPairs(params ConsolidationParams) ([]ConsolidationPair, error) {
minSim := params.MinSimilarity
if minSim <= 0 {
minSim = defaultMinConsolidationSimilarity
}
s.mu.RLock()
defer s.mu.RUnlock()
pairs := make([]ConsolidationPair, 0)
seen := make(map[string]struct{})
for id, sc := range s.chunks {
// Apply type filter on the anchor side.
if params.TypeFilter != "" && sc.Type != params.TypeFilter {
continue
}
for otherID, sim := range sc.edges {
if sim < minSim {
continue
}
other := s.chunks[otherID]
if other == nil {
continue
}
// Apply type filter on the neighbor side.
if params.TypeFilter != "" && other.Type != params.TypeFilter {
continue
}
aID, bID := id, otherID
if aID == bID {
continue
}
if aID > bID {
aID, bID = bID, aID
}
key := aID + "|" + bID
if _, ok := seen[key]; ok {
continue
}
seen[key] = struct{}{}
pairType := sc.Type
if pairType == "" {
pairType = other.Type
}
pairs = append(pairs, ConsolidationPair{
AID: aID,
BID: bID,
Similarity: sim,
Type: pairType,
})
if params.Limit > 0 && len(pairs) >= params.Limit {
// We'll still sort below; limit is just a cap.
break
}
}
}
// Sort by similarity desc, then by IDs for stability.
sort.Slice(pairs, func(i, j int) bool {
if pairs[i].Similarity == pairs[j].Similarity {
if pairs[i].AID == pairs[j].AID {
return pairs[i].BID < pairs[j].BID
}
return pairs[i].AID < pairs[j].AID
}
return pairs[i].Similarity > pairs[j].Similarity
})
if params.Limit > 0 && len(pairs) > params.Limit {
pairs = pairs[:params.Limit]
}
log.Printf("MEMORY: CONSOLIDATION_CANDIDATES pairs=%d", len(pairs))
return pairs, nil
}
// Consolidate merges the given memory IDs into a single new chunk, returning the merged
// chunk and the list of source IDs that were deleted (if any). It uses the same creation
// pipeline as Add() so that summarization, tokenization, and edges are handled consistently.
func (s *MemoryStore) Consolidate(ids []string, opts ConsolidateOptions) (MemoryChunk, []string, error) {
// Normalize and deduplicate IDs.
uniq := make([]string, 0, len(ids))
seen := make(map[string]struct{})
for _, raw := range ids {
id := strings.TrimSpace(raw)
if id == "" {
continue
}
if _, ok := seen[id]; ok {
continue
}
seen[id] = struct{}{}
uniq = append(uniq, id)
}
if len(uniq) < 2 {
return MemoryChunk{}, nil, ErrTooFewIDs
}
// Read source chunks under a single lock for atomicity.
// We read directly instead of using Get() to avoid inflating access counts
// on chunks that are about to be deleted.
sourceChunks := make([]MemoryChunk, 0, len(uniq))
s.mu.RLock()
for _, id := range uniq {
sc := s.chunks[id]
if sc == nil {
s.mu.RUnlock()
return MemoryChunk{}, nil, ErrNotFound
}
sourceChunks = append(sourceChunks, sc.MemoryChunk)
}
s.mu.RUnlock()
// Determine resulting type.
targetType := strings.TrimSpace(opts.NewType)
if targetType == "" {
// Infer type from sources; require compatibility when non-empty.
for _, ch := range sourceChunks {
if ch.Type == "" {
continue
}
if targetType == "" {
targetType = ch.Type
} else if ch.Type != targetType {
return MemoryChunk{}, nil, ErrIncompatibleTypes
}
}
}
// Build merged text in a deterministic order (by creation time, then ID).
sort.Slice(sourceChunks, func(i, j int) bool {
if sourceChunks[i].CreatedAt.Equal(sourceChunks[j].CreatedAt) {
return sourceChunks[i].ID < sourceChunks[j].ID
}
return sourceChunks[i].CreatedAt.Before(sourceChunks[j].CreatedAt)
})
joiner := opts.TextJoiner
if joiner == "" {
joiner = "\n\n---\n\n"
}
textParts := make([]string, 0, len(sourceChunks))
for _, ch := range sourceChunks {
textParts = append(textParts, ch.Text)
}
mergedText := strings.Join(textParts, joiner)
// Use Add() to create the new chunk so vectors/edges/token indexes and persistence are handled.
// Preserve scopes if all source chunks have the same scopes; otherwise make it global.
var mergedScopes []string
if len(sourceChunks) > 0 {
firstScopes := sourceChunks[0].Scopes
scopeSet := make(map[string]struct{}, len(firstScopes))
for _, s := range firstScopes {
scopeSet[s] = struct{}{}
}
allSame := true
for _, ch := range sourceChunks[1:] {
if len(ch.Scopes) != len(firstScopes) {
allSame = false
break
}
for _, s := range ch.Scopes {
if _, ok := scopeSet[s]; !ok {
allSame = false
break
}
}
if !allSame {
break
}
}
if allSame {
mergedScopes = firstScopes
}
}
newLabel := strings.TrimSpace(opts.NewLabel)
mergedChunk, _, err := s.Add(mergedText, newLabel, targetType, mergedScopes)
if err != nil {
return MemoryChunk{}, nil, err
}
// Decide whether to delete sources; default is true.
deleteSources := true
if opts.DeleteSources != nil {
deleteSources = *opts.DeleteSources
}
removed := make([]string, 0)
if deleteSources {
for _, id := range uniq {
if id == mergedChunk.ID {
// Shouldn't happen (new ID), but be defensive.
continue
}
if _, err := s.Delete(id); err == nil {
removed = append(removed, id)
}
}
}
log.Printf("MEMORY: CONSOLIDATE merged=%s from=%v '%s'", mergedChunk.ID, removed, ExcerptForLog(mergedChunk.Text, logExcerptLen))
return mergedChunk, removed, nil
}