Files
konstruct/packages/orchestrator/orchestrator/main.py
Adolfo Delorenzo 8257c554d7 feat(01-02): Celery orchestrator — handle_message task, system prompt builder, LLM pool runner
- Create orchestrator/main.py: Celery app with Redis broker/backend, task_acks_late=True, 10-min timeout
- Create orchestrator/tasks.py: SYNC def handle_message (critical pattern: asyncio.run for async work)
  - Deserializes KonstructMessage, sets RLS context, loads agent from DB, calls run_agent
  - Retries up to 3x on deserialization failure
- Create orchestrator/agents/builder.py: build_system_prompt assembles system_prompt + identity + persona + AI transparency clause
- Create orchestrator/agents/runner.py: run_agent posts to llm-pool /complete via httpx, returns polite fallback on error
- Add Celery[redis] dependency to orchestrator pyproject.toml
- Create tests/integration/test_llm_fallback.py: 7 tests for fallback routing and 503 on total failure (LLM-01)
- Create tests/integration/test_llm_providers.py: 12 tests verifying all three providers configured correctly (LLM-02)
- All 19 integration tests pass
2026-03-23 10:06:44 -06:00

43 lines
1.3 KiB
Python

"""
Celery application for the Konstruct Agent Orchestrator.
Broker and result backend are both Redis (separate DB indexes to avoid
key collisions). Tasks are discovered automatically from orchestrator.tasks.
Usage (development):
celery -A orchestrator.main worker --loglevel=info
Usage (production — via Docker Compose):
celery -A orchestrator.main worker --loglevel=info --concurrency=4
"""
from __future__ import annotations
from celery import Celery
from shared.config import settings
app = Celery(
"konstruct_orchestrator",
broker=settings.celery_broker_url,
backend=settings.celery_result_backend,
include=["orchestrator.tasks"],
)
# ---------------------------------------------------------------------------
# Celery configuration
# ---------------------------------------------------------------------------
app.conf.update(
task_serializer="json",
accept_content=["json"],
result_serializer="json",
timezone="UTC",
enable_utc=True,
# Acknowledge tasks only after they complete (not on receipt)
# This ensures tasks are retried if the worker crashes mid-execution.
task_acks_late=True,
# Reject tasks that exceed 10 minutes — prevents runaway LLM calls
task_soft_time_limit=540,
task_time_limit=600,
)