# ixi-enki/ferngeist architecture

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

- Interactive diagram: https://gitdiagram.com/ixi-enki/ferngeist
- Repository: https://github.com/ixi-enki/ferngeist

## Overview

1. **Overall shape**
   Ferngeist is a modular Android client for ACP-compatible coding agents. The APK is split into an `app` shell plus feature, bridge, gateway, data, and shared-core modules. The main architectural goal is to keep UI, persisted state, and ACP transport concerns separated.

2. **App shell and composition root**
   `app/` is the Android entry point: it owns `MainActivity`, the application class, Hilt wiring, theme setup, and the foreground service used to keep agent connections alive. It does not contain the core domain logic; it assembles the modules and drives navigation.

3. **Shared contracts and domain models**
   `core/model/` defines the app-wide abstractions: chat/domain models, server/session models, repository interfaces, and lightweight stores such as recent CWD and recent selection. This module is the boundary between UI/logic and persistence, so feature modules talk to interfaces rather than database classes.

4. **Shared UI and common utilities**
   `core/common/` contains reusable Compose UI pieces and a shared `MviViewModel` base. It also holds UI-specific helpers like connection diagnostics and shared-bounds keys used across screens. This is the common presentation layer used by multiple features.

5. **Persistence subsystem**
   `data/database/` is the local persistence implementation. It provides the Room database, DAOs, entities, encryption helpers, and repository implementations for servers, sessions, gateway bindings, and target session settings. Its role is to implement the `core/model` repository contracts and store app state locally.

6. **Gateway client subsystem**
   `gateway-client/` encapsulates communication with the optional Ferngeist ACP Gateway. It handles pairing payload parsing, URL resolution, credential refresh, proof-based auth, and repository calls to the gateway’s REST endpoint. This module exists so the rest of the app does not need to know gateway-specific protocol details.

7. **ACP bridge and session orchestration**
   `acp-bridge/` is the transport/session layer that connects the app to ACP agents. It includes connection management, diagnostics, permissions/connectivity handling, auth header building, session registry/update mapping, and a session state engine/reducer. The bridge exposes a chat-session facade that the features can use without dealing with raw transport callbacks.

8. **Feature modules**
   `feature/serverlist/` manages saved servers, gateways, pairing/auth data, and consent for launching agents.  
   `feature/sessionlist/` manages session discovery and the user’s recent working directory.  
   `feature/chat/` renders the live chat experience, including message streaming, markdown state, scroll behavior, tool-call rendering, and session coordination.  
   Each feature module owns its own ViewModels, Compose screens, and small local stores.

9. **Primary data flows**
   User actions start in Compose screens, flow into feature ViewModels, then into shared repositories/stores and the ACP bridge. Persisted server/session/gateway state comes from Room/DataStore implementations in `data/database` and feature-local stores. When a chat starts, the bridge streams ACP updates back into the chat reducer/ViewModel, which updates the UI incrementally.

10. **Connection modes**
   Ferngeist supports two connection paths: direct/manual ACP servers over WebSocket, and gateway-backed local agents through the optional Ferngeist ACP Gateway. The gateway path adds pairing, auth, and URL resolution before the ACP session is opened; the manual path connects directly to the agent endpoint via the ACP SDK.

11. **Android-specific runtime concerns**
   The app includes a foreground service and battery-optimization helpers, indicating the connection may need to survive backgrounding. Network/security and localization resources are bundled in the app, and the project targets Android 13+.

12. **Boundaries to draw in an architecture diagram**
   The clearest boundaries are: UI/features vs shared contracts, contracts vs persistence implementations, app vs background/runtime services, and app vs external ACP/Gateway services. The ACP bridge and gateway client are the two key integration layers between the Android UI and the outside agent ecosystem.

