# hkuds/openspace architecture

> Architecture diagram of the GitHub repository hkuds/openspace, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hkuds/openspace
- Repository: https://github.com/hkuds/openspace

## Overview

#### Purpose and Entry Points
OpenSpace is a local-first skill-management and execution layer for AI agents: it runs tasks, retrieves local/cloud skills, records outcomes, and conditionally evolves skills. Python is the primary runtime; public processes include CLI, MCP server, dashboard API, gateway, and embedded Python API.
- `openspace/application.py`
- `openspace/entrypoints/cli/main.py`
- `openspace/entrypoints/mcp/server.py`
- `openspace/entrypoints/dashboard/server.py`

#### Shared Execution Runtime
All invocation surfaces converge on a shared runtime that owns requests, sessions/workspaces, event publication, scheduling, turn execution, and finalization. This is the main control boundary: UI/host adapters submit execution requests rather than implementing agent logic themselves.
- `openspace/runtime/app.py`
- `openspace/runtime/execution_scheduler.py`
- `openspace/runtime/turn_runner.py`
- `openspace/runtime/session_runtime.py`

#### Agent and Turn Loop
The agent layer builds context, injects/retrieves skills, calls the configured LLM, interprets tool turns, and applies stop/compaction/session policies. Coordinator and multi-agent modules support delegated/background work. Conversation history, tool results, and task state feed subsequent turns and recoverable sessions.
- `openspace/agents/grounding_agent.py`
- `openspace/agents/turns/loop.py`
- `openspace/agents/turns/model_call_controller.py`
- `openspace/agents/coordinator.py`

#### Tool Grounding, Security, and Permissions
A unified grounding client abstracts local and remote tools. Providers cover shell, MCP, GUI/computer use, and web; tool execution is gated by permission engines, shell/path validation, and optional platform sandboxing. This is the safety boundary between model-directed actions and host resources.
- `openspace/grounding/core/grounding_client.py`
- `openspace/grounding/core/tool_executor.py`
- `openspace/grounding/core/permissions/engine.py`
- `openspace/services/sandbox/manager.py`

#### Skill Registry, Retrieval, and Persistent Evidence
Skills are discovered from configured directories and represented with stable IDs, safety checks, local metadata, lineage, and quality records. Hybrid ranking selects relevant skills; runtime/tool/session evidence is assembled into packets used for quality decisions and later evolution. Local persistence is SQLite-oriented; project state conventionally lives under `.openspace`.
- `openspace/skill_engine/registry.py`
- `openspace/skill_engine/skill_ranker.py`
- `openspace/skill_engine/store.py`
- `openspace/skill_engine/evidence/packet_builder.py`

#### Controlled Skill Evolution
Post-run analysis detects signals and proposes FIX, DERIVED, or CAPTURED changes. Admission, authoring, validation, behavioral evaluation, and auditing constrain writes; candidates may remain audit-only, and trust/enablement are separate lifecycle controls. Successful evolved skills re-enter the registry and retrieval flow.
- `openspace/skill_engine/evolution/engine.py`
- `openspace/skill_engine/evolution/admission.py`
- `openspace/skill_engine/evolution/validator.py`
- `openspace/skill_engine/evolver.py`

#### External Boundaries and Clients
LLM access is centralized behind a LiteLLM-oriented client. Cloud services provide authenticated skill upload/download/search, embeddings, quality/trace telemetry, and redaction; cloud discovery is distinct from local execution/import. MCP supports stdio and network transports; communication gateway adapters connect Feishu and WhatsApp, with the latter including a Node bridge.
- `openspace/llm/client.py`
- `openspace/cloud/client.py`
- `openspace/grounding/backends/mcp/client.py`
- `openspace/communication/gateway_runtime.py`

#### User Interfaces
The TypeScript Ink TUI communicates with Python through an NDJSON bridge/protocol and renders streamed runtime events, permissions, transcripts, and background tasks. The separate React/Vite dashboard consumes the dashboard API to browse skill classes, lineage, workflow records, and evolution details.
- `apps/tui/src/index.tsx`
- `apps/tui/src/bridge/protocol.ts`
- `apps/dashboard/src/main.tsx`
- `apps/dashboard/src/api/client.ts`

