Skip to content

API Reference parrot.integrations.liveavatar.broadcast.registry

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.integrations.liveavatar.broadcast.registry

View source

Broadcast registry contract and in-memory reference implementation (FEAT-537).

Implements spec §2 "Ownership, admission and cleanup" and "Moderation and exclusive speaking floor" as a storage-agnostic contract, plus the in-memory implementation that defines the reference semantics :class:RedisBroadcastRegistry (TASK-2953) must reproduce and that every deterministic unit test runs against.

The registry owns state and arbitration only. It never holds PCM, tokens, AWS credentials, LiveAvatar access tokens or live SDK objects, and it never talks to LiveKit — removing a participant from a room is the caller's job, and the registry deliberately reports an expiry rather than releasing a seat behind the caller's back (fail closed; see :meth:BroadcastRegistry.expire).

Three invariants hold at every await point:

  • pending + active + tombstoned reservations never exceed max_viewers;
  • there is exactly one moderator_lease_id while any lease remains;
  • there is at most one speaker_lease_id, and it is None for the whole duration of a floor switch.

Class Admission

Outcome of a seat reservation.

Unpacks as the (lease, is_first) tuple the caller needs: is_first is True for exactly one admission per broadcast — the one that elected the moderator — and tells the caller to run claim_owner plus producer startup once.

Attributes:

Attribute Type Description
lease ViewerLease The reserved :class:ViewerLease.
is_first bool Whether this admission elected the moderator.

Class BroadcastRegistry

Storage-agnostic broadcast state, admission and moderation contract.

Implementations must be safe to call concurrently from multiple tasks and, for the Redis implementation, from multiple worker processes. Every mutating method takes an optional now so a distributed implementation can pass authoritative server time and tests can inject a fake clock.

Method BroadcastRegistry.create

Persist a new pending broadcast.

create(descriptor: BroadcastDescriptor) -> BroadcastDescriptor

Parameter Type Default Description
descriptor BroadcastDescriptor Fully-formed pending descriptor.

Returns: BroadcastDescriptor — The stored descriptor.

Raises:

  • BroadcastError — If the broadcast id already exists.

Method BroadcastRegistry.get

Return a descriptor, or None when unknown or out of tenant scope.

get(tenant_id: str, broadcast_id: str) -> Optional[BroadcastDescriptor]

Parameter Type Default Description
tenant_id str
broadcast_id str

Returns: Optional[BroadcastDescriptor]

Method BroadcastRegistry.transition

Move the broadcast to new_state, fenced by the ownership epoch.

transition(tenant_id: str, broadcast_id: str, new_state: BroadcastState, *, output_epoch: Optional[int] = None, reason: Optional[BroadcastReason] = None, expected_owner_epoch: int) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str Tenant scope.
broadcast_id str Broadcast to transition.
new_state BroadcastState Target state. audio_only → avatar is not legal.
output_epoch Optional[int] None New monotonic output generation, when cutting over.
reason Optional[BroadcastReason] None Sanitized reason to record.
expected_owner_epoch int Ownership generation the caller believes it holds; a mismatch means it was fenced.

Returns: BroadcastDescriptor — The updated descriptor.

Raises:

  • NotOwner — If the caller's ownership epoch is stale.
  • BroadcastTerminal — If the broadcast already ended or failed.
  • ValueError — If the edge is not in the legal state graph.

Method BroadcastRegistry.claim_owner

Atomically claim producer ownership.

claim_owner(tenant_id: str, broadcast_id: str, worker_id: str, *, now: Optional[float] = None) -> tuple[bool, int]

Parameter Type Default Description
tenant_id str
broadcast_id str
worker_id str
now Optional[float] None

Returns: tuple[bool, int] — (claimed, owner_epoch). claimed is False when a live owner already holds the lease; owner_epoch is then that owner's epoch, so the caller can fence itself.

Method BroadcastRegistry.renew_owner

Extend an ownership lease.

renew_owner(tenant_id: str, broadcast_id: str, worker_id: str, owner_epoch: int, *, now: Optional[float] = None) -> bool

Parameter Type Default Description
tenant_id str
broadcast_id str
worker_id str
owner_epoch int
now Optional[float] None

Returns: bool — False when the caller has been fenced — it must stop publishing immediately rather than wait for its own lease to expire.

Method BroadcastRegistry.reserve_viewer

Atomically reserve one of the ten receiving seats.

The first successful reservation — and only the first — sets moderator_lease_id, sets the initial speaker_lease_id to the same lease, moves the floor to granted at epoch 1, and reports is_first=True so the caller starts the producer exactly once.

