- Notifications
You must be signed in to change notification settings - Fork 0
Fix repository regression findings#913
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| 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) { | ||
BigSimmo marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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(); | ||
| } | ||
| } | ||
| 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, | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -15,7 +15,7 @@ import { | ||
| TriangleAlert, | ||
| Zap, | ||
| } from "lucide-react"; | ||
| import { useState, type ReactNode } from "react"; | ||
| import { useEffect, useState, type ReactNode } from "react"; | ||
| import { | ||
| categoryTheme, | ||
| @@ -27,6 +27,12 @@ import { | ||
| } from "@/components/factsheets/factsheets-data"; | ||
| import { factsheetGlyph } from "@/components/factsheets/factsheets-icons"; | ||
| import { cn, toneDanger, toneWarning } from "@/components/ui-primitives"; | ||
| import { | ||
| readSavedRegistrySlugs, | ||
| savedFactsheetsStorageKey, | ||
| subscribeSavedRegistrySlugs, | ||
| writeSavedRegistrySlugs, | ||
| } from "@/lib/saved-registry-storage"; | ||
| function accentBorder(accent: string) { | ||
| return `color-mix(in srgb, ${accent} 35%, var(--surface))`; | ||
| @@ -40,12 +46,33 @@ export function FactsheetDetailPage({ factsheet }: { factsheet: Factsheet }) { | ||
| const theme = categoryTheme(factsheet.category); | ||
| const [readingLevel, setReadingLevel] = useState<"easy" | "standard">("easy"); | ||
| const [saved, setSaved] = useState(false); | ||
| const [saveNotice, setSaveNotice] = useState(""); | ||
| const [copied, setCopied] = useState(false); | ||
| const related = relatedFactsheets(factsheet.slug); | ||
| const moreInTopic = sameTopicFactsheets(factsheet.slug); | ||
| const toc = tocFor(factsheet); | ||
| const blocks = printBlocks(factsheet); | ||
| const blocks = printBlocks(factsheet, readingLevel); | ||
| useEffect(() => { | ||
| const refresh = () => setSaved(readSavedRegistrySlugs(savedFactsheetsStorageKey).includes(factsheet.slug)); | ||
| refresh(); | ||
| return subscribeSavedRegistrySlugs(refresh); | ||
| }, [factsheet.slug]); | ||
| function toggleSaved() { | ||
| const current = readSavedRegistrySlugs(savedFactsheetsStorageKey); | ||
| const next = current.includes(factsheet.slug) | ||
| ? current.filter((slug) => slug !== factsheet.slug) | ||
| : [factsheet.slug, ...current]; | ||
| if (!writeSavedRegistrySlugs(savedFactsheetsStorageKey, next)) { | ||
BigSimmo marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| setSaveNotice("Save failed. Check browser storage permissions and try again."); | ||
| return; | ||
| } | ||
| const nowSaved = next.includes(factsheet.slug); | ||
| setSaved(nowSaved); | ||
| setSaveNotice(nowSaved ? "Factsheet saved." : "Factsheet removed from saved items."); | ||
| } | ||
| function downloadPdf() { | ||
| if (typeof document === "undefined") return; | ||
| @@ -109,7 +136,7 @@ export function FactsheetDetailPage({ factsheet }: { factsheet: Factsheet }) { | ||
| ) : null} | ||
| <button | ||
| type="button" | ||
| onClick={() => setSaved((value) => !value)} | ||
| onClick={toggleSaved} | ||
| aria-pressed={saved} | ||
| className={cn( | ||
| "inline-flex min-h-tap items-center gap-1.5 rounded-lg border px-3 text-sm font-bold transition focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[color:var(--focus)]", | ||
| @@ -121,6 +148,9 @@ export function FactsheetDetailPage({ factsheet }: { factsheet: Factsheet }) { | ||
| <Bookmark className="h-4 w-4" aria-hidden="true" fill={saved ? "currentColor" : "none"} /> | ||
| {saved ? "Saved" : "Save"} | ||
| </button> | ||
| <span aria-live="polite" className="sr-only"> | ||
| {saveNotice} | ||
| </span> | ||
| <button | ||
| type="button" | ||
| onClick={downloadPdf} | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.