Skip to content

perf: hoist uniqueQueryTokens out of per-result loop in rankAnswerEvidence#77

Merged
BigSimmo merged 1 commit into
mainfrom
copilot/task-87357024-1243497866-251362f0-d540-4973-9b2d-4ef07954a0d6
Jun 27, 2026
Merged

perf: hoist uniqueQueryTokens out of per-result loop in rankAnswerEvidence#77
BigSimmo merged 1 commit into
mainfrom
copilot/task-87357024-1243497866-251362f0-d540-4973-9b2d-4ef07954a0d6

Conversation

Copilot AI commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Summary

  • uniqueQueryTokens(query) was called 2× per result in rankAnswerEvidence — once in answerEvidenceScore and again inside phraseScore — producing N×2 identical tokenizations for a constant query. For a typical 15-result ranking pass this meant ~30 redundant calls (each involving normalizedClinicalSearchTokens + regex + Set dedup).

  • Tokens are now computed once in rankAnswerEvidence and threaded into answerEvidenceScore and phraseScore as a parameter:

// before: answerEvidenceScore called uniqueQueryTokens, then phraseScore called it again
score: answerEvidenceScore(query, result, queryClass)

// after: tokens computed once, passed through
const tokens = uniqueQueryTokens(query);
score: answerEvidenceScore(tokens, result, queryClass)

Verification

  • npm run verify:cheap
  • npm run verify:ui when UI, routing, styling, browser behavior, reduced-motion, or forced-colors behavior changed
  • npm run verify:release before release or handoff confidence claims
  • npm run format:check
  • npm run check:production-readiness when clinical workflow, privacy, environment, Supabase, source governance, or deployment behavior changed

Clinical Governance Preflight

Complete this section when the change touches ingestion, answer generation, search/ranking, source rendering, document access, privacy, production env, or clinical output.

  • Source-backed claims still require linked source verification before clinical use
  • No patient-identifiable document workflow was introduced or expanded without explicit governance approval
  • Supabase target remains Clinical KB Database (sjrfecxgysukkwxsowpy)
  • Service-role keys and private document access remain server-only
  • Demo/synthetic content remains clearly separated from real clinical sources
  • Source metadata, review status, and outdated/unknown-source behavior remain conservative
  • Deployment classification/TGA SaMD impact was checked when clinical decision-support behavior changed

Notes

  • Pure refactor — no behaviour change. Ranking scores are identical; all 7 answer-ranking tests and 14 related tests pass unchanged.
  • phraseScore signature changed from (query: string, haystack: string) to (tokens: string[], haystack: string) — private function, no external callers.

Copilot AI requested a review from BigSimmo June 27, 2026 06:42
@BigSimmo
BigSimmo marked this pull request as ready for review June 27, 2026 07:15
Copilot AI review requested due to automatic review settings June 27, 2026 07:15
@BigSimmo
BigSimmo merged commit 9356235 into main Jun 27, 2026
@BigSimmo
BigSimmo deleted the copilot/task-87357024-1243497866-251362f0-d540-4973-9b2d-4ef07954a0d6 branch June 27, 2026 07:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes answer-evidence ranking by avoiding repeated tokenization of a constant query during scoring, reducing redundant work per result while preserving scoring behavior.

Changes:

  • Hoists uniqueQueryTokens(query) to rankAnswerEvidence and reuses the computed tokens for all results.
  • Refactors answerEvidenceScore and phraseScore to accept precomputed tokens instead of the raw query string.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/lib/answer-ranking.ts
results: SearchResult[],
queryClass: RagQueryClass = classifyRagQuery(query).queryClass,
) {
const tokens = uniqueQueryTokens(query);
cursor Bot pushed a commit that referenced this pull request Jul 25, 2026
Keep main's ledger encoding and #54/#7 queue updates, and retain this
branch's closures for #67/#77/#78 in the resolved archive.

Co-authored-by: BigSimmo <BigSimmo@users.noreply.github.com>
cursor Bot pushed a commit that referenced this pull request Jul 25, 2026
Resolve outstanding-issues against main's #66/#67 closures, keep #77/#78
resolved here, and format the files that failed Static PR format:check.

Co-authored-by: BigSimmo <BigSimmo@users.noreply.github.com>
BigSimmo added a commit that referenced this pull request Jul 25, 2026
)

* fix: make reconciliation tooling deterministic and multi-task safe

Inject a fixture repositoryRoot into preflight collection so the contract no
longer scales with the live worktree farm (#67). Add a cooperative primary
checkout write lease (#77) and a secret-safe atomic reconciliation evidence
pack (#78), wired into lifecycle planning and the outstanding-issues ledger.

Co-authored-by: BigSimmo <BigSimmo@users.noreply.github.com>

* fix: remove accidental conflict markers from package.json

The merge commit retained both script sets but left Git conflict markers
in the published package.json; strip them so the manifest parses again.

Co-authored-by: BigSimmo <BigSimmo@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: BigSimmo <BigSimmo@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.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.

3 participants