# wbt v0.2.2
> Release date: 2026-05-23
> Crate tag: `crate-v0.2.2` · Python tag: `v0.2.2`
## Summary
PATCH release restoring three fields that `STATS_FIELD_ORDER` already declared
but `WeightBacktest.segment_stats` failed to return. No public API
addition/removal — `segment_stats` now matches the documented schema.
## Fixes
- **`segment_stats` 输出补齐 `多头占比` / `空头占比` / `品种数量`**: 之前 `WeightBacktest.segment_stats(kind=...)`
在三种 `kind`(`多空` / `多头` / `空头`)下都不返回这三个字段,与 `WeightBacktest.stats`
及 `STATS_FIELD_ORDER` 声明的 canonical 字段集不一致。
- Rust 侧在 `DailyTotals` 新增 `weight_count_dates` 与三个 `*_per_day` 计数向量,
按 YYYYMMDD 升序覆盖所有出现过权重行的日期(与 `date_keys` 解耦:后者按 symbol-day
聚合,会丢掉每个 symbol 的最后一天)。`segment_stats` 用 `partition_point` 在该向量上
二分锁定 `[sdt, edt]`,求和后注入 `多头占比` / `空头占比`,`品种数量` 直接取 `self.symbols.len()`。
- `多头占比` / `空头占比` 描述 `[sdt, edt]` 范围内权重行的多空构成,与 `kind` 无关
(`kind` 只影响收益序列与交易对方向的筛选)。
- 覆盖测试:Rust `segment_stats_contains_long_short_rate`(三种 kind 字段存在性 +
自洽性 + 每日计数向量与全局计数累计相等);Python `TestSegmentStatsLongShortRate`
两条用例。
## Internal
- 新增 `.pre-commit-config.yaml`,本地镜像 CI 的 `Rust Lint` job(`cargo fmt --check` +
`cargo clippy -D warnings`),单次启用 `pre-commit install`。
## Compatibility
- `BREAKING CHANGE`: none. 仅向 `segment_stats` 返回的 dict 增加三个键,已声明在
`STATS_FIELD_ORDER` 中,旧调用方读取既有字段不受影响。
- Python: requires ≥ 3.10 (unchanged).
- Rust: edition 2024, `pyo3 = 0.28` / `numpy = 0.28` (unchanged).
## Release checklist outcome
- **§1 SemVer**: 修复 dict 缺字段,公共 API 表面(`__init__.py` / `_wbt.pyi`)无 diff
→ PATCH bump (`0.2.1 → 0.2.2`)。
- **§2 Lint/typecheck**: `cargo fmt --check`、`cargo clippy -D warnings`、
`cargo test --lib`(143 passed)、`ruff check`、`ruff format --check`、`basedpyright` 全清。
- **§3 Tests**: `pytest` 233 passed / 1 skipped(含 2 条新增 `segment_stats` 用例)。
- **§4 LLM review**: 范围限定在 `src/core/backtest.rs:336`(`segment_stats` 路径)+
`src/core/native_engine.rs:535`(`process_all_symbols` 每日计数累加)+
`src/core/report.rs` 测试 fixture + `python/wbt/backtest.py:351` docstring +
新增 Python 用例。重点核查:(a) `weight_count_dates` 与 `date_keys` 的对齐错位
——已通过新增 `segment_stats_contains_long_short_rate` 显式断言每日计数向量求和与
全局 `long_count` / `short_count` / `total_weight_rows` 相等来防护;
(b) `partition_point` 边界(`< sdt` / `<= edt`);(c) 全零段返回 `(0.0, 0.0)`。
- **§5 doc-vs-code**: 同步更新 `WeightBacktest.segment_stats` docstring,
显式说明返回字段与 `stats` 同构、`多头占比` / `空头占比` 不受 `kind` 影响。
## Known issues (carried forward from v0.2.0)
Unchanged; see `docs/release_notes/v0.2.0.md`.