"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
refactor(api): extract remaining routes into modules
shadowinlifefa52c0069b25
diff --git a/agent/api_server.py b/agent/api_server.pyindex 8f74c10a5..6b3b612ae 100644--- a/agent/api_server.py+++ b/agent/api_server.py@@ -23,14 +23,13 @@ from pathlib import Pathfrom typing import Any, Dict, List, Optionalfrom fastapi import BackgroundTasks, Depends, FastAPI, HTTPException, Query, Request, Security, status-from fastapi.responses import FileResponse, JSONResponse, StreamingResponse+from fastapi.responses import FileResponse, JSONResponsefrom fastapi.security import HTTPAuthorizationCredentials, HTTPBearerfrom pydantic import BaseModel, Fieldfrom fastapi.middleware.cors import CORSMiddlewarefrom rich.console import Consolefrom cli._version import __version__ as APP_VERSION-from src.goal.context import default_goal_criteriafrom src.ui_services import build_run_analysis, load_run_context# UTF-8 on Windows@@ -140,258 +139,10 @@ class RunResponse(BaseModel):+# Session/goal Pydantic models are defined in src/api/sessions_routes.py.-class CreateSessionRequest(BaseModel):- """Create session request body."""- title: str = Field("", description="Session title")- config: Optional[Dict[str, Any]] = Field(None, description="Session config")--class SessionResponse(BaseModel):- """Session record."""- session_id: str- title: str- status: str- created_at: str- updated_at: str- last_attempt_id: Optional[str] = None---class SendMessageRequest(BaseModel):- """Send chat message: natural-language strategy description."""- content: str = Field(..., description="Natural language strategy description", min_length=1, max_length=5000)---class MessageResponse(BaseModel):- """Stored chat message."""- message_id: str- session_id: str- role: str- content: str- created_at: str- linked_attempt_id: Optional[str] = None- metadata: Optional[Dict[str, Any]] = None---class CreateGoalRequest(BaseModel):- """Create or replace a finance research goal."""-- objective: str = Field(..., min_length=1, max_length=5000)- criteria: List[str] = Field(default_factory=list)- ui_summary: str = ""- protocol: str = "thesis_review"- risk_tier: str = "research_general"- token_budget: Optional[int] = Field(None, ge=1)- turn_budget: Optional[int] = Field(None, ge=1)- time_budget_seconds: Optional[int] = Field(None, ge=1)---class UpdateGoalRequest(BaseModel):- """Edit mutable finance research goal fields."""-- goal_id: str = Field(..., min_length=1)- expected_goal_id: str = Field(..., min_length=1)- objective: Optional[str] = Field(None, min_length=1, max_length=5000)- ui_summary: Optional[str] = Field(None, max_length=500)---class AddGoalEvidenceRequest(BaseModel):- """Append evidence to a finance research goal."""-- goal_id: str = Field(..., min_length=1)- expected_goal_id: str = Field(..., min_length=1)- text: str = Field(..., min_length=1, max_length=10000)- criterion_id: Optional[str] = None- claim_id: Optional[str] = None- evidence_type: str = "evidence"- tool_call_id: Optional[str] = None- run_id: Optional[str] = None- source_provider: Optional[str] = None- source_type: Optional[str] = None- source_uri: Optional[str] = None- symbol_universe: List[str] = Field(default_factory=list)- benchmark: List[str] = Field(default_factory=list)- timeframe: Optional[str] = None- method: Optional[str] = None- assumptions: Dict[str, Any] = Field(default_factory=dict)- artifact_path: Optional[str] = None- artifact_hash: Optional[str] = None- data_as_of: Optional[str] = None- confidence: Optional[str] = None- caveat: Optional[str] = None- contradicts_claim_ids: List[str] = Field(default_factory=list)---class GoalSnapshotResponse(BaseModel):- """Finance research goal snapshot."""-- goal: Dict[str, Any]- claims: List[Dict[str, Any]]- criteria: List[Dict[str, Any]]- evidence: List[Dict[str, Any]]- evidence_count: int = 0---class AddGoalEvidenceResponse(BaseModel):- """Response after appending goal evidence."""-- evidence: Dict[str, Any]- snapshot: GoalSnapshotResponse---class GoalAuditRowRequest(BaseModel):- """One criterion row for goal status audits."""-- criterion_id: str = Field(..., min_length=1)- result: str = Field(..., min_length=1)- evidence_ids: List[str] = Field(default_factory=list)- notes: str = ""---class UpdateGoalStatusRequest(BaseModel):- """Update a finance research goal status."""-- goal_id: str = Field(..., min_length=1)- expected_goal_id: str = Field(..., min_length=1)- status: str = Field(..., min_length=1)- audit: List[GoalAuditRowRequest] = Field(default_factory=list)- recap: Optional[str] = None---class UpdateGoalStatusResponse(BaseModel):- """Response after changing a goal status."""-- goal: Dict[str, Any]- snapshot: GoalSnapshotResponse---class UpdateGoalResponse(BaseModel):- """Response after editing a goal."""-- goal: Dict[str, Any]- snapshot: GoalSnapshotResponse---# ---- Live trading channel: consent commit + kill switch -------class CommitMandateRequest(BaseModel):- """Surface-originated mandate commit (Consent §1 / §3).-- This is the ONLY write path that activates a live-trading mandate. It is a- privileged HTTP action the user surface sends on an explicit click/keypress- — NOT a tool the agent model can call. ``consent_ack`` MUST be ``true``.- """-- broker: str = Field(..., min_length=1, max_length=64)- proposal_id: str = Field(..., pattern=r"^mp_[0-9a-f]{32}$")- selected_ordinal: int = Field(..., ge=1, le=10)- adjustments: Optional[Dict[str, Any]] = None- consent_ack: bool = Field(..., description="Explicit affirmative; must be true")- session_id: Optional[str] = None- account_ref: str = Field("", max_length=128)- lifetime_days: int = Field(30, ge=1, le=365)---class LiveHaltRequest(BaseModel):- """Trip or clear the live kill switch (Consent §4).-- Tripping/clearing is a privileged surface action, never an agent tool. When- ``broker`` is omitted the GLOBAL switch is used (halts every broker).- """-- broker: Optional[str] = Field(None, max_length=64)- reason: str = Field("user requested halt", max_length=500)- session_id: Optional[str] = None---class LiveAuthorizeRequest(BaseModel):- """Kick off (or describe) the OAuth bootstrap for a live broker (C2).-- Vibe-Trading never holds funds and never operates a venue, so the OAuth- bootstrap runs through the broker's own user-authorized device flow on the- client (CLI / desktop MCP), not a server-side redirect. This endpoint is the- web on-ramp: it tells a Web UI user exactly how to discover/start the flow.- """-- broker: str = Field(..., min_length=1, max_length=64)---class LiveRunnerControlRequest(BaseModel):- """Start or stop the persistent live runner for one broker (SPEC §7.5).-- The runner wakes on schedule/market events and trades autonomously inside a- committed mandate. Starting it is a privileged surface action, never an- agent tool. A committed, unexpired mandate must already exist.- """-- broker: str = Field(..., min_length=1, max_length=64)- session_id: Optional[str] = None---class BrokerAuthState(BaseModel):- """Per-broker authorization snapshot for ``GET /live/status``."""-- broker: str- oauth_token_present: bool = Field(..., description="Whether an OAuth token cache exists")- is_live_broker: bool = Field(..., description="Whether this key is a recognized live broker")---class MandateLimits(BaseModel):- """Flattened active-mandate limits surfaced to the UI (Mandate layer a/b)."""-- max_order_notional_usd: float- max_total_exposure_usd: float- max_leverage: float- max_trades_per_day: int- allowed_instruments: List[str]- account_funding_usd: float---class ActiveMandateState(BaseModel):- """Active-mandate snapshot with the expiry countdown (SPEC §9 dec. 2)."""-- broker: str- account_ref: str- created_at: str- expires_at: str- expires_in_seconds: Optional[int] = Field(- None, description="Seconds until expiry; negative when already expired"- )- expired: bool- limits: MandateLimits---class RunnerLivenessState(BaseModel):- """Runner liveness snapshot via the §7.5 liveness contract."""-- broker: str- alive: bool- last_tick: Optional[float] = Field(None, description="Unix epoch of last heartbeat tick")- last_tick_age_seconds: Optional[float] = None---class LiveBrokerStatus(BaseModel):- """Combined live-channel status for a single broker."""-- auth: BrokerAuthState- mandate: Optional[ActiveMandateState] = None- runner: RunnerLivenessState- halted: bool = Field(..., description="Per-broker OR global kill switch is tripped")---class LiveStatusResponse(BaseModel):- """Top-level live-channel status (C2)."""-- global_halted: bool = Field(..., description="Whether the GLOBAL kill switch is tripped")- brokers: List[LiveBrokerStatus]---class ChannelPairingCommandRequest(BaseModel):- """Pairing command executed through the IM control surface."""-- channel: str = Field(..., min_length=1, max_length=64)- command: str = Field("list", max_length=500)+# Live-trading Pydantic models are defined in src/api/live_routes.py.# ============================================================================@@ -577,6 +328,10 @@ from src.api.channels_routes import ( # noqa: E402_start_channel_runtime,_stop_channel_runtime,)+from src.api.scheduled_routes import ( # noqa: E402+ _start_scheduled_research_executor,+ _stop_scheduled_research_executor,+)@app.on_event("startup")@@ -1003,187 +758,6 @@ def _coerce_int(value: str, default: int) -> int:return default-def _load_json_file(path: Path) -> Optional[Dict[str, Any]]:- """Load JSON from disk if present."""- try:- if path.exists():- return json.loads(path.read_text(encoding="utf-8"))- except Exception:- pass- return None---def _load_csv_to_dict(path: Path, limit: Optional[int] = None) -> List[Dict[str, Any]]:- """Load CSV rows into a list of dictionaries."""- try:- if not path.exists():- return []- with path.open("r", encoding="utf-8", newline="") as handle:- rows = [dict(row) for row in csv.DictReader(handle)]- if limit is not None:- rows = rows[:limit]- return rows- except Exception:- return []----def _build_response_from_run_dir(- run_dir: Path,- elapsed: float,- *,- include_analysis: bool = False,- chart_symbol: Optional[str] = None,- chart_payload: str = "full",- chart_symbols_out: Optional[List[str]] = None,-) -> RunResponse:- """Build a run response from a persisted run directory."""- run_id = run_dir.name-- response = RunResponse(- status="unknown",- run_id=run_id,- elapsed_seconds=elapsed,- run_directory=str(run_dir),- )-- state_data = _load_json_file(run_dir / "state.json")- if state_data:- state_status = str(state_data.get("status") or "").lower()- if state_status == "success":- response.status = "success"- elif state_status == "failed":- response.status = "failed"- response.reason = state_data.get("reason", "")- else:- response.status = state_status or "unknown"- else:- response.status = "unknown"-- planner_path = run_dir / "planner_output.json"- response.planner_output = _load_json_file(planner_path)-- design_path = run_dir / "design_spec.json"- response.strategy_spec = _load_json_file(design_path)-- rag_path = run_dir / "rag_metadata.json"- rag_data = _load_json_file(rag_path)- if rag_data:- response.rag_selection = RAGSelection(- selected_api=rag_data.get("selected_api") or rag_data.get("api_code", ""),- selected_name=rag_data.get("selected_name") or rag_data.get("api_name", ""),- selected_score=float(rag_data.get("selected_score") or rag_data.get("score", 0.0)),- )-- metrics_path = run_dir / "artifacts" / "metrics.csv"- if metrics_path.exists():- metrics_dict_list = _load_csv_to_dict(metrics_path, limit=1)- if metrics_dict_list:- row = metrics_dict_list[0]- try:- # Pass ALL CSV columns to BacktestMetrics (extra="allow")- parsed: dict = {}- for k, v in row.items():- if not k or not v:- continue- try:- parsed[k] = int(float(v)) if k == "trade_count" or k == "max_consecutive_loss" else float(v)- except (ValueError, TypeError):- continue- if "final_value" in parsed:- response.metrics = BacktestMetrics(**parsed)- except (ValueError, TypeError):- pass--- artifacts_dir = run_dir / "artifacts"- if artifacts_dir.exists():- for file_path in artifacts_dir.iterdir():- if file_path.is_file():- file_type = file_path.suffix.lstrip(".")- response.artifacts.append(- Artifact(- name=file_path.name,- path=str(file_path),- type=file_type if file_type else "unknown",- size=file_path.stat().st_size,- exists=True,- )- )-- equity_path = run_dir / "artifacts" / "equity.csv"- if equity_path.exists():- response.artifacts_equity_csv = _load_csv_to_dict(equity_path)-- metrics_csv_path = run_dir / "artifacts" / "metrics.csv"- if metrics_csv_path.exists():- response.artifacts_metrics_csv = _load_csv_to_dict(metrics_csv_path)-- run_card_path = run_dir / "run_card.json"- if run_card_path.exists():- try:- response.run_card = json.loads(run_card_path.read_text(encoding="utf-8"))- except (json.JSONDecodeError, OSError):- pass-- llm_usage_path = run_dir / "llm_usage.json"- if llm_usage_path.exists():- try:- response.llm_usage = json.loads(llm_usage_path.read_text(encoding="utf-8"))- except (json.JSONDecodeError, OSError):- pass-- trades_path = run_dir / "artifacts" / "trades.csv"- if trades_path.exists():- response.artifacts_trades_csv = _load_csv_to_dict(trades_path)-- validation_path = run_dir / "artifacts" / "validation.json"- if validation_path.exists():- try:- response.validation = json.loads(validation_path.read_text(encoding="utf-8"))- except (json.JSONDecodeError, OSError):- pass-- if response.artifacts_equity_csv:- filtered_equity = []- for row in response.artifacts_equity_csv[:1000]:- filtered_row: Dict[str, Any] = {}- if "timestamp" in row:- filtered_row["time"] = row["timestamp"]- if "equity" in row:- filtered_row["equity"] = row["equity"]- if "drawdown" in row:- filtered_row["drawdown"] = row["drawdown"]- filtered_equity.append(filtered_row)- response.equity_curve = filtered_equity-- if response.artifacts_trades_csv:- response.trade_log = response.artifacts_trades_csv[:500]-- if include_analysis:- analysis = build_run_analysis(- run_dir,- symbols=[chart_symbol] if chart_symbol else None,- include_payload=chart_payload != "summary" or bool(chart_symbol),- include_symbol_list=chart_symbols_out is not None,- )- if chart_symbols_out is not None:- chart_symbols_out.extend(analysis.get("chart_symbols") or [])- response.run_stage = analysis.get("run_stage")- response.run_context = analysis.get("run_context")- response.price_series = analysis.get("price_series")- response.indicator_series = analysis.get("indicator_series")- response.trade_markers = analysis.get("trade_markers")- response.run_logs = analysis.get("run_logs")-- return response---def _run_response_payload(response: RunResponse) -> Dict[str, Any]:- """Return a JSON-ready payload for opt-in run response variants."""- return response.model_dump(mode="json")--# ============================================================================# Path-parameter validation# ============================================================================@@ -1210,202 +784,25 @@ def _validate_path_param(value: str, kind: str) -> None:# ============================================================================-# API Endpoints+# Runs routes - defined in src/api/runs_routes.py# ============================================================================[email protected]("/runs/{run_id}/code", dependencies=[Depends(require_auth)])-async def get_run_code(run_id: str):- """Return strategy source files for a run.-- Args:- run_id: Run identifier.-- Returns:- Map filename -> source text.- """- _validate_path_param(run_id, "run_id")- run_dir = RUNS_DIR / run_id / "code"- if not run_dir.exists():- raise HTTPException(status_code=404, detail=f"Code directory for run {run_id} not found")- result = {}- for f in ["signal_engine.py"]:- p = run_dir / f- if p.exists():- result[f] = p.read_text(encoding="utf-8")- return result--[email protected]("/runs/{run_id}/pine", dependencies=[Depends(require_auth)])-async def get_run_pine(run_id: str):- """Return Pine Script file for a run.+from src.api.runs_routes import register_runs_routes # noqa: E402+register_runs_routes(app)- Args:- run_id: Run identifier.-- Returns:- Object with pine script content and exists flag.- """- _validate_path_param(run_id, "run_id")- pine_path = RUNS_DIR / run_id / "artifacts" / "strategy.pine"- if not pine_path.exists():- return {"exists": False, "content": None}- return {- "exists": True,- "content": pine_path.read_text(encoding="utf-8"),- }--[email protected]("/runs/{run_id}", response_model=RunResponse, dependencies=[Depends(require_auth)])-async def get_run_result(- run_id: str,- chart_symbol: Optional[str] = Query(None, description="Opt in to chart payloads for a single symbol"),- chart_payload: Optional[str] = Query(- None,- description="Optional chart payload mode. Use 'summary' to omit chart rows and trade markers.",- ),-):- """Fetch details for a historical run by ``run_id``.-- The default response stays unchanged for existing consumers. Chart-heavy- optimizations are opt-in via query parameters.- """- _validate_path_param(run_id, "run_id")- if chart_payload not in (None, "summary"):- raise HTTPException(status_code=400, detail="invalid chart_payload")- run_dir = RUNS_DIR / run_id-- if not run_dir.exists():- raise HTTPException(- status_code=status.HTTP_404_NOT_FOUND,- detail=f"Run {run_id} not found"- )-- wants_chart_meta = bool(chart_payload or chart_symbol)- chart_symbols: List[str] = []- response = _build_response_from_run_dir(- run_dir,- elapsed=0.0,- include_analysis=True,- chart_symbol=chart_symbol,- chart_payload=chart_payload or "full",- chart_symbols_out=chart_symbols if wants_chart_meta else None,- )-- if wants_chart_meta:- payload = _run_response_payload(response)- payload["chart_symbols"] = chart_symbols- return JSONResponse(payload)-- return response--[email protected]("/runs", response_model=List[RunInfo], dependencies=[Depends(require_auth)])-async def list_runs(limit: int = 20):- """List recent runs with summary fields."""- limit = min(max(1, limit), 100)- runs_dir = RUNS_DIR-- if not runs_dir.exists():- return []-- run_dirs = sorted(- [d for d in runs_dir.iterdir() if d.is_dir()],- key=lambda x: x.name,- reverse=True- )-- results = []- for d in run_dirs[:limit]:- run_id = d.name-- # Status from state.json or artifacts- status_val = "unknown"- state_file = _load_json_file(d / "state.json")- if state_file:- status_val = str(state_file.get("status") or "unknown").lower()- elif (d / "artifacts" / "equity.csv").exists():- status_val = "success"- elif (d / "review_report.json").exists():- status_val = "success"-- # Parse created_at from run_id (YYYYMMDD_HHMMSS or run_YYYYMMDD_HHMMSS)- created_at = "Unknown"- if run_id.startswith("run_"):- parts = run_id.split('_')- if len(parts) >= 3:- d_str, t_str = parts[1], parts[2]- if len(d_str) == 8 and len(t_str) == 6:- created_at = f"{d_str[:4]}-{d_str[4:6]}-{d_str[6:8]} {t_str[:2]}:{t_str[2:4]}:{t_str[4:6]}"- elif "_" in run_id:- parts = run_id.split('_')- if len(parts) >= 2:- d_str, t_str = parts[0], parts[1]- if len(d_str) == 8 and len(t_str) == 6:- created_at = f"{d_str[:4]}-{d_str[4:6]}-{d_str[6:8]} {t_str[:2]}:{t_str[2:4]}:{t_str[4:6]}"-- if created_at == "Unknown":- mtime = datetime.fromtimestamp(d.stat().st_mtime)- created_at = mtime.strftime("%Y-%m-%d %H:%M:%S")-- prompt = None- req_file = d / "req.json"- planner_file = d / "planner_output.json"- if req_file.exists():- try:- req_data = json.loads(req_file.read_text(encoding="utf-8"))- prompt = req_data.get("prompt")- except (json.JSONDecodeError, OSError):- pass-- if not prompt and planner_file.exists():- try:- planner_data = json.loads(planner_file.read_text(encoding="utf-8"))- prompt = planner_data.get("user_goal") or planner_data.get("goal")- except (json.JSONDecodeError, OSError):- pass-- if not prompt:- prompt_file = d / "user_prompt.txt"- if prompt_file.exists():- prompt = prompt_file.read_text(encoding="utf-8").strip()-- total_return = None- sharpe = None- metrics_file = d / "artifacts" / "metrics.csv"- if metrics_file.exists():- try:- import csv- with open(metrics_file, 'r', encoding='utf-8') as f:- reader = csv.DictReader(f)- for row in reader:- total_return = float(row.get('total_return', 0) or 0)- sharpe = float(row.get('sharpe', 0) or 0)- break- except (OSError, ValueError):- pass-- run_context = load_run_context(d)- results.append(RunInfo(- run_id=run_id,- status=status_val,- created_at=created_at,- prompt=prompt or "Manual Analysis",- total_return=total_return,- sharpe=sharpe,- codes=run_context.get("codes") or [],- start_date=run_context.get("start_date"),- end_date=run_context.get("end_date"),- ))-- return results+# Re-export for test access via api_server.*+from src.api.runs_routes import ( # noqa: F401, E402+ _load_json_file,+ _load_csv_to_dict,+ _build_response_from_run_dir,+)# ============================================================================-# Session API+# Session service (shared by session routes, channels, scheduled, live)# ============================================================================_session_service = None-_goal_store = None_channel_runtime = None_channel_bus = None_channel_manager = None@@ -1468,422 +865,20 @@ def _get_channel_runtime():return _channel_runtime-def _get_goal_store():- """Return the shared finance goal store."""- global _goal_store- if _goal_store is None:- from src.goal import GoalStore-- _goal_store = GoalStore()- return _goal_store---def _get_existing_session_or_404(session_id: str):- svc = _get_session_service()- if not svc:- raise HTTPException(status_code=501, detail="Session runtime not enabled")- session = svc.get_session(session_id)- if not session:- raise HTTPException(status_code=404, detail=f"Session {session_id} not found")- return svc, session--[email protected]("/sessions", response_model=SessionResponse, status_code=status.HTTP_201_CREATED, dependencies=[Depends(require_auth)])-async def create_session(request: CreateSessionRequest):- """Create a chat session."""- svc = _get_session_service()- if not svc:- raise HTTPException(status_code=501, detail="Session runtime not enabled")- session = svc.create_session(title=request.title, config=request.config)- return SessionResponse(- session_id=session.session_id,- title=session.title,- status=session.status.value,- created_at=session.created_at,- updated_at=session.updated_at,- last_attempt_id=session.last_attempt_id,- )--[email protected]("/sessions", response_model=List[SessionResponse], dependencies=[Depends(require_auth)])-async def list_sessions(limit: int = Query(50, ge=1, le=200)):- """List sessions."""- svc = _get_session_service()- if not svc:- raise HTTPException(status_code=501, detail="Session runtime not enabled")- sessions = svc.list_sessions(limit=limit)- return [- SessionResponse(- session_id=s.session_id,- title=s.title,- status=s.status.value,- created_at=s.created_at,- updated_at=s.updated_at,- last_attempt_id=s.last_attempt_id,- )- for s in sessions- ]--[email protected]("/sessions/{session_id}", response_model=SessionResponse, dependencies=[Depends(require_auth)])-async def get_session(session_id: str):- """Get one session by id."""- _validate_path_param(session_id, "session_id")- svc = _get_session_service()- if not svc:- raise HTTPException(status_code=501, detail="Session runtime not enabled")- session = svc.get_session(session_id)- if not session:- raise HTTPException(status_code=404, detail=f"Session {session_id} not found")- return SessionResponse(- session_id=session.session_id,- title=session.title,- status=session.status.value,- created_at=session.created_at,- updated_at=session.updated_at,- last_attempt_id=session.last_attempt_id,- )--- "/sessions/{session_id}/goal",- response_model=GoalSnapshotResponse,- status_code=status.HTTP_201_CREATED,- dependencies=[Depends(require_auth)],-)-async def create_session_goal(session_id: str, req: CreateGoalRequest):- """Create or replace the current finance research goal for a session."""- _validate_path_param(session_id, "session_id")- svc, _session = _get_existing_session_or_404(session_id)- from src.goal import RiskTier-- criteria = [item.strip() for item in req.criteria if item.strip()]- if not criteria:- criteria = default_goal_criteria()- try:- risk_tier = RiskTier(req.risk_tier)- except ValueError as exc:- raise HTTPException(status_code=400, detail=f"invalid risk_tier: {req.risk_tier}") from exc- if risk_tier is RiskTier.LIVE_TRADING_OR_EXECUTION:- raise HTTPException(status_code=400, detail="live trading or execution goals are not supported")-- goal_store = _get_goal_store()- try:- goal = goal_store.replace_goal(- session_id=session_id,- objective=req.objective,- criteria=criteria,- ui_summary=req.ui_summary,- source="api",- protocol=req.protocol,- risk_tier=risk_tier,- token_budget=req.token_budget,- turn_budget=req.turn_budget,- time_budget_seconds=req.time_budget_seconds,- )- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc)) from exc- snapshot = goal_store.get_goal_snapshot(goal.goal_id)- if snapshot is None:- raise HTTPException(status_code=500, detail="Goal created but could not be reloaded")- svc.event_bus.emit(session_id, "goal.created", {"goal": snapshot["goal"]})- return snapshot--- "/sessions/{session_id}/goal",- response_model=GoalSnapshotResponse,- dependencies=[Depends(require_auth)],-)-async def get_session_goal(session_id: str):- """Return the current finance research goal snapshot for a session."""- _validate_path_param(session_id, "session_id")- _get_existing_session_or_404(session_id)- snapshot = _get_goal_store().get_current_snapshot(session_id)- if snapshot is None:- raise HTTPException(status_code=404, detail="No current goal")- return snapshot--- "/sessions/{session_id}/goal",- response_model=UpdateGoalResponse,- dependencies=[Depends(require_auth)],-)-async def update_session_goal(session_id: str, req: UpdateGoalRequest):- """Edit the current finance research goal without replacing the session."""- _validate_path_param(session_id, "session_id")- svc, _session = _get_existing_session_or_404(session_id)- from src.goal import StaleGoalError-- if req.objective is None and req.ui_summary is None:- raise HTTPException(status_code=400, detail="objective or ui_summary is required")+# ============================================================================+# Session routes - defined in src/api/sessions_routes.py+# ============================================================================- goal_store = _get_goal_store()- try:- goal = goal_store.update_goal(- session_id=session_id,- goal_id=req.goal_id,- expected_goal_id=req.expected_goal_id,- objective=req.objective,- ui_summary=req.ui_summary,- )- except StaleGoalError as exc:- raise HTTPException(status_code=409, detail=str(exc)) from exc- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc)) from exc-- snapshot = goal_store.get_goal_snapshot(goal.goal_id)- if snapshot is None:- raise HTTPException(status_code=500, detail="Goal snapshot could not be reloaded")- svc.event_bus.emit(session_id, "goal.updated", {"goal": snapshot["goal"], "snapshot": snapshot})- return {"goal": snapshot["goal"], "snapshot": snapshot}--- "/sessions/{session_id}/goal/evidence",- response_model=AddGoalEvidenceResponse,- status_code=status.HTTP_201_CREATED,- dependencies=[Depends(require_auth)],-)-async def add_session_goal_evidence(session_id: str, req: AddGoalEvidenceRequest):- """Append traceable evidence to the current finance research goal."""- _validate_path_param(session_id, "session_id")- svc, _session = _get_existing_session_or_404(session_id)- from dataclasses import asdict- from src.goal import EvidenceInput, StaleGoalError-- goal_store = _get_goal_store()- try:- evidence = goal_store.append_evidence(- session_id=session_id,- goal_id=req.goal_id,- expected_goal_id=req.expected_goal_id,- evidence=EvidenceInput(- criterion_id=req.criterion_id,- claim_id=req.claim_id,- evidence_type=req.evidence_type,- text=req.text,- tool_call_id=req.tool_call_id,- run_id=req.run_id,- source_provider=req.source_provider,- source_type=req.source_type,- source_uri=req.source_uri,- symbol_universe=req.symbol_universe,- benchmark=req.benchmark,- timeframe=req.timeframe,- method=req.method,- assumptions=req.assumptions,- artifact_path=req.artifact_path,- artifact_hash=req.artifact_hash,- data_as_of=req.data_as_of,- confidence=req.confidence,- caveat=req.caveat,- contradicts_claim_ids=req.contradicts_claim_ids,- ),- )- except StaleGoalError as exc:- raise HTTPException(status_code=409, detail=str(exc)) from exc- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc)) from exc-- snapshot = goal_store.get_goal_snapshot(req.goal_id)- if snapshot is None:- raise HTTPException(status_code=500, detail="Goal snapshot could not be reloaded")- svc.event_bus.emit(- session_id,- "goal.evidence",- {"evidence": asdict(evidence), "goal_id": req.goal_id},- )- return {"evidence": asdict(evidence), "snapshot": snapshot}+from src.api.sessions_routes import register_sessions_routes # noqa: E402+register_sessions_routes(app)- "/sessions/{session_id}/goal/status",- response_model=UpdateGoalStatusResponse,- dependencies=[Depends(require_auth)],+# Re-export for test monkeypatch compatibility+from src.api.sessions_routes import ( # noqa: F401, E402+ _goal_store,+ _live_action_frame_from_tool_result,+ _mandate_proposal_frame_from_tool_result,)-async def update_session_goal_status(session_id: str, req: UpdateGoalStatusRequest):- """Update the current finance research goal status."""- _validate_path_param(session_id, "session_id")- svc, _session = _get_existing_session_or_404(session_id)- from src.goal import AuditRow, GoalStatus, StaleGoalError-- try:- next_status = GoalStatus(req.status)- except ValueError as exc:- raise HTTPException(status_code=400, detail=f"invalid goal status: {req.status}") from exc-- goal_store = _get_goal_store()- try:- goal = goal_store.update_status(- session_id=session_id,- goal_id=req.goal_id,- expected_goal_id=req.expected_goal_id,- status=next_status,- audit=[- AuditRow(- criterion_id=row.criterion_id,- result=row.result,- evidence_ids=row.evidence_ids,- notes=row.notes,- )- for row in req.audit- ],- recap=req.recap,- )- except StaleGoalError as exc:- raise HTTPException(status_code=409, detail=str(exc)) from exc- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc)) from exc-- snapshot = goal_store.get_goal_snapshot(goal.goal_id)- if snapshot is None:- raise HTTPException(status_code=500, detail="Goal snapshot could not be reloaded")- svc.event_bus.emit(session_id, "goal.updated", {"goal": snapshot["goal"], "snapshot": snapshot})- return {"goal": snapshot["goal"], "snapshot": snapshot}--[email protected]("/sessions/{session_id}", dependencies=[Depends(require_auth)])-async def delete_session(session_id: str):- """Delete a session."""- _validate_path_param(session_id, "session_id")- svc = _get_session_service()- if not svc:- raise HTTPException(status_code=501, detail="Session runtime not enabled")- deleted = svc.delete_session(session_id)- if not deleted:- raise HTTPException(status_code=404, detail=f"Session {session_id} not found")- _get_goal_store().delete_session_goals(session_id)- return {"status": "deleted", "session_id": session_id}---class UpdateSessionRequest(BaseModel):- """Session update fields."""- title: Optional[str] = None--[email protected]("/sessions/{session_id}", dependencies=[Depends(require_auth)])-async def update_session(session_id: str, req: UpdateSessionRequest):- """Update session fields (e.g. title)."""- _validate_path_param(session_id, "session_id")- svc = _get_session_service()- if not svc:- raise HTTPException(status_code=501, detail="Session runtime not enabled")- session = svc.store.get_session(session_id)- if not session:- raise HTTPException(status_code=404, detail=f"Session {session_id} not found")- if req.title is not None:- session.title = req.title- from datetime import datetime- session.updated_at = datetime.now().isoformat()- svc.store.update_session(session)- return {"status": "updated", "session_id": session_id}--[email protected]("/sessions/{session_id}/messages", dependencies=[Depends(require_auth)])-async def send_message(session_id: str, payload: SendMessageRequest, http_request: Request):- """Send a user message and start the agent loop (natural language strategy)."""- _validate_path_param(session_id, "session_id")- svc = _get_session_service()- if not svc:- raise HTTPException(status_code=501, detail="Session runtime not enabled")- try:- result = await svc.send_message(- session_id=session_id,- content=payload.content,- include_shell_tools=_shell_tools_enabled_for_request(http_request),- )- return result- except ValueError as exc:- raise HTTPException(status_code=404, detail=str(exc))--[email protected]("/sessions/{session_id}/cancel", dependencies=[Depends(require_auth)])-async def cancel_session(session_id: str):- """Cancel the in-flight agent loop for this session."""- _validate_path_param(session_id, "session_id")- svc = _get_session_service()- if not svc:- raise HTTPException(status_code=501, detail="Session runtime not enabled")- cancelled = svc.cancel_current(session_id)- if not cancelled:- return {"status": "no_active_loop"}- return {"status": "cancelled"}--[email protected]("/sessions/{session_id}/messages", response_model=List[MessageResponse], dependencies=[Depends(require_auth)])-async def get_messages(session_id: str, limit: int = Query(100, ge=1, le=1000)):- """List messages for a session."""- _validate_path_param(session_id, "session_id")- svc = _get_session_service()- if not svc:- raise HTTPException(status_code=501, detail="Session runtime not enabled")- messages = svc.get_messages(session_id, limit=limit)- return [- MessageResponse(- message_id=m.message_id,- session_id=m.session_id,- role=m.role,- content=m.content,- created_at=m.created_at,- linked_attempt_id=m.linked_attempt_id,- metadata=m.metadata if m.metadata else None,- )- for m in messages- ]--[email protected]("/sessions/{session_id}/events", dependencies=[Depends(require_event_stream_auth)])-async def session_events(- session_id: str,- request: Request,- last_event_id: Optional[str] = Query(None, alias="Last-Event-ID"),- replay: Optional[str] = Query(None),-):- """SSE stream for agent events."""- _validate_path_param(session_id, "session_id")- svc = _get_session_service()- if not svc:- raise HTTPException(status_code=501, detail="Session runtime not enabled")- session = svc.get_session(session_id)- if not session:- raise HTTPException(status_code=404, detail=f"Session {session_id} not found")-- header_id = request.headers.get("Last-Event-ID")- event_id = header_id or last_event_id- replay_active = (replay or "").lower() == "active"- replay_all = False- if replay_active and not event_id and session.last_attempt_id:- attempt = svc.store.get_attempt(session_id, session.last_attempt_id)- attempt_status = getattr(attempt.status, "value", attempt.status) if attempt else None- replay_all = attempt_status == "running"-- async def event_generator():- async for event in svc.event_bus.subscribe(- session_id,- last_event_id=event_id,- replay_all=replay_all,- ):- if await request.is_disconnected():- break- yield event.to_sse()- relayed = _mandate_proposal_frame_from_tool_result(event)- if relayed is not None:- yield relayed- live_action = _live_action_frame_from_tool_result(event)- if live_action is not None:- yield live_action-- return StreamingResponse(- event_generator(),- media_type="text/event-stream",- headers={- "Cache-Control": "no-cache",- "Connection": "keep-alive",- "X-Accel-Buffering": "no",- },- )# ============================================================================@@ -1944,964 +939,52 @@ from src.api.channels_routes import ( # noqa: F401, E402)-# ============================================================================-# Swarm API-# ============================================================================--_swarm_runtime = None---def _get_swarm_runtime():- """Lazy-init SwarmRuntime singleton."""- global _swarm_runtime- if _swarm_runtime is not None:- return _swarm_runtime- from src.config import load_swarm_agent_config- from src.swarm.store import SwarmStore- from src.swarm.runtime import SwarmRuntime- swarm_dir = Path(__file__).resolve().parent / ".swarm" / "runs"- store = SwarmStore(base_dir=swarm_dir)- # Boot-time / operator-trusted: REST API callers cannot influence the- # config path. See docs/2026-05-25_swarm_mcp_tools_roadmap.md.- agent_config = load_swarm_agent_config()- _swarm_runtime = SwarmRuntime(store=store, agent_config=agent_config)- return _swarm_runtime--[email protected]("/swarm/presets")-async def list_swarm_presets():- """List Swarm YAML presets."""- from src.swarm.presets import list_presets- return list_presets()--[email protected]("/swarm/runs", dependencies=[Depends(require_auth)])-async def create_swarm_run(payload: dict, http_request: Request):- """Start a swarm run: body must include preset_name and user_vars."""- runtime = _get_swarm_runtime()- preset_name = payload.get("preset_name", "")- user_vars = payload.get("user_vars", {})- try:- run = runtime.start_run(- preset_name,- user_vars,- include_shell_tools=_shell_tools_enabled_for_request(http_request),- )- return {"id": run.id, "status": run.status.value, "preset_name": run.preset_name}- except FileNotFoundError as e:- raise HTTPException(status_code=404, detail=str(e))- except ValueError as e:- raise HTTPException(status_code=400, detail=str(e))--[email protected]("/swarm/runs", dependencies=[Depends(require_auth)])-async def list_swarm_runs(limit: int = Query(20, ge=1, le=100)):- """List swarm runs (newest first), reconciled."""- runtime = _get_swarm_runtime()- runs = runtime._store.list_runs(limit=limit)- items = []- for r in runs:- # Reconcile each row: a zombie running run will be auto-finalized so- # the dashboard never shows a permanent "running" stuck row.- reconciled = runtime._store.reconcile_run(r, write=True)- items.append(- {- "id": reconciled.id,- "preset_name": reconciled.preset_name,- "status": reconciled.status.value,- "is_stale": runtime._store.is_run_stale(reconciled),- "created_at": reconciled.created_at,- "completed_at": reconciled.completed_at,- "task_count": len(reconciled.tasks),- "completed_count": sum(1 for t in reconciled.tasks if t.status.value == "completed"),- }- )- return items--[email protected]("/swarm/runs/{run_id}", dependencies=[Depends(require_auth)])-async def get_swarm_run(run_id: str):- """Swarm run detail including task statuses (reconciled)."""- _validate_path_param(run_id, "run_id")- runtime = _get_swarm_runtime()- loaded = runtime._store.load_run(run_id)- if not loaded:- raise HTTPException(status_code=404, detail=f"Run {run_id} not found")-- run = runtime._store.reconcile_run(loaded, write=True)-- return {- "id": run.id,- "preset_name": run.preset_name,- "status": run.status.value,- "is_stale": runtime._store.is_run_stale(run),- "user_vars": run.user_vars,- "agents": [a.model_dump() for a in run.agents],- "tasks": [t.model_dump() for t in run.tasks],- "created_at": run.created_at,- "completed_at": run.completed_at,- "final_report": run.final_report,- }--[email protected]("/swarm/runs/{run_id}/events", dependencies=[Depends(require_event_stream_auth)])-async def swarm_run_events(run_id: str, request: Request, last_index: int = Query(0, ge=0)):- """SSE stream for a swarm run."""- import asyncio-- _validate_path_param(run_id, "run_id")- runtime = _get_swarm_runtime()-- async def event_stream():- idx = last_index- while True:- if await request.is_disconnected():- break- events = runtime._store.read_events(run_id, after_index=idx)- for evt in events:- idx += 1- yield f"id: {idx}\nevent: {evt.type}\ndata: {json.dumps(evt.model_dump(), ensure_ascii=False)}\n\n"- run = runtime._store.load_run(run_id)- if run:- # Reconcile so a zombie running run can still close this SSE- # stream cleanly — without it, a dead host would keep the- # stream open forever and block the dashboard's "done" state.- reconciled = runtime._store.reconcile_run(run, write=True)- if reconciled.status.value in ("completed", "failed", "cancelled"):- yield f"event: done\ndata: {{\"status\": \"{reconciled.status.value}\"}}\n\n"- break- await asyncio.sleep(2)-- return StreamingResponse(event_stream(), media_type="text/event-stream")--[email protected]("/swarm/runs/{run_id}/cancel", dependencies=[Depends(require_auth)])-async def cancel_swarm_run(run_id: str):- """Cancel an active swarm run."""- _validate_path_param(run_id, "run_id")- runtime = _get_swarm_runtime()- ok = runtime.cancel_run(run_id)- if not ok:- raise HTTPException(status_code=404, detail=f"No active run {run_id}")- return {"status": "cancelled"}--[email protected]("/swarm/runs/{run_id}/retry", dependencies=[Depends(require_auth)])-async def retry_swarm_run(run_id: str, http_request: Request):- """Retry a failed, stale, or cancelled swarm run.-- Creates a new run with the same preset and user_vars as the original.- """- _validate_path_param(run_id, "run_id")- runtime = _get_swarm_runtime()- loaded = runtime._store.load_run(run_id)- if not loaded:- raise HTTPException(status_code=404, detail=f"Run {run_id} not found")-- # Reconcile first so a stale "running" run whose host died gets demoted- # before we gate on status; only a genuinely active run blocks retry.- from src.swarm.models import RunStatus-- reconciled = runtime._store.reconcile_run(loaded, write=True)- if reconciled.status == RunStatus.running:- raise HTTPException(status_code=409, detail="Cannot retry a running run. Cancel it first.")-- try:- new_run = runtime.start_run(- reconciled.preset_name,- reconciled.user_vars or {},- include_shell_tools=_shell_tools_enabled_for_request(http_request),- )- return {"id": new_run.id, "status": new_run.status.value, "preset_name": new_run.preset_name}- except FileNotFoundError as e:- raise HTTPException(status_code=404, detail=str(e))- except ValueError as e:- raise HTTPException(status_code=400, detail=str(e))-# ============================================================================-# Live trading channel — consent commit + kill switch+# Swarm routes - defined in src/api/swarm_routes.py# ============================================================================-#-# These are the privileged SURFACE actions of the live-trading channel-# (live-trading SPEC, Consent §1/§3/§4). None is an agent tool:-# - POST /mandate/commit -> the single mandate writer (commit_mandate)-# - POST /live/halt -> trip the kill switch (P5 trip_halt)-# - POST /live/resume -> clear the kill switch (P5 clear_halt)-# Each best-effort relays a mandate.committed / live.halted / live.action event-# through the EXISTING session EventBus, so the frontend's already-wired-# /sessions/{id}/events SSE stream reflects the state change. No new bus.---def _emit_live_event(session_id: Optional[str], event_type: str, data: Dict[str, Any]) -> None:- """Best-effort relay of a live-channel event through the existing bus.-- The event flows out the existing ``/sessions/{session_id}/events`` SSE- stream. Notifications never gate autonomy (SPEC Consent §5): a relay failure- or a missing session is swallowed — the state change already happened on disk.-- Args:- session_id: Target session, or ``None`` to skip relay.- event_type: SSE event name (``mandate.committed`` / ``live.halted`` /- ``live.resumed`` / ``live.action``).- data: JSON-serializable event payload.- """- if not session_id:- return- try:- svc = _get_session_service()- if svc and svc.get_session(session_id):- svc.event_bus.emit(session_id, event_type, data)- except Exception: # pragma: no cover - relay is non-blocking by contract- logger.debug("live event relay failed for %s/%s", session_id, event_type, exc_info=True)---# ---- C1: propose_mandate_profiles tool_result -> mandate.proposal SSE frame -----#-# The agent surfaces a proposal by calling the read-only ``propose_mandate_profiles``-# tool whose tool_result JSON body is ``{"type":"mandate.proposal", ...}`` (SPEC-# Consent §1). The CLI / frontend listen for a TOP-LEVEL ``mandate.proposal`` SSE-# event. ``src/agent/loop.py`` only emits a truncated ``tool_result`` event-# (``preview = result[:200]``) and is PROTECTED — we do NOT edit it. Instead this-# open-file SSE seam (TASKS "Remaining integration items" #1, the recommended-# wiring) detects the propose tool's tool_result on the stream, recovers the-# ``proposal_id`` from the preview, reloads the FULL persisted proposal from the-# proposal store (written by the tool before it returned), and emits the-# ``mandate.proposal`` frame. No protected touch.--_PROPOSAL_TOOL_NAME = "propose_mandate_profiles"-_PROPOSAL_ID_RE = re.compile(r'"proposal_id"\s*:\s*"(mp_[0-9a-f]{32})"')---def _load_full_proposal(proposal_id: str) -> Optional[Dict[str, Any]]:- """Reload a persisted ``mandate.proposal`` payload by id, broker-agnostic.-- The propose tool persists the full proposal under- ``<runtime_root>/live/<broker>/proposals/<proposal_id>.json`` before- returning. The SSE ``tool_result`` preview is too short to carry the full- body, so the relay reloads it from disk. The broker segment is unknown from- the preview alone, so every broker's proposals directory is searched.-- Args:- proposal_id: The ``mp_...`` id parsed from the tool_result preview.-- Returns:- The full proposal dict, or ``None`` when not found / unreadable.- """- try:- from src.live.paths import live_root-- for proposal_path in live_root().glob(f"*/proposals/{proposal_id}.json"):- try:- data = json.loads(proposal_path.read_text(encoding="utf-8"))- except (OSError, json.JSONDecodeError):- continue- if isinstance(data, dict) and data.get("type") == "mandate.proposal":- return data- except Exception: # pragma: no cover - relay must never break the stream- logger.debug("mandate.proposal reload failed for %s", proposal_id, exc_info=True)- return None---def _mandate_proposal_frame_from_tool_result(event: Any) -> Optional[str]:- """Build a ``mandate.proposal`` SSE frame from a propose-tool tool_result.-- Args:- event: An ``SSEEvent`` flowing through the session stream.-- Returns:- A ready-to-yield SSE text frame for the ``mandate.proposal`` event, or- ``None`` when ``event`` is not a successful propose-tool result or the- proposal cannot be recovered.- """- data = getattr(event, "data", None)- if getattr(event, "event_type", None) != "tool_result" or not isinstance(data, dict):- return None- if data.get("tool") != _PROPOSAL_TOOL_NAME or data.get("status") != "ok":- return None- match = _PROPOSAL_ID_RE.search(str(data.get("preview") or ""))- if not match:- return None- proposal = _load_full_proposal(match.group(1))- if proposal is None:- return None-- from src.session.events import SSEEvent-- frame = SSEEvent(- event_type="mandate.proposal",- data=proposal,- session_id=getattr(event, "session_id", "") or "",- )- return frame.to_sse()---_LIVE_ACTION_ID_RE = re.compile(r'"audit_id"\s*:\s*"(la_[0-9a-zA-Z]+)"')---def _load_live_action_record(audit_id: str) -> Optional[Dict[str, Any]]:- """Reload a redacted live-action record from the ledger by ``audit_id``.-- The order guard embeds its (already-redacted) audit record under the- ``live_action`` key of its tool_result, but the SSE ``tool_result`` preview- is truncated to ~200 chars, so the full record is reloaded from the- append-only ledger at ``<runtime_root>/live/audit.jsonl``.-- Args:- audit_id: The ``la_...`` id parsed from the tool_result preview.-- Returns:- The full redacted live-action record, or ``None`` when not found.- """- try:- from src.live.paths import live_root-- ledger = live_root() / "audit.jsonl"- if not ledger.exists():- return None- for line in reversed(ledger.read_text(encoding="utf-8").splitlines()):- if audit_id not in line:- continue- try:- record = json.loads(line)- except json.JSONDecodeError:- continue- if isinstance(record, dict) and record.get("audit_id") == audit_id:- return record- except Exception: # pragma: no cover - relay must never break the stream- logger.debug("live.action reload failed for %s", audit_id, exc_info=True)- return None---def _live_action_frame_from_tool_result(event: Any) -> Optional[str]:- """Build a ``live.action`` SSE frame from an order-guard tool_result.-- The order guard stamps a ``live_action`` audit record onto its tool_result- (and the ledger) for every live order placed/rejected. The interactive agent- loop only emits a truncated ``tool_result`` event and is PROTECTED, so this- open-file relay surfaces the live action as a top-level ``live.action`` event- for the timeline — without touching ``src/agent/loop.py``. (Autonomous-runner- actions already emit ``live.action`` natively via the runner's event bus.)-- Args:- event: An ``SSEEvent`` flowing through the session stream.-- Returns:- A ready-to-yield ``live.action`` SSE frame, or ``None`` when the event is- not an order-guard result carrying a recoverable live-action record.- """- data = getattr(event, "data", None)- if getattr(event, "event_type", None) != "tool_result" or not isinstance(data, dict):- return None- preview = str(data.get("preview") or "")- if '"live_action"' not in preview:- return None- match = _LIVE_ACTION_ID_RE.search(preview)- if not match:- return None- record = _load_live_action_record(match.group(1))- if record is None:- return None-- from src.session.events import SSEEvent-- frame = SSEEvent(- event_type="live.action",- data=record,- session_id=getattr(event, "session_id", "") or "",- )- return frame.to_sse()--def _fetch_broker_ceilings(broker: str) -> Optional[Dict[str, Any]]:- """Best-effort fetch of broker-side account ceilings for the commit re-check.+from src.api.swarm_routes import register_swarm_routes # noqa: E402- Reads the broker's mapped account/portfolio tool and derives an authoritative- ceiling snapshot (buying power / funding) so the commit-time fit check binds- to the venue's real limits rather than an agent-proposed number. Returns- ``None`` on any failure (channel not configured, tool error, fields not- recognized) so the caller falls back to the proposal's own snapshot — a- commit is never blocked on a broker read.+register_swarm_routes(app)- Args:- broker: The live-broker key.-- Returns:- A ceilings dict (canonical keys) or ``None`` to fall back.- """- try:- adapter = _live_broker_adapter(broker)- except LiveRunnerUnavailable:- return None- try:- from src.trading.service import runner_tool_name-- account_tool = runner_tool_name(broker, "account") or "get_account"- result = adapter.call_tool(account_tool, {})- except Exception: # pragma: no cover - status/commit must never raise here- logger.debug("broker ceiling fetch failed for %s", broker, exc_info=True)- return None- if not isinstance(result, dict) or result.get("status") == "error":- return None- payload = result.get("result") if isinstance(result.get("result"), dict) else result- funding: Optional[float] = None- for key in ("account_funding_usd", "buying_power", "cash", "portfolio_value", "equity"):- raw = payload.get(key) if isinstance(payload, dict) else None- try:- if raw is not None:- funding = float(raw)- break- except (TypeError, ValueError):- continue- if funding is None or funding <= 0:- return None- # A single order can never exceed available funding; total exposure is capped- # at funding for a cash account. Leverage stays at 1.0 unless the broker- # reports margin (L6). These canonical keys are normalized by commit_mandate.- return {- "account_funding_usd": funding,- "max_order_notional_usd": funding,- "max_total_exposure_usd": funding,- }--[email protected]("/mandate/commit", dependencies=[Depends(require_auth)])-async def commit_mandate_endpoint(payload: CommitMandateRequest):- """Commit a user-selected mandate profile — the only mandate write path.-- Calls :func:`src.live.mandate.commit.commit_mandate`, which re-validates the- proposal is live and the resolved profile still fits the ceilings the user- saw. Requires ``consent_ack=true`` (rejected otherwise). On success emits a- ``mandate.committed`` + ``live.action`` event so all surfaces reflect the- newly active mandate.- """- if payload.consent_ack is not True:- raise HTTPException(status_code=400, detail="consent_ack must be true to commit a mandate")-- from src.live.mandate.commit import CommitError, commit_mandate-- # Prefer broker-DERIVED ceilings over the agent-supplied proposal snapshot:- # the commit re-check should bind to the venue's real account limits, not a- # number the model proposed. Best-effort — falls back to the proposal's own- # ceilings (commit_mandate handles ceilings_ref=None) when the broker channel- # is unavailable or the read fails (we never block a commit on a broker read).- broker_ceilings = _fetch_broker_ceilings(payload.broker)-- try:- result = commit_mandate(- proposal_id=payload.proposal_id,- ordinal=payload.selected_ordinal,- adjustments=payload.adjustments,- consent_ack=payload.consent_ack,- broker=payload.broker,- account_ref=payload.account_ref,- session_id=payload.session_id,- ceilings_ref=broker_ceilings,- lifetime_days=payload.lifetime_days,- )- except CommitError as exc:- raise HTTPException(status_code=400, detail=str(exc)) from exc- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc)) from exc-- _emit_live_event(payload.session_id, "mandate.committed", result)- _emit_live_event(- payload.session_id,- "live.action",- {"kind": "mandate_committed", "broker": result["broker"], "mandate_id": result["mandate_id"]},- )- return result--[email protected]("/live/halt", dependencies=[Depends(require_auth)])-async def halt_live_endpoint(payload: LiveHaltRequest):- """Trip the live kill switch (privileged surface action, Consent §4).-- Writes the HALT sentinel via :func:`src.live.halt.trip_halt`; the- enforcement gate then rejects every order attempt until resumed. Emits a- ``live.halted`` event so all surfaces reflect the halted state.- """- from src.live.halt import trip_halt-- try:- path = trip_halt(by="frontend", reason=payload.reason, broker=payload.broker)- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc)) from exc-- result = {"halted": True, "broker": payload.broker, "reason": payload.reason, "sentinel": str(path)}- _emit_live_event(payload.session_id, "live.halted", result)- _emit_live_event(- payload.session_id,- "live.action",- {"kind": "halt_tripped", "broker": payload.broker, "reason": payload.reason},- )- return result--[email protected]("/live/resume", dependencies=[Depends(require_auth)])-async def resume_live_endpoint(payload: LiveHaltRequest):- """Clear the live kill switch (privileged surface action, Consent §4).-- Deletes the HALT sentinel via :func:`src.live.halt.clear_halt` (an explicit- re-enable; never an agent tool). Emits a ``live.resumed`` event.- """- from src.live.halt import clear_halt-- try:- cleared = clear_halt(broker=payload.broker)- except ValueError as exc:- raise HTTPException(status_code=400, detail=str(exc)) from exc-- result = {"halted": False, "broker": payload.broker, "cleared": cleared}- _emit_live_event(payload.session_id, "live.resumed", result)- _emit_live_event(- payload.session_id,- "live.action",- {"kind": "halt_cleared", "broker": payload.broker, "cleared": cleared},- )- return result+# Re-export for test monkeypatch compatibility+from src.api.swarm_routes import _get_swarm_runtime # noqa: F401, E402# ============================================================================-# Live trading channel — status, authorize on-ramp, runner control (C2 + §7.5)+# Live trading routes - defined in src/api/live_routes.py# ============================================================================-#-# C2 surfaces the dormant-by-default channel state so a user can SEE what is and-# is not authorized before trusting it: per-broker OAuth presence, the active-# mandate with its expiry countdown, runner liveness, and the kill-switch state.-# The runner-control endpoints start/stop the persistent §7.5 runner that trades-# autonomously inside a committed mandate. None of these is an agent tool; they-# are privileged surface actions like /mandate/commit and /live/halt.---def _known_live_brokers() -> List[str]:- """Return the recognized live-broker keys (SPEC §7.2)."""- from src.config.schema import LIVE_BROKER_SERVER_KEYS-- return sorted(LIVE_BROKER_SERVER_KEYS)---def _oauth_token_present(broker: str) -> bool:- """Return whether an OAuth token cache exists for a broker (C2 auth state).-- The token cache lives at ``<runtime_root>/live/<broker>/oauth/`` (0700) and- is created only when the user OAuth-authorizes the channel. A missing or- empty directory means the channel is dormant (read-only, no live path).- """- try:- from src.live.paths import broker_dir-- oauth_dir = broker_dir(broker) / "oauth"- return oauth_dir.is_dir() and any(oauth_dir.iterdir())- except Exception: # pragma: no cover - status must never raise- logger.debug("oauth presence check failed for %s", broker, exc_info=True)- return False---def _active_mandate_state(broker: str) -> Optional[ActiveMandateState]:- """Build the active-mandate snapshot for a broker, or ``None`` when absent.-- Reads the committed mandate via the frozen store contract and computes the- ``expires_at`` countdown (SPEC §9 dec. 2). A mandate whose ``expires_at`` has- passed is still surfaced, flagged ``expired`` so the UI can prompt re-consent.- """- from src.live.mandate.store import load_mandate-- mandate = load_mandate(broker)- if mandate is None:- return None-- consent = mandate.consent- caps = mandate.hard_caps- expires_in: Optional[int] = None- expired = False- try:- expires_dt = datetime.fromisoformat(consent.expires_at.replace("Z", "+00:00"))- from datetime import timezone-- now = datetime.now(timezone.utc)- if expires_dt.tzinfo is None:- expires_dt = expires_dt.replace(tzinfo=timezone.utc)- delta = expires_dt - now- expires_in = int(delta.total_seconds())- expired = expires_in <= 0- except (ValueError, AttributeError):- logger.debug("could not parse expires_at for %s mandate", broker, exc_info=True)-- return ActiveMandateState(- broker=broker,- account_ref=consent.account_ref,- created_at=consent.created_at,- expires_at=consent.expires_at,- expires_in_seconds=expires_in,- expired=expired,- limits=MandateLimits(- max_order_notional_usd=caps.max_order_notional_usd,- max_total_exposure_usd=caps.max_total_exposure_usd,- max_leverage=caps.max_leverage,- max_trades_per_day=caps.max_trades_per_day,- allowed_instruments=[str(getattr(i, "value", i)) for i in caps.allowed_instruments],- account_funding_usd=caps.account_funding_usd,- ),- )---def _runner_liveness_state(broker: str) -> RunnerLivenessState:- """Build the runner-liveness snapshot for a broker (SPEC §7.5 contract).-- Uses the §7.5 ``liveness`` module (``is_runner_alive`` / ``last_tick``),- keyed by broker as the runner id. The module is built concurrently (R1); a- missing module or any error is treated as "not alive" (fail-safe display).- """- alive = False- tick: Optional[float] = None- age: Optional[float] = None- try:- from src.live.runtime import liveness-- alive = bool(liveness.is_runner_alive(broker))- raw_tick = liveness.last_tick(broker)- if raw_tick is not None:- tick = float(raw_tick)- age = max(0.0, time.time() - tick)- except Exception: # pragma: no cover - liveness module is built concurrently- logger.debug("runner liveness lookup failed for %s", broker, exc_info=True)-- return RunnerLivenessState(broker=broker, alive=alive, last_tick=tick, last_tick_age_seconds=age)--[email protected]("/live/status", response_model=LiveStatusResponse, dependencies=[Depends(require_auth)])-async def live_status_endpoint(broker: Optional[str] = Query(None, max_length=64)):- """Return live-channel status: auth, active mandate, runner liveness, halt (C2).-- Args:- broker: Optional single-broker filter. When omitted, every recognized- live broker is reported.-- Returns:- A :class:`LiveStatusResponse` with the global kill-switch state and a- per-broker breakdown so the UI can show exactly what is authorized.- """- from src.live.halt import halt_flag_set-- if broker is not None:- target = broker.strip().lower()- if not target:- raise HTTPException(status_code=400, detail="broker must not be blank")- brokers = [target]- else:- brokers = _known_live_brokers()-- known = set(_known_live_brokers())- statuses: List[LiveBrokerStatus] = []- for key in brokers:- statuses.append(- LiveBrokerStatus(- auth=BrokerAuthState(- broker=key,- oauth_token_present=_oauth_token_present(key),- is_live_broker=key in known,- ),- mandate=_active_mandate_state(key),- runner=_runner_liveness_state(key),- halted=halt_flag_set(broker=key),- )- )-- return LiveStatusResponse(global_halted=halt_flag_set(broker=None), brokers=statuses)--[email protected]("/live/authorize", dependencies=[Depends(require_auth)])-async def live_authorize_endpoint(payload: LiveAuthorizeRequest):- """Describe the OAuth bootstrap on-ramp for a live broker (C2 web on-ramp).-- Vibe-Trading holds no funds and runs no venue: the OAuth flow happens on the- broker's own user-authorized device channel (CLI / desktop MCP), never a- server-side redirect. A Web UI user reaches this endpoint to DISCOVER how to- start the flow. It performs no authorization itself and never returns a token.- """- broker = payload.broker.strip().lower()- if not broker:- raise HTTPException(status_code=400, detail="broker must not be blank")- if broker not in set(_known_live_brokers()):- raise HTTPException(status_code=400, detail=f"unknown live broker: {broker}")-- from src.trading.service import connector_profile_id_for_broker-- connector_profile = connector_profile_id_for_broker(broker)- return {- "broker": broker,- "connector_profile": connector_profile,- "oauth_token_present": _oauth_token_present(broker),- "instruction": (- f"Run `vibe-trading connector authorize {connector_profile}` "- "from the device that will hold the broker session. This opens the "- "broker's own OAuth consent flow; Vibe-Trading never holds funds and "- "only relays intent once you authorize."- ),- "note": (- "The live channel stays read-only until the OAuth token is present AND a "- "mandate is committed AND order tools are explicitly enabled."- ),- }---# ---- Runner control (SPEC §7.5): start / stop the persistent live runner -----#-# A LiveRunner (R2 contract: ``LiveRunner(broker)`` with ``run_loop()`` /-# ``run_once()``) is driven in a background task per broker. The factory is-# injectable (``_runner_factory``) so tests stub it with no real agent/broker.-# ``run_loop`` may be sync (long-blocking) or async; both are supported.--_runner_tasks: Dict[str, "asyncio.Task[Any]"] = {}-_runner_factory: Optional[Any] = None---class LiveRunnerUnavailable(RuntimeError):- """Raised when a live runner cannot be wired (broker not configured/authorized).-- Distinct from a programming error so the start endpoint can map it to a 503- rather than a 500: the runtime is fine, the broker channel just isn't ready.- """---def _live_broker_adapter(broker: str) -> Any:- """Build an ``MCPServerAdapter`` for a live broker from the user-side config.-- Resolves the broker's MCP server entry by config key OR by a live-broker URL- host (so an aliased key still resolves), mirroring the registry's detection.-- Args:- broker: The live-broker key, e.g. ``"robinhood"``.-- Returns:- A constructed :class:`MCPServerAdapter` for the broker's read/write tools.-- Raises:- LiveRunnerUnavailable: When no MCP server is configured for the broker.- """- from src.config.loader import load_agent_config- from src.tools.mcp import MCPServerAdapter-- try:- from src.config.schema import is_live_broker_entry- except Exception: # pragma: no cover - older schema without URL detection- is_live_broker_entry = None # type: ignore[assignment]-- cfg = load_agent_config()- servers = getattr(cfg, "mcp_servers", {}) or {}- for name, server_cfg in servers.items():- is_match = name == broker- if not is_match and is_live_broker_entry is not None and broker == "robinhood":- try:- is_match = is_live_broker_entry(name, server_cfg)- except Exception: # pragma: no cover- is_match = False- if is_match:- return MCPServerAdapter(name, server_cfg)- raise LiveRunnerUnavailable(f"no MCP server configured for live broker {broker!r}")---def _build_live_runner(broker: str) -> Any:- """Construct a fully-wired ``LiveRunner`` for a broker (SPEC §7.5 R-INT).-- Wires the runner to the real surfaces — the public ``SessionService`` agent- caller (never the protected loop internals), the broker's READ/WRITE MCP- tools, the R4 reconciler, the R1 scheduler, and R3 market-hours triggers —- and injects an audit ``event_callback`` so every autonomous live action is- broadcast as a ``live.action`` SSE event on the runner's session bus.-- Args:- broker: The live-broker key.-- Returns:- A runner object exposing ``run_loop`` / ``run_once`` (R2 contract).-- Raises:- LiveRunnerUnavailable: When the broker channel is not configured.- """- if _runner_factory is not None:- return _runner_factory(broker)-- from src.live.audit import write_live_action- from src.live.runtime.reconcile import reconcile- from src.live.runtime.runner import LiveRunner- from src.live.runtime.scheduler import Scheduler- from src.live.runtime.triggers import Trigger- from src.trading.service import runner_tool_name-- def _tool(operation: str) -> str:- remote_tool = runner_tool_name(broker, operation)- if remote_tool is None:- raise LiveRunnerUnavailable(- f"live runner for {broker!r} does not define remote tool {operation!r}"- )- return remote_tool-- positions_tool = _tool("positions")- balance_tool = _tool("account")- open_orders_tool = _tool("orders")- submit_order_tool = _tool("submit_order")- cancel_order_tool = _tool("cancel_order")- adapter = _live_broker_adapter(broker) # raises LiveRunnerUnavailable if absent-- def _read(remote_tool: str):- """A zero-arg broker READ callable bound to one remote tool."""- return lambda: adapter.call_tool(remote_tool, {})-- def _submit(order: Dict[str, Any]) -> Dict[str, Any]:- # Route the flatten sweep's normalized order to the broker's write tools.- # Field mapping against the real Robinhood schema is finalized post-access- # (L6); the action discriminator is broker-agnostic.- if order.get("action") == "cancel":- return adapter.call_tool(cancel_order_tool, order)- return adapter.call_tool(submit_order_tool, order)-- svc = _get_session_service()- session = svc.create_session(title=f"live-runner:{broker}")- session_id = session.session_id-- async def _agent_caller(sid: str, prompt: str) -> Dict[str, Any]:- # Dispatch one autonomous turn through the PUBLIC SessionService entry.- # The agent then trades within the mandate via the gated order tools.- return await svc.send_message(sid, prompt)-- def _audit_with_bus(event: Any) -> Dict[str, Any]:- # Broadcast each live action as a live.action SSE event on the runner's- # session bus (no protected-loop touch — the runner owns its session).- return write_live_action(- event,- event_callback=lambda etype, record: svc.event_bus.emit(session_id, etype, record),- )-- # Wire the scheduler's fire callback to the runner's tick. The scheduler is- # constructed before the runner (it needs on_fire), and the runner needs the- # scheduler, so late-bind via a holder to break the cycle.- runner_holder: Dict[str, Any] = {}-- async def _on_fire(_job: Any) -> None:- runner = runner_holder.get("runner")- if runner is not None:- await runner.run_once()-- scheduler = Scheduler(_on_fire)-- runner = LiveRunner(- broker,- agent_caller=_agent_caller,- reconcile_fn=reconcile,- read_positions=_read(positions_tool),- read_balance=_read(balance_tool),- read_open_orders=_read(open_orders_tool),- submit_fn=_submit,- write_audit_fn=_audit_with_bus,- scheduler=scheduler,- triggers=[Trigger.market("us_equity")],- session_id=session_id,- )- runner_holder["runner"] = runner- return runner---async def _drive_runner(runner: Any) -> None:- """Run a runner's ``run_loop`` to completion, sync or async.-- A synchronous ``run_loop`` is offloaded to a worker thread so it does not- block the event loop; an async ``run_loop`` is awaited directly.- """- result = runner.run_loop()- if asyncio.iscoroutine(result):- await result- else:- await asyncio.get_running_loop().run_in_executor(None, lambda: result)--[email protected]("/live/runner/start", dependencies=[Depends(require_auth)])-async def start_runner_endpoint(payload: LiveRunnerControlRequest):- """Start the persistent live runner for a broker (SPEC §7.5).- Refuses to start unless a committed, unexpired mandate exists and the kill- switch is clear — the runner trades autonomously, so it must not start into a- dead/halted channel. Idempotent: a request for an already-running broker- returns ``already_running`` without spawning a second task.- """- from src.live.halt import halt_flag_set-- broker = payload.broker.strip().lower()- if not broker:- raise HTTPException(status_code=400, detail="broker must not be blank")- from src.trading.service import broker_supports_live_runner-- if not broker_supports_live_runner(broker):- raise HTTPException(- status_code=400,- detail=f"live runner is not supported for {broker}",- )-- existing = _runner_tasks.get(broker)- if existing is not None and not existing.done():- return {"broker": broker, "started": False, "already_running": True}-- mandate = _active_mandate_state(broker)- if mandate is None:- raise HTTPException(status_code=409, detail=f"no committed mandate for {broker}")- if mandate.expired:- raise HTTPException(status_code=409, detail=f"mandate for {broker} has expired; re-authorize first")- if halt_flag_set(broker=broker) or halt_flag_set(broker=None):- raise HTTPException(status_code=409, detail="kill switch is tripped; resume before starting the runner")-- try:- runner = _build_live_runner(broker)- except LiveRunnerUnavailable as exc:- raise HTTPException(status_code=503, detail=str(exc)) from exc- except Exception as exc:- raise HTTPException(status_code=500, detail=f"could not construct runner: {exc}") from exc-- task = asyncio.ensure_future(_drive_runner(runner))- _runner_tasks[broker] = task- task.add_done_callback(- lambda t, b=broker: _runner_tasks.pop(b, None) if _runner_tasks.get(b) is t else None- )-- _emit_live_event(- payload.session_id,- "live.action",- {"kind": "runner_started", "broker": broker},- )- return {"broker": broker, "started": True, "already_running": False}--[email protected]("/live/runner/stop", dependencies=[Depends(require_auth)])-async def stop_runner_endpoint(payload: LiveRunnerControlRequest):- """Stop the persistent live runner for a broker (SPEC §7.5).-- Cancels the background task. This does NOT flatten positions — that is the- preemptive kill switch's job (``/live/halt`` -> flatten); stopping the runner- simply ceases new autonomous turns. Idempotent for an already-stopped broker.- """- broker = payload.broker.strip().lower()- if not broker:- raise HTTPException(status_code=400, detail="broker must not be blank")- from src.trading.service import broker_supports_live_runner-- if not broker_supports_live_runner(broker):- raise HTTPException(- status_code=400,- detail=f"live runner is not supported for {broker}",- )+from src.api.live_routes import register_live_routes # noqa: E402- task = _runner_tasks.pop(broker, None)- if task is None or task.done():- return {"broker": broker, "stopped": False, "was_running": False}-- task.cancel()- _emit_live_event(- payload.session_id,- "live.action",- {"kind": "runner_stopped", "broker": broker},- )- return {"broker": broker, "stopped": True, "was_running": True}+register_live_routes(app)+# Re-export for test monkeypatch compatibility+from src.api.live_routes import ( # noqa: F401, E402+ CommitMandateRequest,+ LiveHaltRequest,+ LiveAuthorizeRequest,+ LiveRunnerControlRequest,+ BrokerAuthState,+ MandateLimits,+ ActiveMandateState,+ RunnerLivenessState,+ LiveBrokerStatus,+ LiveStatusResponse,+ LiveRunnerUnavailable,+ _runner_tasks,+ _runner_factory,+ _emit_live_event,+ _fetch_broker_ceilings,+ _known_live_brokers,+ _oauth_token_present,+ _active_mandate_state,+ _runner_liveness_state,+ _live_broker_adapter,+ _build_live_runner,+ _drive_runner,+)# ============================================================================# Alpha Zoo routes (Web UI) — defined in src/api/alpha_routes.py@@ -2912,163 +995,26 @@ register_alpha_routes(app)# ============================================================================-# Scheduled Research Routes+# Scheduled Research Routes - defined in src/api/scheduled_routes.py# ============================================================================## Lightweight CRUD endpoints backed by ScheduledResearchJobStore. The endpoint# handlers only record and expose jobs; the optional executor lifecycle is# guarded separately by VIBE_TRADING_ENABLE_SCHEDULER.+from src.api.scheduled_routes import register_scheduled_routes # noqa: E402-_SCHEDULED_RESEARCH_SCHEDULER_ENV = "VIBE_TRADING_ENABLE_SCHEDULER"-_SCHEDULED_RESEARCH_TRUE_VALUES = {"1", "true", "yes", "on"}--_scheduled_research_store: Optional["ScheduledResearchJobStore"] = None-_scheduled_research_executor: Optional["ScheduledResearchExecutor"] = None---def _get_scheduled_research_store() -> "ScheduledResearchJobStore":- """Return the singleton ScheduledResearchJobStore, creating it on first call."""- global _scheduled_research_store- if _scheduled_research_store is None:- from src.scheduled_research.store import ScheduledResearchJobStore-- _scheduled_research_store = ScheduledResearchJobStore()- return _scheduled_research_store---def _scheduled_research_scheduler_enabled() -> bool:- """Return whether scheduled research execution is enabled."""- return os.getenv(_SCHEDULED_RESEARCH_SCHEDULER_ENV, "").strip().lower() in _SCHEDULED_RESEARCH_TRUE_VALUES-+register_scheduled_routes(app)-async def _dispatch_scheduled_research_job(job: "ScheduledResearchJob") -> None:- """Enqueue one scheduled research job through the session runtime.-- ``send_message`` queues the agent attempt and returns once accepted; it- does not wait for that agent run to reach a terminal status. The executor's- ``COMPLETED`` state for this dispatch path means "successfully enqueued."- """- svc = _get_session_service()- if not svc:- raise RuntimeError("Session runtime not enabled")- # Pass a copy so the session runtime's internal config writes (e.g.- # include_shell_tools) do not mutate the persisted scheduled-run config.- session = svc.create_session(title=f"scheduled-research:{job.id}", config=dict(job.config))- logger.info("dispatching scheduled research job %s via session %s", job.id, session.session_id)- await svc.send_message(session.session_id, job.prompt)---def _get_scheduled_research_executor() -> "ScheduledResearchExecutor":- """Return the singleton scheduled research executor."""- global _scheduled_research_executor- if _scheduled_research_executor is None:- from src.scheduled_research.executor import ScheduledResearchExecutor-- _scheduled_research_executor = ScheduledResearchExecutor(- _get_scheduled_research_store(),- _dispatch_scheduled_research_job,- enabled=_scheduled_research_scheduler_enabled(),- )- return _scheduled_research_executor---def _start_scheduled_research_executor() -> None:- """Start scheduled research execution when explicitly enabled."""- if not _scheduled_research_scheduler_enabled():- return- _get_scheduled_research_executor().start()---async def _stop_scheduled_research_executor() -> None:- """Stop scheduled research execution if it was started."""- executor = _scheduled_research_executor- if executor is not None:- await executor.stop()---class CreateScheduledRunRequest(BaseModel):- """Request body for POST /scheduled-runs."""-- id: Optional[str] = Field(None, description="Job id; auto-generated UUID when omitted")- prompt: str = Field(..., min_length=1, description="Research prompt or backtest description")- schedule: str = Field(..., min_length=1, description="Interval-ms or 5-field cron expression")- next_run_at: Optional[int] = Field(None, description="Epoch-ms for next run; defaults to now")- config: Dict[str, Any] = Field(default_factory=dict, description="Optional backtest parameters")---class ScheduledRunResponse(BaseModel):- """API response for a single scheduled job."""-- id: str- prompt: str- schedule: str- next_run_at: int- status: str- created_at: int- config: Dict[str, Any] = Field(default_factory=dict)--- "/scheduled-runs",- response_model=ScheduledRunResponse,- status_code=status.HTTP_201_CREATED,- dependencies=[Depends(require_auth)],-)-async def create_scheduled_run(request: CreateScheduledRunRequest) -> ScheduledRunResponse:- """Create (or replace) a scheduled research job.-- The job is persisted immediately. No execution is triggered.- """- import time-- from src.scheduled_research.models import JobStatus, ScheduledResearchJob- from src.scheduled_research.models import validate_schedule-- try:- validate_schedule(request.schedule)- except ValueError as exc:- raise HTTPException(status_code=422, detail=str(exc)) from exc-- now_ms = int(time.time() * 1000)- job = ScheduledResearchJob(- id=request.id or str(uuid.uuid4()),- prompt=request.prompt,- schedule=request.schedule,- next_run_at=request.next_run_at if request.next_run_at is not None else now_ms,- status=JobStatus.PENDING,- created_at=now_ms,- config=request.config,- )- _get_scheduled_research_store().upsert(job)- return ScheduledRunResponse(**job.to_dict())--- "/scheduled-runs",- response_model=List[ScheduledRunResponse],- dependencies=[Depends(require_auth)],-)-async def list_scheduled_runs(- status_filter: Optional[str] = Query(None, alias="status"),- limit: int = Query(50, ge=1, le=200),-) -> List[ScheduledRunResponse]:- """List scheduled research jobs, optionally filtered by status."""- jobs = _get_scheduled_research_store().list_jobs(status=status_filter, limit=limit)- return [ScheduledRunResponse(**j.to_dict()) for j in jobs]--- "/scheduled-runs/{job_id}",- status_code=status.HTTP_204_NO_CONTENT,- dependencies=[Depends(require_auth)],+# Re-exported for backward-compatibility / external consumers+from src.api.scheduled_routes import ( # noqa: E402, F401+ CreateScheduledRunRequest,+ ScheduledRunResponse,+ _dispatch_scheduled_research_job,+ _get_scheduled_research_executor,+ _get_scheduled_research_store,+ _scheduled_research_scheduler_enabled,)-async def delete_scheduled_run(job_id: str) -> None:- """Cancel (delete) a scheduled research job by id."""- _validate_path_param(job_id, "job_id")- removed = _get_scheduled_research_store().delete(job_id)- if not removed:- raise HTTPException(status_code=404, detail=f"scheduled run {job_id} not found")# ============================================================================diff --git a/agent/src/api/live_routes.py b/agent/src/api/live_routes.pynew file mode 100644index 000000000..420dbbe3f--- /dev/null+++ b/agent/src/api/live_routes.py@@ -0,0 +1,691 @@+"""Live-trading channel HTTP routes (consent commit, kill switch, C2 status, runner control).++Mounted by ``agent/api_server.py`` via ``register_live_routes(app)``.++These are the privileged SURFACE actions of the live-trading channel+(live-trading SPEC, Consent §1/§3/§4). None is an agent tool:++- ``POST /mandate/commit`` — the single mandate writer (commit_mandate)+- ``POST /live/halt`` — trip the kill switch (P5 trip_halt)+- ``POST /live/resume`` — clear the kill switch (P5 clear_halt)+- ``GET /live/status`` — per-broker auth + mandate + runner + halt state (C2)+- ``POST /live/authorize`` — discover-only OAuth bootstrap on-ramp (C2)+- ``POST /live/runner/start`` — start the persistent §7.5 runner+- ``POST /live/runner/stop`` — stop the persistent §7.5 runner++Each best-effort relays a ``mandate.committed`` / ``live.halted`` / ``live.action``+event through the EXISTING session EventBus, so the frontend's already-wired+``/sessions/{id}/events`` SSE stream reflects the state change. No new bus.+"""++from __future__ import annotations++import asyncio+import logging+import sys as _sys+import time+from datetime import datetime+from typing import Any, Awaitable, Callable, Dict, List, Optional++from fastapi import Depends, FastAPI, HTTPException, Query+from pydantic import BaseModel, Field++logger = logging.getLogger(__name__)+++# ============================================================================+# Pydantic Models (live-exclusive; defined locally, monkeypatch-safe via re-export)+# ============================================================================+++class CommitMandateRequest(BaseModel):+ """Surface-originated mandate commit (Consent §1 / §3).++ This is the ONLY write path that activates a live-trading mandate. It is a+ privileged HTTP action the user surface sends on an explicit click/keypress+ — NOT a tool the agent model can call. ``consent_ack`` MUST be ``true``.+ """++ broker: str = Field(..., min_length=1, max_length=64)+ proposal_id: str = Field(..., pattern=r"^mp_[0-9a-f]{32}$")+ selected_ordinal: int = Field(..., ge=1, le=10)+ adjustments: Optional[Dict[str, Any]] = None+ consent_ack: bool = Field(..., description="Explicit affirmative; must be true")+ session_id: Optional[str] = None+ account_ref: str = Field("", max_length=128)+ lifetime_days: int = Field(30, ge=1, le=365)+++class LiveHaltRequest(BaseModel):+ """Trip or clear the live kill switch (Consent §4).++ Tripping/clearing is a privileged surface action, never an agent tool. When+ ``broker`` is omitted the GLOBAL switch is used (halts every broker).+ """++ broker: Optional[str] = Field(None, max_length=64)+ reason: str = Field("user requested halt", max_length=500)+ session_id: Optional[str] = None+++class LiveAuthorizeRequest(BaseModel):+ """Kick off (or describe) the OAuth bootstrap for a live broker (C2).++ Vibe-Trading never holds funds and never operates a venue, so the OAuth+ bootstrap runs through the broker's own user-authorized device flow on the+ client (CLI / desktop MCP), not a server-side redirect. This endpoint is the+ web on-ramp: it tells a Web UI user exactly how to discover/start the flow.+ """++ broker: str = Field(..., min_length=1, max_length=64)+++class LiveRunnerControlRequest(BaseModel):+ """Start or stop the persistent live runner for one broker (SPEC §7.5).++ The runner wakes on schedule/market events and trades autonomously inside a+ committed mandate. Starting it is a privileged surface action, never an+ agent tool. A committed, unexpired mandate must already exist.+ """++ broker: str = Field(..., min_length=1, max_length=64)+ session_id: Optional[str] = None+++class BrokerAuthState(BaseModel):+ """Per-broker authorization snapshot for ``GET /live/status``."""++ broker: str+ oauth_token_present: bool = Field(..., description="Whether an OAuth token cache exists")+ is_live_broker: bool = Field(..., description="Whether this key is a recognized live broker")+++class MandateLimits(BaseModel):+ """Flattened active-mandate limits surfaced to the UI (Mandate layer a/b)."""++ max_order_notional_usd: float+ max_total_exposure_usd: float+ max_leverage: float+ max_trades_per_day: int+ allowed_instruments: List[str]+ account_funding_usd: float+++class ActiveMandateState(BaseModel):+ """Active-mandate snapshot with the expiry countdown (SPEC §9 dec. 2)."""++ broker: str+ account_ref: str+ created_at: str+ expires_at: str+ expires_in_seconds: Optional[int] = Field(+ None, description="Seconds until expiry; negative when already expired"+ )+ expired: bool+ limits: MandateLimits+++class RunnerLivenessState(BaseModel):+ """Runner liveness snapshot via the §7.5 liveness contract."""++ broker: str+ alive: bool+ last_tick: Optional[float] = Field(None, description="Unix epoch of last heartbeat tick")+ last_tick_age_seconds: Optional[float] = None+++class LiveBrokerStatus(BaseModel):+ """Combined live-channel status for a single broker."""++ auth: BrokerAuthState+ mandate: Optional[ActiveMandateState] = None+ runner: RunnerLivenessState+ halted: bool = Field(..., description="Per-broker OR global kill switch is tripped")+++class LiveStatusResponse(BaseModel):+ """Top-level live-channel status (C2)."""++ global_halted: bool = Field(..., description="Whether the GLOBAL kill switch is tripped")+ brokers: List[LiveBrokerStatus]+++# ============================================================================+# Runner state (module-level; monkeypatched by tests via api_server re-export)+# ============================================================================++_runner_tasks: Dict[str, "asyncio.Task[Any]"] = {}+_runner_factory: Optional[Any] = None+++# ============================================================================+# Exception (live-exclusive)+# ============================================================================+++class LiveRunnerUnavailable(RuntimeError):+ """Raised when a live runner cannot be wired (broker not configured/authorized).++ Distinct from a programming error so the start endpoint can map it to a 503+ rather than a 500: the runtime is fine, the broker channel just isn't ready.+ """+++# ============================================================================+# Host resolution — shared deps and monkeypatched symbols+# ============================================================================+++def _host() -> Any:+ """Return the host ``api_server`` module for shared deps and monkeypatched symbols."""+ return _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")+++# ============================================================================+# Helper functions+# ============================================================================+++def _emit_live_event(session_id: Optional[str], event_type: str, data: Dict[str, Any]) -> None:+ """Best-effort relay of a live-channel event through the existing bus.++ The event flows out the existing ``/sessions/{session_id}/events`` SSE+ stream. Notifications never gate autonomy (SPEC Consent §5): a relay failure+ or a missing session is swallowed — the state change already happened on disk.+ """+ if not session_id:+ return+ try:+ svc = _host()._get_session_service()+ if svc and svc.get_session(session_id):+ svc.event_bus.emit(session_id, event_type, data)+ except Exception: # pragma: no cover - relay is non-blocking by contract+ logger.debug("live event relay failed for %s/%s", session_id, event_type, exc_info=True)+++def _live_broker_adapter(broker: str) -> Any:+ """Build an ``MCPServerAdapter`` for a live broker from the user-side config.++ Raises:+ LiveRunnerUnavailable: When no MCP server is configured for the broker.+ """+ from src.config.loader import load_agent_config+ from src.tools.mcp import MCPServerAdapter++ try:+ from src.config.schema import is_live_broker_entry+ except Exception: # pragma: no cover - older schema without URL detection+ is_live_broker_entry = None # type: ignore[assignment]++ cfg = load_agent_config()+ servers = getattr(cfg, "mcp_servers", {}) or {}+ for name, server_cfg in servers.items():+ is_match = name == broker+ if not is_match and is_live_broker_entry is not None and broker == "robinhood":+ try:+ is_match = is_live_broker_entry(name, server_cfg)+ except Exception: # pragma: no cover+ is_match = False+ if is_match:+ return MCPServerAdapter(name, server_cfg)+ raise LiveRunnerUnavailable(f"no MCP server configured for live broker {broker!r}")+++def _fetch_broker_ceilings(broker: str) -> Optional[Dict[str, Any]]:+ """Best-effort fetch of broker-side account ceilings for the commit re-check.++ Returns ``None`` on any failure so the caller falls back to the proposal's+ own snapshot — a commit is never blocked on a broker read.+ """+ h = _host()+ try:+ adapter = h._live_broker_adapter(broker)+ except LiveRunnerUnavailable:+ return None+ try:+ from src.trading.service import runner_tool_name++ account_tool = runner_tool_name(broker, "account") or "get_account"+ result = adapter.call_tool(account_tool, {})+ except Exception: # pragma: no cover - status/commit must never raise here+ logger.debug("broker ceiling fetch failed for %s", broker, exc_info=True)+ return None+ if not isinstance(result, dict) or result.get("status") == "error":+ return None+ payload = result.get("result") if isinstance(result.get("result"), dict) else result+ funding: Optional[float] = None+ for key in ("account_funding_usd", "buying_power", "cash", "portfolio_value", "equity"):+ raw = payload.get(key) if isinstance(payload, dict) else None+ try:+ if raw is not None:+ funding = float(raw)+ break+ except (TypeError, ValueError):+ continue+ if funding is None or funding <= 0:+ return None+ return {+ "account_funding_usd": funding,+ "max_order_notional_usd": funding,+ "max_total_exposure_usd": funding,+ }+++def _known_live_brokers() -> List[str]:+ """Return the recognized live-broker keys (SPEC §7.2)."""+ from src.config.schema import LIVE_BROKER_SERVER_KEYS++ return sorted(LIVE_BROKER_SERVER_KEYS)+++def _oauth_token_present(broker: str) -> bool:+ """Return whether an OAuth token cache exists for a broker (C2 auth state)."""+ try:+ from src.live.paths import broker_dir++ oauth_dir = broker_dir(broker) / "oauth"+ return oauth_dir.is_dir() and any(oauth_dir.iterdir())+ except Exception: # pragma: no cover - status must never raise+ logger.debug("oauth presence check failed for %s", broker, exc_info=True)+ return False+++def _active_mandate_state(broker: str) -> Optional[ActiveMandateState]:+ """Build the active-mandate snapshot for a broker, or ``None`` when absent."""+ from src.live.mandate.store import load_mandate++ mandate = load_mandate(broker)+ if mandate is None:+ return None++ consent = mandate.consent+ caps = mandate.hard_caps+ expires_in: Optional[int] = None+ expired = False+ try:+ expires_dt = datetime.fromisoformat(consent.expires_at.replace("Z", "+00:00"))+ from datetime import timezone++ now = datetime.now(timezone.utc)+ if expires_dt.tzinfo is None:+ expires_dt = expires_dt.replace(tzinfo=timezone.utc)+ delta = expires_dt - now+ expires_in = int(delta.total_seconds())+ expired = expires_in <= 0+ except (ValueError, AttributeError):+ logger.debug("could not parse expires_at for %s mandate", broker, exc_info=True)++ return ActiveMandateState(+ broker=broker,+ account_ref=consent.account_ref,+ created_at=consent.created_at,+ expires_at=consent.expires_at,+ expires_in_seconds=expires_in,+ expired=expired,+ limits=MandateLimits(+ max_order_notional_usd=caps.max_order_notional_usd,+ max_total_exposure_usd=caps.max_total_exposure_usd,+ max_leverage=caps.max_leverage,+ max_trades_per_day=caps.max_trades_per_day,+ allowed_instruments=[str(getattr(i, "value", i)) for i in caps.allowed_instruments],+ account_funding_usd=caps.account_funding_usd,+ ),+ )+++def _runner_liveness_state(broker: str) -> RunnerLivenessState:+ """Build the runner-liveness snapshot for a broker (SPEC §7.5 contract)."""+ alive = False+ tick: Optional[float] = None+ age: Optional[float] = None+ try:+ from src.live.runtime import liveness++ alive = bool(liveness.is_runner_alive(broker))+ raw_tick = liveness.last_tick(broker)+ if raw_tick is not None:+ tick = float(raw_tick)+ age = max(0.0, time.time() - tick)+ except Exception: # pragma: no cover - liveness module is built concurrently+ logger.debug("runner liveness lookup failed for %s", broker, exc_info=True)++ return RunnerLivenessState(broker=broker, alive=alive, last_tick=tick, last_tick_age_seconds=age)+++def _build_live_runner(broker: str) -> Any:+ """Construct a fully-wired ``LiveRunner`` for a broker (SPEC §7.5 R-INT).++ Wires the runner to the real surfaces — the public ``SessionService`` agent+ caller (never the protected loop internals), the broker's READ/WRITE MCP+ tools, the R4 reconciler, the R1 scheduler, and R3 market-hours triggers —+ and injects an audit ``event_callback`` so every autonomous live action is+ broadcast as a ``live.action`` SSE event on the runner's session bus.++ Raises:+ LiveRunnerUnavailable: When the broker channel is not configured.+ """+ h = _host()++ # _runner_factory is monkeypatched on host by tests+ factory = getattr(h, "_runner_factory", None)+ if factory is not None:+ return factory(broker)++ from src.live.audit import write_live_action+ from src.live.runtime.reconcile import reconcile+ from src.live.runtime.runner import LiveRunner+ from src.live.runtime.scheduler import Scheduler+ from src.live.runtime.triggers import Trigger+ from src.trading.service import runner_tool_name++ def _tool(operation: str) -> str:+ remote_tool = runner_tool_name(broker, operation)+ if remote_tool is None:+ raise LiveRunnerUnavailable(+ f"live runner for {broker!r} does not define remote tool {operation!r}"+ )+ return remote_tool++ positions_tool = _tool("positions")+ balance_tool = _tool("account")+ open_orders_tool = _tool("orders")+ submit_order_tool = _tool("submit_order")+ cancel_order_tool = _tool("cancel_order")++ # _live_broker_adapter is monkeypatched on host by tests+ adapter = h._live_broker_adapter(broker)++ def _read(remote_tool: str):+ return lambda: adapter.call_tool(remote_tool, {})++ def _submit(order: Dict[str, Any]) -> Dict[str, Any]:+ if order.get("action") == "cancel":+ return adapter.call_tool(cancel_order_tool, order)+ return adapter.call_tool(submit_order_tool, order)++ svc = h._get_session_service()+ session = svc.create_session(title=f"live-runner:{broker}")+ session_id = session.session_id++ async def _agent_caller(sid: str, prompt: str) -> Dict[str, Any]:+ return await svc.send_message(sid, prompt)++ def _audit_with_bus(event: Any) -> Dict[str, Any]:+ return write_live_action(+ event,+ event_callback=lambda etype, record: svc.event_bus.emit(session_id, etype, record),+ )++ runner_holder: Dict[str, Any] = {}++ async def _on_fire(_job: Any) -> None:+ runner = runner_holder.get("runner")+ if runner is not None:+ await runner.run_once()++ scheduler = Scheduler(_on_fire)++ runner = LiveRunner(+ broker,+ agent_caller=_agent_caller,+ reconcile_fn=reconcile,+ read_positions=_read(positions_tool),+ read_balance=_read(balance_tool),+ read_open_orders=_read(open_orders_tool),+ submit_fn=_submit,+ write_audit_fn=_audit_with_bus,+ scheduler=scheduler,+ triggers=[Trigger.market("us_equity")],+ session_id=session_id,+ )+ runner_holder["runner"] = runner+ return runner+++async def _drive_runner(runner: Any) -> None:+ """Run a runner's ``run_loop`` to completion, sync or async."""+ result = runner.run_loop()+ if asyncio.iscoroutine(result):+ await result+ else:+ await asyncio.get_running_loop().run_in_executor(None, lambda: result)+++# ============================================================================+# Route registration+# ============================================================================+++AuthDep = Callable[..., Awaitable[Any] | Any]+++def register_live_routes(+ app: FastAPI,+ require_auth: AuthDep | None = None,+) -> None:+ """Mount the live-trading routes onto ``app``."""+ h = _host()+ if h is None:+ raise RuntimeError(+ "register_live_routes: api_server module not in sys.modules; "+ "ensure api_server is imported before calling this function"+ )++ if require_auth is None:+ require_auth = h.require_auth++ # All route handlers resolve monkeypatched symbols from host at call time+ # via ``_host()`` so that ``monkeypatch.setattr(api_server, ...)`` works.++ @app.post("/mandate/commit", dependencies=[Depends(require_auth)])+ async def commit_mandate_endpoint(payload: CommitMandateRequest):+ """Commit a user-selected mandate profile — the only mandate write path."""+ if payload.consent_ack is not True:+ raise HTTPException(status_code=400, detail="consent_ack must be true to commit a mandate")++ from src.live.mandate.commit import CommitError, commit_mandate++ broker_ceilings = _host()._fetch_broker_ceilings(payload.broker)++ try:+ result = commit_mandate(+ proposal_id=payload.proposal_id,+ ordinal=payload.selected_ordinal,+ adjustments=payload.adjustments,+ consent_ack=payload.consent_ack,+ broker=payload.broker,+ account_ref=payload.account_ref,+ session_id=payload.session_id,+ ceilings_ref=broker_ceilings,+ lifetime_days=payload.lifetime_days,+ )+ except CommitError as exc:+ raise HTTPException(status_code=400, detail=str(exc)) from exc+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=str(exc)) from exc++ _host()._emit_live_event(payload.session_id, "mandate.committed", result)+ _host()._emit_live_event(+ payload.session_id,+ "live.action",+ {"kind": "mandate_committed", "broker": result["broker"], "mandate_id": result["mandate_id"]},+ )+ return result++ @app.post("/live/halt", dependencies=[Depends(require_auth)])+ async def halt_live_endpoint(payload: LiveHaltRequest):+ """Trip the live kill switch (privileged surface action, Consent §4)."""+ from src.live.halt import trip_halt++ try:+ path = trip_halt(by="frontend", reason=payload.reason, broker=payload.broker)+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=str(exc)) from exc++ result = {"halted": True, "broker": payload.broker, "reason": payload.reason, "sentinel": str(path)}+ _host()._emit_live_event(payload.session_id, "live.halted", result)+ _host()._emit_live_event(+ payload.session_id,+ "live.action",+ {"kind": "halt_tripped", "broker": payload.broker, "reason": payload.reason},+ )+ return result++ @app.post("/live/resume", dependencies=[Depends(require_auth)])+ async def resume_live_endpoint(payload: LiveHaltRequest):+ """Clear the live kill switch (privileged surface action, Consent §4)."""+ from src.live.halt import clear_halt++ try:+ cleared = clear_halt(broker=payload.broker)+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=str(exc)) from exc++ result = {"halted": False, "broker": payload.broker, "cleared": cleared}+ _host()._emit_live_event(payload.session_id, "live.resumed", result)+ _host()._emit_live_event(+ payload.session_id,+ "live.action",+ {"kind": "halt_cleared", "broker": payload.broker, "cleared": cleared},+ )+ return result++ @app.get("/live/status", response_model=LiveStatusResponse, dependencies=[Depends(require_auth)])+ async def live_status_endpoint(broker: Optional[str] = Query(None, max_length=64)):+ """Return live-channel status: auth, active mandate, runner liveness, halt (C2)."""+ from src.live.halt import halt_flag_set++ if broker is not None:+ target = broker.strip().lower()+ if not target:+ raise HTTPException(status_code=400, detail="broker must not be blank")+ brokers = [target]+ else:+ brokers = _known_live_brokers()++ known = set(_known_live_brokers())+ h = _host()+ statuses: List[LiveBrokerStatus] = []+ for key in brokers:+ statuses.append(+ LiveBrokerStatus(+ auth=BrokerAuthState(+ broker=key,+ oauth_token_present=_oauth_token_present(key),+ is_live_broker=key in known,+ ),+ mandate=h._active_mandate_state(key),+ runner=_runner_liveness_state(key),+ halted=halt_flag_set(broker=key),+ )+ )++ return LiveStatusResponse(global_halted=halt_flag_set(broker=None), brokers=statuses)++ @app.post("/live/authorize", dependencies=[Depends(require_auth)])+ async def live_authorize_endpoint(payload: LiveAuthorizeRequest):+ """Describe the OAuth bootstrap on-ramp for a live broker (C2 web on-ramp)."""+ broker = payload.broker.strip().lower()+ if not broker:+ raise HTTPException(status_code=400, detail="broker must not be blank")+ if broker not in set(_known_live_brokers()):+ raise HTTPException(status_code=400, detail=f"unknown live broker: {broker}")++ from src.trading.service import connector_profile_id_for_broker++ connector_profile = connector_profile_id_for_broker(broker)+ return {+ "broker": broker,+ "connector_profile": connector_profile,+ "oauth_token_present": _oauth_token_present(broker),+ "instruction": (+ f"Run `vibe-trading connector authorize {connector_profile}` "+ "from the device that will hold the broker session. This opens the "+ "broker's own OAuth consent flow; Vibe-Trading never holds funds and "+ "only relays intent once you authorize."+ ),+ "note": (+ "The live channel stays read-only until the OAuth token is present AND a "+ "mandate is committed AND order tools are explicitly enabled."+ ),+ }++ @app.post("/live/runner/start", dependencies=[Depends(require_auth)])+ async def start_runner_endpoint(payload: LiveRunnerControlRequest):+ """Start the persistent live runner for a broker (SPEC §7.5)."""+ from src.live.halt import halt_flag_set+ from src.trading.service import broker_supports_live_runner++ broker = payload.broker.strip().lower()+ if not broker:+ raise HTTPException(status_code=400, detail="broker must not be blank")++ if not broker_supports_live_runner(broker):+ raise HTTPException(+ status_code=400,+ detail=f"live runner is not supported for {broker}",+ )++ h = _host()+ tasks = h._runner_tasks++ existing = tasks.get(broker)+ if existing is not None and not existing.done():+ return {"broker": broker, "started": False, "already_running": True}++ mandate = h._active_mandate_state(broker)+ if mandate is None:+ raise HTTPException(status_code=409, detail=f"no committed mandate for {broker}")+ if mandate.expired:+ raise HTTPException(status_code=409, detail=f"mandate for {broker} has expired; re-authorize first")+ if halt_flag_set(broker=broker) or halt_flag_set(broker=None):+ raise HTTPException(status_code=409, detail="kill switch is tripped; resume before starting the runner")++ try:+ runner = h._build_live_runner(broker)+ except LiveRunnerUnavailable as exc:+ raise HTTPException(status_code=503, detail=str(exc)) from exc+ except Exception as exc:+ raise HTTPException(status_code=500, detail=f"could not construct runner: {exc}") from exc++ task = asyncio.ensure_future(h._drive_runner(runner))+ tasks[broker] = task+ task.add_done_callback(+ lambda t, b=broker: tasks.pop(b, None) if tasks.get(b) is t else None+ )++ h._emit_live_event(+ payload.session_id,+ "live.action",+ {"kind": "runner_started", "broker": broker},+ )+ return {"broker": broker, "started": True, "already_running": False}++ @app.post("/live/runner/stop", dependencies=[Depends(require_auth)])+ async def stop_runner_endpoint(payload: LiveRunnerControlRequest):+ """Stop the persistent live runner for a broker (SPEC §7.5)."""+ from src.trading.service import broker_supports_live_runner++ broker = payload.broker.strip().lower()+ if not broker:+ raise HTTPException(status_code=400, detail="broker must not be blank")++ if not broker_supports_live_runner(broker):+ raise HTTPException(+ status_code=400,+ detail=f"live runner is not supported for {broker}",+ )++ h = _host()+ tasks = h._runner_tasks+ task = tasks.pop(broker, None)+ if task is None or task.done():+ return {"broker": broker, "stopped": False, "was_running": False}++ task.cancel()+ h._emit_live_event(+ payload.session_id,+ "live.action",+ {"kind": "runner_stopped", "broker": broker},+ )+ return {"broker": broker, "stopped": True, "was_running": True}diff --git a/agent/src/api/runs_routes.py b/agent/src/api/runs_routes.pynew file mode 100644index 000000000..bf7e62533--- /dev/null+++ b/agent/src/api/runs_routes.py@@ -0,0 +1,437 @@+"""Run listing and detail HTTP routes.++Mounted by ``agent/api_server.py`` via ``register_runs_routes(app, ...)``.+"""++from __future__ import annotations++import csv+import json+from datetime import datetime+from pathlib import Path+from typing import Any, Awaitable, Callable, Dict, List, Optional++from fastapi import Depends, FastAPI, HTTPException, Query, status+from fastapi.responses import JSONResponse+++# ---------------------------------------------------------------------------+# Helper functions (module-level; host Pydantic models resolved via sys.modules)+# ---------------------------------------------------------------------------+++def _load_json_file(path: Path) -> Optional[Dict[str, Any]]:+ """Load JSON from disk if present."""+ try:+ if path.exists():+ return json.loads(path.read_text(encoding="utf-8"))+ except Exception:+ pass+ return None+++def _load_csv_to_dict(path: Path, limit: Optional[int] = None) -> List[Dict[str, Any]]:+ """Load CSV rows into a list of dictionaries."""+ try:+ if not path.exists():+ return []+ with path.open("r", encoding="utf-8", newline="") as handle:+ rows = [dict(row) for row in csv.DictReader(handle)]+ if limit is not None:+ rows = rows[:limit]+ return rows+ except Exception:+ return []+++def _run_response_payload(response: Any) -> Dict[str, Any]:+ """Return a JSON-ready payload for opt-in run response variants."""+ return response.model_dump(mode="json")+++def _build_response_from_run_dir(+ run_dir: Path,+ elapsed: float,+ *,+ include_analysis: bool = False,+ chart_symbol: Optional[str] = None,+ chart_payload: str = "full",+ chart_symbols_out: Optional[List[str]] = None,+):+ """Build a run response from a persisted run directory."""+ import sys as _sys++ host = _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")+ RunResponse = host.RunResponse+ BacktestMetrics = host.BacktestMetrics+ RAGSelection = host.RAGSelection+ Artifact = host.Artifact++ run_id = run_dir.name++ response = RunResponse(+ status="unknown",+ run_id=run_id,+ elapsed_seconds=elapsed,+ run_directory=str(run_dir),+ )++ state_data = _load_json_file(run_dir / "state.json")+ if state_data:+ state_status = str(state_data.get("status") or "").lower()+ if state_status == "success":+ response.status = "success"+ elif state_status == "failed":+ response.status = "failed"+ response.reason = state_data.get("reason", "")+ else:+ response.status = state_status or "unknown"+ else:+ response.status = "unknown"++ planner_path = run_dir / "planner_output.json"+ response.planner_output = _load_json_file(planner_path)++ design_path = run_dir / "design_spec.json"+ response.strategy_spec = _load_json_file(design_path)++ rag_path = run_dir / "rag_metadata.json"+ rag_data = _load_json_file(rag_path)+ if rag_data:+ response.rag_selection = RAGSelection(+ selected_api=rag_data.get("selected_api") or rag_data.get("api_code", ""),+ selected_name=rag_data.get("selected_name") or rag_data.get("api_name", ""),+ selected_score=float(rag_data.get("selected_score") or rag_data.get("score", 0.0)),+ )++ metrics_path = run_dir / "artifacts" / "metrics.csv"+ if metrics_path.exists():+ metrics_dict_list = _load_csv_to_dict(metrics_path, limit=1)+ if metrics_dict_list:+ row = metrics_dict_list[0]+ try:+ # Pass ALL CSV columns to BacktestMetrics (extra="allow")+ parsed: dict = {}+ for k, v in row.items():+ if not k or not v:+ continue+ try:+ parsed[k] = int(float(v)) if k == "trade_count" or k == "max_consecutive_loss" else float(v)+ except (ValueError, TypeError):+ continue+ if "final_value" in parsed:+ response.metrics = BacktestMetrics(**parsed)+ except (ValueError, TypeError):+ pass++ artifacts_dir = run_dir / "artifacts"+ if artifacts_dir.exists():+ for file_path in artifacts_dir.iterdir():+ if file_path.is_file():+ file_type = file_path.suffix.lstrip(".")+ response.artifacts.append(+ Artifact(+ name=file_path.name,+ path=str(file_path),+ type=file_type if file_type else "unknown",+ size=file_path.stat().st_size,+ exists=True,+ )+ )++ equity_path = run_dir / "artifacts" / "equity.csv"+ if equity_path.exists():+ response.artifacts_equity_csv = _load_csv_to_dict(equity_path)++ metrics_csv_path = run_dir / "artifacts" / "metrics.csv"+ if metrics_csv_path.exists():+ response.artifacts_metrics_csv = _load_csv_to_dict(metrics_csv_path)++ run_card_path = run_dir / "run_card.json"+ if run_card_path.exists():+ try:+ response.run_card = json.loads(run_card_path.read_text(encoding="utf-8"))+ except (json.JSONDecodeError, OSError):+ pass++ llm_usage_path = run_dir / "llm_usage.json"+ if llm_usage_path.exists():+ try:+ response.llm_usage = json.loads(llm_usage_path.read_text(encoding="utf-8"))+ except (json.JSONDecodeError, OSError):+ pass++ trades_path = run_dir / "artifacts" / "trades.csv"+ if trades_path.exists():+ response.artifacts_trades_csv = _load_csv_to_dict(trades_path)++ validation_path = run_dir / "artifacts" / "validation.json"+ if validation_path.exists():+ try:+ response.validation = json.loads(validation_path.read_text(encoding="utf-8"))+ except (json.JSONDecodeError, OSError):+ pass++ if response.artifacts_equity_csv:+ filtered_equity = []+ for row in response.artifacts_equity_csv[:1000]:+ filtered_row: Dict[str, Any] = {}+ if "timestamp" in row:+ filtered_row["time"] = row["timestamp"]+ if "equity" in row:+ filtered_row["equity"] = row["equity"]+ if "drawdown" in row:+ filtered_row["drawdown"] = row["drawdown"]+ filtered_equity.append(filtered_row)+ response.equity_curve = filtered_equity++ if response.artifacts_trades_csv:+ response.trade_log = response.artifacts_trades_csv[:500]++ if include_analysis:+ from src.ui_services import build_run_analysis++ analysis = build_run_analysis(+ run_dir,+ symbols=[chart_symbol] if chart_symbol else None,+ include_payload=chart_payload != "summary" or bool(chart_symbol),+ include_symbol_list=chart_symbols_out is not None,+ )+ if chart_symbols_out is not None:+ chart_symbols_out.extend(analysis.get("chart_symbols") or [])+ response.run_stage = analysis.get("run_stage")+ response.run_context = analysis.get("run_context")+ response.price_series = analysis.get("price_series")+ response.indicator_series = analysis.get("indicator_series")+ response.trade_markers = analysis.get("trade_markers")+ response.run_logs = analysis.get("run_logs")++ return response+++# ---------------------------------------------------------------------------+# Registration+# ---------------------------------------------------------------------------++AuthDep = Callable[..., Awaitable[Any] | Any]+++def register_runs_routes(+ app: FastAPI,+ require_auth: AuthDep | None = None,+) -> None:+ """Mount the runs routes onto ``app``.++ Resolves ``require_auth`` from the host ``api_server`` module via+ ``sys.modules`` when not passed explicitly.+ """+ import sys as _sys++ host = _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")+ if host is None:+ raise RuntimeError(+ "register_runs_routes: api_server module not in sys.modules; "+ "ensure api_server is imported before calling this function"+ )++ if require_auth is None:+ require_auth = host.require_auth++ # Late-access closures for shared host symbols (monkeypatch-safe)+ def _host_validate_path_param(value: str, kind: str) -> None:+ h = _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")+ return h._validate_path_param(value, kind)++ def _host_RUNS_DIR() -> Path:+ h = _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")+ return h.RUNS_DIR++ # Pydantic models for response_model (resolved at registration time)+ RunResponse = host.RunResponse+ RunInfo = host.RunInfo++ # --- Routes ---++ @app.get("/runs/{run_id}/code", dependencies=[Depends(require_auth)])+ async def get_run_code(run_id: str):+ """Return strategy source files for a run.++ Args:+ run_id: Run identifier.++ Returns:+ Map filename -> source text.+ """+ _host_validate_path_param(run_id, "run_id")+ run_dir = _host_RUNS_DIR() / run_id / "code"+ if not run_dir.exists():+ raise HTTPException(status_code=404, detail=f"Code directory for run {run_id} not found")+ result = {}+ for f in ["signal_engine.py"]:+ p = run_dir / f+ if p.exists():+ result[f] = p.read_text(encoding="utf-8")+ return result++ @app.get("/runs/{run_id}/pine", dependencies=[Depends(require_auth)])+ async def get_run_pine(run_id: str):+ """Return Pine Script file for a run.++ Args:+ run_id: Run identifier.++ Returns:+ Object with pine script content and exists flag.+ """+ _host_validate_path_param(run_id, "run_id")+ pine_path = _host_RUNS_DIR() / run_id / "artifacts" / "strategy.pine"+ if not pine_path.exists():+ return {"exists": False, "content": None}+ return {+ "exists": True,+ "content": pine_path.read_text(encoding="utf-8"),+ }++ @app.get("/runs/{run_id}", response_model=RunResponse, dependencies=[Depends(require_auth)])+ async def get_run_result(+ run_id: str,+ chart_symbol: Optional[str] = Query(None, description="Opt in to chart payloads for a single symbol"),+ chart_payload: Optional[str] = Query(+ None,+ description="Optional chart payload mode. Use 'summary' to omit chart rows and trade markers.",+ ),+ ):+ """Fetch details for a historical run by ``run_id``.++ The default response stays unchanged for existing consumers. Chart-heavy+ optimizations are opt-in via query parameters.+ """+ _host_validate_path_param(run_id, "run_id")+ if chart_payload not in (None, "summary"):+ raise HTTPException(status_code=400, detail="invalid chart_payload")+ run_dir = _host_RUNS_DIR() / run_id++ if not run_dir.exists():+ raise HTTPException(+ status_code=status.HTTP_404_NOT_FOUND,+ detail=f"Run {run_id} not found"+ )++ wants_chart_meta = bool(chart_payload or chart_symbol)+ chart_symbols: List[str] = []+ response = _build_response_from_run_dir(+ run_dir,+ elapsed=0.0,+ include_analysis=True,+ chart_symbol=chart_symbol,+ chart_payload=chart_payload or "full",+ chart_symbols_out=chart_symbols if wants_chart_meta else None,+ )++ if wants_chart_meta:+ payload = _run_response_payload(response)+ payload["chart_symbols"] = chart_symbols+ return JSONResponse(payload)++ return response++ @app.get("/runs", response_model=List[RunInfo], dependencies=[Depends(require_auth)])+ async def list_runs(limit: int = 20):+ """List recent runs with summary fields."""+ from src.ui_services import load_run_context++ limit = min(max(1, limit), 100)+ runs_dir = _host_RUNS_DIR()++ if not runs_dir.exists():+ return []++ run_dirs = sorted(+ [d for d in runs_dir.iterdir() if d.is_dir()],+ key=lambda x: x.name,+ reverse=True+ )++ results = []+ for d in run_dirs[:limit]:+ run_id = d.name++ # Status from state.json or artifacts+ status_val = "unknown"+ state_file = _load_json_file(d / "state.json")+ if state_file:+ status_val = str(state_file.get("status") or "unknown").lower()+ elif (d / "artifacts" / "equity.csv").exists():+ status_val = "success"+ elif (d / "review_report.json").exists():+ status_val = "success"++ # Parse created_at from run_id (YYYYMMDD_HHMMSS or run_YYYYMMDD_HHMMSS)+ created_at = "Unknown"+ if run_id.startswith("run_"):+ parts = run_id.split('_')+ if len(parts) >= 3:+ d_str, t_str = parts[1], parts[2]+ if len(d_str) == 8 and len(t_str) == 6:+ created_at = f"{d_str[:4]}-{d_str[4:6]}-{d_str[6:8]} {t_str[:2]}:{t_str[2:4]}:{t_str[4:6]}"+ elif "_" in run_id:+ parts = run_id.split('_')+ if len(parts) >= 2:+ d_str, t_str = parts[0], parts[1]+ if len(d_str) == 8 and len(t_str) == 6:+ created_at = f"{d_str[:4]}-{d_str[4:6]}-{d_str[6:8]} {t_str[:2]}:{t_str[2:4]}:{t_str[4:6]}"++ if created_at == "Unknown":+ mtime = datetime.fromtimestamp(d.stat().st_mtime)+ created_at = mtime.strftime("%Y-%m-%d %H:%M:%S")++ prompt = None+ req_file = d / "req.json"+ planner_file = d / "planner_output.json"+ if req_file.exists():+ try:+ req_data = json.loads(req_file.read_text(encoding="utf-8"))+ prompt = req_data.get("prompt")+ except (json.JSONDecodeError, OSError):+ pass++ if not prompt and planner_file.exists():+ try:+ planner_data = json.loads(planner_file.read_text(encoding="utf-8"))+ prompt = planner_data.get("user_goal") or planner_data.get("goal")+ except (json.JSONDecodeError, OSError):+ pass++ if not prompt:+ prompt_file = d / "user_prompt.txt"+ if prompt_file.exists():+ prompt = prompt_file.read_text(encoding="utf-8").strip()++ total_return = None+ sharpe = None+ metrics_file = d / "artifacts" / "metrics.csv"+ if metrics_file.exists():+ try:+ with open(metrics_file, 'r', encoding='utf-8') as f:+ reader = csv.DictReader(f)+ for row in reader:+ total_return = float(row.get('total_return', 0) or 0)+ sharpe = float(row.get('sharpe', 0) or 0)+ break+ except (OSError, ValueError):+ pass++ run_context = load_run_context(d)+ results.append(RunInfo(+ run_id=run_id,+ status=status_val,+ created_at=created_at,+ prompt=prompt or "Manual Analysis",+ total_return=total_return,+ sharpe=sharpe,+ codes=run_context.get("codes") or [],+ start_date=run_context.get("start_date"),+ end_date=run_context.get("end_date"),+ ))++ return resultsdiff --git a/agent/src/api/scheduled_routes.py b/agent/src/api/scheduled_routes.pynew file mode 100644index 000000000..6ba2907da--- /dev/null+++ b/agent/src/api/scheduled_routes.py@@ -0,0 +1,241 @@+"""Scheduled research HTTP routes.++Mounted by ``agent/api_server.py`` via ``register_scheduled_routes(app, ...)``.+"""++from __future__ import annotations++import logging+import os+import sys as _sys+import time+import uuid+from typing import Any, Awaitable, Callable, Dict, List, Optional++from fastapi import Depends, FastAPI, HTTPException, Query, status+from pydantic import BaseModel, Field++logger = logging.getLogger(__name__)+++# ---------------------------------------------------------------------------+# Constants+# ---------------------------------------------------------------------------++_SCHEDULED_RESEARCH_SCHEDULER_ENV = "VIBE_TRADING_ENABLE_SCHEDULER"+_SCHEDULED_RESEARCH_TRUE_VALUES = {"1", "true", "yes", "on"}+++# ---------------------------------------------------------------------------+# Module-level state+# ---------------------------------------------------------------------------++_scheduled_research_store: Any = None+_scheduled_research_executor: Any = None+++def _scheduled_research_scheduler_enabled() -> bool:+ """Return whether scheduled research execution is enabled."""+ return (+ os.getenv(_SCHEDULED_RESEARCH_SCHEDULER_ENV, "").strip().lower()+ in _SCHEDULED_RESEARCH_TRUE_VALUES+ )+++def _get_scheduled_research_store():+ """Return the singleton ScheduledResearchJobStore, creating it on first call."""+ global _scheduled_research_store+ if _scheduled_research_store is None:+ from src.scheduled_research.store import ScheduledResearchJobStore++ _scheduled_research_store = ScheduledResearchJobStore()+ return _scheduled_research_store+++async def _dispatch_scheduled_research_job(job) -> None:+ """Enqueue one scheduled research job through the session runtime.++ ``send_message`` queues the agent attempt and returns once accepted; it+ does not wait for that agent run to reach a terminal status. The executor's+ ``COMPLETED`` state for this dispatch path means "successfully enqueued."+ """+ host = _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")+ svc = host._get_session_service()+ if not svc:+ raise RuntimeError("Session runtime not enabled")+ # Pass a copy so the session runtime's internal config writes (e.g.+ # include_shell_tools) do not mutate the persisted scheduled-run config.+ session = svc.create_session(+ title=f"scheduled-research:{job.id}", config=dict(job.config)+ )+ logger.info(+ "dispatching scheduled research job %s via session %s",+ job.id,+ session.session_id,+ )+ await svc.send_message(session.session_id, job.prompt)+++def _get_scheduled_research_executor():+ """Return the singleton scheduled research executor."""+ global _scheduled_research_executor+ if _scheduled_research_executor is None:+ from src.scheduled_research.executor import ScheduledResearchExecutor++ _scheduled_research_executor = ScheduledResearchExecutor(+ _get_scheduled_research_store(),+ _dispatch_scheduled_research_job,+ enabled=_scheduled_research_scheduler_enabled(),+ )+ return _scheduled_research_executor+++def _start_scheduled_research_executor() -> None:+ """Start scheduled research execution when explicitly enabled."""+ if not _scheduled_research_scheduler_enabled():+ return+ _get_scheduled_research_executor().start()+++async def _stop_scheduled_research_executor() -> None:+ """Stop scheduled research execution if it was started."""+ executor = _scheduled_research_executor+ if executor is not None:+ await executor.stop()+++# ---------------------------------------------------------------------------+# Pydantic models+# ---------------------------------------------------------------------------+++class CreateScheduledRunRequest(BaseModel):+ """Request body for POST /scheduled-runs."""++ id: Optional[str] = Field(+ None, description="Job id; auto-generated UUID when omitted"+ )+ prompt: str = Field(+ ..., min_length=1, description="Research prompt or backtest description"+ )+ schedule: str = Field(+ ..., min_length=1, description="Interval-ms or 5-field cron expression"+ )+ next_run_at: Optional[int] = Field(+ None, description="Epoch-ms for next run; defaults to now"+ )+ config: Dict[str, Any] = Field(+ default_factory=dict, description="Optional backtest parameters"+ )+++class ScheduledRunResponse(BaseModel):+ """API response for a single scheduled job."""++ id: str+ prompt: str+ schedule: str+ next_run_at: int+ status: str+ created_at: int+ config: Dict[str, Any] = Field(default_factory=dict)+++# ---------------------------------------------------------------------------+# Registration+# ---------------------------------------------------------------------------++AuthDep = Callable[..., Awaitable[Any] | Any]+++def register_scheduled_routes(+ app: FastAPI,+ require_auth: AuthDep | None = None,+) -> None:+ """Mount the scheduled routes onto ``app``.++ Resolves ``require_auth`` from the host ``api_server`` module via+ ``sys.modules`` when not passed explicitly.+ """+ host = _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")++ if host is None:+ raise RuntimeError(+ "register_scheduled_routes: api_server module not in sys.modules; "+ "ensure api_server is imported before calling this function"+ )++ if require_auth is None:+ require_auth = host.require_auth++ def _host_validate_path_param(value: str, kind: str) -> None:+ h = _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")+ h._validate_path_param(value, kind)++ # --- Routes ---++ @app.post(+ "/scheduled-runs",+ response_model=ScheduledRunResponse,+ status_code=status.HTTP_201_CREATED,+ dependencies=[Depends(require_auth)],+ )+ async def create_scheduled_run(+ request: CreateScheduledRunRequest,+ ) -> ScheduledRunResponse:+ """Create (or replace) a scheduled research job.++ The job is persisted immediately. No execution is triggered.+ """+ from src.scheduled_research.models import (+ JobStatus,+ ScheduledResearchJob,+ validate_schedule,+ )++ try:+ validate_schedule(request.schedule)+ except ValueError as exc:+ raise HTTPException(status_code=422, detail=str(exc)) from exc++ now_ms = int(time.time() * 1000)+ job = ScheduledResearchJob(+ id=request.id or str(uuid.uuid4()),+ prompt=request.prompt,+ schedule=request.schedule,+ next_run_at=request.next_run_at if request.next_run_at is not None else now_ms,+ status=JobStatus.PENDING,+ created_at=now_ms,+ config=request.config,+ )+ _get_scheduled_research_store().upsert(job)+ return ScheduledRunResponse(**job.to_dict())++ @app.get(+ "/scheduled-runs",+ response_model=List[ScheduledRunResponse],+ dependencies=[Depends(require_auth)],+ )+ async def list_scheduled_runs(+ status_filter: Optional[str] = Query(None, alias="status"),+ limit: int = Query(50, ge=1, le=200),+ ) -> List[ScheduledRunResponse]:+ """List scheduled research jobs, optionally filtered by status."""+ jobs = _get_scheduled_research_store().list_jobs(+ status=status_filter, limit=limit+ )+ return [ScheduledRunResponse(**j.to_dict()) for j in jobs]++ @app.delete(+ "/scheduled-runs/{job_id}",+ status_code=status.HTTP_204_NO_CONTENT,+ dependencies=[Depends(require_auth)],+ )+ async def delete_scheduled_run(job_id: str) -> None:+ """Cancel (delete) a scheduled research job by id."""+ _host_validate_path_param(job_id, "job_id")+ removed = _get_scheduled_research_store().delete(job_id)+ if not removed:+ raise HTTPException(+ status_code=404, detail=f"scheduled run {job_id} not found"+ )diff --git a/agent/src/api/sessions_routes.py b/agent/src/api/sessions_routes.pynew file mode 100644index 000000000..00352dfec--- /dev/null+++ b/agent/src/api/sessions_routes.py@@ -0,0 +1,716 @@+"""Session and goal HTTP routes.++Mounted by ``agent/api_server.py`` via ``register_sessions_routes(app)``.+"""++from __future__ import annotations++import json+import logging+import re+from typing import Any, Dict, List, Optional++from fastapi import Depends, FastAPI, HTTPException, Query, Request, status+from fastapi.responses import StreamingResponse+from pydantic import BaseModel, Field++logger = logging.getLogger(__name__)+++# ============================================================================+# Pydantic Models+# ============================================================================++class CreateSessionRequest(BaseModel):+ """Create session request body."""+ title: str = Field("", description="Session title")+ config: Optional[Dict[str, Any]] = Field(None, description="Session config")+++class SessionResponse(BaseModel):+ """Session record."""+ session_id: str+ title: str+ status: str+ created_at: str+ updated_at: str+ last_attempt_id: Optional[str] = None+++class SendMessageRequest(BaseModel):+ """Send chat message: natural-language strategy description."""+ content: str = Field(..., description="Natural language strategy description", min_length=1, max_length=5000)+++class MessageResponse(BaseModel):+ """Stored chat message."""+ message_id: str+ session_id: str+ role: str+ content: str+ created_at: str+ linked_attempt_id: Optional[str] = None+ metadata: Optional[Dict[str, Any]] = None+++class CreateGoalRequest(BaseModel):+ """Create or replace a finance research goal."""++ objective: str = Field(..., min_length=1, max_length=5000)+ criteria: List[str] = Field(default_factory=list)+ ui_summary: str = ""+ protocol: str = "thesis_review"+ risk_tier: str = "research_general"+ token_budget: Optional[int] = Field(None, ge=1)+ turn_budget: Optional[int] = Field(None, ge=1)+ time_budget_seconds: Optional[int] = Field(None, ge=1)+++class UpdateGoalRequest(BaseModel):+ """Edit mutable finance research goal fields."""++ goal_id: str = Field(..., min_length=1)+ expected_goal_id: str = Field(..., min_length=1)+ objective: Optional[str] = Field(None, min_length=1, max_length=5000)+ ui_summary: Optional[str] = Field(None, max_length=500)+++class AddGoalEvidenceRequest(BaseModel):+ """Append evidence to a finance research goal."""++ goal_id: str = Field(..., min_length=1)+ expected_goal_id: str = Field(..., min_length=1)+ text: str = Field(..., min_length=1, max_length=10000)+ criterion_id: Optional[str] = None+ claim_id: Optional[str] = None+ evidence_type: str = "evidence"+ tool_call_id: Optional[str] = None+ run_id: Optional[str] = None+ source_provider: Optional[str] = None+ source_type: Optional[str] = None+ source_uri: Optional[str] = None+ symbol_universe: List[str] = Field(default_factory=list)+ benchmark: List[str] = Field(default_factory=list)+ timeframe: Optional[str] = None+ method: Optional[str] = None+ assumptions: Dict[str, Any] = Field(default_factory=dict)+ artifact_path: Optional[str] = None+ artifact_hash: Optional[str] = None+ data_as_of: Optional[str] = None+ confidence: Optional[str] = None+ caveat: Optional[str] = None+ contradicts_claim_ids: List[str] = Field(default_factory=list)+++class GoalSnapshotResponse(BaseModel):+ """Finance research goal snapshot."""++ goal: Dict[str, Any]+ claims: List[Dict[str, Any]]+ criteria: List[Dict[str, Any]]+ evidence: List[Dict[str, Any]]+ evidence_count: int = 0+++class AddGoalEvidenceResponse(BaseModel):+ """Response after appending goal evidence."""++ evidence: Dict[str, Any]+ snapshot: GoalSnapshotResponse+++class GoalAuditRowRequest(BaseModel):+ """One criterion row for goal status audits."""++ criterion_id: str = Field(..., min_length=1)+ result: str = Field(..., min_length=1)+ evidence_ids: List[str] = Field(default_factory=list)+ notes: str = ""+++class UpdateGoalStatusRequest(BaseModel):+ """Update a finance research goal status."""++ goal_id: str = Field(..., min_length=1)+ expected_goal_id: str = Field(..., min_length=1)+ status: str = Field(..., min_length=1)+ audit: List[GoalAuditRowRequest] = Field(default_factory=list)+ recap: Optional[str] = None+++class UpdateGoalStatusResponse(BaseModel):+ """Response after changing a goal status."""++ goal: Dict[str, Any]+ snapshot: GoalSnapshotResponse+++class UpdateGoalResponse(BaseModel):+ """Response after editing a goal."""++ goal: Dict[str, Any]+ snapshot: GoalSnapshotResponse+++class UpdateSessionRequest(BaseModel):+ """Session update fields."""+ title: Optional[str] = None+++# ============================================================================+# State variables+# ============================================================================++_goal_store = None+++# ============================================================================+# Helper Functions+# ============================================================================++def _get_goal_store():+ """Return the shared finance goal store."""+ global _goal_store+ if _goal_store is None:+ from src.goal import GoalStore++ _goal_store = GoalStore()+ return _goal_store++# ============================================================================+# SSE frame helpers for session events (module-level for re-export)+# ============================================================================++_PROPOSAL_TOOL_NAME = "propose_mandate_profiles"+_PROPOSAL_ID_RE = re.compile(r'"proposal_id"\s*:\s*"(mp_[0-9a-f]{32})"')+++def _load_full_proposal(proposal_id: str) -> Optional[Dict[str, Any]]:+ """Reload a persisted mandate proposal by id, broker-agnostic."""+ try:+ from src.live.paths import live_root++ for proposal_path in live_root().glob(f"*/proposals/{proposal_id}.json"):+ try:+ data = json.loads(proposal_path.read_text(encoding="utf-8"))+ except (OSError, json.JSONDecodeError):+ continue+ if isinstance(data, dict) and data.get("type") == "mandate.proposal":+ return data+ except Exception: # pragma: no cover - relay must never break the stream+ logger.debug("mandate.proposal reload failed for %s", proposal_id, exc_info=True)+ return None+++def _mandate_proposal_frame_from_tool_result(event: Any) -> Optional[str]:+ """Build a mandate.proposal SSE frame from a propose-tool tool_result."""+ data = getattr(event, "data", None)+ if getattr(event, "event_type", None) != "tool_result" or not isinstance(data, dict):+ return None+ if data.get("tool") != _PROPOSAL_TOOL_NAME or data.get("status") != "ok":+ return None+ match = _PROPOSAL_ID_RE.search(str(data.get("preview") or ""))+ if not match:+ return None+ proposal = _load_full_proposal(match.group(1))+ if proposal is None:+ return None++ from src.session.events import SSEEvent++ frame = SSEEvent(+ event_type="mandate.proposal",+ data=proposal,+ session_id=getattr(event, "session_id", "") or "",+ )+ return frame.to_sse()+++_LIVE_ACTION_ID_RE = re.compile(r'"audit_id"\s*:\s*"(la_[0-9a-zA-Z]+)"')+++def _load_live_action_record(audit_id: str) -> Optional[Dict[str, Any]]:+ """Reload a redacted live-action record from the ledger by audit_id."""+ try:+ from src.live.paths import live_root++ ledger = live_root() / "audit.jsonl"+ if not ledger.exists():+ return None+ for line in reversed(ledger.read_text(encoding="utf-8").splitlines()):+ if audit_id not in line:+ continue+ try:+ record = json.loads(line)+ except json.JSONDecodeError:+ continue+ if isinstance(record, dict) and record.get("audit_id") == audit_id:+ return record+ except Exception: # pragma: no cover - relay must never break the stream+ logger.debug("live.action reload failed for %s", audit_id, exc_info=True)+ return None+++def _live_action_frame_from_tool_result(event: Any) -> Optional[str]:+ """Build a live.action SSE frame from an order-guard tool_result."""+ data = getattr(event, "data", None)+ if getattr(event, "event_type", None) != "tool_result" or not isinstance(data, dict):+ return None+ preview = str(data.get("preview") or "")+ if '"live_action"' not in preview:+ return None+ match = _LIVE_ACTION_ID_RE.search(preview)+ if not match:+ return None+ record = _load_live_action_record(match.group(1))+ if record is None:+ return None++ from src.session.events import SSEEvent++ frame = SSEEvent(+ event_type="live.action",+ data=record,+ session_id=getattr(event, "session_id", "") or "",+ )+ return frame.to_sse()++++# ============================================================================+# Registration+# ============================================================================++def register_sessions_routes(app: FastAPI) -> None:+ """Mount the session/goal routes onto ``app``.++ Resolves shared dependencies from the host ``api_server`` module via+ ``sys.modules``.+ """+ import sys as _sys++ host = _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")+ if host is None:+ raise RuntimeError(+ "register_sessions_routes: api_server module not in sys.modules; "+ "ensure api_server is imported before calling this function"+ )++ # Auth dependencies+ require_auth = host.require_auth+ require_event_stream_auth = host.require_event_stream_auth++ # Late-access closures for shared host symbols (monkeypatch-safe)+ def _host_get_session_service():+ h = _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")+ return h._get_session_service()++ def _host_validate_path_param(value: str, kind: str) -> None:+ h = _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")+ return h._validate_path_param(value, kind)++ def _host_shell_tools_enabled_for_request(request: Request) -> bool:+ h = _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")+ return h._shell_tools_enabled_for_request(request)++ def _get_existing_session_or_404(session_id: str):+ """Return (service, session) or raise 404."""+ svc = _host_get_session_service()+ if not svc:+ raise HTTPException(status_code=501, detail="Session runtime not enabled")+ session = svc.get_session(session_id)+ if not session:+ raise HTTPException(status_code=404, detail=f"Session {session_id} not found")+ return svc, session++ # -----------------------------------------------------------------------+ # Session CRUD routes+ # -----------------------------------------------------------------------++ @app.post("/sessions", response_model=SessionResponse, status_code=status.HTTP_201_CREATED, dependencies=[Depends(require_auth)])+ async def create_session(request: CreateSessionRequest):+ """Create a chat session."""+ svc = _host_get_session_service()+ if not svc:+ raise HTTPException(status_code=501, detail="Session runtime not enabled")+ session = svc.create_session(title=request.title, config=request.config)+ return SessionResponse(+ session_id=session.session_id,+ title=session.title,+ status=session.status.value,+ created_at=session.created_at,+ updated_at=session.updated_at,+ last_attempt_id=session.last_attempt_id,+ )++ @app.get("/sessions", response_model=List[SessionResponse], dependencies=[Depends(require_auth)])+ async def list_sessions(limit: int = Query(50, ge=1, le=200)):+ """List sessions."""+ svc = _host_get_session_service()+ if not svc:+ raise HTTPException(status_code=501, detail="Session runtime not enabled")+ sessions = svc.list_sessions(limit=limit)+ return [+ SessionResponse(+ session_id=s.session_id,+ title=s.title,+ status=s.status.value,+ created_at=s.created_at,+ updated_at=s.updated_at,+ last_attempt_id=s.last_attempt_id,+ )+ for s in sessions+ ]++ @app.get("/sessions/{session_id}", response_model=SessionResponse, dependencies=[Depends(require_auth)])+ async def get_session(session_id: str):+ """Get one session by id."""+ _host_validate_path_param(session_id, "session_id")+ svc = _host_get_session_service()+ if not svc:+ raise HTTPException(status_code=501, detail="Session runtime not enabled")+ session = svc.get_session(session_id)+ if not session:+ raise HTTPException(status_code=404, detail=f"Session {session_id} not found")+ return SessionResponse(+ session_id=session.session_id,+ title=session.title,+ status=session.status.value,+ created_at=session.created_at,+ updated_at=session.updated_at,+ last_attempt_id=session.last_attempt_id,+ )++ # -----------------------------------------------------------------------+ # Goal sub-group routes+ # -----------------------------------------------------------------------++ @app.post(+ "/sessions/{session_id}/goal",+ response_model=GoalSnapshotResponse,+ status_code=status.HTTP_201_CREATED,+ dependencies=[Depends(require_auth)],+ )+ async def create_session_goal(session_id: str, req: CreateGoalRequest):+ """Create or replace the current finance research goal for a session."""+ _host_validate_path_param(session_id, "session_id")+ svc, _session = _get_existing_session_or_404(session_id)+ from src.goal import RiskTier+ from src.goal.context import default_goal_criteria++ criteria = [item.strip() for item in req.criteria if item.strip()]+ if not criteria:+ criteria = default_goal_criteria()+ try:+ risk_tier = RiskTier(req.risk_tier)+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=f"invalid risk_tier: {req.risk_tier}") from exc+ if risk_tier is RiskTier.LIVE_TRADING_OR_EXECUTION:+ raise HTTPException(status_code=400, detail="live trading or execution goals are not supported")++ goal_store = _get_goal_store()+ try:+ goal = goal_store.replace_goal(+ session_id=session_id,+ objective=req.objective,+ criteria=criteria,+ ui_summary=req.ui_summary,+ source="api",+ protocol=req.protocol,+ risk_tier=risk_tier,+ token_budget=req.token_budget,+ turn_budget=req.turn_budget,+ time_budget_seconds=req.time_budget_seconds,+ )+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=str(exc)) from exc+ snapshot = goal_store.get_goal_snapshot(goal.goal_id)+ if snapshot is None:+ raise HTTPException(status_code=500, detail="Goal created but could not be reloaded")+ svc.event_bus.emit(session_id, "goal.created", {"goal": snapshot["goal"]})+ return snapshot++ @app.get(+ "/sessions/{session_id}/goal",+ response_model=GoalSnapshotResponse,+ dependencies=[Depends(require_auth)],+ )+ async def get_session_goal(session_id: str):+ """Return the current finance research goal snapshot for a session."""+ _host_validate_path_param(session_id, "session_id")+ _get_existing_session_or_404(session_id)+ snapshot = _get_goal_store().get_current_snapshot(session_id)+ if snapshot is None:+ raise HTTPException(status_code=404, detail="No current goal")+ return snapshot++ @app.patch(+ "/sessions/{session_id}/goal",+ response_model=UpdateGoalResponse,+ dependencies=[Depends(require_auth)],+ )+ async def update_session_goal(session_id: str, req: UpdateGoalRequest):+ """Edit the current finance research goal without replacing the session."""+ _host_validate_path_param(session_id, "session_id")+ svc, _session = _get_existing_session_or_404(session_id)+ from src.goal import StaleGoalError++ if req.objective is None and req.ui_summary is None:+ raise HTTPException(status_code=400, detail="objective or ui_summary is required")++ goal_store = _get_goal_store()+ try:+ goal = goal_store.update_goal(+ session_id=session_id,+ goal_id=req.goal_id,+ expected_goal_id=req.expected_goal_id,+ objective=req.objective,+ ui_summary=req.ui_summary,+ )+ except StaleGoalError as exc:+ raise HTTPException(status_code=409, detail=str(exc)) from exc+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=str(exc)) from exc++ snapshot = goal_store.get_goal_snapshot(goal.goal_id)+ if snapshot is None:+ raise HTTPException(status_code=500, detail="Goal snapshot could not be reloaded")+ svc.event_bus.emit(session_id, "goal.updated", {"goal": snapshot["goal"], "snapshot": snapshot})+ return {"goal": snapshot["goal"], "snapshot": snapshot}++ @app.post(+ "/sessions/{session_id}/goal/evidence",+ response_model=AddGoalEvidenceResponse,+ status_code=status.HTTP_201_CREATED,+ dependencies=[Depends(require_auth)],+ )+ async def add_session_goal_evidence(session_id: str, req: AddGoalEvidenceRequest):+ """Append traceable evidence to the current finance research goal."""+ _host_validate_path_param(session_id, "session_id")+ svc, _session = _get_existing_session_or_404(session_id)+ from dataclasses import asdict+ from src.goal import EvidenceInput, StaleGoalError++ goal_store = _get_goal_store()+ try:+ evidence = goal_store.append_evidence(+ session_id=session_id,+ goal_id=req.goal_id,+ expected_goal_id=req.expected_goal_id,+ evidence=EvidenceInput(+ criterion_id=req.criterion_id,+ claim_id=req.claim_id,+ evidence_type=req.evidence_type,+ text=req.text,+ tool_call_id=req.tool_call_id,+ run_id=req.run_id,+ source_provider=req.source_provider,+ source_type=req.source_type,+ source_uri=req.source_uri,+ symbol_universe=req.symbol_universe,+ benchmark=req.benchmark,+ timeframe=req.timeframe,+ method=req.method,+ assumptions=req.assumptions,+ artifact_path=req.artifact_path,+ artifact_hash=req.artifact_hash,+ data_as_of=req.data_as_of,+ confidence=req.confidence,+ caveat=req.caveat,+ contradicts_claim_ids=req.contradicts_claim_ids,+ ),+ )+ except StaleGoalError as exc:+ raise HTTPException(status_code=409, detail=str(exc)) from exc+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=str(exc)) from exc++ snapshot = goal_store.get_goal_snapshot(req.goal_id)+ if snapshot is None:+ raise HTTPException(status_code=500, detail="Goal snapshot could not be reloaded")+ svc.event_bus.emit(+ session_id,+ "goal.evidence",+ {"evidence": asdict(evidence), "goal_id": req.goal_id},+ )+ return {"evidence": asdict(evidence), "snapshot": snapshot}++ @app.patch(+ "/sessions/{session_id}/goal/status",+ response_model=UpdateGoalStatusResponse,+ dependencies=[Depends(require_auth)],+ )+ async def update_session_goal_status(session_id: str, req: UpdateGoalStatusRequest):+ """Update the current finance research goal status."""+ _host_validate_path_param(session_id, "session_id")+ svc, _session = _get_existing_session_or_404(session_id)+ from src.goal import AuditRow, GoalStatus, StaleGoalError++ try:+ next_status = GoalStatus(req.status)+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=f"invalid goal status: {req.status}") from exc++ goal_store = _get_goal_store()+ try:+ goal = goal_store.update_status(+ session_id=session_id,+ goal_id=req.goal_id,+ expected_goal_id=req.expected_goal_id,+ status=next_status,+ audit=[+ AuditRow(+ criterion_id=row.criterion_id,+ result=row.result,+ evidence_ids=row.evidence_ids,+ notes=row.notes,+ )+ for row in req.audit+ ],+ recap=req.recap,+ )+ except StaleGoalError as exc:+ raise HTTPException(status_code=409, detail=str(exc)) from exc+ except ValueError as exc:+ raise HTTPException(status_code=400, detail=str(exc)) from exc++ snapshot = goal_store.get_goal_snapshot(goal.goal_id)+ if snapshot is None:+ raise HTTPException(status_code=500, detail="Goal snapshot could not be reloaded")+ svc.event_bus.emit(session_id, "goal.updated", {"goal": snapshot["goal"], "snapshot": snapshot})+ return {"goal": snapshot["goal"], "snapshot": snapshot}++ # -----------------------------------------------------------------------+ # Session action routes+ # -----------------------------------------------------------------------++ @app.delete("/sessions/{session_id}", dependencies=[Depends(require_auth)])+ async def delete_session(session_id: str):+ """Delete a session."""+ _host_validate_path_param(session_id, "session_id")+ svc = _host_get_session_service()+ if not svc:+ raise HTTPException(status_code=501, detail="Session runtime not enabled")+ deleted = svc.delete_session(session_id)+ if not deleted:+ raise HTTPException(status_code=404, detail=f"Session {session_id} not found")+ _get_goal_store().delete_session_goals(session_id)+ return {"status": "deleted", "session_id": session_id}++ @app.patch("/sessions/{session_id}", dependencies=[Depends(require_auth)])+ async def update_session(session_id: str, req: UpdateSessionRequest):+ """Update session fields (e.g. title)."""+ _host_validate_path_param(session_id, "session_id")+ svc = _host_get_session_service()+ if not svc:+ raise HTTPException(status_code=501, detail="Session runtime not enabled")+ session = svc.store.get_session(session_id)+ if not session:+ raise HTTPException(status_code=404, detail=f"Session {session_id} not found")+ if req.title is not None:+ session.title = req.title+ from datetime import datetime+ session.updated_at = datetime.now().isoformat()+ svc.store.update_session(session)+ return {"status": "updated", "session_id": session_id}++ @app.post("/sessions/{session_id}/messages", dependencies=[Depends(require_auth)])+ async def send_message(session_id: str, payload: SendMessageRequest, http_request: Request):+ """Send a user message and start the agent loop (natural language strategy)."""+ _host_validate_path_param(session_id, "session_id")+ svc = _host_get_session_service()+ if not svc:+ raise HTTPException(status_code=501, detail="Session runtime not enabled")+ try:+ result = await svc.send_message(+ session_id=session_id,+ content=payload.content,+ include_shell_tools=_host_shell_tools_enabled_for_request(http_request),+ )+ return result+ except ValueError as exc:+ raise HTTPException(status_code=404, detail=str(exc))++ @app.post("/sessions/{session_id}/cancel", dependencies=[Depends(require_auth)])+ async def cancel_session(session_id: str):+ """Cancel the in-flight agent loop for this session."""+ _host_validate_path_param(session_id, "session_id")+ svc = _host_get_session_service()+ if not svc:+ raise HTTPException(status_code=501, detail="Session runtime not enabled")+ cancelled = svc.cancel_current(session_id)+ if not cancelled:+ return {"status": "no_active_loop"}+ return {"status": "cancelled"}++ @app.get("/sessions/{session_id}/messages", response_model=List[MessageResponse], dependencies=[Depends(require_auth)])+ async def get_messages(session_id: str, limit: int = Query(100, ge=1, le=1000)):+ """List messages for a session."""+ _host_validate_path_param(session_id, "session_id")+ svc = _host_get_session_service()+ if not svc:+ raise HTTPException(status_code=501, detail="Session runtime not enabled")+ messages = svc.get_messages(session_id, limit=limit)+ return [+ MessageResponse(+ message_id=m.message_id,+ session_id=m.session_id,+ role=m.role,+ content=m.content,+ created_at=m.created_at,+ linked_attempt_id=m.linked_attempt_id,+ metadata=m.metadata if m.metadata else None,+ )+ for m in messages+ ]++ @app.get("/sessions/{session_id}/events", dependencies=[Depends(require_event_stream_auth)])+ async def session_events(+ session_id: str,+ request: Request,+ last_event_id: Optional[str] = Query(None, alias="Last-Event-ID"),+ replay: Optional[str] = Query(None),+ ):+ """SSE stream for agent events."""+ _host_validate_path_param(session_id, "session_id")+ svc = _host_get_session_service()+ if not svc:+ raise HTTPException(status_code=501, detail="Session runtime not enabled")+ session = svc.get_session(session_id)+ if not session:+ raise HTTPException(status_code=404, detail=f"Session {session_id} not found")++ header_id = request.headers.get("Last-Event-ID")+ event_id = header_id or last_event_id+ replay_active = (replay or "").lower() == "active"+ replay_all = False+ if replay_active and not event_id and session.last_attempt_id:+ attempt = svc.store.get_attempt(session_id, session.last_attempt_id)+ attempt_status = getattr(attempt.status, "value", attempt.status) if attempt else None+ replay_all = attempt_status == "running"++ async def event_generator():+ async for event in svc.event_bus.subscribe(+ session_id,+ last_event_id=event_id,+ replay_all=replay_all,+ ):+ if await request.is_disconnected():+ break+ yield event.to_sse()+ relayed = _mandate_proposal_frame_from_tool_result(event)+ if relayed is not None:+ yield relayed+ live_action = _live_action_frame_from_tool_result(event)+ if live_action is not None:+ yield live_action++ return StreamingResponse(+ event_generator(),+ media_type="text/event-stream",+ headers={+ "Cache-Control": "no-cache",+ "Connection": "keep-alive",+ "X-Accel-Buffering": "no",+ },+ )diff --git a/agent/src/api/swarm_routes.py b/agent/src/api/swarm_routes.pynew file mode 100644index 000000000..bf95663b4--- /dev/null+++ b/agent/src/api/swarm_routes.py@@ -0,0 +1,238 @@+"""Swarm HTTP routes.++Mounted by ``agent/api_server.py`` via ``register_swarm_routes(app, ...)``.+"""++from __future__ import annotations++import json+from pathlib import Path+from typing import Any, Awaitable, Callable++from fastapi import Depends, FastAPI, HTTPException, Query, Request+from fastapi.responses import StreamingResponse++# ---------------------------------------------------------------------------+# Module-level state+# ---------------------------------------------------------------------------++_swarm_runtime = None+++def _get_swarm_runtime():+ """Lazy-init SwarmRuntime singleton."""+ global _swarm_runtime+ if _swarm_runtime is not None:+ return _swarm_runtime+ from src.config import load_swarm_agent_config+ from src.swarm.store import SwarmStore+ from src.swarm.runtime import SwarmRuntime++ # Adjust path: this file is at agent/src/api/, so parent.parent.parent = agent/+ swarm_dir = Path(__file__).resolve().parent.parent.parent / ".swarm" / "runs"+ store = SwarmStore(base_dir=swarm_dir)+ # Boot-time / operator-trusted: REST API callers cannot influence the+ # config path. See docs/2026-05-25_swarm_mcp_tools_roadmap.md.+ agent_config = load_swarm_agent_config()+ _swarm_runtime = SwarmRuntime(store=store, agent_config=agent_config)+ return _swarm_runtime+++# ---------------------------------------------------------------------------+# Registration+# ---------------------------------------------------------------------------++AuthDep = Callable[..., Awaitable[Any] | Any]+++def register_swarm_routes(+ app: FastAPI,+ require_auth: AuthDep | None = None,+ require_event_stream_auth: AuthDep | None = None,+) -> None:+ """Mount the swarm routes onto ``app``.++ Resolves ``require_auth`` and ``require_event_stream_auth`` from the host+ ``api_server`` module via ``sys.modules`` when not passed explicitly.+ """+ import sys as _sys++ host = _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")+ if host is None:+ raise RuntimeError(+ "register_swarm_routes: api_server module not in sys.modules; "+ "ensure api_server is imported before calling this function"+ )++ if require_auth is None:+ require_auth = host.require_auth+ if require_event_stream_auth is None:+ require_event_stream_auth = host.require_event_stream_auth++ def _host_validate_path_param(value: str, kind: str) -> None:+ h = _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")+ h._validate_path_param(value, kind)++ def _host_shell_tools_enabled_for_request(request: Request) -> bool:+ h = _sys.modules.get("api_server") or _sys.modules.get("agent.api_server")+ return h._shell_tools_enabled_for_request(request)++ # --- Routes ---++ @app.get("/swarm/presets")+ async def list_swarm_presets():+ """List Swarm YAML presets."""+ from src.swarm.presets import list_presets++ return list_presets()++ @app.post("/swarm/runs", dependencies=[Depends(require_auth)])+ async def create_swarm_run(payload: dict, http_request: Request):+ """Start a swarm run: body must include preset_name and user_vars."""+ runtime = _get_swarm_runtime()+ preset_name = payload.get("preset_name", "")+ user_vars = payload.get("user_vars", {})+ try:+ run = runtime.start_run(+ preset_name,+ user_vars,+ include_shell_tools=_host_shell_tools_enabled_for_request(http_request),+ )+ return {"id": run.id, "status": run.status.value, "preset_name": run.preset_name}+ except FileNotFoundError as e:+ raise HTTPException(status_code=404, detail=str(e))+ except ValueError as e:+ raise HTTPException(status_code=400, detail=str(e))++ @app.get("/swarm/runs", dependencies=[Depends(require_auth)])+ async def list_swarm_runs(limit: int = Query(20, ge=1, le=100)):+ """List swarm runs (newest first), reconciled."""+ runtime = _get_swarm_runtime()+ runs = runtime._store.list_runs(limit=limit)+ items = []+ for r in runs:+ # Reconcile each row: a zombie running run will be auto-finalized so+ # the dashboard never shows a "running" stuck row.+ reconciled = runtime._store.reconcile_run(r, write=True)+ items.append(+ {+ "id": reconciled.id,+ "preset_name": reconciled.preset_name,+ "status": reconciled.status.value,+ "is_stale": runtime._store.is_run_stale(reconciled),+ "created_at": reconciled.created_at,+ "completed_at": reconciled.completed_at,+ "task_count": len(reconciled.tasks),+ "completed_count": sum(+ 1 for t in reconciled.tasks if t.status.value == "completed"+ ),+ }+ )+ return items++ @app.get("/swarm/runs/{run_id}", dependencies=[Depends(require_auth)])+ async def get_swarm_run(run_id: str):+ """Swarm run detail including task statuses (reconciled)."""+ _host_validate_path_param(run_id, "run_id")+ runtime = _get_swarm_runtime()+ loaded = runtime._store.load_run(run_id)+ if not loaded:+ raise HTTPException(status_code=404, detail=f"Run {run_id} not found")++ run = runtime._store.reconcile_run(loaded, write=True)++ return {+ "id": run.id,+ "preset_name": run.preset_name,+ "status": run.status.value,+ "is_stale": runtime._store.is_run_stale(run),+ "user_vars": run.user_vars,+ "agents": [a.model_dump() for a in run.agents],+ "tasks": [t.model_dump() for t in run.tasks],+ "created_at": run.created_at,+ "completed_at": run.completed_at,+ "final_report": run.final_report,+ }++ @app.get(+ "/swarm/runs/{run_id}/events",+ dependencies=[Depends(require_event_stream_auth)],+ )+ async def swarm_run_events(+ run_id: str, request: Request, last_index: int = Query(0, ge=0)+ ):+ """SSE stream for a swarm run."""+ import asyncio++ _host_validate_path_param(run_id, "run_id")+ runtime = _get_swarm_runtime()++ async def event_stream():+ idx = last_index+ while True:+ if await request.is_disconnected():+ break+ events = runtime._store.read_events(run_id, after_index=idx)+ for evt in events:+ idx += 1+ yield f"id: {idx}\nevent: {evt.type}\ndata: {json.dumps(evt.model_dump(), ensure_ascii=False)}\n\n"+ run = runtime._store.load_run(run_id)+ if run:+ # Reconcile so a zombie running run can still close this SSE+ # stream cleanly — without it, a dead host would keep the+ # stream open forever and block the dashboard's "done" state.+ reconciled = runtime._store.reconcile_run(run, write=True)+ if reconciled.status.value in ("completed", "failed", "cancelled"):+ yield f"event: done\ndata: {{\"status\": \"{reconciled.status.value}\"}}\n\n"+ break+ await asyncio.sleep(2)++ return StreamingResponse(event_stream(), media_type="text/event-stream")++ @app.post("/swarm/runs/{run_id}/cancel", dependencies=[Depends(require_auth)])+ async def cancel_swarm_run(run_id: str):+ """Cancel an active swarm run."""+ _host_validate_path_param(run_id, "run_id")+ runtime = _get_swarm_runtime()+ ok = runtime.cancel_run(run_id)+ if not ok:+ raise HTTPException(status_code=404, detail=f"No active run {run_id}")+ return {"status": "cancelled"}++ @app.post("/swarm/runs/{run_id}/retry", dependencies=[Depends(require_auth)])+ async def retry_swarm_run(run_id: str, http_request: Request):+ """Retry a failed, stale, or cancelled swarm run.++ Creates a new run with the same preset and user_vars as the original.+ """+ _host_validate_path_param(run_id, "run_id")+ runtime = _get_swarm_runtime()+ loaded = runtime._store.load_run(run_id)+ if not loaded:+ raise HTTPException(status_code=404, detail=f"Run {run_id} not found")++ # Reconcile first so a stale "running" run whose host died gets demoted+ # before we gate on status; only a genuinely active run blocks retry.+ from src.swarm.models import RunStatus++ reconciled = runtime._store.reconcile_run(loaded, write=True)+ if reconciled.status == RunStatus.running:+ raise HTTPException(+ status_code=409, detail="Cannot retry a running run. Cancel it first."+ )++ try:+ new_run = runtime.start_run(+ reconciled.preset_name,+ reconciled.user_vars or {},+ include_shell_tools=_host_shell_tools_enabled_for_request(http_request),+ )+ return {+ "id": new_run.id,+ "status": new_run.status.value,+ "preset_name": new_run.preset_name,+ }+ except FileNotFoundError as e:+ raise HTTPException(status_code=404, detail=str(e))+ except ValueError as e:+ raise HTTPException(status_code=400, detail=str(e))