import{n as e}from"./chunk-DR8-3Aex.js";import{n as t,s as n}from"./envUtils-BWeoiL4Y.js";import{o as r,t as i}from"./localSearch-B0p0_bjP.js";import{l as a,u as o}from"./instinctParser-DOtj1Kni.js";import{basename as s,dirname as c,join as l}from"node:path";import{existsSync as u}from"node:fs";import{mkdir as d,readFile as f,readdir as p,rename as ee,rm as m,writeFile as h}from"node:fs/promises";function g(e){return e.length===0?!1:e.reduce((e,t)=>e+t.confidence,0)/e.length>=a().minConfidence}function _(e){let t=b[e[0]?.domain??`project`],n=new Set;for(let t of e){for(let e of`${t.trigger} ${t.action}`.toLowerCase().split(/[^a-z0-9]+/))if(te(e)&&n.add(e),n.size>=5)break;if(n.size>=5)break}let r=v([t,...n].join(`-`));return y(r)?`${t}-learned-pattern`:r}function v(e){return e.toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-|-$/g,``).slice(0,64).replace(/-$/g,``)||`learned-skill`}function y(e){return x.has(e)}function te(e){return e.length>2&&![`when`,`with`,`this`,`that`,`user`,`project`,`prefer`,`avoid`,`use`,`using`,`the`,`and`,`for`].includes(e)}var b,x,S=e((()=>{o(),b={workflow:`workflow`,testing:`testing`,debugging:`debugging`,"code-style":`style`,security:`security`,git:`git`,project:`project`},x=new Set([`learned-skill`,`better-skill`,`new-skill`,`project-skill`,`workflow-skill`])}));async function C(e,t,n){let r=n.length>0&&typeof n[0]==`string`?await w(e,n):n,i=L(`${t.name} ${t.description} ${t.content}`);return r.map(e=>({skill:e,score:R(i,L(`${e.name} ${e.description} ${e.content}`))})).filter(e=>e.score>=.18).sort((e,t)=>t.score-e.score).map(e=>e.skill)}async function ne(e,t){return C(`skill`,e,t)}async function w(e,t){if(e===`skill`)return D(t);let n=[];for(let e of t)u(e)&&await P(e,n);return n}function T(e,t,n={}){let r=t.find(e=>j(e));if(n.allowHardDelete&&r)return{type:`delete`,targetSkill:r,reason:`Existing skill is low quality, unreferenced, and safe to delete.`,confirmed:!0};let i=t[0];if(!i)return{type:`create`,draft:e,reason:`No overlapping active skill found.`};let a=R(L(`${e.name} ${e.description} ${e.content}`),L(`${i.name} ${i.description} ${i.content}`));return a>=.72&&e.confidence>=.75&&ae(e,i)?{type:`replace`,targetSkill:i,draft:e,reason:`New learned skill has high overlap (${a.toFixed(2)}) and higher confidence.`}:a>=.35?{type:`merge`,targetSkill:i,patch:ie(e),reason:`Existing skill overlaps with the learned pattern (${a.toFixed(2)}).`}:{type:`create`,draft:e,reason:`Overlap is too low to merge.`}}async function E(e,t={}){switch(e.type){case`create`:return{activePath:await W(e.draft)};case`merge`:return I(e.targetSkill)?{activePath:await re(e.targetSkill,e.patch)}:(process.stderr.write(`[skill-learning] skip user-authored skill: ${e.targetSkill.path}\n`),{});case`replace`:{if(!I(e.targetSkill))return process.stderr.write(`[skill-learning] skip user-authored skill: ${e.targetSkill.path}\n`),{};let n=e.draft.outputPath;if(e.hardDelete){let{deletedPath:r,manifestPath:i,tombstonePath:a}=await k(e.targetSkill,e.reason,{newSkill:e.draft.name,newPath:n},{...t,allowHardDelete:!0});return{activePath:await W(e.draft),deletedPath:r,manifestPath:i,tombstonePath:a}}let{archivedPath:r,manifestPath:i}=await O(e.targetSkill,e.reason,{newSkill:e.draft.name,newPath:n},t);return{activePath:await W(e.draft),archivedPath:r,manifestPath:i}}case`archive`:return await O(e.targetSkill,e.reason,void 0,t);case`delete`:return await k(e.targetSkill,e.reason,void 0,{...t,allowHardDelete:t.allowHardDelete&&e.confirmed!==!1})}}async function D(e){let t=[];for(let n of e)u(n)&&await N(n,t);return t}async function O(e,t,n,r={}){let a=c(e.path),o=r.archiveRoot??l(c(a),`.archive`),u=l(o,`${s(a)}-${M(r.now)}`);await d(o,{recursive:!0}),await ee(a,u);let f=await A(r.manifestRoot??u,{oldSkill:e.name,oldPath:e.path,newSkill:n?.newSkill,newPath:n?.newPath,action:`archive`,reason:t,replacedAt:(r.now??new Date).toISOString(),recoverable:!0});return i(),{archivedPath:u,manifestPath:f}}async function k(e,t,n,r={}){if(!r.allowHardDelete)throw Error(`Hard delete requires allowHardDelete=true`);let a=c(e.path),o=u(e.path)?await f(e.path,`utf8`):``,s=r.manifestRoot??l(c(a),`.tombstones`),d=await A(s,{oldSkill:e.name,oldPath:e.path,newSkill:n?.newSkill,newPath:n?.newPath,action:`delete`,reason:t,replacedAt:(r.now??new Date).toISOString(),recoverable:!1}),p=l(s,`${e.name}-${M(r.now)}.tombstone.json`);return await h(p,`${JSON.stringify({deletedSkill:e.name,oldPath:e.path,content:o},null,2)}\n`,`utf8`),await m(a,{recursive:!0,force:!0}),i(),{deletedPath:e.path,manifestPath:d,tombstonePath:p}}async function A(e,t){await d(e,{recursive:!0});let n=l(e,`replacement-manifest.json`);return await h(n,`${JSON.stringify(t,null,2)}\n`,`utf8`),n}async function re(e,t){let n=l(c(e.path),`learned-skill.patch.md`);return await h(n,t,`utf8`),i(),n}function ie(e){return[`# Learned Skill Merge Patch`,``,`Target learned skill: ${e.name}`,`Confidence: ${e.confidence}`,``,`## Suggested additions`,``,e.content].join(`
`)}function ae(e,t){if(t.status===`superseded`||t.status===`archived`)return!0;let n=e.confidence-(t.confidence??.5),r=e.content.length-t.content.length;return n>=.15||r>160}function j(e){return e.safeToDelete===!0&&(e.referencedBy?.length??0)===0&&e.quality===`low`}function M(e=new Date){return e.toISOString().replace(/[:.]/g,`-`)}async function N(e,t){let n=await p(e,{withFileTypes:!0});for(let r of n){let n=l(e,r.name);if(r.isDirectory()){if(r.name===`.archive`)continue;await N(n,t);continue}if(r.isFile()&&r.name===`SKILL.md`){let e=await f(n,`utf8`);t.push({name:F(e,`name`)??s(c(n)),description:F(e,`description`)??``,path:n,content:e})}}}async function P(e,t){let n=await p(e,{withFileTypes:!0});for(let r of n){let n=l(e,r.name);if(r.isDirectory()){if(r.name===`.archive`)continue;await P(n,t);continue}if(r.isFile()&&r.name.endsWith(`.md`)){let e=await f(n,`utf8`);t.push({name:F(e,`name`)??r.name.replace(/\.md$/,``),description:F(e,`description`)??``,path:n,content:e})}}}function F(e,t){let n=e.match(/^---\r?\n([\s\S]*?)\r?\n---/);if(n)return n[1].match(RegExp(`^${t}:\\s*"?([^"\\n]+)"?`,`m`))?.[1]?.trim()}function I(e){return F(e.content,`origin`)===`skill-learning`}function L(e){return new Set(e.toLowerCase().split(/[^a-z0-9]+/).filter(e=>e.length>2))}function R(e,t){if(e.size===0||t.size===0)return 0;let n=0;for(let r of e)t.has(r)&&n++;return n/Math.min(e.size,t.size)}function z(e,t){return R(L(`${e.name} ${e.description} ${e.content}`),L(`${t.name} ${t.description} ${t.content}`))}var B=e((()=>{r(),$()}));function V(e,t){if(e.length===0)throw Error(`Cannot generate a skill draft without instincts`);let n=t?.scope??e[0]?.scope??`project`,r=t?.name?v(t.name):K(e),i=e.reduce((e,t)=>e+t.confidence,0)/e.length,a=t?.description??q(e),o=G(r,n,t),s=J({name:r,description:a,confidence:i,instincts:e});return{name:r,description:a,scope:n,sourceInstinctIds:e.map(e=>e.id),confidence:Number(i.toFixed(2)),content:s,outputPath:o}}async function H(e,t,n){let r=V(e,t),i=await C(`skill`,r,n);for(let t of i){let n=z(r,t);if(n>=.8)return{action:`append-evidence`,target:t,overlap:n,appendedPath:await U(t,e)}}return{action:`create`,draft:r}}async function U(e,t){let n=await f(e.path,`utf8`).catch(()=>e.content);if(Buffer.byteLength(n,`utf8`)>=Q)return e.path;let r=t.flatMap(e=>e.evidence.map(e=>`- ${e}`)),a=r.slice(0,X);a.length<r.length&&a.push(`- [... ${r.length-a.length} more evidence entries omitted]`);let o=[``,`## Learned evidence (${new Date().toISOString()})`,``,...a,``].join(`
`),s=n.endsWith(`
`)?n+o:`${n}\n${o}`,c=Buffer.byteLength(s,`utf8`)>Q?s.slice(0,Q):s;return await h(e.path,c,`utf8`),i(),e.path}async function W(e){await d(e.outputPath,{recursive:!0});let t=l(e.outputPath,`SKILL.md`);await h(t,e.content,`utf8`),i();try{let{clearCommandsCache:e}=await import(`./commands-CfwfGt3T.js`);e()}catch{}return t}function G(e,n,r){return r?.outputRoot?l(r.outputRoot,e):n===`project`?l(r?.cwd??process.cwd(),`.claude`,`skills`,e):l(r?.globalSkillsDir??l(t(),`skills`),e)}function K(e){return _(e)}function q(e){let t=(e[0]?.action??`Apply a learned project pattern`).replace(/\s+/g,` `).slice(0,120);return t.length>0?t:`Apply learned project patterns`}function J(e){let{name:t,description:n,confidence:r,instincts:i}=e;return[`---`,`name: ${t}`,`description: ${JSON.stringify(n)}`,`origin: skill-learning`,`confidence: ${Number(r.toFixed(2))}`,`evolved_from: [${i.map(e=>JSON.stringify(e.id)).join(`, `)}]`,`---`,``,`# ${oe(t)}`,``,`## Trigger`,``,i.map(e=>`- ${e.trigger}`).join(`
`),``,`## Action`,``,i.map(e=>`- ${e.action}`).join(`
`),``,`## Evidence`,``,i.flatMap(e=>e.evidence.map(e=>`- ${e}`)).slice(0,Z).join(`
`),``].join(`
`)}function oe(e){return e.split(`-`).filter(Boolean).map(e=>e[0]?.toUpperCase()+e.slice(1)).join(` `)}var Y,X,Z,Q,$=e((()=>{n(),r(),S(),B(),Y=.8,X=20,Z=20,Q=5e4}));export{g as C,v as S,z as _,G as a,S as b,E as c,ne as d,T as f,D as g,w as h,V as i,O as l,B as m,U as n,$ as o,k as p,H as r,W as s,Y as t,C as u,A as v,y as x,_ as y};