ilml-plugin-linkedin 1.11.5 → 1.11.6

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,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.11.6
4
+ - **`lastMessageBy` is now correct across the whole inbox, not just freshly-synced threads.** v1.11.5 fixed the card-vs-tail mismatch only in the Phase 2 re-read path; threads that didn't get re-read in any given run still kept whatever stale `lastMessageBy` was on disk, because `enrichment.mjs` had a `_cardSynced` gate that refused to recompute it during enrichment. After you sent a message via push-drafts, the conversation could keep showing `lastMessageBy: 'them'` and `conversationStatus: 'unanswered_by_us'` for hours — `today` would list it under "needs reply" even though the ball was in their court. Enrichment now always derives `lastMessageBy` from `messages[]`, regardless of whether the card was synced in this run.
5
+ - **`lastMessageBy` is now derived from the latest message by time, not by array position.** When a post-send sync appends a message that's older-by-clock than one already at the end of `messages[]`, the array order drifts out of chronological order. The old logic walked the array from the tail, so it picked the wrong sender. The new logic sorts by parsed timestamp first.
6
+ - **Push-drafts no longer fails when LinkedIn's compose page renders a slightly different variant.** The wait-for-composer step used a single selector (`div.msg-form__contenteditable`); some compose-page variants render the input as `div[contenteditable="true"][role="textbox"]` instead, and only the type-message step had a fallback for it. The wait step now accepts either selector, matching the rest of the file.
7
+ - **Schema migration v003**: re-runs the corrected `lastMessageBy` / `conversationStatus` computation across all existing conversations on first launch. No messages or notes touched — only the derived fields. Backed up to `*.pre-v003.backup`.
8
+
3
9
  ## 1.11.5
4
10
  - **`lastMessageBy` now reflects who actually wrote the latest message.** For InMail / starred / subject-line threads, LinkedIn's sidebar snippet never shows the "You:" prefix, so the card extractor defaulted `lastMessageBy` to `'them'` even when the latest message was yours — `today` and other reports would then list those threads under "ball in their court" or "need reply" indefinitely. After a Phase 2 re-read, sync now derives `lastMessageBy` from the tail of `messages[]` (the authoritative source) instead of trusting the card. The card-based `sender_changed` re-read trigger is also tightened: it now fires only on the positive "card says we wrote" signal, not on the unreliable `'them'` fallback.
5
11
  - No schema migration; existing data files unchanged.
