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
File diff suppressed because it is too large Load Diff
+141
View File
@@ -0,0 +1,141 @@
import { useEffect, useState } from "react";
import { Button } from "@douyinfe/semi-ui-19";
import type { ToolCard, UIAction } from "../ai/types";
import { api } from "../api";
import { controlLabels, labels } from "./types";
import type { Run } from "./types";
export function BacktestToolCard({
call,
onAction,
}: {
call: ToolCard;
onAction: (action: UIAction) => void;
}) {
const result = call.result || {};
const preview =
call.preview.backtest ||
(typeof result.preview_id === "string" ? result : null);
const runId =
typeof result.backtest_run_id === "string"
? result.backtest_run_id
: typeof call.preview.backtest_run?.backtest_run_id === "string"
? call.preview.backtest_run.backtest_run_id
: null;
const [run, setRun] = useState<Run | null>(null);
const [error, setError] = useState("");
useEffect(() => {
if (!runId) return;
let alive = true;
const load = () =>
api<Run>(`/backtests/runs/${runId}`)
.then((r) => {
if (alive) {
setRun(r);
setError("");
}
})
.catch((e) => {
if (alive) setError(e.message);
});
void load();
const timer = window.setInterval(load, 3000);
return () => {
alive = false;
clearInterval(timer);
};
}, [runId]);
return (
<div className="backtest-tool-summary">
{preview && (
<>
<p>
{String(preview.name)} · {String(preview.total)} 条候选 ·{" "}
{String(preview.batch_count)} 个批次
</p>
<p>重复提示 {String(preview.duplicate_count)} 条,确认后独立执行。</p>
<Button
onClick={() =>
onAction({
type: "open_backtest_preview",
preview_id: String(preview.preview_id),
nonce: Date.now(),
})
}
>
查看完整固定输入
</Button>
<details>
<summary>本页候选及最终参数</summary>
<pre>{JSON.stringify(preview.items, null, 2)}</pre>
</details>
</>
)}
{call.preview.action && (
<p>
{controlLabels[call.preview.action]} ·{" "}
{String(call.preview.backtest_run?.name || "")}
</p>
)}
{run && (
<>
<p>
{run.name} · {labels[run.status]}
</p>
<p>
已保存 {run.counts.persistence.saved || 0}/{run.total} · 平台失败{" "}
{run.counts.platform.failed || 0}
</p>
<Button
onClick={() =>
onAction({
type: "open_backtest",
run_id: run.backtest_run_id,
nonce: Date.now(),
})
}
>
打开回测详情
</Button>
</>
)}
{call.name === "get_backtest_capabilities" && (
<p>
支持 REGULAR /
FASTEXPR。并发与批大小为本地配置;启动前展示固定候选供确认。
</p>
)}
{call.name === "list_backtests" && Array.isArray(result.items) && (
<>
{result.items.map((value: Run) => (
<Button
key={value.backtest_run_id}
onClick={() =>
onAction({
type: "open_backtest",
run_id: value.backtest_run_id,
nonce: Date.now(),
})
}
>
{value.name} · {labels[value.status]}
</Button>
))}
<p>
共 {String(result.total)} 条,本页 {result.items.length} 条
</p>
</>
)}
{call.name === "get_backtest_results" && (
<details>
<summary>
本页结果({Array.isArray(result.items) ? result.items.length : 0}/
{String(result.total)})
</summary>
<pre>{JSON.stringify(result.items, null, 2)}</pre>
</details>
)}
{error && <p className="error-text">进度暂时不可用:{error}</p>}
</div>
);
}
+83
View File
@@ -0,0 +1,83 @@
.backtest-page {
display: flex;
flex-direction: column;
height: 100%;
min-height: 0;
min-width: 0;
gap: 12px;
}
.backtest-toolbar {
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
}
.backtest-toolbar .semi-select {
min-width: 144px;
}
.backtest-spacer {
flex: 1;
}
.backtest-sheet {
display: flex;
flex-direction: column;
gap: 16px;
min-width: 0;
}
.backtest-form-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px 16px;
}
.backtest-form-grid .semi-input-number {
width: 100%;
}
.backtest-sheet pre {
font-size: 12px;
white-space: pre-wrap;
overflow-wrap: anywhere;
margin: 8px 0;
}
.backtest-sheet .semi-table-row-cell {
font-weight: 400;
}
.backtest-sheet .semi-table-row-cell .semi-button-content {
overflow: hidden;
text-overflow: ellipsis;
}
.backtest-sheet .semi-table-row-cell .semi-button {
max-width: 100%;
}
.backtest-attempt {
border-bottom: 1px solid var(--line);
padding: 12px 0;
}
.backtest-attempt code {
display: block;
color: var(--muted);
overflow-wrap: anywhere;
}
.backtest-item-detail {
border-top: 1px solid var(--line);
padding-top: 12px;
}
.backtest-item-detail h3 {
margin: 0;
}
.backtest-page-view {
height: 100%;
min-height: 0;
}
.backtest-tool-summary {
display: flex;
flex-direction: column;
gap: 8px;
}
@media (max-width: 640px) {
.backtest-form-grid {
grid-template-columns: minmax(0, 1fr);
}
.backtest-toolbar {
gap: 8px;
}
}
+160
View File
@@ -0,0 +1,160 @@
export type SimulationSettings = {
instrumentType: "EQUITY";
region: string;
universe: string;
delay: 0 | 1;
decay: number;
neutralization: string;
truncation: number;
pasteurization: "ON" | "OFF";
unitHandling: "VERIFY";
nanHandling: "ON" | "OFF";
language: "FASTEXPR";
visualization: boolean;
maxTrade: "ON" | "OFF";
};
export const initialSettings: SimulationSettings = {
instrumentType: "EQUITY",
region: "",
universe: "",
delay: 1,
decay: 0,
neutralization: "INDUSTRY",
truncation: 0.08,
pasteurization: "ON",
unitHandling: "VERIFY",
nanHandling: "OFF",
language: "FASTEXPR",
visualization: false,
maxTrade: "OFF",
};
export type Candidate = {
client_item_id: string;
expression: string;
settings: SimulationSettings;
alpha_type?: "REGULAR";
};
export type Source = {
kind: string;
reference?: string | null;
batch_id?: string | null;
template_input_id?: string | null;
research_id?: string | null;
parent_run_id?: string | null;
};
export type Draft = {
id: string;
version: number;
name: string;
source: Source;
candidates: Candidate[];
updated_at: string;
};
export type DraftSummary = Pick<
Draft,
"id" | "version" | "name" | "updated_at"
> & { total: number };
export type Page<T> = {
items: T[];
total: number;
limit: number;
offset: number;
};
export type Preview = Page<Candidate> & {
preview_id: string;
version: number;
name: string;
source: Source;
digest: string;
batch_count: number;
batch_size: number;
duplicate_count: number;
has_more: boolean;
};
export type Scheduler = {
concurrency: number;
batch_size: number;
version: number;
blocked_reason: string | null;
blocked_until: string | null;
};
export type Run = {
backtest_run_id: string;
preview_id: string;
name: string;
source: Source;
control: string;
status: string;
version: number;
total: number;
counts: {
platform: Record<string, number>;
collection: Record<string, number>;
persistence: Record<string, number>;
};
cursor: number;
created_at: string;
updated_at: string;
scheduler: Scheduler;
};
export type Item = {
id: string;
client_item_id: string;
expression: string;
settings: SimulationSettings;
attempt_id: string;
platform_status: string;
collection_status: string;
persistence_status: string;
simulation_id: string | null;
alpha_id: string | null;
error: string | null;
result: {
snapshot: {
is?: Record<string, unknown>;
os?: Record<string, unknown>;
checks?: unknown[];
[key: string]: unknown;
};
observed_at: string;
complete: boolean;
} | null;
};
export type Attempt = {
id: string;
state: string;
progress_url: string | null;
children: string[];
error: string | null;
error_code: string | null;
submit_count: number;
poll_count: number;
next_poll_at: string | null;
};
export const labels: Record<string, string> = {
queued: "排队中",
running: "执行中",
paused: "已暂停推送",
stopping: "停止中 · 收集已提交结果",
stopped: "已停止",
completed: "已完成",
completed_with_errors: "部分失败",
needs_review: "待核对",
collection_failed: "结果补取失败",
pending: "待处理",
submitting: "正在提交",
submitted: "平台执行中",
collecting: "收集结果",
failed: "失败",
unknown: "待核对",
skipped: "已跳过",
saved: "已保存",
complete: "完整",
not_required: "无需处理",
};
export const controlLabels: Record<string, string> = {
pause: "暂停推送",
resume: "继续推送",
stop: "停止剩余项",
recover: "找回原结果",
};