EventStream Class

A multi-cast event stream.

Four data-flow methods: emit, close, subscribe, last_cursor. Lifecycle (create / lookup / destroy) is the registry's job (streams); the Protocol intentionally does NOT include a destructive method.

See docs/streaming-guide.md for the developer guide.

Constructor

EventStream(*args, **kwargs)

Methods

close

Transition the stream from active to closed. Idempotent.

On an already-closed or destroyed stream, this is a no-op (never raises). Subscribers attached at close time drain any remaining queued items, then their iterators terminate cleanly with StopAsyncIteration.

emit

Emit a payload to all currently-attached subscribers.

last_cursor

Return the highest cursor seen so far, or None.

Semantics:

  • While the stream is active: the highest cursor value persisted so far, or None if zero emits OR the active backing has no cursor support.

  • After the stream is closed: the last cursor the backing ever saw, even if those events have since been evicted by per-event TTL. last_cursor() is a read-only watermark query and does not itself fire the close → destroy auto-transition. This is load-bearing for the file-backed replay rehydration path (handler reads last_cursor() on entry to pick the next cursor).

  • After the stream is destroyed (auto-transition has fired): raises EventStreamNotFoundError.

last_cursor() is the emitter's recovery primitive. It is NOT a workflow-recovery primitive — workflow watermarks (what work is done) belong in an application-owned FoundryStateStore, batched per side-effecting operation. See docs/streaming-guide.md for the state-vs-cursor antipattern note.

subscribe

Return an async iterator over emitted payloads.

NOT a coroutine: call without await and immediately use with async for / aiter() / anext().

close

Transition the stream from active to closed. Idempotent.

On an already-closed or destroyed stream, this is a no-op (never raises). Subscribers attached at close time drain any remaining queued items, then their iterators terminate cleanly with StopAsyncIteration.

async close() -> None

emit

Emit a payload to all currently-attached subscribers.

async emit(payload: Any, *, close: bool = False) -> None

Parameters

Name Description
payload
Required
Any

Opaque value. The framework never inspects, validates, or rewrites it.

Keyword-Only Parameters

Name Description
close

If True, the emit and the close-of-stream are observably atomic: every subscriber attached before this call returns sees BOTH the payload AND the end-of-stream signal; subscribers attached after see neither.

Default value: False

Exceptions

Type Description

If the stream has already been closed.

If the stream has been destroyed.

last_cursor

Return the highest cursor seen so far, or None.

Semantics:

  • While the stream is active: the highest cursor value persisted so far, or None if zero emits OR the active backing has no cursor support.

  • After the stream is closed: the last cursor the backing ever saw, even if those events have since been evicted by per-event TTL. last_cursor() is a read-only watermark query and does not itself fire the close → destroy auto-transition. This is load-bearing for the file-backed replay rehydration path (handler reads last_cursor() on entry to pick the next cursor).

  • After the stream is destroyed (auto-transition has fired): raises EventStreamNotFoundError.

last_cursor() is the emitter's recovery primitive. It is NOT a workflow-recovery primitive — workflow watermarks (what work is done) belong in an application-owned FoundryStateStore, batched per side-effecting operation. See docs/streaming-guide.md for the state-vs-cursor antipattern note.

async last_cursor() -> int | None

subscribe

Return an async iterator over emitted payloads.

NOT a coroutine: call without await and immediately use with async for / aiter() / anext().

subscribe(*, after: int | None = None) -> AsyncIterator[Any]

Keyword-Only Parameters

Name Description
after

If supplied and the active backing supports cursored replay, yield only payloads whose cursor value is strictly greater than after. Backings without cursor support silently ignore non-None values.

Default value: None

Exceptions

Type Description

Raised synchronously at the call site (before the iterator is returned) if the stream has been destroyed.