Skip to content

feat(reference): Vector embedding 検索 MVP(Phase 4-E / Refs #191)#198

Merged
akiojin merged 2 commits into
developfrom
work/20260511-0223
May 11, 2026
Merged

feat(reference): Vector embedding 検索 MVP(Phase 4-E / Refs #191)#198
akiojin merged 2 commits into
developfrom
work/20260511-0223

Conversation

@akiojin
Copy link
Copy Markdown
Owner

@akiojin akiojin commented May 11, 2026

Summary

  • SPEC feat(reference): unity-csharp-reference Phase 4 - 内部実装の改善 (umbrella) #191 (Phase 4 umbrella) の最終サブタスク Phase 4-E として、Vector embedding 検索を MVP として land する。
  • fastembed-rs (BGE-Small-EN, ONNX Runtime) で symbol 単位の embedding を生成し、線形 scan ベースの cosine 類似度検索で曖昧クエリから関連シンボルを引けるようにする。
  • gwt-discussion で確定したスコープ: lib = fastembed-rs + 線形 scan / build = explicit reference embed-build / 対象 = symbol unit (Phase 2 + 4-A index 再利用) / model = BAAI/bge-small-en-v1.5 (~130MB)。

Changes

  • Cargo.toml: fastembed = "4", bincode = "1"[dependencies] に追加。fastembed は内部で ort (ONNX Runtime) / hf-hub / tokenizers を pull する。
  • src/reference/embed.rs (新規):
    • Embedder trait + FastEmbedder (ONNX 実装) + test 用 MockEmbedder
    • EmbeddedSymbol / EmbeddingIndex の serde 型
    • symbol_to_text / build_embedding_index / save_embedding_index / load_embedding_index / search / cosine_similarity の純関数
    • 9 件のテスト(純関数中心、ONNX 不要)
  • src/reference/mod.rs: execute_embed_build / execute_embed_search を追加し、maybe_execute_reference_toolreference_embed_build / reference_embed_search 分岐を登録。dispatcher 経由テスト 3 件 (required query / cache missing / index missing)。
  • src/cli.rs: ReferenceCommand::EmbedBuild { version } / EmbedSearch { query, version, top_k } variant。
  • src/app/runner.rs::build_reference_call: 2 分岐 + 4 件のテスト。
  • src/tooling/tool_catalog.rs: 2 tool を TOOL_NAMES / tool_executor::Local / is_read_only_tool (search のみ read_only、build は mutating) / tool_params_schema (required: query / なし) / parity count (127→129) に登録。
  • .claude-plugin/plugins/unity-cli/skills/unity-csharp-reference/SKILL.md: ## Preferred Flow に embed-build / embed-search の使い分けを追記。
  • .claude-plugin/plugins/unity-cli/skills/unity-csharp-reference/references/embed-search-playbook.md (新規): Phase 4-E の運用ガイド。
  • docs/tools.md: Reference Cache (9 → 11 tools)、新 tool 行を追加。

Testing

  • cargo fmt -- --check — clean
  • cargo clippy --all-targets -- -D warnings — clean
  • cargo test --bin unity-cli408 passed / 0 failed (embed 9 + dispatcher 3 + runner 4 を含む)
  • cargo llvm-cov --all-targets --summary-only -- --test-threads=1 — TOTAL line coverage 90.67%
  • unity-cli skills lint --severity error — 15 skills / 0 violations

ONNX 実 model を呼ぶ FastEmbedder は test 内では使わず、MockEmbedder で純関数経路をカバーするため CI でモデル download は発生しない。

Closing Issues

Related Issues / Links

Checklist

  • Tests added/updated (16 件 / MockEmbedder で ONNX を回避)
  • Lint/format passed (cargo clippy, cargo fmt, unity-cli skills lint)
  • Documentation updated (skill SKILL.md / references/embed-search-playbook.md / docs/tools.md)
  • Migration/backfill plan included — Not applicable: 既存 .unity-cli-index/symbols.json (Phase 2) には触らず、embeddings.bin を別ファイルとして追加する。先行 PR でも gitignore 済みの .unity-cli-index/ 配下。
  • CHANGELOG impact considered — minor bump per Conventional Commits (feat)

Context

Phase 2 / 4-A で構築した symbol index は名前完全一致の lookup なので、「Animator まわりで使うコールバック型」のような曖昧な要求には応えられなかった。本 PR で reference embed-search --query "..." を提供し、LLM が自然言語クエリから関連シンボルを発見できるようにする。これで unity-csharp-reference の Phase 1-4 ロードマップ (umbrella #191 含む A/B/C/D/E) がすべて land する。

Out of Scope (umbrella #191 で継続管理)

  • 多言語 model (multilingual-e5-large, ~520MB) は --model オプションで後追い
  • HNSW / IVF / Annoy などの ANN は線形 scan MVP の後段に検討
  • file-chunk 単位の embedding は別軸として後追い検討
  • score 正規化 / re-ranking は未対応
  • incremental rebuild (symbol index 更新時に embeddings.bin を patch する) は未対応

Notes

  • fastembed = "4" 依存追加で Cargo.lock が大きく増えた (+~2000 lines)。ort / hf-hub / tokenizers の解決による。target binary は ONNX Runtime の dynamic load 機構を使うため、起動時 model 読み込み以外のオーバーヘッドは限定的。
  • embed-build は数千 symbol 規模で数十秒〜数分。--top-k の既定値は 10。
  • MockEmbedder は char コードを使った決定的なベクトルを返すだけの test double で、CI 環境で ONNX の有無に依存しない。

SPEC #191 (Phase 4 umbrella) 最終サブタスク E を land する。
fastembed-rs (BGE-Small-EN, ONNX Runtime) で symbol 単位の embedding
を生成し、線形 scan ベースの cosine 類似度検索で「Animator まわり
で使うコールバック型」のような曖昧 query から関連シンボルを引ける
ようにする。子 SPEC は作らず umbrella #191 に統合。

採用方針 (gwt-discussion で確定):

- lib: fastembed-rs + 線形 scan (MVP, HNSW は後追い余地)
- build timing: 明示的な `reference embed-build`
- 対象: Phase 2 / 4-A の symbol index を再利用 (type / method / property)
- model: fastembed-rs default (BAAI/bge-small-en-v1.5, ~130MB)

主な変更:

- Cargo.toml: fastembed = "4", bincode = "1" を追加
- src/reference/embed.rs (新規): Embedder trait と FastEmbedder /
  MockEmbedder、symbol_to_text / build_embedding_index /
  save_embedding_index / load_embedding_index / search /
  cosine_similarity の純関数 + 9 件のテスト
- src/reference/mod.rs: maybe_execute_reference_tool に
  reference_embed_build と reference_embed_search 分岐、
  dispatcher 経由テスト 3 件
- src/cli.rs: ReferenceCommand::EmbedBuild / EmbedSearch variant
- src/app/runner.rs::build_reference_call: 2 分岐と 4 件のテスト
- src/tooling/tool_catalog.rs: 2 tool を TOOL_NAMES / executor /
  read_only (search のみ) / params_schema / parity count (127→129)
  に登録
- skill SKILL.md / references/embed-search-playbook.md (新規) /
  docs/tools.md: Reference Cache (9→11 tools) と embed-build /
  embed-search の使い分けを反映

設計上の意図的スコープアウト (umbrella #191 で継続管理):

- 多言語 model (multilingual-e5-large) は --model オプションで後追い
- HNSW / IVF ベースの ANN は線形 scan の後段に検討
- file-chunk 単位の embedding は別軸として後追い
- score 正規化 / re-ranking は未対応

ONNX 実 model を呼ぶ部分は FastEmbedder で隔離し、test 内では
MockEmbedder を用いるので CI で model download が発生しない。実
embed-search の動作確認は user の手元で `embed-build` を 1 度
走らせる前提。

ローカル検証:

- cargo fmt -- --check: clean
- cargo clippy --all-targets -- -D warnings: clean
- cargo test --bin unity-cli: 408 passed / 0 failed
- cargo llvm-cov --all-targets: TOTAL line coverage 90.67%
- unity-cli skills lint --severity error: 15 skills / 0 violations

Refs #191

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 11, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 9d7a6128-4359-4bf1-a1d0-aa38ff7b5db2

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch work/20260511-0223

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

vectors.into_iter() は既に IntoIterator なので冗長。zip(vectors) に
書き換え。

Refs #191

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@akiojin akiojin merged commit e5e9a70 into develop May 11, 2026
9 checks passed
@akiojin akiojin deleted the work/20260511-0223 branch May 11, 2026 16:40
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