canonarc
Branchable, spoiler-free narrative memory & world-state engine for LLM games, interactive fiction, localization, and multi-agent simulations — in a single SQLite file.
Handbook • Architecture • Epistemics & POV • Examples • Benchmarks • API Reference
Why canonarc?
Most LLM memory frameworks (mem0, Zep, LangMem) are built for conversational chatbots running in continuous wall-clock time. They break down in Interactive Fiction, Visual Novels (Ren'Py / Ink), Multi-Agent Simulations, Creative Writing Co-Pilots, and Sequential Localization Pipelines, where time advances in discrete causal steps (turns, scenes, ticks, or chunks) and world state must be rewindable, branchable, character-scoped, and spoiler-free.
The 6-Tier World-State Model
canonarc unifies narrative state, dialogue voice, and episodic memory inside a single append-only SQLite database (or :memory:):
| Tier | Layer | What It Tracks |
|---|---|---|
| Tier 1 | Base Entity Attributes | Immutable character/item metadata seeded at step 0 (gender, declension, archetypes, exonyms). |
| Tier 2 | Asymmetric Dyadic Edges | Directed relationships (A → B ≠ B → A) such as register, tone, trust, or hidden facade vs. stance. |
| Tier 3 | Rolling State & Trajectories | Point-in-time entity/edge state deltas with windowed history (prior_state, prior_step, revision_count). |
| Tier 4 | Temporal Versioning & 1-Hop Coreference | Strict step < current_step resolution, alias linking ("the old wizard" → char_merlin), and co-present vs. background separation. |
| Tier 5 | Gated Voice Bank | Empirical historical dialogue anchors filtered by speaker and active register/tone, protected by a 2-stage anti-drift gate. |
| Tier 6 | Episodic Ledger & Epistemics | N-ary plot events with causal provenance (caused_by_episode_id) and per-character knowledge (direct, told, deceived, forgotten, suppressed). |
How It Fits in Your Loop
Scene Text / Tick
│
▼
┌──────────────────────────────────────────────────────────┐
│ step_transaction(step, text, pov_entity_ids=[...]) │
│ 1. Retrieve Tiers 1–6 strictly at (step < current) │
│ 2. Project through POV lens (redact secrets/disguises) │
└──────────────────────────┬───────────────────────────────┘
│ formatted prompt block
▼
Your LLM / Game Loop
│ generated scene / delta
▼
┌──────────────────────────────────────────────────────────┐
│ Extract StepDelta + 2-Stage Anti-Drift Voice Gate │
│ Atomic commit to single-file SQLite lineage tree │
└──────────────────────────────────────────────────────────┘
How canonarc Compares
| Capability | Chatbot Memory (mem0, Zep, LangMem) |
Storyworld / GraphRAG (Graphiti, ComoRAG) |
canonarc |
|---|---|---|---|
| Time Model | Continuous wall-clock timestamps | Wall-clock or linear chapter order | Discrete causal steps (step: int) + flashback-safe story_time |
| Branching & Rollback | Linear history only | None or heavy subgraph copies | Zero-copy SQLite lineage tree (0.09 ms branch, 0.07 ms undo) |
| Character POV & Secrets | Omniscient queries | Omniscient world graph | Per-POV epistemic projection (direct, told, deceived, disguise masking) |
| Dialogue Voice Control | None | Unfiltered prose retrieval (prone to drift) | Tier 5 Voice Bank (2-stage anti-drift gate + dyadic register filtering) |
| State + Event Provenance | Flat facts or key-value dicts | Nodes/edges or text chunks | Tiers 1–4 State Graph linked to Tier 6 Episodes via caused_by_episode_id |
| Storage & Dependencies | Cloud APIs / Vector DB + Graph DB | Neo4j / Kuzu / FalkorDB + Vector DB | Single .sqlite file (pydantic, numpy, rapidfuzz only) |
When to Choose Something Else
canonarc is intentionally scoped to storyworlds, simulations, and discrete-step pipelines (see Domains of Application). Consider another tool if you need:
- Continuous wall-clock enterprise chatbots or CRM logs: Systems built around real-world validity timestamps (
t_valid/t_invalid) likeZeporGraphitiare a better fit. - Million-document static corpus RAG or multi-hop analytical QA:
canonarcuses in-memory NumPy scoring over storyworld-scale histories and 1-hop graph expansion; dedicated vector stores or full graph databases (e.g., Neo4j) suit massive static corpora. - High-concurrency multi-writer distributed backends: Single-file SQLite excels for local saves, game loops, and per-session sandboxes, not horizontally sharded multi-tenant writes.
Quickstart
Installation
pip install canonarc
(Requires Python 3.11+. Core dependencies: pydantic>=2.0, numpy>=1.26, rapidfuzz>=3.0, and stdlib sqlite3. To install from source: pip install git+https://github.com/jovistos/canonarc.git.)
1. The Transactional LLM Loop (step_transaction)
For interactive game loops, visual novels, and automated LLM pipelines, step_transaction() handles spoiler-free retrieval, LLM extraction, anti-drift voice gating, and atomic commits in one block:
from canonarc import CanonArcEngine, OpenAICompatibleExtractor
engine = CanonArcEngine(
"story_save.sqlite",
extractor=OpenAICompatibleExtractor.from_gemini(), # or .from_lmstudio(), .from_deepseek()
)
with engine.step_transaction(step=85, text=scene_text, pov_entity_ids=["char_arthur"]) as tx:
# 1. Point-in-time context (clean on reruns, zero forward spoiler leakage)
prompt = engine.format_context(tx.context)
# 2. Generate the next scene or dialogue with your LLM
generated_scene = call_llm(prompt)
# 3. Extract StepDelta JSON + Gate 1/2 voice exemplars & commit atomically
tx.extract_and_commit(generated_scene)
# Or commit deterministic game-engine deltas directly:
# tx.commit(delta=my_step_delta, exemplars=candidate_lines)
2. Explicit World-State Walkthrough (No LLM Required)
You can also drive canonarc deterministically from a game engine or script:
from canonarc import (
CanonArcEngine, EntityRecord, StepDelta,
EntityDelta, EdgeDelta, EpisodeRecord, VoiceExemplar,
)
engine = CanonArcEngine("story_save.sqlite") # or ":memory:"
# Seed entities & 1-hop alias (Step 0)
engine.register_entity(
EntityRecord(entity_id="char_arthur", canonical_name="Arthur", base_attributes={"title": "King", "gender": "M"}),
step=0,
)
engine.register_entity(
EntityRecord(entity_id="char_merlin", canonical_name="Merlin", base_attributes={"title": "Court Wizard", "gender": "M"}),
aliases=["the old wizard"],
step=0,
)
engine.register_entity(
EntityRecord(entity_id="char_mordred", canonical_name="Mordred", base_attributes={"title": "Knight", "gender": "M"}),
step=0,
)
# Seed initial relationship (Step 10) & Voice Bank anchors (Step 15)
engine.commit_step(
step=10,
delta=StepDelta(edges=[
EdgeDelta(source_id="char_arthur", target_id="char_merlin", state={"register": "formal", "tone": "respectful"})
]),
)
engine.index_voice_exemplars(
step=15,
exemplars=[
VoiceExemplar(
speaker_id="char_arthur", step=15,
text="I beseech your counsel, venerable wizard, for the kingdom stands upon the precipice.",
metadata={"register": "formal", "tone": "respectful"}, quality_score=9.5,
),
VoiceExemplar(
speaker_id="char_arthur", step=15,
text="Enough riddles, old man — answer me plainly or leave this hall!",
metadata={"register": "informal", "tone": "furious"}, quality_score=9.2,
),
],
)
# Step 82: Argument occurs -> mood, stance, and causal episode committed together
engine.commit_step(
step=82,
delta=StepDelta(
entities=[EntityDelta(
entity_id="char_arthur", state={"mood": "furious"},
caused_by_episode_id="ep_step82_prophecy_dispute",
reason="Discovered Merlin concealed the prophecy",
)],
edges=[EdgeDelta(
source_id="char_arthur", target_id="char_merlin",
state={"register": "informal", "tone": "furious"},
caused_by_episode_id="ep_step82_prophecy_dispute",
reason="Concealed the dragon prophecy",
)],
episodes=[EpisodeRecord(
episode_id="ep_step82_prophecy_dispute",
predicate="argument",
summary="Arthur confronted Merlin over the concealed dragon prophecy in the council chamber.",
participants={"char_arthur": "actor", "char_merlin": "target"},
witnesses=["char_arthur", "char_merlin"],
salience=8.5,
)],
),
)
# Step 85: Unified Tier 1–6 retrieval ("the old wizard" resolves to char_merlin)
context = engine.retrieve_context(
step=85,
text='Arthur glared at the old wizard while Mordred watched. "Speak truth now!"',
mentions=["Arthur", "the old wizard", "Mordred"],
top_k_voice=3,
top_k_episodes=5,
)
print(engine.format_context(context))
Rendered output of engine.format_context(context):
### Active Scene Narrative Context (Step: 85)
#### Characters Present & State:
- **Arthur** (char_arthur): mood: furious [reason: Discovered Merlin concealed the prophecy]
- **Merlin** (char_merlin): mood: neutral
- **Mordred** (char_mordred): mood: neutral
#### Interpersonal Stances & Register:
- char_arthur → char_merlin: register=informal, tone=furious, trust=0.0 (because: Concealed the dragon prophecy)
#### Episodic Memory (Relevant Past Events):
- [Step 82 | argument | known to: char_arthur, char_merlin | ignorant: char_mordred] Arthur confronted Merlin over the concealed dragon prophecy in the council chamber.
#### Voice Bank (Historical Dialogue Anchors):
- char_arthur [informal, furious]: "Enough riddles, old man — answer me plainly or leave this hall!"
Core Capabilities
Anti-Metagaming, POV Projection & Deception
Project the entire 6-tier world state through a character's eyes so NPCs never act on secrets they didn't witness (see Epistemic State, Disguises & POV Projection):
# Query Step 85 strictly from Mordred's POV (absent at Step 82)
mordred_pov = engine.retrieve_context(
step=85,
text="Mordred observed Arthur and Merlin.",
mentions=["Arthur", "Merlin", "Mordred"],
pov_entity_ids=["char_mordred"],
)
# Mordred sees Arthur is furious, but the secret episode & causal reason are redacted:
# mordred_pov.episodes == []
# arthur.reason is None
# Feed Mordred a deceptive cover story at Step 86
engine.inform_entity(
step=86,
episode_id="ep_step82_prophecy_dispute",
entity_id="char_mordred",
epistemic="deceived",
believed_summary="Arthur reprimanded Merlin over a missing supply ledger.",
)
# Mordred recalls the cover story — and vector search embeds believed_summary, never the secret!
mordred_deceived = engine.retrieve_context(
step=87,
text="Mordred asked about the dispute.",
mentions=["Mordred", "Arthur"],
pov_entity_ids=["char_mordred"],
)
print(mordred_deceived.episodes[0].summary)
# -> "Arthur reprimanded Merlin over a missing supply ledger."
Zero-Copy Branching & Fast Rollback
# Fork an alternate timeline at Step 50 (zero data copying)
engine.branch(at_step=50, name="reconciliation_route")
engine.checkout("reconciliation_route")
# Inherits state up to Step 50, ignoring Step 82 on 'main'
branch_ctx = engine.retrieve_context(step=85, mentions=["Arthur", "Merlin"])
# branch_ctx.active_edges[0].state["register"] == "formal"
# Compare divergent timelines at Step 85
diff = engine.diff_timelines("main", "reconciliation_route", at_step=85)
# Atomically rewind 'main' back to Step 10 (revisions & episodes > 10 deleted)
engine.rollback_to(step=10, timeline_id="main")
CLI Timeline Inspector
Inspect branching timelines, POV world state, diffs, and entity trajectories directly from .sqlite saves (pass --json to any subcommand for machine-readable output):
$ canonarc tree story_save.sqlite
main [head=86]
└─ reconciliation_route (fork@50) [head=50]
$ canonarc diff story_save.sqlite main reconciliation_route --step 85
Diff main vs reconciliation_route @ step 85:
Changed (2):
- char_arthur: {... 'mood': 'furious', 'reason': 'Discovered Merlin concealed the prophecy'} vs {... 'state': {}}
- char_arthur:stance->char_merlin: {'register': 'informal', 'tone': 'furious', ...} vs {'register': 'formal', 'tone': 'respectful'}
Identical keys: 3
# Additional commands:
canonarc inspect story_save.sqlite --timeline main --step 85 --pov char_arthur
canonarc history story_save.sqlite char_arthur --timeline main
Performance & Benchmarks at a Glance
Engine algorithmic scaling micro-benchmarks from scripts/benchmark_engine_performance.py (see Benchmarks for full LLM extraction & long-horizon marathon tables):
| Metric (1,500 Steps, 120 Characters) | Result | Notes |
|---|---|---|
Zero-Copy branch() |
0.087 ms |
Constant-time lineage fork |
1-Step Undo rollback_to() |
0.067 ms |
4.48 ms for 50% deep rollback (-750 steps) |
retrieve_context() P50 |
26.9 ms |
21.3 ms after consolidate_episodes() |
| SQLite Save File Size | 2.03 MB |
436 KB (-78.5%) post-consolidation |
| Deceived-POV Vector Isolation | 0% leakage |
100% Recall@1 on believed_summary, adversarial secret queries blocked |
Interactive Example Notebooks
Self-contained Jupyter notebooks in examples/ walk through each primary domain:
| Notebook | Domain | Preset | Key Capabilities Demonstrated |
|---|---|---|---|
01_interactive_fiction_and_rpg.ipynb |
Interactive Fiction, Visual Novels & RPGs | InteractiveFictionPreset |
Tiers 1–6, 1-hop alias coreference, 2-stage anti-drift IngestionGate, prior_state trajectory, dramatic irony (| ignorant:), branch(), diff_timelines(), rollback_to(), export_snapshot() |
02_literary_translation.ipynb |
Long-Form Literary Translation | TranslationPreset |
Pure-English A/B comparison, transactional step_transaction() pipeline, verified Vi→ti register propagation, automated Tier 5 Voice Bank harvesting & anti-drift gating, flashback safety, linguistic glosses & QA audit |
03_multi_agent_simulation.ipynb |
Multi-Agent Autonomous Simulations | SimulationPreset |
Custom SchemaPolicy & EpisodicScoringPolicy, per-tick directed affinity edges, partial observability (pov_entity_ids), secondhand gossip (InformDelta), consolidate_episodes() |
04_mystery_and_social_deduction.ipynb |
Mystery, Espionage & Social Deduction | InteractiveFictionPreset |
Disguise & persona masking (AliasRecord.known_to → masked:<id>), facade vs. hidden stance edges, private_state redaction, deceived beliefs (believed_summary vector isolation), disguise piercing |
pip install -e ".[notebooks]"
jupyter lab examples/
Built-In Adapters, Sessions & Lifecycle Hooks
- Embedders (no extra SDKs required) (
canonarc.embedders):OfflineNgramEmbedder(with CJK tokenization & signed feature hashing),CallableEmbedder,OpenAICompatibleEmbedder, andOllamaEmbedderwithembed_batch()and automatic embedder-mismatch detection (on_embedder_mismatch="warn" | "raise" | "reindex" | "ignore"). - Extractors (no extra SDKs required) (
canonarc.extractors):CallableExtractor,OpenAICompatibleExtractor(with.from_lmstudio(),.from_gemini(),.from_deepseek()), and content-addressedCachedDeltaExtractorwithTokenUsagecost estimation. - Tier 6 Episodic Policy & Consolidation (
canonarc.episodic): TunableEpisodicScoringPolicy,DefaultEpisodeRanker, flashback-safeStepDelta(is_flashback=True),"forgotten"/"suppressed"epistemic updates, andengine.consolidate_episodes(...). - Concurrency-Safe Sessions & Hooks (
canonarc.hooks): Usewith CanonArcEngine("save.sqlite") as engine:,engine.session("branch_a"),with engine.use_timeline("branch_a"):, andBaseEngineHooks(on_before_commit,on_after_commit,on_rollback,on_branch,on_gate_rejected).
Documentation
- The
canonarcHandbook: End-to-end tutorial & architectural book covering ontology, recursive CTE lineage, anti-drift gates, epistemic POV projection, live LLM extraction, and worked examples. - Architecture Guide: Deep dive into the 6-tier state superset, SQLite schema, and windowed recursive CTE.
- Epistemic State, Disguises & POV Projection: Guide to anti-metagaming,
"direct"/"told"/"deceived"/"forgotten"modes, scoped disguises (known_to), andfacadevs.stanceedges. - Causal Time Travel: Lineage trees, copy-on-write branching, atomic rollback, non-linear flashbacks, and save-slot exports.
- Voice Bank & Anti-Drift: The 2-stage ingestion gate, multi-party dyadic register resolution, and embedder migration.
- Domain Presets: Pre-built prompt formatters and
SchemaPolicyconfiguration for Interactive Fiction, Localization, and Multi-Agent Simulations. - Domains of Application: Primary and emerging use cases, domain-to-capability matrix, and when to choose another tool.
- Synthetic Story & Engine Benchmarks: Multi-model extraction accuracy, epistemic leak metrics, engine scaling micro-benchmarks, and reproduction guide.
- API Reference: Detailed reference for classes, methods, extractors, embedders, CLI commands, and Pydantic schemas.
Development & Contributing
Contributions, bug reports, and new domain presets or adapters are welcome:
git clone https://github.com/jovistos/canonarc.git
cd canonarc
pip install -e ".[dev]"
# Run linter, strict type checker, and test suite
ruff check src tests
mypy src
pytest
License
MIT License. Free and open source.
Metadata
Release files for canonarc 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| canonarc-0.1.0.tar.gz | 118.8 kB | With provenance | Details for canonarc-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| canonarc-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for canonarc-0.1.0-py3-none-any.whl |
Total release size: 192.5 kB
Release files / canonarc-0.1.0.tar.gz
| Download URL | canonarc-0.1.0.tar.gz |
|---|---|
| Size | 118.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c7f79b5a3f45210988febf90be5254accb28348463af24d12623f67699aa9443
|
|
BLAKE2b-256 checksum How to use checksums |
47480258d168c1b7e7f4fd241bd2a34f80f7e25c8a40a00ab4f06b969bef44dc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logRelease files / canonarc-0.1.0-py3-none-any.whl
| Download URL | canonarc-0.1.0-py3-none-any.whl |
|---|---|
| Size | 73.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
44b3da49738989220572f366bac6b55ca460643c92893f1185c78b973ef43d59
|
|
BLAKE2b-256 checksum How to use checksums |
f0e80e3b943083c23489e831bfe1888ce8fb543ce62ce489be9b7a75e286c988
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logSecurity for canonarc 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for canonarc-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.