DEV Community

Cover image for Bi-Temporal Memory for AI Agents: Knowledge Graphs, Auditable Retention, and Lossless Context Compression
Benni Alencar
Benni Alencar

Posted on Originally published at benni-os.net

Bi-Temporal Memory for AI Agents: Knowledge Graphs, Auditable Retention, and Lossless Context Compression

By **Benni Alencar* — Chief AI Systems Architect at Benni OS. Canonical article & interactive benchmarks at benni-os.net.*


1. The Amnesia & Stale-Fact Paradox in Agentic RAG

Most production AI agents do not fail because the underlying foundation model cannot reason. They fail because their memory substrate cannot distinguish historical truth from current truth, and cannot compact long-horizon observations without silently erasing provenance.

When teams bolt a flat vector database onto an autonomous loop, three failure modes emerge under load:

  • Semantic Overwrite Without Versioning: If a deployment policy changes from region: us-east-1 to region: eu-central-1, both embeddings share 92% cosine similarity with the query. The retriever pulls both chunks, and the LLM flips a coin.
  • Destructive Summarization Drift: Naive sliding-window compaction asks an LLM to summarize older turns. Every recursive summary drops edge-case constraints, exact SHA hashes, and numerical thresholds—hallucinating confidence where precision was lost.
  • Cross-Tenant Contamination: Shared embedding namespaces and unpartitioned caches risk leaking entity context across tenants or workspaces when metadata filters are bypassed or misconfigured.

    **Core Thesis:** Production agent memory is a **database systems problem**, not an embedding similarity problem. By combining bi-temporal typed knowledge graphs in **`BenniAlencar/agent-memory-mesh`** with pointer-preserving compaction in **`BenniAlencar/agent-deep-research`**, agents gain auditable recall and lossless long-horizon context.
    

2. Valid-Time vs. Transaction-Time: The Bi-Temporal Edge Model

In classical temporal databases (SQL:2011 and immutable accounting ledgers), overwriting a row destroys auditability. agent-memory-mesh models every fact as an immutable bi-temporal edge carrying two orthogonal time axes:

  • Valid-Time (t_valid_start, t_valid_end): The interval during which the assertion holds in the external world (e.g., when a contract was active or a feature flag was enabled).
  • Transaction-Time (t_tx_recorded, t_tx_superseded): The interval during which the agent's memory ledger believed that assertion to be current, before a newer observation superseded it.
flowchart LR
    A["New Observation\n(Event @ t_valid)"] --> B["Typed Triple Extractor\n(Subject, Predicate, Object)"]
    B --> C{"Existing Edge\nConflict?"}
    C -->|"No"| D["Insert Active Edge\nt_tx_superseded = NULL"]
    C -->|"Yes"| E["Close Prior Edge\nSet t_tx_superseded = now()"]
    E --> F["Append New Edge\nLink superseded_by pointer"]
    D --> G["Point-in-Time Audit\nAS OF (t_valid, t_tx)"]
    F --> G

Because superseded edges are closed rather than deleted, an operator can execute two fundamentally different queries against the same graph: "What is the customer's rate limit today?" (t_tx_superseded IS NULL) and "What did the agent believe the rate limit was when it triggered the incident on Tuesday?" (AS OF t_tx = Tuesday).


3. Typed Triples, Provenance & Strict Tenant Isolation

Unstructured text chunks cannot enforce schema invariants. Every assertion persisted in agent-memory-mesh is normalized into a Typed Triple bound to an explicit tenant_id partition:

from dataclasses import dataclass
from typing import Optional

@dataclass(frozen=True)
class BiTemporalTriple:
    tenant_id: str          # Hard partition key across graph + BM25 index
    subject_id: str         # Typed entity, e.g. "service:billing-api"
    subject_type: str       # "service" | "user" | "policy" | "artifact"
    predicate: str          # Controlled relation, e.g. "deployed_in"
    object_id: str          # e.g. "region:eu-central-1"
    confidence: float       # Calibrated [0.0, 1.0]
    source_digest: str      # SHA-256 of raw evidence payload
    t_valid_start: str      # ISO-8601 UTC
    t_valid_end: Optional[str]
    t_tx_recorded: str      # ISO-8601 UTC
    t_tx_superseded: Optional[str] = None
Enter fullscreen mode Exit fullscreen mode

Notice two critical engineering properties:

  1. Storage-Level Tenant Isolation: Every graph adjacency lookup and lexical index scan requires tenant_id as a mandatory prefix key. Multi-hop traversals cannot cross tenant boundaries even if two tenants reference identical external entity names like service:auth-gateway.
  2. Immutable Provenance Binding: Every triple stores source_digest, anchoring the extracted claim to the exact raw tool output, document chunk, or API payload that justified it.

4. Hybrid Retrieval: BM25 Lexical Precision + Multi-Hop Graph Traversal

Pure vector search notoriously struggles with exact identifiers—error codes like ERR_TLS_CERT_ALTNAME_INVALID, ticket IDs, or specific function symbols. Pure graph traversal, conversely, requires an exact starting node.

agent-memory-mesh solves this with a two-stage hybrid retrieval pipeline fused via Reciprocal Rank Fusion (RRF):

  • Stage 1 — Tenant-Scoped BM25 Sparse Scoring: Matches exact tokens, symbols, and domain terms against active triples and evidence headers in sub-millisecond time.
  • Stage 2 — Bi-Temporal k-Hop Expansion: Expands from the top-ranked entity anchors along active edges (t_tx_superseded IS NULL), collecting causal dependencies, owner policies, and recent state transitions.
  • Stage 3 — RRF + Temporal Decay Reranking: Combines lexical rank, graph centrality, and valid-time recency into a deterministic context slice bounded by the caller's token budget.

