chain-rdeveloper portal
build

Core Service (svc-core)

Overview

svc-core is the main FastAPI backend. It owns the primary relational database (postgres-core) and is the single point of entry for all authenticated application logic. svc-bff forwards all frontend API calls here with an injected access token.

Internal port: 8080 (also exposed to host in dev for direct access)

Responsibilities

  • CRUD operations for all core domain entities

  • User provisioning (Auth0 → local user record)

  • Orchestrating AI operations via svc-ai

  • File parsing and management via docling

  • Knowledge base document storage (MinIO) and indexing (Qdrant, via svc-ai)

  • Graph knowledge base management (Neo4j, via svc-ai)

  • Conversation history persistence

  • Evaluation experiments and test suites

  • Simulation worlds and scenarios

API Domains

See spec/api/svc-core.md for the full auto-generated reference.

Router prefixDomain
/organisationsOrganisation CRUD
/projectsProject CRUD
/agentsAgent definitions, tool/knowledge/skill assignments
/modelsLLM model registry (proxies svc-ai)
/conversationsConversation list and detail
/filesFile upload, parsing, listing
/knowledgeKnowledge base CRUD, document upload with indexing status, POST /{id}/reindex and per-document reindex, graph management, POST /{id}/search retrieval
/repositoriesKnowledge repositories (collections of KBs), POST /{id}/search retrieval across members
/mcp_serversMCP server configuration — withdrawn: reads, history and deletes only; every write and probe answers 403 (app/services/mcp.py)
/toolsTool definitions
/skillsSkill graph definitions
/workersCustom worker definitions
/tasksTask definitions
/evaluationsA/B experiments and test suites
/simulationsSimulation worlds, scenarios, and runs
/statsToken usage statistics (proxies svc-ai)
/industriesIndustry reference data
/rolesRole reference data
/monitoringHealth and ping endpoints
/schemaDev — DB schema inspection
/scenario-loaderDev — seed/clear test data

User Provisioning

Auth0 is the identity source. The local users table stores a mirror record keyed on auth0_id. The frontend calls POST /users/me once after first login to provision the record. Subsequent calls to GET /users/me return the local record; a 404 (not 403) signals the account has not been provisioned yet.

LLM Model Registry

svc-core does not own the model registry — it proxies /models/* calls to svc-ai. Model IDs used in agent and conversation records are UUIDs from svc-ai's llm_models table. svc-core uses the /models/lookup endpoint on svc-ai to resolve provider metadata without cross-database foreign keys.

Conversation Persistence

Chat history is persisted in svc-core (conversations, chat_messages, chat_message_segments). When a model or agent is prompted via /models/{id}/prompt or /agents/{id}/prompt, svc-core calls svc-ai for the LLM execution and writes the result back to its own database.

A turn outlives the page

A turn is paid for from its first model call, so it runs to its end and is stored whether or not the person is still on the page. They come back to the answer, not to a half-run that was paid for and thrown away.

  • Agent chat (WebSocket, agents/router.py agent_prompt_ws). Every frame goes through ChatSocket (app/services/chat_socket.py). A send to a page that has closed is dropped, never raised into the turn. Before this, the first failed send tore down svc-ai's stream and stored the half-answer as an error.

    • Stop is explicit. fe-core sends {"type": "stop"} before it closes the socket. That alone cancels the step the turn is waiting on (ChatSocket.run), which closes svc-ai's stream and so its graph run. The turn is stored with what it had written, or *Generation stopped.*.

    • A pause nobody can now answer (the page closed, or Stop) is recorded as abandoned and its svc-ai checkpoint freed (see human-in-the-loop).

    • fe-core closes the socket when the chat page unmounts, without Stop. While an answer is coming it tells the person they can leave.

  • Model chat (POST /models/{id}/prompt) is a plain def handler. It runs in a worker thread that a dropped request cannot cancel, so the answer is stored either way.

    • Stop ends svc-ai's run. svc-ai's batch endpoint runs a call to its end whatever its client does, so the chat's Stop used to stop only the page waiting: the whole answer was paid for and stored. The call now goes through svc-ai's stream (ai_client._invoke_model_streamed). It runs as a task on the event loop (anyio.from_thread), in a copy of the request thread's context, so the usage capture and workspace reach it. Stop cancels that task, and closing the stream ends the run.

    • The page names its turn (turn_id on the request) so its Stop can reach it before the response arrives. The request returns with what the turn had written (stopped: true), as it is stored.

Running turns

Every chat turn is in a registry while it runs (app/services/turns.py): whose it is, the conversation it will be stored in, its status (running or awaiting_input), the person's message as it will be stored, and a stop hook. It leaves only once it has been stored, so a page that sees it gone reads the conversation and finds the answer.

  • GET /app/turns (?agent_id= or ?model_id=) lists the caller's running turns. An agent turn is listed in its chat's workspace only.

  • POST /app/turns/{id}/stop stops one of the caller's turns. It ends as Stop from its own page ends it, so a turn started before the person left, or in another window, can be stopped from here.

  • One turn at a time per conversation. A second turn is refused (an error frame with code: "busy", or a 409), since its history would not hold the first one's answer. A turn being stopped does not count.

  • A stopped answer is stored with status error and meta.stopped, so fe-core shows what it had written rather than an error. A stream cancelled this way reports no usage, so svc-ai's meter does not count the tokens generated before the stop. It guesses no cost for them.

  • It is held in memory, because a turn is a task in this process. svc-core runs as one process (one uvicorn worker, one replica). Serving it from more would need the registry, and its stop signal, behind a shared store.

fe-core's chat pages poll the list while a turn runs (hooks/useRunningTurns, every 3 s). The sidebar marks a conversation that is still answering, and lists a new chat before it is stored. Opening one shows its message, a status in place of the live stream, and a working Stop (util/runningTurns.ts). When the turn has gone, the conversation is read again.

Pinned by tests/test_chat_turn_outlives_the_page.py and tests/test_running_turns.py. The first makes the test client's sends fail once its client has closed, as uvicorn's do, because Starlette's test client otherwise accepts them.

Runtime Dependencies

  • postgres-core — primary database (db-network)

  • svc-ai — LLM execution, model registry, skill/worker execution, stats (backend-network)

  • Neo4j — graph knowledge bases (graph-db-network)

  • MinIO — knowledge document storage (via file-storage-network)

  • Qdrant — vector search for RAG (via svc-ai)

  • Docling — document parsing (file-storage-network)

Configuration

Key environment variables (set via dotenv/dev/postgres-core.env, dotenv/dev/environment.env, dotenv/dev/ai.env, dotenv/dev/neo4j.env, dotenv/dev/minio.env):

VariableDescription
ENVIRONMENTdevelopment / production
DEBUGEnable debug logging
LOG_LEVELLog verbosity
MINIO_ENDPOINTMinIO address (minio:9000)
QDRANT_URLQdrant address (http://qdrant:6333)
DOCLING_URLDocling address (http://docling:5001)
NEO4J_URINeo4j Bolt URI (bolt://neo4j:7687)

Networks

  • backend-network — receives traffic from svc-bff; calls svc-ai

  • db-network — postgres-core

  • file-storage-network — MinIO, Qdrant, Docling

  • graph-db-network — Neo4j