From db0652ab24bb0c8e16a3610a12e09cbae59125b4 Mon Sep 17 00:00:00 2001 From: Talha Ahmed Date: Thu, 3 Sep 2026 16:39:55 +0500 Subject: [PATCH 1/8] Inbox: serve the queue from cache instead of re-skeletoning every visit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Opening the Recruitment Inbox showed skeleton placeholders every time, and on the combined All channel it showed them stacked ABOVE rows that had already arrived — the channel ORs its two sources' pending flags together, so the email rows sat under placeholders while the sheet fetch finished. The queue is a local table, not a live feed. Rows only appear when the Sync worker writes them, and that path already invalidates qk.mailbox.all(). So refetching on every visit bought nothing and cost a full-width skeleton each time — worse on the All channel, which fetches BOTH sources unpaged and so re-downloaded thousands of rows to render ten. - LIST_CACHE / COUNT_CACHE: 10 min staleTime, 1 h gcTime, keepPreviousData. A revisit inside the window paints rows immediately and issues no request. - Placeholders now mean "nothing to show yet": gated on the list being empty. A refetch over live rows is a 2px bar and the word "Updating…", with the rows readable and clickable throughout. - "Updated 4 min ago" + a Refresh button under the search box, because a cached list that never admits its age reads as a broken list. Refresh re-reads the DB; Sync pulls new mail from Outlook. Both tooltips now say which is which — two circular arrows were indistinguishable. - Search debounced to 300ms. Each keystroke used to mint a query key, a request and a skeleton, so the list strobed while you typed. - resume_text dropped from the list row mapping. serialize_application ships the whole extracted CV on every row and the queue renders none of it; caching a thousand of them for an hour is not a trade worth making. The detail query fetches it for the one row actually open. Trade-off: a sync run started elsewhere is now invisible here for up to ten minutes. The age label and the Refresh button are the deliberate mitigation. inbox-loading.test.mjs pins all of it, driving hand-gated fetches so the frames a fast API flashes past become assertable: both sources pending, one source home, and a second visit served from cache. Reverting the placeholder rule reproduces the original defect as "skeleton=true rows=2". Wired into npm run verify; needed a mountRoute export on the smoke harness, since renderRoute tears the tree down before those intermediate frames can be read. Co-Authored-By: Claude Opus 5 (1M context) --- frontend/inbox-loading.test.mjs | 255 +++++++++++++++++++++++++++++++ frontend/package.json | 3 +- frontend/src/__smoke__/entry.jsx | 34 ++++- frontend/src/screens/Inbox.jsx | 172 +++++++++++++++++++-- frontend/src/styles/styles.css | 56 +++++++ frontend/src/ui/SyncButton.jsx | 12 +- 6 files changed, 515 insertions(+), 17 deletions(-) create mode 100644 frontend/inbox-loading.test.mjs diff --git a/frontend/inbox-loading.test.mjs b/frontend/inbox-loading.test.mjs new file mode 100644 index 0000000..55e4a0b --- /dev/null +++ b/frontend/inbox-loading.test.mjs @@ -0,0 +1,255 @@ +/** + * Inbox loading-state test — what the queue SHOWS while its data is in flight. + * + * node inbox-loading.test.mjs + * + * The complaint this pins down: opening the Recruitment Inbox showed skeleton + * placeholders every single time, even on a revisit, and on the combined "All" + * channel it showed them stacked ABOVE rows that had already arrived. The queue + * is a local table that only grows when the Sync worker writes to it, so a + * revisit has no business blanking itself. + * + * Every fetch here is gated by hand, so the assertions land on the exact frames + * a fast local API would flash past: both sources pending, one source home, and + * a second visit served from cache. + */ +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { pathToFileURL } from 'node:url' + +import esbuild from 'esbuild' +import { JSDOM } from 'jsdom' + +// ---------------------------------------------------------------- environment +const dom = new JSDOM('
', { + url: 'http://localhost:5173/', + pretendToBeVisual: true, +}) + +globalThis.window = dom.window +globalThis.document = dom.window.document +Object.defineProperty(globalThis, 'navigator', { value: dom.window.navigator, configurable: true }) +globalThis.HTMLElement = dom.window.HTMLElement +globalThis.Element = dom.window.Element +globalThis.Node = dom.window.Node +globalThis.getComputedStyle = dom.window.getComputedStyle +globalThis.localStorage = dom.window.localStorage +globalThis.DOMParser = dom.window.DOMParser +globalThis.requestAnimationFrame = (cb) => setTimeout(() => cb(Date.now()), 0) +globalThis.cancelAnimationFrame = clearTimeout +globalThis.IS_REACT_ACT_ENVIRONMENT = true + +class RO { observe() {} unobserve() {} disconnect() {} } +class MO { observe() {} disconnect() {} takeRecords() { return [] } } +globalThis.ResizeObserver = RO +globalThis.MutationObserver = MO +dom.window.ResizeObserver = RO +dom.window.MutationObserver = MO +dom.window.matchMedia = () => ({ + matches: false, addEventListener() {}, removeEventListener() {}, addListener() {}, removeListener() {}, +}) +dom.window.HTMLCanvasElement.prototype.getContext = () => + new Proxy({}, { get: (_, k) => (k === 'canvas' ? {} : () => {}) }) + +const MODULES = ['dashboard', 'inbox', 'jobs', 'candidates', 'pipeline', 'interviews', 'assessments', + 'offers', 'reports', 'analytics', 'job_board', 'settings', 'rbac_users', 'tasks', 'talent', + 'requisitions'] +const ACTIONS = ['view', 'create', 'edit', 'delete', 'approve', 'export', 'manage', 'configure'] +const permissions = MODULES.flatMap((m) => ACTIONS.map((a) => `${m}.${a}`)) + +dom.window.localStorage.setItem('tf-auth', JSON.stringify({ + access_token: 'test', refresh_token: 'test', expires_in: 1800, + expires_at: Date.now() + 1800_000, + data: { id: 1, name: 'Test User', email: 't@example.com', role_name: 'system_administrator', permissions }, +})) + +// ---------------------------------------------------------------- fixtures +const EMAIL_ROWS = [ + { + id: '11111111-1111-1111-1111-111111111111', + name: 'Ada Lovelace', email: 'ada@example.com', + position: 'Backend Engineer', source: 'careers-rozee@example.com', + received: '2026-09-02T10:00:00Z', unread: true, processing: 'Unread', + }, + { + id: '22222222-2222-2222-2222-222222222222', + name: 'Grace Hopper', email: 'grace@example.com', + position: 'Platform Engineer', source: 'careers-rozee@example.com', + received: '2026-09-01T10:00:00Z', unread: false, processing: 'Read', + }, +] + +const FORM_ROWS = [ + { + id: '33333333-3333-3333-3333-333333333333', + name: 'Katherine Johnson', candidate_email: 'kj@example.com', + position_applied_for: 'Data Analyst', source_of_application: 'LinkedIn', + entry_date: '2026-09-03T00:00:00Z', entry_time: '09:15', processing_state: 'unread', + }, +] + +/** + * One gate per source. `open()` releases every request parked on it, and every + * request that arrives afterwards resolves straight away — which is what makes + * "email home, sheet still travelling" an assertable frame rather than a race. + */ +function gate(payload) { + const waiting = [] + let open = false + return { + calls: 0, + payload, + take() { + this.calls += 1 + if (open) return Promise.resolve(this.payload) + return new Promise((resolve) => waiting.push(() => resolve(this.payload))) + }, + release() { + open = true + waiting.splice(0).forEach((fn) => fn()) + }, + close() { open = false }, + } +} + +const emailGate = gate({ data: EMAIL_ROWS, total: EMAIL_ROWS.length, status_code: 200 }) +const formGate = gate({ data: FORM_ROWS, total: FORM_ROWS.length, status_code: 200 }) + +const COUNTS = { all: 3, unread: 1, processed: 0, rejected: 0, duplicates: 0 } + +globalThis.fetch = async (input) => { + const url = String(input?.url ?? input) + let body + if (url.includes('/inbox/all-applications/count')) body = { total: EMAIL_ROWS.length, status_code: 200 } + else if (url.includes('/inbox/all-applications')) body = await emailGate.take() + else if (url.includes('/inbox/counts')) body = { data: COUNTS, status_code: 200 } + else if (url.includes('/sheet/form-data/count')) body = { total: FORM_ROWS.length, status_code: 200 } + else if (url.includes('/sheet/form-data/counts')) body = { data: { all: 1 }, status_code: 200 } + else if (url.includes('/sheet/form-data/fetch')) body = await formGate.take() + else body = { data: [], status_code: 200 } + return { + ok: true, status: 200, statusText: 'OK', + text: async () => JSON.stringify(body), + } +} + +// ---------------------------------------------------------------- bundle +const outDir = mkdtempSync(join(tmpdir(), 'tf-inbox-')) +const outFile = join(outDir, 'entry.mjs') + +await esbuild.build({ + entryPoints: ['src/__smoke__/entry.jsx'], + outfile: outFile, + bundle: true, + format: 'esm', + platform: 'node', + target: 'node20', + jsx: 'automatic', + loader: { '.js': 'jsx', '.jsx': 'jsx' }, + logLevel: 'error', + define: { + 'process.env.NODE_ENV': '"development"', + 'import.meta.env': JSON.stringify({ DEV: true, PROD: false, MODE: 'development', VITE_API_BASE: '' }), + }, +}) + +// ---------------------------------------------------------------- run +let failed = 0 +function check(name, condition, detail) { + if (condition) { + console.log(`ok ${name}`) + if (detail) console.log(` ${detail}`) + } else { + console.log(`FAIL ${name}`) + if (detail) console.log(` ${detail}`) + failed++ + } +} + +const hasSkeleton = (html) => html.includes('skeleton-row') +const hasRefreshBar = (html) => html.includes('inbox-refresh-bar') +const rowCount = (html) => (html.match(/class="inbox-item/g) || []).length + +try { + const mod = await import(pathToFileURL(outFile).href) + mod.boot() + + const container = dom.window.document.createElement('div') + dom.window.document.body.appendChild(container) + + // ---- frame 1: cold open, neither source home ---------------------------- + const view = await mod.mountRoute('/inbox', container) + check( + 'cold open with nothing cached shows placeholders', + hasSkeleton(view.html()) && rowCount(view.html()) === 0, + `skeleton=${hasSkeleton(view.html())} rows=${rowCount(view.html())}`, + ) + + // ---- frame 2: email home, sheet still travelling ------------------------ + // THE REGRESSION: this frame used to render six placeholders on top of two + // real rows, because the All channel ORs the two pending flags together. + emailGate.release() + await view.settle(60) + const partial = view.html() + check( + 'THE REGRESSION: rows that arrived are never buried under placeholders', + !hasSkeleton(partial) && rowCount(partial) === EMAIL_ROWS.length, + `skeleton=${hasSkeleton(partial)} rows=${rowCount(partial)}`, + ) + check( + 'the half still in flight is named, not mimed', + view.text().includes('Still loading Sheet Form applications'), + ) + check( + 'a refetch over live rows is a hairline bar', + hasRefreshBar(partial), + ) + + // ---- frame 3: both home ------------------------------------------------- + formGate.release() + await view.settle(60) + const settled = view.html() + check( + 'both sources merge into one list', + rowCount(settled) === EMAIL_ROWS.length + FORM_ROWS.length, + `rows=${rowCount(settled)}`, + ) + check( + 'no placeholders and no progress bar once the queue is settled', + !hasSkeleton(settled) && !hasRefreshBar(settled), + ) + check( + 'the list says how old it is', + view.text().includes('Updated just now'), + ) + + const firstVisitEmailCalls = emailGate.calls + await view.unmount() + + // ---- frame 4: leave, come back ----------------------------------------- + // The headline fix. Same query keys, inside staleTime: React Query serves + // the cache, so the first painted frame already has rows and no request goes + // out. This is the assertion that fails if LIST_CACHE is ever removed. + const second = dom.window.document.createElement('div') + dom.window.document.body.appendChild(second) + const revisit = await mod.mountRoute('/inbox', second) + const firstFrame = revisit.html() + check( + 'THE FIX: re-opening the Inbox paints rows immediately, no placeholders', + !hasSkeleton(firstFrame) && rowCount(firstFrame) === EMAIL_ROWS.length + FORM_ROWS.length, + `skeleton=${hasSkeleton(firstFrame)} rows=${rowCount(firstFrame)}`, + ) + await revisit.settle(60) + check( + 're-opening inside staleTime issues no new list request', + emailGate.calls === firstVisitEmailCalls, + `calls before=${firstVisitEmailCalls} after=${emailGate.calls}`, + ) + await revisit.unmount() +} finally { + rmSync(outDir, { recursive: true, force: true }) +} + +console.log(failed ? `\n${failed} inbox loading check(s) FAILED` : '\nAll inbox loading checks passed') +process.exit(failed ? 1 : 0) diff --git a/frontend/package.json b/frontend/package.json index 1ab5aea..c3763f3 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -11,8 +11,9 @@ "smoke": "node smoke.test.mjs", "test:token": "node token.test.mjs", "test:theme": "node theme.test.mjs", + "test:inbox": "node inbox-loading.test.mjs", "test:mobile": "node mobile.test.mjs", - "verify": "vite build && node smoke.test.mjs && node token.test.mjs && node theme.test.mjs" + "verify": "vite build && node smoke.test.mjs && node token.test.mjs && node theme.test.mjs && node inbox-loading.test.mjs" }, "dependencies": { "@tanstack/react-query": "^5.101.4", diff --git a/frontend/src/__smoke__/entry.jsx b/frontend/src/__smoke__/entry.jsx index c06942c..aa7018f 100644 --- a/frontend/src/__smoke__/entry.jsx +++ b/frontend/src/__smoke__/entry.jsx @@ -78,8 +78,32 @@ export function boot() { initializeCache(queryClient) } -/** Mount one route, wait for effects to settle, return its rendered text. */ -export async function renderRoute(path, container) { +/** + * Like renderRoute, but hands the mount BACK instead of tearing it down. + * + * renderRoute answers "did this route render at all". The Inbox loading test + * asks a different question — what is on screen between one fetch resolving + * and the next — so it needs to step time itself and read the DOM at each + * step. `settle` must come from here, not the test file, because act() has to + * be the bundle's React, not a second copy. + */ +export async function mountRoute(path, container) { + const tree = routeTree(path) + const root = createRoot(container) + await act(async () => { root.render(tree) }) + const settle = async (ms = 20) => { + await act(async () => { await new Promise((r) => setTimeout(r, ms)) }) + } + await settle() + return { + settle, + html: () => container.innerHTML, + text: () => container.textContent || '', + unmount: async () => { await act(async () => { root.unmount() }) }, + } +} + +function routeTree(path) { const h = React.createElement const isAuth = path.startsWith('/auth/') const def = TABLE.find((r) => `/${r.path}` === path) @@ -93,7 +117,7 @@ export async function renderRoute(path, container) { h(Route, { path, element: h(Screen) }), ) - const tree = h( + return h( QueryClientProvider, { client: queryClient }, h(ThemeProvider, null, h(ToastProvider, null, @@ -105,7 +129,11 @@ export async function renderRoute(path, container) { ), ), ) +} +/** Mount one route, wait for effects to settle, return its rendered text. */ +export async function renderRoute(path, container) { + const tree = routeTree(path) const root = createRoot(container) try { await act(async () => { root.render(tree) }) diff --git a/frontend/src/screens/Inbox.jsx b/frontend/src/screens/Inbox.jsx index 988596f..95905f2 100644 --- a/frontend/src/screens/Inbox.jsx +++ b/frontend/src/screens/Inbox.jsx @@ -10,7 +10,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { useNavigate } from 'react-router-dom' -import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' +import { keepPreviousData, useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import Modal from '../ui/Modal' import EmailBody, { looksLikeHtml } from '../ui/EmailBody' @@ -42,6 +42,46 @@ const FORM_TABS = ['All Applications', 'Processed', 'Rejected', 'Duplicates'] /** Inbox GET `top` / sheet GET `limit` both cap at 500. */ const PAGE_SIZE_MAX = 500 +/** + * Cache policy for the queue. + * + * This list is a local table, not a live feed. Applications only appear when + * the Sync worker writes them, and that path already invalidates + * qk.mailbox.all() the moment a run completes — so refetching on every visit + * bought nothing and cost a full-width skeleton each time. The All channel + * makes that worse: it fetches BOTH sources unpaged, so the "loading" state a + * recruiter saw on re-entry was thousands of rows being re-downloaded to + * render the same ten. + * + * staleTime therefore covers a normal working stretch, and keepPreviousData + * means a tab switch, a page turn or a keystroke re-renders the rows already + * on screen instead of blanking them. Nothing here can hide new mail: Sync + * invalidates, and every write mutation on this screen already does too. + */ +const INBOX_STALE_MS = 10 * 60_000 +const INBOX_GC_MS = 60 * 60_000 +const LIST_CACHE = { + staleTime: INBOX_STALE_MS, + gcTime: INBOX_GC_MS, + placeholderData: keepPreviousData, +} +const COUNT_CACHE = { staleTime: INBOX_STALE_MS, gcTime: INBOX_GC_MS } + +/** Typing must not put a query key (and a skeleton) on screen per keystroke. */ +const SEARCH_DEBOUNCE_MS = 300 + +/** "Updated 4 min ago" under the search box — the honest label for a cached list. */ +function agoLabel(ts) { + if (!ts) return null + const secs = Math.max(0, Math.round((Date.now() - ts) / 1000)) + if (secs < 45) return 'just now' + const mins = Math.round(secs / 60) + if (mins < 60) return `${mins} min ago` + const hours = Math.round(mins / 60) + if (hours < 24) return `${hours} hr ago` + return `${Math.round(hours / 24)} d ago` +} + const SYNC_RUN_KEY = 'mailbox_sync_run_id' function readStoredSyncRunId() { @@ -379,7 +419,11 @@ async function fetchApplications(params) { filePath: row.file_path || '', linkedinSlug: row.linkedin_slug || '', linkedinUrl: row.linkedin_url || '', - resumeText: row.resume_text || '', + // resume_text is deliberately DROPPED here. serialize_application ships + // the whole extracted CV on every row, and the queue renders none of + // it — but the cache would then hold a thousand full resumes for the + // hour that LIST_CACHE keeps a page alive. The detail query fetches it + // for the one row actually open, which is the only place it is read. atsScore: row.ats_score, phone: row.phone, experience: row.experience, @@ -722,6 +766,47 @@ function BulkReadBar({ rows, selection, onSetRead, onSetAllRead, busy, canEdit, ) } +/** + * "Updated 4 min ago" + Refresh, under the search box. + * + * A cached list that never says how old it is reads as a broken list. This is + * the honest label, and the button next to it re-reads the DB — deliberately + * NOT the same act as Sync, which pulls new mail from Outlook. The tooltips + * say which is which because the two were previously indistinguishable. + * + * Owns its own interval so the minute count stays current without re-rendering + * the queue: under "Show all" that parent render is a thousand rows. + */ +function QueueFreshness({ at, refreshing, onRefresh }) { + const [, tick] = useState(0) + useEffect(() => { + if (!at) return undefined + const t = setInterval(() => tick((n) => n + 1), 30_000) + return () => clearInterval(t) + }, [at]) + + return ( +
+ + {refreshing + ? 'Updating…' + : at + ? `Updated ${agoLabel(at)}` + : 'Loading…'} + + +
+ ) +} + export default function Inbox() { const { toast } = useToast() const navigate = useNavigate() @@ -741,6 +826,15 @@ export default function Inbox() { const [assigning, setAssigning] = useState(null) const [noting, setNoting] = useState(null) + // The box updates on every keystroke; the QUERY KEY only settles when typing + // pauses. Untyped, each character produced a fresh key, an in-flight request + // and a skeleton — the list strobed while you searched. + const [search, setSearch] = useState('') + useEffect(() => { + const t = setTimeout(() => setSearch(q.trim()), SEARCH_DEBOUNCE_MS) + return () => clearTimeout(t) + }, [q]) + const isForms = channel === 'forms' // Combined channel: both sources fetched UNPAGED (each endpoint reads a // missing top/limit as no LIMIT), merged by date, and paged client-side — @@ -756,8 +850,8 @@ export default function Inbox() { // endpoint reads a missing top as unpaged. top: pageSize === 'all' || isAllChannel ? undefined : pageSize, skip: isAllChannel ? 0 : skip, - ...(q.trim() ? { search: q.trim() } : {}), - }), [tabFilter, skip, pageSize, q, isAllChannel]) + ...(search ? { search } : {}), + }), [tabFilter, skip, pageSize, search, isAllChannel]) const formParams = useMemo(() => ({ // All channel spans every sheet tab, not just the selected one. @@ -765,13 +859,14 @@ export default function Inbox() { offset: isAllChannel ? 0 : skip, limit: pageSize === 'all' || isAllChannel ? undefined : pageSize, ...formTabFilter, - ...(q.trim() ? { search: q.trim() } : {}), - }), [formSheet, skip, pageSize, q, formTabFilter, isAllChannel]) + ...(search ? { search } : {}), + }), [formSheet, skip, pageSize, search, formTabFilter, isAllChannel]) const applicationsQuery = useQuery({ queryKey: qk.mailbox.applications(listParams), queryFn: () => fetchApplications(listParams), enabled: !isForms, + ...LIST_CACHE, }) const formSheetsQuery = useQuery({ @@ -788,12 +883,14 @@ export default function Inbox() { queryKey: qk.mailbox.formData(formParams), queryFn: () => fetchFormApplications(formParams), enabled: isForms || isAllChannel, + ...LIST_CACHE, }) const countsQuery = useQuery({ queryKey: qk.mailbox.counts(), queryFn: fetchInboxCounts, enabled: !isForms, + ...COUNT_CACHE, }) const formCountsSheet = isAllChannel ? undefined : (formSheet || undefined) @@ -804,6 +901,7 @@ export default function Inbox() { return res?.data ?? {} }, enabled: isForms || isAllChannel, + ...COUNT_CACHE, }) const emailTotalQuery = useQuery({ @@ -858,6 +956,14 @@ export default function Inbox() { // one healthy source still renders; error only when both are down isError: applicationsQuery.isError && formQuery.isError, isSuccess: applicationsQuery.isSuccess && formQuery.isSuccess, + // Either source still on the wire keeps the quiet refresh bar up. + isFetching: applicationsQuery.isFetching || formQuery.isFetching, + // The OLDER of the two: the merged list is only as fresh as its + // staler half, and claiming otherwise would be a lie on the label. + dataUpdatedAt: Math.min( + applicationsQuery.dataUpdatedAt || Infinity, + formQuery.dataUpdatedAt || Infinity, + ), error: applicationsQuery.error ?? formQuery.error, data: { rows: mergedRows ?? [], total: mergedRows?.length ?? 0 }, } @@ -890,7 +996,7 @@ export default function Inbox() { const countsReady = isAllChannel ? countsQuery.isSuccess && formCountsQuery.isSuccess : (isForms ? formCountsQuery.isSuccess : countsQuery.isSuccess) - const total = q.trim() + const total = search ? (activeQuery.data?.total ?? 0) : (countsReady ? tabTotal : (poolTotal || (activeQuery.data?.total ?? 0))) // 'all' = unpaged: the fetch omits top/limit (the endpoints treat a missing @@ -934,18 +1040,41 @@ export default function Inbox() { * What "Mark all" means here, in words, for the button tooltip. Kept next to * setReadEverything so the label and the scope cannot drift apart. */ - const scopeLabel = q.trim() + const scopeLabel = search ? `the ${list.length} row${list.length === 1 ? '' : 's'} matching this search` : tab === 'All Applications' ? 'every application' : `the ${tab} tab` + /** + * Loading state, in priority order: rows already on screen ALWAYS win. + * + * The old rule was `activeQuery.isPending && `, which on the + * All channel put six placeholders ABOVE the email rows that had already + * arrived while the sheet fetch finished — a list that looked broken while + * it worked. Placeholders now mean "there is genuinely nothing to show yet"; + * a refresh over existing rows is a hairline bar and one word of text. + */ + const hasRows = list.length > 0 + const showSkeleton = !hasRows && (activeQuery.isPending || activeQuery.isFetching) + const refreshing = hasRows && Boolean(activeQuery.isFetching) + const updatedAt = Number.isFinite(activeQuery.dataUpdatedAt) && activeQuery.dataUpdatedAt > 0 + ? activeQuery.dataUpdatedAt + : null + + /** Re-read what is already stored here. Sync is the one that fetches mail. */ + const refreshQueue = useCallback(() => { + qc.invalidateQueries({ queryKey: qk.mailbox.all() }) + }, [qc]) + function switchChannel(next) { if (next === channel) return setChannel(next) setSkip(0) setSelectedId(null) setQ('') + setSearch('') // clear the committed term too, or the new channel's first + // fetch carries the old channel's search for 300ms // Unread is email-only; leave it behind when opening Sheet Forms or All. if (next !== 'email' && tab === 'Unread') setTab('All Applications') selection.clear() @@ -967,7 +1096,7 @@ export default function Inbox() { if (SERVER_SCOPED_TABS.has(tab)) { setReadAll.mutate({ read, - filter: { ...tabFilter, ...(q.trim() ? { search: q.trim() } : {}) }, + filter: { ...tabFilter, ...(search ? { search } : {}) }, // sheet rows carry no mailbox read state — email rows only ids: list.filter((i) => i.kind !== 'form').map((i) => i.id), }) @@ -1257,9 +1386,13 @@ export default function Inbox() { placeholder={isForms ? 'Search name, email, position, city…' : 'Search applications…'} /> + -
- {activeQuery.isPending && ( +
+ {/* Hairline, not a skeleton: the rows below stay readable and + clickable while the refetch lands. */} + {refreshing &&
} + {showSkeleton && ( )} {/* The big error state only when there is truly nothing to @@ -1269,6 +1402,18 @@ export default function Inbox() { {friendlyAuthError(activeQuery.error, 'Request failed')} )} + {/* All channel, one source home and one still travelling: say + which. The merge re-sorts by date when the second lands, so + the list is about to reshuffle and that should not be a + surprise. */} + {isAllChannel && hasRows + && (Boolean(applicationsQuery.isFetching) !== Boolean(formQuery.isFetching)) && ( +
+ {applicationsQuery.isFetching + ? 'Still loading email applications…' + : 'Still loading Sheet Form applications…'} +
+ )} {/* All channel with one source down: keep the healthy list, note the gap in one line instead of a full error state. */} {isAllChannel && applicationsQuery.isError !== formQuery.isError @@ -1287,7 +1432,10 @@ export default function Inbox() { : 'Sheet Form applications couldn’t load right now — showing Email only.'}
)} - {activeQuery.isSuccess && list.length === 0 ? ( + {/* `!showSkeleton` matters now that keepPreviousData holds the + query in `success` while a new key loads: without it a cold + tab flashed "Nothing here" over a request still in flight. */} + {activeQuery.isSuccess && list.length === 0 && !showSkeleton ? ( {isForms ? 'No form responses in this sheet tab.' : 'No applications in this view.'} diff --git a/frontend/src/styles/styles.css b/frontend/src/styles/styles.css index 49e13be..e363438 100644 --- a/frontend/src/styles/styles.css +++ b/frontend/src/styles/styles.css @@ -1137,6 +1137,62 @@ canvas { width: 100%; max-width: 100%; display: block; } font-size: 12px; white-space: nowrap; } + +/* ---- Quiet refresh ------------------------------------------------------- + The queue is cached between visits (see LIST_CACHE in screens/Inbox.jsx), + so a revisit repaints the rows instantly and any refetch happens underneath + them. These three elements are that refetch's entire vocabulary: an age + label so a cached list never pretends to be live, a hairline bar while a + request is out, and one line naming the slower half of the All channel. */ +.inbox-freshness { + display: flex; + align-items: center; + justify-content: space-between; + gap: 8px; + margin-top: 8px; + min-width: 0; +} +.inbox-freshness .cell-sub { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} +.inbox-freshness .btn-sm { + padding: 3px 8px; + font-size: 12px; + flex-shrink: 0; +} +.inbox-list-body { position: relative; } +/* Sticky so it stays visible when the refetch starts from a scrolled queue. */ +.inbox-refresh-bar { + position: sticky; + top: 0; + z-index: 2; + height: 2px; + overflow: hidden; + background: var(--bg-sunken); +} +.inbox-refresh-bar::after { + content: ''; + position: absolute; + inset: 0; + width: 38%; + background: var(--primary); + animation: inboxRefreshSlide 1.1s ease-in-out infinite; +} +@keyframes inboxRefreshSlide { + 0% { transform: translateX(-100%); } + 100% { transform: translateX(365%); } +} +@media (prefers-reduced-motion: reduce) { + .inbox-refresh-bar::after { animation: none; width: 100%; opacity: .5; } +} +.inbox-partial-note { + padding: 7px 16px; + border-bottom: 1px solid var(--border); + font-size: var(--fs-xs); + color: var(--text-3); +} /* `--chip` is the source's own brand colour, set inline. It tints the background and fills the dot, while the label stays on theme text so 11px copy keeps its contrast in both modes. */ diff --git a/frontend/src/ui/SyncButton.jsx b/frontend/src/ui/SyncButton.jsx index 5c81835..a386a0e 100644 --- a/frontend/src/ui/SyncButton.jsx +++ b/frontend/src/ui/SyncButton.jsx @@ -35,13 +35,23 @@ export default function SyncButton({ run, pending, disabled, onClick }) { ? 'Syncing' : 'Sync' + // Sync and the queue's own Refresh button are different acts: this one goes + // out to Outlook and writes new rows, the other re-reads what is already + // stored. Say so — two circular arrows on one screen otherwise read as the + // same button. + const tip = disabled && !busy + ? 'Requires inbox.edit' + : busy + ? undefined + : 'Pull new mail from Outlook into this inbox' + return (
{i.processing} + {/* Exceptions only: a healthy row shows nothing, so the + badge stays a signal rather than decoration. + The kind check is redundant TODAY — mapFormRow sets no + resumeStatus, so the truthiness test already excludes + sheet rows. It is kept because those applicants have no + mailbox attachment to parse at all, and a future field + named resumeStatus on a form row must not be read as a + parse verdict. */} + {i.kind !== 'form' && i.resumeStatus && i.resumeStatus !== 'Parsed' && ( + + {i.resumeStatus} + + )} {i.applicationStatus && i.applicationStatus !== 'CLOSED' && ( {formatRole(i.applicationStatus)} )} @@ -2075,7 +2116,10 @@ function ApplicationDetail({ {i.applicationStatus && i.applicationStatus !== 'CLOSED' && ( <>{formatRole(i.applicationStatus)}{' '} )} - + {i.resumeStatus} {' '} {loading && Loading details…} diff --git a/frontend/src/ui/primitives.jsx b/frontend/src/ui/primitives.jsx index baf8757..da48984 100644 --- a/frontend/src/ui/primitives.jsx +++ b/frontend/src/ui/primitives.jsx @@ -48,9 +48,15 @@ export const STATUS_CLASS = { // define an identical private copy. export const PRIORITY_CLASS = { High: 'b-red', Medium: 'b-amber', Low: 'b-gray' } -export function Badge({ children, className }) { +/** + * `title` is optional and usually unset. It exists for badges whose one-word + * label is not self-explanatory — "Failed" on an Inbox row, for instance, where + * the hover has to say that the CV text could not be read and the attachment is + * still there to open by hand. + */ +export function Badge({ children, className, title }) { const cls = className || STATUS_CLASS[children] || 'b-gray' - return {children} + return {children} } export function ScoreChip({ score }) { From 3b5ba425e87949a9c772579d4668e618e6008023 Mon Sep 17 00:00:00 2001 From: Talha Ahmed Date: Thu, 3 Sep 2026 18:22:59 +0500 Subject: [PATCH 3/8] CI: verify the build before it ships, and fix two dead lines in the deploy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pipeline deployed whatever was on main without checking any of it. No build, no tests, no lint. A frontend that failed to compile zipped and uploaded exactly like a working one, and the first sign of trouble would have been the running site. The checks now gate the deploy. `deploy` needs a `checks` job, so a red main never reaches the bucket. A second workflow runs the same script on branches and pull requests, so the answer arrives before the merge rather than after it. main is excluded there because the deploy workflow already covers it. Both call scripts/ci-checks.sh rather than inlining the commands twice, which is the only way the two cannot drift into disagreeing about what passing means. What gates, and what deliberately does not: - frontend: npm ci then npm run verify. All four suites are jsdom, so no browser is needed on the runner. Node 22 matches frontend/Dockerfile. - bulk-ats: ruff check, ruff format --check, mypy, pytest. Scoped to app and tests. Repo-wide, ruff reports 926 errors and would reformat 126 files — the backend is written to another style and demanding a rewrite of it is not this change's business. The scoped set is clean today. - backend/tests: passes and gates, minus test_candidate_forms.py and test_employment_agent.py. Those nine failures predate this change and are unrelated to it. They are named in the script, not silently skipped, so the exclusion stays visible and someone can delete the two lines. Two dead lines in the deploy, found while reading it: - The step named "Configure AWS credentials" set three variables and then only echoed a message. `env:` is scoped to its own step, so the values were discarded before anything could use them. It did nothing while reading as though credentials were configured globally. Removed; the upload step sets them where they are actually used. - `-x ".gitignore/*"` excludes a *directory* named .gitignore, which does not exist, so the file was never excluded. Now `-x ".gitignore"`. The zip still ships frontend/node_modules, about 90 MB and most of the artifact. Left alone on purpose: nothing in this repo says what unpacks the object — there is no appspec and no deploy script here — so if that side runs without installing dependencies, dropping it would break the deploy. The workflow now carries a comment saying so and the one-line change to make once that is confirmed. .gitattributes pins *.sh to LF. The script is bash on a Linux runner; committed with CRLF from a machine with core.autocrlf=false it would fail on line one with `$'\r': command not found`, which reads as a broken pipeline rather than a line-ending problem. Verified by running scripts/ci-checks.sh locally end to end, exit 0. Only the npm ci line was skipped, because it would rewrite the 5,230 committed node_modules files; frontend/Dockerfile already builds that way. Both workflow files were parsed and their job graph inspected. Co-Authored-By: Claude Opus 5 (1M context) --- .gitattributes | 9 ++++ .gitea/workflows/ci.yml | 36 +++++++++++++++ .gitea/workflows/deploy-to-s3.yml | 73 ++++++++++++++++++++++--------- scripts/ci-checks.sh | 58 ++++++++++++++++++++++++ 4 files changed, 155 insertions(+), 21 deletions(-) create mode 100644 .gitattributes create mode 100644 .gitea/workflows/ci.yml create mode 100755 scripts/ci-checks.sh diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..51a702b --- /dev/null +++ b/.gitattributes @@ -0,0 +1,9 @@ +# Shell scripts must be LF in the repository, whatever a contributor's +# core.autocrlf happens to be. +# +# scripts/ci-checks.sh is executed by bash on the Gitea runner. Committed with +# CRLF it fails there with `$'\r': command not found` on the first line, which +# reads as a broken pipeline rather than a line-ending problem. This machine +# has core.autocrlf=true and normalises correctly on its own; that is a local +# setting, not a property of the repo, so it is pinned here instead. +*.sh text eol=lf diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..7db53b6 --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,36 @@ +name: CI + +# Same checks deploy-to-s3.yml gates on, run before a change reaches main. +# main itself is excluded because the deploy workflow already runs them there; +# without branches-ignore every merge would run the suite twice. +on: + push: + branches-ignore: + - main + pull_request: + +jobs: + checks: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Node + uses: actions/setup-node@v4 + with: + node-version: '22' + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - name: Install Python dependencies + run: | + python -m pip install --upgrade pip + pip install -e ".[dev]" + pip install -r backend/requirements.txt + + - name: Run checks + run: bash scripts/ci-checks.sh diff --git a/.gitea/workflows/deploy-to-s3.yml b/.gitea/workflows/deploy-to-s3.yml index 76500c8..b4102e0 100644 --- a/.gitea/workflows/deploy-to-s3.yml +++ b/.gitea/workflows/deploy-to-s3.yml @@ -1,25 +1,58 @@ name: Deploy to S3 +# main only. Everything else is covered by ci.yml, which runs the same checks +# without deploying. on: push: - branches: + branches: - main jobs: - deploy: + # Nothing was verified before this existed: a frontend that failed to compile + # would zip and ship exactly like a working one. `deploy` now needs this job, + # so a red main does not reach the bucket. + checks: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v3 - - name: Configure AWS credentials - env: - AWS_ACCESS_KEY_ID: ${{ secrets.DEVOPS_USER_AWS_ACCESS_KEY_ID }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.DEVOPS_USER_AWS_SECRET_ACCESS_KEY }} - AWS_DEFAULT_REGION: us-east-1 - run: | - echo "AWS credentials configured" + # 22 to match frontend/Dockerfile, so CI resolves the same tree the + # production image builds from. + - name: Set up Node + uses: actions/setup-node@v4 + with: + node-version: '22' + # 3.11 is the floor in pyproject.toml and the version the project's conda + # env runs. + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - name: Install Python dependencies + run: | + python -m pip install --upgrade pip + pip install -e ".[dev]" + pip install -r backend/requirements.txt + + - name: Run checks + run: bash scripts/ci-checks.sh + + deploy: + needs: checks + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + # NOTE: this zip still contains frontend/node_modules, roughly 90 MB and + # the bulk of the artifact. It is left in deliberately. Nothing in this + # repo says what unpacks the zip on the other side — there is no appspec + # file and no deploy script here — so if that side runs the app without + # installing dependencies, dropping node_modules would break the deploy. + # Confirm what consumes the bucket object, then add -x "frontend/node_modules/*". - name: Archive project run: | apt-get update -y @@ -27,8 +60,8 @@ jobs: zip -r utopia-ai-hr-ats-portal.zip . \ -x ".git/*" \ -x ".gitea/*" \ - -x ".gitignore/*" \ - -x "*.DS_Store" + -x ".gitignore" \ + -x "*.DS_Store" - name: Install AWS CLI run: | @@ -37,20 +70,18 @@ jobs: curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" unzip -q awscliv2.zip ./aws/install - aws --version + aws --version + # The credentials live only on this step. There used to be a separate + # "Configure AWS credentials" step above that set the same three variables + # and then only echoed a message — env: is scoped to its own step, so + # those values were discarded before anything could use them. It was doing + # nothing, and it read as though credentials were set up globally. - name: Upload files to S3 env: AWS_ACCESS_KEY_ID: ${{ secrets.DEVOPS_USER_AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.DEVOPS_USER_AWS_SECRET_ACCESS_KEY }} AWS_DEFAULT_REGION: us-east-1 run: | - echo "Uploading repo contents to S3..." - aws s3 cp utopia-ai-hr-ats-portal.zip s3://utopia-ai-s3-repo-bucket/utopia-ai-hr-ats-portal.zip - - - - - - - + echo "Uploading repo contents to S3..." + aws s3 cp utopia-ai-hr-ats-portal.zip s3://utopia-ai-s3-repo-bucket/utopia-ai-hr-ats-portal.zip diff --git a/scripts/ci-checks.sh b/scripts/ci-checks.sh new file mode 100755 index 0000000..c820a4e --- /dev/null +++ b/scripts/ci-checks.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +# +# Everything CI gates on, in one place. +# +# Two workflows call this: deploy-to-s3.yml runs it before shipping, and ci.yml +# runs it on branches and pull requests. Keeping the commands here rather than +# inline in both YAML files is the only way those two can't drift into +# disagreeing about what "passing" means. +# +# Runnable locally, from the repo root: bash scripts/ci-checks.sh +# +set -euo pipefail + +cd "$(dirname "$0")/.." + +step() { printf '\n\033[1m=== %s ===\033[0m\n' "$1"; } + +# ---------------------------------------------------------------- frontend +# npm ci, not npm install: it installs the lockfile exactly and fails if +# package.json and the lock have drifted apart. frontend/Dockerfile already +# builds this way, so CI and the production image resolve identical trees. +# +# It also wipes node_modules first, which matters here because node_modules is +# committed to this repo. CI gets a clean tree regardless of what was checked in. +step "frontend — build, 31 route renders, token, theme, inbox" +( + cd frontend + npm ci --no-audit --no-fund + npm run verify +) + +# ---------------------------------------------------------------- bulk-ats +# Scoped to `app` and `tests` on purpose. `ruff check .` over the whole repo +# reports 926 errors and `ruff format --check .` would rewrite 126 files: the +# backend was written to a different style and reformatting it is not CI's job +# to demand. These two directories are the bulk-ats package that CLAUDE.md sets +# the standard for, and they are clean today, so they can gate. +step "bulk-ats package — lint, format, types, tests" +ruff check app tests +ruff format --check app tests +mypy app +pytest tests -q + +# ---------------------------------------------------------------- backend +# The rest of backend/tests passes and gates normally. These two files do not, +# on main, independently of any change in this repo: +# +# test_candidate_forms.py 6 failures +# test_employment_agent.py 3 failures +# +# They are named here rather than silently skipped so the exclusion stays +# visible and temporary. Fix them and delete these two lines. +step "backend suite — minus two files that fail on main today" +pytest backend/tests -q \ + --ignore=backend/tests/test_candidate_forms.py \ + --ignore=backend/tests/test_employment_agent.py + +printf '\n\033[1mAll CI checks passed\033[0m\n' From 0113509fa17c49f93550c655d941aea859b2ad69 Mon Sep 17 00:00:00 2001 From: Talha Ahmed Date: Thu, 3 Sep 2026 18:39:43 +0500 Subject: [PATCH 4/8] Deploy: stop shipping 90 MB of node_modules the build throws away MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The S3 artifact is 32.9 MiB compressed. Every other project's zip in that bucket is under 1 MiB. The difference is frontend/node_modules, which is committed to this repo and so was swept into every upload. Earlier I left it in because nothing in the repo says what consumes the bucket object, and if that side ran the app without installing dependencies, dropping it would have broken the deploy. That is now answered rather than assumed. Traced on the instance: CodeDeploy extracts to /opt/codedeploy-extracted-5, the AfterInstall hook copies the tree to /home/ec2-user/utopia-ai-hr-ats-portal-deployment-group, then runs `docker compose --env-file ./backend/.env up -d --build`. Eleven containers come up and the only Node one is hrms-frontend, built from frontend/Dockerfile, which does `npm ci` against the lockfile. On top of that frontend/.dockerignore excludes node_modules/ from the build context outright, so even the copy that arrived could not have been read. It was carried across the wire on every merge to main and then discarded unread. The exclusion patterns were verified rather than trusted: zip -r with -x on a synthetic tree in a temp dir on the box, since zip is not available locally. That run also confirmed the previous commit's other fix — the original `-x ".gitignore/*"` really did fail to match the file, and `-x ".gitignore"` matches it. Not done here, deliberately: node_modules is still tracked in git, which is why it was in the artifact in the first place. Untracking it deletes 5,230 files from every other contributor's working tree on their next pull, across thirteen active branches, and needs a heads-up rather than a surprise. Two things found while reading the deploy script, neither touched: - It copies with `cp -r` and never deletes, so a file removed from the repo survives on the server indefinitely. Switching to a delete-on-sync would risk backend/.env, which the script deliberately preserves. - It re-downloads the latest docker compose and buildx from GitHub on every single deploy, unpinned, as root. Co-Authored-By: Claude Opus 5 (1M context) --- .gitea/workflows/deploy-to-s3.yml | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/.gitea/workflows/deploy-to-s3.yml b/.gitea/workflows/deploy-to-s3.yml index b4102e0..c5883f0 100644 --- a/.gitea/workflows/deploy-to-s3.yml +++ b/.gitea/workflows/deploy-to-s3.yml @@ -47,12 +47,18 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 - # NOTE: this zip still contains frontend/node_modules, roughly 90 MB and - # the bulk of the artifact. It is left in deliberately. Nothing in this - # repo says what unpacks the zip on the other side — there is no appspec - # file and no deploy script here — so if that side runs the app without - # installing dependencies, dropping node_modules would break the deploy. - # Confirm what consumes the bucket object, then add -x "frontend/node_modules/*". + # frontend/node_modules is excluded, and that is safe because of what + # happens to this object downstream. CodeDeploy pulls it, extracts to + # /opt/codedeploy-extracted-5, copies the tree to + # /home/ec2-user/utopia-ai-hr-ats-portal-deployment-group and runs + # `docker compose --env-file ./backend/.env up -d --build`. The only Node + # service is the frontend, whose image does `npm ci` from the lockfile, + # and frontend/.dockerignore excludes node_modules/ from the build context + # outright. So the committed tree was carried into every artifact and then + # thrown away unread. It was 90 MB of a 33 MB compressed upload. + # + # node_modules is still tracked in git, which is the reason it was here at + # all. Untracking it is a separate change and affects other branches. - name: Archive project run: | apt-get update -y @@ -61,6 +67,7 @@ jobs: -x ".git/*" \ -x ".gitea/*" \ -x ".gitignore" \ + -x "frontend/node_modules/*" \ -x "*.DS_Store" - name: Install AWS CLI From 22b2a7323fe4fc23a4afc89e386add750614fa08 Mon Sep 17 00:00:00 2001 From: Talha Ahmed Date: Thu, 3 Sep 2026 19:53:23 +0500 Subject: [PATCH 5/8] Fix inbox loading stall Co-authored-by: Cursor --- backend/inbox/models.py | 17 +++++++++++++++-- backend/inbox/serializers.py | 31 +++++++++++++++++++++++-------- backend/inbox/views.py | 8 ++++---- frontend/src/screens/Inbox.jsx | 17 ++++++++++------- 4 files changed, 52 insertions(+), 21 deletions(-) diff --git a/backend/inbox/models.py b/backend/inbox/models.py index 8875179..0aba27e 100644 --- a/backend/inbox/models.py +++ b/backend/inbox/models.py @@ -13,7 +13,7 @@ from sqlalchemy import Column, DateTime, case, func, or_, update from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.exc import IntegrityError from sqlalchemy.ext.asyncio import AsyncSession -from sqlalchemy.orm import selectinload +from sqlalchemy.orm import defer, selectinload from sqlmodel import Field, Relationship, SQLModel, select, true from job.candidate.models import Activity, Feedback, Interviews @@ -821,7 +821,7 @@ class Inbox_Messages(SQLModel, table=True): @classmethod async def get_inbox_messages( - cls, session: AsyncSession, top: int | None, skip: int, search: str | None, isread: bool=True, application_status: Candidate_application_Status=Candidate_application_Status.CLOSED, assigned: bool | None=None, is_duplicate: bool | None=None, no_suggestions: bool | None=None, processing_state: str | None=None + cls, session: AsyncSession, top: int | None, skip: int, search: str | None, isread: bool=True, application_status: Candidate_application_Status=Candidate_application_Status.CLOSED, assigned: bool | None=None, is_duplicate: bool | None=None, no_suggestions: bool | None=None, processing_state: str | None=None, light: bool=False ): statement = cls._apply_filters( @@ -835,6 +835,19 @@ class Inbox_Messages(SQLModel, table=True): if top is not None: statement = statement.limit(top) + # List screens never render these. Loading them for every row is what + # makes GET /inbox/all-applications hang on a full mailbox: each value + # is TOASTed (Graph payload, extracted CV, HTML body). Do not read + # them after this — a deferred access lazy-loads per row. + if light: + statement = statement.options( + defer(cls.full_email_response), + defer(cls.resume_text), + defer(cls.message_body), + defer(cls.message_reply), + defer(cls.match_reasoning), + ) + result = await session.execute(statement) return result.scalars().all() diff --git a/backend/inbox/serializers.py b/backend/inbox/serializers.py index 84250d2..61c43d7 100644 --- a/backend/inbox/serializers.py +++ b/backend/inbox/serializers.py @@ -13,9 +13,19 @@ _RESUME_STATUS = { } -def _sender_name(message: Inbox_Messages) -> str: - """Graph's display name when the payload carries one, else the raw address.""" - sender_name = message.message_from +def _sender_name(message: Inbox_Messages, *, light: bool = False) -> str: + """Graph's display name when the payload carries one, else the raw address. + + `light` must not touch `full_email_response` — the list query defers that + column, and reading it here would lazy-load the whole Graph payload per row. + """ + raw = message.message_from or "" + if light: + # "Jane Doe " → Jane Doe; otherwise the address as stored. + if "<" in raw and raw.endswith(">"): + return raw.split("<", 1)[0].strip() or raw + return raw + sender_name = raw full = message.full_email_response if isinstance(full, dict): from_block = full.get("from") @@ -81,7 +91,7 @@ _PROCESSING_LABEL = { } -def serialize_application(message: Inbox_Messages, *, linkedin_url=None) -> dict: +def serialize_application(message: Inbox_Messages, *, linkedin_url=None, light: bool = False) -> dict: """inbox_messages row -> the shape the #inbox All Applications tab renders. `position` is the mail subject and `source` is the To address, which is where @@ -92,15 +102,18 @@ def serialize_application(message: Inbox_Messages, *, linkedin_url=None) -> dict processed / rejected from the processing_state column so those writes are visible; the default `unread` state still follows message_read so existing rows keep Read/Unread until someone PATCHes a later state. + + `light=True` is the list path: skip resume_text / match_reasoning / + Graph-payload name lookup so we never touch columns the list query defers. """ state = (message.processing_state or "").strip().lower() if state in ("imported", "processed", "rejected"): processing = _PROCESSING_LABEL[state] else: processing = "Read" if message.message_read else "Unread" - return { + payload = { "id": str(message.id), - "name": _sender_name(message), + "name": _sender_name(message, light=light), "email": message.message_from, "position": message.message_subject, "source": message.message_to, @@ -114,11 +127,9 @@ def serialize_application(message: Inbox_Messages, *, linkedin_url=None) -> dict "file_path": message.file_path, "linkedin_slug": message.linkedin_slug or None, "linkedin_url": linkedin_url or None, - "resume_text": message.resume_text, "suggested_job_post_ids": list(message.suggested_job_post_ids or []), "assigned_job_post_id": str(message.assigned_job_post_id) if message.assigned_job_post_id else None, "match_summary": message.match_summary, - "match_reasoning": message.match_reasoning, "match_status": message.match_status, "match_error": message.match_error, "matched_at": message.matched_at.isoformat() if message.matched_at else None, @@ -133,6 +144,10 @@ def serialize_application(message: Inbox_Messages, *, linkedin_url=None) -> dict "processing_state": message.processing_state, "source_channel_id": message.source_channel_id, } + if not light: + payload["resume_text"] = message.resume_text + payload["match_reasoning"] = message.match_reasoning + return payload def serialize_triage(row: Inbox_Message_Triage) -> dict: diff --git a/backend/inbox/views.py b/backend/inbox/views.py index c569990..c9122d2 100644 --- a/backend/inbox/views.py +++ b/backend/inbox/views.py @@ -275,13 +275,13 @@ class Email: async def get_all_applications(self,top,skip,search=None,isread:bool=True,application_status:Candidate_application_Status=Candidate_application_Status.CLOSED,assigned=None,is_duplicate=None,no_suggestions=None,processing_state=None): if application_status in (Candidate_application_Status.PROCESS, Candidate_application_Status.REJECTED, Candidate_application_Status.SCREENING, Candidate_application_Status.ASSESSMENT, Candidate_application_Status.INTERVIEW, Candidate_application_Status.OFFER, Candidate_application_Status.HIRED) or processing_state: - messages=await Inbox_Messages.get_inbox_messages(self.session,top,skip,search,application_status=application_status,assigned=assigned,is_duplicate=is_duplicate,no_suggestions=no_suggestions,processing_state=processing_state) + messages=await Inbox_Messages.get_inbox_messages(self.session,top,skip,search,application_status=application_status,assigned=assigned,is_duplicate=is_duplicate,no_suggestions=no_suggestions,processing_state=processing_state,light=True) elif isread==False: - messages=await Inbox_Messages.get_inbox_messages(self.session,top,skip,search,isread,assigned=assigned,is_duplicate=is_duplicate,no_suggestions=no_suggestions,processing_state=processing_state) + messages=await Inbox_Messages.get_inbox_messages(self.session,top,skip,search,isread,assigned=assigned,is_duplicate=is_duplicate,no_suggestions=no_suggestions,processing_state=processing_state,light=True) else: - messages=await Inbox_Messages.get_inbox_messages(self.session,top,skip,search,assigned=assigned,is_duplicate=is_duplicate,no_suggestions=no_suggestions,processing_state=processing_state) + messages=await Inbox_Messages.get_inbox_messages(self.session,top,skip,search,assigned=assigned,is_duplicate=is_duplicate,no_suggestions=no_suggestions,processing_state=processing_state,light=True) urls=await Inbox.linkedin_urls_by_message_ids(self.session,[m.id for m in messages]) - return [serialize_application(m,linkedin_url=urls.get(m.id)) for m in messages] + return [serialize_application(m,linkedin_url=urls.get(m.id),light=True) for m in messages] async def get_application_by_id(self,record_id): message=await Inbox_Messages.get_inbox_message_by_id(self.session,record_id) diff --git a/frontend/src/screens/Inbox.jsx b/frontend/src/screens/Inbox.jsx index 048c971..48649f7 100644 --- a/frontend/src/screens/Inbox.jsx +++ b/frontend/src/screens/Inbox.jsx @@ -444,11 +444,8 @@ async function fetchApplications(params) { filePath: row.file_path || '', linkedinSlug: row.linkedin_slug || '', linkedinUrl: row.linkedin_url || '', - // resume_text is deliberately DROPPED here. serialize_application ships - // the whole extracted CV on every row, and the queue renders none of - // it — but the cache would then hold a thousand full resumes for the - // hour that LIST_CACHE keeps a page alive. The detail query fetches it - // for the one row actually open, which is the only place it is read. + // resume_text is not on the list payload (serialize_application + // light=True). The detail query fetches it for the open row. atsScore: row.ats_score, phone: row.phone, experience: row.experience, @@ -730,7 +727,7 @@ function BulkReadBar({ rows, selection, onSetRead, onSetAllRead, busy, canEdit, > {n > 0 ? `${n} selected` - : `${total}${unreadCount ? ` · ${unreadCount}` : ''}`} + : `${total}${unreadCount ? ` · ${unreadCount} unread` : ''}`} {/* Same switch as the Per-page "All" entry below, surfaced at the top of the queue where the eye actually is. */} @@ -977,7 +974,13 @@ export default function Inbox() { const activeQuery = isAllChannel ? { - isPending: applicationsQuery.isPending || formQuery.isPending, + // Pending only while NOTHING has arrived. The old OR kept the + // combined query "pending" until the slower source finished, which + // painted six skeletons on top of the email rows that were already + // on screen — the list looked broken on every visit. + isPending: + (applicationsQuery.isPending || formQuery.isPending) + && !(mergedRows && mergedRows.length), // one healthy source still renders; error only when both are down isError: applicationsQuery.isError && formQuery.isError, isSuccess: applicationsQuery.isSuccess && formQuery.isSuccess, From 0eff43def4fb3d794aabf09be58d466389f091cb Mon Sep 17 00:00:00 2001 From: Talha Ahmed Date: Thu, 3 Sep 2026 20:45:35 +0500 Subject: [PATCH 6/8] Candidates: show ATS score, stage and recruiter instead of just an email column The recruiter table was user-centric (GET /candidate/fetch/users), so it could only ever render account fields - name, email, created date. Everything a recruiter actually triages on lives on the application, not the user. Point the table at GET /candidate/fetch and map application rows through a new toApplicationListView, adding Job, ATS (score + band chip), Stage and Recruiter columns. Stage and band become real filters; the dead Department facet is gone. Manual uploads came back unscored because the list path never joined the ATS results, so attach scores there and expose ai_score/recommendation from the manager serializer, deriving the band from the score when the model omitted it. Co-authored-by: Cursor --- backend/job/candidate/serializers.py | 7 + backend/job/candidate/views.py | 11 ++ frontend/candidates-table.test.mjs | 130 +++++++++++++++ frontend/src/api/candidates.js | 52 +++++- frontend/src/screens/Candidates.jsx | 230 ++++++++++++++------------- 5 files changed, 320 insertions(+), 110 deletions(-) create mode 100644 frontend/candidates-table.test.mjs diff --git a/backend/job/candidate/serializers.py b/backend/job/candidate/serializers.py index d2d4c58..8900e6e 100644 --- a/backend/job/candidate/serializers.py +++ b/backend/job/candidate/serializers.py @@ -228,6 +228,11 @@ def serialize_manager_candidate(row, *, source) -> dict: manual_id = row.get("id") if source == "manual" else None job_post_id = row.get("assigned_job_post_id") or row.get("job_post_id") user_id = row.get("user_id") + ats = row.get("ats_result") or {} + score = ats.get("overall_score") + band = (ats.get("band") or "").strip() or None + if score is not None and not band: + band = "Strong Match" if score >= 82 else "Potential Match" if score >= 65 else "Weak Match" return { "id": user_id or (f"inbox:{inbox_id}" if inbox_id is not None else f"manual:{manual_id}"), "user_id": user_id, @@ -240,4 +245,6 @@ def serialize_manager_candidate(row, *, source) -> dict: "manual_upload_candidate_id": str(manual_id) if manual_id else None, "created_at": row.get("created_at"), "source": source, + "ai_score": score, + "recommendation": band, } diff --git a/backend/job/candidate/views.py b/backend/job/candidate/views.py index 124138c..5a079d7 100644 --- a/backend/job/candidate/views.py +++ b/backend/job/candidate/views.py @@ -876,6 +876,9 @@ class CandidateView: "assigned_job_post_id":payload.get("assigned_job_post_id"), "job_posts":payload.get("job_posts") or [], "assigned_job_post":payload.get("assigned_job_post"), + "job_title":payload.get("job_title"), + "recruiter":payload.get("recruiter"), + "recruiter_id":payload.get("recruiter_id"), "source":payload.get("source"), "file_path":payload.get("file_path"), "ai_score":None, @@ -883,6 +886,14 @@ class CandidateView: }) if uid: seen.add(uid) + from inbox.plugins import get_ats_scores_for_users + owners=[p.get("user_id") for p in manual_payloads if p.get("user_id")] + ats=await get_ats_scores_for_users(self.session,owners) + for payload in manual_payloads: + row=ats.get(str(payload.get("user_id") or "")) + if row and row.get("overall_score") is not None: + payload["ai_score"]=row["overall_score"] + payload["recommendation"]=row.get("band") or self._recommendation(row["overall_score"]) return inbox_payloads+manual_payloads except HTTPException: raise diff --git a/frontend/candidates-table.test.mjs b/frontend/candidates-table.test.mjs new file mode 100644 index 0000000..afdb05b --- /dev/null +++ b/frontend/candidates-table.test.mjs @@ -0,0 +1,130 @@ +/** + * Candidates table mapper — score, stage, recruiter, rejected. + * + * node candidates-table.test.mjs + */ +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { pathToFileURL } from 'node:url' + +import esbuild from 'esbuild' + +const outDir = mkdtempSync(join(tmpdir(), 'tf-cand-')) +const outFile = join(outDir, 'candidates.mjs') + +await esbuild.build({ + entryPoints: ['src/api/candidates.js'], + outfile: outFile, + bundle: true, + format: 'esm', + platform: 'node', + target: 'node20', + logLevel: 'error', + define: { 'import.meta.env': JSON.stringify({ VITE_API_BASE: '' }) }, +}) + +const api = await import(pathToFileURL(outFile).href) +const { toApplicationListView } = api + +let failed = 0 +function ok(name, cond, extra) { + if (cond) { + console.log(`ok ${name}`) + if (extra) console.log(` ${extra}`) + } else { + failed += 1 + console.log(`FAIL ${name}`) + if (extra) console.log(` ${extra}`) + } +} + +const scored = toApplicationListView({ + inbox_id: 41, + user_id: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', + name: 'Ada Lovelace', + email: 'ada@example.com', + job_title: 'Backend Engineer', + recruiter: 'Sam Recruiter', + application_status: 'PENDING', + ai_score: 88, + recommendation: 'Strong Match', + created_at: '2026-09-03T10:00:00Z', + source: 'Email', + is_active: true, +}) + +ok('row is application-keyed', scored.id === 'inbox:41', `id=${scored.id}`) +ok('keeps userId for profile navigation', scored.userId === 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa') +ok('job title comes through', scored.jobTitle === 'Backend Engineer') +ok('ATS score is numeric', scored.aiScore === 88) +ok('band is Strong Match', scored.recommendation === 'Strong Match') +ok('PENDING maps to Shortlist', scored.stage === 'Shortlist', `stage=${scored.stage}`) +ok('recruiter name is kept', scored.recruiter === 'Sam Recruiter') + +const rejected = toApplicationListView({ + inbox_id: 42, + user_id: 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb', + name: 'Rejected Candidate', + email: 'r@example.com', + application_status: 'REJECTED', + ai_score: 40, + created_at: '2026-09-01T10:00:00Z', +}) +ok('REJECTED maps to Rejected stage', rejected.stage === 'Rejected', `stage=${rejected.stage}`) +ok('CLOSED also reads as Rejected', toApplicationListView({ + inbox_id: 43, application_status: 'CLOSED', name: 'Closed', +}).stage === 'Rejected') + +const hired = toApplicationListView({ + inbox_id: 44, + application_status: 'HIRED', + name: 'Hired Person', + ai_score: 91, +}) +ok('HIRED maps to Hired', hired.stage === 'Hired') + +const unscored = toApplicationListView({ + manual_upload_candidate_id: 'cccccccc-cccc-cccc-cccc-cccccccccccc', + user_id: 'dddddddd-dddd-dddd-dddd-dddddddddddd', + name: 'No Score Yet', + email: 'ns@example.com', + assigned_job_post: { title: 'Brand Manager' }, + recruiter: null, + application_status: 'SCREENING', +}) +ok('manual row key', unscored.id === 'manual:cccccccc-cccc-cccc-cccc-cccccccccccc') +ok('job falls back to assigned post title', unscored.jobTitle === 'Brand Manager') +ok('missing score stays null, not 0', unscored.aiScore === null) +ok('unscored has no invented band', unscored.recommendation === null) +ok('SCREENING maps to Screening', unscored.stage === 'Screening') +ok('missing recruiter is null so the table can say Unassigned', unscored.recruiter === null) + +const weak = toApplicationListView({ + inbox_id: 45, + name: 'Weak', + ai_score: 50, +}) +ok('score without band derives Weak Match', weak.recommendation === 'Weak Match') + +const potential = toApplicationListView({ + inbox_id: 46, + name: 'Mid', + ai_score: 70, +}) +ok('65–81 derives Potential Match', potential.recommendation === 'Potential Match') + +const enumStatus = toApplicationListView({ + inbox_id: 47, + name: 'Enum', + application_status: { value: 'OFFER' }, +}) +ok('enum-shaped status still maps', enumStatus.stage === 'Offer') + +rmSync(outDir, { recursive: true, force: true }) + +if (failed) { + console.log(`\n${failed} check(s) failed`) + process.exit(1) +} +console.log('\nAll candidates-table mapper checks passed') diff --git a/frontend/src/api/candidates.js b/frontend/src/api/candidates.js index 48aeca4..076d53a 100644 --- a/frontend/src/api/candidates.js +++ b/frontend/src/api/candidates.js @@ -13,7 +13,7 @@ ============================================================ */ import { downloadFile, fetchBlobUrl, request } from '../lib/apiClient' -import { STATUS_FROM_STAGE } from './pipeline' +import { STAGE_FROM_STATUS, STATUS_FROM_STAGE } from './pipeline' /** Active job posts for pickers. Needs job_board.view OR candidates.view. * @@ -227,6 +227,56 @@ export function toCandidateUserView(row) { } } +function statusKey(value) { + if (value == null || value === '') return '' + if (typeof value === 'object' && value.value != null) return String(value.value).toUpperCase() + return String(value).toUpperCase() +} + +function bandOf(score, recommendation) { + if (recommendation) return recommendation + if (score == null || !Number.isFinite(Number(score))) return null + const n = Number(score) + return n >= 82 ? 'Strong Match' : n >= 65 ? 'Potential Match' : 'Weak Match' +} + +/** + * GET /candidate/fetch list row -> the Candidates table. + * + * Application-centric: score, stage, job and recruiter belong to one + * inbox/manual application, not to the user account. + */ +export function toApplicationListView(row) { + const status = statusKey(row.application_status ?? row.stage) + const name = row.name || row.email || 'Unknown' + const jobTitle = row.job_title + || row.assigned_job_post?.title + || (Array.isArray(row.job_posts) ? row.job_posts.find((j) => j?.title)?.title : null) + || null + const rawScore = row.ai_score ?? row.match_score + const aiScore = rawScore == null || rawScore === '' ? null : Number(rawScore) + const score = Number.isFinite(aiScore) ? aiScore : null + return { + id: row.inbox_id != null + ? `inbox:${row.inbox_id}` + : (row.manual_upload_candidate_id + ? `manual:${row.manual_upload_candidate_id}` + : String(row.user_id || row.id || name)), + userId: row.user_id || null, + name, + email: row.email ?? null, + isActive: row.is_active ?? null, + jobTitle, + recruiter: row.recruiter || null, + applicationStatus: status || null, + stage: status ? (STAGE_FROM_STATUS[status] ?? 'Shortlist') : null, + source: row.source || null, + aiScore: score, + recommendation: bandOf(score, row.recommendation || null), + applied: row.created_at ? new Date(row.created_at) : (row.applied ? new Date(row.applied) : null), + } +} + /** * Candidate profiles — the `inbox -> users -> roles` join, restricted server-side * to role_name == CANDIDATE (backend/inbox/models.py:get_candidate_profile). diff --git a/frontend/src/screens/Candidates.jsx b/frontend/src/screens/Candidates.jsx index f0b6972..433cf33 100644 --- a/frontend/src/screens/Candidates.jsx +++ b/frontend/src/screens/Candidates.jsx @@ -1,12 +1,11 @@ /* ============================================================ - Candidates — the scored-candidate pool, on live backend data. + Candidates — applications on live backend data. - Rows come from GET /candidate/fetch (all jobs) via the shared - toCandidateView mapper. Facets, columns and actions that had no backing - column (stage, recruiter, notice period, favourites…) are gone rather than - rendered as placeholders — the Inbox screen set that precedent. Adding - candidates happens through CV Import or the Add Candidate modal below — - both run the CV through the same persisted ATS scoring pipeline. + Recruiter rows come from GET /candidate/fetch (inbox + manual), one row + per application, so score / stage / job / recruiter have a source. + Hiring managers use GET /candidate/manager/fetch (jobs on their + requisitions). Adding a candidate still goes through CV Import or the + Add Candidate modal — both run the CV through persisted ATS scoring. ============================================================ */ import { useCallback, useEffect, useMemo, useRef, useState } from 'react' @@ -16,7 +15,7 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import Modal from '../ui/Modal' import DataTable, { DataTableHead, DEFAULT_PAGE_SIZE, Pagination, pageWindow, useDataTable } from '../ui/DataTable' import PageHeader from '../ui/PageHeader' -import { Avatar, Badge, EmptyState, FieldError, Icon, SkeletonRows } from '../ui/primitives' +import { Avatar, Badge, EmptyState, FieldError, Icon, ScoreChip, SkeletonRows } from '../ui/primitives' import { useToast } from '../ui/Toast' import { useAuth } from '../auth/AuthContext' import { isHiringManager } from '../auth/permissions' @@ -24,7 +23,6 @@ import CandidateProfile from './CandidateProfile' import { useJobTitles } from './ScoredCandidateProfile' import { qk } from '../lib/queryKeys' import { exportStyledXlsx } from '../lib/exportXlsx' -import { formatRole } from '../lib/format' import { friendlyAuthError } from '../lib/errors' import * as candidatesApi from '../api/candidates' import * as jobPostsApi from '../api/jobPosts' @@ -33,42 +31,30 @@ import { useFormState } from '../components/AuthLayout' import { persist, useSeedMutation } from '../data/seedQueries' import { avatarColor, fmtDate, initials as initialsOf, sources, stages } from '../data/seed' -const EMPTY_FILTERS = { account: '', department: '' } +const EMPTY_FILTERS = { account: '', stage: '', band: '' } +const SEARCH_DEBOUNCE_MS = 300 +const STAGE_FILTERS = ['Shortlist', 'Screening', 'Assessment', 'Interview', 'Offer', 'Approved', 'Hired', 'On Hold', 'Rejected'] +const BAND_FILTERS = ['Strong Match', 'Potential Match', 'Weak Match', 'Unscored'] +const BAND_BADGE = { + 'Strong Match': 'b-green', + 'Potential Match': 'b-amber', + 'Weak Match': 'b-gray', +} /** Same ladder Talent Pool uses for Advance Stage on the shared profile modal. */ const STAGE_ORDER = ['Shortlist', 'Screening', 'Assessment', 'Interview', 'Offer', 'Approved', 'Hired'] -/** Seeded `candidate` role is id 8; id 4 is hiring_manager (signup default). */ -const CANDIDATE_ROLE_ID = 8 - -/* Rows are candidate USER accounts (GET /candidate/fetch/users?role_id=8), not - rows of the scored `candidates` table. - - Why: /candidate/scored/fetch only ever returns CVs that have been through the - ATS, so the pool was empty for every candidate who has an account but no score - yet. The user list is the real population; the score is an attribute some of - them have. - - The consequence is that the ATS columns have no source on this screen — see - toCandidateUserView. Open a candidate to get their score, which the shared - Talent Pool profile modal (CandidateProfile.jsx) reads live by userId. */ -async function fetchCandidates({ top = DEFAULT_PAGE_SIZE, skip = 0 } = {}) { - const [usersRes, appsRes] = await Promise.all([ - candidatesApi.listCandidateUsers({ roleId: CANDIDATE_ROLE_ID, top, skip }), - candidatesApi.list({ limit: 100 }).catch(() => null), - ]) - const rows = Array.isArray(usersRes?.data) ? usersRes.data : [] - const sourceByUser = new Map() - for (const app of Array.isArray(appsRes?.data) ? appsRes.data : []) { - const uid = app.user_id - if (!uid || sourceByUser.has(uid)) continue - if (app.source) sourceByUser.set(String(uid), app.source) - } - return rows.map((row) => { - const view = candidatesApi.toCandidateUserView(row) - const source = sourceByUser.get(String(view.userId)) - return source ? { ...view, source } : view +async function fetchCandidates({ limit = DEFAULT_PAGE_SIZE, offset = 0, search } = {}) { + const res = await candidatesApi.list({ + limit, + offset, + search: search || undefined, }) + const rows = Array.isArray(res?.data) ? res.data : [] + return { + rows: rows.map(candidatesApi.toApplicationListView), + total: Number(res?.total ?? rows.length) || 0, + } } async function fetchJobs() { @@ -78,10 +64,31 @@ async function fetchJobs() { } function recommendationOf(c) { - if (c.aiScore == null) return 'Weak Match' + if (c.recommendation) return c.recommendation + if (c.aiScore == null) return null return c.aiScore >= 82 ? 'Strong Match' : c.aiScore >= 65 ? 'Potential Match' : 'Weak Match' } +function stageOf(status) { + const key = String(status || '').toUpperCase() + return pipelineApi.STAGE_FROM_STATUS[key] ?? (key ? 'Shortlist' : null) +} + +function AtsCell({ score, recommendation }) { + if (score == null) return Not scored + const band = recommendation || recommendationOf({ aiScore: score }) + return ( +
+ + {band && ( +
+ {band} +
+ )} +
+ ) +} + /* Client-side guard only — the route has no size cap of its own, so this just stops an obviously wrong file from being read into memory and posted. */ const MAX_CV_MB = 10 @@ -175,14 +182,20 @@ function HiringManagerCandidates() { sortValue: (r) => r.job_title || '', render: (r) => r.job_title || '—', }, + { + key: 'score', + label: 'ATS', + sortable: true, + sortValue: (r) => (r.ai_score == null ? -1 : Number(r.ai_score)), + render: (r) => , + }, { key: 'stage', label: 'Stage', sortable: true, sortValue: (r) => r.application_status || '', render: (r) => { - const status = String(r.application_status || '').toUpperCase() - const stage = pipelineApi.STAGE_FROM_STATUS[status] ?? 'Shortlist' + const stage = stageOf(r.application_status) || 'Shortlist' return {stage} }, }, @@ -245,6 +258,7 @@ function RecruiterCandidates() { const updateCandidates = useSeedMutation('candidates') const [q, setQ] = useState('') + const [search, setSearch] = useState('') const [filters, setFilters] = useState(EMPTY_FILTERS) const [showFilters, setShowFilters] = useState(false) const [sortMode, setSortMode] = useState('recent') @@ -254,27 +268,18 @@ function RecruiterCandidates() { const [atsFor, setAtsFor] = useState(null) const [adding, setAdding] = useState(false) - const countQuery = useQuery({ - queryKey: qk.candidates.count({ roleId: CANDIDATE_ROLE_ID }), - queryFn: async () => { - const res = await candidatesApi.countCandidateUsers({ roleId: CANDIDATE_ROLE_ID }) - return typeof res?.total === 'number' ? res.total : (res?.data?.total ?? 0) - }, - staleTime: Infinity, - }) + useEffect(() => { + const t = setTimeout(() => setSearch(q.trim()), SEARCH_DEBOUNCE_MS) + return () => clearTimeout(t) + }, [q]) + useEffect(() => { setSkip(0) }, [search]) + const candidatesQuery = useQuery({ - queryKey: qk.candidates.list({ top: pageSize, skip }), - queryFn: () => fetchCandidates({ top: pageSize, skip }), + queryKey: qk.candidates.list({ limit: pageSize, offset: skip, search }), + queryFn: () => fetchCandidates({ limit: pageSize, offset: skip, search }), }) const jobsQuery = useQuery({ queryKey: qk.jobPosts.list(), queryFn: fetchJobs }) - const deptsQuery = useQuery({ - queryKey: qk.jobPosts.departments(), - queryFn: async () => { - const res = await jobPostsApi.listDepartments() - return Array.isArray(res?.data) ? res.data : [] - }, - }) - const candidates = useMemo(() => candidatesQuery.data ?? [], [candidatesQuery.data]) + const candidates = useMemo(() => candidatesQuery.data?.rows ?? [], [candidatesQuery.data]) const jobsById = useMemo( () => Object.fromEntries((jobsQuery.data ?? []).map((j) => [j.id, j])), [jobsQuery.data], @@ -287,7 +292,7 @@ function RecruiterCandidates() { }) const jobTitleOf = useCallback( - (c) => jobsById[c.jobId]?.title ?? '—', + (c) => c.jobTitle || jobsById[c.jobId]?.title || '—', [jobsById], ) @@ -301,9 +306,6 @@ function RecruiterCandidates() { }) const atsScore = scoreQuery.data?.overall_score ?? null - /* The relevance blend (score + matched-skill ratio + recency) went with the - scoring columns — none of its three inputs exists on a users row. */ - const openProfile = useCallback( (c) => { qc.setQueryData(qk.seed.recentlyViewed(), (old = []) => { @@ -313,7 +315,7 @@ function RecruiterCandidates() { }) // Real candidates get the full profile PAGE; the modal stays only as the // fallback for rows without a user account. - const uid = c.userId || c.id + const uid = c.userId if (uid) navigate(`/candidate/${uid}`) else setProfileFor(c) }, @@ -333,39 +335,35 @@ function RecruiterCandidates() { let list = candidates.filter((c) => { if (f.account === 'Active' && !c.isActive) return false if (f.account === 'Unconfirmed' && c.isActive) return false - if (q) { - // Client-side: the route accepts `search` but never forwards it to the - // service layer, so asking the server to filter would be a silent no-op. - const term = q.toLowerCase() - const hay = [ - c.name, c.email ?? '', c.filename ?? '', c.currentTitle ?? '', - c.currentCompany ?? '', c.matchedSkills.join(' '), - ].join(' ').toLowerCase() - if (!hay.includes(term)) return false - } + if (f.stage && (c.stage || '') !== f.stage) return false + if (f.band === 'Unscored' && c.aiScore != null) return false + if (f.band && f.band !== 'Unscored' && recommendationOf(c) !== f.band) return false return true }) if (sortMode === 'name') list = [...list].sort((a, b) => a.name.localeCompare(b.name)) + else if (sortMode === 'score') { + list = [...list].sort((a, b) => (b.aiScore ?? -1) - (a.aiScore ?? -1)) + } else { + list = [...list].sort((a, b) => (b.applied?.getTime() ?? 0) - (a.applied?.getTime() ?? 0)) + } return list - }, [candidates, filters, q, sortMode]) + }, [candidates, filters, sortMode]) - /* Columns follow the row source. A `users` row carries identity only, so the - four scoring columns (Scored For / Exp / Relevance / ATS) have nothing to - read and are gone rather than rendered as permanent em-dashes — the same - rule the Inbox screen set and this file's header states. They come back the - moment the rows carry a score again. */ const columns = useMemo( () => [ { key: 'name', label: 'Candidate', sortable: true }, - { key: 'email', label: 'Email', sortable: true }, + { key: 'jobTitle', label: 'Job', sortable: true }, + { key: 'aiScore', label: 'ATS', sortable: true }, + { key: 'stage', label: 'Stage', sortable: true }, + { key: 'recruiter', label: 'Recruiter', sortable: true }, { key: 'applied', label: 'Added', sortable: true }, ], [], ) const t = useDataTable({ columns, rows, pageSize: Math.max(rows.length, 1) }) - const total = countQuery.data ?? 0 + const total = candidatesQuery.data?.total ?? 0 const pages = Math.max(1, Math.ceil(total / pageSize)) const from = total ? skip + 1 : 0 const to = total ? skip + rows.length : 0 @@ -381,13 +379,11 @@ function RecruiterCandidates() { .map((id) => candidates.find((c) => c.id === id)) .filter(Boolean) - const setFilter = (k, v) => setFilters((f) => ({ ...f, [k]: v })) + const setFilter = (k, v) => { + setFilters((f) => ({ ...f, [k]: v })) + setSkip(0) + } - /* The gate is the ACCOUNT, not `scoringStatus`. Rows here are `users` rows and - toCandidateUserView leaves scoringStatus null by construction, so checking it - rejected every candidate on the screen and the ATS Match button only ever - toasted. Whether a score exists is the modal's own question — it resolves - that from ats_results, which the row cannot know about. */ function openAts(c) { if (!c.userId) { toast('This candidate has no account to look a score up against', 'info') @@ -438,7 +434,7 @@ function RecruiterCandidates() {
{total} candidate account{total === 1 ? '' : 's'} · role_id {CANDIDATE_ROLE_ID}} + sub={<>{total} application{total === 1 ? '' : 's'}} actions={<>
@@ -522,11 +525,9 @@ function RecruiterCandidates() { className="filter-panel" style={{ display: 'grid', padding: '16px 0', borderTop: '1px solid var(--border)', marginTop: 12 }} > - {/* Job / Matched Skill / Source / ATS Score / scoring Status are gone - with the scoring columns: on a users row every one of them would - match nothing and silently empty the table. */} - setFilter('account', v)} any="Any Account" options={['Active', 'Unconfirmed']} /> - setFilter('department', v)} any="All Departments" options={deptsQuery.data ?? []} /> + setFilter('stage', v)} any="Any stage" options={STAGE_FILTERS} /> + setFilter('band', v)} any="Any band" options={BAND_FILTERS} /> + setFilter('account', v)} any="Any account" options={['Active', 'Unconfirmed']} />
)}
@@ -553,8 +554,8 @@ function RecruiterCandidates() { {t.pageRows.length === 0 ? ( - - Score resumes in CV Import to fill this table. + + Import a CV or add a candidate to see score, stage, and recruiter on this table. @@ -575,12 +576,23 @@ function RecruiterCandidates() { Form )} -
{formatRole(c.roleName) || '—'}
+
{c.email || '—'}
- {c.email ?? '—'} + {c.jobTitle || '—'} + + + + + + {c.stage + ? {c.stage} + : } + + + {c.recruiter || 'Unassigned'} From 8dcd262dc469d39fe4c3078a4890781df32406bc Mon Sep 17 00:00:00 2001 From: Talha Ahmed Date: Fri, 4 Sep 2026 15:29:05 +0500 Subject: [PATCH 7/8] Repair CVs that pypdf extracts one character per line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reported as "LinkedIn is not showing though the resume has it". The LinkedIn was never the problem. Traced on the live application (Mohammad Raza, inbox row 2f81cebc). Its stored resume_text is 4,555 characters over 2,278 lines, and every one of those lines is exactly one character long. The CV really does say LinkedIn:linkedin.com/in/mohammad-raza-digital-marketer but it is stored as forty separate lines, so nothing that looks for a substring can find it. Not a link annotation, not an image, not OCR: pypdf's default mode breaks after every glyph on PDFs whose author positioned each one separately, which design tools do routinely. It survived review because a model reads that text fine. The candidate was classified, matched and scored normally. What fails, silently, is every check that asks "does this string appear in the resume": - slugs_from_text finds no profile, so linkedin_slug is stored empty - _clean_skills drops every skill, since each must appear in the text - the company and education clamps drop theirs for the same reason - verify_matched_keywords drops every matched keyword in the ATS engine despace_line could not help: it rebuilds glyphs padded *within* a line, and here there is nothing left on a line to rebuild. is_glyph_fragmented measures the giveaway — the share of non-empty lines that are a single character — and extract_pdf_text re-extracts with pypdf's layout mode when it trips. Layout mode is the fallback, never the default: it is slower and pads ordinary documents with alignment whitespace, so a CV that extracts cleanly today is untouched. The fallback is checked before it is trusted; fragmented text still scores a candidate, empty text fails them. Both extractors had the defect, so the helpers live in app/services/pdf.py, which owns PDF handling and is already imported by the recruiting path. Measured against that real CV, before and after: slugs_from_text [] -> ['mohammad-raza-digital-marketer'] profile_url_from_text None -> https://www.linkedin.com/in/... lines 2278 -> 61 single-char lines 2278 -> 0 'performance' found False -> True 'google ads' found False -> True Existing rows keep their broken text; extraction runs at ingest. Re-running the match on affected rows is what backfills them. .gitignore had `tests/**` twice and `/backend/tests/**` once. Both suites are tracked and both run in CI, so the rules were inert for existing files and did nothing but swallow new ones — this test was invisible to `git status` until they went. That is also why they are removed rather than negated. Co-Authored-By: Claude Opus 5 (1M context) --- .gitignore | 9 ++- app/services/pdf.py | 77 ++++++++++++++++++++-- backend/job/candidate/plugins.py | 5 ++ backend/job/candidate/views.py | 7 +- tests/unit/test_pdf_fragmentation.py | 97 ++++++++++++++++++++++++++++ 5 files changed, 183 insertions(+), 12 deletions(-) create mode 100644 tests/unit/test_pdf_fragmentation.py diff --git a/.gitignore b/.gitignore index 27df84e..bffe937 100644 --- a/.gitignore +++ b/.gitignore @@ -71,7 +71,6 @@ Utopia-ai-hr-ats-portal 1.pem # Local-only Compose overrides (never deployed) docker.local.env -tests/** **/.env** # Paper form source documents (Annexure A/E/J) — reference material, not code. @@ -81,8 +80,12 @@ frontend/dist/** */ docker.local.frontend/dist/** */ frontend/dist/index.html frontend/dist/index.html -tests/** -/backend/tests/** +# `tests/**` and `/backend/tests/**` used to sit here. Both test suites are +# tracked and both are run by scripts/ci-checks.sh, so the rules were inert for +# the files that already existed and did nothing but silently swallow NEW ones: +# a test added to either suite never showed up in `git status`, and CI ran a +# suite that did not include it. Removed rather than negated, because there is +# nothing under either path that should be ignored. frontend/dist/** nginx.conf smoke.test.mjs \ No newline at end of file diff --git a/app/services/pdf.py b/app/services/pdf.py index 822f3de..e3afdf4 100644 --- a/app/services/pdf.py +++ b/app/services/pdf.py @@ -12,6 +12,7 @@ import re import uuid from dataclasses import dataclass from pathlib import PurePosixPath, PureWindowsPath +from typing import Literal from pypdf import PdfReader @@ -82,6 +83,55 @@ def _normalize_text(text: str) -> str: return text.strip() +def is_glyph_fragmented(text: str | None, *, min_lines: int = 20, ratio: float = 0.4) -> bool: + """True when pypdf emitted one character per line instead of words. + + Design tools that position every glyph separately (Canva, InDesign and + friends) make pypdf's default mode break after each one, so a CV reading + "LinkedIn: linkedin.com/in/jane" arrives as thirty single-character lines. + A model reads that fine, which is why it hides: what breaks is every + substring check downstream. ``verify_matched_keywords`` drops every keyword, + and on the recruiting side the LinkedIn scan and the skills, company and + education clamps all return nothing, silently. + + ``min_lines`` stops a two-line PDF or a near-empty page from tripping the + check on a handful of legitimately short lines. + """ + lines = [ln.strip() for ln in (text or "").splitlines() if ln.strip()] + if len(lines) < min_lines: + return False + singles = sum(1 for ln in lines if len(ln) == 1) + return singles / len(lines) >= ratio + + +def extract_pdf_text(reader: PdfReader) -> str: + """Page text from a reader, repaired when the default mode shatters it. + + Default mode first: it is faster and already correct for ordinary CVs. + Layout mode is the fallback, never the default -- it rebuilds the page from + glyph coordinates, which recovers word and line structure on a fragmented + file but is slower and pads ordinary documents with alignment whitespace. + Reaching for it only when the default output is measurably broken means a + CV that extracts cleanly today keeps extracting exactly as it does now. + + The fallback is checked before it is trusted: if layout mode comes back + fragmented too, or empty, the default text is kept. Fragmented text still + scores a candidate; empty text fails them outright. + """ + default = "\n".join((page.extract_text() or "") for page in reader.pages) + if not is_glyph_fragmented(default): + return default + try: + layout = "\n".join( + (page.extract_text(extraction_mode="layout") or "") for page in reader.pages + ) + except Exception: # older pypdf, or a page layout mode chokes on + return default + if not layout.strip() or is_glyph_fragmented(layout): + return default + return layout + + def _truncate(text: str, max_chars: int) -> tuple[str, bool]: """Cut at a line boundary near the limit rather than mid-word.""" if len(text) <= max_chars: @@ -120,13 +170,26 @@ def extract_resume(data: bytes, filename: str, max_chars: int) -> ExtractedResum if not pages: raise InvalidPDFError("document has no pages") - page_texts: list[str] = [] - for page in pages: - try: - raw_text = page.extract_text() or "" - except Exception: # a single bad page must not sink the whole document - raw_text = "" - page_texts.append(_normalize_text(raw_text)) + def _pages_in_mode(mode: Literal["plain", "layout"]) -> list[str]: + out: list[str] = [] + for page in pages: + try: + raw = page.extract_text(extraction_mode=mode) or "" + except Exception: # a single bad page must not sink the whole document + raw = "" + out.append(_normalize_text(raw)) + return out + + page_texts = _pages_in_mode("plain") + # The same repair extract_pdf_text performs, but page by page, because the + # page markers below need the split preserved. The whole document is judged + # together and then every page is re-extracted in one mode, so a document + # cannot end up half in each. + if is_glyph_fragmented("\n".join(page_texts)): + repaired = _pages_in_mode("layout") + joined = "\n".join(repaired) + if joined.strip() and not is_glyph_fragmented(joined): + page_texts = repaired body = "\n".join(chunk for chunk in page_texts if chunk) if len(body) < _MIN_USABLE_CHARS or not _ALPHANUMERIC.search(body): diff --git a/backend/job/candidate/plugins.py b/backend/job/candidate/plugins.py index 606984b..cdcf3ec 100644 --- a/backend/job/candidate/plugins.py +++ b/backend/job/candidate/plugins.py @@ -19,6 +19,11 @@ from pathlib import Path from app.core.config import Settings, get_settings from app.services.llm import OpenAIScorer +# One definition, two extractors. The bulk-ATS engine and this recruiting path +# both read CVs with pypdf and both broke the same way on glyph-fragmented +# files, so the repair lives in the package that owns PDF handling and is +# re-exported here for the callers that import it from this module. +from app.services.pdf import extract_pdf_text, is_glyph_fragmented # noqa: F401 from dotenv import load_dotenv from job.candidate.decorators import despace_line, normalize_unicode diff --git a/backend/job/candidate/views.py b/backend/job/candidate/views.py index 5a079d7..1e96a75 100644 --- a/backend/job/candidate/views.py +++ b/backend/job/candidate/views.py @@ -19,6 +19,7 @@ from job.candidate.plugins import ( contained_download_path, documents_from_message, extract_pdf_link_uris, + extract_pdf_text, get_scorer, get_scoring_settings, normalize_spaced_text, @@ -125,8 +126,10 @@ class FileRead: reader = PdfReader(io.BytesIO(self.file)) if reader.is_encrypted: raise HTTPException(400, "PDF is password protected") - pages = [(page.extract_text() or "") for page in reader.pages] - text = normalize_spaced_text("\n".join(pages)) + # extract_pdf_text, not page.extract_text() directly: some CVs come + # out of pypdf one character per line, which reads fine to the LLM + # but defeats every substring check downstream. See its docstring. + text = normalize_spaced_text(extract_pdf_text(reader)) # Icon-only LinkedIn buttons never appear in extract_text(); the # URL is on the annotation. Append so the employment agent can # return linkedin_url as its own parsed key. diff --git a/tests/unit/test_pdf_fragmentation.py b/tests/unit/test_pdf_fragmentation.py new file mode 100644 index 0000000..0b573ec --- /dev/null +++ b/tests/unit/test_pdf_fragmentation.py @@ -0,0 +1,97 @@ +"""Glyph-fragmented CVs must not silently lose every substring check. + +The defect these cover, found on a live application: a CV whose PDF positions +each glyph separately came out of pypdf's default mode as one character per +line. The text was all there, so the LLM read it and scored the candidate +fine — but `linkedin.com/in/...` was spelled across forty lines, so the +LinkedIn scan found nothing, and so did the skills, company and education +clamps, which all ask whether a string appears in the resume. + +No real PDF and no pypdf call: a stub reader returns canned page text, which +is the only input the function under test actually reads. +""" + +from __future__ import annotations + +import pytest + +from app.services.pdf import extract_pdf_text, is_glyph_fragmented + +# The real shape of the failure, taken from the CV that exposed it. +FRAGMENTED = "\n".join("LinkedIn:linkedin.com/in/mohammad-raza-digital-marketer") +REPAIRED = ( + "MOHAMMAD RAZA\n" + "Performance Marketing Specialist\n" + "Karachi, Pakistan | +923362837939\n" + "LinkedIn:linkedin.com/in/mohammad-raza-digital-marketer\n" +) +ORDINARY = "\n".join(f"Line {i} of an ordinary resume with real words on it." for i in range(30)) + + +class _Page: + """Minimal pypdf page: default text, and optionally a layout-mode variant.""" + + def __init__(self, default, layout=None, layout_raises=False): + self._default = default + self._layout = layout + self._layout_raises = layout_raises + + def extract_text(self, extraction_mode="plain"): + if extraction_mode == "layout": + if self._layout_raises: + raise ValueError("layout mode unsupported") + return self._layout + return self._default + + +class _Reader: + def __init__(self, *pages): + self.pages = list(pages) + + +class TestIsGlyphFragmented: + def test_one_character_per_line_is_fragmented(self): + assert is_glyph_fragmented(FRAGMENTED) is True + + def test_ordinary_text_is_not(self): + assert is_glyph_fragmented(ORDINARY) is False + + def test_short_input_never_trips_it(self): + # A two-line PDF of initials is not evidence of a broken extractor, and + # treating it as such would send every tiny document through layout mode. + assert is_glyph_fragmented("A\nB\nC") is False + + @pytest.mark.parametrize("text", ["", None]) + def test_empty_is_not_fragmented(self, text): + assert is_glyph_fragmented(text) is False + + +class TestExtractPdfText: + def test_ordinary_pdf_is_returned_untouched(self): + # The guarantee that matters most: a CV that extracts cleanly today must + # keep extracting byte-identically, never routed through layout mode. + reader = _Reader(_Page(ORDINARY, layout="LAYOUT SHOULD NOT BE USED")) + assert extract_pdf_text(reader) == ORDINARY + + def test_fragmented_pdf_falls_back_to_layout(self): + reader = _Reader(_Page(FRAGMENTED, layout=REPAIRED)) + out = extract_pdf_text(reader) + assert out == REPAIRED + assert "linkedin.com/in/mohammad-raza-digital-marketer" in out.lower() + + def test_layout_that_is_also_fragmented_is_rejected(self): + reader = _Reader(_Page(FRAGMENTED, layout=FRAGMENTED)) + assert extract_pdf_text(reader) == FRAGMENTED + + def test_empty_layout_is_rejected(self): + # Fragmented text still scores a candidate. Empty text fails them outright. + reader = _Reader(_Page(FRAGMENTED, layout=" ")) + assert extract_pdf_text(reader) == FRAGMENTED + + def test_layout_mode_unsupported_falls_back_to_default(self): + reader = _Reader(_Page(FRAGMENTED, layout_raises=True)) + assert extract_pdf_text(reader) == FRAGMENTED + + def test_pages_are_joined(self): + reader = _Reader(_Page("page one text here"), _Page("page two text here")) + assert extract_pdf_text(reader) == "page one text here\npage two text here" From 5b29e2b59274d70bc05d2c6db66d21fcbcc90e62 Mon Sep 17 00:00:00 2001 From: Talha Ahmed Date: Mon, 7 Sep 2026 13:39:20 +0500 Subject: [PATCH 8/8] CV Bank: replace Talent Pool with a searchable, ranked bank of stored CVs The bank used to be write-only: a CV uploaded with no job carried only its full text, so nothing could search or rank it. Now the employment agent's extraction (title, company, education, plus new skills and years_experience, both clamped to what the resume actually states) is stored on the row, and the bank is ranked against a job the moment that job opens. - New CV Bank screen at /cvbank replaces Talent Pool; the inline bank card moves out of CV Import. One table, two populations: speculative uploads, and silver medalists (rejected applicants scoring >= CV_BANK_SILVER_FLOOR, read live from their application rather than copied). - matching/ranking.py: the tier-1 keyword ranker moves out of talent/plugins.py so Find Talent and the bank share one implementation; talent/plugins.py re-exports it and its numbers are unchanged. - Taskiq tasks in job.candidate.bank_tasks: backfill profiles for CVs banked before extraction existed, and rank the bank when a job opens so recruiters are told about matches above CV_BANK_SUGGEST_THRESHOLD. Retention (CV_BANK_RETENTION_MONTHS) is stamped on the row at upload; the sweep flags expired rows and never deletes. - Migrations 029 (bank profile columns) and 030 (per-job bank matches). - Routes: POST /candidate/cv-bank/score, GET /candidate/cv-bank/suggestions. - README: The CV Bank, plus the retention and deletion policy. Also in this change: - Inbox, Sheet Forms: has_linkedin / has_resume filters, tri-valued so "no link" is a real filter and NULL rows are kept in it; tab badge counts now narrow with the list and the search box. - Hiring-manager candidate rows carry the ATS score and band. - Tests: analytics dashboard merge logic, employment extraction clamps, form-data filters, manager candidate serializer, CV Bank mapper. Co-Authored-By: Claude Fable 5.1 --- backend/.env.example | 12 + backend/README.md | 65 +- backend/employment_agent/decorators.py | 65 +- backend/employment_agent/execute_agent.py | 2 + backend/employment_agent/prompt.py | 34 +- backend/g_sheet/app.py | 17 +- backend/g_sheet/models.py | 70 +- backend/g_sheet/views.py | 18 +- backend/inbox/models.py | 80 +++ backend/job/app.py | 109 ++- backend/job/candidate/bank_tasks.py | 215 ++++++ backend/job/candidate/models.py | 180 ++++- backend/job/candidate/serializers.py | 85 +++ backend/job/candidate/views.py | 232 +++++++ backend/job/job_post/views.py | 25 + backend/matching/__init__.py | 5 + backend/matching/ranking.py | 121 ++++ .../migrations/manual/029_cv_bank_profile.sql | 30 + .../migrations/manual/030_cv_bank_matches.sql | 25 + backend/talent/plugins.py | 85 +-- backend/taskiq_management/broker_setup.py | 2 +- backend/tests/test_analytics_dashboard.py | 98 +++ backend/tests/test_cv_bank_ranking.py | 133 ++++ backend/tests/test_employment_agent.py | 51 +- .../test_employment_extraction_clamps.py | 165 +++++ backend/tests/test_form_data_filters.py | 108 +++ .../tests/test_manager_candidate_serialize.py | 58 ++ frontend/cvbank.test.mjs | 166 +++++ frontend/dist/index.html | 4 +- frontend/inbox-loading.test.mjs | 105 ++- frontend/mobile.test.mjs | 2 +- frontend/package.json | 4 +- frontend/src/App.jsx | 2 +- frontend/src/__smoke__/entry.jsx | 27 +- frontend/src/api/candidates.js | 117 +++- frontend/src/api/sheet.js | 25 +- frontend/src/app/routes.js | 7 +- frontend/src/auth/permissions.js | 2 +- frontend/src/lib/queryKeys.js | 3 +- frontend/src/screens/Candidates.jsx | 4 +- frontend/src/screens/CvBank.jsx | 639 ++++++++++++++++++ frontend/src/screens/CvImport.jsx | 176 +---- frontend/src/screens/Inbox.jsx | 122 +++- .../src/screens/ScoredCandidateProfile.jsx | 2 +- frontend/src/screens/TalentPool.jsx | 403 ----------- frontend/src/styles/styles.css | 29 + 46 files changed, 3215 insertions(+), 714 deletions(-) create mode 100644 backend/job/candidate/bank_tasks.py create mode 100644 backend/matching/__init__.py create mode 100644 backend/matching/ranking.py create mode 100644 backend/migrations/manual/029_cv_bank_profile.sql create mode 100644 backend/migrations/manual/030_cv_bank_matches.sql create mode 100644 backend/tests/test_analytics_dashboard.py create mode 100644 backend/tests/test_cv_bank_ranking.py create mode 100644 backend/tests/test_employment_extraction_clamps.py create mode 100644 backend/tests/test_form_data_filters.py create mode 100644 backend/tests/test_manager_candidate_serialize.py create mode 100644 frontend/cvbank.test.mjs create mode 100644 frontend/src/screens/CvBank.jsx delete mode 100644 frontend/src/screens/TalentPool.jsx diff --git a/backend/.env.example b/backend/.env.example index e7890d6..d1c83cf 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -112,6 +112,18 @@ TASKIQ_IDLE_TIMEOUT_MS=600000 MANUAL_UPLOAD_TO_ADDRESS=manual-cv-upload@hr-ats.local APP_VERSION=dev +# CV Bank. Retention is stamped on the row at upload, so raising this later does +# not extend CVs already taken in. The sweep flags expired entries; it never +# deletes. Leave the notify address blank to keep the log line only. +CV_BANK_RETENTION_MONTHS=24 +CV_BANK_RETENTION_CRON=0 3 * * * +CV_BANK_RETENTION_NOTIFY_EMAIL= +# Tier-1 rank (free keyword overlap) a banked CV must clear to notify a recruiter +# when a job opens; and the ATS score a rejected applicant needs to count as a +# silver medalist. +CV_BANK_SUGGEST_THRESHOLD=55 +CV_BANK_SILVER_FLOOR=60 + # Compose host ports (docker compose --env-file ./backend/.env …). FRONTEND_PORT=5173 BACKEND_PORT=8000 diff --git a/backend/README.md b/backend/README.md index 34f5f14..d238b48 100644 --- a/backend/README.md +++ b/backend/README.md @@ -593,6 +593,9 @@ Taskiq over **Redis Streams**, with a result backend and a Redis-backed schedule | `inbox.match_message` (CV broker) | enqueued by `/candidate/cv_upload` onto the `cv_upload` stream | Same matcher as above; isolated so uploads never sit behind `/email/fetch` backlog | | `inbox.score_message` | enqueued by `PATCH /inbox/{id}/assign-job-post` onto the `inbox` stream | ATS-score one message against one job. Idempotent — a completed (message, job) pair returns `already_scored` without paying for a second call | | `inbox.sync_read_status` | cron, `EMAIL_SYNC_CRON` (default every minute) | Pull read-status deltas from the Email API and apply them | +| `cvbank.rank_for_job` | enqueued by `POST /job/post-job` after `insert_job_post` | Tier-1 rank every banked CV against the new job into `cv_bank_matches`, then notify the recruiter if any clears `CV_BANK_SUGGEST_THRESHOLD` | +| `cvbank.backfill_profiles` | manual, one-off | Extract skills/title/company/years for CVs banked before migration 029. Re-runnable; returns `remaining` so it can be enqueued in batches | +| `cvbank.sweep_expired` | cron, `CV_BANK_RETENTION_CRON` (default `0 3 * * *`) | Flag bank CVs past `bank_expires_at` for review. Flags only — it never deletes | | `ping` | manual | Framework smoke test | **Auto-scoring never fails a match.** `match_inbox_message` commits the agent result first, @@ -616,6 +619,55 @@ un-reading a mail in Outlook no longer propagates here. `sync_read_status` holds (`inbox:sync_read_status:lock`, 300s TTL) so overlapping cron ticks cannot double-run, and pages at most 10 rounds per tick. +**Ranking on job creation is fire-and-forget.** `JobPost._rank_cv_bank` swallows broker +errors: the job post is already committed, and Redis being down must not turn a successful +creation into a 500. The CV Bank screen recomputes any missing rank on read, so a dropped +enqueue degrades to a slower page rather than a wrong one. + +--- + +## The CV Bank + +Two populations behind one screen and one endpoint (`GET /candidate/cv-bank/fetch`): + +| Source | Where it lives | How it got there | +|---|---|---| +| `speculative` | `manual_upload_candidate` with `apply_via='cv_bank'`, `job_post_id IS NULL` | `POST /candidate/cv-bank/upload` — a CV with no job | +| `silver_medalist` | `inbox_messages` + `ats_results`, read live | Applied, scored at or above `CV_BANK_SILVER_FLOOR`, `application_status='REJECTED'` | + +Silver medalists are a **union query, not a copy**. The application rows keep being the +source of truth, so there is no sync to get wrong. Only `REJECTED` qualifies — `CLOSED` is +the ingest default for unprocessed mail, and treating it as a rejection would tip the whole +unread inbox into the bank. + +**Two-tier matching.** `matching/ranking.py::rank_profile` is deterministic keyword overlap, +free, and runs over the entire bank whenever a job opens. The real ATS score costs money and +runs only from `POST /candidate/cv-bank/score`, per row, on the ones a recruiter picks. The +UI draws them differently on purpose — a rank is not an assessment. The same function backs +Find Talent (`talent/plugins.py` re-exports it as `relevance_score`) so the two cannot drift. + +**Extraction is what makes the bank usable.** `run_employment_agent` returns skills, years, +title, company, education and phone; before migration 029 the bank stored only `full_text` +and could not be searched or ranked at all. + +### Retention and deletion + +A banked CV is personal data held with no job to justify it, so it is held for a stated +period rather than indefinitely. + +- `bank_expires_at` is stamped **at upload** from `CV_BANK_RETENTION_MONTHS` (default 24). + Stamping on the row rather than computing on read means changing the setting later cannot + silently extend CVs already taken in. +- Expired rows are excluded from `list_bank_for_ranking`, so an expired CV is never put in + front of a recruiter. +- `cvbank.sweep_expired` runs nightly and **flags, never deletes.** A misconfigured window + would otherwise destroy the entire bank on one cron tick, and a resume someone sent us is + not something to drop on a timer with no record. Set + `CV_BANK_RETENTION_NOTIFY_EMAIL` to have the sweep raise an in-app notification. +- Deletion is a human action: `DELETE /candidate/cv-bank/delete` hard-deletes the row and + its bytes (`cv_bank_files` cascades) and removes the S3 object. It refuses rows that + already have a `job_post_id` — those are applications, not bank entries. + --- ## The matching agent @@ -958,6 +1010,16 @@ own keys with `os.getenv` from the same file. | `MANUAL_UPLOAD_TO_ADDRESS` | `manual-cv-upload@hr-ats.local` — To address stamped on synthetic inbox rows so source resolves to `Manual CV Upload` | | `APP_VERSION` | `dev` | +### CV Bank + +| Variable | Default | Notes | +|---|---|---| +| `CV_BANK_RETENTION_MONTHS` | `24` | Stamped onto `bank_expires_at` at upload, so a later change cannot extend CVs already taken in | +| `CV_BANK_RETENTION_CRON` | `0 3 * * *` | `cvbank.sweep_expired` schedule | +| `CV_BANK_RETENTION_NOTIFY_EMAIL` | — | Recipient of the expiry-review notification; blank disables it (the log line is still written) | +| `CV_BANK_SUGGEST_THRESHOLD` | `55` | Tier-1 rank a banked CV must clear before the recruiter is notified on job creation | +| `CV_BANK_SILVER_FLOOR` | `60` | Minimum ATS score for a rejected applicant to appear as a silver medalist | + --- ## Running locally @@ -990,7 +1052,8 @@ LLM failures are logged and skipped; the API still comes up. ```bash taskiq worker taskiq_management.broker_setup:broker \ - inbox.tasks inbox.sync_tasks taskiq_management.tasks g_sheet.tasks + inbox.tasks inbox.sync_tasks taskiq_management.tasks g_sheet.tasks \ + job.candidate.bank_tasks ``` **CV-upload worker** (isolated stream for manual uploads): diff --git a/backend/employment_agent/decorators.py b/backend/employment_agent/decorators.py index 563b5fd..614a7d3 100644 --- a/backend/employment_agent/decorators.py +++ b/backend/employment_agent/decorators.py @@ -102,6 +102,57 @@ def _clean_phone(value,resume_text): return text +def _clean_skills(value,resume_text): + """Keep only skills the resume actually contains, deduplicated, capped at 30. + + Same discipline as the company/education clamps: the model is asked for the + resume's own spelling, so anything absent from the text is an invention. A + skill chip is read as "this is in the CV", and the bank filters on it. + + Deduplication runs BEFORE the ceiling so a model that returns 31 near- + duplicates collapses under the limit instead of losing real skills. + """ + if not isinstance(value,list): + return [] + haystack=(resume_text or "").lower() + kept=[] + seen=set() + for entry in value: + if not isinstance(entry,str): + continue + text=entry.strip() + if not text or len(text)>60: + continue + lowered=text.lower() + if lowered in seen: + continue + if haystack and lowered not in haystack: + continue + seen.add(lowered) + kept.append(text) + return kept[:30] + + +def _clean_years(value,resume_text): + """Whole years of experience, bounded 0-60. Anything else is None. + + Seniority language is not a duration, so an unparseable value has to read + as "unknown" rather than 0 — 0 would sort as a junior candidate. + """ + if isinstance(value,bool): + return None + if isinstance(value,(int,float)): + years=int(value) + elif isinstance(value,str): + digits=re.search(r"\d+",value) + if not digits: + return None + years=int(digits.group()) + else: + return None + return years if 0<=years<=60 else None + + def prefer_extracted_phone(func): """Merge CV regex phone with the LLM value; keep the longer complete number.""" @@ -118,6 +169,8 @@ clamp_company_to_resume=clamp_in_resume("current_employment",NO_COMPANY) clamp_education_to_resume=clamp_in_resume("education",EDUCATION) clamp_linkedin_url=clamp_field("linkedin_url",_clean_linkedin) clamp_phone=clamp_field("phone",_clean_phone) +clamp_skills=clamp_field("skills",_clean_skills) +clamp_years_experience=clamp_field("years_experience",_clean_years) @require_json_object @@ -126,8 +179,16 @@ clamp_phone=clamp_field("phone",_clean_phone) @clamp_linkedin_url @prefer_extracted_phone @clamp_phone +@clamp_skills +@clamp_years_experience def parse_employment_response(data,resume_text=""): - """Pull company, education, title, linkedin_url, and phone from the agent JSON.""" + """Pull company, education, title, linkedin_url, phone, skills, and years + from the agent JSON. + + skills and years_experience default to []/None when the key is absent, so a + model reply predating the extended prompt still parses — the inbox match + path reads the other five keys and must not break on a partial response. + """ def as_str(key): value=data.get(key) return value.strip() if isinstance(value,str) else "" @@ -137,4 +198,6 @@ def parse_employment_response(data,resume_text=""): "current_title":as_str("current_title"), "linkedin_url":as_str("linkedin_url"), "phone":as_str("phone"), + "skills":data.get("skills") if isinstance(data.get("skills"),list) else [], + "years_experience":data.get("years_experience"), } diff --git a/backend/employment_agent/execute_agent.py b/backend/employment_agent/execute_agent.py index 8d15f90..debac70 100644 --- a/backend/employment_agent/execute_agent.py +++ b/backend/employment_agent/execute_agent.py @@ -24,6 +24,8 @@ async def run_employment_agent(*,resume_text=""): "current_title":CURRENT_TITLE, "linkedin_url":None, "phone":None, + "skills":[], + "years_experience":None, } try: data=await llm_call(prompt(),user_prompt(text),json_mode=True) diff --git a/backend/employment_agent/prompt.py b/backend/employment_agent/prompt.py index b453aff..070367f 100644 --- a/backend/employment_agent/prompt.py +++ b/backend/employment_agent/prompt.py @@ -19,7 +19,8 @@ def prompt(): You are given CV/resume text. Identify the candidate's CURRENT employer company name, their education (degree / school), their current job title, their -LinkedIn profile URL, and their phone number when present. +LinkedIn profile URL, their phone number, their skills, and their total years +of professional experience, when present. Rules: - Return only the company name that appears in the resume text for the ongoing / most recent role. @@ -32,6 +33,19 @@ Rules: - Do not invent education. If none is mentioned, return exactly: {EDUCATION} - Do not invent job title. If none is mentioned, return exactly: {CURRENT_TITLE} +skills (its own key — a JSON array of strings): +- List the candidate's concrete technical and professional skills: technologies, tools, languages, platforms, and named methodologies. +- Write each skill using the resume's own spelling. Every skill you return MUST appear in the resume text. +- Do not infer a skill from a job title, an employer, or a degree. "Backend Engineer" is not evidence of "Python". +- One skill per entry. Do not return sentences, responsibilities, or soft-skill filler like "team player" or "hard working". +- At most 30 entries, most relevant first. If the resume lists none, return an empty array []. + +years_experience (its own key — an integer or null): +- If the resume states a total (for example "6 years of experience"), use that stated number. +- Otherwise compute whole years only from employment dates explicitly written in the resume. +- Never infer it from seniority words, education dates, or the number of jobs listed. +- Must be between 0 and 60. If the resume supports neither a stated total nor explicit dates, return null. + linkedin_url (its own key — extract this separately from the other fields): - Return the candidate's own public LinkedIn profile URL (linkedin.com/in/..., /pub/..., /mwlite/in/..., or lnkd.in/...). - Reconstruct the URL if PDF extraction wrapped or spaced it (e.g. "linkedin.com/in/\\njane-doe" or "linkedin . com / in / jane-doe"). @@ -51,14 +65,16 @@ phone (its own key — extract this separately; copy EVERY digit): Examples of CORRECT values (copy this completeness; these are format samples, not this candidate): Example 1 — local 11-digit PK mobile, full LinkedIn: -Resume: "Ali Khan | 0321-5551234 | https://www.linkedin.com/in/ali-khan | Acme | BS CS | Engineer" +Resume: "Ali Khan | 0321-5551234 | https://www.linkedin.com/in/ali-khan | Acme | BS CS | Engineer | Skills: Python, Django, PostgreSQL | 6 years of experience" JSON: {{ "current_employment": "Acme", "education": "BS CS", "current_title": "Engineer", "linkedin_url": "https://www.linkedin.com/in/ali-khan", - "phone": "0321-5551234" + "phone": "0321-5551234", + "skills": ["Python", "Django", "PostgreSQL"], + "years_experience": 6 }} Example 2 — +92 with spaces; every digit kept: @@ -77,13 +93,23 @@ Example 5 — wrapped LinkedIn slug: Resume: "linkedin.com/in/\\njane-doe-123" JSON linkedin_url must be "https://www.linkedin.com/in/jane-doe-123". Not ".../jane-doe". +Example 6 — no stated total and no dates: +Resume: "Senior Architect. Led large teams." +JSON years_experience must be null. "Senior" is not a duration. + +Example 7 — dates only: +Resume: "Acme, Jan 2018 - Jan 2024, Engineer" +JSON years_experience must be 6, and skills must be [] because none are listed. + Respond with JSON only: {{ "current_employment": "Company Name", "education": "Degree / School", "current_title": "Job Title", "linkedin_url": "https://www.linkedin.com/in/slug", - "phone": "+92 300 1234567" + "phone": "+92 300 1234567", + "skills": ["Skill One", "Skill Two"], + "years_experience": 5 }} """ diff --git a/backend/g_sheet/app.py b/backend/g_sheet/app.py index 2b399ad..a181ff1 100644 --- a/backend/g_sheet/app.py +++ b/backend/g_sheet/app.py @@ -196,6 +196,11 @@ async def fetch_form_data( search: str | None = Query(None), processing_state: str | None = Query(None), is_duplicate: bool | None = Query(None), + # Tri-valued, like is_duplicate above: omit for no filter, true for rows that + # have the link, false for the ones missing it. Chasing the gaps is half the + # reason these exist, so `false` has to be a real filter and not "unset". + has_linkedin: bool | None = Query(None), + has_resume: bool | None = Query(None), offset: int = Query(0,ge=0), # Caller-chosen page size (Inbox sends 10/25/50/100). None = unpaged. limit: int | None = Query(None,ge=1,le=500), @@ -207,6 +212,7 @@ async def fetch_form_data( items,total=await service.get_form_data( sheet=sheet,search=search,offset=offset,limit=limit, processing_state=processing_state,is_duplicate=is_duplicate, + has_linkedin=has_linkedin,has_resume=has_resume, ) return JSONResponse(content={"data":items,"total":total,"status_code":200}) except HTTPException: @@ -218,12 +224,21 @@ async def fetch_form_data( @router.get("/sheet/form-data/counts") async def fetch_form_data_counts( sheet: str | None = Query(None), + # The badges narrow with the list. Without these the tab counts describe the + # whole sheet while the rows beneath them describe a filtered slice. + # processing_state and is_duplicate are absent on purpose: those two ARE the + # tabs, so passing them would make every badge report the current tab. + search: str | None = Query(None), + has_linkedin: bool | None = Query(None), + has_resume: bool | None = Query(None), current_user: dict = Depends(_FORM_DATA_READ), session: AsyncSession = Depends(get_session), ): try: service=SheetFormData(session=session) - data=await service.get_counts(sheet=sheet) + data=await service.get_counts( + sheet=sheet,search=search,has_linkedin=has_linkedin,has_resume=has_resume, + ) return JSONResponse(content={"data":data,"total":1,"status_code":200}) except HTTPException: raise diff --git a/backend/g_sheet/models.py b/backend/g_sheet/models.py index c40ff7e..1be1a5d 100644 --- a/backend/g_sheet/models.py +++ b/backend/g_sheet/models.py @@ -5,7 +5,7 @@ from __future__ import annotations import uuid from datetime import datetime, timezone -from sqlalchemy import Column, DateTime, Index, case, delete, func, insert, or_ +from sqlalchemy import Column, DateTime, Index, and_, case, delete, func, insert, or_ from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.ext.asyncio import AsyncSession from sqlmodel import Field, SQLModel, select @@ -17,6 +17,17 @@ def _now() -> datetime: _BULK_CHUNK = 1000 +# profile_link holds whatever the candidate typed into the form's "LinkedIn +# Profile Link" box. Nothing on the ingest path validates it — the real LinkedIn +# parsing runs only when a row is promoted, and writes to a different table — so +# matching on these is a heuristic, not proof of a profile. It misses a bare +# handle and it accepts a malformed URL that merely contains the domain. +# +# Module level, not a class attribute: SQLModel hands any leading-underscore +# class attribute to Pydantic, which turns it into a ModelPrivateAttr that is not +# iterable at class scope. +LINKEDIN_PATTERNS = ("%linkedin.com%", "%lnkd.in%") + class FormData(SQLModel, table=True): """One spreadsheet data row. raw_record keeps the full original header→value map.""" @@ -99,7 +110,10 @@ class FormData(SQLModel, table=True): updated_at: datetime = Field(default_factory=_now, sa_type=DateTime(timezone=True)) @classmethod - def _filters(cls, *, sheet=None, search=None, processing_state=None, is_duplicate=None): + def _filters( + cls, *, sheet=None, search=None, processing_state=None, is_duplicate=None, + has_linkedin=None, has_resume=None, + ): filters = [] if sheet: filters.append(cls.sheet == sheet) @@ -107,6 +121,25 @@ class FormData(SQLModel, table=True): filters.append(cls.processing_state == processing_state) if is_duplicate is not None: filters.append(cls.is_duplicate == bool(is_duplicate)) + if has_linkedin is not None: + matches = [cls.profile_link.ilike(p) for p in LINKEDIN_PATTERNS] + if has_linkedin: + filters.append(or_(*matches)) + else: + # The NULL arm is load-bearing. `NOT (NULL ILIKE ...)` evaluates to + # NULL, which WHERE discards, so without it the rows with no link + # at all would drop out of the "no LinkedIn" view — precisely the + # rows that view exists to find. + filters.append(or_( + cls.profile_link.is_(None), + and_(*[~m for m in matches]), + )) + if has_resume is not None: + # _cell() stores a blank sheet cell as NULL, never "", so a NULL test + # is the whole check and an empty-string arm would be dead weight. + filters.append( + cls.resume_link.is_not(None) if has_resume else cls.resume_link.is_(None) + ) if search: # Twelve unanchored ILIKEs over ~26k rows is a sequential scan of a few # tens of ms — acceptable at this size; a pg_trgm GIN index is the @@ -279,12 +312,14 @@ class FormData(SQLModel, table=True): @classmethod async def fetch_form_data( cls, session: AsyncSession, *, sheet=None, search=None, - processing_state=None, is_duplicate=None, offset=0, limit=None, + processing_state=None, is_duplicate=None, has_linkedin=None, + has_resume=None, offset=0, limit=None, ): statement = select(cls).order_by(cls.sheet, cls.row_number) for clause in cls._filters( sheet=sheet, search=search, processing_state=processing_state, is_duplicate=is_duplicate, + has_linkedin=has_linkedin, has_resume=has_resume, ): statement = statement.where(clause) if offset: @@ -336,20 +371,36 @@ class FormData(SQLModel, table=True): @classmethod async def count_form_data( cls, session: AsyncSession, *, sheet=None, search=None, - processing_state=None, is_duplicate=None, + processing_state=None, is_duplicate=None, has_linkedin=None, + has_resume=None, ): statement = select(func.count()).select_from(cls) for clause in cls._filters( sheet=sheet, search=search, processing_state=processing_state, is_duplicate=is_duplicate, + has_linkedin=has_linkedin, has_resume=has_resume, ): statement = statement.where(clause) result = await session.execute(statement) return result.scalar_one() @classmethod - async def count_processing(cls, session: AsyncSession, *, sheet=None): - """Tab badge counts for the Sheet Forms channel.""" + async def count_processing( + cls, session: AsyncSession, *, sheet=None, search=None, + has_linkedin=None, has_resume=None, + ): + """Tab badge counts for the Sheet Forms channel. + + Narrowed by the same predicates as the list, through the same _filters() + call, because a badge that disagrees with the rows under it reads as a + bug. This used to take only `sheet`, so switching on the search box + already left "All Applications 612" sitting above twelve rows; adding + the link filters would have made that worse. + + processing_state and is_duplicate are deliberately NOT accepted: those + two ARE the tabs. Passing them would have each badge count only its own + tab, so every badge would report the tab the user is already on. + """ statement = select( func.count().label("all"), func.coalesce(func.sum(case((cls.processing_state == "unread", 1), else_=0)), 0).label("unread"), @@ -358,8 +409,11 @@ class FormData(SQLModel, table=True): func.coalesce(func.sum(case((cls.processing_state == "rejected", 1), else_=0)), 0).label("rejected"), func.coalesce(func.sum(case((cls.is_duplicate == True, 1), else_=0)), 0).label("duplicates"), # noqa: E712 ).select_from(cls) - if sheet: - statement = statement.where(cls.sheet == sheet) + for clause in cls._filters( + sheet=sheet, search=search, + has_linkedin=has_linkedin, has_resume=has_resume, + ): + statement = statement.where(clause) row = (await session.execute(statement)).one() return { "all": int(row.all or 0), diff --git a/backend/g_sheet/views.py b/backend/g_sheet/views.py index ec32b82..3764182 100644 --- a/backend/g_sheet/views.py +++ b/backend/g_sheet/views.py @@ -431,16 +431,18 @@ class SheetFormData(Sheet): async def get_form_data( self,sheet=None,search=None,offset=0,limit=None, - processing_state=None,is_duplicate=None, + processing_state=None,is_duplicate=None,has_linkedin=None,has_resume=None, ): session=self._require_session() rows=await FormData.fetch_form_data( session,sheet=sheet,search=search,offset=offset,limit=limit, processing_state=processing_state,is_duplicate=is_duplicate, + has_linkedin=has_linkedin,has_resume=has_resume, ) total=await FormData.count_form_data( session,sheet=sheet,search=search, processing_state=processing_state,is_duplicate=is_duplicate, + has_linkedin=has_linkedin,has_resume=has_resume, ) items=await self._hydrate_job_posts([serialize_form_data(row) for row in rows]) from job.candidate.views import CandidateView @@ -587,11 +589,17 @@ class SheetFormData(Sheet): raise HTTPException(status_code=404,detail="Form data not found") return await self.get_form_data_by_id(record_id) - async def get_counts(self,sheet=None): - return await FormData.count_processing(self._require_session(),sheet=sheet) + async def get_counts(self,sheet=None,search=None,has_linkedin=None,has_resume=None): + return await FormData.count_processing( + self._require_session(),sheet=sheet,search=search, + has_linkedin=has_linkedin,has_resume=has_resume, + ) - async def count_rows(self,sheet=None): - return await FormData.count_form_data(self._require_session(),sheet=sheet) + async def count_rows(self,sheet=None,search=None,has_linkedin=None,has_resume=None): + return await FormData.count_form_data( + self._require_session(),sheet=sheet,search=search, + has_linkedin=has_linkedin,has_resume=has_resume, + ) async def get_imported_sheets(self): session=self._require_session() diff --git a/backend/inbox/models.py b/backend/inbox/models.py index a39c3a7..07b54ba 100644 --- a/backend/inbox/models.py +++ b/backend/inbox/models.py @@ -210,6 +210,86 @@ class Inbox(SQLModel, table=True): }) return rows + @classmethod + async def list_silver_medalists(cls, session: AsyncSession, *, min_score=60, limit=500): + """Rejected applicants who scored well — the CV Bank's second population. + + Read live rather than copied into manual_upload_candidate: these rows + already exist, and a copy would immediately start drifting from the + application it was taken from. + + Only REJECTED counts. CLOSED is the ingest DEFAULT for any unprocessed + email (see Inbox_Messages.application_status), so treating it as a + rejection would tip the entire unread inbox into the bank. + + Requiring a score is what makes these "silver" rather than merely + "not hired": an unscored rejection carries no evidence worth keeping. + """ + try: + from job.job_post.models import JobPosts + from job.candidate.models import Candidates + qry=( + select( + cls.id.label("inbox_id"), + cls.user_id, + Users.name, + Users.email, + Users.linkedin_url, + Inbox_Messages.candidate_phone_number.label("phone"), + Inbox_Messages.current_employment.label("current_company"), + Inbox_Messages.current_title, + Inbox_Messages.candidate_education.label("education"), + Inbox_Messages.file_name, + Inbox_Messages.file_path, + Inbox_Messages.ats_score, + Inbox_Messages.ats_band, + cls.created_at, + JobPosts.title.label("last_job_title"), + Candidates.matched_keywords, + Candidates.years_experience, + ) + .join(Users,cls.user_id==Users.id) + .join(Inbox_Messages,cls.message_id==Inbox_Messages.id) + .outerjoin(JobPosts,Inbox_Messages.assigned_job_post_id==JobPosts.id) + .outerjoin(AtsResults,cls.ats_id==AtsResults.id) + # candidate_id is NULL whenever the CV email matched a user, so + # this join only sometimes lands — hence the keyword list being + # optional rather than the filter. + .outerjoin(Candidates,AtsResults.candidate_id==Candidates.id) + .where(Inbox_Messages.application_status==Candidate_application_Status.REJECTED) + .where(Inbox_Messages.ats_score.is_not(None)) + .where(Inbox_Messages.ats_score>=float(min_score)) + .where(Inbox_Messages.is_duplicate==False) # noqa: E712 + .order_by(Inbox_Messages.ats_score.desc(),cls.created_at.desc(),cls.id.desc()) + .limit(limit) + ) + result=await session.execute(qry) + rows=[] + for row in result.mappings().all(): + rows.append({ + "inbox_id":row["inbox_id"], + "user_id":str(row["user_id"]) if row["user_id"] else None, + "name":row["name"], + "email":row["email"], + "phone":row["phone"] or None, + "linkedin_url":row["linkedin_url"] or None, + "current_company":row["current_company"] or None, + "current_title":row["current_title"] or None, + "education":row["education"] or None, + "file_name":row["file_name"] or None, + "file_path":row["file_path"] or None, + "ai_score":int(row["ats_score"]) if row["ats_score"] is not None else None, + "recommendation":row["ats_band"] or None, + "last_job_title":row["last_job_title"] or None, + "matched_keywords":list(row["matched_keywords"] or []), + "years_experience":row["years_experience"], + "bank_expires_at":None, + "created_at":row["created_at"], + }) + return rows + except Exception as e: + raise HTTPException(status_code=500,detail=str(e)) + @classmethod async def linkedin_urls_by_message_ids(cls, session: AsyncSession, message_ids) -> dict: """users.linkedin_url keyed by inbox_messages.id for one list page.""" diff --git a/backend/job/app.py b/backend/job/app.py index 70fee20..caee209 100644 --- a/backend/job/app.py +++ b/backend/job/app.py @@ -2,7 +2,7 @@ from fastapi import APIRouter,Depends,Query,Response from fastapi.responses import FileResponse,JSONResponse from fastapi import HTTPException from db_setup import get_session -from job.candidate.views import CandidateScoring,FileRead,CandidateView,parse_linkedin_url_from_cv +from job.candidate.views import CandidateScoring,FileRead,CandidateView,extract_bank_profile_from_cv,parse_linkedin_url_from_cv from job.interviews.views import Interview from job.notes.views import Note from job.activity.views import ActivityLog @@ -25,6 +25,7 @@ from datetime import datetime, time, timezone from pydantic import BaseModel from uuid import UUID from typing import Literal, Optional +import os import uuid load_dotenv() logging.basicConfig(level=logging.INFO) @@ -32,12 +33,25 @@ logger = logging.getLogger(__name__) router = APIRouter() +# A banked CV is personal data held with no job to justify it, so it is held for +# a stated period rather than forever. Stamped on the row at upload so changing +# the setting later cannot silently extend CVs already taken in. +CV_BANK_RETENTION_MONTHS = int(os.getenv("CV_BANK_RETENTION_MONTHS", "24")) +# Deterministic keyword overlap, not comprehension — the floor only decides who +# is worth telling a recruiter about, never who is qualified. +CV_BANK_SUGGEST_THRESHOLD = int(os.getenv("CV_BANK_SUGGEST_THRESHOLD", "55")) + class MatchingAssign(BaseModel): id: UUID job_post_id: UUID | None = None +class CvBankScoreRequest(BaseModel): + job_id: UUID + ids: list[UUID] + + class CandidateUpdate(BaseModel): favorite: bool | None = None rating: float | None = None @@ -321,7 +335,10 @@ async def cv_bank_upload( parsed=await reader.injest_manual_upload() # 400 on unreadable/empty PDF text=parsed.get("text") or "" detected,_=extract_candidate_email(text) - parsed_linkedin=await parse_linkedin_url_from_cv(text) + # One agent call for the whole profile. Banking is the only ingest path + # with no job attached, so this is the CV's only structured data until + # a recruiter scores it against a real opening. + profile=await extract_bank_profile_from_cv(text) # Basename against both separator styles — a Windows client sends # C:\Users\x\cv.pdf whose PosixPath name is the whole string. original=PurePosixPath(PureWindowsPath(file.filename or "resume.pdf").name).name or "resume.pdf" @@ -333,7 +350,10 @@ async def cv_bank_upload( file_name=original, created_by=current_user.get("id"), pdf_bytes=content, - linkedin_url=parsed_linkedin, + linkedin_url=profile.get("linkedin_url"), + profile=profile, + bank_reason="speculative", + retention_months=CV_BANK_RETENTION_MONTHS, ) try: uploaded=S3().upload_for_record( @@ -372,27 +392,84 @@ async def cv_bank_upload( async def cv_bank_fetch( top: int = Query(100, ge=1, le=500), skip: int = Query(0, ge=0), + source: Literal["speculative","silver_medalist"] | None = Query(default=None), + search: str | None = Query(default=None), + skills: list[str] | None = Query(default=None), + min_years: int | None = Query(default=None, ge=0, le=60), + band: str | None = Query(default=None), + job_post_id: str | None = Query(default=None), current_user: dict = Depends(require_permission(PermissionTag.CANDIDATES_VIEW)), session: AsyncSession = Depends(get_session), ): - """The stored-CV bank, newest first. Download the file via - GET /documents/download?manual_upload_candidate_id=.""" - from job.candidate.models import Manual_UPLOAD_CANDIDATE + """The CV Bank: speculative uploads plus rejected applicants who scored well. + + `job_post_id` does not filter the list — it attaches the deterministic + tier-1 rank for that job and sorts by it, which is the "a role just opened, + who do we already have" view. Download a file via + GET /candidate/cv-bank/file?id=.""" try: - rows,total=await Manual_UPLOAD_CANDIDATE.list_bank(session,limit=top,offset=skip) - data=[{ - "id":str(r.id), - "file_name":r.file_name, - "file_path":(r.file_path or "").strip() or None, - "candidate_email":r.candidate_email or None, - "candidate_name":r.candidate_name or None, - "linkedin_url":r.linkedin_url or None, - "created_at":r.created_at.isoformat() if r.created_at else None, - } for r in rows] + service=CandidateView(session=session) + data,total=await service.list_bank( + source=source,search=search,skills=skills,min_years=min_years, + band=band,job_post_id=job_post_id,limit=top,offset=skip, + ) return JSONResponse(content={"data":data,"total":total,"status_code":200}) except HTTPException: raise except Exception as e: + logger.exception("cv-bank fetch failed") + raise HTTPException(status_code=500,detail=str(e)) + + +@router.post("/candidate/cv-bank/score") +async def cv_bank_score( + payload: CvBankScoreRequest, + current_user: dict = Depends(require_permission(PermissionTag.CANDIDATES_CREATE)), + session: AsyncSession = Depends(get_session), +): + """Run the real ATS score on CVs already in the bank — the paid tier. + + No upload: the bytes are already stored. Mirrors POST /candidate/score_inbox, + and the results land in candidates / ats_results like any other scored CV, + so a banked candidate shows up on the leaderboard the same way.""" + try: + service=CandidateScoring(session=session) + data=await service.score_bank(str(payload.job_id),payload.ids,current_user) + return JSONResponse(content={"data":data,"total":len(data),"status_code":200}) + except HTTPException: + raise + except Exception as e: + logger.exception("cv-bank scoring failed") + raise HTTPException(status_code=500,detail=str(e)) + + +@router.get("/candidate/cv-bank/suggestions") +async def cv_bank_suggestions( + job_post_id: str = Query(...), + top: int = Query(20, ge=1, le=200), + min_rank: int | None = Query(default=None, ge=0, le=100), + current_user: dict = Depends(require_permission(PermissionTag.CANDIDATES_VIEW)), + session: AsyncSession = Depends(get_session), +): + """Banked CVs worth looking at for one job, best first. + + rank_score is deterministic keyword overlap, not an ATS score — it orders + the bank so a recruiter knows where to start. Scoring for real costs money + and happens via POST /candidate/cv-bank/score on the ones they pick.""" + try: + service=CandidateView(session=session) + floor=CV_BANK_SUGGEST_THRESHOLD if min_rank is None else min_rank + rows,_=await service.list_bank( + job_post_id=job_post_id,limit=service.BANK_SCAN_CAP,offset=0, + ) + data=[r for r in rows if (r.get("rank_score") or 0)>=floor][:top] + return JSONResponse(content={ + "data":data,"total":len(data),"threshold":floor,"status_code":200, + }) + except HTTPException: + raise + except Exception as e: + logger.exception("cv-bank suggestions failed") raise HTTPException(status_code=500,detail=str(e)) diff --git a/backend/job/candidate/bank_tasks.py b/backend/job/candidate/bank_tasks.py new file mode 100644 index 0000000..5a07b21 --- /dev/null +++ b/backend/job/candidate/bank_tasks.py @@ -0,0 +1,215 @@ +"""CV Bank Taskiq tasks — profile backfill and job-opening rank. + +Worker: taskiq worker taskiq_management.broker_setup:broker job.candidate.bank_tasks + +Two jobs live here, both about the bank being useful rather than merely stored: + + cvbank.backfill_profiles one-off, for CVs banked before extraction existed + cvbank.rank_for_job fired when a job opens, so the bank is offered up + instead of waiting to be remembered +""" + +from __future__ import annotations + +import logging +import os + +from db_setup import session_scope +from taskiq_management.broker_setup import MAX_RETRIES, RETRY_DELAY, broker +from taskiq_management.middleware import PermanentTaskError + +logger = logging.getLogger("cvbank.tasks") + +# One agent call per CV, so a backfill of a large bank is paced across runs +# rather than fired as one unbounded burst. +BACKFILL_BATCH = 25 + +# 03:00 daily. The sweep only flags, so the exact hour does not matter; off-peak +# just keeps it away from the scoring workload. +RETENTION_SWEEP_CRON = os.getenv("CV_BANK_RETENTION_CRON", "0 3 * * *") + + +@broker.task( + task_name="cvbank.backfill_profiles", + retry_on_error=True, + max_retries=MAX_RETRIES, + delay=RETRY_DELAY, +) +async def backfill_bank_profiles(limit: int = BACKFILL_BATCH) -> dict: + """Extract skills/title/company/years for CVs banked before migration 029. + + Re-runnable: rows are selected by "has no extraction yet", so a finished + bank returns scanned=0 and the task becomes a no-op. Returns `remaining` + so a caller can decide whether to enqueue another batch. + """ + from job.candidate.models import Manual_UPLOAD_CANDIDATE + from job.candidate.views import extract_bank_profile_from_cv + + updated = 0 + failed = 0 + async with session_scope() as session: + rows = await Manual_UPLOAD_CANDIDATE.list_bank_needing_profile( + session, limit=max(1, int(limit or BACKFILL_BATCH)), + ) + for row in rows: + # extract_bank_profile_from_cv never raises, but a bad row must not + # cost the whole batch either. + try: + profile = await extract_bank_profile_from_cv(row.full_text) + except Exception: + logger.exception("bank profile backfill failed id=%s", row.id) + failed += 1 + continue + if not any(profile.get(k) for k in ("skills", "current_position", "current_company")): + continue + await Manual_UPLOAD_CANDIDATE.set_bank_profile(session, row.id, profile) + updated += 1 + remaining = len( + await Manual_UPLOAD_CANDIDATE.list_bank_needing_profile(session, limit=1) + ) + return {"scanned": len(rows), "updated": updated, "failed": failed, "remaining": remaining} + + +@broker.task( + task_name="cvbank.rank_for_job", + retry_on_error=True, + max_retries=MAX_RETRIES, + delay=RETRY_DELAY, +) +async def rank_bank_for_job(job_post_id: str) -> dict: + """Score every banked CV against a newly opened job — tier 1, free. + + Deterministic keyword overlap only. No LLM call, so this runs over the + whole bank on every job opening without a bill; the paid ATS score happens + later and only for the handful a recruiter shortlists. + """ + from job.candidate.models import CvBankMatches, Manual_UPLOAD_CANDIDATE + from job.job_post.models import JobPosts + from matching.ranking import rank_bank_row + + if not job_post_id or not str(job_post_id).strip(): + raise PermanentTaskError("job_post_id is required") + job_post_id = str(job_post_id).strip() + + async with session_scope() as session: + job = await JobPosts.get_job_post_by_id(session, job_post_id) + if job is None or job.is_deleted: + raise PermanentTaskError("job post missing or deleted") + job_fields = { + "title": job.title, + "requirements": job.requirements, + "optional_skills": job.optional_skills, + } + rows = await Manual_UPLOAD_CANDIDATE.list_bank_for_ranking(session) + scores = [(row.id, rank_bank_row(job_fields, row)) for row in rows] + await CvBankMatches.replace_for_job(session, job.id, scores) + + threshold = _suggest_threshold() + strong = [s for _, s in scores if s >= threshold] + if strong: + await _notify_owner(job_post_id, len(strong)) + return {"ranked": len(scores), "above_threshold": len(strong)} + + +@broker.task( + task_name="cvbank.sweep_expired", + schedule=[{"cron": RETENTION_SWEEP_CRON}], +) +async def sweep_expired_bank_cvs() -> dict: + """Flag banked CVs past their retention window — nightly. + + Flags, never deletes. These are resumes a person sent us: dropping them on + a timer with no record would be worse than holding them, and a wrongly + configured window would silently destroy the whole bank. A human decides, + the sweep only makes the decision unavoidable. + + Expired rows are already excluded from ranking (list_bank_for_ranking), so + nothing is being surfaced to recruiters in the meantime. + """ + from job.candidate.models import Manual_UPLOAD_CANDIDATE + + async with session_scope() as session: + rows = await Manual_UPLOAD_CANDIDATE.list_bank_expired(session) + for row in rows: + logger.info( + "cv-bank retention expired id=%s banked_at=%s expired_at=%s", + row.id, + row.created_at.isoformat() if row.created_at else None, + row.bank_expires_at.isoformat() if row.bank_expires_at else None, + ) + if rows: + await _notify_retention_review(len(rows)) + return {"expired": len(rows)} + + +async def _notify_retention_review(count: int) -> None: + """Tell whoever banked the CVs that the window has run out. + + Best effort — the log line above is the durable record. + """ + import uuid as _uuid + + try: + from notifications.models import Notifications + from users.models import Users + + recipient = os.getenv("CV_BANK_RETENTION_NOTIFY_EMAIL", "").strip().lower() + if not recipient: + return + async with session_scope() as session: + user = await Users.get_user_by_email(session, recipient) + if user is None: + return + await Notifications.insert_notification(session, { + "user_id": _uuid.UUID(str(user.id)), + "kind": "system", + "title": "CV Bank retention review", + "body": ( + f"{count} stored CV{'s' if count != 1 else ''} passed the retention " + "window and need to be kept with a reason or deleted." + ), + "link_path": "/cvbank", + }) + except Exception: + logger.exception("cv-bank retention notification failed") + + +def _suggest_threshold() -> int: + return int(os.getenv("CV_BANK_SUGGEST_THRESHOLD", "55")) + + +async def _notify_owner(job_post_id: str, count: int) -> None: + """Tell the job's recruiter the bank already holds plausible candidates. + + This is the whole point of ranking on job creation: without it the bank + only gets searched by someone who remembers it exists. + + Best effort — a missing notification must never fail the ranking that has + already been persisted. + """ + import uuid as _uuid + + try: + from job.job_post.models import JobPosts + from notifications.models import Notifications + + async with session_scope() as session: + job = await JobPosts.get_job_post_by_id(session, job_post_id) + if job is None: + return + raw = getattr(job, "current_recruiter_id", None) or getattr(job, "created_by", None) + if not raw: + return + await Notifications.insert_notification(session, { + "user_id": _uuid.UUID(str(raw)), + "kind": "application", + "title": "CVs in the bank match this job", + "body": ( + f"{count} stored CV{'s' if count != 1 else ''} look relevant to " + f"{job.title}. Open the CV Bank to review them." + ), + "link_path": f"/cvbank?job={job_post_id}", + "job_post_id": job.id, + }) + except Exception: + logger.exception("cv-bank suggestion notification failed job=%s", job_post_id) diff --git a/backend/job/candidate/models.py b/backend/job/candidate/models.py index 0ac06bf..f7a2af9 100644 --- a/backend/job/candidate/models.py +++ b/backend/job/candidate/models.py @@ -1,9 +1,10 @@ import uuid -from datetime import datetime, timezone +from datetime import datetime, timedelta, timezone from typing import TYPE_CHECKING, List, Optional from fastapi import HTTPException from sqlalchemy import JSON, DateTime, Index, UniqueConstraint, and_, func, or_ +from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.exc import IntegrityError from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.orm import selectinload @@ -52,6 +53,16 @@ class Manual_UPLOAD_CANDIDATE(SQLModel, table=True): platform: str = Field(default="") created_by: uuid.UUID | None = Field(default=None, foreign_key="users.id") experience: str = Field(default="") + # Employment-agent extractions, written at CV-bank ingest (see 029). These + # are what make the bank searchable — full_text alone cannot be filtered on. + # `experience` above is free text from the Add Candidate form; this one is + # the numeric years the bank filters and sorts by, so they stay separate. + skills: list[str] = Field(default_factory=list, sa_type=JSONB, sa_column_kwargs={"server_default": "[]"}) + years_experience: int | None = Field(default=None) + education: str = Field(default="", sa_column_kwargs={"server_default": ""}) + # Why the CV is held (speculative / referral) and when retention expires. + bank_reason: str = Field(default="", sa_column_kwargs={"server_default": ""}) + bank_expires_at: datetime | None = Field(default=None, sa_type=DateTime(timezone=True)) # Candidate_application_Status value (PENDING, SCREENING, …). Empty reads as Shortlist. status: str = Field(default="") # Free text, not a users FK: a referrer is often someone outside the system @@ -512,7 +523,8 @@ class Manual_UPLOAD_CANDIDATE(SQLModel, table=True): candidate_name, full_text, file_name, created_by, pdf_bytes, content_type="application/pdf", - linkedin_url=None): + linkedin_url=None, profile=None, + bank_reason="speculative", retention_months=None): """Bank a CV: metadata row + its bytes (cv_bank_files) in one commit. file_path is filled after S3 upload under Temp/{id}/ (see cv_bank_upload). @@ -521,7 +533,13 @@ class Manual_UPLOAD_CANDIDATE(SQLModel, table=True): candidate ACCOUNT is created/reused so the person shows up on the Candidates screen; unlike an application there is still no inbox entry, no scoring, and no setup email. A CV with no detectable email banks - fine and simply stays account-less.""" + fine and simply stays account-less. + + `profile` is the rest of the employment-agent extraction (company, + title, education, phone, skills, years) — the only structured data a + banked CV gets, since nothing scores it until it is matched to a job. + `retention_months` stamps bank_expires_at so the CV is held for a + stated period rather than indefinitely.""" import os from role.models import EnumRoles, Roles @@ -550,7 +568,8 @@ class Manual_UPLOAD_CANDIDATE(SQLModel, table=True): user.is_active = True session.add(user) - url = (linkedin_url or "").strip() or None + extracted = profile or {} + url = (linkedin_url or extracted.get("linkedin_url") or "").strip() or None if url: linkedin_slug = slug_from_url(url) or NO_SLUG else: @@ -558,13 +577,25 @@ class Manual_UPLOAD_CANDIDATE(SQLModel, table=True): if user and url: await Users.set_linkedin_url_if_empty(session, user_id=user.id, url=url) + expires_at = None + if retention_months: + expires_at = _now() + timedelta(days=30 * int(retention_months)) + row = cls( candidate_email=email, candidate_name=(candidate_name or "").strip() or (email or ""), + candidate_phone=(extracted.get("candidate_phone") or "").strip(), job_post_id=None, full_text=full_text or "", linkedin_slug=linkedin_slug, linkedin_url=url, + current_company=(extracted.get("current_company") or "").strip(), + current_position=(extracted.get("current_position") or "").strip(), + education=(extracted.get("education") or "").strip(), + skills=extracted.get("skills") or [], + years_experience=extracted.get("years_experience"), + bank_reason=(bank_reason or "").strip(), + bank_expires_at=expires_at, apply_via="cv_bank", user_id=user.id if user else None, created_by=cls._as_uuid(created_by), @@ -603,6 +634,58 @@ class Manual_UPLOAD_CANDIDATE(SQLModel, table=True): ) return list(result.scalars().all()), total + @classmethod + async def list_bank_needing_profile(cls, session: AsyncSession, limit=50): + """Bank rows stored before the extraction existed (see 029). + + Skills is the marker: a CV that genuinely lists none still gets its + title or years filled, so an empty skills array plus a blank title + means the agent never ran, not that the CV was sparse. + """ + result = await session.execute( + select(cls) + .where( + cls.apply_via == "cv_bank", + cls.full_text != "", + func.coalesce(func.jsonb_array_length(cls.skills), 0) == 0, + or_(cls.current_position == "", cls.current_position.is_(None)), + ) + .order_by(cls.created_at.desc()) + .limit(limit) + ) + return list(result.scalars().all()) + + @classmethod + async def set_bank_profile(cls, session: AsyncSession, record_id, profile): + """Write an employment-agent extraction onto an existing bank row. + + Only fills blanks — a recruiter may have corrected the company or title + by hand, and a backfill must not overwrite that. + """ + row = await session.get(cls, cls._as_uuid(record_id)) + if row is None: + return None + if not (row.current_company or "").strip(): + row.current_company = (profile.get("current_company") or "").strip() + if not (row.current_position or "").strip(): + row.current_position = (profile.get("current_position") or "").strip() + if not (row.education or "").strip(): + row.education = (profile.get("education") or "").strip() + if not (row.candidate_phone or "").strip(): + row.candidate_phone = (profile.get("candidate_phone") or "").strip() + if not row.skills: + row.skills = profile.get("skills") or [] + if row.years_experience is None: + row.years_experience = profile.get("years_experience") + if not (row.linkedin_url or "").strip() and profile.get("linkedin_url"): + row.linkedin_url = profile["linkedin_url"] + row.linkedin_slug = slug_from_url(profile["linkedin_url"]) or NO_SLUG + row.updated_at = _now() + session.add(row) + await session.commit() + await session.refresh(row) + return row + @classmethod async def list_matching(cls, session: AsyncSession, *, assigned=None, search=None, limit=100, offset=0): @@ -695,6 +778,41 @@ class Manual_UPLOAD_CANDIDATE(SQLModel, table=True): await session.refresh(row) return row + @classmethod + async def list_bank_for_ranking(cls, session: AsyncSession, limit=5000): + """Every CV still in the bank, for tier-1 ranking against a new job. + + Expired CVs are excluded: ranking one would put a candidate in front of + a recruiter after the retention window said to stop holding them. + """ + result = await session.execute( + select(cls) + .where( + cls.apply_via == "cv_bank", + cls.job_post_id.is_(None), + or_(cls.bank_expires_at.is_(None), cls.bank_expires_at > _now()), + ) + .order_by(cls.created_at.desc()) + .limit(limit) + ) + return list(result.scalars().all()) + + @classmethod + async def list_bank_expired(cls, session: AsyncSession, limit=500): + """Bank CVs past their retention window, for the nightly sweep.""" + result = await session.execute( + select(cls) + .where( + cls.apply_via == "cv_bank", + cls.job_post_id.is_(None), + cls.bank_expires_at.is_not(None), + cls.bank_expires_at <= _now(), + ) + .order_by(cls.bank_expires_at.asc()) + .limit(limit) + ) + return list(result.scalars().all()) + @classmethod async def delete_bank_cv(cls, session: AsyncSession, record_id): """Hard delete unassigned bank rows only — assigned rows are applications. @@ -710,6 +828,60 @@ class Manual_UPLOAD_CANDIDATE(SQLModel, table=True): return row +class CvBankMatches(SQLModel, table=True): + """Tier-1 rank of one banked CV against one job post (see 030). + + Persisted rather than computed on read because the whole point is to tell a + recruiter the bank already holds candidates the moment a job opens — a + notification cannot wait for someone to open the screen. + """ + + __tablename__ = "cv_bank_matches" + __table_args__ = ( + UniqueConstraint( + "manual_upload_candidate_id", "job_post_id", + name="uq_cv_bank_matches_pair", + ), + ) + + id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) + manual_upload_candidate_id: uuid.UUID = Field(foreign_key="manual_upload_candidate.id") + job_post_id: uuid.UUID = Field(foreign_key="job_posts.id", index=True) + rank_score: int = Field(default=0) + computed_at: datetime = Field(default_factory=_now, sa_type=DateTime(timezone=True)) + + @classmethod + async def replace_for_job(cls, session: AsyncSession, job_post_id, scores): + """Swap in a fresh ranking for one job. + + Delete-then-insert rather than upsert: a re-rank after the job's + requirements were edited must not leave behind scores for CVs that have + since been assigned or deleted. + """ + jid = job_post_id if isinstance(job_post_id, uuid.UUID) else uuid.UUID(str(job_post_id)) + existing = await session.execute(select(cls).where(cls.job_post_id == jid)) + for row in existing.scalars().all(): + await session.delete(row) + await session.flush() + for record_id, score in scores: + session.add(cls( + manual_upload_candidate_id=record_id, + job_post_id=jid, + rank_score=int(score or 0), + )) + await session.commit() + return len(scores) + + @classmethod + async def scores_for_job(cls, session: AsyncSession, job_post_id) -> dict: + """rank_score keyed by manual_upload_candidate_id, as strings.""" + jid = job_post_id if isinstance(job_post_id, uuid.UUID) else uuid.UUID(str(job_post_id)) + result = await session.execute( + select(cls.manual_upload_candidate_id, cls.rank_score).where(cls.job_post_id == jid) + ) + return {str(record_id): int(score) for record_id, score in result.all()} + + class CvBankFiles(SQLModel, table=True): """PDF bytes of a CV-bank entry — in the database so production redeploys (ephemeral container filesystems) can never lose a stored CV. Created in diff --git a/backend/job/candidate/serializers.py b/backend/job/candidate/serializers.py index e79371d..92212d1 100644 --- a/backend/job/candidate/serializers.py +++ b/backend/job/candidate/serializers.py @@ -63,6 +63,91 @@ def serialize_matching_candidate(row, job_post=None) -> Dict[str,Any]: } +def serialize_bank_candidate(row, *, rank_score=None) -> Dict[str,Any]: + """A CV held with no job, for the CV Bank screen. + + Same shape as serialize_bank_silver_medalist so the table renders one row + type regardless of which population the candidate came from. `id` is + prefixed because the two sources have different key spaces and would + otherwise collide in a merged list. + + rank_score is the deterministic tier-1 overlap against whichever job the + recruiter is ranking by; it is None until they pick one, and it is NOT an + ATS score — ai_score is. + """ + name=(row.candidate_name or "").strip() or (row.candidate_email or "").strip() or (row.file_name or "").strip() or "Unknown" + return { + "id":f"bank:{row.id}", + "record_id":str(row.id), + "bank_source":"speculative", + "name":name, + "email":(row.candidate_email or "").strip() or None, + "phone":(row.candidate_phone or "").strip() or None, + "file_name":(row.file_name or "").strip() or None, + "file_path":(row.file_path or "").strip() or None, + "linkedin_url":row.linkedin_url or None, + "current_company":(row.current_company or "").strip() or None, + "current_position":(row.current_position or "").strip() or None, + "education":(row.education or "").strip() or None, + "skills":list(row.skills or []), + "years_experience":row.years_experience, + "ai_score":None, + "recommendation":None, + "rank_score":rank_score, + "last_job_title":None, + "bank_reason":(row.bank_reason or "").strip() or None, + "bank_expires_at":row.bank_expires_at.isoformat() if row.bank_expires_at else None, + "user_id":str(row.user_id) if row.user_id else None, + "assigned_job_post_id":str(row.job_post_id) if row.job_post_id else None, + "created_at":row.created_at.isoformat() if row.created_at else None, + "updated_at":row.updated_at.isoformat() if row.updated_at else None, + } + + +def serialize_bank_silver_medalist(row, *, rank_score=None) -> Dict[str,Any]: + """A past applicant who scored well and did not get the job. + + Read from the live application tables rather than copied into the bank, so + there is no second source of truth to keep in sync. `row` is the flat + mapping produced by Inbox.list_silver_medalists. + """ + def get(key): + value=row.get(key) + return value.strip() if isinstance(value,str) else value + + name=(get("name") or "") or (get("email") or "") or "Unknown" + expires=get("bank_expires_at") + created=get("created_at") + return { + "id":f"app:{get('inbox_id')}", + "record_id":str(get("inbox_id")), + "bank_source":"silver_medalist", + "name":name, + "email":get("email") or None, + "phone":get("phone") or None, + "file_name":get("file_name") or None, + "file_path":get("file_path") or None, + "linkedin_url":get("linkedin_url") or None, + "current_company":get("current_company") or None, + "current_position":get("current_title") or None, + "education":get("education") or None, + # Inbox applications never ran the skills extraction — their structured + # signal is the ATS score, which is stronger than a keyword list. + "skills":list(row.get("matched_keywords") or []), + "years_experience":get("years_experience"), + "ai_score":get("ai_score"), + "recommendation":get("recommendation"), + "rank_score":rank_score, + "last_job_title":get("last_job_title") or None, + "bank_reason":"silver_medalist", + "bank_expires_at":expires.isoformat() if hasattr(expires,"isoformat") else expires, + "user_id":str(get("user_id")) if get("user_id") else None, + "assigned_job_post_id":None, + "created_at":created.isoformat() if hasattr(created,"isoformat") else created, + "updated_at":None, + } + + def serialize_manual_upload_candidate(row) -> Dict[str,Any]: return { "id":str(row.id) if row.id else None, diff --git a/backend/job/candidate/views.py b/backend/job/candidate/views.py index 92c9b2d..023e8ce 100644 --- a/backend/job/candidate/views.py +++ b/backend/job/candidate/views.py @@ -209,6 +209,101 @@ async def parse_linkedin_url_from_cv(resume_text) -> str | None: logger.exception("employment agent linkedin_url parse failed") return None + +async def extract_bank_profile_from_cv(resume_text) -> dict: + """Full employment-agent profile for a banked CV. + + parse_linkedin_url_from_cv runs this same agent and keeps only the URL, + which left the bank with nothing to search on. Banking is the one ingest + path with no job attached, so this extraction is the ONLY structured data + the CV will ever have until someone scores it against a real job. + + Never raises: a failed extraction must still bank the file. Sentinels + normalize to "" / None so "not stated" stays distinguishable from a value. + """ + blank={ + "linkedin_url":None,"current_company":"","current_position":"", + "education":"","candidate_phone":"","skills":[],"years_experience":None, + } + text=(resume_text or "").strip() + if not text: + return blank + try: + from employment_agent.execute_agent import run_employment_agent + from employment_agent.plugins import parse_linkedin + from employment_agent.prompt import CURRENT_TITLE,EDUCATION,NO_COMPANY + fields=await run_employment_agent(resume_text=text) + except Exception: + logger.exception("employment agent bank profile extraction failed") + return blank + + def unless_sentinel(key,sentinel): + value=(fields.get(key) or "").strip() + return "" if not value or value.lower()==sentinel.lower() else value + + try: + url=parse_linkedin({"linkedin_url":fields.get("linkedin_url") or ""},text).get("linkedin_url") + except Exception: + url=None + years=fields.get("years_experience") + return { + "linkedin_url":url, + "current_company":unless_sentinel("current_employment",NO_COMPANY), + "current_position":unless_sentinel("current_title",CURRENT_TITLE), + "education":unless_sentinel("education",EDUCATION), + "candidate_phone":(fields.get("phone") or "").strip(), + "skills":fields.get("skills") if isinstance(fields.get("skills"),list) else [], + "years_experience":years if isinstance(years,int) else None, + } + +def _bank_row_matches(row,*,search=None,skills=None,min_years=None,band=None) -> bool: + """Client-side facets for the merged bank list. + + The two populations live in different tables, so these cannot be one WHERE + clause; they run over the merged page instead. + """ + if search and str(search).strip(): + needle=str(search).strip().lower() + haystack=" ".join(str(v or "") for v in ( + row.get("name"),row.get("email"),row.get("current_company"), + row.get("current_position"),row.get("last_job_title"), + " ".join(row.get("skills") or []), + )).lower() + if needle not in haystack: + return False + if skills: + owned={s.lower() for s in (row.get("skills") or [])} + # Every requested skill must be present: filters narrow, they do not widen. + for wanted in skills: + key=str(wanted).strip().lower() + if key and not any(key in owned_skill for owned_skill in owned): + return False + if min_years is not None: + years=row.get("years_experience") + if years is None or years None: + """Newest first, best score first, and best job-rank first when ranking. + + Three stable passes rather than one composite key: created_at is an ISO + string and cannot be negated into a descending tuple slot. + """ + rows.sort(key=lambda r:str(r.get("created_at") or ""),reverse=True) + rows.sort(key=lambda r:r.get("ai_score") if isinstance(r.get("ai_score"),(int,float)) else -1,reverse=True) + if ranked: + rows.sort(key=lambda r:r.get("rank_score") if isinstance(r.get("rank_score"),(int,float)) else -1,reverse=True) + + class FileRead: def __init__(self,session:AsyncSession,filename=None,file=None): self.session=session @@ -524,6 +619,60 @@ class CandidateScoring: raise HTTPException(status_code=400,detail="No attachments found for the given message(s)") return await self._score_and_persist(job_id,sources,"inbox",current_user) + async def score_bank(self,job_id,record_ids,current_user): + """ATS-score CVs already sitting in the bank — tier 2, the paid step. + + The bank's automatic ranking is keyword overlap and says nothing about + whether anyone is actually qualified. This is the real score, and it is + deliberately explicit: a recruiter picks the handful worth paying for + rather than the whole bank being scored against every new job. + + Bytes come from cv_bank_files (the CV is already stored, so there is + nothing to re-upload), falling back to S3 for rows banked before the + bytes were kept in the database. + """ + from inbox.plugins import load_file_bytes + from job.candidate.models import CvBankFiles + + settings=get_scoring_settings() + ids=[str(r) for r in (record_ids or [])] + if not ids: + raise HTTPException(status_code=400,detail="Select at least one CV to score") + if len(ids)>settings.max_resumes_per_request: + raise HTTPException( + status_code=413, + detail=f"At most {settings.max_resumes_per_request} CVs per request", + ) + sources=[] + for record_id in ids: + row=await Manual_UPLOAD_CANDIDATE.get_by_id(self.session,record_id) + if row is None or row.apply_via!="cv_bank": + raise HTTPException(status_code=404,detail=f"CV {record_id} not found in the bank") + name=(row.file_name or "").strip() or "resume.pdf" + source={ + "filename":name, + "data":None, + "file_path":(row.file_path or "").strip() or None, + "candidate_email":(row.candidate_email or "").strip().lower() or None, + "manual_upload_candidate_id":row.id, + "precheck":None, + } + file_row=await CvBankFiles.get(self.session,row.id) + data=file_row.data if file_row and file_row.data else None + if data is None and source["file_path"]: + try: + data=await asyncio.to_thread(load_file_bytes,source["file_path"]) + except Exception: + data=None + if data is None: + source["precheck"]=(FILE_NOT_FOUND,"The stored CV could not be loaded.") + elif len(data)>settings.max_pdf_size_bytes: + source["precheck"]=(ErrorCode.PAYLOAD_TOO_LARGE,"The file exceeds the size limit.") + else: + source["data"]=data + sources.append(source) + return await self._score_and_persist(job_id,sources,"bank",current_user) + async def fetch_candidates(self,job_id=None,limit=10,offset=0): # job_id omitted -> the whole pool across jobs (frontend Candidates/TalentPool). if job_id is not None: @@ -1319,6 +1468,89 @@ class CandidateView: name=(entry.get("name") or path.name).strip() or path.name return path,name + # The bank holds two populations that live in different tables, so paging + # cannot happen in SQL. Both are read up to this cap, merged, filtered, and + # paged in Python. A bank larger than this needs a materialized view, not a + # bigger number. + BANK_SCAN_CAP=2000 + + async def list_bank(self,*,source=None,search=None,skills=None,min_years=None, + band=None,job_post_id=None,limit=50,offset=0): + """The unified CV Bank: speculative uploads plus scored rejections. + + job_post_id does not filter — it attaches the tier-1 rank_score for + that job and sorts by it, which is how a recruiter "pulls from" the + bank when an opening appears. + """ + from inbox.models import Inbox + from job.candidate.serializers import serialize_bank_candidate,serialize_bank_silver_medalist + + rows=[] + if source in (None,"","speculative"): + bank_rows,_=await Manual_UPLOAD_CANDIDATE.list_bank( + self.session,limit=self.BANK_SCAN_CAP,offset=0, + ) + rows.extend(serialize_bank_candidate(r) for r in bank_rows) + if source in (None,"","silver_medalist"): + medalists=await Inbox.list_silver_medalists( + self.session,min_score=self._silver_floor(),limit=self.BANK_SCAN_CAP, + ) + rows.extend(serialize_bank_silver_medalist(r) for r in medalists) + + if job_post_id: + rows=await self._attach_rank_scores(rows,job_post_id) + + rows=[r for r in rows if _bank_row_matches(r,search=search,skills=skills, + min_years=min_years,band=band)] + _sort_bank_rows(rows,ranked=bool(job_post_id)) + total=len(rows) + return rows[offset:offset+limit],total + + @staticmethod + def _silver_floor(): + import os + + return int(os.getenv("CV_BANK_SILVER_FLOOR","60")) + + async def _attach_rank_scores(self,rows,job_post_id): + """Fill rank_score from the stored tier-1 ranking for one job. + + Speculative rows are ranked by the background task. Silver medalists + are ranked here, in-process: they are read live and never had a row in + cv_bank_matches to begin with. + """ + from job.candidate.models import CvBankMatches + from matching.ranking import rank_profile + + job=await JobPosts.get_job_post_by_id(self.session,str(job_post_id)) + if not job: + return rows + job_fields={ + "title":job.title, + "requirements":job.requirements, + "optional_skills":job.optional_skills, + } + stored=await CvBankMatches.scores_for_job(self.session,job.id) + for row in rows: + if row["bank_source"]=="speculative": + row["rank_score"]=stored.get(row["record_id"]) + if row["rank_score"] is None: + # Banked after the job opened, so the task never saw it. + row["rank_score"]=rank_profile(job_fields,{ + "current_title":row.get("current_position"), + "headline":row.get("current_company"), + "skills":row.get("skills") or [], + "summary":None, + }) + else: + row["rank_score"]=rank_profile(job_fields,{ + "current_title":row.get("current_position"), + "headline":row.get("current_company"), + "skills":row.get("skills") or [], + "summary":None, + }) + return rows + async def list_matching(self,assigned=None,search=None,limit=10,offset=0): rows,total=await Manual_UPLOAD_CANDIDATE.list_matching( self.session,assigned=assigned,search=search,limit=limit,offset=offset, diff --git a/backend/job/job_post/views.py b/backend/job/job_post/views.py index de109e5..bd28518 100644 --- a/backend/job/job_post/views.py +++ b/backend/job/job_post/views.py @@ -191,6 +191,12 @@ class JobPost: if rec: await assignment.record_job_owner(row.id,rec.id,"primary_recruiter",assigned_by) + # A new opening is the moment the CV Bank is worth reading. Ranking it + # here is what turns the bank from a pile someone has to remember into + # something that offers itself up. Fire-and-forget: the job is already + # created, and a queue that is down must not fail the request. + await self._rank_cv_bank(row.id) + if not publish: return serialize_job_post(row) @@ -217,6 +223,25 @@ class JobPost: ) return serialize_job_post(saved) + async def _rank_cv_bank(self,job_post_id): + """Queue the tier-1 rank of every banked CV against a brand-new job. + + Best effort by design: this is a convenience signal, not part of + creating the job post. Redis being unavailable must not turn a + successful job creation into a 500. + """ + try: + from datetime import datetime as _dt,timezone as _tz + + from job.candidate.bank_tasks import rank_bank_for_job + await rank_bank_for_job.kicker().with_labels( + created_at=_dt.now(_tz.utc).isoformat(), + correlation_id=str(job_post_id), + queue="inbox", + ).kiq(str(job_post_id)) + except Exception as exc: + logger.warning("cv-bank rank not queued for job %s: %s",job_post_id,exc) + async def list_channels(self): try: return await list_buffer_channels() diff --git a/backend/matching/__init__.py b/backend/matching/__init__.py new file mode 100644 index 0000000..70702ef --- /dev/null +++ b/backend/matching/__init__.py @@ -0,0 +1,5 @@ +"""Shared deterministic matching — provider-free, no LLM, no database. + +Pure functions only, so both Find Talent (LinkedIn profiles) and the CV Bank +(stored resumes) rank against a job with the same arithmetic. +""" diff --git a/backend/matching/ranking.py b/backend/matching/ranking.py new file mode 100644 index 0000000..fb5c795 --- /dev/null +++ b/backend/matching/ranking.py @@ -0,0 +1,121 @@ +"""Deterministic job-fit ranking, shared by Find Talent and the CV Bank. + +This arithmetic started life in talent/plugins.py for LinkedIn profiles. The CV +Bank needs the same thing for stored resumes, and two copies of a scoring rule +drift: one gets tuned against live data and the other quietly does not. So the +implementation lives here and talent/plugins.py re-exports it. + +What this is NOT: an ATS score. There is no comprehension here, only token +overlap. It orders a pile of CVs so a recruiter can start at the top; it does +not judge whether anyone is qualified. The paid OpenAI score does that, and +only for the handful a human decides to shortlist. + +Pure module: no FastAPI, no database, no I/O. +""" + +from __future__ import annotations + +import re + +_TOKEN_STOPWORDS = { + "and", "or", "the", "of", "for", "with", "in", "a", "an", "to", + # Requirement-prose filler that appears in almost every profile and would + # inflate every score equally, flattening the ranking. + "experience", "years", "year", "strong", "including", "ability", + "knowledge", "skills", "understanding", "familiarity", "proficiency", + "hands", "must", "have", "plus", "good", "excellent", "etc", +} + +# A resume's full text is mostly prose; feeding all of it to the token overlap +# would match half the dictionary and flatten every score toward the ceiling. +# Only a lead excerpt is used, which in practice is the summary/skills header. +RESUME_EXCERPT_CHARS = 1200 + + +def _clean_phrase(text) -> str: + cleaned = re.sub(r"[^a-z0-9+#]+", " ", str(text or "").lower()) + return " ".join( + t for t in cleaned.split() if len(t) > 1 and t not in _TOKEN_STOPWORDS + ) + + +def _match_tokens(*texts) -> set[str]: + tokens: set[str] = set() + for text in texts: + tokens.update(_clean_phrase(text).split()) + return tokens + + +def rank_profile(job: dict, profile: dict) -> int: + """0-100 job-fit rank for sorting, computed when a profile is persisted. + + Deterministic and free. Title component: a current title CONTAINING every + job-title token scores 55 — containment, not exact phrase, because job + titles rarely reappear verbatim ("Generative Engineer" vs the pool's + "Generative AI Engineer"; seen live: the phrase rule dropped every real + match to the scattered tier and compressed the whole pool into the 40s). + The job title as an exact phrase in the headline scores 45; scattered + token overlap caps at 35 — a keyword-stuffed headline ("AI/ML Engineer | + Python | FastAPI | ...") must not outrank someone whose title IS the job + title, which is exactly what token overlap alone did on live data. The + headline tier stays phrase-only for the same reason: stuffed headlines + contain every token of every hot title. + + Skills component (up to 45): GRADED token overlap between the content + words of the job's requirements + optional skills and the person's + title/headline/skills/summary. Graded, not per-term all-or-nothing: the + title facet makes every sourced profile earn the same title points, so + all differentiation lives here — an all-or-nothing single term put a + whole live pool on exactly 60. + """ + job_title = _clean_phrase(job.get("title")) + job_title_tokens = set(job_title.split()) + title_text = _clean_phrase(profile.get("current_title")) + headline_text = _clean_phrase(profile.get("headline")) + if job_title and job_title_tokens <= set(title_text.split()): + title_component = 55.0 + elif job_title and job_title in headline_text: + title_component = 45.0 + else: + role_tokens = set(title_text.split()) | set(headline_text.split()) + ratio = ( + len(job_title_tokens & role_tokens) / len(job_title_tokens) + if job_title_tokens + else 0.0 + ) + title_component = 35 * ratio + + job_tokens = _match_tokens( + *(job.get("requirements") or []), *(job.get("optional_skills") or []) + ) + profile_tokens = _match_tokens( + profile.get("current_title"), + profile.get("headline"), + " ".join(profile.get("skills") or []), + profile.get("summary"), + ) + skills_ratio = ( + len(job_tokens & profile_tokens) / len(job_tokens) if job_tokens else 0.0 + ) + + return round(title_component + 45 * skills_ratio) + + +def bank_row_as_profile(row) -> dict: + """Map a manual_upload_candidate bank row onto the profile shape. + + A resume has no headline, so the employer stands in for one: it is the + other short, title-adjacent string a person is described by. full_text is + the summary, truncated — see RESUME_EXCERPT_CHARS. + """ + return { + "current_title": getattr(row, "current_position", "") or "", + "headline": getattr(row, "current_company", "") or "", + "skills": list(getattr(row, "skills", None) or []), + "summary": (getattr(row, "full_text", "") or "")[:RESUME_EXCERPT_CHARS], + } + + +def rank_bank_row(job: dict, row) -> int: + """Tier-1 rank for one banked CV against one job.""" + return rank_profile(job, bank_row_as_profile(row)) diff --git a/backend/migrations/manual/029_cv_bank_profile.sql b/backend/migrations/manual/029_cv_bank_profile.sql new file mode 100644 index 0000000..57cd437 --- /dev/null +++ b/backend/migrations/manual/029_cv_bank_profile.sql @@ -0,0 +1,30 @@ +-- 029_cv_bank_profile.sql +-- Structured profile fields for banked CVs. Until now a bank row carried only +-- full_text, so the bank was write-only: you could store a CV but not search +-- or rank it. The employment agent already extracts these on upload; its +-- output was being discarded. +-- +-- bank_reason records WHY the CV is held (speculative / referral); bank_expires_at +-- gives the retention policy something to enforce. +-- Applied at startup by alembic_setup.run_manual_sql(). + +ALTER TABLE app.manual_upload_candidate + ADD COLUMN IF NOT EXISTS skills JSONB NOT NULL DEFAULT '[]'::jsonb, + ADD COLUMN IF NOT EXISTS years_experience INTEGER, + ADD COLUMN IF NOT EXISTS education TEXT NOT NULL DEFAULT '', + ADD COLUMN IF NOT EXISTS bank_reason TEXT NOT NULL DEFAULT '', + ADD COLUMN IF NOT EXISTS bank_expires_at TIMESTAMPTZ; + +-- Containment queries ("has React") need GIN; a btree on a JSONB array is useless. +CREATE INDEX IF NOT EXISTS ix_manual_upload_candidate_skills + ON app.manual_upload_candidate USING GIN (skills); + +CREATE INDEX IF NOT EXISTS ix_manual_upload_candidate_years_experience + ON app.manual_upload_candidate (years_experience); + +-- Rows banked before this migration were all speculative uploads: the only +-- writer of apply_via='cv_bank' is POST /candidate/cv-bank/upload. +UPDATE app.manual_upload_candidate +SET bank_reason = 'speculative' +WHERE apply_via = 'cv_bank' + AND COALESCE(TRIM(bank_reason), '') = ''; diff --git a/backend/migrations/manual/030_cv_bank_matches.sql b/backend/migrations/manual/030_cv_bank_matches.sql new file mode 100644 index 0000000..956779b --- /dev/null +++ b/backend/migrations/manual/030_cv_bank_matches.sql @@ -0,0 +1,25 @@ +-- 030_cv_bank_matches.sql +-- Tier-1 ranking of banked CVs against a job post. +-- +-- Computed by the cvbank.rank_for_job task when a job opens, not on read: the +-- point is to notify a recruiter that the bank already holds candidates, and a +-- notification needs a result that exists before anyone opens the screen. +-- +-- rank_score is deterministic keyword overlap (matching/ranking.py), NOT an ATS +-- score. Cheap enough to recompute for the whole bank on every job opening. +-- Applied at startup by alembic_setup.run_manual_sql(). + +CREATE TABLE IF NOT EXISTS app.cv_bank_matches ( + id UUID PRIMARY KEY, + manual_upload_candidate_id UUID NOT NULL + REFERENCES app.manual_upload_candidate (id) ON DELETE CASCADE, + job_post_id UUID NOT NULL + REFERENCES app.job_posts (id) ON DELETE CASCADE, + rank_score INTEGER NOT NULL DEFAULT 0, + computed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), + CONSTRAINT uq_cv_bank_matches_pair UNIQUE (manual_upload_candidate_id, job_post_id) +); + +-- The read is always "best candidates for THIS job", so the job leads. +CREATE INDEX IF NOT EXISTS ix_cv_bank_matches_job_rank + ON app.cv_bank_matches (job_post_id, rank_score DESC); diff --git a/backend/talent/plugins.py b/backend/talent/plugins.py index 89c6229..a90ffa3 100644 --- a/backend/talent/plugins.py +++ b/backend/talent/plugins.py @@ -13,12 +13,13 @@ normalize_profile. from __future__ import annotations import os -import re from urllib.parse import urlsplit import httpx from dotenv import load_dotenv +from matching.ranking import rank_profile + load_dotenv() # The user's console .env entry is APIFY_TOKEN; APIFY_API_TOKEN is the documented name. @@ -437,83 +438,11 @@ def _current_position(item: dict) -> tuple[str | None, str | None]: return None, _first_string(item, "companyName", "currentCompany") -_TOKEN_STOPWORDS = { - "and", "or", "the", "of", "for", "with", "in", "a", "an", "to", - # Requirement-prose filler that appears in almost every profile and would - # inflate every score equally, flattening the ranking. - "experience", "years", "year", "strong", "including", "ability", - "knowledge", "skills", "understanding", "familiarity", "proficiency", - "hands", "must", "have", "plus", "good", "excellent", "etc", -} - - -def _clean_phrase(text) -> str: - cleaned = re.sub(r"[^a-z0-9+#]+", " ", str(text or "").lower()) - return " ".join( - t for t in cleaned.split() if len(t) > 1 and t not in _TOKEN_STOPWORDS - ) - - -def _match_tokens(*texts) -> set[str]: - tokens: set[str] = set() - for text in texts: - tokens.update(_clean_phrase(text).split()) - return tokens - - -def relevance_score(job: dict, profile: dict) -> int: - """0-100 job-fit rank for sorting, computed when a profile is persisted. - - Deterministic and free. Title component: a current title CONTAINING every - job-title token scores 55 — containment, not exact phrase, because job - titles rarely reappear verbatim ("Generative Engineer" vs the pool's - "Generative AI Engineer"; seen live: the phrase rule dropped every real - match to the scattered tier and compressed the whole pool into the 40s). - The job title as an exact phrase in the headline scores 45; scattered - token overlap caps at 35 — a keyword-stuffed headline ("AI/ML Engineer | - Python | FastAPI | ...") must not outrank someone whose title IS the job - title, which is exactly what token overlap alone did on live data. The - headline tier stays phrase-only for the same reason: stuffed headlines - contain every token of every hot title. - - Skills component (up to 45): GRADED token overlap between the content - words of the job's requirements + optional skills and the person's - title/headline/skills/summary. Graded, not per-term all-or-nothing: the - title facet makes every sourced profile earn the same title points, so - all differentiation lives here — an all-or-nothing single term put a - whole live pool on exactly 60. - """ - job_title = _clean_phrase(job.get("title")) - job_title_tokens = set(job_title.split()) - title_text = _clean_phrase(profile.get("current_title")) - headline_text = _clean_phrase(profile.get("headline")) - if job_title and job_title_tokens <= set(title_text.split()): - title_component = 55.0 - elif job_title and job_title in headline_text: - title_component = 45.0 - else: - role_tokens = set(title_text.split()) | set(headline_text.split()) - ratio = ( - len(job_title_tokens & role_tokens) / len(job_title_tokens) - if job_title_tokens - else 0.0 - ) - title_component = 35 * ratio - - job_tokens = _match_tokens( - *(job.get("requirements") or []), *(job.get("optional_skills") or []) - ) - profile_tokens = _match_tokens( - profile.get("current_title"), - profile.get("headline"), - " ".join(profile.get("skills") or []), - profile.get("summary"), - ) - skills_ratio = ( - len(job_tokens & profile_tokens) / len(job_tokens) if job_tokens else 0.0 - ) - - return round(title_component + 45 * skills_ratio) +# Moved to matching/ranking.py so the CV Bank ranks stored resumes with the +# same arithmetic instead of growing a second copy that drifts. Re-exported +# under the original name: every call site here and in talent/views.py is +# unchanged, and the numbers this produces are identical. +relevance_score = rank_profile def _date_text(value) -> str | None: diff --git a/backend/taskiq_management/broker_setup.py b/backend/taskiq_management/broker_setup.py index 931634c..318665f 100644 --- a/backend/taskiq_management/broker_setup.py +++ b/backend/taskiq_management/broker_setup.py @@ -1,6 +1,6 @@ """Taskiq broker — Redis Streams + smart retry + DLQ. -Worker: taskiq worker taskiq_management.broker_setup:broker inbox.tasks inbox.sync_tasks taskiq_management.tasks +Worker: taskiq worker taskiq_management.broker_setup:broker inbox.tasks inbox.sync_tasks taskiq_management.tasks job.candidate.bank_tasks Scheduler: taskiq scheduler taskiq_management.broker_setup:scheduler """ diff --git a/backend/tests/test_analytics_dashboard.py b/backend/tests/test_analytics_dashboard.py new file mode 100644 index 0000000..ca85489 --- /dev/null +++ b/backend/tests/test_analytics_dashboard.py @@ -0,0 +1,98 @@ +"""Pure-logic tests for the dashboard's applications-per-job aggregate. + +No DB: the SQL group-bys live on the models, but every decision this endpoint +makes — summing the two sources, zero-filling open reqs without resurrecting +dead postings, ordering, capping — is in _merge_job_counts and the serializer, +which is what the dashboard's numbers stand on. +""" + +from __future__ import annotations + +from types import SimpleNamespace +from uuid import uuid4 + +from analytics.serializers import serialize_job_application_count +from analytics.views import _merge_job_counts + + +def _job(title="Backend Engineer", status="open", **overrides): + fields = { + "id": uuid4(), + "title": title, + "department": "Engineering", + "requisition_status": status, + "vacancies": 2, + "is_active": True, + } + fields.update(overrides) + return SimpleNamespace(**fields) + + +# ---------------------------------------------------------------- serializer + +def test_serializer_coerces_and_stringifies(): + job = _job(vacancies=None, department=None) + row = serialize_job_application_count(job, None) + assert row["job_post_id"] == str(job.id) + assert row["count"] == 0 + assert row["vacancies"] == 0 + assert row["department"] == "" + assert row["is_active"] is True + + +# ---------------------------------------------------------------- merge + +def test_merge_sums_both_sources(): + job = _job() + key = str(job.id) + rows = _merge_job_counts({key: 3}, {key: 2}, [job], [], 10) + assert len(rows) == 1 + assert rows[0]["count"] == 5 + + +def test_merge_includes_zero_application_open_reqs(): + starving = _job(title="Unloved Role") + rows = _merge_job_counts({}, {}, [], [starving], 10) + assert len(rows) == 1 + assert rows[0]["count"] == 0 + assert rows[0]["title"] == "Unloved Role" + + +def test_merge_never_resurrects_closed_jobs_without_counts(): + # A closed job appears only when it actually received applications: + # it arrives via job_rows (it had counts), never via the open-req fill. + closed_with_apps = _job(title="Closed But Applied", status="closed", is_active=False) + rows = _merge_job_counts({str(closed_with_apps.id): 4}, {}, [closed_with_apps], [], 10) + assert [r["title"] for r in rows] == ["Closed But Applied"] + + # No counts and not open -> absent entirely (it is in neither input). + rows = _merge_job_counts({}, {}, [], [], 10) + assert rows == [] + + +def test_merge_drops_counts_for_unknown_jobs(): + # A count whose job row could not be loaded must not crash or emit a row. + rows = _merge_job_counts({str(uuid4()): 7}, {}, [], [], 10) + assert rows == [] + + +def test_merge_orders_by_count_desc_then_title_and_caps(): + a = _job(title="Alpha") + b = _job(title="beta") + c = _job(title="Zeta") + d = _job(title="Delta") + counts = {str(a.id): 1, str(c.id): 5, str(d.id): 1} + rows = _merge_job_counts(counts, {}, [a, c, d], [b], 3) + # count desc; ties by title case-insensitively; zero rows last; capped at 3. + assert [r["title"] for r in rows] == ["Zeta", "Alpha", "Delta"] + + rows = _merge_job_counts(counts, {}, [a, c, d], [b], 10) + assert [r["title"] for r in rows] == ["Zeta", "Alpha", "Delta", "beta"] + + +def test_merge_top_survives_garbage(): + job = _job() + rows = _merge_job_counts({str(job.id): 1}, {}, [job], [], None) + assert len(rows) == 1 + rows = _merge_job_counts({str(job.id): 1}, {}, [job], [], 0) + assert len(rows) == 1 diff --git a/backend/tests/test_cv_bank_ranking.py b/backend/tests/test_cv_bank_ranking.py new file mode 100644 index 0000000..90b980a --- /dev/null +++ b/backend/tests/test_cv_bank_ranking.py @@ -0,0 +1,133 @@ +"""matching/ranking.py — the shared tier-1 ranker. + +Two things are being protected here: + + 1. Find Talent's numbers did not change when relevance_score moved out of + talent/plugins.py. The scoring tiers were tuned against live LinkedIn + pools, so a silent shift would be a regression nobody would notice until + the ordering looked wrong. + 2. A banked CV maps onto the same profile shape and therefore scores the + same as the equivalent sourced profile. +""" + +import pytest + +from matching.ranking import bank_row_as_profile, rank_bank_row, rank_profile +from talent.plugins import relevance_score + +JOB = { + "title": "Backend Engineer", + "requirements": ["Python", "FastAPI", "PostgreSQL"], + "optional_skills": ["Docker"], +} + + +class FakeBankRow: + """The columns bank_row_as_profile reads. Not a SQLModel — this test must + not need a database to check arithmetic.""" + + def __init__(self, *, current_position="", current_company="", skills=None, full_text=""): + self.current_position = current_position + self.current_company = current_company + self.skills = skills or [] + self.full_text = full_text + + +# -------------------------------------------------------------------------- +# Find Talent parity +# -------------------------------------------------------------------------- + +def test_relevance_score_is_the_shared_ranker(): + """talent/plugins.py re-exports rather than reimplements.""" + assert relevance_score is rank_profile + + +@pytest.mark.parametrize( + "profile", + [ + {"current_title": "Backend Engineer", "headline": "", "skills": [], "summary": None}, + {"current_title": "", "headline": "Backend Engineer | Python", "skills": [], "summary": None}, + {"current_title": "", "headline": "", "skills": ["Python", "FastAPI"], "summary": None}, + {"current_title": "Senior Backend Engineer", "headline": "", "skills": ["Python"], "summary": None}, + {"current_title": None, "headline": None, "skills": None, "summary": None}, + ], +) +def test_find_talent_call_shape_still_works(profile): + """The old call site passes exactly this shape, including Nones.""" + score = relevance_score(JOB, profile) + assert isinstance(score, int) + assert 0 <= score <= 100 + + +def test_title_containment_beats_headline_phrase(): + """The tuned tier order: title containment 55 > headline phrase 45 > overlap. + + This is the rule the live pool forced (a keyword-stuffed headline must not + outrank someone whose title IS the job title), so it is the one most worth + pinning. + """ + own_title = rank_profile(JOB, {"current_title": "Senior Backend Engineer", "headline": "", "skills": [], "summary": None}) + stuffed = rank_profile(JOB, {"current_title": "", "headline": "Backend Engineer | AI | ML", "skills": [], "summary": None}) + assert own_title > stuffed + + +def test_unrelated_profile_scores_low(): + score = rank_profile(JOB, { + "current_title": "Pastry Chef", + "headline": "Baking and patisserie", + "skills": ["Sourdough"], + "summary": None, + }) + assert score < 20 + + +def test_empty_job_does_not_crash_or_credit(): + assert rank_profile({}, {"current_title": "Backend Engineer", "skills": ["Python"]}) == 0 + + +# -------------------------------------------------------------------------- +# Banked CVs score identically to the equivalent sourced profile +# -------------------------------------------------------------------------- + +def test_bank_row_scores_the_same_as_the_equivalent_profile(): + row = FakeBankRow( + current_position="Backend Engineer", + current_company="Acme", + skills=["Python", "FastAPI", "PostgreSQL"], + full_text="Built services at Acme.", + ) + equivalent = { + "current_title": "Backend Engineer", + "headline": "Acme", + "skills": ["Python", "FastAPI", "PostgreSQL"], + "summary": "Built services at Acme.", + } + assert rank_bank_row(JOB, row) == rank_profile(JOB, equivalent) + + +def test_bank_mapping_uses_company_as_the_headline(): + """A resume has no headline; the employer is the nearest equivalent.""" + profile = bank_row_as_profile(FakeBankRow(current_position="Engineer", current_company="Acme")) + assert profile["current_title"] == "Engineer" + assert profile["headline"] == "Acme" + + +def test_bank_mapping_truncates_full_text(): + """Feeding a whole resume to the token overlap would flatten every score.""" + from matching.ranking import RESUME_EXCERPT_CHARS + + profile = bank_row_as_profile(FakeBankRow(full_text="x" * (RESUME_EXCERPT_CHARS + 500))) + assert len(profile["summary"]) == RESUME_EXCERPT_CHARS + + +def test_bank_mapping_survives_missing_columns(): + """A CV banked before extraction existed has no skills and no title.""" + profile = bank_row_as_profile(FakeBankRow()) + assert profile == {"current_title": "", "headline": "", "skills": [], "summary": ""} + assert rank_bank_row(JOB, FakeBankRow()) == 0 + + +def test_skills_only_bank_row_still_ranks(): + """Extraction is what makes an untitled CV rankable at all.""" + row = FakeBankRow(skills=["Python", "FastAPI", "PostgreSQL", "Docker"]) + assert rank_bank_row(JOB, row) > 0 diff --git a/backend/tests/test_employment_agent.py b/backend/tests/test_employment_agent.py index 56be7aa..4c33cf4 100644 --- a/backend/tests/test_employment_agent.py +++ b/backend/tests/test_employment_agent.py @@ -1,4 +1,9 @@ -"""employment_agent parse_employment_response — linkedin_url is an agent key.""" +"""employment_agent parse_employment_response — linkedin_url is an agent key. + +parse_employment_response returns a DICT. These tests used to unpack it +positionally, which silently read dict KEYS instead of values and asserted +against whatever the last key happened to be. +""" from __future__ import annotations @@ -7,7 +12,26 @@ from employment_agent.prompt import EDUCATION, NO_COMPANY, NO_LINKEDIN def test_parses_linkedin_url_key_separately(): - company, education, title, url = parse_employment_response( + # The resume must actually contain the slug: _clean_linkedin keeps a URL + # only when the CV evidences it, so a resume that never mentions LinkedIn + # correctly yields None however confident the model was. + fields = parse_employment_response( + { + "current_employment": "Acme", + "education": "BS CS", + "current_title": "Engineer", + "linkedin_url": "https://www.linkedin.com/in/jane-doe", + }, + "Acme BS CS Engineer https://www.linkedin.com/in/jane-doe", + ) + assert fields["current_employment"] == "Acme" + assert fields["education"] == "BS CS" + assert fields["current_title"] == "Engineer" + assert fields["linkedin_url"] == "https://www.linkedin.com/in/jane-doe" + + +def test_url_absent_from_the_resume_is_not_trusted(): + fields = parse_employment_response( { "current_employment": "Acme", "education": "BS CS", @@ -16,14 +40,11 @@ def test_parses_linkedin_url_key_separately(): }, "Acme BS CS Engineer", ) - assert company == "Acme" - assert education == "BS CS" - assert title == "Engineer" - assert url == "https://www.linkedin.com/in/jane-doe" + assert fields["linkedin_url"] is None def test_sentinel_and_non_linkedin_are_dropped(): - *_, url = parse_employment_response( + sentinel = parse_employment_response( { "current_employment": NO_COMPANY, "education": EDUCATION, @@ -32,8 +53,9 @@ def test_sentinel_and_non_linkedin_are_dropped(): }, "", ) - assert url is None - *_, github = parse_employment_response( + assert sentinel["linkedin_url"] is None + + github = parse_employment_response( { "current_employment": NO_COMPANY, "education": EDUCATION, @@ -42,11 +64,11 @@ def test_sentinel_and_non_linkedin_are_dropped(): }, "", ) - assert github is None + assert github["linkedin_url"] is None def test_adds_scheme_and_rejects_company_page(): - *_, url = parse_employment_response( + bare = parse_employment_response( { "current_employment": NO_COMPANY, "education": EDUCATION, @@ -55,8 +77,9 @@ def test_adds_scheme_and_rejects_company_page(): }, "", ) - assert url == "https://www.linkedin.com/in/jane-doe" - *_, company = parse_employment_response( + assert bare["linkedin_url"] == "https://www.linkedin.com/in/jane-doe" + + company_page = parse_employment_response( { "current_employment": NO_COMPANY, "education": EDUCATION, @@ -65,4 +88,4 @@ def test_adds_scheme_and_rejects_company_page(): }, "", ) - assert company is None + assert company_page["linkedin_url"] is None diff --git a/backend/tests/test_employment_extraction_clamps.py b/backend/tests/test_employment_extraction_clamps.py new file mode 100644 index 0000000..d4bdacd --- /dev/null +++ b/backend/tests/test_employment_extraction_clamps.py @@ -0,0 +1,165 @@ +"""employment_agent clamps for the new skills / years_experience fields. + +These are the CV Bank's only structured data, and the model is the only source, +so the clamps are what stop a hallucinated skill becoming a searchable fact. +""" + +from employment_agent.decorators import parse_employment_response +from employment_agent.prompt import CURRENT_TITLE, EDUCATION, NO_COMPANY + +RESUME = ( + "Ada Lovelace\n" + "Backend Engineer at Acme\n" + "Skills: Python, FastAPI, PostgreSQL, Docker\n" + "BS Computer Science\n" + "6 years of experience\n" +) + + +def parse(payload, resume_text=RESUME): + return parse_employment_response(payload, resume_text) + + +# -------------------------------------------------------------------------- +# Backwards compatibility — the inbox match path predates these fields +# -------------------------------------------------------------------------- + +def test_response_without_the_new_keys_still_parses(): + """An older or partial reply must not break inbox matching.""" + fields = parse({ + "current_employment": "Acme", + "education": "BS Computer Science", + "current_title": "Backend Engineer", + "linkedin_url": "", + "phone": "", + }) + assert fields["skills"] == [] + assert fields["years_experience"] is None + assert fields["current_employment"] == "Acme" + + +def test_non_list_skills_degrade_to_empty(): + assert parse({"skills": "Python, FastAPI"})["skills"] == [] + assert parse({"skills": None})["skills"] == [] + assert parse({"skills": {"a": 1}})["skills"] == [] + + +# -------------------------------------------------------------------------- +# skills +# -------------------------------------------------------------------------- + +def test_skills_present_in_the_resume_are_kept_with_their_own_spelling(): + fields = parse({"skills": ["Python", "FastAPI", "PostgreSQL"]}) + assert fields["skills"] == ["Python", "FastAPI", "PostgreSQL"] + + +def test_fabricated_skills_are_dropped(): + """The model crediting Kubernetes to a CV that never mentions it is the + exact defect this clamp exists for.""" + fields = parse({"skills": ["Python", "Kubernetes", "Terraform"]}) + assert fields["skills"] == ["Python"] + + +def test_skills_are_deduplicated_case_insensitively_keeping_first_spelling(): + fields = parse({"skills": ["Python", "python", "PYTHON", "FastAPI"]}) + assert fields["skills"] == ["Python", "FastAPI"] + + +def test_blank_and_whitespace_skills_are_removed(): + fields = parse({"skills": ["Python", "", " ", "\n", "FastAPI"]}) + assert fields["skills"] == ["Python", "FastAPI"] + + +def test_skills_are_trimmed_before_matching(): + fields = parse({"skills": [" Python ", " FastAPI"]}) + assert fields["skills"] == ["Python", "FastAPI"] + + +def test_dedup_runs_before_the_thirty_cap(): + """31 near-duplicates must collapse under the limit rather than push real + skills out of it — same ordering rule as ATSScore's keyword arrays.""" + resume = "Skills: " + ", ".join(f"skill{i}" for i in range(30)) + ", Python\n" + noisy = ["Python"] * 5 + [f"skill{i}" for i in range(30)] + fields = parse_employment_response({"skills": noisy}, resume) + assert len(fields["skills"]) == 30 + assert fields["skills"][0] == "Python" + assert fields["skills"].count("Python") == 1 + + +def test_skills_are_capped_at_thirty(): + resume = "Skills: " + ", ".join(f"skill{i}" for i in range(50)) + fields = parse_employment_response( + {"skills": [f"skill{i}" for i in range(50)]}, resume, + ) + assert len(fields["skills"]) == 30 + + +def test_sentence_length_entries_are_rejected(): + """A responsibility is not a skill; a 60-char ceiling keeps chips renderable.""" + long_entry = "Responsible for building and maintaining backend services at scale" + fields = parse_employment_response({"skills": [long_entry]}, long_entry) + assert fields["skills"] == [] + + +def test_non_string_entries_are_ignored(): + fields = parse({"skills": ["Python", 42, None, {"x": 1}, ["FastAPI"]]}) + assert fields["skills"] == ["Python"] + + +def test_skills_pass_through_when_there_is_no_resume_text_to_check_against(): + """Nothing to verify against is not evidence of fabrication.""" + fields = parse_employment_response({"skills": ["Python", "Kubernetes"]}, "") + assert fields["skills"] == ["Python", "Kubernetes"] + + +# -------------------------------------------------------------------------- +# years_experience +# -------------------------------------------------------------------------- + +def test_stated_years_are_kept(): + assert parse({"years_experience": 6})["years_experience"] == 6 + + +def test_zero_years_is_a_real_value(): + assert parse({"years_experience": 0})["years_experience"] == 0 + + +def test_years_are_bounded_at_sixty(): + assert parse({"years_experience": 61})["years_experience"] is None + assert parse({"years_experience": 60})["years_experience"] == 60 + + +def test_negative_years_are_rejected(): + assert parse({"years_experience": -3})["years_experience"] is None + + +def test_years_as_a_string_are_parsed(): + assert parse({"years_experience": "6"})["years_experience"] == 6 + assert parse({"years_experience": "6 years"})["years_experience"] == 6 + + +def test_unparseable_years_read_as_unknown_not_zero(): + """0 would sort the candidate as a fresh graduate; unknown must stay unknown.""" + for value in (None, "", "several", "many years", [], {}, True, False): + assert parse({"years_experience": value})["years_experience"] is None + + +def test_float_years_truncate_to_whole_years(): + assert parse({"years_experience": 6.8})["years_experience"] == 6 + + +# -------------------------------------------------------------------------- +# The pre-existing fields are unaffected by the new clamps +# -------------------------------------------------------------------------- + +def test_existing_sentinels_still_normalize(): + fields = parse({ + "current_employment": NO_COMPANY, + "education": EDUCATION, + "current_title": CURRENT_TITLE, + "skills": ["Python"], + "years_experience": 6, + }) + assert fields["current_employment"] == NO_COMPANY + assert fields["education"] == EDUCATION + assert fields["skills"] == ["Python"] diff --git a/backend/tests/test_form_data_filters.py b/backend/tests/test_form_data_filters.py new file mode 100644 index 0000000..9eef6e3 --- /dev/null +++ b/backend/tests/test_form_data_filters.py @@ -0,0 +1,108 @@ +"""Sheet Forms link filters, and the badge/list agreement they depend on. + +No database: `_filters` returns SQLAlchemy expressions, so compiling them to SQL +is enough to see exactly what would reach Postgres. + +The defect these guard against is quiet. A filter that drops the NULL rows, or a +badge query that ignores a filter the list applies, produces a screen that is +merely *wrong* rather than broken: plausible numbers above rows that contradict +them, and nothing in the logs. +""" + +from __future__ import annotations + +import pytest +from sqlalchemy.dialects import postgresql + +from g_sheet.models import FormData + + +def sql(*clauses) -> str: + """Clauses as one lowercase SQL string, literals inlined so patterns show.""" + return " AND ".join( + str(c.compile(dialect=postgresql.dialect(), compile_kwargs={"literal_binds": True})) + for c in clauses + ).lower() + + +class TestHasLinkedin: + def test_true_matches_both_domains(self): + out = sql(*FormData._filters(has_linkedin=True)) + assert "linkedin.com" in out + assert "lnkd.in" in out + assert "ilike" in out + + def test_false_keeps_the_rows_with_no_link_at_all(self): + # The whole point of "no LinkedIn" is the rows where profile_link is NULL. + # `NOT (NULL ILIKE ...)` is NULL, which WHERE discards, so without an + # explicit IS NULL arm this view would return nothing useful. + out = sql(*FormData._filters(has_linkedin=False)) + assert "profile_link is null" in out + assert "not" in out + assert "linkedin.com" in out + + def test_omitted_emits_nothing(self): + assert FormData._filters() == [] + assert "profile_link" not in sql(*FormData._filters(sheet="S")) + + +class TestHasResume: + def test_true_is_a_not_null_test(self): + out = sql(*FormData._filters(has_resume=True)) + assert "resume_link is not null" in out + + def test_false_is_a_null_test(self): + out = sql(*FormData._filters(has_resume=False)) + assert "resume_link is null" in out + + @pytest.mark.parametrize("value", [True, False]) + def test_no_empty_string_arm(self, value): + # _cell() stores a blank sheet cell as NULL, never "". An empty-string + # comparison here would be dead code implying otherwise. + assert "''" not in sql(*FormData._filters(has_resume=value)) + + +class TestListAndBadgesAgree: + """The badge-desync guard. + + count_processing is hand-written rather than built on _filters, so it is the + one place a new filter can silently fail to apply. These pin the contract + that both sides narrow on the same predicates. + """ + + FILTERS = {"sheet": "Sheet A", "search": "khan", "has_linkedin": True, "has_resume": False} + + def test_badge_predicates_match_the_list_predicates(self): + # Same call the list makes, minus the two that ARE the tabs. + assert sql(*FormData._filters(**self.FILTERS)) == sql(*FormData._filters(**self.FILTERS)) + + def test_every_filter_reaches_the_sql(self): + out = sql(*FormData._filters(**self.FILTERS)) + assert "sheet" in out + assert "khan" in out + assert "linkedin.com" in out + assert "resume_link is null" in out + + def test_tab_filters_are_not_part_of_the_badge_call(self): + # processing_state and is_duplicate ARE the tabs. If count_processing ever + # accepted them, each badge would count only its own tab and every badge + # would report the tab the user is already looking at. + import inspect + + params = inspect.signature(FormData.count_processing).parameters + assert "processing_state" not in params + assert "is_duplicate" not in params + for name in ("sheet", "search", "has_linkedin", "has_resume"): + assert name in params, f"count_processing should narrow on {name}" + + +class TestPlumbing: + @pytest.mark.parametrize( + "func", [FormData.fetch_form_data, FormData.count_form_data], + ) + def test_list_helpers_accept_the_new_filters(self, func): + import inspect + + params = inspect.signature(func).parameters + assert "has_linkedin" in params + assert "has_resume" in params diff --git a/backend/tests/test_manager_candidate_serialize.py b/backend/tests/test_manager_candidate_serialize.py new file mode 100644 index 0000000..60edf96 --- /dev/null +++ b/backend/tests/test_manager_candidate_serialize.py @@ -0,0 +1,58 @@ +"""serialize_manager_candidate now carries ATS score + band for the HM table.""" + +from job.candidate.serializers import serialize_manager_candidate + + +def test_manager_row_exposes_ats_from_nested_result(): + row = serialize_manager_candidate( + { + "inbox_id": 9, + "user_id": "11111111-1111-1111-1111-111111111111", + "name": "Ada", + "email": "ada@example.com", + "title": "Backend Engineer", + "application_status": "REJECTED", + "assigned_job_post_id": "22222222-2222-2222-2222-222222222222", + "created_at": "2026-09-03T10:00:00", + "ats_result": {"overall_score": 88.0, "band": "Strong Match"}, + }, + source="inbox", + ) + assert row["ai_score"] == 88.0 + assert row["recommendation"] == "Strong Match" + assert row["application_status"] == "REJECTED" + assert row["job_title"] == "Backend Engineer" + + +def test_manager_row_derives_band_when_missing(): + row = serialize_manager_candidate( + { + "id": "33333333-3333-3333-3333-333333333333", + "user_id": "44444444-4444-4444-4444-444444444444", + "candidate_email": "m@example.com", + "name": "Manual", + "title": "Brand Manager", + "application_status": "PENDING", + "job_post_id": "55555555-5555-5555-5555-555555555555", + "ats_result": {"overall_score": 70}, + }, + source="manual", + ) + assert row["ai_score"] == 70 + assert row["recommendation"] == "Potential Match" + + +def test_manager_row_unscored_stays_empty(): + row = serialize_manager_candidate( + { + "inbox_id": 1, + "user_id": "66666666-6666-6666-6666-666666666666", + "name": "New", + "email": "n@example.com", + "title": "Role", + "application_status": "CLOSED", + }, + source="inbox", + ) + assert row["ai_score"] is None + assert row["recommendation"] is None diff --git a/frontend/cvbank.test.mjs b/frontend/cvbank.test.mjs new file mode 100644 index 0000000..9c676bf --- /dev/null +++ b/frontend/cvbank.test.mjs @@ -0,0 +1,166 @@ +/** + * CV Bank mapper — the two populations, and the two numbers that must not be + * confused (free rank_score vs paid ai_score). + * + * node cvbank.test.mjs + */ +import { mkdtempSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { pathToFileURL } from 'node:url' + +import esbuild from 'esbuild' + +const outDir = mkdtempSync(join(tmpdir(), 'tf-bank-')) +const outFile = join(outDir, 'candidates.mjs') + +await esbuild.build({ + entryPoints: ['src/api/candidates.js'], + outfile: outFile, + bundle: true, + format: 'esm', + platform: 'node', + target: 'node20', + logLevel: 'error', + define: { 'import.meta.env': JSON.stringify({ VITE_API_BASE: '' }) }, +}) + +const { toBankRowView, expiryLabel } = await import(pathToFileURL(outFile).href) + +let failed = 0 +function ok(name, cond, extra) { + if (cond) { + console.log(`ok ${name}`) + } else { + failed += 1 + console.log(`FAIL ${name}`) + if (extra) console.log(` ${extra}`) + } +} + +/* --- a speculative upload: extracted, never scored ------------------------ */ + +const speculative = toBankRowView({ + id: 'bank:11111111-1111-1111-1111-111111111111', + record_id: '11111111-1111-1111-1111-111111111111', + bank_source: 'speculative', + name: 'Ada Lovelace', + email: 'ada@example.com', + phone: '0321-5551234', + file_name: 'ada.pdf', + file_path: 'https://s3/Temp/x/ada.pdf', + current_company: 'Acme', + current_position: 'Backend Engineer', + education: 'BS CS', + skills: ['Python', 'FastAPI', 'Docker'], + years_experience: 6, + ai_score: null, + recommendation: null, + rank_score: null, + bank_reason: 'speculative', + bank_expires_at: '2028-09-03T00:00:00Z', + created_at: '2026-09-03T10:00:00Z', +}) + +ok('speculative row keeps the prefixed list id', speculative.id === 'bank:11111111-1111-1111-1111-111111111111') +ok('recordId is the raw uuid the delete/file routes take', speculative.recordId === '11111111-1111-1111-1111-111111111111') +ok('source label is human', speculative.sourceLabel === 'Speculative') +ok('speculative rows are removable stored CVs', speculative.isStoredCv === true) +ok('extracted skills come through', speculative.skills.join(',') === 'Python,FastAPI,Docker') +ok('years is numeric', speculative.years === 6) +ok('an unscored CV has no ATS score', speculative.aiScore === null) +ok('and no invented band', speculative.recommendation === null) +ok('and no rank until a job is picked', speculative.rankScore === null) +ok('expiry parses to a Date', speculative.expiresAt instanceof Date) +ok('added parses to a Date', speculative.added instanceof Date) + +/* --- a silver medalist: scored, read live, not the bank's to delete ------- */ + +const silver = toBankRowView({ + id: 'app:42', + record_id: '42', + bank_source: 'silver_medalist', + name: 'Grace Hopper', + email: 'grace@example.com', + current_company: 'Navy', + current_position: 'Rear Admiral', + skills: ['COBOL', 'Compilers'], + years_experience: 20, + ai_score: 88, + recommendation: 'Strong Match', + last_job_title: 'Principal Engineer', + user_id: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', + created_at: '2026-08-01T10:00:00Z', +}) + +ok('silver medalist is keyed off the application', silver.id === 'app:42') +ok('silver medalist label', silver.sourceLabel === 'Silver medalist') +ok('silver medalist is NOT a stored CV, so the bank cannot delete it', silver.isStoredCv === false) +ok('paid ATS score survives', silver.aiScore === 88) +ok('band survives', silver.recommendation === 'Strong Match') +ok('the job they were rejected from is kept for context', silver.lastJobTitle === 'Principal Engineer') +ok('userId is kept so the row can open a real profile', silver.userId === 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa') + +/* --- rank_score is the free number, and separate from ai_score ----------- */ + +const ranked = toBankRowView({ + id: 'bank:2', + record_id: '2', + bank_source: 'speculative', + name: 'Ranked', + skills: [], + rank_score: 72, + ai_score: null, +}) +ok('rank_score maps without becoming an ATS score', ranked.rankScore === 72 && ranked.aiScore === null) + +const bothNumbers = toBankRowView({ + id: 'app:3', record_id: '3', bank_source: 'silver_medalist', + name: 'Both', rank_score: 61, ai_score: 84, +}) +ok('a row can carry both numbers independently', bothNumbers.rankScore === 61 && bothNumbers.aiScore === 84) + +/* --- absent values stay absent ------------------------------------------- */ + +const sparse = toBankRowView({ + id: 'bank:4', + record_id: '4', + bank_source: 'speculative', + file_name: 'unknown.pdf', +}) +ok('a nameless CV falls back rather than rendering blank', sparse.name === 'Unknown') +ok('no skills is an empty array, not null', Array.isArray(sparse.skills) && sparse.skills.length === 0) +ok('unknown years stays null, never 0', sparse.years === null) +ok('no expiry stays null', sparse.expiresAt === null) +ok('unknown source defaults to speculative', sparse.source === 'speculative') + +const zeroYears = toBankRowView({ + id: 'bank:5', record_id: '5', bank_source: 'speculative', + name: 'Fresh Grad', years_experience: 0, +}) +ok('0 years is a real value and must not collapse to null', zeroYears.years === 0) + +const derivedBand = toBankRowView({ + id: 'app:6', record_id: '6', bank_source: 'silver_medalist', + name: 'Derived', ai_score: 70, +}) +ok('a score without a band derives one', derivedBand.recommendation === 'Potential Match') + +/* --- expiry wording ------------------------------------------------------ */ + +const now = new Date('2026-09-03T00:00:00Z') +ok('no expiry has no label', expiryLabel(null, now) === null) +ok('months are counted forward', expiryLabel(new Date('2027-04-01T00:00:00Z'), now) === 'expires in 7 months') +ok('one month is singular', expiryLabel(new Date('2026-10-03T00:00:00Z'), now) === 'expires in 1 month') +ok( + 'a past window reads as expired, not a negative count', + expiryLabel(new Date('2026-01-01T00:00:00Z'), now) === 'expired', +) + +rmSync(outDir, { recursive: true, force: true }) + +if (failed) { + console.log(`\n${failed} check(s) failed`) + process.exit(1) +} +console.log('\nAll CV Bank mapper checks passed') diff --git a/frontend/dist/index.html b/frontend/dist/index.html index 9fdb59b..b6f287a 100644 --- a/frontend/dist/index.html +++ b/frontend/dist/index.html @@ -24,8 +24,8 @@ - - + +
diff --git a/frontend/inbox-loading.test.mjs b/frontend/inbox-loading.test.mjs index 18f276d..e68b450 100644 --- a/frontend/inbox-loading.test.mjs +++ b/frontend/inbox-loading.test.mjs @@ -85,6 +85,9 @@ const EMAIL_ROWS = [ }, ] +/** Matches DEFAULT_FORM_SHEET in Inbox.jsx, so the sheet picker resolves. */ +const FORM_SHEET = 'Form Responses - Candidate Database Sheet 2026' + const FORM_ROWS = [ { id: '33333333-3333-3333-3333-333333333333', @@ -123,14 +126,23 @@ const formGate = gate({ data: FORM_ROWS, total: FORM_ROWS.length, status_code: 2 const COUNTS = { all: 3, unread: 1, processed: 0, rejected: 0, duplicates: 0 } +/** Every URL the app asked for, so a test can assert on query params. */ +const REQUESTS = [] +const requestsMatching = (fragment) => REQUESTS.filter((u) => u.includes(fragment)) + globalThis.fetch = async (input) => { const url = String(input?.url ?? input) + REQUESTS.push(url) let body if (url.includes('/inbox/all-applications/count')) body = { total: EMAIL_ROWS.length, status_code: 200 } else if (url.includes('/inbox/all-applications')) body = await emailGate.take() else if (url.includes('/inbox/counts')) body = { data: COUNTS, status_code: 200 } + // '/counts' BEFORE '/count': the shorter path is a substring of the longer + // one, so testing it first swallowed every counts request and handed the tab + // badges a payload of the wrong shape. + else if (url.includes('/sheet/form-data/counts')) body = { data: { all: FORM_ROWS.length }, status_code: 200 } else if (url.includes('/sheet/form-data/count')) body = { total: FORM_ROWS.length, status_code: 200 } - else if (url.includes('/sheet/form-data/counts')) body = { data: { all: 1 }, status_code: 200 } + else if (url.includes('/sheet/form-data/sheets')) body = { data: { sheets: [FORM_SHEET] }, status_code: 200 } else if (url.includes('/sheet/form-data/fetch')) body = await formGate.take() else body = { data: [], status_code: 200 } return { @@ -297,6 +309,97 @@ try { `calls before=${firstVisitEmailCalls} after=${emailGate.calls}`, ) await revisit.unmount() + + // ---- Sheet Forms link filters ------------------------------------------- + // A Google Form pile has two questions worth asking of it in bulk: who gave + // us a LinkedIn, and who gave us a CV. Both answers were already in the + // database and neither was reachable from the screen. + const third = dom.window.document.createElement('div') + dom.window.document.body.appendChild(third) + const forms = await mod.mountRoute('/inbox', third) + + check( + 'no filter panel on the Email channel', + !forms.findByText('button', 'Filters'), + 'the link fields are sheet-only; email rows carry different columns', + ) + + REQUESTS.length = 0 + await forms.click(forms.findByText('.pill-tab', 'Sheet Forms')) + await forms.settle(60) + + // Captured BEFORE any filter is chosen. This is what makes the "clearing + // removes the params" claim further down mean something: an inactive filter + // has to be absent from the URL, not present and empty. + const unfiltered = requestsMatching('/sheet/form-data/fetch') + check( + 'an unset filter is absent from the request, not sent empty', + unfiltered.length > 0 + && unfiltered.every((u) => !u.includes('has_linkedin') && !u.includes('has_resume')), + unfiltered.slice(-1)[0] || 'no list request went out at all', + ) + + const toggle = forms.findByText('button', 'Filters') + check('the Sheet Forms channel offers a filter toggle', Boolean(toggle)) + + await forms.click(toggle) + const linkedinSelect = forms.find('#inbox-f-linkedin') + const resumeSelect = forms.find('#inbox-f-resume') + check( + 'opening it reveals both link filters', + Boolean(linkedinSelect) && Boolean(resumeSelect), + ) + check( + 'the LinkedIn filter admits what it can actually prove', + forms.text().includes('not a verified profile'), + ) + + REQUESTS.length = 0 + await forms.selectOption(linkedinSelect, 'yes') + await forms.settle(80) + + const listHits = requestsMatching('/sheet/form-data/fetch') + const countHits = requestsMatching('/sheet/form-data/counts') + check( + 'choosing a filter sends it to the list endpoint', + listHits.some((u) => u.includes('has_linkedin=true')), + listHits[listHits.length - 1] || 'no list request went out', + ) + check( + 'THE DESYNC GUARD: the tab badges are recounted with the same filter', + countHits.some((u) => u.includes('has_linkedin=true')), + countHits[countHits.length - 1] || 'no counts request went out', + ) + + // 'no' has to survive as a real filter. A checkbox would collapse it into + // "unset", and chasing the rows MISSING a link is half the point. + REQUESTS.length = 0 + await forms.selectOption(resumeSelect, 'no') + await forms.settle(80) + check( + 'a negative filter reaches the wire as false, not as omitted', + requestsMatching('/sheet/form-data/fetch').some((u) => u.includes('has_resume=false')), + requestsMatching('/sheet/form-data/fetch').slice(-1)[0] || 'no request', + ) + + check( + 'the toggle reports how many filters are hiding under it', + (forms.findByText('button', 'Filters')?.textContent || '').includes('(2)'), + `toggle reads: ${forms.findByText('button', 'Filters')?.textContent?.trim()}`, + ) + + await forms.click(forms.findByText('button', 'Clear')) + await forms.settle(80) + const clearedToggle = forms.findByText('button', 'Filters') + check( + 'clearing resets both selects and the toggle count', + !(clearedToggle?.textContent || '').includes('(') + && forms.find('#inbox-f-linkedin')?.value === '' + && forms.find('#inbox-f-resume')?.value === '', + `toggle reads: ${clearedToggle?.textContent?.trim()}`, + ) + + await forms.unmount() } finally { rmSync(outDir, { recursive: true, force: true }) } diff --git a/frontend/mobile.test.mjs b/frontend/mobile.test.mjs index 78bc00a..b9d6d8f 100644 --- a/frontend/mobile.test.mjs +++ b/frontend/mobile.test.mjs @@ -33,7 +33,7 @@ const PASSWORD = process.env.ATS_TEST_PASSWORD || 'Test12345!' // Keep in sync with src/app/routes.js (paths only — titles don't matter here). const ROUTES = [ - 'dashboard', 'inbox', 'matching', 'jobs', 'candidates', 'talentpool', 'pipeline', + 'dashboard', 'inbox', 'matching', 'jobs', 'candidates', 'cvbank', 'pipeline', 'progress', 'import', 'jobboard', 'recruiterhub', 'talent', 'tasks', 'aiassistant', 'interviews', 'requisitions', 'assessments', 'offers', 'managers', 'calendar', 'reports', 'analytics', 'aistudio', 'notifications', 'rbac', 'settings', 'help', diff --git a/frontend/package.json b/frontend/package.json index c3763f3..c3b0f70 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -12,8 +12,10 @@ "test:token": "node token.test.mjs", "test:theme": "node theme.test.mjs", "test:inbox": "node inbox-loading.test.mjs", + "test:candidates": "node candidates-table.test.mjs", + "test:cvbank": "node cvbank.test.mjs", "test:mobile": "node mobile.test.mjs", - "verify": "vite build && node smoke.test.mjs && node token.test.mjs && node theme.test.mjs && node inbox-loading.test.mjs" + "verify": "vite build && node smoke.test.mjs && node token.test.mjs && node theme.test.mjs && node inbox-loading.test.mjs && node candidates-table.test.mjs && node cvbank.test.mjs" }, "dependencies": { "@tanstack/react-query": "^5.101.4", diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 621f0d6..9c52297 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -20,7 +20,7 @@ const SCREENS = { matching: lazy(() => import('./screens/Matching')), jobs: lazy(() => import('./screens/Jobs')), candidates: lazy(() => import('./screens/Candidates')), - talentpool: lazy(() => import('./screens/TalentPool')), + cvbank: lazy(() => import('./screens/CvBank')), pipeline: lazy(() => import('./screens/Pipeline')), progress: lazy(() => import('./screens/Progress')), import: lazy(() => import('./screens/CvImport')), diff --git a/frontend/src/__smoke__/entry.jsx b/frontend/src/__smoke__/entry.jsx index aa7018f..e6287d4 100644 --- a/frontend/src/__smoke__/entry.jsx +++ b/frontend/src/__smoke__/entry.jsx @@ -28,7 +28,7 @@ import Inbox from '../screens/Inbox' import Matching from '../screens/Matching' import Jobs from '../screens/Jobs' import Candidates from '../screens/Candidates' -import TalentPool from '../screens/TalentPool' +import CvBank from '../screens/CvBank' import Pipeline from '../screens/Pipeline' import Progress from '../screens/Progress' import CvImport from '../screens/CvImport' @@ -53,7 +53,7 @@ import Help from '../screens/Help' const SCREENS = { dashboard: Dashboard, inbox: Inbox, matching: Matching, jobs: Jobs, candidates: Candidates, - talentpool: TalentPool, pipeline: Pipeline, progress: Progress, import: CvImport, jobboard: JobBoard, + cvbank: CvBank, pipeline: Pipeline, progress: Progress, import: CvImport, jobboard: JobBoard, recruiterhub: RecruiterHub, talent: Talent, tasks: Tasks, aiassistant: AiAssistant, interviews: Interviews, requisitions: Requisitions, assessments: Assessments, offers: Offers, managers: Managers, calendar: Calendar, reports: Reports, analytics: Analytics, @@ -95,10 +95,33 @@ export async function mountRoute(path, container) { await act(async () => { await new Promise((r) => setTimeout(r, ms)) }) } await settle() + + // Interaction helpers live here for the same reason `settle` does: act() has + // to be the bundle's React instance, not a second copy imported by the test. + const click = async (el) => { + if (!el) throw new Error('click: element not found') + await act(async () => { el.click() }) + await settle() + } + const selectOption = async (el, value) => { + if (!el) throw new Error('selectOption: element not found') + const Ev = el.ownerDocument.defaultView.Event + await act(async () => { + el.value = value + el.dispatchEvent(new Ev('change', { bubbles: true })) + }) + await settle() + } + return { settle, + click, + selectOption, html: () => container.innerHTML, text: () => container.textContent || '', + find: (selector) => container.querySelector(selector), + findByText: (selector, text) => [...container.querySelectorAll(selector)] + .find((el) => (el.textContent || '').includes(text)) || null, unmount: async () => { await act(async () => { root.unmount() }) }, } } diff --git a/frontend/src/api/candidates.js b/frontend/src/api/candidates.js index 4b9acec..92d83ec 100644 --- a/frontend/src/api/candidates.js +++ b/frontend/src/api/candidates.js @@ -55,9 +55,47 @@ export function uploadToCvBank(file) { return request('/candidate/cv-bank/upload', { method: 'POST', body: form }) } -/** The stored-CV bank, newest first — GET /candidate/cv-bank/fetch. */ -export function listCvBank({ top = 100, skip = 0 } = {}) { - return request('/candidate/cv-bank/fetch', { params: { top, skip } }) +/** + * The CV Bank — GET /candidate/cv-bank/fetch. Two populations in one list: + * speculative uploads with no job, and rejected applicants who scored well. + * + * `jobPostId` does NOT filter. It attaches rank_score (deterministic keyword + * overlap against that job) and sorts by it — the "a role just opened, who do + * we already have" view. + */ +export function listCvBank({ + top = 100, skip = 0, source, search, skills, minYears, band, jobPostId, +} = {}) { + return request('/candidate/cv-bank/fetch', { + params: { + top, skip, source, search, skills, + min_years: minYears, + band, + job_post_id: jobPostId, + }, + }) +} + +/** + * Banked CVs worth reviewing for one job, best first. Needs candidates.view. + * Same rows as listCvBank with a job context, already cut at the threshold. + */ +export function listCvBankSuggestions({ jobPostId, top = 20, minRank } = {}) { + return request('/candidate/cv-bank/suggestions', { + params: { job_post_id: jobPostId, top, min_rank: minRank }, + }) +} + +/** + * Run the real ATS score on CVs already in the bank. Needs candidates.create. + * This is the paid step — rank_score on the list is free keyword overlap and + * is not a score. Results land in candidates/ats_results like any other CV. + */ +export function scoreCvBank(jobId, ids) { + return request('/candidate/cv-bank/score', { + method: 'POST', + body: { job_id: jobId, ids }, + }) } /** Permanently remove a stored CV (file included). Needs candidates.delete. */ @@ -246,7 +284,80 @@ export function toApplicationListView(row) { } } +export const BANK_SOURCE_LABELS = { + speculative: 'Speculative', + silver_medalist: 'Silver medalist', +} +/** + * GET /candidate/cv-bank/fetch row -> the CV Bank table. + * + * Two numbers that must never be confused: `aiScore` is a real paid ATS score + * and only exists once someone ran one; `rankScore` is free keyword overlap + * against whichever job is selected. The screen renders them differently on + * purpose. + */ +export function toBankRowView(row) { + const score = row.ai_score == null || row.ai_score === '' ? null : Number(row.ai_score) + const aiScore = Number.isFinite(score) ? score : null + const rank = row.rank_score == null || row.rank_score === '' ? null : Number(row.rank_score) + const years = row.years_experience == null || row.years_experience === '' + ? null + : Number(row.years_experience) + const expires = row.bank_expires_at ? new Date(row.bank_expires_at) : null + return { + id: String(row.id || ''), + recordId: row.record_id != null ? String(row.record_id) : null, + source: row.bank_source || 'speculative', + sourceLabel: BANK_SOURCE_LABELS[row.bank_source] || 'Speculative', + // A silver medalist is read live from their application, so removing or + // re-scoring them is not the bank's call to make. + isStoredCv: row.bank_source !== 'silver_medalist', + name: row.name || row.email || 'Unknown', + email: row.email ?? null, + phone: row.phone ?? null, + fileName: row.file_name ?? null, + filePath: row.file_path ?? null, + linkedinUrl: row.linkedin_url ?? null, + company: row.current_company ?? null, + title: row.current_position ?? null, + education: row.education ?? null, + skills: Array.isArray(row.skills) ? row.skills : [], + years: Number.isFinite(years) ? years : null, + aiScore, + recommendation: bandOf(aiScore, row.recommendation || null), + rankScore: Number.isFinite(rank) ? rank : null, + lastJobTitle: row.last_job_title ?? null, + bankReason: row.bank_reason ?? null, + expiresAt: expires && !Number.isNaN(expires.getTime()) ? expires : null, + userId: row.user_id ?? null, + added: row.created_at ? new Date(row.created_at) : null, + } +} + +/** + * "expires in 7 months", or null when nothing is set. Past expiry reads as + * "expired" rather than a negative count — the row still exists and someone + * has to decide what to do about it. + */ +export function expiryLabel(expiresAt, now = new Date()) { + if (!expiresAt) return null + const months = Math.round((expiresAt - now) / (1000 * 60 * 60 * 24 * 30)) + if (months <= 0) return 'expired' + if (months === 1) return 'expires in 1 month' + return `expires in ${months} months` +} + +/** + * Candidate profiles — the `inbox -> users -> roles` join, restricted server-side + * to role_name == CANDIDATE (backend/inbox/models.py:get_candidate_profile). + * + * Permissioned with require_permission(CANDIDATES_VIEW), so a caller without the + * tag gets a 403. + * + * `search` is an ilike over users.name / users.email only — it does NOT reach + * the résumé text or the suggested job titles. + */ export function list({ search, limit, offset, assignedJobPostId } = {}) { return request('/candidate/fetch', { params: { search, limit, offset, assigned_job_post_id: assignedJobPostId }, diff --git a/frontend/src/api/sheet.js b/frontend/src/api/sheet.js index 99d18a0..29a436f 100644 --- a/frontend/src/api/sheet.js +++ b/frontend/src/api/sheet.js @@ -17,12 +17,20 @@ export function listFormDataSheets() { * * `offset` / `limit` map 1:1 to the backend Query params (not skip/top). * Optional `processing_state` / `is_duplicate` power the Sheet Forms tabs. + * + * `has_linkedin` / `has_resume` are tri-valued on the wire, the same convention + * `is_duplicate` uses: omit for no filter, false to find the rows MISSING the + * link. buildUrl drops undefined but keeps false, so `undefined` sends no param. */ export function listFormData({ sheet, search, offset = 0, limit, processing_state, is_duplicate, + hasLinkedin, hasResume, } = {}) { return request('/sheet/form-data/fetch', { - params: { sheet, search, offset, limit, processing_state, is_duplicate }, + params: { + sheet, search, offset, limit, processing_state, is_duplicate, + has_linkedin: hasLinkedin, has_resume: hasResume, + }, }) } @@ -31,9 +39,18 @@ export function countFormData({ sheet } = {}) { return request('/sheet/form-data/count', { params: { sheet } }) } -/** Tab badge counts for one sheet (or all sheets when sheet omitted). */ -export function fetchFormCounts({ sheet } = {}) { - return request('/sheet/form-data/counts', { params: { sheet } }) +/** + * Tab badge counts for one sheet (or all sheets when sheet omitted). + * + * Takes the same narrowing filters as the list, because a badge reading 612 + * above twelve visible rows reads as a bug. It deliberately does NOT take + * processing_state or is_duplicate: those two ARE the tabs, and passing them + * would make every badge report the tab the user is already on. + */ +export function fetchFormCounts({ sheet, search, hasLinkedin, hasResume } = {}) { + return request('/sheet/form-data/counts', { + params: { sheet, search, has_linkedin: hasLinkedin, has_resume: hasResume }, + }) } /** One form_data row by UUID. */ diff --git a/frontend/src/app/routes.js b/frontend/src/app/routes.js index b1c52b4..6ad2ad0 100644 --- a/frontend/src/app/routes.js +++ b/frontend/src/app/routes.js @@ -21,7 +21,12 @@ export const ROUTES = [ { path: 'matching', title: 'Job Matching', icon: 'target', group: 'Workspace', permission: 'candidates.view', badge: 'matching' }, { path: 'jobs', title: 'Jobs', icon: 'briefcase', group: 'Workspace', permission: 'jobs.view', badge: 'jobs' }, { path: 'candidates', title: 'Candidates', icon: 'users', group: 'Workspace', permission: 'candidates.view' }, - { path: 'talentpool', title: 'Talent Pool', icon: 'talent', group: 'Workspace', permission: 'candidates.view' }, + // Replaces Talent Pool. That screen browsed candidate accounts over a seed + // overlay of invented skills and companies; /candidates already does the real + // version of that. This one holds the people we have no job for yet. + // No `badge`: the matching badge counts the unassigned queue and reusing the + // key here would make the same rows read as two separate counts. + { path: 'cvbank', title: 'CV Bank', icon: 'talent', group: 'Workspace', permission: 'candidates.view' }, { path: 'pipeline', title: 'Pipeline', icon: 'pipeline', group: 'Workspace', permission: 'pipeline.view' }, { path: 'progress', title: 'Progress', icon: 'trending-up', group: 'Workspace', permission: 'jobs.view' }, diff --git a/frontend/src/auth/permissions.js b/frontend/src/auth/permissions.js index c42fe03..fb454bf 100644 --- a/frontend/src/auth/permissions.js +++ b/frontend/src/auth/permissions.js @@ -37,7 +37,7 @@ export function makeCan(permissions) { export const HIRING_MANAGER_ROLE = 'hiring_manager' -/** Sidebar paths a manager-type role may see. Talent Pool / Matching / Import +/** Sidebar paths a manager-type role may see. CV Bank / Matching / Import also sit on candidates.view/create, so they are excluded here. */ export const HIRING_MANAGER_NAV = new Set([ 'candidates', 'requisitions', 'interviews', 'calendar', diff --git a/frontend/src/lib/queryKeys.js b/frontend/src/lib/queryKeys.js index b906e0f..5a9ccbd 100644 --- a/frontend/src/lib/queryKeys.js +++ b/frontend/src/lib/queryKeys.js @@ -45,7 +45,8 @@ export const qk = { }, cvBank: { all: () => ['cvBank'], - list: () => ['cvBank', 'list'], + list: (p = {}) => ['cvBank', 'list', p], + suggestions: (jobId) => ['cvBank', 'suggestions', jobId], }, notifications: { all: () => ['notifications'], diff --git a/frontend/src/screens/Candidates.jsx b/frontend/src/screens/Candidates.jsx index f2fe9de..35a563e 100644 --- a/frontend/src/screens/Candidates.jsx +++ b/frontend/src/screens/Candidates.jsx @@ -788,9 +788,7 @@ function fmtStamp(value) { * (backend/job/candidate/views.py:782-792). * * The prop is the last fallback, for callers whose rows already carry a score - * (Talent Pool cards, the scored leaderboard). - * - * Exported so TalentPool's profile modal can open the same ATS breakdown. + * (the scored leaderboard). */ export function AtsMatch({ candidate: c, jobTitle, onClose, onProfile }) { const userId = c.userId ?? null diff --git a/frontend/src/screens/CvBank.jsx b/frontend/src/screens/CvBank.jsx new file mode 100644 index 0000000..70cf1bf --- /dev/null +++ b/frontend/src/screens/CvBank.jsx @@ -0,0 +1,639 @@ +/* ============================================================ + CV Bank — people we already have, for jobs we do not have yet. + + Two populations, one table (GET /candidate/cv-bank/fetch): + + Speculative a CV uploaded with no job attached. Skills, title, + company and years are extracted at upload, which is what + makes the row searchable at all. + Silver medalist someone who applied, scored well, and did not get the + job. Read live from their application rather than copied + here, so there is one source of truth and nothing to sync. + + Two very different numbers live on this screen and must not be confused: + + Match free, deterministic keyword overlap against the job picked in + "Rank against job". It orders the pile. It is not an assessment. + ATS a real paid score, and only present once someone ran one. The + "Score against job" action is what runs it, deliberately per-row. + + That split is the whole design: ranking the bank costs nothing and happens + automatically when a job opens, so scoring can stay explicit and cheap. + ============================================================ */ + +import { useEffect, useMemo, useState } from 'react' +import { useNavigate, useSearchParams } from 'react-router-dom' +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' + +import Modal from '../ui/Modal' +import OpenResumeButton from '../ui/OpenResumeButton' +import PageHeader from '../ui/PageHeader' +import { DataTableHead, DEFAULT_PAGE_SIZE, Pagination, pageWindow, useDataTable } from '../ui/DataTable' +import { Avatar, Badge, EmptyState, Icon, ScoreChip, SkeletonRows } from '../ui/primitives' +import { useToast } from '../ui/Toast' +import { qk } from '../lib/queryKeys' +import { exportStyledXlsx } from '../lib/exportXlsx' +import { friendlyAuthError } from '../lib/errors' +import * as candidatesApi from '../api/candidates' +import * as s3Api from '../api/s3' +import { avatarColor, initials as initialsOf } from '../data/seed' + +const SEARCH_DEBOUNCE_MS = 300 +const SOURCE_FILTERS = ['speculative', 'silver_medalist'] +const SOURCE_LABELS = candidatesApi.BANK_SOURCE_LABELS +const BAND_FILTERS = ['Strong Match', 'Potential Match', 'Weak Match', 'Unscored'] +const YEARS_FILTERS = ['1', '2', '3', '5', '8', '10'] +const BAND_BADGE = { + 'Strong Match': 'b-green', + 'Potential Match': 'b-amber', + 'Weak Match': 'b-gray', +} +const SOURCE_BADGE = { + speculative: 'b-indigo', + silver_medalist: 'b-teal', +} +/* Chips past this are collapsed into "+N" — a CV with 25 skills would + otherwise make one row taller than the rest of the page. */ +const SKILL_CHIPS = 4 + +const EMPTY_FILTERS = { source: '', band: '', years: '' } + +async function fetchBank({ limit, offset, search, filters, jobPostId }) { + const res = await candidatesApi.listCvBank({ + top: limit, + skip: offset, + search: search || undefined, + source: filters.source || undefined, + band: filters.band || undefined, + minYears: filters.years ? Number(filters.years) : undefined, + jobPostId: jobPostId || undefined, + }) + const rows = Array.isArray(res?.data) ? res.data : [] + return { + rows: rows.map(candidatesApi.toBankRowView), + total: Number(res?.total ?? rows.length) || 0, + } +} + +async function fetchJobs() { + const res = await candidatesApi.listJobs() + const rows = Array.isArray(res?.data) ? res.data : [] + return rows.map((row) => ({ id: String(row.id), title: row.title })) +} + +/** The free deterministic rank. Drawn as a plain bar, never as a ScoreChip — + a recruiter must not read it in the same visual language as a real ATS score. */ +function MatchCell({ rank, hasJob }) { + if (!hasJob) return Pick a job + if (rank == null) return + return ( +
+
{rank}/100
+ + ) +} + +function AtsCell({ score, recommendation }) { + if (score == null) return Not scored + return ( +
+ + {recommendation && ( +
+ {recommendation} +
+ )} +
+ ) +} + +export default function CvBank() { + const { toast } = useToast() + const qc = useQueryClient() + const navigate = useNavigate() + const [params, setParams] = useSearchParams() + + const [q, setQ] = useState('') + const [search, setSearch] = useState('') + const [filters, setFilters] = useState(EMPTY_FILTERS) + const [showFilters, setShowFilters] = useState(false) + const [skip, setSkip] = useState(0) + const [pageSize, setPageSize] = useState(DEFAULT_PAGE_SIZE) + const [preview, setPreview] = useState(null) // { name, url } — object URL we own + const [scoreFor, setScoreFor] = useState(null) + const [assignFor, setAssignFor] = useState(null) + + /* The rank job lives in the URL so the notification fired on job creation + ("/cvbank?job=") lands on the ranked view rather than a generic list. */ + const jobPostId = params.get('job') || '' + const setJobPostId = (next) => { + const p = new URLSearchParams(params) + if (next) p.set('job', next) + else p.delete('job') + setParams(p, { replace: true }) + setSkip(0) + } + + useEffect(() => { + const t = setTimeout(() => setSearch(q.trim()), SEARCH_DEBOUNCE_MS) + return () => clearTimeout(t) + }, [q]) + useEffect(() => { setSkip(0) }, [search]) + + const bankQuery = useQuery({ + queryKey: qk.cvBank.list({ limit: pageSize, offset: skip, search, ...filters, jobPostId }), + queryFn: () => fetchBank({ limit: pageSize, offset: skip, search, filters, jobPostId }), + }) + const jobsQuery = useQuery({ queryKey: qk.jobPosts.list(), queryFn: fetchJobs }) + + const rows = useMemo(() => bankQuery.data?.rows ?? [], [bankQuery.data]) + const total = bankQuery.data?.total ?? 0 + const jobs = jobsQuery.data ?? [] + const selectedJob = jobs.find((j) => j.id === jobPostId) || null + + const pages = Math.max(1, Math.ceil(total / pageSize)) + const from = total ? skip + 1 : 0 + const to = total ? skip + rows.length : 0 + const currentPage = Math.min(Math.floor(skip / pageSize) + 1, pages) + + useEffect(() => { + if (total <= 0 || skip < total) return + setSkip(Math.max(0, Math.floor((total - 1) / pageSize) * pageSize)) + }, [total, pageSize, skip]) + + const columns = useMemo(() => [ + { key: 'name', label: 'Candidate', sortable: true }, + { key: 'source', label: 'Source', sortable: true }, + { key: 'title', label: 'Role', sortable: true }, + { key: 'years', label: 'Years', sortable: true }, + { key: 'skills', label: 'Skills', sortable: false }, + { key: 'rankScore', label: 'Match', sortable: true }, + { key: 'aiScore', label: 'ATS', sortable: true }, + { key: 'added', label: 'Added', sortable: true }, + { key: 'actions', label: '', sortable: false }, + ], []) + + // The server already sorted and paged; pageSize here is just "show them all". + const t = useDataTable({ columns, rows, pageSize: Math.max(rows.length, 1) }) + + const setFilter = (k, v) => { + setFilters((f) => ({ ...f, [k]: v })) + setSkip(0) + } + + const removing = useMutation({ + mutationFn: (id) => candidatesApi.deleteCvBankCv(id), + onSuccess: () => { + qc.invalidateQueries({ queryKey: qk.cvBank.all() }) + toast('CV removed from the bank', 'success') + }, + onError: (err) => toast(friendlyAuthError(err, 'Could not remove the CV'), 'error'), + }) + + const scoring = useMutation({ + mutationFn: ({ jobId, ids }) => candidatesApi.scoreCvBank(jobId, ids), + onSuccess: (res) => { + const row = Array.isArray(res?.data) ? res.data[0] : null + if (row?.status === 'completed') { + toast(`Scored ${row.match_score}/100 — the result is on Candidates now`, 'success') + } else { + toast(`Could not score the CV${row?.error_code ? ` — ${row.error_code}` : ''}`, 'warning') + } + qc.invalidateQueries({ queryKey: qk.cvBank.all() }) + qc.invalidateQueries({ queryKey: qk.candidates.all() }) + setScoreFor(null) + }, + onError: (err) => toast(friendlyAuthError(err, 'Scoring failed'), 'error'), + }) + + const assigning = useMutation({ + mutationFn: ({ id, jobId }) => candidatesApi.assignMatchingJob(id, jobId), + onSuccess: () => { + qc.invalidateQueries({ queryKey: qk.cvBank.all() }) + qc.invalidateQueries({ queryKey: qk.candidates.all() }) + toast('CV assigned — it is in the pipeline now', 'success') + setAssignFor(null) + }, + onError: (err) => toast(friendlyAuthError(err, 'Could not assign the job'), 'error'), + }) + + async function view(row) { + if (!row.isStoredCv) { + if (row.userId) navigate(`/candidate/${row.userId}`) + else toast('This applicant has no profile to open', 'info') + return + } + const tab = s3Api.canOpen(row.filePath) ? window.open('about:blank', '_blank') : null + try { + if (s3Api.canOpen(row.filePath)) { + await s3Api.openPdf(row.filePath, { tab }) + return + } + const url = await candidatesApi.viewCvBankCv(row.recordId) + if (!url) { + toast('The CV file could not be found', 'error') + return + } + setPreview({ name: row.fileName || row.name || 'CV', url }) + } catch (err) { + if (tab && !tab.closed) tab.close() + toast(friendlyAuthError(err, 'Could not open the CV'), 'error') + } + } + + function closePreview() { + if (preview) URL.revokeObjectURL(preview.url) + setPreview(null) + } + + async function download(row) { + try { + await candidatesApi.downloadCvBankCv(row.recordId) + } catch (err) { + toast(friendlyAuthError(err, 'Could not download the CV'), 'error') + } + } + + async function exportRows() { + if (!rows.length) { + toast('Nothing to export — current filters match no CVs', 'warning') + return + } + try { + await exportStyledXlsx({ + filename: `cv-bank-${new Date().toISOString().slice(0, 10)}`, + title: 'CV Bank', + subtitle: `${rows.length} CV${rows.length === 1 ? '' : 's'}${selectedJob ? ` · ranked against ${selectedJob.title}` : ''} · exported ${new Date().toLocaleDateString()}`, + columns: [ + { header: 'Name', key: 'name', width: 26 }, + { header: 'Email', key: 'email', width: 30 }, + { header: 'Source', key: 'source', width: 16 }, + { header: 'Title', key: 'title', width: 24 }, + { header: 'Company', key: 'company', width: 24 }, + { header: 'Years', key: 'years', width: 8 }, + { header: 'Skills', key: 'skills', width: 42 }, + { header: 'Match', key: 'match', width: 10 }, + { header: 'ATS', key: 'ats', width: 10 }, + { header: 'Added', key: 'added', width: 12 }, + ], + // Every column is extracted from the CV or read off a real application. + // Talent Pool exported invented skills and companies; this does not. + rows: rows.map((r) => ({ + name: r.name, + email: r.email || '', + source: r.sourceLabel, + title: r.title || '', + company: r.company || '', + years: r.years ?? '', + skills: r.skills.join(', '), + match: r.rankScore ?? '', + ats: r.aiScore ?? '', + added: r.added ? r.added.toLocaleDateString() : '', + })), + }) + toast(`Exported ${rows.length} CV${rows.length === 1 ? '' : 's'}`, 'success') + } catch { + toast('Export failed', 'error') + } + } + + return ( +
+ {total} CV{total === 1 ? '' : 's'} held for future roles{selectedJob ? <> · ranked against {selectedJob.title} : null} + : 'CVs held for future roles' + } + actions={<> + + + } + /> + +
+
+
+
+ + setQ(e.target.value)} + placeholder="Search name, email, company, or skill…" + /> +
+ +
+ {/* The "a role just opened, who do we already have" control. This is + the moment the bank is meant to be used. */} +
+ + +
+
+ + {showFilters && ( +
+ setFilter('source', v)} + any="Any source" + options={SOURCE_FILTERS} + labels={SOURCE_LABELS} + /> + setFilter('band', v)} + any="Any band" + options={BAND_FILTERS} + /> + setFilter('years', v)} + any="Any experience" + options={YEARS_FILTERS} + labels={Object.fromEntries(YEARS_FILTERS.map((y) => [y, `${y}+ years`]))} + /> +
+ )} +
+ + {bankQuery.isPending && ( +
+ )} + {bankQuery.isError && ( +
+ + {friendlyAuthError(bankQuery.error, 'Request failed')} + +
+ )} + + {bankQuery.isSuccess && ( +
+
+ + + + {t.pageRows.length === 0 ? ( + + + + ) : ( + t.pageRows.map((r) => ( + + + + + + + + + + + + )) + )} + +
+ {search || filters.source || filters.band || filters.years ? ( + + No held CV matches these filters. Try widening them. + + ) : ( + + Import CVs with “No job — store in CV bank” selected, and + rejected applicants who scored well will show up here too. + + )} +
+
+ +
+
{r.name}
+
{r.email || r.fileName || 'No email detected'}
+
+
+
+ {r.sourceLabel} + {r.lastJobTitle && ( +
+ applied for {r.lastJobTitle} +
+ )} + {r.expiresAt && ( +
{candidatesApi.expiryLabel(r.expiresAt)}
+ )} +
+
{r.title || '—'}
+ {r.company &&
{r.company}
} +
+ {r.years == null ? '—' : r.years} + + {r.skills.length ? ( +
+ {r.skills.slice(0, SKILL_CHIPS).map((s) => ( + {s} + ))} + {r.skills.length > SKILL_CHIPS && ( + + +{r.skills.length - SKILL_CHIPS} + + )} +
+ ) : ( + None extracted + )} +
+ {r.added ? r.added.toLocaleDateString() : '—'} + +
+ {r.isStoredCv && } + + {r.isStoredCv && (<> + + + + + )} +
+
+
+ setSkip((p - 1) * pageSize)} + pageButtons={pageWindow(currentPage, pages)} + pageSize={pageSize} + onPageSizeChange={(n) => setPageSize(n)} + pageSizeMax={500} + /> +
+ )} +
+ +

+ Match is free keyword overlap against the selected + job — it orders this list, it does not assess anyone. ATS is a real + scored result and only appears once someone runs one. +

+ + {scoreFor && ( + setScoreFor(null)} + onConfirm={(jobId) => scoring.mutate({ jobId, ids: [scoreFor.recordId] })} + /> + )} + + {assignFor && ( + setAssignFor(null)} + onConfirm={(jobId) => assigning.mutate({ id: assignFor.recordId, jobId })} + /> + )} + + {preview && ( + Close} + > + {/* Blob URL re-typed to application/pdf so the browser's built-in + viewer renders inline instead of triggering a download. */} +