Skip to content

feat(reference): UnityCsReference のローカルキャッシュ参照機構(Phase 1)#186

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

feat(reference): UnityCsReference のローカルキャッシュ参照機構(Phase 1)#186
akiojin merged 5 commits into
developfrom
work/20260511-0223

Conversation

@akiojin
Copy link
Copy Markdown
Owner

@akiojin akiojin commented May 11, 2026

Summary

  • Add unity-cli reference subcommand family and unity-csharp-reference skill so LLMs and developers can browse the official Unity C# source (UnityCsReference) as a read-only local cache when implementing Unity scripts.
  • Establish a per-Unity-version cache layout under ~/.unity/cache/UnityCsReference/<version>/ driven by ProjectVersion.txt detection plus a static branch map.
  • Land the SPEC feat(reference): UnityCsReference のローカルキャッシュ参照機構(Phase 1) #185 Phase 1 scope only; Phase 2 (symbol lookup index) and Phase 3 (cross-version diff / LSP integration) will be tracked as separate SPECs.

Changes

  • src/reference/version.rs: parse ProjectSettings/ProjectVersion.txt, map major.minor to UnityCsReference branches (2020.3/staging through 6000.0/staging), surface --branch hint for unmapped versions.
  • src/reference/cache.rs: reference_root, version_dir, read_meta / write_meta (.unity-cli-meta.json), list_versions, and gc(keep, dry_run) for LRU-by-mtime cleanup.
  • src/reference/fetcher.rs: build_clone_args (--depth 1 --single-branch --branch <ver>), license gate (--accept-license / UNITY_CLI_ACCEPT_LICENSE=1), git availability probe, GITHUB_TOKEN / GH_TOKEN injection via http.extraHeader.
  • src/reference/search.rs: run_grep (walkdir + regex with optional file-name glob and context lines) and run_view (line range slice, .. traversal guard).
  • src/reference/mod.rs: maybe_execute_reference_tool JSON dispatcher for reference_fetch / reference_status / reference_search / reference_grep / reference_view / reference_clean.
  • src/cli.rs and src/app/runner.rs: Command::Reference plus ReferenceCommand and build_reference_call helper routing each subcommand to execute_tool("reference_*", params).
  • src/tooling/tool_catalog.rs and src/tooling/local_tools.rs: register reference_* 6 tools, mark them ToolExecutor::Local, classify fetch/clean as mutating and status/search/grep/view as read-only, define per-tool params_schema, and delegate from maybe_execute_local_tool.
  • src/core/managed_binaries.rs: add cache_root() (env override via UNITY_CLI_CACHE_ROOT, defaults to ~/.unity/cache).
  • .claude-plugin/plugins/unity-cli/skills/unity-csharp-reference/: new skill (SKILL.md + references/{runtime-checklist,fetch-and-cache,symbol-lookup-playbook}.md) under Skill Contract v1. .claude/skills/unity-csharp-reference and .agents/skills/unity-csharp-reference symlinks added for discovery.
  • .claude-plugin/plugins/unity-cli/skills/unity-csharp-{edit,navigate}/SKILL.md and unity-scene-inspect/SKILL.md: cross-list unity-csharp-reference in siblings to clear R12 / R22 collisions.
  • docs/tools.md, README.ja.md, ATTRIBUTION.md: new Reference Cache section, README feature line, and Unity Companion License attribution (English + Japanese).
  • tests/fixtures/reference-cache/: minimal fixture (Editor + Runtime/Export/Animation) used by grep / view snapshot tests.

Testing

  • cargo fmt -- --check — clean (no diff)
  • cargo clippy --all-targets -- -D warnings — clean
  • cargo test --bin unity-cli — 278 passed, 0 failed (19 new tests across reference modules)
  • unity-cli skills lint --severity error — 15 skills checked, 0 violations
  • CLI smoke: UNITY_CLI_CACHE_ROOT="$(mktemp -d)" unity-cli reference status --output json{ "ok": true, "versions": [] }

Closing Issues

Related Issues / Links

  • None (Phase 2 / Phase 3 follow-up SPECs will be filed after this lands)

Checklist

  • Tests added/updated
  • Lint/format passed (cargo clippy, cargo fmt, unity-cli skills lint)
  • Documentation updated (docs/tools.md, README.ja.md, ATTRIBUTION.md, new skill)
  • Migration/backfill plan included — Not applicable: no schema or data change.
  • CHANGELOG impact considered — minor bump per Conventional Commits (feat)

