Skip to content

feat: add local cross-encoder reranker - #355

Merged
hurttlocker merged 4 commits into
mainfrom
feat/cross-encoder-reranker
Mar 23, 2026
Merged

hurttlocker merged 4 commits into
mainfrom
feat/cross-encoder-reranker

Conversation

@hurttlocker

@hurttlocker hurttlocker commented Mar 23, 2026 •

Copy link
Copy Markdown
Owner

Closes #353

What changed

  • added internal/rerank/ with:
    • generic rerank service
    • local ONNX cross-encoder scorer
    • model download/cache helpers
    • cgo / non-cgo split for optional runtime support
  • wired reranking into internal/search/search.go after hybrid / RRF fusion and before final top-k return
  • added CLI support:
    • --rerank[=auto|on|off] for search, answer, and ask
    • cortex rerank-setup to download the local model into ~/.cortex/models/rerank/
  • changed reranker input from full memory blobs to deterministic evidence windows:
    • query-aware 1-3 block span
    • lexical/entity/temporal scoring only
    • max 128 tokens
  • added unit and integration coverage for rerank ordering, no-regression off-path behavior, and evidence-window extraction
  • added benchmark note at docs/research/reranker-benchmark-2026-03-23.md

Why this change exists

Issue #353 identified local cross-encoder reranking as the highest-upside retrieval improvement from the SuperLocalMemory audit. This PR lands the infrastructure needed to test that thesis inside Cortex’s real retrieval hot path instead of a wrapper harness.

Touched surfaces

  • cmd/cortex/main.go
  • internal/search/search.go
  • internal/search/search_test.go
  • internal/rerank/
  • go.mod
  • go.sum
  • docs/research/reranker-benchmark-2026-03-23.md

Tests run

gofmt -w cmd/cortex/main.go internal/search/search.go internal/search/search_test.go internal/rerank/*.go
go test ./internal/rerank ./internal/search ./cmd/cortex
go test ./...
go vet ./...

Benchmark / eval truth

This PR changes both product behavior and benchmark interpretation.

Measured on the conv-30 81-question cortex ask path with the same populated LoCoMo DB:

  • Phase 1, full-memory rerank input:
    • ask --rerank off: 10.53% F1
    • ask --rerank on: 1.14% F1
  • Phase 2, evidence-window rerank input:
    • ask --rerank off: 12.48% F1
    • ask --rerank on: 11.93% F1

So the evidence-window fix materially recovered the regression, but the reranked path is still slightly below the same-run no-rerank baseline and significantly slower.

The benchmark note also records one more finding:

  • the stronger target model onnx-community/bge-reranker-v2-m3-ONNX:int8 looked directionally better on a spot check, but was too slow for the current process-per-query CLI architecture to benchmark fully in a reasonable time

Risk note

  • main risk: the shipped default base int8 reranker is still slightly worse than no reranker on the full slice
  • latency more than doubles on the measured ask path even after the evidence-window fix
  • the ONNX runtime dependency is optional at runtime, but still introduces cgo into the reranker implementation path
  • fresh re-import benchmarking on live main produced an empty DB in this environment, so the final benchmark used the existing populated LoCoMo DB as allowed by the issue prompt

Docs

  • added: docs/research/reranker-benchmark-2026-03-23.md

Recommendation

Keep this PR as draft until we either:

  1. move reranking into a long-lived process so stronger models are viable, or
  2. get the default local reranker to at least neutral on the same-run no-rerank baseline

@hurttlocker
hurttlocker force-pushed the feat/cross-encoder-reranker branch from 6a6926c to 710f365 Compare March 23, 2026 14:55
@hurttlocker
hurttlocker marked this pull request as ready for review March 23, 2026 14:58
@hurttlocker
hurttlocker merged commit 46ee0ca into main Mar 23, 2026
4 checks passed
@hurttlocker
hurttlocker deleted the feat/cross-encoder-reranker branch March 25, 2026 17:59
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.

feat: local cross-encoder reranker in retrieval hot path

1 participant