core/generate/realize/recall.py
Shay ef3181aa01 feat(realize): R1 — structural identity & recall (relation-space + span-free idempotency)
R0 keyed a realized fact by its subject's field versor, which is NOT injective:
two facts about one subject embed to byte-identical versors and collide at inf on
metric recall (proven). R1 adds the missing structural key.

- RealizedRecord/metadata carry ordered `relation_arguments` (the relation-space
  key R0's sorted `entity_names` discards) and a span-free `structure_key`.
- `recall_realized(ctx, subject=/predicate=/content_hash=/structure_key=/
  structure_kind=/entity=)` retrieves realized facts by EXACT structural metadata
  (no metric / ANN), via a new read-only `VaultStore.iter_metadata()` accessor.
- Idempotency now dedups on the span-free `structure_key`, so the same proposition
  told from a different source/offset collapses (R0's span-inclusive content_hash
  could not). Guarded by an ambiguous-entity-name refusal — a wrong=0 defense,
  since `Entity.name` is non-unique in the model (only `entity_id` is enforced).
- `content_hash` retained for provenance + replay_hash; `vault_index` pinned to the
  live deque position.

Design adversarially verified (docs/analysis/REALIZE-R1-DETERMINE-scope-2026-06-06.md);
the false "established pattern" private-access comment is removed in favor of the
public accessor. wrong=0 + versor_condition<1e-6 + exact CGA recall preserved;
vault/store.py adds only a read-only accessor (no normalization). Green: 23 realize
+ 110 invariant/vault + 90 smoke; ruff check clean.
2026-06-06 05:52:49 -07:00

62 lines
2.7 KiB
Python

"""REALIZE R1 — structural recall of realized knowledge.
The field versor is NOT an injective key: two facts about the same subject embed
to the byte-identical versor, so the metric reader (``vault.recall``) returns
both at ``inf`` and cannot tell them apart. ``recall_realized`` retrieves
realized facts by their EXACT structural metadata — subject (the first relation
argument), predicate, content_hash, span-free structure_key, or structure_kind —
an exact, deterministic equality scan (no cosine / HNSW / ANN). It reads through
the public ``VaultStore.iter_metadata`` accessor and never mutates the vault.
"""
from __future__ import annotations
from session.context import SessionContext
from .realize import RealizedRecord, _record_from_metadata
def recall_realized(
ctx: SessionContext,
*,
subject: str | None = None,
predicate: str | None = None,
content_hash: str | None = None,
structure_key: str | None = None,
structure_kind: str | None = None,
entity: str | None = None,
) -> tuple[RealizedRecord, ...]:
"""Return realized records matching ALL provided structural filters.
Filters are conjunctive; ``None`` filters are ignored, so ``recall_realized(ctx)``
returns every realized record (in live deque order).
- ``subject`` — the relation's first argument (the subject) equals this.
- ``predicate`` — the relation predicate equals this.
- ``content_hash`` — exact span-inclusive identity (disambiguates a single fact).
- ``structure_key`` — exact span-FREE identity (same proposition, any source).
- ``structure_kind``— the substrate (``meaning_graph`` / ``binding_graph``).
- ``entity`` — this name appears among the fact's entities (any role).
The scan is exact and order-preserving; it makes no metric call and does not
mutate the vault.
"""
out: list[RealizedRecord] = []
for idx, meta in ctx.vault.iter_metadata():
if meta.get("kind") != "realized":
continue
args = meta.get("relation_arguments", [])
if subject is not None and (not args or args[0] != subject):
continue
if predicate is not None and meta.get("relation_predicate") != predicate:
continue
if content_hash is not None and meta.get("content_hash") != content_hash:
continue
if structure_key is not None and meta.get("structure_key") != structure_key:
continue
if structure_kind is not None and meta.get("structure_kind") != structure_kind:
continue
if entity is not None and entity not in meta.get("entity_names", []):
continue
out.append(_record_from_metadata(meta, idx))
return tuple(out)