Repository navigation
Description
Problem
The embedding benchmark ranks every query against every chunk in pure Python. It calls cosine_similarity once for each pair and holds its own recall, MRR and nDCG code (src/devops_cli/ai/benchmark/embedding_runner.py). The task file of #481 records the old ranking: 57 seconds for 100 queries over the 7,699 chunks of src, against 0.7 seconds with a numpy matrix product and a metrics library, with the same scores.
Evidence
- A person-run over the repository root (17,623 chunks, 100 samples) took 27 and 29 minutes for the two models on 2026-10-22. The reports (
items/s15.6 and 12.3) show that the embedding of the corpus took about 19 and 24 minutes. The old ranking took about 2 minutes of each run. So the change saves about two minutes of 27, not most of the time. - A finished and reviewed build exists as a patch:
.data/agent/scratch/481a-numpy-ranking.patch(one commit, 962 targeted tests pass, nine mutants killed). It addsretrieval_metrics.pywith a numpy matrix product andpytrec_eval, and deletescompute_ndcg_at_k, the inline loop and one C901 marker. - The library
pytrec-eval-terrieris maintained, MIT licensed and has no advisory. It has Python 3.14 wheels for x86_64, macOS and Windows. The images build forlinux/amd64only (perf(ci): the release builds the service image for linux/amd64 only, with no QEMU #1956), so the missing aarch64 wheel does not matter. Thelinux/arm64build was not run (no container engine was available), and only the release job builds that platform. A failure there would show only when a release publishes. - The embedding time dominates the run. Batching the corpus embedding is the larger saving.
Remedy
The change touches the release image build. Build the linux/amd64 service image with the change in the pull request's Service Image check, which builds it on every pull request.
Constraint (from the rejected plan #5140, 2026-11-12): type pytrec_eval with a stub typings/pytrec_eval.pyi, as typings/jq.pyi does. Add no [[tool.mypy.overrides]] entry, and raise no suppression ceiling. Declare numpy and pytrec-eval-terrier in pyproject.toml and uv.lock (requirement "Dependencies declared").
Alternatives check of 2026-11-01: not-applicable.
Tool: none found.
Proposed design
Problem statement
The embedding benchmark ranks every query against every chunk in a pure Python loop. It calls cosine_similarity once for each pair and holds its own recall, MRR and nDCG code in src/devops_cli/ai/benchmark/embedding_runner.py. The change replaces the loop with one numpy matrix product and computes the metrics with pytrec-eval-terrier. It removes compute_ndcg_at_k and the C901 marker, declares numpy and pytrec-eval-terrier, and types pytrec_eval with a stub under typings so no mypy override or higher suppression ceiling is needed.
Acceptance criteria
Requirement: Numpy ranking
The benchmark SHALL rank each query against the corpus with one numpy matrix product over row-normalized vectors and SHALL NOT call cosine_similarity for each pair.
Scenario: Ranking matches pairwise cosine
- WHEN The ranking helper scores a fixed 2-query by 4-chunk fixture whose vectors are not unit length.
- THEN Each score equals cosine_similarity for that query and chunk within 1e-9, and the ranking path calls numpy.matmul or numpy.dot once and cosine_similarity zero times.
Requirement: Metrics from pytrec_eval
The benchmark SHALL compute recall_at_1, recall_at_3, recall_at_5, mrr and ndcg_at_5 with pytrec_eval.
Scenario: Metrics match the pure-Python reference
- WHEN The new retrieval metrics module scores a fixed qrels and run whose known target ranks are 1, 3 and 6.
- THEN The module returns the same five metric values as the current pure-Python implementation on that fixture.
Requirement: Remove compute_ndcg_at_k
The change SHALL remove compute_ndcg_at_k from src/devops_cli/ai/benchmark/embedding_runner.py and SHALL update or remove every test that imports it.
Scenario: No remaining reference
- WHEN The test searches the source and test trees for compute_ndcg_at_k.
- THEN rg compute_ndcg_at_k src tests returns no match, and the benchmark test suite passes.
Requirement: Remove C901 marker
The change SHALL remove the # noqa: C901 marker from evaluate_model_on_server and the function SHALL pass the Ruff C901 check without it.
Scenario: Ruff C901 passes
- WHEN ruff check --select C901 src/devops_cli/ai/benchmark/embedding_runner.py runs.
- THEN The command exits 0 and the file contains no # noqa: C901 marker.
Requirement: Dependencies declared
The change SHALL declare numpy and pytrec-eval-terrier as direct dependencies in pyproject.toml and SHALL resolve both in uv.lock.
Scenario: Lockfile holds the direct dependencies
- WHEN pyproject.toml and uv.lock are read after uv lock.
- THEN pyproject.toml lists numpy and pytrec-eval-terrier in [project].dependencies, and uv.lock holds a resolved version and a linux/amd64 wheel for each.
Requirement: Type stub for pytrec_eval
The change SHALL add typings/pytrec_eval.pyi for the pytrec_eval names that the benchmark uses, SHALL add no [[tool.mypy.overrides]] entry for pytrec_eval, and SHALL NOT raise the suppression ceiling.
Scenario: Mypy passes with the stub and no override
- WHEN mypy runs on src/devops_cli/ai/benchmark/retrieval_metrics.py with the project configuration, and tests/invariants/test_architectural_invariants.py::test_suppressions_stay_under_their_ceilings runs.
- THEN Both commands exit 0, pyproject.toml holds no pytrec_eval override, and the suppression ceiling stays at 1.
Requirement: Service Image check
The Service Image check SHALL build the linux/amd64 image and its smoke test SHALL import numpy and pytrec_eval.
Scenario: Service image smoke test imports the libraries
- WHEN The Service Image check builds and smoke-tests devops-cli-service:local.
- THEN The check passes and the smoke test's python -c probe imports numpy and pytrec_eval without error.
Existing Tool Considered
The alternatives check returned not-applicable and named no tool, so it compared nothing. The change uses pytrec-eval-terrier, imported as pytrec_eval, for recall, MRR and nDCG. It uses numpy for the matrix product. pytrec-eval-terrier lacks an aarch64 wheel, but every image build here targets linux/amd64, including the release and Service Image jobs. No code is built in place of either library.
Key questions
- Does the missing aarch64 wheel for pytrec-eval-terrier block the pull request or a release?: No. Every image build in this repository targets linux/amd64: the release service, runner and MCP-server builds set platforms: linux/amd64, the DevContainer build passes --platform linux/amd64, and the pull request Service Image job builds on the amd64 runner. No workflow builds an arm64 image, so the wheel gap does not block the change. (best practice) [sources: .github/workflows/release.yml:412, .github/workflows/release.yml:542, .github/workflows/release.yml:666, .github/actions/devcontainer-image/action.yml:85, .github/workflows/ci.yml:497]
- Chosen: Proceed with the linux/amd64 Service Image check. The pull request Service Image job builds the same linux/amd64 image that the release publishes, so it catches a missing or broken wheel before merge.
- Rejected: Block until an arm64 image build is verified. No workflow builds any image for linux/arm64; release.yml sets linux/amd64 for the service, runner and MCP-server images.
- Which library computes recall, MRR and nDCG?: pytrec-eval-terrier, imported as pytrec_eval, computes recall, MRR and nDCG. The issue records that it is maintained, MIT licensed, has no advisory, and publishes Python 3.14 wheels for x86_64, macOS and Windows. It lacks an aarch64 wheel, which no build here needs. numpy supplies the matrix product. (best practice) [sources: src/devops_cli/ai/benchmark/embedding_runner.py:428, src/devops_cli/ai/benchmark/embedding_runner.py:443, AGENTS.md:11]
- Chosen: Use pytrec-eval-terrier with numpy. The library covers recall, MRR and nDCG, and numpy covers the matrix product. AGENTS.md:11 requires an open tool before project code.
- Rejected: Keep the inline Python metrics. The inline code is the 57-second ranking path and keeps the C901 marker. The feat(ai): embeddings through the llm gateway, with embedding model evaluation #481 fixture takes 0.7 seconds with the libraries.
- Rejected: Build project code for the metrics. It duplicates a maintained library and adds code that the project must keep and fix.
- What must the numpy matrix product do to keep the current scores?: Normalize each query and corpus vector to unit length before the product, and keep the zero-norm row at zero. cosine_similarity divides the dot product by both norms, so a raw dot product would change the scores. (best practice) [sources: src/devops_cli/ai/vector_similarity.py:15, src/devops_cli/ai/vector_similarity.py:18, src/devops_cli/ai/vector_similarity.py:20, src/devops_cli/ai/benchmark/embedding_runner.py:449]
- Chosen: Row-normalize both matrices, then multiply. It reproduces cosine_similarity on every pair in one matrix product, including the zero-vector result of 0.
- Rejected: Use a raw numpy dot product. The embeddings are not guaranteed unit length, and the current function divides by both norms, so raw dot products change the ranking.
- Rejected: Keep the pairwise cosine loop. The pairwise loop is the slow path that the change replaces.
- Should the new metrics live in a separate module?: Yes. Add src/devops_cli/ai/benchmark/retrieval_metrics.py for the normalized matrix product and the pytrec_eval calls. embedding_runner.py keeps the orchestration, and the C901 marker on evaluate_model_on_server can go. (best practice) [sources: src/devops_cli/ai/benchmark/embedding_runner.py:350, src/devops_cli/ai/benchmark/embedding_runner.py:443]
- Chosen: Add retrieval_metrics.py. It isolates the numpy and pytrec_eval code, makes the ranking testable on a fixed fixture, and lets the C901 marker go.
- Rejected: Keep the metrics inline in embedding_runner.py. The function already carries a C901 suppression, and the inline loop is what the change replaces.
- How should pytrec_eval be typed so the change does not raise the mypy suppression ceiling?: Add typings/pytrec_eval.pyi for the pytrec_eval names that the benchmark uses. Add no [[tool.mypy.overrides]] entry and do not raise the suppression ceiling. pyproject.toml sets mypy_path to typings, and typings/jq.pyi is the existing example of a stub for a library with no type information. The ceiling test at tests/invariants/test_architectural_invariants.py:143 fails a second override with the message 2 > ceiling 1. (best practice) [sources: pyproject.toml, typings/jq.pyi, tests/invariants/test_architectural_invariants.py:143]
- Chosen: Add typings/pytrec_eval.pyi stub. pyproject.toml sets mypy_path to typings, and typings/jq.pyi shows the pattern. A stub adds no override and keeps the suppression ceiling at 1.
- Rejected: Add a [[tool.mypy.overrides]] entry for pytrec_eval.*. The ceiling test at tests/invariants/test_architectural_invariants.py:143 fails a second override with 2 > ceiling 1.
- Rejected: Use an inline type ignore at each pytrec_eval call. It leaves the library untyped, and the stage return asks for a stub like typings/jq.pyi.
- Where does the pure-Python ranking loop live?: It lives in evaluate_model_on_server in src/devops_cli/ai/benchmark/embedding_runner.py. It loops over the corpus at line 443 and calls cosine_similarity(q_vec, c_vec) at line 449. [sources: src/devops_cli/ai/benchmark/embedding_runner.py:443, src/devops_cli/ai/benchmark/embedding_runner.py:449]
- Where is compute_ndcg_at_k defined and used?: It is defined at src/devops_cli/ai/benchmark/embedding_runner.py:69 and called at line 466. Three test files import it: tests/ai/benchmark/test_ai_benchmark.py:589, tests/ai/benchmark/test_embedding_benchmark.py:323, and tests/ai/client/test_ai_client_providers_and_embeddings.py:19. [sources: src/devops_cli/ai/benchmark/embedding_runner.py:69, src/devops_cli/ai/benchmark/embedding_runner.py:466, tests/ai/benchmark/test_ai_benchmark.py:589, tests/ai/benchmark/test_embedding_benchmark.py:323, tests/ai/client/test_ai_client_providers_and_embeddings.py:19]
- Can the patch at .data/agent/scratch/481a-numpy-ranking.patch be verified from the repository?: No. AGENTS.md:20 keeps agent files under .data/agent/ out of git, and the checked commit holds no .data/agent/scratch directory. The per-pair loop is still at embedding_runner.py:443-450. The criteria therefore rest on tests in the pull request, not on the patch or its 962-test count. [sources: AGENTS.md:20, src/devops_cli/ai/benchmark/embedding_runner.py:443]
- How much time does the change actually save?: About 2 minutes of a 27 to 29 minute run. The corpus embedding takes 19 to 24 minutes and dominates. The ranking is 57 seconds on the 7,699-chunk feat(ai): embeddings through the llm gateway, with embedding model evaluation #481 fixture and about 2 minutes on the 17,623-chunk run. Batching the corpus embedding is the larger saving and is outside this change. [sources: src/devops_cli/ai/benchmark/embedding_runner.py:409, src/devops_cli/ai/benchmark/embedding_runner.py:443]
- Does the pull request Service Image check build the image when the dependencies change?: Yes. pyproject.toml and uv.lock are in the job's IMAGE_CONTENT_PATHS, so a change to either rebuilds devops-cli-service:local. The Dockerfile installs the runtime dependencies with uv sync --locked --no-dev --extra review. [sources: .github/workflows/ci.yml:471, .github/workflows/ci.yml:472, .github/workflows/ci.yml:497]
- Where are the dependencies declared, and is numpy already a direct dependency?: pyproject.toml:11-85 holds the direct runtime dependencies. numpy is not there; it reaches uv.lock only through formulaic at uv.lock:1265. The change declares numpy and pytrec-eval-terrier in pyproject.toml and locks both. [sources: pyproject.toml:11, pyproject.toml:85, uv.lock:1265]
- How many [[tool.mypy.overrides]] entries does pyproject.toml hold, and what ceiling applies?: One entry exists, for statsmodels.*. The ceiling is 1 at tests/invariants/test_architectural_invariants.py:143. A second override fails test_suppressions_stay_under_their_ceilings with 2 > ceiling 1. [sources: pyproject.toml, tests/invariants/test_architectural_invariants.py:143]
Fit
Fits in one pull request.
Suspected block
None.
Dependencies
None.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNew
- StatusShow more project fieldsNew
Activity
dan-petty commentedon Oct 22, 2026
Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.
The candidate proposes to refactor the embedding benchmark code.The change improves performance but is not urgent or critical.The change saves time for users running benchmarks, which is valuable but not essential.The change requires modifying code and testing on different platforms including arm64.feat(k8s): serve qwen3-embedding:0.6b as the gateway embedding model (#…
feat(release): v0.2.39 (#1947)
dan-petty commentedon Oct 28, 2026
Refine cannot set this item to Ready at bb46660 until a person answers the questions below. Reply in a comment or edit the body, and the next refine round looks at the item again.
56 remaining items
dan-petty commentedon Nov 18, 2026
implement: retry
devops-cli-service commentedon Nov 18, 2026
Cleared the stop at push by the rule
retry: a person commentedimplement: retry. The item is Planned again, and the Service takes it in the next round.refactor(ai): rank the benchmark with numpy and pytrec-eval
devops-cli-service commentedon Nov 19, 2026
Released in v0.179.5: https://github.com/dan-petty/devops-cli/releases/tag/v0.179.5
devops-cli-service commentedon Nov 19, 2026
Live since 2026-11-19 11:42 UTC