Skip to content

feat(context): rerank candidates with Jev (TypeSafe SystemOne) - #59

Merged
hungpham10 merged 4 commits into
mainfrom
feat/jev-context-rerank
Oct 28, 2026
Merged

hungpham10 merged 4 commits into
mainfrom
feat/jev-context-rerank

Conversation

@hungpham10

@hungpham10 hungpham10 commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

Vấn đề

codegraph_context chỉ tìm symbol bằng Contains trên tên. Query tự nhiên
("where do we validate that config.toml parses") trả _No matches._, còn query
một từ thì trả symbol đầu tiên khớp chuỗi, không liên quan ngữ nghĩa — trong khi
callers/callees chỉ có giá trị khi symbol gốc đúng.

Cách giải

Thêm rerank = "jev" (MCP arg, GraphQL field; mặc định none). Khi bật:

  1. Pool: query gốc + từng token (tách camelCase/snake_case, bỏ stopword) qua
    Contains, lọc kind code (bỏ variable/parameter/field/constant) và loại
    test/bench
    , cap [jev] max_candidates (mặc định 40).
  2. Một request Jev (TypeSafe SystemOne — decision model, không phải chat):
    • best_symbol (Choice): mỗi candidate là một option, cộng
      none_of_the_above.
    • any_fits (Noul): 0–1, chỉ để chẩn đoán.
  3. Quyết định: sort theo probabilities (giữ đủ thứ tự, ngưỡng chỉ áp cho
    hạng 1); no_match khi none thắng/hoà xác suất hạng 1, hoặc hạng 1 dưới
    min_probability. Pool < 3 → no_match luôn, không gọi API.
  4. Fail-open toàn bộ: thiếu token, timeout, 4xx/403, JSON lỗi → giữ thứ tự
    pool, kèm rerank.used = false + reason. Không bao giờ làm hỏng context.
  5. Output: ContextHit.match_score { probability, confidence },
    ContextResponse.no_match / rerank, markdown in (p=0.62) và
    _No symbol matches "<query>"._ khi không có gì đủ liên quan.

Config opt-in (dữ liệu rời máy), token chỉ đọc từ env:

[jev]
enabled = true
model = "jev-1.13-free"
api_key_env = "OPENCODE_API_KEY"
min_probability = 0.25

Đo thực tế (jev-1.13-free)

Kiểu hỏi Kết quả Latency Input token
choice + none + noul (4 candidate) chọn đúng parse_config, p=0.62 / none 0.38 1.0 s 527
choice không có none ép chọn, confidence = 1.0 kể cả pool rác 1.0 s 672
1 noul / candidate tách tốt hơn nhưng chậm gấp 3 3.2 s 658

→ Chọn choice + none; không dùng Noul làm cổng: Jev trả noul = 0.33
ngay cả khi đã chọn đúng p=0.75. Jev cũng từng chọn một #[test] fn trong file
đúng → pool loại test/bench mặc định.

Test

  • 18 unit test mới (crates/codegraph-context): tokenize, pool, lọc test/kind,
    ngưỡng no_match, sort + cắt limit, markdown có p, fail-open, pool nhỏ không
    gọi API, parse [jev].
  • 1 live test #[ignore] gọi API thật:
    cargo test -p codegraph-context --test jev_live -- --ignored --nocapture.

Verify

  • cargo fmt --all ✅
  • cargo clippy --workspace --all-targets -- -D warnings ✅
  • cargo test --workspace ✅ (4 test storage::redis fail do không có
    redis-server — pre-existing, đã xác nhận chạy standalone)
  • cargo test -p codegraph-context --test jev_live -- --ignored ✅

Ghi chú

  • Chưa thêm bench hit@5 (cần bộ query có nhãn); min_probability chỉnh được
    trong config nên không phải build lại.
  • codegraph_search_symbol chưa rerank — đợt sau nếu context chứng minh hiệu quả.

`codegraph_context` chỉ biết `Contains` trên tên symbol, nên query tự nhiên
("where do we validate config.toml") trả rỗng hoặc trả symbol không liên quan.

Thêm `rerank = "jev"` (MCP arg + GraphQL field, mặc định off):

- Pool: query gốc + từng token (camelCase/snake_case, bỏ stopword) qua
  `Contains`, lọc kind code và loại test/bench, cap `max_candidates`.
- Một request Jev: Choice (mỗi candidate một option + `none_of_the_above`)
  cộng Noul để chẩn đoán. Trả `probabilities` → xếp hạng, `no_match` khi
  `none` thắng xác suất hạng 1 hoặc hạng 1 dưới `min_probability`.
