# gparab/absolute-self-governance architecture

> Architecture diagram of the GitHub repository gparab/absolute-self-governance, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gparab/absolute-self-governance
- Repository: https://github.com/gparab/absolute-self-governance

## Overview

#### Purpose and Entry Points
Absolute Self-Governance (ASG) is a Python 3.11+ agent-orchestration package that sizes role-based software-agent swarms, reaches roster consensus, and coordinates phase handoffs. Primary entry points are the packaged CLI in `src/self_governance/cli.py`, local watcher/dev runtime in `src/self_governance/nudger.py` and `src/self_governance/devserver.py`, GitHub webhook service in `src/self_governance/github_app.py`, and stdio MCP server in `src/self_governance/mcp_server.py`.

#### Core Orchestration Lifecycle
A filesystem bus connects external IDE/agent runners to ASG. `ContinuousNudger` watches a workspace `handoff.md`; a completed handoff triggers candidate extraction, consensus, swarm sizing, and generated next-phase prompt/configuration output. Dry-run approval can pause this transition before execution.
- `src/self_governance/nudger.py`
- `src/self_governance/consensus.py`
- `src/self_governance/dimensioning.py`

#### Consensus and Swarm Sizing
TETD consensus iteratively approves a successor roster, adjusting temperature and approval thresholds to avoid deadlock. Dimensioning converts a requirement vector through a transition matrix into per-role counts. Its lazy agent representation allows large theoretical swarms without eagerly allocating every agent.
- `src/self_governance/consensus.py`
- `src/self_governance/dimensioning.py`
- `src/self_governance/models.py`

#### Agent Execution and Provider Boundary
Adapters separate orchestration from LLM-backed execution. The Gemini adapter supports structured file-edit responses and sandbox/path safeguards; provider routing can select Gemini or OpenRouter-compatible models based on credentials. Persona and capability injection supply role-specific instructions from the catalog and ability definitions.
- `src/self_governance/base_adapter.py`
- `src/self_governance/gemini_adapter.py`
- `src/self_governance/providers.py`
- `src/self_governance/agency_agents_adapter.py`

#### Governance, Safety, and Learning
Policy rules, injection defense, anti-drift controls, and security checks constrain commands, paths, budgets, rate limits, and sandbox routing. Feedback/learning modules adapt swarm behavior, while memory modules retain procedural, graph, and extracted-fact context across work.
- `src/self_governance/policy.py`
- `src/self_governance/policy_rules/`
- `src/self_governance/injection_defense.py`
- `src/self_governance/learning.py`
- `src/self_governance/graph_memory.py`

#### Service and Persistence Plane
The GitHub-facing FastAPI application exposes authenticated webhook/status operations for multi-tenant use. SQLAlchemy persistence supports SQLite or PostgreSQL; Alembic owns schema migration. Auth, billing, and alerts provide tenant protection, token/cost accounting, and operational escalation.
- `src/self_governance/github_app.py`
- `src/self_governance/db.py`
- `src/self_governance/auth.py`
- `src/self_governance/billing.py`
- `alembic/versions/d6c85050648f_initial_schema.py`

#### Operations and Deployment
Local dev mode provides a localhost dashboard and Prometheus metrics. Telemetry supports structured correlation/logging and OpenTelemetry OTLP or console tracing. Production packaging includes a non-root Docker image and Kubernetes webhook deployment reference; PostgreSQL, GitHub webhooks/HMAC secrets, Gemini/OpenRouter, Prometheus, and OTLP are material external integrations.
- `src/self_governance/dashboard.py`
- `src/self_governance/metrics.py`
- `src/self_governance/tracing.py`
- `Dockerfile`
- `k8s-webhook.yaml`

Main entry points: [`src/self_governance/nudger.py`](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/nudger.py) (Continuous Nudger), [`src/self_governance/github_app.py`](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/github_app.py) (GitHub Webhook API), [`src/self_governance/base_adapter.py`](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/base_adapter.py) (Agent Adapter Layer), [`src/self_governance/providers.py`](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/providers.py) (Provider Router)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry Points"]
  node_cli["Packaged CLI<br/>Python CLI<br/>[cli.py]"]
  node_mcp["MCP Server<br/>stdio server<br/>[mcp_server.py]"]
