Skip to content

feat(protocol): implement O(1) event routing with Strategy Pattern#63

Merged
Otoru merged 1 commit intomainfrom
feature/protocol-o1-routing
Feb 7, 2026
Merged

feat(protocol): implement O(1) event routing with Strategy Pattern#63
Otoru merged 1 commit intomainfrom
feature/protocol-o1-routing

Conversation

@Otoru
Copy link
Owner

@Otoru Otoru commented Jan 31, 2026

Overview

This PR implements O(1) event routing using the Strategy Pattern, directly addressing the scalability concerns raised in #59.

Scalability Impact

Problem Addressed

As discussed in #59, the previous implementation used an Observer Pattern where every channel listens to the global event stream, resulting in O(N) overhead:

  • With 50 active channels, every event triggers iteration through all 50 listeners
  • 49 channels wake up, check if event.uuid != self.uuid, and sleep again
  • With 100 parallel calls: 100 completion events = 10,000 filtering checks (100 × 100)

Solution

Replaced O(N) broadcasting with O(1) hash-based routing:

  • Global handlers: Direct dictionary lookup by event name
  • Channel handlers: Two-level lookup (channel UUID → event name)
  • Composite routing: Combines both strategies efficiently

Performance: 100 parallel calls now trigger exactly 100 dispatch actions instead of 10,000 checks.

Implementation Details

Architecture

Refactored genesis/protocol into a structured subpackage:

genesis/protocol/
├── __init__.py          # Main Protocol class
├── base.py              # Core protocol logic
├── parser.py            # Event parsing
├── metrics.py           # Prometheus metrics
├── telemetry.py         # OpenTelemetry tracing
└── routing/             # Strategy Pattern implementation
    ├── base.py          # BaseRouter abstract class
    ├── global_.py       # GlobalRouter for global handlers
    ├── channel.py       # ChannelRouter for channel-specific handlers
    ├── composite.py     # CompositeRouter combining both
    └── dispatcher.py    # Event dispatching logic

Strategy Pattern

Implemented the Strategy Pattern for flexible routing:

  • BaseRouter: Abstract interface for all routing strategies
  • GlobalRouter: Handles global event subscriptions
  • ChannelRouter: Manages channel-specific handlers
  • CompositeRouter: Orchestrates both strategies

Performance Characteristics

Operation Before After
Event routing O(n) O(1)
Handler registration O(1) O(1)
Handler removal O(n) O(1)

Where n = number of registered handlers

Related

@Otoru Otoru force-pushed the feature/protocol-o1-routing branch 2 times, most recently from e3558d2 to ecb15ac Compare February 7, 2026 13:41
- Refactor protocol into subpackage; add routing strategies (Channel, Global, Composite)
- Channel.from_session: register CHANNEL_STATE via register_channel_handler for O(1)
- Session._awaitable_complete_command: register CHANNEL_EXECUTE_COMPLETE and
  CHANNEL_HANGUP_COMPLETE per channel when session has uuid
- test_outbound_blocking_command: broadcast with session.uuid so event is routed O(1)
- Centralize EventHandler in genesis/types.py; add routing metrics and docs

Co-authored-by: Cursor <cursoragent@cursor.com>
@Otoru Otoru force-pushed the feature/protocol-o1-routing branch from ecb15ac to bc8cf89 Compare February 7, 2026 13:42
@Otoru Otoru merged commit 9a7e7e1 into main Feb 7, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments