Skip to content

API Reference parrot.memory.episodic.backends.sqlite

github-actions[bot] edited this page Dec 2, 2026 · 1 revision

parrot.memory.episodic.backends.sqlite

View source

SQLite episode persistence under the accepted local-store contract.

Alias AbstractEpisodeBackend

Re-exported from parrot.memory.episodic.backends.abstract.AbstractEpisodeBackend.

Alias EpisodeSearchResult

Re-exported from parrot.memory.episodic.models.EpisodeSearchResult.

Alias EpisodicMemory

Re-exported from parrot.memory.episodic.models.EpisodicMemory.

Alias Grade

Re-exported from parrot.memory.dynamics.models.Grade.

Alias MemoryCitation

Re-exported from parrot.memory.dynamics.models.MemoryCitation.

Alias MemoryNamespace

Re-exported from parrot.memory.episodic.models.MemoryNamespace.

Alias MemoryParameters

Re-exported from parrot.memory.dynamics.models.MemoryParameters.

Alias MemoryState

Re-exported from parrot.memory.dynamics.models.MemoryState.

Alias ReviewCommand

Re-exported from parrot.memory.dynamics.models.ReviewCommand.

Alias ReviewReceipt

Re-exported from parrot.memory.dynamics.models.ReviewReceipt.

Class SQLiteEpisodeBackend

Persist scoped episodes using async I/O, WAL and the accepted vector ceiling.

Parameter Type Default Description
path Path SQLite database path owned by this backend.
busy_timeout_ms int 5000 SQLite busy timeout for contending writers.
vector_ceiling int _DEFAULT_VECTOR_CEILING Largest scoped candidate set scored exactly per search.

Method SQLiteEpisodeBackend.configure

Open the connection and apply idempotent versioned schema changes.

configure() -> None

Raises:

  • ValueError — If the database schema is newer than this backend supports.

Method SQLiteEpisodeBackend.close

Drain owned writes and release resources without claiming lost acknowledgements.

close() -> None

Method SQLiteEpisodeBackend.store

Persist one episode and acknowledge only after commit.

store(episode: EpisodicMemory) -> str

Parameter Type Default Description
episode EpisodicMemory Episode to write once by its immutable identifier.

Returns: str — The supplied episode identifier.

Method SQLiteEpisodeBackend.apply_review

Atomically deduplicate, check revision and commit review state.

The review row contains the complete validated command, making the local transaction both the authoritative review log and the replay/archive source. A caller can therefore safely retry after losing an acknowledgement or after a subordinate export fails.

apply_review(command: ReviewCommand, *, grade: Grade | None = None, memory_error_signature: str | None = None, parameters: MemoryParameters | None = None) -> ReviewReceipt

Parameter Type Default Description
command ReviewCommand
grade Grade | None None
memory_error_signature str | None None
parameters MemoryParameters | None None

Returns: ReviewReceipt

Method SQLiteEpisodeBackend.get_review_expected_revision

Read the expected_revision of the review committed for a dedupe key.

A retry has to present the command exactly as it was committed to be recognized as a duplicate. The committed row is the only source that survives a restart, so a caller rebuilds its command from this value instead of from process-local bookkeeping.

get_review_expected_revision(dedupe_key: str) -> int | None

Parameter Type Default Description
dedupe_key str ReviewCommand.dedupe_key of the review being retried.

Returns: int | None — The committed command's expected_revision, or None when no review is committed for the key.

Raises:

  • RuntimeError — If the backend has not been configured.

Method SQLiteEpisodeBackend.merge_citation

Durably merge a citation into the references already cited for its exposure.

The read, merge and write happen in one BEGIN IMMEDIATE transaction, so two processes citing the same exposure cannot lose each other's references. Citations older than the retention window are pruned in the same transaction, which keeps the table bounded without a separate maintenance job.

merge_citation(citation: MemoryCitation) -> MemoryCitation

Parameter Type Default Description
citation MemoryCitation Validated references cited for one final-delivery exposure.

Returns: MemoryCitation — Every reference cited for the exposure after the merge.

Raises:

  • ValueError — If the exposure was cited under another namespace or the merge exceeds the citation cardinality.
  • RuntimeError — If the backend has not been configured.

Method SQLiteEpisodeBackend.get_citation

Read the references durably cited for one exposure.

get_citation(exposure_id: str) -> MemoryCitation | None

Parameter Type Default Description
exposure_id str Final-delivery exposure identifier.

Returns: MemoryCitation | None — The committed citation, or None when the exposure was never cited or its citation has been pruned.

