-
Notifications
You must be signed in to change notification settings - Fork 0
Fix repository regression findings #912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,7 +67,13 @@ const searchSchema = z.object({ | |
|
|
||
| type SearchRequestBody = z.infer<typeof searchSchema>; | ||
|
|
||
| const scopedSearchInflight = new Map<string, Promise<unknown>>(); | ||
| type ScopedSearchInflight = { | ||
| promise: Promise<Record<string, unknown>>; | ||
| controller: AbortController; | ||
| waiters: number; | ||
| settled: boolean; | ||
| }; | ||
| const scopedSearchInflight = new Map<string, ScopedSearchInflight>(); | ||
|
|
||
| function isSourceLibrarySearchMode(mode: SearchRequestBody["mode"]) { | ||
| return mode === "documents" || mode === "differentials"; | ||
|
|
@@ -89,15 +95,62 @@ function scopedSearchKey(body: SearchRequestBody, ownerId?: string | null, publi | |
| }); | ||
| } | ||
|
|
||
| async function coalesceScopedSearch<T extends Record<string, unknown>>(key: string, producer: () => Promise<T>) { | ||
| const existing = scopedSearchInflight.get(key) as Promise<T> | undefined; | ||
| if (existing) return { payload: await existing, coalesced: true }; | ||
| function callerAbortReason(signal: AbortSignal): Error { | ||
| return signal.reason instanceof Error ? signal.reason : new DOMException("The operation was aborted.", "AbortError"); | ||
| } | ||
|
|
||
| const pending = producer().finally(() => { | ||
| scopedSearchInflight.delete(key); | ||
| function awaitWithCallerSignal<T>(promise: Promise<T>, signal: AbortSignal): Promise<T> { | ||
| if (signal.aborted) return Promise.reject(callerAbortReason(signal)); | ||
| return new Promise<T>((resolve, reject) => { | ||
| const onAbort = () => { | ||
| cleanup(); | ||
| reject(callerAbortReason(signal)); | ||
| }; | ||
| const cleanup = () => signal.removeEventListener("abort", onAbort); | ||
| signal.addEventListener("abort", onAbort, { once: true }); | ||
| promise.then( | ||
| (value) => { | ||
| cleanup(); | ||
| resolve(value); | ||
| }, | ||
| (error) => { | ||
| cleanup(); | ||
| reject(error); | ||
| }, | ||
| ); | ||
| }); | ||
| scopedSearchInflight.set(key, pending); | ||
| return { payload: await pending, coalesced: false }; | ||
| } | ||
|
|
||
| async function coalesceScopedSearch<T extends Record<string, unknown>>( | ||
| key: string, | ||
| producer: (signal: AbortSignal) => Promise<T>, | ||
| signal: AbortSignal, | ||
| ) { | ||
| signal.throwIfAborted(); | ||
| let entry = scopedSearchInflight.get(key); | ||
| const coalesced = Boolean(entry); | ||
| if (!entry) { | ||
| const controller = new AbortController(); | ||
| const created: ScopedSearchInflight = { | ||
| promise: Promise.resolve({}), | ||
| controller, | ||
| waiters: 0, | ||
| settled: false, | ||
| }; | ||
| created.promise = producer(controller.signal).finally(() => { | ||
| created.settled = true; | ||
| if (scopedSearchInflight.get(key) === created) scopedSearchInflight.delete(key); | ||
| }); | ||
| scopedSearchInflight.set(key, created); | ||
| entry = created; | ||
| } | ||
| entry.waiters += 1; | ||
| try { | ||
| return { payload: (await awaitWithCallerSignal(entry.promise, signal)) as T, coalesced }; | ||
| } finally { | ||
| entry.waiters -= 1; | ||
| if (entry.waiters === 0 && !entry.settled) entry.controller.abort(); | ||
| } | ||
|
Comment on lines
+124
to
+153
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift Evict the inflight entry when the last waiter aborts the shared producer (both coalescers). In both files, when
📍 Affects 2 files
🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| function buildDocumentMatchesFromResults(results: SearchResult[], limit: number) { | ||
|
|
@@ -190,7 +243,6 @@ function compactImage(image: ChunkImage) { | |
| image_type: image.image_type, | ||
| clinicalUseClass: image.clinicalUseClass, | ||
| caption: image.caption ? compactText(image.caption, 240) : "", | ||
| storage_path: image.storage_path, | ||
| searchable: image.searchable, | ||
| clinical_relevance_score: image.clinical_relevance_score, | ||
| tableLabel: image.tableLabel, | ||
|
|
@@ -686,7 +738,9 @@ async function buildScopedSearchPayload( | |
| body: SearchRequestBody, | ||
| supabase: ReturnType<typeof createAdminClient>, | ||
| ownerId?: string | null, | ||
| signal?: AbortSignal, | ||
| ) { | ||
| signal?.throwIfAborted(); | ||
| const searchFocusQuery = queryForClinicalMode(body.query, body.queryMode); | ||
| const effectiveQueryClass = | ||
| queryClassForClinicalMode(body.queryMode) ?? classifyRagQuery(searchFocusQuery).queryClass; | ||
|
|
@@ -696,7 +750,9 @@ async function buildScopedSearchPayload( | |
| accessScope, | ||
| documentIds: body.documentIds ?? (body.documentId ? [body.documentId] : undefined), | ||
| filters: body.filters, | ||
| signal, | ||
| }); | ||
| signal?.throwIfAborted(); | ||
| if (scope.documentIds?.length === 0) { | ||
| const relevance = buildEvidenceRelevance(searchFocusQuery, []); | ||
| const payload = { | ||
|
|
@@ -741,6 +797,7 @@ async function buildScopedSearchPayload( | |
| accessScope, | ||
| allowGlobalSearch: !ownerId, | ||
| queryMode: body.queryMode, | ||
| signal, | ||
| }); | ||
| const resultLimit = isSourceLibrarySearchMode(body.mode) | ||
| ? Math.max(body.topK ?? 12, Math.min(20, body.documentLimit)) | ||
|
|
@@ -760,6 +817,7 @@ async function buildScopedSearchPayload( | |
| query: searchFocusQuery, | ||
| results, | ||
| limit: isSourceLibrarySearchMode(body.mode) ? body.documentLimit : undefined, | ||
| signal, | ||
| }) | ||
| : []; | ||
| // Audit L10: compute relevance/visual evidence ONCE and share with the | ||
|
|
@@ -930,8 +988,10 @@ export async function POST(request: Request) { | |
| } | ||
|
|
||
| const key = scopedSearchKey(searchBody, ownerId, publicOnly); | ||
| const { payload, coalesced } = await coalesceScopedSearch(key, () => | ||
| buildScopedSearchPayload(searchBody, supabase!, ownerId), | ||
| const { payload, coalesced } = await coalesceScopedSearch( | ||
| key, | ||
| (signal) => buildScopedSearchPayload(searchBody, supabase!, ownerId, signal), | ||
| request.signal, | ||
| ); | ||
| return NextResponse.json({ | ||
| ...payload, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: BigSimmo/Database
Length of output: 3839
🏁 Script executed:
Repository: BigSimmo/Database
Length of output: 7978
Thread the abort signal through summary retrieval.
request.signalonly reaches structured generation here; the document/chunk reads still run after disconnect, and the unindexed fallback can still be computed. Pass the signal into those reads or bail out after each await so aborted requests stop work earlier.🤖 Prompt for AI Agents
Source: Coding guidelines