Request lifecycle

What happens between an HTTP POST /api/chat and the SSE chunk landing in the client.

[1] Client POST /api/chat with X-Ghost-Key + OpenAI-format messages [2] api/routes.py — auth, set request_id ContextVar, choose stream vs JSON [3] GhostAgent.handle_chat — classify thinking budget, sampling profile [4] MemoryBus.hydrate_context — parallel fetch from vector / graph / skill / episodic + RRF [5] tools.registry.get_active_tool_definitions — base tools + dynamic acquired/composed skills [6] ContextManager.compress_if_needed — L0..L4 progressive compression as required [7] LLMClient.chat_completion(stream=True) — circuit-breaker filtered, round-robin pool [8] Stream loop — accumulate <tool_call>XML, parse args, dispatch tool [9] tool_failure.classify → retry / fatal / diagnostic / unknown — fold result back [10] Verifier.verify_* (worker pool) — confirm/refute substantive evidence [11] Final SSE chunk + DONE marker; UncertaintyTracker may append a risk summary [12] Background: write events to journal · maybe trigger Dream

Figure 10 — Twelve-step request lifecycle.

Stage details

  1. HTTP entryroutes.py validates the X-Ghost-Key header. Streaming responses set the X-Request-ID response header for correlation with the agent log.
  2. Context setup — the request_id is bound to a contextvars.ContextVar so log lines from inside tool calls inherit it.
  3. Sampling decisionget_sampling_params picks coding vs general profile; classify_thinking_budget picks tight / extended / selfplay.
  4. Hydration — see memory hydration (RRF).
  5. Tool list — built per-turn so semantic skill routing reflects the current query.
  6. CompressionContextManager decides how aggressively to summarise, only escalating beyond L0 if the budget is > 60 % full.
  7. Upstream call — chat completion streamed back through LLMClient; circuit breaker skips OPEN nodes.
  8. Tool dispatch — XML tool blocks parsed; the registry's bound async lambda runs the tool inside the Docker sandbox.
  9. Failure handlingtool_failure.classify + retry/replan/diagnostic; fallback_chains may surface alternative tools.
  10. Verification — only invoked on substantive tool output (skips bookkeeping tools).
  11. Final response — risk summary from UncertaintyTracker appended if any unknown reaches impact ≥ 4.
  12. Background — events appended to the journal; the biological watchdog may decide it's time to trigger a dream cycle.