Raises:

  • RuntimeError — If the backend has not been configured.

Method SQLiteEpisodeBackend.delete_citations

Delete the durable citations of released exposures.

delete_citations(exposure_ids: list[str]) -> int

Parameter Type Default Description
exposure_ids list[str] Final-delivery exposure identifiers to forget.

Returns: int — The number of citations deleted.

Raises:

  • RuntimeError — If the backend has not been configured.

Method SQLiteEpisodeBackend.get_memory_states

Read the earned dynamics state committed for the given memory references.

A reference key is the MemoryRef.canonical_key hash of the full namespace, memory id and content version, so a caller can only read the state of a memory whose exact scope it already holds.

get_memory_states(ref_keys: Sequence[str]) -> dict[str, MemoryState]

Parameter Type Default Description
ref_keys Sequence[str] Canonical reference keys to resolve; duplicates are read once.

Returns: dict[str, MemoryState] — The committed state per reference key. A key with no review, or with a review that recorded no state, is absent.

Raises:

  • RuntimeError — If the backend has not been configured.

Method SQLiteEpisodeBackend.list_reviews

Return committed reviews for one namespace in a deterministic total order.

list_reviews(namespace: MemoryNamespace, *, limit: int | None = None) -> list[dict[str, Any]]

Parameter Type Default Description
namespace MemoryNamespace Scope of committed review commands to return. Every set dimension must match; unset dimensions are unconstrained, exactly as in build_filter.
limit int | None None Optional maximum row count after scope filtering and ordering.

Returns: list[dict[str, Any]] — Serialized committed review rows in durable total order.

Raises:

  • ValueError — If limit is non-positive.
  • RuntimeError — If the backend has not been configured.

Method SQLiteEpisodeBackend.search_similar

Search scoped vectors through the generic backend contract.

search_similar(embedding: list[float], namespace_filter: dict[str, Any], top_k: int = 5, score_threshold: float = 0.3, include_failures_only: bool = False) -> list[EpisodeSearchResult]

Parameter Type Default Description
embedding list[float]
namespace_filter dict[str, Any]
top_k int 5
score_threshold float 0.3
include_failures_only bool False

Returns: list[EpisodeSearchResult]

Method SQLiteEpisodeBackend.search_semantic

Filter before ranking and expose vector-ceiling degradation explicitly.

The first vector_ceiling candidates are the most recent scoped rows when the measured exact-search ceiling is exceeded. Candidates are scored in a worker thread. SQLite evaluates metadata predicates; only winning payloads are materialized in Python.

search_semantic(embedding: list[float], namespace_filter: dict[str, Any], top_k: int = 5, score_threshold: float = 0.3, include_failures_only: bool = False, *, interaction_mode: bool = False, exclude_session_id: str | None = None, embedding_identity: str | None = None) -> list[EpisodeSearchResult]

Parameter Type Default Description
embedding list[float]
namespace_filter dict[str, Any]
top_k int 5
score_threshold float 0.3
include_failures_only bool False
interaction_mode bool False
exclude_session_id str | None None
embedding_identity str | None None

Returns: list[EpisodeSearchResult]

Method SQLiteEpisodeBackend.embedding_backfill_page

Read a bounded, scoped page needing vectors, retaining payloads for compare-and-swap.

embedding_backfill_page(namespace_filter: dict[str, Any], embedding_identity: str, *, after_id: str = '', limit: int = 32) -> list[tuple[EpisodicMemory, str]]

Parameter Type Default Description
namespace_filter dict[str, Any]
embedding_identity str
after_id str ''
limit int 32

Returns: list[tuple[EpisodicMemory, str]]

Method SQLiteEpisodeBackend.update_episode_embedding

Update derived vectors only if the scoped episode still exists with the same payload.

Concurrent metadata edits or deletion cause a miss, never an overwrite or resurrection. Content, citations, dynamics and full-text index entries are unchanged.

update_episode_embedding(episode_id: str, namespace_filter: dict[str, Any], *, expected_payload: str, embedding: list[float], embedding_identity: str) -> bool

Parameter Type Default Description
episode_id str
namespace_filter dict[str, Any]
expected_payload str
embedding list[float]
embedding_identity str

Returns: bool

Method SQLiteEpisodeBackend.review_status

Verify storage availability before reporting this backend's transactional review state.

SQLite commits each review and its derived state atomically; it has no durable replay queue. A closed or failed connection raises instead of advertising a synchronized store.

review_status(namespace_filter: dict[str, Any]) -> dict[str, int | str]

Parameter Type Default Description
namespace_filter dict[str, Any]

