Skip to main content

build_entity_envelope

Function build_entity_envelope 

Source
pub fn build_entity_envelope(
    entity: &Entity,
    rendered_body_tokens: usize,
    full_tokens: Option<usize>,
    sections_filter: Option<&[String]>,
    schema_anchor: Option<&str>,
    outgoing_edges: &[Edge],
) -> Value
Expand description

Build the structured memstead_entity envelope. Identity fields (_hash, id, mem, type, _stub_kind) come from the parsed Entity and live at the top level. Every schema-declared frontmatter key surfaces under a nested metadata: {...} map — its single home. Read a metadata value as envelope.metadata.<key>; generic consumers iterate the map without per-type branching. The prior shape additionally hoisted level/stability/created_date/last_modified to the top level, serialising those fields twice; that hoist is gone. The read-only identity triple (mem/id/type) is excluded from the nested map — it appears only top-level — and underscore-prefixed internal keys (_hash, _tokens*, _mem_schema, _stub_*) live in dedicated top-level slots and never appear inside the nested map. sections and relationships round-trip the engine’s internal IndexMap / Vec shapes verbatim. _tokens is computed from the rendered body (filter and opt-in inserts applied) so agents can pre-size before a follow-up token_budget-bounded read. _mem_schema rides when the workspace pinned a schema for the mem.

Per-section filtering applies — when sections_filter is Some, the structured sections map carries only the requested keys (matching the markdown projection). The unfiltered-base token cost surfaces as _tokens_unfiltered_body so agents can predict the cost of dropping the filter. The name avoids implying a monotonic relationship (_tokens_unfiltered_body ≥ _tokens) that the opt-in (include_relations / include_context) path can invert: opt-in inserts contribute to _tokens but not to this baseline. Stub entities ship every key with empty sections / relationships arrays.

The structured envelope is the contract for memstead_entity: agents read _hash, sections, and relations from typed fields rather than string-scraping the markdown frontmatter.