// ONE SEARCH PIPELINE — result ordering. // // Two orderings exist in this corpus and both were written inline at their one // call site: // // newest-first over uploadDate the search result list // (`components/SearchSessionContext.tsx`), which sorts videos and social // posts into ONE list so a unified search reads as one result set rather // than videos-then-posts. // oldest-first over createdAt a social thread // (`mcp/src/search.ts` getThread), tie-broken by id so a thread whose posts // share a timestamp still has a stable order. // // They are comparators, not policies: a caller picks one, and the point of // naming them is that the next consumer (an MCP tool that wants the viewer's // order, say) reaches for the same function instead of re-deriving a // three-branch ternary with its own idea of which way "newest" points. // // Deliberately NOT here: relevance ranking. Nothing in this repo ranks by // score today — the scanner emits in corpus scan order and the viewer emits in // date order — and inventing one under cover of a refactor would change what // every caller returns. export type UploadDated = { uploadDate: string }; export type Threaded = { createdAt: string; id: string }; // Newest upload first. Dates are "YYYYMMDD", so a lexicographic compare is a // chronological one. Equal dates keep their input order (Array#sort is stable), // which is what preserves the summaries index's own ordering within a day. export function byUploadDateDesc(a: UploadDated, b: UploadDated): number { return a.uploadDate === b.uploadDate ? 0 : a.uploadDate < b.uploadDate ? 1 : -1; } // Sorts IN PLACE and returns the same array — matching the call site this // replaced, where the array is freshly built and nobody else holds it. export function rankByUploadDateDesc(items: T[]): T[] { return items.sort(byUploadDateDesc); } // Oldest post first within a thread; ties broken by id so the order is total. export function byCreatedAtThenId(a: Threaded, b: Threaded): number { return a.createdAt === b.createdAt ? a.id.localeCompare(b.id) : a.createdAt.localeCompare(b.createdAt); } export function rankThread(items: T[]): T[] { return items.sort(byCreatedAtThenId); }