From 80d2323a37fbb1bb99828f303bd06874b57bd4ca Mon Sep 17 00:00:00 2001 From: Matthew Jackson Date: Fri, 6 Mar 2026 16:02:19 -0800 Subject: [PATCH] Dynamic profile lookup from auth connection Profile name is returned by ensureAuth() from the auth connection (looked up by domain). No more storing profile names in settings.json for the applier flow. createBrowser() still supports legacy platform keys as fallback for searcher/setup. Co-Authored-By: Claude Opus 4.6 --- job_applier.mjs | 9 +++++---- lib/browser.mjs | 9 ++++++--- lib/session.mjs | 4 ++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/job_applier.mjs b/job_applier.mjs index 900c9df..39b0c20 100644 --- a/job_applier.mjs +++ b/job_applier.mjs @@ -131,24 +131,25 @@ async function main() { if (timedOut) break; console.log(`\n--- ${platform.toUpperCase()} (${platformJobs.length} jobs) ---\n`); let browser; + let platformProfileName = null; try { // LinkedIn and Wellfound need authenticated sessions; external ATS uses plain browser if (platform === 'external') { browser = await createBrowser(settings, null); // no profile needed } else { - // Check auth status before creating browser + // Check auth status and get profile name dynamically const kernelApiKey = process.env.KERNEL_API_KEY || settings.kernel_api_key; const authResult = await ensureAuth(platform, kernelApiKey); if (!authResult.ok) { console.error(` ❌ ${platform} auth failed: ${authResult.reason}`); await sendTelegram(settings, `⚠️ *${platform}* auth failed — ${authResult.reason}`).catch(() => {}); - // Mark all jobs for this platform as needing retry for (const job of platformJobs) { updateJobStatus(job.id, 'new', { retry_reason: 'auth_failed' }); } continue; } - browser = await createBrowser(settings, platform); + platformProfileName = authResult.profileName; + browser = await createBrowser(settings, platformProfileName); console.log(' ✅ Logged in\n'); } @@ -212,7 +213,7 @@ async function main() { try { const newBrowser = platform === 'external' ? await createBrowser(settings, null) - : await createBrowser(settings, platform); + : await createBrowser(settings, platformProfileName); browser = newBrowser; console.log(` ✅ New browser session created`); } catch (browserErr) { diff --git a/lib/browser.mjs b/lib/browser.mjs index 4be7218..c397cc5 100644 --- a/lib/browser.mjs +++ b/lib/browser.mjs @@ -14,7 +14,7 @@ async function getChromium(playwrightPath) { throw new Error('Playwright not found — install with: npm install -g playwright'); } -export async function createBrowser(settings, profileKey) { +export async function createBrowser(settings, profileNameOrKey) { const kernelConfig = settings.kernel || {}; const playwrightPath = settings.browser?.playwright_path; const apiKey = process.env.KERNEL_API_KEY || settings.kernel_api_key; @@ -31,8 +31,11 @@ export async function createBrowser(settings, profileKey) { const kernel = new Kernel({ apiKey }); - const profileName = profileKey ? kernelConfig.profiles?.[profileKey] : null; - if (profileKey && !profileName) throw new Error(`No Kernel profile configured for "${profileKey}"`); + // Accept either a profile name directly or a platform key (legacy fallback) + let profileName = profileNameOrKey; + if (profileName && kernelConfig.profiles?.[profileName]) { + profileName = kernelConfig.profiles[profileName]; + } const opts = { stealth: true }; if (profileName) opts.profile = { name: profileName }; diff --git a/lib/session.mjs b/lib/session.mjs index efe524c..d7e81c3 100644 --- a/lib/session.mjs +++ b/lib/session.mjs @@ -59,7 +59,7 @@ export async function ensureAuth(platform, apiKey) { } if (conn.status === 'AUTHENTICATED') { - return { ok: true }; + return { ok: true, profileName: conn.profile_name }; } // NEEDS_AUTH — can we auto re-auth? @@ -88,7 +88,7 @@ export async function ensureAuth(platform, apiKey) { if (updated.status === 'AUTHENTICATED') { console.log(` ✅ ${platform} re-authenticated`); - return { ok: true }; + return { ok: true, profileName: updated.profile_name }; } if (updated.flow_status === 'FAILED') {