System Context Construction
This page documents how the discretionary agent constructs the live trading environment and injects it into the LLM on every turn.
Turn Entry
Section titled “Turn Entry”Discretionary turns enter through the shared websocket endpoint:
/ws/agent- handled in
backend/services/agent/ws/handler.ts
The transport layer dispatches discretionary events to:
backend/services/autodiscretionary/turn-runner.ts
For a user message, the path is:
disc_messagehandleDiscMessage(...)runDiscTurnAndDrain(...)
If the thread is already running, the new message is queued into Redis instead of starting another concurrent turn.
Session / Thread Resolution
Section titled “Session / Thread Resolution”runDiscTurnAndDrain(...) first resolves:
- the thread
- the owning discretionary session
- the runtime scope
The runtime scope is:
getDiscRuntimeScope(session.id) -> `session:${sessionId}`This is the core isolation boundary for multi-agent-per-user support.
Model / Credential Resolution
Section titled “Model / Credential Resolution”The runner then resolves:
- the configured discretionary provider/model from the session
- the user’s active LLM key from
llm_api_keys - fallback key/provider if needed
System Prompt Composition
Section titled “System Prompt Composition”The discretionary system prompt is assembled in:
backend/services/autodiscretionary/system-prompt.ts
The final prompt is:
- base system prompt
- thread-type prompt
- formatted live trading context from
formatContextForPrompt(...) - optional portfolio strategy prompt
- alert-specific suffix for alert threads
- persistent prompt notes
Live Trading Context Builder
Section titled “Live Trading Context Builder”The live trading context is built in:
backend/services/autodiscretionary/context-builder.ts
Main entry point:
buildTradingContext(userId, runtimeScope, sessionId)This function consumes the canonical runtime snapshot:
buildDiscretionaryRuntimeSnapshot(...)
It then enriches that snapshot with prompt-specific material such as:
- recent closed-trade performance
- market metadata / funding / open interest
- execution config
- risk config
- prompt notes
- formatted market hours
Canonical Runtime Snapshot
Section titled “Canonical Runtime Snapshot”The canonical runtime snapshot lives in:
backend/services/autodiscretionary/runtime-snapshot.ts
This is the shared backend source for both:
- the UI live-state API
- the prompt context builder
It loads and normalizes:
- account state from Redis
- trade risk state from Redis
- time alerts from Redis
- merged persisted + runtime alerts
- trading notes from Postgres
- active trades from the trade ledger
- market conditions from Redis
- drawdown lockout from Redis
- market hours
- live prices
- runtime health
Tool Construction
Section titled “Tool Construction”Discretionary tools are built in:
backend/services/autodiscretionary/tools/index.ts
Main entry:
buildDiscretionaryTools(ctx, threadType, alertData, creds)Tool surface differs by thread type:
chatthreads: full discretionary toolsetalertthreads: scoped by alert type
Shared Agent Loop Invocation
Section titled “Shared Agent Loop Invocation”After prompt and tools are ready, discretionary still uses the shared agent core:
runAgentTurn(...)
So the discretionary runtime owns environment orchestration, but not a duplicate turn engine.