Main entry points: [`openspace/runtime/app.py`](https://github.com/hkuds/openspace/blob/main/openspace/runtime/app.py) (Runtime App), [`openspace/agents/turns/loop.py`](https://github.com/hkuds/openspace/blob/main/openspace/agents/turns/loop.py) (Grounding Agent Loop), [`openspace/runtime/turn_runner.py`](https://github.com/hkuds/openspace/blob/main/openspace/runtime/turn_runner.py) (Turn Runner), [`openspace/grounding/core/grounding_client.py`](https://github.com/hkuds/openspace/blob/main/openspace/grounding/core/grounding_client.py) (Grounding Client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["Invocation &amp; UI"]
  node_embedded_api["Embedded Python API<br/>[application.py]"]
  node_host_adapters["CLI, MCP &amp; Dashboard API<br/>entrypoints<br/>[main.py]"]
  node_tui_bridge["Ink TUI Bridge<br/>ndjson protocol<br/>[protocol.ts]"]
  node_dashboard_ui["React Dashboard<br/>web client<br/>[main.tsx]"]
end

subgraph group_runtime["Execution Runtime"]
  node_runtime_app["Runtime App<br/>execution control<br/>[app.py]"]
  node_scheduler["Execution Scheduler"]
  node_turn_runner["Turn Runner<br/>turn execution<br/>[turn_runner.py]"]
  node_session_runtime[("Session Runtime<br/>session state<br/>[session_runtime.py]")]
  node_agent_loop["Grounding Agent Loop<br/>[loop.py]"]
  node_coordinator["Agent Coordinator<br/>delegation<br/>[coordinator.py]"]
  node_grounding["Grounding Client<br/>tool gateway"]
  node_permissions{{"Permissions &amp; Sandbox<br/>safety boundary<br/>[engine.py]"}}
end

subgraph group_skills["Skill Lifecycle"]
  node_registry[("Skill Registry &amp; Store<br/>local skill store<br/>[registry.py]")]
  node_ranker["Hybrid Skill Ranker<br/>retrieval<br/>[skill_ranker.py]"]
  node_evidence["Evidence Packets<br/>run evidence<br/>[packet_builder.py]"]
  node_evolution["Controlled Evolution<br/>skill governance<br/>[engine.py]"]
end

subgraph group_external["External Boundaries"]
  node_llm["LLM Client<br/>model client<br/>[client.py]"]
  node_cloud["Cloud Services<br/>cloud client<br/>[client.py]"]
  node_communication["Communication Gateway<br/>channel adapters<br/>[gateway_runtime.py]"]
end

node_embedded_api -->|"submits requests"| node_runtime_app
node_host_adapters -->|"submits requests"| node_runtime_app
node_tui_bridge -->|"NDJSON requests/events"| node_host_adapters
node_dashboard_ui -->|"dashboard API"| node_host_adapters
node_communication -->|"channel tasks"| node_runtime_app
node_runtime_app -->|"schedules"| node_scheduler
node_runtime_app -->|"owns sessions"| node_session_runtime
node_scheduler -->|"dispatches turns"| node_turn_runner
node_turn_runner -->|"runs"| node_agent_loop
node_turn_runner -->|"reads and records state"| node_session_runtime
node_agent_loop -->|"retrieves skills"| node_ranker
node_ranker -->|"ranks local skills"| node_registry
node_agent_loop -->|"model calls"| node_llm
node_agent_loop -->|"tool turns"| node_grounding
node_agent_loop -->|"delegates work"| node_coordinator
node_grounding -->|"authorizes actions"| node_permissions
node_grounding -->|"tool outcomes"| node_evidence
node_session_runtime -->|"run context"| node_evidence
node_evidence -->|"quality signals"| node_evolution
node_evolution -->|"validated skills"| node_registry
node_registry -->|"upload, download &amp; search"| node_cloud
node_cloud -.->|"cloud discovery"| node_ranker
node_runtime_app -->|"streamed events"| node_tui_bridge

click node_embedded_api "https://github.com/hkuds/openspace/blob/main/openspace/application.py"
click node_host_adapters "https://github.com/hkuds/openspace/blob/main/openspace/entrypoints/cli/main.py"
click node_tui_bridge "https://github.com/hkuds/openspace/blob/main/apps/tui/src/bridge/protocol.ts"
click node_dashboard_ui "https://github.com/hkuds/openspace/blob/main/apps/dashboard/src/main.tsx"
click node_runtime_app "https://github.com/hkuds/openspace/blob/main/openspace/runtime/app.py"
click node_scheduler "https://github.com/hkuds/openspace/blob/main/openspace/runtime/execution_scheduler.py"
click node_turn_runner "https://github.com/hkuds/openspace/blob/main/openspace/runtime/turn_runner.py"
click node_session_runtime "https://github.com/hkuds/openspace/blob/main/openspace/runtime/session_runtime.py"
click node_agent_loop "https://github.com/hkuds/openspace/blob/main/openspace/agents/turns/loop.py"
click node_coordinator "https://github.com/hkuds/openspace/blob/main/openspace/agents/coordinator.py"
click node_grounding "https://github.com/hkuds/openspace/blob/main/openspace/grounding/core/grounding_client.py"
click node_permissions "https://github.com/hkuds/openspace/blob/main/openspace/grounding/core/permissions/engine.py"
click node_registry "https://github.com/hkuds/openspace/blob/main/openspace/skill_engine/registry.py"
click node_ranker "https://github.com/hkuds/openspace/blob/main/openspace/skill_engine/skill_ranker.py"
click node_evidence "https://github.com/hkuds/openspace/blob/main/openspace/skill_engine/evidence/packet_builder.py"
click node_evolution "https://github.com/hkuds/openspace/blob/main/openspace/skill_engine/evolution/engine.py"
click node_llm "https://github.com/hkuds/openspace/blob/main/openspace/llm/client.py"
click node_cloud "https://github.com/hkuds/openspace/blob/main/openspace/cloud/client.py"
click node_communication "https://github.com/hkuds/openspace/blob/main/openspace/communication/gateway_runtime.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_embedded_api,node_host_adapters,node_tui_bridge,node_dashboard_ui toneBlue
class node_runtime_app,node_scheduler,node_turn_runner,node_session_runtime,node_agent_loop,node_coordinator,node_grounding,node_permissions toneAmber
class node_registry,node_ranker,node_evidence,node_evolution toneMint
class node_llm,node_cloud,node_communication toneRose
```

## Components

### Invocation & UI

- [Embedded Python API](https://github.com/hkuds/openspace/blob/main/openspace/application.py): api · `openspace/application.py`
- [CLI, MCP & Dashboard API](https://github.com/hkuds/openspace/blob/main/openspace/entrypoints/cli/main.py): entrypoints · Host-facing adapters submit requests to the shared runtime. · `openspace/entrypoints/cli/main.py`
- [Ink TUI Bridge](https://github.com/hkuds/openspace/blob/main/apps/tui/src/bridge/protocol.ts): ndjson protocol · Streams runtime events and permission interactions to the TypeScript TUI. · `apps/tui/src/bridge/protocol.ts`
- [React Dashboard](https://github.com/hkuds/openspace/blob/main/apps/dashboard/src/main.tsx): web client · `apps/dashboard/src/main.tsx`

### Execution Runtime

- [Runtime App](https://github.com/hkuds/openspace/blob/main/openspace/runtime/app.py): execution control · Shared request, workspace, event, and finalization boundary. · `openspace/runtime/app.py`
- [Execution Scheduler](https://github.com/hkuds/openspace/blob/main/openspace/runtime/execution_scheduler.py): scheduler · `openspace/runtime/execution_scheduler.py`
- [Turn Runner](https://github.com/hkuds/openspace/blob/main/openspace/runtime/turn_runner.py): turn execution · `openspace/runtime/turn_runner.py`
- [Session Runtime](https://github.com/hkuds/openspace/blob/main/openspace/runtime/session_runtime.py): session state · Maintains recoverable conversations, task state, and tool results. · `openspace/runtime/session_runtime.py`
- [Grounding Agent Loop](https://github.com/hkuds/openspace/blob/main/openspace/agents/turns/loop.py): agent loop · Builds context, retrieves skills, calls models, and interprets tool turns. · `openspace/agents/turns/loop.py`
- [Agent Coordinator](https://github.com/hkuds/openspace/blob/main/openspace/agents/coordinator.py): delegation · Supports delegated and background agent work. · `openspace/agents/coordinator.py`
- [Grounding Client](https://github.com/hkuds/openspace/blob/main/openspace/grounding/core/grounding_client.py): tool gateway · Unifies local and remote shell, MCP, GUI, and web tools. · `openspace/grounding/core/grounding_client.py`
- [Permissions & Sandbox](https://github.com/hkuds/openspace/blob/main/openspace/grounding/core/permissions/engine.py): safety boundary · Gates model-directed host actions with validation and sandboxing. · `openspace/grounding/core/permissions/engine.py`

### Skill Lifecycle

- [Skill Registry & Store](https://github.com/hkuds/openspace/blob/main/openspace/skill_engine/registry.py): local skill store · Persists discovered skills, IDs, metadata, lineage, and quality records under local project state. · `openspace/skill_engine/registry.py`
- [Hybrid Skill Ranker](https://github.com/hkuds/openspace/blob/main/openspace/skill_engine/skill_ranker.py): retrieval · `openspace/skill_engine/skill_ranker.py`
- [Evidence Packets](https://github.com/hkuds/openspace/blob/main/openspace/skill_engine/evidence/packet_builder.py): run evidence · Assembles runtime, tool, and session evidence for quality decisions. · `openspace/skill_engine/evidence/packet_builder.py`
- [Controlled Evolution](https://github.com/hkuds/openspace/blob/main/openspace/skill_engine/evolution/engine.py): skill governance · Admits, authors, validates, evaluates, and audits proposed skill changes. · `openspace/skill_engine/evolution/engine.py`

### External Boundaries

- [LLM Client](https://github.com/hkuds/openspace/blob/main/openspace/llm/client.py): model client · Central LiteLLM-oriented model access. · `openspace/llm/client.py`
- [Cloud Services](https://github.com/hkuds/openspace/blob/main/openspace/cloud/client.py): cloud client · Authenticated skill discovery, embeddings, telemetry, and redaction boundary. · `openspace/cloud/client.py`
- [Communication Gateway](https://github.com/hkuds/openspace/blob/main/openspace/communication/gateway_runtime.py): channel adapters · Connects Feishu and WhatsApp, including the WhatsApp Node bridge. · `openspace/communication/gateway_runtime.py`

## Connections

- Embedded Python API → Runtime App: submits requests
- CLI, MCP & Dashboard API → Runtime App: submits requests
- Ink TUI Bridge → CLI, MCP & Dashboard API: NDJSON requests/events
- React Dashboard → CLI, MCP & Dashboard API: dashboard API
- Communication Gateway → Runtime App: channel tasks
- Runtime App → Execution Scheduler: schedules
- Runtime App → Session Runtime: owns sessions
- Execution Scheduler → Turn Runner: dispatches turns
- Turn Runner → Grounding Agent Loop: runs
- Turn Runner → Session Runtime: reads and records state
- Grounding Agent Loop → Hybrid Skill Ranker: retrieves skills
- Hybrid Skill Ranker → Skill Registry & Store: ranks local skills
- Grounding Agent Loop → LLM Client: model calls
- Grounding Agent Loop → Grounding Client: tool turns
- Grounding Agent Loop → Agent Coordinator: delegates work
- Grounding Client → Permissions & Sandbox: authorizes actions
- Grounding Client → Evidence Packets: tool outcomes
- Session Runtime → Evidence Packets: run context
- Evidence Packets → Controlled Evolution: quality signals
- Controlled Evolution → Skill Registry & Store: validated skills
- Skill Registry & Store → Cloud Services: upload, download & search
- Cloud Services → Hybrid Skill Ranker: cloud discovery
- Runtime App → Ink TUI Bridge: streamed events
