Skip to content

fix: 北交所股票处理差异,异常 (#1234) - #1235

Merged
ZhuLinsen merged 2 commits into
mainfrom
autocode/issue-1234-bug
May 9, 2026
Merged

fix: 北交所股票处理差异,异常 (#1234)#1235
ZhuLinsen merged 2 commits into
mainfrom
autocode/issue-1234-bug

Conversation

@ZhuLinsen

@ZhuLinsen ZhuLinsen commented May 9, 2026

Copy link
Copy Markdown
Owner

PR Type

  • fix
  • feat
  • refactor
  • docs
  • chore
  • test

Background And Problem

  • 当前问题:以最小改动统一支持北交所股票代码的 BJ 后缀/前缀识别、API 校验、前端提交和 Tushare ts_code 调用。
  • 影响范围:本次改动涉及 10 个文件,Diff 为 +182 / -9
  • 触发来源:Issue 自动执行(Issue [Bug] 北交所股票处理差异,异常 #1234)。

Scope Of Change

  • apps/dsa-web/src/components/StockAutocomplete/__tests__/StockAutocomplete.test.tsx
  • docs/CHANGELOG.md
  • docs/full-guide.md
  • docs/full-guide_EN.md
  • docs/openclaw-skill-integration.md
  • src/services/stock_code_utils.py
  • tests/test_analysis_api_contract.py
  • tests/test_name_to_code_resolver.py
  • tests/test_stock_code_bse.py
  • tests/test_stock_code_utils.py

Documentation And Changelog

  • 已同步更新文档/变更记录:docs/CHANGELOG.md, docs/full-guide.md, docs/full-guide_EN.md, docs/openclaw-skill-integration.md

Issue Link

Closes #1234

Verification Commands And Results

./scripts/ci_gate.sh flake8
./scripts/ci_gate.sh offline-tests

关键输出/结论 / Key output & conclusion:

  • lint:PASS, test:PASS

Compatibility And Risk

  • Medium:涉及 apps/dsa-web/src/components/StockAutocomplete/__tests__/StockAutocomplete.test.tsx, docs/CHANGELOG.md, docs/full-guide.md, docs/full-guide_EN.md, docs/openclaw-skill-integration.md, src/services/stock_code_utils.py,建议按文件范围复核。
  • 前提假设:
    • 北交所代码应支持 920493.BJ 这类带后缀输入,并兼容前端识别后点击分析的提交路径。
    • 现有沪深后缀处理逻辑可作为参考,应复用现有 normalize/validate/provider 入口而不是新增平行实现。
    • Tushare Pro 调用需要保留或生成 .BJ 后缀,避免无后缀时落入旧版实时行情接口或被误判为深市。
    • 具体北交所代码段以仓库现有股票代码识别规则为基础,只补齐缺失的 BJ 交易所映射与校验分支。

Rollback Plan

  • git revert <merge-commit> 回滚本 PR 提交,重点确认 apps/dsa-web/src/components/StockAutocomplete/__tests__/StockAutocomplete.test.tsx, docs/CHANGELOG.md, docs/full-guide.md, docs/full-guide_EN.md 恢复正常。

Acceptance Criteria

  • POST /api/v1/analysis/analyze 提交 {"stock_code":"920493.BJ","async_mode":true} 不再返回 validation_error。
  • 前端识别到北交所股票后点击分析时提交的代码格式能被后端接受。
  • Tushare 获取北交所股票数据时使用带 .BJ 的 ts_code,不再因缺少后缀误走旧版接口或默认深市。
  • 现有沪市、深市、港股、美股代码识别与分析行为不回归。

Checklist

  • 本 PR 有明确动机和业务价值 / This PR has a clear motivation and value
  • 已提供可复现的验证命令与结果 / Reproducible verification commands and results are included
  • 已评估兼容性与风险 / Compatibility and risk have been assessed
  • 已提供回滚方案 / A rollback plan is provided
  • 已同步更新相关文档与 docs/CHANGELOG.md,并在 PR 描述中说明文档落点 / Relevant docs and docs/CHANGELOG.md are updated, and the documentation location is stated in this PR

Copilot AI review requested due to automatic review settings May 9, 2026 09:08
@github-actions github-actions Bot added documentation Improvements or additions to documentation size/M testing labels May 9, 2026
@github-actions

github-actions Bot commented May 9, 2026

Copy link
Copy Markdown

🤖 自动审查报告

项目 结果
📊 变更文件 10 个
➕ 新增行数 182 行
➖ 删除行数 9 行
🔍 静态检查 ✅ 通过
🧠 AI 审查 ✅ 已完成

📁 修改的文件

  • 📝 apps/dsa-web/src/components/StockAutocomplete/__tests__/StockAutocomplete.test.tsx (+44/-0)
  • 📝 docs/CHANGELOG.md (+1/-0)
  • 📝 docs/full-guide.md (+1/-1)
  • 📝 docs/full-guide_EN.md (+1/-1)
  • 📝 docs/openclaw-skill-integration.md (+2/-1)
  • 📝 src/services/stock_code_utils.py (+19/-6)
  • 📝 tests/test_analysis_api_contract.py (+67/-0)
  • 📝 tests/test_name_to_code_resolver.py (+17/-0)
  • 📝 tests/test_stock_code_bse.py (+6/-0)
  • 📝 tests/test_stock_code_utils.py (+24/-0)

🧠 AI 代码审查意见

结论: Ready to Merge


审查结果:

  • 必要性: 通过
    • 理由: PR 明确指出了当前北交所股票代码处理存在的差异和异常问题,旨在统一支持 BJ 后缀/前缀识别、API 校验、前端提交和 Tushare ts_code 调用,有明确的业务价值和修复动机。关联 Issue [Bug] 北交所股票处理差异,异常 #1234 也提供了问题背景。
  • 关联性: 通过
    • 证据: PR 描述中明确 Closes #1234,与 Issue 有直接关联。
  • 类型判定: fix
    • 建议类型: 与 PR 描述 [x] fix 和标题 fix: 完全匹配,是正确的类型。
  • 描述完整性: 完整
    • 缺失项: 无。PR 描述包含了所有必要信息,如背景、范围、验证命令与结果、兼容性风险、回滚方案、验收标准以及文档更新说明。特别是对于 ci_gate.sh 的执行情况,PR 描述中已明确给出 lint:PASS, test:PASS,证明已执行并成功。
  • 风险级别:
    • 关键风险: 涉及 src/services/stock_code_utils.py 等核心代码,对股票代码的识别、标准化和验证逻辑进行了修改,可能影响现有沪深港美股的识别。PR 描述中已明确评估为 Medium,并提出“建议按文件范围复核”。本次改动引入的 _valid_exchange_code 很好地降低了错误识别的风险,且测试覆盖全面。
  • 必改项: 无
  • 建议项: 无

详细审查意见:

  1. 代码层面 (src/services/stock_code_utils.py):

    • 引入 _valid_exchange_code 函数是一个非常好的设计,它确保了在识别 BJ 前缀或 .BJ 后缀时,不仅检查代码位数,还会调用 is_bse_code 来验证该数字串是否确实是北交所的代码段,有效避免了将非北交所代码误判为北交所代码(例如 600519.BJ)。这显著提升了代码的健壮性和准确性。
    • _PREFIX_DIGIT_LENS_SUFFIX_DIGIT_LENS 中添加 "BJ": (6,) 的改动是核心功能实现。
    • 更新 is_code_likenormalize_code 中的注释与功能保持一致,体现了良好的代码维护习惯。
  2. 文档层面:

    • docs/CHANGELOG.md 更新了修复记录,符合规范。
    • docs/full-guide.md, docs/full-guide_EN.md, docs/openclaw-skill-integration.md 同步更新了北交所股票代码的格式说明,明确指出支持 BJ 前缀或 .BJ 后缀,这对用户是及时且有用的信息。文档的更新非常全面。
  3. 测试层面:

    • 前端测试: apps/dsa-web/src/components/StockAutocomplete/__tests__/StockAutocomplete.test.tsx 中新增了北交所股票的自动补全测试,确认前端能够正确提交 .BJ 后缀的代码。
    • 后端 API 合约测试: tests/test_analysis_api_contract.py 中新增了接受 920493.BJ 和拒绝 600519.BJ (非北交所代码) 的测试,直接验证了 API 的行为符合预期,尤其是对错误输入的拒绝,这与 _valid_exchange_code 的引入相呼应。
    • 代码解析与标准化测试: tests/test_name_to_code_resolver.pytests/test_stock_code_utils.py 中增加了大量的单元测试,覆盖了 is_code_likenormalize_code 对北交所代码的识别、标准化,以及对非北交所代码带 BJ 提示的拒绝。这些测试覆盖全面,确保了核心工具函数的正确性。
    • 数据提供方集成测试: tests/test_stock_code_bse.py 中新增测试,确认 TushareFetcher 能够正确处理带 .BJ 后缀的代码,确保 Tushare 调用时 ts_code 格式的正确性。
  4. CI 检查:

    • PR 信息显示 CI 自动化流水线和本地 ci_gate.sh 均已通过,保证了代码的语法正确性和基本的质量标准。

总结:
本次 PR 针对北交所股票代码的处理差异提供了全面且稳健的解决方案。变更内容覆盖了前端、后端核心逻辑、文档和各类测试,确保了功能实现的正确性、兼容性和可维护性。代码设计考虑周全,特别是 _valid_exchange_code 的引入,有效降低了潜在的误判风险。测试覆盖广泛且针对性强,满足了所有验收标准。整个 PR 的质量非常高,可以合入。


💡 提示: 请确保代码已通过本地测试,并遵循项目代码规范。

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

本 PR 以最小改动补齐北交所(BSE)股票代码在系统各入口的兼容性:统一支持 BJ 前缀与 .BJ 后缀的识别/校验,使 Web 自动补全提交与后端 Analysis API 校验路径一致,并确保 Tushare ts_code 生成能保留/推导 .BJ

Changes:

  • 后端:在共享股票代码工具中加入 .BJ/BJ 的 code-like 识别与归一化支持,用于 Analysis API 输入边界判断。
  • 前端:补充 StockAutocomplete 测试,覆盖北交所建议项按 canonical .BJ 形式提交。
  • 测试与文档:新增/扩展多处测试用例与用户文档/Changelog,明确北交所代码格式与支持的前后缀形式。

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
apps/dsa-web/src/components/StockAutocomplete/tests/StockAutocomplete.test.tsx 增加北交所建议项的提交行为测试,确保提交 canonical .BJ code。
docs/CHANGELOG.md [Unreleased] 末尾追加北交所 BJ 前后缀校验修复记录。
docs/full-guide.md 更新“支持的股票代码格式”表格,补充 BJ 前缀/.BJ 后缀说明与示例。
docs/full-guide_EN.md 同步英文指南中的北交所代码格式说明与示例。
docs/openclaw-skill-integration.md 更新 openclaw Skill 集成文档中的北交所代码格式说明与示例。
src/services/stock_code_utils.py 在 code-like 识别/归一化工具中加入 .BJ/BJ 支持。
tests/test_analysis_api_contract.py 增加 Analysis API 合约测试:autocomplete 来源的 920493.BJ 不再触发 name resolver 且可入队。
tests/test_name_to_code_resolver.py 扩展 resolver 的 code-like/normalize 边界测试,覆盖 BJ920493/920493.BJ
tests/test_stock_code_bse.py 扩展 TushareFetcher 北交所 exchange hint 输入的保留/转换测试。
tests/test_stock_code_utils.py 扩展 stock_code_utils 的 is_code_like/normalize_code 测试,覆盖 .BJBJ

Comment thread src/services/stock_code_utils.py Outdated
@@ -69,7 +71,7 @@ def normalize_code(raw: str) -> Optional[str]:
Supports:
- Plain digit codes: 600519, 00700
- Suffix format: 600519.SH, 600519.SZ, 00700.HK

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7d0c93dae5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

".SH": (6,),
".SZ": (6,),
".SS": (6,),
".BJ": (6,),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject non-BSE codes with BJ exchange hints

When a user submits a non-BSE six-digit code with a BJ hint, e.g. 600519.BJ (or BJ600519 via the new prefix entry), this length-only rule makes is_code_like accept it and the analysis API bypasses name resolution; downstream TushareFetcher._convert_stock_code preserves any dotted suffix unchanged, so the request is sent as the invalid 600519.BJ instead of being rejected or treated as Shanghai. The repo already defines BSE ranges in is_bse_code, so the BJ prefix/suffix path should validate those ranges rather than accepting any 6 digits.

Useful? React with 👍 / 👎.

@ZhuLinsen ZhuLinsen left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

评审结论

  • 必要性:通过,修复北交所 BJ 前缀/.BJ 后缀在 API 校验、前端自动补全提交与 Tushare ts_code 路径中的一致性问题,符合 Issue #1234 的目标。
  • 是否有对应 issue:有,Closes #1234
  • PR 类型:fix,改动集中在股票代码识别/归一化与相关回归测试、文档同步。
  • description 完整性:完整,已包含动机、范围、Issue、验证命令与结果、兼容性风险、文档落点和回滚方案,符合 AGENTS.md 对 fix 类 PR 的说明要求。
  • 是否可直接合入:可;当前 CI 全部成功,改动已同步 docs/CHANGELOG.md 与中英文相关文档,且新增测试覆盖 API、resolver、工具函数、Tushare 转换与前端提交路径。mergeable_state=blocked 在当前事实下不等同于代码不可合入,更像分支保护/权限状态。

🤖 此回复由 OpenReview Bot 自动生成,仅供参考。如有疑问请 @维护者。

@ZhuLinsen
ZhuLinsen force-pushed the autocode/issue-1234-bug branch from 6a73fc9 to ac76234 Compare May 9, 2026 12:02

@ZhuLinsen ZhuLinsen left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

评审结论

  • 必要性:通过,修复北交所 BJ 前缀/.BJ 后缀在 API 校验、前端提交与 Tushare ts_code 路径中的不一致,属于真实用户路径问题。
  • 是否有对应 issue:有,Closes #1234
  • PR 类型:fix,改动集中在股票代码识别/归一化、API 契约回归、前端提交测试和对应文档同步。
  • description 完整性:完整,已覆盖动机、范围、Issue、验证命令与结果、兼容性风险、文档落点和 revert 级回滚方案,符合 AGENTS.md 对 fix 类 PR 的交付要求。
  • 是否可直接合入:可,当前 CI 全部成功;mergeable_state=blocked 更像分支保护/权限状态,不构成代码层阻断。

🤖 此回复由 OpenReview Bot 自动生成,仅供参考。如有疑问请 @维护者。

@ZhuLinsen
ZhuLinsen merged commit d6e4699 into main May 9, 2026
10 checks passed
cwjyu pushed a commit to cwjyu/daily_stock_analysis that referenced this pull request May 10, 2026
* fix(issue-1234): [bug]-北交所股票处理差异,异常
zbl-96 pushed a commit to zbl-96/daily_stock_analysis that referenced this pull request May 10, 2026
* fix(issue-1234): [bug]-北交所股票处理差异,异常
EchoingFootsteps pushed a commit to EchoingFootsteps/daily_stock_analysis that referenced this pull request Jul 4, 2026
* fix(issue-1234): [bug]-北交所股票处理差异,异常
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation size/M testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 北交所股票处理差异,异常

2 participants