Switch memory on: the agent may keep one, and carries them into the next run
Some checks failed
CI / test (push) Failing after 4m38s
CI / fixture (push) Failing after 8s

The store and the write trigger existed and nothing used either. This wires
both ends, so "we have long-term memory" stops being a statement about code
that exists and becomes one about behaviour.

READ. Memories are recalled before retrieval and placed before it in the
prompt: it is the smaller block and the more general one — a standing
preference frames how the documents should be read, where a document does not
frame a preference. The question stays last, because a model reads the last
thing and answers it, and evidence after the question becomes the prompt.

Skipped for smalltalk on the same terms as retrieval. Nobody needs remembering
to say good morning, and paying for it is how "hi" came to cost six thousand
tokens.

FAILS QUIET, RECORDED LOUDLY. A memory store that is unreachable must not take
the run with it: an answer without memory is worse, not wrong, and the
alternative is an outage in the knowledge layer becoming an outage in the
product. The trajectory records the failure, and records separately when the
store returned recency instead of relevance — a reader comparing two answers
needs to know which one got which.

WRITE. tools.Remember is registered only where there is somewhere to put it,
through DefaultToolsWithMemory rather than a nil check inside the old
constructor: a deployment that has not migrated 000017 must not offer a tool
whose every call fails against a table that is not there. Passing nil
registers exactly the catalogue that was there before.

Memory shares retrieval's embedder, and memory.Embedder is knowledge.Embedder
by structure so it cannot be given a different one. Two embedding models in
one deployment produce vectors that cannot be compared, and the failure is
silent: a recall that returns nothing rather than an error.

Five new tests on the loop, including the two that matter — a failing store
still answers, and a greeting carries nothing.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-10-07 20:09:59 +05:30
parent 57abafe73b
commit 43dabb5f72
5 changed files with 250 additions and 14 deletions

View File

@@ -38,6 +38,7 @@ import (
"time"
"github.com/krow/krow-backend/go-api/internal/authctx"
"github.com/krow/krow-backend/go-api/internal/knowledge"
"github.com/krow/krow-backend/go-api/internal/repo"
)
@@ -141,11 +142,15 @@ func (w Write) Validate() error {
return nil
}
// Embedder turns text into a comparable vector. The knowledge package's
// embedder satisfies this; memory does not define its own, so a deployment
// cannot end up with two embedding models and vectors that cannot be compared.
// Embedder turns text into a comparable vector.
//
// It is knowledge.Embedder by structure rather than by import: memory does not
// define its own embedding, because two embedding models in one deployment
// produce vectors that cannot be compared and the failure is silent — a recall
// that returns nothing rather than an error. Declaring the shape here keeps
// the dependency one-way while making it impossible to pass a different one.
type Embedder interface {
Embed(ctx context.Context, texts []string, kind string) ([][]float32, error)
Embed(ctx context.Context, texts []string, kind knowledge.Kind) ([][]float32, error)
Model() string
}
@@ -183,7 +188,7 @@ func (s *Store) Remember(ctx context.Context, who authctx.Identity, w Write) (st
var vector []float32
model := ""
if s.embedder != nil {
vectors, err := s.embedder.Embed(ctx, []string{w.Text}, "document")
vectors, err := s.embedder.Embed(ctx, []string{w.Text}, knowledge.KindDocument)
// Degraded, not failed: a memory that is stored but not yet searchable
// is recoverable by re-embedding, and losing it is not.
if err == nil && len(vectors) == 1 && len(vectors[0]) > 0 {
@@ -274,7 +279,7 @@ func (s *Store) Recall(ctx context.Context, who authctx.Identity, question strin
}
if s.embedder != nil && strings.TrimSpace(question) != "" {
vectors, err := s.embedder.Embed(ctx, []string{question}, "query")
vectors, err := s.embedder.Embed(ctx, []string{question}, knowledge.KindQuery)
if err == nil && len(vectors) == 1 && len(vectors[0]) > 0 {
rows, err := s.query(ctx, `
SELECT id, subject_type, coalesce(subject_id::text, ''), text, author,