# labcoder/xana architecture

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

- Interactive diagram: https://gitdiagram.com/labcoder/xana
- Repository: https://github.com/labcoder/xana

## Overview

#### Purpose and entry points
Xana is a Rust personal AI-agent harness for terminal interaction, workspace tools, provider-backed chat, managed Codex execution, and bounded external integrations. The binary starts at `src/main.rs`; library composition is in `src/lib.rs`. Command parsing and mode selection live in `src/cli.rs`, with application command handlers in `src/app.rs`. Bare interactive execution chooses TUI or plain terminal; one-shot, JSON, setup, service-host, and administrative commands share the CLI boundary.

#### Frontend and foreground control
Frontend code separates semantic UI behavior from execution ownership. `src/frontend.rs` and `src/frontend/protocol.rs` define frontend/runtime interaction; `src/tui.rs` implements the full-screen Ratatui surface, while `src/plain_terminal.rs` supports append-only control-free output. The local foreground host (`src/local_host.rs`, `src/local_host/protocol.rs`, `src/workspace_host.rs`) enables loopback observer/controller attachment with a single controller lease, rather than exposing a network service.

#### Configuration, identity, and durable local state
Configuration and credential references are validated and atomically managed through `src/config.rs`, `src/config_migration.rs`, and `src/credential.rs`. Profiles, routes, projects, plugins, and private bindings define effective execution without embedding secrets in portable project files. Durable local state is partitioned across `src/private_state/store.rs`, `src/session/store.rs`, and immutable artifact handling in `src/artifact.rs`; workspace/session ownership is a material concurrency boundary.

#### Native agent execution pipeline
Native conversations freeze prompt/context/tool state, execute a provider turn, stream responses, broker tool calls, and durably record session events. Core responsibilities span `src/agent.rs`, `src/native_runtime.rs`, and `src/prompt/render.rs`. Context construction is bounded and sourced from workspace/project materials through `src/context.rs`; session records are reduced and persisted via `src/session/durable.rs` and `src/session/reduce.rs`. Tool intents/results are operation-aware, enabling explicit—not automatic—recovery.

#### Model providers and managed runtimes
`src/provider.rs` abstracts native model connections, with concrete Anthropic and OpenAI-compatible implementations in `src/provider/anthropic.rs` and `src/provider/openai_compat/client.rs`. Catalog and model capability resolution reside in `src/model_catalog.rs` and `src/capability.rs`. Codex is intentionally a managed runtime, not another native model loop: `src/managed/codex.rs` supervises its app-server protocol and threads, while `src/managed_execution.rs` projects managed activity and approval events into Xana’s frontend.

#### Tool authority, outbound data, and integrations
Native tool definitions are centralized in `src/tool.rs`, including filesystem, document, shell, child-agent, and orchestration-plan tools. Every effect passes the permission policy/broker boundary in `src/permission/broker.rs` and `src/permission/policy.rs`; permission does not sandbox the host process. External data dispatch is separately gated by `src/outbound.rs`. MCP supports supervised stdio/HTTP/OAuth transports (`src/mcp/stdio.rs`, `src/mcp/http.rs`, `src/mcp/oauth.rs`); remote-agent delegation uses `src/a2a.rs`; image/vision routes are independent focused services in `src/focused_service.rs`.

#### Orchestration, operations, and observability
Child work is admission-controlled, budgeted, routed, and supervised by `src/orchestration/plan.rs`, `src/orchestration/supervisor.rs`, and `src/orchestration/execution.rs`. Children receive bounded handoff data and cannot recursively delegate. Durable operation state and explicit resume planning are in `src/operation.rs` and `src/operation/recovery.rs`. Diagnostics, structured metadata, crash reporting, and read-only repair guidance are handled by `src/diagnostics.rs` and `src/doctor.rs`; release packaging is driven by Cargo plus `.github/workflows/release.yml` and installer scripts under `install/`.

