toolnet-memory 0.3.15 → 0.3.17

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.
Files changed (48) hide show
  1. package/.env.example +74 -0
  2. package/.release-target +1 -1
  3. package/README.md +316 -6
  4. package/bin/toolnet-memory +12 -0
  5. package/bundle/agy-hook.js +66 -66
  6. package/bundle/agy.js +56 -56
  7. package/bundle/api.js +15 -15
  8. package/bundle/audit.js +2 -0
  9. package/bundle/background-refresh.js +7 -7
  10. package/bundle/claude-hook.js +49 -49
  11. package/bundle/code-capabilities.js +2 -0
  12. package/bundle/codex-context.js +29 -29
  13. package/bundle/codex.js +71 -71
  14. package/bundle/context-runtime.js +39 -39
  15. package/bundle/context.js +15 -15
  16. package/bundle/copilot-hook.js +78 -78
  17. package/bundle/cursor-hook.js +78 -78
  18. package/bundle/docker-healthcheck.js +3 -0
  19. package/bundle/doctor.js +4 -4
  20. package/bundle/full-index.js +22 -21
  21. package/bundle/gc.js +5 -3
  22. package/bundle/graph-cli.js +1 -1
  23. package/bundle/graph-index.js +5 -4
  24. package/bundle/graph-ui.js +4 -3
  25. package/bundle/grok-hook.js +56 -56
  26. package/bundle/handoff-refresh.js +10 -10
  27. package/bundle/help-cli.js +4 -4
  28. package/bundle/impact.js +5 -4
  29. package/bundle/incremental.js +5 -5
  30. package/bundle/init.js +36 -35
  31. package/bundle/kiro-hook.js +85 -85
  32. package/bundle/learner.js +8 -6
  33. package/bundle/mcp.js +19 -17
  34. package/bundle/memory-agent.js +6 -6
  35. package/bundle/memory-query.js +7 -7
  36. package/bundle/opencode.js +77 -77
  37. package/bundle/project-manual.js +7 -7
  38. package/bundle/public/index.html +51 -2
  39. package/bundle/recovery-certify.js +9 -9
  40. package/bundle/runtime.js +16 -13
  41. package/bundle/semantic.js +6 -5
  42. package/bundle/service.js +11 -6
  43. package/bundle/snapshot.js +6 -4
  44. package/bundle/status-cli.js +4 -4
  45. package/bundle/toolnet-cli.js +46 -46
  46. package/bundle/work-continuity.js +7 -7
  47. package/package.json +38 -2
  48. package/release-manifest.json +143 -4
