accumulate_managed_agents_event in src/anthropic/lib/sessions/_accumulate.py folds one streaming preview event into a BetaManagedAgentsAgentMessageEvent snapshot, always returning a fresh object — the accumulated argument is never mutated.[1] AccumulatedEvent is a type alias for BetaManagedAgentsAgentMessageEvent — the only event type this accumulator tracks.[1]
On an event_start for an agent.message, accumulate_managed_agents_event returns a new snapshot with empty content and processed_at set to the Unix epoch (1970-01-01T00:00:00Z) as a placeholder; the buffered final event later replaces this with the real server timestamp.[1] When an agent.message (buffered final event) is received, accumulate_managed_agents_event returns a deep copy of that event, replacing whatever the preview had accumulated — the buffered final event is canonical.[1]
An event_delta received before its event_start — i.e., when accumulated is None — causes accumulate_managed_agents_event to raise AnthropicError.[1] An event_delta whose index exceeds the current content length also raises AnthropicError, indicating deltas arrived out of order or were mis-routed.[1] The exhaustiveness guard uses assert_never inside if TYPE_CHECKING blocks, so type checkers receive exhaustive-union validation at lint time with zero runtime overhead.[1] When accumulate_managed_agents_event encounters an unrecognized event type, it silently ignores that event rather than raising an error, leaving the accumulated state unchanged — making the accumulator forward-compatible with new server-side event variants.
Sources