#!/usr/bin/env node
import'./chunk-XHJ27CER.js';import*as s from'fs';import*as y from'path';import t from'chalk';import*as u from'js-yaml';var f=".paradigm/history";async function v(o,r={}){let n=process.cwd(),c=y.join(n,f);if(!s.existsSync(c)){console.log(t.yellow("No history directory found.")),console.log(t.gray("Run `paradigm history init` to create .paradigm/history/"));return}let e=x(y.join(c,"index.yaml")),i=w(y.join(c,"log.jsonl"));if(r.json){if(o){let l=e?.by_symbol?.[o],g=i.filter(a=>a.symbols.includes(o));console.log(JSON.stringify({symbol:o,summary:l,entries:g},null,2));}else console.log(JSON.stringify({index:e,entries:i.slice(-(r.limit||20))},null,2));return}if(console.log(t.magenta(`
  History
`)),o){let l=e?.by_symbol?.[o],g=i.filter(a=>a.symbols.includes(o)).slice(-(r.limit||10));if(console.log(t.cyan(`  Symbol: ${o}
`)),l){let a=l.fragility==="critical"||l.fragility==="high"?t.red:l.fragility==="medium"?t.yellow:t.green;console.log(t.white("  Summary:")),console.log(t.gray(`    Total changes: ${l.total_changes}`)),console.log(t.gray(`    Last modified: ${l.last_modified}`)),console.log(t.gray(`    Stability: ${(l.stability_score*100).toFixed(0)}%`)),console.log(a(`    Fragility: ${l.fragility}`)),console.log();}g.length>0&&(console.log(t.white("  Recent changes:")),g.forEach(a=>{let p=a.type==="rollback"?t.red:a.type==="validate"?a.result==="pass"?t.green:t.red:t.blue;console.log(p(`    [${a.type}] ${a.description||a.id}`)+t.gray(` - ${a.author.id} @ ${a.ts.split("T")[0]}`));}));return}let m=Object.keys(e?.by_symbol||{});console.log(t.white(`  Tracked symbols: ${m.length}`)),console.log(t.white(`  Total entries: ${i.length}`)),console.log();let h=e?.fragile_symbols||[];h.length>0&&(console.log(t.red("  Fragile symbols:")),h.slice(0,5).forEach(l=>{console.log(t.gray(`    [${l.fragility}] ${l.symbol}`));}),console.log());let b=i.slice(-10);b.length>0&&(console.log(t.white("  Recent entries:")),b.reverse().forEach(l=>{let g=l.type==="rollback"?t.red:l.type==="validate"?l.result==="pass"?t.green:t.red:t.blue;console.log(g(`    [${l.type}] ${l.symbols.join(", ")}`)+t.gray(` - ${l.ts.split("T")[0]}`));}));}async function H(o={}){let r=process.cwd(),n=y.join(r,f);if(s.existsSync(n)&&!o.force){console.log(t.yellow("History directory already exists.")),console.log(t.gray("Use --force to reinitialize"));return}s.mkdirSync(n,{recursive:true}),s.writeFileSync(y.join(n,"log.jsonl"),"");let c={version:"1.0",generated:new Date().toISOString(),by_symbol:{},co_changes:[],fragile_symbols:[]};s.writeFileSync(y.join(n,"index.yaml"),u.dump(c,{lineWidth:-1}));let e={version:"1.0",total_validations:0,pass_rate:0,by_symbol:{}};s.writeFileSync(y.join(n,"validation.yaml"),u.dump(e,{lineWidth:-1})),console.log(t.green("History directory initialized!")),console.log(t.gray(`  ${n}/`)),console.log(t.gray("    log.jsonl")),console.log(t.gray("    index.yaml")),console.log(t.gray("    validation.yaml"));}async function E(o={}){let r=process.cwd(),n=y.join(r,f,"index.yaml");if(!s.existsSync(n)){console.log(t.yellow("No history index found.")),console.log(t.gray("Run `paradigm history reindex` to generate"));return}let e=x(n)?.fragile_symbols||[];if(o.json){console.log(JSON.stringify({fragile:e},null,2));return}if(console.log(t.magenta(`
  Fragile Symbols
`)),e.length===0){console.log(t.green("  No fragile symbols detected!"));return}e.forEach(i=>{let m=i.fragility==="critical"?t.red:t.yellow;console.log(m(`  [${i.fragility}] ${i.symbol}`)),console.log(t.gray(`    ${i.reason}`));}),console.log(),console.log(t.gray("  Recommendation: Add extra test coverage before modifying these symbols"));}async function F(){let o=process.cwd(),r=y.join(o,f),n=y.join(r,"log.jsonl");if(!s.existsSync(n)){console.log(t.yellow("No history log found."));return}let c=w(n),e=_(c),i=y.join(r,"index.yaml");s.writeFileSync(i,u.dump(e,{lineWidth:-1})),console.log(t.green("History index regenerated!")),console.log(t.gray(`  Symbols: ${Object.keys(e.by_symbol).length}`)),console.log(t.gray(`  Entries: ${c.length}`)),console.log(t.gray(`  Fragile: ${e.fragile_symbols.length}`));}async function O(o){let r=process.cwd(),n=y.join(r,f,"log.jsonl");s.mkdirSync(y.join(r,f),{recursive:true});let c=1;s.existsSync(n)&&(c=s.readFileSync(n,"utf8").split(`
`).filter(m=>m.trim()).length+1);let e={id:`h${String(c).padStart(4,"0")}`,ts:new Date().toISOString(),type:o.type,symbols:o.symbols.split(",").map(i=>i.trim()),author:{type:"human",id:process.env.USER||"unknown"},description:o.description,intent:o.intent,commit:o.commit,reason:o.reason};s.appendFileSync(n,JSON.stringify(e)+`
`),console.log(t.green(`Entry ${e.id} recorded!`));}async function k(o){let r=process.cwd(),n=y.join(r,f,"log.jsonl");s.mkdirSync(y.join(r,f),{recursive:true});let c=1;s.existsSync(n)&&(c=s.readFileSync(n,"utf8").split(`
`).filter(m=>m.trim()).length+1);let e={id:`h${String(c).padStart(4,"0")}`,ts:new Date().toISOString(),type:"validate",symbols:[],author:{type:"agent",id:"cli"},ref:o.ref,result:o.result,tests:o.passed!==void 0?{passed:o.passed,failed:o.failed||0}:void 0};s.appendFileSync(n,JSON.stringify(e)+`
`),console.log(t.green(`Validation ${e.id} recorded: ${o.result}`));}function x(o){if(!s.existsSync(o))return null;try{let r=s.readFileSync(o,"utf8");return u.load(r)}catch{return null}}function w(o){return s.existsSync(o)?s.readFileSync(o,"utf8").split(`
`).filter(n=>n.trim()).map(n=>{try{return JSON.parse(n)}catch{return null}}).filter(Boolean):[]}function _(o){let r={},n={};for(let e of o)for(let i of e.symbols)n[i]||(n[i]=[]),n[i].push(e);for(let[e,i]of Object.entries(n)){let m=i.sort((d,j)=>new Date(j.ts).getTime()-new Date(d.ts).getTime()),h=i.filter(d=>d.type==="rollback").length,b=i.filter(d=>d.type==="validate"&&d.result==="fail").length,l=i.length,g=Math.max(0,1-(h*.2+b*.1)/Math.max(1,l/10)),a="low";g<.5?a="critical":g<.7?a="high":g<.85&&(a="medium");let p=new Set,S=new Set;for(let d of i)d.author.type==="human"?p.add(d.author.id):S.add(d.author.id);r[e]={symbol:e,total_changes:l,last_modified:m[0]?.ts||new Date().toISOString(),stability_score:g,fragility:a,recent:m.slice(0,5),contributors:{human:Array.from(p),agent:Array.from(S)}};}let c=Object.values(r).filter(e=>e.fragility==="high"||e.fragility==="critical").map(e=>({symbol:e.symbol,fragility:e.fragility,reason:`Stability score: ${(e.stability_score*100).toFixed(0)}%`}));return {version:"1.0",generated:new Date().toISOString(),by_symbol:r,co_changes:[],fragile_symbols:c}}export{E as historyFragileCommand,H as historyInitCommand,O as historyRecordCommand,F as historyReindexCommand,v as historyShowCommand,k as historyValidateCommand};