# communist-engineer/saaf-os architecture

> Architecture diagram of the GitHub repository communist-engineer/saaf-os, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/communist-engineer/saaf-os
- Repository: https://github.com/communist-engineer/saaf-os

## Overview

##### Project type and purpose
- SAAF-OS (Self-Adaptive Agent Framework) is an open-source, Python-based AI agent platform.
- It implements a microkernel/event-driven architecture: a central message bus (LangGraph style) orchestrates “modules” (planners, memory, meta-reasoners, world models, audit/RSI engines, contradiction detectors, etc.).
- Key capabilities: dynamic DAG workflow mutation at runtime, tool discovery/integration, vector memory and KG overlay, peer-to-peer delegation via JSON-RPC/A2A.

##### File structure
- Top-level:
- bus/adapter.py & message_bus_spec.md → the central message bus/interface layer.
- base_agent.py → core agent process entrypoints.
- modules/… → plug-in modules grouped by functionality (contradiction, memory, planner, meta, world_model, rsi, uls).
- models/ → distilled planner ML model.
- losses/ → custom loss functions (e.g., contradiction_loss).
- simulation/ → scenario loader, backends, harness for offline testing.
- scripts/ → CLI entrypoints (run_demo, run_tests, stress_scenarios, view_memory).
- docs (.md files) → architectural design, specs (message bus, memory, planning, recursive self-improvement, polycentric governance, etc.).
- tests/ → unit tests validating each component and scenario.
- governance_rules.toml → governance constraints.
- class_ontology.jsonld, value_vector_schema.json → semantic/embedding specs.

