ilml-plugin-linkedin 1.11.1 → 1.11.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.11.3
4
+ - **Fixes plugin ignoring your configured `DATA_DIR` in CLI mode.** `ilml linkedin today` and other read-only commands were silently reading from an empty folder inside the plugin install dir instead of the `DATA_DIR` you set via `ilml plugin config`. Reports showed zeros even on databases with thousands of conversations. No data loss — just the wrong folder was being read.
5
+
6
+ ## 1.11.2
7
+ - **Plugin loads again when installed via `ilml plugin install linkedin`.** v1.11.0 introduced a transitive dependency (`debug` via Puppeteer's stealth plugin) that uses `require('tty')` at runtime — esbuild can't bundle dynamic CommonJS requires into an ESM module, so any `ilml linkedin <command>` died on startup with `Dynamic require of "tty" is not supported`. The build now uses `packages: 'external'` to keep all `node_modules` deps unbundled at runtime (installed by npm into the plugin directory as usual). Same install footprint, no schema change — just the bundle behaves correctly.
8
+ - No schema migration; existing data files unchanged.
9
+
3
10
  ## 1.11.1
4
11
  - **`sync-all` now backfills profile URLs for card-only stub conversations.** When the inbox scan saw a conversation card but never successfully opened its thread (e.g. an earlier sync failed mid-read, or the thread was created from outbound activity alone), the entry was stuck with 0 stored messages and no `profileUrl` — and the previous "re-read to capture URL" check required `messages.length > 0`, so these stubs were never picked up again. The condition now triggers re-read on any sign of activity (stored messages OR sidebar preview), so empty-but-recently-active stubs are queued for thread-open on the next sync. Net effect on long-running databases: dozens of conversations that had only sidebar metadata get their profile URL and message body filled in over the next few sync passes. Backwards-compatible — only adds re-read attempts that previously weren't made.
5
12
  - No schema migration; existing data files unchanged.
@@ -1,11 +1,11 @@
1
- var Ie=Object.defineProperty;var se=(e,n)=>{for(var o in n)Ie(e,o,{get:n[o],enumerable:!0})};import"dotenv/config";import*as E from"fs";import*as _ from"path";import{fileURLToPath as Jo}from"url";import{execFileSync as Uo}from"child_process";import"dotenv/config";import*as m from"fs";import*as g from"path";import{fileURLToPath as Ee}from"url";var De=Ee(import.meta.url),Ce=g.dirname(g.dirname(De)),O=process.env.DATA_DIR?g.resolve(process.env.DATA_DIR,"collected-profiles"):g.join(Ce,"collected-profiles"),Ne=g.join(O,"people.json"),Ko=g.join(O,"jobs.json"),zo=g.join(O,"quota.json"),Xo=g.join(O,"conversations.json"),Zo=g.join(O,"sync-state.json"),en=g.join(O,"application-questions.json"),on=g.join(O,"profile-history");var L=null,xe=null,_e=null,Fe=null,Le=null,Pe=null;function re(){L=null,xe=null,_e=null,Fe=null,Le=null,Pe=null}function Me(){m.existsSync(O)||m.mkdirSync(O,{recursive:!0})}function Je(e,n){if(!m.existsSync(e))return null;try{let o=JSON.parse(m.readFileSync(e,"utf-8"));try{m.copyFileSync(e,e+".backup")}catch{}return o}catch(o){console.error(`[!] ${n} is corrupted: ${o.message}`),console.error(` File: ${g.resolve(e)}`);let t=e+".backup";if(m.existsSync(t))try{let s=JSON.parse(m.readFileSync(t,"utf-8"));console.error(` Restored from ${t}`);try{m.copyFileSync(t,e)}catch{}return s}catch{console.error(" Backup also corrupted!")}console.error(` Starting with empty data. Old file preserved as ${e}.corrupted`);try{m.copyFileSync(e,e+".corrupted")}catch{}return null}}function ie(){return L||(Me(),L=Je(Ne,"people.json")||{},L)}import"dotenv/config";import*as v from"fs";import*as b from"path";import{fileURLToPath as Ue}from"url";var qe=Ue(import.meta.url),Be=b.dirname(b.dirname(qe)),D=process.env.DATA_DIR?b.resolve(process.env.DATA_DIR,"market-research"):b.join(Be,"market-research"),ce=b.join(D,"scouted-jobs.json"),rn=b.join(D,"discovered-people.json"),cn=b.join(D,"visit-log.json"),ln=b.join(D,"companies.json"),Ve=b.join(D,"job-decisions.json");function Ge(){v.existsSync(D)||v.mkdirSync(D,{recursive:!0})}function We(e,n){let o=JSON.stringify(n,null,2),t=e+".tmp";v.writeFileSync(t,o);for(let s=0;s<3;s++)try{v.renameSync(t,e);return}catch(r){if(s<2&&(r.code==="EPERM"||r.code==="EBUSY")){let i=Date.now();for(;Date.now()-i<200;);continue}throw r}}function le(e){try{if(v.existsSync(e))return JSON.parse(v.readFileSync(e,"utf-8"))}catch{}return null}function ae(){P=null,M=null,Ye=null,He=null,Qe=null}var P=null;function V(){return P||(P=le(ce)||{},P)}function ue(e,n){if(!e)return;Ge();let o=V(),t=new Date().toISOString().slice(0,10);if(o[e]){for(let[s,r]of Object.entries(n))r!=null&&(o[e][s]=r);o[e].lastSeen=t}else o[e]={...n,scoutedAt:t,lastSeen:t};try{We(ce,o)}catch(s){console.error(`[!] Failed to save scouted-jobs.json: ${s.message}`)}}var M=null;function fe(){return M||(M=le(Ve)||{},M)}var Ye=null;var He=null;var Qe=null;var Ke=new Set(["java","c#",".net","scala","python","typescript","javascript","react","node.js","aws","azure","docker","kubernetes","microservices","graphql","rest","sql","postgresql","mongodb","kafka","redis","ci/cd","terraform","spring"]),ze=new Set(["iot","machine learning","ai","data engineering","spark","go","rust","kotlin","next.js","devops","sre"]),pe=["vancouver","british columbia"],Xe=["canada","montreal","quebec","alberta","calgary","ottawa"],Ze=["toronto","ontario"],eo=["india","pakistan","nigeria","philippines"];function oo(e,n={}){let o=0,t=(e.techStack||[]).map(a=>a.toLowerCase()),s=0,r=0;for(let a of t)Ke.has(a)&&s++,ze.has(a)&&r++;if(o+=Math.min(s*6,24),o+=Math.min(r*3,6),e.salary){let a=no(e.salary);a&&(a>=15e4?o+=20:a>=12e4?o+=15:a>=1e5?o+=10:a>=8e4?o+=5:a>=100&&a<1e3?o+=15:a>=70&&a<1e3&&(o+=10))}let i=(e.location||"").toLowerCase(),u=/remote/i.test(i)||e.workplaceType==="remote";eo.some(a=>i.includes(a))?o-=30:u&&/united states|usa|\bus\b/i.test(i)?o+=25:u&&pe.some(a=>i.includes(a))?o+=22:u?o+=20:pe.some(a=>i.includes(a))?o+=18:Xe.some(a=>i.includes(a))?o+=10:Ze.some(a=>i.includes(a))&&(o+=5),e.isEasyApply&&(o+=5);let l=(e.seniority||"").toLowerCase();l.includes("senior")||l.includes("lead")||l.includes("staff")?o+=10:l.includes("mid")?o+=5:(l.includes("entry")||l.includes("intern"))&&(o-=10),e.recruiterUrl&&(n.conversationRecruiters?.has(e.recruiterUrl)?o+=20:n.connectedRecruiters?.has(e.recruiterUrl)?o+=15:o+=3);let f=new Set(e.jobTags||[]);return f.has("managerial")?o+=8:f.has("senior-ic")&&(o+=5),f.has("junior")&&(o-=15),f.has("consultant")&&(o-=3),Math.max(0,Math.min(100,o))}function G(e=10,n={}){let{easyApplyOnly:o=!0}=n,t=V(),s=ie(),r=new Set,i=new Set;for(let[h,d]of Object.entries(s))d.connectionStatus==="connected"&&r.add(h);let u={connectedRecruiters:r,conversationRecruiters:i},l=fe(),f=new Set(["applied","skipped","expired","archived"]);return Object.entries(t).filter(([h,d])=>{if(typeof d!="object"||o&&!d.isEasyApply||d.applied)return!1;let y=l[h];return!(y&&f.has(y.status))}).map(([h,d])=>({jobId:h,score:oo(d,u),job:d})).sort((h,d)=>{if(d.score!==h.score)return d.score-h.score;let y=h.job.scoutedAt||h.job.postedTime||"";return(d.job.scoutedAt||d.job.postedTime||"").localeCompare(y)}).slice(0,e)}function no(e){if(!e)return null;let n=e.match(/[\$£€CAD\s]*([\d,]+(?:\.\d+)?)\s*[kK]?/g);if(!n||n.length===0)return null;let o=n.map(t=>{let s=parseFloat(t.replace(/[^\d.]/g,""));return t.toLowerCase().includes("k")&&(s*=1e3),s}).filter(t=>t>0);return o.length>=2?(o[0]+o[1])/2:o.length===1?o[0]:null}import"dotenv/config";import*as $ from"fs";import*as T from"path";import{fileURLToPath as lo}from"url";import"dotenv/config";import{createGraphClient as to,resolveToken as so}from"@ilivemylife/graph-sdk";import{config as ro}from"dotenv";ro();var de=so()||process.env.TOKEN;de||(console.error(`
1
+ var Ie=Object.defineProperty;var re=(e,n)=>{for(var o in n)Ie(e,o,{get:n[o],enumerable:!0})};import"dotenv/config";import*as I from"fs";import*as F from"path";import{fileURLToPath as Vo}from"url";import{execFileSync as Bo}from"child_process";import{getPluginConfig as De}from"@ilivemylife/graph-sdk";var Ce=await De();for(let[e,n]of Object.entries(Ce))process.env[e]===void 0&&(process.env[e]=n);var Xo=process.env.LINKEDIN_NAME,D=Object.freeze({DRAFT:"draft",CONFIRMED:"confirmed",REJECTED:"rejected",NEEDS_REVIEW:"needs_review",SENT:"sent"}),Zo=Object.freeze(new Set([D.DRAFT,D.CONFIRMED,D.NEEDS_REVIEW])),en=Object.freeze(new Set([D.DRAFT,D.CONFIRMED,D.REJECTED,D.NEEDS_REVIEW]));import"dotenv/config";import*as m from"fs";import*as g from"path";import{fileURLToPath as Ne}from"url";var xe=Ne(import.meta.url),_e=g.dirname(g.dirname(xe)),O=process.env.DATA_DIR?g.resolve(process.env.DATA_DIR,"collected-profiles"):g.join(_e,"collected-profiles"),Fe=g.join(O,"people.json"),rn=g.join(O,"jobs.json"),cn=g.join(O,"quota.json"),ln=g.join(O,"conversations.json"),an=g.join(O,"sync-state.json"),un=g.join(O,"application-questions.json"),fn=g.join(O,"profile-history");var P=null,Le=null,Pe=null,Me=null,Je=null,Ue=null;function ie(){P=null,Le=null,Pe=null,Me=null,Je=null,Ue=null}function qe(){m.existsSync(O)||m.mkdirSync(O,{recursive:!0})}function Ve(e,n){if(!m.existsSync(e))return null;try{let o=JSON.parse(m.readFileSync(e,"utf-8"));try{m.copyFileSync(e,e+".backup")}catch{}return o}catch(o){console.error(`[!] ${n} is corrupted: ${o.message}`),console.error(` File: ${g.resolve(e)}`);let t=e+".backup";if(m.existsSync(t))try{let s=JSON.parse(m.readFileSync(t,"utf-8"));console.error(` Restored from ${t}`);try{m.copyFileSync(t,e)}catch{}return s}catch{console.error(" Backup also corrupted!")}console.error(` Starting with empty data. Old file preserved as ${e}.corrupted`);try{m.copyFileSync(e,e+".corrupted")}catch{}return null}}function ce(){return P||(qe(),P=Ve(Fe,"people.json")||{},P)}import"dotenv/config";import*as v from"fs";import*as b from"path";import{fileURLToPath as Be}from"url";var We=Be(import.meta.url),Ge=b.dirname(b.dirname(We)),C=process.env.DATA_DIR?b.resolve(process.env.DATA_DIR,"market-research"):b.join(Ge,"market-research"),le=b.join(C,"scouted-jobs.json"),mn=b.join(C,"discovered-people.json"),gn=b.join(C,"visit-log.json"),yn=b.join(C,"companies.json"),Ye=b.join(C,"job-decisions.json");function He(){v.existsSync(C)||v.mkdirSync(C,{recursive:!0})}function Qe(e,n){let o=JSON.stringify(n,null,2),t=e+".tmp";v.writeFileSync(t,o);for(let s=0;s<3;s++)try{v.renameSync(t,e);return}catch(r){if(s<2&&(r.code==="EPERM"||r.code==="EBUSY")){let i=Date.now();for(;Date.now()-i<200;);continue}throw r}}function ae(e){try{if(v.existsSync(e))return JSON.parse(v.readFileSync(e,"utf-8"))}catch{}return null}function ue(){M=null,J=null,ze=null,Ke=null,Xe=null}var M=null;function W(){return M||(M=ae(le)||{},M)}function fe(e,n){if(!e)return;He();let o=W(),t=new Date().toISOString().slice(0,10);if(o[e]){for(let[s,r]of Object.entries(n))r!=null&&(o[e][s]=r);o[e].lastSeen=t}else o[e]={...n,scoutedAt:t,lastSeen:t};try{Qe(le,o)}catch(s){console.error(`[!] Failed to save scouted-jobs.json: ${s.message}`)}}var J=null;function pe(){return J||(J=ae(Ye)||{},J)}var ze=null;var Ke=null;var Xe=null;var Ze=new Set(["java","c#",".net","scala","python","typescript","javascript","react","node.js","aws","azure","docker","kubernetes","microservices","graphql","rest","sql","postgresql","mongodb","kafka","redis","ci/cd","terraform","spring"]),eo=new Set(["iot","machine learning","ai","data engineering","spark","go","rust","kotlin","next.js","devops","sre"]),de=["vancouver","british columbia"],oo=["canada","montreal","quebec","alberta","calgary","ottawa"],no=["toronto","ontario"],to=["india","pakistan","nigeria","philippines"];function so(e,n={}){let o=0,t=(e.techStack||[]).map(a=>a.toLowerCase()),s=0,r=0;for(let a of t)Ze.has(a)&&s++,eo.has(a)&&r++;if(o+=Math.min(s*6,24),o+=Math.min(r*3,6),e.salary){let a=ro(e.salary);a&&(a>=15e4?o+=20:a>=12e4?o+=15:a>=1e5?o+=10:a>=8e4?o+=5:a>=100&&a<1e3?o+=15:a>=70&&a<1e3&&(o+=10))}let i=(e.location||"").toLowerCase(),u=/remote/i.test(i)||e.workplaceType==="remote";to.some(a=>i.includes(a))?o-=30:u&&/united states|usa|\bus\b/i.test(i)?o+=25:u&&de.some(a=>i.includes(a))?o+=22:u?o+=20:de.some(a=>i.includes(a))?o+=18:oo.some(a=>i.includes(a))?o+=10:no.some(a=>i.includes(a))&&(o+=5),e.isEasyApply&&(o+=5);let l=(e.seniority||"").toLowerCase();l.includes("senior")||l.includes("lead")||l.includes("staff")?o+=10:l.includes("mid")?o+=5:(l.includes("entry")||l.includes("intern"))&&(o-=10),e.recruiterUrl&&(n.conversationRecruiters?.has(e.recruiterUrl)?o+=20:n.connectedRecruiters?.has(e.recruiterUrl)?o+=15:o+=3);let f=new Set(e.jobTags||[]);return f.has("managerial")?o+=8:f.has("senior-ic")&&(o+=5),f.has("junior")&&(o-=15),f.has("consultant")&&(o-=3),Math.max(0,Math.min(100,o))}function G(e=10,n={}){let{easyApplyOnly:o=!0}=n,t=W(),s=ce(),r=new Set,i=new Set;for(let[h,d]of Object.entries(s))d.connectionStatus==="connected"&&r.add(h);let u={connectedRecruiters:r,conversationRecruiters:i},l=pe(),f=new Set(["applied","skipped","expired","archived"]);return Object.entries(t).filter(([h,d])=>{if(typeof d!="object"||o&&!d.isEasyApply||d.applied)return!1;let y=l[h];return!(y&&f.has(y.status))}).map(([h,d])=>({jobId:h,score:so(d,u),job:d})).sort((h,d)=>{if(d.score!==h.score)return d.score-h.score;let y=h.job.scoutedAt||h.job.postedTime||"";return(d.job.scoutedAt||d.job.postedTime||"").localeCompare(y)}).slice(0,e)}function ro(e){if(!e)return null;let n=e.match(/[\$£€CAD\s]*([\d,]+(?:\.\d+)?)\s*[kK]?/g);if(!n||n.length===0)return null;let o=n.map(t=>{let s=parseFloat(t.replace(/[^\d.]/g,""));return t.toLowerCase().includes("k")&&(s*=1e3),s}).filter(t=>t>0);return o.length>=2?(o[0]+o[1])/2:o.length===1?o[0]:null}import"dotenv/config";import*as $ from"fs";import*as T from"path";import{fileURLToPath as fo}from"url";import"dotenv/config";import{createGraphClient as io,resolveToken as co}from"@ilivemylife/graph-sdk";import{config as lo}from"dotenv";lo();var he=co()||process.env.TOKEN;he||(console.error(`
2
2
  [FATAL] No iLiveMyLife token found.`),console.error(" The bot uses Lifebot AI to decide which jobs to apply for"),console.error(` and how to fill application forms. It cannot work without it.
3
3
  `),console.error(" Easiest way:"),console.error(" 1. npm install -g @ilivemylife/graph-sdk"),console.error(` 2. ilml login your@email.com yourpassword
4
4
  `),console.error(` Or add ILML_TOKEN=... to .env
5
- `),process.exit(1));var he=to({token:de});function io(e){if(!e||e<1e3)return`${e||0}ms`;let n=Math.round(e/1e3);if(n<60)return`${n}s`;let o=Math.floor(n/60),t=n%60;return t?`${o}m ${t}s`:`${o}m`}function co({scriptName:e,mode:n,status:o,error:t,duration:s}){let r=o==="error"?"\u2717":o==="quota-reached"?"\u26A0":"\u2713",i=new Date().toISOString().slice(0,16).replace("T"," "),u=s?` ${io(s)}`:"",l=o==="error"?`error${t?`: ${String(t).slice(0,80)}`:""}`:o==="quota-reached"?"daily quota reached":"done",f=n&&n!=="default"?` (${n})`:"";return`${r} ${e}${f} \u2014 ${l} \u2014 ${i}${u}`}async function me({scriptName:e,mode:n,status:o,summaryLines:t,error:s,duration:r}){let i=process.env.NODE_RUN_REPORTS;if(!i||!Array.isArray(t)||t.length===0)return;let l=`**${co({scriptName:e,mode:n,status:o,error:s,duration:r})}**
5
+ `),process.exit(1));var me=io({token:he});function ao(e){if(!e||e<1e3)return`${e||0}ms`;let n=Math.round(e/1e3);if(n<60)return`${n}s`;let o=Math.floor(n/60),t=n%60;return t?`${o}m ${t}s`:`${o}m`}function uo({scriptName:e,mode:n,status:o,error:t,duration:s}){let r=o==="error"?"\u2717":o==="quota-reached"?"\u26A0":"\u2713",i=new Date().toISOString().slice(0,16).replace("T"," "),u=s?` ${ao(s)}`:"",l=o==="error"?`error${t?`: ${String(t).slice(0,80)}`:""}`:o==="quota-reached"?"daily quota reached":"done",f=n&&n!=="default"?` (${n})`:"";return`${r} ${e}${f} \u2014 ${l} \u2014 ${i}${u}`}async function ge({scriptName:e,mode:n,status:o,summaryLines:t,error:s,duration:r}){let i=process.env.NODE_RUN_REPORTS;if(!i||!Array.isArray(t)||t.length===0)return;let l=`**${uo({scriptName:e,mode:n,status:o,error:s,duration:r})}**
6
6
 
7
7
  ${t.join(`
8
- `)}`;try{await he.addMessage(i,l)}catch(f){console.error(`[runReport] Failed to post end-of-session message: ${f.message}`)}}var ao=lo(import.meta.url),uo=T.dirname(T.dirname(ao)),W=process.env.DATA_DIR?T.resolve(process.env.DATA_DIR,"collected-profiles"):T.join(uo,"collected-profiles"),Y=T.join(W,"run-log.json");function fo(e,n){let o=JSON.stringify(n,null,2),t=e+".tmp";$.writeFileSync(t,o);for(let s=0;s<3;s++)try{$.renameSync(t,e);return}catch(r){if(s<2&&(r.code==="EPERM"||r.code==="EBUSY")){let i=Date.now();for(;Date.now()-i<200;);continue}throw r}}function ge(){try{if(!$.existsSync(Y))return{runs:[]};let e=JSON.parse($.readFileSync(Y,"utf-8"));return!e.runs||!Array.isArray(e.runs)?{runs:[]}:e}catch{return{runs:[]}}}function ye(e){$.existsSync(W)||$.mkdirSync(W,{recursive:!0});try{fo(Y,e)}catch(n){console.error(`[!] Failed to save run-log.json: ${n.message}`)}}function Se(e,n="default"){let o=ge();for(let i of o.runs)i.status==="running"&&(i.status="interrupted",i.result="interrupted",i.timestamp&&(i.duration=Date.now()-new Date(i.timestamp).getTime()));let t=new Date,s={timestamp:t.toISOString(),script:e,mode:n,status:"running",result:null,duration:null,stats:null,error:null};o.runs.push(s);let r=Date.now()-30*24*60*60*1e3;return o.runs=o.runs.filter(i=>new Date(i.timestamp).getTime()>r),ye(o),{script:e,mode:n,startTime:t.getTime(),runIndex:o.runs.length-1}}function H(e,n,o=null,t=null){if(!e)return;let s=null;try{let i=ge(),u=i.runs[e.runIndex];if(u&&u.status==="running"&&u.script===e.script)u.status=n,u.result=n,u.duration=Date.now()-e.startTime,u.stats=o,u.error=t,s=u.duration;else{let l=i.runs.find(f=>f.script===e.script&&f.status==="running"&&f.timestamp===new Date(e.startTime).toISOString());l&&(l.status=n,l.result=n,l.duration=Date.now()-e.startTime,l.stats=o,l.error=t,s=l.duration)}ye(i)}catch(i){console.error(`[runLog] Failed to end run: ${i.message}`)}let r=o&&Array.isArray(o.summaryLines)?o.summaryLines:null;if(r&&r.length>0&&process.env.NODE_RUN_REPORTS){let i=n==="error"?"error":o&&o.dailyLimitHit?"quota-reached":"done";me({scriptName:e.script,mode:e.mode,status:i,summaryLines:r,error:t,duration:s??Date.now()-e.startTime}).catch(u=>console.error(`[runLog] postRunReport failed: ${u.message}`))}}import"dotenv/config";import*as c from"fs";import*as ee from"os";import*as p from"path";import{fileURLToPath as ko}from"url";var z={};se(z,{NAME:()=>ho,VERSION:()=>po,up:()=>yo});import*as w from"fs";import*as J from"path";var po=1,ho="normalize-jobs-and-questions",mo=["description","techStack","jobTags","seniority","employmentType","industries","jobFunction","postedTime","applicantCount","companySize","workplaceType","contractType","isEasyApply","applyType","companyUrl","scoutedAt","lastSeen"],go=50;function Q(e){return w.existsSync(e)?JSON.parse(w.readFileSync(e,"utf-8")):null}function K(e,n){let o=JSON.stringify(n,null,2),t=e+".tmp";try{w.writeFileSync(t,o)}catch(s){try{w.unlinkSync(t)}catch{}throw s}try{w.renameSync(t,e)}catch(s){try{w.unlinkSync(t)}catch{}if(s.code==="EPERM"||s.code==="EBUSY"){let r=new Error(`File locked: ${e} (${s.code}). On Windows usually antivirus / file explorer / open editor. Close it and re-run.`);throw r.code=s.code,r}throw s}}async function yo(e,n){let o=J.join(e,"jobs.json"),t=J.join(n,"scouted-jobs.json"),s=J.join(e,"application-questions.json"),r=Q(o)||{},i=Q(t)||{},u=0,l=[];for(let a of Object.keys(r)){if(!i[a])continue;let h=i[a],d=r[a];for(let y of mo)h[y]!=null&&d[y]==null&&(d[y]=h[y]);l.push(a),u++}u>0?(K(o,r),console.log(` Phase 1: Merged ${u} scouted jobs into jobs.json`)):console.log(" Phase 1: No overlapping jobs to merge");let f=Q(s);if(f&&Array.isArray(f.applications)){let a={},h=0;for(let d of f.applications){let y=d.jobId||"unknown";a[y]||(a[y]={applications:[]});let B=(d.questions||[]).map(j=>{let F={type:j.type||"input",question:j.question||"",answer:j.answer||""};return j.options&&(j.options.length<=go?F.options=j.options:(F.optionsOmitted=!0,h++)),j.page&&(F.page=j.page),j.wasPreFilled&&(F.wasPreFilled=!0),j.wasRetry&&(F.wasRetry=!0),F});a[y].applications.push({applicationId:d.applicationId,date:d.date,result:d.result||"submitted",formPages:d.formPages||null,duration:d.duration||null,retries:d.retries||0,failedField:d.failedField||null,questionCount:d.questionCount||B.length,questions:B})}K(s,a),console.log(` Phase 2: Rekeyed ${f.applications.length} applications by jobId (${Object.keys(a).length} unique jobs)`),h>0&&console.log(` Phase 2: Stripped bloated options from ${h} questions`)}else f&&!f.applications?console.log(" Phase 2: application-questions.json already in keyed format"):console.log(" Phase 2: No application-questions.json found");if(l.length>0){for(let a of l)delete i[a];K(t,i),console.log(` Phase 3: Removed ${l.length} merged entries from scouted-jobs.json (${Object.keys(i).length} remaining)`)}else console.log(" Phase 3: No entries to remove from scouted-jobs.json")}var X={};se(X,{NAME:()=>bo,VERSION:()=>So,up:()=>wo});import*as k from"fs";import*as be from"path";var So=2,bo="reset-unread-flags";function vo(e){return k.existsSync(e)?JSON.parse(k.readFileSync(e,"utf-8")):null}function $o(e,n){let o=JSON.stringify(n,null,2),t=e+".tmp";try{k.writeFileSync(t,o)}catch(s){try{k.unlinkSync(t)}catch{}throw s}try{k.renameSync(t,e)}catch(s){try{k.unlinkSync(t)}catch{}if(s.code==="EPERM"||s.code==="EBUSY"){let r=new Error(`File locked: ${e} (${s.code}). On Windows usually antivirus / file explorer / open editor. Close it and re-run.`);throw r.code=s.code,r}throw s}}async function wo(e){let n=be.join(e,"conversations.json"),o=vo(n);if(!o){console.log(" conversations.json not found \u2014 nothing to reset.");return}let t=0;for(let s of Object.keys(o))o[s].unread===!0&&(o[s].unread=!1,t++);if(t===0){console.log(" No stale unread flags found \u2014 nothing to do.");return}$o(n,o),console.log(` Reset unread=false on ${t} conversations. Run 'ilml linkedin sync-all' to re-establish real unread state from LinkedIn.`)}var jo=ko(import.meta.url),Ao=p.dirname(jo),Oo=p.dirname(p.dirname(Ao));function Ro(e){return e&&(e==="~"?ee.homedir():e.startsWith("~/")||e.startsWith("~\\")?p.join(ee.homedir(),e.slice(2)):e)}var A=process.env.DATA_DIR?p.resolve(Ro(process.env.DATA_DIR)):Oo,N=p.join(A,"collected-profiles"),x=p.join(A,"market-research"),S=p.join(A,".schema-version"),ve=[p.join(N,".schema-version"),p.join(x,".schema-version")],I=2,R=[{version:1,module:z},{version:2,module:X}];(function(){R.sort((t,s)=>t.version-s.version);let n=new Set;for(let t of R){if(!Number.isInteger(t.version)||t.version<1)throw new Error(`MIGRATION_REGISTRY: invalid version ${JSON.stringify(t.version)} (must be positive integer)`);if(n.has(t.version))throw new Error(`MIGRATION_REGISTRY: duplicate version v${t.version}`);n.add(t.version);let s=t.module;if(!s||typeof s.up!="function")throw new Error(`MIGRATION_REGISTRY: v${t.version} module is missing up()`);if(s.VERSION!==void 0&&s.VERSION!==t.version)throw new Error(`MIGRATION_REGISTRY: v${t.version} entry does not match module VERSION (${s.VERSION}). Drift between registry and module \u2014 pick one source of truth.`)}let o=R.length?R[R.length-1].version:0;if(o!==I)throw new Error(`MIGRATION_REGISTRY: highest version v${o} but CURRENT_SCHEMA_VERSION=${I}. Forgot to bump CURRENT_SCHEMA_VERSION when adding the new migration?`);for(let t=0;t<R.length;t++){let s=t+1;if(R[t].version!==s)throw new Error(`MIGRATION_REGISTRY: gap detected \u2014 expected v${s} at position ${t} but found v${R[t].version}`)}})();function je(){process.env.DATA_DIR!==void 0&&process.env.DATA_DIR.trim()===""&&process.env.DATA_DIR.length>0&&(console.error(`
8
+ `)}`;try{await me.addMessage(i,l)}catch(f){console.error(`[runReport] Failed to post end-of-session message: ${f.message}`)}}var po=fo(import.meta.url),ho=T.dirname(T.dirname(po)),Y=process.env.DATA_DIR?T.resolve(process.env.DATA_DIR,"collected-profiles"):T.join(ho,"collected-profiles"),H=T.join(Y,"run-log.json");function mo(e,n){let o=JSON.stringify(n,null,2),t=e+".tmp";$.writeFileSync(t,o);for(let s=0;s<3;s++)try{$.renameSync(t,e);return}catch(r){if(s<2&&(r.code==="EPERM"||r.code==="EBUSY")){let i=Date.now();for(;Date.now()-i<200;);continue}throw r}}function ye(){try{if(!$.existsSync(H))return{runs:[]};let e=JSON.parse($.readFileSync(H,"utf-8"));return!e.runs||!Array.isArray(e.runs)?{runs:[]}:e}catch{return{runs:[]}}}function Se(e){$.existsSync(Y)||$.mkdirSync(Y,{recursive:!0});try{mo(H,e)}catch(n){console.error(`[!] Failed to save run-log.json: ${n.message}`)}}function be(e,n="default"){let o=ye();for(let i of o.runs)i.status==="running"&&(i.status="interrupted",i.result="interrupted",i.timestamp&&(i.duration=Date.now()-new Date(i.timestamp).getTime()));let t=new Date,s={timestamp:t.toISOString(),script:e,mode:n,status:"running",result:null,duration:null,stats:null,error:null};o.runs.push(s);let r=Date.now()-30*24*60*60*1e3;return o.runs=o.runs.filter(i=>new Date(i.timestamp).getTime()>r),Se(o),{script:e,mode:n,startTime:t.getTime(),runIndex:o.runs.length-1}}function Q(e,n,o=null,t=null){if(!e)return;let s=null;try{let i=ye(),u=i.runs[e.runIndex];if(u&&u.status==="running"&&u.script===e.script)u.status=n,u.result=n,u.duration=Date.now()-e.startTime,u.stats=o,u.error=t,s=u.duration;else{let l=i.runs.find(f=>f.script===e.script&&f.status==="running"&&f.timestamp===new Date(e.startTime).toISOString());l&&(l.status=n,l.result=n,l.duration=Date.now()-e.startTime,l.stats=o,l.error=t,s=l.duration)}Se(i)}catch(i){console.error(`[runLog] Failed to end run: ${i.message}`)}let r=o&&Array.isArray(o.summaryLines)?o.summaryLines:null;if(r&&r.length>0&&process.env.NODE_RUN_REPORTS){let i=n==="error"?"error":o&&o.dailyLimitHit?"quota-reached":"done";ge({scriptName:e.script,mode:e.mode,status:i,summaryLines:r,error:t,duration:s??Date.now()-e.startTime}).catch(u=>console.error(`[runLog] postRunReport failed: ${u.message}`))}}import"dotenv/config";import*as c from"fs";import*as oe from"os";import*as p from"path";import{fileURLToPath as Oo}from"url";var X={};re(X,{NAME:()=>yo,VERSION:()=>go,up:()=>vo});import*as w from"fs";import*as U from"path";var go=1,yo="normalize-jobs-and-questions",So=["description","techStack","jobTags","seniority","employmentType","industries","jobFunction","postedTime","applicantCount","companySize","workplaceType","contractType","isEasyApply","applyType","companyUrl","scoutedAt","lastSeen"],bo=50;function z(e){return w.existsSync(e)?JSON.parse(w.readFileSync(e,"utf-8")):null}function K(e,n){let o=JSON.stringify(n,null,2),t=e+".tmp";try{w.writeFileSync(t,o)}catch(s){try{w.unlinkSync(t)}catch{}throw s}try{w.renameSync(t,e)}catch(s){try{w.unlinkSync(t)}catch{}if(s.code==="EPERM"||s.code==="EBUSY"){let r=new Error(`File locked: ${e} (${s.code}). On Windows usually antivirus / file explorer / open editor. Close it and re-run.`);throw r.code=s.code,r}throw s}}async function vo(e,n){let o=U.join(e,"jobs.json"),t=U.join(n,"scouted-jobs.json"),s=U.join(e,"application-questions.json"),r=z(o)||{},i=z(t)||{},u=0,l=[];for(let a of Object.keys(r)){if(!i[a])continue;let h=i[a],d=r[a];for(let y of So)h[y]!=null&&d[y]==null&&(d[y]=h[y]);l.push(a),u++}u>0?(K(o,r),console.log(` Phase 1: Merged ${u} scouted jobs into jobs.json`)):console.log(" Phase 1: No overlapping jobs to merge");let f=z(s);if(f&&Array.isArray(f.applications)){let a={},h=0;for(let d of f.applications){let y=d.jobId||"unknown";a[y]||(a[y]={applications:[]});let B=(d.questions||[]).map(j=>{let L={type:j.type||"input",question:j.question||"",answer:j.answer||""};return j.options&&(j.options.length<=bo?L.options=j.options:(L.optionsOmitted=!0,h++)),j.page&&(L.page=j.page),j.wasPreFilled&&(L.wasPreFilled=!0),j.wasRetry&&(L.wasRetry=!0),L});a[y].applications.push({applicationId:d.applicationId,date:d.date,result:d.result||"submitted",formPages:d.formPages||null,duration:d.duration||null,retries:d.retries||0,failedField:d.failedField||null,questionCount:d.questionCount||B.length,questions:B})}K(s,a),console.log(` Phase 2: Rekeyed ${f.applications.length} applications by jobId (${Object.keys(a).length} unique jobs)`),h>0&&console.log(` Phase 2: Stripped bloated options from ${h} questions`)}else f&&!f.applications?console.log(" Phase 2: application-questions.json already in keyed format"):console.log(" Phase 2: No application-questions.json found");if(l.length>0){for(let a of l)delete i[a];K(t,i),console.log(` Phase 3: Removed ${l.length} merged entries from scouted-jobs.json (${Object.keys(i).length} remaining)`)}else console.log(" Phase 3: No entries to remove from scouted-jobs.json")}var Z={};re(Z,{NAME:()=>wo,VERSION:()=>$o,up:()=>Ao});import*as k from"fs";import*as ve from"path";var $o=2,wo="reset-unread-flags";function ko(e){return k.existsSync(e)?JSON.parse(k.readFileSync(e,"utf-8")):null}function jo(e,n){let o=JSON.stringify(n,null,2),t=e+".tmp";try{k.writeFileSync(t,o)}catch(s){try{k.unlinkSync(t)}catch{}throw s}try{k.renameSync(t,e)}catch(s){try{k.unlinkSync(t)}catch{}if(s.code==="EPERM"||s.code==="EBUSY"){let r=new Error(`File locked: ${e} (${s.code}). On Windows usually antivirus / file explorer / open editor. Close it and re-run.`);throw r.code=s.code,r}throw s}}async function Ao(e){let n=ve.join(e,"conversations.json"),o=ko(n);if(!o){console.log(" conversations.json not found \u2014 nothing to reset.");return}let t=0;for(let s of Object.keys(o))o[s].unread===!0&&(o[s].unread=!1,t++);if(t===0){console.log(" No stale unread flags found \u2014 nothing to do.");return}jo(n,o),console.log(` Reset unread=false on ${t} conversations. Run 'ilml linkedin sync-all' to re-establish real unread state from LinkedIn.`)}var Ro=Oo(import.meta.url),To=p.dirname(Ro),Eo=p.dirname(p.dirname(To));function Io(e){return e&&(e==="~"?oe.homedir():e.startsWith("~/")||e.startsWith("~\\")?p.join(oe.homedir(),e.slice(2)):e)}var A=process.env.DATA_DIR?p.resolve(Io(process.env.DATA_DIR)):Eo,x=p.join(A,"collected-profiles"),_=p.join(A,"market-research"),S=p.join(A,".schema-version"),$e=[p.join(x,".schema-version"),p.join(_,".schema-version")],E=2,R=[{version:1,module:X},{version:2,module:Z}];(function(){R.sort((t,s)=>t.version-s.version);let n=new Set;for(let t of R){if(!Number.isInteger(t.version)||t.version<1)throw new Error(`MIGRATION_REGISTRY: invalid version ${JSON.stringify(t.version)} (must be positive integer)`);if(n.has(t.version))throw new Error(`MIGRATION_REGISTRY: duplicate version v${t.version}`);n.add(t.version);let s=t.module;if(!s||typeof s.up!="function")throw new Error(`MIGRATION_REGISTRY: v${t.version} module is missing up()`);if(s.VERSION!==void 0&&s.VERSION!==t.version)throw new Error(`MIGRATION_REGISTRY: v${t.version} entry does not match module VERSION (${s.VERSION}). Drift between registry and module \u2014 pick one source of truth.`)}let o=R.length?R[R.length-1].version:0;if(o!==E)throw new Error(`MIGRATION_REGISTRY: highest version v${o} but CURRENT_SCHEMA_VERSION=${E}. Forgot to bump CURRENT_SCHEMA_VERSION when adding the new migration?`);for(let t=0;t<R.length;t++){let s=t+1;if(R[t].version!==s)throw new Error(`MIGRATION_REGISTRY: gap detected \u2014 expected v${s} at position ${t} but found v${R[t].version}`)}})();function Ae(){process.env.DATA_DIR!==void 0&&process.env.DATA_DIR.trim()===""&&process.env.DATA_DIR.length>0&&(console.error(`
9
9
  [FATAL] DATA_DIR is set to whitespace ("${process.env.DATA_DIR}"). Either unset it or set a real path.
10
10
  `),process.exit(1));try{c.mkdirSync(A,{recursive:!0})}catch(n){n.code!=="EEXIST"&&(console.error(`
11
11
  [FATAL] Cannot create DATA_DIR (${A}): ${n.message}`),console.error(` Fix the path or permissions and try again.
@@ -13,46 +13,46 @@ ${t.join(`
13
13
  [FATAL] DATA_DIR (${A}) is not accessible: ${n.message}
14
14
  `),process.exit(1)}e.isDirectory()||(console.error(`
15
15
  [FATAL] DATA_DIR (${A}) exists but is NOT a directory.`),console.error(` Looks like a typo \u2014 your DATA_DIR points at a file. Fix the path and try again.
16
- `),process.exit(1));for(let n of[N,x]){let o;try{o=c.lstatSync(n)}catch(t){if(t.code==="ENOENT")continue;throw t}o.isSymbolicLink()&&(console.error(`
16
+ `),process.exit(1));for(let n of[x,_]){let o;try{o=c.lstatSync(n)}catch(t){if(t.code==="ENOENT")continue;throw t}o.isSymbolicLink()&&(console.error(`
17
17
  [FATAL] Data subdirectory is a symbolic link: ${n}`),console.error(" Refusing to follow \u2014 backup code would copy whatever the link points at."),console.error(` If this is intentional, replace the symlink with the real directory.
18
- `),process.exit(1))}}var C=p.join(A,".migration-lock");function To(){let e=JSON.stringify({pid:process.pid,startedAt:new Date().toISOString()},null,2);try{return c.writeFileSync(C,e,{flag:"wx"}),!0}catch(t){if(t.code!=="EEXIST")throw t}let n;try{n=JSON.parse(c.readFileSync(C,"utf-8"))}catch{n=null}if(n&&Number.isInteger(n.pid)){let t=!1;try{process.kill(n.pid,0),t=!0}catch(i){i.code==="EPERM"&&(t=!0)}let s=24*60*60*1e3,r=1/0;if(n.startedAt){let i=Date.parse(n.startedAt);Number.isNaN(i)||(r=Date.now()-i)}t&&r<s&&(console.error(`
19
- [FATAL] Another plugin process is currently running migrations:`),console.error(` PID: ${n.pid}`),console.error(` Started at: ${n.startedAt||"(unknown)"}`),console.error(` Lock file: ${C}
18
+ `),process.exit(1))}}var N=p.join(A,".migration-lock");function Do(){let e=JSON.stringify({pid:process.pid,startedAt:new Date().toISOString()},null,2);try{return c.writeFileSync(N,e,{flag:"wx"}),!0}catch(t){if(t.code!=="EEXIST")throw t}let n;try{n=JSON.parse(c.readFileSync(N,"utf-8"))}catch{n=null}if(n&&Number.isInteger(n.pid)){let t=!1;try{process.kill(n.pid,0),t=!0}catch(i){i.code==="EPERM"&&(t=!0)}let s=24*60*60*1e3,r=1/0;if(n.startedAt){let i=Date.parse(n.startedAt);Number.isNaN(i)||(r=Date.now()-i)}t&&r<s&&(console.error(`
19
+ [FATAL] Another plugin process is currently running migrations:`),console.error(` PID: ${n.pid}`),console.error(` Started at: ${n.startedAt||"(unknown)"}`),console.error(` Lock file: ${N}
20
20
  `),console.error(" Wait for it to finish, or if you're sure it's not actually running,"),console.error(` delete the lock file manually and re-run.
21
- `),process.exit(1)),t&&r>=s&&console.log(` Lock holder PID ${n.pid} is alive but lock is older than 24h \u2014 assuming PID reuse, taking over.`)}console.log(` Stale migration lock from PID ${n?.pid??"?"} found \u2014 taking over.`);let o=C+".tmp";try{return c.writeFileSync(o,e),c.renameSync(o,C),!0}catch(t){try{c.unlinkSync(o)}catch{}throw new Error(`Failed to take over stale migration lock: ${t.message}`)}}function oe(){try{if(JSON.parse(c.readFileSync(C,"utf-8")).pid!==process.pid)return;c.unlinkSync(C)}catch{}}var $e=!1;function Io(){if($e)return;$e=!0;let e=()=>{try{oe()}catch{}process.exit(1)};for(let n of["SIGINT","SIGTERM","SIGHUP"])try{process.on(n,e)}catch{}}function Eo(){if(c.existsSync(S))return;let e=[],n=[];for(let r of ve)if(c.existsSync(r))try{e.push({file:r,schema:U(r)})}catch(i){n.push({file:r,err:i.message})}if(n.length>0){console.error(`
21
+ `),process.exit(1)),t&&r>=s&&console.log(` Lock holder PID ${n.pid} is alive but lock is older than 24h \u2014 assuming PID reuse, taking over.`)}console.log(` Stale migration lock from PID ${n?.pid??"?"} found \u2014 taking over.`);let o=N+".tmp";try{return c.writeFileSync(o,e),c.renameSync(o,N),!0}catch(t){try{c.unlinkSync(o)}catch{}throw new Error(`Failed to take over stale migration lock: ${t.message}`)}}function ne(){try{if(JSON.parse(c.readFileSync(N,"utf-8")).pid!==process.pid)return;c.unlinkSync(N)}catch{}}var we=!1;function Co(){if(we)return;we=!0;let e=()=>{try{ne()}catch{}process.exit(1)};for(let n of["SIGINT","SIGTERM","SIGHUP"])try{process.on(n,e)}catch{}}function No(){if(c.existsSync(S))return;let e=[],n=[];for(let r of $e)if(c.existsSync(r))try{e.push({file:r,schema:q(r)})}catch(i){n.push({file:r,err:i.message})}if(n.length>0){console.error(`
22
22
  [FATAL] Legacy .schema-version file(s) corrupted \u2014 cannot determine current schema version:`);for(let r of n)console.error(` ${r.file}: ${r.err}`);console.error(`
23
23
  Repair the file(s) (likely just an integer 'version' field \u2014 see another working DATA_DIR`),console.error(` or default to {"version":1,"migrations":[]} if you're sure you're on schema v1) and try again.
24
24
  `),process.exit(1)}if(e.length===0)return;let o=e.map(r=>r.schema.version);if(new Set(o).size>1){console.error(`
25
25
  [FATAL] Legacy .schema-version files disagree:`);for(let r of e)console.error(` ${r.file}: v${r.schema.version}`);console.error(" This means a previous migration crashed between writing the two files."),console.error(` Reconcile manually (delete the wrong one, keep the right one), then re-run.
26
- `),process.exit(1)}let t=e[0].schema,s=S+".tmp";try{c.writeFileSync(s,JSON.stringify(t,null,2)),c.renameSync(s,S)}catch(r){try{c.unlinkSync(s)}catch{}throw r}for(let r of ve)if(c.existsSync(r))try{c.unlinkSync(r)}catch{}console.log(` Consolidated legacy .schema-version files \u2192 ${S}`)}function Do(e){if(e===null||typeof e!="object"||Array.isArray(e))return null;let n=Number(e.version);return!Number.isFinite(n)||n<0||!Number.isInteger(n)?null:(e.version=n,Array.isArray(e.migrations)||(e.migrations=[]),e)}function U(e){let n=c.readFileSync(e,"utf-8");return n.charCodeAt(0)===65279&&(n=n.slice(1)),JSON.parse(n)}function we(){if(Eo(),!c.existsSync(S))return{version:0,migrations:[]};let e;try{e=U(S)}catch(o){if(o.code==="ENOENT")return{version:0,migrations:[]};console.error(`
26
+ `),process.exit(1)}let t=e[0].schema,s=S+".tmp";try{c.writeFileSync(s,JSON.stringify(t,null,2)),c.renameSync(s,S)}catch(r){try{c.unlinkSync(s)}catch{}throw r}for(let r of $e)if(c.existsSync(r))try{c.unlinkSync(r)}catch{}console.log(` Consolidated legacy .schema-version files \u2192 ${S}`)}function xo(e){if(e===null||typeof e!="object"||Array.isArray(e))return null;let n=Number(e.version);return!Number.isFinite(n)||n<0||!Number.isInteger(n)?null:(e.version=n,Array.isArray(e.migrations)||(e.migrations=[]),e)}function q(e){let n=c.readFileSync(e,"utf-8");return n.charCodeAt(0)===65279&&(n=n.slice(1)),JSON.parse(n)}function ke(){if(No(),!c.existsSync(S))return{version:0,migrations:[]};let e;try{e=q(S)}catch(o){if(o.code==="ENOENT")return{version:0,migrations:[]};console.error(`
27
27
  [FATAL] ${S} is not valid JSON: ${o.message}`),console.error(" Refusing to proceed \u2014 silently treating it as 'fresh install' would re-apply"),console.error(" every migration on top of already-migrated data and corrupt it."),console.error(' Repair the file (set it to {"version":N,"migrations":[...]} matching your real state)'),console.error(` or restore from the *.pre-vNNN.backup files if you have them.
28
- `),process.exit(1)}let n=Do(e);return n===null&&(console.error(`
28
+ `),process.exit(1)}let n=xo(e);return n===null&&(console.error(`
29
29
  [FATAL] ${S} has invalid structure:`),console.error(` ${JSON.stringify(e)?.slice(0,200)||"(unparseable)"}`),console.error(' Expected shape: { "version": <integer>, "migrations": [...] }'),console.error(` Repair the file or restore from a backup.
30
- `),process.exit(1)),n}function Ae(e){try{c.chmodSync(e,384)}catch{}}function Co(e){if(je(),c.existsSync(S))try{if(c.lstatSync(S).isSymbolicLink())throw new Error(`SCHEMA_FILE is a symbolic link: ${S}. Refusing to write through it (potential symlink attack). Remove the link and re-run.`)}catch(o){if(o.message?.includes("symbolic link"))throw o}let n=S+".tmp";try{c.writeFileSync(n,JSON.stringify(e,null,2)),Ae(n),c.renameSync(n,S)}catch(o){try{c.unlinkSync(n)}catch{}throw o}}function ke(e,n){if(!c.existsSync(e))return;let o=`.pre-v${String(n).padStart(3,"0")}.backup`,t=c.readdirSync(e).filter(u=>{let l=u.toLowerCase();if(l.includes(".backup")||l===".schema-version"||l.endsWith(".tmp"))return!1;try{return c.statSync(p.join(e,u)).isFile()}catch{return!1}}),s=0,r=0,i=[];for(let u of t){let l=p.join(e,u),f=p.join(e,u+o);if(c.existsSync(f)){r++;continue}try{Re(l,f),s++}catch(a){i.push({file:u,err:a.message})}}if(i.length>0){let u=new Error(`Failed to back up ${i.length} file(s) in ${p.basename(e)}/: `+i.map(l=>`${l.file} (${l.err})`).join(", "));throw u.code="BACKUP_INCOMPLETE",u}if(s>0||r>0){let u=r>0?` (${r} pre-existing backup(s) preserved)`:"";console.log(` Backed up ${s} files in ${p.basename(e)}/ (${o})${u}`)}}var Z=!1;async function Oe(){if(Z)return;je(),Lo();let n=we().version;if(n===I){Z=!0;return}Io(),To(),process.on("exit",oe),n>I&&(console.error(`
31
- [FATAL] Your data schema is at v${n}, but this plugin build only understands up to v${I}.`),console.error(" This usually means you downgraded the plugin without rolling back data."),console.error(` Operating on the newer-format data with this build would risk corrupting it.
32
- `),console.error(" Options:"),console.error(" A) Re-install the newer plugin version that produced this schema, then"),console.error(" (if you really want to downgrade) run 'rollback-data --confirm' first,"),console.error(" then re-install the older plugin version."),console.error(` B) Manually restore your data from the *.pre-vNNN.backup files matching v${I}.
30
+ `),process.exit(1)),n}function Oe(e){try{c.chmodSync(e,384)}catch{}}function _o(e){if(Ae(),c.existsSync(S))try{if(c.lstatSync(S).isSymbolicLink())throw new Error(`SCHEMA_FILE is a symbolic link: ${S}. Refusing to write through it (potential symlink attack). Remove the link and re-run.`)}catch(o){if(o.message?.includes("symbolic link"))throw o}let n=S+".tmp";try{c.writeFileSync(n,JSON.stringify(e,null,2)),Oe(n),c.renameSync(n,S)}catch(o){try{c.unlinkSync(n)}catch{}throw o}}function je(e,n){if(!c.existsSync(e))return;let o=`.pre-v${String(n).padStart(3,"0")}.backup`,t=c.readdirSync(e).filter(u=>{let l=u.toLowerCase();if(l.includes(".backup")||l===".schema-version"||l.endsWith(".tmp"))return!1;try{return c.statSync(p.join(e,u)).isFile()}catch{return!1}}),s=0,r=0,i=[];for(let u of t){let l=p.join(e,u),f=p.join(e,u+o);if(c.existsSync(f)){r++;continue}try{Te(l,f),s++}catch(a){i.push({file:u,err:a.message})}}if(i.length>0){let u=new Error(`Failed to back up ${i.length} file(s) in ${p.basename(e)}/: `+i.map(l=>`${l.file} (${l.err})`).join(", "));throw u.code="BACKUP_INCOMPLETE",u}if(s>0||r>0){let u=r>0?` (${r} pre-existing backup(s) preserved)`:"";console.log(` Backed up ${s} files in ${p.basename(e)}/ (${o})${u}`)}}var ee=!1;async function Re(){if(ee)return;Ae(),Jo();let n=ke().version;if(n===E){ee=!0;return}Co(),Do(),process.on("exit",ne),n>E&&(console.error(`
31
+ [FATAL] Your data schema is at v${n}, but this plugin build only understands up to v${E}.`),console.error(" This usually means you downgraded the plugin without rolling back data."),console.error(` Operating on the newer-format data with this build would risk corrupting it.
32
+ `),console.error(" Options:"),console.error(" A) Re-install the newer plugin version that produced this schema, then"),console.error(" (if you really want to downgrade) run 'rollback-data --confirm' first,"),console.error(" then re-install the older plugin version."),console.error(` B) Manually restore your data from the *.pre-vNNN.backup files matching v${E}.
33
33
  `),process.exit(1)),console.log(`
34
- \u{1F4E6} Schema migration: v${n} \u2192 v${I}`);let o=Po();if(!o.ok){console.error(`
34
+ \u{1F4E6} Schema migration: v${n} \u2192 v${E}`);let o=Uo();if(!o.ok){console.error(`
35
35
  [FATAL] Cannot migrate \u2014 current data is corrupted:`);for(let s of o.broken)console.error(` ${s.dir}/${s.file}: ${s.err}`);console.error(`
36
36
  Migration ABORTED. No backups taken, no files modified.`),console.error(" Repair the file(s) above (restore from your own backup, or fix manually)"),console.error(` and try again. To inspect: 'ilml linkedin migrate-status'.
37
37
  `),process.exit(1)}let t=R.filter(s=>s.version>n);for(let s of t){console.log(`
38
- Running migration v${String(s.version).padStart(3,"0")}...`);let r=`v${String(s.version).padStart(3,"0")}`,i=`.pre-${r}.backup`;try{ke(N,s.version),ke(x,s.version)}catch(l){console.error(`
38
+ Running migration v${String(s.version).padStart(3,"0")}...`);let r=`v${String(s.version).padStart(3,"0")}`,i=`.pre-${r}.backup`;try{je(x,s.version),je(_,s.version)}catch(l){console.error(`
39
39
  [FATAL] Pre-migration backup failed for ${r}: ${l.message}`),console.error(" Migration NOT applied. Repair the underlying issue (disk space, permissions, locked files)"),console.error(` and re-run the plugin command.
40
- `),process.exit(1)}let u=s.module;try{await u.up(N,x)}catch(l){console.error(`
41
- [FATAL] Migration ${r} failed: ${l.message}`),console.error(` Attempting auto-rollback from ${i} files...`);try{let f=Mo(i);f.ok?(console.error(` \u2713 Auto-rollback restored ${f.restored} files. Schema stays at v${n}.`),console.error(` The migration will be retried on next plugin command. Fix the root cause first.
40
+ `),process.exit(1)}let u=s.module;try{await u.up(x,_)}catch(l){console.error(`
41
+ [FATAL] Migration ${r} failed: ${l.message}`),console.error(` Attempting auto-rollback from ${i} files...`);try{let f=qo(i);f.ok?(console.error(` \u2713 Auto-rollback restored ${f.restored} files. Schema stays at v${n}.`),console.error(` The migration will be retried on next plugin command. Fix the root cause first.
42
42
  `)):(console.error(` [!] Auto-rollback INCOMPLETE: ${f.error}`),console.error(` Manual recovery: copy *${i} files back over the originals.
43
43
  `))}catch(f){console.error(` [!] Auto-rollback CRASHED while restoring: ${f.message}`),console.error(` Manual recovery: copy *${i} files back over the originals.
44
- `)}process.exit(1)}try{let l=new Date().toISOString(),f={version:s.version,name:u.NAME||r,appliedAt:l},a=we();a.version=s.version,a.migratedAt=l,a.migrations=a.migrations||[],a.migrations.push(f),Co(a)}catch(l){console.error(`
44
+ `)}process.exit(1)}try{let l=new Date().toISOString(),f={version:s.version,name:u.NAME||r,appliedAt:l},a=ke();a.version=s.version,a.migratedAt=l,a.migrations=a.migrations||[],a.migrations.push(f),_o(a)}catch(l){console.error(`
45
45
  [FATAL] Migration ${r} applied successfully but schema version file could NOT be updated: ${l.message}`),console.error(` Your data is in the v${s.version} state but .schema-version still shows v${n}.`),console.error(` On next plugin command the runner will try to re-apply ${r}. If that migration is`),console.error(` idempotent (most are), it will succeed harmlessly. If you're unsure, run 'data-cleanup' and contact support.
46
- `),process.exit(1)}console.log(` \u2713 Migration ${r} complete`)}try{re()}catch{}try{ae()}catch{}Z=!0,oe(),console.log(`
47
- \u2713 Schema is now at v${I}
48
- `)}var No=[/\.json\.tmp$/,/\.schema-version\.tmp$/,/\.backup\.tmp$/];function xo(e){let n=e.toLowerCase();return No.some(o=>o.test(n))}var _o=6e4;function Fo(){let e=Date.now(),n=[];for(let o of[A,N,x]){if(!c.existsSync(o))continue;let t;try{t=c.readdirSync(o)}catch{continue}for(let s of t){if(!xo(s))continue;let r=p.join(o,s);try{let i=c.statSync(r);if(!i.isFile()||e-i.mtimeMs<_o)continue;n.push({dir:o,file:s,fullPath:r})}catch{}}}return n}function Lo({verbose:e=!1}={}){let n=Fo();if(n.length===0)return e&&console.log(" No orphan .tmp files found."),0;let o=0;for(let t of n)try{c.unlinkSync(t.fullPath),o++,e&&console.log(` Removed orphan: ${p.basename(t.dir)}/${t.file}`)}catch(s){console.warn(` [!] Could not remove orphan ${t.fullPath}: ${s.message}`)}return!e&&o>0&&console.log(` Cleaned up ${o} orphan .tmp file(s) from previous run.`),o}function Po(){let e=[];for(let n of[N,x]){if(!c.existsSync(n))continue;let o=c.readdirSync(n).filter(t=>{let s=t.toLowerCase();return s.endsWith(".json")&&!s.includes(".backup")});for(let t of o){let s=p.join(n,t);try{U(s)}catch(r){e.push({dir:p.basename(n),file:t,err:r.message})}}}return e.length===0?{ok:!0}:{ok:!1,broken:e}}function Mo(e){let n=[],o=[];for(let r of[N,x]){if(!c.existsSync(r))continue;let i=c.readdirSync(r).filter(u=>u.endsWith(e));for(let u of i){let l=p.join(r,u),f=u.slice(0,-e.length),a=p.join(r,f);try{f.toLowerCase().endsWith(".json")?U(l):c.accessSync(l,c.constants.R_OK),n.push({backupPath:l,originalPath:a,fileName:u,dir:r})}catch(h){o.push(`${p.basename(r)}/${u}: ${h.message}`)}}}if(o.length>0)return{ok:!1,error:o.join("; "),restored:0};let t=0,s=[];for(let{backupPath:r,originalPath:i,fileName:u,dir:l}of n)try{Re(r,i),t++}catch(f){s.push(`${p.basename(l)}/${u}: ${f.message}`)}return s.length>0?{ok:!1,error:s.join("; "),restored:t}:{ok:!0,restored:t}}function Re(e,n){let o=n+".tmp";try{c.copyFileSync(e,o),Ae(o)}catch(t){try{c.unlinkSync(o)}catch{}throw t}try{c.renameSync(o,n)}catch(t){try{c.unlinkSync(o)}catch{}if(t.code==="EPERM"||t.code==="EBUSY"){let s=new Error(`File locked: ${n} (${t.code}). On Windows this is usually antivirus / Search Indexer / the file open in another app. Close anything that might be holding it and re-run.`);throw s.code=t.code,s}throw t}}var qo=Jo(import.meta.url),Bo=_.dirname(qo),ne=process.env.DATA_DIR?_.resolve(process.env.DATA_DIR,"collected-profiles"):_.join(Bo,"collected-profiles");await Oe();var te=process.argv.slice(2),Te=parseInt(te.find(e=>e.startsWith("--max="))?.split("=")[1]||"50"),q=parseInt(te.find(e=>e.startsWith("--min-score="))?.split("=")[1]||"50"),Vo=te.includes("--list");function Go(){let n=G(20,{easyApplyOnly:!0}).filter(o=>o.score>=q);if(console.log(`
49
- \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550`),console.log(` APPLY QUEUE (${n.length} jobs, min score ${q})`),console.log("\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550"),n.length===0)console.log(" No jobs in queue. Run: npm run scout");else for(let o=0;o<n.length;o++){let{score:t,job:s,jobId:r}=n[o],i=(s.techStack||[]).slice(0,4).join(", ");console.log(` ${String(o+1).padStart(2)}. [${t}] ${(s.title||"?").slice(0,35)} @ ${(s.company||"?").slice(0,20)}`),console.log(` ${s.salary||"no salary"} | ${s.location||"?"} | ${i||"no tech"}`),console.log(` ${s.url}`)}console.log(`\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550
50
- `)}async function Wo(){if(console.log(`
46
+ `),process.exit(1)}console.log(` \u2713 Migration ${r} complete`)}try{ie()}catch{}try{ue()}catch{}ee=!0,ne(),console.log(`
47
+ \u2713 Schema is now at v${E}
48
+ `)}var Fo=[/\.json\.tmp$/,/\.schema-version\.tmp$/,/\.backup\.tmp$/];function Lo(e){let n=e.toLowerCase();return Fo.some(o=>o.test(n))}var Po=6e4;function Mo(){let e=Date.now(),n=[];for(let o of[A,x,_]){if(!c.existsSync(o))continue;let t;try{t=c.readdirSync(o)}catch{continue}for(let s of t){if(!Lo(s))continue;let r=p.join(o,s);try{let i=c.statSync(r);if(!i.isFile()||e-i.mtimeMs<Po)continue;n.push({dir:o,file:s,fullPath:r})}catch{}}}return n}function Jo({verbose:e=!1}={}){let n=Mo();if(n.length===0)return e&&console.log(" No orphan .tmp files found."),0;let o=0;for(let t of n)try{c.unlinkSync(t.fullPath),o++,e&&console.log(` Removed orphan: ${p.basename(t.dir)}/${t.file}`)}catch(s){console.warn(` [!] Could not remove orphan ${t.fullPath}: ${s.message}`)}return!e&&o>0&&console.log(` Cleaned up ${o} orphan .tmp file(s) from previous run.`),o}function Uo(){let e=[];for(let n of[x,_]){if(!c.existsSync(n))continue;let o=c.readdirSync(n).filter(t=>{let s=t.toLowerCase();return s.endsWith(".json")&&!s.includes(".backup")});for(let t of o){let s=p.join(n,t);try{q(s)}catch(r){e.push({dir:p.basename(n),file:t,err:r.message})}}}return e.length===0?{ok:!0}:{ok:!1,broken:e}}function qo(e){let n=[],o=[];for(let r of[x,_]){if(!c.existsSync(r))continue;let i=c.readdirSync(r).filter(u=>u.endsWith(e));for(let u of i){let l=p.join(r,u),f=u.slice(0,-e.length),a=p.join(r,f);try{f.toLowerCase().endsWith(".json")?q(l):c.accessSync(l,c.constants.R_OK),n.push({backupPath:l,originalPath:a,fileName:u,dir:r})}catch(h){o.push(`${p.basename(r)}/${u}: ${h.message}`)}}}if(o.length>0)return{ok:!1,error:o.join("; "),restored:0};let t=0,s=[];for(let{backupPath:r,originalPath:i,fileName:u,dir:l}of n)try{Te(r,i),t++}catch(f){s.push(`${p.basename(l)}/${u}: ${f.message}`)}return s.length>0?{ok:!1,error:s.join("; "),restored:t}:{ok:!0,restored:t}}function Te(e,n){let o=n+".tmp";try{c.copyFileSync(e,o),Oe(o)}catch(t){try{c.unlinkSync(o)}catch{}throw t}try{c.renameSync(o,n)}catch(t){try{c.unlinkSync(o)}catch{}if(t.code==="EPERM"||t.code==="EBUSY"){let s=new Error(`File locked: ${n} (${t.code}). On Windows this is usually antivirus / Search Indexer / the file open in another app. Close anything that might be holding it and re-run.`);throw s.code=t.code,s}throw t}}var Wo=Vo(import.meta.url),Go=F.dirname(Wo),te=process.env.DATA_DIR?F.resolve(process.env.DATA_DIR,"collected-profiles"):F.join(Go,"collected-profiles");await Re();var se=process.argv.slice(2),Ee=parseInt(se.find(e=>e.startsWith("--max="))?.split("=")[1]||"50"),V=parseInt(se.find(e=>e.startsWith("--min-score="))?.split("=")[1]||"50"),Yo=se.includes("--list");function Ho(){let n=G(20,{easyApplyOnly:!0}).filter(o=>o.score>=V);if(console.log(`
49
+ \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550`),console.log(` APPLY QUEUE (${n.length} jobs, min score ${V})`),console.log("\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550"),n.length===0)console.log(" No jobs in queue. Run: npm run scout");else for(let o=0;o<n.length;o++){let{score:t,job:s,jobId:r}=n[o],i=(s.techStack||[]).slice(0,4).join(", ");console.log(` ${String(o+1).padStart(2)}. [${t}] ${(s.title||"?").slice(0,35)} @ ${(s.company||"?").slice(0,20)}`),console.log(` ${s.salary||"no salary"} | ${s.location||"?"} | ${i||"no tech"}`),console.log(` ${s.url}`)}console.log(`\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550
50
+ `)}async function Qo(){if(console.log(`
51
51
  LinkedIn Apply Queue
52
- `),Vo){Go();return}let n=G(Te*3,{easyApplyOnly:!0}).filter(r=>r.score>=q).slice(0,Te);if(n.length===0){console.log(` No jobs to apply. Run: npm run scout
53
- `);return}console.log(` Applying to ${n.length} jobs (score >= ${q}):
54
- `);for(let{score:r,job:i}of n)console.log(` [${r}] ${(i.title||"?").slice(0,40)} @ ${(i.company||"?").slice(0,20)}`);E.existsSync(ne)||E.mkdirSync(ne,{recursive:!0});let o=_.join(ne,"apply-queue-urls.json"),t=n.map(({job:r,jobId:i})=>({url:r.url,title:r.title,company:r.company,location:r.location,salary:r.salary}));E.writeFileSync(o,JSON.stringify(t,null,2)),console.log(`
55
- Generated ${o} with ${t.length} jobs`);for(let{jobId:r}of n)ue(r,{queued:!0,queuedAt:new Date().toISOString().slice(0,10)});let s=Se("apply-queue",`${n.length} jobs`);console.log(`
52
+ `),Yo){Ho();return}let n=G(Ee*3,{easyApplyOnly:!0}).filter(r=>r.score>=V).slice(0,Ee);if(n.length===0){console.log(` No jobs to apply. Run: npm run scout
53
+ `);return}console.log(` Applying to ${n.length} jobs (score >= ${V}):
54
+ `);for(let{score:r,job:i}of n)console.log(` [${r}] ${(i.title||"?").slice(0,40)} @ ${(i.company||"?").slice(0,20)}`);I.existsSync(te)||I.mkdirSync(te,{recursive:!0});let o=F.join(te,"apply-queue-urls.json"),t=n.map(({job:r,jobId:i})=>({url:r.url,title:r.title,company:r.company,location:r.location,salary:r.salary}));I.writeFileSync(o,JSON.stringify(t,null,2)),console.log(`
55
+ Generated ${o} with ${t.length} jobs`);for(let{jobId:r}of n)fe(r,{queued:!0,queuedAt:new Date().toISOString().slice(0,10)});let s=be("apply-queue",`${n.length} jobs`);console.log(`
56
56
  Launching run.mjs --job-urls...
57
- `);try{Uo("node",["run.mjs",`--job-urls=${o}`],{stdio:"inherit",timeout:144e5}),H(s,"success",{applied:n.length})}catch(r){console.error(`
58
- Apply queue error: ${r.message?.slice(0,80)}`),H(s,"error",{applied:n.length},r.message)}try{E.unlinkSync(o)}catch{}}Wo().catch(e=>{console.error("Apply queue failed:",e),process.exit(1)});
57
+ `);try{Bo("node",["run.mjs",`--job-urls=${o}`],{stdio:"inherit",timeout:144e5}),Q(s,"success",{applied:n.length})}catch(r){console.error(`
58
+ Apply queue error: ${r.message?.slice(0,80)}`),Q(s,"error",{applied:n.length},r.message)}try{I.unlinkSync(o)}catch{}}Qo().catch(e=>{console.error("Apply queue failed:",e),process.exit(1)});
package/dist/daily.mjs CHANGED
@@ -1,11 +1,11 @@
1
- var Te=Object.defineProperty;var Z=(e,o)=>{for(var t in o)Te(e,t,{get:o[t],enumerable:!0})};import{execFileSync as Co}from"child_process";import"dotenv/config";import*as S from"fs";import*as R from"path";import{fileURLToPath as Fe}from"url";import"dotenv/config";import{createGraphClient as Ee,resolveToken as De}from"@ilivemylife/graph-sdk";import{config as Ne}from"dotenv";Ne();var ee=De()||process.env.TOKEN;ee||(console.error(`
1
+ var Te=Object.defineProperty;var ee=(e,o)=>{for(var t in o)Te(e,t,{get:o[t],enumerable:!0})};import{execFileSync as _o}from"child_process";import"dotenv/config";import*as S from"fs";import*as O from"path";import{fileURLToPath as _e}from"url";import"dotenv/config";import{createGraphClient as De,resolveToken as Ne}from"@ilivemylife/graph-sdk";import{config as Ce}from"dotenv";Ce();var oe=Ne()||process.env.TOKEN;oe||(console.error(`
2
2
  [FATAL] No iLiveMyLife token found.`),console.error(" The bot uses Lifebot AI to decide which jobs to apply for"),console.error(` and how to fill application forms. It cannot work without it.
3
3
  `),console.error(" Easiest way:"),console.error(" 1. npm install -g @ilivemylife/graph-sdk"),console.error(` 2. ilml login your@email.com yourpassword
4
4
  `),console.error(` Or add ILML_TOKEN=... to .env
5
- `),process.exit(1));var oe=Ee({token:ee});function Ce(e){if(!e||e<1e3)return`${e||0}ms`;let o=Math.round(e/1e3);if(o<60)return`${o}s`;let t=Math.floor(o/60),n=o%60;return n?`${t}m ${n}s`:`${t}m`}function xe({scriptName:e,mode:o,status:t,error:n,duration:s}){let r=t==="error"?"\u2717":t==="quota-reached"?"\u26A0":"\u2713",i=new Date().toISOString().slice(0,16).replace("T"," "),l=s?` ${Ce(s)}`:"",a=t==="error"?`error${n?`: ${String(n).slice(0,80)}`:""}`:t==="quota-reached"?"daily quota reached":"done",u=o&&o!=="default"?` (${o})`:"";return`${r} ${e}${u} \u2014 ${a} \u2014 ${i}${l}`}async function ne({scriptName:e,mode:o,status:t,summaryLines:n,error:s,duration:r}){let i=process.env.NODE_RUN_REPORTS;if(!i||!Array.isArray(n)||n.length===0)return;let a=`**${xe({scriptName:e,mode:o,status:t,error:s,duration:r})}**
5
+ `),process.exit(1));var ne=De({token:oe});function xe(e){if(!e||e<1e3)return`${e||0}ms`;let o=Math.round(e/1e3);if(o<60)return`${o}s`;let t=Math.floor(o/60),n=o%60;return n?`${t}m ${n}s`:`${t}m`}function Fe({scriptName:e,mode:o,status:t,error:n,duration:s}){let r=t==="error"?"\u2717":t==="quota-reached"?"\u26A0":"\u2713",i=new Date().toISOString().slice(0,16).replace("T"," "),l=s?` ${xe(s)}`:"",a=t==="error"?`error${n?`: ${String(n).slice(0,80)}`:""}`:t==="quota-reached"?"daily quota reached":"done",u=o&&o!=="default"?` (${o})`:"";return`${r} ${e}${u} \u2014 ${a} \u2014 ${i}${l}`}async function te({scriptName:e,mode:o,status:t,summaryLines:n,error:s,duration:r}){let i=process.env.NODE_RUN_REPORTS;if(!i||!Array.isArray(n)||n.length===0)return;let a=`**${Fe({scriptName:e,mode:o,status:t,error:s,duration:r})}**
6
6
 
7
7
  ${n.join(`
8
- `)}`;try{await oe.addMessage(i,a)}catch(u){console.error(`[runReport] Failed to post end-of-session message: ${u.message}`)}}var _e=Fe(import.meta.url),Le=R.dirname(R.dirname(_e)),B=process.env.DATA_DIR?R.resolve(process.env.DATA_DIR,"collected-profiles"):R.join(Le,"collected-profiles"),V=R.join(B,"run-log.json");function Pe(e,o){let t=JSON.stringify(o,null,2),n=e+".tmp";S.writeFileSync(n,t);for(let s=0;s<3;s++)try{S.renameSync(n,e);return}catch(r){if(s<2&&(r.code==="EPERM"||r.code==="EBUSY")){let i=Date.now();for(;Date.now()-i<200;);continue}throw r}}function te(){try{if(!S.existsSync(V))return{runs:[]};let e=JSON.parse(S.readFileSync(V,"utf-8"));return!e.runs||!Array.isArray(e.runs)?{runs:[]}:e}catch{return{runs:[]}}}function se(e){S.existsSync(B)||S.mkdirSync(B,{recursive:!0});try{Pe(V,e)}catch(o){console.error(`[!] Failed to save run-log.json: ${o.message}`)}}function re(e,o="default"){let t=te();for(let i of t.runs)i.status==="running"&&(i.status="interrupted",i.result="interrupted",i.timestamp&&(i.duration=Date.now()-new Date(i.timestamp).getTime()));let n=new Date,s={timestamp:n.toISOString(),script:e,mode:o,status:"running",result:null,duration:null,stats:null,error:null};t.runs.push(s);let r=Date.now()-30*24*60*60*1e3;return t.runs=t.runs.filter(i=>new Date(i.timestamp).getTime()>r),se(t),{script:e,mode:o,startTime:n.getTime(),runIndex:t.runs.length-1}}function ie(e,o,t=null,n=null){if(!e)return;let s=null;try{let i=te(),l=i.runs[e.runIndex];if(l&&l.status==="running"&&l.script===e.script)l.status=o,l.result=o,l.duration=Date.now()-e.startTime,l.stats=t,l.error=n,s=l.duration;else{let a=i.runs.find(u=>u.script===e.script&&u.status==="running"&&u.timestamp===new Date(e.startTime).toISOString());a&&(a.status=o,a.result=o,a.duration=Date.now()-e.startTime,a.stats=t,a.error=n,s=a.duration)}se(i)}catch(i){console.error(`[runLog] Failed to end run: ${i.message}`)}let r=t&&Array.isArray(t.summaryLines)?t.summaryLines:null;if(r&&r.length>0&&process.env.NODE_RUN_REPORTS){let i=o==="error"?"error":t&&t.dailyLimitHit?"quota-reached":"done";ne({scriptName:e.script,mode:e.mode,status:i,summaryLines:r,error:n,duration:s??Date.now()-e.startTime}).catch(l=>console.error(`[runLog] postRunReport failed: ${l.message}`))}}import"dotenv/config";import*as d from"fs";import*as h from"path";import{fileURLToPath as Me}from"url";var Je=Me(import.meta.url),Ue=h.dirname(h.dirname(Je)),k=process.env.DATA_DIR?h.resolve(process.env.DATA_DIR,"collected-profiles"):h.join(Ue,"collected-profiles"),Ho=h.join(k,"people.json"),Qo=h.join(k,"jobs.json"),Be=h.join(k,"quota.json"),zo=h.join(k,"conversations.json"),Ko=h.join(k,"sync-state.json"),Xo=h.join(k,"application-questions.json"),Zo=h.join(k,"profile-history"),_=20,L=100,Ve=null,qe=null,I=null,We=null,Ye=null,Ge=null;function ce(){Ve=null,qe=null,I=null,We=null,Ye=null,Ge=null}function He(){d.existsSync(k)||d.mkdirSync(k,{recursive:!0})}function Qe(e,o){if(!d.existsSync(e))return null;try{let t=JSON.parse(d.readFileSync(e,"utf-8"));try{d.copyFileSync(e,e+".backup")}catch{}return t}catch(t){console.error(`[!] ${o} is corrupted: ${t.message}`),console.error(` File: ${h.resolve(e)}`);let n=e+".backup";if(d.existsSync(n))try{let s=JSON.parse(d.readFileSync(n,"utf-8"));console.error(` Restored from ${n}`);try{d.copyFileSync(n,e)}catch{}return s}catch{console.error(" Backup also corrupted!")}console.error(` Starting with empty data. Old file preserved as ${e}.corrupted`);try{d.copyFileSync(e,e+".corrupted")}catch{}return null}}function le(){return new Date().toISOString().split("T")[0]}function ze(){return I||(He(),I=Qe(Be,"quota.json"),(!I||!I.connections)&&(I={connections:[]}),I)}function ae(){let e=ze(),o=new Date;o.setDate(o.getDate()-7);let t=o.toISOString().split("T")[0];return e.connections.filter(n=>n.date>=t)}function ue(){let e=ae(),o=le(),t=e.filter(a=>a.date===o).length,n=e.length,s=t>=_,r=n>=L,i=!s&&!r,l={allowed:i,daily:{sent:t,max:_},weekly:{sent:n,max:L}};return i||(l.reason=s?`Daily limit reached (${t}/${_})`:`Weekly limit reached (${n}/${L})`),l}function fe(){let e=ae(),o=le(),t=e.filter(s=>s.date===o).length,n=e.length;return{daily:{sent:t,max:_,remaining:_-t},weekly:{sent:n,max:L,remaining:L-n}}}import"dotenv/config";import*as c from"fs";import*as Q from"os";import*as f from"path";import{fileURLToPath as go}from"url";var Y={};Z(Y,{NAME:()=>Xe,VERSION:()=>Ke,up:()=>oo});import*as b from"fs";import*as J from"path";var Ke=1,Xe="normalize-jobs-and-questions",Ze=["description","techStack","jobTags","seniority","employmentType","industries","jobFunction","postedTime","applicantCount","companySize","workplaceType","contractType","isEasyApply","applyType","companyUrl","scoutedAt","lastSeen"],eo=50;function q(e){return b.existsSync(e)?JSON.parse(b.readFileSync(e,"utf-8")):null}function W(e,o){let t=JSON.stringify(o,null,2),n=e+".tmp";try{b.writeFileSync(n,t)}catch(s){try{b.unlinkSync(n)}catch{}throw s}try{b.renameSync(n,e)}catch(s){try{b.unlinkSync(n)}catch{}if(s.code==="EPERM"||s.code==="EBUSY"){let r=new Error(`File locked: ${e} (${s.code}). On Windows usually antivirus / file explorer / open editor. Close it and re-run.`);throw r.code=s.code,r}throw s}}async function oo(e,o){let t=J.join(e,"jobs.json"),n=J.join(o,"scouted-jobs.json"),s=J.join(e,"application-questions.json"),r=q(t)||{},i=q(n)||{},l=0,a=[];for(let p of Object.keys(r)){if(!i[p])continue;let O=i[p],m=r[p];for(let A of Ze)O[A]!=null&&m[A]==null&&(m[A]=O[A]);a.push(p),l++}l>0?(W(t,r),console.log(` Phase 1: Merged ${l} scouted jobs into jobs.json`)):console.log(" Phase 1: No overlapping jobs to merge");let u=q(s);if(u&&Array.isArray(u.applications)){let p={},O=0;for(let m of u.applications){let A=m.jobId||"unknown";p[A]||(p[A]={applications:[]});let X=(m.questions||[]).map($=>{let x={type:$.type||"input",question:$.question||"",answer:$.answer||""};return $.options&&($.options.length<=eo?x.options=$.options:(x.optionsOmitted=!0,O++)),$.page&&(x.page=$.page),$.wasPreFilled&&(x.wasPreFilled=!0),$.wasRetry&&(x.wasRetry=!0),x});p[A].applications.push({applicationId:m.applicationId,date:m.date,result:m.result||"submitted",formPages:m.formPages||null,duration:m.duration||null,retries:m.retries||0,failedField:m.failedField||null,questionCount:m.questionCount||X.length,questions:X})}W(s,p),console.log(` Phase 2: Rekeyed ${u.applications.length} applications by jobId (${Object.keys(p).length} unique jobs)`),O>0&&console.log(` Phase 2: Stripped bloated options from ${O} questions`)}else u&&!u.applications?console.log(" Phase 2: application-questions.json already in keyed format"):console.log(" Phase 2: No application-questions.json found");if(a.length>0){for(let p of a)delete i[p];W(n,i),console.log(` Phase 3: Removed ${a.length} merged entries from scouted-jobs.json (${Object.keys(i).length} remaining)`)}else console.log(" Phase 3: No entries to remove from scouted-jobs.json")}var G={};Z(G,{NAME:()=>to,VERSION:()=>no,up:()=>io});import*as v from"fs";import*as pe from"path";var no=2,to="reset-unread-flags";function so(e){return v.existsSync(e)?JSON.parse(v.readFileSync(e,"utf-8")):null}function ro(e,o){let t=JSON.stringify(o,null,2),n=e+".tmp";try{v.writeFileSync(n,t)}catch(s){try{v.unlinkSync(n)}catch{}throw s}try{v.renameSync(n,e)}catch(s){try{v.unlinkSync(n)}catch{}if(s.code==="EPERM"||s.code==="EBUSY"){let r=new Error(`File locked: ${e} (${s.code}). On Windows usually antivirus / file explorer / open editor. Close it and re-run.`);throw r.code=s.code,r}throw s}}async function io(e){let o=pe.join(e,"conversations.json"),t=so(o);if(!t){console.log(" conversations.json not found \u2014 nothing to reset.");return}let n=0;for(let s of Object.keys(t))t[s].unread===!0&&(t[s].unread=!1,n++);if(n===0){console.log(" No stale unread flags found \u2014 nothing to do.");return}ro(o,t),console.log(` Reset unread=false on ${n} conversations. Run 'ilml linkedin sync-all' to re-establish real unread state from LinkedIn.`)}import"dotenv/config";import*as y from"path";import{fileURLToPath as co}from"url";var lo=co(import.meta.url),ao=y.dirname(y.dirname(lo)),P=process.env.DATA_DIR?y.resolve(process.env.DATA_DIR,"market-research"):y.join(ao,"market-research"),tn=y.join(P,"scouted-jobs.json"),sn=y.join(P,"discovered-people.json"),rn=y.join(P,"visit-log.json"),cn=y.join(P,"companies.json"),ln=y.join(P,"job-decisions.json");function de(){uo=null,fo=null,po=null,ho=null,mo=null}var uo=null;var fo=null;var po=null;var ho=null;var mo=null;var yo=go(import.meta.url),So=f.dirname(yo),bo=f.dirname(f.dirname(So));function vo(e){return e&&(e==="~"?Q.homedir():e.startsWith("~/")||e.startsWith("~\\")?f.join(Q.homedir(),e.slice(2)):e)}var w=process.env.DATA_DIR?f.resolve(vo(process.env.DATA_DIR)):bo,N=f.join(w,"collected-profiles"),C=f.join(w,"market-research"),g=f.join(w,".schema-version"),he=[f.join(N,".schema-version"),f.join(C,".schema-version")],T=2,j=[{version:1,module:Y},{version:2,module:G}];(function(){j.sort((n,s)=>n.version-s.version);let o=new Set;for(let n of j){if(!Number.isInteger(n.version)||n.version<1)throw new Error(`MIGRATION_REGISTRY: invalid version ${JSON.stringify(n.version)} (must be positive integer)`);if(o.has(n.version))throw new Error(`MIGRATION_REGISTRY: duplicate version v${n.version}`);o.add(n.version);let s=n.module;if(!s||typeof s.up!="function")throw new Error(`MIGRATION_REGISTRY: v${n.version} module is missing up()`);if(s.VERSION!==void 0&&s.VERSION!==n.version)throw new Error(`MIGRATION_REGISTRY: v${n.version} entry does not match module VERSION (${s.VERSION}). Drift between registry and module \u2014 pick one source of truth.`)}let t=j.length?j[j.length-1].version:0;if(t!==T)throw new Error(`MIGRATION_REGISTRY: highest version v${t} but CURRENT_SCHEMA_VERSION=${T}. Forgot to bump CURRENT_SCHEMA_VERSION when adding the new migration?`);for(let n=0;n<j.length;n++){let s=n+1;if(j[n].version!==s)throw new Error(`MIGRATION_REGISTRY: gap detected \u2014 expected v${s} at position ${n} but found v${j[n].version}`)}})();function Se(){process.env.DATA_DIR!==void 0&&process.env.DATA_DIR.trim()===""&&process.env.DATA_DIR.length>0&&(console.error(`
8
+ `)}`;try{await ne.addMessage(i,a)}catch(u){console.error(`[runReport] Failed to post end-of-session message: ${u.message}`)}}var Le=_e(import.meta.url),Pe=O.dirname(O.dirname(Le)),B=process.env.DATA_DIR?O.resolve(process.env.DATA_DIR,"collected-profiles"):O.join(Pe,"collected-profiles"),W=O.join(B,"run-log.json");function Me(e,o){let t=JSON.stringify(o,null,2),n=e+".tmp";S.writeFileSync(n,t);for(let s=0;s<3;s++)try{S.renameSync(n,e);return}catch(r){if(s<2&&(r.code==="EPERM"||r.code==="EBUSY")){let i=Date.now();for(;Date.now()-i<200;);continue}throw r}}function se(){try{if(!S.existsSync(W))return{runs:[]};let e=JSON.parse(S.readFileSync(W,"utf-8"));return!e.runs||!Array.isArray(e.runs)?{runs:[]}:e}catch{return{runs:[]}}}function re(e){S.existsSync(B)||S.mkdirSync(B,{recursive:!0});try{Me(W,e)}catch(o){console.error(`[!] Failed to save run-log.json: ${o.message}`)}}function ie(e,o="default"){let t=se();for(let i of t.runs)i.status==="running"&&(i.status="interrupted",i.result="interrupted",i.timestamp&&(i.duration=Date.now()-new Date(i.timestamp).getTime()));let n=new Date,s={timestamp:n.toISOString(),script:e,mode:o,status:"running",result:null,duration:null,stats:null,error:null};t.runs.push(s);let r=Date.now()-30*24*60*60*1e3;return t.runs=t.runs.filter(i=>new Date(i.timestamp).getTime()>r),re(t),{script:e,mode:o,startTime:n.getTime(),runIndex:t.runs.length-1}}function ce(e,o,t=null,n=null){if(!e)return;let s=null;try{let i=se(),l=i.runs[e.runIndex];if(l&&l.status==="running"&&l.script===e.script)l.status=o,l.result=o,l.duration=Date.now()-e.startTime,l.stats=t,l.error=n,s=l.duration;else{let a=i.runs.find(u=>u.script===e.script&&u.status==="running"&&u.timestamp===new Date(e.startTime).toISOString());a&&(a.status=o,a.result=o,a.duration=Date.now()-e.startTime,a.stats=t,a.error=n,s=a.duration)}re(i)}catch(i){console.error(`[runLog] Failed to end run: ${i.message}`)}let r=t&&Array.isArray(t.summaryLines)?t.summaryLines:null;if(r&&r.length>0&&process.env.NODE_RUN_REPORTS){let i=o==="error"?"error":t&&t.dailyLimitHit?"quota-reached":"done";te({scriptName:e.script,mode:e.mode,status:i,summaryLines:r,error:n,duration:s??Date.now()-e.startTime}).catch(l=>console.error(`[runLog] postRunReport failed: ${l.message}`))}}import{getPluginConfig as Je}from"@ilivemylife/graph-sdk";var Ue=await Je();for(let[e,o]of Object.entries(Ue))process.env[e]===void 0&&(process.env[e]=o);var zo=process.env.LINKEDIN_NAME,D=Object.freeze({DRAFT:"draft",CONFIRMED:"confirmed",REJECTED:"rejected",NEEDS_REVIEW:"needs_review",SENT:"sent"}),Ko=Object.freeze(new Set([D.DRAFT,D.CONFIRMED,D.NEEDS_REVIEW])),Xo=Object.freeze(new Set([D.DRAFT,D.CONFIRMED,D.REJECTED,D.NEEDS_REVIEW]));import"dotenv/config";import*as d from"fs";import*as h from"path";import{fileURLToPath as Ve}from"url";var Be=Ve(import.meta.url),We=h.dirname(h.dirname(Be)),k=process.env.DATA_DIR?h.resolve(process.env.DATA_DIR,"collected-profiles"):h.join(We,"collected-profiles"),tn=h.join(k,"people.json"),sn=h.join(k,"jobs.json"),qe=h.join(k,"quota.json"),rn=h.join(k,"conversations.json"),cn=h.join(k,"sync-state.json"),ln=h.join(k,"application-questions.json"),an=h.join(k,"profile-history"),L=20,P=100,Ye=null,Ge=null,E=null,He=null,Qe=null,ze=null;function le(){Ye=null,Ge=null,E=null,He=null,Qe=null,ze=null}function Ke(){d.existsSync(k)||d.mkdirSync(k,{recursive:!0})}function Xe(e,o){if(!d.existsSync(e))return null;try{let t=JSON.parse(d.readFileSync(e,"utf-8"));try{d.copyFileSync(e,e+".backup")}catch{}return t}catch(t){console.error(`[!] ${o} is corrupted: ${t.message}`),console.error(` File: ${h.resolve(e)}`);let n=e+".backup";if(d.existsSync(n))try{let s=JSON.parse(d.readFileSync(n,"utf-8"));console.error(` Restored from ${n}`);try{d.copyFileSync(n,e)}catch{}return s}catch{console.error(" Backup also corrupted!")}console.error(` Starting with empty data. Old file preserved as ${e}.corrupted`);try{d.copyFileSync(e,e+".corrupted")}catch{}return null}}function ae(){return new Date().toISOString().split("T")[0]}function Ze(){return E||(Ke(),E=Xe(qe,"quota.json"),(!E||!E.connections)&&(E={connections:[]}),E)}function ue(){let e=Ze(),o=new Date;o.setDate(o.getDate()-7);let t=o.toISOString().split("T")[0];return e.connections.filter(n=>n.date>=t)}function fe(){let e=ue(),o=ae(),t=e.filter(a=>a.date===o).length,n=e.length,s=t>=L,r=n>=P,i=!s&&!r,l={allowed:i,daily:{sent:t,max:L},weekly:{sent:n,max:P}};return i||(l.reason=s?`Daily limit reached (${t}/${L})`:`Weekly limit reached (${n}/${P})`),l}function pe(){let e=ue(),o=ae(),t=e.filter(s=>s.date===o).length,n=e.length;return{daily:{sent:t,max:L,remaining:L-t},weekly:{sent:n,max:P,remaining:P-n}}}import"dotenv/config";import*as c from"fs";import*as z from"os";import*as f from"path";import{fileURLToPath as bo}from"url";var G={};ee(G,{NAME:()=>oo,VERSION:()=>eo,up:()=>so});import*as b from"fs";import*as U from"path";var eo=1,oo="normalize-jobs-and-questions",no=["description","techStack","jobTags","seniority","employmentType","industries","jobFunction","postedTime","applicantCount","companySize","workplaceType","contractType","isEasyApply","applyType","companyUrl","scoutedAt","lastSeen"],to=50;function q(e){return b.existsSync(e)?JSON.parse(b.readFileSync(e,"utf-8")):null}function Y(e,o){let t=JSON.stringify(o,null,2),n=e+".tmp";try{b.writeFileSync(n,t)}catch(s){try{b.unlinkSync(n)}catch{}throw s}try{b.renameSync(n,e)}catch(s){try{b.unlinkSync(n)}catch{}if(s.code==="EPERM"||s.code==="EBUSY"){let r=new Error(`File locked: ${e} (${s.code}). On Windows usually antivirus / file explorer / open editor. Close it and re-run.`);throw r.code=s.code,r}throw s}}async function so(e,o){let t=U.join(e,"jobs.json"),n=U.join(o,"scouted-jobs.json"),s=U.join(e,"application-questions.json"),r=q(t)||{},i=q(n)||{},l=0,a=[];for(let p of Object.keys(r)){if(!i[p])continue;let A=i[p],m=r[p];for(let R of no)A[R]!=null&&m[R]==null&&(m[R]=A[R]);a.push(p),l++}l>0?(Y(t,r),console.log(` Phase 1: Merged ${l} scouted jobs into jobs.json`)):console.log(" Phase 1: No overlapping jobs to merge");let u=q(s);if(u&&Array.isArray(u.applications)){let p={},A=0;for(let m of u.applications){let R=m.jobId||"unknown";p[R]||(p[R]={applications:[]});let Z=(m.questions||[]).map($=>{let F={type:$.type||"input",question:$.question||"",answer:$.answer||""};return $.options&&($.options.length<=to?F.options=$.options:(F.optionsOmitted=!0,A++)),$.page&&(F.page=$.page),$.wasPreFilled&&(F.wasPreFilled=!0),$.wasRetry&&(F.wasRetry=!0),F});p[R].applications.push({applicationId:m.applicationId,date:m.date,result:m.result||"submitted",formPages:m.formPages||null,duration:m.duration||null,retries:m.retries||0,failedField:m.failedField||null,questionCount:m.questionCount||Z.length,questions:Z})}Y(s,p),console.log(` Phase 2: Rekeyed ${u.applications.length} applications by jobId (${Object.keys(p).length} unique jobs)`),A>0&&console.log(` Phase 2: Stripped bloated options from ${A} questions`)}else u&&!u.applications?console.log(" Phase 2: application-questions.json already in keyed format"):console.log(" Phase 2: No application-questions.json found");if(a.length>0){for(let p of a)delete i[p];Y(n,i),console.log(` Phase 3: Removed ${a.length} merged entries from scouted-jobs.json (${Object.keys(i).length} remaining)`)}else console.log(" Phase 3: No entries to remove from scouted-jobs.json")}var H={};ee(H,{NAME:()=>io,VERSION:()=>ro,up:()=>ao});import*as v from"fs";import*as de from"path";var ro=2,io="reset-unread-flags";function co(e){return v.existsSync(e)?JSON.parse(v.readFileSync(e,"utf-8")):null}function lo(e,o){let t=JSON.stringify(o,null,2),n=e+".tmp";try{v.writeFileSync(n,t)}catch(s){try{v.unlinkSync(n)}catch{}throw s}try{v.renameSync(n,e)}catch(s){try{v.unlinkSync(n)}catch{}if(s.code==="EPERM"||s.code==="EBUSY"){let r=new Error(`File locked: ${e} (${s.code}). On Windows usually antivirus / file explorer / open editor. Close it and re-run.`);throw r.code=s.code,r}throw s}}async function ao(e){let o=de.join(e,"conversations.json"),t=co(o);if(!t){console.log(" conversations.json not found \u2014 nothing to reset.");return}let n=0;for(let s of Object.keys(t))t[s].unread===!0&&(t[s].unread=!1,n++);if(n===0){console.log(" No stale unread flags found \u2014 nothing to do.");return}lo(o,t),console.log(` Reset unread=false on ${n} conversations. Run 'ilml linkedin sync-all' to re-establish real unread state from LinkedIn.`)}import"dotenv/config";import*as y from"path";import{fileURLToPath as uo}from"url";var fo=uo(import.meta.url),po=y.dirname(y.dirname(fo)),M=process.env.DATA_DIR?y.resolve(process.env.DATA_DIR,"market-research"):y.join(po,"market-research"),dn=y.join(M,"scouted-jobs.json"),hn=y.join(M,"discovered-people.json"),mn=y.join(M,"visit-log.json"),gn=y.join(M,"companies.json"),yn=y.join(M,"job-decisions.json");function he(){ho=null,mo=null,go=null,yo=null,So=null}var ho=null;var mo=null;var go=null;var yo=null;var So=null;var vo=bo(import.meta.url),$o=f.dirname(vo),wo=f.dirname(f.dirname($o));function ko(e){return e&&(e==="~"?z.homedir():e.startsWith("~/")||e.startsWith("~\\")?f.join(z.homedir(),e.slice(2)):e)}var w=process.env.DATA_DIR?f.resolve(ko(process.env.DATA_DIR)):wo,C=f.join(w,"collected-profiles"),x=f.join(w,"market-research"),g=f.join(w,".schema-version"),me=[f.join(C,".schema-version"),f.join(x,".schema-version")],I=2,j=[{version:1,module:G},{version:2,module:H}];(function(){j.sort((n,s)=>n.version-s.version);let o=new Set;for(let n of j){if(!Number.isInteger(n.version)||n.version<1)throw new Error(`MIGRATION_REGISTRY: invalid version ${JSON.stringify(n.version)} (must be positive integer)`);if(o.has(n.version))throw new Error(`MIGRATION_REGISTRY: duplicate version v${n.version}`);o.add(n.version);let s=n.module;if(!s||typeof s.up!="function")throw new Error(`MIGRATION_REGISTRY: v${n.version} module is missing up()`);if(s.VERSION!==void 0&&s.VERSION!==n.version)throw new Error(`MIGRATION_REGISTRY: v${n.version} entry does not match module VERSION (${s.VERSION}). Drift between registry and module \u2014 pick one source of truth.`)}let t=j.length?j[j.length-1].version:0;if(t!==I)throw new Error(`MIGRATION_REGISTRY: highest version v${t} but CURRENT_SCHEMA_VERSION=${I}. Forgot to bump CURRENT_SCHEMA_VERSION when adding the new migration?`);for(let n=0;n<j.length;n++){let s=n+1;if(j[n].version!==s)throw new Error(`MIGRATION_REGISTRY: gap detected \u2014 expected v${s} at position ${n} but found v${j[n].version}`)}})();function be(){process.env.DATA_DIR!==void 0&&process.env.DATA_DIR.trim()===""&&process.env.DATA_DIR.length>0&&(console.error(`
9
9
  [FATAL] DATA_DIR is set to whitespace ("${process.env.DATA_DIR}"). Either unset it or set a real path.
10
10
  `),process.exit(1));try{c.mkdirSync(w,{recursive:!0})}catch(o){o.code!=="EEXIST"&&(console.error(`
11
11
  [FATAL] Cannot create DATA_DIR (${w}): ${o.message}`),console.error(` Fix the path or permissions and try again.
@@ -13,41 +13,41 @@ ${n.join(`
13
13
  [FATAL] DATA_DIR (${w}) is not accessible: ${o.message}
14
14
  `),process.exit(1)}e.isDirectory()||(console.error(`
15
15
  [FATAL] DATA_DIR (${w}) exists but is NOT a directory.`),console.error(` Looks like a typo \u2014 your DATA_DIR points at a file. Fix the path and try again.
16
- `),process.exit(1));for(let o of[N,C]){let t;try{t=c.lstatSync(o)}catch(n){if(n.code==="ENOENT")continue;throw n}t.isSymbolicLink()&&(console.error(`
16
+ `),process.exit(1));for(let o of[C,x]){let t;try{t=c.lstatSync(o)}catch(n){if(n.code==="ENOENT")continue;throw n}t.isSymbolicLink()&&(console.error(`
17
17
  [FATAL] Data subdirectory is a symbolic link: ${o}`),console.error(" Refusing to follow \u2014 backup code would copy whatever the link points at."),console.error(` If this is intentional, replace the symlink with the real directory.
18
- `),process.exit(1))}}var D=f.join(w,".migration-lock");function $o(){let e=JSON.stringify({pid:process.pid,startedAt:new Date().toISOString()},null,2);try{return c.writeFileSync(D,e,{flag:"wx"}),!0}catch(n){if(n.code!=="EEXIST")throw n}let o;try{o=JSON.parse(c.readFileSync(D,"utf-8"))}catch{o=null}if(o&&Number.isInteger(o.pid)){let n=!1;try{process.kill(o.pid,0),n=!0}catch(i){i.code==="EPERM"&&(n=!0)}let s=24*60*60*1e3,r=1/0;if(o.startedAt){let i=Date.parse(o.startedAt);Number.isNaN(i)||(r=Date.now()-i)}n&&r<s&&(console.error(`
19
- [FATAL] Another plugin process is currently running migrations:`),console.error(` PID: ${o.pid}`),console.error(` Started at: ${o.startedAt||"(unknown)"}`),console.error(` Lock file: ${D}
18
+ `),process.exit(1))}}var N=f.join(w,".migration-lock");function jo(){let e=JSON.stringify({pid:process.pid,startedAt:new Date().toISOString()},null,2);try{return c.writeFileSync(N,e,{flag:"wx"}),!0}catch(n){if(n.code!=="EEXIST")throw n}let o;try{o=JSON.parse(c.readFileSync(N,"utf-8"))}catch{o=null}if(o&&Number.isInteger(o.pid)){let n=!1;try{process.kill(o.pid,0),n=!0}catch(i){i.code==="EPERM"&&(n=!0)}let s=24*60*60*1e3,r=1/0;if(o.startedAt){let i=Date.parse(o.startedAt);Number.isNaN(i)||(r=Date.now()-i)}n&&r<s&&(console.error(`
19
+ [FATAL] Another plugin process is currently running migrations:`),console.error(` PID: ${o.pid}`),console.error(` Started at: ${o.startedAt||"(unknown)"}`),console.error(` Lock file: ${N}
20
20
  `),console.error(" Wait for it to finish, or if you're sure it's not actually running,"),console.error(` delete the lock file manually and re-run.
21
- `),process.exit(1)),n&&r>=s&&console.log(` Lock holder PID ${o.pid} is alive but lock is older than 24h \u2014 assuming PID reuse, taking over.`)}console.log(` Stale migration lock from PID ${o?.pid??"?"} found \u2014 taking over.`);let t=D+".tmp";try{return c.writeFileSync(t,e),c.renameSync(t,D),!0}catch(n){try{c.unlinkSync(t)}catch{}throw new Error(`Failed to take over stale migration lock: ${n.message}`)}}function z(){try{if(JSON.parse(c.readFileSync(D,"utf-8")).pid!==process.pid)return;c.unlinkSync(D)}catch{}}var me=!1;function wo(){if(me)return;me=!0;let e=()=>{try{z()}catch{}process.exit(1)};for(let o of["SIGINT","SIGTERM","SIGHUP"])try{process.on(o,e)}catch{}}function ko(){if(c.existsSync(g))return;let e=[],o=[];for(let r of he)if(c.existsSync(r))try{e.push({file:r,schema:U(r)})}catch(i){o.push({file:r,err:i.message})}if(o.length>0){console.error(`
21
+ `),process.exit(1)),n&&r>=s&&console.log(` Lock holder PID ${o.pid} is alive but lock is older than 24h \u2014 assuming PID reuse, taking over.`)}console.log(` Stale migration lock from PID ${o?.pid??"?"} found \u2014 taking over.`);let t=N+".tmp";try{return c.writeFileSync(t,e),c.renameSync(t,N),!0}catch(n){try{c.unlinkSync(t)}catch{}throw new Error(`Failed to take over stale migration lock: ${n.message}`)}}function K(){try{if(JSON.parse(c.readFileSync(N,"utf-8")).pid!==process.pid)return;c.unlinkSync(N)}catch{}}var ge=!1;function Ao(){if(ge)return;ge=!0;let e=()=>{try{K()}catch{}process.exit(1)};for(let o of["SIGINT","SIGTERM","SIGHUP"])try{process.on(o,e)}catch{}}function Ro(){if(c.existsSync(g))return;let e=[],o=[];for(let r of me)if(c.existsSync(r))try{e.push({file:r,schema:V(r)})}catch(i){o.push({file:r,err:i.message})}if(o.length>0){console.error(`
22
22
  [FATAL] Legacy .schema-version file(s) corrupted \u2014 cannot determine current schema version:`);for(let r of o)console.error(` ${r.file}: ${r.err}`);console.error(`
23
23
  Repair the file(s) (likely just an integer 'version' field \u2014 see another working DATA_DIR`),console.error(` or default to {"version":1,"migrations":[]} if you're sure you're on schema v1) and try again.
24
24
  `),process.exit(1)}if(e.length===0)return;let t=e.map(r=>r.schema.version);if(new Set(t).size>1){console.error(`
25
25
  [FATAL] Legacy .schema-version files disagree:`);for(let r of e)console.error(` ${r.file}: v${r.schema.version}`);console.error(" This means a previous migration crashed between writing the two files."),console.error(` Reconcile manually (delete the wrong one, keep the right one), then re-run.
26
- `),process.exit(1)}let n=e[0].schema,s=g+".tmp";try{c.writeFileSync(s,JSON.stringify(n,null,2)),c.renameSync(s,g)}catch(r){try{c.unlinkSync(s)}catch{}throw r}for(let r of he)if(c.existsSync(r))try{c.unlinkSync(r)}catch{}console.log(` Consolidated legacy .schema-version files \u2192 ${g}`)}function jo(e){if(e===null||typeof e!="object"||Array.isArray(e))return null;let o=Number(e.version);return!Number.isFinite(o)||o<0||!Number.isInteger(o)?null:(e.version=o,Array.isArray(e.migrations)||(e.migrations=[]),e)}function U(e){let o=c.readFileSync(e,"utf-8");return o.charCodeAt(0)===65279&&(o=o.slice(1)),JSON.parse(o)}function ge(){if(ko(),!c.existsSync(g))return{version:0,migrations:[]};let e;try{e=U(g)}catch(t){if(t.code==="ENOENT")return{version:0,migrations:[]};console.error(`
26
+ `),process.exit(1)}let n=e[0].schema,s=g+".tmp";try{c.writeFileSync(s,JSON.stringify(n,null,2)),c.renameSync(s,g)}catch(r){try{c.unlinkSync(s)}catch{}throw r}for(let r of me)if(c.existsSync(r))try{c.unlinkSync(r)}catch{}console.log(` Consolidated legacy .schema-version files \u2192 ${g}`)}function Oo(e){if(e===null||typeof e!="object"||Array.isArray(e))return null;let o=Number(e.version);return!Number.isFinite(o)||o<0||!Number.isInteger(o)?null:(e.version=o,Array.isArray(e.migrations)||(e.migrations=[]),e)}function V(e){let o=c.readFileSync(e,"utf-8");return o.charCodeAt(0)===65279&&(o=o.slice(1)),JSON.parse(o)}function ye(){if(Ro(),!c.existsSync(g))return{version:0,migrations:[]};let e;try{e=V(g)}catch(t){if(t.code==="ENOENT")return{version:0,migrations:[]};console.error(`
27
27
  [FATAL] ${g} is not valid JSON: ${t.message}`),console.error(" Refusing to proceed \u2014 silently treating it as 'fresh install' would re-apply"),console.error(" every migration on top of already-migrated data and corrupt it."),console.error(' Repair the file (set it to {"version":N,"migrations":[...]} matching your real state)'),console.error(` or restore from the *.pre-vNNN.backup files if you have them.
28
- `),process.exit(1)}let o=jo(e);return o===null&&(console.error(`
28
+ `),process.exit(1)}let o=Oo(e);return o===null&&(console.error(`
29
29
  [FATAL] ${g} has invalid structure:`),console.error(` ${JSON.stringify(e)?.slice(0,200)||"(unparseable)"}`),console.error(' Expected shape: { "version": <integer>, "migrations": [...] }'),console.error(` Repair the file or restore from a backup.
30
- `),process.exit(1)),o}function be(e){try{c.chmodSync(e,384)}catch{}}function Oo(e){if(Se(),c.existsSync(g))try{if(c.lstatSync(g).isSymbolicLink())throw new Error(`SCHEMA_FILE is a symbolic link: ${g}. Refusing to write through it (potential symlink attack). Remove the link and re-run.`)}catch(t){if(t.message?.includes("symbolic link"))throw t}let o=g+".tmp";try{c.writeFileSync(o,JSON.stringify(e,null,2)),be(o),c.renameSync(o,g)}catch(t){try{c.unlinkSync(o)}catch{}throw t}}function ye(e,o){if(!c.existsSync(e))return;let t=`.pre-v${String(o).padStart(3,"0")}.backup`,n=c.readdirSync(e).filter(l=>{let a=l.toLowerCase();if(a.includes(".backup")||a===".schema-version"||a.endsWith(".tmp"))return!1;try{return c.statSync(f.join(e,l)).isFile()}catch{return!1}}),s=0,r=0,i=[];for(let l of n){let a=f.join(e,l),u=f.join(e,l+t);if(c.existsSync(u)){r++;continue}try{$e(a,u),s++}catch(p){i.push({file:l,err:p.message})}}if(i.length>0){let l=new Error(`Failed to back up ${i.length} file(s) in ${f.basename(e)}/: `+i.map(a=>`${a.file} (${a.err})`).join(", "));throw l.code="BACKUP_INCOMPLETE",l}if(s>0||r>0){let l=r>0?` (${r} pre-existing backup(s) preserved)`:"";console.log(` Backed up ${s} files in ${f.basename(e)}/ (${t})${l}`)}}var H=!1;async function ve(){if(H)return;Se(),Eo();let o=ge().version;if(o===T){H=!0;return}wo(),$o(),process.on("exit",z),o>T&&(console.error(`
31
- [FATAL] Your data schema is at v${o}, but this plugin build only understands up to v${T}.`),console.error(" This usually means you downgraded the plugin without rolling back data."),console.error(` Operating on the newer-format data with this build would risk corrupting it.
32
- `),console.error(" Options:"),console.error(" A) Re-install the newer plugin version that produced this schema, then"),console.error(" (if you really want to downgrade) run 'rollback-data --confirm' first,"),console.error(" then re-install the older plugin version."),console.error(` B) Manually restore your data from the *.pre-vNNN.backup files matching v${T}.
30
+ `),process.exit(1)),o}function ve(e){try{c.chmodSync(e,384)}catch{}}function Eo(e){if(be(),c.existsSync(g))try{if(c.lstatSync(g).isSymbolicLink())throw new Error(`SCHEMA_FILE is a symbolic link: ${g}. Refusing to write through it (potential symlink attack). Remove the link and re-run.`)}catch(t){if(t.message?.includes("symbolic link"))throw t}let o=g+".tmp";try{c.writeFileSync(o,JSON.stringify(e,null,2)),ve(o),c.renameSync(o,g)}catch(t){try{c.unlinkSync(o)}catch{}throw t}}function Se(e,o){if(!c.existsSync(e))return;let t=`.pre-v${String(o).padStart(3,"0")}.backup`,n=c.readdirSync(e).filter(l=>{let a=l.toLowerCase();if(a.includes(".backup")||a===".schema-version"||a.endsWith(".tmp"))return!1;try{return c.statSync(f.join(e,l)).isFile()}catch{return!1}}),s=0,r=0,i=[];for(let l of n){let a=f.join(e,l),u=f.join(e,l+t);if(c.existsSync(u)){r++;continue}try{we(a,u),s++}catch(p){i.push({file:l,err:p.message})}}if(i.length>0){let l=new Error(`Failed to back up ${i.length} file(s) in ${f.basename(e)}/: `+i.map(a=>`${a.file} (${a.err})`).join(", "));throw l.code="BACKUP_INCOMPLETE",l}if(s>0||r>0){let l=r>0?` (${r} pre-existing backup(s) preserved)`:"";console.log(` Backed up ${s} files in ${f.basename(e)}/ (${t})${l}`)}}var Q=!1;async function $e(){if(Q)return;be(),Co();let o=ye().version;if(o===I){Q=!0;return}Ao(),jo(),process.on("exit",K),o>I&&(console.error(`
31
+ [FATAL] Your data schema is at v${o}, but this plugin build only understands up to v${I}.`),console.error(" This usually means you downgraded the plugin without rolling back data."),console.error(` Operating on the newer-format data with this build would risk corrupting it.
32
+ `),console.error(" Options:"),console.error(" A) Re-install the newer plugin version that produced this schema, then"),console.error(" (if you really want to downgrade) run 'rollback-data --confirm' first,"),console.error(" then re-install the older plugin version."),console.error(` B) Manually restore your data from the *.pre-vNNN.backup files matching v${I}.
33
33
  `),process.exit(1)),console.log(`
34
- \u{1F4E6} Schema migration: v${o} \u2192 v${T}`);let t=Do();if(!t.ok){console.error(`
34
+ \u{1F4E6} Schema migration: v${o} \u2192 v${I}`);let t=xo();if(!t.ok){console.error(`
35
35
  [FATAL] Cannot migrate \u2014 current data is corrupted:`);for(let s of t.broken)console.error(` ${s.dir}/${s.file}: ${s.err}`);console.error(`
36
36
  Migration ABORTED. No backups taken, no files modified.`),console.error(" Repair the file(s) above (restore from your own backup, or fix manually)"),console.error(` and try again. To inspect: 'ilml linkedin migrate-status'.
37
37
  `),process.exit(1)}let n=j.filter(s=>s.version>o);for(let s of n){console.log(`
38
- Running migration v${String(s.version).padStart(3,"0")}...`);let r=`v${String(s.version).padStart(3,"0")}`,i=`.pre-${r}.backup`;try{ye(N,s.version),ye(C,s.version)}catch(a){console.error(`
38
+ Running migration v${String(s.version).padStart(3,"0")}...`);let r=`v${String(s.version).padStart(3,"0")}`,i=`.pre-${r}.backup`;try{Se(C,s.version),Se(x,s.version)}catch(a){console.error(`
39
39
  [FATAL] Pre-migration backup failed for ${r}: ${a.message}`),console.error(" Migration NOT applied. Repair the underlying issue (disk space, permissions, locked files)"),console.error(` and re-run the plugin command.
40
- `),process.exit(1)}let l=s.module;try{await l.up(N,C)}catch(a){console.error(`
41
- [FATAL] Migration ${r} failed: ${a.message}`),console.error(` Attempting auto-rollback from ${i} files...`);try{let u=No(i);u.ok?(console.error(` \u2713 Auto-rollback restored ${u.restored} files. Schema stays at v${o}.`),console.error(` The migration will be retried on next plugin command. Fix the root cause first.
40
+ `),process.exit(1)}let l=s.module;try{await l.up(C,x)}catch(a){console.error(`
41
+ [FATAL] Migration ${r} failed: ${a.message}`),console.error(` Attempting auto-rollback from ${i} files...`);try{let u=Fo(i);u.ok?(console.error(` \u2713 Auto-rollback restored ${u.restored} files. Schema stays at v${o}.`),console.error(` The migration will be retried on next plugin command. Fix the root cause first.
42
42
  `)):(console.error(` [!] Auto-rollback INCOMPLETE: ${u.error}`),console.error(` Manual recovery: copy *${i} files back over the originals.
43
43
  `))}catch(u){console.error(` [!] Auto-rollback CRASHED while restoring: ${u.message}`),console.error(` Manual recovery: copy *${i} files back over the originals.
44
- `)}process.exit(1)}try{let a=new Date().toISOString(),u={version:s.version,name:l.NAME||r,appliedAt:a},p=ge();p.version=s.version,p.migratedAt=a,p.migrations=p.migrations||[],p.migrations.push(u),Oo(p)}catch(a){console.error(`
44
+ `)}process.exit(1)}try{let a=new Date().toISOString(),u={version:s.version,name:l.NAME||r,appliedAt:a},p=ye();p.version=s.version,p.migratedAt=a,p.migrations=p.migrations||[],p.migrations.push(u),Eo(p)}catch(a){console.error(`
45
45
  [FATAL] Migration ${r} applied successfully but schema version file could NOT be updated: ${a.message}`),console.error(` Your data is in the v${s.version} state but .schema-version still shows v${o}.`),console.error(` On next plugin command the runner will try to re-apply ${r}. If that migration is`),console.error(` idempotent (most are), it will succeed harmlessly. If you're unsure, run 'data-cleanup' and contact support.
46
- `),process.exit(1)}console.log(` \u2713 Migration ${r} complete`)}try{ce()}catch{}try{de()}catch{}H=!0,z(),console.log(`
47
- \u2713 Schema is now at v${T}
48
- `)}var Ao=[/\.json\.tmp$/,/\.schema-version\.tmp$/,/\.backup\.tmp$/];function Ro(e){let o=e.toLowerCase();return Ao.some(t=>t.test(o))}var Io=6e4;function To(){let e=Date.now(),o=[];for(let t of[w,N,C]){if(!c.existsSync(t))continue;let n;try{n=c.readdirSync(t)}catch{continue}for(let s of n){if(!Ro(s))continue;let r=f.join(t,s);try{let i=c.statSync(r);if(!i.isFile()||e-i.mtimeMs<Io)continue;o.push({dir:t,file:s,fullPath:r})}catch{}}}return o}function Eo({verbose:e=!1}={}){let o=To();if(o.length===0)return e&&console.log(" No orphan .tmp files found."),0;let t=0;for(let n of o)try{c.unlinkSync(n.fullPath),t++,e&&console.log(` Removed orphan: ${f.basename(n.dir)}/${n.file}`)}catch(s){console.warn(` [!] Could not remove orphan ${n.fullPath}: ${s.message}`)}return!e&&t>0&&console.log(` Cleaned up ${t} orphan .tmp file(s) from previous run.`),t}function Do(){let e=[];for(let o of[N,C]){if(!c.existsSync(o))continue;let t=c.readdirSync(o).filter(n=>{let s=n.toLowerCase();return s.endsWith(".json")&&!s.includes(".backup")});for(let n of t){let s=f.join(o,n);try{U(s)}catch(r){e.push({dir:f.basename(o),file:n,err:r.message})}}}return e.length===0?{ok:!0}:{ok:!1,broken:e}}function No(e){let o=[],t=[];for(let r of[N,C]){if(!c.existsSync(r))continue;let i=c.readdirSync(r).filter(l=>l.endsWith(e));for(let l of i){let a=f.join(r,l),u=l.slice(0,-e.length),p=f.join(r,u);try{u.toLowerCase().endsWith(".json")?U(a):c.accessSync(a,c.constants.R_OK),o.push({backupPath:a,originalPath:p,fileName:l,dir:r})}catch(O){t.push(`${f.basename(r)}/${l}: ${O.message}`)}}}if(t.length>0)return{ok:!1,error:t.join("; "),restored:0};let n=0,s=[];for(let{backupPath:r,originalPath:i,fileName:l,dir:a}of o)try{$e(r,i),n++}catch(u){s.push(`${f.basename(a)}/${l}: ${u.message}`)}return s.length>0?{ok:!1,error:s.join("; "),restored:n}:{ok:!0,restored:n}}function $e(e,o){let t=o+".tmp";try{c.copyFileSync(e,t),be(t)}catch(n){try{c.unlinkSync(t)}catch{}throw n}try{c.renameSync(t,o)}catch(n){try{c.unlinkSync(t)}catch{}if(n.code==="EPERM"||n.code==="EBUSY"){let s=new Error(`File locked: ${o} (${n.code}). On Windows this is usually antivirus / Search Indexer / the file open in another app. Close anything that might be holding it and re-run.`);throw s.code=n.code,s}throw n}}await ve();var M=process.argv.slice(2),K=new Set((M.find(e=>e.startsWith("--skip="))?.split("=")[1]||"").split(",").filter(Boolean)),we=new Set((M.find(e=>e.startsWith("--only="))?.split("=")[1]||"").split(",").filter(Boolean)),Oe=M.includes("--dry-run"),Ae=M.includes("--jobs"),Re=M.includes("--comms"),ke=["apply-queue","apply","funnel","scout","visit"],je=["sync","today"],Ie=Ae?ke:Re?je:[...ke,...je];function E(e){return we.size>0?we.has(e):!K.has(e)&&Ie.includes(e)}function F(e,o,t=[],{optional:n=!1}={}){if(console.log(`
49
- ${"\u2550".repeat(50)}`),console.log(` STEP: ${e}`),console.log("\u2550".repeat(50)),Oe)return console.log(` [DRY RUN] Would run: node ${o} ${t.join(" ")}`),!0;try{return Co("node",[o,...t],{stdio:"inherit",timeout:18e6}),console.log(` \u2713 ${e} completed`),!0}catch(s){return n?(console.log(` \u26A0 ${e} failed (optional): ${s.message?.slice(0,80)}`),!1):(console.error(` \u2717 ${e} failed: ${s.message?.slice(0,80)}`),!1)}}async function xo(){let e=Ae?"jobs":Re?"comms":"full";console.log(`
50
- \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550`),console.log(` DAILY PIPELINE (${e})`),console.log("\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550");let o=Ie.filter(E);console.log(` Steps: ${o.join(" \u2192 ")}`),K.size>0&&console.log(` Skipping: ${[...K].join(", ")}`),Oe&&console.log(" [DRY RUN MODE]");let t=re("daily",e),n={};if(E("apply-queue")&&(n["apply-queue"]=F("Apply Queue (scored jobs)","applyQueue.mjs",["--max=50","--min-score=50"],{optional:!0})),E("apply")){let r=fe();n.apply=F("Apply from Search","run.mjs",[],{optional:!0})}if(E("funnel")&&!E("apply")){let r=ue();r.allowed?n.funnel=F("Connection Funnel","funnelRunner.mjs",["--max=20"],{optional:!0}):(console.log(`
51
- \u26A0 Funnel skipped: ${r.reason}`),n.funnel="skipped_quota")}E("scout")&&(n.scout=F("Scout Jobs for Tomorrow","scout.mjs",["--max=300","--verbose"],{optional:!0})),E("sync")&&(n.sync=F("Sync Inbox","syncAll.mjs")),E("today")&&(n.today=F("Daily Report","syncAll.mjs",["--today"])),console.log(`
46
+ `),process.exit(1)}console.log(` \u2713 Migration ${r} complete`)}try{le()}catch{}try{he()}catch{}Q=!0,K(),console.log(`
47
+ \u2713 Schema is now at v${I}
48
+ `)}var Io=[/\.json\.tmp$/,/\.schema-version\.tmp$/,/\.backup\.tmp$/];function To(e){let o=e.toLowerCase();return Io.some(t=>t.test(o))}var Do=6e4;function No(){let e=Date.now(),o=[];for(let t of[w,C,x]){if(!c.existsSync(t))continue;let n;try{n=c.readdirSync(t)}catch{continue}for(let s of n){if(!To(s))continue;let r=f.join(t,s);try{let i=c.statSync(r);if(!i.isFile()||e-i.mtimeMs<Do)continue;o.push({dir:t,file:s,fullPath:r})}catch{}}}return o}function Co({verbose:e=!1}={}){let o=No();if(o.length===0)return e&&console.log(" No orphan .tmp files found."),0;let t=0;for(let n of o)try{c.unlinkSync(n.fullPath),t++,e&&console.log(` Removed orphan: ${f.basename(n.dir)}/${n.file}`)}catch(s){console.warn(` [!] Could not remove orphan ${n.fullPath}: ${s.message}`)}return!e&&t>0&&console.log(` Cleaned up ${t} orphan .tmp file(s) from previous run.`),t}function xo(){let e=[];for(let o of[C,x]){if(!c.existsSync(o))continue;let t=c.readdirSync(o).filter(n=>{let s=n.toLowerCase();return s.endsWith(".json")&&!s.includes(".backup")});for(let n of t){let s=f.join(o,n);try{V(s)}catch(r){e.push({dir:f.basename(o),file:n,err:r.message})}}}return e.length===0?{ok:!0}:{ok:!1,broken:e}}function Fo(e){let o=[],t=[];for(let r of[C,x]){if(!c.existsSync(r))continue;let i=c.readdirSync(r).filter(l=>l.endsWith(e));for(let l of i){let a=f.join(r,l),u=l.slice(0,-e.length),p=f.join(r,u);try{u.toLowerCase().endsWith(".json")?V(a):c.accessSync(a,c.constants.R_OK),o.push({backupPath:a,originalPath:p,fileName:l,dir:r})}catch(A){t.push(`${f.basename(r)}/${l}: ${A.message}`)}}}if(t.length>0)return{ok:!1,error:t.join("; "),restored:0};let n=0,s=[];for(let{backupPath:r,originalPath:i,fileName:l,dir:a}of o)try{we(r,i),n++}catch(u){s.push(`${f.basename(a)}/${l}: ${u.message}`)}return s.length>0?{ok:!1,error:s.join("; "),restored:n}:{ok:!0,restored:n}}function we(e,o){let t=o+".tmp";try{c.copyFileSync(e,t),ve(t)}catch(n){try{c.unlinkSync(t)}catch{}throw n}try{c.renameSync(t,o)}catch(n){try{c.unlinkSync(t)}catch{}if(n.code==="EPERM"||n.code==="EBUSY"){let s=new Error(`File locked: ${o} (${n.code}). On Windows this is usually antivirus / Search Indexer / the file open in another app. Close anything that might be holding it and re-run.`);throw s.code=n.code,s}throw n}}await $e();var J=process.argv.slice(2),X=new Set((J.find(e=>e.startsWith("--skip="))?.split("=")[1]||"").split(",").filter(Boolean)),ke=new Set((J.find(e=>e.startsWith("--only="))?.split("=")[1]||"").split(",").filter(Boolean)),Re=J.includes("--dry-run"),Oe=J.includes("--jobs"),Ee=J.includes("--comms"),je=["apply-queue","apply","funnel","scout","visit"],Ae=["sync","today"],Ie=Oe?je:Ee?Ae:[...je,...Ae];function T(e){return ke.size>0?ke.has(e):!X.has(e)&&Ie.includes(e)}function _(e,o,t=[],{optional:n=!1}={}){if(console.log(`
49
+ ${"\u2550".repeat(50)}`),console.log(` STEP: ${e}`),console.log("\u2550".repeat(50)),Re)return console.log(` [DRY RUN] Would run: node ${o} ${t.join(" ")}`),!0;try{return _o("node",[o,...t],{stdio:"inherit",timeout:18e6}),console.log(` \u2713 ${e} completed`),!0}catch(s){return n?(console.log(` \u26A0 ${e} failed (optional): ${s.message?.slice(0,80)}`),!1):(console.error(` \u2717 ${e} failed: ${s.message?.slice(0,80)}`),!1)}}async function Lo(){let e=Oe?"jobs":Ee?"comms":"full";console.log(`
50
+ \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550`),console.log(` DAILY PIPELINE (${e})`),console.log("\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550");let o=Ie.filter(T);console.log(` Steps: ${o.join(" \u2192 ")}`),X.size>0&&console.log(` Skipping: ${[...X].join(", ")}`),Re&&console.log(" [DRY RUN MODE]");let t=ie("daily",e),n={};if(T("apply-queue")&&(n["apply-queue"]=_("Apply Queue (scored jobs)","applyQueue.mjs",["--max=50","--min-score=50"],{optional:!0})),T("apply")){let r=pe();n.apply=_("Apply from Search","run.mjs",[],{optional:!0})}if(T("funnel")&&!T("apply")){let r=fe();r.allowed?n.funnel=_("Connection Funnel","funnelRunner.mjs",["--max=20"],{optional:!0}):(console.log(`
51
+ \u26A0 Funnel skipped: ${r.reason}`),n.funnel="skipped_quota")}T("scout")&&(n.scout=_("Scout Jobs for Tomorrow","scout.mjs",["--max=300","--verbose"],{optional:!0})),T("sync")&&(n.sync=_("Sync Inbox","syncAll.mjs")),T("today")&&(n.today=_("Daily Report","syncAll.mjs",["--today"])),console.log(`
52
52
  \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550`),console.log(" PIPELINE COMPLETE"),console.log("\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550");for(let[r,i]of Object.entries(n))console.log(` ${i===!0?"\u2713":i===!1?"\u2717":"\u26A0"} ${r}: ${i===!0?"success":i===!1?"failed":i}`);console.log(`\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550
53
- `);let s=Object.values(n).some(r=>r===!1);ie(t,s?"partial":"success",n)}xo().catch(e=>{console.error("Daily pipeline failed:",e),process.exit(1)});
53
+ `);let s=Object.values(n).some(r=>r===!1);ce(t,s?"partial":"success",n)}Lo().catch(e=>{console.error("Daily pipeline failed:",e),process.exit(1)});