Skip to content

API Reference parrot.storage.turn_bus

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

parrot.storage.turn_bus

View source

Application-level turn bus: Redis Streams + durable outbox + dead letters (FEAT-886 M10).

Builds the dedicated bus that carries conversation.turn.completed to ChatStorage, subscribes the storage handlers and tears everything down. Mirrors observability/fanout.py but is a SEPARATE bus and is not publish-only: it consumes.

Alias BusTurnPublisher

Re-exported from parrot.memory.persistence.BusTurnPublisher.

Alias CONVERSATION_DELETED_TOPIC

Re-exported from parrot.memory.persistence.CONVERSATION_DELETED_TOPIC.

Alias ChatStorage

Re-exported from parrot.storage.chat.ChatStorage.

Attribute RECOVERY_BATCH_SIZE

Class SqliteDeadLetterStore

Local, durable dead-letter store for retry-exhausted envelopes (spec §8 Q15).

SqliteOutbox keeps no dead letters, and DLQHandler without a DSN persists nothing, so envelopes whose handler exhausted its retries are kept here until replayed. One file per host.

Parameter Type Default Description
path Path

Method SqliteDeadLetterStore.add

Persist (or re-open) a dead letter; a replayed envelope that fails again becomes pending again.

add(envelope: EventEnvelope, *, attempts: int, error: BaseException, subscriber_id: str) -> None

Parameter Type Default Description
envelope EventEnvelope
attempts int
error BaseException
subscriber_id str

Method SqliteDeadLetterStore.pending

Un-replayed envelopes (optionally failed_at >= since), oldest first.

pending(since: Optional[datetime] = None) -> List[EventEnvelope]

Parameter Type Default Description
since Optional[datetime] None

Returns: List[EventEnvelope]

Method SqliteDeadLetterStore.mark_replayed

Mark a dead letter as replayed (kept for audit, not deleted).

mark_replayed(event_id: str) -> None

Parameter Type Default Description
event_id str

Method SqliteDeadLetterStore.close

No persistent connection is held; present for handle symmetry.

close() -> None

Alias TURN_COMPLETED_TOPIC

Re-exported from parrot.memory.persistence.TURN_COMPLETED_TOPIC.

Class TurnPersistence

Handle returned by :func:setup_turn_persistence.

Parameter Type Default Description
bus Any
publisher BusTurnPublisher
subscription_ids List[str]
dlq Optional[Any] None

Fields:

Field Type Default Description
bus Any The turn EventBus.
publisher BusTurnPublisher The BusTurnPublisher bots publish through.
subscription_ids List[str] bus.core subscription ids (turn + deletion handlers).
dlq Optional[Any] None A DLQHandler (Postgres) or a SqliteDeadLetterStore; None for an injected bus.

Class TurnPersistenceNotConfigured

Neither PARROT_TURN_BUS_REDIS_URL nor PARROT_TURN_BUS_OUTBOX_DIR is set: turn persistence is disabled.

Alias conf

Re-exported from parrot.conf.

Attribute logger

Function recover_abandoned_outboxes

Drain the outboxes of dead workers into bus (a replacement pod has a new hostname and PID).

Every <any-hostname>-<pid>.db in directory is a candidate, except this process's own file (<own hostname>-<own pid>.db, skipped by exact name: PIDs repeat across hosts) and dead-letter files. Each candidate is opened through SqliteOutbox, which takes the exclusive non-blocking flock: a file whose owner is still alive raises OutboxLockedError and is skipped, so two relays never share one file. This relies on flock working across hosts on the shared volume (Linux NFSv4/EFS emulates it with POSIX locks); if it did not, the worst case is a duplicate delivery, which the bus dedup and the upsert-by-turn-id handlers already tolerate. EVERY pending row is re-published through bus.core.publish (including rows still in retry backoff; the bus dedups on event_id and the handlers upsert by turn id, so a crash between publish and ack only duplicates a delivery that is already idempotent) and acknowledged in the old file only after its publish succeeded. A file is removed only when it holds no pending row; a failing or corrupt file is kept, logged, and the remaining files are still drained.

recover_abandoned_outboxes(bus: Any, directory: Path, *, own_pid: Optional[int] = None) -> int

Parameter Type Default Description
bus Any
directory Path
own_pid Optional[int] None

Returns: int — The number of envelopes re-published.

Function replay_dead_letters

Replay dead letters through the same handlers; store_turn re-checks tombstones.

replay_dead_letters(handle: TurnPersistence, *, since: Optional[datetime] = None) -> int

Parameter Type Default Description
handle TurnPersistence
since Optional[datetime] None

Returns: int — Number of envelopes re-published.

Raises:

  • RuntimeError — The handle has no DLQ (an injected bus).

Function setup_turn_persistence

Build (or accept) the turn bus and subscribe ChatStorage.

setup_turn_persistence(chat_storage: ChatStorage, *, bus: Any = None) -> TurnPersistence

Parameter Type Default Description
chat_storage ChatStorage The initialised ChatStorage.
bus Any None An existing EventBus (tests / hosts that own their bus); its DLQ is then the caller's.

Returns: TurnPersistence — class:TurnPersistence handle.

Raises:

  • TurnPersistenceNotConfigured — Neither turn-persistence setting is configured.
  • RuntimeError — Redis URL or outbox directory is unset / unusable (bus not built).

Function shutdown_turn_persistence

Unsubscribe and close the bus, preserving the outbox. Safe to call twice.

shutdown_turn_persistence(handle: TurnPersistence) -> None

Parameter Type Default Description
handle TurnPersistence

Function turn_dedup_prefix

Return the dedup key prefix for stream_prefix, outside the <stream_prefix>* namespace.

RedisStreamsBackend discovers its streams with SCAN <stream_prefix>* and runs XGROUP CREATE on every match, so a dedup key (a plain string) stored under the stream prefix fails the consumer with WRONGTYPE.

turn_dedup_prefix(stream_prefix: str) -> str

Parameter Type Default Description
stream_prefix str The stream key prefix, e.g. "parrot:turns:".

Returns: str — A sibling prefix that no <stream_prefix>* pattern matches, e.g. "parrot:turns-dedup:".

Function turn_persistence_configured

Return whether either turn-persistence setting is configured.

turn_persistence_configured() -> bool

Returns: bool

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