fix(api): bound inspect_graph with relevance-ranked projection; regen soul.c
High-fanout identity anchors (voice, writing-imprint, self-root) have ~670KB
neighborhoods. inspect_graph returned the full traversal, which overflowed the
MCP client's context and socket-closed the wrapper mid self-load -- the soul
could not traverse its own identity graph.
handle_api_inspect_graph gains an opt-in `compact` projection (compact=1|true):
the neighborhood is relevance-ranked, the top K (default 12) keep a UTF-8-safe
content snippet (default snip=600), and the remainder collapse to lightweight
{id,label,node_type,tier,edge,pointer:true} stubs. This bounds the voice node
from 669,799B -> 25,353B (HTTP 200, valid JSON) and the wrapper's soul-load no
longer socket-closes. New helpers: api_compact_neighbors, api_neigh_full,
api_neigh_pointer, api_neigh_rank, api_neigh_better, api_float_or.
The flag is gated: ABSENT it, the response is byte-identical to the old plain
traversal, so the studio app (which never sends it) is unaffected. The MCP
wrapper (mcp-wrapper/src/main.el) appends &compact=1 on its inspectGraph and
fetch-by-id paths.
dist/soul.c is REGENERATED so CI ships the fix: CI compiles the committed
single-TU dist/soul.c directly (running elb/elc on the Linux runner OOM-kills
it), so an .el-only change would build the OLD behavior. Regenerated and verified
on macOS -- compiles with the CI cc line (0 errors) and, on a throwaway soul over
a copy of the live snapshot, serves compact ~25KB / non-compact ~670KB. The regen
also syncs the amalgamation to this branch's .el sources, which had drifted
several self-review commits ahead of the previously-committed soul.c.
Docs: docs/architecture/00-05 added; 01/02/05 corrected so the relevance-ranked
inspect_graph projection reads as committed source, not an in-flight concern.
This commit is contained in:
+131
@@ -188,6 +188,125 @@ fn api_compact_activated(raw: String, max_items: Int, snip: Int) -> String {
|
||||
return out + "]"
|
||||
}
|
||||
|
||||
// api_float_or — parse a numeric JSON field of `obj` as Float, or `dflt` when
|
||||
// the field is absent. Backs neighbor relevance scoring.
|
||||
fn api_float_or(obj: String, key: String, dflt: Float) -> Float {
|
||||
let v: String = json_get_raw(obj, key)
|
||||
if str_eq(v, "") { return dflt }
|
||||
return str_to_float(v)
|
||||
}
|
||||
|
||||
// api_neigh_better — strict relevance ordering of two neighbor elements
|
||||
// {node,edge,hops}. Lexicographic and comparison-ONLY (no arithmetic): El's `+`
|
||||
// operator is overloaded to string concatenation, so float scoring like
|
||||
// weight*salience mis-compiles; ordering by `>`/`<` (always numeric on the
|
||||
// int64 el_val_t, correct for the non-negative fields here) is safe. Keys, in
|
||||
// order: fewer hops (closer), stronger edge weight, higher node salience, higher
|
||||
// node importance. Returns true iff `a` ranks strictly ahead of `b`.
|
||||
fn api_neigh_better(a: String, b: String) -> Bool {
|
||||
let na: String = json_get_raw(a, "node")
|
||||
let nb: String = json_get_raw(b, "node")
|
||||
let ea: String = json_get_raw(a, "edge")
|
||||
let eb: String = json_get_raw(b, "edge")
|
||||
let ha: Float = api_float_or(a, "hops", 1.0)
|
||||
let hb: Float = api_float_or(b, "hops", 1.0)
|
||||
if ha < hb { return true }
|
||||
if hb < ha { return false }
|
||||
let wa: Float = api_float_or(ea, "weight", 0.0)
|
||||
let wb: Float = api_float_or(eb, "weight", 0.0)
|
||||
if wa > wb { return true }
|
||||
if wb > wa { return false }
|
||||
let sa: Float = api_float_or(na, "salience", 0.0)
|
||||
let sb: Float = api_float_or(nb, "salience", 0.0)
|
||||
if sa > sb { return true }
|
||||
if sb > sa { return false }
|
||||
let ia: Float = api_float_or(na, "importance", 0.0)
|
||||
let ib: Float = api_float_or(nb, "importance", 0.0)
|
||||
if ia > ib { return true }
|
||||
return false
|
||||
}
|
||||
|
||||
// api_neigh_rank — count of elements that outrank element `i` under the
|
||||
// api_neigh_better ordering, with array index as the final tiebreak. Element i
|
||||
// belongs to the content tier iff rank < k. O(n) per element (n bounded ~90
|
||||
// neighbors), so O(n^2) overall — acceptable for a bounded neighborhood.
|
||||
fn api_neigh_rank(raw: String, n: Int, i: Int) -> Int {
|
||||
let el_i: String = json_array_get(raw, i)
|
||||
let better: Int = 0
|
||||
let j: Int = 0
|
||||
while j < n {
|
||||
let el_j: String = json_array_get(raw, j)
|
||||
let j_better: Bool = api_neigh_better(el_j, el_i)
|
||||
let i_better: Bool = api_neigh_better(el_i, el_j)
|
||||
let eq: Bool = !j_better && !i_better
|
||||
let wins: Bool = j_better || (eq && j < i)
|
||||
let better = if wins { better + 1 } else { better }
|
||||
let j = j + 1
|
||||
}
|
||||
return better
|
||||
}
|
||||
|
||||
// api_neigh_full — top-tier neighbor: the node compacted to a bounded content
|
||||
// snippet, the full edge raw preserved (guard empty -> null), hops, pointer:false.
|
||||
fn api_neigh_full(node: String, edge: String, el: String, snip: Int) -> String {
|
||||
let e: String = if str_eq(edge, "") { "null" } else { edge }
|
||||
return "{\"node\":" + api_compact_node(node, snip)
|
||||
+ ",\"edge\":" + e
|
||||
+ ",\"hops\":" + api_num_or_zero(el, "hops")
|
||||
+ ",\"pointer\":false}"
|
||||
}
|
||||
|
||||
// api_neigh_pointer — tail neighbor: a lightweight, addressable POINTER with NO
|
||||
// content. Just enough identity (id/label/node_type/tier) to dereference on
|
||||
// demand, plus edge relation+weight and hops. This is what keeps the payload
|
||||
// bounded on high-fanout nodes.
|
||||
fn api_neigh_pointer(node: String, edge: String, el: String) -> String {
|
||||
let id: String = json_get(node, "id")
|
||||
let label: String = json_get(node, "label")
|
||||
let ntype: String = json_get(node, "node_type")
|
||||
let tier: String = json_get(node, "tier")
|
||||
let relation: String = json_get(edge, "relation")
|
||||
return "{\"node\":{\"id\":\"" + api_json_escape(id) + "\""
|
||||
+ ",\"label\":\"" + api_json_escape(label) + "\""
|
||||
+ ",\"node_type\":\"" + api_json_escape(ntype) + "\""
|
||||
+ ",\"tier\":\"" + api_json_escape(tier) + "\"}"
|
||||
+ ",\"edge\":{\"relation\":\"" + api_json_escape(relation) + "\""
|
||||
+ ",\"weight\":" + api_num_or_zero(edge, "weight") + "}"
|
||||
+ ",\"hops\":" + api_num_or_zero(el, "hops")
|
||||
+ ",\"pointer\":true}"
|
||||
}
|
||||
|
||||
// api_compact_neighbors — bounded projection of an engram neighbor array
|
||||
// [{node,edge,hops},...]. Relevance-ranks neighbors (via api_neigh_rank /
|
||||
// api_neigh_better): the top `k_content` are emitted WITH a content snippet; every other neighbor is
|
||||
// emitted as a lightweight POINTER (no content) the caller dereferences on
|
||||
// demand. Every element is emitted (as full or pointer), so total fan-out COUNT
|
||||
// stays visible. Mirrors api_compact_activated but adds the ranking + the
|
||||
// content/pointer split, keeping high-fanout identity nodes (voice,
|
||||
// writing-imprint) well under the transport socket-close threshold. Returns a
|
||||
// valid JSON array.
|
||||
fn api_compact_neighbors(raw: String, k_content: Int, snip: Int) -> String {
|
||||
if !api_nonempty(raw) { return "[]" }
|
||||
let n: Int = json_array_len(raw)
|
||||
let out: String = "["
|
||||
let i: Int = 0
|
||||
while i < n {
|
||||
let el: String = json_array_get(raw, i)
|
||||
let node: String = json_get_raw(el, "node")
|
||||
let edge: String = json_get_raw(el, "edge")
|
||||
let rank: Int = api_neigh_rank(raw, n, i)
|
||||
let sep: String = if i == 0 { "" } else { "," }
|
||||
let elem: String = if rank < k_content {
|
||||
api_neigh_full(node, edge, el, snip)
|
||||
} else {
|
||||
api_neigh_pointer(node, edge, el)
|
||||
}
|
||||
let out = out + sep + elem
|
||||
let i = i + 1
|
||||
}
|
||||
return out + "]"
|
||||
}
|
||||
|
||||
// api_persisted — read-back-after-write guard against hallucinated saves.
|
||||
// After a write builtin returns an id, confirm the node is actually queryable
|
||||
// via engram_get_node_json(id) (returns "" or "null" when missing). Returns
|
||||
@@ -678,6 +797,18 @@ fn handle_api_inspect_graph(method: String, path: String, body: String) -> Strin
|
||||
return api_err("entity_id or name required. Known names: self, neuron, values, values_hub")
|
||||
}
|
||||
let results: String = engram_neighbors_json(resolved, depth, "both")
|
||||
// Optional bounded projection. `compact=1` relevance-ranks the neighborhood
|
||||
// (top-K get content snippets, the rest become lightweight pointers) so the
|
||||
// MCP transport never socket-closes on high-fanout identity anchors (voice,
|
||||
// writing-imprint). Absent the flag the studio app's calls are UNCHANGED.
|
||||
let compact: String = if str_eq(method, "GET") { api_query_param(path, "compact") } else { json_get(body, "compact") }
|
||||
if str_eq(compact, "1") || str_eq(compact, "true") {
|
||||
let snip_q: Int = api_query_int(path, "snip", 0)
|
||||
let snip: Int = if snip_q == 0 { 600 } else { snip_q }
|
||||
let k_q: Int = api_query_int(path, "k", 0)
|
||||
let k: Int = if k_q == 0 { 12 } else { k_q }
|
||||
return api_or_empty(api_compact_neighbors(results, k, snip))
|
||||
}
|
||||
return api_or_empty(results)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user