reserve_viewer(tenant_id: str, broadcast_id: str, principal: ParticipantPrincipal, livekit_identity: str, *, now: Optional[float] = None) -> Admission

Parameter Type Default Description
tenant_id str
broadcast_id str
principal ParticipantPrincipal
livekit_identity str
now Optional[float] None

Returns: Admission

Raises:

  • ViewerLimitReached — If seats plus retained reservations are full.
  • IdentityTombstoned — If that LiveKit identity recently departed.
  • BroadcastTerminal — If the broadcast ended or failed.

Method BroadcastRegistry.confirm_viewer

Mark a lease as an active, confirmed room participant.

confirm_viewer(tenant_id: str, broadcast_id: str, lease_id: str) -> ViewerLease

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str

Returns: ViewerLease

Method BroadcastRegistry.heartbeat_control

Record a control-socket heartbeat. Does not bump the version.

heartbeat_control(tenant_id: str, broadcast_id: str, lease_id: str, *, now: Optional[float] = None) -> None

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str
now Optional[float] None

Method BroadcastRegistry.release_viewer

Release a seat idempotently and report the moderation consequences.

release_viewer(tenant_id: str, broadcast_id: str, lease_id: str, *, now: Optional[float] = None) -> ReleaseOutcome

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str
now Optional[float] None

Returns: ReleaseOutcome

Method BroadcastRegistry.list_leases

Return every lease currently holding a seat, in admission order.

list_leases(tenant_id: str, broadcast_id: str) -> List[ViewerLease]

Parameter Type Default Description
tenant_id str
broadcast_id str

Returns: List[ViewerLease]

Method BroadcastRegistry.raise_hand

Record an idempotent raise-hand request. Grants no permission.

raise_hand(tenant_id: str, broadcast_id: str, lease_id: str, *, now: Optional[float] = None) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str
now Optional[float] None

Returns: BroadcastDescriptor

Method BroadcastRegistry.cancel_hand

Withdraw the caller's own hand request. Idempotent.

cancel_hand(tenant_id: str, broadcast_id: str, lease_id: str) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str

Returns: BroadcastDescriptor

Method BroadcastRegistry.dismiss_hand

Moderator-only dismissal of another participant's request.

dismiss_hand(tenant_id: str, broadcast_id: str, moderator_lease_id: str, target_lease_id: str) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
moderator_lease_id str
target_lease_id str

Returns: BroadcastDescriptor

Raises:

  • NotModerator — If the caller is not the current moderator.

Method BroadcastRegistry.grant_floor

Open the handoff barrier towards target_lease_id.

Clears the permitted speaker, increments floor_epoch and enters switching atomically. target_lease_id=None means revoke — the floor returns to the moderator once the barrier is acknowledged.

grant_floor(tenant_id: str, broadcast_id: str, moderator_lease_id: str, target_lease_id: Optional[str], expected_version: int) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
moderator_lease_id str
target_lease_id Optional[str]
expected_version int

Returns: BroadcastDescriptor

Raises:

  • NotModerator — If the caller is not the current moderator.
  • StaleVersion — On a compare-and-set conflict, including an already-switching floor.
  • FloorNotGranted — If the target is not an active admitted lease.

Method BroadcastRegistry.commit_floor

Acknowledge the barrier and install the new speaker.

commit_floor(tenant_id: str, broadcast_id: str, target_lease_id: str, floor_epoch: int) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
target_lease_id str
floor_epoch int

Returns: BroadcastDescriptor

Raises:

  • StaleFloorEpoch — If the floor is not switching at floor_epoch.
  • FloorNotGranted — If the target is not the pending target or has since departed.

Method BroadcastRegistry.abort_floor

Abandon a failed or timed-out barrier, leaving the floor idle.

abort_floor(tenant_id: str, broadcast_id: str, floor_epoch: int) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
floor_epoch int

Returns: BroadcastDescriptor

Method BroadcastRegistry.release_floor

Speaker-initiated "Finish Speaking" — barrier back to the moderator.

release_floor(tenant_id: str, broadcast_id: str, speaker_lease_id: str) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
speaker_lease_id str

Returns: BroadcastDescriptor

Raises:

  • NotSpeaker — If the caller does not currently hold the floor.

Method BroadcastRegistry.revoke_floor

Moderator revoke — :meth:grant_floor with a None target.

revoke_floor(tenant_id: str, broadcast_id: str, moderator_lease_id: str, expected_version: int) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
moderator_lease_id str
expected_version int

