Skip to content

feat: Native Memory System — language-level remember/recall/forget primitives #517

@gHashTag

Description

@gHashTag

Native Memory System for t27 Language

Inspiration: MemPalace — associative memory architecture embedded at the language level.

Source of Truth: experience/ + .trinity/ seals
PHI LOOP: edit spec → seal hash → gen → test → verdict → save experience → skill commit → git commit


Problem Statement

The t27 language currently has no first-class memory primitives. Agents and programs must use external state, files, or databases to persist knowledge across invocations. MemPalace demonstrates that memory should be a language-level concern — associative, typed, and φ-aligned.

Goal: Add remember, recall, forget, and reflect as native keywords/builtins in .t27/.tri specs, backed by a .trinity-sealed content-addressable store.


Research: Best Practices in Language-Native Memory

Prior Art Survey

System Approach Key Insight
MemPalace Loci-based associative memory Spatial anchoring → fast retrieval
Lisp PROGN + property lists Symbol-attached memory Memory as first-class value
Prolog facts/assert Dynamic knowledge base Memory = logic assertions
Rust thread_local! Scoped persistent state Lifetime-bounded memory
Lua __index metamethod Transparent fallback store Memory as transparent protocol
CLIPS working memory Production rule memory Pattern-matched recall

Key Design Principles

  1. Memory is typed — every memory cell carries a φ-hash signature
  2. Memory is scopedagent, session, permanent lifetimes
  3. Memory is searchable — fuzzy recall via semantic distance
  4. Memory is auditable — every write is sealed in .trinity/ SSOT
  5. Memory is forgettable — explicit forget with tombstone log

Architecture

specs/memory/
├── memory_primitives.t27     ← SSOT spec (L4: must have test/invariant)
├── memory_store.t27          ← content-addressable backend
├── memory_scope.t27          ← agent/session/permanent lifetimes
└── memory_phi.t27            ← φ-hash seal integration

.trinity/
└── memory/
    ├── SEAL_HASH             ← frozen hash of spec (L6: CEILING)
    └── experiences/
        └── YYYYMMDD_*.md     ← experience log per PHI LOOP iteration

experience/
└── memory/
    ├── 001_primitives.md
    ├── 002_store_backend.md
    └── 003_phi_alignment.md

gen/
└── memory/                   ← generated output (L2: NO-HAND-EDIT)

tests/
└── memory/
    ├── test_remember.t27
    ├── test_recall_fuzzy.t27
    └── test_forget_tombstone.t27

Decomposed Plan — PHI LOOP Iterations

PHASE 0 — Spec & Research (tri CLI: tri spec init memory)

  • P0.1 — Read MemPalace architecture docs, extract core memory model
  • P0.2 — Survey experience/ for prior memory-related work in t27
  • P0.3 — Define MemoryCell type in specs/memory/memory_primitives.t27
    • Fields: key: GoldenFloat, value: Tri, scope: MemScope, phi_hash: Hash27, timestamp: u64
  • P0.4 — Define MemScope enum: Agent | Session | Permanent | Ephemeral
  • P0.5seal hashtri seal specs/memory/memory_primitives.t27

PHASE 1 — Primitives Spec (tri CLI: tri spec write memory_primitives)

  • P1.1remember(key, value, scope) — write to memory store
    fn remember(key: Str, value: Tri, scope: MemScope) -> Result<Hash27>
    
  • P1.2recall(key)Option<MemoryCell> — exact retrieval
  • P1.3recall_like(query, threshold: GoldenFloat)Vec<MemoryCell> — fuzzy recall (φ-distance)
  • P1.4forget(key) → tombstone, not delete (audit trail preserved)
  • P1.5reflect()Vec<MemoryCell> — list all active memories in scope
  • P1.6 — Write invariant: every remember must produce phi_hash s.t. phi_hash mod phi ≈ 0 (L5: PHI-IDENTITY)
  • P1.7seal hashtri seal specs/memory/memory_primitives.t27

PHASE 2 — Backend Store (tri CLI: tri gen memory_store)

  • P2.1 — Implement content-addressable store in Rust (compiler/memory/store.rs)
    • Backend: sled embedded KV or pure file-based .trinity/memory/
    • Key: SHA3-27(phi_hash || key_bytes)
  • P2.2 — Implement scope isolation: agent_id prefix for Agent scope
  • P2.3 — Implement TTL for Session scope (evict on tri session end)
  • P2.4gen/ output: Rust FFI bindings auto-generated from spec (L2)
  • P2.5seal hash → update bootstrap/stage0/FROZEN_HASH