Returns: dict[str, int | str]

Method SQLiteEpisodeBackend.vector_status

Report whether a scope exceeded the measured exact-vector ceiling.

vector_status(namespace_filter: dict[str, Any]) -> dict[str, int | str]

Parameter Type Default Description
namespace_filter dict[str, Any] Scope dimensions applied before counting candidates.

Returns: dict[str, int | str] — Status, scoped candidate count, and configured vector ceiling.

Method SQLiteEpisodeBackend.search_text

Search scoped text through FTS5 or a safely escaped LIKE fallback.

search_text(query: str, namespace_filter: dict[str, Any], top_k: int = 5) -> list[EpisodeSearchResult]

Parameter Type Default Description
query str Literal search text.
namespace_filter dict[str, Any] Scope dimensions applied before limiting results.
top_k int 5 Maximum number of lexical results.

Returns: list[EpisodeSearchResult] — Text-ranked episodes. These are never substituted for vector results.

Method SQLiteEpisodeBackend.get_recent

Return scoped recent episodes, applying filters before the limit.

get_recent(namespace_filter: dict[str, Any], limit: int = 10, since: datetime | None = None) -> list[EpisodicMemory]

Parameter Type Default Description
namespace_filter dict[str, Any]
limit int 10
since datetime | None None

Returns: list[EpisodicMemory]

Method SQLiteEpisodeBackend.get_by_ids

Return the requested episodes that lie inside a scope, newest first.

An unknown id and an id outside the scope are both simply absent, so a caller cannot tell one from the other.

get_by_ids(episode_ids: Sequence[str], namespace_filter: dict[str, Any]) -> list[EpisodicMemory]

Parameter Type Default Description
episode_ids Sequence[str] Episode ids to read. Duplicates are read once.
namespace_filter dict[str, Any] Scope dimensions every returned episode must match.

Returns: list[EpisodicMemory] — The matching episodes, ordered like :meth:get_recent.

Method SQLiteEpisodeBackend.get_forgotten_derivations

Return forgotten episodes whose derived brain page is not removed yet, oldest first.

An episode qualifies when its metadata has forgotten true, a consolidated_into page and no forgotten_page_removed flag (FEAT-889 R7).

get_forgotten_derivations(namespace_filter: dict[str, Any], limit: int = 500) -> list[EpisodicMemory]

Parameter Type Default Description
namespace_filter dict[str, Any] Scope dimensions every returned episode must match.
limit int 500 Maximum number of episodes to return.

Returns: list[EpisodicMemory] — The matching episodes.

Method SQLiteEpisodeBackend.get_recent_matching

Return scoped episodes newest first, selecting by category, failure and page in SQL.

get_recent_matching(namespace_filter: dict[str, Any], *, limit: int | None = None, categories: Sequence[str] | None = None, failures_only: bool = False, before_id: str | None = None) -> list[EpisodicMemory]

Parameter Type Default Description
namespace_filter dict[str, Any] Scope dimensions applied before every other predicate.
limit int | None None Maximum number of rows, or None for every matching row.
categories Sequence[str] | None None Category values to keep; None keeps every category.
failures_only bool False Whether to keep only failure episodes.
before_id str | None None Episode id of the last row of the previous page. Only rows ordered after it are returned; if that episode no longer exists the page is empty.

Returns: list[EpisodicMemory] — The matching episodes, ordered like :meth:get_recent.

Raises:

  • ValueError — If limit is negative.

Method SQLiteEpisodeBackend.get_failures

Preserve baseline failure retrieval without bypassing scoped store admission.

get_failures(agent_id: str, tenant_id: str = 'default', limit: int = 5) -> list[EpisodicMemory]

Parameter Type Default Description
agent_id str
tenant_id str 'default'
limit int 5

Returns: list[EpisodicMemory]

Method SQLiteEpisodeBackend.delete_expired

Prune permitted episode data without destroying imported feedback history.

delete_expired() -> int

Returns: int

Method SQLiteEpisodeBackend.count

Count only records in the supplied scope.

count(namespace_filter: dict[str, Any]) -> int

Parameter Type Default Description
namespace_filter dict[str, Any]

Returns: int

Method SQLiteEpisodeBackend.update_metadata

Preserve baseline metadata merge semantics and transactional persistence.

update_metadata(episode_ids: list[str], patch: dict[str, Any]) -> int

Parameter Type Default Description
episode_ids list[str]
patch dict[str, Any]

Returns: int

Attribute logger

Alias transition

Re-exported from parrot.memory.dynamics.fsrs.transition.

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally