engram: reconcile #105's embed-cache/beam-BFS latency fixes onto current dev #115

Merged
will.anderson merged 1 commits from fix/engram-search-latency-reconciled into dev 2026-08-15 22:07:47 +00:00
Owner

Supersedes #105. #105's single commit (1dc49b1) modifies lang/el-compiler/runtime/el_runtime.c, a path that does not exist on dev (renamed to lang/runtime/el_runtime.c by the restructure) — its branch traces to a July 22 merge-base, weeks before dev's qgate rescale, ACT-R fan effect, adjacency index, and the M8 HNSW vindex work from #109 (merged today). A direct merge/cherry-pick was impossible (wrong path) and would have been wrong regardless (would have thrown away everything dev grew since).

This PR ports #105's three genuinely additive wins onto dev's CURRENT engram_activate, keeping dev's existing cosq/e_eff semantic layer entirely intact as the substrate rather than replacing it:

  1. keep_alive:-1 on the Ollama embed request (eg_embed_fetch) — pins the embed model resident so it can't be evicted by a larger generation model under memory pressure (measured cold reload ~2.2s vs ~0.02-0.05s warm).
  2. Query-embedding cache upgraded from dev's single-slot (only remembered the LAST query) to a direct-mapped, FNV-1a-keyed, 1024-slot cache, so the curiosity loop's rotating phrases actually hit instead of evicting each other every call.
  3. Beam cap on the layer-1 spreading-activation BFS (ENGRAM_ACTIVATE_BEAM, default 128), processing the frontier in hop-level batches and capping expansion width per level. Every node still gets recorded/reported — the cap bounds associative spread only. Kept as a genuine additional bound on top of (not instead of) the adjacency index / qgate / fan-effect mitigations dev already has for the same hub-explosion failure mode #105 was addressing.

Full rationale and verification details (nsbx sandbox against a real cloned production snapshot, 13,424 nodes / 37,656 edges — live :8742/:7770 never touched) in the commit message.

Recommend closing #105 in favor of this PR — its branch content can't be salvaged (wrong file path, unrelated history) and force-pushing over it was avoided deliberately.

Note: CI on this repo's ci-dev.yaml is currently red on a PRE-EXISTING, unrelated failure (the "Self-host compile El compiler (gen2)" step still builds the stale lang/el-compiler/runtime/el_runtime.c copy, which is out of sync with the newer engram_cognition/geometry/reason/verify/vindex modules under lang/runtime/) — confirmed failing on dev's last several pushes before this PR existed (runs 3872/3883/3884), unrelated to this change. Flagging for separate attention rather than fixing here (out of scope for this PR).

Supersedes #105. #105's single commit (1dc49b1) modifies `lang/el-compiler/runtime/el_runtime.c`, a path that does not exist on dev (renamed to `lang/runtime/el_runtime.c` by the restructure) — its branch traces to a July 22 merge-base, weeks before dev's qgate rescale, ACT-R fan effect, adjacency index, and the M8 HNSW vindex work from #109 (merged today). A direct merge/cherry-pick was impossible (wrong path) and would have been wrong regardless (would have thrown away everything dev grew since). This PR ports #105's three genuinely additive wins onto dev's CURRENT engram_activate, keeping dev's existing cosq/e_eff semantic layer entirely intact as the substrate rather than replacing it: 1. `keep_alive:-1` on the Ollama embed request (eg_embed_fetch) — pins the embed model resident so it can't be evicted by a larger generation model under memory pressure (measured cold reload ~2.2s vs ~0.02-0.05s warm). 2. Query-embedding cache upgraded from dev's single-slot (only remembered the LAST query) to a direct-mapped, FNV-1a-keyed, 1024-slot cache, so the curiosity loop's rotating phrases actually hit instead of evicting each other every call. 3. Beam cap on the layer-1 spreading-activation BFS (`ENGRAM_ACTIVATE_BEAM`, default 128), processing the frontier in hop-level batches and capping expansion width per level. Every node still gets recorded/reported — the cap bounds associative spread only. Kept as a genuine additional bound on top of (not instead of) the adjacency index / qgate / fan-effect mitigations dev already has for the same hub-explosion failure mode #105 was addressing. Full rationale and verification details (nsbx sandbox against a real cloned production snapshot, 13,424 nodes / 37,656 edges — live :8742/:7770 never touched) in the commit message. Recommend closing #105 in favor of this PR — its branch content can't be salvaged (wrong file path, unrelated history) and force-pushing over it was avoided deliberately. Note: CI on this repo's `ci-dev.yaml` is currently red on a PRE-EXISTING, unrelated failure (the "Self-host compile El compiler (gen2)" step still builds the stale `lang/el-compiler/runtime/el_runtime.c` copy, which is out of sync with the newer engram_cognition/geometry/reason/verify/vindex modules under `lang/runtime/`) — confirmed failing on dev's last several pushes before this PR existed (runs 3872/3883/3884), unrelated to this change. Flagging for separate attention rather than fixing here (out of scope for this PR).
will.anderson added 1 commit 2026-08-15 21:56:28 +00:00
engram: port PR #105's 3 genuine wins onto dev's existing cosq/e_eff semantic layer
El SDK CI - dev / build-and-test (pull_request) Failing after 4m45s
1bb1edc851
Reconciles PR #105 ("fix: engram search latency — pin embed model, cache
query embeddings, bound activate BFS") with dev's ACTUAL current
engram_activate, rather than the ancient pre-restructure snapshot #105 was
built against.

