Files
neuron/imprint.el
T
will.anderson e6da638536
Neuron Soul CI / build (pull_request) Has been cancelled
fix(reliability): state-management — document and partially fix concurrent state races
Issues addressed:
- #2: Document session_index non-atomic RMW (engram node safe under new mutex)
- #3: Document conv_history global race in handle_chat (session path unaffected)
- #4: Scope session_continuity state key per session_id in layered_cycle
- #5: Document active_imprint_id global race with fix path
- #6: Fix next_bridge_id to use uuid_v4() for collision-free IDs
- #7: Document session_hist_save delete-then-insert race
- #8: Document /api/graph/edges engram_save race (fixed in el_runtime.c)
- #10: Document agentic_conv_history global race in awareness loop

Issues #1 (engram_global mutex) and #8 (atomic engram_save write-to-temp+rename)
are fully fixed in el_runtime.c (committed to foundation/el repo separately).
Issue #9 skipped — already fixed in PR #31.
2026-06-22 12:12:58 -05:00

86 lines
3.8 KiB
EmacsLisp

// Layer 3 Imprint
// Domain knowledge, voice, and tools bounded by the L2 stewardship surface.
// Imprints cannot write BellEvent or StewardshipEvent nodes.
// Lower layers (L0 core, L1 safety, L2 stewardship) are structurally inaccessible from here.
// imprint_current returns the active imprint ID from state.
// Falls back to "base" (bare Neuron, no suit) when nothing is loaded.
//
// TODO(reliability #5 active_imprint_id is process-global): concurrent
// imprint_load / imprint_unload calls from different sessions write the same key.
// Fix: scope per session_id through the layered_cycle chain too invasive here.
fn imprint_current() -> String {
let id: String = state_get("active_imprint_id")
return if str_eq(id, "") { "base" } else { id }
}
// imprint_load activate an imprint by ID.
// Searches engram for a node labelled "imprint:<id>".
// Verifies the returned node's label matches before accepting the match.
// On success: sets active_imprint_id state and returns {"ok":true,"id":"<id>"}.
// On miss: returns {"ok":false,"error":"imprint not found: <id>"}.
fn imprint_load(imprint_id: String) -> String {
let label: String = "imprint:" + imprint_id
let results: String = engram_search_json(label, 1)
if str_eq(results, "") {
return "{\"ok\":false,\"error\":\"imprint not found: " + imprint_id + "\"}"
}
if str_eq(results, "[]") {
return "{\"ok\":false,\"error\":\"imprint not found: " + imprint_id + "\"}"
}
let found_label: String = json_get(results, "label")
if str_eq(found_label, label) {
state_set("active_imprint_id", imprint_id)
return "{\"ok\":true,\"id\":\"" + imprint_id + "\"}"
}
return "{\"ok\":false,\"error\":\"imprint not found: " + imprint_id + "\"}"
}
// imprint_respond route steward-aligned input through the active imprint's voice/domain context.
// If imprint_id is "base" or empty: pass input through unchanged (base Neuron, no suit).
// If the imprint is confirmed loaded in state: annotate the input with imprint context.
// If the state does not match: graceful fallback to base never hard-fail at L3.
fn imprint_respond(input: String, imprint_id: String) -> String {
if str_eq(imprint_id, "base") {
return input
}
if str_eq(imprint_id, "") {
return input
}
// Cross-check imprint_id against loaded state rather than re-querying engram
let current: String = imprint_current()
if str_eq(current, imprint_id) {
return input + " [imprint:" + imprint_id + " active]"
}
// Graceful fallback: imprint not loaded in state, return input unchanged
return input
}
// imprint_surface_knowledge domain-scoped knowledge search for the active imprint.
// Imprints can search knowledge but only domain-relevant nodes.
// For "base" imprint: full query, no scope restriction.
// For named imprints: query is narrowed to "domain:<imprint_id>" scope.
fn imprint_surface_knowledge(query: String, imprint_id: String) -> String {
if str_eq(imprint_id, "base") {
return engram_search_json(query, 10)
}
if str_eq(imprint_id, "") {
return engram_search_json(query, 10)
}
let scoped_query: String = query + " domain:" + imprint_id
return engram_search_json(scoped_query, 10)
}
// imprint_surface_memory_read imprints can read memories from engram.
// Read-only: no write surface is exposed here.
// Imprints CANNOT write BellEvent, StewardshipEvent, or InternalStateEvent nodes
// those write paths are sealed in L1 and L2, which are structurally inaccessible.
fn imprint_surface_memory_read(query: String) -> String {
return engram_search_json(query, 10)
}
// imprint_unload deactivate the current imprint, returning to base Neuron.
fn imprint_unload() -> Void {
state_set("active_imprint_id", "")
}