Skip to content

variant補間の再帰深さ上限をroute毎の停留所数から動的に算出 (Closes #1513)#1520

Merged
TinyKitten merged 1 commit into
devfrom
fix/variant-chain-recursion-depth
May 15, 2026
Merged

variant補間の再帰深さ上限をroute毎の停留所数から動的に算出 (Closes #1513)#1520
TinyKitten merged 1 commit into
devfrom
fix/variant-chain-recursion-depth

Conversation

@TinyKitten
Copy link
Copy Markdown
Member

@TinyKitten TinyKitten commented May 15, 2026

概要

build_stop_route_mapping()prev_chain / next_chain 再帰 CTE が固定の depth < 10 で打ち切られていたため、メイン系統に戻るまでに 11 停留所以上ある variant 経路でアンカー解決に失敗し、estimated_seq が末尾フォールバック (mtms.max_seq + 9999) や「next のみ解決」分岐に落ちて停留所順が崩れる不具合があった。再帰深さの上限を route ごとの variant-only 停留所数から動的に算出するように修正する。

変更の種類

  • バグ修正
  • 新機能
  • データの修正・追加
  • リファクタリング
  • ドキュメント
  • CI/CD
  • その他

変更内容

  • variant_chain_limit CTE を新設し、route ごとに variant_only_with_neighbors の行数 + 1 を max_depth として算出する。visited 配列で各 variant 停留所は 1 度しか踏まないことが保証されているため、stop 数 + 1 はサイクル防止と整合したタイトな上限になる。
  • prev_chain / next_chainWHERE pc.depth < 10 / nc.depth < 10variant_chain_limit への JOIN 経由で depth < vcl.max_depth に置き換え。
  • 長い variant チェーン (M01 → V01..V12 → M15) のシナリオで V02..V11 が両端 (M01, M15) を再帰解決して中央付近 (~7.91〜8.09) に補間されることを検証する SQL 回帰テスト test_build_stop_route_mapping_long_variant_chain_dynamic_limit を追加。旧実装では V11 が depth < 10 で打ち切られ M14 付近 (~14.8) に流される回帰が、本テストで検出できる。

テスト

  • cargo fmt --all -- --check が通ること
  • cargo clippy -- -D warnings が通ること
  • cargo testSQLX_OFFLINE=true)が通ること

加えて、TEST_DATABASE_URL を指定して cargo test -p stationapi --features integration-tests --bin stationapi build_stop_route_mapping を実行し、既存 6 件 + 新規 1 件 = 計 7 件の SQL 回帰テストが全て pass することを確認済み。

関連Issue

Closes #1513

スクリーンショット(任意)

Summary by CodeRabbit

  • バグ修正

    • GTFS統合ロジックを改善し、複数のバリアント停留所が含まれるルートで停留所の配置順序がより正確に処理されるようになりました。ルート末尾への誤った配置が防止されます。
  • テスト

    • バリアント停留所チェーン処理の検証テストを追加しました。

Review Change Stack

`build_stop_route_mapping()` の `prev_chain` / `next_chain` 再帰 CTE が
`depth < 10` の固定値で打ち切られていたため、メイン系統に戻るまでに
11 停留所以上ある variant 経路ではアンカー解決に失敗し、`estimated_seq`
が末尾フォールバック (`mtms.max_seq + 9999`) や「next のみ解決」分岐に
落ちて停留所順が崩れていた。

route ごとの `variant_only_with_neighbors` の行数から `max_depth` を
計算する `variant_chain_limit` CTE を追加し、`prev_chain` / `next_chain`
の上限をこれに置き換える。`visited` 配列で各 variant 停留所は 1 度しか
踏まないことが保証されているため、stop 数 + 1 はサイクル防止と整合した
タイトな上限になる。

長い variant チェーン (M01 → V01..V12 → M15) のシナリオで V02..V11 が
両端を再帰解決して中央付近に挿入されることを検証する SQL 回帰テスト
`test_build_stop_route_mapping_long_variant_chain_dynamic_limit` を追加。

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@TinyKitten TinyKitten self-assigned this May 15, 2026
@github-actions github-actions Bot added fix 直した deploy-dev and removed fix 直した labels May 15, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3452ac1f-8ed6-4830-9b15-46fd5a31818d

📥 Commits

Reviewing files that changed from the base of the PR and between dce816f and 59cdafc.

📒 Files selected for processing (1)
  • stationapi/src/import.rs

📝 Walkthrough

Walkthrough

build_stop_route_mapping の SQL で、variant 停留所の prev/next 連鎖を追跡する再帰 CTE の打ち切り条件を、固定値 depth < 10 から動的上限 variant_chain_limit.max_depth に置き換え、長い variant-only 連鎖のケースで再帰が正常に解決されるよう改善。回帰テストを追加。

Changes

Variant Chain Recursion Depth Limit

Layer / File(s) Summary
Dynamic variant chain depth limit calculation and application
stationapi/src/import.rs
ルートごとの variant-only 停留所数から variant_chain_limit.max_depth を算出する CTE を新規追加。prev_chainnext_chain の再帰ステップで、固定条件 pc.depth < 10 および nc.depth < 10pc.depth < vcl.max_depth および nc.depth < vcl.max_depth に置き換える。
Long variant chain regression test
stationapi/src/import.rs
12 段の variant-only チェーンシナリオで期待される stop_sequence 配置を検証する test_build_stop_route_mapping_long_variant_chain_dynamic_limit テストを追加。

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

  • TrainLCD/StationAPI#1514: 同じ関数の variant-only チェーン・再帰深さ挙動に関する既存テストインフラおよびテストケースを扱っており、本 PR の長チェーン検証と直接的に関連。

Possibly related PRs

  • TrainLCD/StationAPI#1519: 同じ build_stop_route_mapping 関数の variant-only 停留所の再帰 CTE 打ち切り条件(depth < 10variant_chain_limit.max_depth)変更と、variant-only 連鎖補間の SQL 回帰テスト追加で直接関連。

  • TrainLCD/StationAPI#1512: 同一ファイルの build_stop_route_mapping における variant-only 停留所の prev/next 連鎖(再帰 CTE)ロジックを両方とも変更しており、本 PR の再帰打ち切り条件を variant_chain_limit.max_depth へ置き換える点で深く関連。

Suggested labels

fix

Poem

ウサギが掘るトンネル、深くなりけり 🐰
固い壁では 10 で止まるけど
今は数えて動的に
長い連鎖も道へ帰ろう
テストで確かめ、安心かな ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed タイトルはPRの主要な変更内容(variant補間の再帰深さ上限の動的算出)を簡潔かつ明確に要約しており、Issue #1513を参照している
Description check ✅ Passed PR説明は必須テンプレートのセクションをほぼ完全に記入しており、問題背景、修正内容、テスト実施状況、関連Issueが明記されている。
Linked Issues check ✅ Passed PR内の修正は#1513の要求(再帰深さ上限の動的算出)を完全に実装し、variant-only停留所数+1をmax_depthとする具体的解決策を提供している。
Out of Scope Changes check ✅ Passed 変更内容はすべて#1513の要求範囲内であり、variant_chain_limitの追加、prev_chain/next_chainの修正、回帰テストの追加のみで、スコープ外の変更は見当たらない。
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/variant-chain-recursion-depth

Comment @coderabbitai help to get the list of available commands and usage tips.

@TinyKitten TinyKitten merged commit fb65f5d into dev May 15, 2026
11 checks passed
@TinyKitten TinyKitten deleted the fix/variant-chain-recursion-depth branch May 15, 2026 12:58
@TinyKitten TinyKitten mentioned this pull request May 15, 2026
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

バス経路順序: variant補間の再帰深さ上限が固定10でアンカー解決に失敗するケースがある

1 participant