Context

  • unity-cli previously gave LLMs access only to project-local C# sources and csharp-lsp (OmniSharp/Roslyn) metadata when implementing Unity scripts. The official Unity C# implementation was unreachable, so API misuse, behavior assumptions, and version-specific differences had to be guessed.
  • SPEC feat(reference): UnityCsReference のローカルキャッシュ参照機構(Phase 1) #185 (gwt-spec) was filed after a gwt-discussion session decided to mirror UnityCsReference locally as a read-only cache exposed through unity-cli reference and a sibling unity-csharp-reference skill.

Risk / Impact

  • Affected areas: new src/reference/ module + CLI / catalog / local dispatcher wiring; new skill alongside existing unity-csharp-{navigate,edit} / unity-scene-inspect (siblings cross-listed). Cache lives under ~/.unity/cache/UnityCsReference/<version>/ and is independent of ~/.unity/tools/ (managed_binaries). LSP workspace folders are intentionally untouched to avoid rename/diagnostic noise from external sources.
  • Disk footprint: shallow clones land at roughly 400-600 MB per Unity version. reference clean --keep 1 is the default knob; reference status --output json surfaces sizeBytes.
  • License: cached source is under Unity Companion License. Fetch is hard-gated by --accept-license or UNITY_CLI_ACCEPT_LICENSE=1; ATTRIBUTION.md documents the consent and no-redistribution rule.
  • Rollback plan: revert this commit. The reference cache directory can be removed safely (rm -rf ~/.unity/cache/UnityCsReference). No persisted state outside that directory.

Notes

SPEC #185 の Phase 1 を完了。Unity 公式 C# ソース UnityCsReference を
`unity-cli reference fetch` でローカルキャッシュ
(`~/.unity/cache/UnityCsReference/<version>/`)し、grep / view / status / clean
のサブコマンドと `unity-csharp-reference` skill から読み取り専用で参照できる
ようにする。

- src/reference/{mod,version,cache,fetcher,search}.rs を新規追加
  - ProjectVersion.txt からの Unity バージョン検出と静的ブランチマッピング
  - git clone --depth 1 --single-branch --branch <ver-branch> の shallow clone
  - walkdir + regex の grep / view(行範囲 + 行番号 + context lines)
  - cache GC (--keep N --dry-run) と meta JSON I/O
- src/cli.rs / src/app/runner.rs に Reference サブコマンドと dispatcher 配線
- src/tooling/{tool_catalog,local_tools}.rs に reference_* 6 ツール登録と
  local_tools.maybe_execute_local_tool からの委譲
- src/core/managed_binaries.rs に cache_root()(UNITY_CLI_CACHE_ROOT 対応)
- 新 skill unity-csharp-reference(Skill Contract v1 準拠、symlink 含む)
- 既存 skill (navigate / edit / scene-inspect) の siblings に追加(R12/R22 解消)
- docs/tools.md / README.ja.md / ATTRIBUTION.md(Unity Companion License)

検証ゲート:
- cargo fmt --check / cargo clippy --all-targets -- -D warnings: clean
- cargo test --bin unity-cli: 278 passed
- unity-cli skills lint --severity error: 15 skills / 0 violations

Refs #185

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: fd49a3f5-0909-4758-b7c7-ed92ba896961

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.

akiojin and others added 4 commits May 11, 2026 21:38
- src/app/runner.rs: collapsible_match (string/boolean/number arms) を
  match guard 形式に書き換え、`-D warnings` での fail を解消
- src/reference/cache.rs: gc の sort_by を sort_by_key(|e| Reverse(e.1)) に
  置き換え、clippy 1.95 の unnecessary_sort_by を解消
- src/reference/{mod,fetcher}.rs: dispatcher / resolve_version /
  resolve_version_and_branch / dir_size / now_unix_seconds_string /
  github_token / ensure_git_available / run_clone license guard /
  execute_grep|view|search|fetch|status の 17 件の追加 test を加え、
  reference module の line coverage を 90% threshold まで引き上げ

ローカル検証:
- cargo fmt -- --check: clean
- cargo clippy --all-targets -- -D warnings: clean (local clippy 0.1.94)
- cargo test --bin unity-cli: 295 passed / 0 failed (+17 from previous run)

Refs #185

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Phase 1 PR の Rust Coverage >= 90% gate を満たすため、テストのみで
全体 line coverage を 89.15% から 90.44% まで引き上げる。

- src/reference/version.rs: detect の error path (ファイル不在、行不在、空値)、
  parse_editor_version、resolve_branch、minor_version_key の error inputs に
  対する unit test を追加
- src/reference/cache.rs: list_versions の root 不在 / file 混在、gc(keep=0)、
  read_meta の error / invalid JSON など 6 件の境界 test を追加
- src/reference/mod.rs: execute_view/_search/_grep の required param 検証、
  invalid regex、file_glob filter、clean(dryRun=false)、fetch(force=true) の
  既存 dest 削除パスなど 8 件の dispatcher test を追加
- src/core/self_update.rs: maybe_self_update の opt-out / throttle、
  warn_cargo_conflict、touch、is_recent、last_check_path に対する 7 件の test
- src/core/managed_binaries.rs: tools_root / cache_root の env fallback と
  detect_rid の triple 検証で 3 件
- src/app/runner.rs: build_reference_call の 6 variants 全網羅で 12 件

ローカル測定:
- 327 tests pass (serial)
- cargo llvm-cov --all-targets: TOTAL line coverage 90.44%
  (前回 89.15% → +1.29%、reference 系単独は 91-99%)

Refs #185

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Pure cosmetic change: rustfmt now wraps the test module imports onto a
single line and reformats one assert_eq! into the multi-line layout
rustfmt prefers for long expression arguments. No behavior change.

Refs #185

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…ttr 位置)

- build_reference_call_grep_defaults_context_to_zero /
  build_reference_call_view_omits_optional_lines /
  build_reference_call_clean_defaults: 戻り値の tool を _tool に変更し
  rust 1.95 の unused_variables を解消
- run_with_cli_set_active_text_output_when_reachable の前に付くべき
  #[allow(clippy::await_holding_lock)] が build_reference_call_status_* に
  ずれていたのを正しい位置へ戻し、await_holding_lock を解消

Refs #185

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

akiojin commented May 11, 2026

CI gate clear: merge_state CLEAN

gwt-manage-pr fix mode で発見された 2 系統の blocker を解消し、CI を緑にしました。

修正サマリ

  • Rust Format & Lint (clippy 1.95): src/app/runner.rs 内の collapsible_match 3 件を match guard に書き換え、src/reference/cache.rssort_bysort_by_key(Reverse(_)) に書き換え。追加 test 内の未使用 tool_tool に、#[allow(clippy::await_holding_lock)] の位置を run_with_cli_set_active_text_output_when_reachable に正しく戻した。
  • Rust Coverage >= 90% (required): reference 系 / core/self_update.rs / core/managed_binaries.rs / app/runner.rs::build_reference_call に計 60+ 件のユニットテストを追加し、ローカル cargo llvm-cov --all-targets 計測で TOTAL 89.15% → 90.44%。CI でも >= 90% を pass。

現在の CI 状態

  • Rust Format & Lint: SUCCESS
  • Rust Tests / LSP Tests / LSP Performance: SUCCESS
  • Rust Coverage / LSP Coverage (>= 90%): SUCCESS
  • Skill Contract Lint / Markdown & Commitlint / CodeRabbit: SUCCESS
  • mergeable: MERGEABLE, merge_state: CLEAN

レビューおよび merge をお願いします。Phase 2 (シンボル lookup インデックス) と Phase 3 (バージョン差分 + LSP go-to-definition 実験) は別 SPEC として後続で切り出します。

@akiojin akiojin merged commit d895744 into develop May 11, 2026
9 checks passed
@akiojin akiojin deleted the work/20260511-0223 branch May 11, 2026 14:02
akiojin added a commit that referenced this pull request May 11, 2026
SPEC #187 (Phase 2 シンボル lookup インデックス) を land し、Phase 1
で得た知見を tasks/lessons.md に追記する。Phase 1 は PR #186 で
develop 統合済みのため、本 PR の diff は Phase 2 と lessons のみ。

採用方針 B (独自 ReferenceSymbolEntry) と Symbol scope (型のみ) は
gwt-discussion で確定済み。member-level lookup は Phase 2.5 以降の
拡張余地として残し、validate_kind の受理リストには method/property/
field も含めておく。

主な変更:

- src/reference/index.rs (新規): ReferenceSymbolEntry と
  ReferenceSymbolIndex 構造、build_or_update_index で size+mtime
  ベースの incremental 更新、find_symbol で kind / namespace フィルタ
- src/reference/mod.rs: maybe_execute_reference_tool に
  reference_find_symbol 分岐と execute_find_symbol を追加、
  dispatcher 経由テスト 6 件
- src/cli.rs: ReferenceCommand::FindSymbol variant
- src/app/runner.rs: build_reference_call に FindSymbol 分岐と
  2 件のテスト
- src/tooling/tool_catalog.rs: reference_find_symbol を TOOL_NAMES /
  executor / read_only / params_schema / parity count(125) に登録
- .claude-plugin/.../unity-csharp-reference/SKILL.md: Preferred Flow
  に find-symbol 例とフォールバック説明
- references/symbol-lookup-playbook.md: workflow step 1 を反映
- docs/tools.md: Reference Cache (6 -> 7 tools)
- tasks/lessons.md: SPEC #185 振り返り 3 件 (clippy 1.95 差、
  coverage 90% 維持、SPEC section markers)

ローカル検証:

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

Refs #187

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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