Two bugs that made `embed = openai` useless while looking healthy:
1. encode_remote() read every vector element with mcp_arg_num(el, NULL, 0.0).
mcp_arg_num() looks up a key in an object; on a bare array element it always
returned the default. Every stored vector was zeros, so the index was
"built" and no search could ever match.
2. The search encodes the question locally, even when the index was built
remotely. Two unrelated vector spaces: a query never landed near anything.
encode_query() now sends the question to the same endpoint that built the
index.
Proof in tests/test_embed.c (test_remote_vectors_are_the_models): a forked
HTTP server answers /v1/embeddings with a bag-of-words vector that is not the
local hash. Without the fix the stored vector is all 'A' (base64 zeros) and
the search finds nothing; with the fix the stored vector carries 5 non-zero
components and "which port does the deploy machine use" hits the right message
at 0.713.
make check: all tests passed. make check-asan: all 45 test binaries clean.
catch_up() allocated one vector per record and only commit() ever
freed them: a remote encoder that refused the batch (no endpoint, a
non-2xx, a wrong dimension count) leaked dims doubles per record on
every retry, and the worker retried until it parked.
valgrind on tests/test_embed before the fix: 512 bytes definitely
lost in 4 blocks; after: 0 errors. `make check`: all tests passed.
Compaction drops most of a session out of the window, and the only way back
to it was a substring search. That answers "where does this word stand",
not "what did we say about this".
history_search is the second question: every message the session ever wrote
gets a vector, and the model asks by meaning. Three rules shaped it.
It works with no model. The default is a local encoder - the hashing trick
over words, word bigrams and character 4-grams, 256 dimensions, pure CPU -
so remembering does not stop when an API key expires. `embed = openai`
points it at a real embedding model instead. The index records which one
built it: a vector from one space compared against another is noise that
looks like a result.
It never costs the turn. Embedding runs on its own thread; the turn only
says "this session grew" (sp_embed_note_compaction, called from the
compaction event) and goes on. An index that lags is reported as lagging.
It indexes the session file, not the conversation. The conversation is what
is left after the last compaction; the file is everything that was said.
"Before compaction" is not a snapshot to take, it is the file - and the
index stores the byte offset it reached, so the update is incremental for
free. It is derived data, append-only at <session dir>/emb/<id>.emb, and
losing it costs a rebuild, never history.
/status gains a row for it (index: N messages, lags by M).