5. Lossless Context Compression in agent-deep-research

Deep research and multi-step engineering workflows routinely accumulate 150,000+ tokens of web scrapes, code diffs, and tool outputs. Re-injecting raw payloads into every turn blows past latency and cost budgets, while lossy LLM summarization destroys citations and exact numbers.

BenniAlencar/agent-deep-research pairs with agent-memory-mesh to implement Lossless Context Compression:

  • Content-Addressed Spillover: Raw payloads exceeding a threshold are hashed (SHA-256), persisted in the local artifact store, and replaced in the active prompt by a compact Semantic Stub containing the extracted typed triples plus the content pointer.
  • Zero-Loss Dereferencing: If a synthesis or verification step needs the verbatim table, stack trace, or quote, the agent invokes a targeted deref(source_digest, byte_range) lookup—hydrating only the exact span required.
  • Auditable Retention Policies: Expired or superseded evidence can be garbage-collected by TTL or compliance policy while preserving cryptographic tombstones in the audit log.

6. Architecture Comparison: Agent Memory Substrates

Dimension Sliding Window + Summary Flat Vector RAG agent-memory-mesh + deep-research
Temporal Semantics None (chronological overwrite) Unversioned chunks Bi-Temporal (Valid + Transaction Time)
Fact Mutation Handling Silently forgets prior state Stale chunks compete with updates Supersedes edge; preserves full audit trail
Exact Symbol / ID Recall Lost after compaction Weak (dense embedding blur) BM25 + Graph Hybrid RRF
Context Compression Lossy LLM paraphrasing Top-k chunk truncation Lossless SHA-256 Pointer Compaction
Multi-Tenant Isolation Session-only Post-filter metadata (error-prone) Mandatory Prefix-Key Partitioning

Build Auditable, Lossless Agent Memory

Explore the open-source implementations powering Benni OS: inspect the bi-temporal graph engine in agent-memory-mesh and lossless research compaction in agent-deep-research.

    Explore agent-memory-mesh →
Explore agent-deep-research →
Enter fullscreen mode Exit fullscreen mode




Frequently Asked Questions


      What is the difference between valid-time and transaction-time in AI agent memory?
Valid-time tracks when a fact was true in the external world (for example, a user's subscription tier or an API endpoint version from March 1 to May 15). Transaction-time tracks when the agent recorded or superseded that fact in its memory ledger. Separating both axes lets an agent answer both "what is true right now?" and "what did the agent believe was true at audit timestamp T?" without overwriting history.
  Why does flat vector RAG fail for long-running autonomous agents?
  Flat vector stores treat memory as an unversioned bag of embeddings. When a constraint or state changes, old chunks remain semantically similar to the query and outrank newer updates, causing stale-fact hallucinations. Vector similarity also struggles with exact entity identifiers, multi-hop causal chains, and strict per-tenant isolation.


  How does lossless context compression work without dropping provenance?
  Instead of destructive LLM summarization that discards raw evidence, lossless context compression replaces verbose intermediate payloads with content-addressed SHA-256 pointers and typed graph triples. When a downstream reasoning step in `agent-deep-research` needs exact quotes, numbers, or citations, it dereferences the pointer on demand while keeping the active prompt window compact.


  How does BenniAlencar/agent-memory-mesh enforce multi-tenant isolation?
  Every node, typed edge, BM25 inverted index entry, and retention policy in `agent-memory-mesh` is scoped to an explicit `tenant_id` partition key. Cross-tenant graph traversal is impossible at the storage and query-planner layers, preventing context leakage across workspaces or customer sessions.
Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode




Frequently Asked Questions

What is the difference between valid-time and transaction-time in AI agent memory?

Valid-time tracks when a fact was true in the external world (for example, a user's subscription tier or an API endpoint version from March 1 to May 15). Transaction-time tracks when the agent recorded or superseded that fact in its memory ledger. Separating both axes lets an agent answer both "what is true right now?" and "what did the agent believe was true at audit timestamp T?" without overwriting history.

Why does flat vector RAG fail for long-running autonomous agents?

Flat vector stores treat memory as an unversioned bag of embeddings. When a constraint or state changes, old chunks remain semantically similar to the query and outrank newer updates, causing stale-fact hallucinations. Vector similarity also struggles with exact entity identifiers, multi-hop causal chains, and strict per-tenant isolation.

How does lossless context compression work without dropping provenance?

Instead of destructive LLM summarization that discards raw evidence, lossless context compression replaces verbose intermediate payloads with content-addressed SHA-256 pointers and typed graph triples. When a downstream reasoning step in agent-deep-research needs exact quotes, numbers, or citations, it dereferences the pointer on demand while keeping the active prompt window compact.

How does BenniAlencar/agent-memory-mesh enforce multi-tenant isolation?

Every node, typed edge, BM25 inverted index entry, and retention policy in agent-memory-mesh is scoped to an explicit tenant_id partition key. Cross-tenant graph traversal is impossible at the storage and query-planner layers, preventing context leakage across workspaces or customer sessions.


Explore the Benni Master OS Open-Source Ecosystem

Top comments (0)