#!/usr/bin/env node
import {c,a}from'./chunk-EMMMBAID.js';import {b as b$1,a as a$1}from'./chunk-F5BSUC2L.js';import*as s from'fs';import*as o from'path';import*as v from'os';import*as d from'crypto';c();b$1();function $(t){return t.contentType!==void 0&&a.includes(t.contentType)}function ct(t,n,e){try{let r={id:d.randomUUID(),timestamp:new Date().toISOString(),agent:t,boundary:e,data_category:n.contentType,action:"blocked",destination_ring:"network-public",details:n.id},i=o.join(v.homedir(),".paradigm","events","audit-ring1.jsonl");I(i,r);}catch(r){a$1.component("#symphony-loader").warn(`Ring 1 audit write failed: ${r}`);}}var g=o.join(v.homedir(),".paradigm","score"),N=o.join(v.homedir(),".paradigm","mail"),h=o.join(g,"agents"),S=o.join(g,"threads"),m=o.join(g,"file-requests"),E=o.join(g,"trust.yaml"),X=3600*1e3,b={users:{},defaults:{level:"restricted",autoApprove:[],neverApprove:[".env*","**/*.key","**/*.pem","**/credentials*","**/secrets/**"]}};function tt(){if(s.existsSync(N)&&!s.existsSync(g))try{s.renameSync(N,g);}catch{}}function p(){tt();for(let t of [h,S,m])s.existsSync(t)||s.mkdirSync(t,{recursive:true});}var ut=p;function f(t){return o.join(h,t)}function R(t){let n=f(t);return s.existsSync(n)||s.mkdirSync(n,{recursive:true}),n}function x(t){if(!s.existsSync(t))return [];let e=s.readFileSync(t,"utf-8").split(`
`).filter(i=>i.trim().length>0),r=[];for(let i of e)try{r.push(JSON.parse(i));}catch{}return r}function I(t,n){let e=o.dirname(t);s.existsSync(e)||s.mkdirSync(e,{recursive:true}),s.appendFileSync(t,JSON.stringify(n)+`
`,"utf-8");}function C(t){return t.toLowerCase().replace(/[^a-z0-9-]/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"").slice(0,40)||"unknown"}function _(t){try{let n=o.join(t,".paradigm","config.yaml");if(s.existsSync(n)){let r=s.readFileSync(n,"utf-8").match(/^project:\s*(.+)$/m);if(r)return C(r[1].trim().replace(/["']/g,""))}}catch{}return C(o.basename(t))}function z(t,n){return `${_(t)}/${n||"core"}`}function ft(t,n,e){p();let r=z(t,n),i=R(r),a=_(t),c={id:r,name:e||`${a} (${n||"core"})`,type:"agent",project:a,role:n||"core",pid:process.pid,startedAt:new Date().toISOString(),label:e};return s.writeFileSync(o.join(i,"identity.json"),JSON.stringify(c,null,2),"utf-8"),c}function et(t){let n=f(t);if(!s.existsSync(n))return  false;try{return s.rmSync(n,{recursive:!0,force:!0}),!0}catch{return  false}}function P(){if(p(),!s.existsSync(h))return [];let t=[],n=s.readdirSync(h,{withFileTypes:true}).filter(e=>e.isDirectory());for(let e of n){let r=o.join(h,e.name),i=s.readdirSync(r,{withFileTypes:true}).filter(a=>a.isDirectory());for(let a of i){let c=o.join(r,a.name,"identity.json");if(s.existsSync(c))try{let u=s.readFileSync(c,"utf-8"),l=JSON.parse(u);t.push(l);}catch{}}}return t}function lt(){let t=P(),n=0;for(let e of t)F(e.pid)||(et(e.id),n++);return n}function dt(t){let n=z(t),e=o.join(f(n),"identity.json");if(!s.existsSync(e))return null;try{return JSON.parse(s.readFileSync(e,"utf-8"))}catch{return null}}function pt(t,n){let e=o.join(f(t),"identity.json");if(s.existsSync(e))try{let r=JSON.parse(s.readFileSync(e,"utf-8"));r.lastPoll=new Date().toISOString(),n!==void 0&&(r.statusBlurb=n||void 0),s.writeFileSync(e,JSON.stringify(r,null,2),"utf-8");}catch{}}function gt(t,n){let e=o.join(f(t),"identity.json");if(s.existsSync(e))try{let r=JSON.parse(s.readFileSync(e,"utf-8"));r.statusBlurb=n||void 0,s.writeFileSync(e,JSON.stringify(r,null,2),"utf-8");}catch{}}function yt(t,n=6e4){if(!t.lastPoll)return  true;let e=new Date(t.lastPoll).getTime();return Date.now()-e>n}function ht(){let n=P().filter(e=>F(e.pid));try{let e=o.join(v.homedir(),".conductor","sessions");if(s.existsSync(e)){let r=s.readdirSync(e).filter(i=>i.endsWith(".json"));for(let i of r)try{let a=s.readFileSync(o.join(e,i),"utf-8"),c=JSON.parse(a),u=parseInt(o.basename(i,".json"),10);!n.some(l=>l.pid===u)&&F(u)&&n.push({id:`conductor/${u}`,name:c.label||`Session ${u}`,type:"agent",project:c.projectDir?o.basename(c.projectDir):"unknown",role:"conductor",pid:u,startedAt:c.registeredAt||new Date().toISOString()});}catch{}}}catch{}return n}function y(t){return o.join(f(t),"inbox.jsonl")}function j(t){return o.join(f(t),"outbox.jsonl")}function B(t){return o.join(f(t),"ack.json")}function St(t){let n=y(t);if(!s.existsSync(n))return {hasNew:false,inboxSize:0};let r=s.statSync(n).size;if(!k(t))return {hasNew:r>0,inboxSize:r};let a=o.join(f(t),"ack-size.json");if(s.existsSync(a))try{let c=JSON.parse(s.readFileSync(a,"utf-8"));return {hasNew:r>(c.size||0),inboxSize:r}}catch{}return {hasNew:r>0,inboxSize:r}}function mt(t){let n=y(t),e=o.join(f(t),"ack-size.json");try{let r=s.existsSync(n)?s.statSync(n).size:0;R(t),s.writeFileSync(e,JSON.stringify({size:r}),"utf-8");}catch{}}function J(t,n){R(t),I(y(t),n);}function xt(t,n){let e=x(y(t));if(!n){let i=k(t);if(i){let a=e.findIndex(c=>c.id===i);if(a>=0)return e.slice(a+1)}return e}let r=e.findIndex(i=>i.id===n);return r>=0?e.slice(r+1):e}function nt(t,n){R(t),I(j(t),n);}function rt(t){return x(j(t))}function vt(t){return rt(t).filter(n=>!$(n))}function Rt(t,n){let e=B(t);R(t),s.writeFileSync(e,JSON.stringify({lastAck:n}),"utf-8");}function k(t){let n=B(t);if(!s.existsSync(n))return null;try{return JSON.parse(s.readFileSync(n,"utf-8")).lastAck||null}catch{return null}}function At(t){let n=k(t);if(!n)return 0;let e=y(t),r=x(e),i=r.findIndex(u=>u.id===n);if(i<0)return 0;let a=r.slice(i+1),c=r.length-a.length;return a.length===0?s.existsSync(e)&&s.writeFileSync(e,"","utf-8"):s.writeFileSync(e,a.map(u=>JSON.stringify(u)).join(`
`)+`
`,"utf-8"),c}function q(t){return o.join(S,`${t}.json`)}function Tt(t,n){p();let e="thr-"+d.randomBytes(4).toString("hex"),r=new Date().toISOString(),i={id:e,topic:t,initiator:n,participants:[n],status:"active",createdAt:r,lastActivity:r,messageCount:0};return s.writeFileSync(q(e),JSON.stringify(i,null,2),"utf-8"),i}function L(t){let n=q(t);if(!s.existsSync(n))return null;try{return JSON.parse(s.readFileSync(n,"utf-8"))}catch{return null}}function Pt(t){if(p(),!s.existsSync(S))return [];let n=s.readdirSync(S).filter(r=>r.endsWith(".json")),e=[];for(let r of n)try{let i=s.readFileSync(o.join(S,r),"utf-8"),a=JSON.parse(i);(!t||a.status===t)&&e.push(a);}catch{}return e.sort((r,i)=>i.lastActivity.localeCompare(r.lastActivity))}function U(t,n){let e=L(t);if(!e)return  false;let r={...e,...n};return s.writeFileSync(q(t),JSON.stringify(r,null,2),"utf-8"),true}function bt(t,n){return U(t,{status:"resolved",resolvedAt:new Date().toISOString(),decision:n})}function Ft(t){let n=P(),e=[];for(let r of n){let i=x(y(r.id)),a=x(j(r.id));for(let c of [...i,...a])(c.threadRoot===t||c.id===t)&&!e.some(u=>u.id===c.id)&&!$(c)&&e.push(c);}return e.sort((r,i)=>r.timestamp.localeCompare(i.timestamp))}function G(t){return {id:d.randomUUID(),parentId:t.parentId,threadRoot:t.threadRoot,timestamp:new Date().toISOString(),sender:t.sender,recipients:t.recipients,intent:t.intent,content:{text:t.text,diff:t.diff,decision:t.decision},symbols:t.symbols||[],attachments:t.attachments,metadata:t.metadata}}function W(t){p(),nt(t.sender.id,t);let n=0;if(t.recipients&&t.recipients.length>0)for(let e of t.recipients)J(e.id,t),n++;else {let e=P();for(let r of e)r.id!==t.sender.id&&(J(r.id,t),n++);}if(t.threadRoot){let e=L(t.threadRoot);if(e){let i=e.participants.some(a=>a.id===t.sender.id)?e.participants:[...e.participants,t.sender];U(t.threadRoot,{participants:i,lastActivity:t.timestamp,messageCount:e.messageCount+1});}}return n}function A(t){return o.join(m,`${t}.json`)}function H(){if(!s.existsSync(E))return b;try{let t=s.readFileSync(E,"utf-8");try{return JSON.parse(t)}catch{return b}}catch{return b}}function It(t){p();let n="freq-"+d.randomBytes(4).toString("hex"),e={request:{requestId:n,filePath:t.filePath,reason:t.reason,requester:t.requester,urgency:t.urgency||"normal",snippet:t.snippet,threadRoot:t.threadRoot},status:"pending",createdAt:new Date().toISOString()};return s.writeFileSync(A(n),JSON.stringify(e,null,2),"utf-8"),e}function K(t){let n=A(t);if(!s.existsSync(n))return null;try{return JSON.parse(s.readFileSync(n,"utf-8"))}catch{return null}}function st(t){if(p(),!s.existsSync(m))return [];let n=s.readdirSync(m).filter(r=>r.endsWith(".json")),e=[];for(let r of n)try{let i=s.readFileSync(o.join(m,r),"utf-8"),a=JSON.parse(i);(!t||a.status===t)&&e.push(a);}catch{}return e.sort((r,i)=>i.createdAt.localeCompare(r.createdAt))}function jt(t,n,e){let r=K(t);if(!r)return {success:false,error:`File request not found: ${t}`};if(r.status!=="pending")return {success:false,error:`Request already ${r.status}`};let i=o.resolve(n,r.request.filePath);if(!i.startsWith(o.resolve(n)))return {success:false,error:"File path escapes project directory"};if(!s.existsSync(i))return {success:false,error:`File not found: ${r.request.filePath}`};try{let a=s.readFileSync(i,"utf-8"),c="utf8";if(e){let Y=[/(?:api[_-]?key|secret|token|password|credential|auth)\s*[:=]/i,/(?:^|\s)(?:export\s+)?[A-Z_]+(?:KEY|SECRET|TOKEN|PASSWORD|CREDENTIAL)\s*=/,/-----BEGIN (?:RSA |EC )?PRIVATE KEY-----/];a=a.split(`
`).map(O=>{for(let Q of Y)if(Q.test(O))return "[REDACTED]";return O}).join(`
`);}let u=d.createHash("sha256").update(a).digest("hex"),l={requestId:t,filePath:r.request.filePath,content:a,encoding:c,size:Buffer.byteLength(a),hash:u};r.status="approved",r.resolvedAt=new Date().toISOString(),r.delivery=l,s.writeFileSync(A(t),JSON.stringify(r,null,2),"utf-8");let M=G({sender:{id:"system",name:"File Transfer",type:"human"},recipients:[r.request.requester],intent:"fileDelivery",text:`File delivered: ${r.request.filePath} (${l.size} bytes, SHA-256: ${u.slice(0,12)}...)`,threadRoot:r.request.threadRoot,symbols:[]});return M.attachments=[{name:o.basename(r.request.filePath),type:"file",content:l.content,encoding:l.encoding}],W(M),{success:!0,delivery:l}}catch(a){return {success:false,error:`Failed to read file: ${a.message}`}}}function kt(t,n){let e=K(t);if(!e||e.status!=="pending")return  false;e.status="denied",e.resolvedAt=new Date().toISOString(),e.denyReason=n,s.writeFileSync(A(t),JSON.stringify(e,null,2),"utf-8");let r=G({sender:{id:"system",name:"File Transfer",type:"human"},recipients:[e.request.requester],intent:"fileDenied",text:`File request denied: ${e.request.filePath}${n?` \u2014 ${n}`:""}`,threadRoot:e.request.threadRoot,symbols:[]});return W(r),true}function T(t,n){let e=n.replace(/\./g,"\\.").replace(/\*\*/g,"{{GLOBSTAR}}").replace(/\*/g,"[^/]*").replace(/\?/g,"[^/]").replace(/\{\{GLOBSTAR\}\}/g,".*");return new RegExp(`^${e}$`).test(t)}function it(t,n,e){let r=n||H();if(e&&r.users[e]){for(let i of r.users[e].neverApprove)if(T(t,i))return  true}for(let i of r.defaults.neverApprove)if(T(t,i))return  true;return  false}function qt(t,n,e){let r=n||H();if(it(t,r,e))return  false;if(e&&r.users[e]){for(let i of r.users[e].autoApprove)if(T(t,i))return  true}for(let i of r.defaults.autoApprove)if(T(t,i))return  true;return  false}function Mt(){let t=st("pending"),n=0;for(let e of t)Date.now()-new Date(e.createdAt).getTime()>X&&(e.status="expired",e.resolvedAt=new Date().toISOString(),s.writeFileSync(A(e.request.requestId),JSON.stringify(e,null,2),"utf-8"),n++);return n}function F(t){try{return process.kill(t,0),!0}catch{return  false}}export{k as A,At as B,Tt as C,L as D,Pt as E,U as F,bt as G,Ft as H,G as I,W as J,H as K,It as L,K as M,st as N,jt as O,kt as P,it as Q,qt as R,Mt as S,$ as a,ct as b,g as c,p as d,ut as e,f,x as g,I as h,z as i,ft as j,et as k,P as l,lt as m,dt as n,pt as o,gt as p,yt as q,ht as r,St as s,mt as t,J as u,xt as v,nt as w,rt as x,vt as y,Rt as z};