Implement Phase 1: LangGraph backend MVP

Sets up the full backend foundation for CouncilOS:

- CouncilState TypedDict with all required fields and LangGraph reducers
- Three agent nodes: master_agent (drafts), critic_agent (scores + routes),
  writer_agent (final polish)
- LangGraph graph with cyclic rework loop: Master → Critic → (score < 8:
  back to Master | score ≥ 8: Writer → END)
- Safety valve: MAX_ITERATIONS=5 prevents infinite loops
- FastAPI app with REST endpoints (POST /api/councils/run, GET /api/councils/run/{id})
  and WebSocket endpoint (/ws/council/{run_id}) for real-time agent status events
- In-memory RunStore for Phase 1 (PostgreSQL-backed in Phase 3)
- pytest test suite: state, routing logic, critic parser, agent nodes, API endpoints
- .env.example, .gitignore, docker-compose.yml, Dockerfile

https://claude.ai/code/session_01RfMpt3TbMjZEtK3CAyP5iQ
This commit is contained in:
Claude 2026-02-20 16:33:39 +00:00
parent 34dcfb3dcd
commit 797f02c74d
No known key found for this signature in database
24 changed files with 1472 additions and 0 deletions

59
backend/main.py Normal file
View file

@ -0,0 +1,59 @@
"""
CouncilOS FastAPI application entrypoint.
Start the server:
uvicorn main:app --reload --port 8000
API Overview:
POST /api/councils/run Start a council run
GET /api/councils/run/{run_id} Poll run status/result
GET /api/health Health check
WS /ws/council/{run_id} Real-time agent status events
"""
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from api.routes import router
from api.websocket import ws_router
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Application lifespan: startup and shutdown logic."""
print("CouncilOS API starting up...")
yield
print("CouncilOS API shutting down...")
app = FastAPI(
title="CouncilOS API",
description=(
"Backend for the CouncilOS multi-agent AI pipeline platform. "
"Orchestrates LangGraph council runs and streams real-time agent "
"status via WebSockets."
),
version="0.1.0",
lifespan=lifespan,
)
# CORS — allow all origins in development; tighten in production
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Mount REST routes under /api prefix
app.include_router(router, prefix="/api")
# Mount WebSocket routes (no prefix — path is /ws/council/{run_id})
app.include_router(ws_router)
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)