|
|
|
@@ -6826,6 +6826,116 @@ static int istr_contains(const char* hay, const char* needle) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ---- SPEC-SEARCH-UPGRADE-OURS-2026-07-14: ranked search (BM25 + recency) ----
|
|
|
|
|
* Replaces first-N-in-storage-order substring matching (measured 13% hit@5 on
|
|
|
|
|
* the 15-query pinned eval; ranked model measured 93% offline). Deterministic,
|
|
|
|
|
* local, transparent — no model call on the hot path. Multi-word queries score
|
|
|
|
|
* per-token (rare+concentrated terms weigh most); ties break newest-first so
|
|
|
|
|
* fresh memories stop losing to storage order. The transparent-layer identity
|
|
|
|
|
* filter is preserved unchanged: hidden self layers stay invisible here and
|
|
|
|
|
* surface only via engram_activate — the legitimate path. */
|
|
|
|
|
|
|
|
|
|
#define ENGRAM_BM25_MAX_QTOK 16
|
|
|
|
|
#define ENGRAM_BM25_TOKLEN 48
|
|
|
|
|
|
|
|
|
|
static int engram_tok_next(const char** ps, char* out, int cap) {
|
|
|
|
|
const char* s = *ps;
|
|
|
|
|
while (*s && !isalnum((unsigned char)*s)) s++;
|
|
|
|
|
if (!*s) { *ps = s; return 0; }
|
|
|
|
|
int n = 0;
|
|
|
|
|
while (*s && isalnum((unsigned char)*s)) {
|
|
|
|
|
if (n < cap - 1) out[n++] = (char)tolower((unsigned char)*s);
|
|
|
|
|
s++;
|
|
|
|
|
}
|
|
|
|
|
out[n] = 0; *ps = s; return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void engram_field_stats(const char* field,
|
|
|
|
|
char qtok[][ENGRAM_BM25_TOKLEN], int nq,
|
|
|
|
|
int64_t* tf, int64_t* doclen) {
|
|
|
|
|
if (!field) return;
|
|
|
|
|
char buf[ENGRAM_BM25_TOKLEN];
|
|
|
|
|
const char* p = field;
|
|
|
|
|
while (engram_tok_next(&p, buf, sizeof buf)) {
|
|
|
|
|
(*doclen)++;
|
|
|
|
|
for (int t = 0; t < nq; t++)
|
|
|
|
|
if (strcmp(buf, qtok[t]) == 0) tf[t]++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct { double score; int64_t created; int64_t idx; } EngramHit;
|
|
|
|
|
|
|
|
|
|
static int engram_hit_cmp(const void* a, const void* b) {
|
|
|
|
|
const EngramHit* x = (const EngramHit*)a;
|
|
|
|
|
const EngramHit* y = (const EngramHit*)b;
|
|
|
|
|
if (x->score != y->score) return (x->score < y->score) ? 1 : -1;
|
|
|
|
|
if (x->created != y->created) return (x->created < y->created) ? 1 : -1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Scores every visible node against the query; writes ranked hits into `out`
|
|
|
|
|
* (caller allocates g->node_count entries). Returns min(hits, lim). */
|
|
|
|
|
static int64_t engram_search_ranked(EngramStore* g, const char* q, int64_t lim,
|
|
|
|
|
EngramHit* out) {
|
|
|
|
|
char qtok[ENGRAM_BM25_MAX_QTOK][ENGRAM_BM25_TOKLEN];
|
|
|
|
|
int nq = 0;
|
|
|
|
|
{
|
|
|
|
|
const char* p = q; char buf[ENGRAM_BM25_TOKLEN];
|
|
|
|
|
while (nq < ENGRAM_BM25_MAX_QTOK && engram_tok_next(&p, buf, sizeof buf)) {
|
|
|
|
|
int dup = 0;
|
|
|
|
|
for (int t = 0; t < nq; t++)
|
|
|
|
|
if (strcmp(qtok[t], buf) == 0) { dup = 1; break; }
|
|
|
|
|
if (!dup) { strcpy(qtok[nq], buf); nq++; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (nq == 0) return 0;
|
|
|
|
|
|
|
|
|
|
int64_t N = g->node_count;
|
|
|
|
|
int64_t* tfm = (int64_t*)calloc((size_t)(N * nq), sizeof(int64_t));
|
|
|
|
|
int64_t* dlen = (int64_t*)calloc((size_t)N, sizeof(int64_t));
|
|
|
|
|
if (!tfm || !dlen) { free(tfm); free(dlen); return 0; }
|
|
|
|
|
int64_t df[ENGRAM_BM25_MAX_QTOK] = {0};
|
|
|
|
|
double total_len = 0.0; int64_t live = 0;
|
|
|
|
|
for (int64_t i = 0; i < N; i++) {
|
|
|
|
|
EngramNode* n = &g->nodes[i];
|
|
|
|
|
if (engram_layer_is_transparent(n->layer_id)) continue;
|
|
|
|
|
live++;
|
|
|
|
|
int64_t* tf = &tfm[i * nq];
|
|
|
|
|
engram_field_stats(n->content, qtok, nq, tf, &dlen[i]);
|
|
|
|
|
engram_field_stats(n->label, qtok, nq, tf, &dlen[i]);
|
|
|
|
|
engram_field_stats(n->tags, qtok, nq, tf, &dlen[i]);
|
|
|
|
|
total_len += (double)dlen[i];
|
|
|
|
|
for (int t = 0; t < nq; t++) if (tf[t] > 0) df[t]++;
|
|
|
|
|
}
|
|
|
|
|
double avg = (live > 0) ? total_len / (double)live : 1.0;
|
|
|
|
|
if (avg <= 0.0) avg = 1.0;
|
|
|
|
|
const double k1 = 1.2, b = 0.75;
|
|
|
|
|
int64_t nhits = 0;
|
|
|
|
|
for (int64_t i = 0; i < N; i++) {
|
|
|
|
|
EngramNode* n = &g->nodes[i];
|
|
|
|
|
if (engram_layer_is_transparent(n->layer_id)) continue;
|
|
|
|
|
int64_t* tf = &tfm[i * nq];
|
|
|
|
|
double s = 0.0;
|
|
|
|
|
for (int t = 0; t < nq; t++) {
|
|
|
|
|
if (tf[t] == 0) continue;
|
|
|
|
|
double idf = log(((double)live - (double)df[t] + 0.5) /
|
|
|
|
|
((double)df[t] + 0.5) + 1.0);
|
|
|
|
|
double tfd = (double)tf[t];
|
|
|
|
|
s += idf * (tfd * (k1 + 1.0)) /
|
|
|
|
|
(tfd + k1 * (1.0 - b + b * (double)dlen[i] / avg));
|
|
|
|
|
}
|
|
|
|
|
if (s > 0.0) {
|
|
|
|
|
out[nhits].score = s;
|
|
|
|
|
out[nhits].created = n->created_at;
|
|
|
|
|
out[nhits].idx = i;
|
|
|
|
|
nhits++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free(tfm); free(dlen);
|
|
|
|
|
qsort(out, (size_t)nhits, sizeof(EngramHit), engram_hit_cmp);
|
|
|
|
|
return (nhits < lim) ? nhits : lim;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
el_val_t engram_search(el_val_t query, el_val_t limit) {
|
|
|
|
|
EngramStore* g = engram_get();
|
|
|
|
|
const char* q = EL_CSTR(query);
|
|
|
|
@@ -6833,21 +6943,13 @@ el_val_t engram_search(el_val_t query, el_val_t limit) {
|
|
|
|
|
if (lim <= 0) lim = 100;
|
|
|
|
|
el_val_t lst = el_list_empty();
|
|
|
|
|
if (!q || !*q) return lst;
|
|
|
|
|
int64_t found = 0;
|
|
|
|
|
for (int64_t i = 0; i < g->node_count && found < lim; i++) {
|
|
|
|
|
EngramNode* n = &g->nodes[i];
|
|
|
|
|
/* Filter transparent layers: nodes whose layer is `transparent=1`
|
|
|
|
|
* shape output but are invisible to introspection ("what do you
|
|
|
|
|
* know about yourself"). They still surface via engram_activate
|
|
|
|
|
* + engram_compile_layered_json — that's the legitimate path. */
|
|
|
|
|
if (engram_layer_is_transparent(n->layer_id)) continue;
|
|
|
|
|
if (istr_contains(n->content, q) ||
|
|
|
|
|
istr_contains(n->label, q) ||
|
|
|
|
|
istr_contains(n->tags, q)) {
|
|
|
|
|
lst = el_list_append(lst, engram_node_to_map(n));
|
|
|
|
|
found++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (g->node_count == 0) return lst;
|
|
|
|
|
EngramHit* hits = (EngramHit*)malloc((size_t)g->node_count * sizeof(EngramHit));
|
|
|
|
|
if (!hits) return lst;
|
|
|
|
|
int64_t k = engram_search_ranked(g, q, lim, hits);
|
|
|
|
|
for (int64_t i = 0; i < k; i++)
|
|
|
|
|
lst = el_list_append(lst, engram_node_to_map(&g->nodes[hits[i].idx]));
|
|
|
|
|
free(hits);
|
|
|
|
|
return lst;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -7762,27 +7864,23 @@ el_val_t engram_get_node_json(el_val_t id) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
el_val_t engram_search_json(el_val_t query, el_val_t limit) {
|
|
|
|
|
/* SPEC-SEARCH-UPGRADE 2026-07-14: same ranked BM25+recency core as
|
|
|
|
|
* engram_search; transparent-layer identity filter enforced inside it. */
|
|
|
|
|
EngramStore* g = engram_get();
|
|
|
|
|
const char* q = EL_CSTR(query);
|
|
|
|
|
int64_t lim = (int64_t)limit;
|
|
|
|
|
if (lim <= 0) lim = 100;
|
|
|
|
|
JsonBuf b; jb_init(&b);
|
|
|
|
|
jb_putc(&b, '[');
|
|
|
|
|
int first = 1;
|
|
|
|
|
int64_t found = 0;
|
|
|
|
|
if (q && *q) {
|
|
|
|
|
for (int64_t i = 0; i < g->node_count && found < lim; i++) {
|
|
|
|
|
EngramNode* n = &g->nodes[i];
|
|
|
|
|
/* Filter transparent layers — same as engram_search. */
|
|
|
|
|
if (engram_layer_is_transparent(n->layer_id)) continue;
|
|
|
|
|
if (istr_contains(n->content, q) ||
|
|
|
|
|
istr_contains(n->label, q) ||
|
|
|
|
|
istr_contains(n->tags, q)) {
|
|
|
|
|
if (!first) jb_putc(&b, ',');
|
|
|
|
|
engram_emit_node_json(&b, n);
|
|
|
|
|
first = 0;
|
|
|
|
|
found++;
|
|
|
|
|
if (q && *q && g->node_count > 0) {
|
|
|
|
|
EngramHit* hits = (EngramHit*)malloc((size_t)g->node_count * sizeof(EngramHit));
|
|
|
|
|
if (hits) {
|
|
|
|
|
int64_t k = engram_search_ranked(g, q, lim, hits);
|
|
|
|
|
for (int64_t i = 0; i < k; i++) {
|
|
|
|
|
if (i) jb_putc(&b, ',');
|
|
|
|
|
engram_emit_node_json(&b, &g->nodes[hits[i].idx]);
|
|
|
|
|
}
|
|
|
|
|
free(hits);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
jb_putc(&b, ']');
|
|
|
|
|