Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Fix ListMemories text search missing older matching rows

Cursor Agent1ebdfc4798ca
diff --git a/cli/beacon/internal/learning/store.go b/cli/beacon/internal/learning/store.go
index 9858713d..1d8981d4 100644
--- a/cli/beacon/internal/learning/store.go
+++ b/cli/beacon/internal/learning/store.go
@@ -429,11 +429,21 @@ func (s *Store) ListMemories(query Query) ([]asymptoteobserve.LearningMemoryV1,
} else {
where += ` AND (superseded_by IS NULL OR superseded_by = '')`
}
- rows, err := db.Query(`SELECT memory_json FROM memories`+where+` ORDER BY updated_at DESC LIMIT ? OFFSET ?`, append(args, normalizeLimit(query.Limit), offset(query))...)
+ hasTextFilter := strings.TrimSpace(query.Q) != ""
+ sql := `SELECT memory_json FROM memories` + where + ` ORDER BY updated_at DESC`
+ if hasTextFilter {
+ args = append(args, -1, offset(query))
+ sql += ` LIMIT ? OFFSET ?`
+ } else {
+ args = append(args, normalizeLimit(query.Limit), offset(query))
+ sql += ` LIMIT ? OFFSET ?`
+ }
+ rows, err := db.Query(sql, args...)
if err != nil {
return nil, err
}
defer rows.Close()
+ limit := normalizeLimit(query.Limit)
var out []asymptoteobserve.LearningMemoryV1
for rows.Next() {
var raw string
@@ -444,7 +454,15 @@ func (s *Store) ListMemories(query Query) ([]asymptoteobserve.LearningMemoryV1,
if err := json.Unmarshal([]byte(raw), &value); err != nil {
return nil, err
}
- if query.Q == "" || matchesText(query.Q, value.ID, value.Title, value.Body, value.Kind) {
+ if hasTextFilter {
+ if !matchesText(query.Q, value.ID, value.Title, value.Body, value.Kind) {
+ continue
+ }
+ out = append(out, value)
+ if len(out) >= limit {
+ break
+ }
+ } else {
out = append(out, value)
}
}
diff --git a/cli/beacon/internal/learning/store_test.go b/cli/beacon/internal/learning/store_test.go
index 6fd9e84d..387ebfa1 100644
--- a/cli/beacon/internal/learning/store_test.go
+++ b/cli/beacon/internal/learning/store_test.go
@@ -1,6 +1,7 @@
package learning
import (
+ "fmt"
"os"
"path/filepath"
"testing"
@@ -125,6 +126,42 @@ func TestStorePersistsLearningArtifacts(t *testing.T) {
}
}
+func TestListMemoriesSearchesAllRows(t *testing.T) {
+ store := Open(filepath.Join(t.TempDir(), "memory.db"))
+ project := asymptoteobserve.LearningProjectV1{ID: "proj"}
+ for i := 0; i < 10; i++ {
+ if err := store.PutMemory(asymptoteobserve.LearningMemoryV1{
+ ID: fmt.Sprintf("memory-%d", i),
+ CandidateID: fmt.Sprintf("candidate-%d", i),
+ Kind: asymptoteobserve.LearningMemoryKindConvention,
+ Title: fmt.Sprintf("Convention %d", i),
+ Body: "Generic body text.",
+ Project: project,
+ }); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := store.PutMemory(asymptoteobserve.LearningMemoryV1{
+ ID: "memory-target",
+ CandidateID: "candidate-target",
+ Kind: asymptoteobserve.LearningMemoryKindDebuggingPattern,
+ Title: "Unique needle title",
+ Body: "Special body for matching.",
+ Project: project,
+ CreatedAt: "2020-01-01T00:00:00Z",
+ UpdatedAt: "2020-01-01T00:00:00Z",
+ }); err != nil {
+ t.Fatal(err)
+ }
+ memories, err := store.ListMemories(Query{ProjectID: "proj", Q: "needle", Limit: 5})
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(memories) != 1 || memories[0].ID != "memory-target" {
+ t.Fatalf("expected to find older matching memory, got %d results", len(memories))
+ }
+}
+
func TestStoreScopesByProject(t *testing.T) {
store := Open(filepath.Join(t.TempDir(), "memory.db"))
for _, projectID := range []string{"p1", "p2"} {