# s34tiwar/ghostshift architecture

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

- Interactive diagram: https://gitdiagram.com/s34tiwar/ghostshift
- Repository: https://github.com/s34tiwar/ghostshift
- Video tour (about a minute): https://gitdiagram.com/s34tiwar/ghostshift/video

## Overview

GhostShift is a simulated-factory recovery agent with a browser dashboard, FastAPI control plane, separate durable worker, and MongoDB-backed factory state and audit ledger. Its main workflow detects machine faults, retrieves a recovery procedure, proposes and policy-checks an action, records intent, repairs and independently verifies the machine, then exposes run progress and audit history to the UI. The README documents UI polling and worker-to-factory behavior; some internal executor and route details are unsampled, so uncertain links are omitted.

Main entry points: [`backend/reliability/ledger.py`](https://github.com/s34tiwar/ghostshift/blob/main/backend/reliability/ledger.py) (Run and audit ledger), [`frontend/app/page.tsx`](https://github.com/s34tiwar/ghostshift/blob/main/frontend/app/page.tsx) (Dashboard page), [`backend/api/read_routes.py`](https://github.com/s34tiwar/ghostshift/blob/main/backend/api/read_routes.py) (Read routes), [`backend/api/scenario_routes.py`](https://github.com/s34tiwar/ghostshift/blob/main/backend/api/scenario_routes.py) (Run controls)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Dashboard"]
  node_dashboard["Dashboard page<br/>[page.tsx]"]
  node_ui_views["Progress views"]
  node_ui_api["API client<br/>[api.ts]"]
  node_ui_activity["Activity mapping<br/>[activity.ts]"]
  node_ui_types["UI contracts<br/>[types.ts]"]
end

subgraph group_api["API control plane"]
  node_api_app["API application<br/>[main.py]"]
  node_read_routes["Read routes<br/>[read_routes.py]"]
  node_scenario_routes["Run controls<br/>[scenario_routes.py]"]
  node_factory_client["Factory client<br/>[factory_client.py]"]
  node_schemas["API schemas<br/>[schemas.py]"]
end

subgraph group_agent["Recovery planning"]
  node_planner["Planner graph<br/>[graph.py]"]
  node_retrieval["Procedure retrieval<br/>[retrieval.py]"]
  node_procedures["Recovery procedures<br/>[procedures.py]"]
end

subgraph group_reliability["Durable execution"]
  node_worker["Recovery worker<br/>[worker.py]"]
  node_executor["Action executor<br/>[executor.py]"]
  node_policies["Policy gate<br/>[policies.py]"]
  node_ledger["Run and audit ledger<br/>[ledger.py]"]
  node_storage[("Mongo persistence<br/>[storage.py]")]
end

subgraph group_factory["Factory simulator"]
  node_factory_app["Factory endpoints<br/>[main.py]"]
  node_factory_state["Machine logic<br/>[state.py]"]
  node_factory_store[("Machine store<br/>[mongo_store.py]")]
end

node_browser(("Operator"))
node_atlas[("MongoDB Atlas")]
node_gemini["Gemini API"]

node_browser -->|"uses"| node_dashboard
node_dashboard -->|"polls and submits"| node_ui_api
node_dashboard -->|"renders"| node_ui_views
node_dashboard -->|"formats activity"| node_ui_activity
node_ui_api -->|"uses contracts"| node_ui_types
node_ui_activity -->|"uses contracts"| node_ui_types
node_ui_api -->|"HTTP requests"| node_api_app
node_api_app -->|"registers routes"| node_read_routes
node_api_app -->|"registers routes"| node_scenario_routes
node_read_routes -->|"reads machine state"| node_factory_client
node_read_routes -->|"reads run history"| node_ledger
node_scenario_routes -->|"breaks or resets"| node_factory_client
node_scenario_routes -->|"enqueues runs"| node_storage
node_scenario_routes -->|"resumes or arms"| node_ledger
node_read_routes -->|"validates responses"| node_schemas
node_worker -->|"requests plan"| node_planner
node_planner -->|"retrieves procedures"| node_retrieval
node_planner -->|"uses fallback"| node_procedures
node_retrieval -->|"uses procedure model"| node_procedures
node_retrieval -->|"vector search"| node_atlas
node_planner -->|"generates proposal"| node_gemini
node_worker -->|"executes run"| node_executor
node_executor -->|"records action"| node_ledger
node_executor -->|"checks proposal"| node_policies
node_worker -->|"claims and updates"| node_ledger
node_ledger -->|"persists transitions"| node_storage
node_policies -->|"loads policy data"| node_storage
node_storage -->|"stores runs and audit"| node_atlas
node_factory_client -->|"HTTP calls"| node_factory_app
node_executor -->|"repairs and verifies"| node_factory_app
node_factory_app -->|"delegates machine actions"| node_factory_state
node_factory_app -->|"uses store"| node_factory_store
node_factory_store -->|"applies state logic"| node_factory_state
node_factory_store -->|"persists machine state"| node_atlas

click node_dashboard "https://github.com/s34tiwar/ghostshift/blob/main/frontend/app/page.tsx"
click node_ui_views "https://github.com/s34tiwar/ghostshift/tree/main/frontend/components"
click node_ui_api "https://github.com/s34tiwar/ghostshift/blob/main/frontend/lib/api.ts"
click node_ui_activity "https://github.com/s34tiwar/ghostshift/blob/main/frontend/lib/activity.ts"
click node_ui_types "https://github.com/s34tiwar/ghostshift/blob/main/frontend/lib/types.ts"
click node_api_app "https://github.com/s34tiwar/ghostshift/blob/main/backend/api/main.py"
click node_read_routes "https://github.com/s34tiwar/ghostshift/blob/main/backend/api/read_routes.py"
click node_scenario_routes "https://github.com/s34tiwar/ghostshift/blob/main/backend/api/scenario_routes.py"
click node_factory_client "https://github.com/s34tiwar/ghostshift/blob/main/backend/api/factory_client.py"
click node_schemas "https://github.com/s34tiwar/ghostshift/blob/main/backend/api/schemas.py"
click node_planner "https://github.com/s34tiwar/ghostshift/blob/main/backend/agent/graph.py"
click node_retrieval "https://github.com/s34tiwar/ghostshift/blob/main/backend/agent/retrieval.py"
click node_procedures "https://github.com/s34tiwar/ghostshift/blob/main/backend/agent/procedures.py"
click node_worker "https://github.com/s34tiwar/ghostshift/blob/main/backend/reliability/worker.py"
click node_executor "https://github.com/s34tiwar/ghostshift/blob/main/backend/reliability/executor.py"
click node_policies "https://github.com/s34tiwar/ghostshift/blob/main/backend/reliability/policies.py"
click node_ledger "https://github.com/s34tiwar/ghostshift/blob/main/backend/reliability/ledger.py"
click node_storage "https://github.com/s34tiwar/ghostshift/blob/main/backend/reliability/storage.py"
click node_factory_app "https://github.com/s34tiwar/ghostshift/blob/main/backend/factory/main.py"
click node_factory_state "https://github.com/s34tiwar/ghostshift/blob/main/backend/factory/state.py"
click node_factory_store "https://github.com/s34tiwar/ghostshift/blob/main/backend/factory/mongo_store.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_dashboard,node_ui_views,node_ui_api,node_ui_activity,node_ui_types toneBlue
class node_api_app,node_read_routes,node_scenario_routes,node_factory_client,node_schemas,node_atlas toneAmber
class node_planner,node_retrieval,node_procedures,node_gemini toneMint
class node_worker,node_executor,node_policies,node_ledger,node_storage toneRose
class node_factory_app,node_factory_state,node_factory_store,node_browser toneIndigo
```

## Components

### Dashboard

- [Dashboard page](https://github.com/s34tiwar/ghostshift/blob/main/frontend/app/page.tsx): `frontend/app/page.tsx`
- [Progress views](https://github.com/s34tiwar/ghostshift/tree/main/frontend/components): `frontend/components`
- [API client](https://github.com/s34tiwar/ghostshift/blob/main/frontend/lib/api.ts): `frontend/lib/api.ts`
- [Activity mapping](https://github.com/s34tiwar/ghostshift/blob/main/frontend/lib/activity.ts): `frontend/lib/activity.ts`
- [UI contracts](https://github.com/s34tiwar/ghostshift/blob/main/frontend/lib/types.ts): `frontend/lib/types.ts`

### API control plane

- [API application](https://github.com/s34tiwar/ghostshift/blob/main/backend/api/main.py): `backend/api/main.py`
- [Read routes](https://github.com/s34tiwar/ghostshift/blob/main/backend/api/read_routes.py): `backend/api/read_routes.py`
- [Run controls](https://github.com/s34tiwar/ghostshift/blob/main/backend/api/scenario_routes.py): `backend/api/scenario_routes.py`
- [Factory client](https://github.com/s34tiwar/ghostshift/blob/main/backend/api/factory_client.py): `backend/api/factory_client.py`
- [API schemas](https://github.com/s34tiwar/ghostshift/blob/main/backend/api/schemas.py): `backend/api/schemas.py`

### Recovery planning

- [Planner graph](https://github.com/s34tiwar/ghostshift/blob/main/backend/agent/graph.py): `backend/agent/graph.py`
- [Procedure retrieval](https://github.com/s34tiwar/ghostshift/blob/main/backend/agent/retrieval.py): `backend/agent/retrieval.py`
- [Recovery procedures](https://github.com/s34tiwar/ghostshift/blob/main/backend/agent/procedures.py): `backend/agent/procedures.py`

### Durable execution

- [Recovery worker](https://github.com/s34tiwar/ghostshift/blob/main/backend/reliability/worker.py): `backend/reliability/worker.py`
- [Action executor](https://github.com/s34tiwar/ghostshift/blob/main/backend/reliability/executor.py): `backend/reliability/executor.py`
- [Policy gate](https://github.com/s34tiwar/ghostshift/blob/main/backend/reliability/policies.py): `backend/reliability/policies.py`
- [Run and audit ledger](https://github.com/s34tiwar/ghostshift/blob/main/backend/reliability/ledger.py): `backend/reliability/ledger.py`
- [Mongo persistence](https://github.com/s34tiwar/ghostshift/blob/main/backend/reliability/storage.py): `backend/reliability/storage.py`

### Factory simulator

- [Factory endpoints](https://github.com/s34tiwar/ghostshift/blob/main/backend/factory/main.py): `backend/factory/main.py`
- [Machine logic](https://github.com/s34tiwar/ghostshift/blob/main/backend/factory/state.py): `backend/factory/state.py`
- [Machine store](https://github.com/s34tiwar/ghostshift/blob/main/backend/factory/mongo_store.py): `backend/factory/mongo_store.py`

### Other components

- **Operator**
- **MongoDB Atlas**
- **Gemini API**

## Connections

- Operator → Dashboard page: uses (evidence: `README.md`)
- Dashboard page → API client: polls and submits (evidence: `README.md`)
- Dashboard page → Progress views: renders (evidence: `frontend/app/page.tsx`)
- Dashboard page → Activity mapping: formats activity (evidence: `frontend/app/page.tsx`)
- API client → UI contracts: uses contracts (evidence: `frontend/lib/api.ts`)
- Activity mapping → UI contracts: uses contracts (evidence: `frontend/lib/activity.ts`)
- API client → API application: HTTP requests (evidence: `README.md`)
- API application → Read routes: registers routes (evidence: `backend/api/main.py`)
- API application → Run controls: registers routes (evidence: `backend/api/main.py`)
- Read routes → Factory client: reads machine state (evidence: `backend/api/read_routes.py`)
- Read routes → Run and audit ledger: reads run history (evidence: `backend/api/read_routes.py`)
- Run controls → Factory client: breaks or resets (evidence: `backend/api/scenario_routes.py`)
- Run controls → Mongo persistence: enqueues runs (evidence: `backend/api/scenario_routes.py`)
- Run controls → Run and audit ledger: resumes or arms (evidence: `backend/api/scenario_routes.py`)
- Read routes → API schemas: validates responses (evidence: `backend/api/read_routes.py`)
- Recovery worker → Planner graph: requests plan (evidence: `backend/reliability/worker.py`)
- Planner graph → Procedure retrieval: retrieves procedures (evidence: `backend/agent/graph.py`)
- Planner graph → Recovery procedures: uses fallback (evidence: `backend/agent/graph.py`)
- Procedure retrieval → Recovery procedures: uses procedure model (evidence: `backend/agent/retrieval.py`)
- Procedure retrieval → MongoDB Atlas: vector search (evidence: `backend/agent/retrieval.py`)
- Planner graph → Gemini API: generates proposal (evidence: `backend/agent/graph.py`)
- Recovery worker → Action executor: executes run (evidence: `backend/reliability/worker.py`)
- Action executor → Run and audit ledger: records action (evidence: `backend/reliability/executor.py`)
- Action executor → Policy gate: checks proposal (evidence: `backend/reliability/executor.py`)
- Recovery worker → Run and audit ledger: claims and updates (evidence: `backend/reliability/worker.py`)
- Run and audit ledger → Mongo persistence: persists transitions (evidence: `backend/reliability/ledger.py`)
- Policy gate → Mongo persistence: loads policy data (evidence: `backend/reliability/policies.py`)
- Mongo persistence → MongoDB Atlas: stores runs and audit (evidence: `backend/reliability/storage.py`)
- Factory client → Factory endpoints: HTTP calls (evidence: `backend/api/factory_client.py`)
- Action executor → Factory endpoints: repairs and verifies (evidence: `README.md`)
- Factory endpoints → Machine logic: delegates machine actions (evidence: `backend/factory/main.py`)
- Factory endpoints → Machine store: uses store (evidence: `backend/factory/main.py`)
- Machine store → Machine logic: applies state logic (evidence: `backend/factory/mongo_store.py`)
- Machine store → MongoDB Atlas: persists machine state (evidence: `README.md`)