Returns: BroadcastDescriptor

Method BroadcastRegistry.elect_moderator

Elect the earliest remaining healthy participant as moderator.

elect_moderator(tenant_id: str, broadcast_id: str, *, now: Optional[float] = None) -> Optional[str]

Parameter Type Default Description
tenant_id str
broadcast_id str
now Optional[float] None

Returns: Optional[str] — The elected lease id, or None when nobody is eligible.

Method BroadcastRegistry.bind_speaker_socket

Bind the single microphone socket permitted to send audio.

bind_speaker_socket(tenant_id: str, broadcast_id: str, lease_id: str, socket_id: str, floor_epoch: int) -> bool

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str
socket_id str
floor_epoch int

Returns: bool

Raises:

  • FloorNotGranted — If the lease does not hold the floor.
  • StaleFloorEpoch — If the epoch is superseded.
  • SpeakerConnectionExists — If a different socket is already bound.

Method BroadcastRegistry.unbind_speaker_socket

Drop a microphone-socket binding. Idempotent.

unbind_speaker_socket(tenant_id: str, broadcast_id: str, lease_id: str, socket_id: str) -> bool

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str
socket_id str

Returns: bool

Method BroadcastRegistry.request_stop

Record the durable desired terminal state. Moderator only.

request_stop(tenant_id: str, broadcast_id: str, by_lease_id: str) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
by_lease_id str

Returns: BroadcastDescriptor

Raises:

  • NotModerator — If the caller is not the current moderator.

Method BroadcastRegistry.stop_requested

Whether a stop has been requested. Polled by the owner every second.

stop_requested(tenant_id: str, broadcast_id: str) -> bool

Parameter Type Default Description
tenant_id str
broadcast_id str

Returns: bool

Method BroadcastRegistry.set_media_state

Persist producer-owned media facts onto the durable descriptor.

The publisher identities decide which track a browser plays. They were previously known only to the process running the producer, so any other worker projected selected_identity=None and its viewers could not tell which publisher to attach — the descriptor had the fields, nothing ever filled them in.

set_media_state(tenant_id: str, broadcast_id: str, *, room_name: Optional[str] = None, avatar_identity: Optional[str] = None, direct_identity: Optional[str] = None, liveavatar_session_id: Optional[str] = None) -> Optional[BroadcastDescriptor]

Parameter Type Default Description
tenant_id str Tenant scope.
broadcast_id str Broadcast concerned.
room_name Optional[str] None LiveKit room backing the broadcast.
avatar_identity Optional[str] None Avatar publisher identity.
direct_identity Optional[str] None Direct audio publisher identity.
liveavatar_session_id Optional[str] None Vendor session id, when one exists.

Returns: Optional[BroadcastDescriptor] — The updated descriptor, or None when it is gone.

Method BroadcastRegistry.list_broadcasts

Every live broadcast in the store, as (tenant_id, broadcast_id).

Reconciliation needs this to be store-wide, not process-local: the whole point of fencing a dead owner is that some other worker — one that may never have served this broadcast — notices it and takes over. A reconciler that only walks what its own process happened to serve cannot, by construction, recover a broadcast whose only worker died.

list_broadcasts() -> List[Tuple[str, str]]

Returns: List[Tuple[str, str]] — Live broadcasts, in unspecified order.

Method BroadcastRegistry.expire

Run one reconciliation pass and report what expired.

Deliberately conservative: an expired control participant is marked leaving and reported, not released. The caller must remove it from the LiveKit room first and then call :meth:release_viewer, so an unreachable participant can never be replaced by an over-admitted one.

expire(tenant_id: str, broadcast_id: str, *, now: Optional[float] = None) -> List[ExpiryEvent]

Parameter Type Default Description
tenant_id str
broadcast_id str
now Optional[float] None

Returns: List[ExpiryEvent]

Class ExpiryEvent

One thing a reconciliation pass expired.

Parameter Type Default Description
kind ExpiryKind
lease_id Optional[str] None
detail str ''

Fields:

Field Type Default Description
kind ExpiryKind What expired.
lease_id Optional[str] None Lease concerned, for lease-scoped kinds.
detail str '' Operator-facing detail; never returned to a client.

Class ExpiryKind

What a reconciliation pass found expired.

Class InMemoryBroadcastRegistry

Single-process reference implementation of :class:BroadcastRegistry.

Serialises every mutation of one broadcast behind a per-broadcast :class:asyncio.Lock, which is what makes the concurrent-admission and conflicting-grant races deterministic. This is the semantics the Redis implementation reproduces with atomic scripts.

