Skip to content

Optimize retrieval search and simplify document search home - #85

Merged
BigSimmo merged 2 commits into
mainfrom
codex/rag-retry-telemetry-main
Jun 27, 2026
Merged

Optimize retrieval search and simplify document search home#85
BigSimmo merged 2 commits into
mainfrom
codex/rag-retry-telemetry-main

Conversation

@BigSimmo

Copy link
Copy Markdown
Owner

Summary

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

Copilot AI review requested due to automatic review settings June 27, 2026 14:52
@BigSimmo
BigSimmo merged commit 882c956 into main Jun 27, 2026
2 of 4 checks passed

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 improves retrieval performance and reliability by increasing pgvector HNSW recall, reducing redundant/serial Supabase RPC work during RAG retrieval, and tightening worker/agent completion semantics, while also simplifying the Documents search “home” UI state.

Changes:

  • Raise pgvector hnsw.ef_search for key retrieval RPCs to better match their deeper candidate fetch limits.
  • Speed up retrieval by memoizing per-request memory-card fetches and running independent embedding-based RPCs concurrently.
  • Harden indexing completion by introducing strict enrichment completion via RPC and simplifying the document search empty state UI.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
worker/main.ts Adds strict enrichment completion RPC flow; reuses image bytes for caption+upload; refactors metadata/agent repair flags.
tests/worker-visual-capture.test.ts Updates worker source expectations for new strict completion and agent-repair logic.
tests/ui-smoke.spec.ts Adjusts UI smoke assertions for simplified Documents empty state and tag/button expectations.
tests/indexing-v3-agent.test.ts Adds coverage asserting the agent recognizes local-worker visual units as satisfying artifact capture.
tests/document-index-units.test.ts Expects generated_by: "local-worker" in visual index unit metadata.
supabase/schema.sql Records per-function hnsw.ef_search settings for retrieval functions.
supabase/migrations/20260627000000_retrieval_hnsw_ef_search.sql Introduces migration to set hnsw.ef_search = 100 on retrieval functions.
supabase/functions/indexing-v3-agent/index.ts Broadens “visual unit generated” detection to include local-worker / visual-intelligence metadata.
src/lib/rag.ts Adds per-request memory-card memoization and parallelizes independent embedding-based retrieval RPCs.
src/lib/document-index-units.ts Marks visual units as generated_by: "local-worker".
src/components/ClinicalDashboard.tsx Removes unused document-search callbacks from DocumentSearchResultsPanel usage.
src/components/clinical-dashboard/document-search-results.tsx Simplifies the document search home/empty state UI and removes suggested-search wiring.

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

Comment thread worker/main.ts
Comment on lines 690 to 699
const preparedImages = await Promise.all(
extracted.images.map(async (image) => {
const bytes = await readFile(image.path);
const imageHash = hashBytes(bytes);
return {
// B4: retain the buffer so caption (base64) and storage upload reuse this single
// read instead of re-reading the same file from disk two more times per image.
bytes,
imageHash,
bytesLength: bytes.length,
Copilot AI review requested due to automatic review settings June 27, 2026 14:56

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

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Comment thread src/lib/rag.ts
Comment on lines +2490 to +2492
const effectiveMatchCount = Math.max(args.matchCount, 48);
const cacheKey = `${args.query}\0${args.queryEmbedding?.length ? "vec" : "text"}\0${effectiveMatchCount}`;
let cardsPromise = args.cardCache?.get(cacheKey);
Comment thread worker/main.ts
enrichment_error: strictCompletionMessage,
indexing_v3_agent_status: "pending",
indexing_v3_agent_last_error: strictCompletionMessage,
indexing_v3_agent_repair_reason: "strict_completion_gate_blocked",
Comment thread worker/main.ts
Comment on lines 690 to +697
const preparedImages = await Promise.all(
extracted.images.map(async (image) => {
const bytes = await readFile(image.path);
const imageHash = hashBytes(bytes);
return {
// B4: retain the buffer so caption (base64) and storage upload reuse this single
// read instead of re-reading the same file from disk two more times per image.
bytes,

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c202e26eb8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


it("documents that local worker visual units satisfy visual artifact capture", () => {
const edgeSource = String(
await import("node:fs/promises").then((fs) =>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Mark the visual-agent test callback async

When this spec is loaded, the new callback is a normal function but contains await import(...), which is invalid syntax inside a non-async function. Any vitest/npm run test run that parses this file will abort before executing tests; make the callback async or use a synchronous file read.

Useful? React with 👍 / 👎.

Comment thread worker/main.ts
...(agentRepairRequired
? {
indexing_v3_agent_status: "pending",
indexing_v3_agent_last_error: enrichmentErrorMessage ?? optionalRepairMessage,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid reporting deferred enrichment as an optional-write failure

With the default WORKER_INLINE_ENRICHMENT=false, agentRepairRequired is true because enrichment is deferred, but enrichmentErrorMessage is null and this line writes the optional-write failure message into indexing_v3_agent_last_error. That makes normal successful core indexing look like optional artifacts failed until the agent claims it; set a deferred-enrichment message/null unless optionalRepairRequired is actually true.

Useful? React with 👍 / 👎.

Comment thread worker/main.ts
return {
// B4: retain the buffer so caption (base64) and storage upload reuse this single
// read instead of re-reading the same file from disk two more times per image.
bytes,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Don't retain every extracted image buffer for the whole job

For image-heavy PDFs/DOCX and default worker concurrency, this new bytes field keeps all extracted image buffers alive after the initial hashing pass, including images later skipped by cheap filters or caption budgets. That can push ingestion workers into high heap/RSS or OOM on large source documents; keep only hash/size/perceptual metadata here and reread or lazily cache bytes for the image currently being captioned/uploaded.

Useful? React with 👍 / 👎.

@BigSimmo
BigSimmo deleted the codex/rag-retry-telemetry-main branch June 28, 2026 02:38
cursor Bot pushed a commit that referenced this pull request Jul 28, 2026
Resolve outstanding-issues ledger conflict by keeping main's archive
claim on #84 and capturing the upload-limit sync recommendation as #85.

Co-authored-by: BigSimmo <BigSimmo@users.noreply.github.com>
BigSimmo pushed a commit that referenced this pull request Jul 30, 2026
Fifth consecutive genuine conflict on docs/outstanding-issues.md,
confirmed with git merge-tree before acting.

Main's d8b034c (PR #1441) deleted three rows from the Open items table
and archived none of them: #85, which that PR genuinely resolved by
adding check-upload-limit-parity.mjs; #119, a duplicate of #122; and
#105, which is open and unresolved — its LoadingPanel half is still
unverified. This resolution mirrors main on #85 and #119, which were
dispositionable, and keeps #105, which was not.

#105 survived only because this resolution was done row by row and then
diffed with column padding normalised away. Taking either side wholesale
would have lost it silently, which is what AGENTS.md forbids for this
file and what appears to have happened upstream.

Filed as #148: check:outstanding-issues validates duplicate ids, ids in
both tables, stale markers and malformed rows, but never compares
against the previous revision, so a row that simply disappears is
invisible to it in verify:cheap and static-pr alike. The fix is a
deletion check against the merge base, not a union merge driver — #133
removed that driver deliberately and reinstating it would bring back the
duplicate-row damage it was removed to stop.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01361jh3eYVjJCzXWjAhdZiF
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