@@ -1,24 +1,25 @@
1
- import{closeSync as Ms,mkdirSync as As,openSync as Is,rmSync as Ns,writeFileSync as js}from"node:fs";import{join as We}from"node:path";import{existsSync as kt,readFileSync as Rt}from"node:fs";import{homedir as Tt}from"node:os";import{join as Mt}from"node:path";function At(s){let e=s.trim();return e.length>=2&&e.startsWith('"')&&e.endsWith('"')?(e=e.slice(1,-1),e.replace(/\\n/g,`
2
- `).replace(/\\r/g,"\r").replace(/\\t/g," ").replace(/\\"/g,'"').replace(/\\\\/g,"\\")):e.length>=2&&e.startsWith("'")&&e.endsWith("'")?e.slice(1,-1):e}function It(){let s=process.env.TOOLNET_GLOBAL_ENV??Mt(Tt(),".config","toolnet-memory",".env");if(!kt(s))return;let e=Rt(s,"utf8");for(let t of e.split(/\r?\n/)){let r=t.trim();if(!r||r.startsWith("#"))continue;r.startsWith("export ")&&(r=r.slice(7));let n=r.indexOf("=");if(n<=0)continue;let i=r.slice(0,n).trim();/^[A-Za-z_][A-Za-z0-9_]*$/.test(i)&&process.env[i]===void 0&&(process.env[i]=At(r.slice(n+1)))}}It();function G(s,e){return s===void 0?e:["1","true","yes","on"].includes(s.toLowerCase())}function z(s,e){if(!s)return e;let t=Number(s);return Number.isFinite(t)?t:e}function Ve(){return{memory:{autoCapture:G(process.env.MEMORY_AUTO_CAPTURE,!0),autoRetrieve:G(process.env.MEMORY_AUTO_RETRIEVE,!0),autoSummarize:G(process.env.MEMORY_AUTO_SUMMARIZE,!0),autoSync:G(process.env.MEMORY_AUTO_SYNC,!0)},retrieval:{maxCandidates:z(process.env.MEMORY_MAX_CANDIDATES,50),rerankTop:z(process.env.MEMORY_RERANK_TOP,10),finalContext:z(process.env.MEMORY_FINAL_CONTEXT,5),tokenBudget:z(process.env.MEMORY_TOKEN_BUDGET,2e3)},storage:{provider:process.env.MEMORY_STORAGE_PROVIDER??"huggingface",r2:{accountId:process.env.R2_ACCOUNT_ID,bucket:process.env.R2_BUCKET,accessKeyId:process.env.R2_ACCESS_KEY_ID,secretAccessKey:process.env.R2_SECRET_ACCESS_KEY},s3:{endpoint:process.env.S3_ENDPOINT,region:process.env.S3_REGION,bucket:process.env.S3_BUCKET,accessKeyId:process.env.S3_ACCESS_KEY_ID,secretAccessKey:process.env.S3_SECRET_ACCESS_KEY,forcePathStyle:G(process.env.S3_FORCE_PATH_STYLE,!1)},huggingface:{namespace:process.env.HF_NAMESPACE,bucket:process.env.HF_BUCKET,accessKeyId:process.env.HF_S3_ACCESS_KEY_ID,secretAccessKey:process.env.HF_S3_SECRET_ACCESS_KEY},localRoot:process.env.MEMORY_LOCAL_STORAGE_PATH},cache:{maxMb:z(process.env.MEMORY_LOCAL_CACHE_MB,200)}}}import{createHash as Nt}from"node:crypto";import{existsSync as Q,mkdirSync as jt,readFileSync as Lt,renameSync as Ot,writeFileSync as _t}from"node:fs";import{basename as Ft,dirname as X,join as K,parse as Qe,resolve as j}from"node:path";var Xe=".toolnet",$t="project.json";function Dt(s){return Nt("sha256").update(s).digest("hex").slice(0,16)}function ee(s){return K(s,Xe,$t)}function Gt(s){return Q(ee(s))}function Ye(s,e){let t=j(s),r=Qe(t).root;for(;;){if(Gt(t))return t;if(t===r||e&&t===j(e))break;let n=X(t);if(n===t)break;t=n}return null}function Je(s){let e=j(s),t=Qe(e).root,r=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"];for(;;){if(r.some(i=>Q(K(e,i))))return e;if(e===t)break;let n=X(e);if(n===e)break;e=n}return j(s)}function qe(s){let e;try{e=JSON.parse(Lt(s,"utf8"))}catch(n){throw new Error(`Invalid ToolNet project manifest: ${s}: ${n instanceof Error?n.message:String(n)}`)}if(!e||typeof e!="object")throw new Error(`Invalid ToolNet project manifest: ${s}`);let t=e;if(typeof t.id!="string"||!t.id.trim())throw new Error(`ToolNet project manifest is missing id: ${s}`);if(typeof t.name!="string"||!t.name.trim())throw new Error(`ToolNet project manifest is missing name: ${s}`);let r=new Date().toISOString();return{version:1,id:t.id,name:t.name,remote:typeof t.remote=="string"&&t.remote.trim()?t.remote:t.name,rootPath:typeof t.rootPath=="string"?t.rootPath:X(X(s)),createdAt:typeof t.createdAt=="string"?t.createdAt:r,updatedAt:typeof t.updatedAt=="string"?t.updatedAt:r,graphVersion:typeof t.graphVersion=="number"?t.graphVersion:0,memoryVersion:typeof t.memoryVersion=="number"?t.memoryVersion:0,metadata:t.metadata&&typeof t.metadata=="object"?t.metadata:void 0}}function Ze(s,e){let t=K(s,Xe);jt(t,{recursive:!0});let r=ee(s),n=`${r}.tmp-${process.pid}`;_t(n,JSON.stringify(e,null,2)+`
3
- `,{encoding:"utf8",mode:384}),Ot(n,r)}function je(s,e){return{id:s.id,name:s.name,remote:s.remote,rootPath:e,createdAt:s.createdAt,updatedAt:s.updatedAt,graphVersion:s.graphVersion,memoryVersion:s.memoryVersion,metadata:s.metadata}}var te=class{findExisting(e=process.cwd()){let t=j(e),r=Je(t),i=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(u=>Q(K(r,u))),o=Ye(t,i?r:void 0);if(!o)return null;let a=qe(ee(o));return je(a,o)}requireExisting(e=process.cwd()){let t=this.findExisting(e);if(!t)throw new Error("PROJECT_NOT_INITIALIZED");return t}detect(e=process.cwd()){let t=j(e),r=Je(t),i=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(l=>Q(K(r,l))),o=Ye(t,i?r:void 0);if(o){let l=ee(o),f=qe(l);return f.rootPath!==o&&(f.rootPath=o,f.updatedAt=new Date().toISOString(),Ze(o,f)),je(f,o)}let a=new Date().toISOString(),u=Ft(r),c={version:1,id:Dt(r),name:u,remote:u,rootPath:r,createdAt:a,updatedAt:a,graphVersion:0,memoryVersion:0};return Ze(r,c),je(c,r)}};var zt=[{type:"openai_key",regex:/\bsk-[A-Za-z0-9_-]{20,}\b/g,confidence:"exact"},{type:"huggingface_token",regex:/\bhf_[A-Za-z0-9]{20,}\b/g,confidence:"exact"},{type:"hf_s3_access_key",regex:/\bHFAK[A-Za-z0-9]{8,}\b/g,confidence:"exact"},{type:"aws_access_key",regex:/\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/g,confidence:"exact"},{type:"github_token",regex:/\b(?:gh[pousr]_[A-Za-z0-9]{30,255}|github_pat_[A-Za-z0-9_]{40,255})\b/g,confidence:"exact"},{type:"stripe_secret_key",regex:/\b(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{16,}\b/g,confidence:"exact"},{type:"google_api_key",regex:/\bAIza[A-Za-z0-9_-]{30,}\b/g,confidence:"exact"},{type:"slack_token",regex:/\bxox[baprs]-[A-Za-z0-9-]{16,}\b/g,confidence:"exact"},{type:"npm_token",regex:/\bnpm_[A-Za-z0-9]{20,}\b/g,confidence:"exact"},{type:"bearer_token",regex:/\bBearer\s+[A-Za-z0-9._~+/=-]{16,}\b/gi,confidence:"high"},{type:"jwt",regex:/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/g,confidence:"exact"},{type:"private_key",regex:/-----BEGIN (?:RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----[\s\S]*?-----END (?:RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----/g,confidence:"exact"},{type:"password_assignment",regex:/\b(?:password|passwd|pwd)\s*[:=]\s*["']?[^"' \t\r\n]{6,}["']?/gi,confidence:"high"},{type:"secret_assignment",regex:/\b(?:secret|api[_-]?key|access[_-]?token|refresh[_-]?token|client[_-]?secret|private[_-]?key)\s*[:=]\s*["']?[^"' \t\r\n]{8,}["']?/gi,confidence:"high"},{type:"cookie",regex:/\b(?:cookie|set-cookie)\s*[:=]\s*[^;\n]{8,}/gi,confidence:"high"},{type:"url_credentials",regex:/\bhttps?:\/\/[^:/@\s]+:[^/@\s]{4,}@[^/\s]+/gi,confidence:"high"}],Kt=new Set(["example","example-key","example-token","changeme","change-me","password","secret","your-api-key","your-token","<token>","<secret>","<password>","[redacted]"]);function et(s){return s.normalize("NFKC").trim().toLowerCase()}function Bt(s){if(s.length===0)return 0;let e=new Map;for(let r of s)e.set(r,(e.get(r)??0)+1);let t=0;for(let r of e.values()){let n=r/s.length;t-=n*Math.log2(n)}return t}function Ut(s){return/^[a-f0-9]{32}$/iu.test(s)||/^[a-f0-9]{40}$/iu.test(s)||/^[a-f0-9]{64}$/iu.test(s)}function Wt(s,e,t){let r=s.slice(Math.max(0,e-48),e),n=s.slice(t,Math.min(s.length,t+16));return/\b(?:token|secret|key|credential|authorization|password|passwd|apikey|api_key|access[_-]?key)\b/iu.test(`${r} ${n}`)}function Ht(s,e){return s.start<e.end&&e.start<s.end}function tt(s){return s.sort((e,t)=>e.start!==t.start?e.start-t.start:t.end-t.start-(e.end-e.start))}var re=class{allowValues=new Set;enableEntropyHeuristic;constructor(e={}){for(let t of e.allowValues??[]){let r=et(t);r&&this.allowValues.add(r)}this.enableEntropyHeuristic=e.enableEntropyHeuristic??!0}scan(e){let t=[];for(let i of zt){let o=new RegExp(i.regex.source,i.regex.flags);for(let a of e.matchAll(o))a.index===void 0||!a[0]||this.allowed(a[0])||t.push({type:i.type,value:a[0],start:a.index,end:a.index+a[0].length,confidence:i.confidence})}this.enableEntropyHeuristic&&t.push(...this.entropyMatches(e));let r=tt(t),n=[];for(let i of r)n.some(o=>Ht(o,i))||n.push(i);return tt(n)}hasSecrets(e){return this.scan(e).length>0}allowed(e){let t=et(e);return Kt.has(t)?!0:this.allowValues.has(t)}entropyMatches(e){let t=[],r=/[A-Za-z0-9_+/=-]{32,160}/g;for(let n of e.matchAll(r)){if(n.index===void 0||!n[0])continue;let i=n[0];this.allowed(i)||Ut(i)||!/[A-Za-z]/u.test(i)||!/[0-9]/u.test(i)||Wt(e,n.index,n.index+i.length)&&(Bt(i)<3.7||t.push({type:"high_entropy_secret",value:i,start:n.index,end:n.index+i.length,confidence:"heuristic"}))}return t}};var L=class{scanner;constructor(e={}){this.scanner=new re(e)}sanitize(e){let t=this.scanner.scan(e);if(t.length===0)return{text:e,redacted:0,secretTypes:[]};let r=e,n=[...t].sort((o,a)=>a.start-o.start),i=new Set;for(let o of n)i.add(o.type),r=r.slice(0,o.start)+`[REDACTED:${o.type}]`+r.slice(o.end);return{text:r,redacted:t.length,secretTypes:[...i].sort()}}sanitizeValue(e){if(typeof e=="string")return this.sanitize(e).text;if(Array.isArray(e))return e.map(t=>this.sanitizeValue(t));if(e&&typeof e=="object"){let t={};for(let[r,n]of Object.entries(e)){let i=r.normalize("NFKC").toLowerCase().replace(/[^a-z0-9]+/g,"");if(i.includes("password")||i.includes("passwd")||i==="pwd"||i.includes("secret")||i.includes("token")||i.includes("cookie")||i.includes("authorization")||i.includes("apikey")||i.includes("accesskey")||i.includes("privatekey")||i.includes("clientsecret")||i.includes("credential")){t[r]="[REDACTED]";continue}t[r]=this.sanitizeValue(n)}return t}return e}};var Zs=new L;import{lstat as qt,readdir as Zt}from"node:fs/promises";import{extname as Qt,join as Xt,relative as er,resolve as tr}from"node:path";import{basename as Vt}from"node:path";var Yt=new Set([".env",".env.local",".env.production",".env.development","id_rsa","id_ed25519","credentials","credentials.json","service-account.json"]),Jt=new Set([".pem",".key",".p12",".pfx"]);function rt(s){let e=Vt(s).toLowerCase();if(Yt.has(e))return!0;for(let t of Jt)if(e.endsWith(t))return!0;return!1}var rr=2*1024*1024,sr=new Set([".ts",".tsx",".js",".jsx",".mjs",".cjs",".mts",".cts"]),nr=new Set(["node_modules",".git",".toolnet",".toolnet-memory","dist","build","out","coverage",".next",".nuxt",".cache",".parcel-cache",".turbo","vendor","target"]);function st(s,e){if(!Number.isSafeInteger(s)||s<1)throw new Error(`${e} must be a positive integer`);return s}function nt(s){if(s?.aborted)throw new Error("Repository scan aborted")}function or(s){let e=s.trim().toLowerCase();if(!e)throw new Error("Repository scan extension cannot be empty");return e.startsWith(".")?e:`.${e}`}function ir(s){let e=s.toLowerCase();return/\.min\.(?:js|mjs|cjs)$/u.test(e)||/\.bundle\.(?:js|mjs|cjs)$/u.test(e)}function ar(s){return s.replaceAll("\\","/")}async function ot(s,e={}){let t=tr(s),r=e.maxFileBytes??rr;if(!Number.isSafeInteger(r)||r<1)throw new Error("maxFileBytes must be a positive integer");let n=e.maxFiles??Number.MAX_SAFE_INTEGER;st(n,"maxFiles");let i=e.progressInterval??1e3;st(i,"progressInterval");let o=new Set(e.extensions?[...e.extensions].map(or):sr),a=new Set(nr);for(let f of e.extraIgnoredDirectories??[]){let d=f.trim();d&&a.add(d)}let u=[],c={directories:0,entries:0,accepted:0,skippedIgnoredDirectories:0,skippedSymlinks:0,skippedGenerated:0,skippedSensitive:0,skippedOversized:0,skippedUnreadable:0},l=[t];for(;l.length>0;){nt(e.signal);let f=l.pop(),d;try{d=await Zt(f,{withFileTypes:!0})}catch{c.skippedUnreadable+=1;continue}c.directories+=1;for(let m of d){nt(e.signal),c.entries+=1,c.entries%i===0&&e.onProgress?.({directories:c.directories,entries:c.entries,accepted:c.accepted});let g=Xt(f,m.name);if(m.isSymbolicLink()){c.skippedSymlinks+=1;continue}if(m.isDirectory()){if(a.has(m.name)){c.skippedIgnoredDirectories+=1;continue}l.push(g);continue}if(!m.isFile())continue;let h=Qt(m.name).toLowerCase();if(!o.has(h))continue;if(ir(m.name)){c.skippedGenerated+=1;continue}if(rt(g)){c.skippedSensitive+=1;continue}let y;try{y=await qt(g)}catch{c.skippedUnreadable+=1;continue}if(y.isSymbolicLink()){c.skippedSymlinks+=1;continue}if(y.isFile()){if(y.size>r){c.skippedOversized+=1;continue}if(u.length>=n)throw new Error(`Repository scan file limit exceeded: ${n}`);u.push(ar(er(t,g))),c.accepted+=1}}}return u.sort(),e.onProgress?.({directories:c.directories,entries:c.entries,accepted:c.accepted}),{files:u,stats:c}}import{createHash as cr}from"node:crypto";import{dirname as lr,extname as dr,normalize as ur,posix as pr}from"node:path";var se=class{symbols=new Map;edges=new Map;addSymbol(e){this.symbols.set(e.id,e)}addEdge(e){this.edges.set(e.id,e)}getSymbol(e){return this.symbols.get(e)}findByName(e,t){return[...this.symbols.values()].filter(r=>r.projectId===e&&(r.name===t||r.qualifiedName===t))}allSymbols(e){return[...this.symbols.values()].filter(t=>t.projectId===e)}allEdges(e){return[...this.edges.values()].filter(t=>t.projectId===e)}import(e,t){for(let r of e)this.addSymbol(r);for(let r of t)this.addEdge(r)}clearProject(e){for(let[t,r]of this.symbols)r.projectId===e&&this.symbols.delete(t);for(let[t,r]of this.edges)r.projectId===e&&this.edges.delete(t)}};function mr(s,e,t,r){return cr("sha256").update(`${s}:${e}:${t}:${r}`).digest("hex").slice(0,24)}function R(s){return ur(s).replaceAll("\\","/")}function fr(s,e,t){if(!e.startsWith("."))return;let r=R(pr.join(lr(s),e)),n=dr(r),i;if(n===".js"){let o=r.slice(0,-3);i=[`${o}.ts`,`${o}.tsx`,`${o}.js`,`${o}.jsx`]}else if(n===".mjs"){let o=r.slice(0,-4);i=[`${o}.mts`,`${o}.ts`,`${o}.mjs`]}else if(n===".cjs"){let o=r.slice(0,-4);i=[`${o}.cts`,`${o}.ts`,`${o}.cjs`]}else n?i=[r]:i=[`${r}.ts`,`${r}.tsx`,`${r}.mts`,`${r}.cts`,`${r}.js`,`${r}.jsx`,`${r}.mjs`,`${r}.cjs`,`${r}/index.ts`,`${r}/index.tsx`,`${r}/index.mts`,`${r}/index.cts`,`${r}/index.js`,`${r}/index.jsx`,`${r}/index.mjs`,`${r}/index.cjs`];return i.find(o=>t.has(R(o)))}function Le(s,e,t,r){let n=r?.(s,e,t);if(n){let i=R(n);if(t.has(i))return i}return fr(s,e,t)}var B=class{build(e,t,r={}){let n=new se;for(let a of r.seedSymbols??[])n.addSymbol(a);for(let a of t)for(let u of a.symbols)n.addSymbol(u);let i=new Map;for(let a of n.allSymbols(e))a.type==="file"&&i.set(R(a.filePath),a);let o=new Set(i.keys());for(let a of t){let u=R(a.filePath),c=i.get(u);if(c){for(let l of a.symbols)l.id!==c.id&&n.addEdge(this.edge(e,c.id,"DEFINES",l.id));for(let l of a.imports){let f=Le(u,l.source,o,r.resolveModule);if(!f)continue;let d=i.get(f);d&&n.addEdge(this.edge(e,c.id,"IMPORTS",d.id,{source:l.source,resolvedFile:f}))}for(let l of a.heritage){let f=n.findByName(e,l.targetName).filter(d=>d.type==="class"||d.type==="interface");for(let d of f)n.addEdge(this.edge(e,l.fromId,l.type,d.id))}for(let l of a.calls){if(!l.callerId)continue;let f=this.resolveCallTargets(e,a,l.calleeName,l.qualifier,n,o,r.resolveModule);for(let d of f)d.id!==l.callerId&&n.addEdge(this.edge(e,l.callerId,"CALLS",d.id,{line:l.line,qualifier:l.qualifier}))}}}return n}resolveCallTargets(e,t,r,n,i,o,a){let u=R(t.filePath),c=i.findByName(e,r).filter(l=>R(l.filePath)===u);if(!n&&c.length)return c;if(n)for(let l of t.imports){if(!l.bindings.find(m=>m.kind==="namespace"&&m.localName===n))continue;let d=Le(u,l.source,o,a);if(d)return i.findByName(e,r).filter(m=>R(m.filePath)===d)}for(let l of t.imports){let f=l.bindings.find(h=>h.localName===r);if(!f)continue;let d=Le(u,l.source,o,a);if(!d)continue;let m=f.importedName==="default"?r:f.importedName,g=i.findByName(e,m).filter(h=>R(h.filePath)===d);if(g.length)return g}return i.findByName(e,r).filter(l=>l.type==="function"||l.type==="method")}edge(e,t,r,n,i){return{id:mr(e,t,r,n),projectId:e,from:t,to:n,type:r,metadata:i}}};import{readFile as gr}from"node:fs/promises";import{join as hr}from"node:path";import{createHash as yr}from"node:crypto";import*as v from"typescript";function it(s,e){return yr("sha256").update(`${s}:${e}`).digest("hex").slice(0,24)}function Oe(s,e){return s.getLineAndCharacterOfPosition(e.getStart(s)).line+1}async function _e(s,e,t){let r=hr(e,t),n=await gr(r,"utf8"),i=v.createSourceFile(t,n,v.ScriptTarget.Latest,!0,t.endsWith(".tsx")||t.endsWith(".jsx")?v.ScriptKind.TSX:v.ScriptKind.TS),o=[],a=[],u=[],c=[],l={id:it(s,`file:${t}`),projectId:s,name:t,qualifiedName:t,type:"file",filePath:t,startLine:1,endLine:i.getLineAndCharacterOfPosition(i.end).line+1};o.push(l);let f=[],d=[];function m(p,S,x,w){let E=it(s,`${t}:${x}:${w??S}:${p.pos}`);return o.push({id:E,projectId:s,name:S,qualifiedName:w??S,type:x,filePath:t,startLine:Oe(i,p),endLine:i.getLineAndCharacterOfPosition(p.end).line+1}),E}function g(p){if(!v.isStringLiteral(p.moduleSpecifier))return;let S=[],x=p.importClause;if(x?.name&&S.push({localName:x.name.text,importedName:"default",kind:"default"}),x?.namedBindings&&v.isNamedImports(x.namedBindings))for(let w of x.namedBindings.elements)S.push({localName:w.name.text,importedName:w.propertyName?.text??w.name.text,kind:"named"});x?.namedBindings&&v.isNamespaceImport(x.namedBindings)&&S.push({localName:x.namedBindings.name.text,importedName:"*",kind:"namespace"}),a.push({source:p.moduleSpecifier.text,bindings:S})}function h(p,S){for(let x of p.heritageClauses??[])for(let w of x.types){let E=w.expression.getText(i).split(".").at(-1)??"";E&&c.push({fromId:S,targetName:E,type:x.token===v.SyntaxKind.ExtendsKeyword?"INHERITS":"IMPLEMENTS"})}}function y(p){let S=!1,x=!1;if(v.isImportDeclaration(p)&&g(p),v.isInterfaceDeclaration(p)&&m(p,p.name.text,"interface",p.name.text),v.isClassDeclaration(p)&&p.name){let w=m(p,p.name.text,"class",p.name.text);h(p,w),d.push({id:w,name:p.name.text}),x=!0}if(v.isFunctionDeclaration(p)&&p.name){let w=m(p,p.name.text,"function",p.name.text);f.push(w),S=!0}if(v.isMethodDeclaration(p)&&p.name){let w=p.name.getText(i),E=d.at(-1)?.name,T=m(p,w,"method",E?`${E}.${w}`:w);f.push(T),S=!0}if(v.isCallExpression(p)&&(v.isIdentifier(p.expression)&&u.push({callerId:f.at(-1),calleeName:p.expression.text,line:Oe(i,p)}),v.isPropertyAccessExpression(p.expression))){let w=p.expression;u.push({callerId:f.at(-1),qualifier:w.expression.getText(i),calleeName:w.name.text,line:Oe(i,p)})}v.forEachChild(p,y),S&&f.pop(),x&&d.pop()}return y(i),{filePath:t,symbols:o,imports:a,calls:u,heritage:c}}function Sr(s){let e=s??4;if(!Number.isSafeInteger(e)||e<1||e>64)throw new Error("concurrency must be an integer between 1 and 64");return e}function br(s){if(s?.aborted)throw new Error("Operation aborted")}async function ne(s,e,t={}){if(s.length===0)return[];let r=Math.min(Sr(t.concurrency),s.length),n=new Array(s.length),i=0,o=0,a=async()=>{for(;;){br(t.signal);let c=i;if(i+=1,c>=s.length)return;n[c]=await e(s[c],c),o+=1,t.onProgress?.({completed:o,total:s.length,index:c})}},u=Array.from({length:r},()=>a());return await Promise.all(u),n}var oe=class{async index(e,t,r={}){let n={...r.scan};r.signal&&(n.signal=r.signal);let i=await ot(t,n),o=i.files;r.onProgress?.({phase:"scan",current:o.length,total:o.length});let a=r.maxWarnings??20;if(!Number.isSafeInteger(a)||a<0)throw new Error("maxWarnings must be a non-negative integer");let u=0,c=await ne(o,async d=>{try{return await _e(e,t,d)}catch(m){return u+=1,u<=a&&console.warn(`[indexer] skipped ${d}:`,m instanceof Error?m.message:m),null}},{concurrency:r.concurrency??4,signal:r.signal,onProgress:({completed:d,total:m,index:g})=>{r.onProgress?.({phase:"parse",current:d,total:m,file:o[g]})}});u>a&&console.warn(`[indexer] ${u-a} additional parse warnings suppressed`);let l=c.filter(d=>d!==null),f=new B().build(e,l);return{files:l.length,symbols:f.allSymbols(e).length,edges:f.allEdges(e).length,parseFailures:u,scan:i.stats,graph:f}}};import{execFile as xr}from"node:child_process";import{promisify as vr}from"node:util";var oo=vr(xr);var U=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"graph","current.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e),"application/json")}};import*as M from"typescript";import{createHash as Cr}from"node:crypto";import{readFile as Er}from"node:fs/promises";import{join as Pr}from"node:path";function ct(s){return Cr("sha256").update(s).digest("hex")}var ie=class{sanitizer=new L;async build(e,t,r,n={}){let i=n.maxLines??80,o=n.overlapLines??10,a=new Map;for(let c of r){let l=a.get(c.filePath)??[];l.push(c),a.set(c.filePath,l)}let u=[];for(let[c,l]of a){let f;try{f=await Er(Pr(t,c),"utf8")}catch{continue}let d=f.split(/\r?\n/),m=l.filter(g=>g.type!=="file"&&typeof g.startLine=="number"&&typeof g.endLine=="number");if(m.length>0)for(let g of m)u.push(...this.chunkRange({projectId:e,filePath:c,lines:d,symbol:g,startLine:g.startLine,endLine:g.endLine,maxLines:i,overlapLines:o}));else u.push(...this.chunkRange({projectId:e,filePath:c,lines:d,startLine:1,endLine:Math.max(1,d.length),maxLines:i,overlapLines:o}))}return u}chunkRange(e){let{projectId:t,filePath:r,lines:n,symbol:i,maxLines:o}=e,a=Math.min(e.overlapLines,Math.max(0,o-1)),u=Math.max(1,e.startLine),c=Math.min(n.length,Math.max(u,e.endLine)),l=[],f=u,d=0;for(;f<=c;){let m=Math.min(c,f+o-1),g=n.slice(f-1,m).join(`
4
- `).trim();if(g){let h=this.sanitizer.sanitize(g).text,y=ct(h),p=[t,r,i?.id??"file",String(d),y].join(":");l.push({id:ct(p).slice(0,32),projectId:t,filePath:r,symbolId:i?.id,symbolName:i?.qualifiedName??i?.name,symbolType:i?.type,startLine:f,endLine:m,content:h,contentHash:y})}if(m>=c)break;f=m-a+1,d++}return l}};var ae=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"code","chunks","current.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e),"application/json")}};import{DatabaseSync as kr}from"node:sqlite";var ce=class{constructor(e){this.projectId=e;this.db.exec("CREATE VIRTUAL TABLE code_fts USING fts5(id UNINDEXED, projectId UNINDEXED, filePath, symbol, content, tokenize='unicode61')")}projectId;db=new kr(":memory:");chunks=new Map;get size(){return this.chunks.size}build(e){if(this.db.exec("DELETE FROM code_fts"),this.chunks.clear(),e.length===0)return 0;let t=this.db.prepare("INSERT INTO code_fts (id, projectId, filePath, symbol, content) VALUES (?, ?, ?, ?, ?)");this.db.exec("BEGIN");try{for(let r of e)this.chunks.set(r.id,r),t.run(r.id,this.projectId,r.filePath,r.symbolName??"",r.content);this.db.exec("COMMIT")}catch(r){throw this.db.exec("ROLLBACK"),r}return e.length}search(e,t=8){let r=this.toMatchQuery(e);if(!r)return[];let n=this.db.prepare("SELECT id, bm25(code_fts) AS rank FROM code_fts WHERE code_fts MATCH ? ORDER BY rank LIMIT ?").all(r,Math.max(t*4,20)),i=[];for(let o of n){let a=this.chunks.get(o.id);if(!a)continue;let u=1/(1+Math.abs(o.rank));i.push({chunk:a,score:u})}return i.slice(0,t)}toMatchQuery(e){let t=e.toLowerCase().normalize("NFKC").split(/[^\p{L}\p{N}]+/u).map(r=>r.trim()).filter(r=>r.length>0);return t.length===0?null:t.length===1?`"${t[0]}"`:`(${t.map(r=>`"${r}"`).join(" OR ")})`}close(){this.db.close()}};function lt(s,e){let t=s.toLowerCase().replaceAll("\\","/"),r=e.toLowerCase(),n=/\b(test|tests|spec|fixture|mock)\b/.test(r);return(t.startsWith("tests/")||t.includes("/tests/")||t.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(t))&&!n?.35:t.includes("/generated/")||t.includes(".generated.")?.25:1}var le=class{constructor(e){this.options=e;this.fts=new ce(e.projectId)}options;fts;async initialize(e){let{projectId:t,rootPath:r,storage:n,graph:i}=this.options,o=new ae(n),a=await new ie().build(t,r,i.allSymbols(t)),u=await o.load(t),c=!u||u.chunks.length!==a.length||u.chunks.some(f=>!a.some(d=>d.id===f.id));c&&await o.save({version:1,projectId:t,updatedAt:new Date().toISOString(),chunks:a}),e?.({current:a.length,total:a.length,phase:"index"});let l=this.fts.build(a);return{chunks:a.length,vectorsLoaded:0,vectorsIndexed:l,vectorsRemoved:0,totalVectors:this.fts.size,savedChunks:c,savedVectors:c}}async search(e,t=8){let r=this.fts.search(e,t),n=[];for(let i of r){let o=i.chunk,a=lt(o.filePath,e),u=i.score,c=0,l=u*a;n.push({chunk:o,score:l,vectorScore:c,lexicalScore:u})}return n}size(){return this.fts.size}};import{createHash as Rr}from"node:crypto";import{dirname as Tr,relative as W,resolve as H}from"node:path";import*as C from"typescript";function I(s){return s.replaceAll("\\","/").replace(/^\.\//,"")}function dt(s){return Rr("sha256").update(s).digest("hex").slice(0,24)}function V(s,e){return s.getLineAndCharacterOfPosition(e.getStart(s)).line+1}function Mr(s){let e=s;if(e.name&&C.isIdentifier(e.name))return e.name.text}var de=class{constructor(e){this.graph=e}graph;async resolveProject(e,t,r){let n=this.loadConfig(t),i=C.createProgram({rootNames:n.fileNames,options:n.options}),o=i.getTypeChecker(),a=[],u=i.getSourceFiles().filter(l=>{if(l.isDeclarationFile)return!1;let f=H(l.fileName),d=I(W(t,f));return!(d.startsWith("../")||d.includes("node_modules/"))}),c=0;for(let l of u){let f=H(l.fileName),d=I(W(t,f)),m=g=>{if(C.isCallExpression(g)){let h=this.resolveCall({projectId:e,rootPath:t,source:l,node:g,checker:o});h&&a.push(h)}C.isClassDeclaration(g)&&a.push(...this.resolveHeritage({projectId:e,rootPath:t,source:l,node:g,checker:o})),C.forEachChild(g,m)};m(l),c++,r?.({current:c,total:u.length,detail:d})}return{version:1,projectId:e,updatedAt:new Date().toISOString(),total:a.length,exact:a.filter(l=>l.confidence==="exact").length,high:a.filter(l=>l.confidence==="high").length,fallback:a.filter(l=>l.confidence==="fallback").length,resolutions:a}}resolveCall(e){let{projectId:t,rootPath:r,source:n,node:i,checker:o}=e,a;if(a=o.getResolvedSignature(i)?.declaration,!a){let c=o.getSymbolAtLocation(i.expression);a=c?.valueDeclaration??c?.declarations?.[0]}return a?this.fromDeclaration({projectId:t,rootPath:r,source:n,sourceNode:i,declaration:a,expression:i.expression.getText(n),kind:"CALL"}):this.fallbackCall(t,r,n,i)}resolveHeritage(e){let{projectId:t,rootPath:r,source:n,node:i,checker:o}=e,a=[];for(let u of i.heritageClauses??[])for(let c of u.types){let l=o.getSymbolAtLocation(c.expression),f=l?.valueDeclaration??l?.declarations?.[0];if(!f)continue;let d=this.fromDeclaration({projectId:t,rootPath:r,source:n,sourceNode:c,declaration:f,expression:c.getText(n),kind:u.token===C.SyntaxKind.ExtendsKeyword?"EXTENDS":"IMPLEMENTS"});d&&a.push(d)}return a}fromDeclaration(e){let t=e.declaration.getSourceFile();if(t.isDeclarationFile)return null;let r=I(W(e.rootPath,H(t.fileName)));if(r.startsWith("../")||r.includes("node_modules/"))return null;let n=V(t,e.declaration),i=Mr(e.declaration)??e.expression.split(".").at(-1),o=this.findGraphSymbol(e.projectId,r,n,i),a=I(W(e.rootPath,H(e.source.fileName)));return{id:dt([e.projectId,a,V(e.source,e.sourceNode),e.kind,r,n].join(":")),projectId:e.projectId,kind:e.kind,sourceFile:a,sourceLine:V(e.source,e.sourceNode),expression:e.expression,targetFile:r,targetLine:n,targetName:i,targetQualifiedName:o?.qualifiedName,targetSymbolId:o?.id,confidence:o?"exact":"high",resolver:"typescript-checker"}}fallbackCall(e,t,r,n){let i=n.expression.getText(r).split(".").at(-1);if(!i)return null;let o=this.graph.findByName(e,i).filter(c=>c.type==="function"||c.type==="method");if(o.length!==1)return null;let a=o[0],u=I(W(t,H(r.fileName)));return{id:dt(`${e}:${u}:${V(r,n)}:${a.id}`),projectId:e,kind:"CALL",sourceFile:u,sourceLine:V(r,n),expression:n.expression.getText(r),targetFile:a.filePath,targetLine:a.startLine,targetName:a.name,targetQualifiedName:a.qualifiedName,targetSymbolId:a.id,confidence:"fallback",resolver:"graph-fallback"}}findGraphSymbol(e,t,r,n){let o=this.graph.allSymbols(e).filter(a=>I(a.filePath)===I(t)).filter(a=>typeof a.startLine=="number"&&typeof a.endLine=="number"&&a.startLine<=r&&a.endLine>=r);if(n){let a=o.find(u=>u.name===n||(u.qualifiedName?.endsWith(`.${n}`)??!1));if(a)return a}return o.sort((a,u)=>(a.endLine??0)-(a.startLine??0)-((u.endLine??0)-(u.startLine??0)))[0]}loadConfig(e){let t=C.findConfigFile(e,C.sys.fileExists,"tsconfig.json");if(t){let n=C.readConfigFile(t,C.sys.readFile);if(n.error)throw new Error(C.flattenDiagnosticMessageText(n.error.messageText,`
5
- `));return C.parseJsonConfigFileContent(n.config,C.sys,Tr(t))}return{fileNames:C.sys.readDirectory(e,[".ts",".tsx",".js",".jsx",".mts",".cts",".mjs",".cjs"],["node_modules",".git","dist","build","coverage"]),options:{allowJs:!0,checkJs:!1,target:C.ScriptTarget.ES2022,module:C.ModuleKind.NodeNext,moduleResolution:C.ModuleResolutionKind.NodeNext,skipLibCheck:!0,noEmit:!0}}}};import{createHash as Ar}from"node:crypto";import{dirname as Ir,relative as O,resolve as _}from"node:path";import*as b from"typescript";function k(s){return s.replaceAll("\\","/").replace(/^\.\//,"")}function Fe(...s){return Ar("sha256").update(s.join(":")).digest("hex").slice(0,24)}function Y(s,e){return s.getLineAndCharacterOfPosition(e.getStart(s)).line+1}var Nr=new Set(["get","post","put","patch","delete","options","head"]),jr=new Set([b.SyntaxKind.EqualsToken,b.SyntaxKind.PlusEqualsToken,b.SyntaxKind.MinusEqualsToken,b.SyntaxKind.AsteriskEqualsToken,b.SyntaxKind.SlashEqualsToken,b.SyntaxKind.PercentEqualsToken,b.SyntaxKind.QuestionQuestionEqualsToken,b.SyntaxKind.AmpersandAmpersandEqualsToken,b.SyntaxKind.BarBarEqualsToken]),ue=class{constructor(e){this.graph=e}graph;enrich(e,t,r,n){let i={routes:0,tests:0,usesType:0,writes:0,callReferences:0,properties:0},o=this.loadConfig(t),a=b.createProgram({rootNames:o.fileNames,options:o.options}),u=a.getTypeChecker(),c=a.getSourceFiles().filter(f=>{if(f.isDeclarationFile)return!1;let d=k(O(t,_(f.fileName)));return!(d.startsWith("../")||d.includes("node_modules/"))}),l=0;for(let f of c){let d=k(O(t,_(f.fileName))),m=this.fileNode(e,d);if(!m)continue;let g=h=>{if(b.isCallExpression(h)&&this.addRoute(e,t,f,m,h,u)&&i.routes++,b.isTypeReferenceNode(h)&&this.addTypeUse(e,t,f,h,u)&&i.usesType++,b.isBinaryExpression(h)&&jr.has(h.operatorToken.kind)){let y=this.addWrite(e,t,f,h.left,u);y.added&&(i.writes++,y.propertyCreated&&i.properties++)}if((b.isPrefixUnaryExpression(h)||b.isPostfixUnaryExpression(h))&&(h.operator===b.SyntaxKind.PlusPlusToken||h.operator===b.SyntaxKind.MinusMinusToken)){let y=this.addWrite(e,t,f,h.operand,u);y.added&&(i.writes++,y.propertyCreated&&i.properties++)}b.forEachChild(h,g)};g(f),l++,n?.({current:l,total:c.length,detail:d})}return i.tests+=this.addTestEdges(e),r&&(i.callReferences+=this.addResolvedCalls(e,r)),i}addRoute(e,t,r,n,i,o){if(!b.isPropertyAccessExpression(i.expression))return!1;let a=i.expression.name.text.toLowerCase();if(!Nr.has(a))return!1;let u=i.arguments[0];if(!u||!(b.isStringLiteral(u)||b.isNoSubstitutionTemplateLiteral(u)))return!1;let c=u.text;if(!c.startsWith("/"))return!1;let l=Y(r,i),f=Fe(e,r.fileName,l,a,c);this.graph.getSymbol(f)||(this.graph.addSymbol({id:f,projectId:e,name:`${a.toUpperCase()} ${c}`,qualifiedName:`${a.toUpperCase()} ${c}`,type:"route",filePath:n.filePath,startLine:l,endLine:l}),this.graph.addEdge(this.edge(e,n.id,"DEFINES",f)));let d=i.arguments.at(-1);if(d){let m=this.resolveNodeSymbol(e,t,d,o);m&&this.graph.addEdge(this.edge(e,f,"ROUTE",m.id,{method:a.toUpperCase(),path:c}))}return!0}addTypeUse(e,t,r,n,i){let o=this.resolveNodeSymbol(e,t,n.typeName,i);if(!o)return!1;let a=this.containingSymbol(e,k(O(t,_(r.fileName))),Y(r,n));return!a||a.id===o.id?!1:(this.graph.addEdge(this.edge(e,a.id,"USES_TYPE",o.id)),!0)}addWrite(e,t,r,n,i){let o=this.resolveTsSymbol(n,i),a=o?.valueDeclaration??o?.declarations?.[0];if(!a)return{added:!1,propertyCreated:!1};let u=a.getSourceFile();if(u.isDeclarationFile)return{added:!1,propertyCreated:!1};let c=k(O(t,_(u.fileName)));if(c.startsWith("../")||c.includes("node_modules/"))return{added:!1,propertyCreated:!1};let l=Y(u,a),f=this.findSymbolAt(e,c,l),d=!1;if(!f||f.type==="file"){let h=o?.getName()??n.getText(r),y=Fe(e,c,"property",h,l);if(f=this.graph.getSymbol(y),!f){f={id:y,projectId:e,name:h,qualifiedName:h,type:"property",filePath:c,startLine:l,endLine:l},this.graph.addSymbol(f);let p=this.fileNode(e,c);p&&this.graph.addEdge(this.edge(e,p.id,"DEFINES",f.id)),d=!0}}if(!f)return{added:!1,propertyCreated:d};let m=k(O(t,_(r.fileName))),g=this.containingSymbol(e,m,Y(r,n));return!g||g.id===f.id?{added:!1,propertyCreated:d}:(this.graph.addEdge(this.edge(e,g.id,"WRITES",f.id,{expression:n.getText(r)})),{added:!0,propertyCreated:d})}addTestEdges(e){let t=this.graph.allEdges(e),r=0;for(let n of t){if(n.type!=="IMPORTS")continue;let i=this.graph.getSymbol(n.from),o=this.graph.getSymbol(n.to);!i||!o||!this.isTestFile(i.filePath)||this.isTestFile(o.filePath)||(this.graph.addEdge(this.edge(e,i.id,"TESTS",o.id)),r++)}return r}addResolvedCalls(e,t){let r=0;for(let n of t.resolutions){if(n.kind!=="CALL"||!n.targetSymbolId)continue;let i=this.graph.getSymbol(n.targetSymbolId);if(!i)continue;let o=this.containingSymbol(e,n.sourceFile,n.sourceLine);!o||o.id===i.id||this.graph.allEdges(e).some(u=>u.type==="CALLS"&&u.from===o.id&&u.to===i.id)||(this.graph.addEdge(this.edge(e,o.id,"CALL_REFERENCE",i.id,{expression:n.expression,confidence:n.confidence,resolver:n.resolver})),r++)}return r}resolveNodeSymbol(e,t,r,n){let i=this.resolveTsSymbol(r,n),o=i?.valueDeclaration??i?.declarations?.[0];if(!o)return;let a=o.getSourceFile();if(a.isDeclarationFile)return;let u=k(O(t,_(a.fileName)));if(!(u.startsWith("../")||u.includes("node_modules/")))return this.findSymbolAt(e,u,Y(a,o),i?.getName())}resolveTsSymbol(e,t){let r=t.getSymbolAtLocation(e);if(r&&r.flags&b.SymbolFlags.Alias)try{r=t.getAliasedSymbol(r)}catch{}return r}containingSymbol(e,t,r){return this.graph.allSymbols(e).filter(n=>k(n.filePath)===k(t)&&typeof n.startLine=="number"&&typeof n.endLine=="number"&&n.startLine<=r&&n.endLine>=r).sort((n,i)=>(n.endLine??0)-(n.startLine??0)-((i.endLine??0)-(i.startLine??0)))[0]}findSymbolAt(e,t,r,n){let i=this.graph.allSymbols(e).filter(o=>k(o.filePath)===k(t)&&typeof o.startLine=="number"&&typeof o.endLine=="number"&&o.startLine<=r&&o.endLine>=r).sort((o,a)=>(o.endLine??0)-(o.startLine??0)-((a.endLine??0)-(a.startLine??0)));if(n){let o=i.find(a=>a.name===n||(a.qualifiedName?.endsWith(`.${n}`)??!1));if(o)return o}return i[0]}fileNode(e,t){return this.graph.allSymbols(e).find(r=>r.type==="file"&&k(r.filePath)===k(t))}isTestFile(e){let t=k(e).toLowerCase();return t.startsWith("tests/")||t.includes("/tests/")||t.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(t)}edge(e,t,r,n,i){return{id:Fe(e,t,r,n,JSON.stringify(i??{})),projectId:e,from:t,to:n,type:r,metadata:i}}loadConfig(e){let t=b.findConfigFile(e,b.sys.fileExists,"tsconfig.json");if(t){let r=b.readConfigFile(t,b.sys.readFile);return b.parseJsonConfigFileContent(r.config,b.sys,Ir(t))}return{fileNames:b.sys.readDirectory(e,[".ts",".tsx",".js",".jsx",".mts",".cts",".mjs",".cjs"],["node_modules",".git","dist","build","coverage"]),options:{allowJs:!0,noEmit:!0,skipLibCheck:!0,target:b.ScriptTarget.ES2022,module:b.ModuleKind.NodeNext,moduleResolution:b.ModuleResolutionKind.NodeNext}}}};function Lr(s){return s.replaceAll("\\","/").toLowerCase()}var F=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),n=[];for(let o of t){if(o.type!=="route"&&o.type!=="file")continue;let a=this.classify(o,r);a&&n.push({symbolId:o.id,filePath:o.filePath,name:o.name,kind:a.kind,score:a.score,reasons:a.reasons})}let i=new Map;for(let o of n){let a=o.kind==="route"?o.symbolId:o.filePath,u=i.get(a);(!u||o.score>u.score)&&i.set(a,o)}return[...i.values()].sort((o,a)=>a.score-o.score||o.filePath.localeCompare(a.filePath))}classify(e,t){let r=Lr(e.filePath),n=[];if(e.type==="route")return{kind:"route",score:100,reasons:["HTTP route"]};let i=null,o=0;if(r.startsWith("bin/"))i="cli",o=100,n.push("bin executable");else if(/(^|\/)(cli)\.[^.]+$/.test(r))i="cli",o=90,n.push("CLI entry file");else if(/(^|\/)(main|server)\.[^.]+$/.test(r))i="main",o=90,n.push("main/server file");else if(r.includes("/runtime/")&&/(^|\/)(bootstrap|index)\.[^.]+$/.test(r))i="runtime",o=75,n.push("runtime bootstrap");else if(/(^|\/)index\.[^.]+$/.test(r)){let u=t.filter(l=>l.to===e.id&&l.type!=="DEFINES").length,c=t.filter(l=>l.from===e.id&&l.type!=="DEFINES").length;(r.startsWith("packages/")||u===0||c>0)&&(i="package",o=r.startsWith("packages/")?70:55,n.push("package/public index"))}return i?(t.filter(u=>u.to===e.id&&u.type!=="DEFINES").length===0&&(o+=10,n.push("no incoming dependency")),{kind:i,score:o,reasons:n}):null}};var Or={CALL_REFERENCE:6,CALLS:4,IMPORTS:3,USES_TYPE:4,WRITES:5,ROUTE:4,TESTS:1,INHERITS:5,IMPLEMENTS:5,DEFINES:.25},pe=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),n=new Map(t.map(c=>[c.id,c])),i=new Map,o=c=>{let l=i.get(c);return l||(l={symbols:0,incoming:0,outgoing:0,weightedIncoming:0,weightedOutgoing:0,writes:0,callReferences:0,crossFile:0},i.set(c,l)),l};for(let c of t)o(c.filePath).symbols++;for(let c of r){let l=n.get(c.from),f=n.get(c.to);if(!l||!f)continue;let d=Or[c.type]??1,m=o(l.filePath),g=o(f.filePath);m.outgoing++,g.incoming++,m.weightedOutgoing+=d,g.weightedIncoming+=d,c.type==="WRITES"&&m.writes++,c.type==="CALL_REFERENCE"&&m.callReferences++,l.filePath!==f.filePath&&(m.crossFile++,g.crossFile++)}let a=[...i.entries()].map(([c,l])=>{let f=Number((l.symbols*.4+l.weightedIncoming*1.75+l.weightedOutgoing*.65+l.crossFile*1.5+l.writes*3+l.callReferences*2).toFixed(2)),d=[];return l.incoming>=10&&d.push("high incoming dependency"),l.crossFile>=10&&d.push("high cross-file coupling"),l.callReferences>=5&&d.push("many exact call references"),l.writes>0&&d.push("contains write relationships"),l.symbols>=10&&d.push("high symbol density"),{filePath:c,score:f,symbols:l.symbols,incoming:l.incoming,outgoing:l.outgoing,reasons:d}}).filter(c=>c.score>0).sort((c,l)=>l.score-c.score||c.filePath.localeCompare(l.filePath));if(a.length===0)return[];let u=Math.max(1,Math.min(30,Math.ceil(a.length*.15)));return a.slice(0,u)}};function _r(s){return s.replaceAll("\\","/").toLowerCase()}var Fr={CALL_REFERENCE:6,CALLS:4,IMPORTS:4,USES_TYPE:4,WRITES:5,INHERITS:6,IMPLEMENTS:6,ROUTE:3,TESTS:1},me=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),n=new Map(t.map(a=>[a.id,a])),i=[...new Set(t.map(a=>a.filePath))].sort(),o=new Map;for(let a of i)o.set(a,this.classifyPath(a));for(let a=0;a<3;a++){let u=!1;for(let c of i){let l=o.get(c);if(!l||l.layer!=="unknown")continue;let f=new Map;for(let m of r){let g=n.get(m.from),h=n.get(m.to);if(!g||!h||g.filePath===h.filePath)continue;let y=null;if(g.filePath===c?y=h.filePath:h.filePath===c&&(y=g.filePath),!y)continue;let p=o.get(y);if(!p||p.layer==="unknown"||p.layer==="tests")continue;let S=Fr[m.type]??1;f.set(p.layer,(f.get(p.layer)??0)+S)}let d=[...f.entries()].sort((m,g)=>g[1]-m[1]||m[0].localeCompare(g[0]))[0];d&&d[1]>=4&&(o.set(c,{filePath:c,layer:d[0],confidence:.65,reasons:[`graph-neighbor vote=${d[1]}`]}),u=!0)}if(!u)break}return i.map(a=>o.get(a))}classifyPath(e){let t=_r(e);return t.startsWith("tests/")||t.includes("/tests/")||t.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(t)?{filePath:e,layer:"tests",confidence:1,reasons:["test path"]}:t.startsWith("bin/")||t.includes("/mcp/")||t.includes("/cli/")||t.includes("/api/")||t.includes("/routes/")||t.includes("/hooks/")?{filePath:e,layer:"interface",confidence:.9,reasons:["interface boundary"]}:t.includes("/runtime/")||t.includes("/processor/")||t.includes("/retrieval/")||t.includes("/snapshot/")||t.includes("/code-intelligence/")?{filePath:e,layer:"application",confidence:.85,reasons:["application orchestration"]}:t.includes("/core/")||t.includes("/memory/")||/(^|\/)types\.[^.]+$/.test(t)?{filePath:e,layer:"domain",confidence:.9,reasons:["domain/core logic"]}:t.includes("/storage/")||t.includes("/production/")||t.includes("/security/")||t.includes("/config/")||t.includes("/utils/")?{filePath:e,layer:"infrastructure",confidence:.85,reasons:["infrastructure boundary"]}:{filePath:e,layer:"unknown",confidence:.35,reasons:[]}}};import{createHash as $r}from"node:crypto";var Dr={CALL_REFERENCE:7,CALLS:3,IMPORTS:4,USES_TYPE:4,WRITES:5,INHERITS:7,IMPLEMENTS:7,ROUTE:4,TESTS:1};function Gr(s){return $r("sha256").update(s).digest("hex").slice(0,12)}function fe(s){return s.replaceAll("\\","/")}function zr(s){let t=fe(s).split("/").filter(Boolean);return t[0]==="src"&&t[1]?`src/${t[1]}`:t[0]==="packages"&&t[1]?`packages/${t[1]}`:t[0]==="tests"?"tests":t[0]==="bin"?"bin":t[0]??"root"}function Kr(s){let e=s.replace(/^src\//,"").replace(/^packages\//,"");return{"code-intelligence":"Code Intelligence",memory:"Memory",storage:"Storage",retrieval:"Retrieval",runtime:"Runtime",mcp:"MCP",hooks:"Hooks",security:"Security",snapshot:"Snapshots",production:"Production",capture:"Capture",processor:"Processor",core:"Core",tests:"Tests",bin:"CLI",sdk:"SDK",cli:"CLI"}[e]??e}var ge=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),n=new Map(t.map(d=>[d.id,d])),i=[...new Set(t.map(d=>fe(d.filePath)))].sort(),o=new Map,a=(d,m)=>{let g=[d,m].sort();return`${g[0]}\0${g[1]}`};for(let d of r){let m=n.get(d.from),g=n.get(d.to);if(!m||!g)continue;let h=fe(m.filePath),y=fe(g.filePath);if(h===y)continue;let p=Dr[d.type]??0;if(p<=0)continue;let S=a(h,y),x=o.get(S);x?x.weight+=p:o.set(S,{a:[h,y].sort()[0],b:[h,y].sort()[1],weight:p})}let u=new Map;for(let d of i){let m=zr(d),g=u.get(m)??[];g.push(d),u.set(m,g)}let c=[];for(let[d,m]of u){if(m.sort(),m.length<=30){c.push({subsystem:d,files:m});continue}let g=new Set(m),h=new Map;for(let x of m)h.set(x,new Set);for(let x of o.values())!g.has(x.a)||!g.has(x.b)||x.weight>=8&&(h.get(x.a)?.add(x.b),h.get(x.b)?.add(x.a));let y=new Set,p=[];for(let x of m){if(y.has(x))continue;let w=[x],E=[];for(y.add(x);w.length>0;){let T=w.shift();E.push(T);for(let Ne of h.get(T)??[])y.has(Ne)||(y.add(Ne),w.push(Ne))}p.push(E.sort())}let S=[];for(let x of p)x.length>=2?c.push({subsystem:d,files:x}):S.push(...x);S.length>0&&c.push({subsystem:`${d}/residual`,files:S.sort()})}let l=c.map(d=>({subsystem:d.subsystem,files:[...d.files]}));for(let d=0;d<3;d++){let m=!1;e:for(let g=0;g<l.length;g++){let h=l[g];if(!(!h||h.files.length>3))for(let y=0;y<l.length;y++){if(g===y)continue;let p=l[y];if(!p)continue;let S=0,x=new Set(h.files),w=new Set(p.files);for(let E of o.values())(x.has(E.a)&&w.has(E.b)||x.has(E.b)&&w.has(E.a))&&(S+=E.weight);if(S>=20){p.files=[...new Set([...p.files,...h.files])].sort(),l.splice(g,1),m=!0;break e}}}if(!m)break}let f=[];for(let d of l){let m=new Set(d.files),g=0,h=0;for(let w of o.values()){let E=m.has(w.a),T=m.has(w.b);E&&T?g+=w.weight:(E||T)&&(h+=w.weight)}let y=g+h,p=d.subsystem.replace(/\/residual$/,""),S=Kr(p),x=d.subsystem.endsWith("/residual")?`${S} Misc`:S;f.push({id:`cluster-${Gr(`${d.subsystem}:${d.files.join("|")}`)}`,label:x,subsystem:p,files:d.files,size:d.files.length,internalWeight:g,externalWeight:h,cohesion:y>0?Number((g/y).toFixed(4)):0})}return f.sort((d,m)=>m.size-d.size||m.cohesion-d.cohesion||d.label.localeCompare(m.label))}};var he=class{constructor(e){this.graph=e;this.entryPoints=new F(e),this.hotspots=new pe(e),this.layers=new me(e),this.clusters=new ge(e)}graph;entryPoints;hotspots;layers;clusters;analyze(e){let t=this.entryPoints.detect(e),r=this.hotspots.analyze(e),n=this.layers.detect(e),i=this.clusters.detect(e),o=new Set(this.graph.allSymbols(e).map(a=>a.filePath));return{version:1,projectId:e,updatedAt:new Date().toISOString(),summary:{files:o.size,entryPoints:t.length,hotspots:r.length,layers:n.length,clusters:i.length},entryPoints:t,hotspots:r,layers:n,clusters:i}}};var ut=new Set(["CALLS","CALL_REFERENCE","IMPORTS","USES_TYPE","WRITES","INHERITS","IMPLEMENTS","ROUTE","TESTS"]);function $e(s){return s.replaceAll("\\","/").toLowerCase()}function Br(s){let e=$e(s);return e.startsWith("tests/")||e.includes("/tests/")||e.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(e)}function Ur(s){let e=$e(s.filePath);return e.startsWith("bin/")||/(^|\/)index\.[^.]+$/.test(e)||/(^|\/)(main|server|cli)\.[^.]+$/.test(e)}var ye=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),n=new Set(new F(this.graph).detect(e).map(o=>o.symbolId)),i=[];for(let o of t){if(!["function","method","class","interface","property"].includes(o.type)||Br(o.filePath)||n.has(o.id)||Ur(o)||r.filter(m=>m.to===o.id&&ut.has(m.type)).length>0)continue;let u=r.filter(m=>m.from===o.id&&ut.has(m.type)),c=["no incoming usage edge"],l=50;(o.type==="function"||o.type==="method")&&(l+=20,c.push("callable symbol")),o.type==="property"&&(l-=15),u.length===0&&(l+=10,c.push("no outgoing dependency")),$e(o.filePath).includes("/internal/")&&(l+=10,c.push("internal implementation")),o.name.startsWith("_")&&(l+=5);let d=l>=75?"high":l>=55?"medium":"low";i.push({symbolId:o.id,name:o.name,qualifiedName:o.qualifiedName,type:o.type,filePath:o.filePath,startLine:o.startLine,confidence:d,score:l,reasons:c})}return i.sort((o,a)=>a.score-o.score||o.filePath.localeCompare(a.filePath)||o.name.localeCompare(a.name))}};var Wr=new Set(["CALLS","CALL_REFERENCE","IMPORTS","USES_TYPE","WRITES","INHERITS","IMPLEMENTS","ROUTE"]),Se=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e).filter(o=>Wr.has(o.type)),n=new Map(t.map(o=>[o.id,o]));return[...new Set(t.map(o=>o.filePath))].sort().map(o=>{let a=new Set,u=new Set,c=0,l=0;for(let f of r){let d=n.get(f.from),m=n.get(f.to);!d||!m||d.filePath===m.filePath||(d.filePath===o&&(a.add(m.filePath),c++),m.filePath===o&&(u.add(d.filePath),l++))}return{filePath:o,dependencies:[...a].sort(),dependents:[...u].sort(),outgoingEdges:c,incomingEdges:l}})}};var be=class{constructor(e){this.graph=e}graph;analyze(e){let t=new ye(this.graph).analyze(e),r=new Se(this.graph).analyze(e);return{version:1,projectId:e,updatedAt:new Date().toISOString(),summary:{deadCodeCandidates:t.length,highConfidenceDeadCode:t.filter(n=>n.confidence==="high").length,files:r.length},deadCode:t,dependencies:r}}};var xe=class{constructor(e){this.graph=e}graph;build(e,t,r,n){let i=this.graph.allSymbols(e),o=this.graph.allEdges(e),a=new Map(t?.layers.map(p=>[p.filePath,p.layer])??[]),u=new Map;for(let p of t?.clusters??[])for(let S of p.files)u.set(S,{id:p.id,label:p.label});let c=new Map(t?.hotspots.map(p=>[p.filePath,p.score])??[]),l=new Map(r?.deadCode.map(p=>[p.symbolId,{confidence:p.confidence,score:p.score}])??[]),f=new Map,d=new Map;for(let p of o)d.set(p.from,(d.get(p.from)??0)+1),f.set(p.to,(f.get(p.to)??0)+1);let m=[];for(let p=0;p<i.length;p++){let S=i[p],x=u.get(S.filePath);m.push({id:S.id,name:S.name,qualifiedName:S.qualifiedName,type:S.type,filePath:S.filePath,layer:a.get(S.filePath),cluster:x?.id,clusterLabel:x?.label,hotspotScore:c.get(S.filePath),deadCode:l.get(S.id),incoming:f.get(S.id)??0,outgoing:d.get(S.id)??0}),((p+1)%100===0||p===i.length-1)&&n?.({current:p+1,total:i.length,phase:"nodes"})}let g=new Set(m.map(p=>p.id)),h=o.filter(p=>g.has(p.from)&&g.has(p.to)),y=[];for(let p=0;p<h.length;p++){let S=h[p];y.push({source:S.from,target:S.to,type:S.type,confidence:typeof S.metadata?.confidence=="string"?S.metadata.confidence:void 0,resolver:typeof S.metadata?.resolver=="string"?S.metadata.resolver:void 0}),((p+1)%100===0||p===h.length-1)&&n?.({current:p+1,total:h.length,phase:"links"})}return{version:1,projectId:e,generatedAt:new Date().toISOString(),summary:{nodes:m.length,links:y.length,files:new Set(m.map(p=>p.filePath)).size,clusters:new Set(m.map(p=>p.cluster).filter(Boolean)).size},nodes:m,links:y}}};import{homedir as fs}from"node:os";import{join as gs}from"node:path";import{DeleteObjectCommand as Hr,GetObjectCommand as Vr,HeadObjectCommand as Yr,ListObjectsV2Command as Jr,PutObjectCommand as qr,S3Client as Zr}from"@aws-sdk/client-s3";import{getSignedUrl as Qr}from"@aws-sdk/s3-request-presigner";var ve=class{name="huggingface";client;bucket;constructor(e){this.bucket=e.bucket,this.client=new Zr({region:"us-east-1",endpoint:`https://s3.hf.co/${e.namespace}`,forcePathStyle:!0,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}})}async put(e,t,r="application/octet-stream"){let n=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new qr({Bucket:this.bucket,Key:e,Body:n,ContentType:r}))}async get(e){let t=await Qr(this.client,new Vr({Bucket:this.bucket,Key:e}),{expiresIn:60}),r=await fetch(t,{redirect:"follow"});if(r.status===404)return null;if(!r.ok)throw new Error(`HF download failed: ${r.status} ${r.statusText}`);return new Uint8Array(await r.arrayBuffer())}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await this.client.send(new Yr({Bucket:this.bucket,Key:e})),!0}catch(t){if(typeof t=="object"&&t!==null&&"$metadata"in t&&t.$metadata?.httpStatusCode===404)return!1;throw t}}async delete(e){await this.client.send(new Hr({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let n=await this.client.send(new Jr({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let i of n.Contents??[])i.Key&&t.push({key:i.Key,size:i.Size,updatedAt:i.LastModified?.toISOString()});r=n.IsTruncated?n.NextContinuationToken:void 0}while(r);return t}};import{access as pt,mkdir as Xr,readFile as es,readdir as ts,rm as rs,stat as mt,writeFile as ss}from"node:fs/promises";import{dirname as ns,join as os,relative as ft,resolve as is}from"node:path";var J=class{constructor(e){this.root=e}root;name="local";path(e){let t=e.replace(/^\/+/,"");return is(this.root,t)}async put(e,t){let r=this.path(e);await Xr(ns(r),{recursive:!0}),await ss(r,t)}async get(e){try{return await es(this.path(e))}catch(t){if(typeof t=="object"&&t!==null&&"code"in t&&t.code==="ENOENT")return null;throw t}}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await pt(this.path(e)),!0}catch{return!1}}async delete(e){await rs(this.path(e),{force:!0})}async list(e=""){let t=this.path(e),r=[];try{await pt(t)}catch{return r}let n=async o=>{let a=await ts(o,{withFileTypes:!0});for(let u of a){let c=os(o,u.name);if(u.isDirectory()){await n(c);continue}let l=await mt(c);r.push({key:ft(this.root,c),size:l.size,updatedAt:l.mtime.toISOString()})}},i=await mt(t);return i.isDirectory()?await n(t):r.push({key:ft(this.root,t),size:i.size,updatedAt:i.mtime.toISOString()}),r}};import{DeleteObjectCommand as as,GetObjectCommand as cs,HeadObjectCommand as ls,ListObjectsV2Command as ds,PutObjectCommand as us,S3Client as ps}from"@aws-sdk/client-s3";import{getSignedUrl as ms}from"@aws-sdk/s3-request-presigner";var q=class{name;client;bucket;constructor(e){this.name=e.name??"s3",this.bucket=e.bucket,this.client=new ps({region:e.region??"us-east-1",endpoint:e.endpoint||void 0,forcePathStyle:e.forcePathStyle??!1,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}})}async put(e,t,r="application/octet-stream"){let n=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new us({Bucket:this.bucket,Key:e,Body:n,ContentType:r}))}async get(e){let t=await ms(this.client,new cs({Bucket:this.bucket,Key:e}),{expiresIn:60}),r=await fetch(t,{redirect:"follow"});if(r.status===404)return null;if(!r.ok)throw new Error(`${this.name} download failed: ${r.status} ${r.statusText}`);return new Uint8Array(await r.arrayBuffer())}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await this.client.send(new ls({Bucket:this.bucket,Key:e})),!0}catch(t){if(typeof t=="object"&&t!==null&&"$metadata"in t&&t.$metadata?.httpStatusCode===404)return!1;throw t}}async delete(e){await this.client.send(new as({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let n=await this.client.send(new ds({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let i of n.Contents??[])i.Key&&t.push({key:i.Key,size:i.Size,updatedAt:i.LastModified?.toISOString()});r=n.IsTruncated?n.NextContinuationToken:void 0}while(r);return t}};function De(s,e){return console.warn(e),new J(s)}function gt(s){let e=s.localRoot??gs(fs(),".toolnet-memory","storage");if(s.provider==="r2"){let t=s.r2;return t?.accountId&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new q({name:"r2",endpoint:`https://${t.accountId}.r2.cloudflarestorage.com`,region:"auto",bucket:t.bucket,forcePathStyle:!0,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):De(e,"[storage] Cloudflare R2 credentials missing. Using local fallback.")}if(s.provider==="s3"){let t=s.s3;return t?.bucket&&t.accessKeyId&&t.secretAccessKey?new q({name:"s3",endpoint:t.endpoint,region:t.region??"us-east-1",bucket:t.bucket,forcePathStyle:t.forcePathStyle??!1,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):De(e,"[storage] S3 credentials missing. Using local fallback.")}if(s.provider==="huggingface"){let t=s.huggingface;return t?.namespace&&t.bucket&&t.accessKeyId&&t.secretAccessKey?new ve({namespace:t.namespace,bucket:t.bucket,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey}):De(e,"[storage] Hugging Face credentials missing. Using local fallback.")}return new J(e)}function hs(s){return new Promise(e=>setTimeout(e,s))}async function ht(s,e={}){let t=Math.max(1,e.attempts??3),r=e.baseDelayMs??150,n=e.maxDelayMs??2e3,i;for(let o=1;o<=t;o++)try{return await s()}catch(a){if(i=a,o>=t)break;let u=Math.min(n,r*2**(o-1)),c=Math.floor(Math.random()*Math.max(1,u*.2));await hs(u+c)}throw i}var ys=new Set(["put","get","getText","delete","list"]);function yt(s,e={}){return new Proxy(s,{get(t,r){let n=Reflect.get(t,r,t);return typeof n!="function"?n:ys.has(r)?(...i)=>ht(()=>Promise.resolve(n.apply(t,i)),e):n.bind(t)}})}function St(s){let e=s.trim().replace(/\s+/g,"_").replace(/[^A-Za-z0-9._-]/g,"_").replace(/_+/g,"_").replace(/^\.+|\.+$/g,"").slice(0,100);if(!e||e==="."||e==="..")throw new Error("Invalid project storage folder");return e}function bt(s){let e=s.replace(/^\/+/,"");if(e.startsWith("memories/"))return"memory/records/"+e.slice(9);if(e.startsWith("vectors/"))return"memory/vectors/"+e.slice(8);if(e.startsWith("graph/"))return"code/graph/"+e.slice(6);let t=e.match(/^(snapshots\/[^/]+\/)memories\/(.+)$/);if(t)return`${t[1]}memory/records/${t[2]}`;if(t=e.match(/^(snapshots\/[^/]+\/)vectors\/(.+)$/),t)return`${t[1]}memory/vectors/${t[2]}`;if(t=e.match(/^(snapshots\/[^/]+\/)graph\/(.+)$/),t)return`${t[1]}code/graph/${t[2]}`;let r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)memories\/(.+)$/);if(r)return`${r[1]}memory/records/${r[2]}`;if(r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)vectors\/(.+)$/),r)return`${r[1]}memory/vectors/${r[2]}`;if(r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)graph\/(.+)$/),r)return`${r[1]}code/graph/${r[2]}`;let n=e.match(/^(projects\/[^/]+\/)memories\/(.+)$/);return n?`${n[1]}memory/records/${n[2]}`:(n=e.match(/^(projects\/[^/]+\/)vectors\/(.+)$/),n?`${n[1]}memory/vectors/${n[2]}`:(n=e.match(/^(projects\/[^/]+\/)graph\/(.+)$/),n?`${n[1]}code/graph/${n[2]}`:e))}var we=class{constructor(e,t,r,n){this.provider=e;this.name=e.name,this.projectId=t,this.projectName=r,this.folder=St(n??r),this.sourcePrefix=`projects/${t}`,this.targetPrefix=`projects/${this.folder}`}provider;name;folder;sourcePrefix;targetPrefix;projectId;projectName;registryPromise;async registerProject(){let e=`${this.targetPrefix}/project.json`,t=new Date().toISOString(),r=t,n=await this.provider.getText(e);if(n){let o;try{o=JSON.parse(n)}catch(a){throw new Error(`Invalid remote ToolNet project manifest at ${e}: ${a instanceof Error?a.message:String(a)}`)}if(typeof o.id=="string"&&o.id!==this.projectId)throw new Error(["ToolNet remote project namespace collision.",`Remote folder: ${this.targetPrefix}`,`Existing project id: ${o.id}`,`Current project id: ${this.projectId}`,"Refusing to mix data from two projects."].join(" "));typeof o.createdAt=="string"&&(r=o.createdAt)}let i={version:1,id:this.projectId,name:this.projectName,remote:this.folder,createdAt:r,updatedAt:t};await this.provider.put(e,JSON.stringify(i,null,2)+`
6
- `,"application/json")}async ensureRegistered(){return this.registryPromise||(this.registryPromise=this.registerProject()),this.registryPromise}key(e){if(e=bt(e),e===this.sourcePrefix)return this.targetPrefix;if(e.startsWith(`${this.sourcePrefix}/`))return this.targetPrefix+e.slice(this.sourcePrefix.length);if(e===this.targetPrefix||e.startsWith(`${this.targetPrefix}/`))return e;if(e.startsWith("projects/"))throw new Error(["Cross-project storage access denied.",`Current project: ${this.targetPrefix}`,`Requested key: ${e}`].join(" "));return e}async put(e,t,r){return await this.ensureRegistered(),this.provider.put(this.key(e),t,r)}async get(e){return await this.ensureRegistered(),this.provider.get(this.key(e))}async getText(e){return await this.ensureRegistered(),this.provider.getText(this.key(e))}async delete(e){return await this.ensureRegistered(),this.provider.delete(this.key(e))}async exists(e){return await this.ensureRegistered(),this.provider.exists(this.key(e))}async list(e){return await this.ensureRegistered(),this.provider.list(this.key(e))}};var Ce=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"graph","resolution","current.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e),"application/json")}};var Ee=class{constructor(e){this.storage=e}storage;base(e){return["projects",e,"code","architecture"].join("/")}async load(e){let t=await this.storage.getText(`${this.base(e)}/current.json`);return t?JSON.parse(t):null}async save(e){let t=this.base(e.projectId),r=n=>JSON.stringify({version:e.version,projectId:e.projectId,updatedAt:e.updatedAt,data:n},null,2);await Promise.all([this.storage.put(`${t}/current.json`,JSON.stringify(e,null,2),"application/json"),this.storage.put(`${t}/entry-points.json`,r(e.entryPoints),"application/json"),this.storage.put(`${t}/hotspots.json`,r(e.hotspots),"application/json"),this.storage.put(`${t}/layers.json`,r(e.layers),"application/json"),this.storage.put(`${t}/clusters.json`,r(e.clusters),"application/json")])}};var Pe=class{constructor(e){this.storage=e}storage;base(e){return["projects",e,"code","analysis"].join("/")}async load(e){let t=await this.storage.getText(`${this.base(e)}/current.json`);return t?JSON.parse(t):null}async save(e){let t=this.base(e.projectId),r=n=>JSON.stringify({version:e.version,projectId:e.projectId,updatedAt:e.updatedAt,data:n},null,2);await Promise.all([this.storage.put(`${t}/current.json`,JSON.stringify(e,null,2),"application/json"),this.storage.put(`${t}/dead-code.json`,r(e.deadCode),"application/json"),this.storage.put(`${t}/dependencies.json`,r(e.dependencies),"application/json")])}};var ke=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"code","visualization","graph.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e,null,2),"application/json")}};async function xt(s,e={}){let t=Date.now();async function r(h,y,p){await e.onStage?.({id:h,title:y,state:"start"});let S=Date.now(),x=await p();return await e.onStage?.({id:h,title:y,state:"complete",durationMs:Date.now()-S}),x}let n=Ve(),i=yt(gt({provider:n.storage.provider,huggingface:n.storage.huggingface,localRoot:n.storage.localRoot}),{attempts:3}),o=new we(i,s.id,s.name,s.remote??s.name),a=new U(o),u=await r("source-index","Source Index",async()=>{let h=await new oe().index(s.id,s.rootPath,{onProgress:y=>{e.onSourceProgress?.(y)}});return await a.save({version:1,projectId:s.id,updatedAt:new Date().toISOString(),files:h.files,symbols:h.graph.allSymbols(s.id),edges:h.graph.allEdges(s.id)}),h}),c=u.graph,l=await r("type-resolution","Type Resolution",async()=>{let h=await new de(c).resolveProject(s.id,s.rootPath,y=>{e.onStageProgress?.({stage:"type-resolution",current:y.current,total:y.total,phase:y.phase,detail:y.detail})});return await new Ce(o).save(h),h});await r("rich-graph","Rich Graph",async()=>{let h=new ue(c).enrich(s.id,s.rootPath,l,S=>{e.onStageProgress?.({stage:"rich-graph",current:S.current,total:S.total,phase:S.phase,detail:S.detail})}),y=c.allSymbols(s.id),p=c.allEdges(s.id);return await a.save({version:1,projectId:s.id,updatedAt:new Date().toISOString(),files:y.filter(S=>S.type==="file").length,symbols:y,edges:p}),h});let f=await r("semantic-index","Semantic Code Index",async()=>new le({projectId:s.id,rootPath:s.rootPath,storage:o,graph:c}).initialize(y=>{e.onStageProgress?.({stage:"semantic-index",current:y.current,total:y.total,phase:y.phase,detail:y.detail})})),d=await r("architecture","Architecture Intelligence",async()=>{let h=new he(c).analyze(s.id);return await new Ee(o).save(h),h}),m=await r("analysis","Graph Analysis",async()=>{let h=new be(c).analyze(s.id);return await new Pe(o).save(h),h}),g=await r("visualization","3D Visualization Dataset",async()=>{let h=new xe(c).build(s.id,d,m,y=>{e.onStageProgress?.({stage:"visualization",current:y.current,total:y.total,phase:y.phase,detail:y.detail})});return await new ke(o).save(h),h});return{files:u.files,project:{id:s.id,name:s.name,remote:s.remote??s.name},storage:o.name,durationMs:Date.now()-t,graph:{symbols:c.allSymbols(s.id).length,edges:c.allEdges(s.id).length},resolution:{total:l.total,exact:l.exact,high:l.high,fallback:l.fallback},semantic:f,architecture:d.summary,analysis:m.summary,visualization:g.summary}}import{Worker as vs}from"worker_threads";import*as Te from"path";import{fileURLToPath as ws}from"node:url";function Re(){if(process.argv.includes("--no-color"))return!1;if(process.argv.includes("--color"))return!0;let s=process.env.NO_COLOR;if(s!==void 0&&s!=="")return!1;let e=process.env.FORCE_COLOR;if(e!==void 0&&e!=="")return e!=="0"&&e.toLowerCase()!=="false";if(process.stdout.isTTY===!0&&process.env.TERM!=="dumb")return!0;let t=process.env.CI;return t!==void 0&&t!==""}var Ss={spinner:["\xB7","\u2722","\u2733","\u2736","\u273B","\u273D"],barFilled:"\u2588",barEmpty:"\u2591",phaseDone:"\u25C6",phaseActive:"\u25C7",rail:"\u2502",branch:"\u251C",corner:"\u2514",dash:"\u2014"},bs={spinner:[".","*","+","x","*","+"],barFilled:"#",barEmpty:"-",phaseDone:"[OK]",phaseActive:"[>>]",rail:"|",branch:"|",corner:"\\",dash:"-"};function xs(){if(process.env.TOOLNET_ASCII==="1")return!1;if(process.env.TOOLNET_UNICODE==="1")return!0;if(process.platform==="win32"){let s=process.env.TERM_PROGRAM,e=process.env.WT_SESSION,t=process.env.VSCODE_PID,r=process.env.ConEmuPID;return!!(e||t||r||s==="vscode")}return process.env.TERM!=="linux"}var Ge;function $(){return Ge||(Ge=xs()?Ss:bs),Ge}var vt={scan:"Scanning files",parse:"Parsing code","type-resolution":"Type Resolution","rich-graph":"Rich Graph","semantic-index":"Semantic Code Index",architecture:"Architecture Intelligence","graph-analysis":"Graph Analysis",visualization:"3D Visualization Dataset"};function wt(){if(process.stdout.isTTY!==!0)return Cs();let s=Re(),e=$(),t=s?"\x1B[2m":"",r=s?"\x1B[32m":"",n=s?"\x1B[0m":"",i="",o="",a=-1,u=0,c=()=>{if(!o)return;let g="";a>=0?g=` ${e.dash} done`:u>0&&(g=` ${e.dash} ${u.toLocaleString()} found`),process.stdout.write(`\r\x1B[K${t}${e.branch}${n} ${r}${e.phaseDone}${n} ${o}${g}
7
- `),o="",a=-1,u=0},l=Te.dirname(ws(import.meta.url)),f=l.includes("/bundle/")||l.includes("\\bundle\\"),d=Te.join(l,f?"shimmer-worker.js":"shimmer-worker.ts"),m=new vs(d,{workerData:{startTime:Date.now(),colors:s}});return{onProgress(g){let h=vt[g.stage]||g.stage;g.stage!==i&&i&&c(),i=g.stage,o=h;let y=-1,p=0;if(g.total>0&&g.current>=0){let S=g.current/g.total*100;y=g.current>=g.total?100:Math.min(99,Math.round(S))}else g.current>0&&(p=g.current);a=y,u=p,m.postMessage({type:"update",phase:g.stage,phaseName:h,percent:y,count:p})},stop(){return new Promise(g=>{let h=!1,y=()=>{h||(h=!0,c(),g())},p=setTimeout(()=>{m.terminate().then(y)},2e3);m.on("message",S=>{S.type==="stopped"&&(clearTimeout(p),m.terminate().then(y))}),m.postMessage({type:"stop"})})}}}function Cs(){let s="";return{onProgress(e){if(e.stage===s)return;s=e.stage;let t=vt[e.stage]||e.stage;process.stdout.write(`${t}...
8
- `)},stop(){return Promise.resolve()}}}var P={reset:"\x1B[0m",green:"\x1B[32m",gray:"\x1B[90m",white:"\x1B[97m",cyan:"\x1B[36m",red:"\x1B[31m"};function N(s){return new Intl.NumberFormat("en-US").format(s)}function ze(s){return s<1e3?`${s}ms`:`${(s/1e3).toFixed(1)}s`}function Ct(s,e,t){return Math.max(e,Math.min(t,s))}function Es(s,e){return e<=0?"":s.length<=e?s:e<=3?s.slice(0,e):`${s.slice(0,e-1)}\u2026`}var Me=class{stream;interactive;enabled;colorEnabled;rootPath;stages;shimmerProgress;startedAt=Date.now();scanTotal;parseCurrent=0;parseTotal=0;finished=!1;lastPlainParseBucket=-1;constructor(e){this.stream=e.stream??process.stdout,this.enabled=e.enabled??!0;let t=e.interactive??this.stream.isTTY===!0;this.interactive=t&&process.env.TERM!=="dumb",this.colorEnabled=this.interactive&&Re(),this.rootPath=e.rootPath,this.stages=e.stages.map(r=>({...r,state:"pending"}))}start(){if(!this.enabled)return;if(this.startedAt=Date.now(),!this.interactive){this.stream.write(`\u25C7 Initializing ToolNet Index
1
+ import{closeSync as Us,mkdirSync as Hs,openSync as Ws,rmSync as Ys,writeFileSync as Vs}from"node:fs";import{join as mt}from"node:path";import{existsSync as fr,readFileSync as gr}from"node:fs";import{homedir as hr}from"node:os";import{join as yr}from"node:path";function Sr(n){let e=n.trim();return e.length>=2&&e.startsWith('"')&&e.endsWith('"')?(e=e.slice(1,-1),e.replace(/\\n/g,`
2
+ `).replace(/\\r/g,"\r").replace(/\\t/g," ").replace(/\\"/g,'"').replace(/\\\\/g,"\\")):e.length>=2&&e.startsWith("'")&&e.endsWith("'")?e.slice(1,-1):e}function br(){let n=process.env.TOOLNET_GLOBAL_ENV??yr(hr(),".config","toolnet-memory",".env");if(!fr(n))return;let e=gr(n,"utf8");for(let t of e.split(/\r?\n/)){let r=t.trim();if(!r||r.startsWith("#"))continue;r.startsWith("export ")&&(r=r.slice(7));let s=r.indexOf("=");if(s<=0)continue;let i=r.slice(0,s).trim();/^[A-Za-z_][A-Za-z0-9_]*$/.test(i)&&process.env[i]===void 0&&(process.env[i]=Sr(r.slice(s+1)))}}br();function W(n,e){return n===void 0?e:["1","true","yes","on"].includes(n.toLowerCase())}function Y(n,e){if(!n)return e;let t=Number(n);return Number.isFinite(t)?t:e}function gt(){return{memory:{autoCapture:W(process.env.MEMORY_AUTO_CAPTURE,!0),autoRetrieve:W(process.env.MEMORY_AUTO_RETRIEVE,!0),autoSummarize:W(process.env.MEMORY_AUTO_SUMMARIZE,!0),autoSync:W(process.env.MEMORY_AUTO_SYNC,!0)},retrieval:{maxCandidates:Y(process.env.MEMORY_MAX_CANDIDATES,50),rerankTop:Y(process.env.MEMORY_RERANK_TOP,10),finalContext:Y(process.env.MEMORY_FINAL_CONTEXT,5),tokenBudget:Y(process.env.MEMORY_TOKEN_BUDGET,2e3)},storage:{provider:process.env.MEMORY_STORAGE_PROVIDER??"huggingface",r2:{accountId:process.env.R2_ACCOUNT_ID,bucket:process.env.R2_BUCKET,accessKeyId:process.env.R2_ACCESS_KEY_ID,secretAccessKey:process.env.R2_SECRET_ACCESS_KEY},s3:{endpoint:process.env.S3_ENDPOINT,region:process.env.S3_REGION,bucket:process.env.S3_BUCKET,accessKeyId:process.env.S3_ACCESS_KEY_ID,secretAccessKey:process.env.S3_SECRET_ACCESS_KEY,forcePathStyle:W(process.env.S3_FORCE_PATH_STYLE,!1)},huggingface:{namespace:process.env.HF_NAMESPACE,bucket:process.env.HF_BUCKET,accessKeyId:process.env.HF_S3_ACCESS_KEY_ID,secretAccessKey:process.env.HF_S3_SECRET_ACCESS_KEY},localRoot:process.env.MEMORY_LOCAL_STORAGE_PATH},cache:{maxMb:Y(process.env.MEMORY_LOCAL_CACHE_MB,200)}}}import{createHash as Rr}from"node:crypto";import{existsSync as pe,mkdirSync as Tr,readFileSync as kr,renameSync as Ir,writeFileSync as Ar}from"node:fs";import{basename as Mr,dirname as me,join as q,parse as vt,resolve as O}from"node:path";import{createHash as St}from"node:crypto";import{spawnSync as xr}from"node:child_process";var V="git-remote-v1",Er=new Set(["github.com","gitlab.com","bitbucket.org"]);function bt(n,e){let t=e.replaceAll("\\","/").replace(/^\/+/u,"").replace(/\/+$/u,"").replace(/\.git$/iu,"").replace(/\/+/gu,"/");return!t||t==="."||t===".."||t.split("/").some(r=>!r||r==="."||r==="..")?null:(Er.has(n)&&(t=t.toLowerCase()),t)}function Cr(n){let e;try{e=new URL(n)}catch{return null}if(!["https:","http:","ssh:","git:"].includes(e.protocol))return null;let t=e.hostname.trim().toLowerCase();if(!t)return null;let r=e.protocol==="https:"&&e.port==="443"||e.protocol==="http:"&&e.port==="80"||e.protocol==="ssh:"&&e.port==="22",s=e.port&&!r?`${t}:${e.port}`:t,i=bt(t,e.pathname);return i?`${s}/${i}`:null}function vr(n){let e=n.match(/^(?:[^@\s/:]+@)?([^:/\s]+):(.+)$/u);if(!e)return null;let t=e[1]?.trim().toLowerCase();if(!t||t.length===1)return null;let r=bt(t,e[2]??"");return r?`${t}/${r}`:null}function ht(n){let e=n.trim();return e?e.includes("://")?Cr(e):vr(e):null}function wr(n){return St("sha256").update(`${V}:${n}`).digest("hex")}function xt(n){return St("sha256").update(`toolnet-project:${V}:${n}`).digest("hex").slice(0,16)}function Pr(n){return n.split("/").filter(Boolean).at(-1)?.trim()||null}function Ye(n,e){let t=xr("git",["-C",n,...e],{encoding:"utf8",windowsHide:!0,stdio:["ignore","pipe","ignore"]});return t.error||t.status!==0?null:t.stdout?.trim()||null}function yt(n,e){let t=Pr(n);return t?{scheme:V,canonicalRemote:n,fingerprint:wr(n),repositoryName:t,source:e}:null}function Et(n){let e=Ye(n,["remote","get-url","origin"]);if(e){let s=ht(e);if(s)return yt(s,"origin")}let t=Ye(n,["remote"]);if(!t)return null;let r=new Set;for(let s of t.split(/\r?\n/u).map(i=>i.trim()).filter(Boolean)){let i=Ye(n,["remote","get-url",s]);if(!i)continue;let o=ht(i);o&&r.add(o)}return r.size!==1?null:yt([...r][0],"unique-remote")}var wt=".toolnet",Nr="project.json";function Lr(n){return Rr("sha256").update(n).digest("hex").slice(0,16)}function F(n){return q(n,wt,Nr)}function Pt(n){return pe(F(n))}function Ct(n,e){let t=O(n),r=vt(t).root;for(;;){if(Pt(t))return t;if(t===r||e&&t===O(e))break;let s=me(t);if(s===t)break;t=s}return null}function Ve(n){let e=O(n),t=vt(e).root,r=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"];for(;;){if(r.some(i=>pe(q(e,i))))return e;if(e===t)break;let s=me(e);if(s===e)break;e=s}return O(n)}function de(n){let e;try{e=JSON.parse(kr(n,"utf8"))}catch(s){throw new Error(`Invalid ToolNet project manifest: ${n}: ${s instanceof Error?s.message:String(s)}`)}if(!e||typeof e!="object")throw new Error(`Invalid ToolNet project manifest: ${n}`);let t=e;if(typeof t.id!="string"||!t.id.trim())throw new Error(`ToolNet project manifest is missing id: ${n}`);if(typeof t.name!="string"||!t.name.trim())throw new Error(`ToolNet project manifest is missing name: ${n}`);let r=new Date().toISOString();return{version:1,id:t.id,name:t.name,remote:typeof t.remote=="string"&&t.remote.trim()?t.remote:t.name,rootPath:typeof t.rootPath=="string"?t.rootPath:me(me(n)),createdAt:typeof t.createdAt=="string"?t.createdAt:r,updatedAt:typeof t.updatedAt=="string"?t.updatedAt:r,graphVersion:typeof t.graphVersion=="number"?t.graphVersion:0,memoryVersion:typeof t.memoryVersion=="number"?t.memoryVersion:0,metadata:t.metadata&&typeof t.metadata=="object"?t.metadata:void 0}}function ue(n,e){let t=q(n,wt);Tr(t,{recursive:!0});let r=F(n),s=`${r}.tmp-${process.pid}`;Ar(s,JSON.stringify(e,null,2)+`
3
+ `,{encoding:"utf8",mode:384}),Ir(s,r)}function L(n,e){return{id:n.id,name:n.name,remote:n.remote,rootPath:e,createdAt:n.createdAt,updatedAt:n.updatedAt,graphVersion:n.graphVersion,memoryVersion:n.memoryVersion,metadata:n.metadata}}function qe(n){return{version:1,scheme:V,canonicalRemote:n.canonicalRemote,fingerprint:n.fingerprint,repositoryName:n.repositoryName}}function Or(n){let e=n.metadata?.toolnetIdentity;if(!e||typeof e!="object"||Array.isArray(e))return null;let t=e;return typeof t.fingerprint=="string"?t.fingerprint:null}var fe=class{adopt(e,t){let r=Ve(O(e));if(!t.id.trim())throw new Error("PROJECT_ADOPTION_INVALID_ID");if(!t.name.trim())throw new Error("PROJECT_ADOPTION_INVALID_NAME");if(!t.remote.trim())throw new Error("PROJECT_ADOPTION_INVALID_REMOTE");if(Pt(r)){let a=de(F(r));if(a.id!==t.id)throw new Error(["PROJECT_IDENTITY_ALREADY_EXISTS",`existing=${a.id}`,`requested=${t.id}`].join(" "));return L(a,r)}let s=new Date().toISOString(),i={...t.metadata};t.gitIdentity&&(i.toolnetIdentity=qe(t.gitIdentity));let o={version:1,id:t.id.trim(),name:t.name.trim(),remote:t.remote.trim(),rootPath:r,createdAt:t.createdAt??s,updatedAt:s,graphVersion:t.graphVersion??0,memoryVersion:t.memoryVersion??0,metadata:Object.keys(i).length?i:void 0};return ue(r,o),L(o,r)}recordGitIdentity(e,t,r={}){let s=this.requireExisting(e),i=F(s.rootPath),o=de(i),a=Or(o);if(a&&a!==t.fingerprint&&!r.allowRebind)throw new Error(["PROJECT_GIT_REMOTE_CHANGED",`existing=${a}`,`current=${t.fingerprint}`,"Use explicit rebind only when this repository identity change is intentional."].join(" "));let d=o.metadata?.toolnetIdentity;return d&&typeof d=="object"&&!Array.isArray(d)&&d.fingerprint===t.fingerprint||(o.metadata={...o.metadata,toolnetIdentity:qe(t)},o.updatedAt=new Date().toISOString(),ue(s.rootPath,o)),L(o,s.rootPath)}findExisting(e=process.cwd()){let t=O(e),r=Ve(t),i=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(d=>pe(q(r,d))),o=Ct(t,i?r:void 0);if(!o)return null;let a=de(F(o));return L(a,o)}requireExisting(e=process.cwd()){let t=this.findExisting(e);if(!t)throw new Error("PROJECT_NOT_INITIALIZED");return t}detect(e=process.cwd()){let t=O(e),r=Ve(t),i=[".git","package.json","pyproject.toml","Cargo.toml","go.mod","composer.json"].some(m=>pe(q(r,m))),o=Ct(t,i?r:void 0);if(o){let m=F(o),u=de(m);return u.rootPath!==o&&(u.rootPath=o,u.updatedAt=new Date().toISOString(),ue(o,u)),L(u,o)}let a=new Date().toISOString(),d=Mr(r),c=Et(r),l={version:1,id:c?xt(c.canonicalRemote):Lr(r),name:d,remote:c?.repositoryName??d,rootPath:r,createdAt:a,updatedAt:a,graphVersion:0,memoryVersion:0,metadata:c?{toolnetIdentity:qe(c)}:void 0};return ue(r,l),L(l,r)}};var _r=[{type:"openai_key",regex:/\bsk-[A-Za-z0-9_-]{20,}\b/g,confidence:"exact"},{type:"huggingface_token",regex:/\bhf_[A-Za-z0-9]{20,}\b/g,confidence:"exact"},{type:"hf_s3_access_key",regex:/\bHFAK[A-Za-z0-9]{8,}\b/g,confidence:"exact"},{type:"aws_access_key",regex:/\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/g,confidence:"exact"},{type:"github_token",regex:/\b(?:gh[pousr]_[A-Za-z0-9]{30,255}|github_pat_[A-Za-z0-9_]{40,255})\b/g,confidence:"exact"},{type:"stripe_secret_key",regex:/\b(?:sk|rk)_(?:live|test)_[A-Za-z0-9]{16,}\b/g,confidence:"exact"},{type:"google_api_key",regex:/\bAIza[A-Za-z0-9_-]{30,}\b/g,confidence:"exact"},{type:"slack_token",regex:/\bxox[baprs]-[A-Za-z0-9-]{16,}\b/g,confidence:"exact"},{type:"npm_token",regex:/\bnpm_[A-Za-z0-9]{20,}\b/g,confidence:"exact"},{type:"bearer_token",regex:/\bBearer\s+[A-Za-z0-9._~+/=-]{16,}\b/gi,confidence:"high"},{type:"jwt",regex:/\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/g,confidence:"exact"},{type:"private_key",regex:/-----BEGIN (?:RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----[\s\S]*?-----END (?:RSA |EC |DSA |OPENSSH )?PRIVATE KEY-----/g,confidence:"exact"},{type:"password_assignment",regex:/\b(?:password|passwd|pwd)\s*[:=]\s*["']?[^"' \t\r\n]{6,}["']?/gi,confidence:"high"},{type:"secret_assignment",regex:/\b(?:secret|api[_-]?key|access[_-]?token|refresh[_-]?token|client[_-]?secret|private[_-]?key)\s*[:=]\s*["']?[^"' \t\r\n]{8,}["']?/gi,confidence:"high"},{type:"cookie",regex:/\b(?:cookie|set-cookie)\s*[:=]\s*[^;\n]{8,}/gi,confidence:"high"},{type:"url_credentials",regex:/\bhttps?:\/\/[^:/@\s]+:[^/@\s]{4,}@[^/\s]+/gi,confidence:"high"}],jr=new Set(["example","example-key","example-token","changeme","change-me","password","secret","your-api-key","your-token","<token>","<secret>","<password>","[redacted]"]);function Rt(n){return n.normalize("NFKC").trim().toLowerCase()}function Dr(n){if(n.length===0)return 0;let e=new Map;for(let r of n)e.set(r,(e.get(r)??0)+1);let t=0;for(let r of e.values()){let s=r/n.length;t-=s*Math.log2(s)}return t}function Fr(n){return/^[a-f0-9]{32}$/iu.test(n)||/^[a-f0-9]{40}$/iu.test(n)||/^[a-f0-9]{64}$/iu.test(n)}function $r(n,e,t){let r=n.slice(Math.max(0,e-48),e),s=n.slice(t,Math.min(n.length,t+16));return/\b(?:token|secret|key|credential|authorization|password|passwd|apikey|api_key|access[_-]?key)\b/iu.test(`${r} ${s}`)}function Gr(n,e){return n.start<e.end&&e.start<n.end}function Tt(n){return n.sort((e,t)=>e.start!==t.start?e.start-t.start:t.end-t.start-(e.end-e.start))}var ge=class{allowValues=new Set;enableEntropyHeuristic;constructor(e={}){for(let t of e.allowValues??[]){let r=Rt(t);r&&this.allowValues.add(r)}this.enableEntropyHeuristic=e.enableEntropyHeuristic??!0}scan(e){let t=[];for(let i of _r){let o=new RegExp(i.regex.source,i.regex.flags);for(let a of e.matchAll(o))a.index===void 0||!a[0]||this.allowed(a[0])||t.push({type:i.type,value:a[0],start:a.index,end:a.index+a[0].length,confidence:i.confidence})}this.enableEntropyHeuristic&&t.push(...this.entropyMatches(e));let r=Tt(t),s=[];for(let i of r)s.some(o=>Gr(o,i))||s.push(i);return Tt(s)}hasSecrets(e){return this.scan(e).length>0}allowed(e){let t=Rt(e);return jr.has(t)?!0:this.allowValues.has(t)}entropyMatches(e){let t=[],r=/[A-Za-z0-9_+/=-]{32,160}/g;for(let s of e.matchAll(r)){if(s.index===void 0||!s[0])continue;let i=s[0];this.allowed(i)||Fr(i)||!/[A-Za-z]/u.test(i)||!/[0-9]/u.test(i)||$r(e,s.index,s.index+i.length)&&(Dr(i)<3.7||t.push({type:"high_entropy_secret",value:i,start:s.index,end:s.index+i.length,confidence:"heuristic"}))}return t}};var $=class{scanner;constructor(e={}){this.scanner=new ge(e)}sanitize(e){let t=this.scanner.scan(e);if(t.length===0)return{text:e,redacted:0,secretTypes:[]};let r=e,s=[...t].sort((o,a)=>a.start-o.start),i=new Set;for(let o of s)i.add(o.type),r=r.slice(0,o.start)+`[REDACTED:${o.type}]`+r.slice(o.end);return{text:r,redacted:t.length,secretTypes:[...i].sort()}}sanitizeValue(e){if(typeof e=="string")return this.sanitize(e).text;if(Array.isArray(e))return e.map(t=>this.sanitizeValue(t));if(e&&typeof e=="object"){let t={};for(let[r,s]of Object.entries(e)){let i=r.normalize("NFKC").toLowerCase().replace(/[^a-z0-9]+/g,"");if(i.includes("password")||i.includes("passwd")||i==="pwd"||i.includes("secret")||i.includes("token")||i.includes("cookie")||i.includes("authorization")||i.includes("apikey")||i.includes("accesskey")||i.includes("privatekey")||i.includes("clientsecret")||i.includes("credential")){t[r]="[REDACTED]";continue}t[r]=this.sanitizeValue(s)}return t}return e}};var yo=new $;import{lstat as Ur,readdir as Hr}from"node:fs/promises";import{extname as Wr,join as Yr,relative as Vr,resolve as qr}from"node:path";import{basename as Kr}from"node:path";var zr=new Set([".env",".env.local",".env.production",".env.development","id_rsa","id_ed25519","credentials","credentials.json","service-account.json"]),Br=new Set([".pem",".key",".p12",".pfx"]);function kt(n){let e=Kr(n).toLowerCase();if(zr.has(e))return!0;for(let t of Br)if(e.endsWith(t))return!0;return!1}var Jr=2*1024*1024,Qr=new Set([".ts",".tsx",".js",".jsx",".mjs",".cjs",".mts",".cts"]),Zr=new Set(["node_modules",".git",".toolnet",".toolnet-memory","dist","build","out","coverage",".next",".nuxt",".cache",".parcel-cache",".turbo","vendor","target"]);function It(n,e){if(!Number.isSafeInteger(n)||n<1)throw new Error(`${e} must be a positive integer`);return n}function At(n){if(n?.aborted)throw new Error("Repository scan aborted")}function Xr(n){let e=n.trim().toLowerCase();if(!e)throw new Error("Repository scan extension cannot be empty");return e.startsWith(".")?e:`.${e}`}function en(n){let e=n.toLowerCase();return/\.min\.(?:js|mjs|cjs)$/u.test(e)||/\.bundle\.(?:js|mjs|cjs)$/u.test(e)}function tn(n){return n.replaceAll("\\","/")}async function Mt(n,e={}){let t=qr(n),r=e.maxFileBytes??Jr;if(!Number.isSafeInteger(r)||r<1)throw new Error("maxFileBytes must be a positive integer");let s=e.maxFiles??Number.MAX_SAFE_INTEGER;It(s,"maxFiles");let i=e.progressInterval??1e3;It(i,"progressInterval");let o=new Set(e.extensions?[...e.extensions].map(Xr):Qr),a=new Set(Zr);for(let m of e.extraIgnoredDirectories??[]){let u=m.trim();u&&a.add(u)}let d=[],c={directories:0,entries:0,accepted:0,skippedIgnoredDirectories:0,skippedSymlinks:0,skippedGenerated:0,skippedSensitive:0,skippedOversized:0,skippedUnreadable:0},l=[t];for(;l.length>0;){At(e.signal);let m=l.pop(),u;try{u=await Hr(m,{withFileTypes:!0})}catch{c.skippedUnreadable+=1;continue}c.directories+=1;for(let f of u){At(e.signal),c.entries+=1,c.entries%i===0&&e.onProgress?.({directories:c.directories,entries:c.entries,accepted:c.accepted});let g=Yr(m,f.name);if(f.isSymbolicLink()){c.skippedSymlinks+=1;continue}if(f.isDirectory()){if(a.has(f.name)){c.skippedIgnoredDirectories+=1;continue}l.push(g);continue}if(!f.isFile())continue;let h=Wr(f.name).toLowerCase();if(!o.has(h))continue;if(en(f.name)){c.skippedGenerated+=1;continue}if(kt(g)){c.skippedSensitive+=1;continue}let y;try{y=await Ur(g)}catch{c.skippedUnreadable+=1;continue}if(y.isSymbolicLink()){c.skippedSymlinks+=1;continue}if(y.isFile()){if(y.size>r){c.skippedOversized+=1;continue}if(d.length>=s)throw new Error(`Repository scan file limit exceeded: ${s}`);d.push(tn(Vr(t,g))),c.accepted+=1}}}return d.sort(),e.onProgress?.({directories:c.directories,entries:c.entries,accepted:c.accepted}),{files:d,stats:c}}import{createHash as rn}from"node:crypto";import{dirname as nn,extname as sn,normalize as on,posix as an}from"node:path";var he=class{symbols=new Map;edges=new Map;addSymbol(e){this.symbols.set(e.id,e)}addEdge(e){this.edges.set(e.id,e)}getSymbol(e){return this.symbols.get(e)}findByName(e,t){return[...this.symbols.values()].filter(r=>r.projectId===e&&(r.name===t||r.qualifiedName===t))}allSymbols(e){return[...this.symbols.values()].filter(t=>t.projectId===e)}allEdges(e){return[...this.edges.values()].filter(t=>t.projectId===e)}import(e,t){for(let r of e)this.addSymbol(r);for(let r of t)this.addEdge(r)}clearProject(e){for(let[t,r]of this.symbols)r.projectId===e&&this.symbols.delete(t);for(let[t,r]of this.edges)r.projectId===e&&this.edges.delete(t)}};function cn(n,e,t,r){return rn("sha256").update(`${n}:${e}:${t}:${r}`).digest("hex").slice(0,24)}function k(n){return on(n).replaceAll("\\","/")}function ln(n,e,t){if(!e.startsWith("."))return;let r=k(an.join(nn(n),e)),s=sn(r),i;if(s===".js"){let o=r.slice(0,-3);i=[`${o}.ts`,`${o}.tsx`,`${o}.js`,`${o}.jsx`]}else if(s===".mjs"){let o=r.slice(0,-4);i=[`${o}.mts`,`${o}.ts`,`${o}.mjs`]}else if(s===".cjs"){let o=r.slice(0,-4);i=[`${o}.cts`,`${o}.ts`,`${o}.cjs`]}else s?i=[r]:i=[`${r}.ts`,`${r}.tsx`,`${r}.mts`,`${r}.cts`,`${r}.js`,`${r}.jsx`,`${r}.mjs`,`${r}.cjs`,`${r}/index.ts`,`${r}/index.tsx`,`${r}/index.mts`,`${r}/index.cts`,`${r}/index.js`,`${r}/index.jsx`,`${r}/index.mjs`,`${r}/index.cjs`];return i.find(o=>t.has(k(o)))}function Je(n,e,t,r){let s=r?.(n,e,t);if(s){let i=k(s);if(t.has(i))return i}return ln(n,e,t)}var J=class{build(e,t,r={}){let s=new he;for(let a of r.seedSymbols??[])s.addSymbol(a);for(let a of t)for(let d of a.symbols)s.addSymbol(d);let i=new Map;for(let a of s.allSymbols(e))a.type==="file"&&i.set(k(a.filePath),a);let o=new Set(i.keys());for(let a of t){let d=k(a.filePath),c=i.get(d);if(c){for(let l of a.symbols)l.id!==c.id&&s.addEdge(this.edge(e,c.id,"DEFINES",l.id));for(let l of a.imports){let m=Je(d,l.source,o,r.resolveModule);if(!m)continue;let u=i.get(m);u&&s.addEdge(this.edge(e,c.id,"IMPORTS",u.id,{source:l.source,resolvedFile:m}))}for(let l of a.heritage){let m=s.findByName(e,l.targetName).filter(u=>u.type==="class"||u.type==="interface");for(let u of m)s.addEdge(this.edge(e,l.fromId,l.type,u.id))}for(let l of a.calls){if(!l.callerId)continue;let m=this.resolveCallTargets(e,a,l.calleeName,l.qualifier,s,o,r.resolveModule);for(let u of m)u.id!==l.callerId&&s.addEdge(this.edge(e,l.callerId,"CALLS",u.id,{line:l.line,qualifier:l.qualifier}))}}}return s}resolveCallTargets(e,t,r,s,i,o,a){let d=k(t.filePath),c=i.findByName(e,r).filter(l=>k(l.filePath)===d);if(!s&&c.length)return c;if(s)for(let l of t.imports){if(!l.bindings.find(f=>f.kind==="namespace"&&f.localName===s))continue;let u=Je(d,l.source,o,a);if(u)return i.findByName(e,r).filter(f=>k(f.filePath)===u)}for(let l of t.imports){let m=l.bindings.find(h=>h.localName===r);if(!m)continue;let u=Je(d,l.source,o,a);if(!u)continue;let f=m.importedName==="default"?r:m.importedName,g=i.findByName(e,f).filter(h=>k(h.filePath)===u);if(g.length)return g}return i.findByName(e,r).filter(l=>l.type==="function"||l.type==="method")}edge(e,t,r,s,i){return{id:cn(e,t,r,s),projectId:e,from:t,to:s,type:r,metadata:i}}};import{createHash as fn}from"node:crypto";import{readFile as gn}from"node:fs/promises";import{join as hn}from"node:path";import{extname as dn}from"node:path";var A={supported:!0,structural:!0,lexicalSearch:!0,engine:"typescript-compiler-api",lexicalEngine:"file-chunk-fts5-bm25"},Q={supported:!1,structural:!1,lexicalSearch:!0,engine:"unsupported",lexicalEngine:"file-chunk-fts5-bm25"},G=[{language:"typescript",extensions:[".ts"],...A},{language:"tsx",extensions:[".tsx"],...A},{language:"javascript",extensions:[".js"],...A},{language:"jsx",extensions:[".jsx"],...A},{language:"mts",extensions:[".mts"],...A},{language:"cts",extensions:[".cts"],...A},{language:"mjs",extensions:[".mjs"],...A},{language:"cjs",extensions:[".cjs"],...A},{language:"python",extensions:[".py"],...Q,lspServer:"pyright-langserver"},{language:"go",extensions:[".go"],...Q,lspServer:"gopls"},{language:"rust",extensions:[".rs"],...Q,lspServer:"rust-analyzer"},{language:"c",extensions:[".c",".h"],...Q,lspServer:"clangd"},{language:"cpp",extensions:[".cc",".cpp",".cxx",".hpp",".hh"],...Q,lspServer:"clangd"}];function Qe(n){let e=dn(n).toLowerCase();return G.find(t=>t.extensions.includes(e))}function Nt(){return[...new Set(G.filter(n=>n.lexicalSearch).flatMap(n=>n.extensions))]}import{readFile as un}from"node:fs/promises";import{join as pn}from"node:path";import{createHash as mn}from"node:crypto";import*as E from"typescript";function Lt(n,e){return mn("sha256").update(`${n}:${e}`).digest("hex").slice(0,24)}function Ze(n,e){return n.getLineAndCharacterOfPosition(e.getStart(n)).line+1}async function Xe(n,e,t){let r=pn(e,t),s=await un(r,"utf8"),i=E.createSourceFile(t,s,E.ScriptTarget.Latest,!0,t.endsWith(".tsx")||t.endsWith(".jsx")?E.ScriptKind.TSX:E.ScriptKind.TS),o=[],a=[],d=[],c=[],l={id:Lt(n,`file:${t}`),projectId:n,name:t,qualifiedName:t,type:"file",filePath:t,startLine:1,endLine:i.getLineAndCharacterOfPosition(i.end).line+1};o.push(l);let m=[],u=[];function f(p,S,x,C){let w=Lt(n,`${t}:${x}:${C??S}:${p.pos}`);return o.push({id:w,projectId:n,name:S,qualifiedName:C??S,type:x,filePath:t,startLine:Ze(i,p),endLine:i.getLineAndCharacterOfPosition(p.end).line+1}),w}function g(p){if(!E.isStringLiteral(p.moduleSpecifier))return;let S=[],x=p.importClause;if(x?.name&&S.push({localName:x.name.text,importedName:"default",kind:"default"}),x?.namedBindings&&E.isNamedImports(x.namedBindings))for(let C of x.namedBindings.elements)S.push({localName:C.name.text,importedName:C.propertyName?.text??C.name.text,kind:"named"});x?.namedBindings&&E.isNamespaceImport(x.namedBindings)&&S.push({localName:x.namedBindings.name.text,importedName:"*",kind:"namespace"}),a.push({source:p.moduleSpecifier.text,bindings:S})}function h(p,S){for(let x of p.heritageClauses??[])for(let C of x.types){let w=C.expression.getText(i).split(".").at(-1)??"";w&&c.push({fromId:S,targetName:w,type:x.token===E.SyntaxKind.ExtendsKeyword?"INHERITS":"IMPLEMENTS"})}}function y(p){let S=!1,x=!1;if(E.isImportDeclaration(p)&&g(p),E.isInterfaceDeclaration(p)&&f(p,p.name.text,"interface",p.name.text),E.isClassDeclaration(p)&&p.name){let C=f(p,p.name.text,"class",p.name.text);h(p,C),u.push({id:C,name:p.name.text}),x=!0}if(E.isFunctionDeclaration(p)&&p.name){let C=f(p,p.name.text,"function",p.name.text);m.push(C),S=!0}if(E.isMethodDeclaration(p)&&p.name){let C=p.name.getText(i),w=u.at(-1)?.name,I=f(p,C,"method",w?`${w}.${C}`:C);m.push(I),S=!0}if(E.isCallExpression(p)&&(E.isIdentifier(p.expression)&&d.push({callerId:m.at(-1),calleeName:p.expression.text,line:Ze(i,p)}),E.isPropertyAccessExpression(p.expression))){let C=p.expression;d.push({callerId:m.at(-1),qualifier:C.expression.getText(i),calleeName:C.name.text,line:Ze(i,p)})}E.forEachChild(p,y),S&&m.pop(),x&&u.pop()}return y(i),{filePath:t,symbols:o,imports:a,calls:d,heritage:c}}function yn(n,e){return fn("sha256").update(`${n}:${e}`).digest("hex").slice(0,24)}async function Sn(n,e,t){let r=Qe(t);if(!r||!r.lexicalSearch)throw new Error(`Unsupported code file: ${t}`);let s=await gn(hn(e,t),"utf8"),i=Math.max(1,s.split(/\r?\n/u).length),o={id:yn(n,`file:${t}`),projectId:n,name:t,qualifiedName:t,type:"file",filePath:t,startLine:1,endLine:i,metadata:{language:r.language,structuralParser:!1,lexicalSearch:!0,lexicalEngine:r.lexicalEngine,...r.lspServer?{lspServer:r.lspServer}:{}}};return{filePath:t,symbols:[o],imports:[],calls:[],heritage:[]}}async function Ot(n,e,t){let r=Qe(t);if(!r)throw new Error(`No parser capability for: ${t}`);if(r.supported)return Xe(n,e,t);if(r.lexicalSearch)return Sn(n,e,t);throw new Error(`Structural and lexical parsing unsupported for: ${t}`)}function bn(n){let e=n??4;if(!Number.isSafeInteger(e)||e<1||e>64)throw new Error("concurrency must be an integer between 1 and 64");return e}function xn(n){if(n?.aborted)throw new Error("Operation aborted")}async function ye(n,e,t={}){if(n.length===0)return[];let r=Math.min(bn(t.concurrency),n.length),s=new Array(n.length),i=0,o=0,a=async()=>{for(;;){xn(t.signal);let c=i;if(i+=1,c>=n.length)return;s[c]=await e(n[c],c),o+=1,t.onProgress?.({completed:o,total:n.length,index:c})}},d=Array.from({length:r},()=>a());return await Promise.all(d),s}var Se=class{async index(e,t,r={}){let s={...r.scan,extensions:r.scan?.extensions??Nt()};r.signal&&(s.signal=r.signal);let i=await Mt(t,s),o=i.files;r.onProgress?.({phase:"scan",current:o.length,total:o.length});let a=r.maxWarnings??20;if(!Number.isSafeInteger(a)||a<0)throw new Error("maxWarnings must be a non-negative integer");let d=0,c=await ye(o,async u=>{try{return await Ot(e,t,u)}catch(f){return d+=1,d<=a&&console.warn(`[indexer] skipped ${u}:`,f instanceof Error?f.message:f),null}},{concurrency:r.concurrency??4,signal:r.signal,onProgress:({completed:u,total:f,index:g})=>{r.onProgress?.({phase:"parse",current:u,total:f,file:o[g]})}});d>a&&console.warn(`[indexer] ${d-a} additional parse warnings suppressed`);let l=c.filter(u=>u!==null),m=new J().build(e,l);return{files:l.length,symbols:m.allSymbols(e).length,edges:m.allEdges(e).length,parseFailures:d,scan:i.stats,graph:m}}};import{execFile as En}from"node:child_process";import{promisify as Cn}from"node:util";var ji=Cn(En);var Z=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"graph","current.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e),"application/json")}};import*as M from"typescript";import{createHash as wn}from"node:crypto";import{readFile as Pn}from"node:fs/promises";import{join as Rn}from"node:path";function jt(n){return wn("sha256").update(n).digest("hex")}var be=class{sanitizer=new $;async build(e,t,r,s={}){let i=s.maxLines??80,o=s.overlapLines??10,a=new Map;for(let c of r){let l=a.get(c.filePath)??[];l.push(c),a.set(c.filePath,l)}let d=[];for(let[c,l]of a){let m;try{m=await Pn(Rn(t,c),"utf8")}catch{continue}let u=m.split(/\r?\n/),f=l.filter(g=>g.type!=="file"&&typeof g.startLine=="number"&&typeof g.endLine=="number");if(f.length>0)for(let g of f)d.push(...this.chunkRange({projectId:e,filePath:c,lines:u,symbol:g,startLine:g.startLine,endLine:g.endLine,maxLines:i,overlapLines:o}));else d.push(...this.chunkRange({projectId:e,filePath:c,lines:u,startLine:1,endLine:Math.max(1,u.length),maxLines:i,overlapLines:o}))}return d}chunkRange(e){let{projectId:t,filePath:r,lines:s,symbol:i,maxLines:o}=e,a=Math.min(e.overlapLines,Math.max(0,o-1)),d=Math.max(1,e.startLine),c=Math.min(s.length,Math.max(d,e.endLine)),l=[],m=d,u=0;for(;m<=c;){let f=Math.min(c,m+o-1),g=s.slice(m-1,f).join(`
4
+ `).trim();if(g){let h=this.sanitizer.sanitize(g).text,y=jt(h),p=[t,r,i?.id??"file",String(u),y].join(":");l.push({id:jt(p).slice(0,32),projectId:t,filePath:r,symbolId:i?.id,symbolName:i?.qualifiedName??i?.name,symbolType:i?.type,startLine:m,endLine:f,content:h,contentHash:y})}if(f>=c)break;m=f-a+1,u++}return l}};var xe=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"code","chunks","current.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e),"application/json")}};import{DatabaseSync as Tn}from"node:sqlite";var Ee=class{constructor(e){this.projectId=e;this.db.exec("CREATE VIRTUAL TABLE code_fts USING fts5(id UNINDEXED, projectId UNINDEXED, filePath, symbol, content, tokenize='unicode61')")}projectId;db=new Tn(":memory:");chunks=new Map;get size(){return this.chunks.size}build(e){if(this.db.exec("DELETE FROM code_fts"),this.chunks.clear(),e.length===0)return 0;let t=this.db.prepare("INSERT INTO code_fts (id, projectId, filePath, symbol, content) VALUES (?, ?, ?, ?, ?)");this.db.exec("BEGIN");try{for(let r of e)this.chunks.set(r.id,r),t.run(r.id,this.projectId,r.filePath,r.symbolName??"",r.content);this.db.exec("COMMIT")}catch(r){throw this.db.exec("ROLLBACK"),r}return e.length}search(e,t=8){let r=this.toMatchQuery(e);if(!r)return[];let s=this.db.prepare("SELECT id, bm25(code_fts) AS rank FROM code_fts WHERE code_fts MATCH ? ORDER BY rank LIMIT ?").all(r,Math.max(t*4,20)),i=[];for(let o of s){let a=this.chunks.get(o.id);if(!a)continue;let d=1/(1+Math.abs(o.rank));i.push({chunk:a,score:d})}return i.slice(0,t)}toMatchQuery(e){let t=e.toLowerCase().normalize("NFKC").split(/[^\p{L}\p{N}]+/u).map(r=>r.trim()).filter(r=>r.length>0);return t.length===0?null:t.length===1?`"${t[0]}"`:`(${t.map(r=>`"${r}"`).join(" OR ")})`}close(){this.db.close()}};function Dt(n,e){let t=n.toLowerCase().replaceAll("\\","/"),r=e.toLowerCase(),s=/\b(test|tests|spec|fixture|mock)\b/.test(r);return(t.startsWith("tests/")||t.includes("/tests/")||t.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(t))&&!s?.35:t.includes("/generated/")||t.includes(".generated.")?.25:1}var X="sqlite-fts5-bm25",Ce="lexical",kn="Local Code Search \u2014 SQLite FTS5/BM25",Ft={engine:X,mode:Ce,displayName:kn,semantic:!1,embedding:!1,vectorDatabase:!1,queryExpansion:!1,synonymExpansion:!1,networkRequired:!1,indexBackend:"sqlite-fts5",ranking:"bm25",compatibilityAliases:["SemanticCodeEngine","semantic_code_search","toolnet-memory semantic","semantic-index"]};var ve=class{constructor(e){this.options=e;this.fts=new Ee(e.projectId)}options;fts;get searchContract(){return Ft}async initialize(e){let{projectId:t,rootPath:r,storage:s,graph:i}=this.options,o=new xe(s),a=await new be().build(t,r,i.allSymbols(t)),d=await o.load(t),c=!d||d.chunks.length!==a.length||d.chunks.some(m=>!a.some(u=>u.id===m.id));c&&await o.save({version:1,projectId:t,updatedAt:new Date().toISOString(),chunks:a}),e?.({current:a.length,total:a.length,phase:"index",detail:X});let l=this.fts.build(a);return{chunks:a.length,documentsIndexed:l,indexSize:this.fts.size,engine:X,mode:Ce,embedding:!1,vectorDatabase:!1,savedChunks:c,vectorsLoaded:0,vectorsIndexed:l,vectorsRemoved:0,totalVectors:this.fts.size,savedVectors:c}}async search(e,t=8){let r=this.fts.search(e,t),s=[];for(let i of r){let o=i.chunk,a=Dt(o.filePath,e),d=i.score,c=0,l=d*a;s.push({chunk:o,score:l,lexicalScore:d,vectorScore:c,engine:X,mode:Ce})}return s}size(){return this.fts.size}};import{createHash as In}from"node:crypto";import{dirname as An,relative as ee,resolve as te}from"node:path";import*as v from"typescript";function _(n){return n.replaceAll("\\","/").replace(/^\.\//,"")}function $t(n){return In("sha256").update(n).digest("hex").slice(0,24)}function re(n,e){return n.getLineAndCharacterOfPosition(e.getStart(n)).line+1}function Mn(n){let e=n;if(e.name&&v.isIdentifier(e.name))return e.name.text}var we=class{constructor(e){this.graph=e}graph;async resolveProject(e,t,r){let s=this.loadConfig(t),i=v.createProgram({rootNames:s.fileNames,options:s.options}),o=i.getTypeChecker(),a=[],d=i.getSourceFiles().filter(l=>{if(l.isDeclarationFile)return!1;let m=te(l.fileName),u=_(ee(t,m));return!(u.startsWith("../")||u.includes("node_modules/"))}),c=0;for(let l of d){let m=te(l.fileName),u=_(ee(t,m)),f=g=>{if(v.isCallExpression(g)){let h=this.resolveCall({projectId:e,rootPath:t,source:l,node:g,checker:o});h&&a.push(h)}v.isClassDeclaration(g)&&a.push(...this.resolveHeritage({projectId:e,rootPath:t,source:l,node:g,checker:o})),v.forEachChild(g,f)};f(l),c++,r?.({current:c,total:d.length,detail:u})}return{version:1,projectId:e,updatedAt:new Date().toISOString(),total:a.length,exact:a.filter(l=>l.confidence==="exact").length,high:a.filter(l=>l.confidence==="high").length,fallback:a.filter(l=>l.confidence==="fallback").length,resolutions:a}}resolveCall(e){let{projectId:t,rootPath:r,source:s,node:i,checker:o}=e,a;if(a=o.getResolvedSignature(i)?.declaration,!a){let c=o.getSymbolAtLocation(i.expression);a=c?.valueDeclaration??c?.declarations?.[0]}return a?this.fromDeclaration({projectId:t,rootPath:r,source:s,sourceNode:i,declaration:a,expression:i.expression.getText(s),kind:"CALL"}):this.fallbackCall(t,r,s,i)}resolveHeritage(e){let{projectId:t,rootPath:r,source:s,node:i,checker:o}=e,a=[];for(let d of i.heritageClauses??[])for(let c of d.types){let l=o.getSymbolAtLocation(c.expression),m=l?.valueDeclaration??l?.declarations?.[0];if(!m)continue;let u=this.fromDeclaration({projectId:t,rootPath:r,source:s,sourceNode:c,declaration:m,expression:c.getText(s),kind:d.token===v.SyntaxKind.ExtendsKeyword?"EXTENDS":"IMPLEMENTS"});u&&a.push(u)}return a}fromDeclaration(e){let t=e.declaration.getSourceFile();if(t.isDeclarationFile)return null;let r=_(ee(e.rootPath,te(t.fileName)));if(r.startsWith("../")||r.includes("node_modules/"))return null;let s=re(t,e.declaration),i=Mn(e.declaration)??e.expression.split(".").at(-1),o=this.findGraphSymbol(e.projectId,r,s,i),a=_(ee(e.rootPath,te(e.source.fileName)));return{id:$t([e.projectId,a,re(e.source,e.sourceNode),e.kind,r,s].join(":")),projectId:e.projectId,kind:e.kind,sourceFile:a,sourceLine:re(e.source,e.sourceNode),expression:e.expression,targetFile:r,targetLine:s,targetName:i,targetQualifiedName:o?.qualifiedName,targetSymbolId:o?.id,confidence:o?"exact":"high",resolver:"typescript-checker"}}fallbackCall(e,t,r,s){let i=s.expression.getText(r).split(".").at(-1);if(!i)return null;let o=this.graph.findByName(e,i).filter(c=>c.type==="function"||c.type==="method");if(o.length!==1)return null;let a=o[0],d=_(ee(t,te(r.fileName)));return{id:$t(`${e}:${d}:${re(r,s)}:${a.id}`),projectId:e,kind:"CALL",sourceFile:d,sourceLine:re(r,s),expression:s.expression.getText(r),targetFile:a.filePath,targetLine:a.startLine,targetName:a.name,targetQualifiedName:a.qualifiedName,targetSymbolId:a.id,confidence:"fallback",resolver:"graph-fallback"}}findGraphSymbol(e,t,r,s){let o=this.graph.allSymbols(e).filter(a=>_(a.filePath)===_(t)).filter(a=>typeof a.startLine=="number"&&typeof a.endLine=="number"&&a.startLine<=r&&a.endLine>=r);if(s){let a=o.find(d=>d.name===s||(d.qualifiedName?.endsWith(`.${s}`)??!1));if(a)return a}return o.sort((a,d)=>(a.endLine??0)-(a.startLine??0)-((d.endLine??0)-(d.startLine??0)))[0]}loadConfig(e){let t=v.findConfigFile(e,v.sys.fileExists,"tsconfig.json");if(t){let s=v.readConfigFile(t,v.sys.readFile);if(s.error)throw new Error(v.flattenDiagnosticMessageText(s.error.messageText,`
5
+ `));return v.parseJsonConfigFileContent(s.config,v.sys,An(t))}return{fileNames:v.sys.readDirectory(e,[".ts",".tsx",".js",".jsx",".mts",".cts",".mjs",".cjs"],["node_modules",".git","dist","build","coverage"]),options:{allowJs:!0,checkJs:!1,target:v.ScriptTarget.ES2022,module:v.ModuleKind.NodeNext,moduleResolution:v.ModuleResolutionKind.NodeNext,skipLibCheck:!0,noEmit:!0}}}};import{createHash as Nn}from"node:crypto";import{dirname as Ln,relative as K,resolve as z}from"node:path";import*as b from"typescript";function T(n){return n.replaceAll("\\","/").replace(/^\.\//,"")}function et(...n){return Nn("sha256").update(n.join(":")).digest("hex").slice(0,24)}function ne(n,e){return n.getLineAndCharacterOfPosition(e.getStart(n)).line+1}var On=new Set(["get","post","put","patch","delete","options","head"]),_n=new Set([b.SyntaxKind.EqualsToken,b.SyntaxKind.PlusEqualsToken,b.SyntaxKind.MinusEqualsToken,b.SyntaxKind.AsteriskEqualsToken,b.SyntaxKind.SlashEqualsToken,b.SyntaxKind.PercentEqualsToken,b.SyntaxKind.QuestionQuestionEqualsToken,b.SyntaxKind.AmpersandAmpersandEqualsToken,b.SyntaxKind.BarBarEqualsToken]),Pe=class{constructor(e){this.graph=e}graph;enrich(e,t,r,s){let i={routes:0,tests:0,usesType:0,writes:0,callReferences:0,properties:0},o=this.loadConfig(t),a=b.createProgram({rootNames:o.fileNames,options:o.options}),d=a.getTypeChecker(),c=a.getSourceFiles().filter(m=>{if(m.isDeclarationFile)return!1;let u=T(K(t,z(m.fileName)));return!(u.startsWith("../")||u.includes("node_modules/"))}),l=0;for(let m of c){let u=T(K(t,z(m.fileName))),f=this.fileNode(e,u);if(!f)continue;let g=h=>{if(b.isCallExpression(h)&&this.addRoute(e,t,m,f,h,d)&&i.routes++,b.isTypeReferenceNode(h)&&this.addTypeUse(e,t,m,h,d)&&i.usesType++,b.isBinaryExpression(h)&&_n.has(h.operatorToken.kind)){let y=this.addWrite(e,t,m,h.left,d);y.added&&(i.writes++,y.propertyCreated&&i.properties++)}if((b.isPrefixUnaryExpression(h)||b.isPostfixUnaryExpression(h))&&(h.operator===b.SyntaxKind.PlusPlusToken||h.operator===b.SyntaxKind.MinusMinusToken)){let y=this.addWrite(e,t,m,h.operand,d);y.added&&(i.writes++,y.propertyCreated&&i.properties++)}b.forEachChild(h,g)};g(m),l++,s?.({current:l,total:c.length,detail:u})}return i.tests+=this.addTestEdges(e),r&&(i.callReferences+=this.addResolvedCalls(e,r)),i}addRoute(e,t,r,s,i,o){if(!b.isPropertyAccessExpression(i.expression))return!1;let a=i.expression.name.text.toLowerCase();if(!On.has(a))return!1;let d=i.arguments[0];if(!d||!(b.isStringLiteral(d)||b.isNoSubstitutionTemplateLiteral(d)))return!1;let c=d.text;if(!c.startsWith("/"))return!1;let l=ne(r,i),m=et(e,r.fileName,l,a,c);this.graph.getSymbol(m)||(this.graph.addSymbol({id:m,projectId:e,name:`${a.toUpperCase()} ${c}`,qualifiedName:`${a.toUpperCase()} ${c}`,type:"route",filePath:s.filePath,startLine:l,endLine:l}),this.graph.addEdge(this.edge(e,s.id,"DEFINES",m)));let u=i.arguments.at(-1);if(u){let f=this.resolveNodeSymbol(e,t,u,o);f&&this.graph.addEdge(this.edge(e,m,"ROUTE",f.id,{method:a.toUpperCase(),path:c}))}return!0}addTypeUse(e,t,r,s,i){let o=this.resolveNodeSymbol(e,t,s.typeName,i);if(!o)return!1;let a=this.containingSymbol(e,T(K(t,z(r.fileName))),ne(r,s));return!a||a.id===o.id?!1:(this.graph.addEdge(this.edge(e,a.id,"USES_TYPE",o.id)),!0)}addWrite(e,t,r,s,i){let o=this.resolveTsSymbol(s,i),a=o?.valueDeclaration??o?.declarations?.[0];if(!a)return{added:!1,propertyCreated:!1};let d=a.getSourceFile();if(d.isDeclarationFile)return{added:!1,propertyCreated:!1};let c=T(K(t,z(d.fileName)));if(c.startsWith("../")||c.includes("node_modules/"))return{added:!1,propertyCreated:!1};let l=ne(d,a),m=this.findSymbolAt(e,c,l),u=!1;if(!m||m.type==="file"){let h=o?.getName()??s.getText(r),y=et(e,c,"property",h,l);if(m=this.graph.getSymbol(y),!m){m={id:y,projectId:e,name:h,qualifiedName:h,type:"property",filePath:c,startLine:l,endLine:l},this.graph.addSymbol(m);let p=this.fileNode(e,c);p&&this.graph.addEdge(this.edge(e,p.id,"DEFINES",m.id)),u=!0}}if(!m)return{added:!1,propertyCreated:u};let f=T(K(t,z(r.fileName))),g=this.containingSymbol(e,f,ne(r,s));return!g||g.id===m.id?{added:!1,propertyCreated:u}:(this.graph.addEdge(this.edge(e,g.id,"WRITES",m.id,{expression:s.getText(r)})),{added:!0,propertyCreated:u})}addTestEdges(e){let t=this.graph.allEdges(e),r=0;for(let s of t){if(s.type!=="IMPORTS")continue;let i=this.graph.getSymbol(s.from),o=this.graph.getSymbol(s.to);!i||!o||!this.isTestFile(i.filePath)||this.isTestFile(o.filePath)||(this.graph.addEdge(this.edge(e,i.id,"TESTS",o.id)),r++)}return r}addResolvedCalls(e,t){let r=0;for(let s of t.resolutions){if(s.kind!=="CALL"||!s.targetSymbolId)continue;let i=this.graph.getSymbol(s.targetSymbolId);if(!i)continue;let o=this.containingSymbol(e,s.sourceFile,s.sourceLine);!o||o.id===i.id||this.graph.allEdges(e).some(d=>d.type==="CALLS"&&d.from===o.id&&d.to===i.id)||(this.graph.addEdge(this.edge(e,o.id,"CALL_REFERENCE",i.id,{expression:s.expression,confidence:s.confidence,resolver:s.resolver})),r++)}return r}resolveNodeSymbol(e,t,r,s){let i=this.resolveTsSymbol(r,s),o=i?.valueDeclaration??i?.declarations?.[0];if(!o)return;let a=o.getSourceFile();if(a.isDeclarationFile)return;let d=T(K(t,z(a.fileName)));if(!(d.startsWith("../")||d.includes("node_modules/")))return this.findSymbolAt(e,d,ne(a,o),i?.getName())}resolveTsSymbol(e,t){let r=t.getSymbolAtLocation(e);if(r&&r.flags&b.SymbolFlags.Alias)try{r=t.getAliasedSymbol(r)}catch{}return r}containingSymbol(e,t,r){return this.graph.allSymbols(e).filter(s=>T(s.filePath)===T(t)&&typeof s.startLine=="number"&&typeof s.endLine=="number"&&s.startLine<=r&&s.endLine>=r).sort((s,i)=>(s.endLine??0)-(s.startLine??0)-((i.endLine??0)-(i.startLine??0)))[0]}findSymbolAt(e,t,r,s){let i=this.graph.allSymbols(e).filter(o=>T(o.filePath)===T(t)&&typeof o.startLine=="number"&&typeof o.endLine=="number"&&o.startLine<=r&&o.endLine>=r).sort((o,a)=>(o.endLine??0)-(o.startLine??0)-((a.endLine??0)-(a.startLine??0)));if(s){let o=i.find(a=>a.name===s||(a.qualifiedName?.endsWith(`.${s}`)??!1));if(o)return o}return i[0]}fileNode(e,t){return this.graph.allSymbols(e).find(r=>r.type==="file"&&T(r.filePath)===T(t))}isTestFile(e){let t=T(e).toLowerCase();return t.startsWith("tests/")||t.includes("/tests/")||t.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(t)}edge(e,t,r,s,i){return{id:et(e,t,r,s,JSON.stringify(i??{})),projectId:e,from:t,to:s,type:r,metadata:i}}loadConfig(e){let t=b.findConfigFile(e,b.sys.fileExists,"tsconfig.json");if(t){let r=b.readConfigFile(t,b.sys.readFile);return b.parseJsonConfigFileContent(r.config,b.sys,Ln(t))}return{fileNames:b.sys.readDirectory(e,[".ts",".tsx",".js",".jsx",".mts",".cts",".mjs",".cjs"],["node_modules",".git","dist","build","coverage"]),options:{allowJs:!0,noEmit:!0,skipLibCheck:!0,target:b.ScriptTarget.ES2022,module:b.ModuleKind.NodeNext,moduleResolution:b.ModuleResolutionKind.NodeNext}}}};function jn(n){return n.replaceAll("\\","/").toLowerCase()}var B=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=[];for(let o of t){if(o.type!=="route"&&o.type!=="file")continue;let a=this.classify(o,r);a&&s.push({symbolId:o.id,filePath:o.filePath,name:o.name,kind:a.kind,score:a.score,reasons:a.reasons})}let i=new Map;for(let o of s){let a=o.kind==="route"?o.symbolId:o.filePath,d=i.get(a);(!d||o.score>d.score)&&i.set(a,o)}return[...i.values()].sort((o,a)=>a.score-o.score||o.filePath.localeCompare(a.filePath))}classify(e,t){let r=jn(e.filePath),s=[];if(e.type==="route")return{kind:"route",score:100,reasons:["HTTP route"]};let i=null,o=0;if(r.startsWith("bin/"))i="cli",o=100,s.push("bin executable");else if(/(^|\/)(cli)\.[^.]+$/.test(r))i="cli",o=90,s.push("CLI entry file");else if(/(^|\/)(main|server)\.[^.]+$/.test(r))i="main",o=90,s.push("main/server file");else if(r.includes("/runtime/")&&/(^|\/)(bootstrap|index)\.[^.]+$/.test(r))i="runtime",o=75,s.push("runtime bootstrap");else if(/(^|\/)index\.[^.]+$/.test(r)){let d=t.filter(l=>l.to===e.id&&l.type!=="DEFINES").length,c=t.filter(l=>l.from===e.id&&l.type!=="DEFINES").length;(r.startsWith("packages/")||d===0||c>0)&&(i="package",o=r.startsWith("packages/")?70:55,s.push("package/public index"))}return i?(t.filter(d=>d.to===e.id&&d.type!=="DEFINES").length===0&&(o+=10,s.push("no incoming dependency")),{kind:i,score:o,reasons:s}):null}};var Dn={CALL_REFERENCE:6,CALLS:4,IMPORTS:3,USES_TYPE:4,WRITES:5,ROUTE:4,TESTS:1,INHERITS:5,IMPLEMENTS:5,DEFINES:.25},Re=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=new Map(t.map(c=>[c.id,c])),i=new Map,o=c=>{let l=i.get(c);return l||(l={symbols:0,incoming:0,outgoing:0,weightedIncoming:0,weightedOutgoing:0,writes:0,callReferences:0,crossFile:0},i.set(c,l)),l};for(let c of t)o(c.filePath).symbols++;for(let c of r){let l=s.get(c.from),m=s.get(c.to);if(!l||!m)continue;let u=Dn[c.type]??1,f=o(l.filePath),g=o(m.filePath);f.outgoing++,g.incoming++,f.weightedOutgoing+=u,g.weightedIncoming+=u,c.type==="WRITES"&&f.writes++,c.type==="CALL_REFERENCE"&&f.callReferences++,l.filePath!==m.filePath&&(f.crossFile++,g.crossFile++)}let a=[...i.entries()].map(([c,l])=>{let m=Number((l.symbols*.4+l.weightedIncoming*1.75+l.weightedOutgoing*.65+l.crossFile*1.5+l.writes*3+l.callReferences*2).toFixed(2)),u=[];return l.incoming>=10&&u.push("high incoming dependency"),l.crossFile>=10&&u.push("high cross-file coupling"),l.callReferences>=5&&u.push("many exact call references"),l.writes>0&&u.push("contains write relationships"),l.symbols>=10&&u.push("high symbol density"),{filePath:c,score:m,symbols:l.symbols,incoming:l.incoming,outgoing:l.outgoing,reasons:u}}).filter(c=>c.score>0).sort((c,l)=>l.score-c.score||c.filePath.localeCompare(l.filePath));if(a.length===0)return[];let d=Math.max(1,Math.min(30,Math.ceil(a.length*.15)));return a.slice(0,d)}};function Fn(n){return n.replaceAll("\\","/").toLowerCase()}var $n={CALL_REFERENCE:6,CALLS:4,IMPORTS:4,USES_TYPE:4,WRITES:5,INHERITS:6,IMPLEMENTS:6,ROUTE:3,TESTS:1},Te=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=new Map(t.map(a=>[a.id,a])),i=[...new Set(t.map(a=>a.filePath))].sort(),o=new Map;for(let a of i)o.set(a,this.classifyPath(a));for(let a=0;a<3;a++){let d=!1;for(let c of i){let l=o.get(c);if(!l||l.layer!=="unknown")continue;let m=new Map;for(let f of r){let g=s.get(f.from),h=s.get(f.to);if(!g||!h||g.filePath===h.filePath)continue;let y=null;if(g.filePath===c?y=h.filePath:h.filePath===c&&(y=g.filePath),!y)continue;let p=o.get(y);if(!p||p.layer==="unknown"||p.layer==="tests")continue;let S=$n[f.type]??1;m.set(p.layer,(m.get(p.layer)??0)+S)}let u=[...m.entries()].sort((f,g)=>g[1]-f[1]||f[0].localeCompare(g[0]))[0];u&&u[1]>=4&&(o.set(c,{filePath:c,layer:u[0],confidence:.65,reasons:[`graph-neighbor vote=${u[1]}`]}),d=!0)}if(!d)break}return i.map(a=>o.get(a))}classifyPath(e){let t=Fn(e);return t.startsWith("tests/")||t.includes("/tests/")||t.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(t)?{filePath:e,layer:"tests",confidence:1,reasons:["test path"]}:t.startsWith("bin/")||t.includes("/mcp/")||t.includes("/cli/")||t.includes("/api/")||t.includes("/routes/")||t.includes("/hooks/")?{filePath:e,layer:"interface",confidence:.9,reasons:["interface boundary"]}:t.includes("/runtime/")||t.includes("/processor/")||t.includes("/retrieval/")||t.includes("/snapshot/")||t.includes("/code-intelligence/")?{filePath:e,layer:"application",confidence:.85,reasons:["application orchestration"]}:t.includes("/core/")||t.includes("/memory/")||/(^|\/)types\.[^.]+$/.test(t)?{filePath:e,layer:"domain",confidence:.9,reasons:["domain/core logic"]}:t.includes("/storage/")||t.includes("/production/")||t.includes("/security/")||t.includes("/config/")||t.includes("/utils/")?{filePath:e,layer:"infrastructure",confidence:.85,reasons:["infrastructure boundary"]}:{filePath:e,layer:"unknown",confidence:.35,reasons:[]}}};import{createHash as Gn}from"node:crypto";var Kn={CALL_REFERENCE:7,CALLS:3,IMPORTS:4,USES_TYPE:4,WRITES:5,INHERITS:7,IMPLEMENTS:7,ROUTE:4,TESTS:1};function zn(n){return Gn("sha256").update(n).digest("hex").slice(0,12)}function ke(n){return n.replaceAll("\\","/")}function Bn(n){let t=ke(n).split("/").filter(Boolean);return t[0]==="src"&&t[1]?`src/${t[1]}`:t[0]==="packages"&&t[1]?`packages/${t[1]}`:t[0]==="tests"?"tests":t[0]==="bin"?"bin":t[0]??"root"}function Un(n){let e=n.replace(/^src\//,"").replace(/^packages\//,"");return{"code-intelligence":"Code Intelligence",memory:"Memory",storage:"Storage",retrieval:"Retrieval",runtime:"Runtime",mcp:"MCP",hooks:"Hooks",security:"Security",snapshot:"Snapshots",production:"Production",capture:"Capture",processor:"Processor",core:"Core",tests:"Tests",bin:"CLI",sdk:"SDK",cli:"CLI"}[e]??e}var Ie=class{constructor(e){this.graph=e}graph;detect(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=new Map(t.map(u=>[u.id,u])),i=[...new Set(t.map(u=>ke(u.filePath)))].sort(),o=new Map,a=(u,f)=>{let g=[u,f].sort();return`${g[0]}\0${g[1]}`};for(let u of r){let f=s.get(u.from),g=s.get(u.to);if(!f||!g)continue;let h=ke(f.filePath),y=ke(g.filePath);if(h===y)continue;let p=Kn[u.type]??0;if(p<=0)continue;let S=a(h,y),x=o.get(S);x?x.weight+=p:o.set(S,{a:[h,y].sort()[0],b:[h,y].sort()[1],weight:p})}let d=new Map;for(let u of i){let f=Bn(u),g=d.get(f)??[];g.push(u),d.set(f,g)}let c=[];for(let[u,f]of d){if(f.sort(),f.length<=30){c.push({subsystem:u,files:f});continue}let g=new Set(f),h=new Map;for(let x of f)h.set(x,new Set);for(let x of o.values())!g.has(x.a)||!g.has(x.b)||x.weight>=8&&(h.get(x.a)?.add(x.b),h.get(x.b)?.add(x.a));let y=new Set,p=[];for(let x of f){if(y.has(x))continue;let C=[x],w=[];for(y.add(x);C.length>0;){let I=C.shift();w.push(I);for(let We of h.get(I)??[])y.has(We)||(y.add(We),C.push(We))}p.push(w.sort())}let S=[];for(let x of p)x.length>=2?c.push({subsystem:u,files:x}):S.push(...x);S.length>0&&c.push({subsystem:`${u}/residual`,files:S.sort()})}let l=c.map(u=>({subsystem:u.subsystem,files:[...u.files]}));for(let u=0;u<3;u++){let f=!1;e:for(let g=0;g<l.length;g++){let h=l[g];if(!(!h||h.files.length>3))for(let y=0;y<l.length;y++){if(g===y)continue;let p=l[y];if(!p)continue;let S=0,x=new Set(h.files),C=new Set(p.files);for(let w of o.values())(x.has(w.a)&&C.has(w.b)||x.has(w.b)&&C.has(w.a))&&(S+=w.weight);if(S>=20){p.files=[...new Set([...p.files,...h.files])].sort(),l.splice(g,1),f=!0;break e}}}if(!f)break}let m=[];for(let u of l){let f=new Set(u.files),g=0,h=0;for(let C of o.values()){let w=f.has(C.a),I=f.has(C.b);w&&I?g+=C.weight:(w||I)&&(h+=C.weight)}let y=g+h,p=u.subsystem.replace(/\/residual$/,""),S=Un(p),x=u.subsystem.endsWith("/residual")?`${S} Misc`:S;m.push({id:`cluster-${zn(`${u.subsystem}:${u.files.join("|")}`)}`,label:x,subsystem:p,files:u.files,size:u.files.length,internalWeight:g,externalWeight:h,cohesion:y>0?Number((g/y).toFixed(4)):0})}return m.sort((u,f)=>f.size-u.size||f.cohesion-u.cohesion||u.label.localeCompare(f.label))}};var Ae=class{constructor(e){this.graph=e;this.entryPoints=new B(e),this.hotspots=new Re(e),this.layers=new Te(e),this.clusters=new Ie(e)}graph;entryPoints;hotspots;layers;clusters;analyze(e){let t=this.entryPoints.detect(e),r=this.hotspots.analyze(e),s=this.layers.detect(e),i=this.clusters.detect(e),o=new Set(this.graph.allSymbols(e).map(a=>a.filePath));return{version:1,projectId:e,updatedAt:new Date().toISOString(),summary:{files:o.size,entryPoints:t.length,hotspots:r.length,layers:s.length,clusters:i.length},entryPoints:t,hotspots:r,layers:s,clusters:i}}};var Gt=new Set(["CALLS","CALL_REFERENCE","IMPORTS","USES_TYPE","WRITES","INHERITS","IMPLEMENTS","ROUTE","TESTS"]);function tt(n){return n.replaceAll("\\","/").toLowerCase()}function Hn(n){let e=tt(n);return e.startsWith("tests/")||e.includes("/tests/")||e.includes("__tests__")||/\.(test|spec)\.[^.]+$/.test(e)}function Wn(n){let e=tt(n.filePath);return e.startsWith("bin/")||/(^|\/)index\.[^.]+$/.test(e)||/(^|\/)(main|server|cli)\.[^.]+$/.test(e)}var Me=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e),s=new Set(new B(this.graph).detect(e).map(o=>o.symbolId)),i=[];for(let o of t){if(!["function","method","class","interface","property"].includes(o.type)||Hn(o.filePath)||s.has(o.id)||Wn(o)||r.filter(f=>f.to===o.id&&Gt.has(f.type)).length>0)continue;let d=r.filter(f=>f.from===o.id&&Gt.has(f.type)),c=["no incoming usage edge"],l=50;(o.type==="function"||o.type==="method")&&(l+=20,c.push("callable symbol")),o.type==="property"&&(l-=15),d.length===0&&(l+=10,c.push("no outgoing dependency")),tt(o.filePath).includes("/internal/")&&(l+=10,c.push("internal implementation")),o.name.startsWith("_")&&(l+=5);let u=l>=75?"high":l>=55?"medium":"low";i.push({symbolId:o.id,name:o.name,qualifiedName:o.qualifiedName,type:o.type,filePath:o.filePath,startLine:o.startLine,confidence:u,score:l,reasons:c})}return i.sort((o,a)=>a.score-o.score||o.filePath.localeCompare(a.filePath)||o.name.localeCompare(a.name))}};var Yn=new Set(["CALLS","CALL_REFERENCE","IMPORTS","USES_TYPE","WRITES","INHERITS","IMPLEMENTS","ROUTE"]),Ne=class{constructor(e){this.graph=e}graph;analyze(e){let t=this.graph.allSymbols(e),r=this.graph.allEdges(e).filter(o=>Yn.has(o.type)),s=new Map(t.map(o=>[o.id,o]));return[...new Set(t.map(o=>o.filePath))].sort().map(o=>{let a=new Set,d=new Set,c=0,l=0;for(let m of r){let u=s.get(m.from),f=s.get(m.to);!u||!f||u.filePath===f.filePath||(u.filePath===o&&(a.add(f.filePath),c++),f.filePath===o&&(d.add(u.filePath),l++))}return{filePath:o,dependencies:[...a].sort(),dependents:[...d].sort(),outgoingEdges:c,incomingEdges:l}})}};var Le=class{constructor(e){this.graph=e}graph;analyze(e){let t=new Me(this.graph).analyze(e),r=new Ne(this.graph).analyze(e);return{version:1,projectId:e,updatedAt:new Date().toISOString(),summary:{deadCodeCandidates:t.length,highConfidenceDeadCode:t.filter(s=>s.confidence==="high").length,files:r.length},deadCode:t,dependencies:r}}};var Oe=class{constructor(e){this.graph=e}graph;build(e,t,r,s){let i=this.graph.allSymbols(e),o=this.graph.allEdges(e),a=new Map(t?.layers.map(p=>[p.filePath,p.layer])??[]),d=new Map;for(let p of t?.clusters??[])for(let S of p.files)d.set(S,{id:p.id,label:p.label});let c=new Map(t?.hotspots.map(p=>[p.filePath,p.score])??[]),l=new Map(r?.deadCode.map(p=>[p.symbolId,{confidence:p.confidence,score:p.score}])??[]),m=new Map,u=new Map;for(let p of o)u.set(p.from,(u.get(p.from)??0)+1),m.set(p.to,(m.get(p.to)??0)+1);let f=[];for(let p=0;p<i.length;p++){let S=i[p],x=d.get(S.filePath);f.push({id:S.id,name:S.name,qualifiedName:S.qualifiedName,type:S.type,filePath:S.filePath,layer:a.get(S.filePath),cluster:x?.id,clusterLabel:x?.label,hotspotScore:c.get(S.filePath),deadCode:l.get(S.id),incoming:m.get(S.id)??0,outgoing:u.get(S.id)??0}),((p+1)%100===0||p===i.length-1)&&s?.({current:p+1,total:i.length,phase:"nodes"})}let g=new Set(f.map(p=>p.id)),h=o.filter(p=>g.has(p.from)&&g.has(p.to)),y=[];for(let p=0;p<h.length;p++){let S=h[p];y.push({source:S.from,target:S.to,type:S.type,confidence:typeof S.metadata?.confidence=="string"?S.metadata.confidence:void 0,resolver:typeof S.metadata?.resolver=="string"?S.metadata.resolver:void 0}),((p+1)%100===0||p===h.length-1)&&s?.({current:p+1,total:h.length,phase:"links"})}return{version:1,projectId:e,generatedAt:new Date().toISOString(),summary:{nodes:f.length,links:y.length,files:new Set(f.map(p=>p.filePath)).size,clusters:new Set(f.map(p=>p.cluster).filter(Boolean)).size},nodes:f,links:y}}};import{spawnSync as Vn}from"node:child_process";var qn=[{language:"python",command:"pyright-langserver"},{language:"go",command:"gopls"},{language:"rust",command:"rust-analyzer"},{language:"c",command:"clangd"},{language:"cpp",command:"clangd"}];function Jn(n){return n.split(/\r?\n/u).map(t=>t.trim()).find(Boolean)||void 0}function Qn(n,e){let t=Vn(e,["--version"],{encoding:"utf8",timeout:2e3,windowsHide:!0}),r=!t.error&&t.status===0,s=[t.stdout??"",t.stderr??""].join(`
6
+ `);return{language:n,command:e,installed:r,...r?{version:Jn(s)}:{},mode:"external-optional",activeInStructuralGraph:!1}}function Kt(){return qn.map(n=>Qn(n.language,n.command))}var Zn=process.argv.includes("--json"),zt=Kt(),Xn={structuralParser:{engine:"typescript-compiler-api",languages:G.filter(n=>n.structural).map(n=>n.language)},lexicalSearch:{engine:"sqlite-fts5-bm25",languages:G.filter(n=>n.lexicalSearch).map(n=>n.language)},lspFoundation:{mode:"detection-only",automaticDownload:!1,structuralGraphIntegration:!1,servers:zt}};if(Zn)console.log(JSON.stringify(Xn,null,2));else{console.log("ToolNet Code Intelligence Capabilities"),console.log(""),console.log("Structural parser: TypeScript Compiler API");for(let n of G)console.log([n.language.padEnd(12),n.structural?"structural=yes":"structural=no",n.lexicalSearch?"lexical=yes":"lexical=no",n.lspServer?`lsp=${n.lspServer}`:""].filter(Boolean).join(" "));console.log(""),console.log("LSP detection:");for(let n of zt)console.log([n.language.padEnd(12),n.command.padEnd(22),n.installed?"available":"not-installed"].join(" "))}import{homedir as As}from"node:os";import{join as Ms}from"node:path";import{DeleteObjectCommand as es,GetObjectCommand as ts,HeadObjectCommand as rs,ListObjectsV2Command as ns,PutObjectCommand as ss,S3Client as os}from"@aws-sdk/client-s3";import{getSignedUrl as is}from"@aws-sdk/s3-request-presigner";var _e=class{name="huggingface";client;bucket;constructor(e){this.bucket=e.bucket,this.client=new os({region:"us-east-1",endpoint:`https://s3.hf.co/${e.namespace}`,forcePathStyle:!0,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}})}async put(e,t,r="application/octet-stream"){let s=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new ss({Bucket:this.bucket,Key:e,Body:s,ContentType:r}))}async get(e){let t=await is(this.client,new ts({Bucket:this.bucket,Key:e}),{expiresIn:60}),r=await fetch(t,{redirect:"follow"});if(r.status===404)return null;if(!r.ok)throw new Error(`HF download failed: ${r.status} ${r.statusText}`);return new Uint8Array(await r.arrayBuffer())}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await this.client.send(new rs({Bucket:this.bucket,Key:e})),!0}catch(t){if(typeof t=="object"&&t!==null&&"$metadata"in t&&t.$metadata?.httpStatusCode===404)return!1;throw t}}async delete(e){await this.client.send(new es({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let s=await this.client.send(new ns({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let i of s.Contents??[])i.Key&&t.push({key:i.Key,size:i.Size,updatedAt:i.LastModified?.toISOString()});r=s.IsTruncated?s.NextContinuationToken:void 0}while(r);return t}};import{access as Bt,mkdir as as,readFile as cs,readdir as ls,rm as ds,stat as Ut,writeFile as us}from"node:fs/promises";import{dirname as ps,join as ms,relative as Ht,resolve as fs}from"node:path";var se=class{constructor(e){this.root=e}root;name="local";path(e){let t=e.replace(/^\/+/,"");return fs(this.root,t)}async put(e,t){let r=this.path(e);await as(ps(r),{recursive:!0}),await us(r,t)}async get(e){try{return await cs(this.path(e))}catch(t){if(typeof t=="object"&&t!==null&&"code"in t&&t.code==="ENOENT")return null;throw t}}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await Bt(this.path(e)),!0}catch{return!1}}async delete(e){await ds(this.path(e),{force:!0})}async list(e=""){let t=this.path(e),r=[];try{await Bt(t)}catch{return r}let s=async o=>{let a=await ls(o,{withFileTypes:!0});for(let d of a){let c=ms(o,d.name);if(d.isDirectory()){await s(c);continue}let l=await Ut(c);r.push({key:Ht(this.root,c),size:l.size,updatedAt:l.mtime.toISOString()})}},i=await Ut(t);return i.isDirectory()?await s(t):r.push({key:Ht(this.root,t),size:i.size,updatedAt:i.mtime.toISOString()}),r}};import{DeleteObjectCommand as gs,GetObjectCommand as hs,HeadObjectCommand as ys,ListObjectsV2Command as Ss,PutObjectCommand as bs,S3Client as xs}from"@aws-sdk/client-s3";import{getSignedUrl as Es}from"@aws-sdk/s3-request-presigner";var oe=class{name;client;bucket;constructor(e){this.name=e.name??"s3",this.bucket=e.bucket,this.client=new xs({region:e.region??"us-east-1",endpoint:e.endpoint||void 0,forcePathStyle:e.forcePathStyle??!1,requestChecksumCalculation:"WHEN_REQUIRED",responseChecksumValidation:"WHEN_REQUIRED",credentials:{accessKeyId:e.accessKeyId,secretAccessKey:e.secretAccessKey}})}async put(e,t,r="application/octet-stream"){let s=typeof t=="string"?Buffer.from(t,"utf8"):t;await this.client.send(new bs({Bucket:this.bucket,Key:e,Body:s,ContentType:r}))}async get(e){let t=await Es(this.client,new hs({Bucket:this.bucket,Key:e}),{expiresIn:60}),r=await fetch(t,{redirect:"follow"});if(r.status===404)return null;if(!r.ok)throw new Error(`${this.name} download failed: ${r.status} ${r.statusText}`);return new Uint8Array(await r.arrayBuffer())}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){try{return await this.client.send(new ys({Bucket:this.bucket,Key:e})),!0}catch(t){if(typeof t=="object"&&t!==null&&"$metadata"in t&&t.$metadata?.httpStatusCode===404)return!1;throw t}}async delete(e){await this.client.send(new gs({Bucket:this.bucket,Key:e}))}async list(e=""){let t=[],r;do{let s=await this.client.send(new Ss({Bucket:this.bucket,Prefix:e||void 0,ContinuationToken:r}));for(let i of s.Contents??[])i.Key&&t.push({key:i.Key,size:i.Size,updatedAt:i.LastModified?.toISOString()});r=s.IsTruncated?s.NextContinuationToken:void 0}while(r);return t}};import{createCipheriv as Cs,createDecipheriv as vs,createHash as ws,randomBytes as Ps,timingSafeEqual as Rs}from"node:crypto";import{readFileSync as Ts}from"node:fs";var j=Buffer.from("TNMEME01","ascii"),Yt=1,ie=8,ae=12,rt=16,Vt=j.length+1+ie+ae+rt,ks="toolnet-memory:remote-encryption:v1:",qt="aes-256-gcm",nt=32,P=class extends Error{constructor(t,r){super(r);this.code=t;this.name="RemoteEncryptionError"}code};function Is(n){return n?["1","true","yes","on","enabled"].includes(n.trim().toLowerCase()):!1}function st(n=process.env){return Is(n.TOOLNET_REMOTE_ENCRYPTION)}function Wt(n){let e=n.trim();if(!e)throw new P("REMOTE_ENCRYPTION_KEY_EMPTY","Remote encryption key is empty.");let t;if(e.startsWith("hex:")){let r=e.slice(4);if(!/^[0-9a-f]{64}$/iu.test(r))throw new P("REMOTE_ENCRYPTION_KEY_INVALID","hex: remote encryption key must contain exactly 64 hexadecimal characters.");t=Buffer.from(r,"hex")}else if(/^[0-9a-f]{64}$/iu.test(e))t=Buffer.from(e,"hex");else{let r=e.startsWith("base64:")?e.slice(7):e;if(!/^[A-Za-z0-9+/_-]+={0,2}$/u.test(r))throw new P("REMOTE_ENCRYPTION_KEY_INVALID","Remote encryption key must be 32 raw bytes encoded as hexadecimal or base64.");t=Buffer.from(r,r.includes("-")||r.includes("_")?"base64url":"base64")}if(t.length!==nt)throw new P("REMOTE_ENCRYPTION_KEY_INVALID_LENGTH",`Remote encryption key must decode to exactly ${nt} bytes.`);return t}function Jt(n=process.env){let e=n.TOOLNET_REMOTE_ENCRYPTION_KEY?.trim(),t=n.TOOLNET_REMOTE_ENCRYPTION_KEY_FILE?.trim();if(e&&t)throw new P("REMOTE_ENCRYPTION_KEY_AMBIGUOUS","Configure either TOOLNET_REMOTE_ENCRYPTION_KEY or TOOLNET_REMOTE_ENCRYPTION_KEY_FILE, not both.");if(e)return Wt(e);if(t){let r;try{r=Ts(t,"utf8")}catch(s){throw new P("REMOTE_ENCRYPTION_KEY_FILE_READ_FAILED",[`Unable to read remote encryption key file: ${t}.`,s instanceof Error?s.message:String(s)].join(" "))}return Wt(r)}}function Qt(n){return ws("sha256").update(n).digest().subarray(0,ie)}function Zt(n){return Buffer.from(`${ks}${n}`,"utf8")}function ot(n){return n.byteLength<j.length?!1:Buffer.from(n).subarray(0,j.length).equals(j)}function Xt(n,e,t){if(t.byteLength!==nt)throw new P("REMOTE_ENCRYPTION_KEY_INVALID_LENGTH","AES-256-GCM requires a 32-byte key.");let r=typeof e=="string"?Buffer.from(e,"utf8"):Buffer.from(e),s=Ps(ae),i=Cs(qt,t,s);i.setAAD(Zt(n));let o=Buffer.concat([i.update(r),i.final()]),a=i.getAuthTag(),d=Buffer.alloc(Vt),c=0;return j.copy(d,c),c+=j.length,d.writeUInt8(Yt,c),c+=1,Qt(t).copy(d,c),c+=ie,s.copy(d,c),c+=ae,a.copy(d,c),Buffer.concat([d,o])}function er(n,e,t){let r=Buffer.from(e);if(!ot(r))throw new P("REMOTE_ENCRYPTION_ENVELOPE_REQUIRED","Payload is not a ToolNet encrypted remote object.");if(r.length<Vt)throw new P("REMOTE_ENCRYPTION_ENVELOPE_TRUNCATED","Encrypted remote payload is truncated.");let s=j.length,i=r.readUInt8(s);if(s+=1,i!==Yt)throw new P("REMOTE_ENCRYPTION_VERSION_UNSUPPORTED",`Unsupported remote encryption envelope version: ${i}.`);let o=r.subarray(s,s+ie);s+=ie;let a=Qt(t);if(!Rs(o,a))throw new P("REMOTE_ENCRYPTION_KEY_MISMATCH","Configured remote encryption key does not match this encrypted object.");let d=r.subarray(s,s+ae);s+=ae;let c=r.subarray(s,s+rt);s+=rt;let l=r.subarray(s),m=vs(qt,t,d);m.setAAD(Zt(n)),m.setAuthTag(c);try{return Buffer.concat([m.update(l),m.final()])}catch{throw new P("REMOTE_ENCRYPTION_AUTH_FAILED","Encrypted remote object failed AES-GCM authentication.")}}var it=class{constructor(e,t){this.inner=e;this.options=t;if(this.name=e.name,t.enabled&&!t.key)throw new P("REMOTE_ENCRYPTION_KEY_REQUIRED","Remote client-side encryption is enabled but no encryption key is configured.")}inner;options;name;async put(e,t,r){if(!this.options.enabled){await this.inner.put(e,t,r);return}let s=this.options.key;if(!s)throw new P("REMOTE_ENCRYPTION_KEY_REQUIRED","Remote encryption key is unavailable.");let i=Xt(e,t,s);await this.inner.put(e,i,"application/octet-stream")}async get(e){let t=await this.inner.get(e);if(!t)return null;if(!ot(t))return t;if(!this.options.enabled)throw new P("REMOTE_ENCRYPTION_REQUIRED",["Remote object is client-side encrypted.","Enable TOOLNET_REMOTE_ENCRYPTION and configure the matching key."].join(" "));let r=this.options.key;if(!r)throw new P("REMOTE_ENCRYPTION_KEY_REQUIRED","Remote object is encrypted but no decryption key is configured.");return er(e,t,r)}async getText(e){let t=await this.get(e);return t?Buffer.from(t).toString("utf8"):null}async exists(e){return this.inner.exists(e)}async delete(e){await this.inner.delete(e)}async list(e=""){return this.inner.list(e)}};function tr(n,e=process.env){if(n.name==="local")return st(e)&&console.warn("[storage] Remote encryption requested but active storage provider is local; local data remains unchanged."),n;let t=st(e),r=t?Jt(e):void 0;return new it(n,{enabled:t,key:r})}function ce(n){return tr(n)}function at(n,e){return console.warn(e),ce(new se(n))}function rr(n){let e=n.localRoot??Ms(As(),".toolnet-memory","storage");if(n.provider==="r2"){let t=n.r2;return t?.accountId&&t.bucket&&t.accessKeyId&&t.secretAccessKey?ce(new oe({name:"r2",endpoint:`https://${t.accountId}.r2.cloudflarestorage.com`,region:"auto",bucket:t.bucket,forcePathStyle:!0,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey})):at(e,"[storage] Cloudflare R2 credentials missing. Using local fallback.")}if(n.provider==="s3"){let t=n.s3;return t?.bucket&&t.accessKeyId&&t.secretAccessKey?ce(new oe({name:"s3",endpoint:t.endpoint,region:t.region??"us-east-1",bucket:t.bucket,forcePathStyle:t.forcePathStyle??!1,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey})):at(e,"[storage] S3 credentials missing. Using local fallback.")}if(n.provider==="huggingface"){let t=n.huggingface;return t?.namespace&&t.bucket&&t.accessKeyId&&t.secretAccessKey?ce(new _e({namespace:t.namespace,bucket:t.bucket,accessKeyId:t.accessKeyId,secretAccessKey:t.secretAccessKey})):at(e,"[storage] Hugging Face credentials missing. Using local fallback.")}return ce(new se(e))}function Ns(n){return new Promise(e=>setTimeout(e,n))}async function nr(n,e={}){let t=Math.max(1,e.attempts??3),r=e.baseDelayMs??150,s=e.maxDelayMs??2e3,i;for(let o=1;o<=t;o++)try{return await n()}catch(a){if(i=a,o>=t)break;let d=Math.min(s,r*2**(o-1)),c=Math.floor(Math.random()*Math.max(1,d*.2));await Ns(d+c)}throw i}var Ls=new Set(["put","get","getText","delete","list"]);function sr(n,e={}){return new Proxy(n,{get(t,r){let s=Reflect.get(t,r,t);return typeof s!="function"?s:Ls.has(r)?(...i)=>nr(()=>Promise.resolve(s.apply(t,i)),e):s.bind(t)}})}function or(n){let e=n.trim().replace(/\s+/g,"_").replace(/[^A-Za-z0-9._-]/g,"_").replace(/_+/g,"_").replace(/^\.+|\.+$/g,"").slice(0,100);if(!e||e==="."||e==="..")throw new Error("Invalid project storage folder");return e}function ir(n){let e=n.replace(/^\/+/,"");if(e.startsWith("memories/"))return"memory/records/"+e.slice(9);if(e.startsWith("vectors/"))return"memory/vectors/"+e.slice(8);if(e.startsWith("graph/"))return"code/graph/"+e.slice(6);let t=e.match(/^(snapshots\/[^/]+\/)memories\/(.+)$/);if(t)return`${t[1]}memory/records/${t[2]}`;if(t=e.match(/^(snapshots\/[^/]+\/)vectors\/(.+)$/),t)return`${t[1]}memory/vectors/${t[2]}`;if(t=e.match(/^(snapshots\/[^/]+\/)graph\/(.+)$/),t)return`${t[1]}code/graph/${t[2]}`;let r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)memories\/(.+)$/);if(r)return`${r[1]}memory/records/${r[2]}`;if(r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)vectors\/(.+)$/),r)return`${r[1]}memory/vectors/${r[2]}`;if(r=e.match(/^(projects\/[^/]+\/snapshots\/[^/]+\/)graph\/(.+)$/),r)return`${r[1]}code/graph/${r[2]}`;let s=e.match(/^(projects\/[^/]+\/)memories\/(.+)$/);return s?`${s[1]}memory/records/${s[2]}`:(s=e.match(/^(projects\/[^/]+\/)vectors\/(.+)$/),s?`${s[1]}memory/vectors/${s[2]}`:(s=e.match(/^(projects\/[^/]+\/)graph\/(.+)$/),s?`${s[1]}code/graph/${s[2]}`:e))}var je=class{constructor(e,t,r,s){this.provider=e;this.name=e.name,this.projectId=t,this.projectName=r,this.folder=or(s??r),this.sourcePrefix=`projects/${t}`,this.targetPrefix=`projects/${this.folder}`}provider;name;folder;sourcePrefix;targetPrefix;projectId;projectName;registryPromise;async registerProject(){let e=`${this.targetPrefix}/project.json`,t=new Date().toISOString(),r=t,s=await this.provider.getText(e);if(s){let o;try{o=JSON.parse(s)}catch(a){throw new Error(`Invalid remote ToolNet project manifest at ${e}: ${a instanceof Error?a.message:String(a)}`)}if(typeof o.id=="string"&&o.id!==this.projectId)throw new Error(["ToolNet remote project namespace collision.",`Remote folder: ${this.targetPrefix}`,`Existing project id: ${o.id}`,`Current project id: ${this.projectId}`,"Refusing to mix data from two projects."].join(" "));typeof o.createdAt=="string"&&(r=o.createdAt)}let i={version:1,id:this.projectId,name:this.projectName,remote:this.folder,createdAt:r,updatedAt:t};await this.provider.put(e,JSON.stringify(i,null,2)+`
7
+ `,"application/json")}async ensureRegistered(){return this.registryPromise||(this.registryPromise=this.registerProject()),this.registryPromise}key(e){if(e=ir(e),e===this.sourcePrefix)return this.targetPrefix;if(e.startsWith(`${this.sourcePrefix}/`))return this.targetPrefix+e.slice(this.sourcePrefix.length);if(e===this.targetPrefix||e.startsWith(`${this.targetPrefix}/`))return e;if(e.startsWith("projects/"))throw new Error(["Cross-project storage access denied.",`Current project: ${this.targetPrefix}`,`Requested key: ${e}`].join(" "));return e}async put(e,t,r){return await this.ensureRegistered(),this.provider.put(this.key(e),t,r)}async get(e){return await this.ensureRegistered(),this.provider.get(this.key(e))}async getText(e){return await this.ensureRegistered(),this.provider.getText(this.key(e))}async delete(e){return await this.ensureRegistered(),this.provider.delete(this.key(e))}async exists(e){return await this.ensureRegistered(),this.provider.exists(this.key(e))}async list(e){return await this.ensureRegistered(),this.provider.list(this.key(e))}};var De=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"graph","resolution","current.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e),"application/json")}};var Fe=class{constructor(e){this.storage=e}storage;base(e){return["projects",e,"code","architecture"].join("/")}async load(e){let t=await this.storage.getText(`${this.base(e)}/current.json`);return t?JSON.parse(t):null}async save(e){let t=this.base(e.projectId),r=s=>JSON.stringify({version:e.version,projectId:e.projectId,updatedAt:e.updatedAt,data:s},null,2);await Promise.all([this.storage.put(`${t}/current.json`,JSON.stringify(e,null,2),"application/json"),this.storage.put(`${t}/entry-points.json`,r(e.entryPoints),"application/json"),this.storage.put(`${t}/hotspots.json`,r(e.hotspots),"application/json"),this.storage.put(`${t}/layers.json`,r(e.layers),"application/json"),this.storage.put(`${t}/clusters.json`,r(e.clusters),"application/json")])}};var $e=class{constructor(e){this.storage=e}storage;base(e){return["projects",e,"code","analysis"].join("/")}async load(e){let t=await this.storage.getText(`${this.base(e)}/current.json`);return t?JSON.parse(t):null}async save(e){let t=this.base(e.projectId),r=s=>JSON.stringify({version:e.version,projectId:e.projectId,updatedAt:e.updatedAt,data:s},null,2);await Promise.all([this.storage.put(`${t}/current.json`,JSON.stringify(e,null,2),"application/json"),this.storage.put(`${t}/dead-code.json`,r(e.deadCode),"application/json"),this.storage.put(`${t}/dependencies.json`,r(e.dependencies),"application/json")])}};var Ge=class{constructor(e){this.storage=e}storage;key(e){return["projects",e,"code","visualization","graph.json"].join("/")}async load(e){let t=await this.storage.getText(this.key(e));return t?JSON.parse(t):null}async save(e){await this.storage.put(this.key(e.projectId),JSON.stringify(e,null,2),"application/json")}};async function ar(n,e={}){let t=Date.now();async function r(h,y,p){await e.onStage?.({id:h,title:y,state:"start"});let S=Date.now(),x=await p();return await e.onStage?.({id:h,title:y,state:"complete",durationMs:Date.now()-S}),x}let s=gt(),i=sr(rr({provider:s.storage.provider,huggingface:s.storage.huggingface,localRoot:s.storage.localRoot}),{attempts:3}),o=new je(i,n.id,n.name,n.remote??n.name),a=new Z(o),d=await r("source-index","Source Index",async()=>{let h=await new Se().index(n.id,n.rootPath,{onProgress:y=>{e.onSourceProgress?.(y)}});return await a.save({version:1,projectId:n.id,updatedAt:new Date().toISOString(),files:h.files,symbols:h.graph.allSymbols(n.id),edges:h.graph.allEdges(n.id)}),h}),c=d.graph,l=await r("type-resolution","Type Resolution",async()=>{let h=await new we(c).resolveProject(n.id,n.rootPath,y=>{e.onStageProgress?.({stage:"type-resolution",current:y.current,total:y.total,phase:y.phase,detail:y.detail})});return await new De(o).save(h),h});await r("rich-graph","Rich Graph",async()=>{let h=new Pe(c).enrich(n.id,n.rootPath,l,S=>{e.onStageProgress?.({stage:"rich-graph",current:S.current,total:S.total,phase:S.phase,detail:S.detail})}),y=c.allSymbols(n.id),p=c.allEdges(n.id);return await a.save({version:1,projectId:n.id,updatedAt:new Date().toISOString(),files:y.filter(S=>S.type==="file").length,symbols:y,edges:p}),h});let m=await r("semantic-index","Local Code Search \u2014 SQLite FTS5/BM25",async()=>new ve({projectId:n.id,rootPath:n.rootPath,storage:o,graph:c}).initialize(y=>{e.onStageProgress?.({stage:"semantic-index",current:y.current,total:y.total,phase:y.phase,detail:y.detail})})),u=await r("architecture","Architecture Intelligence",async()=>{let h=new Ae(c).analyze(n.id);return await new Fe(o).save(h),h}),f=await r("analysis","Graph Analysis",async()=>{let h=new Le(c).analyze(n.id);return await new $e(o).save(h),h}),g=await r("visualization","3D Visualization Dataset",async()=>{let h=new Oe(c).build(n.id,u,f,y=>{e.onStageProgress?.({stage:"visualization",current:y.current,total:y.total,phase:y.phase,detail:y.detail})});return await new Ge(o).save(h),h});return{files:d.files,project:{id:n.id,name:n.name,remote:n.remote??n.name},storage:o.name,durationMs:Date.now()-t,graph:{symbols:c.allSymbols(n.id).length,edges:c.allEdges(n.id).length},resolution:{total:l.total,exact:l.exact,high:l.high,fallback:l.fallback},semantic:m,architecture:u.summary,analysis:f.summary,visualization:g.summary}}import{Worker as Ds}from"worker_threads";import*as ze from"path";import{fileURLToPath as Fs}from"node:url";function Ke(){if(process.argv.includes("--no-color"))return!1;if(process.argv.includes("--color"))return!0;let n=process.env.NO_COLOR;if(n!==void 0&&n!=="")return!1;let e=process.env.FORCE_COLOR;if(e!==void 0&&e!=="")return e!=="0"&&e.toLowerCase()!=="false";if(process.stdout.isTTY===!0&&process.env.TERM!=="dumb")return!0;let t=process.env.CI;return t!==void 0&&t!==""}var Os={spinner:["\xB7","\u2722","\u2733","\u2736","\u273B","\u273D"],barFilled:"\u2588",barEmpty:"\u2591",phaseDone:"\u25C6",phaseActive:"\u25C7",rail:"\u2502",branch:"\u251C",corner:"\u2514",dash:"\u2014"},_s={spinner:[".","*","+","x","*","+"],barFilled:"#",barEmpty:"-",phaseDone:"[OK]",phaseActive:"[>>]",rail:"|",branch:"|",corner:"\\",dash:"-"};function js(){if(process.env.TOOLNET_ASCII==="1")return!1;if(process.env.TOOLNET_UNICODE==="1")return!0;if(process.platform==="win32"){let n=process.env.TERM_PROGRAM,e=process.env.WT_SESSION,t=process.env.VSCODE_PID,r=process.env.ConEmuPID;return!!(e||t||r||n==="vscode")}return process.env.TERM!=="linux"}var ct;function U(){return ct||(ct=js()?Os:_s),ct}var lr={scan:"Scanning files",parse:"Parsing code","type-resolution":"Type Resolution","rich-graph":"Rich Graph","semantic-index":"Semantic Code Index",architecture:"Architecture Intelligence","graph-analysis":"Graph Analysis",visualization:"3D Visualization Dataset"};function dr(){if(process.env.TOOLNET_STANDALONE==="1"||process.stdout.isTTY!==!0)return cr();let n=Ke(),e=U(),t=n?"\x1B[2m":"",r=n?"\x1B[32m":"",s=n?"\x1B[0m":"",i="",o="",a=-1,d=0,c=()=>{if(!o)return;let g="";a>=0?g=` ${e.dash} done`:d>0&&(g=` ${e.dash} ${d.toLocaleString()} found`),process.stdout.write(`\r\x1B[K${t}${e.branch}${s} ${r}${e.phaseDone}${s} ${o}${g}
8
+ `),o="",a=-1,d=0},l=ze.dirname(Fs(import.meta.url)),m=l.includes("/bundle/")||l.includes("\\bundle\\"),u=ze.join(l,m?"shimmer-worker.js":"shimmer-worker.ts"),f=new Ds(u,{workerData:{startTime:Date.now(),colors:n}});return{onProgress(g){let h=lr[g.stage]||g.stage;g.stage!==i&&i&&c(),i=g.stage,o=h;let y=-1,p=0;if(g.total>0&&g.current>=0){let S=g.current/g.total*100;y=g.current>=g.total?100:Math.min(99,Math.round(S))}else g.current>0&&(p=g.current);a=y,d=p,f.postMessage({type:"update",phase:g.stage,phaseName:h,percent:y,count:p})},stop(){return new Promise(g=>{let h=!1,y=()=>{h||(h=!0,c(),g())},p=setTimeout(()=>{f.terminate().then(y)},2e3);f.on("message",S=>{S.type==="stopped"&&(clearTimeout(p),f.terminate().then(y))}),f.postMessage({type:"stop"})})}}}function cr(){let n="";return{onProgress(e){if(e.stage===n)return;n=e.stage;let t=lr[e.stage]||e.stage;process.stdout.write(`${t}...
9
+ `)},stop(){return Promise.resolve()}}}var R={reset:"\x1B[0m",green:"\x1B[32m",gray:"\x1B[90m",white:"\x1B[97m",cyan:"\x1B[36m",red:"\x1B[31m"};function D(n){return new Intl.NumberFormat("en-US").format(n)}function lt(n){return n<1e3?`${n}ms`:`${(n/1e3).toFixed(1)}s`}function ur(n,e,t){return Math.max(e,Math.min(t,n))}function $s(n,e){return e<=0?"":n.length<=e?n:e<=3?n.slice(0,e):`${n.slice(0,e-1)}\u2026`}var Be=class{stream;interactive;enabled;colorEnabled;rootPath;stages;shimmerProgress;startedAt=Date.now();scanTotal;parseCurrent=0;parseTotal=0;finished=!1;lastPlainParseBucket=-1;constructor(e){this.stream=e.stream??process.stdout,this.enabled=e.enabled??!0;let t=e.interactive??this.stream.isTTY===!0;this.interactive=t&&process.env.TERM!=="dumb",this.colorEnabled=this.interactive&&Ke(),this.rootPath=e.rootPath,this.stages=e.stages.map(r=>({...r,state:"pending"}))}start(){if(!this.enabled)return;if(this.startedAt=Date.now(),!this.interactive){this.stream.write(`\u25C7 Initializing ToolNet Index
9
10
  `),this.stream.write(`\u25C7 Initialized in ${this.rootPath}
10
- `);return}let e=$();this.writeStatic(this.color("\u25C7 Initializing ToolNet Index",P.white)),this.stream.write(`
11
- `);let t=Math.max(16,this.terminalWidth()-18);this.writeStatic(`${this.color(e.phaseDone,P.green)} Initialized in ${Es(this.rootPath,t)}`),this.writeStatic(this.color(e.rail,P.gray)),this.shimmerProgress=wt()}scanningComplete(e){if(this.scanTotal=e,this.parseTotal=e,!!this.enabled){if(!this.interactive){this.stream.write(`\u25C6 Scanning files \u2014 ${N(e)} found
12
- `);return}this.shimmerProgress&&this.shimmerProgress.onProgress({stage:"scan",current:e,total:0})}}parsingProgress(e,t){if(this.parseCurrent=e,this.parseTotal=t,!this.enabled)return;let r=t>0?Math.floor(e/t*100):100;if(!this.interactive){let n=r>=100?100:Math.floor(r/25)*25;n!==this.lastPlainParseBucket&&(this.lastPlainParseBucket=n,this.stream.write(`\u2192 Parsing code \u2014 ${r}%
11
+ `);return}let e=U();this.writeStatic(this.color("\u25C7 Initializing ToolNet Index",R.white)),this.stream.write(`
12
+ `);let t=Math.max(16,this.terminalWidth()-18);this.writeStatic(`${this.color(e.phaseDone,R.green)} Initialized in ${$s(this.rootPath,t)}`),this.writeStatic(this.color(e.rail,R.gray)),this.shimmerProgress=dr()}scanningComplete(e){if(this.scanTotal=e,this.parseTotal=e,!!this.enabled){if(!this.interactive){this.stream.write(`\u25C6 Scanning files \u2014 ${D(e)} found
13
+ `);return}this.shimmerProgress&&this.shimmerProgress.onProgress({stage:"scan",current:e,total:0})}}parsingProgress(e,t){if(this.parseCurrent=e,this.parseTotal=t,!this.enabled)return;let r=t>0?Math.floor(e/t*100):100;if(!this.interactive){let s=r>=100?100:Math.floor(r/25)*25;s!==this.lastPlainParseBucket&&(this.lastPlainParseBucket=s,this.stream.write(`\u2192 Parsing code \u2014 ${r}%
13
14
  `));return}this.shimmerProgress&&this.shimmerProgress.onProgress({stage:"parse",current:e,total:t})}startStage(e,t){let r=this.stages.find(o=>o.id===e);if(r&&(r.state="active",r.title=t,r.startedAt=Date.now()),!this.enabled||e==="source-index")return;if(!this.interactive){this.stream.write(`\u2192 ${t}
14
- `);return}let i={"type-resolution":"type-resolution","rich-graph":"rich-graph","semantic-index":"semantic-index",architecture:"architecture",analysis:"graph-analysis",visualization:"visualization"}[e]||e;this.shimmerProgress&&this.shimmerProgress.onProgress({stage:i,current:0,total:0})}stageProgress(e){!this.enabled||!this.interactive||!this.shimmerProgress||this.shimmerProgress.onProgress({stage:e.stage,current:e.current,total:e.total})}completeStage(e,t,r=0){let n=this.stages.find(o=>o.id===e);if(n&&(n.state="done",n.title=t,n.durationMs=r),!this.enabled)return;if(!this.interactive){e==="source-index"?this.stream.write(`\u2713 Parsing code \u2014 done
15
- `):this.stream.write(`\u2713 ${t} (${ze(r)})
16
- `);return}let i=$();this.writeStatic(`${this.color(i.rail,P.gray)} ${this.color(i.phaseDone,P.green)} ${t} ${this.color(i.dash,P.gray)} done`)}async finish(e){this.finished=!0,this.enabled&&(this.shimmerProgress&&this.interactive&&await this.shimmerProgress.stop(),this.printFinishSummary(e))}printFinishSummary(e){let t=$();if(this.interactive){this.writeStatic(this.color(t.rail,P.gray)),this.writeStatic(`${this.color(t.phaseDone,P.green)} Indexed ${N(e.files)} files`),this.writeStatic(this.color(t.rail,P.gray)),this.writeStatic(`${this.color(t.phaseActive,P.cyan)} ${N(e.symbols)} symbols, ${N(e.edges)} edges in ${ze(e.durationMs)}`),this.writeStatic(`${this.color(t.phaseActive,P.cyan)} Storage: ${e.storage}`),this.writeStatic(this.color(t.rail,P.gray)),this.writeStatic(`${this.color(t.corner,P.gray)} ${this.color(t.phaseDone,P.green)} Done`),this.stream.write(`
17
- `);return}let r=["",`\u25C6 Indexed ${N(e.files)} files`,"",`\u25C7 ${N(e.symbols)} symbols, ${N(e.edges)} edges in ${ze(e.durationMs)}`,`\u25C7 Storage: ${e.storage}`,"","Done",""];this.stream.write(r.join(`
18
- `))}fail(e){this.finished=!0,this.enabled&&(this.shimmerProgress&&this.interactive?this.shimmerProgress.stop().then(()=>{this.printFailMessage(e)}):this.printFailMessage(e))}printFailMessage(e){if(this.interactive){this.stream.write(`${this.color("\u2717",P.red)} ${e}
15
+ `);return}let i={"type-resolution":"type-resolution","rich-graph":"rich-graph","semantic-index":"semantic-index",architecture:"architecture",analysis:"graph-analysis",visualization:"visualization"}[e]||e;this.shimmerProgress&&this.shimmerProgress.onProgress({stage:i,current:0,total:0})}stageProgress(e){!this.enabled||!this.interactive||!this.shimmerProgress||this.shimmerProgress.onProgress({stage:e.stage,current:e.current,total:e.total})}completeStage(e,t,r=0){let s=this.stages.find(o=>o.id===e);if(s&&(s.state="done",s.title=t,s.durationMs=r),!this.enabled)return;if(!this.interactive){e==="source-index"?this.stream.write(`\u2713 Parsing code \u2014 done
16
+ `):this.stream.write(`\u2713 ${t} (${lt(r)})
17
+ `);return}let i=U();this.writeStatic(`${this.color(i.rail,R.gray)} ${this.color(i.phaseDone,R.green)} ${t} ${this.color(i.dash,R.gray)} done`)}async finish(e){this.finished=!0,this.enabled&&(this.shimmerProgress&&this.interactive&&await this.shimmerProgress.stop(),this.printFinishSummary(e))}printFinishSummary(e){let t=U();if(this.interactive){this.writeStatic(this.color(t.rail,R.gray)),this.writeStatic(`${this.color(t.phaseDone,R.green)} Indexed ${D(e.files)} files`),this.writeStatic(this.color(t.rail,R.gray)),this.writeStatic(`${this.color(t.phaseActive,R.cyan)} ${D(e.symbols)} symbols, ${D(e.edges)} edges in ${lt(e.durationMs)}`),this.writeStatic(`${this.color(t.phaseActive,R.cyan)} Storage: ${e.storage}`),this.writeStatic(this.color(t.rail,R.gray)),this.writeStatic(`${this.color(t.corner,R.gray)} ${this.color(t.phaseDone,R.green)} Done`),this.stream.write(`
18
+ `);return}let r=["",`\u25C6 Indexed ${D(e.files)} files`,"",`\u25C7 ${D(e.symbols)} symbols, ${D(e.edges)} edges in ${lt(e.durationMs)}`,`\u25C7 Storage: ${e.storage}`,"","Done",""];this.stream.write(r.join(`
19
+ `))}fail(e){this.finished=!0,this.enabled&&(this.shimmerProgress&&this.interactive?this.shimmerProgress.stop().then(()=>{this.printFailMessage(e)}):this.printFailMessage(e))}printFailMessage(e){if(this.interactive){this.stream.write(`${this.color("\u2717",R.red)} ${e}
19
20
  `);return}this.stream.write(`\u2717 ${e}
20
- `)}terminalWidth(){let e=this.stream.columns;if(typeof e=="number"&&Number.isFinite(e))return Ct(Math.floor(e),32,240);let t=process.stdout.columns;return typeof t=="number"&&Number.isFinite(t)?Ct(Math.floor(t),32,240):80}color(e,t){return this.colorEnabled?`${t}${e}${P.reset}`:e}writeStatic(e){this.stream.write(`${e}
21
- `)}};import{createConnection as Rs}from"node:net";import{homedir as Ps}from"node:os";import{join as ks}from"node:path";function Et(){return process.env.TOOLNET_SERVICE_SOCKET??ks(Ps(),".toolnet","run","service.sock")}function Ts(s,e={}){let t=e.socketPath??Et(),r=e.timeoutMs??1e3;return new Promise((n,i)=>{let o=Rs({path:t}),a="",u=!1,c=f=>{u||(u=!0,clearTimeout(l),o.destroy(),f())},l=setTimeout(()=>{c(()=>{i(new Error(`ToolNet service timeout after ${r}ms`))})},r);o.setEncoding("utf8"),o.once("connect",()=>{o.write(`${JSON.stringify(s)}
22
- `)}),o.on("data",f=>{a+=f;let d=a.indexOf(`
23
- `);if(d===-1)return;let m=a.slice(0,d).trim();c(()=>{try{n(JSON.parse(m))}catch(g){i(g instanceof Error?g:new Error(String(g)))}})}),o.once("error",f=>{c(()=>{i(f)})}),o.once("end",()=>{u||c(()=>{i(new Error("ToolNet service closed without response"))})})})}async function Pt(s,e={}){try{let t=await Ts({type:"invalidate",project:s},{timeoutMs:250,...e});return t.ok&&t.type==="invalidate"}catch{return!1}}var D=new te().detect(),He=We(D.rootPath,".toolnet"),Ke=We(He,"index.lock"),Ls=We(He,"last-index.json"),Be=process.argv.includes("--json"),Os=[{id:"source-index",title:"Source Index"},{id:"type-resolution",title:"Resolving refs"},{id:"rich-graph",title:"Building rich graph"},{id:"semantic-index",title:"Semantic index"},{id:"architecture",title:"Architecture intelligence"},{id:"analysis",title:"Graph analysis"},{id:"visualization",title:"Visualization dataset"}],A=new Me({rootPath:D.rootPath,stages:Os,enabled:!Be});As(He,{recursive:!0});function Ae(s){js(Ls,JSON.stringify(s,null,2)+`
24
- `,{encoding:"utf8",mode:384})}var Ue;try{Ue=Is(Ke,"wx",384)}catch{console.error("\u274C Index already running for this project."),console.error(`Lock: ${Ke}`),process.exit(2)}var Ie=new Date().toISOString(),Z=[];try{A.start(),Ae({version:2,state:"running",startedAt:Ie,completedStages:Z,project:D});let s=await xt(D,{onSourceProgress:e=>{if(e.phase==="scan"){A.scanningComplete(e.total);return}A.parsingProgress(e.current,e.total)},onStage:async e=>{e.state==="start"?A.startStage(e.id,e.title):(Z.push(e.id),A.completeStage(e.id,e.title,e.durationMs??0)),Ae({version:2,state:"running",startedAt:Ie,currentStage:e.id,completedStages:[...Z],project:D})},onStageProgress:e=>{A.stageProgress({stage:e.stage,current:e.current,total:e.total,phase:e.phase,detail:e.detail})}});Ae({version:2,state:"complete",startedAt:Ie,finishedAt:new Date().toISOString(),completedStages:Z,result:s}),await Pt(D),Be?console.log(JSON.stringify(s,null,2)):await A.finish({files:s.files,symbols:s.graph.symbols,edges:s.graph.edges,durationMs:s.durationMs,storage:s.storage})}catch(s){let e=s instanceof Error?s.message:String(s);Ae({version:2,state:"failed",startedAt:Ie,failedAt:new Date().toISOString(),completedStages:Z,error:e}),A.fail(`Full index failed: ${e}`),Be&&console.error(e),process.exitCode=1}finally{Ue!==void 0&&Ms(Ue),Ns(Ke,{force:!0})}
21
+ `)}terminalWidth(){let e=this.stream.columns;if(typeof e=="number"&&Number.isFinite(e))return ur(Math.floor(e),32,240);let t=process.stdout.columns;return typeof t=="number"&&Number.isFinite(t)?ur(Math.floor(t),32,240):80}color(e,t){return this.colorEnabled?`${t}${e}${R.reset}`:e}writeStatic(e){this.stream.write(`${e}
22
+ `)}};import{createConnection as zs}from"node:net";import{homedir as Gs}from"node:os";import{join as Ks}from"node:path";function pr(){return process.env.TOOLNET_SERVICE_SOCKET??Ks(Gs(),".toolnet","run","service.sock")}function Bs(n,e={}){let t=e.socketPath??pr(),r=e.timeoutMs??1e3;return new Promise((s,i)=>{let o=zs({path:t}),a="",d=!1,c=m=>{d||(d=!0,clearTimeout(l),o.destroy(),m())},l=setTimeout(()=>{c(()=>{i(new Error(`ToolNet service timeout after ${r}ms`))})},r);o.setEncoding("utf8"),o.once("connect",()=>{o.write(`${JSON.stringify(n)}
23
+ `)}),o.on("data",m=>{a+=m;let u=a.indexOf(`
24
+ `);if(u===-1)return;let f=a.slice(0,u).trim();c(()=>{try{s(JSON.parse(f))}catch(g){i(g instanceof Error?g:new Error(String(g)))}})}),o.once("error",m=>{c(()=>{i(m)})}),o.once("end",()=>{d||c(()=>{i(new Error("ToolNet service closed without response"))})})})}async function mr(n,e={}){try{let t=await Bs({type:"invalidate",project:n},{timeoutMs:250,...e});return t.ok&&t.type==="invalidate"}catch{return!1}}var H=new fe().detect(),ft=mt(H.rootPath,".toolnet"),dt=mt(ft,"index.lock"),qs=mt(ft,"last-index.json"),ut=process.argv.includes("--json"),Js=[{id:"source-index",title:"Source Index"},{id:"type-resolution",title:"Resolving refs"},{id:"rich-graph",title:"Building rich graph"},{id:"semantic-index",title:"Local code search \u2014 FTS5/BM25"},{id:"architecture",title:"Architecture intelligence"},{id:"analysis",title:"Graph analysis"},{id:"visualization",title:"Visualization dataset"}],N=new Be({rootPath:H.rootPath,stages:Js,enabled:!ut});Hs(ft,{recursive:!0});function Ue(n){Vs(qs,JSON.stringify(n,null,2)+`
25
+ `,{encoding:"utf8",mode:384})}var pt;try{pt=Ws(dt,"wx",384)}catch{console.error("\u274C Index already running for this project."),console.error(`Lock: ${dt}`),process.exit(2)}var He=new Date().toISOString(),le=[];try{N.start(),Ue({version:2,state:"running",startedAt:He,completedStages:le,project:H});let n=await ar(H,{onSourceProgress:e=>{if(e.phase==="scan"){N.scanningComplete(e.total);return}N.parsingProgress(e.current,e.total)},onStage:async e=>{e.state==="start"?N.startStage(e.id,e.title):(le.push(e.id),N.completeStage(e.id,e.title,e.durationMs??0)),Ue({version:2,state:"running",startedAt:He,currentStage:e.id,completedStages:[...le],project:H})},onStageProgress:e=>{N.stageProgress({stage:e.stage,current:e.current,total:e.total,phase:e.phase,detail:e.detail})}});Ue({version:2,state:"complete",startedAt:He,finishedAt:new Date().toISOString(),completedStages:le,result:n}),await mr(H),ut?console.log(JSON.stringify(n,null,2)):await N.finish({files:n.files,symbols:n.graph.symbols,edges:n.graph.edges,durationMs:n.durationMs,storage:n.storage})}catch(n){let e=n instanceof Error?n.message:String(n);Ue({version:2,state:"failed",startedAt:He,failedAt:new Date().toISOString(),completedStages:le,error:e}),N.fail(`Full index failed: ${e}`),ut&&console.error(e),process.exitCode=1}finally{pt!==void 0&&Us(pt),Ys(dt,{force:!0})}