System Architecture

A bird's-eye view of how the Ghost Agent runtime fits together.

Top-level diagram

Interface Layer Web UI · port 8080 interface/server.py Slack Bot · Socket Mode externals/slack_bot/main.py Clockwork Ghost · PyQt6 externals/clockwork_ghost/ Voice · Jetson externals/tts_stt/voice_server.py Image Gen · GPU externals/image_generation/ API Layer · FastAPI · uvicorn · port 8000 /chat · /api/chat · /v1/chat/completions /api/upload · /api/download /api/projects/* /api/workspace/save · /load catch-all proxy Reasoning Core GhostAgent · agent.py LLMClient · circuit breaker TaskTree planner MCTS reasoner Hypothesis tester MemoryBus · RRF ContextManager Verifier Uncertainty Dream · Self-Play · Frontier Project advancer · safety Memory · Chroma + SQLite + JSON Vector · Chroma Graph · SQLite+nx Episodic · SQLite Skills · JSON Profile · JSON Projects · SQLite Journal · JSON Scratchpad · LRU Frontier · fcntl Adaptive Threshold Contradiction Log Execution Substrate Docker Sandbox python:3.11-slim · workspace bind Tor proxy socks5h:// APScheduler cron + interval Tool Registry · 50+ tools execute · file_system · web · db · vision Acquired + Composed Skills TDD-validated · semantic-routed Biological watchdog · Dream cycle · Skill graduation Upstream LLM Topology Foreground · --upstream-url --swarm-nodes --worker-nodes --visual-nodes --coding-nodes --image-gen-nodes NodeCircuitBreaker · round-robin · per-node httpx

Figure 1 — Ghost Agent runtime topology. Panels group subsystems; coloured card borders trace the ownership family.

Process & thread model

The Ghost Agent process is a single FastAPI / uvicorn instance running on port 8000. All work happens inside one Python interpreter; concurrency comes from asyncio for I/O and threads for blocking operations:

SubsystemConcurrencyNotes
FastAPI request handlersasyncioStreaming responses use Server-Sent Events.
LLM calls (foreground & pools)asyncio + httpx.AsyncClientPer-pool round-robin with circuit breakers.
Memory writesthread-locked, atomic temp-file replaceEach store guards itself with threading.RLock.
Memory bus fan-outasyncio.gather + asyncio.to_threadPer-tier writes run in parallel.
Docker sandboxblocking docker SDK in worker threadContainer reused across calls; execute() wraps in timeout.
Biological watchdogasyncio daemon taskIdle dream + skill graduation; cancelled at shutdown.
APSchedulerinternal AsyncIO schedulerCron / interval triggers for user-defined tasks.

Data flow on a chat request

Detailed walkthrough lives in Request lifecycle. The short version:

  1. Client posts /api/chat with OpenAI-format messages and X-Ghost-Key.
  2. routes.py validates the key, picks streaming vs JSON, and hands the payload to GhostAgent.handle_chat.
  3. MemoryBus.hydrate_context fans out to vector / graph / skill / episodic stores and fuses results with Reciprocal Rank Fusion.
  4. The combined system prompt + history + hydration is forwarded to the upstream LLM via LLMClient.chat_completion.
  5. The agent streams chunks back, parses any <tool_call> XML, executes the tool inside the Docker sandbox, and re-injects results.
  6. Verifier / uncertainty modules optionally adjudicate before the final SSE chunk reaches the client.

Repository layout

Agent/
├── src/ghost_agent/
│   ├── main.py             CLI + FastAPI lifespan
│   ├── api/                routes.py, projects_routes.py, app.py
│   ├── core/               agent, llm, planning, dream, mcts, hypothesis,
│   │                       verifier, prompts, bus, context_manager, …
│   ├── memory/             vector, graph, profile, skills, journal,
│   │                       scratchpad, episodes, frontier, projects,
│   │                       adaptive_threshold, contradiction_log
│   ├── tools/              registry + 16 tool modules + qwen_bridge
│   ├── sandbox/docker.py
│   └── utils/              helpers, logging, sanitizer, token_counter
├── interface/
│   ├── server.py           Web UI proxy (port 8080)
│   ├── slack_project_commands.py
│   └── externals/
│       ├── slack_bot/
│       ├── tts_stt/
│       ├── image_generation/
│       └── clockwork_ghost/
└── Documentation/          (this set)