"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(api): bound research-artifact evidence and id fields
Haozhe Wu4a5f3266e06d
diff --git a/agent/src/api/research_artifact_routes.py b/agent/src/api/research_artifact_routes.pyindex a5fa890e6..0dc5c0880 100644--- a/agent/src/api/research_artifact_routes.py+++ b/agent/src/api/research_artifact_routes.py@@ -2,29 +2,43 @@from __future__ import annotations+import jsonfrom typing import Anyfrom fastapi import Depends, FastAPI, HTTPException, Query, status-from pydantic import BaseModel, Field+from pydantic import BaseModel, Field, field_validatorfrom src.research_artifacts import ResearchArtifactStore+# The content field already caps the conclusion; evidence references and ids+# were unbounded, so one authenticated request could store an arbitrarily+# large row.+_MAX_EVIDENCE_ITEMS = 1000+_MAX_EVIDENCE_CHARS = 1_000_000+class ResearchArtifactSaveRequest(BaseModel):"""Create a series or append an explicitly linked revision."""- artifact_id: str | None = None+ artifact_id: str | None = Field(default=None, max_length=200)subject: str | None = Field(default=None, max_length=5000)universe: str | None = Field(default=None, max_length=1000)content: str = Field(..., min_length=1, max_length=500_000)data_as_of: str = Field(..., min_length=1, max_length=100)- evidence: list[dict[str, Any]] | None = None+ evidence: list[dict[str, Any]] | None = Field(default=None, max_length=_MAX_EVIDENCE_ITEMS)verification_status: str | None = Field(default=None, max_length=100)completeness: str | None = Field(default=None, max_length=100)- hypothesis_id: str | None = None- goal_id: str | None = None- session_id: str | None = None- run_id: str | None = None+ hypothesis_id: str | None = Field(default=None, max_length=200)+ goal_id: str | None = Field(default=None, max_length=200)+ session_id: str | None = Field(default=None, max_length=200)+ run_id: str | None = Field(default=None, max_length=200)++ @field_validator("evidence")+ @classmethod+ def _bound_evidence_size(cls, value: list[dict[str, Any]] | None) -> list[dict[str, Any]] | None:+ if value is not None and len(json.dumps(value, ensure_ascii=False, default=str)) > _MAX_EVIDENCE_CHARS:+ raise ValueError(f"evidence must serialize to at most {_MAX_EVIDENCE_CHARS} characters")+ return valuedef register_research_artifact_routes(app: FastAPI) -> None:diff --git a/agent/tests/test_research_artifact_api.py b/agent/tests/test_research_artifact_api.pyindex e3f148993..801145616 100644--- a/agent/tests/test_research_artifact_api.py+++ b/agent/tests/test_research_artifact_api.py@@ -72,3 +72,17 @@ def test_api_reports_missing_and_invalid_research_artifacts(tmp_path, monkeypatcassert missing.status_code == 404assert invalid.status_code == 404assert blank.status_code == 422+++def test_api_refuses_unbounded_evidence_and_ids(tmp_path, monkeypatch):+ client = _client(tmp_path, monkeypatch)++ too_many = client.post("/api/research-artifacts", json=_payload(evidence=[{"source_uri": "u"}] * 1001))+ too_large = client.post("/api/research-artifacts", json=_payload(evidence=[{"note": "x" * 1_000_001}]))+ long_id = client.post("/api/research-artifacts", json=_payload(goal_id="g" * 201))+ at_limit = client.post("/api/research-artifacts", json=_payload(evidence=[{"source_uri": "u"}] * 1000))++ assert too_many.status_code == 422+ assert too_large.status_code == 422+ assert long_id.status_code == 422+ assert at_limit.status_code == 201