- Pool < 3 candidate → `no_match` luôn, không gọi API.
- Fail-open: thiếu token, timeout, 4xx, JSON lỗi → giữ thứ tự pool cũ kèm
  `rerank.used = false` + `reason`; không bao giờ làm hỏng context.
- Response: `ContextHit.match_score { probability, confidence }`,
  `ContextResponse.no_match`/`rerank`, markdown in thêm `(p=0.62)`.
- Config `[jev]` opt-in (dữ liệu rời máy): endpoint/model/api_key_env/
  timeout_ms/max_candidates/min_probability/repo. Token chỉ đọc từ env.

Đo với jev-1.13-free: 4 candidate → 1.0s, ~530 input token, cost 0; chọn
đúng `parse_config` (p=0.62, none 0.38). Choice một mình luôn `confidence`
cao kể cả với pool rác (đo được 1.0 với 8 candidate sai), nên `none` +
`min_probability` mới là tín hiệu "không khớp" — không dùng Noul làm cổng
vì Jev trả noul thấp (0.33) ngay cả khi đã chọn đúng.

Test: 18 unit test (pool/tokenize/ngưỡng/fail-open/markdown) + 1 live
`#[ignore]` gọi API thật.
@coderabbitai

coderabbitai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8e4d3564-02d2-4042-aa4d-fa33b985c280
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@codecov-commenter

codecov-commenter commented Oct 28, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.12839% with 76 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.29%. Comparing base (055e4b7) to head (37ed9d7).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/codegraph-mcp/src/tools.rs 82.85% 30 Missing ⚠️
crates/codegraph-context/src/jev.rs 95.12% 14 Missing ⚠️
crates/codegraph-graphql/src/types.rs 0.00% 9 Missing ⚠️
crates/codegraph-api/src/lib.rs 89.61% 8 Missing ⚠️
crates/codegraph-context/src/lib.rs 98.22% 7 Missing ⚠️
crates/codegraph-core/src/jev.rs 96.52% 4 Missing ⚠️
crates/codegraph-mcp/src/lib.rs 33.33% 4 Missing ⚠️

❌ Your patch status has failed because the patch coverage (93.12%) is below the target coverage (100.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #59      +/-   ##
==========================================
+ Coverage   80.46%   81.29%   +0.83%     
==========================================
  Files         103      105       +2     
  Lines       29526    30561    +1035     
==========================================
+ Hits        23758    24846    +1088     
+ Misses       5768     5715      -53     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread crates/codegraph-context/tests/jev_live.rs Outdated
@codspeed

codspeed Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 23 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 38 untouched benchmarks


Comparing feat/jev-context-rerank (8694fe0) with main (055e4b7)

Open in CodSpeed

hungpham10 and others added 3 commits October 29, 2026 06:34
* feat(search): rerank symbol and document search with Jev

Mở rộng rerank Jev (TypeSafe SystemOne) từ `codegraph_context` sang
`codegraph_search_symbol` và 3 tool document search
(`graphdoc_search`, `graphdoc_search_value`, `graphdoc_search_struct`).

- `codegraph_search_symbol`: arg `rerank = "jev"`, pool = `limit × 4`
  (cap `[jev] max_candidates`), gắn `match_score.probability`, trả
  `no_match`/`rerank`. Chỉ trang đầu — `offset > 0` hoặc `resume` bị từ
  chối vì rerank phá tính ổn định phân trang.
- Doc search: cùng arg `rerank`; response đổi thành object
  `{results, no_match, rerank}` khi bật (mặc định vẫn là array).
  Candidate = `kind \`key\` in \`doc\`` + value tuỳ chọn.
- Config `[jev] doc_include_values` (default false) và `doc_value_chars`
  (default 120): value giúp phân biệt node trùng key ở nhiều file nhưng
  tốn token nên là opt-in.
- Fail-open toàn bộ: Jev lỗi/tắt/timeout → giữ thứ tự cũ + `rerank.used=false`.
- Gộp phần chọn candidate + fail-open vào `codegraph_context::rank()`
  dùng chung cho cả 3 nguồn.

Test: unit cho candidate/apply outcome (MCP), `search_symbol_reranked`
với judge giả (API), live test `#[ignore]` thêm case doc-node-nhờ-value
(đã chạy thật: chọn đúng id 2, p=0.81).

* Bump version to v2.2.13
… sensitive information'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Theo quy ước: thêm config key phải cập nhật DEFAULT_CONFIG_TOML để
 sinh file mẫu có đủ key (kèm ghi chú opt-in + token chỉ
đọc từ env).
@hungpham10
hungpham10 merged commit 5a7addc into main Oct 28, 2026
11 checks passed
@hungpham10
hungpham10 deleted the feat/jev-context-rerank branch October 28, 2026 23:54
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.

3 participants