Parameter Type Default Description
clock Optional[Callable[[], float]] None Callable returning epoch seconds. Injected so TTL behaviour is testable without sleeping.

Method InMemoryBroadcastRegistry.create

create(descriptor: BroadcastDescriptor) -> BroadcastDescriptor

Parameter Type Default Description
descriptor BroadcastDescriptor

Returns: BroadcastDescriptor

Method InMemoryBroadcastRegistry.get

get(tenant_id: str, broadcast_id: str) -> Optional[BroadcastDescriptor]

Parameter Type Default Description
tenant_id str
broadcast_id str

Returns: Optional[BroadcastDescriptor]

Method InMemoryBroadcastRegistry.transition

transition(tenant_id: str, broadcast_id: str, new_state: BroadcastState, *, output_epoch: Optional[int] = None, reason: Optional[BroadcastReason] = None, expected_owner_epoch: int) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
new_state BroadcastState
output_epoch Optional[int] None
reason Optional[BroadcastReason] None
expected_owner_epoch int

Returns: BroadcastDescriptor

Method InMemoryBroadcastRegistry.claim_owner

claim_owner(tenant_id: str, broadcast_id: str, worker_id: str, *, now: Optional[float] = None) -> tuple[bool, int]

Parameter Type Default Description
tenant_id str
broadcast_id str
worker_id str
now Optional[float] None

Returns: tuple[bool, int]

Method InMemoryBroadcastRegistry.renew_owner

renew_owner(tenant_id: str, broadcast_id: str, worker_id: str, owner_epoch: int, *, now: Optional[float] = None) -> bool

Parameter Type Default Description
tenant_id str
broadcast_id str
worker_id str
owner_epoch int
now Optional[float] None

Returns: bool

Method InMemoryBroadcastRegistry.reserve_viewer

reserve_viewer(tenant_id: str, broadcast_id: str, principal: ParticipantPrincipal, livekit_identity: str, *, now: Optional[float] = None) -> Admission

Parameter Type Default Description
tenant_id str
broadcast_id str
principal ParticipantPrincipal
livekit_identity str
now Optional[float] None

Returns: Admission

Method InMemoryBroadcastRegistry.confirm_viewer

confirm_viewer(tenant_id: str, broadcast_id: str, lease_id: str) -> ViewerLease

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str

Returns: ViewerLease

Method InMemoryBroadcastRegistry.heartbeat_control

heartbeat_control(tenant_id: str, broadcast_id: str, lease_id: str, *, now: Optional[float] = None) -> None

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str
now Optional[float] None

Method InMemoryBroadcastRegistry.release_viewer

release_viewer(tenant_id: str, broadcast_id: str, lease_id: str, *, now: Optional[float] = None) -> ReleaseOutcome

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str
now Optional[float] None

Returns: ReleaseOutcome

Method InMemoryBroadcastRegistry.list_leases

list_leases(tenant_id: str, broadcast_id: str) -> List[ViewerLease]

Parameter Type Default Description
tenant_id str
broadcast_id str

Returns: List[ViewerLease]

Method InMemoryBroadcastRegistry.raise_hand

raise_hand(tenant_id: str, broadcast_id: str, lease_id: str, *, now: Optional[float] = None) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str
now Optional[float] None

Returns: BroadcastDescriptor

Method InMemoryBroadcastRegistry.cancel_hand

cancel_hand(tenant_id: str, broadcast_id: str, lease_id: str) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str

Returns: BroadcastDescriptor

Method InMemoryBroadcastRegistry.dismiss_hand

dismiss_hand(tenant_id: str, broadcast_id: str, moderator_lease_id: str, target_lease_id: str) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
moderator_lease_id str
target_lease_id str

Returns: BroadcastDescriptor

Method InMemoryBroadcastRegistry.grant_floor

grant_floor(tenant_id: str, broadcast_id: str, moderator_lease_id: str, target_lease_id: Optional[str], expected_version: int) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
moderator_lease_id str
target_lease_id Optional[str]
expected_version int

Returns: BroadcastDescriptor

Method InMemoryBroadcastRegistry.commit_floor

commit_floor(tenant_id: str, broadcast_id: str, target_lease_id: str, floor_epoch: int) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
target_lease_id str
floor_epoch int

Returns: BroadcastDescriptor

Method InMemoryBroadcastRegistry.abort_floor

abort_floor(tenant_id: str, broadcast_id: str, floor_epoch: int) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
floor_epoch int

Returns: BroadcastDescriptor