end

subgraph group_orchestration["Orchestration Core"]
  node_nudger["Continuous Nudger<br/>workspace watcher<br/>[nudger.py]"]
  node_consensus["TETD Consensus<br/>roster approval<br/>[consensus.py]"]
  node_dimensioning["Swarm Dimensioning<br/>role sizing<br/>[dimensioning.py]"]
  node_models["Lazy Agent Models<br/>domain models<br/>[models.py]"]
  node_learning_memory["Learning &amp; Graph Memory<br/>adaptive context<br/>[learning.py]"]
end

subgraph group_execution["Agent Execution &amp; Safety"]
  node_agent_adapter["Agent Adapter Layer<br/>execution boundary<br/>[base_adapter.py]"]
  node_providers["Provider Router<br/>LLM routing<br/>[providers.py]"]
  node_gemini["Gemini Adapter<br/>LLM adapter<br/>[gemini_adapter.py]"]
  node_personas["Persona &amp; Capabilities<br/>role injection"]
  node_governance["Governance Guardrails<br/>policy and safety<br/>[policy.py]"]
end

subgraph group_service["Service &amp; Persistence"]
  node_github_app["GitHub Webhook API<br/>FastAPI service<br/>[github_app.py]"]
  node_database[("SQLAlchemy Persistence<br/>database access<br/>[db.py]")]
  node_auth_billing["Auth &amp; Billing<br/>tenant controls<br/>[auth.py]"]
end

subgraph group_operations["Operations"]
  node_dashboard_metrics["Dashboard &amp; Metrics<br/>local observability<br/>[dashboard.py]"]
  node_tracing["Telemetry Tracing<br/>observability<br/>[tracing.py]"]
end

node_external_llms(("LLM Providers<br/>external integration"))

node_cli -->|"runs local lifecycle"| node_nudger
node_mcp -->|"invokes orchestration"| node_nudger
node_github_app -->|"webhook-triggered work"| node_nudger
node_nudger -->|"extracts candidates"| node_consensus
node_consensus -->|"approved roster"| node_dimensioning
node_dimensioning -->|"creates lazy swarm"| node_models
node_nudger -->|"generates next-phase execution"| node_agent_adapter
node_agent_adapter -->|"routes model calls"| node_providers
node_providers -->|"Gemini selection"| node_gemini
node_providers -->|"provider requests"| node_external_llms
node_personas -->|"role instructions"| node_agent_adapter
node_governance -->|"enforces safety"| node_agent_adapter
node_nudger -->|"reads and updates context"| node_learning_memory
node_github_app -->|"authenticates and accounts"| node_auth_billing
node_github_app -->|"persists tenant state"| node_database
node_auth_billing -->|"stores account data"| node_database
node_nudger -->|"publishes local status"| node_dashboard_metrics
node_github_app -->|"emits service telemetry"| node_tracing
node_nudger -->|"emits lifecycle telemetry"| node_tracing

