cc-safety-net 2.6.17 → 2.6.19

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.
@@ -0,0 +1,4 @@
1
+ import{l,a,ee,h,Ve,p,Qe,E,H,g,G}from"./index-hxp3yxf9.js";import{appendFileSync as re,mkdirSync as oe}from"node:fs";import{isAbsolute as ie,join as S}from"node:path";import{lstatSync as D,readdirSync as U,readFileSync as j,rmdirSync as V,statSync as C,unlinkSync as B,utimesSync as K,writeFileSync as X}from"node:fs";import{join as s}from"node:path";function ue(t,e={}){return Qe(p(t,e),t.home).policy.audit.retention_days}var A=86400000,Y=".last-prune",Z=/^\d{4}-\d{2}$/,J=/^((\d{4}-\d{2})-\d{2})-.+\.jsonl$/,L=(t)=>Math.floor(t/A);function Q(t,e,r=()=>new Date){try{let n=r().getTime();if(!C(e,{throwIfNoEntry:!1})?.isDirectory())return;let o=s(e,Y),c=C(o,{throwIfNoEntry:!1})?.mtimeMs;if(c!==void 0&&L(c)===L(n))return;let i=n-ue(t)*A,d=new Date(n).toISOString().slice(0,7);for(let u of v(e)){if(u.isDirectory()){q(s(e,u.name),i,d);continue}if(u.isFile()&&u.name.endsWith(".jsonl"))te(s(e,u.name),i)}X(o,"",{mode:384}),K(o,n/1000,n/1000)}catch{}}function q(t,e,r){for(let n of v(t)){if(!n.isDirectory()||!Z.test(n.name))continue;let o=s(t,n.name);for(let c of v(o)){if(!c.isFile())continue;let i=J.exec(c.name);if(!i||i[2]!==n.name)continue;let d=Date.parse(`${i[1]}T00:00:00.000Z`)+A;if(!Number.isFinite(d)||d>=e)continue;T(s(o,c.name))}if(n.name!==r)O(o)}O(t)}function te(t,e){try{let r=D(t);if(r.mtimeMs>=e)return;let n=j(t,"utf-8").split(`
2
+ `).filter((o)=>o.trim()).map(ne);if(n.length===0)return;if(n.some((o)=>o===void 0||o>=e))return;if(D(t).mtimeMs!==r.mtimeMs)return;T(t)}catch{}}function ne(t){try{let e=JSON.parse(t).ts,r=typeof e==="string"?Date.parse(e):Number.NaN;return Number.isFinite(r)?r:void 0}catch{return}}function v(t){try{return U(t,{withFileTypes:!0,encoding:"utf8"})}catch{return[]}}function T(t){try{B(t)}catch{}}function O(t){try{V(t)}catch{}}var ae="2.6.19",ce=1e4,de=2000,se=256,le=32768;function me(t){let e=t.trim();if(!e)return null;let r=e.replace(/[^A-Za-z0-9_.-]+/g,"_");if(r=r.replace(/^[._-]+|[._-]+$/g,"").slice(0,128),!r||r==="."||r==="..")return null;return r}function fe(t){return(t??"").replace(/[^A-Za-z0-9]/g,"-").slice(0,180)||"no-cwd"}function I(t,e,r,n,o,c,i={}){let d=me(e);if(!d)return;let u=W(t);if(!u)return;try{let f=(i.now??(()=>new Date))().toISOString(),_=m(ee(r),i.failureStage?Number.POSITIVE_INFINITY:ce),N=m(ee(n),de),w=i.toolName?m(ee(i.toolName),se):void 0,y=c===null?void 0:m(ee(c),le),F=S(u,fe(y?.value??null),f.slice(0,7));oe(F,{recursive:!0,mode:448});let R=S(F,`${f.slice(0,10)}-${d}.jsonl`),P={ts:f,id:(i.createId??Ve)(),v:ae,sessionId:d,decision:i.decision??"deny",agent:i.agent,shape:i.shape,level:i.level,configFallback:i.configFallback,toolName:w?.value,command:_.value,segment:N.value,..._.truncated||N.truncated||w?.truncated||y?.truncated?{truncated:!0}:{},reason:o,ruleId:i.ruleId,intent:i.intent,failureStage:i.failureStage,errorCode:i.errorCode,cwd:y?.value??null};re(R,`${JSON.stringify(P)}
3
+ `,{encoding:"utf-8",mode:384}),Q(t,u,i.now)}catch{}}function m(t,e){return{value:t.slice(0,e),truncated:t.length>e}}function ut(t){let e=t.env.get("CC_SAFETY_NET_AUDIT_HOME");if(t.env.get("NODE_ENV")==="test"&&!e)return null;let r=e||t.home;return r&&ie(r)?r:null}function W(t){let e=ut(t);return e?S(e,".cc-safety-net","logs"):null}function k(t,e,r,n=!0,o){if(e.decision.kind==="allow"){if(!r||t.route.kind!=="command")return;let d=b(t);return{decision:"allow",command:d,segment:d,reason:"allowed",cwd:t.context.executionCwd,toolName:t.toolName,level:e.level,...e.configFallback?{configFallback:!0}:{}}}let c=e.decision.evidence,i=c?.command??(n?b(t):"");return{decision:"deny",command:i,segment:c?.segment??i,reason:e.decision.reason,cwd:t.context.executionCwd,toolName:t.toolName,level:e.level,...e.configFallback?{configFallback:!0}:{},ruleId:e.decision.ruleId,intent:e.decision.intent,failureStage:o?.stage,errorCode:o?.errorCode}}function b(t){return"command"in t?t.command??"":""}function x(t,e,r,n){if(!e)return;let o;try{o=r()}catch{return}if(typeof o!=="string"||!o.trim())return;I(t,o,e.command,e.segment,e.reason,e.cwd,{decision:e.decision,agent:n.agent,shape:n.shape,level:e.level,configFallback:e.configFallback,toolName:e.toolName,ruleId:e.ruleId,intent:e.intent,failureStage:e.failureStage,errorCode:e.errorCode})}function Le(t,e,r,n){let o;try{o=r()}catch{return}if(typeof o!=="string"||!o.trim())return;I(t,o,e.command??"",e.segment??e.command??"",e.reason,n.cwd??null,{decision:"deny",agent:n.agent,shape:n.shape,toolName:n.toolName??e.toolName,ruleId:e.ruleId,intent:e.intent})}function z(t,e,r){try{let n=G(e,{environment:t,...r.guard});return M(t,e,n,r),n}catch(n){if(!(n instanceof g))throw n;throw M(t,e,n.evaluation,r,!(n.cause instanceof h),{stage:n.stage,errorCode:ge(n.cause)}),n}}function M(t,e,r,n,o=!0,c){x(t,k(r.promotedInvocation??e,r,n.guard?.auditAllowed??!1,o,c),n.audit.getSessionId,{agent:n.audit.agent,shape:n.audit.shape})}function ge(t){if(t instanceof a)return l[t.kind].errorCode;if(t instanceof h)return"tool-input-limit";if(t instanceof E)return"structural-shell-syntax-limit";if(t instanceof H)return"unparseable-command";return"unexpected-error"}
4
+ export{ue,Q,ut,W,Le,z};
@@ -0,0 +1,2 @@
1
+ import{Me,te,V,I,_t,c,r,Fe,k,g}from"./index-hxp3yxf9.js";import{Le,z}from"./index-pdfz2d83.js";function X(n,o={}){return{malformed:!0,denial:V(o),cwd:n}}function De(n,o={}){return{malformed:!0,denial:te(o),cwd:n}}function be(n,o){return{malformed:!0,denial:I(n,o),cwd:n.cwd}}function Ne(n,o={}){return(e,i)=>{try{return p(e,i,n,o)}catch(a){return console.error("CC Safety Net error:",a),n.block(te())}}}function p(n,o,e,i){let a=c();if(e.cancelled?.(o))return e.block(te());let t=e.extract(n,o,a.paths);if(!t)return e.allow;let d=()=>e.getSessionId(n,o);if("malformed"in t)return Le(a,t.denial,d,{agent:e.agent,toolName:t.denial.toolName,cwd:t.cwd}),e.block(t.denial);let u=(l,s)=>{let m=Me(l,{includeEvidence:s});return m?e.block(m):e.allow};try{let l=z(a,t,{guard:{auditAllowed:Fe(a.env),policyOptions:i.policyOptions,dependencies:i.guardDependencies},audit:{agent:e.agent,getSessionId:d}});return u(l,!0)}catch(l){if(!(l instanceof g))throw l;if(k(r.debug,a.env))console.error(`CC Safety Net debug: ${e.debugLabel} analysis failed: ${_t(l.cause)}`);return u(l.evaluation,e.includeEvidenceOnError(t))}}
2
+ export{X,De,be,Ne};
@@ -0,0 +1,2 @@
1
+ import{ne,c,b,mt,d,w}from"./index-hxp3yxf9.js";import{X,De,be,Ne}from"./index-xkgawhr8.js";import{randomUUID as E}from"node:crypto";import{resolve as O}from"node:path";import{readdirSync as P}from"node:fs";import{join as k,resolve as T}from"node:path";import{existsSync as S,lstatSync as x,readFileSync as H}from"node:fs";import{join as C}from"node:path";function v(e,t=(s)=>s){if(!S(e))return{kind:"missing"};try{return{kind:"ok",value:JSON.parse(t(H(e,"utf-8")))}}catch{return{kind:"unreadable"}}}function B(e,t){if(e==="~")return t;if(e.startsWith("~/")||e.startsWith("~\\"))return C(t,e.slice(2));return e}function u(e){try{return x(e)}catch{return}}function Be(e,t){let s=u(t);if(!s)return{platform:e,status:"n/a",configPath:t};if(!s.isSymbolicLink()&&s.isDirectory())return;return{platform:e,status:"n/a",configPath:t,errors:[`${t} is a symlink or not a directory; move or remove it before installing`]}}function n(e,t){return typeof e==="object"&&e!==null?e[t]:void 0}var h="cc-safety-net";function Oe(e){let t=e.env.get("DSH_HOME");return k(t?.trim()?T(B(t,e.home)):k(e.home,".dsh"),"profiles")}function D(e){let t=Oe(e),s=u(t)?.isDirectory()?P(t,{withFileTypes:!0}).filter((o)=>o.isDirectory()&&o.name!=="node_modules").map((o)=>{let r=k(t,o.name,"package.json");return{name:o.name,configPath:r,manifest:v(r)}}):[];return{installed:s.flatMap((o)=>{if(o.manifest.kind!=="ok")return[];let r=o.manifest.value;if(n(n(r,"dependencies"),h)===void 0)return[];let i=n(n(n(r,"dsh"),"profile"),"bundles");return[{name:o.name,configPath:o.configPath,enabled:Array.isArray(i)&&i.includes(h)}]}),unreadable:s.some((o)=>o.manifest.kind==="unreadable")}}function lt(e){return D(e).installed}function Rt(e){let t=D(e.environment),s=t.installed.filter((r)=>r.enabled),a=t.installed.filter((r)=>!r.enabled),o=a.map((r)=>`${h} is installed in the ${r.name} profile but its bundle is disabled`);if(s.length>0)return{platform:"deepseek-harness",status:"configured",method:"dsh bundle",configPaths:s.map((r)=>r.configPath),...o.length>0?{errors:o}:{}};if(a.length>0)return{platform:"deepseek-harness",status:"disabled",method:"dsh bundle",configPaths:a.map((r)=>r.configPath),errors:o};return t.unreadable?{platform:"deepseek-harness",status:"not-inspected"}:{platform:"deepseek-harness",status:"n/a"}}var We=new Map([["bash","posix"],["pwsh","powershell"]]),j={agent:"deepseek-harness",debugLabel:"deepseek-harness tool guard",extract:(e,t,s)=>_(e,t,s),getSessionId:(e)=>e?.agent?.session.header.id,allow:void 0,block:(e)=>ne(e),includeEvidenceOnError:(e)=>e.route.kind==="command"};function Tt(e){let t=I();e.tools.guard((a)=>t(a,process.cwd()));let s=new Set;e.on("tools/post-execute",async(a,o,r)=>{let i=await r(),l=A(a,s);if(!l)return i;return{...i,additionalContexts:[l,...i.additionalContexts??[]]}})}function A(e,t){try{let s=e,a=s?.agent?.session.header,o=s?.name,i=s?.arguments?.command;if(!a||typeof o!=="string"||We.has(o)||b(o)!=="unknown"||typeof i!=="string"||i.trim()==="")return;let l=JSON.stringify([a.id,o]);if(t.has(l))return;let p=c();if(w(p,{cwd:a.cwd??process.cwd()}).policy.commandTools.some((m)=>m.name===o))return;t.add(l);let f=Oe(p);return Object.freeze({id:E(),role:"user",content:Object.freeze([Object.freeze({type:"text",text:`CC Safety Net did not check the command \`${o}\` just ran. It checks a plugin's shell tool only after the tool is declared. First find out where \`${o}\` runs its commands: check its tool description, and if that doesn't say, read its plugin's source under \`${f}/<profile>/node_modules\`. If it runs them on this machine, declare it with \`npx -y cc-safety-net rule tool add ${o}\`. Add \`--cwd-arg <name>\` if it takes its working directory in an argument other than \`workdir\`, \`cwd\` or \`working_directory\`. Don't test the declaration by running a destructive command; \`npx -y cc-safety-net rule tool list\` confirms it. If it runs them somewhere else, such as a remote host, or you can't tell, don't declare it. Either way, tell the user what you found and what you did.`})]),source:Object.freeze({kind:"cc-safety-net",form:"notice",summary:`${o} commands are not checked`})})}catch{return}}function I(e={}){return Ne(j,e)}function _(e,t,s){if(!e||typeof e!=="object")return De(null);let a=e,o=a.agent?.session.header.cwd??t,r=mt([o],s),i=a.name;if(typeof i!=="string"||i.trim()==="")return X(r??null);if(!r)return be({directory:"session",problem:"unusable",cwd:o},{toolName:i});let l=a.arguments,p=We.get(i);if(!p)return d(i,l,{kind:b(i)},{configCwd:r,executionCwd:r},null);if(!l||typeof l!=="object"||Array.isArray(l))return X(r,{toolName:i});let g=l,f=g.command;if(typeof f!=="string"||f.trim()==="")return X(r,{toolName:i});let m=g.workdir;if(m!==void 0&&(typeof m!=="string"||m.trim()===""))return X(r,{command:f,toolName:i});let y=m===void 0?r:mt([O(r,m)],s);if(!y)return be({directory:"requested",problem:"unusable",cwd:m},{command:f,toolName:i});return d(i,l,{kind:"command",shell:p},{configCwd:r,executionCwd:y},f)}
2
+ export{v,B,u,Be,n,Oe,lt,Rt,We,Tt};