PHASE 3 — Compiler Integration (tri CLI: tri compile --with-memory)

  • P3.1 — Add remember/recall/forget/reflect to t27 lexer keywords
  • P3.2 — Add AST nodes: RememberExpr, RecallExpr, ForgetStmt, ReflectExpr
  • P3.3 — Type checker: validate MemScope at compile time
  • P3.4 — Code gen: lower memory ops to Rust backend calls
  • P3.5 — Error messages: E_RECALL_MISS (key not found), E_SCOPE_VIOLATION (wrong lifetime)

PHASE 4 — Tests (tri CLI: tri test memory)

  • P4.1 — Unit: test_remember_exact — store and retrieve exact value
  • P4.2 — Unit: test_recall_miss — returns None for unknown key
  • P4.3 — Unit: test_forget_tombstone — verify tombstone, value inaccessible
  • P4.4 — Unit: test_phi_hash_invariant — every cell satisfies L5
  • P4.5 — Integration: test_agent_scope_isolation — two agents cannot read each other's memory
  • P4.6 — Integration: test_session_eviction — session memories evicted after tri session end
  • P4.7 — Fuzzy: test_recall_like_phi_distance — recall returns top-k by φ-distance
  • P4.8verdict → all tests green → proceed

PHASE 5 — Experience & Seal (tri CLI: tri experience save memory)

  • P5.1 — Write experience/memory/001_primitives.md — what worked, what didn't, φ-alignment notes
  • P5.2 — Write .trinity/memory/experiences/YYYYMMDD_memory_v1.md
  • P5.3 — Update phi-loop-skills.md with memory skill entry
  • P5.4 — Update NOW.md to reflect current focus
  • P5.5skill commitgit commit -m "skill(memory): native remember/recall/forget primitives v1"

PHASE 6 — MemPalace Integration Bridge

  • P6.1 — Research MemPalace API surface: loci, palace, traversal
  • P6.2 — Define MemPalace adapter in specs/memory/mempalace_bridge.t27
  • P6.3 — Map t27 remember(key, locus: Locus) → MemPalace spatial anchor
  • P6.4 — Export: tri export --format mempalace generates compatible palace structure
  • P6.5seal hash → git commit Closes #<this_issue>

Language Syntax Preview

-- Native memory in t27 language
-- Scope: agent-level persistent memory

agent MyAgent {
  fn on_input(msg: Str) -> Tri {
    -- Store learned fact
    remember("user_preference", msg, scope: Agent)
    
    -- Retrieve with fallback
    let pref = recall("user_preference") ?? "unknown"
    
    -- Fuzzy recall: find related memories (φ-threshold)
    let related = recall_like(msg, threshold: PHI_INV)
    
    -- Reflect on all known facts
    let all = reflect()
    
    -- Structured output
    tri { pref: pref, related_count: related.len() }
  }
  
  fn reset() {
    forget("user_preference")  -- tombstone, not delete
  }
}

Constitutional Compliance

Law Check
L1 TRACEABILITY This PR closes this issue — Closes #N
L2 GENERATION gen/memory/ auto-generated from spec ✓
L3 PURITY All identifiers ASCII/English ✓
L4 TESTABILITY 8 tests defined in Phase 4 ✓
L5 PHI-IDENTITY φ-hash invariant on every MemoryCell ✓
L6 CEILING .trinity/memory/SEAL_HASH as SSOT ✓
L7 UNITY No .sh — all via tri CLI ✓

Acceptance Criteria

  • tri spec check memory passes with 0 errors
  • All 8 tests in Phase 4 pass: tri test memory
  • φ-hash invariant verified by tri verify --phi memory
  • .trinity/memory/SEAL_HASH committed and sealed
  • experience/memory/ has at least 3 entries
  • phi-loop-skills.md updated with memory skill

References


φ² + 1/φ² = 3 | TRINITY
PHI LOOP: edit spec → seal hash → gen → test → verdict → save experience → skill commit → git commit

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions