Skip to main content

Local peer memory system for AI agents — zero-dependency, SQLite-native, FTS5 trigram retrieval with Dual Mirror architecture.

Project description

dyadcore

Local peer memory system for AI agents — zero dependencies, SQLite-native, FTS5 trigram retrieval.

The user and the agent are two poles of a magnetic field, jointly leaving traces in a relational field. Memory is not data — it is a trace that happens in the field.

Quick Start

from dyadcore import DyadCore

dc = DyadCore("agent_memory.db")

# Write traces
uid = dc.write("用户偏好本地部署", source="user", field="技术选型")
dc.anchor(uid)  # pin important memories

# Recall with semantic search + graph expansion
results = dc.recall("本地记忆方案", field_hint="技术选型")

dc.close()

Architecture: Dual Mirror

Mirror Source Role
User Mirror source="user" Records user behavior and expressions
Agent Mirror source="agent" Records agent observations and reasoning
Relation Network reflections table Cross-trace edges: triggered, echoed, contradicted, related

Retrieval pipeline (pure SQLite):

  • Primary: FTS5 trigram with OR query construction (Chinese-friendly, zero-cost)
  • Fallback: LIKE search for terms < 3 characters
  • Graph expansion: 1-hop along reflections edges for semantic neighbors

API Overview

Core Operations

Method Description
write(content, *, source, field, memory_type) Write a trace, returns memory_id
write_batch(items) Bulk write in a single transaction (5-10x faster)
recall(query, *, field_hint, limit) Semantic recall with ranking
anchor(memory_id) / unanchor(memory_id) Pin / unpin a memory
archive(memory_id) / unarchive(memory_id) Archive / restore a memory

Relations

Method Description
add_relation(source_id, target_id, type, strength) Add a relation edge
get_relations(memory_id) Get all edges for a memory
get_relation_graph(memory_id, max_depth) Get subgraph via recursive CTE
get_contradicted_map(result_ids) Get old→new mapping for evolution chains

Introspection

Method Description
stats() Total memories, active, anchored, fields, reflections
list_fields() Field-level stats with polarity
list_by_field(field) All memories in a field
field_snapshot(field) Field strength, polarity, center of gravity
check_silence(days) / check_silence_by_field(days) Find dormant memories

Design Constraints

  • Single-file SQLite — no backend service, no config files
  • Zero external dependencies — no sqlite-vec, no embedding model, no C extensions
  • FTS5 trigram is the only retrieval engine (built into SQLite)
  • Python >= 3.10

Agent Integration

Synchronous Agent Loop

from dyadcore import DyadCore
from hermes_bridge import should_recall, should_write_agent_self, format_for_prompt

dc = DyadCore("agent_memory.db")
history = []
agent_self_count = 0

for turn in conversation:
    field = infer_field_with_llm(turn["user_msg"])  # use your LLM for field classification

    if should_recall(history):
        results = dc.recall(turn["user_msg"], field_hint=field, limit=5)
        memory_context = format_for_prompt(results, dyadcore=dc)
        # inject memory_context into LLM system prompt

    dc.write(turn["user_msg"], source="user", field=field)
    dc.write(turn["agent_msg"], source="agent", field=field)

    if should_write_agent_self(history, agent_self_count):
        reflection = build_agent_reflection(history)  # use your LLM
        dc.write(reflection, source="agent", field=field)
        agent_self_count += 1

    history.append({"role": "user", "content": turn["user_msg"]})
    history.append({"role": "agent", "content": turn["agent_msg"]})

Async Agent Frameworks

dyadcore is synchronous (SQLite). For async agent frameworks, wrap calls with asyncio.to_thread:

import asyncio

results = await asyncio.to_thread(dc.recall, query, field_hint=field)
dc.write(query, source="user", field=field)  # writes are fast, no need to offload

Or use loop.run_in_executor for persistent thread pool reuse.

Bulk Import

items = [
    {"content": "msg1", "source": "user", "field": "onboarding"},
    {"content": "msg2", "source": "agent", "field": "onboarding"},
    # ... hundreds more
]
ids = dc.write_batch(items)  # single transaction

Running Tests

python test_dyadcore.py        # Full test suite (9 test classes)
python test_decay_topology.py  # Decay + topology verification
python test_real_data.py       # Integration test

Benchmarks

python benchmark.py          # Local performance (no LLM)
python eval_retrieval.py     # Retrieval quality (MRR, Hit Rate, NDCG)

License

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dyadcore_mem-0.2.0.tar.gz (24.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dyadcore_mem-0.2.0-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file dyadcore_mem-0.2.0.tar.gz.

File metadata

  • Download URL: dyadcore_mem-0.2.0.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for dyadcore_mem-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a3d8c8bfc550b0fd864cc5fadfef68e27688f7750302899322c83081a4a660b5
MD5 a909afa79a610833d6eddf4a0a5294dc
BLAKE2b-256 8481d0b0b5162d1dd30560be3fe35ba3e8213f74899d92f496a8f1c42b74a88c

See more details on using hashes here.

File details

Details for the file dyadcore_mem-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: dyadcore_mem-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for dyadcore_mem-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c3ad08ecae04dd0dbbcc128746605fc5b7920db4d6c8db06db7f65bff078fb79
MD5 c970166998de68f3a903ab2703854464
BLAKE2b-256 b0415b9d44cf4949083c074464b92cd0c6d048e032bbd7154c59fa381c02fcae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page