Method InMemoryBroadcastRegistry.release_floor

release_floor(tenant_id: str, broadcast_id: str, speaker_lease_id: str) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
speaker_lease_id str

Returns: BroadcastDescriptor

Method InMemoryBroadcastRegistry.revoke_floor

revoke_floor(tenant_id: str, broadcast_id: str, moderator_lease_id: str, expected_version: int) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
moderator_lease_id str
expected_version int

Returns: BroadcastDescriptor

Method InMemoryBroadcastRegistry.elect_moderator

elect_moderator(tenant_id: str, broadcast_id: str, *, now: Optional[float] = None) -> Optional[str]

Parameter Type Default Description
tenant_id str
broadcast_id str
now Optional[float] None

Returns: Optional[str]

Method InMemoryBroadcastRegistry.bind_speaker_socket

bind_speaker_socket(tenant_id: str, broadcast_id: str, lease_id: str, socket_id: str, floor_epoch: int) -> bool

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str
socket_id str
floor_epoch int

Returns: bool

Method InMemoryBroadcastRegistry.unbind_speaker_socket

unbind_speaker_socket(tenant_id: str, broadcast_id: str, lease_id: str, socket_id: str) -> bool

Parameter Type Default Description
tenant_id str
broadcast_id str
lease_id str
socket_id str

Returns: bool

Method InMemoryBroadcastRegistry.request_stop

request_stop(tenant_id: str, broadcast_id: str, by_lease_id: str) -> BroadcastDescriptor

Parameter Type Default Description
tenant_id str
broadcast_id str
by_lease_id str

Returns: BroadcastDescriptor

Method InMemoryBroadcastRegistry.stop_requested

stop_requested(tenant_id: str, broadcast_id: str) -> bool

Parameter Type Default Description
tenant_id str
broadcast_id str

Returns: bool

Method InMemoryBroadcastRegistry.set_media_state

set_media_state(tenant_id: str, broadcast_id: str, *, room_name: Optional[str] = None, avatar_identity: Optional[str] = None, direct_identity: Optional[str] = None, liveavatar_session_id: Optional[str] = None) -> Optional[BroadcastDescriptor]

Parameter Type Default Description
tenant_id str
broadcast_id str
room_name Optional[str] None
avatar_identity Optional[str] None
direct_identity Optional[str] None
liveavatar_session_id Optional[str] None

Returns: Optional[BroadcastDescriptor]

Method InMemoryBroadcastRegistry.list_broadcasts

list_broadcasts() -> List[Tuple[str, str]]

Returns: List[Tuple[str, str]]

Method InMemoryBroadcastRegistry.expire

expire(tenant_id: str, broadcast_id: str, *, now: Optional[float] = None) -> List[ExpiryEvent]

Parameter Type Default Description
tenant_id str
broadcast_id str
now Optional[float] None

Returns: List[ExpiryEvent]

Class ReleaseOutcome

Consequences of releasing one viewer seat.

Parameter Type Default Description
audience_empty bool
floor_returned_to Optional[str] None
new_moderator Optional[str] None

Fields:

Field Type Default Description
audience_empty bool Whether no participants remain, so the caller must tear the producer down.
floor_returned_to Optional[str] None Lease the floor barrier now targets, if the departure moved the floor. The caller must acknowledge with commit_floor.
new_moderator Optional[str] None Lease elected as moderator, if the moderator departed.

Function validate_audio_authority

Reject microphone input that is unauthorized, stale or from a stray socket.

Called at both ingress and the producer, immediately before queueing PCM (spec §2: "Validate authority at both ingress and the producer just before queueing PCM"). Pure and synchronous so the producer can call it per frame without touching the store.

Socket possession, moderator role, or a valid LiveKit viewer JWT alone never authorize input — only the fencing tuple below does.

validate_audio_authority(descriptor: BroadcastDescriptor, lease_id: str, floor_epoch: int, socket_id: str, *, bound_socket_id: Optional[str] = None) -> None

Parameter Type Default Description
descriptor BroadcastDescriptor Current broadcast descriptor.
lease_id str Lease the frame claims to come from.
floor_epoch int Floor epoch the frame was produced under.
socket_id str Microphone socket the frame arrived on.
bound_socket_id Optional[str] None The socket currently bound to the floor, when known.

Raises:

  • FloorNotGranted — If no floor is granted, or it is granted to someone else.
  • StaleFloorEpoch — If the frame belongs to a superseded floor epoch.
  • SpeakerConnectionExists — If a different socket holds the binding.

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