Main entry points: [`acp-bridge/src/main/kotlin/com/tamimarafat/ferngeist/acp/bridge/facade/AcpChatSessionFacade.kt`](https://github.com/ixi-enki/ferngeist/blob/master/acp-bridge/src/main/kotlin/com/tamimarafat/ferngeist/acp/bridge/facade/AcpChatSessionFacade.kt) (ACP bridge), [`app/src/main/kotlin/com/tamimarafat/ferngeist/MainActivity.kt`](https://github.com/ixi-enki/ferngeist/blob/master/app/src/main/kotlin/com/tamimarafat/ferngeist/MainActivity.kt) (App), [`feature/serverlist/src/main/kotlin/com/tamimarafat/ferngeist/feature/serverlist/ServerListViewModel.kt`](https://github.com/ixi-enki/ferngeist/blob/master/feature/serverlist/src/main/kotlin/com/tamimarafat/ferngeist/feature/serverlist/ServerListViewModel.kt) (Servers), [`data/database/src/main/kotlin/com/tamimarafat/ferngeist/data/database/repository/ServerRepositoryImpl.kt`](https://github.com/ixi-enki/ferngeist/blob/master/data/database/src/main/kotlin/com/tamimarafat/ferngeist/data/database/repository/ServerRepositoryImpl.kt) (Repos impl)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app_shell["App shell"]
  node_app_main["App<br/>android entry<br/>[MainActivity.kt]"]
  node_app_service["Foreground svc<br/>runtime service"]
  node_app_battery["Battery help<br/>android helper"]
end

subgraph group_core["Shared core"]
  node_model_contracts["Models<br/>domain contracts"]
  node_common_ui["Common UI<br/>compose shared<br/>[MviViewModel.kt]"]
end

subgraph group_storage["Persistence"]
  node_database[("Database<br/>room persistence")]
  node_repositories_impl["Repos impl<br/>repo adapters"]
  node_entities_daos["DAOs<br/>storage schema<br/>[ServerDao.kt]"]
  node_encryptor["Crypto<br/>encryption"]
end

subgraph group_integration["Integrations"]
  node_acp_bridge["ACP bridge<br/>transport layer"]
  node_acp_session["Session engine<br/>state engine"]
  node_acp_connection["Connection<br/>transport control"]
  node_gateway_client["Gateway client<br/>gateway integration"]
end

subgraph group_features["Feature modules"]
  node_server_feature["Servers<br/>feature ui"]
  node_session_feature["Sessions<br/>feature ui"]
  node_chat_feature["Chat<br/>feature ui<br/>[ChatViewModel.kt]"]
end

node_app_main -->|"navigates to"| node_server_feature
node_app_main -->|"navigates to"| node_session_feature
node_app_main -->|"hosts"| node_chat_feature
node_app_main -->|"starts"| node_app_service
node_app_main -->|"uses"| node_app_battery
node_server_feature -->|"uses"| node_model_contracts
node_session_feature -->|"uses"| node_model_contracts
node_chat_feature -->|"uses"| node_model_contracts
node_server_feature -->|"calls"| node_gateway_client
node_server_feature -->|"persists via"| node_repositories_impl
node_session_feature -->|"persists via"| node_repositories_impl
node_chat_feature -->|"streams via"| node_acp_bridge
node_chat_feature -->|"renders with"| node_common_ui
node_server_feature -->|"renders with"| node_common_ui
node_session_feature -->|"renders with"| node_common_ui
node_repositories_impl -->|"implements"| node_database
node_repositories_impl -->|"reads/writes"| node_entities_daos
node_repositories_impl -->|"secures"| node_encryptor
node_acp_bridge -->|"orchestrates"| node_acp_session
node_acp_bridge -->|"opens"| node_acp_connection
node_acp_bridge -->|"integrates with"| node_gateway_client
node_acp_bridge -->|"adapts"| node_model_contracts
node_gateway_client -->|"returns"| node_model_contracts
node_app_service -->|"keeps alive"| node_acp_bridge

click node_app_main "https://github.com/ixi-enki/ferngeist/blob/master/app/src/main/kotlin/com/tamimarafat/ferngeist/MainActivity.kt"
click node_app_service "https://github.com/ixi-enki/ferngeist/blob/master/app/src/main/kotlin/com/tamimarafat/ferngeist/service/FerngeistForegroundService.kt"
click node_app_battery "https://github.com/ixi-enki/ferngeist/blob/master/app/src/main/kotlin/com/tamimarafat/ferngeist/service/BatteryOptimizationHelper.kt"
click node_model_contracts "https://github.com/ixi-enki/ferngeist/blob/master/core/model/src/main/kotlin/com/tamimarafat/ferngeist/core/model/ChatDomainModels.kt"
click node_common_ui "https://github.com/ixi-enki/ferngeist/blob/master/core/common/src/main/kotlin/com/tamimarafat/ferngeist/core/common/MviViewModel.kt"
click node_database "https://github.com/ixi-enki/ferngeist/blob/master/data/database/src/main/kotlin/com/tamimarafat/ferngeist/data/database/FerngeistDatabase.kt"
click node_repositories_impl "https://github.com/ixi-enki/ferngeist/blob/master/data/database/src/main/kotlin/com/tamimarafat/ferngeist/data/database/repository/ServerRepositoryImpl.kt"
click node_entities_daos "https://github.com/ixi-enki/ferngeist/blob/master/data/database/src/main/kotlin/com/tamimarafat/ferngeist/data/database/dao/ServerDao.kt"
click node_encryptor "https://github.com/ixi-enki/ferngeist/blob/master/data/database/src/main/kotlin/com/tamimarafat/ferngeist/data/database/crypto/CredentialEncryptor.kt"
click node_acp_bridge "https://github.com/ixi-enki/ferngeist/blob/master/acp-bridge/src/main/kotlin/com/tamimarafat/ferngeist/acp/bridge/facade/AcpChatSessionFacade.kt"
click node_acp_session "https://github.com/ixi-enki/ferngeist/blob/master/acp-bridge/src/main/kotlin/com/tamimarafat/ferngeist/acp/bridge/session/SessionStateEngine.kt"
click node_acp_connection "https://github.com/ixi-enki/ferngeist/blob/master/acp-bridge/src/main/kotlin/com/tamimarafat/ferngeist/acp/bridge/connection/ConnectionOrchestrator.kt"
click node_gateway_client "https://github.com/ixi-enki/ferngeist/blob/master/gateway-client/src/main/kotlin/com/tamimarafat/ferngeist/gateway/GatewayRepositoryImpl.kt"
click node_server_feature "https://github.com/ixi-enki/ferngeist/blob/master/feature/serverlist/src/main/kotlin/com/tamimarafat/ferngeist/feature/serverlist/ServerListViewModel.kt"
click node_session_feature "https://github.com/ixi-enki/ferngeist/blob/master/feature/sessionlist/src/main/kotlin/com/tamimarafat/ferngeist/feature/sessionlist/SessionListViewModel.kt"
click node_chat_feature "https://github.com/ixi-enki/ferngeist/blob/master/feature/chat/src/main/kotlin/com/tamimarafat/ferngeist/feature/chat/ChatViewModel.kt"

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_app_main,node_app_service,node_app_battery toneBlue
class node_model_contracts,node_common_ui toneAmber
class node_database,node_repositories_impl,node_entities_daos,node_encryptor toneMint
class node_acp_bridge,node_acp_session,node_acp_connection,node_gateway_client toneRose
class node_server_feature,node_session_feature,node_chat_feature toneIndigo
```

## Components

### App shell

Android entry point, dependency wiring, and runtime services.

- [App](https://github.com/ixi-enki/ferngeist/blob/master/app/src/main/kotlin/com/tamimarafat/ferngeist/MainActivity.kt): android entry · Composition root with application, activity, DI, theme, and foreground service. · `app/src/main/kotlin/com/tamimarafat/ferngeist/MainActivity.kt`
- [Foreground svc](https://github.com/ixi-enki/ferngeist/blob/master/app/src/main/kotlin/com/tamimarafat/ferngeist/service/FerngeistForegroundService.kt): runtime service · Keeps agent connections alive in backgrounded app states. · `app/src/main/kotlin/com/tamimarafat/ferngeist/service/FerngeistForegroundService.kt`
- [Battery help](https://github.com/ixi-enki/ferngeist/blob/master/app/src/main/kotlin/com/tamimarafat/ferngeist/service/BatteryOptimizationHelper.kt): android helper · Battery optimization prompts, preferences, and controller support. · `app/src/main/kotlin/com/tamimarafat/ferngeist/service/BatteryOptimizationHelper.kt`

### Shared core

App-wide contracts, models, and reusable presentation utilities.

- [Models](https://github.com/ixi-enki/ferngeist/blob/master/core/model/src/main/kotlin/com/tamimarafat/ferngeist/core/model/ChatDomainModels.kt): domain contracts · Chat, server, session, gateway-binding models and repository/store interfaces. · `core/model/src/main/kotlin/com/tamimarafat/ferngeist/core/model/ChatDomainModels.kt`
- [Common UI](https://github.com/ixi-enki/ferngeist/blob/master/core/common/src/main/kotlin/com/tamimarafat/ferngeist/core/common/MviViewModel.kt): compose shared · Reusable Compose widgets and shared MVI base view model. · `core/common/src/main/kotlin/com/tamimarafat/ferngeist/core/common/MviViewModel.kt`

### Persistence

Local database, entities, DAOs, and repository implementations.

- [Database](https://github.com/ixi-enki/ferngeist/blob/master/data/database/src/main/kotlin/com/tamimarafat/ferngeist/data/database/FerngeistDatabase.kt): room persistence · Room database entry point for local state storage. · `data/database/src/main/kotlin/com/tamimarafat/ferngeist/data/database/FerngeistDatabase.kt`
- [Repos impl](https://github.com/ixi-enki/ferngeist/blob/master/data/database/src/main/kotlin/com/tamimarafat/ferngeist/data/database/repository/ServerRepositoryImpl.kt): repo adapters · Repository implementations bridging contracts to local persistence. · `data/database/src/main/kotlin/com/tamimarafat/ferngeist/data/database/repository/ServerRepositoryImpl.kt`
- [DAOs](https://github.com/ixi-enki/ferngeist/blob/master/data/database/src/main/kotlin/com/tamimarafat/ferngeist/data/database/dao/ServerDao.kt): storage schema · Entities and DAOs for servers, sessions, gateways, and launch settings. · `data/database/src/main/kotlin/com/tamimarafat/ferngeist/data/database/dao/ServerDao.kt`
- [Crypto](https://github.com/ixi-enki/ferngeist/blob/master/data/database/src/main/kotlin/com/tamimarafat/ferngeist/data/database/crypto/CredentialEncryptor.kt): encryption · Credential encryption helpers used by persisted auth data. · `data/database/src/main/kotlin/com/tamimarafat/ferngeist/data/database/crypto/CredentialEncryptor.kt`

### Integrations

ACP bridge and optional Ferngeist gateway client.

- [ACP bridge](https://github.com/ixi-enki/ferngeist/blob/master/acp-bridge/src/main/kotlin/com/tamimarafat/ferngeist/acp/bridge/facade/AcpChatSessionFacade.kt): transport layer · Session orchestration, connection management, diagnostics, and chat-session facade. · `acp-bridge/src/main/kotlin/com/tamimarafat/ferngeist/acp/bridge/facade/AcpChatSessionFacade.kt`
- [Session engine](https://github.com/ixi-enki/ferngeist/blob/master/acp-bridge/src/main/kotlin/com/tamimarafat/ferngeist/acp/bridge/session/SessionStateEngine.kt): state engine · Reducer, runtime, snapshot, and registry for ACP session updates. · `acp-bridge/src/main/kotlin/com/tamimarafat/ferngeist/acp/bridge/session/SessionStateEngine.kt`
- [Connection](https://github.com/ixi-enki/ferngeist/blob/master/acp-bridge/src/main/kotlin/com/tamimarafat/ferngeist/acp/bridge/connection/ConnectionOrchestrator.kt): transport control · Connection manager, orchestrator, auth headers, permissions, and connectivity diagnostics. · `acp-bridge/src/main/kotlin/com/tamimarafat/ferngeist/acp/bridge/connection/ConnectionOrchestrator.kt`
- [Gateway client](https://github.com/ixi-enki/ferngeist/blob/master/gateway-client/src/main/kotlin/com/tamimarafat/ferngeist/gateway/GatewayRepositoryImpl.kt): gateway integration · Optional Ferngeist ACP Gateway client for pairing, proof auth, and URL resolution. · `gateway-client/src/main/kotlin/com/tamimarafat/ferngeist/gateway/GatewayRepositoryImpl.kt`

### Feature modules

User-facing screens and their view models.

- [Servers](https://github.com/ixi-enki/ferngeist/blob/master/feature/serverlist/src/main/kotlin/com/tamimarafat/ferngeist/feature/serverlist/ServerListViewModel.kt): feature ui · Saved servers, gateways, pairing data, and launch consent flows. · `feature/serverlist/src/main/kotlin/com/tamimarafat/ferngeist/feature/serverlist/ServerListViewModel.kt`
- [Sessions](https://github.com/ixi-enki/ferngeist/blob/master/feature/sessionlist/src/main/kotlin/com/tamimarafat/ferngeist/feature/sessionlist/SessionListViewModel.kt): feature ui · Session discovery and recent working-directory selection. · `feature/sessionlist/src/main/kotlin/com/tamimarafat/ferngeist/feature/sessionlist/SessionListViewModel.kt`
- [Chat](https://github.com/ixi-enki/ferngeist/blob/master/feature/chat/src/main/kotlin/com/tamimarafat/ferngeist/feature/chat/ChatViewModel.kt): feature ui · Live chat screen, streaming updates, markdown state, and tool-call rendering. · `feature/chat/src/main/kotlin/com/tamimarafat/ferngeist/feature/chat/ChatViewModel.kt`

## Connections

- App → Servers: navigates to · App shell hosts the server management entry points.
- App → Sessions: navigates to · App shell routes into session selection and discovery.
- App → Chat: hosts · App shell launches the live chat experience.
- App → Foreground svc: starts · Foreground service is managed by the app runtime.
- App → Battery help: uses · Battery-optimization helpers support background longevity.
- Servers → Models: uses · Feature logic depends on shared domain models and repository contracts.
- Sessions → Models: uses · Recent CWD and session-related contracts are shared through core models.
- Chat → Models: uses · Chat UI and reducers consume shared chat/session domain types.
- Servers → Gateway client: calls · Gateway setup, pairing, and auth flows are delegated to the gateway client.
- Servers → Repos impl: persists via · Saved server and gateway state is stored through repository implementations.
- Sessions → Repos impl: persists via · Recent working-directory and session state are stored locally.
- Chat → ACP bridge: streams via · Chat session coordination relies on the ACP bridge facade.
- Chat → Common UI: renders with · Shared UI primitives and MVI base support the chat experience.
- Servers → Common UI: renders with · Common Compose widgets and state helpers are reused in server screens.
- Sessions → Common UI: renders with · Shared UI primitives are reused in session-related screens.
- Repos impl → Database: implements · Repository adapters are backed by the Room database.
- Repos impl → DAOs: reads/writes · Data access flows through DAOs and entity mappings.
- Repos impl → Crypto: secures · Encrypted storage is used for sensitive credential-like values.
- ACP bridge → Session engine: orchestrates · The bridge coordinates session state, updates, and lifecycle.
- ACP bridge → Connection: opens · Transport setup and connection management are handled in the bridge layer.
- ACP bridge → Gateway client: integrates with · Gateway-backed sessions depend on pairing and auth support from the gateway client.
- ACP bridge → Models: adapts · Bridge-facing session and chat types align with shared contracts.
- Gateway client → Models: returns · Gateway repository results map into shared domain models.
- Foreground svc → ACP bridge: keeps alive · Foreground runtime support protects active ACP connections.
