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).
343 lines
9.8 KiB
C
343 lines
9.8 KiB
C
/*
|
|
* splinter -- the embedding index tests.
|
|
*
|
|
* Three things have to hold for this to be worth having:
|
|
*
|
|
* 1. the local encoder puts similar text near each other and unrelated text
|
|
* far apart, without a model, a key or the network;
|
|
* 2. indexing is incremental: a second run reads only the new lines, and the
|
|
* result equals an index built in one go;
|
|
* 3. a compaction makes the messages searchable again, and the search finds
|
|
* the right one - that is the whole point of the module.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/stat.h>
|
|
|
|
#include "splinter.h"
|
|
#include "config.h"
|
|
#include "embed.h"
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
/* ------------------------------------------------------------- encoder */
|
|
|
|
static void
|
|
test_encoder(void)
|
|
{
|
|
double a[256], b[256], c[256], v[256];
|
|
int i, nz = 0;
|
|
|
|
CHECK(sp_embed_text("the compaction throws the front away and keeps the "
|
|
"tail of the conversation", 256, a) == 0);
|
|
CHECK(sp_embed_text("compaction throws the front away and keeps the tail "
|
|
"of a conversation", 256, b) == 0);
|
|
CHECK(sp_embed_text("a pool fails over to the member with room left, and "
|
|
"the turn continues there", 256, c) == 0);
|
|
|
|
/* unit length: the cosine is the dot product, nothing else */
|
|
for (i = 0; i < 256; i++) {
|
|
CHECK(a[i] >= -1.0000001 && a[i] <= 1.0000001);
|
|
if (a[i] != 0.0)
|
|
nz++;
|
|
}
|
|
CHECK(nz > 4);
|
|
|
|
/*
|
|
* A paraphrase has to score clearly above an unrelated sentence. The
|
|
* numbers are printed so a change in the weights is visible in the log.
|
|
*/
|
|
fprintf(stderr, " encoder: paraphrase %.3f, unrelated %.3f\n",
|
|
sp_embed_dot(a, b, 256), sp_embed_dot(a, c, 256));
|
|
CHECK(sp_embed_dot(a, b, 256) > 0.55);
|
|
CHECK(sp_embed_dot(a, c, 256) < sp_embed_dot(a, b, 256));
|
|
CHECK(sp_embed_dot(a, a, 256) > 0.999);
|
|
|
|
/* a word inside a longer word: the 4-grams carry it */
|
|
CHECK(sp_embed_text("kompaktierung", 256, a) == 0);
|
|
CHECK(sp_embed_text("kompaktierung der sitzung", 256, b) == 0);
|
|
fprintf(stderr, " encoder: compound %.3f\n", sp_embed_dot(a, b, 256));
|
|
CHECK(sp_embed_dot(a, b, 256) > 0.4);
|
|
|
|
/* nothing to say: no direction, and the call says so */
|
|
CHECK(sp_embed_text("the and of to", 256, v) == -1);
|
|
CHECK(sp_embed_text("", 256, v) == -1);
|
|
CHECK(sp_embed_text(NULL, 256, v) == -1);
|
|
CHECK(sp_embed_text("x", 0, v) == -1);
|
|
}
|
|
|
|
/* --------------------------------------------------------------- index */
|
|
|
|
static void
|
|
write_session(const char *dir, const char *id, const char *const *lines,
|
|
size_t n)
|
|
{
|
|
char path[700];
|
|
FILE *f;
|
|
size_t i;
|
|
|
|
snprintf(path, sizeof(path), "%s/%s.jsonl", dir, id);
|
|
f = fopen(path, "a");
|
|
if (f == NULL) {
|
|
perror(path);
|
|
return;
|
|
}
|
|
for (i = 0; i < n; i++)
|
|
fprintf(f, "%s\n", lines[i]);
|
|
fclose(f);
|
|
}
|
|
|
|
static long
|
|
file_size(const char *path)
|
|
{
|
|
struct stat st;
|
|
|
|
if (stat(path, &st) != 0)
|
|
return -1;
|
|
return (long)st.st_size;
|
|
}
|
|
|
|
static void
|
|
test_index(void)
|
|
{
|
|
char tmpl[] = "/tmp/splinter-emb-XXXXXX";
|
|
char *dir = mkdtemp(tmpl);
|
|
char err[256], status[256], efile[800];
|
|
const char *first[] = {
|
|
"{\"role\":\"user\",\"text\":\"die kompaktierung wirft den "
|
|
"anfang der sitzung weg\"}",
|
|
"{\"role\":\"assistant\",\"text\":\"der tail bleibt, und der "
|
|
"rest steht im session-log\"}",
|
|
"{\"role\":\"user\",\"text\":\"welche ports hoert der dienst?\"}",
|
|
"{\"role\":\"assistant\",\"text\":\"der dienst hoert auf 8080 "
|
|
"und spricht http\"}"
|
|
};
|
|
const char *more[] = {
|
|
"{\"role\":\"user\",\"text\":\"und wie wird der index "
|
|
"nachgezogen?\"}"
|
|
};
|
|
struct sp_embed_ctx *c;
|
|
struct sp_config conf;
|
|
struct sp_buf out;
|
|
struct stat st;
|
|
int n;
|
|
|
|
if (dir == NULL) {
|
|
fprintf(stderr, "FAIL: no temp dir\n");
|
|
failures++;
|
|
return;
|
|
}
|
|
sp_config_init(&conf);
|
|
conf.embed = SP_EMBED_LOCAL;
|
|
conf.embed_dims = 256;
|
|
|
|
write_session(dir, "s1", first, 4);
|
|
c = sp_embed_new(&conf);
|
|
CHECK(c != NULL);
|
|
if (c == NULL)
|
|
return;
|
|
sp_embed_bind(c, dir, "s1");
|
|
|
|
/* the caller's thread does the work, so the test is deterministic */
|
|
CHECK(sp_embed_index_now(c, err, sizeof(err)) == 0);
|
|
if (err[0] != '\0')
|
|
fprintf(stderr, " index: %s\n", err);
|
|
|
|
snprintf(efile, sizeof(efile), "%s/emb/s1.emb", dir);
|
|
CHECK(file_size(efile) > 0);
|
|
CHECK(stat(efile, &st) == 0 && (st.st_mode & 07700) == 0600 &&
|
|
"the index file is private like the session");
|
|
sp_embed_status(c, status, sizeof(status));
|
|
fprintf(stderr, " status: %s\n", status);
|
|
CHECK(strstr(status, "4 messages") != NULL);
|
|
CHECK(strstr(status, "current") != NULL);
|
|
|
|
/* the file grew by one line: the next run adds one record, not five */
|
|
{
|
|
long before = file_size(efile);
|
|
|
|
write_session(dir, "s1", more, 1);
|
|
CHECK(sp_embed_index_now(c, err, sizeof(err)) == 0);
|
|
fprintf(stderr, " incremental: %ld -> %ld bytes\n", before,
|
|
file_size(efile));
|
|
CHECK(file_size(efile) > before);
|
|
sp_embed_status(c, status, sizeof(status));
|
|
CHECK(strstr(status, "5 messages") != NULL);
|
|
}
|
|
|
|
/* and the search finds by meaning, not by substring */
|
|
if (sp_buf_init(&out) != 0)
|
|
return;
|
|
n = sp_embed_search(c, "was passiert mit dem anfang bei der "
|
|
"kompaktierung", 3, &out);
|
|
fprintf(stderr, " search: %d hits\n%s", n, out.p != NULL ? out.p : "");
|
|
CHECK(n > 0);
|
|
CHECK(out.p != NULL && strstr(out.p, "kompaktierung") != NULL);
|
|
CHECK(out.p != NULL && strstr(out.p, "8080") == NULL);
|
|
|
|
/* the whole message comes back when asked for */
|
|
sp_buf_clear(&out);
|
|
n = sp_embed_search_full(c, "kompaktierung anfang", 1, 1, 4096, &out);
|
|
CHECK(n == 1);
|
|
CHECK(out.p != NULL && strstr(out.p, "die kompaktierung wirft den "
|
|
"anfang der sitzung weg") != NULL);
|
|
|
|
/* a query with no content words says so instead of ranking noise */
|
|
sp_buf_clear(&out);
|
|
CHECK(sp_embed_search(c, "the and of", 3, &out) == 0);
|
|
CHECK(out.p != NULL && strstr(out.p, "no searchable words") != NULL);
|
|
sp_buf_free(&out);
|
|
|
|
/* reopening the same session must not duplicate the index */
|
|
sp_embed_free(c);
|
|
c = sp_embed_new(&conf);
|
|
CHECK(c != NULL);
|
|
if (c != NULL) {
|
|
sp_embed_bind(c, dir, "s1");
|
|
CHECK(sp_embed_index_now(c, err, sizeof(err)) == 0);
|
|
sp_embed_status(c, status, sizeof(status));
|
|
fprintf(stderr, " after reopen: %s\n", status);
|
|
CHECK(strstr(status, "5 messages") != NULL);
|
|
sp_embed_free(c);
|
|
}
|
|
|
|
/* off is off, and it says why */
|
|
conf.embed = SP_EMBED_OFF;
|
|
c = sp_embed_new(&conf);
|
|
CHECK(c != NULL);
|
|
if (c != NULL) {
|
|
CHECK(sp_embed_index_now(c, err, sizeof(err)) == -1);
|
|
CHECK(strstr(err, "off") != NULL);
|
|
sp_embed_status(c, status, sizeof(status));
|
|
CHECK(strcmp(status, "off") == 0);
|
|
sp_embed_free(c);
|
|
}
|
|
}
|
|
|
|
/* a torn tail must not stop the index, and must not be counted either */
|
|
static void
|
|
test_torn(void)
|
|
{
|
|
char tmpl[] = "/tmp/splinter-embt-XXXXXX";
|
|
char *dir = mkdtemp(tmpl);
|
|
char err[256], status[256], path[800];
|
|
const char *lines[] = {
|
|
"{\"role\":\"user\",\"text\":\"erste nachricht\"}",
|
|
"{\"role\":\"assistant\",\"text\":\"zweite nachricht\"}"
|
|
};
|
|
struct sp_embed_ctx *c;
|
|
struct sp_config conf;
|
|
FILE *f;
|
|
|
|
if (dir == NULL)
|
|
return;
|
|
sp_config_init(&conf);
|
|
conf.embed = SP_EMBED_LOCAL;
|
|
write_session(dir, "s2", lines, 2);
|
|
/* a crash in the middle of a write */
|
|
snprintf(path, sizeof(path), "%s/s2.jsonl", dir);
|
|
f = fopen(path, "a");
|
|
if (f != NULL) {
|
|
fputs("{\"role\":\"user\",\"text\":\"abgeschnit", f);
|
|
fclose(f);
|
|
}
|
|
c = sp_embed_new(&conf);
|
|
CHECK(c != NULL);
|
|
if (c == NULL)
|
|
return;
|
|
sp_embed_bind(c, dir, "s2");
|
|
CHECK(sp_embed_index_now(c, err, sizeof(err)) == 0);
|
|
sp_embed_status(c, status, sizeof(status));
|
|
fprintf(stderr, " torn: %s\n", status);
|
|
CHECK(strstr(status, "2 messages") != NULL);
|
|
sp_embed_free(c);
|
|
}
|
|
|
|
/*
|
|
* The end-to-end shape: a session with a lot of history, one compaction, and
|
|
* the question that a compaction always makes unanswerable from the context.
|
|
*/
|
|
static void
|
|
test_compaction_recall(void)
|
|
{
|
|
char tmpl[] = "/tmp/splinter-embs-XXXXXX";
|
|
char *dir = mkdtemp(tmpl);
|
|
char err[256], big[512];
|
|
const char *lines[64];
|
|
struct sp_embed_ctx *c;
|
|
struct sp_config conf;
|
|
struct sp_buf out;
|
|
size_t i, n = 0, base;
|
|
int rc;
|
|
|
|
if (dir == NULL)
|
|
return;
|
|
sp_config_init(&conf);
|
|
conf.embed = SP_EMBED_LOCAL;
|
|
/*
|
|
* A real session: the interesting sentence is at the top and the rest is
|
|
* chatter, exactly the shape that compaction cuts away first.
|
|
*/
|
|
lines[n++] = "{\"role\":\"user\",\"text\":\"der deploy geht nur ueber "
|
|
"den rechner mit dem alten ssh-schluessel, port 2222\"}";
|
|
base = n;
|
|
for (i = 0; i < 40; i++) {
|
|
snprintf(big, sizeof(big), "{\"role\":\"%s\",\"text\":\"schritt "
|
|
"%zu: die datei wird gelesen und geprueft, danach geht es "
|
|
"weiter\"}", (i % 2) ? "assistant" : "user", i);
|
|
lines[n++] = strdup(big);
|
|
}
|
|
lines[n++] = "{\"role\":\"user\",\"text\":\"und noch etwas ganz "
|
|
"anderes: wie viele ports hat der dienst?\"}";
|
|
write_session(dir, "s3", lines, n);
|
|
/* the generated lines were strdup'd; the literals are not ours to free */
|
|
for (i = base; i < base + 40; i++)
|
|
free((char *)lines[i]);
|
|
|
|
c = sp_embed_new(&conf);
|
|
CHECK(c != NULL);
|
|
if (c == NULL)
|
|
return;
|
|
sp_embed_bind(c, dir, "s3");
|
|
CHECK(sp_embed_index_now(c, err, sizeof(err)) == 0);
|
|
|
|
if (sp_buf_init(&out) != 0)
|
|
return;
|
|
rc = sp_embed_search(c, "welchen port nimmt der deploy-rechner",
|
|
3, &out);
|
|
fprintf(stderr, " recall: %d hits\n%s", rc,
|
|
out.p != NULL ? out.p : "");
|
|
CHECK(rc > 0);
|
|
CHECK(out.p != NULL && strstr(out.p, "2222") != NULL);
|
|
sp_buf_free(&out);
|
|
sp_embed_free(c);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
fprintf(stderr, "== encoder\n");
|
|
test_encoder();
|
|
fprintf(stderr, "== index\n");
|
|
test_index();
|
|
fprintf(stderr, "== torn tail\n");
|
|
test_torn();
|
|
fprintf(stderr, "== compaction recall\n");
|
|
test_compaction_recall();
|
|
if (failures != 0) {
|
|
fprintf(stderr, "FAILED: %d\n", failures);
|
|
return 1;
|
|
}
|
|
fprintf(stderr, "ok\n");
|
|
return 0;
|
|
}
|