Files
el/engram/test/test_interoception_p0_emb.c
T
will.anderson c20cb3b97c M-INTEROCEPTION P0: add read-only engram_scan_nodes_emb_json builtin
Read routes (GET /api/embeddings, /api/graph/dump) need node embedding
vectors, but every consumer emit path deliberately drops the ~5.7KB emb
vector (include_emb=0) to stay under MCP token limits. Rather than perturb
that shared path, add a dedicated additive builtin that pages nodes WITH
their dense vector, emitting id/node_type/label/created_at/emb_dim plus emb
as a JSON array whose length equals emb_dim (so a consumer can verify the
vector round-trips). Un-embedded nodes emit emb_dim:0 / emb:[]. Same
salience-sorted, transparent-layer-skipped, bounded pagination as
engram_scan_nodes_json; default page 256.

Purely additive: engram_emit_node_json and the default include_emb=0 are
untouched, so every existing endpoint is byte-identical to trunk (verified:
scan_nodes_json still carries no emb). The HTTP route wiring in server.el is
DEFERRED to cutover per the elc-drift blocker (regenerating engram/dist
diverges ~285 lines with no source change); the builtin is exercised
directly by the pure-C gate instead.

Measured on a copy: len(emb)==emb_dim for all nodes, pagination disjoint,
existing path unchanged; full 256-node x 768-dim page = 16.7 ms / 1.18 MB.
ASan+UBSan clean.
2026-08-12 23:28:13 -05:00

80 lines
3.5 KiB
C

/* test_interoception_p0_emb.c — M-INTEROCEPTION Priority 0.
*
* Verifies the new READ-ONLY builtin engram_scan_nodes_emb_json(limit,offset):
* - every emitted node carries emb_dim and an emb JSON array of that length,
* - nodes without an embedding emit emb_dim:0 / emb:[],
* - pagination (limit/offset) is honoured,
* - the count matches engram_node_count,
* - the EXISTING engram_scan_nodes_json path is byte-unchanged (no emb field),
* i.e. the addition is purely additive / behavior-neutral.
*
* Pure-C harness (no elc). We craft a snapshot with real emb vectors, load it
* (engram_load parses "emb" comma-lists into node->emb via eg_parse_emb), then
* dump via both scan paths. Assertions live in run_interoception_p0.sh.
*/
#include "el_runtime.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static el_val_t S(const char* s){ return EL_STR(s); }
/* 16-d embedding as a comma list (>=8 required by eg_parse_emb). */
static void emb_list(char* out, size_t cap, int dim, double base){
size_t o = 0;
for (int i = 0; i < dim; i++){
o += snprintf(out+o, cap-o, "%s%.4f", i?",":"", base + 0.01*i);
}
}
int main(int argc, char** argv){
if (argc < 2){ fprintf(stderr, "usage: %s <dir>\n", argv[0]); return 2; }
const char* dir = argv[1];
char snap[1024]; snprintf(snap, sizeof snap, "%s/seed.json", dir);
char e1[512], e2[512];
emb_list(e1, sizeof e1, 16, 0.10);
emb_list(e2, sizeof e2, 16, 0.50);
/* Two embedded nodes (distinct salience → deterministic sort order) and one
* un-embedded node. */
FILE* f = fopen(snap, "w");
if (!f){ perror("fopen"); return 2; }
fprintf(f,
"{\"nodes\":["
"{\"id\":\"n-high\",\"content\":\"high salience embedded\",\"node_type\":\"Concept\","
"\"label\":\"emb-high\",\"tier\":\"Semantic\",\"salience\":0.9,\"importance\":0.8,"
"\"confidence\":1.0,\"created_at\":1000,\"emb\":\"%s\"},"
"{\"id\":\"n-mid\",\"content\":\"mid salience embedded\",\"node_type\":\"Concept\","
"\"label\":\"emb-mid\",\"tier\":\"Semantic\",\"salience\":0.5,\"importance\":0.5,"
"\"confidence\":1.0,\"created_at\":2000,\"emb\":\"%s\"},"
"{\"id\":\"n-low\",\"content\":\"low salience no embedding\",\"node_type\":\"Fact\","
"\"label\":\"noemb-low\",\"tier\":\"Semantic\",\"salience\":0.1,\"importance\":0.2,"
"\"confidence\":1.0,\"created_at\":3000}"
"],\"edges\":[]}", e1, e2);
fclose(f);
if (!engram_load(S(snap))){ fprintf(stderr, "load failed\n"); return 2; }
long long nc = (long long)(int64_t)engram_node_count();
printf("node_count=%lld\n", nc);
/* full page */
el_val_t all = engram_scan_nodes_emb_json((el_val_t)256, (el_val_t)0);
char p[1024];
snprintf(p, sizeof p, "%s/emb_all.json", dir);
f = fopen(p, "w"); fputs(EL_CSTR(all), f); fclose(f);
/* pagination: one node at offset 0 and one at offset 1 */
el_val_t pg0 = engram_scan_nodes_emb_json((el_val_t)1, (el_val_t)0);
el_val_t pg1 = engram_scan_nodes_emb_json((el_val_t)1, (el_val_t)1);
snprintf(p, sizeof p, "%s/emb_pg0.json", dir); f = fopen(p, "w"); fputs(EL_CSTR(pg0), f); fclose(f);
snprintf(p, sizeof p, "%s/emb_pg1.json", dir); f = fopen(p, "w"); fputs(EL_CSTR(pg1), f); fclose(f);
/* existing path — must be unchanged / carry NO emb */
el_val_t plain = engram_scan_nodes_json((el_val_t)256, (el_val_t)0);
snprintf(p, sizeof p, "%s/plain.json", dir); f = fopen(p, "w"); fputs(EL_CSTR(plain), f); fclose(f);
printf("wrote dumps to %s\n", dir);
return 0;
}