程序员量化交易实战 42:每日运行 artifact 落盘

程序员量化交易实战 42:每日运行 artifact 落盘
第 41 篇把每日运行计划压成了一行摘要适合日志和命令行。但日志只能回答“当时看起来是什么状态”。如果要排查一次运行为什么 blocked还需要更完整的证据输入股票、失败检查、失败动作、生成时间。这就是 artifact 的作用。artifact 存什么第 42 章新增app/daily_run_artifacts.py。dataclass(frozenTrue) class DailyRunArtifact: path: Path payload: dict[str, Any]payload不是把 dataclass 直接粗暴序列化而是写成稳定的调试格式。字段说明trade_date运行对应的交易日statusready、dry_run_ready 或 blockedrequired_symbols本次运行请求的股票列表failed_checks未通过的检查项actions每个失败检查对应的处理动作generated_at请求生成时间executable是否允许真实执行构造 JSON payload核心函数是daily_run_artifact_payload()。def daily_run_artifact_payload(plan: DailyRunPlan) - dict[str, Any]: summary build_daily_run_summary(plan) return { trade_date: summary.trade_date, status: summary.status, dry_run: summary.dry_run, symbol_count: summary.symbol_count, required_symbols: list(plan.request.required_symbols), failed_checks: list(plan.result.failed_checks), actions: [ { check_name: action.check_name, action: action.action, severity: action.severity, } for action in plan.failure_actions ], action_summary: summary.action_summary, executable: summary.executable, generated_at: plan.request.generated_at.isoformat(), }这里刻意把 tuple 转成 list。artifact 是给外部读取的不应该暴露 Python 内部对象习惯。写入文件文件名按交易日生成def write_daily_run_artifact(plan: DailyRunPlan, *, directory: Path) - DailyRunArtifact: payload daily_run_artifact_payload(plan) directory.mkdir(parentsTrue, exist_okTrue) path directory / fdaily-run-{payload[trade_date]}.json path.write_text(json.dumps(payload, ensure_asciiFalse, indent2) \n, encodingutf-8) return DailyRunArtifact(pathpath, payloadpayload)blocked 场景下artifact 会保留这样的动作信息{ actions: [ { check_name: run_health, action: inspect_run_health, severity: blocker } ] }这比只在日志里看到blocked有用得多。接到可运行示例第 42 篇继续使用同一个命令复现 artifact 落盘uv run python -m scripts.chapter_examples paper-command本章对应的输出如下这张图里有三个排查时真正有用的点。artifactdaily-run-2026-03-07.json说明文件名稳定按交易日定位。值班时不需要在日志目录里猜哪个文件是当天结果。payload_keys说明 artifact 里同时保留了请求、状态、失败检查、动作和生成时间。它不是一行摘要的备份而是更完整的运行证据。failed_checks[data_gaps] actions[repair_market_data] executableFalse把失败原因和处理方向放在一起。看到这行就知道不是策略信号问题也不是运行窗口问题而是行情数据缺口挡住了执行。生产系统里我会把日志、artifact 和后续 runbook 分开看日志用于第一眼判断artifact 用于复盘证据runbook 用于下一步动作。三者互相引用但不要互相替代。测试本章测试覆盖payload 是否包含调试所需上下文。写入后的 JSON 是否能完整读回。blocked 场景下动作是否保留check_name、action和severity。运行命令uv run pytest tests/test_daily_run_artifacts.py tests/test_daily_run_summary.py tests/test_daily_run_plan.py本批次补充paper-command后全量测试通过276 passed, 2 warnings本章更新与代码仓库本章更新内容新增app/daily_run_artifacts.py。新增DailyRunArtifact。实现daily_run_artifact_payload()。实现write_daily_run_artifact()和read_daily_run_artifact()。新增tests/test_daily_run_artifacts.py覆盖 payload 与 JSON round trip。在scripts/chapter_examples.py中接入 artifact 写入与读回文章截图来自该命令输出。代码仓库https://github.com/ax2/zi-quant-platform本章代码git clone https://github.com/ax2/zi-quant-platform.git cd zi-quant-platform git checkout chapter-41-45-paper-command uv sync --extra dev uv run python -m scripts.chapter_examples paper-command uv run pytest tests/test_daily_run_artifacts.py tests/test_daily_run_summary.py tests/test_daily_run_plan.py tests/test_chapter_examples.py第 41-45 篇共用 tagchapter-41-45-paper-command。当前全量测试通过276 passed只有既有 FastAPI deprecation warning。本篇小结artifact 是生产系统里的证据文件。第 42 篇把每日运行计划落成 JSON让后续命令、告警和排查都能指向同一个文件而不是各自拼一份上下文。