stakpak-server
HTTP/SSE runtime shell around stakpak-agent-core.
This crate owns session/run orchestration, API contracts, durable event streaming, checkpoint persistence, auth boundaries, and MCP tool bridge wiring.
What this crate owns
- REST + SSE API surface (Axum)
- Session runtime state machine (
SessionManager) - Per-run actor orchestration (
session_actor) - Durable per-session event log with replay (
EventLog) - Idempotency handling for mutating endpoints
- Checkpoint persistence envelope store
- MCP tool execution bridge (through
stakpak-mcp-client) - Runtime config exposure (
/v1/configandsession.config)
What this crate does not own
- Core agent loop logic (delegated to
stakpak-agent-core::run_agent) - Provider/client bootstrap policy from CLI profile resolution
- CLI/TUI UI behavior
High-level architecture
HTTP client
│
▼
Axum routes (routes.rs)
├─ auth boundary (public vs protected routers)
├─ request validation + idempotency
├─ SessionManager (start/cancel/command/run scope)
└─ spawn_session_actor(...)
session_actor
├─ load checkpoint/messages
├─ build AgentConfig + tool snapshot
├─ run stakpak_agent_core::run_agent(...)
├─ forward AgentEvent -> EventLog
└─ persist checkpoints (periodic + lifecycle hooks)
EventLog
├─ monotonic event ids per session
├─ replay ring buffer
└─ live broadcast stream (SSE)
Main components
routes.rs— all HTTP handlers and SSE stream endpointsession_manager.rs— run-scoped state machine (Idle|Starting|Running|Failed)session_actor.rs— adapter from server runtime torun_agentevent_log.rs— durable replay + live pub/subcheckpoint_store.rs— latest checkpoint envelope storageidempotency.rs—Idempotency-Keylookup/replay/conflict logicmessage_bridge.rs— temporary storage edge adapters (ChatMessage<->stakai::Message)auth.rs— bearer auth middlewareopenapi.rs— generated OpenAPI document definitionsstate.rs— sharedAppStateand MCP tool cache helpers
Run lifecycle
- Client calls
POST /v1/sessions/{id}/messageswith astakai::Message. - Route validates request and run-scope constraints.
SessionManager::start_runatomically reservesStarting { run_id }.- Actor boots and transitions state to
Running { run_id, handle }. - Actor calls
run_agentfromstakpak-agent-core. - Core events are published to
EventLogand exposed via SSE. - Checkpoints are flushed periodically and on terminal transitions.
SessionManager::mark_run_finishedmoves runtime state back toIdleorFailed.
API and runtime guarantees
- Run-scoped safety: run-mismatch returns conflict (prevents stale commands).
- Idempotency: mutating endpoints support
Idempotency-Keyreplay semantics. - Durable events: replay from
Last-Event-ID, withgap_detectedcontrol event when cursor is out of ring window. - Deterministic tooling per run: tools are snapshotted for the run; refresh affects new runs.
- StakAI-native boundary: session message input/output is
stakai::Message. - Runtime config visibility: session detail responses include a
configobject with effective runtime snapshot fields like:default_modelauto_approve_mode
Public vs protected routes
- Public:
GET /v1/healthGET /v1/openapi.json
- Protected:
- all
/v1/sessions/*,/v1/models,/v1/config, etc.
- all
Auth middleware is applied only to the protected router.
Running the server (operator quickstart)
The server runs as part of the autopilot runtime:
# Start full autopilot (server + gateway + scheduler)
Default bind is 127.0.0.1:4096.
If auth is enabled, provide a bearer token (Authorization: Bearer <token>) for protected routes.
Useful endpoints while serving
GET /v1/healthGET /v1/openapi.jsonPOST /v1/sessionsPOST /v1/sessions/{id}/messagesGET /v1/sessions/{id}/events(SSE)GET /v1/sessions/{id}/messagesPOST /v1/sessions/{id}/cancel
When gateway is enabled, these are also mounted:
GET /v1/gateway/statusGET /v1/gateway/channelsGET /v1/gateway/sessionsPOST /v1/gateway/send
Channel-related CLI commands
Channels are managed through the autopilot system:
# Add channels
# Validate configured channels
# Start everything (server + gateway + scheduler)
Relationship to core
stakpak-server should be treated as a runtime adapter + API shell.
All agent behavior (turn loop, approvals, retries, compaction, event semantics) should remain centralized in stakpak-agent-core.