Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions go/core/internal/database/client_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,11 +684,13 @@ func (c *postgresClient) StoreAgentMemories(ctx context.Context, memories []*dbp
}

func (c *postgresClient) SearchAgentMemory(ctx context.Context, agentName, userID string, embedding pgvector.Vector, limit int) ([]dbpkg.AgentMemorySearchResult, error) {
normalized := strings.ReplaceAll(agentName, "-", "_")
rows, err := c.q.SearchAgentMemory(ctx, dbgen.SearchAgentMemoryParams{
Embedding: embedding,
AgentName: &agentName,
UserID: &userID,
Limit: int32(limit),
Embedding: embedding,
AgentName: &agentName,
AgentName_2: &normalized,
UserID: &userID,
Limit: int32(limit),
})
if err != nil {
return nil, fmt.Errorf("failed to search agent memory: %w", err)
Expand Down
17 changes: 17 additions & 0 deletions go/core/internal/database/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,23 @@ func TestSearchAgentMemoryIsolation(t *testing.T) {
assert.Equal(t, mem1.ID, results[0].ID)
}

// TestSearchAgentMemoryNormalizedName verifies that a search with a hyphenated
// agent name finds memories stored under the underscore form, matching the
// normalization ListAgentMemories and DeleteAgentMemory already apply.
func TestSearchAgentMemoryNormalizedName(t *testing.T) {
db := setupTestDB(t)
client := NewClient(db)
ctx := context.Background()

stored := &dbpkg.Memory{AgentName: "ns__my_agent", UserID: "user-1", Content: "stored under underscore form", Embedding: makeEmbedding(0.5)}
require.NoError(t, client.StoreAgentMemory(ctx, stored))

results, err := client.SearchAgentMemory(ctx, "ns__my-agent", "user-1", makeEmbedding(0.5), 10)
require.NoError(t, err)
require.Len(t, results, 1, "Search should find memories stored under the normalized name")
assert.Equal(t, stored.ID, results[0].ID)
}

// TestDeleteAgentMemory verifies that DeleteAgentMemory removes all memories for the
// given agent/user pair and that the hyphen-to-underscore normalization works correctly.
func TestDeleteAgentMemory(t *testing.T) {
Expand Down
14 changes: 8 additions & 6 deletions go/core/internal/database/gen/memory.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go/core/internal/database/queries/memory.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ RETURNING id;
-- COALESCE guards against NULL embeddings (score=0 rather than NULL); rows are still ordered last by the ORDER BY clause.
SELECT *, COALESCE(1 - (embedding <=> $1), 0) AS score
FROM memory
WHERE agent_name = $2 AND user_id = $3
WHERE (agent_name = $2 OR agent_name = $3) AND user_id = $4
ORDER BY embedding <=> $1 ASC
LIMIT $4;
LIMIT $5;

-- name: IncrementMemoryAccessCount :exec
-- Lock rows in id order to avoid deadlocks between concurrent overlapping increments.
Expand Down
Loading