##### Extract key architecture insights from README
- Uses LangGraph patterns: modules communicate via a directed message bus.
- Vector store (Chroma) + optional knowledge graph for memory.
- Google A2A protocol + JSON-RPC fallback for agent-to-agent.
- Python packaging conventions for extensibility (cookiecutter, setuptools).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Core Infrastructure
    subgraph "Core Infrastructure"
        direction TB
        BaseAgent["BaseAgent"]:::core
        MessageBus["Message Bus / Orchestrator"]:::core
    end

    %% Computation Modules
    subgraph "Computation Modules"
        direction TB
        Planner["Planner Module"]:::module
        MetaReasoner["Meta-Reasoner Module"]:::module
        WorldModel["Forward World Model Module"]:::module
        Memory["Memory Module"]:::module
        ULS["ULS Encoder Module"]:::module
        RSI["RSI Engine & Audit Module"]:::module
        Contradiction["Contradiction Detection Module"]:::module
        ToolDiscovery["Tool Discovery Subsystem"]:::module
    end

    %% Data Persistence
    subgraph "Data Stores"
        direction TB
        VectorDB["Vector DB (Chroma)"]:::datastore
        KGStore["Knowledge Graph Store"]:::datastore
        FileSnapshots["File-based Snapshots"]:::datastore
    end

    %% External Interfaces
    subgraph "External Interfaces"
        direction TB
        ExternalAPIs["External APIs"]:::external
        PeerAgentInterface["Peer Agent Interface (JSON-RPC/A2A)"]:::external
    end

    %% Simulation & Testing
    subgraph "Simulation & Testing"
        direction TB
        SimulationLoader["Simulation Loader & Backends"]:::sim
        Scripts["Scripts & Test Harness"]:::sim
        Tests["Unit Tests"]:::sim
    end

    %% Relationships
    BaseAgent -->|initializes| MessageBus
    MessageBus -->|registers handlers| Planner
    MessageBus -->|registers handlers| MetaReasoner
    MessageBus -->|registers handlers| WorldModel
    MessageBus -->|registers handlers| Memory
    MessageBus -->|registers handlers| ULS
    MessageBus -->|registers handlers| RSI
    MessageBus -->|registers handlers| Contradiction
    MessageBus -->|registers handlers| ToolDiscovery

    Planner -->|queries| Memory
    Planner -->|queries| WorldModel
    Planner -->|emits planning tasks| MessageBus
    MetaReasoner -->|inspects workflow| MessageBus
    MetaReasoner -->|patches DAG| MessageBus
    WorldModel -->|reads embeddings| Memory
    Memory -->|persists| VectorDB
    Memory -->|persists| KGStore
    Memory -->|writes| FileSnapshots
    ULS -->|encodes states| MessageBus
    Contradiction -->|scores plans| MessageBus
    Contradiction -->|uses| lossesContradiction
    RSI -->|logs actions| MessageBus
    RSI -->|rollbacks via| FileSnapshots
    ToolDiscovery -->|calls| ExternalAPIs
    Planner -->|makes tool calls| ToolDiscovery
    PeerAgentInterface -->|communicates via| MessageBus

    SimulationLoader -->|uses| MessageBus
    Scripts -->|drives| SimulationLoader
    Tests -->|validate| SimulationLoader
    Tests -->|validate| Planner
    Tests -->|validate| MetaReasoner
    Tests -->|validate| WorldModel
    Tests -->|validate| Memory
    Tests -->|validate| ULS
    Tests -->|validate| RSI
    Tests -->|validate| Contradiction

    %% External protocols and annotations
    MessageBus -.->|A2A / JSON-RPC| PeerAgentInterface
    Planner -.->|tool discovery| ExternalAPIs

    %% Click Events
    click BaseAgent "https://github.com/communist-engineer/saaf-os/blob/main/base_agent.py"
    click MessageBus "https://github.com/communist-engineer/saaf-os/blob/main/bus/adapter.py"
    click Planner "https://github.com/communist-engineer/saaf-os/blob/main/modules/planner.py"
    click Planner "https://github.com/communist-engineer/saaf-os/blob/main/modules/planning/rl_planner.py"
    click Planner "https://github.com/communist-engineer/saaf-os/blob/main/modules/planning/plan_arbitrator.py"
    click MetaReasoner "https://github.com/communist-engineer/saaf-os/blob/main/modules/meta/meta_reasoner.py"
    click WorldModel "https://github.com/communist-engineer/saaf-os/blob/main/modules/fwm.py"
    click WorldModel "https://github.com/communist-engineer/saaf-os/blob/main/modules/world_model/fwm.py"
    click Memory "https://github.com/communist-engineer/saaf-os/blob/main/modules/memory/manager.py"
    click Memory "https://github.com/communist-engineer/saaf-os/blob/main/modules/memory/logger.py"
    click Memory "https://github.com/communist-engineer/saaf-os/blob/main/modules/memory/retrieval.py"
    click FileSnapshots "https://github.com/communist-engineer/saaf-os/blob/main/memory/episodes.jsonl"
    click ULS "https://github.com/communist-engineer/saaf-os/blob/main/modules/uls/encoder.py"
    click ULS "https://github.com/communist-engineer/saaf-os/blob/main/modules/uls_encoder.py"
    click RSI "https://github.com/communist-engineer/saaf-os/blob/main/modules/rsi/engine.py"
    click RSI "https://github.com/communist-engineer/saaf-os/blob/main/modules/rsi/audit_log.py"
    click RSI "https://github.com/communist-engineer/saaf-os/blob/main/modules/rsi/crypto_utils.py"
    click Contradiction "https://github.com/communist-engineer/saaf-os/blob/main/modules/contradiction/engine.py"
    click Contradiction "https://github.com/communist-engineer/saaf-os/blob/main/losses/contradiction_loss.py"
    click SimulationLoader "https://github.com/communist-engineer/saaf-os/blob/main/simulation/loader.py"
    click SimulationLoader "https://github.com/communist-engineer/saaf-os/blob/main/simulation_backends.md"
    click Scripts "https://github.com/communist-engineer/saaf-os/blob/main/scripts/run_demo.py"
    click Scripts "https://github.com/communist-engineer/saaf-os/blob/main/scripts/run_tests.py"
    click Scripts "https://github.com/communist-engineer/saaf-os/blob/main/scripts/stress_scenarios.py"
    click Scripts "https://github.com/communist-engineer/saaf-os/blob/main/scripts/view_memory.py"
    click Tests "https://github.com/communist-engineer/saaf-os/tree/main/tests/"
    click KGStore "https://github.com/communist-engineer/saaf-os/blob/main/class_ontology.jsonld"
    click KGStore "https://github.com/communist-engineer/saaf-os/blob/main/value_vector_schema.json"
    click KGStore "https://github.com/communist-engineer/saaf-os/blob/main/latent_encoding_spec.md"
    click governance "https://github.com/communist-engineer/saaf-os/blob/main/governance_rules.toml"
    click governance "https://github.com/communist-engineer/saaf-os/blob/main/polycentric_governance_planner.md"

    %% Styles
    classDef core fill:#FFD700,stroke:#333,stroke-width:1px
    classDef module fill:#87CEEB,stroke:#333,stroke-width:1px
    classDef datastore fill:#98FB98,stroke:#333,stroke-width:1px
    classDef external fill:#FFB6C1,stroke:#333,stroke-width:1px
    classDef sim fill:#D3D3D3,stroke:#333,stroke-width:1px
```