@@ -1,58 +1,58 @@
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(`
1
+ var Ce=Object.defineProperty;var G=(e,n)=>{for(var o in n)Ce(e,o,{get:n[o],enumerable:!0})};import"dotenv/config";import*as D from"fs";import*as L from"path";import{fileURLToPath as Zo}from"url";import{execFileSync as en}from"child_process";import{getPluginConfig as _e}from"@ilivemylife/graph-sdk";var xe=await _e();for(let[e,n]of Object.entries(xe))process.env[e]===void 0&&(process.env[e]=n);var an=process.env.LINKEDIN_NAME,N=Object.freeze({DRAFT:"draft",CONFIRMED:"confirmed",REJECTED:"rejected",NEEDS_REVIEW:"needs_review",SENT:"sent"}),un=Object.freeze(new Set([N.DRAFT,N.CONFIRMED,N.NEEDS_REVIEW])),fn=Object.freeze(new Set([N.DRAFT,N.CONFIRMED,N.REJECTED,N.NEEDS_REVIEW]));import"dotenv/config";import*as h from"fs";import*as g from"path";import{fileURLToPath as Fe}from"url";var Le=Fe(import.meta.url),Me=g.dirname(g.dirname(Le)),E=process.env.DATA_DIR?g.resolve(process.env.DATA_DIR,"collected-profiles"):g.join(Me,"collected-profiles"),Pe=g.join(E,"people.json"),gn=g.join(E,"jobs.json"),yn=g.join(E,"quota.json"),Sn=g.join(E,"conversations.json"),bn=g.join(E,"sync-state.json"),vn=g.join(E,"application-questions.json"),wn=g.join(E,"profile-history");var P=null,Je=null,Ue=null,Be=null,qe=null,Ve=null;function le(){P=null,Je=null,Ue=null,Be=null,qe=null,Ve=null}function We(){h.existsSync(E)||h.mkdirSync(E,{recursive:!0})}function Ge(e,n){if(!h.existsSync(e))return null;try{let o=JSON.parse(h.readFileSync(e,"utf-8"));try{h.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(h.existsSync(t))try{let s=JSON.parse(h.readFileSync(t,"utf-8"));console.error(` Restored from ${t}`);try{h.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{h.copyFileSync(e,e+".corrupted")}catch{}return null}}function ae(){return P||(We(),P=Ge(Pe,"people.json")||{},P)}import"dotenv/config";import*as v from"fs";import*as b from"path";import{fileURLToPath as Ye}from"url";var He=Ye(import.meta.url),Ke=b.dirname(b.dirname(He)),C=process.env.DATA_DIR?b.resolve(process.env.DATA_DIR,"market-research"):b.join(Ke,"market-research"),ue=b.join(C,"scouted-jobs.json"),jn=b.join(C,"discovered-people.json"),On=b.join(C,"visit-log.json"),En=b.join(C,"companies.json"),Qe=b.join(C,"job-decisions.json");function ze(){v.existsSync(C)||v.mkdirSync(C,{recursive:!0})}function Xe(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 fe(e){try{if(v.existsSync(e))return JSON.parse(v.readFileSync(e,"utf-8"))}catch{}return null}function pe(){J=null,U=null,Ze=null,eo=null,oo=null}var J=null;function Y(){return J||(J=fe(ue)||{},J)}function de(e,n){if(!e)return;ze();let o=Y(),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{Xe(ue,o)}catch(s){console.error(`[!] Failed to save scouted-jobs.json: ${s.message}`)}}var U=null;function me(){return U||(U=fe(Qe)||{},U)}var Ze=null;var eo=null;var oo=null;var no=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"]),to=new Set(["iot","machine learning","ai","data engineering","spark","go","rust","kotlin","next.js","devops","sre"]),he=["vancouver","british columbia"],so=["canada","montreal","quebec","alberta","calgary","ottawa"],ro=["toronto","ontario"],io=["india","pakistan","nigeria","philippines"];function co(e,n={}){let o=0,t=(e.techStack||[]).map(a=>a.toLowerCase()),s=0,r=0;for(let a of t)no.has(a)&&s++,to.has(a)&&r++;if(o+=Math.min(s*6,24),o+=Math.min(r*3,6),e.salary){let a=lo(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";io.some(a=>i.includes(a))?o-=30:u&&/united states|usa|\bus\b/i.test(i)?o+=25:u&&he.some(a=>i.includes(a))?o+=22:u?o+=20:he.some(a=>i.includes(a))?o+=18:so.some(a=>i.includes(a))?o+=10:ro.some(a=>i.includes(a))&&(o+=5),e.isEasyApply&&(o+=5);let c=(e.seniority||"").toLowerCase();c.includes("senior")||c.includes("lead")||c.includes("staff")?o+=10:c.includes("mid")?o+=5:(c.includes("entry")||c.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 H(e=10,n={}){let{easyApplyOnly:o=!0}=n,t=Y(),s=ae(),r=new Set,i=new Set;for(let[m,p]of Object.entries(s))p.connectionStatus==="connected"&&r.add(m);let u={connectedRecruiters:r,conversationRecruiters:i},c=me(),f=new Set(["applied","skipped","expired","archived"]);return Object.entries(t).filter(([m,p])=>{if(typeof p!="object"||o&&!p.isEasyApply||p.applied)return!1;let y=c[m];return!(y&&f.has(y.status))}).map(([m,p])=>({jobId:m,score:co(p,u),job:p})).sort((m,p)=>{if(p.score!==m.score)return p.score-m.score;let y=m.job.scoutedAt||m.job.postedTime||"";return(p.job.scoutedAt||p.job.postedTime||"").localeCompare(y)}).slice(0,e)}function lo(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 w from"fs";import*as I from"path";import{fileURLToPath as ho}from"url";import"dotenv/config";import{createGraphClient as ao,resolveToken as uo}from"@ilivemylife/graph-sdk";import{config as fo}from"dotenv";fo();var ge=uo()||process.env.TOKEN;ge||(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 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})}**
5
+ `),process.exit(1));var ye=ao({token:ge});function po(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 mo({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?` ${po(s)}`:"",c=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 ${c} \u2014 ${i}${u}`}async function Se({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 c=`**${mo({scriptName:e,mode:n,status:o,error:s,duration:r})}**
6
6
 
7
7
  ${t.join(`
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(`
8
+ `)}`;try{await ye.addMessage(i,c)}catch(f){console.error(`[runReport] Failed to post end-of-session message: ${f.message}`)}}var go=ho(import.meta.url),yo=I.dirname(I.dirname(go)),K=process.env.DATA_DIR?I.resolve(process.env.DATA_DIR,"collected-profiles"):I.join(yo,"collected-profiles"),Q=I.join(K,"run-log.json");function So(e,n){let o=JSON.stringify(n,null,2),t=e+".tmp";w.writeFileSync(t,o);for(let s=0;s<3;s++)try{w.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 be(){try{if(!w.existsSync(Q))return{runs:[]};let e=JSON.parse(w.readFileSync(Q,"utf-8"));return!e.runs||!Array.isArray(e.runs)?{runs:[]}:e}catch{return{runs:[]}}}function ve(e){w.existsSync(K)||w.mkdirSync(K,{recursive:!0});try{So(Q,e)}catch(n){console.error(`[!] Failed to save run-log.json: ${n.message}`)}}function we(e,n="default"){let o=be();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),ve(o),{script:e,mode:n,startTime:t.getTime(),runIndex:o.runs.length-1}}function z(e,n,o=null,t=null){if(!e)return;let s=null;try{let i=be(),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 c=i.runs.find(f=>f.script===e.script&&f.status==="running"&&f.timestamp===new Date(e.startTime).toISOString());c&&(c.status=n,c.result=n,c.duration=Date.now()-e.startTime,c.stats=o,c.error=t,s=c.duration)}ve(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";Se({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 l from"fs";import*as se from"os";import*as d from"path";import{fileURLToPath as Fo}from"url";var ee={};G(ee,{NAME:()=>vo,VERSION:()=>bo,up:()=>ko});import*as $ from"fs";import*as B from"path";var bo=1,vo="normalize-jobs-and-questions",wo=["description","techStack","jobTags","seniority","employmentType","industries","jobFunction","postedTime","applicantCount","companySize","workplaceType","contractType","isEasyApply","applyType","companyUrl","scoutedAt","lastSeen"],$o=50;function X(e){return $.existsSync(e)?JSON.parse($.readFileSync(e,"utf-8")):null}function Z(e,n){let o=JSON.stringify(n,null,2),t=e+".tmp";try{$.writeFileSync(t,o)}catch(s){try{$.unlinkSync(t)}catch{}throw s}try{$.renameSync(t,e)}catch(s){try{$.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 ko(e,n){let o=B.join(e,"jobs.json"),t=B.join(n,"scouted-jobs.json"),s=B.join(e,"application-questions.json"),r=X(o)||{},i=X(t)||{},u=0,c=[];for(let a of Object.keys(r)){if(!i[a])continue;let m=i[a],p=r[a];for(let y of wo)m[y]!=null&&p[y]==null&&(p[y]=m[y]);c.push(a),u++}u>0?(Z(o,r),console.log(` Phase 1: Merged ${u} scouted jobs into jobs.json`)):console.log(" Phase 1: No overlapping jobs to merge");let f=X(s);if(f&&Array.isArray(f.applications)){let a={},m=0;for(let p of f.applications){let y=p.jobId||"unknown";a[y]||(a[y]={applications:[]});let W=(p.questions||[]).map(j=>{let M={type:j.type||"input",question:j.question||"",answer:j.answer||""};return j.options&&(j.options.length<=$o?M.options=j.options:(M.optionsOmitted=!0,m++)),j.page&&(M.page=j.page),j.wasPreFilled&&(M.wasPreFilled=!0),j.wasRetry&&(M.wasRetry=!0),M});a[y].applications.push({applicationId:p.applicationId,date:p.date,result:p.result||"submitted",formPages:p.formPages||null,duration:p.duration||null,retries:p.retries||0,failedField:p.failedField||null,questionCount:p.questionCount||W.length,questions:W})}Z(s,a),console.log(` Phase 2: Rekeyed ${f.applications.length} applications by jobId (${Object.keys(a).length} unique jobs)`),m>0&&console.log(` Phase 2: Stripped bloated options from ${m} 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(c.length>0){for(let a of c)delete i[a];Z(t,i),console.log(` Phase 3: Removed ${c.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 oe={};G(oe,{NAME:()=>jo,VERSION:()=>Ao,up:()=>Ro});import*as k from"fs";import*as $e from"path";var Ao=2,jo="reset-unread-flags";function Oo(e){return k.existsSync(e)?JSON.parse(k.readFileSync(e,"utf-8")):null}function Eo(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 Ro(e){let n=$e.join(e,"conversations.json"),o=Oo(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}Eo(n,o),console.log(` Reset unread=false on ${t} conversations. Run 'ilml linkedin sync-all' to re-establish real unread state from LinkedIn.`)}var ne={};G(ne,{NAME:()=>To,VERSION:()=>Io,up:()=>xo});import*as A from"fs";import*as ke from"path";var Io=3,To="recompute-last-message-by",Do=new Set(["draft","confirmed","rejected","needs_review"]);function No(e){return A.existsSync(e)?JSON.parse(A.readFileSync(e,"utf-8")):null}function Co(e,n){let o=JSON.stringify(n,null,2),t=e+".tmp";try{A.writeFileSync(t,o)}catch(s){try{A.unlinkSync(t)}catch{}throw s}try{A.renameSync(t,e)}catch(s){try{A.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}}function _o(e,n){if(!e||e.length===0)return"them";let o=e.filter(r=>!Do.has(r.status));if(o.length===0)return"them";let t=o.map(r=>({m:r,t:Date.parse(r.time)})).filter(r=>Number.isFinite(r.t)).sort((r,i)=>i.t-r.t).map(r=>r.m),s=t.length>0?t:[...o].reverse();for(let r of s){let i=r.sender;if(i===n)return"me";if(i!=="Unknown"&&i!=="")return"them"}return"them"}async function xo(e){let n=ke.join(e,"conversations.json"),o=No(n);if(!o){console.log(" conversations.json not found \u2014 nothing to recompute.");return}let t=process.env.LINKEDIN_NAME;if(!t||typeof t!="string"||!t.trim())throw new Error(`LINKEDIN_NAME env var is not set. Migration v003 needs your display name to determine which messages are yours vs theirs. Set LINKEDIN_NAME in your .env (or run "ilml plugin config linkedin set LINKEDIN_NAME 'Your Name'") and retry.`);let s=0,r=0,i=0;for(let u of Object.keys(o)){let c=o[u];if(!c||typeof c!="object")continue;Object.prototype.hasOwnProperty.call(c,"_cardSynced")&&(delete c._cardSynced,r++);let f=Array.isArray(c.messages)?c.messages:[],a=_o(f,t),m=a==="me"?"awaiting_reply":"unanswered_by_us";c.lastMessageBy!==a&&i++,c.lastMessageBy=a,c.conversationStatus=m,s++}if(s===0){console.log(" No conversations to recompute.");return}Co(n,o),console.log(` Recomputed lastMessageBy + conversationStatus on ${s} conversations.`),console.log(` ${i} had stale lastMessageBy (now corrected).`),r>0&&console.log(` Cleaned ${r} leftover _cardSynced flags.`),console.log(" Re-run 'ilml linkedin enrich' if you want actionNeeded / summary / autoTags to also update.")}var Lo=Fo(import.meta.url),Mo=d.dirname(Lo),Po=d.dirname(d.dirname(Mo));function Jo(e){return e&&(e==="~"?se.homedir():e.startsWith("~/")||e.startsWith("~\\")?d.join(se.homedir(),e.slice(2)):e)}var O=process.env.DATA_DIR?d.resolve(Jo(process.env.DATA_DIR)):Po,x=d.join(O,"collected-profiles"),F=d.join(O,"market-research"),S=d.join(O,".schema-version"),Ae=[d.join(x,".schema-version"),d.join(F,".schema-version")],T=3,R=[{version:1,module:ee},{version:2,module:oe},{version:3,module:ne}];(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!==T)throw new Error(`MIGRATION_REGISTRY: highest version v${o} but CURRENT_SCHEMA_VERSION=${T}. 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 Re(){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
- `),process.exit(1));try{c.mkdirSync(A,{recursive:!0})}catch(n){n.code!=="EEXIST"&&(console.error(`
11
- [FATAL] Cannot create DATA_DIR (${A}): ${n.message}`),console.error(` Fix the path or permissions and try again.
12
- `),process.exit(1))}let e;try{e=c.statSync(A)}catch(n){console.error(`
13
- [FATAL] DATA_DIR (${A}) is not accessible: ${n.message}
10
+ `),process.exit(1));try{l.mkdirSync(O,{recursive:!0})}catch(n){n.code!=="EEXIST"&&(console.error(`
11
+ [FATAL] Cannot create DATA_DIR (${O}): ${n.message}`),console.error(` Fix the path or permissions and try again.
12
+ `),process.exit(1))}let e;try{e=l.statSync(O)}catch(n){console.error(`
13
+ [FATAL] DATA_DIR (${O}) is not accessible: ${n.message}
14
14
  `),process.exit(1)}e.isDirectory()||(console.error(`
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[x,_]){let o;try{o=c.lstatSync(n)}catch(t){if(t.code==="ENOENT")continue;throw t}o.isSymbolicLink()&&(console.error(`
15
+ [FATAL] DATA_DIR (${O}) 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[x,F]){let o;try{o=l.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 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}
18
+ `),process.exit(1))}}var _=d.join(O,".migration-lock");function Uo(){let e=JSON.stringify({pid:process.pid,startedAt:new Date().toISOString()},null,2);try{return l.writeFileSync(_,e,{flag:"wx"}),!0}catch(t){if(t.code!=="EEXIST")throw t}let n;try{n=JSON.parse(l.readFileSync(_,"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: ${_}
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=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(`
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=_+".tmp";try{return l.writeFileSync(o,e),l.renameSync(o,_),!0}catch(t){try{l.unlinkSync(o)}catch{}throw new Error(`Failed to take over stale migration lock: ${t.message}`)}}function re(){try{if(JSON.parse(l.readFileSync(_,"utf-8")).pid!==process.pid)return;l.unlinkSync(_)}catch{}}var je=!1;function Bo(){if(je)return;je=!0;let e=()=>{try{re()}catch{}process.exit(1)};for(let n of["SIGINT","SIGTERM","SIGHUP"])try{process.on(n,e)}catch{}}function qo(){if(l.existsSync(S))return;let e=[],n=[];for(let r of Ae)if(l.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 $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(`
26
+ `),process.exit(1)}let t=e[0].schema,s=S+".tmp";try{l.writeFileSync(s,JSON.stringify(t,null,2)),l.renameSync(s,S)}catch(r){try{l.unlinkSync(s)}catch{}throw r}for(let r of Ae)if(l.existsSync(r))try{l.unlinkSync(r)}catch{}console.log(` Consolidated legacy .schema-version files \u2192 ${S}`)}function Vo(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=l.readFileSync(e,"utf-8");return n.charCodeAt(0)===65279&&(n=n.slice(1)),JSON.parse(n)}function Oe(){if(qo(),!l.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=xo(e);return n===null&&(console.error(`
28
+ `),process.exit(1)}let n=Vo(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 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}.
30
+ `),process.exit(1)),n}function Ie(e){try{l.chmodSync(e,384)}catch{}}function Wo(e){if(Re(),l.existsSync(S))try{if(l.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{l.writeFileSync(n,JSON.stringify(e,null,2)),Ie(n),l.renameSync(n,S)}catch(o){try{l.unlinkSync(n)}catch{}throw o}}function Ee(e,n){if(!l.existsSync(e))return;let o=`.pre-v${String(n).padStart(3,"0")}.backup`,t=l.readdirSync(e).filter(u=>{let c=u.toLowerCase();if(c.includes(".backup")||c===".schema-version"||c.endsWith(".tmp"))return!1;try{return l.statSync(d.join(e,u)).isFile()}catch{return!1}}),s=0,r=0,i=[];for(let u of t){let c=d.join(e,u),f=d.join(e,u+o);if(l.existsSync(f)){r++;continue}try{De(c,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 ${d.basename(e)}/: `+i.map(c=>`${c.file} (${c.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 ${d.basename(e)}/ (${o})${u}`)}}var te=!1;async function Te(){if(te)return;Re(),Qo();let n=Oe().version;if(n===T){te=!0;return}Bo(),Uo(),process.on("exit",re),n>T&&(console.error(`
31
+ [FATAL] Your data schema is at v${n}, 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}.
33
33
  `),process.exit(1)),console.log(`
34
- \u{1F4E6} Schema migration: v${n} \u2192 v${E}`);let o=Uo();if(!o.ok){console.error(`
34
+ \u{1F4E6} Schema migration: v${n} \u2192 v${T}`);let o=zo();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{je(x,s.version),je(_,s.version)}catch(l){console.error(`
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(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.
38
+ Running migration v${String(s.version).padStart(3,"0")}...`);let r=`v${String(s.version).padStart(3,"0")}`,i=`.pre-${r}.backup`;try{Ee(x,s.version),Ee(F,s.version)}catch(c){console.error(`
39
+ [FATAL] Pre-migration backup failed for ${r}: ${c.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(x,F)}catch(c){console.error(`
41
+ [FATAL] Migration ${r} failed: ${c.message}`),console.error(` Attempting auto-rollback from ${i} files...`);try{let f=Xo(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=ke();a.version=s.version,a.migratedAt=l,a.migrations=a.migrations||[],a.migrations.push(f),_o(a)}catch(l){console.error(`
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{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(`
44
+ `)}process.exit(1)}try{let c=new Date().toISOString(),f={version:s.version,name:u.NAME||r,appliedAt:c},a=Oe();a.version=s.version,a.migratedAt=c,a.migrations=a.migrations||[],a.migrations.push(f),Wo(a)}catch(c){console.error(`
45
+ [FATAL] Migration ${r} applied successfully but schema version file could NOT be updated: ${c.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{le()}catch{}try{pe()}catch{}te=!0,re(),console.log(`
47
+ \u2713 Schema is now at v${T}
48
+ `)}var Go=[/\.json\.tmp$/,/\.schema-version\.tmp$/,/\.backup\.tmp$/];function Yo(e){let n=e.toLowerCase();return Go.some(o=>o.test(n))}var Ho=6e4;function Ko(){let e=Date.now(),n=[];for(let o of[O,x,F]){if(!l.existsSync(o))continue;let t;try{t=l.readdirSync(o)}catch{continue}for(let s of t){if(!Yo(s))continue;let r=d.join(o,s);try{let i=l.statSync(r);if(!i.isFile()||e-i.mtimeMs<Ho)continue;n.push({dir:o,file:s,fullPath:r})}catch{}}}return n}function Qo({verbose:e=!1}={}){let n=Ko();if(n.length===0)return e&&console.log(" No orphan .tmp files found."),0;let o=0;for(let t of n)try{l.unlinkSync(t.fullPath),o++,e&&console.log(` Removed orphan: ${d.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 zo(){let e=[];for(let n of[x,F]){if(!l.existsSync(n))continue;let o=l.readdirSync(n).filter(t=>{let s=t.toLowerCase();return s.endsWith(".json")&&!s.includes(".backup")});for(let t of o){let s=d.join(n,t);try{q(s)}catch(r){e.push({dir:d.basename(n),file:t,err:r.message})}}}return e.length===0?{ok:!0}:{ok:!1,broken:e}}function Xo(e){let n=[],o=[];for(let r of[x,F]){if(!l.existsSync(r))continue;let i=l.readdirSync(r).filter(u=>u.endsWith(e));for(let u of i){let c=d.join(r,u),f=u.slice(0,-e.length),a=d.join(r,f);try{f.toLowerCase().endsWith(".json")?q(c):l.accessSync(c,l.constants.R_OK),n.push({backupPath:c,originalPath:a,fileName:u,dir:r})}catch(m){o.push(`${d.basename(r)}/${u}: ${m.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:c}of n)try{De(r,i),t++}catch(f){s.push(`${d.basename(c)}/${u}: ${f.message}`)}return s.length>0?{ok:!1,error:s.join("; "),restored:t}:{ok:!0,restored:t}}function De(e,n){let o=n+".tmp";try{l.copyFileSync(e,o),Ie(o)}catch(t){try{l.unlinkSync(o)}catch{}throw t}try{l.renameSync(o,n)}catch(t){try{l.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 on=Zo(import.meta.url),nn=L.dirname(on),ie=process.env.DATA_DIR?L.resolve(process.env.DATA_DIR,"collected-profiles"):L.join(nn,"collected-profiles");await Te();var ce=process.argv.slice(2),Ne=parseInt(ce.find(e=>e.startsWith("--max="))?.split("=")[1]||"50"),V=parseInt(ce.find(e=>e.startsWith("--min-score="))?.split("=")[1]||"50"),tn=ce.includes("--list");function sn(){let n=H(20,{easyApplyOnly:!0}).filter(o=>o.score>=V);if(console.log(`
49
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(`
50
+ `)}async function rn(){if(console.log(`
51
51
  LinkedIn Apply Queue
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
52
+ `),tn){sn();return}let n=H(Ne*3,{easyApplyOnly:!0}).filter(r=>r.score>=V).slice(0,Ne);if(n.length===0){console.log(` No jobs to apply. Run: npm run scout
53
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(`
54
+ `);for(let{score:r,job:i}of n)console.log(` [${r}] ${(i.title||"?").slice(0,40)} @ ${(i.company||"?").slice(0,20)}`);D.existsSync(ie)||D.mkdirSync(ie,{recursive:!0});let o=L.join(ie,"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}));D.writeFileSync(o,JSON.stringify(t,null,2)),console.log(`
55
+ Generated ${o} with ${t.length} jobs`);for(let{jobId:r}of n)de(r,{queued:!0,queuedAt:new Date().toISOString().slice(0,10)});let s=we("apply-queue",`${n.length} jobs`);console.log(`
56
56
  Launching run.mjs --job-urls...
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)});
57
+ `);try{en("node",["run.mjs",`--job-urls=${o}`],{stdio:"inherit",timeout:144e5}),z(s,"success",{applied:n.length})}catch(r){console.error(`
58
+ Apply queue error: ${r.message?.slice(0,80)}`),z(s,"error",{applied:n.length},r.message)}try{D.unlinkSync(o)}catch{}}rn().catch(e=>{console.error("Apply queue failed:",e),process.exit(1)});
package/dist/daily.mjs CHANGED
@@ -1,53 +1,53 @@
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(`
1
+ var Ce=Object.defineProperty;var W=(e,n)=>{for(var t in n)Ce(e,t,{get:n[t],enumerable:!0})};import{execFileSync as Yo}from"child_process";import"dotenv/config";import*as S from"fs";import*as R from"path";import{fileURLToPath as Pe}from"url";import"dotenv/config";import{createGraphClient as _e,resolveToken as xe}from"@ilivemylife/graph-sdk";import{config as Fe}from"dotenv";Fe();var te=xe()||process.env.TOKEN;te||(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 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})}**
5
+ `),process.exit(1));var se=_e({token:te});function Le(e){if(!e||e<1e3)return`${e||0}ms`;let n=Math.round(e/1e3);if(n<60)return`${n}s`;let t=Math.floor(n/60),o=n%60;return o?`${t}m ${o}s`:`${t}m`}function Me({scriptName:e,mode:n,status:t,error:o,duration:s}){let r=t==="error"?"\u2717":t==="quota-reached"?"\u26A0":"\u2713",i=new Date().toISOString().slice(0,16).replace("T"," "),a=s?` ${Le(s)}`:"",l=t==="error"?`error${o?`: ${String(o).slice(0,80)}`:""}`:t==="quota-reached"?"daily quota reached":"done",u=n&&n!=="default"?` (${n})`:"";return`${r} ${e}${u} \u2014 ${l} \u2014 ${i}${a}`}async function re({scriptName:e,mode:n,status:t,summaryLines:o,error:s,duration:r}){let i=process.env.NODE_RUN_REPORTS;if(!i||!Array.isArray(o)||o.length===0)return;let l=`**${Me({scriptName:e,mode:n,status:t,error:s,duration:r})}**
6
6
 
7
- ${n.join(`
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(`
7
+ ${o.join(`
8
+ `)}`;try{await se.addMessage(i,l)}catch(u){console.error(`[runReport] Failed to post end-of-session message: ${u.message}`)}}var Je=Pe(import.meta.url),Ue=R.dirname(R.dirname(Je)),q=process.env.DATA_DIR?R.resolve(process.env.DATA_DIR,"collected-profiles"):R.join(Ue,"collected-profiles"),Y=R.join(q,"run-log.json");function Be(e,n){let t=JSON.stringify(n,null,2),o=e+".tmp";S.writeFileSync(o,t);for(let s=0;s<3;s++)try{S.renameSync(o,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 ie(){try{if(!S.existsSync(Y))return{runs:[]};let e=JSON.parse(S.readFileSync(Y,"utf-8"));return!e.runs||!Array.isArray(e.runs)?{runs:[]}:e}catch{return{runs:[]}}}function ce(e){S.existsSync(q)||S.mkdirSync(q,{recursive:!0});try{Be(Y,e)}catch(n){console.error(`[!] Failed to save run-log.json: ${n.message}`)}}function le(e,n="default"){let t=ie();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 o=new Date,s={timestamp:o.toISOString(),script:e,mode:n,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),ce(t),{script:e,mode:n,startTime:o.getTime(),runIndex:t.runs.length-1}}function ae(e,n,t=null,o=null){if(!e)return;let s=null;try{let i=ie(),a=i.runs[e.runIndex];if(a&&a.status==="running"&&a.script===e.script)a.status=n,a.result=n,a.duration=Date.now()-e.startTime,a.stats=t,a.error=o,s=a.duration;else{let l=i.runs.find(u=>u.script===e.script&&u.status==="running"&&u.timestamp===new Date(e.startTime).toISOString());l&&(l.status=n,l.result=n,l.duration=Date.now()-e.startTime,l.stats=t,l.error=o,s=l.duration)}ce(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=n==="error"?"error":t&&t.dailyLimitHit?"quota-reached":"done";re({scriptName:e.script,mode:e.mode,status:i,summaryLines:r,error:o,duration:s??Date.now()-e.startTime}).catch(a=>console.error(`[runLog] postRunReport failed: ${a.message}`))}}import{getPluginConfig as Ve}from"@ilivemylife/graph-sdk";var We=await Ve();for(let[e,n]of Object.entries(We))process.env[e]===void 0&&(process.env[e]=n);var cn=process.env.LINKEDIN_NAME,N=Object.freeze({DRAFT:"draft",CONFIRMED:"confirmed",REJECTED:"rejected",NEEDS_REVIEW:"needs_review",SENT:"sent"}),ln=Object.freeze(new Set([N.DRAFT,N.CONFIRMED,N.NEEDS_REVIEW])),an=Object.freeze(new Set([N.DRAFT,N.CONFIRMED,N.REJECTED,N.NEEDS_REVIEW]));import"dotenv/config";import*as d from"fs";import*as m from"path";import{fileURLToPath as qe}from"url";var Ye=qe(import.meta.url),Ge=m.dirname(m.dirname(Ye)),E=process.env.DATA_DIR?m.resolve(process.env.DATA_DIR,"collected-profiles"):m.join(Ge,"collected-profiles"),mn=m.join(E,"people.json"),hn=m.join(E,"jobs.json"),He=m.join(E,"quota.json"),gn=m.join(E,"conversations.json"),yn=m.join(E,"sync-state.json"),Sn=m.join(E,"application-questions.json"),bn=m.join(E,"profile-history"),M=20,P=100,Ke=null,Qe=null,I=null,ze=null,Xe=null,Ze=null;function ue(){Ke=null,Qe=null,I=null,ze=null,Xe=null,Ze=null}function eo(){d.existsSync(E)||d.mkdirSync(E,{recursive:!0})}function oo(e,n){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(`[!] ${n} is corrupted: ${t.message}`),console.error(` File: ${m.resolve(e)}`);let o=e+".backup";if(d.existsSync(o))try{let s=JSON.parse(d.readFileSync(o,"utf-8"));console.error(` Restored from ${o}`);try{d.copyFileSync(o,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 fe(){return new Date().toISOString().split("T")[0]}function no(){return I||(eo(),I=oo(He,"quota.json"),(!I||!I.connections)&&(I={connections:[]}),I)}function pe(){let e=no(),n=new Date;n.setDate(n.getDate()-7);let t=n.toISOString().split("T")[0];return e.connections.filter(o=>o.date>=t)}function de(){let e=pe(),n=fe(),t=e.filter(l=>l.date===n).length,o=e.length,s=t>=M,r=o>=P,i=!s&&!r,a={allowed:i,daily:{sent:t,max:M},weekly:{sent:o,max:P}};return i||(a.reason=s?`Daily limit reached (${t}/${M})`:`Weekly limit reached (${o}/${P})`),a}function me(){let e=pe(),n=fe(),t=e.filter(s=>s.date===n).length,o=e.length;return{daily:{sent:t,max:M,remaining:M-t},weekly:{sent:o,max:P,remaining:P-o}}}import"dotenv/config";import*as c from"fs";import*as Z from"os";import*as f from"path";import{fileURLToPath as Io}from"url";var K={};W(K,{NAME:()=>so,VERSION:()=>to,up:()=>co});import*as b from"fs";import*as B from"path";var to=1,so="normalize-jobs-and-questions",ro=["description","techStack","jobTags","seniority","employmentType","industries","jobFunction","postedTime","applicantCount","companySize","workplaceType","contractType","isEasyApply","applyType","companyUrl","scoutedAt","lastSeen"],io=50;function G(e){return b.existsSync(e)?JSON.parse(b.readFileSync(e,"utf-8")):null}function H(e,n){let t=JSON.stringify(n,null,2),o=e+".tmp";try{b.writeFileSync(o,t)}catch(s){try{b.unlinkSync(o)}catch{}throw s}try{b.renameSync(o,e)}catch(s){try{b.unlinkSync(o)}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 co(e,n){let t=B.join(e,"jobs.json"),o=B.join(n,"scouted-jobs.json"),s=B.join(e,"application-questions.json"),r=G(t)||{},i=G(o)||{},a=0,l=[];for(let p of Object.keys(r)){if(!i[p])continue;let w=i[p],h=r[p];for(let O of ro)w[O]!=null&&h[O]==null&&(h[O]=w[O]);l.push(p),a++}a>0?(H(t,r),console.log(` Phase 1: Merged ${a} scouted jobs into jobs.json`)):console.log(" Phase 1: No overlapping jobs to merge");let u=G(s);if(u&&Array.isArray(u.applications)){let p={},w=0;for(let h of u.applications){let O=h.jobId||"unknown";p[O]||(p[O]={applications:[]});let ne=(h.questions||[]).map(k=>{let F={type:k.type||"input",question:k.question||"",answer:k.answer||""};return k.options&&(k.options.length<=io?F.options=k.options:(F.optionsOmitted=!0,w++)),k.page&&(F.page=k.page),k.wasPreFilled&&(F.wasPreFilled=!0),k.wasRetry&&(F.wasRetry=!0),F});p[O].applications.push({applicationId:h.applicationId,date:h.date,result:h.result||"submitted",formPages:h.formPages||null,duration:h.duration||null,retries:h.retries||0,failedField:h.failedField||null,questionCount:h.questionCount||ne.length,questions:ne})}H(s,p),console.log(` Phase 2: Rekeyed ${u.applications.length} applications by jobId (${Object.keys(p).length} unique jobs)`),w>0&&console.log(` Phase 2: Stripped bloated options from ${w} 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(l.length>0){for(let p of l)delete i[p];H(o,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 Q={};W(Q,{NAME:()=>ao,VERSION:()=>lo,up:()=>po});import*as v from"fs";import*as he from"path";var lo=2,ao="reset-unread-flags";function uo(e){return v.existsSync(e)?JSON.parse(v.readFileSync(e,"utf-8")):null}function fo(e,n){let t=JSON.stringify(n,null,2),o=e+".tmp";try{v.writeFileSync(o,t)}catch(s){try{v.unlinkSync(o)}catch{}throw s}try{v.renameSync(o,e)}catch(s){try{v.unlinkSync(o)}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 po(e){let n=he.join(e,"conversations.json"),t=uo(n);if(!t){console.log(" conversations.json not found \u2014 nothing to reset.");return}let o=0;for(let s of Object.keys(t))t[s].unread===!0&&(t[s].unread=!1,o++);if(o===0){console.log(" No stale unread flags found \u2014 nothing to do.");return}fo(n,t),console.log(` Reset unread=false on ${o} conversations. Run 'ilml linkedin sync-all' to re-establish real unread state from LinkedIn.`)}var z={};W(z,{NAME:()=>ho,VERSION:()=>mo,up:()=>vo});import*as $ from"fs";import*as ge from"path";var mo=3,ho="recompute-last-message-by",go=new Set(["draft","confirmed","rejected","needs_review"]);function yo(e){return $.existsSync(e)?JSON.parse($.readFileSync(e,"utf-8")):null}function So(e,n){let t=JSON.stringify(n,null,2),o=e+".tmp";try{$.writeFileSync(o,t)}catch(s){try{$.unlinkSync(o)}catch{}throw s}try{$.renameSync(o,e)}catch(s){try{$.unlinkSync(o)}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}}function bo(e,n){if(!e||e.length===0)return"them";let t=e.filter(r=>!go.has(r.status));if(t.length===0)return"them";let o=t.map(r=>({m:r,t:Date.parse(r.time)})).filter(r=>Number.isFinite(r.t)).sort((r,i)=>i.t-r.t).map(r=>r.m),s=o.length>0?o:[...t].reverse();for(let r of s){let i=r.sender;if(i===n)return"me";if(i!=="Unknown"&&i!=="")return"them"}return"them"}async function vo(e){let n=ge.join(e,"conversations.json"),t=yo(n);if(!t){console.log(" conversations.json not found \u2014 nothing to recompute.");return}let o=process.env.LINKEDIN_NAME;if(!o||typeof o!="string"||!o.trim())throw new Error(`LINKEDIN_NAME env var is not set. Migration v003 needs your display name to determine which messages are yours vs theirs. Set LINKEDIN_NAME in your .env (or run "ilml plugin config linkedin set LINKEDIN_NAME 'Your Name'") and retry.`);let s=0,r=0,i=0;for(let a of Object.keys(t)){let l=t[a];if(!l||typeof l!="object")continue;Object.prototype.hasOwnProperty.call(l,"_cardSynced")&&(delete l._cardSynced,r++);let u=Array.isArray(l.messages)?l.messages:[],p=bo(u,o),w=p==="me"?"awaiting_reply":"unanswered_by_us";l.lastMessageBy!==p&&i++,l.lastMessageBy=p,l.conversationStatus=w,s++}if(s===0){console.log(" No conversations to recompute.");return}So(n,t),console.log(` Recomputed lastMessageBy + conversationStatus on ${s} conversations.`),console.log(` ${i} had stale lastMessageBy (now corrected).`),r>0&&console.log(` Cleaned ${r} leftover _cardSynced flags.`),console.log(" Re-run 'ilml linkedin enrich' if you want actionNeeded / summary / autoTags to also update.")}import"dotenv/config";import*as y from"path";import{fileURLToPath as $o}from"url";var wo=$o(import.meta.url),ko=y.dirname(y.dirname(wo)),J=process.env.DATA_DIR?y.resolve(process.env.DATA_DIR,"market-research"):y.join(ko,"market-research"),kn=y.join(J,"scouted-jobs.json"),jn=y.join(J,"discovered-people.json"),En=y.join(J,"visit-log.json"),An=y.join(J,"companies.json"),On=y.join(J,"job-decisions.json");function ye(){jo=null,Eo=null,Ao=null,Oo=null,Ro=null}var jo=null;var Eo=null;var Ao=null;var Oo=null;var Ro=null;var To=Io(import.meta.url),Do=f.dirname(To),No=f.dirname(f.dirname(Do));function Co(e){return e&&(e==="~"?Z.homedir():e.startsWith("~/")||e.startsWith("~\\")?f.join(Z.homedir(),e.slice(2)):e)}var j=process.env.DATA_DIR?f.resolve(Co(process.env.DATA_DIR)):No,_=f.join(j,"collected-profiles"),x=f.join(j,"market-research"),g=f.join(j,".schema-version"),Se=[f.join(_,".schema-version"),f.join(x,".schema-version")],T=3,A=[{version:1,module:K},{version:2,module:Q},{version:3,module:z}];(function(){A.sort((o,s)=>o.version-s.version);let n=new Set;for(let o of A){if(!Number.isInteger(o.version)||o.version<1)throw new Error(`MIGRATION_REGISTRY: invalid version ${JSON.stringify(o.version)} (must be positive integer)`);if(n.has(o.version))throw new Error(`MIGRATION_REGISTRY: duplicate version v${o.version}`);n.add(o.version);let s=o.module;if(!s||typeof s.up!="function")throw new Error(`MIGRATION_REGISTRY: v${o.version} module is missing up()`);if(s.VERSION!==void 0&&s.VERSION!==o.version)throw new Error(`MIGRATION_REGISTRY: v${o.version} entry does not match module VERSION (${s.VERSION}). Drift between registry and module \u2014 pick one source of truth.`)}let t=A.length?A[A.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 o=0;o<A.length;o++){let s=o+1;if(A[o].version!==s)throw new Error(`MIGRATION_REGISTRY: gap detected \u2014 expected v${s} at position ${o} but found v${A[o].version}`)}})();function we(){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
- `),process.exit(1));try{c.mkdirSync(w,{recursive:!0})}catch(o){o.code!=="EEXIST"&&(console.error(`
11
- [FATAL] Cannot create DATA_DIR (${w}): ${o.message}`),console.error(` Fix the path or permissions and try again.
12
- `),process.exit(1))}let e;try{e=c.statSync(w)}catch(o){console.error(`
13
- [FATAL] DATA_DIR (${w}) is not accessible: ${o.message}
10
+ `),process.exit(1));try{c.mkdirSync(j,{recursive:!0})}catch(n){n.code!=="EEXIST"&&(console.error(`
11
+ [FATAL] Cannot create DATA_DIR (${j}): ${n.message}`),console.error(` Fix the path or permissions and try again.
12
+ `),process.exit(1))}let e;try{e=c.statSync(j)}catch(n){console.error(`
13
+ [FATAL] DATA_DIR (${j}) is not accessible: ${n.message}
14
14
  `),process.exit(1)}e.isDirectory()||(console.error(`
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[C,x]){let t;try{t=c.lstatSync(o)}catch(n){if(n.code==="ENOENT")continue;throw n}t.isSymbolicLink()&&(console.error(`
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 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}
15
+ [FATAL] DATA_DIR (${j}) 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[_,x]){let t;try{t=c.lstatSync(n)}catch(o){if(o.code==="ENOENT")continue;throw o}t.isSymbolicLink()&&(console.error(`
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=f.join(j,".migration-lock");function _o(){let e=JSON.stringify({pid:process.pid,startedAt:new Date().toISOString()},null,2);try{return c.writeFileSync(C,e,{flag:"wx"}),!0}catch(o){if(o.code!=="EEXIST")throw o}let n;try{n=JSON.parse(c.readFileSync(C,"utf-8"))}catch{n=null}if(n&&Number.isInteger(n.pid)){let o=!1;try{process.kill(n.pid,0),o=!0}catch(i){i.code==="EPERM"&&(o=!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)}o&&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}
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=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
- [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(`
21
+ `),process.exit(1)),o&&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 t=C+".tmp";try{return c.writeFileSync(t,e),c.renameSync(t,C),!0}catch(o){try{c.unlinkSync(t)}catch{}throw new Error(`Failed to take over stale migration lock: ${o.message}`)}}function ee(){try{if(JSON.parse(c.readFileSync(C,"utf-8")).pid!==process.pid)return;c.unlinkSync(C)}catch{}}var be=!1;function xo(){if(be)return;be=!0;let e=()=>{try{ee()}catch{}process.exit(1)};for(let n of["SIGINT","SIGTERM","SIGHUP"])try{process.on(n,e)}catch{}}function Fo(){if(c.existsSync(g))return;let e=[],n=[];for(let r of Se)if(c.existsSync(r))try{e.push({file:r,schema:V(r)})}catch(i){n.push({file:r,err:i.message})}if(n.length>0){console.error(`
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 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 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(`
26
+ `),process.exit(1)}let o=e[0].schema,s=g+".tmp";try{c.writeFileSync(s,JSON.stringify(o,null,2)),c.renameSync(s,g)}catch(r){try{c.unlinkSync(s)}catch{}throw r}for(let r of Se)if(c.existsSync(r))try{c.unlinkSync(r)}catch{}console.log(` Consolidated legacy .schema-version files \u2192 ${g}`)}function Lo(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 V(e){let n=c.readFileSync(e,"utf-8");return n.charCodeAt(0)===65279&&(n=n.slice(1)),JSON.parse(n)}function ve(){if(Fo(),!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=Oo(e);return o===null&&(console.error(`
28
+ `),process.exit(1)}let n=Lo(e);return n===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 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}.
30
+ `),process.exit(1)),n}function ke(e){try{c.chmodSync(e,384)}catch{}}function Mo(e){if(we(),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 n=g+".tmp";try{c.writeFileSync(n,JSON.stringify(e,null,2)),ke(n),c.renameSync(n,g)}catch(t){try{c.unlinkSync(n)}catch{}throw t}}function $e(e,n){if(!c.existsSync(e))return;let t=`.pre-v${String(n).padStart(3,"0")}.backup`,o=c.readdirSync(e).filter(a=>{let l=a.toLowerCase();if(l.includes(".backup")||l===".schema-version"||l.endsWith(".tmp"))return!1;try{return c.statSync(f.join(e,a)).isFile()}catch{return!1}}),s=0,r=0,i=[];for(let a of o){let l=f.join(e,a),u=f.join(e,a+t);if(c.existsSync(u)){r++;continue}try{Ee(l,u),s++}catch(p){i.push({file:a,err:p.message})}}if(i.length>0){let a=new Error(`Failed to back up ${i.length} file(s) in ${f.basename(e)}/: `+i.map(l=>`${l.file} (${l.err})`).join(", "));throw a.code="BACKUP_INCOMPLETE",a}if(s>0||r>0){let a=r>0?` (${r} pre-existing backup(s) preserved)`:"";console.log(` Backed up ${s} files in ${f.basename(e)}/ (${t})${a}`)}}var X=!1;async function je(){if(X)return;we(),Vo();let n=ve().version;if(n===T){X=!0;return}xo(),_o(),process.on("exit",ee),n>T&&(console.error(`
31
+ [FATAL] Your data schema is at v${n}, 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}.
33
33
  `),process.exit(1)),console.log(`
34
- \u{1F4E6} Schema migration: v${o} \u2192 v${I}`);let t=xo();if(!t.ok){console.error(`
34
+ \u{1F4E6} Schema migration: v${n} \u2192 v${T}`);let t=Wo();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
- `),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{Se(C,s.version),Se(x,s.version)}catch(a){console.error(`
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(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.
37
+ `),process.exit(1)}let o=A.filter(s=>s.version>n);for(let s of o){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{$e(_,s.version),$e(x,s.version)}catch(l){console.error(`
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 a=s.module;try{await a.up(_,x)}catch(l){console.error(`
41
+ [FATAL] Migration ${r} failed: ${l.message}`),console.error(` Attempting auto-rollback from ${i} files...`);try{let u=qo(i);u.ok?(console.error(` \u2713 Auto-rollback restored ${u.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: ${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=ye();p.version=s.version,p.migratedAt=a,p.migrations=p.migrations||[],p.migrations.push(u),Eo(p)}catch(a){console.error(`
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{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
- \u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\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);ce(t,s?"partial":"success",n)}Lo().catch(e=>{console.error("Daily pipeline failed:",e),process.exit(1)});
44
+ `)}process.exit(1)}try{let l=new Date().toISOString(),u={version:s.version,name:a.NAME||r,appliedAt:l},p=ve();p.version=s.version,p.migratedAt=l,p.migrations=p.migrations||[],p.migrations.push(u),Mo(p)}catch(l){console.error(`
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{ue()}catch{}try{ye()}catch{}X=!0,ee(),console.log(`
47
+ \u2713 Schema is now at v${T}
48
+ `)}var Po=[/\.json\.tmp$/,/\.schema-version\.tmp$/,/\.backup\.tmp$/];function Jo(e){let n=e.toLowerCase();return Po.some(t=>t.test(n))}var Uo=6e4;function Bo(){let e=Date.now(),n=[];for(let t of[j,_,x]){if(!c.existsSync(t))continue;let o;try{o=c.readdirSync(t)}catch{continue}for(let s of o){if(!Jo(s))continue;let r=f.join(t,s);try{let i=c.statSync(r);if(!i.isFile()||e-i.mtimeMs<Uo)continue;n.push({dir:t,file:s,fullPath:r})}catch{}}}return n}function Vo({verbose:e=!1}={}){let n=Bo();if(n.length===0)return e&&console.log(" No orphan .tmp files found."),0;let t=0;for(let o of n)try{c.unlinkSync(o.fullPath),t++,e&&console.log(` Removed orphan: ${f.basename(o.dir)}/${o.file}`)}catch(s){console.warn(` [!] Could not remove orphan ${o.fullPath}: ${s.message}`)}return!e&&t>0&&console.log(` Cleaned up ${t} orphan .tmp file(s) from previous run.`),t}function Wo(){let e=[];for(let n of[_,x]){if(!c.existsSync(n))continue;let t=c.readdirSync(n).filter(o=>{let s=o.toLowerCase();return s.endsWith(".json")&&!s.includes(".backup")});for(let o of t){let s=f.join(n,o);try{V(s)}catch(r){e.push({dir:f.basename(n),file:o,err:r.message})}}}return e.length===0?{ok:!0}:{ok:!1,broken:e}}function qo(e){let n=[],t=[];for(let r of[_,x]){if(!c.existsSync(r))continue;let i=c.readdirSync(r).filter(a=>a.endsWith(e));for(let a of i){let l=f.join(r,a),u=a.slice(0,-e.length),p=f.join(r,u);try{u.toLowerCase().endsWith(".json")?V(l):c.accessSync(l,c.constants.R_OK),n.push({backupPath:l,originalPath:p,fileName:a,dir:r})}catch(w){t.push(`${f.basename(r)}/${a}: ${w.message}`)}}}if(t.length>0)return{ok:!1,error:t.join("; "),restored:0};let o=0,s=[];for(let{backupPath:r,originalPath:i,fileName:a,dir:l}of n)try{Ee(r,i),o++}catch(u){s.push(`${f.basename(l)}/${a}: ${u.message}`)}return s.length>0?{ok:!1,error:s.join("; "),restored:o}:{ok:!0,restored:o}}function Ee(e,n){let t=n+".tmp";try{c.copyFileSync(e,t),ke(t)}catch(o){try{c.unlinkSync(t)}catch{}throw o}try{c.renameSync(t,n)}catch(o){try{c.unlinkSync(t)}catch{}if(o.code==="EPERM"||o.code==="EBUSY"){let s=new Error(`File locked: ${n} (${o.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=o.code,s}throw o}}await je();var U=process.argv.slice(2),oe=new Set((U.find(e=>e.startsWith("--skip="))?.split("=")[1]||"").split(",").filter(Boolean)),Ae=new Set((U.find(e=>e.startsWith("--only="))?.split("=")[1]||"").split(",").filter(Boolean)),Ie=U.includes("--dry-run"),Te=U.includes("--jobs"),De=U.includes("--comms"),Oe=["apply-queue","apply","funnel","scout","visit"],Re=["sync","today"],Ne=Te?Oe:De?Re:[...Oe,...Re];function D(e){return Ae.size>0?Ae.has(e):!oe.has(e)&&Ne.includes(e)}function L(e,n,t=[],{optional:o=!1}={}){if(console.log(`
49
+ ${"\u2550".repeat(50)}`),console.log(` STEP: ${e}`),console.log("\u2550".repeat(50)),Ie)return console.log(` [DRY RUN] Would run: node ${n} ${t.join(" ")}`),!0;try{return Yo("node",[n,...t],{stdio:"inherit",timeout:18e6}),console.log(` \u2713 ${e} completed`),!0}catch(s){return o?(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 Go(){let e=Te?"jobs":De?"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 n=Ne.filter(D);console.log(` Steps: ${n.join(" \u2192 ")}`),oe.size>0&&console.log(` Skipping: ${[...oe].join(", ")}`),Ie&&console.log(" [DRY RUN MODE]");let t=le("daily",e),o={};if(D("apply-queue")&&(o["apply-queue"]=L("Apply Queue (scored jobs)","applyQueue.mjs",["--max=50","--min-score=50"],{optional:!0})),D("apply")){let r=me();o.apply=L("Apply from Search","run.mjs",[],{optional:!0})}if(D("funnel")&&!D("apply")){let r=de();r.allowed?o.funnel=L("Connection Funnel","funnelRunner.mjs",["--max=20"],{optional:!0}):(console.log(`
51
+ \u26A0 Funnel skipped: ${r.reason}`),o.funnel="skipped_quota")}D("scout")&&(o.scout=L("Scout Jobs for Tomorrow","scout.mjs",["--max=300","--verbose"],{optional:!0})),D("sync")&&(o.sync=L("Sync Inbox","syncAll.mjs")),D("today")&&(o.today=L("Daily Report","syncAll.mjs",["--today"])),console.log(`
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(o))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(o).some(r=>r===!1);ae(t,s?"partial":"success",o)}Go().catch(e=>{console.error("Daily pipeline failed:",e),process.exit(1)});