import{spawnSync as d}from"node:child_process";import{existsSync as s,mkdirSync as c,readdirSync as h,writeFileSync as m}from"node:fs";import{createHash as u}from"node:crypto";import{join as n}from"node:path";import{SKILL_META as y}from"@zibby/skill-ids";function i(){return process.env.CBM_BIN||"/usr/local/bin/codebase-memory-mcp"}function a(){if(process.env.CBM_CACHE_DIR)return process.env.CBM_CACHE_DIR;let e=process.env.WORKSPACE||process.env.ZIBBY_WORKSPACE;return e?n(e,".zibby","cbm-cache"):"/tmp/zibby-cbm-cache"}function l(){let e=process.env.WORKSPACE||process.env.ZIBBY_WORKSPACE||"/workspace",r=n(e,".zibby","repos");try{if(s(r)){let t=h(r,{withFileTypes:!0}).filter(o=>o.isDirectory()).map(o=>n(r,o.name));if(t.length===1)return t[0];if(t.length>1)return r}}catch{}return s(e)?e:null}function _(e){return u("sha256").update(e).digest("hex").slice(0,16)}var x={id:"codebase-memory",serverName:"codebase_memory",meta:y["codebase-memory"],allowedTools:["mcp__codebase_memory__*"],description:"Codebase memory \u2014 code-graph + semantic index over the checked-out repo (architecture, graph search, dependency trace, change detection)",promptFragment:`## Codebase Memory (code-graph + semantic index over THIS repo)
The checked-out repository is indexed into a queryable code graph + semantic
index. Reach for these instead of blindly grepping when you need structure,
relationships, or "where does X live / what depends on Y":
- get_architecture: high-level architecture / module map \u2014 START HERE to orient.
- search_graph: semantic-ish search over the code graph (symbols, files, concepts).
- query_graph: structured query against the graph schema (use get_graph_schema first).
- trace_path: trace a dependency / call path between two nodes (impact analysis).
- detect_changes: what changed vs the indexed baseline \u2014 scope your work.
- get_code_snippet / search_code: pull the exact code for a node / text match.
- index_status / list_projects: confirm the index is present before querying.
The repo is indexed for you at the start of the run; if a query comes back
empty, call index_status, and only re-index (index_repository) if needed.`,resolve(){let e=a();try{c(e,{recursive:!0})}catch{}let r={CBM_CACHE_DIR:e};return process.env.WORKSPACE&&(r.WORKSPACE=process.env.WORKSPACE),{type:"stdio",command:i(),args:[],env:r,description:this.description,alwaysLoad:!0}},invokeAgentOptions(){try{let e=l();if(!e)return{};let r=a();try{c(r,{recursive:!0})}catch{}let t=n(r,`.cbm-indexed-${_(e)}`);if(s(t))return{};let o=i();if(!s(o)&&!process.env.CBM_BIN)return{};let p=d(o,["cli","index_repository",JSON.stringify({repo_path:e})],{env:{...process.env,CBM_CACHE_DIR:r},encoding:"utf-8",timeout:300*1e3,maxBuffer:32*1024*1024});try{m(t,`${new Date().toISOString()} status=${p.status}
`)}catch{}}catch{}return{}}};export{x as codebaseMemorySkill};
