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>
70 lines
2.2 KiB
Go
70 lines
2.2 KiB
Go
package runtime
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"github.com/krow/krow-backend/go-api/internal/authctx"
|
|
"github.com/krow/krow-backend/go-api/internal/memory"
|
|
)
|
|
|
|
// Recaller is the memory store's read half, as the loop needs it.
|
|
//
|
|
// An interface rather than the struct so the runtime does not depend on
|
|
// memory's internals and a test can drive the loop without a database — the
|
|
// same shape Retriever has, for the same reason.
|
|
type Recaller interface {
|
|
Recall(ctx context.Context, who authctx.Identity, question string, limit int) ([]memory.Record, string, error)
|
|
}
|
|
|
|
// WithMemory gives the executor a long-term memory to read.
|
|
//
|
|
// Optional, like the retriever. Nil means a deployment that has not migrated
|
|
// 000017, or has chosen not to remember, and the loop behaves exactly as it
|
|
// did before memory existed.
|
|
func (m *ModelExecutor) WithMemory(r Recaller) *ModelExecutor {
|
|
m.memory = r
|
|
return m
|
|
}
|
|
|
|
// recall returns the memory block for this question, or "".
|
|
//
|
|
// FAILS QUIET, LOUDLY RECORDED. A memory store that is unreachable must not
|
|
// take the run with it: the 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 says what happened, so an answer that reads thin is
|
|
// explainable afterwards rather than mysterious.
|
|
//
|
|
// The degraded note from the store — "no embedder is configured; these
|
|
// memories are the most recent rather than the most relevant" — is recorded
|
|
// too. A reader comparing two answers needs to know which one got relevance
|
|
// and which got recency.
|
|
func (m *ModelExecutor) recall(ctx context.Context, rec *Recorder, input ExecutionInput, question string) string {
|
|
if m.memory == nil {
|
|
return ""
|
|
}
|
|
|
|
records, degraded, err := m.memory.Recall(ctx, input.Identity, question, memory.DefaultRecall)
|
|
if err != nil {
|
|
rec.Error("memory.failed", err.Error())
|
|
return ""
|
|
}
|
|
if degraded != "" {
|
|
rec.Error("memory.degraded", degraded)
|
|
}
|
|
if len(records) == 0 {
|
|
return ""
|
|
}
|
|
|
|
rec.Error("runtime.memory_recalled",
|
|
pluralMemories(len(records))+" carried into this run")
|
|
return memory.Render(records)
|
|
}
|
|
|
|
func pluralMemories(n int) string {
|
|
if n == 1 {
|
|
return "1 memory"
|
|
}
|
|
return strconv.Itoa(n) + " memories"
|
|
}
|