2026-08-13 07:09:12 +00:00
|
|
|
|
package memory
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"sort"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/logger"
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
// embedTimeout bounds the query-side embedding call.
|
|
|
|
|
|
//
|
|
|
|
|
|
// Recall sits in front of every answer, and before this it made no model
|
|
|
|
|
|
// call at all. Semantic matching is worth a fraction of a turn; it is not
|
|
|
|
|
|
// worth a turn that hangs because an embedding endpoint is wedged. On
|
|
|
|
|
|
// timeout recall silently falls back to lexical matching, which is exactly
|
|
|
|
|
|
// the behaviour that existed before.
|
|
|
|
|
|
embedTimeout = 2 * time.Second
|
|
|
|
|
|
// embedWriteTimeout bounds the write-side call. Writes are already off the
|
|
|
|
|
|
// response path, so this can be more generous.
|
|
|
|
|
|
embedWriteTimeout = 10 * time.Second
|
|
|
|
|
|
// rrfK is the reciprocal-rank-fusion constant. 60 is the value from the
|
|
|
|
|
|
// original TREC work and the one most systems use; Graphiti uses 1, which
|
|
|
|
|
|
// sharpens the top of the list at the cost of ignoring almost everything
|
|
|
|
|
|
// below it. With candidate sets this small, the standard value keeps
|
|
|
|
|
|
// agreement between the two rankings meaningful.
|
|
|
|
|
|
rrfK = 60.0
|
|
|
|
|
|
// minCosine is the floor below which a vector match is not a match.
|
|
|
|
|
|
//
|
|
|
|
|
|
// Without it every memory that has a vector enters the ranking, including
|
|
|
|
|
|
// the ones scoring zero, and fusion then pulls them into the prompt — the
|
|
|
|
|
|
// feature would go from "cannot find a re-worded memory" straight to
|
|
|
|
|
|
// "recalls everything". Graphiti holds its equivalent at 0.6; this sits
|
|
|
|
|
|
// slightly lower because the lexical ranking is fused in alongside and can
|
|
|
|
|
|
// still rescue an exact-term match the model embedded poorly.
|
|
|
|
|
|
minCosine = 0.5
|
|
|
|
|
|
// backfillPerRun is how many missing vectors one maintenance pass fills.
|
fix(memory): rank memories by the query, not by importance
Long-term memory chose its candidates before it looked at the question.
Recall listed 400 items by `importance DESC, valid_from DESC`, loaded the
vectors belonging to that list, and scored those; extraction did the same
with a 200-item pool. Importance is uncorrelated with relevance, so the
vector stage could only re-order what importance had already picked, and a
memory below the cut was unreachable no matter how well it matched. The
capacity cap goes up to 2000, so everything past the 400th most important
memory was permanently unrecallable, and `memory_item_embeddings.vector`
being a BYTEA blob meant pgvector was never involved.
The store now answers "which of this person's memories is this question
about":
- Migration 000095 (and the SQLite mirror) add `embedding halfvec` plus a
`(tenant_id, subject_id, model_id, dims)` index. The column is
conditional on the vector extension, so a deployment whose retrieval
driver excludes postgres keeps working. No HNSW: an approximate index is
filtered after the fact, and one subject holds at most a couple of
thousand rows, so exact ordering inside the scope is both correct and a
few milliseconds.
- `SearchItemsByVector` ranks in SQL over every vector the subject has,
with the status, expiry and kind filters joined in so reaching past the
candidate list cannot reach past the filters. SQLite and pgvector-less
PostgreSQL fall back to scoring in process, still over the whole scope.
- Recall fuses the lexical ranking with those hits and grows the candidate
pool with matches the pool did not hold, which is what lets the search
escape it. The lexical pool is now the capacity cap, so it can no longer
be a second, invisible limit under the one that decides what stays active.
- Extraction picks the memories it shows the model the same way. This
matters more than recall: the memory a new statement contradicts is not
reliably an important one, so an importance-ordered window hid exactly
the collision that stops a duplicate being written.
- Vectors are written to the searchable column in the upsert's transaction;
rows predating the column are moved by `SyncVectorColumn` during
maintenance, which costs no model calls. The embedding backfill goes from
50 to 200 per run — at 50, a subject at the capacity cap took over a
month to become fully searchable.
- Traces and logs report `outside_pool`, the number of semantic matches the
lexical pool did not contain. It was structurally zero before, so it is
the number to watch when asking whether semantic recall does anything.
Tests cover whole-scope ranking, the lifecycle and kind filters, pool
growth, the pool-size invariant, and extraction seeing a low-importance
memory it would otherwise duplicate. A pgvector integration test
(WEKNORA_MEMORY_TEST_POSTGRES_DSN) exercises the migration, the halfvec
write, the SQL ranking and the column sync.
2026-09-13 03:06:12 +00:00
|
|
|
|
//
|
|
|
|
|
|
// Each one costs an embedding call, so this is a rate rather than a batch
|
|
|
|
|
|
// size. It has to outpace what a busy subject accumulates while its model
|
|
|
|
|
|
// is unreachable; at the previous 50 a subject sitting at the capacity cap
|
|
|
|
|
|
// took over a month before semantic recall could see all of it, which in
|
|
|
|
|
|
// practice meant it never could.
|
|
|
|
|
|
backfillPerRun = 200
|
|
|
|
|
|
// vectorSyncPerRun is how many rows one maintenance pass moves into the
|
|
|
|
|
|
// database's vector type. Far larger than the embedding backfill because
|
|
|
|
|
|
// it makes no model calls: the vector already exists.
|
|
|
|
|
|
vectorSyncPerRun = 2000
|
2026-08-13 07:09:12 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// embedder resolves the embedding model pinned on this workspace.
|
|
|
|
|
|
//
|
|
|
|
|
|
// Memory is one vector space per workspace. Knowledge bases each bind their
|
|
|
|
|
|
// own embedding model, so there is no "the workspace embedding model" to fall
|
|
|
|
|
|
// back to — picking the first listed one would silently mix incomparable
|
|
|
|
|
|
// spaces as models are added or deleted. Blank means semantic recall is off.
|
|
|
|
|
|
func (s *Service) embedder(_ context.Context, cfg *types.MemoryConfig) (string, bool) {
|
|
|
|
|
|
if cfg == nil || !cfg.VectorRecallEnabled() || s.modelService == nil {
|
|
|
|
|
|
return "", false
|
|
|
|
|
|
}
|
|
|
|
|
|
if cfg.EmbeddingModelID == "" {
|
|
|
|
|
|
return "", false
|
|
|
|
|
|
}
|
|
|
|
|
|
return cfg.EmbeddingModelID, true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// embedText produces one vector, bounded and non-fatal.
|
|
|
|
|
|
func (s *Service) embedText(
|
|
|
|
|
|
ctx context.Context, modelID, text string, timeout time.Duration,
|
|
|
|
|
|
) []float32 {
|
|
|
|
|
|
if modelID == "" || text == "" || s.modelService == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
embedder, err := s.modelService.GetEmbeddingModel(ctx, modelID)
|
|
|
|
|
|
if err != nil || embedder == nil {
|
|
|
|
|
|
logger.Warnf(ctx, "memory: embedding model %s unavailable: %v", modelID, err)
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
callCtx, cancel := context.WithTimeout(ctx, timeout)
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
vector, err := embedder.Embed(callCtx, text)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.Warnf(ctx, "memory: embed failed: %v", err)
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return vector
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// storeItemEmbedding records the vector for one memory. Best effort: a memory
|
|
|
|
|
|
// without a vector is still a memory, it is just invisible to semantic recall
|
|
|
|
|
|
// until the backfill catches it.
|
|
|
|
|
|
func (s *Service) storeItemEmbedding(
|
|
|
|
|
|
ctx context.Context, scope interfaces.MemoryScope, cfg *types.MemoryConfig, item *types.MemoryItem,
|
|
|
|
|
|
) {
|
|
|
|
|
|
if item == nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
modelID, ok := s.embedder(ctx, cfg)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
text := embeddableText(item, s.embedAliases(ctx, scope, item))
|
|
|
|
|
|
vector := s.embedText(ctx, modelID, text, embedWriteTimeout)
|
|
|
|
|
|
if len(vector) == 0 {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
err := s.repo.UpsertItemEmbedding(ctx, scope, &types.MemoryItemEmbedding{
|
2026-09-12 16:04:16 +00:00
|
|
|
|
ItemID: item.ID,
|
|
|
|
|
|
SourceContent: item.Content,
|
|
|
|
|
|
SourceTopic: item.Topic,
|
|
|
|
|
|
ModelID: modelID,
|
|
|
|
|
|
Dims: len(vector),
|
|
|
|
|
|
Vector: types.EncodeEmbedding(vector),
|
2026-08-13 07:09:12 +00:00
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.Warnf(ctx, "memory: store embedding failed: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// embeddableText is what gets embedded for a memory.
|
|
|
|
|
|
//
|
|
|
|
|
|
// Topic and content together, because the topic carries the subject the
|
|
|
|
|
|
// statement is about and the statement alone is often too terse to place —
|
|
|
|
|
|
// "PostgreSQL 17" means little without "生产数据库".
|
|
|
|
|
|
//
|
|
|
|
|
|
// An interest is promoted from a subject label, so its topic and content are
|
|
|
|
|
|
// the same string. Joining them would embed "X:X", which is not the sentence
|
|
|
|
|
|
// any question resembles.
|
|
|
|
|
|
//
|
|
|
|
|
|
// aliases are the other wordings this person has used for the same subject.
|
|
|
|
|
|
// They widen what a question can match without widening what the model is
|
|
|
|
|
|
// told: they exist only in the vector, never in the injected block.
|
|
|
|
|
|
func embeddableText(item *types.MemoryItem, aliases []string) string {
|
|
|
|
|
|
if item == nil {
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
topic := types.SanitizeMemoryTopic(item.Topic)
|
|
|
|
|
|
content := types.SanitizeMemoryContent(item.Content)
|
|
|
|
|
|
text := content
|
|
|
|
|
|
if topic != "" && topic != content {
|
|
|
|
|
|
text = topic + ":" + content
|
|
|
|
|
|
}
|
|
|
|
|
|
if text == "" {
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
|
|
|
|
|
seen := map[string]bool{text: true, content: true, topic: true}
|
|
|
|
|
|
for _, alias := range aliases {
|
|
|
|
|
|
alias = types.SanitizeMemoryTopic(alias)
|
|
|
|
|
|
if alias == "" || seen[alias] {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
seen[alias] = true
|
|
|
|
|
|
text += ";" + alias
|
|
|
|
|
|
}
|
|
|
|
|
|
return text
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// embedAliases returns the other wordings this person has used for an
|
|
|
|
|
|
// interest's subject.
|
|
|
|
|
|
//
|
|
|
|
|
|
// Only interests: every other kind already carries a sentence of its own, and
|
|
|
|
|
|
// its topic is a heading rather than a subject the topic tracker follows. Best
|
|
|
|
|
|
// effort — a lookup failure costs a slightly narrower vector, nothing else.
|
|
|
|
|
|
func (s *Service) embedAliases(
|
|
|
|
|
|
ctx context.Context, scope interfaces.MemoryScope, item *types.MemoryItem,
|
|
|
|
|
|
) []string {
|
|
|
|
|
|
if item == nil || item.Kind != types.MemoryKindInterest {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
key := types.NormalizeTopicKey(item.Topic)
|
|
|
|
|
|
if key == "" {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
stat, err := s.repo.TopicByKey(ctx, scope, key)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.Warnf(ctx, "memory: load topic aliases failed: %v", err)
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
if stat == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return stat.Aliases
|
|
|
|
|
|
}
|
|
|
|
|
|
|
fix(memory): rank memories by the query, not by importance
Long-term memory chose its candidates before it looked at the question.
Recall listed 400 items by `importance DESC, valid_from DESC`, loaded the
vectors belonging to that list, and scored those; extraction did the same
with a 200-item pool. Importance is uncorrelated with relevance, so the
vector stage could only re-order what importance had already picked, and a
memory below the cut was unreachable no matter how well it matched. The
capacity cap goes up to 2000, so everything past the 400th most important
memory was permanently unrecallable, and `memory_item_embeddings.vector`
being a BYTEA blob meant pgvector was never involved.
The store now answers "which of this person's memories is this question
about":
- Migration 000095 (and the SQLite mirror) add `embedding halfvec` plus a
`(tenant_id, subject_id, model_id, dims)` index. The column is
conditional on the vector extension, so a deployment whose retrieval
driver excludes postgres keeps working. No HNSW: an approximate index is
filtered after the fact, and one subject holds at most a couple of
thousand rows, so exact ordering inside the scope is both correct and a
few milliseconds.
- `SearchItemsByVector` ranks in SQL over every vector the subject has,
with the status, expiry and kind filters joined in so reaching past the
candidate list cannot reach past the filters. SQLite and pgvector-less
PostgreSQL fall back to scoring in process, still over the whole scope.
- Recall fuses the lexical ranking with those hits and grows the candidate
pool with matches the pool did not hold, which is what lets the search
escape it. The lexical pool is now the capacity cap, so it can no longer
be a second, invisible limit under the one that decides what stays active.
- Extraction picks the memories it shows the model the same way. This
matters more than recall: the memory a new statement contradicts is not
reliably an important one, so an importance-ordered window hid exactly
the collision that stops a duplicate being written.
- Vectors are written to the searchable column in the upsert's transaction;
rows predating the column are moved by `SyncVectorColumn` during
maintenance, which costs no model calls. The embedding backfill goes from
50 to 200 per run — at 50, a subject at the capacity cap took over a
month to become fully searchable.
- Traces and logs report `outside_pool`, the number of semantic matches the
lexical pool did not contain. It was structurally zero before, so it is
the number to watch when asking whether semantic recall does anything.
Tests cover whole-scope ranking, the lifecycle and kind filters, pool
growth, the pool-size invariant, and extraction seeing a low-importance
memory it would otherwise duplicate. A pgvector integration test
(WEKNORA_MEMORY_TEST_POSTGRES_DSN) exercises the migration, the halfvec
write, the SQL ranking and the column sync.
2026-09-13 03:06:12 +00:00
|
|
|
|
// vectorSearch asks the store for the memories closest to the query.
|
|
|
|
|
|
//
|
|
|
|
|
|
// The search runs over every vector the subject has. It used to run over the
|
|
|
|
|
|
// vectors of an already-chosen candidate list, which meant semantic recall
|
|
|
|
|
|
// could only re-order what a plain `ORDER BY importance` had picked — a memory
|
|
|
|
|
|
// that answered the question exactly but sat outside that window was
|
|
|
|
|
|
// unreachable, and no amount of widening the window fixes the ordering being
|
|
|
|
|
|
// blind to the question in the first place.
|
|
|
|
|
|
//
|
|
|
|
|
|
// An empty result means semantic matching was unavailable or found nothing;
|
|
|
|
|
|
// callers fall back to lexical matching rather than treating it as "nothing
|
|
|
|
|
|
// matched". skipReason says which.
|
|
|
|
|
|
func (s *Service) vectorSearch(
|
2026-08-13 07:09:12 +00:00
|
|
|
|
ctx context.Context,
|
|
|
|
|
|
scope interfaces.MemoryScope,
|
|
|
|
|
|
cfg *types.MemoryConfig,
|
|
|
|
|
|
query string,
|
fix(memory): rank memories by the query, not by importance
Long-term memory chose its candidates before it looked at the question.
Recall listed 400 items by `importance DESC, valid_from DESC`, loaded the
vectors belonging to that list, and scored those; extraction did the same
with a 200-item pool. Importance is uncorrelated with relevance, so the
vector stage could only re-order what importance had already picked, and a
memory below the cut was unreachable no matter how well it matched. The
capacity cap goes up to 2000, so everything past the 400th most important
memory was permanently unrecallable, and `memory_item_embeddings.vector`
being a BYTEA blob meant pgvector was never involved.
The store now answers "which of this person's memories is this question
about":
- Migration 000095 (and the SQLite mirror) add `embedding halfvec` plus a
`(tenant_id, subject_id, model_id, dims)` index. The column is
conditional on the vector extension, so a deployment whose retrieval
driver excludes postgres keeps working. No HNSW: an approximate index is
filtered after the fact, and one subject holds at most a couple of
thousand rows, so exact ordering inside the scope is both correct and a
few milliseconds.
- `SearchItemsByVector` ranks in SQL over every vector the subject has,
with the status, expiry and kind filters joined in so reaching past the
candidate list cannot reach past the filters. SQLite and pgvector-less
PostgreSQL fall back to scoring in process, still over the whole scope.
- Recall fuses the lexical ranking with those hits and grows the candidate
pool with matches the pool did not hold, which is what lets the search
escape it. The lexical pool is now the capacity cap, so it can no longer
be a second, invisible limit under the one that decides what stays active.
- Extraction picks the memories it shows the model the same way. This
matters more than recall: the memory a new statement contradicts is not
reliably an important one, so an importance-ordered window hid exactly
the collision that stops a duplicate being written.
- Vectors are written to the searchable column in the upsert's transaction;
rows predating the column are moved by `SyncVectorColumn` during
maintenance, which costs no model calls. The embedding backfill goes from
50 to 200 per run — at 50, a subject at the capacity cap took over a
month to become fully searchable.
- Traces and logs report `outside_pool`, the number of semantic matches the
lexical pool did not contain. It was structurally zero before, so it is
the number to watch when asking whether semantic recall does anything.
Tests cover whole-scope ranking, the lifecycle and kind filters, pool
growth, the pool-size invariant, and extraction seeing a low-importance
memory it would otherwise duplicate. A pgvector integration test
(WEKNORA_MEMORY_TEST_POSTGRES_DSN) exercises the migration, the halfvec
write, the SQL ranking and the column sync.
2026-09-13 03:06:12 +00:00
|
|
|
|
kinds []string,
|
|
|
|
|
|
limit int,
|
|
|
|
|
|
) ([]interfaces.MemoryVectorHit, string) {
|
2026-08-13 07:09:12 +00:00
|
|
|
|
modelID, ok := s.embedder(ctx, cfg)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return nil, "vector_disabled"
|
|
|
|
|
|
}
|
2026-09-21 03:55:38 +00:00
|
|
|
|
queryVector := s.embedText(types.WithEmbedQuery(ctx), modelID, query, embedTimeout)
|
2026-08-13 07:09:12 +00:00
|
|
|
|
if len(queryVector) == 0 {
|
|
|
|
|
|
return nil, "embed_failed"
|
|
|
|
|
|
}
|
fix(memory): rank memories by the query, not by importance
Long-term memory chose its candidates before it looked at the question.
Recall listed 400 items by `importance DESC, valid_from DESC`, loaded the
vectors belonging to that list, and scored those; extraction did the same
with a 200-item pool. Importance is uncorrelated with relevance, so the
vector stage could only re-order what importance had already picked, and a
memory below the cut was unreachable no matter how well it matched. The
capacity cap goes up to 2000, so everything past the 400th most important
memory was permanently unrecallable, and `memory_item_embeddings.vector`
being a BYTEA blob meant pgvector was never involved.
The store now answers "which of this person's memories is this question
about":
- Migration 000095 (and the SQLite mirror) add `embedding halfvec` plus a
`(tenant_id, subject_id, model_id, dims)` index. The column is
conditional on the vector extension, so a deployment whose retrieval
driver excludes postgres keeps working. No HNSW: an approximate index is
filtered after the fact, and one subject holds at most a couple of
thousand rows, so exact ordering inside the scope is both correct and a
few milliseconds.
- `SearchItemsByVector` ranks in SQL over every vector the subject has,
with the status, expiry and kind filters joined in so reaching past the
candidate list cannot reach past the filters. SQLite and pgvector-less
PostgreSQL fall back to scoring in process, still over the whole scope.
- Recall fuses the lexical ranking with those hits and grows the candidate
pool with matches the pool did not hold, which is what lets the search
escape it. The lexical pool is now the capacity cap, so it can no longer
be a second, invisible limit under the one that decides what stays active.
- Extraction picks the memories it shows the model the same way. This
matters more than recall: the memory a new statement contradicts is not
reliably an important one, so an importance-ordered window hid exactly
the collision that stops a duplicate being written.
- Vectors are written to the searchable column in the upsert's transaction;
rows predating the column are moved by `SyncVectorColumn` during
maintenance, which costs no model calls. The embedding backfill goes from
50 to 200 per run — at 50, a subject at the capacity cap took over a
month to become fully searchable.
- Traces and logs report `outside_pool`, the number of semantic matches the
lexical pool did not contain. It was structurally zero before, so it is
the number to watch when asking whether semantic recall does anything.
Tests cover whole-scope ranking, the lifecycle and kind filters, pool
growth, the pool-size invariant, and extraction seeing a low-importance
memory it would otherwise duplicate. A pgvector integration test
(WEKNORA_MEMORY_TEST_POSTGRES_DSN) exercises the migration, the halfvec
write, the SQL ranking and the column sync.
2026-09-13 03:06:12 +00:00
|
|
|
|
hits, err := s.repo.SearchItemsByVector(ctx, scope, interfaces.MemoryVectorQuery{
|
|
|
|
|
|
ModelID: modelID,
|
|
|
|
|
|
Vector: queryVector,
|
|
|
|
|
|
Kinds: kinds,
|
|
|
|
|
|
MinScore: minCosine,
|
|
|
|
|
|
Limit: limit,
|
|
|
|
|
|
})
|
2026-08-13 07:09:12 +00:00
|
|
|
|
if err != nil {
|
fix(memory): rank memories by the query, not by importance
Long-term memory chose its candidates before it looked at the question.
Recall listed 400 items by `importance DESC, valid_from DESC`, loaded the
vectors belonging to that list, and scored those; extraction did the same
with a 200-item pool. Importance is uncorrelated with relevance, so the
vector stage could only re-order what importance had already picked, and a
memory below the cut was unreachable no matter how well it matched. The
capacity cap goes up to 2000, so everything past the 400th most important
memory was permanently unrecallable, and `memory_item_embeddings.vector`
being a BYTEA blob meant pgvector was never involved.
The store now answers "which of this person's memories is this question
about":
- Migration 000095 (and the SQLite mirror) add `embedding halfvec` plus a
`(tenant_id, subject_id, model_id, dims)` index. The column is
conditional on the vector extension, so a deployment whose retrieval
driver excludes postgres keeps working. No HNSW: an approximate index is
filtered after the fact, and one subject holds at most a couple of
thousand rows, so exact ordering inside the scope is both correct and a
few milliseconds.
- `SearchItemsByVector` ranks in SQL over every vector the subject has,
with the status, expiry and kind filters joined in so reaching past the
candidate list cannot reach past the filters. SQLite and pgvector-less
PostgreSQL fall back to scoring in process, still over the whole scope.
- Recall fuses the lexical ranking with those hits and grows the candidate
pool with matches the pool did not hold, which is what lets the search
escape it. The lexical pool is now the capacity cap, so it can no longer
be a second, invisible limit under the one that decides what stays active.
- Extraction picks the memories it shows the model the same way. This
matters more than recall: the memory a new statement contradicts is not
reliably an important one, so an importance-ordered window hid exactly
the collision that stops a duplicate being written.
- Vectors are written to the searchable column in the upsert's transaction;
rows predating the column are moved by `SyncVectorColumn` during
maintenance, which costs no model calls. The embedding backfill goes from
50 to 200 per run — at 50, a subject at the capacity cap took over a
month to become fully searchable.
- Traces and logs report `outside_pool`, the number of semantic matches the
lexical pool did not contain. It was structurally zero before, so it is
the number to watch when asking whether semantic recall does anything.
Tests cover whole-scope ranking, the lifecycle and kind filters, pool
growth, the pool-size invariant, and extraction seeing a low-importance
memory it would otherwise duplicate. A pgvector integration test
(WEKNORA_MEMORY_TEST_POSTGRES_DSN) exercises the migration, the halfvec
write, the SQL ranking and the column sync.
2026-09-13 03:06:12 +00:00
|
|
|
|
logger.Warnf(ctx, "memory: vector search failed: %v", err)
|
|
|
|
|
|
return nil, "vector_search_failed"
|
2026-08-13 07:09:12 +00:00
|
|
|
|
}
|
fix(memory): rank memories by the query, not by importance
Long-term memory chose its candidates before it looked at the question.
Recall listed 400 items by `importance DESC, valid_from DESC`, loaded the
vectors belonging to that list, and scored those; extraction did the same
with a 200-item pool. Importance is uncorrelated with relevance, so the
vector stage could only re-order what importance had already picked, and a
memory below the cut was unreachable no matter how well it matched. The
capacity cap goes up to 2000, so everything past the 400th most important
memory was permanently unrecallable, and `memory_item_embeddings.vector`
being a BYTEA blob meant pgvector was never involved.
The store now answers "which of this person's memories is this question
about":
- Migration 000095 (and the SQLite mirror) add `embedding halfvec` plus a
`(tenant_id, subject_id, model_id, dims)` index. The column is
conditional on the vector extension, so a deployment whose retrieval
driver excludes postgres keeps working. No HNSW: an approximate index is
filtered after the fact, and one subject holds at most a couple of
thousand rows, so exact ordering inside the scope is both correct and a
few milliseconds.
- `SearchItemsByVector` ranks in SQL over every vector the subject has,
with the status, expiry and kind filters joined in so reaching past the
candidate list cannot reach past the filters. SQLite and pgvector-less
PostgreSQL fall back to scoring in process, still over the whole scope.
- Recall fuses the lexical ranking with those hits and grows the candidate
pool with matches the pool did not hold, which is what lets the search
escape it. The lexical pool is now the capacity cap, so it can no longer
be a second, invisible limit under the one that decides what stays active.
- Extraction picks the memories it shows the model the same way. This
matters more than recall: the memory a new statement contradicts is not
reliably an important one, so an importance-ordered window hid exactly
the collision that stops a duplicate being written.
- Vectors are written to the searchable column in the upsert's transaction;
rows predating the column are moved by `SyncVectorColumn` during
maintenance, which costs no model calls. The embedding backfill goes from
50 to 200 per run — at 50, a subject at the capacity cap took over a
month to become fully searchable.
- Traces and logs report `outside_pool`, the number of semantic matches the
lexical pool did not contain. It was structurally zero before, so it is
the number to watch when asking whether semantic recall does anything.
Tests cover whole-scope ranking, the lifecycle and kind filters, pool
growth, the pool-size invariant, and extraction seeing a low-importance
memory it would otherwise duplicate. A pgvector integration test
(WEKNORA_MEMORY_TEST_POSTGRES_DSN) exercises the migration, the halfvec
write, the SQL ranking and the column sync.
2026-09-13 03:06:12 +00:00
|
|
|
|
if len(hits) == 0 {
|
|
|
|
|
|
return nil, "no_vector_matches"
|
2026-08-13 07:09:12 +00:00
|
|
|
|
}
|
fix(memory): rank memories by the query, not by importance
Long-term memory chose its candidates before it looked at the question.
Recall listed 400 items by `importance DESC, valid_from DESC`, loaded the
vectors belonging to that list, and scored those; extraction did the same
with a 200-item pool. Importance is uncorrelated with relevance, so the
vector stage could only re-order what importance had already picked, and a
memory below the cut was unreachable no matter how well it matched. The
capacity cap goes up to 2000, so everything past the 400th most important
memory was permanently unrecallable, and `memory_item_embeddings.vector`
being a BYTEA blob meant pgvector was never involved.
The store now answers "which of this person's memories is this question
about":
- Migration 000095 (and the SQLite mirror) add `embedding halfvec` plus a
`(tenant_id, subject_id, model_id, dims)` index. The column is
conditional on the vector extension, so a deployment whose retrieval
driver excludes postgres keeps working. No HNSW: an approximate index is
filtered after the fact, and one subject holds at most a couple of
thousand rows, so exact ordering inside the scope is both correct and a
few milliseconds.
- `SearchItemsByVector` ranks in SQL over every vector the subject has,
with the status, expiry and kind filters joined in so reaching past the
candidate list cannot reach past the filters. SQLite and pgvector-less
PostgreSQL fall back to scoring in process, still over the whole scope.
- Recall fuses the lexical ranking with those hits and grows the candidate
pool with matches the pool did not hold, which is what lets the search
escape it. The lexical pool is now the capacity cap, so it can no longer
be a second, invisible limit under the one that decides what stays active.
- Extraction picks the memories it shows the model the same way. This
matters more than recall: the memory a new statement contradicts is not
reliably an important one, so an importance-ordered window hid exactly
the collision that stops a duplicate being written.
- Vectors are written to the searchable column in the upsert's transaction;
rows predating the column are moved by `SyncVectorColumn` during
maintenance, which costs no model calls. The embedding backfill goes from
50 to 200 per run — at 50, a subject at the capacity cap took over a
month to become fully searchable.
- Traces and logs report `outside_pool`, the number of semantic matches the
lexical pool did not contain. It was structurally zero before, so it is
the number to watch when asking whether semantic recall does anything.
Tests cover whole-scope ranking, the lifecycle and kind filters, pool
growth, the pool-size invariant, and extraction seeing a low-importance
memory it would otherwise duplicate. A pgvector integration test
(WEKNORA_MEMORY_TEST_POSTGRES_DSN) exercises the migration, the halfvec
write, the SQL ranking and the column sync.
2026-09-13 03:06:12 +00:00
|
|
|
|
return hits, ""
|
2026-08-13 07:09:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// fuseRankings combines two ranked id lists by reciprocal rank fusion.
|
|
|
|
|
|
//
|
|
|
|
|
|
// RRF rather than a weighted score sum because the two signals are not on a
|
|
|
|
|
|
// comparable scale: cosine is bounded and calibrated, the lexical score is a
|
|
|
|
|
|
// bag-of-ngrams overlap count that means nothing in absolute terms. Fusing
|
|
|
|
|
|
// ranks sidesteps the question entirely, and an item both signals agree on
|
|
|
|
|
|
// beats one that only a single signal likes.
|
|
|
|
|
|
func fuseRankings(lexical, vector []int) []int {
|
|
|
|
|
|
scores := make(map[int]float64, len(lexical)+len(vector))
|
|
|
|
|
|
order := make([]int, 0, len(lexical)+len(vector))
|
|
|
|
|
|
seen := make(map[int]struct{}, len(lexical)+len(vector))
|
|
|
|
|
|
|
|
|
|
|
|
for _, list := range [][]int{lexical, vector} {
|
|
|
|
|
|
for rank, index := range list {
|
|
|
|
|
|
scores[index] += 1.0 / (rrfK + float64(rank))
|
|
|
|
|
|
if _, dup := seen[index]; !dup {
|
|
|
|
|
|
seen[index] = struct{}{}
|
|
|
|
|
|
order = append(order, index)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sortStableByIndexScore(order, func(index int) float64 { return scores[index] })
|
|
|
|
|
|
return order
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// backfillEmbeddings fills in vectors for memories written before an embedding
|
|
|
|
|
|
// model was available. Bounded per run; the daily maintenance pass calls it, so
|
|
|
|
|
|
// a large backlog drains over days rather than in one burst.
|
|
|
|
|
|
func (s *Service) backfillEmbeddings(
|
|
|
|
|
|
ctx context.Context, scope interfaces.MemoryScope, cfg *types.MemoryConfig,
|
|
|
|
|
|
) int {
|
|
|
|
|
|
modelID, ok := s.embedder(ctx, cfg)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
items, err := s.repo.ItemsMissingEmbeddings(ctx, scope, modelID, backfillPerRun)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.Warnf(ctx, "memory: find items missing embeddings failed: %v", err)
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
filled := 0
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
|
text := embeddableText(item, s.embedAliases(ctx, scope, item))
|
|
|
|
|
|
vector := s.embedText(ctx, modelID, text, embedWriteTimeout)
|
|
|
|
|
|
if len(vector) == 0 {
|
|
|
|
|
|
// The model just failed; the rest of this batch will fail too.
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
err := s.repo.UpsertItemEmbedding(ctx, scope, &types.MemoryItemEmbedding{
|
2026-09-12 16:04:16 +00:00
|
|
|
|
ItemID: item.ID,
|
|
|
|
|
|
SourceContent: item.Content,
|
|
|
|
|
|
SourceTopic: item.Topic,
|
|
|
|
|
|
ModelID: modelID,
|
|
|
|
|
|
Dims: len(vector),
|
|
|
|
|
|
Vector: types.EncodeEmbedding(vector),
|
2026-08-13 07:09:12 +00:00
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.Warnf(ctx, "memory: backfill embedding failed: %v", err)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
filled++
|
|
|
|
|
|
}
|
|
|
|
|
|
if filled > 0 {
|
|
|
|
|
|
logger.Infof(ctx, "memory: backfilled %d embeddings for %s", filled, scope.SubjectID)
|
|
|
|
|
|
}
|
|
|
|
|
|
return filled
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// sortStableByIndexScore sorts in place, highest score first, preserving the
|
|
|
|
|
|
// original order among ties so a stable input produces a stable output.
|
|
|
|
|
|
func sortStableByIndexScore(indexes []int, score func(int) float64) {
|
|
|
|
|
|
sort.SliceStable(indexes, func(i, j int) bool {
|
|
|
|
|
|
return score(indexes[i]) > score(indexes[j])
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|