# all-hands-ai/openhands architecture

> Architecture diagram of the GitHub repository all-hands-ai/openhands, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/all-hands-ai/openhands
- Repository: https://github.com/all-hands-ai/openhands

## Overview

##### Project Type & Purpose
- OpenHands is a full‑stack AI‑driven software development platform. It includes:
- A Python core (CLI, controller, agents, runtime, server, storage, events, etc.)
- A TypeScript/React frontend served by the Python server
- Dockerized runtimes for sandboxed code execution

##### Focus: openhands/events Subsystem
The `openhands/events` package implements a domain‑specific event system for:
- Capturing every “action” an AI agent takes (e.g., run bash, browse web, file edits)
- Recording every “observation” the agent receives (e.g., command output, file contents)
- Serializing/deserializing events for storage, replay, and streaming
- Feeding events into the memory and analysis layers

##### Main Components (to include
A. Event Model
- `events/event.py` – base `Event` class (timestamp, metadata)
- `events/action/*.py` – `Action` subclasses (agent, browse, commands, files, mcp, message)
- `events/observation/*.py` – `Observation` subclasses (agent, browse, commands, empty, error, files, mcp, reject, success)

B. Event Storage & Streaming
- `events/event_store.py` – synchronous in‐memory event store API (append, query)
- `events/async_event_store_wrapper.py` – async façade for non‐blocking writes/reads
- `events/stream.py` – optional real‐time stream interface for subscribers

C. Serialization Layer
- `events/serialization/event.py` – JSON (de)serialization of the base event envelope
- `events/serialization/action.py` – (de)serialize `Action` objects
- `events/serialization/observation.py` – (de)serialize `Observation` objects
- `events/serialization/utils.py` – shared helpers (e.g., mapping class ↔ type string)

D. Tooling & Utilities
- `events/tool.py` – definitions of external tooling invoked by events
- `events/utils.py` – common utilities (e.g., event validation, type guards)

##### Relationships & Data Flow
1) Agent Controller (in `openhands/controller/agent_controller.py`)
– invokes an `Action` (e.g., “run bash”)
– constructs `Action` event object
2) Event Dispatcher
– passes the `Action` to `event_store.append(action)`
– `async_event_store_wrapper` may enqueue this call
– subscribers (memory condenser, replay engine) receive the action via `stream`
3) Runtime or external tool executes the action
– captures output, errors → constructs corresponding `Observation`
– again, `event_store.append(observation)`
4) Serializer
– when persisting to disk or sending over HTTP, each event goes through `serialization/*`
– JSON payload stored in conversation log or a database
5) Consumers
– Memory condenser pipeline (`openhands/memory`) reads event stream to build context
– Replayer / testing harness uses `stream` to replay events for debugging
– Monitoring / logging layers subscribe to event stream for metrics & audits

##### Architectural Patterns & Principles
- Event Sourcing: every state change (action/observation) is an immutable event
- Publish‑Subscribe: real-time `stream` allows loose coupling between producers & consumers
- Serialization Abstraction: separate modules for action vs. observation keep (de)serialization clean
- Async Wrapping: non‑blocking I/O via `async_event_store_wrapper` for high throughput

## Diagram (Mermaid)

```mermaid
graph TB
    %% Layers top to bottom
    
    subgraph "Controller/Agents"
        AC["Agent Controller"]:::util
    end

    subgraph "Event Model"
        direction TB
        EB["Event (Base)"]:::core
        ASubs["Action subclasses"]:::core
        OSubs["Observation subclasses"]:::core
    end

    subgraph "Event Store & Stream"
        direction TB
        AEW["Async Event Store Wrapper"]:::storage
        ES["Event Store (in-memory/disk)"]:::storage
        ST["Stream"]:::storage
    end

    subgraph "Serialization / Persistence"
        direction TB
        ENV["Envelope Serializer"]:::ser
        SAct["Action Serializer"]:::ser
        SObs["Observation Serializer"]:::ser
        SUtils["Serialization Utils"]:::util
    end

    subgraph "Consumers"
        direction TB
        MEM["Memory Condenser"]:::util
        RPL["Replay Engine"]:::util
        MON["Monitoring / Logging"]:::util
    end

    %% Flow
    AC -->|"append(Action)"| AEW
    AEW -->|"writes"| ES
    ES -->|"publishes"| ST
    ST -->|"/subscribe"| MEM
    ST -->|"/subscribe"| RPL
    ST -->|"/subscribe"| MON

    %% Observation flow
    ST -->|executes Action| OSubs
    OSubs -->|"append(Observation)"| AEW

    %% Serialization flow
    ES -->|"serialize"| ENV
    ENV --> SAct
    ENV --> SObs
    SAct --> SUtils
    SObs --> SUtils

    %% Click Events
    click AC "https://github.com/all-hands-ai/openhands/blob/main/openhands/controller/agent_controller.py"
    click EB "https://github.com/all-hands-ai/openhands/blob/main/openhands/events/event.py"
    click ASubs "https://github.com/all-hands-ai/openhands/tree/main/openhands/events/action/"
    click OSubs "https://github.com/all-hands-ai/openhands/tree/main/openhands/events/observation/"
    click ES "https://github.com/all-hands-ai/openhands/blob/main/openhands/events/event_store.py"
    click AEW "https://github.com/all-hands-ai/openhands/blob/main/openhands/events/async_event_store_wrapper.py"
    click ST "https://github.com/all-hands-ai/openhands/blob/main/openhands/events/stream.py"
    click ENV "https://github.com/all-hands-ai/openhands/blob/main/openhands/events/serialization/event.py"
    click SAct "https://github.com/all-hands-ai/openhands/blob/main/openhands/events/serialization/action.py"
    click SObs "https://github.com/all-hands-ai/openhands/blob/main/openhands/events/serialization/observation.py"
    click SUtils "https://github.com/all-hands-ai/openhands/blob/main/openhands/events/serialization/utils.py"

    %% Styles
    classDef core fill:#cce5ff,stroke:#004085,color:#004085
    classDef storage fill:#d4edda,stroke:#155724,color:#155724
    classDef ser fill:#fff3cd,stroke:#856404,color:#856404
    classDef util fill:#e2e3e5,stroke:#6c757d,color:#6c757d
```