click node_cli "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/cli.py"
click node_nudger "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/nudger.py"
click node_github_app "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/github_app.py"
click node_mcp "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/mcp_server.py"
click node_consensus "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/consensus.py"
click node_dimensioning "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/dimensioning.py"
click node_models "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/models.py"
click node_agent_adapter "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/base_adapter.py"
click node_providers "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/providers.py"
click node_gemini "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/gemini_adapter.py"
click node_personas "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/agency_agents_adapter.py"
click node_governance "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/policy.py"
click node_learning_memory "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/learning.py"
click node_database "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/db.py"
click node_auth_billing "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/auth.py"
click node_dashboard_metrics "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/dashboard.py"
click node_tracing "https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/tracing.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_mcp toneBlue
class node_nudger,node_consensus,node_dimensioning,node_models,node_learning_memory toneAmber
class node_agent_adapter,node_providers,node_gemini,node_personas,node_governance toneMint
class node_github_app,node_database,node_auth_billing toneRose
class node_dashboard_metrics,node_tracing toneIndigo
class node_external_llms toneNeutral
```

## Components

### Entry Points

- [Packaged CLI](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/cli.py): Python CLI · `src/self_governance/cli.py`
- [MCP Server](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/mcp_server.py): stdio server · `src/self_governance/mcp_server.py`

### Orchestration Core

- [Continuous Nudger](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/nudger.py): workspace watcher · Watches handoff.md and coordinates phase transitions. · `src/self_governance/nudger.py`
- [TETD Consensus](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/consensus.py): roster approval · Iteratively approves successor rosters while avoiding deadlock. · `src/self_governance/consensus.py`
- [Swarm Dimensioning](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/dimensioning.py): role sizing · Maps requirements to per-role swarm counts. · `src/self_governance/dimensioning.py`
- [Lazy Agent Models](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/models.py): domain models · Represents large theoretical swarms without eager allocation. · `src/self_governance/models.py`
- [Learning & Graph Memory](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/learning.py): adaptive context · Retains procedural, graph, and extracted-fact context. · `src/self_governance/learning.py`

### Agent Execution & Safety

- [Agent Adapter Layer](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/base_adapter.py): execution boundary · `src/self_governance/base_adapter.py`
- [Provider Router](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/providers.py): LLM routing · Selects Gemini or OpenRouter-compatible providers by credentials. · `src/self_governance/providers.py`
- [Gemini Adapter](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/gemini_adapter.py): LLM adapter · Handles structured file edits and sandbox safeguards. · `src/self_governance/gemini_adapter.py`
- [Persona & Capabilities](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/agency_agents_adapter.py): role injection · `src/self_governance/agency_agents_adapter.py`
- [Governance Guardrails](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/policy.py): policy and safety · Constrains commands, paths, budgets, rates, and prompt injection. · `src/self_governance/policy.py`

### Service & Persistence

- [GitHub Webhook API](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/github_app.py): FastAPI service · Authenticated multi-tenant webhook and status service. · `src/self_governance/github_app.py`
- [SQLAlchemy Persistence](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/db.py): database access · Supports SQLite and PostgreSQL tenant state. · `src/self_governance/db.py`
- [Auth & Billing](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/auth.py): tenant controls · Protects tenants and tracks tokens and cost. · `src/self_governance/auth.py`

### Operations

- [Dashboard & Metrics](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/dashboard.py): local observability · Provides localhost dashboard and Prometheus metrics. · `src/self_governance/dashboard.py`
- [Telemetry Tracing](https://github.com/gparab/absolute-self-governance/blob/master/src/self_governance/tracing.py): observability · Emits correlated logs and OTLP or console traces. · `src/self_governance/tracing.py`

### Other components

- **LLM Providers**: external integration · Gemini and OpenRouter-compatible models.

## Connections

- Packaged CLI → Continuous Nudger: runs local lifecycle
- MCP Server → Continuous Nudger: invokes orchestration
- GitHub Webhook API → Continuous Nudger: webhook-triggered work
- Continuous Nudger → TETD Consensus: extracts candidates
- TETD Consensus → Swarm Dimensioning: approved roster
- Swarm Dimensioning → Lazy Agent Models: creates lazy swarm
- Continuous Nudger → Agent Adapter Layer: generates next-phase execution
- Agent Adapter Layer → Provider Router: routes model calls
- Provider Router → Gemini Adapter: Gemini selection
- Provider Router → LLM Providers: provider requests
- Persona & Capabilities → Agent Adapter Layer: role instructions
- Governance Guardrails → Agent Adapter Layer: enforces safety
- Continuous Nudger → Learning & Graph Memory: reads and updates context
- GitHub Webhook API → Auth & Billing: authenticates and accounts
- GitHub Webhook API → SQLAlchemy Persistence: persists tenant state
- Auth & Billing → SQLAlchemy Persistence: stores account data
- Continuous Nudger → Dashboard & Metrics: publishes local status
- GitHub Webhook API → Telemetry Tracing: emits service telemetry
- Continuous Nudger → Telemetry Tracing: emits lifecycle telemetry
