Structured Project Memory (SPM) is Lexon’s native “second brain.” It
ingests raw artifacts (code, guides, decisions, logs), normalizes them
into canonical MemoryObjects, stores them in pluggable
tree/index backends, and exposes retrieval primitives
(recall_context, recall_kind,
before_action use_context) so every Lexon program can
access curated context before or alongside traditional RAG. The goal is
to provide durable knowledge per project with deterministic behavior,
governance, and zero extra frameworks.
Raw assets ──> Semantic layer ──> MemoryObject ──> Backend (basic/patricia/raptor/hybrid)
^ | | |
| | | └─> Policy evaluation, summaries
| └─> remember_raw / remember_structured ↓
Lexon runtime <────────────────────── recall_context / recall_kind / before_action use_context
path_hint, kind,
project, tags, metadata).MemoryObject.remember_raw (LLM-assisted) or
remember_structured (pre-built JSON).MemorySpaceFile under
.lexon/structured_memory/<space>.json (see
space_path helper). Each file contains the entire space,
sorted deterministically by updated_at.order_for_topic)
and kinds (order_for_kind).basic, patricia,
raptor, hybrid (GraphRAG/MemTree).memory_space.create/list, remember_*,
recall_*, pin_memory,
set_memory_policy.before_action use_context hook injects bundles into MCP
agents/flows.{
"id": "mem_* (optional override)",
"path": "project/module/topic",
"kind": "guide|config|decision|log|custom",
"raw": "... original text ...",
"summary_micro": "1-2 sentences",
"summary_short": "short paragraph / bullets",
"summary_long": "long-form abstract",
"tags": ["lowercase", "slugs"],
"metadata": {"project": "...", "space": "...", "importance": "high"},
"relevance": "high|medium|low",
"pinned": true|false,
"created_at": "RFC3339",
"updated_at": "RFC3339"
}MemorySpaceFile aggregates objects,
metadata, policies,
updated_at.{
"id": "mem_runtime_decision_001",
"path": "lexon/runtime/release_checklist",
"kind": "decision",
"raw": "Ship RC once structured memory recalls <200ms and MCP supervisor is green.",
"summary_micro": "RC go/no-go checklist",
"summary_short": "Decisions and blockers for promoting Lexon v1.0.0-rc.1 to GA.",
"summary_long": "Context, telemetry targets, and required samples-smoke pass/fail report for the release.",
"tags": ["runtime", "release", "checklist"],
"metadata": {"project": "lexon", "space": "runtime", "source": "docs/runtime_decision.md", "hash": "sha256:6be0...", "pii_flags": []},
"relevance": "high",
"pinned": true,
"chunks": [
{"chunk_id": "runtime_decision_001#0", "text": "Context..."},
{"chunk_id": "runtime_decision_001#1", "text": "Checklist..."}
],
"created_at": "2025-12-01T11:32:00Z",
"updated_at": "2025-12-01T12:04:00Z"
}MemorySpaceFile aggregates objects,
metadata, policies,
updated_at.ttl_days — Max age before objects are
archived or pruned. Enforced via memory_space.gc(); default
null (never expires).pii_redaction — Regex/detector list
(emails, keys, etc.) removed from raw; default
[].export_path — Path used by
memory_space.export to emit JSON bundles per space. Default
.lexon/exports/<space>.json.allow_delete — Boolean guard required
before memory_space.delete(space) runs; default
false.Retention enforcement flow: 1. remember_* stamps
created_at/updated_at using deterministic
clock when freeze_clock is set. 2.
set_memory_policy with ttl_days +
allow_delete=true enables
memory_space.gc(space) to purge expired objects (and write
audit logs). 3. Redaction occurs before serialization: detectors strip
emails/API keys from raw and stash hashes inside
metadata.pii_flags. 4. Exports are JSON bundles signed with
space + hash so teams can hand them to
audit/compliance or restore elsewhere.
Deletion/export commands are CLI-level operations so teams can satisfy GDPR/CCPA requests without editing Lexon files manually.
memory_space.create(name, metadata_json?) — Initialize
or reset a space. Pass {"reset": true} for deterministic
wipes.memory_space.list() — Enumerate existing spaces with
summaries.remember_structured(space, payload_json, options?) —
Ingest a pre-built MemoryObject; fills missing summaries
from raw.remember_raw(space, kind, raw_text, options?) — Use the
semantic LLM (OpenAI/Anthropic/Google/Ollama/HF/custom) to infer path,
summaries, tags, relevance. Options: model,
temperature, max_tokens,
path_hint, project, tags,
metadata, auto_pin. Fallback:
options.model → LEXON_MEMORY_SEMANTIC_MODEL →
config.llm_model.pin_memory(space, id_or_path) /
unpin_memory(...) — Toggle the pinned
flag.set_memory_policy(space, policy_json) — Persist
per-project auto-pin/retention/visibility rules.recall_context(space, topic, options_json?) — Return
bundles (global_summary, sections[], optional
raw[], generated_at). Options:
limit, raw_limit, include_raw,
include_metadata, prefer_kinds,
prefer_tags, require_high_relevance,
freeze_clock.recall_kind(space, kind, options_json?) — Ordered list
filtered by kind.before_action use_context project=... topic=... — MCP
hook to inject recall bundles before agent steps.Determinism hooks: - freeze_clock overrides timestamps
in bundles for goldens. - {"reset": true} ensures
predictable states in tests.
prefer_kinds,
prefer_tags.basic.rs scoring function returning
float; orders descending.patricia_prefix_depth).basic heuristics plus prefix depth.updated_at timestamp) + pin bonus.cluster_overlap adds points for matching tags or
summary tokens.basic_score for baseline.StructuredMemoryService::new reads
LEXON_MEMORY_BACKEND env var (default basic)
and instantiates the backend trait via
build_backend(name).basic.{"backend": "patricia"}).pin_memory / unpin_memory delegate to
toggle_pin which accepts either id or
path.updated_at so
recalls reflect the latest ordering.ExecutorError::RuntimeError("Memory '...' not found in space '...'").id +
path; best for <5k objects.metadata.hash for dedupe;
good for mixed specs + decision logs.All backends share the same persistence primitive
(MemorySpaceFile). Switching only changes ordering/scoring;
the serialized data stays identical.
recall_context returns:
{
"space": "lexon_demo",
"topic": "runtime",
"generated_at": "2025-01-01T00:00:00Z",
"global_summary": "Context bundle for 'runtime' (N items): ...",
"sections": [
{
"id": "...",
"path": "...",
"kind": "...",
"summary_micro": "...",
"summary_short": "...",
"summary_long": "...",
"relevance": "...",
"pinned": true,
"tags": [...],
"metadata": {...},
"updated_at": "..."
}
],
"raw": [
{"path": "...", "raw": "...", "kind": "..."}
],
"limit": 2
}Ordering rules: - Pinned + high relevance first. - Respect
limit, raw_limit. -
require_high_relevance filters out lower scores. -
prefer_kinds and prefer_tags boost relevant
memories.
watcher (git diff / filesystem) --> enqueue(raw_asset)
-> normalizer (assign path/kind/tags)
-> remember_raw / remember_structured
-> dedupe by metadata.hash
-> persist to MemorySpaceFile (JSON)
-> backend caches ordering view
recall_context/topic --> backend.order_for_topic --> bundle --> before_action hook --> agents/prompts
Pseudo-API for deterministic ingest:
let files = filesystem.watch("docs/runtime/*.md");
for file in files {
let raw = read_file(file.path);
remember_raw("runtime", "guide", raw, strings.json({
path_hint: file.path,
project: "lexon",
tags: ["runtime", file.name]
}));
}
Chunk metadata (chunks[]) is optional, but when present
it lets downstream RAG pipelines correlate structured memory with vector
search hits.
remember_* /
recall_* share the same budget manager as
ask_*. Policies can cap USD/token spend per provider and
per memory space.LEXON_OTEL=1
emits lexon.memory.remember_raw,
lexon.memory.recall_context, lexon.memory.gc,
and backend-specific spans with attributes (space,
topic, backend, items_returned,
budget_usd).lexon_memory_objects_total,
lexon_memory_gc_runs_total,
lexon_memory_pin_toggle_total, plus latency
histograms.samples/memory/structured_semantic.lx
+ golden/memory/structured_semantic.txt
rely on {"reset": true} + freeze_clock. They
fail CI if bundles drift..lexon/structured_memory/<space>.json and
.lexon/exports/*.json live next to other runtime state so
workspaces can be version-controlled or shipped as artifacts.ExecutorError::RuntimeError("Invalid space ...").basic.ExecutorError::RuntimeError.toggle_pin/pin_memory on unknown IDs/paths
returns explicit "Memory '...' not found" errors.remember_raw surfaces adapter errors (LLM failures)
just like ask.Goal: index today’s repo changes and answer “What changed in telemetry?”
Reset + ingest recent diffs:
memory_space.create("lexon_repo", """{"reset": true}""");
git diff --name-only HEAD~1 | grep '.rs$' | while read file; do \
lexc remember_raw --space lexon_repo --kind code --path "$file" "$file"; \
doneRecall telemetry:
let bundle = recall_context("lexon_repo", "telemetry", """{"limit":3,"include_raw":true}""");
print(bundle.global_summary);Feed bundle into agents/RAG:
before_action use_context project="lexon_repo", topic="telemetry";
let analyst = agent_create("lexon_repo_analyst", """{"model":"openai:gpt-4o-mini"}""");
let report = agent_run(analyst, "Describe telemetry changes today", """{"deadline_ms":15000}""");
print(report);Export or prune:
lexc memory export lexon_repo --out exports/lexon_repo.json
lexc memory gc lexon_repo --ttl-days 30The flow exercises ingestion, ordering, recall, MCP hooks, and governance knobs end-to-end.
recall_context /
recall_kind.lexc memory browse),
query/filter UI.See ROADMAP.md for
cross-cutting roadmap items (DX, providers, IR optimizations,
networking/stdlib, sockets, CI hardening, etc.).
Last updated: 2025-12-03
Files referenced:
lexc/src/executor/structured_memory.rs,
lexc/src/executor/structured_memory/backends/*.rs,
samples/memory/structured_semantic.lx,
golden/memory/structured_semantic.txt.