Main entry points: [`src/native_runtime.rs`](https://github.com/labcoder/xana/blob/main/src/native_runtime.rs) (Native agent runtime), [`src/cli.rs`](https://github.com/labcoder/xana/blob/main/src/cli.rs) (Command dispatch), [`src/frontend/protocol.rs`](https://github.com/labcoder/xana/blob/main/src/frontend/protocol.rs) (Frontend protocol), [`src/tool.rs`](https://github.com/labcoder/xana/blob/main/src/tool.rs) (Native tool intents)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry &amp; Frontends"]
  node_main{{"CLI binary<br/>Rust entrypoint<br/>[main.rs]"}}
  node_cli_app["Command dispatch<br/>CLI application<br/>[cli.rs]"]
  node_frontend["Frontend protocol<br/>runtime UI contract<br/>[protocol.rs]"]
  node_terminal_ui["Terminal frontends<br/>TUI and plain output<br/>[tui.rs]"]
  node_local_host["Local foreground host<br/>loopback control host<br/>[local_host.rs]"]
end

subgraph group_state["State &amp; Configuration"]
  node_config["Config &amp; credentials<br/>validated local config<br/>[config.rs]"]
  node_durable_state[("Private and session state<br/>durable local stores<br/>[store.rs]")]
  node_artifacts[("Immutable artifacts<br/>artifact store<br/>[artifact.rs]")]
end

subgraph group_native["Native Execution"]
  node_native_runtime["Native agent runtime<br/>conversation executor<br/>[native_runtime.rs]"]
  node_prompt_context["Prompt &amp; bounded context<br/>context builder<br/>[context.rs]"]
  node_session_durable[("Session event reduction<br/>event persistence<br/>[durable.rs]")]
  node_tools["Native tool intents<br/>tool registry<br/>[tool.rs]"]
  node_permissions{{"Permission broker<br/>effect authorization<br/>[broker.rs]"}}
end

subgraph group_external["Providers &amp; Integrations"]
  node_providers["Model providers<br/>native provider abstraction<br/>[provider.rs]"]
  node_managed_codex["Managed Codex runtime<br/>supervised app-server<br/>[codex.rs]"]
  node_outbound{{"Outbound data gate<br/>external dispatch policy<br/>[outbound.rs]"}}
  node_mcp["MCP transports<br/>supervised integrations<br/>[stdio.rs]"]
end

subgraph group_ops["Orchestration &amp; Operations"]
  node_orchestration["Child orchestration<br/>admission and supervision<br/>[supervisor.rs]"]
  node_operations[("Operation recovery<br/>durable resume planner<br/>[recovery.rs]")]
  node_diagnostics["Diagnostics &amp; doctor<br/>support operations<br/>[doctor.rs]"]
end

node_main -->|"starts"| node_cli_app
node_cli_app -->|"selects mode"| node_frontend
node_frontend -->|"drives"| node_terminal_ui
node_frontend -->|"attaches locally"| node_local_host
node_cli_app -->|"loads"| node_config
node_cli_app -->|"runs native mode"| node_native_runtime
node_cli_app -->|"runs managed mode"| node_managed_codex
node_native_runtime -->|"builds turn context"| node_prompt_context
node_native_runtime -->|"executes provider turn"| node_providers
node_native_runtime -->|"brokers tool calls"| node_tools
node_native_runtime -->|"records events"| node_session_durable
node_prompt_context -->|"reads materials"| node_artifacts
node_tools -->|"authorizes effects"| node_permissions
node_permissions -->|"gates external dispatch"| node_outbound
node_outbound -->|"dispatches integration data"| node_mcp
node_tools -->|"starts child work"| node_orchestration
node_orchestration -->|"routes child execution"| node_native_runtime
node_managed_codex -->|"projects activity and approvals"| node_frontend
node_native_runtime -->|"persists operation state"| node_operations
node_operations -.->|"plans explicit resume"| node_native_runtime
node_diagnostics -.->|"inspects read-only"| node_durable_state
node_session_durable -->|"persists sessions"| node_durable_state

click node_main "https://github.com/labcoder/xana/blob/main/src/main.rs"
click node_cli_app "https://github.com/labcoder/xana/blob/main/src/cli.rs"
click node_frontend "https://github.com/labcoder/xana/blob/main/src/frontend/protocol.rs"
click node_terminal_ui "https://github.com/labcoder/xana/blob/main/src/tui.rs"
click node_local_host "https://github.com/labcoder/xana/blob/main/src/local_host.rs"
click node_config "https://github.com/labcoder/xana/blob/main/src/config.rs"
click node_durable_state "https://github.com/labcoder/xana/blob/main/src/session/store.rs"
click node_artifacts "https://github.com/labcoder/xana/blob/main/src/artifact.rs"
click node_native_runtime "https://github.com/labcoder/xana/blob/main/src/native_runtime.rs"
click node_prompt_context "https://github.com/labcoder/xana/blob/main/src/context.rs"
click node_session_durable "https://github.com/labcoder/xana/blob/main/src/session/durable.rs"
click node_tools "https://github.com/labcoder/xana/blob/main/src/tool.rs"
click node_permissions "https://github.com/labcoder/xana/blob/main/src/permission/broker.rs"
click node_providers "https://github.com/labcoder/xana/blob/main/src/provider.rs"
click node_managed_codex "https://github.com/labcoder/xana/blob/main/src/managed/codex.rs"
click node_outbound "https://github.com/labcoder/xana/blob/main/src/outbound.rs"
click node_mcp "https://github.com/labcoder/xana/blob/main/src/mcp/stdio.rs"
click node_orchestration "https://github.com/labcoder/xana/blob/main/src/orchestration/supervisor.rs"
click node_operations "https://github.com/labcoder/xana/blob/main/src/operation/recovery.rs"
click node_diagnostics "https://github.com/labcoder/xana/blob/main/src/doctor.rs"

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_main,node_cli_app,node_frontend,node_terminal_ui,node_local_host toneBlue
class node_config,node_durable_state,node_artifacts toneAmber
class node_native_runtime,node_prompt_context,node_session_durable,node_tools,node_permissions toneMint
class node_providers,node_managed_codex,node_outbound,node_mcp toneRose
class node_orchestration,node_operations,node_diagnostics toneIndigo
```

## Components

### Entry & Frontends

- [CLI binary](https://github.com/labcoder/xana/blob/main/src/main.rs): Rust entrypoint · `src/main.rs`
- [Command dispatch](https://github.com/labcoder/xana/blob/main/src/cli.rs): CLI application · Selects interactive, one-shot, service, setup, and admin modes. · `src/cli.rs`
- [Frontend protocol](https://github.com/labcoder/xana/blob/main/src/frontend/protocol.rs): runtime UI contract · `src/frontend/protocol.rs`
- [Terminal frontends](https://github.com/labcoder/xana/blob/main/src/tui.rs): TUI and plain output · `src/tui.rs`
- [Local foreground host](https://github.com/labcoder/xana/blob/main/src/local_host.rs): loopback control host · Provides observer attachment and a single controller lease. · `src/local_host.rs`

### State & Configuration

- [Config & credentials](https://github.com/labcoder/xana/blob/main/src/config.rs): validated local config · Profiles and bindings avoid portable embedded secrets. · `src/config.rs`
- [Private and session state](https://github.com/labcoder/xana/blob/main/src/session/store.rs): durable local stores · Workspace and session ownership form a concurrency boundary. · `src/session/store.rs`
- [Immutable artifacts](https://github.com/labcoder/xana/blob/main/src/artifact.rs): artifact store · `src/artifact.rs`

### Native Execution

- [Native agent runtime](https://github.com/labcoder/xana/blob/main/src/native_runtime.rs): conversation executor · Freezes turn state, streams output, and coordinates tools. · `src/native_runtime.rs`
- [Prompt & bounded context](https://github.com/labcoder/xana/blob/main/src/context.rs): context builder · Builds bounded workspace and project context. · `src/context.rs`
- [Session event reduction](https://github.com/labcoder/xana/blob/main/src/session/durable.rs): event persistence · Reduces and records durable session events. · `src/session/durable.rs`
- [Native tool intents](https://github.com/labcoder/xana/blob/main/src/tool.rs): tool registry · Defines filesystem, shell, document, child-agent, and plan tools. · `src/tool.rs`
- [Permission broker](https://github.com/labcoder/xana/blob/main/src/permission/broker.rs): effect authorization · Policy-gates effects but does not sandbox the host process. · `src/permission/broker.rs`

### Providers & Integrations

- [Model providers](https://github.com/labcoder/xana/blob/main/src/provider.rs): native provider abstraction · Connects native turns to Anthropic and OpenAI-compatible models. · `src/provider.rs`
- [Managed Codex runtime](https://github.com/labcoder/xana/blob/main/src/managed/codex.rs): supervised app-server · A separate managed runtime with supervised protocol threads. · `src/managed/codex.rs`
- [Outbound data gate](https://github.com/labcoder/xana/blob/main/src/outbound.rs): external dispatch policy · `src/outbound.rs`
- [MCP transports](https://github.com/labcoder/xana/blob/main/src/mcp/stdio.rs): supervised integrations · Supports stdio, HTTP, and OAuth transports. · `src/mcp/stdio.rs`

### Orchestration & Operations

- [Child orchestration](https://github.com/labcoder/xana/blob/main/src/orchestration/supervisor.rs): admission and supervision · Budgets, routes, and supervises bounded non-recursive child work. · `src/orchestration/supervisor.rs`
- [Operation recovery](https://github.com/labcoder/xana/blob/main/src/operation/recovery.rs): durable resume planner · Stores operation state and plans explicit resume. · `src/operation/recovery.rs`
- [Diagnostics & doctor](https://github.com/labcoder/xana/blob/main/src/doctor.rs): support operations · Provides crash reporting and read-only repair guidance. · `src/doctor.rs`

## Connections

- CLI binary → Command dispatch: starts
- Command dispatch → Frontend protocol: selects mode
- Frontend protocol → Terminal frontends: drives
- Frontend protocol → Local foreground host: attaches locally
- Command dispatch → Config & credentials: loads
- Command dispatch → Native agent runtime: runs native mode
- Command dispatch → Managed Codex runtime: runs managed mode
- Native agent runtime → Prompt & bounded context: builds turn context
- Native agent runtime → Model providers: executes provider turn
- Native agent runtime → Native tool intents: brokers tool calls
- Native agent runtime → Session event reduction: records events
- Prompt & bounded context → Immutable artifacts: reads materials
- Native tool intents → Permission broker: authorizes effects
- Permission broker → Outbound data gate: gates external dispatch
- Outbound data gate → MCP transports: dispatches integration data
- Native tool intents → Child orchestration: starts child work
- Child orchestration → Native agent runtime: routes child execution
- Managed Codex runtime → Frontend protocol: projects activity and approvals
- Native agent runtime → Operation recovery: persists operation state
- Operation recovery → Native agent runtime: plans explicit resume
- Diagnostics & doctor → Private and session state: inspects read-only
- Session event reduction → Private and session state: persists sessions