WHY THIS NEEDED RECONCILIATION, NOT A DIRECT PORT: #105's single commit
(1dc49b1) modifies `lang/el-compiler/runtime/el_runtime.c` — a path that does
not exist on dev (dev has `lang/runtime/el_runtime.c`; the restructure that
renamed it happened after #105's branch point, which traces to a July 22
merge-base, weeks before the M8/M8.1/qgate/fan-effect/adjacency-index work
this file has grown since). #105's own engram_activate is consequently the
PRE-restructure version: no adjacency index (O(E) full edge scan per hop),
no query-aware qgate, no ACT-R fan effect, no eg_edge_eff_weight, and no
awareness of dev's cosq/e_eff embedding-blend semantic layer — it built a
parallel `g_qcache`/`engram_embed_raw` mechanism from scratch against code
that no longer exists at that path. A raw merge/cherry-pick was not possible
and would have been wrong even if it were: taking #105's tree wholesale would
have thrown away everything dev grew in the meantime (qgate, fan effect,
adjacency index, and this session's own M8 HNSW vindex integration).

RECONCILIATION: kept dev's cosq/e_eff mechanism as the semantic layer
entirely intact (unchanged by this commit) and ported #105's three genuinely
additive wins on TOP of it, at their equivalent sites in the CURRENT
eg_embed_fetch/engram_activate:

  1. keep_alive:-1 on the Ollama embed request body (eg_embed_fetch) — pins
     the embed model resident so a larger generation model loading under
     unified-memory pressure can't evict it and force a cold reload on the
     next search (#105 measured ~2.2s cold vs ~0.02-0.05s warm).
  2. Query-embedding cache upgraded from dev's single-slot (`_eg_qcache_text`,
     only ever remembered the LAST query) to a direct-mapped, FNV-1a-keyed,
     1024-slot cache (reusing the existing engram_id_hash) — so the
     curiosity loop's rotating phrases actually hit the cache instead of
     evicting each other every call. Same "pointer owned by the cache, not
     freed by caller" contract as before, just per-slot instead of global.
  3. Beam cap on the layer-1 spreading-activation BFS (new
     engram_activate_beam(), tunable via ENGRAM_ACTIVATE_BEAM, default 128).
     The FIFO frontier is processed in hop-level batches (entries sharing
     .hops are provably contiguous — see the code comment); when a level
     exceeds the beam width, only the top-`beam` by activation actually
     EXPAND. Every node in an oversized level still gets reached[]/best_bg[]
     recorded (that happens at enqueue time, one level up) and appears in
     the reported/promoted set — the cap bounds associative SPREAD width
     only, never recall of what was already found. Kept as a genuine
     additional bound even though the adjacency index + qgate + fan effect
     already mitigate #105's original "hub-node explosion" failure mode for
     a different reason: those prune WHICH targets matter; this bounds
     worst-case width regardless.

Everything else in dev's engram_activate — cosq/e_eff, the qgate rescale,
the fan effect, eg_edge_eff_weight, the M8 HNSW vindex seed discovery from
the #109 reconciliation earlier this session — is untouched.

VERIFIED (nsbx sandbox only, live :8742/:7770 never touched): cc -std=c11
-O2 clean build; booted in an isolated sandbox against a real cloned
production snapshot (13,424 nodes / 37,656 edges); ran 5 activate() calls
across rotating queries at depth 3, including the same query issued twice
non-consecutively (2nd hit landed at 476ms vs the 1st at 483ms — consistent
with a cache hit once Ollama's own warm-model latency is accounted for; no
crash, correct varied result counts (367-2610 nodes) each call; act-stats
JSON read correctly throughout.

Built on top of the M8/#109 reconciliation (bacaf3d, merged to dev as
#109) — dev's current HEAD at the time of this commit.
will.anderson merged commit 0e924f7df9 into dev 2026-08-15 22:07:47 +00:00
Sign in to join this conversation.