feat: add durable WorldQuant backtests with UI and AI confirmation

This commit is contained in:
yuxuanhui
2026-09-08 10:06:00 +08:00
parent 404a4d8a04
commit a4b93200c5
34 changed files with 4437 additions and 23 deletions
+111
View File
@@ -199,3 +199,114 @@ class AIToolCall(Base):
status: Mapped[str] = mapped_column(String(30), default="pending")
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=now)
__table_args__ = (UniqueConstraint("run_id", "call_id"),)
class BacktestConfig(Base):
__tablename__ = "backtest_config"
id: Mapped[int] = mapped_column(primary_key=True, default=1)
concurrency: Mapped[int] = mapped_column(Integer, default=3)
batch_size: Mapped[int] = mapped_column(Integer, default=8)
version: Mapped[int] = mapped_column(Integer, default=1)
blocked_reason: Mapped[str | None] = mapped_column(Text)
blocked_until: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
class BacktestDraft(Base):
__tablename__ = "backtest_drafts"
id: Mapped[str] = mapped_column(String(36), primary_key=True)
version: Mapped[int] = mapped_column(Integer, default=1)
name: Mapped[str] = mapped_column(String(200))
source: Mapped[dict] = mapped_column(JSON)
candidates: Mapped[list] = mapped_column(JSON)
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=now)
class BacktestPreview(Base):
__tablename__ = "backtest_previews"
id: Mapped[str] = mapped_column(String(36), primary_key=True)
version: Mapped[int] = mapped_column(Integer, default=1)
name: Mapped[str] = mapped_column(String(200))
source: Mapped[dict] = mapped_column(JSON)
candidates: Mapped[list] = mapped_column(JSON)
batches: Mapped[list] = mapped_column(JSON)
batch_size: Mapped[int] = mapped_column(Integer)
digest: Mapped[str] = mapped_column(String(64))
duplicates: Mapped[list] = mapped_column(JSON)
ai_context: Mapped[dict] = mapped_column(JSON, default=dict)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=now)
class BacktestRun(Base):
__tablename__ = "backtest_runs"
id: Mapped[str] = mapped_column(String(36), primary_key=True)
preview_id: Mapped[str] = mapped_column(ForeignKey("backtest_previews.id"), unique=True)
idempotency_key: Mapped[str] = mapped_column(String(100), unique=True)
name: Mapped[str] = mapped_column(String(200))
source: Mapped[dict] = mapped_column(JSON)
ai_context: Mapped[dict] = mapped_column(JSON, default=dict)
control: Mapped[str] = mapped_column(String(20), default="active")
status: Mapped[str] = mapped_column(String(30), default="queued", index=True)
version: Mapped[int] = mapped_column(Integer, default=1)
event_seq: Mapped[int] = mapped_column(Integer, default=0)
total: Mapped[int] = mapped_column(Integer)
batch_size: Mapped[int] = mapped_column(Integer)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=now)
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=now)
class SimulationAttempt(Base):
__tablename__ = "simulation_attempts"
id: Mapped[str] = mapped_column(String(36), primary_key=True)
run_id: Mapped[str] = mapped_column(ForeignKey("backtest_runs.id"), index=True)
ordinal: Mapped[int] = mapped_column(Integer)
state: Mapped[str] = mapped_column(String(30), default="queued", index=True)
payload: Mapped[list] = mapped_column(JSON)
progress_url: Mapped[str | None] = mapped_column(Text)
remote_complete: Mapped[bool] = mapped_column(Boolean, default=False)
children: Mapped[list] = mapped_column(JSON, default=list)
receipts: Mapped[dict] = mapped_column(JSON, default=dict)
poll_count: Mapped[int] = mapped_column(Integer, default=0)
submit_count: Mapped[int] = mapped_column(Integer, default=0)
next_poll_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
error: Mapped[str | None] = mapped_column(Text)
error_code: Mapped[str | None] = mapped_column(String(50))
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=now)
__table_args__ = (UniqueConstraint("run_id", "ordinal"),)
class BacktestItem(Base):
__tablename__ = "backtest_items"
id: Mapped[str] = mapped_column(String(36), primary_key=True)
run_id: Mapped[str] = mapped_column(ForeignKey("backtest_runs.id"), index=True)
attempt_id: Mapped[str] = mapped_column(ForeignKey("simulation_attempts.id"), index=True)
client_item_id: Mapped[str] = mapped_column(String(100))
ordinal: Mapped[int] = mapped_column(Integer)
expression: Mapped[str] = mapped_column(Text)
settings: Mapped[dict] = mapped_column(JSON)
fingerprint: Mapped[str] = mapped_column(String(64), index=True)
platform_status: Mapped[str] = mapped_column(String(30), default="pending")
collection_status: Mapped[str] = mapped_column(String(30), default="pending")
persistence_status: Mapped[str] = mapped_column(String(30), default="pending")
simulation_id: Mapped[str | None] = mapped_column(String(100))
alpha_id: Mapped[str | None] = mapped_column(String(100))
error: Mapped[str | None] = mapped_column(Text)
__table_args__ = (UniqueConstraint("run_id", "client_item_id"),)
class BacktestResult(Base):
__tablename__ = "backtest_results"
item_id: Mapped[str] = mapped_column(ForeignKey("backtest_items.id"), primary_key=True)
attempt_id: Mapped[str] = mapped_column(ForeignKey("simulation_attempts.id"))
alpha_id: Mapped[str] = mapped_column(ForeignKey("alphas.id"), index=True)
snapshot: Mapped[dict] = mapped_column(JSON)
observed_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=now)
complete: Mapped[bool] = mapped_column(Boolean, default=True)
class BacktestEvent(Base):
__tablename__ = "backtest_events"
run_id: Mapped[str] = mapped_column(ForeignKey("backtest_runs.id"), primary_key=True)
seq: Mapped[int] = mapped_column(Integer, primary_key=True)
kind: Mapped[str] = mapped_column(String(50))
payload: Mapped[dict] = mapped_column(JSON)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=now)