Skip to content

fix(data): akshare backfill 静默返 0 根修复 + factor_candidates 多租户隔离#135

Merged
mirror29 merged 7 commits into
mainfrom
fix/akshare-backfill-silent-zero
Jul 9, 2026
Merged

fix(data): akshare backfill 静默返 0 根修复 + factor_candidates 多租户隔离#135
mirror29 merged 7 commits into
mainfrom
fix/akshare-backfill-silent-zero

Conversation

@mirror29

@mirror29 mirror29 commented Jul 8, 2026

Copy link
Copy Markdown
Owner

变更

1. akshare backfill 静默返 0 根修复

  • akshare.py 重写:requests → akshare/baostock 直连,加全局锁 + per-symbol 缓存 + 超时控制
  • 接口不可用/超时异常冒泡(之前静默返空 DataFrame → 调用方误以为无数据)
  • baostock 持久会话:进程级单次 login,避免每请求 ~4.3s 重连开销
  • ruff 全修:15 处格式/类型检查报错(E402/F401/F821/B905)

2. 市场路由修正

  • 港/日/英/德股从 akshare 切到 yfinance——a-stock-data 配方专注 A 股
  • orchestrator market.ts / data.ts / paper.ts / swarm.ts 四文件同步更新 venue 路由文档

3. factor_candidates 多租户数据隔离(cherry-pick from feat/e2-evolution-phase1)

  • owner_account_id 字段:因子发现候选按账户隔离
  • 写入/查询/审核校验全部带账户校验
  • 新增 services/factor/src/inalpha_factor/account_id.py(JWT sub → 稳定 UUID)

DB 迁移

  • 0028_factor_candidates_owner:factor_candidates 加 owner_account_id
  • 0030_bars_compression:K 线压缩表(支撑多 granularity 缓存)

验证

  • ruff 全过(本地修复 + CI 验证)
  • orchestrator typecheck
  • mypy(不挡 CI,|| true

🤖 Generated with Claude Code

核心修复(akshare.py):
- 重写 _fetch_bars 回填路径:requests → akshare 直连 + 并发锁 + 超时控制
- 接口不可用/超时将异常冒泡而非静默返空 DataFrame
- 全局 _akshare_lock + per-symbol 缓存避免重复请求与频繁重连

市场路由修正:
- 港/日/英/德股从 akshare 切到 yfinance(a-stock-data 配方专注 A 股)
- orchestrator market context / data/paper/swarm tools 同步更新 venue 路由文档

DB 迁移:
- 0030_bars_compression: K 线压缩表支撑多 granularity 缓存(隔夜/周末/周期数据应用)

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jul 8, 2026

Copy link
Copy Markdown

Review base: 86da8e3

🤖 GLM-5.2 PR Review

⚠️ review 未完成:GLM API HTTP 503:{"error":{"message":"The system is busy, please try again later.","type":"api_error","param":"","code":10310}}

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 8, 2026

Copy link
Copy Markdown

Deploying inalpha-web with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8274fab
Status: ✅  Deploy successful!
Preview URL: https://a8fed8e2.inalpha-web.pages.dev
Branch Preview URL: https://fix-akshare-backfill-silent.inalpha-web.pages.dev

View logs

mirror29 and others added 2 commits July 8, 2026 18:28
根因:factor_candidates 表无 owner_account_id 列,所有 candidates API 端点(propose/list/review)无用户身份过滤,所有用户看到全部候选池。

修复(对齐 paper strategy_candidates 的 owner_account_id 模式):
- Migration 0028: 加 owner_account_id UUID 列 + (owner_account_id, created_at DESC) 复合索引
- storage: insert_candidate 加 owner_account_id 参数写入;list_candidates 加 owner_account_id 过滤
- schema: FactorCandidateRecord 加 owner_account_id 字段
- API: 三个 endpoints 全部加 Depends(get_current_user);propose 设 proposed_by/owner_account_id 为当前用户;list 按 owner_account_id 过滤;review 校验候选所有权(非 owner 返回 404)
- 新建 factor 的 account_id.py(与 paper 共用同一 namespace 确保跨服务稳定映射)

不修改:catalog/timing/score/panel 等纯计算端点(无用户数据);
custom_registry.list_registered 全局注册目录(所有用户共享)。

Co-Authored-By: Claude <noreply@anthropic.com>
- 移动 import threading 到文件顶部
- 移除 fetch_financials 中未使用的 import math/baostock
- _query_baostock_financials 内部补 import math as _math(_math.isnan 真正使用点)
- 12 处 zip() 补 strict=True

Co-Authored-By: Claude <noreply@anthropic.com>
@mirror29 mirror29 changed the title fix(data): akshare backfill 静默返 0 根修复——加锁/超时/日志/异常捕获 fix(data): akshare backfill 静默返 0 根修复 + factor_candidates 多租户隔离 Jul 8, 2026
Co-Authored-By: Claude <noreply@anthropic.com>
@mirror29

mirror29 commented Jul 9, 2026

Copy link
Copy Markdown
Owner Author

trigger re-review

@mirror29

mirror29 commented Jul 9, 2026

Copy link
Copy Markdown
Owner Author

Claude Code review result — let's see what was found

mirror29 and others added 3 commits July 9, 2026 09:53
1. baostock login fork 安全:已登录状态加 query_trade_dates 探活,
   连接失效时自动重新 login(进程 fork 后状态不一致)

2. 港股 K 线 df.to_dict 空安全:空值检查已在 elif prefix hk 分支内完成

3. 港股财报空值日志:stock_hk_financial_abstract 返回 None 时打
   warning 日志并返回空 dict,不再静默传给 _flatten_financial_abstract

Co-Authored-By: Claude <noreply@anthropic.com>
1. _ensure_bs_login 死锁修复:移除持锁后的 bs.login 调用,锁内只操作
   _bs_logged_in 标志;fork 安全通过 Uvicorn 多 worker 的内建支持保证

2. 港股财报空值检查修复:ak.stock_hk_financial_abstract 返回 DataFrame
   而非 dict,改用 hasattr(raw, 'empty') 检测

3. baostock PIT 年报补拉修复:profit_annual 补拉用 ref.year - 1 而非
   py - 1,避免 PIT 截断后指向错误的较早年份

4. factor review 接口 TOCTOU 修复:review SQL 的 WHERE 中原子检查
   owner_account_id,不再先查后改(Time-of-check Time-of-use 竞态关闭)

Co-Authored-By: Claude <noreply@anthropic.com>
1. _query_first year_range 扩大到 10 年(4→10),覆盖退市股/停牌股
2. _ensure_bs_login 注释改为诚实说明不 fork-safe(多 worker 用 --workers 1)
3. _period_ok 解析失败时 return False(之前放行,泄漏未来数据);
   增加多格式兼容(%Y-%m-%d 和 %Y-%m-%d %H:%M:%S)
4. dividends 年份范围从硬编码 2023-2025 改为动态 ref.year - 5
5. balance/growth/cash_flow/operation/dupont 在同季无数据时 fallback 到年报(Q4)

Co-Authored-By: Claude <noreply@anthropic.com>
@mirror29

mirror29 commented Jul 9, 2026

Copy link
Copy Markdown
Owner Author

retry review after GLM API 503

@mirror29 mirror29 merged commit d208b88 into main Jul 9, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant